1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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
90 -- unit 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 only 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
191 -- main 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
199 -- name is established, load and analyze. Nam is the non-overloadable
200 -- entity for which the proper body provides a completion. Subprogram
201 -- stubs are 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
223 -- types that are in one-one correspondence with the real types in the
224 -- package, 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
276 -- the name entity of Clause with any list element. If the match
277 -- occurs on a use package clause, set Used to True, for a use
278 -- type clause, pragma Elaborate or pragma Elaborate_All, set
279 -- Used_Type_Or_Elab to True.
281 procedure Process_Spec_Clauses
282 (Context_List : List_Id;
284 Used : in out Boolean;
285 Withed : in out Boolean;
286 Exit_On_Self : Boolean := False);
287 -- Examine the context clauses of a package spec, trying to match
288 -- the name entity of Clause with any list element. If the match
289 -- occurs on a use package clause, set Used to True, for a with
290 -- package clause other than Clause, set Withed to True. Limited
291 -- with clauses, implicitly generated with clauses and withs
292 -- having pragmas Elaborate or Elaborate_All applied to them are
293 -- skipped. Exit_On_Self is used to control the search loop and
294 -- force an exit whenever Clause sees itself in the search.
296 --------------------------
297 -- Process_Body_Clauses --
298 --------------------------
300 procedure Process_Body_Clauses
301 (Context_List : List_Id;
303 Used : in out Boolean;
304 Used_Type_Or_Elab : in out Boolean)
306 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
312 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
313 -- In an expanded name in a use clause, if the prefix is a
314 -- renamed package, the entity is set to the original package
315 -- as a result, when checking whether the package appears in a
316 -- previous with_clause, the renaming has to be taken into
317 -- account, to prevent spurious or incorrect warnings. The
318 -- common case is the use of Text_IO.
324 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
326 return Entity (N) = P
328 (Present (Renamed_Object (P))
329 and then Entity (N) = Renamed_Object (P));
332 -- Start of processing for Process_Body_Clauses
336 Used_Type_Or_Elab := False;
338 Cont_Item := First (Context_List);
339 while Present (Cont_Item) loop
341 -- Package use clause
343 if Nkind (Cont_Item) = N_Use_Package_Clause
346 -- Search through use clauses
348 Use_Item := First (Names (Cont_Item));
349 while Present (Use_Item) and then not Used loop
351 -- Case of a direct use of the one we are looking for
353 if Entity (Use_Item) = Nam_Ent then
356 -- Handle nested case, as in "with P; use P.Q.R"
363 -- Loop through prefixes looking for match
366 while Nkind (UE) = N_Expanded_Name loop
367 if Same_Unit (Prefix (UE), Nam_Ent) then
382 elsif Nkind (Cont_Item) = N_Use_Type_Clause
383 and then not Used_Type_Or_Elab
385 Subt_Mark := First (Subtype_Marks (Cont_Item));
386 while Present (Subt_Mark)
387 and then not Used_Type_Or_Elab
389 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
390 Used_Type_Or_Elab := True;
396 -- Pragma Elaborate or Elaborate_All
398 elsif Nkind (Cont_Item) = N_Pragma
400 (Pragma_Name (Cont_Item) = Name_Elaborate
402 Pragma_Name (Cont_Item) = Name_Elaborate_All)
403 and then not Used_Type_Or_Elab
406 First (Pragma_Argument_Associations (Cont_Item));
407 while Present (Prag_Unit)
408 and then not Used_Type_Or_Elab
410 if Entity (Expression (Prag_Unit)) = Nam_Ent then
411 Used_Type_Or_Elab := True;
420 end Process_Body_Clauses;
422 --------------------------
423 -- Process_Spec_Clauses --
424 --------------------------
426 procedure Process_Spec_Clauses
427 (Context_List : List_Id;
429 Used : in out Boolean;
430 Withed : in out Boolean;
431 Exit_On_Self : Boolean := False)
433 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
441 Cont_Item := First (Context_List);
442 while Present (Cont_Item) loop
444 -- Stop the search since the context items after Cont_Item
445 -- have already been examined in a previous iteration of
446 -- the reverse loop in Check_Redundant_Withs.
449 and Cont_Item = Clause
454 -- Package use clause
456 if Nkind (Cont_Item) = N_Use_Package_Clause
459 Use_Item := First (Names (Cont_Item));
460 while Present (Use_Item) and then not Used loop
461 if Entity (Use_Item) = Nam_Ent then
468 -- Package with clause. Avoid processing self, implicitly
469 -- generated with clauses or limited with clauses. Note
470 -- that we examine with clauses having pragmas Elaborate
471 -- or Elaborate_All applied to them due to cases such as:
475 -- pragma Elaborate (Pack);
477 -- In this case, the second with clause is redundant since
478 -- the pragma applies only to the first "with Pack;".
480 elsif Nkind (Cont_Item) = N_With_Clause
481 and then not Implicit_With (Cont_Item)
482 and then not Limited_Present (Cont_Item)
483 and then Cont_Item /= Clause
484 and then Entity (Name (Cont_Item)) = Nam_Ent
491 end Process_Spec_Clauses;
493 -- Start of processing for Check_Redundant_Withs
496 Clause := Last (Context_Items);
497 while Present (Clause) loop
499 -- Avoid checking implicitly generated with clauses, limited
500 -- with clauses or withs that have pragma Elaborate or
501 -- Elaborate_All applied.
503 if Nkind (Clause) = N_With_Clause
504 and then not Implicit_With (Clause)
505 and then not Limited_Present (Clause)
506 and then not Elaborate_Present (Clause)
508 -- Package body-to-spec check
510 if Present (Spec_Context_Items) then
512 Used_In_Body : Boolean := False;
513 Used_In_Spec : Boolean := False;
514 Used_Type_Or_Elab : Boolean := False;
515 Withed_In_Spec : Boolean := False;
519 (Context_List => Spec_Context_Items,
521 Used => Used_In_Spec,
522 Withed => Withed_In_Spec);
525 (Context_List => Context_Items,
527 Used => Used_In_Body,
528 Used_Type_Or_Elab => Used_Type_Or_Elab);
530 -- "Type Elab" refers to the presence of either a use
531 -- type clause, pragmas Elaborate or Elaborate_All.
533 -- +---------------+---------------------------+------+
534 -- | Spec | Body | Warn |
535 -- +--------+------+--------+------+-----------+------+
536 -- | Withed | Used | Withed | Used | Type Elab | |
537 -- | X | | X | | | X |
538 -- | X | | X | X | | |
539 -- | X | | X | | X | |
540 -- | X | | X | X | X | |
541 -- | X | X | X | | | X |
542 -- | X | X | X | | X | |
543 -- | X | X | X | X | | X |
544 -- | X | X | X | X | X | |
545 -- +--------+------+--------+------+-----------+------+
548 and then not Used_Type_Or_Elab)
551 and then not Used_In_Body)
555 Error_Msg_N -- CODEFIX
556 ("?redundant with clause in body", Clause);
559 Used_In_Body := False;
560 Used_In_Spec := False;
561 Used_Type_Or_Elab := False;
562 Withed_In_Spec := False;
565 -- Standalone package spec or body check
569 Dont_Care : Boolean := False;
570 Withed : Boolean := False;
573 -- The mechanism for examining the context clauses of a
574 -- package spec can be applied to package body clauses.
577 (Context_List => Context_Items,
581 Exit_On_Self => True);
584 Error_Msg_N -- CODEFIX
585 ("?redundant with clause", Clause);
593 end Check_Redundant_Withs;
595 --------------------------------
596 -- Generate_Parent_References --
597 --------------------------------
599 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
601 P_Name : Entity_Id := P_Id;
604 Pref := Name (Parent (Defining_Entity (N)));
606 if Nkind (Pref) = N_Expanded_Name then
608 -- Done already, if the unit has been compiled indirectly as
609 -- part of the closure of its context because of inlining.
614 while Nkind (Pref) = N_Selected_Component loop
615 Change_Selected_Component_To_Expanded_Name (Pref);
616 Set_Entity (Pref, P_Name);
617 Set_Etype (Pref, Etype (P_Name));
618 Generate_Reference (P_Name, Pref, 'r');
619 Pref := Prefix (Pref);
620 P_Name := Scope (P_Name);
623 -- The guard here on P_Name is to handle the error condition where
624 -- the parent unit is missing because the file was not found.
626 if Present (P_Name) then
627 Set_Entity (Pref, P_Name);
628 Set_Etype (Pref, Etype (P_Name));
629 Generate_Reference (P_Name, Pref, 'r');
630 Style.Check_Identifier (Pref, P_Name);
632 end Generate_Parent_References;
634 -- Start of processing for Analyze_Compilation_Unit
637 Process_Compilation_Unit_Pragmas (N);
639 -- If the unit is a subunit whose parent has not been analyzed (which
640 -- indicates that the main unit is a subunit, either the current one or
641 -- one of its descendents) then the subunit is compiled as part of the
642 -- analysis of the parent, which we proceed to do. Basically this gets
643 -- handled from the top down and we don't want to do anything at this
644 -- level (i.e. this subunit will be handled on the way down from the
645 -- parent), so at this level we immediately return. If the subunit
646 -- ends up not analyzed, it means that the parent did not contain a
647 -- stub for it, or that there errors were detected in some ancestor.
649 if Nkind (Unit_Node) = N_Subunit
650 and then not Analyzed (Lib_Unit)
652 Semantics (Lib_Unit);
654 if not Analyzed (Proper_Body (Unit_Node)) then
655 if Serious_Errors_Detected > 0 then
656 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
658 Error_Msg_N ("missing stub for subunit", N);
665 -- Analyze context (this will call Sem recursively for with'ed units)
666 -- To detect circularities among with-clauses that are not caught during
667 -- loading, we set the Context_Pending flag on the current unit. If the
668 -- flag is already set there is a potential circularity.
669 -- We exclude predefined units from this check because they are known
670 -- to be safe. We also exclude package bodies that are present because
671 -- circularities between bodies are harmless (and necessary).
673 if Context_Pending (N) then
675 Circularity : Boolean := True;
678 if Is_Predefined_File_Name
679 (Unit_File_Name (Get_Source_Unit (Unit (N))))
681 Circularity := False;
684 for U in Main_Unit + 1 .. Last_Unit loop
685 if Nkind (Unit (Cunit (U))) = N_Package_Body
686 and then not Analyzed (Cunit (U))
688 Circularity := False;
695 Error_Msg_N ("circular dependency caused by with_clauses", N);
697 ("\possibly missing limited_with clause"
698 & " in one of the following", N);
700 for U in Main_Unit .. Last_Unit loop
701 if Context_Pending (Cunit (U)) then
702 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
703 Error_Msg_N ("\unit$", N);
707 raise Unrecoverable_Error;
711 Set_Context_Pending (N);
716 Set_Context_Pending (N, False);
718 -- If the unit is a package body, the spec is already loaded and must be
719 -- analyzed first, before we analyze the body.
721 if Nkind (Unit_Node) = N_Package_Body then
723 -- If no Lib_Unit, then there was a serious previous error, so just
724 -- ignore the entire analysis effort
726 if No (Lib_Unit) then
730 Semantics (Lib_Unit);
731 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
733 -- Verify that the library unit is a package declaration
735 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
736 N_Generic_Package_Declaration)
739 ("no legal package declaration for package body", N);
742 -- Otherwise, the entity in the declaration is visible. Update the
743 -- version to reflect dependence of this body on the spec.
746 Spec_Id := Defining_Entity (Unit (Lib_Unit));
747 Set_Is_Immediately_Visible (Spec_Id, True);
748 Version_Update (N, Lib_Unit);
750 if Nkind (Defining_Unit_Name (Unit_Node)) =
751 N_Defining_Program_Unit_Name
753 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
758 -- If the unit is a subprogram body, then we similarly need to analyze
759 -- its spec. However, things are a little simpler in this case, because
760 -- here, this analysis is done only for error checking and consistency
761 -- purposes, so there's nothing else to be done.
763 elsif Nkind (Unit_Node) = N_Subprogram_Body then
764 if Acts_As_Spec (N) then
766 -- If the subprogram body is a child unit, we must create a
767 -- declaration for it, in order to properly load the parent(s).
768 -- After this, the original unit does not acts as a spec, because
769 -- there is an explicit one. If this unit appears in a context
770 -- clause, then an implicit with on the parent will be added when
771 -- installing the context. If this is the main unit, there is no
772 -- Unit_Table entry for the declaration (it has the unit number
773 -- of the main unit) and code generation is unaffected.
775 Unum := Get_Cunit_Unit_Number (N);
776 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
778 if Par_Spec_Name /= No_Unit_Name then
781 (Load_Name => Par_Spec_Name,
786 if Unum /= No_Unit then
788 -- Build subprogram declaration and attach parent unit to it
789 -- This subprogram declaration does not come from source,
790 -- Nevertheless the backend must generate debugging info for
791 -- it, and this must be indicated explicitly. We also mark
792 -- the body entity as a child unit now, to prevent a
793 -- cascaded error if the spec entity cannot be entered
794 -- in its scope. Finally we create a Units table entry for
795 -- the subprogram declaration, to maintain a one-to-one
796 -- correspondence with compilation unit nodes. This is
797 -- critical for the tree traversals performed by CodePeer.
800 Loc : constant Source_Ptr := Sloc (N);
801 SCS : constant Boolean :=
802 Get_Comes_From_Source_Default;
805 Set_Comes_From_Source_Default (False);
807 Make_Compilation_Unit (Loc,
808 Context_Items => New_Copy_List (Context_Items (N)),
810 Make_Subprogram_Declaration (Sloc (N),
813 (Specification (Unit_Node))),
815 Make_Compilation_Unit_Aux (Loc));
817 Set_Library_Unit (N, Lib_Unit);
818 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
819 Make_Child_Decl_Unit (N);
820 Semantics (Lib_Unit);
822 -- Now that a separate declaration exists, the body
823 -- of the child unit does not act as spec any longer.
825 Set_Acts_As_Spec (N, False);
826 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
827 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
828 Set_Comes_From_Source_Default (SCS);
833 -- Here for subprogram with separate declaration
836 Semantics (Lib_Unit);
837 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
838 Version_Update (N, Lib_Unit);
841 -- If this is a child unit, generate references to the parents
843 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
844 N_Defining_Program_Unit_Name
846 Generate_Parent_References (
847 Specification (Unit_Node),
848 Scope (Defining_Entity (Unit (Lib_Unit))));
852 -- If it is a child unit, the parent must be elaborated first and we
853 -- update version, since we are dependent on our parent.
855 if Is_Child_Spec (Unit_Node) then
857 -- The analysis of the parent is done with style checks off
860 Save_Style_Check : constant Boolean := Style_Check;
861 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
862 Cunit_Boolean_Restrictions_Save;
865 if not GNAT_Mode then
866 Style_Check := False;
869 Semantics (Parent_Spec (Unit_Node));
870 Version_Update (N, Parent_Spec (Unit_Node));
871 Style_Check := Save_Style_Check;
872 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
876 -- With the analysis done, install the context. Note that we can't
877 -- install the context from the with clauses as we analyze them, because
878 -- each with clause must be analyzed in a clean visibility context, so
879 -- we have to wait and install them all at once.
883 if Is_Child_Spec (Unit_Node) then
885 -- Set the entities of all parents in the program_unit_name
887 Generate_Parent_References (
888 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
891 -- All components of the context: with-clauses, library unit, ancestors
892 -- if any, (and their context) are analyzed and installed.
894 -- Call special debug routine sm if this is the main unit
896 if Current_Sem_Unit = Main_Unit then
900 -- Now analyze the unit (package, subprogram spec, body) itself
904 if Warn_On_Redundant_Constructs then
905 Check_Redundant_Withs (Context_Items (N));
907 if Nkind (Unit_Node) = N_Package_Body then
908 Check_Redundant_Withs
909 (Context_Items => Context_Items (N),
910 Spec_Context_Items => Context_Items (Lib_Unit));
914 -- The above call might have made Unit_Node an N_Subprogram_Body from
915 -- something else, so propagate any Acts_As_Spec flag.
917 if Nkind (Unit_Node) = N_Subprogram_Body
918 and then Acts_As_Spec (Unit_Node)
920 Set_Acts_As_Spec (N);
923 -- Register predefined units in Rtsfind
926 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
928 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
929 Set_RTU_Loaded (Unit_Node);
933 -- Treat compilation unit pragmas that appear after the library unit
935 if Present (Pragmas_After (Aux_Decls_Node (N))) then
937 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
939 while Present (Prag_Node) loop
946 -- Generate distribution stubs if requested and no error
949 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
951 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
952 and then not Fatal_Error (Main_Unit)
954 if Is_RCI_Pkg_Spec_Or_Body (N) then
956 -- Regular RCI package
958 Add_Stub_Constructs (N);
960 elsif (Nkind (Unit_Node) = N_Package_Declaration
961 and then Is_Shared_Passive (Defining_Entity
962 (Specification (Unit_Node))))
963 or else (Nkind (Unit_Node) = N_Package_Body
965 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
967 -- Shared passive package
969 Add_Stub_Constructs (N);
971 elsif Nkind (Unit_Node) = N_Package_Instantiation
973 Is_Remote_Call_Interface
974 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
976 -- Instantiation of a RCI generic package
978 Add_Stub_Constructs (N);
982 -- Remove unit from visibility, so that environment is clean for
983 -- the next compilation, which is either the main unit or some
984 -- other unit in the context.
986 if Nkind_In (Unit_Node, N_Package_Declaration,
987 N_Package_Renaming_Declaration,
988 N_Subprogram_Declaration)
989 or else Nkind (Unit_Node) in N_Generic_Declaration
991 (Nkind (Unit_Node) = N_Subprogram_Body
992 and then Acts_As_Spec (Unit_Node))
994 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
996 -- If the unit is an instantiation whose body will be elaborated for
997 -- inlining purposes, use the proper entity of the instance. The
998 -- entity may be missing if the instantiation was illegal.
1000 elsif Nkind (Unit_Node) = N_Package_Instantiation
1001 and then not Error_Posted (Unit_Node)
1002 and then Present (Instance_Spec (Unit_Node))
1004 Remove_Unit_From_Visibility
1005 (Defining_Entity (Instance_Spec (Unit_Node)));
1007 elsif Nkind (Unit_Node) = N_Package_Body
1008 or else (Nkind (Unit_Node) = N_Subprogram_Body
1009 and then not Acts_As_Spec (Unit_Node))
1011 -- Bodies that are not the main unit are compiled if they are generic
1012 -- or contain generic or inlined units. Their analysis brings in the
1013 -- context of the corresponding spec (unit declaration) which must be
1014 -- removed as well, to return the compilation environment to its
1017 Remove_Context (Lib_Unit);
1018 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1021 -- Last step is to deinstall the context we just installed as well as
1022 -- the unit just compiled.
1026 -- If this is the main unit and we are generating code, we must check
1027 -- that all generic units in the context have a body if they need it,
1028 -- even if they have not been instantiated. In the absence of .ali files
1029 -- for generic units, we must force the load of the body, just to
1030 -- produce the proper error if the body is absent. We skip this
1031 -- verification if the main unit itself is generic.
1033 if Get_Cunit_Unit_Number (N) = Main_Unit
1034 and then Operating_Mode = Generate_Code
1035 and then Expander_Active
1037 -- Check whether the source for the body of the unit must be included
1038 -- in a standalone library.
1040 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1042 -- Indicate that the main unit is now analyzed, to catch possible
1043 -- circularities between it and generic bodies. Remove main unit from
1044 -- visibility. This might seem superfluous, but the main unit must
1045 -- not be visible in the generic body expansions that follow.
1047 Set_Analyzed (N, True);
1048 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1053 Un : Unit_Number_Type;
1055 Save_Style_Check : constant Boolean := Style_Check;
1056 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
1057 Cunit_Boolean_Restrictions_Save;
1060 Item := First (Context_Items (N));
1061 while Present (Item) loop
1063 -- Check for explicit with clause
1065 if Nkind (Item) = N_With_Clause
1066 and then not Implicit_With (Item)
1068 -- Ada 2005 (AI-50217): Ignore limited-withed units
1070 and then not Limited_Present (Item)
1072 Nam := Entity (Name (Item));
1074 -- Compile generic subprogram, unless it is intrinsic or
1075 -- imported so no body is required, or generic package body
1076 -- if the package spec requires a body.
1078 if (Is_Generic_Subprogram (Nam)
1079 and then not Is_Intrinsic_Subprogram (Nam)
1080 and then not Is_Imported (Nam))
1081 or else (Ekind (Nam) = E_Generic_Package
1082 and then Unit_Requires_Body (Nam))
1084 Style_Check := False;
1086 if Present (Renamed_Object (Nam)) then
1089 (Load_Name => Get_Body_Name
1091 (Unit_Declaration_Node
1092 (Renamed_Object (Nam)))),
1100 (Load_Name => Get_Body_Name
1101 (Get_Unit_Name (Item)),
1108 if Un = No_Unit then
1110 ("body of generic unit& not found", Item, Nam);
1113 elsif not Analyzed (Cunit (Un))
1114 and then Un /= Main_Unit
1115 and then not Fatal_Error (Un)
1117 Style_Check := False;
1118 Semantics (Cunit (Un));
1126 Style_Check := Save_Style_Check;
1127 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1131 -- Deal with creating elaboration Boolean if needed. We create an
1132 -- elaboration boolean only for units that come from source since
1133 -- units manufactured by the compiler never need elab checks.
1135 if Comes_From_Source (N)
1136 and then Nkind_In (Unit_Node, N_Package_Declaration,
1137 N_Generic_Package_Declaration,
1138 N_Subprogram_Declaration,
1139 N_Generic_Subprogram_Declaration)
1142 Loc : constant Source_Ptr := Sloc (N);
1143 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1146 Spec_Id := Defining_Entity (Unit_Node);
1147 Generate_Definition (Spec_Id);
1149 -- See if an elaboration entity is required for possible access
1150 -- before elaboration checking. Note that we must allow for this
1151 -- even if -gnatE is not set, since a client may be compiled in
1152 -- -gnatE mode and reference the entity.
1154 -- These entities are also used by the binder to prevent multiple
1155 -- attempts to execute the elaboration code for the library case
1156 -- where the elaboration routine might otherwise be called more
1159 -- Case of units which do not require elaboration checks
1162 -- Pure units do not need checks
1166 -- Preelaborated units do not need checks
1168 or else Is_Preelaborated (Spec_Id)
1170 -- No checks needed if pragma Elaborate_Body present
1172 or else Has_Pragma_Elaborate_Body (Spec_Id)
1174 -- No checks needed if unit does not require a body
1176 or else not Unit_Requires_Body (Spec_Id)
1178 -- No checks needed for predefined files
1180 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1182 -- No checks required if no separate spec
1184 or else Acts_As_Spec (N)
1186 -- This is a case where we only need the entity for
1187 -- checking to prevent multiple elaboration checks.
1189 Set_Elaboration_Entity_Required (Spec_Id, False);
1191 -- Case of elaboration entity is required for access before
1192 -- elaboration checking (so certainly we must build it!)
1195 Set_Elaboration_Entity_Required (Spec_Id, True);
1198 Build_Elaboration_Entity (N, Spec_Id);
1202 -- Freeze the compilation unit entity. This for sure is needed because
1203 -- of some warnings that can be output (see Freeze_Subprogram), but may
1204 -- in general be required. If freezing actions result, place them in the
1205 -- compilation unit actions list, and analyze them.
1208 Loc : constant Source_Ptr := Sloc (N);
1209 L : constant List_Id :=
1210 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), Loc);
1212 while Is_Non_Empty_List (L) loop
1213 Insert_Library_Level_Action (Remove_Head (L));
1219 if Nkind (Unit_Node) = N_Package_Declaration
1220 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1221 and then Expander_Active
1224 Save_Style_Check : constant Boolean := Style_Check;
1225 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1226 Options : Style_Check_Options;
1229 Save_Style_Check_Options (Options);
1230 Reset_Style_Check_Options;
1231 Opt.Warning_Mode := Suppress;
1232 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1234 Reset_Style_Check_Options;
1235 Set_Style_Check_Options (Options);
1236 Style_Check := Save_Style_Check;
1237 Warning_Mode := Save_Warning;
1241 -- If we are generating obsolescent warnings, then here is where we
1242 -- generate them for the with'ed items. The reason for this special
1243 -- processing is that the normal mechanism of generating the warnings
1244 -- for referenced entities does not work for context clause references.
1245 -- That's because when we first analyze the context, it is too early to
1246 -- know if the with'ing unit is itself obsolescent (which suppresses
1249 if not GNAT_Mode and then Warn_On_Obsolescent_Feature then
1251 -- Push current compilation unit as scope, so that the test for
1252 -- being within an obsolescent unit will work correctly.
1254 Push_Scope (Defining_Entity (Unit_Node));
1256 -- Loop through context items to deal with with clauses
1264 Item := First (Context_Items (N));
1265 while Present (Item) loop
1266 if Nkind (Item) = N_With_Clause
1268 -- Suppress this check in limited-withed units. Further work
1269 -- needed here if we decide to incorporate this check on
1270 -- limited-withed units.
1272 and then not Limited_Present (Item)
1275 Ent := Entity (Nam);
1277 if Is_Obsolescent (Ent) then
1278 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1286 -- Remove temporary install of current unit as scope
1290 end Analyze_Compilation_Unit;
1292 ---------------------
1293 -- Analyze_Context --
1294 ---------------------
1296 procedure Analyze_Context (N : Node_Id) is
1297 Ukind : constant Node_Kind := Nkind (Unit (N));
1301 -- First process all configuration pragmas at the start of the context
1302 -- items. Strictly these are not part of the context clause, but that
1303 -- is where the parser puts them. In any case for sure we must analyze
1304 -- these before analyzing the actual context items, since they can have
1305 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1306 -- be with'ed as a result of changing categorizations in Ada 2005).
1308 Item := First (Context_Items (N));
1309 while Present (Item)
1310 and then Nkind (Item) = N_Pragma
1311 and then Pragma_Name (Item) in Configuration_Pragma_Names
1317 -- This is the point at which we capture the configuration settings
1318 -- for the unit. At the moment only the Optimize_Alignment setting
1319 -- needs to be captured. Probably more later ???
1321 if Optimize_Alignment_Local then
1322 Set_OA_Setting (Current_Sem_Unit, 'L');
1324 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1327 -- Loop through actual context items. This is done in two passes:
1329 -- a) The first pass analyzes non-limited with-clauses and also any
1330 -- configuration pragmas (we need to get the latter analyzed right
1331 -- away, since they can affect processing of subsequent items.
1333 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1335 while Present (Item) loop
1337 -- For with clause, analyze the with clause, and then update the
1338 -- version, since we are dependent on a unit that we with.
1340 if Nkind (Item) = N_With_Clause
1341 and then not Limited_Present (Item)
1343 -- Skip analyzing with clause if no unit, nothing to do (this
1344 -- happens for a with that references a non-existent unit). Skip
1345 -- as well if this is a with_clause for the main unit, which
1346 -- happens if a subunit has a useless with_clause on its parent.
1348 if Present (Library_Unit (Item)) then
1349 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1353 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1357 if not Implicit_With (Item) then
1358 Version_Update (N, Library_Unit (Item));
1361 -- Skip pragmas. Configuration pragmas at the start were handled in
1362 -- the loop above, and remaining pragmas are not processed until we
1363 -- actually install the context (see Install_Context). We delay the
1364 -- analysis of these pragmas to make sure that we have installed all
1365 -- the implicit with's on parent units.
1367 -- Skip use clauses at this stage, since we don't want to do any
1368 -- installing of potentially use-visible entities until we
1369 -- actually install the complete context (in Install_Context).
1370 -- Otherwise things can get installed in the wrong context.
1379 -- Second pass: examine all limited_with clauses. All other context
1380 -- items are ignored in this pass.
1382 Item := First (Context_Items (N));
1383 while Present (Item) loop
1384 if Nkind (Item) = N_With_Clause
1385 and then Limited_Present (Item)
1387 -- No need to check errors on implicitly generated limited-with
1390 if not Implicit_With (Item) then
1392 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1393 -- properly rejected, including renaming declarations.
1395 if not Nkind_In (Ukind, N_Package_Declaration,
1396 N_Subprogram_Declaration)
1397 and then Ukind not in N_Generic_Declaration
1398 and then Ukind not in N_Generic_Instantiation
1400 Error_Msg_N ("limited with_clause not allowed here", Item);
1402 -- Check wrong use of a limited with clause applied to the
1403 -- compilation unit containing the limited-with clause.
1405 -- limited with P.Q;
1406 -- package P.Q is ...
1408 elsif Unit (Library_Unit (Item)) = Unit (N) then
1409 Error_Msg_N ("wrong use of limited-with clause", Item);
1411 -- Check wrong use of limited-with clause applied to some
1412 -- immediate ancestor.
1414 elsif Is_Child_Spec (Unit (N)) then
1416 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1420 P := Parent_Spec (Unit (N));
1422 if Unit (P) = Lib_U then
1423 Error_Msg_N ("limited with_clause of immediate "
1424 & "ancestor not allowed", Item);
1428 exit when not Is_Child_Spec (Unit (P));
1429 P := Parent_Spec (Unit (P));
1434 -- Check if the limited-withed unit is already visible through
1435 -- some context clause of the current compilation unit or some
1436 -- ancestor of the current compilation unit.
1439 Lim_Unit_Name : constant Node_Id := Name (Item);
1440 Comp_Unit : Node_Id;
1442 Unit_Name : Node_Id;
1447 It := First (Context_Items (Comp_Unit));
1448 while Present (It) loop
1450 and then Nkind (It) = N_With_Clause
1451 and then not Limited_Present (It)
1453 Nkind_In (Unit (Library_Unit (It)),
1454 N_Package_Declaration,
1455 N_Package_Renaming_Declaration)
1457 if Nkind (Unit (Library_Unit (It))) =
1458 N_Package_Declaration
1460 Unit_Name := Name (It);
1462 Unit_Name := Name (Unit (Library_Unit (It)));
1465 -- Check if the named package (or some ancestor)
1466 -- leaves visible the full-view of the unit given
1467 -- in the limited-with clause
1470 if Designate_Same_Unit (Lim_Unit_Name,
1473 Error_Msg_Sloc := Sloc (It);
1475 ("simultaneous visibility of limited "
1476 & "and unlimited views not allowed",
1479 ("\unlimited view visible through "
1480 & "context clause #",
1484 elsif Nkind (Unit_Name) = N_Identifier then
1488 Unit_Name := Prefix (Unit_Name);
1495 exit when not Is_Child_Spec (Unit (Comp_Unit));
1497 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1502 -- Skip analyzing with clause if no unit, see above
1504 if Present (Library_Unit (Item)) then
1508 -- A limited_with does not impose an elaboration order, but
1509 -- there is a semantic dependency for recompilation purposes.
1511 if not Implicit_With (Item) then
1512 Version_Update (N, Library_Unit (Item));
1515 -- Pragmas and use clauses and with clauses other than limited
1516 -- with's are ignored in this pass through the context items.
1524 end Analyze_Context;
1526 -------------------------------
1527 -- Analyze_Package_Body_Stub --
1528 -------------------------------
1530 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1531 Id : constant Entity_Id := Defining_Identifier (N);
1535 -- The package declaration must be in the current declarative part
1537 Check_Stub_Level (N);
1538 Nam := Current_Entity_In_Scope (Id);
1540 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1541 Error_Msg_N ("missing specification for package stub", N);
1543 elsif Has_Completion (Nam)
1544 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1546 Error_Msg_N ("duplicate or redundant stub for package", N);
1549 -- Indicate that the body of the package exists. If we are doing
1550 -- only semantic analysis, the stub stands for the body. If we are
1551 -- generating code, the existence of the body will be confirmed
1552 -- when we load the proper body.
1554 Set_Has_Completion (Nam);
1555 Set_Scope (Defining_Entity (N), Current_Scope);
1556 Generate_Reference (Nam, Id, 'b');
1557 Analyze_Proper_Body (N, Nam);
1559 end Analyze_Package_Body_Stub;
1561 -------------------------
1562 -- Analyze_Proper_Body --
1563 -------------------------
1565 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1566 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1567 Unum : Unit_Number_Type;
1569 procedure Optional_Subunit;
1570 -- This procedure is called when the main unit is a stub, or when we
1571 -- are not generating code. In such a case, we analyze the subunit if
1572 -- present, which is user-friendly and in fact required for ASIS, but
1573 -- we don't complain if the subunit is missing.
1575 ----------------------
1576 -- Optional_Subunit --
1577 ----------------------
1579 procedure Optional_Subunit is
1580 Comp_Unit : Node_Id;
1583 -- Try to load subunit, but ignore any errors that occur during
1584 -- the loading of the subunit, by using the special feature in
1585 -- Errout to ignore all errors. Note that Fatal_Error will still
1586 -- be set, so we will be able to check for this case below.
1588 if not ASIS_Mode then
1589 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1594 (Load_Name => Subunit_Name,
1599 if not ASIS_Mode then
1600 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1603 -- All done if we successfully loaded the subunit
1606 and then (not Fatal_Error (Unum) or else Try_Semantics)
1608 Comp_Unit := Cunit (Unum);
1610 -- If the file was empty or seriously mangled, the unit itself may
1613 if No (Unit (Comp_Unit)) then
1615 ("subunit does not contain expected proper body", N);
1617 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1619 ("expected SEPARATE subunit, found child unit",
1620 Cunit_Entity (Unum));
1622 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1623 Analyze_Subunit (Comp_Unit);
1624 Set_Library_Unit (N, Comp_Unit);
1627 elsif Unum = No_Unit
1628 and then Present (Nam)
1630 if Is_Protected_Type (Nam) then
1631 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1633 Set_Corresponding_Body (
1634 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1637 end Optional_Subunit;
1639 -- Start of processing for Analyze_Proper_Body
1642 -- If the subunit is already loaded, it means that the main unit is a
1643 -- subunit, and that the current unit is one of its parents which was
1644 -- being analyzed to provide the needed context for the analysis of the
1645 -- subunit. In this case we analyze the subunit and continue with the
1646 -- parent, without looking a subsequent subunits.
1648 if Is_Loaded (Subunit_Name) then
1650 -- If the proper body is already linked to the stub node, the stub is
1651 -- in a generic unit and just needs analyzing.
1653 if Present (Library_Unit (N)) then
1654 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1655 Analyze_Subunit (Library_Unit (N));
1657 -- Otherwise we must load the subunit and link to it
1660 -- Load the subunit, this must work, since we originally loaded
1661 -- the subunit earlier on. So this will not really load it, just
1662 -- give access to it.
1666 (Load_Name => Subunit_Name,
1671 -- And analyze the subunit in the parent context (note that we
1672 -- do not call Semantics, since that would remove the parent
1673 -- context). Because of this, we have to manually reset the
1674 -- compiler state to Analyzing since it got destroyed by Load.
1676 if Unum /= No_Unit then
1677 Compiler_State := Analyzing;
1679 -- Check that the proper body is a subunit and not a child
1680 -- unit. If the unit was previously loaded, the error will
1681 -- have been emitted when copying the generic node, so we
1682 -- just return to avoid cascaded errors.
1684 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1688 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1689 Analyze_Subunit (Cunit (Unum));
1690 Set_Library_Unit (N, Cunit (Unum));
1694 -- If the main unit is a subunit, then we are just performing semantic
1695 -- analysis on that subunit, and any other subunits of any parent unit
1696 -- should be ignored, except that if we are building trees for ASIS
1697 -- usage we want to annotate the stub properly.
1699 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1700 and then Subunit_Name /= Unit_Name (Main_Unit)
1706 -- But before we return, set the flag for unloaded subunits. This
1707 -- will suppress junk warnings of variables in the same declarative
1708 -- part (or a higher level one) that are in danger of looking unused
1709 -- when in fact there might be a declaration in the subunit that we
1710 -- do not intend to load.
1712 Unloaded_Subunits := True;
1715 -- If the subunit is not already loaded, and we are generating code,
1716 -- then this is the case where compilation started from the parent,
1717 -- and we are generating code for an entire subunit tree. In that
1718 -- case we definitely need to load the subunit.
1720 -- In order to continue the analysis with the rest of the parent,
1721 -- and other subunits, we load the unit without requiring its
1722 -- presence, and emit a warning if not found, rather than terminating
1723 -- the compilation abruptly, as for other missing file problems.
1725 elsif Original_Operating_Mode = Generate_Code then
1727 -- If the proper body is already linked to the stub node,
1728 -- the stub is in a generic unit and just needs analyzing.
1730 -- We update the version. Although we are not technically
1731 -- semantically dependent on the subunit, given our approach
1732 -- of macro substitution of subunits, it makes sense to
1733 -- include it in the version identification.
1735 if Present (Library_Unit (N)) then
1736 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1737 Analyze_Subunit (Library_Unit (N));
1738 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1740 -- Otherwise we must load the subunit and link to it
1745 (Load_Name => Subunit_Name,
1750 -- Give message if we did not get the unit
1751 -- Emit warning even if missing subunit is not
1752 -- within main unit, to simplify debugging.
1754 if Original_Operating_Mode = Generate_Code
1755 and then Unum = No_Unit
1757 Error_Msg_Unit_1 := Subunit_Name;
1759 Get_File_Name (Subunit_Name, Subunit => True);
1761 ("subunit$$ in file{ not found?!!", N);
1762 Subunits_Missing := True;
1765 -- Load_Unit may reset Compiler_State, since it may have been
1766 -- necessary to parse an additional units, so we make sure
1767 -- that we reset it to the Analyzing state.
1769 Compiler_State := Analyzing;
1771 if Unum /= No_Unit then
1772 if Debug_Flag_L then
1773 Write_Str ("*** Loaded subunit from stub. Analyze");
1778 Comp_Unit : constant Node_Id := Cunit (Unum);
1781 -- Check for child unit instead of subunit
1783 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1785 ("expected SEPARATE subunit, found child unit",
1786 Cunit_Entity (Unum));
1788 -- OK, we have a subunit
1791 -- Set corresponding stub (even if errors)
1793 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1795 -- Collect SCO information for loaded subunit if we are
1796 -- in the main unit).
1800 In_Extended_Main_Source_Unit
1801 (Cunit_Entity (Current_Sem_Unit))
1806 -- Analyze the unit if semantics active
1808 if not Fatal_Error (Unum) or else Try_Semantics then
1809 Analyze_Subunit (Comp_Unit);
1812 -- Set the library unit pointer in any case
1814 Set_Library_Unit (N, Comp_Unit);
1816 -- We update the version. Although we are not technically
1817 -- semantically dependent on the subunit, given our
1818 -- approach of macro substitution of subunits, it makes
1819 -- sense to include it in the version identification.
1821 Version_Update (Cunit (Main_Unit), Comp_Unit);
1827 -- The remaining case is when the subunit is not already loaded and
1828 -- we are not generating code. In this case we are just performing
1829 -- semantic analysis on the parent, and we are not interested in
1830 -- the subunit. For subprograms, analyze the stub as a body. For
1831 -- other entities the stub has already been marked as completed.
1836 end Analyze_Proper_Body;
1838 ----------------------------------
1839 -- Analyze_Protected_Body_Stub --
1840 ----------------------------------
1842 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1843 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1846 Check_Stub_Level (N);
1848 -- First occurrence of name may have been as an incomplete type
1850 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1851 Nam := Full_View (Nam);
1855 or else not Is_Protected_Type (Etype (Nam))
1857 Error_Msg_N ("missing specification for Protected body", N);
1859 Set_Scope (Defining_Entity (N), Current_Scope);
1860 Set_Has_Completion (Etype (Nam));
1861 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1862 Analyze_Proper_Body (N, Etype (Nam));
1864 end Analyze_Protected_Body_Stub;
1866 ----------------------------------
1867 -- Analyze_Subprogram_Body_Stub --
1868 ----------------------------------
1870 -- A subprogram body stub can appear with or without a previous spec. If
1871 -- there is one, then the analysis of the body will find it and verify
1872 -- conformance. The formals appearing in the specification of the stub play
1873 -- no role, except for requiring an additional conformance check. If there
1874 -- is no previous subprogram declaration, the stub acts as a spec, and
1875 -- provides the defining entity for the subprogram.
1877 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1881 Check_Stub_Level (N);
1883 -- Verify that the identifier for the stub is unique within this
1884 -- declarative part.
1886 if Nkind_In (Parent (N), N_Block_Statement,
1890 Decl := First (Declarations (Parent (N)));
1891 while Present (Decl)
1894 if Nkind (Decl) = N_Subprogram_Body_Stub
1895 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1896 Chars (Defining_Unit_Name (Specification (N))))
1898 Error_Msg_N ("identifier for stub is not unique", N);
1905 -- Treat stub as a body, which checks conformance if there is a previous
1906 -- declaration, or else introduces entity and its signature.
1908 Analyze_Subprogram_Body (N);
1909 Analyze_Proper_Body (N, Empty);
1910 end Analyze_Subprogram_Body_Stub;
1912 ---------------------
1913 -- Analyze_Subunit --
1914 ---------------------
1916 -- A subunit is compiled either by itself (for semantic checking) or as
1917 -- part of compiling the parent (for code generation). In either case, by
1918 -- the time we actually process the subunit, the parent has already been
1919 -- installed and analyzed. The node N is a compilation unit, whose context
1920 -- needs to be treated here, because we come directly here from the parent
1921 -- without calling Analyze_Compilation_Unit.
1923 -- The compilation context includes the explicit context of the subunit,
1924 -- and the context of the parent, together with the parent itself. In order
1925 -- to compile the current context, we remove the one inherited from the
1926 -- parent, in order to have a clean visibility table. We restore the parent
1927 -- context before analyzing the proper body itself. On exit, we remove only
1928 -- the explicit context of the subunit.
1930 procedure Analyze_Subunit (N : Node_Id) is
1931 Lib_Unit : constant Node_Id := Library_Unit (N);
1932 Par_Unit : constant Entity_Id := Current_Scope;
1934 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1935 Num_Scopes : Int := 0;
1936 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1937 Enclosing_Child : Entity_Id := Empty;
1938 Svg : constant Suppress_Array := Scope_Suppress;
1940 procedure Analyze_Subunit_Context;
1941 -- Capture names in use clauses of the subunit. This must be done before
1942 -- re-installing parent declarations, because items in the context must
1943 -- not be hidden by declarations local to the parent.
1945 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1946 -- Recursive procedure to restore scope of all ancestors of subunit,
1947 -- from outermost in. If parent is not a subunit, the call to install
1948 -- context installs context of spec and (if parent is a child unit) the
1949 -- context of its parents as well. It is confusing that parents should
1950 -- be treated differently in both cases, but the semantics are just not
1953 procedure Re_Install_Use_Clauses;
1954 -- As part of the removal of the parent scope, the use clauses are
1955 -- removed, to be reinstalled when the context of the subunit has been
1956 -- analyzed. Use clauses may also have been affected by the analysis of
1957 -- the context of the subunit, so they have to be applied again, to
1958 -- insure that the compilation environment of the rest of the parent
1959 -- unit is identical.
1961 procedure Remove_Scope;
1962 -- Remove current scope from scope stack, and preserve the list of use
1963 -- clauses in it, to be reinstalled after context is analyzed.
1965 -----------------------------
1966 -- Analyze_Subunit_Context --
1967 -----------------------------
1969 procedure Analyze_Subunit_Context is
1972 Unit_Name : Entity_Id;
1975 Analyze_Context (N);
1977 -- Make withed units immediately visible. If child unit, make the
1978 -- ultimate parent immediately visible.
1980 Item := First (Context_Items (N));
1981 while Present (Item) loop
1982 if Nkind (Item) = N_With_Clause then
1984 -- Protect frontend against previous errors in context clauses
1986 if Nkind (Name (Item)) /= N_Selected_Component then
1987 if Error_Posted (Item) then
1991 Unit_Name := Entity (Name (Item));
1992 while Is_Child_Unit (Unit_Name) loop
1993 Set_Is_Visible_Child_Unit (Unit_Name);
1994 Unit_Name := Scope (Unit_Name);
1997 if not Is_Immediately_Visible (Unit_Name) then
1998 Set_Is_Immediately_Visible (Unit_Name);
1999 Set_Context_Installed (Item);
2004 elsif Nkind (Item) = N_Use_Package_Clause then
2005 Nam := First (Names (Item));
2006 while Present (Nam) loop
2011 elsif Nkind (Item) = N_Use_Type_Clause then
2012 Nam := First (Subtype_Marks (Item));
2013 while Present (Nam) loop
2022 -- Reset visibility of withed units. They will be made visible again
2023 -- when we install the subunit context.
2025 Item := First (Context_Items (N));
2026 while Present (Item) loop
2027 if Nkind (Item) = N_With_Clause
2029 -- Protect frontend against previous errors in context clauses
2031 and then Nkind (Name (Item)) /= N_Selected_Component
2032 and then not Error_Posted (Item)
2034 Unit_Name := Entity (Name (Item));
2035 while Is_Child_Unit (Unit_Name) loop
2036 Set_Is_Visible_Child_Unit (Unit_Name, False);
2037 Unit_Name := Scope (Unit_Name);
2040 if Context_Installed (Item) then
2041 Set_Is_Immediately_Visible (Unit_Name, False);
2042 Set_Context_Installed (Item, False);
2048 end Analyze_Subunit_Context;
2050 ------------------------
2051 -- Re_Install_Parents --
2052 ------------------------
2054 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2058 if Nkind (Unit (L)) = N_Subunit then
2059 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2062 Install_Context (L);
2064 -- If the subunit occurs within a child unit, we must restore the
2065 -- immediate visibility of any siblings that may occur in context.
2067 if Present (Enclosing_Child) then
2068 Install_Siblings (Enclosing_Child, L);
2073 if Scop /= Par_Unit then
2074 Set_Is_Immediately_Visible (Scop);
2077 -- Make entities in scope visible again. For child units, restore
2078 -- visibility only if they are actually in context.
2080 E := First_Entity (Current_Scope);
2081 while Present (E) loop
2082 if not Is_Child_Unit (E)
2083 or else Is_Visible_Child_Unit (E)
2085 Set_Is_Immediately_Visible (E);
2091 -- A subunit appears within a body, and for a nested subunits all the
2092 -- parents are bodies. Restore full visibility of their private
2095 if Is_Package_Or_Generic_Package (Scop) then
2096 Set_In_Package_Body (Scop);
2097 Install_Private_Declarations (Scop);
2099 end Re_Install_Parents;
2101 ----------------------------
2102 -- Re_Install_Use_Clauses --
2103 ----------------------------
2105 procedure Re_Install_Use_Clauses is
2108 for J in reverse 1 .. Num_Scopes loop
2109 U := Use_Clauses (J);
2110 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2111 Install_Use_Clauses (U, Force_Installation => True);
2113 end Re_Install_Use_Clauses;
2119 procedure Remove_Scope is
2123 Num_Scopes := Num_Scopes + 1;
2124 Use_Clauses (Num_Scopes) :=
2125 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2127 E := First_Entity (Current_Scope);
2128 while Present (E) loop
2129 Set_Is_Immediately_Visible (E, False);
2133 if Is_Child_Unit (Current_Scope) then
2134 Enclosing_Child := Current_Scope;
2140 -- Start of processing for Analyze_Subunit
2143 if not Is_Empty_List (Context_Items (N)) then
2145 -- Save current use clauses
2148 Remove_Context (Lib_Unit);
2150 -- Now remove parents and their context, including enclosing subunits
2151 -- and the outer parent body which is not a subunit.
2153 if Present (Lib_Spec) then
2154 Remove_Context (Lib_Spec);
2156 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2157 Lib_Spec := Library_Unit (Lib_Spec);
2159 Remove_Context (Lib_Spec);
2162 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2166 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2167 Remove_Context (Library_Unit (Lib_Spec));
2171 Set_Is_Immediately_Visible (Par_Unit, False);
2173 Analyze_Subunit_Context;
2175 Re_Install_Parents (Lib_Unit, Par_Unit);
2176 Set_Is_Immediately_Visible (Par_Unit);
2178 -- If the context includes a child unit of the parent of the subunit,
2179 -- the parent will have been removed from visibility, after compiling
2180 -- that cousin in the context. The visibility of the parent must be
2181 -- restored now. This also applies if the context includes another
2182 -- subunit of the same parent which in turn includes a child unit in
2185 if Is_Package_Or_Generic_Package (Par_Unit) then
2186 if not Is_Immediately_Visible (Par_Unit)
2187 or else (Present (First_Entity (Par_Unit))
2188 and then not Is_Immediately_Visible
2189 (First_Entity (Par_Unit)))
2191 Set_Is_Immediately_Visible (Par_Unit);
2192 Install_Visible_Declarations (Par_Unit);
2193 Install_Private_Declarations (Par_Unit);
2197 Re_Install_Use_Clauses;
2198 Install_Context (N);
2200 -- Restore state of suppress flags for current body
2202 Scope_Suppress := Svg;
2204 -- If the subunit is within a child unit, then siblings of any parent
2205 -- unit that appear in the context clause of the subunit must also be
2206 -- made immediately visible.
2208 if Present (Enclosing_Child) then
2209 Install_Siblings (Enclosing_Child, N);
2214 Analyze (Proper_Body (Unit (N)));
2217 -- The subunit may contain a with_clause on a sibling of some ancestor.
2218 -- Removing the context will remove from visibility those ancestor child
2219 -- units, which must be restored to the visibility they have in the
2222 if Present (Enclosing_Child) then
2228 and then Is_Child_Unit (C)
2230 Set_Is_Immediately_Visible (C);
2231 Set_Is_Visible_Child_Unit (C);
2236 end Analyze_Subunit;
2238 ----------------------------
2239 -- Analyze_Task_Body_Stub --
2240 ----------------------------
2242 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2243 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2244 Loc : constant Source_Ptr := Sloc (N);
2247 Check_Stub_Level (N);
2249 -- First occurrence of name may have been as an incomplete type
2251 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2252 Nam := Full_View (Nam);
2255 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2256 Error_Msg_N ("missing specification for task body", N);
2258 Set_Scope (Defining_Entity (N), Current_Scope);
2259 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2260 Set_Has_Completion (Etype (Nam));
2261 Analyze_Proper_Body (N, Etype (Nam));
2263 -- Set elaboration flag to indicate that entity is callable. This
2264 -- cannot be done in the expansion of the body itself, because the
2265 -- proper body is not in a declarative part. This is only done if
2266 -- expansion is active, because the context may be generic and the
2267 -- flag not defined yet.
2269 if Expander_Active then
2271 Make_Assignment_Statement (Loc,
2273 Make_Identifier (Loc,
2274 New_External_Name (Chars (Etype (Nam)), 'E')),
2275 Expression => New_Reference_To (Standard_True, Loc)));
2278 end Analyze_Task_Body_Stub;
2280 -------------------------
2281 -- Analyze_With_Clause --
2282 -------------------------
2284 -- Analyze the declaration of a unit in a with clause. At end, label the
2285 -- with clause with the defining entity for the unit.
2287 procedure Analyze_With_Clause (N : Node_Id) is
2289 -- Retrieve the original kind of the unit node, before analysis. If it
2290 -- is a subprogram instantiation, its analysis below will rewrite the
2291 -- node as the declaration of the wrapper package. If the same
2292 -- instantiation appears indirectly elsewhere in the context, it will
2293 -- have been analyzed already.
2295 Unit_Kind : constant Node_Kind :=
2296 Nkind (Original_Node (Unit (Library_Unit (N))));
2297 Nam : constant Node_Id := Name (N);
2299 Par_Name : Entity_Id;
2304 -- Set True if the unit currently being compiled is an internal unit
2306 Save_Style_Check : constant Boolean := Opt.Style_Check;
2307 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
2308 Cunit_Boolean_Restrictions_Save;
2311 U := Unit (Library_Unit (N));
2313 -- Several actions are skipped for dummy packages (those supplied for
2314 -- with's where no matching file could be found). Such packages are
2315 -- identified by the Sloc value being set to No_Location.
2317 if Limited_Present (N) then
2319 -- Ada 2005 (AI-50217): Build visibility structures but do not
2320 -- analyze the unit.
2322 if Sloc (U) /= No_Location then
2323 Build_Limited_Views (N);
2329 -- We reset ordinary style checking during the analysis of a with'ed
2330 -- unit, but we do NOT reset GNAT special analysis mode (the latter
2331 -- definitely *does* apply to with'ed units).
2333 if not GNAT_Mode then
2334 Style_Check := False;
2337 -- If the library unit is a predefined unit, and we are in high
2338 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2339 -- for the analysis of the with'ed unit. This mode does not prevent
2340 -- explicit with'ing of run-time units.
2342 if Configurable_Run_Time_Mode
2344 Is_Predefined_File_Name
2345 (Unit_File_Name (Get_Source_Unit (Unit (Library_Unit (N)))))
2347 Configurable_Run_Time_Mode := False;
2348 Semantics (Library_Unit (N));
2349 Configurable_Run_Time_Mode := True;
2352 Semantics (Library_Unit (N));
2355 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2357 if Sloc (U) /= No_Location then
2359 -- Check restrictions, except that we skip the check if this is an
2360 -- internal unit unless we are compiling the internal unit as the
2361 -- main unit. We also skip this for dummy packages.
2363 Check_Restriction_No_Dependence (Nam, N);
2365 if not Intunit or else Current_Sem_Unit = Main_Unit then
2366 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2369 -- Deal with special case of GNAT.Current_Exceptions which interacts
2370 -- with the optimization of local raise statements into gotos.
2372 if Nkind (Nam) = N_Selected_Component
2373 and then Nkind (Prefix (Nam)) = N_Identifier
2374 and then Chars (Prefix (Nam)) = Name_Gnat
2375 and then (Chars (Selector_Name (Nam)) = Name_Most_Recent_Exception
2377 Chars (Selector_Name (Nam)) = Name_Exception_Traces)
2379 Check_Restriction (No_Exception_Propagation, N);
2380 Special_Exception_Package_Used := True;
2383 -- Check for inappropriate with of internal implementation unit if we
2384 -- are not compiling an internal unit. We do not issue this message
2385 -- for implicit with's generated by the compiler itself.
2387 if Implementation_Unit_Warnings
2388 and then not Intunit
2389 and then not Implicit_With (N)
2392 U_Kind : constant Kind_Of_Unit :=
2393 Get_Kind_Of_Unit (Get_Source_Unit (U));
2396 if U_Kind = Implementation_Unit then
2397 Error_Msg_F ("& is an internal 'G'N'A'T unit?", Name (N));
2399 -- Add alternative name if available, otherwise issue a
2400 -- general warning message.
2402 if Error_Msg_Strlen /= 0 then
2403 Error_Msg_F ("\use ""~"" instead", Name (N));
2406 ("\use of this unit is non-portable " &
2407 "and version-dependent?", Name (N));
2410 elsif U_Kind = Ada_05_Unit
2411 and then Ada_Version < Ada_05
2412 and then Warn_On_Ada_2005_Compatibility
2414 Error_Msg_N ("& is an Ada 2005 unit?", Name (N));
2420 -- Semantic analysis of a generic unit is performed on a copy of
2421 -- the original tree. Retrieve the entity on which semantic info
2422 -- actually appears.
2424 if Unit_Kind in N_Generic_Declaration then
2425 E_Name := Defining_Entity (U);
2427 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2428 -- the case of an instantiation, semantic analysis above will have
2429 -- replaced the unit by its instantiated version. If the instance body
2430 -- has been generated, the instance now denotes the body entity. For
2431 -- visibility purposes we need the entity of its spec.
2433 elsif (Unit_Kind = N_Package_Instantiation
2434 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2435 N_Package_Instantiation)
2436 and then Nkind (U) = N_Package_Body
2438 E_Name := Corresponding_Spec (U);
2440 elsif Unit_Kind = N_Package_Instantiation
2441 and then Nkind (U) = N_Package_Instantiation
2443 -- If the instance has not been rewritten as a package declaration,
2444 -- then it appeared already in a previous with clause. Retrieve
2445 -- the entity from the previous instance.
2447 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2449 elsif Unit_Kind in N_Subprogram_Instantiation then
2451 -- The visible subprogram is created during instantiation, and is
2452 -- an attribute of the wrapper package. We retrieve the wrapper
2453 -- package directly from the instantiation node. If the instance
2454 -- is inlined the unit is still an instantiation. Otherwise it has
2455 -- been rewritten as the declaration of the wrapper itself.
2457 if Nkind (U) in N_Subprogram_Instantiation then
2460 (Defining_Entity (Specification (Instance_Spec (U))));
2462 E_Name := Related_Instance (Defining_Entity (U));
2465 elsif Unit_Kind = N_Package_Renaming_Declaration
2466 or else Unit_Kind in N_Generic_Renaming_Declaration
2468 E_Name := Defining_Entity (U);
2470 elsif Unit_Kind = N_Subprogram_Body
2471 and then Nkind (Name (N)) = N_Selected_Component
2472 and then not Acts_As_Spec (Library_Unit (N))
2474 -- For a child unit that has no spec, one has been created and
2475 -- analyzed. The entity required is that of the spec.
2477 E_Name := Corresponding_Spec (U);
2480 E_Name := Defining_Entity (U);
2483 if Nkind (Name (N)) = N_Selected_Component then
2485 -- Child unit in a with clause
2487 Change_Selected_Component_To_Expanded_Name (Name (N));
2490 -- Restore style checks and restrictions
2492 Style_Check := Save_Style_Check;
2493 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
2495 -- Record the reference, but do NOT set the unit as referenced, we want
2496 -- to consider the unit as unreferenced if this is the only reference
2499 Set_Entity_With_Style_Check (Name (N), E_Name);
2500 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2502 -- Generate references and check No_Dependence restriction for parents
2504 if Is_Child_Unit (E_Name) then
2505 Pref := Prefix (Name (N));
2506 Par_Name := Scope (E_Name);
2507 while Nkind (Pref) = N_Selected_Component loop
2508 Change_Selected_Component_To_Expanded_Name (Pref);
2509 Set_Entity_With_Style_Check (Pref, Par_Name);
2511 Generate_Reference (Par_Name, Pref);
2512 Check_Restriction_No_Dependence (Pref, N);
2513 Pref := Prefix (Pref);
2515 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2516 -- is set to Standard_Standard, and no attempt should be made to
2517 -- further unwind scopes.
2519 if Par_Name /= Standard_Standard then
2520 Par_Name := Scope (Par_Name);
2524 if Present (Entity (Pref))
2525 and then not Analyzed (Parent (Parent (Entity (Pref))))
2527 -- If the entity is set without its unit being compiled, the
2528 -- original parent is a renaming, and Par_Name is the renamed
2529 -- entity. For visibility purposes, we need the original entity,
2530 -- which must be analyzed now because Load_Unit directly retrieves
2531 -- the renamed unit, and the renaming declaration itself has not
2534 Analyze (Parent (Parent (Entity (Pref))));
2535 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2536 Par_Name := Entity (Pref);
2539 Set_Entity_With_Style_Check (Pref, Par_Name);
2540 Generate_Reference (Par_Name, Pref);
2543 -- If the withed unit is System, and a system extension pragma is
2544 -- present, compile the extension now, rather than waiting for a
2545 -- visibility check on a specific entity.
2547 if Chars (E_Name) = Name_System
2548 and then Scope (E_Name) = Standard_Standard
2549 and then Present (System_Extend_Unit)
2550 and then Present_System_Aux (N)
2552 -- If the extension is not present, an error will have been emitted
2557 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2558 -- to private_with units; they will be made visible later (just before
2559 -- the private part is analyzed)
2561 if Private_Present (N) then
2562 Set_Is_Immediately_Visible (E_Name, False);
2564 end Analyze_With_Clause;
2566 ------------------------------
2567 -- Check_Private_Child_Unit --
2568 ------------------------------
2570 procedure Check_Private_Child_Unit (N : Node_Id) is
2571 Lib_Unit : constant Node_Id := Unit (N);
2573 Curr_Unit : Entity_Id;
2574 Sub_Parent : Node_Id;
2575 Priv_Child : Entity_Id;
2576 Par_Lib : Entity_Id;
2579 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2580 -- Returns true if and only if the library unit is declared with
2581 -- an explicit designation of private.
2583 -----------------------------
2584 -- Is_Private_Library_Unit --
2585 -----------------------------
2587 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2588 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2591 return Private_Present (Comp_Unit);
2592 end Is_Private_Library_Unit;
2594 -- Start of processing for Check_Private_Child_Unit
2597 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2598 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2599 Par_Lib := Curr_Unit;
2601 elsif Nkind (Lib_Unit) = N_Subunit then
2603 -- The parent is itself a body. The parent entity is to be found in
2604 -- the corresponding spec.
2606 Sub_Parent := Library_Unit (N);
2607 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2609 -- If the parent itself is a subunit, Curr_Unit is the entity
2610 -- of the enclosing body, retrieve the spec entity which is
2611 -- the proper ancestor we need for the following tests.
2613 if Ekind (Curr_Unit) = E_Package_Body then
2614 Curr_Unit := Spec_Entity (Curr_Unit);
2617 Par_Lib := Curr_Unit;
2620 Curr_Unit := Defining_Entity (Lib_Unit);
2622 Par_Lib := Curr_Unit;
2623 Par_Spec := Parent_Spec (Lib_Unit);
2625 if No (Par_Spec) then
2628 Par_Lib := Defining_Entity (Unit (Par_Spec));
2632 -- Loop through context items
2634 Item := First (Context_Items (N));
2635 while Present (Item) loop
2637 -- Ada 2005 (AI-262): Allow private_with of a private child package
2638 -- in public siblings
2640 if Nkind (Item) = N_With_Clause
2641 and then not Implicit_With (Item)
2642 and then not Limited_Present (Item)
2643 and then Is_Private_Descendant (Entity (Name (Item)))
2645 Priv_Child := Entity (Name (Item));
2648 Curr_Parent : Entity_Id := Par_Lib;
2649 Child_Parent : Entity_Id := Scope (Priv_Child);
2650 Prv_Ancestor : Entity_Id := Child_Parent;
2651 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2654 -- If the child unit is a public child then locate the nearest
2655 -- private ancestor. Child_Parent will then be set to the
2656 -- parent of that ancestor.
2658 if not Is_Private_Library_Unit (Priv_Child) then
2659 while Present (Prv_Ancestor)
2660 and then not Is_Private_Library_Unit (Prv_Ancestor)
2662 Prv_Ancestor := Scope (Prv_Ancestor);
2665 if Present (Prv_Ancestor) then
2666 Child_Parent := Scope (Prv_Ancestor);
2670 while Present (Curr_Parent)
2671 and then Curr_Parent /= Standard_Standard
2672 and then Curr_Parent /= Child_Parent
2675 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2676 Curr_Parent := Scope (Curr_Parent);
2679 if No (Curr_Parent) then
2680 Curr_Parent := Standard_Standard;
2683 if Curr_Parent /= Child_Parent then
2684 if Ekind (Priv_Child) = E_Generic_Package
2685 and then Chars (Priv_Child) in Text_IO_Package_Name
2686 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2689 ("& is a nested package, not a compilation unit",
2690 Name (Item), Priv_Child);
2694 ("unit in with clause is private child unit!", Item);
2696 ("\current unit must also have parent&!",
2697 Item, Child_Parent);
2701 or else Private_Present (Item)
2702 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2703 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2704 and then not Acts_As_Spec (Parent (Lib_Unit)))
2710 ("current unit must also be private descendant of&",
2711 Item, Child_Parent);
2719 end Check_Private_Child_Unit;
2721 ----------------------
2722 -- Check_Stub_Level --
2723 ----------------------
2725 procedure Check_Stub_Level (N : Node_Id) is
2726 Par : constant Node_Id := Parent (N);
2727 Kind : constant Node_Kind := Nkind (Par);
2730 if Nkind_In (Kind, N_Package_Body,
2734 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
2738 -- In an instance, a missing stub appears at any level. A warning
2739 -- message will have been emitted already for the missing file.
2741 elsif not In_Instance then
2742 Error_Msg_N ("stub cannot appear in an inner scope", N);
2744 elsif Expander_Active then
2745 Error_Msg_N ("missing proper body", N);
2747 end Check_Stub_Level;
2749 ------------------------
2750 -- Expand_With_Clause --
2751 ------------------------
2753 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2754 Loc : constant Source_Ptr := Sloc (Nam);
2755 Ent : constant Entity_Id := Entity (Nam);
2759 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2760 -- Build name to be used in implicit with_clause. In most cases this
2761 -- is the source name, but if renamings are present we must make the
2762 -- original unit visible, not the one it renames. The entity in the
2763 -- with clause is the renamed unit, but the identifier is the one from
2764 -- the source, which allows us to recover the unit renaming.
2766 ---------------------
2767 -- Build_Unit_Name --
2768 ---------------------
2770 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2772 Renaming : Entity_Id;
2776 if Nkind (Nam) = N_Identifier then
2778 -- If the parent unit P in the name of the with_clause for P.Q
2779 -- is a renaming of package R, then the entity of the parent is
2780 -- set to R, but the identifier retains Chars (P) to be consistent
2781 -- with the source (see details in lib-load). However, the
2782 -- implicit_with_clause for the parent must make the entity for
2783 -- P visible, because P.Q may be used as a prefix within the
2784 -- current unit. The entity for P is the current_entity with that
2785 -- name, because the package renaming declaration for it has just
2786 -- been analyzed. Note that this case can only happen if P.Q has
2787 -- already appeared in a previous with_clause in a related unit,
2788 -- such as the library body of the current unit.
2790 if Chars (Nam) /= Chars (Entity (Nam)) then
2791 Renaming := Current_Entity (Nam);
2792 pragma Assert (Renamed_Entity (Renaming) = Entity (Nam));
2793 return New_Occurrence_Of (Renaming, Loc);
2796 return New_Occurrence_Of (Entity (Nam), Loc);
2800 Ent := Entity (Nam);
2802 if Present (Entity (Selector_Name (Nam)))
2803 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
2805 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
2806 = N_Package_Renaming_Declaration
2808 -- The name in the with_clause is of the form A.B.C, and B
2809 -- is given by a renaming declaration. In that case we may
2810 -- not have analyzed the unit for B, but replaced it directly
2811 -- in lib-load with the unit it renames. We have to make A.B
2812 -- visible, so analyze the declaration for B now, in case it
2813 -- has not been done yet.
2815 Ent := Entity (Selector_Name (Nam));
2818 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
2822 Make_Expanded_Name (Loc,
2823 Chars => Chars (Entity (Nam)),
2824 Prefix => Build_Unit_Name (Prefix (Nam)),
2825 Selector_Name => New_Occurrence_Of (Ent, Loc));
2826 Set_Entity (Result, Ent);
2829 end Build_Unit_Name;
2831 -- Start of processing for Expand_With_Clause
2834 New_Nodes_OK := New_Nodes_OK + 1;
2836 Make_With_Clause (Loc,
2837 Name => Build_Unit_Name (Nam));
2839 P := Parent (Unit_Declaration_Node (Ent));
2840 Set_Library_Unit (Withn, P);
2841 Set_Corresponding_Spec (Withn, Ent);
2842 Set_First_Name (Withn, True);
2843 Set_Implicit_With (Withn, True);
2845 -- If the unit is a package declaration, a private_with_clause on a
2846 -- child unit implies the implicit with on the parent is also private.
2848 if Nkind (Unit (N)) = N_Package_Declaration then
2849 Set_Private_Present (Withn, Private_Present (Item));
2852 Prepend (Withn, Context_Items (N));
2853 Mark_Rewrite_Insertion (Withn);
2854 Install_Withed_Unit (Withn);
2856 if Nkind (Nam) = N_Expanded_Name then
2857 Expand_With_Clause (Item, Prefix (Nam), N);
2860 New_Nodes_OK := New_Nodes_OK - 1;
2861 end Expand_With_Clause;
2863 -----------------------
2864 -- Get_Parent_Entity --
2865 -----------------------
2867 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2869 if Nkind (Unit) = N_Package_Body
2870 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
2872 return Defining_Entity
2873 (Specification (Instance_Spec (Original_Node (Unit))));
2874 elsif Nkind (Unit) = N_Package_Instantiation then
2875 return Defining_Entity (Specification (Instance_Spec (Unit)));
2877 return Defining_Entity (Unit);
2879 end Get_Parent_Entity;
2881 ---------------------
2882 -- Has_With_Clause --
2883 ---------------------
2885 function Has_With_Clause
2888 Is_Limited : Boolean := False) return Boolean
2892 function Named_Unit (Clause : Node_Id) return Entity_Id;
2893 -- Return the entity for the unit named in a [limited] with clause
2899 function Named_Unit (Clause : Node_Id) return Entity_Id is
2901 if Nkind (Name (Clause)) = N_Selected_Component then
2902 return Entity (Selector_Name (Name (Clause)));
2904 return Entity (Name (Clause));
2908 -- Start of processing for Has_With_Clause
2911 if Present (Context_Items (C_Unit)) then
2912 Item := First (Context_Items (C_Unit));
2913 while Present (Item) loop
2914 if Nkind (Item) = N_With_Clause
2915 and then Limited_Present (Item) = Is_Limited
2916 and then Named_Unit (Item) = Pack
2926 end Has_With_Clause;
2928 -----------------------------
2929 -- Implicit_With_On_Parent --
2930 -----------------------------
2932 procedure Implicit_With_On_Parent
2933 (Child_Unit : Node_Id;
2936 Loc : constant Source_Ptr := Sloc (N);
2937 P : constant Node_Id := Parent_Spec (Child_Unit);
2938 P_Unit : Node_Id := Unit (P);
2939 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
2942 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2943 -- Build prefix of child unit name. Recurse if needed
2945 function Build_Unit_Name return Node_Id;
2946 -- If the unit is a child unit, build qualified name with all ancestors
2948 -------------------------
2949 -- Build_Ancestor_Name --
2950 -------------------------
2952 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
2953 P_Ref : constant Node_Id :=
2954 New_Reference_To (Defining_Entity (P), Loc);
2955 P_Spec : Node_Id := P;
2958 -- Ancestor may have been rewritten as a package body. Retrieve
2959 -- the original spec to trace earlier ancestors.
2961 if Nkind (P) = N_Package_Body
2962 and then Nkind (Original_Node (P)) = N_Package_Instantiation
2964 P_Spec := Original_Node (P);
2967 if No (Parent_Spec (P_Spec)) then
2971 Make_Selected_Component (Loc,
2972 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
2973 Selector_Name => P_Ref);
2975 end Build_Ancestor_Name;
2977 ---------------------
2978 -- Build_Unit_Name --
2979 ---------------------
2981 function Build_Unit_Name return Node_Id is
2985 if No (Parent_Spec (P_Unit)) then
2986 return New_Reference_To (P_Name, Loc);
2990 Make_Expanded_Name (Loc,
2991 Chars => Chars (P_Name),
2992 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
2993 Selector_Name => New_Reference_To (P_Name, Loc));
2994 Set_Entity (Result, P_Name);
2997 end Build_Unit_Name;
2999 -- Start of processing for Implicit_With_On_Parent
3002 -- The unit of the current compilation may be a package body that
3003 -- replaces an instance node. In this case we need the original instance
3004 -- node to construct the proper parent name.
3006 if Nkind (P_Unit) = N_Package_Body
3007 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3009 P_Unit := Original_Node (P_Unit);
3012 -- We add the implicit with if the child unit is the current unit being
3013 -- compiled. If the current unit is a body, we do not want to add an
3014 -- implicit_with a second time to the corresponding spec.
3016 if Nkind (Child_Unit) = N_Package_Declaration
3017 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3022 New_Nodes_OK := New_Nodes_OK + 1;
3023 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3025 Set_Library_Unit (Withn, P);
3026 Set_Corresponding_Spec (Withn, P_Name);
3027 Set_First_Name (Withn, True);
3028 Set_Implicit_With (Withn, True);
3030 -- Node is placed at the beginning of the context items, so that
3031 -- subsequent use clauses on the parent can be validated.
3033 Prepend (Withn, Context_Items (N));
3034 Mark_Rewrite_Insertion (Withn);
3035 Install_Withed_Unit (Withn);
3037 if Is_Child_Spec (P_Unit) then
3038 Implicit_With_On_Parent (P_Unit, N);
3041 New_Nodes_OK := New_Nodes_OK - 1;
3042 end Implicit_With_On_Parent;
3048 function In_Chain (E : Entity_Id) return Boolean is
3052 H := Current_Entity (E);
3053 while Present (H) loop
3064 ---------------------
3065 -- Install_Context --
3066 ---------------------
3068 procedure Install_Context (N : Node_Id) is
3069 Lib_Unit : constant Node_Id := Unit (N);
3072 Install_Context_Clauses (N);
3074 if Is_Child_Spec (Lib_Unit) then
3075 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3078 Install_Limited_Context_Clauses (N);
3079 end Install_Context;
3081 -----------------------------
3082 -- Install_Context_Clauses --
3083 -----------------------------
3085 procedure Install_Context_Clauses (N : Node_Id) is
3086 Lib_Unit : constant Node_Id := Unit (N);
3088 Uname_Node : Entity_Id;
3089 Check_Private : Boolean := False;
3090 Decl_Node : Node_Id;
3091 Lib_Parent : Entity_Id;
3094 -- First skip configuration pragmas at the start of the context. They
3095 -- are not technically part of the context clause, but that's where the
3096 -- parser puts them. Note they were analyzed in Analyze_Context.
3098 Item := First (Context_Items (N));
3099 while Present (Item)
3100 and then Nkind (Item) = N_Pragma
3101 and then Pragma_Name (Item) in Configuration_Pragma_Names
3106 -- Loop through the actual context clause items. We process everything
3107 -- except Limited_With clauses in this routine. Limited_With clauses
3108 -- are separately installed (see Install_Limited_Context_Clauses).
3110 while Present (Item) loop
3112 -- Case of explicit WITH clause
3114 if Nkind (Item) = N_With_Clause
3115 and then not Implicit_With (Item)
3117 if Limited_Present (Item) then
3119 -- Limited withed units will be installed later
3123 -- If Name (Item) is not an entity name, something is wrong, and
3124 -- this will be detected in due course, for now ignore the item
3126 elsif not Is_Entity_Name (Name (Item)) then
3129 elsif No (Entity (Name (Item))) then
3130 Set_Entity (Name (Item), Any_Id);
3134 Uname_Node := Entity (Name (Item));
3136 if Is_Private_Descendant (Uname_Node) then
3137 Check_Private := True;
3140 Install_Withed_Unit (Item);
3142 Decl_Node := Unit_Declaration_Node (Uname_Node);
3144 -- If the unit is a subprogram instance, it appears nested within
3145 -- a package that carries the parent information.
3147 if Is_Generic_Instance (Uname_Node)
3148 and then Ekind (Uname_Node) /= E_Package
3150 Decl_Node := Parent (Parent (Decl_Node));
3153 if Is_Child_Spec (Decl_Node) then
3154 if Nkind (Name (Item)) = N_Expanded_Name then
3155 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3157 -- If not an expanded name, the child unit must be a
3158 -- renaming, nothing to do.
3163 elsif Nkind (Decl_Node) = N_Subprogram_Body
3164 and then not Acts_As_Spec (Parent (Decl_Node))
3165 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3167 Implicit_With_On_Parent
3168 (Unit (Library_Unit (Parent (Decl_Node))), N);
3171 -- Check license conditions unless this is a dummy unit
3173 if Sloc (Library_Unit (Item)) /= No_Location then
3174 License_Check : declare
3175 Withu : constant Unit_Number_Type :=
3176 Get_Source_Unit (Library_Unit (Item));
3177 Withl : constant License_Type :=
3178 License (Source_Index (Withu));
3179 Unitl : constant License_Type :=
3180 License (Source_Index (Current_Sem_Unit));
3182 procedure License_Error;
3183 -- Signal error of bad license
3189 procedure License_Error is
3192 ("?license of with'ed unit & may be inconsistent",
3196 -- Start of processing for License_Check
3199 -- Exclude license check if withed unit is an internal unit.
3200 -- This situation arises e.g. with the GPL version of GNAT.
3202 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3205 -- Otherwise check various cases
3217 if Withl = Restricted then
3221 when Modified_GPL =>
3222 if Withl = Restricted or else Withl = GPL then
3226 when Unrestricted =>
3233 -- Case of USE PACKAGE clause
3235 elsif Nkind (Item) = N_Use_Package_Clause then
3236 Analyze_Use_Package (Item);
3238 -- Case of USE TYPE clause
3240 elsif Nkind (Item) = N_Use_Type_Clause then
3241 Analyze_Use_Type (Item);
3245 elsif Nkind (Item) = N_Pragma then
3253 if Is_Child_Spec (Lib_Unit) then
3255 -- The unit also has implicit with_clauses on its own parents
3257 if No (Context_Items (N)) then
3258 Set_Context_Items (N, New_List);
3261 Implicit_With_On_Parent (Lib_Unit, N);
3264 -- If the unit is a body, the context of the specification must also
3265 -- be installed. That includes private with_clauses in that context.
3267 if Nkind (Lib_Unit) = N_Package_Body
3268 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3269 and then not Acts_As_Spec (N))
3271 Install_Context (Library_Unit (N));
3273 -- Only install private with-clauses of a spec that comes from
3274 -- source, excluding specs created for a subprogram body that is
3277 if Comes_From_Source (Library_Unit (N)) then
3278 Install_Private_With_Clauses
3279 (Defining_Entity (Unit (Library_Unit (N))));
3282 if Is_Child_Spec (Unit (Library_Unit (N))) then
3284 -- If the unit is the body of a public child unit, the private
3285 -- declarations of the parent must be made visible. If the child
3286 -- unit is private, the private declarations have been installed
3287 -- already in the call to Install_Parents for the spec. Installing
3288 -- private declarations must be done for all ancestors of public
3289 -- child units. In addition, sibling units mentioned in the
3290 -- context clause of the body are directly visible.
3298 Lib_Spec := Unit (Library_Unit (N));
3299 while Is_Child_Spec (Lib_Spec) loop
3300 P := Unit (Parent_Spec (Lib_Spec));
3301 P_Name := Defining_Entity (P);
3303 if not (Private_Present (Parent (Lib_Spec)))
3304 and then not In_Private_Part (P_Name)
3306 Install_Private_Declarations (P_Name);
3307 Install_Private_With_Clauses (P_Name);
3308 Set_Use (Private_Declarations (Specification (P)));
3316 -- For a package body, children in context are immediately visible
3318 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3321 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3322 N_Generic_Subprogram_Declaration,
3323 N_Package_Declaration,
3324 N_Subprogram_Declaration)
3326 if Is_Child_Spec (Lib_Unit) then
3327 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3328 Set_Is_Private_Descendant
3329 (Defining_Entity (Lib_Unit),
3330 Is_Private_Descendant (Lib_Parent)
3331 or else Private_Present (Parent (Lib_Unit)));
3334 Set_Is_Private_Descendant
3335 (Defining_Entity (Lib_Unit),
3336 Private_Present (Parent (Lib_Unit)));
3340 if Check_Private then
3341 Check_Private_Child_Unit (N);
3343 end Install_Context_Clauses;
3345 -------------------------------------
3346 -- Install_Limited_Context_Clauses --
3347 -------------------------------------
3349 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3352 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3353 -- Check that the unlimited view of a given compilation_unit is not
3354 -- already visible through "use + renamings".
3356 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3357 -- Check that if a limited_with clause of a given compilation_unit
3358 -- mentions a descendant of a private child of some library unit, then
3359 -- the given compilation_unit shall be the declaration of a private
3360 -- descendant of that library unit, or a public descendant of such. The
3361 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3362 -- use entities on the limited with_clauses because their units have not
3363 -- been analyzed, so we have to climb the tree of ancestors looking for
3364 -- private keywords.
3366 procedure Expand_Limited_With_Clause
3367 (Comp_Unit : Node_Id;
3370 -- If a child unit appears in a limited_with clause, there are implicit
3371 -- limited_with clauses on all parents that are not already visible
3372 -- through a regular with clause. This procedure creates the implicit
3373 -- limited with_clauses for the parents and loads the corresponding
3374 -- units. The shadow entities are created when the inserted clause is
3375 -- analyzed. Implements Ada 2005 (AI-50217).
3377 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
3378 -- When compiling a unit Q descended from some parent unit P, a limited
3379 -- with_clause in the context of P that names some other ancestor of Q
3380 -- must not be installed because the ancestor is immediately visible.
3382 ---------------------
3383 -- Check_Renamings --
3384 ---------------------
3386 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3395 pragma Assert (Nkind (W) = N_With_Clause);
3397 -- Protect the frontend against previous critical errors
3399 case Nkind (Unit (Library_Unit (W))) is
3400 when N_Subprogram_Declaration |
3401 N_Package_Declaration |
3402 N_Generic_Subprogram_Declaration |
3403 N_Generic_Package_Declaration =>
3410 -- Check "use + renamings"
3412 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3413 Spec := Specification (Unit (P));
3415 Item := First (Visible_Declarations (Spec));
3416 while Present (Item) loop
3418 -- Look only at use package clauses
3420 if Nkind (Item) = N_Use_Package_Clause then
3422 -- Traverse the list of packages
3424 Nam := First (Names (Item));
3425 while Present (Nam) loop
3428 pragma Assert (Present (Parent (E)));
3430 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3431 and then Renamed_Entity (E) = WEnt
3433 -- The unlimited view is visible through use clause and
3434 -- renamings. There is no need to generate the error
3435 -- message here because Is_Visible_Through_Renamings
3436 -- takes care of generating the precise error message.
3440 elsif Nkind (Parent (E)) = N_Package_Specification then
3442 -- The use clause may refer to a local package.
3443 -- Check all the enclosing scopes.
3446 while E2 /= Standard_Standard
3454 ("unlimited view visible through use clause ", W);
3466 -- Recursive call to check all the ancestors
3468 if Is_Child_Spec (Unit (P)) then
3469 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3471 end Check_Renamings;
3473 ---------------------------------------
3474 -- Check_Private_Limited_Withed_Unit --
3475 ---------------------------------------
3477 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3478 Curr_Parent : Node_Id;
3479 Child_Parent : Node_Id;
3480 Curr_Private : Boolean;
3483 -- Compilation unit of the parent of the withed library unit
3485 Child_Parent := Library_Unit (Item);
3487 -- If the child unit is a public child, then locate its nearest
3488 -- private ancestor, if any, then Child_Parent will then be set to
3489 -- the parent of that ancestor.
3491 if not Private_Present (Library_Unit (Item)) then
3492 while Present (Child_Parent)
3493 and then not Private_Present (Child_Parent)
3495 Child_Parent := Parent_Spec (Unit (Child_Parent));
3498 if No (Child_Parent) then
3503 Child_Parent := Parent_Spec (Unit (Child_Parent));
3505 -- Traverse all the ancestors of the current compilation unit to
3506 -- check if it is a descendant of named library unit.
3508 Curr_Parent := Parent (Item);
3509 Curr_Private := Private_Present (Curr_Parent);
3511 while Present (Parent_Spec (Unit (Curr_Parent)))
3512 and then Curr_Parent /= Child_Parent
3514 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3515 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3518 if Curr_Parent /= Child_Parent then
3520 ("unit in with clause is private child unit!", Item);
3522 ("\current unit must also have parent&!",
3523 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3525 elsif Private_Present (Parent (Item))
3526 or else Curr_Private
3527 or else Private_Present (Item)
3528 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3532 -- Current unit is private, of descendant of a private unit
3538 ("current unit must also be private descendant of&",
3539 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3541 end Check_Private_Limited_Withed_Unit;
3543 --------------------------------
3544 -- Expand_Limited_With_Clause --
3545 --------------------------------
3547 procedure Expand_Limited_With_Clause
3548 (Comp_Unit : Node_Id;
3552 Loc : constant Source_Ptr := Sloc (Nam);
3553 Unum : Unit_Number_Type;
3556 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3557 -- Returns true if the context already includes a with_clause for
3558 -- this unit. If the with_clause is non-limited, the unit is fully
3559 -- visible and an implicit limited_with should not be created. If
3560 -- there is already a limited_with clause for W, a second one is
3561 -- simply redundant.
3563 --------------------------
3564 -- Previous_Withed_Unit --
3565 --------------------------
3567 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3571 -- A limited with_clause cannot appear in the same context_clause
3572 -- as a nonlimited with_clause which mentions the same library.
3574 Item := First (Context_Items (Comp_Unit));
3575 while Present (Item) loop
3576 if Nkind (Item) = N_With_Clause
3577 and then Library_Unit (Item) = Library_Unit (W)
3586 end Previous_Withed_Unit;
3588 -- Start of processing for Expand_Limited_With_Clause
3591 New_Nodes_OK := New_Nodes_OK + 1;
3593 if Nkind (Nam) = N_Identifier then
3595 -- Create node for name of withed unit
3598 Make_With_Clause (Loc,
3599 Name => New_Copy (Nam));
3601 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3603 Make_With_Clause (Loc,
3604 Name => Make_Selected_Component (Loc,
3605 Prefix => New_Copy_Tree (Prefix (Nam)),
3606 Selector_Name => New_Copy (Selector_Name (Nam))));
3607 Set_Parent (Withn, Parent (N));
3610 Set_Limited_Present (Withn);
3611 Set_First_Name (Withn);
3612 Set_Implicit_With (Withn);
3616 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3621 -- Do not generate a limited_with_clause on the current unit.
3622 -- This path is taken when a unit has a limited_with clause on
3623 -- one of its child units.
3625 if Unum = Current_Sem_Unit then
3629 Set_Library_Unit (Withn, Cunit (Unum));
3630 Set_Corresponding_Spec
3631 (Withn, Specification (Unit (Cunit (Unum))));
3633 if not Previous_Withed_Unit (Withn) then
3634 Prepend (Withn, Context_Items (Parent (N)));
3635 Mark_Rewrite_Insertion (Withn);
3637 -- Add implicit limited_with_clauses for parents of child units
3638 -- mentioned in limited_with clauses.
3640 if Nkind (Nam) = N_Selected_Component then
3641 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3646 if not Limited_View_Installed (Withn) then
3647 Install_Limited_Withed_Unit (Withn);
3651 New_Nodes_OK := New_Nodes_OK - 1;
3652 end Expand_Limited_With_Clause;
3654 ----------------------
3655 -- Is_Ancestor_Unit --
3656 ----------------------
3658 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
3659 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
3662 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
3663 E2 := Defining_Entity (Unit (Library_Unit (U2)));
3664 return Is_Ancestor_Package (E1, E2);
3668 end Is_Ancestor_Unit;
3670 -- Start of processing for Install_Limited_Context_Clauses
3673 Item := First (Context_Items (N));
3674 while Present (Item) loop
3675 if Nkind (Item) = N_With_Clause
3676 and then Limited_Present (Item)
3678 if Nkind (Name (Item)) = N_Selected_Component then
3679 Expand_Limited_With_Clause
3680 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3683 Check_Private_Limited_Withed_Unit (Item);
3685 if not Implicit_With (Item)
3686 and then Is_Child_Spec (Unit (N))
3688 Check_Renamings (Parent_Spec (Unit (N)), Item);
3691 -- A unit may have a limited with on itself if it has a limited
3692 -- with_clause on one of its child units. In that case it is
3693 -- already being compiled and it makes no sense to install its
3696 -- If the item is a limited_private_with_clause, install it if the
3697 -- current unit is a body or if it is a private child. Otherwise
3698 -- the private clause is installed before analyzing the private
3699 -- part of the current unit.
3701 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3702 and then not Limited_View_Installed (Item)
3704 not Is_Ancestor_Unit
3705 (Library_Unit (Item), Cunit (Current_Sem_Unit))
3707 if not Private_Present (Item)
3708 or else Private_Present (N)
3709 or else Nkind_In (Unit (N), N_Package_Body,
3713 Install_Limited_Withed_Unit (Item);
3721 -- Ada 2005 (AI-412): Examine the visible declarations of a package
3722 -- spec, looking for incomplete subtype declarations of incomplete types
3723 -- visible through a limited with clause.
3725 if Ada_Version >= Ada_05
3726 and then Analyzed (N)
3727 and then Nkind (Unit (N)) = N_Package_Declaration
3732 Non_Lim_View : Entity_Id;
3735 Decl := First (Visible_Declarations (Specification (Unit (N))));
3736 while Present (Decl) loop
3737 if Nkind (Decl) = N_Subtype_Declaration
3739 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3741 From_With_Type (Defining_Identifier (Decl))
3743 Def_Id := Defining_Identifier (Decl);
3744 Non_Lim_View := Non_Limited_View (Def_Id);
3746 if not Is_Incomplete_Type (Non_Lim_View) then
3748 -- Convert an incomplete subtype declaration into a
3749 -- corresponding non-limited view subtype declaration.
3750 -- This is usually the case when analyzing a body that
3751 -- has regular with-clauses, when the spec has limited
3754 -- If the non-limited view is still incomplete, it is
3755 -- the dummy entry already created, and the declaration
3756 -- cannot be reanalyzed. This is the case when installing
3757 -- a parent unit that has limited with-clauses.
3759 Set_Subtype_Indication (Decl,
3760 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3761 Set_Etype (Def_Id, Non_Lim_View);
3762 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3763 Set_Analyzed (Decl, False);
3765 -- Reanalyze the declaration, suppressing the call to
3766 -- Enter_Name to avoid duplicate names.
3768 Analyze_Subtype_Declaration
3778 end Install_Limited_Context_Clauses;
3780 ---------------------
3781 -- Install_Parents --
3782 ---------------------
3784 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3791 P := Unit (Parent_Spec (Lib_Unit));
3792 P_Name := Get_Parent_Entity (P);
3794 if Etype (P_Name) = Any_Type then
3798 if Ekind (P_Name) = E_Generic_Package
3799 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3800 N_Generic_Package_Declaration)
3801 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3804 ("child of a generic package must be a generic unit", Lib_Unit);
3806 elsif not Is_Package_Or_Generic_Package (P_Name) then
3808 ("parent unit must be package or generic package", Lib_Unit);
3809 raise Unrecoverable_Error;
3811 elsif Present (Renamed_Object (P_Name)) then
3812 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3813 raise Unrecoverable_Error;
3815 -- Verify that a child of an instance is itself an instance, or the
3816 -- renaming of one. Given that an instance that is a unit is replaced
3817 -- with a package declaration, check against the original node. The
3818 -- parent may be currently being instantiated, in which case it appears
3819 -- as a declaration, but the generic_parent is already established
3820 -- indicating that we deal with an instance.
3822 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3823 if Nkind (Lib_Unit) in N_Renaming_Declaration
3824 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3826 (Nkind (Lib_Unit) = N_Package_Declaration
3827 and then Present (Generic_Parent (Specification (Lib_Unit))))
3832 ("child of an instance must be an instance or renaming",
3837 -- This is the recursive call that ensures all parents are loaded
3839 if Is_Child_Spec (P) then
3841 Is_Private or else Private_Present (Parent (Lib_Unit)));
3844 -- Now we can install the context for this parent
3846 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3847 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3848 Install_Siblings (P_Name, Parent (Lib_Unit));
3850 -- The child unit is in the declarative region of the parent. The parent
3851 -- must therefore appear in the scope stack and be visible, as when
3852 -- compiling the corresponding body. If the child unit is private or it
3853 -- is a package body, private declarations must be accessible as well.
3854 -- Use declarations in the parent must also be installed. Finally, other
3855 -- child units of the same parent that are in the context are
3856 -- immediately visible.
3858 -- Find entity for compilation unit, and set its private descendant
3859 -- status as needed. Indicate that it is a compilation unit, which is
3860 -- redundant in general, but needed if this is a generated child spec
3861 -- for a child body without previous spec.
3863 E_Name := Defining_Entity (Lib_Unit);
3865 Set_Is_Child_Unit (E_Name);
3866 Set_Is_Compilation_Unit (E_Name);
3868 Set_Is_Private_Descendant (E_Name,
3869 Is_Private_Descendant (P_Name)
3870 or else Private_Present (Parent (Lib_Unit)));
3872 P_Spec := Specification (Unit_Declaration_Node (P_Name));
3873 Push_Scope (P_Name);
3875 -- Save current visibility of unit
3877 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3878 Is_Immediately_Visible (P_Name);
3879 Set_Is_Immediately_Visible (P_Name);
3880 Install_Visible_Declarations (P_Name);
3881 Set_Use (Visible_Declarations (P_Spec));
3883 -- If the parent is a generic unit, its formal part may contain formal
3884 -- packages and use clauses for them.
3886 if Ekind (P_Name) = E_Generic_Package then
3887 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3891 or else Private_Present (Parent (Lib_Unit))
3893 Install_Private_Declarations (P_Name);
3894 Install_Private_With_Clauses (P_Name);
3895 Set_Use (Private_Declarations (P_Spec));
3897 end Install_Parents;
3899 ----------------------------------
3900 -- Install_Private_With_Clauses --
3901 ----------------------------------
3903 procedure Install_Private_With_Clauses (P : Entity_Id) is
3904 Decl : constant Node_Id := Unit_Declaration_Node (P);
3908 if Debug_Flag_I then
3909 Write_Str ("install private with clauses of ");
3910 Write_Name (Chars (P));
3914 if Nkind (Parent (Decl)) = N_Compilation_Unit then
3915 Item := First (Context_Items (Parent (Decl)));
3916 while Present (Item) loop
3917 if Nkind (Item) = N_With_Clause
3918 and then Private_Present (Item)
3920 if Limited_Present (Item) then
3921 if not Limited_View_Installed (Item) then
3922 Install_Limited_Withed_Unit (Item);
3925 Install_Withed_Unit (Item, Private_With_OK => True);
3932 end Install_Private_With_Clauses;
3934 ----------------------
3935 -- Install_Siblings --
3936 ----------------------
3938 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3944 -- Iterate over explicit with clauses, and check whether the scope of
3945 -- each entity is an ancestor of the current unit, in which case it is
3946 -- immediately visible.
3948 Item := First (Context_Items (N));
3949 while Present (Item) loop
3951 -- Do not install private_with_clauses declaration, unless unit
3952 -- is itself a private child unit, or is a body. Note that for a
3953 -- subprogram body the private_with_clause does not take effect until
3954 -- after the specification.
3956 if Nkind (Item) /= N_With_Clause
3957 or else Implicit_With (Item)
3958 or else Limited_Present (Item)
3962 elsif not Private_Present (Item)
3963 or else Private_Present (N)
3964 or else Nkind (Unit (N)) = N_Package_Body
3966 Id := Entity (Name (Item));
3968 if Is_Child_Unit (Id)
3969 and then Is_Ancestor_Package (Scope (Id), U_Name)
3971 Set_Is_Immediately_Visible (Id);
3973 -- Check for the presence of another unit in the context that
3974 -- may be inadvertently hidden by the child.
3976 Prev := Current_Entity (Id);
3979 and then Is_Immediately_Visible (Prev)
3980 and then not Is_Child_Unit (Prev)
3986 Clause := First (Context_Items (N));
3987 while Present (Clause) loop
3988 if Nkind (Clause) = N_With_Clause
3989 and then Entity (Name (Clause)) = Prev
3992 ("child unit& hides compilation unit " &
3993 "with the same name?",
4003 -- The With_Clause may be on a grand-child or one of its further
4004 -- descendants, which makes a child immediately visible. Examine
4005 -- ancestry to determine whether such a child exists. For example,
4006 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4007 -- is immediately visible.
4009 elsif Is_Child_Unit (Id) then
4015 while Is_Child_Unit (Par) loop
4016 if Is_Ancestor_Package (Scope (Par), U_Name) then
4017 Set_Is_Immediately_Visible (Par);
4026 -- If the item is a private with-clause on a child unit, the parent
4027 -- may have been installed already, but the child unit must remain
4028 -- invisible until installed in a private part or body, unless there
4029 -- is already a regular with_clause for it in the current unit.
4031 elsif Private_Present (Item) then
4032 Id := Entity (Name (Item));
4034 if Is_Child_Unit (Id) then
4038 function In_Context return Boolean;
4039 -- Scan context of current unit, to check whether there is
4040 -- a with_clause on the same unit as a private with-clause
4041 -- on a parent, in which case child unit is visible. If the
4042 -- unit is a grand-child, the same applies to its parent.
4048 function In_Context return Boolean is
4051 First (Context_Items (Cunit (Current_Sem_Unit)));
4052 while Present (Clause) loop
4053 if Nkind (Clause) = N_With_Clause
4054 and then Comes_From_Source (Clause)
4055 and then Is_Entity_Name (Name (Clause))
4056 and then not Private_Present (Clause)
4058 if Entity (Name (Clause)) = Id
4060 (Nkind (Name (Clause)) = N_Expanded_Name
4061 and then Entity (Prefix (Name (Clause))) = Id)
4074 Set_Is_Visible_Child_Unit (Id, In_Context);
4081 end Install_Siblings;
4083 ---------------------------------
4084 -- Install_Limited_Withed_Unit --
4085 ---------------------------------
4087 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4088 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4091 Is_Child_Package : Boolean := False;
4092 Lim_Header : Entity_Id;
4093 Lim_Typ : Entity_Id;
4095 procedure Check_Body_Required;
4096 -- A unit mentioned in a limited with_clause may not be mentioned in
4097 -- a regular with_clause, but must still be included in the current
4098 -- partition. We need to determine whether the unit needs a body, so
4099 -- that the binder can determine the name of the file to be compiled.
4100 -- Checking whether a unit needs a body can be done without semantic
4101 -- analysis, by examining the nature of the declarations in the package.
4103 function Has_Limited_With_Clause
4104 (C_Unit : Entity_Id;
4105 Pack : Entity_Id) return Boolean;
4106 -- Determine whether any package in the ancestor chain starting with
4107 -- C_Unit has a limited with clause for package Pack.
4109 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4110 -- Check if some package installed though normal with-clauses has a
4111 -- renaming declaration of package P. AARM 10.1.2(21/2).
4113 -------------------------
4114 -- Check_Body_Required --
4115 -------------------------
4117 procedure Check_Body_Required is
4118 PA : constant List_Id :=
4119 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4121 procedure Check_Declarations (Spec : Node_Id);
4122 -- Recursive procedure that does the work and checks nested packages
4124 ------------------------
4125 -- Check_Declarations --
4126 ------------------------
4128 procedure Check_Declarations (Spec : Node_Id) is
4130 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4132 Subp_List : constant Elist_Id := New_Elmt_List;
4134 procedure Check_Pragma_Import (P : Node_Id);
4135 -- If a pragma import applies to a previous subprogram, the
4136 -- enclosing unit may not need a body. The processing is syntactic
4137 -- and does not require a declaration to be analyzed. The code
4138 -- below also handles pragma Import when applied to a subprogram
4139 -- that renames another. In this case the pragma applies to the
4142 -- Chains of multiple renames are not handled by the code below.
4143 -- It is probably impossible to handle all cases without proper
4144 -- name resolution. In such cases the algorithm is conservative
4145 -- and will indicate that a body is needed???
4147 -------------------------
4148 -- Check_Pragma_Import --
4149 -------------------------
4151 procedure Check_Pragma_Import (P : Node_Id) is
4157 procedure Remove_Homonyms (E : Node_Id);
4158 -- Make one pass over list of subprograms. Called again if
4159 -- subprogram is a renaming. E is known to be an identifier.
4161 ---------------------
4162 -- Remove_Homonyms --
4163 ---------------------
4165 procedure Remove_Homonyms (E : Node_Id) is
4166 R : Entity_Id := Empty;
4167 -- Name of renamed entity, if any
4170 Subp_Id := First_Elmt (Subp_List);
4171 while Present (Subp_Id) loop
4172 if Chars (Node (Subp_Id)) = Chars (E) then
4173 if Nkind (Parent (Parent (Node (Subp_Id))))
4174 /= N_Subprogram_Renaming_Declaration
4177 Next_Elmt (Subp_Id);
4178 Remove_Elmt (Subp_List, Prev_Id);
4180 R := Name (Parent (Parent (Node (Subp_Id))));
4184 Next_Elmt (Subp_Id);
4189 if Nkind (R) = N_Identifier then
4190 Remove_Homonyms (R);
4192 elsif Nkind (R) = N_Selected_Component then
4193 Remove_Homonyms (Selector_Name (R));
4195 -- Renaming of attribute
4201 end Remove_Homonyms;
4203 -- Start of processing for Check_Pragma_Import
4206 -- Find name of entity in Import pragma. We have not analyzed
4207 -- the construct, so we must guard against syntax errors.
4209 Arg := Next (First (Pragma_Argument_Associations (P)));
4212 or else Nkind (Expression (Arg)) /= N_Identifier
4216 Imported := Expression (Arg);
4219 Remove_Homonyms (Imported);
4220 end Check_Pragma_Import;
4222 -- Start of processing for Check_Declarations
4225 -- Search for Elaborate Body pragma
4227 Decl := First (Visible_Declarations (Spec));
4228 while Present (Decl)
4229 and then Nkind (Decl) = N_Pragma
4231 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4232 Set_Body_Required (Library_Unit (N));
4239 -- Look for declarations that require the presence of a body. We
4240 -- have already skipped pragmas at the start of the list.
4242 while Present (Decl) loop
4244 -- Subprogram that comes from source means body may be needed.
4245 -- Save for subsequent examination of import pragmas.
4247 if Comes_From_Source (Decl)
4248 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4249 N_Subprogram_Renaming_Declaration,
4250 N_Generic_Subprogram_Declaration))
4252 Append_Elmt (Defining_Entity (Decl), Subp_List);
4254 -- Package declaration of generic package declaration. We need
4255 -- to recursively examine nested declarations.
4257 elsif Nkind_In (Decl, N_Package_Declaration,
4258 N_Generic_Package_Declaration)
4260 Check_Declarations (Specification (Decl));
4262 elsif Nkind (Decl) = N_Pragma
4263 and then Pragma_Name (Decl) = Name_Import
4265 Check_Pragma_Import (Decl);
4271 -- Same set of tests for private part. In addition to subprograms
4272 -- detect the presence of Taft Amendment types (incomplete types
4273 -- completed in the body).
4275 Decl := First (Private_Declarations (Spec));
4276 while Present (Decl) loop
4277 if Comes_From_Source (Decl)
4278 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4279 N_Subprogram_Renaming_Declaration,
4280 N_Generic_Subprogram_Declaration))
4282 Append_Elmt (Defining_Entity (Decl), Subp_List);
4284 elsif Nkind_In (Decl, N_Package_Declaration,
4285 N_Generic_Package_Declaration)
4287 Check_Declarations (Specification (Decl));
4289 -- Collect incomplete type declarations for separate pass
4291 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4292 Append_Elmt (Decl, Incomplete_Decls);
4294 elsif Nkind (Decl) = N_Pragma
4295 and then Pragma_Name (Decl) = Name_Import
4297 Check_Pragma_Import (Decl);
4303 -- Now check incomplete declarations to locate Taft amendment
4304 -- types. This can be done by examining the defining identifiers
4305 -- of type declarations without real semantic analysis.
4311 Inc := First_Elmt (Incomplete_Decls);
4312 while Present (Inc) loop
4313 Decl := Next (Node (Inc));
4314 while Present (Decl) loop
4315 if Nkind (Decl) = N_Full_Type_Declaration
4316 and then Chars (Defining_Identifier (Decl)) =
4317 Chars (Defining_Identifier (Node (Inc)))
4325 -- If no completion, this is a TAT, and a body is needed
4328 Set_Body_Required (Library_Unit (N));
4336 -- Finally, check whether there are subprograms that still
4337 -- require a body, i.e. are not renamings or null.
4339 if not Is_Empty_Elmt_List (Subp_List) then
4345 Subp_Id := First_Elmt (Subp_List);
4346 Spec := Parent (Node (Subp_Id));
4348 while Present (Subp_Id) loop
4349 if Nkind (Parent (Spec))
4350 = N_Subprogram_Renaming_Declaration
4354 elsif Nkind (Spec) = N_Procedure_Specification
4355 and then Null_Present (Spec)
4360 Set_Body_Required (Library_Unit (N));
4364 Next_Elmt (Subp_Id);
4368 end Check_Declarations;
4370 -- Start of processing for Check_Body_Required
4373 -- If this is an imported package (Java and CIL usage) no body is
4374 -- needed. Scan list of pragmas that may follow a compilation unit
4375 -- to look for a relevant pragma Import.
4377 if Present (PA) then
4383 while Present (Prag) loop
4384 if Nkind (Prag) = N_Pragma
4385 and then Get_Pragma_Id (Prag) = Pragma_Import
4395 Check_Declarations (Specification (P_Unit));
4396 end Check_Body_Required;
4398 -----------------------------
4399 -- Has_Limited_With_Clause --
4400 -----------------------------
4402 function Has_Limited_With_Clause
4403 (C_Unit : Entity_Id;
4404 Pack : Entity_Id) return Boolean
4411 while Present (Par) loop
4412 if Ekind (Par) /= E_Package then
4416 -- Retrieve the Compilation_Unit node for Par and determine if
4417 -- its context clauses contain a limited with for Pack.
4419 Par_Unit := Parent (Parent (Parent (Par)));
4421 if Nkind (Par_Unit) = N_Package_Declaration then
4422 Par_Unit := Parent (Par_Unit);
4425 if Has_With_Clause (Par_Unit, Pack, True) then
4429 -- If there are more ancestors, climb up the tree, otherwise
4432 if Is_Child_Unit (Par) then
4440 end Has_Limited_With_Clause;
4442 ----------------------------------
4443 -- Is_Visible_Through_Renamings --
4444 ----------------------------------
4446 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4447 Kind : constant Node_Kind :=
4448 Nkind (Unit (Cunit (Current_Sem_Unit)));
4454 -- Example of the error detected by this subprogram:
4462 -- package Ren_P renames P;
4468 -- limited with P; -- ERROR
4469 -- package R.C is ...
4471 Aux_Unit := Cunit (Current_Sem_Unit);
4474 Item := First (Context_Items (Aux_Unit));
4475 while Present (Item) loop
4476 if Nkind (Item) = N_With_Clause
4477 and then not Limited_Present (Item)
4478 and then Nkind (Unit (Library_Unit (Item))) =
4479 N_Package_Declaration
4482 First (Visible_Declarations
4483 (Specification (Unit (Library_Unit (Item)))));
4484 while Present (Decl) loop
4485 if Nkind (Decl) = N_Package_Renaming_Declaration
4486 and then Entity (Name (Decl)) = P
4488 -- Generate the error message only if the current unit
4489 -- is a package declaration; in case of subprogram
4490 -- bodies and package bodies we just return True to
4491 -- indicate that the limited view must not be
4494 if Kind = N_Package_Declaration then
4496 ("simultaneous visibility of the limited and " &
4497 "unlimited views not allowed", N);
4498 Error_Msg_Sloc := Sloc (Item);
4500 ("\\ unlimited view of & visible through the " &
4501 "context clause #", N, P);
4502 Error_Msg_Sloc := Sloc (Decl);
4503 Error_Msg_NE ("\\ and the renaming #", N, P);
4516 -- If it is a body not acting as spec, follow pointer to the
4517 -- corresponding spec, otherwise follow pointer to parent spec.
4519 if Present (Library_Unit (Aux_Unit))
4520 and then Nkind_In (Unit (Aux_Unit),
4521 N_Package_Body, N_Subprogram_Body)
4523 if Aux_Unit = Library_Unit (Aux_Unit) then
4525 -- Aux_Unit is a body that acts as a spec. Clause has
4526 -- already been flagged as illegal.
4531 Aux_Unit := Library_Unit (Aux_Unit);
4535 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4538 exit when No (Aux_Unit);
4542 end Is_Visible_Through_Renamings;
4544 -- Start of processing for Install_Limited_Withed_Unit
4547 pragma Assert (not Limited_View_Installed (N));
4549 -- In case of limited with_clause on subprograms, generics, instances,
4550 -- or renamings, the corresponding error was previously posted and we
4551 -- have nothing to do here. If the file is missing altogether, it has
4552 -- no source location.
4554 if Nkind (P_Unit) /= N_Package_Declaration
4555 or else Sloc (P_Unit) = No_Location
4560 P := Defining_Unit_Name (Specification (P_Unit));
4562 -- Handle child packages
4564 if Nkind (P) = N_Defining_Program_Unit_Name then
4565 Is_Child_Package := True;
4566 P := Defining_Identifier (P);
4569 -- Do not install the limited-view if the context of the unit is already
4570 -- available through a regular with clause.
4572 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4573 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4578 -- Do not install the limited-view if the full-view is already visible
4579 -- through renaming declarations.
4581 if Is_Visible_Through_Renamings (P) then
4585 -- Do not install the limited view if this is the unit being analyzed.
4586 -- This unusual case will happen when a unit has a limited_with clause
4587 -- on one of its children. The compilation of the child forces the
4588 -- load of the parent which tries to install the limited view of the
4589 -- child again. Installing the limited view must also be disabled
4590 -- when compiling the body of the child unit.
4592 if P = Cunit_Entity (Current_Sem_Unit)
4594 (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4595 and then P = Main_Unit_Entity)
4600 -- This scenario is similar to the one above, the difference is that
4601 -- the compilation of sibling Par.Sib forces the load of parent Par
4602 -- which tries to install the limited view of Lim_Pack [1]. However
4603 -- Par.Sib has a with clause for Lim_Pack [2] in its body, and thus
4604 -- needs the non-limited views of all entities from Lim_Pack.
4606 -- limited with Lim_Pack; -- [1]
4607 -- package Par is ... package Lim_Pack is ...
4609 -- with Lim_Pack; -- [2]
4610 -- package Par.Sib is ... package body Par.Sib is ...
4612 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4613 -- Sem_Unit is the body of Par.Sib.
4615 if Ekind (P) = E_Package
4616 and then Ekind (Main_Unit_Entity) = E_Package
4617 and then Is_Child_Unit (Main_Unit_Entity)
4619 -- The body has a regular with clause
4621 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4622 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4624 -- One of the ancestors has a limited with clause
4626 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4627 N_Package_Specification
4628 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4633 -- A common use of the limited-with is to have a limited-with
4634 -- in the package spec, and a normal with in its package body.
4637 -- limited with X; -- [1]
4641 -- package body A is ...
4643 -- The compilation of A's body installs the context clauses found at [2]
4644 -- and then the context clauses of its specification (found at [1]). As
4645 -- a consequence, at [1] the specification of X has been analyzed and it
4646 -- is immediately visible. According to the semantics of limited-with
4647 -- context clauses we don't install the limited view because the full
4648 -- view of X supersedes its limited view.
4650 if Analyzed (P_Unit)
4652 (Is_Immediately_Visible (P)
4653 or else (Is_Child_Package and then Is_Visible_Child_Unit (P)))
4658 if Debug_Flag_I then
4659 Write_Str ("install limited view of ");
4660 Write_Name (Chars (P));
4664 -- If the unit has not been analyzed and the limited view has not been
4665 -- already installed then we install it.
4667 if not Analyzed (P_Unit) then
4668 if not In_Chain (P) then
4670 -- Minimum decoration
4672 Set_Ekind (P, E_Package);
4673 Set_Etype (P, Standard_Void_Type);
4674 Set_Scope (P, Standard_Standard);
4676 if Is_Child_Package then
4677 Set_Is_Child_Unit (P);
4678 Set_Is_Visible_Child_Unit (P);
4679 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4682 -- Place entity on visibility structure
4684 Set_Homonym (P, Current_Entity (P));
4685 Set_Current_Entity (P);
4687 if Debug_Flag_I then
4688 Write_Str (" (homonym) chain ");
4689 Write_Name (Chars (P));
4693 -- Install the incomplete view. The first element of the limited
4694 -- view is a header (an E_Package entity) used to reference the
4695 -- first shadow entity in the private part of the package.
4697 Lim_Header := Limited_View (P);
4698 Lim_Typ := First_Entity (Lim_Header);
4700 while Present (Lim_Typ)
4701 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4703 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4704 Set_Current_Entity (Lim_Typ);
4706 if Debug_Flag_I then
4707 Write_Str (" (homonym) chain ");
4708 Write_Name (Chars (Lim_Typ));
4712 Next_Entity (Lim_Typ);
4716 -- If the unit appears in a previous regular with_clause, the regular
4717 -- entities of the public part of the withed package must be replaced
4718 -- by the shadow ones.
4720 -- This code must be kept synchronized with the code that replaces the
4721 -- shadow entities by the real entities (see body of Remove_Limited
4722 -- With_Clause); otherwise the contents of the homonym chains are not
4726 -- Hide all the type entities of the public part of the package to
4727 -- avoid its usage. This is needed to cover all the subtype decla-
4728 -- rations because we do not remove them from the homonym chain.
4730 E := First_Entity (P);
4731 while Present (E) and then E /= First_Private_Entity (P) loop
4733 Set_Was_Hidden (E, Is_Hidden (E));
4740 -- Replace the real entities by the shadow entities of the limited
4741 -- view. The first element of the limited view is a header that is
4742 -- used to reference the first shadow entity in the private part
4743 -- of the package. Successive elements are the limited views of the
4744 -- type (including regular incomplete types) declared in the package.
4746 Lim_Header := Limited_View (P);
4748 Lim_Typ := First_Entity (Lim_Header);
4749 while Present (Lim_Typ)
4750 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4752 pragma Assert (not In_Chain (Lim_Typ));
4754 -- Do not unchain nested packages and child units
4756 if Ekind (Lim_Typ) /= E_Package
4757 and then not Is_Child_Unit (Lim_Typ)
4763 Prev := Current_Entity (Lim_Typ);
4766 -- Replace E in the homonyms list, so that the limited
4767 -- view becomes available.
4769 if E = Non_Limited_View (Lim_Typ) then
4770 Set_Homonym (Lim_Typ, Homonym (Prev));
4771 Set_Current_Entity (Lim_Typ);
4775 E := Homonym (Prev);
4777 -- E may have been removed when installing a
4778 -- previous limited_with_clause.
4782 exit when E = Non_Limited_View (Lim_Typ);
4784 Prev := Homonym (Prev);
4788 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4789 Set_Homonym (Prev, Lim_Typ);
4794 if Debug_Flag_I then
4795 Write_Str (" (homonym) chain ");
4796 Write_Name (Chars (Lim_Typ));
4801 Next_Entity (Lim_Typ);
4805 -- The package must be visible while the limited-with clause is active
4806 -- because references to the type P.T must resolve in the usual way.
4807 -- In addition, we remember that the limited-view has been installed to
4808 -- uninstall it at the point of context removal.
4810 Set_Is_Immediately_Visible (P);
4811 Set_Limited_View_Installed (N);
4813 -- If unit has not been analyzed in some previous context, check
4814 -- (imperfectly ???) whether it might need a body.
4816 if not Analyzed (P_Unit) then
4817 Check_Body_Required;
4820 -- If the package in the limited_with clause is a child unit, the
4821 -- clause is unanalyzed and appears as a selected component. Recast
4822 -- it as an expanded name so that the entity can be properly set. Use
4823 -- entity of parent, if available, for higher ancestors in the name.
4825 if Nkind (Name (N)) = N_Selected_Component then
4833 while Nkind (Nam) = N_Selected_Component
4834 and then Present (Ent)
4836 Change_Selected_Component_To_Expanded_Name (Nam);
4838 -- Set entity of parent identifiers if the unit is a child
4839 -- unit. This ensures that the tree is properly formed from
4840 -- semantic point of view (e.g. for ASIS queries).
4842 Set_Entity (Nam, Ent);
4844 Nam := Prefix (Nam);
4847 -- Set entity of last ancestor
4849 if Nkind (Nam) = N_Identifier then
4850 Set_Entity (Nam, Ent);
4856 Set_Entity (Name (N), P);
4857 Set_From_With_Type (P);
4858 end Install_Limited_Withed_Unit;
4860 -------------------------
4861 -- Install_Withed_Unit --
4862 -------------------------
4864 procedure Install_Withed_Unit
4865 (With_Clause : Node_Id;
4866 Private_With_OK : Boolean := False)
4868 Uname : constant Entity_Id := Entity (Name (With_Clause));
4869 P : constant Entity_Id := Scope (Uname);
4872 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
4873 -- compiling a package declaration and the Private_With_OK flag was not
4874 -- set by the caller. These declarations will be installed later (before
4875 -- analyzing the private part of the package).
4877 if Private_Present (With_Clause)
4878 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
4879 and then not (Private_With_OK)
4884 if Debug_Flag_I then
4885 if Private_Present (With_Clause) then
4886 Write_Str ("install private withed unit ");
4888 Write_Str ("install withed unit ");
4891 Write_Name (Chars (Uname));
4895 -- We do not apply the restrictions to an internal unit unless we are
4896 -- compiling the internal unit as a main unit. This check is also
4897 -- skipped for dummy units (for missing packages).
4899 if Sloc (Uname) /= No_Location
4900 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
4901 or else Current_Sem_Unit = Main_Unit)
4903 Check_Restricted_Unit
4904 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
4907 if P /= Standard_Standard then
4909 -- If the unit is not analyzed after analysis of the with clause and
4910 -- it is an instantiation then it awaits a body and is the main unit.
4911 -- Its appearance in the context of some other unit indicates a
4912 -- circular dependency (DEC suite perversity).
4914 if not Analyzed (Uname)
4915 and then Nkind (Parent (Uname)) = N_Package_Instantiation
4918 ("instantiation depends on itself", Name (With_Clause));
4920 elsif not Is_Visible_Child_Unit (Uname) then
4921 Set_Is_Visible_Child_Unit (Uname);
4923 -- If the child unit appears in the context of its parent, it is
4924 -- immediately visible.
4926 if In_Open_Scopes (Scope (Uname)) then
4927 Set_Is_Immediately_Visible (Uname);
4930 if Is_Generic_Instance (Uname)
4931 and then Ekind (Uname) in Subprogram_Kind
4933 -- Set flag as well on the visible entity that denotes the
4934 -- instance, which renames the current one.
4936 Set_Is_Visible_Child_Unit
4938 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
4941 -- The parent unit may have been installed already, and may have
4942 -- appeared in a use clause.
4944 if In_Use (Scope (Uname)) then
4945 Set_Is_Potentially_Use_Visible (Uname);
4948 Set_Context_Installed (With_Clause);
4951 elsif not Is_Immediately_Visible (Uname) then
4952 if not Private_Present (With_Clause)
4953 or else Private_With_OK
4955 Set_Is_Immediately_Visible (Uname);
4958 Set_Context_Installed (With_Clause);
4961 -- A with-clause overrides a with-type clause: there are no restric-
4962 -- tions on the use of package entities.
4964 if Ekind (Uname) = E_Package then
4965 Set_From_With_Type (Uname, False);
4968 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
4969 -- unit if there is a visible homograph for it declared in the same
4970 -- declarative region. This pathological case can only arise when an
4971 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
4972 -- G1 has a generic child also named G2, and the context includes with_
4973 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
4974 -- of I1.G2 visible as well. If the child unit is named Standard, do
4975 -- not apply the check to the Standard package itself.
4977 if Is_Child_Unit (Uname)
4978 and then Is_Visible_Child_Unit (Uname)
4979 and then Ada_Version >= Ada_05
4982 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
4988 U2 := Homonym (Uname);
4990 and then U2 /= Standard_Standard
4993 Decl2 := Unit_Declaration_Node (P2);
4995 if Is_Child_Unit (U2)
4996 and then Is_Visible_Child_Unit (U2)
4998 if Is_Generic_Instance (P)
4999 and then Nkind (Decl1) = N_Package_Declaration
5000 and then Generic_Parent (Specification (Decl1)) = P2
5002 Error_Msg_N ("illegal with_clause", With_Clause);
5004 ("\child unit has visible homograph" &
5005 " (RM 8.3(26), 10.1.1(19))",
5009 elsif Is_Generic_Instance (P2)
5010 and then Nkind (Decl2) = N_Package_Declaration
5011 and then Generic_Parent (Specification (Decl2)) = P
5013 -- With_clause for child unit of instance appears before
5014 -- in the context. We want to place the error message on
5015 -- it, not on the generic child unit itself.
5018 Prev_Clause : Node_Id;
5021 Prev_Clause := First (List_Containing (With_Clause));
5022 while Entity (Name (Prev_Clause)) /= U2 loop
5026 pragma Assert (Present (Prev_Clause));
5027 Error_Msg_N ("illegal with_clause", Prev_Clause);
5029 ("\child unit has visible homograph" &
5030 " (RM 8.3(26), 10.1.1(19))",
5041 end Install_Withed_Unit;
5047 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5048 K : constant Node_Kind := Nkind (Lib_Unit);
5051 return (K in N_Generic_Declaration or else
5052 K in N_Generic_Instantiation or else
5053 K in N_Generic_Renaming_Declaration or else
5054 K = N_Package_Declaration or else
5055 K = N_Package_Renaming_Declaration or else
5056 K = N_Subprogram_Declaration or else
5057 K = N_Subprogram_Renaming_Declaration)
5058 and then Present (Parent_Spec (Lib_Unit));
5061 ------------------------------------
5062 -- Is_Legal_Shadow_Entity_In_Body --
5063 ------------------------------------
5065 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5066 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5068 return Nkind (Unit (C_Unit)) = N_Package_Body
5071 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5072 end Is_Legal_Shadow_Entity_In_Body;
5074 -----------------------
5075 -- Load_Needed_Body --
5076 -----------------------
5078 -- N is a generic unit named in a with clause, or else it is a unit that
5079 -- contains a generic unit or an inlined function. In order to perform an
5080 -- instantiation, the body of the unit must be present. If the unit itself
5081 -- is generic, we assume that an instantiation follows, and load & analyze
5082 -- the body unconditionally. This forces analysis of the spec as well.
5084 -- If the unit is not generic, but contains a generic unit, it is loaded on
5085 -- demand, at the point of instantiation (see ch12).
5087 procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
5088 Body_Name : Unit_Name_Type;
5089 Unum : Unit_Number_Type;
5091 Save_Style_Check : constant Boolean := Opt.Style_Check;
5092 -- The loading and analysis is done with style checks off
5095 if not GNAT_Mode then
5096 Style_Check := False;
5099 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5102 (Load_Name => Body_Name,
5108 if Unum = No_Unit then
5112 Compiler_State := Analyzing; -- reset after load
5114 if not Fatal_Error (Unum) or else Try_Semantics then
5115 if Debug_Flag_L then
5116 Write_Str ("*** Loaded generic body");
5120 Semantics (Cunit (Unum));
5126 Style_Check := Save_Style_Check;
5127 end Load_Needed_Body;
5129 -------------------------
5130 -- Build_Limited_Views --
5131 -------------------------
5133 procedure Build_Limited_Views (N : Node_Id) is
5134 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
5135 P : constant Entity_Id := Cunit_Entity (Unum);
5137 Spec : Node_Id; -- To denote a package specification
5138 Lim_Typ : Entity_Id; -- To denote shadow entities
5139 Comp_Typ : Entity_Id; -- To denote real entities
5141 Lim_Header : Entity_Id; -- Package entity
5142 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
5143 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
5145 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id);
5146 -- Add attributes of an incomplete type to a shadow entity. The same
5147 -- attributes are placed on the real entity, so that gigi receives
5148 -- a consistent view.
5150 procedure Decorate_Package_Specification (P : Entity_Id);
5151 -- Add attributes of a package entity to the entity in a package
5154 procedure Decorate_Tagged_Type
5158 -- Set basic attributes of tagged type T, including its class_wide type.
5159 -- The parameters Loc, Scope are used to decorate the class_wide type.
5161 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id);
5162 -- Construct list of shadow entities and attach it to entity of
5163 -- package that is mentioned in a limited_with clause.
5165 function New_Internal_Shadow_Entity
5166 (Kind : Entity_Kind;
5167 Sloc_Value : Source_Ptr;
5168 Id_Char : Character) return Entity_Id;
5169 -- Build a new internal entity and append it to the list of shadow
5170 -- entities available through the limited-header
5176 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id) is
5177 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
5178 Is_Tagged : Boolean;
5183 while Present (Decl) loop
5185 -- For each library_package_declaration in the environment, there
5186 -- is an implicit declaration of a *limited view* of that library
5187 -- package. The limited view of a package contains:
5189 -- * For each nested package_declaration, a declaration of the
5190 -- limited view of that package, with the same defining-
5191 -- program-unit name.
5193 -- * For each type_declaration in the visible part, an incomplete
5194 -- type-declaration with the same defining_identifier, whose
5195 -- completion is the type_declaration. If the type_declaration
5196 -- is tagged, then the incomplete_type_declaration is tagged
5199 -- The partial view is tagged if the declaration has the
5200 -- explicit keyword, or else if it is a type extension, both
5201 -- of which can be ascertained syntactically.
5203 if Nkind (Decl) = N_Full_Type_Declaration then
5205 (Nkind (Type_Definition (Decl)) = N_Record_Definition
5206 and then Tagged_Present (Type_Definition (Decl)))
5208 (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
5211 (Record_Extension_Part (Type_Definition (Decl))));
5213 Comp_Typ := Defining_Identifier (Decl);
5215 if not Analyzed_Unit then
5217 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5219 Decorate_Incomplete_Type (Comp_Typ, Scope);
5223 -- Create shadow entity for type
5226 New_Internal_Shadow_Entity
5227 (Kind => Ekind (Comp_Typ),
5228 Sloc_Value => Sloc (Comp_Typ),
5231 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5232 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5233 Set_From_With_Type (Lim_Typ);
5236 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5238 Decorate_Incomplete_Type (Lim_Typ, Scope);
5241 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5243 elsif Nkind_In (Decl, N_Private_Type_Declaration,
5244 N_Incomplete_Type_Declaration,
5245 N_Task_Type_Declaration,
5246 N_Protected_Type_Declaration)
5248 Comp_Typ := Defining_Identifier (Decl);
5251 Nkind_In (Decl, N_Private_Type_Declaration,
5252 N_Incomplete_Type_Declaration)
5253 and then Tagged_Present (Decl);
5255 if not Analyzed_Unit then
5257 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5259 Decorate_Incomplete_Type (Comp_Typ, Scope);
5264 New_Internal_Shadow_Entity
5265 (Kind => Ekind (Comp_Typ),
5266 Sloc_Value => Sloc (Comp_Typ),
5269 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5270 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5271 Set_From_With_Type (Lim_Typ);
5274 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5276 Decorate_Incomplete_Type (Lim_Typ, Scope);
5279 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5281 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5282 Comp_Typ := Defining_Identifier (Decl);
5284 if not Analyzed_Unit then
5285 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5288 -- Create shadow entity for type
5291 New_Internal_Shadow_Entity
5292 (Kind => Ekind (Comp_Typ),
5293 Sloc_Value => Sloc (Comp_Typ),
5296 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5297 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5298 Set_From_With_Type (Lim_Typ);
5300 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5301 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5303 elsif Nkind (Decl) = N_Package_Declaration then
5308 Spec : constant Node_Id := Specification (Decl);
5311 Comp_Typ := Defining_Unit_Name (Spec);
5313 if not Analyzed (Cunit (Unum)) then
5314 Decorate_Package_Specification (Comp_Typ);
5315 Set_Scope (Comp_Typ, Scope);
5319 New_Internal_Shadow_Entity
5320 (Kind => Ekind (Comp_Typ),
5321 Sloc_Value => Sloc (Comp_Typ),
5324 Decorate_Package_Specification (Lim_Typ);
5325 Set_Scope (Lim_Typ, Scope);
5327 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5328 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5329 Set_From_With_Type (Lim_Typ);
5331 -- Note: The non_limited_view attribute is not used
5332 -- for local packages.
5336 First_Decl => First (Visible_Declarations (Spec)));
5344 ------------------------------
5345 -- Decorate_Incomplete_Type --
5346 ------------------------------
5348 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id) is
5350 Set_Ekind (E, E_Incomplete_Type);
5351 Set_Scope (E, Scop);
5353 Set_Is_First_Subtype (E, True);
5354 Set_Stored_Constraint (E, No_Elist);
5355 Set_Full_View (E, Empty);
5356 Init_Size_Align (E);
5357 end Decorate_Incomplete_Type;
5359 --------------------------
5360 -- Decorate_Tagged_Type --
5361 --------------------------
5363 procedure Decorate_Tagged_Type
5371 Decorate_Incomplete_Type (T, Scop);
5372 Set_Is_Tagged_Type (T);
5374 -- Build corresponding class_wide type, if not previously done
5376 -- Note: The class-wide entity is shared by the limited-view
5377 -- and the full-view.
5379 if No (Class_Wide_Type (T)) then
5380 CW := Make_Temporary (Loc, 'S');
5382 -- Set parent to be the same as the parent of the tagged type.
5383 -- We need a parent field set, and it is supposed to point to
5384 -- the declaration of the type. The tagged type declaration
5385 -- essentially declares two separate types, the tagged type
5386 -- itself and the corresponding class-wide type, so it is
5387 -- reasonable for the parent fields to point to the declaration
5390 Set_Parent (CW, Parent (T));
5392 -- Set remaining fields of classwide type
5394 Set_Ekind (CW, E_Class_Wide_Type);
5396 Set_Scope (CW, Scop);
5397 Set_Is_Tagged_Type (CW);
5398 Set_Is_First_Subtype (CW, True);
5399 Init_Size_Align (CW);
5400 Set_Has_Unknown_Discriminants (CW, True);
5401 Set_Class_Wide_Type (CW, CW);
5402 Set_Equivalent_Type (CW, Empty);
5403 Set_From_With_Type (CW, From_With_Type (T));
5405 -- Link type to its class-wide type
5407 Set_Class_Wide_Type (T, CW);
5409 end Decorate_Tagged_Type;
5411 ------------------------------------
5412 -- Decorate_Package_Specification --
5413 ------------------------------------
5415 procedure Decorate_Package_Specification (P : Entity_Id) is
5417 -- Place only the most basic attributes
5419 Set_Ekind (P, E_Package);
5420 Set_Etype (P, Standard_Void_Type);
5421 end Decorate_Package_Specification;
5423 --------------------------------
5424 -- New_Internal_Shadow_Entity --
5425 --------------------------------
5427 function New_Internal_Shadow_Entity
5428 (Kind : Entity_Kind;
5429 Sloc_Value : Source_Ptr;
5430 Id_Char : Character) return Entity_Id
5432 E : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
5435 Set_Ekind (E, Kind);
5436 Set_Is_Internal (E, True);
5438 if Kind in Type_Kind then
5439 Init_Size_Align (E);
5442 Append_Entity (E, Lim_Header);
5445 end New_Internal_Shadow_Entity;
5447 -- Start of processing for Build_Limited_Views
5450 pragma Assert (Limited_Present (N));
5452 -- A library_item mentioned in a limited_with_clause is a package
5453 -- declaration, not a subprogram declaration, generic declaration,
5454 -- generic instantiation, or package renaming declaration.
5456 case Nkind (Unit (Library_Unit (N))) is
5457 when N_Package_Declaration =>
5460 when N_Subprogram_Declaration =>
5461 Error_Msg_N ("subprograms not allowed in "
5462 & "limited with_clauses", N);
5465 when N_Generic_Package_Declaration |
5466 N_Generic_Subprogram_Declaration =>
5467 Error_Msg_N ("generics not allowed in "
5468 & "limited with_clauses", N);
5471 when N_Generic_Instantiation =>
5472 Error_Msg_N ("generic instantiations not allowed in "
5473 & "limited with_clauses", N);
5476 when N_Generic_Renaming_Declaration =>
5477 Error_Msg_N ("generic renamings not allowed in "
5478 & "limited with_clauses", N);
5481 when N_Subprogram_Renaming_Declaration =>
5482 Error_Msg_N ("renamed subprograms not allowed in "
5483 & "limited with_clauses", N);
5486 when N_Package_Renaming_Declaration =>
5487 Error_Msg_N ("renamed packages not allowed in "
5488 & "limited with_clauses", N);
5492 raise Program_Error;
5495 -- Check if the chain is already built
5497 Spec := Specification (Unit (Library_Unit (N)));
5499 if Limited_View_Installed (Spec) then
5503 Set_Ekind (P, E_Package);
5505 -- Build the header of the limited_view
5507 Lim_Header := Make_Temporary (Sloc (N), 'Z');
5508 Set_Ekind (Lim_Header, E_Package);
5509 Set_Is_Internal (Lim_Header);
5510 Set_Limited_View (P, Lim_Header);
5512 -- Create the auxiliary chain. All the shadow entities are appended to
5513 -- the list of entities of the limited-view header
5517 First_Decl => First (Visible_Declarations (Spec)));
5519 -- Save the last built shadow entity. It is needed later to set the
5520 -- reference to the first shadow entity in the private part
5522 Last_Pub_Lim_E := Last_Lim_E;
5524 -- Ada 2005 (AI-262): Add the limited view of the private declarations
5525 -- Required to give support to limited-private-with clauses
5527 Build_Chain (Scope => P,
5528 First_Decl => First (Private_Declarations (Spec)));
5530 if Last_Pub_Lim_E /= Empty then
5531 Set_First_Private_Entity
5532 (Lim_Header, Next_Entity (Last_Pub_Lim_E));
5534 Set_First_Private_Entity
5535 (Lim_Header, First_Entity (P));
5538 Set_Limited_View_Installed (Spec);
5539 end Build_Limited_Views;
5541 -------------------------------
5542 -- Check_Body_Needed_For_SAL --
5543 -------------------------------
5545 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5547 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5548 -- Determine whether use of entity E might require the presence of its
5549 -- body. For a package this requires a recursive traversal of all nested
5552 ---------------------------
5553 -- Entity_Needed_For_SAL --
5554 ---------------------------
5556 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5560 if Is_Subprogram (E)
5561 and then Has_Pragma_Inline (E)
5565 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
5568 elsif Ekind (E) = E_Generic_Package
5570 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5571 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5575 elsif Ekind (E) = E_Package
5576 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5577 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5579 Ent := First_Entity (E);
5580 while Present (Ent) loop
5581 if Entity_Needs_Body (Ent) then
5593 end Entity_Needs_Body;
5595 -- Start of processing for Check_Body_Needed_For_SAL
5598 if Ekind (Unit_Name) = E_Generic_Package
5599 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5600 N_Generic_Package_Declaration
5602 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5604 Set_Body_Needed_For_SAL (Unit_Name);
5606 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
5607 Set_Body_Needed_For_SAL (Unit_Name);
5609 elsif Is_Subprogram (Unit_Name)
5610 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5611 N_Subprogram_Declaration
5612 and then Has_Pragma_Inline (Unit_Name)
5614 Set_Body_Needed_For_SAL (Unit_Name);
5616 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5617 Check_Body_Needed_For_SAL
5618 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5620 elsif Ekind (Unit_Name) = E_Package
5621 and then Entity_Needs_Body (Unit_Name)
5623 Set_Body_Needed_For_SAL (Unit_Name);
5625 elsif Ekind (Unit_Name) = E_Package_Body
5626 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5628 Check_Body_Needed_For_SAL
5629 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5631 end Check_Body_Needed_For_SAL;
5633 --------------------
5634 -- Remove_Context --
5635 --------------------
5637 procedure Remove_Context (N : Node_Id) is
5638 Lib_Unit : constant Node_Id := Unit (N);
5641 -- If this is a child unit, first remove the parent units
5643 if Is_Child_Spec (Lib_Unit) then
5644 Remove_Parents (Lib_Unit);
5647 Remove_Context_Clauses (N);
5650 ----------------------------
5651 -- Remove_Context_Clauses --
5652 ----------------------------
5654 procedure Remove_Context_Clauses (N : Node_Id) is
5656 Unit_Name : Entity_Id;
5659 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5660 -- limited-views first and regular-views later (to maintain the
5663 -- First Phase: Remove limited_with context clauses
5665 Item := First (Context_Items (N));
5666 while Present (Item) loop
5668 -- We are interested only in with clauses which got installed
5671 if Nkind (Item) = N_With_Clause
5672 and then Limited_Present (Item)
5673 and then Limited_View_Installed (Item)
5675 Remove_Limited_With_Clause (Item);
5681 -- Second Phase: Loop through context items and undo regular
5682 -- with_clauses and use_clauses.
5684 Item := First (Context_Items (N));
5685 while Present (Item) loop
5687 -- We are interested only in with clauses which got installed on
5688 -- entry, as indicated by their Context_Installed flag set
5690 if Nkind (Item) = N_With_Clause
5691 and then Limited_Present (Item)
5692 and then Limited_View_Installed (Item)
5696 elsif Nkind (Item) = N_With_Clause
5697 and then Context_Installed (Item)
5699 -- Remove items from one with'ed unit
5701 Unit_Name := Entity (Name (Item));
5702 Remove_Unit_From_Visibility (Unit_Name);
5703 Set_Context_Installed (Item, False);
5705 elsif Nkind (Item) = N_Use_Package_Clause then
5706 End_Use_Package (Item);
5708 elsif Nkind (Item) = N_Use_Type_Clause then
5709 End_Use_Type (Item);
5714 end Remove_Context_Clauses;
5716 --------------------------------
5717 -- Remove_Limited_With_Clause --
5718 --------------------------------
5720 procedure Remove_Limited_With_Clause (N : Node_Id) is
5721 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5724 Lim_Header : Entity_Id;
5725 Lim_Typ : Entity_Id;
5729 pragma Assert (Limited_View_Installed (N));
5731 -- In case of limited with_clause on subprograms, generics, instances,
5732 -- or renamings, the corresponding error was previously posted and we
5733 -- have nothing to do here.
5735 if Nkind (P_Unit) /= N_Package_Declaration then
5739 P := Defining_Unit_Name (Specification (P_Unit));
5741 -- Handle child packages
5743 if Nkind (P) = N_Defining_Program_Unit_Name then
5744 P := Defining_Identifier (P);
5747 if Debug_Flag_I then
5748 Write_Str ("remove limited view of ");
5749 Write_Name (Chars (P));
5750 Write_Str (" from visibility");
5754 -- Prepare the removal of the shadow entities from visibility. The
5755 -- first element of the limited view is a header (an E_Package
5756 -- entity) that is used to reference the first shadow entity in the
5757 -- private part of the package
5759 Lim_Header := Limited_View (P);
5760 Lim_Typ := First_Entity (Lim_Header);
5762 -- Remove package and shadow entities from visibility if it has not
5765 if not Analyzed (P_Unit) then
5767 Set_Is_Immediately_Visible (P, False);
5769 while Present (Lim_Typ) loop
5771 Next_Entity (Lim_Typ);
5774 -- Otherwise this package has already appeared in the closure and its
5775 -- shadow entities must be replaced by its real entities. This code
5776 -- must be kept synchronized with the complementary code in Install
5777 -- Limited_Withed_Unit.
5780 -- Real entities that are type or subtype declarations were hidden
5781 -- from visibility at the point of installation of the limited-view.
5782 -- Now we recover the previous value of the hidden attribute.
5784 E := First_Entity (P);
5785 while Present (E) and then E /= First_Private_Entity (P) loop
5787 Set_Is_Hidden (E, Was_Hidden (E));
5793 while Present (Lim_Typ)
5794 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5796 -- Nested packages and child units were not unchained
5798 if Ekind (Lim_Typ) /= E_Package
5799 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5801 -- If the package has incomplete types, the limited view of the
5802 -- incomplete type is in fact never visible (AI05-129) but we
5803 -- have created a shadow entity E1 for it, that points to E2,
5804 -- a non-limited incomplete type. This in turn has a full view
5805 -- E3 that is the full declaration. There is a corresponding
5806 -- shadow entity E4. When reinstalling the non-limited view,
5807 -- E2 must become the current entity and E3 must be ignored.
5809 E := Non_Limited_View (Lim_Typ);
5811 if Present (Current_Entity (E))
5812 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
5813 and then Full_View (Current_Entity (E)) = E
5816 -- Lim_Typ is the limited view of a full type declaration
5817 -- that has a previous incomplete declaration, i.e. E3 from
5818 -- the previous description. Nothing to insert.
5823 pragma Assert (not In_Chain (E));
5825 Prev := Current_Entity (Lim_Typ);
5827 if Prev = Lim_Typ then
5828 Set_Current_Entity (E);
5831 while Present (Prev)
5832 and then Homonym (Prev) /= Lim_Typ
5834 Prev := Homonym (Prev);
5837 if Present (Prev) then
5838 Set_Homonym (Prev, E);
5842 -- Preserve structure of homonym chain
5844 Set_Homonym (E, Homonym (Lim_Typ));
5848 Next_Entity (Lim_Typ);
5852 -- Indicate that the limited view of the package is not installed
5854 Set_From_With_Type (P, False);
5855 Set_Limited_View_Installed (N, False);
5856 end Remove_Limited_With_Clause;
5858 --------------------
5859 -- Remove_Parents --
5860 --------------------
5862 procedure Remove_Parents (Lib_Unit : Node_Id) is
5865 P_Spec : Node_Id := Empty;
5867 Vis : constant Boolean :=
5868 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
5871 if Is_Child_Spec (Lib_Unit) then
5872 P_Spec := Parent_Spec (Lib_Unit);
5874 elsif Nkind (Lib_Unit) = N_Package_Body
5875 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
5877 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
5880 if Present (P_Spec) then
5882 P_Name := Get_Parent_Entity (P);
5883 Remove_Context_Clauses (P_Spec);
5884 End_Package_Scope (P_Name);
5885 Set_Is_Immediately_Visible (P_Name, Vis);
5887 -- Remove from visibility the siblings as well, which are directly
5888 -- visible while the parent is in scope.
5890 E := First_Entity (P_Name);
5891 while Present (E) loop
5892 if Is_Child_Unit (E) then
5893 Set_Is_Immediately_Visible (E, False);
5899 Set_In_Package_Body (P_Name, False);
5901 -- This is the recursive call to remove the context of any higher
5902 -- level parent. This recursion ensures that all parents are removed
5903 -- in the reverse order of their installation.
5909 ---------------------------------
5910 -- Remove_Private_With_Clauses --
5911 ---------------------------------
5913 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
5916 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
5917 -- Check whether a given unit appears in a regular with_clause. Used to
5918 -- determine whether a private_with_clause, implicit or explicit, should
5921 ----------------------------
5922 -- In_Regular_With_Clause --
5923 ----------------------------
5925 function In_Regular_With_Clause (E : Entity_Id) return Boolean
5930 Item := First (Context_Items (Comp_Unit));
5931 while Present (Item) loop
5932 if Nkind (Item) = N_With_Clause
5933 and then Entity (Name (Item)) = E
5934 and then not Private_Present (Item)
5942 end In_Regular_With_Clause;
5944 -- Start of processing for Remove_Private_With_Clauses
5947 Item := First (Context_Items (Comp_Unit));
5948 while Present (Item) loop
5949 if Nkind (Item) = N_With_Clause
5950 and then Private_Present (Item)
5952 -- If private_with_clause is redundant, remove it from context,
5953 -- as a small optimization to subsequent handling of private_with
5954 -- clauses in other nested packages.
5956 if In_Regular_With_Clause (Entity (Name (Item))) then
5958 Nxt : constant Node_Id := Next (Item);
5964 elsif Limited_Present (Item) then
5965 if not Limited_View_Installed (Item) then
5966 Remove_Limited_With_Clause (Item);
5972 Remove_Unit_From_Visibility (Entity (Name (Item)));
5973 Set_Context_Installed (Item, False);
5981 end Remove_Private_With_Clauses;
5983 ---------------------------------
5984 -- Remove_Unit_From_Visibility --
5985 ---------------------------------
5987 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
5988 P : constant Entity_Id := Scope (Unit_Name);
5991 if Debug_Flag_I then
5992 Write_Str ("remove unit ");
5993 Write_Name (Chars (Unit_Name));
5994 Write_Str (" from visibility");
5998 if P /= Standard_Standard then
5999 Set_Is_Visible_Child_Unit (Unit_Name, False);
6002 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6003 Set_Is_Immediately_Visible (Unit_Name, False);
6004 end Remove_Unit_From_Visibility;
6019 procedure Unchain (E : Entity_Id) is
6023 Prev := Current_Entity (E);
6029 Set_Name_Entity_Id (Chars (E), Homonym (E));
6032 while Present (Prev)
6033 and then Homonym (Prev) /= E
6035 Prev := Homonym (Prev);
6038 if Present (Prev) then
6039 Set_Homonym (Prev, Homonym (E));
6043 if Debug_Flag_I then
6044 Write_Str (" (homonym) unchain ");
6045 Write_Name (Chars (E));