1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Util; use Exp_Util;
31 with Elists; use Elists;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Inline; use Inline;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Output; use Output;
45 with Par_SCO; use Par_SCO;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch7; use Sem_Ch7;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Util; use Sem_Util;
57 with Sem_Warn; use Sem_Warn;
58 with Stand; use Stand;
59 with Sinfo; use Sinfo;
60 with Sinfo.CN; use Sinfo.CN;
61 with Sinput; use Sinput;
62 with Snames; use Snames;
63 with Style; use Style;
64 with Stylesw; use Stylesw;
65 with Tbuild; use Tbuild;
66 with Uname; use Uname;
68 package body Sem_Ch10 is
70 -----------------------
71 -- Local Subprograms --
72 -----------------------
74 procedure Analyze_Context (N : Node_Id);
75 -- Analyzes items in the context clause of compilation unit
77 procedure Build_Limited_Views (N : Node_Id);
78 -- Build and decorate the list of shadow entities for a package mentioned
79 -- in a limited_with clause. If the package was not previously analyzed
80 -- then it also performs a basic decoration of the real entities. This is
81 -- required to do not pass non-decorated entities to the back-end.
82 -- Implements Ada 2005 (AI-50217).
84 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
85 -- Check whether the source for the body of a compilation unit must be
86 -- included in a standalone library.
88 procedure Check_Private_Child_Unit (N : Node_Id);
89 -- If a with_clause mentions a private child unit, the compilation unit
90 -- must be a member of the same family, as described in 10.1.2.
92 procedure Check_Stub_Level (N : Node_Id);
93 -- Verify that a stub is declared immediately within a compilation unit,
94 -- and not in an inner frame.
96 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
97 -- When a child unit appears in a context clause, the implicit withs on
98 -- parents are made explicit, and with clauses are inserted in the context
99 -- clause before the one for the child. If a parent in the with_clause
100 -- is a renaming, the implicit with_clause is on the renaming whose name
101 -- is mentioned in the with_clause, and not on the package it renames.
102 -- N is the compilation unit whose list of context items receives the
103 -- implicit with_clauses.
105 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
106 -- Get defining entity of parent unit of a child unit. In most cases this
107 -- is the defining entity of the unit, but for a child instance whose
108 -- parent needs a body for inlining, the instantiation node of the parent
109 -- has not yet been rewritten as a package declaration, and the entity has
110 -- to be retrieved from the Instance_Spec of the unit.
112 function Has_With_Clause
115 Is_Limited : Boolean := False) return Boolean;
116 -- Determine whether compilation unit C_Unit contains a [limited] with
117 -- clause for package Pack. Use the flag Is_Limited to designate desired
120 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
121 -- If the main unit is a child unit, implicit withs are also added for
122 -- all its ancestors.
124 function In_Chain (E : Entity_Id) return Boolean;
125 -- Check that the shadow entity is not already in the homonym chain, for
126 -- example through a limited_with clause in a parent unit.
128 procedure Install_Context_Clauses (N : Node_Id);
129 -- Subsidiary to Install_Context and Install_Parents. Process all with
130 -- and use clauses for current unit and its library unit if any.
132 procedure Install_Limited_Context_Clauses (N : Node_Id);
133 -- Subsidiary to Install_Context. Process only limited with_clauses for
134 -- current unit. Implements Ada 2005 (AI-50217).
136 procedure Install_Limited_Withed_Unit (N : Node_Id);
137 -- Place shadow entities for a limited_with package in the visibility
138 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
140 procedure Install_Withed_Unit
141 (With_Clause : Node_Id;
142 Private_With_OK : Boolean := False);
143 -- If the unit is not a child unit, make unit immediately visible. The
144 -- caller ensures that the unit is not already currently installed. The
145 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
146 -- is called when compiling the private part of a package, or installing
147 -- the private declarations of a parent unit.
149 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
150 -- This procedure establishes the context for the compilation of a child
151 -- unit. If Lib_Unit is a child library spec then the context of the parent
152 -- is installed, and the parent itself made immediately visible, so that
153 -- the child unit is processed in the declarative region of the parent.
154 -- Install_Parents makes a recursive call to itself to ensure that all
155 -- parents are loaded in the nested case. If Lib_Unit is a library body,
156 -- the only effect of Install_Parents is to install the private decls of
157 -- the parents, because the visible parent declarations will have been
158 -- installed as part of the context of the corresponding spec.
160 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
161 -- In the compilation of a child unit, a child of any of the ancestor
162 -- units is directly visible if it is visible, because the parent is in
163 -- an enclosing scope. Iterate over context to find child units of U_Name
164 -- or of some ancestor of it.
166 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
167 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
168 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
169 -- a library spec that has a parent. If the call to Is_Child_Spec returns
170 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
171 -- compilation unit for the parent spec.
173 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
174 -- Parent_Spec is non-empty, this is also a child unit.
176 procedure Remove_Context_Clauses (N : Node_Id);
177 -- Subsidiary of previous one. Remove use_ and with_clauses
179 procedure Remove_Limited_With_Clause (N : Node_Id);
180 -- Remove from visibility the shadow entities introduced for a package
181 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
183 procedure Remove_Parents (Lib_Unit : Node_Id);
184 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
185 -- contexts established by the corresponding call to Install_Parents are
186 -- removed. Remove_Parents contains a recursive call to itself to ensure
187 -- that all parents are removed in the nested case.
189 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
190 -- Reset all visibility flags on unit after compiling it, either as a main
191 -- unit or as a unit in the context.
193 procedure Unchain (E : Entity_Id);
194 -- Remove single entity from visibility list
196 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
197 -- Common processing for all stubs (subprograms, tasks, packages, and
198 -- protected cases). N is the stub to be analyzed. Once the subunit name
199 -- is established, load and analyze. Nam is the non-overloadable entity
200 -- for which the proper body provides a completion. Subprogram stubs are
201 -- handled differently because they can be declarations.
204 -- A dummy procedure, for debugging use, called just before analyzing the
205 -- main unit (after dealing with any context clauses).
207 --------------------------
208 -- Limited_With_Clauses --
209 --------------------------
211 -- Limited_With clauses are the mechanism chosen for Ada05 to support
212 -- mutually recursive types declared in different units. A limited_with
213 -- clause that names package P in the context of unit U makes the types
214 -- declared in the visible part of P available within U, but with the
215 -- restriction that these types can only be used as incomplete types.
216 -- The limited_with clause does not impose a semantic dependence on P,
217 -- and it is possible for two packages to have limited_with_clauses on
218 -- each other without creating an elaboration circularity.
220 -- To support this feature, the analysis of a limited_with clause must
221 -- create an abbreviated view of the package, without performing any
222 -- semantic analysis on it. This "package abstract" contains shadow types
223 -- that are in one-one correspondence with the real types in the package,
224 -- and that have the properties of incomplete types.
226 -- The implementation creates two element lists: one to chain the shadow
227 -- entities, and one to chain the corresponding type entities in the tree
228 -- of the package. Links between corresponding entities in both chains
229 -- allow the compiler to select the proper view of a given type, depending
230 -- on the context. Note that in contrast with the handling of private
231 -- types, the limited view and the non-limited view of a type are treated
232 -- as separate entities, and no entity exchange needs to take place, which
233 -- makes the implementation must simpler than could be feared.
235 ------------------------------
236 -- Analyze_Compilation_Unit --
237 ------------------------------
239 procedure Analyze_Compilation_Unit (N : Node_Id) is
240 Unit_Node : constant Node_Id := Unit (N);
241 Lib_Unit : Node_Id := Library_Unit (N);
243 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
244 Par_Spec_Name : Unit_Name_Type;
245 Unum : Unit_Number_Type;
247 procedure Check_Redundant_Withs
248 (Context_Items : List_Id;
249 Spec_Context_Items : List_Id := No_List);
250 -- Determine whether the context list of a compilation unit contains
251 -- redundant with clauses. When checking body clauses against spec
252 -- clauses, set Context_Items to the context list of the body and
253 -- Spec_Context_Items to that of the spec. Parent packages are not
254 -- examined for documentation purposes.
256 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
257 -- Generate cross-reference information for the parents of child units.
258 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
260 ---------------------------
261 -- Check_Redundant_Withs --
262 ---------------------------
264 procedure Check_Redundant_Withs
265 (Context_Items : List_Id;
266 Spec_Context_Items : List_Id := No_List)
270 procedure Process_Body_Clauses
271 (Context_List : List_Id;
273 Used : in out Boolean;
274 Used_Type_Or_Elab : in out Boolean);
275 -- Examine the context clauses of a package body, trying to match the
276 -- name entity of Clause with any list element. If the match occurs
277 -- on a use package clause set Used to True, for a use type clause or
278 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
280 procedure Process_Spec_Clauses
281 (Context_List : List_Id;
283 Used : in out Boolean;
284 Withed : in out Boolean;
285 Exit_On_Self : Boolean := False);
286 -- Examine the context clauses of a package spec, trying to match
287 -- the name entity of Clause with any list element. If the match
288 -- occurs on a use package clause, set Used to True, for a with
289 -- package clause other than Clause, set Withed to True. Limited
290 -- with clauses, implicitly generated with clauses and withs
291 -- having pragmas Elaborate or Elaborate_All applied to them are
292 -- skipped. Exit_On_Self is used to control the search loop and
293 -- force an exit whenever Clause sees itself in the search.
295 --------------------------
296 -- Process_Body_Clauses --
297 --------------------------
299 procedure Process_Body_Clauses
300 (Context_List : List_Id;
302 Used : in out Boolean;
303 Used_Type_Or_Elab : in out Boolean)
305 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
311 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
312 -- In an expanded name in a use clause, if the prefix is a renamed
313 -- package, the entity is set to the original package as a result,
314 -- when checking whether the package appears in a previous with
315 -- clause, the renaming has to be taken into account, to prevent
316 -- spurious/incorrect warnings. A common case is use of Text_IO.
322 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
324 return Entity (N) = P
326 (Present (Renamed_Object (P))
327 and then Entity (N) = Renamed_Object (P));
330 -- Start of processing for Process_Body_Clauses
334 Used_Type_Or_Elab := False;
336 Cont_Item := First (Context_List);
337 while Present (Cont_Item) loop
339 -- Package use clause
341 if Nkind (Cont_Item) = N_Use_Package_Clause
344 -- Search through use clauses
346 Use_Item := First (Names (Cont_Item));
347 while Present (Use_Item) and then not Used loop
349 -- Case of a direct use of the one we are looking for
351 if Entity (Use_Item) = Nam_Ent then
354 -- Handle nested case, as in "with P; use P.Q.R"
361 -- Loop through prefixes looking for match
364 while Nkind (UE) = N_Expanded_Name loop
365 if Same_Unit (Prefix (UE), Nam_Ent) then
380 elsif Nkind (Cont_Item) = N_Use_Type_Clause
381 and then not Used_Type_Or_Elab
383 Subt_Mark := First (Subtype_Marks (Cont_Item));
384 while Present (Subt_Mark)
385 and then not Used_Type_Or_Elab
387 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
388 Used_Type_Or_Elab := True;
394 -- Pragma Elaborate or Elaborate_All
396 elsif Nkind (Cont_Item) = N_Pragma
398 (Pragma_Name (Cont_Item) = Name_Elaborate
400 Pragma_Name (Cont_Item) = Name_Elaborate_All)
401 and then not Used_Type_Or_Elab
404 First (Pragma_Argument_Associations (Cont_Item));
405 while Present (Prag_Unit)
406 and then not Used_Type_Or_Elab
408 if Entity (Expression (Prag_Unit)) = Nam_Ent then
409 Used_Type_Or_Elab := True;
418 end Process_Body_Clauses;
420 --------------------------
421 -- Process_Spec_Clauses --
422 --------------------------
424 procedure Process_Spec_Clauses
425 (Context_List : List_Id;
427 Used : in out Boolean;
428 Withed : in out Boolean;
429 Exit_On_Self : Boolean := False)
431 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
439 Cont_Item := First (Context_List);
440 while Present (Cont_Item) loop
442 -- Stop the search since the context items after Cont_Item have
443 -- already been examined in a previous iteration of the reverse
444 -- loop in Check_Redundant_Withs.
447 and Cont_Item = Clause
452 -- Package use clause
454 if Nkind (Cont_Item) = N_Use_Package_Clause
457 Use_Item := First (Names (Cont_Item));
458 while Present (Use_Item) and then not Used loop
459 if Entity (Use_Item) = Nam_Ent then
466 -- Package with clause. Avoid processing self, implicitly
467 -- generated with clauses or limited with clauses. Note that
468 -- we examine with clauses having pragmas Elaborate or
469 -- Elaborate_All applied to them due to cases such as:
474 -- pragma Elaborate (Pack);
476 -- In this case, the second with clause is redundant since
477 -- the pragma applies only to the first "with Pack;".
479 elsif Nkind (Cont_Item) = N_With_Clause
480 and then not Implicit_With (Cont_Item)
481 and then not Limited_Present (Cont_Item)
482 and then Cont_Item /= Clause
483 and then Entity (Name (Cont_Item)) = Nam_Ent
490 end Process_Spec_Clauses;
492 -- Start of processing for Check_Redundant_Withs
495 Clause := Last (Context_Items);
496 while Present (Clause) loop
498 -- Avoid checking implicitly generated with clauses, limited with
499 -- clauses or withs that have pragma Elaborate or Elaborate_All.
501 if Nkind (Clause) = N_With_Clause
502 and then not Implicit_With (Clause)
503 and then not Limited_Present (Clause)
504 and then not Elaborate_Present (Clause)
506 -- Package body-to-spec check
508 if Present (Spec_Context_Items) then
510 Used_In_Body : Boolean := False;
511 Used_In_Spec : Boolean := False;
512 Used_Type_Or_Elab : Boolean := False;
513 Withed_In_Spec : Boolean := False;
517 (Context_List => Spec_Context_Items,
519 Used => Used_In_Spec,
520 Withed => Withed_In_Spec);
523 (Context_List => Context_Items,
525 Used => Used_In_Body,
526 Used_Type_Or_Elab => Used_Type_Or_Elab);
528 -- "Type Elab" refers to the presence of either a use
529 -- type clause, pragmas Elaborate or Elaborate_All.
531 -- +---------------+---------------------------+------+
532 -- | Spec | Body | Warn |
533 -- +--------+------+--------+------+-----------+------+
534 -- | Withed | Used | Withed | Used | Type Elab | |
535 -- | X | | X | | | X |
536 -- | X | | X | X | | |
537 -- | X | | X | | X | |
538 -- | X | | X | X | X | |
539 -- | X | X | X | | | X |
540 -- | X | X | X | | X | |
541 -- | X | X | X | X | | X |
542 -- | X | X | X | X | X | |
543 -- +--------+------+--------+------+-----------+------+
546 and then not Used_Type_Or_Elab)
549 and then not Used_In_Body)
553 Error_Msg_N -- CODEFIX
554 ("?redundant with clause in body", Clause);
557 Used_In_Body := False;
558 Used_In_Spec := False;
559 Used_Type_Or_Elab := False;
560 Withed_In_Spec := False;
563 -- Standalone package spec or body check
567 Dont_Care : Boolean := False;
568 Withed : Boolean := False;
571 -- The mechanism for examining the context clauses of a
572 -- package spec can be applied to package body clauses.
575 (Context_List => Context_Items,
579 Exit_On_Self => True);
582 Error_Msg_N -- CODEFIX
583 ("?redundant with clause", Clause);
591 end Check_Redundant_Withs;
593 --------------------------------
594 -- Generate_Parent_References --
595 --------------------------------
597 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
599 P_Name : Entity_Id := P_Id;
602 Pref := Name (Parent (Defining_Entity (N)));
604 if Nkind (Pref) = N_Expanded_Name then
606 -- Done already, if the unit has been compiled indirectly as
607 -- part of the closure of its context because of inlining.
612 while Nkind (Pref) = N_Selected_Component loop
613 Change_Selected_Component_To_Expanded_Name (Pref);
614 Set_Entity (Pref, P_Name);
615 Set_Etype (Pref, Etype (P_Name));
616 Generate_Reference (P_Name, Pref, 'r');
617 Pref := Prefix (Pref);
618 P_Name := Scope (P_Name);
621 -- The guard here on P_Name is to handle the error condition where
622 -- the parent unit is missing because the file was not found.
624 if Present (P_Name) then
625 Set_Entity (Pref, P_Name);
626 Set_Etype (Pref, Etype (P_Name));
627 Generate_Reference (P_Name, Pref, 'r');
628 Style.Check_Identifier (Pref, P_Name);
630 end Generate_Parent_References;
632 -- Start of processing for Analyze_Compilation_Unit
635 Process_Compilation_Unit_Pragmas (N);
637 -- If the unit is a subunit whose parent has not been analyzed (which
638 -- indicates that the main unit is a subunit, either the current one or
639 -- one of its descendents) then the subunit is compiled as part of the
640 -- analysis of the parent, which we proceed to do. Basically this gets
641 -- handled from the top down and we don't want to do anything at this
642 -- level (i.e. this subunit will be handled on the way down from the
643 -- parent), so at this level we immediately return. If the subunit ends
644 -- up not analyzed, it means that the parent did not contain a stub for
645 -- it, or that there errors were detected in some ancestor.
647 if Nkind (Unit_Node) = N_Subunit
648 and then not Analyzed (Lib_Unit)
650 Semantics (Lib_Unit);
652 if not Analyzed (Proper_Body (Unit_Node)) then
653 if Serious_Errors_Detected > 0 then
654 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
656 Error_Msg_N ("missing stub for subunit", N);
663 -- Analyze context (this will call Sem recursively for with'ed units) To
664 -- detect circularities among with-clauses that are not caught during
665 -- loading, we set the Context_Pending flag on the current unit. If the
666 -- flag is already set there is a potential circularity. We exclude
667 -- predefined units from this check because they are known to be safe.
668 -- We also exclude package bodies that are present because circularities
669 -- between bodies are harmless (and necessary).
671 if Context_Pending (N) then
673 Circularity : Boolean := True;
676 if Is_Predefined_File_Name
677 (Unit_File_Name (Get_Source_Unit (Unit (N))))
679 Circularity := False;
682 for U in Main_Unit + 1 .. Last_Unit loop
683 if Nkind (Unit (Cunit (U))) = N_Package_Body
684 and then not Analyzed (Cunit (U))
686 Circularity := False;
693 Error_Msg_N ("circular dependency caused by with_clauses", N);
695 ("\possibly missing limited_with clause"
696 & " in one of the following", N);
698 for U in Main_Unit .. Last_Unit loop
699 if Context_Pending (Cunit (U)) then
700 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
701 Error_Msg_N ("\unit$", N);
705 raise Unrecoverable_Error;
709 Set_Context_Pending (N);
714 Set_Context_Pending (N, False);
716 -- If the unit is a package body, the spec is already loaded and must be
717 -- analyzed first, before we analyze the body.
719 if Nkind (Unit_Node) = N_Package_Body then
721 -- If no Lib_Unit, then there was a serious previous error, so just
722 -- ignore the entire analysis effort
724 if No (Lib_Unit) then
728 Semantics (Lib_Unit);
729 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
731 -- Verify that the library unit is a package declaration
733 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
734 N_Generic_Package_Declaration)
737 ("no legal package declaration for package body", N);
740 -- Otherwise, the entity in the declaration is visible. Update the
741 -- version to reflect dependence of this body on the spec.
744 Spec_Id := Defining_Entity (Unit (Lib_Unit));
745 Set_Is_Immediately_Visible (Spec_Id, True);
746 Version_Update (N, Lib_Unit);
748 if Nkind (Defining_Unit_Name (Unit_Node)) =
749 N_Defining_Program_Unit_Name
751 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
756 -- If the unit is a subprogram body, then we similarly need to analyze
757 -- its spec. However, things are a little simpler in this case, because
758 -- here, this analysis is done mostly for error checking and consistency
759 -- purposes (but not only, e.g. there could be a contract on the spec),
760 -- so there's nothing else to be done.
762 elsif Nkind (Unit_Node) = N_Subprogram_Body then
763 if Acts_As_Spec (N) then
765 -- If the subprogram body is a child unit, we must create a
766 -- declaration for it, in order to properly load the parent(s).
767 -- After this, the original unit does not acts as a spec, because
768 -- there is an explicit one. If this unit appears in a context
769 -- clause, then an implicit with on the parent will be added when
770 -- installing the context. If this is the main unit, there is no
771 -- Unit_Table entry for the declaration (it has the unit number
772 -- of the main unit) and code generation is unaffected.
774 Unum := Get_Cunit_Unit_Number (N);
775 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
777 if Par_Spec_Name /= No_Unit_Name then
780 (Load_Name => Par_Spec_Name,
785 if Unum /= No_Unit then
787 -- Build subprogram declaration and attach parent unit to it
788 -- This subprogram declaration does not come from source,
789 -- Nevertheless the backend must generate debugging info for
790 -- it, and this must be indicated explicitly. We also mark
791 -- the body entity as a child unit now, to prevent a
792 -- cascaded error if the spec entity cannot be entered
793 -- in its scope. Finally we create a Units table entry for
794 -- the subprogram declaration, to maintain a one-to-one
795 -- correspondence with compilation unit nodes. This is
796 -- critical for the tree traversals performed by CodePeer.
799 Loc : constant Source_Ptr := Sloc (N);
800 SCS : constant Boolean :=
801 Get_Comes_From_Source_Default;
804 Set_Comes_From_Source_Default (False);
806 Make_Compilation_Unit (Loc,
807 Context_Items => New_Copy_List (Context_Items (N)),
809 Make_Subprogram_Declaration (Sloc (N),
812 (Specification (Unit_Node))),
814 Make_Compilation_Unit_Aux (Loc));
816 Set_Library_Unit (N, Lib_Unit);
817 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
818 Make_Child_Decl_Unit (N);
819 Semantics (Lib_Unit);
821 -- Now that a separate declaration exists, the body
822 -- of the child unit does not act as spec any longer.
824 Set_Acts_As_Spec (N, False);
825 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
826 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
827 Set_Comes_From_Source_Default (SCS);
832 -- Here for subprogram with separate declaration
835 Semantics (Lib_Unit);
836 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
837 Version_Update (N, Lib_Unit);
840 -- If this is a child unit, generate references to the parents
842 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
843 N_Defining_Program_Unit_Name
845 Generate_Parent_References (
846 Specification (Unit_Node),
847 Scope (Defining_Entity (Unit (Lib_Unit))));
851 -- If it is a child unit, the parent must be elaborated first and we
852 -- update version, since we are dependent on our parent.
854 if Is_Child_Spec (Unit_Node) then
856 -- The analysis of the parent is done with style checks off
859 Save_Style_Check : constant Boolean := Style_Check;
860 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
861 Cunit_Boolean_Restrictions_Save;
864 if not GNAT_Mode then
865 Style_Check := False;
868 Semantics (Parent_Spec (Unit_Node));
869 Version_Update (N, Parent_Spec (Unit_Node));
870 Style_Check := Save_Style_Check;
871 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
875 -- With the analysis done, install the context. Note that we can't
876 -- install the context from the with clauses as we analyze them, because
877 -- each with clause must be analyzed in a clean visibility context, so
878 -- we have to wait and install them all at once.
882 if Is_Child_Spec (Unit_Node) then
884 -- Set the entities of all parents in the program_unit_name
886 Generate_Parent_References (
887 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
890 -- All components of the context: with-clauses, library unit, ancestors
891 -- if any, (and their context) are analyzed and installed.
893 -- Call special debug routine sm if this is the main unit
895 if Current_Sem_Unit = Main_Unit then
899 -- Now analyze the unit (package, subprogram spec, body) itself
903 if Warn_On_Redundant_Constructs then
904 Check_Redundant_Withs (Context_Items (N));
906 if Nkind (Unit_Node) = N_Package_Body then
907 Check_Redundant_Withs
908 (Context_Items => Context_Items (N),
909 Spec_Context_Items => Context_Items (Lib_Unit));
913 -- The above call might have made Unit_Node an N_Subprogram_Body from
914 -- something else, so propagate any Acts_As_Spec flag.
916 if Nkind (Unit_Node) = N_Subprogram_Body
917 and then Acts_As_Spec (Unit_Node)
919 Set_Acts_As_Spec (N);
922 -- Register predefined units in Rtsfind
925 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
927 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
928 Set_RTU_Loaded (Unit_Node);
932 -- Treat compilation unit pragmas that appear after the library unit
934 if Present (Pragmas_After (Aux_Decls_Node (N))) then
936 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
938 while Present (Prag_Node) loop
945 -- Generate distribution stubs if requested and no error
948 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
950 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
951 and then not Fatal_Error (Main_Unit)
953 if Is_RCI_Pkg_Spec_Or_Body (N) then
955 -- Regular RCI package
957 Add_Stub_Constructs (N);
959 elsif (Nkind (Unit_Node) = N_Package_Declaration
960 and then Is_Shared_Passive (Defining_Entity
961 (Specification (Unit_Node))))
962 or else (Nkind (Unit_Node) = N_Package_Body
964 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
966 -- Shared passive package
968 Add_Stub_Constructs (N);
970 elsif Nkind (Unit_Node) = N_Package_Instantiation
972 Is_Remote_Call_Interface
973 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
975 -- Instantiation of a RCI generic package
977 Add_Stub_Constructs (N);
981 -- Remove unit from visibility, so that environment is clean for the
982 -- next compilation, which is either the main unit or some other unit
985 if Nkind_In (Unit_Node, N_Package_Declaration,
986 N_Package_Renaming_Declaration,
987 N_Subprogram_Declaration)
988 or else Nkind (Unit_Node) in N_Generic_Declaration
990 (Nkind (Unit_Node) = N_Subprogram_Body
991 and then Acts_As_Spec (Unit_Node))
993 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
995 -- If the unit is an instantiation whose body will be elaborated for
996 -- inlining purposes, use the proper entity of the instance. The entity
997 -- may be missing if the instantiation was illegal.
999 elsif Nkind (Unit_Node) = N_Package_Instantiation
1000 and then not Error_Posted (Unit_Node)
1001 and then Present (Instance_Spec (Unit_Node))
1003 Remove_Unit_From_Visibility
1004 (Defining_Entity (Instance_Spec (Unit_Node)));
1006 elsif Nkind (Unit_Node) = N_Package_Body
1007 or else (Nkind (Unit_Node) = N_Subprogram_Body
1008 and then not Acts_As_Spec (Unit_Node))
1010 -- Bodies that are not the main unit are compiled if they are generic
1011 -- or contain generic or inlined units. Their analysis brings in the
1012 -- context of the corresponding spec (unit declaration) which must be
1013 -- removed as well, to return the compilation environment to its
1016 Remove_Context (Lib_Unit);
1017 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1020 -- Last step is to deinstall the context we just installed as well as
1021 -- the unit just compiled.
1025 -- If this is the main unit and we are generating code, we must check
1026 -- that all generic units in the context have a body if they need it,
1027 -- even if they have not been instantiated. In the absence of .ali files
1028 -- for generic units, we must force the load of the body, just to
1029 -- produce the proper error if the body is absent. We skip this
1030 -- verification if the main unit itself is generic.
1032 if Get_Cunit_Unit_Number (N) = Main_Unit
1033 and then Operating_Mode = Generate_Code
1034 and then Expander_Active
1036 -- Check whether the source for the body of the unit must be included
1037 -- in a standalone library.
1039 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1041 -- Indicate that the main unit is now analyzed, to catch possible
1042 -- circularities between it and generic bodies. Remove main unit from
1043 -- visibility. This might seem superfluous, but the main unit must
1044 -- not be visible in the generic body expansions that follow.
1046 Set_Analyzed (N, True);
1047 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1052 Un : Unit_Number_Type;
1054 Save_Style_Check : constant Boolean := Style_Check;
1055 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
1056 Cunit_Boolean_Restrictions_Save;
1059 Item := First (Context_Items (N));
1060 while Present (Item) loop
1062 -- Check for explicit with clause
1064 if Nkind (Item) = N_With_Clause
1065 and then not Implicit_With (Item)
1067 -- Ada 2005 (AI-50217): Ignore limited-withed units
1069 and then not Limited_Present (Item)
1071 Nam := Entity (Name (Item));
1073 -- Compile generic subprogram, unless it is intrinsic or
1074 -- imported so no body is required, or generic package body
1075 -- if the package spec requires a body.
1077 if (Is_Generic_Subprogram (Nam)
1078 and then not Is_Intrinsic_Subprogram (Nam)
1079 and then not Is_Imported (Nam))
1080 or else (Ekind (Nam) = E_Generic_Package
1081 and then Unit_Requires_Body (Nam))
1083 Style_Check := False;
1085 if Present (Renamed_Object (Nam)) then
1088 (Load_Name => Get_Body_Name
1090 (Unit_Declaration_Node
1091 (Renamed_Object (Nam)))),
1099 (Load_Name => Get_Body_Name
1100 (Get_Unit_Name (Item)),
1107 if Un = No_Unit then
1109 ("body of generic unit& not found", Item, Nam);
1112 elsif not Analyzed (Cunit (Un))
1113 and then Un /= Main_Unit
1114 and then not Fatal_Error (Un)
1116 Style_Check := False;
1117 Semantics (Cunit (Un));
1125 Style_Check := Save_Style_Check;
1126 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1130 -- Deal with creating elaboration Boolean if needed. We create an
1131 -- elaboration boolean only for units that come from source since
1132 -- units manufactured by the compiler never need elab checks.
1134 if Comes_From_Source (N)
1135 and then Nkind_In (Unit_Node, N_Package_Declaration,
1136 N_Generic_Package_Declaration,
1137 N_Subprogram_Declaration,
1138 N_Generic_Subprogram_Declaration)
1141 Loc : constant Source_Ptr := Sloc (N);
1142 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1145 Spec_Id := Defining_Entity (Unit_Node);
1146 Generate_Definition (Spec_Id);
1148 -- See if an elaboration entity is required for possible access
1149 -- before elaboration checking. Note that we must allow for this
1150 -- even if -gnatE is not set, since a client may be compiled in
1151 -- -gnatE mode and reference the entity.
1153 -- These entities are also used by the binder to prevent multiple
1154 -- attempts to execute the elaboration code for the library case
1155 -- where the elaboration routine might otherwise be called more
1158 -- Case of units which do not require elaboration checks
1161 -- Pure units do not need checks
1165 -- Preelaborated units do not need checks
1167 or else Is_Preelaborated (Spec_Id)
1169 -- No checks needed if pragma Elaborate_Body present
1171 or else Has_Pragma_Elaborate_Body (Spec_Id)
1173 -- No checks needed if unit does not require a body
1175 or else not Unit_Requires_Body (Spec_Id)
1177 -- No checks needed for predefined files
1179 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1181 -- No checks required if no separate spec
1183 or else Acts_As_Spec (N)
1185 -- This is a case where we only need the entity for
1186 -- checking to prevent multiple elaboration checks.
1188 Set_Elaboration_Entity_Required (Spec_Id, False);
1190 -- Case of elaboration entity is required for access before
1191 -- elaboration checking (so certainly we must build it!)
1194 Set_Elaboration_Entity_Required (Spec_Id, True);
1197 Build_Elaboration_Entity (N, Spec_Id);
1201 -- Freeze the compilation unit entity. This for sure is needed because
1202 -- of some warnings that can be output (see Freeze_Subprogram), but may
1203 -- in general be required. If freezing actions result, place them in the
1204 -- compilation unit actions list, and analyze them.
1207 L : constant List_Id :=
1208 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1210 while Is_Non_Empty_List (L) loop
1211 Insert_Library_Level_Action (Remove_Head (L));
1217 if Nkind (Unit_Node) = N_Package_Declaration
1218 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1219 and then Expander_Active
1222 Save_Style_Check : constant Boolean := Style_Check;
1223 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1224 Options : Style_Check_Options;
1227 Save_Style_Check_Options (Options);
1228 Reset_Style_Check_Options;
1229 Opt.Warning_Mode := Suppress;
1230 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1232 Reset_Style_Check_Options;
1233 Set_Style_Check_Options (Options);
1234 Style_Check := Save_Style_Check;
1235 Warning_Mode := Save_Warning;
1239 -- If we are generating obsolescent warnings, then here is where we
1240 -- generate them for the with'ed items. The reason for this special
1241 -- processing is that the normal mechanism of generating the warnings
1242 -- for referenced entities does not work for context clause references.
1243 -- That's because when we first analyze the context, it is too early to
1244 -- know if the with'ing unit is itself obsolescent (which suppresses
1247 if not GNAT_Mode and then Warn_On_Obsolescent_Feature then
1249 -- Push current compilation unit as scope, so that the test for
1250 -- being within an obsolescent unit will work correctly.
1252 Push_Scope (Defining_Entity (Unit_Node));
1254 -- Loop through context items to deal with with clauses
1262 Item := First (Context_Items (N));
1263 while Present (Item) loop
1264 if Nkind (Item) = N_With_Clause
1266 -- Suppress this check in limited-withed units. Further work
1267 -- needed here if we decide to incorporate this check on
1268 -- limited-withed units.
1270 and then not Limited_Present (Item)
1273 Ent := Entity (Nam);
1275 if Is_Obsolescent (Ent) then
1276 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1284 -- Remove temporary install of current unit as scope
1288 end Analyze_Compilation_Unit;
1290 ---------------------
1291 -- Analyze_Context --
1292 ---------------------
1294 procedure Analyze_Context (N : Node_Id) is
1295 Ukind : constant Node_Kind := Nkind (Unit (N));
1299 -- First process all configuration pragmas at the start of the context
1300 -- items. Strictly these are not part of the context clause, but that
1301 -- is where the parser puts them. In any case for sure we must analyze
1302 -- these before analyzing the actual context items, since they can have
1303 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1304 -- be with'ed as a result of changing categorizations in Ada 2005).
1306 Item := First (Context_Items (N));
1307 while Present (Item)
1308 and then Nkind (Item) = N_Pragma
1309 and then Pragma_Name (Item) in Configuration_Pragma_Names
1315 -- This is the point at which we capture the configuration settings
1316 -- for the unit. At the moment only the Optimize_Alignment setting
1317 -- needs to be captured. Probably more later ???
1319 if Optimize_Alignment_Local then
1320 Set_OA_Setting (Current_Sem_Unit, 'L');
1322 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1325 -- Loop through actual context items. This is done in two passes:
1327 -- a) The first pass analyzes non-limited with-clauses and also any
1328 -- configuration pragmas (we need to get the latter analyzed right
1329 -- away, since they can affect processing of subsequent items.
1331 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1333 while Present (Item) loop
1335 -- For with clause, analyze the with clause, and then update the
1336 -- version, since we are dependent on a unit that we with.
1338 if Nkind (Item) = N_With_Clause
1339 and then not Limited_Present (Item)
1341 -- Skip analyzing with clause if no unit, nothing to do (this
1342 -- happens for a with that references a non-existent unit). Skip
1343 -- as well if this is a with_clause for the main unit, which
1344 -- happens if a subunit has a useless with_clause on its parent.
1346 if Present (Library_Unit (Item)) then
1347 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1351 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1355 if not Implicit_With (Item) then
1356 Version_Update (N, Library_Unit (Item));
1359 -- Skip pragmas. Configuration pragmas at the start were handled in
1360 -- the loop above, and remaining pragmas are not processed until we
1361 -- actually install the context (see Install_Context). We delay the
1362 -- analysis of these pragmas to make sure that we have installed all
1363 -- the implicit with's on parent units.
1365 -- Skip use clauses at this stage, since we don't want to do any
1366 -- installing of potentially use-visible entities until we
1367 -- actually install the complete context (in Install_Context).
1368 -- Otherwise things can get installed in the wrong context.
1377 -- Second pass: examine all limited_with clauses. All other context
1378 -- items are ignored in this pass.
1380 Item := First (Context_Items (N));
1381 while Present (Item) loop
1382 if Nkind (Item) = N_With_Clause
1383 and then Limited_Present (Item)
1385 -- No need to check errors on implicitly generated limited-with
1388 if not Implicit_With (Item) then
1390 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1391 -- properly rejected, including renaming declarations.
1393 if not Nkind_In (Ukind, N_Package_Declaration,
1394 N_Subprogram_Declaration)
1395 and then Ukind not in N_Generic_Declaration
1396 and then Ukind not in N_Generic_Instantiation
1398 Error_Msg_N ("limited with_clause not allowed here", Item);
1400 -- Check wrong use of a limited with clause applied to the
1401 -- compilation unit containing the limited-with clause.
1403 -- limited with P.Q;
1404 -- package P.Q is ...
1406 elsif Unit (Library_Unit (Item)) = Unit (N) then
1407 Error_Msg_N ("wrong use of limited-with clause", Item);
1409 -- Check wrong use of limited-with clause applied to some
1410 -- immediate ancestor.
1412 elsif Is_Child_Spec (Unit (N)) then
1414 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1418 P := Parent_Spec (Unit (N));
1420 if Unit (P) = Lib_U then
1421 Error_Msg_N ("limited with_clause cannot "
1422 & "name ancestor", Item);
1426 exit when not Is_Child_Spec (Unit (P));
1427 P := Parent_Spec (Unit (P));
1432 -- Check if the limited-withed unit is already visible through
1433 -- some context clause of the current compilation unit or some
1434 -- ancestor of the current compilation unit.
1437 Lim_Unit_Name : constant Node_Id := Name (Item);
1438 Comp_Unit : Node_Id;
1440 Unit_Name : Node_Id;
1445 It := First (Context_Items (Comp_Unit));
1446 while Present (It) loop
1448 and then Nkind (It) = N_With_Clause
1449 and then not Limited_Present (It)
1451 Nkind_In (Unit (Library_Unit (It)),
1452 N_Package_Declaration,
1453 N_Package_Renaming_Declaration)
1455 if Nkind (Unit (Library_Unit (It))) =
1456 N_Package_Declaration
1458 Unit_Name := Name (It);
1460 Unit_Name := Name (Unit (Library_Unit (It)));
1463 -- Check if the named package (or some ancestor)
1464 -- leaves visible the full-view of the unit given
1465 -- in the limited-with clause
1468 if Designate_Same_Unit (Lim_Unit_Name,
1471 Error_Msg_Sloc := Sloc (It);
1473 ("simultaneous visibility of limited "
1474 & "and unlimited views not allowed",
1477 ("\unlimited view visible through "
1478 & "context clause #",
1482 elsif Nkind (Unit_Name) = N_Identifier then
1486 Unit_Name := Prefix (Unit_Name);
1493 exit when not Is_Child_Spec (Unit (Comp_Unit));
1495 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1500 -- Skip analyzing with clause if no unit, see above
1502 if Present (Library_Unit (Item)) then
1506 -- A limited_with does not impose an elaboration order, but
1507 -- there is a semantic dependency for recompilation purposes.
1509 if not Implicit_With (Item) then
1510 Version_Update (N, Library_Unit (Item));
1513 -- Pragmas and use clauses and with clauses other than limited
1514 -- with's are ignored in this pass through the context items.
1522 end Analyze_Context;
1524 -------------------------------
1525 -- Analyze_Package_Body_Stub --
1526 -------------------------------
1528 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1529 Id : constant Entity_Id := Defining_Identifier (N);
1533 -- The package declaration must be in the current declarative part
1535 Check_Stub_Level (N);
1536 Nam := Current_Entity_In_Scope (Id);
1538 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1539 Error_Msg_N ("missing specification for package stub", N);
1541 elsif Has_Completion (Nam)
1542 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1544 Error_Msg_N ("duplicate or redundant stub for package", N);
1547 -- Indicate that the body of the package exists. If we are doing
1548 -- only semantic analysis, the stub stands for the body. If we are
1549 -- generating code, the existence of the body will be confirmed
1550 -- when we load the proper body.
1552 Set_Has_Completion (Nam);
1553 Set_Scope (Defining_Entity (N), Current_Scope);
1554 Generate_Reference (Nam, Id, 'b');
1555 Analyze_Proper_Body (N, Nam);
1557 end Analyze_Package_Body_Stub;
1559 -------------------------
1560 -- Analyze_Proper_Body --
1561 -------------------------
1563 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1564 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1565 Unum : Unit_Number_Type;
1567 procedure Optional_Subunit;
1568 -- This procedure is called when the main unit is a stub, or when we
1569 -- are not generating code. In such a case, we analyze the subunit if
1570 -- present, which is user-friendly and in fact required for ASIS, but
1571 -- we don't complain if the subunit is missing.
1573 ----------------------
1574 -- Optional_Subunit --
1575 ----------------------
1577 procedure Optional_Subunit is
1578 Comp_Unit : Node_Id;
1581 -- Try to load subunit, but ignore any errors that occur during the
1582 -- loading of the subunit, by using the special feature in Errout to
1583 -- ignore all errors. Note that Fatal_Error will still be set, so we
1584 -- will be able to check for this case below.
1586 if not ASIS_Mode then
1587 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1592 (Load_Name => Subunit_Name,
1597 if not ASIS_Mode then
1598 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1601 -- All done if we successfully loaded the subunit
1604 and then (not Fatal_Error (Unum) or else Try_Semantics)
1606 Comp_Unit := Cunit (Unum);
1608 -- If the file was empty or seriously mangled, the unit itself may
1611 if No (Unit (Comp_Unit)) then
1613 ("subunit does not contain expected proper body", N);
1615 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1617 ("expected SEPARATE subunit, found child unit",
1618 Cunit_Entity (Unum));
1620 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1621 Analyze_Subunit (Comp_Unit);
1622 Set_Library_Unit (N, Comp_Unit);
1625 elsif Unum = No_Unit
1626 and then Present (Nam)
1628 if Is_Protected_Type (Nam) then
1629 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1631 Set_Corresponding_Body (
1632 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1635 end Optional_Subunit;
1637 -- Start of processing for Analyze_Proper_Body
1640 -- If the subunit is already loaded, it means that the main unit is a
1641 -- subunit, and that the current unit is one of its parents which was
1642 -- being analyzed to provide the needed context for the analysis of the
1643 -- subunit. In this case we analyze the subunit and continue with the
1644 -- parent, without looking a subsequent subunits.
1646 if Is_Loaded (Subunit_Name) then
1648 -- If the proper body is already linked to the stub node, the stub is
1649 -- in a generic unit and just needs analyzing.
1651 if Present (Library_Unit (N)) then
1652 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1653 Analyze_Subunit (Library_Unit (N));
1655 -- Otherwise we must load the subunit and link to it
1658 -- Load the subunit, this must work, since we originally loaded
1659 -- the subunit earlier on. So this will not really load it, just
1660 -- give access to it.
1664 (Load_Name => Subunit_Name,
1669 -- And analyze the subunit in the parent context (note that we
1670 -- do not call Semantics, since that would remove the parent
1671 -- context). Because of this, we have to manually reset the
1672 -- compiler state to Analyzing since it got destroyed by Load.
1674 if Unum /= No_Unit then
1675 Compiler_State := Analyzing;
1677 -- Check that the proper body is a subunit and not a child
1678 -- unit. If the unit was previously loaded, the error will
1679 -- have been emitted when copying the generic node, so we
1680 -- just return to avoid cascaded errors.
1682 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1686 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1687 Analyze_Subunit (Cunit (Unum));
1688 Set_Library_Unit (N, Cunit (Unum));
1692 -- If the main unit is a subunit, then we are just performing semantic
1693 -- analysis on that subunit, and any other subunits of any parent unit
1694 -- should be ignored, except that if we are building trees for ASIS
1695 -- usage we want to annotate the stub properly.
1697 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1698 and then Subunit_Name /= Unit_Name (Main_Unit)
1704 -- But before we return, set the flag for unloaded subunits. This
1705 -- will suppress junk warnings of variables in the same declarative
1706 -- part (or a higher level one) that are in danger of looking unused
1707 -- when in fact there might be a declaration in the subunit that we
1708 -- do not intend to load.
1710 Unloaded_Subunits := True;
1713 -- If the subunit is not already loaded, and we are generating code,
1714 -- then this is the case where compilation started from the parent, and
1715 -- we are generating code for an entire subunit tree. In that case we
1716 -- definitely need to load the subunit.
1718 -- In order to continue the analysis with the rest of the parent,
1719 -- and other subunits, we load the unit without requiring its
1720 -- presence, and emit a warning if not found, rather than terminating
1721 -- the compilation abruptly, as for other missing file problems.
1723 elsif Original_Operating_Mode = Generate_Code then
1725 -- If the proper body is already linked to the stub node, the stub is
1726 -- in a generic unit and just needs analyzing.
1728 -- We update the version. Although we are not strictly technically
1729 -- semantically dependent on the subunit, given our approach of macro
1730 -- substitution of subunits, it makes sense to include it in the
1731 -- version identification.
1733 if Present (Library_Unit (N)) then
1734 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1735 Analyze_Subunit (Library_Unit (N));
1736 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1738 -- Otherwise we must load the subunit and link to it
1741 -- Make sure that, if the subunit is preprocessed and -gnateG is
1742 -- specified, the preprocessed file will be written.
1744 Lib.Analysing_Subunit_Of_Main := True;
1747 (Load_Name => Subunit_Name,
1751 Lib.Analysing_Subunit_Of_Main := False;
1753 -- Give message if we did not get the unit Emit warning even if
1754 -- missing subunit is not within main unit, to simplify debugging.
1756 if Original_Operating_Mode = Generate_Code
1757 and then Unum = No_Unit
1759 Error_Msg_Unit_1 := Subunit_Name;
1761 Get_File_Name (Subunit_Name, Subunit => True);
1763 ("subunit$$ in file{ not found?!!", N);
1764 Subunits_Missing := True;
1767 -- Load_Unit may reset Compiler_State, since it may have been
1768 -- necessary to parse an additional units, so we make sure that
1769 -- we reset it to the Analyzing state.
1771 Compiler_State := Analyzing;
1773 if Unum /= No_Unit then
1774 if Debug_Flag_L then
1775 Write_Str ("*** Loaded subunit from stub. Analyze");
1780 Comp_Unit : constant Node_Id := Cunit (Unum);
1783 -- Check for child unit instead of subunit
1785 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1787 ("expected SEPARATE subunit, found child unit",
1788 Cunit_Entity (Unum));
1790 -- OK, we have a subunit
1793 -- Set corresponding stub (even if errors)
1795 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1797 -- Collect SCO information for loaded subunit if we are
1798 -- in the main unit).
1802 In_Extended_Main_Source_Unit
1803 (Cunit_Entity (Current_Sem_Unit))
1808 -- Analyze the unit if semantics active
1810 if not Fatal_Error (Unum) or else Try_Semantics then
1811 Analyze_Subunit (Comp_Unit);
1814 -- Set the library unit pointer in any case
1816 Set_Library_Unit (N, Comp_Unit);
1818 -- We update the version. Although we are not technically
1819 -- semantically dependent on the subunit, given our
1820 -- approach of macro substitution of subunits, it makes
1821 -- sense to include it in the version identification.
1823 Version_Update (Cunit (Main_Unit), Comp_Unit);
1829 -- The remaining case is when the subunit is not already loaded and we
1830 -- are not generating code. In this case we are just performing semantic
1831 -- analysis on the parent, and we are not interested in the subunit. For
1832 -- subprograms, analyze the stub as a body. For other entities the stub
1833 -- has already been marked as completed.
1838 end Analyze_Proper_Body;
1840 ----------------------------------
1841 -- Analyze_Protected_Body_Stub --
1842 ----------------------------------
1844 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1845 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1848 Check_Stub_Level (N);
1850 -- First occurrence of name may have been as an incomplete type
1852 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1853 Nam := Full_View (Nam);
1857 or else not Is_Protected_Type (Etype (Nam))
1859 Error_Msg_N ("missing specification for Protected body", N);
1861 Set_Scope (Defining_Entity (N), Current_Scope);
1862 Set_Has_Completion (Etype (Nam));
1863 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1864 Analyze_Proper_Body (N, Etype (Nam));
1866 end Analyze_Protected_Body_Stub;
1868 ----------------------------------
1869 -- Analyze_Subprogram_Body_Stub --
1870 ----------------------------------
1872 -- A subprogram body stub can appear with or without a previous spec. If
1873 -- there is one, then the analysis of the body will find it and verify
1874 -- conformance. The formals appearing in the specification of the stub play
1875 -- no role, except for requiring an additional conformance check. If there
1876 -- is no previous subprogram declaration, the stub acts as a spec, and
1877 -- provides the defining entity for the subprogram.
1879 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1883 Check_Stub_Level (N);
1885 -- Verify that the identifier for the stub is unique within this
1886 -- declarative part.
1888 if Nkind_In (Parent (N), N_Block_Statement,
1892 Decl := First (Declarations (Parent (N)));
1893 while Present (Decl)
1896 if Nkind (Decl) = N_Subprogram_Body_Stub
1897 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1898 Chars (Defining_Unit_Name (Specification (N))))
1900 Error_Msg_N ("identifier for stub is not unique", N);
1907 -- Treat stub as a body, which checks conformance if there is a previous
1908 -- declaration, or else introduces entity and its signature.
1910 Analyze_Subprogram_Body (N);
1911 Analyze_Proper_Body (N, Empty);
1912 end Analyze_Subprogram_Body_Stub;
1914 ---------------------
1915 -- Analyze_Subunit --
1916 ---------------------
1918 -- A subunit is compiled either by itself (for semantic checking) or as
1919 -- part of compiling the parent (for code generation). In either case, by
1920 -- the time we actually process the subunit, the parent has already been
1921 -- installed and analyzed. The node N is a compilation unit, whose context
1922 -- needs to be treated here, because we come directly here from the parent
1923 -- without calling Analyze_Compilation_Unit.
1925 -- The compilation context includes the explicit context of the subunit,
1926 -- and the context of the parent, together with the parent itself. In order
1927 -- to compile the current context, we remove the one inherited from the
1928 -- parent, in order to have a clean visibility table. We restore the parent
1929 -- context before analyzing the proper body itself. On exit, we remove only
1930 -- the explicit context of the subunit.
1932 procedure Analyze_Subunit (N : Node_Id) is
1933 Lib_Unit : constant Node_Id := Library_Unit (N);
1934 Par_Unit : constant Entity_Id := Current_Scope;
1936 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1937 Num_Scopes : Int := 0;
1938 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1939 Enclosing_Child : Entity_Id := Empty;
1940 Svg : constant Suppress_Array := Scope_Suppress;
1942 procedure Analyze_Subunit_Context;
1943 -- Capture names in use clauses of the subunit. This must be done before
1944 -- re-installing parent declarations, because items in the context must
1945 -- not be hidden by declarations local to the parent.
1947 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1948 -- Recursive procedure to restore scope of all ancestors of subunit,
1949 -- from outermost in. If parent is not a subunit, the call to install
1950 -- context installs context of spec and (if parent is a child unit) the
1951 -- context of its parents as well. It is confusing that parents should
1952 -- be treated differently in both cases, but the semantics are just not
1955 procedure Re_Install_Use_Clauses;
1956 -- As part of the removal of the parent scope, the use clauses are
1957 -- removed, to be reinstalled when the context of the subunit has been
1958 -- analyzed. Use clauses may also have been affected by the analysis of
1959 -- the context of the subunit, so they have to be applied again, to
1960 -- insure that the compilation environment of the rest of the parent
1961 -- unit is identical.
1963 procedure Remove_Scope;
1964 -- Remove current scope from scope stack, and preserve the list of use
1965 -- clauses in it, to be reinstalled after context is analyzed.
1967 -----------------------------
1968 -- Analyze_Subunit_Context --
1969 -----------------------------
1971 procedure Analyze_Subunit_Context is
1974 Unit_Name : Entity_Id;
1977 Analyze_Context (N);
1979 -- Make withed units immediately visible. If child unit, make the
1980 -- ultimate parent immediately visible.
1982 Item := First (Context_Items (N));
1983 while Present (Item) loop
1984 if Nkind (Item) = N_With_Clause then
1986 -- Protect frontend against previous errors in context clauses
1988 if Nkind (Name (Item)) /= N_Selected_Component then
1989 if Error_Posted (Item) then
1993 Unit_Name := Entity (Name (Item));
1994 while Is_Child_Unit (Unit_Name) loop
1995 Set_Is_Visible_Child_Unit (Unit_Name);
1996 Unit_Name := Scope (Unit_Name);
1999 if not Is_Immediately_Visible (Unit_Name) then
2000 Set_Is_Immediately_Visible (Unit_Name);
2001 Set_Context_Installed (Item);
2006 elsif Nkind (Item) = N_Use_Package_Clause then
2007 Nam := First (Names (Item));
2008 while Present (Nam) loop
2013 elsif Nkind (Item) = N_Use_Type_Clause then
2014 Nam := First (Subtype_Marks (Item));
2015 while Present (Nam) loop
2024 -- Reset visibility of withed units. They will be made visible again
2025 -- when we install the subunit context.
2027 Item := First (Context_Items (N));
2028 while Present (Item) loop
2029 if Nkind (Item) = N_With_Clause
2031 -- Protect frontend against previous errors in context clauses
2033 and then Nkind (Name (Item)) /= N_Selected_Component
2034 and then not Error_Posted (Item)
2036 Unit_Name := Entity (Name (Item));
2037 while Is_Child_Unit (Unit_Name) loop
2038 Set_Is_Visible_Child_Unit (Unit_Name, False);
2039 Unit_Name := Scope (Unit_Name);
2042 if Context_Installed (Item) then
2043 Set_Is_Immediately_Visible (Unit_Name, False);
2044 Set_Context_Installed (Item, False);
2050 end Analyze_Subunit_Context;
2052 ------------------------
2053 -- Re_Install_Parents --
2054 ------------------------
2056 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2060 if Nkind (Unit (L)) = N_Subunit then
2061 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2064 Install_Context (L);
2066 -- If the subunit occurs within a child unit, we must restore the
2067 -- immediate visibility of any siblings that may occur in context.
2069 if Present (Enclosing_Child) then
2070 Install_Siblings (Enclosing_Child, L);
2075 if Scop /= Par_Unit then
2076 Set_Is_Immediately_Visible (Scop);
2079 -- Make entities in scope visible again. For child units, restore
2080 -- visibility only if they are actually in context.
2082 E := First_Entity (Current_Scope);
2083 while Present (E) loop
2084 if not Is_Child_Unit (E)
2085 or else Is_Visible_Child_Unit (E)
2087 Set_Is_Immediately_Visible (E);
2093 -- A subunit appears within a body, and for a nested subunits all the
2094 -- parents are bodies. Restore full visibility of their private
2097 if Is_Package_Or_Generic_Package (Scop) then
2098 Set_In_Package_Body (Scop);
2099 Install_Private_Declarations (Scop);
2101 end Re_Install_Parents;
2103 ----------------------------
2104 -- Re_Install_Use_Clauses --
2105 ----------------------------
2107 procedure Re_Install_Use_Clauses is
2110 for J in reverse 1 .. Num_Scopes loop
2111 U := Use_Clauses (J);
2112 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2113 Install_Use_Clauses (U, Force_Installation => True);
2115 end Re_Install_Use_Clauses;
2121 procedure Remove_Scope is
2125 Num_Scopes := Num_Scopes + 1;
2126 Use_Clauses (Num_Scopes) :=
2127 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2129 E := First_Entity (Current_Scope);
2130 while Present (E) loop
2131 Set_Is_Immediately_Visible (E, False);
2135 if Is_Child_Unit (Current_Scope) then
2136 Enclosing_Child := Current_Scope;
2142 -- Start of processing for Analyze_Subunit
2147 Nam : Node_Id := Name (Unit (N));
2150 if Nkind (Nam) = N_Selected_Component then
2151 Nam := Selector_Name (Nam);
2154 Check_Identifier (Nam, Par_Unit);
2158 if not Is_Empty_List (Context_Items (N)) then
2160 -- Save current use clauses
2163 Remove_Context (Lib_Unit);
2165 -- Now remove parents and their context, including enclosing subunits
2166 -- and the outer parent body which is not a subunit.
2168 if Present (Lib_Spec) then
2169 Remove_Context (Lib_Spec);
2171 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2172 Lib_Spec := Library_Unit (Lib_Spec);
2174 Remove_Context (Lib_Spec);
2177 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2181 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2182 Remove_Context (Library_Unit (Lib_Spec));
2186 Set_Is_Immediately_Visible (Par_Unit, False);
2188 Analyze_Subunit_Context;
2190 Re_Install_Parents (Lib_Unit, Par_Unit);
2191 Set_Is_Immediately_Visible (Par_Unit);
2193 -- If the context includes a child unit of the parent of the subunit,
2194 -- the parent will have been removed from visibility, after compiling
2195 -- that cousin in the context. The visibility of the parent must be
2196 -- restored now. This also applies if the context includes another
2197 -- subunit of the same parent which in turn includes a child unit in
2200 if Is_Package_Or_Generic_Package (Par_Unit) then
2201 if not Is_Immediately_Visible (Par_Unit)
2202 or else (Present (First_Entity (Par_Unit))
2203 and then not Is_Immediately_Visible
2204 (First_Entity (Par_Unit)))
2206 Set_Is_Immediately_Visible (Par_Unit);
2207 Install_Visible_Declarations (Par_Unit);
2208 Install_Private_Declarations (Par_Unit);
2212 Re_Install_Use_Clauses;
2213 Install_Context (N);
2215 -- Restore state of suppress flags for current body
2217 Scope_Suppress := Svg;
2219 -- If the subunit is within a child unit, then siblings of any parent
2220 -- unit that appear in the context clause of the subunit must also be
2221 -- made immediately visible.
2223 if Present (Enclosing_Child) then
2224 Install_Siblings (Enclosing_Child, N);
2228 Analyze (Proper_Body (Unit (N)));
2231 -- The subunit may contain a with_clause on a sibling of some ancestor.
2232 -- Removing the context will remove from visibility those ancestor child
2233 -- units, which must be restored to the visibility they have in the
2236 if Present (Enclosing_Child) then
2242 and then Is_Child_Unit (C)
2244 Set_Is_Immediately_Visible (C);
2245 Set_Is_Visible_Child_Unit (C);
2250 end Analyze_Subunit;
2252 ----------------------------
2253 -- Analyze_Task_Body_Stub --
2254 ----------------------------
2256 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2257 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2258 Loc : constant Source_Ptr := Sloc (N);
2261 Check_Stub_Level (N);
2263 -- First occurrence of name may have been as an incomplete type
2265 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2266 Nam := Full_View (Nam);
2269 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2270 Error_Msg_N ("missing specification for task body", N);
2272 Set_Scope (Defining_Entity (N), Current_Scope);
2273 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2275 -- Check for duplicate stub, if so give message and terminate
2277 if Has_Completion (Etype (Nam)) then
2278 Error_Msg_N ("duplicate stub for task", N);
2281 Set_Has_Completion (Etype (Nam));
2284 Analyze_Proper_Body (N, Etype (Nam));
2286 -- Set elaboration flag to indicate that entity is callable. This
2287 -- cannot be done in the expansion of the body itself, because the
2288 -- proper body is not in a declarative part. This is only done if
2289 -- expansion is active, because the context may be generic and the
2290 -- flag not defined yet.
2292 if Expander_Active then
2294 Make_Assignment_Statement (Loc,
2296 Make_Identifier (Loc,
2297 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2298 Expression => New_Reference_To (Standard_True, Loc)));
2301 end Analyze_Task_Body_Stub;
2303 -------------------------
2304 -- Analyze_With_Clause --
2305 -------------------------
2307 -- Analyze the declaration of a unit in a with clause. At end, label the
2308 -- with clause with the defining entity for the unit.
2310 procedure Analyze_With_Clause (N : Node_Id) is
2312 -- Retrieve the original kind of the unit node, before analysis. If it
2313 -- is a subprogram instantiation, its analysis below will rewrite the
2314 -- node as the declaration of the wrapper package. If the same
2315 -- instantiation appears indirectly elsewhere in the context, it will
2316 -- have been analyzed already.
2318 Unit_Kind : constant Node_Kind :=
2319 Nkind (Original_Node (Unit (Library_Unit (N))));
2320 Nam : constant Node_Id := Name (N);
2322 Par_Name : Entity_Id;
2327 -- Set True if the unit currently being compiled is an internal unit
2329 Save_Style_Check : constant Boolean := Opt.Style_Check;
2330 Save_C_Restrict : Save_Cunit_Boolean_Restrictions;
2333 U := Unit (Library_Unit (N));
2335 -- If this is an internal unit which is a renaming, then this is a
2336 -- violation of No_Obsolescent_Features.
2338 -- Note: this is not quite right if the user defines one of these units
2339 -- himself, but that's a marginal case, and fixing it is hard ???
2341 if Restriction_Check_Required (No_Obsolescent_Features) then
2343 F : constant File_Name_Type :=
2344 Unit_File_Name (Get_Source_Unit (U));
2346 if Is_Predefined_File_Name (F, Renamings_Included => True)
2348 Is_Predefined_File_Name (F, Renamings_Included => False)
2350 Check_Restriction (No_Obsolescent_Features, N);
2355 -- Save current restriction set, does not apply to with'ed unit
2357 Save_C_Restrict := Cunit_Boolean_Restrictions_Save;
2359 -- Several actions are skipped for dummy packages (those supplied for
2360 -- with's where no matching file could be found). Such packages are
2361 -- identified by the Sloc value being set to No_Location.
2363 if Limited_Present (N) then
2365 -- Ada 2005 (AI-50217): Build visibility structures but do not
2366 -- analyze the unit.
2368 if Sloc (U) /= No_Location then
2369 Build_Limited_Views (N);
2375 -- We reset ordinary style checking during the analysis of a with'ed
2376 -- unit, but we do NOT reset GNAT special analysis mode (the latter
2377 -- definitely *does* apply to with'ed units).
2379 if not GNAT_Mode then
2380 Style_Check := False;
2383 -- If the library unit is a predefined unit, and we are in high
2384 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2385 -- for the analysis of the with'ed unit. This mode does not prevent
2386 -- explicit with'ing of run-time units.
2388 if Configurable_Run_Time_Mode
2389 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (U)))
2391 Configurable_Run_Time_Mode := False;
2392 Semantics (Library_Unit (N));
2393 Configurable_Run_Time_Mode := True;
2396 Semantics (Library_Unit (N));
2399 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2401 if Sloc (U) /= No_Location then
2403 -- Check restrictions, except that we skip the check if this is an
2404 -- internal unit unless we are compiling the internal unit as the
2405 -- main unit. We also skip this for dummy packages.
2407 Check_Restriction_No_Dependence (Nam, N);
2409 if not Intunit or else Current_Sem_Unit = Main_Unit then
2410 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2413 -- Deal with special case of GNAT.Current_Exceptions which interacts
2414 -- with the optimization of local raise statements into gotos.
2416 if Nkind (Nam) = N_Selected_Component
2417 and then Nkind (Prefix (Nam)) = N_Identifier
2418 and then Chars (Prefix (Nam)) = Name_Gnat
2419 and then (Chars (Selector_Name (Nam)) = Name_Most_Recent_Exception
2421 Chars (Selector_Name (Nam)) = Name_Exception_Traces)
2423 Check_Restriction (No_Exception_Propagation, N);
2424 Special_Exception_Package_Used := True;
2427 -- Check for inappropriate with of internal implementation unit if we
2428 -- are not compiling an internal unit. We do not issue this message
2429 -- for implicit with's generated by the compiler itself.
2431 if Implementation_Unit_Warnings
2432 and then not Intunit
2433 and then not Implicit_With (N)
2436 U_Kind : constant Kind_Of_Unit :=
2437 Get_Kind_Of_Unit (Get_Source_Unit (U));
2440 if U_Kind = Implementation_Unit then
2441 Error_Msg_F ("& is an internal 'G'N'A'T unit?", Name (N));
2443 -- Add alternative name if available, otherwise issue a
2444 -- general warning message.
2446 if Error_Msg_Strlen /= 0 then
2447 Error_Msg_F ("\use ""~"" instead", Name (N));
2450 ("\use of this unit is non-portable " &
2451 "and version-dependent?", Name (N));
2454 elsif U_Kind = Ada_2005_Unit
2455 and then Ada_Version < Ada_2005
2456 and then Warn_On_Ada_2005_Compatibility
2458 Error_Msg_N ("& is an Ada 2005 unit?", Name (N));
2460 elsif U_Kind = Ada_2012_Unit
2461 and then Ada_Version < Ada_2012
2462 and then Warn_On_Ada_2012_Compatibility
2464 Error_Msg_N ("& is an Ada 2012 unit?", Name (N));
2470 -- Semantic analysis of a generic unit is performed on a copy of
2471 -- the original tree. Retrieve the entity on which semantic info
2472 -- actually appears.
2474 if Unit_Kind in N_Generic_Declaration then
2475 E_Name := Defining_Entity (U);
2477 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2478 -- the case of an instantiation, semantic analysis above will have
2479 -- replaced the unit by its instantiated version. If the instance body
2480 -- has been generated, the instance now denotes the body entity. For
2481 -- visibility purposes we need the entity of its spec.
2483 elsif (Unit_Kind = N_Package_Instantiation
2484 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2485 N_Package_Instantiation)
2486 and then Nkind (U) = N_Package_Body
2488 E_Name := Corresponding_Spec (U);
2490 elsif Unit_Kind = N_Package_Instantiation
2491 and then Nkind (U) = N_Package_Instantiation
2492 and then Present (Instance_Spec (U))
2494 -- If the instance has not been rewritten as a package declaration,
2495 -- then it appeared already in a previous with clause. Retrieve
2496 -- the entity from the previous instance.
2498 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2500 elsif Unit_Kind in N_Subprogram_Instantiation then
2502 -- The visible subprogram is created during instantiation, and is
2503 -- an attribute of the wrapper package. We retrieve the wrapper
2504 -- package directly from the instantiation node. If the instance
2505 -- is inlined the unit is still an instantiation. Otherwise it has
2506 -- been rewritten as the declaration of the wrapper itself.
2508 if Nkind (U) in N_Subprogram_Instantiation then
2511 (Defining_Entity (Specification (Instance_Spec (U))));
2513 E_Name := Related_Instance (Defining_Entity (U));
2516 elsif Unit_Kind = N_Package_Renaming_Declaration
2517 or else Unit_Kind in N_Generic_Renaming_Declaration
2519 E_Name := Defining_Entity (U);
2521 elsif Unit_Kind = N_Subprogram_Body
2522 and then Nkind (Name (N)) = N_Selected_Component
2523 and then not Acts_As_Spec (Library_Unit (N))
2525 -- For a child unit that has no spec, one has been created and
2526 -- analyzed. The entity required is that of the spec.
2528 E_Name := Corresponding_Spec (U);
2531 E_Name := Defining_Entity (U);
2534 if Nkind (Name (N)) = N_Selected_Component then
2536 -- Child unit in a with clause
2538 Change_Selected_Component_To_Expanded_Name (Name (N));
2541 -- Restore style checks and restrictions
2543 Style_Check := Save_Style_Check;
2544 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
2546 -- Record the reference, but do NOT set the unit as referenced, we want
2547 -- to consider the unit as unreferenced if this is the only reference
2550 Set_Entity_With_Style_Check (Name (N), E_Name);
2551 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2553 -- Generate references and check No_Dependence restriction for parents
2555 if Is_Child_Unit (E_Name) then
2556 Pref := Prefix (Name (N));
2557 Par_Name := Scope (E_Name);
2558 while Nkind (Pref) = N_Selected_Component loop
2559 Change_Selected_Component_To_Expanded_Name (Pref);
2561 if Present (Entity (Selector_Name (Pref)))
2563 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2564 and then Entity (Selector_Name (Pref)) /= Par_Name
2566 -- The prefix is a child unit that denotes a renaming declaration.
2567 -- Replace the prefix directly with the renamed unit, because the
2568 -- rest of the prefix is irrelevant to the visibility of the real
2571 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2575 Set_Entity_With_Style_Check (Pref, Par_Name);
2577 Generate_Reference (Par_Name, Pref);
2578 Check_Restriction_No_Dependence (Pref, N);
2579 Pref := Prefix (Pref);
2581 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2582 -- is set to Standard_Standard, and no attempt should be made to
2583 -- further unwind scopes.
2585 if Par_Name /= Standard_Standard then
2586 Par_Name := Scope (Par_Name);
2590 if Present (Entity (Pref))
2591 and then not Analyzed (Parent (Parent (Entity (Pref))))
2593 -- If the entity is set without its unit being compiled, the
2594 -- original parent is a renaming, and Par_Name is the renamed
2595 -- entity. For visibility purposes, we need the original entity,
2596 -- which must be analyzed now because Load_Unit directly retrieves
2597 -- the renamed unit, and the renaming declaration itself has not
2600 Analyze (Parent (Parent (Entity (Pref))));
2601 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2602 Par_Name := Entity (Pref);
2605 -- Guard against missing or misspelled child units
2607 if Present (Par_Name) then
2608 Set_Entity_With_Style_Check (Pref, Par_Name);
2609 Generate_Reference (Par_Name, Pref);
2612 Set_Name (N, Make_Null (Sloc (N)));
2617 -- If the withed unit is System, and a system extension pragma is
2618 -- present, compile the extension now, rather than waiting for a
2619 -- visibility check on a specific entity.
2621 if Chars (E_Name) = Name_System
2622 and then Scope (E_Name) = Standard_Standard
2623 and then Present (System_Extend_Unit)
2624 and then Present_System_Aux (N)
2626 -- If the extension is not present, an error will have been emitted
2631 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2632 -- to private_with units; they will be made visible later (just before
2633 -- the private part is analyzed)
2635 if Private_Present (N) then
2636 Set_Is_Immediately_Visible (E_Name, False);
2638 end Analyze_With_Clause;
2640 ------------------------------
2641 -- Check_Private_Child_Unit --
2642 ------------------------------
2644 procedure Check_Private_Child_Unit (N : Node_Id) is
2645 Lib_Unit : constant Node_Id := Unit (N);
2647 Curr_Unit : Entity_Id;
2648 Sub_Parent : Node_Id;
2649 Priv_Child : Entity_Id;
2650 Par_Lib : Entity_Id;
2653 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2654 -- Returns true if and only if the library unit is declared with
2655 -- an explicit designation of private.
2657 -----------------------------
2658 -- Is_Private_Library_Unit --
2659 -----------------------------
2661 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2662 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2665 return Private_Present (Comp_Unit);
2666 end Is_Private_Library_Unit;
2668 -- Start of processing for Check_Private_Child_Unit
2671 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2672 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2673 Par_Lib := Curr_Unit;
2675 elsif Nkind (Lib_Unit) = N_Subunit then
2677 -- The parent is itself a body. The parent entity is to be found in
2678 -- the corresponding spec.
2680 Sub_Parent := Library_Unit (N);
2681 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2683 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2684 -- enclosing body, retrieve the spec entity which is the proper
2685 -- ancestor we need for the following tests.
2687 if Ekind (Curr_Unit) = E_Package_Body then
2688 Curr_Unit := Spec_Entity (Curr_Unit);
2691 Par_Lib := Curr_Unit;
2694 Curr_Unit := Defining_Entity (Lib_Unit);
2696 Par_Lib := Curr_Unit;
2697 Par_Spec := Parent_Spec (Lib_Unit);
2699 if No (Par_Spec) then
2702 Par_Lib := Defining_Entity (Unit (Par_Spec));
2706 -- Loop through context items
2708 Item := First (Context_Items (N));
2709 while Present (Item) loop
2711 -- Ada 2005 (AI-262): Allow private_with of a private child package
2712 -- in public siblings
2714 if Nkind (Item) = N_With_Clause
2715 and then not Implicit_With (Item)
2716 and then not Limited_Present (Item)
2717 and then Is_Private_Descendant (Entity (Name (Item)))
2719 Priv_Child := Entity (Name (Item));
2722 Curr_Parent : Entity_Id := Par_Lib;
2723 Child_Parent : Entity_Id := Scope (Priv_Child);
2724 Prv_Ancestor : Entity_Id := Child_Parent;
2725 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2728 -- If the child unit is a public child then locate the nearest
2729 -- private ancestor. Child_Parent will then be set to the
2730 -- parent of that ancestor.
2732 if not Is_Private_Library_Unit (Priv_Child) then
2733 while Present (Prv_Ancestor)
2734 and then not Is_Private_Library_Unit (Prv_Ancestor)
2736 Prv_Ancestor := Scope (Prv_Ancestor);
2739 if Present (Prv_Ancestor) then
2740 Child_Parent := Scope (Prv_Ancestor);
2744 while Present (Curr_Parent)
2745 and then Curr_Parent /= Standard_Standard
2746 and then Curr_Parent /= Child_Parent
2749 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2750 Curr_Parent := Scope (Curr_Parent);
2753 if No (Curr_Parent) then
2754 Curr_Parent := Standard_Standard;
2757 if Curr_Parent /= Child_Parent then
2758 if Ekind (Priv_Child) = E_Generic_Package
2759 and then Chars (Priv_Child) in Text_IO_Package_Name
2760 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2763 ("& is a nested package, not a compilation unit",
2764 Name (Item), Priv_Child);
2768 ("unit in with clause is private child unit!", Item);
2770 ("\current unit must also have parent&!",
2771 Item, Child_Parent);
2775 or else Private_Present (Item)
2776 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2777 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2778 and then not Acts_As_Spec (Parent (Lib_Unit)))
2784 ("current unit must also be private descendant of&",
2785 Item, Child_Parent);
2793 end Check_Private_Child_Unit;
2795 ----------------------
2796 -- Check_Stub_Level --
2797 ----------------------
2799 procedure Check_Stub_Level (N : Node_Id) is
2800 Par : constant Node_Id := Parent (N);
2801 Kind : constant Node_Kind := Nkind (Par);
2804 if Nkind_In (Kind, N_Package_Body,
2808 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
2812 -- In an instance, a missing stub appears at any level. A warning
2813 -- message will have been emitted already for the missing file.
2815 elsif not In_Instance then
2816 Error_Msg_N ("stub cannot appear in an inner scope", N);
2818 elsif Expander_Active then
2819 Error_Msg_N ("missing proper body", N);
2821 end Check_Stub_Level;
2823 ------------------------
2824 -- Expand_With_Clause --
2825 ------------------------
2827 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2828 Loc : constant Source_Ptr := Sloc (Nam);
2829 Ent : constant Entity_Id := Entity (Nam);
2833 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2834 -- Build name to be used in implicit with_clause. In most cases this
2835 -- is the source name, but if renamings are present we must make the
2836 -- original unit visible, not the one it renames. The entity in the
2837 -- with clause is the renamed unit, but the identifier is the one from
2838 -- the source, which allows us to recover the unit renaming.
2840 ---------------------
2841 -- Build_Unit_Name --
2842 ---------------------
2844 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2846 Renaming : Entity_Id;
2850 if Nkind (Nam) = N_Identifier then
2852 -- If the parent unit P in the name of the with_clause for P.Q is
2853 -- a renaming of package R, then the entity of the parent is set
2854 -- to R, but the identifier retains Chars (P) to be consistent
2855 -- with the source (see details in lib-load). However the implicit
2856 -- with_clause for the parent must make the entity for P visible,
2857 -- because P.Q may be used as a prefix within the current unit.
2858 -- The entity for P is the current_entity with that name, because
2859 -- the package renaming declaration for it has just been analyzed.
2860 -- Note that this case can only happen if P.Q has already appeared
2861 -- in a previous with_clause in a related unit, such as the
2862 -- library body of the current unit.
2864 if Chars (Nam) /= Chars (Entity (Nam)) then
2865 Renaming := Current_Entity (Nam);
2866 pragma Assert (Renamed_Entity (Renaming) = Entity (Nam));
2867 return New_Occurrence_Of (Renaming, Loc);
2870 return New_Occurrence_Of (Entity (Nam), Loc);
2874 Ent := Entity (Nam);
2876 if Present (Entity (Selector_Name (Nam)))
2877 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
2879 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
2880 = N_Package_Renaming_Declaration
2882 -- The name in the with_clause is of the form A.B.C, and B is
2883 -- given by a renaming declaration. In that case we may not
2884 -- have analyzed the unit for B, but replaced it directly in
2885 -- lib-load with the unit it renames. We have to make A.B
2886 -- visible, so analyze the declaration for B now, in case it
2887 -- has not been done yet.
2889 Ent := Entity (Selector_Name (Nam));
2892 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
2896 Make_Expanded_Name (Loc,
2897 Chars => Chars (Entity (Nam)),
2898 Prefix => Build_Unit_Name (Prefix (Nam)),
2899 Selector_Name => New_Occurrence_Of (Ent, Loc));
2900 Set_Entity (Result, Ent);
2903 end Build_Unit_Name;
2905 -- Start of processing for Expand_With_Clause
2908 New_Nodes_OK := New_Nodes_OK + 1;
2910 Make_With_Clause (Loc,
2911 Name => Build_Unit_Name (Nam));
2913 P := Parent (Unit_Declaration_Node (Ent));
2914 Set_Library_Unit (Withn, P);
2915 Set_Corresponding_Spec (Withn, Ent);
2916 Set_First_Name (Withn, True);
2917 Set_Implicit_With (Withn, True);
2919 -- If the unit is a package declaration, a private_with_clause on a
2920 -- child unit implies the implicit with on the parent is also private.
2922 if Nkind (Unit (N)) = N_Package_Declaration then
2923 Set_Private_Present (Withn, Private_Present (Item));
2926 Prepend (Withn, Context_Items (N));
2927 Mark_Rewrite_Insertion (Withn);
2928 Install_Withed_Unit (Withn);
2930 if Nkind (Nam) = N_Expanded_Name then
2931 Expand_With_Clause (Item, Prefix (Nam), N);
2934 New_Nodes_OK := New_Nodes_OK - 1;
2935 end Expand_With_Clause;
2937 -----------------------
2938 -- Get_Parent_Entity --
2939 -----------------------
2941 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2943 if Nkind (Unit) = N_Package_Body
2944 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
2946 return Defining_Entity
2947 (Specification (Instance_Spec (Original_Node (Unit))));
2948 elsif Nkind (Unit) = N_Package_Instantiation then
2949 return Defining_Entity (Specification (Instance_Spec (Unit)));
2951 return Defining_Entity (Unit);
2953 end Get_Parent_Entity;
2955 ---------------------
2956 -- Has_With_Clause --
2957 ---------------------
2959 function Has_With_Clause
2962 Is_Limited : Boolean := False) return Boolean
2966 function Named_Unit (Clause : Node_Id) return Entity_Id;
2967 -- Return the entity for the unit named in a [limited] with clause
2973 function Named_Unit (Clause : Node_Id) return Entity_Id is
2975 if Nkind (Name (Clause)) = N_Selected_Component then
2976 return Entity (Selector_Name (Name (Clause)));
2978 return Entity (Name (Clause));
2982 -- Start of processing for Has_With_Clause
2985 if Present (Context_Items (C_Unit)) then
2986 Item := First (Context_Items (C_Unit));
2987 while Present (Item) loop
2988 if Nkind (Item) = N_With_Clause
2989 and then Limited_Present (Item) = Is_Limited
2990 and then Named_Unit (Item) = Pack
3000 end Has_With_Clause;
3002 -----------------------------
3003 -- Implicit_With_On_Parent --
3004 -----------------------------
3006 procedure Implicit_With_On_Parent
3007 (Child_Unit : Node_Id;
3010 Loc : constant Source_Ptr := Sloc (N);
3011 P : constant Node_Id := Parent_Spec (Child_Unit);
3012 P_Unit : Node_Id := Unit (P);
3013 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3016 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3017 -- Build prefix of child unit name. Recurse if needed
3019 function Build_Unit_Name return Node_Id;
3020 -- If the unit is a child unit, build qualified name with all ancestors
3022 -------------------------
3023 -- Build_Ancestor_Name --
3024 -------------------------
3026 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3027 P_Ref : constant Node_Id :=
3028 New_Reference_To (Defining_Entity (P), Loc);
3029 P_Spec : Node_Id := P;
3032 -- Ancestor may have been rewritten as a package body. Retrieve
3033 -- the original spec to trace earlier ancestors.
3035 if Nkind (P) = N_Package_Body
3036 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3038 P_Spec := Original_Node (P);
3041 if No (Parent_Spec (P_Spec)) then
3045 Make_Selected_Component (Loc,
3046 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3047 Selector_Name => P_Ref);
3049 end Build_Ancestor_Name;
3051 ---------------------
3052 -- Build_Unit_Name --
3053 ---------------------
3055 function Build_Unit_Name return Node_Id is
3059 if No (Parent_Spec (P_Unit)) then
3060 return New_Reference_To (P_Name, Loc);
3064 Make_Expanded_Name (Loc,
3065 Chars => Chars (P_Name),
3066 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3067 Selector_Name => New_Reference_To (P_Name, Loc));
3068 Set_Entity (Result, P_Name);
3071 end Build_Unit_Name;
3073 -- Start of processing for Implicit_With_On_Parent
3076 -- The unit of the current compilation may be a package body that
3077 -- replaces an instance node. In this case we need the original instance
3078 -- node to construct the proper parent name.
3080 if Nkind (P_Unit) = N_Package_Body
3081 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3083 P_Unit := Original_Node (P_Unit);
3086 -- We add the implicit with if the child unit is the current unit being
3087 -- compiled. If the current unit is a body, we do not want to add an
3088 -- implicit_with a second time to the corresponding spec.
3090 if Nkind (Child_Unit) = N_Package_Declaration
3091 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3096 New_Nodes_OK := New_Nodes_OK + 1;
3097 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3099 Set_Library_Unit (Withn, P);
3100 Set_Corresponding_Spec (Withn, P_Name);
3101 Set_First_Name (Withn, True);
3102 Set_Implicit_With (Withn, True);
3104 -- Node is placed at the beginning of the context items, so that
3105 -- subsequent use clauses on the parent can be validated.
3107 Prepend (Withn, Context_Items (N));
3108 Mark_Rewrite_Insertion (Withn);
3109 Install_Withed_Unit (Withn);
3111 if Is_Child_Spec (P_Unit) then
3112 Implicit_With_On_Parent (P_Unit, N);
3115 New_Nodes_OK := New_Nodes_OK - 1;
3116 end Implicit_With_On_Parent;
3122 function In_Chain (E : Entity_Id) return Boolean is
3126 H := Current_Entity (E);
3127 while Present (H) loop
3138 ---------------------
3139 -- Install_Context --
3140 ---------------------
3142 procedure Install_Context (N : Node_Id) is
3143 Lib_Unit : constant Node_Id := Unit (N);
3146 Install_Context_Clauses (N);
3148 if Is_Child_Spec (Lib_Unit) then
3149 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3152 Install_Limited_Context_Clauses (N);
3153 end Install_Context;
3155 -----------------------------
3156 -- Install_Context_Clauses --
3157 -----------------------------
3159 procedure Install_Context_Clauses (N : Node_Id) is
3160 Lib_Unit : constant Node_Id := Unit (N);
3162 Uname_Node : Entity_Id;
3163 Check_Private : Boolean := False;
3164 Decl_Node : Node_Id;
3165 Lib_Parent : Entity_Id;
3168 -- First skip configuration pragmas at the start of the context. They
3169 -- are not technically part of the context clause, but that's where the
3170 -- parser puts them. Note they were analyzed in Analyze_Context.
3172 Item := First (Context_Items (N));
3173 while Present (Item)
3174 and then Nkind (Item) = N_Pragma
3175 and then Pragma_Name (Item) in Configuration_Pragma_Names
3180 -- Loop through the actual context clause items. We process everything
3181 -- except Limited_With clauses in this routine. Limited_With clauses
3182 -- are separately installed (see Install_Limited_Context_Clauses).
3184 while Present (Item) loop
3186 -- Case of explicit WITH clause
3188 if Nkind (Item) = N_With_Clause
3189 and then not Implicit_With (Item)
3191 if Limited_Present (Item) then
3193 -- Limited withed units will be installed later
3197 -- If Name (Item) is not an entity name, something is wrong, and
3198 -- this will be detected in due course, for now ignore the item
3200 elsif not Is_Entity_Name (Name (Item)) then
3203 elsif No (Entity (Name (Item))) then
3204 Set_Entity (Name (Item), Any_Id);
3208 Uname_Node := Entity (Name (Item));
3210 if Is_Private_Descendant (Uname_Node) then
3211 Check_Private := True;
3214 Install_Withed_Unit (Item);
3216 Decl_Node := Unit_Declaration_Node (Uname_Node);
3218 -- If the unit is a subprogram instance, it appears nested within
3219 -- a package that carries the parent information.
3221 if Is_Generic_Instance (Uname_Node)
3222 and then Ekind (Uname_Node) /= E_Package
3224 Decl_Node := Parent (Parent (Decl_Node));
3227 if Is_Child_Spec (Decl_Node) then
3228 if Nkind (Name (Item)) = N_Expanded_Name then
3229 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3231 -- If not an expanded name, the child unit must be a
3232 -- renaming, nothing to do.
3237 elsif Nkind (Decl_Node) = N_Subprogram_Body
3238 and then not Acts_As_Spec (Parent (Decl_Node))
3239 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3241 Implicit_With_On_Parent
3242 (Unit (Library_Unit (Parent (Decl_Node))), N);
3245 -- Check license conditions unless this is a dummy unit
3247 if Sloc (Library_Unit (Item)) /= No_Location then
3248 License_Check : declare
3249 Withu : constant Unit_Number_Type :=
3250 Get_Source_Unit (Library_Unit (Item));
3251 Withl : constant License_Type :=
3252 License (Source_Index (Withu));
3253 Unitl : constant License_Type :=
3254 License (Source_Index (Current_Sem_Unit));
3256 procedure License_Error;
3257 -- Signal error of bad license
3263 procedure License_Error is
3266 ("?license of with'ed unit & may be inconsistent",
3270 -- Start of processing for License_Check
3273 -- Exclude license check if withed unit is an internal unit.
3274 -- This situation arises e.g. with the GPL version of GNAT.
3276 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3279 -- Otherwise check various cases
3291 if Withl = Restricted then
3295 when Modified_GPL =>
3296 if Withl = Restricted or else Withl = GPL then
3300 when Unrestricted =>
3307 -- Case of USE PACKAGE clause
3309 elsif Nkind (Item) = N_Use_Package_Clause then
3310 Analyze_Use_Package (Item);
3312 -- Case of USE TYPE clause
3314 elsif Nkind (Item) = N_Use_Type_Clause then
3315 Analyze_Use_Type (Item);
3319 elsif Nkind (Item) = N_Pragma then
3327 if Is_Child_Spec (Lib_Unit) then
3329 -- The unit also has implicit with_clauses on its own parents
3331 if No (Context_Items (N)) then
3332 Set_Context_Items (N, New_List);
3335 Implicit_With_On_Parent (Lib_Unit, N);
3338 -- If the unit is a body, the context of the specification must also
3339 -- be installed. That includes private with_clauses in that context.
3341 if Nkind (Lib_Unit) = N_Package_Body
3342 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3343 and then not Acts_As_Spec (N))
3345 Install_Context (Library_Unit (N));
3347 -- Only install private with-clauses of a spec that comes from
3348 -- source, excluding specs created for a subprogram body that is
3351 if Comes_From_Source (Library_Unit (N)) then
3352 Install_Private_With_Clauses
3353 (Defining_Entity (Unit (Library_Unit (N))));
3356 if Is_Child_Spec (Unit (Library_Unit (N))) then
3358 -- If the unit is the body of a public child unit, the private
3359 -- declarations of the parent must be made visible. If the child
3360 -- unit is private, the private declarations have been installed
3361 -- already in the call to Install_Parents for the spec. Installing
3362 -- private declarations must be done for all ancestors of public
3363 -- child units. In addition, sibling units mentioned in the
3364 -- context clause of the body are directly visible.
3372 Lib_Spec := Unit (Library_Unit (N));
3373 while Is_Child_Spec (Lib_Spec) loop
3374 P := Unit (Parent_Spec (Lib_Spec));
3375 P_Name := Defining_Entity (P);
3377 if not (Private_Present (Parent (Lib_Spec)))
3378 and then not In_Private_Part (P_Name)
3380 Install_Private_Declarations (P_Name);
3381 Install_Private_With_Clauses (P_Name);
3382 Set_Use (Private_Declarations (Specification (P)));
3390 -- For a package body, children in context are immediately visible
3392 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3395 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3396 N_Generic_Subprogram_Declaration,
3397 N_Package_Declaration,
3398 N_Subprogram_Declaration)
3400 if Is_Child_Spec (Lib_Unit) then
3401 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3402 Set_Is_Private_Descendant
3403 (Defining_Entity (Lib_Unit),
3404 Is_Private_Descendant (Lib_Parent)
3405 or else Private_Present (Parent (Lib_Unit)));
3408 Set_Is_Private_Descendant
3409 (Defining_Entity (Lib_Unit),
3410 Private_Present (Parent (Lib_Unit)));
3414 if Check_Private then
3415 Check_Private_Child_Unit (N);
3417 end Install_Context_Clauses;
3419 -------------------------------------
3420 -- Install_Limited_Context_Clauses --
3421 -------------------------------------
3423 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3426 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3427 -- Check that the unlimited view of a given compilation_unit is not
3428 -- already visible through "use + renamings".
3430 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3431 -- Check that if a limited_with clause of a given compilation_unit
3432 -- mentions a descendant of a private child of some library unit, then
3433 -- the given compilation_unit shall be the declaration of a private
3434 -- descendant of that library unit, or a public descendant of such. The
3435 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3436 -- use entities on the limited with_clauses because their units have not
3437 -- been analyzed, so we have to climb the tree of ancestors looking for
3438 -- private keywords.
3440 procedure Expand_Limited_With_Clause
3441 (Comp_Unit : Node_Id;
3444 -- If a child unit appears in a limited_with clause, there are implicit
3445 -- limited_with clauses on all parents that are not already visible
3446 -- through a regular with clause. This procedure creates the implicit
3447 -- limited with_clauses for the parents and loads the corresponding
3448 -- units. The shadow entities are created when the inserted clause is
3449 -- analyzed. Implements Ada 2005 (AI-50217).
3451 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
3452 -- When compiling a unit Q descended from some parent unit P, a limited
3453 -- with_clause in the context of P that names some other ancestor of Q
3454 -- must not be installed because the ancestor is immediately visible.
3456 ---------------------
3457 -- Check_Renamings --
3458 ---------------------
3460 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3469 pragma Assert (Nkind (W) = N_With_Clause);
3471 -- Protect the frontend against previous critical errors
3473 case Nkind (Unit (Library_Unit (W))) is
3474 when N_Subprogram_Declaration |
3475 N_Package_Declaration |
3476 N_Generic_Subprogram_Declaration |
3477 N_Generic_Package_Declaration =>
3484 -- Check "use + renamings"
3486 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3487 Spec := Specification (Unit (P));
3489 Item := First (Visible_Declarations (Spec));
3490 while Present (Item) loop
3492 -- Look only at use package clauses
3494 if Nkind (Item) = N_Use_Package_Clause then
3496 -- Traverse the list of packages
3498 Nam := First (Names (Item));
3499 while Present (Nam) loop
3502 pragma Assert (Present (Parent (E)));
3504 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3505 and then Renamed_Entity (E) = WEnt
3507 -- The unlimited view is visible through use clause and
3508 -- renamings. There is no need to generate the error
3509 -- message here because Is_Visible_Through_Renamings
3510 -- takes care of generating the precise error message.
3514 elsif Nkind (Parent (E)) = N_Package_Specification then
3516 -- The use clause may refer to a local package.
3517 -- Check all the enclosing scopes.
3520 while E2 /= Standard_Standard
3528 ("unlimited view visible through use clause ", W);
3540 -- Recursive call to check all the ancestors
3542 if Is_Child_Spec (Unit (P)) then
3543 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3545 end Check_Renamings;
3547 ---------------------------------------
3548 -- Check_Private_Limited_Withed_Unit --
3549 ---------------------------------------
3551 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3552 Curr_Parent : Node_Id;
3553 Child_Parent : Node_Id;
3554 Curr_Private : Boolean;
3557 -- Compilation unit of the parent of the withed library unit
3559 Child_Parent := Library_Unit (Item);
3561 -- If the child unit is a public child, then locate its nearest
3562 -- private ancestor, if any, then Child_Parent will then be set to
3563 -- the parent of that ancestor.
3565 if not Private_Present (Library_Unit (Item)) then
3566 while Present (Child_Parent)
3567 and then not Private_Present (Child_Parent)
3569 Child_Parent := Parent_Spec (Unit (Child_Parent));
3572 if No (Child_Parent) then
3577 Child_Parent := Parent_Spec (Unit (Child_Parent));
3579 -- Traverse all the ancestors of the current compilation unit to
3580 -- check if it is a descendant of named library unit.
3582 Curr_Parent := Parent (Item);
3583 Curr_Private := Private_Present (Curr_Parent);
3585 while Present (Parent_Spec (Unit (Curr_Parent)))
3586 and then Curr_Parent /= Child_Parent
3588 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3589 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3592 if Curr_Parent /= Child_Parent then
3594 ("unit in with clause is private child unit!", Item);
3596 ("\current unit must also have parent&!",
3597 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3599 elsif Private_Present (Parent (Item))
3600 or else Curr_Private
3601 or else Private_Present (Item)
3602 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3606 -- Current unit is private, of descendant of a private unit
3612 ("current unit must also be private descendant of&",
3613 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3615 end Check_Private_Limited_Withed_Unit;
3617 --------------------------------
3618 -- Expand_Limited_With_Clause --
3619 --------------------------------
3621 procedure Expand_Limited_With_Clause
3622 (Comp_Unit : Node_Id;
3626 Loc : constant Source_Ptr := Sloc (Nam);
3627 Unum : Unit_Number_Type;
3630 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3631 -- Returns true if the context already includes a with_clause for
3632 -- this unit. If the with_clause is non-limited, the unit is fully
3633 -- visible and an implicit limited_with should not be created. If
3634 -- there is already a limited_with clause for W, a second one is
3635 -- simply redundant.
3637 --------------------------
3638 -- Previous_Withed_Unit --
3639 --------------------------
3641 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3645 -- A limited with_clause cannot appear in the same context_clause
3646 -- as a nonlimited with_clause which mentions the same library.
3648 Item := First (Context_Items (Comp_Unit));
3649 while Present (Item) loop
3650 if Nkind (Item) = N_With_Clause
3651 and then Library_Unit (Item) = Library_Unit (W)
3660 end Previous_Withed_Unit;
3662 -- Start of processing for Expand_Limited_With_Clause
3665 New_Nodes_OK := New_Nodes_OK + 1;
3667 if Nkind (Nam) = N_Identifier then
3669 -- Create node for name of withed unit
3672 Make_With_Clause (Loc,
3673 Name => New_Copy (Nam));
3675 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3677 Make_With_Clause (Loc,
3678 Name => Make_Selected_Component (Loc,
3679 Prefix => New_Copy_Tree (Prefix (Nam)),
3680 Selector_Name => New_Copy (Selector_Name (Nam))));
3681 Set_Parent (Withn, Parent (N));
3684 Set_Limited_Present (Withn);
3685 Set_First_Name (Withn);
3686 Set_Implicit_With (Withn);
3690 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3695 -- Do not generate a limited_with_clause on the current unit. This
3696 -- path is taken when a unit has a limited_with clause on one of its
3699 if Unum = Current_Sem_Unit then
3703 Set_Library_Unit (Withn, Cunit (Unum));
3704 Set_Corresponding_Spec
3705 (Withn, Specification (Unit (Cunit (Unum))));
3707 if not Previous_Withed_Unit (Withn) then
3708 Prepend (Withn, Context_Items (Parent (N)));
3709 Mark_Rewrite_Insertion (Withn);
3711 -- Add implicit limited_with_clauses for parents of child units
3712 -- mentioned in limited_with clauses.
3714 if Nkind (Nam) = N_Selected_Component then
3715 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3720 if not Limited_View_Installed (Withn) then
3721 Install_Limited_Withed_Unit (Withn);
3725 New_Nodes_OK := New_Nodes_OK - 1;
3726 end Expand_Limited_With_Clause;
3728 ----------------------
3729 -- Is_Ancestor_Unit --
3730 ----------------------
3732 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
3733 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
3736 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
3737 E2 := Defining_Entity (Unit (Library_Unit (U2)));
3738 return Is_Ancestor_Package (E1, E2);
3742 end Is_Ancestor_Unit;
3744 -- Start of processing for Install_Limited_Context_Clauses
3747 Item := First (Context_Items (N));
3748 while Present (Item) loop
3749 if Nkind (Item) = N_With_Clause
3750 and then Limited_Present (Item)
3751 and then not Error_Posted (Item)
3753 if Nkind (Name (Item)) = N_Selected_Component then
3754 Expand_Limited_With_Clause
3755 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3758 Check_Private_Limited_Withed_Unit (Item);
3760 if not Implicit_With (Item)
3761 and then Is_Child_Spec (Unit (N))
3763 Check_Renamings (Parent_Spec (Unit (N)), Item);
3766 -- A unit may have a limited with on itself if it has a limited
3767 -- with_clause on one of its child units. In that case it is
3768 -- already being compiled and it makes no sense to install its
3771 -- If the item is a limited_private_with_clause, install it if the
3772 -- current unit is a body or if it is a private child. Otherwise
3773 -- the private clause is installed before analyzing the private
3774 -- part of the current unit.
3776 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3777 and then not Limited_View_Installed (Item)
3779 not Is_Ancestor_Unit
3780 (Library_Unit (Item), Cunit (Current_Sem_Unit))
3782 if not Private_Present (Item)
3783 or else Private_Present (N)
3784 or else Nkind_In (Unit (N), N_Package_Body,
3788 Install_Limited_Withed_Unit (Item);
3796 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
3797 -- looking for incomplete subtype declarations of incomplete types
3798 -- visible through a limited with clause.
3800 if Ada_Version >= Ada_2005
3801 and then Analyzed (N)
3802 and then Nkind (Unit (N)) = N_Package_Declaration
3807 Non_Lim_View : Entity_Id;
3810 Decl := First (Visible_Declarations (Specification (Unit (N))));
3811 while Present (Decl) loop
3812 if Nkind (Decl) = N_Subtype_Declaration
3814 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3816 From_With_Type (Defining_Identifier (Decl))
3818 Def_Id := Defining_Identifier (Decl);
3819 Non_Lim_View := Non_Limited_View (Def_Id);
3821 if not Is_Incomplete_Type (Non_Lim_View) then
3823 -- Convert an incomplete subtype declaration into a
3824 -- corresponding non-limited view subtype declaration.
3825 -- This is usually the case when analyzing a body that
3826 -- has regular with clauses, when the spec has limited
3829 -- If the non-limited view is still incomplete, it is
3830 -- the dummy entry already created, and the declaration
3831 -- cannot be reanalyzed. This is the case when installing
3832 -- a parent unit that has limited with-clauses.
3834 Set_Subtype_Indication (Decl,
3835 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3836 Set_Etype (Def_Id, Non_Lim_View);
3837 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3838 Set_Analyzed (Decl, False);
3840 -- Reanalyze the declaration, suppressing the call to
3841 -- Enter_Name to avoid duplicate names.
3843 Analyze_Subtype_Declaration
3853 end Install_Limited_Context_Clauses;
3855 ---------------------
3856 -- Install_Parents --
3857 ---------------------
3859 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3866 P := Unit (Parent_Spec (Lib_Unit));
3867 P_Name := Get_Parent_Entity (P);
3869 if Etype (P_Name) = Any_Type then
3873 if Ekind (P_Name) = E_Generic_Package
3874 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3875 N_Generic_Package_Declaration)
3876 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3879 ("child of a generic package must be a generic unit", Lib_Unit);
3881 elsif not Is_Package_Or_Generic_Package (P_Name) then
3883 ("parent unit must be package or generic package", Lib_Unit);
3884 raise Unrecoverable_Error;
3886 elsif Present (Renamed_Object (P_Name)) then
3887 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3888 raise Unrecoverable_Error;
3890 -- Verify that a child of an instance is itself an instance, or the
3891 -- renaming of one. Given that an instance that is a unit is replaced
3892 -- with a package declaration, check against the original node. The
3893 -- parent may be currently being instantiated, in which case it appears
3894 -- as a declaration, but the generic_parent is already established
3895 -- indicating that we deal with an instance.
3897 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3898 if Nkind (Lib_Unit) in N_Renaming_Declaration
3899 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3901 (Nkind (Lib_Unit) = N_Package_Declaration
3902 and then Present (Generic_Parent (Specification (Lib_Unit))))
3907 ("child of an instance must be an instance or renaming",
3912 -- This is the recursive call that ensures all parents are loaded
3914 if Is_Child_Spec (P) then
3916 Is_Private or else Private_Present (Parent (Lib_Unit)));
3919 -- Now we can install the context for this parent
3921 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3922 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3923 Install_Siblings (P_Name, Parent (Lib_Unit));
3925 -- The child unit is in the declarative region of the parent. The parent
3926 -- must therefore appear in the scope stack and be visible, as when
3927 -- compiling the corresponding body. If the child unit is private or it
3928 -- is a package body, private declarations must be accessible as well.
3929 -- Use declarations in the parent must also be installed. Finally, other
3930 -- child units of the same parent that are in the context are
3931 -- immediately visible.
3933 -- Find entity for compilation unit, and set its private descendant
3934 -- status as needed. Indicate that it is a compilation unit, which is
3935 -- redundant in general, but needed if this is a generated child spec
3936 -- for a child body without previous spec.
3938 E_Name := Defining_Entity (Lib_Unit);
3940 Set_Is_Child_Unit (E_Name);
3941 Set_Is_Compilation_Unit (E_Name);
3943 Set_Is_Private_Descendant (E_Name,
3944 Is_Private_Descendant (P_Name)
3945 or else Private_Present (Parent (Lib_Unit)));
3947 P_Spec := Specification (Unit_Declaration_Node (P_Name));
3948 Push_Scope (P_Name);
3950 -- Save current visibility of unit
3952 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3953 Is_Immediately_Visible (P_Name);
3954 Set_Is_Immediately_Visible (P_Name);
3955 Install_Visible_Declarations (P_Name);
3956 Set_Use (Visible_Declarations (P_Spec));
3958 -- If the parent is a generic unit, its formal part may contain formal
3959 -- packages and use clauses for them.
3961 if Ekind (P_Name) = E_Generic_Package then
3962 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3966 or else Private_Present (Parent (Lib_Unit))
3968 Install_Private_Declarations (P_Name);
3969 Install_Private_With_Clauses (P_Name);
3970 Set_Use (Private_Declarations (P_Spec));
3972 end Install_Parents;
3974 ----------------------------------
3975 -- Install_Private_With_Clauses --
3976 ----------------------------------
3978 procedure Install_Private_With_Clauses (P : Entity_Id) is
3979 Decl : constant Node_Id := Unit_Declaration_Node (P);
3983 if Debug_Flag_I then
3984 Write_Str ("install private with clauses of ");
3985 Write_Name (Chars (P));
3989 if Nkind (Parent (Decl)) = N_Compilation_Unit then
3990 Item := First (Context_Items (Parent (Decl)));
3991 while Present (Item) loop
3992 if Nkind (Item) = N_With_Clause
3993 and then Private_Present (Item)
3995 if Limited_Present (Item) then
3996 if not Limited_View_Installed (Item) then
3997 Install_Limited_Withed_Unit (Item);
4000 Install_Withed_Unit (Item, Private_With_OK => True);
4007 end Install_Private_With_Clauses;
4009 ----------------------
4010 -- Install_Siblings --
4011 ----------------------
4013 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4019 -- Iterate over explicit with clauses, and check whether the scope of
4020 -- each entity is an ancestor of the current unit, in which case it is
4021 -- immediately visible.
4023 Item := First (Context_Items (N));
4024 while Present (Item) loop
4026 -- Do not install private_with_clauses declaration, unless unit
4027 -- is itself a private child unit, or is a body. Note that for a
4028 -- subprogram body the private_with_clause does not take effect until
4029 -- after the specification.
4031 if Nkind (Item) /= N_With_Clause
4032 or else Implicit_With (Item)
4033 or else Limited_Present (Item)
4037 elsif not Private_Present (Item)
4038 or else Private_Present (N)
4039 or else Nkind (Unit (N)) = N_Package_Body
4041 Id := Entity (Name (Item));
4043 if Is_Child_Unit (Id)
4044 and then Is_Ancestor_Package (Scope (Id), U_Name)
4046 Set_Is_Immediately_Visible (Id);
4048 -- Check for the presence of another unit in the context that
4049 -- may be inadvertently hidden by the child.
4051 Prev := Current_Entity (Id);
4054 and then Is_Immediately_Visible (Prev)
4055 and then not Is_Child_Unit (Prev)
4061 Clause := First (Context_Items (N));
4062 while Present (Clause) loop
4063 if Nkind (Clause) = N_With_Clause
4064 and then Entity (Name (Clause)) = Prev
4067 ("child unit& hides compilation unit " &
4068 "with the same name?",
4078 -- The With_Clause may be on a grand-child or one of its further
4079 -- descendants, which makes a child immediately visible. Examine
4080 -- ancestry to determine whether such a child exists. For example,
4081 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4082 -- is immediately visible.
4084 elsif Is_Child_Unit (Id) then
4090 while Is_Child_Unit (Par) loop
4091 if Is_Ancestor_Package (Scope (Par), U_Name) then
4092 Set_Is_Immediately_Visible (Par);
4101 -- If the item is a private with-clause on a child unit, the parent
4102 -- may have been installed already, but the child unit must remain
4103 -- invisible until installed in a private part or body, unless there
4104 -- is already a regular with_clause for it in the current unit.
4106 elsif Private_Present (Item) then
4107 Id := Entity (Name (Item));
4109 if Is_Child_Unit (Id) then
4113 function In_Context return Boolean;
4114 -- Scan context of current unit, to check whether there is
4115 -- a with_clause on the same unit as a private with-clause
4116 -- on a parent, in which case child unit is visible. If the
4117 -- unit is a grand-child, the same applies to its parent.
4123 function In_Context return Boolean is
4126 First (Context_Items (Cunit (Current_Sem_Unit)));
4127 while Present (Clause) loop
4128 if Nkind (Clause) = N_With_Clause
4129 and then Comes_From_Source (Clause)
4130 and then Is_Entity_Name (Name (Clause))
4131 and then not Private_Present (Clause)
4133 if Entity (Name (Clause)) = Id
4135 (Nkind (Name (Clause)) = N_Expanded_Name
4136 and then Entity (Prefix (Name (Clause))) = Id)
4149 Set_Is_Visible_Child_Unit (Id, In_Context);
4156 end Install_Siblings;
4158 ---------------------------------
4159 -- Install_Limited_Withed_Unit --
4160 ---------------------------------
4162 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4163 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4166 Is_Child_Package : Boolean := False;
4167 Lim_Header : Entity_Id;
4168 Lim_Typ : Entity_Id;
4170 procedure Check_Body_Required;
4171 -- A unit mentioned in a limited with_clause may not be mentioned in
4172 -- a regular with_clause, but must still be included in the current
4173 -- partition. We need to determine whether the unit needs a body, so
4174 -- that the binder can determine the name of the file to be compiled.
4175 -- Checking whether a unit needs a body can be done without semantic
4176 -- analysis, by examining the nature of the declarations in the package.
4178 function Has_Limited_With_Clause
4179 (C_Unit : Entity_Id;
4180 Pack : Entity_Id) return Boolean;
4181 -- Determine whether any package in the ancestor chain starting with
4182 -- C_Unit has a limited with clause for package Pack.
4184 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4185 -- Check if some package installed though normal with-clauses has a
4186 -- renaming declaration of package P. AARM 10.1.2(21/2).
4188 -------------------------
4189 -- Check_Body_Required --
4190 -------------------------
4192 procedure Check_Body_Required is
4193 PA : constant List_Id :=
4194 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4196 procedure Check_Declarations (Spec : Node_Id);
4197 -- Recursive procedure that does the work and checks nested packages
4199 ------------------------
4200 -- Check_Declarations --
4201 ------------------------
4203 procedure Check_Declarations (Spec : Node_Id) is
4205 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4207 Subp_List : constant Elist_Id := New_Elmt_List;
4209 procedure Check_Pragma_Import (P : Node_Id);
4210 -- If a pragma import applies to a previous subprogram, the
4211 -- enclosing unit may not need a body. The processing is syntactic
4212 -- and does not require a declaration to be analyzed. The code
4213 -- below also handles pragma Import when applied to a subprogram
4214 -- that renames another. In this case the pragma applies to the
4217 -- Chains of multiple renames are not handled by the code below.
4218 -- It is probably impossible to handle all cases without proper
4219 -- name resolution. In such cases the algorithm is conservative
4220 -- and will indicate that a body is needed???
4222 -------------------------
4223 -- Check_Pragma_Import --
4224 -------------------------
4226 procedure Check_Pragma_Import (P : Node_Id) is
4232 procedure Remove_Homonyms (E : Node_Id);
4233 -- Make one pass over list of subprograms. Called again if
4234 -- subprogram is a renaming. E is known to be an identifier.
4236 ---------------------
4237 -- Remove_Homonyms --
4238 ---------------------
4240 procedure Remove_Homonyms (E : Node_Id) is
4241 R : Entity_Id := Empty;
4242 -- Name of renamed entity, if any
4245 Subp_Id := First_Elmt (Subp_List);
4246 while Present (Subp_Id) loop
4247 if Chars (Node (Subp_Id)) = Chars (E) then
4248 if Nkind (Parent (Parent (Node (Subp_Id))))
4249 /= N_Subprogram_Renaming_Declaration
4252 Next_Elmt (Subp_Id);
4253 Remove_Elmt (Subp_List, Prev_Id);
4255 R := Name (Parent (Parent (Node (Subp_Id))));
4259 Next_Elmt (Subp_Id);
4264 if Nkind (R) = N_Identifier then
4265 Remove_Homonyms (R);
4267 elsif Nkind (R) = N_Selected_Component then
4268 Remove_Homonyms (Selector_Name (R));
4270 -- Renaming of attribute
4276 end Remove_Homonyms;
4278 -- Start of processing for Check_Pragma_Import
4281 -- Find name of entity in Import pragma. We have not analyzed
4282 -- the construct, so we must guard against syntax errors.
4284 Arg := Next (First (Pragma_Argument_Associations (P)));
4287 or else Nkind (Expression (Arg)) /= N_Identifier
4291 Imported := Expression (Arg);
4294 Remove_Homonyms (Imported);
4295 end Check_Pragma_Import;
4297 -- Start of processing for Check_Declarations
4300 -- Search for Elaborate Body pragma
4302 Decl := First (Visible_Declarations (Spec));
4303 while Present (Decl)
4304 and then Nkind (Decl) = N_Pragma
4306 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4307 Set_Body_Required (Library_Unit (N));
4314 -- Look for declarations that require the presence of a body. We
4315 -- have already skipped pragmas at the start of the list.
4317 while Present (Decl) loop
4319 -- Subprogram that comes from source means body may be needed.
4320 -- Save for subsequent examination of import pragmas.
4322 if Comes_From_Source (Decl)
4323 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4324 N_Subprogram_Renaming_Declaration,
4325 N_Generic_Subprogram_Declaration))
4327 Append_Elmt (Defining_Entity (Decl), Subp_List);
4329 -- Package declaration of generic package declaration. We need
4330 -- to recursively examine nested declarations.
4332 elsif Nkind_In (Decl, N_Package_Declaration,
4333 N_Generic_Package_Declaration)
4335 Check_Declarations (Specification (Decl));
4337 elsif Nkind (Decl) = N_Pragma
4338 and then Pragma_Name (Decl) = Name_Import
4340 Check_Pragma_Import (Decl);
4346 -- Same set of tests for private part. In addition to subprograms
4347 -- detect the presence of Taft Amendment types (incomplete types
4348 -- completed in the body).
4350 Decl := First (Private_Declarations (Spec));
4351 while Present (Decl) loop
4352 if Comes_From_Source (Decl)
4353 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4354 N_Subprogram_Renaming_Declaration,
4355 N_Generic_Subprogram_Declaration))
4357 Append_Elmt (Defining_Entity (Decl), Subp_List);
4359 elsif Nkind_In (Decl, N_Package_Declaration,
4360 N_Generic_Package_Declaration)
4362 Check_Declarations (Specification (Decl));
4364 -- Collect incomplete type declarations for separate pass
4366 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4367 Append_Elmt (Decl, Incomplete_Decls);
4369 elsif Nkind (Decl) = N_Pragma
4370 and then Pragma_Name (Decl) = Name_Import
4372 Check_Pragma_Import (Decl);
4378 -- Now check incomplete declarations to locate Taft amendment
4379 -- types. This can be done by examining the defining identifiers
4380 -- of type declarations without real semantic analysis.
4386 Inc := First_Elmt (Incomplete_Decls);
4387 while Present (Inc) loop
4388 Decl := Next (Node (Inc));
4389 while Present (Decl) loop
4390 if Nkind (Decl) = N_Full_Type_Declaration
4391 and then Chars (Defining_Identifier (Decl)) =
4392 Chars (Defining_Identifier (Node (Inc)))
4400 -- If no completion, this is a TAT, and a body is needed
4403 Set_Body_Required (Library_Unit (N));
4411 -- Finally, check whether there are subprograms that still require
4412 -- a body, i.e. are not renamings or null.
4414 if not Is_Empty_Elmt_List (Subp_List) then
4420 Subp_Id := First_Elmt (Subp_List);
4421 Spec := Parent (Node (Subp_Id));
4423 while Present (Subp_Id) loop
4424 if Nkind (Parent (Spec))
4425 = N_Subprogram_Renaming_Declaration
4429 elsif Nkind (Spec) = N_Procedure_Specification
4430 and then Null_Present (Spec)
4435 Set_Body_Required (Library_Unit (N));
4439 Next_Elmt (Subp_Id);
4443 end Check_Declarations;
4445 -- Start of processing for Check_Body_Required
4448 -- If this is an imported package (Java and CIL usage) no body is
4449 -- needed. Scan list of pragmas that may follow a compilation unit
4450 -- to look for a relevant pragma Import.
4452 if Present (PA) then
4458 while Present (Prag) loop
4459 if Nkind (Prag) = N_Pragma
4460 and then Get_Pragma_Id (Prag) = Pragma_Import
4470 Check_Declarations (Specification (P_Unit));
4471 end Check_Body_Required;
4473 -----------------------------
4474 -- Has_Limited_With_Clause --
4475 -----------------------------
4477 function Has_Limited_With_Clause
4478 (C_Unit : Entity_Id;
4479 Pack : Entity_Id) return Boolean
4486 while Present (Par) loop
4487 if Ekind (Par) /= E_Package then
4491 -- Retrieve the Compilation_Unit node for Par and determine if
4492 -- its context clauses contain a limited with for Pack.
4494 Par_Unit := Parent (Parent (Parent (Par)));
4496 if Nkind (Par_Unit) = N_Package_Declaration then
4497 Par_Unit := Parent (Par_Unit);
4500 if Has_With_Clause (Par_Unit, Pack, True) then
4504 -- If there are more ancestors, climb up the tree, otherwise we
4507 if Is_Child_Unit (Par) then
4515 end Has_Limited_With_Clause;
4517 ----------------------------------
4518 -- Is_Visible_Through_Renamings --
4519 ----------------------------------
4521 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4522 Kind : constant Node_Kind :=
4523 Nkind (Unit (Cunit (Current_Sem_Unit)));
4529 -- Example of the error detected by this subprogram:
4537 -- package Ren_P renames P;
4543 -- limited with P; -- ERROR
4544 -- package R.C is ...
4546 Aux_Unit := Cunit (Current_Sem_Unit);
4549 Item := First (Context_Items (Aux_Unit));
4550 while Present (Item) loop
4551 if Nkind (Item) = N_With_Clause
4552 and then not Limited_Present (Item)
4553 and then Nkind (Unit (Library_Unit (Item))) =
4554 N_Package_Declaration
4557 First (Visible_Declarations
4558 (Specification (Unit (Library_Unit (Item)))));
4559 while Present (Decl) loop
4560 if Nkind (Decl) = N_Package_Renaming_Declaration
4561 and then Entity (Name (Decl)) = P
4563 -- Generate the error message only if the current unit
4564 -- is a package declaration; in case of subprogram
4565 -- bodies and package bodies we just return True to
4566 -- indicate that the limited view must not be
4569 if Kind = N_Package_Declaration then
4571 ("simultaneous visibility of the limited and " &
4572 "unlimited views not allowed", N);
4573 Error_Msg_Sloc := Sloc (Item);
4575 ("\\ unlimited view of & visible through the " &
4576 "context clause #", N, P);
4577 Error_Msg_Sloc := Sloc (Decl);
4578 Error_Msg_NE ("\\ and the renaming #", N, P);
4591 -- If it is a body not acting as spec, follow pointer to the
4592 -- corresponding spec, otherwise follow pointer to parent spec.
4594 if Present (Library_Unit (Aux_Unit))
4595 and then Nkind_In (Unit (Aux_Unit),
4596 N_Package_Body, N_Subprogram_Body)
4598 if Aux_Unit = Library_Unit (Aux_Unit) then
4600 -- Aux_Unit is a body that acts as a spec. Clause has
4601 -- already been flagged as illegal.
4606 Aux_Unit := Library_Unit (Aux_Unit);
4610 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4613 exit when No (Aux_Unit);
4617 end Is_Visible_Through_Renamings;
4619 -- Start of processing for Install_Limited_Withed_Unit
4622 pragma Assert (not Limited_View_Installed (N));
4624 -- In case of limited with_clause on subprograms, generics, instances,
4625 -- or renamings, the corresponding error was previously posted and we
4626 -- have nothing to do here. If the file is missing altogether, it has
4627 -- no source location.
4629 if Nkind (P_Unit) /= N_Package_Declaration
4630 or else Sloc (P_Unit) = No_Location
4635 P := Defining_Unit_Name (Specification (P_Unit));
4637 -- Handle child packages
4639 if Nkind (P) = N_Defining_Program_Unit_Name then
4640 Is_Child_Package := True;
4641 P := Defining_Identifier (P);
4644 -- Do not install the limited-view if the context of the unit is already
4645 -- available through a regular with clause.
4647 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4648 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4653 -- Do not install the limited-view if the full-view is already visible
4654 -- through renaming declarations.
4656 if Is_Visible_Through_Renamings (P) then
4660 -- Do not install the limited view if this is the unit being analyzed.
4661 -- This unusual case will happen when a unit has a limited_with clause
4662 -- on one of its children. The compilation of the child forces the load
4663 -- of the parent which tries to install the limited view of the child
4664 -- again. Installing the limited view must also be disabled when
4665 -- compiling the body of the child unit.
4667 if P = Cunit_Entity (Current_Sem_Unit)
4669 (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4670 and then P = Main_Unit_Entity)
4675 -- This scenario is similar to the one above, the difference is that the
4676 -- compilation of sibling Par.Sib forces the load of parent Par which
4677 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4678 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4679 -- non-limited views of all entities from Lim_Pack.
4681 -- limited with Lim_Pack; -- [1]
4682 -- package Par is ... package Lim_Pack is ...
4684 -- with Lim_Pack; -- [2]
4685 -- package Par.Sib is ... package body Par.Sib is ...
4687 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4688 -- Sem_Unit is the body of Par.Sib.
4690 if Ekind (P) = E_Package
4691 and then Ekind (Main_Unit_Entity) = E_Package
4692 and then Is_Child_Unit (Main_Unit_Entity)
4694 -- The body has a regular with clause
4696 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4697 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4699 -- One of the ancestors has a limited with clause
4701 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4702 N_Package_Specification
4703 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4708 -- A common use of the limited-with is to have a limited-with in the
4709 -- package spec, and a normal with in its package body. For example:
4711 -- limited with X; -- [1]
4715 -- package body A is ...
4717 -- The compilation of A's body installs the context clauses found at [2]
4718 -- and then the context clauses of its specification (found at [1]). As
4719 -- a consequence, at [1] the specification of X has been analyzed and it
4720 -- is immediately visible. According to the semantics of limited-with
4721 -- context clauses we don't install the limited view because the full
4722 -- view of X supersedes its limited view.
4724 if Analyzed (P_Unit)
4726 (Is_Immediately_Visible (P)
4727 or else (Is_Child_Package and then Is_Visible_Child_Unit (P)))
4730 -- The presence of both the limited and the analyzed nonlimited view
4731 -- may also be an error, such as an illegal context for a limited
4732 -- with_clause. In that case, do not process the context item at all.
4734 if Error_Posted (N) then
4738 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
4742 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
4743 while Present (Item) loop
4744 if Nkind (Item) = N_With_Clause
4745 and then Comes_From_Source (Item)
4746 and then Entity (Name (Item)) = P
4755 -- If this is a child body, assume that the nonlimited with_clause
4756 -- appears in an ancestor. Could be refined ???
4760 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
4767 -- If in package declaration, nonlimited view brought in from
4768 -- parent unit or some error condition.
4774 if Debug_Flag_I then
4775 Write_Str ("install limited view of ");
4776 Write_Name (Chars (P));
4780 -- If the unit has not been analyzed and the limited view has not been
4781 -- already installed then we install it.
4783 if not Analyzed (P_Unit) then
4784 if not In_Chain (P) then
4786 -- Minimum decoration
4788 Set_Ekind (P, E_Package);
4789 Set_Etype (P, Standard_Void_Type);
4790 Set_Scope (P, Standard_Standard);
4792 if Is_Child_Package then
4793 Set_Is_Child_Unit (P);
4794 Set_Is_Visible_Child_Unit (P);
4795 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4798 -- Place entity on visibility structure
4800 Set_Homonym (P, Current_Entity (P));
4801 Set_Current_Entity (P);
4803 if Debug_Flag_I then
4804 Write_Str (" (homonym) chain ");
4805 Write_Name (Chars (P));
4809 -- Install the incomplete view. The first element of the limited
4810 -- view is a header (an E_Package entity) used to reference the
4811 -- first shadow entity in the private part of the package.
4813 Lim_Header := Limited_View (P);
4814 Lim_Typ := First_Entity (Lim_Header);
4816 while Present (Lim_Typ)
4817 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4819 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4820 Set_Current_Entity (Lim_Typ);
4822 if Debug_Flag_I then
4823 Write_Str (" (homonym) chain ");
4824 Write_Name (Chars (Lim_Typ));
4828 Next_Entity (Lim_Typ);
4832 -- If the unit appears in a previous regular with_clause, the regular
4833 -- entities of the public part of the withed package must be replaced
4834 -- by the shadow ones.
4836 -- This code must be kept synchronized with the code that replaces the
4837 -- shadow entities by the real entities (see body of Remove_Limited
4838 -- With_Clause); otherwise the contents of the homonym chains are not
4842 -- Hide all the type entities of the public part of the package to
4843 -- avoid its usage. This is needed to cover all the subtype decla-
4844 -- rations because we do not remove them from the homonym chain.
4846 E := First_Entity (P);
4847 while Present (E) and then E /= First_Private_Entity (P) loop
4849 Set_Was_Hidden (E, Is_Hidden (E));
4856 -- Replace the real entities by the shadow entities of the limited
4857 -- view. The first element of the limited view is a header that is
4858 -- used to reference the first shadow entity in the private part
4859 -- of the package. Successive elements are the limited views of the
4860 -- type (including regular incomplete types) declared in the package.
4862 Lim_Header := Limited_View (P);
4864 Lim_Typ := First_Entity (Lim_Header);
4865 while Present (Lim_Typ)
4866 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4868 pragma Assert (not In_Chain (Lim_Typ));
4870 -- Do not unchain nested packages and child units
4872 if Ekind (Lim_Typ) /= E_Package
4873 and then not Is_Child_Unit (Lim_Typ)
4879 Prev := Current_Entity (Lim_Typ);
4882 -- Replace E in the homonyms list, so that the limited view
4883 -- becomes available.
4885 if E = Non_Limited_View (Lim_Typ) then
4886 Set_Homonym (Lim_Typ, Homonym (Prev));
4887 Set_Current_Entity (Lim_Typ);
4891 E := Homonym (Prev);
4893 -- E may have been removed when installing a previous
4894 -- limited_with_clause.
4898 exit when E = Non_Limited_View (Lim_Typ);
4900 Prev := Homonym (Prev);
4904 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4905 Set_Homonym (Prev, Lim_Typ);
4910 if Debug_Flag_I then
4911 Write_Str (" (homonym) chain ");
4912 Write_Name (Chars (Lim_Typ));
4917 Next_Entity (Lim_Typ);
4921 -- The package must be visible while the limited-with clause is active
4922 -- because references to the type P.T must resolve in the usual way.
4923 -- In addition, we remember that the limited-view has been installed to
4924 -- uninstall it at the point of context removal.
4926 Set_Is_Immediately_Visible (P);
4927 Set_Limited_View_Installed (N);
4929 -- If unit has not been analyzed in some previous context, check
4930 -- (imperfectly ???) whether it might need a body.
4932 if not Analyzed (P_Unit) then
4933 Check_Body_Required;
4936 -- If the package in the limited_with clause is a child unit, the clause
4937 -- is unanalyzed and appears as a selected component. Recast it as an
4938 -- expanded name so that the entity can be properly set. Use entity of
4939 -- parent, if available, for higher ancestors in the name.
4941 if Nkind (Name (N)) = N_Selected_Component then
4949 while Nkind (Nam) = N_Selected_Component
4950 and then Present (Ent)
4952 Change_Selected_Component_To_Expanded_Name (Nam);
4954 -- Set entity of parent identifiers if the unit is a child
4955 -- unit. This ensures that the tree is properly formed from
4956 -- semantic point of view (e.g. for ASIS queries).
4958 Set_Entity (Nam, Ent);
4960 Nam := Prefix (Nam);
4963 -- Set entity of last ancestor
4965 if Nkind (Nam) = N_Identifier then
4966 Set_Entity (Nam, Ent);
4972 Set_Entity (Name (N), P);
4973 Set_From_With_Type (P);
4974 end Install_Limited_Withed_Unit;
4976 -------------------------
4977 -- Install_Withed_Unit --
4978 -------------------------
4980 procedure Install_Withed_Unit
4981 (With_Clause : Node_Id;
4982 Private_With_OK : Boolean := False)
4984 Uname : constant Entity_Id := Entity (Name (With_Clause));
4985 P : constant Entity_Id := Scope (Uname);
4988 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
4989 -- compiling a package declaration and the Private_With_OK flag was not
4990 -- set by the caller. These declarations will be installed later (before
4991 -- analyzing the private part of the package).
4993 if Private_Present (With_Clause)
4994 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
4995 and then not (Private_With_OK)
5000 if Debug_Flag_I then
5001 if Private_Present (With_Clause) then
5002 Write_Str ("install private withed unit ");
5004 Write_Str ("install withed unit ");
5007 Write_Name (Chars (Uname));
5011 -- We do not apply the restrictions to an internal unit unless we are
5012 -- compiling the internal unit as a main unit. This check is also
5013 -- skipped for dummy units (for missing packages).
5015 if Sloc (Uname) /= No_Location
5016 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
5017 or else Current_Sem_Unit = Main_Unit)
5019 Check_Restricted_Unit
5020 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5023 if P /= Standard_Standard then
5025 -- If the unit is not analyzed after analysis of the with clause and
5026 -- it is an instantiation then it awaits a body and is the main unit.
5027 -- Its appearance in the context of some other unit indicates a
5028 -- circular dependency (DEC suite perversity).
5030 if not Analyzed (Uname)
5031 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5034 ("instantiation depends on itself", Name (With_Clause));
5036 elsif not Is_Visible_Child_Unit (Uname) then
5037 Set_Is_Visible_Child_Unit (Uname);
5039 -- If the child unit appears in the context of its parent, it is
5040 -- immediately visible.
5042 if In_Open_Scopes (Scope (Uname)) then
5043 Set_Is_Immediately_Visible (Uname);
5046 if Is_Generic_Instance (Uname)
5047 and then Ekind (Uname) in Subprogram_Kind
5049 -- Set flag as well on the visible entity that denotes the
5050 -- instance, which renames the current one.
5052 Set_Is_Visible_Child_Unit
5054 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5057 -- The parent unit may have been installed already, and may have
5058 -- appeared in a use clause.
5060 if In_Use (Scope (Uname)) then
5061 Set_Is_Potentially_Use_Visible (Uname);
5064 Set_Context_Installed (With_Clause);
5067 elsif not Is_Immediately_Visible (Uname) then
5068 if not Private_Present (With_Clause)
5069 or else Private_With_OK
5071 Set_Is_Immediately_Visible (Uname);
5074 Set_Context_Installed (With_Clause);
5077 -- A with-clause overrides a with-type clause: there are no restric-
5078 -- tions on the use of package entities.
5080 if Ekind (Uname) = E_Package then
5081 Set_From_With_Type (Uname, False);
5084 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5085 -- unit if there is a visible homograph for it declared in the same
5086 -- declarative region. This pathological case can only arise when an
5087 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5088 -- G1 has a generic child also named G2, and the context includes with_
5089 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5090 -- of I1.G2 visible as well. If the child unit is named Standard, do
5091 -- not apply the check to the Standard package itself.
5093 if Is_Child_Unit (Uname)
5094 and then Is_Visible_Child_Unit (Uname)
5095 and then Ada_Version >= Ada_2005
5098 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5104 U2 := Homonym (Uname);
5106 and then U2 /= Standard_Standard
5109 Decl2 := Unit_Declaration_Node (P2);
5111 if Is_Child_Unit (U2)
5112 and then Is_Visible_Child_Unit (U2)
5114 if Is_Generic_Instance (P)
5115 and then Nkind (Decl1) = N_Package_Declaration
5116 and then Generic_Parent (Specification (Decl1)) = P2
5118 Error_Msg_N ("illegal with_clause", With_Clause);
5120 ("\child unit has visible homograph" &
5121 " (RM 8.3(26), 10.1.1(19))",
5125 elsif Is_Generic_Instance (P2)
5126 and then Nkind (Decl2) = N_Package_Declaration
5127 and then Generic_Parent (Specification (Decl2)) = P
5129 -- With_clause for child unit of instance appears before
5130 -- in the context. We want to place the error message on
5131 -- it, not on the generic child unit itself.
5134 Prev_Clause : Node_Id;
5137 Prev_Clause := First (List_Containing (With_Clause));
5138 while Entity (Name (Prev_Clause)) /= U2 loop
5142 pragma Assert (Present (Prev_Clause));
5143 Error_Msg_N ("illegal with_clause", Prev_Clause);
5145 ("\child unit has visible homograph" &
5146 " (RM 8.3(26), 10.1.1(19))",
5157 end Install_Withed_Unit;
5163 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5164 K : constant Node_Kind := Nkind (Lib_Unit);
5167 return (K in N_Generic_Declaration or else
5168 K in N_Generic_Instantiation or else
5169 K in N_Generic_Renaming_Declaration or else
5170 K = N_Package_Declaration or else
5171 K = N_Package_Renaming_Declaration or else
5172 K = N_Subprogram_Declaration or else
5173 K = N_Subprogram_Renaming_Declaration)
5174 and then Present (Parent_Spec (Lib_Unit));
5177 ------------------------------------
5178 -- Is_Legal_Shadow_Entity_In_Body --
5179 ------------------------------------
5181 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5182 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5184 return Nkind (Unit (C_Unit)) = N_Package_Body
5187 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5188 end Is_Legal_Shadow_Entity_In_Body;
5190 -----------------------
5191 -- Load_Needed_Body --
5192 -----------------------
5194 -- N is a generic unit named in a with clause, or else it is a unit that
5195 -- contains a generic unit or an inlined function. In order to perform an
5196 -- instantiation, the body of the unit must be present. If the unit itself
5197 -- is generic, we assume that an instantiation follows, and load & analyze
5198 -- the body unconditionally. This forces analysis of the spec as well.
5200 -- If the unit is not generic, but contains a generic unit, it is loaded on
5201 -- demand, at the point of instantiation (see ch12).
5203 procedure Load_Needed_Body
5206 Do_Analyze : Boolean := True)
5208 Body_Name : Unit_Name_Type;
5209 Unum : Unit_Number_Type;
5211 Save_Style_Check : constant Boolean := Opt.Style_Check;
5212 -- The loading and analysis is done with style checks off
5215 if not GNAT_Mode then
5216 Style_Check := False;
5219 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5222 (Load_Name => Body_Name,
5228 if Unum = No_Unit then
5232 Compiler_State := Analyzing; -- reset after load
5234 if not Fatal_Error (Unum) or else Try_Semantics then
5235 if Debug_Flag_L then
5236 Write_Str ("*** Loaded generic body");
5241 Semantics (Cunit (Unum));
5248 Style_Check := Save_Style_Check;
5249 end Load_Needed_Body;
5251 -------------------------
5252 -- Build_Limited_Views --
5253 -------------------------
5255 procedure Build_Limited_Views (N : Node_Id) is
5256 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
5257 P : constant Entity_Id := Cunit_Entity (Unum);
5259 Spec : Node_Id; -- To denote a package specification
5260 Lim_Typ : Entity_Id; -- To denote shadow entities
5261 Comp_Typ : Entity_Id; -- To denote real entities
5263 Lim_Header : Entity_Id; -- Package entity
5264 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
5265 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
5267 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id);
5268 -- Add attributes of an incomplete type to a shadow entity. The same
5269 -- attributes are placed on the real entity, so that gigi receives
5270 -- a consistent view.
5272 procedure Decorate_Package_Specification (P : Entity_Id);
5273 -- Add attributes of a package entity to the entity in a package
5276 procedure Decorate_Tagged_Type
5280 Mark : Boolean := False);
5281 -- Set basic attributes of tagged type T, including its class-wide type.
5282 -- The parameters Loc, Scope are used to decorate the class-wide type.
5283 -- Use flag Mark to label the class-wide type as Materialize_Entity.
5285 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id);
5286 -- Construct list of shadow entities and attach it to entity of
5287 -- package that is mentioned in a limited_with clause.
5289 function New_Internal_Shadow_Entity
5290 (Kind : Entity_Kind;
5291 Sloc_Value : Source_Ptr;
5292 Id_Char : Character) return Entity_Id;
5293 -- Build a new internal entity and append it to the list of shadow
5294 -- entities available through the limited-header
5300 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id) is
5301 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
5302 Is_Tagged : Boolean;
5307 while Present (Decl) loop
5309 -- For each library_package_declaration in the environment, there
5310 -- is an implicit declaration of a *limited view* of that library
5311 -- package. The limited view of a package contains:
5313 -- * For each nested package_declaration, a declaration of the
5314 -- limited view of that package, with the same defining-
5315 -- program-unit name.
5317 -- * For each type_declaration in the visible part, an incomplete
5318 -- type-declaration with the same defining_identifier, whose
5319 -- completion is the type_declaration. If the type_declaration
5320 -- is tagged, then the incomplete_type_declaration is tagged
5323 -- The partial view is tagged if the declaration has the
5324 -- explicit keyword, or else if it is a type extension, both
5325 -- of which can be ascertained syntactically.
5327 if Nkind (Decl) = N_Full_Type_Declaration then
5329 (Nkind (Type_Definition (Decl)) = N_Record_Definition
5330 and then Tagged_Present (Type_Definition (Decl)))
5332 (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
5335 (Record_Extension_Part (Type_Definition (Decl))));
5337 Comp_Typ := Defining_Identifier (Decl);
5339 if not Analyzed_Unit then
5341 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope, True);
5343 Decorate_Incomplete_Type (Comp_Typ, Scope);
5347 -- Create shadow entity for type
5350 New_Internal_Shadow_Entity
5351 (Kind => Ekind (Comp_Typ),
5352 Sloc_Value => Sloc (Comp_Typ),
5355 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5356 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5357 Set_From_With_Type (Lim_Typ);
5360 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5362 Decorate_Incomplete_Type (Lim_Typ, Scope);
5365 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5367 elsif Nkind_In (Decl, N_Private_Type_Declaration,
5368 N_Incomplete_Type_Declaration,
5369 N_Task_Type_Declaration,
5370 N_Protected_Type_Declaration)
5372 Comp_Typ := Defining_Identifier (Decl);
5375 Nkind_In (Decl, N_Private_Type_Declaration,
5376 N_Incomplete_Type_Declaration)
5377 and then Tagged_Present (Decl);
5379 if not Analyzed_Unit then
5381 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope, True);
5383 Decorate_Incomplete_Type (Comp_Typ, Scope);
5388 New_Internal_Shadow_Entity
5389 (Kind => Ekind (Comp_Typ),
5390 Sloc_Value => Sloc (Comp_Typ),
5393 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5394 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5395 Set_From_With_Type (Lim_Typ);
5398 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5400 Decorate_Incomplete_Type (Lim_Typ, Scope);
5403 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5405 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5406 Comp_Typ := Defining_Identifier (Decl);
5408 if not Analyzed_Unit then
5409 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope, True);
5412 -- Create shadow entity for type
5415 New_Internal_Shadow_Entity
5416 (Kind => Ekind (Comp_Typ),
5417 Sloc_Value => Sloc (Comp_Typ),
5420 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5421 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5422 Set_From_With_Type (Lim_Typ);
5424 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5425 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5427 elsif Nkind (Decl) = N_Package_Declaration then
5432 Spec : constant Node_Id := Specification (Decl);
5435 Comp_Typ := Defining_Unit_Name (Spec);
5437 if not Analyzed (Cunit (Unum)) then
5438 Decorate_Package_Specification (Comp_Typ);
5439 Set_Scope (Comp_Typ, Scope);
5443 New_Internal_Shadow_Entity
5444 (Kind => Ekind (Comp_Typ),
5445 Sloc_Value => Sloc (Comp_Typ),
5448 Decorate_Package_Specification (Lim_Typ);
5449 Set_Scope (Lim_Typ, Scope);
5451 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5452 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5453 Set_From_With_Type (Lim_Typ);
5455 -- Note: The non_limited_view attribute is not used
5456 -- for local packages.
5460 First_Decl => First (Visible_Declarations (Spec)));
5468 ------------------------------
5469 -- Decorate_Incomplete_Type --
5470 ------------------------------
5472 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id) is
5474 Set_Ekind (E, E_Incomplete_Type);
5475 Set_Scope (E, Scop);
5477 Set_Is_First_Subtype (E, True);
5478 Set_Stored_Constraint (E, No_Elist);
5479 Set_Full_View (E, Empty);
5480 Init_Size_Align (E);
5481 end Decorate_Incomplete_Type;
5483 --------------------------
5484 -- Decorate_Tagged_Type --
5485 --------------------------
5487 procedure Decorate_Tagged_Type
5491 Mark : Boolean := False)
5496 Decorate_Incomplete_Type (T, Scop);
5497 Set_Is_Tagged_Type (T);
5499 -- Build corresponding class_wide type, if not previously done
5501 -- Note: The class-wide entity is shared by the limited-view
5502 -- and the full-view.
5504 if No (Class_Wide_Type (T)) then
5505 CW := New_External_Entity (E_Void, Scope (T), Loc, T, 'C', 0, 'T');
5507 -- Set parent to be the same as the parent of the tagged type.
5508 -- We need a parent field set, and it is supposed to point to
5509 -- the declaration of the type. The tagged type declaration
5510 -- essentially declares two separate types, the tagged type
5511 -- itself and the corresponding class-wide type, so it is
5512 -- reasonable for the parent fields to point to the declaration
5515 Set_Parent (CW, Parent (T));
5517 -- Set remaining fields of classwide type
5519 Set_Ekind (CW, E_Class_Wide_Type);
5521 Set_Scope (CW, Scop);
5522 Set_Is_Tagged_Type (CW);
5523 Set_Is_First_Subtype (CW, True);
5524 Init_Size_Align (CW);
5525 Set_Has_Unknown_Discriminants (CW, True);
5526 Set_Class_Wide_Type (CW, CW);
5527 Set_Equivalent_Type (CW, Empty);
5528 Set_From_With_Type (CW, From_With_Type (T));
5529 Set_Materialize_Entity (CW, Mark);
5531 -- Link type to its class-wide type
5533 Set_Class_Wide_Type (T, CW);
5535 end Decorate_Tagged_Type;
5537 ------------------------------------
5538 -- Decorate_Package_Specification --
5539 ------------------------------------
5541 procedure Decorate_Package_Specification (P : Entity_Id) is
5543 -- Place only the most basic attributes
5545 Set_Ekind (P, E_Package);
5546 Set_Etype (P, Standard_Void_Type);
5547 end Decorate_Package_Specification;
5549 --------------------------------
5550 -- New_Internal_Shadow_Entity --
5551 --------------------------------
5553 function New_Internal_Shadow_Entity
5554 (Kind : Entity_Kind;
5555 Sloc_Value : Source_Ptr;
5556 Id_Char : Character) return Entity_Id
5558 E : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
5561 Set_Ekind (E, Kind);
5562 Set_Is_Internal (E, True);
5564 if Kind in Type_Kind then
5565 Init_Size_Align (E);
5568 Append_Entity (E, Lim_Header);
5571 end New_Internal_Shadow_Entity;
5573 -- Start of processing for Build_Limited_Views
5576 pragma Assert (Limited_Present (N));
5578 -- A library_item mentioned in a limited_with_clause is a package
5579 -- declaration, not a subprogram declaration, generic declaration,
5580 -- generic instantiation, or package renaming declaration.
5582 case Nkind (Unit (Library_Unit (N))) is
5583 when N_Package_Declaration =>
5586 when N_Subprogram_Declaration =>
5587 Error_Msg_N ("subprograms not allowed in "
5588 & "limited with_clauses", N);
5591 when N_Generic_Package_Declaration |
5592 N_Generic_Subprogram_Declaration =>
5593 Error_Msg_N ("generics not allowed in "
5594 & "limited with_clauses", N);
5597 when N_Generic_Instantiation =>
5598 Error_Msg_N ("generic instantiations not allowed in "
5599 & "limited with_clauses", N);
5602 when N_Generic_Renaming_Declaration =>
5603 Error_Msg_N ("generic renamings not allowed in "
5604 & "limited with_clauses", N);
5607 when N_Subprogram_Renaming_Declaration =>
5608 Error_Msg_N ("renamed subprograms not allowed in "
5609 & "limited with_clauses", N);
5612 when N_Package_Renaming_Declaration =>
5613 Error_Msg_N ("renamed packages not allowed in "
5614 & "limited with_clauses", N);
5618 raise Program_Error;
5621 -- Check if the chain is already built
5623 Spec := Specification (Unit (Library_Unit (N)));
5625 if Limited_View_Installed (Spec) then
5629 Set_Ekind (P, E_Package);
5631 -- Build the header of the limited_view
5633 Lim_Header := Make_Temporary (Sloc (N), 'Z');
5634 Set_Ekind (Lim_Header, E_Package);
5635 Set_Is_Internal (Lim_Header);
5636 Set_Limited_View (P, Lim_Header);
5638 -- Create the auxiliary chain. All the shadow entities are appended to
5639 -- the list of entities of the limited-view header
5643 First_Decl => First (Visible_Declarations (Spec)));
5645 -- Save the last built shadow entity. It is needed later to set the
5646 -- reference to the first shadow entity in the private part
5648 Last_Pub_Lim_E := Last_Lim_E;
5650 -- Ada 2005 (AI-262): Add the limited view of the private declarations
5651 -- Required to give support to limited-private-with clauses
5653 Build_Chain (Scope => P,
5654 First_Decl => First (Private_Declarations (Spec)));
5656 if Last_Pub_Lim_E /= Empty then
5657 Set_First_Private_Entity
5658 (Lim_Header, Next_Entity (Last_Pub_Lim_E));
5660 Set_First_Private_Entity
5661 (Lim_Header, First_Entity (P));
5664 Set_Limited_View_Installed (Spec);
5665 end Build_Limited_Views;
5667 -------------------------------
5668 -- Check_Body_Needed_For_SAL --
5669 -------------------------------
5671 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5673 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5674 -- Determine whether use of entity E might require the presence of its
5675 -- body. For a package this requires a recursive traversal of all nested
5678 ---------------------------
5679 -- Entity_Needed_For_SAL --
5680 ---------------------------
5682 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5686 if Is_Subprogram (E)
5687 and then Has_Pragma_Inline (E)
5691 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
5694 elsif Ekind (E) = E_Generic_Package
5696 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5697 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5701 elsif Ekind (E) = E_Package
5702 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5703 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5705 Ent := First_Entity (E);
5706 while Present (Ent) loop
5707 if Entity_Needs_Body (Ent) then
5719 end Entity_Needs_Body;
5721 -- Start of processing for Check_Body_Needed_For_SAL
5724 if Ekind (Unit_Name) = E_Generic_Package
5725 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5726 N_Generic_Package_Declaration
5728 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5730 Set_Body_Needed_For_SAL (Unit_Name);
5732 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
5733 Set_Body_Needed_For_SAL (Unit_Name);
5735 elsif Is_Subprogram (Unit_Name)
5736 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5737 N_Subprogram_Declaration
5738 and then Has_Pragma_Inline (Unit_Name)
5740 Set_Body_Needed_For_SAL (Unit_Name);
5742 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5743 Check_Body_Needed_For_SAL
5744 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5746 elsif Ekind (Unit_Name) = E_Package
5747 and then Entity_Needs_Body (Unit_Name)
5749 Set_Body_Needed_For_SAL (Unit_Name);
5751 elsif Ekind (Unit_Name) = E_Package_Body
5752 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5754 Check_Body_Needed_For_SAL
5755 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5757 end Check_Body_Needed_For_SAL;
5759 --------------------
5760 -- Remove_Context --
5761 --------------------
5763 procedure Remove_Context (N : Node_Id) is
5764 Lib_Unit : constant Node_Id := Unit (N);
5767 -- If this is a child unit, first remove the parent units
5769 if Is_Child_Spec (Lib_Unit) then
5770 Remove_Parents (Lib_Unit);
5773 Remove_Context_Clauses (N);
5776 ----------------------------
5777 -- Remove_Context_Clauses --
5778 ----------------------------
5780 procedure Remove_Context_Clauses (N : Node_Id) is
5782 Unit_Name : Entity_Id;
5785 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5786 -- limited-views first and regular-views later (to maintain the
5789 -- First Phase: Remove limited_with context clauses
5791 Item := First (Context_Items (N));
5792 while Present (Item) loop
5794 -- We are interested only in with clauses which got installed
5797 if Nkind (Item) = N_With_Clause
5798 and then Limited_Present (Item)
5799 and then Limited_View_Installed (Item)
5801 Remove_Limited_With_Clause (Item);
5807 -- Second Phase: Loop through context items and undo regular
5808 -- with_clauses and use_clauses.
5810 Item := First (Context_Items (N));
5811 while Present (Item) loop
5813 -- We are interested only in with clauses which got installed on
5814 -- entry, as indicated by their Context_Installed flag set
5816 if Nkind (Item) = N_With_Clause
5817 and then Limited_Present (Item)
5818 and then Limited_View_Installed (Item)
5822 elsif Nkind (Item) = N_With_Clause
5823 and then Context_Installed (Item)
5825 -- Remove items from one with'ed unit
5827 Unit_Name := Entity (Name (Item));
5828 Remove_Unit_From_Visibility (Unit_Name);
5829 Set_Context_Installed (Item, False);
5831 elsif Nkind (Item) = N_Use_Package_Clause then
5832 End_Use_Package (Item);
5834 elsif Nkind (Item) = N_Use_Type_Clause then
5835 End_Use_Type (Item);
5840 end Remove_Context_Clauses;
5842 --------------------------------
5843 -- Remove_Limited_With_Clause --
5844 --------------------------------
5846 procedure Remove_Limited_With_Clause (N : Node_Id) is
5847 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5850 Lim_Header : Entity_Id;
5851 Lim_Typ : Entity_Id;
5855 pragma Assert (Limited_View_Installed (N));
5857 -- In case of limited with_clause on subprograms, generics, instances,
5858 -- or renamings, the corresponding error was previously posted and we
5859 -- have nothing to do here.
5861 if Nkind (P_Unit) /= N_Package_Declaration then
5865 P := Defining_Unit_Name (Specification (P_Unit));
5867 -- Handle child packages
5869 if Nkind (P) = N_Defining_Program_Unit_Name then
5870 P := Defining_Identifier (P);
5873 if Debug_Flag_I then
5874 Write_Str ("remove limited view of ");
5875 Write_Name (Chars (P));
5876 Write_Str (" from visibility");
5880 -- Prepare the removal of the shadow entities from visibility. The first
5881 -- element of the limited view is a header (an E_Package entity) that is
5882 -- used to reference the first shadow entity in the private part of the
5885 Lim_Header := Limited_View (P);
5886 Lim_Typ := First_Entity (Lim_Header);
5888 -- Remove package and shadow entities from visibility if it has not
5891 if not Analyzed (P_Unit) then
5893 Set_Is_Immediately_Visible (P, False);
5895 while Present (Lim_Typ) loop
5897 Next_Entity (Lim_Typ);
5900 -- Otherwise this package has already appeared in the closure and its
5901 -- shadow entities must be replaced by its real entities. This code
5902 -- must be kept synchronized with the complementary code in Install
5903 -- Limited_Withed_Unit.
5906 -- Real entities that are type or subtype declarations were hidden
5907 -- from visibility at the point of installation of the limited-view.
5908 -- Now we recover the previous value of the hidden attribute.
5910 E := First_Entity (P);
5911 while Present (E) and then E /= First_Private_Entity (P) loop
5913 Set_Is_Hidden (E, Was_Hidden (E));
5919 while Present (Lim_Typ)
5920 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5922 -- Nested packages and child units were not unchained
5924 if Ekind (Lim_Typ) /= E_Package
5925 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5927 -- If the package has incomplete types, the limited view of the
5928 -- incomplete type is in fact never visible (AI05-129) but we
5929 -- have created a shadow entity E1 for it, that points to E2,
5930 -- a non-limited incomplete type. This in turn has a full view
5931 -- E3 that is the full declaration. There is a corresponding
5932 -- shadow entity E4. When reinstalling the non-limited view,
5933 -- E2 must become the current entity and E3 must be ignored.
5935 E := Non_Limited_View (Lim_Typ);
5937 if Present (Current_Entity (E))
5938 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
5939 and then Full_View (Current_Entity (E)) = E
5942 -- Lim_Typ is the limited view of a full type declaration
5943 -- that has a previous incomplete declaration, i.e. E3 from
5944 -- the previous description. Nothing to insert.
5949 pragma Assert (not In_Chain (E));
5951 Prev := Current_Entity (Lim_Typ);
5953 if Prev = Lim_Typ then
5954 Set_Current_Entity (E);
5957 while Present (Prev)
5958 and then Homonym (Prev) /= Lim_Typ
5960 Prev := Homonym (Prev);
5963 if Present (Prev) then
5964 Set_Homonym (Prev, E);
5968 -- Preserve structure of homonym chain
5970 Set_Homonym (E, Homonym (Lim_Typ));
5974 Next_Entity (Lim_Typ);
5978 -- Indicate that the limited view of the package is not installed
5980 Set_From_With_Type (P, False);
5981 Set_Limited_View_Installed (N, False);
5982 end Remove_Limited_With_Clause;
5984 --------------------
5985 -- Remove_Parents --
5986 --------------------
5988 procedure Remove_Parents (Lib_Unit : Node_Id) is
5991 P_Spec : Node_Id := Empty;
5993 Vis : constant Boolean :=
5994 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
5997 if Is_Child_Spec (Lib_Unit) then
5998 P_Spec := Parent_Spec (Lib_Unit);
6000 elsif Nkind (Lib_Unit) = N_Package_Body
6001 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6003 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6006 if Present (P_Spec) then
6008 P_Name := Get_Parent_Entity (P);
6009 Remove_Context_Clauses (P_Spec);
6010 End_Package_Scope (P_Name);
6011 Set_Is_Immediately_Visible (P_Name, Vis);
6013 -- Remove from visibility the siblings as well, which are directly
6014 -- visible while the parent is in scope.
6016 E := First_Entity (P_Name);
6017 while Present (E) loop
6018 if Is_Child_Unit (E) then
6019 Set_Is_Immediately_Visible (E, False);
6025 Set_In_Package_Body (P_Name, False);
6027 -- This is the recursive call to remove the context of any higher
6028 -- level parent. This recursion ensures that all parents are removed
6029 -- in the reverse order of their installation.
6035 ---------------------------------
6036 -- Remove_Private_With_Clauses --
6037 ---------------------------------
6039 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6042 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6043 -- Check whether a given unit appears in a regular with_clause. Used to
6044 -- determine whether a private_with_clause, implicit or explicit, should
6047 ----------------------------
6048 -- In_Regular_With_Clause --
6049 ----------------------------
6051 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6056 Item := First (Context_Items (Comp_Unit));
6057 while Present (Item) loop
6058 if Nkind (Item) = N_With_Clause
6059 and then Entity (Name (Item)) = E
6060 and then not Private_Present (Item)
6068 end In_Regular_With_Clause;
6070 -- Start of processing for Remove_Private_With_Clauses
6073 Item := First (Context_Items (Comp_Unit));
6074 while Present (Item) loop
6075 if Nkind (Item) = N_With_Clause
6076 and then Private_Present (Item)
6078 -- If private_with_clause is redundant, remove it from context,
6079 -- as a small optimization to subsequent handling of private_with
6080 -- clauses in other nested packages.
6082 if In_Regular_With_Clause (Entity (Name (Item))) then
6084 Nxt : constant Node_Id := Next (Item);
6090 elsif Limited_Present (Item) then
6091 if not Limited_View_Installed (Item) then
6092 Remove_Limited_With_Clause (Item);
6098 Remove_Unit_From_Visibility (Entity (Name (Item)));
6099 Set_Context_Installed (Item, False);
6107 end Remove_Private_With_Clauses;
6109 ---------------------------------
6110 -- Remove_Unit_From_Visibility --
6111 ---------------------------------
6113 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6114 P : constant Entity_Id := Scope (Unit_Name);
6117 if Debug_Flag_I then
6118 Write_Str ("remove unit ");
6119 Write_Name (Chars (Unit_Name));
6120 Write_Str (" from visibility");
6124 if P /= Standard_Standard then
6125 Set_Is_Visible_Child_Unit (Unit_Name, False);
6128 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6129 Set_Is_Immediately_Visible (Unit_Name, False);
6130 end Remove_Unit_From_Visibility;
6145 procedure Unchain (E : Entity_Id) is
6149 Prev := Current_Entity (E);
6155 Set_Name_Entity_Id (Chars (E), Homonym (E));
6158 while Present (Prev)
6159 and then Homonym (Prev) /= E
6161 Prev := Homonym (Prev);
6164 if Present (Prev) then
6165 Set_Homonym (Prev, Homonym (E));
6169 if Debug_Flag_I then
6170 Write_Str (" (homonym) unchain ");
6171 Write_Name (Chars (E));