1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 Atree
; use Atree
;
27 with Debug
; use Debug
;
28 with Einfo
; use Einfo
;
29 with Errout
; use Errout
;
30 with Exp_Util
; use Exp_Util
;
31 with Fname
; use Fname
;
32 with Fname
.UF
; use Fname
.UF
;
33 with Freeze
; use Freeze
;
34 with Impunit
; use Impunit
;
35 with Inline
; use Inline
;
37 with Lib
.Load
; use Lib
.Load
;
38 with Lib
.Xref
; use Lib
.Xref
;
39 with Namet
; use Namet
;
40 with Nlists
; use Nlists
;
41 with Nmake
; use Nmake
;
43 with Output
; use Output
;
44 with Restrict
; use Restrict
;
45 with Rident
; use Rident
;
46 with Rtsfind
; use Rtsfind
;
48 with Sem_Ch3
; use Sem_Ch3
;
49 with Sem_Ch6
; use Sem_Ch6
;
50 with Sem_Ch7
; use Sem_Ch7
;
51 with Sem_Ch8
; use Sem_Ch8
;
52 with Sem_Dist
; use Sem_Dist
;
53 with Sem_Prag
; use Sem_Prag
;
54 with Sem_Util
; use Sem_Util
;
55 with Sem_Warn
; use Sem_Warn
;
56 with Stand
; use Stand
;
57 with Sinfo
; use Sinfo
;
58 with Sinfo
.CN
; use Sinfo
.CN
;
59 with Sinput
; use Sinput
;
60 with Snames
; use Snames
;
61 with Style
; use Style
;
62 with Stylesw
; use Stylesw
;
63 with Tbuild
; use Tbuild
;
64 with Uname
; use Uname
;
66 package body Sem_Ch10
is
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Analyze_Context
(N
: Node_Id
);
73 -- Analyzes items in the context clause of compilation unit
75 procedure Build_Limited_Views
(N
: Node_Id
);
76 -- Build and decorate the list of shadow entities for a package mentioned
77 -- in a limited_with clause. If the package was not previously analyzed
78 -- then it also performs a basic decoration of the real entities; this
79 -- is required to do not pass non-decorated entities to the back-end.
80 -- Implements Ada 2005 (AI-50217).
82 procedure Check_Body_Needed_For_SAL
(Unit_Name
: Entity_Id
);
83 -- Check whether the source for the body of a compilation unit must
84 -- be included in a standalone library.
86 procedure Check_Private_Child_Unit
(N
: Node_Id
);
87 -- If a with_clause mentions a private child unit, the compilation
88 -- unit must be a member of the same family, as described in 10.1.2 (8).
90 procedure Check_Stub_Level
(N
: Node_Id
);
91 -- Verify that a stub is declared immediately within a compilation unit,
92 -- and not in an inner frame.
94 procedure Expand_With_Clause
(Item
: Node_Id
; Nam
: Node_Id
; N
: Node_Id
);
95 -- When a child unit appears in a context clause, the implicit withs on
96 -- parents are made explicit, and with clauses are inserted in the context
97 -- clause before the one for the child. If a parent in the with_clause
98 -- is a renaming, the implicit with_clause is on the renaming whose name
99 -- is mentioned in the with_clause, and not on the package it renames.
100 -- N is the compilation unit whose list of context items receives the
101 -- implicit with_clauses.
103 function Get_Parent_Entity
(Unit
: Node_Id
) return Entity_Id
;
104 -- Get defining entity of parent unit of a child unit. In most cases this
105 -- is the defining entity of the unit, but for a child instance whose
106 -- parent needs a body for inlining, the instantiation node of the parent
107 -- has not yet been rewritten as a package declaration, and the entity has
108 -- to be retrieved from the Instance_Spec of the unit.
110 procedure Implicit_With_On_Parent
(Child_Unit
: Node_Id
; N
: Node_Id
);
111 -- If the main unit is a child unit, implicit withs are also added for
112 -- all its ancestors.
114 function In_Chain
(E
: Entity_Id
) return Boolean;
115 -- Check that the shadow entity is not already in the homonym chain, for
116 -- example through a limited_with clause in a parent unit.
118 procedure Install_Context_Clauses
(N
: Node_Id
);
119 -- Subsidiary to Install_Context and Install_Parents. Process only with_
120 -- and use_clauses for current unit and its library unit if any.
122 procedure Install_Limited_Context_Clauses
(N
: Node_Id
);
123 -- Subsidiary to Install_Context. Process only limited with_clauses
124 -- for current unit. Implements Ada 2005 (AI-50217).
126 procedure Install_Limited_Withed_Unit
(N
: Node_Id
);
127 -- Place shadow entities for a limited_with package in the visibility
128 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
130 procedure Install_Withed_Unit
131 (With_Clause
: Node_Id
;
132 Private_With_OK
: Boolean := False);
133 -- If the unit is not a child unit, make unit immediately visible.
134 -- The caller ensures that the unit is not already currently installed.
135 -- The flag Private_With_OK is set true in Install_Private_With_Clauses,
136 -- which is called when compiling the private part of a package, or
137 -- installing the private declarations of a parent unit.
139 procedure Install_Parents
(Lib_Unit
: Node_Id
; Is_Private
: Boolean);
140 -- This procedure establishes the context for the compilation of a child
141 -- unit. If Lib_Unit is a child library spec then the context of the parent
142 -- is installed, and the parent itself made immediately visible, so that
143 -- the child unit is processed in the declarative region of the parent.
144 -- Install_Parents makes a recursive call to itself to ensure that all
145 -- parents are loaded in the nested case. If Lib_Unit is a library body,
146 -- the only effect of Install_Parents is to install the private decls of
147 -- the parents, because the visible parent declarations will have been
148 -- installed as part of the context of the corresponding spec.
150 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
);
151 -- In the compilation of a child unit, a child of any of the ancestor
152 -- units is directly visible if it is visible, because the parent is in
153 -- an enclosing scope. Iterate over context to find child units of U_Name
154 -- or of some ancestor of it.
156 function Is_Child_Spec
(Lib_Unit
: Node_Id
) return Boolean;
157 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
158 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
159 -- a library spec that has a parent. If the call to Is_Child_Spec returns
160 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
161 -- compilation unit for the parent spec.
163 -- Lib_Unit can also be a subprogram body that acts as its own spec. If
164 -- the Parent_Spec is non-empty, this is also a child unit.
166 procedure Remove_Context_Clauses
(N
: Node_Id
);
167 -- Subsidiary of previous one. Remove use_ and with_clauses
169 procedure Remove_Limited_With_Clause
(N
: Node_Id
);
170 -- Remove from visibility the shadow entities introduced for a package
171 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
173 procedure Remove_Parents
(Lib_Unit
: Node_Id
);
174 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
175 -- contexts established by the corresponding call to Install_Parents are
176 -- removed. Remove_Parents contains a recursive call to itself to ensure
177 -- that all parents are removed in the nested case.
179 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
);
180 -- Reset all visibility flags on unit after compiling it, either as a
181 -- main unit or as a unit in the context.
183 procedure Unchain
(E
: Entity_Id
);
184 -- Remove single entity from visibility list
186 procedure Analyze_Proper_Body
(N
: Node_Id
; Nam
: Entity_Id
);
187 -- Common processing for all stubs (subprograms, tasks, packages, and
188 -- protected cases). N is the stub to be analyzed. Once the subunit
189 -- name is established, load and analyze. Nam is the non-overloadable
190 -- entity for which the proper body provides a completion. Subprogram
191 -- stubs are handled differently because they can be declarations.
194 -- A dummy procedure, for debugging use, called just before analyzing the
195 -- main unit (after dealing with any context clauses).
197 --------------------------
198 -- Limited_With_Clauses --
199 --------------------------
201 -- Limited_With clauses are the mechanism chosen for Ada05 to support
202 -- mutually recursive types declared in different units. A limited_with
203 -- clause that names package P in the context of unit U makes the types
204 -- declared in the visible part of P available within U, but with the
205 -- restriction that these types can only be used as incomplete types.
206 -- The limited_with clause does not impose a semantic dependence on P,
207 -- and it is possible for two packages to have limited_with_clauses on
208 -- each other without creating an elaboration circularity.
210 -- To support this feature, the analysis of a limited_with clause must
211 -- create an abbreviated view of the package, without performing any
212 -- semantic analysis on it. This "package abstract" contains shadow
213 -- types that are in one-one correspondence with the real types in the
214 -- package, and that have the properties of incomplete types.
216 -- The implementation creates two element lists: one to chain the shadow
217 -- entities, and one to chain the corresponding type entities in the tree
218 -- of the package. Links between corresponding entities in both chains
219 -- allow the compiler to select the proper view of a given type, depending
220 -- on the context. Note that in contrast with the handling of private
221 -- types, the limited view and the non-limited view of a type are treated
222 -- as separate entities, and no entity exchange needs to take place, which
223 -- makes the implementation must simpler than could be feared.
225 ------------------------------
226 -- Analyze_Compilation_Unit --
227 ------------------------------
229 procedure Analyze_Compilation_Unit
(N
: Node_Id
) is
230 Unit_Node
: constant Node_Id
:= Unit
(N
);
231 Lib_Unit
: Node_Id
:= Library_Unit
(N
);
233 Main_Cunit
: constant Node_Id
:= Cunit
(Main_Unit
);
234 Par_Spec_Name
: Unit_Name_Type
;
235 Unum
: Unit_Number_Type
;
237 procedure Check_Redundant_Withs
238 (Context_Items
: List_Id
;
239 Spec_Context_Items
: List_Id
:= No_List
);
240 -- Determine whether the context list of a compilation unit contains
241 -- redundant with clauses. When checking body clauses against spec
242 -- clauses, set Context_Items to the context list of the body and
243 -- Spec_Context_Items to that of the spec. Parent packages are not
244 -- examined for documentation purposes.
246 procedure Generate_Parent_References
(N
: Node_Id
; P_Id
: Entity_Id
);
247 -- Generate cross-reference information for the parents of child units.
248 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
250 ---------------------------
251 -- Check_Redundant_Withs --
252 ---------------------------
254 procedure Check_Redundant_Withs
255 (Context_Items
: List_Id
;
256 Spec_Context_Items
: List_Id
:= No_List
)
260 procedure Process_Body_Clauses
261 (Context_List
: List_Id
;
263 Used
: in out Boolean;
264 Used_Type_Or_Elab
: in out Boolean);
265 -- Examine the context clauses of a package body, trying to match
266 -- the name entity of Clause with any list element. If the match
267 -- occurs on a use package clause, set Used to True, for a use
268 -- type clause, pragma Elaborate or pragma Elaborate_All, set
269 -- Used_Type_Or_Elab to True.
271 procedure Process_Spec_Clauses
272 (Context_List
: List_Id
;
274 Used
: in out Boolean;
275 Withed
: in out Boolean;
276 Exit_On_Self
: Boolean := False);
277 -- Examine the context clauses of a package spec, trying to match
278 -- the name entity of Clause with any list element. If the match
279 -- occurs on a use package clause, set Used to True, for a with
280 -- package clause other than Clause, set Withed to True. Limited
281 -- with clauses, implicitly generated with clauses and withs
282 -- having pragmas Elaborate or Elaborate_All applied to them are
283 -- skipped. Exit_On_Self is used to control the search loop and
284 -- force an exit whenever Clause sees itself in the search.
286 --------------------------
287 -- Process_Body_Clauses --
288 --------------------------
290 procedure Process_Body_Clauses
291 (Context_List
: List_Id
;
293 Used
: in out Boolean;
294 Used_Type_Or_Elab
: in out Boolean)
296 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
302 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean;
303 -- In an expanded name in a use clause, if the prefix is a
304 -- renamed package, the entity is set to the original package
305 -- as a result, when checking whether the package appears in a
306 -- previous with_clause, the renaming has to be taken into
307 -- account, to prevent spurious or incorrect warnings. The
308 -- common case is the use of Text_IO.
314 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean is
316 return Entity
(N
) = P
318 (Present
(Renamed_Object
(P
))
319 and then Entity
(N
) = Renamed_Object
(P
));
322 -- Start of processing for Process_Body_Clauses
326 Used_Type_Or_Elab
:= False;
328 Cont_Item
:= First
(Context_List
);
329 while Present
(Cont_Item
) loop
331 -- Package use clause
333 if Nkind
(Cont_Item
) = N_Use_Package_Clause
336 -- Search through use clauses
338 Use_Item
:= First
(Names
(Cont_Item
));
339 while Present
(Use_Item
) and then not Used
loop
341 -- Case of a direct use of the one we are looking for
343 if Entity
(Use_Item
) = Nam_Ent
then
346 -- Handle nested case, as in "with P; use P.Q.R"
353 -- Loop through prefixes looking for match
356 while Nkind
(UE
) = N_Expanded_Name
loop
357 if Same_Unit
(Prefix
(UE
), Nam_Ent
) then
372 elsif Nkind
(Cont_Item
) = N_Use_Type_Clause
373 and then not Used_Type_Or_Elab
375 Subt_Mark
:= First
(Subtype_Marks
(Cont_Item
));
376 while Present
(Subt_Mark
)
377 and then not Used_Type_Or_Elab
379 if Same_Unit
(Prefix
(Subt_Mark
), Nam_Ent
) then
380 Used_Type_Or_Elab
:= True;
386 -- Pragma Elaborate or Elaborate_All
388 elsif Nkind
(Cont_Item
) = N_Pragma
390 (Chars
(Cont_Item
) = Name_Elaborate
392 Chars
(Cont_Item
) = Name_Elaborate_All
)
393 and then not Used_Type_Or_Elab
396 First
(Pragma_Argument_Associations
(Cont_Item
));
397 while Present
(Prag_Unit
)
398 and then not Used_Type_Or_Elab
400 if Entity
(Expression
(Prag_Unit
)) = Nam_Ent
then
401 Used_Type_Or_Elab
:= True;
410 end Process_Body_Clauses
;
412 --------------------------
413 -- Process_Spec_Clauses --
414 --------------------------
416 procedure Process_Spec_Clauses
417 (Context_List
: List_Id
;
419 Used
: in out Boolean;
420 Withed
: in out Boolean;
421 Exit_On_Self
: Boolean := False)
423 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
431 Cont_Item
:= First
(Context_List
);
432 while Present
(Cont_Item
) loop
434 -- Stop the search since the context items after Cont_Item
435 -- have already been examined in a previous iteration of
436 -- the reverse loop in Check_Redundant_Withs.
439 and Cont_Item
= Clause
444 -- Package use clause
446 if Nkind
(Cont_Item
) = N_Use_Package_Clause
449 Use_Item
:= First
(Names
(Cont_Item
));
450 while Present
(Use_Item
) and then not Used
loop
451 if Entity
(Use_Item
) = Nam_Ent
then
458 -- Package with clause. Avoid processing self, implicitly
459 -- generated with clauses or limited with clauses. Note
460 -- that we examine with clauses having pragmas Elaborate
461 -- or Elaborate_All applied to them due to cases such as:
465 -- pragma Elaborate (Pack);
467 -- In this case, the second with clause is redundant since
468 -- the pragma applies only to the first "with Pack;".
470 elsif Nkind
(Cont_Item
) = N_With_Clause
471 and then not Implicit_With
(Cont_Item
)
472 and then not Limited_Present
(Cont_Item
)
473 and then Cont_Item
/= Clause
474 and then Entity
(Name
(Cont_Item
)) = Nam_Ent
481 end Process_Spec_Clauses
;
483 -- Start of processing for Check_Redundant_Withs
486 Clause
:= Last
(Context_Items
);
487 while Present
(Clause
) loop
489 -- Avoid checking implicitly generated with clauses, limited
490 -- with clauses or withs that have pragma Elaborate or
491 -- Elaborate_All apllied.
493 if Nkind
(Clause
) = N_With_Clause
494 and then not Implicit_With
(Clause
)
495 and then not Limited_Present
(Clause
)
496 and then not Elaborate_Present
(Clause
)
498 -- Package body-to-spec check
500 if Present
(Spec_Context_Items
) then
502 Used_In_Body
: Boolean := False;
503 Used_In_Spec
: Boolean := False;
504 Used_Type_Or_Elab
: Boolean := False;
505 Withed_In_Spec
: Boolean := False;
509 (Context_List
=> Spec_Context_Items
,
511 Used
=> Used_In_Spec
,
512 Withed
=> Withed_In_Spec
);
515 (Context_List
=> Context_Items
,
517 Used
=> Used_In_Body
,
518 Used_Type_Or_Elab
=> Used_Type_Or_Elab
);
520 -- "Type Elab" refers to the presence of either a use
521 -- type clause, pragmas Elaborate or Elaborate_All.
523 -- +---------------+---------------------------+------+
524 -- | Spec | Body | Warn |
525 -- +--------+------+--------+------+-----------+------+
526 -- | Withed | Used | Withed | Used | Type Elab | |
527 -- | X | | X | | | X |
528 -- | X | | X | X | | |
529 -- | X | | X | | X | |
530 -- | X | | X | X | X | |
531 -- | X | X | X | | | X |
532 -- | X | X | X | | X | |
533 -- | X | X | X | X | | X |
534 -- | X | X | X | X | X | |
535 -- +--------+------+--------+------+-----------+------+
538 and then not Used_Type_Or_Elab
)
541 and then not Used_In_Body
)
545 Error_Msg_N
("?redundant with clause in body", Clause
);
548 Used_In_Body
:= False;
549 Used_In_Spec
:= False;
550 Used_Type_Or_Elab
:= False;
551 Withed_In_Spec
:= False;
554 -- Standalone package spec or body check
558 Dont_Care
: Boolean := False;
559 Withed
: Boolean := False;
562 -- The mechanism for examining the context clauses of a
563 -- package spec can be applied to package body clauses.
566 (Context_List
=> Context_Items
,
570 Exit_On_Self
=> True);
573 Error_Msg_N
("?redundant with clause", Clause
);
581 end Check_Redundant_Withs
;
583 --------------------------------
584 -- Generate_Parent_References --
585 --------------------------------
587 procedure Generate_Parent_References
(N
: Node_Id
; P_Id
: Entity_Id
) is
589 P_Name
: Entity_Id
:= P_Id
;
592 Pref
:= Name
(Parent
(Defining_Entity
(N
)));
594 if Nkind
(Pref
) = N_Expanded_Name
then
596 -- Done already, if the unit has been compiled indirectly as
597 -- part of the closure of its context because of inlining.
602 while Nkind
(Pref
) = N_Selected_Component
loop
603 Change_Selected_Component_To_Expanded_Name
(Pref
);
604 Set_Entity
(Pref
, P_Name
);
605 Set_Etype
(Pref
, Etype
(P_Name
));
606 Generate_Reference
(P_Name
, Pref
, 'r');
607 Pref
:= Prefix
(Pref
);
608 P_Name
:= Scope
(P_Name
);
611 -- The guard here on P_Name is to handle the error condition where
612 -- the parent unit is missing because the file was not found.
614 if Present
(P_Name
) then
615 Set_Entity
(Pref
, P_Name
);
616 Set_Etype
(Pref
, Etype
(P_Name
));
617 Generate_Reference
(P_Name
, Pref
, 'r');
618 Style
.Check_Identifier
(Pref
, P_Name
);
620 end Generate_Parent_References
;
622 -- Start of processing for Analyze_Compilation_Unit
625 Process_Compilation_Unit_Pragmas
(N
);
627 -- If the unit is a subunit whose parent has not been analyzed (which
628 -- indicates that the main unit is a subunit, either the current one or
629 -- one of its descendents) then the subunit is compiled as part of the
630 -- analysis of the parent, which we proceed to do. Basically this gets
631 -- handled from the top down and we don't want to do anything at this
632 -- level (i.e. this subunit will be handled on the way down from the
633 -- parent), so at this level we immediately return. If the subunit
634 -- ends up not analyzed, it means that the parent did not contain a
635 -- stub for it, or that there errors were dectected in some ancestor.
637 if Nkind
(Unit_Node
) = N_Subunit
638 and then not Analyzed
(Lib_Unit
)
640 Semantics
(Lib_Unit
);
642 if not Analyzed
(Proper_Body
(Unit_Node
)) then
643 if Serious_Errors_Detected
> 0 then
644 Error_Msg_N
("subunit not analyzed (errors in parent unit)", N
);
646 Error_Msg_N
("missing stub for subunit", N
);
653 -- Analyze context (this will call Sem recursively for with'ed units)
657 -- If the unit is a package body, the spec is already loaded and must
658 -- be analyzed first, before we analyze the body.
660 if Nkind
(Unit_Node
) = N_Package_Body
then
662 -- If no Lib_Unit, then there was a serious previous error, so
663 -- just ignore the entire analysis effort
665 if No
(Lib_Unit
) then
669 Semantics
(Lib_Unit
);
670 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
672 -- Verify that the library unit is a package declaration
674 if Nkind
(Unit
(Lib_Unit
)) /= N_Package_Declaration
676 Nkind
(Unit
(Lib_Unit
)) /= N_Generic_Package_Declaration
679 ("no legal package declaration for package body", N
);
682 -- Otherwise, the entity in the declaration is visible. Update
683 -- the version to reflect dependence of this body on the spec.
686 Spec_Id
:= Defining_Entity
(Unit
(Lib_Unit
));
687 Set_Is_Immediately_Visible
(Spec_Id
, True);
688 Version_Update
(N
, Lib_Unit
);
690 if Nkind
(Defining_Unit_Name
(Unit_Node
))
691 = N_Defining_Program_Unit_Name
693 Generate_Parent_References
(Unit_Node
, Scope
(Spec_Id
));
698 -- If the unit is a subprogram body, then we similarly need to analyze
699 -- its spec. However, things are a little simpler in this case, because
700 -- here, this analysis is done only for error checking and consistency
701 -- purposes, so there's nothing else to be done.
703 elsif Nkind
(Unit_Node
) = N_Subprogram_Body
then
704 if Acts_As_Spec
(N
) then
706 -- If the subprogram body is a child unit, we must create a
707 -- declaration for it, in order to properly load the parent(s).
708 -- After this, the original unit does not acts as a spec, because
709 -- there is an explicit one. If this unit appears in a context
710 -- clause, then an implicit with on the parent will be added when
711 -- installing the context. If this is the main unit, there is no
712 -- Unit_Table entry for the declaration (it has the unit number
713 -- of the main unit) and code generation is unaffected.
715 Unum
:= Get_Cunit_Unit_Number
(N
);
716 Par_Spec_Name
:= Get_Parent_Spec_Name
(Unit_Name
(Unum
));
718 if Par_Spec_Name
/= No_Unit_Name
then
721 (Load_Name
=> Par_Spec_Name
,
726 if Unum
/= No_Unit
then
728 -- Build subprogram declaration and attach parent unit to it
729 -- This subprogram declaration does not come from source,
730 -- Nevertheless the backend must generate debugging info for
731 -- it, and this must be indicated explicitly. We also mark
732 -- the body entity as a child unit now, to prevent a
733 -- cascaded error if the spec entity cannot be entered
737 Loc
: constant Source_Ptr
:= Sloc
(N
);
738 SCS
: constant Boolean :=
739 Get_Comes_From_Source_Default
;
742 Set_Comes_From_Source_Default
(False);
744 Make_Compilation_Unit
(Loc
,
745 Context_Items
=> New_Copy_List
(Context_Items
(N
)),
747 Make_Subprogram_Declaration
(Sloc
(N
),
750 (Specification
(Unit_Node
))),
752 Make_Compilation_Unit_Aux
(Loc
));
754 Set_Library_Unit
(N
, Lib_Unit
);
755 Set_Parent_Spec
(Unit
(Lib_Unit
), Cunit
(Unum
));
756 Semantics
(Lib_Unit
);
758 -- Now that a separate declaration exists, the body
759 -- of the child unit does not act as spec any longer.
761 Set_Acts_As_Spec
(N
, False);
762 Set_Is_Child_Unit
(Defining_Entity
(Unit_Node
));
763 Set_Needs_Debug_Info
(Defining_Entity
(Unit
(Lib_Unit
)));
764 Set_Comes_From_Source_Default
(SCS
);
769 -- Here for subprogram with separate declaration
772 Semantics
(Lib_Unit
);
773 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
774 Version_Update
(N
, Lib_Unit
);
777 if Nkind
(Defining_Unit_Name
(Specification
(Unit_Node
))) =
778 N_Defining_Program_Unit_Name
780 Generate_Parent_References
(
781 Specification
(Unit_Node
),
782 Scope
(Defining_Entity
(Unit
(Lib_Unit
))));
786 -- If it is a child unit, the parent must be elaborated first
787 -- and we update version, since we are dependent on our parent.
789 if Is_Child_Spec
(Unit_Node
) then
791 -- The analysis of the parent is done with style checks off
794 Save_Style_Check
: constant Boolean := Style_Check
;
795 Save_C_Restrict
: constant Save_Cunit_Boolean_Restrictions
:=
796 Cunit_Boolean_Restrictions_Save
;
799 if not GNAT_Mode
then
800 Style_Check
:= False;
803 Semantics
(Parent_Spec
(Unit_Node
));
804 Version_Update
(N
, Parent_Spec
(Unit_Node
));
805 Style_Check
:= Save_Style_Check
;
806 Cunit_Boolean_Restrictions_Restore
(Save_C_Restrict
);
810 -- With the analysis done, install the context. Note that we can't
811 -- install the context from the with clauses as we analyze them, because
812 -- each with clause must be analyzed in a clean visibility context, so
813 -- we have to wait and install them all at once.
817 if Is_Child_Spec
(Unit_Node
) then
819 -- Set the entities of all parents in the program_unit_name
821 Generate_Parent_References
(
822 Unit_Node
, Get_Parent_Entity
(Unit
(Parent_Spec
(Unit_Node
))));
825 -- All components of the context: with-clauses, library unit, ancestors
826 -- if any, (and their context) are analyzed and installed.
828 -- Call special debug routine sm if this is the main unit
830 if Current_Sem_Unit
= Main_Unit
then
834 -- Now analyze the unit (package, subprogram spec, body) itself
838 if Warn_On_Redundant_Constructs
then
839 Check_Redundant_Withs
(Context_Items
(N
));
841 if Nkind
(Unit_Node
) = N_Package_Body
then
842 Check_Redundant_Withs
843 (Context_Items
=> Context_Items
(N
),
844 Spec_Context_Items
=> Context_Items
(Lib_Unit
));
848 -- The above call might have made Unit_Node an N_Subprogram_Body from
849 -- something else, so propagate any Acts_As_Spec flag.
851 if Nkind
(Unit_Node
) = N_Subprogram_Body
852 and then Acts_As_Spec
(Unit_Node
)
854 Set_Acts_As_Spec
(N
);
857 -- Register predefined units in Rtsfind
860 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(Sloc
(N
));
862 if Is_Predefined_File_Name
(Unit_File_Name
(Unum
)) then
863 Set_RTU_Loaded
(Unit_Node
);
867 -- Treat compilation unit pragmas that appear after the library unit
869 if Present
(Pragmas_After
(Aux_Decls_Node
(N
))) then
871 Prag_Node
: Node_Id
:= First
(Pragmas_After
(Aux_Decls_Node
(N
)));
873 while Present
(Prag_Node
) loop
880 -- Generate distribution stubs if requested and no error
883 and then (Distribution_Stub_Mode
= Generate_Receiver_Stub_Body
885 Distribution_Stub_Mode
= Generate_Caller_Stub_Body
)
886 and then not Fatal_Error
(Main_Unit
)
888 if Is_RCI_Pkg_Spec_Or_Body
(N
) then
890 -- Regular RCI package
892 Add_Stub_Constructs
(N
);
894 elsif (Nkind
(Unit_Node
) = N_Package_Declaration
895 and then Is_Shared_Passive
(Defining_Entity
896 (Specification
(Unit_Node
))))
897 or else (Nkind
(Unit_Node
) = N_Package_Body
899 Is_Shared_Passive
(Corresponding_Spec
(Unit_Node
)))
901 -- Shared passive package
903 Add_Stub_Constructs
(N
);
905 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
907 Is_Remote_Call_Interface
908 (Defining_Entity
(Specification
(Instance_Spec
(Unit_Node
))))
910 -- Instantiation of a RCI generic package
912 Add_Stub_Constructs
(N
);
917 -- Remove unit from visibility, so that environment is clean for
918 -- the next compilation, which is either the main unit or some
919 -- other unit in the context.
921 if Nkind
(Unit_Node
) = N_Package_Declaration
922 or else Nkind
(Unit_Node
) in N_Generic_Declaration
923 or else Nkind
(Unit_Node
) = N_Package_Renaming_Declaration
924 or else Nkind
(Unit_Node
) = N_Subprogram_Declaration
926 (Nkind
(Unit_Node
) = N_Subprogram_Body
927 and then Acts_As_Spec
(Unit_Node
))
929 Remove_Unit_From_Visibility
(Defining_Entity
(Unit_Node
));
931 -- If the unit is an instantiation whose body will be elaborated for
932 -- inlining purposes, use the the proper entity of the instance. The
933 -- entity may be missing if the instantiation was illegal.
935 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
936 and then not Error_Posted
(Unit_Node
)
937 and then Present
(Instance_Spec
(Unit_Node
))
939 Remove_Unit_From_Visibility
940 (Defining_Entity
(Instance_Spec
(Unit_Node
)));
942 elsif Nkind
(Unit_Node
) = N_Package_Body
943 or else (Nkind
(Unit_Node
) = N_Subprogram_Body
944 and then not Acts_As_Spec
(Unit_Node
))
946 -- Bodies that are not the main unit are compiled if they are generic
947 -- or contain generic or inlined units. Their analysis brings in the
948 -- context of the corresponding spec (unit declaration) which must be
949 -- removed as well, to return the compilation environment to its
952 Remove_Context
(Lib_Unit
);
953 Set_Is_Immediately_Visible
(Defining_Entity
(Unit
(Lib_Unit
)), False);
956 -- Last step is to deinstall the context we just installed as well as
957 -- the unit just compiled.
961 -- If this is the main unit and we are generating code, we must check
962 -- that all generic units in the context have a body if they need it,
963 -- even if they have not been instantiated. In the absence of .ali files
964 -- for generic units, we must force the load of the body, just to
965 -- produce the proper error if the body is absent. We skip this
966 -- verification if the main unit itself is generic.
968 if Get_Cunit_Unit_Number
(N
) = Main_Unit
969 and then Operating_Mode
= Generate_Code
970 and then Expander_Active
972 -- Check whether the source for the body of the unit must be included
973 -- in a standalone library.
975 Check_Body_Needed_For_SAL
(Cunit_Entity
(Main_Unit
));
977 -- Indicate that the main unit is now analyzed, to catch possible
978 -- circularities between it and generic bodies. Remove main unit from
979 -- visibility. This might seem superfluous, but the main unit must
980 -- not be visible in the generic body expansions that follow.
982 Set_Analyzed
(N
, True);
983 Set_Is_Immediately_Visible
(Cunit_Entity
(Main_Unit
), False);
988 Un
: Unit_Number_Type
;
990 Save_Style_Check
: constant Boolean := Style_Check
;
991 Save_C_Restrict
: constant Save_Cunit_Boolean_Restrictions
:=
992 Cunit_Boolean_Restrictions_Save
;
995 Item
:= First
(Context_Items
(N
));
996 while Present
(Item
) loop
998 -- Check for explicit with clause
1000 if Nkind
(Item
) = N_With_Clause
1001 and then not Implicit_With
(Item
)
1003 -- Ada 2005 (AI-50217): Ignore limited-withed units
1005 and then not Limited_Present
(Item
)
1007 Nam
:= Entity
(Name
(Item
));
1009 if (Is_Generic_Subprogram
(Nam
)
1010 and then not Is_Intrinsic_Subprogram
(Nam
))
1011 or else (Ekind
(Nam
) = E_Generic_Package
1012 and then Unit_Requires_Body
(Nam
))
1014 Style_Check
:= False;
1016 if Present
(Renamed_Object
(Nam
)) then
1019 (Load_Name
=> Get_Body_Name
1021 (Unit_Declaration_Node
1022 (Renamed_Object
(Nam
)))),
1030 (Load_Name
=> Get_Body_Name
1031 (Get_Unit_Name
(Item
)),
1038 if Un
= No_Unit
then
1040 ("body of generic unit& not found", Item
, Nam
);
1043 elsif not Analyzed
(Cunit
(Un
))
1044 and then Un
/= Main_Unit
1045 and then not Fatal_Error
(Un
)
1047 Style_Check
:= False;
1048 Semantics
(Cunit
(Un
));
1056 Style_Check
:= Save_Style_Check
;
1057 Cunit_Boolean_Restrictions_Restore
(Save_C_Restrict
);
1061 -- Deal with creating elaboration Boolean if needed. We create an
1062 -- elaboration boolean only for units that come from source since
1063 -- units manufactured by the compiler never need elab checks.
1065 if Comes_From_Source
(N
)
1067 (Nkind
(Unit_Node
) = N_Package_Declaration
or else
1068 Nkind
(Unit_Node
) = N_Generic_Package_Declaration
or else
1069 Nkind
(Unit_Node
) = N_Subprogram_Declaration
or else
1070 Nkind
(Unit_Node
) = N_Generic_Subprogram_Declaration
)
1073 Loc
: constant Source_Ptr
:= Sloc
(N
);
1074 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(Loc
);
1077 Spec_Id
:= Defining_Entity
(Unit_Node
);
1078 Generate_Definition
(Spec_Id
);
1080 -- See if an elaboration entity is required for possible access
1081 -- before elaboration checking. Note that we must allow for this
1082 -- even if -gnatE is not set, since a client may be compiled in
1083 -- -gnatE mode and reference the entity.
1085 -- These entities are also used by the binder to prevent multiple
1086 -- attempts to execute the elaboration code for the library case
1087 -- where the elaboration routine might otherwise be called more
1090 -- Case of units which do not require elaboration checks
1093 -- Pure units do not need checks
1097 -- Preelaborated units do not need checks
1099 or else Is_Preelaborated
(Spec_Id
)
1101 -- No checks needed if pagma Elaborate_Body present
1103 or else Has_Pragma_Elaborate_Body
(Spec_Id
)
1105 -- No checks needed if unit does not require a body
1107 or else not Unit_Requires_Body
(Spec_Id
)
1109 -- No checks needed for predefined files
1111 or else Is_Predefined_File_Name
(Unit_File_Name
(Unum
))
1113 -- No checks required if no separate spec
1115 or else Acts_As_Spec
(N
)
1117 -- This is a case where we only need the entity for
1118 -- checking to prevent multiple elaboration checks.
1120 Set_Elaboration_Entity_Required
(Spec_Id
, False);
1122 -- Case of elaboration entity is required for access before
1123 -- elaboration checking (so certainly we must build it!)
1126 Set_Elaboration_Entity_Required
(Spec_Id
, True);
1129 Build_Elaboration_Entity
(N
, Spec_Id
);
1133 -- Freeze the compilation unit entity. This for sure is needed because
1134 -- of some warnings that can be output (see Freeze_Subprogram), but may
1135 -- in general be required. If freezing actions result, place them in the
1136 -- compilation unit actions list, and analyze them.
1139 Loc
: constant Source_Ptr
:= Sloc
(N
);
1140 L
: constant List_Id
:=
1141 Freeze_Entity
(Cunit_Entity
(Current_Sem_Unit
), Loc
);
1143 while Is_Non_Empty_List
(L
) loop
1144 Insert_Library_Level_Action
(Remove_Head
(L
));
1150 if Nkind
(Unit_Node
) = N_Package_Declaration
1151 and then Get_Cunit_Unit_Number
(N
) /= Main_Unit
1152 and then Expander_Active
1155 Save_Style_Check
: constant Boolean := Style_Check
;
1156 Save_Warning
: constant Warning_Mode_Type
:= Warning_Mode
;
1157 Options
: Style_Check_Options
;
1160 Save_Style_Check_Options
(Options
);
1161 Reset_Style_Check_Options
;
1162 Opt
.Warning_Mode
:= Suppress
;
1163 Check_Body_For_Inlining
(N
, Defining_Entity
(Unit_Node
));
1165 Reset_Style_Check_Options
;
1166 Set_Style_Check_Options
(Options
);
1167 Style_Check
:= Save_Style_Check
;
1168 Warning_Mode
:= Save_Warning
;
1172 -- If we are generating obsolescent warnings, then here is where we
1173 -- generate them for the with'ed items. The reason for this special
1174 -- processing is that the normal mechanism of generating the warnings
1175 -- for referenced entities does not work for context clause references.
1176 -- That's because when we first analyze the context, it is too early to
1177 -- know if the with'ing unit is itself obsolescent (which suppresses
1180 if not GNAT_Mode
and then Warn_On_Obsolescent_Feature
then
1182 -- Push current compilation unit as scope, so that the test for
1183 -- being within an obsolescent unit will work correctly.
1185 Push_Scope
(Defining_Entity
(Unit_Node
));
1187 -- Loop through context items to deal with with clauses
1195 Item
:= First
(Context_Items
(N
));
1196 while Present
(Item
) loop
1197 if Nkind
(Item
) = N_With_Clause
1199 -- Suppress this check in limited-withed units. Further work
1200 -- needed here if we decide to incorporate this check on
1201 -- limited-withed units.
1203 and then not Limited_Present
(Item
)
1206 Ent
:= Entity
(Nam
);
1208 if Is_Obsolescent
(Ent
) then
1209 Output_Obsolescent_Entity_Warnings
(Nam
, Ent
);
1217 -- Remove temporary install of current unit as scope
1221 end Analyze_Compilation_Unit
;
1223 ---------------------
1224 -- Analyze_Context --
1225 ---------------------
1227 procedure Analyze_Context
(N
: Node_Id
) is
1228 Ukind
: constant Node_Kind
:= Nkind
(Unit
(N
));
1232 -- First process all configuration pragmas at the start of the context
1233 -- items. Strictly these are not part of the context clause, but that
1234 -- is where the parser puts them. In any case for sure we must analyze
1235 -- these before analyzing the actual context items, since they can have
1236 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1237 -- be with'ed as a result of changing categorizations in Ada 2005).
1239 Item
:= First
(Context_Items
(N
));
1240 while Present
(Item
)
1241 and then Nkind
(Item
) = N_Pragma
1242 and then Chars
(Item
) in Configuration_Pragma_Names
1248 -- Loop through actual context items. This is done in two passes:
1250 -- a) The first pass analyzes non-limited with-clauses and also any
1251 -- configuration pragmas (we need to get the latter analyzed right
1252 -- away, since they can affect processing of subsequent items.
1254 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1256 while Present
(Item
) loop
1258 -- For with clause, analyze the with clause, and then update
1259 -- the version, since we are dependent on a unit that we with.
1261 if Nkind
(Item
) = N_With_Clause
1262 and then not Limited_Present
(Item
)
1264 -- Skip analyzing with clause if no unit, nothing to do (this
1265 -- happens for a with that references a non-existant unit)
1267 if Present
(Library_Unit
(Item
)) then
1271 if not Implicit_With
(Item
) then
1272 Version_Update
(N
, Library_Unit
(Item
));
1275 -- Skip pragmas. Configuration pragmas at the start were handled in
1276 -- the loop above, and remaining pragmas are not processed until we
1277 -- actually install the context (see Install_Context). We delay the
1278 -- analysis of these pragmas to make sure that we have installed all
1279 -- the implicit with's on parent units.
1281 -- Skip use clauses at this stage, since we don't want to do any
1282 -- installing of potentially use visible entities until we we
1283 -- actually install the complete context (in Install_Context).
1284 -- Otherwise things can get installed in the wrong context.
1293 -- Second pass: examine all limited_with clauses. All other context
1294 -- items are ignored in this pass.
1296 Item
:= First
(Context_Items
(N
));
1297 while Present
(Item
) loop
1298 if Nkind
(Item
) = N_With_Clause
1299 and then Limited_Present
(Item
)
1301 -- No need to check errors on implicitly generated limited-with
1304 if not Implicit_With
(Item
) then
1306 -- Check compilation unit containing the limited-with clause
1308 if Ukind
/= N_Package_Declaration
1309 and then Ukind
/= N_Subprogram_Declaration
1310 and then Ukind
/= N_Package_Renaming_Declaration
1311 and then Ukind
/= N_Subprogram_Renaming_Declaration
1312 and then Ukind
not in N_Generic_Declaration
1313 and then Ukind
not in N_Generic_Renaming_Declaration
1314 and then Ukind
not in N_Generic_Instantiation
1316 Error_Msg_N
("limited with_clause not allowed here", Item
);
1318 -- Check wrong use of a limited with clause applied to the
1319 -- compilation unit containing the limited-with clause.
1321 -- limited with P.Q;
1322 -- package P.Q is ...
1324 elsif Unit
(Library_Unit
(Item
)) = Unit
(N
) then
1325 Error_Msg_N
("wrong use of limited-with clause", Item
);
1327 -- Check wrong use of limited-with clause applied to some
1328 -- immediate ancestor.
1330 elsif Is_Child_Spec
(Unit
(N
)) then
1332 Lib_U
: constant Entity_Id
:= Unit
(Library_Unit
(Item
));
1336 P
:= Parent_Spec
(Unit
(N
));
1338 if Unit
(P
) = Lib_U
then
1339 Error_Msg_N
("limited with_clause of immediate "
1340 & "ancestor not allowed", Item
);
1344 exit when not Is_Child_Spec
(Unit
(P
));
1345 P
:= Parent_Spec
(Unit
(P
));
1350 -- Check if the limited-withed unit is already visible through
1351 -- some context clause of the current compilation unit or some
1352 -- ancestor of the current compilation unit.
1355 Lim_Unit_Name
: constant Node_Id
:= Name
(Item
);
1356 Comp_Unit
: Node_Id
;
1358 Unit_Name
: Node_Id
;
1363 It
:= First
(Context_Items
(Comp_Unit
));
1364 while Present
(It
) loop
1366 and then Nkind
(It
) = N_With_Clause
1367 and then not Limited_Present
(It
)
1369 (Nkind
(Unit
(Library_Unit
(It
)))
1370 = N_Package_Declaration
1372 Nkind
(Unit
(Library_Unit
(It
)))
1373 = N_Package_Renaming_Declaration
)
1375 if Nkind
(Unit
(Library_Unit
(It
)))
1376 = N_Package_Declaration
1378 Unit_Name
:= Name
(It
);
1380 Unit_Name
:= Name
(Unit
(Library_Unit
(It
)));
1383 -- Check if the named package (or some ancestor)
1384 -- leaves visible the full-view of the unit given
1385 -- in the limited-with clause
1388 if Designate_Same_Unit
(Lim_Unit_Name
,
1391 Error_Msg_Sloc
:= Sloc
(It
);
1393 ("simultaneous visibility of limited "
1394 & "and unlimited views not allowed",
1397 ("\unlimited view visible through "
1398 & "context clause #",
1402 elsif Nkind
(Unit_Name
) = N_Identifier
then
1406 Unit_Name
:= Prefix
(Unit_Name
);
1413 exit when not Is_Child_Spec
(Unit
(Comp_Unit
));
1415 Comp_Unit
:= Parent_Spec
(Unit
(Comp_Unit
));
1420 -- Skip analyzing with clause if no unit, see above
1422 if Present
(Library_Unit
(Item
)) then
1426 -- A limited_with does not impose an elaboration order, but
1427 -- there is a semantic dependency for recompilation purposes.
1429 if not Implicit_With
(Item
) then
1430 Version_Update
(N
, Library_Unit
(Item
));
1433 -- Pragmas and use clauses and with clauses other than limited
1434 -- with's are ignored in this pass through the context items.
1442 end Analyze_Context
;
1444 -------------------------------
1445 -- Analyze_Package_Body_Stub --
1446 -------------------------------
1448 procedure Analyze_Package_Body_Stub
(N
: Node_Id
) is
1449 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1453 -- The package declaration must be in the current declarative part
1455 Check_Stub_Level
(N
);
1456 Nam
:= Current_Entity_In_Scope
(Id
);
1458 if No
(Nam
) or else not Is_Package_Or_Generic_Package
(Nam
) then
1459 Error_Msg_N
("missing specification for package stub", N
);
1461 elsif Has_Completion
(Nam
)
1462 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(Nam
)))
1464 Error_Msg_N
("duplicate or redundant stub for package", N
);
1467 -- Indicate that the body of the package exists. If we are doing
1468 -- only semantic analysis, the stub stands for the body. If we are
1469 -- generating code, the existence of the body will be confirmed
1470 -- when we load the proper body.
1472 Set_Has_Completion
(Nam
);
1473 Set_Scope
(Defining_Entity
(N
), Current_Scope
);
1474 Generate_Reference
(Nam
, Id
, 'b');
1475 Analyze_Proper_Body
(N
, Nam
);
1477 end Analyze_Package_Body_Stub
;
1479 -------------------------
1480 -- Analyze_Proper_Body --
1481 -------------------------
1483 procedure Analyze_Proper_Body
(N
: Node_Id
; Nam
: Entity_Id
) is
1484 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
1485 Unum
: Unit_Number_Type
;
1487 procedure Optional_Subunit
;
1488 -- This procedure is called when the main unit is a stub, or when we
1489 -- are not generating code. In such a case, we analyze the subunit if
1490 -- present, which is user-friendly and in fact required for ASIS, but
1491 -- we don't complain if the subunit is missing.
1493 ----------------------
1494 -- Optional_Subunit --
1495 ----------------------
1497 procedure Optional_Subunit
is
1498 Comp_Unit
: Node_Id
;
1501 -- Try to load subunit, but ignore any errors that occur during
1502 -- the loading of the subunit, by using the special feature in
1503 -- Errout to ignore all errors. Note that Fatal_Error will still
1504 -- be set, so we will be able to check for this case below.
1506 if not ASIS_Mode
then
1507 Ignore_Errors_Enable
:= Ignore_Errors_Enable
+ 1;
1512 (Load_Name
=> Subunit_Name
,
1517 if not ASIS_Mode
then
1518 Ignore_Errors_Enable
:= Ignore_Errors_Enable
- 1;
1521 -- All done if we successfully loaded the subunit
1524 and then (not Fatal_Error
(Unum
) or else Try_Semantics
)
1526 Comp_Unit
:= Cunit
(Unum
);
1528 -- If the file was empty or seriously mangled, the unit
1529 -- itself may be missing.
1531 if No
(Unit
(Comp_Unit
)) then
1533 ("subunit does not contain expected proper body", N
);
1535 elsif Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
1537 ("expected SEPARATE subunit, found child unit",
1538 Cunit_Entity
(Unum
));
1540 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
1541 Analyze_Subunit
(Comp_Unit
);
1542 Set_Library_Unit
(N
, Comp_Unit
);
1545 elsif Unum
= No_Unit
1546 and then Present
(Nam
)
1548 if Is_Protected_Type
(Nam
) then
1549 Set_Corresponding_Body
(Parent
(Nam
), Defining_Identifier
(N
));
1551 Set_Corresponding_Body
(
1552 Unit_Declaration_Node
(Nam
), Defining_Identifier
(N
));
1555 end Optional_Subunit
;
1557 -- Start of processing for Analyze_Proper_Body
1560 -- If the subunit is already loaded, it means that the main unit
1561 -- is a subunit, and that the current unit is one of its parents
1562 -- which was being analyzed to provide the needed context for the
1563 -- analysis of the subunit. In this case we analyze the subunit and
1564 -- continue with the parent, without looking a subsequent subunits.
1566 if Is_Loaded
(Subunit_Name
) then
1568 -- If the proper body is already linked to the stub node,
1569 -- the stub is in a generic unit and just needs analyzing.
1571 if Present
(Library_Unit
(N
)) then
1572 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
1573 Analyze_Subunit
(Library_Unit
(N
));
1575 -- Otherwise we must load the subunit and link to it
1578 -- Load the subunit, this must work, since we originally
1579 -- loaded the subunit earlier on. So this will not really
1580 -- load it, just give access to it.
1584 (Load_Name
=> Subunit_Name
,
1589 -- And analyze the subunit in the parent context (note that we
1590 -- do not call Semantics, since that would remove the parent
1591 -- context). Because of this, we have to manually reset the
1592 -- compiler state to Analyzing since it got destroyed by Load.
1594 if Unum
/= No_Unit
then
1595 Compiler_State
:= Analyzing
;
1597 -- Check that the proper body is a subunit and not a child
1598 -- unit. If the unit was previously loaded, the error will
1599 -- have been emitted when copying the generic node, so we
1600 -- just return to avoid cascaded errors.
1602 if Nkind
(Unit
(Cunit
(Unum
))) /= N_Subunit
then
1606 Set_Corresponding_Stub
(Unit
(Cunit
(Unum
)), N
);
1607 Analyze_Subunit
(Cunit
(Unum
));
1608 Set_Library_Unit
(N
, Cunit
(Unum
));
1612 -- If the main unit is a subunit, then we are just performing semantic
1613 -- analysis on that subunit, and any other subunits of any parent unit
1614 -- should be ignored, except that if we are building trees for ASIS
1615 -- usage we want to annotate the stub properly.
1617 elsif Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
1618 and then Subunit_Name
/= Unit_Name
(Main_Unit
)
1624 -- But before we return, set the flag for unloaded subunits. This
1625 -- will suppress junk warnings of variables in the same declarative
1626 -- part (or a higher level one) that are in danger of looking unused
1627 -- when in fact there might be a declaration in the subunit that we
1628 -- do not intend to load.
1630 Unloaded_Subunits
:= True;
1633 -- If the subunit is not already loaded, and we are generating code,
1634 -- then this is the case where compilation started from the parent,
1635 -- and we are generating code for an entire subunit tree. In that
1636 -- case we definitely need to load the subunit.
1638 -- In order to continue the analysis with the rest of the parent,
1639 -- and other subunits, we load the unit without requiring its
1640 -- presence, and emit a warning if not found, rather than terminating
1641 -- the compilation abruptly, as for other missing file problems.
1643 elsif Original_Operating_Mode
= Generate_Code
then
1645 -- If the proper body is already linked to the stub node,
1646 -- the stub is in a generic unit and just needs analyzing.
1648 -- We update the version. Although we are not technically
1649 -- semantically dependent on the subunit, given our approach
1650 -- of macro substitution of subunits, it makes sense to
1651 -- include it in the version identification.
1653 if Present
(Library_Unit
(N
)) then
1654 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
1655 Analyze_Subunit
(Library_Unit
(N
));
1656 Version_Update
(Cunit
(Main_Unit
), Library_Unit
(N
));
1658 -- Otherwise we must load the subunit and link to it
1663 (Load_Name
=> Subunit_Name
,
1668 if Original_Operating_Mode
= Generate_Code
1669 and then Unum
= No_Unit
1671 Error_Msg_Unit_1
:= Subunit_Name
;
1673 Get_File_Name
(Subunit_Name
, Subunit
=> True);
1675 ("subunit$$ in file{ not found?", N
);
1676 Subunits_Missing
:= True;
1679 -- Load_Unit may reset Compiler_State, since it may have been
1680 -- necessary to parse an additional units, so we make sure
1681 -- that we reset it to the Analyzing state.
1683 Compiler_State
:= Analyzing
;
1685 if Unum
/= No_Unit
then
1686 if Debug_Flag_L
then
1687 Write_Str
("*** Loaded subunit from stub. Analyze");
1692 Comp_Unit
: constant Node_Id
:= Cunit
(Unum
);
1695 -- Check for child unit instead of subunit
1697 if Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
1699 ("expected SEPARATE subunit, found child unit",
1700 Cunit_Entity
(Unum
));
1702 -- OK, we have a subunit
1705 -- Set corresponding stub (even if errors)
1707 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
1709 -- Analyze the unit if semantics active
1711 if not Fatal_Error
(Unum
) or else Try_Semantics
then
1712 Analyze_Subunit
(Comp_Unit
);
1715 -- Set the library unit pointer in any case
1717 Set_Library_Unit
(N
, Comp_Unit
);
1719 -- We update the version. Although we are not technically
1720 -- semantically dependent on the subunit, given our
1721 -- approach of macro substitution of subunits, it makes
1722 -- sense to include it in the version identification.
1724 Version_Update
(Cunit
(Main_Unit
), Comp_Unit
);
1730 -- The remaining case is when the subunit is not already loaded and
1731 -- we are not generating code. In this case we are just performing
1732 -- semantic analysis on the parent, and we are not interested in
1733 -- the subunit. For subprograms, analyze the stub as a body. For
1734 -- other entities the stub has already been marked as completed.
1740 end Analyze_Proper_Body
;
1742 ----------------------------------
1743 -- Analyze_Protected_Body_Stub --
1744 ----------------------------------
1746 procedure Analyze_Protected_Body_Stub
(N
: Node_Id
) is
1747 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Defining_Identifier
(N
));
1750 Check_Stub_Level
(N
);
1752 -- First occurence of name may have been as an incomplete type
1754 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
1755 Nam
:= Full_View
(Nam
);
1759 or else not Is_Protected_Type
(Etype
(Nam
))
1761 Error_Msg_N
("missing specification for Protected body", N
);
1763 Set_Scope
(Defining_Entity
(N
), Current_Scope
);
1764 Set_Has_Completion
(Etype
(Nam
));
1765 Generate_Reference
(Nam
, Defining_Identifier
(N
), 'b');
1766 Analyze_Proper_Body
(N
, Etype
(Nam
));
1768 end Analyze_Protected_Body_Stub
;
1770 ----------------------------------
1771 -- Analyze_Subprogram_Body_Stub --
1772 ----------------------------------
1774 -- A subprogram body stub can appear with or without a previous
1775 -- specification. If there is one, the analysis of the body will
1776 -- find it and verify conformance. The formals appearing in the
1777 -- specification of the stub play no role, except for requiring an
1778 -- additional conformance check. If there is no previous subprogram
1779 -- declaration, the stub acts as a spec, and provides the defining
1780 -- entity for the subprogram.
1782 procedure Analyze_Subprogram_Body_Stub
(N
: Node_Id
) is
1786 Check_Stub_Level
(N
);
1788 -- Verify that the identifier for the stub is unique within this
1789 -- declarative part.
1791 if Nkind
(Parent
(N
)) = N_Block_Statement
1792 or else Nkind
(Parent
(N
)) = N_Package_Body
1793 or else Nkind
(Parent
(N
)) = N_Subprogram_Body
1795 Decl
:= First
(Declarations
(Parent
(N
)));
1796 while Present
(Decl
)
1799 if Nkind
(Decl
) = N_Subprogram_Body_Stub
1800 and then (Chars
(Defining_Unit_Name
(Specification
(Decl
)))
1801 = Chars
(Defining_Unit_Name
(Specification
(N
))))
1803 Error_Msg_N
("identifier for stub is not unique", N
);
1810 -- Treat stub as a body, which checks conformance if there is a previous
1811 -- declaration, or else introduces entity and its signature.
1813 Analyze_Subprogram_Body
(N
);
1814 Analyze_Proper_Body
(N
, Empty
);
1815 end Analyze_Subprogram_Body_Stub
;
1817 ---------------------
1818 -- Analyze_Subunit --
1819 ---------------------
1821 -- A subunit is compiled either by itself (for semantic checking)
1822 -- or as part of compiling the parent (for code generation). In
1823 -- either case, by the time we actually process the subunit, the
1824 -- parent has already been installed and analyzed. The node N is
1825 -- a compilation unit, whose context needs to be treated here,
1826 -- because we come directly here from the parent without calling
1827 -- Analyze_Compilation_Unit.
1829 -- The compilation context includes the explicit context of the
1830 -- subunit, and the context of the parent, together with the parent
1831 -- itself. In order to compile the current context, we remove the
1832 -- one inherited from the parent, in order to have a clean visibility
1833 -- table. We restore the parent context before analyzing the proper
1834 -- body itself. On exit, we remove only the explicit context of the
1837 procedure Analyze_Subunit
(N
: Node_Id
) is
1838 Lib_Unit
: constant Node_Id
:= Library_Unit
(N
);
1839 Par_Unit
: constant Entity_Id
:= Current_Scope
;
1841 Lib_Spec
: Node_Id
:= Library_Unit
(Lib_Unit
);
1842 Num_Scopes
: Int
:= 0;
1843 Use_Clauses
: array (1 .. Scope_Stack
.Last
) of Node_Id
;
1844 Enclosing_Child
: Entity_Id
:= Empty
;
1845 Svg
: constant Suppress_Array
:= Scope_Suppress
;
1847 procedure Analyze_Subunit_Context
;
1848 -- Capture names in use clauses of the subunit. This must be done
1849 -- before re-installing parent declarations, because items in the
1850 -- context must not be hidden by declarations local to the parent.
1852 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
);
1853 -- Recursive procedure to restore scope of all ancestors of subunit,
1854 -- from outermost in. If parent is not a subunit, the call to install
1855 -- context installs context of spec and (if parent is a child unit)
1856 -- the context of its parents as well. It is confusing that parents
1857 -- should be treated differently in both cases, but the semantics are
1858 -- just not identical.
1860 procedure Re_Install_Use_Clauses
;
1861 -- As part of the removal of the parent scope, the use clauses are
1862 -- removed, to be reinstalled when the context of the subunit has
1863 -- been analyzed. Use clauses may also have been affected by the
1864 -- analysis of the context of the subunit, so they have to be applied
1865 -- again, to insure that the compilation environment of the rest of
1866 -- the parent unit is identical.
1868 procedure Remove_Scope
;
1869 -- Remove current scope from scope stack, and preserve the list
1870 -- of use clauses in it, to be reinstalled after context is analyzed.
1872 -----------------------------
1873 -- Analyze_Subunit_Context --
1874 -----------------------------
1876 procedure Analyze_Subunit_Context
is
1879 Unit_Name
: Entity_Id
;
1882 Analyze_Context
(N
);
1884 -- Make withed units immediately visible. If child unit, make the
1885 -- ultimate parent immediately visible.
1887 Item
:= First
(Context_Items
(N
));
1888 while Present
(Item
) loop
1889 if Nkind
(Item
) = N_With_Clause
then
1891 -- Protect frontend against previous errors in context clauses
1893 if Nkind
(Name
(Item
)) /= N_Selected_Component
then
1894 if Error_Posted
(Item
) then
1898 Unit_Name
:= Entity
(Name
(Item
));
1899 while Is_Child_Unit
(Unit_Name
) loop
1900 Set_Is_Visible_Child_Unit
(Unit_Name
);
1901 Unit_Name
:= Scope
(Unit_Name
);
1904 if not Is_Immediately_Visible
(Unit_Name
) then
1905 Set_Is_Immediately_Visible
(Unit_Name
);
1906 Set_Context_Installed
(Item
);
1911 elsif Nkind
(Item
) = N_Use_Package_Clause
then
1912 Nam
:= First
(Names
(Item
));
1913 while Present
(Nam
) loop
1918 elsif Nkind
(Item
) = N_Use_Type_Clause
then
1919 Nam
:= First
(Subtype_Marks
(Item
));
1920 while Present
(Nam
) loop
1929 -- Reset visibility of withed units. They will be made visible
1930 -- again when we install the subunit context.
1932 Item
:= First
(Context_Items
(N
));
1933 while Present
(Item
) loop
1934 if Nkind
(Item
) = N_With_Clause
1936 -- Protect frontend against previous errors in context clauses
1938 and then Nkind
(Name
(Item
)) /= N_Selected_Component
1939 and then not Error_Posted
(Item
)
1941 Unit_Name
:= Entity
(Name
(Item
));
1942 while Is_Child_Unit
(Unit_Name
) loop
1943 Set_Is_Visible_Child_Unit
(Unit_Name
, False);
1944 Unit_Name
:= Scope
(Unit_Name
);
1947 if Context_Installed
(Item
) then
1948 Set_Is_Immediately_Visible
(Unit_Name
, False);
1949 Set_Context_Installed
(Item
, False);
1955 end Analyze_Subunit_Context
;
1957 ------------------------
1958 -- Re_Install_Parents --
1959 ------------------------
1961 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
) is
1965 if Nkind
(Unit
(L
)) = N_Subunit
then
1966 Re_Install_Parents
(Library_Unit
(L
), Scope
(Scop
));
1969 Install_Context
(L
);
1971 -- If the subunit occurs within a child unit, we must restore the
1972 -- immediate visibility of any siblings that may occur in context.
1974 if Present
(Enclosing_Child
) then
1975 Install_Siblings
(Enclosing_Child
, L
);
1980 if Scop
/= Par_Unit
then
1981 Set_Is_Immediately_Visible
(Scop
);
1984 -- Make entities in scope visible again. For child units, restore
1985 -- visibility only if they are actually in context.
1987 E
:= First_Entity
(Current_Scope
);
1988 while Present
(E
) loop
1989 if not Is_Child_Unit
(E
)
1990 or else Is_Visible_Child_Unit
(E
)
1992 Set_Is_Immediately_Visible
(E
);
1998 -- A subunit appears within a body, and for a nested subunits
1999 -- all the parents are bodies. Restore full visibility of their
2000 -- private entities.
2002 if Ekind
(Scop
) = E_Package
2003 or else Ekind
(Scop
) = E_Generic_Package
2005 Set_In_Package_Body
(Scop
);
2006 Install_Private_Declarations
(Scop
);
2008 end Re_Install_Parents
;
2010 ----------------------------
2011 -- Re_Install_Use_Clauses --
2012 ----------------------------
2014 procedure Re_Install_Use_Clauses
is
2017 for J
in reverse 1 .. Num_Scopes
loop
2018 U
:= Use_Clauses
(J
);
2019 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:= U
;
2020 Install_Use_Clauses
(U
, Force_Installation
=> True);
2022 end Re_Install_Use_Clauses
;
2028 procedure Remove_Scope
is
2032 Num_Scopes
:= Num_Scopes
+ 1;
2033 Use_Clauses
(Num_Scopes
) :=
2034 Scope_Stack
.Table
(Scope_Stack
.Last
).First_Use_Clause
;
2036 E
:= First_Entity
(Current_Scope
);
2037 while Present
(E
) loop
2038 Set_Is_Immediately_Visible
(E
, False);
2042 if Is_Child_Unit
(Current_Scope
) then
2043 Enclosing_Child
:= Current_Scope
;
2049 -- Start of processing for Analyze_Subunit
2052 if not Is_Empty_List
(Context_Items
(N
)) then
2054 -- Save current use clauses
2057 Remove_Context
(Lib_Unit
);
2059 -- Now remove parents and their context, including enclosing
2060 -- subunits and the outer parent body which is not a subunit.
2062 if Present
(Lib_Spec
) then
2063 Remove_Context
(Lib_Spec
);
2065 while Nkind
(Unit
(Lib_Spec
)) = N_Subunit
loop
2066 Lib_Spec
:= Library_Unit
(Lib_Spec
);
2068 Remove_Context
(Lib_Spec
);
2071 if Nkind
(Unit
(Lib_Unit
)) = N_Subunit
then
2075 if Nkind
(Unit
(Lib_Spec
)) = N_Package_Body
then
2076 Remove_Context
(Library_Unit
(Lib_Spec
));
2080 Set_Is_Immediately_Visible
(Par_Unit
, False);
2082 Analyze_Subunit_Context
;
2084 Re_Install_Parents
(Lib_Unit
, Par_Unit
);
2085 Set_Is_Immediately_Visible
(Par_Unit
);
2087 -- If the context includes a child unit of the parent of the
2088 -- subunit, the parent will have been removed from visibility,
2089 -- after compiling that cousin in the context. The visibility
2090 -- of the parent must be restored now. This also applies if the
2091 -- context includes another subunit of the same parent which in
2092 -- turn includes a child unit in its context.
2094 if Ekind
(Par_Unit
) = E_Package
2095 or else Ekind
(Par_Unit
) = E_Generic_Package
2097 if not Is_Immediately_Visible
(Par_Unit
)
2098 or else (Present
(First_Entity
(Par_Unit
))
2099 and then not Is_Immediately_Visible
2100 (First_Entity
(Par_Unit
)))
2102 Set_Is_Immediately_Visible
(Par_Unit
);
2103 Install_Visible_Declarations
(Par_Unit
);
2104 Install_Private_Declarations
(Par_Unit
);
2108 Re_Install_Use_Clauses
;
2109 Install_Context
(N
);
2111 -- Restore state of suppress flags for current body
2113 Scope_Suppress
:= Svg
;
2115 -- If the subunit is within a child unit, then siblings of any
2116 -- parent unit that appear in the context clause of the subunit
2117 -- must also be made immediately visible.
2119 if Present
(Enclosing_Child
) then
2120 Install_Siblings
(Enclosing_Child
, N
);
2125 Analyze
(Proper_Body
(Unit
(N
)));
2128 -- The subunit may contain a with_clause on a sibling of some
2129 -- ancestor. Removing the context will remove from visibility those
2130 -- ancestor child units, which must be restored to the visibility
2131 -- they have in the enclosing body.
2133 if Present
(Enclosing_Child
) then
2139 and then Is_Child_Unit
(C
)
2141 Set_Is_Immediately_Visible
(C
);
2142 Set_Is_Visible_Child_Unit
(C
);
2147 end Analyze_Subunit
;
2149 ----------------------------
2150 -- Analyze_Task_Body_Stub --
2151 ----------------------------
2153 procedure Analyze_Task_Body_Stub
(N
: Node_Id
) is
2154 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Defining_Identifier
(N
));
2155 Loc
: constant Source_Ptr
:= Sloc
(N
);
2158 Check_Stub_Level
(N
);
2160 -- First occurence of name may have been as an incomplete type
2162 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
2163 Nam
:= Full_View
(Nam
);
2167 or else not Is_Task_Type
(Etype
(Nam
))
2169 Error_Msg_N
("missing specification for task body", N
);
2171 Set_Scope
(Defining_Entity
(N
), Current_Scope
);
2172 Generate_Reference
(Nam
, Defining_Identifier
(N
), 'b');
2173 Set_Has_Completion
(Etype
(Nam
));
2174 Analyze_Proper_Body
(N
, Etype
(Nam
));
2176 -- Set elaboration flag to indicate that entity is callable.
2177 -- This cannot be done in the expansion of the body itself,
2178 -- because the proper body is not in a declarative part. This
2179 -- is only done if expansion is active, because the context
2180 -- may be generic and the flag not defined yet.
2182 if Expander_Active
then
2184 Make_Assignment_Statement
(Loc
,
2186 Make_Identifier
(Loc
,
2187 New_External_Name
(Chars
(Etype
(Nam
)), 'E')),
2188 Expression
=> New_Reference_To
(Standard_True
, Loc
)));
2192 end Analyze_Task_Body_Stub
;
2194 -------------------------
2195 -- Analyze_With_Clause --
2196 -------------------------
2198 -- Analyze the declaration of a unit in a with clause. At end,
2199 -- label the with clause with the defining entity for the unit.
2201 procedure Analyze_With_Clause
(N
: Node_Id
) is
2203 -- Retrieve the original kind of the unit node, before analysis.
2204 -- If it is a subprogram instantiation, its analysis below will
2205 -- rewrite as the declaration of the wrapper package. If the same
2206 -- instantiation appears indirectly elsewhere in the context, it
2207 -- will have been analyzed already.
2209 Unit_Kind
: constant Node_Kind
:=
2210 Nkind
(Original_Node
(Unit
(Library_Unit
(N
))));
2211 Nam
: constant Node_Id
:= Name
(N
);
2213 Par_Name
: Entity_Id
;
2218 -- Set True if the unit currently being compiled is an internal unit
2220 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
2221 Save_C_Restrict
: constant Save_Cunit_Boolean_Restrictions
:=
2222 Cunit_Boolean_Restrictions_Save
;
2225 if Limited_Present
(N
) then
2227 -- Ada 2005 (AI-50217): Build visibility structures but do not
2228 -- analyze the unit.
2230 Build_Limited_Views
(N
);
2234 -- We reset ordinary style checking during the analysis of a with'ed
2235 -- unit, but we do NOT reset GNAT special analysis mode (the latter
2236 -- definitely *does* apply to with'ed units).
2238 if not GNAT_Mode
then
2239 Style_Check
:= False;
2242 -- If the library unit is a predefined unit, and we are in high
2243 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2244 -- for the analysis of the with'ed unit. This mode does not prevent
2245 -- explicit with'ing of run-time units.
2247 if Configurable_Run_Time_Mode
2249 Is_Predefined_File_Name
2250 (Unit_File_Name
(Get_Source_Unit
(Unit
(Library_Unit
(N
)))))
2252 Configurable_Run_Time_Mode
:= False;
2253 Semantics
(Library_Unit
(N
));
2254 Configurable_Run_Time_Mode
:= True;
2257 Semantics
(Library_Unit
(N
));
2260 U
:= Unit
(Library_Unit
(N
));
2261 Intunit
:= Is_Internal_File_Name
(Unit_File_Name
(Current_Sem_Unit
));
2263 -- Following checks are skipped for dummy packages (those supplied for
2264 -- with's where no matching file could be found). Such packages are
2265 -- identified by the Sloc value being set to No_Location
2267 if Sloc
(U
) /= No_Location
then
2269 -- Check restrictions, except that we skip the check if this is an
2270 -- internal unit unless we are compiling the internal unit as the
2271 -- main unit. We also skip this for dummy packages.
2273 Check_Restriction_No_Dependence
(Nam
, N
);
2275 if not Intunit
or else Current_Sem_Unit
= Main_Unit
then
2276 Check_Restricted_Unit
(Unit_Name
(Get_Source_Unit
(U
)), N
);
2279 -- Deal with special case of GNAT.Current_Exceptions which interacts
2280 -- with the optimization of local raise statements into gotos.
2282 if Nkind
(Nam
) = N_Selected_Component
2283 and then Nkind
(Prefix
(Nam
)) = N_Identifier
2284 and then Chars
(Prefix
(Nam
)) = Name_Gnat
2285 and then (Chars
(Selector_Name
(Nam
)) = Name_Most_Recent_Exception
2287 Chars
(Selector_Name
(Nam
)) = Name_Exception_Traces
)
2289 Check_Restriction
(No_Exception_Propagation
, N
);
2290 Special_Exception_Package_Used
:= True;
2293 -- Check for inappropriate with of internal implementation unit if we
2294 -- are currently compiling the main unit and the main unit is itself
2295 -- not an internal unit. We do not issue this message for implicit
2296 -- with's generated by the compiler itself.
2298 if Implementation_Unit_Warnings
2299 and then Current_Sem_Unit
= Main_Unit
2300 and then not Intunit
2301 and then not Implicit_With
(N
)
2302 and then not GNAT_Mode
2305 U_Kind
: constant Kind_Of_Unit
:=
2306 Get_Kind_Of_Unit
(Get_Source_Unit
(U
));
2309 if U_Kind
= Implementation_Unit
then
2310 Error_Msg_F
("& is an internal 'G'N'A'T unit?", Name
(N
));
2312 ("\use of this unit is non-portable " &
2313 "and version-dependent?",
2316 elsif U_Kind
= Ada_05_Unit
2317 and then Ada_Version
< Ada_05
2318 and then Warn_On_Ada_2005_Compatibility
2320 Error_Msg_N
("& is an Ada 2005 unit?", Name
(N
));
2326 -- Semantic analysis of a generic unit is performed on a copy of
2327 -- the original tree. Retrieve the entity on which semantic info
2328 -- actually appears.
2330 if Unit_Kind
in N_Generic_Declaration
then
2331 E_Name
:= Defining_Entity
(U
);
2333 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2334 -- the case of an instantiation, semantic analysis above will have
2335 -- replaced the unit by its instantiated version. If the instance body
2336 -- has been generated, the instance now denotes the body entity. For
2337 -- visibility purposes we need the entity of its spec.
2339 elsif (Unit_Kind
= N_Package_Instantiation
2340 or else Nkind
(Original_Node
(Unit
(Library_Unit
(N
)))) =
2341 N_Package_Instantiation
)
2342 and then Nkind
(U
) = N_Package_Body
2344 E_Name
:= Corresponding_Spec
(U
);
2346 elsif Unit_Kind
= N_Package_Instantiation
2347 and then Nkind
(U
) = N_Package_Instantiation
2349 -- If the instance has not been rewritten as a package declaration,
2350 -- then it appeared already in a previous with clause. Retrieve
2351 -- the entity from the previous instance.
2353 E_Name
:= Defining_Entity
(Specification
(Instance_Spec
(U
)));
2355 elsif Unit_Kind
in N_Subprogram_Instantiation
then
2357 -- Instantiation node is replaced with a wrapper package. Retrieve
2358 -- the visible subprogram created by the instance from corresponding
2359 -- attribute of the wrapper.
2361 E_Name
:= Related_Instance
(Defining_Entity
(U
));
2363 elsif Unit_Kind
= N_Package_Renaming_Declaration
2364 or else Unit_Kind
in N_Generic_Renaming_Declaration
2366 E_Name
:= Defining_Entity
(U
);
2368 elsif Unit_Kind
= N_Subprogram_Body
2369 and then Nkind
(Name
(N
)) = N_Selected_Component
2370 and then not Acts_As_Spec
(Library_Unit
(N
))
2372 -- For a child unit that has no spec, one has been created and
2373 -- analyzed. The entity required is that of the spec.
2375 E_Name
:= Corresponding_Spec
(U
);
2378 E_Name
:= Defining_Entity
(U
);
2381 if Nkind
(Name
(N
)) = N_Selected_Component
then
2383 -- Child unit in a with clause
2385 Change_Selected_Component_To_Expanded_Name
(Name
(N
));
2388 -- Restore style checks and restrictions
2390 Style_Check
:= Save_Style_Check
;
2391 Cunit_Boolean_Restrictions_Restore
(Save_C_Restrict
);
2393 -- Record the reference, but do NOT set the unit as referenced, we want
2394 -- to consider the unit as unreferenced if this is the only reference
2397 Set_Entity_With_Style_Check
(Name
(N
), E_Name
);
2398 Generate_Reference
(E_Name
, Name
(N
), 'w', Set_Ref
=> False);
2400 if Is_Child_Unit
(E_Name
) then
2401 Pref
:= Prefix
(Name
(N
));
2402 Par_Name
:= Scope
(E_Name
);
2403 while Nkind
(Pref
) = N_Selected_Component
loop
2404 Change_Selected_Component_To_Expanded_Name
(Pref
);
2405 Set_Entity_With_Style_Check
(Pref
, Par_Name
);
2407 Generate_Reference
(Par_Name
, Pref
);
2408 Pref
:= Prefix
(Pref
);
2410 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2411 -- is set to Standard_Standard, and no attempt should be made to
2412 -- further unwind scopes.
2414 if Par_Name
/= Standard_Standard
then
2415 Par_Name
:= Scope
(Par_Name
);
2419 if Present
(Entity
(Pref
))
2420 and then not Analyzed
(Parent
(Parent
(Entity
(Pref
))))
2422 -- If the entity is set without its unit being compiled, the
2423 -- original parent is a renaming, and Par_Name is the renamed
2424 -- entity. For visibility purposes, we need the original entity,
2425 -- which must be analyzed now because Load_Unit directly retrieves
2426 -- the renamed unit, and the renaming declaration itself has not
2429 Analyze
(Parent
(Parent
(Entity
(Pref
))));
2430 pragma Assert
(Renamed_Object
(Entity
(Pref
)) = Par_Name
);
2431 Par_Name
:= Entity
(Pref
);
2434 Set_Entity_With_Style_Check
(Pref
, Par_Name
);
2435 Generate_Reference
(Par_Name
, Pref
);
2438 -- If the withed unit is System, and a system extension pragma is
2439 -- present, compile the extension now, rather than waiting for a
2440 -- visibility check on a specific entity.
2442 if Chars
(E_Name
) = Name_System
2443 and then Scope
(E_Name
) = Standard_Standard
2444 and then Present
(System_Extend_Unit
)
2445 and then Present_System_Aux
(N
)
2447 -- If the extension is not present, an error will have been emitted
2452 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2453 -- to private_with units; they will be made visible later (just before
2454 -- the private part is analyzed)
2456 if Private_Present
(N
) then
2457 Set_Is_Immediately_Visible
(E_Name
, False);
2459 end Analyze_With_Clause
;
2461 ------------------------------
2462 -- Check_Private_Child_Unit --
2463 ------------------------------
2465 procedure Check_Private_Child_Unit
(N
: Node_Id
) is
2466 Lib_Unit
: constant Node_Id
:= Unit
(N
);
2468 Curr_Unit
: Entity_Id
;
2469 Sub_Parent
: Node_Id
;
2470 Priv_Child
: Entity_Id
;
2471 Par_Lib
: Entity_Id
;
2474 function Is_Private_Library_Unit
(Unit
: Entity_Id
) return Boolean;
2475 -- Returns true if and only if the library unit is declared with
2476 -- an explicit designation of private.
2478 function Is_Private_Library_Unit
(Unit
: Entity_Id
) return Boolean is
2479 Comp_Unit
: constant Node_Id
:= Parent
(Unit_Declaration_Node
(Unit
));
2482 return Private_Present
(Comp_Unit
);
2483 end Is_Private_Library_Unit
;
2485 -- Start of processing for Check_Private_Child_Unit
2488 if Nkind
(Lib_Unit
) = N_Package_Body
2489 or else Nkind
(Lib_Unit
) = N_Subprogram_Body
2491 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(N
)));
2492 Par_Lib
:= Curr_Unit
;
2494 elsif Nkind
(Lib_Unit
) = N_Subunit
then
2496 -- The parent is itself a body. The parent entity is to be found in
2497 -- the corresponding spec.
2499 Sub_Parent
:= Library_Unit
(N
);
2500 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(Sub_Parent
)));
2502 -- If the parent itself is a subunit, Curr_Unit is the entity
2503 -- of the enclosing body, retrieve the spec entity which is
2504 -- the proper ancestor we need for the following tests.
2506 if Ekind
(Curr_Unit
) = E_Package_Body
then
2507 Curr_Unit
:= Spec_Entity
(Curr_Unit
);
2510 Par_Lib
:= Curr_Unit
;
2513 Curr_Unit
:= Defining_Entity
(Lib_Unit
);
2515 Par_Lib
:= Curr_Unit
;
2516 Par_Spec
:= Parent_Spec
(Lib_Unit
);
2518 if No
(Par_Spec
) then
2521 Par_Lib
:= Defining_Entity
(Unit
(Par_Spec
));
2525 -- Loop through context items
2527 Item
:= First
(Context_Items
(N
));
2528 while Present
(Item
) loop
2530 -- Ada 2005 (AI-262): Allow private_with of a private child package
2531 -- in public siblings
2533 if Nkind
(Item
) = N_With_Clause
2534 and then not Implicit_With
(Item
)
2535 and then Is_Private_Descendant
(Entity
(Name
(Item
)))
2537 Priv_Child
:= Entity
(Name
(Item
));
2540 Curr_Parent
: Entity_Id
:= Par_Lib
;
2541 Child_Parent
: Entity_Id
:= Scope
(Priv_Child
);
2542 Prv_Ancestor
: Entity_Id
:= Child_Parent
;
2543 Curr_Private
: Boolean := Is_Private_Library_Unit
(Curr_Unit
);
2546 -- If the child unit is a public child then locate the nearest
2547 -- private ancestor. Child_Parent will then be set to the
2548 -- parent of that ancestor.
2550 if not Is_Private_Library_Unit
(Priv_Child
) then
2551 while Present
(Prv_Ancestor
)
2552 and then not Is_Private_Library_Unit
(Prv_Ancestor
)
2554 Prv_Ancestor
:= Scope
(Prv_Ancestor
);
2557 if Present
(Prv_Ancestor
) then
2558 Child_Parent
:= Scope
(Prv_Ancestor
);
2562 while Present
(Curr_Parent
)
2563 and then Curr_Parent
/= Standard_Standard
2564 and then Curr_Parent
/= Child_Parent
2567 Curr_Private
or else Is_Private_Library_Unit
(Curr_Parent
);
2568 Curr_Parent
:= Scope
(Curr_Parent
);
2571 if No
(Curr_Parent
) then
2572 Curr_Parent
:= Standard_Standard
;
2575 if Curr_Parent
/= Child_Parent
then
2576 if Ekind
(Priv_Child
) = E_Generic_Package
2577 and then Chars
(Priv_Child
) in Text_IO_Package_Name
2578 and then Chars
(Scope
(Scope
(Priv_Child
))) = Name_Ada
2581 ("& is a nested package, not a compilation unit",
2582 Name
(Item
), Priv_Child
);
2586 ("unit in with clause is private child unit!", Item
);
2588 ("\current unit must also have parent&!",
2589 Item
, Child_Parent
);
2592 elsif not Curr_Private
2593 and then not Private_Present
(Item
)
2594 and then Nkind
(Lib_Unit
) /= N_Package_Body
2595 and then Nkind
(Lib_Unit
) /= N_Subprogram_Body
2596 and then Nkind
(Lib_Unit
) /= N_Subunit
2599 ("current unit must also be private descendant of&",
2600 Item
, Child_Parent
);
2608 end Check_Private_Child_Unit
;
2610 ----------------------
2611 -- Check_Stub_Level --
2612 ----------------------
2614 procedure Check_Stub_Level
(N
: Node_Id
) is
2615 Par
: constant Node_Id
:= Parent
(N
);
2616 Kind
: constant Node_Kind
:= Nkind
(Par
);
2619 if (Kind
= N_Package_Body
2620 or else Kind
= N_Subprogram_Body
2621 or else Kind
= N_Task_Body
2622 or else Kind
= N_Protected_Body
)
2623 and then (Nkind
(Parent
(Par
)) = N_Compilation_Unit
2624 or else Nkind
(Parent
(Par
)) = N_Subunit
)
2628 -- In an instance, a missing stub appears at any level. A warning
2629 -- message will have been emitted already for the missing file.
2631 elsif not In_Instance
then
2632 Error_Msg_N
("stub cannot appear in an inner scope", N
);
2634 elsif Expander_Active
then
2635 Error_Msg_N
("missing proper body", N
);
2637 end Check_Stub_Level
;
2639 ------------------------
2640 -- Expand_With_Clause --
2641 ------------------------
2643 procedure Expand_With_Clause
(Item
: Node_Id
; Nam
: Node_Id
; N
: Node_Id
) is
2644 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
2645 Ent
: constant Entity_Id
:= Entity
(Nam
);
2649 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
;
2650 -- Comment requireed here ???
2652 ---------------------
2653 -- Build_Unit_Name --
2654 ---------------------
2656 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
is
2660 if Nkind
(Nam
) = N_Identifier
then
2661 return New_Occurrence_Of
(Entity
(Nam
), Loc
);
2665 Make_Expanded_Name
(Loc
,
2666 Chars
=> Chars
(Entity
(Nam
)),
2667 Prefix
=> Build_Unit_Name
(Prefix
(Nam
)),
2668 Selector_Name
=> New_Occurrence_Of
(Entity
(Nam
), Loc
));
2669 Set_Entity
(Result
, Entity
(Nam
));
2672 end Build_Unit_Name
;
2674 -- Start of processing for Expand_With_Clause
2677 New_Nodes_OK
:= New_Nodes_OK
+ 1;
2679 Make_With_Clause
(Loc
, Name
=> Build_Unit_Name
(Nam
));
2681 P
:= Parent
(Unit_Declaration_Node
(Ent
));
2682 Set_Library_Unit
(Withn
, P
);
2683 Set_Corresponding_Spec
(Withn
, Ent
);
2684 Set_First_Name
(Withn
, True);
2685 Set_Implicit_With
(Withn
, True);
2687 -- If the unit is a package declaration, a private_with_clause on a
2688 -- child unit implies that the implicit with on the parent is also
2691 if Nkind
(Unit
(N
)) = N_Package_Declaration
then
2692 Set_Private_Present
(Withn
, Private_Present
(Item
));
2695 Prepend
(Withn
, Context_Items
(N
));
2696 Mark_Rewrite_Insertion
(Withn
);
2697 Install_Withed_Unit
(Withn
);
2699 if Nkind
(Nam
) = N_Expanded_Name
then
2700 Expand_With_Clause
(Item
, Prefix
(Nam
), N
);
2703 New_Nodes_OK
:= New_Nodes_OK
- 1;
2704 end Expand_With_Clause
;
2706 -----------------------
2707 -- Get_Parent_Entity --
2708 -----------------------
2710 function Get_Parent_Entity
(Unit
: Node_Id
) return Entity_Id
is
2712 if Nkind
(Unit
) = N_Package_Body
2713 and then Nkind
(Original_Node
(Unit
)) = N_Package_Instantiation
2717 (Specification
(Instance_Spec
(Original_Node
(Unit
))));
2719 elsif Nkind
(Unit
) = N_Package_Instantiation
then
2720 return Defining_Entity
(Specification
(Instance_Spec
(Unit
)));
2723 return Defining_Entity
(Unit
);
2725 end Get_Parent_Entity
;
2727 -----------------------------
2728 -- Implicit_With_On_Parent --
2729 -----------------------------
2731 procedure Implicit_With_On_Parent
2732 (Child_Unit
: Node_Id
;
2735 Loc
: constant Source_Ptr
:= Sloc
(N
);
2736 P
: constant Node_Id
:= Parent_Spec
(Child_Unit
);
2737 P_Unit
: Node_Id
:= Unit
(P
);
2738 P_Name
: constant Entity_Id
:= Get_Parent_Entity
(P_Unit
);
2741 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
;
2742 -- Build prefix of child unit name. Recurse if needed
2744 function Build_Unit_Name
return Node_Id
;
2745 -- If the unit is a child unit, build qualified name with all ancestors
2747 -------------------------
2748 -- Build_Ancestor_Name --
2749 -------------------------
2751 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
is
2752 P_Ref
: constant Node_Id
:=
2753 New_Reference_To
(Defining_Entity
(P
), Loc
);
2754 P_Spec
: Node_Id
:= P
;
2757 -- Ancestor may have been rewritten as a package body. Retrieve
2758 -- the original spec to trace earlier ancestors.
2760 if Nkind
(P
) = N_Package_Body
2761 and then Nkind
(Original_Node
(P
)) = N_Package_Instantiation
2763 P_Spec
:= Original_Node
(P
);
2766 if No
(Parent_Spec
(P_Spec
)) then
2770 Make_Selected_Component
(Loc
,
2771 Prefix
=> Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Spec
))),
2772 Selector_Name
=> P_Ref
);
2774 end Build_Ancestor_Name
;
2776 ---------------------
2777 -- Build_Unit_Name --
2778 ---------------------
2780 function Build_Unit_Name
return Node_Id
is
2783 if No
(Parent_Spec
(P_Unit
)) then
2784 return New_Reference_To
(P_Name
, Loc
);
2787 Make_Expanded_Name
(Loc
,
2788 Chars
=> Chars
(P_Name
),
2789 Prefix
=> Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Unit
))),
2790 Selector_Name
=> New_Reference_To
(P_Name
, Loc
));
2791 Set_Entity
(Result
, P_Name
);
2794 end Build_Unit_Name
;
2796 -- Start of processing for Implicit_With_On_Parent
2799 -- The unit of the current compilation may be a package body that
2800 -- replaces an instance node. In this case we need the original instance
2801 -- node to construct the proper parent name.
2803 if Nkind
(P_Unit
) = N_Package_Body
2804 and then Nkind
(Original_Node
(P_Unit
)) = N_Package_Instantiation
2806 P_Unit
:= Original_Node
(P_Unit
);
2809 -- We add the implicit with if the child unit is the current unit being
2810 -- compiled. If the current unit is a body, we do not want to add an
2811 -- implicit_with a second time to the corresponding spec.
2813 if Nkind
(Child_Unit
) = N_Package_Declaration
2814 and then Child_Unit
/= Unit
(Cunit
(Current_Sem_Unit
))
2819 New_Nodes_OK
:= New_Nodes_OK
+ 1;
2820 Withn
:= Make_With_Clause
(Loc
, Name
=> Build_Unit_Name
);
2822 Set_Library_Unit
(Withn
, P
);
2823 Set_Corresponding_Spec
(Withn
, P_Name
);
2824 Set_First_Name
(Withn
, True);
2825 Set_Implicit_With
(Withn
, True);
2827 -- Node is placed at the beginning of the context items, so that
2828 -- subsequent use clauses on the parent can be validated.
2830 Prepend
(Withn
, Context_Items
(N
));
2831 Mark_Rewrite_Insertion
(Withn
);
2832 Install_Withed_Unit
(Withn
);
2834 if Is_Child_Spec
(P_Unit
) then
2835 Implicit_With_On_Parent
(P_Unit
, N
);
2838 New_Nodes_OK
:= New_Nodes_OK
- 1;
2839 end Implicit_With_On_Parent
;
2845 function In_Chain
(E
: Entity_Id
) return Boolean is
2849 H
:= Current_Entity
(E
);
2850 while Present
(H
) loop
2861 ---------------------
2862 -- Install_Context --
2863 ---------------------
2865 procedure Install_Context
(N
: Node_Id
) is
2866 Lib_Unit
: constant Node_Id
:= Unit
(N
);
2869 Install_Context_Clauses
(N
);
2871 if Is_Child_Spec
(Lib_Unit
) then
2872 Install_Parents
(Lib_Unit
, Private_Present
(Parent
(Lib_Unit
)));
2875 Install_Limited_Context_Clauses
(N
);
2877 end Install_Context
;
2879 -----------------------------
2880 -- Install_Context_Clauses --
2881 -----------------------------
2883 procedure Install_Context_Clauses
(N
: Node_Id
) is
2884 Lib_Unit
: constant Node_Id
:= Unit
(N
);
2886 Uname_Node
: Entity_Id
;
2887 Check_Private
: Boolean := False;
2888 Decl_Node
: Node_Id
;
2889 Lib_Parent
: Entity_Id
;
2892 -- First skip configuration pragmas at the start of the context. They
2893 -- are not technically part of the context clause, but that's where the
2894 -- parser puts them. Note they were analyzed in Analyze_Context.
2896 Item
:= First
(Context_Items
(N
));
2897 while Present
(Item
)
2898 and then Nkind
(Item
) = N_Pragma
2899 and then Chars
(Item
) in Configuration_Pragma_Names
2904 -- Loop through the actual context clause items. We process everything
2905 -- except Limited_With clauses in this routine. Limited_With clauses
2906 -- are separately installed (see Install_Limited_Context_Clauses).
2908 while Present
(Item
) loop
2910 -- Case of explicit WITH clause
2912 if Nkind
(Item
) = N_With_Clause
2913 and then not Implicit_With
(Item
)
2915 if Limited_Present
(Item
) then
2917 -- Limited withed units will be installed later
2921 -- If Name (Item) is not an entity name, something is wrong, and
2922 -- this will be detected in due course, for now ignore the item
2924 elsif not Is_Entity_Name
(Name
(Item
)) then
2927 elsif No
(Entity
(Name
(Item
))) then
2928 Set_Entity
(Name
(Item
), Any_Id
);
2932 Uname_Node
:= Entity
(Name
(Item
));
2934 if Is_Private_Descendant
(Uname_Node
) then
2935 Check_Private
:= True;
2938 Install_Withed_Unit
(Item
);
2940 Decl_Node
:= Unit_Declaration_Node
(Uname_Node
);
2942 -- If the unit is a subprogram instance, it appears nested within
2943 -- a package that carries the parent information.
2945 if Is_Generic_Instance
(Uname_Node
)
2946 and then Ekind
(Uname_Node
) /= E_Package
2948 Decl_Node
:= Parent
(Parent
(Decl_Node
));
2951 if Is_Child_Spec
(Decl_Node
) then
2952 if Nkind
(Name
(Item
)) = N_Expanded_Name
then
2953 Expand_With_Clause
(Item
, Prefix
(Name
(Item
)), N
);
2955 -- if not an expanded name, the child unit must be a
2956 -- renaming, nothing to do.
2961 elsif Nkind
(Decl_Node
) = N_Subprogram_Body
2962 and then not Acts_As_Spec
(Parent
(Decl_Node
))
2963 and then Is_Child_Spec
(Unit
(Library_Unit
(Parent
(Decl_Node
))))
2965 Implicit_With_On_Parent
2966 (Unit
(Library_Unit
(Parent
(Decl_Node
))), N
);
2969 -- Check license conditions unless this is a dummy unit
2971 if Sloc
(Library_Unit
(Item
)) /= No_Location
then
2972 License_Check
: declare
2974 Withu
: constant Unit_Number_Type
:=
2975 Get_Source_Unit
(Library_Unit
(Item
));
2977 Withl
: constant License_Type
:=
2978 License
(Source_Index
(Withu
));
2980 Unitl
: constant License_Type
:=
2981 License
(Source_Index
(Current_Sem_Unit
));
2983 procedure License_Error
;
2984 -- Signal error of bad license
2990 procedure License_Error
is
2993 ("?license of with'ed unit & may be inconsistent",
2997 -- Start of processing for License_Check
3000 -- Exclude license check if withed unit is an internal unit.
3001 -- This situation arises e.g. with the GPL version of GNAT.
3003 if Is_Internal_File_Name
(Unit_File_Name
(Withu
)) then
3006 -- Otherwise check various cases
3018 if Withl
= Restricted
then
3022 when Modified_GPL
=>
3023 if Withl
= Restricted
or else Withl
= GPL
then
3027 when Unrestricted
=>
3034 -- Case of USE PACKAGE clause
3036 elsif Nkind
(Item
) = N_Use_Package_Clause
then
3037 Analyze_Use_Package
(Item
);
3039 -- Case of USE TYPE clause
3041 elsif Nkind
(Item
) = N_Use_Type_Clause
then
3042 Analyze_Use_Type
(Item
);
3046 elsif Nkind
(Item
) = N_Pragma
then
3054 if Is_Child_Spec
(Lib_Unit
) then
3056 -- The unit also has implicit withs on its own parents
3058 if No
(Context_Items
(N
)) then
3059 Set_Context_Items
(N
, New_List
);
3062 Implicit_With_On_Parent
(Lib_Unit
, N
);
3065 -- If the unit is a body, the context of the specification must also
3068 if Nkind
(Lib_Unit
) = N_Package_Body
3069 or else (Nkind
(Lib_Unit
) = N_Subprogram_Body
3070 and then not Acts_As_Spec
(N
))
3072 Install_Context
(Library_Unit
(N
));
3074 if Is_Child_Spec
(Unit
(Library_Unit
(N
))) then
3076 -- If the unit is the body of a public child unit, the private
3077 -- declarations of the parent must be made visible. If the child
3078 -- unit is private, the private declarations have been installed
3079 -- already in the call to Install_Parents for the spec. Installing
3080 -- private declarations must be done for all ancestors of public
3081 -- child units. In addition, sibling units mentioned in the
3082 -- context clause of the body are directly visible.
3090 Lib_Spec
:= Unit
(Library_Unit
(N
));
3091 while Is_Child_Spec
(Lib_Spec
) loop
3092 P
:= Unit
(Parent_Spec
(Lib_Spec
));
3093 P_Name
:= Defining_Entity
(P
);
3095 if not (Private_Present
(Parent
(Lib_Spec
)))
3096 and then not In_Private_Part
(P_Name
)
3098 Install_Private_Declarations
(P_Name
);
3099 Install_Private_With_Clauses
(P_Name
);
3100 Set_Use
(Private_Declarations
(Specification
(P
)));
3108 -- For a package body, children in context are immediately visible
3110 Install_Siblings
(Defining_Entity
(Unit
(Library_Unit
(N
))), N
);
3113 if Nkind
(Lib_Unit
) = N_Generic_Package_Declaration
3114 or else Nkind
(Lib_Unit
) = N_Generic_Subprogram_Declaration
3115 or else Nkind
(Lib_Unit
) = N_Package_Declaration
3116 or else Nkind
(Lib_Unit
) = N_Subprogram_Declaration
3118 if Is_Child_Spec
(Lib_Unit
) then
3119 Lib_Parent
:= Defining_Entity
(Unit
(Parent_Spec
(Lib_Unit
)));
3120 Set_Is_Private_Descendant
3121 (Defining_Entity
(Lib_Unit
),
3122 Is_Private_Descendant
(Lib_Parent
)
3123 or else Private_Present
(Parent
(Lib_Unit
)));
3126 Set_Is_Private_Descendant
3127 (Defining_Entity
(Lib_Unit
),
3128 Private_Present
(Parent
(Lib_Unit
)));
3132 if Check_Private
then
3133 Check_Private_Child_Unit
(N
);
3135 end Install_Context_Clauses
;
3137 -------------------------------------
3138 -- Install_Limited_Context_Clauses --
3139 -------------------------------------
3141 procedure Install_Limited_Context_Clauses
(N
: Node_Id
) is
3144 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
);
3145 -- Check that the unlimited view of a given compilation_unit is not
3146 -- already visible through "use + renamings".
3148 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
);
3149 -- Check that if a limited_with clause of a given compilation_unit
3150 -- mentions a descendant of a private child of some library unit,
3151 -- then the given compilation_unit shall be the declaration of a
3152 -- private descendant of that library unit.
3154 procedure Expand_Limited_With_Clause
3155 (Comp_Unit
: Node_Id
;
3158 -- If a child unit appears in a limited_with clause, there are implicit
3159 -- limited_with clauses on all parents that are not already visible
3160 -- through a regular with clause. This procedure creates the implicit
3161 -- limited with_clauses for the parents and loads the corresponding
3162 -- units. The shadow entities are created when the inserted clause is
3163 -- analyzed. Implements Ada 2005 (AI-50217).
3165 ---------------------
3166 -- Check_Renamings --
3167 ---------------------
3169 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
) is
3178 pragma Assert
(Nkind
(W
) = N_With_Clause
);
3180 -- Protect the frontend against previous critical errors
3182 case Nkind
(Unit
(Library_Unit
(W
))) is
3183 when N_Subprogram_Declaration |
3184 N_Package_Declaration |
3185 N_Generic_Subprogram_Declaration |
3186 N_Generic_Package_Declaration
=>
3193 -- Check "use + renamings"
3195 WEnt
:= Defining_Unit_Name
(Specification
(Unit
(Library_Unit
(W
))));
3196 Spec
:= Specification
(Unit
(P
));
3198 Item
:= First
(Visible_Declarations
(Spec
));
3199 while Present
(Item
) loop
3201 -- Look only at use package clauses
3203 if Nkind
(Item
) = N_Use_Package_Clause
then
3205 -- Traverse the list of packages
3207 Nam
:= First
(Names
(Item
));
3208 while Present
(Nam
) loop
3211 pragma Assert
(Present
(Parent
(E
)));
3213 if Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
3214 and then Renamed_Entity
(E
) = WEnt
3216 -- The unlimited view is visible through use clause and
3217 -- renamings. There is not need to generate the error
3218 -- message here because Is_Visible_Through_Renamings
3219 -- takes care of generating the precise error message.
3223 elsif Nkind
(Parent
(E
)) = N_Package_Specification
then
3225 -- The use clause may refer to a local package.
3226 -- Check all the enclosing scopes.
3229 while E2
/= Standard_Standard
3237 ("unlimited view visible through use clause ", W
);
3249 -- Recursive call to check all the ancestors
3251 if Is_Child_Spec
(Unit
(P
)) then
3252 Check_Renamings
(P
=> Parent_Spec
(Unit
(P
)), W
=> W
);
3254 end Check_Renamings
;
3256 ---------------------------------------
3257 -- Check_Private_Limited_Withed_Unit --
3258 ---------------------------------------
3260 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
) is
3261 Curr_Parent
: Node_Id
;
3262 Child_Parent
: Node_Id
;
3265 -- Compilation unit of the parent of the withed library unit
3267 Child_Parent
:= Parent_Spec
(Unit
(Library_Unit
(Item
)));
3269 -- If the child unit is a public child, then locate its nearest
3270 -- private ancestor, if any; Child_Parent will then be set to
3271 -- the parent of that ancestor.
3273 if not Private_Present
(Library_Unit
(Item
)) then
3274 while Present
(Child_Parent
)
3275 and then not Private_Present
(Child_Parent
)
3277 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
3280 if No
(Child_Parent
) then
3284 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
3287 -- Traverse all the ancestors of the current compilation
3288 -- unit to check if it is a descendant of named library unit.
3290 Curr_Parent
:= Parent
(Item
);
3291 while Present
(Parent_Spec
(Unit
(Curr_Parent
)))
3292 and then Curr_Parent
/= Child_Parent
3294 Curr_Parent
:= Parent_Spec
(Unit
(Curr_Parent
));
3297 if Curr_Parent
/= Child_Parent
then
3299 ("unit in with clause is private child unit!", Item
);
3301 ("\current unit must also have parent&!",
3302 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
3304 elsif not Private_Present
(Parent
(Item
))
3305 and then not Private_Present
(Item
)
3306 and then Nkind
(Unit
(Parent
(Item
))) /= N_Package_Body
3307 and then Nkind
(Unit
(Parent
(Item
))) /= N_Subprogram_Body
3308 and then Nkind
(Unit
(Parent
(Item
))) /= N_Subunit
3311 ("current unit must also be private descendant of&",
3312 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
3314 end Check_Private_Limited_Withed_Unit
;
3316 --------------------------------
3317 -- Expand_Limited_With_Clause --
3318 --------------------------------
3320 procedure Expand_Limited_With_Clause
3321 (Comp_Unit
: Node_Id
;
3325 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
3326 Unum
: Unit_Number_Type
;
3329 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean;
3330 -- Returns true if the context already includes a with_clause for
3331 -- this unit. If the with_clause is non-limited, the unit is fully
3332 -- visible and an implicit limited_with should not be created. If
3333 -- there is already a limited_with clause for W, a second one is
3334 -- simply redundant.
3336 --------------------------
3337 -- Previous_Withed_Unit --
3338 --------------------------
3340 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean is
3344 -- A limited with_clause cannot appear in the same context_clause
3345 -- as a nonlimited with_clause which mentions the same library.
3347 Item
:= First
(Context_Items
(Comp_Unit
));
3348 while Present
(Item
) loop
3349 if Nkind
(Item
) = N_With_Clause
3350 and then Library_Unit
(Item
) = Library_Unit
(W
)
3359 end Previous_Withed_Unit
;
3361 -- Start of processing for Expand_Limited_With_Clause
3364 New_Nodes_OK
:= New_Nodes_OK
+ 1;
3366 if Nkind
(Nam
) = N_Identifier
then
3368 -- Create node for name of withed unit
3371 Make_With_Clause
(Loc
,
3372 Name
=> New_Copy
(Nam
));
3374 else pragma Assert
(Nkind
(Nam
) = N_Selected_Component
);
3376 Make_With_Clause
(Loc
,
3377 Name
=> Make_Selected_Component
(Loc
,
3378 Prefix
=> New_Copy_Tree
(Prefix
(Nam
)),
3379 Selector_Name
=> New_Copy
(Selector_Name
(Nam
))));
3380 Set_Parent
(Withn
, Parent
(N
));
3383 Set_Limited_Present
(Withn
);
3384 Set_First_Name
(Withn
);
3385 Set_Implicit_With
(Withn
);
3389 (Load_Name
=> Get_Spec_Name
(Get_Unit_Name
(Nam
)),
3394 -- Do not generate a limited_with_clause on the current unit.
3395 -- This path is taken when a unit has a limited_with clause on
3396 -- one of its child units.
3398 if Unum
= Current_Sem_Unit
then
3402 Set_Library_Unit
(Withn
, Cunit
(Unum
));
3403 Set_Corresponding_Spec
3404 (Withn
, Specification
(Unit
(Cunit
(Unum
))));
3406 if not Previous_Withed_Unit
(Withn
) then
3407 Prepend
(Withn
, Context_Items
(Parent
(N
)));
3408 Mark_Rewrite_Insertion
(Withn
);
3410 -- Add implicit limited_with_clauses for parents of child units
3411 -- mentioned in limited_with clauses.
3413 if Nkind
(Nam
) = N_Selected_Component
then
3414 Expand_Limited_With_Clause
(Comp_Unit
, Prefix
(Nam
), N
);
3419 if not Limited_View_Installed
(Withn
) then
3420 Install_Limited_Withed_Unit
(Withn
);
3424 New_Nodes_OK
:= New_Nodes_OK
- 1;
3425 end Expand_Limited_With_Clause
;
3427 -- Start of processing for Install_Limited_Context_Clauses
3430 Item
:= First
(Context_Items
(N
));
3431 while Present
(Item
) loop
3432 if Nkind
(Item
) = N_With_Clause
3433 and then Limited_Present
(Item
)
3435 if Nkind
(Name
(Item
)) = N_Selected_Component
then
3436 Expand_Limited_With_Clause
3437 (Comp_Unit
=> N
, Nam
=> Prefix
(Name
(Item
)), N
=> Item
);
3440 Check_Private_Limited_Withed_Unit
(Item
);
3442 if not Implicit_With
(Item
)
3443 and then Is_Child_Spec
(Unit
(N
))
3445 Check_Renamings
(Parent_Spec
(Unit
(N
)), Item
);
3448 -- A unit may have a limited with on itself if it has a limited
3449 -- with_clause on one of its child units. In that case it is
3450 -- already being compiled and it makes no sense to install its
3453 -- If the item is a limited_private_with_clause, install it if the
3454 -- current unit is a body or if it is a private child. Otherwise
3455 -- the private clause is installed before analyzing the private
3456 -- part of the current unit.
3458 if Library_Unit
(Item
) /= Cunit
(Current_Sem_Unit
)
3459 and then not Limited_View_Installed
(Item
)
3461 if not Private_Present
(Item
)
3462 or else Private_Present
(N
)
3463 or else Nkind
(Unit
(N
)) = N_Package_Body
3464 or else Nkind
(Unit
(N
)) = N_Subprogram_Body
3465 or else Nkind
(Unit
(N
)) = N_Subunit
3467 Install_Limited_Withed_Unit
(Item
);
3471 -- All items other than Limited_With clauses are ignored (they were
3472 -- installed separately early on by Install_Context_Clause).
3481 -- Ada 2005 (AI-412): Examine the visible declarations of a package
3482 -- spec, looking for incomplete subtype declarations of incomplete
3483 -- types visible through a limited with clause.
3485 if Ada_Version
>= Ada_05
3486 and then Analyzed
(N
)
3487 and then Nkind
(Unit
(N
)) = N_Package_Declaration
3492 Non_Lim_View
: Entity_Id
;
3495 Decl
:= First
(Visible_Declarations
(Specification
(Unit
(N
))));
3496 while Present
(Decl
) loop
3497 if Nkind
(Decl
) = N_Subtype_Declaration
3499 Ekind
(Defining_Identifier
(Decl
)) = E_Incomplete_Subtype
3501 From_With_Type
(Defining_Identifier
(Decl
))
3503 Def_Id
:= Defining_Identifier
(Decl
);
3504 Non_Lim_View
:= Non_Limited_View
(Def_Id
);
3506 if not Is_Incomplete_Type
(Non_Lim_View
) then
3508 -- Convert an incomplete subtype declaration into a
3509 -- corresponding non-limited view subtype declaration.
3510 -- This is usually the case when analyzing a body that
3511 -- has regular with-clauses, when the spec has limited
3514 -- If the non-limited view is still incomplete, it is
3515 -- the dummy entry already created, and the declaration
3516 -- cannot be reanalyzed. This is the case when installing
3517 -- a parent unit that has limited with-clauses.
3519 Set_Subtype_Indication
(Decl
,
3520 New_Reference_To
(Non_Lim_View
, Sloc
(Def_Id
)));
3521 Set_Etype
(Def_Id
, Non_Lim_View
);
3522 Set_Ekind
(Def_Id
, Subtype_Kind
(Ekind
(Non_Lim_View
)));
3523 Set_Analyzed
(Decl
, False);
3525 -- Reanalyze the declaration, suppressing the call to
3526 -- Enter_Name to avoid duplicate names.
3528 Analyze_Subtype_Declaration
3538 end Install_Limited_Context_Clauses
;
3540 ---------------------
3541 -- Install_Parents --
3542 ---------------------
3544 procedure Install_Parents
(Lib_Unit
: Node_Id
; Is_Private
: Boolean) is
3551 P
:= Unit
(Parent_Spec
(Lib_Unit
));
3552 P_Name
:= Get_Parent_Entity
(P
);
3554 if Etype
(P_Name
) = Any_Type
then
3558 if Ekind
(P_Name
) = E_Generic_Package
3559 and then Nkind
(Lib_Unit
) /= N_Generic_Subprogram_Declaration
3560 and then Nkind
(Lib_Unit
) /= N_Generic_Package_Declaration
3561 and then Nkind
(Lib_Unit
) not in N_Generic_Renaming_Declaration
3564 ("child of a generic package must be a generic unit", Lib_Unit
);
3566 elsif not Is_Package_Or_Generic_Package
(P_Name
) then
3568 ("parent unit must be package or generic package", Lib_Unit
);
3569 raise Unrecoverable_Error
;
3571 elsif Present
(Renamed_Object
(P_Name
)) then
3572 Error_Msg_N
("parent unit cannot be a renaming", Lib_Unit
);
3573 raise Unrecoverable_Error
;
3575 -- Verify that a child of an instance is itself an instance, or the
3576 -- renaming of one. Given that an instance that is a unit is replaced
3577 -- with a package declaration, check against the original node. The
3578 -- parent may be currently being instantiated, in which case it appears
3579 -- as a declaration, but the generic_parent is already established
3580 -- indicating that we deal with an instance.
3582 elsif Nkind
(Original_Node
(P
)) = N_Package_Instantiation
then
3584 if Nkind
(Lib_Unit
) in N_Renaming_Declaration
3585 or else Nkind
(Original_Node
(Lib_Unit
)) in N_Generic_Instantiation
3587 (Nkind
(Lib_Unit
) = N_Package_Declaration
3588 and then Present
(Generic_Parent
(Specification
(Lib_Unit
))))
3593 ("child of an instance must be an instance or renaming",
3598 -- This is the recursive call that ensures all parents are loaded
3600 if Is_Child_Spec
(P
) then
3602 Is_Private
or else Private_Present
(Parent
(Lib_Unit
)));
3605 -- Now we can install the context for this parent
3607 Install_Context_Clauses
(Parent_Spec
(Lib_Unit
));
3608 Install_Limited_Context_Clauses
(Parent_Spec
(Lib_Unit
));
3609 Install_Siblings
(P_Name
, Parent
(Lib_Unit
));
3611 -- The child unit is in the declarative region of the parent. The parent
3612 -- must therefore appear in the scope stack and be visible, as when
3613 -- compiling the corresponding body. If the child unit is private or it
3614 -- is a package body, private declarations must be accessible as well.
3615 -- Use declarations in the parent must also be installed. Finally, other
3616 -- child units of the same parent that are in the context are
3617 -- immediately visible.
3619 -- Find entity for compilation unit, and set its private descendant
3620 -- status as needed.
3622 E_Name
:= Defining_Entity
(Lib_Unit
);
3624 Set_Is_Child_Unit
(E_Name
);
3626 Set_Is_Private_Descendant
(E_Name
,
3627 Is_Private_Descendant
(P_Name
)
3628 or else Private_Present
(Parent
(Lib_Unit
)));
3630 P_Spec
:= Specification
(Unit_Declaration_Node
(P_Name
));
3631 Push_Scope
(P_Name
);
3633 -- Save current visibility of unit
3635 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
:=
3636 Is_Immediately_Visible
(P_Name
);
3637 Set_Is_Immediately_Visible
(P_Name
);
3638 Install_Visible_Declarations
(P_Name
);
3639 Set_Use
(Visible_Declarations
(P_Spec
));
3641 -- If the parent is a generic unit, its formal part may contain formal
3642 -- packages and use clauses for them.
3644 if Ekind
(P_Name
) = E_Generic_Package
then
3645 Set_Use
(Generic_Formal_Declarations
(Parent
(P_Spec
)));
3649 or else Private_Present
(Parent
(Lib_Unit
))
3651 Install_Private_Declarations
(P_Name
);
3652 Install_Private_With_Clauses
(P_Name
);
3653 Set_Use
(Private_Declarations
(P_Spec
));
3655 end Install_Parents
;
3657 ----------------------------------
3658 -- Install_Private_With_Clauses --
3659 ----------------------------------
3661 procedure Install_Private_With_Clauses
(P
: Entity_Id
) is
3662 Decl
: constant Node_Id
:= Unit_Declaration_Node
(P
);
3666 if Debug_Flag_I
then
3667 Write_Str
("install private with clauses of ");
3668 Write_Name
(Chars
(P
));
3672 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
3673 Item
:= First
(Context_Items
(Parent
(Decl
)));
3674 while Present
(Item
) loop
3675 if Nkind
(Item
) = N_With_Clause
3676 and then Private_Present
(Item
)
3678 if Limited_Present
(Item
) then
3679 if not Limited_View_Installed
(Item
) then
3680 Install_Limited_Withed_Unit
(Item
);
3683 Install_Withed_Unit
(Item
, Private_With_OK
=> True);
3690 end Install_Private_With_Clauses
;
3692 ----------------------
3693 -- Install_Siblings --
3694 ----------------------
3696 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
) is
3701 -- Iterate over explicit with clauses, and check whether the scope of
3702 -- each entity is an ancestor of the current unit, in which case it is
3703 -- immediately visible.
3705 Item
:= First
(Context_Items
(N
));
3706 while Present
(Item
) loop
3708 -- Do not install private_with_clauses if the unit is a package
3709 -- declaration, unless it is itself a private child unit.
3711 if Nkind
(Item
) = N_With_Clause
3712 and then not Implicit_With
(Item
)
3713 and then not Limited_Present
(Item
)
3715 (not Private_Present
(Item
)
3716 or else Nkind
(Unit
(N
)) /= N_Package_Declaration
3717 or else Private_Present
(N
))
3719 Id
:= Entity
(Name
(Item
));
3721 if Is_Child_Unit
(Id
)
3722 and then Is_Ancestor_Package
(Scope
(Id
), U_Name
)
3724 Set_Is_Immediately_Visible
(Id
);
3726 -- Check for the presence of another unit in the context,
3727 -- that may be inadvertently hidden by the child.
3729 Prev
:= Current_Entity
(Id
);
3732 and then Is_Immediately_Visible
(Prev
)
3733 and then not Is_Child_Unit
(Prev
)
3739 Clause
:= First
(Context_Items
(N
));
3740 while Present
(Clause
) loop
3741 if Nkind
(Clause
) = N_With_Clause
3742 and then Entity
(Name
(Clause
)) = Prev
3745 ("child unit& hides compilation unit " &
3746 "with the same name?",
3756 -- The With_Clause may be on a grand-child or one of its further
3757 -- descendants, which makes a child immediately visible. Examine
3758 -- ancestry to determine whether such a child exists. For example,
3759 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
3760 -- is immediately visible.
3762 elsif Is_Child_Unit
(Id
) then
3768 while Is_Child_Unit
(Par
) loop
3769 if Is_Ancestor_Package
(Scope
(Par
), U_Name
) then
3770 Set_Is_Immediately_Visible
(Par
);
3782 end Install_Siblings
;
3784 -------------------------------
3785 -- Install_Limited_With_Unit --
3786 -------------------------------
3788 procedure Install_Limited_Withed_Unit
(N
: Node_Id
) is
3789 P_Unit
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
3792 Is_Child_Package
: Boolean := False;
3793 Lim_Header
: Entity_Id
;
3794 Lim_Typ
: Entity_Id
;
3796 function Has_Limited_With_Clause
3797 (C_Unit
: Entity_Id
;
3798 Pack
: Entity_Id
) return Boolean;
3799 -- Determine whether any package in the ancestor chain starting with
3800 -- C_Unit has a limited with clause for package Pack.
3802 function Has_With_Clause
3805 Is_Limited
: Boolean := False) return Boolean;
3806 -- Determine whether compilation unit C_Unit contains a with clause
3807 -- for package Pack. Use flag Is_Limited to designate desired clause
3808 -- kind. This is a subsidiary routine to Has_Limited_With_Clause.
3810 function Is_Visible_Through_Renamings
(P
: Entity_Id
) return Boolean;
3811 -- Check if some package installed though normal with-clauses has a
3812 -- renaming declaration of package P. AARM 10.1.2(21/2).
3814 -----------------------------
3815 -- Has_Limited_With_Clause --
3816 -----------------------------
3818 function Has_Limited_With_Clause
3819 (C_Unit
: Entity_Id
;
3820 Pack
: Entity_Id
) return Boolean
3827 while Present
(Par
) loop
3828 if Ekind
(Par
) /= E_Package
then
3832 -- Retrieve the Compilation_Unit node for Par and determine if
3833 -- its context clauses contain a limited with for Pack.
3835 Par_Unit
:= Parent
(Parent
(Parent
(Par
)));
3837 if Nkind
(Par_Unit
) = N_Package_Declaration
then
3838 Par_Unit
:= Parent
(Par_Unit
);
3841 if Has_With_Clause
(Par_Unit
, Pack
, True) then
3845 -- If there are more ancestors, climb up the tree, otherwise
3848 if Is_Child_Unit
(Par
) then
3856 end Has_Limited_With_Clause
;
3858 ---------------------
3859 -- Has_With_Clause --
3860 ---------------------
3862 function Has_With_Clause
3865 Is_Limited
: Boolean := False) return Boolean
3871 if Present
(Context_Items
(C_Unit
)) then
3872 Item
:= First
(Context_Items
(C_Unit
));
3873 while Present
(Item
) loop
3874 if Nkind
(Item
) = N_With_Clause
then
3876 -- Retrieve the entity of the imported compilation unit
3878 if Nkind
(Name
(Item
)) = N_Selected_Component
then
3879 Nam
:= Entity
(Selector_Name
(Name
(Item
)));
3881 Nam
:= Entity
(Name
(Item
));
3886 ((Is_Limited
and then Limited_Present
(Item
))
3888 (not Is_Limited
and then not Limited_Present
(Item
)))
3899 end Has_With_Clause
;
3901 ----------------------------------
3902 -- Is_Visible_Through_Renamings --
3903 ----------------------------------
3905 function Is_Visible_Through_Renamings
(P
: Entity_Id
) return Boolean is
3906 Kind
: constant Node_Kind
:=
3907 Nkind
(Unit
(Cunit
(Current_Sem_Unit
)));
3913 -- Example of the error detected by this subprogram:
3921 -- package Ren_P renames P;
3927 -- limited with P; -- ERROR
3928 -- package R.C is ...
3930 Aux_Unit
:= Cunit
(Current_Sem_Unit
);
3933 Item
:= First
(Context_Items
(Aux_Unit
));
3934 while Present
(Item
) loop
3935 if Nkind
(Item
) = N_With_Clause
3936 and then not Limited_Present
(Item
)
3937 and then Nkind
(Unit
(Library_Unit
(Item
)))
3938 = N_Package_Declaration
3941 First
(Visible_Declarations
3942 (Specification
(Unit
(Library_Unit
(Item
)))));
3943 while Present
(Decl
) loop
3944 if Nkind
(Decl
) = N_Package_Renaming_Declaration
3945 and then Entity
(Name
(Decl
)) = P
3947 -- Generate the error message only if the current unit
3948 -- is a package declaration; in case of subprogram
3949 -- bodies and package bodies we just return true to
3950 -- indicate that the limited view must not be
3953 if Kind
= N_Package_Declaration
then
3955 ("simultaneous visibility of the limited and " &
3956 "unlimited views not allowed", N
);
3957 Error_Msg_Sloc
:= Sloc
(Item
);
3959 ("\\ unlimited view of & visible through the " &
3960 "context clause #", N
, P
);
3961 Error_Msg_Sloc
:= Sloc
(Decl
);
3962 Error_Msg_NE
("\\ and the renaming #", N
, P
);
3975 if Present
(Library_Unit
(Aux_Unit
)) then
3976 if Aux_Unit
= Library_Unit
(Aux_Unit
) then
3978 -- Aux_Unit is a body that acts as a spec. Clause has
3979 -- already been flagged as illegal.
3984 Aux_Unit
:= Library_Unit
(Aux_Unit
);
3987 Aux_Unit
:= Parent_Spec
(Unit
(Aux_Unit
));
3990 exit when No
(Aux_Unit
);
3994 end Is_Visible_Through_Renamings
;
3996 -- Start of processing for Install_Limited_Withed_Unit
3999 pragma Assert
(not Limited_View_Installed
(N
));
4001 -- In case of limited with_clause on subprograms, generics, instances,
4002 -- or renamings, the corresponding error was previously posted and we
4003 -- have nothing to do here.
4005 if Nkind
(P_Unit
) /= N_Package_Declaration
then
4009 P
:= Defining_Unit_Name
(Specification
(P_Unit
));
4011 -- Handle child packages
4013 if Nkind
(P
) = N_Defining_Program_Unit_Name
then
4014 Is_Child_Package
:= True;
4015 P
:= Defining_Identifier
(P
);
4018 -- Do not install the limited-view if the full-view is already visible
4019 -- through renaming declarations.
4021 if Is_Visible_Through_Renamings
(P
) then
4025 -- Do not install the limited view if this is the unit being analyzed.
4026 -- This unusual case will happen when a unit has a limited_with clause
4027 -- on one of its children. The compilation of the child forces the
4028 -- load of the parent which tries to install the limited view of the
4029 -- child again. Installing the limited view must also be disabled
4030 -- when compiling the body of the child unit.
4032 if P
= Cunit_Entity
(Current_Sem_Unit
)
4034 (Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4035 and then P
= Main_Unit_Entity
)
4040 -- This scenario is similar to the one above, the difference is that
4041 -- the compilation of sibling Par.Sib forces the load of parent Par
4042 -- which tries to install the limited view of Lim_Pack [1]. However
4043 -- Par.Sib has a with clause for Lim_Pack [2] in its body, and thus
4044 -- needs the non-limited views of all entities from Lim_Pack.
4046 -- limited with Lim_Pack; -- [1]
4047 -- package Par is ... package Lim_Pack is ...
4049 -- with Lim_Pack; -- [2]
4050 -- package Par.Sib is ... package body Par.Sib is ...
4052 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4053 -- Sem_Unit is the body of Par.Sib.
4055 if Ekind
(P
) = E_Package
4056 and then Ekind
(Main_Unit_Entity
) = E_Package
4057 and then Is_Child_Unit
(Main_Unit_Entity
)
4059 -- The body has a regular with clause
4061 and then Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4062 and then Has_With_Clause
(Cunit
(Current_Sem_Unit
), P
)
4064 -- One of the ancestors has a limited with clause
4066 and then Nkind
(Parent
(Parent
(Main_Unit_Entity
))) =
4067 N_Package_Specification
4068 and then Has_Limited_With_Clause
(Scope
(Main_Unit_Entity
), P
)
4073 -- A common use of the limited-with is to have a limited-with
4074 -- in the package spec, and a normal with in its package body.
4077 -- limited with X; -- [1]
4081 -- package body A is ...
4083 -- The compilation of A's body installs the context clauses found at [2]
4084 -- and then the context clauses of its specification (found at [1]). As
4085 -- a consequence, at [1] the specification of X has been analyzed and it
4086 -- is immediately visible. According to the semantics of limited-with
4087 -- context clauses we don't install the limited view because the full
4088 -- view of X supersedes its limited view.
4090 if Analyzed
(P_Unit
)
4091 and then (Is_Immediately_Visible
(P
)
4092 or else (Is_Child_Package
4093 and then Is_Visible_Child_Unit
(P
)))
4095 -- Ada 2005 (AI-262): Install the private declarations of P
4097 if Private_Present
(N
)
4098 and then not In_Private_Part
(P
)
4104 Id
:= First_Private_Entity
(P
);
4105 while Present
(Id
) loop
4106 if not Is_Internal
(Id
)
4107 and then not Is_Child_Unit
(Id
)
4109 if not In_Chain
(Id
) then
4110 Set_Homonym
(Id
, Current_Entity
(Id
));
4111 Set_Current_Entity
(Id
);
4114 Set_Is_Immediately_Visible
(Id
);
4120 Set_In_Private_Part
(P
);
4127 if Debug_Flag_I
then
4128 Write_Str
("install limited view of ");
4129 Write_Name
(Chars
(P
));
4133 -- If the unit has not been analyzed and the limited view has not been
4134 -- already installed then we install it.
4136 if not Analyzed
(P_Unit
) then
4137 if not In_Chain
(P
) then
4139 -- Minimum decoration
4141 Set_Ekind
(P
, E_Package
);
4142 Set_Etype
(P
, Standard_Void_Type
);
4143 Set_Scope
(P
, Standard_Standard
);
4145 if Is_Child_Package
then
4146 Set_Is_Child_Unit
(P
);
4147 Set_Is_Visible_Child_Unit
(P
);
4148 Set_Scope
(P
, Defining_Entity
(Unit
(Parent_Spec
(P_Unit
))));
4151 -- Place entity on visibility structure
4153 Set_Homonym
(P
, Current_Entity
(P
));
4154 Set_Current_Entity
(P
);
4156 if Debug_Flag_I
then
4157 Write_Str
(" (homonym) chain ");
4158 Write_Name
(Chars
(P
));
4162 -- Install the incomplete view. The first element of the limited
4163 -- view is a header (an E_Package entity) used to reference the
4164 -- first shadow entity in the private part of the package.
4166 Lim_Header
:= Limited_View
(P
);
4167 Lim_Typ
:= First_Entity
(Lim_Header
);
4169 while Present
(Lim_Typ
)
4170 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
4172 Set_Homonym
(Lim_Typ
, Current_Entity
(Lim_Typ
));
4173 Set_Current_Entity
(Lim_Typ
);
4175 if Debug_Flag_I
then
4176 Write_Str
(" (homonym) chain ");
4177 Write_Name
(Chars
(Lim_Typ
));
4181 Next_Entity
(Lim_Typ
);
4185 -- If the unit appears in a previous regular with_clause, the regular
4186 -- entities of the public part of the withed package must be replaced
4187 -- by the shadow ones.
4189 -- This code must be kept synchronized with the code that replaces the
4190 -- shadow entities by the real entities (see body of Remove_Limited
4191 -- With_Clause); otherwise the contents of the homonym chains are not
4195 -- Hide all the type entities of the public part of the package to
4196 -- avoid its usage. This is needed to cover all the subtype decla-
4197 -- rations because we do not remove them from the homonym chain.
4199 E
:= First_Entity
(P
);
4200 while Present
(E
) and then E
/= First_Private_Entity
(P
) loop
4202 Set_Was_Hidden
(E
, Is_Hidden
(E
));
4209 -- Replace the real entities by the shadow entities of the limited
4210 -- view. The first element of the limited view is a header that is
4211 -- used to reference the first shadow entity in the private part
4212 -- of the package. Successive elements are the limited views of the
4213 -- type (including regular incomplete types) declared in the package.
4215 Lim_Header
:= Limited_View
(P
);
4217 Lim_Typ
:= First_Entity
(Lim_Header
);
4218 while Present
(Lim_Typ
)
4219 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
4221 pragma Assert
(not In_Chain
(Lim_Typ
));
4223 -- Do not unchain nested packages and child units
4225 if Ekind
(Lim_Typ
) /= E_Package
4226 and then not Is_Child_Unit
(Lim_Typ
)
4232 Prev
:= Current_Entity
(Lim_Typ
);
4235 -- Replace E in the homonyms list, so that the limited
4236 -- view becomes available.
4238 if E
= Non_Limited_View
(Lim_Typ
) then
4239 Set_Homonym
(Lim_Typ
, Homonym
(Prev
));
4240 Set_Current_Entity
(Lim_Typ
);
4244 E
:= Homonym
(Prev
);
4246 -- E may have been removed when installing a
4247 -- previous limited_with_clause.
4251 exit when E
= Non_Limited_View
(Lim_Typ
);
4253 Prev
:= Homonym
(Prev
);
4257 Set_Homonym
(Lim_Typ
, Homonym
(Homonym
(Prev
)));
4258 Set_Homonym
(Prev
, Lim_Typ
);
4263 if Debug_Flag_I
then
4264 Write_Str
(" (homonym) chain ");
4265 Write_Name
(Chars
(Lim_Typ
));
4270 Next_Entity
(Lim_Typ
);
4274 -- The package must be visible while the limited-with clause is active
4275 -- because references to the type P.T must resolve in the usual way.
4276 -- In addition, we remember that the limited-view has been installed to
4277 -- uninstall it at the point of context removal.
4279 Set_Is_Immediately_Visible
(P
);
4280 Set_Limited_View_Installed
(N
);
4282 -- If the package in the limited_with clause is a child unit, the
4283 -- clause is unanalyzed and appears as a selected component. Recast
4284 -- it as an expanded name so that the entity can be properly set. Use
4285 -- entity of parent, if available, for higher ancestors in the name.
4287 if Nkind
(Name
(N
)) = N_Selected_Component
then
4295 while Nkind
(Nam
) = N_Selected_Component
4296 and then Present
(Ent
)
4298 Change_Selected_Component_To_Expanded_Name
(Nam
);
4300 -- Set entity of parent identifiers if the unit is a child
4301 -- unit. This ensures that the tree is properly formed from
4302 -- semantic point of view (e.g. for ASIS queries).
4304 Set_Entity
(Nam
, Ent
);
4306 Nam
:= Prefix
(Nam
);
4309 -- Set entity of last ancestor
4311 if Nkind
(Nam
) = N_Identifier
then
4312 Set_Entity
(Nam
, Ent
);
4318 Set_Entity
(Name
(N
), P
);
4319 Set_From_With_Type
(P
);
4320 end Install_Limited_Withed_Unit
;
4322 -------------------------
4323 -- Install_Withed_Unit --
4324 -------------------------
4326 procedure Install_Withed_Unit
4327 (With_Clause
: Node_Id
;
4328 Private_With_OK
: Boolean := False)
4330 Uname
: constant Entity_Id
:= Entity
(Name
(With_Clause
));
4331 P
: constant Entity_Id
:= Scope
(Uname
);
4334 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
4335 -- compiling a package declaration and the Private_With_OK flag was not
4336 -- set by the caller. These declarations will be installed later (before
4337 -- analyzing the private part of the package).
4339 if Private_Present
(With_Clause
)
4340 and then Nkind
(Unit
(Parent
(With_Clause
))) = N_Package_Declaration
4341 and then not (Private_With_OK
)
4346 if Debug_Flag_I
then
4347 if Private_Present
(With_Clause
) then
4348 Write_Str
("install private withed unit ");
4350 Write_Str
("install withed unit ");
4353 Write_Name
(Chars
(Uname
));
4357 -- We do not apply the restrictions to an internal unit unless
4358 -- we are compiling the internal unit as a main unit. This check
4359 -- is also skipped for dummy units (for missing packages).
4361 if Sloc
(Uname
) /= No_Location
4362 and then (not Is_Internal_File_Name
(Unit_File_Name
(Current_Sem_Unit
))
4363 or else Current_Sem_Unit
= Main_Unit
)
4365 Check_Restricted_Unit
4366 (Unit_Name
(Get_Source_Unit
(Uname
)), With_Clause
);
4369 if P
/= Standard_Standard
then
4371 -- If the unit is not analyzed after analysis of the with clause and
4372 -- it is an instantiation then it awaits a body and is the main unit.
4373 -- Its appearance in the context of some other unit indicates a
4374 -- circular dependency (DEC suite perversity).
4376 if not Analyzed
(Uname
)
4377 and then Nkind
(Parent
(Uname
)) = N_Package_Instantiation
4380 ("instantiation depends on itself", Name
(With_Clause
));
4382 elsif not Is_Visible_Child_Unit
(Uname
) then
4383 Set_Is_Visible_Child_Unit
(Uname
);
4385 -- If the child unit appears in the context of its parent, it is
4386 -- immediately visible.
4388 if In_Open_Scopes
(Scope
(Uname
)) then
4389 Set_Is_Immediately_Visible
(Uname
);
4392 if Is_Generic_Instance
(Uname
)
4393 and then Ekind
(Uname
) in Subprogram_Kind
4395 -- Set flag as well on the visible entity that denotes the
4396 -- instance, which renames the current one.
4398 Set_Is_Visible_Child_Unit
4400 (Defining_Entity
(Unit
(Library_Unit
(With_Clause
)))));
4403 -- The parent unit may have been installed already, and may have
4404 -- appeared in a use clause.
4406 if In_Use
(Scope
(Uname
)) then
4407 Set_Is_Potentially_Use_Visible
(Uname
);
4410 Set_Context_Installed
(With_Clause
);
4413 elsif not Is_Immediately_Visible
(Uname
) then
4414 if not Private_Present
(With_Clause
)
4415 or else Private_With_OK
4417 Set_Is_Immediately_Visible
(Uname
);
4420 Set_Context_Installed
(With_Clause
);
4423 -- A with-clause overrides a with-type clause: there are no restric-
4424 -- tions on the use of package entities.
4426 if Ekind
(Uname
) = E_Package
then
4427 Set_From_With_Type
(Uname
, False);
4430 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
4431 -- unit if there is a visible homograph for it declared in the same
4432 -- declarative region. This pathological case can only arise when an
4433 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
4434 -- G1 has a generic child also named G2, and the context includes with_
4435 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
4436 -- of I1.G2 visible as well. If the child unit is named Standard, do
4437 -- not apply the check to the Standard package itself.
4439 if Is_Child_Unit
(Uname
)
4440 and then Is_Visible_Child_Unit
(Uname
)
4441 and then Ada_Version
>= Ada_05
4444 Decl1
: constant Node_Id
:= Unit_Declaration_Node
(P
);
4450 U2
:= Homonym
(Uname
);
4452 and then U2
/= Standard_Standard
4455 Decl2
:= Unit_Declaration_Node
(P2
);
4457 if Is_Child_Unit
(U2
)
4458 and then Is_Visible_Child_Unit
(U2
)
4460 if Is_Generic_Instance
(P
)
4461 and then Nkind
(Decl1
) = N_Package_Declaration
4462 and then Generic_Parent
(Specification
(Decl1
)) = P2
4464 Error_Msg_N
("illegal with_clause", With_Clause
);
4466 ("\child unit has visible homograph" &
4467 " (RM 8.3(26), 10.1.1(19))",
4471 elsif Is_Generic_Instance
(P2
)
4472 and then Nkind
(Decl2
) = N_Package_Declaration
4473 and then Generic_Parent
(Specification
(Decl2
)) = P
4475 -- With_clause for child unit of instance appears before
4476 -- in the context. We want to place the error message on
4477 -- it, not on the generic child unit itself.
4480 Prev_Clause
: Node_Id
;
4483 Prev_Clause
:= First
(List_Containing
(With_Clause
));
4484 while Entity
(Name
(Prev_Clause
)) /= U2
loop
4488 pragma Assert
(Present
(Prev_Clause
));
4489 Error_Msg_N
("illegal with_clause", Prev_Clause
);
4491 ("\child unit has visible homograph" &
4492 " (RM 8.3(26), 10.1.1(19))",
4503 end Install_Withed_Unit
;
4509 function Is_Child_Spec
(Lib_Unit
: Node_Id
) return Boolean is
4510 K
: constant Node_Kind
:= Nkind
(Lib_Unit
);
4513 return (K
in N_Generic_Declaration
or else
4514 K
in N_Generic_Instantiation
or else
4515 K
in N_Generic_Renaming_Declaration
or else
4516 K
= N_Package_Declaration
or else
4517 K
= N_Package_Renaming_Declaration
or else
4518 K
= N_Subprogram_Declaration
or else
4519 K
= N_Subprogram_Renaming_Declaration
)
4520 and then Present
(Parent_Spec
(Lib_Unit
));
4523 -----------------------
4524 -- Load_Needed_Body --
4525 -----------------------
4527 -- N is a generic unit named in a with clause, or else it is a unit that
4528 -- contains a generic unit or an inlined function. In order to perform an
4529 -- instantiation, the body of the unit must be present. If the unit itself
4530 -- is generic, we assume that an instantiation follows, and load & analyze
4531 -- the body unconditionally. This forces analysis of the spec as well.
4533 -- If the unit is not generic, but contains a generic unit, it is loaded on
4534 -- demand, at the point of instantiation (see ch12).
4536 procedure Load_Needed_Body
(N
: Node_Id
; OK
: out Boolean) is
4537 Body_Name
: Unit_Name_Type
;
4538 Unum
: Unit_Number_Type
;
4540 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
4541 -- The loading and analysis is done with style checks off
4544 if not GNAT_Mode
then
4545 Style_Check
:= False;
4548 Body_Name
:= Get_Body_Name
(Get_Unit_Name
(Unit
(N
)));
4551 (Load_Name
=> Body_Name
,
4557 if Unum
= No_Unit
then
4561 Compiler_State
:= Analyzing
; -- reset after load
4563 if not Fatal_Error
(Unum
) or else Try_Semantics
then
4564 if Debug_Flag_L
then
4565 Write_Str
("*** Loaded generic body");
4569 Semantics
(Cunit
(Unum
));
4575 Style_Check
:= Save_Style_Check
;
4576 end Load_Needed_Body
;
4578 -------------------------
4579 -- Build_Limited_Views --
4580 -------------------------
4582 procedure Build_Limited_Views
(N
: Node_Id
) is
4583 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(Library_Unit
(N
));
4584 P
: constant Entity_Id
:= Cunit_Entity
(Unum
);
4586 Spec
: Node_Id
; -- To denote a package specification
4587 Lim_Typ
: Entity_Id
; -- To denote shadow entities
4588 Comp_Typ
: Entity_Id
; -- To denote real entities
4590 Lim_Header
: Entity_Id
; -- Package entity
4591 Last_Lim_E
: Entity_Id
:= Empty
; -- Last limited entity built
4592 Last_Pub_Lim_E
: Entity_Id
; -- To set the first private entity
4594 procedure Decorate_Incomplete_Type
4597 -- Add attributes of an incomplete type to a shadow entity. The same
4598 -- attributes are placed on the real entity, so that gigi receives
4599 -- a consistent view.
4601 procedure Decorate_Package_Specification
(P
: Entity_Id
);
4602 -- Add attributes of a package entity to the entity in a package
4605 procedure Decorate_Tagged_Type
4609 -- Set basic attributes of tagged type T, including its class_wide type.
4610 -- The parameters Loc, Scope are used to decorate the class_wide type.
4612 procedure Build_Chain
4614 First_Decl
: Node_Id
);
4615 -- Construct list of shadow entities and attach it to entity of
4616 -- package that is mentioned in a limited_with clause.
4618 function New_Internal_Shadow_Entity
4619 (Kind
: Entity_Kind
;
4620 Sloc_Value
: Source_Ptr
;
4621 Id_Char
: Character) return Entity_Id
;
4622 -- Build a new internal entity and append it to the list of shadow
4623 -- entities available through the limited-header
4625 ------------------------------
4626 -- Decorate_Incomplete_Type --
4627 ------------------------------
4629 procedure Decorate_Incomplete_Type
4634 Set_Ekind
(E
, E_Incomplete_Type
);
4635 Set_Scope
(E
, Scop
);
4637 Set_Is_First_Subtype
(E
, True);
4638 Set_Stored_Constraint
(E
, No_Elist
);
4639 Set_Full_View
(E
, Empty
);
4640 Init_Size_Align
(E
);
4641 end Decorate_Incomplete_Type
;
4643 --------------------------
4644 -- Decorate_Tagged_Type --
4645 --------------------------
4647 procedure Decorate_Tagged_Type
4655 Decorate_Incomplete_Type
(T
, Scop
);
4656 Set_Is_Tagged_Type
(T
);
4658 -- Build corresponding class_wide type, if not previously done
4660 -- Warning: The class-wide entity is shared by the limited-view
4661 -- and the full-view.
4663 if No
(Class_Wide_Type
(T
)) then
4664 CW
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('S'));
4666 Set_Ekind
(CW
, E_Class_Wide_Type
);
4668 Set_Scope
(CW
, Scop
);
4669 Set_Is_Tagged_Type
(CW
);
4670 Set_Is_First_Subtype
(CW
, True);
4671 Init_Size_Align
(CW
);
4672 Set_Has_Unknown_Discriminants
(CW
, True);
4673 Set_Class_Wide_Type
(CW
, CW
);
4674 Set_Equivalent_Type
(CW
, Empty
);
4675 Set_From_With_Type
(CW
, From_With_Type
(T
));
4677 Set_Class_Wide_Type
(T
, CW
);
4679 end Decorate_Tagged_Type
;
4681 ------------------------------------
4682 -- Decorate_Package_Specification --
4683 ------------------------------------
4685 procedure Decorate_Package_Specification
(P
: Entity_Id
) is
4687 -- Place only the most basic attributes
4689 Set_Ekind
(P
, E_Package
);
4690 Set_Etype
(P
, Standard_Void_Type
);
4691 end Decorate_Package_Specification
;
4693 --------------------------------
4694 -- New_Internal_Shadow_Entity --
4695 --------------------------------
4697 function New_Internal_Shadow_Entity
4698 (Kind
: Entity_Kind
;
4699 Sloc_Value
: Source_Ptr
;
4700 Id_Char
: Character) return Entity_Id
4702 E
: constant Entity_Id
:=
4703 Make_Defining_Identifier
(Sloc_Value
,
4704 Chars
=> New_Internal_Name
(Id_Char
));
4707 Set_Ekind
(E
, Kind
);
4708 Set_Is_Internal
(E
, True);
4710 if Kind
in Type_Kind
then
4711 Init_Size_Align
(E
);
4714 Append_Entity
(E
, Lim_Header
);
4717 end New_Internal_Shadow_Entity
;
4723 procedure Build_Chain
4725 First_Decl
: Node_Id
)
4727 Analyzed_Unit
: constant Boolean := Analyzed
(Cunit
(Unum
));
4728 Is_Tagged
: Boolean;
4733 while Present
(Decl
) loop
4735 -- For each library_package_declaration in the environment, there
4736 -- is an implicit declaration of a *limited view* of that library
4737 -- package. The limited view of a package contains:
4739 -- * For each nested package_declaration, a declaration of the
4740 -- limited view of that package, with the same defining-
4741 -- program-unit name.
4743 -- * For each type_declaration in the visible part, an incomplete
4744 -- type-declaration with the same defining_identifier, whose
4745 -- completion is the type_declaration. If the type_declaration
4746 -- is tagged, then the incomplete_type_declaration is tagged
4749 -- The partial view is tagged if the declaration has the
4750 -- explicit keyword, or else if it is a type extension, both
4751 -- of which can be ascertained syntactically.
4753 if Nkind
(Decl
) = N_Full_Type_Declaration
then
4755 (Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
4756 and then Tagged_Present
(Type_Definition
(Decl
)))
4758 (Nkind
(Type_Definition
(Decl
)) = N_Derived_Type_Definition
4761 (Record_Extension_Part
(Type_Definition
(Decl
))));
4763 Comp_Typ
:= Defining_Identifier
(Decl
);
4765 if not Analyzed_Unit
then
4767 Decorate_Tagged_Type
(Sloc
(Decl
), Comp_Typ
, Scope
);
4769 Decorate_Incomplete_Type
(Comp_Typ
, Scope
);
4773 -- Create shadow entity for type
4775 Lim_Typ
:= New_Internal_Shadow_Entity
4776 (Kind
=> Ekind
(Comp_Typ
),
4777 Sloc_Value
=> Sloc
(Comp_Typ
),
4780 Set_Chars
(Lim_Typ
, Chars
(Comp_Typ
));
4781 Set_Parent
(Lim_Typ
, Parent
(Comp_Typ
));
4782 Set_From_With_Type
(Lim_Typ
);
4785 Decorate_Tagged_Type
(Sloc
(Decl
), Lim_Typ
, Scope
);
4787 Decorate_Incomplete_Type
(Lim_Typ
, Scope
);
4790 Set_Non_Limited_View
(Lim_Typ
, Comp_Typ
);
4792 elsif Nkind
(Decl
) = N_Private_Type_Declaration
4793 or else Nkind
(Decl
) = N_Incomplete_Type_Declaration
4795 Comp_Typ
:= Defining_Identifier
(Decl
);
4797 if not Analyzed_Unit
then
4798 if Tagged_Present
(Decl
) then
4799 Decorate_Tagged_Type
(Sloc
(Decl
), Comp_Typ
, Scope
);
4801 Decorate_Incomplete_Type
(Comp_Typ
, Scope
);
4805 Lim_Typ
:= New_Internal_Shadow_Entity
4806 (Kind
=> Ekind
(Comp_Typ
),
4807 Sloc_Value
=> Sloc
(Comp_Typ
),
4810 Set_Chars
(Lim_Typ
, Chars
(Comp_Typ
));
4811 Set_Parent
(Lim_Typ
, Parent
(Comp_Typ
));
4812 Set_From_With_Type
(Lim_Typ
);
4814 if Tagged_Present
(Decl
) then
4815 Decorate_Tagged_Type
(Sloc
(Decl
), Lim_Typ
, Scope
);
4817 Decorate_Incomplete_Type
(Lim_Typ
, Scope
);
4820 Set_Non_Limited_View
(Lim_Typ
, Comp_Typ
);
4822 elsif Nkind
(Decl
) = N_Private_Extension_Declaration
then
4823 Comp_Typ
:= Defining_Identifier
(Decl
);
4825 if not Analyzed_Unit
then
4826 Decorate_Tagged_Type
(Sloc
(Decl
), Comp_Typ
, Scope
);
4829 -- Create shadow entity for type
4831 Lim_Typ
:= New_Internal_Shadow_Entity
4832 (Kind
=> Ekind
(Comp_Typ
),
4833 Sloc_Value
=> Sloc
(Comp_Typ
),
4836 Set_Chars
(Lim_Typ
, Chars
(Comp_Typ
));
4837 Set_Parent
(Lim_Typ
, Parent
(Comp_Typ
));
4838 Set_From_With_Type
(Lim_Typ
);
4840 Decorate_Tagged_Type
(Sloc
(Decl
), Lim_Typ
, Scope
);
4841 Set_Non_Limited_View
(Lim_Typ
, Comp_Typ
);
4843 elsif Nkind
(Decl
) = N_Package_Declaration
then
4848 Spec
: constant Node_Id
:= Specification
(Decl
);
4851 Comp_Typ
:= Defining_Unit_Name
(Spec
);
4853 if not Analyzed
(Cunit
(Unum
)) then
4854 Decorate_Package_Specification
(Comp_Typ
);
4855 Set_Scope
(Comp_Typ
, Scope
);
4858 Lim_Typ
:= New_Internal_Shadow_Entity
4859 (Kind
=> Ekind
(Comp_Typ
),
4860 Sloc_Value
=> Sloc
(Comp_Typ
),
4863 Decorate_Package_Specification
(Lim_Typ
);
4864 Set_Scope
(Lim_Typ
, Scope
);
4866 Set_Chars
(Lim_Typ
, Chars
(Comp_Typ
));
4867 Set_Parent
(Lim_Typ
, Parent
(Comp_Typ
));
4868 Set_From_With_Type
(Lim_Typ
);
4870 -- Note: The non_limited_view attribute is not used
4871 -- for local packages.
4875 First_Decl
=> First
(Visible_Declarations
(Spec
)));
4883 -- Start of processing for Build_Limited_Views
4886 pragma Assert
(Limited_Present
(N
));
4888 -- A library_item mentioned in a limited_with_clause shall
4889 -- be a package_declaration, not a subprogram_declaration,
4890 -- generic_declaration, generic_instantiation, or
4891 -- package_renaming_declaration
4893 case Nkind
(Unit
(Library_Unit
(N
))) is
4895 when N_Package_Declaration
=>
4898 when N_Subprogram_Declaration
=>
4899 Error_Msg_N
("subprograms not allowed in "
4900 & "limited with_clauses", N
);
4903 when N_Generic_Package_Declaration |
4904 N_Generic_Subprogram_Declaration
=>
4905 Error_Msg_N
("generics not allowed in "
4906 & "limited with_clauses", N
);
4909 when N_Generic_Instantiation
=>
4910 Error_Msg_N
("generic instantiations not allowed in "
4911 & "limited with_clauses", N
);
4914 when N_Generic_Renaming_Declaration
=>
4915 Error_Msg_N
("generic renamings not allowed in "
4916 & "limited with_clauses", N
);
4919 when N_Subprogram_Renaming_Declaration
=>
4920 Error_Msg_N
("renamed subprograms not allowed in "
4921 & "limited with_clauses", N
);
4924 when N_Package_Renaming_Declaration
=>
4925 Error_Msg_N
("renamed packages not allowed in "
4926 & "limited with_clauses", N
);
4930 raise Program_Error
;
4933 -- Check if the chain is already built
4935 Spec
:= Specification
(Unit
(Library_Unit
(N
)));
4937 if Limited_View_Installed
(Spec
) then
4941 Set_Ekind
(P
, E_Package
);
4943 -- Build the header of the limited_view
4945 Lim_Header
:= Make_Defining_Identifier
(Sloc
(N
),
4946 Chars
=> New_Internal_Name
(Id_Char
=> 'Z'));
4947 Set_Ekind
(Lim_Header
, E_Package
);
4948 Set_Is_Internal
(Lim_Header
);
4949 Set_Limited_View
(P
, Lim_Header
);
4951 -- Create the auxiliary chain. All the shadow entities are appended to
4952 -- the list of entities of the limited-view header
4956 First_Decl
=> First
(Visible_Declarations
(Spec
)));
4958 -- Save the last built shadow entity. It is needed later to set the
4959 -- reference to the first shadow entity in the private part
4961 Last_Pub_Lim_E
:= Last_Lim_E
;
4963 -- Ada 2005 (AI-262): Add the limited view of the private declarations
4964 -- Required to give support to limited-private-with clauses
4966 Build_Chain
(Scope
=> P
,
4967 First_Decl
=> First
(Private_Declarations
(Spec
)));
4969 if Last_Pub_Lim_E
/= Empty
then
4970 Set_First_Private_Entity
(Lim_Header
,
4971 Next_Entity
(Last_Pub_Lim_E
));
4973 Set_First_Private_Entity
(Lim_Header
,
4977 Set_Limited_View_Installed
(Spec
);
4978 end Build_Limited_Views
;
4980 -------------------------------
4981 -- Check_Body_Needed_For_SAL --
4982 -------------------------------
4984 procedure Check_Body_Needed_For_SAL
(Unit_Name
: Entity_Id
) is
4986 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean;
4987 -- Determine whether use of entity E might require the presence of its
4988 -- body. For a package this requires a recursive traversal of all nested
4991 ---------------------------
4992 -- Entity_Needed_For_SAL --
4993 ---------------------------
4995 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean is
4999 if Is_Subprogram
(E
)
5000 and then Has_Pragma_Inline
(E
)
5004 elsif Ekind
(E
) = E_Generic_Function
5005 or else Ekind
(E
) = E_Generic_Procedure
5009 elsif Ekind
(E
) = E_Generic_Package
5011 Nkind
(Unit_Declaration_Node
(E
)) = N_Generic_Package_Declaration
5012 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
5016 elsif Ekind
(E
) = E_Package
5018 Nkind
(Unit_Declaration_Node
(E
)) = N_Package_Declaration
5019 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
5021 Ent
:= First_Entity
(E
);
5022 while Present
(Ent
) loop
5023 if Entity_Needs_Body
(Ent
) then
5035 end Entity_Needs_Body
;
5037 -- Start of processing for Check_Body_Needed_For_SAL
5040 if Ekind
(Unit_Name
) = E_Generic_Package
5042 Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
5043 N_Generic_Package_Declaration
5045 Present
(Corresponding_Body
(Unit_Declaration_Node
(Unit_Name
)))
5047 Set_Body_Needed_For_SAL
(Unit_Name
);
5049 elsif Ekind
(Unit_Name
) = E_Generic_Procedure
5050 or else Ekind
(Unit_Name
) = E_Generic_Function
5052 Set_Body_Needed_For_SAL
(Unit_Name
);
5054 elsif Is_Subprogram
(Unit_Name
)
5055 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
5056 N_Subprogram_Declaration
5057 and then Has_Pragma_Inline
(Unit_Name
)
5059 Set_Body_Needed_For_SAL
(Unit_Name
);
5061 elsif Ekind
(Unit_Name
) = E_Subprogram_Body
then
5062 Check_Body_Needed_For_SAL
5063 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
5065 elsif Ekind
(Unit_Name
) = E_Package
5066 and then Entity_Needs_Body
(Unit_Name
)
5068 Set_Body_Needed_For_SAL
(Unit_Name
);
5070 elsif Ekind
(Unit_Name
) = E_Package_Body
5071 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) = N_Package_Body
5073 Check_Body_Needed_For_SAL
5074 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
5076 end Check_Body_Needed_For_SAL
;
5078 --------------------
5079 -- Remove_Context --
5080 --------------------
5082 procedure Remove_Context
(N
: Node_Id
) is
5083 Lib_Unit
: constant Node_Id
:= Unit
(N
);
5086 -- If this is a child unit, first remove the parent units
5088 if Is_Child_Spec
(Lib_Unit
) then
5089 Remove_Parents
(Lib_Unit
);
5092 Remove_Context_Clauses
(N
);
5095 ----------------------------
5096 -- Remove_Context_Clauses --
5097 ----------------------------
5099 procedure Remove_Context_Clauses
(N
: Node_Id
) is
5101 Unit_Name
: Entity_Id
;
5104 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5105 -- limited-views first and regular-views later (to maintain the
5108 -- First Phase: Remove limited_with context clauses
5110 Item
:= First
(Context_Items
(N
));
5111 while Present
(Item
) loop
5113 -- We are interested only in with clauses which got installed
5116 if Nkind
(Item
) = N_With_Clause
5117 and then Limited_Present
(Item
)
5118 and then Limited_View_Installed
(Item
)
5120 Remove_Limited_With_Clause
(Item
);
5126 -- Second Phase: Loop through context items and undo regular
5127 -- with_clauses and use_clauses.
5129 Item
:= First
(Context_Items
(N
));
5130 while Present
(Item
) loop
5132 -- We are interested only in with clauses which got installed on
5133 -- entry, as indicated by their Context_Installed flag set
5135 if Nkind
(Item
) = N_With_Clause
5136 and then Limited_Present
(Item
)
5137 and then Limited_View_Installed
(Item
)
5141 elsif Nkind
(Item
) = N_With_Clause
5142 and then Context_Installed
(Item
)
5144 -- Remove items from one with'ed unit
5146 Unit_Name
:= Entity
(Name
(Item
));
5147 Remove_Unit_From_Visibility
(Unit_Name
);
5148 Set_Context_Installed
(Item
, False);
5150 elsif Nkind
(Item
) = N_Use_Package_Clause
then
5151 End_Use_Package
(Item
);
5153 elsif Nkind
(Item
) = N_Use_Type_Clause
then
5154 End_Use_Type
(Item
);
5159 end Remove_Context_Clauses
;
5161 --------------------------------
5162 -- Remove_Limited_With_Clause --
5163 --------------------------------
5165 procedure Remove_Limited_With_Clause
(N
: Node_Id
) is
5166 P_Unit
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
5169 Lim_Header
: Entity_Id
;
5170 Lim_Typ
: Entity_Id
;
5174 pragma Assert
(Limited_View_Installed
(N
));
5176 -- In case of limited with_clause on subprograms, generics, instances,
5177 -- or renamings, the corresponding error was previously posted and we
5178 -- have nothing to do here.
5180 if Nkind
(P_Unit
) /= N_Package_Declaration
then
5184 P
:= Defining_Unit_Name
(Specification
(P_Unit
));
5186 -- Handle child packages
5188 if Nkind
(P
) = N_Defining_Program_Unit_Name
then
5189 P
:= Defining_Identifier
(P
);
5192 if Debug_Flag_I
then
5193 Write_Str
("remove limited view of ");
5194 Write_Name
(Chars
(P
));
5195 Write_Str
(" from visibility");
5199 -- Prepare the removal of the shadow entities from visibility. The
5200 -- first element of the limited view is a header (an E_Package
5201 -- entity) that is used to reference the first shadow entity in the
5202 -- private part of the package
5204 Lim_Header
:= Limited_View
(P
);
5205 Lim_Typ
:= First_Entity
(Lim_Header
);
5207 -- Remove package and shadow entities from visibility if it has not
5210 if not Analyzed
(P_Unit
) then
5212 Set_Is_Immediately_Visible
(P
, False);
5214 while Present
(Lim_Typ
) loop
5216 Next_Entity
(Lim_Typ
);
5219 -- Otherwise this package has already appeared in the closure and its
5220 -- shadow entities must be replaced by its real entities. This code
5221 -- must be kept synchronized with the complementary code in Install
5222 -- Limited_Withed_Unit.
5225 -- Real entities that are type or subtype declarations were hidden
5226 -- from visibility at the point of installation of the limited-view.
5227 -- Now we recover the previous value of the hidden attribute.
5229 E
:= First_Entity
(P
);
5230 while Present
(E
) and then E
/= First_Private_Entity
(P
) loop
5232 Set_Is_Hidden
(E
, Was_Hidden
(E
));
5238 while Present
(Lim_Typ
)
5239 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
5241 -- Nested packages and child units were not unchained
5243 if Ekind
(Lim_Typ
) /= E_Package
5244 and then not Is_Child_Unit
(Non_Limited_View
(Lim_Typ
))
5246 -- Handle incomplete types of the real view. For this purpose
5247 -- we traverse the list of visible entities to look for an
5248 -- incomplete type in the real-view associated with Lim_Typ.
5250 E
:= First_Entity
(P
);
5251 while Present
(E
) and then E
/= First_Private_Entity
(P
) loop
5252 exit when Ekind
(E
) = E_Incomplete_Type
5253 and then Present
(Full_View
(E
))
5254 and then Full_View
(E
) = Lim_Typ
;
5259 -- If the previous search was not sucessful then the entity
5260 -- to be restored in the homonym list is the non-limited view
5262 if E
= First_Private_Entity
(P
) then
5263 E
:= Non_Limited_View
(Lim_Typ
);
5266 pragma Assert
(not In_Chain
(E
));
5268 Prev
:= Current_Entity
(Lim_Typ
);
5270 if Prev
= Lim_Typ
then
5271 Set_Current_Entity
(E
);
5274 while Present
(Prev
)
5275 and then Homonym
(Prev
) /= Lim_Typ
5277 Prev
:= Homonym
(Prev
);
5280 if Present
(Prev
) then
5281 Set_Homonym
(Prev
, E
);
5285 -- We must also set the next homonym entity of the real entity
5286 -- to handle the case in which the next homonym was a shadow
5289 Set_Homonym
(E
, Homonym
(Lim_Typ
));
5292 Next_Entity
(Lim_Typ
);
5296 -- Indicate that the limited view of the package is not installed
5298 Set_From_With_Type
(P
, False);
5299 Set_Limited_View_Installed
(N
, False);
5300 end Remove_Limited_With_Clause
;
5302 --------------------
5303 -- Remove_Parents --
5304 --------------------
5306 procedure Remove_Parents
(Lib_Unit
: Node_Id
) is
5309 P_Spec
: Node_Id
:= Empty
;
5311 Vis
: constant Boolean :=
5312 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
;
5315 if Is_Child_Spec
(Lib_Unit
) then
5316 P_Spec
:= Parent_Spec
(Lib_Unit
);
5318 elsif Nkind
(Lib_Unit
) = N_Package_Body
5319 and then Nkind
(Original_Node
(Lib_Unit
)) = N_Package_Instantiation
5321 P_Spec
:= Parent_Spec
(Original_Node
(Lib_Unit
));
5324 if Present
(P_Spec
) then
5327 P_Name
:= Get_Parent_Entity
(P
);
5328 Remove_Context_Clauses
(P_Spec
);
5329 End_Package_Scope
(P_Name
);
5330 Set_Is_Immediately_Visible
(P_Name
, Vis
);
5332 -- Remove from visibility the siblings as well, which are directly
5333 -- visible while the parent is in scope.
5335 E
:= First_Entity
(P_Name
);
5336 while Present
(E
) loop
5337 if Is_Child_Unit
(E
) then
5338 Set_Is_Immediately_Visible
(E
, False);
5344 Set_In_Package_Body
(P_Name
, False);
5346 -- This is the recursive call to remove the context of any
5347 -- higher level parent. This recursion ensures that all parents
5348 -- are removed in the reverse order of their installation.
5354 ---------------------------------
5355 -- Remove_Private_With_Clauses --
5356 ---------------------------------
5358 procedure Remove_Private_With_Clauses
(Comp_Unit
: Node_Id
) is
5361 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean;
5362 -- Check whether a given unit appears in a regular with_clause.
5363 -- Used to determine whether a private_with_clause, implicit or
5364 -- explicit, should be ignored.
5366 ----------------------------
5367 -- In_Regular_With_Clause --
5368 ----------------------------
5370 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean
5375 Item
:= First
(Context_Items
(Comp_Unit
));
5376 while Present
(Item
) loop
5377 if Nkind
(Item
) = N_With_Clause
5378 and then Entity
(Name
(Item
)) = E
5379 and then not Private_Present
(Item
)
5387 end In_Regular_With_Clause
;
5389 -- Start of processing for Remove_Private_With_Clauses
5392 Item
:= First
(Context_Items
(Comp_Unit
));
5393 while Present
(Item
) loop
5394 if Nkind
(Item
) = N_With_Clause
5395 and then Private_Present
(Item
)
5398 -- If private_with_clause is redundant, remove it from
5399 -- context, as a small optimization to subsequent handling
5400 -- of private_with clauses in other nested packages..
5402 if In_Regular_With_Clause
(Entity
(Name
(Item
))) then
5404 Nxt
: constant Node_Id
:= Next
(Item
);
5411 elsif Limited_Present
(Item
) then
5412 if not Limited_View_Installed
(Item
) then
5413 Remove_Limited_With_Clause
(Item
);
5419 Remove_Unit_From_Visibility
(Entity
(Name
(Item
)));
5420 Set_Context_Installed
(Item
, False);
5428 end Remove_Private_With_Clauses
;
5430 ---------------------------------
5431 -- Remove_Unit_From_Visibility --
5432 ---------------------------------
5434 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
) is
5435 P
: constant Entity_Id
:= Scope
(Unit_Name
);
5439 if Debug_Flag_I
then
5440 Write_Str
("remove unit ");
5441 Write_Name
(Chars
(Unit_Name
));
5442 Write_Str
(" from visibility");
5446 if P
/= Standard_Standard
then
5447 Set_Is_Visible_Child_Unit
(Unit_Name
, False);
5450 Set_Is_Potentially_Use_Visible
(Unit_Name
, False);
5451 Set_Is_Immediately_Visible
(Unit_Name
, False);
5452 end Remove_Unit_From_Visibility
;
5467 procedure Unchain
(E
: Entity_Id
) is
5471 Prev
:= Current_Entity
(E
);
5477 Set_Name_Entity_Id
(Chars
(E
), Homonym
(E
));
5480 while Present
(Prev
)
5481 and then Homonym
(Prev
) /= E
5483 Prev
:= Homonym
(Prev
);
5486 if Present
(Prev
) then
5487 Set_Homonym
(Prev
, Homonym
(E
));
5491 if Debug_Flag_I
then
5492 Write_Str
(" (homonym) unchain ");
5493 Write_Name
(Chars
(E
));