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 Fname; use Fname;
32 with Fname.UF; use Fname.UF;
33 with Freeze; use Freeze;
34 with Impunit; use Impunit;
35 with Inline; use Inline;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Output; use Output;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dist; use Sem_Dist;
53 with Sem_Prag; use Sem_Prag;
54 with Sem_Util; use Sem_Util;
55 with Sem_Warn; use Sem_Warn;
56 with Stand; use Stand;
57 with Sinfo; use Sinfo;
58 with Sinfo.CN; use Sinfo.CN;
59 with Sinput; use Sinput;
60 with Snames; use Snames;
61 with Style; use Style;
62 with Stylesw; use Stylesw;
63 with Tbuild; use Tbuild;
64 with Uname; use Uname;
66 package body Sem_Ch10 is
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Analyze_Context (N : Node_Id);
73 -- Analyzes items in the context clause of compilation unit
75 procedure Build_Limited_Views (N : Node_Id);
76 -- Build and decorate the list of shadow entities for a package mentioned
77 -- in a limited_with clause. If the package was not previously analyzed
78 -- then it also performs a basic decoration of the real entities; this
79 -- is required to do not pass non-decorated entities to the back-end.
80 -- Implements Ada 2005 (AI-50217).
82 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
83 -- Check whether the source for the body of a compilation unit must
84 -- be included in a standalone library.
86 procedure Check_Private_Child_Unit (N : Node_Id);
87 -- If a with_clause mentions a private child unit, the compilation
88 -- unit must be a member of the same family, as described in 10.1.2.
90 procedure Check_Stub_Level (N : Node_Id);
91 -- Verify that a stub is declared immediately within a compilation unit,
92 -- and not in an inner frame.
94 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
95 -- When a child unit appears in a context clause, the implicit withs on
96 -- parents are made explicit, and with clauses are inserted in the context
97 -- clause before the one for the child. If a parent in the with_clause
98 -- is a renaming, the implicit with_clause is on the renaming whose name
99 -- is mentioned in the with_clause, and not on the package it renames.
100 -- N is the compilation unit whose list of context items receives the
101 -- implicit with_clauses.
103 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
104 -- Get defining entity of parent unit of a child unit. In most cases this
105 -- is the defining entity of the unit, but for a child instance whose
106 -- parent needs a body for inlining, the instantiation node of the parent
107 -- has not yet been rewritten as a package declaration, and the entity has
108 -- to be retrieved from the Instance_Spec of the unit.
110 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
111 -- If the main unit is a child unit, implicit withs are also added for
112 -- all its ancestors.
114 function In_Chain (E : Entity_Id) return Boolean;
115 -- Check that the shadow entity is not already in the homonym chain, for
116 -- example through a limited_with clause in a parent unit.
118 procedure Install_Context_Clauses (N : Node_Id);
119 -- Subsidiary to Install_Context and Install_Parents. Process only with_
120 -- and use_clauses for current unit and its library unit if any.
122 procedure Install_Limited_Context_Clauses (N : Node_Id);
123 -- Subsidiary to Install_Context. Process only limited with_clauses
124 -- for current unit. Implements Ada 2005 (AI-50217).
126 procedure Install_Limited_Withed_Unit (N : Node_Id);
127 -- Place shadow entities for a limited_with package in the visibility
128 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
130 procedure Install_Withed_Unit
131 (With_Clause : Node_Id;
132 Private_With_OK : Boolean := False);
133 -- If the unit is not a child unit, make unit immediately visible.
134 -- The caller ensures that the unit is not already currently installed.
135 -- The flag Private_With_OK is set true in Install_Private_With_Clauses,
136 -- which is called when compiling the private part of a package, or
137 -- installing the private declarations of a parent unit.
139 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
140 -- This procedure establishes the context for the compilation of a child
141 -- unit. If Lib_Unit is a child library spec then the context of the parent
142 -- is installed, and the parent itself made immediately visible, so that
143 -- the child unit is processed in the declarative region of the parent.
144 -- Install_Parents makes a recursive call to itself to ensure that all
145 -- parents are loaded in the nested case. If Lib_Unit is a library body,
146 -- the only effect of Install_Parents is to install the private decls of
147 -- the parents, because the visible parent declarations will have been
148 -- installed as part of the context of the corresponding spec.
150 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
151 -- In the compilation of a child unit, a child of any of the ancestor
152 -- units is directly visible if it is visible, because the parent is in
153 -- an enclosing scope. Iterate over context to find child units of U_Name
154 -- or of some ancestor of it.
156 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
157 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
158 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
159 -- a library spec that has a parent. If the call to Is_Child_Spec returns
160 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
161 -- compilation unit for the parent spec.
163 -- Lib_Unit can also be a subprogram body that acts as its own spec. If
164 -- the Parent_Spec is non-empty, this is also a child unit.
166 procedure Remove_Context_Clauses (N : Node_Id);
167 -- Subsidiary of previous one. Remove use_ and with_clauses
169 procedure Remove_Limited_With_Clause (N : Node_Id);
170 -- Remove from visibility the shadow entities introduced for a package
171 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
173 procedure Remove_Parents (Lib_Unit : Node_Id);
174 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
175 -- contexts established by the corresponding call to Install_Parents are
176 -- removed. Remove_Parents contains a recursive call to itself to ensure
177 -- that all parents are removed in the nested case.
179 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
180 -- Reset all visibility flags on unit after compiling it, either as a
181 -- main unit or as a unit in the context.
183 procedure Unchain (E : Entity_Id);
184 -- Remove single entity from visibility list
186 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
187 -- Common processing for all stubs (subprograms, tasks, packages, and
188 -- protected cases). N is the stub to be analyzed. Once the subunit
189 -- name is established, load and analyze. Nam is the non-overloadable
190 -- entity for which the proper body provides a completion. Subprogram
191 -- stubs are handled differently because they can be declarations.
194 -- A dummy procedure, for debugging use, called just before analyzing the
195 -- main unit (after dealing with any context clauses).
197 --------------------------
198 -- Limited_With_Clauses --
199 --------------------------
201 -- Limited_With clauses are the mechanism chosen for Ada05 to support
202 -- mutually recursive types declared in different units. A limited_with
203 -- clause that names package P in the context of unit U makes the types
204 -- declared in the visible part of P available within U, but with the
205 -- restriction that these types can only be used as incomplete types.
206 -- The limited_with clause does not impose a semantic dependence on P,
207 -- and it is possible for two packages to have limited_with_clauses on
208 -- each other without creating an elaboration circularity.
210 -- To support this feature, the analysis of a limited_with clause must
211 -- create an abbreviated view of the package, without performing any
212 -- semantic analysis on it. This "package abstract" contains shadow
213 -- types that are in one-one correspondence with the real types in the
214 -- package, and that have the properties of incomplete types.
216 -- The implementation creates two element lists: one to chain the shadow
217 -- entities, and one to chain the corresponding type entities in the tree
218 -- of the package. Links between corresponding entities in both chains
219 -- allow the compiler to select the proper view of a given type, depending
220 -- on the context. Note that in contrast with the handling of private
221 -- types, the limited view and the non-limited view of a type are treated
222 -- as separate entities, and no entity exchange needs to take place, which
223 -- makes the implementation must simpler than could be feared.
225 ------------------------------
226 -- Analyze_Compilation_Unit --
227 ------------------------------
229 procedure Analyze_Compilation_Unit (N : Node_Id) is
230 Unit_Node : constant Node_Id := Unit (N);
231 Lib_Unit : Node_Id := Library_Unit (N);
233 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
234 Par_Spec_Name : Unit_Name_Type;
235 Unum : Unit_Number_Type;
237 procedure Check_Redundant_Withs
238 (Context_Items : List_Id;
239 Spec_Context_Items : List_Id := No_List);
240 -- Determine whether the context list of a compilation unit contains
241 -- redundant with clauses. When checking body clauses against spec
242 -- clauses, set Context_Items to the context list of the body and
243 -- Spec_Context_Items to that of the spec. Parent packages are not
244 -- examined for documentation purposes.
246 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
247 -- Generate cross-reference information for the parents of child units.
248 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
250 ---------------------------
251 -- Check_Redundant_Withs --
252 ---------------------------
254 procedure Check_Redundant_Withs
255 (Context_Items : List_Id;
256 Spec_Context_Items : List_Id := No_List)
260 procedure Process_Body_Clauses
261 (Context_List : List_Id;
263 Used : in out Boolean;
264 Used_Type_Or_Elab : in out Boolean);
265 -- Examine the context clauses of a package body, trying to match
266 -- the name entity of Clause with any list element. If the match
267 -- occurs on a use package clause, set Used to True, for a use
268 -- type clause, pragma Elaborate or pragma Elaborate_All, set
269 -- Used_Type_Or_Elab to True.
271 procedure Process_Spec_Clauses
272 (Context_List : List_Id;
274 Used : in out Boolean;
275 Withed : in out Boolean;
276 Exit_On_Self : Boolean := False);
277 -- Examine the context clauses of a package spec, trying to match
278 -- the name entity of Clause with any list element. If the match
279 -- occurs on a use package clause, set Used to True, for a with
280 -- package clause other than Clause, set Withed to True. Limited
281 -- with clauses, implicitly generated with clauses and withs
282 -- having pragmas Elaborate or Elaborate_All applied to them are
283 -- skipped. Exit_On_Self is used to control the search loop and
284 -- force an exit whenever Clause sees itself in the search.
286 --------------------------
287 -- Process_Body_Clauses --
288 --------------------------
290 procedure Process_Body_Clauses
291 (Context_List : List_Id;
293 Used : in out Boolean;
294 Used_Type_Or_Elab : in out Boolean)
296 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
302 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
303 -- In an expanded name in a use clause, if the prefix is a
304 -- renamed package, the entity is set to the original package
305 -- as a result, when checking whether the package appears in a
306 -- previous with_clause, the renaming has to be taken into
307 -- account, to prevent spurious or incorrect warnings. The
308 -- common case is the use of Text_IO.
314 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
316 return Entity (N) = P
318 (Present (Renamed_Object (P))
319 and then Entity (N) = Renamed_Object (P));
322 -- Start of processing for Process_Body_Clauses
326 Used_Type_Or_Elab := False;
328 Cont_Item := First (Context_List);
329 while Present (Cont_Item) loop
331 -- Package use clause
333 if Nkind (Cont_Item) = N_Use_Package_Clause
336 -- Search through use clauses
338 Use_Item := First (Names (Cont_Item));
339 while Present (Use_Item) and then not Used loop
341 -- Case of a direct use of the one we are looking for
343 if Entity (Use_Item) = Nam_Ent then
346 -- Handle nested case, as in "with P; use P.Q.R"
353 -- Loop through prefixes looking for match
356 while Nkind (UE) = N_Expanded_Name loop
357 if Same_Unit (Prefix (UE), Nam_Ent) then
372 elsif Nkind (Cont_Item) = N_Use_Type_Clause
373 and then not Used_Type_Or_Elab
375 Subt_Mark := First (Subtype_Marks (Cont_Item));
376 while Present (Subt_Mark)
377 and then not Used_Type_Or_Elab
379 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
380 Used_Type_Or_Elab := True;
386 -- Pragma Elaborate or Elaborate_All
388 elsif Nkind (Cont_Item) = N_Pragma
390 (Pragma_Name (Cont_Item) = Name_Elaborate
392 Pragma_Name (Cont_Item) = Name_Elaborate_All)
393 and then not Used_Type_Or_Elab
396 First (Pragma_Argument_Associations (Cont_Item));
397 while Present (Prag_Unit)
398 and then not Used_Type_Or_Elab
400 if Entity (Expression (Prag_Unit)) = Nam_Ent then
401 Used_Type_Or_Elab := True;
410 end Process_Body_Clauses;
412 --------------------------
413 -- Process_Spec_Clauses --
414 --------------------------
416 procedure Process_Spec_Clauses
417 (Context_List : List_Id;
419 Used : in out Boolean;
420 Withed : in out Boolean;
421 Exit_On_Self : Boolean := False)
423 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
431 Cont_Item := First (Context_List);
432 while Present (Cont_Item) loop
434 -- Stop the search since the context items after Cont_Item
435 -- have already been examined in a previous iteration of
436 -- the reverse loop in Check_Redundant_Withs.
439 and Cont_Item = Clause
444 -- Package use clause
446 if Nkind (Cont_Item) = N_Use_Package_Clause
449 Use_Item := First (Names (Cont_Item));
450 while Present (Use_Item) and then not Used loop
451 if Entity (Use_Item) = Nam_Ent then
458 -- Package with clause. Avoid processing self, implicitly
459 -- generated with clauses or limited with clauses. Note
460 -- that we examine with clauses having pragmas Elaborate
461 -- or Elaborate_All applied to them due to cases such as:
465 -- pragma Elaborate (Pack);
467 -- In this case, the second with clause is redundant since
468 -- the pragma applies only to the first "with Pack;".
470 elsif Nkind (Cont_Item) = N_With_Clause
471 and then not Implicit_With (Cont_Item)
472 and then not Limited_Present (Cont_Item)
473 and then Cont_Item /= Clause
474 and then Entity (Name (Cont_Item)) = Nam_Ent
481 end Process_Spec_Clauses;
483 -- Start of processing for Check_Redundant_Withs
486 Clause := Last (Context_Items);
487 while Present (Clause) loop
489 -- Avoid checking implicitly generated with clauses, limited
490 -- with clauses or withs that have pragma Elaborate or
491 -- Elaborate_All apllied.
493 if Nkind (Clause) = N_With_Clause
494 and then not Implicit_With (Clause)
495 and then not Limited_Present (Clause)
496 and then not Elaborate_Present (Clause)
498 -- Package body-to-spec check
500 if Present (Spec_Context_Items) then
502 Used_In_Body : Boolean := False;
503 Used_In_Spec : Boolean := False;
504 Used_Type_Or_Elab : Boolean := False;
505 Withed_In_Spec : Boolean := False;
509 (Context_List => Spec_Context_Items,
511 Used => Used_In_Spec,
512 Withed => Withed_In_Spec);
515 (Context_List => Context_Items,
517 Used => Used_In_Body,
518 Used_Type_Or_Elab => Used_Type_Or_Elab);
520 -- "Type Elab" refers to the presence of either a use
521 -- type clause, pragmas Elaborate or Elaborate_All.
523 -- +---------------+---------------------------+------+
524 -- | Spec | Body | Warn |
525 -- +--------+------+--------+------+-----------+------+
526 -- | Withed | Used | Withed | Used | Type Elab | |
527 -- | X | | X | | | X |
528 -- | X | | X | X | | |
529 -- | X | | X | | X | |
530 -- | X | | X | X | X | |
531 -- | X | X | X | | | X |
532 -- | X | X | X | | X | |
533 -- | X | X | X | X | | X |
534 -- | X | X | X | X | X | |
535 -- +--------+------+--------+------+-----------+------+
538 and then not Used_Type_Or_Elab)
541 and then not Used_In_Body)
545 Error_Msg_N ("?redundant with clause in body", Clause);
548 Used_In_Body := False;
549 Used_In_Spec := False;
550 Used_Type_Or_Elab := False;
551 Withed_In_Spec := False;
554 -- Standalone package spec or body check
558 Dont_Care : Boolean := False;
559 Withed : Boolean := False;
562 -- The mechanism for examining the context clauses of a
563 -- package spec can be applied to package body clauses.
566 (Context_List => Context_Items,
570 Exit_On_Self => True);
573 Error_Msg_N ("?redundant with clause", Clause);
581 end Check_Redundant_Withs;
583 --------------------------------
584 -- Generate_Parent_References --
585 --------------------------------
587 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
589 P_Name : Entity_Id := P_Id;
592 Pref := Name (Parent (Defining_Entity (N)));
594 if Nkind (Pref) = N_Expanded_Name then
596 -- Done already, if the unit has been compiled indirectly as
597 -- part of the closure of its context because of inlining.
602 while Nkind (Pref) = N_Selected_Component loop
603 Change_Selected_Component_To_Expanded_Name (Pref);
604 Set_Entity (Pref, P_Name);
605 Set_Etype (Pref, Etype (P_Name));
606 Generate_Reference (P_Name, Pref, 'r');
607 Pref := Prefix (Pref);
608 P_Name := Scope (P_Name);
611 -- The guard here on P_Name is to handle the error condition where
612 -- the parent unit is missing because the file was not found.
614 if Present (P_Name) then
615 Set_Entity (Pref, P_Name);
616 Set_Etype (Pref, Etype (P_Name));
617 Generate_Reference (P_Name, Pref, 'r');
618 Style.Check_Identifier (Pref, P_Name);
620 end Generate_Parent_References;
622 -- Start of processing for Analyze_Compilation_Unit
625 Process_Compilation_Unit_Pragmas (N);
627 -- If the unit is a subunit whose parent has not been analyzed (which
628 -- indicates that the main unit is a subunit, either the current one or
629 -- one of its descendents) then the subunit is compiled as part of the
630 -- analysis of the parent, which we proceed to do. Basically this gets
631 -- handled from the top down and we don't want to do anything at this
632 -- level (i.e. this subunit will be handled on the way down from the
633 -- parent), so at this level we immediately return. If the subunit
634 -- ends up not analyzed, it means that the parent did not contain a
635 -- stub for it, or that there errors were dectected in some ancestor.
637 if Nkind (Unit_Node) = N_Subunit
638 and then not Analyzed (Lib_Unit)
640 Semantics (Lib_Unit);
642 if not Analyzed (Proper_Body (Unit_Node)) then
643 if Serious_Errors_Detected > 0 then
644 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
646 Error_Msg_N ("missing stub for subunit", N);
653 -- Analyze context (this will call Sem recursively for with'ed units)
657 -- If the unit is a package body, the spec is already loaded and must
658 -- be analyzed first, before we analyze the body.
660 if Nkind (Unit_Node) = N_Package_Body then
662 -- If no Lib_Unit, then there was a serious previous error, so
663 -- just ignore the entire analysis effort
665 if No (Lib_Unit) then
669 Semantics (Lib_Unit);
670 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
672 -- Verify that the library unit is a package declaration
674 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
675 N_Generic_Package_Declaration)
678 ("no legal package declaration for package body", N);
681 -- Otherwise, the entity in the declaration is visible. Update
682 -- the version to reflect dependence of this body on the spec.
685 Spec_Id := Defining_Entity (Unit (Lib_Unit));
686 Set_Is_Immediately_Visible (Spec_Id, True);
687 Version_Update (N, Lib_Unit);
689 if Nkind (Defining_Unit_Name (Unit_Node)) =
690 N_Defining_Program_Unit_Name
692 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
697 -- If the unit is a subprogram body, then we similarly need to analyze
698 -- its spec. However, things are a little simpler in this case, because
699 -- here, this analysis is done only for error checking and consistency
700 -- purposes, so there's nothing else to be done.
702 elsif Nkind (Unit_Node) = N_Subprogram_Body then
703 if Acts_As_Spec (N) then
705 -- If the subprogram body is a child unit, we must create a
706 -- declaration for it, in order to properly load the parent(s).
707 -- After this, the original unit does not acts as a spec, because
708 -- there is an explicit one. If this unit appears in a context
709 -- clause, then an implicit with on the parent will be added when
710 -- installing the context. If this is the main unit, there is no
711 -- Unit_Table entry for the declaration (it has the unit number
712 -- of the main unit) and code generation is unaffected.
714 Unum := Get_Cunit_Unit_Number (N);
715 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
717 if Par_Spec_Name /= No_Unit_Name then
720 (Load_Name => Par_Spec_Name,
725 if Unum /= No_Unit then
727 -- Build subprogram declaration and attach parent unit to it
728 -- This subprogram declaration does not come from source,
729 -- Nevertheless the backend must generate debugging info for
730 -- it, and this must be indicated explicitly. We also mark
731 -- the body entity as a child unit now, to prevent a
732 -- cascaded error if the spec entity cannot be entered
736 Loc : constant Source_Ptr := Sloc (N);
737 SCS : constant Boolean :=
738 Get_Comes_From_Source_Default;
741 Set_Comes_From_Source_Default (False);
743 Make_Compilation_Unit (Loc,
744 Context_Items => New_Copy_List (Context_Items (N)),
746 Make_Subprogram_Declaration (Sloc (N),
749 (Specification (Unit_Node))),
751 Make_Compilation_Unit_Aux (Loc));
753 Set_Library_Unit (N, Lib_Unit);
754 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
755 Semantics (Lib_Unit);
757 -- Now that a separate declaration exists, the body
758 -- of the child unit does not act as spec any longer.
760 Set_Acts_As_Spec (N, False);
761 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
762 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
763 Set_Comes_From_Source_Default (SCS);
768 -- Here for subprogram with separate declaration
771 Semantics (Lib_Unit);
772 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
773 Version_Update (N, Lib_Unit);
776 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
777 N_Defining_Program_Unit_Name
779 Generate_Parent_References (
780 Specification (Unit_Node),
781 Scope (Defining_Entity (Unit (Lib_Unit))));
785 -- If it is a child unit, the parent must be elaborated first
786 -- and we update version, since we are dependent on our parent.
788 if Is_Child_Spec (Unit_Node) then
790 -- The analysis of the parent is done with style checks off
793 Save_Style_Check : constant Boolean := Style_Check;
794 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
795 Cunit_Boolean_Restrictions_Save;
798 if not GNAT_Mode then
799 Style_Check := False;
802 Semantics (Parent_Spec (Unit_Node));
803 Version_Update (N, Parent_Spec (Unit_Node));
804 Style_Check := Save_Style_Check;
805 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
809 -- With the analysis done, install the context. Note that we can't
810 -- install the context from the with clauses as we analyze them, because
811 -- each with clause must be analyzed in a clean visibility context, so
812 -- we have to wait and install them all at once.
816 if Is_Child_Spec (Unit_Node) then
818 -- Set the entities of all parents in the program_unit_name
820 Generate_Parent_References (
821 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
824 -- All components of the context: with-clauses, library unit, ancestors
825 -- if any, (and their context) are analyzed and installed.
827 -- Call special debug routine sm if this is the main unit
829 if Current_Sem_Unit = Main_Unit then
833 -- Now analyze the unit (package, subprogram spec, body) itself
837 if Warn_On_Redundant_Constructs then
838 Check_Redundant_Withs (Context_Items (N));
840 if Nkind (Unit_Node) = N_Package_Body then
841 Check_Redundant_Withs
842 (Context_Items => Context_Items (N),
843 Spec_Context_Items => Context_Items (Lib_Unit));
847 -- The above call might have made Unit_Node an N_Subprogram_Body from
848 -- something else, so propagate any Acts_As_Spec flag.
850 if Nkind (Unit_Node) = N_Subprogram_Body
851 and then Acts_As_Spec (Unit_Node)
853 Set_Acts_As_Spec (N);
856 -- Register predefined units in Rtsfind
859 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
861 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
862 Set_RTU_Loaded (Unit_Node);
866 -- Treat compilation unit pragmas that appear after the library unit
868 if Present (Pragmas_After (Aux_Decls_Node (N))) then
870 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
872 while Present (Prag_Node) loop
879 -- Generate distribution stubs if requested and no error
882 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
884 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
885 and then not Fatal_Error (Main_Unit)
887 if Is_RCI_Pkg_Spec_Or_Body (N) then
889 -- Regular RCI package
891 Add_Stub_Constructs (N);
893 elsif (Nkind (Unit_Node) = N_Package_Declaration
894 and then Is_Shared_Passive (Defining_Entity
895 (Specification (Unit_Node))))
896 or else (Nkind (Unit_Node) = N_Package_Body
898 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
900 -- Shared passive package
902 Add_Stub_Constructs (N);
904 elsif Nkind (Unit_Node) = N_Package_Instantiation
906 Is_Remote_Call_Interface
907 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
909 -- Instantiation of a RCI generic package
911 Add_Stub_Constructs (N);
915 -- Remove unit from visibility, so that environment is clean for
916 -- the next compilation, which is either the main unit or some
917 -- other unit in the context.
919 if Nkind_In (Unit_Node, N_Package_Declaration,
920 N_Package_Renaming_Declaration,
921 N_Subprogram_Declaration)
922 or else Nkind (Unit_Node) in N_Generic_Declaration
924 (Nkind (Unit_Node) = N_Subprogram_Body
925 and then Acts_As_Spec (Unit_Node))
927 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
929 -- If the unit is an instantiation whose body will be elaborated for
930 -- inlining purposes, use the the proper entity of the instance. The
931 -- entity may be missing if the instantiation was illegal.
933 elsif Nkind (Unit_Node) = N_Package_Instantiation
934 and then not Error_Posted (Unit_Node)
935 and then Present (Instance_Spec (Unit_Node))
937 Remove_Unit_From_Visibility
938 (Defining_Entity (Instance_Spec (Unit_Node)));
940 elsif Nkind (Unit_Node) = N_Package_Body
941 or else (Nkind (Unit_Node) = N_Subprogram_Body
942 and then not Acts_As_Spec (Unit_Node))
944 -- Bodies that are not the main unit are compiled if they are generic
945 -- or contain generic or inlined units. Their analysis brings in the
946 -- context of the corresponding spec (unit declaration) which must be
947 -- removed as well, to return the compilation environment to its
950 Remove_Context (Lib_Unit);
951 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
954 -- Last step is to deinstall the context we just installed as well as
955 -- the unit just compiled.
959 -- If this is the main unit and we are generating code, we must check
960 -- that all generic units in the context have a body if they need it,
961 -- even if they have not been instantiated. In the absence of .ali files
962 -- for generic units, we must force the load of the body, just to
963 -- produce the proper error if the body is absent. We skip this
964 -- verification if the main unit itself is generic.
966 if Get_Cunit_Unit_Number (N) = Main_Unit
967 and then Operating_Mode = Generate_Code
968 and then Expander_Active
970 -- Check whether the source for the body of the unit must be included
971 -- in a standalone library.
973 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
975 -- Indicate that the main unit is now analyzed, to catch possible
976 -- circularities between it and generic bodies. Remove main unit from
977 -- visibility. This might seem superfluous, but the main unit must
978 -- not be visible in the generic body expansions that follow.
980 Set_Analyzed (N, True);
981 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
986 Un : Unit_Number_Type;
988 Save_Style_Check : constant Boolean := Style_Check;
989 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
990 Cunit_Boolean_Restrictions_Save;
993 Item := First (Context_Items (N));
994 while Present (Item) loop
996 -- Check for explicit with clause
998 if Nkind (Item) = N_With_Clause
999 and then not Implicit_With (Item)
1001 -- Ada 2005 (AI-50217): Ignore limited-withed units
1003 and then not Limited_Present (Item)
1005 Nam := Entity (Name (Item));
1007 -- Compile generic subprogram, unless it is intrinsic or
1008 -- imported so no body is required, or generic package body
1009 -- if the package spec requires a body.
1011 if (Is_Generic_Subprogram (Nam)
1012 and then not Is_Intrinsic_Subprogram (Nam)
1013 and then not Is_Imported (Nam))
1014 or else (Ekind (Nam) = E_Generic_Package
1015 and then Unit_Requires_Body (Nam))
1017 Style_Check := False;
1019 if Present (Renamed_Object (Nam)) then
1022 (Load_Name => Get_Body_Name
1024 (Unit_Declaration_Node
1025 (Renamed_Object (Nam)))),
1033 (Load_Name => Get_Body_Name
1034 (Get_Unit_Name (Item)),
1041 if Un = No_Unit then
1043 ("body of generic unit& not found", Item, Nam);
1046 elsif not Analyzed (Cunit (Un))
1047 and then Un /= Main_Unit
1048 and then not Fatal_Error (Un)
1050 Style_Check := False;
1051 Semantics (Cunit (Un));
1059 Style_Check := Save_Style_Check;
1060 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1064 -- Deal with creating elaboration Boolean if needed. We create an
1065 -- elaboration boolean only for units that come from source since
1066 -- units manufactured by the compiler never need elab checks.
1068 if Comes_From_Source (N)
1069 and then Nkind_In (Unit_Node, N_Package_Declaration,
1070 N_Generic_Package_Declaration,
1071 N_Subprogram_Declaration,
1072 N_Generic_Subprogram_Declaration)
1075 Loc : constant Source_Ptr := Sloc (N);
1076 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1079 Spec_Id := Defining_Entity (Unit_Node);
1080 Generate_Definition (Spec_Id);
1082 -- See if an elaboration entity is required for possible access
1083 -- before elaboration checking. Note that we must allow for this
1084 -- even if -gnatE is not set, since a client may be compiled in
1085 -- -gnatE mode and reference the entity.
1087 -- These entities are also used by the binder to prevent multiple
1088 -- attempts to execute the elaboration code for the library case
1089 -- where the elaboration routine might otherwise be called more
1092 -- Case of units which do not require elaboration checks
1095 -- Pure units do not need checks
1099 -- Preelaborated units do not need checks
1101 or else Is_Preelaborated (Spec_Id)
1103 -- No checks needed if pagma Elaborate_Body present
1105 or else Has_Pragma_Elaborate_Body (Spec_Id)
1107 -- No checks needed if unit does not require a body
1109 or else not Unit_Requires_Body (Spec_Id)
1111 -- No checks needed for predefined files
1113 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1115 -- No checks required if no separate spec
1117 or else Acts_As_Spec (N)
1119 -- This is a case where we only need the entity for
1120 -- checking to prevent multiple elaboration checks.
1122 Set_Elaboration_Entity_Required (Spec_Id, False);
1124 -- Case of elaboration entity is required for access before
1125 -- elaboration checking (so certainly we must build it!)
1128 Set_Elaboration_Entity_Required (Spec_Id, True);
1131 Build_Elaboration_Entity (N, Spec_Id);
1135 -- Freeze the compilation unit entity. This for sure is needed because
1136 -- of some warnings that can be output (see Freeze_Subprogram), but may
1137 -- in general be required. If freezing actions result, place them in the
1138 -- compilation unit actions list, and analyze them.
1141 Loc : constant Source_Ptr := Sloc (N);
1142 L : constant List_Id :=
1143 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), Loc);
1145 while Is_Non_Empty_List (L) loop
1146 Insert_Library_Level_Action (Remove_Head (L));
1152 if Nkind (Unit_Node) = N_Package_Declaration
1153 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1154 and then Expander_Active
1157 Save_Style_Check : constant Boolean := Style_Check;
1158 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1159 Options : Style_Check_Options;
1162 Save_Style_Check_Options (Options);
1163 Reset_Style_Check_Options;
1164 Opt.Warning_Mode := Suppress;
1165 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1167 Reset_Style_Check_Options;
1168 Set_Style_Check_Options (Options);
1169 Style_Check := Save_Style_Check;
1170 Warning_Mode := Save_Warning;
1174 -- If we are generating obsolescent warnings, then here is where we
1175 -- generate them for the with'ed items. The reason for this special
1176 -- processing is that the normal mechanism of generating the warnings
1177 -- for referenced entities does not work for context clause references.
1178 -- That's because when we first analyze the context, it is too early to
1179 -- know if the with'ing unit is itself obsolescent (which suppresses
1182 if not GNAT_Mode and then Warn_On_Obsolescent_Feature then
1184 -- Push current compilation unit as scope, so that the test for
1185 -- being within an obsolescent unit will work correctly.
1187 Push_Scope (Defining_Entity (Unit_Node));
1189 -- Loop through context items to deal with with clauses
1197 Item := First (Context_Items (N));
1198 while Present (Item) loop
1199 if Nkind (Item) = N_With_Clause
1201 -- Suppress this check in limited-withed units. Further work
1202 -- needed here if we decide to incorporate this check on
1203 -- limited-withed units.
1205 and then not Limited_Present (Item)
1208 Ent := Entity (Nam);
1210 if Is_Obsolescent (Ent) then
1211 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1219 -- Remove temporary install of current unit as scope
1223 end Analyze_Compilation_Unit;
1225 ---------------------
1226 -- Analyze_Context --
1227 ---------------------
1229 procedure Analyze_Context (N : Node_Id) is
1230 Ukind : constant Node_Kind := Nkind (Unit (N));
1234 -- First process all configuration pragmas at the start of the context
1235 -- items. Strictly these are not part of the context clause, but that
1236 -- is where the parser puts them. In any case for sure we must analyze
1237 -- these before analyzing the actual context items, since they can have
1238 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1239 -- be with'ed as a result of changing categorizations in Ada 2005).
1241 Item := First (Context_Items (N));
1242 while Present (Item)
1243 and then Nkind (Item) = N_Pragma
1244 and then Pragma_Name (Item) in Configuration_Pragma_Names
1250 -- Loop through actual context items. This is done in two passes:
1252 -- a) The first pass analyzes non-limited with-clauses and also any
1253 -- configuration pragmas (we need to get the latter analyzed right
1254 -- away, since they can affect processing of subsequent items.
1256 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1258 while Present (Item) loop
1260 -- For with clause, analyze the with clause, and then update
1261 -- the version, since we are dependent on a unit that we with.
1263 if Nkind (Item) = N_With_Clause
1264 and then not Limited_Present (Item)
1266 -- Skip analyzing with clause if no unit, nothing to do (this
1267 -- happens for a with that references a non-existant unit)
1269 if Present (Library_Unit (Item)) then
1273 if not Implicit_With (Item) then
1274 Version_Update (N, Library_Unit (Item));
1277 -- Skip pragmas. Configuration pragmas at the start were handled in
1278 -- the loop above, and remaining pragmas are not processed until we
1279 -- actually install the context (see Install_Context). We delay the
1280 -- analysis of these pragmas to make sure that we have installed all
1281 -- the implicit with's on parent units.
1283 -- Skip use clauses at this stage, since we don't want to do any
1284 -- installing of potentially use visible entities until we we
1285 -- actually install the complete context (in Install_Context).
1286 -- Otherwise things can get installed in the wrong context.
1295 -- Second pass: examine all limited_with clauses. All other context
1296 -- items are ignored in this pass.
1298 Item := First (Context_Items (N));
1299 while Present (Item) loop
1300 if Nkind (Item) = N_With_Clause
1301 and then Limited_Present (Item)
1303 -- No need to check errors on implicitly generated limited-with
1306 if not Implicit_With (Item) then
1308 -- Check compilation unit containing the limited-with clause
1310 if not Nkind_In (Ukind, N_Package_Declaration,
1311 N_Subprogram_Declaration,
1312 N_Package_Renaming_Declaration,
1313 N_Subprogram_Renaming_Declaration)
1314 and then Ukind not in N_Generic_Declaration
1315 and then Ukind not in N_Generic_Renaming_Declaration
1316 and then Ukind not in N_Generic_Instantiation
1318 Error_Msg_N ("limited with_clause not allowed here", Item);
1320 -- Check wrong use of a limited with clause applied to the
1321 -- compilation unit containing the limited-with clause.
1323 -- limited with P.Q;
1324 -- package P.Q is ...
1326 elsif Unit (Library_Unit (Item)) = Unit (N) then
1327 Error_Msg_N ("wrong use of limited-with clause", Item);
1329 -- Check wrong use of limited-with clause applied to some
1330 -- immediate ancestor.
1332 elsif Is_Child_Spec (Unit (N)) then
1334 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1338 P := Parent_Spec (Unit (N));
1340 if Unit (P) = Lib_U then
1341 Error_Msg_N ("limited with_clause of immediate "
1342 & "ancestor not allowed", Item);
1346 exit when not Is_Child_Spec (Unit (P));
1347 P := Parent_Spec (Unit (P));
1352 -- Check if the limited-withed unit is already visible through
1353 -- some context clause of the current compilation unit or some
1354 -- ancestor of the current compilation unit.
1357 Lim_Unit_Name : constant Node_Id := Name (Item);
1358 Comp_Unit : Node_Id;
1360 Unit_Name : Node_Id;
1365 It := First (Context_Items (Comp_Unit));
1366 while Present (It) loop
1368 and then Nkind (It) = N_With_Clause
1369 and then not Limited_Present (It)
1371 Nkind_In (Unit (Library_Unit (It)),
1372 N_Package_Declaration,
1373 N_Package_Renaming_Declaration)
1375 if Nkind (Unit (Library_Unit (It))) =
1376 N_Package_Declaration
1378 Unit_Name := Name (It);
1380 Unit_Name := Name (Unit (Library_Unit (It)));
1383 -- Check if the named package (or some ancestor)
1384 -- leaves visible the full-view of the unit given
1385 -- in the limited-with clause
1388 if Designate_Same_Unit (Lim_Unit_Name,
1391 Error_Msg_Sloc := Sloc (It);
1393 ("simultaneous visibility of limited "
1394 & "and unlimited views not allowed",
1397 ("\unlimited view visible through "
1398 & "context clause #",
1402 elsif Nkind (Unit_Name) = N_Identifier then
1406 Unit_Name := Prefix (Unit_Name);
1413 exit when not Is_Child_Spec (Unit (Comp_Unit));
1415 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1420 -- Skip analyzing with clause if no unit, see above
1422 if Present (Library_Unit (Item)) then
1426 -- A limited_with does not impose an elaboration order, but
1427 -- there is a semantic dependency for recompilation purposes.
1429 if not Implicit_With (Item) then
1430 Version_Update (N, Library_Unit (Item));
1433 -- Pragmas and use clauses and with clauses other than limited
1434 -- with's are ignored in this pass through the context items.
1442 end Analyze_Context;
1444 -------------------------------
1445 -- Analyze_Package_Body_Stub --
1446 -------------------------------
1448 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1449 Id : constant Entity_Id := Defining_Identifier (N);
1453 -- The package declaration must be in the current declarative part
1455 Check_Stub_Level (N);
1456 Nam := Current_Entity_In_Scope (Id);
1458 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1459 Error_Msg_N ("missing specification for package stub", N);
1461 elsif Has_Completion (Nam)
1462 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1464 Error_Msg_N ("duplicate or redundant stub for package", N);
1467 -- Indicate that the body of the package exists. If we are doing
1468 -- only semantic analysis, the stub stands for the body. If we are
1469 -- generating code, the existence of the body will be confirmed
1470 -- when we load the proper body.
1472 Set_Has_Completion (Nam);
1473 Set_Scope (Defining_Entity (N), Current_Scope);
1474 Generate_Reference (Nam, Id, 'b');
1475 Analyze_Proper_Body (N, Nam);
1477 end Analyze_Package_Body_Stub;
1479 -------------------------
1480 -- Analyze_Proper_Body --
1481 -------------------------
1483 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1484 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1485 Unum : Unit_Number_Type;
1487 procedure Optional_Subunit;
1488 -- This procedure is called when the main unit is a stub, or when we
1489 -- are not generating code. In such a case, we analyze the subunit if
1490 -- present, which is user-friendly and in fact required for ASIS, but
1491 -- we don't complain if the subunit is missing.
1493 ----------------------
1494 -- Optional_Subunit --
1495 ----------------------
1497 procedure Optional_Subunit is
1498 Comp_Unit : Node_Id;
1501 -- Try to load subunit, but ignore any errors that occur during
1502 -- the loading of the subunit, by using the special feature in
1503 -- Errout to ignore all errors. Note that Fatal_Error will still
1504 -- be set, so we will be able to check for this case below.
1506 if not ASIS_Mode then
1507 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1512 (Load_Name => Subunit_Name,
1517 if not ASIS_Mode then
1518 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1521 -- All done if we successfully loaded the subunit
1524 and then (not Fatal_Error (Unum) or else Try_Semantics)
1526 Comp_Unit := Cunit (Unum);
1528 -- If the file was empty or seriously mangled, the unit
1529 -- itself may be missing.
1531 if No (Unit (Comp_Unit)) then
1533 ("subunit does not contain expected proper body", N);
1535 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1537 ("expected SEPARATE subunit, found child unit",
1538 Cunit_Entity (Unum));
1540 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1541 Analyze_Subunit (Comp_Unit);
1542 Set_Library_Unit (N, Comp_Unit);
1545 elsif Unum = No_Unit
1546 and then Present (Nam)
1548 if Is_Protected_Type (Nam) then
1549 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1551 Set_Corresponding_Body (
1552 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1555 end Optional_Subunit;
1557 -- Start of processing for Analyze_Proper_Body
1560 -- If the subunit is already loaded, it means that the main unit
1561 -- is a subunit, and that the current unit is one of its parents
1562 -- which was being analyzed to provide the needed context for the
1563 -- analysis of the subunit. In this case we analyze the subunit and
1564 -- continue with the parent, without looking a subsequent subunits.
1566 if Is_Loaded (Subunit_Name) then
1568 -- If the proper body is already linked to the stub node,
1569 -- the stub is in a generic unit and just needs analyzing.
1571 if Present (Library_Unit (N)) then
1572 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1573 Analyze_Subunit (Library_Unit (N));
1575 -- Otherwise we must load the subunit and link to it
1578 -- Load the subunit, this must work, since we originally
1579 -- loaded the subunit earlier on. So this will not really
1580 -- load it, just give access to it.
1584 (Load_Name => Subunit_Name,
1589 -- And analyze the subunit in the parent context (note that we
1590 -- do not call Semantics, since that would remove the parent
1591 -- context). Because of this, we have to manually reset the
1592 -- compiler state to Analyzing since it got destroyed by Load.
1594 if Unum /= No_Unit then
1595 Compiler_State := Analyzing;
1597 -- Check that the proper body is a subunit and not a child
1598 -- unit. If the unit was previously loaded, the error will
1599 -- have been emitted when copying the generic node, so we
1600 -- just return to avoid cascaded errors.
1602 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1606 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1607 Analyze_Subunit (Cunit (Unum));
1608 Set_Library_Unit (N, Cunit (Unum));
1612 -- If the main unit is a subunit, then we are just performing semantic
1613 -- analysis on that subunit, and any other subunits of any parent unit
1614 -- should be ignored, except that if we are building trees for ASIS
1615 -- usage we want to annotate the stub properly.
1617 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1618 and then Subunit_Name /= Unit_Name (Main_Unit)
1624 -- But before we return, set the flag for unloaded subunits. This
1625 -- will suppress junk warnings of variables in the same declarative
1626 -- part (or a higher level one) that are in danger of looking unused
1627 -- when in fact there might be a declaration in the subunit that we
1628 -- do not intend to load.
1630 Unloaded_Subunits := True;
1633 -- If the subunit is not already loaded, and we are generating code,
1634 -- then this is the case where compilation started from the parent,
1635 -- and we are generating code for an entire subunit tree. In that
1636 -- case we definitely need to load the subunit.
1638 -- In order to continue the analysis with the rest of the parent,
1639 -- and other subunits, we load the unit without requiring its
1640 -- presence, and emit a warning if not found, rather than terminating
1641 -- the compilation abruptly, as for other missing file problems.
1643 elsif Original_Operating_Mode = Generate_Code then
1645 -- If the proper body is already linked to the stub node,
1646 -- the stub is in a generic unit and just needs analyzing.
1648 -- We update the version. Although we are not technically
1649 -- semantically dependent on the subunit, given our approach
1650 -- of macro substitution of subunits, it makes sense to
1651 -- include it in the version identification.
1653 if Present (Library_Unit (N)) then
1654 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1655 Analyze_Subunit (Library_Unit (N));
1656 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1658 -- Otherwise we must load the subunit and link to it
1663 (Load_Name => Subunit_Name,
1668 if Original_Operating_Mode = Generate_Code
1669 and then Unum = No_Unit
1671 Error_Msg_Unit_1 := Subunit_Name;
1673 Get_File_Name (Subunit_Name, Subunit => True);
1675 ("subunit$$ in file{ not found?", N);
1676 Subunits_Missing := True;
1679 -- Load_Unit may reset Compiler_State, since it may have been
1680 -- necessary to parse an additional units, so we make sure
1681 -- that we reset it to the Analyzing state.
1683 Compiler_State := Analyzing;
1685 if Unum /= No_Unit then
1686 if Debug_Flag_L then
1687 Write_Str ("*** Loaded subunit from stub. Analyze");
1692 Comp_Unit : constant Node_Id := Cunit (Unum);
1695 -- Check for child unit instead of subunit
1697 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1699 ("expected SEPARATE subunit, found child unit",
1700 Cunit_Entity (Unum));
1702 -- OK, we have a subunit
1705 -- Set corresponding stub (even if errors)
1707 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1709 -- Analyze the unit if semantics active
1711 if not Fatal_Error (Unum) or else Try_Semantics then
1712 Analyze_Subunit (Comp_Unit);
1715 -- Set the library unit pointer in any case
1717 Set_Library_Unit (N, Comp_Unit);
1719 -- We update the version. Although we are not technically
1720 -- semantically dependent on the subunit, given our
1721 -- approach of macro substitution of subunits, it makes
1722 -- sense to include it in the version identification.
1724 Version_Update (Cunit (Main_Unit), Comp_Unit);
1730 -- The remaining case is when the subunit is not already loaded and
1731 -- we are not generating code. In this case we are just performing
1732 -- semantic analysis on the parent, and we are not interested in
1733 -- the subunit. For subprograms, analyze the stub as a body. For
1734 -- other entities the stub has already been marked as completed.
1739 end Analyze_Proper_Body;
1741 ----------------------------------
1742 -- Analyze_Protected_Body_Stub --
1743 ----------------------------------
1745 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1746 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1749 Check_Stub_Level (N);
1751 -- First occurence of name may have been as an incomplete type
1753 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1754 Nam := Full_View (Nam);
1758 or else not Is_Protected_Type (Etype (Nam))
1760 Error_Msg_N ("missing specification for Protected body", N);
1762 Set_Scope (Defining_Entity (N), Current_Scope);
1763 Set_Has_Completion (Etype (Nam));
1764 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1765 Analyze_Proper_Body (N, Etype (Nam));
1767 end Analyze_Protected_Body_Stub;
1769 ----------------------------------
1770 -- Analyze_Subprogram_Body_Stub --
1771 ----------------------------------
1773 -- A subprogram body stub can appear with or without a previous
1774 -- specification. If there is one, the analysis of the body will
1775 -- find it and verify conformance. The formals appearing in the
1776 -- specification of the stub play no role, except for requiring an
1777 -- additional conformance check. If there is no previous subprogram
1778 -- declaration, the stub acts as a spec, and provides the defining
1779 -- entity for the subprogram.
1781 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1785 Check_Stub_Level (N);
1787 -- Verify that the identifier for the stub is unique within this
1788 -- declarative part.
1790 if Nkind_In (Parent (N), N_Block_Statement,
1794 Decl := First (Declarations (Parent (N)));
1795 while Present (Decl)
1798 if Nkind (Decl) = N_Subprogram_Body_Stub
1799 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1800 Chars (Defining_Unit_Name (Specification (N))))
1802 Error_Msg_N ("identifier for stub is not unique", N);
1809 -- Treat stub as a body, which checks conformance if there is a previous
1810 -- declaration, or else introduces entity and its signature.
1812 Analyze_Subprogram_Body (N);
1813 Analyze_Proper_Body (N, Empty);
1814 end Analyze_Subprogram_Body_Stub;
1816 ---------------------
1817 -- Analyze_Subunit --
1818 ---------------------
1820 -- A subunit is compiled either by itself (for semantic checking)
1821 -- or as part of compiling the parent (for code generation). In
1822 -- either case, by the time we actually process the subunit, the
1823 -- parent has already been installed and analyzed. The node N is
1824 -- a compilation unit, whose context needs to be treated here,
1825 -- because we come directly here from the parent without calling
1826 -- Analyze_Compilation_Unit.
1828 -- The compilation context includes the explicit context of the
1829 -- subunit, and the context of the parent, together with the parent
1830 -- itself. In order to compile the current context, we remove the
1831 -- one inherited from the parent, in order to have a clean visibility
1832 -- table. We restore the parent context before analyzing the proper
1833 -- body itself. On exit, we remove only the explicit context of the
1836 procedure Analyze_Subunit (N : Node_Id) is
1837 Lib_Unit : constant Node_Id := Library_Unit (N);
1838 Par_Unit : constant Entity_Id := Current_Scope;
1840 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1841 Num_Scopes : Int := 0;
1842 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1843 Enclosing_Child : Entity_Id := Empty;
1844 Svg : constant Suppress_Array := Scope_Suppress;
1846 procedure Analyze_Subunit_Context;
1847 -- Capture names in use clauses of the subunit. This must be done
1848 -- before re-installing parent declarations, because items in the
1849 -- context must not be hidden by declarations local to the parent.
1851 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1852 -- Recursive procedure to restore scope of all ancestors of subunit,
1853 -- from outermost in. If parent is not a subunit, the call to install
1854 -- context installs context of spec and (if parent is a child unit)
1855 -- the context of its parents as well. It is confusing that parents
1856 -- should be treated differently in both cases, but the semantics are
1857 -- just not identical.
1859 procedure Re_Install_Use_Clauses;
1860 -- As part of the removal of the parent scope, the use clauses are
1861 -- removed, to be reinstalled when the context of the subunit has
1862 -- been analyzed. Use clauses may also have been affected by the
1863 -- analysis of the context of the subunit, so they have to be applied
1864 -- again, to insure that the compilation environment of the rest of
1865 -- the parent unit is identical.
1867 procedure Remove_Scope;
1868 -- Remove current scope from scope stack, and preserve the list
1869 -- of use clauses in it, to be reinstalled after context is analyzed.
1871 -----------------------------
1872 -- Analyze_Subunit_Context --
1873 -----------------------------
1875 procedure Analyze_Subunit_Context is
1878 Unit_Name : Entity_Id;
1881 Analyze_Context (N);
1883 -- Make withed units immediately visible. If child unit, make the
1884 -- ultimate parent immediately visible.
1886 Item := First (Context_Items (N));
1887 while Present (Item) loop
1888 if Nkind (Item) = N_With_Clause then
1890 -- Protect frontend against previous errors in context clauses
1892 if Nkind (Name (Item)) /= N_Selected_Component then
1893 if Error_Posted (Item) then
1897 Unit_Name := Entity (Name (Item));
1898 while Is_Child_Unit (Unit_Name) loop
1899 Set_Is_Visible_Child_Unit (Unit_Name);
1900 Unit_Name := Scope (Unit_Name);
1903 if not Is_Immediately_Visible (Unit_Name) then
1904 Set_Is_Immediately_Visible (Unit_Name);
1905 Set_Context_Installed (Item);
1910 elsif Nkind (Item) = N_Use_Package_Clause then
1911 Nam := First (Names (Item));
1912 while Present (Nam) loop
1917 elsif Nkind (Item) = N_Use_Type_Clause then
1918 Nam := First (Subtype_Marks (Item));
1919 while Present (Nam) loop
1928 -- Reset visibility of withed units. They will be made visible
1929 -- again when we install the subunit context.
1931 Item := First (Context_Items (N));
1932 while Present (Item) loop
1933 if Nkind (Item) = N_With_Clause
1935 -- Protect frontend against previous errors in context clauses
1937 and then Nkind (Name (Item)) /= N_Selected_Component
1938 and then not Error_Posted (Item)
1940 Unit_Name := Entity (Name (Item));
1941 while Is_Child_Unit (Unit_Name) loop
1942 Set_Is_Visible_Child_Unit (Unit_Name, False);
1943 Unit_Name := Scope (Unit_Name);
1946 if Context_Installed (Item) then
1947 Set_Is_Immediately_Visible (Unit_Name, False);
1948 Set_Context_Installed (Item, False);
1954 end Analyze_Subunit_Context;
1956 ------------------------
1957 -- Re_Install_Parents --
1958 ------------------------
1960 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
1964 if Nkind (Unit (L)) = N_Subunit then
1965 Re_Install_Parents (Library_Unit (L), Scope (Scop));
1968 Install_Context (L);
1970 -- If the subunit occurs within a child unit, we must restore the
1971 -- immediate visibility of any siblings that may occur in context.
1973 if Present (Enclosing_Child) then
1974 Install_Siblings (Enclosing_Child, L);
1979 if Scop /= Par_Unit then
1980 Set_Is_Immediately_Visible (Scop);
1983 -- Make entities in scope visible again. For child units, restore
1984 -- visibility only if they are actually in context.
1986 E := First_Entity (Current_Scope);
1987 while Present (E) loop
1988 if not Is_Child_Unit (E)
1989 or else Is_Visible_Child_Unit (E)
1991 Set_Is_Immediately_Visible (E);
1997 -- A subunit appears within a body, and for a nested subunits
1998 -- all the parents are bodies. Restore full visibility of their
1999 -- private entities.
2001 if Ekind (Scop) = E_Package
2002 or else Ekind (Scop) = E_Generic_Package
2004 Set_In_Package_Body (Scop);
2005 Install_Private_Declarations (Scop);
2007 end Re_Install_Parents;
2009 ----------------------------
2010 -- Re_Install_Use_Clauses --
2011 ----------------------------
2013 procedure Re_Install_Use_Clauses is
2016 for J in reverse 1 .. Num_Scopes loop
2017 U := Use_Clauses (J);
2018 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2019 Install_Use_Clauses (U, Force_Installation => True);
2021 end Re_Install_Use_Clauses;
2027 procedure Remove_Scope is
2031 Num_Scopes := Num_Scopes + 1;
2032 Use_Clauses (Num_Scopes) :=
2033 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2035 E := First_Entity (Current_Scope);
2036 while Present (E) loop
2037 Set_Is_Immediately_Visible (E, False);
2041 if Is_Child_Unit (Current_Scope) then
2042 Enclosing_Child := Current_Scope;
2048 -- Start of processing for Analyze_Subunit
2051 if not Is_Empty_List (Context_Items (N)) then
2053 -- Save current use clauses
2056 Remove_Context (Lib_Unit);
2058 -- Now remove parents and their context, including enclosing
2059 -- subunits and the outer parent body which is not a subunit.
2061 if Present (Lib_Spec) then
2062 Remove_Context (Lib_Spec);
2064 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2065 Lib_Spec := Library_Unit (Lib_Spec);
2067 Remove_Context (Lib_Spec);
2070 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2074 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2075 Remove_Context (Library_Unit (Lib_Spec));
2079 Set_Is_Immediately_Visible (Par_Unit, False);
2081 Analyze_Subunit_Context;
2083 Re_Install_Parents (Lib_Unit, Par_Unit);
2084 Set_Is_Immediately_Visible (Par_Unit);
2086 -- If the context includes a child unit of the parent of the
2087 -- subunit, the parent will have been removed from visibility,
2088 -- after compiling that cousin in the context. The visibility
2089 -- of the parent must be restored now. This also applies if the
2090 -- context includes another subunit of the same parent which in
2091 -- turn includes a child unit in its context.
2093 if Ekind (Par_Unit) = E_Package
2094 or else Ekind (Par_Unit) = E_Generic_Package
2096 if not Is_Immediately_Visible (Par_Unit)
2097 or else (Present (First_Entity (Par_Unit))
2098 and then not Is_Immediately_Visible
2099 (First_Entity (Par_Unit)))
2101 Set_Is_Immediately_Visible (Par_Unit);
2102 Install_Visible_Declarations (Par_Unit);
2103 Install_Private_Declarations (Par_Unit);
2107 Re_Install_Use_Clauses;
2108 Install_Context (N);
2110 -- Restore state of suppress flags for current body
2112 Scope_Suppress := Svg;
2114 -- If the subunit is within a child unit, then siblings of any
2115 -- parent unit that appear in the context clause of the subunit
2116 -- must also be made immediately visible.
2118 if Present (Enclosing_Child) then
2119 Install_Siblings (Enclosing_Child, N);
2124 Analyze (Proper_Body (Unit (N)));
2127 -- The subunit may contain a with_clause on a sibling of some
2128 -- ancestor. Removing the context will remove from visibility those
2129 -- ancestor child units, which must be restored to the visibility
2130 -- they have in the enclosing body.
2132 if Present (Enclosing_Child) then
2138 and then Is_Child_Unit (C)
2140 Set_Is_Immediately_Visible (C);
2141 Set_Is_Visible_Child_Unit (C);
2146 end Analyze_Subunit;
2148 ----------------------------
2149 -- Analyze_Task_Body_Stub --
2150 ----------------------------
2152 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2153 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2154 Loc : constant Source_Ptr := Sloc (N);
2157 Check_Stub_Level (N);
2159 -- First occurence of name may have been as an incomplete type
2161 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2162 Nam := Full_View (Nam);
2166 or else not Is_Task_Type (Etype (Nam))
2168 Error_Msg_N ("missing specification for task body", N);
2170 Set_Scope (Defining_Entity (N), Current_Scope);
2171 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2172 Set_Has_Completion (Etype (Nam));
2173 Analyze_Proper_Body (N, Etype (Nam));
2175 -- Set elaboration flag to indicate that entity is callable.
2176 -- This cannot be done in the expansion of the body itself,
2177 -- because the proper body is not in a declarative part. This
2178 -- is only done if expansion is active, because the context
2179 -- may be generic and the flag not defined yet.
2181 if Expander_Active then
2183 Make_Assignment_Statement (Loc,
2185 Make_Identifier (Loc,
2186 New_External_Name (Chars (Etype (Nam)), 'E')),
2187 Expression => New_Reference_To (Standard_True, Loc)));
2191 end Analyze_Task_Body_Stub;
2193 -------------------------
2194 -- Analyze_With_Clause --
2195 -------------------------
2197 -- Analyze the declaration of a unit in a with clause. At end,
2198 -- label the with clause with the defining entity for the unit.
2200 procedure Analyze_With_Clause (N : Node_Id) is
2202 -- Retrieve the original kind of the unit node, before analysis.
2203 -- If it is a subprogram instantiation, its analysis below will
2204 -- rewrite as the declaration of the wrapper package. If the same
2205 -- instantiation appears indirectly elsewhere in the context, it
2206 -- will have been analyzed already.
2208 Unit_Kind : constant Node_Kind :=
2209 Nkind (Original_Node (Unit (Library_Unit (N))));
2210 Nam : constant Node_Id := Name (N);
2212 Par_Name : Entity_Id;
2217 -- Set True if the unit currently being compiled is an internal unit
2219 Save_Style_Check : constant Boolean := Opt.Style_Check;
2220 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
2221 Cunit_Boolean_Restrictions_Save;
2224 if Limited_Present (N) then
2226 -- Ada 2005 (AI-50217): Build visibility structures but do not
2227 -- analyze the unit.
2229 Build_Limited_Views (N);
2233 -- We reset ordinary style checking during the analysis of a with'ed
2234 -- unit, but we do NOT reset GNAT special analysis mode (the latter
2235 -- definitely *does* apply to with'ed units).
2237 if not GNAT_Mode then
2238 Style_Check := False;
2241 -- If the library unit is a predefined unit, and we are in high
2242 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2243 -- for the analysis of the with'ed unit. This mode does not prevent
2244 -- explicit with'ing of run-time units.
2246 if Configurable_Run_Time_Mode
2248 Is_Predefined_File_Name
2249 (Unit_File_Name (Get_Source_Unit (Unit (Library_Unit (N)))))
2251 Configurable_Run_Time_Mode := False;
2252 Semantics (Library_Unit (N));
2253 Configurable_Run_Time_Mode := True;
2256 Semantics (Library_Unit (N));
2259 U := Unit (Library_Unit (N));
2260 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2262 -- Following checks are skipped for dummy packages (those supplied for
2263 -- with's where no matching file could be found). Such packages are
2264 -- identified by the Sloc value being set to No_Location
2266 if Sloc (U) /= No_Location then
2268 -- Check restrictions, except that we skip the check if this is an
2269 -- internal unit unless we are compiling the internal unit as the
2270 -- main unit. We also skip this for dummy packages.
2272 Check_Restriction_No_Dependence (Nam, N);
2274 if not Intunit or else Current_Sem_Unit = Main_Unit then
2275 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2278 -- Deal with special case of GNAT.Current_Exceptions which interacts
2279 -- with the optimization of local raise statements into gotos.
2281 if Nkind (Nam) = N_Selected_Component
2282 and then Nkind (Prefix (Nam)) = N_Identifier
2283 and then Chars (Prefix (Nam)) = Name_Gnat
2284 and then (Chars (Selector_Name (Nam)) = Name_Most_Recent_Exception
2286 Chars (Selector_Name (Nam)) = Name_Exception_Traces)
2288 Check_Restriction (No_Exception_Propagation, N);
2289 Special_Exception_Package_Used := True;
2292 -- Check for inappropriate with of internal implementation unit if we
2293 -- are currently compiling the main unit and the main unit is itself
2294 -- not an internal unit. We do not issue this message for implicit
2295 -- with's generated by the compiler itself.
2297 if Implementation_Unit_Warnings
2298 and then Current_Sem_Unit = Main_Unit
2299 and then not Intunit
2300 and then not Implicit_With (N)
2301 and then not GNAT_Mode
2304 U_Kind : constant Kind_Of_Unit :=
2305 Get_Kind_Of_Unit (Get_Source_Unit (U));
2308 if U_Kind = Implementation_Unit then
2309 Error_Msg_F ("& is an internal 'G'N'A'T unit?", Name (N));
2311 ("\use of this unit is non-portable " &
2312 "and version-dependent?",
2315 elsif U_Kind = Ada_05_Unit
2316 and then Ada_Version < Ada_05
2317 and then Warn_On_Ada_2005_Compatibility
2319 Error_Msg_N ("& is an Ada 2005 unit?", Name (N));
2325 -- Semantic analysis of a generic unit is performed on a copy of
2326 -- the original tree. Retrieve the entity on which semantic info
2327 -- actually appears.
2329 if Unit_Kind in N_Generic_Declaration then
2330 E_Name := Defining_Entity (U);
2332 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2333 -- the case of an instantiation, semantic analysis above will have
2334 -- replaced the unit by its instantiated version. If the instance body
2335 -- has been generated, the instance now denotes the body entity. For
2336 -- visibility purposes we need the entity of its spec.
2338 elsif (Unit_Kind = N_Package_Instantiation
2339 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2340 N_Package_Instantiation)
2341 and then Nkind (U) = N_Package_Body
2343 E_Name := Corresponding_Spec (U);
2345 elsif Unit_Kind = N_Package_Instantiation
2346 and then Nkind (U) = N_Package_Instantiation
2348 -- If the instance has not been rewritten as a package declaration,
2349 -- then it appeared already in a previous with clause. Retrieve
2350 -- the entity from the previous instance.
2352 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2354 elsif Unit_Kind in N_Subprogram_Instantiation then
2356 -- Instantiation node is replaced with a wrapper package. Retrieve
2357 -- the visible subprogram created by the instance from corresponding
2358 -- attribute of the wrapper.
2360 E_Name := Related_Instance (Defining_Entity (U));
2362 elsif Unit_Kind = N_Package_Renaming_Declaration
2363 or else Unit_Kind in N_Generic_Renaming_Declaration
2365 E_Name := Defining_Entity (U);
2367 elsif Unit_Kind = N_Subprogram_Body
2368 and then Nkind (Name (N)) = N_Selected_Component
2369 and then not Acts_As_Spec (Library_Unit (N))
2371 -- For a child unit that has no spec, one has been created and
2372 -- analyzed. The entity required is that of the spec.
2374 E_Name := Corresponding_Spec (U);
2377 E_Name := Defining_Entity (U);
2380 if Nkind (Name (N)) = N_Selected_Component then
2382 -- Child unit in a with clause
2384 Change_Selected_Component_To_Expanded_Name (Name (N));
2387 -- Restore style checks and restrictions
2389 Style_Check := Save_Style_Check;
2390 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
2392 -- Record the reference, but do NOT set the unit as referenced, we want
2393 -- to consider the unit as unreferenced if this is the only reference
2396 Set_Entity_With_Style_Check (Name (N), E_Name);
2397 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2399 if Is_Child_Unit (E_Name) then
2400 Pref := Prefix (Name (N));
2401 Par_Name := Scope (E_Name);
2402 while Nkind (Pref) = N_Selected_Component loop
2403 Change_Selected_Component_To_Expanded_Name (Pref);
2404 Set_Entity_With_Style_Check (Pref, Par_Name);
2406 Generate_Reference (Par_Name, Pref);
2407 Pref := Prefix (Pref);
2409 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2410 -- is set to Standard_Standard, and no attempt should be made to
2411 -- further unwind scopes.
2413 if Par_Name /= Standard_Standard then
2414 Par_Name := Scope (Par_Name);
2418 if Present (Entity (Pref))
2419 and then not Analyzed (Parent (Parent (Entity (Pref))))
2421 -- If the entity is set without its unit being compiled, the
2422 -- original parent is a renaming, and Par_Name is the renamed
2423 -- entity. For visibility purposes, we need the original entity,
2424 -- which must be analyzed now because Load_Unit directly retrieves
2425 -- the renamed unit, and the renaming declaration itself has not
2428 Analyze (Parent (Parent (Entity (Pref))));
2429 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2430 Par_Name := Entity (Pref);
2433 Set_Entity_With_Style_Check (Pref, Par_Name);
2434 Generate_Reference (Par_Name, Pref);
2437 -- If the withed unit is System, and a system extension pragma is
2438 -- present, compile the extension now, rather than waiting for a
2439 -- visibility check on a specific entity.
2441 if Chars (E_Name) = Name_System
2442 and then Scope (E_Name) = Standard_Standard
2443 and then Present (System_Extend_Unit)
2444 and then Present_System_Aux (N)
2446 -- If the extension is not present, an error will have been emitted
2451 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2452 -- to private_with units; they will be made visible later (just before
2453 -- the private part is analyzed)
2455 if Private_Present (N) then
2456 Set_Is_Immediately_Visible (E_Name, False);
2458 end Analyze_With_Clause;
2460 ------------------------------
2461 -- Check_Private_Child_Unit --
2462 ------------------------------
2464 procedure Check_Private_Child_Unit (N : Node_Id) is
2465 Lib_Unit : constant Node_Id := Unit (N);
2467 Curr_Unit : Entity_Id;
2468 Sub_Parent : Node_Id;
2469 Priv_Child : Entity_Id;
2470 Par_Lib : Entity_Id;
2473 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2474 -- Returns true if and only if the library unit is declared with
2475 -- an explicit designation of private.
2477 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2478 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2481 return Private_Present (Comp_Unit);
2482 end Is_Private_Library_Unit;
2484 -- Start of processing for Check_Private_Child_Unit
2487 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2488 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2489 Par_Lib := Curr_Unit;
2491 elsif Nkind (Lib_Unit) = N_Subunit then
2493 -- The parent is itself a body. The parent entity is to be found in
2494 -- the corresponding spec.
2496 Sub_Parent := Library_Unit (N);
2497 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2499 -- If the parent itself is a subunit, Curr_Unit is the entity
2500 -- of the enclosing body, retrieve the spec entity which is
2501 -- the proper ancestor we need for the following tests.
2503 if Ekind (Curr_Unit) = E_Package_Body then
2504 Curr_Unit := Spec_Entity (Curr_Unit);
2507 Par_Lib := Curr_Unit;
2510 Curr_Unit := Defining_Entity (Lib_Unit);
2512 Par_Lib := Curr_Unit;
2513 Par_Spec := Parent_Spec (Lib_Unit);
2515 if No (Par_Spec) then
2518 Par_Lib := Defining_Entity (Unit (Par_Spec));
2522 -- Loop through context items
2524 Item := First (Context_Items (N));
2525 while Present (Item) loop
2527 -- Ada 2005 (AI-262): Allow private_with of a private child package
2528 -- in public siblings
2530 if Nkind (Item) = N_With_Clause
2531 and then not Implicit_With (Item)
2532 and then Is_Private_Descendant (Entity (Name (Item)))
2534 Priv_Child := Entity (Name (Item));
2537 Curr_Parent : Entity_Id := Par_Lib;
2538 Child_Parent : Entity_Id := Scope (Priv_Child);
2539 Prv_Ancestor : Entity_Id := Child_Parent;
2540 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2543 -- If the child unit is a public child then locate the nearest
2544 -- private ancestor. Child_Parent will then be set to the
2545 -- parent of that ancestor.
2547 if not Is_Private_Library_Unit (Priv_Child) then
2548 while Present (Prv_Ancestor)
2549 and then not Is_Private_Library_Unit (Prv_Ancestor)
2551 Prv_Ancestor := Scope (Prv_Ancestor);
2554 if Present (Prv_Ancestor) then
2555 Child_Parent := Scope (Prv_Ancestor);
2559 while Present (Curr_Parent)
2560 and then Curr_Parent /= Standard_Standard
2561 and then Curr_Parent /= Child_Parent
2564 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2565 Curr_Parent := Scope (Curr_Parent);
2568 if No (Curr_Parent) then
2569 Curr_Parent := Standard_Standard;
2572 if Curr_Parent /= Child_Parent then
2573 if Ekind (Priv_Child) = E_Generic_Package
2574 and then Chars (Priv_Child) in Text_IO_Package_Name
2575 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2578 ("& is a nested package, not a compilation unit",
2579 Name (Item), Priv_Child);
2583 ("unit in with clause is private child unit!", Item);
2585 ("\current unit must also have parent&!",
2586 Item, Child_Parent);
2590 or else Private_Present (Item)
2591 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2592 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2593 and then not Acts_As_Spec (Parent (Lib_Unit)))
2599 ("current unit must also be private descendant of&",
2600 Item, Child_Parent);
2608 end Check_Private_Child_Unit;
2610 ----------------------
2611 -- Check_Stub_Level --
2612 ----------------------
2614 procedure Check_Stub_Level (N : Node_Id) is
2615 Par : constant Node_Id := Parent (N);
2616 Kind : constant Node_Kind := Nkind (Par);
2619 if Nkind_In (Kind, N_Package_Body,
2623 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
2627 -- In an instance, a missing stub appears at any level. A warning
2628 -- message will have been emitted already for the missing file.
2630 elsif not In_Instance then
2631 Error_Msg_N ("stub cannot appear in an inner scope", N);
2633 elsif Expander_Active then
2634 Error_Msg_N ("missing proper body", N);
2636 end Check_Stub_Level;
2638 ------------------------
2639 -- Expand_With_Clause --
2640 ------------------------
2642 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2643 Loc : constant Source_Ptr := Sloc (Nam);
2644 Ent : constant Entity_Id := Entity (Nam);
2648 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2649 -- Comment requireed here ???
2651 ---------------------
2652 -- Build_Unit_Name --
2653 ---------------------
2655 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2656 Renaming : Entity_Id;
2660 if Nkind (Nam) = N_Identifier then
2662 -- If the parent unit P in the name of the with_clause for P.Q
2663 -- is a renaming of package R, then the entity of the parent is
2664 -- set to R, but the identifier retains Chars (P) to be consistent
2665 -- with the source (see details in lib-load). However, the
2666 -- implicit_with_clause for the parent must make the entity for
2667 -- P visible, because P.Q may be used as a prefix within the
2668 -- current unit. The entity for P is the current_entity with that
2669 -- name, because the package renaming declaration for it has just
2670 -- been analyzed. Note that this case can only happen if P.Q has
2671 -- already appeared in a previous with_clause in a related unit,
2672 -- such as the library body of the current unit.
2674 if Chars (Nam) /= Chars (Entity (Nam)) then
2675 Renaming := Current_Entity (Nam);
2676 pragma Assert (Renamed_Entity (Renaming) = Entity (Nam));
2677 return New_Occurrence_Of (Renaming, Loc);
2680 return New_Occurrence_Of (Entity (Nam), Loc);
2685 Make_Expanded_Name (Loc,
2686 Chars => Chars (Entity (Nam)),
2687 Prefix => Build_Unit_Name (Prefix (Nam)),
2688 Selector_Name => New_Occurrence_Of (Entity (Nam), Loc));
2689 Set_Entity (Result, Entity (Nam));
2692 end Build_Unit_Name;
2694 -- Start of processing for Expand_With_Clause
2697 New_Nodes_OK := New_Nodes_OK + 1;
2699 Make_With_Clause (Loc,
2700 Name => Build_Unit_Name (Nam));
2702 P := Parent (Unit_Declaration_Node (Ent));
2703 Set_Library_Unit (Withn, P);
2704 Set_Corresponding_Spec (Withn, Ent);
2705 Set_First_Name (Withn, True);
2706 Set_Implicit_With (Withn, True);
2708 -- If the unit is a package declaration, a private_with_clause on a
2709 -- child unit implies that the implicit with on the parent is also
2712 if Nkind (Unit (N)) = N_Package_Declaration then
2713 Set_Private_Present (Withn, Private_Present (Item));
2716 Prepend (Withn, Context_Items (N));
2717 Mark_Rewrite_Insertion (Withn);
2718 Install_Withed_Unit (Withn);
2720 if Nkind (Nam) = N_Expanded_Name then
2721 Expand_With_Clause (Item, Prefix (Nam), N);
2724 New_Nodes_OK := New_Nodes_OK - 1;
2725 end Expand_With_Clause;
2727 -----------------------
2728 -- Get_Parent_Entity --
2729 -----------------------
2731 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2733 if Nkind (Unit) = N_Package_Body
2734 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
2736 return Defining_Entity
2737 (Specification (Instance_Spec (Original_Node (Unit))));
2738 elsif Nkind (Unit) = N_Package_Instantiation then
2739 return Defining_Entity (Specification (Instance_Spec (Unit)));
2741 return Defining_Entity (Unit);
2743 end Get_Parent_Entity;
2745 -----------------------------
2746 -- Implicit_With_On_Parent --
2747 -----------------------------
2749 procedure Implicit_With_On_Parent
2750 (Child_Unit : Node_Id;
2753 Loc : constant Source_Ptr := Sloc (N);
2754 P : constant Node_Id := Parent_Spec (Child_Unit);
2755 P_Unit : Node_Id := Unit (P);
2756 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
2759 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2760 -- Build prefix of child unit name. Recurse if needed
2762 function Build_Unit_Name return Node_Id;
2763 -- If the unit is a child unit, build qualified name with all ancestors
2765 -------------------------
2766 -- Build_Ancestor_Name --
2767 -------------------------
2769 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
2770 P_Ref : constant Node_Id :=
2771 New_Reference_To (Defining_Entity (P), Loc);
2772 P_Spec : Node_Id := P;
2775 -- Ancestor may have been rewritten as a package body. Retrieve
2776 -- the original spec to trace earlier ancestors.
2778 if Nkind (P) = N_Package_Body
2779 and then Nkind (Original_Node (P)) = N_Package_Instantiation
2781 P_Spec := Original_Node (P);
2784 if No (Parent_Spec (P_Spec)) then
2788 Make_Selected_Component (Loc,
2789 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
2790 Selector_Name => P_Ref);
2792 end Build_Ancestor_Name;
2794 ---------------------
2795 -- Build_Unit_Name --
2796 ---------------------
2798 function Build_Unit_Name return Node_Id is
2801 if No (Parent_Spec (P_Unit)) then
2802 return New_Reference_To (P_Name, Loc);
2805 Make_Expanded_Name (Loc,
2806 Chars => Chars (P_Name),
2807 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
2808 Selector_Name => New_Reference_To (P_Name, Loc));
2809 Set_Entity (Result, P_Name);
2812 end Build_Unit_Name;
2814 -- Start of processing for Implicit_With_On_Parent
2817 -- The unit of the current compilation may be a package body that
2818 -- replaces an instance node. In this case we need the original instance
2819 -- node to construct the proper parent name.
2821 if Nkind (P_Unit) = N_Package_Body
2822 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
2824 P_Unit := Original_Node (P_Unit);
2827 -- We add the implicit with if the child unit is the current unit being
2828 -- compiled. If the current unit is a body, we do not want to add an
2829 -- implicit_with a second time to the corresponding spec.
2831 if Nkind (Child_Unit) = N_Package_Declaration
2832 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
2837 New_Nodes_OK := New_Nodes_OK + 1;
2838 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
2840 Set_Library_Unit (Withn, P);
2841 Set_Corresponding_Spec (Withn, P_Name);
2842 Set_First_Name (Withn, True);
2843 Set_Implicit_With (Withn, True);
2845 -- Node is placed at the beginning of the context items, so that
2846 -- subsequent use clauses on the parent can be validated.
2848 Prepend (Withn, Context_Items (N));
2849 Mark_Rewrite_Insertion (Withn);
2850 Install_Withed_Unit (Withn);
2852 if Is_Child_Spec (P_Unit) then
2853 Implicit_With_On_Parent (P_Unit, N);
2856 New_Nodes_OK := New_Nodes_OK - 1;
2857 end Implicit_With_On_Parent;
2863 function In_Chain (E : Entity_Id) return Boolean is
2867 H := Current_Entity (E);
2868 while Present (H) loop
2879 ---------------------
2880 -- Install_Context --
2881 ---------------------
2883 procedure Install_Context (N : Node_Id) is
2884 Lib_Unit : constant Node_Id := Unit (N);
2887 Install_Context_Clauses (N);
2889 if Is_Child_Spec (Lib_Unit) then
2890 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
2893 Install_Limited_Context_Clauses (N);
2894 end Install_Context;
2896 -----------------------------
2897 -- Install_Context_Clauses --
2898 -----------------------------
2900 procedure Install_Context_Clauses (N : Node_Id) is
2901 Lib_Unit : constant Node_Id := Unit (N);
2903 Uname_Node : Entity_Id;
2904 Check_Private : Boolean := False;
2905 Decl_Node : Node_Id;
2906 Lib_Parent : Entity_Id;
2909 -- First skip configuration pragmas at the start of the context. They
2910 -- are not technically part of the context clause, but that's where the
2911 -- parser puts them. Note they were analyzed in Analyze_Context.
2913 Item := First (Context_Items (N));
2914 while Present (Item)
2915 and then Nkind (Item) = N_Pragma
2916 and then Pragma_Name (Item) in Configuration_Pragma_Names
2921 -- Loop through the actual context clause items. We process everything
2922 -- except Limited_With clauses in this routine. Limited_With clauses
2923 -- are separately installed (see Install_Limited_Context_Clauses).
2925 while Present (Item) loop
2927 -- Case of explicit WITH clause
2929 if Nkind (Item) = N_With_Clause
2930 and then not Implicit_With (Item)
2932 if Limited_Present (Item) then
2934 -- Limited withed units will be installed later
2938 -- If Name (Item) is not an entity name, something is wrong, and
2939 -- this will be detected in due course, for now ignore the item
2941 elsif not Is_Entity_Name (Name (Item)) then
2944 elsif No (Entity (Name (Item))) then
2945 Set_Entity (Name (Item), Any_Id);
2949 Uname_Node := Entity (Name (Item));
2951 if Is_Private_Descendant (Uname_Node) then
2952 Check_Private := True;
2955 Install_Withed_Unit (Item);
2957 Decl_Node := Unit_Declaration_Node (Uname_Node);
2959 -- If the unit is a subprogram instance, it appears nested within
2960 -- a package that carries the parent information.
2962 if Is_Generic_Instance (Uname_Node)
2963 and then Ekind (Uname_Node) /= E_Package
2965 Decl_Node := Parent (Parent (Decl_Node));
2968 if Is_Child_Spec (Decl_Node) then
2969 if Nkind (Name (Item)) = N_Expanded_Name then
2970 Expand_With_Clause (Item, Prefix (Name (Item)), N);
2972 -- If not an expanded name, the child unit must be a
2973 -- renaming, nothing to do.
2978 elsif Nkind (Decl_Node) = N_Subprogram_Body
2979 and then not Acts_As_Spec (Parent (Decl_Node))
2980 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
2982 Implicit_With_On_Parent
2983 (Unit (Library_Unit (Parent (Decl_Node))), N);
2986 -- Check license conditions unless this is a dummy unit
2988 if Sloc (Library_Unit (Item)) /= No_Location then
2989 License_Check : declare
2991 Withu : constant Unit_Number_Type :=
2992 Get_Source_Unit (Library_Unit (Item));
2994 Withl : constant License_Type :=
2995 License (Source_Index (Withu));
2997 Unitl : constant License_Type :=
2998 License (Source_Index (Current_Sem_Unit));
3000 procedure License_Error;
3001 -- Signal error of bad license
3007 procedure License_Error is
3010 ("?license of with'ed unit & may be inconsistent",
3014 -- Start of processing for License_Check
3017 -- Exclude license check if withed unit is an internal unit.
3018 -- This situation arises e.g. with the GPL version of GNAT.
3020 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3023 -- Otherwise check various cases
3035 if Withl = Restricted then
3039 when Modified_GPL =>
3040 if Withl = Restricted or else Withl = GPL then
3044 when Unrestricted =>
3051 -- Case of USE PACKAGE clause
3053 elsif Nkind (Item) = N_Use_Package_Clause then
3054 Analyze_Use_Package (Item);
3056 -- Case of USE TYPE clause
3058 elsif Nkind (Item) = N_Use_Type_Clause then
3059 Analyze_Use_Type (Item);
3063 elsif Nkind (Item) = N_Pragma then
3071 if Is_Child_Spec (Lib_Unit) then
3073 -- The unit also has implicit withs on its own parents
3075 if No (Context_Items (N)) then
3076 Set_Context_Items (N, New_List);
3079 Implicit_With_On_Parent (Lib_Unit, N);
3082 -- If the unit is a body, the context of the specification must also
3085 if Nkind (Lib_Unit) = N_Package_Body
3086 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3087 and then not Acts_As_Spec (N))
3089 Install_Context (Library_Unit (N));
3091 if Is_Child_Spec (Unit (Library_Unit (N))) then
3093 -- If the unit is the body of a public child unit, the private
3094 -- declarations of the parent must be made visible. If the child
3095 -- unit is private, the private declarations have been installed
3096 -- already in the call to Install_Parents for the spec. Installing
3097 -- private declarations must be done for all ancestors of public
3098 -- child units. In addition, sibling units mentioned in the
3099 -- context clause of the body are directly visible.
3107 Lib_Spec := Unit (Library_Unit (N));
3108 while Is_Child_Spec (Lib_Spec) loop
3109 P := Unit (Parent_Spec (Lib_Spec));
3110 P_Name := Defining_Entity (P);
3112 if not (Private_Present (Parent (Lib_Spec)))
3113 and then not In_Private_Part (P_Name)
3115 Install_Private_Declarations (P_Name);
3116 Install_Private_With_Clauses (P_Name);
3117 Set_Use (Private_Declarations (Specification (P)));
3125 -- For a package body, children in context are immediately visible
3127 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3130 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3131 N_Generic_Subprogram_Declaration,
3132 N_Package_Declaration,
3133 N_Subprogram_Declaration)
3135 if Is_Child_Spec (Lib_Unit) then
3136 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3137 Set_Is_Private_Descendant
3138 (Defining_Entity (Lib_Unit),
3139 Is_Private_Descendant (Lib_Parent)
3140 or else Private_Present (Parent (Lib_Unit)));
3143 Set_Is_Private_Descendant
3144 (Defining_Entity (Lib_Unit),
3145 Private_Present (Parent (Lib_Unit)));
3149 if Check_Private then
3150 Check_Private_Child_Unit (N);
3152 end Install_Context_Clauses;
3154 -------------------------------------
3155 -- Install_Limited_Context_Clauses --
3156 -------------------------------------
3158 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3161 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3162 -- Check that the unlimited view of a given compilation_unit is not
3163 -- already visible through "use + renamings".
3165 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3166 -- Check that if a limited_with clause of a given compilation_unit
3167 -- mentions a descendant of a private child of some library unit,
3168 -- then the given compilation_unit shall be the declaration of a
3169 -- private descendant of that library unit.
3171 procedure Expand_Limited_With_Clause
3172 (Comp_Unit : Node_Id;
3175 -- If a child unit appears in a limited_with clause, there are implicit
3176 -- limited_with clauses on all parents that are not already visible
3177 -- through a regular with clause. This procedure creates the implicit
3178 -- limited with_clauses for the parents and loads the corresponding
3179 -- units. The shadow entities are created when the inserted clause is
3180 -- analyzed. Implements Ada 2005 (AI-50217).
3182 ---------------------
3183 -- Check_Renamings --
3184 ---------------------
3186 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3195 pragma Assert (Nkind (W) = N_With_Clause);
3197 -- Protect the frontend against previous critical errors
3199 case Nkind (Unit (Library_Unit (W))) is
3200 when N_Subprogram_Declaration |
3201 N_Package_Declaration |
3202 N_Generic_Subprogram_Declaration |
3203 N_Generic_Package_Declaration =>
3210 -- Check "use + renamings"
3212 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3213 Spec := Specification (Unit (P));
3215 Item := First (Visible_Declarations (Spec));
3216 while Present (Item) loop
3218 -- Look only at use package clauses
3220 if Nkind (Item) = N_Use_Package_Clause then
3222 -- Traverse the list of packages
3224 Nam := First (Names (Item));
3225 while Present (Nam) loop
3228 pragma Assert (Present (Parent (E)));
3230 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3231 and then Renamed_Entity (E) = WEnt
3233 -- The unlimited view is visible through use clause and
3234 -- renamings. There is not need to generate the error
3235 -- message here because Is_Visible_Through_Renamings
3236 -- takes care of generating the precise error message.
3240 elsif Nkind (Parent (E)) = N_Package_Specification then
3242 -- The use clause may refer to a local package.
3243 -- Check all the enclosing scopes.
3246 while E2 /= Standard_Standard
3254 ("unlimited view visible through use clause ", W);
3266 -- Recursive call to check all the ancestors
3268 if Is_Child_Spec (Unit (P)) then
3269 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3271 end Check_Renamings;
3273 ---------------------------------------
3274 -- Check_Private_Limited_Withed_Unit --
3275 ---------------------------------------
3277 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3278 Curr_Parent : Node_Id;
3279 Child_Parent : Node_Id;
3282 -- Compilation unit of the parent of the withed library unit
3284 Child_Parent := Parent_Spec (Unit (Library_Unit (Item)));
3286 -- If the child unit is a public child, then locate its nearest
3287 -- private ancestor, if any; Child_Parent will then be set to
3288 -- the parent of that ancestor.
3290 if not Private_Present (Library_Unit (Item)) then
3291 while Present (Child_Parent)
3292 and then not Private_Present (Child_Parent)
3294 Child_Parent := Parent_Spec (Unit (Child_Parent));
3297 if No (Child_Parent) then
3301 Child_Parent := Parent_Spec (Unit (Child_Parent));
3304 -- Traverse all the ancestors of the current compilation
3305 -- unit to check if it is a descendant of named library unit.
3307 Curr_Parent := Parent (Item);
3308 while Present (Parent_Spec (Unit (Curr_Parent)))
3309 and then Curr_Parent /= Child_Parent
3311 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3314 if Curr_Parent /= Child_Parent then
3316 ("unit in with clause is private child unit!", Item);
3318 ("\current unit must also have parent&!",
3319 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3321 elsif not Private_Present (Parent (Item))
3322 and then not Private_Present (Item)
3323 and then not Nkind_In (Unit (Parent (Item)), N_Package_Body,
3328 ("current unit must also be private descendant of&",
3329 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3331 end Check_Private_Limited_Withed_Unit;
3333 --------------------------------
3334 -- Expand_Limited_With_Clause --
3335 --------------------------------
3337 procedure Expand_Limited_With_Clause
3338 (Comp_Unit : Node_Id;
3342 Loc : constant Source_Ptr := Sloc (Nam);
3343 Unum : Unit_Number_Type;
3346 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3347 -- Returns true if the context already includes a with_clause for
3348 -- this unit. If the with_clause is non-limited, the unit is fully
3349 -- visible and an implicit limited_with should not be created. If
3350 -- there is already a limited_with clause for W, a second one is
3351 -- simply redundant.
3353 --------------------------
3354 -- Previous_Withed_Unit --
3355 --------------------------
3357 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3361 -- A limited with_clause cannot appear in the same context_clause
3362 -- as a nonlimited with_clause which mentions the same library.
3364 Item := First (Context_Items (Comp_Unit));
3365 while Present (Item) loop
3366 if Nkind (Item) = N_With_Clause
3367 and then Library_Unit (Item) = Library_Unit (W)
3376 end Previous_Withed_Unit;
3378 -- Start of processing for Expand_Limited_With_Clause
3381 New_Nodes_OK := New_Nodes_OK + 1;
3383 if Nkind (Nam) = N_Identifier then
3385 -- Create node for name of withed unit
3388 Make_With_Clause (Loc,
3389 Name => New_Copy (Nam));
3391 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3393 Make_With_Clause (Loc,
3394 Name => Make_Selected_Component (Loc,
3395 Prefix => New_Copy_Tree (Prefix (Nam)),
3396 Selector_Name => New_Copy (Selector_Name (Nam))));
3397 Set_Parent (Withn, Parent (N));
3400 Set_Limited_Present (Withn);
3401 Set_First_Name (Withn);
3402 Set_Implicit_With (Withn);
3406 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3411 -- Do not generate a limited_with_clause on the current unit.
3412 -- This path is taken when a unit has a limited_with clause on
3413 -- one of its child units.
3415 if Unum = Current_Sem_Unit then
3419 Set_Library_Unit (Withn, Cunit (Unum));
3420 Set_Corresponding_Spec
3421 (Withn, Specification (Unit (Cunit (Unum))));
3423 if not Previous_Withed_Unit (Withn) then
3424 Prepend (Withn, Context_Items (Parent (N)));
3425 Mark_Rewrite_Insertion (Withn);
3427 -- Add implicit limited_with_clauses for parents of child units
3428 -- mentioned in limited_with clauses.
3430 if Nkind (Nam) = N_Selected_Component then
3431 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3436 if not Limited_View_Installed (Withn) then
3437 Install_Limited_Withed_Unit (Withn);
3441 New_Nodes_OK := New_Nodes_OK - 1;
3442 end Expand_Limited_With_Clause;
3444 -- Start of processing for Install_Limited_Context_Clauses
3447 Item := First (Context_Items (N));
3448 while Present (Item) loop
3449 if Nkind (Item) = N_With_Clause
3450 and then Limited_Present (Item)
3452 if Nkind (Name (Item)) = N_Selected_Component then
3453 Expand_Limited_With_Clause
3454 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3457 Check_Private_Limited_Withed_Unit (Item);
3459 if not Implicit_With (Item)
3460 and then Is_Child_Spec (Unit (N))
3462 Check_Renamings (Parent_Spec (Unit (N)), Item);
3465 -- A unit may have a limited with on itself if it has a limited
3466 -- with_clause on one of its child units. In that case it is
3467 -- already being compiled and it makes no sense to install its
3470 -- If the item is a limited_private_with_clause, install it if the
3471 -- current unit is a body or if it is a private child. Otherwise
3472 -- the private clause is installed before analyzing the private
3473 -- part of the current unit.
3475 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3476 and then not Limited_View_Installed (Item)
3478 if not Private_Present (Item)
3479 or else Private_Present (N)
3480 or else Nkind_In (Unit (N), N_Package_Body,
3484 Install_Limited_Withed_Unit (Item);
3488 -- All items other than Limited_With clauses are ignored (they were
3489 -- installed separately early on by Install_Context_Clause).
3498 -- Ada 2005 (AI-412): Examine the visible declarations of a package
3499 -- spec, looking for incomplete subtype declarations of incomplete
3500 -- types visible through a limited with clause.
3502 if Ada_Version >= Ada_05
3503 and then Analyzed (N)
3504 and then Nkind (Unit (N)) = N_Package_Declaration
3509 Non_Lim_View : Entity_Id;
3512 Decl := First (Visible_Declarations (Specification (Unit (N))));
3513 while Present (Decl) loop
3514 if Nkind (Decl) = N_Subtype_Declaration
3516 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3518 From_With_Type (Defining_Identifier (Decl))
3520 Def_Id := Defining_Identifier (Decl);
3521 Non_Lim_View := Non_Limited_View (Def_Id);
3523 if not Is_Incomplete_Type (Non_Lim_View) then
3525 -- Convert an incomplete subtype declaration into a
3526 -- corresponding non-limited view subtype declaration.
3527 -- This is usually the case when analyzing a body that
3528 -- has regular with-clauses, when the spec has limited
3531 -- If the non-limited view is still incomplete, it is
3532 -- the dummy entry already created, and the declaration
3533 -- cannot be reanalyzed. This is the case when installing
3534 -- a parent unit that has limited with-clauses.
3536 Set_Subtype_Indication (Decl,
3537 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3538 Set_Etype (Def_Id, Non_Lim_View);
3539 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3540 Set_Analyzed (Decl, False);
3542 -- Reanalyze the declaration, suppressing the call to
3543 -- Enter_Name to avoid duplicate names.
3545 Analyze_Subtype_Declaration
3555 end Install_Limited_Context_Clauses;
3557 ---------------------
3558 -- Install_Parents --
3559 ---------------------
3561 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3568 P := Unit (Parent_Spec (Lib_Unit));
3569 P_Name := Get_Parent_Entity (P);
3571 if Etype (P_Name) = Any_Type then
3575 if Ekind (P_Name) = E_Generic_Package
3576 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3577 N_Generic_Package_Declaration)
3578 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3581 ("child of a generic package must be a generic unit", Lib_Unit);
3583 elsif not Is_Package_Or_Generic_Package (P_Name) then
3585 ("parent unit must be package or generic package", Lib_Unit);
3586 raise Unrecoverable_Error;
3588 elsif Present (Renamed_Object (P_Name)) then
3589 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3590 raise Unrecoverable_Error;
3592 -- Verify that a child of an instance is itself an instance, or the
3593 -- renaming of one. Given that an instance that is a unit is replaced
3594 -- with a package declaration, check against the original node. The
3595 -- parent may be currently being instantiated, in which case it appears
3596 -- as a declaration, but the generic_parent is already established
3597 -- indicating that we deal with an instance.
3599 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3600 if Nkind (Lib_Unit) in N_Renaming_Declaration
3601 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3603 (Nkind (Lib_Unit) = N_Package_Declaration
3604 and then Present (Generic_Parent (Specification (Lib_Unit))))
3609 ("child of an instance must be an instance or renaming",
3614 -- This is the recursive call that ensures all parents are loaded
3616 if Is_Child_Spec (P) then
3618 Is_Private or else Private_Present (Parent (Lib_Unit)));
3621 -- Now we can install the context for this parent
3623 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3624 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3625 Install_Siblings (P_Name, Parent (Lib_Unit));
3627 -- The child unit is in the declarative region of the parent. The parent
3628 -- must therefore appear in the scope stack and be visible, as when
3629 -- compiling the corresponding body. If the child unit is private or it
3630 -- is a package body, private declarations must be accessible as well.
3631 -- Use declarations in the parent must also be installed. Finally, other
3632 -- child units of the same parent that are in the context are
3633 -- immediately visible.
3635 -- Find entity for compilation unit, and set its private descendant
3636 -- status as needed.
3638 E_Name := Defining_Entity (Lib_Unit);
3640 Set_Is_Child_Unit (E_Name);
3642 Set_Is_Private_Descendant (E_Name,
3643 Is_Private_Descendant (P_Name)
3644 or else Private_Present (Parent (Lib_Unit)));
3646 P_Spec := Specification (Unit_Declaration_Node (P_Name));
3647 Push_Scope (P_Name);
3649 -- Save current visibility of unit
3651 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3652 Is_Immediately_Visible (P_Name);
3653 Set_Is_Immediately_Visible (P_Name);
3654 Install_Visible_Declarations (P_Name);
3655 Set_Use (Visible_Declarations (P_Spec));
3657 -- If the parent is a generic unit, its formal part may contain formal
3658 -- packages and use clauses for them.
3660 if Ekind (P_Name) = E_Generic_Package then
3661 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3665 or else Private_Present (Parent (Lib_Unit))
3667 Install_Private_Declarations (P_Name);
3668 Install_Private_With_Clauses (P_Name);
3669 Set_Use (Private_Declarations (P_Spec));
3671 end Install_Parents;
3673 ----------------------------------
3674 -- Install_Private_With_Clauses --
3675 ----------------------------------
3677 procedure Install_Private_With_Clauses (P : Entity_Id) is
3678 Decl : constant Node_Id := Unit_Declaration_Node (P);
3682 if Debug_Flag_I then
3683 Write_Str ("install private with clauses of ");
3684 Write_Name (Chars (P));
3688 if Nkind (Parent (Decl)) = N_Compilation_Unit then
3689 Item := First (Context_Items (Parent (Decl)));
3690 while Present (Item) loop
3691 if Nkind (Item) = N_With_Clause
3692 and then Private_Present (Item)
3694 if Limited_Present (Item) then
3695 if not Limited_View_Installed (Item) then
3696 Install_Limited_Withed_Unit (Item);
3699 Install_Withed_Unit (Item, Private_With_OK => True);
3706 end Install_Private_With_Clauses;
3708 ----------------------
3709 -- Install_Siblings --
3710 ----------------------
3712 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3718 -- Iterate over explicit with clauses, and check whether the scope of
3719 -- each entity is an ancestor of the current unit, in which case it is
3720 -- immediately visible.
3722 Item := First (Context_Items (N));
3723 while Present (Item) loop
3725 -- Do not install private_with_clauses if the unit is a package
3726 -- declaration, unless it is itself a private child unit.
3728 if Nkind (Item) = N_With_Clause
3729 and then not Implicit_With (Item)
3730 and then not Limited_Present (Item)
3732 (not Private_Present (Item)
3733 or else Nkind (Unit (N)) /= N_Package_Declaration
3734 or else Private_Present (N))
3736 Id := Entity (Name (Item));
3738 if Is_Child_Unit (Id)
3739 and then Is_Ancestor_Package (Scope (Id), U_Name)
3741 Set_Is_Immediately_Visible (Id);
3743 -- Check for the presence of another unit in the context,
3744 -- that may be inadvertently hidden by the child.
3746 Prev := Current_Entity (Id);
3749 and then Is_Immediately_Visible (Prev)
3750 and then not Is_Child_Unit (Prev)
3756 Clause := First (Context_Items (N));
3757 while Present (Clause) loop
3758 if Nkind (Clause) = N_With_Clause
3759 and then Entity (Name (Clause)) = Prev
3762 ("child unit& hides compilation unit " &
3763 "with the same name?",
3773 -- The With_Clause may be on a grand-child or one of its further
3774 -- descendants, which makes a child immediately visible. Examine
3775 -- ancestry to determine whether such a child exists. For example,
3776 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
3777 -- is immediately visible.
3779 elsif Is_Child_Unit (Id) then
3785 while Is_Child_Unit (Par) loop
3786 if Is_Ancestor_Package (Scope (Par), U_Name) then
3787 Set_Is_Immediately_Visible (Par);
3799 end Install_Siblings;
3801 -------------------------------
3802 -- Install_Limited_With_Unit --
3803 -------------------------------
3805 procedure Install_Limited_Withed_Unit (N : Node_Id) is
3806 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
3809 Is_Child_Package : Boolean := False;
3810 Lim_Header : Entity_Id;
3811 Lim_Typ : Entity_Id;
3813 function Has_Limited_With_Clause
3814 (C_Unit : Entity_Id;
3815 Pack : Entity_Id) return Boolean;
3816 -- Determine whether any package in the ancestor chain starting with
3817 -- C_Unit has a limited with clause for package Pack.
3819 function Has_With_Clause
3822 Is_Limited : Boolean := False) return Boolean;
3823 -- Determine whether compilation unit C_Unit contains a with clause
3824 -- for package Pack. Use flag Is_Limited to designate desired clause
3825 -- kind. This is a subsidiary routine to Has_Limited_With_Clause.
3827 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
3828 -- Check if some package installed though normal with-clauses has a
3829 -- renaming declaration of package P. AARM 10.1.2(21/2).
3831 -----------------------------
3832 -- Has_Limited_With_Clause --
3833 -----------------------------
3835 function Has_Limited_With_Clause
3836 (C_Unit : Entity_Id;
3837 Pack : Entity_Id) return Boolean
3844 while Present (Par) loop
3845 if Ekind (Par) /= E_Package then
3849 -- Retrieve the Compilation_Unit node for Par and determine if
3850 -- its context clauses contain a limited with for Pack.
3852 Par_Unit := Parent (Parent (Parent (Par)));
3854 if Nkind (Par_Unit) = N_Package_Declaration then
3855 Par_Unit := Parent (Par_Unit);
3858 if Has_With_Clause (Par_Unit, Pack, True) then
3862 -- If there are more ancestors, climb up the tree, otherwise
3865 if Is_Child_Unit (Par) then
3873 end Has_Limited_With_Clause;
3875 ---------------------
3876 -- Has_With_Clause --
3877 ---------------------
3879 function Has_With_Clause
3882 Is_Limited : Boolean := False) return Boolean
3888 if Present (Context_Items (C_Unit)) then
3889 Item := First (Context_Items (C_Unit));
3890 while Present (Item) loop
3891 if Nkind (Item) = N_With_Clause then
3893 -- Retrieve the entity of the imported compilation unit
3895 if Nkind (Name (Item)) = N_Selected_Component then
3896 Nam := Entity (Selector_Name (Name (Item)));
3898 Nam := Entity (Name (Item));
3903 ((Is_Limited and then Limited_Present (Item))
3905 (not Is_Limited and then not Limited_Present (Item)))
3916 end Has_With_Clause;
3918 ----------------------------------
3919 -- Is_Visible_Through_Renamings --
3920 ----------------------------------
3922 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
3923 Kind : constant Node_Kind :=
3924 Nkind (Unit (Cunit (Current_Sem_Unit)));
3930 -- Example of the error detected by this subprogram:
3938 -- package Ren_P renames P;
3944 -- limited with P; -- ERROR
3945 -- package R.C is ...
3947 Aux_Unit := Cunit (Current_Sem_Unit);
3950 Item := First (Context_Items (Aux_Unit));
3951 while Present (Item) loop
3952 if Nkind (Item) = N_With_Clause
3953 and then not Limited_Present (Item)
3954 and then Nkind (Unit (Library_Unit (Item))) =
3955 N_Package_Declaration
3958 First (Visible_Declarations
3959 (Specification (Unit (Library_Unit (Item)))));
3960 while Present (Decl) loop
3961 if Nkind (Decl) = N_Package_Renaming_Declaration
3962 and then Entity (Name (Decl)) = P
3964 -- Generate the error message only if the current unit
3965 -- is a package declaration; in case of subprogram
3966 -- bodies and package bodies we just return true to
3967 -- indicate that the limited view must not be
3970 if Kind = N_Package_Declaration then
3972 ("simultaneous visibility of the limited and " &
3973 "unlimited views not allowed", N);
3974 Error_Msg_Sloc := Sloc (Item);
3976 ("\\ unlimited view of & visible through the " &
3977 "context clause #", N, P);
3978 Error_Msg_Sloc := Sloc (Decl);
3979 Error_Msg_NE ("\\ and the renaming #", N, P);
3992 if Present (Library_Unit (Aux_Unit)) then
3993 if Aux_Unit = Library_Unit (Aux_Unit) then
3995 -- Aux_Unit is a body that acts as a spec. Clause has
3996 -- already been flagged as illegal.
4001 Aux_Unit := Library_Unit (Aux_Unit);
4004 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4007 exit when No (Aux_Unit);
4011 end Is_Visible_Through_Renamings;
4013 -- Start of processing for Install_Limited_Withed_Unit
4016 pragma Assert (not Limited_View_Installed (N));
4018 -- In case of limited with_clause on subprograms, generics, instances,
4019 -- or renamings, the corresponding error was previously posted and we
4020 -- have nothing to do here.
4022 if Nkind (P_Unit) /= N_Package_Declaration then
4026 P := Defining_Unit_Name (Specification (P_Unit));
4028 -- Handle child packages
4030 if Nkind (P) = N_Defining_Program_Unit_Name then
4031 Is_Child_Package := True;
4032 P := Defining_Identifier (P);
4035 -- Do not install the limited-view if the full-view is already visible
4036 -- through renaming declarations.
4038 if Is_Visible_Through_Renamings (P) then
4042 -- Do not install the limited view if this is the unit being analyzed.
4043 -- This unusual case will happen when a unit has a limited_with clause
4044 -- on one of its children. The compilation of the child forces the
4045 -- load of the parent which tries to install the limited view of the
4046 -- child again. Installing the limited view must also be disabled
4047 -- when compiling the body of the child unit.
4049 if P = Cunit_Entity (Current_Sem_Unit)
4051 (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4052 and then P = Main_Unit_Entity)
4057 -- This scenario is similar to the one above, the difference is that
4058 -- the compilation of sibling Par.Sib forces the load of parent Par
4059 -- which tries to install the limited view of Lim_Pack [1]. However
4060 -- Par.Sib has a with clause for Lim_Pack [2] in its body, and thus
4061 -- needs the non-limited views of all entities from Lim_Pack.
4063 -- limited with Lim_Pack; -- [1]
4064 -- package Par is ... package Lim_Pack is ...
4066 -- with Lim_Pack; -- [2]
4067 -- package Par.Sib is ... package body Par.Sib is ...
4069 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4070 -- Sem_Unit is the body of Par.Sib.
4072 if Ekind (P) = E_Package
4073 and then Ekind (Main_Unit_Entity) = E_Package
4074 and then Is_Child_Unit (Main_Unit_Entity)
4076 -- The body has a regular with clause
4078 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4079 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4081 -- One of the ancestors has a limited with clause
4083 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4084 N_Package_Specification
4085 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4090 -- A common use of the limited-with is to have a limited-with
4091 -- in the package spec, and a normal with in its package body.
4094 -- limited with X; -- [1]
4098 -- package body A is ...
4100 -- The compilation of A's body installs the context clauses found at [2]
4101 -- and then the context clauses of its specification (found at [1]). As
4102 -- a consequence, at [1] the specification of X has been analyzed and it
4103 -- is immediately visible. According to the semantics of limited-with
4104 -- context clauses we don't install the limited view because the full
4105 -- view of X supersedes its limited view.
4107 if Analyzed (P_Unit)
4108 and then (Is_Immediately_Visible (P)
4109 or else (Is_Child_Package
4110 and then Is_Visible_Child_Unit (P)))
4112 -- Ada 2005 (AI-262): Install the private declarations of P
4114 if Private_Present (N)
4115 and then not In_Private_Part (P)
4121 Id := First_Private_Entity (P);
4122 while Present (Id) loop
4123 if not Is_Internal (Id)
4124 and then not Is_Child_Unit (Id)
4126 if not In_Chain (Id) then
4127 Set_Homonym (Id, Current_Entity (Id));
4128 Set_Current_Entity (Id);
4131 Set_Is_Immediately_Visible (Id);
4137 Set_In_Private_Part (P);
4144 if Debug_Flag_I then
4145 Write_Str ("install limited view of ");
4146 Write_Name (Chars (P));
4150 -- If the unit has not been analyzed and the limited view has not been
4151 -- already installed then we install it.
4153 if not Analyzed (P_Unit) then
4154 if not In_Chain (P) then
4156 -- Minimum decoration
4158 Set_Ekind (P, E_Package);
4159 Set_Etype (P, Standard_Void_Type);
4160 Set_Scope (P, Standard_Standard);
4162 if Is_Child_Package then
4163 Set_Is_Child_Unit (P);
4164 Set_Is_Visible_Child_Unit (P);
4165 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4168 -- Place entity on visibility structure
4170 Set_Homonym (P, Current_Entity (P));
4171 Set_Current_Entity (P);
4173 if Debug_Flag_I then
4174 Write_Str (" (homonym) chain ");
4175 Write_Name (Chars (P));
4179 -- Install the incomplete view. The first element of the limited
4180 -- view is a header (an E_Package entity) used to reference the
4181 -- first shadow entity in the private part of the package.
4183 Lim_Header := Limited_View (P);
4184 Lim_Typ := First_Entity (Lim_Header);
4186 while Present (Lim_Typ)
4187 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4189 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4190 Set_Current_Entity (Lim_Typ);
4192 if Debug_Flag_I then
4193 Write_Str (" (homonym) chain ");
4194 Write_Name (Chars (Lim_Typ));
4198 Next_Entity (Lim_Typ);
4202 -- If the unit appears in a previous regular with_clause, the regular
4203 -- entities of the public part of the withed package must be replaced
4204 -- by the shadow ones.
4206 -- This code must be kept synchronized with the code that replaces the
4207 -- shadow entities by the real entities (see body of Remove_Limited
4208 -- With_Clause); otherwise the contents of the homonym chains are not
4212 -- Hide all the type entities of the public part of the package to
4213 -- avoid its usage. This is needed to cover all the subtype decla-
4214 -- rations because we do not remove them from the homonym chain.
4216 E := First_Entity (P);
4217 while Present (E) and then E /= First_Private_Entity (P) loop
4219 Set_Was_Hidden (E, Is_Hidden (E));
4226 -- Replace the real entities by the shadow entities of the limited
4227 -- view. The first element of the limited view is a header that is
4228 -- used to reference the first shadow entity in the private part
4229 -- of the package. Successive elements are the limited views of the
4230 -- type (including regular incomplete types) declared in the package.
4232 Lim_Header := Limited_View (P);
4234 Lim_Typ := First_Entity (Lim_Header);
4235 while Present (Lim_Typ)
4236 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4238 pragma Assert (not In_Chain (Lim_Typ));
4240 -- Do not unchain nested packages and child units
4242 if Ekind (Lim_Typ) /= E_Package
4243 and then not Is_Child_Unit (Lim_Typ)
4249 Prev := Current_Entity (Lim_Typ);
4252 -- Replace E in the homonyms list, so that the limited
4253 -- view becomes available.
4255 if E = Non_Limited_View (Lim_Typ) then
4256 Set_Homonym (Lim_Typ, Homonym (Prev));
4257 Set_Current_Entity (Lim_Typ);
4261 E := Homonym (Prev);
4263 -- E may have been removed when installing a
4264 -- previous limited_with_clause.
4268 exit when E = Non_Limited_View (Lim_Typ);
4270 Prev := Homonym (Prev);
4274 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4275 Set_Homonym (Prev, Lim_Typ);
4280 if Debug_Flag_I then
4281 Write_Str (" (homonym) chain ");
4282 Write_Name (Chars (Lim_Typ));
4287 Next_Entity (Lim_Typ);
4291 -- The package must be visible while the limited-with clause is active
4292 -- because references to the type P.T must resolve in the usual way.
4293 -- In addition, we remember that the limited-view has been installed to
4294 -- uninstall it at the point of context removal.
4296 Set_Is_Immediately_Visible (P);
4297 Set_Limited_View_Installed (N);
4299 -- If the package in the limited_with clause is a child unit, the
4300 -- clause is unanalyzed and appears as a selected component. Recast
4301 -- it as an expanded name so that the entity can be properly set. Use
4302 -- entity of parent, if available, for higher ancestors in the name.
4304 if Nkind (Name (N)) = N_Selected_Component then
4312 while Nkind (Nam) = N_Selected_Component
4313 and then Present (Ent)
4315 Change_Selected_Component_To_Expanded_Name (Nam);
4317 -- Set entity of parent identifiers if the unit is a child
4318 -- unit. This ensures that the tree is properly formed from
4319 -- semantic point of view (e.g. for ASIS queries).
4321 Set_Entity (Nam, Ent);
4323 Nam := Prefix (Nam);
4326 -- Set entity of last ancestor
4328 if Nkind (Nam) = N_Identifier then
4329 Set_Entity (Nam, Ent);
4335 Set_Entity (Name (N), P);
4336 Set_From_With_Type (P);
4337 end Install_Limited_Withed_Unit;
4339 -------------------------
4340 -- Install_Withed_Unit --
4341 -------------------------
4343 procedure Install_Withed_Unit
4344 (With_Clause : Node_Id;
4345 Private_With_OK : Boolean := False)
4347 Uname : constant Entity_Id := Entity (Name (With_Clause));
4348 P : constant Entity_Id := Scope (Uname);
4351 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
4352 -- compiling a package declaration and the Private_With_OK flag was not
4353 -- set by the caller. These declarations will be installed later (before
4354 -- analyzing the private part of the package).
4356 if Private_Present (With_Clause)
4357 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
4358 and then not (Private_With_OK)
4363 if Debug_Flag_I then
4364 if Private_Present (With_Clause) then
4365 Write_Str ("install private withed unit ");
4367 Write_Str ("install withed unit ");
4370 Write_Name (Chars (Uname));
4374 -- We do not apply the restrictions to an internal unit unless
4375 -- we are compiling the internal unit as a main unit. This check
4376 -- is also skipped for dummy units (for missing packages).
4378 if Sloc (Uname) /= No_Location
4379 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
4380 or else Current_Sem_Unit = Main_Unit)
4382 Check_Restricted_Unit
4383 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
4386 if P /= Standard_Standard then
4388 -- If the unit is not analyzed after analysis of the with clause and
4389 -- it is an instantiation then it awaits a body and is the main unit.
4390 -- Its appearance in the context of some other unit indicates a
4391 -- circular dependency (DEC suite perversity).
4393 if not Analyzed (Uname)
4394 and then Nkind (Parent (Uname)) = N_Package_Instantiation
4397 ("instantiation depends on itself", Name (With_Clause));
4399 elsif not Is_Visible_Child_Unit (Uname) then
4400 Set_Is_Visible_Child_Unit (Uname);
4402 -- If the child unit appears in the context of its parent, it is
4403 -- immediately visible.
4405 if In_Open_Scopes (Scope (Uname)) then
4406 Set_Is_Immediately_Visible (Uname);
4409 if Is_Generic_Instance (Uname)
4410 and then Ekind (Uname) in Subprogram_Kind
4412 -- Set flag as well on the visible entity that denotes the
4413 -- instance, which renames the current one.
4415 Set_Is_Visible_Child_Unit
4417 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
4420 -- The parent unit may have been installed already, and may have
4421 -- appeared in a use clause.
4423 if In_Use (Scope (Uname)) then
4424 Set_Is_Potentially_Use_Visible (Uname);
4427 Set_Context_Installed (With_Clause);
4430 elsif not Is_Immediately_Visible (Uname) then
4431 if not Private_Present (With_Clause)
4432 or else Private_With_OK
4434 Set_Is_Immediately_Visible (Uname);
4437 Set_Context_Installed (With_Clause);
4440 -- A with-clause overrides a with-type clause: there are no restric-
4441 -- tions on the use of package entities.
4443 if Ekind (Uname) = E_Package then
4444 Set_From_With_Type (Uname, False);
4447 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
4448 -- unit if there is a visible homograph for it declared in the same
4449 -- declarative region. This pathological case can only arise when an
4450 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
4451 -- G1 has a generic child also named G2, and the context includes with_
4452 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
4453 -- of I1.G2 visible as well. If the child unit is named Standard, do
4454 -- not apply the check to the Standard package itself.
4456 if Is_Child_Unit (Uname)
4457 and then Is_Visible_Child_Unit (Uname)
4458 and then Ada_Version >= Ada_05
4461 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
4467 U2 := Homonym (Uname);
4469 and then U2 /= Standard_Standard
4472 Decl2 := Unit_Declaration_Node (P2);
4474 if Is_Child_Unit (U2)
4475 and then Is_Visible_Child_Unit (U2)
4477 if Is_Generic_Instance (P)
4478 and then Nkind (Decl1) = N_Package_Declaration
4479 and then Generic_Parent (Specification (Decl1)) = P2
4481 Error_Msg_N ("illegal with_clause", With_Clause);
4483 ("\child unit has visible homograph" &
4484 " (RM 8.3(26), 10.1.1(19))",
4488 elsif Is_Generic_Instance (P2)
4489 and then Nkind (Decl2) = N_Package_Declaration
4490 and then Generic_Parent (Specification (Decl2)) = P
4492 -- With_clause for child unit of instance appears before
4493 -- in the context. We want to place the error message on
4494 -- it, not on the generic child unit itself.
4497 Prev_Clause : Node_Id;
4500 Prev_Clause := First (List_Containing (With_Clause));
4501 while Entity (Name (Prev_Clause)) /= U2 loop
4505 pragma Assert (Present (Prev_Clause));
4506 Error_Msg_N ("illegal with_clause", Prev_Clause);
4508 ("\child unit has visible homograph" &
4509 " (RM 8.3(26), 10.1.1(19))",
4520 end Install_Withed_Unit;
4526 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
4527 K : constant Node_Kind := Nkind (Lib_Unit);
4530 return (K in N_Generic_Declaration or else
4531 K in N_Generic_Instantiation or else
4532 K in N_Generic_Renaming_Declaration or else
4533 K = N_Package_Declaration or else
4534 K = N_Package_Renaming_Declaration or else
4535 K = N_Subprogram_Declaration or else
4536 K = N_Subprogram_Renaming_Declaration)
4537 and then Present (Parent_Spec (Lib_Unit));
4540 -----------------------
4541 -- Load_Needed_Body --
4542 -----------------------
4544 -- N is a generic unit named in a with clause, or else it is a unit that
4545 -- contains a generic unit or an inlined function. In order to perform an
4546 -- instantiation, the body of the unit must be present. If the unit itself
4547 -- is generic, we assume that an instantiation follows, and load & analyze
4548 -- the body unconditionally. This forces analysis of the spec as well.
4550 -- If the unit is not generic, but contains a generic unit, it is loaded on
4551 -- demand, at the point of instantiation (see ch12).
4553 procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
4554 Body_Name : Unit_Name_Type;
4555 Unum : Unit_Number_Type;
4557 Save_Style_Check : constant Boolean := Opt.Style_Check;
4558 -- The loading and analysis is done with style checks off
4561 if not GNAT_Mode then
4562 Style_Check := False;
4565 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
4568 (Load_Name => Body_Name,
4574 if Unum = No_Unit then
4578 Compiler_State := Analyzing; -- reset after load
4580 if not Fatal_Error (Unum) or else Try_Semantics then
4581 if Debug_Flag_L then
4582 Write_Str ("*** Loaded generic body");
4586 Semantics (Cunit (Unum));
4592 Style_Check := Save_Style_Check;
4593 end Load_Needed_Body;
4595 -------------------------
4596 -- Build_Limited_Views --
4597 -------------------------
4599 procedure Build_Limited_Views (N : Node_Id) is
4600 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
4601 P : constant Entity_Id := Cunit_Entity (Unum);
4603 Spec : Node_Id; -- To denote a package specification
4604 Lim_Typ : Entity_Id; -- To denote shadow entities
4605 Comp_Typ : Entity_Id; -- To denote real entities
4607 Lim_Header : Entity_Id; -- Package entity
4608 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
4609 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
4611 procedure Decorate_Incomplete_Type
4614 -- Add attributes of an incomplete type to a shadow entity. The same
4615 -- attributes are placed on the real entity, so that gigi receives
4616 -- a consistent view.
4618 procedure Decorate_Package_Specification (P : Entity_Id);
4619 -- Add attributes of a package entity to the entity in a package
4622 procedure Decorate_Tagged_Type
4626 -- Set basic attributes of tagged type T, including its class_wide type.
4627 -- The parameters Loc, Scope are used to decorate the class_wide type.
4629 procedure Build_Chain
4631 First_Decl : Node_Id);
4632 -- Construct list of shadow entities and attach it to entity of
4633 -- package that is mentioned in a limited_with clause.
4635 function New_Internal_Shadow_Entity
4636 (Kind : Entity_Kind;
4637 Sloc_Value : Source_Ptr;
4638 Id_Char : Character) return Entity_Id;
4639 -- Build a new internal entity and append it to the list of shadow
4640 -- entities available through the limited-header
4642 ------------------------------
4643 -- Decorate_Incomplete_Type --
4644 ------------------------------
4646 procedure Decorate_Incomplete_Type
4651 Set_Ekind (E, E_Incomplete_Type);
4652 Set_Scope (E, Scop);
4654 Set_Is_First_Subtype (E, True);
4655 Set_Stored_Constraint (E, No_Elist);
4656 Set_Full_View (E, Empty);
4657 Init_Size_Align (E);
4658 end Decorate_Incomplete_Type;
4660 --------------------------
4661 -- Decorate_Tagged_Type --
4662 --------------------------
4664 procedure Decorate_Tagged_Type
4672 Decorate_Incomplete_Type (T, Scop);
4673 Set_Is_Tagged_Type (T);
4675 -- Build corresponding class_wide type, if not previously done
4677 -- Warning: The class-wide entity is shared by the limited-view
4678 -- and the full-view.
4680 if No (Class_Wide_Type (T)) then
4681 CW := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
4683 Set_Ekind (CW, E_Class_Wide_Type);
4685 Set_Scope (CW, Scop);
4686 Set_Is_Tagged_Type (CW);
4687 Set_Is_First_Subtype (CW, True);
4688 Init_Size_Align (CW);
4689 Set_Has_Unknown_Discriminants (CW, True);
4690 Set_Class_Wide_Type (CW, CW);
4691 Set_Equivalent_Type (CW, Empty);
4692 Set_From_With_Type (CW, From_With_Type (T));
4694 Set_Class_Wide_Type (T, CW);
4696 end Decorate_Tagged_Type;
4698 ------------------------------------
4699 -- Decorate_Package_Specification --
4700 ------------------------------------
4702 procedure Decorate_Package_Specification (P : Entity_Id) is
4704 -- Place only the most basic attributes
4706 Set_Ekind (P, E_Package);
4707 Set_Etype (P, Standard_Void_Type);
4708 end Decorate_Package_Specification;
4710 --------------------------------
4711 -- New_Internal_Shadow_Entity --
4712 --------------------------------
4714 function New_Internal_Shadow_Entity
4715 (Kind : Entity_Kind;
4716 Sloc_Value : Source_Ptr;
4717 Id_Char : Character) return Entity_Id
4719 E : constant Entity_Id :=
4720 Make_Defining_Identifier (Sloc_Value,
4721 Chars => New_Internal_Name (Id_Char));
4724 Set_Ekind (E, Kind);
4725 Set_Is_Internal (E, True);
4727 if Kind in Type_Kind then
4728 Init_Size_Align (E);
4731 Append_Entity (E, Lim_Header);
4734 end New_Internal_Shadow_Entity;
4740 procedure Build_Chain
4742 First_Decl : Node_Id)
4744 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
4745 Is_Tagged : Boolean;
4750 while Present (Decl) loop
4752 -- For each library_package_declaration in the environment, there
4753 -- is an implicit declaration of a *limited view* of that library
4754 -- package. The limited view of a package contains:
4756 -- * For each nested package_declaration, a declaration of the
4757 -- limited view of that package, with the same defining-
4758 -- program-unit name.
4760 -- * For each type_declaration in the visible part, an incomplete
4761 -- type-declaration with the same defining_identifier, whose
4762 -- completion is the type_declaration. If the type_declaration
4763 -- is tagged, then the incomplete_type_declaration is tagged
4766 -- The partial view is tagged if the declaration has the
4767 -- explicit keyword, or else if it is a type extension, both
4768 -- of which can be ascertained syntactically.
4770 if Nkind (Decl) = N_Full_Type_Declaration then
4772 (Nkind (Type_Definition (Decl)) = N_Record_Definition
4773 and then Tagged_Present (Type_Definition (Decl)))
4775 (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
4778 (Record_Extension_Part (Type_Definition (Decl))));
4780 Comp_Typ := Defining_Identifier (Decl);
4782 if not Analyzed_Unit then
4784 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
4786 Decorate_Incomplete_Type (Comp_Typ, Scope);
4790 -- Create shadow entity for type
4792 Lim_Typ := New_Internal_Shadow_Entity
4793 (Kind => Ekind (Comp_Typ),
4794 Sloc_Value => Sloc (Comp_Typ),
4797 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4798 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4799 Set_From_With_Type (Lim_Typ);
4802 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
4804 Decorate_Incomplete_Type (Lim_Typ, Scope);
4807 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
4809 elsif Nkind_In (Decl, N_Private_Type_Declaration,
4810 N_Incomplete_Type_Declaration)
4812 Comp_Typ := Defining_Identifier (Decl);
4814 if not Analyzed_Unit then
4815 if Tagged_Present (Decl) then
4816 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
4818 Decorate_Incomplete_Type (Comp_Typ, Scope);
4822 Lim_Typ := New_Internal_Shadow_Entity
4823 (Kind => Ekind (Comp_Typ),
4824 Sloc_Value => Sloc (Comp_Typ),
4827 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4828 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4829 Set_From_With_Type (Lim_Typ);
4831 if Tagged_Present (Decl) then
4832 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
4834 Decorate_Incomplete_Type (Lim_Typ, Scope);
4837 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
4839 elsif Nkind (Decl) = N_Private_Extension_Declaration then
4840 Comp_Typ := Defining_Identifier (Decl);
4842 if not Analyzed_Unit then
4843 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
4846 -- Create shadow entity for type
4848 Lim_Typ := New_Internal_Shadow_Entity
4849 (Kind => Ekind (Comp_Typ),
4850 Sloc_Value => Sloc (Comp_Typ),
4853 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4854 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4855 Set_From_With_Type (Lim_Typ);
4857 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
4858 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
4860 elsif Nkind (Decl) = N_Package_Declaration then
4865 Spec : constant Node_Id := Specification (Decl);
4868 Comp_Typ := Defining_Unit_Name (Spec);
4870 if not Analyzed (Cunit (Unum)) then
4871 Decorate_Package_Specification (Comp_Typ);
4872 Set_Scope (Comp_Typ, Scope);
4875 Lim_Typ := New_Internal_Shadow_Entity
4876 (Kind => Ekind (Comp_Typ),
4877 Sloc_Value => Sloc (Comp_Typ),
4880 Decorate_Package_Specification (Lim_Typ);
4881 Set_Scope (Lim_Typ, Scope);
4883 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4884 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4885 Set_From_With_Type (Lim_Typ);
4887 -- Note: The non_limited_view attribute is not used
4888 -- for local packages.
4892 First_Decl => First (Visible_Declarations (Spec)));
4900 -- Start of processing for Build_Limited_Views
4903 pragma Assert (Limited_Present (N));
4905 -- A library_item mentioned in a limited_with_clause shall
4906 -- be a package_declaration, not a subprogram_declaration,
4907 -- generic_declaration, generic_instantiation, or
4908 -- package_renaming_declaration
4910 case Nkind (Unit (Library_Unit (N))) is
4912 when N_Package_Declaration =>
4915 when N_Subprogram_Declaration =>
4916 Error_Msg_N ("subprograms not allowed in "
4917 & "limited with_clauses", N);
4920 when N_Generic_Package_Declaration |
4921 N_Generic_Subprogram_Declaration =>
4922 Error_Msg_N ("generics not allowed in "
4923 & "limited with_clauses", N);
4926 when N_Generic_Instantiation =>
4927 Error_Msg_N ("generic instantiations not allowed in "
4928 & "limited with_clauses", N);
4931 when N_Generic_Renaming_Declaration =>
4932 Error_Msg_N ("generic renamings not allowed in "
4933 & "limited with_clauses", N);
4936 when N_Subprogram_Renaming_Declaration =>
4937 Error_Msg_N ("renamed subprograms not allowed in "
4938 & "limited with_clauses", N);
4941 when N_Package_Renaming_Declaration =>
4942 Error_Msg_N ("renamed packages not allowed in "
4943 & "limited with_clauses", N);
4947 raise Program_Error;
4950 -- Check if the chain is already built
4952 Spec := Specification (Unit (Library_Unit (N)));
4954 if Limited_View_Installed (Spec) then
4958 Set_Ekind (P, E_Package);
4960 -- Build the header of the limited_view
4963 Make_Defining_Identifier (Sloc (N),
4964 Chars => New_Internal_Name (Id_Char => 'Z'));
4965 Set_Ekind (Lim_Header, E_Package);
4966 Set_Is_Internal (Lim_Header);
4967 Set_Limited_View (P, Lim_Header);
4969 -- Create the auxiliary chain. All the shadow entities are appended to
4970 -- the list of entities of the limited-view header
4974 First_Decl => First (Visible_Declarations (Spec)));
4976 -- Save the last built shadow entity. It is needed later to set the
4977 -- reference to the first shadow entity in the private part
4979 Last_Pub_Lim_E := Last_Lim_E;
4981 -- Ada 2005 (AI-262): Add the limited view of the private declarations
4982 -- Required to give support to limited-private-with clauses
4984 Build_Chain (Scope => P,
4985 First_Decl => First (Private_Declarations (Spec)));
4987 if Last_Pub_Lim_E /= Empty then
4988 Set_First_Private_Entity (Lim_Header,
4989 Next_Entity (Last_Pub_Lim_E));
4991 Set_First_Private_Entity (Lim_Header,
4995 Set_Limited_View_Installed (Spec);
4996 end Build_Limited_Views;
4998 -------------------------------
4999 -- Check_Body_Needed_For_SAL --
5000 -------------------------------
5002 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5004 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5005 -- Determine whether use of entity E might require the presence of its
5006 -- body. For a package this requires a recursive traversal of all nested
5009 ---------------------------
5010 -- Entity_Needed_For_SAL --
5011 ---------------------------
5013 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5017 if Is_Subprogram (E)
5018 and then Has_Pragma_Inline (E)
5022 elsif Ekind (E) = E_Generic_Function
5023 or else Ekind (E) = E_Generic_Procedure
5027 elsif Ekind (E) = E_Generic_Package
5029 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5030 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5034 elsif Ekind (E) = E_Package
5036 Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5037 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5039 Ent := First_Entity (E);
5040 while Present (Ent) loop
5041 if Entity_Needs_Body (Ent) then
5053 end Entity_Needs_Body;
5055 -- Start of processing for Check_Body_Needed_For_SAL
5058 if Ekind (Unit_Name) = E_Generic_Package
5060 Nkind (Unit_Declaration_Node (Unit_Name)) =
5061 N_Generic_Package_Declaration
5063 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5065 Set_Body_Needed_For_SAL (Unit_Name);
5067 elsif Ekind (Unit_Name) = E_Generic_Procedure
5068 or else Ekind (Unit_Name) = E_Generic_Function
5070 Set_Body_Needed_For_SAL (Unit_Name);
5072 elsif Is_Subprogram (Unit_Name)
5073 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5074 N_Subprogram_Declaration
5075 and then Has_Pragma_Inline (Unit_Name)
5077 Set_Body_Needed_For_SAL (Unit_Name);
5079 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5080 Check_Body_Needed_For_SAL
5081 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5083 elsif Ekind (Unit_Name) = E_Package
5084 and then Entity_Needs_Body (Unit_Name)
5086 Set_Body_Needed_For_SAL (Unit_Name);
5088 elsif Ekind (Unit_Name) = E_Package_Body
5089 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5091 Check_Body_Needed_For_SAL
5092 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5094 end Check_Body_Needed_For_SAL;
5096 --------------------
5097 -- Remove_Context --
5098 --------------------
5100 procedure Remove_Context (N : Node_Id) is
5101 Lib_Unit : constant Node_Id := Unit (N);
5104 -- If this is a child unit, first remove the parent units
5106 if Is_Child_Spec (Lib_Unit) then
5107 Remove_Parents (Lib_Unit);
5110 Remove_Context_Clauses (N);
5113 ----------------------------
5114 -- Remove_Context_Clauses --
5115 ----------------------------
5117 procedure Remove_Context_Clauses (N : Node_Id) is
5119 Unit_Name : Entity_Id;
5122 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5123 -- limited-views first and regular-views later (to maintain the
5126 -- First Phase: Remove limited_with context clauses
5128 Item := First (Context_Items (N));
5129 while Present (Item) loop
5131 -- We are interested only in with clauses which got installed
5134 if Nkind (Item) = N_With_Clause
5135 and then Limited_Present (Item)
5136 and then Limited_View_Installed (Item)
5138 Remove_Limited_With_Clause (Item);
5144 -- Second Phase: Loop through context items and undo regular
5145 -- with_clauses and use_clauses.
5147 Item := First (Context_Items (N));
5148 while Present (Item) loop
5150 -- We are interested only in with clauses which got installed on
5151 -- entry, as indicated by their Context_Installed flag set
5153 if Nkind (Item) = N_With_Clause
5154 and then Limited_Present (Item)
5155 and then Limited_View_Installed (Item)
5159 elsif Nkind (Item) = N_With_Clause
5160 and then Context_Installed (Item)
5162 -- Remove items from one with'ed unit
5164 Unit_Name := Entity (Name (Item));
5165 Remove_Unit_From_Visibility (Unit_Name);
5166 Set_Context_Installed (Item, False);
5168 elsif Nkind (Item) = N_Use_Package_Clause then
5169 End_Use_Package (Item);
5171 elsif Nkind (Item) = N_Use_Type_Clause then
5172 End_Use_Type (Item);
5177 end Remove_Context_Clauses;
5179 --------------------------------
5180 -- Remove_Limited_With_Clause --
5181 --------------------------------
5183 procedure Remove_Limited_With_Clause (N : Node_Id) is
5184 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5187 Lim_Header : Entity_Id;
5188 Lim_Typ : Entity_Id;
5192 pragma Assert (Limited_View_Installed (N));
5194 -- In case of limited with_clause on subprograms, generics, instances,
5195 -- or renamings, the corresponding error was previously posted and we
5196 -- have nothing to do here.
5198 if Nkind (P_Unit) /= N_Package_Declaration then
5202 P := Defining_Unit_Name (Specification (P_Unit));
5204 -- Handle child packages
5206 if Nkind (P) = N_Defining_Program_Unit_Name then
5207 P := Defining_Identifier (P);
5210 if Debug_Flag_I then
5211 Write_Str ("remove limited view of ");
5212 Write_Name (Chars (P));
5213 Write_Str (" from visibility");
5217 -- Prepare the removal of the shadow entities from visibility. The
5218 -- first element of the limited view is a header (an E_Package
5219 -- entity) that is used to reference the first shadow entity in the
5220 -- private part of the package
5222 Lim_Header := Limited_View (P);
5223 Lim_Typ := First_Entity (Lim_Header);
5225 -- Remove package and shadow entities from visibility if it has not
5228 if not Analyzed (P_Unit) then
5230 Set_Is_Immediately_Visible (P, False);
5232 while Present (Lim_Typ) loop
5234 Next_Entity (Lim_Typ);
5237 -- Otherwise this package has already appeared in the closure and its
5238 -- shadow entities must be replaced by its real entities. This code
5239 -- must be kept synchronized with the complementary code in Install
5240 -- Limited_Withed_Unit.
5243 -- Real entities that are type or subtype declarations were hidden
5244 -- from visibility at the point of installation of the limited-view.
5245 -- Now we recover the previous value of the hidden attribute.
5247 E := First_Entity (P);
5248 while Present (E) and then E /= First_Private_Entity (P) loop
5250 Set_Is_Hidden (E, Was_Hidden (E));
5256 while Present (Lim_Typ)
5257 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5259 -- Nested packages and child units were not unchained
5261 if Ekind (Lim_Typ) /= E_Package
5262 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5264 -- Handle incomplete types of the real view. For this purpose
5265 -- we traverse the list of visible entities to look for an
5266 -- incomplete type in the real-view associated with Lim_Typ.
5268 E := First_Entity (P);
5269 while Present (E) and then E /= First_Private_Entity (P) loop
5270 exit when Ekind (E) = E_Incomplete_Type
5271 and then Present (Full_View (E))
5272 and then Full_View (E) = Lim_Typ;
5277 -- If the previous search was not sucessful then the entity
5278 -- to be restored in the homonym list is the non-limited view
5280 if E = First_Private_Entity (P) then
5281 E := Non_Limited_View (Lim_Typ);
5284 pragma Assert (not In_Chain (E));
5286 Prev := Current_Entity (Lim_Typ);
5288 if Prev = Lim_Typ then
5289 Set_Current_Entity (E);
5292 while Present (Prev)
5293 and then Homonym (Prev) /= Lim_Typ
5295 Prev := Homonym (Prev);
5298 if Present (Prev) then
5299 Set_Homonym (Prev, E);
5303 -- We must also set the next homonym entity of the real entity
5304 -- to handle the case in which the next homonym was a shadow
5307 Set_Homonym (E, Homonym (Lim_Typ));
5310 Next_Entity (Lim_Typ);
5314 -- Indicate that the limited view of the package is not installed
5316 Set_From_With_Type (P, False);
5317 Set_Limited_View_Installed (N, False);
5318 end Remove_Limited_With_Clause;
5320 --------------------
5321 -- Remove_Parents --
5322 --------------------
5324 procedure Remove_Parents (Lib_Unit : Node_Id) is
5327 P_Spec : Node_Id := Empty;
5329 Vis : constant Boolean :=
5330 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
5333 if Is_Child_Spec (Lib_Unit) then
5334 P_Spec := Parent_Spec (Lib_Unit);
5336 elsif Nkind (Lib_Unit) = N_Package_Body
5337 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
5339 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
5342 if Present (P_Spec) then
5345 P_Name := Get_Parent_Entity (P);
5346 Remove_Context_Clauses (P_Spec);
5347 End_Package_Scope (P_Name);
5348 Set_Is_Immediately_Visible (P_Name, Vis);
5350 -- Remove from visibility the siblings as well, which are directly
5351 -- visible while the parent is in scope.
5353 E := First_Entity (P_Name);
5354 while Present (E) loop
5355 if Is_Child_Unit (E) then
5356 Set_Is_Immediately_Visible (E, False);
5362 Set_In_Package_Body (P_Name, False);
5364 -- This is the recursive call to remove the context of any
5365 -- higher level parent. This recursion ensures that all parents
5366 -- are removed in the reverse order of their installation.
5372 ---------------------------------
5373 -- Remove_Private_With_Clauses --
5374 ---------------------------------
5376 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
5379 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
5380 -- Check whether a given unit appears in a regular with_clause.
5381 -- Used to determine whether a private_with_clause, implicit or
5382 -- explicit, should be ignored.
5384 ----------------------------
5385 -- In_Regular_With_Clause --
5386 ----------------------------
5388 function In_Regular_With_Clause (E : Entity_Id) return Boolean
5393 Item := First (Context_Items (Comp_Unit));
5394 while Present (Item) loop
5395 if Nkind (Item) = N_With_Clause
5396 and then Entity (Name (Item)) = E
5397 and then not Private_Present (Item)
5405 end In_Regular_With_Clause;
5407 -- Start of processing for Remove_Private_With_Clauses
5410 Item := First (Context_Items (Comp_Unit));
5411 while Present (Item) loop
5412 if Nkind (Item) = N_With_Clause
5413 and then Private_Present (Item)
5415 -- If private_with_clause is redundant, remove it from
5416 -- context, as a small optimization to subsequent handling
5417 -- of private_with clauses in other nested packages..
5419 if In_Regular_With_Clause (Entity (Name (Item))) then
5421 Nxt : constant Node_Id := Next (Item);
5427 elsif Limited_Present (Item) then
5428 if not Limited_View_Installed (Item) then
5429 Remove_Limited_With_Clause (Item);
5435 Remove_Unit_From_Visibility (Entity (Name (Item)));
5436 Set_Context_Installed (Item, False);
5444 end Remove_Private_With_Clauses;
5446 ---------------------------------
5447 -- Remove_Unit_From_Visibility --
5448 ---------------------------------
5450 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
5451 P : constant Entity_Id := Scope (Unit_Name);
5454 if Debug_Flag_I then
5455 Write_Str ("remove unit ");
5456 Write_Name (Chars (Unit_Name));
5457 Write_Str (" from visibility");
5461 if P /= Standard_Standard then
5462 Set_Is_Visible_Child_Unit (Unit_Name, False);
5465 Set_Is_Potentially_Use_Visible (Unit_Name, False);
5466 Set_Is_Immediately_Visible (Unit_Name, False);
5467 end Remove_Unit_From_Visibility;
5482 procedure Unchain (E : Entity_Id) is
5486 Prev := Current_Entity (E);
5492 Set_Name_Entity_Id (Chars (E), Homonym (E));
5495 while Present (Prev)
5496 and then Homonym (Prev) /= E
5498 Prev := Homonym (Prev);
5501 if Present (Prev) then
5502 Set_Homonym (Prev, Homonym (E));
5506 if Debug_Flag_I then
5507 Write_Str (" (homonym) unchain ");
5508 Write_Name (Chars (E));