1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Contracts
; use Contracts
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Errout
; use Errout
;
32 with Exp_Util
; use Exp_Util
;
33 with Elists
; use Elists
;
34 with Fname
; use Fname
;
35 with Fname
.UF
; use Fname
.UF
;
36 with Freeze
; use Freeze
;
37 with Ghost
; use Ghost
;
38 with Impunit
; use Impunit
;
39 with Inline
; use Inline
;
41 with Lib
.Load
; use Lib
.Load
;
42 with Lib
.Xref
; use Lib
.Xref
;
43 with Namet
; use Namet
;
44 with Nlists
; use Nlists
;
45 with Nmake
; use Nmake
;
47 with Output
; use Output
;
48 with Par_SCO
; use Par_SCO
;
49 with Restrict
; use Restrict
;
50 with Rident
; use Rident
;
51 with Rtsfind
; use Rtsfind
;
53 with Sem_Aux
; use Sem_Aux
;
54 with Sem_Ch3
; use Sem_Ch3
;
55 with Sem_Ch6
; use Sem_Ch6
;
56 with Sem_Ch7
; use Sem_Ch7
;
57 with Sem_Ch8
; use Sem_Ch8
;
58 with Sem_Dist
; use Sem_Dist
;
59 with Sem_Prag
; use Sem_Prag
;
60 with Sem_Util
; use Sem_Util
;
61 with Sem_Warn
; use Sem_Warn
;
62 with Stand
; use Stand
;
63 with Sinfo
; use Sinfo
;
64 with Sinfo
.CN
; use Sinfo
.CN
;
65 with Sinput
; use Sinput
;
66 with Snames
; use Snames
;
67 with Style
; use Style
;
68 with Stylesw
; use Stylesw
;
69 with Tbuild
; use Tbuild
;
70 with Uname
; use Uname
;
72 package body Sem_Ch10
is
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 procedure Analyze_Context
(N
: Node_Id
);
79 -- Analyzes items in the context clause of compilation unit
81 procedure Build_Limited_Views
(N
: Node_Id
);
82 -- Build and decorate the list of shadow entities for a package mentioned
83 -- in a limited_with clause. If the package was not previously analyzed
84 -- then it also performs a basic decoration of the real entities. This is
85 -- required in order to avoid passing non-decorated entities to the
86 -- back-end. Implements Ada 2005 (AI-50217).
88 procedure Analyze_Proper_Body
(N
: Node_Id
; Nam
: Entity_Id
);
89 -- Common processing for all stubs (subprograms, tasks, packages, and
90 -- protected cases). N is the stub to be analyzed. Once the subunit name
91 -- is established, load and analyze. Nam is the non-overloadable entity
92 -- for which the proper body provides a completion. Subprogram stubs are
93 -- handled differently because they can be declarations.
95 procedure Check_Body_Needed_For_SAL
(Unit_Name
: Entity_Id
);
96 -- Check whether the source for the body of a compilation unit must be
97 -- included in a standalone library.
99 procedure Check_No_Elab_Code_All
(N
: Node_Id
);
100 -- Carries out possible tests for violation of No_Elab_Code all for withed
101 -- units in the Context_Items of unit N.
103 procedure Check_Private_Child_Unit
(N
: Node_Id
);
104 -- If a with_clause mentions a private child unit, the compilation unit
105 -- must be a member of the same family, as described in 10.1.2.
107 procedure Check_Stub_Level
(N
: Node_Id
);
108 -- Verify that a stub is declared immediately within a compilation unit,
109 -- and not in an inner frame.
111 procedure Expand_With_Clause
(Item
: Node_Id
; Nam
: Node_Id
; N
: Node_Id
);
112 -- When a child unit appears in a context clause, the implicit withs on
113 -- parents are made explicit, and with clauses are inserted in the context
114 -- clause before the one for the child. If a parent in the with_clause
115 -- is a renaming, the implicit with_clause is on the renaming whose name
116 -- is mentioned in the with_clause, and not on the package it renames.
117 -- N is the compilation unit whose list of context items receives the
118 -- implicit with_clauses.
120 procedure Generate_Parent_References
(N
: Node_Id
; P_Id
: Entity_Id
);
121 -- Generate cross-reference information for the parents of child units
122 -- and of subunits. N is a defining_program_unit_name, and P_Id is the
123 -- immediate parent scope.
125 function Has_With_Clause
128 Is_Limited
: Boolean := False) return Boolean;
129 -- Determine whether compilation unit C_Unit contains a [limited] with
130 -- clause for package Pack. Use the flag Is_Limited to designate desired
133 procedure Implicit_With_On_Parent
(Child_Unit
: Node_Id
; N
: Node_Id
);
134 -- If the main unit is a child unit, implicit withs are also added for
135 -- all its ancestors.
137 function In_Chain
(E
: Entity_Id
) return Boolean;
138 -- Check that the shadow entity is not already in the homonym chain, for
139 -- example through a limited_with clause in a parent unit.
141 procedure Install_Context_Clauses
(N
: Node_Id
; Chain
: Boolean := True);
142 -- Subsidiary to Install_Context and Install_Parents. Process all with
143 -- and use clauses for current unit and its library unit if any. The flag
144 -- Chain is used to control the "chaining" or linking together of use-type
145 -- and use-package clauses to avoid circularities with reinstalling
148 procedure Install_Limited_Context_Clauses
(N
: Node_Id
);
149 -- Subsidiary to Install_Context. Process only limited with_clauses for
150 -- current unit. Implements Ada 2005 (AI-50217).
152 procedure Install_Limited_Withed_Unit
(N
: Node_Id
);
153 -- Place shadow entities for a limited_with package in the visibility
154 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
156 procedure Install_Withed_Unit
157 (With_Clause
: Node_Id
;
158 Private_With_OK
: Boolean := False);
159 -- If the unit is not a child unit, make unit immediately visible. The
160 -- caller ensures that the unit is not already currently installed. The
161 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
162 -- is called when compiling the private part of a package, or installing
163 -- the private declarations of a parent unit.
165 procedure Install_Parents
167 Is_Private
: Boolean;
168 Chain
: Boolean := True);
169 -- This procedure establishes the context for the compilation of a child
170 -- unit. If Lib_Unit is a child library spec then the context of the parent
171 -- is installed, and the parent itself made immediately visible, so that
172 -- the child unit is processed in the declarative region of the parent.
173 -- Install_Parents makes a recursive call to itself to ensure that all
174 -- parents are loaded in the nested case. If Lib_Unit is a library body,
175 -- the only effect of Install_Parents is to install the private decls of
176 -- the parents, because the visible parent declarations will have been
177 -- installed as part of the context of the corresponding spec. The flag
178 -- Chain is used to control the "chaining" or linking of use-type and
179 -- use-package clauses to avoid circularities when installing context.
181 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
);
182 -- In the compilation of a child unit, a child of any of the ancestor
183 -- units is directly visible if it is visible, because the parent is in
184 -- an enclosing scope. Iterate over context to find child units of U_Name
185 -- or of some ancestor of it.
187 function Is_Ancestor_Unit
(U1
: Node_Id
; U2
: Node_Id
) return Boolean;
188 -- When compiling a unit Q descended from some parent unit P, a limited
189 -- with_clause in the context of P that names some other ancestor of Q
190 -- must not be installed because the ancestor is immediately visible.
192 function Is_Child_Spec
(Lib_Unit
: Node_Id
) return Boolean;
193 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
194 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
195 -- a library spec that has a parent. If the call to Is_Child_Spec returns
196 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
197 -- compilation unit for the parent spec.
199 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
200 -- Parent_Spec is non-empty, this is also a child unit.
202 procedure Remove_Context_Clauses
(N
: Node_Id
);
203 -- Subsidiary of previous one. Remove use_ and with_clauses
205 procedure Remove_Limited_With_Clause
(N
: Node_Id
);
206 -- Remove from visibility the shadow entities introduced for a package
207 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
209 procedure Remove_Parents
(Lib_Unit
: Node_Id
);
210 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
211 -- contexts established by the corresponding call to Install_Parents are
212 -- removed. Remove_Parents contains a recursive call to itself to ensure
213 -- that all parents are removed in the nested case.
215 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
);
216 -- Reset all visibility flags on unit after compiling it, either as a main
217 -- unit or as a unit in the context.
219 procedure Unchain
(E
: Entity_Id
);
220 -- Remove single entity from visibility list
223 -- A dummy procedure, for debugging use, called just before analyzing the
224 -- main unit (after dealing with any context clauses).
226 --------------------------
227 -- Limited_With_Clauses --
228 --------------------------
230 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
231 -- mutually recursive types declared in different units. A limited_with
232 -- clause that names package P in the context of unit U makes the types
233 -- declared in the visible part of P available within U, but with the
234 -- restriction that these types can only be used as incomplete types.
235 -- The limited_with clause does not impose a semantic dependence on P,
236 -- and it is possible for two packages to have limited_with_clauses on
237 -- each other without creating an elaboration circularity.
239 -- To support this feature, the analysis of a limited_with clause must
240 -- create an abbreviated view of the package, without performing any
241 -- semantic analysis on it. This "package abstract" contains shadow types
242 -- that are in one-one correspondence with the real types in the package,
243 -- and that have the properties of incomplete types.
245 -- The implementation creates two element lists: one to chain the shadow
246 -- entities, and one to chain the corresponding type entities in the tree
247 -- of the package. Links between corresponding entities in both chains
248 -- allow the compiler to select the proper view of a given type, depending
249 -- on the context. Note that in contrast with the handling of private
250 -- types, the limited view and the non-limited view of a type are treated
251 -- as separate entities, and no entity exchange needs to take place, which
252 -- makes the implementation much simpler than could be feared.
254 ------------------------------
255 -- Analyze_Compilation_Unit --
256 ------------------------------
258 procedure Analyze_Compilation_Unit
(N
: Node_Id
) is
259 procedure Check_Redundant_Withs
260 (Context_Items
: List_Id
;
261 Spec_Context_Items
: List_Id
:= No_List
);
262 -- Determine whether the context list of a compilation unit contains
263 -- redundant with clauses. When checking body clauses against spec
264 -- clauses, set Context_Items to the context list of the body and
265 -- Spec_Context_Items to that of the spec. Parent packages are not
266 -- examined for documentation purposes.
268 ---------------------------
269 -- Check_Redundant_Withs --
270 ---------------------------
272 procedure Check_Redundant_Withs
273 (Context_Items
: List_Id
;
274 Spec_Context_Items
: List_Id
:= No_List
)
278 procedure Process_Body_Clauses
279 (Context_List
: List_Id
;
282 Used_Type_Or_Elab
: out Boolean);
283 -- Examine the context clauses of a package body, trying to match the
284 -- name entity of Clause with any list element. If the match occurs
285 -- on a use package clause set Used to True, for a use type clause or
286 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
288 procedure Process_Spec_Clauses
289 (Context_List
: List_Id
;
292 Withed
: out Boolean;
293 Exit_On_Self
: Boolean := False);
294 -- Examine the context clauses of a package spec, trying to match
295 -- the name entity of Clause with any list element. If the match
296 -- occurs on a use package clause, set Used to True, for a with
297 -- package clause other than Clause, set Withed to True. Limited
298 -- with clauses, implicitly generated with clauses and withs
299 -- having pragmas Elaborate or Elaborate_All applied to them are
300 -- skipped. Exit_On_Self is used to control the search loop and
301 -- force an exit whenever Clause sees itself in the search.
303 --------------------------
304 -- Process_Body_Clauses --
305 --------------------------
307 procedure Process_Body_Clauses
308 (Context_List
: List_Id
;
311 Used_Type_Or_Elab
: out Boolean)
313 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
319 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean;
320 -- In an expanded name in a use clause, if the prefix is a renamed
321 -- package, the entity is set to the original package as a result,
322 -- when checking whether the package appears in a previous with
323 -- clause, the renaming has to be taken into account, to prevent
324 -- spurious/incorrect warnings. A common case is use of Text_IO.
330 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean is
332 return Entity
(N
) = P
333 or else (Present
(Renamed_Object
(P
))
334 and then Entity
(N
) = Renamed_Object
(P
));
337 -- Start of processing for Process_Body_Clauses
341 Used_Type_Or_Elab
:= False;
343 Cont_Item
:= First
(Context_List
);
344 while Present
(Cont_Item
) loop
346 -- Package use clause
348 if Nkind
(Cont_Item
) = N_Use_Package_Clause
351 -- Search through use clauses
353 Use_Item
:= Name
(Cont_Item
);
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
383 elsif Nkind
(Cont_Item
) = N_Use_Type_Clause
384 and then not Used_Type_Or_Elab
386 Subt_Mark
:= Subtype_Mark
(Cont_Item
);
387 if not Used_Type_Or_Elab
388 and then Same_Unit
(Prefix
(Subt_Mark
), Nam_Ent
)
390 Used_Type_Or_Elab
:= True;
393 -- Pragma Elaborate or Elaborate_All
395 elsif Nkind
(Cont_Item
) = N_Pragma
397 Nam_In
(Pragma_Name_Unmapped
(Cont_Item
),
398 Name_Elaborate
, Name_Elaborate_All
)
399 and then not Used_Type_Or_Elab
402 First
(Pragma_Argument_Associations
(Cont_Item
));
403 while Present
(Prag_Unit
) and then not Used_Type_Or_Elab
loop
404 if Entity
(Expression
(Prag_Unit
)) = Nam_Ent
then
405 Used_Type_Or_Elab
:= True;
414 end Process_Body_Clauses
;
416 --------------------------
417 -- Process_Spec_Clauses --
418 --------------------------
420 procedure Process_Spec_Clauses
421 (Context_List
: List_Id
;
424 Withed
: out Boolean;
425 Exit_On_Self
: Boolean := False)
427 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
434 Cont_Item
:= First
(Context_List
);
435 while Present
(Cont_Item
) loop
437 -- Stop the search since the context items after Cont_Item have
438 -- already been examined in a previous iteration of the reverse
439 -- loop in Check_Redundant_Withs.
442 and Cont_Item
= Clause
447 -- Package use clause
449 if Nkind
(Cont_Item
) = N_Use_Package_Clause
452 if Entity
(Name
(Cont_Item
)) = Nam_Ent
then
456 -- Package with clause. Avoid processing self, implicitly
457 -- generated with clauses or limited with clauses. Note that
458 -- we examine with clauses having pragmas Elaborate or
459 -- Elaborate_All applied to them due to cases such as:
463 -- pragma Elaborate (Pack);
465 -- In this case, the second with clause is redundant since
466 -- the pragma applies only to the first "with Pack;".
468 -- Note that we only consider with_clauses that comes from
469 -- source. In the case of renamings used as prefixes of names
470 -- in with_clauses, we generate a with_clause for the prefix,
471 -- which we do not treat as implicit because it is needed for
472 -- visibility analysis, but is also not redundant.
474 elsif Nkind
(Cont_Item
) = N_With_Clause
475 and then not Implicit_With
(Cont_Item
)
476 and then Comes_From_Source
(Cont_Item
)
477 and then not Limited_Present
(Cont_Item
)
478 and then Cont_Item
/= Clause
479 and then Entity
(Name
(Cont_Item
)) = Nam_Ent
486 end Process_Spec_Clauses
;
488 -- Start of processing for Check_Redundant_Withs
491 Clause
:= Last
(Context_Items
);
492 while Present
(Clause
) loop
494 -- Avoid checking implicitly generated with clauses, limited with
495 -- clauses or withs that have pragma Elaborate or Elaborate_All.
497 if Nkind
(Clause
) = N_With_Clause
498 and then not Implicit_With
(Clause
)
499 and then not Limited_Present
(Clause
)
500 and then not Elaborate_Present
(Clause
)
502 -- With_clauses introduced for renamings of parent clauses
503 -- are not marked implicit because they need to be properly
504 -- installed, but they do not come from source and do not
507 and then Comes_From_Source
(Clause
)
509 -- Package body-to-spec check
511 if Present
(Spec_Context_Items
) then
513 Used_In_Body
: Boolean;
514 Used_In_Spec
: Boolean;
515 Used_Type_Or_Elab
: Boolean;
516 Withed_In_Spec
: Boolean;
520 (Context_List
=> Spec_Context_Items
,
522 Used
=> Used_In_Spec
,
523 Withed
=> Withed_In_Spec
);
526 (Context_List
=> Context_Items
,
528 Used
=> Used_In_Body
,
529 Used_Type_Or_Elab
=> Used_Type_Or_Elab
);
531 -- "Type Elab" refers to the presence of either a use
532 -- type clause, pragmas Elaborate or Elaborate_All.
534 -- +---------------+---------------------------+------+
535 -- | Spec | Body | Warn |
536 -- +--------+------+--------+------+-----------+------+
537 -- | Withed | Used | Withed | Used | Type Elab | |
538 -- | X | | X | | | X |
539 -- | X | | X | X | | |
540 -- | X | | X | | X | |
541 -- | X | | X | X | X | |
542 -- | X | X | X | | | X |
543 -- | X | X | X | | X | |
544 -- | X | X | X | X | | X |
545 -- | X | X | X | X | X | |
546 -- +--------+------+--------+------+-----------+------+
549 and then not Used_Type_Or_Elab
)
551 ((not Used_In_Spec
and then not Used_In_Body
)
552 or else Used_In_Spec
)
554 Error_Msg_N
-- CODEFIX
555 ("redundant with clause in body?r?", Clause
);
558 Used_In_Body
:= False;
559 Used_In_Spec
:= False;
560 Used_Type_Or_Elab
:= False;
561 Withed_In_Spec
:= False;
564 -- Standalone package spec or body check
568 Dont_Care
: Boolean := False;
569 Withed
: Boolean := False;
572 -- The mechanism for examining the context clauses of a
573 -- package spec can be applied to package body clauses.
576 (Context_List
=> Context_Items
,
580 Exit_On_Self
=> True);
583 Error_Msg_N
-- CODEFIX
584 ("redundant with clause?r?", Clause
);
592 end Check_Redundant_Withs
;
596 Main_Cunit
: constant Node_Id
:= Cunit
(Main_Unit
);
597 Unit_Node
: constant Node_Id
:= Unit
(N
);
598 Lib_Unit
: Node_Id
:= Library_Unit
(N
);
599 Par_Spec_Name
: Unit_Name_Type
;
601 Unum
: Unit_Number_Type
;
603 -- Start of processing for Analyze_Compilation_Unit
606 Process_Compilation_Unit_Pragmas
(N
);
608 -- If the unit is a subunit whose parent has not been analyzed (which
609 -- indicates that the main unit is a subunit, either the current one or
610 -- one of its descendants) then the subunit is compiled as part of the
611 -- analysis of the parent, which we proceed to do. Basically this gets
612 -- handled from the top down and we don't want to do anything at this
613 -- level (i.e. this subunit will be handled on the way down from the
614 -- parent), so at this level we immediately return. If the subunit ends
615 -- up not analyzed, it means that the parent did not contain a stub for
616 -- it, or that there errors were detected in some ancestor.
618 if Nkind
(Unit_Node
) = N_Subunit
and then not Analyzed
(Lib_Unit
) then
619 Semantics
(Lib_Unit
);
621 if not Analyzed
(Proper_Body
(Unit_Node
)) then
622 if Serious_Errors_Detected
> 0 then
623 Error_Msg_N
("subunit not analyzed (errors in parent unit)", N
);
625 Error_Msg_N
("missing stub for subunit", N
);
632 -- Analyze context (this will call Sem recursively for with'ed units) To
633 -- detect circularities among with-clauses that are not caught during
634 -- loading, we set the Context_Pending flag on the current unit. If the
635 -- flag is already set there is a potential circularity. We exclude
636 -- predefined units from this check because they are known to be safe.
637 -- We also exclude package bodies that are present because circularities
638 -- between bodies are harmless (and necessary).
640 if Context_Pending
(N
) then
642 Circularity
: Boolean := True;
645 if In_Predefined_Unit
(N
) then
646 Circularity
:= False;
649 for U
in Main_Unit
+ 1 .. Last_Unit
loop
650 if Nkind
(Unit
(Cunit
(U
))) = N_Package_Body
651 and then not Analyzed
(Cunit
(U
))
653 Circularity
:= False;
660 Error_Msg_N
("circular dependency caused by with_clauses", N
);
662 ("\possibly missing limited_with clause"
663 & " in one of the following", N
);
665 for U
in Main_Unit
.. Last_Unit
loop
666 if Context_Pending
(Cunit
(U
)) then
667 Error_Msg_Unit_1
:= Get_Unit_Name
(Unit
(Cunit
(U
)));
668 Error_Msg_N
("\unit$", N
);
672 raise Unrecoverable_Error
;
676 Set_Context_Pending
(N
);
681 Set_Context_Pending
(N
, False);
683 -- If the unit is a package body, the spec is already loaded and must be
684 -- analyzed first, before we analyze the body.
686 if Nkind
(Unit_Node
) = N_Package_Body
then
688 -- If no Lib_Unit, then there was a serious previous error, so just
689 -- ignore the entire analysis effort.
691 if No
(Lib_Unit
) then
692 Check_Error_Detected
;
696 -- Analyze the package spec
698 Semantics
(Lib_Unit
);
700 -- Check for unused with's
702 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
704 -- Verify that the library unit is a package declaration
706 if not Nkind_In
(Unit
(Lib_Unit
), N_Package_Declaration
,
707 N_Generic_Package_Declaration
)
710 ("no legal package declaration for package body", N
);
713 -- Otherwise, the entity in the declaration is visible. Update the
714 -- version to reflect dependence of this body on the spec.
717 Spec_Id
:= Defining_Entity
(Unit
(Lib_Unit
));
718 Set_Is_Immediately_Visible
(Spec_Id
, True);
719 Version_Update
(N
, Lib_Unit
);
721 if Nkind
(Defining_Unit_Name
(Unit_Node
)) =
722 N_Defining_Program_Unit_Name
724 Generate_Parent_References
(Unit_Node
, Scope
(Spec_Id
));
729 -- If the unit is a subprogram body, then we similarly need to analyze
730 -- its spec. However, things are a little simpler in this case, because
731 -- here, this analysis is done mostly for error checking and consistency
732 -- purposes (but not only, e.g. there could be a contract on the spec),
733 -- so there's nothing else to be done.
735 elsif Nkind
(Unit_Node
) = N_Subprogram_Body
then
736 if Acts_As_Spec
(N
) then
738 -- If the subprogram body is a child unit, we must create a
739 -- declaration for it, in order to properly load the parent(s).
740 -- After this, the original unit does not acts as a spec, because
741 -- there is an explicit one. If this unit appears in a context
742 -- clause, then an implicit with on the parent will be added when
743 -- installing the context. If this is the main unit, there is no
744 -- Unit_Table entry for the declaration (it has the unit number
745 -- of the main unit) and code generation is unaffected.
747 Unum
:= Get_Cunit_Unit_Number
(N
);
748 Par_Spec_Name
:= Get_Parent_Spec_Name
(Unit_Name
(Unum
));
750 if Par_Spec_Name
/= No_Unit_Name
then
753 (Load_Name
=> Par_Spec_Name
,
758 if Unum
/= No_Unit
then
760 -- Build subprogram declaration and attach parent unit to it
761 -- This subprogram declaration does not come from source,
762 -- Nevertheless the backend must generate debugging info for
763 -- it, and this must be indicated explicitly. We also mark
764 -- the body entity as a child unit now, to prevent a
765 -- cascaded error if the spec entity cannot be entered
766 -- in its scope. Finally we create a Units table entry for
767 -- the subprogram declaration, to maintain a one-to-one
768 -- correspondence with compilation unit nodes. This is
769 -- critical for the tree traversals performed by CodePeer.
772 Loc
: constant Source_Ptr
:= Sloc
(N
);
773 SCS
: constant Boolean :=
774 Get_Comes_From_Source_Default
;
777 Set_Comes_From_Source_Default
(False);
779 -- Note: We copy the Context_Items from the explicit body
780 -- to the implicit spec, setting the former to Empty_List
781 -- to preserve the treeish nature of the tree, during
782 -- analysis of the spec. Then we put it back the way it
783 -- was -- copy the Context_Items from the spec to the
784 -- body, and set the spec Context_Items to Empty_List.
785 -- It is necessary to preserve the treeish nature,
786 -- because otherwise we will call End_Use_* twice on the
790 Make_Compilation_Unit
(Loc
,
791 Context_Items
=> Context_Items
(N
),
793 Make_Subprogram_Declaration
(Sloc
(N
),
796 (Specification
(Unit_Node
))),
798 Make_Compilation_Unit_Aux
(Loc
));
800 Set_Context_Items
(N
, Empty_List
);
801 Set_Library_Unit
(N
, Lib_Unit
);
802 Set_Parent_Spec
(Unit
(Lib_Unit
), Cunit
(Unum
));
803 Make_Child_Decl_Unit
(N
);
804 Semantics
(Lib_Unit
);
806 -- Now that a separate declaration exists, the body
807 -- of the child unit does not act as spec any longer.
809 Set_Acts_As_Spec
(N
, False);
810 Set_Is_Child_Unit
(Defining_Entity
(Unit_Node
));
811 Set_Debug_Info_Needed
(Defining_Entity
(Unit
(Lib_Unit
)));
812 Set_Comes_From_Source_Default
(SCS
);
814 -- Restore Context_Items to the body
816 Set_Context_Items
(N
, Context_Items
(Lib_Unit
));
817 Set_Context_Items
(Lib_Unit
, Empty_List
);
822 -- Here for subprogram with separate declaration
825 Semantics
(Lib_Unit
);
826 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
827 Version_Update
(N
, Lib_Unit
);
830 -- If this is a child unit, generate references to the parents
832 if Nkind
(Defining_Unit_Name
(Specification
(Unit_Node
))) =
833 N_Defining_Program_Unit_Name
835 Generate_Parent_References
836 (Specification
(Unit_Node
),
837 Scope
(Defining_Entity
(Unit
(Lib_Unit
))));
841 -- If it is a child unit, the parent must be elaborated first and we
842 -- update version, since we are dependent on our parent.
844 if Is_Child_Spec
(Unit_Node
) then
846 -- The analysis of the parent is done with style checks off
849 Save_Style_Check
: constant Boolean := Style_Check
;
852 if not GNAT_Mode
then
853 Style_Check
:= False;
856 Semantics
(Parent_Spec
(Unit_Node
));
857 Version_Update
(N
, Parent_Spec
(Unit_Node
));
859 -- Restore style check settings
861 Style_Check
:= Save_Style_Check
;
865 -- With the analysis done, install the context. Note that we can't
866 -- install the context from the with clauses as we analyze them, because
867 -- each with clause must be analyzed in a clean visibility context, so
868 -- we have to wait and install them all at once.
872 if Is_Child_Spec
(Unit_Node
) then
874 -- Set the entities of all parents in the program_unit_name
876 Generate_Parent_References
877 (Unit_Node
, Get_Parent_Entity
(Unit
(Parent_Spec
(Unit_Node
))));
880 -- All components of the context: with-clauses, library unit, ancestors
881 -- if any, (and their context) are analyzed and installed.
883 -- Call special debug routine sm if this is the main unit
885 if Current_Sem_Unit
= Main_Unit
then
889 -- Now analyze the unit (package, subprogram spec, body) itself
893 if Warn_On_Redundant_Constructs
then
894 Check_Redundant_Withs
(Context_Items
(N
));
896 if Nkind
(Unit_Node
) = N_Package_Body
then
897 Check_Redundant_Withs
898 (Context_Items
=> Context_Items
(N
),
899 Spec_Context_Items
=> Context_Items
(Lib_Unit
));
903 -- The above call might have made Unit_Node an N_Subprogram_Body from
904 -- something else, so propagate any Acts_As_Spec flag.
906 if Nkind
(Unit_Node
) = N_Subprogram_Body
907 and then Acts_As_Spec
(Unit_Node
)
909 Set_Acts_As_Spec
(N
);
912 -- Register predefined units in Rtsfind
914 if In_Predefined_Unit
(N
) then
915 Set_RTU_Loaded
(Unit_Node
);
918 -- Treat compilation unit pragmas that appear after the library unit
920 if Present
(Pragmas_After
(Aux_Decls_Node
(N
))) then
922 Prag_Node
: Node_Id
:= First
(Pragmas_After
(Aux_Decls_Node
(N
)));
924 while Present
(Prag_Node
) loop
931 -- Analyze the contract of a [generic] subprogram that acts as a
932 -- compilation unit after all compilation pragmas have been analyzed.
934 if Nkind_In
(Unit_Node
, N_Generic_Subprogram_Declaration
,
935 N_Subprogram_Declaration
)
937 Analyze_Entry_Or_Subprogram_Contract
(Defining_Entity
(Unit_Node
));
940 -- Generate distribution stubs if requested and no error
943 and then (Distribution_Stub_Mode
= Generate_Receiver_Stub_Body
945 Distribution_Stub_Mode
= Generate_Caller_Stub_Body
)
946 and then Fatal_Error
(Main_Unit
) /= Error_Detected
948 if Is_RCI_Pkg_Spec_Or_Body
(N
) then
950 -- Regular RCI package
952 Add_Stub_Constructs
(N
);
954 elsif (Nkind
(Unit_Node
) = N_Package_Declaration
955 and then Is_Shared_Passive
(Defining_Entity
956 (Specification
(Unit_Node
))))
957 or else (Nkind
(Unit_Node
) = N_Package_Body
959 Is_Shared_Passive
(Corresponding_Spec
(Unit_Node
)))
961 -- Shared passive package
963 Add_Stub_Constructs
(N
);
965 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
967 Is_Remote_Call_Interface
968 (Defining_Entity
(Specification
(Instance_Spec
(Unit_Node
))))
970 -- Instantiation of a RCI generic package
972 Add_Stub_Constructs
(N
);
976 -- Remove unit from visibility, so that environment is clean for the
977 -- next compilation, which is either the main unit or some other unit
980 if Nkind_In
(Unit_Node
, N_Package_Declaration
,
981 N_Package_Renaming_Declaration
,
982 N_Subprogram_Declaration
)
983 or else Nkind
(Unit_Node
) in N_Generic_Declaration
984 or else (Nkind
(Unit_Node
) = N_Subprogram_Body
985 and then Acts_As_Spec
(Unit_Node
))
987 Remove_Unit_From_Visibility
(Defining_Entity
(Unit_Node
));
989 -- If the unit is an instantiation whose body will be elaborated for
990 -- inlining purposes, use the proper entity of the instance. The entity
991 -- may be missing if the instantiation was illegal.
993 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
994 and then not Error_Posted
(Unit_Node
)
995 and then Present
(Instance_Spec
(Unit_Node
))
997 Remove_Unit_From_Visibility
998 (Defining_Entity
(Instance_Spec
(Unit_Node
)));
1000 elsif Nkind
(Unit_Node
) = N_Package_Body
1001 or else (Nkind
(Unit_Node
) = N_Subprogram_Body
1002 and then not Acts_As_Spec
(Unit_Node
))
1004 -- Bodies that are not the main unit are compiled if they are generic
1005 -- or contain generic or inlined units. Their analysis brings in the
1006 -- context of the corresponding spec (unit declaration) which must be
1007 -- removed as well, to return the compilation environment to its
1010 Remove_Context
(Lib_Unit
);
1011 Set_Is_Immediately_Visible
(Defining_Entity
(Unit
(Lib_Unit
)), False);
1014 -- Last step is to deinstall the context we just installed as well as
1015 -- the unit just compiled.
1019 -- When generating code for a non-generic main unit, check that withed
1020 -- generic units have a body if they need it, even if the units have not
1021 -- been instantiated. Force the load of the bodies to produce the proper
1022 -- error if the body is absent. The same applies to GNATprove mode, with
1023 -- the added benefit of capturing global references within the generic.
1024 -- This in turn allows for proper inlining of subprogram bodies without
1025 -- a previous declaration.
1027 if Get_Cunit_Unit_Number
(N
) = Main_Unit
1028 and then ((Operating_Mode
= Generate_Code
and then Expander_Active
)
1030 (Operating_Mode
= Check_Semantics
and then GNATprove_Mode
))
1032 -- Check whether the source for the body of the unit must be included
1033 -- in a standalone library.
1035 Check_Body_Needed_For_SAL
(Cunit_Entity
(Main_Unit
));
1037 -- Indicate that the main unit is now analyzed, to catch possible
1038 -- circularities between it and generic bodies. Remove main unit from
1039 -- visibility. This might seem superfluous, but the main unit must
1040 -- not be visible in the generic body expansions that follow.
1042 Set_Analyzed
(N
, True);
1043 Set_Is_Immediately_Visible
(Cunit_Entity
(Main_Unit
), False);
1048 Un
: Unit_Number_Type
;
1050 Save_Style_Check
: constant Boolean := Style_Check
;
1053 Item
:= First
(Context_Items
(N
));
1054 while Present
(Item
) loop
1056 -- Check for explicit with clause
1058 if Nkind
(Item
) = N_With_Clause
1059 and then not Implicit_With
(Item
)
1061 -- Ada 2005 (AI-50217): Ignore limited-withed units
1063 and then not Limited_Present
(Item
)
1065 Nam
:= Entity
(Name
(Item
));
1067 -- Compile the generic subprogram, unless it is intrinsic or
1068 -- imported so no body is required, or generic package body
1069 -- if the package spec requires a body.
1071 if (Is_Generic_Subprogram
(Nam
)
1072 and then not Is_Intrinsic_Subprogram
(Nam
)
1073 and then not Is_Imported
(Nam
))
1074 or else (Ekind
(Nam
) = E_Generic_Package
1075 and then Unit_Requires_Body
(Nam
))
1077 Style_Check
:= False;
1079 if Present
(Renamed_Object
(Nam
)) then
1085 (Unit_Declaration_Node
1086 (Renamed_Object
(Nam
)))),
1095 Get_Body_Name
(Get_Unit_Name
(Item
)),
1102 if Un
= No_Unit
then
1104 ("body of generic unit& not found", Item
, Nam
);
1107 elsif not Analyzed
(Cunit
(Un
))
1108 and then Un
/= Main_Unit
1109 and then Fatal_Error
(Un
) /= Error_Detected
1111 Style_Check
:= False;
1112 Semantics
(Cunit
(Un
));
1120 -- Restore style checks settings
1122 Style_Check
:= Save_Style_Check
;
1125 -- In GNATprove mode, force the loading of an Interrupt_Priority when
1126 -- processing compilation units with potentially "main" subprograms.
1127 -- This is required for the ceiling priority protocol checks, which
1128 -- are triggered by these subprograms.
1131 and then Nkind_In
(Unit_Node
, N_Function_Instantiation
,
1132 N_Procedure_Instantiation
,
1139 case Nkind
(Unit_Node
) is
1140 when N_Subprogram_Body
=>
1141 Spec
:= Specification
(Unit_Node
);
1143 when N_Subprogram_Instantiation
=>
1145 Subprogram_Specification
(Entity
(Name
(Unit_Node
)));
1148 raise Program_Error
;
1151 pragma Assert
(Nkind
(Spec
) in N_Subprogram_Specification
);
1153 -- Main subprogram must have no parameters, and if it is a
1154 -- function, it must return an integer.
1156 if No
(Parameter_Specifications
(Spec
))
1157 and then (Nkind
(Spec
) = N_Procedure_Specification
1159 Is_Integer_Type
(Etype
(Result_Definition
(Spec
))))
1161 SPARK_Implicit_Load
(RE_Interrupt_Priority
);
1167 -- Deal with creating elaboration counter if needed. We create an
1168 -- elaboration counter only for units that come from source since
1169 -- units manufactured by the compiler never need elab checks.
1171 if Comes_From_Source
(N
)
1172 and then Nkind_In
(Unit_Node
, N_Package_Declaration
,
1173 N_Generic_Package_Declaration
,
1174 N_Subprogram_Declaration
,
1175 N_Generic_Subprogram_Declaration
)
1178 Loc
: constant Source_Ptr
:= Sloc
(N
);
1179 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(Loc
);
1182 Spec_Id
:= Defining_Entity
(Unit_Node
);
1183 Generate_Definition
(Spec_Id
);
1185 -- See if an elaboration entity is required for possible access
1186 -- before elaboration checking. Note that we must allow for this
1187 -- even if -gnatE is not set, since a client may be compiled in
1188 -- -gnatE mode and reference the entity.
1190 -- These entities are also used by the binder to prevent multiple
1191 -- attempts to execute the elaboration code for the library case
1192 -- where the elaboration routine might otherwise be called more
1195 -- They are also needed to ensure explicit visibility from the
1196 -- binder generated code of all the units involved in a partition
1197 -- when control-flow preservation is requested.
1199 -- Case of units which do not require an elaboration entity
1201 if not Opt
.Suppress_Control_Flow_Optimizations
1203 ( -- Pure units do not need checks
1207 -- Preelaborated units do not need checks
1209 or else Is_Preelaborated
(Spec_Id
)
1211 -- No checks needed if pragma Elaborate_Body present
1213 or else Has_Pragma_Elaborate_Body
(Spec_Id
)
1215 -- No checks needed if unit does not require a body
1217 or else not Unit_Requires_Body
(Spec_Id
)
1219 -- No checks needed for predefined files
1221 or else Is_Predefined_Unit
(Unum
)
1223 -- No checks required if no separate spec
1225 or else Acts_As_Spec
(N
)
1228 -- This is a case where we only need the entity for
1229 -- checking to prevent multiple elaboration checks.
1231 Set_Elaboration_Entity_Required
(Spec_Id
, False);
1233 -- Case of elaboration entity is required for access before
1234 -- elaboration checking (so certainly we must build it).
1237 Set_Elaboration_Entity_Required
(Spec_Id
, True);
1240 Build_Elaboration_Entity
(N
, Spec_Id
);
1244 -- Freeze the compilation unit entity. This for sure is needed because
1245 -- of some warnings that can be output (see Freeze_Subprogram), but may
1246 -- in general be required. If freezing actions result, place them in the
1247 -- compilation unit actions list, and analyze them.
1250 L
: constant List_Id
:=
1251 Freeze_Entity
(Cunit_Entity
(Current_Sem_Unit
), N
);
1253 while Is_Non_Empty_List
(L
) loop
1254 Insert_Library_Level_Action
(Remove_Head
(L
));
1260 -- Call Check_Package_Body so that a body containing subprograms with
1261 -- Inline_Always can be made available for front end inlining.
1263 if Nkind
(Unit_Node
) = N_Package_Declaration
1264 and then Get_Cunit_Unit_Number
(N
) /= Main_Unit
1266 -- We don't need to do this if the Expander is not active, since there
1267 -- is no code to inline.
1269 and then Expander_Active
1272 Save_Style_Check
: constant Boolean := Style_Check
;
1273 Save_Warning
: constant Warning_Mode_Type
:= Warning_Mode
;
1274 Options
: Style_Check_Options
;
1277 Save_Style_Check_Options
(Options
);
1278 Reset_Style_Check_Options
;
1279 Opt
.Warning_Mode
:= Suppress
;
1281 Check_Package_Body_For_Inlining
(N
, Defining_Entity
(Unit_Node
));
1283 Reset_Style_Check_Options
;
1284 Set_Style_Check_Options
(Options
);
1285 Style_Check
:= Save_Style_Check
;
1286 Warning_Mode
:= Save_Warning
;
1290 -- If we are generating obsolescent warnings, then here is where we
1291 -- generate them for the with'ed items. The reason for this special
1292 -- processing is that the normal mechanism of generating the warnings
1293 -- for referenced entities does not work for context clause references.
1294 -- That's because when we first analyze the context, it is too early to
1295 -- know if the with'ing unit is itself obsolescent (which suppresses
1299 and then Warn_On_Obsolescent_Feature
1300 and then Nkind
(Unit_Node
) not in N_Generic_Instantiation
1302 -- Push current compilation unit as scope, so that the test for
1303 -- being within an obsolescent unit will work correctly. The check
1304 -- is not performed within an instantiation, because the warning
1305 -- will have been emitted in the corresponding generic unit.
1307 Push_Scope
(Defining_Entity
(Unit_Node
));
1309 -- Loop through context items to deal with with clauses
1317 Item
:= First
(Context_Items
(N
));
1318 while Present
(Item
) loop
1319 if Nkind
(Item
) = N_With_Clause
1321 -- Suppress this check in limited-withed units. Further work
1322 -- needed here if we decide to incorporate this check on
1323 -- limited-withed units.
1325 and then not Limited_Present
(Item
)
1328 Ent
:= Entity
(Nam
);
1330 if Is_Obsolescent
(Ent
) then
1331 Output_Obsolescent_Entity_Warnings
(Nam
, Ent
);
1339 -- Remove temporary install of current unit as scope
1344 -- If No_Elaboration_Code_All was encountered, this is where we do the
1345 -- transitive test of with'ed units to make sure they have the aspect.
1346 -- This is delayed till the end of analyzing the compilation unit to
1347 -- ensure that the pragma/aspect, if present, has been analyzed.
1349 Check_No_Elab_Code_All
(N
);
1350 end Analyze_Compilation_Unit
;
1352 ---------------------
1353 -- Analyze_Context --
1354 ---------------------
1356 procedure Analyze_Context
(N
: Node_Id
) is
1357 Ukind
: constant Node_Kind
:= Nkind
(Unit
(N
));
1361 -- First process all configuration pragmas at the start of the context
1362 -- items. Strictly these are not part of the context clause, but that
1363 -- is where the parser puts them. In any case for sure we must analyze
1364 -- these before analyzing the actual context items, since they can have
1365 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1366 -- be with'ed as a result of changing categorizations in Ada 2005).
1368 Item
:= First
(Context_Items
(N
));
1369 while Present
(Item
)
1370 and then Nkind
(Item
) = N_Pragma
1371 and then Pragma_Name
(Item
) in Configuration_Pragma_Names
1377 -- This is the point at which we capture the configuration settings
1378 -- for the unit. At the moment only the Optimize_Alignment setting
1379 -- needs to be captured. Probably more later ???
1381 if Optimize_Alignment_Local
then
1382 Set_OA_Setting
(Current_Sem_Unit
, 'L');
1384 Set_OA_Setting
(Current_Sem_Unit
, Optimize_Alignment
);
1387 -- Loop through actual context items. This is done in two passes:
1389 -- a) The first pass analyzes non-limited with-clauses and also any
1390 -- configuration pragmas (we need to get the latter analyzed right
1391 -- away, since they can affect processing of subsequent items).
1393 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1395 while Present
(Item
) loop
1397 -- For with clause, analyze the with clause, and then update the
1398 -- version, since we are dependent on a unit that we with.
1400 if Nkind
(Item
) = N_With_Clause
1401 and then not Limited_Present
(Item
)
1403 -- Skip analyzing with clause if no unit, nothing to do (this
1404 -- happens for a with that references a non-existent unit).
1406 if Present
(Library_Unit
(Item
)) then
1408 -- Skip analyzing with clause if this is a with_clause for
1409 -- the main unit, which happens if a subunit has a useless
1410 -- with_clause on its parent.
1412 if Library_Unit
(Item
) /= Cunit
(Current_Sem_Unit
) then
1415 -- Here for the case of a useless with for the main unit
1418 Set_Entity
(Name
(Item
), Cunit_Entity
(Current_Sem_Unit
));
1422 -- Do version update (skipped for implicit with)
1424 if not Implicit_With
(Item
) then
1425 Version_Update
(N
, Library_Unit
(Item
));
1428 -- Skip pragmas. Configuration pragmas at the start were handled in
1429 -- the loop above, and remaining pragmas are not processed until we
1430 -- actually install the context (see Install_Context). We delay the
1431 -- analysis of these pragmas to make sure that we have installed all
1432 -- the implicit with's on parent units.
1434 -- Skip use clauses at this stage, since we don't want to do any
1435 -- installing of potentially use-visible entities until we
1436 -- actually install the complete context (in Install_Context).
1437 -- Otherwise things can get installed in the wrong context.
1446 -- Second pass: examine all limited_with clauses. All other context
1447 -- items are ignored in this pass.
1449 Item
:= First
(Context_Items
(N
));
1450 while Present
(Item
) loop
1451 if Nkind
(Item
) = N_With_Clause
1452 and then Limited_Present
(Item
)
1454 -- No need to check errors on implicitly generated limited-with
1457 if not Implicit_With
(Item
) then
1459 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1460 -- properly rejected, including renaming declarations.
1462 if not Nkind_In
(Ukind
, N_Package_Declaration
,
1463 N_Subprogram_Declaration
)
1464 and then Ukind
not in N_Generic_Declaration
1465 and then Ukind
not in N_Generic_Instantiation
1467 Error_Msg_N
("limited with_clause not allowed here", Item
);
1469 -- Check wrong use of a limited with clause applied to the
1470 -- compilation unit containing the limited-with clause.
1472 -- limited with P.Q;
1473 -- package P.Q is ...
1475 elsif Unit
(Library_Unit
(Item
)) = Unit
(N
) then
1476 Error_Msg_N
("wrong use of limited-with clause", Item
);
1478 -- Check wrong use of limited-with clause applied to some
1479 -- immediate ancestor.
1481 elsif Is_Child_Spec
(Unit
(N
)) then
1483 Lib_U
: constant Entity_Id
:= Unit
(Library_Unit
(Item
));
1487 P
:= Parent_Spec
(Unit
(N
));
1489 if Unit
(P
) = Lib_U
then
1490 Error_Msg_N
("limited with_clause cannot "
1491 & "name ancestor", Item
);
1495 exit when not Is_Child_Spec
(Unit
(P
));
1496 P
:= Parent_Spec
(Unit
(P
));
1501 -- Check if the limited-withed unit is already visible through
1502 -- some context clause of the current compilation unit or some
1503 -- ancestor of the current compilation unit.
1506 Lim_Unit_Name
: constant Node_Id
:= Name
(Item
);
1507 Comp_Unit
: Node_Id
;
1509 Unit_Name
: Node_Id
;
1514 It
:= First
(Context_Items
(Comp_Unit
));
1515 while Present
(It
) loop
1517 and then Nkind
(It
) = N_With_Clause
1518 and then not Limited_Present
(It
)
1520 Nkind_In
(Unit
(Library_Unit
(It
)),
1521 N_Package_Declaration
,
1522 N_Package_Renaming_Declaration
)
1524 if Nkind
(Unit
(Library_Unit
(It
))) =
1525 N_Package_Declaration
1527 Unit_Name
:= Name
(It
);
1529 Unit_Name
:= Name
(Unit
(Library_Unit
(It
)));
1532 -- Check if the named package (or some ancestor)
1533 -- leaves visible the full-view of the unit given
1534 -- in the limited-with clause.
1537 if Designate_Same_Unit
(Lim_Unit_Name
,
1540 Error_Msg_Sloc
:= Sloc
(It
);
1542 ("simultaneous visibility of limited "
1543 & "and unlimited views not allowed",
1546 ("\unlimited view visible through "
1547 & "context clause #",
1551 elsif Nkind
(Unit_Name
) = N_Identifier
then
1555 Unit_Name
:= Prefix
(Unit_Name
);
1562 exit when not Is_Child_Spec
(Unit
(Comp_Unit
));
1564 Comp_Unit
:= Parent_Spec
(Unit
(Comp_Unit
));
1569 -- Skip analyzing with clause if no unit, see above
1571 if Present
(Library_Unit
(Item
)) then
1575 -- A limited_with does not impose an elaboration order, but
1576 -- there is a semantic dependency for recompilation purposes.
1578 if not Implicit_With
(Item
) then
1579 Version_Update
(N
, Library_Unit
(Item
));
1582 -- Pragmas and use clauses and with clauses other than limited
1583 -- with's are ignored in this pass through the context items.
1591 end Analyze_Context
;
1593 -------------------------------
1594 -- Analyze_Package_Body_Stub --
1595 -------------------------------
1597 procedure Analyze_Package_Body_Stub
(N
: Node_Id
) is
1598 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1600 Opts
: Config_Switches_Type
;
1603 -- The package declaration must be in the current declarative part
1605 Check_Stub_Level
(N
);
1606 Nam
:= Current_Entity_In_Scope
(Id
);
1608 if No
(Nam
) or else not Is_Package_Or_Generic_Package
(Nam
) then
1609 Error_Msg_N
("missing specification for package stub", N
);
1611 elsif Has_Completion
(Nam
)
1612 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(Nam
)))
1614 Error_Msg_N
("duplicate or redundant stub for package", N
);
1617 -- Retain and restore the configuration options of the enclosing
1618 -- context as the proper body may introduce a set of its own.
1620 Save_Opt_Config_Switches
(Opts
);
1622 -- Indicate that the body of the package exists. If we are doing
1623 -- only semantic analysis, the stub stands for the body. If we are
1624 -- generating code, the existence of the body will be confirmed
1625 -- when we load the proper body.
1627 Set_Has_Completion
(Nam
);
1628 Set_Scope
(Defining_Entity
(N
), Current_Scope
);
1629 Set_Ekind
(Defining_Entity
(N
), E_Package_Body
);
1630 Set_Corresponding_Spec_Of_Stub
(N
, Nam
);
1631 Generate_Reference
(Nam
, Id
, 'b');
1632 Analyze_Proper_Body
(N
, Nam
);
1634 Restore_Opt_Config_Switches
(Opts
);
1636 end Analyze_Package_Body_Stub
;
1638 -------------------------
1639 -- Analyze_Proper_Body --
1640 -------------------------
1642 procedure Analyze_Proper_Body
(N
: Node_Id
; Nam
: Entity_Id
) is
1643 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
1645 procedure Optional_Subunit
;
1646 -- This procedure is called when the main unit is a stub, or when we
1647 -- are not generating code. In such a case, we analyze the subunit if
1648 -- present, which is user-friendly and in fact required for ASIS, but we
1649 -- don't complain if the subunit is missing. In GNATprove_Mode, we issue
1650 -- an error to avoid formal verification of a partial unit.
1652 ----------------------
1653 -- Optional_Subunit --
1654 ----------------------
1656 procedure Optional_Subunit
is
1657 Comp_Unit
: Node_Id
;
1658 Unum
: Unit_Number_Type
;
1661 -- Try to load subunit, but ignore any errors that occur during the
1662 -- loading of the subunit, by using the special feature in Errout to
1663 -- ignore all errors. Note that Fatal_Error will still be set, so we
1664 -- will be able to check for this case below.
1666 if not (ASIS_Mode
or GNATprove_Mode
) then
1667 Ignore_Errors_Enable
:= Ignore_Errors_Enable
+ 1;
1672 (Load_Name
=> Subunit_Name
,
1673 Required
=> GNATprove_Mode
,
1677 if not (ASIS_Mode
or GNATprove_Mode
) then
1678 Ignore_Errors_Enable
:= Ignore_Errors_Enable
- 1;
1681 -- All done if we successfully loaded the subunit
1684 and then (Fatal_Error
(Unum
) /= Error_Detected
1685 or else Try_Semantics
)
1687 Comp_Unit
:= Cunit
(Unum
);
1689 -- If the file was empty or seriously mangled, the unit itself may
1692 if No
(Unit
(Comp_Unit
)) then
1694 ("subunit does not contain expected proper body", N
);
1696 elsif Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
1698 ("expected SEPARATE subunit, found child unit",
1699 Cunit_Entity
(Unum
));
1701 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
1702 Analyze_Subunit
(Comp_Unit
);
1703 Set_Library_Unit
(N
, Comp_Unit
);
1704 Set_Corresponding_Body
(N
, Defining_Entity
(Unit
(Comp_Unit
)));
1707 elsif Unum
= No_Unit
1708 and then Present
(Nam
)
1710 if Is_Protected_Type
(Nam
) then
1711 Set_Corresponding_Body
(Parent
(Nam
), Defining_Identifier
(N
));
1713 Set_Corresponding_Body
(
1714 Unit_Declaration_Node
(Nam
), Defining_Identifier
(N
));
1717 end Optional_Subunit
;
1721 Comp_Unit
: Node_Id
;
1722 Unum
: Unit_Number_Type
;
1724 -- Start of processing for Analyze_Proper_Body
1727 -- If the subunit is already loaded, it means that the main unit is a
1728 -- subunit, and that the current unit is one of its parents which was
1729 -- being analyzed to provide the needed context for the analysis of the
1730 -- subunit. In this case we analyze the subunit and continue with the
1731 -- parent, without looking at subsequent subunits.
1733 if Is_Loaded
(Subunit_Name
) then
1735 -- If the proper body is already linked to the stub node, the stub is
1736 -- in a generic unit and just needs analyzing.
1738 if Present
(Library_Unit
(N
)) then
1739 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
1741 -- If the subunit has severe errors, the spec of the enclosing
1742 -- body may not be available, in which case do not try analysis.
1744 if Serious_Errors_Detected
> 0
1745 and then No
(Library_Unit
(Library_Unit
(N
)))
1750 -- Collect SCO information for loaded subunit if we are in the
1751 -- extended main unit.
1754 and then In_Extended_Main_Source_Unit
1755 (Cunit_Entity
(Current_Sem_Unit
))
1757 SCO_Record_Raw
(Get_Cunit_Unit_Number
(Library_Unit
(N
)));
1760 Analyze_Subunit
(Library_Unit
(N
));
1762 -- Otherwise we must load the subunit and link to it
1765 -- Load the subunit, this must work, since we originally loaded
1766 -- the subunit earlier on. So this will not really load it, just
1767 -- give access to it.
1771 (Load_Name
=> Subunit_Name
,
1776 -- And analyze the subunit in the parent context (note that we
1777 -- do not call Semantics, since that would remove the parent
1778 -- context). Because of this, we have to manually reset the
1779 -- compiler state to Analyzing since it got destroyed by Load.
1781 if Unum
/= No_Unit
then
1782 Compiler_State
:= Analyzing
;
1784 -- Check that the proper body is a subunit and not a child
1785 -- unit. If the unit was previously loaded, the error will
1786 -- have been emitted when copying the generic node, so we
1787 -- just return to avoid cascaded errors.
1789 if Nkind
(Unit
(Cunit
(Unum
))) /= N_Subunit
then
1793 Set_Corresponding_Stub
(Unit
(Cunit
(Unum
)), N
);
1794 Analyze_Subunit
(Cunit
(Unum
));
1795 Set_Library_Unit
(N
, Cunit
(Unum
));
1799 -- If the main unit is a subunit, then we are just performing semantic
1800 -- analysis on that subunit, and any other subunits of any parent unit
1801 -- should be ignored, except that if we are building trees for ASIS
1802 -- usage we want to annotate the stub properly. If the main unit is
1803 -- itself a subunit, another subunit is irrelevant unless it is a
1804 -- subunit of the current one, that is to say appears in the current
1807 elsif Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
1808 and then Subunit_Name
/= Unit_Name
(Main_Unit
)
1812 PB
: constant Node_Id
:= Proper_Body
(Unit
(Cunit
(Main_Unit
)));
1814 if Nkind_In
(PB
, N_Package_Body
, N_Subprogram_Body
)
1815 and then List_Containing
(N
) = Declarations
(PB
)
1822 -- But before we return, set the flag for unloaded subunits. This
1823 -- will suppress junk warnings of variables in the same declarative
1824 -- part (or a higher level one) that are in danger of looking unused
1825 -- when in fact there might be a declaration in the subunit that we
1826 -- do not intend to load.
1828 Unloaded_Subunits
:= True;
1831 -- If the subunit is not already loaded, and we are generating code,
1832 -- then this is the case where compilation started from the parent, and
1833 -- we are generating code for an entire subunit tree. In that case we
1834 -- definitely need to load the subunit.
1836 -- In order to continue the analysis with the rest of the parent,
1837 -- and other subunits, we load the unit without requiring its
1838 -- presence, and emit a warning if not found, rather than terminating
1839 -- the compilation abruptly, as for other missing file problems.
1841 elsif Original_Operating_Mode
= Generate_Code
then
1843 -- If the proper body is already linked to the stub node, the stub is
1844 -- in a generic unit and just needs analyzing.
1846 -- We update the version. Although we are not strictly technically
1847 -- semantically dependent on the subunit, given our approach of macro
1848 -- substitution of subunits, it makes sense to include it in the
1849 -- version identification.
1851 if Present
(Library_Unit
(N
)) then
1852 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
1853 Analyze_Subunit
(Library_Unit
(N
));
1854 Version_Update
(Cunit
(Main_Unit
), Library_Unit
(N
));
1856 -- Otherwise we must load the subunit and link to it
1859 -- Make sure that, if the subunit is preprocessed and -gnateG is
1860 -- specified, the preprocessed file will be written.
1862 Lib
.Analysing_Subunit_Of_Main
:= True;
1865 (Load_Name
=> Subunit_Name
,
1869 Lib
.Analysing_Subunit_Of_Main
:= False;
1871 -- Give message if we did not get the unit Emit warning even if
1872 -- missing subunit is not within main unit, to simplify debugging.
1874 pragma Assert
(Original_Operating_Mode
= Generate_Code
);
1875 if Unum
= No_Unit
then
1876 Error_Msg_Unit_1
:= Subunit_Name
;
1878 Get_File_Name
(Subunit_Name
, Subunit
=> True);
1880 ("subunit$$ in file{ not found??!!", N
);
1881 Subunits_Missing
:= True;
1884 -- Load_Unit may reset Compiler_State, since it may have been
1885 -- necessary to parse an additional units, so we make sure that
1886 -- we reset it to the Analyzing state.
1888 Compiler_State
:= Analyzing
;
1890 if Unum
/= No_Unit
then
1891 if Debug_Flag_L
then
1892 Write_Str
("*** Loaded subunit from stub. Analyze");
1896 Comp_Unit
:= Cunit
(Unum
);
1898 -- Check for child unit instead of subunit
1900 if Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
1902 ("expected SEPARATE subunit, found child unit",
1903 Cunit_Entity
(Unum
));
1905 -- OK, we have a subunit
1908 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
1909 Set_Library_Unit
(N
, Comp_Unit
);
1911 -- We update the version. Although we are not technically
1912 -- semantically dependent on the subunit, given our approach
1913 -- of macro substitution of subunits, it makes sense to
1914 -- include it in the version identification.
1916 Version_Update
(Cunit
(Main_Unit
), Comp_Unit
);
1918 -- Collect SCO information for loaded subunit if we are in
1919 -- the extended main unit.
1922 and then In_Extended_Main_Source_Unit
1923 (Cunit_Entity
(Current_Sem_Unit
))
1925 SCO_Record_Raw
(Unum
);
1928 -- Analyze the unit if semantics active
1930 if Fatal_Error
(Unum
) /= Error_Detected
1931 or else Try_Semantics
1933 Analyze_Subunit
(Comp_Unit
);
1939 -- The remaining case is when the subunit is not already loaded and we
1940 -- are not generating code. In this case we are just performing semantic
1941 -- analysis on the parent, and we are not interested in the subunit. For
1942 -- subprograms, analyze the stub as a body. For other entities the stub
1943 -- has already been marked as completed.
1948 end Analyze_Proper_Body
;
1950 ----------------------------------
1951 -- Analyze_Protected_Body_Stub --
1952 ----------------------------------
1954 procedure Analyze_Protected_Body_Stub
(N
: Node_Id
) is
1955 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Defining_Identifier
(N
));
1958 Check_Stub_Level
(N
);
1960 -- First occurrence of name may have been as an incomplete type
1962 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
1963 Nam
:= Full_View
(Nam
);
1966 if No
(Nam
) or else not Is_Protected_Type
(Etype
(Nam
)) then
1967 Error_Msg_N
("missing specification for Protected body", N
);
1970 Set_Scope
(Defining_Entity
(N
), Current_Scope
);
1971 Set_Ekind
(Defining_Entity
(N
), E_Protected_Body
);
1972 Set_Has_Completion
(Etype
(Nam
));
1973 Set_Corresponding_Spec_Of_Stub
(N
, Nam
);
1974 Generate_Reference
(Nam
, Defining_Identifier
(N
), 'b');
1975 Analyze_Proper_Body
(N
, Etype
(Nam
));
1977 end Analyze_Protected_Body_Stub
;
1979 ----------------------------------
1980 -- Analyze_Subprogram_Body_Stub --
1981 ----------------------------------
1983 -- A subprogram body stub can appear with or without a previous spec. If
1984 -- there is one, then the analysis of the body will find it and verify
1985 -- conformance. The formals appearing in the specification of the stub play
1986 -- no role, except for requiring an additional conformance check. If there
1987 -- is no previous subprogram declaration, the stub acts as a spec, and
1988 -- provides the defining entity for the subprogram.
1990 procedure Analyze_Subprogram_Body_Stub
(N
: Node_Id
) is
1992 Opts
: Config_Switches_Type
;
1995 Check_Stub_Level
(N
);
1997 -- Verify that the identifier for the stub is unique within this
1998 -- declarative part.
2000 if Nkind_In
(Parent
(N
), N_Block_Statement
,
2004 Decl
:= First
(Declarations
(Parent
(N
)));
2005 while Present
(Decl
) and then Decl
/= N
loop
2006 if Nkind
(Decl
) = N_Subprogram_Body_Stub
2007 and then (Chars
(Defining_Unit_Name
(Specification
(Decl
))) =
2008 Chars
(Defining_Unit_Name
(Specification
(N
))))
2010 Error_Msg_N
("identifier for stub is not unique", N
);
2017 -- Retain and restore the configuration options of the enclosing context
2018 -- as the proper body may introduce a set of its own.
2020 Save_Opt_Config_Switches
(Opts
);
2022 -- Treat stub as a body, which checks conformance if there is a previous
2023 -- declaration, or else introduces entity and its signature.
2025 Analyze_Subprogram_Body
(N
);
2026 Analyze_Proper_Body
(N
, Empty
);
2028 Restore_Opt_Config_Switches
(Opts
);
2029 end Analyze_Subprogram_Body_Stub
;
2031 ---------------------
2032 -- Analyze_Subunit --
2033 ---------------------
2035 -- A subunit is compiled either by itself (for semantic checking) or as
2036 -- part of compiling the parent (for code generation). In either case, by
2037 -- the time we actually process the subunit, the parent has already been
2038 -- installed and analyzed. The node N is a compilation unit, whose context
2039 -- needs to be treated here, because we come directly here from the parent
2040 -- without calling Analyze_Compilation_Unit.
2042 -- The compilation context includes the explicit context of the subunit,
2043 -- and the context of the parent, together with the parent itself. In order
2044 -- to compile the current context, we remove the one inherited from the
2045 -- parent, in order to have a clean visibility table. We restore the parent
2046 -- context before analyzing the proper body itself. On exit, we remove only
2047 -- the explicit context of the subunit.
2049 -- WARNING: This routine manages SPARK regions. Return statements must be
2050 -- replaced by gotos which jump to the end of the routine and restore the
2053 procedure Analyze_Subunit
(N
: Node_Id
) is
2054 Lib_Unit
: constant Node_Id
:= Library_Unit
(N
);
2055 Par_Unit
: constant Entity_Id
:= Current_Scope
;
2057 Lib_Spec
: Node_Id
:= Library_Unit
(Lib_Unit
);
2058 Num_Scopes
: Nat
:= 0;
2059 Use_Clauses
: array (1 .. Scope_Stack
.Last
) of Node_Id
;
2060 Enclosing_Child
: Entity_Id
:= Empty
;
2061 Svg
: constant Suppress_Record
:= Scope_Suppress
;
2063 Save_Cunit_Restrictions
: constant Save_Cunit_Boolean_Restrictions
:=
2064 Cunit_Boolean_Restrictions_Save
;
2065 -- Save non-partition wide restrictions before processing the subunit.
2066 -- All subunits are analyzed with config restrictions reset and we need
2067 -- to restore these saved values at the end.
2069 procedure Analyze_Subunit_Context
;
2070 -- Capture names in use clauses of the subunit. This must be done before
2071 -- re-installing parent declarations, because items in the context must
2072 -- not be hidden by declarations local to the parent.
2074 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
);
2075 -- Recursive procedure to restore scope of all ancestors of subunit,
2076 -- from outermost in. If parent is not a subunit, the call to install
2077 -- context installs context of spec and (if parent is a child unit) the
2078 -- context of its parents as well. It is confusing that parents should
2079 -- be treated differently in both cases, but the semantics are just not
2082 procedure Re_Install_Use_Clauses
;
2083 -- As part of the removal of the parent scope, the use clauses are
2084 -- removed, to be reinstalled when the context of the subunit has been
2085 -- analyzed. Use clauses may also have been affected by the analysis of
2086 -- the context of the subunit, so they have to be applied again, to
2087 -- insure that the compilation environment of the rest of the parent
2088 -- unit is identical.
2090 procedure Remove_Scope
;
2091 -- Remove current scope from scope stack, and preserve the list of use
2092 -- clauses in it, to be reinstalled after context is analyzed.
2094 -----------------------------
2095 -- Analyze_Subunit_Context --
2096 -----------------------------
2098 procedure Analyze_Subunit_Context
is
2100 Unit_Name
: Entity_Id
;
2103 Analyze_Context
(N
);
2104 Check_No_Elab_Code_All
(N
);
2106 -- Make withed units immediately visible. If child unit, make the
2107 -- ultimate parent immediately visible.
2109 Item
:= First
(Context_Items
(N
));
2110 while Present
(Item
) loop
2111 if Nkind
(Item
) = N_With_Clause
then
2113 -- Protect frontend against previous errors in context clauses
2115 if Nkind
(Name
(Item
)) /= N_Selected_Component
then
2116 if Error_Posted
(Item
) then
2120 -- If a subunits has serious syntax errors, the context
2121 -- may not have been loaded. Add a harmless unit name to
2122 -- attempt processing.
2124 if Serious_Errors_Detected
> 0
2125 and then No
(Entity
(Name
(Item
)))
2127 Set_Entity
(Name
(Item
), Standard_Standard
);
2130 Unit_Name
:= Entity
(Name
(Item
));
2132 Set_Is_Visible_Lib_Unit
(Unit_Name
);
2133 exit when Scope
(Unit_Name
) = Standard_Standard
;
2134 Unit_Name
:= Scope
(Unit_Name
);
2136 if No
(Unit_Name
) then
2137 Check_Error_Detected
;
2142 if not Is_Immediately_Visible
(Unit_Name
) then
2143 Set_Is_Immediately_Visible
(Unit_Name
);
2144 Set_Context_Installed
(Item
);
2149 elsif Nkind
(Item
) = N_Use_Package_Clause
then
2150 Analyze
(Name
(Item
));
2152 elsif Nkind
(Item
) = N_Use_Type_Clause
then
2153 Analyze
(Subtype_Mark
(Item
));
2159 -- Reset visibility of withed units. They will be made visible again
2160 -- when we install the subunit context.
2162 Item
:= First
(Context_Items
(N
));
2163 while Present
(Item
) loop
2164 if Nkind
(Item
) = N_With_Clause
2166 -- Protect frontend against previous errors in context clauses
2168 and then Nkind
(Name
(Item
)) /= N_Selected_Component
2169 and then not Error_Posted
(Item
)
2171 Unit_Name
:= Entity
(Name
(Item
));
2173 Set_Is_Visible_Lib_Unit
(Unit_Name
, False);
2174 exit when Scope
(Unit_Name
) = Standard_Standard
;
2175 Unit_Name
:= Scope
(Unit_Name
);
2178 if Context_Installed
(Item
) then
2179 Set_Is_Immediately_Visible
(Unit_Name
, False);
2180 Set_Context_Installed
(Item
, False);
2186 end Analyze_Subunit_Context
;
2188 ------------------------
2189 -- Re_Install_Parents --
2190 ------------------------
2192 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
) is
2196 if Nkind
(Unit
(L
)) = N_Subunit
then
2197 Re_Install_Parents
(Library_Unit
(L
), Scope
(Scop
));
2200 Install_Context
(L
, False);
2202 -- If the subunit occurs within a child unit, we must restore the
2203 -- immediate visibility of any siblings that may occur in context.
2205 if Present
(Enclosing_Child
) then
2206 Install_Siblings
(Enclosing_Child
, L
);
2211 if Scop
/= Par_Unit
then
2212 Set_Is_Immediately_Visible
(Scop
);
2215 -- Make entities in scope visible again. For child units, restore
2216 -- visibility only if they are actually in context.
2218 E
:= First_Entity
(Current_Scope
);
2219 while Present
(E
) loop
2220 if not Is_Child_Unit
(E
) or else Is_Visible_Lib_Unit
(E
) then
2221 Set_Is_Immediately_Visible
(E
);
2227 -- A subunit appears within a body, and for a nested subunits all the
2228 -- parents are bodies. Restore full visibility of their private
2231 if Is_Package_Or_Generic_Package
(Scop
) then
2232 Set_In_Package_Body
(Scop
);
2233 Install_Private_Declarations
(Scop
);
2235 end Re_Install_Parents
;
2237 ----------------------------
2238 -- Re_Install_Use_Clauses --
2239 ----------------------------
2241 procedure Re_Install_Use_Clauses
is
2244 for J
in reverse 1 .. Num_Scopes
loop
2245 U
:= Use_Clauses
(J
);
2246 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:= U
;
2247 Install_Use_Clauses
(U
);
2249 end Re_Install_Use_Clauses
;
2255 procedure Remove_Scope
is
2259 Num_Scopes
:= Num_Scopes
+ 1;
2260 Use_Clauses
(Num_Scopes
) :=
2261 Scope_Stack
.Table
(Scope_Stack
.Last
).First_Use_Clause
;
2263 E
:= First_Entity
(Current_Scope
);
2264 while Present
(E
) loop
2265 Set_Is_Immediately_Visible
(E
, False);
2269 if Is_Child_Unit
(Current_Scope
) then
2270 Enclosing_Child
:= Current_Scope
;
2276 Saved_SM
: SPARK_Mode_Type
:= SPARK_Mode
;
2277 Saved_SMP
: Node_Id
:= SPARK_Mode_Pragma
;
2278 -- Save the SPARK mode-related data to restore on exit. Removing
2279 -- enclosing scopes and contexts to provide a clean environment for the
2280 -- context of the subunit will eliminate any previously set SPARK_Mode.
2282 -- Start of processing for Analyze_Subunit
2285 -- For subunit in main extended unit, we reset the configuration values
2286 -- for the non-partition-wide restrictions. For other units reset them.
2288 if In_Extended_Main_Source_Unit
(N
) then
2289 Restore_Config_Cunit_Boolean_Restrictions
;
2291 Reset_Cunit_Boolean_Restrictions
;
2296 Nam
: Node_Id
:= Name
(Unit
(N
));
2299 if Nkind
(Nam
) = N_Selected_Component
then
2300 Nam
:= Selector_Name
(Nam
);
2303 Check_Identifier
(Nam
, Par_Unit
);
2307 if not Is_Empty_List
(Context_Items
(N
)) then
2309 -- Save current use clauses
2312 Remove_Context
(Lib_Unit
);
2314 -- Now remove parents and their context, including enclosing subunits
2315 -- and the outer parent body which is not a subunit.
2317 if Present
(Lib_Spec
) then
2318 Remove_Context
(Lib_Spec
);
2320 while Nkind
(Unit
(Lib_Spec
)) = N_Subunit
loop
2321 Lib_Spec
:= Library_Unit
(Lib_Spec
);
2323 Remove_Context
(Lib_Spec
);
2326 if Nkind
(Unit
(Lib_Unit
)) = N_Subunit
then
2330 if Nkind
(Unit
(Lib_Spec
)) = N_Package_Body
then
2331 Remove_Context
(Library_Unit
(Lib_Spec
));
2335 Set_Is_Immediately_Visible
(Par_Unit
, False);
2337 Analyze_Subunit_Context
;
2339 -- Take into account the effect of any SPARK_Mode configuration
2340 -- pragma, which takes precedence over a different value of
2341 -- SPARK_Mode inherited from the context of the stub.
2343 if SPARK_Mode
/= None
then
2344 Saved_SM
:= SPARK_Mode
;
2345 Saved_SMP
:= SPARK_Mode_Pragma
;
2348 Re_Install_Parents
(Lib_Unit
, Par_Unit
);
2349 Set_Is_Immediately_Visible
(Par_Unit
);
2351 -- If the context includes a child unit of the parent of the subunit,
2352 -- the parent will have been removed from visibility, after compiling
2353 -- that cousin in the context. The visibility of the parent must be
2354 -- restored now. This also applies if the context includes another
2355 -- subunit of the same parent which in turn includes a child unit in
2358 if Is_Package_Or_Generic_Package
(Par_Unit
) then
2359 if not Is_Immediately_Visible
(Par_Unit
)
2360 or else (Present
(First_Entity
(Par_Unit
))
2362 Is_Immediately_Visible
(First_Entity
(Par_Unit
)))
2364 Set_Is_Immediately_Visible
(Par_Unit
);
2365 Install_Visible_Declarations
(Par_Unit
);
2366 Install_Private_Declarations
(Par_Unit
);
2370 Re_Install_Use_Clauses
;
2371 Install_Context
(N
, Chain
=> False);
2373 -- Restore state of suppress flags for current body
2375 Scope_Suppress
:= Svg
;
2377 -- If the subunit is within a child unit, then siblings of any parent
2378 -- unit that appear in the context clause of the subunit must also be
2379 -- made immediately visible.
2381 if Present
(Enclosing_Child
) then
2382 Install_Siblings
(Enclosing_Child
, N
);
2386 Generate_Parent_References
(Unit
(N
), Par_Unit
);
2388 -- Reinstall the SPARK_Mode which was in effect prior to any scope and
2389 -- context manipulations, taking into account a possible SPARK_Mode
2390 -- configuration pragma if present.
2392 Install_SPARK_Mode
(Saved_SM
, Saved_SMP
);
2394 Analyze
(Proper_Body
(Unit
(N
)));
2397 -- The subunit may contain a with_clause on a sibling of some ancestor.
2398 -- Removing the context will remove from visibility those ancestor child
2399 -- units, which must be restored to the visibility they have in the
2402 if Present
(Enclosing_Child
) then
2407 while Present
(C
) and then C
/= Standard_Standard
loop
2408 Set_Is_Immediately_Visible
(C
);
2409 Set_Is_Visible_Lib_Unit
(C
);
2415 -- Deal with restore of restrictions
2417 Cunit_Boolean_Restrictions_Restore
(Save_Cunit_Restrictions
);
2418 end Analyze_Subunit
;
2420 ----------------------------
2421 -- Analyze_Task_Body_Stub --
2422 ----------------------------
2424 procedure Analyze_Task_Body_Stub
(N
: Node_Id
) is
2425 Loc
: constant Source_Ptr
:= Sloc
(N
);
2426 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Defining_Identifier
(N
));
2429 Check_Stub_Level
(N
);
2431 -- First occurrence of name may have been as an incomplete type
2433 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
2434 Nam
:= Full_View
(Nam
);
2437 if No
(Nam
) or else not Is_Task_Type
(Etype
(Nam
)) then
2438 Error_Msg_N
("missing specification for task body", N
);
2441 Set_Scope
(Defining_Entity
(N
), Current_Scope
);
2442 Set_Ekind
(Defining_Entity
(N
), E_Task_Body
);
2443 Generate_Reference
(Nam
, Defining_Identifier
(N
), 'b');
2444 Set_Corresponding_Spec_Of_Stub
(N
, Nam
);
2446 -- Check for duplicate stub, if so give message and terminate
2448 if Has_Completion
(Etype
(Nam
)) then
2449 Error_Msg_N
("duplicate stub for task", N
);
2452 Set_Has_Completion
(Etype
(Nam
));
2455 Analyze_Proper_Body
(N
, Etype
(Nam
));
2457 -- Set elaboration flag to indicate that entity is callable. This
2458 -- cannot be done in the expansion of the body itself, because the
2459 -- proper body is not in a declarative part. This is only done if
2460 -- expansion is active, because the context may be generic and the
2461 -- flag not defined yet.
2463 if Expander_Active
then
2465 Make_Assignment_Statement
(Loc
,
2467 Make_Identifier
(Loc
,
2468 Chars
=> New_External_Name
(Chars
(Etype
(Nam
)), 'E')),
2469 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
2472 end Analyze_Task_Body_Stub
;
2474 -------------------------
2475 -- Analyze_With_Clause --
2476 -------------------------
2478 -- Analyze the declaration of a unit in a with clause. At end, label the
2479 -- with clause with the defining entity for the unit.
2481 procedure Analyze_With_Clause
(N
: Node_Id
) is
2483 -- Retrieve the original kind of the unit node, before analysis. If it
2484 -- is a subprogram instantiation, its analysis below will rewrite the
2485 -- node as the declaration of the wrapper package. If the same
2486 -- instantiation appears indirectly elsewhere in the context, it will
2487 -- have been analyzed already.
2489 Unit_Kind
: constant Node_Kind
:=
2490 Nkind
(Original_Node
(Unit
(Library_Unit
(N
))));
2491 Nam
: constant Node_Id
:= Name
(N
);
2493 Par_Name
: Entity_Id
;
2498 -- Set True if the unit currently being compiled is an internal unit
2500 Restriction_Violation
: Boolean := False;
2501 -- Set True if a with violates a restriction, no point in giving any
2502 -- warnings if we have this definite error.
2504 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
2507 U
:= Unit
(Library_Unit
(N
));
2509 -- If this is an internal unit which is a renaming, then this is a
2510 -- violation of No_Obsolescent_Features.
2512 -- Note: this is not quite right if the user defines one of these units
2513 -- himself, but that's a marginal case, and fixing it is hard ???
2515 if Restriction_Check_Required
(No_Obsolescent_Features
) then
2516 if In_Predefined_Renaming
(U
) then
2517 Check_Restriction
(No_Obsolescent_Features
, N
);
2518 Restriction_Violation
:= True;
2522 -- Check No_Implementation_Units violation
2524 if Restriction_Check_Required
(No_Implementation_Units
) then
2525 if Not_Impl_Defined_Unit
(Get_Source_Unit
(U
)) then
2528 Check_Restriction
(No_Implementation_Units
, Nam
);
2529 Restriction_Violation
:= True;
2533 -- Several actions are skipped for dummy packages (those supplied for
2534 -- with's where no matching file could be found). Such packages are
2535 -- identified by the Sloc value being set to No_Location.
2537 if Limited_Present
(N
) then
2539 -- Ada 2005 (AI-50217): Build visibility structures but do not
2540 -- analyze the unit.
2542 -- If the designated unit is a predefined unit, which might be used
2543 -- implicitly through the rtsfind machinery, a limited with clause
2544 -- on such a unit is usually pointless, because run-time units are
2545 -- unlikely to appear in mutually dependent units, and because this
2546 -- disables the rtsfind mechanism. We transform such limited with
2547 -- clauses into regular with clauses.
2549 if Sloc
(U
) /= No_Location
then
2550 if In_Predefined_Unit
(U
)
2552 -- In ASIS mode the rtsfind mechanism plays no role, and
2553 -- we need to maintain the original tree structure, so
2554 -- this transformation is not performed in this case.
2556 and then not ASIS_Mode
2558 Set_Limited_Present
(N
, False);
2559 Analyze_With_Clause
(N
);
2561 Build_Limited_Views
(N
);
2568 -- If we are compiling under "don't quit" mode (-gnatq) and we have
2569 -- already detected serious errors then we mark the with-clause nodes as
2570 -- analyzed before the corresponding compilation unit is analyzed. This
2571 -- is done here to protect the frontend against never ending recursion
2572 -- caused by circularities in the sources (because the previous errors
2573 -- may break the regular machine of the compiler implemented in
2574 -- Load_Unit to detect circularities).
2576 if Serious_Errors_Detected
> 0 and then Try_Semantics
then
2580 Semantics
(Library_Unit
(N
));
2582 Intunit
:= Is_Internal_Unit
(Current_Sem_Unit
);
2584 if Sloc
(U
) /= No_Location
then
2586 -- Check restrictions, except that we skip the check if this is an
2587 -- internal unit unless we are compiling the internal unit as the
2588 -- main unit. We also skip this for dummy packages.
2590 Check_Restriction_No_Dependence
(Nam
, N
);
2592 if not Intunit
or else Current_Sem_Unit
= Main_Unit
then
2593 Check_Restricted_Unit
(Unit_Name
(Get_Source_Unit
(U
)), N
);
2596 -- Deal with special case of GNAT.Current_Exceptions which interacts
2597 -- with the optimization of local raise statements into gotos.
2599 if Nkind
(Nam
) = N_Selected_Component
2600 and then Nkind
(Prefix
(Nam
)) = N_Identifier
2601 and then Chars
(Prefix
(Nam
)) = Name_Gnat
2602 and then Nam_In
(Chars
(Selector_Name
(Nam
)),
2603 Name_Most_Recent_Exception
,
2604 Name_Exception_Traces
)
2606 Check_Restriction
(No_Exception_Propagation
, N
);
2607 Special_Exception_Package_Used
:= True;
2610 -- Check for inappropriate with of internal implementation unit if we
2611 -- are not compiling an internal unit and also check for withing unit
2612 -- in wrong version of Ada. Do not issue these messages for implicit
2613 -- with's generated by the compiler itself.
2615 if Implementation_Unit_Warnings
2616 and then not Intunit
2617 and then not Implicit_With
(N
)
2618 and then not Restriction_Violation
2621 U_Kind
: constant Kind_Of_Unit
:=
2622 Get_Kind_Of_Unit
(Get_Source_Unit
(U
));
2625 if U_Kind
= Implementation_Unit
then
2626 Error_Msg_F
("& is an internal 'G'N'A'T unit?i?", Name
(N
));
2628 -- Add alternative name if available, otherwise issue a
2629 -- general warning message.
2631 if Error_Msg_Strlen
/= 0 then
2632 Error_Msg_F
("\use ""~"" instead?i?", Name
(N
));
2635 ("\use of this unit is non-portable " &
2636 "and version-dependent?i?", Name
(N
));
2639 elsif U_Kind
= Ada_2005_Unit
2640 and then Ada_Version
< Ada_2005
2641 and then Warn_On_Ada_2005_Compatibility
2643 Error_Msg_N
("& is an Ada 2005 unit?i?", Name
(N
));
2645 elsif U_Kind
= Ada_2012_Unit
2646 and then Ada_Version
< Ada_2012
2647 and then Warn_On_Ada_2012_Compatibility
2649 Error_Msg_N
("& is an Ada 2012 unit?i?", Name
(N
));
2655 -- Semantic analysis of a generic unit is performed on a copy of
2656 -- the original tree. Retrieve the entity on which semantic info
2657 -- actually appears.
2659 if Unit_Kind
in N_Generic_Declaration
then
2660 E_Name
:= Defining_Entity
(U
);
2662 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2663 -- the case of an instantiation, semantic analysis above will have
2664 -- replaced the unit by its instantiated version. If the instance body
2665 -- has been generated, the instance now denotes the body entity. For
2666 -- visibility purposes we need the entity of its spec.
2668 elsif (Unit_Kind
= N_Package_Instantiation
2669 or else Nkind
(Original_Node
(Unit
(Library_Unit
(N
)))) =
2670 N_Package_Instantiation
)
2671 and then Nkind
(U
) = N_Package_Body
2673 E_Name
:= Corresponding_Spec
(U
);
2675 elsif Unit_Kind
= N_Package_Instantiation
2676 and then Nkind
(U
) = N_Package_Instantiation
2677 and then Present
(Instance_Spec
(U
))
2679 -- If the instance has not been rewritten as a package declaration,
2680 -- then it appeared already in a previous with clause. Retrieve
2681 -- the entity from the previous instance.
2683 E_Name
:= Defining_Entity
(Specification
(Instance_Spec
(U
)));
2685 elsif Unit_Kind
in N_Subprogram_Instantiation
then
2687 -- The visible subprogram is created during instantiation, and is
2688 -- an attribute of the wrapper package. We retrieve the wrapper
2689 -- package directly from the instantiation node. If the instance
2690 -- is inlined the unit is still an instantiation. Otherwise it has
2691 -- been rewritten as the declaration of the wrapper itself.
2693 if Nkind
(U
) in N_Subprogram_Instantiation
then
2696 (Defining_Entity
(Specification
(Instance_Spec
(U
))));
2698 E_Name
:= Related_Instance
(Defining_Entity
(U
));
2701 elsif Unit_Kind
= N_Package_Renaming_Declaration
2702 or else Unit_Kind
in N_Generic_Renaming_Declaration
2704 E_Name
:= Defining_Entity
(U
);
2706 elsif Unit_Kind
= N_Subprogram_Body
2707 and then Nkind
(Name
(N
)) = N_Selected_Component
2708 and then not Acts_As_Spec
(Library_Unit
(N
))
2710 -- For a child unit that has no spec, one has been created and
2711 -- analyzed. The entity required is that of the spec.
2713 E_Name
:= Corresponding_Spec
(U
);
2716 E_Name
:= Defining_Entity
(U
);
2719 if Nkind
(Name
(N
)) = N_Selected_Component
then
2721 -- Child unit in a with clause
2723 Change_Selected_Component_To_Expanded_Name
(Name
(N
));
2725 -- If this is a child unit without a spec, and it has been analyzed
2726 -- already, a declaration has been created for it. The with_clause
2727 -- must reflect the actual body, and not the generated declaration,
2728 -- to prevent spurious binding errors involving an out-of-date spec.
2729 -- Note that this can only happen if the unit includes more than one
2730 -- with_clause for the child unit (e.g. in separate subunits).
2732 if Unit_Kind
= N_Subprogram_Declaration
2733 and then Analyzed
(Library_Unit
(N
))
2734 and then not Comes_From_Source
(Library_Unit
(N
))
2736 Set_Library_Unit
(N
,
2737 Cunit
(Get_Source_Unit
(Corresponding_Body
(U
))));
2741 -- Restore style checks
2743 Style_Check
:= Save_Style_Check
;
2745 -- Record the reference, but do NOT set the unit as referenced, we want
2746 -- to consider the unit as unreferenced if this is the only reference
2749 Set_Entity_With_Checks
(Name
(N
), E_Name
);
2750 Generate_Reference
(E_Name
, Name
(N
), 'w', Set_Ref
=> False);
2752 -- Generate references and check No_Dependence restriction for parents
2754 if Is_Child_Unit
(E_Name
) then
2755 Pref
:= Prefix
(Name
(N
));
2756 Par_Name
:= Scope
(E_Name
);
2757 while Nkind
(Pref
) = N_Selected_Component
loop
2758 Change_Selected_Component_To_Expanded_Name
(Pref
);
2760 if Present
(Entity
(Selector_Name
(Pref
)))
2762 Present
(Renamed_Entity
(Entity
(Selector_Name
(Pref
))))
2763 and then Entity
(Selector_Name
(Pref
)) /= Par_Name
2765 -- The prefix is a child unit that denotes a renaming declaration.
2766 -- Replace the prefix directly with the renamed unit, because the
2767 -- rest of the prefix is irrelevant to the visibility of the real
2770 Rewrite
(Pref
, New_Occurrence_Of
(Par_Name
, Sloc
(Pref
)));
2774 Set_Entity_With_Checks
(Pref
, Par_Name
);
2776 Generate_Reference
(Par_Name
, Pref
);
2777 Check_Restriction_No_Dependence
(Pref
, N
);
2778 Pref
:= Prefix
(Pref
);
2780 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2781 -- is set to Standard_Standard, and no attempt should be made to
2782 -- further unwind scopes.
2784 if Par_Name
/= Standard_Standard
then
2785 Par_Name
:= Scope
(Par_Name
);
2788 -- Abandon processing in case of previous errors
2790 if No
(Par_Name
) then
2791 Check_Error_Detected
;
2796 if Present
(Entity
(Pref
))
2797 and then not Analyzed
(Parent
(Parent
(Entity
(Pref
))))
2799 -- If the entity is set without its unit being compiled, the
2800 -- original parent is a renaming, and Par_Name is the renamed
2801 -- entity. For visibility purposes, we need the original entity,
2802 -- which must be analyzed now because Load_Unit directly retrieves
2803 -- the renamed unit, and the renaming declaration itself has not
2806 Analyze
(Parent
(Parent
(Entity
(Pref
))));
2807 pragma Assert
(Renamed_Object
(Entity
(Pref
)) = Par_Name
);
2808 Par_Name
:= Entity
(Pref
);
2811 -- Guard against missing or misspelled child units
2813 if Present
(Par_Name
) then
2814 Set_Entity_With_Checks
(Pref
, Par_Name
);
2815 Generate_Reference
(Par_Name
, Pref
);
2818 pragma Assert
(Serious_Errors_Detected
/= 0);
2820 -- Mark the node to indicate that a related error has been posted.
2821 -- This defends further compilation passes against improper use of
2822 -- the invalid WITH clause node.
2824 Set_Error_Posted
(N
);
2825 Set_Name
(N
, Error
);
2830 -- If the withed unit is System, and a system extension pragma is
2831 -- present, compile the extension now, rather than waiting for a
2832 -- visibility check on a specific entity.
2834 if Chars
(E_Name
) = Name_System
2835 and then Scope
(E_Name
) = Standard_Standard
2836 and then Present
(System_Extend_Unit
)
2837 and then Present_System_Aux
(N
)
2839 -- If the extension is not present, an error will have been emitted
2844 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2845 -- to private_with units; they will be made visible later (just before
2846 -- the private part is analyzed)
2848 if Private_Present
(N
) then
2849 Set_Is_Immediately_Visible
(E_Name
, False);
2852 -- Propagate Fatal_Error setting from with'ed unit to current unit
2854 case Fatal_Error
(Get_Source_Unit
(Library_Unit
(N
))) is
2856 -- Nothing to do if with'ed unit had no error
2861 -- If with'ed unit had a detected fatal error, propagate it
2863 when Error_Detected
=>
2864 Set_Fatal_Error
(Current_Sem_Unit
, Error_Detected
);
2866 -- If with'ed unit had an ignored error, then propagate it but do not
2867 -- overide an existring setting.
2869 when Error_Ignored
=>
2870 if Fatal_Error
(Current_Sem_Unit
) = None
then
2871 Set_Fatal_Error
(Current_Sem_Unit
, Error_Ignored
);
2875 Mark_Ghost_Clause
(N
);
2876 end Analyze_With_Clause
;
2878 ------------------------------
2879 -- Check_Private_Child_Unit --
2880 ------------------------------
2882 procedure Check_Private_Child_Unit
(N
: Node_Id
) is
2883 Lib_Unit
: constant Node_Id
:= Unit
(N
);
2885 Curr_Unit
: Entity_Id
;
2886 Sub_Parent
: Node_Id
;
2887 Priv_Child
: Entity_Id
;
2888 Par_Lib
: Entity_Id
;
2891 function Is_Private_Library_Unit
(Unit
: Entity_Id
) return Boolean;
2892 -- Returns true if and only if the library unit is declared with
2893 -- an explicit designation of private.
2895 -----------------------------
2896 -- Is_Private_Library_Unit --
2897 -----------------------------
2899 function Is_Private_Library_Unit
(Unit
: Entity_Id
) return Boolean is
2900 Comp_Unit
: constant Node_Id
:= Parent
(Unit_Declaration_Node
(Unit
));
2903 return Private_Present
(Comp_Unit
);
2904 end Is_Private_Library_Unit
;
2906 -- Start of processing for Check_Private_Child_Unit
2909 if Nkind_In
(Lib_Unit
, N_Package_Body
, N_Subprogram_Body
) then
2910 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(N
)));
2911 Par_Lib
:= Curr_Unit
;
2913 elsif Nkind
(Lib_Unit
) = N_Subunit
then
2915 -- The parent is itself a body. The parent entity is to be found in
2916 -- the corresponding spec.
2918 Sub_Parent
:= Library_Unit
(N
);
2919 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(Sub_Parent
)));
2921 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2922 -- enclosing body, retrieve the spec entity which is the proper
2923 -- ancestor we need for the following tests.
2925 if Ekind
(Curr_Unit
) = E_Package_Body
then
2926 Curr_Unit
:= Spec_Entity
(Curr_Unit
);
2929 Par_Lib
:= Curr_Unit
;
2932 Curr_Unit
:= Defining_Entity
(Lib_Unit
);
2934 Par_Lib
:= Curr_Unit
;
2935 Par_Spec
:= Parent_Spec
(Lib_Unit
);
2937 if No
(Par_Spec
) then
2940 Par_Lib
:= Defining_Entity
(Unit
(Par_Spec
));
2944 -- Loop through context items
2946 Item
:= First
(Context_Items
(N
));
2947 while Present
(Item
) loop
2949 -- Ada 2005 (AI-262): Allow private_with of a private child package
2950 -- in public siblings
2952 if Nkind
(Item
) = N_With_Clause
2953 and then not Implicit_With
(Item
)
2954 and then not Limited_Present
(Item
)
2955 and then Is_Private_Descendant
(Entity
(Name
(Item
)))
2957 Priv_Child
:= Entity
(Name
(Item
));
2960 Curr_Parent
: Entity_Id
:= Par_Lib
;
2961 Child_Parent
: Entity_Id
:= Scope
(Priv_Child
);
2962 Prv_Ancestor
: Entity_Id
:= Child_Parent
;
2963 Curr_Private
: Boolean := Is_Private_Library_Unit
(Curr_Unit
);
2966 -- If the child unit is a public child then locate the nearest
2967 -- private ancestor. Child_Parent will then be set to the
2968 -- parent of that ancestor.
2970 if not Is_Private_Library_Unit
(Priv_Child
) then
2971 while Present
(Prv_Ancestor
)
2972 and then not Is_Private_Library_Unit
(Prv_Ancestor
)
2974 Prv_Ancestor
:= Scope
(Prv_Ancestor
);
2977 if Present
(Prv_Ancestor
) then
2978 Child_Parent
:= Scope
(Prv_Ancestor
);
2982 while Present
(Curr_Parent
)
2983 and then Curr_Parent
/= Standard_Standard
2984 and then Curr_Parent
/= Child_Parent
2987 Curr_Private
or else Is_Private_Library_Unit
(Curr_Parent
);
2988 Curr_Parent
:= Scope
(Curr_Parent
);
2991 if No
(Curr_Parent
) then
2992 Curr_Parent
:= Standard_Standard
;
2995 if Curr_Parent
/= Child_Parent
then
2996 if Ekind
(Priv_Child
) = E_Generic_Package
2997 and then Chars
(Priv_Child
) in Text_IO_Package_Name
2998 and then Chars
(Scope
(Scope
(Priv_Child
))) = Name_Ada
3001 ("& is a nested package, not a compilation unit",
3002 Name
(Item
), Priv_Child
);
3006 ("unit in with clause is private child unit!", Item
);
3008 ("\current unit must also have parent&!",
3009 Item
, Child_Parent
);
3013 or else Private_Present
(Item
)
3014 or else Nkind_In
(Lib_Unit
, N_Package_Body
, N_Subunit
)
3015 or else (Nkind
(Lib_Unit
) = N_Subprogram_Body
3016 and then not Acts_As_Spec
(Parent
(Lib_Unit
)))
3022 ("current unit must also be private descendant of&",
3023 Item
, Child_Parent
);
3031 end Check_Private_Child_Unit
;
3033 ----------------------
3034 -- Check_Stub_Level --
3035 ----------------------
3037 procedure Check_Stub_Level
(N
: Node_Id
) is
3038 Par
: constant Node_Id
:= Parent
(N
);
3039 Kind
: constant Node_Kind
:= Nkind
(Par
);
3042 if Nkind_In
(Kind
, N_Package_Body
,
3046 and then Nkind_In
(Parent
(Par
), N_Compilation_Unit
, N_Subunit
)
3050 -- In an instance, a missing stub appears at any level. A warning
3051 -- message will have been emitted already for the missing file.
3053 elsif not In_Instance
then
3054 Error_Msg_N
("stub cannot appear in an inner scope", N
);
3056 elsif Expander_Active
then
3057 Error_Msg_N
("missing proper body", N
);
3059 end Check_Stub_Level
;
3061 ------------------------
3062 -- Expand_With_Clause --
3063 ------------------------
3065 procedure Expand_With_Clause
(Item
: Node_Id
; Nam
: Node_Id
; N
: Node_Id
) is
3066 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
3067 Ent
: constant Entity_Id
:= Entity
(Nam
);
3071 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
;
3072 -- Build name to be used in implicit with_clause. In most cases this
3073 -- is the source name, but if renamings are present we must make the
3074 -- original unit visible, not the one it renames. The entity in the
3075 -- with clause is the renamed unit, but the identifier is the one from
3076 -- the source, which allows us to recover the unit renaming.
3078 ---------------------
3079 -- Build_Unit_Name --
3080 ---------------------
3082 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
is
3087 if Nkind
(Nam
) = N_Identifier
then
3088 return New_Occurrence_Of
(Entity
(Nam
), Loc
);
3091 Ent
:= Entity
(Nam
);
3093 if Present
(Entity
(Selector_Name
(Nam
)))
3094 and then Chars
(Entity
(Selector_Name
(Nam
))) /= Chars
(Ent
)
3096 Nkind
(Unit_Declaration_Node
(Entity
(Selector_Name
(Nam
))))
3097 = N_Package_Renaming_Declaration
3099 -- The name in the with_clause is of the form A.B.C, and B is
3100 -- given by a renaming declaration. In that case we may not
3101 -- have analyzed the unit for B, but replaced it directly in
3102 -- lib-load with the unit it renames. We have to make A.B
3103 -- visible, so analyze the declaration for B now, in case it
3104 -- has not been done yet.
3106 Ent
:= Entity
(Selector_Name
(Nam
));
3109 (Unit_Declaration_Node
(Entity
(Selector_Name
(Nam
)))));
3113 Make_Expanded_Name
(Loc
,
3114 Chars
=> Chars
(Entity
(Nam
)),
3115 Prefix
=> Build_Unit_Name
(Prefix
(Nam
)),
3116 Selector_Name
=> New_Occurrence_Of
(Ent
, Loc
));
3117 Set_Entity
(Result
, Ent
);
3120 end Build_Unit_Name
;
3122 -- Start of processing for Expand_With_Clause
3126 Make_With_Clause
(Loc
,
3127 Name
=> Build_Unit_Name
(Nam
));
3129 P
:= Parent
(Unit_Declaration_Node
(Ent
));
3130 Set_Library_Unit
(Withn
, P
);
3131 Set_Corresponding_Spec
(Withn
, Ent
);
3132 Set_First_Name
(Withn
, True);
3133 Set_Implicit_With
(Withn
, True);
3135 -- If the unit is a package or generic package declaration, a private_
3136 -- with_clause on a child unit implies that the implicit with on the
3137 -- parent is also private.
3139 if Nkind_In
(Unit
(N
), N_Package_Declaration
,
3140 N_Generic_Package_Declaration
)
3142 Set_Private_Present
(Withn
, Private_Present
(Item
));
3145 Prepend
(Withn
, Context_Items
(N
));
3146 Mark_Rewrite_Insertion
(Withn
);
3147 Install_Withed_Unit
(Withn
);
3149 -- If we have "with X.Y;", we want to recurse on "X", except in the
3150 -- unusual case where X.Y is a renaming of X. In that case, the scope
3151 -- of X will be null.
3153 if Nkind
(Nam
) = N_Expanded_Name
3154 and then Present
(Scope
(Entity
(Prefix
(Nam
))))
3156 Expand_With_Clause
(Item
, Prefix
(Nam
), N
);
3158 end Expand_With_Clause
;
3160 --------------------------------
3161 -- Generate_Parent_References --
3162 --------------------------------
3164 procedure Generate_Parent_References
(N
: Node_Id
; P_Id
: Entity_Id
) is
3166 P_Name
: Entity_Id
:= P_Id
;
3169 if Nkind
(N
) = N_Subunit
then
3172 Pref
:= Name
(Parent
(Defining_Entity
(N
)));
3175 if Nkind
(Pref
) = N_Expanded_Name
then
3177 -- Done already, if the unit has been compiled indirectly as
3178 -- part of the closure of its context because of inlining.
3183 while Nkind
(Pref
) = N_Selected_Component
loop
3184 Change_Selected_Component_To_Expanded_Name
(Pref
);
3185 Set_Entity
(Pref
, P_Name
);
3186 Set_Etype
(Pref
, Etype
(P_Name
));
3187 Generate_Reference
(P_Name
, Pref
, 'r');
3188 Pref
:= Prefix
(Pref
);
3189 P_Name
:= Scope
(P_Name
);
3192 -- The guard here on P_Name is to handle the error condition where
3193 -- the parent unit is missing because the file was not found.
3195 if Present
(P_Name
) then
3196 Set_Entity
(Pref
, P_Name
);
3197 Set_Etype
(Pref
, Etype
(P_Name
));
3198 Generate_Reference
(P_Name
, Pref
, 'r');
3199 Style
.Check_Identifier
(Pref
, P_Name
);
3201 end Generate_Parent_References
;
3203 ---------------------
3204 -- Has_With_Clause --
3205 ---------------------
3207 function Has_With_Clause
3210 Is_Limited
: Boolean := False) return Boolean
3214 function Named_Unit
(Clause
: Node_Id
) return Entity_Id
;
3215 -- Return the entity for the unit named in a [limited] with clause
3221 function Named_Unit
(Clause
: Node_Id
) return Entity_Id
is
3223 if Nkind
(Name
(Clause
)) = N_Selected_Component
then
3224 return Entity
(Selector_Name
(Name
(Clause
)));
3226 return Entity
(Name
(Clause
));
3230 -- Start of processing for Has_With_Clause
3233 if Present
(Context_Items
(C_Unit
)) then
3234 Item
:= First
(Context_Items
(C_Unit
));
3235 while Present
(Item
) loop
3236 if Nkind
(Item
) = N_With_Clause
3237 and then Limited_Present
(Item
) = Is_Limited
3238 and then Named_Unit
(Item
) = Pack
3248 end Has_With_Clause
;
3250 -----------------------------
3251 -- Implicit_With_On_Parent --
3252 -----------------------------
3254 procedure Implicit_With_On_Parent
3255 (Child_Unit
: Node_Id
;
3258 Loc
: constant Source_Ptr
:= Sloc
(N
);
3259 P
: constant Node_Id
:= Parent_Spec
(Child_Unit
);
3260 P_Unit
: Node_Id
:= Unit
(P
);
3261 P_Name
: constant Entity_Id
:= Get_Parent_Entity
(P_Unit
);
3264 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
;
3265 -- Build prefix of child unit name. Recurse if needed
3267 function Build_Unit_Name
return Node_Id
;
3268 -- If the unit is a child unit, build qualified name with all ancestors
3270 -------------------------
3271 -- Build_Ancestor_Name --
3272 -------------------------
3274 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
is
3275 P_Ref
: constant Node_Id
:=
3276 New_Occurrence_Of
(Defining_Entity
(P
), Loc
);
3277 P_Spec
: Node_Id
:= P
;
3280 -- Ancestor may have been rewritten as a package body. Retrieve
3281 -- the original spec to trace earlier ancestors.
3283 if Nkind
(P
) = N_Package_Body
3284 and then Nkind
(Original_Node
(P
)) = N_Package_Instantiation
3286 P_Spec
:= Original_Node
(P
);
3289 if No
(Parent_Spec
(P_Spec
)) then
3293 Make_Selected_Component
(Loc
,
3294 Prefix
=> Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Spec
))),
3295 Selector_Name
=> P_Ref
);
3297 end Build_Ancestor_Name
;
3299 ---------------------
3300 -- Build_Unit_Name --
3301 ---------------------
3303 function Build_Unit_Name
return Node_Id
is
3307 if No
(Parent_Spec
(P_Unit
)) then
3308 return New_Occurrence_Of
(P_Name
, Loc
);
3312 Make_Expanded_Name
(Loc
,
3313 Chars
=> Chars
(P_Name
),
3314 Prefix
=> Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Unit
))),
3315 Selector_Name
=> New_Occurrence_Of
(P_Name
, Loc
));
3316 Set_Entity
(Result
, P_Name
);
3319 end Build_Unit_Name
;
3321 -- Start of processing for Implicit_With_On_Parent
3324 -- The unit of the current compilation may be a package body that
3325 -- replaces an instance node. In this case we need the original instance
3326 -- node to construct the proper parent name.
3328 if Nkind
(P_Unit
) = N_Package_Body
3329 and then Nkind
(Original_Node
(P_Unit
)) = N_Package_Instantiation
3331 P_Unit
:= Original_Node
(P_Unit
);
3334 -- We add the implicit with if the child unit is the current unit being
3335 -- compiled. If the current unit is a body, we do not want to add an
3336 -- implicit_with a second time to the corresponding spec.
3338 if Nkind
(Child_Unit
) = N_Package_Declaration
3339 and then Child_Unit
/= Unit
(Cunit
(Current_Sem_Unit
))
3344 Withn
:= Make_With_Clause
(Loc
, Name
=> Build_Unit_Name
);
3346 Set_Library_Unit
(Withn
, P
);
3347 Set_Corresponding_Spec
(Withn
, P_Name
);
3348 Set_First_Name
(Withn
, True);
3349 Set_Implicit_With
(Withn
, True);
3351 -- Node is placed at the beginning of the context items, so that
3352 -- subsequent use clauses on the parent can be validated.
3354 Prepend
(Withn
, Context_Items
(N
));
3355 Mark_Rewrite_Insertion
(Withn
);
3356 Install_Withed_Unit
(Withn
);
3358 if Is_Child_Spec
(P_Unit
) then
3359 Implicit_With_On_Parent
(P_Unit
, N
);
3361 end Implicit_With_On_Parent
;
3367 function In_Chain
(E
: Entity_Id
) return Boolean is
3371 H
:= Current_Entity
(E
);
3372 while Present
(H
) loop
3383 ---------------------
3384 -- Install_Context --
3385 ---------------------
3387 procedure Install_Context
(N
: Node_Id
; Chain
: Boolean := True) is
3388 Lib_Unit
: constant Node_Id
:= Unit
(N
);
3391 Install_Context_Clauses
(N
, Chain
);
3393 if Is_Child_Spec
(Lib_Unit
) then
3395 (Lib_Unit
=> Lib_Unit
,
3396 Is_Private
=> Private_Present
(Parent
(Lib_Unit
)),
3400 Install_Limited_Context_Clauses
(N
);
3401 end Install_Context
;
3403 -----------------------------
3404 -- Install_Context_Clauses --
3405 -----------------------------
3407 procedure Install_Context_Clauses
(N
: Node_Id
; Chain
: Boolean := True) is
3408 Lib_Unit
: constant Node_Id
:= Unit
(N
);
3410 Uname_Node
: Entity_Id
;
3411 Check_Private
: Boolean := False;
3412 Decl_Node
: Node_Id
;
3413 Lib_Parent
: Entity_Id
;
3416 -- First skip configuration pragmas at the start of the context. They
3417 -- are not technically part of the context clause, but that's where the
3418 -- parser puts them. Note they were analyzed in Analyze_Context.
3420 Item
:= First
(Context_Items
(N
));
3421 while Present
(Item
)
3422 and then Nkind
(Item
) = N_Pragma
3423 and then Pragma_Name
(Item
) in Configuration_Pragma_Names
3428 -- Loop through the actual context clause items. We process everything
3429 -- except Limited_With clauses in this routine. Limited_With clauses
3430 -- are separately installed (see Install_Limited_Context_Clauses).
3432 while Present
(Item
) loop
3434 -- Case of explicit WITH clause
3436 if Nkind
(Item
) = N_With_Clause
3437 and then not Implicit_With
(Item
)
3439 if Limited_Present
(Item
) then
3441 -- Limited withed units will be installed later
3445 -- If Name (Item) is not an entity name, something is wrong, and
3446 -- this will be detected in due course, for now ignore the item
3448 elsif not Is_Entity_Name
(Name
(Item
)) then
3451 elsif No
(Entity
(Name
(Item
))) then
3452 Set_Entity
(Name
(Item
), Any_Id
);
3456 Uname_Node
:= Entity
(Name
(Item
));
3458 if Is_Private_Descendant
(Uname_Node
) then
3459 Check_Private
:= True;
3462 Install_Withed_Unit
(Item
);
3464 Decl_Node
:= Unit_Declaration_Node
(Uname_Node
);
3466 -- If the unit is a subprogram instance, it appears nested within
3467 -- a package that carries the parent information.
3469 if Is_Generic_Instance
(Uname_Node
)
3470 and then Ekind
(Uname_Node
) /= E_Package
3472 Decl_Node
:= Parent
(Parent
(Decl_Node
));
3475 if Is_Child_Spec
(Decl_Node
) then
3476 if Nkind
(Name
(Item
)) = N_Expanded_Name
then
3477 Expand_With_Clause
(Item
, Prefix
(Name
(Item
)), N
);
3479 -- If not an expanded name, the child unit must be a
3480 -- renaming, nothing to do.
3485 elsif Nkind
(Decl_Node
) = N_Subprogram_Body
3486 and then not Acts_As_Spec
(Parent
(Decl_Node
))
3487 and then Is_Child_Spec
(Unit
(Library_Unit
(Parent
(Decl_Node
))))
3489 Implicit_With_On_Parent
3490 (Unit
(Library_Unit
(Parent
(Decl_Node
))), N
);
3493 -- Check license conditions unless this is a dummy unit
3495 if Sloc
(Library_Unit
(Item
)) /= No_Location
then
3496 License_Check
: declare
3497 Withu
: constant Unit_Number_Type
:=
3498 Get_Source_Unit
(Library_Unit
(Item
));
3499 Withl
: constant License_Type
:=
3500 License
(Source_Index
(Withu
));
3501 Unitl
: constant License_Type
:=
3502 License
(Source_Index
(Current_Sem_Unit
));
3504 procedure License_Error
;
3505 -- Signal error of bad license
3511 procedure License_Error
is
3514 ("license of withed unit & may be inconsistent??",
3518 -- Start of processing for License_Check
3521 -- Exclude license check if withed unit is an internal unit.
3522 -- This situation arises e.g. with the GPL version of GNAT.
3524 if Is_Internal_Unit
(Withu
) then
3527 -- Otherwise check various cases
3539 if Withl
= Restricted
then
3543 when Modified_GPL
=>
3544 if Withl
= Restricted
or else Withl
= GPL
then
3548 when Unrestricted
=>
3555 -- Case of USE PACKAGE clause
3557 elsif Nkind
(Item
) = N_Use_Package_Clause
then
3558 Analyze_Use_Package
(Item
, Chain
);
3560 -- Case of USE TYPE clause
3562 elsif Nkind
(Item
) = N_Use_Type_Clause
then
3563 Analyze_Use_Type
(Item
, Chain
);
3567 elsif Nkind
(Item
) = N_Pragma
then
3575 if Is_Child_Spec
(Lib_Unit
) then
3577 -- The unit also has implicit with_clauses on its own parents
3579 if No
(Context_Items
(N
)) then
3580 Set_Context_Items
(N
, New_List
);
3583 Implicit_With_On_Parent
(Lib_Unit
, N
);
3586 -- If the unit is a body, the context of the specification must also
3587 -- be installed. That includes private with_clauses in that context.
3589 if Nkind
(Lib_Unit
) = N_Package_Body
3590 or else (Nkind
(Lib_Unit
) = N_Subprogram_Body
3591 and then not Acts_As_Spec
(N
))
3593 Install_Context
(Library_Unit
(N
), Chain
);
3595 -- Only install private with-clauses of a spec that comes from
3596 -- source, excluding specs created for a subprogram body that is
3599 if Comes_From_Source
(Library_Unit
(N
)) then
3600 Install_Private_With_Clauses
3601 (Defining_Entity
(Unit
(Library_Unit
(N
))));
3604 if Is_Child_Spec
(Unit
(Library_Unit
(N
))) then
3606 -- If the unit is the body of a public child unit, the private
3607 -- declarations of the parent must be made visible. If the child
3608 -- unit is private, the private declarations have been installed
3609 -- already in the call to Install_Parents for the spec. Installing
3610 -- private declarations must be done for all ancestors of public
3611 -- child units. In addition, sibling units mentioned in the
3612 -- context clause of the body are directly visible.
3620 Lib_Spec
:= Unit
(Library_Unit
(N
));
3621 while Is_Child_Spec
(Lib_Spec
) loop
3622 P
:= Unit
(Parent_Spec
(Lib_Spec
));
3623 P_Name
:= Defining_Entity
(P
);
3625 if not (Private_Present
(Parent
(Lib_Spec
)))
3626 and then not In_Private_Part
(P_Name
)
3628 Install_Private_Declarations
(P_Name
);
3629 Install_Private_With_Clauses
(P_Name
);
3630 Set_Use
(Private_Declarations
(Specification
(P
)));
3638 -- For a package body, children in context are immediately visible
3640 Install_Siblings
(Defining_Entity
(Unit
(Library_Unit
(N
))), N
);
3643 if Nkind_In
(Lib_Unit
, N_Generic_Package_Declaration
,
3644 N_Generic_Subprogram_Declaration
,
3645 N_Package_Declaration
,
3646 N_Subprogram_Declaration
)
3648 if Is_Child_Spec
(Lib_Unit
) then
3649 Lib_Parent
:= Defining_Entity
(Unit
(Parent_Spec
(Lib_Unit
)));
3650 Set_Is_Private_Descendant
3651 (Defining_Entity
(Lib_Unit
),
3652 Is_Private_Descendant
(Lib_Parent
)
3653 or else Private_Present
(Parent
(Lib_Unit
)));
3656 Set_Is_Private_Descendant
3657 (Defining_Entity
(Lib_Unit
),
3658 Private_Present
(Parent
(Lib_Unit
)));
3662 if Check_Private
then
3663 Check_Private_Child_Unit
(N
);
3665 end Install_Context_Clauses
;
3667 -------------------------------------
3668 -- Install_Limited_Context_Clauses --
3669 -------------------------------------
3671 procedure Install_Limited_Context_Clauses
(N
: Node_Id
) is
3674 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
);
3675 -- Check that the unlimited view of a given compilation_unit is not
3676 -- already visible through "use + renamings".
3678 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
);
3679 -- Check that if a limited_with clause of a given compilation_unit
3680 -- mentions a descendant of a private child of some library unit, then
3681 -- the given compilation_unit must be the declaration of a private
3682 -- descendant of that library unit, or a public descendant of such. The
3683 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3684 -- use entities on the limited with_clauses because their units have not
3685 -- been analyzed, so we have to climb the tree of ancestors looking for
3686 -- private keywords.
3688 procedure Expand_Limited_With_Clause
3689 (Comp_Unit
: Node_Id
;
3692 -- If a child unit appears in a limited_with clause, there are implicit
3693 -- limited_with clauses on all parents that are not already visible
3694 -- through a regular with clause. This procedure creates the implicit
3695 -- limited with_clauses for the parents and loads the corresponding
3696 -- units. The shadow entities are created when the inserted clause is
3697 -- analyzed. Implements Ada 2005 (AI-50217).
3699 ---------------------
3700 -- Check_Renamings --
3701 ---------------------
3703 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
) is
3711 pragma Assert
(Nkind
(W
) = N_With_Clause
);
3713 -- Protect the frontend against previous critical errors
3715 case Nkind
(Unit
(Library_Unit
(W
))) is
3716 when N_Generic_Package_Declaration
3717 | N_Generic_Subprogram_Declaration
3718 | N_Package_Declaration
3719 | N_Subprogram_Declaration
3727 -- Check "use + renamings"
3729 WEnt
:= Defining_Unit_Name
(Specification
(Unit
(Library_Unit
(W
))));
3730 Spec
:= Specification
(Unit
(P
));
3732 Item
:= First
(Visible_Declarations
(Spec
));
3733 while Present
(Item
) loop
3735 -- Look only at use package clauses
3737 if Nkind
(Item
) = N_Use_Package_Clause
then
3739 E
:= Entity
(Name
(Item
));
3741 pragma Assert
(Present
(Parent
(E
)));
3743 if Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
3744 and then Renamed_Entity
(E
) = WEnt
3746 -- The unlimited view is visible through use clause and
3747 -- renamings. There is no need to generate the error
3748 -- message here because Is_Visible_Through_Renamings
3749 -- takes care of generating the precise error message.
3753 elsif Nkind
(Parent
(E
)) = N_Package_Specification
then
3755 -- The use clause may refer to a local package.
3756 -- Check all the enclosing scopes.
3759 while E2
/= Standard_Standard
and then E2
/= WEnt
loop
3765 ("unlimited view visible through use clause ", W
);
3774 -- Recursive call to check all the ancestors
3776 if Is_Child_Spec
(Unit
(P
)) then
3777 Check_Renamings
(P
=> Parent_Spec
(Unit
(P
)), W
=> W
);
3779 end Check_Renamings
;
3781 ---------------------------------------
3782 -- Check_Private_Limited_Withed_Unit --
3783 ---------------------------------------
3785 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
) is
3786 Curr_Parent
: Node_Id
;
3787 Child_Parent
: Node_Id
;
3788 Curr_Private
: Boolean;
3791 -- Compilation unit of the parent of the withed library unit
3793 Child_Parent
:= Library_Unit
(Item
);
3795 -- If the child unit is a public child, then locate its nearest
3796 -- private ancestor, if any, then Child_Parent will then be set to
3797 -- the parent of that ancestor.
3799 if not Private_Present
(Library_Unit
(Item
)) then
3800 while Present
(Child_Parent
)
3801 and then not Private_Present
(Child_Parent
)
3803 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
3806 if No
(Child_Parent
) then
3811 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
3813 -- Traverse all the ancestors of the current compilation unit to
3814 -- check if it is a descendant of named library unit.
3816 Curr_Parent
:= Parent
(Item
);
3817 Curr_Private
:= Private_Present
(Curr_Parent
);
3819 while Present
(Parent_Spec
(Unit
(Curr_Parent
)))
3820 and then Curr_Parent
/= Child_Parent
3822 Curr_Parent
:= Parent_Spec
(Unit
(Curr_Parent
));
3823 Curr_Private
:= Curr_Private
or else Private_Present
(Curr_Parent
);
3826 if Curr_Parent
/= Child_Parent
then
3828 ("unit in with clause is private child unit!", Item
);
3830 ("\current unit must also have parent&!",
3831 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
3833 elsif Private_Present
(Parent
(Item
))
3834 or else Curr_Private
3835 or else Private_Present
(Item
)
3836 or else Nkind_In
(Unit
(Parent
(Item
)), N_Package_Body
,
3840 -- Current unit is private, of descendant of a private unit
3846 ("current unit must also be private descendant of&",
3847 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
3849 end Check_Private_Limited_Withed_Unit
;
3851 --------------------------------
3852 -- Expand_Limited_With_Clause --
3853 --------------------------------
3855 procedure Expand_Limited_With_Clause
3856 (Comp_Unit
: Node_Id
;
3860 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
3861 Unum
: Unit_Number_Type
;
3864 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean;
3865 -- Returns true if the context already includes a with_clause for
3866 -- this unit. If the with_clause is non-limited, the unit is fully
3867 -- visible and an implicit limited_with should not be created. If
3868 -- there is already a limited_with clause for W, a second one is
3869 -- simply redundant.
3871 --------------------------
3872 -- Previous_Withed_Unit --
3873 --------------------------
3875 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean is
3879 -- A limited with_clause cannot appear in the same context_clause
3880 -- as a nonlimited with_clause which mentions the same library.
3882 Item
:= First
(Context_Items
(Comp_Unit
));
3883 while Present
(Item
) loop
3884 if Nkind
(Item
) = N_With_Clause
3885 and then Library_Unit
(Item
) = Library_Unit
(W
)
3894 end Previous_Withed_Unit
;
3896 -- Start of processing for Expand_Limited_With_Clause
3899 if Nkind
(Nam
) = N_Identifier
then
3901 -- Create node for name of withed unit
3904 Make_With_Clause
(Loc
,
3905 Name
=> New_Copy
(Nam
));
3907 else pragma Assert
(Nkind
(Nam
) = N_Selected_Component
);
3909 Make_With_Clause
(Loc
,
3910 Name
=> Make_Selected_Component
(Loc
,
3911 Prefix
=> New_Copy_Tree
(Prefix
(Nam
)),
3912 Selector_Name
=> New_Copy
(Selector_Name
(Nam
))));
3913 Set_Parent
(Withn
, Parent
(N
));
3916 Set_Limited_Present
(Withn
);
3917 Set_First_Name
(Withn
);
3918 Set_Implicit_With
(Withn
);
3922 (Load_Name
=> Get_Spec_Name
(Get_Unit_Name
(Nam
)),
3927 -- Do not generate a limited_with_clause on the current unit. This
3928 -- path is taken when a unit has a limited_with clause on one of its
3931 if Unum
= Current_Sem_Unit
then
3935 Set_Library_Unit
(Withn
, Cunit
(Unum
));
3936 Set_Corresponding_Spec
3937 (Withn
, Specification
(Unit
(Cunit
(Unum
))));
3939 if not Previous_Withed_Unit
(Withn
) then
3940 Prepend
(Withn
, Context_Items
(Parent
(N
)));
3941 Mark_Rewrite_Insertion
(Withn
);
3943 -- Add implicit limited_with_clauses for parents of child units
3944 -- mentioned in limited_with clauses.
3946 if Nkind
(Nam
) = N_Selected_Component
then
3947 Expand_Limited_With_Clause
(Comp_Unit
, Prefix
(Nam
), N
);
3952 if not Limited_View_Installed
(Withn
) then
3953 Install_Limited_Withed_Unit
(Withn
);
3956 end Expand_Limited_With_Clause
;
3958 -- Start of processing for Install_Limited_Context_Clauses
3961 Item
:= First
(Context_Items
(N
));
3962 while Present
(Item
) loop
3963 if Nkind
(Item
) = N_With_Clause
3964 and then Limited_Present
(Item
)
3965 and then not Error_Posted
(Item
)
3967 if Nkind
(Name
(Item
)) = N_Selected_Component
then
3968 Expand_Limited_With_Clause
3969 (Comp_Unit
=> N
, Nam
=> Prefix
(Name
(Item
)), N
=> Item
);
3972 Check_Private_Limited_Withed_Unit
(Item
);
3974 if not Implicit_With
(Item
) and then Is_Child_Spec
(Unit
(N
)) then
3975 Check_Renamings
(Parent_Spec
(Unit
(N
)), Item
);
3978 -- A unit may have a limited with on itself if it has a limited
3979 -- with_clause on one of its child units. In that case it is
3980 -- already being compiled and it makes no sense to install its
3983 -- If the item is a limited_private_with_clause, install it if the
3984 -- current unit is a body or if it is a private child. Otherwise
3985 -- the private clause is installed before analyzing the private
3986 -- part of the current unit.
3988 if Library_Unit
(Item
) /= Cunit
(Current_Sem_Unit
)
3989 and then not Limited_View_Installed
(Item
)
3991 not Is_Ancestor_Unit
3992 (Library_Unit
(Item
), Cunit
(Current_Sem_Unit
))
3994 if not Private_Present
(Item
)
3995 or else Private_Present
(N
)
3996 or else Nkind_In
(Unit
(N
), N_Package_Body
,
4000 Install_Limited_Withed_Unit
(Item
);
4008 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4009 -- looking for incomplete subtype declarations of incomplete types
4010 -- visible through a limited with clause.
4012 if Ada_Version
>= Ada_2005
4013 and then Analyzed
(N
)
4014 and then Nkind
(Unit
(N
)) = N_Package_Declaration
4019 Non_Lim_View
: Entity_Id
;
4022 Decl
:= First
(Visible_Declarations
(Specification
(Unit
(N
))));
4023 while Present
(Decl
) loop
4024 if Nkind
(Decl
) = N_Subtype_Declaration
4026 Ekind
(Defining_Identifier
(Decl
)) = E_Incomplete_Subtype
4028 From_Limited_With
(Defining_Identifier
(Decl
))
4030 Def_Id
:= Defining_Identifier
(Decl
);
4031 Non_Lim_View
:= Non_Limited_View
(Def_Id
);
4033 if not Is_Incomplete_Type
(Non_Lim_View
) then
4035 -- Convert an incomplete subtype declaration into a
4036 -- corresponding non-limited view subtype declaration.
4037 -- This is usually the case when analyzing a body that
4038 -- has regular with clauses, when the spec has limited
4041 -- If the non-limited view is still incomplete, it is
4042 -- the dummy entry already created, and the declaration
4043 -- cannot be reanalyzed. This is the case when installing
4044 -- a parent unit that has limited with-clauses.
4046 Set_Subtype_Indication
(Decl
,
4047 New_Occurrence_Of
(Non_Lim_View
, Sloc
(Def_Id
)));
4048 Set_Etype
(Def_Id
, Non_Lim_View
);
4049 Set_Ekind
(Def_Id
, Subtype_Kind
(Ekind
(Non_Lim_View
)));
4050 Set_Analyzed
(Decl
, False);
4052 -- Reanalyze the declaration, suppressing the call to
4053 -- Enter_Name to avoid duplicate names.
4055 Analyze_Subtype_Declaration
4065 end Install_Limited_Context_Clauses
;
4067 ---------------------
4068 -- Install_Parents --
4069 ---------------------
4071 procedure Install_Parents
4072 (Lib_Unit
: Node_Id
;
4073 Is_Private
: Boolean;
4074 Chain
: Boolean := True)
4082 P
:= Unit
(Parent_Spec
(Lib_Unit
));
4083 P_Name
:= Get_Parent_Entity
(P
);
4085 if Etype
(P_Name
) = Any_Type
then
4089 if Ekind
(P_Name
) = E_Generic_Package
4090 and then not Nkind_In
(Lib_Unit
, N_Generic_Subprogram_Declaration
,
4091 N_Generic_Package_Declaration
)
4092 and then Nkind
(Lib_Unit
) not in N_Generic_Renaming_Declaration
4095 ("child of a generic package must be a generic unit", Lib_Unit
);
4097 elsif not Is_Package_Or_Generic_Package
(P_Name
) then
4099 ("parent unit must be package or generic package", Lib_Unit
);
4100 raise Unrecoverable_Error
;
4102 elsif Present
(Renamed_Object
(P_Name
)) then
4103 Error_Msg_N
("parent unit cannot be a renaming", Lib_Unit
);
4104 raise Unrecoverable_Error
;
4106 -- Verify that a child of an instance is itself an instance, or the
4107 -- renaming of one. Given that an instance that is a unit is replaced
4108 -- with a package declaration, check against the original node. The
4109 -- parent may be currently being instantiated, in which case it appears
4110 -- as a declaration, but the generic_parent is already established
4111 -- indicating that we deal with an instance.
4113 elsif Nkind
(Original_Node
(P
)) = N_Package_Instantiation
then
4114 if Nkind
(Lib_Unit
) in N_Renaming_Declaration
4115 or else Nkind
(Original_Node
(Lib_Unit
)) in N_Generic_Instantiation
4117 (Nkind
(Lib_Unit
) = N_Package_Declaration
4118 and then Present
(Generic_Parent
(Specification
(Lib_Unit
))))
4123 ("child of an instance must be an instance or renaming",
4128 -- This is the recursive call that ensures all parents are loaded
4130 if Is_Child_Spec
(P
) then
4134 Is_Private
or else Private_Present
(Parent
(Lib_Unit
)),
4138 -- Now we can install the context for this parent
4140 Install_Context_Clauses
(Parent_Spec
(Lib_Unit
), Chain
);
4141 Install_Limited_Context_Clauses
(Parent_Spec
(Lib_Unit
));
4142 Install_Siblings
(P_Name
, Parent
(Lib_Unit
));
4144 -- The child unit is in the declarative region of the parent. The parent
4145 -- must therefore appear in the scope stack and be visible, as when
4146 -- compiling the corresponding body. If the child unit is private or it
4147 -- is a package body, private declarations must be accessible as well.
4148 -- Use declarations in the parent must also be installed. Finally, other
4149 -- child units of the same parent that are in the context are
4150 -- immediately visible.
4152 -- Find entity for compilation unit, and set its private descendant
4153 -- status as needed. Indicate that it is a compilation unit, which is
4154 -- redundant in general, but needed if this is a generated child spec
4155 -- for a child body without previous spec.
4157 E_Name
:= Defining_Entity
(Lib_Unit
);
4159 Set_Is_Child_Unit
(E_Name
);
4160 Set_Is_Compilation_Unit
(E_Name
);
4162 Set_Is_Private_Descendant
(E_Name
,
4163 Is_Private_Descendant
(P_Name
)
4164 or else Private_Present
(Parent
(Lib_Unit
)));
4166 P_Spec
:= Package_Specification
(P_Name
);
4167 Push_Scope
(P_Name
);
4169 -- Save current visibility of unit
4171 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
:=
4172 Is_Immediately_Visible
(P_Name
);
4173 Set_Is_Immediately_Visible
(P_Name
);
4174 Install_Visible_Declarations
(P_Name
);
4175 Set_Use
(Visible_Declarations
(P_Spec
));
4177 -- If the parent is a generic unit, its formal part may contain formal
4178 -- packages and use clauses for them.
4180 if Ekind
(P_Name
) = E_Generic_Package
then
4181 Set_Use
(Generic_Formal_Declarations
(Parent
(P_Spec
)));
4184 if Is_Private
or else Private_Present
(Parent
(Lib_Unit
)) then
4185 Install_Private_Declarations
(P_Name
);
4186 Install_Private_With_Clauses
(P_Name
);
4187 Set_Use
(Private_Declarations
(P_Spec
));
4189 end Install_Parents
;
4191 ----------------------------------
4192 -- Install_Private_With_Clauses --
4193 ----------------------------------
4195 procedure Install_Private_With_Clauses
(P
: Entity_Id
) is
4196 Decl
: constant Node_Id
:= Unit_Declaration_Node
(P
);
4200 if Debug_Flag_I
then
4201 Write_Str
("install private with clauses of ");
4202 Write_Name
(Chars
(P
));
4206 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
4207 Item
:= First
(Context_Items
(Parent
(Decl
)));
4208 while Present
(Item
) loop
4209 if Nkind
(Item
) = N_With_Clause
4210 and then Private_Present
(Item
)
4212 -- If the unit is an ancestor of the current one, it is the
4213 -- case of a private limited with clause on a child unit, and
4214 -- the compilation of one of its descendants, In that case the
4215 -- limited view is errelevant.
4217 if Limited_Present
(Item
) then
4218 if not Limited_View_Installed
(Item
)
4220 not Is_Ancestor_Unit
(Library_Unit
(Item
),
4221 Cunit
(Current_Sem_Unit
))
4223 Install_Limited_Withed_Unit
(Item
);
4226 Install_Withed_Unit
(Item
, Private_With_OK
=> True);
4233 end Install_Private_With_Clauses
;
4235 ----------------------
4236 -- Install_Siblings --
4237 ----------------------
4239 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
) is
4245 -- Iterate over explicit with clauses, and check whether the scope of
4246 -- each entity is an ancestor of the current unit, in which case it is
4247 -- immediately visible.
4249 Item
:= First
(Context_Items
(N
));
4250 while Present
(Item
) loop
4252 -- Do not install private_with_clauses declaration, unless unit
4253 -- is itself a private child unit, or is a body. Note that for a
4254 -- subprogram body the private_with_clause does not take effect
4255 -- until after the specification.
4257 if Nkind
(Item
) /= N_With_Clause
4258 or else Implicit_With
(Item
)
4259 or else Limited_Present
(Item
)
4260 or else Error_Posted
(Item
)
4262 -- Skip processing malformed trees
4264 or else (Try_Semantics
4265 and then Nkind
(Name
(Item
)) not in N_Has_Entity
)
4269 elsif not Private_Present
(Item
)
4270 or else Private_Present
(N
)
4271 or else Nkind
(Unit
(N
)) = N_Package_Body
4273 Id
:= Entity
(Name
(Item
));
4275 if Is_Child_Unit
(Id
)
4276 and then Is_Ancestor_Package
(Scope
(Id
), U_Name
)
4278 Set_Is_Immediately_Visible
(Id
);
4280 -- Check for the presence of another unit in the context that
4281 -- may be inadvertently hidden by the child.
4283 Prev
:= Current_Entity
(Id
);
4286 and then Is_Immediately_Visible
(Prev
)
4287 and then not Is_Child_Unit
(Prev
)
4293 Clause
:= First
(Context_Items
(N
));
4294 while Present
(Clause
) loop
4295 if Nkind
(Clause
) = N_With_Clause
4296 and then Entity
(Name
(Clause
)) = Prev
4299 ("child unit& hides compilation unit " &
4300 "with the same name??",
4310 -- The With_Clause may be on a grand-child or one of its further
4311 -- descendants, which makes a child immediately visible. Examine
4312 -- ancestry to determine whether such a child exists. For example,
4313 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4314 -- is immediately visible.
4316 elsif Is_Child_Unit
(Id
) then
4322 while Is_Child_Unit
(Par
) loop
4323 if Is_Ancestor_Package
(Scope
(Par
), U_Name
) then
4324 Set_Is_Immediately_Visible
(Par
);
4333 -- If the item is a private with-clause on a child unit, the parent
4334 -- may have been installed already, but the child unit must remain
4335 -- invisible until installed in a private part or body, unless there
4336 -- is already a regular with_clause for it in the current unit.
4338 elsif Private_Present
(Item
) then
4339 Id
:= Entity
(Name
(Item
));
4341 if Is_Child_Unit
(Id
) then
4345 function In_Context
return Boolean;
4346 -- Scan context of current unit, to check whether there is
4347 -- a with_clause on the same unit as a private with-clause
4348 -- on a parent, in which case child unit is visible. If the
4349 -- unit is a grand-child, the same applies to its parent.
4355 function In_Context
return Boolean is
4358 First
(Context_Items
(Cunit
(Current_Sem_Unit
)));
4359 while Present
(Clause
) loop
4360 if Nkind
(Clause
) = N_With_Clause
4361 and then Comes_From_Source
(Clause
)
4362 and then Is_Entity_Name
(Name
(Clause
))
4363 and then not Private_Present
(Clause
)
4365 if Entity
(Name
(Clause
)) = Id
4367 (Nkind
(Name
(Clause
)) = N_Expanded_Name
4368 and then Entity
(Prefix
(Name
(Clause
))) = Id
)
4381 Set_Is_Visible_Lib_Unit
(Id
, In_Context
);
4388 end Install_Siblings
;
4390 ---------------------------------
4391 -- Install_Limited_Withed_Unit --
4392 ---------------------------------
4394 procedure Install_Limited_Withed_Unit
(N
: Node_Id
) is
4395 P_Unit
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
4398 Is_Child_Package
: Boolean := False;
4399 Lim_Header
: Entity_Id
;
4400 Lim_Typ
: Entity_Id
;
4402 procedure Check_Body_Required
;
4403 -- A unit mentioned in a limited with_clause may not be mentioned in
4404 -- a regular with_clause, but must still be included in the current
4405 -- partition. We need to determine whether the unit needs a body, so
4406 -- that the binder can determine the name of the file to be compiled.
4407 -- Checking whether a unit needs a body can be done without semantic
4408 -- analysis, by examining the nature of the declarations in the package.
4410 function Has_Limited_With_Clause
4411 (C_Unit
: Entity_Id
;
4412 Pack
: Entity_Id
) return Boolean;
4413 -- Determine whether any package in the ancestor chain starting with
4414 -- C_Unit has a limited with clause for package Pack.
4416 function Is_Visible_Through_Renamings
(P
: Entity_Id
) return Boolean;
4417 -- Check if some package installed though normal with-clauses has a
4418 -- renaming declaration of package P. AARM 10.1.2(21/2).
4420 -------------------------
4421 -- Check_Body_Required --
4422 -------------------------
4424 procedure Check_Body_Required
is
4425 PA
: constant List_Id
:=
4426 Pragmas_After
(Aux_Decls_Node
(Parent
(P_Unit
)));
4428 procedure Check_Declarations
(Spec
: Node_Id
);
4429 -- Recursive procedure that does the work and checks nested packages
4431 ------------------------
4432 -- Check_Declarations --
4433 ------------------------
4435 procedure Check_Declarations
(Spec
: Node_Id
) is
4437 Incomplete_Decls
: constant Elist_Id
:= New_Elmt_List
;
4439 Subp_List
: constant Elist_Id
:= New_Elmt_List
;
4441 procedure Check_Pragma_Import
(P
: Node_Id
);
4442 -- If a pragma import applies to a previous subprogram, the
4443 -- enclosing unit may not need a body. The processing is syntactic
4444 -- and does not require a declaration to be analyzed. The code
4445 -- below also handles pragma Import when applied to a subprogram
4446 -- that renames another. In this case the pragma applies to the
4449 -- Chains of multiple renames are not handled by the code below.
4450 -- It is probably impossible to handle all cases without proper
4451 -- name resolution. In such cases the algorithm is conservative
4452 -- and will indicate that a body is needed???
4454 -------------------------
4455 -- Check_Pragma_Import --
4456 -------------------------
4458 procedure Check_Pragma_Import
(P
: Node_Id
) is
4464 procedure Remove_Homonyms
(E
: Node_Id
);
4465 -- Make one pass over list of subprograms. Called again if
4466 -- subprogram is a renaming. E is known to be an identifier.
4468 ---------------------
4469 -- Remove_Homonyms --
4470 ---------------------
4472 procedure Remove_Homonyms
(E
: Node_Id
) is
4473 R
: Entity_Id
:= Empty
;
4474 -- Name of renamed entity, if any
4477 Subp_Id
:= First_Elmt
(Subp_List
);
4478 while Present
(Subp_Id
) loop
4479 if Chars
(Node
(Subp_Id
)) = Chars
(E
) then
4480 if Nkind
(Parent
(Parent
(Node
(Subp_Id
))))
4481 /= N_Subprogram_Renaming_Declaration
4484 Next_Elmt
(Subp_Id
);
4485 Remove_Elmt
(Subp_List
, Prev_Id
);
4487 R
:= Name
(Parent
(Parent
(Node
(Subp_Id
))));
4491 Next_Elmt
(Subp_Id
);
4496 if Nkind
(R
) = N_Identifier
then
4497 Remove_Homonyms
(R
);
4499 elsif Nkind
(R
) = N_Selected_Component
then
4500 Remove_Homonyms
(Selector_Name
(R
));
4502 -- Renaming of attribute
4508 end Remove_Homonyms
;
4510 -- Start of processing for Check_Pragma_Import
4513 -- Find name of entity in Import pragma. We have not analyzed
4514 -- the construct, so we must guard against syntax errors.
4516 Arg
:= Next
(First
(Pragma_Argument_Associations
(P
)));
4519 or else Nkind
(Expression
(Arg
)) /= N_Identifier
4523 Imported
:= Expression
(Arg
);
4526 Remove_Homonyms
(Imported
);
4527 end Check_Pragma_Import
;
4529 -- Start of processing for Check_Declarations
4532 -- Search for Elaborate Body pragma
4534 Decl
:= First
(Visible_Declarations
(Spec
));
4535 while Present
(Decl
)
4536 and then Nkind
(Decl
) = N_Pragma
4538 if Get_Pragma_Id
(Decl
) = Pragma_Elaborate_Body
then
4539 Set_Body_Required
(Library_Unit
(N
));
4546 -- Look for declarations that require the presence of a body. We
4547 -- have already skipped pragmas at the start of the list.
4549 while Present
(Decl
) loop
4551 -- Subprogram that comes from source means body may be needed.
4552 -- Save for subsequent examination of import pragmas.
4554 if Comes_From_Source
(Decl
)
4555 and then (Nkind_In
(Decl
, N_Subprogram_Declaration
,
4556 N_Subprogram_Renaming_Declaration
,
4557 N_Generic_Subprogram_Declaration
))
4559 Append_Elmt
(Defining_Entity
(Decl
), Subp_List
);
4561 -- Package declaration of generic package declaration. We need
4562 -- to recursively examine nested declarations.
4564 elsif Nkind_In
(Decl
, N_Package_Declaration
,
4565 N_Generic_Package_Declaration
)
4567 Check_Declarations
(Specification
(Decl
));
4569 elsif Nkind
(Decl
) = N_Pragma
4570 and then Pragma_Name
(Decl
) = Name_Import
4572 Check_Pragma_Import
(Decl
);
4578 -- Same set of tests for private part. In addition to subprograms
4579 -- detect the presence of Taft Amendment types (incomplete types
4580 -- completed in the body).
4582 Decl
:= First
(Private_Declarations
(Spec
));
4583 while Present
(Decl
) loop
4584 if Comes_From_Source
(Decl
)
4585 and then (Nkind_In
(Decl
, N_Subprogram_Declaration
,
4586 N_Subprogram_Renaming_Declaration
,
4587 N_Generic_Subprogram_Declaration
))
4589 Append_Elmt
(Defining_Entity
(Decl
), Subp_List
);
4591 elsif Nkind_In
(Decl
, N_Package_Declaration
,
4592 N_Generic_Package_Declaration
)
4594 Check_Declarations
(Specification
(Decl
));
4596 -- Collect incomplete type declarations for separate pass
4598 elsif Nkind
(Decl
) = N_Incomplete_Type_Declaration
then
4599 Append_Elmt
(Decl
, Incomplete_Decls
);
4601 elsif Nkind
(Decl
) = N_Pragma
4602 and then Pragma_Name
(Decl
) = Name_Import
4604 Check_Pragma_Import
(Decl
);
4610 -- Now check incomplete declarations to locate Taft amendment
4611 -- types. This can be done by examining the defining identifiers
4612 -- of type declarations without real semantic analysis.
4618 Inc
:= First_Elmt
(Incomplete_Decls
);
4619 while Present
(Inc
) loop
4620 Decl
:= Next
(Node
(Inc
));
4621 while Present
(Decl
) loop
4622 if Nkind
(Decl
) = N_Full_Type_Declaration
4623 and then Chars
(Defining_Identifier
(Decl
)) =
4624 Chars
(Defining_Identifier
(Node
(Inc
)))
4632 -- If no completion, this is a TAT, and a body is needed
4635 Set_Body_Required
(Library_Unit
(N
));
4643 -- Finally, check whether there are subprograms that still require
4644 -- a body, i.e. are not renamings or null.
4646 if not Is_Empty_Elmt_List
(Subp_List
) then
4652 Subp_Id
:= First_Elmt
(Subp_List
);
4653 Spec
:= Parent
(Node
(Subp_Id
));
4655 while Present
(Subp_Id
) loop
4656 if Nkind
(Parent
(Spec
))
4657 = N_Subprogram_Renaming_Declaration
4661 elsif Nkind
(Spec
) = N_Procedure_Specification
4662 and then Null_Present
(Spec
)
4667 Set_Body_Required
(Library_Unit
(N
));
4671 Next_Elmt
(Subp_Id
);
4675 end Check_Declarations
;
4677 -- Start of processing for Check_Body_Required
4680 -- If this is an imported package (Java and CIL usage) no body is
4681 -- needed. Scan list of pragmas that may follow a compilation unit
4682 -- to look for a relevant pragma Import.
4684 if Present
(PA
) then
4690 while Present
(Prag
) loop
4691 if Nkind
(Prag
) = N_Pragma
4692 and then Get_Pragma_Id
(Prag
) = Pragma_Import
4702 Check_Declarations
(Specification
(P_Unit
));
4703 end Check_Body_Required
;
4705 -----------------------------
4706 -- Has_Limited_With_Clause --
4707 -----------------------------
4709 function Has_Limited_With_Clause
4710 (C_Unit
: Entity_Id
;
4711 Pack
: Entity_Id
) return Boolean
4718 while Present
(Par
) loop
4719 if Ekind
(Par
) /= E_Package
then
4723 -- Retrieve the Compilation_Unit node for Par and determine if
4724 -- its context clauses contain a limited with for Pack.
4726 Par_Unit
:= Parent
(Parent
(Parent
(Par
)));
4728 if Nkind
(Par_Unit
) = N_Package_Declaration
then
4729 Par_Unit
:= Parent
(Par_Unit
);
4732 if Has_With_Clause
(Par_Unit
, Pack
, True) then
4736 -- If there are more ancestors, climb up the tree, otherwise we
4739 if Is_Child_Unit
(Par
) then
4747 end Has_Limited_With_Clause
;
4749 ----------------------------------
4750 -- Is_Visible_Through_Renamings --
4751 ----------------------------------
4753 function Is_Visible_Through_Renamings
(P
: Entity_Id
) return Boolean is
4754 Kind
: constant Node_Kind
:=
4755 Nkind
(Unit
(Cunit
(Current_Sem_Unit
)));
4761 -- Example of the error detected by this subprogram:
4769 -- package Ren_P renames P;
4775 -- limited with P; -- ERROR
4776 -- package R.C is ...
4778 Aux_Unit
:= Cunit
(Current_Sem_Unit
);
4781 Item
:= First
(Context_Items
(Aux_Unit
));
4782 while Present
(Item
) loop
4783 if Nkind
(Item
) = N_With_Clause
4784 and then not Limited_Present
(Item
)
4785 and then Nkind
(Unit
(Library_Unit
(Item
))) =
4786 N_Package_Declaration
4789 First
(Visible_Declarations
4790 (Specification
(Unit
(Library_Unit
(Item
)))));
4791 while Present
(Decl
) loop
4792 if Nkind
(Decl
) = N_Package_Renaming_Declaration
4793 and then Entity
(Name
(Decl
)) = P
4795 -- Generate the error message only if the current unit
4796 -- is a package declaration; in case of subprogram
4797 -- bodies and package bodies we just return True to
4798 -- indicate that the limited view must not be
4801 if Kind
= N_Package_Declaration
then
4803 ("simultaneous visibility of the limited and " &
4804 "unlimited views not allowed", N
);
4805 Error_Msg_Sloc
:= Sloc
(Item
);
4807 ("\\ unlimited view of & visible through the " &
4808 "context clause #", N
, P
);
4809 Error_Msg_Sloc
:= Sloc
(Decl
);
4810 Error_Msg_NE
("\\ and the renaming #", N
, P
);
4823 -- If it is a body not acting as spec, follow pointer to the
4824 -- corresponding spec, otherwise follow pointer to parent spec.
4826 if Present
(Library_Unit
(Aux_Unit
))
4827 and then Nkind_In
(Unit
(Aux_Unit
),
4828 N_Package_Body
, N_Subprogram_Body
)
4830 if Aux_Unit
= Library_Unit
(Aux_Unit
) then
4832 -- Aux_Unit is a body that acts as a spec. Clause has
4833 -- already been flagged as illegal.
4838 Aux_Unit
:= Library_Unit
(Aux_Unit
);
4842 Aux_Unit
:= Parent_Spec
(Unit
(Aux_Unit
));
4845 exit when No
(Aux_Unit
);
4849 end Is_Visible_Through_Renamings
;
4851 -- Start of processing for Install_Limited_Withed_Unit
4854 pragma Assert
(not Limited_View_Installed
(N
));
4856 -- In case of limited with_clause on subprograms, generics, instances,
4857 -- or renamings, the corresponding error was previously posted and we
4858 -- have nothing to do here. If the file is missing altogether, it has
4859 -- no source location.
4861 if Nkind
(P_Unit
) /= N_Package_Declaration
4862 or else Sloc
(P_Unit
) = No_Location
4867 P
:= Defining_Unit_Name
(Specification
(P_Unit
));
4869 -- Handle child packages
4871 if Nkind
(P
) = N_Defining_Program_Unit_Name
then
4872 Is_Child_Package
:= True;
4873 P
:= Defining_Identifier
(P
);
4876 -- Do not install the limited-view if the context of the unit is already
4877 -- available through a regular with clause.
4879 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4880 and then Has_With_Clause
(Cunit
(Current_Sem_Unit
), P
)
4885 -- Do not install the limited-view if the full-view is already visible
4886 -- through renaming declarations.
4888 if Is_Visible_Through_Renamings
(P
) then
4892 -- Do not install the limited view if this is the unit being analyzed.
4893 -- This unusual case will happen when a unit has a limited_with clause
4894 -- on one of its children. The compilation of the child forces the load
4895 -- of the parent which tries to install the limited view of the child
4896 -- again. Installing the limited view must also be disabled when
4897 -- compiling the body of the child unit.
4899 if P
= Cunit_Entity
(Current_Sem_Unit
)
4900 or else (Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4901 and then P
= Main_Unit_Entity
4902 and then Is_Ancestor_Unit
4903 (Cunit
(Main_Unit
), Cunit
(Current_Sem_Unit
)))
4908 -- This scenario is similar to the one above, the difference is that the
4909 -- compilation of sibling Par.Sib forces the load of parent Par which
4910 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4911 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4912 -- non-limited views of all entities from Lim_Pack.
4914 -- limited with Lim_Pack; -- [1]
4915 -- package Par is ... package Lim_Pack is ...
4917 -- with Lim_Pack; -- [2]
4918 -- package Par.Sib is ... package body Par.Sib is ...
4920 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4921 -- Sem_Unit is the body of Par.Sib.
4923 if Ekind
(P
) = E_Package
4924 and then Ekind
(Main_Unit_Entity
) = E_Package
4925 and then Is_Child_Unit
(Main_Unit_Entity
)
4927 -- The body has a regular with clause
4929 and then Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4930 and then Has_With_Clause
(Cunit
(Current_Sem_Unit
), P
)
4932 -- One of the ancestors has a limited with clause
4934 and then Nkind
(Parent
(Parent
(Main_Unit_Entity
))) =
4935 N_Package_Specification
4936 and then Has_Limited_With_Clause
(Scope
(Main_Unit_Entity
), P
)
4941 -- A common use of the limited-with is to have a limited-with in the
4942 -- package spec, and a normal with in its package body. For example:
4944 -- limited with X; -- [1]
4948 -- package body A is ...
4950 -- The compilation of A's body installs the context clauses found at [2]
4951 -- and then the context clauses of its specification (found at [1]). As
4952 -- a consequence, at [1] the specification of X has been analyzed and it
4953 -- is immediately visible. According to the semantics of limited-with
4954 -- context clauses we don't install the limited view because the full
4955 -- view of X supersedes its limited view.
4957 if Analyzed
(P_Unit
)
4959 (Is_Immediately_Visible
(P
)
4960 or else (Is_Child_Package
and then Is_Visible_Lib_Unit
(P
)))
4963 -- The presence of both the limited and the analyzed nonlimited view
4964 -- may also be an error, such as an illegal context for a limited
4965 -- with_clause. In that case, do not process the context item at all.
4967 if Error_Posted
(N
) then
4971 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
then
4975 Item
:= First
(Context_Items
(Cunit
(Current_Sem_Unit
)));
4976 while Present
(Item
) loop
4977 if Nkind
(Item
) = N_With_Clause
4978 and then Comes_From_Source
(Item
)
4979 and then Entity
(Name
(Item
)) = P
4988 -- If this is a child body, assume that the nonlimited with_clause
4989 -- appears in an ancestor. Could be refined ???
4993 (Unit
(Library_Unit
(Cunit
(Current_Sem_Unit
)))))
5000 -- If in package declaration, nonlimited view brought in from
5001 -- parent unit or some error condition.
5007 if Debug_Flag_I
then
5008 Write_Str
("install limited view of ");
5009 Write_Name
(Chars
(P
));
5013 -- If the unit has not been analyzed and the limited view has not been
5014 -- already installed then we install it.
5016 if not Analyzed
(P_Unit
) then
5017 if not In_Chain
(P
) then
5019 -- Minimum decoration
5021 Set_Ekind
(P
, E_Package
);
5022 Set_Etype
(P
, Standard_Void_Type
);
5023 Set_Scope
(P
, Standard_Standard
);
5024 Set_Is_Visible_Lib_Unit
(P
);
5026 if Is_Child_Package
then
5027 Set_Is_Child_Unit
(P
);
5028 Set_Scope
(P
, Defining_Entity
(Unit
(Parent_Spec
(P_Unit
))));
5031 -- Place entity on visibility structure
5033 Set_Homonym
(P
, Current_Entity
(P
));
5034 Set_Current_Entity
(P
);
5036 if Debug_Flag_I
then
5037 Write_Str
(" (homonym) chain ");
5038 Write_Name
(Chars
(P
));
5042 -- Install the incomplete view. The first element of the limited
5043 -- view is a header (an E_Package entity) used to reference the
5044 -- first shadow entity in the private part of the package.
5046 Lim_Header
:= Limited_View
(P
);
5047 Lim_Typ
:= First_Entity
(Lim_Header
);
5049 while Present
(Lim_Typ
)
5050 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
5052 Set_Homonym
(Lim_Typ
, Current_Entity
(Lim_Typ
));
5053 Set_Current_Entity
(Lim_Typ
);
5055 if Debug_Flag_I
then
5056 Write_Str
(" (homonym) chain ");
5057 Write_Name
(Chars
(Lim_Typ
));
5061 Next_Entity
(Lim_Typ
);
5065 -- If the unit appears in a previous regular with_clause, the regular
5066 -- entities of the public part of the withed package must be replaced
5067 -- by the shadow ones.
5069 -- This code must be kept synchronized with the code that replaces the
5070 -- shadow entities by the real entities (see body of Remove_Limited
5071 -- With_Clause); otherwise the contents of the homonym chains are not
5075 -- Hide all the type entities of the public part of the package to
5076 -- avoid its usage. This is needed to cover all the subtype decla-
5077 -- rations because we do not remove them from the homonym chain.
5079 E
:= First_Entity
(P
);
5080 while Present
(E
) and then E
/= First_Private_Entity
(P
) loop
5082 Set_Was_Hidden
(E
, Is_Hidden
(E
));
5089 -- Replace the real entities by the shadow entities of the limited
5090 -- view. The first element of the limited view is a header that is
5091 -- used to reference the first shadow entity in the private part
5092 -- of the package. Successive elements are the limited views of the
5093 -- type (including regular incomplete types) declared in the package.
5095 Lim_Header
:= Limited_View
(P
);
5097 Lim_Typ
:= First_Entity
(Lim_Header
);
5098 while Present
(Lim_Typ
)
5099 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
5101 pragma Assert
(not In_Chain
(Lim_Typ
));
5103 -- Do not unchain nested packages and child units
5105 if Ekind
(Lim_Typ
) /= E_Package
5106 and then not Is_Child_Unit
(Lim_Typ
)
5112 Prev
:= Current_Entity
(Lim_Typ
);
5115 -- Replace E in the homonyms list, so that the limited view
5116 -- becomes available.
5118 -- If the non-limited view is a record with an anonymous
5119 -- self-referential component, the analysis of the record
5120 -- declaration creates an incomplete type with the same name
5121 -- in order to define an internal access type. The visible
5122 -- entity is now the incomplete type, and that is the one to
5123 -- replace in the visibility structure.
5125 if E
= Non_Limited_View
(Lim_Typ
)
5127 (Ekind
(E
) = E_Incomplete_Type
5128 and then Full_View
(E
) = Non_Limited_View
(Lim_Typ
))
5130 Set_Homonym
(Lim_Typ
, Homonym
(Prev
));
5131 Set_Current_Entity
(Lim_Typ
);
5135 E
:= Homonym
(Prev
);
5137 -- E may have been removed when installing a previous
5138 -- limited_with_clause.
5141 exit when E
= Non_Limited_View
(Lim_Typ
);
5142 Prev
:= Homonym
(Prev
);
5146 Set_Homonym
(Lim_Typ
, Homonym
(Homonym
(Prev
)));
5147 Set_Homonym
(Prev
, Lim_Typ
);
5152 if Debug_Flag_I
then
5153 Write_Str
(" (homonym) chain ");
5154 Write_Name
(Chars
(Lim_Typ
));
5159 Next_Entity
(Lim_Typ
);
5163 -- The package must be visible while the limited-with clause is active
5164 -- because references to the type P.T must resolve in the usual way.
5165 -- In addition, we remember that the limited-view has been installed to
5166 -- uninstall it at the point of context removal.
5168 Set_Is_Immediately_Visible
(P
);
5169 Set_Limited_View_Installed
(N
);
5171 -- If unit has not been analyzed in some previous context, check
5172 -- (imperfectly ???) whether it might need a body.
5174 if not Analyzed
(P_Unit
) then
5175 Check_Body_Required
;
5178 -- If the package in the limited_with clause is a child unit, the clause
5179 -- is unanalyzed and appears as a selected component. Recast it as an
5180 -- expanded name so that the entity can be properly set. Use entity of
5181 -- parent, if available, for higher ancestors in the name.
5183 if Nkind
(Name
(N
)) = N_Selected_Component
then
5191 while Nkind
(Nam
) = N_Selected_Component
5192 and then Present
(Ent
)
5194 Change_Selected_Component_To_Expanded_Name
(Nam
);
5196 -- Set entity of parent identifiers if the unit is a child
5197 -- unit. This ensures that the tree is properly formed from
5198 -- semantic point of view (e.g. for ASIS queries). The unit
5199 -- entities are not fully analyzed, so we need to follow unit
5200 -- links in the tree.
5202 Set_Entity
(Nam
, Ent
);
5204 Nam
:= Prefix
(Nam
);
5207 (Unit
(Parent_Spec
(Unit_Declaration_Node
(Ent
))));
5209 -- Set entity of last ancestor
5211 if Nkind
(Nam
) = N_Identifier
then
5212 Set_Entity
(Nam
, Ent
);
5218 Set_Entity
(Name
(N
), P
);
5219 Set_From_Limited_With
(P
);
5220 end Install_Limited_Withed_Unit
;
5222 -------------------------
5223 -- Install_Withed_Unit --
5224 -------------------------
5226 procedure Install_Withed_Unit
5227 (With_Clause
: Node_Id
;
5228 Private_With_OK
: Boolean := False)
5230 Uname
: constant Entity_Id
:= Entity
(Name
(With_Clause
));
5231 P
: constant Entity_Id
:= Scope
(Uname
);
5234 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5235 -- compiling a package declaration and the Private_With_OK flag was not
5236 -- set by the caller. These declarations will be installed later (before
5237 -- analyzing the private part of the package).
5239 if Private_Present
(With_Clause
)
5240 and then Nkind
(Unit
(Parent
(With_Clause
))) = N_Package_Declaration
5241 and then not (Private_With_OK
)
5246 if Debug_Flag_I
then
5247 if Private_Present
(With_Clause
) then
5248 Write_Str
("install private withed unit ");
5250 Write_Str
("install withed unit ");
5253 Write_Name
(Chars
(Uname
));
5257 -- We do not apply the restrictions to an internal unit unless we are
5258 -- compiling the internal unit as a main unit. This check is also
5259 -- skipped for dummy units (for missing packages).
5261 if Sloc
(Uname
) /= No_Location
5262 and then (not Is_Internal_Unit
(Current_Sem_Unit
)
5263 or else Current_Sem_Unit
= Main_Unit
)
5265 Check_Restricted_Unit
5266 (Unit_Name
(Get_Source_Unit
(Uname
)), With_Clause
);
5269 if P
/= Standard_Standard
then
5271 -- If the unit is not analyzed after analysis of the with clause and
5272 -- it is an instantiation then it awaits a body and is the main unit.
5273 -- Its appearance in the context of some other unit indicates a
5274 -- circular dependency (DEC suite perversity).
5276 if not Analyzed
(Uname
)
5277 and then Nkind
(Parent
(Uname
)) = N_Package_Instantiation
5280 ("instantiation depends on itself", Name
(With_Clause
));
5282 elsif not Is_Visible_Lib_Unit
(Uname
) then
5284 -- Abandon processing in case of previous errors
5286 if No
(Scope
(Uname
)) then
5287 Check_Error_Detected
;
5291 Set_Is_Visible_Lib_Unit
(Uname
);
5293 -- If the unit is a wrapper package for a compilation unit that is
5294 -- a subprogrm instance, indicate that the instance itself is a
5295 -- visible unit. This is necessary if the instance is inlined.
5297 if Is_Wrapper_Package
(Uname
) then
5298 Set_Is_Visible_Lib_Unit
(Related_Instance
(Uname
));
5301 -- If the child unit appears in the context of its parent, it is
5302 -- immediately visible.
5304 if In_Open_Scopes
(Scope
(Uname
)) then
5305 Set_Is_Immediately_Visible
(Uname
);
5308 if Is_Generic_Instance
(Uname
)
5309 and then Ekind
(Uname
) in Subprogram_Kind
5311 -- Set flag as well on the visible entity that denotes the
5312 -- instance, which renames the current one.
5314 Set_Is_Visible_Lib_Unit
5316 (Defining_Entity
(Unit
(Library_Unit
(With_Clause
)))));
5319 -- The parent unit may have been installed already, and may have
5320 -- appeared in a use clause.
5322 if In_Use
(Scope
(Uname
)) then
5323 Set_Is_Potentially_Use_Visible
(Uname
);
5326 Set_Context_Installed
(With_Clause
);
5329 elsif not Is_Immediately_Visible
(Uname
) then
5330 Set_Is_Visible_Lib_Unit
(Uname
);
5332 if not Private_Present
(With_Clause
) or else Private_With_OK
then
5333 Set_Is_Immediately_Visible
(Uname
);
5336 Set_Context_Installed
(With_Clause
);
5339 -- A with-clause overrides a with-type clause: there are no restric-
5340 -- tions on the use of package entities.
5342 if Ekind
(Uname
) = E_Package
then
5343 Set_From_Limited_With
(Uname
, False);
5346 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5347 -- unit if there is a visible homograph for it declared in the same
5348 -- declarative region. This pathological case can only arise when an
5349 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5350 -- G1 has a generic child also named G2, and the context includes with_
5351 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5352 -- of I1.G2 visible as well. If the child unit is named Standard, do
5353 -- not apply the check to the Standard package itself.
5355 if Is_Child_Unit
(Uname
)
5356 and then Is_Visible_Lib_Unit
(Uname
)
5357 and then Ada_Version
>= Ada_2005
5360 Decl1
: constant Node_Id
:= Unit_Declaration_Node
(P
);
5366 U2
:= Homonym
(Uname
);
5367 while Present
(U2
) and then U2
/= Standard_Standard
loop
5369 Decl2
:= Unit_Declaration_Node
(P2
);
5371 if Is_Child_Unit
(U2
) and then Is_Visible_Lib_Unit
(U2
) then
5372 if Is_Generic_Instance
(P
)
5373 and then Nkind
(Decl1
) = N_Package_Declaration
5374 and then Generic_Parent
(Specification
(Decl1
)) = P2
5376 Error_Msg_N
("illegal with_clause", With_Clause
);
5378 ("\child unit has visible homograph" &
5379 " (RM 8.3(26), 10.1.1(19))",
5383 elsif Is_Generic_Instance
(P2
)
5384 and then Nkind
(Decl2
) = N_Package_Declaration
5385 and then Generic_Parent
(Specification
(Decl2
)) = P
5387 -- With_clause for child unit of instance appears before
5388 -- in the context. We want to place the error message on
5389 -- it, not on the generic child unit itself.
5392 Prev_Clause
: Node_Id
;
5395 Prev_Clause
:= First
(List_Containing
(With_Clause
));
5396 while Entity
(Name
(Prev_Clause
)) /= U2
loop
5400 pragma Assert
(Present
(Prev_Clause
));
5401 Error_Msg_N
("illegal with_clause", Prev_Clause
);
5403 ("\child unit has visible homograph" &
5404 " (RM 8.3(26), 10.1.1(19))",
5415 end Install_Withed_Unit
;
5421 function Is_Child_Spec
(Lib_Unit
: Node_Id
) return Boolean is
5422 K
: constant Node_Kind
:= Nkind
(Lib_Unit
);
5425 return (K
in N_Generic_Declaration
or else
5426 K
in N_Generic_Instantiation
or else
5427 K
in N_Generic_Renaming_Declaration
or else
5428 K
= N_Package_Declaration
or else
5429 K
= N_Package_Renaming_Declaration
or else
5430 K
= N_Subprogram_Declaration
or else
5431 K
= N_Subprogram_Renaming_Declaration
)
5432 and then Present
(Parent_Spec
(Lib_Unit
));
5435 ------------------------------------
5436 -- Is_Legal_Shadow_Entity_In_Body --
5437 ------------------------------------
5439 function Is_Legal_Shadow_Entity_In_Body
(T
: Entity_Id
) return Boolean is
5440 C_Unit
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
5442 return Nkind
(Unit
(C_Unit
)) = N_Package_Body
5445 (C_Unit
, Cunit_Entity
(Get_Source_Unit
(Non_Limited_View
(T
))));
5446 end Is_Legal_Shadow_Entity_In_Body
;
5448 ----------------------
5449 -- Is_Ancestor_Unit --
5450 ----------------------
5452 function Is_Ancestor_Unit
(U1
: Node_Id
; U2
: Node_Id
) return Boolean is
5453 E1
: constant Entity_Id
:= Defining_Entity
(Unit
(U1
));
5456 if Nkind_In
(Unit
(U2
), N_Package_Body
, N_Subprogram_Body
) then
5457 E2
:= Defining_Entity
(Unit
(Library_Unit
(U2
)));
5458 return Is_Ancestor_Package
(E1
, E2
);
5462 end Is_Ancestor_Unit
;
5464 -----------------------
5465 -- Load_Needed_Body --
5466 -----------------------
5468 -- N is a generic unit named in a with clause, or else it is a unit that
5469 -- contains a generic unit or an inlined function. In order to perform an
5470 -- instantiation, the body of the unit must be present. If the unit itself
5471 -- is generic, we assume that an instantiation follows, and load & analyze
5472 -- the body unconditionally. This forces analysis of the spec as well.
5474 -- If the unit is not generic, but contains a generic unit, it is loaded on
5475 -- demand, at the point of instantiation (see ch12).
5477 procedure Load_Needed_Body
5480 Do_Analyze
: Boolean := True)
5482 Body_Name
: Unit_Name_Type
;
5483 Unum
: Unit_Number_Type
;
5485 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
5486 -- The loading and analysis is done with style checks off
5489 if not GNAT_Mode
then
5490 Style_Check
:= False;
5493 Body_Name
:= Get_Body_Name
(Get_Unit_Name
(Unit
(N
)));
5496 (Load_Name
=> Body_Name
,
5502 if Unum
= No_Unit
then
5506 Compiler_State
:= Analyzing
; -- reset after load
5508 if Fatal_Error
(Unum
) /= Error_Detected
or else Try_Semantics
then
5509 if Debug_Flag_L
then
5510 Write_Str
("*** Loaded generic body");
5515 Semantics
(Cunit
(Unum
));
5522 Style_Check
:= Save_Style_Check
;
5523 end Load_Needed_Body
;
5525 -------------------------
5526 -- Build_Limited_Views --
5527 -------------------------
5529 procedure Build_Limited_Views
(N
: Node_Id
) is
5530 Unum
: constant Unit_Number_Type
:=
5531 Get_Source_Unit
(Library_Unit
(N
));
5532 Is_Analyzed
: constant Boolean := Analyzed
(Cunit
(Unum
));
5534 Shadow_Pack
: Entity_Id
;
5535 -- The corresponding shadow entity of the withed package. This entity
5536 -- offers incomplete views of packages and types as well as abstract
5537 -- views of states and variables declared within.
5539 Last_Shadow
: Entity_Id
:= Empty
;
5540 -- The last shadow entity created by routine Build_Shadow_Entity
5542 procedure Build_Shadow_Entity
5545 Shadow
: out Entity_Id
;
5546 Is_Tagged
: Boolean := False);
5547 -- Create a shadow entity that hides Ent and offers an abstract or
5548 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5549 -- should be set when Ent is a tagged type. The generated entity is
5550 -- added to Lim_Header. This routine updates the value of Last_Shadow.
5552 procedure Decorate_Package
(Ent
: Entity_Id
; Scop
: Entity_Id
);
5553 -- Perform minimal decoration of a package or its corresponding shadow
5554 -- entity denoted by Ent. Scop is the proper scope.
5556 procedure Decorate_State
(Ent
: Entity_Id
; Scop
: Entity_Id
);
5557 -- Perform full decoration of an abstract state or its corresponding
5558 -- shadow entity denoted by Ent. Scop is the proper scope.
5560 procedure Decorate_Type
5563 Is_Tagged
: Boolean := False;
5564 Materialize
: Boolean := False);
5565 -- Perform minimal decoration of a type or its corresponding shadow
5566 -- entity denoted by Ent. Scop is the proper scope. Flag Is_Tagged
5567 -- should be set when Ent is a tagged type. Flag Materialize should be
5568 -- set when Ent is a tagged type and its class-wide type needs to appear
5571 procedure Decorate_Variable
(Ent
: Entity_Id
; Scop
: Entity_Id
);
5572 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5575 procedure Process_Declarations_And_States
5579 Create_Abstract_Views
: Boolean);
5580 -- Inspect the states of package Pack and declarative list Decls. Create
5581 -- shadow entities for all nested packages, states, types and variables
5582 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5583 -- be set when the abstract states and variables need to be processed.
5585 -------------------------
5586 -- Build_Shadow_Entity --
5587 -------------------------
5589 procedure Build_Shadow_Entity
5592 Shadow
: out Entity_Id
;
5593 Is_Tagged
: Boolean := False)
5596 Shadow
:= Make_Temporary
(Sloc
(Ent
), 'Z');
5598 -- The shadow entity must share the same name and parent as the
5601 Set_Chars
(Shadow
, Chars
(Ent
));
5602 Set_Parent
(Shadow
, Parent
(Ent
));
5604 -- The abstract view of a variable is a state, not another variable
5606 if Ekind
(Ent
) = E_Variable
then
5607 Set_Ekind
(Shadow
, E_Abstract_State
);
5609 Set_Ekind
(Shadow
, Ekind
(Ent
));
5612 Set_Is_Internal
(Shadow
);
5613 Set_From_Limited_With
(Shadow
);
5615 -- Add the new shadow entity to the limited view of the package
5617 Last_Shadow
:= Shadow
;
5618 Append_Entity
(Shadow
, Shadow_Pack
);
5620 -- Perform context-specific decoration of the shadow entity
5622 if Ekind
(Ent
) = E_Abstract_State
then
5623 Decorate_State
(Shadow
, Scop
);
5624 Set_Non_Limited_View
(Shadow
, Ent
);
5626 elsif Ekind
(Ent
) = E_Package
then
5627 Decorate_Package
(Shadow
, Scop
);
5629 elsif Is_Type
(Ent
) then
5630 Decorate_Type
(Shadow
, Scop
, Is_Tagged
);
5631 Set_Non_Limited_View
(Shadow
, Ent
);
5634 Set_Non_Limited_View
5635 (Class_Wide_Type
(Shadow
), Class_Wide_Type
(Ent
));
5638 if Is_Incomplete_Or_Private_Type
(Ent
) then
5639 Set_Private_Dependents
(Shadow
, New_Elmt_List
);
5642 elsif Ekind
(Ent
) = E_Variable
then
5643 Decorate_State
(Shadow
, Scop
);
5644 Set_Non_Limited_View
(Shadow
, Ent
);
5646 end Build_Shadow_Entity
;
5648 ----------------------
5649 -- Decorate_Package --
5650 ----------------------
5652 procedure Decorate_Package
(Ent
: Entity_Id
; Scop
: Entity_Id
) is
5654 Set_Ekind
(Ent
, E_Package
);
5655 Set_Etype
(Ent
, Standard_Void_Type
);
5656 Set_Scope
(Ent
, Scop
);
5657 end Decorate_Package
;
5659 --------------------
5660 -- Decorate_State --
5661 --------------------
5663 procedure Decorate_State
(Ent
: Entity_Id
; Scop
: Entity_Id
) is
5665 Set_Ekind
(Ent
, E_Abstract_State
);
5666 Set_Etype
(Ent
, Standard_Void_Type
);
5667 Set_Scope
(Ent
, Scop
);
5668 Set_Encapsulating_State
(Ent
, Empty
);
5675 procedure Decorate_Type
5678 Is_Tagged
: Boolean := False;
5679 Materialize
: Boolean := False)
5684 -- An unanalyzed type or a shadow entity of a type is treated as an
5685 -- incomplete type, and carries the corresponding attributes.
5687 Set_Ekind
(Ent
, E_Incomplete_Type
);
5688 Set_Etype
(Ent
, Ent
);
5689 Set_Full_View
(Ent
, Empty
);
5690 Set_Is_First_Subtype
(Ent
);
5691 Set_Scope
(Ent
, Scop
);
5692 Set_Stored_Constraint
(Ent
, No_Elist
);
5693 Init_Size_Align
(Ent
);
5695 if From_Limited_With
(Ent
) then
5696 Set_Private_Dependents
(Ent
, New_Elmt_List
);
5699 -- A tagged type and its corresponding shadow entity share one common
5700 -- class-wide type. The list of primitive operations for the shadow
5704 Set_Is_Tagged_Type
(Ent
);
5705 Set_Direct_Primitive_Operations
(Ent
, New_Elmt_List
);
5709 (E_Void
, Scope
(Ent
), Sloc
(Ent
), Ent
, 'C', 0, 'T');
5711 Set_Class_Wide_Type
(Ent
, CW_Typ
);
5713 -- Set parent to be the same as the parent of the tagged type.
5714 -- We need a parent field set, and it is supposed to point to
5715 -- the declaration of the type. The tagged type declaration
5716 -- essentially declares two separate types, the tagged type
5717 -- itself and the corresponding class-wide type, so it is
5718 -- reasonable for the parent fields to point to the declaration
5721 Set_Parent
(CW_Typ
, Parent
(Ent
));
5723 Set_Ekind
(CW_Typ
, E_Class_Wide_Type
);
5724 Set_Class_Wide_Type
(CW_Typ
, CW_Typ
);
5725 Set_Etype
(CW_Typ
, Ent
);
5726 Set_Equivalent_Type
(CW_Typ
, Empty
);
5727 Set_From_Limited_With
(CW_Typ
, From_Limited_With
(Ent
));
5728 Set_Has_Unknown_Discriminants
(CW_Typ
);
5729 Set_Is_First_Subtype
(CW_Typ
);
5730 Set_Is_Tagged_Type
(CW_Typ
);
5731 Set_Materialize_Entity
(CW_Typ
, Materialize
);
5732 Set_Scope
(CW_Typ
, Scop
);
5733 Init_Size_Align
(CW_Typ
);
5737 -----------------------
5738 -- Decorate_Variable --
5739 -----------------------
5741 procedure Decorate_Variable
(Ent
: Entity_Id
; Scop
: Entity_Id
) is
5743 Set_Ekind
(Ent
, E_Variable
);
5744 Set_Etype
(Ent
, Standard_Void_Type
);
5745 Set_Scope
(Ent
, Scop
);
5746 end Decorate_Variable
;
5748 -------------------------------------
5749 -- Process_Declarations_And_States --
5750 -------------------------------------
5752 procedure Process_Declarations_And_States
5756 Create_Abstract_Views
: Boolean)
5758 procedure Find_And_Process_States
;
5759 -- Determine whether package Pack defines abstract state either by
5760 -- using an aspect or a pragma. If this is the case, build shadow
5761 -- entities for all abstract states of Pack.
5763 procedure Process_States
(States
: Elist_Id
);
5764 -- Generate shadow entities for all abstract states in list States
5766 -----------------------------
5767 -- Find_And_Process_States --
5768 -----------------------------
5770 procedure Find_And_Process_States
is
5771 procedure Process_State
(State
: Node_Id
);
5772 -- Generate shadow entities for a single abstract state or
5773 -- multiple states expressed as an aggregate.
5779 procedure Process_State
(State
: Node_Id
) is
5780 Loc
: constant Source_Ptr
:= Sloc
(State
);
5787 -- Multiple abstract states appear as an aggregate
5789 if Nkind
(State
) = N_Aggregate
then
5790 Elmt
:= First
(Expressions
(State
));
5791 while Present
(Elmt
) loop
5792 Process_State
(Elmt
);
5798 -- A null state has no abstract view
5800 elsif Nkind
(State
) = N_Null
then
5803 -- State declaration with various options appears as an
5804 -- extension aggregate.
5806 elsif Nkind
(State
) = N_Extension_Aggregate
then
5807 Decl
:= Ancestor_Part
(State
);
5809 -- Simple state declaration
5811 elsif Nkind
(State
) = N_Identifier
then
5814 -- Possibly an illegal state declaration
5820 -- Abstract states are elaborated when the related pragma is
5821 -- elaborated. Since the withed package is not analyzed yet,
5822 -- the entities of the abstract states are not available. To
5823 -- overcome this complication, create the entities now and
5824 -- store them in their respective declarations. The entities
5825 -- are later used by routine Create_Abstract_State to declare
5826 -- and enter the states into visibility.
5828 if No
(Entity
(Decl
)) then
5829 Id
:= Make_Defining_Identifier
(Loc
, Chars
(Decl
));
5831 Set_Entity
(Decl
, Id
);
5832 Set_Parent
(Id
, State
);
5833 Decorate_State
(Id
, Scop
);
5835 -- Otherwise the package was previously withed
5838 Id
:= Entity
(Decl
);
5841 Build_Shadow_Entity
(Id
, Scop
, Dummy
);
5846 Pack_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Pack
);
5850 -- Start of processing for Find_And_Process_States
5853 -- Find aspect Abstract_State
5855 Asp
:= First
(Aspect_Specifications
(Pack_Decl
));
5856 while Present
(Asp
) loop
5857 if Chars
(Identifier
(Asp
)) = Name_Abstract_State
then
5858 Process_State
(Expression
(Asp
));
5866 -- Find pragma Abstract_State by inspecting the declarations
5868 Decl
:= First
(Decls
);
5869 while Present
(Decl
) and then Nkind
(Decl
) = N_Pragma
loop
5870 if Pragma_Name
(Decl
) = Name_Abstract_State
then
5873 (First
(Pragma_Argument_Associations
(Decl
))));
5880 end Find_And_Process_States
;
5882 --------------------
5883 -- Process_States --
5884 --------------------
5886 procedure Process_States
(States
: Elist_Id
) is
5891 Elmt
:= First_Elmt
(States
);
5892 while Present
(Elmt
) loop
5893 Build_Shadow_Entity
(Node
(Elmt
), Scop
, Dummy
);
5901 Is_Tagged
: Boolean;
5907 -- Start of processing for Process_Declarations_And_States
5910 -- Build abstract views for all states defined in the package
5912 if Create_Abstract_Views
then
5914 -- When a package has been analyzed, all states are stored in list
5915 -- Abstract_States. Generate the shadow entities directly.
5918 if Present
(Abstract_States
(Pack
)) then
5919 Process_States
(Abstract_States
(Pack
));
5922 -- The package may declare abstract states by using an aspect or a
5923 -- pragma. Attempt to locate one of these construct and if found,
5924 -- build the shadow entities.
5927 Find_And_Process_States
;
5931 -- Inspect the declarative list, looking for nested packages, types
5932 -- and variable declarations.
5934 Decl
:= First
(Decls
);
5935 while Present
(Decl
) loop
5939 if Nkind
(Decl
) = N_Package_Declaration
then
5940 Def_Id
:= Defining_Entity
(Decl
);
5942 -- Perform minor decoration when the withed package has not
5945 if not Is_Analyzed
then
5946 Decorate_Package
(Def_Id
, Scop
);
5949 -- Create a shadow entity that offers a limited view of all
5950 -- visible types declared within.
5952 Build_Shadow_Entity
(Def_Id
, Scop
, Shadow
);
5954 Process_Declarations_And_States
5956 Decls
=> Visible_Declarations
(Specification
(Decl
)),
5958 Create_Abstract_Views
=> Create_Abstract_Views
);
5962 elsif Nkind_In
(Decl
, N_Full_Type_Declaration
,
5963 N_Incomplete_Type_Declaration
,
5964 N_Private_Extension_Declaration
,
5965 N_Private_Type_Declaration
,
5966 N_Protected_Type_Declaration
,
5967 N_Task_Type_Declaration
)
5969 Def_Id
:= Defining_Entity
(Decl
);
5971 -- Determine whether the type is tagged. Note that packages
5972 -- included via a limited with clause are not always analyzed,
5973 -- hence the tree lookup rather than the use of attribute
5976 if Nkind
(Decl
) = N_Full_Type_Declaration
then
5977 Def
:= Type_Definition
(Decl
);
5980 (Nkind
(Def
) = N_Record_Definition
5981 and then Tagged_Present
(Def
))
5983 (Nkind
(Def
) = N_Derived_Type_Definition
5984 and then Present
(Record_Extension_Part
(Def
)));
5986 elsif Nkind_In
(Decl
, N_Incomplete_Type_Declaration
,
5987 N_Private_Type_Declaration
)
5989 Is_Tagged
:= Tagged_Present
(Decl
);
5991 elsif Nkind
(Decl
) = N_Private_Extension_Declaration
then
5998 -- Perform minor decoration when the withed package has not
6001 if not Is_Analyzed
then
6002 Decorate_Type
(Def_Id
, Scop
, Is_Tagged
, True);
6005 -- Create a shadow entity that hides the type and offers an
6006 -- incomplete view of the said type.
6008 Build_Shadow_Entity
(Def_Id
, Scop
, Shadow
, Is_Tagged
);
6012 elsif Create_Abstract_Views
6013 and then Nkind
(Decl
) = N_Object_Declaration
6014 and then not Constant_Present
(Decl
)
6016 Def_Id
:= Defining_Entity
(Decl
);
6018 -- Perform minor decoration when the withed package has not
6021 if not Is_Analyzed
then
6022 Decorate_Variable
(Def_Id
, Scop
);
6025 -- Create a shadow entity that hides the variable and offers an
6026 -- abstract view of the said variable.
6028 Build_Shadow_Entity
(Def_Id
, Scop
, Shadow
);
6033 end Process_Declarations_And_States
;
6037 Nam
: constant Node_Id
:= Name
(N
);
6038 Pack
: constant Entity_Id
:= Cunit_Entity
(Unum
);
6040 Last_Public_Shadow
: Entity_Id
:= Empty
;
6041 Private_Shadow
: Entity_Id
;
6044 -- Start of processing for Build_Limited_Views
6047 pragma Assert
(Limited_Present
(N
));
6049 -- A library_item mentioned in a limited_with_clause is a package
6050 -- declaration, not a subprogram declaration, generic declaration,
6051 -- generic instantiation, or package renaming declaration.
6053 case Nkind
(Unit
(Library_Unit
(N
))) is
6054 when N_Package_Declaration
=>
6057 when N_Subprogram_Declaration
=>
6058 Error_Msg_N
("subprograms not allowed in limited with_clauses", N
);
6061 when N_Generic_Package_Declaration
6062 | N_Generic_Subprogram_Declaration
6064 Error_Msg_N
("generics not allowed in limited with_clauses", N
);
6067 when N_Generic_Instantiation
=>
6069 ("generic instantiations not allowed in limited with_clauses",
6073 when N_Generic_Renaming_Declaration
=>
6075 ("generic renamings not allowed in limited with_clauses", N
);
6078 when N_Subprogram_Renaming_Declaration
=>
6080 ("renamed subprograms not allowed in limited with_clauses", N
);
6083 when N_Package_Renaming_Declaration
=>
6085 ("renamed packages not allowed in limited with_clauses", N
);
6089 raise Program_Error
;
6092 -- The withed unit may not be analyzed, but the with calause itself
6093 -- must be minimally decorated. This ensures that the checks on unused
6094 -- with clauses also process limieted withs.
6096 Set_Ekind
(Pack
, E_Package
);
6097 Set_Etype
(Pack
, Standard_Void_Type
);
6099 if Is_Entity_Name
(Nam
) then
6100 Set_Entity
(Nam
, Pack
);
6102 elsif Nkind
(Nam
) = N_Selected_Component
then
6103 Set_Entity
(Selector_Name
(Nam
), Pack
);
6106 -- Check if the chain is already built
6108 Spec
:= Specification
(Unit
(Library_Unit
(N
)));
6110 if Limited_View_Installed
(Spec
) then
6114 -- Create the shadow package wich hides the withed unit and provides
6115 -- incomplete view of all types and packages declared within.
6117 Shadow_Pack
:= Make_Temporary
(Sloc
(N
), 'Z');
6118 Set_Ekind
(Shadow_Pack
, E_Package
);
6119 Set_Is_Internal
(Shadow_Pack
);
6120 Set_Limited_View
(Pack
, Shadow_Pack
);
6122 -- Inspect the abstract states and visible declarations of the withed
6123 -- unit and create shadow entities that hide existing packages, states,
6124 -- variables and types.
6126 Process_Declarations_And_States
6128 Decls
=> Visible_Declarations
(Spec
),
6130 Create_Abstract_Views
=> True);
6132 Last_Public_Shadow
:= Last_Shadow
;
6134 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6135 -- to accommodate limited private with clauses.
6137 Process_Declarations_And_States
6139 Decls
=> Private_Declarations
(Spec
),
6141 Create_Abstract_Views
=> False);
6143 if Present
(Last_Public_Shadow
) then
6144 Private_Shadow
:= Next_Entity
(Last_Public_Shadow
);
6146 Private_Shadow
:= First_Entity
(Shadow_Pack
);
6149 Set_First_Private_Entity
(Shadow_Pack
, Private_Shadow
);
6150 Set_Limited_View_Installed
(Spec
);
6151 end Build_Limited_Views
;
6153 ----------------------------
6154 -- Check_No_Elab_Code_All --
6155 ----------------------------
6157 procedure Check_No_Elab_Code_All
(N
: Node_Id
) is
6159 if Present
(No_Elab_Code_All_Pragma
)
6160 and then In_Extended_Main_Source_Unit
(N
)
6161 and then Present
(Context_Items
(N
))
6164 CL
: constant List_Id
:= Context_Items
(N
);
6169 while Present
(CI
) loop
6170 if Nkind
(CI
) = N_With_Clause
6172 No_Elab_Code_All
(Get_Source_Unit
(Library_Unit
(CI
)))
6174 -- In GNATprove mode, some runtime units are implicitly
6175 -- loaded to make their entities available for analysis. In
6176 -- this case, ignore violations of No_Elaboration_Code_All
6177 -- for this special analysis mode.
6180 (GNATprove_Mode
and then Implicit_With
(CI
))
6182 Error_Msg_Sloc
:= Sloc
(No_Elab_Code_All_Pragma
);
6184 ("violation of No_Elaboration_Code_All#", CI
);
6186 ("\unit& does not have No_Elaboration_Code_All",
6187 CI
, Entity
(Name
(CI
)));
6194 end Check_No_Elab_Code_All
;
6196 -------------------------------
6197 -- Check_Body_Needed_For_SAL --
6198 -------------------------------
6200 procedure Check_Body_Needed_For_SAL
(Unit_Name
: Entity_Id
) is
6201 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean;
6202 -- Determine whether use of entity E might require the presence of its
6203 -- body. For a package this requires a recursive traversal of all nested
6206 -----------------------
6207 -- Entity_Needs_Body --
6208 -----------------------
6210 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean is
6214 if Is_Subprogram
(E
) and then Has_Pragma_Inline
(E
) then
6217 elsif Ekind_In
(E
, E_Generic_Function
, E_Generic_Procedure
) then
6219 -- A generic subprogram always requires the presence of its
6220 -- body because an instantiation needs both templates. The only
6221 -- exceptions is a generic subprogram renaming. In this case the
6222 -- body is needed only when the template is declared outside the
6223 -- compilation unit being checked.
6225 if Present
(Renamed_Entity
(E
)) then
6226 return not Within_Scope
(E
, Unit_Name
);
6231 elsif Ekind
(E
) = E_Generic_Package
6233 Nkind
(Unit_Declaration_Node
(E
)) = N_Generic_Package_Declaration
6234 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
6238 elsif Ekind
(E
) = E_Package
6239 and then Nkind
(Unit_Declaration_Node
(E
)) = N_Package_Declaration
6240 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
6242 Ent
:= First_Entity
(E
);
6243 while Present
(Ent
) loop
6244 if Entity_Needs_Body
(Ent
) then
6256 end Entity_Needs_Body
;
6258 -- Start of processing for Check_Body_Needed_For_SAL
6261 if Ekind
(Unit_Name
) = E_Generic_Package
6262 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
6263 N_Generic_Package_Declaration
6265 Present
(Corresponding_Body
(Unit_Declaration_Node
(Unit_Name
)))
6267 Set_Body_Needed_For_SAL
(Unit_Name
);
6269 elsif Ekind_In
(Unit_Name
, E_Generic_Procedure
, E_Generic_Function
) then
6270 Set_Body_Needed_For_SAL
(Unit_Name
);
6272 elsif Is_Subprogram
(Unit_Name
)
6273 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
6274 N_Subprogram_Declaration
6275 and then Has_Pragma_Inline
(Unit_Name
)
6277 Set_Body_Needed_For_SAL
(Unit_Name
);
6279 elsif Ekind
(Unit_Name
) = E_Subprogram_Body
then
6280 Check_Body_Needed_For_SAL
6281 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
6283 elsif Ekind
(Unit_Name
) = E_Package
6284 and then Entity_Needs_Body
(Unit_Name
)
6286 Set_Body_Needed_For_SAL
(Unit_Name
);
6288 elsif Ekind
(Unit_Name
) = E_Package_Body
6289 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) = N_Package_Body
6291 Check_Body_Needed_For_SAL
6292 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
6294 end Check_Body_Needed_For_SAL
;
6296 --------------------
6297 -- Remove_Context --
6298 --------------------
6300 procedure Remove_Context
(N
: Node_Id
) is
6301 Lib_Unit
: constant Node_Id
:= Unit
(N
);
6304 -- If this is a child unit, first remove the parent units
6306 if Is_Child_Spec
(Lib_Unit
) then
6307 Remove_Parents
(Lib_Unit
);
6310 Remove_Context_Clauses
(N
);
6313 ----------------------------
6314 -- Remove_Context_Clauses --
6315 ----------------------------
6317 procedure Remove_Context_Clauses
(N
: Node_Id
) is
6319 Unit_Name
: Entity_Id
;
6322 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6323 -- limited-views first and regular-views later (to maintain the
6326 -- First Phase: Remove limited_with context clauses
6328 Item
:= First
(Context_Items
(N
));
6329 while Present
(Item
) loop
6331 -- We are interested only in with clauses which got installed
6334 if Nkind
(Item
) = N_With_Clause
6335 and then Limited_Present
(Item
)
6336 and then Limited_View_Installed
(Item
)
6338 Remove_Limited_With_Clause
(Item
);
6344 -- Second Phase: Loop through context items and undo regular
6345 -- with_clauses and use_clauses.
6347 Item
:= First
(Context_Items
(N
));
6348 while Present
(Item
) loop
6350 -- We are interested only in with clauses which got installed on
6351 -- entry, as indicated by their Context_Installed flag set
6353 if Nkind
(Item
) = N_With_Clause
6354 and then Limited_Present
(Item
)
6355 and then Limited_View_Installed
(Item
)
6359 elsif Nkind
(Item
) = N_With_Clause
6360 and then Context_Installed
(Item
)
6362 -- Remove items from one with'ed unit
6364 Unit_Name
:= Entity
(Name
(Item
));
6365 Remove_Unit_From_Visibility
(Unit_Name
);
6366 Set_Context_Installed
(Item
, False);
6368 elsif Nkind
(Item
) = N_Use_Package_Clause
then
6369 End_Use_Package
(Item
);
6371 elsif Nkind
(Item
) = N_Use_Type_Clause
then
6372 End_Use_Type
(Item
);
6377 end Remove_Context_Clauses
;
6379 --------------------------------
6380 -- Remove_Limited_With_Clause --
6381 --------------------------------
6383 procedure Remove_Limited_With_Clause
(N
: Node_Id
) is
6384 P_Unit
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
6387 Lim_Header
: Entity_Id
;
6388 Lim_Typ
: Entity_Id
;
6392 pragma Assert
(Limited_View_Installed
(N
));
6394 -- In case of limited with_clause on subprograms, generics, instances,
6395 -- or renamings, the corresponding error was previously posted and we
6396 -- have nothing to do here.
6398 if Nkind
(P_Unit
) /= N_Package_Declaration
then
6402 P
:= Defining_Unit_Name
(Specification
(P_Unit
));
6404 -- Handle child packages
6406 if Nkind
(P
) = N_Defining_Program_Unit_Name
then
6407 P
:= Defining_Identifier
(P
);
6410 if Debug_Flag_I
then
6411 Write_Str
("remove limited view of ");
6412 Write_Name
(Chars
(P
));
6413 Write_Str
(" from visibility");
6417 -- Prepare the removal of the shadow entities from visibility. The first
6418 -- element of the limited view is a header (an E_Package entity) that is
6419 -- used to reference the first shadow entity in the private part of the
6422 Lim_Header
:= Limited_View
(P
);
6423 Lim_Typ
:= First_Entity
(Lim_Header
);
6425 -- Remove package and shadow entities from visibility if it has not
6428 if not Analyzed
(P_Unit
) then
6430 Set_Is_Immediately_Visible
(P
, False);
6432 while Present
(Lim_Typ
) loop
6434 Next_Entity
(Lim_Typ
);
6437 -- Otherwise this package has already appeared in the closure and its
6438 -- shadow entities must be replaced by its real entities. This code
6439 -- must be kept synchronized with the complementary code in Install
6440 -- Limited_Withed_Unit.
6443 -- If the limited_with_clause is in some other unit in the context
6444 -- then it is not visible in the main unit.
6446 if not In_Extended_Main_Source_Unit
(N
) then
6447 Set_Is_Immediately_Visible
(P
, False);
6450 -- Real entities that are type or subtype declarations were hidden
6451 -- from visibility at the point of installation of the limited-view.
6452 -- Now we recover the previous value of the hidden attribute.
6454 E
:= First_Entity
(P
);
6455 while Present
(E
) and then E
/= First_Private_Entity
(P
) loop
6457 Set_Is_Hidden
(E
, Was_Hidden
(E
));
6463 while Present
(Lim_Typ
)
6464 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
6466 -- Nested packages and child units were not unchained
6468 if Ekind
(Lim_Typ
) /= E_Package
6469 and then not Is_Child_Unit
(Non_Limited_View
(Lim_Typ
))
6471 -- If the package has incomplete types, the limited view of the
6472 -- incomplete type is in fact never visible (AI05-129) but we
6473 -- have created a shadow entity E1 for it, that points to E2,
6474 -- a non-limited incomplete type. This in turn has a full view
6475 -- E3 that is the full declaration. There is a corresponding
6476 -- shadow entity E4. When reinstalling the non-limited view,
6477 -- E2 must become the current entity and E3 must be ignored.
6479 E
:= Non_Limited_View
(Lim_Typ
);
6481 if Present
(Current_Entity
(E
))
6482 and then Ekind
(Current_Entity
(E
)) = E_Incomplete_Type
6483 and then Full_View
(Current_Entity
(E
)) = E
6486 -- Lim_Typ is the limited view of a full type declaration
6487 -- that has a previous incomplete declaration, i.e. E3 from
6488 -- the previous description. Nothing to insert.
6493 pragma Assert
(not In_Chain
(E
));
6495 Prev
:= Current_Entity
(Lim_Typ
);
6497 if Prev
= Lim_Typ
then
6498 Set_Current_Entity
(E
);
6501 while Present
(Prev
)
6502 and then Homonym
(Prev
) /= Lim_Typ
6504 Prev
:= Homonym
(Prev
);
6507 if Present
(Prev
) then
6508 Set_Homonym
(Prev
, E
);
6512 -- Preserve structure of homonym chain
6514 Set_Homonym
(E
, Homonym
(Lim_Typ
));
6518 Next_Entity
(Lim_Typ
);
6522 -- Indicate that the limited view of the package is not installed
6524 Set_From_Limited_With
(P
, False);
6525 Set_Limited_View_Installed
(N
, False);
6526 end Remove_Limited_With_Clause
;
6528 --------------------
6529 -- Remove_Parents --
6530 --------------------
6532 procedure Remove_Parents
(Lib_Unit
: Node_Id
) is
6535 P_Spec
: Node_Id
:= Empty
;
6537 Vis
: constant Boolean :=
6538 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
;
6541 if Is_Child_Spec
(Lib_Unit
) then
6542 P_Spec
:= Parent_Spec
(Lib_Unit
);
6544 elsif Nkind
(Lib_Unit
) = N_Package_Body
6545 and then Nkind
(Original_Node
(Lib_Unit
)) = N_Package_Instantiation
6547 P_Spec
:= Parent_Spec
(Original_Node
(Lib_Unit
));
6550 if Present
(P_Spec
) then
6552 P_Name
:= Get_Parent_Entity
(P
);
6553 Remove_Context_Clauses
(P_Spec
);
6554 End_Package_Scope
(P_Name
);
6555 Set_Is_Immediately_Visible
(P_Name
, Vis
);
6557 -- Remove from visibility the siblings as well, which are directly
6558 -- visible while the parent is in scope.
6560 E
:= First_Entity
(P_Name
);
6561 while Present
(E
) loop
6562 if Is_Child_Unit
(E
) then
6563 Set_Is_Immediately_Visible
(E
, False);
6569 Set_In_Package_Body
(P_Name
, False);
6571 -- This is the recursive call to remove the context of any higher
6572 -- level parent. This recursion ensures that all parents are removed
6573 -- in the reverse order of their installation.
6579 ---------------------------------
6580 -- Remove_Private_With_Clauses --
6581 ---------------------------------
6583 procedure Remove_Private_With_Clauses
(Comp_Unit
: Node_Id
) is
6586 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean;
6587 -- Check whether a given unit appears in a regular with_clause. Used to
6588 -- determine whether a private_with_clause, implicit or explicit, should
6591 ----------------------------
6592 -- In_Regular_With_Clause --
6593 ----------------------------
6595 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean
6600 Item
:= First
(Context_Items
(Comp_Unit
));
6601 while Present
(Item
) loop
6602 if Nkind
(Item
) = N_With_Clause
6604 -- The following guard is needed to ensure that the name has
6605 -- been properly analyzed before we go fetching its entity.
6607 and then Is_Entity_Name
(Name
(Item
))
6608 and then Entity
(Name
(Item
)) = E
6609 and then not Private_Present
(Item
)
6617 end In_Regular_With_Clause
;
6619 -- Start of processing for Remove_Private_With_Clauses
6622 Item
:= First
(Context_Items
(Comp_Unit
));
6623 while Present
(Item
) loop
6624 if Nkind
(Item
) = N_With_Clause
and then Private_Present
(Item
) then
6626 -- If private_with_clause is redundant, remove it from context,
6627 -- as a small optimization to subsequent handling of private_with
6628 -- clauses in other nested packages. We replace the clause with
6629 -- a null statement, which is otherwise ignored by the rest of
6630 -- the compiler, so that ASIS tools can reconstruct the source.
6632 if In_Regular_With_Clause
(Entity
(Name
(Item
))) then
6634 Nxt
: constant Node_Id
:= Next
(Item
);
6636 Rewrite
(Item
, Make_Null_Statement
(Sloc
(Item
)));
6641 elsif Limited_Present
(Item
) then
6642 if not Limited_View_Installed
(Item
) then
6643 Remove_Limited_With_Clause
(Item
);
6649 Remove_Unit_From_Visibility
(Entity
(Name
(Item
)));
6650 Set_Context_Installed
(Item
, False);
6658 end Remove_Private_With_Clauses
;
6660 ---------------------------------
6661 -- Remove_Unit_From_Visibility --
6662 ---------------------------------
6664 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
) is
6666 if Debug_Flag_I
then
6667 Write_Str
("remove unit ");
6668 Write_Name
(Chars
(Unit_Name
));
6669 Write_Str
(" from visibility");
6673 Set_Is_Visible_Lib_Unit
(Unit_Name
, False);
6674 Set_Is_Potentially_Use_Visible
(Unit_Name
, False);
6675 Set_Is_Immediately_Visible
(Unit_Name
, False);
6677 -- If the unit is a wrapper package, the subprogram instance is
6678 -- what must be removed from visibility.
6679 -- Should we use Related_Instance instead???
6681 if Is_Wrapper_Package
(Unit_Name
) then
6682 Set_Is_Immediately_Visible
(Current_Entity
(Unit_Name
), False);
6684 end Remove_Unit_From_Visibility
;
6699 procedure Unchain
(E
: Entity_Id
) is
6703 Prev
:= Current_Entity
(E
);
6709 Set_Name_Entity_Id
(Chars
(E
), Homonym
(E
));
6712 while Present
(Prev
) and then Homonym
(Prev
) /= E
loop
6713 Prev
:= Homonym
(Prev
);
6716 if Present
(Prev
) then
6717 Set_Homonym
(Prev
, Homonym
(E
));
6721 if Debug_Flag_I
then
6722 Write_Str
(" (homonym) unchain ");
6723 Write_Name
(Chars
(E
));