1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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 Elists
; use Elists
;
32 with Fname
; use Fname
;
33 with Fname
.UF
; use Fname
.UF
;
34 with Freeze
; use Freeze
;
35 with Impunit
; use Impunit
;
36 with Inline
; use Inline
;
38 with Lib
.Load
; use Lib
.Load
;
39 with Lib
.Xref
; use Lib
.Xref
;
40 with Namet
; use Namet
;
41 with Nlists
; use Nlists
;
42 with Nmake
; use Nmake
;
44 with Output
; use Output
;
45 with Par_SCO
; use Par_SCO
;
46 with Restrict
; use Restrict
;
47 with Rident
; use Rident
;
48 with Rtsfind
; use Rtsfind
;
50 with Sem_Aux
; use Sem_Aux
;
51 with Sem_Ch3
; use Sem_Ch3
;
52 with Sem_Ch6
; use Sem_Ch6
;
53 with Sem_Ch7
; use Sem_Ch7
;
54 with Sem_Ch8
; use Sem_Ch8
;
55 with Sem_Dist
; use Sem_Dist
;
56 with Sem_Prag
; use Sem_Prag
;
57 with Sem_Util
; use Sem_Util
;
58 with Sem_Warn
; use Sem_Warn
;
59 with Stand
; use Stand
;
60 with Sinfo
; use Sinfo
;
61 with Sinfo
.CN
; use Sinfo
.CN
;
62 with Sinput
; use Sinput
;
63 with Snames
; use Snames
;
64 with Style
; use Style
;
65 with Stylesw
; use Stylesw
;
66 with Tbuild
; use Tbuild
;
67 with Uname
; use Uname
;
69 package body Sem_Ch10
is
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
75 procedure Analyze_Context
(N
: Node_Id
);
76 -- Analyzes items in the context clause of compilation unit
78 procedure Build_Limited_Views
(N
: Node_Id
);
79 -- Build and decorate the list of shadow entities for a package mentioned
80 -- in a limited_with clause. If the package was not previously analyzed
81 -- then it also performs a basic decoration of the real entities. This is
82 -- required in order to avoid passing non-decorated entities to the
83 -- back-end. Implements Ada 2005 (AI-50217).
85 procedure Check_Body_Needed_For_SAL
(Unit_Name
: Entity_Id
);
86 -- Check whether the source for the body of a compilation unit must be
87 -- included in a standalone library.
89 procedure Check_Private_Child_Unit
(N
: Node_Id
);
90 -- If a with_clause mentions a private child unit, the compilation unit
91 -- must be a member of the same family, as described in 10.1.2.
93 procedure Check_Stub_Level
(N
: Node_Id
);
94 -- Verify that a stub is declared immediately within a compilation unit,
95 -- and not in an inner frame.
97 procedure Expand_With_Clause
(Item
: Node_Id
; Nam
: Node_Id
; N
: Node_Id
);
98 -- When a child unit appears in a context clause, the implicit withs on
99 -- parents are made explicit, and with clauses are inserted in the context
100 -- clause before the one for the child. If a parent in the with_clause
101 -- is a renaming, the implicit with_clause is on the renaming whose name
102 -- is mentioned in the with_clause, and not on the package it renames.
103 -- N is the compilation unit whose list of context items receives the
104 -- implicit with_clauses.
106 function Get_Parent_Entity
(Unit
: Node_Id
) return Entity_Id
;
107 -- Get defining entity of parent unit of a child unit. In most cases this
108 -- is the defining entity of the unit, but for a child instance whose
109 -- parent needs a body for inlining, the instantiation node of the parent
110 -- has not yet been rewritten as a package declaration, and the entity has
111 -- to be retrieved from the Instance_Spec of the unit.
113 function Has_With_Clause
116 Is_Limited
: Boolean := False) return Boolean;
117 -- Determine whether compilation unit C_Unit contains a [limited] with
118 -- clause for package Pack. Use the flag Is_Limited to designate desired
121 procedure Implicit_With_On_Parent
(Child_Unit
: Node_Id
; N
: Node_Id
);
122 -- If the main unit is a child unit, implicit withs are also added for
123 -- all its ancestors.
125 function In_Chain
(E
: Entity_Id
) return Boolean;
126 -- Check that the shadow entity is not already in the homonym chain, for
127 -- example through a limited_with clause in a parent unit.
129 procedure Install_Context_Clauses
(N
: Node_Id
);
130 -- Subsidiary to Install_Context and Install_Parents. Process all with
131 -- and use clauses for current unit and its library unit if any.
133 procedure Install_Limited_Context_Clauses
(N
: Node_Id
);
134 -- Subsidiary to Install_Context. Process only limited with_clauses for
135 -- current unit. Implements Ada 2005 (AI-50217).
137 procedure Install_Limited_Withed_Unit
(N
: Node_Id
);
138 -- Place shadow entities for a limited_with package in the visibility
139 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
141 procedure Install_Withed_Unit
142 (With_Clause
: Node_Id
;
143 Private_With_OK
: Boolean := False);
144 -- If the unit is not a child unit, make unit immediately visible. The
145 -- caller ensures that the unit is not already currently installed. The
146 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
147 -- is called when compiling the private part of a package, or installing
148 -- the private declarations of a parent unit.
150 procedure Install_Parents
(Lib_Unit
: Node_Id
; Is_Private
: Boolean);
151 -- This procedure establishes the context for the compilation of a child
152 -- unit. If Lib_Unit is a child library spec then the context of the parent
153 -- is installed, and the parent itself made immediately visible, so that
154 -- the child unit is processed in the declarative region of the parent.
155 -- Install_Parents makes a recursive call to itself to ensure that all
156 -- parents are loaded in the nested case. If Lib_Unit is a library body,
157 -- the only effect of Install_Parents is to install the private decls of
158 -- the parents, because the visible parent declarations will have been
159 -- installed as part of the context of the corresponding spec.
161 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
);
162 -- In the compilation of a child unit, a child of any of the ancestor
163 -- units is directly visible if it is visible, because the parent is in
164 -- an enclosing scope. Iterate over context to find child units of U_Name
165 -- or of some ancestor of it.
167 function Is_Ancestor_Unit
(U1
: Node_Id
; U2
: Node_Id
) return Boolean;
168 -- When compiling a unit Q descended from some parent unit P, a limited
169 -- with_clause in the context of P that names some other ancestor of Q
170 -- must not be installed because the ancestor is immediately visible.
172 function Is_Child_Spec
(Lib_Unit
: Node_Id
) return Boolean;
173 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
174 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
175 -- a library spec that has a parent. If the call to Is_Child_Spec returns
176 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
177 -- compilation unit for the parent spec.
179 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
180 -- Parent_Spec is non-empty, this is also a child unit.
182 procedure Remove_Context_Clauses
(N
: Node_Id
);
183 -- Subsidiary of previous one. Remove use_ and with_clauses
185 procedure Remove_Limited_With_Clause
(N
: Node_Id
);
186 -- Remove from visibility the shadow entities introduced for a package
187 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
189 procedure Remove_Parents
(Lib_Unit
: Node_Id
);
190 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
191 -- contexts established by the corresponding call to Install_Parents are
192 -- removed. Remove_Parents contains a recursive call to itself to ensure
193 -- that all parents are removed in the nested case.
195 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
);
196 -- Reset all visibility flags on unit after compiling it, either as a main
197 -- unit or as a unit in the context.
199 procedure Unchain
(E
: Entity_Id
);
200 -- Remove single entity from visibility list
202 procedure Analyze_Proper_Body
(N
: Node_Id
; Nam
: Entity_Id
);
203 -- Common processing for all stubs (subprograms, tasks, packages, and
204 -- protected cases). N is the stub to be analyzed. Once the subunit name
205 -- is established, load and analyze. Nam is the non-overloadable entity
206 -- for which the proper body provides a completion. Subprogram stubs are
207 -- handled differently because they can be declarations.
210 -- A dummy procedure, for debugging use, called just before analyzing the
211 -- main unit (after dealing with any context clauses).
213 --------------------------
214 -- Limited_With_Clauses --
215 --------------------------
217 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
218 -- mutually recursive types declared in different units. A limited_with
219 -- clause that names package P in the context of unit U makes the types
220 -- declared in the visible part of P available within U, but with the
221 -- restriction that these types can only be used as incomplete types.
222 -- The limited_with clause does not impose a semantic dependence on P,
223 -- and it is possible for two packages to have limited_with_clauses on
224 -- each other without creating an elaboration circularity.
226 -- To support this feature, the analysis of a limited_with clause must
227 -- create an abbreviated view of the package, without performing any
228 -- semantic analysis on it. This "package abstract" contains shadow types
229 -- that are in one-one correspondence with the real types in the package,
230 -- and that have the properties of incomplete types.
232 -- The implementation creates two element lists: one to chain the shadow
233 -- entities, and one to chain the corresponding type entities in the tree
234 -- of the package. Links between corresponding entities in both chains
235 -- allow the compiler to select the proper view of a given type, depending
236 -- on the context. Note that in contrast with the handling of private
237 -- types, the limited view and the non-limited view of a type are treated
238 -- as separate entities, and no entity exchange needs to take place, which
239 -- makes the implementation must simpler than could be feared.
241 ------------------------------
242 -- Analyze_Compilation_Unit --
243 ------------------------------
245 procedure Analyze_Compilation_Unit
(N
: Node_Id
) is
246 Unit_Node
: constant Node_Id
:= Unit
(N
);
247 Lib_Unit
: Node_Id
:= Library_Unit
(N
);
249 Main_Cunit
: constant Node_Id
:= Cunit
(Main_Unit
);
250 Par_Spec_Name
: Unit_Name_Type
;
251 Unum
: Unit_Number_Type
;
253 procedure Check_Redundant_Withs
254 (Context_Items
: List_Id
;
255 Spec_Context_Items
: List_Id
:= No_List
);
256 -- Determine whether the context list of a compilation unit contains
257 -- redundant with clauses. When checking body clauses against spec
258 -- clauses, set Context_Items to the context list of the body and
259 -- Spec_Context_Items to that of the spec. Parent packages are not
260 -- examined for documentation purposes.
262 procedure Generate_Parent_References
(N
: Node_Id
; P_Id
: Entity_Id
);
263 -- Generate cross-reference information for the parents of child units.
264 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
266 ---------------------------
267 -- Check_Redundant_Withs --
268 ---------------------------
270 procedure Check_Redundant_Withs
271 (Context_Items
: List_Id
;
272 Spec_Context_Items
: List_Id
:= No_List
)
276 procedure Process_Body_Clauses
277 (Context_List
: List_Id
;
279 Used
: in out Boolean;
280 Used_Type_Or_Elab
: in out Boolean);
281 -- Examine the context clauses of a package body, trying to match the
282 -- name entity of Clause with any list element. If the match occurs
283 -- on a use package clause set Used to True, for a use type clause or
284 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
286 procedure Process_Spec_Clauses
287 (Context_List
: List_Id
;
289 Used
: in out Boolean;
290 Withed
: in out Boolean;
291 Exit_On_Self
: Boolean := False);
292 -- Examine the context clauses of a package spec, trying to match
293 -- the name entity of Clause with any list element. If the match
294 -- occurs on a use package clause, set Used to True, for a with
295 -- package clause other than Clause, set Withed to True. Limited
296 -- with clauses, implicitly generated with clauses and withs
297 -- having pragmas Elaborate or Elaborate_All applied to them are
298 -- skipped. Exit_On_Self is used to control the search loop and
299 -- force an exit whenever Clause sees itself in the search.
301 --------------------------
302 -- Process_Body_Clauses --
303 --------------------------
305 procedure Process_Body_Clauses
306 (Context_List
: List_Id
;
308 Used
: in out Boolean;
309 Used_Type_Or_Elab
: in out Boolean)
311 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
317 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean;
318 -- In an expanded name in a use clause, if the prefix is a renamed
319 -- package, the entity is set to the original package as a result,
320 -- when checking whether the package appears in a previous with
321 -- clause, the renaming has to be taken into account, to prevent
322 -- spurious/incorrect warnings. A common case is use of Text_IO.
328 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean is
330 return Entity
(N
) = P
332 (Present
(Renamed_Object
(P
))
333 and then Entity
(N
) = Renamed_Object
(P
));
336 -- Start of processing for Process_Body_Clauses
340 Used_Type_Or_Elab
:= False;
342 Cont_Item
:= First
(Context_List
);
343 while Present
(Cont_Item
) loop
345 -- Package use clause
347 if Nkind
(Cont_Item
) = N_Use_Package_Clause
350 -- Search through use clauses
352 Use_Item
:= First
(Names
(Cont_Item
));
353 while Present
(Use_Item
) and then not Used
loop
355 -- Case of a direct use of the one we are looking for
357 if Entity
(Use_Item
) = Nam_Ent
then
360 -- Handle nested case, as in "with P; use P.Q.R"
367 -- Loop through prefixes looking for match
370 while Nkind
(UE
) = N_Expanded_Name
loop
371 if Same_Unit
(Prefix
(UE
), Nam_Ent
) then
386 elsif Nkind
(Cont_Item
) = N_Use_Type_Clause
387 and then not Used_Type_Or_Elab
389 Subt_Mark
:= First
(Subtype_Marks
(Cont_Item
));
390 while Present
(Subt_Mark
)
391 and then not Used_Type_Or_Elab
393 if Same_Unit
(Prefix
(Subt_Mark
), Nam_Ent
) then
394 Used_Type_Or_Elab
:= True;
400 -- Pragma Elaborate or Elaborate_All
402 elsif Nkind
(Cont_Item
) = N_Pragma
404 Nam_In
(Pragma_Name
(Cont_Item
), Name_Elaborate
,
406 and then not Used_Type_Or_Elab
409 First
(Pragma_Argument_Associations
(Cont_Item
));
410 while Present
(Prag_Unit
)
411 and then not Used_Type_Or_Elab
413 if Entity
(Expression
(Prag_Unit
)) = Nam_Ent
then
414 Used_Type_Or_Elab
:= True;
423 end Process_Body_Clauses
;
425 --------------------------
426 -- Process_Spec_Clauses --
427 --------------------------
429 procedure Process_Spec_Clauses
430 (Context_List
: List_Id
;
432 Used
: in out Boolean;
433 Withed
: in out Boolean;
434 Exit_On_Self
: Boolean := False)
436 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
444 Cont_Item
:= First
(Context_List
);
445 while Present
(Cont_Item
) loop
447 -- Stop the search since the context items after Cont_Item have
448 -- already been examined in a previous iteration of the reverse
449 -- loop in Check_Redundant_Withs.
452 and Cont_Item
= Clause
457 -- Package use clause
459 if Nkind
(Cont_Item
) = N_Use_Package_Clause
462 Use_Item
:= First
(Names
(Cont_Item
));
463 while Present
(Use_Item
) and then not Used
loop
464 if Entity
(Use_Item
) = Nam_Ent
then
471 -- Package with clause. Avoid processing self, implicitly
472 -- generated with clauses or limited with clauses. Note that
473 -- we examine with clauses having pragmas Elaborate or
474 -- Elaborate_All applied to them due to cases such as:
478 -- pragma Elaborate (Pack);
480 -- In this case, the second with clause is redundant since
481 -- the pragma applies only to the first "with Pack;".
483 -- Note that we only consider with_clauses that comes from
484 -- source. In the case of renamings used as prefixes of names
485 -- in with_clauses, we generate a with_clause for the prefix,
486 -- which we do not treat as implicit because it is needed for
487 -- visibility analysis, but is also not redundant.
489 elsif Nkind
(Cont_Item
) = N_With_Clause
490 and then not Implicit_With
(Cont_Item
)
491 and then Comes_From_Source
(Cont_Item
)
492 and then not Limited_Present
(Cont_Item
)
493 and then Cont_Item
/= Clause
494 and then Entity
(Name
(Cont_Item
)) = Nam_Ent
501 end Process_Spec_Clauses
;
503 -- Start of processing for Check_Redundant_Withs
506 Clause
:= Last
(Context_Items
);
507 while Present
(Clause
) loop
509 -- Avoid checking implicitly generated with clauses, limited with
510 -- clauses or withs that have pragma Elaborate or Elaborate_All.
512 if Nkind
(Clause
) = N_With_Clause
513 and then not Implicit_With
(Clause
)
514 and then not Limited_Present
(Clause
)
515 and then not Elaborate_Present
(Clause
)
517 -- Package body-to-spec check
519 if Present
(Spec_Context_Items
) then
521 Used_In_Body
: Boolean := False;
522 Used_In_Spec
: Boolean := False;
523 Used_Type_Or_Elab
: Boolean := False;
524 Withed_In_Spec
: Boolean := False;
528 (Context_List
=> Spec_Context_Items
,
530 Used
=> Used_In_Spec
,
531 Withed
=> Withed_In_Spec
);
534 (Context_List
=> Context_Items
,
536 Used
=> Used_In_Body
,
537 Used_Type_Or_Elab
=> Used_Type_Or_Elab
);
539 -- "Type Elab" refers to the presence of either a use
540 -- type clause, pragmas Elaborate or Elaborate_All.
542 -- +---------------+---------------------------+------+
543 -- | Spec | Body | Warn |
544 -- +--------+------+--------+------+-----------+------+
545 -- | Withed | Used | Withed | Used | Type Elab | |
546 -- | X | | X | | | X |
547 -- | X | | X | X | | |
548 -- | X | | X | | X | |
549 -- | X | | X | X | X | |
550 -- | X | X | X | | | X |
551 -- | X | X | X | | X | |
552 -- | X | X | X | X | | X |
553 -- | X | X | X | X | X | |
554 -- +--------+------+--------+------+-----------+------+
557 and then not Used_Type_Or_Elab
)
560 and then not Used_In_Body
)
564 Error_Msg_N
-- CODEFIX
565 ("redundant with clause in body??", Clause
);
568 Used_In_Body
:= False;
569 Used_In_Spec
:= False;
570 Used_Type_Or_Elab
:= False;
571 Withed_In_Spec
:= False;
574 -- Standalone package spec or body check
578 Dont_Care
: Boolean := False;
579 Withed
: Boolean := False;
582 -- The mechanism for examining the context clauses of a
583 -- package spec can be applied to package body clauses.
586 (Context_List
=> Context_Items
,
590 Exit_On_Self
=> True);
593 Error_Msg_N
-- CODEFIX
594 ("redundant with clause??", Clause
);
602 end Check_Redundant_Withs
;
604 --------------------------------
605 -- Generate_Parent_References --
606 --------------------------------
608 procedure Generate_Parent_References
(N
: Node_Id
; P_Id
: Entity_Id
) is
610 P_Name
: Entity_Id
:= P_Id
;
613 Pref
:= Name
(Parent
(Defining_Entity
(N
)));
615 if Nkind
(Pref
) = N_Expanded_Name
then
617 -- Done already, if the unit has been compiled indirectly as
618 -- part of the closure of its context because of inlining.
623 while Nkind
(Pref
) = N_Selected_Component
loop
624 Change_Selected_Component_To_Expanded_Name
(Pref
);
625 Set_Entity
(Pref
, P_Name
);
626 Set_Etype
(Pref
, Etype
(P_Name
));
627 Generate_Reference
(P_Name
, Pref
, 'r');
628 Pref
:= Prefix
(Pref
);
629 P_Name
:= Scope
(P_Name
);
632 -- The guard here on P_Name is to handle the error condition where
633 -- the parent unit is missing because the file was not found.
635 if Present
(P_Name
) then
636 Set_Entity
(Pref
, P_Name
);
637 Set_Etype
(Pref
, Etype
(P_Name
));
638 Generate_Reference
(P_Name
, Pref
, 'r');
639 Style
.Check_Identifier
(Pref
, P_Name
);
641 end Generate_Parent_References
;
643 -- Start of processing for Analyze_Compilation_Unit
646 Process_Compilation_Unit_Pragmas
(N
);
648 -- If the unit is a subunit whose parent has not been analyzed (which
649 -- indicates that the main unit is a subunit, either the current one or
650 -- one of its descendents) then the subunit is compiled as part of the
651 -- analysis of the parent, which we proceed to do. Basically this gets
652 -- handled from the top down and we don't want to do anything at this
653 -- level (i.e. this subunit will be handled on the way down from the
654 -- parent), so at this level we immediately return. If the subunit ends
655 -- up not analyzed, it means that the parent did not contain a stub for
656 -- it, or that there errors were detected in some ancestor.
658 if Nkind
(Unit_Node
) = N_Subunit
and then not Analyzed
(Lib_Unit
) then
659 Semantics
(Lib_Unit
);
661 if not Analyzed
(Proper_Body
(Unit_Node
)) then
662 if Serious_Errors_Detected
> 0 then
663 Error_Msg_N
("subunit not analyzed (errors in parent unit)", N
);
665 Error_Msg_N
("missing stub for subunit", N
);
672 -- Analyze context (this will call Sem recursively for with'ed units) To
673 -- detect circularities among with-clauses that are not caught during
674 -- loading, we set the Context_Pending flag on the current unit. If the
675 -- flag is already set there is a potential circularity. We exclude
676 -- predefined units from this check because they are known to be safe.
677 -- We also exclude package bodies that are present because circularities
678 -- between bodies are harmless (and necessary).
680 if Context_Pending
(N
) then
682 Circularity
: Boolean := True;
685 if Is_Predefined_File_Name
686 (Unit_File_Name
(Get_Source_Unit
(Unit
(N
))))
688 Circularity
:= False;
691 for U
in Main_Unit
+ 1 .. Last_Unit
loop
692 if Nkind
(Unit
(Cunit
(U
))) = N_Package_Body
693 and then not Analyzed
(Cunit
(U
))
695 Circularity
:= False;
702 Error_Msg_N
("circular dependency caused by with_clauses", N
);
704 ("\possibly missing limited_with clause"
705 & " in one of the following", N
);
707 for U
in Main_Unit
.. Last_Unit
loop
708 if Context_Pending
(Cunit
(U
)) then
709 Error_Msg_Unit_1
:= Get_Unit_Name
(Unit
(Cunit
(U
)));
710 Error_Msg_N
("\unit$", N
);
714 raise Unrecoverable_Error
;
718 Set_Context_Pending
(N
);
723 Set_Context_Pending
(N
, False);
725 -- If the unit is a package body, the spec is already loaded and must be
726 -- analyzed first, before we analyze the body.
728 if Nkind
(Unit_Node
) = N_Package_Body
then
730 -- If no Lib_Unit, then there was a serious previous error, so just
731 -- ignore the entire analysis effort
733 if No
(Lib_Unit
) then
734 Check_Error_Detected
;
738 -- Analyze the package spec
740 Semantics
(Lib_Unit
);
742 -- Check for unused with's
744 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
746 -- Verify that the library unit is a package declaration
748 if not Nkind_In
(Unit
(Lib_Unit
), N_Package_Declaration
,
749 N_Generic_Package_Declaration
)
752 ("no legal package declaration for package body", N
);
755 -- Otherwise, the entity in the declaration is visible. Update the
756 -- version to reflect dependence of this body on the spec.
759 Spec_Id
:= Defining_Entity
(Unit
(Lib_Unit
));
760 Set_Is_Immediately_Visible
(Spec_Id
, True);
761 Version_Update
(N
, Lib_Unit
);
763 if Nkind
(Defining_Unit_Name
(Unit_Node
)) =
764 N_Defining_Program_Unit_Name
766 Generate_Parent_References
(Unit_Node
, Scope
(Spec_Id
));
771 -- If the unit is a subprogram body, then we similarly need to analyze
772 -- its spec. However, things are a little simpler in this case, because
773 -- here, this analysis is done mostly for error checking and consistency
774 -- purposes (but not only, e.g. there could be a contract on the spec),
775 -- so there's nothing else to be done.
777 elsif Nkind
(Unit_Node
) = N_Subprogram_Body
then
778 if Acts_As_Spec
(N
) then
780 -- If the subprogram body is a child unit, we must create a
781 -- declaration for it, in order to properly load the parent(s).
782 -- After this, the original unit does not acts as a spec, because
783 -- there is an explicit one. If this unit appears in a context
784 -- clause, then an implicit with on the parent will be added when
785 -- installing the context. If this is the main unit, there is no
786 -- Unit_Table entry for the declaration (it has the unit number
787 -- of the main unit) and code generation is unaffected.
789 Unum
:= Get_Cunit_Unit_Number
(N
);
790 Par_Spec_Name
:= Get_Parent_Spec_Name
(Unit_Name
(Unum
));
792 if Par_Spec_Name
/= No_Unit_Name
then
795 (Load_Name
=> Par_Spec_Name
,
800 if Unum
/= No_Unit
then
802 -- Build subprogram declaration and attach parent unit to it
803 -- This subprogram declaration does not come from source,
804 -- Nevertheless the backend must generate debugging info for
805 -- it, and this must be indicated explicitly. We also mark
806 -- the body entity as a child unit now, to prevent a
807 -- cascaded error if the spec entity cannot be entered
808 -- in its scope. Finally we create a Units table entry for
809 -- the subprogram declaration, to maintain a one-to-one
810 -- correspondence with compilation unit nodes. This is
811 -- critical for the tree traversals performed by CodePeer.
814 Loc
: constant Source_Ptr
:= Sloc
(N
);
815 SCS
: constant Boolean :=
816 Get_Comes_From_Source_Default
;
819 Set_Comes_From_Source_Default
(False);
821 -- Checks for redundant USE TYPE clauses have a special
822 -- exception for the synthetic spec we create here. This
823 -- special case relies on the two compilation units
824 -- sharing the same context clause.
826 -- Note: We used to do a shallow copy (New_Copy_List),
827 -- which defeated those checks and also created malformed
828 -- trees (subtype mark shared by two distinct
829 -- N_Use_Type_Clause nodes) which crashed the compiler.
832 Make_Compilation_Unit
(Loc
,
833 Context_Items
=> Context_Items
(N
),
835 Make_Subprogram_Declaration
(Sloc
(N
),
838 (Specification
(Unit_Node
))),
840 Make_Compilation_Unit_Aux
(Loc
));
842 Set_Library_Unit
(N
, Lib_Unit
);
843 Set_Parent_Spec
(Unit
(Lib_Unit
), Cunit
(Unum
));
844 Make_Child_Decl_Unit
(N
);
845 Semantics
(Lib_Unit
);
847 -- Now that a separate declaration exists, the body
848 -- of the child unit does not act as spec any longer.
850 Set_Acts_As_Spec
(N
, False);
851 Set_Is_Child_Unit
(Defining_Entity
(Unit_Node
));
852 Set_Debug_Info_Needed
(Defining_Entity
(Unit
(Lib_Unit
)));
853 Set_Comes_From_Source_Default
(SCS
);
858 -- Here for subprogram with separate declaration
861 Semantics
(Lib_Unit
);
862 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
863 Version_Update
(N
, Lib_Unit
);
866 -- If this is a child unit, generate references to the parents
868 if Nkind
(Defining_Unit_Name
(Specification
(Unit_Node
))) =
869 N_Defining_Program_Unit_Name
871 Generate_Parent_References
(
872 Specification
(Unit_Node
),
873 Scope
(Defining_Entity
(Unit
(Lib_Unit
))));
877 -- If it is a child unit, the parent must be elaborated first and we
878 -- update version, since we are dependent on our parent.
880 if Is_Child_Spec
(Unit_Node
) then
882 -- The analysis of the parent is done with style checks off
885 Save_Style_Check
: constant Boolean := Style_Check
;
888 if not GNAT_Mode
then
889 Style_Check
:= False;
892 Semantics
(Parent_Spec
(Unit_Node
));
893 Version_Update
(N
, Parent_Spec
(Unit_Node
));
895 -- Restore style check settings
897 Style_Check
:= Save_Style_Check
;
901 -- With the analysis done, install the context. Note that we can't
902 -- install the context from the with clauses as we analyze them, because
903 -- each with clause must be analyzed in a clean visibility context, so
904 -- we have to wait and install them all at once.
908 if Is_Child_Spec
(Unit_Node
) then
910 -- Set the entities of all parents in the program_unit_name
912 Generate_Parent_References
(
913 Unit_Node
, Get_Parent_Entity
(Unit
(Parent_Spec
(Unit_Node
))));
916 -- All components of the context: with-clauses, library unit, ancestors
917 -- if any, (and their context) are analyzed and installed.
919 -- Call special debug routine sm if this is the main unit
921 if Current_Sem_Unit
= Main_Unit
then
925 -- Now analyze the unit (package, subprogram spec, body) itself
929 if Warn_On_Redundant_Constructs
then
930 Check_Redundant_Withs
(Context_Items
(N
));
932 if Nkind
(Unit_Node
) = N_Package_Body
then
933 Check_Redundant_Withs
934 (Context_Items
=> Context_Items
(N
),
935 Spec_Context_Items
=> Context_Items
(Lib_Unit
));
939 -- The above call might have made Unit_Node an N_Subprogram_Body from
940 -- something else, so propagate any Acts_As_Spec flag.
942 if Nkind
(Unit_Node
) = N_Subprogram_Body
943 and then Acts_As_Spec
(Unit_Node
)
945 Set_Acts_As_Spec
(N
);
948 -- Register predefined units in Rtsfind
951 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(Sloc
(N
));
953 if Is_Predefined_File_Name
(Unit_File_Name
(Unum
)) then
954 Set_RTU_Loaded
(Unit_Node
);
958 -- Treat compilation unit pragmas that appear after the library unit
960 if Present
(Pragmas_After
(Aux_Decls_Node
(N
))) then
962 Prag_Node
: Node_Id
:= First
(Pragmas_After
(Aux_Decls_Node
(N
)));
964 while Present
(Prag_Node
) loop
971 -- Generate distribution stubs if requested and no error
974 and then (Distribution_Stub_Mode
= Generate_Receiver_Stub_Body
976 Distribution_Stub_Mode
= Generate_Caller_Stub_Body
)
977 and then not Fatal_Error
(Main_Unit
)
979 if Is_RCI_Pkg_Spec_Or_Body
(N
) then
981 -- Regular RCI package
983 Add_Stub_Constructs
(N
);
985 elsif (Nkind
(Unit_Node
) = N_Package_Declaration
986 and then Is_Shared_Passive
(Defining_Entity
987 (Specification
(Unit_Node
))))
988 or else (Nkind
(Unit_Node
) = N_Package_Body
990 Is_Shared_Passive
(Corresponding_Spec
(Unit_Node
)))
992 -- Shared passive package
994 Add_Stub_Constructs
(N
);
996 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
998 Is_Remote_Call_Interface
999 (Defining_Entity
(Specification
(Instance_Spec
(Unit_Node
))))
1001 -- Instantiation of a RCI generic package
1003 Add_Stub_Constructs
(N
);
1007 -- Remove unit from visibility, so that environment is clean for the
1008 -- next compilation, which is either the main unit or some other unit
1011 if Nkind_In
(Unit_Node
, N_Package_Declaration
,
1012 N_Package_Renaming_Declaration
,
1013 N_Subprogram_Declaration
)
1014 or else Nkind
(Unit_Node
) in N_Generic_Declaration
1016 (Nkind
(Unit_Node
) = N_Subprogram_Body
1017 and then Acts_As_Spec
(Unit_Node
))
1019 Remove_Unit_From_Visibility
(Defining_Entity
(Unit_Node
));
1021 -- If the unit is an instantiation whose body will be elaborated for
1022 -- inlining purposes, use the proper entity of the instance. The entity
1023 -- may be missing if the instantiation was illegal.
1025 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
1026 and then not Error_Posted
(Unit_Node
)
1027 and then Present
(Instance_Spec
(Unit_Node
))
1029 Remove_Unit_From_Visibility
1030 (Defining_Entity
(Instance_Spec
(Unit_Node
)));
1032 elsif Nkind
(Unit_Node
) = N_Package_Body
1033 or else (Nkind
(Unit_Node
) = N_Subprogram_Body
1034 and then not Acts_As_Spec
(Unit_Node
))
1036 -- Bodies that are not the main unit are compiled if they are generic
1037 -- or contain generic or inlined units. Their analysis brings in the
1038 -- context of the corresponding spec (unit declaration) which must be
1039 -- removed as well, to return the compilation environment to its
1042 Remove_Context
(Lib_Unit
);
1043 Set_Is_Immediately_Visible
(Defining_Entity
(Unit
(Lib_Unit
)), False);
1046 -- Last step is to deinstall the context we just installed as well as
1047 -- the unit just compiled.
1051 -- If this is the main unit and we are generating code, we must check
1052 -- that all generic units in the context have a body if they need it,
1053 -- even if they have not been instantiated. In the absence of .ali files
1054 -- for generic units, we must force the load of the body, just to
1055 -- produce the proper error if the body is absent. We skip this
1056 -- verification if the main unit itself is generic.
1058 if Get_Cunit_Unit_Number
(N
) = Main_Unit
1059 and then Operating_Mode
= Generate_Code
1060 and then Expander_Active
1062 -- Check whether the source for the body of the unit must be included
1063 -- in a standalone library.
1065 Check_Body_Needed_For_SAL
(Cunit_Entity
(Main_Unit
));
1067 -- Indicate that the main unit is now analyzed, to catch possible
1068 -- circularities between it and generic bodies. Remove main unit from
1069 -- visibility. This might seem superfluous, but the main unit must
1070 -- not be visible in the generic body expansions that follow.
1072 Set_Analyzed
(N
, True);
1073 Set_Is_Immediately_Visible
(Cunit_Entity
(Main_Unit
), False);
1078 Un
: Unit_Number_Type
;
1080 Save_Style_Check
: constant Boolean := Style_Check
;
1083 Item
:= First
(Context_Items
(N
));
1084 while Present
(Item
) loop
1086 -- Check for explicit with clause
1088 if Nkind
(Item
) = N_With_Clause
1089 and then not Implicit_With
(Item
)
1091 -- Ada 2005 (AI-50217): Ignore limited-withed units
1093 and then not Limited_Present
(Item
)
1095 Nam
:= Entity
(Name
(Item
));
1097 -- Compile generic subprogram, unless it is intrinsic or
1098 -- imported so no body is required, or generic package body
1099 -- if the package spec requires a body.
1101 if (Is_Generic_Subprogram
(Nam
)
1102 and then not Is_Intrinsic_Subprogram
(Nam
)
1103 and then not Is_Imported
(Nam
))
1104 or else (Ekind
(Nam
) = E_Generic_Package
1105 and then Unit_Requires_Body
(Nam
))
1107 Style_Check
:= False;
1109 if Present
(Renamed_Object
(Nam
)) then
1112 (Load_Name
=> Get_Body_Name
1114 (Unit_Declaration_Node
1115 (Renamed_Object
(Nam
)))),
1123 (Load_Name
=> Get_Body_Name
1124 (Get_Unit_Name
(Item
)),
1131 if Un
= No_Unit
then
1133 ("body of generic unit& not found", Item
, Nam
);
1136 elsif not Analyzed
(Cunit
(Un
))
1137 and then Un
/= Main_Unit
1138 and then not Fatal_Error
(Un
)
1140 Style_Check
:= False;
1141 Semantics
(Cunit
(Un
));
1149 -- Restore style checks settings
1151 Style_Check
:= Save_Style_Check
;
1155 -- Deal with creating elaboration Boolean if needed. We create an
1156 -- elaboration boolean only for units that come from source since
1157 -- units manufactured by the compiler never need elab checks.
1159 if Comes_From_Source
(N
)
1160 and then Nkind_In
(Unit_Node
, N_Package_Declaration
,
1161 N_Generic_Package_Declaration
,
1162 N_Subprogram_Declaration
,
1163 N_Generic_Subprogram_Declaration
)
1166 Loc
: constant Source_Ptr
:= Sloc
(N
);
1167 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(Loc
);
1170 Spec_Id
:= Defining_Entity
(Unit_Node
);
1171 Generate_Definition
(Spec_Id
);
1173 -- See if an elaboration entity is required for possible access
1174 -- before elaboration checking. Note that we must allow for this
1175 -- even if -gnatE is not set, since a client may be compiled in
1176 -- -gnatE mode and reference the entity.
1178 -- These entities are also used by the binder to prevent multiple
1179 -- attempts to execute the elaboration code for the library case
1180 -- where the elaboration routine might otherwise be called more
1183 -- Case of units which do not require elaboration checks
1186 -- Pure units do not need checks
1190 -- Preelaborated units do not need checks
1192 or else Is_Preelaborated
(Spec_Id
)
1194 -- No checks needed if pragma Elaborate_Body present
1196 or else Has_Pragma_Elaborate_Body
(Spec_Id
)
1198 -- No checks needed if unit does not require a body
1200 or else not Unit_Requires_Body
(Spec_Id
)
1202 -- No checks needed for predefined files
1204 or else Is_Predefined_File_Name
(Unit_File_Name
(Unum
))
1206 -- No checks required if no separate spec
1208 or else Acts_As_Spec
(N
)
1210 -- This is a case where we only need the entity for
1211 -- checking to prevent multiple elaboration checks.
1213 Set_Elaboration_Entity_Required
(Spec_Id
, False);
1215 -- Case of elaboration entity is required for access before
1216 -- elaboration checking (so certainly we must build it!)
1219 Set_Elaboration_Entity_Required
(Spec_Id
, True);
1222 Build_Elaboration_Entity
(N
, Spec_Id
);
1226 -- Freeze the compilation unit entity. This for sure is needed because
1227 -- of some warnings that can be output (see Freeze_Subprogram), but may
1228 -- in general be required. If freezing actions result, place them in the
1229 -- compilation unit actions list, and analyze them.
1232 L
: constant List_Id
:=
1233 Freeze_Entity
(Cunit_Entity
(Current_Sem_Unit
), N
);
1235 while Is_Non_Empty_List
(L
) loop
1236 Insert_Library_Level_Action
(Remove_Head
(L
));
1242 if Nkind
(Unit_Node
) = N_Package_Declaration
1243 and then Get_Cunit_Unit_Number
(N
) /= Main_Unit
1244 and then Expander_Active
1247 Save_Style_Check
: constant Boolean := Style_Check
;
1248 Save_Warning
: constant Warning_Mode_Type
:= Warning_Mode
;
1249 Options
: Style_Check_Options
;
1252 Save_Style_Check_Options
(Options
);
1253 Reset_Style_Check_Options
;
1254 Opt
.Warning_Mode
:= Suppress
;
1255 Check_Body_For_Inlining
(N
, Defining_Entity
(Unit_Node
));
1257 Reset_Style_Check_Options
;
1258 Set_Style_Check_Options
(Options
);
1259 Style_Check
:= Save_Style_Check
;
1260 Warning_Mode
:= Save_Warning
;
1264 -- If we are generating obsolescent warnings, then here is where we
1265 -- generate them for the with'ed items. The reason for this special
1266 -- processing is that the normal mechanism of generating the warnings
1267 -- for referenced entities does not work for context clause references.
1268 -- That's because when we first analyze the context, it is too early to
1269 -- know if the with'ing unit is itself obsolescent (which suppresses
1273 and then Warn_On_Obsolescent_Feature
1274 and then Nkind
(Unit_Node
) not in N_Generic_Instantiation
1276 -- Push current compilation unit as scope, so that the test for
1277 -- being within an obsolescent unit will work correctly. The check
1278 -- is not performed within an instantiation, because the warning
1279 -- will have been emitted in the corresponding generic unit.
1281 Push_Scope
(Defining_Entity
(Unit_Node
));
1283 -- Loop through context items to deal with with clauses
1291 Item
:= First
(Context_Items
(N
));
1292 while Present
(Item
) loop
1293 if Nkind
(Item
) = N_With_Clause
1295 -- Suppress this check in limited-withed units. Further work
1296 -- needed here if we decide to incorporate this check on
1297 -- limited-withed units.
1299 and then not Limited_Present
(Item
)
1302 Ent
:= Entity
(Nam
);
1304 if Is_Obsolescent
(Ent
) then
1305 Output_Obsolescent_Entity_Warnings
(Nam
, Ent
);
1313 -- Remove temporary install of current unit as scope
1317 end Analyze_Compilation_Unit
;
1319 ---------------------
1320 -- Analyze_Context --
1321 ---------------------
1323 procedure Analyze_Context
(N
: Node_Id
) is
1324 Ukind
: constant Node_Kind
:= Nkind
(Unit
(N
));
1328 -- First process all configuration pragmas at the start of the context
1329 -- items. Strictly these are not part of the context clause, but that
1330 -- is where the parser puts them. In any case for sure we must analyze
1331 -- these before analyzing the actual context items, since they can have
1332 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1333 -- be with'ed as a result of changing categorizations in Ada 2005).
1335 Item
:= First
(Context_Items
(N
));
1336 while Present
(Item
)
1337 and then Nkind
(Item
) = N_Pragma
1338 and then Pragma_Name
(Item
) in Configuration_Pragma_Names
1344 -- This is the point at which we capture the configuration settings
1345 -- for the unit. At the moment only the Optimize_Alignment setting
1346 -- needs to be captured. Probably more later ???
1348 if Optimize_Alignment_Local
then
1349 Set_OA_Setting
(Current_Sem_Unit
, 'L');
1351 Set_OA_Setting
(Current_Sem_Unit
, Optimize_Alignment
);
1354 -- Loop through actual context items. This is done in two passes:
1356 -- a) The first pass analyzes non-limited with-clauses and also any
1357 -- configuration pragmas (we need to get the latter analyzed right
1358 -- away, since they can affect processing of subsequent items.
1360 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1362 while Present
(Item
) loop
1364 -- For with clause, analyze the with clause, and then update the
1365 -- version, since we are dependent on a unit that we with.
1367 if Nkind
(Item
) = N_With_Clause
1368 and then not Limited_Present
(Item
)
1370 -- Skip analyzing with clause if no unit, nothing to do (this
1371 -- happens for a with that references a non-existent unit). Skip
1372 -- as well if this is a with_clause for the main unit, which
1373 -- happens if a subunit has a useless with_clause on its parent.
1375 if Present
(Library_Unit
(Item
)) then
1376 if Library_Unit
(Item
) /= Cunit
(Current_Sem_Unit
) then
1380 Set_Entity
(Name
(Item
), Cunit_Entity
(Current_Sem_Unit
));
1384 if not Implicit_With
(Item
) then
1385 Version_Update
(N
, Library_Unit
(Item
));
1388 -- Skip pragmas. Configuration pragmas at the start were handled in
1389 -- the loop above, and remaining pragmas are not processed until we
1390 -- actually install the context (see Install_Context). We delay the
1391 -- analysis of these pragmas to make sure that we have installed all
1392 -- the implicit with's on parent units.
1394 -- Skip use clauses at this stage, since we don't want to do any
1395 -- installing of potentially use-visible entities until we
1396 -- actually install the complete context (in Install_Context).
1397 -- Otherwise things can get installed in the wrong context.
1406 -- Second pass: examine all limited_with clauses. All other context
1407 -- items are ignored in this pass.
1409 Item
:= First
(Context_Items
(N
));
1410 while Present
(Item
) loop
1411 if Nkind
(Item
) = N_With_Clause
1412 and then Limited_Present
(Item
)
1414 -- No need to check errors on implicitly generated limited-with
1417 if not Implicit_With
(Item
) then
1419 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1420 -- properly rejected, including renaming declarations.
1422 if not Nkind_In
(Ukind
, N_Package_Declaration
,
1423 N_Subprogram_Declaration
)
1424 and then Ukind
not in N_Generic_Declaration
1425 and then Ukind
not in N_Generic_Instantiation
1427 Error_Msg_N
("limited with_clause not allowed here", Item
);
1429 -- Check wrong use of a limited with clause applied to the
1430 -- compilation unit containing the limited-with clause.
1432 -- limited with P.Q;
1433 -- package P.Q is ...
1435 elsif Unit
(Library_Unit
(Item
)) = Unit
(N
) then
1436 Error_Msg_N
("wrong use of limited-with clause", Item
);
1438 -- Check wrong use of limited-with clause applied to some
1439 -- immediate ancestor.
1441 elsif Is_Child_Spec
(Unit
(N
)) then
1443 Lib_U
: constant Entity_Id
:= Unit
(Library_Unit
(Item
));
1447 P
:= Parent_Spec
(Unit
(N
));
1449 if Unit
(P
) = Lib_U
then
1450 Error_Msg_N
("limited with_clause cannot "
1451 & "name ancestor", Item
);
1455 exit when not Is_Child_Spec
(Unit
(P
));
1456 P
:= Parent_Spec
(Unit
(P
));
1461 -- Check if the limited-withed unit is already visible through
1462 -- some context clause of the current compilation unit or some
1463 -- ancestor of the current compilation unit.
1466 Lim_Unit_Name
: constant Node_Id
:= Name
(Item
);
1467 Comp_Unit
: Node_Id
;
1469 Unit_Name
: Node_Id
;
1474 It
:= First
(Context_Items
(Comp_Unit
));
1475 while Present
(It
) loop
1477 and then Nkind
(It
) = N_With_Clause
1478 and then not Limited_Present
(It
)
1480 Nkind_In
(Unit
(Library_Unit
(It
)),
1481 N_Package_Declaration
,
1482 N_Package_Renaming_Declaration
)
1484 if Nkind
(Unit
(Library_Unit
(It
))) =
1485 N_Package_Declaration
1487 Unit_Name
:= Name
(It
);
1489 Unit_Name
:= Name
(Unit
(Library_Unit
(It
)));
1492 -- Check if the named package (or some ancestor)
1493 -- leaves visible the full-view of the unit given
1494 -- in the limited-with clause
1497 if Designate_Same_Unit
(Lim_Unit_Name
,
1500 Error_Msg_Sloc
:= Sloc
(It
);
1502 ("simultaneous visibility of limited "
1503 & "and unlimited views not allowed",
1506 ("\unlimited view visible through "
1507 & "context clause #",
1511 elsif Nkind
(Unit_Name
) = N_Identifier
then
1515 Unit_Name
:= Prefix
(Unit_Name
);
1522 exit when not Is_Child_Spec
(Unit
(Comp_Unit
));
1524 Comp_Unit
:= Parent_Spec
(Unit
(Comp_Unit
));
1529 -- Skip analyzing with clause if no unit, see above
1531 if Present
(Library_Unit
(Item
)) then
1535 -- A limited_with does not impose an elaboration order, but
1536 -- there is a semantic dependency for recompilation purposes.
1538 if not Implicit_With
(Item
) then
1539 Version_Update
(N
, Library_Unit
(Item
));
1542 -- Pragmas and use clauses and with clauses other than limited
1543 -- with's are ignored in this pass through the context items.
1551 end Analyze_Context
;
1553 -------------------------------
1554 -- Analyze_Package_Body_Stub --
1555 -------------------------------
1557 procedure Analyze_Package_Body_Stub
(N
: Node_Id
) is
1558 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1562 -- The package declaration must be in the current declarative part
1564 Check_Stub_Level
(N
);
1565 Nam
:= Current_Entity_In_Scope
(Id
);
1567 if No
(Nam
) or else not Is_Package_Or_Generic_Package
(Nam
) then
1568 Error_Msg_N
("missing specification for package stub", N
);
1570 elsif Has_Completion
(Nam
)
1571 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(Nam
)))
1573 Error_Msg_N
("duplicate or redundant stub for package", N
);
1576 -- Indicate that the body of the package exists. If we are doing
1577 -- only semantic analysis, the stub stands for the body. If we are
1578 -- generating code, the existence of the body will be confirmed
1579 -- when we load the proper body.
1581 Set_Has_Completion
(Nam
);
1582 Set_Scope
(Defining_Entity
(N
), Current_Scope
);
1583 Generate_Reference
(Nam
, Id
, 'b');
1584 Analyze_Proper_Body
(N
, Nam
);
1586 end Analyze_Package_Body_Stub
;
1588 -------------------------
1589 -- Analyze_Proper_Body --
1590 -------------------------
1592 procedure Analyze_Proper_Body
(N
: Node_Id
; Nam
: Entity_Id
) is
1593 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
1594 Unum
: Unit_Number_Type
;
1596 procedure Optional_Subunit
;
1597 -- This procedure is called when the main unit is a stub, or when we
1598 -- are not generating code. In such a case, we analyze the subunit if
1599 -- present, which is user-friendly and in fact required for ASIS, but
1600 -- we don't complain if the subunit is missing.
1602 ----------------------
1603 -- Optional_Subunit --
1604 ----------------------
1606 procedure Optional_Subunit
is
1607 Comp_Unit
: Node_Id
;
1610 -- Try to load subunit, but ignore any errors that occur during the
1611 -- loading of the subunit, by using the special feature in Errout to
1612 -- ignore all errors. Note that Fatal_Error will still be set, so we
1613 -- will be able to check for this case below.
1615 if not ASIS_Mode
then
1616 Ignore_Errors_Enable
:= Ignore_Errors_Enable
+ 1;
1621 (Load_Name
=> Subunit_Name
,
1626 if not ASIS_Mode
then
1627 Ignore_Errors_Enable
:= Ignore_Errors_Enable
- 1;
1630 -- All done if we successfully loaded the subunit
1633 and then (not Fatal_Error
(Unum
) or else Try_Semantics
)
1635 Comp_Unit
:= Cunit
(Unum
);
1637 -- If the file was empty or seriously mangled, the unit itself may
1640 if No
(Unit
(Comp_Unit
)) then
1642 ("subunit does not contain expected proper body", N
);
1644 elsif Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
1646 ("expected SEPARATE subunit, found child unit",
1647 Cunit_Entity
(Unum
));
1649 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
1650 Analyze_Subunit
(Comp_Unit
);
1651 Set_Library_Unit
(N
, Comp_Unit
);
1654 elsif Unum
= No_Unit
1655 and then Present
(Nam
)
1657 if Is_Protected_Type
(Nam
) then
1658 Set_Corresponding_Body
(Parent
(Nam
), Defining_Identifier
(N
));
1660 Set_Corresponding_Body
(
1661 Unit_Declaration_Node
(Nam
), Defining_Identifier
(N
));
1664 end Optional_Subunit
;
1666 -- Start of processing for Analyze_Proper_Body
1669 -- If the subunit is already loaded, it means that the main unit is a
1670 -- subunit, and that the current unit is one of its parents which was
1671 -- being analyzed to provide the needed context for the analysis of the
1672 -- subunit. In this case we analyze the subunit and continue with the
1673 -- parent, without looking at subsequent subunits.
1675 if Is_Loaded
(Subunit_Name
) then
1677 -- If the proper body is already linked to the stub node, the stub is
1678 -- in a generic unit and just needs analyzing.
1680 if Present
(Library_Unit
(N
)) then
1681 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
1683 -- If the subunit has severe errors, the spec of the enclosing
1684 -- body may not be available, in which case do not try analysis.
1686 if Serious_Errors_Detected
> 0
1687 and then No
(Library_Unit
(Library_Unit
(N
)))
1692 Analyze_Subunit
(Library_Unit
(N
));
1694 -- Otherwise we must load the subunit and link to it
1697 -- Load the subunit, this must work, since we originally loaded
1698 -- the subunit earlier on. So this will not really load it, just
1699 -- give access to it.
1703 (Load_Name
=> Subunit_Name
,
1708 -- And analyze the subunit in the parent context (note that we
1709 -- do not call Semantics, since that would remove the parent
1710 -- context). Because of this, we have to manually reset the
1711 -- compiler state to Analyzing since it got destroyed by Load.
1713 if Unum
/= No_Unit
then
1714 Compiler_State
:= Analyzing
;
1716 -- Check that the proper body is a subunit and not a child
1717 -- unit. If the unit was previously loaded, the error will
1718 -- have been emitted when copying the generic node, so we
1719 -- just return to avoid cascaded errors.
1721 if Nkind
(Unit
(Cunit
(Unum
))) /= N_Subunit
then
1725 Set_Corresponding_Stub
(Unit
(Cunit
(Unum
)), N
);
1726 Analyze_Subunit
(Cunit
(Unum
));
1727 Set_Library_Unit
(N
, Cunit
(Unum
));
1731 -- If the main unit is a subunit, then we are just performing semantic
1732 -- analysis on that subunit, and any other subunits of any parent unit
1733 -- should be ignored, except that if we are building trees for ASIS
1734 -- usage we want to annotate the stub properly.
1736 elsif Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
1737 and then Subunit_Name
/= Unit_Name
(Main_Unit
)
1743 -- But before we return, set the flag for unloaded subunits. This
1744 -- will suppress junk warnings of variables in the same declarative
1745 -- part (or a higher level one) that are in danger of looking unused
1746 -- when in fact there might be a declaration in the subunit that we
1747 -- do not intend to load.
1749 Unloaded_Subunits
:= True;
1752 -- If the subunit is not already loaded, and we are generating code,
1753 -- then this is the case where compilation started from the parent, and
1754 -- we are generating code for an entire subunit tree. In that case we
1755 -- definitely need to load the subunit.
1757 -- In order to continue the analysis with the rest of the parent,
1758 -- and other subunits, we load the unit without requiring its
1759 -- presence, and emit a warning if not found, rather than terminating
1760 -- the compilation abruptly, as for other missing file problems.
1762 elsif Original_Operating_Mode
= Generate_Code
then
1764 -- If the proper body is already linked to the stub node, the stub is
1765 -- in a generic unit and just needs analyzing.
1767 -- We update the version. Although we are not strictly technically
1768 -- semantically dependent on the subunit, given our approach of macro
1769 -- substitution of subunits, it makes sense to include it in the
1770 -- version identification.
1772 if Present
(Library_Unit
(N
)) then
1773 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
1774 Analyze_Subunit
(Library_Unit
(N
));
1775 Version_Update
(Cunit
(Main_Unit
), Library_Unit
(N
));
1777 -- Otherwise we must load the subunit and link to it
1780 -- Make sure that, if the subunit is preprocessed and -gnateG is
1781 -- specified, the preprocessed file will be written.
1783 Lib
.Analysing_Subunit_Of_Main
:= True;
1786 (Load_Name
=> Subunit_Name
,
1790 Lib
.Analysing_Subunit_Of_Main
:= False;
1792 -- Give message if we did not get the unit Emit warning even if
1793 -- missing subunit is not within main unit, to simplify debugging.
1795 if Original_Operating_Mode
= Generate_Code
1796 and then Unum
= No_Unit
1798 Error_Msg_Unit_1
:= Subunit_Name
;
1800 Get_File_Name
(Subunit_Name
, Subunit
=> True);
1802 ("subunit$$ in file{ not found??!!", N
);
1803 Subunits_Missing
:= True;
1806 -- Load_Unit may reset Compiler_State, since it may have been
1807 -- necessary to parse an additional units, so we make sure that
1808 -- we reset it to the Analyzing state.
1810 Compiler_State
:= Analyzing
;
1812 if Unum
/= No_Unit
then
1813 if Debug_Flag_L
then
1814 Write_Str
("*** Loaded subunit from stub. Analyze");
1819 Comp_Unit
: constant Node_Id
:= Cunit
(Unum
);
1822 -- Check for child unit instead of subunit
1824 if Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
1826 ("expected SEPARATE subunit, found child unit",
1827 Cunit_Entity
(Unum
));
1829 -- OK, we have a subunit
1832 -- Set corresponding stub (even if errors)
1834 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
1836 -- Collect SCO information for loaded subunit if we are
1837 -- in the main unit.
1841 In_Extended_Main_Source_Unit
1842 (Cunit_Entity
(Current_Sem_Unit
))
1847 -- Analyze the unit if semantics active
1849 if not Fatal_Error
(Unum
) or else Try_Semantics
then
1850 Analyze_Subunit
(Comp_Unit
);
1853 -- Set the library unit pointer in any case
1855 Set_Library_Unit
(N
, Comp_Unit
);
1857 -- We update the version. Although we are not technically
1858 -- semantically dependent on the subunit, given our
1859 -- approach of macro substitution of subunits, it makes
1860 -- sense to include it in the version identification.
1862 Version_Update
(Cunit
(Main_Unit
), Comp_Unit
);
1868 -- The remaining case is when the subunit is not already loaded and we
1869 -- are not generating code. In this case we are just performing semantic
1870 -- analysis on the parent, and we are not interested in the subunit. For
1871 -- subprograms, analyze the stub as a body. For other entities the stub
1872 -- has already been marked as completed.
1877 end Analyze_Proper_Body
;
1879 ----------------------------------
1880 -- Analyze_Protected_Body_Stub --
1881 ----------------------------------
1883 procedure Analyze_Protected_Body_Stub
(N
: Node_Id
) is
1884 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Defining_Identifier
(N
));
1887 Check_Stub_Level
(N
);
1889 -- First occurrence of name may have been as an incomplete type
1891 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
1892 Nam
:= Full_View
(Nam
);
1896 or else not Is_Protected_Type
(Etype
(Nam
))
1898 Error_Msg_N
("missing specification for Protected body", N
);
1900 Set_Scope
(Defining_Entity
(N
), Current_Scope
);
1901 Set_Has_Completion
(Etype
(Nam
));
1902 Generate_Reference
(Nam
, Defining_Identifier
(N
), 'b');
1903 Analyze_Proper_Body
(N
, Etype
(Nam
));
1905 end Analyze_Protected_Body_Stub
;
1907 ----------------------------------
1908 -- Analyze_Subprogram_Body_Stub --
1909 ----------------------------------
1911 -- A subprogram body stub can appear with or without a previous spec. If
1912 -- there is one, then the analysis of the body will find it and verify
1913 -- conformance. The formals appearing in the specification of the stub play
1914 -- no role, except for requiring an additional conformance check. If there
1915 -- is no previous subprogram declaration, the stub acts as a spec, and
1916 -- provides the defining entity for the subprogram.
1918 procedure Analyze_Subprogram_Body_Stub
(N
: Node_Id
) is
1922 Check_Stub_Level
(N
);
1924 -- Verify that the identifier for the stub is unique within this
1925 -- declarative part.
1927 if Nkind_In
(Parent
(N
), N_Block_Statement
,
1931 Decl
:= First
(Declarations
(Parent
(N
)));
1932 while Present
(Decl
)
1935 if Nkind
(Decl
) = N_Subprogram_Body_Stub
1936 and then (Chars
(Defining_Unit_Name
(Specification
(Decl
))) =
1937 Chars
(Defining_Unit_Name
(Specification
(N
))))
1939 Error_Msg_N
("identifier for stub is not unique", N
);
1946 -- Treat stub as a body, which checks conformance if there is a previous
1947 -- declaration, or else introduces entity and its signature.
1949 Analyze_Subprogram_Body
(N
);
1950 Analyze_Proper_Body
(N
, Empty
);
1951 end Analyze_Subprogram_Body_Stub
;
1953 ---------------------
1954 -- Analyze_Subunit --
1955 ---------------------
1957 -- A subunit is compiled either by itself (for semantic checking) or as
1958 -- part of compiling the parent (for code generation). In either case, by
1959 -- the time we actually process the subunit, the parent has already been
1960 -- installed and analyzed. The node N is a compilation unit, whose context
1961 -- needs to be treated here, because we come directly here from the parent
1962 -- without calling Analyze_Compilation_Unit.
1964 -- The compilation context includes the explicit context of the subunit,
1965 -- and the context of the parent, together with the parent itself. In order
1966 -- to compile the current context, we remove the one inherited from the
1967 -- parent, in order to have a clean visibility table. We restore the parent
1968 -- context before analyzing the proper body itself. On exit, we remove only
1969 -- the explicit context of the subunit.
1971 procedure Analyze_Subunit
(N
: Node_Id
) is
1972 Lib_Unit
: constant Node_Id
:= Library_Unit
(N
);
1973 Par_Unit
: constant Entity_Id
:= Current_Scope
;
1975 Lib_Spec
: Node_Id
:= Library_Unit
(Lib_Unit
);
1976 Num_Scopes
: Int
:= 0;
1977 Use_Clauses
: array (1 .. Scope_Stack
.Last
) of Node_Id
;
1978 Enclosing_Child
: Entity_Id
:= Empty
;
1979 Svg
: constant Suppress_Record
:= Scope_Suppress
;
1981 Save_Cunit_Restrictions
: constant Save_Cunit_Boolean_Restrictions
:=
1982 Cunit_Boolean_Restrictions_Save
;
1983 -- Save non-partition wide restrictions before processing the subunit.
1984 -- All subunits are analyzed with config restrictions reset and we need
1985 -- to restore these saved values at the end.
1987 procedure Analyze_Subunit_Context
;
1988 -- Capture names in use clauses of the subunit. This must be done before
1989 -- re-installing parent declarations, because items in the context must
1990 -- not be hidden by declarations local to the parent.
1992 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
);
1993 -- Recursive procedure to restore scope of all ancestors of subunit,
1994 -- from outermost in. If parent is not a subunit, the call to install
1995 -- context installs context of spec and (if parent is a child unit) the
1996 -- context of its parents as well. It is confusing that parents should
1997 -- be treated differently in both cases, but the semantics are just not
2000 procedure Re_Install_Use_Clauses
;
2001 -- As part of the removal of the parent scope, the use clauses are
2002 -- removed, to be reinstalled when the context of the subunit has been
2003 -- analyzed. Use clauses may also have been affected by the analysis of
2004 -- the context of the subunit, so they have to be applied again, to
2005 -- insure that the compilation environment of the rest of the parent
2006 -- unit is identical.
2008 procedure Remove_Scope
;
2009 -- Remove current scope from scope stack, and preserve the list of use
2010 -- clauses in it, to be reinstalled after context is analyzed.
2012 -----------------------------
2013 -- Analyze_Subunit_Context --
2014 -----------------------------
2016 procedure Analyze_Subunit_Context
is
2019 Unit_Name
: Entity_Id
;
2022 Analyze_Context
(N
);
2024 -- Make withed units immediately visible. If child unit, make the
2025 -- ultimate parent immediately visible.
2027 Item
:= First
(Context_Items
(N
));
2028 while Present
(Item
) loop
2029 if Nkind
(Item
) = N_With_Clause
then
2031 -- Protect frontend against previous errors in context clauses
2033 if Nkind
(Name
(Item
)) /= N_Selected_Component
then
2034 if Error_Posted
(Item
) then
2038 -- If a subunits has serious syntax errors, the context
2039 -- may not have been loaded. Add a harmless unit name to
2040 -- attempt processing.
2042 if Serious_Errors_Detected
> 0
2043 and then No
(Entity
(Name
(Item
)))
2045 Set_Entity
(Name
(Item
), Standard_Standard
);
2048 Unit_Name
:= Entity
(Name
(Item
));
2050 Set_Is_Visible_Lib_Unit
(Unit_Name
);
2051 exit when Scope
(Unit_Name
) = Standard_Standard
;
2052 Unit_Name
:= Scope
(Unit_Name
);
2054 if No
(Unit_Name
) then
2055 Check_Error_Detected
;
2060 if not Is_Immediately_Visible
(Unit_Name
) then
2061 Set_Is_Immediately_Visible
(Unit_Name
);
2062 Set_Context_Installed
(Item
);
2067 elsif Nkind
(Item
) = N_Use_Package_Clause
then
2068 Nam
:= First
(Names
(Item
));
2069 while Present
(Nam
) loop
2074 elsif Nkind
(Item
) = N_Use_Type_Clause
then
2075 Nam
:= First
(Subtype_Marks
(Item
));
2076 while Present
(Nam
) loop
2085 -- Reset visibility of withed units. They will be made visible again
2086 -- when we install the subunit context.
2088 Item
:= First
(Context_Items
(N
));
2089 while Present
(Item
) loop
2090 if Nkind
(Item
) = N_With_Clause
2092 -- Protect frontend against previous errors in context clauses
2094 and then Nkind
(Name
(Item
)) /= N_Selected_Component
2095 and then not Error_Posted
(Item
)
2097 Unit_Name
:= Entity
(Name
(Item
));
2099 Set_Is_Visible_Lib_Unit
(Unit_Name
, False);
2100 exit when Scope
(Unit_Name
) = Standard_Standard
;
2101 Unit_Name
:= Scope
(Unit_Name
);
2104 if Context_Installed
(Item
) then
2105 Set_Is_Immediately_Visible
(Unit_Name
, False);
2106 Set_Context_Installed
(Item
, False);
2112 end Analyze_Subunit_Context
;
2114 ------------------------
2115 -- Re_Install_Parents --
2116 ------------------------
2118 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
) is
2122 if Nkind
(Unit
(L
)) = N_Subunit
then
2123 Re_Install_Parents
(Library_Unit
(L
), Scope
(Scop
));
2126 Install_Context
(L
);
2128 -- If the subunit occurs within a child unit, we must restore the
2129 -- immediate visibility of any siblings that may occur in context.
2131 if Present
(Enclosing_Child
) then
2132 Install_Siblings
(Enclosing_Child
, L
);
2137 if Scop
/= Par_Unit
then
2138 Set_Is_Immediately_Visible
(Scop
);
2141 -- Make entities in scope visible again. For child units, restore
2142 -- visibility only if they are actually in context.
2144 E
:= First_Entity
(Current_Scope
);
2145 while Present
(E
) loop
2146 if not Is_Child_Unit
(E
)
2147 or else Is_Visible_Lib_Unit
(E
)
2149 Set_Is_Immediately_Visible
(E
);
2155 -- A subunit appears within a body, and for a nested subunits all the
2156 -- parents are bodies. Restore full visibility of their private
2159 if Is_Package_Or_Generic_Package
(Scop
) then
2160 Set_In_Package_Body
(Scop
);
2161 Install_Private_Declarations
(Scop
);
2163 end Re_Install_Parents
;
2165 ----------------------------
2166 -- Re_Install_Use_Clauses --
2167 ----------------------------
2169 procedure Re_Install_Use_Clauses
is
2172 for J
in reverse 1 .. Num_Scopes
loop
2173 U
:= Use_Clauses
(J
);
2174 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:= U
;
2175 Install_Use_Clauses
(U
, Force_Installation
=> True);
2177 end Re_Install_Use_Clauses
;
2183 procedure Remove_Scope
is
2187 Num_Scopes
:= Num_Scopes
+ 1;
2188 Use_Clauses
(Num_Scopes
) :=
2189 Scope_Stack
.Table
(Scope_Stack
.Last
).First_Use_Clause
;
2191 E
:= First_Entity
(Current_Scope
);
2192 while Present
(E
) loop
2193 Set_Is_Immediately_Visible
(E
, False);
2197 if Is_Child_Unit
(Current_Scope
) then
2198 Enclosing_Child
:= Current_Scope
;
2204 -- Start of processing for Analyze_Subunit
2207 -- For subunit in main extended unit, we reset the configuration values
2208 -- for the non-partition-wide restrictions. For other units reset them.
2210 if In_Extended_Main_Source_Unit
(N
) then
2211 Restore_Config_Cunit_Boolean_Restrictions
;
2213 Reset_Cunit_Boolean_Restrictions
;
2218 Nam
: Node_Id
:= Name
(Unit
(N
));
2221 if Nkind
(Nam
) = N_Selected_Component
then
2222 Nam
:= Selector_Name
(Nam
);
2225 Check_Identifier
(Nam
, Par_Unit
);
2229 if not Is_Empty_List
(Context_Items
(N
)) then
2231 -- Save current use clauses
2234 Remove_Context
(Lib_Unit
);
2236 -- Now remove parents and their context, including enclosing subunits
2237 -- and the outer parent body which is not a subunit.
2239 if Present
(Lib_Spec
) then
2240 Remove_Context
(Lib_Spec
);
2242 while Nkind
(Unit
(Lib_Spec
)) = N_Subunit
loop
2243 Lib_Spec
:= Library_Unit
(Lib_Spec
);
2245 Remove_Context
(Lib_Spec
);
2248 if Nkind
(Unit
(Lib_Unit
)) = N_Subunit
then
2252 if Nkind
(Unit
(Lib_Spec
)) = N_Package_Body
then
2253 Remove_Context
(Library_Unit
(Lib_Spec
));
2257 Set_Is_Immediately_Visible
(Par_Unit
, False);
2259 Analyze_Subunit_Context
;
2261 Re_Install_Parents
(Lib_Unit
, Par_Unit
);
2262 Set_Is_Immediately_Visible
(Par_Unit
);
2264 -- If the context includes a child unit of the parent of the subunit,
2265 -- the parent will have been removed from visibility, after compiling
2266 -- that cousin in the context. The visibility of the parent must be
2267 -- restored now. This also applies if the context includes another
2268 -- subunit of the same parent which in turn includes a child unit in
2271 if Is_Package_Or_Generic_Package
(Par_Unit
) then
2272 if not Is_Immediately_Visible
(Par_Unit
)
2273 or else (Present
(First_Entity
(Par_Unit
))
2274 and then not Is_Immediately_Visible
2275 (First_Entity
(Par_Unit
)))
2277 Set_Is_Immediately_Visible
(Par_Unit
);
2278 Install_Visible_Declarations
(Par_Unit
);
2279 Install_Private_Declarations
(Par_Unit
);
2283 Re_Install_Use_Clauses
;
2284 Install_Context
(N
);
2286 -- Restore state of suppress flags for current body
2288 Scope_Suppress
:= Svg
;
2290 -- If the subunit is within a child unit, then siblings of any parent
2291 -- unit that appear in the context clause of the subunit must also be
2292 -- made immediately visible.
2294 if Present
(Enclosing_Child
) then
2295 Install_Siblings
(Enclosing_Child
, N
);
2299 Analyze
(Proper_Body
(Unit
(N
)));
2302 -- The subunit may contain a with_clause on a sibling of some ancestor.
2303 -- Removing the context will remove from visibility those ancestor child
2304 -- units, which must be restored to the visibility they have in the
2307 if Present
(Enclosing_Child
) then
2312 while Present
(C
) and then C
/= Standard_Standard
loop
2313 Set_Is_Immediately_Visible
(C
);
2314 Set_Is_Visible_Lib_Unit
(C
);
2320 -- Deal with restore of restrictions
2322 Cunit_Boolean_Restrictions_Restore
(Save_Cunit_Restrictions
);
2323 end Analyze_Subunit
;
2325 ----------------------------
2326 -- Analyze_Task_Body_Stub --
2327 ----------------------------
2329 procedure Analyze_Task_Body_Stub
(N
: Node_Id
) is
2330 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Defining_Identifier
(N
));
2331 Loc
: constant Source_Ptr
:= Sloc
(N
);
2334 Check_Stub_Level
(N
);
2336 -- First occurrence of name may have been as an incomplete type
2338 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
2339 Nam
:= Full_View
(Nam
);
2342 if No
(Nam
) or else not Is_Task_Type
(Etype
(Nam
)) then
2343 Error_Msg_N
("missing specification for task body", N
);
2345 Set_Scope
(Defining_Entity
(N
), Current_Scope
);
2346 Generate_Reference
(Nam
, Defining_Identifier
(N
), 'b');
2348 -- Check for duplicate stub, if so give message and terminate
2350 if Has_Completion
(Etype
(Nam
)) then
2351 Error_Msg_N
("duplicate stub for task", N
);
2354 Set_Has_Completion
(Etype
(Nam
));
2357 Analyze_Proper_Body
(N
, Etype
(Nam
));
2359 -- Set elaboration flag to indicate that entity is callable. This
2360 -- cannot be done in the expansion of the body itself, because the
2361 -- proper body is not in a declarative part. This is only done if
2362 -- expansion is active, because the context may be generic and the
2363 -- flag not defined yet.
2365 if Full_Expander_Active
then
2367 Make_Assignment_Statement
(Loc
,
2369 Make_Identifier
(Loc
,
2370 Chars
=> New_External_Name
(Chars
(Etype
(Nam
)), 'E')),
2371 Expression
=> New_Reference_To
(Standard_True
, Loc
)));
2374 end Analyze_Task_Body_Stub
;
2376 -------------------------
2377 -- Analyze_With_Clause --
2378 -------------------------
2380 -- Analyze the declaration of a unit in a with clause. At end, label the
2381 -- with clause with the defining entity for the unit.
2383 procedure Analyze_With_Clause
(N
: Node_Id
) is
2385 -- Retrieve the original kind of the unit node, before analysis. If it
2386 -- is a subprogram instantiation, its analysis below will rewrite the
2387 -- node as the declaration of the wrapper package. If the same
2388 -- instantiation appears indirectly elsewhere in the context, it will
2389 -- have been analyzed already.
2391 Unit_Kind
: constant Node_Kind
:=
2392 Nkind
(Original_Node
(Unit
(Library_Unit
(N
))));
2393 Nam
: constant Node_Id
:= Name
(N
);
2395 Par_Name
: Entity_Id
;
2400 -- Set True if the unit currently being compiled is an internal unit
2402 Restriction_Violation
: Boolean := False;
2403 -- Set True if a with violates a restriction, no point in giving any
2404 -- warnings if we have this definite error.
2406 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
2409 U
:= Unit
(Library_Unit
(N
));
2411 -- If this is an internal unit which is a renaming, then this is a
2412 -- violation of No_Obsolescent_Features.
2414 -- Note: this is not quite right if the user defines one of these units
2415 -- himself, but that's a marginal case, and fixing it is hard ???
2417 if Restriction_Check_Required
(No_Obsolescent_Features
) then
2419 F
: constant File_Name_Type
:=
2420 Unit_File_Name
(Get_Source_Unit
(U
));
2422 if Is_Predefined_File_Name
(F
, Renamings_Included
=> True)
2424 Is_Predefined_File_Name
(F
, Renamings_Included
=> False)
2426 Check_Restriction
(No_Obsolescent_Features
, N
);
2427 Restriction_Violation
:= True;
2432 -- Check No_Implementation_Units violation
2434 if Restriction_Check_Required
(No_Implementation_Units
) then
2435 if Not_Impl_Defined_Unit
(Get_Source_Unit
(U
)) then
2438 Check_Restriction
(No_Implementation_Units
, Nam
);
2439 Restriction_Violation
:= True;
2443 -- Several actions are skipped for dummy packages (those supplied for
2444 -- with's where no matching file could be found). Such packages are
2445 -- identified by the Sloc value being set to No_Location.
2447 if Limited_Present
(N
) then
2449 -- Ada 2005 (AI-50217): Build visibility structures but do not
2450 -- analyze the unit.
2452 if Sloc
(U
) /= No_Location
then
2453 Build_Limited_Views
(N
);
2459 -- If the library unit is a predefined unit, and we are in high
2460 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2461 -- for the analysis of the with'ed unit. This mode does not prevent
2462 -- explicit with'ing of run-time units.
2464 if Configurable_Run_Time_Mode
2465 and then Is_Predefined_File_Name
(Unit_File_Name
(Get_Source_Unit
(U
)))
2467 Configurable_Run_Time_Mode
:= False;
2468 Semantics
(Library_Unit
(N
));
2469 Configurable_Run_Time_Mode
:= True;
2472 Semantics
(Library_Unit
(N
));
2475 Intunit
:= Is_Internal_File_Name
(Unit_File_Name
(Current_Sem_Unit
));
2477 if Sloc
(U
) /= No_Location
then
2479 -- Check restrictions, except that we skip the check if this is an
2480 -- internal unit unless we are compiling the internal unit as the
2481 -- main unit. We also skip this for dummy packages.
2483 Check_Restriction_No_Dependence
(Nam
, N
);
2485 if not Intunit
or else Current_Sem_Unit
= Main_Unit
then
2486 Check_Restricted_Unit
(Unit_Name
(Get_Source_Unit
(U
)), N
);
2489 -- Deal with special case of GNAT.Current_Exceptions which interacts
2490 -- with the optimization of local raise statements into gotos.
2492 if Nkind
(Nam
) = N_Selected_Component
2493 and then Nkind
(Prefix
(Nam
)) = N_Identifier
2494 and then Chars
(Prefix
(Nam
)) = Name_Gnat
2495 and then Nam_In
(Chars
(Selector_Name
(Nam
)),
2496 Name_Most_Recent_Exception
,
2497 Name_Exception_Traces
)
2499 Check_Restriction
(No_Exception_Propagation
, N
);
2500 Special_Exception_Package_Used
:= True;
2503 -- Check for inappropriate with of internal implementation unit if we
2504 -- are not compiling an internal unit and also check for withing unit
2505 -- in wrong version of Ada. Do not issue these messages for implicit
2506 -- with's generated by the compiler itself.
2508 if Implementation_Unit_Warnings
2509 and then not Intunit
2510 and then not Implicit_With
(N
)
2511 and then not Restriction_Violation
2514 U_Kind
: constant Kind_Of_Unit
:=
2515 Get_Kind_Of_Unit
(Get_Source_Unit
(U
));
2518 if U_Kind
= Implementation_Unit
then
2519 Error_Msg_F
("& is an internal 'G'N'A'T unit?i?", Name
(N
));
2521 -- Add alternative name if available, otherwise issue a
2522 -- general warning message.
2524 if Error_Msg_Strlen
/= 0 then
2525 Error_Msg_F
("\use ""~"" instead?i?", Name
(N
));
2528 ("\use of this unit is non-portable " &
2529 "and version-dependent?i?", Name
(N
));
2532 elsif U_Kind
= Ada_2005_Unit
2533 and then Ada_Version
< Ada_2005
2534 and then Warn_On_Ada_2005_Compatibility
2536 Error_Msg_N
("& is an Ada 2005 unit?i?", Name
(N
));
2538 elsif U_Kind
= Ada_2012_Unit
2539 and then Ada_Version
< Ada_2012
2540 and then Warn_On_Ada_2012_Compatibility
2542 Error_Msg_N
("& is an Ada 2012 unit?i?", Name
(N
));
2548 -- Semantic analysis of a generic unit is performed on a copy of
2549 -- the original tree. Retrieve the entity on which semantic info
2550 -- actually appears.
2552 if Unit_Kind
in N_Generic_Declaration
then
2553 E_Name
:= Defining_Entity
(U
);
2555 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2556 -- the case of an instantiation, semantic analysis above will have
2557 -- replaced the unit by its instantiated version. If the instance body
2558 -- has been generated, the instance now denotes the body entity. For
2559 -- visibility purposes we need the entity of its spec.
2561 elsif (Unit_Kind
= N_Package_Instantiation
2562 or else Nkind
(Original_Node
(Unit
(Library_Unit
(N
)))) =
2563 N_Package_Instantiation
)
2564 and then Nkind
(U
) = N_Package_Body
2566 E_Name
:= Corresponding_Spec
(U
);
2568 elsif Unit_Kind
= N_Package_Instantiation
2569 and then Nkind
(U
) = N_Package_Instantiation
2570 and then Present
(Instance_Spec
(U
))
2572 -- If the instance has not been rewritten as a package declaration,
2573 -- then it appeared already in a previous with clause. Retrieve
2574 -- the entity from the previous instance.
2576 E_Name
:= Defining_Entity
(Specification
(Instance_Spec
(U
)));
2578 elsif Unit_Kind
in N_Subprogram_Instantiation
then
2580 -- The visible subprogram is created during instantiation, and is
2581 -- an attribute of the wrapper package. We retrieve the wrapper
2582 -- package directly from the instantiation node. If the instance
2583 -- is inlined the unit is still an instantiation. Otherwise it has
2584 -- been rewritten as the declaration of the wrapper itself.
2586 if Nkind
(U
) in N_Subprogram_Instantiation
then
2589 (Defining_Entity
(Specification
(Instance_Spec
(U
))));
2591 E_Name
:= Related_Instance
(Defining_Entity
(U
));
2594 elsif Unit_Kind
= N_Package_Renaming_Declaration
2595 or else Unit_Kind
in N_Generic_Renaming_Declaration
2597 E_Name
:= Defining_Entity
(U
);
2599 elsif Unit_Kind
= N_Subprogram_Body
2600 and then Nkind
(Name
(N
)) = N_Selected_Component
2601 and then not Acts_As_Spec
(Library_Unit
(N
))
2603 -- For a child unit that has no spec, one has been created and
2604 -- analyzed. The entity required is that of the spec.
2606 E_Name
:= Corresponding_Spec
(U
);
2609 E_Name
:= Defining_Entity
(U
);
2612 if Nkind
(Name
(N
)) = N_Selected_Component
then
2614 -- Child unit in a with clause
2616 Change_Selected_Component_To_Expanded_Name
(Name
(N
));
2618 -- If this is a child unit without a spec, and it has been analyzed
2619 -- already, a declaration has been created for it. The with_clause
2620 -- must reflect the actual body, and not the generated declaration,
2621 -- to prevent spurious binding errors involving an out-of-date spec.
2622 -- Note that this can only happen if the unit includes more than one
2623 -- with_clause for the child unit (e.g. in separate subunits).
2625 if Unit_Kind
= N_Subprogram_Declaration
2626 and then Analyzed
(Library_Unit
(N
))
2627 and then not Comes_From_Source
(Library_Unit
(N
))
2629 Set_Library_Unit
(N
,
2630 Cunit
(Get_Source_Unit
(Corresponding_Body
(U
))));
2634 -- Restore style checks
2636 Style_Check
:= Save_Style_Check
;
2638 -- Record the reference, but do NOT set the unit as referenced, we want
2639 -- to consider the unit as unreferenced if this is the only reference
2642 Set_Entity_With_Style_Check
(Name
(N
), E_Name
);
2643 Generate_Reference
(E_Name
, Name
(N
), 'w', Set_Ref
=> False);
2645 -- Generate references and check No_Dependence restriction for parents
2647 if Is_Child_Unit
(E_Name
) then
2648 Pref
:= Prefix
(Name
(N
));
2649 Par_Name
:= Scope
(E_Name
);
2650 while Nkind
(Pref
) = N_Selected_Component
loop
2651 Change_Selected_Component_To_Expanded_Name
(Pref
);
2653 if Present
(Entity
(Selector_Name
(Pref
)))
2655 Present
(Renamed_Entity
(Entity
(Selector_Name
(Pref
))))
2656 and then Entity
(Selector_Name
(Pref
)) /= Par_Name
2658 -- The prefix is a child unit that denotes a renaming declaration.
2659 -- Replace the prefix directly with the renamed unit, because the
2660 -- rest of the prefix is irrelevant to the visibility of the real
2663 Rewrite
(Pref
, New_Occurrence_Of
(Par_Name
, Sloc
(Pref
)));
2667 Set_Entity_With_Style_Check
(Pref
, Par_Name
);
2669 Generate_Reference
(Par_Name
, Pref
);
2670 Check_Restriction_No_Dependence
(Pref
, N
);
2671 Pref
:= Prefix
(Pref
);
2673 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2674 -- is set to Standard_Standard, and no attempt should be made to
2675 -- further unwind scopes.
2677 if Par_Name
/= Standard_Standard
then
2678 Par_Name
:= Scope
(Par_Name
);
2681 -- Abandon processing in case of previous errors
2683 if No
(Par_Name
) then
2684 Check_Error_Detected
;
2689 if Present
(Entity
(Pref
))
2690 and then not Analyzed
(Parent
(Parent
(Entity
(Pref
))))
2692 -- If the entity is set without its unit being compiled, the
2693 -- original parent is a renaming, and Par_Name is the renamed
2694 -- entity. For visibility purposes, we need the original entity,
2695 -- which must be analyzed now because Load_Unit directly retrieves
2696 -- the renamed unit, and the renaming declaration itself has not
2699 Analyze
(Parent
(Parent
(Entity
(Pref
))));
2700 pragma Assert
(Renamed_Object
(Entity
(Pref
)) = Par_Name
);
2701 Par_Name
:= Entity
(Pref
);
2704 -- Guard against missing or misspelled child units
2706 if Present
(Par_Name
) then
2707 Set_Entity_With_Style_Check
(Pref
, Par_Name
);
2708 Generate_Reference
(Par_Name
, Pref
);
2711 pragma Assert
(Serious_Errors_Detected
/= 0);
2713 -- Mark the node to indicate that a related error has been posted.
2714 -- This defends further compilation passes against improper use of
2715 -- the invalid WITH clause node.
2717 Set_Error_Posted
(N
);
2718 Set_Name
(N
, Error
);
2723 -- If the withed unit is System, and a system extension pragma is
2724 -- present, compile the extension now, rather than waiting for a
2725 -- visibility check on a specific entity.
2727 if Chars
(E_Name
) = Name_System
2728 and then Scope
(E_Name
) = Standard_Standard
2729 and then Present
(System_Extend_Unit
)
2730 and then Present_System_Aux
(N
)
2732 -- If the extension is not present, an error will have been emitted
2737 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2738 -- to private_with units; they will be made visible later (just before
2739 -- the private part is analyzed)
2741 if Private_Present
(N
) then
2742 Set_Is_Immediately_Visible
(E_Name
, False);
2744 end Analyze_With_Clause
;
2746 ------------------------------
2747 -- Check_Private_Child_Unit --
2748 ------------------------------
2750 procedure Check_Private_Child_Unit
(N
: Node_Id
) is
2751 Lib_Unit
: constant Node_Id
:= Unit
(N
);
2753 Curr_Unit
: Entity_Id
;
2754 Sub_Parent
: Node_Id
;
2755 Priv_Child
: Entity_Id
;
2756 Par_Lib
: Entity_Id
;
2759 function Is_Private_Library_Unit
(Unit
: Entity_Id
) return Boolean;
2760 -- Returns true if and only if the library unit is declared with
2761 -- an explicit designation of private.
2763 -----------------------------
2764 -- Is_Private_Library_Unit --
2765 -----------------------------
2767 function Is_Private_Library_Unit
(Unit
: Entity_Id
) return Boolean is
2768 Comp_Unit
: constant Node_Id
:= Parent
(Unit_Declaration_Node
(Unit
));
2771 return Private_Present
(Comp_Unit
);
2772 end Is_Private_Library_Unit
;
2774 -- Start of processing for Check_Private_Child_Unit
2777 if Nkind_In
(Lib_Unit
, N_Package_Body
, N_Subprogram_Body
) then
2778 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(N
)));
2779 Par_Lib
:= Curr_Unit
;
2781 elsif Nkind
(Lib_Unit
) = N_Subunit
then
2783 -- The parent is itself a body. The parent entity is to be found in
2784 -- the corresponding spec.
2786 Sub_Parent
:= Library_Unit
(N
);
2787 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(Sub_Parent
)));
2789 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2790 -- enclosing body, retrieve the spec entity which is the proper
2791 -- ancestor we need for the following tests.
2793 if Ekind
(Curr_Unit
) = E_Package_Body
then
2794 Curr_Unit
:= Spec_Entity
(Curr_Unit
);
2797 Par_Lib
:= Curr_Unit
;
2800 Curr_Unit
:= Defining_Entity
(Lib_Unit
);
2802 Par_Lib
:= Curr_Unit
;
2803 Par_Spec
:= Parent_Spec
(Lib_Unit
);
2805 if No
(Par_Spec
) then
2808 Par_Lib
:= Defining_Entity
(Unit
(Par_Spec
));
2812 -- Loop through context items
2814 Item
:= First
(Context_Items
(N
));
2815 while Present
(Item
) loop
2817 -- Ada 2005 (AI-262): Allow private_with of a private child package
2818 -- in public siblings
2820 if Nkind
(Item
) = N_With_Clause
2821 and then not Implicit_With
(Item
)
2822 and then not Limited_Present
(Item
)
2823 and then Is_Private_Descendant
(Entity
(Name
(Item
)))
2825 Priv_Child
:= Entity
(Name
(Item
));
2828 Curr_Parent
: Entity_Id
:= Par_Lib
;
2829 Child_Parent
: Entity_Id
:= Scope
(Priv_Child
);
2830 Prv_Ancestor
: Entity_Id
:= Child_Parent
;
2831 Curr_Private
: Boolean := Is_Private_Library_Unit
(Curr_Unit
);
2834 -- If the child unit is a public child then locate the nearest
2835 -- private ancestor. Child_Parent will then be set to the
2836 -- parent of that ancestor.
2838 if not Is_Private_Library_Unit
(Priv_Child
) then
2839 while Present
(Prv_Ancestor
)
2840 and then not Is_Private_Library_Unit
(Prv_Ancestor
)
2842 Prv_Ancestor
:= Scope
(Prv_Ancestor
);
2845 if Present
(Prv_Ancestor
) then
2846 Child_Parent
:= Scope
(Prv_Ancestor
);
2850 while Present
(Curr_Parent
)
2851 and then Curr_Parent
/= Standard_Standard
2852 and then Curr_Parent
/= Child_Parent
2855 Curr_Private
or else Is_Private_Library_Unit
(Curr_Parent
);
2856 Curr_Parent
:= Scope
(Curr_Parent
);
2859 if No
(Curr_Parent
) then
2860 Curr_Parent
:= Standard_Standard
;
2863 if Curr_Parent
/= Child_Parent
then
2864 if Ekind
(Priv_Child
) = E_Generic_Package
2865 and then Chars
(Priv_Child
) in Text_IO_Package_Name
2866 and then Chars
(Scope
(Scope
(Priv_Child
))) = Name_Ada
2869 ("& is a nested package, not a compilation unit",
2870 Name
(Item
), Priv_Child
);
2874 ("unit in with clause is private child unit!", Item
);
2876 ("\current unit must also have parent&!",
2877 Item
, Child_Parent
);
2881 or else Private_Present
(Item
)
2882 or else Nkind_In
(Lib_Unit
, N_Package_Body
, N_Subunit
)
2883 or else (Nkind
(Lib_Unit
) = N_Subprogram_Body
2884 and then not Acts_As_Spec
(Parent
(Lib_Unit
)))
2890 ("current unit must also be private descendant of&",
2891 Item
, Child_Parent
);
2899 end Check_Private_Child_Unit
;
2901 ----------------------
2902 -- Check_Stub_Level --
2903 ----------------------
2905 procedure Check_Stub_Level
(N
: Node_Id
) is
2906 Par
: constant Node_Id
:= Parent
(N
);
2907 Kind
: constant Node_Kind
:= Nkind
(Par
);
2910 if Nkind_In
(Kind
, N_Package_Body
,
2914 and then Nkind_In
(Parent
(Par
), N_Compilation_Unit
, N_Subunit
)
2918 -- In an instance, a missing stub appears at any level. A warning
2919 -- message will have been emitted already for the missing file.
2921 elsif not In_Instance
then
2922 Error_Msg_N
("stub cannot appear in an inner scope", N
);
2924 elsif Expander_Active
then
2925 Error_Msg_N
("missing proper body", N
);
2927 end Check_Stub_Level
;
2929 ------------------------
2930 -- Expand_With_Clause --
2931 ------------------------
2933 procedure Expand_With_Clause
(Item
: Node_Id
; Nam
: Node_Id
; N
: Node_Id
) is
2934 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
2935 Ent
: constant Entity_Id
:= Entity
(Nam
);
2939 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
;
2940 -- Build name to be used in implicit with_clause. In most cases this
2941 -- is the source name, but if renamings are present we must make the
2942 -- original unit visible, not the one it renames. The entity in the
2943 -- with clause is the renamed unit, but the identifier is the one from
2944 -- the source, which allows us to recover the unit renaming.
2946 ---------------------
2947 -- Build_Unit_Name --
2948 ---------------------
2950 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
is
2955 if Nkind
(Nam
) = N_Identifier
then
2956 return New_Occurrence_Of
(Entity
(Nam
), Loc
);
2959 Ent
:= Entity
(Nam
);
2961 if Present
(Entity
(Selector_Name
(Nam
)))
2962 and then Chars
(Entity
(Selector_Name
(Nam
))) /= Chars
(Ent
)
2964 Nkind
(Unit_Declaration_Node
(Entity
(Selector_Name
(Nam
))))
2965 = N_Package_Renaming_Declaration
2967 -- The name in the with_clause is of the form A.B.C, and B is
2968 -- given by a renaming declaration. In that case we may not
2969 -- have analyzed the unit for B, but replaced it directly in
2970 -- lib-load with the unit it renames. We have to make A.B
2971 -- visible, so analyze the declaration for B now, in case it
2972 -- has not been done yet.
2974 Ent
:= Entity
(Selector_Name
(Nam
));
2977 (Unit_Declaration_Node
(Entity
(Selector_Name
(Nam
)))));
2981 Make_Expanded_Name
(Loc
,
2982 Chars
=> Chars
(Entity
(Nam
)),
2983 Prefix
=> Build_Unit_Name
(Prefix
(Nam
)),
2984 Selector_Name
=> New_Occurrence_Of
(Ent
, Loc
));
2985 Set_Entity
(Result
, Ent
);
2988 end Build_Unit_Name
;
2990 -- Start of processing for Expand_With_Clause
2994 Make_With_Clause
(Loc
,
2995 Name
=> Build_Unit_Name
(Nam
));
2997 P
:= Parent
(Unit_Declaration_Node
(Ent
));
2998 Set_Library_Unit
(Withn
, P
);
2999 Set_Corresponding_Spec
(Withn
, Ent
);
3000 Set_First_Name
(Withn
, True);
3001 Set_Implicit_With
(Withn
, True);
3003 -- If the unit is a package or generic package declaration, a private_
3004 -- with_clause on a child unit implies that the implicit with on the
3005 -- parent is also private.
3007 if Nkind_In
(Unit
(N
), N_Package_Declaration
,
3008 N_Generic_Package_Declaration
)
3010 Set_Private_Present
(Withn
, Private_Present
(Item
));
3013 Prepend
(Withn
, Context_Items
(N
));
3014 Mark_Rewrite_Insertion
(Withn
);
3015 Install_Withed_Unit
(Withn
);
3017 if Nkind
(Nam
) = N_Expanded_Name
then
3018 Expand_With_Clause
(Item
, Prefix
(Nam
), N
);
3020 end Expand_With_Clause
;
3022 -----------------------
3023 -- Get_Parent_Entity --
3024 -----------------------
3026 function Get_Parent_Entity
(Unit
: Node_Id
) return Entity_Id
is
3028 if Nkind
(Unit
) = N_Package_Body
3029 and then Nkind
(Original_Node
(Unit
)) = N_Package_Instantiation
3031 return Defining_Entity
3032 (Specification
(Instance_Spec
(Original_Node
(Unit
))));
3033 elsif Nkind
(Unit
) = N_Package_Instantiation
then
3034 return Defining_Entity
(Specification
(Instance_Spec
(Unit
)));
3036 return Defining_Entity
(Unit
);
3038 end Get_Parent_Entity
;
3040 ---------------------
3041 -- Has_With_Clause --
3042 ---------------------
3044 function Has_With_Clause
3047 Is_Limited
: Boolean := False) return Boolean
3051 function Named_Unit
(Clause
: Node_Id
) return Entity_Id
;
3052 -- Return the entity for the unit named in a [limited] with clause
3058 function Named_Unit
(Clause
: Node_Id
) return Entity_Id
is
3060 if Nkind
(Name
(Clause
)) = N_Selected_Component
then
3061 return Entity
(Selector_Name
(Name
(Clause
)));
3063 return Entity
(Name
(Clause
));
3067 -- Start of processing for Has_With_Clause
3070 if Present
(Context_Items
(C_Unit
)) then
3071 Item
:= First
(Context_Items
(C_Unit
));
3072 while Present
(Item
) loop
3073 if Nkind
(Item
) = N_With_Clause
3074 and then Limited_Present
(Item
) = Is_Limited
3075 and then Named_Unit
(Item
) = Pack
3085 end Has_With_Clause
;
3087 -----------------------------
3088 -- Implicit_With_On_Parent --
3089 -----------------------------
3091 procedure Implicit_With_On_Parent
3092 (Child_Unit
: Node_Id
;
3095 Loc
: constant Source_Ptr
:= Sloc
(N
);
3096 P
: constant Node_Id
:= Parent_Spec
(Child_Unit
);
3097 P_Unit
: Node_Id
:= Unit
(P
);
3098 P_Name
: constant Entity_Id
:= Get_Parent_Entity
(P_Unit
);
3101 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
;
3102 -- Build prefix of child unit name. Recurse if needed
3104 function Build_Unit_Name
return Node_Id
;
3105 -- If the unit is a child unit, build qualified name with all ancestors
3107 -------------------------
3108 -- Build_Ancestor_Name --
3109 -------------------------
3111 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
is
3112 P_Ref
: constant Node_Id
:=
3113 New_Reference_To
(Defining_Entity
(P
), Loc
);
3114 P_Spec
: Node_Id
:= P
;
3117 -- Ancestor may have been rewritten as a package body. Retrieve
3118 -- the original spec to trace earlier ancestors.
3120 if Nkind
(P
) = N_Package_Body
3121 and then Nkind
(Original_Node
(P
)) = N_Package_Instantiation
3123 P_Spec
:= Original_Node
(P
);
3126 if No
(Parent_Spec
(P_Spec
)) then
3130 Make_Selected_Component
(Loc
,
3131 Prefix
=> Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Spec
))),
3132 Selector_Name
=> P_Ref
);
3134 end Build_Ancestor_Name
;
3136 ---------------------
3137 -- Build_Unit_Name --
3138 ---------------------
3140 function Build_Unit_Name
return Node_Id
is
3144 if No
(Parent_Spec
(P_Unit
)) then
3145 return New_Reference_To
(P_Name
, Loc
);
3149 Make_Expanded_Name
(Loc
,
3150 Chars
=> Chars
(P_Name
),
3151 Prefix
=> Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Unit
))),
3152 Selector_Name
=> New_Reference_To
(P_Name
, Loc
));
3153 Set_Entity
(Result
, P_Name
);
3156 end Build_Unit_Name
;
3158 -- Start of processing for Implicit_With_On_Parent
3161 -- The unit of the current compilation may be a package body that
3162 -- replaces an instance node. In this case we need the original instance
3163 -- node to construct the proper parent name.
3165 if Nkind
(P_Unit
) = N_Package_Body
3166 and then Nkind
(Original_Node
(P_Unit
)) = N_Package_Instantiation
3168 P_Unit
:= Original_Node
(P_Unit
);
3171 -- We add the implicit with if the child unit is the current unit being
3172 -- compiled. If the current unit is a body, we do not want to add an
3173 -- implicit_with a second time to the corresponding spec.
3175 if Nkind
(Child_Unit
) = N_Package_Declaration
3176 and then Child_Unit
/= Unit
(Cunit
(Current_Sem_Unit
))
3181 Withn
:= Make_With_Clause
(Loc
, Name
=> Build_Unit_Name
);
3183 Set_Library_Unit
(Withn
, P
);
3184 Set_Corresponding_Spec
(Withn
, P_Name
);
3185 Set_First_Name
(Withn
, True);
3186 Set_Implicit_With
(Withn
, True);
3188 -- Node is placed at the beginning of the context items, so that
3189 -- subsequent use clauses on the parent can be validated.
3191 Prepend
(Withn
, Context_Items
(N
));
3192 Mark_Rewrite_Insertion
(Withn
);
3193 Install_Withed_Unit
(Withn
);
3195 if Is_Child_Spec
(P_Unit
) then
3196 Implicit_With_On_Parent
(P_Unit
, N
);
3198 end Implicit_With_On_Parent
;
3204 function In_Chain
(E
: Entity_Id
) return Boolean is
3208 H
:= Current_Entity
(E
);
3209 while Present
(H
) loop
3220 ---------------------
3221 -- Install_Context --
3222 ---------------------
3224 procedure Install_Context
(N
: Node_Id
) is
3225 Lib_Unit
: constant Node_Id
:= Unit
(N
);
3228 Install_Context_Clauses
(N
);
3230 if Is_Child_Spec
(Lib_Unit
) then
3231 Install_Parents
(Lib_Unit
, Private_Present
(Parent
(Lib_Unit
)));
3234 Install_Limited_Context_Clauses
(N
);
3235 end Install_Context
;
3237 -----------------------------
3238 -- Install_Context_Clauses --
3239 -----------------------------
3241 procedure Install_Context_Clauses
(N
: Node_Id
) is
3242 Lib_Unit
: constant Node_Id
:= Unit
(N
);
3244 Uname_Node
: Entity_Id
;
3245 Check_Private
: Boolean := False;
3246 Decl_Node
: Node_Id
;
3247 Lib_Parent
: Entity_Id
;
3250 -- First skip configuration pragmas at the start of the context. They
3251 -- are not technically part of the context clause, but that's where the
3252 -- parser puts them. Note they were analyzed in Analyze_Context.
3254 Item
:= First
(Context_Items
(N
));
3255 while Present
(Item
)
3256 and then Nkind
(Item
) = N_Pragma
3257 and then Pragma_Name
(Item
) in Configuration_Pragma_Names
3262 -- Loop through the actual context clause items. We process everything
3263 -- except Limited_With clauses in this routine. Limited_With clauses
3264 -- are separately installed (see Install_Limited_Context_Clauses).
3266 while Present
(Item
) loop
3268 -- Case of explicit WITH clause
3270 if Nkind
(Item
) = N_With_Clause
3271 and then not Implicit_With
(Item
)
3273 if Limited_Present
(Item
) then
3275 -- Limited withed units will be installed later
3279 -- If Name (Item) is not an entity name, something is wrong, and
3280 -- this will be detected in due course, for now ignore the item
3282 elsif not Is_Entity_Name
(Name
(Item
)) then
3285 elsif No
(Entity
(Name
(Item
))) then
3286 Set_Entity
(Name
(Item
), Any_Id
);
3290 Uname_Node
:= Entity
(Name
(Item
));
3292 if Is_Private_Descendant
(Uname_Node
) then
3293 Check_Private
:= True;
3296 Install_Withed_Unit
(Item
);
3298 Decl_Node
:= Unit_Declaration_Node
(Uname_Node
);
3300 -- If the unit is a subprogram instance, it appears nested within
3301 -- a package that carries the parent information.
3303 if Is_Generic_Instance
(Uname_Node
)
3304 and then Ekind
(Uname_Node
) /= E_Package
3306 Decl_Node
:= Parent
(Parent
(Decl_Node
));
3309 if Is_Child_Spec
(Decl_Node
) then
3310 if Nkind
(Name
(Item
)) = N_Expanded_Name
then
3311 Expand_With_Clause
(Item
, Prefix
(Name
(Item
)), N
);
3313 -- If not an expanded name, the child unit must be a
3314 -- renaming, nothing to do.
3319 elsif Nkind
(Decl_Node
) = N_Subprogram_Body
3320 and then not Acts_As_Spec
(Parent
(Decl_Node
))
3321 and then Is_Child_Spec
(Unit
(Library_Unit
(Parent
(Decl_Node
))))
3323 Implicit_With_On_Parent
3324 (Unit
(Library_Unit
(Parent
(Decl_Node
))), N
);
3327 -- Check license conditions unless this is a dummy unit
3329 if Sloc
(Library_Unit
(Item
)) /= No_Location
then
3330 License_Check
: declare
3331 Withu
: constant Unit_Number_Type
:=
3332 Get_Source_Unit
(Library_Unit
(Item
));
3333 Withl
: constant License_Type
:=
3334 License
(Source_Index
(Withu
));
3335 Unitl
: constant License_Type
:=
3336 License
(Source_Index
(Current_Sem_Unit
));
3338 procedure License_Error
;
3339 -- Signal error of bad license
3345 procedure License_Error
is
3348 ("license of withed unit & may be inconsistent??",
3352 -- Start of processing for License_Check
3355 -- Exclude license check if withed unit is an internal unit.
3356 -- This situation arises e.g. with the GPL version of GNAT.
3358 if Is_Internal_File_Name
(Unit_File_Name
(Withu
)) then
3361 -- Otherwise check various cases
3373 if Withl
= Restricted
then
3377 when Modified_GPL
=>
3378 if Withl
= Restricted
or else Withl
= GPL
then
3382 when Unrestricted
=>
3389 -- Case of USE PACKAGE clause
3391 elsif Nkind
(Item
) = N_Use_Package_Clause
then
3392 Analyze_Use_Package
(Item
);
3394 -- Case of USE TYPE clause
3396 elsif Nkind
(Item
) = N_Use_Type_Clause
then
3397 Analyze_Use_Type
(Item
);
3401 elsif Nkind
(Item
) = N_Pragma
then
3409 if Is_Child_Spec
(Lib_Unit
) then
3411 -- The unit also has implicit with_clauses on its own parents
3413 if No
(Context_Items
(N
)) then
3414 Set_Context_Items
(N
, New_List
);
3417 Implicit_With_On_Parent
(Lib_Unit
, N
);
3420 -- If the unit is a body, the context of the specification must also
3421 -- be installed. That includes private with_clauses in that context.
3423 if Nkind
(Lib_Unit
) = N_Package_Body
3424 or else (Nkind
(Lib_Unit
) = N_Subprogram_Body
3425 and then not Acts_As_Spec
(N
))
3427 Install_Context
(Library_Unit
(N
));
3429 -- Only install private with-clauses of a spec that comes from
3430 -- source, excluding specs created for a subprogram body that is
3433 if Comes_From_Source
(Library_Unit
(N
)) then
3434 Install_Private_With_Clauses
3435 (Defining_Entity
(Unit
(Library_Unit
(N
))));
3438 if Is_Child_Spec
(Unit
(Library_Unit
(N
))) then
3440 -- If the unit is the body of a public child unit, the private
3441 -- declarations of the parent must be made visible. If the child
3442 -- unit is private, the private declarations have been installed
3443 -- already in the call to Install_Parents for the spec. Installing
3444 -- private declarations must be done for all ancestors of public
3445 -- child units. In addition, sibling units mentioned in the
3446 -- context clause of the body are directly visible.
3454 Lib_Spec
:= Unit
(Library_Unit
(N
));
3455 while Is_Child_Spec
(Lib_Spec
) loop
3456 P
:= Unit
(Parent_Spec
(Lib_Spec
));
3457 P_Name
:= Defining_Entity
(P
);
3459 if not (Private_Present
(Parent
(Lib_Spec
)))
3460 and then not In_Private_Part
(P_Name
)
3462 Install_Private_Declarations
(P_Name
);
3463 Install_Private_With_Clauses
(P_Name
);
3464 Set_Use
(Private_Declarations
(Specification
(P
)));
3472 -- For a package body, children in context are immediately visible
3474 Install_Siblings
(Defining_Entity
(Unit
(Library_Unit
(N
))), N
);
3477 if Nkind_In
(Lib_Unit
, N_Generic_Package_Declaration
,
3478 N_Generic_Subprogram_Declaration
,
3479 N_Package_Declaration
,
3480 N_Subprogram_Declaration
)
3482 if Is_Child_Spec
(Lib_Unit
) then
3483 Lib_Parent
:= Defining_Entity
(Unit
(Parent_Spec
(Lib_Unit
)));
3484 Set_Is_Private_Descendant
3485 (Defining_Entity
(Lib_Unit
),
3486 Is_Private_Descendant
(Lib_Parent
)
3487 or else Private_Present
(Parent
(Lib_Unit
)));
3490 Set_Is_Private_Descendant
3491 (Defining_Entity
(Lib_Unit
),
3492 Private_Present
(Parent
(Lib_Unit
)));
3496 if Check_Private
then
3497 Check_Private_Child_Unit
(N
);
3499 end Install_Context_Clauses
;
3501 -------------------------------------
3502 -- Install_Limited_Context_Clauses --
3503 -------------------------------------
3505 procedure Install_Limited_Context_Clauses
(N
: Node_Id
) is
3508 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
);
3509 -- Check that the unlimited view of a given compilation_unit is not
3510 -- already visible through "use + renamings".
3512 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
);
3513 -- Check that if a limited_with clause of a given compilation_unit
3514 -- mentions a descendant of a private child of some library unit, then
3515 -- the given compilation_unit shall be the declaration of a private
3516 -- descendant of that library unit, or a public descendant of such. The
3517 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3518 -- use entities on the limited with_clauses because their units have not
3519 -- been analyzed, so we have to climb the tree of ancestors looking for
3520 -- private keywords.
3522 procedure Expand_Limited_With_Clause
3523 (Comp_Unit
: Node_Id
;
3526 -- If a child unit appears in a limited_with clause, there are implicit
3527 -- limited_with clauses on all parents that are not already visible
3528 -- through a regular with clause. This procedure creates the implicit
3529 -- limited with_clauses for the parents and loads the corresponding
3530 -- units. The shadow entities are created when the inserted clause is
3531 -- analyzed. Implements Ada 2005 (AI-50217).
3533 ---------------------
3534 -- Check_Renamings --
3535 ---------------------
3537 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
) is
3546 pragma Assert
(Nkind
(W
) = N_With_Clause
);
3548 -- Protect the frontend against previous critical errors
3550 case Nkind
(Unit
(Library_Unit
(W
))) is
3551 when N_Subprogram_Declaration |
3552 N_Package_Declaration |
3553 N_Generic_Subprogram_Declaration |
3554 N_Generic_Package_Declaration
=>
3561 -- Check "use + renamings"
3563 WEnt
:= Defining_Unit_Name
(Specification
(Unit
(Library_Unit
(W
))));
3564 Spec
:= Specification
(Unit
(P
));
3566 Item
:= First
(Visible_Declarations
(Spec
));
3567 while Present
(Item
) loop
3569 -- Look only at use package clauses
3571 if Nkind
(Item
) = N_Use_Package_Clause
then
3573 -- Traverse the list of packages
3575 Nam
:= First
(Names
(Item
));
3576 while Present
(Nam
) loop
3579 pragma Assert
(Present
(Parent
(E
)));
3581 if Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
3582 and then Renamed_Entity
(E
) = WEnt
3584 -- The unlimited view is visible through use clause and
3585 -- renamings. There is no need to generate the error
3586 -- message here because Is_Visible_Through_Renamings
3587 -- takes care of generating the precise error message.
3591 elsif Nkind
(Parent
(E
)) = N_Package_Specification
then
3593 -- The use clause may refer to a local package.
3594 -- Check all the enclosing scopes.
3597 while E2
/= Standard_Standard
3605 ("unlimited view visible through use clause ", W
);
3617 -- Recursive call to check all the ancestors
3619 if Is_Child_Spec
(Unit
(P
)) then
3620 Check_Renamings
(P
=> Parent_Spec
(Unit
(P
)), W
=> W
);
3622 end Check_Renamings
;
3624 ---------------------------------------
3625 -- Check_Private_Limited_Withed_Unit --
3626 ---------------------------------------
3628 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
) is
3629 Curr_Parent
: Node_Id
;
3630 Child_Parent
: Node_Id
;
3631 Curr_Private
: Boolean;
3634 -- Compilation unit of the parent of the withed library unit
3636 Child_Parent
:= Library_Unit
(Item
);
3638 -- If the child unit is a public child, then locate its nearest
3639 -- private ancestor, if any, then Child_Parent will then be set to
3640 -- the parent of that ancestor.
3642 if not Private_Present
(Library_Unit
(Item
)) then
3643 while Present
(Child_Parent
)
3644 and then not Private_Present
(Child_Parent
)
3646 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
3649 if No
(Child_Parent
) then
3654 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
3656 -- Traverse all the ancestors of the current compilation unit to
3657 -- check if it is a descendant of named library unit.
3659 Curr_Parent
:= Parent
(Item
);
3660 Curr_Private
:= Private_Present
(Curr_Parent
);
3662 while Present
(Parent_Spec
(Unit
(Curr_Parent
)))
3663 and then Curr_Parent
/= Child_Parent
3665 Curr_Parent
:= Parent_Spec
(Unit
(Curr_Parent
));
3666 Curr_Private
:= Curr_Private
or else Private_Present
(Curr_Parent
);
3669 if Curr_Parent
/= Child_Parent
then
3671 ("unit in with clause is private child unit!", Item
);
3673 ("\current unit must also have parent&!",
3674 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
3676 elsif Private_Present
(Parent
(Item
))
3677 or else Curr_Private
3678 or else Private_Present
(Item
)
3679 or else Nkind_In
(Unit
(Parent
(Item
)), N_Package_Body
,
3683 -- Current unit is private, of descendant of a private unit
3689 ("current unit must also be private descendant of&",
3690 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
3692 end Check_Private_Limited_Withed_Unit
;
3694 --------------------------------
3695 -- Expand_Limited_With_Clause --
3696 --------------------------------
3698 procedure Expand_Limited_With_Clause
3699 (Comp_Unit
: Node_Id
;
3703 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
3704 Unum
: Unit_Number_Type
;
3707 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean;
3708 -- Returns true if the context already includes a with_clause for
3709 -- this unit. If the with_clause is non-limited, the unit is fully
3710 -- visible and an implicit limited_with should not be created. If
3711 -- there is already a limited_with clause for W, a second one is
3712 -- simply redundant.
3714 --------------------------
3715 -- Previous_Withed_Unit --
3716 --------------------------
3718 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean is
3722 -- A limited with_clause cannot appear in the same context_clause
3723 -- as a nonlimited with_clause which mentions the same library.
3725 Item
:= First
(Context_Items
(Comp_Unit
));
3726 while Present
(Item
) loop
3727 if Nkind
(Item
) = N_With_Clause
3728 and then Library_Unit
(Item
) = Library_Unit
(W
)
3737 end Previous_Withed_Unit
;
3739 -- Start of processing for Expand_Limited_With_Clause
3742 if Nkind
(Nam
) = N_Identifier
then
3744 -- Create node for name of withed unit
3747 Make_With_Clause
(Loc
,
3748 Name
=> New_Copy
(Nam
));
3750 else pragma Assert
(Nkind
(Nam
) = N_Selected_Component
);
3752 Make_With_Clause
(Loc
,
3753 Name
=> Make_Selected_Component
(Loc
,
3754 Prefix
=> New_Copy_Tree
(Prefix
(Nam
)),
3755 Selector_Name
=> New_Copy
(Selector_Name
(Nam
))));
3756 Set_Parent
(Withn
, Parent
(N
));
3759 Set_Limited_Present
(Withn
);
3760 Set_First_Name
(Withn
);
3761 Set_Implicit_With
(Withn
);
3765 (Load_Name
=> Get_Spec_Name
(Get_Unit_Name
(Nam
)),
3770 -- Do not generate a limited_with_clause on the current unit. This
3771 -- path is taken when a unit has a limited_with clause on one of its
3774 if Unum
= Current_Sem_Unit
then
3778 Set_Library_Unit
(Withn
, Cunit
(Unum
));
3779 Set_Corresponding_Spec
3780 (Withn
, Specification
(Unit
(Cunit
(Unum
))));
3782 if not Previous_Withed_Unit
(Withn
) then
3783 Prepend
(Withn
, Context_Items
(Parent
(N
)));
3784 Mark_Rewrite_Insertion
(Withn
);
3786 -- Add implicit limited_with_clauses for parents of child units
3787 -- mentioned in limited_with clauses.
3789 if Nkind
(Nam
) = N_Selected_Component
then
3790 Expand_Limited_With_Clause
(Comp_Unit
, Prefix
(Nam
), N
);
3795 if not Limited_View_Installed
(Withn
) then
3796 Install_Limited_Withed_Unit
(Withn
);
3799 end Expand_Limited_With_Clause
;
3801 -- Start of processing for Install_Limited_Context_Clauses
3804 Item
:= First
(Context_Items
(N
));
3805 while Present
(Item
) loop
3806 if Nkind
(Item
) = N_With_Clause
3807 and then Limited_Present
(Item
)
3808 and then not Error_Posted
(Item
)
3810 if Nkind
(Name
(Item
)) = N_Selected_Component
then
3811 Expand_Limited_With_Clause
3812 (Comp_Unit
=> N
, Nam
=> Prefix
(Name
(Item
)), N
=> Item
);
3815 Check_Private_Limited_Withed_Unit
(Item
);
3817 if not Implicit_With
(Item
)
3818 and then Is_Child_Spec
(Unit
(N
))
3820 Check_Renamings
(Parent_Spec
(Unit
(N
)), Item
);
3823 -- A unit may have a limited with on itself if it has a limited
3824 -- with_clause on one of its child units. In that case it is
3825 -- already being compiled and it makes no sense to install its
3828 -- If the item is a limited_private_with_clause, install it if the
3829 -- current unit is a body or if it is a private child. Otherwise
3830 -- the private clause is installed before analyzing the private
3831 -- part of the current unit.
3833 if Library_Unit
(Item
) /= Cunit
(Current_Sem_Unit
)
3834 and then not Limited_View_Installed
(Item
)
3836 not Is_Ancestor_Unit
3837 (Library_Unit
(Item
), Cunit
(Current_Sem_Unit
))
3839 if not Private_Present
(Item
)
3840 or else Private_Present
(N
)
3841 or else Nkind_In
(Unit
(N
), N_Package_Body
,
3845 Install_Limited_Withed_Unit
(Item
);
3853 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
3854 -- looking for incomplete subtype declarations of incomplete types
3855 -- visible through a limited with clause.
3857 if Ada_Version
>= Ada_2005
3858 and then Analyzed
(N
)
3859 and then Nkind
(Unit
(N
)) = N_Package_Declaration
3864 Non_Lim_View
: Entity_Id
;
3867 Decl
:= First
(Visible_Declarations
(Specification
(Unit
(N
))));
3868 while Present
(Decl
) loop
3869 if Nkind
(Decl
) = N_Subtype_Declaration
3871 Ekind
(Defining_Identifier
(Decl
)) = E_Incomplete_Subtype
3873 From_With_Type
(Defining_Identifier
(Decl
))
3875 Def_Id
:= Defining_Identifier
(Decl
);
3876 Non_Lim_View
:= Non_Limited_View
(Def_Id
);
3878 if not Is_Incomplete_Type
(Non_Lim_View
) then
3880 -- Convert an incomplete subtype declaration into a
3881 -- corresponding non-limited view subtype declaration.
3882 -- This is usually the case when analyzing a body that
3883 -- has regular with clauses, when the spec has limited
3886 -- If the non-limited view is still incomplete, it is
3887 -- the dummy entry already created, and the declaration
3888 -- cannot be reanalyzed. This is the case when installing
3889 -- a parent unit that has limited with-clauses.
3891 Set_Subtype_Indication
(Decl
,
3892 New_Reference_To
(Non_Lim_View
, Sloc
(Def_Id
)));
3893 Set_Etype
(Def_Id
, Non_Lim_View
);
3894 Set_Ekind
(Def_Id
, Subtype_Kind
(Ekind
(Non_Lim_View
)));
3895 Set_Analyzed
(Decl
, False);
3897 -- Reanalyze the declaration, suppressing the call to
3898 -- Enter_Name to avoid duplicate names.
3900 Analyze_Subtype_Declaration
3910 end Install_Limited_Context_Clauses
;
3912 ---------------------
3913 -- Install_Parents --
3914 ---------------------
3916 procedure Install_Parents
(Lib_Unit
: Node_Id
; Is_Private
: Boolean) is
3923 P
:= Unit
(Parent_Spec
(Lib_Unit
));
3924 P_Name
:= Get_Parent_Entity
(P
);
3926 if Etype
(P_Name
) = Any_Type
then
3930 if Ekind
(P_Name
) = E_Generic_Package
3931 and then not Nkind_In
(Lib_Unit
, N_Generic_Subprogram_Declaration
,
3932 N_Generic_Package_Declaration
)
3933 and then Nkind
(Lib_Unit
) not in N_Generic_Renaming_Declaration
3936 ("child of a generic package must be a generic unit", Lib_Unit
);
3938 elsif not Is_Package_Or_Generic_Package
(P_Name
) then
3940 ("parent unit must be package or generic package", Lib_Unit
);
3941 raise Unrecoverable_Error
;
3943 elsif Present
(Renamed_Object
(P_Name
)) then
3944 Error_Msg_N
("parent unit cannot be a renaming", Lib_Unit
);
3945 raise Unrecoverable_Error
;
3947 -- Verify that a child of an instance is itself an instance, or the
3948 -- renaming of one. Given that an instance that is a unit is replaced
3949 -- with a package declaration, check against the original node. The
3950 -- parent may be currently being instantiated, in which case it appears
3951 -- as a declaration, but the generic_parent is already established
3952 -- indicating that we deal with an instance.
3954 elsif Nkind
(Original_Node
(P
)) = N_Package_Instantiation
then
3955 if Nkind
(Lib_Unit
) in N_Renaming_Declaration
3956 or else Nkind
(Original_Node
(Lib_Unit
)) in N_Generic_Instantiation
3958 (Nkind
(Lib_Unit
) = N_Package_Declaration
3959 and then Present
(Generic_Parent
(Specification
(Lib_Unit
))))
3964 ("child of an instance must be an instance or renaming",
3969 -- This is the recursive call that ensures all parents are loaded
3971 if Is_Child_Spec
(P
) then
3973 Is_Private
or else Private_Present
(Parent
(Lib_Unit
)));
3976 -- Now we can install the context for this parent
3978 Install_Context_Clauses
(Parent_Spec
(Lib_Unit
));
3979 Install_Limited_Context_Clauses
(Parent_Spec
(Lib_Unit
));
3980 Install_Siblings
(P_Name
, Parent
(Lib_Unit
));
3982 -- The child unit is in the declarative region of the parent. The parent
3983 -- must therefore appear in the scope stack and be visible, as when
3984 -- compiling the corresponding body. If the child unit is private or it
3985 -- is a package body, private declarations must be accessible as well.
3986 -- Use declarations in the parent must also be installed. Finally, other
3987 -- child units of the same parent that are in the context are
3988 -- immediately visible.
3990 -- Find entity for compilation unit, and set its private descendant
3991 -- status as needed. Indicate that it is a compilation unit, which is
3992 -- redundant in general, but needed if this is a generated child spec
3993 -- for a child body without previous spec.
3995 E_Name
:= Defining_Entity
(Lib_Unit
);
3997 Set_Is_Child_Unit
(E_Name
);
3998 Set_Is_Compilation_Unit
(E_Name
);
4000 Set_Is_Private_Descendant
(E_Name
,
4001 Is_Private_Descendant
(P_Name
)
4002 or else Private_Present
(Parent
(Lib_Unit
)));
4004 P_Spec
:= Specification
(Unit_Declaration_Node
(P_Name
));
4005 Push_Scope
(P_Name
);
4007 -- Save current visibility of unit
4009 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
:=
4010 Is_Immediately_Visible
(P_Name
);
4011 Set_Is_Immediately_Visible
(P_Name
);
4012 Install_Visible_Declarations
(P_Name
);
4013 Set_Use
(Visible_Declarations
(P_Spec
));
4015 -- If the parent is a generic unit, its formal part may contain formal
4016 -- packages and use clauses for them.
4018 if Ekind
(P_Name
) = E_Generic_Package
then
4019 Set_Use
(Generic_Formal_Declarations
(Parent
(P_Spec
)));
4023 or else Private_Present
(Parent
(Lib_Unit
))
4025 Install_Private_Declarations
(P_Name
);
4026 Install_Private_With_Clauses
(P_Name
);
4027 Set_Use
(Private_Declarations
(P_Spec
));
4029 end Install_Parents
;
4031 ----------------------------------
4032 -- Install_Private_With_Clauses --
4033 ----------------------------------
4035 procedure Install_Private_With_Clauses
(P
: Entity_Id
) is
4036 Decl
: constant Node_Id
:= Unit_Declaration_Node
(P
);
4040 if Debug_Flag_I
then
4041 Write_Str
("install private with clauses of ");
4042 Write_Name
(Chars
(P
));
4046 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
4047 Item
:= First
(Context_Items
(Parent
(Decl
)));
4048 while Present
(Item
) loop
4049 if Nkind
(Item
) = N_With_Clause
4050 and then Private_Present
(Item
)
4052 -- If the unit is an ancestor of the current one, it is the
4053 -- case of a private limited with clause on a child unit, and
4054 -- the compilation of one of its descendants, In that case the
4055 -- limited view is errelevant.
4057 if Limited_Present
(Item
) then
4058 if not Limited_View_Installed
(Item
)
4060 not Is_Ancestor_Unit
(Library_Unit
(Item
),
4061 Cunit
(Current_Sem_Unit
))
4063 Install_Limited_Withed_Unit
(Item
);
4066 Install_Withed_Unit
(Item
, Private_With_OK
=> True);
4073 end Install_Private_With_Clauses
;
4075 ----------------------
4076 -- Install_Siblings --
4077 ----------------------
4079 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
) is
4085 -- Iterate over explicit with clauses, and check whether the scope of
4086 -- each entity is an ancestor of the current unit, in which case it is
4087 -- immediately visible.
4089 Item
:= First
(Context_Items
(N
));
4090 while Present
(Item
) loop
4092 -- Do not install private_with_clauses declaration, unless unit
4093 -- is itself a private child unit, or is a body. Note that for a
4094 -- subprogram body the private_with_clause does not take effect until
4095 -- after the specification.
4097 if Nkind
(Item
) /= N_With_Clause
4098 or else Implicit_With
(Item
)
4099 or else Limited_Present
(Item
)
4100 or else Error_Posted
(Item
)
4104 elsif not Private_Present
(Item
)
4105 or else Private_Present
(N
)
4106 or else Nkind
(Unit
(N
)) = N_Package_Body
4108 Id
:= Entity
(Name
(Item
));
4110 if Is_Child_Unit
(Id
)
4111 and then Is_Ancestor_Package
(Scope
(Id
), U_Name
)
4113 Set_Is_Immediately_Visible
(Id
);
4115 -- Check for the presence of another unit in the context that
4116 -- may be inadvertently hidden by the child.
4118 Prev
:= Current_Entity
(Id
);
4121 and then Is_Immediately_Visible
(Prev
)
4122 and then not Is_Child_Unit
(Prev
)
4128 Clause
:= First
(Context_Items
(N
));
4129 while Present
(Clause
) loop
4130 if Nkind
(Clause
) = N_With_Clause
4131 and then Entity
(Name
(Clause
)) = Prev
4134 ("child unit& hides compilation unit " &
4135 "with the same name??",
4145 -- The With_Clause may be on a grand-child or one of its further
4146 -- descendants, which makes a child immediately visible. Examine
4147 -- ancestry to determine whether such a child exists. For example,
4148 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4149 -- is immediately visible.
4151 elsif Is_Child_Unit
(Id
) then
4157 while Is_Child_Unit
(Par
) loop
4158 if Is_Ancestor_Package
(Scope
(Par
), U_Name
) then
4159 Set_Is_Immediately_Visible
(Par
);
4168 -- If the item is a private with-clause on a child unit, the parent
4169 -- may have been installed already, but the child unit must remain
4170 -- invisible until installed in a private part or body, unless there
4171 -- is already a regular with_clause for it in the current unit.
4173 elsif Private_Present
(Item
) then
4174 Id
:= Entity
(Name
(Item
));
4176 if Is_Child_Unit
(Id
) then
4180 function In_Context
return Boolean;
4181 -- Scan context of current unit, to check whether there is
4182 -- a with_clause on the same unit as a private with-clause
4183 -- on a parent, in which case child unit is visible. If the
4184 -- unit is a grand-child, the same applies to its parent.
4190 function In_Context
return Boolean is
4193 First
(Context_Items
(Cunit
(Current_Sem_Unit
)));
4194 while Present
(Clause
) loop
4195 if Nkind
(Clause
) = N_With_Clause
4196 and then Comes_From_Source
(Clause
)
4197 and then Is_Entity_Name
(Name
(Clause
))
4198 and then not Private_Present
(Clause
)
4200 if Entity
(Name
(Clause
)) = Id
4202 (Nkind
(Name
(Clause
)) = N_Expanded_Name
4203 and then Entity
(Prefix
(Name
(Clause
))) = Id
)
4216 Set_Is_Visible_Lib_Unit
(Id
, In_Context
);
4223 end Install_Siblings
;
4225 ---------------------------------
4226 -- Install_Limited_Withed_Unit --
4227 ---------------------------------
4229 procedure Install_Limited_Withed_Unit
(N
: Node_Id
) is
4230 P_Unit
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
4233 Is_Child_Package
: Boolean := False;
4234 Lim_Header
: Entity_Id
;
4235 Lim_Typ
: Entity_Id
;
4237 procedure Check_Body_Required
;
4238 -- A unit mentioned in a limited with_clause may not be mentioned in
4239 -- a regular with_clause, but must still be included in the current
4240 -- partition. We need to determine whether the unit needs a body, so
4241 -- that the binder can determine the name of the file to be compiled.
4242 -- Checking whether a unit needs a body can be done without semantic
4243 -- analysis, by examining the nature of the declarations in the package.
4245 function Has_Limited_With_Clause
4246 (C_Unit
: Entity_Id
;
4247 Pack
: Entity_Id
) return Boolean;
4248 -- Determine whether any package in the ancestor chain starting with
4249 -- C_Unit has a limited with clause for package Pack.
4251 function Is_Visible_Through_Renamings
(P
: Entity_Id
) return Boolean;
4252 -- Check if some package installed though normal with-clauses has a
4253 -- renaming declaration of package P. AARM 10.1.2(21/2).
4255 -------------------------
4256 -- Check_Body_Required --
4257 -------------------------
4259 procedure Check_Body_Required
is
4260 PA
: constant List_Id
:=
4261 Pragmas_After
(Aux_Decls_Node
(Parent
(P_Unit
)));
4263 procedure Check_Declarations
(Spec
: Node_Id
);
4264 -- Recursive procedure that does the work and checks nested packages
4266 ------------------------
4267 -- Check_Declarations --
4268 ------------------------
4270 procedure Check_Declarations
(Spec
: Node_Id
) is
4272 Incomplete_Decls
: constant Elist_Id
:= New_Elmt_List
;
4274 Subp_List
: constant Elist_Id
:= New_Elmt_List
;
4276 procedure Check_Pragma_Import
(P
: Node_Id
);
4277 -- If a pragma import applies to a previous subprogram, the
4278 -- enclosing unit may not need a body. The processing is syntactic
4279 -- and does not require a declaration to be analyzed. The code
4280 -- below also handles pragma Import when applied to a subprogram
4281 -- that renames another. In this case the pragma applies to the
4284 -- Chains of multiple renames are not handled by the code below.
4285 -- It is probably impossible to handle all cases without proper
4286 -- name resolution. In such cases the algorithm is conservative
4287 -- and will indicate that a body is needed???
4289 -------------------------
4290 -- Check_Pragma_Import --
4291 -------------------------
4293 procedure Check_Pragma_Import
(P
: Node_Id
) is
4299 procedure Remove_Homonyms
(E
: Node_Id
);
4300 -- Make one pass over list of subprograms. Called again if
4301 -- subprogram is a renaming. E is known to be an identifier.
4303 ---------------------
4304 -- Remove_Homonyms --
4305 ---------------------
4307 procedure Remove_Homonyms
(E
: Node_Id
) is
4308 R
: Entity_Id
:= Empty
;
4309 -- Name of renamed entity, if any
4312 Subp_Id
:= First_Elmt
(Subp_List
);
4313 while Present
(Subp_Id
) loop
4314 if Chars
(Node
(Subp_Id
)) = Chars
(E
) then
4315 if Nkind
(Parent
(Parent
(Node
(Subp_Id
))))
4316 /= N_Subprogram_Renaming_Declaration
4319 Next_Elmt
(Subp_Id
);
4320 Remove_Elmt
(Subp_List
, Prev_Id
);
4322 R
:= Name
(Parent
(Parent
(Node
(Subp_Id
))));
4326 Next_Elmt
(Subp_Id
);
4331 if Nkind
(R
) = N_Identifier
then
4332 Remove_Homonyms
(R
);
4334 elsif Nkind
(R
) = N_Selected_Component
then
4335 Remove_Homonyms
(Selector_Name
(R
));
4337 -- Renaming of attribute
4343 end Remove_Homonyms
;
4345 -- Start of processing for Check_Pragma_Import
4348 -- Find name of entity in Import pragma. We have not analyzed
4349 -- the construct, so we must guard against syntax errors.
4351 Arg
:= Next
(First
(Pragma_Argument_Associations
(P
)));
4354 or else Nkind
(Expression
(Arg
)) /= N_Identifier
4358 Imported
:= Expression
(Arg
);
4361 Remove_Homonyms
(Imported
);
4362 end Check_Pragma_Import
;
4364 -- Start of processing for Check_Declarations
4367 -- Search for Elaborate Body pragma
4369 Decl
:= First
(Visible_Declarations
(Spec
));
4370 while Present
(Decl
)
4371 and then Nkind
(Decl
) = N_Pragma
4373 if Get_Pragma_Id
(Decl
) = Pragma_Elaborate_Body
then
4374 Set_Body_Required
(Library_Unit
(N
));
4381 -- Look for declarations that require the presence of a body. We
4382 -- have already skipped pragmas at the start of the list.
4384 while Present
(Decl
) loop
4386 -- Subprogram that comes from source means body may be needed.
4387 -- Save for subsequent examination of import pragmas.
4389 if Comes_From_Source
(Decl
)
4390 and then (Nkind_In
(Decl
, N_Subprogram_Declaration
,
4391 N_Subprogram_Renaming_Declaration
,
4392 N_Generic_Subprogram_Declaration
))
4394 Append_Elmt
(Defining_Entity
(Decl
), Subp_List
);
4396 -- Package declaration of generic package declaration. We need
4397 -- to recursively examine nested declarations.
4399 elsif Nkind_In
(Decl
, N_Package_Declaration
,
4400 N_Generic_Package_Declaration
)
4402 Check_Declarations
(Specification
(Decl
));
4404 elsif Nkind
(Decl
) = N_Pragma
4405 and then Pragma_Name
(Decl
) = Name_Import
4407 Check_Pragma_Import
(Decl
);
4413 -- Same set of tests for private part. In addition to subprograms
4414 -- detect the presence of Taft Amendment types (incomplete types
4415 -- completed in the body).
4417 Decl
:= First
(Private_Declarations
(Spec
));
4418 while Present
(Decl
) loop
4419 if Comes_From_Source
(Decl
)
4420 and then (Nkind_In
(Decl
, N_Subprogram_Declaration
,
4421 N_Subprogram_Renaming_Declaration
,
4422 N_Generic_Subprogram_Declaration
))
4424 Append_Elmt
(Defining_Entity
(Decl
), Subp_List
);
4426 elsif Nkind_In
(Decl
, N_Package_Declaration
,
4427 N_Generic_Package_Declaration
)
4429 Check_Declarations
(Specification
(Decl
));
4431 -- Collect incomplete type declarations for separate pass
4433 elsif Nkind
(Decl
) = N_Incomplete_Type_Declaration
then
4434 Append_Elmt
(Decl
, Incomplete_Decls
);
4436 elsif Nkind
(Decl
) = N_Pragma
4437 and then Pragma_Name
(Decl
) = Name_Import
4439 Check_Pragma_Import
(Decl
);
4445 -- Now check incomplete declarations to locate Taft amendment
4446 -- types. This can be done by examining the defining identifiers
4447 -- of type declarations without real semantic analysis.
4453 Inc
:= First_Elmt
(Incomplete_Decls
);
4454 while Present
(Inc
) loop
4455 Decl
:= Next
(Node
(Inc
));
4456 while Present
(Decl
) loop
4457 if Nkind
(Decl
) = N_Full_Type_Declaration
4458 and then Chars
(Defining_Identifier
(Decl
)) =
4459 Chars
(Defining_Identifier
(Node
(Inc
)))
4467 -- If no completion, this is a TAT, and a body is needed
4470 Set_Body_Required
(Library_Unit
(N
));
4478 -- Finally, check whether there are subprograms that still require
4479 -- a body, i.e. are not renamings or null.
4481 if not Is_Empty_Elmt_List
(Subp_List
) then
4487 Subp_Id
:= First_Elmt
(Subp_List
);
4488 Spec
:= Parent
(Node
(Subp_Id
));
4490 while Present
(Subp_Id
) loop
4491 if Nkind
(Parent
(Spec
))
4492 = N_Subprogram_Renaming_Declaration
4496 elsif Nkind
(Spec
) = N_Procedure_Specification
4497 and then Null_Present
(Spec
)
4502 Set_Body_Required
(Library_Unit
(N
));
4506 Next_Elmt
(Subp_Id
);
4510 end Check_Declarations
;
4512 -- Start of processing for Check_Body_Required
4515 -- If this is an imported package (Java and CIL usage) no body is
4516 -- needed. Scan list of pragmas that may follow a compilation unit
4517 -- to look for a relevant pragma Import.
4519 if Present
(PA
) then
4525 while Present
(Prag
) loop
4526 if Nkind
(Prag
) = N_Pragma
4527 and then Get_Pragma_Id
(Prag
) = Pragma_Import
4537 Check_Declarations
(Specification
(P_Unit
));
4538 end Check_Body_Required
;
4540 -----------------------------
4541 -- Has_Limited_With_Clause --
4542 -----------------------------
4544 function Has_Limited_With_Clause
4545 (C_Unit
: Entity_Id
;
4546 Pack
: Entity_Id
) return Boolean
4553 while Present
(Par
) loop
4554 if Ekind
(Par
) /= E_Package
then
4558 -- Retrieve the Compilation_Unit node for Par and determine if
4559 -- its context clauses contain a limited with for Pack.
4561 Par_Unit
:= Parent
(Parent
(Parent
(Par
)));
4563 if Nkind
(Par_Unit
) = N_Package_Declaration
then
4564 Par_Unit
:= Parent
(Par_Unit
);
4567 if Has_With_Clause
(Par_Unit
, Pack
, True) then
4571 -- If there are more ancestors, climb up the tree, otherwise we
4574 if Is_Child_Unit
(Par
) then
4582 end Has_Limited_With_Clause
;
4584 ----------------------------------
4585 -- Is_Visible_Through_Renamings --
4586 ----------------------------------
4588 function Is_Visible_Through_Renamings
(P
: Entity_Id
) return Boolean is
4589 Kind
: constant Node_Kind
:=
4590 Nkind
(Unit
(Cunit
(Current_Sem_Unit
)));
4596 -- Example of the error detected by this subprogram:
4604 -- package Ren_P renames P;
4610 -- limited with P; -- ERROR
4611 -- package R.C is ...
4613 Aux_Unit
:= Cunit
(Current_Sem_Unit
);
4616 Item
:= First
(Context_Items
(Aux_Unit
));
4617 while Present
(Item
) loop
4618 if Nkind
(Item
) = N_With_Clause
4619 and then not Limited_Present
(Item
)
4620 and then Nkind
(Unit
(Library_Unit
(Item
))) =
4621 N_Package_Declaration
4624 First
(Visible_Declarations
4625 (Specification
(Unit
(Library_Unit
(Item
)))));
4626 while Present
(Decl
) loop
4627 if Nkind
(Decl
) = N_Package_Renaming_Declaration
4628 and then Entity
(Name
(Decl
)) = P
4630 -- Generate the error message only if the current unit
4631 -- is a package declaration; in case of subprogram
4632 -- bodies and package bodies we just return True to
4633 -- indicate that the limited view must not be
4636 if Kind
= N_Package_Declaration
then
4638 ("simultaneous visibility of the limited and " &
4639 "unlimited views not allowed", N
);
4640 Error_Msg_Sloc
:= Sloc
(Item
);
4642 ("\\ unlimited view of & visible through the " &
4643 "context clause #", N
, P
);
4644 Error_Msg_Sloc
:= Sloc
(Decl
);
4645 Error_Msg_NE
("\\ and the renaming #", N
, P
);
4658 -- If it is a body not acting as spec, follow pointer to the
4659 -- corresponding spec, otherwise follow pointer to parent spec.
4661 if Present
(Library_Unit
(Aux_Unit
))
4662 and then Nkind_In
(Unit
(Aux_Unit
),
4663 N_Package_Body
, N_Subprogram_Body
)
4665 if Aux_Unit
= Library_Unit
(Aux_Unit
) then
4667 -- Aux_Unit is a body that acts as a spec. Clause has
4668 -- already been flagged as illegal.
4673 Aux_Unit
:= Library_Unit
(Aux_Unit
);
4677 Aux_Unit
:= Parent_Spec
(Unit
(Aux_Unit
));
4680 exit when No
(Aux_Unit
);
4684 end Is_Visible_Through_Renamings
;
4686 -- Start of processing for Install_Limited_Withed_Unit
4689 pragma Assert
(not Limited_View_Installed
(N
));
4691 -- In case of limited with_clause on subprograms, generics, instances,
4692 -- or renamings, the corresponding error was previously posted and we
4693 -- have nothing to do here. If the file is missing altogether, it has
4694 -- no source location.
4696 if Nkind
(P_Unit
) /= N_Package_Declaration
4697 or else Sloc
(P_Unit
) = No_Location
4702 P
:= Defining_Unit_Name
(Specification
(P_Unit
));
4704 -- Handle child packages
4706 if Nkind
(P
) = N_Defining_Program_Unit_Name
then
4707 Is_Child_Package
:= True;
4708 P
:= Defining_Identifier
(P
);
4711 -- Do not install the limited-view if the context of the unit is already
4712 -- available through a regular with clause.
4714 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4715 and then Has_With_Clause
(Cunit
(Current_Sem_Unit
), P
)
4720 -- Do not install the limited-view if the full-view is already visible
4721 -- through renaming declarations.
4723 if Is_Visible_Through_Renamings
(P
) then
4727 -- Do not install the limited view if this is the unit being analyzed.
4728 -- This unusual case will happen when a unit has a limited_with clause
4729 -- on one of its children. The compilation of the child forces the load
4730 -- of the parent which tries to install the limited view of the child
4731 -- again. Installing the limited view must also be disabled when
4732 -- compiling the body of the child unit.
4734 if P
= Cunit_Entity
(Current_Sem_Unit
)
4735 or else (Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4736 and then P
= Main_Unit_Entity
4737 and then Is_Ancestor_Unit
4738 (Cunit
(Main_Unit
), Cunit
(Current_Sem_Unit
)))
4743 -- This scenario is similar to the one above, the difference is that the
4744 -- compilation of sibling Par.Sib forces the load of parent Par which
4745 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4746 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4747 -- non-limited views of all entities from Lim_Pack.
4749 -- limited with Lim_Pack; -- [1]
4750 -- package Par is ... package Lim_Pack is ...
4752 -- with Lim_Pack; -- [2]
4753 -- package Par.Sib is ... package body Par.Sib is ...
4755 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4756 -- Sem_Unit is the body of Par.Sib.
4758 if Ekind
(P
) = E_Package
4759 and then Ekind
(Main_Unit_Entity
) = E_Package
4760 and then Is_Child_Unit
(Main_Unit_Entity
)
4762 -- The body has a regular with clause
4764 and then Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4765 and then Has_With_Clause
(Cunit
(Current_Sem_Unit
), P
)
4767 -- One of the ancestors has a limited with clause
4769 and then Nkind
(Parent
(Parent
(Main_Unit_Entity
))) =
4770 N_Package_Specification
4771 and then Has_Limited_With_Clause
(Scope
(Main_Unit_Entity
), P
)
4776 -- A common use of the limited-with is to have a limited-with in the
4777 -- package spec, and a normal with in its package body. For example:
4779 -- limited with X; -- [1]
4783 -- package body A is ...
4785 -- The compilation of A's body installs the context clauses found at [2]
4786 -- and then the context clauses of its specification (found at [1]). As
4787 -- a consequence, at [1] the specification of X has been analyzed and it
4788 -- is immediately visible. According to the semantics of limited-with
4789 -- context clauses we don't install the limited view because the full
4790 -- view of X supersedes its limited view.
4792 if Analyzed
(P_Unit
)
4794 (Is_Immediately_Visible
(P
)
4795 or else (Is_Child_Package
and then Is_Visible_Lib_Unit
(P
)))
4798 -- The presence of both the limited and the analyzed nonlimited view
4799 -- may also be an error, such as an illegal context for a limited
4800 -- with_clause. In that case, do not process the context item at all.
4802 if Error_Posted
(N
) then
4806 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
then
4810 Item
:= First
(Context_Items
(Cunit
(Current_Sem_Unit
)));
4811 while Present
(Item
) loop
4812 if Nkind
(Item
) = N_With_Clause
4813 and then Comes_From_Source
(Item
)
4814 and then Entity
(Name
(Item
)) = P
4823 -- If this is a child body, assume that the nonlimited with_clause
4824 -- appears in an ancestor. Could be refined ???
4828 (Unit
(Library_Unit
(Cunit
(Current_Sem_Unit
)))))
4835 -- If in package declaration, nonlimited view brought in from
4836 -- parent unit or some error condition.
4842 if Debug_Flag_I
then
4843 Write_Str
("install limited view of ");
4844 Write_Name
(Chars
(P
));
4848 -- If the unit has not been analyzed and the limited view has not been
4849 -- already installed then we install it.
4851 if not Analyzed
(P_Unit
) then
4852 if not In_Chain
(P
) then
4854 -- Minimum decoration
4856 Set_Ekind
(P
, E_Package
);
4857 Set_Etype
(P
, Standard_Void_Type
);
4858 Set_Scope
(P
, Standard_Standard
);
4859 Set_Is_Visible_Lib_Unit
(P
);
4861 if Is_Child_Package
then
4862 Set_Is_Child_Unit
(P
);
4863 Set_Scope
(P
, Defining_Entity
(Unit
(Parent_Spec
(P_Unit
))));
4866 -- Place entity on visibility structure
4868 Set_Homonym
(P
, Current_Entity
(P
));
4869 Set_Current_Entity
(P
);
4871 if Debug_Flag_I
then
4872 Write_Str
(" (homonym) chain ");
4873 Write_Name
(Chars
(P
));
4877 -- Install the incomplete view. The first element of the limited
4878 -- view is a header (an E_Package entity) used to reference the
4879 -- first shadow entity in the private part of the package.
4881 Lim_Header
:= Limited_View
(P
);
4882 Lim_Typ
:= First_Entity
(Lim_Header
);
4884 while Present
(Lim_Typ
)
4885 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
4887 Set_Homonym
(Lim_Typ
, Current_Entity
(Lim_Typ
));
4888 Set_Current_Entity
(Lim_Typ
);
4890 if Debug_Flag_I
then
4891 Write_Str
(" (homonym) chain ");
4892 Write_Name
(Chars
(Lim_Typ
));
4896 Next_Entity
(Lim_Typ
);
4900 -- If the unit appears in a previous regular with_clause, the regular
4901 -- entities of the public part of the withed package must be replaced
4902 -- by the shadow ones.
4904 -- This code must be kept synchronized with the code that replaces the
4905 -- shadow entities by the real entities (see body of Remove_Limited
4906 -- With_Clause); otherwise the contents of the homonym chains are not
4910 -- Hide all the type entities of the public part of the package to
4911 -- avoid its usage. This is needed to cover all the subtype decla-
4912 -- rations because we do not remove them from the homonym chain.
4914 E
:= First_Entity
(P
);
4915 while Present
(E
) and then E
/= First_Private_Entity
(P
) loop
4917 Set_Was_Hidden
(E
, Is_Hidden
(E
));
4924 -- Replace the real entities by the shadow entities of the limited
4925 -- view. The first element of the limited view is a header that is
4926 -- used to reference the first shadow entity in the private part
4927 -- of the package. Successive elements are the limited views of the
4928 -- type (including regular incomplete types) declared in the package.
4930 Lim_Header
:= Limited_View
(P
);
4932 Lim_Typ
:= First_Entity
(Lim_Header
);
4933 while Present
(Lim_Typ
)
4934 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
4936 pragma Assert
(not In_Chain
(Lim_Typ
));
4938 -- Do not unchain nested packages and child units
4940 if Ekind
(Lim_Typ
) /= E_Package
4941 and then not Is_Child_Unit
(Lim_Typ
)
4947 Prev
:= Current_Entity
(Lim_Typ
);
4950 -- Replace E in the homonyms list, so that the limited view
4951 -- becomes available.
4953 if E
= Non_Limited_View
(Lim_Typ
) then
4954 Set_Homonym
(Lim_Typ
, Homonym
(Prev
));
4955 Set_Current_Entity
(Lim_Typ
);
4959 E
:= Homonym
(Prev
);
4961 -- E may have been removed when installing a previous
4962 -- limited_with_clause.
4966 exit when E
= Non_Limited_View
(Lim_Typ
);
4968 Prev
:= Homonym
(Prev
);
4972 Set_Homonym
(Lim_Typ
, Homonym
(Homonym
(Prev
)));
4973 Set_Homonym
(Prev
, Lim_Typ
);
4978 if Debug_Flag_I
then
4979 Write_Str
(" (homonym) chain ");
4980 Write_Name
(Chars
(Lim_Typ
));
4985 Next_Entity
(Lim_Typ
);
4989 -- The package must be visible while the limited-with clause is active
4990 -- because references to the type P.T must resolve in the usual way.
4991 -- In addition, we remember that the limited-view has been installed to
4992 -- uninstall it at the point of context removal.
4994 Set_Is_Immediately_Visible
(P
);
4995 Set_Limited_View_Installed
(N
);
4997 -- If unit has not been analyzed in some previous context, check
4998 -- (imperfectly ???) whether it might need a body.
5000 if not Analyzed
(P_Unit
) then
5001 Check_Body_Required
;
5004 -- If the package in the limited_with clause is a child unit, the clause
5005 -- is unanalyzed and appears as a selected component. Recast it as an
5006 -- expanded name so that the entity can be properly set. Use entity of
5007 -- parent, if available, for higher ancestors in the name.
5009 if Nkind
(Name
(N
)) = N_Selected_Component
then
5017 while Nkind
(Nam
) = N_Selected_Component
5018 and then Present
(Ent
)
5020 Change_Selected_Component_To_Expanded_Name
(Nam
);
5022 -- Set entity of parent identifiers if the unit is a child
5023 -- unit. This ensures that the tree is properly formed from
5024 -- semantic point of view (e.g. for ASIS queries). The unit
5025 -- entities are not fully analyzed, so we need to follow unit
5026 -- links in the tree.
5028 Set_Entity
(Nam
, Ent
);
5030 Nam
:= Prefix
(Nam
);
5033 (Unit
(Parent_Spec
(Unit_Declaration_Node
(Ent
))));
5035 -- Set entity of last ancestor
5037 if Nkind
(Nam
) = N_Identifier
then
5038 Set_Entity
(Nam
, Ent
);
5044 Set_Entity
(Name
(N
), P
);
5045 Set_From_With_Type
(P
);
5046 end Install_Limited_Withed_Unit
;
5048 -------------------------
5049 -- Install_Withed_Unit --
5050 -------------------------
5052 procedure Install_Withed_Unit
5053 (With_Clause
: Node_Id
;
5054 Private_With_OK
: Boolean := False)
5056 Uname
: constant Entity_Id
:= Entity
(Name
(With_Clause
));
5057 P
: constant Entity_Id
:= Scope
(Uname
);
5060 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5061 -- compiling a package declaration and the Private_With_OK flag was not
5062 -- set by the caller. These declarations will be installed later (before
5063 -- analyzing the private part of the package).
5065 if Private_Present
(With_Clause
)
5066 and then Nkind
(Unit
(Parent
(With_Clause
))) = N_Package_Declaration
5067 and then not (Private_With_OK
)
5072 if Debug_Flag_I
then
5073 if Private_Present
(With_Clause
) then
5074 Write_Str
("install private withed unit ");
5076 Write_Str
("install withed unit ");
5079 Write_Name
(Chars
(Uname
));
5083 -- We do not apply the restrictions to an internal unit unless we are
5084 -- compiling the internal unit as a main unit. This check is also
5085 -- skipped for dummy units (for missing packages).
5087 if Sloc
(Uname
) /= No_Location
5088 and then (not Is_Internal_File_Name
(Unit_File_Name
(Current_Sem_Unit
))
5089 or else Current_Sem_Unit
= Main_Unit
)
5091 Check_Restricted_Unit
5092 (Unit_Name
(Get_Source_Unit
(Uname
)), With_Clause
);
5095 if P
/= Standard_Standard
then
5097 -- If the unit is not analyzed after analysis of the with clause and
5098 -- it is an instantiation then it awaits a body and is the main unit.
5099 -- Its appearance in the context of some other unit indicates a
5100 -- circular dependency (DEC suite perversity).
5102 if not Analyzed
(Uname
)
5103 and then Nkind
(Parent
(Uname
)) = N_Package_Instantiation
5106 ("instantiation depends on itself", Name
(With_Clause
));
5108 elsif not Is_Visible_Lib_Unit
(Uname
) then
5110 -- Abandon processing in case of previous errors
5112 if No
(Scope
(Uname
)) then
5113 Check_Error_Detected
;
5117 Set_Is_Visible_Lib_Unit
(Uname
);
5119 -- If the child unit appears in the context of its parent, it is
5120 -- immediately visible.
5122 if In_Open_Scopes
(Scope
(Uname
)) then
5123 Set_Is_Immediately_Visible
(Uname
);
5126 if Is_Generic_Instance
(Uname
)
5127 and then Ekind
(Uname
) in Subprogram_Kind
5129 -- Set flag as well on the visible entity that denotes the
5130 -- instance, which renames the current one.
5132 Set_Is_Visible_Lib_Unit
5134 (Defining_Entity
(Unit
(Library_Unit
(With_Clause
)))));
5137 -- The parent unit may have been installed already, and may have
5138 -- appeared in a use clause.
5140 if In_Use
(Scope
(Uname
)) then
5141 Set_Is_Potentially_Use_Visible
(Uname
);
5144 Set_Context_Installed
(With_Clause
);
5147 elsif not Is_Immediately_Visible
(Uname
) then
5148 Set_Is_Visible_Lib_Unit
(Uname
);
5150 if not Private_Present
(With_Clause
) or else Private_With_OK
then
5151 Set_Is_Immediately_Visible
(Uname
);
5154 Set_Context_Installed
(With_Clause
);
5157 -- A with-clause overrides a with-type clause: there are no restric-
5158 -- tions on the use of package entities.
5160 if Ekind
(Uname
) = E_Package
then
5161 Set_From_With_Type
(Uname
, False);
5164 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5165 -- unit if there is a visible homograph for it declared in the same
5166 -- declarative region. This pathological case can only arise when an
5167 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5168 -- G1 has a generic child also named G2, and the context includes with_
5169 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5170 -- of I1.G2 visible as well. If the child unit is named Standard, do
5171 -- not apply the check to the Standard package itself.
5173 if Is_Child_Unit
(Uname
)
5174 and then Is_Visible_Lib_Unit
(Uname
)
5175 and then Ada_Version
>= Ada_2005
5178 Decl1
: constant Node_Id
:= Unit_Declaration_Node
(P
);
5184 U2
:= Homonym
(Uname
);
5186 and then U2
/= Standard_Standard
5189 Decl2
:= Unit_Declaration_Node
(P2
);
5191 if Is_Child_Unit
(U2
) and then Is_Visible_Lib_Unit
(U2
) then
5192 if Is_Generic_Instance
(P
)
5193 and then Nkind
(Decl1
) = N_Package_Declaration
5194 and then Generic_Parent
(Specification
(Decl1
)) = P2
5196 Error_Msg_N
("illegal with_clause", With_Clause
);
5198 ("\child unit has visible homograph" &
5199 " (RM 8.3(26), 10.1.1(19))",
5203 elsif Is_Generic_Instance
(P2
)
5204 and then Nkind
(Decl2
) = N_Package_Declaration
5205 and then Generic_Parent
(Specification
(Decl2
)) = P
5207 -- With_clause for child unit of instance appears before
5208 -- in the context. We want to place the error message on
5209 -- it, not on the generic child unit itself.
5212 Prev_Clause
: Node_Id
;
5215 Prev_Clause
:= First
(List_Containing
(With_Clause
));
5216 while Entity
(Name
(Prev_Clause
)) /= U2
loop
5220 pragma Assert
(Present
(Prev_Clause
));
5221 Error_Msg_N
("illegal with_clause", Prev_Clause
);
5223 ("\child unit has visible homograph" &
5224 " (RM 8.3(26), 10.1.1(19))",
5235 end Install_Withed_Unit
;
5241 function Is_Child_Spec
(Lib_Unit
: Node_Id
) return Boolean is
5242 K
: constant Node_Kind
:= Nkind
(Lib_Unit
);
5245 return (K
in N_Generic_Declaration
or else
5246 K
in N_Generic_Instantiation
or else
5247 K
in N_Generic_Renaming_Declaration
or else
5248 K
= N_Package_Declaration
or else
5249 K
= N_Package_Renaming_Declaration
or else
5250 K
= N_Subprogram_Declaration
or else
5251 K
= N_Subprogram_Renaming_Declaration
)
5252 and then Present
(Parent_Spec
(Lib_Unit
));
5255 ------------------------------------
5256 -- Is_Legal_Shadow_Entity_In_Body --
5257 ------------------------------------
5259 function Is_Legal_Shadow_Entity_In_Body
(T
: Entity_Id
) return Boolean is
5260 C_Unit
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
5262 return Nkind
(Unit
(C_Unit
)) = N_Package_Body
5265 (C_Unit
, Cunit_Entity
(Get_Source_Unit
(Non_Limited_View
(T
))));
5266 end Is_Legal_Shadow_Entity_In_Body
;
5268 ----------------------
5269 -- Is_Ancestor_Unit --
5270 ----------------------
5272 function Is_Ancestor_Unit
(U1
: Node_Id
; U2
: Node_Id
) return Boolean is
5273 E1
: constant Entity_Id
:= Defining_Entity
(Unit
(U1
));
5276 if Nkind_In
(Unit
(U2
), N_Package_Body
, N_Subprogram_Body
) then
5277 E2
:= Defining_Entity
(Unit
(Library_Unit
(U2
)));
5278 return Is_Ancestor_Package
(E1
, E2
);
5282 end Is_Ancestor_Unit
;
5284 -----------------------
5285 -- Load_Needed_Body --
5286 -----------------------
5288 -- N is a generic unit named in a with clause, or else it is a unit that
5289 -- contains a generic unit or an inlined function. In order to perform an
5290 -- instantiation, the body of the unit must be present. If the unit itself
5291 -- is generic, we assume that an instantiation follows, and load & analyze
5292 -- the body unconditionally. This forces analysis of the spec as well.
5294 -- If the unit is not generic, but contains a generic unit, it is loaded on
5295 -- demand, at the point of instantiation (see ch12).
5297 procedure Load_Needed_Body
5300 Do_Analyze
: Boolean := True)
5302 Body_Name
: Unit_Name_Type
;
5303 Unum
: Unit_Number_Type
;
5305 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
5306 -- The loading and analysis is done with style checks off
5309 if not GNAT_Mode
then
5310 Style_Check
:= False;
5313 Body_Name
:= Get_Body_Name
(Get_Unit_Name
(Unit
(N
)));
5316 (Load_Name
=> Body_Name
,
5322 if Unum
= No_Unit
then
5326 Compiler_State
:= Analyzing
; -- reset after load
5328 if not Fatal_Error
(Unum
) or else Try_Semantics
then
5329 if Debug_Flag_L
then
5330 Write_Str
("*** Loaded generic body");
5335 Semantics
(Cunit
(Unum
));
5342 Style_Check
:= Save_Style_Check
;
5343 end Load_Needed_Body
;
5345 -------------------------
5346 -- Build_Limited_Views --
5347 -------------------------
5349 procedure Build_Limited_Views
(N
: Node_Id
) is
5350 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(Library_Unit
(N
));
5351 P
: constant Entity_Id
:= Cunit_Entity
(Unum
);
5353 Spec
: Node_Id
; -- To denote a package specification
5354 Lim_Typ
: Entity_Id
; -- To denote shadow entities
5355 Comp_Typ
: Entity_Id
; -- To denote real entities
5357 Lim_Header
: Entity_Id
; -- Package entity
5358 Last_Lim_E
: Entity_Id
:= Empty
; -- Last limited entity built
5359 Last_Pub_Lim_E
: Entity_Id
; -- To set the first private entity
5361 procedure Decorate_Incomplete_Type
(E
: Entity_Id
; Scop
: Entity_Id
);
5362 -- Add attributes of an incomplete type to a shadow entity. The same
5363 -- attributes are placed on the real entity, so that gigi receives
5364 -- a consistent view.
5366 procedure Decorate_Package_Specification
(P
: Entity_Id
);
5367 -- Add attributes of a package entity to the entity in a package
5370 procedure Decorate_Tagged_Type
5374 Mark
: Boolean := False);
5375 -- Set basic attributes of tagged type T, including its class-wide type.
5376 -- The parameters Loc, Scope are used to decorate the class-wide type.
5377 -- Use flag Mark to label the class-wide type as Materialize_Entity.
5379 procedure Build_Chain
(Scope
: Entity_Id
; First_Decl
: Node_Id
);
5380 -- Construct list of shadow entities and attach it to entity of
5381 -- package that is mentioned in a limited_with clause.
5383 function New_Internal_Shadow_Entity
5384 (Kind
: Entity_Kind
;
5385 Sloc_Value
: Source_Ptr
;
5386 Id_Char
: Character) return Entity_Id
;
5387 -- Build a new internal entity and append it to the list of shadow
5388 -- entities available through the limited-header
5394 procedure Build_Chain
(Scope
: Entity_Id
; First_Decl
: Node_Id
) is
5395 Analyzed_Unit
: constant Boolean := Analyzed
(Cunit
(Unum
));
5396 Is_Tagged
: Boolean;
5401 while Present
(Decl
) loop
5403 -- For each library_package_declaration in the environment, there
5404 -- is an implicit declaration of a *limited view* of that library
5405 -- package. The limited view of a package contains:
5407 -- * For each nested package_declaration, a declaration of the
5408 -- limited view of that package, with the same defining-
5409 -- program-unit name.
5411 -- * For each type_declaration in the visible part, an incomplete
5412 -- type-declaration with the same defining_identifier, whose
5413 -- completion is the type_declaration. If the type_declaration
5414 -- is tagged, then the incomplete_type_declaration is tagged
5417 -- The partial view is tagged if the declaration has the
5418 -- explicit keyword, or else if it is a type extension, both
5419 -- of which can be ascertained syntactically.
5421 if Nkind
(Decl
) = N_Full_Type_Declaration
then
5423 (Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
5424 and then Tagged_Present
(Type_Definition
(Decl
)))
5426 (Nkind
(Type_Definition
(Decl
)) = N_Derived_Type_Definition
5429 (Record_Extension_Part
(Type_Definition
(Decl
))));
5431 Comp_Typ
:= Defining_Identifier
(Decl
);
5433 if not Analyzed_Unit
then
5435 Decorate_Tagged_Type
(Sloc
(Decl
), Comp_Typ
, Scope
, True);
5437 Decorate_Incomplete_Type
(Comp_Typ
, Scope
);
5441 -- Create shadow entity for type
5444 New_Internal_Shadow_Entity
5445 (Kind
=> Ekind
(Comp_Typ
),
5446 Sloc_Value
=> Sloc
(Comp_Typ
),
5449 Set_Chars
(Lim_Typ
, Chars
(Comp_Typ
));
5450 Set_Parent
(Lim_Typ
, Parent
(Comp_Typ
));
5451 Set_From_With_Type
(Lim_Typ
);
5454 Decorate_Tagged_Type
(Sloc
(Decl
), Lim_Typ
, Scope
);
5456 Decorate_Incomplete_Type
(Lim_Typ
, Scope
);
5459 Set_Non_Limited_View
(Lim_Typ
, Comp_Typ
);
5460 Set_Private_Dependents
(Lim_Typ
, New_Elmt_List
);
5462 elsif Nkind_In
(Decl
, N_Private_Type_Declaration
,
5463 N_Incomplete_Type_Declaration
,
5464 N_Task_Type_Declaration
,
5465 N_Protected_Type_Declaration
)
5467 Comp_Typ
:= Defining_Identifier
(Decl
);
5470 Nkind_In
(Decl
, N_Private_Type_Declaration
,
5471 N_Incomplete_Type_Declaration
)
5472 and then Tagged_Present
(Decl
);
5474 if not Analyzed_Unit
then
5476 Decorate_Tagged_Type
(Sloc
(Decl
), Comp_Typ
, Scope
, True);
5478 Decorate_Incomplete_Type
(Comp_Typ
, Scope
);
5483 New_Internal_Shadow_Entity
5484 (Kind
=> Ekind
(Comp_Typ
),
5485 Sloc_Value
=> Sloc
(Comp_Typ
),
5488 Set_Chars
(Lim_Typ
, Chars
(Comp_Typ
));
5489 Set_Parent
(Lim_Typ
, Parent
(Comp_Typ
));
5490 Set_From_With_Type
(Lim_Typ
);
5493 Decorate_Tagged_Type
(Sloc
(Decl
), Lim_Typ
, Scope
);
5495 Decorate_Incomplete_Type
(Lim_Typ
, Scope
);
5498 Set_Non_Limited_View
(Lim_Typ
, Comp_Typ
);
5500 -- Initialize Private_Depedents, so the field has the proper
5501 -- type, even though the list will remain empty.
5503 Set_Private_Dependents
(Lim_Typ
, New_Elmt_List
);
5505 elsif Nkind
(Decl
) = N_Private_Extension_Declaration
then
5506 Comp_Typ
:= Defining_Identifier
(Decl
);
5508 if not Analyzed_Unit
then
5509 Decorate_Tagged_Type
(Sloc
(Decl
), Comp_Typ
, Scope
, True);
5512 -- Create shadow entity for type
5515 New_Internal_Shadow_Entity
5516 (Kind
=> Ekind
(Comp_Typ
),
5517 Sloc_Value
=> Sloc
(Comp_Typ
),
5520 Set_Chars
(Lim_Typ
, Chars
(Comp_Typ
));
5521 Set_Parent
(Lim_Typ
, Parent
(Comp_Typ
));
5522 Set_From_With_Type
(Lim_Typ
);
5524 Decorate_Tagged_Type
(Sloc
(Decl
), Lim_Typ
, Scope
);
5525 Set_Non_Limited_View
(Lim_Typ
, Comp_Typ
);
5527 elsif Nkind
(Decl
) = N_Package_Declaration
then
5532 Spec
: constant Node_Id
:= Specification
(Decl
);
5535 Comp_Typ
:= Defining_Unit_Name
(Spec
);
5537 if not Analyzed
(Cunit
(Unum
)) then
5538 Decorate_Package_Specification
(Comp_Typ
);
5539 Set_Scope
(Comp_Typ
, Scope
);
5543 New_Internal_Shadow_Entity
5544 (Kind
=> Ekind
(Comp_Typ
),
5545 Sloc_Value
=> Sloc
(Comp_Typ
),
5548 Decorate_Package_Specification
(Lim_Typ
);
5549 Set_Scope
(Lim_Typ
, Scope
);
5551 Set_Chars
(Lim_Typ
, Chars
(Comp_Typ
));
5552 Set_Parent
(Lim_Typ
, Parent
(Comp_Typ
));
5553 Set_From_With_Type
(Lim_Typ
);
5555 -- Note: The non_limited_view attribute is not used
5556 -- for local packages.
5560 First_Decl
=> First
(Visible_Declarations
(Spec
)));
5568 ------------------------------
5569 -- Decorate_Incomplete_Type --
5570 ------------------------------
5572 procedure Decorate_Incomplete_Type
(E
: Entity_Id
; Scop
: Entity_Id
) is
5574 Set_Ekind
(E
, E_Incomplete_Type
);
5575 Set_Scope
(E
, Scop
);
5577 Set_Is_First_Subtype
(E
, True);
5578 Set_Stored_Constraint
(E
, No_Elist
);
5579 Set_Full_View
(E
, Empty
);
5580 Init_Size_Align
(E
);
5581 end Decorate_Incomplete_Type
;
5583 --------------------------
5584 -- Decorate_Tagged_Type --
5585 --------------------------
5587 procedure Decorate_Tagged_Type
5591 Mark
: Boolean := False)
5596 Decorate_Incomplete_Type
(T
, Scop
);
5597 Set_Is_Tagged_Type
(T
);
5599 -- Build corresponding class_wide type, if not previously done
5601 -- Note: The class-wide entity is shared by the limited-view
5602 -- and the full-view.
5604 if No
(Class_Wide_Type
(T
)) then
5605 CW
:= New_External_Entity
(E_Void
, Scope
(T
), Loc
, T
, 'C', 0, 'T');
5607 -- Set parent to be the same as the parent of the tagged type.
5608 -- We need a parent field set, and it is supposed to point to
5609 -- the declaration of the type. The tagged type declaration
5610 -- essentially declares two separate types, the tagged type
5611 -- itself and the corresponding class-wide type, so it is
5612 -- reasonable for the parent fields to point to the declaration
5615 Set_Parent
(CW
, Parent
(T
));
5617 -- Set remaining fields of classwide type
5619 Set_Ekind
(CW
, E_Class_Wide_Type
);
5621 Set_Scope
(CW
, Scop
);
5622 Set_Is_Tagged_Type
(CW
);
5623 Set_Is_First_Subtype
(CW
, True);
5624 Init_Size_Align
(CW
);
5625 Set_Has_Unknown_Discriminants
(CW
, True);
5626 Set_Class_Wide_Type
(CW
, CW
);
5627 Set_Equivalent_Type
(CW
, Empty
);
5628 Set_From_With_Type
(CW
, From_With_Type
(T
));
5629 Set_Materialize_Entity
(CW
, Mark
);
5631 -- Link type to its class-wide type
5633 Set_Class_Wide_Type
(T
, CW
);
5635 end Decorate_Tagged_Type
;
5637 ------------------------------------
5638 -- Decorate_Package_Specification --
5639 ------------------------------------
5641 procedure Decorate_Package_Specification
(P
: Entity_Id
) is
5643 -- Place only the most basic attributes
5645 Set_Ekind
(P
, E_Package
);
5646 Set_Etype
(P
, Standard_Void_Type
);
5647 end Decorate_Package_Specification
;
5649 --------------------------------
5650 -- New_Internal_Shadow_Entity --
5651 --------------------------------
5653 function New_Internal_Shadow_Entity
5654 (Kind
: Entity_Kind
;
5655 Sloc_Value
: Source_Ptr
;
5656 Id_Char
: Character) return Entity_Id
5658 E
: constant Entity_Id
:= Make_Temporary
(Sloc_Value
, Id_Char
);
5661 Set_Ekind
(E
, Kind
);
5662 Set_Is_Internal
(E
, True);
5664 if Kind
in Type_Kind
then
5665 Init_Size_Align
(E
);
5668 Append_Entity
(E
, Lim_Header
);
5671 end New_Internal_Shadow_Entity
;
5673 -- Start of processing for Build_Limited_Views
5676 pragma Assert
(Limited_Present
(N
));
5678 -- A library_item mentioned in a limited_with_clause is a package
5679 -- declaration, not a subprogram declaration, generic declaration,
5680 -- generic instantiation, or package renaming declaration.
5682 case Nkind
(Unit
(Library_Unit
(N
))) is
5683 when N_Package_Declaration
=>
5686 when N_Subprogram_Declaration
=>
5687 Error_Msg_N
("subprograms not allowed in "
5688 & "limited with_clauses", N
);
5691 when N_Generic_Package_Declaration |
5692 N_Generic_Subprogram_Declaration
=>
5693 Error_Msg_N
("generics not allowed in "
5694 & "limited with_clauses", N
);
5697 when N_Generic_Instantiation
=>
5698 Error_Msg_N
("generic instantiations not allowed in "
5699 & "limited with_clauses", N
);
5702 when N_Generic_Renaming_Declaration
=>
5703 Error_Msg_N
("generic renamings not allowed in "
5704 & "limited with_clauses", N
);
5707 when N_Subprogram_Renaming_Declaration
=>
5708 Error_Msg_N
("renamed subprograms not allowed in "
5709 & "limited with_clauses", N
);
5712 when N_Package_Renaming_Declaration
=>
5713 Error_Msg_N
("renamed packages not allowed in "
5714 & "limited with_clauses", N
);
5718 raise Program_Error
;
5721 -- The limited unit is not analyzed but the with clause must be
5722 -- minimally decorated so that checks on unused with clause also work
5723 -- with limited with clauses.
5725 if Is_Entity_Name
(Name
(N
)) then
5726 Set_Entity
(Name
(N
), P
);
5728 elsif Nkind
(Name
(N
)) = N_Selected_Component
then
5729 Set_Entity
(Selector_Name
(Name
(N
)), P
);
5732 -- Check if the chain is already built
5734 Spec
:= Specification
(Unit
(Library_Unit
(N
)));
5736 if Limited_View_Installed
(Spec
) then
5740 Set_Ekind
(P
, E_Package
);
5742 -- Build the header of the limited_view
5744 Lim_Header
:= Make_Temporary
(Sloc
(N
), 'Z');
5745 Set_Ekind
(Lim_Header
, E_Package
);
5746 Set_Is_Internal
(Lim_Header
);
5747 Set_Limited_View
(P
, Lim_Header
);
5749 -- Create the auxiliary chain. All the shadow entities are appended to
5750 -- the list of entities of the limited-view header
5754 First_Decl
=> First
(Visible_Declarations
(Spec
)));
5756 -- Save the last built shadow entity. It is needed later to set the
5757 -- reference to the first shadow entity in the private part
5759 Last_Pub_Lim_E
:= Last_Lim_E
;
5761 -- Ada 2005 (AI-262): Add the limited view of the private declarations
5762 -- Required to give support to limited-private-with clauses
5764 Build_Chain
(Scope
=> P
,
5765 First_Decl
=> First
(Private_Declarations
(Spec
)));
5767 if Last_Pub_Lim_E
/= Empty
then
5768 Set_First_Private_Entity
5769 (Lim_Header
, Next_Entity
(Last_Pub_Lim_E
));
5771 Set_First_Private_Entity
5772 (Lim_Header
, First_Entity
(P
));
5775 Set_Limited_View_Installed
(Spec
);
5776 end Build_Limited_Views
;
5778 -------------------------------
5779 -- Check_Body_Needed_For_SAL --
5780 -------------------------------
5782 procedure Check_Body_Needed_For_SAL
(Unit_Name
: Entity_Id
) is
5784 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean;
5785 -- Determine whether use of entity E might require the presence of its
5786 -- body. For a package this requires a recursive traversal of all nested
5789 ---------------------------
5790 -- Entity_Needed_For_SAL --
5791 ---------------------------
5793 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean is
5797 if Is_Subprogram
(E
)
5798 and then Has_Pragma_Inline
(E
)
5802 elsif Ekind_In
(E
, E_Generic_Function
, E_Generic_Procedure
) then
5805 elsif Ekind
(E
) = E_Generic_Package
5807 Nkind
(Unit_Declaration_Node
(E
)) = N_Generic_Package_Declaration
5808 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
5812 elsif Ekind
(E
) = E_Package
5813 and then Nkind
(Unit_Declaration_Node
(E
)) = N_Package_Declaration
5814 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
5816 Ent
:= First_Entity
(E
);
5817 while Present
(Ent
) loop
5818 if Entity_Needs_Body
(Ent
) then
5830 end Entity_Needs_Body
;
5832 -- Start of processing for Check_Body_Needed_For_SAL
5835 if Ekind
(Unit_Name
) = E_Generic_Package
5836 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
5837 N_Generic_Package_Declaration
5839 Present
(Corresponding_Body
(Unit_Declaration_Node
(Unit_Name
)))
5841 Set_Body_Needed_For_SAL
(Unit_Name
);
5843 elsif Ekind_In
(Unit_Name
, E_Generic_Procedure
, E_Generic_Function
) then
5844 Set_Body_Needed_For_SAL
(Unit_Name
);
5846 elsif Is_Subprogram
(Unit_Name
)
5847 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
5848 N_Subprogram_Declaration
5849 and then Has_Pragma_Inline
(Unit_Name
)
5851 Set_Body_Needed_For_SAL
(Unit_Name
);
5853 elsif Ekind
(Unit_Name
) = E_Subprogram_Body
then
5854 Check_Body_Needed_For_SAL
5855 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
5857 elsif Ekind
(Unit_Name
) = E_Package
5858 and then Entity_Needs_Body
(Unit_Name
)
5860 Set_Body_Needed_For_SAL
(Unit_Name
);
5862 elsif Ekind
(Unit_Name
) = E_Package_Body
5863 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) = N_Package_Body
5865 Check_Body_Needed_For_SAL
5866 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
5868 end Check_Body_Needed_For_SAL
;
5870 --------------------
5871 -- Remove_Context --
5872 --------------------
5874 procedure Remove_Context
(N
: Node_Id
) is
5875 Lib_Unit
: constant Node_Id
:= Unit
(N
);
5878 -- If this is a child unit, first remove the parent units
5880 if Is_Child_Spec
(Lib_Unit
) then
5881 Remove_Parents
(Lib_Unit
);
5884 Remove_Context_Clauses
(N
);
5887 ----------------------------
5888 -- Remove_Context_Clauses --
5889 ----------------------------
5891 procedure Remove_Context_Clauses
(N
: Node_Id
) is
5893 Unit_Name
: Entity_Id
;
5896 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5897 -- limited-views first and regular-views later (to maintain the
5900 -- First Phase: Remove limited_with context clauses
5902 Item
:= First
(Context_Items
(N
));
5903 while Present
(Item
) loop
5905 -- We are interested only in with clauses which got installed
5908 if Nkind
(Item
) = N_With_Clause
5909 and then Limited_Present
(Item
)
5910 and then Limited_View_Installed
(Item
)
5912 Remove_Limited_With_Clause
(Item
);
5918 -- Second Phase: Loop through context items and undo regular
5919 -- with_clauses and use_clauses.
5921 Item
:= First
(Context_Items
(N
));
5922 while Present
(Item
) loop
5924 -- We are interested only in with clauses which got installed on
5925 -- entry, as indicated by their Context_Installed flag set
5927 if Nkind
(Item
) = N_With_Clause
5928 and then Limited_Present
(Item
)
5929 and then Limited_View_Installed
(Item
)
5933 elsif Nkind
(Item
) = N_With_Clause
5934 and then Context_Installed
(Item
)
5936 -- Remove items from one with'ed unit
5938 Unit_Name
:= Entity
(Name
(Item
));
5939 Remove_Unit_From_Visibility
(Unit_Name
);
5940 Set_Context_Installed
(Item
, False);
5942 elsif Nkind
(Item
) = N_Use_Package_Clause
then
5943 End_Use_Package
(Item
);
5945 elsif Nkind
(Item
) = N_Use_Type_Clause
then
5946 End_Use_Type
(Item
);
5951 end Remove_Context_Clauses
;
5953 --------------------------------
5954 -- Remove_Limited_With_Clause --
5955 --------------------------------
5957 procedure Remove_Limited_With_Clause
(N
: Node_Id
) is
5958 P_Unit
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
5961 Lim_Header
: Entity_Id
;
5962 Lim_Typ
: Entity_Id
;
5966 pragma Assert
(Limited_View_Installed
(N
));
5968 -- In case of limited with_clause on subprograms, generics, instances,
5969 -- or renamings, the corresponding error was previously posted and we
5970 -- have nothing to do here.
5972 if Nkind
(P_Unit
) /= N_Package_Declaration
then
5976 P
:= Defining_Unit_Name
(Specification
(P_Unit
));
5978 -- Handle child packages
5980 if Nkind
(P
) = N_Defining_Program_Unit_Name
then
5981 P
:= Defining_Identifier
(P
);
5984 if Debug_Flag_I
then
5985 Write_Str
("remove limited view of ");
5986 Write_Name
(Chars
(P
));
5987 Write_Str
(" from visibility");
5991 -- Prepare the removal of the shadow entities from visibility. The first
5992 -- element of the limited view is a header (an E_Package entity) that is
5993 -- used to reference the first shadow entity in the private part of the
5996 Lim_Header
:= Limited_View
(P
);
5997 Lim_Typ
:= First_Entity
(Lim_Header
);
5999 -- Remove package and shadow entities from visibility if it has not
6002 if not Analyzed
(P_Unit
) then
6004 Set_Is_Immediately_Visible
(P
, False);
6006 while Present
(Lim_Typ
) loop
6008 Next_Entity
(Lim_Typ
);
6011 -- Otherwise this package has already appeared in the closure and its
6012 -- shadow entities must be replaced by its real entities. This code
6013 -- must be kept synchronized with the complementary code in Install
6014 -- Limited_Withed_Unit.
6017 -- Real entities that are type or subtype declarations were hidden
6018 -- from visibility at the point of installation of the limited-view.
6019 -- Now we recover the previous value of the hidden attribute.
6021 E
:= First_Entity
(P
);
6022 while Present
(E
) and then E
/= First_Private_Entity
(P
) loop
6024 Set_Is_Hidden
(E
, Was_Hidden
(E
));
6030 while Present
(Lim_Typ
)
6031 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
6033 -- Nested packages and child units were not unchained
6035 if Ekind
(Lim_Typ
) /= E_Package
6036 and then not Is_Child_Unit
(Non_Limited_View
(Lim_Typ
))
6038 -- If the package has incomplete types, the limited view of the
6039 -- incomplete type is in fact never visible (AI05-129) but we
6040 -- have created a shadow entity E1 for it, that points to E2,
6041 -- a non-limited incomplete type. This in turn has a full view
6042 -- E3 that is the full declaration. There is a corresponding
6043 -- shadow entity E4. When reinstalling the non-limited view,
6044 -- E2 must become the current entity and E3 must be ignored.
6046 E
:= Non_Limited_View
(Lim_Typ
);
6048 if Present
(Current_Entity
(E
))
6049 and then Ekind
(Current_Entity
(E
)) = E_Incomplete_Type
6050 and then Full_View
(Current_Entity
(E
)) = E
6053 -- Lim_Typ is the limited view of a full type declaration
6054 -- that has a previous incomplete declaration, i.e. E3 from
6055 -- the previous description. Nothing to insert.
6060 pragma Assert
(not In_Chain
(E
));
6062 Prev
:= Current_Entity
(Lim_Typ
);
6064 if Prev
= Lim_Typ
then
6065 Set_Current_Entity
(E
);
6068 while Present
(Prev
)
6069 and then Homonym
(Prev
) /= Lim_Typ
6071 Prev
:= Homonym
(Prev
);
6074 if Present
(Prev
) then
6075 Set_Homonym
(Prev
, E
);
6079 -- Preserve structure of homonym chain
6081 Set_Homonym
(E
, Homonym
(Lim_Typ
));
6085 Next_Entity
(Lim_Typ
);
6089 -- Indicate that the limited view of the package is not installed
6091 Set_From_With_Type
(P
, False);
6092 Set_Limited_View_Installed
(N
, False);
6093 end Remove_Limited_With_Clause
;
6095 --------------------
6096 -- Remove_Parents --
6097 --------------------
6099 procedure Remove_Parents
(Lib_Unit
: Node_Id
) is
6102 P_Spec
: Node_Id
:= Empty
;
6104 Vis
: constant Boolean :=
6105 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
;
6108 if Is_Child_Spec
(Lib_Unit
) then
6109 P_Spec
:= Parent_Spec
(Lib_Unit
);
6111 elsif Nkind
(Lib_Unit
) = N_Package_Body
6112 and then Nkind
(Original_Node
(Lib_Unit
)) = N_Package_Instantiation
6114 P_Spec
:= Parent_Spec
(Original_Node
(Lib_Unit
));
6117 if Present
(P_Spec
) then
6119 P_Name
:= Get_Parent_Entity
(P
);
6120 Remove_Context_Clauses
(P_Spec
);
6121 End_Package_Scope
(P_Name
);
6122 Set_Is_Immediately_Visible
(P_Name
, Vis
);
6124 -- Remove from visibility the siblings as well, which are directly
6125 -- visible while the parent is in scope.
6127 E
:= First_Entity
(P_Name
);
6128 while Present
(E
) loop
6129 if Is_Child_Unit
(E
) then
6130 Set_Is_Immediately_Visible
(E
, False);
6136 Set_In_Package_Body
(P_Name
, False);
6138 -- This is the recursive call to remove the context of any higher
6139 -- level parent. This recursion ensures that all parents are removed
6140 -- in the reverse order of their installation.
6146 ---------------------------------
6147 -- Remove_Private_With_Clauses --
6148 ---------------------------------
6150 procedure Remove_Private_With_Clauses
(Comp_Unit
: Node_Id
) is
6153 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean;
6154 -- Check whether a given unit appears in a regular with_clause. Used to
6155 -- determine whether a private_with_clause, implicit or explicit, should
6158 ----------------------------
6159 -- In_Regular_With_Clause --
6160 ----------------------------
6162 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean
6167 Item
:= First
(Context_Items
(Comp_Unit
));
6168 while Present
(Item
) loop
6169 if Nkind
(Item
) = N_With_Clause
6170 and then Entity
(Name
(Item
)) = E
6171 and then not Private_Present
(Item
)
6179 end In_Regular_With_Clause
;
6181 -- Start of processing for Remove_Private_With_Clauses
6184 Item
:= First
(Context_Items
(Comp_Unit
));
6185 while Present
(Item
) loop
6186 if Nkind
(Item
) = N_With_Clause
6187 and then Private_Present
(Item
)
6189 -- If private_with_clause is redundant, remove it from context,
6190 -- as a small optimization to subsequent handling of private_with
6191 -- clauses in other nested packages.
6193 if In_Regular_With_Clause
(Entity
(Name
(Item
))) then
6195 Nxt
: constant Node_Id
:= Next
(Item
);
6201 elsif Limited_Present
(Item
) then
6202 if not Limited_View_Installed
(Item
) then
6203 Remove_Limited_With_Clause
(Item
);
6209 Remove_Unit_From_Visibility
(Entity
(Name
(Item
)));
6210 Set_Context_Installed
(Item
, False);
6218 end Remove_Private_With_Clauses
;
6220 ---------------------------------
6221 -- Remove_Unit_From_Visibility --
6222 ---------------------------------
6224 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
) is
6226 if Debug_Flag_I
then
6227 Write_Str
("remove unit ");
6228 Write_Name
(Chars
(Unit_Name
));
6229 Write_Str
(" from visibility");
6233 Set_Is_Visible_Lib_Unit
(Unit_Name
, False);
6234 Set_Is_Potentially_Use_Visible
(Unit_Name
, False);
6235 Set_Is_Immediately_Visible
(Unit_Name
, False);
6237 -- If the unit is a wrapper package, the subprogram instance is
6238 -- what must be removed from visibility.
6240 if Is_Wrapper_Package
(Unit_Name
) then
6241 Set_Is_Immediately_Visible
(Current_Entity
(Unit_Name
), False);
6243 end Remove_Unit_From_Visibility
;
6258 procedure Unchain
(E
: Entity_Id
) is
6262 Prev
:= Current_Entity
(E
);
6268 Set_Name_Entity_Id
(Chars
(E
), Homonym
(E
));
6271 while Present
(Prev
)
6272 and then Homonym
(Prev
) /= E
6274 Prev
:= Homonym
(Prev
);
6277 if Present
(Prev
) then
6278 Set_Homonym
(Prev
, Homonym
(E
));
6282 if Debug_Flag_I
then
6283 Write_Str
(" (homonym) unchain ");
6284 Write_Name
(Chars
(E
));