1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Util; use Exp_Util;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Inline; use Inline;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rtsfind; use Rtsfind;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dist; use Sem_Dist;
53 with Sem_Prag; use Sem_Prag;
54 with Sem_Util; use Sem_Util;
55 with Sem_Warn; use Sem_Warn;
56 with Stand; use Stand;
57 with Sinfo; use Sinfo;
58 with Sinfo.CN; use Sinfo.CN;
59 with Sinput; use Sinput;
60 with Snames; use Snames;
61 with Style; use Style;
62 with Stylesw; use Stylesw;
63 with Tbuild; use Tbuild;
64 with Ttypes; use Ttypes;
65 with Uname; use Uname;
67 package body Sem_Ch10 is
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 procedure Analyze_Context (N : Node_Id);
74 -- Analyzes items in the context clause of compilation unit
76 procedure Build_Limited_Views (N : Node_Id);
77 -- Build and decorate the list of shadow entities for a package mentioned
78 -- in a limited_with clause. If the package was not previously analyzed
79 -- then it also performs a basic decoration of the real entities; this
80 -- is required to do not pass non-decorated entities to the back-end.
81 -- Implements Ada 2005 (AI-50217).
83 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
84 -- Check whether the source for the body of a compilation unit must
85 -- be included in a standalone library.
87 procedure Check_With_Type_Clauses (N : Node_Id);
88 -- If N is a body, verify that any with_type clauses on the spec, or
89 -- on the spec of any parent, have a matching with_clause.
91 procedure Check_Private_Child_Unit (N : Node_Id);
92 -- If a with_clause mentions a private child unit, the compilation
93 -- unit must be a member of the same family, as described in 10.1.2 (8).
95 procedure Check_Stub_Level (N : Node_Id);
96 -- Verify that a stub is declared immediately within a compilation unit,
97 -- and not in an inner frame.
99 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
100 -- When a child unit appears in a context clause, the implicit withs on
101 -- parents are made explicit, and with clauses are inserted in the context
102 -- clause before the one for the child. If a parent in the with_clause
103 -- is a renaming, the implicit with_clause is on the renaming whose name
104 -- is mentioned in the with_clause, and not on the package it renames.
105 -- N is the compilation unit whose list of context items receives the
106 -- implicit with_clauses.
108 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
109 -- Get defining entity of parent unit of a child unit. In most cases this
110 -- is the defining entity of the unit, but for a child instance whose
111 -- parent needs a body for inlining, the instantiation node of the parent
112 -- has not yet been rewritten as a package declaration, and the entity has
113 -- to be retrieved from the Instance_Spec of the unit.
115 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
116 -- If the main unit is a child unit, implicit withs are also added for
117 -- all its ancestors.
119 function In_Chain (E : Entity_Id) return Boolean;
120 -- Check that the shadow entity is not already in the homonym chain, for
121 -- example through a limited_with clause in a parent unit.
123 procedure Install_Context_Clauses (N : Node_Id);
124 -- Subsidiary to Install_Context and Install_Parents. Process only with_
125 -- and use_clauses for current unit and its library unit if any.
127 procedure Install_Limited_Context_Clauses (N : Node_Id);
128 -- Subsidiary to Install_Context. Process only limited with_clauses
129 -- for current unit. Implements Ada 2005 (AI-50217).
131 procedure Install_Limited_Withed_Unit (N : Node_Id);
132 -- Place shadow entities for a limited_with package in the visibility
133 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
135 procedure Install_Withed_Unit
136 (With_Clause : Node_Id;
137 Private_With_OK : Boolean := False);
138 -- If the unit is not a child unit, make unit immediately visible.
139 -- The caller ensures that the unit is not already currently installed.
140 -- The flag Private_With_OK is set true in Install_Private_With_Clauses,
141 -- which is called when compiling the private part of a package, or
142 -- installing the private declarations of a parent unit.
144 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
145 -- This procedure establishes the context for the compilation of a child
146 -- unit. If Lib_Unit is a child library spec then the context of the parent
147 -- is installed, and the parent itself made immediately visible, so that
148 -- the child unit is processed in the declarative region of the parent.
149 -- Install_Parents makes a recursive call to itself to ensure that all
150 -- parents are loaded in the nested case. If Lib_Unit is a library body,
151 -- the only effect of Install_Parents is to install the private decls of
152 -- the parents, because the visible parent declarations will have been
153 -- installed as part of the context of the corresponding spec.
155 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
156 -- In the compilation of a child unit, a child of any of the ancestor
157 -- units is directly visible if it is visible, because the parent is in
158 -- an enclosing scope. Iterate over context to find child units of U_Name
159 -- or of some ancestor of it.
161 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
162 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
163 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
164 -- a library spec that has a parent. If the call to Is_Child_Spec returns
165 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
166 -- compilation unit for the parent spec.
168 -- Lib_Unit can also be a subprogram body that acts as its own spec. If
169 -- the Parent_Spec is non-empty, this is also a child unit.
171 procedure Remove_With_Type_Clause (Name : Node_Id);
172 -- Remove imported type and its enclosing package from visibility, and
173 -- remove attributes of imported type so they don't interfere with its
174 -- analysis (should it appear otherwise in the context).
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.
203 --------------------------
204 -- Limited_With_Clauses --
205 --------------------------
207 -- Limited_With clauses are the mechanism chosen for Ada05 to support
208 -- mutually recursive types declared in different units. A limited_with
209 -- clause that names package P in the context of unit U makes the types
210 -- declared in the visible part of P available within U, but with the
211 -- restriction that these types can only be used as incomplete types.
212 -- The limited_with clause does not impose a semantic dependence on P,
213 -- and it is possible for two packages to have limited_with_clauses on
214 -- each other without creating an elaboration circularity.
216 -- To support this feature, the analysis of a limited_with clause must
217 -- create an abbreviated view of the package, without performing any
218 -- semantic analysis on it. This "package abstract" contains shadow
219 -- types that are in one-one correspondence with the real types in the
220 -- package, and that have the properties of incomplete types.
222 -- The implementation creates two element lists: one to chain the shadow
223 -- entities, and one to chain the corresponding type entities in the tree
224 -- of the package. Links between corresponding entities in both chains
225 -- allow the compiler to select the proper view of a given type, depending
226 -- on the context. Note that in contrast with the handling of private
227 -- types, the limited view and the non-limited view of a type are treated
228 -- as separate entities, and no entity exchange needs to take place, which
229 -- makes the implementation must simpler than could be feared.
231 ------------------------------
232 -- Analyze_Compilation_Unit --
233 ------------------------------
235 procedure Analyze_Compilation_Unit (N : Node_Id) is
236 Unit_Node : constant Node_Id := Unit (N);
237 Lib_Unit : Node_Id := Library_Unit (N);
239 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
240 Par_Spec_Name : Unit_Name_Type;
241 Unum : Unit_Number_Type;
243 procedure Check_Redundant_Withs
244 (Context_Items : List_Id;
245 Spec_Context_Items : List_Id := No_List);
246 -- Determine whether the context list of a compilation unit contains
247 -- redundant with clauses. When checking body clauses against spec
248 -- clauses, set Context_Items to the context list of the body and
249 -- Spec_Context_Items to that of the spec. Parent packages are not
250 -- examined for documentation purposes.
252 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
253 -- Generate cross-reference information for the parents of child units.
254 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
256 ---------------------------
257 -- Check_Redundant_Withs --
258 ---------------------------
260 procedure Check_Redundant_Withs
261 (Context_Items : List_Id;
262 Spec_Context_Items : List_Id := No_List)
266 procedure Process_Body_Clauses
267 (Context_List : List_Id;
269 Used : in out Boolean;
270 Used_Type_Or_Elab : in out Boolean);
271 -- Examine the context clauses of a package body, trying to match
272 -- the name entity of Clause with any list element. If the match
273 -- occurs on a use package clause, set Used to True, for a use
274 -- type clause, pragma Elaborate or pragma Elaborate_All, set
275 -- Used_Type_Or_Elab to True.
277 procedure Process_Spec_Clauses
278 (Context_List : List_Id;
280 Used : in out Boolean;
281 Withed : in out Boolean;
282 Exit_On_Self : Boolean := False);
283 -- Examine the context clauses of a package spec, trying to match
284 -- the name entity of Clause with any list element. If the match
285 -- occurs on a use package clause, set Used to True, for a with
286 -- package clause other than Clause, set Withed to True. Limited
287 -- with clauses, implicitly generated with clauses and withs
288 -- having pragmas Elaborate or Elaborate_All applied to them are
289 -- skipped. Exit_On_Self is used to control the search loop and
290 -- force an exit whenever Clause sees itself in the search.
292 --------------------------
293 -- Process_Body_Clauses --
294 --------------------------
296 procedure Process_Body_Clauses
297 (Context_List : List_Id;
299 Used : in out Boolean;
300 Used_Type_Or_Elab : in out Boolean)
302 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
308 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
309 -- In an expanded name in a use clause, if the prefix is a
310 -- renamed package, the entity is set to the original package
311 -- as a result, when checking whether the package appears in a
312 -- previous with_clause, the renaming has to be taken into
313 -- account, to prevent spurious or incorrect warnings. The
314 -- common case is the use of Text_IO.
320 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
322 return Entity (N) = P
324 (Present (Renamed_Object (P))
325 and then Entity (N) = Renamed_Object (P));
328 -- Start of processing for Process_Body_Clauses
332 Used_Type_Or_Elab := False;
334 Cont_Item := First (Context_List);
335 while Present (Cont_Item) loop
337 -- Package use clause
339 if Nkind (Cont_Item) = N_Use_Package_Clause
342 -- Search through use clauses
344 Use_Item := First (Names (Cont_Item));
345 while Present (Use_Item) and then not Used loop
347 -- Case of a direct use of the one we are looking for
349 if Entity (Use_Item) = Nam_Ent then
352 -- Handle nested case, as in "with P; use P.Q.R"
359 -- Loop through prefixes looking for match
362 while Nkind (UE) = N_Expanded_Name loop
363 if Same_Unit (Prefix (UE), Nam_Ent) then
378 elsif Nkind (Cont_Item) = N_Use_Type_Clause
379 and then not Used_Type_Or_Elab
381 Subt_Mark := First (Subtype_Marks (Cont_Item));
382 while Present (Subt_Mark)
383 and then not Used_Type_Or_Elab
385 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
386 Used_Type_Or_Elab := True;
392 -- Pragma Elaborate or Elaborate_All
394 elsif Nkind (Cont_Item) = N_Pragma
396 (Chars (Cont_Item) = Name_Elaborate
398 Chars (Cont_Item) = Name_Elaborate_All)
399 and then not Used_Type_Or_Elab
402 First (Pragma_Argument_Associations (Cont_Item));
403 while Present (Prag_Unit)
404 and then not Used_Type_Or_Elab
406 if Entity (Expression (Prag_Unit)) = Nam_Ent then
407 Used_Type_Or_Elab := True;
416 end Process_Body_Clauses;
418 --------------------------
419 -- Process_Spec_Clauses --
420 --------------------------
422 procedure Process_Spec_Clauses
423 (Context_List : List_Id;
425 Used : in out Boolean;
426 Withed : in out Boolean;
427 Exit_On_Self : Boolean := False)
429 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
437 Cont_Item := First (Context_List);
438 while Present (Cont_Item) loop
440 -- Stop the search since the context items after Cont_Item
441 -- have already been examined in a previous iteration of
442 -- the reverse loop in Check_Redundant_Withs.
445 and Cont_Item = Clause
450 -- Package use clause
452 if Nkind (Cont_Item) = N_Use_Package_Clause
455 Use_Item := First (Names (Cont_Item));
456 while Present (Use_Item) and then not Used loop
457 if Entity (Use_Item) = Nam_Ent then
464 -- Package with clause. Avoid processing self, implicitly
465 -- generated with clauses or limited with clauses. Note
466 -- that we examine with clauses having pragmas Elaborate
467 -- or Elaborate_All applied to them due to cases such as:
471 -- pragma Elaborate (Pack);
473 -- In this case, the second with clause is redundant since
474 -- the pragma applies only to the first "with Pack;".
476 elsif Nkind (Cont_Item) = N_With_Clause
477 and then not Implicit_With (Cont_Item)
478 and then not Limited_Present (Cont_Item)
479 and then Cont_Item /= Clause
480 and then Entity (Name (Cont_Item)) = Nam_Ent
487 end Process_Spec_Clauses;
489 -- Start of processing for Check_Redundant_Withs
492 Clause := Last (Context_Items);
493 while Present (Clause) loop
495 -- Avoid checking implicitly generated with clauses, limited
496 -- with clauses or withs that have pragma Elaborate or
497 -- Elaborate_All apllied.
499 if Nkind (Clause) = N_With_Clause
500 and then not Implicit_With (Clause)
501 and then not Limited_Present (Clause)
502 and then not Elaborate_Present (Clause)
504 -- Package body-to-spec check
506 if Present (Spec_Context_Items) then
508 Used_In_Body : Boolean := False;
509 Used_In_Spec : Boolean := False;
510 Used_Type_Or_Elab : Boolean := False;
511 Withed_In_Spec : Boolean := False;
515 (Context_List => Spec_Context_Items,
517 Used => Used_In_Spec,
518 Withed => Withed_In_Spec);
521 (Context_List => Context_Items,
523 Used => Used_In_Body,
524 Used_Type_Or_Elab => Used_Type_Or_Elab);
526 -- "Type Elab" refers to the presence of either a use
527 -- type clause, pragmas Elaborate or Elaborate_All.
529 -- +---------------+---------------------------+------+
530 -- | Spec | Body | Warn |
531 -- +--------+------+--------+------+-----------+------+
532 -- | Withed | Used | Withed | Used | Type Elab | |
533 -- | X | | X | | | X |
534 -- | X | | X | X | | |
535 -- | X | | X | | X | |
536 -- | X | | X | X | X | |
537 -- | X | X | X | | | X |
538 -- | X | X | X | | X | |
539 -- | X | X | X | X | | X |
540 -- | X | X | X | X | X | |
541 -- +--------+------+--------+------+-----------+------+
544 and then not Used_Type_Or_Elab)
547 and then not Used_In_Body)
551 Error_Msg_N ("?redundant with clause in body", Clause);
554 Used_In_Body := False;
555 Used_In_Spec := False;
556 Used_Type_Or_Elab := False;
557 Withed_In_Spec := False;
560 -- Standalone package spec or body check
564 Dont_Care : Boolean := False;
565 Withed : Boolean := False;
568 -- The mechanism for examining the context clauses of a
569 -- package spec can be applied to package body clauses.
572 (Context_List => Context_Items,
576 Exit_On_Self => True);
579 Error_Msg_N ("?redundant with clause", Clause);
587 end Check_Redundant_Withs;
589 --------------------------------
590 -- Generate_Parent_References --
591 --------------------------------
593 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
595 P_Name : Entity_Id := P_Id;
598 Pref := Name (Parent (Defining_Entity (N)));
600 if Nkind (Pref) = N_Expanded_Name then
602 -- Done already, if the unit has been compiled indirectly as
603 -- part of the closure of its context because of inlining.
608 while Nkind (Pref) = N_Selected_Component loop
609 Change_Selected_Component_To_Expanded_Name (Pref);
610 Set_Entity (Pref, P_Name);
611 Set_Etype (Pref, Etype (P_Name));
612 Generate_Reference (P_Name, Pref, 'r');
613 Pref := Prefix (Pref);
614 P_Name := Scope (P_Name);
617 -- The guard here on P_Name is to handle the error condition where
618 -- the parent unit is missing because the file was not found.
620 if Present (P_Name) then
621 Set_Entity (Pref, P_Name);
622 Set_Etype (Pref, Etype (P_Name));
623 Generate_Reference (P_Name, Pref, 'r');
624 Style.Check_Identifier (Pref, P_Name);
626 end Generate_Parent_References;
628 -- Start of processing for Analyze_Compilation_Unit
631 Process_Compilation_Unit_Pragmas (N);
633 -- If the unit is a subunit whose parent has not been analyzed (which
634 -- indicates that the main unit is a subunit, either the current one or
635 -- one of its descendents) then the subunit is compiled as part of the
636 -- analysis of the parent, which we proceed to do. Basically this gets
637 -- handled from the top down and we don't want to do anything at this
638 -- level (i.e. this subunit will be handled on the way down from the
639 -- parent), so at this level we immediately return. If the subunit
640 -- ends up not analyzed, it means that the parent did not contain a
641 -- stub for it, or that there errors were dectected in some ancestor.
643 if Nkind (Unit_Node) = N_Subunit
644 and then not Analyzed (Lib_Unit)
646 Semantics (Lib_Unit);
648 if not Analyzed (Proper_Body (Unit_Node)) then
649 if Serious_Errors_Detected > 0 then
650 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
652 Error_Msg_N ("missing stub for subunit", N);
659 -- Analyze context (this will call Sem recursively for with'ed units)
663 -- If the unit is a package body, the spec is already loaded and must
664 -- be analyzed first, before we analyze the body.
666 if Nkind (Unit_Node) = N_Package_Body then
668 -- If no Lib_Unit, then there was a serious previous error, so
669 -- just ignore the entire analysis effort
671 if No (Lib_Unit) then
675 Semantics (Lib_Unit);
676 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
678 -- Verify that the library unit is a package declaration
680 if Nkind (Unit (Lib_Unit)) /= N_Package_Declaration
682 Nkind (Unit (Lib_Unit)) /= N_Generic_Package_Declaration
685 ("no legal package declaration for package body", N);
688 -- Otherwise, the entity in the declaration is visible. Update
689 -- the version to reflect dependence of this body on the spec.
692 Spec_Id := Defining_Entity (Unit (Lib_Unit));
693 Set_Is_Immediately_Visible (Spec_Id, True);
694 Version_Update (N, Lib_Unit);
696 if Nkind (Defining_Unit_Name (Unit_Node))
697 = N_Defining_Program_Unit_Name
699 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
704 -- If the unit is a subprogram body, then we similarly need to analyze
705 -- its spec. However, things are a little simpler in this case, because
706 -- here, this analysis is done only for error checking and consistency
707 -- purposes, so there's nothing else to be done.
709 elsif Nkind (Unit_Node) = N_Subprogram_Body then
710 if Acts_As_Spec (N) then
712 -- If the subprogram body is a child unit, we must create a
713 -- declaration for it, in order to properly load the parent(s).
714 -- After this, the original unit does not acts as a spec, because
715 -- there is an explicit one. If this unit appears in a context
716 -- clause, then an implicit with on the parent will be added when
717 -- installing the context. If this is the main unit, there is no
718 -- Unit_Table entry for the declaration, (It has the unit number
719 -- of the main unit) and code generation is unaffected.
721 Unum := Get_Cunit_Unit_Number (N);
722 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
724 if Par_Spec_Name /= No_Name then
727 (Load_Name => Par_Spec_Name,
732 if Unum /= No_Unit then
734 -- Build subprogram declaration and attach parent unit to it
735 -- This subprogram declaration does not come from source,
736 -- Nevertheless the backend must generate debugging info for
737 -- it, and this must be indicated explicitly.
740 Loc : constant Source_Ptr := Sloc (N);
741 SCS : constant Boolean :=
742 Get_Comes_From_Source_Default;
745 Set_Comes_From_Source_Default (False);
747 Make_Compilation_Unit (Loc,
748 Context_Items => New_Copy_List (Context_Items (N)),
750 Make_Subprogram_Declaration (Sloc (N),
753 (Specification (Unit_Node))),
755 Make_Compilation_Unit_Aux (Loc));
757 Set_Library_Unit (N, Lib_Unit);
758 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
759 Semantics (Lib_Unit);
760 Set_Acts_As_Spec (N, False);
761 Set_Needs_Debug_Info (Defining_Entity (Unit (Lib_Unit)));
762 Set_Comes_From_Source_Default (SCS);
767 -- Here for subprogram with separate declaration
770 Semantics (Lib_Unit);
771 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
772 Version_Update (N, Lib_Unit);
775 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
776 N_Defining_Program_Unit_Name
778 Generate_Parent_References (
779 Specification (Unit_Node),
780 Scope (Defining_Entity (Unit (Lib_Unit))));
784 -- If it is a child unit, the parent must be elaborated first
785 -- and we update version, since we are dependent on our parent.
787 if Is_Child_Spec (Unit_Node) then
789 -- The analysis of the parent is done with style checks off
792 Save_Style_Check : constant Boolean := Style_Check;
793 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
794 Cunit_Boolean_Restrictions_Save;
797 if not GNAT_Mode then
798 Style_Check := False;
801 Semantics (Parent_Spec (Unit_Node));
802 Version_Update (N, Parent_Spec (Unit_Node));
803 Style_Check := Save_Style_Check;
804 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
808 -- With the analysis done, install the context. Note that we can't
809 -- install the context from the with clauses as we analyze them,
810 -- because each with clause must be analyzed in a clean visibility
811 -- context, so we have to wait and install them all at once.
815 if Is_Child_Spec (Unit_Node) then
817 -- Set the entities of all parents in the program_unit_name
819 Generate_Parent_References (
820 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
823 -- All components of the context: with-clauses, library unit, ancestors
824 -- if any, (and their context) are analyzed and installed. Now analyze
825 -- the unit itself, which is either a package, subprogram spec or body.
829 if Warn_On_Redundant_Constructs then
830 Check_Redundant_Withs (Context_Items (N));
832 if Nkind (Unit_Node) = N_Package_Body then
833 Check_Redundant_Withs
834 (Context_Items => Context_Items (N),
835 Spec_Context_Items => Context_Items (Lib_Unit));
839 -- The above call might have made Unit_Node an N_Subprogram_Body
840 -- from something else, so propagate any Acts_As_Spec flag.
842 if Nkind (Unit_Node) = N_Subprogram_Body
843 and then Acts_As_Spec (Unit_Node)
845 Set_Acts_As_Spec (N);
848 -- Register predefined units in Rtsfind
851 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
853 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
854 Set_RTU_Loaded (Unit_Node);
858 -- Treat compilation unit pragmas that appear after the library unit
860 if Present (Pragmas_After (Aux_Decls_Node (N))) then
862 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
864 while Present (Prag_Node) loop
871 -- Generate distribution stubs if requested and no error
874 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
876 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
877 and then not Fatal_Error (Main_Unit)
879 if Is_RCI_Pkg_Spec_Or_Body (N) then
881 -- Regular RCI package
883 Add_Stub_Constructs (N);
885 elsif (Nkind (Unit_Node) = N_Package_Declaration
886 and then Is_Shared_Passive (Defining_Entity
887 (Specification (Unit_Node))))
888 or else (Nkind (Unit_Node) = N_Package_Body
890 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
892 -- Shared passive package
894 Add_Stub_Constructs (N);
896 elsif Nkind (Unit_Node) = N_Package_Instantiation
898 Is_Remote_Call_Interface
899 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
901 -- Instantiation of a RCI generic package
903 Add_Stub_Constructs (N);
908 if Nkind (Unit_Node) = N_Package_Declaration
909 or else Nkind (Unit_Node) in N_Generic_Declaration
910 or else Nkind (Unit_Node) = N_Package_Renaming_Declaration
911 or else Nkind (Unit_Node) = N_Subprogram_Declaration
913 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
915 -- If the unit is an instantiation whose body will be elaborated
916 -- for inlining purposes, use the the proper entity of the instance.
918 elsif Nkind (Unit_Node) = N_Package_Instantiation
919 and then not Error_Posted (Unit_Node)
921 Remove_Unit_From_Visibility
922 (Defining_Entity (Instance_Spec (Unit_Node)));
924 elsif Nkind (Unit_Node) = N_Package_Body
925 or else (Nkind (Unit_Node) = N_Subprogram_Body
926 and then not Acts_As_Spec (Unit_Node))
928 -- Bodies that are not the main unit are compiled if they
929 -- are generic or contain generic or inlined units. Their
930 -- analysis brings in the context of the corresponding spec
931 -- (unit declaration) which must be removed as well, to
932 -- return the compilation environment to its proper state.
934 Remove_Context (Lib_Unit);
935 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
938 -- Last step is to deinstall the context we just installed
939 -- as well as the unit just compiled.
943 -- If this is the main unit and we are generating code, we must
944 -- check that all generic units in the context have a body if they
945 -- need it, even if they have not been instantiated. In the absence
946 -- of .ali files for generic units, we must force the load of the body,
947 -- just to produce the proper error if the body is absent. We skip this
948 -- verification if the main unit itself is generic.
950 if Get_Cunit_Unit_Number (N) = Main_Unit
951 and then Operating_Mode = Generate_Code
952 and then Expander_Active
954 -- Check whether the source for the body of the unit must be
955 -- included in a standalone library.
957 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
959 -- Indicate that the main unit is now analyzed, to catch possible
960 -- circularities between it and generic bodies. Remove main unit
961 -- from visibility. This might seem superfluous, but the main unit
962 -- must not be visible in the generic body expansions that follow.
964 Set_Analyzed (N, True);
965 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
970 Un : Unit_Number_Type;
972 Save_Style_Check : constant Boolean := Style_Check;
973 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
974 Cunit_Boolean_Restrictions_Save;
977 Item := First (Context_Items (N));
978 while Present (Item) loop
980 -- Check for explicit with clause
982 if Nkind (Item) = N_With_Clause
983 and then not Implicit_With (Item)
985 -- Ada 2005 (AI-50217): Ignore limited-withed units
987 and then not Limited_Present (Item)
989 Nam := Entity (Name (Item));
991 if (Is_Generic_Subprogram (Nam)
992 and then not Is_Intrinsic_Subprogram (Nam))
993 or else (Ekind (Nam) = E_Generic_Package
994 and then Unit_Requires_Body (Nam))
996 Style_Check := False;
998 if Present (Renamed_Object (Nam)) then
1001 (Load_Name => Get_Body_Name
1003 (Unit_Declaration_Node
1004 (Renamed_Object (Nam)))),
1012 (Load_Name => Get_Body_Name
1013 (Get_Unit_Name (Item)),
1020 if Un = No_Unit then
1022 ("body of generic unit& not found", Item, Nam);
1025 elsif not Analyzed (Cunit (Un))
1026 and then Un /= Main_Unit
1027 and then not Fatal_Error (Un)
1029 Style_Check := False;
1030 Semantics (Cunit (Un));
1038 Style_Check := Save_Style_Check;
1039 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1043 -- Deal with creating elaboration Boolean if needed. We create an
1044 -- elaboration boolean only for units that come from source since
1045 -- units manufactured by the compiler never need elab checks.
1047 if Comes_From_Source (N)
1049 (Nkind (Unit (N)) = N_Package_Declaration or else
1050 Nkind (Unit (N)) = N_Generic_Package_Declaration or else
1051 Nkind (Unit (N)) = N_Subprogram_Declaration or else
1052 Nkind (Unit (N)) = N_Generic_Subprogram_Declaration)
1055 Loc : constant Source_Ptr := Sloc (N);
1056 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1059 Spec_Id := Defining_Entity (Unit (N));
1060 Generate_Definition (Spec_Id);
1062 -- See if an elaboration entity is required for possible
1063 -- access before elaboration checking. Note that we must
1064 -- allow for this even if -gnatE is not set, since a client
1065 -- may be compiled in -gnatE mode and reference the entity.
1067 -- Case of units which do not require elaboration checks
1070 -- Pure units do not need checks
1074 -- Preelaborated units do not need checks
1076 or else Is_Preelaborated (Spec_Id)
1078 -- No checks needed if pagma Elaborate_Body present
1080 or else Has_Pragma_Elaborate_Body (Spec_Id)
1082 -- No checks needed if unit does not require a body
1084 or else not Unit_Requires_Body (Spec_Id)
1086 -- No checks needed for predefined files
1088 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1090 -- No checks required if no separate spec
1092 or else Acts_As_Spec (N)
1094 -- This is a case where we only need the entity for
1095 -- checking to prevent multiple elaboration checks.
1097 Set_Elaboration_Entity_Required (Spec_Id, False);
1099 -- Case of elaboration entity is required for access before
1100 -- elaboration checking (so certainly we must build it!)
1103 Set_Elaboration_Entity_Required (Spec_Id, True);
1106 Build_Elaboration_Entity (N, Spec_Id);
1110 -- Freeze the compilation unit entity. This for sure is needed because
1111 -- of some warnings that can be output (see Freeze_Subprogram), but may
1112 -- in general be required. If freezing actions result, place them in the
1113 -- compilation unit actions list, and analyze them.
1116 Loc : constant Source_Ptr := Sloc (N);
1117 L : constant List_Id :=
1118 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), Loc);
1120 while Is_Non_Empty_List (L) loop
1121 Insert_Library_Level_Action (Remove_Head (L));
1127 if Nkind (Unit_Node) = N_Package_Declaration
1128 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1129 and then Expander_Active
1132 Save_Style_Check : constant Boolean := Style_Check;
1133 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1134 Options : Style_Check_Options;
1137 Save_Style_Check_Options (Options);
1138 Reset_Style_Check_Options;
1139 Opt.Warning_Mode := Suppress;
1140 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1142 Reset_Style_Check_Options;
1143 Set_Style_Check_Options (Options);
1144 Style_Check := Save_Style_Check;
1145 Warning_Mode := Save_Warning;
1149 -- If we are generating obsolescent warnings, then here is where we
1150 -- generate them for the with'ed items. The reason for this special
1151 -- processing is that the normal mechanism of generating the warnings
1152 -- for referenced entities does not work for context clause references.
1153 -- That's because when we first analyze the context, it is too early to
1154 -- know if the with'ing unit is itself obsolescent (which suppresses
1157 if not GNAT_Mode and then Warn_On_Obsolescent_Feature then
1159 -- Push current compilation unit as scope, so that the test for
1160 -- being within an obsolescent unit will work correctly.
1162 New_Scope (Defining_Entity (Unit (N)));
1164 -- Loop through context items to deal with with clauses
1172 Item := First (Context_Items (N));
1173 while Present (Item) loop
1174 if Nkind (Item) = N_With_Clause then
1176 Ent := Entity (Nam);
1178 if Is_Obsolescent (Ent) then
1179 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1187 -- Remove temporary install of current unit as scope
1191 end Analyze_Compilation_Unit;
1193 ---------------------
1194 -- Analyze_Context --
1195 ---------------------
1197 procedure Analyze_Context (N : Node_Id) is
1198 Ukind : constant Node_Kind := Nkind (Unit (N));
1202 -- First process all configuration pragmas at the start of the context
1203 -- items. Strictly these are not part of the context clause, but that
1204 -- is where the parser puts them. In any case for sure we must analyze
1205 -- these before analyzing the actual context items, since they can have
1206 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1207 -- be with'ed as a result of changing categorizations in Ada 2005).
1209 Item := First (Context_Items (N));
1210 while Present (Item)
1211 and then Nkind (Item) = N_Pragma
1212 and then Chars (Item) in Configuration_Pragma_Names
1218 -- Loop through actual context items. This is done in two passes:
1220 -- a) The first pass analyzes non-limited with-clauses and also any
1221 -- configuration pragmas (we need to get the latter analyzed right
1222 -- away, since they can affect processing of subsequent items.
1224 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1226 while Present (Item) loop
1228 -- For with clause, analyze the with clause, and then update
1229 -- the version, since we are dependent on a unit that we with.
1231 if Nkind (Item) = N_With_Clause
1232 and then not Limited_Present (Item)
1234 -- Skip analyzing with clause if no unit, nothing to do (this
1235 -- happens for a with that references a non-existant unit)
1237 if Present (Library_Unit (Item)) then
1241 if not Implicit_With (Item) then
1242 Version_Update (N, Library_Unit (Item));
1245 -- Skip pragmas. Configuration pragmas at the start were handled in
1246 -- the loop above, and remaining pragmas are not processed until we
1247 -- actually install the context (see Install_Context). We delay the
1248 -- analysis of these pragmas to make sure that we have installed all
1249 -- the implicit with's on parent units.
1251 -- Skip use clauses at this stage, since we don't want to do any
1252 -- installing of potentially use visible entities until we we
1253 -- actually install the complete context (in Install_Context).
1254 -- Otherwise things can get installed in the wrong context.
1263 -- Second pass: examine all limited_with clauses. All other context
1264 -- items are ignored in this pass.
1266 Item := First (Context_Items (N));
1267 while Present (Item) loop
1268 if Nkind (Item) = N_With_Clause
1269 and then Limited_Present (Item)
1271 -- No need to check errors on implicitly generated limited-with
1274 if not Implicit_With (Item) then
1276 -- Check compilation unit containing the limited-with clause
1278 if Ukind /= N_Package_Declaration
1279 and then Ukind /= N_Subprogram_Declaration
1280 and then Ukind /= N_Package_Renaming_Declaration
1281 and then Ukind /= N_Subprogram_Renaming_Declaration
1282 and then Ukind not in N_Generic_Declaration
1283 and then Ukind not in N_Generic_Renaming_Declaration
1284 and then Ukind not in N_Generic_Instantiation
1286 Error_Msg_N ("limited with_clause not allowed here", Item);
1288 -- Check wrong use of a limited with clause applied to the
1289 -- compilation unit containing the limited-with clause.
1291 -- limited with P.Q;
1292 -- package P.Q is ...
1294 elsif Unit (Library_Unit (Item)) = Unit (N) then
1295 Error_Msg_N ("wrong use of limited-with clause", Item);
1297 -- Check wrong use of limited-with clause applied to some
1298 -- immediate ancestor.
1300 elsif Is_Child_Spec (Unit (N)) then
1302 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1306 P := Parent_Spec (Unit (N));
1308 if Unit (P) = Lib_U then
1309 Error_Msg_N ("limited with_clause of immediate "
1310 & "ancestor not allowed", Item);
1314 exit when not Is_Child_Spec (Unit (P));
1315 P := Parent_Spec (Unit (P));
1320 -- Check if the limited-withed unit is already visible through
1321 -- some context clause of the current compilation unit or some
1322 -- ancestor of the current compilation unit.
1325 Lim_Unit_Name : constant Node_Id := Name (Item);
1326 Comp_Unit : Node_Id;
1328 Unit_Name : Node_Id;
1333 It := First (Context_Items (Comp_Unit));
1334 while Present (It) loop
1336 and then Nkind (It) = N_With_Clause
1337 and then not Limited_Present (It)
1339 (Nkind (Unit (Library_Unit (It)))
1340 = N_Package_Declaration
1342 Nkind (Unit (Library_Unit (It)))
1343 = N_Package_Renaming_Declaration)
1345 if Nkind (Unit (Library_Unit (It)))
1346 = N_Package_Declaration
1348 Unit_Name := Name (It);
1350 Unit_Name := Name (Unit (Library_Unit (It)));
1353 -- Check if the named package (or some ancestor)
1354 -- leaves visible the full-view of the unit given
1355 -- in the limited-with clause
1358 if Designate_Same_Unit (Lim_Unit_Name,
1361 Error_Msg_Sloc := Sloc (It);
1363 ("unlimited view visible through the"
1364 & " context clause found #",
1367 ("\simultaneous visibility of the limited"
1368 & " and unlimited views not allowed"
1372 elsif Nkind (Unit_Name) = N_Identifier then
1376 Unit_Name := Prefix (Unit_Name);
1383 exit when not Is_Child_Spec (Unit (Comp_Unit));
1385 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1390 -- Skip analyzing with clause if no unit, see above
1392 if Present (Library_Unit (Item)) then
1396 -- A limited_with does not impose an elaboration order, but
1397 -- there is a semantic dependency for recompilation purposes.
1399 if not Implicit_With (Item) then
1400 Version_Update (N, Library_Unit (Item));
1403 -- Pragmas and use clauses and with clauses other than limited
1404 -- with's are ignored in this pass through the context items.
1412 end Analyze_Context;
1414 -------------------------------
1415 -- Analyze_Package_Body_Stub --
1416 -------------------------------
1418 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1419 Id : constant Entity_Id := Defining_Identifier (N);
1423 -- The package declaration must be in the current declarative part
1425 Check_Stub_Level (N);
1426 Nam := Current_Entity_In_Scope (Id);
1428 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1429 Error_Msg_N ("missing specification for package stub", N);
1431 elsif Has_Completion (Nam)
1432 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1434 Error_Msg_N ("duplicate or redundant stub for package", N);
1437 -- Indicate that the body of the package exists. If we are doing
1438 -- only semantic analysis, the stub stands for the body. If we are
1439 -- generating code, the existence of the body will be confirmed
1440 -- when we load the proper body.
1442 Set_Has_Completion (Nam);
1443 Set_Scope (Defining_Entity (N), Current_Scope);
1444 Generate_Reference (Nam, Id, 'b');
1445 Analyze_Proper_Body (N, Nam);
1447 end Analyze_Package_Body_Stub;
1449 -------------------------
1450 -- Analyze_Proper_Body --
1451 -------------------------
1453 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1454 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1455 Unum : Unit_Number_Type;
1457 procedure Optional_Subunit;
1458 -- This procedure is called when the main unit is a stub, or when we
1459 -- are not generating code. In such a case, we analyze the subunit if
1460 -- present, which is user-friendly and in fact required for ASIS, but
1461 -- we don't complain if the subunit is missing.
1463 ----------------------
1464 -- Optional_Subunit --
1465 ----------------------
1467 procedure Optional_Subunit is
1468 Comp_Unit : Node_Id;
1471 -- Try to load subunit, but ignore any errors that occur during
1472 -- the loading of the subunit, by using the special feature in
1473 -- Errout to ignore all errors. Note that Fatal_Error will still
1474 -- be set, so we will be able to check for this case below.
1476 if not ASIS_Mode then
1477 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1482 (Load_Name => Subunit_Name,
1487 if not ASIS_Mode then
1488 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1491 -- All done if we successfully loaded the subunit
1494 and then (not Fatal_Error (Unum) or else Try_Semantics)
1496 Comp_Unit := Cunit (Unum);
1498 -- If the file was empty or seriously mangled, the unit
1499 -- itself may be missing.
1501 if No (Unit (Comp_Unit)) then
1503 ("subunit does not contain expected proper body", N);
1505 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1507 ("expected SEPARATE subunit, found child unit",
1508 Cunit_Entity (Unum));
1510 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1511 Analyze_Subunit (Comp_Unit);
1512 Set_Library_Unit (N, Comp_Unit);
1515 elsif Unum = No_Unit
1516 and then Present (Nam)
1518 if Is_Protected_Type (Nam) then
1519 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1521 Set_Corresponding_Body (
1522 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1525 end Optional_Subunit;
1527 -- Start of processing for Analyze_Proper_Body
1530 -- If the subunit is already loaded, it means that the main unit
1531 -- is a subunit, and that the current unit is one of its parents
1532 -- which was being analyzed to provide the needed context for the
1533 -- analysis of the subunit. In this case we analyze the subunit and
1534 -- continue with the parent, without looking a subsequent subunits.
1536 if Is_Loaded (Subunit_Name) then
1538 -- If the proper body is already linked to the stub node,
1539 -- the stub is in a generic unit and just needs analyzing.
1541 if Present (Library_Unit (N)) then
1542 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1543 Analyze_Subunit (Library_Unit (N));
1545 -- Otherwise we must load the subunit and link to it
1548 -- Load the subunit, this must work, since we originally
1549 -- loaded the subunit earlier on. So this will not really
1550 -- load it, just give access to it.
1554 (Load_Name => Subunit_Name,
1559 -- And analyze the subunit in the parent context (note that we
1560 -- do not call Semantics, since that would remove the parent
1561 -- context). Because of this, we have to manually reset the
1562 -- compiler state to Analyzing since it got destroyed by Load.
1564 if Unum /= No_Unit then
1565 Compiler_State := Analyzing;
1567 -- Check that the proper body is a subunit and not a child
1568 -- unit. If the unit was previously loaded, the error will
1569 -- have been emitted when copying the generic node, so we
1570 -- just return to avoid cascaded errors.
1572 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1576 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1577 Analyze_Subunit (Cunit (Unum));
1578 Set_Library_Unit (N, Cunit (Unum));
1582 -- If the main unit is a subunit, then we are just performing semantic
1583 -- analysis on that subunit, and any other subunits of any parent unit
1584 -- should be ignored, except that if we are building trees for ASIS
1585 -- usage we want to annotate the stub properly.
1587 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1588 and then Subunit_Name /= Unit_Name (Main_Unit)
1594 -- But before we return, set the flag for unloaded subunits. This
1595 -- will suppress junk warnings of variables in the same declarative
1596 -- part (or a higher level one) that are in danger of looking unused
1597 -- when in fact there might be a declaration in the subunit that we
1598 -- do not intend to load.
1600 Unloaded_Subunits := True;
1603 -- If the subunit is not already loaded, and we are generating code,
1604 -- then this is the case where compilation started from the parent,
1605 -- and we are generating code for an entire subunit tree. In that
1606 -- case we definitely need to load the subunit.
1608 -- In order to continue the analysis with the rest of the parent,
1609 -- and other subunits, we load the unit without requiring its
1610 -- presence, and emit a warning if not found, rather than terminating
1611 -- the compilation abruptly, as for other missing file problems.
1613 elsif Original_Operating_Mode = Generate_Code then
1615 -- If the proper body is already linked to the stub node,
1616 -- the stub is in a generic unit and just needs analyzing.
1618 -- We update the version. Although we are not technically
1619 -- semantically dependent on the subunit, given our approach
1620 -- of macro substitution of subunits, it makes sense to
1621 -- include it in the version identification.
1623 if Present (Library_Unit (N)) then
1624 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1625 Analyze_Subunit (Library_Unit (N));
1626 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1628 -- Otherwise we must load the subunit and link to it
1633 (Load_Name => Subunit_Name,
1638 if Original_Operating_Mode = Generate_Code
1639 and then Unum = No_Unit
1641 Error_Msg_Name_1 := Subunit_Name;
1643 Get_File_Name (Subunit_Name, Subunit => True);
1645 ("subunit% in file{ not found?", N);
1646 Subunits_Missing := True;
1649 -- Load_Unit may reset Compiler_State, since it may have been
1650 -- necessary to parse an additional units, so we make sure
1651 -- that we reset it to the Analyzing state.
1653 Compiler_State := Analyzing;
1655 if Unum /= No_Unit then
1656 if Debug_Flag_L then
1657 Write_Str ("*** Loaded subunit from stub. Analyze");
1662 Comp_Unit : constant Node_Id := Cunit (Unum);
1665 -- Check for child unit instead of subunit
1667 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1669 ("expected SEPARATE subunit, found child unit",
1670 Cunit_Entity (Unum));
1672 -- OK, we have a subunit
1675 -- Set corresponding stub (even if errors)
1677 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1679 -- Analyze the unit if semantics active
1681 if not Fatal_Error (Unum) or else Try_Semantics then
1682 Analyze_Subunit (Comp_Unit);
1685 -- Set the library unit pointer in any case
1687 Set_Library_Unit (N, Comp_Unit);
1689 -- We update the version. Although we are not technically
1690 -- semantically dependent on the subunit, given our
1691 -- approach of macro substitution of subunits, it makes
1692 -- sense to include it in the version identification.
1694 Version_Update (Cunit (Main_Unit), Comp_Unit);
1700 -- The remaining case is when the subunit is not already loaded and
1701 -- we are not generating code. In this case we are just performing
1702 -- semantic analysis on the parent, and we are not interested in
1703 -- the subunit. For subprograms, analyze the stub as a body. For
1704 -- other entities the stub has already been marked as completed.
1710 end Analyze_Proper_Body;
1712 ----------------------------------
1713 -- Analyze_Protected_Body_Stub --
1714 ----------------------------------
1716 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1717 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1720 Check_Stub_Level (N);
1722 -- First occurence of name may have been as an incomplete type
1724 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1725 Nam := Full_View (Nam);
1729 or else not Is_Protected_Type (Etype (Nam))
1731 Error_Msg_N ("missing specification for Protected body", N);
1733 Set_Scope (Defining_Entity (N), Current_Scope);
1734 Set_Has_Completion (Etype (Nam));
1735 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1736 Analyze_Proper_Body (N, Etype (Nam));
1738 end Analyze_Protected_Body_Stub;
1740 ----------------------------------
1741 -- Analyze_Subprogram_Body_Stub --
1742 ----------------------------------
1744 -- A subprogram body stub can appear with or without a previous
1745 -- specification. If there is one, the analysis of the body will
1746 -- find it and verify conformance. The formals appearing in the
1747 -- specification of the stub play no role, except for requiring an
1748 -- additional conformance check. If there is no previous subprogram
1749 -- declaration, the stub acts as a spec, and provides the defining
1750 -- entity for the subprogram.
1752 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1756 Check_Stub_Level (N);
1758 -- Verify that the identifier for the stub is unique within this
1759 -- declarative part.
1761 if Nkind (Parent (N)) = N_Block_Statement
1762 or else Nkind (Parent (N)) = N_Package_Body
1763 or else Nkind (Parent (N)) = N_Subprogram_Body
1765 Decl := First (Declarations (Parent (N)));
1766 while Present (Decl)
1769 if Nkind (Decl) = N_Subprogram_Body_Stub
1770 and then (Chars (Defining_Unit_Name (Specification (Decl)))
1771 = Chars (Defining_Unit_Name (Specification (N))))
1773 Error_Msg_N ("identifier for stub is not unique", N);
1780 -- Treat stub as a body, which checks conformance if there is a previous
1781 -- declaration, or else introduces entity and its signature.
1783 Analyze_Subprogram_Body (N);
1784 Analyze_Proper_Body (N, Empty);
1785 end Analyze_Subprogram_Body_Stub;
1787 ---------------------
1788 -- Analyze_Subunit --
1789 ---------------------
1791 -- A subunit is compiled either by itself (for semantic checking)
1792 -- or as part of compiling the parent (for code generation). In
1793 -- either case, by the time we actually process the subunit, the
1794 -- parent has already been installed and analyzed. The node N is
1795 -- a compilation unit, whose context needs to be treated here,
1796 -- because we come directly here from the parent without calling
1797 -- Analyze_Compilation_Unit.
1799 -- The compilation context includes the explicit context of the
1800 -- subunit, and the context of the parent, together with the parent
1801 -- itself. In order to compile the current context, we remove the
1802 -- one inherited from the parent, in order to have a clean visibility
1803 -- table. We restore the parent context before analyzing the proper
1804 -- body itself. On exit, we remove only the explicit context of the
1807 procedure Analyze_Subunit (N : Node_Id) is
1808 Lib_Unit : constant Node_Id := Library_Unit (N);
1809 Par_Unit : constant Entity_Id := Current_Scope;
1811 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1812 Num_Scopes : Int := 0;
1813 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1814 Enclosing_Child : Entity_Id := Empty;
1815 Svg : constant Suppress_Array := Scope_Suppress;
1817 procedure Analyze_Subunit_Context;
1818 -- Capture names in use clauses of the subunit. This must be done
1819 -- before re-installing parent declarations, because items in the
1820 -- context must not be hidden by declarations local to the parent.
1822 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1823 -- Recursive procedure to restore scope of all ancestors of subunit,
1824 -- from outermost in. If parent is not a subunit, the call to install
1825 -- context installs context of spec and (if parent is a child unit)
1826 -- the context of its parents as well. It is confusing that parents
1827 -- should be treated differently in both cases, but the semantics are
1828 -- just not identical.
1830 procedure Re_Install_Use_Clauses;
1831 -- As part of the removal of the parent scope, the use clauses are
1832 -- removed, to be reinstalled when the context of the subunit has
1833 -- been analyzed. Use clauses may also have been affected by the
1834 -- analysis of the context of the subunit, so they have to be applied
1835 -- again, to insure that the compilation environment of the rest of
1836 -- the parent unit is identical.
1838 procedure Remove_Scope;
1839 -- Remove current scope from scope stack, and preserve the list
1840 -- of use clauses in it, to be reinstalled after context is analyzed.
1842 -----------------------------
1843 -- Analyze_Subunit_Context --
1844 -----------------------------
1846 procedure Analyze_Subunit_Context is
1849 Unit_Name : Entity_Id;
1852 Analyze_Context (N);
1854 -- Make withed units immediately visible. If child unit, make the
1855 -- ultimate parent immediately visible.
1857 Item := First (Context_Items (N));
1858 while Present (Item) loop
1859 if Nkind (Item) = N_With_Clause then
1861 -- Protect frontend against previous errors in context clauses
1863 if Nkind (Name (Item)) /= N_Selected_Component then
1864 Unit_Name := Entity (Name (Item));
1865 while Is_Child_Unit (Unit_Name) loop
1866 Set_Is_Visible_Child_Unit (Unit_Name);
1867 Unit_Name := Scope (Unit_Name);
1870 if not Is_Immediately_Visible (Unit_Name) then
1871 Set_Is_Immediately_Visible (Unit_Name);
1872 Set_Context_Installed (Item);
1876 elsif Nkind (Item) = N_Use_Package_Clause then
1877 Nam := First (Names (Item));
1878 while Present (Nam) loop
1883 elsif Nkind (Item) = N_Use_Type_Clause then
1884 Nam := First (Subtype_Marks (Item));
1885 while Present (Nam) loop
1894 -- Reset visibility of withed units. They will be made visible
1895 -- again when we install the subunit context.
1897 Item := First (Context_Items (N));
1898 while Present (Item) loop
1899 if Nkind (Item) = N_With_Clause
1901 -- Protect frontend against previous errors in context clauses
1903 and then Nkind (Name (Item)) /= N_Selected_Component
1905 Unit_Name := Entity (Name (Item));
1906 while Is_Child_Unit (Unit_Name) loop
1907 Set_Is_Visible_Child_Unit (Unit_Name, False);
1908 Unit_Name := Scope (Unit_Name);
1911 if Context_Installed (Item) then
1912 Set_Is_Immediately_Visible (Unit_Name, False);
1913 Set_Context_Installed (Item, False);
1919 end Analyze_Subunit_Context;
1921 ------------------------
1922 -- Re_Install_Parents --
1923 ------------------------
1925 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
1929 if Nkind (Unit (L)) = N_Subunit then
1930 Re_Install_Parents (Library_Unit (L), Scope (Scop));
1933 Install_Context (L);
1935 -- If the subunit occurs within a child unit, we must restore the
1936 -- immediate visibility of any siblings that may occur in context.
1938 if Present (Enclosing_Child) then
1939 Install_Siblings (Enclosing_Child, L);
1944 if Scop /= Par_Unit then
1945 Set_Is_Immediately_Visible (Scop);
1948 -- Make entities in scope visible again. For child units, restore
1949 -- visibility only if they are actually in context.
1951 E := First_Entity (Current_Scope);
1952 while Present (E) loop
1953 if not Is_Child_Unit (E)
1954 or else Is_Visible_Child_Unit (E)
1956 Set_Is_Immediately_Visible (E);
1962 -- A subunit appears within a body, and for a nested subunits
1963 -- all the parents are bodies. Restore full visibility of their
1964 -- private entities.
1966 if Ekind (Scop) = E_Package then
1967 Set_In_Package_Body (Scop);
1968 Install_Private_Declarations (Scop);
1970 end Re_Install_Parents;
1972 ----------------------------
1973 -- Re_Install_Use_Clauses --
1974 ----------------------------
1976 procedure Re_Install_Use_Clauses is
1979 for J in reverse 1 .. Num_Scopes loop
1980 U := Use_Clauses (J);
1981 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
1982 Install_Use_Clauses (U, Force_Installation => True);
1984 end Re_Install_Use_Clauses;
1990 procedure Remove_Scope is
1994 Num_Scopes := Num_Scopes + 1;
1995 Use_Clauses (Num_Scopes) :=
1996 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
1998 E := First_Entity (Current_Scope);
1999 while Present (E) loop
2000 Set_Is_Immediately_Visible (E, False);
2004 if Is_Child_Unit (Current_Scope) then
2005 Enclosing_Child := Current_Scope;
2011 -- Start of processing for Analyze_Subunit
2014 if not Is_Empty_List (Context_Items (N)) then
2016 -- Save current use clauses
2019 Remove_Context (Lib_Unit);
2021 -- Now remove parents and their context, including enclosing
2022 -- subunits and the outer parent body which is not a subunit.
2024 if Present (Lib_Spec) then
2025 Remove_Context (Lib_Spec);
2027 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2028 Lib_Spec := Library_Unit (Lib_Spec);
2030 Remove_Context (Lib_Spec);
2033 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2037 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2038 Remove_Context (Library_Unit (Lib_Spec));
2042 Set_Is_Immediately_Visible (Par_Unit, False);
2044 Analyze_Subunit_Context;
2046 Re_Install_Parents (Lib_Unit, Par_Unit);
2047 Set_Is_Immediately_Visible (Par_Unit);
2049 -- If the context includes a child unit of the parent of the
2050 -- subunit, the parent will have been removed from visibility,
2051 -- after compiling that cousin in the context. The visibility
2052 -- of the parent must be restored now. This also applies if the
2053 -- context includes another subunit of the same parent which in
2054 -- turn includes a child unit in its context.
2056 if Ekind (Par_Unit) = E_Package then
2057 if not Is_Immediately_Visible (Par_Unit)
2058 or else (Present (First_Entity (Par_Unit))
2059 and then not Is_Immediately_Visible
2060 (First_Entity (Par_Unit)))
2062 Set_Is_Immediately_Visible (Par_Unit);
2063 Install_Visible_Declarations (Par_Unit);
2064 Install_Private_Declarations (Par_Unit);
2068 Re_Install_Use_Clauses;
2069 Install_Context (N);
2071 -- Restore state of suppress flags for current body
2073 Scope_Suppress := Svg;
2075 -- If the subunit is within a child unit, then siblings of any
2076 -- parent unit that appear in the context clause of the subunit
2077 -- must also be made immediately visible.
2079 if Present (Enclosing_Child) then
2080 Install_Siblings (Enclosing_Child, N);
2085 Analyze (Proper_Body (Unit (N)));
2088 -- The subunit may contain a with_clause on a sibling of some
2089 -- ancestor. Removing the context will remove from visibility those
2090 -- ancestor child units, which must be restored to the visibility
2091 -- they have in the enclosing body.
2093 if Present (Enclosing_Child) then
2099 and then Is_Child_Unit (C)
2101 Set_Is_Immediately_Visible (C);
2102 Set_Is_Visible_Child_Unit (C);
2107 end Analyze_Subunit;
2109 ----------------------------
2110 -- Analyze_Task_Body_Stub --
2111 ----------------------------
2113 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2114 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2115 Loc : constant Source_Ptr := Sloc (N);
2118 Check_Stub_Level (N);
2120 -- First occurence of name may have been as an incomplete type
2122 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2123 Nam := Full_View (Nam);
2127 or else not Is_Task_Type (Etype (Nam))
2129 Error_Msg_N ("missing specification for task body", N);
2131 Set_Scope (Defining_Entity (N), Current_Scope);
2132 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2133 Set_Has_Completion (Etype (Nam));
2134 Analyze_Proper_Body (N, Etype (Nam));
2136 -- Set elaboration flag to indicate that entity is callable.
2137 -- This cannot be done in the expansion of the body itself,
2138 -- because the proper body is not in a declarative part. This
2139 -- is only done if expansion is active, because the context
2140 -- may be generic and the flag not defined yet.
2142 if Expander_Active then
2144 Make_Assignment_Statement (Loc,
2146 Make_Identifier (Loc,
2147 New_External_Name (Chars (Etype (Nam)), 'E')),
2148 Expression => New_Reference_To (Standard_True, Loc)));
2152 end Analyze_Task_Body_Stub;
2154 -------------------------
2155 -- Analyze_With_Clause --
2156 -------------------------
2158 -- Analyze the declaration of a unit in a with clause. At end,
2159 -- label the with clause with the defining entity for the unit.
2161 procedure Analyze_With_Clause (N : Node_Id) is
2163 -- Retrieve the original kind of the unit node, before analysis.
2164 -- If it is a subprogram instantiation, its analysis below will
2165 -- rewrite as the declaration of the wrapper package. If the same
2166 -- instantiation appears indirectly elsewhere in the context, it
2167 -- will have been analyzed already.
2169 Unit_Kind : constant Node_Kind :=
2170 Nkind (Original_Node (Unit (Library_Unit (N))));
2173 Par_Name : Entity_Id;
2178 -- Set True if the unit currently being compiled is an internal unit
2180 Save_Style_Check : constant Boolean := Opt.Style_Check;
2181 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
2182 Cunit_Boolean_Restrictions_Save;
2185 if Limited_Present (N) then
2187 -- Ada 2005 (AI-50217): Build visibility structures but do not
2190 Build_Limited_Views (N);
2194 -- We reset ordinary style checking during the analysis of a with'ed
2195 -- unit, but we do NOT reset GNAT special analysis mode (the latter
2196 -- definitely *does* apply to with'ed units).
2198 if not GNAT_Mode then
2199 Style_Check := False;
2202 -- If the library unit is a predefined unit, and we are in high
2203 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2204 -- for the analysis of the with'ed unit. This mode does not prevent
2205 -- explicit with'ing of run-time units.
2207 if Configurable_Run_Time_Mode
2209 Is_Predefined_File_Name
2210 (Unit_File_Name (Get_Source_Unit (Unit (Library_Unit (N)))))
2212 Configurable_Run_Time_Mode := False;
2213 Semantics (Library_Unit (N));
2214 Configurable_Run_Time_Mode := True;
2217 Semantics (Library_Unit (N));
2220 U := Unit (Library_Unit (N));
2221 Check_Restriction_No_Dependence (Name (N), N);
2222 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2224 -- Following checks are skipped for dummy packages (those supplied
2225 -- for with's where no matching file could be found). Such packages
2226 -- are identified by the Sloc value being set to No_Location
2228 if Sloc (U) /= No_Location then
2230 -- Check restrictions, except that we skip the check if this
2231 -- is an internal unit unless we are compiling the internal
2232 -- unit as the main unit. We also skip this for dummy packages.
2234 if not Intunit or else Current_Sem_Unit = Main_Unit then
2235 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2238 -- Check for inappropriate with of internal implementation unit
2239 -- if we are currently compiling the main unit and the main unit
2240 -- is itself not an internal unit. We do not issue this message
2241 -- for implicit with's generated by the compiler itself.
2243 if Implementation_Unit_Warnings
2244 and then Current_Sem_Unit = Main_Unit
2245 and then not Intunit
2246 and then not Implicit_With (N)
2247 and then not GNAT_Mode
2250 U_Kind : constant Kind_Of_Unit :=
2251 Get_Kind_Of_Unit (Get_Source_Unit (U));
2254 if U_Kind = Implementation_Unit then
2255 Error_Msg_N ("& is an internal 'G'N'A'T unit?", Name (N));
2257 ("\use of this unit is non-portable " &
2258 "and version-dependent?",
2261 elsif U_Kind = Ada_05_Unit
2262 and then Ada_Version < Ada_05
2263 and then Warn_On_Ada_2005_Compatibility
2265 Error_Msg_N ("& is an Ada 2005 unit?", Name (N));
2271 -- Semantic analysis of a generic unit is performed on a copy of
2272 -- the original tree. Retrieve the entity on which semantic info
2273 -- actually appears.
2275 if Unit_Kind in N_Generic_Declaration then
2276 E_Name := Defining_Entity (U);
2278 -- Note: in the following test, Unit_Kind is the original Nkind, but
2279 -- in the case of an instantiation, semantic analysis above will
2280 -- have replaced the unit by its instantiated version. If the instance
2281 -- body has been generated, the instance now denotes the body entity.
2282 -- For visibility purposes we need the entity of its spec.
2284 elsif (Unit_Kind = N_Package_Instantiation
2285 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2286 N_Package_Instantiation)
2287 and then Nkind (U) = N_Package_Body
2289 E_Name := Corresponding_Spec (U);
2291 elsif Unit_Kind = N_Package_Instantiation
2292 and then Nkind (U) = N_Package_Instantiation
2294 -- If the instance has not been rewritten as a package declaration,
2295 -- then it appeared already in a previous with clause. Retrieve
2296 -- the entity from the previous instance.
2298 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2300 elsif Unit_Kind in N_Subprogram_Instantiation then
2302 -- Instantiation node is replaced with a wrapper package.
2303 -- Retrieve the visible subprogram created by the instance from
2304 -- the corresponding attribute of the wrapper.
2306 E_Name := Related_Instance (Defining_Entity (U));
2308 elsif Unit_Kind = N_Package_Renaming_Declaration
2309 or else Unit_Kind in N_Generic_Renaming_Declaration
2311 E_Name := Defining_Entity (U);
2313 elsif Unit_Kind = N_Subprogram_Body
2314 and then Nkind (Name (N)) = N_Selected_Component
2315 and then not Acts_As_Spec (Library_Unit (N))
2317 -- For a child unit that has no spec, one has been created and
2318 -- analyzed. The entity required is that of the spec.
2320 E_Name := Corresponding_Spec (U);
2323 E_Name := Defining_Entity (U);
2326 if Nkind (Name (N)) = N_Selected_Component then
2328 -- Child unit in a with clause
2330 Change_Selected_Component_To_Expanded_Name (Name (N));
2333 -- Restore style checks and restrictions
2335 Style_Check := Save_Style_Check;
2336 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
2338 -- Record the reference, but do NOT set the unit as referenced, we want
2339 -- to consider the unit as unreferenced if this is the only reference
2342 Set_Entity_With_Style_Check (Name (N), E_Name);
2343 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2345 if Is_Child_Unit (E_Name) then
2346 Pref := Prefix (Name (N));
2347 Par_Name := Scope (E_Name);
2348 while Nkind (Pref) = N_Selected_Component loop
2349 Change_Selected_Component_To_Expanded_Name (Pref);
2350 Set_Entity_With_Style_Check (Pref, Par_Name);
2352 Generate_Reference (Par_Name, Pref);
2353 Pref := Prefix (Pref);
2355 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2356 -- is set to Standard_Standard, and no attempt should be made to
2357 -- further unwind scopes.
2359 if Par_Name /= Standard_Standard then
2360 Par_Name := Scope (Par_Name);
2364 if Present (Entity (Pref))
2365 and then not Analyzed (Parent (Parent (Entity (Pref))))
2367 -- If the entity is set without its unit being compiled, the
2368 -- original parent is a renaming, and Par_Name is the renamed
2369 -- entity. For visibility purposes, we need the original entity,
2370 -- which must be analyzed now because Load_Unit directly retrieves
2371 -- the renamed unit, and the renaming declaration itself has not
2374 Analyze (Parent (Parent (Entity (Pref))));
2375 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2376 Par_Name := Entity (Pref);
2379 Set_Entity_With_Style_Check (Pref, Par_Name);
2380 Generate_Reference (Par_Name, Pref);
2383 -- If the withed unit is System, and a system extension pragma is
2384 -- present, compile the extension now, rather than waiting for a
2385 -- visibility check on a specific entity.
2387 if Chars (E_Name) = Name_System
2388 and then Scope (E_Name) = Standard_Standard
2389 and then Present (System_Extend_Unit)
2390 and then Present_System_Aux (N)
2392 -- If the extension is not present, an error will have been emitted
2397 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2398 -- to private_with units; they will be made visible later (just before
2399 -- the private part is analyzed)
2401 if Private_Present (N) then
2402 Set_Is_Immediately_Visible (E_Name, False);
2404 end Analyze_With_Clause;
2406 ------------------------------
2407 -- Analyze_With_Type_Clause --
2408 ------------------------------
2410 procedure Analyze_With_Type_Clause (N : Node_Id) is
2411 Loc : constant Source_Ptr := Sloc (N);
2412 Nam : constant Node_Id := Name (N);
2416 Unum : Unit_Number_Type;
2419 procedure Decorate_Tagged_Type (T : Entity_Id);
2420 -- Set basic attributes of type, including its class_wide type
2422 function In_Chain (E : Entity_Id) return Boolean;
2423 -- Check that the imported type is not already in the homonym chain,
2424 -- for example through a with_type clause in a parent unit.
2426 --------------------------
2427 -- Decorate_Tagged_Type --
2428 --------------------------
2430 procedure Decorate_Tagged_Type (T : Entity_Id) is
2434 Set_Ekind (T, E_Record_Type);
2435 Set_Is_Tagged_Type (T);
2437 Set_From_With_Type (T);
2440 if not In_Chain (T) then
2441 Set_Homonym (T, Current_Entity (T));
2442 Set_Current_Entity (T);
2445 -- Build bogus class_wide type, if not previously done
2447 if No (Class_Wide_Type (T)) then
2448 CW := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
2450 Set_Ekind (CW, E_Class_Wide_Type);
2453 Set_Is_Tagged_Type (CW);
2454 Set_Is_First_Subtype (CW, True);
2455 Init_Size_Align (CW);
2456 Set_Has_Unknown_Discriminants
2458 Set_Class_Wide_Type (CW, CW);
2459 Set_Equivalent_Type (CW, Empty);
2460 Set_From_With_Type (CW);
2462 Set_Class_Wide_Type (T, CW);
2464 end Decorate_Tagged_Type;
2470 function In_Chain (E : Entity_Id) return Boolean is
2474 H := Current_Entity (E);
2475 while Present (H) loop
2486 -- Start of processing for Analyze_With_Type_Clause
2489 if Nkind (Nam) = N_Selected_Component then
2490 Pack := New_Copy_Tree (Prefix (Nam));
2491 Sel := Selector_Name (Nam);
2494 Error_Msg_N ("illegal name for imported type", Nam);
2499 Make_Package_Declaration (Loc,
2501 (Make_Package_Specification (Loc,
2502 Defining_Unit_Name => Pack,
2503 Visible_Declarations => New_List,
2504 End_Label => Empty)));
2508 (Load_Name => Get_Unit_Name (Decl),
2514 or else Nkind (Unit (Cunit (Unum))) /= N_Package_Declaration
2516 Error_Msg_N ("imported type must be declared in package", Nam);
2519 elsif Unum = Current_Sem_Unit then
2521 -- If type is defined in unit being analyzed, then the clause
2527 P := Cunit_Entity (Unum);
2530 -- Find declaration for imported type, and set its basic attributes
2531 -- if it has not been analyzed (which will be the case if there is
2532 -- circular dependence).
2539 if not Analyzed (Cunit (Unum))
2540 and then not From_With_Type (P)
2542 Set_Ekind (P, E_Package);
2543 Set_Etype (P, Standard_Void_Type);
2544 Set_From_With_Type (P);
2545 Set_Scope (P, Standard_Standard);
2546 Set_Homonym (P, Current_Entity (P));
2547 Set_Current_Entity (P);
2549 elsif Analyzed (Cunit (Unum))
2550 and then Is_Child_Unit (P)
2552 -- If the child unit is already in scope, indicate that it is
2553 -- visible, and remains so after intervening calls to rtsfind.
2555 Set_Is_Visible_Child_Unit (P);
2558 if Nkind (Parent (P)) = N_Defining_Program_Unit_Name then
2560 -- Make parent packages visible
2563 Parent_Comp : Node_Id;
2564 Parent_Id : Entity_Id;
2569 Parent_Comp := Parent_Spec (Unit (Cunit (Unum)));
2572 Parent_Id := Defining_Entity (Unit (Parent_Comp));
2573 Set_Scope (Child, Parent_Id);
2575 -- The type may be imported from a child unit, in which
2576 -- case the current compilation appears in the name. Do
2577 -- not change its visibility here because it will conflict
2578 -- with the subsequent normal processing.
2580 if not Analyzed (Unit_Declaration_Node (Parent_Id))
2581 and then Parent_Id /= Cunit_Entity (Current_Sem_Unit)
2583 Set_Ekind (Parent_Id, E_Package);
2584 Set_Etype (Parent_Id, Standard_Void_Type);
2586 -- The same package may appear is several with_type
2589 if not From_With_Type (Parent_Id) then
2590 Set_Homonym (Parent_Id, Current_Entity (Parent_Id));
2591 Set_Current_Entity (Parent_Id);
2592 Set_From_With_Type (Parent_Id);
2596 Set_Is_Immediately_Visible (Parent_Id);
2599 Parent_Comp := Parent_Spec (Unit (Parent_Comp));
2600 exit when No (Parent_Comp);
2603 Set_Scope (Parent_Id, Standard_Standard);
2607 -- Even if analyzed, the package may not be currently visible. It
2608 -- must be while the with_type clause is active.
2610 Set_Is_Immediately_Visible (P);
2613 First (Visible_Declarations (Specification (Unit (Cunit (Unum)))));
2614 while Present (Decl) loop
2615 if Nkind (Decl) = N_Full_Type_Declaration
2616 and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
2618 Typ := Defining_Identifier (Decl);
2620 if Tagged_Present (N) then
2622 -- The declaration must indicate that this is a tagged
2623 -- type or a type extension.
2625 if (Nkind (Type_Definition (Decl)) = N_Record_Definition
2626 and then Tagged_Present (Type_Definition (Decl)))
2628 (Nkind (Type_Definition (Decl))
2629 = N_Derived_Type_Definition
2631 (Record_Extension_Part (Type_Definition (Decl))))
2635 Error_Msg_N ("imported type is not a tagged type", Nam);
2639 if not Analyzed (Decl) then
2641 -- Unit is not currently visible. Add basic attributes
2642 -- to type and build its class-wide type.
2644 Init_Size_Align (Typ);
2645 Decorate_Tagged_Type (Typ);
2649 if Nkind (Type_Definition (Decl))
2650 /= N_Access_To_Object_Definition
2653 ("imported type is not an access type", Nam);
2655 elsif not Analyzed (Decl) then
2656 Set_Ekind (Typ, E_Access_Type);
2657 Set_Etype (Typ, Typ);
2659 Init_Size (Typ, System_Address_Size);
2660 Init_Alignment (Typ);
2661 Set_Directly_Designated_Type (Typ, Standard_Integer);
2662 Set_From_With_Type (Typ);
2664 if not In_Chain (Typ) then
2665 Set_Homonym (Typ, Current_Entity (Typ));
2666 Set_Current_Entity (Typ);
2671 Set_Entity (Sel, Typ);
2674 elsif ((Nkind (Decl) = N_Private_Type_Declaration
2675 and then Tagged_Present (Decl))
2676 or else (Nkind (Decl) = N_Private_Extension_Declaration))
2677 and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
2679 Typ := Defining_Identifier (Decl);
2681 if not Tagged_Present (N) then
2682 Error_Msg_N ("type must be declared tagged", N);
2684 elsif not Analyzed (Decl) then
2685 Decorate_Tagged_Type (Typ);
2688 Set_Entity (Sel, Typ);
2689 Set_From_With_Type (Typ);
2693 Decl := Next (Decl);
2696 Error_Msg_NE ("not a visible access or tagged type in&", Nam, P);
2698 end Analyze_With_Type_Clause;
2700 -----------------------------
2701 -- Check_With_Type_Clauses --
2702 -----------------------------
2704 procedure Check_With_Type_Clauses (N : Node_Id) is
2705 Lib_Unit : constant Node_Id := Unit (N);
2707 procedure Check_Parent_Context (U : Node_Id);
2708 -- Examine context items of parent unit to locate with_type clauses
2710 --------------------------
2711 -- Check_Parent_Context --
2712 --------------------------
2714 procedure Check_Parent_Context (U : Node_Id) is
2718 Item := First (Context_Items (U));
2719 while Present (Item) loop
2720 if Nkind (Item) = N_With_Type_Clause
2721 and then not Error_Posted (Item)
2723 From_With_Type (Scope (Entity (Selector_Name (Name (Item)))))
2725 Error_Msg_Sloc := Sloc (Item);
2726 Error_Msg_N ("missing With_Clause for With_Type_Clause#", N);
2731 end Check_Parent_Context;
2733 -- Start of processing for Check_With_Type_Clauses
2736 if Extensions_Allowed
2737 and then (Nkind (Lib_Unit) = N_Package_Body
2738 or else Nkind (Lib_Unit) = N_Subprogram_Body)
2740 Check_Parent_Context (Library_Unit (N));
2742 if Is_Child_Spec (Unit (Library_Unit (N))) then
2743 Check_Parent_Context (Parent_Spec (Unit (Library_Unit (N))));
2746 end Check_With_Type_Clauses;
2748 ------------------------------
2749 -- Check_Private_Child_Unit --
2750 ------------------------------
2752 procedure Check_Private_Child_Unit (N : Node_Id) is
2753 Lib_Unit : constant Node_Id := Unit (N);
2755 Curr_Unit : Entity_Id;
2756 Sub_Parent : Node_Id;
2757 Priv_Child : Entity_Id;
2758 Par_Lib : Entity_Id;
2761 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2762 -- Returns true if and only if the library unit is declared with
2763 -- an explicit designation of private.
2765 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2766 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2769 return Private_Present (Comp_Unit);
2770 end Is_Private_Library_Unit;
2772 -- Start of processing for Check_Private_Child_Unit
2775 if Nkind (Lib_Unit) = N_Package_Body
2776 or else Nkind (Lib_Unit) = N_Subprogram_Body
2778 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2779 Par_Lib := Curr_Unit;
2781 elsif Nkind (Lib_Unit) = N_Subunit then
2783 -- The parent is itself a body. The parent entity is to be found
2784 -- in the corresponding spec.
2786 Sub_Parent := Library_Unit (N);
2787 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2789 -- If the parent itself is a subunit, Curr_Unit is the entity
2790 -- of the enclosing body, retrieve the spec entity which is
2791 -- the proper ancestor we need for the following tests.
2793 if Ekind (Curr_Unit) = E_Package_Body then
2794 Curr_Unit := Spec_Entity (Curr_Unit);
2797 Par_Lib := Curr_Unit;
2800 Curr_Unit := Defining_Entity (Lib_Unit);
2802 Par_Lib := Curr_Unit;
2803 Par_Spec := Parent_Spec (Lib_Unit);
2805 if No (Par_Spec) then
2808 Par_Lib := Defining_Entity (Unit (Par_Spec));
2812 -- Loop through context items
2814 Item := First (Context_Items (N));
2815 while Present (Item) loop
2817 -- Ada 2005 (AI-262): Allow private_with of a private child package
2818 -- in public siblings
2820 if Nkind (Item) = N_With_Clause
2821 and then not Implicit_With (Item)
2822 and then Is_Private_Descendant (Entity (Name (Item)))
2824 Priv_Child := Entity (Name (Item));
2827 Curr_Parent : Entity_Id := Par_Lib;
2828 Child_Parent : Entity_Id := Scope (Priv_Child);
2829 Prv_Ancestor : Entity_Id := Child_Parent;
2830 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2833 -- If the child unit is a public child then locate
2834 -- the nearest private ancestor; Child_Parent will
2835 -- then be set to the parent of that ancestor.
2837 if not Is_Private_Library_Unit (Priv_Child) then
2838 while Present (Prv_Ancestor)
2839 and then not Is_Private_Library_Unit (Prv_Ancestor)
2841 Prv_Ancestor := Scope (Prv_Ancestor);
2844 if Present (Prv_Ancestor) then
2845 Child_Parent := Scope (Prv_Ancestor);
2849 while Present (Curr_Parent)
2850 and then Curr_Parent /= Standard_Standard
2851 and then Curr_Parent /= Child_Parent
2854 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2855 Curr_Parent := Scope (Curr_Parent);
2858 if No (Curr_Parent) then
2859 Curr_Parent := Standard_Standard;
2862 if Curr_Parent /= Child_Parent then
2863 if Ekind (Priv_Child) = E_Generic_Package
2864 and then Chars (Priv_Child) in Text_IO_Package_Name
2865 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2868 ("& is a nested package, not a compilation unit",
2869 Name (Item), Priv_Child);
2873 ("unit in with clause is private child unit!", Item);
2875 ("\current unit must also have parent&!",
2876 Item, Child_Parent);
2879 elsif not Curr_Private
2880 and then not Private_Present (Item)
2881 and then Nkind (Lib_Unit) /= N_Package_Body
2882 and then Nkind (Lib_Unit) /= N_Subprogram_Body
2883 and then Nkind (Lib_Unit) /= N_Subunit
2886 ("current unit must also be private descendant of&",
2887 Item, Child_Parent);
2895 end Check_Private_Child_Unit;
2897 ----------------------
2898 -- Check_Stub_Level --
2899 ----------------------
2901 procedure Check_Stub_Level (N : Node_Id) is
2902 Par : constant Node_Id := Parent (N);
2903 Kind : constant Node_Kind := Nkind (Par);
2906 if (Kind = N_Package_Body
2907 or else Kind = N_Subprogram_Body
2908 or else Kind = N_Task_Body
2909 or else Kind = N_Protected_Body)
2910 and then (Nkind (Parent (Par)) = N_Compilation_Unit
2911 or else Nkind (Parent (Par)) = N_Subunit)
2915 -- In an instance, a missing stub appears at any level. A warning
2916 -- message will have been emitted already for the missing file.
2918 elsif not In_Instance then
2919 Error_Msg_N ("stub cannot appear in an inner scope", N);
2921 elsif Expander_Active then
2922 Error_Msg_N ("missing proper body", N);
2924 end Check_Stub_Level;
2926 ------------------------
2927 -- Expand_With_Clause --
2928 ------------------------
2930 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2931 Loc : constant Source_Ptr := Sloc (Nam);
2932 Ent : constant Entity_Id := Entity (Nam);
2936 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2937 -- Comment requireed here ???
2939 ---------------------
2940 -- Build_Unit_Name --
2941 ---------------------
2943 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2947 if Nkind (Nam) = N_Identifier then
2948 return New_Occurrence_Of (Entity (Nam), Loc);
2952 Make_Expanded_Name (Loc,
2953 Chars => Chars (Entity (Nam)),
2954 Prefix => Build_Unit_Name (Prefix (Nam)),
2955 Selector_Name => New_Occurrence_Of (Entity (Nam), Loc));
2956 Set_Entity (Result, Entity (Nam));
2959 end Build_Unit_Name;
2961 -- Start of processing for Expand_With_Clause
2964 New_Nodes_OK := New_Nodes_OK + 1;
2966 Make_With_Clause (Loc, Name => Build_Unit_Name (Nam));
2968 P := Parent (Unit_Declaration_Node (Ent));
2969 Set_Library_Unit (Withn, P);
2970 Set_Corresponding_Spec (Withn, Ent);
2971 Set_First_Name (Withn, True);
2972 Set_Implicit_With (Withn, True);
2974 -- If the unit is a package declaration, a private_with_clause on a
2975 -- child unit implies that the implicit with on the parent is also
2978 if Nkind (Unit (N)) = N_Package_Declaration then
2979 Set_Private_Present (Withn, Private_Present (Item));
2982 Prepend (Withn, Context_Items (N));
2983 Mark_Rewrite_Insertion (Withn);
2984 Install_Withed_Unit (Withn);
2986 if Nkind (Nam) = N_Expanded_Name then
2987 Expand_With_Clause (Item, Prefix (Nam), N);
2990 New_Nodes_OK := New_Nodes_OK - 1;
2991 end Expand_With_Clause;
2993 -----------------------
2994 -- Get_Parent_Entity --
2995 -----------------------
2997 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2999 if Nkind (Unit) = N_Package_Body
3000 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
3004 (Specification (Instance_Spec (Original_Node (Unit))));
3006 elsif Nkind (Unit) = N_Package_Instantiation then
3007 return Defining_Entity (Specification (Instance_Spec (Unit)));
3010 return Defining_Entity (Unit);
3012 end Get_Parent_Entity;
3014 -----------------------------
3015 -- Implicit_With_On_Parent --
3016 -----------------------------
3018 procedure Implicit_With_On_Parent
3019 (Child_Unit : Node_Id;
3022 Loc : constant Source_Ptr := Sloc (N);
3023 P : constant Node_Id := Parent_Spec (Child_Unit);
3025 P_Unit : Node_Id := Unit (P);
3027 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3030 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3031 -- Build prefix of child unit name. Recurse if needed
3033 function Build_Unit_Name return Node_Id;
3034 -- If the unit is a child unit, build qualified name with all
3037 -------------------------
3038 -- Build_Ancestor_Name --
3039 -------------------------
3041 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3042 P_Ref : constant Node_Id :=
3043 New_Reference_To (Defining_Entity (P), Loc);
3044 P_Spec : Node_Id := P;
3047 -- Ancestor may have been rewritten as a package body. Retrieve
3048 -- the original spec to trace earlier ancestors.
3050 if Nkind (P) = N_Package_Body
3051 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3053 P_Spec := Original_Node (P);
3056 if No (Parent_Spec (P_Spec)) then
3060 Make_Selected_Component (Loc,
3061 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3062 Selector_Name => P_Ref);
3064 end Build_Ancestor_Name;
3066 ---------------------
3067 -- Build_Unit_Name --
3068 ---------------------
3070 function Build_Unit_Name return Node_Id is
3073 if No (Parent_Spec (P_Unit)) then
3074 return New_Reference_To (P_Name, Loc);
3077 Make_Expanded_Name (Loc,
3078 Chars => Chars (P_Name),
3079 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3080 Selector_Name => New_Reference_To (P_Name, Loc));
3081 Set_Entity (Result, P_Name);
3084 end Build_Unit_Name;
3086 -- Start of processing for Implicit_With_On_Parent
3089 -- The unit of the current compilation may be a package body
3090 -- that replaces an instance node. In this case we need the
3091 -- original instance node to construct the proper parent name.
3093 if Nkind (P_Unit) = N_Package_Body
3094 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3096 P_Unit := Original_Node (P_Unit);
3099 -- We add the implicit with if the child unit is the current unit
3100 -- being compiled. If the current unit is a body, we do not want
3101 -- to add an implicit_with a second time to the corresponding spec.
3103 if Nkind (Child_Unit) = N_Package_Declaration
3104 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3109 New_Nodes_OK := New_Nodes_OK + 1;
3110 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3112 Set_Library_Unit (Withn, P);
3113 Set_Corresponding_Spec (Withn, P_Name);
3114 Set_First_Name (Withn, True);
3115 Set_Implicit_With (Withn, True);
3117 -- Node is placed at the beginning of the context items, so that
3118 -- subsequent use clauses on the parent can be validated.
3120 Prepend (Withn, Context_Items (N));
3121 Mark_Rewrite_Insertion (Withn);
3122 Install_Withed_Unit (Withn);
3124 if Is_Child_Spec (P_Unit) then
3125 Implicit_With_On_Parent (P_Unit, N);
3128 New_Nodes_OK := New_Nodes_OK - 1;
3129 end Implicit_With_On_Parent;
3135 function In_Chain (E : Entity_Id) return Boolean is
3139 H := Current_Entity (E);
3140 while Present (H) loop
3151 ---------------------
3152 -- Install_Context --
3153 ---------------------
3155 procedure Install_Context (N : Node_Id) is
3156 Lib_Unit : constant Node_Id := Unit (N);
3159 Install_Context_Clauses (N);
3161 if Is_Child_Spec (Lib_Unit) then
3162 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3165 Install_Limited_Context_Clauses (N);
3167 Check_With_Type_Clauses (N);
3168 end Install_Context;
3170 -----------------------------
3171 -- Install_Context_Clauses --
3172 -----------------------------
3174 procedure Install_Context_Clauses (N : Node_Id) is
3175 Lib_Unit : constant Node_Id := Unit (N);
3177 Uname_Node : Entity_Id;
3178 Check_Private : Boolean := False;
3179 Decl_Node : Node_Id;
3180 Lib_Parent : Entity_Id;
3183 -- First skip configuration pragmas at the start of the context. They
3184 -- are not technically part of the context clause, but that's where the
3185 -- parser puts them. Note they were analyzed in Analyze_Context.
3187 Item := First (Context_Items (N));
3188 while Present (Item)
3189 and then Nkind (Item) = N_Pragma
3190 and then Chars (Item) in Configuration_Pragma_Names
3195 -- Loop through the actual context clause items. We process everything
3196 -- except Limited_With clauses in this routine. Limited_With clauses
3197 -- are separately installed (see Install_Limited_Context_Clauses).
3199 while Present (Item) loop
3201 -- Case of explicit WITH clause
3203 if Nkind (Item) = N_With_Clause
3204 and then not Implicit_With (Item)
3206 if Limited_Present (Item) then
3208 -- Limited withed units will be installed later
3212 -- If Name (Item) is not an entity name, something is wrong, and
3213 -- this will be detected in due course, for now ignore the item
3215 elsif not Is_Entity_Name (Name (Item)) then
3218 elsif No (Entity (Name (Item))) then
3219 Set_Entity (Name (Item), Any_Id);
3223 Uname_Node := Entity (Name (Item));
3225 if Is_Private_Descendant (Uname_Node) then
3226 Check_Private := True;
3229 Install_Withed_Unit (Item);
3231 Decl_Node := Unit_Declaration_Node (Uname_Node);
3233 -- If the unit is a subprogram instance, it appears nested
3234 -- within a package that carries the parent information.
3236 if Is_Generic_Instance (Uname_Node)
3237 and then Ekind (Uname_Node) /= E_Package
3239 Decl_Node := Parent (Parent (Decl_Node));
3242 if Is_Child_Spec (Decl_Node) then
3243 if Nkind (Name (Item)) = N_Expanded_Name then
3244 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3246 -- if not an expanded name, the child unit must be a
3247 -- renaming, nothing to do.
3252 elsif Nkind (Decl_Node) = N_Subprogram_Body
3253 and then not Acts_As_Spec (Parent (Decl_Node))
3254 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3256 Implicit_With_On_Parent
3257 (Unit (Library_Unit (Parent (Decl_Node))), N);
3260 -- Check license conditions unless this is a dummy unit
3262 if Sloc (Library_Unit (Item)) /= No_Location then
3263 License_Check : declare
3265 Withu : constant Unit_Number_Type :=
3266 Get_Source_Unit (Library_Unit (Item));
3268 Withl : constant License_Type :=
3269 License (Source_Index (Withu));
3271 Unitl : constant License_Type :=
3272 License (Source_Index (Current_Sem_Unit));
3274 procedure License_Error;
3275 -- Signal error of bad license
3281 procedure License_Error is
3284 ("?license of with'ed unit & may be inconsistent",
3288 -- Start of processing for License_Check
3291 -- Exclude license check if withed unit is an internal unit.
3292 -- This situation arises e.g. with the GPL version of GNAT.
3294 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3297 -- Otherwise check various cases
3309 if Withl = Restricted then
3313 when Modified_GPL =>
3314 if Withl = Restricted or else Withl = GPL then
3318 when Unrestricted =>
3325 -- Case of USE PACKAGE clause
3327 elsif Nkind (Item) = N_Use_Package_Clause then
3328 Analyze_Use_Package (Item);
3330 -- Case of USE TYPE clause
3332 elsif Nkind (Item) = N_Use_Type_Clause then
3333 Analyze_Use_Type (Item);
3335 -- Case of WITH TYPE clause
3337 -- A With_Type_Clause is processed when installing the context,
3338 -- because it is a visibility mechanism and does not create a
3339 -- semantic dependence on other units, as a With_Clause does.
3341 elsif Nkind (Item) = N_With_Type_Clause then
3342 Analyze_With_Type_Clause (Item);
3346 elsif Nkind (Item) = N_Pragma then
3354 if Is_Child_Spec (Lib_Unit) then
3356 -- The unit also has implicit withs on its own parents
3358 if No (Context_Items (N)) then
3359 Set_Context_Items (N, New_List);
3362 Implicit_With_On_Parent (Lib_Unit, N);
3365 -- If the unit is a body, the context of the specification must also
3368 if Nkind (Lib_Unit) = N_Package_Body
3369 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3370 and then not Acts_As_Spec (N))
3372 Install_Context (Library_Unit (N));
3374 if Is_Child_Spec (Unit (Library_Unit (N))) then
3376 -- If the unit is the body of a public child unit, the private
3377 -- declarations of the parent must be made visible. If the child
3378 -- unit is private, the private declarations have been installed
3379 -- already in the call to Install_Parents for the spec. Installing
3380 -- private declarations must be done for all ancestors of public
3381 -- child units. In addition, sibling units mentioned in the
3382 -- context clause of the body are directly visible.
3390 Lib_Spec := Unit (Library_Unit (N));
3391 while Is_Child_Spec (Lib_Spec) loop
3392 P := Unit (Parent_Spec (Lib_Spec));
3393 P_Name := Defining_Entity (P);
3395 if not (Private_Present (Parent (Lib_Spec)))
3396 and then not In_Private_Part (P_Name)
3398 Install_Private_Declarations (P_Name);
3399 Install_Private_With_Clauses (P_Name);
3400 Set_Use (Private_Declarations (Specification (P)));
3408 -- For a package body, children in context are immediately visible
3410 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3413 if Nkind (Lib_Unit) = N_Generic_Package_Declaration
3414 or else Nkind (Lib_Unit) = N_Generic_Subprogram_Declaration
3415 or else Nkind (Lib_Unit) = N_Package_Declaration
3416 or else Nkind (Lib_Unit) = N_Subprogram_Declaration
3418 if Is_Child_Spec (Lib_Unit) then
3419 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3420 Set_Is_Private_Descendant
3421 (Defining_Entity (Lib_Unit),
3422 Is_Private_Descendant (Lib_Parent)
3423 or else Private_Present (Parent (Lib_Unit)));
3426 Set_Is_Private_Descendant
3427 (Defining_Entity (Lib_Unit),
3428 Private_Present (Parent (Lib_Unit)));
3432 if Check_Private then
3433 Check_Private_Child_Unit (N);
3435 end Install_Context_Clauses;
3437 -------------------------------------
3438 -- Install_Limited_Context_Clauses --
3439 -------------------------------------
3441 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3444 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3445 -- Check that the unlimited view of a given compilation_unit is not
3446 -- already visible through "use + renamings".
3448 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3449 -- Check that if a limited_with clause of a given compilation_unit
3450 -- mentions a descendant of a private child of some library unit,
3451 -- then the given compilation_unit shall be the declaration of a
3452 -- private descendant of that library unit.
3454 procedure Expand_Limited_With_Clause
3455 (Comp_Unit : Node_Id; Nam : Node_Id; N : Node_Id);
3456 -- If a child unit appears in a limited_with clause, there are implicit
3457 -- limited_with clauses on all parents that are not already visible
3458 -- through a regular with clause. This procedure creates the implicit
3459 -- limited with_clauses for the parents and loads the corresponding
3460 -- units. The shadow entities are created when the inserted clause is
3461 -- analyzed. Implements Ada 2005 (AI-50217).
3463 ---------------------
3464 -- Check_Renamings --
3465 ---------------------
3467 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3476 pragma Assert (Nkind (W) = N_With_Clause);
3478 -- Protect the frontend against previous critical errors
3480 case Nkind (Unit (Library_Unit (W))) is
3481 when N_Subprogram_Declaration |
3482 N_Package_Declaration |
3483 N_Generic_Subprogram_Declaration |
3484 N_Generic_Package_Declaration =>
3491 -- Check "use + renamings"
3493 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3494 Spec := Specification (Unit (P));
3496 Item := First (Visible_Declarations (Spec));
3497 while Present (Item) loop
3499 -- Look only at use package clauses
3501 if Nkind (Item) = N_Use_Package_Clause then
3503 -- Traverse the list of packages
3505 Nam := First (Names (Item));
3506 while Present (Nam) loop
3509 pragma Assert (Present (Parent (E)));
3511 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3512 and then Renamed_Entity (E) = WEnt
3514 -- The unlimited view is visible through use clause and
3515 -- renamings. There is not need to generate the error
3516 -- message here because Is_Visible_Through_Renamings
3517 -- takes care of generating the precise error message.
3521 elsif Nkind (Parent (E)) = N_Package_Specification then
3523 -- The use clause may refer to a local package.
3524 -- Check all the enclosing scopes.
3527 while E2 /= Standard_Standard
3528 and then E2 /= WEnt loop
3534 ("unlimited view visible through use clause ", W);
3546 -- Recursive call to check all the ancestors
3548 if Is_Child_Spec (Unit (P)) then
3549 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3551 end Check_Renamings;
3553 ---------------------------------------
3554 -- Check_Private_Limited_Withed_Unit --
3555 ---------------------------------------
3557 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3558 Curr_Parent : Node_Id;
3559 Child_Parent : Node_Id;
3562 -- Compilation unit of the parent of the withed library unit
3564 Child_Parent := Parent_Spec (Unit (Library_Unit (Item)));
3566 -- If the child unit is a public child, then locate its nearest
3567 -- private ancestor, if any; Child_Parent will then be set to
3568 -- the parent of that ancestor.
3570 if not Private_Present (Library_Unit (Item)) then
3571 while Present (Child_Parent)
3572 and then not Private_Present (Child_Parent)
3574 Child_Parent := Parent_Spec (Unit (Child_Parent));
3577 if No (Child_Parent) then
3581 Child_Parent := Parent_Spec (Unit (Child_Parent));
3584 -- Traverse all the ancestors of the current compilation
3585 -- unit to check if it is a descendant of named library unit.
3587 Curr_Parent := Parent (Item);
3589 while Present (Parent_Spec (Unit (Curr_Parent)))
3590 and then Curr_Parent /= Child_Parent
3592 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3595 if Curr_Parent /= Child_Parent then
3597 ("unit in with clause is private child unit!", Item);
3599 ("\current unit must also have parent&!",
3600 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3602 elsif not Private_Present (Parent (Item))
3603 and then not Private_Present (Item)
3604 and then Nkind (Unit (Parent (Item))) /= N_Package_Body
3605 and then Nkind (Unit (Parent (Item))) /= N_Subprogram_Body
3606 and then Nkind (Unit (Parent (Item))) /= N_Subunit
3609 ("current unit must also be private descendant of&",
3610 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3612 end Check_Private_Limited_Withed_Unit;
3614 --------------------------------
3615 -- Expand_Limited_With_Clause --
3616 --------------------------------
3618 procedure Expand_Limited_With_Clause
3619 (Comp_Unit : Node_Id;
3623 Loc : constant Source_Ptr := Sloc (Nam);
3624 Unum : Unit_Number_Type;
3627 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3628 -- Returns true if the context already includes a with_clause for
3629 -- this unit. If the with_clause is non-limited, the unit is fully
3630 -- visible and an implicit limited_with should not be created. If
3631 -- there is already a limited_with clause for W, a second one is
3632 -- simply redundant.
3634 --------------------------
3635 -- Previous_Withed_Unit --
3636 --------------------------
3638 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3642 -- A limited with_clause cannot appear in the same context_clause
3643 -- as a nonlimited with_clause which mentions the same library.
3645 Item := First (Context_Items (Comp_Unit));
3646 while Present (Item) loop
3647 if Nkind (Item) = N_With_Clause
3648 and then Library_Unit (Item) = Library_Unit (W)
3657 end Previous_Withed_Unit;
3659 -- Start of processing for Expand_Limited_With_Clause
3662 New_Nodes_OK := New_Nodes_OK + 1;
3664 if Nkind (Nam) = N_Identifier then
3666 -- Create node for name of withed unit
3669 Make_With_Clause (Loc,
3670 Name => New_Copy (Nam));
3672 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3674 Make_With_Clause (Loc,
3675 Name => Make_Selected_Component (Loc,
3676 Prefix => New_Copy_Tree (Prefix (Nam)),
3677 Selector_Name => New_Copy (Selector_Name (Nam))));
3678 Set_Parent (Withn, Parent (N));
3681 Set_Limited_Present (Withn);
3682 Set_First_Name (Withn);
3683 Set_Implicit_With (Withn);
3687 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3692 -- Do not generate a limited_with_clause on the current unit.
3693 -- This path is taken when a unit has a limited_with clause on
3694 -- one of its child units.
3696 if Unum = Current_Sem_Unit then
3700 Set_Library_Unit (Withn, Cunit (Unum));
3701 Set_Corresponding_Spec
3702 (Withn, Specification (Unit (Cunit (Unum))));
3704 if not Previous_Withed_Unit (Withn) then
3705 Prepend (Withn, Context_Items (Parent (N)));
3706 Mark_Rewrite_Insertion (Withn);
3708 -- Add implicit limited_with_clauses for parents of child units
3709 -- mentioned in limited_with clauses.
3711 if Nkind (Nam) = N_Selected_Component then
3712 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3717 if not Limited_View_Installed (Withn) then
3718 Install_Limited_Withed_Unit (Withn);
3722 New_Nodes_OK := New_Nodes_OK - 1;
3723 end Expand_Limited_With_Clause;
3725 -- Start of processing for Install_Limited_Context_Clauses
3728 Item := First (Context_Items (N));
3729 while Present (Item) loop
3730 if Nkind (Item) = N_With_Clause
3731 and then Limited_Present (Item)
3733 if Nkind (Name (Item)) = N_Selected_Component then
3734 Expand_Limited_With_Clause
3735 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3738 Check_Private_Limited_Withed_Unit (Item);
3740 if not Implicit_With (Item)
3741 and then Is_Child_Spec (Unit (N))
3743 Check_Renamings (Parent_Spec (Unit (N)), Item);
3746 -- A unit may have a limited with on itself if it has a
3747 -- limited with_clause on one of its child units. In that
3748 -- case it is already being compiled and it makes no sense
3749 -- to install its limited view.
3751 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3752 and then not Limited_View_Installed (Item)
3754 Install_Limited_Withed_Unit (Item);
3757 -- All items other than Limited_With clauses are ignored (they were
3758 -- installed separately early on by Install_Context_Clause).
3767 -- Ada 2005 (AI-412): Examine the visible declarations of a package
3768 -- spec, looking for incomplete subtype declarations of incomplete
3769 -- types visible through a limited with clause.
3771 if Ada_Version >= Ada_05
3772 and then Analyzed (N)
3773 and then Nkind (Unit (N)) = N_Package_Declaration
3778 Non_Lim_View : Entity_Id;
3781 Decl := First (Visible_Declarations (Specification (Unit (N))));
3782 while Present (Decl) loop
3783 if Nkind (Decl) = N_Subtype_Declaration
3785 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3787 From_With_Type (Defining_Identifier (Decl))
3789 Def_Id := Defining_Identifier (Decl);
3790 Non_Lim_View := Non_Limited_View (Def_Id);
3792 if not Is_Incomplete_Type (Non_Lim_View) then
3794 -- Convert an incomplete subtype declaration into a
3795 -- corresponding non-limited view subtype declaration.
3796 -- This is usually the case when analyzing a body that
3797 -- has regular with-clauses, when the spec has limited
3799 -- if the non-limited view is still incomplete, it is
3800 -- the dummy entry already created, and the declaration
3801 -- cannot be reanalyzed. This is the case when installing
3802 -- a parent unit that has limited with-clauses.
3804 Set_Subtype_Indication (Decl,
3805 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3806 Set_Etype (Def_Id, Non_Lim_View);
3807 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3808 Set_Analyzed (Decl, False);
3810 -- Reanalyze the declaration, suppressing the call to
3811 -- Enter_Name to avoid duplicate names.
3813 Analyze_Subtype_Declaration
3823 end Install_Limited_Context_Clauses;
3825 ---------------------
3826 -- Install_Parents --
3827 ---------------------
3829 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3836 P := Unit (Parent_Spec (Lib_Unit));
3837 P_Name := Get_Parent_Entity (P);
3839 if Etype (P_Name) = Any_Type then
3843 if Ekind (P_Name) = E_Generic_Package
3844 and then Nkind (Lib_Unit) /= N_Generic_Subprogram_Declaration
3845 and then Nkind (Lib_Unit) /= N_Generic_Package_Declaration
3846 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3849 ("child of a generic package must be a generic unit", Lib_Unit);
3851 elsif not Is_Package_Or_Generic_Package (P_Name) then
3853 ("parent unit must be package or generic package", Lib_Unit);
3854 raise Unrecoverable_Error;
3856 elsif Present (Renamed_Object (P_Name)) then
3857 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3858 raise Unrecoverable_Error;
3860 -- Verify that a child of an instance is itself an instance, or
3861 -- the renaming of one. Given that an instance that is a unit is
3862 -- replaced with a package declaration, check against the original
3863 -- node. The parent may be currently being instantiated, in which
3864 -- case it appears as a declaration, but the generic_parent is
3865 -- already established indicating that we deal with an instance.
3867 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3869 if Nkind (Lib_Unit) in N_Renaming_Declaration
3870 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3872 (Nkind (Lib_Unit) = N_Package_Declaration
3873 and then Present (Generic_Parent (Specification (Lib_Unit))))
3878 ("child of an instance must be an instance or renaming",
3883 -- This is the recursive call that ensures all parents are loaded
3885 if Is_Child_Spec (P) then
3887 Is_Private or else Private_Present (Parent (Lib_Unit)));
3890 -- Now we can install the context for this parent
3892 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3893 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3894 Install_Siblings (P_Name, Parent (Lib_Unit));
3896 -- The child unit is in the declarative region of the parent. The
3897 -- parent must therefore appear in the scope stack and be visible,
3898 -- as when compiling the corresponding body. If the child unit is
3899 -- private or it is a package body, private declarations must be
3900 -- accessible as well. Use declarations in the parent must also
3901 -- be installed. Finally, other child units of the same parent that
3902 -- are in the context are immediately visible.
3904 -- Find entity for compilation unit, and set its private descendant
3905 -- status as needed.
3907 E_Name := Defining_Entity (Lib_Unit);
3909 Set_Is_Child_Unit (E_Name);
3911 Set_Is_Private_Descendant (E_Name,
3912 Is_Private_Descendant (P_Name)
3913 or else Private_Present (Parent (Lib_Unit)));
3915 P_Spec := Specification (Unit_Declaration_Node (P_Name));
3918 -- Save current visibility of unit
3920 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3921 Is_Immediately_Visible (P_Name);
3922 Set_Is_Immediately_Visible (P_Name);
3923 Install_Visible_Declarations (P_Name);
3924 Set_Use (Visible_Declarations (P_Spec));
3926 -- If the parent is a generic unit, its formal part may contain
3927 -- formal packages and use clauses for them.
3929 if Ekind (P_Name) = E_Generic_Package then
3930 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3934 or else Private_Present (Parent (Lib_Unit))
3936 Install_Private_Declarations (P_Name);
3937 Install_Private_With_Clauses (P_Name);
3938 Set_Use (Private_Declarations (P_Spec));
3940 end Install_Parents;
3942 ----------------------------------
3943 -- Install_Private_With_Clauses --
3944 ----------------------------------
3946 procedure Install_Private_With_Clauses (P : Entity_Id) is
3947 Decl : constant Node_Id := Unit_Declaration_Node (P);
3951 if Debug_Flag_I then
3952 Write_Str ("install private with clauses of ");
3953 Write_Name (Chars (P));
3957 if Nkind (Parent (Decl)) = N_Compilation_Unit then
3958 Item := First (Context_Items (Parent (Decl)));
3959 while Present (Item) loop
3960 if Nkind (Item) = N_With_Clause
3961 and then Private_Present (Item)
3963 if Limited_Present (Item) then
3964 if not Limited_View_Installed (Item) then
3965 Install_Limited_Withed_Unit (Item);
3968 Install_Withed_Unit (Item, Private_With_OK => True);
3975 end Install_Private_With_Clauses;
3977 ----------------------
3978 -- Install_Siblings --
3979 ----------------------
3981 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3986 -- Iterate over explicit with clauses, and check whether the
3987 -- scope of each entity is an ancestor of the current unit, in
3988 -- which case it is immediately visible.
3990 Item := First (Context_Items (N));
3991 while Present (Item) loop
3993 -- Do not install private_with_clauses if the unit is a package
3994 -- declaration, unless it is itself a private child unit.
3996 if Nkind (Item) = N_With_Clause
3997 and then not Implicit_With (Item)
3998 and then not Limited_Present (Item)
4000 (not Private_Present (Item)
4001 or else Nkind (Unit (N)) /= N_Package_Declaration
4002 or else Private_Present (N))
4004 Id := Entity (Name (Item));
4006 if Is_Child_Unit (Id)
4007 and then Is_Ancestor_Package (Scope (Id), U_Name)
4009 Set_Is_Immediately_Visible (Id);
4011 -- Check for the presence of another unit in the context,
4012 -- that may be inadvertently hidden by the child.
4014 Prev := Current_Entity (Id);
4017 and then Is_Immediately_Visible (Prev)
4018 and then not Is_Child_Unit (Prev)
4024 Clause := First (Context_Items (N));
4025 while Present (Clause) loop
4026 if Nkind (Clause) = N_With_Clause
4027 and then Entity (Name (Clause)) = Prev
4030 ("child unit& hides compilation unit " &
4031 "with the same name?",
4041 -- The With_Clause may be on a grand-child or one of its
4042 -- further descendants, which makes a child immediately visible.
4043 -- Examine ancestry to determine whether such a child exists.
4044 -- For example, if current unit is A.C, and with_clause is on
4045 -- A.X.Y.Z, then X is immediately visible.
4047 elsif Is_Child_Unit (Id) then
4053 while Is_Child_Unit (Par) loop
4054 if Is_Ancestor_Package (Scope (Par), U_Name) then
4055 Set_Is_Immediately_Visible (Par);
4067 end Install_Siblings;
4069 -------------------------------
4070 -- Install_Limited_With_Unit --
4071 -------------------------------
4073 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4074 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4077 Is_Child_Package : Boolean := False;
4079 Lim_Header : Entity_Id;
4080 Lim_Typ : Entity_Id;
4082 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4083 -- Check if some package installed though normal with-clauses has a
4084 -- renaming declaration of package P. AARM 10.1.2(21/2).
4086 ----------------------------------
4087 -- Is_Visible_Through_Renamings --
4088 ----------------------------------
4090 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4091 Kind : constant Node_Kind :=
4092 Nkind (Unit (Cunit (Current_Sem_Unit)));
4098 -- Example of the error detected by this subprogram:
4106 -- package Ren_P renames P;
4112 -- limited with P; -- ERROR
4113 -- package R.C is ...
4115 Aux_Unit := Cunit (Current_Sem_Unit);
4118 Item := First (Context_Items (Aux_Unit));
4119 while Present (Item) loop
4120 if Nkind (Item) = N_With_Clause
4121 and then not Limited_Present (Item)
4122 and then Nkind (Unit (Library_Unit (Item)))
4123 = N_Package_Declaration
4126 First (Visible_Declarations
4127 (Specification (Unit (Library_Unit (Item)))));
4128 while Present (Decl) loop
4129 if Nkind (Decl) = N_Package_Renaming_Declaration
4130 and then Entity (Name (Decl)) = P
4132 -- Generate the error message only if the current unit
4133 -- is a package declaration; in case of subprogram
4134 -- bodies and package bodies we just return true to
4135 -- indicate that the limited view must not be
4138 if Kind = N_Package_Declaration then
4140 ("simultaneous visibility of the limited and" &
4141 " unlimited views not allowed", N);
4142 Error_Msg_Sloc := Sloc (Item);
4144 ("\unlimited view of & visible through the" &
4145 " context clause found #", N, P);
4146 Error_Msg_Sloc := Sloc (Decl);
4147 Error_Msg_NE ("\and the renaming found #", N, P);
4160 if Present (Library_Unit (Aux_Unit)) then
4161 if Aux_Unit = Library_Unit (Aux_Unit) then
4163 -- Aux_Unit is a body that acts as a spec. Clause has
4164 -- already been flagged as illegal.
4169 Aux_Unit := Library_Unit (Aux_Unit);
4172 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4175 exit when No (Aux_Unit);
4179 end Is_Visible_Through_Renamings;
4181 -- Start of processing for Install_Limited_Withed_Unit
4184 pragma Assert (not Limited_View_Installed (N));
4186 -- In case of limited with_clause on subprograms, generics, instances,
4187 -- or renamings, the corresponding error was previously posted and we
4188 -- have nothing to do here.
4190 if Nkind (P_Unit) /= N_Package_Declaration then
4194 P := Defining_Unit_Name (Specification (P_Unit));
4196 -- Handle child packages
4198 if Nkind (P) = N_Defining_Program_Unit_Name then
4199 Is_Child_Package := True;
4200 P := Defining_Identifier (P);
4203 -- Do not install the limited-view if the full-view is already visible
4204 -- through renaming declarations.
4206 if Is_Visible_Through_Renamings (P) then
4210 -- A common use of the limited-with is to have a limited-with
4211 -- in the package spec, and a normal with in its package body.
4214 -- limited with X; -- [1]
4218 -- package body A is ...
4220 -- The compilation of A's body installs the context clauses found at [2]
4221 -- and then the context clauses of its specification (found at [1]). As
4222 -- a consequence, at [1] the specification of X has been analyzed and it
4223 -- is immediately visible. According to the semantics of limited-with
4224 -- context clauses we don't install the limited view because the full
4225 -- view of X supersedes its limited view.
4227 if Analyzed (P_Unit)
4228 and then (Is_Immediately_Visible (P)
4229 or else (Is_Child_Package
4230 and then Is_Visible_Child_Unit (P)))
4232 -- Ada 2005 (AI-262): Install the private declarations of P
4234 if Private_Present (N)
4235 and then not In_Private_Part (P)
4241 Id := First_Private_Entity (P);
4242 while Present (Id) loop
4243 if not Is_Internal (Id)
4244 and then not Is_Child_Unit (Id)
4246 if not In_Chain (Id) then
4247 Set_Homonym (Id, Current_Entity (Id));
4248 Set_Current_Entity (Id);
4251 Set_Is_Immediately_Visible (Id);
4257 Set_In_Private_Part (P);
4264 if Debug_Flag_I then
4265 Write_Str ("install limited view of ");
4266 Write_Name (Chars (P));
4270 -- If the unit has not been analyzed and the limited view has not been
4271 -- already installed then we install it.
4273 if not Analyzed (P_Unit) then
4274 if not In_Chain (P) then
4276 -- Minimum decoration
4278 Set_Ekind (P, E_Package);
4279 Set_Etype (P, Standard_Void_Type);
4280 Set_Scope (P, Standard_Standard);
4282 if Is_Child_Package then
4283 Set_Is_Child_Unit (P);
4284 Set_Is_Visible_Child_Unit (P);
4285 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4288 -- Place entity on visibility structure
4290 Set_Homonym (P, Current_Entity (P));
4291 Set_Current_Entity (P);
4293 if Debug_Flag_I then
4294 Write_Str (" (homonym) chain ");
4295 Write_Name (Chars (P));
4299 -- Install the incomplete view. The first element of the limited
4300 -- view is a header (an E_Package entity) used to reference the
4301 -- first shadow entity in the private part of the package.
4303 Lim_Header := Limited_View (P);
4304 Lim_Typ := First_Entity (Lim_Header);
4306 while Present (Lim_Typ)
4307 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4309 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4310 Set_Current_Entity (Lim_Typ);
4312 if Debug_Flag_I then
4313 Write_Str (" (homonym) chain ");
4314 Write_Name (Chars (Lim_Typ));
4318 Next_Entity (Lim_Typ);
4322 -- If the unit appears in a previous regular with_clause, the regular
4323 -- entities of the public part of the withed package must be replaced
4324 -- by the shadow ones.
4326 -- This code must be kept synchronized with the code that replaces the
4327 -- the shadow entities by the real entities (see body of Remove_Limited
4328 -- With_Clause); otherwise the contents of the homonym chains are not
4332 -- Hide all the type entities of the public part of the package to
4333 -- avoid its usage. This is needed to cover all the subtype decla-
4334 -- rations because we do not remove them from the homonym chain.
4336 E := First_Entity (P);
4337 while Present (E) and then E /= First_Private_Entity (P) loop
4339 Set_Was_Hidden (E, Is_Hidden (E));
4346 -- Replace the real entities by the shadow entities of the limited
4347 -- view. The first element of the limited view is a header that is
4348 -- used to reference the first shadow entity in the private part
4351 Lim_Header := Limited_View (P);
4353 Lim_Typ := First_Entity (Lim_Header);
4354 while Present (Lim_Typ)
4355 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4357 pragma Assert (not In_Chain (Lim_Typ));
4359 -- Do not unchain nested packages and child units
4361 if Ekind (Lim_Typ) /= E_Package
4362 and then not Is_Child_Unit (Lim_Typ)
4368 Prev := Current_Entity (Lim_Typ);
4370 -- Handle incomplete types
4372 if Ekind (Prev) = E_Incomplete_Type then
4373 E := Full_View (Prev);
4378 -- Replace E in the homonyms list
4380 if E = Non_Limited_View (Lim_Typ) then
4381 Set_Homonym (Lim_Typ, Homonym (Prev));
4382 Set_Current_Entity (Lim_Typ);
4386 E := Homonym (Prev);
4387 pragma Assert (Present (E));
4389 -- Handle incomplete types
4391 if Ekind (E) = E_Incomplete_Type then
4395 exit when E = Non_Limited_View (Lim_Typ);
4397 Prev := Homonym (Prev);
4400 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4401 Set_Homonym (Prev, Lim_Typ);
4405 if Debug_Flag_I then
4406 Write_Str (" (homonym) chain ");
4407 Write_Name (Chars (Lim_Typ));
4412 Next_Entity (Lim_Typ);
4416 -- The package must be visible while the limited-with clause is active
4417 -- because references to the type P.T must resolve in the usual way.
4418 -- In addition, we remember that the limited-view has been installed to
4419 -- uninstall it at the point of context removal.
4421 Set_Is_Immediately_Visible (P);
4422 Set_Limited_View_Installed (N);
4424 -- If the package in the limited_with clause is a child unit, the
4425 -- clause is unanalyzed and appears as a selected component. Recast
4426 -- it as an expanded name so that the entity can be properly set. Use
4427 -- entity of parent, if available, for higher ancestors in the name.
4429 if Nkind (Name (N)) = N_Selected_Component then
4437 while Nkind (Nam) = N_Selected_Component
4438 and then Present (Ent)
4440 Change_Selected_Component_To_Expanded_Name (Nam);
4442 -- Set entity of parent identifiers if the unit is a child
4443 -- unit. This ensures that the tree is properly formed from
4444 -- semantic point of view (e.g. for ASIS queries).
4446 Set_Entity (Nam, Ent);
4448 Nam := Prefix (Nam);
4451 -- Set entity of last ancestor
4453 if Nkind (Nam) = N_Identifier then
4454 Set_Entity (Nam, Ent);
4460 Set_Entity (Name (N), P);
4461 Set_From_With_Type (P);
4462 end Install_Limited_Withed_Unit;
4464 -------------------------
4465 -- Install_Withed_Unit --
4466 -------------------------
4468 procedure Install_Withed_Unit
4469 (With_Clause : Node_Id;
4470 Private_With_OK : Boolean := False)
4472 Uname : constant Entity_Id := Entity (Name (With_Clause));
4473 P : constant Entity_Id := Scope (Uname);
4476 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
4477 -- compiling a package declaration and the Private_With_OK flag was not
4478 -- set by the caller. These declarations will be installed later (before
4479 -- analyzing the private part of the package).
4481 if Private_Present (With_Clause)
4482 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
4483 and then not (Private_With_OK)
4488 if Debug_Flag_I then
4489 if Private_Present (With_Clause) then
4490 Write_Str ("install private withed unit ");
4492 Write_Str ("install withed unit ");
4495 Write_Name (Chars (Uname));
4499 -- We do not apply the restrictions to an internal unit unless
4500 -- we are compiling the internal unit as a main unit. This check
4501 -- is also skipped for dummy units (for missing packages).
4503 if Sloc (Uname) /= No_Location
4504 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
4505 or else Current_Sem_Unit = Main_Unit)
4507 Check_Restricted_Unit
4508 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
4511 if P /= Standard_Standard then
4513 -- If the unit is not analyzed after analysis of the with clause and
4514 -- it is an instantiation then it awaits a body and is the main unit.
4515 -- Its appearance in the context of some other unit indicates a
4516 -- circular dependency (DEC suite perversity).
4518 if not Analyzed (Uname)
4519 and then Nkind (Parent (Uname)) = N_Package_Instantiation
4522 ("instantiation depends on itself", Name (With_Clause));
4524 elsif not Is_Visible_Child_Unit (Uname) then
4525 Set_Is_Visible_Child_Unit (Uname);
4527 -- If the child unit appears in the context of its parent, it is
4528 -- immediately visible.
4530 if In_Open_Scopes (Scope (Uname)) then
4531 Set_Is_Immediately_Visible (Uname);
4534 if Is_Generic_Instance (Uname)
4535 and then Ekind (Uname) in Subprogram_Kind
4537 -- Set flag as well on the visible entity that denotes the
4538 -- instance, which renames the current one.
4540 Set_Is_Visible_Child_Unit
4542 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
4545 -- The parent unit may have been installed already, and may have
4546 -- appeared in a use clause.
4548 if In_Use (Scope (Uname)) then
4549 Set_Is_Potentially_Use_Visible (Uname);
4552 Set_Context_Installed (With_Clause);
4555 elsif not Is_Immediately_Visible (Uname) then
4556 if not Private_Present (With_Clause)
4557 or else Private_With_OK
4559 Set_Is_Immediately_Visible (Uname);
4562 Set_Context_Installed (With_Clause);
4565 -- A with-clause overrides a with-type clause: there are no restric-
4566 -- tions on the use of package entities.
4568 if Ekind (Uname) = E_Package then
4569 Set_From_With_Type (Uname, False);
4572 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
4573 -- unit if there is a visible homograph for it declared in the same
4574 -- declarative region. This pathological case can only arise when an
4575 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
4576 -- G1 has a generic child also named G2, and the context includes with_
4577 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
4578 -- of I1.G2 visible as well. If the child unit is named Standard, do
4579 -- not apply the check to the Standard package itself.
4581 if Is_Child_Unit (Uname)
4582 and then Is_Visible_Child_Unit (Uname)
4583 and then Ada_Version >= Ada_05
4586 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
4592 U2 := Homonym (Uname);
4594 and U2 /= Standard_Standard
4597 Decl2 := Unit_Declaration_Node (P2);
4599 if Is_Child_Unit (U2)
4600 and then Is_Visible_Child_Unit (U2)
4602 if Is_Generic_Instance (P)
4603 and then Nkind (Decl1) = N_Package_Declaration
4604 and then Generic_Parent (Specification (Decl1)) = P2
4606 Error_Msg_N ("illegal with_clause", With_Clause);
4608 ("\child unit has visible homograph" &
4609 " ('R'M 8.3(26), 10.1.1(19))",
4613 elsif Is_Generic_Instance (P2)
4614 and then Nkind (Decl2) = N_Package_Declaration
4615 and then Generic_Parent (Specification (Decl2)) = P
4617 -- With_clause for child unit of instance appears before
4618 -- in the context. We want to place the error message on
4619 -- it, not on the generic child unit itself.
4622 Prev_Clause : Node_Id;
4625 Prev_Clause := First (List_Containing (With_Clause));
4626 while Entity (Name (Prev_Clause)) /= U2 loop
4630 pragma Assert (Present (Prev_Clause));
4631 Error_Msg_N ("illegal with_clause", Prev_Clause);
4633 ("\child unit has visible homograph" &
4634 " ('R'M 8.3(26), 10.1.1(19))",
4645 end Install_Withed_Unit;
4651 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
4652 K : constant Node_Kind := Nkind (Lib_Unit);
4655 return (K in N_Generic_Declaration or else
4656 K in N_Generic_Instantiation or else
4657 K in N_Generic_Renaming_Declaration or else
4658 K = N_Package_Declaration or else
4659 K = N_Package_Renaming_Declaration or else
4660 K = N_Subprogram_Declaration or else
4661 K = N_Subprogram_Renaming_Declaration)
4662 and then Present (Parent_Spec (Lib_Unit));
4665 -----------------------
4666 -- Load_Needed_Body --
4667 -----------------------
4669 -- N is a generic unit named in a with clause, or else it is
4670 -- a unit that contains a generic unit or an inlined function.
4671 -- In order to perform an instantiation, the body of the unit
4672 -- must be present. If the unit itself is generic, we assume
4673 -- that an instantiation follows, and load and analyze the body
4674 -- unconditionally. This forces analysis of the spec as well.
4676 -- If the unit is not generic, but contains a generic unit, it
4677 -- is loaded on demand, at the point of instantiation (see ch12).
4679 procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
4680 Body_Name : Unit_Name_Type;
4681 Unum : Unit_Number_Type;
4683 Save_Style_Check : constant Boolean := Opt.Style_Check;
4684 -- The loading and analysis is done with style checks off
4687 if not GNAT_Mode then
4688 Style_Check := False;
4691 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
4694 (Load_Name => Body_Name,
4700 if Unum = No_Unit then
4704 Compiler_State := Analyzing; -- reset after load
4706 if not Fatal_Error (Unum) or else Try_Semantics then
4707 if Debug_Flag_L then
4708 Write_Str ("*** Loaded generic body");
4712 Semantics (Cunit (Unum));
4718 Style_Check := Save_Style_Check;
4719 end Load_Needed_Body;
4721 -------------------------
4722 -- Build_Limited_Views --
4723 -------------------------
4725 procedure Build_Limited_Views (N : Node_Id) is
4726 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
4727 P : constant Entity_Id := Cunit_Entity (Unum);
4729 Spec : Node_Id; -- To denote a package specification
4730 Lim_Typ : Entity_Id; -- To denote shadow entities
4731 Comp_Typ : Entity_Id; -- To denote real entities
4733 Lim_Header : Entity_Id; -- Package entity
4734 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
4735 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
4737 procedure Decorate_Incomplete_Type
4740 -- Add attributes of an incomplete type to a shadow entity. The same
4741 -- attributes are placed on the real entity, so that gigi receives
4742 -- a consistent view.
4744 procedure Decorate_Package_Specification (P : Entity_Id);
4745 -- Add attributes of a package entity to the entity in a package
4748 procedure Decorate_Tagged_Type
4752 -- Set basic attributes of tagged type T, including its class_wide type.
4753 -- The parameters Loc, Scope are used to decorate the class_wide type.
4755 procedure Build_Chain
4757 First_Decl : Node_Id);
4758 -- Construct list of shadow entities and attach it to entity of
4759 -- package that is mentioned in a limited_with clause.
4761 function New_Internal_Shadow_Entity
4762 (Kind : Entity_Kind;
4763 Sloc_Value : Source_Ptr;
4764 Id_Char : Character) return Entity_Id;
4765 -- Build a new internal entity and append it to the list of shadow
4766 -- entities available through the limited-header
4768 ------------------------------
4769 -- Decorate_Incomplete_Type --
4770 ------------------------------
4772 procedure Decorate_Incomplete_Type
4777 Set_Ekind (E, E_Incomplete_Type);
4778 Set_Scope (E, Scop);
4780 Set_Is_First_Subtype (E, True);
4781 Set_Stored_Constraint (E, No_Elist);
4782 Set_Full_View (E, Empty);
4783 Init_Size_Align (E);
4784 end Decorate_Incomplete_Type;
4786 --------------------------
4787 -- Decorate_Tagged_Type --
4788 --------------------------
4790 procedure Decorate_Tagged_Type
4798 Decorate_Incomplete_Type (T, Scop);
4799 Set_Is_Tagged_Type (T);
4801 -- Build corresponding class_wide type, if not previously done
4803 if No (Class_Wide_Type (T)) then
4804 CW := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
4806 Set_Ekind (CW, E_Class_Wide_Type);
4808 Set_Scope (CW, Scop);
4809 Set_Is_Tagged_Type (CW);
4810 Set_Is_First_Subtype (CW, True);
4811 Init_Size_Align (CW);
4812 Set_Has_Unknown_Discriminants (CW, True);
4813 Set_Class_Wide_Type (CW, CW);
4814 Set_Equivalent_Type (CW, Empty);
4815 Set_From_With_Type (CW, From_With_Type (T));
4817 Set_Class_Wide_Type (T, CW);
4819 end Decorate_Tagged_Type;
4821 ------------------------------------
4822 -- Decorate_Package_Specification --
4823 ------------------------------------
4825 procedure Decorate_Package_Specification (P : Entity_Id) is
4827 -- Place only the most basic attributes
4829 Set_Ekind (P, E_Package);
4830 Set_Etype (P, Standard_Void_Type);
4831 end Decorate_Package_Specification;
4833 --------------------------------
4834 -- New_Internal_Shadow_Entity --
4835 --------------------------------
4837 function New_Internal_Shadow_Entity
4838 (Kind : Entity_Kind;
4839 Sloc_Value : Source_Ptr;
4840 Id_Char : Character) return Entity_Id
4842 E : constant Entity_Id :=
4843 Make_Defining_Identifier (Sloc_Value,
4844 Chars => New_Internal_Name (Id_Char));
4847 Set_Ekind (E, Kind);
4848 Set_Is_Internal (E, True);
4850 if Kind in Type_Kind then
4851 Init_Size_Align (E);
4854 Append_Entity (E, Lim_Header);
4857 end New_Internal_Shadow_Entity;
4863 procedure Build_Chain
4865 First_Decl : Node_Id)
4867 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
4868 Is_Tagged : Boolean;
4873 while Present (Decl) loop
4875 -- For each library_package_declaration in the environment, there
4876 -- is an implicit declaration of a *limited view* of that library
4877 -- package. The limited view of a package contains:
4879 -- * For each nested package_declaration, a declaration of the
4880 -- limited view of that package, with the same defining-
4881 -- program-unit name.
4883 -- * For each type_declaration in the visible part, an incomplete
4884 -- type-declaration with the same defining_identifier, whose
4885 -- completion is the type_declaration. If the type_declaration
4886 -- is tagged, then the incomplete_type_declaration is tagged
4888 -- The partial view is tagged if the declaration has the
4889 -- explicit keyword, or else if it is a type extension, both
4890 -- of which can be ascertained syntactically.
4892 if Nkind (Decl) = N_Full_Type_Declaration then
4894 (Nkind (Type_Definition (Decl)) = N_Record_Definition
4895 and then Tagged_Present (Type_Definition (Decl)))
4897 (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
4900 (Record_Extension_Part (Type_Definition (Decl))));
4902 Comp_Typ := Defining_Identifier (Decl);
4904 if not Analyzed_Unit then
4906 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
4908 Decorate_Incomplete_Type (Comp_Typ, Scope);
4912 -- Create shadow entity for type
4914 Lim_Typ := New_Internal_Shadow_Entity
4915 (Kind => Ekind (Comp_Typ),
4916 Sloc_Value => Sloc (Comp_Typ),
4919 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4920 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4921 Set_From_With_Type (Lim_Typ);
4924 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
4926 Decorate_Incomplete_Type (Lim_Typ, Scope);
4929 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
4931 elsif Nkind (Decl) = N_Private_Type_Declaration then
4932 Comp_Typ := Defining_Identifier (Decl);
4934 if not Analyzed_Unit then
4935 if Tagged_Present (Decl) then
4936 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
4938 Decorate_Incomplete_Type (Comp_Typ, Scope);
4942 Lim_Typ := New_Internal_Shadow_Entity
4943 (Kind => Ekind (Comp_Typ),
4944 Sloc_Value => Sloc (Comp_Typ),
4947 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4948 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4949 Set_From_With_Type (Lim_Typ);
4951 if Tagged_Present (Decl) then
4952 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
4954 Decorate_Incomplete_Type (Lim_Typ, Scope);
4957 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
4959 elsif Nkind (Decl) = N_Private_Extension_Declaration then
4960 Comp_Typ := Defining_Identifier (Decl);
4962 if not Analyzed_Unit then
4963 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
4966 -- Create shadow entity for type
4968 Lim_Typ := New_Internal_Shadow_Entity
4969 (Kind => Ekind (Comp_Typ),
4970 Sloc_Value => Sloc (Comp_Typ),
4973 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4974 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4975 Set_From_With_Type (Lim_Typ);
4977 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
4978 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
4980 elsif Nkind (Decl) = N_Package_Declaration then
4985 Spec : constant Node_Id := Specification (Decl);
4988 Comp_Typ := Defining_Unit_Name (Spec);
4990 if not Analyzed (Cunit (Unum)) then
4991 Decorate_Package_Specification (Comp_Typ);
4992 Set_Scope (Comp_Typ, Scope);
4995 Lim_Typ := New_Internal_Shadow_Entity
4996 (Kind => Ekind (Comp_Typ),
4997 Sloc_Value => Sloc (Comp_Typ),
5000 Decorate_Package_Specification (Lim_Typ);
5001 Set_Scope (Lim_Typ, Scope);
5003 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5004 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5005 Set_From_With_Type (Lim_Typ);
5007 -- Note: The non_limited_view attribute is not used
5008 -- for local packages.
5012 First_Decl => First (Visible_Declarations (Spec)));
5020 -- Start of processing for Build_Limited_Views
5023 pragma Assert (Limited_Present (N));
5025 -- A library_item mentioned in a limited_with_clause shall be
5026 -- a package_declaration, not a subprogram_declaration,
5027 -- generic_declaration, generic_instantiation, or
5028 -- package_renaming_declaration
5030 case Nkind (Unit (Library_Unit (N))) is
5032 when N_Package_Declaration =>
5035 when N_Subprogram_Declaration =>
5036 Error_Msg_N ("subprograms not allowed in "
5037 & "limited with_clauses", N);
5040 when N_Generic_Package_Declaration |
5041 N_Generic_Subprogram_Declaration =>
5042 Error_Msg_N ("generics not allowed in "
5043 & "limited with_clauses", N);
5046 when N_Generic_Instantiation =>
5047 Error_Msg_N ("generic instantiations not allowed in "
5048 & "limited with_clauses", N);
5051 when N_Generic_Renaming_Declaration =>
5052 Error_Msg_N ("generic renamings not allowed in "
5053 & "limited with_clauses", N);
5056 when N_Subprogram_Renaming_Declaration =>
5057 Error_Msg_N ("renamed subprograms not allowed in "
5058 & "limited with_clauses", N);
5061 when N_Package_Renaming_Declaration =>
5062 Error_Msg_N ("renamed packages not allowed in "
5063 & "limited with_clauses", N);
5067 raise Program_Error;
5070 -- Check if the chain is already built
5072 Spec := Specification (Unit (Library_Unit (N)));
5074 if Limited_View_Installed (Spec) then
5078 Set_Ekind (P, E_Package);
5080 -- Build the header of the limited_view
5082 Lim_Header := Make_Defining_Identifier (Sloc (N),
5083 Chars => New_Internal_Name (Id_Char => 'Z'));
5084 Set_Ekind (Lim_Header, E_Package);
5085 Set_Is_Internal (Lim_Header);
5086 Set_Limited_View (P, Lim_Header);
5088 -- Create the auxiliary chain. All the shadow entities are appended
5089 -- to the list of entities of the limited-view header
5093 First_Decl => First (Visible_Declarations (Spec)));
5095 -- Save the last built shadow entity. It is needed later to set the
5096 -- reference to the first shadow entity in the private part
5098 Last_Pub_Lim_E := Last_Lim_E;
5100 -- Ada 2005 (AI-262): Add the limited view of the private declarations
5101 -- Required to give support to limited-private-with clauses
5103 Build_Chain (Scope => P,
5104 First_Decl => First (Private_Declarations (Spec)));
5106 if Last_Pub_Lim_E /= Empty then
5107 Set_First_Private_Entity (Lim_Header,
5108 Next_Entity (Last_Pub_Lim_E));
5110 Set_First_Private_Entity (Lim_Header,
5114 Set_Limited_View_Installed (Spec);
5115 end Build_Limited_Views;
5117 -------------------------------
5118 -- Check_Body_Needed_For_SAL --
5119 -------------------------------
5121 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5123 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5124 -- Determine whether use of entity E might require the presence
5125 -- of its body. For a package this requires a recursive traversal
5126 -- of all nested declarations.
5128 ---------------------------
5129 -- Entity_Needed_For_SAL --
5130 ---------------------------
5132 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5136 if Is_Subprogram (E)
5137 and then Has_Pragma_Inline (E)
5141 elsif Ekind (E) = E_Generic_Function
5142 or else Ekind (E) = E_Generic_Procedure
5146 elsif Ekind (E) = E_Generic_Package
5148 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5149 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5153 elsif Ekind (E) = E_Package
5155 Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5156 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5158 Ent := First_Entity (E);
5159 while Present (Ent) loop
5160 if Entity_Needs_Body (Ent) then
5172 end Entity_Needs_Body;
5174 -- Start of processing for Check_Body_Needed_For_SAL
5177 if Ekind (Unit_Name) = E_Generic_Package
5179 Nkind (Unit_Declaration_Node (Unit_Name)) =
5180 N_Generic_Package_Declaration
5182 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5184 Set_Body_Needed_For_SAL (Unit_Name);
5186 elsif Ekind (Unit_Name) = E_Generic_Procedure
5187 or else Ekind (Unit_Name) = E_Generic_Function
5189 Set_Body_Needed_For_SAL (Unit_Name);
5191 elsif Is_Subprogram (Unit_Name)
5192 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5193 N_Subprogram_Declaration
5194 and then Has_Pragma_Inline (Unit_Name)
5196 Set_Body_Needed_For_SAL (Unit_Name);
5198 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5199 Check_Body_Needed_For_SAL
5200 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5202 elsif Ekind (Unit_Name) = E_Package
5203 and then Entity_Needs_Body (Unit_Name)
5205 Set_Body_Needed_For_SAL (Unit_Name);
5207 elsif Ekind (Unit_Name) = E_Package_Body
5208 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5210 Check_Body_Needed_For_SAL
5211 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5213 end Check_Body_Needed_For_SAL;
5215 --------------------
5216 -- Remove_Context --
5217 --------------------
5219 procedure Remove_Context (N : Node_Id) is
5220 Lib_Unit : constant Node_Id := Unit (N);
5223 -- If this is a child unit, first remove the parent units
5225 if Is_Child_Spec (Lib_Unit) then
5226 Remove_Parents (Lib_Unit);
5229 Remove_Context_Clauses (N);
5232 ----------------------------
5233 -- Remove_Context_Clauses --
5234 ----------------------------
5236 procedure Remove_Context_Clauses (N : Node_Id) is
5238 Unit_Name : Entity_Id;
5241 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5242 -- limited-views first and regular-views later (to maintain the
5245 -- First Phase: Remove limited_with context clauses
5247 Item := First (Context_Items (N));
5248 while Present (Item) loop
5250 -- We are interested only in with clauses which got installed
5253 if Nkind (Item) = N_With_Clause
5254 and then Limited_Present (Item)
5255 and then Limited_View_Installed (Item)
5257 Remove_Limited_With_Clause (Item);
5263 -- Second Phase: Loop through context items and undo regular
5264 -- with_clauses and use_clauses.
5266 Item := First (Context_Items (N));
5267 while Present (Item) loop
5269 -- We are interested only in with clauses which got installed
5270 -- on entry, as indicated by their Context_Installed flag set
5272 if Nkind (Item) = N_With_Clause
5273 and then Limited_Present (Item)
5274 and then Limited_View_Installed (Item)
5278 elsif Nkind (Item) = N_With_Clause
5279 and then Context_Installed (Item)
5281 -- Remove items from one with'ed unit
5283 Unit_Name := Entity (Name (Item));
5284 Remove_Unit_From_Visibility (Unit_Name);
5285 Set_Context_Installed (Item, False);
5287 elsif Nkind (Item) = N_Use_Package_Clause then
5288 End_Use_Package (Item);
5290 elsif Nkind (Item) = N_Use_Type_Clause then
5291 End_Use_Type (Item);
5293 elsif Nkind (Item) = N_With_Type_Clause then
5294 Remove_With_Type_Clause (Name (Item));
5299 end Remove_Context_Clauses;
5301 --------------------------------
5302 -- Remove_Limited_With_Clause --
5303 --------------------------------
5305 procedure Remove_Limited_With_Clause (N : Node_Id) is
5306 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5309 Lim_Header : Entity_Id;
5310 Lim_Typ : Entity_Id;
5314 pragma Assert (Limited_View_Installed (N));
5316 -- In case of limited with_clause on subprograms, generics, instances,
5317 -- or renamings, the corresponding error was previously posted and we
5318 -- have nothing to do here.
5320 if Nkind (P_Unit) /= N_Package_Declaration then
5324 P := Defining_Unit_Name (Specification (P_Unit));
5326 -- Handle child packages
5328 if Nkind (P) = N_Defining_Program_Unit_Name then
5329 P := Defining_Identifier (P);
5332 if Debug_Flag_I then
5333 Write_Str ("remove limited view of ");
5334 Write_Name (Chars (P));
5335 Write_Str (" from visibility");
5339 -- Prepare the removal of the shadow entities from visibility. The
5340 -- first element of the limited view is a header (an E_Package
5341 -- entity) that is used to reference the first shadow entity in the
5342 -- private part of the package
5344 Lim_Header := Limited_View (P);
5345 Lim_Typ := First_Entity (Lim_Header);
5347 -- Remove package and shadow entities from visibility if it has not
5350 if not Analyzed (P_Unit) then
5352 Set_Is_Immediately_Visible (P, False);
5354 while Present (Lim_Typ) loop
5356 Next_Entity (Lim_Typ);
5359 -- Otherwise this package has already appeared in the closure and its
5360 -- shadow entities must be replaced by its real entities. This code
5361 -- must be kept synchronized with the complementary code in Install
5362 -- Limited_Withed_Unit.
5365 -- Real entities that are type or subtype declarations were hidden
5366 -- from visibility at the point of installation of the limited-view.
5367 -- Now we recover the previous value of the hidden attribute.
5369 E := First_Entity (P);
5370 while Present (E) and then E /= First_Private_Entity (P) loop
5372 Set_Is_Hidden (E, Was_Hidden (E));
5378 while Present (Lim_Typ)
5379 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5381 -- Nested packages and child units were not unchained
5383 if Ekind (Lim_Typ) /= E_Package
5384 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5386 -- Handle incomplete types of the real view. For this purpose
5387 -- we traverse the list of visible entities to look for an
5388 -- incomplete type in the real-view associated with Lim_Typ.
5390 E := First_Entity (P);
5391 while Present (E) and then E /= First_Private_Entity (P) loop
5392 exit when Ekind (E) = E_Incomplete_Type
5393 and then Present (Full_View (E))
5394 and then Full_View (E) = Lim_Typ;
5399 -- If the previous search was not sucessful then the entity
5400 -- to be restored in the homonym list is the non-limited view
5402 if E = First_Private_Entity (P) then
5403 E := Non_Limited_View (Lim_Typ);
5406 pragma Assert (not In_Chain (E));
5408 Prev := Current_Entity (Lim_Typ);
5410 if Prev = Lim_Typ then
5411 Set_Current_Entity (E);
5414 while Present (Prev)
5415 and then Homonym (Prev) /= Lim_Typ
5417 Prev := Homonym (Prev);
5419 pragma Assert (Present (Prev));
5421 Set_Homonym (Prev, E);
5424 -- We must also set the next homonym entity of the real entity
5425 -- to handle the case in which the next homonym was a shadow
5428 Set_Homonym (E, Homonym (Lim_Typ));
5431 Next_Entity (Lim_Typ);
5435 -- Indicate that the limited view of the package is not installed
5437 Set_From_With_Type (P, False);
5438 Set_Limited_View_Installed (N, False);
5439 end Remove_Limited_With_Clause;
5441 --------------------
5442 -- Remove_Parents --
5443 --------------------
5445 procedure Remove_Parents (Lib_Unit : Node_Id) is
5448 P_Spec : Node_Id := Empty;
5450 Vis : constant Boolean :=
5451 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
5454 if Is_Child_Spec (Lib_Unit) then
5455 P_Spec := Parent_Spec (Lib_Unit);
5457 elsif Nkind (Lib_Unit) = N_Package_Body
5458 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
5460 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
5463 if Present (P_Spec) then
5466 P_Name := Get_Parent_Entity (P);
5467 Remove_Context_Clauses (P_Spec);
5468 End_Package_Scope (P_Name);
5469 Set_Is_Immediately_Visible (P_Name, Vis);
5471 -- Remove from visibility the siblings as well, which are directly
5472 -- visible while the parent is in scope.
5474 E := First_Entity (P_Name);
5475 while Present (E) loop
5476 if Is_Child_Unit (E) then
5477 Set_Is_Immediately_Visible (E, False);
5483 Set_In_Package_Body (P_Name, False);
5485 -- This is the recursive call to remove the context of any
5486 -- higher level parent. This recursion ensures that all parents
5487 -- are removed in the reverse order of their installation.
5493 ---------------------------------
5494 -- Remove_Private_With_Clauses --
5495 ---------------------------------
5497 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
5501 Item := First (Context_Items (Comp_Unit));
5502 while Present (Item) loop
5503 if Nkind (Item) = N_With_Clause
5504 and then Private_Present (Item)
5506 if Limited_Present (Item) then
5507 if not Limited_View_Installed (Item) then
5508 Remove_Limited_With_Clause (Item);
5511 Remove_Unit_From_Visibility (Entity (Name (Item)));
5512 Set_Context_Installed (Item, False);
5518 end Remove_Private_With_Clauses;
5520 -----------------------------
5521 -- Remove_With_Type_Clause --
5522 -----------------------------
5524 procedure Remove_With_Type_Clause (Name : Node_Id) is
5528 procedure Unchain (E : Entity_Id);
5529 -- Remove entity from visibility list
5535 procedure Unchain (E : Entity_Id) is
5539 Prev := Current_Entity (E);
5541 -- Package entity may appear is several with_type_clauses, and
5542 -- may have been removed already.
5548 Set_Name_Entity_Id (Chars (E), Homonym (E));
5551 while Present (Prev)
5552 and then Homonym (Prev) /= E
5554 Prev := Homonym (Prev);
5557 if Present (Prev) then
5558 Set_Homonym (Prev, Homonym (E));
5563 -- Start of processing for Remove_With_Type_Clause
5566 if Nkind (Name) = N_Selected_Component then
5567 Typ := Entity (Selector_Name (Name));
5569 -- If no Typ, then error in declaration, ignore
5580 -- If the exporting package has been analyzed, it has appeared in the
5581 -- context already and should be left alone. Otherwise, remove from
5584 if not Analyzed (Unit_Declaration_Node (P)) then
5587 Set_Is_Frozen (Typ, False);
5590 if Ekind (Typ) = E_Record_Type then
5591 Set_From_With_Type (Class_Wide_Type (Typ), False);
5592 Set_From_With_Type (Typ, False);
5595 Set_From_With_Type (P, False);
5597 -- If P is a child unit, remove parents as well
5601 and then P /= Standard_Standard
5603 Set_From_With_Type (P, False);
5605 if not Analyzed (Unit_Declaration_Node (P)) then
5612 -- The back-end needs to know that an access type is imported, so it
5613 -- does not need elaboration and can appear in a mutually recursive
5614 -- record definition, so the imported flag on an access type is
5617 end Remove_With_Type_Clause;
5619 ---------------------------------
5620 -- Remove_Unit_From_Visibility --
5621 ---------------------------------
5623 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
5624 P : constant Entity_Id := Scope (Unit_Name);
5628 if Debug_Flag_I then
5629 Write_Str ("remove unit ");
5630 Write_Name (Chars (Unit_Name));
5631 Write_Str (" from visibility");
5635 if P /= Standard_Standard then
5636 Set_Is_Visible_Child_Unit (Unit_Name, False);
5639 Set_Is_Potentially_Use_Visible (Unit_Name, False);
5640 Set_Is_Immediately_Visible (Unit_Name, False);
5642 end Remove_Unit_From_Visibility;
5648 procedure Unchain (E : Entity_Id) is
5652 Prev := Current_Entity (E);
5658 Set_Name_Entity_Id (Chars (E), Homonym (E));
5661 while Present (Prev)
5662 and then Homonym (Prev) /= E
5664 Prev := Homonym (Prev);
5667 if Present (Prev) then
5668 Set_Homonym (Prev, Homonym (E));
5672 if Debug_Flag_I then
5673 Write_Str (" (homonym) unchain ");
5674 Write_Name (Chars (E));