1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Util; use Exp_Util;
31 with Elists; use Elists;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Inline; use Inline;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch7; use Sem_Ch7;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Dist; use Sem_Dist;
54 with Sem_Prag; use Sem_Prag;
55 with Sem_Util; use Sem_Util;
56 with Sem_Warn; use Sem_Warn;
57 with Stand; use Stand;
58 with Sinfo; use Sinfo;
59 with Sinfo.CN; use Sinfo.CN;
60 with Sinput; use Sinput;
61 with Snames; use Snames;
62 with Style; use Style;
63 with Stylesw; use Stylesw;
64 with Tbuild; use Tbuild;
65 with Uname; use Uname;
67 package body Sem_Ch10 is
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 procedure Analyze_Context (N : Node_Id);
74 -- Analyzes items in the context clause of compilation unit
76 procedure Build_Limited_Views (N : Node_Id);
77 -- Build and decorate the list of shadow entities for a package mentioned
78 -- in a limited_with clause. If the package was not previously analyzed
79 -- then it also performs a basic decoration of the real entities; this
80 -- is required to do not pass non-decorated entities to the back-end.
81 -- Implements Ada 2005 (AI-50217).
83 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
84 -- Check whether the source for the body of a compilation unit must
85 -- be included in a standalone library.
87 procedure Check_Private_Child_Unit (N : Node_Id);
88 -- If a with_clause mentions a private child unit, the compilation
89 -- unit must be a member of the same family, as described in 10.1.2.
91 procedure Check_Stub_Level (N : Node_Id);
92 -- Verify that a stub is declared immediately within a compilation unit,
93 -- and not in an inner frame.
95 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
96 -- When a child unit appears in a context clause, the implicit withs on
97 -- parents are made explicit, and with clauses are inserted in the context
98 -- clause before the one for the child. If a parent in the with_clause
99 -- is a renaming, the implicit with_clause is on the renaming whose name
100 -- is mentioned in the with_clause, and not on the package it renames.
101 -- N is the compilation unit whose list of context items receives the
102 -- implicit with_clauses.
104 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
105 -- Get defining entity of parent unit of a child unit. In most cases this
106 -- is the defining entity of the unit, but for a child instance whose
107 -- parent needs a body for inlining, the instantiation node of the parent
108 -- has not yet been rewritten as a package declaration, and the entity has
109 -- to be retrieved from the Instance_Spec of the unit.
111 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
112 -- If the main unit is a child unit, implicit withs are also added for
113 -- all its ancestors.
115 function In_Chain (E : Entity_Id) return Boolean;
116 -- Check that the shadow entity is not already in the homonym chain, for
117 -- example through a limited_with clause in a parent unit.
119 procedure Install_Context_Clauses (N : Node_Id);
120 -- Subsidiary to Install_Context and Install_Parents. Process only with_
121 -- and use_clauses for current unit and its library unit if any.
123 procedure Install_Limited_Context_Clauses (N : Node_Id);
124 -- Subsidiary to Install_Context. Process only limited with_clauses
125 -- for current unit. Implements Ada 2005 (AI-50217).
127 procedure Install_Limited_Withed_Unit (N : Node_Id);
128 -- Place shadow entities for a limited_with package in the visibility
129 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
131 procedure Install_Withed_Unit
132 (With_Clause : Node_Id;
133 Private_With_OK : Boolean := False);
134 -- If the unit is not a child unit, make unit immediately visible.
135 -- The caller ensures that the unit is not already currently installed.
136 -- The flag Private_With_OK is set true in Install_Private_With_Clauses,
137 -- which is called when compiling the private part of a package, or
138 -- installing the private declarations of a parent unit.
140 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
141 -- This procedure establishes the context for the compilation of a child
142 -- unit. If Lib_Unit is a child library spec then the context of the parent
143 -- is installed, and the parent itself made immediately visible, so that
144 -- the child unit is processed in the declarative region of the parent.
145 -- Install_Parents makes a recursive call to itself to ensure that all
146 -- parents are loaded in the nested case. If Lib_Unit is a library body,
147 -- the only effect of Install_Parents is to install the private decls of
148 -- the parents, because the visible parent declarations will have been
149 -- installed as part of the context of the corresponding spec.
151 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
152 -- In the compilation of a child unit, a child of any of the ancestor
153 -- units is directly visible if it is visible, because the parent is in
154 -- an enclosing scope. Iterate over context to find child units of U_Name
155 -- or of some ancestor of it.
157 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
158 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
159 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
160 -- a library spec that has a parent. If the call to Is_Child_Spec returns
161 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
162 -- compilation unit for the parent spec.
164 -- Lib_Unit can also be a subprogram body that acts as its own spec. If
165 -- the Parent_Spec is non-empty, this is also a child unit.
167 procedure Remove_Context_Clauses (N : Node_Id);
168 -- Subsidiary of previous one. Remove use_ and with_clauses
170 procedure Remove_Limited_With_Clause (N : Node_Id);
171 -- Remove from visibility the shadow entities introduced for a package
172 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
174 procedure Remove_Parents (Lib_Unit : Node_Id);
175 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
176 -- contexts established by the corresponding call to Install_Parents are
177 -- removed. Remove_Parents contains a recursive call to itself to ensure
178 -- that all parents are removed in the nested case.
180 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
181 -- Reset all visibility flags on unit after compiling it, either as a
182 -- main unit or as a unit in the context.
184 procedure Unchain (E : Entity_Id);
185 -- Remove single entity from visibility list
187 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
188 -- Common processing for all stubs (subprograms, tasks, packages, and
189 -- protected cases). N is the stub to be analyzed. Once the subunit
190 -- name is established, load and analyze. Nam is the non-overloadable
191 -- entity for which the proper body provides a completion. Subprogram
192 -- stubs are handled differently because they can be declarations.
195 -- A dummy procedure, for debugging use, called just before analyzing the
196 -- main unit (after dealing with any context clauses).
198 --------------------------
199 -- Limited_With_Clauses --
200 --------------------------
202 -- Limited_With clauses are the mechanism chosen for Ada05 to support
203 -- mutually recursive types declared in different units. A limited_with
204 -- clause that names package P in the context of unit U makes the types
205 -- declared in the visible part of P available within U, but with the
206 -- restriction that these types can only be used as incomplete types.
207 -- The limited_with clause does not impose a semantic dependence on P,
208 -- and it is possible for two packages to have limited_with_clauses on
209 -- each other without creating an elaboration circularity.
211 -- To support this feature, the analysis of a limited_with clause must
212 -- create an abbreviated view of the package, without performing any
213 -- semantic analysis on it. This "package abstract" contains shadow
214 -- types that are in one-one correspondence with the real types in the
215 -- package, and that have the properties of incomplete types.
217 -- The implementation creates two element lists: one to chain the shadow
218 -- entities, and one to chain the corresponding type entities in the tree
219 -- of the package. Links between corresponding entities in both chains
220 -- allow the compiler to select the proper view of a given type, depending
221 -- on the context. Note that in contrast with the handling of private
222 -- types, the limited view and the non-limited view of a type are treated
223 -- as separate entities, and no entity exchange needs to take place, which
224 -- makes the implementation must simpler than could be feared.
226 ------------------------------
227 -- Analyze_Compilation_Unit --
228 ------------------------------
230 procedure Analyze_Compilation_Unit (N : Node_Id) is
231 Unit_Node : constant Node_Id := Unit (N);
232 Lib_Unit : Node_Id := Library_Unit (N);
234 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
235 Par_Spec_Name : Unit_Name_Type;
236 Unum : Unit_Number_Type;
238 procedure Check_Redundant_Withs
239 (Context_Items : List_Id;
240 Spec_Context_Items : List_Id := No_List);
241 -- Determine whether the context list of a compilation unit contains
242 -- redundant with clauses. When checking body clauses against spec
243 -- clauses, set Context_Items to the context list of the body and
244 -- Spec_Context_Items to that of the spec. Parent packages are not
245 -- examined for documentation purposes.
247 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
248 -- Generate cross-reference information for the parents of child units.
249 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
251 ---------------------------
252 -- Check_Redundant_Withs --
253 ---------------------------
255 procedure Check_Redundant_Withs
256 (Context_Items : List_Id;
257 Spec_Context_Items : List_Id := No_List)
261 procedure Process_Body_Clauses
262 (Context_List : List_Id;
264 Used : in out Boolean;
265 Used_Type_Or_Elab : in out Boolean);
266 -- Examine the context clauses of a package body, trying to match
267 -- the name entity of Clause with any list element. If the match
268 -- occurs on a use package clause, set Used to True, for a use
269 -- type clause, pragma Elaborate or pragma Elaborate_All, set
270 -- Used_Type_Or_Elab to True.
272 procedure Process_Spec_Clauses
273 (Context_List : List_Id;
275 Used : in out Boolean;
276 Withed : in out Boolean;
277 Exit_On_Self : Boolean := False);
278 -- Examine the context clauses of a package spec, trying to match
279 -- the name entity of Clause with any list element. If the match
280 -- occurs on a use package clause, set Used to True, for a with
281 -- package clause other than Clause, set Withed to True. Limited
282 -- with clauses, implicitly generated with clauses and withs
283 -- having pragmas Elaborate or Elaborate_All applied to them are
284 -- skipped. Exit_On_Self is used to control the search loop and
285 -- force an exit whenever Clause sees itself in the search.
287 --------------------------
288 -- Process_Body_Clauses --
289 --------------------------
291 procedure Process_Body_Clauses
292 (Context_List : List_Id;
294 Used : in out Boolean;
295 Used_Type_Or_Elab : in out Boolean)
297 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
303 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
304 -- In an expanded name in a use clause, if the prefix is a
305 -- renamed package, the entity is set to the original package
306 -- as a result, when checking whether the package appears in a
307 -- previous with_clause, the renaming has to be taken into
308 -- account, to prevent spurious or incorrect warnings. The
309 -- common case is the use of Text_IO.
315 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
317 return Entity (N) = P
319 (Present (Renamed_Object (P))
320 and then Entity (N) = Renamed_Object (P));
323 -- Start of processing for Process_Body_Clauses
327 Used_Type_Or_Elab := False;
329 Cont_Item := First (Context_List);
330 while Present (Cont_Item) loop
332 -- Package use clause
334 if Nkind (Cont_Item) = N_Use_Package_Clause
337 -- Search through use clauses
339 Use_Item := First (Names (Cont_Item));
340 while Present (Use_Item) and then not Used loop
342 -- Case of a direct use of the one we are looking for
344 if Entity (Use_Item) = Nam_Ent then
347 -- Handle nested case, as in "with P; use P.Q.R"
354 -- Loop through prefixes looking for match
357 while Nkind (UE) = N_Expanded_Name loop
358 if Same_Unit (Prefix (UE), Nam_Ent) then
373 elsif Nkind (Cont_Item) = N_Use_Type_Clause
374 and then not Used_Type_Or_Elab
376 Subt_Mark := First (Subtype_Marks (Cont_Item));
377 while Present (Subt_Mark)
378 and then not Used_Type_Or_Elab
380 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
381 Used_Type_Or_Elab := True;
387 -- Pragma Elaborate or Elaborate_All
389 elsif Nkind (Cont_Item) = N_Pragma
391 (Pragma_Name (Cont_Item) = Name_Elaborate
393 Pragma_Name (Cont_Item) = Name_Elaborate_All)
394 and then not Used_Type_Or_Elab
397 First (Pragma_Argument_Associations (Cont_Item));
398 while Present (Prag_Unit)
399 and then not Used_Type_Or_Elab
401 if Entity (Expression (Prag_Unit)) = Nam_Ent then
402 Used_Type_Or_Elab := True;
411 end Process_Body_Clauses;
413 --------------------------
414 -- Process_Spec_Clauses --
415 --------------------------
417 procedure Process_Spec_Clauses
418 (Context_List : List_Id;
420 Used : in out Boolean;
421 Withed : in out Boolean;
422 Exit_On_Self : Boolean := False)
424 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
432 Cont_Item := First (Context_List);
433 while Present (Cont_Item) loop
435 -- Stop the search since the context items after Cont_Item
436 -- have already been examined in a previous iteration of
437 -- the reverse loop in Check_Redundant_Withs.
440 and Cont_Item = Clause
445 -- Package use clause
447 if Nkind (Cont_Item) = N_Use_Package_Clause
450 Use_Item := First (Names (Cont_Item));
451 while Present (Use_Item) and then not Used loop
452 if Entity (Use_Item) = Nam_Ent then
459 -- Package with clause. Avoid processing self, implicitly
460 -- generated with clauses or limited with clauses. Note
461 -- that we examine with clauses having pragmas Elaborate
462 -- or Elaborate_All applied to them due to cases such as:
466 -- pragma Elaborate (Pack);
468 -- In this case, the second with clause is redundant since
469 -- the pragma applies only to the first "with Pack;".
471 elsif Nkind (Cont_Item) = N_With_Clause
472 and then not Implicit_With (Cont_Item)
473 and then not Limited_Present (Cont_Item)
474 and then Cont_Item /= Clause
475 and then Entity (Name (Cont_Item)) = Nam_Ent
482 end Process_Spec_Clauses;
484 -- Start of processing for Check_Redundant_Withs
487 Clause := Last (Context_Items);
488 while Present (Clause) loop
490 -- Avoid checking implicitly generated with clauses, limited
491 -- with clauses or withs that have pragma Elaborate or
492 -- Elaborate_All applied.
494 if Nkind (Clause) = N_With_Clause
495 and then not Implicit_With (Clause)
496 and then not Limited_Present (Clause)
497 and then not Elaborate_Present (Clause)
499 -- Package body-to-spec check
501 if Present (Spec_Context_Items) then
503 Used_In_Body : Boolean := False;
504 Used_In_Spec : Boolean := False;
505 Used_Type_Or_Elab : Boolean := False;
506 Withed_In_Spec : Boolean := False;
510 (Context_List => Spec_Context_Items,
512 Used => Used_In_Spec,
513 Withed => Withed_In_Spec);
516 (Context_List => Context_Items,
518 Used => Used_In_Body,
519 Used_Type_Or_Elab => Used_Type_Or_Elab);
521 -- "Type Elab" refers to the presence of either a use
522 -- type clause, pragmas Elaborate or Elaborate_All.
524 -- +---------------+---------------------------+------+
525 -- | Spec | Body | Warn |
526 -- +--------+------+--------+------+-----------+------+
527 -- | Withed | Used | Withed | Used | Type Elab | |
528 -- | X | | X | | | X |
529 -- | X | | X | X | | |
530 -- | X | | X | | X | |
531 -- | X | | X | X | X | |
532 -- | X | X | X | | | X |
533 -- | X | X | X | | X | |
534 -- | X | X | X | X | | X |
535 -- | X | X | X | X | X | |
536 -- +--------+------+--------+------+-----------+------+
539 and then not Used_Type_Or_Elab)
542 and then not Used_In_Body)
546 Error_Msg_N ("?redundant with clause in body", Clause);
549 Used_In_Body := False;
550 Used_In_Spec := False;
551 Used_Type_Or_Elab := False;
552 Withed_In_Spec := False;
555 -- Standalone package spec or body check
559 Dont_Care : Boolean := False;
560 Withed : Boolean := False;
563 -- The mechanism for examining the context clauses of a
564 -- package spec can be applied to package body clauses.
567 (Context_List => Context_Items,
571 Exit_On_Self => True);
574 Error_Msg_N ("?redundant with clause", Clause);
582 end Check_Redundant_Withs;
584 --------------------------------
585 -- Generate_Parent_References --
586 --------------------------------
588 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
590 P_Name : Entity_Id := P_Id;
593 Pref := Name (Parent (Defining_Entity (N)));
595 if Nkind (Pref) = N_Expanded_Name then
597 -- Done already, if the unit has been compiled indirectly as
598 -- part of the closure of its context because of inlining.
603 while Nkind (Pref) = N_Selected_Component loop
604 Change_Selected_Component_To_Expanded_Name (Pref);
605 Set_Entity (Pref, P_Name);
606 Set_Etype (Pref, Etype (P_Name));
607 Generate_Reference (P_Name, Pref, 'r');
608 Pref := Prefix (Pref);
609 P_Name := Scope (P_Name);
612 -- The guard here on P_Name is to handle the error condition where
613 -- the parent unit is missing because the file was not found.
615 if Present (P_Name) then
616 Set_Entity (Pref, P_Name);
617 Set_Etype (Pref, Etype (P_Name));
618 Generate_Reference (P_Name, Pref, 'r');
619 Style.Check_Identifier (Pref, P_Name);
621 end Generate_Parent_References;
623 -- Start of processing for Analyze_Compilation_Unit
626 Process_Compilation_Unit_Pragmas (N);
628 -- If the unit is a subunit whose parent has not been analyzed (which
629 -- indicates that the main unit is a subunit, either the current one or
630 -- one of its descendents) then the subunit is compiled as part of the
631 -- analysis of the parent, which we proceed to do. Basically this gets
632 -- handled from the top down and we don't want to do anything at this
633 -- level (i.e. this subunit will be handled on the way down from the
634 -- parent), so at this level we immediately return. If the subunit
635 -- ends up not analyzed, it means that the parent did not contain a
636 -- stub for it, or that there errors were detected in some ancestor.
638 if Nkind (Unit_Node) = N_Subunit
639 and then not Analyzed (Lib_Unit)
641 Semantics (Lib_Unit);
643 if not Analyzed (Proper_Body (Unit_Node)) then
644 if Serious_Errors_Detected > 0 then
645 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
647 Error_Msg_N ("missing stub for subunit", N);
654 -- Analyze context (this will call Sem recursively for with'ed units)
658 -- If the unit is a package body, the spec is already loaded and must
659 -- be analyzed first, before we analyze the body.
661 if Nkind (Unit_Node) = N_Package_Body then
663 -- If no Lib_Unit, then there was a serious previous error, so
664 -- just ignore the entire analysis effort
666 if No (Lib_Unit) then
670 Semantics (Lib_Unit);
671 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
673 -- Verify that the library unit is a package declaration
675 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
676 N_Generic_Package_Declaration)
679 ("no legal package declaration for package body", N);
682 -- Otherwise, the entity in the declaration is visible. Update
683 -- the version to reflect dependence of this body on the spec.
686 Spec_Id := Defining_Entity (Unit (Lib_Unit));
687 Set_Is_Immediately_Visible (Spec_Id, True);
688 Version_Update (N, Lib_Unit);
690 if Nkind (Defining_Unit_Name (Unit_Node)) =
691 N_Defining_Program_Unit_Name
693 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
698 -- If the unit is a subprogram body, then we similarly need to analyze
699 -- its spec. However, things are a little simpler in this case, because
700 -- here, this analysis is done only for error checking and consistency
701 -- purposes, so there's nothing else to be done.
703 elsif Nkind (Unit_Node) = N_Subprogram_Body then
704 if Acts_As_Spec (N) then
706 -- If the subprogram body is a child unit, we must create a
707 -- declaration for it, in order to properly load the parent(s).
708 -- After this, the original unit does not acts as a spec, because
709 -- there is an explicit one. If this unit appears in a context
710 -- clause, then an implicit with on the parent will be added when
711 -- installing the context. If this is the main unit, there is no
712 -- Unit_Table entry for the declaration (it has the unit number
713 -- of the main unit) and code generation is unaffected.
715 Unum := Get_Cunit_Unit_Number (N);
716 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
718 if Par_Spec_Name /= No_Unit_Name then
721 (Load_Name => Par_Spec_Name,
726 if Unum /= No_Unit then
728 -- Build subprogram declaration and attach parent unit to it
729 -- This subprogram declaration does not come from source,
730 -- Nevertheless the backend must generate debugging info for
731 -- it, and this must be indicated explicitly. We also mark
732 -- the body entity as a child unit now, to prevent a
733 -- cascaded error if the spec entity cannot be entered
737 Loc : constant Source_Ptr := Sloc (N);
738 SCS : constant Boolean :=
739 Get_Comes_From_Source_Default;
742 Set_Comes_From_Source_Default (False);
744 Make_Compilation_Unit (Loc,
745 Context_Items => New_Copy_List (Context_Items (N)),
747 Make_Subprogram_Declaration (Sloc (N),
750 (Specification (Unit_Node))),
752 Make_Compilation_Unit_Aux (Loc));
754 Set_Library_Unit (N, Lib_Unit);
755 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
756 Semantics (Lib_Unit);
758 -- Now that a separate declaration exists, the body
759 -- of the child unit does not act as spec any longer.
761 Set_Acts_As_Spec (N, False);
762 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
763 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
764 Set_Comes_From_Source_Default (SCS);
769 -- Here for subprogram with separate declaration
772 Semantics (Lib_Unit);
773 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
774 Version_Update (N, Lib_Unit);
777 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
778 N_Defining_Program_Unit_Name
780 Generate_Parent_References (
781 Specification (Unit_Node),
782 Scope (Defining_Entity (Unit (Lib_Unit))));
786 -- If it is a child unit, the parent must be elaborated first
787 -- and we update version, since we are dependent on our parent.
789 if Is_Child_Spec (Unit_Node) then
791 -- The analysis of the parent is done with style checks off
794 Save_Style_Check : constant Boolean := Style_Check;
795 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
796 Cunit_Boolean_Restrictions_Save;
799 if not GNAT_Mode then
800 Style_Check := False;
803 Semantics (Parent_Spec (Unit_Node));
804 Version_Update (N, Parent_Spec (Unit_Node));
805 Style_Check := Save_Style_Check;
806 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
810 -- With the analysis done, install the context. Note that we can't
811 -- install the context from the with clauses as we analyze them, because
812 -- each with clause must be analyzed in a clean visibility context, so
813 -- we have to wait and install them all at once.
817 if Is_Child_Spec (Unit_Node) then
819 -- Set the entities of all parents in the program_unit_name
821 Generate_Parent_References (
822 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
825 -- All components of the context: with-clauses, library unit, ancestors
826 -- if any, (and their context) are analyzed and installed.
828 -- Call special debug routine sm if this is the main unit
830 if Current_Sem_Unit = Main_Unit then
834 -- Now analyze the unit (package, subprogram spec, body) itself
838 if Warn_On_Redundant_Constructs then
839 Check_Redundant_Withs (Context_Items (N));
841 if Nkind (Unit_Node) = N_Package_Body then
842 Check_Redundant_Withs
843 (Context_Items => Context_Items (N),
844 Spec_Context_Items => Context_Items (Lib_Unit));
848 -- The above call might have made Unit_Node an N_Subprogram_Body from
849 -- something else, so propagate any Acts_As_Spec flag.
851 if Nkind (Unit_Node) = N_Subprogram_Body
852 and then Acts_As_Spec (Unit_Node)
854 Set_Acts_As_Spec (N);
857 -- Register predefined units in Rtsfind
860 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
862 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
863 Set_RTU_Loaded (Unit_Node);
867 -- Treat compilation unit pragmas that appear after the library unit
869 if Present (Pragmas_After (Aux_Decls_Node (N))) then
871 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
873 while Present (Prag_Node) loop
880 -- Generate distribution stubs if requested and no error
883 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
885 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
886 and then not Fatal_Error (Main_Unit)
888 if Is_RCI_Pkg_Spec_Or_Body (N) then
890 -- Regular RCI package
892 Add_Stub_Constructs (N);
894 elsif (Nkind (Unit_Node) = N_Package_Declaration
895 and then Is_Shared_Passive (Defining_Entity
896 (Specification (Unit_Node))))
897 or else (Nkind (Unit_Node) = N_Package_Body
899 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
901 -- Shared passive package
903 Add_Stub_Constructs (N);
905 elsif Nkind (Unit_Node) = N_Package_Instantiation
907 Is_Remote_Call_Interface
908 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
910 -- Instantiation of a RCI generic package
912 Add_Stub_Constructs (N);
916 -- Remove unit from visibility, so that environment is clean for
917 -- the next compilation, which is either the main unit or some
918 -- other unit in the context.
920 if Nkind_In (Unit_Node, N_Package_Declaration,
921 N_Package_Renaming_Declaration,
922 N_Subprogram_Declaration)
923 or else Nkind (Unit_Node) in N_Generic_Declaration
925 (Nkind (Unit_Node) = N_Subprogram_Body
926 and then Acts_As_Spec (Unit_Node))
928 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
930 -- If the unit is an instantiation whose body will be elaborated for
931 -- inlining purposes, use the proper entity of the instance. The
932 -- entity may be missing if the instantiation was illegal.
934 elsif Nkind (Unit_Node) = N_Package_Instantiation
935 and then not Error_Posted (Unit_Node)
936 and then Present (Instance_Spec (Unit_Node))
938 Remove_Unit_From_Visibility
939 (Defining_Entity (Instance_Spec (Unit_Node)));
941 elsif Nkind (Unit_Node) = N_Package_Body
942 or else (Nkind (Unit_Node) = N_Subprogram_Body
943 and then not Acts_As_Spec (Unit_Node))
945 -- Bodies that are not the main unit are compiled if they are generic
946 -- or contain generic or inlined units. Their analysis brings in the
947 -- context of the corresponding spec (unit declaration) which must be
948 -- removed as well, to return the compilation environment to its
951 Remove_Context (Lib_Unit);
952 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
955 -- Last step is to deinstall the context we just installed as well as
956 -- the unit just compiled.
960 -- If this is the main unit and we are generating code, we must check
961 -- that all generic units in the context have a body if they need it,
962 -- even if they have not been instantiated. In the absence of .ali files
963 -- for generic units, we must force the load of the body, just to
964 -- produce the proper error if the body is absent. We skip this
965 -- verification if the main unit itself is generic.
967 if Get_Cunit_Unit_Number (N) = Main_Unit
968 and then Operating_Mode = Generate_Code
969 and then Expander_Active
971 -- Check whether the source for the body of the unit must be included
972 -- in a standalone library.
974 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
976 -- Indicate that the main unit is now analyzed, to catch possible
977 -- circularities between it and generic bodies. Remove main unit from
978 -- visibility. This might seem superfluous, but the main unit must
979 -- not be visible in the generic body expansions that follow.
981 Set_Analyzed (N, True);
982 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
987 Un : Unit_Number_Type;
989 Save_Style_Check : constant Boolean := Style_Check;
990 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
991 Cunit_Boolean_Restrictions_Save;
994 Item := First (Context_Items (N));
995 while Present (Item) loop
997 -- Check for explicit with clause
999 if Nkind (Item) = N_With_Clause
1000 and then not Implicit_With (Item)
1002 -- Ada 2005 (AI-50217): Ignore limited-withed units
1004 and then not Limited_Present (Item)
1006 Nam := Entity (Name (Item));
1008 -- Compile generic subprogram, unless it is intrinsic or
1009 -- imported so no body is required, or generic package body
1010 -- if the package spec requires a body.
1012 if (Is_Generic_Subprogram (Nam)
1013 and then not Is_Intrinsic_Subprogram (Nam)
1014 and then not Is_Imported (Nam))
1015 or else (Ekind (Nam) = E_Generic_Package
1016 and then Unit_Requires_Body (Nam))
1018 Style_Check := False;
1020 if Present (Renamed_Object (Nam)) then
1023 (Load_Name => Get_Body_Name
1025 (Unit_Declaration_Node
1026 (Renamed_Object (Nam)))),
1034 (Load_Name => Get_Body_Name
1035 (Get_Unit_Name (Item)),
1042 if Un = No_Unit then
1044 ("body of generic unit& not found", Item, Nam);
1047 elsif not Analyzed (Cunit (Un))
1048 and then Un /= Main_Unit
1049 and then not Fatal_Error (Un)
1051 Style_Check := False;
1052 Semantics (Cunit (Un));
1060 Style_Check := Save_Style_Check;
1061 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1065 -- Deal with creating elaboration Boolean if needed. We create an
1066 -- elaboration boolean only for units that come from source since
1067 -- units manufactured by the compiler never need elab checks.
1069 if Comes_From_Source (N)
1070 and then Nkind_In (Unit_Node, N_Package_Declaration,
1071 N_Generic_Package_Declaration,
1072 N_Subprogram_Declaration,
1073 N_Generic_Subprogram_Declaration)
1076 Loc : constant Source_Ptr := Sloc (N);
1077 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1080 Spec_Id := Defining_Entity (Unit_Node);
1081 Generate_Definition (Spec_Id);
1083 -- See if an elaboration entity is required for possible access
1084 -- before elaboration checking. Note that we must allow for this
1085 -- even if -gnatE is not set, since a client may be compiled in
1086 -- -gnatE mode and reference the entity.
1088 -- These entities are also used by the binder to prevent multiple
1089 -- attempts to execute the elaboration code for the library case
1090 -- where the elaboration routine might otherwise be called more
1093 -- Case of units which do not require elaboration checks
1096 -- Pure units do not need checks
1100 -- Preelaborated units do not need checks
1102 or else Is_Preelaborated (Spec_Id)
1104 -- No checks needed if pragma Elaborate_Body present
1106 or else Has_Pragma_Elaborate_Body (Spec_Id)
1108 -- No checks needed if unit does not require a body
1110 or else not Unit_Requires_Body (Spec_Id)
1112 -- No checks needed for predefined files
1114 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1116 -- No checks required if no separate spec
1118 or else Acts_As_Spec (N)
1120 -- This is a case where we only need the entity for
1121 -- checking to prevent multiple elaboration checks.
1123 Set_Elaboration_Entity_Required (Spec_Id, False);
1125 -- Case of elaboration entity is required for access before
1126 -- elaboration checking (so certainly we must build it!)
1129 Set_Elaboration_Entity_Required (Spec_Id, True);
1132 Build_Elaboration_Entity (N, Spec_Id);
1136 -- Freeze the compilation unit entity. This for sure is needed because
1137 -- of some warnings that can be output (see Freeze_Subprogram), but may
1138 -- in general be required. If freezing actions result, place them in the
1139 -- compilation unit actions list, and analyze them.
1142 Loc : constant Source_Ptr := Sloc (N);
1143 L : constant List_Id :=
1144 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), Loc);
1146 while Is_Non_Empty_List (L) loop
1147 Insert_Library_Level_Action (Remove_Head (L));
1153 if Nkind (Unit_Node) = N_Package_Declaration
1154 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1155 and then Expander_Active
1158 Save_Style_Check : constant Boolean := Style_Check;
1159 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1160 Options : Style_Check_Options;
1163 Save_Style_Check_Options (Options);
1164 Reset_Style_Check_Options;
1165 Opt.Warning_Mode := Suppress;
1166 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1168 Reset_Style_Check_Options;
1169 Set_Style_Check_Options (Options);
1170 Style_Check := Save_Style_Check;
1171 Warning_Mode := Save_Warning;
1175 -- If we are generating obsolescent warnings, then here is where we
1176 -- generate them for the with'ed items. The reason for this special
1177 -- processing is that the normal mechanism of generating the warnings
1178 -- for referenced entities does not work for context clause references.
1179 -- That's because when we first analyze the context, it is too early to
1180 -- know if the with'ing unit is itself obsolescent (which suppresses
1183 if not GNAT_Mode and then Warn_On_Obsolescent_Feature then
1185 -- Push current compilation unit as scope, so that the test for
1186 -- being within an obsolescent unit will work correctly.
1188 Push_Scope (Defining_Entity (Unit_Node));
1190 -- Loop through context items to deal with with clauses
1198 Item := First (Context_Items (N));
1199 while Present (Item) loop
1200 if Nkind (Item) = N_With_Clause
1202 -- Suppress this check in limited-withed units. Further work
1203 -- needed here if we decide to incorporate this check on
1204 -- limited-withed units.
1206 and then not Limited_Present (Item)
1209 Ent := Entity (Nam);
1211 if Is_Obsolescent (Ent) then
1212 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1220 -- Remove temporary install of current unit as scope
1224 end Analyze_Compilation_Unit;
1226 ---------------------
1227 -- Analyze_Context --
1228 ---------------------
1230 procedure Analyze_Context (N : Node_Id) is
1231 Ukind : constant Node_Kind := Nkind (Unit (N));
1235 -- First process all configuration pragmas at the start of the context
1236 -- items. Strictly these are not part of the context clause, but that
1237 -- is where the parser puts them. In any case for sure we must analyze
1238 -- these before analyzing the actual context items, since they can have
1239 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1240 -- be with'ed as a result of changing categorizations in Ada 2005).
1242 Item := First (Context_Items (N));
1243 while Present (Item)
1244 and then Nkind (Item) = N_Pragma
1245 and then Pragma_Name (Item) in Configuration_Pragma_Names
1251 -- This is the point at which we capture the configuration settings
1252 -- for the unit. At the moment only the Optimize_Alignment setting
1253 -- needs to be captured. Probably more later ???
1255 if Optimize_Alignment_Local then
1256 Set_OA_Setting (Current_Sem_Unit, 'L');
1258 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1261 -- Loop through actual context items. This is done in two passes:
1263 -- a) The first pass analyzes non-limited with-clauses and also any
1264 -- configuration pragmas (we need to get the latter analyzed right
1265 -- away, since they can affect processing of subsequent items.
1267 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1269 while Present (Item) loop
1271 -- For with clause, analyze the with clause, and then update
1272 -- the version, since we are dependent on a unit that we with.
1274 if Nkind (Item) = N_With_Clause
1275 and then not Limited_Present (Item)
1277 -- Skip analyzing with clause if no unit, nothing to do (this
1278 -- happens for a with that references a non-existent unit)
1280 if Present (Library_Unit (Item)) then
1284 if not Implicit_With (Item) then
1285 Version_Update (N, Library_Unit (Item));
1288 -- Skip pragmas. Configuration pragmas at the start were handled in
1289 -- the loop above, and remaining pragmas are not processed until we
1290 -- actually install the context (see Install_Context). We delay the
1291 -- analysis of these pragmas to make sure that we have installed all
1292 -- the implicit with's on parent units.
1294 -- Skip use clauses at this stage, since we don't want to do any
1295 -- installing of potentially use visible entities until we
1296 -- actually install the complete context (in Install_Context).
1297 -- Otherwise things can get installed in the wrong context.
1306 -- Second pass: examine all limited_with clauses. All other context
1307 -- items are ignored in this pass.
1309 Item := First (Context_Items (N));
1310 while Present (Item) loop
1311 if Nkind (Item) = N_With_Clause
1312 and then Limited_Present (Item)
1314 -- No need to check errors on implicitly generated limited-with
1317 if not Implicit_With (Item) then
1319 -- Verify that the illegal contexts given in 10.1.2 (18/2)
1320 -- are properly rejected, including renaming declarations.
1322 if not Nkind_In (Ukind, N_Package_Declaration,
1323 N_Subprogram_Declaration)
1324 and then Ukind not in N_Generic_Declaration
1325 and then Ukind not in N_Generic_Instantiation
1327 Error_Msg_N ("limited with_clause not allowed here", Item);
1329 -- Check wrong use of a limited with clause applied to the
1330 -- compilation unit containing the limited-with clause.
1332 -- limited with P.Q;
1333 -- package P.Q is ...
1335 elsif Unit (Library_Unit (Item)) = Unit (N) then
1336 Error_Msg_N ("wrong use of limited-with clause", Item);
1338 -- Check wrong use of limited-with clause applied to some
1339 -- immediate ancestor.
1341 elsif Is_Child_Spec (Unit (N)) then
1343 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1347 P := Parent_Spec (Unit (N));
1349 if Unit (P) = Lib_U then
1350 Error_Msg_N ("limited with_clause of immediate "
1351 & "ancestor not allowed", Item);
1355 exit when not Is_Child_Spec (Unit (P));
1356 P := Parent_Spec (Unit (P));
1361 -- Check if the limited-withed unit is already visible through
1362 -- some context clause of the current compilation unit or some
1363 -- ancestor of the current compilation unit.
1366 Lim_Unit_Name : constant Node_Id := Name (Item);
1367 Comp_Unit : Node_Id;
1369 Unit_Name : Node_Id;
1374 It := First (Context_Items (Comp_Unit));
1375 while Present (It) loop
1377 and then Nkind (It) = N_With_Clause
1378 and then not Limited_Present (It)
1380 Nkind_In (Unit (Library_Unit (It)),
1381 N_Package_Declaration,
1382 N_Package_Renaming_Declaration)
1384 if Nkind (Unit (Library_Unit (It))) =
1385 N_Package_Declaration
1387 Unit_Name := Name (It);
1389 Unit_Name := Name (Unit (Library_Unit (It)));
1392 -- Check if the named package (or some ancestor)
1393 -- leaves visible the full-view of the unit given
1394 -- in the limited-with clause
1397 if Designate_Same_Unit (Lim_Unit_Name,
1400 Error_Msg_Sloc := Sloc (It);
1402 ("simultaneous visibility of limited "
1403 & "and unlimited views not allowed",
1406 ("\unlimited view visible through "
1407 & "context clause #",
1411 elsif Nkind (Unit_Name) = N_Identifier then
1415 Unit_Name := Prefix (Unit_Name);
1422 exit when not Is_Child_Spec (Unit (Comp_Unit));
1424 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1429 -- Skip analyzing with clause if no unit, see above
1431 if Present (Library_Unit (Item)) then
1435 -- A limited_with does not impose an elaboration order, but
1436 -- there is a semantic dependency for recompilation purposes.
1438 if not Implicit_With (Item) then
1439 Version_Update (N, Library_Unit (Item));
1442 -- Pragmas and use clauses and with clauses other than limited
1443 -- with's are ignored in this pass through the context items.
1451 end Analyze_Context;
1453 -------------------------------
1454 -- Analyze_Package_Body_Stub --
1455 -------------------------------
1457 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1458 Id : constant Entity_Id := Defining_Identifier (N);
1462 -- The package declaration must be in the current declarative part
1464 Check_Stub_Level (N);
1465 Nam := Current_Entity_In_Scope (Id);
1467 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1468 Error_Msg_N ("missing specification for package stub", N);
1470 elsif Has_Completion (Nam)
1471 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1473 Error_Msg_N ("duplicate or redundant stub for package", N);
1476 -- Indicate that the body of the package exists. If we are doing
1477 -- only semantic analysis, the stub stands for the body. If we are
1478 -- generating code, the existence of the body will be confirmed
1479 -- when we load the proper body.
1481 Set_Has_Completion (Nam);
1482 Set_Scope (Defining_Entity (N), Current_Scope);
1483 Generate_Reference (Nam, Id, 'b');
1484 Analyze_Proper_Body (N, Nam);
1486 end Analyze_Package_Body_Stub;
1488 -------------------------
1489 -- Analyze_Proper_Body --
1490 -------------------------
1492 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1493 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1494 Unum : Unit_Number_Type;
1496 procedure Optional_Subunit;
1497 -- This procedure is called when the main unit is a stub, or when we
1498 -- are not generating code. In such a case, we analyze the subunit if
1499 -- present, which is user-friendly and in fact required for ASIS, but
1500 -- we don't complain if the subunit is missing.
1502 ----------------------
1503 -- Optional_Subunit --
1504 ----------------------
1506 procedure Optional_Subunit is
1507 Comp_Unit : Node_Id;
1510 -- Try to load subunit, but ignore any errors that occur during
1511 -- the loading of the subunit, by using the special feature in
1512 -- Errout to ignore all errors. Note that Fatal_Error will still
1513 -- be set, so we will be able to check for this case below.
1515 if not ASIS_Mode then
1516 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1521 (Load_Name => Subunit_Name,
1526 if not ASIS_Mode then
1527 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1530 -- All done if we successfully loaded the subunit
1533 and then (not Fatal_Error (Unum) or else Try_Semantics)
1535 Comp_Unit := Cunit (Unum);
1537 -- If the file was empty or seriously mangled, the unit
1538 -- itself may be missing.
1540 if No (Unit (Comp_Unit)) then
1542 ("subunit does not contain expected proper body", N);
1544 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1546 ("expected SEPARATE subunit, found child unit",
1547 Cunit_Entity (Unum));
1549 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1550 Analyze_Subunit (Comp_Unit);
1551 Set_Library_Unit (N, Comp_Unit);
1554 elsif Unum = No_Unit
1555 and then Present (Nam)
1557 if Is_Protected_Type (Nam) then
1558 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1560 Set_Corresponding_Body (
1561 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1564 end Optional_Subunit;
1566 -- Start of processing for Analyze_Proper_Body
1569 -- If the subunit is already loaded, it means that the main unit
1570 -- is a subunit, and that the current unit is one of its parents
1571 -- which was being analyzed to provide the needed context for the
1572 -- analysis of the subunit. In this case we analyze the subunit and
1573 -- continue with the parent, without looking a subsequent subunits.
1575 if Is_Loaded (Subunit_Name) then
1577 -- If the proper body is already linked to the stub node,
1578 -- the stub is in a generic unit and just needs analyzing.
1580 if Present (Library_Unit (N)) then
1581 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1582 Analyze_Subunit (Library_Unit (N));
1584 -- Otherwise we must load the subunit and link to it
1587 -- Load the subunit, this must work, since we originally
1588 -- loaded the subunit earlier on. So this will not really
1589 -- load it, just give access to it.
1593 (Load_Name => Subunit_Name,
1598 -- And analyze the subunit in the parent context (note that we
1599 -- do not call Semantics, since that would remove the parent
1600 -- context). Because of this, we have to manually reset the
1601 -- compiler state to Analyzing since it got destroyed by Load.
1603 if Unum /= No_Unit then
1604 Compiler_State := Analyzing;
1606 -- Check that the proper body is a subunit and not a child
1607 -- unit. If the unit was previously loaded, the error will
1608 -- have been emitted when copying the generic node, so we
1609 -- just return to avoid cascaded errors.
1611 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1615 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1616 Analyze_Subunit (Cunit (Unum));
1617 Set_Library_Unit (N, Cunit (Unum));
1621 -- If the main unit is a subunit, then we are just performing semantic
1622 -- analysis on that subunit, and any other subunits of any parent unit
1623 -- should be ignored, except that if we are building trees for ASIS
1624 -- usage we want to annotate the stub properly.
1626 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1627 and then Subunit_Name /= Unit_Name (Main_Unit)
1633 -- But before we return, set the flag for unloaded subunits. This
1634 -- will suppress junk warnings of variables in the same declarative
1635 -- part (or a higher level one) that are in danger of looking unused
1636 -- when in fact there might be a declaration in the subunit that we
1637 -- do not intend to load.
1639 Unloaded_Subunits := True;
1642 -- If the subunit is not already loaded, and we are generating code,
1643 -- then this is the case where compilation started from the parent,
1644 -- and we are generating code for an entire subunit tree. In that
1645 -- case we definitely need to load the subunit.
1647 -- In order to continue the analysis with the rest of the parent,
1648 -- and other subunits, we load the unit without requiring its
1649 -- presence, and emit a warning if not found, rather than terminating
1650 -- the compilation abruptly, as for other missing file problems.
1652 elsif Original_Operating_Mode = Generate_Code then
1654 -- If the proper body is already linked to the stub node,
1655 -- the stub is in a generic unit and just needs analyzing.
1657 -- We update the version. Although we are not technically
1658 -- semantically dependent on the subunit, given our approach
1659 -- of macro substitution of subunits, it makes sense to
1660 -- include it in the version identification.
1662 if Present (Library_Unit (N)) then
1663 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1664 Analyze_Subunit (Library_Unit (N));
1665 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1667 -- Otherwise we must load the subunit and link to it
1672 (Load_Name => Subunit_Name,
1677 if Original_Operating_Mode = Generate_Code
1678 and then Unum = No_Unit
1680 Error_Msg_Unit_1 := Subunit_Name;
1682 Get_File_Name (Subunit_Name, Subunit => True);
1684 ("subunit$$ in file{ not found?", N);
1685 Subunits_Missing := True;
1688 -- Load_Unit may reset Compiler_State, since it may have been
1689 -- necessary to parse an additional units, so we make sure
1690 -- that we reset it to the Analyzing state.
1692 Compiler_State := Analyzing;
1694 if Unum /= No_Unit then
1695 if Debug_Flag_L then
1696 Write_Str ("*** Loaded subunit from stub. Analyze");
1701 Comp_Unit : constant Node_Id := Cunit (Unum);
1704 -- Check for child unit instead of subunit
1706 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1708 ("expected SEPARATE subunit, found child unit",
1709 Cunit_Entity (Unum));
1711 -- OK, we have a subunit
1714 -- Set corresponding stub (even if errors)
1716 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1718 -- Analyze the unit if semantics active
1720 if not Fatal_Error (Unum) or else Try_Semantics then
1721 Analyze_Subunit (Comp_Unit);
1724 -- Set the library unit pointer in any case
1726 Set_Library_Unit (N, Comp_Unit);
1728 -- We update the version. Although we are not technically
1729 -- semantically dependent on the subunit, given our
1730 -- approach of macro substitution of subunits, it makes
1731 -- sense to include it in the version identification.
1733 Version_Update (Cunit (Main_Unit), Comp_Unit);
1739 -- The remaining case is when the subunit is not already loaded and
1740 -- we are not generating code. In this case we are just performing
1741 -- semantic analysis on the parent, and we are not interested in
1742 -- the subunit. For subprograms, analyze the stub as a body. For
1743 -- other entities the stub has already been marked as completed.
1748 end Analyze_Proper_Body;
1750 ----------------------------------
1751 -- Analyze_Protected_Body_Stub --
1752 ----------------------------------
1754 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1755 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1758 Check_Stub_Level (N);
1760 -- First occurrence of name may have been as an incomplete type
1762 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1763 Nam := Full_View (Nam);
1767 or else not Is_Protected_Type (Etype (Nam))
1769 Error_Msg_N ("missing specification for Protected body", N);
1771 Set_Scope (Defining_Entity (N), Current_Scope);
1772 Set_Has_Completion (Etype (Nam));
1773 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1774 Analyze_Proper_Body (N, Etype (Nam));
1776 end Analyze_Protected_Body_Stub;
1778 ----------------------------------
1779 -- Analyze_Subprogram_Body_Stub --
1780 ----------------------------------
1782 -- A subprogram body stub can appear with or without a previous
1783 -- specification. If there is one, the analysis of the body will
1784 -- find it and verify conformance. The formals appearing in the
1785 -- specification of the stub play no role, except for requiring an
1786 -- additional conformance check. If there is no previous subprogram
1787 -- declaration, the stub acts as a spec, and provides the defining
1788 -- entity for the subprogram.
1790 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1794 Check_Stub_Level (N);
1796 -- Verify that the identifier for the stub is unique within this
1797 -- declarative part.
1799 if Nkind_In (Parent (N), N_Block_Statement,
1803 Decl := First (Declarations (Parent (N)));
1804 while Present (Decl)
1807 if Nkind (Decl) = N_Subprogram_Body_Stub
1808 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1809 Chars (Defining_Unit_Name (Specification (N))))
1811 Error_Msg_N ("identifier for stub is not unique", N);
1818 -- Treat stub as a body, which checks conformance if there is a previous
1819 -- declaration, or else introduces entity and its signature.
1821 Analyze_Subprogram_Body (N);
1822 Analyze_Proper_Body (N, Empty);
1823 end Analyze_Subprogram_Body_Stub;
1825 ---------------------
1826 -- Analyze_Subunit --
1827 ---------------------
1829 -- A subunit is compiled either by itself (for semantic checking)
1830 -- or as part of compiling the parent (for code generation). In
1831 -- either case, by the time we actually process the subunit, the
1832 -- parent has already been installed and analyzed. The node N is
1833 -- a compilation unit, whose context needs to be treated here,
1834 -- because we come directly here from the parent without calling
1835 -- Analyze_Compilation_Unit.
1837 -- The compilation context includes the explicit context of the
1838 -- subunit, and the context of the parent, together with the parent
1839 -- itself. In order to compile the current context, we remove the
1840 -- one inherited from the parent, in order to have a clean visibility
1841 -- table. We restore the parent context before analyzing the proper
1842 -- body itself. On exit, we remove only the explicit context of the
1845 procedure Analyze_Subunit (N : Node_Id) is
1846 Lib_Unit : constant Node_Id := Library_Unit (N);
1847 Par_Unit : constant Entity_Id := Current_Scope;
1849 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1850 Num_Scopes : Int := 0;
1851 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1852 Enclosing_Child : Entity_Id := Empty;
1853 Svg : constant Suppress_Array := Scope_Suppress;
1855 procedure Analyze_Subunit_Context;
1856 -- Capture names in use clauses of the subunit. This must be done
1857 -- before re-installing parent declarations, because items in the
1858 -- context must not be hidden by declarations local to the parent.
1860 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1861 -- Recursive procedure to restore scope of all ancestors of subunit,
1862 -- from outermost in. If parent is not a subunit, the call to install
1863 -- context installs context of spec and (if parent is a child unit)
1864 -- the context of its parents as well. It is confusing that parents
1865 -- should be treated differently in both cases, but the semantics are
1866 -- just not identical.
1868 procedure Re_Install_Use_Clauses;
1869 -- As part of the removal of the parent scope, the use clauses are
1870 -- removed, to be reinstalled when the context of the subunit has
1871 -- been analyzed. Use clauses may also have been affected by the
1872 -- analysis of the context of the subunit, so they have to be applied
1873 -- again, to insure that the compilation environment of the rest of
1874 -- the parent unit is identical.
1876 procedure Remove_Scope;
1877 -- Remove current scope from scope stack, and preserve the list
1878 -- of use clauses in it, to be reinstalled after context is analyzed.
1880 -----------------------------
1881 -- Analyze_Subunit_Context --
1882 -----------------------------
1884 procedure Analyze_Subunit_Context is
1887 Unit_Name : Entity_Id;
1890 Analyze_Context (N);
1892 -- Make withed units immediately visible. If child unit, make the
1893 -- ultimate parent immediately visible.
1895 Item := First (Context_Items (N));
1896 while Present (Item) loop
1897 if Nkind (Item) = N_With_Clause then
1899 -- Protect frontend against previous errors in context clauses
1901 if Nkind (Name (Item)) /= N_Selected_Component then
1902 if Error_Posted (Item) then
1906 Unit_Name := Entity (Name (Item));
1907 while Is_Child_Unit (Unit_Name) loop
1908 Set_Is_Visible_Child_Unit (Unit_Name);
1909 Unit_Name := Scope (Unit_Name);
1912 if not Is_Immediately_Visible (Unit_Name) then
1913 Set_Is_Immediately_Visible (Unit_Name);
1914 Set_Context_Installed (Item);
1919 elsif Nkind (Item) = N_Use_Package_Clause then
1920 Nam := First (Names (Item));
1921 while Present (Nam) loop
1926 elsif Nkind (Item) = N_Use_Type_Clause then
1927 Nam := First (Subtype_Marks (Item));
1928 while Present (Nam) loop
1937 -- Reset visibility of withed units. They will be made visible
1938 -- again when we install the subunit context.
1940 Item := First (Context_Items (N));
1941 while Present (Item) loop
1942 if Nkind (Item) = N_With_Clause
1944 -- Protect frontend against previous errors in context clauses
1946 and then Nkind (Name (Item)) /= N_Selected_Component
1947 and then not Error_Posted (Item)
1949 Unit_Name := Entity (Name (Item));
1950 while Is_Child_Unit (Unit_Name) loop
1951 Set_Is_Visible_Child_Unit (Unit_Name, False);
1952 Unit_Name := Scope (Unit_Name);
1955 if Context_Installed (Item) then
1956 Set_Is_Immediately_Visible (Unit_Name, False);
1957 Set_Context_Installed (Item, False);
1963 end Analyze_Subunit_Context;
1965 ------------------------
1966 -- Re_Install_Parents --
1967 ------------------------
1969 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
1973 if Nkind (Unit (L)) = N_Subunit then
1974 Re_Install_Parents (Library_Unit (L), Scope (Scop));
1977 Install_Context (L);
1979 -- If the subunit occurs within a child unit, we must restore the
1980 -- immediate visibility of any siblings that may occur in context.
1982 if Present (Enclosing_Child) then
1983 Install_Siblings (Enclosing_Child, L);
1988 if Scop /= Par_Unit then
1989 Set_Is_Immediately_Visible (Scop);
1992 -- Make entities in scope visible again. For child units, restore
1993 -- visibility only if they are actually in context.
1995 E := First_Entity (Current_Scope);
1996 while Present (E) loop
1997 if not Is_Child_Unit (E)
1998 or else Is_Visible_Child_Unit (E)
2000 Set_Is_Immediately_Visible (E);
2006 -- A subunit appears within a body, and for a nested subunits
2007 -- all the parents are bodies. Restore full visibility of their
2008 -- private entities.
2010 if Is_Package_Or_Generic_Package (Scop) then
2011 Set_In_Package_Body (Scop);
2012 Install_Private_Declarations (Scop);
2014 end Re_Install_Parents;
2016 ----------------------------
2017 -- Re_Install_Use_Clauses --
2018 ----------------------------
2020 procedure Re_Install_Use_Clauses is
2023 for J in reverse 1 .. Num_Scopes loop
2024 U := Use_Clauses (J);
2025 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2026 Install_Use_Clauses (U, Force_Installation => True);
2028 end Re_Install_Use_Clauses;
2034 procedure Remove_Scope is
2038 Num_Scopes := Num_Scopes + 1;
2039 Use_Clauses (Num_Scopes) :=
2040 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2042 E := First_Entity (Current_Scope);
2043 while Present (E) loop
2044 Set_Is_Immediately_Visible (E, False);
2048 if Is_Child_Unit (Current_Scope) then
2049 Enclosing_Child := Current_Scope;
2055 -- Start of processing for Analyze_Subunit
2058 if not Is_Empty_List (Context_Items (N)) then
2060 -- Save current use clauses
2063 Remove_Context (Lib_Unit);
2065 -- Now remove parents and their context, including enclosing
2066 -- subunits and the outer parent body which is not a subunit.
2068 if Present (Lib_Spec) then
2069 Remove_Context (Lib_Spec);
2071 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2072 Lib_Spec := Library_Unit (Lib_Spec);
2074 Remove_Context (Lib_Spec);
2077 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2081 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2082 Remove_Context (Library_Unit (Lib_Spec));
2086 Set_Is_Immediately_Visible (Par_Unit, False);
2088 Analyze_Subunit_Context;
2090 Re_Install_Parents (Lib_Unit, Par_Unit);
2091 Set_Is_Immediately_Visible (Par_Unit);
2093 -- If the context includes a child unit of the parent of the
2094 -- subunit, the parent will have been removed from visibility,
2095 -- after compiling that cousin in the context. The visibility
2096 -- of the parent must be restored now. This also applies if the
2097 -- context includes another subunit of the same parent which in
2098 -- turn includes a child unit in its context.
2100 if Is_Package_Or_Generic_Package (Par_Unit) then
2101 if not Is_Immediately_Visible (Par_Unit)
2102 or else (Present (First_Entity (Par_Unit))
2103 and then not Is_Immediately_Visible
2104 (First_Entity (Par_Unit)))
2106 Set_Is_Immediately_Visible (Par_Unit);
2107 Install_Visible_Declarations (Par_Unit);
2108 Install_Private_Declarations (Par_Unit);
2112 Re_Install_Use_Clauses;
2113 Install_Context (N);
2115 -- Restore state of suppress flags for current body
2117 Scope_Suppress := Svg;
2119 -- If the subunit is within a child unit, then siblings of any
2120 -- parent unit that appear in the context clause of the subunit
2121 -- must also be made immediately visible.
2123 if Present (Enclosing_Child) then
2124 Install_Siblings (Enclosing_Child, N);
2129 Analyze (Proper_Body (Unit (N)));
2132 -- The subunit may contain a with_clause on a sibling of some
2133 -- ancestor. Removing the context will remove from visibility those
2134 -- ancestor child units, which must be restored to the visibility
2135 -- they have in the enclosing body.
2137 if Present (Enclosing_Child) then
2143 and then Is_Child_Unit (C)
2145 Set_Is_Immediately_Visible (C);
2146 Set_Is_Visible_Child_Unit (C);
2151 end Analyze_Subunit;
2153 ----------------------------
2154 -- Analyze_Task_Body_Stub --
2155 ----------------------------
2157 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2158 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2159 Loc : constant Source_Ptr := Sloc (N);
2162 Check_Stub_Level (N);
2164 -- First occurrence of name may have been as an incomplete type
2166 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2167 Nam := Full_View (Nam);
2171 or else not Is_Task_Type (Etype (Nam))
2173 Error_Msg_N ("missing specification for task body", N);
2175 Set_Scope (Defining_Entity (N), Current_Scope);
2176 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2177 Set_Has_Completion (Etype (Nam));
2178 Analyze_Proper_Body (N, Etype (Nam));
2180 -- Set elaboration flag to indicate that entity is callable.
2181 -- This cannot be done in the expansion of the body itself,
2182 -- because the proper body is not in a declarative part. This
2183 -- is only done if expansion is active, because the context
2184 -- may be generic and the flag not defined yet.
2186 if Expander_Active then
2188 Make_Assignment_Statement (Loc,
2190 Make_Identifier (Loc,
2191 New_External_Name (Chars (Etype (Nam)), 'E')),
2192 Expression => New_Reference_To (Standard_True, Loc)));
2196 end Analyze_Task_Body_Stub;
2198 -------------------------
2199 -- Analyze_With_Clause --
2200 -------------------------
2202 -- Analyze the declaration of a unit in a with clause. At end,
2203 -- label the with clause with the defining entity for the unit.
2205 procedure Analyze_With_Clause (N : Node_Id) is
2207 -- Retrieve the original kind of the unit node, before analysis.
2208 -- If it is a subprogram instantiation, its analysis below will
2209 -- rewrite as the declaration of the wrapper package. If the same
2210 -- instantiation appears indirectly elsewhere in the context, it
2211 -- will have been analyzed already.
2213 Unit_Kind : constant Node_Kind :=
2214 Nkind (Original_Node (Unit (Library_Unit (N))));
2215 Nam : constant Node_Id := Name (N);
2217 Par_Name : Entity_Id;
2222 -- Set True if the unit currently being compiled is an internal unit
2224 Save_Style_Check : constant Boolean := Opt.Style_Check;
2225 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
2226 Cunit_Boolean_Restrictions_Save;
2229 U := Unit (Library_Unit (N));
2231 -- Several actions are skipped for dummy packages (those supplied for
2232 -- with's where no matching file could be found). Such packages are
2233 -- identified by the Sloc value being set to No_Location.
2235 if Limited_Present (N) then
2237 -- Ada 2005 (AI-50217): Build visibility structures but do not
2238 -- analyze the unit.
2240 if Sloc (U) /= No_Location then
2241 Build_Limited_Views (N);
2247 -- We reset ordinary style checking during the analysis of a with'ed
2248 -- unit, but we do NOT reset GNAT special analysis mode (the latter
2249 -- definitely *does* apply to with'ed units).
2251 if not GNAT_Mode then
2252 Style_Check := False;
2255 -- If the library unit is a predefined unit, and we are in high
2256 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2257 -- for the analysis of the with'ed unit. This mode does not prevent
2258 -- explicit with'ing of run-time units.
2260 if Configurable_Run_Time_Mode
2262 Is_Predefined_File_Name
2263 (Unit_File_Name (Get_Source_Unit (Unit (Library_Unit (N)))))
2265 Configurable_Run_Time_Mode := False;
2266 Semantics (Library_Unit (N));
2267 Configurable_Run_Time_Mode := True;
2270 Semantics (Library_Unit (N));
2273 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2275 if Sloc (U) /= No_Location then
2277 -- Check restrictions, except that we skip the check if this is an
2278 -- internal unit unless we are compiling the internal unit as the
2279 -- main unit. We also skip this for dummy packages.
2281 Check_Restriction_No_Dependence (Nam, N);
2283 if not Intunit or else Current_Sem_Unit = Main_Unit then
2284 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2287 -- Deal with special case of GNAT.Current_Exceptions which interacts
2288 -- with the optimization of local raise statements into gotos.
2290 if Nkind (Nam) = N_Selected_Component
2291 and then Nkind (Prefix (Nam)) = N_Identifier
2292 and then Chars (Prefix (Nam)) = Name_Gnat
2293 and then (Chars (Selector_Name (Nam)) = Name_Most_Recent_Exception
2295 Chars (Selector_Name (Nam)) = Name_Exception_Traces)
2297 Check_Restriction (No_Exception_Propagation, N);
2298 Special_Exception_Package_Used := True;
2301 -- Check for inappropriate with of internal implementation unit if we
2302 -- are currently compiling the main unit and the main unit is itself
2303 -- not an internal unit. We do not issue this message for implicit
2304 -- with's generated by the compiler itself.
2306 if Implementation_Unit_Warnings
2307 and then Current_Sem_Unit = Main_Unit
2308 and then not Intunit
2309 and then not Implicit_With (N)
2310 and then not GNAT_Mode
2313 U_Kind : constant Kind_Of_Unit :=
2314 Get_Kind_Of_Unit (Get_Source_Unit (U));
2317 if U_Kind = Implementation_Unit then
2318 Error_Msg_F ("& is an internal 'G'N'A'T unit?", Name (N));
2320 ("\use of this unit is non-portable " &
2321 "and version-dependent?",
2324 elsif U_Kind = Ada_05_Unit
2325 and then Ada_Version < Ada_05
2326 and then Warn_On_Ada_2005_Compatibility
2328 Error_Msg_N ("& is an Ada 2005 unit?", Name (N));
2334 -- Semantic analysis of a generic unit is performed on a copy of
2335 -- the original tree. Retrieve the entity on which semantic info
2336 -- actually appears.
2338 if Unit_Kind in N_Generic_Declaration then
2339 E_Name := Defining_Entity (U);
2341 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2342 -- the case of an instantiation, semantic analysis above will have
2343 -- replaced the unit by its instantiated version. If the instance body
2344 -- has been generated, the instance now denotes the body entity. For
2345 -- visibility purposes we need the entity of its spec.
2347 elsif (Unit_Kind = N_Package_Instantiation
2348 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2349 N_Package_Instantiation)
2350 and then Nkind (U) = N_Package_Body
2352 E_Name := Corresponding_Spec (U);
2354 elsif Unit_Kind = N_Package_Instantiation
2355 and then Nkind (U) = N_Package_Instantiation
2357 -- If the instance has not been rewritten as a package declaration,
2358 -- then it appeared already in a previous with clause. Retrieve
2359 -- the entity from the previous instance.
2361 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2363 elsif Unit_Kind in N_Subprogram_Instantiation then
2365 -- Instantiation node is replaced with a wrapper package. Retrieve
2366 -- the visible subprogram created by the instance from corresponding
2367 -- attribute of the wrapper.
2369 E_Name := Related_Instance (Defining_Entity (U));
2371 elsif Unit_Kind = N_Package_Renaming_Declaration
2372 or else Unit_Kind in N_Generic_Renaming_Declaration
2374 E_Name := Defining_Entity (U);
2376 elsif Unit_Kind = N_Subprogram_Body
2377 and then Nkind (Name (N)) = N_Selected_Component
2378 and then not Acts_As_Spec (Library_Unit (N))
2380 -- For a child unit that has no spec, one has been created and
2381 -- analyzed. The entity required is that of the spec.
2383 E_Name := Corresponding_Spec (U);
2386 E_Name := Defining_Entity (U);
2389 if Nkind (Name (N)) = N_Selected_Component then
2391 -- Child unit in a with clause
2393 Change_Selected_Component_To_Expanded_Name (Name (N));
2396 -- Restore style checks and restrictions
2398 Style_Check := Save_Style_Check;
2399 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
2401 -- Record the reference, but do NOT set the unit as referenced, we want
2402 -- to consider the unit as unreferenced if this is the only reference
2405 Set_Entity_With_Style_Check (Name (N), E_Name);
2406 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2408 if Is_Child_Unit (E_Name) then
2409 Pref := Prefix (Name (N));
2410 Par_Name := Scope (E_Name);
2411 while Nkind (Pref) = N_Selected_Component loop
2412 Change_Selected_Component_To_Expanded_Name (Pref);
2413 Set_Entity_With_Style_Check (Pref, Par_Name);
2415 Generate_Reference (Par_Name, Pref);
2416 Pref := Prefix (Pref);
2418 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2419 -- is set to Standard_Standard, and no attempt should be made to
2420 -- further unwind scopes.
2422 if Par_Name /= Standard_Standard then
2423 Par_Name := Scope (Par_Name);
2427 if Present (Entity (Pref))
2428 and then not Analyzed (Parent (Parent (Entity (Pref))))
2430 -- If the entity is set without its unit being compiled, the
2431 -- original parent is a renaming, and Par_Name is the renamed
2432 -- entity. For visibility purposes, we need the original entity,
2433 -- which must be analyzed now because Load_Unit directly retrieves
2434 -- the renamed unit, and the renaming declaration itself has not
2437 Analyze (Parent (Parent (Entity (Pref))));
2438 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2439 Par_Name := Entity (Pref);
2442 Set_Entity_With_Style_Check (Pref, Par_Name);
2443 Generate_Reference (Par_Name, Pref);
2446 -- If the withed unit is System, and a system extension pragma is
2447 -- present, compile the extension now, rather than waiting for a
2448 -- visibility check on a specific entity.
2450 if Chars (E_Name) = Name_System
2451 and then Scope (E_Name) = Standard_Standard
2452 and then Present (System_Extend_Unit)
2453 and then Present_System_Aux (N)
2455 -- If the extension is not present, an error will have been emitted
2460 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2461 -- to private_with units; they will be made visible later (just before
2462 -- the private part is analyzed)
2464 if Private_Present (N) then
2465 Set_Is_Immediately_Visible (E_Name, False);
2467 end Analyze_With_Clause;
2469 ------------------------------
2470 -- Check_Private_Child_Unit --
2471 ------------------------------
2473 procedure Check_Private_Child_Unit (N : Node_Id) is
2474 Lib_Unit : constant Node_Id := Unit (N);
2476 Curr_Unit : Entity_Id;
2477 Sub_Parent : Node_Id;
2478 Priv_Child : Entity_Id;
2479 Par_Lib : Entity_Id;
2482 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2483 -- Returns true if and only if the library unit is declared with
2484 -- an explicit designation of private.
2486 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2487 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2490 return Private_Present (Comp_Unit);
2491 end Is_Private_Library_Unit;
2493 -- Start of processing for Check_Private_Child_Unit
2496 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2497 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2498 Par_Lib := Curr_Unit;
2500 elsif Nkind (Lib_Unit) = N_Subunit then
2502 -- The parent is itself a body. The parent entity is to be found in
2503 -- the corresponding spec.
2505 Sub_Parent := Library_Unit (N);
2506 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2508 -- If the parent itself is a subunit, Curr_Unit is the entity
2509 -- of the enclosing body, retrieve the spec entity which is
2510 -- the proper ancestor we need for the following tests.
2512 if Ekind (Curr_Unit) = E_Package_Body then
2513 Curr_Unit := Spec_Entity (Curr_Unit);
2516 Par_Lib := Curr_Unit;
2519 Curr_Unit := Defining_Entity (Lib_Unit);
2521 Par_Lib := Curr_Unit;
2522 Par_Spec := Parent_Spec (Lib_Unit);
2524 if No (Par_Spec) then
2527 Par_Lib := Defining_Entity (Unit (Par_Spec));
2531 -- Loop through context items
2533 Item := First (Context_Items (N));
2534 while Present (Item) loop
2536 -- Ada 2005 (AI-262): Allow private_with of a private child package
2537 -- in public siblings
2539 if Nkind (Item) = N_With_Clause
2540 and then not Implicit_With (Item)
2541 and then not Limited_Present (Item)
2542 and then Is_Private_Descendant (Entity (Name (Item)))
2544 Priv_Child := Entity (Name (Item));
2547 Curr_Parent : Entity_Id := Par_Lib;
2548 Child_Parent : Entity_Id := Scope (Priv_Child);
2549 Prv_Ancestor : Entity_Id := Child_Parent;
2550 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2553 -- If the child unit is a public child then locate the nearest
2554 -- private ancestor. Child_Parent will then be set to the
2555 -- parent of that ancestor.
2557 if not Is_Private_Library_Unit (Priv_Child) then
2558 while Present (Prv_Ancestor)
2559 and then not Is_Private_Library_Unit (Prv_Ancestor)
2561 Prv_Ancestor := Scope (Prv_Ancestor);
2564 if Present (Prv_Ancestor) then
2565 Child_Parent := Scope (Prv_Ancestor);
2569 while Present (Curr_Parent)
2570 and then Curr_Parent /= Standard_Standard
2571 and then Curr_Parent /= Child_Parent
2574 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2575 Curr_Parent := Scope (Curr_Parent);
2578 if No (Curr_Parent) then
2579 Curr_Parent := Standard_Standard;
2582 if Curr_Parent /= Child_Parent then
2583 if Ekind (Priv_Child) = E_Generic_Package
2584 and then Chars (Priv_Child) in Text_IO_Package_Name
2585 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2588 ("& is a nested package, not a compilation unit",
2589 Name (Item), Priv_Child);
2593 ("unit in with clause is private child unit!", Item);
2595 ("\current unit must also have parent&!",
2596 Item, Child_Parent);
2600 or else Private_Present (Item)
2601 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2602 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2603 and then not Acts_As_Spec (Parent (Lib_Unit)))
2609 ("current unit must also be private descendant of&",
2610 Item, Child_Parent);
2618 end Check_Private_Child_Unit;
2620 ----------------------
2621 -- Check_Stub_Level --
2622 ----------------------
2624 procedure Check_Stub_Level (N : Node_Id) is
2625 Par : constant Node_Id := Parent (N);
2626 Kind : constant Node_Kind := Nkind (Par);
2629 if Nkind_In (Kind, N_Package_Body,
2633 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
2637 -- In an instance, a missing stub appears at any level. A warning
2638 -- message will have been emitted already for the missing file.
2640 elsif not In_Instance then
2641 Error_Msg_N ("stub cannot appear in an inner scope", N);
2643 elsif Expander_Active then
2644 Error_Msg_N ("missing proper body", N);
2646 end Check_Stub_Level;
2648 ------------------------
2649 -- Expand_With_Clause --
2650 ------------------------
2652 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2653 Loc : constant Source_Ptr := Sloc (Nam);
2654 Ent : constant Entity_Id := Entity (Nam);
2658 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2659 -- Build name to be used in implicit with_clause. In most cases this
2660 -- is the source name, but if renamings are present we must make the
2661 -- original unit visible, not the one it renames. The entity in the
2662 -- with clause is the renamed unit, but the identifier is the one from
2663 -- the source, which allows us to recover the unit renaming.
2665 ---------------------
2666 -- Build_Unit_Name --
2667 ---------------------
2669 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2671 Renaming : Entity_Id;
2675 if Nkind (Nam) = N_Identifier then
2677 -- If the parent unit P in the name of the with_clause for P.Q
2678 -- is a renaming of package R, then the entity of the parent is
2679 -- set to R, but the identifier retains Chars (P) to be consistent
2680 -- with the source (see details in lib-load). However, the
2681 -- implicit_with_clause for the parent must make the entity for
2682 -- P visible, because P.Q may be used as a prefix within the
2683 -- current unit. The entity for P is the current_entity with that
2684 -- name, because the package renaming declaration for it has just
2685 -- been analyzed. Note that this case can only happen if P.Q has
2686 -- already appeared in a previous with_clause in a related unit,
2687 -- such as the library body of the current unit.
2689 if Chars (Nam) /= Chars (Entity (Nam)) then
2690 Renaming := Current_Entity (Nam);
2691 pragma Assert (Renamed_Entity (Renaming) = Entity (Nam));
2692 return New_Occurrence_Of (Renaming, Loc);
2695 return New_Occurrence_Of (Entity (Nam), Loc);
2699 Ent := Entity (Nam);
2701 if Present (Entity (Selector_Name (Nam)))
2702 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
2704 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
2705 = N_Package_Renaming_Declaration
2707 -- The name in the with_clause is of the form A.B.C, and B
2708 -- is given by a renaming declaration. In that case we may
2709 -- not have analyzed the unit for B, but replaced it directly
2710 -- in lib-load with the unit it renames. We have to make A.B
2711 -- visible, so analyze the declaration for B now, in case it
2712 -- has not been done yet.
2714 Ent := Entity (Selector_Name (Nam));
2717 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
2721 Make_Expanded_Name (Loc,
2722 Chars => Chars (Entity (Nam)),
2723 Prefix => Build_Unit_Name (Prefix (Nam)),
2724 Selector_Name => New_Occurrence_Of (Ent, Loc));
2725 Set_Entity (Result, Ent);
2728 end Build_Unit_Name;
2730 -- Start of processing for Expand_With_Clause
2733 New_Nodes_OK := New_Nodes_OK + 1;
2735 Make_With_Clause (Loc,
2736 Name => Build_Unit_Name (Nam));
2738 P := Parent (Unit_Declaration_Node (Ent));
2739 Set_Library_Unit (Withn, P);
2740 Set_Corresponding_Spec (Withn, Ent);
2741 Set_First_Name (Withn, True);
2742 Set_Implicit_With (Withn, True);
2744 -- If the unit is a package declaration, a private_with_clause on a
2745 -- child unit implies that the implicit with on the parent is also
2748 if Nkind (Unit (N)) = N_Package_Declaration then
2749 Set_Private_Present (Withn, Private_Present (Item));
2752 Prepend (Withn, Context_Items (N));
2753 Mark_Rewrite_Insertion (Withn);
2754 Install_Withed_Unit (Withn);
2756 if Nkind (Nam) = N_Expanded_Name then
2757 Expand_With_Clause (Item, Prefix (Nam), N);
2760 New_Nodes_OK := New_Nodes_OK - 1;
2761 end Expand_With_Clause;
2763 -----------------------
2764 -- Get_Parent_Entity --
2765 -----------------------
2767 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2769 if Nkind (Unit) = N_Package_Body
2770 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
2772 return Defining_Entity
2773 (Specification (Instance_Spec (Original_Node (Unit))));
2774 elsif Nkind (Unit) = N_Package_Instantiation then
2775 return Defining_Entity (Specification (Instance_Spec (Unit)));
2777 return Defining_Entity (Unit);
2779 end Get_Parent_Entity;
2781 -----------------------------
2782 -- Implicit_With_On_Parent --
2783 -----------------------------
2785 procedure Implicit_With_On_Parent
2786 (Child_Unit : Node_Id;
2789 Loc : constant Source_Ptr := Sloc (N);
2790 P : constant Node_Id := Parent_Spec (Child_Unit);
2791 P_Unit : Node_Id := Unit (P);
2792 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
2795 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2796 -- Build prefix of child unit name. Recurse if needed
2798 function Build_Unit_Name return Node_Id;
2799 -- If the unit is a child unit, build qualified name with all ancestors
2801 -------------------------
2802 -- Build_Ancestor_Name --
2803 -------------------------
2805 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
2806 P_Ref : constant Node_Id :=
2807 New_Reference_To (Defining_Entity (P), Loc);
2808 P_Spec : Node_Id := P;
2811 -- Ancestor may have been rewritten as a package body. Retrieve
2812 -- the original spec to trace earlier ancestors.
2814 if Nkind (P) = N_Package_Body
2815 and then Nkind (Original_Node (P)) = N_Package_Instantiation
2817 P_Spec := Original_Node (P);
2820 if No (Parent_Spec (P_Spec)) then
2824 Make_Selected_Component (Loc,
2825 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
2826 Selector_Name => P_Ref);
2828 end Build_Ancestor_Name;
2830 ---------------------
2831 -- Build_Unit_Name --
2832 ---------------------
2834 function Build_Unit_Name return Node_Id is
2837 if No (Parent_Spec (P_Unit)) then
2838 return New_Reference_To (P_Name, Loc);
2841 Make_Expanded_Name (Loc,
2842 Chars => Chars (P_Name),
2843 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
2844 Selector_Name => New_Reference_To (P_Name, Loc));
2845 Set_Entity (Result, P_Name);
2848 end Build_Unit_Name;
2850 -- Start of processing for Implicit_With_On_Parent
2853 -- The unit of the current compilation may be a package body that
2854 -- replaces an instance node. In this case we need the original instance
2855 -- node to construct the proper parent name.
2857 if Nkind (P_Unit) = N_Package_Body
2858 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
2860 P_Unit := Original_Node (P_Unit);
2863 -- We add the implicit with if the child unit is the current unit being
2864 -- compiled. If the current unit is a body, we do not want to add an
2865 -- implicit_with a second time to the corresponding spec.
2867 if Nkind (Child_Unit) = N_Package_Declaration
2868 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
2873 New_Nodes_OK := New_Nodes_OK + 1;
2874 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
2876 Set_Library_Unit (Withn, P);
2877 Set_Corresponding_Spec (Withn, P_Name);
2878 Set_First_Name (Withn, True);
2879 Set_Implicit_With (Withn, True);
2881 -- Node is placed at the beginning of the context items, so that
2882 -- subsequent use clauses on the parent can be validated.
2884 Prepend (Withn, Context_Items (N));
2885 Mark_Rewrite_Insertion (Withn);
2886 Install_Withed_Unit (Withn);
2888 if Is_Child_Spec (P_Unit) then
2889 Implicit_With_On_Parent (P_Unit, N);
2892 New_Nodes_OK := New_Nodes_OK - 1;
2893 end Implicit_With_On_Parent;
2899 function In_Chain (E : Entity_Id) return Boolean is
2903 H := Current_Entity (E);
2904 while Present (H) loop
2915 ---------------------
2916 -- Install_Context --
2917 ---------------------
2919 procedure Install_Context (N : Node_Id) is
2920 Lib_Unit : constant Node_Id := Unit (N);
2923 Install_Context_Clauses (N);
2925 if Is_Child_Spec (Lib_Unit) then
2926 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
2929 Install_Limited_Context_Clauses (N);
2930 end Install_Context;
2932 -----------------------------
2933 -- Install_Context_Clauses --
2934 -----------------------------
2936 procedure Install_Context_Clauses (N : Node_Id) is
2937 Lib_Unit : constant Node_Id := Unit (N);
2939 Uname_Node : Entity_Id;
2940 Check_Private : Boolean := False;
2941 Decl_Node : Node_Id;
2942 Lib_Parent : Entity_Id;
2945 -- First skip configuration pragmas at the start of the context. They
2946 -- are not technically part of the context clause, but that's where the
2947 -- parser puts them. Note they were analyzed in Analyze_Context.
2949 Item := First (Context_Items (N));
2950 while Present (Item)
2951 and then Nkind (Item) = N_Pragma
2952 and then Pragma_Name (Item) in Configuration_Pragma_Names
2957 -- Loop through the actual context clause items. We process everything
2958 -- except Limited_With clauses in this routine. Limited_With clauses
2959 -- are separately installed (see Install_Limited_Context_Clauses).
2961 while Present (Item) loop
2963 -- Case of explicit WITH clause
2965 if Nkind (Item) = N_With_Clause
2966 and then not Implicit_With (Item)
2968 if Limited_Present (Item) then
2970 -- Limited withed units will be installed later
2974 -- If Name (Item) is not an entity name, something is wrong, and
2975 -- this will be detected in due course, for now ignore the item
2977 elsif not Is_Entity_Name (Name (Item)) then
2980 elsif No (Entity (Name (Item))) then
2981 Set_Entity (Name (Item), Any_Id);
2985 Uname_Node := Entity (Name (Item));
2987 if Is_Private_Descendant (Uname_Node) then
2988 Check_Private := True;
2991 Install_Withed_Unit (Item);
2993 Decl_Node := Unit_Declaration_Node (Uname_Node);
2995 -- If the unit is a subprogram instance, it appears nested within
2996 -- a package that carries the parent information.
2998 if Is_Generic_Instance (Uname_Node)
2999 and then Ekind (Uname_Node) /= E_Package
3001 Decl_Node := Parent (Parent (Decl_Node));
3004 if Is_Child_Spec (Decl_Node) then
3005 if Nkind (Name (Item)) = N_Expanded_Name then
3006 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3008 -- If not an expanded name, the child unit must be a
3009 -- renaming, nothing to do.
3014 elsif Nkind (Decl_Node) = N_Subprogram_Body
3015 and then not Acts_As_Spec (Parent (Decl_Node))
3016 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3018 Implicit_With_On_Parent
3019 (Unit (Library_Unit (Parent (Decl_Node))), N);
3022 -- Check license conditions unless this is a dummy unit
3024 if Sloc (Library_Unit (Item)) /= No_Location then
3025 License_Check : declare
3027 Withu : constant Unit_Number_Type :=
3028 Get_Source_Unit (Library_Unit (Item));
3030 Withl : constant License_Type :=
3031 License (Source_Index (Withu));
3033 Unitl : constant License_Type :=
3034 License (Source_Index (Current_Sem_Unit));
3036 procedure License_Error;
3037 -- Signal error of bad license
3043 procedure License_Error is
3046 ("?license of with'ed unit & may be inconsistent",
3050 -- Start of processing for License_Check
3053 -- Exclude license check if withed unit is an internal unit.
3054 -- This situation arises e.g. with the GPL version of GNAT.
3056 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3059 -- Otherwise check various cases
3071 if Withl = Restricted then
3075 when Modified_GPL =>
3076 if Withl = Restricted or else Withl = GPL then
3080 when Unrestricted =>
3087 -- Case of USE PACKAGE clause
3089 elsif Nkind (Item) = N_Use_Package_Clause then
3090 Analyze_Use_Package (Item);
3092 -- Case of USE TYPE clause
3094 elsif Nkind (Item) = N_Use_Type_Clause then
3095 Analyze_Use_Type (Item);
3099 elsif Nkind (Item) = N_Pragma then
3107 if Is_Child_Spec (Lib_Unit) then
3109 -- The unit also has implicit withs on its own parents
3111 if No (Context_Items (N)) then
3112 Set_Context_Items (N, New_List);
3115 Implicit_With_On_Parent (Lib_Unit, N);
3118 -- If the unit is a body, the context of the specification must also
3121 if Nkind (Lib_Unit) = N_Package_Body
3122 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3123 and then not Acts_As_Spec (N))
3125 Install_Context (Library_Unit (N));
3127 if Is_Child_Spec (Unit (Library_Unit (N))) then
3129 -- If the unit is the body of a public child unit, the private
3130 -- declarations of the parent must be made visible. If the child
3131 -- unit is private, the private declarations have been installed
3132 -- already in the call to Install_Parents for the spec. Installing
3133 -- private declarations must be done for all ancestors of public
3134 -- child units. In addition, sibling units mentioned in the
3135 -- context clause of the body are directly visible.
3143 Lib_Spec := Unit (Library_Unit (N));
3144 while Is_Child_Spec (Lib_Spec) loop
3145 P := Unit (Parent_Spec (Lib_Spec));
3146 P_Name := Defining_Entity (P);
3148 if not (Private_Present (Parent (Lib_Spec)))
3149 and then not In_Private_Part (P_Name)
3151 Install_Private_Declarations (P_Name);
3152 Install_Private_With_Clauses (P_Name);
3153 Set_Use (Private_Declarations (Specification (P)));
3161 -- For a package body, children in context are immediately visible
3163 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3166 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3167 N_Generic_Subprogram_Declaration,
3168 N_Package_Declaration,
3169 N_Subprogram_Declaration)
3171 if Is_Child_Spec (Lib_Unit) then
3172 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3173 Set_Is_Private_Descendant
3174 (Defining_Entity (Lib_Unit),
3175 Is_Private_Descendant (Lib_Parent)
3176 or else Private_Present (Parent (Lib_Unit)));
3179 Set_Is_Private_Descendant
3180 (Defining_Entity (Lib_Unit),
3181 Private_Present (Parent (Lib_Unit)));
3185 if Check_Private then
3186 Check_Private_Child_Unit (N);
3188 end Install_Context_Clauses;
3190 -------------------------------------
3191 -- Install_Limited_Context_Clauses --
3192 -------------------------------------
3194 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3197 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3198 -- Check that the unlimited view of a given compilation_unit is not
3199 -- already visible through "use + renamings".
3201 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3202 -- Check that if a limited_with clause of a given compilation_unit
3203 -- mentions a descendant of a private child of some library unit,
3204 -- then the given compilation_unit shall be the declaration of a
3205 -- private descendant of that library unit, or a public descendant
3206 -- of such. The code is analogous to that of Check_Private_Child_Unit
3207 -- but we cannot use entities on the limited with_clauses because
3208 -- their units have not been analyzed, so we have to climb the tree
3209 -- of ancestors looking for private keywords.
3211 procedure Expand_Limited_With_Clause
3212 (Comp_Unit : Node_Id;
3215 -- If a child unit appears in a limited_with clause, there are implicit
3216 -- limited_with clauses on all parents that are not already visible
3217 -- through a regular with clause. This procedure creates the implicit
3218 -- limited with_clauses for the parents and loads the corresponding
3219 -- units. The shadow entities are created when the inserted clause is
3220 -- analyzed. Implements Ada 2005 (AI-50217).
3222 ---------------------
3223 -- Check_Renamings --
3224 ---------------------
3226 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3235 pragma Assert (Nkind (W) = N_With_Clause);
3237 -- Protect the frontend against previous critical errors
3239 case Nkind (Unit (Library_Unit (W))) is
3240 when N_Subprogram_Declaration |
3241 N_Package_Declaration |
3242 N_Generic_Subprogram_Declaration |
3243 N_Generic_Package_Declaration =>
3250 -- Check "use + renamings"
3252 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3253 Spec := Specification (Unit (P));
3255 Item := First (Visible_Declarations (Spec));
3256 while Present (Item) loop
3258 -- Look only at use package clauses
3260 if Nkind (Item) = N_Use_Package_Clause then
3262 -- Traverse the list of packages
3264 Nam := First (Names (Item));
3265 while Present (Nam) loop
3268 pragma Assert (Present (Parent (E)));
3270 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3271 and then Renamed_Entity (E) = WEnt
3273 -- The unlimited view is visible through use clause and
3274 -- renamings. There is not need to generate the error
3275 -- message here because Is_Visible_Through_Renamings
3276 -- takes care of generating the precise error message.
3280 elsif Nkind (Parent (E)) = N_Package_Specification then
3282 -- The use clause may refer to a local package.
3283 -- Check all the enclosing scopes.
3286 while E2 /= Standard_Standard
3294 ("unlimited view visible through use clause ", W);
3306 -- Recursive call to check all the ancestors
3308 if Is_Child_Spec (Unit (P)) then
3309 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3311 end Check_Renamings;
3313 ---------------------------------------
3314 -- Check_Private_Limited_Withed_Unit --
3315 ---------------------------------------
3317 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3318 Curr_Parent : Node_Id;
3319 Child_Parent : Node_Id;
3320 Curr_Private : Boolean;
3323 -- Compilation unit of the parent of the withed library unit
3325 Child_Parent := Library_Unit (Item);
3327 -- If the child unit is a public child, then locate its nearest
3328 -- private ancestor, if any; Child_Parent will then be set to
3329 -- the parent of that ancestor.
3331 if not Private_Present (Library_Unit (Item)) then
3332 while Present (Child_Parent)
3333 and then not Private_Present (Child_Parent)
3335 Child_Parent := Parent_Spec (Unit (Child_Parent));
3338 if No (Child_Parent) then
3343 Child_Parent := Parent_Spec (Unit (Child_Parent));
3345 -- Traverse all the ancestors of the current compilation
3346 -- unit to check if it is a descendant of named library unit.
3348 Curr_Parent := Parent (Item);
3349 Curr_Private := Private_Present (Curr_Parent);
3351 while Present (Parent_Spec (Unit (Curr_Parent)))
3352 and then Curr_Parent /= Child_Parent
3354 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3355 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3358 if Curr_Parent /= Child_Parent then
3360 ("unit in with clause is private child unit!", Item);
3362 ("\current unit must also have parent&!",
3363 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3365 elsif Private_Present (Parent (Item))
3366 or else Curr_Private
3367 or else Private_Present (Item)
3368 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3372 -- Current unit is private, of descendant of a private unit.
3378 ("current unit must also be private descendant of&",
3379 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3381 end Check_Private_Limited_Withed_Unit;
3383 --------------------------------
3384 -- Expand_Limited_With_Clause --
3385 --------------------------------
3387 procedure Expand_Limited_With_Clause
3388 (Comp_Unit : Node_Id;
3392 Loc : constant Source_Ptr := Sloc (Nam);
3393 Unum : Unit_Number_Type;
3396 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3397 -- Returns true if the context already includes a with_clause for
3398 -- this unit. If the with_clause is non-limited, the unit is fully
3399 -- visible and an implicit limited_with should not be created. If
3400 -- there is already a limited_with clause for W, a second one is
3401 -- simply redundant.
3403 --------------------------
3404 -- Previous_Withed_Unit --
3405 --------------------------
3407 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3411 -- A limited with_clause cannot appear in the same context_clause
3412 -- as a nonlimited with_clause which mentions the same library.
3414 Item := First (Context_Items (Comp_Unit));
3415 while Present (Item) loop
3416 if Nkind (Item) = N_With_Clause
3417 and then Library_Unit (Item) = Library_Unit (W)
3426 end Previous_Withed_Unit;
3428 -- Start of processing for Expand_Limited_With_Clause
3431 New_Nodes_OK := New_Nodes_OK + 1;
3433 if Nkind (Nam) = N_Identifier then
3435 -- Create node for name of withed unit
3438 Make_With_Clause (Loc,
3439 Name => New_Copy (Nam));
3441 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3443 Make_With_Clause (Loc,
3444 Name => Make_Selected_Component (Loc,
3445 Prefix => New_Copy_Tree (Prefix (Nam)),
3446 Selector_Name => New_Copy (Selector_Name (Nam))));
3447 Set_Parent (Withn, Parent (N));
3450 Set_Limited_Present (Withn);
3451 Set_First_Name (Withn);
3452 Set_Implicit_With (Withn);
3456 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3461 -- Do not generate a limited_with_clause on the current unit.
3462 -- This path is taken when a unit has a limited_with clause on
3463 -- one of its child units.
3465 if Unum = Current_Sem_Unit then
3469 Set_Library_Unit (Withn, Cunit (Unum));
3470 Set_Corresponding_Spec
3471 (Withn, Specification (Unit (Cunit (Unum))));
3473 if not Previous_Withed_Unit (Withn) then
3474 Prepend (Withn, Context_Items (Parent (N)));
3475 Mark_Rewrite_Insertion (Withn);
3477 -- Add implicit limited_with_clauses for parents of child units
3478 -- mentioned in limited_with clauses.
3480 if Nkind (Nam) = N_Selected_Component then
3481 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3486 if not Limited_View_Installed (Withn) then
3487 Install_Limited_Withed_Unit (Withn);
3491 New_Nodes_OK := New_Nodes_OK - 1;
3492 end Expand_Limited_With_Clause;
3494 -- Start of processing for Install_Limited_Context_Clauses
3497 Item := First (Context_Items (N));
3498 while Present (Item) loop
3499 if Nkind (Item) = N_With_Clause
3500 and then Limited_Present (Item)
3502 if Nkind (Name (Item)) = N_Selected_Component then
3503 Expand_Limited_With_Clause
3504 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3507 Check_Private_Limited_Withed_Unit (Item);
3509 if not Implicit_With (Item)
3510 and then Is_Child_Spec (Unit (N))
3512 Check_Renamings (Parent_Spec (Unit (N)), Item);
3515 -- A unit may have a limited with on itself if it has a limited
3516 -- with_clause on one of its child units. In that case it is
3517 -- already being compiled and it makes no sense to install its
3520 -- If the item is a limited_private_with_clause, install it if the
3521 -- current unit is a body or if it is a private child. Otherwise
3522 -- the private clause is installed before analyzing the private
3523 -- part of the current unit.
3525 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3526 and then not Limited_View_Installed (Item)
3528 if not Private_Present (Item)
3529 or else Private_Present (N)
3530 or else Nkind_In (Unit (N), N_Package_Body,
3534 Install_Limited_Withed_Unit (Item);
3538 -- All items other than Limited_With clauses are ignored (they were
3539 -- installed separately early on by Install_Context_Clause).
3548 -- Ada 2005 (AI-412): Examine the visible declarations of a package
3549 -- spec, looking for incomplete subtype declarations of incomplete
3550 -- types visible through a limited with clause.
3552 if Ada_Version >= Ada_05
3553 and then Analyzed (N)
3554 and then Nkind (Unit (N)) = N_Package_Declaration
3559 Non_Lim_View : Entity_Id;
3562 Decl := First (Visible_Declarations (Specification (Unit (N))));
3563 while Present (Decl) loop
3564 if Nkind (Decl) = N_Subtype_Declaration
3566 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3568 From_With_Type (Defining_Identifier (Decl))
3570 Def_Id := Defining_Identifier (Decl);
3571 Non_Lim_View := Non_Limited_View (Def_Id);
3573 if not Is_Incomplete_Type (Non_Lim_View) then
3575 -- Convert an incomplete subtype declaration into a
3576 -- corresponding non-limited view subtype declaration.
3577 -- This is usually the case when analyzing a body that
3578 -- has regular with-clauses, when the spec has limited
3581 -- If the non-limited view is still incomplete, it is
3582 -- the dummy entry already created, and the declaration
3583 -- cannot be reanalyzed. This is the case when installing
3584 -- a parent unit that has limited with-clauses.
3586 Set_Subtype_Indication (Decl,
3587 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3588 Set_Etype (Def_Id, Non_Lim_View);
3589 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3590 Set_Analyzed (Decl, False);
3592 -- Reanalyze the declaration, suppressing the call to
3593 -- Enter_Name to avoid duplicate names.
3595 Analyze_Subtype_Declaration
3605 end Install_Limited_Context_Clauses;
3607 ---------------------
3608 -- Install_Parents --
3609 ---------------------
3611 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3618 P := Unit (Parent_Spec (Lib_Unit));
3619 P_Name := Get_Parent_Entity (P);
3621 if Etype (P_Name) = Any_Type then
3625 if Ekind (P_Name) = E_Generic_Package
3626 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3627 N_Generic_Package_Declaration)
3628 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3631 ("child of a generic package must be a generic unit", Lib_Unit);
3633 elsif not Is_Package_Or_Generic_Package (P_Name) then
3635 ("parent unit must be package or generic package", Lib_Unit);
3636 raise Unrecoverable_Error;
3638 elsif Present (Renamed_Object (P_Name)) then
3639 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3640 raise Unrecoverable_Error;
3642 -- Verify that a child of an instance is itself an instance, or the
3643 -- renaming of one. Given that an instance that is a unit is replaced
3644 -- with a package declaration, check against the original node. The
3645 -- parent may be currently being instantiated, in which case it appears
3646 -- as a declaration, but the generic_parent is already established
3647 -- indicating that we deal with an instance.
3649 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3650 if Nkind (Lib_Unit) in N_Renaming_Declaration
3651 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3653 (Nkind (Lib_Unit) = N_Package_Declaration
3654 and then Present (Generic_Parent (Specification (Lib_Unit))))
3659 ("child of an instance must be an instance or renaming",
3664 -- This is the recursive call that ensures all parents are loaded
3666 if Is_Child_Spec (P) then
3668 Is_Private or else Private_Present (Parent (Lib_Unit)));
3671 -- Now we can install the context for this parent
3673 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3674 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3675 Install_Siblings (P_Name, Parent (Lib_Unit));
3677 -- The child unit is in the declarative region of the parent. The parent
3678 -- must therefore appear in the scope stack and be visible, as when
3679 -- compiling the corresponding body. If the child unit is private or it
3680 -- is a package body, private declarations must be accessible as well.
3681 -- Use declarations in the parent must also be installed. Finally, other
3682 -- child units of the same parent that are in the context are
3683 -- immediately visible.
3685 -- Find entity for compilation unit, and set its private descendant
3686 -- status as needed.
3688 E_Name := Defining_Entity (Lib_Unit);
3690 Set_Is_Child_Unit (E_Name);
3692 Set_Is_Private_Descendant (E_Name,
3693 Is_Private_Descendant (P_Name)
3694 or else Private_Present (Parent (Lib_Unit)));
3696 P_Spec := Specification (Unit_Declaration_Node (P_Name));
3697 Push_Scope (P_Name);
3699 -- Save current visibility of unit
3701 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3702 Is_Immediately_Visible (P_Name);
3703 Set_Is_Immediately_Visible (P_Name);
3704 Install_Visible_Declarations (P_Name);
3705 Set_Use (Visible_Declarations (P_Spec));
3707 -- If the parent is a generic unit, its formal part may contain formal
3708 -- packages and use clauses for them.
3710 if Ekind (P_Name) = E_Generic_Package then
3711 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3715 or else Private_Present (Parent (Lib_Unit))
3717 Install_Private_Declarations (P_Name);
3718 Install_Private_With_Clauses (P_Name);
3719 Set_Use (Private_Declarations (P_Spec));
3721 end Install_Parents;
3723 ----------------------------------
3724 -- Install_Private_With_Clauses --
3725 ----------------------------------
3727 procedure Install_Private_With_Clauses (P : Entity_Id) is
3728 Decl : constant Node_Id := Unit_Declaration_Node (P);
3732 if Debug_Flag_I then
3733 Write_Str ("install private with clauses of ");
3734 Write_Name (Chars (P));
3738 if Nkind (Parent (Decl)) = N_Compilation_Unit then
3739 Item := First (Context_Items (Parent (Decl)));
3740 while Present (Item) loop
3741 if Nkind (Item) = N_With_Clause
3742 and then Private_Present (Item)
3744 if Limited_Present (Item) then
3745 if not Limited_View_Installed (Item) then
3746 Install_Limited_Withed_Unit (Item);
3749 Install_Withed_Unit (Item, Private_With_OK => True);
3756 end Install_Private_With_Clauses;
3758 ----------------------
3759 -- Install_Siblings --
3760 ----------------------
3762 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3768 -- Iterate over explicit with clauses, and check whether the scope of
3769 -- each entity is an ancestor of the current unit, in which case it is
3770 -- immediately visible.
3772 Item := First (Context_Items (N));
3773 while Present (Item) loop
3775 -- Do not install private_with_clauses declaration, unless
3776 -- unit is itself a private child unit, or is a body.
3777 -- Note that for a subprogram body the private_with_clause does
3778 -- not take effect until after the specification.
3780 if Nkind (Item) /= N_With_Clause
3781 or else Implicit_With (Item)
3782 or else Limited_Present (Item)
3786 elsif not Private_Present (Item)
3787 or else Private_Present (N)
3788 or else Nkind (Unit (N)) = N_Package_Body
3790 Id := Entity (Name (Item));
3792 if Is_Child_Unit (Id)
3793 and then Is_Ancestor_Package (Scope (Id), U_Name)
3795 Set_Is_Immediately_Visible (Id);
3797 -- Check for the presence of another unit in the context,
3798 -- that may be inadvertently hidden by the child.
3800 Prev := Current_Entity (Id);
3803 and then Is_Immediately_Visible (Prev)
3804 and then not Is_Child_Unit (Prev)
3810 Clause := First (Context_Items (N));
3811 while Present (Clause) loop
3812 if Nkind (Clause) = N_With_Clause
3813 and then Entity (Name (Clause)) = Prev
3816 ("child unit& hides compilation unit " &
3817 "with the same name?",
3827 -- The With_Clause may be on a grand-child or one of its further
3828 -- descendants, which makes a child immediately visible. Examine
3829 -- ancestry to determine whether such a child exists. For example,
3830 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
3831 -- is immediately visible.
3833 elsif Is_Child_Unit (Id) then
3839 while Is_Child_Unit (Par) loop
3840 if Is_Ancestor_Package (Scope (Par), U_Name) then
3841 Set_Is_Immediately_Visible (Par);
3850 -- If the item is a private with-clause on a child unit, the parent
3851 -- may have been installed already, but the child unit must remain
3852 -- invisible until installed in a private part or body.
3854 elsif Private_Present (Item) then
3855 Id := Entity (Name (Item));
3857 if Is_Child_Unit (Id) then
3858 Set_Is_Visible_Child_Unit (Id, False);
3864 end Install_Siblings;
3866 ---------------------------------
3867 -- Install_Limited_Withed_Unit --
3868 ---------------------------------
3870 procedure Install_Limited_Withed_Unit (N : Node_Id) is
3871 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
3874 Is_Child_Package : Boolean := False;
3875 Lim_Header : Entity_Id;
3876 Lim_Typ : Entity_Id;
3878 procedure Check_Body_Required;
3879 -- A unit mentioned in a limited with_clause may not be mentioned in
3880 -- a regular with_clause, but must still be included in the current
3881 -- partition. We need to determine whether the unit needs a body, so
3882 -- that the binder can determine the name of the file to be compiled.
3883 -- Checking whether a unit needs a body can be done without semantic
3884 -- analysis, by examining the nature of the declarations in the package.
3886 function Has_Limited_With_Clause
3887 (C_Unit : Entity_Id;
3888 Pack : Entity_Id) return Boolean;
3889 -- Determine whether any package in the ancestor chain starting with
3890 -- C_Unit has a limited with clause for package Pack.
3892 function Has_With_Clause
3895 Is_Limited : Boolean := False) return Boolean;
3896 -- Determine whether compilation unit C_Unit contains a with clause
3897 -- for package Pack. Use flag Is_Limited to designate desired clause
3898 -- kind. This is a subsidiary routine to Has_Limited_With_Clause.
3900 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
3901 -- Check if some package installed though normal with-clauses has a
3902 -- renaming declaration of package P. AARM 10.1.2(21/2).
3904 -------------------------
3905 -- Check_Body_Required --
3906 -------------------------
3908 procedure Check_Body_Required is
3909 PA : constant List_Id :=
3910 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
3912 procedure Check_Declarations (Spec : Node_Id);
3913 -- Recursive procedure that does the work and checks nested packages
3915 ------------------------
3916 -- Check_Declarations --
3917 ------------------------
3919 procedure Check_Declarations (Spec : Node_Id) is
3921 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
3923 Subp_List : constant Elist_Id := New_Elmt_List;
3925 procedure Check_Pragma_Import (P : Node_Id);
3926 -- If a pragma import applies to a previous subprogram, the
3927 -- enclosing unit may not need a body. The processing is syntactic
3928 -- and does not require a declaration to be analyzed. The code
3929 -- below also handles pragma Import when applied to a subprogram
3930 -- that renames another. In this case the pragma applies to the
3933 -- Chains of multiple renames are not handled by the code below.
3934 -- It is probably impossible to handle all cases without proper
3935 -- name resolution. In such cases the algorithm is conservative
3936 -- and will indicate that a body is needed???
3938 -------------------------
3939 -- Check_Pragma_Import --
3940 -------------------------
3942 procedure Check_Pragma_Import (P : Node_Id) is
3948 procedure Remove_Homonyms (E : Node_Id);
3949 -- Make one pass over list of subprograms. Called again if
3950 -- subprogram is a renaming. E is known to be an identifier.
3952 ---------------------
3953 -- Remove_Homonyms --
3954 ---------------------
3956 procedure Remove_Homonyms (E : Node_Id) is
3957 R : Entity_Id := Empty;
3958 -- Name of renamed entity, if any
3961 Subp_Id := First_Elmt (Subp_List);
3962 while Present (Subp_Id) loop
3963 if Chars (Node (Subp_Id)) = Chars (E) then
3964 if Nkind (Parent (Parent (Node (Subp_Id))))
3965 /= N_Subprogram_Renaming_Declaration
3968 Next_Elmt (Subp_Id);
3969 Remove_Elmt (Subp_List, Prev_Id);
3971 R := Name (Parent (Parent (Node (Subp_Id))));
3975 Next_Elmt (Subp_Id);
3980 if Nkind (R) = N_Identifier then
3981 Remove_Homonyms (R);
3983 elsif Nkind (R) = N_Selected_Component then
3984 Remove_Homonyms (Selector_Name (R));
3986 -- Renaming of attribute
3992 end Remove_Homonyms;
3994 -- Start of processing for Check_Pragma_Import
3997 -- Find name of entity in Import pragma. We have not analyzed
3998 -- the construct, so we must guard against syntax errors.
4000 Arg := Next (First (Pragma_Argument_Associations (P)));
4003 or else Nkind (Expression (Arg)) /= N_Identifier
4007 Imported := Expression (Arg);
4010 Remove_Homonyms (Imported);
4011 end Check_Pragma_Import;
4013 -- Start of processing for Check_Declarations
4016 -- Search for Elaborate Body pragma
4018 Decl := First (Visible_Declarations (Spec));
4019 while Present (Decl)
4020 and then Nkind (Decl) = N_Pragma
4022 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4023 Set_Body_Required (Library_Unit (N));
4030 -- Look for declarations that require the presence of a body
4032 while Present (Decl) loop
4034 -- Subprogram that comes from source means body may be needed.
4035 -- Save for subsequent examination of import pragmas.
4037 if Comes_From_Source (Decl)
4038 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4039 N_Subprogram_Renaming_Declaration,
4040 N_Generic_Subprogram_Declaration))
4042 Append_Elmt (Defining_Entity (Decl), Subp_List);
4044 -- Package declaration of generic package declaration. We need
4045 -- to recursively examine nested declarations.
4047 elsif Nkind_In (Decl, N_Package_Declaration,
4048 N_Generic_Package_Declaration)
4050 Check_Declarations (Specification (Decl));
4052 elsif Nkind (Decl) = N_Pragma
4053 and then Pragma_Name (Decl) = Name_Import
4055 Check_Pragma_Import (Decl);
4061 -- Same set of tests for private part. In addition to subprograms
4062 -- detect the presence of Taft Amendment types (incomplete types
4063 -- completed in the body).
4065 Decl := First (Private_Declarations (Spec));
4066 while Present (Decl) loop
4067 if Comes_From_Source (Decl)
4068 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4069 N_Subprogram_Renaming_Declaration,
4070 N_Generic_Subprogram_Declaration))
4072 Append_Elmt (Defining_Entity (Decl), Subp_List);
4074 elsif Nkind_In (Decl, N_Package_Declaration,
4075 N_Generic_Package_Declaration)
4077 Check_Declarations (Specification (Decl));
4079 -- Collect incomplete type declarations for separate pass
4081 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4082 Append_Elmt (Decl, Incomplete_Decls);
4084 elsif Nkind (Decl) = N_Pragma
4085 and then Pragma_Name (Decl) = Name_Import
4087 Check_Pragma_Import (Decl);
4093 -- Now check incomplete declarations to locate Taft amendment
4094 -- types. This can be done by examining the defining identifiers
4095 -- of type declarations without real semantic analysis.
4101 Inc := First_Elmt (Incomplete_Decls);
4102 while Present (Inc) loop
4103 Decl := Next (Node (Inc));
4104 while Present (Decl) loop
4105 if Nkind (Decl) = N_Full_Type_Declaration
4106 and then Chars (Defining_Identifier (Decl)) =
4107 Chars (Defining_Identifier (Node (Inc)))
4115 -- If no completion, this is a TAT, and a body is needed
4118 Set_Body_Required (Library_Unit (N));
4126 -- Finally, check whether there are subprograms that still
4129 if not Is_Empty_Elmt_List (Subp_List) then
4134 Subp_Id := First_Elmt (Subp_List);
4136 while Present (Subp_Id) loop
4137 if Nkind (Parent (Parent (Node (Subp_Id))))
4138 /= N_Subprogram_Renaming_Declaration
4140 Set_Body_Required (Library_Unit (N));
4144 Next_Elmt (Subp_Id);
4148 end Check_Declarations;
4150 -- Start of processing for Check_Body_Required
4153 -- If this is an imported package (Java and CIL usage) no body is
4154 -- needed. Scan list of pragmas that may follow a compilation unit
4155 -- to look for a relevant pragma Import.
4157 if Present (PA) then
4163 while Present (Prag) loop
4164 if Nkind (Prag) = N_Pragma
4165 and then Get_Pragma_Id (Prag) = Pragma_Import
4175 Check_Declarations (Specification (P_Unit));
4176 end Check_Body_Required;
4178 -----------------------------
4179 -- Has_Limited_With_Clause --
4180 -----------------------------
4182 function Has_Limited_With_Clause
4183 (C_Unit : Entity_Id;
4184 Pack : Entity_Id) return Boolean
4191 while Present (Par) loop
4192 if Ekind (Par) /= E_Package then
4196 -- Retrieve the Compilation_Unit node for Par and determine if
4197 -- its context clauses contain a limited with for Pack.
4199 Par_Unit := Parent (Parent (Parent (Par)));
4201 if Nkind (Par_Unit) = N_Package_Declaration then
4202 Par_Unit := Parent (Par_Unit);
4205 if Has_With_Clause (Par_Unit, Pack, True) then
4209 -- If there are more ancestors, climb up the tree, otherwise
4212 if Is_Child_Unit (Par) then
4220 end Has_Limited_With_Clause;
4222 ---------------------
4223 -- Has_With_Clause --
4224 ---------------------
4226 function Has_With_Clause
4229 Is_Limited : Boolean := False) return Boolean
4235 if Present (Context_Items (C_Unit)) then
4236 Item := First (Context_Items (C_Unit));
4237 while Present (Item) loop
4238 if Nkind (Item) = N_With_Clause then
4240 -- Retrieve the entity of the imported compilation unit
4242 if Nkind (Name (Item)) = N_Selected_Component then
4243 Nam := Entity (Selector_Name (Name (Item)));
4245 Nam := Entity (Name (Item));
4250 ((Is_Limited and then Limited_Present (Item))
4252 (not Is_Limited and then not Limited_Present (Item)))
4263 end Has_With_Clause;
4265 ----------------------------------
4266 -- Is_Visible_Through_Renamings --
4267 ----------------------------------
4269 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4270 Kind : constant Node_Kind :=
4271 Nkind (Unit (Cunit (Current_Sem_Unit)));
4277 -- Example of the error detected by this subprogram:
4285 -- package Ren_P renames P;
4291 -- limited with P; -- ERROR
4292 -- package R.C is ...
4294 Aux_Unit := Cunit (Current_Sem_Unit);
4297 Item := First (Context_Items (Aux_Unit));
4298 while Present (Item) loop
4299 if Nkind (Item) = N_With_Clause
4300 and then not Limited_Present (Item)
4301 and then Nkind (Unit (Library_Unit (Item))) =
4302 N_Package_Declaration
4305 First (Visible_Declarations
4306 (Specification (Unit (Library_Unit (Item)))));
4307 while Present (Decl) loop
4308 if Nkind (Decl) = N_Package_Renaming_Declaration
4309 and then Entity (Name (Decl)) = P
4311 -- Generate the error message only if the current unit
4312 -- is a package declaration; in case of subprogram
4313 -- bodies and package bodies we just return true to
4314 -- indicate that the limited view must not be
4317 if Kind = N_Package_Declaration then
4319 ("simultaneous visibility of the limited and " &
4320 "unlimited views not allowed", N);
4321 Error_Msg_Sloc := Sloc (Item);
4323 ("\\ unlimited view of & visible through the " &
4324 "context clause #", N, P);
4325 Error_Msg_Sloc := Sloc (Decl);
4326 Error_Msg_NE ("\\ and the renaming #", N, P);
4339 if Present (Library_Unit (Aux_Unit)) then
4340 if Aux_Unit = Library_Unit (Aux_Unit) then
4342 -- Aux_Unit is a body that acts as a spec. Clause has
4343 -- already been flagged as illegal.
4348 Aux_Unit := Library_Unit (Aux_Unit);
4351 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4354 exit when No (Aux_Unit);
4358 end Is_Visible_Through_Renamings;
4360 -- Start of processing for Install_Limited_Withed_Unit
4363 pragma Assert (not Limited_View_Installed (N));
4365 -- In case of limited with_clause on subprograms, generics, instances,
4366 -- or renamings, the corresponding error was previously posted and we
4367 -- have nothing to do here. If the file is missing altogether, it has
4368 -- no source location.
4370 if Nkind (P_Unit) /= N_Package_Declaration
4371 or else Sloc (P_Unit) = No_Location
4376 P := Defining_Unit_Name (Specification (P_Unit));
4378 -- Handle child packages
4380 if Nkind (P) = N_Defining_Program_Unit_Name then
4381 Is_Child_Package := True;
4382 P := Defining_Identifier (P);
4385 -- Do not install the limited-view if the full-view is already visible
4386 -- through renaming declarations.
4388 if Is_Visible_Through_Renamings (P) then
4392 -- Do not install the limited view if this is the unit being analyzed.
4393 -- This unusual case will happen when a unit has a limited_with clause
4394 -- on one of its children. The compilation of the child forces the
4395 -- load of the parent which tries to install the limited view of the
4396 -- child again. Installing the limited view must also be disabled
4397 -- when compiling the body of the child unit.
4399 if P = Cunit_Entity (Current_Sem_Unit)
4401 (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4402 and then P = Main_Unit_Entity)
4407 -- This scenario is similar to the one above, the difference is that
4408 -- the compilation of sibling Par.Sib forces the load of parent Par
4409 -- which tries to install the limited view of Lim_Pack [1]. However
4410 -- Par.Sib has a with clause for Lim_Pack [2] in its body, and thus
4411 -- needs the non-limited views of all entities from Lim_Pack.
4413 -- limited with Lim_Pack; -- [1]
4414 -- package Par is ... package Lim_Pack is ...
4416 -- with Lim_Pack; -- [2]
4417 -- package Par.Sib is ... package body Par.Sib is ...
4419 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4420 -- Sem_Unit is the body of Par.Sib.
4422 if Ekind (P) = E_Package
4423 and then Ekind (Main_Unit_Entity) = E_Package
4424 and then Is_Child_Unit (Main_Unit_Entity)
4426 -- The body has a regular with clause
4428 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4429 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4431 -- One of the ancestors has a limited with clause
4433 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4434 N_Package_Specification
4435 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4440 -- A common use of the limited-with is to have a limited-with
4441 -- in the package spec, and a normal with in its package body.
4444 -- limited with X; -- [1]
4448 -- package body A is ...
4450 -- The compilation of A's body installs the context clauses found at [2]
4451 -- and then the context clauses of its specification (found at [1]). As
4452 -- a consequence, at [1] the specification of X has been analyzed and it
4453 -- is immediately visible. According to the semantics of limited-with
4454 -- context clauses we don't install the limited view because the full
4455 -- view of X supersedes its limited view.
4457 if Analyzed (P_Unit)
4459 (Is_Immediately_Visible (P)
4461 (Is_Child_Package and then Is_Visible_Child_Unit (P)))
4466 if Debug_Flag_I then
4467 Write_Str ("install limited view of ");
4468 Write_Name (Chars (P));
4472 -- If the unit has not been analyzed and the limited view has not been
4473 -- already installed then we install it.
4475 if not Analyzed (P_Unit) then
4476 if not In_Chain (P) then
4478 -- Minimum decoration
4480 Set_Ekind (P, E_Package);
4481 Set_Etype (P, Standard_Void_Type);
4482 Set_Scope (P, Standard_Standard);
4484 if Is_Child_Package then
4485 Set_Is_Child_Unit (P);
4486 Set_Is_Visible_Child_Unit (P);
4487 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4490 -- Place entity on visibility structure
4492 Set_Homonym (P, Current_Entity (P));
4493 Set_Current_Entity (P);
4495 if Debug_Flag_I then
4496 Write_Str (" (homonym) chain ");
4497 Write_Name (Chars (P));
4501 -- Install the incomplete view. The first element of the limited
4502 -- view is a header (an E_Package entity) used to reference the
4503 -- first shadow entity in the private part of the package.
4505 Lim_Header := Limited_View (P);
4506 Lim_Typ := First_Entity (Lim_Header);
4508 while Present (Lim_Typ)
4509 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4511 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4512 Set_Current_Entity (Lim_Typ);
4514 if Debug_Flag_I then
4515 Write_Str (" (homonym) chain ");
4516 Write_Name (Chars (Lim_Typ));
4520 Next_Entity (Lim_Typ);
4524 -- If the unit appears in a previous regular with_clause, the regular
4525 -- entities of the public part of the withed package must be replaced
4526 -- by the shadow ones.
4528 -- This code must be kept synchronized with the code that replaces the
4529 -- shadow entities by the real entities (see body of Remove_Limited
4530 -- With_Clause); otherwise the contents of the homonym chains are not
4534 -- Hide all the type entities of the public part of the package to
4535 -- avoid its usage. This is needed to cover all the subtype decla-
4536 -- rations because we do not remove them from the homonym chain.
4538 E := First_Entity (P);
4539 while Present (E) and then E /= First_Private_Entity (P) loop
4541 Set_Was_Hidden (E, Is_Hidden (E));
4548 -- Replace the real entities by the shadow entities of the limited
4549 -- view. The first element of the limited view is a header that is
4550 -- used to reference the first shadow entity in the private part
4551 -- of the package. Successive elements are the limited views of the
4552 -- type (including regular incomplete types) declared in the package.
4554 Lim_Header := Limited_View (P);
4556 Lim_Typ := First_Entity (Lim_Header);
4557 while Present (Lim_Typ)
4558 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4560 pragma Assert (not In_Chain (Lim_Typ));
4562 -- Do not unchain nested packages and child units
4564 if Ekind (Lim_Typ) /= E_Package
4565 and then not Is_Child_Unit (Lim_Typ)
4571 Prev := Current_Entity (Lim_Typ);
4574 -- Replace E in the homonyms list, so that the limited
4575 -- view becomes available.
4577 if E = Non_Limited_View (Lim_Typ) then
4578 Set_Homonym (Lim_Typ, Homonym (Prev));
4579 Set_Current_Entity (Lim_Typ);
4583 E := Homonym (Prev);
4585 -- E may have been removed when installing a
4586 -- previous limited_with_clause.
4590 exit when E = Non_Limited_View (Lim_Typ);
4592 Prev := Homonym (Prev);
4596 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4597 Set_Homonym (Prev, Lim_Typ);
4602 if Debug_Flag_I then
4603 Write_Str (" (homonym) chain ");
4604 Write_Name (Chars (Lim_Typ));
4609 Next_Entity (Lim_Typ);
4613 -- The package must be visible while the limited-with clause is active
4614 -- because references to the type P.T must resolve in the usual way.
4615 -- In addition, we remember that the limited-view has been installed to
4616 -- uninstall it at the point of context removal.
4618 Set_Is_Immediately_Visible (P);
4619 Set_Limited_View_Installed (N);
4621 -- If unit has not been analyzed in some previous context, check
4622 -- (imperfectly ???) whether it might need a body.
4624 if not Analyzed (P_Unit) then
4625 Check_Body_Required;
4628 -- If the package in the limited_with clause is a child unit, the
4629 -- clause is unanalyzed and appears as a selected component. Recast
4630 -- it as an expanded name so that the entity can be properly set. Use
4631 -- entity of parent, if available, for higher ancestors in the name.
4633 if Nkind (Name (N)) = N_Selected_Component then
4641 while Nkind (Nam) = N_Selected_Component
4642 and then Present (Ent)
4644 Change_Selected_Component_To_Expanded_Name (Nam);
4646 -- Set entity of parent identifiers if the unit is a child
4647 -- unit. This ensures that the tree is properly formed from
4648 -- semantic point of view (e.g. for ASIS queries).
4650 Set_Entity (Nam, Ent);
4652 Nam := Prefix (Nam);
4655 -- Set entity of last ancestor
4657 if Nkind (Nam) = N_Identifier then
4658 Set_Entity (Nam, Ent);
4664 Set_Entity (Name (N), P);
4665 Set_From_With_Type (P);
4666 end Install_Limited_Withed_Unit;
4668 -------------------------
4669 -- Install_Withed_Unit --
4670 -------------------------
4672 procedure Install_Withed_Unit
4673 (With_Clause : Node_Id;
4674 Private_With_OK : Boolean := False)
4676 Uname : constant Entity_Id := Entity (Name (With_Clause));
4677 P : constant Entity_Id := Scope (Uname);
4680 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
4681 -- compiling a package declaration and the Private_With_OK flag was not
4682 -- set by the caller. These declarations will be installed later (before
4683 -- analyzing the private part of the package).
4685 if Private_Present (With_Clause)
4686 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
4687 and then not (Private_With_OK)
4692 if Debug_Flag_I then
4693 if Private_Present (With_Clause) then
4694 Write_Str ("install private withed unit ");
4696 Write_Str ("install withed unit ");
4699 Write_Name (Chars (Uname));
4703 -- We do not apply the restrictions to an internal unit unless
4704 -- we are compiling the internal unit as a main unit. This check
4705 -- is also skipped for dummy units (for missing packages).
4707 if Sloc (Uname) /= No_Location
4708 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
4709 or else Current_Sem_Unit = Main_Unit)
4711 Check_Restricted_Unit
4712 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
4715 if P /= Standard_Standard then
4717 -- If the unit is not analyzed after analysis of the with clause and
4718 -- it is an instantiation then it awaits a body and is the main unit.
4719 -- Its appearance in the context of some other unit indicates a
4720 -- circular dependency (DEC suite perversity).
4722 if not Analyzed (Uname)
4723 and then Nkind (Parent (Uname)) = N_Package_Instantiation
4726 ("instantiation depends on itself", Name (With_Clause));
4728 elsif not Is_Visible_Child_Unit (Uname) then
4729 Set_Is_Visible_Child_Unit (Uname);
4731 -- If the child unit appears in the context of its parent, it is
4732 -- immediately visible.
4734 if In_Open_Scopes (Scope (Uname)) then
4735 Set_Is_Immediately_Visible (Uname);
4738 if Is_Generic_Instance (Uname)
4739 and then Ekind (Uname) in Subprogram_Kind
4741 -- Set flag as well on the visible entity that denotes the
4742 -- instance, which renames the current one.
4744 Set_Is_Visible_Child_Unit
4746 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
4749 -- The parent unit may have been installed already, and may have
4750 -- appeared in a use clause.
4752 if In_Use (Scope (Uname)) then
4753 Set_Is_Potentially_Use_Visible (Uname);
4756 Set_Context_Installed (With_Clause);
4759 elsif not Is_Immediately_Visible (Uname) then
4760 if not Private_Present (With_Clause)
4761 or else Private_With_OK
4763 Set_Is_Immediately_Visible (Uname);
4766 Set_Context_Installed (With_Clause);
4769 -- A with-clause overrides a with-type clause: there are no restric-
4770 -- tions on the use of package entities.
4772 if Ekind (Uname) = E_Package then
4773 Set_From_With_Type (Uname, False);
4776 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
4777 -- unit if there is a visible homograph for it declared in the same
4778 -- declarative region. This pathological case can only arise when an
4779 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
4780 -- G1 has a generic child also named G2, and the context includes with_
4781 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
4782 -- of I1.G2 visible as well. If the child unit is named Standard, do
4783 -- not apply the check to the Standard package itself.
4785 if Is_Child_Unit (Uname)
4786 and then Is_Visible_Child_Unit (Uname)
4787 and then Ada_Version >= Ada_05
4790 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
4796 U2 := Homonym (Uname);
4798 and then U2 /= Standard_Standard
4801 Decl2 := Unit_Declaration_Node (P2);
4803 if Is_Child_Unit (U2)
4804 and then Is_Visible_Child_Unit (U2)
4806 if Is_Generic_Instance (P)
4807 and then Nkind (Decl1) = N_Package_Declaration
4808 and then Generic_Parent (Specification (Decl1)) = P2
4810 Error_Msg_N ("illegal with_clause", With_Clause);
4812 ("\child unit has visible homograph" &
4813 " (RM 8.3(26), 10.1.1(19))",
4817 elsif Is_Generic_Instance (P2)
4818 and then Nkind (Decl2) = N_Package_Declaration
4819 and then Generic_Parent (Specification (Decl2)) = P
4821 -- With_clause for child unit of instance appears before
4822 -- in the context. We want to place the error message on
4823 -- it, not on the generic child unit itself.
4826 Prev_Clause : Node_Id;
4829 Prev_Clause := First (List_Containing (With_Clause));
4830 while Entity (Name (Prev_Clause)) /= U2 loop
4834 pragma Assert (Present (Prev_Clause));
4835 Error_Msg_N ("illegal with_clause", Prev_Clause);
4837 ("\child unit has visible homograph" &
4838 " (RM 8.3(26), 10.1.1(19))",
4849 end Install_Withed_Unit;
4855 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
4856 K : constant Node_Kind := Nkind (Lib_Unit);
4859 return (K in N_Generic_Declaration or else
4860 K in N_Generic_Instantiation or else
4861 K in N_Generic_Renaming_Declaration or else
4862 K = N_Package_Declaration or else
4863 K = N_Package_Renaming_Declaration or else
4864 K = N_Subprogram_Declaration or else
4865 K = N_Subprogram_Renaming_Declaration)
4866 and then Present (Parent_Spec (Lib_Unit));
4869 -----------------------
4870 -- Load_Needed_Body --
4871 -----------------------
4873 -- N is a generic unit named in a with clause, or else it is a unit that
4874 -- contains a generic unit or an inlined function. In order to perform an
4875 -- instantiation, the body of the unit must be present. If the unit itself
4876 -- is generic, we assume that an instantiation follows, and load & analyze
4877 -- the body unconditionally. This forces analysis of the spec as well.
4879 -- If the unit is not generic, but contains a generic unit, it is loaded on
4880 -- demand, at the point of instantiation (see ch12).
4882 procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
4883 Body_Name : Unit_Name_Type;
4884 Unum : Unit_Number_Type;
4886 Save_Style_Check : constant Boolean := Opt.Style_Check;
4887 -- The loading and analysis is done with style checks off
4890 if not GNAT_Mode then
4891 Style_Check := False;
4894 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
4897 (Load_Name => Body_Name,
4903 if Unum = No_Unit then
4907 Compiler_State := Analyzing; -- reset after load
4909 if not Fatal_Error (Unum) or else Try_Semantics then
4910 if Debug_Flag_L then
4911 Write_Str ("*** Loaded generic body");
4915 Semantics (Cunit (Unum));
4921 Style_Check := Save_Style_Check;
4922 end Load_Needed_Body;
4924 -------------------------
4925 -- Build_Limited_Views --
4926 -------------------------
4928 procedure Build_Limited_Views (N : Node_Id) is
4929 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
4930 P : constant Entity_Id := Cunit_Entity (Unum);
4932 Spec : Node_Id; -- To denote a package specification
4933 Lim_Typ : Entity_Id; -- To denote shadow entities
4934 Comp_Typ : Entity_Id; -- To denote real entities
4936 Lim_Header : Entity_Id; -- Package entity
4937 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
4938 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
4940 procedure Decorate_Incomplete_Type
4943 -- Add attributes of an incomplete type to a shadow entity. The same
4944 -- attributes are placed on the real entity, so that gigi receives
4945 -- a consistent view.
4947 procedure Decorate_Package_Specification (P : Entity_Id);
4948 -- Add attributes of a package entity to the entity in a package
4951 procedure Decorate_Tagged_Type
4955 -- Set basic attributes of tagged type T, including its class_wide type.
4956 -- The parameters Loc, Scope are used to decorate the class_wide type.
4958 procedure Build_Chain
4960 First_Decl : Node_Id);
4961 -- Construct list of shadow entities and attach it to entity of
4962 -- package that is mentioned in a limited_with clause.
4964 function New_Internal_Shadow_Entity
4965 (Kind : Entity_Kind;
4966 Sloc_Value : Source_Ptr;
4967 Id_Char : Character) return Entity_Id;
4968 -- Build a new internal entity and append it to the list of shadow
4969 -- entities available through the limited-header
4971 ------------------------------
4972 -- Decorate_Incomplete_Type --
4973 ------------------------------
4975 procedure Decorate_Incomplete_Type
4980 Set_Ekind (E, E_Incomplete_Type);
4981 Set_Scope (E, Scop);
4983 Set_Is_First_Subtype (E, True);
4984 Set_Stored_Constraint (E, No_Elist);
4985 Set_Full_View (E, Empty);
4986 Init_Size_Align (E);
4987 end Decorate_Incomplete_Type;
4989 --------------------------
4990 -- Decorate_Tagged_Type --
4991 --------------------------
4993 procedure Decorate_Tagged_Type
5001 Decorate_Incomplete_Type (T, Scop);
5002 Set_Is_Tagged_Type (T);
5004 -- Build corresponding class_wide type, if not previously done
5006 -- Note: The class-wide entity is shared by the limited-view
5007 -- and the full-view.
5009 if No (Class_Wide_Type (T)) then
5010 CW := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
5012 -- Set parent to be the same as the parent of the tagged type.
5013 -- We need a parent field set, and it is supposed to point to
5014 -- the declaration of the type. The tagged type declaration
5015 -- essentially declares two separate types, the tagged type
5016 -- itself and the corresponding class-wide type, so it is
5017 -- reasonable for the parent fields to point to the declaration
5020 Set_Parent (CW, Parent (T));
5022 -- Set remaining fields of classwide type
5024 Set_Ekind (CW, E_Class_Wide_Type);
5026 Set_Scope (CW, Scop);
5027 Set_Is_Tagged_Type (CW);
5028 Set_Is_First_Subtype (CW, True);
5029 Init_Size_Align (CW);
5030 Set_Has_Unknown_Discriminants (CW, True);
5031 Set_Class_Wide_Type (CW, CW);
5032 Set_Equivalent_Type (CW, Empty);
5033 Set_From_With_Type (CW, From_With_Type (T));
5035 -- Link type to its class-wide type
5037 Set_Class_Wide_Type (T, CW);
5039 end Decorate_Tagged_Type;
5041 ------------------------------------
5042 -- Decorate_Package_Specification --
5043 ------------------------------------
5045 procedure Decorate_Package_Specification (P : Entity_Id) is
5047 -- Place only the most basic attributes
5049 Set_Ekind (P, E_Package);
5050 Set_Etype (P, Standard_Void_Type);
5051 end Decorate_Package_Specification;
5053 --------------------------------
5054 -- New_Internal_Shadow_Entity --
5055 --------------------------------
5057 function New_Internal_Shadow_Entity
5058 (Kind : Entity_Kind;
5059 Sloc_Value : Source_Ptr;
5060 Id_Char : Character) return Entity_Id
5062 E : constant Entity_Id :=
5063 Make_Defining_Identifier (Sloc_Value,
5064 Chars => New_Internal_Name (Id_Char));
5067 Set_Ekind (E, Kind);
5068 Set_Is_Internal (E, True);
5070 if Kind in Type_Kind then
5071 Init_Size_Align (E);
5074 Append_Entity (E, Lim_Header);
5077 end New_Internal_Shadow_Entity;
5083 procedure Build_Chain
5085 First_Decl : Node_Id)
5087 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
5088 Is_Tagged : Boolean;
5093 while Present (Decl) loop
5095 -- For each library_package_declaration in the environment, there
5096 -- is an implicit declaration of a *limited view* of that library
5097 -- package. The limited view of a package contains:
5099 -- * For each nested package_declaration, a declaration of the
5100 -- limited view of that package, with the same defining-
5101 -- program-unit name.
5103 -- * For each type_declaration in the visible part, an incomplete
5104 -- type-declaration with the same defining_identifier, whose
5105 -- completion is the type_declaration. If the type_declaration
5106 -- is tagged, then the incomplete_type_declaration is tagged
5109 -- The partial view is tagged if the declaration has the
5110 -- explicit keyword, or else if it is a type extension, both
5111 -- of which can be ascertained syntactically.
5113 if Nkind (Decl) = N_Full_Type_Declaration then
5115 (Nkind (Type_Definition (Decl)) = N_Record_Definition
5116 and then Tagged_Present (Type_Definition (Decl)))
5118 (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
5121 (Record_Extension_Part (Type_Definition (Decl))));
5123 Comp_Typ := Defining_Identifier (Decl);
5125 if not Analyzed_Unit then
5127 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5129 Decorate_Incomplete_Type (Comp_Typ, Scope);
5133 -- Create shadow entity for type
5135 Lim_Typ := New_Internal_Shadow_Entity
5136 (Kind => Ekind (Comp_Typ),
5137 Sloc_Value => Sloc (Comp_Typ),
5140 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5141 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5142 Set_From_With_Type (Lim_Typ);
5145 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5147 Decorate_Incomplete_Type (Lim_Typ, Scope);
5150 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5152 elsif Nkind_In (Decl, N_Private_Type_Declaration,
5153 N_Incomplete_Type_Declaration,
5154 N_Task_Type_Declaration,
5155 N_Protected_Type_Declaration)
5157 Comp_Typ := Defining_Identifier (Decl);
5160 Nkind_In (Decl, N_Private_Type_Declaration,
5161 N_Incomplete_Type_Declaration)
5162 and then Tagged_Present (Decl);
5164 if not Analyzed_Unit then
5166 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5168 Decorate_Incomplete_Type (Comp_Typ, Scope);
5172 Lim_Typ := New_Internal_Shadow_Entity
5173 (Kind => Ekind (Comp_Typ),
5174 Sloc_Value => Sloc (Comp_Typ),
5177 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5178 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5179 Set_From_With_Type (Lim_Typ);
5182 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5184 Decorate_Incomplete_Type (Lim_Typ, Scope);
5187 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5189 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5190 Comp_Typ := Defining_Identifier (Decl);
5192 if not Analyzed_Unit then
5193 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5196 -- Create shadow entity for type
5198 Lim_Typ := New_Internal_Shadow_Entity
5199 (Kind => Ekind (Comp_Typ),
5200 Sloc_Value => Sloc (Comp_Typ),
5203 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5204 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5205 Set_From_With_Type (Lim_Typ);
5207 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5208 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5210 elsif Nkind (Decl) = N_Package_Declaration then
5215 Spec : constant Node_Id := Specification (Decl);
5218 Comp_Typ := Defining_Unit_Name (Spec);
5220 if not Analyzed (Cunit (Unum)) then
5221 Decorate_Package_Specification (Comp_Typ);
5222 Set_Scope (Comp_Typ, Scope);
5225 Lim_Typ := New_Internal_Shadow_Entity
5226 (Kind => Ekind (Comp_Typ),
5227 Sloc_Value => Sloc (Comp_Typ),
5230 Decorate_Package_Specification (Lim_Typ);
5231 Set_Scope (Lim_Typ, Scope);
5233 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5234 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5235 Set_From_With_Type (Lim_Typ);
5237 -- Note: The non_limited_view attribute is not used
5238 -- for local packages.
5242 First_Decl => First (Visible_Declarations (Spec)));
5250 -- Start of processing for Build_Limited_Views
5253 pragma Assert (Limited_Present (N));
5255 -- A library_item mentioned in a limited_with_clause is a package
5256 -- declaration, not a subprogram declaration, generic declaration,
5257 -- generic instantiation, or package renaming declaration.
5259 case Nkind (Unit (Library_Unit (N))) is
5260 when N_Package_Declaration =>
5263 when N_Subprogram_Declaration =>
5264 Error_Msg_N ("subprograms not allowed in "
5265 & "limited with_clauses", N);
5268 when N_Generic_Package_Declaration |
5269 N_Generic_Subprogram_Declaration =>
5270 Error_Msg_N ("generics not allowed in "
5271 & "limited with_clauses", N);
5274 when N_Generic_Instantiation =>
5275 Error_Msg_N ("generic instantiations not allowed in "
5276 & "limited with_clauses", N);
5279 when N_Generic_Renaming_Declaration =>
5280 Error_Msg_N ("generic renamings not allowed in "
5281 & "limited with_clauses", N);
5284 when N_Subprogram_Renaming_Declaration =>
5285 Error_Msg_N ("renamed subprograms not allowed in "
5286 & "limited with_clauses", N);
5289 when N_Package_Renaming_Declaration =>
5290 Error_Msg_N ("renamed packages not allowed in "
5291 & "limited with_clauses", N);
5295 raise Program_Error;
5298 -- Check if the chain is already built
5300 Spec := Specification (Unit (Library_Unit (N)));
5302 if Limited_View_Installed (Spec) then
5306 Set_Ekind (P, E_Package);
5308 -- Build the header of the limited_view
5311 Make_Defining_Identifier (Sloc (N),
5312 Chars => New_Internal_Name (Id_Char => 'Z'));
5313 Set_Ekind (Lim_Header, E_Package);
5314 Set_Is_Internal (Lim_Header);
5315 Set_Limited_View (P, Lim_Header);
5317 -- Create the auxiliary chain. All the shadow entities are appended to
5318 -- the list of entities of the limited-view header
5322 First_Decl => First (Visible_Declarations (Spec)));
5324 -- Save the last built shadow entity. It is needed later to set the
5325 -- reference to the first shadow entity in the private part
5327 Last_Pub_Lim_E := Last_Lim_E;
5329 -- Ada 2005 (AI-262): Add the limited view of the private declarations
5330 -- Required to give support to limited-private-with clauses
5332 Build_Chain (Scope => P,
5333 First_Decl => First (Private_Declarations (Spec)));
5335 if Last_Pub_Lim_E /= Empty then
5336 Set_First_Private_Entity (Lim_Header,
5337 Next_Entity (Last_Pub_Lim_E));
5339 Set_First_Private_Entity (Lim_Header,
5343 Set_Limited_View_Installed (Spec);
5344 end Build_Limited_Views;
5346 -------------------------------
5347 -- Check_Body_Needed_For_SAL --
5348 -------------------------------
5350 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5352 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5353 -- Determine whether use of entity E might require the presence of its
5354 -- body. For a package this requires a recursive traversal of all nested
5357 ---------------------------
5358 -- Entity_Needed_For_SAL --
5359 ---------------------------
5361 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5365 if Is_Subprogram (E)
5366 and then Has_Pragma_Inline (E)
5370 elsif Ekind (E) = E_Generic_Function
5371 or else Ekind (E) = E_Generic_Procedure
5375 elsif Ekind (E) = E_Generic_Package
5377 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5378 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5382 elsif Ekind (E) = E_Package
5384 Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5385 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5387 Ent := First_Entity (E);
5388 while Present (Ent) loop
5389 if Entity_Needs_Body (Ent) then
5401 end Entity_Needs_Body;
5403 -- Start of processing for Check_Body_Needed_For_SAL
5406 if Ekind (Unit_Name) = E_Generic_Package
5408 Nkind (Unit_Declaration_Node (Unit_Name)) =
5409 N_Generic_Package_Declaration
5411 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5413 Set_Body_Needed_For_SAL (Unit_Name);
5415 elsif Ekind (Unit_Name) = E_Generic_Procedure
5416 or else Ekind (Unit_Name) = E_Generic_Function
5418 Set_Body_Needed_For_SAL (Unit_Name);
5420 elsif Is_Subprogram (Unit_Name)
5421 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5422 N_Subprogram_Declaration
5423 and then Has_Pragma_Inline (Unit_Name)
5425 Set_Body_Needed_For_SAL (Unit_Name);
5427 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5428 Check_Body_Needed_For_SAL
5429 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5431 elsif Ekind (Unit_Name) = E_Package
5432 and then Entity_Needs_Body (Unit_Name)
5434 Set_Body_Needed_For_SAL (Unit_Name);
5436 elsif Ekind (Unit_Name) = E_Package_Body
5437 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5439 Check_Body_Needed_For_SAL
5440 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5442 end Check_Body_Needed_For_SAL;
5444 --------------------
5445 -- Remove_Context --
5446 --------------------
5448 procedure Remove_Context (N : Node_Id) is
5449 Lib_Unit : constant Node_Id := Unit (N);
5452 -- If this is a child unit, first remove the parent units
5454 if Is_Child_Spec (Lib_Unit) then
5455 Remove_Parents (Lib_Unit);
5458 Remove_Context_Clauses (N);
5461 ----------------------------
5462 -- Remove_Context_Clauses --
5463 ----------------------------
5465 procedure Remove_Context_Clauses (N : Node_Id) is
5467 Unit_Name : Entity_Id;
5470 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5471 -- limited-views first and regular-views later (to maintain the
5474 -- First Phase: Remove limited_with context clauses
5476 Item := First (Context_Items (N));
5477 while Present (Item) loop
5479 -- We are interested only in with clauses which got installed
5482 if Nkind (Item) = N_With_Clause
5483 and then Limited_Present (Item)
5484 and then Limited_View_Installed (Item)
5486 Remove_Limited_With_Clause (Item);
5492 -- Second Phase: Loop through context items and undo regular
5493 -- with_clauses and use_clauses.
5495 Item := First (Context_Items (N));
5496 while Present (Item) loop
5498 -- We are interested only in with clauses which got installed on
5499 -- entry, as indicated by their Context_Installed flag set
5501 if Nkind (Item) = N_With_Clause
5502 and then Limited_Present (Item)
5503 and then Limited_View_Installed (Item)
5507 elsif Nkind (Item) = N_With_Clause
5508 and then Context_Installed (Item)
5510 -- Remove items from one with'ed unit
5512 Unit_Name := Entity (Name (Item));
5513 Remove_Unit_From_Visibility (Unit_Name);
5514 Set_Context_Installed (Item, False);
5516 elsif Nkind (Item) = N_Use_Package_Clause then
5517 End_Use_Package (Item);
5519 elsif Nkind (Item) = N_Use_Type_Clause then
5520 End_Use_Type (Item);
5525 end Remove_Context_Clauses;
5527 --------------------------------
5528 -- Remove_Limited_With_Clause --
5529 --------------------------------
5531 procedure Remove_Limited_With_Clause (N : Node_Id) is
5532 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5535 Lim_Header : Entity_Id;
5536 Lim_Typ : Entity_Id;
5540 pragma Assert (Limited_View_Installed (N));
5542 -- In case of limited with_clause on subprograms, generics, instances,
5543 -- or renamings, the corresponding error was previously posted and we
5544 -- have nothing to do here.
5546 if Nkind (P_Unit) /= N_Package_Declaration then
5550 P := Defining_Unit_Name (Specification (P_Unit));
5552 -- Handle child packages
5554 if Nkind (P) = N_Defining_Program_Unit_Name then
5555 P := Defining_Identifier (P);
5558 if Debug_Flag_I then
5559 Write_Str ("remove limited view of ");
5560 Write_Name (Chars (P));
5561 Write_Str (" from visibility");
5565 -- Prepare the removal of the shadow entities from visibility. The
5566 -- first element of the limited view is a header (an E_Package
5567 -- entity) that is used to reference the first shadow entity in the
5568 -- private part of the package
5570 Lim_Header := Limited_View (P);
5571 Lim_Typ := First_Entity (Lim_Header);
5573 -- Remove package and shadow entities from visibility if it has not
5576 if not Analyzed (P_Unit) then
5578 Set_Is_Immediately_Visible (P, False);
5580 while Present (Lim_Typ) loop
5582 Next_Entity (Lim_Typ);
5585 -- Otherwise this package has already appeared in the closure and its
5586 -- shadow entities must be replaced by its real entities. This code
5587 -- must be kept synchronized with the complementary code in Install
5588 -- Limited_Withed_Unit.
5591 -- Real entities that are type or subtype declarations were hidden
5592 -- from visibility at the point of installation of the limited-view.
5593 -- Now we recover the previous value of the hidden attribute.
5595 E := First_Entity (P);
5596 while Present (E) and then E /= First_Private_Entity (P) loop
5598 Set_Is_Hidden (E, Was_Hidden (E));
5604 while Present (Lim_Typ)
5605 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5607 -- Nested packages and child units were not unchained
5609 if Ekind (Lim_Typ) /= E_Package
5610 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5612 -- Handle incomplete types of the real view. For this purpose
5613 -- we traverse the list of visible entities to look for an
5614 -- incomplete type in the real-view associated with Lim_Typ.
5616 E := First_Entity (P);
5617 while Present (E) and then E /= First_Private_Entity (P) loop
5618 exit when Ekind (E) = E_Incomplete_Type
5619 and then Present (Full_View (E))
5620 and then Full_View (E) = Lim_Typ;
5625 -- If the previous search was not successful then the entity
5626 -- to be restored in the homonym list is the non-limited view
5628 if E = First_Private_Entity (P) then
5629 E := Non_Limited_View (Lim_Typ);
5632 pragma Assert (not In_Chain (E));
5634 Prev := Current_Entity (Lim_Typ);
5636 if Prev = Lim_Typ then
5637 Set_Current_Entity (E);
5640 while Present (Prev)
5641 and then Homonym (Prev) /= Lim_Typ
5643 Prev := Homonym (Prev);
5646 if Present (Prev) then
5647 Set_Homonym (Prev, E);
5651 -- We must also set the next homonym entity of the real entity
5652 -- to handle the case in which the next homonym was a shadow
5655 Set_Homonym (E, Homonym (Lim_Typ));
5658 Next_Entity (Lim_Typ);
5662 -- Indicate that the limited view of the package is not installed
5664 Set_From_With_Type (P, False);
5665 Set_Limited_View_Installed (N, False);
5666 end Remove_Limited_With_Clause;
5668 --------------------
5669 -- Remove_Parents --
5670 --------------------
5672 procedure Remove_Parents (Lib_Unit : Node_Id) is
5675 P_Spec : Node_Id := Empty;
5677 Vis : constant Boolean :=
5678 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
5681 if Is_Child_Spec (Lib_Unit) then
5682 P_Spec := Parent_Spec (Lib_Unit);
5684 elsif Nkind (Lib_Unit) = N_Package_Body
5685 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
5687 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
5690 if Present (P_Spec) then
5693 P_Name := Get_Parent_Entity (P);
5694 Remove_Context_Clauses (P_Spec);
5695 End_Package_Scope (P_Name);
5696 Set_Is_Immediately_Visible (P_Name, Vis);
5698 -- Remove from visibility the siblings as well, which are directly
5699 -- visible while the parent is in scope.
5701 E := First_Entity (P_Name);
5702 while Present (E) loop
5703 if Is_Child_Unit (E) then
5704 Set_Is_Immediately_Visible (E, False);
5710 Set_In_Package_Body (P_Name, False);
5712 -- This is the recursive call to remove the context of any
5713 -- higher level parent. This recursion ensures that all parents
5714 -- are removed in the reverse order of their installation.
5720 ---------------------------------
5721 -- Remove_Private_With_Clauses --
5722 ---------------------------------
5724 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
5727 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
5728 -- Check whether a given unit appears in a regular with_clause.
5729 -- Used to determine whether a private_with_clause, implicit or
5730 -- explicit, should be ignored.
5732 ----------------------------
5733 -- In_Regular_With_Clause --
5734 ----------------------------
5736 function In_Regular_With_Clause (E : Entity_Id) return Boolean
5741 Item := First (Context_Items (Comp_Unit));
5742 while Present (Item) loop
5743 if Nkind (Item) = N_With_Clause
5744 and then Entity (Name (Item)) = E
5745 and then not Private_Present (Item)
5753 end In_Regular_With_Clause;
5755 -- Start of processing for Remove_Private_With_Clauses
5758 Item := First (Context_Items (Comp_Unit));
5759 while Present (Item) loop
5760 if Nkind (Item) = N_With_Clause
5761 and then Private_Present (Item)
5763 -- If private_with_clause is redundant, remove it from
5764 -- context, as a small optimization to subsequent handling
5765 -- of private_with clauses in other nested packages..
5767 if In_Regular_With_Clause (Entity (Name (Item))) then
5769 Nxt : constant Node_Id := Next (Item);
5775 elsif Limited_Present (Item) then
5776 if not Limited_View_Installed (Item) then
5777 Remove_Limited_With_Clause (Item);
5783 Remove_Unit_From_Visibility (Entity (Name (Item)));
5784 Set_Context_Installed (Item, False);
5792 end Remove_Private_With_Clauses;
5794 ---------------------------------
5795 -- Remove_Unit_From_Visibility --
5796 ---------------------------------
5798 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
5799 P : constant Entity_Id := Scope (Unit_Name);
5802 if Debug_Flag_I then
5803 Write_Str ("remove unit ");
5804 Write_Name (Chars (Unit_Name));
5805 Write_Str (" from visibility");
5809 if P /= Standard_Standard then
5810 Set_Is_Visible_Child_Unit (Unit_Name, False);
5813 Set_Is_Potentially_Use_Visible (Unit_Name, False);
5814 Set_Is_Immediately_Visible (Unit_Name, False);
5815 end Remove_Unit_From_Visibility;
5830 procedure Unchain (E : Entity_Id) is
5834 Prev := Current_Entity (E);
5840 Set_Name_Entity_Id (Chars (E), Homonym (E));
5843 while Present (Prev)
5844 and then Homonym (Prev) /= E
5846 Prev := Homonym (Prev);
5849 if Present (Prev) then
5850 Set_Homonym (Prev, Homonym (E));
5854 if Debug_Flag_I then
5855 Write_Str (" (homonym) unchain ");
5856 Write_Name (Chars (E));