1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, 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 Impunit
; use Impunit
;
38 with Inline
; use Inline
;
40 with Lib
.Load
; use Lib
.Load
;
41 with Lib
.Xref
; use Lib
.Xref
;
42 with Namet
; use Namet
;
43 with Nlists
; use Nlists
;
44 with Nmake
; use Nmake
;
46 with Output
; use Output
;
47 with Par_SCO
; use Par_SCO
;
48 with Restrict
; use Restrict
;
49 with Rident
; use Rident
;
50 with Rtsfind
; use Rtsfind
;
52 with Sem_Aux
; use Sem_Aux
;
53 with Sem_Ch3
; use Sem_Ch3
;
54 with Sem_Ch6
; use Sem_Ch6
;
55 with Sem_Ch7
; use Sem_Ch7
;
56 with Sem_Ch8
; use Sem_Ch8
;
57 with Sem_Ch13
; use Sem_Ch13
;
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_With_Clause
(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_Parents
158 Is_Private
: Boolean;
159 Chain
: Boolean := True);
160 -- This procedure establishes the context for the compilation of a child
161 -- unit. If Lib_Unit is a child library spec then the context of the parent
162 -- is installed, and the parent itself made immediately visible, so that
163 -- the child unit is processed in the declarative region of the parent.
164 -- Install_Parents makes a recursive call to itself to ensure that all
165 -- parents are loaded in the nested case. If Lib_Unit is a library body,
166 -- the only effect of Install_Parents is to install the private decls of
167 -- the parents, because the visible parent declarations will have been
168 -- installed as part of the context of the corresponding spec. The flag
169 -- Chain is used to control the "chaining" or linking of use-type and
170 -- use-package clauses to avoid circularities when installing context.
172 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
);
173 -- In the compilation of a child unit, a child of any of the ancestor
174 -- units is directly visible if it is visible, because the parent is in
175 -- an enclosing scope. Iterate over context to find child units of U_Name
176 -- or of some ancestor of it.
178 procedure Install_With_Clause
179 (With_Clause
: Node_Id
;
180 Private_With_OK
: Boolean := False);
181 -- If the unit is not a child unit, make unit immediately visible. The
182 -- caller ensures that the unit is not already currently installed. The
183 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
184 -- is called when compiling the private part of a package, or installing
185 -- the private declarations of a parent unit.
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 the shadow entities from visibility introduced for a package
207 -- mentioned in limited with clause N. Implements Ada 2005 (AI-50217).
209 procedure Remove_Limited_With_Unit
210 (Pack_Decl
: Node_Id
;
211 Lim_Clause
: Node_Id
:= Empty
);
212 -- Remove the shadow entities from visibility introduced for a package
213 -- denoted by declaration Pack_Decl. Lim_Clause is the related limited
214 -- with clause, if any. Implements Ada 2005 (AI-50217).
216 procedure Remove_Parents
(Lib_Unit
: Node_Id
);
217 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
218 -- contexts established by the corresponding call to Install_Parents are
219 -- removed. Remove_Parents contains a recursive call to itself to ensure
220 -- that all parents are removed in the nested case.
222 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
);
223 -- Reset all visibility flags on unit after compiling it, either as a main
224 -- unit or as a unit in the context.
226 procedure Unchain
(E
: Entity_Id
);
227 -- Remove single entity from visibility list
230 -- A dummy procedure, for debugging use, called just before analyzing the
231 -- main unit (after dealing with any context clauses).
233 --------------------------
234 -- Limited_With_Clauses --
235 --------------------------
237 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
238 -- mutually recursive types declared in different units. A limited_with
239 -- clause that names package P in the context of unit U makes the types
240 -- declared in the visible part of P available within U, but with the
241 -- restriction that these types can only be used as incomplete types.
242 -- The limited_with clause does not impose a semantic dependence on P,
243 -- and it is possible for two packages to have limited_with_clauses on
244 -- each other without creating an elaboration circularity.
246 -- To support this feature, the analysis of a limited_with clause must
247 -- create an abbreviated view of the package, without performing any
248 -- semantic analysis on it. This "package abstract" contains shadow types
249 -- that are in one-one correspondence with the real types in the package,
250 -- and that have the properties of incomplete types.
252 -- The implementation creates two element lists: one to chain the shadow
253 -- entities, and one to chain the corresponding type entities in the tree
254 -- of the package. Links between corresponding entities in both chains
255 -- allow the compiler to select the proper view of a given type, depending
256 -- on the context. Note that in contrast with the handling of private
257 -- types, the limited view and the nonlimited view of a type are treated
258 -- as separate entities, and no entity exchange needs to take place, which
259 -- makes the implementation much simpler than could be feared.
261 ------------------------------
262 -- Analyze_Compilation_Unit --
263 ------------------------------
265 procedure Analyze_Compilation_Unit
(N
: Node_Id
) is
266 procedure Check_Redundant_Withs
267 (Context_Items
: List_Id
;
268 Spec_Context_Items
: List_Id
:= No_List
);
269 -- Determine whether the context list of a compilation unit contains
270 -- redundant with clauses. When checking body clauses against spec
271 -- clauses, set Context_Items to the context list of the body and
272 -- Spec_Context_Items to that of the spec. Parent packages are not
273 -- examined for documentation purposes.
275 ---------------------------
276 -- Check_Redundant_Withs --
277 ---------------------------
279 procedure Check_Redundant_Withs
280 (Context_Items
: List_Id
;
281 Spec_Context_Items
: List_Id
:= No_List
)
285 procedure Process_Body_Clauses
286 (Context_List
: List_Id
;
289 Used_Type_Or_Elab
: out Boolean);
290 -- Examine the context clauses of a package body, trying to match the
291 -- name entity of Clause with any list element. If the match occurs
292 -- on a use package clause set Used to True, for a use type clause or
293 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
295 procedure Process_Spec_Clauses
296 (Context_List
: List_Id
;
299 Withed
: out Boolean;
300 Exit_On_Self
: Boolean := False);
301 -- Examine the context clauses of a package spec, trying to match
302 -- the name entity of Clause with any list element. If the match
303 -- occurs on a use package clause, set Used to True, for a with
304 -- package clause other than Clause, set Withed to True. Limited
305 -- with clauses, implicitly generated with clauses and withs
306 -- having pragmas Elaborate or Elaborate_All applied to them are
307 -- skipped. Exit_On_Self is used to control the search loop and
308 -- force an exit whenever Clause sees itself in the search.
310 --------------------------
311 -- Process_Body_Clauses --
312 --------------------------
314 procedure Process_Body_Clauses
315 (Context_List
: List_Id
;
318 Used_Type_Or_Elab
: out Boolean)
320 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
326 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean;
327 -- In an expanded name in a use clause, if the prefix is a renamed
328 -- package, the entity is set to the original package as a result,
329 -- when checking whether the package appears in a previous with
330 -- clause, the renaming has to be taken into account, to prevent
331 -- spurious/incorrect warnings. A common case is use of Text_IO.
337 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean is
339 return Entity
(N
) = P
340 or else (Present
(Renamed_Object
(P
))
341 and then Entity
(N
) = Renamed_Object
(P
));
344 -- Start of processing for Process_Body_Clauses
348 Used_Type_Or_Elab
:= False;
350 Cont_Item
:= First
(Context_List
);
351 while Present
(Cont_Item
) loop
353 -- Package use clause
355 if Nkind
(Cont_Item
) = N_Use_Package_Clause
358 -- Search through use clauses
360 Use_Item
:= Name
(Cont_Item
);
362 -- Case of a direct use of the one we are looking for
364 if Entity
(Use_Item
) = Nam_Ent
then
367 -- Handle nested case, as in "with P; use P.Q.R"
374 -- Loop through prefixes looking for match
377 while Nkind
(UE
) = N_Expanded_Name
loop
378 if Same_Unit
(Prefix
(UE
), Nam_Ent
) then
390 elsif Nkind
(Cont_Item
) = N_Use_Type_Clause
391 and then not Used_Type_Or_Elab
393 Subt_Mark
:= Subtype_Mark
(Cont_Item
);
394 if not Used_Type_Or_Elab
395 and then Same_Unit
(Prefix
(Subt_Mark
), Nam_Ent
)
397 Used_Type_Or_Elab
:= True;
400 -- Pragma Elaborate or Elaborate_All
402 elsif Nkind
(Cont_Item
) = N_Pragma
404 Nam_In
(Pragma_Name_Unmapped
(Cont_Item
),
405 Name_Elaborate
, Name_Elaborate_All
)
406 and then not Used_Type_Or_Elab
409 First
(Pragma_Argument_Associations
(Cont_Item
));
410 while Present
(Prag_Unit
) and then not Used_Type_Or_Elab
loop
411 if Entity
(Expression
(Prag_Unit
)) = Nam_Ent
then
412 Used_Type_Or_Elab
:= True;
421 end Process_Body_Clauses
;
423 --------------------------
424 -- Process_Spec_Clauses --
425 --------------------------
427 procedure Process_Spec_Clauses
428 (Context_List
: List_Id
;
431 Withed
: out Boolean;
432 Exit_On_Self
: Boolean := False)
434 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
441 Cont_Item
:= First
(Context_List
);
442 while Present
(Cont_Item
) loop
444 -- Stop the search since the context items after Cont_Item have
445 -- already been examined in a previous iteration of the reverse
446 -- loop in Check_Redundant_Withs.
449 and Cont_Item
= Clause
454 -- Package use clause
456 if Nkind
(Cont_Item
) = N_Use_Package_Clause
459 if Entity
(Name
(Cont_Item
)) = Nam_Ent
then
463 -- Package with clause. Avoid processing self, implicitly
464 -- generated with clauses or limited with clauses. Note that
465 -- we examine with clauses having pragmas Elaborate or
466 -- Elaborate_All applied to them due to cases such as:
470 -- pragma Elaborate (Pack);
472 -- In this case, the second with clause is redundant since
473 -- the pragma applies only to the first "with Pack;".
475 -- Note that we only consider with_clauses that comes from
476 -- source. In the case of renamings used as prefixes of names
477 -- in with_clauses, we generate a with_clause for the prefix,
478 -- which we do not treat as implicit because it is needed for
479 -- visibility analysis, but is also not redundant.
481 elsif Nkind
(Cont_Item
) = N_With_Clause
482 and then Comes_From_Source
(Cont_Item
)
483 and then not Implicit_With
(Cont_Item
)
484 and then not Limited_Present
(Cont_Item
)
485 and then Cont_Item
/= Clause
486 and then Entity
(Name
(Cont_Item
)) = Nam_Ent
493 end Process_Spec_Clauses
;
495 -- Start of processing for Check_Redundant_Withs
498 Clause
:= Last
(Context_Items
);
499 while Present
(Clause
) loop
501 -- Avoid checking implicitly generated with clauses, limited with
502 -- clauses or withs that have pragma Elaborate or Elaborate_All.
504 if Nkind
(Clause
) = N_With_Clause
505 and then not Implicit_With
(Clause
)
506 and then not Limited_Present
(Clause
)
507 and then not Elaborate_Present
(Clause
)
509 -- With_clauses introduced for renamings of parent clauses
510 -- are not marked implicit because they need to be properly
511 -- installed, but they do not come from source and do not
514 and then Comes_From_Source
(Clause
)
516 -- Package body-to-spec check
518 if Present
(Spec_Context_Items
) then
520 Used_In_Body
: Boolean;
521 Used_In_Spec
: Boolean;
522 Used_Type_Or_Elab
: Boolean;
523 Withed_In_Spec
: Boolean;
527 (Context_List
=> Spec_Context_Items
,
529 Used
=> Used_In_Spec
,
530 Withed
=> Withed_In_Spec
);
533 (Context_List
=> Context_Items
,
535 Used
=> Used_In_Body
,
536 Used_Type_Or_Elab
=> Used_Type_Or_Elab
);
538 -- "Type Elab" refers to the presence of either a use
539 -- type clause, pragmas Elaborate or Elaborate_All.
541 -- +---------------+---------------------------+------+
542 -- | Spec | Body | Warn |
543 -- +--------+------+--------+------+-----------+------+
544 -- | Withed | Used | Withed | Used | Type Elab | |
545 -- | X | | X | | | X |
546 -- | X | | X | X | | |
547 -- | X | | X | | X | |
548 -- | X | | X | X | X | |
549 -- | X | X | X | | | X |
550 -- | X | X | X | | X | |
551 -- | X | X | X | X | | X |
552 -- | X | X | X | X | X | |
553 -- +--------+------+--------+------+-----------+------+
556 and then not Used_Type_Or_Elab
)
558 ((not Used_In_Spec
and then not Used_In_Body
)
559 or else Used_In_Spec
)
561 Error_Msg_N
-- CODEFIX
562 ("redundant with clause in body?r?", Clause
);
565 Used_In_Body
:= False;
566 Used_In_Spec
:= False;
567 Used_Type_Or_Elab
:= False;
568 Withed_In_Spec
:= False;
571 -- Standalone package spec or body check
575 Dummy
: Boolean := False;
576 Withed
: Boolean := False;
579 -- The mechanism for examining the context clauses of a
580 -- package spec can be applied to package body clauses.
583 (Context_List
=> Context_Items
,
587 Exit_On_Self
=> True);
590 Error_Msg_N
-- CODEFIX
591 ("redundant with clause?r?", Clause
);
599 end Check_Redundant_Withs
;
603 Main_Cunit
: constant Node_Id
:= Cunit
(Main_Unit
);
604 Unit_Node
: constant Node_Id
:= Unit
(N
);
605 Lib_Unit
: Node_Id
:= Library_Unit
(N
);
606 Par_Spec_Name
: Unit_Name_Type
;
608 Unum
: Unit_Number_Type
;
610 -- Start of processing for Analyze_Compilation_Unit
613 Process_Compilation_Unit_Pragmas
(N
);
615 -- If the unit is a subunit whose parent has not been analyzed (which
616 -- indicates that the main unit is a subunit, either the current one or
617 -- one of its descendants) then the subunit is compiled as part of the
618 -- analysis of the parent, which we proceed to do. Basically this gets
619 -- handled from the top down and we don't want to do anything at this
620 -- level (i.e. this subunit will be handled on the way down from the
621 -- parent), so at this level we immediately return. If the subunit ends
622 -- up not analyzed, it means that the parent did not contain a stub for
623 -- it, or that there errors were detected in some ancestor.
625 if Nkind
(Unit_Node
) = N_Subunit
and then not Analyzed
(Lib_Unit
) then
626 Semantics
(Lib_Unit
);
628 if not Analyzed
(Proper_Body
(Unit_Node
)) then
629 if Serious_Errors_Detected
> 0 then
630 Error_Msg_N
("subunit not analyzed (errors in parent unit)", N
);
632 Error_Msg_N
("missing stub for subunit", N
);
639 -- Analyze context (this will call Sem recursively for with'ed units) To
640 -- detect circularities among with-clauses that are not caught during
641 -- loading, we set the Context_Pending flag on the current unit. If the
642 -- flag is already set there is a potential circularity. We exclude
643 -- predefined units from this check because they are known to be safe.
644 -- We also exclude package bodies that are present because circularities
645 -- between bodies are harmless (and necessary).
647 if Context_Pending
(N
) then
649 Circularity
: Boolean := True;
652 if In_Predefined_Unit
(N
) then
653 Circularity
:= False;
656 for U
in Main_Unit
+ 1 .. Last_Unit
loop
657 if Nkind
(Unit
(Cunit
(U
))) = N_Package_Body
658 and then not Analyzed
(Cunit
(U
))
660 Circularity
:= False;
667 Error_Msg_N
("circular dependency caused by with_clauses", N
);
669 ("\possibly missing limited_with clause"
670 & " in one of the following", N
);
672 for U
in Main_Unit
.. Last_Unit
loop
673 if Context_Pending
(Cunit
(U
)) then
674 Error_Msg_Unit_1
:= Get_Unit_Name
(Unit
(Cunit
(U
)));
675 Error_Msg_N
("\unit$", N
);
679 raise Unrecoverable_Error
;
683 Set_Context_Pending
(N
);
688 Set_Context_Pending
(N
, False);
690 -- If the unit is a package body, the spec is already loaded and must be
691 -- analyzed first, before we analyze the body.
693 if Nkind
(Unit_Node
) = N_Package_Body
then
695 -- If no Lib_Unit, then there was a serious previous error, so just
696 -- ignore the entire analysis effort.
698 if No
(Lib_Unit
) then
699 Check_Error_Detected
;
703 -- Analyze the package spec
705 Semantics
(Lib_Unit
);
707 -- Check for unused with's
709 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
711 -- Verify that the library unit is a package declaration
713 if not Nkind_In
(Unit
(Lib_Unit
), N_Package_Declaration
,
714 N_Generic_Package_Declaration
)
717 ("no legal package declaration for package body", N
);
720 -- Otherwise, the entity in the declaration is visible. Update the
721 -- version to reflect dependence of this body on the spec.
724 Spec_Id
:= Defining_Entity
(Unit
(Lib_Unit
));
725 Set_Is_Immediately_Visible
(Spec_Id
, True);
726 Version_Update
(N
, Lib_Unit
);
728 if Nkind
(Defining_Unit_Name
(Unit_Node
)) =
729 N_Defining_Program_Unit_Name
731 Generate_Parent_References
(Unit_Node
, Scope
(Spec_Id
));
736 -- If the unit is a subprogram body, then we similarly need to analyze
737 -- its spec. However, things are a little simpler in this case, because
738 -- here, this analysis is done mostly for error checking and consistency
739 -- purposes (but not only, e.g. there could be a contract on the spec),
740 -- so there's nothing else to be done.
742 elsif Nkind
(Unit_Node
) = N_Subprogram_Body
then
743 if Acts_As_Spec
(N
) then
745 -- If the subprogram body is a child unit, we must create a
746 -- declaration for it, in order to properly load the parent(s).
747 -- After this, the original unit does not acts as a spec, because
748 -- there is an explicit one. If this unit appears in a context
749 -- clause, then an implicit with on the parent will be added when
750 -- installing the context. If this is the main unit, there is no
751 -- Unit_Table entry for the declaration (it has the unit number
752 -- of the main unit) and code generation is unaffected.
754 Unum
:= Get_Cunit_Unit_Number
(N
);
755 Par_Spec_Name
:= Get_Parent_Spec_Name
(Unit_Name
(Unum
));
757 if Par_Spec_Name
/= No_Unit_Name
then
760 (Load_Name
=> Par_Spec_Name
,
765 if Unum
/= No_Unit
then
767 -- Build subprogram declaration and attach parent unit to it
768 -- This subprogram declaration does not come from source,
769 -- Nevertheless the backend must generate debugging info for
770 -- it, and this must be indicated explicitly. We also mark
771 -- the body entity as a child unit now, to prevent a
772 -- cascaded error if the spec entity cannot be entered
773 -- in its scope. Finally we create a Units table entry for
774 -- the subprogram declaration, to maintain a one-to-one
775 -- correspondence with compilation unit nodes. This is
776 -- critical for the tree traversals performed by CodePeer.
779 Loc
: constant Source_Ptr
:= Sloc
(N
);
780 SCS
: constant Boolean :=
781 Get_Comes_From_Source_Default
;
784 Set_Comes_From_Source_Default
(False);
786 -- Note: We copy the Context_Items from the explicit body
787 -- to the implicit spec, setting the former to Empty_List
788 -- to preserve the treeish nature of the tree, during
789 -- analysis of the spec. Then we put it back the way it
790 -- was -- copy the Context_Items from the spec to the
791 -- body, and set the spec Context_Items to Empty_List.
792 -- It is necessary to preserve the treeish nature,
793 -- because otherwise we will call End_Use_* twice on the
797 Make_Compilation_Unit
(Loc
,
798 Context_Items
=> Context_Items
(N
),
800 Make_Subprogram_Declaration
(Sloc
(N
),
803 (Specification
(Unit_Node
))),
805 Make_Compilation_Unit_Aux
(Loc
));
807 Set_Context_Items
(N
, Empty_List
);
808 Set_Library_Unit
(N
, Lib_Unit
);
809 Set_Parent_Spec
(Unit
(Lib_Unit
), Cunit
(Unum
));
810 Make_Child_Decl_Unit
(N
);
811 Semantics
(Lib_Unit
);
813 -- Now that a separate declaration exists, the body
814 -- of the child unit does not act as spec any longer.
816 Set_Acts_As_Spec
(N
, False);
817 Set_Is_Child_Unit
(Defining_Entity
(Unit_Node
));
818 Set_Debug_Info_Needed
(Defining_Entity
(Unit
(Lib_Unit
)));
819 Set_Comes_From_Source_Default
(SCS
);
821 -- Restore Context_Items to the body
823 Set_Context_Items
(N
, Context_Items
(Lib_Unit
));
824 Set_Context_Items
(Lib_Unit
, Empty_List
);
829 -- Here for subprogram with separate declaration
832 Semantics
(Lib_Unit
);
833 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
834 Version_Update
(N
, Lib_Unit
);
837 -- If this is a child unit, generate references to the parents
839 if Nkind
(Defining_Unit_Name
(Specification
(Unit_Node
))) =
840 N_Defining_Program_Unit_Name
842 Generate_Parent_References
843 (Specification
(Unit_Node
),
844 Scope
(Defining_Entity
(Unit
(Lib_Unit
))));
848 -- If it is a child unit, the parent must be elaborated first and we
849 -- update version, since we are dependent on our parent.
851 if Is_Child_Spec
(Unit_Node
) then
853 -- The analysis of the parent is done with style checks off
856 Save_Style_Check
: constant Boolean := Style_Check
;
859 if not GNAT_Mode
then
860 Style_Check
:= False;
863 Semantics
(Parent_Spec
(Unit_Node
));
864 Version_Update
(N
, Parent_Spec
(Unit_Node
));
866 -- Restore style check settings
868 Style_Check
:= Save_Style_Check
;
872 -- With the analysis done, install the context. Note that we can't
873 -- install the context from the with clauses as we analyze them, because
874 -- each with clause must be analyzed in a clean visibility context, so
875 -- we have to wait and install them all at once.
879 if Is_Child_Spec
(Unit_Node
) then
881 -- Set the entities of all parents in the program_unit_name
883 Generate_Parent_References
884 (Unit_Node
, Get_Parent_Entity
(Unit
(Parent_Spec
(Unit_Node
))));
887 -- All components of the context: with-clauses, library unit, ancestors
888 -- if any, (and their context) are analyzed and installed.
890 -- Call special debug routine sm if this is the main unit
892 if Current_Sem_Unit
= Main_Unit
then
896 -- Now analyze the unit (package, subprogram spec, body) itself
900 if Warn_On_Redundant_Constructs
then
901 Check_Redundant_Withs
(Context_Items
(N
));
903 if Nkind
(Unit_Node
) = N_Package_Body
then
904 Check_Redundant_Withs
905 (Context_Items
=> Context_Items
(N
),
906 Spec_Context_Items
=> Context_Items
(Lib_Unit
));
910 -- The above call might have made Unit_Node an N_Subprogram_Body from
911 -- something else, so propagate any Acts_As_Spec flag.
913 if Nkind
(Unit_Node
) = N_Subprogram_Body
914 and then Acts_As_Spec
(Unit_Node
)
916 Set_Acts_As_Spec
(N
);
919 -- Register predefined units in Rtsfind
921 if In_Predefined_Unit
(N
) then
922 Set_RTU_Loaded
(Unit_Node
);
925 -- Treat compilation unit pragmas that appear after the library unit
927 if Present
(Pragmas_After
(Aux_Decls_Node
(N
))) then
929 Prag_Node
: Node_Id
:= First
(Pragmas_After
(Aux_Decls_Node
(N
)));
931 while Present
(Prag_Node
) loop
938 -- Analyze the contract of a [generic] subprogram that acts as a
939 -- compilation unit after all compilation pragmas have been analyzed.
941 if Nkind_In
(Unit_Node
, N_Generic_Subprogram_Declaration
,
942 N_Subprogram_Declaration
)
944 Analyze_Entry_Or_Subprogram_Contract
(Defining_Entity
(Unit_Node
));
947 -- Generate distribution stubs if requested and no error
950 and then (Distribution_Stub_Mode
= Generate_Receiver_Stub_Body
952 Distribution_Stub_Mode
= Generate_Caller_Stub_Body
)
953 and then Fatal_Error
(Main_Unit
) /= Error_Detected
955 if Is_RCI_Pkg_Spec_Or_Body
(N
) then
957 -- Regular RCI package
959 Add_Stub_Constructs
(N
);
961 elsif (Nkind
(Unit_Node
) = N_Package_Declaration
962 and then Is_Shared_Passive
(Defining_Entity
963 (Specification
(Unit_Node
))))
964 or else (Nkind
(Unit_Node
) = N_Package_Body
966 Is_Shared_Passive
(Corresponding_Spec
(Unit_Node
)))
968 -- Shared passive package
970 Add_Stub_Constructs
(N
);
972 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
974 Is_Remote_Call_Interface
975 (Defining_Entity
(Specification
(Instance_Spec
(Unit_Node
))))
977 -- Instantiation of a RCI generic package
979 Add_Stub_Constructs
(N
);
983 -- Remove unit from visibility, so that environment is clean for the
984 -- next compilation, which is either the main unit or some other unit
987 if Nkind_In
(Unit_Node
, N_Package_Declaration
,
988 N_Package_Renaming_Declaration
,
989 N_Subprogram_Declaration
)
990 or else Nkind
(Unit_Node
) in N_Generic_Declaration
991 or else (Nkind
(Unit_Node
) = N_Subprogram_Body
992 and then Acts_As_Spec
(Unit_Node
))
994 Remove_Unit_From_Visibility
(Defining_Entity
(Unit_Node
));
996 -- If the unit is an instantiation whose body will be elaborated for
997 -- inlining purposes, use the proper entity of the instance. The entity
998 -- may be missing if the instantiation was illegal.
1000 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
1001 and then not Error_Posted
(Unit_Node
)
1002 and then Present
(Instance_Spec
(Unit_Node
))
1004 Remove_Unit_From_Visibility
1005 (Defining_Entity
(Instance_Spec
(Unit_Node
)));
1007 elsif Nkind
(Unit_Node
) = N_Package_Body
1008 or else (Nkind
(Unit_Node
) = N_Subprogram_Body
1009 and then not Acts_As_Spec
(Unit_Node
))
1011 -- Bodies that are not the main unit are compiled if they are generic
1012 -- or contain generic or inlined units. Their analysis brings in the
1013 -- context of the corresponding spec (unit declaration) which must be
1014 -- removed as well, to return the compilation environment to its
1017 Remove_Context
(Lib_Unit
);
1018 Set_Is_Immediately_Visible
(Defining_Entity
(Unit
(Lib_Unit
)), False);
1021 -- Last step is to deinstall the context we just installed as well as
1022 -- the unit just compiled.
1026 -- When generating code for a non-generic main unit, check that withed
1027 -- generic units have a body if they need it, even if the units have not
1028 -- been instantiated. Force the load of the bodies to produce the proper
1029 -- error if the body is absent. The same applies to GNATprove mode, with
1030 -- the added benefit of capturing global references within the generic.
1031 -- This in turn allows for proper inlining of subprogram bodies without
1032 -- a previous declaration.
1034 if Get_Cunit_Unit_Number
(N
) = Main_Unit
1035 and then ((Operating_Mode
= Generate_Code
and then Expander_Active
)
1037 (Operating_Mode
= Check_Semantics
and then GNATprove_Mode
))
1039 -- Check whether the source for the body of the unit must be included
1040 -- in a standalone library.
1042 Check_Body_Needed_For_SAL
(Cunit_Entity
(Main_Unit
));
1044 -- Indicate that the main unit is now analyzed, to catch possible
1045 -- circularities between it and generic bodies. Remove main unit from
1046 -- visibility. This might seem superfluous, but the main unit must
1047 -- not be visible in the generic body expansions that follow.
1049 Set_Analyzed
(N
, True);
1050 Set_Is_Immediately_Visible
(Cunit_Entity
(Main_Unit
), False);
1055 Un
: Unit_Number_Type
;
1057 Save_Style_Check
: constant Boolean := Style_Check
;
1060 Item
:= First
(Context_Items
(N
));
1061 while Present
(Item
) loop
1063 -- Check for explicit with clause
1065 if Nkind
(Item
) = N_With_Clause
1066 and then not Implicit_With
(Item
)
1068 -- Ada 2005 (AI-50217): Ignore limited-withed units
1070 and then not Limited_Present
(Item
)
1072 Nam
:= Entity
(Name
(Item
));
1074 -- Compile the generic subprogram, unless it is intrinsic or
1075 -- imported so no body is required, or generic package body
1076 -- if the package spec requires a body.
1078 if (Is_Generic_Subprogram
(Nam
)
1079 and then not Is_Intrinsic_Subprogram
(Nam
)
1080 and then not Is_Imported
(Nam
))
1081 or else (Ekind
(Nam
) = E_Generic_Package
1082 and then Unit_Requires_Body
(Nam
))
1084 Style_Check
:= False;
1086 if Present
(Renamed_Object
(Nam
)) then
1092 (Unit_Declaration_Node
1093 (Renamed_Object
(Nam
)))),
1102 Get_Body_Name
(Get_Unit_Name
(Item
)),
1109 if Un
= No_Unit
then
1111 ("body of generic unit& not found", Item
, Nam
);
1114 elsif not Analyzed
(Cunit
(Un
))
1115 and then Un
/= Main_Unit
1116 and then Fatal_Error
(Un
) /= Error_Detected
1118 Style_Check
:= False;
1119 Semantics
(Cunit
(Un
));
1127 -- Restore style checks settings
1129 Style_Check
:= Save_Style_Check
;
1132 -- In GNATprove mode, force the loading of an Interrupt_Priority when
1133 -- processing compilation units with potentially "main" subprograms.
1134 -- This is required for the ceiling priority protocol checks, which
1135 -- are triggered by these subprograms.
1138 and then Nkind_In
(Unit_Node
, N_Function_Instantiation
,
1139 N_Procedure_Instantiation
,
1146 case Nkind
(Unit_Node
) is
1147 when N_Subprogram_Body
=>
1148 Spec
:= Specification
(Unit_Node
);
1150 when N_Subprogram_Instantiation
=>
1152 Subprogram_Specification
(Entity
(Name
(Unit_Node
)));
1155 raise Program_Error
;
1158 pragma Assert
(Nkind
(Spec
) in N_Subprogram_Specification
);
1160 -- Main subprogram must have no parameters, and if it is a
1161 -- function, it must return an integer.
1163 if No
(Parameter_Specifications
(Spec
))
1164 and then (Nkind
(Spec
) = N_Procedure_Specification
1166 Is_Integer_Type
(Etype
(Result_Definition
(Spec
))))
1168 SPARK_Implicit_Load
(RE_Interrupt_Priority
);
1174 -- Deal with creating elaboration counter if needed. We create an
1175 -- elaboration counter only for units that come from source since
1176 -- units manufactured by the compiler never need elab checks.
1178 if Comes_From_Source
(N
)
1179 and then Nkind_In
(Unit_Node
, N_Package_Declaration
,
1180 N_Generic_Package_Declaration
,
1181 N_Subprogram_Declaration
,
1182 N_Generic_Subprogram_Declaration
)
1185 Loc
: constant Source_Ptr
:= Sloc
(N
);
1186 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(Loc
);
1189 Spec_Id
:= Defining_Entity
(Unit_Node
);
1190 Generate_Definition
(Spec_Id
);
1192 -- See if an elaboration entity is required for possible access
1193 -- before elaboration checking. Note that we must allow for this
1194 -- even if -gnatE is not set, since a client may be compiled in
1195 -- -gnatE mode and reference the entity.
1197 -- These entities are also used by the binder to prevent multiple
1198 -- attempts to execute the elaboration code for the library case
1199 -- where the elaboration routine might otherwise be called more
1202 -- They are also needed to ensure explicit visibility from the
1203 -- binder generated code of all the units involved in a partition
1204 -- when control-flow preservation is requested.
1206 -- Case of units which do not require an elaboration entity
1208 if not Opt
.Suppress_Control_Flow_Optimizations
1210 ( -- Pure units do not need checks
1214 -- Preelaborated units do not need checks
1216 or else Is_Preelaborated
(Spec_Id
)
1218 -- No checks needed if pragma Elaborate_Body present
1220 or else Has_Pragma_Elaborate_Body
(Spec_Id
)
1222 -- No checks needed if unit does not require a body
1224 or else not Unit_Requires_Body
(Spec_Id
)
1226 -- No checks needed for predefined files
1228 or else Is_Predefined_Unit
(Unum
)
1230 -- No checks required if no separate spec
1232 or else Acts_As_Spec
(N
)
1235 -- This is a case where we only need the entity for
1236 -- checking to prevent multiple elaboration checks.
1238 Set_Elaboration_Entity_Required
(Spec_Id
, False);
1240 -- Case of elaboration entity is required for access before
1241 -- elaboration checking (so certainly we must build it).
1244 Set_Elaboration_Entity_Required
(Spec_Id
, True);
1247 Build_Elaboration_Entity
(N
, Spec_Id
);
1251 -- Freeze the compilation unit entity. This for sure is needed because
1252 -- of some warnings that can be output (see Freeze_Subprogram), but may
1253 -- in general be required. If freezing actions result, place them in the
1254 -- compilation unit actions list, and analyze them.
1257 L
: constant List_Id
:=
1258 Freeze_Entity
(Cunit_Entity
(Current_Sem_Unit
), N
);
1260 while Is_Non_Empty_List
(L
) loop
1261 Insert_Library_Level_Action
(Remove_Head
(L
));
1267 -- Call Check_Package_Body so that a body containing subprograms with
1268 -- Inline_Always can be made available for front end inlining.
1270 if Nkind
(Unit_Node
) = N_Package_Declaration
1271 and then Get_Cunit_Unit_Number
(N
) /= Main_Unit
1273 -- We don't need to do this if the Expander is not active, since there
1274 -- is no code to inline.
1276 and then Expander_Active
1279 Save_Style_Check
: constant Boolean := Style_Check
;
1280 Save_Warning
: constant Warning_Mode_Type
:= Warning_Mode
;
1281 Options
: Style_Check_Options
;
1284 Save_Style_Check_Options
(Options
);
1285 Reset_Style_Check_Options
;
1286 Opt
.Warning_Mode
:= Suppress
;
1288 Check_Package_Body_For_Inlining
(N
, Defining_Entity
(Unit_Node
));
1290 Reset_Style_Check_Options
;
1291 Set_Style_Check_Options
(Options
);
1292 Style_Check
:= Save_Style_Check
;
1293 Warning_Mode
:= Save_Warning
;
1297 -- If we are generating obsolescent warnings, then here is where we
1298 -- generate them for the with'ed items. The reason for this special
1299 -- processing is that the normal mechanism of generating the warnings
1300 -- for referenced entities does not work for context clause references.
1301 -- That's because when we first analyze the context, it is too early to
1302 -- know if the with'ing unit is itself obsolescent (which suppresses
1306 and then Warn_On_Obsolescent_Feature
1307 and then Nkind
(Unit_Node
) not in N_Generic_Instantiation
1309 -- Push current compilation unit as scope, so that the test for
1310 -- being within an obsolescent unit will work correctly. The check
1311 -- is not performed within an instantiation, because the warning
1312 -- will have been emitted in the corresponding generic unit.
1314 Push_Scope
(Defining_Entity
(Unit_Node
));
1316 -- Loop through context items to deal with with clauses
1324 Item
:= First
(Context_Items
(N
));
1325 while Present
(Item
) loop
1326 if Nkind
(Item
) = N_With_Clause
1328 -- Suppress this check in limited-withed units. Further work
1329 -- needed here if we decide to incorporate this check on
1330 -- limited-withed units.
1332 and then not Limited_Present
(Item
)
1335 Ent
:= Entity
(Nam
);
1337 if Is_Obsolescent
(Ent
) then
1338 Output_Obsolescent_Entity_Warnings
(Nam
, Ent
);
1346 -- Remove temporary install of current unit as scope
1351 -- If No_Elaboration_Code_All was encountered, this is where we do the
1352 -- transitive test of with'ed units to make sure they have the aspect.
1353 -- This is delayed till the end of analyzing the compilation unit to
1354 -- ensure that the pragma/aspect, if present, has been analyzed.
1356 Check_No_Elab_Code_All
(N
);
1357 end Analyze_Compilation_Unit
;
1359 ---------------------
1360 -- Analyze_Context --
1361 ---------------------
1363 procedure Analyze_Context
(N
: Node_Id
) is
1364 Ukind
: constant Node_Kind
:= Nkind
(Unit
(N
));
1368 -- First process all configuration pragmas at the start of the context
1369 -- items. Strictly these are not part of the context clause, but that
1370 -- is where the parser puts them. In any case for sure we must analyze
1371 -- these before analyzing the actual context items, since they can have
1372 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1373 -- be with'ed as a result of changing categorizations in Ada 2005).
1375 Item
:= First
(Context_Items
(N
));
1376 while Present
(Item
)
1377 and then Nkind
(Item
) = N_Pragma
1378 and then Pragma_Name
(Item
) in Configuration_Pragma_Names
1384 -- This is the point at which we capture the configuration settings
1385 -- for the unit. At the moment only the Optimize_Alignment setting
1386 -- needs to be captured. Probably more later ???
1388 if Optimize_Alignment_Local
then
1389 Set_OA_Setting
(Current_Sem_Unit
, 'L');
1391 Set_OA_Setting
(Current_Sem_Unit
, Optimize_Alignment
);
1394 -- Loop through actual context items. This is done in two passes:
1396 -- a) The first pass analyzes nonlimited with clauses and also any
1397 -- configuration pragmas (we need to get the latter analyzed right
1398 -- away, since they can affect processing of subsequent items).
1400 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1402 while Present
(Item
) loop
1404 -- For with clause, analyze the with clause, and then update the
1405 -- version, since we are dependent on a unit that we with.
1407 if Nkind
(Item
) = N_With_Clause
1408 and then not Limited_Present
(Item
)
1410 -- Skip analyzing with clause if no unit, nothing to do (this
1411 -- happens for a with that references a non-existent unit).
1413 if Present
(Library_Unit
(Item
)) then
1415 -- Skip analyzing with clause if this is a with_clause for
1416 -- the main unit, which happens if a subunit has a useless
1417 -- with_clause on its parent.
1419 if Library_Unit
(Item
) /= Cunit
(Current_Sem_Unit
) then
1422 -- Here for the case of a useless with for the main unit
1425 Set_Entity
(Name
(Item
), Cunit_Entity
(Current_Sem_Unit
));
1429 -- Do version update (skipped for implicit with)
1431 if not Implicit_With
(Item
) then
1432 Version_Update
(N
, Library_Unit
(Item
));
1435 -- Skip pragmas. Configuration pragmas at the start were handled in
1436 -- the loop above, and remaining pragmas are not processed until we
1437 -- actually install the context (see Install_Context). We delay the
1438 -- analysis of these pragmas to make sure that we have installed all
1439 -- the implicit with's on parent units.
1441 -- Skip use clauses at this stage, since we don't want to do any
1442 -- installing of potentially use-visible entities until we
1443 -- actually install the complete context (in Install_Context).
1444 -- Otherwise things can get installed in the wrong context.
1453 -- Second pass: examine all limited_with clauses. All other context
1454 -- items are ignored in this pass.
1456 Item
:= First
(Context_Items
(N
));
1457 while Present
(Item
) loop
1458 if Nkind
(Item
) = N_With_Clause
1459 and then Limited_Present
(Item
)
1461 -- No need to check errors on implicitly generated limited-with
1464 if not Implicit_With
(Item
) then
1466 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1467 -- properly rejected, including renaming declarations.
1469 if not Nkind_In
(Ukind
, N_Package_Declaration
,
1470 N_Subprogram_Declaration
)
1471 and then Ukind
not in N_Generic_Declaration
1472 and then Ukind
not in N_Generic_Instantiation
1474 Error_Msg_N
("limited with_clause not allowed here", Item
);
1476 -- Check wrong use of a limited with clause applied to the
1477 -- compilation unit containing the limited-with clause.
1479 -- limited with P.Q;
1480 -- package P.Q is ...
1482 elsif Unit
(Library_Unit
(Item
)) = Unit
(N
) then
1483 Error_Msg_N
("wrong use of limited-with clause", Item
);
1485 -- Check wrong use of limited-with clause applied to some
1486 -- immediate ancestor.
1488 elsif Is_Child_Spec
(Unit
(N
)) then
1490 Lib_U
: constant Entity_Id
:= Unit
(Library_Unit
(Item
));
1494 P
:= Parent_Spec
(Unit
(N
));
1496 if Unit
(P
) = Lib_U
then
1498 ("limited with_clause cannot name ancestor",
1503 exit when not Is_Child_Spec
(Unit
(P
));
1504 P
:= Parent_Spec
(Unit
(P
));
1509 -- Check if the limited-withed unit is already visible through
1510 -- some context clause of the current compilation unit or some
1511 -- ancestor of the current compilation unit.
1514 Lim_Unit_Name
: constant Node_Id
:= Name
(Item
);
1515 Comp_Unit
: Node_Id
;
1517 Unit_Name
: Node_Id
;
1522 It
:= First
(Context_Items
(Comp_Unit
));
1523 while Present
(It
) loop
1525 and then Nkind
(It
) = N_With_Clause
1526 and then not Limited_Present
(It
)
1528 Nkind_In
(Unit
(Library_Unit
(It
)),
1529 N_Package_Declaration
,
1530 N_Package_Renaming_Declaration
)
1532 if Nkind
(Unit
(Library_Unit
(It
))) =
1533 N_Package_Declaration
1535 Unit_Name
:= Name
(It
);
1537 Unit_Name
:= Name
(Unit
(Library_Unit
(It
)));
1540 -- Check if the named package (or some ancestor)
1541 -- leaves visible the full-view of the unit given
1542 -- in the limited-with clause.
1545 if Designate_Same_Unit
(Lim_Unit_Name
,
1548 Error_Msg_Sloc
:= Sloc
(It
);
1550 ("simultaneous visibility of limited and "
1551 & "unlimited views not allowed", Item
);
1553 ("\unlimited view visible through context "
1554 & "clause #", Item
, It
);
1557 elsif Nkind
(Unit_Name
) = N_Identifier
then
1561 Unit_Name
:= Prefix
(Unit_Name
);
1568 exit when not Is_Child_Spec
(Unit
(Comp_Unit
));
1570 Comp_Unit
:= Parent_Spec
(Unit
(Comp_Unit
));
1575 -- Skip analyzing with clause if no unit, see above
1577 if Present
(Library_Unit
(Item
)) then
1581 -- A limited_with does not impose an elaboration order, but there
1582 -- is a semantic dependency for recompilation purposes.
1584 if not Implicit_With
(Item
) then
1585 Version_Update
(N
, Library_Unit
(Item
));
1588 -- Pragmas and use clauses and with clauses other than limited with's
1589 -- are ignored in this pass through the context items.
1597 end Analyze_Context
;
1599 -------------------------------
1600 -- Analyze_Package_Body_Stub --
1601 -------------------------------
1603 procedure Analyze_Package_Body_Stub
(N
: Node_Id
) is
1604 Id
: constant Entity_Id
:= Defining_Entity
(N
);
1606 Opts
: Config_Switches_Type
;
1609 -- The package declaration must be in the current declarative part
1611 Check_Stub_Level
(N
);
1612 Nam
:= Current_Entity_In_Scope
(Id
);
1614 if No
(Nam
) or else not Is_Package_Or_Generic_Package
(Nam
) then
1615 Error_Msg_N
("missing specification for package stub", N
);
1617 elsif Has_Completion
(Nam
)
1618 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(Nam
)))
1620 Error_Msg_N
("duplicate or redundant stub for package", N
);
1623 -- Retain and restore the configuration options of the enclosing
1624 -- context as the proper body may introduce a set of its own.
1626 Opts
:= Save_Config_Switches
;
1628 -- Indicate that the body of the package exists. If we are doing
1629 -- only semantic analysis, the stub stands for the body. If we are
1630 -- generating code, the existence of the body will be confirmed
1631 -- when we load the proper body.
1633 Set_Scope
(Id
, Current_Scope
);
1634 Set_Ekind
(Id
, E_Package_Body
);
1635 Set_Etype
(Id
, Standard_Void_Type
);
1637 if Has_Aspects
(N
) then
1638 Analyze_Aspect_Specifications
(N
, Id
);
1641 Set_Has_Completion
(Nam
);
1642 Set_Corresponding_Spec_Of_Stub
(N
, Nam
);
1643 Generate_Reference
(Nam
, Id
, 'b');
1644 Analyze_Proper_Body
(N
, Nam
);
1646 Restore_Config_Switches
(Opts
);
1648 end Analyze_Package_Body_Stub
;
1650 -------------------------
1651 -- Analyze_Proper_Body --
1652 -------------------------
1654 procedure Analyze_Proper_Body
(N
: Node_Id
; Nam
: Entity_Id
) is
1655 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
1657 procedure Optional_Subunit
;
1658 -- This procedure is called when the main unit is a stub, or when we
1659 -- are not generating code. In such a case, we analyze the subunit if
1660 -- present, which is user-friendly and in fact required for ASIS, but we
1661 -- don't complain if the subunit is missing. In GNATprove_Mode, we issue
1662 -- an error to avoid formal verification of a partial unit.
1664 ----------------------
1665 -- Optional_Subunit --
1666 ----------------------
1668 procedure Optional_Subunit
is
1669 Comp_Unit
: Node_Id
;
1670 Unum
: Unit_Number_Type
;
1673 -- Try to load subunit, but ignore any errors that occur during the
1674 -- loading of the subunit, by using the special feature in Errout to
1675 -- ignore all errors. Note that Fatal_Error will still be set, so we
1676 -- will be able to check for this case below.
1678 if not (ASIS_Mode
or GNATprove_Mode
) then
1679 Ignore_Errors_Enable
:= Ignore_Errors_Enable
+ 1;
1684 (Load_Name
=> Subunit_Name
,
1685 Required
=> GNATprove_Mode
,
1689 if not (ASIS_Mode
or GNATprove_Mode
) then
1690 Ignore_Errors_Enable
:= Ignore_Errors_Enable
- 1;
1693 -- All done if we successfully loaded the subunit
1696 and then (Fatal_Error
(Unum
) /= Error_Detected
1697 or else Try_Semantics
)
1699 Comp_Unit
:= Cunit
(Unum
);
1701 -- If the file was empty or seriously mangled, the unit itself may
1704 if No
(Unit
(Comp_Unit
)) then
1706 ("subunit does not contain expected proper body", N
);
1708 elsif Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
1710 ("expected SEPARATE subunit, found child unit",
1711 Cunit_Entity
(Unum
));
1713 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
1714 Analyze_Subunit
(Comp_Unit
);
1715 Set_Library_Unit
(N
, Comp_Unit
);
1716 Set_Corresponding_Body
(N
, Defining_Entity
(Unit
(Comp_Unit
)));
1719 elsif Unum
= No_Unit
1720 and then Present
(Nam
)
1722 if Is_Protected_Type
(Nam
) then
1723 Set_Corresponding_Body
(Parent
(Nam
), Defining_Identifier
(N
));
1725 Set_Corresponding_Body
(
1726 Unit_Declaration_Node
(Nam
), Defining_Identifier
(N
));
1729 end Optional_Subunit
;
1733 Comp_Unit
: Node_Id
;
1734 Unum
: Unit_Number_Type
;
1736 -- Start of processing for Analyze_Proper_Body
1739 -- If the subunit is already loaded, it means that the main unit is a
1740 -- subunit, and that the current unit is one of its parents which was
1741 -- being analyzed to provide the needed context for the analysis of the
1742 -- subunit. In this case we analyze the subunit and continue with the
1743 -- parent, without looking at subsequent subunits.
1745 if Is_Loaded
(Subunit_Name
) then
1747 -- If the proper body is already linked to the stub node, the stub is
1748 -- in a generic unit and just needs analyzing.
1750 if Present
(Library_Unit
(N
)) then
1751 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
1753 -- If the subunit has severe errors, the spec of the enclosing
1754 -- body may not be available, in which case do not try analysis.
1756 if Serious_Errors_Detected
> 0
1757 and then No
(Library_Unit
(Library_Unit
(N
)))
1762 -- Collect SCO information for loaded subunit if we are in the
1763 -- extended main unit.
1766 and then In_Extended_Main_Source_Unit
1767 (Cunit_Entity
(Current_Sem_Unit
))
1769 SCO_Record_Raw
(Get_Cunit_Unit_Number
(Library_Unit
(N
)));
1772 Analyze_Subunit
(Library_Unit
(N
));
1774 -- Otherwise we must load the subunit and link to it
1777 -- Load the subunit, this must work, since we originally loaded
1778 -- the subunit earlier on. So this will not really load it, just
1779 -- give access to it.
1783 (Load_Name
=> Subunit_Name
,
1788 -- And analyze the subunit in the parent context (note that we
1789 -- do not call Semantics, since that would remove the parent
1790 -- context). Because of this, we have to manually reset the
1791 -- compiler state to Analyzing since it got destroyed by Load.
1793 if Unum
/= No_Unit
then
1794 Compiler_State
:= Analyzing
;
1796 -- Check that the proper body is a subunit and not a child
1797 -- unit. If the unit was previously loaded, the error will
1798 -- have been emitted when copying the generic node, so we
1799 -- just return to avoid cascaded errors.
1801 if Nkind
(Unit
(Cunit
(Unum
))) /= N_Subunit
then
1805 Set_Corresponding_Stub
(Unit
(Cunit
(Unum
)), N
);
1806 Analyze_Subunit
(Cunit
(Unum
));
1807 Set_Library_Unit
(N
, Cunit
(Unum
));
1811 -- If the main unit is a subunit, then we are just performing semantic
1812 -- analysis on that subunit, and any other subunits of any parent unit
1813 -- should be ignored, except that if we are building trees for ASIS
1814 -- usage we want to annotate the stub properly. If the main unit is
1815 -- itself a subunit, another subunit is irrelevant unless it is a
1816 -- subunit of the current one, that is to say appears in the current
1819 elsif Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
1820 and then Subunit_Name
/= Unit_Name
(Main_Unit
)
1824 PB
: constant Node_Id
:= Proper_Body
(Unit
(Cunit
(Main_Unit
)));
1826 if Nkind_In
(PB
, N_Package_Body
, N_Subprogram_Body
)
1827 and then List_Containing
(N
) = Declarations
(PB
)
1834 -- But before we return, set the flag for unloaded subunits. This
1835 -- will suppress junk warnings of variables in the same declarative
1836 -- part (or a higher level one) that are in danger of looking unused
1837 -- when in fact there might be a declaration in the subunit that we
1838 -- do not intend to load.
1840 Unloaded_Subunits
:= True;
1843 -- If the subunit is not already loaded, and we are generating code,
1844 -- then this is the case where compilation started from the parent, and
1845 -- we are generating code for an entire subunit tree. In that case we
1846 -- definitely need to load the subunit.
1848 -- In order to continue the analysis with the rest of the parent,
1849 -- and other subunits, we load the unit without requiring its
1850 -- presence, and emit a warning if not found, rather than terminating
1851 -- the compilation abruptly, as for other missing file problems.
1853 elsif Original_Operating_Mode
= Generate_Code
then
1855 -- If the proper body is already linked to the stub node, the stub is
1856 -- in a generic unit and just needs analyzing.
1858 -- We update the version. Although we are not strictly technically
1859 -- semantically dependent on the subunit, given our approach of macro
1860 -- substitution of subunits, it makes sense to include it in the
1861 -- version identification.
1863 if Present
(Library_Unit
(N
)) then
1864 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
1865 Analyze_Subunit
(Library_Unit
(N
));
1866 Version_Update
(Cunit
(Main_Unit
), Library_Unit
(N
));
1868 -- Otherwise we must load the subunit and link to it
1871 -- Make sure that, if the subunit is preprocessed and -gnateG is
1872 -- specified, the preprocessed file will be written.
1874 Lib
.Analysing_Subunit_Of_Main
:= True;
1877 (Load_Name
=> Subunit_Name
,
1881 Lib
.Analysing_Subunit_Of_Main
:= False;
1883 -- Give message if we did not get the unit Emit warning even if
1884 -- missing subunit is not within main unit, to simplify debugging.
1886 pragma Assert
(Original_Operating_Mode
= Generate_Code
);
1887 if Unum
= No_Unit
then
1888 Error_Msg_Unit_1
:= Subunit_Name
;
1890 Get_File_Name
(Subunit_Name
, Subunit
=> True);
1892 ("subunit$$ in file{ not found??!!", N
);
1893 Subunits_Missing
:= True;
1896 -- Load_Unit may reset Compiler_State, since it may have been
1897 -- necessary to parse an additional units, so we make sure that
1898 -- we reset it to the Analyzing state.
1900 Compiler_State
:= Analyzing
;
1902 if Unum
/= No_Unit
then
1903 if Debug_Flag_L
then
1904 Write_Str
("*** Loaded subunit from stub. Analyze");
1908 Comp_Unit
:= Cunit
(Unum
);
1910 -- Check for child unit instead of subunit
1912 if Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
1914 ("expected SEPARATE subunit, found child unit",
1915 Cunit_Entity
(Unum
));
1917 -- OK, we have a subunit
1920 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
1921 Set_Library_Unit
(N
, Comp_Unit
);
1923 -- We update the version. Although we are not technically
1924 -- semantically dependent on the subunit, given our approach
1925 -- of macro substitution of subunits, it makes sense to
1926 -- include it in the version identification.
1928 Version_Update
(Cunit
(Main_Unit
), Comp_Unit
);
1930 -- Collect SCO information for loaded subunit if we are in
1931 -- the extended main unit.
1934 and then In_Extended_Main_Source_Unit
1935 (Cunit_Entity
(Current_Sem_Unit
))
1937 SCO_Record_Raw
(Unum
);
1940 -- Analyze the unit if semantics active
1942 if Fatal_Error
(Unum
) /= Error_Detected
1943 or else Try_Semantics
1945 Analyze_Subunit
(Comp_Unit
);
1951 -- The remaining case is when the subunit is not already loaded and we
1952 -- are not generating code. In this case we are just performing semantic
1953 -- analysis on the parent, and we are not interested in the subunit. For
1954 -- subprograms, analyze the stub as a body. For other entities the stub
1955 -- has already been marked as completed.
1960 end Analyze_Proper_Body
;
1962 ----------------------------------
1963 -- Analyze_Protected_Body_Stub --
1964 ----------------------------------
1966 procedure Analyze_Protected_Body_Stub
(N
: Node_Id
) is
1967 Id
: constant Entity_Id
:= Defining_Entity
(N
);
1968 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Id
);
1969 Opts
: Config_Switches_Type
;
1972 Check_Stub_Level
(N
);
1974 -- First occurrence of name may have been as an incomplete type
1976 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
1977 Nam
:= Full_View
(Nam
);
1980 if No
(Nam
) or else not Is_Protected_Type
(Etype
(Nam
)) then
1981 Error_Msg_N
("missing specification for Protected body", N
);
1984 -- Retain and restore the configuration options of the enclosing
1985 -- context as the proper body may introduce a set of its own.
1987 Opts
:= Save_Config_Switches
;
1989 Set_Scope
(Id
, Current_Scope
);
1990 Set_Ekind
(Id
, E_Protected_Body
);
1991 Set_Etype
(Id
, Standard_Void_Type
);
1993 if Has_Aspects
(N
) then
1994 Analyze_Aspect_Specifications
(N
, Id
);
1997 Set_Has_Completion
(Etype
(Nam
));
1998 Set_Corresponding_Spec_Of_Stub
(N
, Nam
);
1999 Generate_Reference
(Nam
, Id
, 'b');
2000 Analyze_Proper_Body
(N
, Etype
(Nam
));
2002 Restore_Config_Switches
(Opts
);
2004 end Analyze_Protected_Body_Stub
;
2006 ----------------------------------
2007 -- Analyze_Subprogram_Body_Stub --
2008 ----------------------------------
2010 -- A subprogram body stub can appear with or without a previous spec. If
2011 -- there is one, then the analysis of the body will find it and verify
2012 -- conformance. The formals appearing in the specification of the stub play
2013 -- no role, except for requiring an additional conformance check. If there
2014 -- is no previous subprogram declaration, the stub acts as a spec, and
2015 -- provides the defining entity for the subprogram.
2017 procedure Analyze_Subprogram_Body_Stub
(N
: Node_Id
) is
2019 Opts
: Config_Switches_Type
;
2022 Check_Stub_Level
(N
);
2024 -- Verify that the identifier for the stub is unique within this
2025 -- declarative part.
2027 if Nkind_In
(Parent
(N
), N_Block_Statement
,
2031 Decl
:= First
(Declarations
(Parent
(N
)));
2032 while Present
(Decl
) and then Decl
/= N
loop
2033 if Nkind
(Decl
) = N_Subprogram_Body_Stub
2034 and then (Chars
(Defining_Unit_Name
(Specification
(Decl
))) =
2035 Chars
(Defining_Unit_Name
(Specification
(N
))))
2037 Error_Msg_N
("identifier for stub is not unique", N
);
2044 -- Retain and restore the configuration options of the enclosing context
2045 -- as the proper body may introduce a set of its own.
2047 Opts
:= Save_Config_Switches
;
2049 -- Treat stub as a body, which checks conformance if there is a previous
2050 -- declaration, or else introduces entity and its signature.
2052 Analyze_Subprogram_Body
(N
);
2053 Analyze_Proper_Body
(N
, Empty
);
2055 Restore_Config_Switches
(Opts
);
2056 end Analyze_Subprogram_Body_Stub
;
2058 ---------------------
2059 -- Analyze_Subunit --
2060 ---------------------
2062 -- A subunit is compiled either by itself (for semantic checking) or as
2063 -- part of compiling the parent (for code generation). In either case, by
2064 -- the time we actually process the subunit, the parent has already been
2065 -- installed and analyzed. The node N is a compilation unit, whose context
2066 -- needs to be treated here, because we come directly here from the parent
2067 -- without calling Analyze_Compilation_Unit.
2069 -- The compilation context includes the explicit context of the subunit,
2070 -- and the context of the parent, together with the parent itself. In order
2071 -- to compile the current context, we remove the one inherited from the
2072 -- parent, in order to have a clean visibility table. We restore the parent
2073 -- context before analyzing the proper body itself. On exit, we remove only
2074 -- the explicit context of the subunit.
2076 -- WARNING: This routine manages SPARK regions. Return statements must be
2077 -- replaced by gotos which jump to the end of the routine and restore the
2080 procedure Analyze_Subunit
(N
: Node_Id
) is
2081 Lib_Unit
: constant Node_Id
:= Library_Unit
(N
);
2082 Par_Unit
: constant Entity_Id
:= Current_Scope
;
2084 Lib_Spec
: Node_Id
:= Library_Unit
(Lib_Unit
);
2085 Num_Scopes
: Nat
:= 0;
2086 Use_Clauses
: array (1 .. Scope_Stack
.Last
) of Node_Id
;
2087 Enclosing_Child
: Entity_Id
:= Empty
;
2088 Svg
: constant Suppress_Record
:= Scope_Suppress
;
2090 Save_Cunit_Restrictions
: constant Save_Cunit_Boolean_Restrictions
:=
2091 Cunit_Boolean_Restrictions_Save
;
2092 -- Save non-partition wide restrictions before processing the subunit.
2093 -- All subunits are analyzed with config restrictions reset and we need
2094 -- to restore these saved values at the end.
2096 procedure Analyze_Subunit_Context
;
2097 -- Capture names in use clauses of the subunit. This must be done before
2098 -- re-installing parent declarations, because items in the context must
2099 -- not be hidden by declarations local to the parent.
2101 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
);
2102 -- Recursive procedure to restore scope of all ancestors of subunit,
2103 -- from outermost in. If parent is not a subunit, the call to install
2104 -- context installs context of spec and (if parent is a child unit) the
2105 -- context of its parents as well. It is confusing that parents should
2106 -- be treated differently in both cases, but the semantics are just not
2109 procedure Re_Install_Use_Clauses
;
2110 -- As part of the removal of the parent scope, the use clauses are
2111 -- removed, to be reinstalled when the context of the subunit has been
2112 -- analyzed. Use clauses may also have been affected by the analysis of
2113 -- the context of the subunit, so they have to be applied again, to
2114 -- insure that the compilation environment of the rest of the parent
2115 -- unit is identical.
2117 procedure Remove_Scope
;
2118 -- Remove current scope from scope stack, and preserve the list of use
2119 -- clauses in it, to be reinstalled after context is analyzed.
2121 -----------------------------
2122 -- Analyze_Subunit_Context --
2123 -----------------------------
2125 procedure Analyze_Subunit_Context
is
2127 Unit_Name
: Entity_Id
;
2130 Analyze_Context
(N
);
2131 Check_No_Elab_Code_All
(N
);
2133 -- Make withed units immediately visible. If child unit, make the
2134 -- ultimate parent immediately visible.
2136 Item
:= First
(Context_Items
(N
));
2137 while Present
(Item
) loop
2138 if Nkind
(Item
) = N_With_Clause
then
2140 -- Protect frontend against previous errors in context clauses
2142 if Nkind
(Name
(Item
)) /= N_Selected_Component
then
2143 if Error_Posted
(Item
) then
2147 -- If a subunits has serious syntax errors, the context
2148 -- may not have been loaded. Add a harmless unit name to
2149 -- attempt processing.
2151 if Serious_Errors_Detected
> 0
2152 and then No
(Entity
(Name
(Item
)))
2154 Set_Entity
(Name
(Item
), Standard_Standard
);
2157 Unit_Name
:= Entity
(Name
(Item
));
2159 Set_Is_Visible_Lib_Unit
(Unit_Name
);
2160 exit when Scope
(Unit_Name
) = Standard_Standard
;
2161 Unit_Name
:= Scope
(Unit_Name
);
2163 if No
(Unit_Name
) then
2164 Check_Error_Detected
;
2169 if not Is_Immediately_Visible
(Unit_Name
) then
2170 Set_Is_Immediately_Visible
(Unit_Name
);
2171 Set_Context_Installed
(Item
);
2176 elsif Nkind
(Item
) = N_Use_Package_Clause
then
2177 Analyze
(Name
(Item
));
2179 elsif Nkind
(Item
) = N_Use_Type_Clause
then
2180 Analyze
(Subtype_Mark
(Item
));
2186 -- Reset visibility of withed units. They will be made visible again
2187 -- when we install the subunit context.
2189 Item
:= First
(Context_Items
(N
));
2190 while Present
(Item
) loop
2191 if Nkind
(Item
) = N_With_Clause
2193 -- Protect frontend against previous errors in context clauses
2195 and then Nkind
(Name
(Item
)) /= N_Selected_Component
2196 and then not Error_Posted
(Item
)
2198 Unit_Name
:= Entity
(Name
(Item
));
2200 Set_Is_Visible_Lib_Unit
(Unit_Name
, False);
2201 exit when Scope
(Unit_Name
) = Standard_Standard
;
2202 Unit_Name
:= Scope
(Unit_Name
);
2205 if Context_Installed
(Item
) then
2206 Set_Is_Immediately_Visible
(Unit_Name
, False);
2207 Set_Context_Installed
(Item
, False);
2213 end Analyze_Subunit_Context
;
2215 ------------------------
2216 -- Re_Install_Parents --
2217 ------------------------
2219 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
) is
2223 if Nkind
(Unit
(L
)) = N_Subunit
then
2224 Re_Install_Parents
(Library_Unit
(L
), Scope
(Scop
));
2227 Install_Context
(L
, False);
2229 -- If the subunit occurs within a child unit, we must restore the
2230 -- immediate visibility of any siblings that may occur in context.
2232 if Present
(Enclosing_Child
) then
2233 Install_Siblings
(Enclosing_Child
, L
);
2238 if Scop
/= Par_Unit
then
2239 Set_Is_Immediately_Visible
(Scop
);
2242 -- Make entities in scope visible again. For child units, restore
2243 -- visibility only if they are actually in context.
2245 E
:= First_Entity
(Current_Scope
);
2246 while Present
(E
) loop
2247 if not Is_Child_Unit
(E
) or else Is_Visible_Lib_Unit
(E
) then
2248 Set_Is_Immediately_Visible
(E
);
2254 -- A subunit appears within a body, and for a nested subunits all the
2255 -- parents are bodies. Restore full visibility of their private
2258 if Is_Package_Or_Generic_Package
(Scop
) then
2259 Set_In_Package_Body
(Scop
);
2260 Install_Private_Declarations
(Scop
);
2262 end Re_Install_Parents
;
2264 ----------------------------
2265 -- Re_Install_Use_Clauses --
2266 ----------------------------
2268 procedure Re_Install_Use_Clauses
is
2271 for J
in reverse 1 .. Num_Scopes
loop
2272 U
:= Use_Clauses
(J
);
2273 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:= U
;
2274 Install_Use_Clauses
(U
);
2276 end Re_Install_Use_Clauses
;
2282 procedure Remove_Scope
is
2286 Num_Scopes
:= Num_Scopes
+ 1;
2287 Use_Clauses
(Num_Scopes
) :=
2288 Scope_Stack
.Table
(Scope_Stack
.Last
).First_Use_Clause
;
2290 E
:= First_Entity
(Current_Scope
);
2291 while Present
(E
) loop
2292 Set_Is_Immediately_Visible
(E
, False);
2296 if Is_Child_Unit
(Current_Scope
) then
2297 Enclosing_Child
:= Current_Scope
;
2303 Saved_SM
: SPARK_Mode_Type
:= SPARK_Mode
;
2304 Saved_SMP
: Node_Id
:= SPARK_Mode_Pragma
;
2305 -- Save the SPARK mode-related data to restore on exit. Removing
2306 -- enclosing scopes and contexts to provide a clean environment for the
2307 -- context of the subunit will eliminate any previously set SPARK_Mode.
2309 -- Start of processing for Analyze_Subunit
2312 -- For subunit in main extended unit, we reset the configuration values
2313 -- for the non-partition-wide restrictions. For other units reset them.
2315 if In_Extended_Main_Source_Unit
(N
) then
2316 Restore_Config_Cunit_Boolean_Restrictions
;
2318 Reset_Cunit_Boolean_Restrictions
;
2323 Nam
: Node_Id
:= Name
(Unit
(N
));
2326 if Nkind
(Nam
) = N_Selected_Component
then
2327 Nam
:= Selector_Name
(Nam
);
2330 Check_Identifier
(Nam
, Par_Unit
);
2334 if not Is_Empty_List
(Context_Items
(N
)) then
2336 -- Save current use clauses
2339 Remove_Context
(Lib_Unit
);
2341 -- Now remove parents and their context, including enclosing subunits
2342 -- and the outer parent body which is not a subunit.
2344 if Present
(Lib_Spec
) then
2345 Remove_Context
(Lib_Spec
);
2347 while Nkind
(Unit
(Lib_Spec
)) = N_Subunit
loop
2348 Lib_Spec
:= Library_Unit
(Lib_Spec
);
2350 Remove_Context
(Lib_Spec
);
2353 if Nkind
(Unit
(Lib_Unit
)) = N_Subunit
then
2357 if Nkind
(Unit
(Lib_Spec
)) = N_Package_Body
then
2358 Remove_Context
(Library_Unit
(Lib_Spec
));
2362 Set_Is_Immediately_Visible
(Par_Unit
, False);
2364 Analyze_Subunit_Context
;
2366 -- Take into account the effect of any SPARK_Mode configuration
2367 -- pragma, which takes precedence over a different value of
2368 -- SPARK_Mode inherited from the context of the stub.
2370 if SPARK_Mode
/= None
then
2371 Saved_SM
:= SPARK_Mode
;
2372 Saved_SMP
:= SPARK_Mode_Pragma
;
2375 Re_Install_Parents
(Lib_Unit
, Par_Unit
);
2376 Set_Is_Immediately_Visible
(Par_Unit
);
2378 -- If the context includes a child unit of the parent of the subunit,
2379 -- the parent will have been removed from visibility, after compiling
2380 -- that cousin in the context. The visibility of the parent must be
2381 -- restored now. This also applies if the context includes another
2382 -- subunit of the same parent which in turn includes a child unit in
2385 if Is_Package_Or_Generic_Package
(Par_Unit
) then
2386 if not Is_Immediately_Visible
(Par_Unit
)
2387 or else (Present
(First_Entity
(Par_Unit
))
2389 Is_Immediately_Visible
(First_Entity
(Par_Unit
)))
2391 Set_Is_Immediately_Visible
(Par_Unit
);
2392 Install_Visible_Declarations
(Par_Unit
);
2393 Install_Private_Declarations
(Par_Unit
);
2397 Re_Install_Use_Clauses
;
2398 Install_Context
(N
, Chain
=> False);
2400 -- Restore state of suppress flags for current body
2402 Scope_Suppress
:= Svg
;
2404 -- If the subunit is within a child unit, then siblings of any parent
2405 -- unit that appear in the context clause of the subunit must also be
2406 -- made immediately visible.
2408 if Present
(Enclosing_Child
) then
2409 Install_Siblings
(Enclosing_Child
, N
);
2413 Generate_Parent_References
(Unit
(N
), Par_Unit
);
2415 -- Reinstall the SPARK_Mode which was in effect prior to any scope and
2416 -- context manipulations, taking into account a possible SPARK_Mode
2417 -- configuration pragma if present.
2419 Install_SPARK_Mode
(Saved_SM
, Saved_SMP
);
2421 -- If the subunit is part of a compilation unit which is subject to
2422 -- pragma Elaboration_Checks, set the model specified by the pragma
2423 -- because it applies to all parts of the unit.
2425 Install_Elaboration_Model
(Par_Unit
);
2427 Analyze
(Proper_Body
(Unit
(N
)));
2430 -- The subunit may contain a with_clause on a sibling of some ancestor.
2431 -- Removing the context will remove from visibility those ancestor child
2432 -- units, which must be restored to the visibility they have in the
2435 if Present
(Enclosing_Child
) then
2440 while Present
(C
) and then C
/= Standard_Standard
loop
2441 Set_Is_Immediately_Visible
(C
);
2442 Set_Is_Visible_Lib_Unit
(C
);
2448 -- Deal with restore of restrictions
2450 Cunit_Boolean_Restrictions_Restore
(Save_Cunit_Restrictions
);
2451 end Analyze_Subunit
;
2453 ----------------------------
2454 -- Analyze_Task_Body_Stub --
2455 ----------------------------
2457 procedure Analyze_Task_Body_Stub
(N
: Node_Id
) is
2458 Id
: constant Entity_Id
:= Defining_Entity
(N
);
2459 Loc
: constant Source_Ptr
:= Sloc
(N
);
2460 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Id
);
2463 Check_Stub_Level
(N
);
2465 -- First occurrence of name may have been as an incomplete type
2467 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
2468 Nam
:= Full_View
(Nam
);
2471 if No
(Nam
) or else not Is_Task_Type
(Etype
(Nam
)) then
2472 Error_Msg_N
("missing specification for task body", N
);
2475 Set_Scope
(Id
, Current_Scope
);
2476 Set_Ekind
(Id
, E_Task_Body
);
2477 Set_Etype
(Id
, Standard_Void_Type
);
2479 if Has_Aspects
(N
) then
2480 Analyze_Aspect_Specifications
(N
, Id
);
2483 Generate_Reference
(Nam
, Id
, 'b');
2484 Set_Corresponding_Spec_Of_Stub
(N
, Nam
);
2486 -- Check for duplicate stub, if so give message and terminate
2488 if Has_Completion
(Etype
(Nam
)) then
2489 Error_Msg_N
("duplicate stub for task", N
);
2492 Set_Has_Completion
(Etype
(Nam
));
2495 Analyze_Proper_Body
(N
, Etype
(Nam
));
2497 -- Set elaboration flag to indicate that entity is callable. This
2498 -- cannot be done in the expansion of the body itself, because the
2499 -- proper body is not in a declarative part. This is only done if
2500 -- expansion is active, because the context may be generic and the
2501 -- flag not defined yet.
2503 if Expander_Active
then
2505 Make_Assignment_Statement
(Loc
,
2507 Make_Identifier
(Loc
,
2508 Chars
=> New_External_Name
(Chars
(Etype
(Nam
)), 'E')),
2509 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
2512 end Analyze_Task_Body_Stub
;
2514 -------------------------
2515 -- Analyze_With_Clause --
2516 -------------------------
2518 -- Analyze the declaration of a unit in a with clause. At end, label the
2519 -- with clause with the defining entity for the unit.
2521 procedure Analyze_With_Clause
(N
: Node_Id
) is
2523 -- Retrieve the original kind of the unit node, before analysis. If it
2524 -- is a subprogram instantiation, its analysis below will rewrite the
2525 -- node as the declaration of the wrapper package. If the same
2526 -- instantiation appears indirectly elsewhere in the context, it will
2527 -- have been analyzed already.
2529 Unit_Kind
: constant Node_Kind
:=
2530 Nkind
(Original_Node
(Unit
(Library_Unit
(N
))));
2531 Nam
: constant Node_Id
:= Name
(N
);
2533 Par_Name
: Entity_Id
;
2538 -- Set True if the unit currently being compiled is an internal unit
2540 Restriction_Violation
: Boolean := False;
2541 -- Set True if a with violates a restriction, no point in giving any
2542 -- warnings if we have this definite error.
2544 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
2547 U
:= Unit
(Library_Unit
(N
));
2549 -- If this is an internal unit which is a renaming, then this is a
2550 -- violation of No_Obsolescent_Features.
2552 -- Note: this is not quite right if the user defines one of these units
2553 -- himself, but that's a marginal case, and fixing it is hard ???
2555 if Restriction_Check_Required
(No_Obsolescent_Features
) then
2556 if In_Predefined_Renaming
(U
) then
2557 Check_Restriction
(No_Obsolescent_Features
, N
);
2558 Restriction_Violation
:= True;
2562 -- Check No_Implementation_Units violation
2564 if Restriction_Check_Required
(No_Implementation_Units
) then
2565 if Not_Impl_Defined_Unit
(Get_Source_Unit
(U
)) then
2568 Check_Restriction
(No_Implementation_Units
, Nam
);
2569 Restriction_Violation
:= True;
2573 -- Several actions are skipped for dummy packages (those supplied for
2574 -- with's where no matching file could be found). Such packages are
2575 -- identified by the Sloc value being set to No_Location.
2577 if Limited_Present
(N
) then
2579 -- Ada 2005 (AI-50217): Build visibility structures but do not
2580 -- analyze the unit.
2582 -- If the designated unit is a predefined unit, which might be used
2583 -- implicitly through the rtsfind machinery, a limited with clause
2584 -- on such a unit is usually pointless, because run-time units are
2585 -- unlikely to appear in mutually dependent units, and because this
2586 -- disables the rtsfind mechanism. We transform such limited with
2587 -- clauses into regular with clauses.
2589 if Sloc
(U
) /= No_Location
then
2590 if In_Predefined_Unit
(U
)
2592 -- In ASIS mode the rtsfind mechanism plays no role, and
2593 -- we need to maintain the original tree structure, so
2594 -- this transformation is not performed in this case.
2596 and then not ASIS_Mode
2598 Set_Limited_Present
(N
, False);
2599 Analyze_With_Clause
(N
);
2601 Build_Limited_Views
(N
);
2608 -- If we are compiling under "don't quit" mode (-gnatq) and we have
2609 -- already detected serious errors then we mark the with-clause nodes as
2610 -- analyzed before the corresponding compilation unit is analyzed. This
2611 -- is done here to protect the frontend against never ending recursion
2612 -- caused by circularities in the sources (because the previous errors
2613 -- may break the regular machine of the compiler implemented in
2614 -- Load_Unit to detect circularities).
2616 if Serious_Errors_Detected
> 0 and then Try_Semantics
then
2620 Semantics
(Library_Unit
(N
));
2622 Intunit
:= Is_Internal_Unit
(Current_Sem_Unit
);
2624 if Sloc
(U
) /= No_Location
then
2626 -- Check restrictions, except that we skip the check if this is an
2627 -- internal unit unless we are compiling the internal unit as the
2628 -- main unit. We also skip this for dummy packages.
2630 Check_Restriction_No_Dependence
(Nam
, N
);
2632 if not Intunit
or else Current_Sem_Unit
= Main_Unit
then
2633 Check_Restricted_Unit
(Unit_Name
(Get_Source_Unit
(U
)), N
);
2636 -- Deal with special case of GNAT.Current_Exceptions which interacts
2637 -- with the optimization of local raise statements into gotos.
2639 if Nkind
(Nam
) = N_Selected_Component
2640 and then Nkind
(Prefix
(Nam
)) = N_Identifier
2641 and then Chars
(Prefix
(Nam
)) = Name_Gnat
2642 and then Nam_In
(Chars
(Selector_Name
(Nam
)),
2643 Name_Most_Recent_Exception
,
2644 Name_Exception_Traces
)
2646 Check_Restriction
(No_Exception_Propagation
, N
);
2647 Special_Exception_Package_Used
:= True;
2650 -- Check for inappropriate with of internal implementation unit if we
2651 -- are not compiling an internal unit and also check for withing unit
2652 -- in wrong version of Ada. Do not issue these messages for implicit
2653 -- with's generated by the compiler itself.
2655 if Implementation_Unit_Warnings
2656 and then not Intunit
2657 and then not Implicit_With
(N
)
2658 and then not Restriction_Violation
2661 U_Kind
: constant Kind_Of_Unit
:=
2662 Get_Kind_Of_Unit
(Get_Source_Unit
(U
));
2665 if U_Kind
= Implementation_Unit
then
2666 Error_Msg_F
("& is an internal 'G'N'A'T unit?i?", Name
(N
));
2668 -- Add alternative name if available, otherwise issue a
2669 -- general warning message.
2671 if Error_Msg_Strlen
/= 0 then
2672 Error_Msg_F
("\use ""~"" instead?i?", Name
(N
));
2675 ("\use of this unit is non-portable and "
2676 & "version-dependent?i?", Name
(N
));
2679 elsif U_Kind
= Ada_2005_Unit
2680 and then Ada_Version
< Ada_2005
2681 and then Warn_On_Ada_2005_Compatibility
2683 Error_Msg_N
("& is an Ada 2005 unit?i?", Name
(N
));
2685 elsif U_Kind
= Ada_2012_Unit
2686 and then Ada_Version
< Ada_2012
2687 and then Warn_On_Ada_2012_Compatibility
2689 Error_Msg_N
("& is an Ada 2012 unit?i?", Name
(N
));
2695 -- Semantic analysis of a generic unit is performed on a copy of
2696 -- the original tree. Retrieve the entity on which semantic info
2697 -- actually appears.
2699 if Unit_Kind
in N_Generic_Declaration
then
2700 E_Name
:= Defining_Entity
(U
);
2702 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2703 -- the case of an instantiation, semantic analysis above will have
2704 -- replaced the unit by its instantiated version. If the instance body
2705 -- has been generated, the instance now denotes the body entity. For
2706 -- visibility purposes we need the entity of its spec.
2708 elsif (Unit_Kind
= N_Package_Instantiation
2709 or else Nkind
(Original_Node
(Unit
(Library_Unit
(N
)))) =
2710 N_Package_Instantiation
)
2711 and then Nkind
(U
) = N_Package_Body
2713 E_Name
:= Corresponding_Spec
(U
);
2715 elsif Unit_Kind
= N_Package_Instantiation
2716 and then Nkind
(U
) = N_Package_Instantiation
2717 and then Present
(Instance_Spec
(U
))
2719 -- If the instance has not been rewritten as a package declaration,
2720 -- then it appeared already in a previous with clause. Retrieve
2721 -- the entity from the previous instance.
2723 E_Name
:= Defining_Entity
(Specification
(Instance_Spec
(U
)));
2725 elsif Unit_Kind
in N_Subprogram_Instantiation
then
2727 -- The visible subprogram is created during instantiation, and is
2728 -- an attribute of the wrapper package. We retrieve the wrapper
2729 -- package directly from the instantiation node. If the instance
2730 -- is inlined the unit is still an instantiation. Otherwise it has
2731 -- been rewritten as the declaration of the wrapper itself.
2733 if Nkind
(U
) in N_Subprogram_Instantiation
then
2736 (Defining_Entity
(Specification
(Instance_Spec
(U
))));
2738 E_Name
:= Related_Instance
(Defining_Entity
(U
));
2741 elsif Unit_Kind
= N_Package_Renaming_Declaration
2742 or else Unit_Kind
in N_Generic_Renaming_Declaration
2744 E_Name
:= Defining_Entity
(U
);
2746 elsif Unit_Kind
= N_Subprogram_Body
2747 and then Nkind
(Name
(N
)) = N_Selected_Component
2748 and then not Acts_As_Spec
(Library_Unit
(N
))
2750 -- For a child unit that has no spec, one has been created and
2751 -- analyzed. The entity required is that of the spec.
2753 E_Name
:= Corresponding_Spec
(U
);
2756 E_Name
:= Defining_Entity
(U
);
2759 if Nkind
(Name
(N
)) = N_Selected_Component
then
2761 -- Child unit in a with clause
2763 Change_Selected_Component_To_Expanded_Name
(Name
(N
));
2765 -- If this is a child unit without a spec, and it has been analyzed
2766 -- already, a declaration has been created for it. The with_clause
2767 -- must reflect the actual body, and not the generated declaration,
2768 -- to prevent spurious binding errors involving an out-of-date spec.
2769 -- Note that this can only happen if the unit includes more than one
2770 -- with_clause for the child unit (e.g. in separate subunits).
2772 if Unit_Kind
= N_Subprogram_Declaration
2773 and then Analyzed
(Library_Unit
(N
))
2774 and then not Comes_From_Source
(Library_Unit
(N
))
2776 Set_Library_Unit
(N
,
2777 Cunit
(Get_Source_Unit
(Corresponding_Body
(U
))));
2781 -- Restore style checks
2783 Style_Check
:= Save_Style_Check
;
2785 -- Record the reference, but do NOT set the unit as referenced, we want
2786 -- to consider the unit as unreferenced if this is the only reference
2789 Set_Entity_With_Checks
(Name
(N
), E_Name
);
2790 Generate_Reference
(E_Name
, Name
(N
), 'w', Set_Ref
=> False);
2792 -- Generate references and check No_Dependence restriction for parents
2794 if Is_Child_Unit
(E_Name
) then
2795 Pref
:= Prefix
(Name
(N
));
2796 Par_Name
:= Scope
(E_Name
);
2797 while Nkind
(Pref
) = N_Selected_Component
loop
2798 Change_Selected_Component_To_Expanded_Name
(Pref
);
2800 if Present
(Entity
(Selector_Name
(Pref
)))
2802 Present
(Renamed_Entity
(Entity
(Selector_Name
(Pref
))))
2803 and then Entity
(Selector_Name
(Pref
)) /= Par_Name
2805 -- The prefix is a child unit that denotes a renaming declaration.
2806 -- Replace the prefix directly with the renamed unit, because the
2807 -- rest of the prefix is irrelevant to the visibility of the real
2810 Rewrite
(Pref
, New_Occurrence_Of
(Par_Name
, Sloc
(Pref
)));
2814 Set_Entity_With_Checks
(Pref
, Par_Name
);
2816 Generate_Reference
(Par_Name
, Pref
);
2817 Check_Restriction_No_Dependence
(Pref
, N
);
2818 Pref
:= Prefix
(Pref
);
2820 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2821 -- is set to Standard_Standard, and no attempt should be made to
2822 -- further unwind scopes.
2824 if Par_Name
/= Standard_Standard
then
2825 Par_Name
:= Scope
(Par_Name
);
2828 -- Abandon processing in case of previous errors
2830 if No
(Par_Name
) then
2831 Check_Error_Detected
;
2836 if Present
(Entity
(Pref
))
2837 and then not Analyzed
(Parent
(Parent
(Entity
(Pref
))))
2839 -- If the entity is set without its unit being compiled, the
2840 -- original parent is a renaming, and Par_Name is the renamed
2841 -- entity. For visibility purposes, we need the original entity,
2842 -- which must be analyzed now because Load_Unit directly retrieves
2843 -- the renamed unit, and the renaming declaration itself has not
2846 Analyze
(Parent
(Parent
(Entity
(Pref
))));
2847 pragma Assert
(Renamed_Object
(Entity
(Pref
)) = Par_Name
);
2848 Par_Name
:= Entity
(Pref
);
2851 -- Guard against missing or misspelled child units
2853 if Present
(Par_Name
) then
2854 Set_Entity_With_Checks
(Pref
, Par_Name
);
2855 Generate_Reference
(Par_Name
, Pref
);
2858 pragma Assert
(Serious_Errors_Detected
/= 0);
2860 -- Mark the node to indicate that a related error has been posted.
2861 -- This defends further compilation passes against improper use of
2862 -- the invalid WITH clause node.
2864 Set_Error_Posted
(N
);
2865 Set_Name
(N
, Error
);
2870 -- If the withed unit is System, and a system extension pragma is
2871 -- present, compile the extension now, rather than waiting for a
2872 -- visibility check on a specific entity.
2874 if Chars
(E_Name
) = Name_System
2875 and then Scope
(E_Name
) = Standard_Standard
2876 and then Present
(System_Extend_Unit
)
2877 and then Present_System_Aux
(N
)
2879 -- If the extension is not present, an error will have been emitted
2884 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2885 -- to private_with units; they will be made visible later (just before
2886 -- the private part is analyzed)
2888 if Private_Present
(N
) then
2889 Set_Is_Immediately_Visible
(E_Name
, False);
2892 -- Propagate Fatal_Error setting from with'ed unit to current unit
2894 case Fatal_Error
(Get_Source_Unit
(Library_Unit
(N
))) is
2896 -- Nothing to do if with'ed unit had no error
2901 -- If with'ed unit had a detected fatal error, propagate it
2903 when Error_Detected
=>
2904 Set_Fatal_Error
(Current_Sem_Unit
, Error_Detected
);
2906 -- If with'ed unit had an ignored error, then propagate it but do not
2907 -- overide an existring setting.
2909 when Error_Ignored
=>
2910 if Fatal_Error
(Current_Sem_Unit
) = None
then
2911 Set_Fatal_Error
(Current_Sem_Unit
, Error_Ignored
);
2914 end Analyze_With_Clause
;
2916 ------------------------------
2917 -- Check_Private_Child_Unit --
2918 ------------------------------
2920 procedure Check_Private_Child_Unit
(N
: Node_Id
) is
2921 Lib_Unit
: constant Node_Id
:= Unit
(N
);
2923 Curr_Unit
: Entity_Id
;
2924 Sub_Parent
: Node_Id
;
2925 Priv_Child
: Entity_Id
;
2926 Par_Lib
: Entity_Id
;
2929 function Is_Private_Library_Unit
(Unit
: Entity_Id
) return Boolean;
2930 -- Returns true if and only if the library unit is declared with
2931 -- an explicit designation of private.
2933 -----------------------------
2934 -- Is_Private_Library_Unit --
2935 -----------------------------
2937 function Is_Private_Library_Unit
(Unit
: Entity_Id
) return Boolean is
2938 Comp_Unit
: constant Node_Id
:= Parent
(Unit_Declaration_Node
(Unit
));
2941 return Private_Present
(Comp_Unit
);
2942 end Is_Private_Library_Unit
;
2944 -- Start of processing for Check_Private_Child_Unit
2947 if Nkind_In
(Lib_Unit
, N_Package_Body
, N_Subprogram_Body
) then
2948 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(N
)));
2949 Par_Lib
:= Curr_Unit
;
2951 elsif Nkind
(Lib_Unit
) = N_Subunit
then
2953 -- The parent is itself a body. The parent entity is to be found in
2954 -- the corresponding spec.
2956 Sub_Parent
:= Library_Unit
(N
);
2957 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(Sub_Parent
)));
2959 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2960 -- enclosing body, retrieve the spec entity which is the proper
2961 -- ancestor we need for the following tests.
2963 if Ekind
(Curr_Unit
) = E_Package_Body
then
2964 Curr_Unit
:= Spec_Entity
(Curr_Unit
);
2967 Par_Lib
:= Curr_Unit
;
2970 Curr_Unit
:= Defining_Entity
(Lib_Unit
);
2972 Par_Lib
:= Curr_Unit
;
2973 Par_Spec
:= Parent_Spec
(Lib_Unit
);
2975 if No
(Par_Spec
) then
2978 Par_Lib
:= Defining_Entity
(Unit
(Par_Spec
));
2982 -- Loop through context items
2984 Item
:= First
(Context_Items
(N
));
2985 while Present
(Item
) loop
2987 -- Ada 2005 (AI-262): Allow private_with of a private child package
2988 -- in public siblings
2990 if Nkind
(Item
) = N_With_Clause
2991 and then not Implicit_With
(Item
)
2992 and then not Limited_Present
(Item
)
2993 and then Is_Private_Descendant
(Entity
(Name
(Item
)))
2995 Priv_Child
:= Entity
(Name
(Item
));
2998 Curr_Parent
: Entity_Id
:= Par_Lib
;
2999 Child_Parent
: Entity_Id
:= Scope
(Priv_Child
);
3000 Prv_Ancestor
: Entity_Id
:= Child_Parent
;
3001 Curr_Private
: Boolean := Is_Private_Library_Unit
(Curr_Unit
);
3004 -- If the child unit is a public child then locate the nearest
3005 -- private ancestor. Child_Parent will then be set to the
3006 -- parent of that ancestor.
3008 if not Is_Private_Library_Unit
(Priv_Child
) then
3009 while Present
(Prv_Ancestor
)
3010 and then not Is_Private_Library_Unit
(Prv_Ancestor
)
3012 Prv_Ancestor
:= Scope
(Prv_Ancestor
);
3015 if Present
(Prv_Ancestor
) then
3016 Child_Parent
:= Scope
(Prv_Ancestor
);
3020 while Present
(Curr_Parent
)
3021 and then Curr_Parent
/= Standard_Standard
3022 and then Curr_Parent
/= Child_Parent
3025 Curr_Private
or else Is_Private_Library_Unit
(Curr_Parent
);
3026 Curr_Parent
:= Scope
(Curr_Parent
);
3029 if No
(Curr_Parent
) then
3030 Curr_Parent
:= Standard_Standard
;
3033 if Curr_Parent
/= Child_Parent
then
3034 if Ekind
(Priv_Child
) = E_Generic_Package
3035 and then Chars
(Priv_Child
) in Text_IO_Package_Name
3036 and then Chars
(Scope
(Scope
(Priv_Child
))) = Name_Ada
3037 and then Scope
(Scope
(Scope
(Priv_Child
))) =
3041 ("& is a nested package, not a compilation unit",
3042 Name
(Item
), Priv_Child
);
3046 ("unit in with clause is private child unit!", Item
);
3048 ("\current unit must also have parent&!",
3049 Item
, Child_Parent
);
3053 or else Private_Present
(Item
)
3054 or else Nkind_In
(Lib_Unit
, N_Package_Body
, N_Subunit
)
3055 or else (Nkind
(Lib_Unit
) = N_Subprogram_Body
3056 and then not Acts_As_Spec
(Parent
(Lib_Unit
)))
3062 ("current unit must also be private descendant of&",
3063 Item
, Child_Parent
);
3070 end Check_Private_Child_Unit
;
3072 ----------------------
3073 -- Check_Stub_Level --
3074 ----------------------
3076 procedure Check_Stub_Level
(N
: Node_Id
) is
3077 Par
: constant Node_Id
:= Parent
(N
);
3078 Kind
: constant Node_Kind
:= Nkind
(Par
);
3081 if Nkind_In
(Kind
, N_Package_Body
,
3085 and then Nkind_In
(Parent
(Par
), N_Compilation_Unit
, N_Subunit
)
3089 -- In an instance, a missing stub appears at any level. A warning
3090 -- message will have been emitted already for the missing file.
3092 elsif not In_Instance
then
3093 Error_Msg_N
("stub cannot appear in an inner scope", N
);
3095 elsif Expander_Active
then
3096 Error_Msg_N
("missing proper body", N
);
3098 end Check_Stub_Level
;
3100 ------------------------
3101 -- Expand_With_Clause --
3102 ------------------------
3104 procedure Expand_With_Clause
(Item
: Node_Id
; Nam
: Node_Id
; N
: Node_Id
) is
3105 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
3107 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
;
3108 -- Build name to be used in implicit with_clause. In most cases this
3109 -- is the source name, but if renamings are present we must make the
3110 -- original unit visible, not the one it renames. The entity in the
3111 -- with clause is the renamed unit, but the identifier is the one from
3112 -- the source, which allows us to recover the unit renaming.
3114 ---------------------
3115 -- Build_Unit_Name --
3116 ---------------------
3118 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
is
3123 if Nkind
(Nam
) = N_Identifier
then
3124 return New_Occurrence_Of
(Entity
(Nam
), Loc
);
3127 Ent
:= Entity
(Nam
);
3129 if Present
(Entity
(Selector_Name
(Nam
)))
3130 and then Chars
(Entity
(Selector_Name
(Nam
))) /= Chars
(Ent
)
3132 Nkind
(Unit_Declaration_Node
(Entity
(Selector_Name
(Nam
)))) =
3133 N_Package_Renaming_Declaration
3135 -- The name in the with_clause is of the form A.B.C, and B is
3136 -- given by a renaming declaration. In that case we may not
3137 -- have analyzed the unit for B, but replaced it directly in
3138 -- lib-load with the unit it renames. We have to make A.B
3139 -- visible, so analyze the declaration for B now, in case it
3140 -- has not been done yet.
3142 Ent
:= Entity
(Selector_Name
(Nam
));
3145 (Unit_Declaration_Node
(Entity
(Selector_Name
(Nam
)))));
3149 Make_Expanded_Name
(Loc
,
3150 Chars
=> Chars
(Entity
(Nam
)),
3151 Prefix
=> Build_Unit_Name
(Prefix
(Nam
)),
3152 Selector_Name
=> New_Occurrence_Of
(Ent
, Loc
));
3153 Set_Entity
(Result
, Ent
);
3157 end Build_Unit_Name
;
3161 Ent
: constant Entity_Id
:= Entity
(Nam
);
3164 -- Start of processing for Expand_With_Clause
3168 Make_With_Clause
(Loc
,
3169 Name
=> Build_Unit_Name
(Nam
));
3171 Set_Corresponding_Spec
(Withn
, Ent
);
3172 Set_First_Name
(Withn
);
3173 Set_Implicit_With
(Withn
);
3174 Set_Library_Unit
(Withn
, Parent
(Unit_Declaration_Node
(Ent
)));
3175 Set_Parent_With
(Withn
);
3177 -- If the unit is a package or generic package declaration, a private_
3178 -- with_clause on a child unit implies that the implicit with on the
3179 -- parent is also private.
3181 if Nkind_In
(Unit
(N
), N_Generic_Package_Declaration
,
3182 N_Package_Declaration
)
3184 Set_Private_Present
(Withn
, Private_Present
(Item
));
3187 Prepend
(Withn
, Context_Items
(N
));
3188 Mark_Rewrite_Insertion
(Withn
);
3190 Install_With_Clause
(Withn
);
3192 -- If we have "with X.Y;", we want to recurse on "X", except in the
3193 -- unusual case where X.Y is a renaming of X. In that case, the scope
3194 -- of X will be null.
3196 if Nkind
(Nam
) = N_Expanded_Name
3197 and then Present
(Scope
(Entity
(Prefix
(Nam
))))
3199 Expand_With_Clause
(Item
, Prefix
(Nam
), N
);
3201 end Expand_With_Clause
;
3203 --------------------------------
3204 -- Generate_Parent_References --
3205 --------------------------------
3207 procedure Generate_Parent_References
(N
: Node_Id
; P_Id
: Entity_Id
) is
3209 P_Name
: Entity_Id
:= P_Id
;
3212 if Nkind
(N
) = N_Subunit
then
3215 Pref
:= Name
(Parent
(Defining_Entity
(N
)));
3218 if Nkind
(Pref
) = N_Expanded_Name
then
3220 -- Done already, if the unit has been compiled indirectly as
3221 -- part of the closure of its context because of inlining.
3226 while Nkind
(Pref
) = N_Selected_Component
loop
3227 Change_Selected_Component_To_Expanded_Name
(Pref
);
3228 Set_Entity
(Pref
, P_Name
);
3229 Set_Etype
(Pref
, Etype
(P_Name
));
3230 Generate_Reference
(P_Name
, Pref
, 'r');
3231 Pref
:= Prefix
(Pref
);
3232 P_Name
:= Scope
(P_Name
);
3235 -- The guard here on P_Name is to handle the error condition where
3236 -- the parent unit is missing because the file was not found.
3238 if Present
(P_Name
) then
3239 Set_Entity
(Pref
, P_Name
);
3240 Set_Etype
(Pref
, Etype
(P_Name
));
3241 Generate_Reference
(P_Name
, Pref
, 'r');
3242 Style
.Check_Identifier
(Pref
, P_Name
);
3244 end Generate_Parent_References
;
3246 ---------------------
3247 -- Has_With_Clause --
3248 ---------------------
3250 function Has_With_Clause
3253 Is_Limited
: Boolean := False) return Boolean
3257 function Named_Unit
(Clause
: Node_Id
) return Entity_Id
;
3258 -- Return the entity for the unit named in a [limited] with clause
3264 function Named_Unit
(Clause
: Node_Id
) return Entity_Id
is
3266 if Nkind
(Name
(Clause
)) = N_Selected_Component
then
3267 return Entity
(Selector_Name
(Name
(Clause
)));
3269 return Entity
(Name
(Clause
));
3273 -- Start of processing for Has_With_Clause
3276 if Present
(Context_Items
(C_Unit
)) then
3277 Item
:= First
(Context_Items
(C_Unit
));
3278 while Present
(Item
) loop
3279 if Nkind
(Item
) = N_With_Clause
3280 and then Limited_Present
(Item
) = Is_Limited
3281 and then Named_Unit
(Item
) = Pack
3291 end Has_With_Clause
;
3293 -----------------------------
3294 -- Implicit_With_On_Parent --
3295 -----------------------------
3297 procedure Implicit_With_On_Parent
3298 (Child_Unit
: Node_Id
;
3301 Loc
: constant Source_Ptr
:= Sloc
(N
);
3302 P
: constant Node_Id
:= Parent_Spec
(Child_Unit
);
3303 P_Unit
: Node_Id
:= Unit
(P
);
3304 P_Name
: constant Entity_Id
:= Get_Parent_Entity
(P_Unit
);
3307 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
;
3308 -- Build prefix of child unit name. Recurse if needed
3310 function Build_Unit_Name
return Node_Id
;
3311 -- If the unit is a child unit, build qualified name with all ancestors
3313 -------------------------
3314 -- Build_Ancestor_Name --
3315 -------------------------
3317 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
is
3318 P_Ref
: constant Node_Id
:=
3319 New_Occurrence_Of
(Defining_Entity
(P
), Loc
);
3320 P_Spec
: Node_Id
:= P
;
3323 -- Ancestor may have been rewritten as a package body. Retrieve the
3324 -- original spec to trace earlier ancestors.
3326 if Nkind
(P
) = N_Package_Body
3327 and then Nkind
(Original_Node
(P
)) = N_Package_Instantiation
3329 P_Spec
:= Original_Node
(P
);
3332 if No
(Parent_Spec
(P_Spec
)) then
3336 Make_Selected_Component
(Loc
,
3338 Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Spec
))),
3339 Selector_Name
=> P_Ref
);
3341 end Build_Ancestor_Name
;
3343 ---------------------
3344 -- Build_Unit_Name --
3345 ---------------------
3347 function Build_Unit_Name
return Node_Id
is
3351 if No
(Parent_Spec
(P_Unit
)) then
3352 return New_Occurrence_Of
(P_Name
, Loc
);
3356 Make_Expanded_Name
(Loc
,
3357 Chars
=> Chars
(P_Name
),
3359 Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Unit
))),
3360 Selector_Name
=> New_Occurrence_Of
(P_Name
, Loc
));
3361 Set_Entity
(Result
, P_Name
);
3365 end Build_Unit_Name
;
3367 -- Start of processing for Implicit_With_On_Parent
3370 -- The unit of the current compilation may be a package body that
3371 -- replaces an instance node. In this case we need the original instance
3372 -- node to construct the proper parent name.
3374 if Nkind
(P_Unit
) = N_Package_Body
3375 and then Nkind
(Original_Node
(P_Unit
)) = N_Package_Instantiation
3377 P_Unit
:= Original_Node
(P_Unit
);
3380 -- We add the implicit with if the child unit is the current unit being
3381 -- compiled. If the current unit is a body, we do not want to add an
3382 -- implicit_with a second time to the corresponding spec.
3384 if Nkind
(Child_Unit
) = N_Package_Declaration
3385 and then Child_Unit
/= Unit
(Cunit
(Current_Sem_Unit
))
3390 Withn
:= Make_With_Clause
(Loc
, Name
=> Build_Unit_Name
);
3392 Set_Corresponding_Spec
(Withn
, P_Name
);
3393 Set_First_Name
(Withn
);
3394 Set_Implicit_With
(Withn
);
3395 Set_Library_Unit
(Withn
, P
);
3396 Set_Parent_With
(Withn
);
3398 -- Node is placed at the beginning of the context items, so that
3399 -- subsequent use clauses on the parent can be validated.
3401 Prepend
(Withn
, Context_Items
(N
));
3402 Mark_Rewrite_Insertion
(Withn
);
3404 Install_With_Clause
(Withn
);
3406 if Is_Child_Spec
(P_Unit
) then
3407 Implicit_With_On_Parent
(P_Unit
, N
);
3409 end Implicit_With_On_Parent
;
3415 function In_Chain
(E
: Entity_Id
) return Boolean is
3419 H
:= Current_Entity
(E
);
3420 while Present
(H
) loop
3431 ---------------------
3432 -- Install_Context --
3433 ---------------------
3435 procedure Install_Context
(N
: Node_Id
; Chain
: Boolean := True) is
3436 Lib_Unit
: constant Node_Id
:= Unit
(N
);
3439 Install_Context_Clauses
(N
, Chain
);
3441 if Is_Child_Spec
(Lib_Unit
) then
3443 (Lib_Unit
=> Lib_Unit
,
3444 Is_Private
=> Private_Present
(Parent
(Lib_Unit
)),
3448 Install_Limited_Context_Clauses
(N
);
3449 end Install_Context
;
3451 -----------------------------
3452 -- Install_Context_Clauses --
3453 -----------------------------
3455 procedure Install_Context_Clauses
(N
: Node_Id
; Chain
: Boolean := True) is
3456 Lib_Unit
: constant Node_Id
:= Unit
(N
);
3458 Uname_Node
: Entity_Id
;
3459 Check_Private
: Boolean := False;
3460 Decl_Node
: Node_Id
;
3461 Lib_Parent
: Entity_Id
;
3464 -- First skip configuration pragmas at the start of the context. They
3465 -- are not technically part of the context clause, but that's where the
3466 -- parser puts them. Note they were analyzed in Analyze_Context.
3468 Item
:= First
(Context_Items
(N
));
3469 while Present
(Item
)
3470 and then Nkind
(Item
) = N_Pragma
3471 and then Pragma_Name
(Item
) in Configuration_Pragma_Names
3476 -- Loop through the actual context clause items. We process everything
3477 -- except Limited_With clauses in this routine. Limited_With clauses
3478 -- are separately installed (see Install_Limited_Context_Clauses).
3480 while Present
(Item
) loop
3482 -- Case of explicit WITH clause
3484 if Nkind
(Item
) = N_With_Clause
3485 and then not Implicit_With
(Item
)
3487 if Limited_Present
(Item
) then
3489 -- Limited withed units will be installed later
3493 -- If Name (Item) is not an entity name, something is wrong, and
3494 -- this will be detected in due course, for now ignore the item
3496 elsif not Is_Entity_Name
(Name
(Item
)) then
3499 elsif No
(Entity
(Name
(Item
))) then
3500 Set_Entity
(Name
(Item
), Any_Id
);
3504 Uname_Node
:= Entity
(Name
(Item
));
3506 if Is_Private_Descendant
(Uname_Node
) then
3507 Check_Private
:= True;
3510 Install_With_Clause
(Item
);
3512 Decl_Node
:= Unit_Declaration_Node
(Uname_Node
);
3514 -- If the unit is a subprogram instance, it appears nested within
3515 -- a package that carries the parent information.
3517 if Is_Generic_Instance
(Uname_Node
)
3518 and then Ekind
(Uname_Node
) /= E_Package
3520 Decl_Node
:= Parent
(Parent
(Decl_Node
));
3523 if Is_Child_Spec
(Decl_Node
) then
3524 if Nkind
(Name
(Item
)) = N_Expanded_Name
then
3525 Expand_With_Clause
(Item
, Prefix
(Name
(Item
)), N
);
3527 -- If not an expanded name, the child unit must be a
3528 -- renaming, nothing to do.
3533 elsif Nkind
(Decl_Node
) = N_Subprogram_Body
3534 and then not Acts_As_Spec
(Parent
(Decl_Node
))
3535 and then Is_Child_Spec
(Unit
(Library_Unit
(Parent
(Decl_Node
))))
3537 Implicit_With_On_Parent
3538 (Unit
(Library_Unit
(Parent
(Decl_Node
))), N
);
3541 -- Check license conditions unless this is a dummy unit
3543 if Sloc
(Library_Unit
(Item
)) /= No_Location
then
3544 License_Check
: declare
3545 Withu
: constant Unit_Number_Type
:=
3546 Get_Source_Unit
(Library_Unit
(Item
));
3547 Withl
: constant License_Type
:=
3548 License
(Source_Index
(Withu
));
3549 Unitl
: constant License_Type
:=
3550 License
(Source_Index
(Current_Sem_Unit
));
3552 procedure License_Error
;
3553 -- Signal error of bad license
3559 procedure License_Error
is
3562 ("license of withed unit & may be inconsistent??",
3566 -- Start of processing for License_Check
3569 -- Exclude license check if withed unit is an internal unit.
3570 -- This situation arises e.g. with the GPL version of GNAT.
3572 if Is_Internal_Unit
(Withu
) then
3575 -- Otherwise check various cases
3587 if Withl
= Restricted
then
3591 when Modified_GPL
=>
3592 if Withl
= Restricted
or else Withl
= GPL
then
3596 when Unrestricted
=>
3603 -- Case of USE PACKAGE clause
3605 elsif Nkind
(Item
) = N_Use_Package_Clause
then
3606 Analyze_Use_Package
(Item
, Chain
);
3608 -- Case of USE TYPE clause
3610 elsif Nkind
(Item
) = N_Use_Type_Clause
then
3611 Analyze_Use_Type
(Item
, Chain
);
3615 elsif Nkind
(Item
) = N_Pragma
then
3623 if Is_Child_Spec
(Lib_Unit
) then
3625 -- The unit also has implicit with_clauses on its own parents
3627 if No
(Context_Items
(N
)) then
3628 Set_Context_Items
(N
, New_List
);
3631 Implicit_With_On_Parent
(Lib_Unit
, N
);
3634 -- If the unit is a body, the context of the specification must also
3635 -- be installed. That includes private with_clauses in that context.
3637 if Nkind
(Lib_Unit
) = N_Package_Body
3638 or else (Nkind
(Lib_Unit
) = N_Subprogram_Body
3639 and then not Acts_As_Spec
(N
))
3641 Install_Context
(Library_Unit
(N
), Chain
);
3643 -- Only install private with-clauses of a spec that comes from
3644 -- source, excluding specs created for a subprogram body that is
3647 if Comes_From_Source
(Library_Unit
(N
)) then
3648 Install_Private_With_Clauses
3649 (Defining_Entity
(Unit
(Library_Unit
(N
))));
3652 if Is_Child_Spec
(Unit
(Library_Unit
(N
))) then
3654 -- If the unit is the body of a public child unit, the private
3655 -- declarations of the parent must be made visible. If the child
3656 -- unit is private, the private declarations have been installed
3657 -- already in the call to Install_Parents for the spec. Installing
3658 -- private declarations must be done for all ancestors of public
3659 -- child units. In addition, sibling units mentioned in the
3660 -- context clause of the body are directly visible.
3668 Lib_Spec
:= Unit
(Library_Unit
(N
));
3669 while Is_Child_Spec
(Lib_Spec
) loop
3670 P
:= Unit
(Parent_Spec
(Lib_Spec
));
3671 P_Name
:= Defining_Entity
(P
);
3673 if not (Private_Present
(Parent
(Lib_Spec
)))
3674 and then not In_Private_Part
(P_Name
)
3676 Install_Private_Declarations
(P_Name
);
3677 Install_Private_With_Clauses
(P_Name
);
3678 Set_Use
(Private_Declarations
(Specification
(P
)));
3686 -- For a package body, children in context are immediately visible
3688 Install_Siblings
(Defining_Entity
(Unit
(Library_Unit
(N
))), N
);
3691 if Nkind_In
(Lib_Unit
, N_Generic_Package_Declaration
,
3692 N_Generic_Subprogram_Declaration
,
3693 N_Package_Declaration
,
3694 N_Subprogram_Declaration
)
3696 if Is_Child_Spec
(Lib_Unit
) then
3697 Lib_Parent
:= Defining_Entity
(Unit
(Parent_Spec
(Lib_Unit
)));
3698 Set_Is_Private_Descendant
3699 (Defining_Entity
(Lib_Unit
),
3700 Is_Private_Descendant
(Lib_Parent
)
3701 or else Private_Present
(Parent
(Lib_Unit
)));
3704 Set_Is_Private_Descendant
3705 (Defining_Entity
(Lib_Unit
),
3706 Private_Present
(Parent
(Lib_Unit
)));
3710 if Check_Private
then
3711 Check_Private_Child_Unit
(N
);
3713 end Install_Context_Clauses
;
3715 -------------------------------------
3716 -- Install_Limited_Context_Clauses --
3717 -------------------------------------
3719 procedure Install_Limited_Context_Clauses
(N
: Node_Id
) is
3722 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
);
3723 -- Check that the unlimited view of a given compilation_unit is not
3724 -- already visible through "use + renamings".
3726 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
);
3727 -- Check that if a limited_with clause of a given compilation_unit
3728 -- mentions a descendant of a private child of some library unit, then
3729 -- the given compilation_unit must be the declaration of a private
3730 -- descendant of that library unit, or a public descendant of such. The
3731 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3732 -- use entities on the limited with_clauses because their units have not
3733 -- been analyzed, so we have to climb the tree of ancestors looking for
3734 -- private keywords.
3736 procedure Expand_Limited_With_Clause
3737 (Comp_Unit
: Node_Id
;
3740 -- If a child unit appears in a limited_with clause, there are implicit
3741 -- limited_with clauses on all parents that are not already visible
3742 -- through a regular with clause. This procedure creates the implicit
3743 -- limited with_clauses for the parents and loads the corresponding
3744 -- units. The shadow entities are created when the inserted clause is
3745 -- analyzed. Implements Ada 2005 (AI-50217).
3747 ---------------------
3748 -- Check_Renamings --
3749 ---------------------
3751 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
) is
3759 pragma Assert
(Nkind
(W
) = N_With_Clause
);
3761 -- Protect the frontend against previous critical errors
3763 case Nkind
(Unit
(Library_Unit
(W
))) is
3764 when N_Generic_Package_Declaration
3765 | N_Generic_Subprogram_Declaration
3766 | N_Package_Declaration
3767 | N_Subprogram_Declaration
3775 -- Check "use + renamings"
3777 WEnt
:= Defining_Unit_Name
(Specification
(Unit
(Library_Unit
(W
))));
3778 Spec
:= Specification
(Unit
(P
));
3780 Item
:= First
(Visible_Declarations
(Spec
));
3781 while Present
(Item
) loop
3783 -- Look only at use package clauses
3785 if Nkind
(Item
) = N_Use_Package_Clause
then
3787 E
:= Entity
(Name
(Item
));
3789 pragma Assert
(Present
(Parent
(E
)));
3791 if Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
3792 and then Renamed_Entity
(E
) = WEnt
3794 -- The unlimited view is visible through use clause and
3795 -- renamings. There is no need to generate the error
3796 -- message here because Is_Visible_Through_Renamings
3797 -- takes care of generating the precise error message.
3801 elsif Nkind
(Parent
(E
)) = N_Package_Specification
then
3803 -- The use clause may refer to a local package.
3804 -- Check all the enclosing scopes.
3807 while E2
/= Standard_Standard
and then E2
/= WEnt
loop
3813 ("unlimited view visible through use clause ", W
);
3822 -- Recursive call to check all the ancestors
3824 if Is_Child_Spec
(Unit
(P
)) then
3825 Check_Renamings
(P
=> Parent_Spec
(Unit
(P
)), W
=> W
);
3827 end Check_Renamings
;
3829 ---------------------------------------
3830 -- Check_Private_Limited_Withed_Unit --
3831 ---------------------------------------
3833 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
) is
3834 Curr_Parent
: Node_Id
;
3835 Child_Parent
: Node_Id
;
3836 Curr_Private
: Boolean;
3839 -- Compilation unit of the parent of the withed library unit
3841 Child_Parent
:= Library_Unit
(Item
);
3843 -- If the child unit is a public child, then locate its nearest
3844 -- private ancestor, if any, then Child_Parent will then be set to
3845 -- the parent of that ancestor.
3847 if not Private_Present
(Library_Unit
(Item
)) then
3848 while Present
(Child_Parent
)
3849 and then not Private_Present
(Child_Parent
)
3851 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
3854 if No
(Child_Parent
) then
3859 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
3861 -- Traverse all the ancestors of the current compilation unit to
3862 -- check if it is a descendant of named library unit.
3864 Curr_Parent
:= Parent
(Item
);
3865 Curr_Private
:= Private_Present
(Curr_Parent
);
3867 while Present
(Parent_Spec
(Unit
(Curr_Parent
)))
3868 and then Curr_Parent
/= Child_Parent
3870 Curr_Parent
:= Parent_Spec
(Unit
(Curr_Parent
));
3871 Curr_Private
:= Curr_Private
or else Private_Present
(Curr_Parent
);
3874 if Curr_Parent
/= Child_Parent
then
3876 ("unit in with clause is private child unit!", Item
);
3878 ("\current unit must also have parent&!",
3879 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
3881 elsif Private_Present
(Parent
(Item
))
3882 or else Curr_Private
3883 or else Private_Present
(Item
)
3884 or else Nkind_In
(Unit
(Parent
(Item
)), N_Package_Body
,
3888 -- Current unit is private, of descendant of a private unit
3894 ("current unit must also be private descendant of&",
3895 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
3897 end Check_Private_Limited_Withed_Unit
;
3899 --------------------------------
3900 -- Expand_Limited_With_Clause --
3901 --------------------------------
3903 procedure Expand_Limited_With_Clause
3904 (Comp_Unit
: Node_Id
;
3908 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
3909 Unum
: Unit_Number_Type
;
3912 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean;
3913 -- Returns true if the context already includes a with_clause for
3914 -- this unit. If the with_clause is nonlimited, the unit is fully
3915 -- visible and an implicit limited_with should not be created. If
3916 -- there is already a limited_with clause for W, a second one is
3917 -- simply redundant.
3919 --------------------------
3920 -- Previous_Withed_Unit --
3921 --------------------------
3923 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean is
3927 -- A limited with_clause cannot appear in the same context_clause
3928 -- as a nonlimited with_clause which mentions the same library.
3930 Item
:= First
(Context_Items
(Comp_Unit
));
3931 while Present
(Item
) loop
3932 if Nkind
(Item
) = N_With_Clause
3933 and then Library_Unit
(Item
) = Library_Unit
(W
)
3942 end Previous_Withed_Unit
;
3944 -- Start of processing for Expand_Limited_With_Clause
3947 if Nkind
(Nam
) = N_Identifier
then
3949 -- Create node for name of withed unit
3952 Make_With_Clause
(Loc
,
3953 Name
=> New_Copy
(Nam
));
3955 else pragma Assert
(Nkind
(Nam
) = N_Selected_Component
);
3957 Make_With_Clause
(Loc
,
3958 Name
=> Make_Selected_Component
(Loc
,
3959 Prefix
=> New_Copy_Tree
(Prefix
(Nam
)),
3960 Selector_Name
=> New_Copy
(Selector_Name
(Nam
))));
3961 Set_Parent
(Withn
, Parent
(N
));
3964 Set_First_Name
(Withn
);
3965 Set_Implicit_With
(Withn
);
3966 Set_Limited_Present
(Withn
);
3970 (Load_Name
=> Get_Spec_Name
(Get_Unit_Name
(Nam
)),
3975 -- Do not generate a limited_with_clause on the current unit. This
3976 -- path is taken when a unit has a limited_with clause on one of its
3979 if Unum
= Current_Sem_Unit
then
3983 Set_Library_Unit
(Withn
, Cunit
(Unum
));
3984 Set_Corresponding_Spec
3985 (Withn
, Specification
(Unit
(Cunit
(Unum
))));
3987 if not Previous_Withed_Unit
(Withn
) then
3988 Prepend
(Withn
, Context_Items
(Parent
(N
)));
3989 Mark_Rewrite_Insertion
(Withn
);
3991 -- Add implicit limited_with_clauses for parents of child units
3992 -- mentioned in limited_with clauses.
3994 if Nkind
(Nam
) = N_Selected_Component
then
3995 Expand_Limited_With_Clause
(Comp_Unit
, Prefix
(Nam
), N
);
4000 if not Limited_View_Installed
(Withn
) then
4001 Install_Limited_With_Clause
(Withn
);
4004 end Expand_Limited_With_Clause
;
4006 -- Start of processing for Install_Limited_Context_Clauses
4009 Item
:= First
(Context_Items
(N
));
4010 while Present
(Item
) loop
4011 if Nkind
(Item
) = N_With_Clause
4012 and then Limited_Present
(Item
)
4013 and then not Error_Posted
(Item
)
4015 if Nkind
(Name
(Item
)) = N_Selected_Component
then
4016 Expand_Limited_With_Clause
4017 (Comp_Unit
=> N
, Nam
=> Prefix
(Name
(Item
)), N
=> Item
);
4020 Check_Private_Limited_Withed_Unit
(Item
);
4022 if not Implicit_With
(Item
) and then Is_Child_Spec
(Unit
(N
)) then
4023 Check_Renamings
(Parent_Spec
(Unit
(N
)), Item
);
4026 -- A unit may have a limited with on itself if it has a limited
4027 -- with_clause on one of its child units. In that case it is
4028 -- already being compiled and it makes no sense to install its
4031 -- If the item is a limited_private_with_clause, install it if the
4032 -- current unit is a body or if it is a private child. Otherwise
4033 -- the private clause is installed before analyzing the private
4034 -- part of the current unit.
4036 if Library_Unit
(Item
) /= Cunit
(Current_Sem_Unit
)
4037 and then not Limited_View_Installed
(Item
)
4039 not Is_Ancestor_Unit
4040 (Library_Unit
(Item
), Cunit
(Current_Sem_Unit
))
4042 if not Private_Present
(Item
)
4043 or else Private_Present
(N
)
4044 or else Nkind_In
(Unit
(N
), N_Package_Body
,
4048 Install_Limited_With_Clause
(Item
);
4056 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4057 -- looking for incomplete subtype declarations of incomplete types
4058 -- visible through a limited with clause.
4060 if Ada_Version
>= Ada_2005
4061 and then Analyzed
(N
)
4062 and then Nkind
(Unit
(N
)) = N_Package_Declaration
4067 Non_Lim_View
: Entity_Id
;
4070 Decl
:= First
(Visible_Declarations
(Specification
(Unit
(N
))));
4071 while Present
(Decl
) loop
4072 if Nkind
(Decl
) = N_Subtype_Declaration
4074 Ekind
(Defining_Identifier
(Decl
)) = E_Incomplete_Subtype
4076 From_Limited_With
(Defining_Identifier
(Decl
))
4078 Def_Id
:= Defining_Identifier
(Decl
);
4079 Non_Lim_View
:= Non_Limited_View
(Def_Id
);
4081 if not Is_Incomplete_Type
(Non_Lim_View
) then
4083 -- Convert an incomplete subtype declaration into a
4084 -- corresponding nonlimited view subtype declaration.
4085 -- This is usually the case when analyzing a body that
4086 -- has regular with clauses, when the spec has limited
4089 -- If the nonlimited view is still incomplete, it is
4090 -- the dummy entry already created, and the declaration
4091 -- cannot be reanalyzed. This is the case when installing
4092 -- a parent unit that has limited with-clauses.
4094 Set_Subtype_Indication
(Decl
,
4095 New_Occurrence_Of
(Non_Lim_View
, Sloc
(Def_Id
)));
4096 Set_Etype
(Def_Id
, Non_Lim_View
);
4097 Set_Ekind
(Def_Id
, Subtype_Kind
(Ekind
(Non_Lim_View
)));
4098 Set_Analyzed
(Decl
, False);
4100 -- Reanalyze the declaration, suppressing the call to
4101 -- Enter_Name to avoid duplicate names.
4103 Analyze_Subtype_Declaration
4113 end Install_Limited_Context_Clauses
;
4115 ---------------------
4116 -- Install_Parents --
4117 ---------------------
4119 procedure Install_Parents
4120 (Lib_Unit
: Node_Id
;
4121 Is_Private
: Boolean;
4122 Chain
: Boolean := True)
4130 P
:= Unit
(Parent_Spec
(Lib_Unit
));
4131 P_Name
:= Get_Parent_Entity
(P
);
4133 if Etype
(P_Name
) = Any_Type
then
4137 if Ekind
(P_Name
) = E_Generic_Package
4138 and then not Nkind_In
(Lib_Unit
, N_Generic_Subprogram_Declaration
,
4139 N_Generic_Package_Declaration
)
4140 and then Nkind
(Lib_Unit
) not in N_Generic_Renaming_Declaration
4143 ("child of a generic package must be a generic unit", Lib_Unit
);
4145 elsif not Is_Package_Or_Generic_Package
(P_Name
) then
4147 ("parent unit must be package or generic package", Lib_Unit
);
4148 raise Unrecoverable_Error
;
4150 elsif Present
(Renamed_Object
(P_Name
)) then
4151 Error_Msg_N
("parent unit cannot be a renaming", Lib_Unit
);
4152 raise Unrecoverable_Error
;
4154 -- Verify that a child of an instance is itself an instance, or the
4155 -- renaming of one. Given that an instance that is a unit is replaced
4156 -- with a package declaration, check against the original node. The
4157 -- parent may be currently being instantiated, in which case it appears
4158 -- as a declaration, but the generic_parent is already established
4159 -- indicating that we deal with an instance.
4161 elsif Nkind
(Original_Node
(P
)) = N_Package_Instantiation
then
4162 if Nkind
(Lib_Unit
) in N_Renaming_Declaration
4163 or else Nkind
(Original_Node
(Lib_Unit
)) in N_Generic_Instantiation
4165 (Nkind
(Lib_Unit
) = N_Package_Declaration
4166 and then Present
(Generic_Parent
(Specification
(Lib_Unit
))))
4171 ("child of an instance must be an instance or renaming",
4176 -- This is the recursive call that ensures all parents are loaded
4178 if Is_Child_Spec
(P
) then
4182 Is_Private
or else Private_Present
(Parent
(Lib_Unit
)),
4186 -- Now we can install the context for this parent
4188 Install_Context_Clauses
(Parent_Spec
(Lib_Unit
), Chain
);
4189 Install_Limited_Context_Clauses
(Parent_Spec
(Lib_Unit
));
4190 Install_Siblings
(P_Name
, Parent
(Lib_Unit
));
4192 -- The child unit is in the declarative region of the parent. The parent
4193 -- must therefore appear in the scope stack and be visible, as when
4194 -- compiling the corresponding body. If the child unit is private or it
4195 -- is a package body, private declarations must be accessible as well.
4196 -- Use declarations in the parent must also be installed. Finally, other
4197 -- child units of the same parent that are in the context are
4198 -- immediately visible.
4200 -- Find entity for compilation unit, and set its private descendant
4201 -- status as needed. Indicate that it is a compilation unit, which is
4202 -- redundant in general, but needed if this is a generated child spec
4203 -- for a child body without previous spec.
4205 E_Name
:= Defining_Entity
(Lib_Unit
);
4207 Set_Is_Child_Unit
(E_Name
);
4208 Set_Is_Compilation_Unit
(E_Name
);
4210 Set_Is_Private_Descendant
(E_Name
,
4211 Is_Private_Descendant
(P_Name
)
4212 or else Private_Present
(Parent
(Lib_Unit
)));
4214 P_Spec
:= Package_Specification
(P_Name
);
4215 Push_Scope
(P_Name
);
4217 -- Save current visibility of unit
4219 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
:=
4220 Is_Immediately_Visible
(P_Name
);
4221 Set_Is_Immediately_Visible
(P_Name
);
4222 Install_Visible_Declarations
(P_Name
);
4223 Set_Use
(Visible_Declarations
(P_Spec
));
4225 -- If the parent is a generic unit, its formal part may contain formal
4226 -- packages and use clauses for them.
4228 if Ekind
(P_Name
) = E_Generic_Package
then
4229 Set_Use
(Generic_Formal_Declarations
(Parent
(P_Spec
)));
4232 if Is_Private
or else Private_Present
(Parent
(Lib_Unit
)) then
4233 Install_Private_Declarations
(P_Name
);
4234 Install_Private_With_Clauses
(P_Name
);
4235 Set_Use
(Private_Declarations
(P_Spec
));
4237 end Install_Parents
;
4239 ----------------------------------
4240 -- Install_Private_With_Clauses --
4241 ----------------------------------
4243 procedure Install_Private_With_Clauses
(P
: Entity_Id
) is
4244 Decl
: constant Node_Id
:= Unit_Declaration_Node
(P
);
4248 if Debug_Flag_I
then
4249 Write_Str
("install private with clauses of ");
4250 Write_Name
(Chars
(P
));
4254 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
4255 Item
:= First
(Context_Items
(Parent
(Decl
)));
4256 while Present
(Item
) loop
4257 if Nkind
(Item
) = N_With_Clause
4258 and then Private_Present
(Item
)
4260 -- If the unit is an ancestor of the current one, it is the
4261 -- case of a private limited with clause on a child unit, and
4262 -- the compilation of one of its descendants, In that case the
4263 -- limited view is errelevant.
4265 if Limited_Present
(Item
) then
4266 if not Limited_View_Installed
(Item
)
4268 not Is_Ancestor_Unit
(Library_Unit
(Item
),
4269 Cunit
(Current_Sem_Unit
))
4271 Install_Limited_With_Clause
(Item
);
4274 Install_With_Clause
(Item
, Private_With_OK
=> True);
4281 end Install_Private_With_Clauses
;
4283 ----------------------
4284 -- Install_Siblings --
4285 ----------------------
4287 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
) is
4293 -- Iterate over explicit with clauses, and check whether the scope of
4294 -- each entity is an ancestor of the current unit, in which case it is
4295 -- immediately visible.
4297 Item
:= First
(Context_Items
(N
));
4298 while Present
(Item
) loop
4300 -- Do not install private_with_clauses declaration, unless unit
4301 -- is itself a private child unit, or is a body. Note that for a
4302 -- subprogram body the private_with_clause does not take effect
4303 -- until after the specification.
4305 if Nkind
(Item
) /= N_With_Clause
4306 or else Implicit_With
(Item
)
4307 or else Limited_Present
(Item
)
4308 or else Error_Posted
(Item
)
4310 -- Skip processing malformed trees
4312 or else (Try_Semantics
4313 and then Nkind
(Name
(Item
)) not in N_Has_Entity
)
4317 elsif not Private_Present
(Item
)
4318 or else Private_Present
(N
)
4319 or else Nkind
(Unit
(N
)) = N_Package_Body
4321 Id
:= Entity
(Name
(Item
));
4323 if Is_Child_Unit
(Id
)
4324 and then Is_Ancestor_Package
(Scope
(Id
), U_Name
)
4326 Set_Is_Immediately_Visible
(Id
);
4328 -- Check for the presence of another unit in the context that
4329 -- may be inadvertently hidden by the child.
4331 Prev
:= Current_Entity
(Id
);
4334 and then Is_Immediately_Visible
(Prev
)
4335 and then not Is_Child_Unit
(Prev
)
4341 Clause
:= First
(Context_Items
(N
));
4342 while Present
(Clause
) loop
4343 if Nkind
(Clause
) = N_With_Clause
4344 and then Entity
(Name
(Clause
)) = Prev
4347 ("child unit& hides compilation unit " &
4348 "with the same name??",
4358 -- The With_Clause may be on a grand-child or one of its further
4359 -- descendants, which makes a child immediately visible. Examine
4360 -- ancestry to determine whether such a child exists. For example,
4361 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4362 -- is immediately visible.
4364 elsif Is_Child_Unit
(Id
) then
4370 while Is_Child_Unit
(Par
) loop
4371 if Is_Ancestor_Package
(Scope
(Par
), U_Name
) then
4372 Set_Is_Immediately_Visible
(Par
);
4381 -- If the item is a private with-clause on a child unit, the parent
4382 -- may have been installed already, but the child unit must remain
4383 -- invisible until installed in a private part or body, unless there
4384 -- is already a regular with_clause for it in the current unit.
4386 elsif Private_Present
(Item
) then
4387 Id
:= Entity
(Name
(Item
));
4389 if Is_Child_Unit
(Id
) then
4393 function In_Context
return Boolean;
4394 -- Scan context of current unit, to check whether there is
4395 -- a with_clause on the same unit as a private with-clause
4396 -- on a parent, in which case child unit is visible. If the
4397 -- unit is a grand-child, the same applies to its parent.
4403 function In_Context
return Boolean is
4406 First
(Context_Items
(Cunit
(Current_Sem_Unit
)));
4407 while Present
(Clause
) loop
4408 if Nkind
(Clause
) = N_With_Clause
4409 and then Comes_From_Source
(Clause
)
4410 and then Is_Entity_Name
(Name
(Clause
))
4411 and then not Private_Present
(Clause
)
4413 if Entity
(Name
(Clause
)) = Id
4415 (Nkind
(Name
(Clause
)) = N_Expanded_Name
4416 and then Entity
(Prefix
(Name
(Clause
))) = Id
)
4429 Set_Is_Visible_Lib_Unit
(Id
, In_Context
);
4436 end Install_Siblings
;
4438 ---------------------------------
4439 -- Install_Limited_With_Clause --
4440 ---------------------------------
4442 procedure Install_Limited_With_Clause
(N
: Node_Id
) is
4443 P_Unit
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
4446 Is_Child_Package
: Boolean := False;
4447 Lim_Header
: Entity_Id
;
4448 Lim_Typ
: Entity_Id
;
4450 procedure Check_Body_Required
;
4451 -- A unit mentioned in a limited with_clause may not be mentioned in
4452 -- a regular with_clause, but must still be included in the current
4453 -- partition. We need to determine whether the unit needs a body, so
4454 -- that the binder can determine the name of the file to be compiled.
4455 -- Checking whether a unit needs a body can be done without semantic
4456 -- analysis, by examining the nature of the declarations in the package.
4458 function Has_Limited_With_Clause
4459 (C_Unit
: Entity_Id
;
4460 Pack
: Entity_Id
) return Boolean;
4461 -- Determine whether any package in the ancestor chain starting with
4462 -- C_Unit has a limited with clause for package Pack.
4464 function Is_Visible_Through_Renamings
(P
: Entity_Id
) return Boolean;
4465 -- Check if some package installed though normal with-clauses has a
4466 -- renaming declaration of package P. AARM 10.1.2(21/2).
4468 -------------------------
4469 -- Check_Body_Required --
4470 -------------------------
4472 procedure Check_Body_Required
is
4473 PA
: constant List_Id
:=
4474 Pragmas_After
(Aux_Decls_Node
(Parent
(P_Unit
)));
4476 procedure Check_Declarations
(Spec
: Node_Id
);
4477 -- Recursive procedure that does the work and checks nested packages
4479 ------------------------
4480 -- Check_Declarations --
4481 ------------------------
4483 procedure Check_Declarations
(Spec
: Node_Id
) is
4485 Incomplete_Decls
: constant Elist_Id
:= New_Elmt_List
;
4487 Subp_List
: constant Elist_Id
:= New_Elmt_List
;
4489 procedure Check_Pragma_Import
(P
: Node_Id
);
4490 -- If a pragma import applies to a previous subprogram, the
4491 -- enclosing unit may not need a body. The processing is syntactic
4492 -- and does not require a declaration to be analyzed. The code
4493 -- below also handles pragma Import when applied to a subprogram
4494 -- that renames another. In this case the pragma applies to the
4497 -- Chains of multiple renames are not handled by the code below.
4498 -- It is probably impossible to handle all cases without proper
4499 -- name resolution. In such cases the algorithm is conservative
4500 -- and will indicate that a body is needed???
4502 -------------------------
4503 -- Check_Pragma_Import --
4504 -------------------------
4506 procedure Check_Pragma_Import
(P
: Node_Id
) is
4512 procedure Remove_Homonyms
(E
: Node_Id
);
4513 -- Make one pass over list of subprograms. Called again if
4514 -- subprogram is a renaming. E is known to be an identifier.
4516 ---------------------
4517 -- Remove_Homonyms --
4518 ---------------------
4520 procedure Remove_Homonyms
(E
: Node_Id
) is
4521 R
: Entity_Id
:= Empty
;
4522 -- Name of renamed entity, if any
4525 Subp_Id
:= First_Elmt
(Subp_List
);
4526 while Present
(Subp_Id
) loop
4527 if Chars
(Node
(Subp_Id
)) = Chars
(E
) then
4528 if Nkind
(Parent
(Parent
(Node
(Subp_Id
))))
4529 /= N_Subprogram_Renaming_Declaration
4532 Next_Elmt
(Subp_Id
);
4533 Remove_Elmt
(Subp_List
, Prev_Id
);
4535 R
:= Name
(Parent
(Parent
(Node
(Subp_Id
))));
4539 Next_Elmt
(Subp_Id
);
4544 if Nkind
(R
) = N_Identifier
then
4545 Remove_Homonyms
(R
);
4547 elsif Nkind
(R
) = N_Selected_Component
then
4548 Remove_Homonyms
(Selector_Name
(R
));
4550 -- Renaming of attribute
4556 end Remove_Homonyms
;
4558 -- Start of processing for Check_Pragma_Import
4561 -- Find name of entity in Import pragma. We have not analyzed
4562 -- the construct, so we must guard against syntax errors.
4564 Arg
:= Next
(First
(Pragma_Argument_Associations
(P
)));
4567 or else Nkind
(Expression
(Arg
)) /= N_Identifier
4571 Imported
:= Expression
(Arg
);
4574 Remove_Homonyms
(Imported
);
4575 end Check_Pragma_Import
;
4577 -- Start of processing for Check_Declarations
4580 -- Search for Elaborate Body pragma
4582 Decl
:= First
(Visible_Declarations
(Spec
));
4583 while Present
(Decl
)
4584 and then Nkind
(Decl
) = N_Pragma
4586 if Get_Pragma_Id
(Decl
) = Pragma_Elaborate_Body
then
4587 Set_Body_Required
(Library_Unit
(N
));
4594 -- Look for declarations that require the presence of a body. We
4595 -- have already skipped pragmas at the start of the list.
4597 while Present
(Decl
) loop
4599 -- Subprogram that comes from source means body may be needed.
4600 -- Save for subsequent examination of import pragmas.
4602 if Comes_From_Source
(Decl
)
4603 and then (Nkind_In
(Decl
, N_Subprogram_Declaration
,
4604 N_Subprogram_Renaming_Declaration
,
4605 N_Generic_Subprogram_Declaration
))
4607 Append_Elmt
(Defining_Entity
(Decl
), Subp_List
);
4609 -- Package declaration of generic package declaration. We need
4610 -- to recursively examine nested declarations.
4612 elsif Nkind_In
(Decl
, N_Package_Declaration
,
4613 N_Generic_Package_Declaration
)
4615 Check_Declarations
(Specification
(Decl
));
4617 elsif Nkind
(Decl
) = N_Pragma
4618 and then Pragma_Name
(Decl
) = Name_Import
4620 Check_Pragma_Import
(Decl
);
4626 -- Same set of tests for private part. In addition to subprograms
4627 -- detect the presence of Taft Amendment types (incomplete types
4628 -- completed in the body).
4630 Decl
:= First
(Private_Declarations
(Spec
));
4631 while Present
(Decl
) loop
4632 if Comes_From_Source
(Decl
)
4633 and then (Nkind_In
(Decl
, N_Subprogram_Declaration
,
4634 N_Subprogram_Renaming_Declaration
,
4635 N_Generic_Subprogram_Declaration
))
4637 Append_Elmt
(Defining_Entity
(Decl
), Subp_List
);
4639 elsif Nkind_In
(Decl
, N_Package_Declaration
,
4640 N_Generic_Package_Declaration
)
4642 Check_Declarations
(Specification
(Decl
));
4644 -- Collect incomplete type declarations for separate pass
4646 elsif Nkind
(Decl
) = N_Incomplete_Type_Declaration
then
4647 Append_Elmt
(Decl
, Incomplete_Decls
);
4649 elsif Nkind
(Decl
) = N_Pragma
4650 and then Pragma_Name
(Decl
) = Name_Import
4652 Check_Pragma_Import
(Decl
);
4658 -- Now check incomplete declarations to locate Taft amendment
4659 -- types. This can be done by examining the defining identifiers
4660 -- of type declarations without real semantic analysis.
4666 Inc
:= First_Elmt
(Incomplete_Decls
);
4667 while Present
(Inc
) loop
4668 Decl
:= Next
(Node
(Inc
));
4669 while Present
(Decl
) loop
4670 if Nkind
(Decl
) = N_Full_Type_Declaration
4671 and then Chars
(Defining_Identifier
(Decl
)) =
4672 Chars
(Defining_Identifier
(Node
(Inc
)))
4680 -- If no completion, this is a TAT, and a body is needed
4683 Set_Body_Required
(Library_Unit
(N
));
4691 -- Finally, check whether there are subprograms that still require
4692 -- a body, i.e. are not renamings or null.
4694 if not Is_Empty_Elmt_List
(Subp_List
) then
4700 Subp_Id
:= First_Elmt
(Subp_List
);
4701 Spec
:= Parent
(Node
(Subp_Id
));
4703 while Present
(Subp_Id
) loop
4704 if Nkind
(Parent
(Spec
))
4705 = N_Subprogram_Renaming_Declaration
4709 elsif Nkind
(Spec
) = N_Procedure_Specification
4710 and then Null_Present
(Spec
)
4715 Set_Body_Required
(Library_Unit
(N
));
4719 Next_Elmt
(Subp_Id
);
4723 end Check_Declarations
;
4725 -- Start of processing for Check_Body_Required
4728 -- If this is an imported package (Java and CIL usage) no body is
4729 -- needed. Scan list of pragmas that may follow a compilation unit
4730 -- to look for a relevant pragma Import.
4732 if Present
(PA
) then
4738 while Present
(Prag
) loop
4739 if Nkind
(Prag
) = N_Pragma
4740 and then Get_Pragma_Id
(Prag
) = Pragma_Import
4750 Check_Declarations
(Specification
(P_Unit
));
4751 end Check_Body_Required
;
4753 -----------------------------
4754 -- Has_Limited_With_Clause --
4755 -----------------------------
4757 function Has_Limited_With_Clause
4758 (C_Unit
: Entity_Id
;
4759 Pack
: Entity_Id
) return Boolean
4766 while Present
(Par
) loop
4767 if Ekind
(Par
) /= E_Package
then
4771 -- Retrieve the Compilation_Unit node for Par and determine if
4772 -- its context clauses contain a limited with for Pack.
4774 Par_Unit
:= Parent
(Parent
(Parent
(Par
)));
4776 if Nkind
(Par_Unit
) = N_Package_Declaration
then
4777 Par_Unit
:= Parent
(Par_Unit
);
4780 if Has_With_Clause
(Par_Unit
, Pack
, True) then
4784 -- If there are more ancestors, climb up the tree, otherwise we
4787 if Is_Child_Unit
(Par
) then
4795 end Has_Limited_With_Clause
;
4797 ----------------------------------
4798 -- Is_Visible_Through_Renamings --
4799 ----------------------------------
4801 function Is_Visible_Through_Renamings
(P
: Entity_Id
) return Boolean is
4802 Kind
: constant Node_Kind
:=
4803 Nkind
(Unit
(Cunit
(Current_Sem_Unit
)));
4809 -- Example of the error detected by this subprogram:
4817 -- package Ren_P renames P;
4823 -- limited with P; -- ERROR
4824 -- package R.C is ...
4826 Aux_Unit
:= Cunit
(Current_Sem_Unit
);
4829 Item
:= First
(Context_Items
(Aux_Unit
));
4830 while Present
(Item
) loop
4831 if Nkind
(Item
) = N_With_Clause
4832 and then not Limited_Present
(Item
)
4833 and then Nkind
(Unit
(Library_Unit
(Item
))) =
4834 N_Package_Declaration
4837 First
(Visible_Declarations
4838 (Specification
(Unit
(Library_Unit
(Item
)))));
4839 while Present
(Decl
) loop
4840 if Nkind
(Decl
) = N_Package_Renaming_Declaration
4841 and then Entity
(Name
(Decl
)) = P
4843 -- Generate the error message only if the current unit
4844 -- is a package declaration; in case of subprogram
4845 -- bodies and package bodies we just return True to
4846 -- indicate that the limited view must not be
4849 if Kind
= N_Package_Declaration
then
4851 ("simultaneous visibility of the limited and " &
4852 "unlimited views not allowed", N
);
4853 Error_Msg_Sloc
:= Sloc
(Item
);
4855 ("\\ unlimited view of & visible through the " &
4856 "context clause #", N
, P
);
4857 Error_Msg_Sloc
:= Sloc
(Decl
);
4858 Error_Msg_NE
("\\ and the renaming #", N
, P
);
4871 -- If it is a body not acting as spec, follow pointer to the
4872 -- corresponding spec, otherwise follow pointer to parent spec.
4874 if Present
(Library_Unit
(Aux_Unit
))
4875 and then Nkind_In
(Unit
(Aux_Unit
),
4876 N_Package_Body
, N_Subprogram_Body
)
4878 if Aux_Unit
= Library_Unit
(Aux_Unit
) then
4880 -- Aux_Unit is a body that acts as a spec. Clause has
4881 -- already been flagged as illegal.
4886 Aux_Unit
:= Library_Unit
(Aux_Unit
);
4890 Aux_Unit
:= Parent_Spec
(Unit
(Aux_Unit
));
4893 exit when No
(Aux_Unit
);
4897 end Is_Visible_Through_Renamings
;
4899 -- Start of processing for Install_Limited_With_Clause
4902 pragma Assert
(not Limited_View_Installed
(N
));
4904 -- In case of limited with_clause on subprograms, generics, instances,
4905 -- or renamings, the corresponding error was previously posted and we
4906 -- have nothing to do here. If the file is missing altogether, it has
4907 -- no source location.
4909 if Nkind
(P_Unit
) /= N_Package_Declaration
4910 or else Sloc
(P_Unit
) = No_Location
4915 P
:= Defining_Unit_Name
(Specification
(P_Unit
));
4917 -- Handle child packages
4919 if Nkind
(P
) = N_Defining_Program_Unit_Name
then
4920 Is_Child_Package
:= True;
4921 P
:= Defining_Identifier
(P
);
4924 -- Do not install the limited-view if the context of the unit is already
4925 -- available through a regular with clause.
4927 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4928 and then Has_With_Clause
(Cunit
(Current_Sem_Unit
), P
)
4933 -- Do not install the limited-view if the full-view is already visible
4934 -- through renaming declarations.
4936 if Is_Visible_Through_Renamings
(P
) then
4940 -- Do not install the limited view if this is the unit being analyzed.
4941 -- This unusual case will happen when a unit has a limited_with clause
4942 -- on one of its children. The compilation of the child forces the load
4943 -- of the parent which tries to install the limited view of the child
4944 -- again. Installing the limited view must also be disabled when
4945 -- compiling the body of the child unit.
4947 if P
= Cunit_Entity
(Current_Sem_Unit
)
4948 or else (Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4949 and then P
= Main_Unit_Entity
4950 and then Is_Ancestor_Unit
4951 (Cunit
(Main_Unit
), Cunit
(Current_Sem_Unit
)))
4956 -- This scenario is similar to the one above, the difference is that the
4957 -- compilation of sibling Par.Sib forces the load of parent Par which
4958 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4959 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4960 -- nonlimited views of all entities from Lim_Pack.
4962 -- limited with Lim_Pack; -- [1]
4963 -- package Par is ... package Lim_Pack is ...
4965 -- with Lim_Pack; -- [2]
4966 -- package Par.Sib is ... package body Par.Sib is ...
4968 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4969 -- Sem_Unit is the body of Par.Sib.
4971 if Ekind
(P
) = E_Package
4972 and then Ekind
(Main_Unit_Entity
) = E_Package
4973 and then Is_Child_Unit
(Main_Unit_Entity
)
4975 -- The body has a regular with clause
4977 and then Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
4978 and then Has_With_Clause
(Cunit
(Current_Sem_Unit
), P
)
4980 -- One of the ancestors has a limited with clause
4982 and then Nkind
(Parent
(Parent
(Main_Unit_Entity
))) =
4983 N_Package_Specification
4984 and then Has_Limited_With_Clause
(Scope
(Main_Unit_Entity
), P
)
4989 -- A common use of the limited-with is to have a limited-with in the
4990 -- package spec, and a normal with in its package body. For example:
4992 -- limited with X; -- [1]
4996 -- package body A is ...
4998 -- The compilation of A's body installs the context clauses found at [2]
4999 -- and then the context clauses of its specification (found at [1]). As
5000 -- a consequence, at [1] the specification of X has been analyzed and it
5001 -- is immediately visible. According to the semantics of limited-with
5002 -- context clauses we don't install the limited view because the full
5003 -- view of X supersedes its limited view.
5005 if Analyzed
(P_Unit
)
5007 (Is_Immediately_Visible
(P
)
5008 or else (Is_Child_Package
and then Is_Visible_Lib_Unit
(P
)))
5011 -- The presence of both the limited and the analyzed nonlimited view
5012 -- may also be an error, such as an illegal context for a limited
5013 -- with_clause. In that case, do not process the context item at all.
5015 if Error_Posted
(N
) then
5019 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
then
5023 Item
:= First
(Context_Items
(Cunit
(Current_Sem_Unit
)));
5024 while Present
(Item
) loop
5025 if Nkind
(Item
) = N_With_Clause
5026 and then Comes_From_Source
(Item
)
5027 and then Entity
(Name
(Item
)) = P
5036 -- If this is a child body, assume that the nonlimited with_clause
5037 -- appears in an ancestor. Could be refined ???
5041 (Unit
(Library_Unit
(Cunit
(Current_Sem_Unit
)))))
5048 -- If in package declaration, nonlimited view brought in from
5049 -- parent unit or some error condition.
5055 if Debug_Flag_I
then
5056 Write_Str
("install limited view of ");
5057 Write_Name
(Chars
(P
));
5061 -- If the unit has not been analyzed and the limited view has not been
5062 -- already installed then we install it.
5064 if not Analyzed
(P_Unit
) then
5065 if not In_Chain
(P
) then
5067 -- Minimum decoration
5069 Set_Ekind
(P
, E_Package
);
5070 Set_Etype
(P
, Standard_Void_Type
);
5071 Set_Scope
(P
, Standard_Standard
);
5072 Set_Is_Visible_Lib_Unit
(P
);
5074 if Is_Child_Package
then
5075 Set_Is_Child_Unit
(P
);
5076 Set_Scope
(P
, Defining_Entity
(Unit
(Parent_Spec
(P_Unit
))));
5079 -- Place entity on visibility structure
5081 Set_Homonym
(P
, Current_Entity
(P
));
5082 Set_Current_Entity
(P
);
5084 if Debug_Flag_I
then
5085 Write_Str
(" (homonym) chain ");
5086 Write_Name
(Chars
(P
));
5090 -- Install the incomplete view. The first element of the limited
5091 -- view is a header (an E_Package entity) used to reference the
5092 -- first shadow entity in the private part of the package.
5094 Lim_Header
:= Limited_View
(P
);
5095 Lim_Typ
:= First_Entity
(Lim_Header
);
5097 while Present
(Lim_Typ
)
5098 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
5100 Set_Homonym
(Lim_Typ
, Current_Entity
(Lim_Typ
));
5101 Set_Current_Entity
(Lim_Typ
);
5103 if Debug_Flag_I
then
5104 Write_Str
(" (homonym) chain ");
5105 Write_Name
(Chars
(Lim_Typ
));
5109 Next_Entity
(Lim_Typ
);
5113 -- If the unit appears in a previous regular with_clause, the regular
5114 -- entities of the public part of the withed package must be replaced
5115 -- by the shadow ones.
5117 -- This code must be kept synchronized with the code that replaces the
5118 -- shadow entities by the real entities (see body of Remove_Limited
5119 -- With_Clause); otherwise the contents of the homonym chains are not
5123 -- Hide all the type entities of the public part of the package to
5124 -- avoid its usage. This is needed to cover all the subtype decla-
5125 -- rations because we do not remove them from the homonym chain.
5127 E
:= First_Entity
(P
);
5128 while Present
(E
) and then E
/= First_Private_Entity
(P
) loop
5130 Set_Was_Hidden
(E
, Is_Hidden
(E
));
5137 -- Replace the real entities by the shadow entities of the limited
5138 -- view. The first element of the limited view is a header that is
5139 -- used to reference the first shadow entity in the private part
5140 -- of the package. Successive elements are the limited views of the
5141 -- type (including regular incomplete types) declared in the package.
5143 Lim_Header
:= Limited_View
(P
);
5145 Lim_Typ
:= First_Entity
(Lim_Header
);
5146 while Present
(Lim_Typ
)
5147 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
5149 pragma Assert
(not In_Chain
(Lim_Typ
));
5151 -- Do not unchain nested packages and child units
5153 if Ekind
(Lim_Typ
) /= E_Package
5154 and then not Is_Child_Unit
(Lim_Typ
)
5160 Prev
:= Current_Entity
(Lim_Typ
);
5163 -- Replace E in the homonyms list, so that the limited view
5164 -- becomes available.
5166 -- If the nonlimited view is a record with an anonymous
5167 -- self-referential component, the analysis of the record
5168 -- declaration creates an incomplete type with the same name
5169 -- in order to define an internal access type. The visible
5170 -- entity is now the incomplete type, and that is the one to
5171 -- replace in the visibility structure.
5173 if E
= Non_Limited_View
(Lim_Typ
)
5175 (Ekind
(E
) = E_Incomplete_Type
5176 and then Full_View
(E
) = Non_Limited_View
(Lim_Typ
))
5178 Set_Homonym
(Lim_Typ
, Homonym
(Prev
));
5179 Set_Current_Entity
(Lim_Typ
);
5183 E
:= Homonym
(Prev
);
5185 -- E may have been removed when installing a previous
5186 -- limited_with_clause.
5189 exit when E
= Non_Limited_View
(Lim_Typ
);
5190 Prev
:= Homonym
(Prev
);
5194 Set_Homonym
(Lim_Typ
, Homonym
(Homonym
(Prev
)));
5195 Set_Homonym
(Prev
, Lim_Typ
);
5200 if Debug_Flag_I
then
5201 Write_Str
(" (homonym) chain ");
5202 Write_Name
(Chars
(Lim_Typ
));
5207 Next_Entity
(Lim_Typ
);
5211 -- The package must be visible while the limited-with clause is active
5212 -- because references to the type P.T must resolve in the usual way.
5213 -- In addition, we remember that the limited-view has been installed to
5214 -- uninstall it at the point of context removal.
5216 Set_Is_Immediately_Visible
(P
);
5217 Set_Limited_View_Installed
(N
);
5219 -- If unit has not been analyzed in some previous context, check
5220 -- (imperfectly ???) whether it might need a body.
5222 if not Analyzed
(P_Unit
) then
5223 Check_Body_Required
;
5226 -- If the package in the limited_with clause is a child unit, the clause
5227 -- is unanalyzed and appears as a selected component. Recast it as an
5228 -- expanded name so that the entity can be properly set. Use entity of
5229 -- parent, if available, for higher ancestors in the name.
5231 if Nkind
(Name
(N
)) = N_Selected_Component
then
5239 while Nkind
(Nam
) = N_Selected_Component
5240 and then Present
(Ent
)
5242 Change_Selected_Component_To_Expanded_Name
(Nam
);
5244 -- Set entity of parent identifiers if the unit is a child
5245 -- unit. This ensures that the tree is properly formed from
5246 -- semantic point of view (e.g. for ASIS queries). The unit
5247 -- entities are not fully analyzed, so we need to follow unit
5248 -- links in the tree.
5250 Set_Entity
(Nam
, Ent
);
5252 Nam
:= Prefix
(Nam
);
5255 (Unit
(Parent_Spec
(Unit_Declaration_Node
(Ent
))));
5257 -- Set entity of last ancestor
5259 if Nkind
(Nam
) = N_Identifier
then
5260 Set_Entity
(Nam
, Ent
);
5266 Set_Entity
(Name
(N
), P
);
5267 Set_From_Limited_With
(P
);
5268 end Install_Limited_With_Clause
;
5270 -------------------------
5271 -- Install_With_Clause --
5272 -------------------------
5274 procedure Install_With_Clause
5275 (With_Clause
: Node_Id
;
5276 Private_With_OK
: Boolean := False)
5278 Uname
: constant Entity_Id
:= Entity
(Name
(With_Clause
));
5279 P
: constant Entity_Id
:= Scope
(Uname
);
5282 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5283 -- compiling a package declaration and the Private_With_OK flag was not
5284 -- set by the caller. These declarations will be installed later (before
5285 -- analyzing the private part of the package).
5287 if Private_Present
(With_Clause
)
5288 and then Nkind
(Unit
(Parent
(With_Clause
))) = N_Package_Declaration
5289 and then not (Private_With_OK
)
5294 if Debug_Flag_I
then
5295 if Private_Present
(With_Clause
) then
5296 Write_Str
("install private withed unit ");
5298 Write_Str
("install withed unit ");
5301 Write_Name
(Chars
(Uname
));
5305 -- We do not apply the restrictions to an internal unit unless we are
5306 -- compiling the internal unit as a main unit. This check is also
5307 -- skipped for dummy units (for missing packages).
5309 if Sloc
(Uname
) /= No_Location
5310 and then (not Is_Internal_Unit
(Current_Sem_Unit
)
5311 or else Current_Sem_Unit
= Main_Unit
)
5313 Check_Restricted_Unit
5314 (Unit_Name
(Get_Source_Unit
(Uname
)), With_Clause
);
5317 if P
/= Standard_Standard
then
5319 -- If the unit is not analyzed after analysis of the with clause and
5320 -- it is an instantiation then it awaits a body and is the main unit.
5321 -- Its appearance in the context of some other unit indicates a
5322 -- circular dependency (DEC suite perversity).
5324 if not Analyzed
(Uname
)
5325 and then Nkind
(Parent
(Uname
)) = N_Package_Instantiation
5328 ("instantiation depends on itself", Name
(With_Clause
));
5330 elsif not Is_Visible_Lib_Unit
(Uname
) then
5332 -- Abandon processing in case of previous errors
5334 if No
(Scope
(Uname
)) then
5335 Check_Error_Detected
;
5339 Set_Is_Visible_Lib_Unit
(Uname
);
5341 -- If the unit is a wrapper package for a compilation unit that is
5342 -- a subprogrm instance, indicate that the instance itself is a
5343 -- visible unit. This is necessary if the instance is inlined.
5345 if Is_Wrapper_Package
(Uname
) then
5346 Set_Is_Visible_Lib_Unit
(Related_Instance
(Uname
));
5349 -- If the child unit appears in the context of its parent, it is
5350 -- immediately visible.
5352 if In_Open_Scopes
(Scope
(Uname
)) then
5353 Set_Is_Immediately_Visible
(Uname
);
5356 if Is_Generic_Instance
(Uname
)
5357 and then Ekind
(Uname
) in Subprogram_Kind
5359 -- Set flag as well on the visible entity that denotes the
5360 -- instance, which renames the current one.
5362 Set_Is_Visible_Lib_Unit
5364 (Defining_Entity
(Unit
(Library_Unit
(With_Clause
)))));
5367 -- The parent unit may have been installed already, and may have
5368 -- appeared in a use clause.
5370 if In_Use
(Scope
(Uname
)) then
5371 Set_Is_Potentially_Use_Visible
(Uname
);
5374 Set_Context_Installed
(With_Clause
);
5377 elsif not Is_Immediately_Visible
(Uname
) then
5378 Set_Is_Visible_Lib_Unit
(Uname
);
5380 if not Private_Present
(With_Clause
) or else Private_With_OK
then
5381 Set_Is_Immediately_Visible
(Uname
);
5384 Set_Context_Installed
(With_Clause
);
5387 -- A [private] with clause overrides a limited with clause. Restore the
5388 -- proper view of the package by performing the following actions:
5390 -- * Remove all shadow entities which hide their corresponding
5391 -- entities from direct visibility by updating the entity and
5394 -- * Enter the corresponding entities back in direct visibility
5396 -- Note that the original limited with clause which installed its view
5397 -- is still marked as "active". This effect is undone when the clause
5398 -- itself is removed, see Remove_Limited_With_Clause.
5400 if Ekind
(Uname
) = E_Package
and then From_Limited_With
(Uname
) then
5401 Remove_Limited_With_Unit
(Unit_Declaration_Node
(Uname
));
5404 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5405 -- unit if there is a visible homograph for it declared in the same
5406 -- declarative region. This pathological case can only arise when an
5407 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5408 -- G1 has a generic child also named G2, and the context includes with_
5409 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5410 -- of I1.G2 visible as well. If the child unit is named Standard, do
5411 -- not apply the check to the Standard package itself.
5413 if Is_Child_Unit
(Uname
)
5414 and then Is_Visible_Lib_Unit
(Uname
)
5415 and then Ada_Version
>= Ada_2005
5418 Decl1
: constant Node_Id
:= Unit_Declaration_Node
(P
);
5424 U2
:= Homonym
(Uname
);
5425 while Present
(U2
) and then U2
/= Standard_Standard
loop
5427 Decl2
:= Unit_Declaration_Node
(P2
);
5429 if Is_Child_Unit
(U2
) and then Is_Visible_Lib_Unit
(U2
) then
5430 if Is_Generic_Instance
(P
)
5431 and then Nkind
(Decl1
) = N_Package_Declaration
5432 and then Generic_Parent
(Specification
(Decl1
)) = P2
5434 Error_Msg_N
("illegal with_clause", With_Clause
);
5436 ("\child unit has visible homograph" &
5437 " (RM 8.3(26), 10.1.1(19))",
5441 elsif Is_Generic_Instance
(P2
)
5442 and then Nkind
(Decl2
) = N_Package_Declaration
5443 and then Generic_Parent
(Specification
(Decl2
)) = P
5445 -- With_clause for child unit of instance appears before
5446 -- in the context. We want to place the error message on
5447 -- it, not on the generic child unit itself.
5450 Prev_Clause
: Node_Id
;
5453 Prev_Clause
:= First
(List_Containing
(With_Clause
));
5454 while Entity
(Name
(Prev_Clause
)) /= U2
loop
5458 pragma Assert
(Present
(Prev_Clause
));
5459 Error_Msg_N
("illegal with_clause", Prev_Clause
);
5461 ("\child unit has visible homograph" &
5462 " (RM 8.3(26), 10.1.1(19))",
5473 end Install_With_Clause
;
5479 function Is_Child_Spec
(Lib_Unit
: Node_Id
) return Boolean is
5480 K
: constant Node_Kind
:= Nkind
(Lib_Unit
);
5483 return (K
in N_Generic_Declaration
or else
5484 K
in N_Generic_Instantiation
or else
5485 K
in N_Generic_Renaming_Declaration
or else
5486 K
= N_Package_Declaration
or else
5487 K
= N_Package_Renaming_Declaration
or else
5488 K
= N_Subprogram_Declaration
or else
5489 K
= N_Subprogram_Renaming_Declaration
)
5490 and then Present
(Parent_Spec
(Lib_Unit
));
5493 ------------------------------------
5494 -- Is_Legal_Shadow_Entity_In_Body --
5495 ------------------------------------
5497 function Is_Legal_Shadow_Entity_In_Body
(T
: Entity_Id
) return Boolean is
5498 C_Unit
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
5500 return Nkind
(Unit
(C_Unit
)) = N_Package_Body
5503 (C_Unit
, Cunit_Entity
(Get_Source_Unit
(Non_Limited_View
(T
))));
5504 end Is_Legal_Shadow_Entity_In_Body
;
5506 ----------------------
5507 -- Is_Ancestor_Unit --
5508 ----------------------
5510 function Is_Ancestor_Unit
(U1
: Node_Id
; U2
: Node_Id
) return Boolean is
5511 E1
: constant Entity_Id
:= Defining_Entity
(Unit
(U1
));
5514 if Nkind_In
(Unit
(U2
), N_Package_Body
, N_Subprogram_Body
) then
5515 E2
:= Defining_Entity
(Unit
(Library_Unit
(U2
)));
5516 return Is_Ancestor_Package
(E1
, E2
);
5520 end Is_Ancestor_Unit
;
5522 -----------------------
5523 -- Load_Needed_Body --
5524 -----------------------
5526 -- N is a generic unit named in a with clause, or else it is a unit that
5527 -- contains a generic unit or an inlined function. In order to perform an
5528 -- instantiation, the body of the unit must be present. If the unit itself
5529 -- is generic, we assume that an instantiation follows, and load & analyze
5530 -- the body unconditionally. This forces analysis of the spec as well.
5532 -- If the unit is not generic, but contains a generic unit, it is loaded on
5533 -- demand, at the point of instantiation (see ch12).
5535 procedure Load_Needed_Body
5538 Do_Analyze
: Boolean := True)
5540 Body_Name
: Unit_Name_Type
;
5541 Unum
: Unit_Number_Type
;
5543 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
5544 -- The loading and analysis is done with style checks off
5547 if not GNAT_Mode
then
5548 Style_Check
:= False;
5551 Body_Name
:= Get_Body_Name
(Get_Unit_Name
(Unit
(N
)));
5554 (Load_Name
=> Body_Name
,
5560 if Unum
= No_Unit
then
5564 Compiler_State
:= Analyzing
; -- reset after load
5566 if Fatal_Error
(Unum
) /= Error_Detected
or else Try_Semantics
then
5567 if Debug_Flag_L
then
5568 Write_Str
("*** Loaded generic body");
5573 Semantics
(Cunit
(Unum
));
5580 Style_Check
:= Save_Style_Check
;
5581 end Load_Needed_Body
;
5583 -------------------------
5584 -- Build_Limited_Views --
5585 -------------------------
5587 procedure Build_Limited_Views
(N
: Node_Id
) is
5588 Unum
: constant Unit_Number_Type
:=
5589 Get_Source_Unit
(Library_Unit
(N
));
5590 Is_Analyzed
: constant Boolean := Analyzed
(Cunit
(Unum
));
5592 Shadow_Pack
: Entity_Id
;
5593 -- The corresponding shadow entity of the withed package. This entity
5594 -- offers incomplete views of packages and types as well as abstract
5595 -- views of states and variables declared within.
5597 Last_Shadow
: Entity_Id
:= Empty
;
5598 -- The last shadow entity created by routine Build_Shadow_Entity
5600 procedure Build_Shadow_Entity
5603 Shadow
: out Entity_Id
;
5604 Is_Tagged
: Boolean := False);
5605 -- Create a shadow entity that hides Ent and offers an abstract or
5606 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5607 -- should be set when Ent is a tagged type. The generated entity is
5608 -- added to Lim_Header. This routine updates the value of Last_Shadow.
5610 procedure Decorate_Package
(Ent
: Entity_Id
; Scop
: Entity_Id
);
5611 -- Perform minimal decoration of a package or its corresponding shadow
5612 -- entity denoted by Ent. Scop is the proper scope.
5614 procedure Decorate_State
(Ent
: Entity_Id
; Scop
: Entity_Id
);
5615 -- Perform full decoration of an abstract state or its corresponding
5616 -- shadow entity denoted by Ent. Scop is the proper scope.
5618 procedure Decorate_Type
5621 Is_Tagged
: Boolean := False;
5622 Materialize
: Boolean := False);
5623 -- Perform minimal decoration of a type or its corresponding shadow
5624 -- entity denoted by Ent. Scop is the proper scope. Flag Is_Tagged
5625 -- should be set when Ent is a tagged type. Flag Materialize should be
5626 -- set when Ent is a tagged type and its class-wide type needs to appear
5629 procedure Decorate_Variable
(Ent
: Entity_Id
; Scop
: Entity_Id
);
5630 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5633 procedure Process_Declarations_And_States
5637 Create_Abstract_Views
: Boolean);
5638 -- Inspect the states of package Pack and declarative list Decls. Create
5639 -- shadow entities for all nested packages, states, types and variables
5640 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5641 -- be set when the abstract states and variables need to be processed.
5643 -------------------------
5644 -- Build_Shadow_Entity --
5645 -------------------------
5647 procedure Build_Shadow_Entity
5650 Shadow
: out Entity_Id
;
5651 Is_Tagged
: Boolean := False)
5654 Shadow
:= Make_Temporary
(Sloc
(Ent
), 'Z');
5656 -- The shadow entity must share the same name and parent as the
5659 Set_Chars
(Shadow
, Chars
(Ent
));
5660 Set_Parent
(Shadow
, Parent
(Ent
));
5662 -- The abstract view of a variable is a state, not another variable
5664 if Ekind
(Ent
) = E_Variable
then
5665 Set_Ekind
(Shadow
, E_Abstract_State
);
5667 Set_Ekind
(Shadow
, Ekind
(Ent
));
5670 Set_Is_Internal
(Shadow
);
5671 Set_From_Limited_With
(Shadow
);
5673 -- Add the new shadow entity to the limited view of the package
5675 Last_Shadow
:= Shadow
;
5676 Append_Entity
(Shadow
, Shadow_Pack
);
5678 -- Perform context-specific decoration of the shadow entity
5680 if Ekind
(Ent
) = E_Abstract_State
then
5681 Decorate_State
(Shadow
, Scop
);
5682 Set_Non_Limited_View
(Shadow
, Ent
);
5684 elsif Ekind
(Ent
) = E_Package
then
5685 Decorate_Package
(Shadow
, Scop
);
5687 elsif Is_Type
(Ent
) then
5688 Decorate_Type
(Shadow
, Scop
, Is_Tagged
);
5689 Set_Non_Limited_View
(Shadow
, Ent
);
5692 Set_Non_Limited_View
5693 (Class_Wide_Type
(Shadow
), Class_Wide_Type
(Ent
));
5696 if Is_Incomplete_Or_Private_Type
(Ent
) then
5697 Set_Private_Dependents
(Shadow
, New_Elmt_List
);
5700 elsif Ekind
(Ent
) = E_Variable
then
5701 Decorate_State
(Shadow
, Scop
);
5702 Set_Non_Limited_View
(Shadow
, Ent
);
5704 end Build_Shadow_Entity
;
5706 ----------------------
5707 -- Decorate_Package --
5708 ----------------------
5710 procedure Decorate_Package
(Ent
: Entity_Id
; Scop
: Entity_Id
) is
5712 Set_Ekind
(Ent
, E_Package
);
5713 Set_Etype
(Ent
, Standard_Void_Type
);
5714 Set_Scope
(Ent
, Scop
);
5715 end Decorate_Package
;
5717 --------------------
5718 -- Decorate_State --
5719 --------------------
5721 procedure Decorate_State
(Ent
: Entity_Id
; Scop
: Entity_Id
) is
5723 Set_Ekind
(Ent
, E_Abstract_State
);
5724 Set_Etype
(Ent
, Standard_Void_Type
);
5725 Set_Scope
(Ent
, Scop
);
5726 Set_Encapsulating_State
(Ent
, Empty
);
5733 procedure Decorate_Type
5736 Is_Tagged
: Boolean := False;
5737 Materialize
: Boolean := False)
5742 -- An unanalyzed type or a shadow entity of a type is treated as an
5743 -- incomplete type, and carries the corresponding attributes.
5745 Set_Ekind
(Ent
, E_Incomplete_Type
);
5746 Set_Etype
(Ent
, Ent
);
5747 Set_Full_View
(Ent
, Empty
);
5748 Set_Is_First_Subtype
(Ent
);
5749 Set_Scope
(Ent
, Scop
);
5750 Set_Stored_Constraint
(Ent
, No_Elist
);
5751 Init_Size_Align
(Ent
);
5753 if From_Limited_With
(Ent
) then
5754 Set_Private_Dependents
(Ent
, New_Elmt_List
);
5757 -- A tagged type and its corresponding shadow entity share one common
5758 -- class-wide type. The list of primitive operations for the shadow
5762 Set_Is_Tagged_Type
(Ent
);
5763 Set_Direct_Primitive_Operations
(Ent
, New_Elmt_List
);
5767 (E_Void
, Scope
(Ent
), Sloc
(Ent
), Ent
, 'C', 0, 'T');
5769 Set_Class_Wide_Type
(Ent
, CW_Typ
);
5771 -- Set parent to be the same as the parent of the tagged type.
5772 -- We need a parent field set, and it is supposed to point to
5773 -- the declaration of the type. The tagged type declaration
5774 -- essentially declares two separate types, the tagged type
5775 -- itself and the corresponding class-wide type, so it is
5776 -- reasonable for the parent fields to point to the declaration
5779 Set_Parent
(CW_Typ
, Parent
(Ent
));
5781 Set_Ekind
(CW_Typ
, E_Class_Wide_Type
);
5782 Set_Class_Wide_Type
(CW_Typ
, CW_Typ
);
5783 Set_Etype
(CW_Typ
, Ent
);
5784 Set_Equivalent_Type
(CW_Typ
, Empty
);
5785 Set_From_Limited_With
(CW_Typ
, From_Limited_With
(Ent
));
5786 Set_Has_Unknown_Discriminants
(CW_Typ
);
5787 Set_Is_First_Subtype
(CW_Typ
);
5788 Set_Is_Tagged_Type
(CW_Typ
);
5789 Set_Materialize_Entity
(CW_Typ
, Materialize
);
5790 Set_Scope
(CW_Typ
, Scop
);
5791 Init_Size_Align
(CW_Typ
);
5795 -----------------------
5796 -- Decorate_Variable --
5797 -----------------------
5799 procedure Decorate_Variable
(Ent
: Entity_Id
; Scop
: Entity_Id
) is
5801 Set_Ekind
(Ent
, E_Variable
);
5802 Set_Etype
(Ent
, Standard_Void_Type
);
5803 Set_Scope
(Ent
, Scop
);
5804 end Decorate_Variable
;
5806 -------------------------------------
5807 -- Process_Declarations_And_States --
5808 -------------------------------------
5810 procedure Process_Declarations_And_States
5814 Create_Abstract_Views
: Boolean)
5816 procedure Find_And_Process_States
;
5817 -- Determine whether package Pack defines abstract state either by
5818 -- using an aspect or a pragma. If this is the case, build shadow
5819 -- entities for all abstract states of Pack.
5821 procedure Process_States
(States
: Elist_Id
);
5822 -- Generate shadow entities for all abstract states in list States
5824 -----------------------------
5825 -- Find_And_Process_States --
5826 -----------------------------
5828 procedure Find_And_Process_States
is
5829 procedure Process_State
(State
: Node_Id
);
5830 -- Generate shadow entities for a single abstract state or
5831 -- multiple states expressed as an aggregate.
5837 procedure Process_State
(State
: Node_Id
) is
5838 Loc
: constant Source_Ptr
:= Sloc
(State
);
5845 -- Multiple abstract states appear as an aggregate
5847 if Nkind
(State
) = N_Aggregate
then
5848 Elmt
:= First
(Expressions
(State
));
5849 while Present
(Elmt
) loop
5850 Process_State
(Elmt
);
5856 -- A null state has no abstract view
5858 elsif Nkind
(State
) = N_Null
then
5861 -- State declaration with various options appears as an
5862 -- extension aggregate.
5864 elsif Nkind
(State
) = N_Extension_Aggregate
then
5865 Decl
:= Ancestor_Part
(State
);
5867 -- Simple state declaration
5869 elsif Nkind
(State
) = N_Identifier
then
5872 -- Possibly an illegal state declaration
5878 -- Abstract states are elaborated when the related pragma is
5879 -- elaborated. Since the withed package is not analyzed yet,
5880 -- the entities of the abstract states are not available. To
5881 -- overcome this complication, create the entities now and
5882 -- store them in their respective declarations. The entities
5883 -- are later used by routine Create_Abstract_State to declare
5884 -- and enter the states into visibility.
5886 if No
(Entity
(Decl
)) then
5887 Id
:= Make_Defining_Identifier
(Loc
, Chars
(Decl
));
5889 Set_Entity
(Decl
, Id
);
5890 Set_Parent
(Id
, State
);
5891 Decorate_State
(Id
, Scop
);
5893 -- Otherwise the package was previously withed
5896 Id
:= Entity
(Decl
);
5899 Build_Shadow_Entity
(Id
, Scop
, Dummy
);
5904 Pack_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Pack
);
5908 -- Start of processing for Find_And_Process_States
5911 -- Find aspect Abstract_State
5913 Asp
:= First
(Aspect_Specifications
(Pack_Decl
));
5914 while Present
(Asp
) loop
5915 if Chars
(Identifier
(Asp
)) = Name_Abstract_State
then
5916 Process_State
(Expression
(Asp
));
5924 -- Find pragma Abstract_State by inspecting the declarations
5926 Decl
:= First
(Decls
);
5927 while Present
(Decl
) and then Nkind
(Decl
) = N_Pragma
loop
5928 if Pragma_Name
(Decl
) = Name_Abstract_State
then
5931 (First
(Pragma_Argument_Associations
(Decl
))));
5938 end Find_And_Process_States
;
5940 --------------------
5941 -- Process_States --
5942 --------------------
5944 procedure Process_States
(States
: Elist_Id
) is
5949 Elmt
:= First_Elmt
(States
);
5950 while Present
(Elmt
) loop
5951 Build_Shadow_Entity
(Node
(Elmt
), Scop
, Dummy
);
5959 Is_Tagged
: Boolean;
5965 -- Start of processing for Process_Declarations_And_States
5968 -- Build abstract views for all states defined in the package
5970 if Create_Abstract_Views
then
5972 -- When a package has been analyzed, all states are stored in list
5973 -- Abstract_States. Generate the shadow entities directly.
5976 if Present
(Abstract_States
(Pack
)) then
5977 Process_States
(Abstract_States
(Pack
));
5980 -- The package may declare abstract states by using an aspect or a
5981 -- pragma. Attempt to locate one of these construct and if found,
5982 -- build the shadow entities.
5985 Find_And_Process_States
;
5989 -- Inspect the declarative list, looking for nested packages, types
5990 -- and variable declarations.
5992 Decl
:= First
(Decls
);
5993 while Present
(Decl
) loop
5997 if Nkind
(Decl
) = N_Package_Declaration
then
5998 Def_Id
:= Defining_Entity
(Decl
);
6000 -- Perform minor decoration when the withed package has not
6003 if not Is_Analyzed
then
6004 Decorate_Package
(Def_Id
, Scop
);
6007 -- Create a shadow entity that offers a limited view of all
6008 -- visible types declared within.
6010 Build_Shadow_Entity
(Def_Id
, Scop
, Shadow
);
6012 Process_Declarations_And_States
6015 Visible_Declarations
(Specification
(Decl
)),
6017 Create_Abstract_Views
=> Create_Abstract_Views
);
6021 elsif Nkind_In
(Decl
, N_Full_Type_Declaration
,
6022 N_Incomplete_Type_Declaration
,
6023 N_Private_Extension_Declaration
,
6024 N_Private_Type_Declaration
,
6025 N_Protected_Type_Declaration
,
6026 N_Task_Type_Declaration
)
6028 Def_Id
:= Defining_Entity
(Decl
);
6030 -- Determine whether the type is tagged. Note that packages
6031 -- included via a limited with clause are not always analyzed,
6032 -- hence the tree lookup rather than the use of attribute
6035 if Nkind
(Decl
) = N_Full_Type_Declaration
then
6036 Def
:= Type_Definition
(Decl
);
6039 (Nkind
(Def
) = N_Record_Definition
6040 and then Tagged_Present
(Def
))
6042 (Nkind
(Def
) = N_Derived_Type_Definition
6043 and then Present
(Record_Extension_Part
(Def
)));
6045 elsif Nkind_In
(Decl
, N_Incomplete_Type_Declaration
,
6046 N_Private_Type_Declaration
)
6048 Is_Tagged
:= Tagged_Present
(Decl
);
6050 elsif Nkind
(Decl
) = N_Private_Extension_Declaration
then
6057 -- Perform minor decoration when the withed package has not
6060 if not Is_Analyzed
then
6061 Decorate_Type
(Def_Id
, Scop
, Is_Tagged
, True);
6064 -- Create a shadow entity that hides the type and offers an
6065 -- incomplete view of the said type.
6067 Build_Shadow_Entity
(Def_Id
, Scop
, Shadow
, Is_Tagged
);
6071 elsif Create_Abstract_Views
6072 and then Nkind
(Decl
) = N_Object_Declaration
6073 and then not Constant_Present
(Decl
)
6075 Def_Id
:= Defining_Entity
(Decl
);
6077 -- Perform minor decoration when the withed package has not
6080 if not Is_Analyzed
then
6081 Decorate_Variable
(Def_Id
, Scop
);
6084 -- Create a shadow entity that hides the variable and offers an
6085 -- abstract view of the said variable.
6087 Build_Shadow_Entity
(Def_Id
, Scop
, Shadow
);
6092 end Process_Declarations_And_States
;
6096 Nam
: constant Node_Id
:= Name
(N
);
6097 Pack
: constant Entity_Id
:= Cunit_Entity
(Unum
);
6099 Last_Public_Shadow
: Entity_Id
:= Empty
;
6100 Private_Shadow
: Entity_Id
;
6103 -- Start of processing for Build_Limited_Views
6106 pragma Assert
(Limited_Present
(N
));
6108 -- A library_item mentioned in a limited_with_clause is a package
6109 -- declaration, not a subprogram declaration, generic declaration,
6110 -- generic instantiation, or package renaming declaration.
6112 case Nkind
(Unit
(Library_Unit
(N
))) is
6113 when N_Package_Declaration
=>
6116 when N_Subprogram_Declaration
=>
6117 Error_Msg_N
("subprograms not allowed in limited with_clauses", N
);
6120 when N_Generic_Package_Declaration
6121 | N_Generic_Subprogram_Declaration
6123 Error_Msg_N
("generics not allowed in limited with_clauses", N
);
6126 when N_Generic_Instantiation
=>
6128 ("generic instantiations not allowed in limited with_clauses",
6132 when N_Generic_Renaming_Declaration
=>
6134 ("generic renamings not allowed in limited with_clauses", N
);
6137 when N_Subprogram_Renaming_Declaration
=>
6139 ("renamed subprograms not allowed in limited with_clauses", N
);
6142 when N_Package_Renaming_Declaration
=>
6144 ("renamed packages not allowed in limited with_clauses", N
);
6148 raise Program_Error
;
6151 -- The withed unit may not be analyzed, but the with calause itself
6152 -- must be minimally decorated. This ensures that the checks on unused
6153 -- with clauses also process limieted withs.
6155 Set_Ekind
(Pack
, E_Package
);
6156 Set_Etype
(Pack
, Standard_Void_Type
);
6158 if Is_Entity_Name
(Nam
) then
6159 Set_Entity
(Nam
, Pack
);
6161 elsif Nkind
(Nam
) = N_Selected_Component
then
6162 Set_Entity
(Selector_Name
(Nam
), Pack
);
6165 -- Check if the chain is already built
6167 Spec
:= Specification
(Unit
(Library_Unit
(N
)));
6169 if Limited_View_Installed
(Spec
) then
6173 -- Create the shadow package wich hides the withed unit and provides
6174 -- incomplete view of all types and packages declared within.
6176 Shadow_Pack
:= Make_Temporary
(Sloc
(N
), 'Z');
6177 Set_Ekind
(Shadow_Pack
, E_Package
);
6178 Set_Is_Internal
(Shadow_Pack
);
6179 Set_Limited_View
(Pack
, Shadow_Pack
);
6181 -- Inspect the abstract states and visible declarations of the withed
6182 -- unit and create shadow entities that hide existing packages, states,
6183 -- variables and types.
6185 Process_Declarations_And_States
6187 Decls
=> Visible_Declarations
(Spec
),
6189 Create_Abstract_Views
=> True);
6191 Last_Public_Shadow
:= Last_Shadow
;
6193 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6194 -- to accommodate limited private with clauses.
6196 Process_Declarations_And_States
6198 Decls
=> Private_Declarations
(Spec
),
6200 Create_Abstract_Views
=> False);
6202 if Present
(Last_Public_Shadow
) then
6203 Private_Shadow
:= Next_Entity
(Last_Public_Shadow
);
6205 Private_Shadow
:= First_Entity
(Shadow_Pack
);
6208 Set_First_Private_Entity
(Shadow_Pack
, Private_Shadow
);
6209 Set_Limited_View_Installed
(Spec
);
6210 end Build_Limited_Views
;
6212 ----------------------------
6213 -- Check_No_Elab_Code_All --
6214 ----------------------------
6216 procedure Check_No_Elab_Code_All
(N
: Node_Id
) is
6218 if Present
(No_Elab_Code_All_Pragma
)
6219 and then In_Extended_Main_Source_Unit
(N
)
6220 and then Present
(Context_Items
(N
))
6223 CL
: constant List_Id
:= Context_Items
(N
);
6228 while Present
(CI
) loop
6229 if Nkind
(CI
) = N_With_Clause
6231 No_Elab_Code_All
(Get_Source_Unit
(Library_Unit
(CI
)))
6233 -- In GNATprove mode, some runtime units are implicitly
6234 -- loaded to make their entities available for analysis. In
6235 -- this case, ignore violations of No_Elaboration_Code_All
6236 -- for this special analysis mode.
6239 (GNATprove_Mode
and then Implicit_With
(CI
))
6241 Error_Msg_Sloc
:= Sloc
(No_Elab_Code_All_Pragma
);
6243 ("violation of No_Elaboration_Code_All#", CI
);
6245 ("\unit& does not have No_Elaboration_Code_All",
6246 CI
, Entity
(Name
(CI
)));
6253 end Check_No_Elab_Code_All
;
6255 -------------------------------
6256 -- Check_Body_Needed_For_SAL --
6257 -------------------------------
6259 procedure Check_Body_Needed_For_SAL
(Unit_Name
: Entity_Id
) is
6260 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean;
6261 -- Determine whether use of entity E might require the presence of its
6262 -- body. For a package this requires a recursive traversal of all nested
6265 -----------------------
6266 -- Entity_Needs_Body --
6267 -----------------------
6269 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean is
6273 if Is_Subprogram
(E
) and then Has_Pragma_Inline
(E
) then
6276 elsif Ekind_In
(E
, E_Generic_Function
, E_Generic_Procedure
) then
6278 -- A generic subprogram always requires the presence of its
6279 -- body because an instantiation needs both templates. The only
6280 -- exceptions is a generic subprogram renaming. In this case the
6281 -- body is needed only when the template is declared outside the
6282 -- compilation unit being checked.
6284 if Present
(Renamed_Entity
(E
)) then
6285 return not Within_Scope
(E
, Unit_Name
);
6290 elsif Ekind
(E
) = E_Generic_Package
6292 Nkind
(Unit_Declaration_Node
(E
)) = N_Generic_Package_Declaration
6293 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
6297 elsif Ekind
(E
) = E_Package
6298 and then Nkind
(Unit_Declaration_Node
(E
)) = N_Package_Declaration
6299 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
6301 Ent
:= First_Entity
(E
);
6302 while Present
(Ent
) loop
6303 if Entity_Needs_Body
(Ent
) then
6315 end Entity_Needs_Body
;
6317 -- Start of processing for Check_Body_Needed_For_SAL
6320 if Ekind
(Unit_Name
) = E_Generic_Package
6321 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
6322 N_Generic_Package_Declaration
6324 Present
(Corresponding_Body
(Unit_Declaration_Node
(Unit_Name
)))
6326 Set_Body_Needed_For_SAL
(Unit_Name
);
6328 elsif Ekind_In
(Unit_Name
, E_Generic_Procedure
, E_Generic_Function
) then
6329 Set_Body_Needed_For_SAL
(Unit_Name
);
6331 elsif Is_Subprogram
(Unit_Name
)
6332 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
6333 N_Subprogram_Declaration
6334 and then Has_Pragma_Inline
(Unit_Name
)
6336 Set_Body_Needed_For_SAL
(Unit_Name
);
6338 elsif Ekind
(Unit_Name
) = E_Subprogram_Body
then
6339 Check_Body_Needed_For_SAL
6340 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
6342 elsif Ekind
(Unit_Name
) = E_Package
6343 and then Entity_Needs_Body
(Unit_Name
)
6345 Set_Body_Needed_For_SAL
(Unit_Name
);
6347 elsif Ekind
(Unit_Name
) = E_Package_Body
6348 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) = N_Package_Body
6350 Check_Body_Needed_For_SAL
6351 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
6353 end Check_Body_Needed_For_SAL
;
6355 --------------------
6356 -- Remove_Context --
6357 --------------------
6359 procedure Remove_Context
(N
: Node_Id
) is
6360 Lib_Unit
: constant Node_Id
:= Unit
(N
);
6363 -- If this is a child unit, first remove the parent units
6365 if Is_Child_Spec
(Lib_Unit
) then
6366 Remove_Parents
(Lib_Unit
);
6369 Remove_Context_Clauses
(N
);
6372 ----------------------------
6373 -- Remove_Context_Clauses --
6374 ----------------------------
6376 procedure Remove_Context_Clauses
(N
: Node_Id
) is
6378 Unit_Name
: Entity_Id
;
6381 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6382 -- limited-views first and regular-views later (to maintain the
6385 -- First Phase: Remove limited_with context clauses
6387 Item
:= First
(Context_Items
(N
));
6388 while Present
(Item
) loop
6390 -- We are interested only in with clauses which got installed
6393 if Nkind
(Item
) = N_With_Clause
6394 and then Limited_Present
(Item
)
6395 and then Limited_View_Installed
(Item
)
6397 Remove_Limited_With_Clause
(Item
);
6403 -- Second Phase: Loop through context items and undo regular
6404 -- with_clauses and use_clauses.
6406 Item
:= First
(Context_Items
(N
));
6407 while Present
(Item
) loop
6409 -- We are interested only in with clauses which got installed on
6410 -- entry, as indicated by their Context_Installed flag set
6412 if Nkind
(Item
) = N_With_Clause
6413 and then Limited_Present
(Item
)
6414 and then Limited_View_Installed
(Item
)
6418 elsif Nkind
(Item
) = N_With_Clause
6419 and then Context_Installed
(Item
)
6421 -- Remove items from one with'ed unit
6423 Unit_Name
:= Entity
(Name
(Item
));
6424 Remove_Unit_From_Visibility
(Unit_Name
);
6425 Set_Context_Installed
(Item
, False);
6427 elsif Nkind
(Item
) = N_Use_Package_Clause
then
6428 End_Use_Package
(Item
);
6430 elsif Nkind
(Item
) = N_Use_Type_Clause
then
6431 End_Use_Type
(Item
);
6436 end Remove_Context_Clauses
;
6438 --------------------------------
6439 -- Remove_Limited_With_Clause --
6440 --------------------------------
6442 procedure Remove_Limited_With_Clause
(N
: Node_Id
) is
6443 Pack_Decl
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
6446 pragma Assert
(Limited_View_Installed
(N
));
6448 -- Limited with clauses that designate units other than packages are
6449 -- illegal and are never installed.
6451 if Nkind
(Pack_Decl
) = N_Package_Declaration
then
6452 Remove_Limited_With_Unit
(Pack_Decl
, N
);
6455 -- Indicate that the limited views of the clause have been removed
6457 Set_Limited_View_Installed
(N
, False);
6458 end Remove_Limited_With_Clause
;
6460 ------------------------------
6461 -- Remove_Limited_With_Unit --
6462 ------------------------------
6464 procedure Remove_Limited_With_Unit
6465 (Pack_Decl
: Node_Id
;
6466 Lim_Clause
: Node_Id
:= Empty
)
6468 procedure Remove_Shadow_Entities_From_Visibility
(Pack_Id
: Entity_Id
);
6469 -- Remove the shadow entities of package Pack_Id from direct visibility
6471 procedure Remove_Shadow_Entities_With_Restore
(Pack_Id
: Entity_Id
);
6472 -- Remove the shadow entities of package Pack_Id from direct visibility,
6473 -- restore the corresponding entities they hide into direct visibility,
6474 -- and update the entity and homonym chains.
6476 --------------------------------------------
6477 -- Remove_Shadow_Entities_From_Visibility --
6478 --------------------------------------------
6480 procedure Remove_Shadow_Entities_From_Visibility
(Pack_Id
: Entity_Id
) is
6481 Lim_Header
: constant Entity_Id
:= Limited_View
(Pack_Id
);
6482 Upto
: constant Entity_Id
:= First_Private_Entity
(Lim_Header
);
6487 -- Remove the package from direct visibility
6490 Set_Is_Immediately_Visible
(Pack_Id
, False);
6492 -- Remove all shadow entities from direct visibility
6494 Shadow
:= First_Entity
(Lim_Header
);
6495 while Present
(Shadow
) and then Shadow
/= Upto
loop
6497 Next_Entity
(Shadow
);
6499 end Remove_Shadow_Entities_From_Visibility
;
6501 -----------------------------------------
6502 -- Remove_Shadow_Entities_With_Restore --
6503 -----------------------------------------
6505 procedure Remove_Shadow_Entities_With_Restore
(Pack_Id
: Entity_Id
) is
6506 procedure Restore_Chain_For_Shadow
(Shadow
: Entity_Id
);
6507 -- Remove shadow entity Shadow by updating the entity and homonym
6510 procedure Restore_Chains
6513 -- Remove a sequence of shadow entities starting from From and ending
6514 -- prior to Upto by updating the entity and homonym chains.
6516 procedure Restore_Type_Visibility
6519 -- Restore a sequence of types starting from From and ending prior to
6520 -- Upto back in direct visibility.
6522 ------------------------------
6523 -- Restore_Chain_For_Shadow --
6524 ------------------------------
6526 procedure Restore_Chain_For_Shadow
(Shadow
: Entity_Id
) is
6531 -- If the package has incomplete types, the limited view of the
6532 -- incomplete type is in fact never visible (AI05-129) but we
6533 -- have created a shadow entity E1 for it, that points to E2,
6534 -- a nonlimited incomplete type. This in turn has a full view
6535 -- E3 that is the full declaration. There is a corresponding
6536 -- shadow entity E4. When reinstalling the nonlimited view,
6537 -- E2 must become the current entity and E3 must be ignored.
6539 Typ
:= Non_Limited_View
(Shadow
);
6541 -- Shadow is the limited view of a full type declaration that has
6542 -- a previous incomplete declaration, i.e. E3 from the previous
6543 -- description. Nothing to insert.
6545 if Present
(Current_Entity
(Typ
))
6546 and then Ekind
(Current_Entity
(Typ
)) = E_Incomplete_Type
6547 and then Full_View
(Current_Entity
(Typ
)) = Typ
6552 pragma Assert
(not In_Chain
(Typ
));
6554 Prev
:= Current_Entity
(Shadow
);
6556 if Prev
= Shadow
then
6557 Set_Current_Entity
(Typ
);
6560 while Present
(Prev
) and then Homonym
(Prev
) /= Shadow
loop
6561 Prev
:= Homonym
(Prev
);
6564 if Present
(Prev
) then
6565 Set_Homonym
(Prev
, Typ
);
6569 Set_Homonym
(Typ
, Homonym
(Shadow
));
6570 end Restore_Chain_For_Shadow
;
6572 --------------------
6573 -- Restore_Chains --
6574 --------------------
6576 procedure Restore_Chains
6584 while Present
(Shadow
) and then Shadow
/= Upto
loop
6586 -- Do not unchain nested packages and child units
6588 if Ekind
(Shadow
) = E_Package
then
6591 elsif Is_Child_Unit
(Non_Limited_View
(Shadow
)) then
6595 Restore_Chain_For_Shadow
(Shadow
);
6598 Next_Entity
(Shadow
);
6602 -----------------------------
6603 -- Restore_Type_Visibility --
6604 -----------------------------
6606 procedure Restore_Type_Visibility
6614 while Present
(Typ
) and then Typ
/= Upto
loop
6615 if Is_Type
(Typ
) then
6616 Set_Is_Hidden
(Typ
, Was_Hidden
(Typ
));
6621 end Restore_Type_Visibility
;
6625 Lim_Header
: constant Entity_Id
:= Limited_View
(Pack_Id
);
6627 -- Start of processing Remove_Shadow_Entities_With_Restore
6630 -- The limited view of a package is being uninstalled by removing
6631 -- the effects of a limited with clause. If the clause appears in a
6632 -- unit which is not part of the main unit closure, then the related
6633 -- package must not be visible.
6635 if Present
(Lim_Clause
)
6636 and then not In_Extended_Main_Source_Unit
(Lim_Clause
)
6638 Set_Is_Immediately_Visible
(Pack_Id
, False);
6640 -- Otherwise a limited view is being overridden by a nonlimited view.
6641 -- Leave the visibility of the package as is because the unit must be
6642 -- visible when the nonlimited view is installed.
6648 -- Remove the shadow entities from visibility by updating the entity
6649 -- and homonym chains.
6652 (From
=> First_Entity
(Lim_Header
),
6653 Upto
=> First_Private_Entity
(Lim_Header
));
6655 -- Reinstate the types that were hidden by the shadow entities back
6656 -- into direct visibility.
6658 Restore_Type_Visibility
6659 (From
=> First_Entity
(Pack_Id
),
6660 Upto
=> First_Private_Entity
(Pack_Id
));
6661 end Remove_Shadow_Entities_With_Restore
;
6665 Pack_Id
: constant Entity_Id
:= Defining_Entity
(Pack_Decl
);
6667 -- Start of processing for Remove_Limited_With_Unit
6670 -- Nothing to do when the limited view of the package is not installed
6672 if not From_Limited_With
(Pack_Id
) then
6676 if Debug_Flag_I
then
6677 Write_Str
("remove limited view of ");
6678 Write_Name
(Chars
(Pack_Id
));
6679 Write_Str
(" from visibility");
6683 -- The package already appears in the compilation closure. As a result,
6684 -- its shadow entities must be replaced by the real entities they hide
6685 -- and the previously hidden entities must be entered back into direct
6688 -- WARNING: This code must be kept synchronized with that of routine
6689 -- Install_Limited_Withed_Clause.
6691 if Analyzed
(Pack_Decl
) then
6692 Remove_Shadow_Entities_With_Restore
(Pack_Id
);
6694 -- Otherwise the package is not analyzed and its shadow entities must be
6695 -- removed from direct visibility.
6698 Remove_Shadow_Entities_From_Visibility
(Pack_Id
);
6701 -- Indicate that the limited view of the package is not installed
6703 Set_From_Limited_With
(Pack_Id
, False);
6704 end Remove_Limited_With_Unit
;
6706 --------------------
6707 -- Remove_Parents --
6708 --------------------
6710 procedure Remove_Parents
(Lib_Unit
: Node_Id
) is
6713 P_Spec
: Node_Id
:= Empty
;
6715 Vis
: constant Boolean :=
6716 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
;
6719 if Is_Child_Spec
(Lib_Unit
) then
6720 P_Spec
:= Parent_Spec
(Lib_Unit
);
6722 elsif Nkind
(Lib_Unit
) = N_Package_Body
6723 and then Nkind
(Original_Node
(Lib_Unit
)) = N_Package_Instantiation
6725 P_Spec
:= Parent_Spec
(Original_Node
(Lib_Unit
));
6728 if Present
(P_Spec
) then
6730 P_Name
:= Get_Parent_Entity
(P
);
6731 Remove_Context_Clauses
(P_Spec
);
6732 End_Package_Scope
(P_Name
);
6733 Set_Is_Immediately_Visible
(P_Name
, Vis
);
6735 -- Remove from visibility the siblings as well, which are directly
6736 -- visible while the parent is in scope.
6738 E
:= First_Entity
(P_Name
);
6739 while Present
(E
) loop
6740 if Is_Child_Unit
(E
) then
6741 Set_Is_Immediately_Visible
(E
, False);
6747 Set_In_Package_Body
(P_Name
, False);
6749 -- This is the recursive call to remove the context of any higher
6750 -- level parent. This recursion ensures that all parents are removed
6751 -- in the reverse order of their installation.
6757 ---------------------------------
6758 -- Remove_Private_With_Clauses --
6759 ---------------------------------
6761 procedure Remove_Private_With_Clauses
(Comp_Unit
: Node_Id
) is
6764 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean;
6765 -- Check whether a given unit appears in a regular with_clause. Used to
6766 -- determine whether a private_with_clause, implicit or explicit, should
6769 ----------------------------
6770 -- In_Regular_With_Clause --
6771 ----------------------------
6773 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean
6778 Item
:= First
(Context_Items
(Comp_Unit
));
6779 while Present
(Item
) loop
6780 if Nkind
(Item
) = N_With_Clause
6782 -- The following guard is needed to ensure that the name has
6783 -- been properly analyzed before we go fetching its entity.
6785 and then Is_Entity_Name
(Name
(Item
))
6786 and then Entity
(Name
(Item
)) = E
6787 and then not Private_Present
(Item
)
6795 end In_Regular_With_Clause
;
6797 -- Start of processing for Remove_Private_With_Clauses
6800 Item
:= First
(Context_Items
(Comp_Unit
));
6801 while Present
(Item
) loop
6802 if Nkind
(Item
) = N_With_Clause
and then Private_Present
(Item
) then
6804 -- If private_with_clause is redundant, remove it from context,
6805 -- as a small optimization to subsequent handling of private_with
6806 -- clauses in other nested packages. We replace the clause with
6807 -- a null statement, which is otherwise ignored by the rest of
6808 -- the compiler, so that ASIS tools can reconstruct the source.
6810 if In_Regular_With_Clause
(Entity
(Name
(Item
))) then
6812 Nxt
: constant Node_Id
:= Next
(Item
);
6814 Rewrite
(Item
, Make_Null_Statement
(Sloc
(Item
)));
6819 elsif Limited_Present
(Item
) then
6820 if not Limited_View_Installed
(Item
) then
6821 Remove_Limited_With_Clause
(Item
);
6827 Remove_Unit_From_Visibility
(Entity
(Name
(Item
)));
6828 Set_Context_Installed
(Item
, False);
6836 end Remove_Private_With_Clauses
;
6838 ---------------------------------
6839 -- Remove_Unit_From_Visibility --
6840 ---------------------------------
6842 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
) is
6844 if Debug_Flag_I
then
6845 Write_Str
("remove unit ");
6846 Write_Name
(Chars
(Unit_Name
));
6847 Write_Str
(" from visibility");
6851 Set_Is_Visible_Lib_Unit
(Unit_Name
, False);
6852 Set_Is_Potentially_Use_Visible
(Unit_Name
, False);
6853 Set_Is_Immediately_Visible
(Unit_Name
, False);
6855 -- If the unit is a wrapper package, the subprogram instance is
6856 -- what must be removed from visibility.
6857 -- Should we use Related_Instance instead???
6859 if Is_Wrapper_Package
(Unit_Name
) then
6860 Set_Is_Immediately_Visible
(Current_Entity
(Unit_Name
), False);
6862 end Remove_Unit_From_Visibility
;
6877 procedure Unchain
(E
: Entity_Id
) is
6881 Prev
:= Current_Entity
(E
);
6887 Set_Name_Entity_Id
(Chars
(E
), Homonym
(E
));
6890 while Present
(Prev
) and then Homonym
(Prev
) /= E
loop
6891 Prev
:= Homonym
(Prev
);
6894 if Present
(Prev
) then
6895 Set_Homonym
(Prev
, Homonym
(E
));
6899 if Debug_Flag_I
then
6900 Write_Str
(" (homonym) unchain ");
6901 Write_Name
(Chars
(E
));