1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Util; use Exp_Util;
31 with Elists; use Elists;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Inline; use Inline;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch7; use Sem_Ch7;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Dist; use Sem_Dist;
54 with Sem_Prag; use Sem_Prag;
55 with Sem_Util; use Sem_Util;
56 with Sem_Warn; use Sem_Warn;
57 with Stand; use Stand;
58 with Sinfo; use Sinfo;
59 with Sinfo.CN; use Sinfo.CN;
60 with Sinput; use Sinput;
61 with Snames; use Snames;
62 with Style; use Style;
63 with Stylesw; use Stylesw;
64 with Tbuild; use Tbuild;
65 with Uname; use Uname;
67 package body Sem_Ch10 is
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 procedure Analyze_Context (N : Node_Id);
74 -- Analyzes items in the context clause of compilation unit
76 procedure Build_Limited_Views (N : Node_Id);
77 -- Build and decorate the list of shadow entities for a package mentioned
78 -- in a limited_with clause. If the package was not previously analyzed
79 -- then it also performs a basic decoration of the real entities; this
80 -- is required to do not pass non-decorated entities to the back-end.
81 -- Implements Ada 2005 (AI-50217).
83 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
84 -- Check whether the source for the body of a compilation unit must
85 -- be included in a standalone library.
87 procedure Check_Private_Child_Unit (N : Node_Id);
88 -- If a with_clause mentions a private child unit, the compilation
89 -- unit must be a member of the same family, as described in 10.1.2.
91 procedure Check_Stub_Level (N : Node_Id);
92 -- Verify that a stub is declared immediately within a compilation unit,
93 -- and not in an inner frame.
95 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
96 -- When a child unit appears in a context clause, the implicit withs on
97 -- parents are made explicit, and with clauses are inserted in the context
98 -- clause before the one for the child. If a parent in the with_clause
99 -- is a renaming, the implicit with_clause is on the renaming whose name
100 -- is mentioned in the with_clause, and not on the package it renames.
101 -- N is the compilation unit whose list of context items receives the
102 -- implicit with_clauses.
104 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
105 -- Get defining entity of parent unit of a child unit. In most cases this
106 -- is the defining entity of the unit, but for a child instance whose
107 -- parent needs a body for inlining, the instantiation node of the parent
108 -- has not yet been rewritten as a package declaration, and the entity has
109 -- to be retrieved from the Instance_Spec of the unit.
111 function Has_With_Clause
114 Is_Limited : Boolean := False) return Boolean;
115 -- Determine whether compilation unit C_Unit contains a [limited] with
116 -- clause for package Pack. Use the flag Is_Limited to designate desired
119 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
120 -- If the main unit is a child unit, implicit withs are also added for
121 -- all its ancestors.
123 function In_Chain (E : Entity_Id) return Boolean;
124 -- Check that the shadow entity is not already in the homonym chain, for
125 -- example through a limited_with clause in a parent unit.
127 procedure Install_Context_Clauses (N : Node_Id);
128 -- Subsidiary to Install_Context and Install_Parents. Process only with_
129 -- and use_clauses for current unit and its library unit if any.
131 procedure Install_Limited_Context_Clauses (N : Node_Id);
132 -- Subsidiary to Install_Context. Process only limited with_clauses
133 -- for current unit. Implements Ada 2005 (AI-50217).
135 procedure Install_Limited_Withed_Unit (N : Node_Id);
136 -- Place shadow entities for a limited_with package in the visibility
137 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
139 procedure Install_Withed_Unit
140 (With_Clause : Node_Id;
141 Private_With_OK : Boolean := False);
142 -- If the unit is not a child unit, make unit immediately visible.
143 -- The caller ensures that the unit is not already currently installed.
144 -- The flag Private_With_OK is set true in Install_Private_With_Clauses,
145 -- which is called when compiling the private part of a package, or
146 -- installing the private declarations of a parent unit.
148 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
149 -- This procedure establishes the context for the compilation of a child
150 -- unit. If Lib_Unit is a child library spec then the context of the parent
151 -- is installed, and the parent itself made immediately visible, so that
152 -- the child unit is processed in the declarative region of the parent.
153 -- Install_Parents makes a recursive call to itself to ensure that all
154 -- parents are loaded in the nested case. If Lib_Unit is a library body,
155 -- the only effect of Install_Parents is to install the private decls of
156 -- the parents, because the visible parent declarations will have been
157 -- installed as part of the context of the corresponding spec.
159 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
160 -- In the compilation of a child unit, a child of any of the ancestor
161 -- units is directly visible if it is visible, because the parent is in
162 -- an enclosing scope. Iterate over context to find child units of U_Name
163 -- or of some ancestor of it.
165 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
166 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
167 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
168 -- a library spec that has a parent. If the call to Is_Child_Spec returns
169 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
170 -- compilation unit for the parent spec.
172 -- Lib_Unit can also be a subprogram body that acts as its own spec. If
173 -- the Parent_Spec is non-empty, this is also a child unit.
175 procedure Remove_Context_Clauses (N : Node_Id);
176 -- Subsidiary of previous one. Remove use_ and with_clauses
178 procedure Remove_Limited_With_Clause (N : Node_Id);
179 -- Remove from visibility the shadow entities introduced for a package
180 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
182 procedure Remove_Parents (Lib_Unit : Node_Id);
183 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
184 -- contexts established by the corresponding call to Install_Parents are
185 -- removed. Remove_Parents contains a recursive call to itself to ensure
186 -- that all parents are removed in the nested case.
188 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
189 -- Reset all visibility flags on unit after compiling it, either as a
190 -- main unit or as a unit in the context.
192 procedure Unchain (E : Entity_Id);
193 -- Remove single entity from visibility list
195 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
196 -- Common processing for all stubs (subprograms, tasks, packages, and
197 -- protected cases). N is the stub to be analyzed. Once the subunit
198 -- name is established, load and analyze. Nam is the non-overloadable
199 -- entity for which the proper body provides a completion. Subprogram
200 -- stubs are handled differently because they can be declarations.
203 -- A dummy procedure, for debugging use, called just before analyzing the
204 -- main unit (after dealing with any context clauses).
206 --------------------------
207 -- Limited_With_Clauses --
208 --------------------------
210 -- Limited_With clauses are the mechanism chosen for Ada05 to support
211 -- mutually recursive types declared in different units. A limited_with
212 -- clause that names package P in the context of unit U makes the types
213 -- declared in the visible part of P available within U, but with the
214 -- restriction that these types can only be used as incomplete types.
215 -- The limited_with clause does not impose a semantic dependence on P,
216 -- and it is possible for two packages to have limited_with_clauses on
217 -- each other without creating an elaboration circularity.
219 -- To support this feature, the analysis of a limited_with clause must
220 -- create an abbreviated view of the package, without performing any
221 -- semantic analysis on it. This "package abstract" contains shadow
222 -- types that are in one-one correspondence with the real types in the
223 -- package, and that have the properties of incomplete types.
225 -- The implementation creates two element lists: one to chain the shadow
226 -- entities, and one to chain the corresponding type entities in the tree
227 -- of the package. Links between corresponding entities in both chains
228 -- allow the compiler to select the proper view of a given type, depending
229 -- on the context. Note that in contrast with the handling of private
230 -- types, the limited view and the non-limited view of a type are treated
231 -- as separate entities, and no entity exchange needs to take place, which
232 -- makes the implementation must simpler than could be feared.
234 ------------------------------
235 -- Analyze_Compilation_Unit --
236 ------------------------------
238 procedure Analyze_Compilation_Unit (N : Node_Id) is
239 Unit_Node : constant Node_Id := Unit (N);
240 Lib_Unit : Node_Id := Library_Unit (N);
242 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
243 Par_Spec_Name : Unit_Name_Type;
244 Unum : Unit_Number_Type;
246 procedure Check_Redundant_Withs
247 (Context_Items : List_Id;
248 Spec_Context_Items : List_Id := No_List);
249 -- Determine whether the context list of a compilation unit contains
250 -- redundant with clauses. When checking body clauses against spec
251 -- clauses, set Context_Items to the context list of the body and
252 -- Spec_Context_Items to that of the spec. Parent packages are not
253 -- examined for documentation purposes.
255 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
256 -- Generate cross-reference information for the parents of child units.
257 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
259 ---------------------------
260 -- Check_Redundant_Withs --
261 ---------------------------
263 procedure Check_Redundant_Withs
264 (Context_Items : List_Id;
265 Spec_Context_Items : List_Id := No_List)
269 procedure Process_Body_Clauses
270 (Context_List : List_Id;
272 Used : in out Boolean;
273 Used_Type_Or_Elab : in out Boolean);
274 -- Examine the context clauses of a package body, trying to match
275 -- the name entity of Clause with any list element. If the match
276 -- occurs on a use package clause, set Used to True, for a use
277 -- type clause, pragma Elaborate or pragma Elaborate_All, set
278 -- Used_Type_Or_Elab to True.
280 procedure Process_Spec_Clauses
281 (Context_List : List_Id;
283 Used : in out Boolean;
284 Withed : in out Boolean;
285 Exit_On_Self : Boolean := False);
286 -- Examine the context clauses of a package spec, trying to match
287 -- the name entity of Clause with any list element. If the match
288 -- occurs on a use package clause, set Used to True, for a with
289 -- package clause other than Clause, set Withed to True. Limited
290 -- with clauses, implicitly generated with clauses and withs
291 -- having pragmas Elaborate or Elaborate_All applied to them are
292 -- skipped. Exit_On_Self is used to control the search loop and
293 -- force an exit whenever Clause sees itself in the search.
295 --------------------------
296 -- Process_Body_Clauses --
297 --------------------------
299 procedure Process_Body_Clauses
300 (Context_List : List_Id;
302 Used : in out Boolean;
303 Used_Type_Or_Elab : in out Boolean)
305 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
311 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
312 -- In an expanded name in a use clause, if the prefix is a
313 -- renamed package, the entity is set to the original package
314 -- as a result, when checking whether the package appears in a
315 -- previous with_clause, the renaming has to be taken into
316 -- account, to prevent spurious or incorrect warnings. The
317 -- common case is the use of Text_IO.
323 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
325 return Entity (N) = P
327 (Present (Renamed_Object (P))
328 and then Entity (N) = Renamed_Object (P));
331 -- Start of processing for Process_Body_Clauses
335 Used_Type_Or_Elab := False;
337 Cont_Item := First (Context_List);
338 while Present (Cont_Item) loop
340 -- Package use clause
342 if Nkind (Cont_Item) = N_Use_Package_Clause
345 -- Search through use clauses
347 Use_Item := First (Names (Cont_Item));
348 while Present (Use_Item) and then not Used loop
350 -- Case of a direct use of the one we are looking for
352 if Entity (Use_Item) = Nam_Ent then
355 -- Handle nested case, as in "with P; use P.Q.R"
362 -- Loop through prefixes looking for match
365 while Nkind (UE) = N_Expanded_Name loop
366 if Same_Unit (Prefix (UE), Nam_Ent) then
381 elsif Nkind (Cont_Item) = N_Use_Type_Clause
382 and then not Used_Type_Or_Elab
384 Subt_Mark := First (Subtype_Marks (Cont_Item));
385 while Present (Subt_Mark)
386 and then not Used_Type_Or_Elab
388 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
389 Used_Type_Or_Elab := True;
395 -- Pragma Elaborate or Elaborate_All
397 elsif Nkind (Cont_Item) = N_Pragma
399 (Pragma_Name (Cont_Item) = Name_Elaborate
401 Pragma_Name (Cont_Item) = Name_Elaborate_All)
402 and then not Used_Type_Or_Elab
405 First (Pragma_Argument_Associations (Cont_Item));
406 while Present (Prag_Unit)
407 and then not Used_Type_Or_Elab
409 if Entity (Expression (Prag_Unit)) = Nam_Ent then
410 Used_Type_Or_Elab := True;
419 end Process_Body_Clauses;
421 --------------------------
422 -- Process_Spec_Clauses --
423 --------------------------
425 procedure Process_Spec_Clauses
426 (Context_List : List_Id;
428 Used : in out Boolean;
429 Withed : in out Boolean;
430 Exit_On_Self : Boolean := False)
432 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
440 Cont_Item := First (Context_List);
441 while Present (Cont_Item) loop
443 -- Stop the search since the context items after Cont_Item
444 -- have already been examined in a previous iteration of
445 -- the reverse loop in Check_Redundant_Withs.
448 and Cont_Item = Clause
453 -- Package use clause
455 if Nkind (Cont_Item) = N_Use_Package_Clause
458 Use_Item := First (Names (Cont_Item));
459 while Present (Use_Item) and then not Used loop
460 if Entity (Use_Item) = Nam_Ent then
467 -- Package with clause. Avoid processing self, implicitly
468 -- generated with clauses or limited with clauses. Note
469 -- that we examine with clauses having pragmas Elaborate
470 -- or Elaborate_All applied to them due to cases such as:
474 -- pragma Elaborate (Pack);
476 -- In this case, the second with clause is redundant since
477 -- the pragma applies only to the first "with Pack;".
479 elsif Nkind (Cont_Item) = N_With_Clause
480 and then not Implicit_With (Cont_Item)
481 and then not Limited_Present (Cont_Item)
482 and then Cont_Item /= Clause
483 and then Entity (Name (Cont_Item)) = Nam_Ent
490 end Process_Spec_Clauses;
492 -- Start of processing for Check_Redundant_Withs
495 Clause := Last (Context_Items);
496 while Present (Clause) loop
498 -- Avoid checking implicitly generated with clauses, limited
499 -- with clauses or withs that have pragma Elaborate or
500 -- Elaborate_All applied.
502 if Nkind (Clause) = N_With_Clause
503 and then not Implicit_With (Clause)
504 and then not Limited_Present (Clause)
505 and then not Elaborate_Present (Clause)
507 -- Package body-to-spec check
509 if Present (Spec_Context_Items) then
511 Used_In_Body : Boolean := False;
512 Used_In_Spec : Boolean := False;
513 Used_Type_Or_Elab : Boolean := False;
514 Withed_In_Spec : Boolean := False;
518 (Context_List => Spec_Context_Items,
520 Used => Used_In_Spec,
521 Withed => Withed_In_Spec);
524 (Context_List => Context_Items,
526 Used => Used_In_Body,
527 Used_Type_Or_Elab => Used_Type_Or_Elab);
529 -- "Type Elab" refers to the presence of either a use
530 -- type clause, pragmas Elaborate or Elaborate_All.
532 -- +---------------+---------------------------+------+
533 -- | Spec | Body | Warn |
534 -- +--------+------+--------+------+-----------+------+
535 -- | Withed | Used | Withed | Used | Type Elab | |
536 -- | X | | X | | | X |
537 -- | X | | X | X | | |
538 -- | X | | X | | X | |
539 -- | X | | X | X | X | |
540 -- | X | X | X | | | X |
541 -- | X | X | X | | X | |
542 -- | X | X | X | X | | X |
543 -- | X | X | X | X | X | |
544 -- +--------+------+--------+------+-----------+------+
547 and then not Used_Type_Or_Elab)
550 and then not Used_In_Body)
554 Error_Msg_N ("?redundant with clause in body", Clause);
557 Used_In_Body := False;
558 Used_In_Spec := False;
559 Used_Type_Or_Elab := False;
560 Withed_In_Spec := False;
563 -- Standalone package spec or body check
567 Dont_Care : Boolean := False;
568 Withed : Boolean := False;
571 -- The mechanism for examining the context clauses of a
572 -- package spec can be applied to package body clauses.
575 (Context_List => Context_Items,
579 Exit_On_Self => True);
582 Error_Msg_N ("?redundant with clause", Clause);
590 end Check_Redundant_Withs;
592 --------------------------------
593 -- Generate_Parent_References --
594 --------------------------------
596 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
598 P_Name : Entity_Id := P_Id;
601 Pref := Name (Parent (Defining_Entity (N)));
603 if Nkind (Pref) = N_Expanded_Name then
605 -- Done already, if the unit has been compiled indirectly as
606 -- part of the closure of its context because of inlining.
611 while Nkind (Pref) = N_Selected_Component loop
612 Change_Selected_Component_To_Expanded_Name (Pref);
613 Set_Entity (Pref, P_Name);
614 Set_Etype (Pref, Etype (P_Name));
615 Generate_Reference (P_Name, Pref, 'r');
616 Pref := Prefix (Pref);
617 P_Name := Scope (P_Name);
620 -- The guard here on P_Name is to handle the error condition where
621 -- the parent unit is missing because the file was not found.
623 if Present (P_Name) then
624 Set_Entity (Pref, P_Name);
625 Set_Etype (Pref, Etype (P_Name));
626 Generate_Reference (P_Name, Pref, 'r');
627 Style.Check_Identifier (Pref, P_Name);
629 end Generate_Parent_References;
631 -- Start of processing for Analyze_Compilation_Unit
634 Process_Compilation_Unit_Pragmas (N);
636 -- If the unit is a subunit whose parent has not been analyzed (which
637 -- indicates that the main unit is a subunit, either the current one or
638 -- one of its descendents) then the subunit is compiled as part of the
639 -- analysis of the parent, which we proceed to do. Basically this gets
640 -- handled from the top down and we don't want to do anything at this
641 -- level (i.e. this subunit will be handled on the way down from the
642 -- parent), so at this level we immediately return. If the subunit
643 -- ends up not analyzed, it means that the parent did not contain a
644 -- stub for it, or that there errors were detected in some ancestor.
646 if Nkind (Unit_Node) = N_Subunit
647 and then not Analyzed (Lib_Unit)
649 Semantics (Lib_Unit);
651 if not Analyzed (Proper_Body (Unit_Node)) then
652 if Serious_Errors_Detected > 0 then
653 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
655 Error_Msg_N ("missing stub for subunit", N);
662 -- Analyze context (this will call Sem recursively for with'ed units)
666 -- If the unit is a package body, the spec is already loaded and must
667 -- be analyzed first, before we analyze the body.
669 if Nkind (Unit_Node) = N_Package_Body then
671 -- If no Lib_Unit, then there was a serious previous error, so
672 -- just ignore the entire analysis effort
674 if No (Lib_Unit) then
678 Semantics (Lib_Unit);
679 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
681 -- Verify that the library unit is a package declaration
683 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
684 N_Generic_Package_Declaration)
687 ("no legal package declaration for package body", N);
690 -- Otherwise, the entity in the declaration is visible. Update
691 -- the version to reflect dependence of this body on the spec.
694 Spec_Id := Defining_Entity (Unit (Lib_Unit));
695 Set_Is_Immediately_Visible (Spec_Id, True);
696 Version_Update (N, Lib_Unit);
698 if Nkind (Defining_Unit_Name (Unit_Node)) =
699 N_Defining_Program_Unit_Name
701 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
706 -- If the unit is a subprogram body, then we similarly need to analyze
707 -- its spec. However, things are a little simpler in this case, because
708 -- here, this analysis is done only for error checking and consistency
709 -- purposes, so there's nothing else to be done.
711 elsif Nkind (Unit_Node) = N_Subprogram_Body then
712 if Acts_As_Spec (N) then
714 -- If the subprogram body is a child unit, we must create a
715 -- declaration for it, in order to properly load the parent(s).
716 -- After this, the original unit does not acts as a spec, because
717 -- there is an explicit one. If this unit appears in a context
718 -- clause, then an implicit with on the parent will be added when
719 -- installing the context. If this is the main unit, there is no
720 -- Unit_Table entry for the declaration (it has the unit number
721 -- of the main unit) and code generation is unaffected.
723 Unum := Get_Cunit_Unit_Number (N);
724 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
726 if Par_Spec_Name /= No_Unit_Name then
729 (Load_Name => Par_Spec_Name,
734 if Unum /= No_Unit then
736 -- Build subprogram declaration and attach parent unit to it
737 -- This subprogram declaration does not come from source,
738 -- Nevertheless the backend must generate debugging info for
739 -- it, and this must be indicated explicitly. We also mark
740 -- the body entity as a child unit now, to prevent a
741 -- cascaded error if the spec entity cannot be entered
742 -- in its scope. Finally we create a Units table entry for
743 -- the subprogram declaration, to maintain a one-to-one
744 -- correspondence with compilation unit nodes. This is
745 -- critical for the tree traversals performed by Codepeer.
748 Loc : constant Source_Ptr := Sloc (N);
749 SCS : constant Boolean :=
750 Get_Comes_From_Source_Default;
753 Set_Comes_From_Source_Default (False);
755 Make_Compilation_Unit (Loc,
756 Context_Items => New_Copy_List (Context_Items (N)),
758 Make_Subprogram_Declaration (Sloc (N),
761 (Specification (Unit_Node))),
763 Make_Compilation_Unit_Aux (Loc));
765 Set_Library_Unit (N, Lib_Unit);
766 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
767 Make_Child_Decl_Unit (N);
768 Semantics (Lib_Unit);
770 -- Now that a separate declaration exists, the body
771 -- of the child unit does not act as spec any longer.
773 Set_Acts_As_Spec (N, False);
774 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
775 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
776 Set_Comes_From_Source_Default (SCS);
781 -- Here for subprogram with separate declaration
784 Semantics (Lib_Unit);
785 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
786 Version_Update (N, Lib_Unit);
789 -- If this is a child unit, generate references to the parents
791 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
792 N_Defining_Program_Unit_Name
794 Generate_Parent_References (
795 Specification (Unit_Node),
796 Scope (Defining_Entity (Unit (Lib_Unit))));
800 -- If it is a child unit, the parent must be elaborated first and we
801 -- update version, since we are dependent on our parent.
803 if Is_Child_Spec (Unit_Node) then
805 -- The analysis of the parent is done with style checks off
808 Save_Style_Check : constant Boolean := Style_Check;
809 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
810 Cunit_Boolean_Restrictions_Save;
813 if not GNAT_Mode then
814 Style_Check := False;
817 Semantics (Parent_Spec (Unit_Node));
818 Version_Update (N, Parent_Spec (Unit_Node));
819 Style_Check := Save_Style_Check;
820 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
824 -- With the analysis done, install the context. Note that we can't
825 -- install the context from the with clauses as we analyze them, because
826 -- each with clause must be analyzed in a clean visibility context, so
827 -- we have to wait and install them all at once.
831 if Is_Child_Spec (Unit_Node) then
833 -- Set the entities of all parents in the program_unit_name
835 Generate_Parent_References (
836 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
839 -- All components of the context: with-clauses, library unit, ancestors
840 -- if any, (and their context) are analyzed and installed.
842 -- Call special debug routine sm if this is the main unit
844 if Current_Sem_Unit = Main_Unit then
848 -- Now analyze the unit (package, subprogram spec, body) itself
852 if Warn_On_Redundant_Constructs then
853 Check_Redundant_Withs (Context_Items (N));
855 if Nkind (Unit_Node) = N_Package_Body then
856 Check_Redundant_Withs
857 (Context_Items => Context_Items (N),
858 Spec_Context_Items => Context_Items (Lib_Unit));
862 -- The above call might have made Unit_Node an N_Subprogram_Body from
863 -- something else, so propagate any Acts_As_Spec flag.
865 if Nkind (Unit_Node) = N_Subprogram_Body
866 and then Acts_As_Spec (Unit_Node)
868 Set_Acts_As_Spec (N);
871 -- Register predefined units in Rtsfind
874 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
876 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
877 Set_RTU_Loaded (Unit_Node);
881 -- Treat compilation unit pragmas that appear after the library unit
883 if Present (Pragmas_After (Aux_Decls_Node (N))) then
885 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
887 while Present (Prag_Node) loop
894 -- Generate distribution stubs if requested and no error
897 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
899 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
900 and then not Fatal_Error (Main_Unit)
902 if Is_RCI_Pkg_Spec_Or_Body (N) then
904 -- Regular RCI package
906 Add_Stub_Constructs (N);
908 elsif (Nkind (Unit_Node) = N_Package_Declaration
909 and then Is_Shared_Passive (Defining_Entity
910 (Specification (Unit_Node))))
911 or else (Nkind (Unit_Node) = N_Package_Body
913 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
915 -- Shared passive package
917 Add_Stub_Constructs (N);
919 elsif Nkind (Unit_Node) = N_Package_Instantiation
921 Is_Remote_Call_Interface
922 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
924 -- Instantiation of a RCI generic package
926 Add_Stub_Constructs (N);
930 -- Remove unit from visibility, so that environment is clean for
931 -- the next compilation, which is either the main unit or some
932 -- other unit in the context.
934 if Nkind_In (Unit_Node, N_Package_Declaration,
935 N_Package_Renaming_Declaration,
936 N_Subprogram_Declaration)
937 or else Nkind (Unit_Node) in N_Generic_Declaration
939 (Nkind (Unit_Node) = N_Subprogram_Body
940 and then Acts_As_Spec (Unit_Node))
942 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
944 -- If the unit is an instantiation whose body will be elaborated for
945 -- inlining purposes, use the proper entity of the instance. The
946 -- entity may be missing if the instantiation was illegal.
948 elsif Nkind (Unit_Node) = N_Package_Instantiation
949 and then not Error_Posted (Unit_Node)
950 and then Present (Instance_Spec (Unit_Node))
952 Remove_Unit_From_Visibility
953 (Defining_Entity (Instance_Spec (Unit_Node)));
955 elsif Nkind (Unit_Node) = N_Package_Body
956 or else (Nkind (Unit_Node) = N_Subprogram_Body
957 and then not Acts_As_Spec (Unit_Node))
959 -- Bodies that are not the main unit are compiled if they are generic
960 -- or contain generic or inlined units. Their analysis brings in the
961 -- context of the corresponding spec (unit declaration) which must be
962 -- removed as well, to return the compilation environment to its
965 Remove_Context (Lib_Unit);
966 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
969 -- Last step is to deinstall the context we just installed as well as
970 -- the unit just compiled.
974 -- If this is the main unit and we are generating code, we must check
975 -- that all generic units in the context have a body if they need it,
976 -- even if they have not been instantiated. In the absence of .ali files
977 -- for generic units, we must force the load of the body, just to
978 -- produce the proper error if the body is absent. We skip this
979 -- verification if the main unit itself is generic.
981 if Get_Cunit_Unit_Number (N) = Main_Unit
982 and then Operating_Mode = Generate_Code
983 and then Expander_Active
985 -- Check whether the source for the body of the unit must be included
986 -- in a standalone library.
988 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
990 -- Indicate that the main unit is now analyzed, to catch possible
991 -- circularities between it and generic bodies. Remove main unit from
992 -- visibility. This might seem superfluous, but the main unit must
993 -- not be visible in the generic body expansions that follow.
995 Set_Analyzed (N, True);
996 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1001 Un : Unit_Number_Type;
1003 Save_Style_Check : constant Boolean := Style_Check;
1004 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
1005 Cunit_Boolean_Restrictions_Save;
1008 Item := First (Context_Items (N));
1009 while Present (Item) loop
1011 -- Check for explicit with clause
1013 if Nkind (Item) = N_With_Clause
1014 and then not Implicit_With (Item)
1016 -- Ada 2005 (AI-50217): Ignore limited-withed units
1018 and then not Limited_Present (Item)
1020 Nam := Entity (Name (Item));
1022 -- Compile generic subprogram, unless it is intrinsic or
1023 -- imported so no body is required, or generic package body
1024 -- if the package spec requires a body.
1026 if (Is_Generic_Subprogram (Nam)
1027 and then not Is_Intrinsic_Subprogram (Nam)
1028 and then not Is_Imported (Nam))
1029 or else (Ekind (Nam) = E_Generic_Package
1030 and then Unit_Requires_Body (Nam))
1032 Style_Check := False;
1034 if Present (Renamed_Object (Nam)) then
1037 (Load_Name => Get_Body_Name
1039 (Unit_Declaration_Node
1040 (Renamed_Object (Nam)))),
1048 (Load_Name => Get_Body_Name
1049 (Get_Unit_Name (Item)),
1056 if Un = No_Unit then
1058 ("body of generic unit& not found", Item, Nam);
1061 elsif not Analyzed (Cunit (Un))
1062 and then Un /= Main_Unit
1063 and then not Fatal_Error (Un)
1065 Style_Check := False;
1066 Semantics (Cunit (Un));
1074 Style_Check := Save_Style_Check;
1075 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1079 -- Deal with creating elaboration Boolean if needed. We create an
1080 -- elaboration boolean only for units that come from source since
1081 -- units manufactured by the compiler never need elab checks.
1083 if Comes_From_Source (N)
1084 and then Nkind_In (Unit_Node, N_Package_Declaration,
1085 N_Generic_Package_Declaration,
1086 N_Subprogram_Declaration,
1087 N_Generic_Subprogram_Declaration)
1090 Loc : constant Source_Ptr := Sloc (N);
1091 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1094 Spec_Id := Defining_Entity (Unit_Node);
1095 Generate_Definition (Spec_Id);
1097 -- See if an elaboration entity is required for possible access
1098 -- before elaboration checking. Note that we must allow for this
1099 -- even if -gnatE is not set, since a client may be compiled in
1100 -- -gnatE mode and reference the entity.
1102 -- These entities are also used by the binder to prevent multiple
1103 -- attempts to execute the elaboration code for the library case
1104 -- where the elaboration routine might otherwise be called more
1107 -- Case of units which do not require elaboration checks
1110 -- Pure units do not need checks
1114 -- Preelaborated units do not need checks
1116 or else Is_Preelaborated (Spec_Id)
1118 -- No checks needed if pragma Elaborate_Body present
1120 or else Has_Pragma_Elaborate_Body (Spec_Id)
1122 -- No checks needed if unit does not require a body
1124 or else not Unit_Requires_Body (Spec_Id)
1126 -- No checks needed for predefined files
1128 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1130 -- No checks required if no separate spec
1132 or else Acts_As_Spec (N)
1134 -- This is a case where we only need the entity for
1135 -- checking to prevent multiple elaboration checks.
1137 Set_Elaboration_Entity_Required (Spec_Id, False);
1139 -- Case of elaboration entity is required for access before
1140 -- elaboration checking (so certainly we must build it!)
1143 Set_Elaboration_Entity_Required (Spec_Id, True);
1146 Build_Elaboration_Entity (N, Spec_Id);
1150 -- Freeze the compilation unit entity. This for sure is needed because
1151 -- of some warnings that can be output (see Freeze_Subprogram), but may
1152 -- in general be required. If freezing actions result, place them in the
1153 -- compilation unit actions list, and analyze them.
1156 Loc : constant Source_Ptr := Sloc (N);
1157 L : constant List_Id :=
1158 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), Loc);
1160 while Is_Non_Empty_List (L) loop
1161 Insert_Library_Level_Action (Remove_Head (L));
1167 if Nkind (Unit_Node) = N_Package_Declaration
1168 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1169 and then Expander_Active
1172 Save_Style_Check : constant Boolean := Style_Check;
1173 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1174 Options : Style_Check_Options;
1177 Save_Style_Check_Options (Options);
1178 Reset_Style_Check_Options;
1179 Opt.Warning_Mode := Suppress;
1180 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1182 Reset_Style_Check_Options;
1183 Set_Style_Check_Options (Options);
1184 Style_Check := Save_Style_Check;
1185 Warning_Mode := Save_Warning;
1189 -- If we are generating obsolescent warnings, then here is where we
1190 -- generate them for the with'ed items. The reason for this special
1191 -- processing is that the normal mechanism of generating the warnings
1192 -- for referenced entities does not work for context clause references.
1193 -- That's because when we first analyze the context, it is too early to
1194 -- know if the with'ing unit is itself obsolescent (which suppresses
1197 if not GNAT_Mode and then Warn_On_Obsolescent_Feature then
1199 -- Push current compilation unit as scope, so that the test for
1200 -- being within an obsolescent unit will work correctly.
1202 Push_Scope (Defining_Entity (Unit_Node));
1204 -- Loop through context items to deal with with clauses
1212 Item := First (Context_Items (N));
1213 while Present (Item) loop
1214 if Nkind (Item) = N_With_Clause
1216 -- Suppress this check in limited-withed units. Further work
1217 -- needed here if we decide to incorporate this check on
1218 -- limited-withed units.
1220 and then not Limited_Present (Item)
1223 Ent := Entity (Nam);
1225 if Is_Obsolescent (Ent) then
1226 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1234 -- Remove temporary install of current unit as scope
1238 end Analyze_Compilation_Unit;
1240 ---------------------
1241 -- Analyze_Context --
1242 ---------------------
1244 procedure Analyze_Context (N : Node_Id) is
1245 Ukind : constant Node_Kind := Nkind (Unit (N));
1249 -- First process all configuration pragmas at the start of the context
1250 -- items. Strictly these are not part of the context clause, but that
1251 -- is where the parser puts them. In any case for sure we must analyze
1252 -- these before analyzing the actual context items, since they can have
1253 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1254 -- be with'ed as a result of changing categorizations in Ada 2005).
1256 Item := First (Context_Items (N));
1257 while Present (Item)
1258 and then Nkind (Item) = N_Pragma
1259 and then Pragma_Name (Item) in Configuration_Pragma_Names
1265 -- This is the point at which we capture the configuration settings
1266 -- for the unit. At the moment only the Optimize_Alignment setting
1267 -- needs to be captured. Probably more later ???
1269 if Optimize_Alignment_Local then
1270 Set_OA_Setting (Current_Sem_Unit, 'L');
1272 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1275 -- Loop through actual context items. This is done in two passes:
1277 -- a) The first pass analyzes non-limited with-clauses and also any
1278 -- configuration pragmas (we need to get the latter analyzed right
1279 -- away, since they can affect processing of subsequent items.
1281 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1283 while Present (Item) loop
1285 -- For with clause, analyze the with clause, and then update
1286 -- the version, since we are dependent on a unit that we with.
1288 if Nkind (Item) = N_With_Clause
1289 and then not Limited_Present (Item)
1291 -- Skip analyzing with clause if no unit, nothing to do (this
1292 -- happens for a with that references a non-existent unit)
1293 -- Skip as well if this is a with_clause for the main unit, which
1294 -- happens if a subunit has a useless with_clause on its parent.
1296 if Present (Library_Unit (Item)) then
1297 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1301 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1305 if not Implicit_With (Item) then
1306 Version_Update (N, Library_Unit (Item));
1309 -- Skip pragmas. Configuration pragmas at the start were handled in
1310 -- the loop above, and remaining pragmas are not processed until we
1311 -- actually install the context (see Install_Context). We delay the
1312 -- analysis of these pragmas to make sure that we have installed all
1313 -- the implicit with's on parent units.
1315 -- Skip use clauses at this stage, since we don't want to do any
1316 -- installing of potentially use-visible entities until we
1317 -- actually install the complete context (in Install_Context).
1318 -- Otherwise things can get installed in the wrong context.
1327 -- Second pass: examine all limited_with clauses. All other context
1328 -- items are ignored in this pass.
1330 Item := First (Context_Items (N));
1331 while Present (Item) loop
1332 if Nkind (Item) = N_With_Clause
1333 and then Limited_Present (Item)
1335 -- No need to check errors on implicitly generated limited-with
1338 if not Implicit_With (Item) then
1340 -- Verify that the illegal contexts given in 10.1.2 (18/2)
1341 -- are properly rejected, including renaming declarations.
1343 if not Nkind_In (Ukind, N_Package_Declaration,
1344 N_Subprogram_Declaration)
1345 and then Ukind not in N_Generic_Declaration
1346 and then Ukind not in N_Generic_Instantiation
1348 Error_Msg_N ("limited with_clause not allowed here", Item);
1350 -- Check wrong use of a limited with clause applied to the
1351 -- compilation unit containing the limited-with clause.
1353 -- limited with P.Q;
1354 -- package P.Q is ...
1356 elsif Unit (Library_Unit (Item)) = Unit (N) then
1357 Error_Msg_N ("wrong use of limited-with clause", Item);
1359 -- Check wrong use of limited-with clause applied to some
1360 -- immediate ancestor.
1362 elsif Is_Child_Spec (Unit (N)) then
1364 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1368 P := Parent_Spec (Unit (N));
1370 if Unit (P) = Lib_U then
1371 Error_Msg_N ("limited with_clause of immediate "
1372 & "ancestor not allowed", Item);
1376 exit when not Is_Child_Spec (Unit (P));
1377 P := Parent_Spec (Unit (P));
1382 -- Check if the limited-withed unit is already visible through
1383 -- some context clause of the current compilation unit or some
1384 -- ancestor of the current compilation unit.
1387 Lim_Unit_Name : constant Node_Id := Name (Item);
1388 Comp_Unit : Node_Id;
1390 Unit_Name : Node_Id;
1395 It := First (Context_Items (Comp_Unit));
1396 while Present (It) loop
1398 and then Nkind (It) = N_With_Clause
1399 and then not Limited_Present (It)
1401 Nkind_In (Unit (Library_Unit (It)),
1402 N_Package_Declaration,
1403 N_Package_Renaming_Declaration)
1405 if Nkind (Unit (Library_Unit (It))) =
1406 N_Package_Declaration
1408 Unit_Name := Name (It);
1410 Unit_Name := Name (Unit (Library_Unit (It)));
1413 -- Check if the named package (or some ancestor)
1414 -- leaves visible the full-view of the unit given
1415 -- in the limited-with clause
1418 if Designate_Same_Unit (Lim_Unit_Name,
1421 Error_Msg_Sloc := Sloc (It);
1423 ("simultaneous visibility of limited "
1424 & "and unlimited views not allowed",
1427 ("\unlimited view visible through "
1428 & "context clause #",
1432 elsif Nkind (Unit_Name) = N_Identifier then
1436 Unit_Name := Prefix (Unit_Name);
1443 exit when not Is_Child_Spec (Unit (Comp_Unit));
1445 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1450 -- Skip analyzing with clause if no unit, see above
1452 if Present (Library_Unit (Item)) then
1456 -- A limited_with does not impose an elaboration order, but
1457 -- there is a semantic dependency for recompilation purposes.
1459 if not Implicit_With (Item) then
1460 Version_Update (N, Library_Unit (Item));
1463 -- Pragmas and use clauses and with clauses other than limited
1464 -- with's are ignored in this pass through the context items.
1472 end Analyze_Context;
1474 -------------------------------
1475 -- Analyze_Package_Body_Stub --
1476 -------------------------------
1478 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1479 Id : constant Entity_Id := Defining_Identifier (N);
1483 -- The package declaration must be in the current declarative part
1485 Check_Stub_Level (N);
1486 Nam := Current_Entity_In_Scope (Id);
1488 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1489 Error_Msg_N ("missing specification for package stub", N);
1491 elsif Has_Completion (Nam)
1492 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1494 Error_Msg_N ("duplicate or redundant stub for package", N);
1497 -- Indicate that the body of the package exists. If we are doing
1498 -- only semantic analysis, the stub stands for the body. If we are
1499 -- generating code, the existence of the body will be confirmed
1500 -- when we load the proper body.
1502 Set_Has_Completion (Nam);
1503 Set_Scope (Defining_Entity (N), Current_Scope);
1504 Generate_Reference (Nam, Id, 'b');
1505 Analyze_Proper_Body (N, Nam);
1507 end Analyze_Package_Body_Stub;
1509 -------------------------
1510 -- Analyze_Proper_Body --
1511 -------------------------
1513 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1514 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1515 Unum : Unit_Number_Type;
1517 procedure Optional_Subunit;
1518 -- This procedure is called when the main unit is a stub, or when we
1519 -- are not generating code. In such a case, we analyze the subunit if
1520 -- present, which is user-friendly and in fact required for ASIS, but
1521 -- we don't complain if the subunit is missing.
1523 ----------------------
1524 -- Optional_Subunit --
1525 ----------------------
1527 procedure Optional_Subunit is
1528 Comp_Unit : Node_Id;
1531 -- Try to load subunit, but ignore any errors that occur during
1532 -- the loading of the subunit, by using the special feature in
1533 -- Errout to ignore all errors. Note that Fatal_Error will still
1534 -- be set, so we will be able to check for this case below.
1536 if not ASIS_Mode then
1537 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1542 (Load_Name => Subunit_Name,
1547 if not ASIS_Mode then
1548 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1551 -- All done if we successfully loaded the subunit
1554 and then (not Fatal_Error (Unum) or else Try_Semantics)
1556 Comp_Unit := Cunit (Unum);
1558 -- If the file was empty or seriously mangled, the unit
1559 -- itself may be missing.
1561 if No (Unit (Comp_Unit)) then
1563 ("subunit does not contain expected proper body", N);
1565 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1567 ("expected SEPARATE subunit, found child unit",
1568 Cunit_Entity (Unum));
1570 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1571 Analyze_Subunit (Comp_Unit);
1572 Set_Library_Unit (N, Comp_Unit);
1575 elsif Unum = No_Unit
1576 and then Present (Nam)
1578 if Is_Protected_Type (Nam) then
1579 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1581 Set_Corresponding_Body (
1582 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1585 end Optional_Subunit;
1587 -- Start of processing for Analyze_Proper_Body
1590 -- If the subunit is already loaded, it means that the main unit
1591 -- is a subunit, and that the current unit is one of its parents
1592 -- which was being analyzed to provide the needed context for the
1593 -- analysis of the subunit. In this case we analyze the subunit and
1594 -- continue with the parent, without looking a subsequent subunits.
1596 if Is_Loaded (Subunit_Name) then
1598 -- If the proper body is already linked to the stub node,
1599 -- the stub is in a generic unit and just needs analyzing.
1601 if Present (Library_Unit (N)) then
1602 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1603 Analyze_Subunit (Library_Unit (N));
1605 -- Otherwise we must load the subunit and link to it
1608 -- Load the subunit, this must work, since we originally
1609 -- loaded the subunit earlier on. So this will not really
1610 -- load it, just give access to it.
1614 (Load_Name => Subunit_Name,
1619 -- And analyze the subunit in the parent context (note that we
1620 -- do not call Semantics, since that would remove the parent
1621 -- context). Because of this, we have to manually reset the
1622 -- compiler state to Analyzing since it got destroyed by Load.
1624 if Unum /= No_Unit then
1625 Compiler_State := Analyzing;
1627 -- Check that the proper body is a subunit and not a child
1628 -- unit. If the unit was previously loaded, the error will
1629 -- have been emitted when copying the generic node, so we
1630 -- just return to avoid cascaded errors.
1632 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1636 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1637 Analyze_Subunit (Cunit (Unum));
1638 Set_Library_Unit (N, Cunit (Unum));
1642 -- If the main unit is a subunit, then we are just performing semantic
1643 -- analysis on that subunit, and any other subunits of any parent unit
1644 -- should be ignored, except that if we are building trees for ASIS
1645 -- usage we want to annotate the stub properly.
1647 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1648 and then Subunit_Name /= Unit_Name (Main_Unit)
1654 -- But before we return, set the flag for unloaded subunits. This
1655 -- will suppress junk warnings of variables in the same declarative
1656 -- part (or a higher level one) that are in danger of looking unused
1657 -- when in fact there might be a declaration in the subunit that we
1658 -- do not intend to load.
1660 Unloaded_Subunits := True;
1663 -- If the subunit is not already loaded, and we are generating code,
1664 -- then this is the case where compilation started from the parent,
1665 -- and we are generating code for an entire subunit tree. In that
1666 -- case we definitely need to load the subunit.
1668 -- In order to continue the analysis with the rest of the parent,
1669 -- and other subunits, we load the unit without requiring its
1670 -- presence, and emit a warning if not found, rather than terminating
1671 -- the compilation abruptly, as for other missing file problems.
1673 elsif Original_Operating_Mode = Generate_Code then
1675 -- If the proper body is already linked to the stub node,
1676 -- the stub is in a generic unit and just needs analyzing.
1678 -- We update the version. Although we are not technically
1679 -- semantically dependent on the subunit, given our approach
1680 -- of macro substitution of subunits, it makes sense to
1681 -- include it in the version identification.
1683 if Present (Library_Unit (N)) then
1684 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1685 Analyze_Subunit (Library_Unit (N));
1686 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1688 -- Otherwise we must load the subunit and link to it
1693 (Load_Name => Subunit_Name,
1698 if Original_Operating_Mode = Generate_Code
1699 and then Unum = No_Unit
1701 Error_Msg_Unit_1 := Subunit_Name;
1703 Get_File_Name (Subunit_Name, Subunit => True);
1705 ("subunit$$ in file{ not found?", N);
1706 Subunits_Missing := True;
1709 -- Load_Unit may reset Compiler_State, since it may have been
1710 -- necessary to parse an additional units, so we make sure
1711 -- that we reset it to the Analyzing state.
1713 Compiler_State := Analyzing;
1715 if Unum /= No_Unit then
1716 if Debug_Flag_L then
1717 Write_Str ("*** Loaded subunit from stub. Analyze");
1722 Comp_Unit : constant Node_Id := Cunit (Unum);
1725 -- Check for child unit instead of subunit
1727 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1729 ("expected SEPARATE subunit, found child unit",
1730 Cunit_Entity (Unum));
1732 -- OK, we have a subunit
1735 -- Set corresponding stub (even if errors)
1737 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1739 -- Analyze the unit if semantics active
1741 if not Fatal_Error (Unum) or else Try_Semantics then
1742 Analyze_Subunit (Comp_Unit);
1745 -- Set the library unit pointer in any case
1747 Set_Library_Unit (N, Comp_Unit);
1749 -- We update the version. Although we are not technically
1750 -- semantically dependent on the subunit, given our
1751 -- approach of macro substitution of subunits, it makes
1752 -- sense to include it in the version identification.
1754 Version_Update (Cunit (Main_Unit), Comp_Unit);
1760 -- The remaining case is when the subunit is not already loaded and
1761 -- we are not generating code. In this case we are just performing
1762 -- semantic analysis on the parent, and we are not interested in
1763 -- the subunit. For subprograms, analyze the stub as a body. For
1764 -- other entities the stub has already been marked as completed.
1769 end Analyze_Proper_Body;
1771 ----------------------------------
1772 -- Analyze_Protected_Body_Stub --
1773 ----------------------------------
1775 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1776 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1779 Check_Stub_Level (N);
1781 -- First occurrence of name may have been as an incomplete type
1783 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1784 Nam := Full_View (Nam);
1788 or else not Is_Protected_Type (Etype (Nam))
1790 Error_Msg_N ("missing specification for Protected body", N);
1792 Set_Scope (Defining_Entity (N), Current_Scope);
1793 Set_Has_Completion (Etype (Nam));
1794 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1795 Analyze_Proper_Body (N, Etype (Nam));
1797 end Analyze_Protected_Body_Stub;
1799 ----------------------------------
1800 -- Analyze_Subprogram_Body_Stub --
1801 ----------------------------------
1803 -- A subprogram body stub can appear with or without a previous
1804 -- specification. If there is one, the analysis of the body will
1805 -- find it and verify conformance. The formals appearing in the
1806 -- specification of the stub play no role, except for requiring an
1807 -- additional conformance check. If there is no previous subprogram
1808 -- declaration, the stub acts as a spec, and provides the defining
1809 -- entity for the subprogram.
1811 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1815 Check_Stub_Level (N);
1817 -- Verify that the identifier for the stub is unique within this
1818 -- declarative part.
1820 if Nkind_In (Parent (N), N_Block_Statement,
1824 Decl := First (Declarations (Parent (N)));
1825 while Present (Decl)
1828 if Nkind (Decl) = N_Subprogram_Body_Stub
1829 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1830 Chars (Defining_Unit_Name (Specification (N))))
1832 Error_Msg_N ("identifier for stub is not unique", N);
1839 -- Treat stub as a body, which checks conformance if there is a previous
1840 -- declaration, or else introduces entity and its signature.
1842 Analyze_Subprogram_Body (N);
1843 Analyze_Proper_Body (N, Empty);
1844 end Analyze_Subprogram_Body_Stub;
1846 ---------------------
1847 -- Analyze_Subunit --
1848 ---------------------
1850 -- A subunit is compiled either by itself (for semantic checking)
1851 -- or as part of compiling the parent (for code generation). In
1852 -- either case, by the time we actually process the subunit, the
1853 -- parent has already been installed and analyzed. The node N is
1854 -- a compilation unit, whose context needs to be treated here,
1855 -- because we come directly here from the parent without calling
1856 -- Analyze_Compilation_Unit.
1858 -- The compilation context includes the explicit context of the
1859 -- subunit, and the context of the parent, together with the parent
1860 -- itself. In order to compile the current context, we remove the
1861 -- one inherited from the parent, in order to have a clean visibility
1862 -- table. We restore the parent context before analyzing the proper
1863 -- body itself. On exit, we remove only the explicit context of the
1866 procedure Analyze_Subunit (N : Node_Id) is
1867 Lib_Unit : constant Node_Id := Library_Unit (N);
1868 Par_Unit : constant Entity_Id := Current_Scope;
1870 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1871 Num_Scopes : Int := 0;
1872 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1873 Enclosing_Child : Entity_Id := Empty;
1874 Svg : constant Suppress_Array := Scope_Suppress;
1876 procedure Analyze_Subunit_Context;
1877 -- Capture names in use clauses of the subunit. This must be done
1878 -- before re-installing parent declarations, because items in the
1879 -- context must not be hidden by declarations local to the parent.
1881 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1882 -- Recursive procedure to restore scope of all ancestors of subunit,
1883 -- from outermost in. If parent is not a subunit, the call to install
1884 -- context installs context of spec and (if parent is a child unit)
1885 -- the context of its parents as well. It is confusing that parents
1886 -- should be treated differently in both cases, but the semantics are
1887 -- just not identical.
1889 procedure Re_Install_Use_Clauses;
1890 -- As part of the removal of the parent scope, the use clauses are
1891 -- removed, to be reinstalled when the context of the subunit has
1892 -- been analyzed. Use clauses may also have been affected by the
1893 -- analysis of the context of the subunit, so they have to be applied
1894 -- again, to insure that the compilation environment of the rest of
1895 -- the parent unit is identical.
1897 procedure Remove_Scope;
1898 -- Remove current scope from scope stack, and preserve the list
1899 -- of use clauses in it, to be reinstalled after context is analyzed.
1901 -----------------------------
1902 -- Analyze_Subunit_Context --
1903 -----------------------------
1905 procedure Analyze_Subunit_Context is
1908 Unit_Name : Entity_Id;
1911 Analyze_Context (N);
1913 -- Make withed units immediately visible. If child unit, make the
1914 -- ultimate parent immediately visible.
1916 Item := First (Context_Items (N));
1917 while Present (Item) loop
1918 if Nkind (Item) = N_With_Clause then
1920 -- Protect frontend against previous errors in context clauses
1922 if Nkind (Name (Item)) /= N_Selected_Component then
1923 if Error_Posted (Item) then
1927 Unit_Name := Entity (Name (Item));
1928 while Is_Child_Unit (Unit_Name) loop
1929 Set_Is_Visible_Child_Unit (Unit_Name);
1930 Unit_Name := Scope (Unit_Name);
1933 if not Is_Immediately_Visible (Unit_Name) then
1934 Set_Is_Immediately_Visible (Unit_Name);
1935 Set_Context_Installed (Item);
1940 elsif Nkind (Item) = N_Use_Package_Clause then
1941 Nam := First (Names (Item));
1942 while Present (Nam) loop
1947 elsif Nkind (Item) = N_Use_Type_Clause then
1948 Nam := First (Subtype_Marks (Item));
1949 while Present (Nam) loop
1958 -- Reset visibility of withed units. They will be made visible
1959 -- again when we install the subunit context.
1961 Item := First (Context_Items (N));
1962 while Present (Item) loop
1963 if Nkind (Item) = N_With_Clause
1965 -- Protect frontend against previous errors in context clauses
1967 and then Nkind (Name (Item)) /= N_Selected_Component
1968 and then not Error_Posted (Item)
1970 Unit_Name := Entity (Name (Item));
1971 while Is_Child_Unit (Unit_Name) loop
1972 Set_Is_Visible_Child_Unit (Unit_Name, False);
1973 Unit_Name := Scope (Unit_Name);
1976 if Context_Installed (Item) then
1977 Set_Is_Immediately_Visible (Unit_Name, False);
1978 Set_Context_Installed (Item, False);
1984 end Analyze_Subunit_Context;
1986 ------------------------
1987 -- Re_Install_Parents --
1988 ------------------------
1990 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
1994 if Nkind (Unit (L)) = N_Subunit then
1995 Re_Install_Parents (Library_Unit (L), Scope (Scop));
1998 Install_Context (L);
2000 -- If the subunit occurs within a child unit, we must restore the
2001 -- immediate visibility of any siblings that may occur in context.
2003 if Present (Enclosing_Child) then
2004 Install_Siblings (Enclosing_Child, L);
2009 if Scop /= Par_Unit then
2010 Set_Is_Immediately_Visible (Scop);
2013 -- Make entities in scope visible again. For child units, restore
2014 -- visibility only if they are actually in context.
2016 E := First_Entity (Current_Scope);
2017 while Present (E) loop
2018 if not Is_Child_Unit (E)
2019 or else Is_Visible_Child_Unit (E)
2021 Set_Is_Immediately_Visible (E);
2027 -- A subunit appears within a body, and for a nested subunits
2028 -- all the parents are bodies. Restore full visibility of their
2029 -- private entities.
2031 if Is_Package_Or_Generic_Package (Scop) then
2032 Set_In_Package_Body (Scop);
2033 Install_Private_Declarations (Scop);
2035 end Re_Install_Parents;
2037 ----------------------------
2038 -- Re_Install_Use_Clauses --
2039 ----------------------------
2041 procedure Re_Install_Use_Clauses is
2044 for J in reverse 1 .. Num_Scopes loop
2045 U := Use_Clauses (J);
2046 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2047 Install_Use_Clauses (U, Force_Installation => True);
2049 end Re_Install_Use_Clauses;
2055 procedure Remove_Scope is
2059 Num_Scopes := Num_Scopes + 1;
2060 Use_Clauses (Num_Scopes) :=
2061 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2063 E := First_Entity (Current_Scope);
2064 while Present (E) loop
2065 Set_Is_Immediately_Visible (E, False);
2069 if Is_Child_Unit (Current_Scope) then
2070 Enclosing_Child := Current_Scope;
2076 -- Start of processing for Analyze_Subunit
2079 if not Is_Empty_List (Context_Items (N)) then
2081 -- Save current use clauses
2084 Remove_Context (Lib_Unit);
2086 -- Now remove parents and their context, including enclosing
2087 -- subunits and the outer parent body which is not a subunit.
2089 if Present (Lib_Spec) then
2090 Remove_Context (Lib_Spec);
2092 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2093 Lib_Spec := Library_Unit (Lib_Spec);
2095 Remove_Context (Lib_Spec);
2098 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2102 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2103 Remove_Context (Library_Unit (Lib_Spec));
2107 Set_Is_Immediately_Visible (Par_Unit, False);
2109 Analyze_Subunit_Context;
2111 Re_Install_Parents (Lib_Unit, Par_Unit);
2112 Set_Is_Immediately_Visible (Par_Unit);
2114 -- If the context includes a child unit of the parent of the
2115 -- subunit, the parent will have been removed from visibility,
2116 -- after compiling that cousin in the context. The visibility
2117 -- of the parent must be restored now. This also applies if the
2118 -- context includes another subunit of the same parent which in
2119 -- turn includes a child unit in its context.
2121 if Is_Package_Or_Generic_Package (Par_Unit) then
2122 if not Is_Immediately_Visible (Par_Unit)
2123 or else (Present (First_Entity (Par_Unit))
2124 and then not Is_Immediately_Visible
2125 (First_Entity (Par_Unit)))
2127 Set_Is_Immediately_Visible (Par_Unit);
2128 Install_Visible_Declarations (Par_Unit);
2129 Install_Private_Declarations (Par_Unit);
2133 Re_Install_Use_Clauses;
2134 Install_Context (N);
2136 -- Restore state of suppress flags for current body
2138 Scope_Suppress := Svg;
2140 -- If the subunit is within a child unit, then siblings of any
2141 -- parent unit that appear in the context clause of the subunit
2142 -- must also be made immediately visible.
2144 if Present (Enclosing_Child) then
2145 Install_Siblings (Enclosing_Child, N);
2150 Analyze (Proper_Body (Unit (N)));
2153 -- The subunit may contain a with_clause on a sibling of some
2154 -- ancestor. Removing the context will remove from visibility those
2155 -- ancestor child units, which must be restored to the visibility
2156 -- they have in the enclosing body.
2158 if Present (Enclosing_Child) then
2164 and then Is_Child_Unit (C)
2166 Set_Is_Immediately_Visible (C);
2167 Set_Is_Visible_Child_Unit (C);
2172 end Analyze_Subunit;
2174 ----------------------------
2175 -- Analyze_Task_Body_Stub --
2176 ----------------------------
2178 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2179 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2180 Loc : constant Source_Ptr := Sloc (N);
2183 Check_Stub_Level (N);
2185 -- First occurrence of name may have been as an incomplete type
2187 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2188 Nam := Full_View (Nam);
2192 or else not Is_Task_Type (Etype (Nam))
2194 Error_Msg_N ("missing specification for task body", N);
2196 Set_Scope (Defining_Entity (N), Current_Scope);
2197 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2198 Set_Has_Completion (Etype (Nam));
2199 Analyze_Proper_Body (N, Etype (Nam));
2201 -- Set elaboration flag to indicate that entity is callable.
2202 -- This cannot be done in the expansion of the body itself,
2203 -- because the proper body is not in a declarative part. This
2204 -- is only done if expansion is active, because the context
2205 -- may be generic and the flag not defined yet.
2207 if Expander_Active then
2209 Make_Assignment_Statement (Loc,
2211 Make_Identifier (Loc,
2212 New_External_Name (Chars (Etype (Nam)), 'E')),
2213 Expression => New_Reference_To (Standard_True, Loc)));
2217 end Analyze_Task_Body_Stub;
2219 -------------------------
2220 -- Analyze_With_Clause --
2221 -------------------------
2223 -- Analyze the declaration of a unit in a with clause. At end,
2224 -- label the with clause with the defining entity for the unit.
2226 procedure Analyze_With_Clause (N : Node_Id) is
2228 -- Retrieve the original kind of the unit node, before analysis.
2229 -- If it is a subprogram instantiation, its analysis below will
2230 -- rewrite as the declaration of the wrapper package. If the same
2231 -- instantiation appears indirectly elsewhere in the context, it
2232 -- will have been analyzed already.
2234 Unit_Kind : constant Node_Kind :=
2235 Nkind (Original_Node (Unit (Library_Unit (N))));
2236 Nam : constant Node_Id := Name (N);
2238 Par_Name : Entity_Id;
2243 -- Set True if the unit currently being compiled is an internal unit
2245 Save_Style_Check : constant Boolean := Opt.Style_Check;
2246 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
2247 Cunit_Boolean_Restrictions_Save;
2250 U := Unit (Library_Unit (N));
2252 -- Several actions are skipped for dummy packages (those supplied for
2253 -- with's where no matching file could be found). Such packages are
2254 -- identified by the Sloc value being set to No_Location.
2256 if Limited_Present (N) then
2258 -- Ada 2005 (AI-50217): Build visibility structures but do not
2259 -- analyze the unit.
2261 if Sloc (U) /= No_Location then
2262 Build_Limited_Views (N);
2268 -- We reset ordinary style checking during the analysis of a with'ed
2269 -- unit, but we do NOT reset GNAT special analysis mode (the latter
2270 -- definitely *does* apply to with'ed units).
2272 if not GNAT_Mode then
2273 Style_Check := False;
2276 -- If the library unit is a predefined unit, and we are in high
2277 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2278 -- for the analysis of the with'ed unit. This mode does not prevent
2279 -- explicit with'ing of run-time units.
2281 if Configurable_Run_Time_Mode
2283 Is_Predefined_File_Name
2284 (Unit_File_Name (Get_Source_Unit (Unit (Library_Unit (N)))))
2286 Configurable_Run_Time_Mode := False;
2287 Semantics (Library_Unit (N));
2288 Configurable_Run_Time_Mode := True;
2291 Semantics (Library_Unit (N));
2294 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2296 if Sloc (U) /= No_Location then
2298 -- Check restrictions, except that we skip the check if this is an
2299 -- internal unit unless we are compiling the internal unit as the
2300 -- main unit. We also skip this for dummy packages.
2302 Check_Restriction_No_Dependence (Nam, N);
2304 if not Intunit or else Current_Sem_Unit = Main_Unit then
2305 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2308 -- Deal with special case of GNAT.Current_Exceptions which interacts
2309 -- with the optimization of local raise statements into gotos.
2311 if Nkind (Nam) = N_Selected_Component
2312 and then Nkind (Prefix (Nam)) = N_Identifier
2313 and then Chars (Prefix (Nam)) = Name_Gnat
2314 and then (Chars (Selector_Name (Nam)) = Name_Most_Recent_Exception
2316 Chars (Selector_Name (Nam)) = Name_Exception_Traces)
2318 Check_Restriction (No_Exception_Propagation, N);
2319 Special_Exception_Package_Used := True;
2322 -- Check for inappropriate with of internal implementation unit if we
2323 -- are currently compiling the main unit and the main unit is itself
2324 -- not an internal unit. We do not issue this message for implicit
2325 -- with's generated by the compiler itself.
2327 if Implementation_Unit_Warnings
2328 and then Current_Sem_Unit = Main_Unit
2329 and then not Intunit
2330 and then not Implicit_With (N)
2331 and then not GNAT_Mode
2334 U_Kind : constant Kind_Of_Unit :=
2335 Get_Kind_Of_Unit (Get_Source_Unit (U));
2338 if U_Kind = Implementation_Unit then
2339 Error_Msg_F ("& is an internal 'G'N'A'T unit?", Name (N));
2341 ("\use of this unit is non-portable " &
2342 "and version-dependent?",
2345 elsif U_Kind = Ada_05_Unit
2346 and then Ada_Version < Ada_05
2347 and then Warn_On_Ada_2005_Compatibility
2349 Error_Msg_N ("& is an Ada 2005 unit?", Name (N));
2355 -- Semantic analysis of a generic unit is performed on a copy of
2356 -- the original tree. Retrieve the entity on which semantic info
2357 -- actually appears.
2359 if Unit_Kind in N_Generic_Declaration then
2360 E_Name := Defining_Entity (U);
2362 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2363 -- the case of an instantiation, semantic analysis above will have
2364 -- replaced the unit by its instantiated version. If the instance body
2365 -- has been generated, the instance now denotes the body entity. For
2366 -- visibility purposes we need the entity of its spec.
2368 elsif (Unit_Kind = N_Package_Instantiation
2369 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2370 N_Package_Instantiation)
2371 and then Nkind (U) = N_Package_Body
2373 E_Name := Corresponding_Spec (U);
2375 elsif Unit_Kind = N_Package_Instantiation
2376 and then Nkind (U) = N_Package_Instantiation
2378 -- If the instance has not been rewritten as a package declaration,
2379 -- then it appeared already in a previous with clause. Retrieve
2380 -- the entity from the previous instance.
2382 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2384 elsif Unit_Kind in N_Subprogram_Instantiation then
2386 -- The visible subprogram is created during instantiation, and is
2387 -- an attribute of the wrapper package. We retrieve the wrapper
2388 -- package directly from the instantiation node. If the instance
2389 -- is inlined the unit is still an instantiation. Otherwise it has
2390 -- been rewritten as the declaration of the wrapper itself.
2392 if Nkind (U) in N_Subprogram_Instantiation then
2395 (Defining_Entity (Specification (Instance_Spec (U))));
2397 E_Name := Related_Instance (Defining_Entity (U));
2400 elsif Unit_Kind = N_Package_Renaming_Declaration
2401 or else Unit_Kind in N_Generic_Renaming_Declaration
2403 E_Name := Defining_Entity (U);
2405 elsif Unit_Kind = N_Subprogram_Body
2406 and then Nkind (Name (N)) = N_Selected_Component
2407 and then not Acts_As_Spec (Library_Unit (N))
2409 -- For a child unit that has no spec, one has been created and
2410 -- analyzed. The entity required is that of the spec.
2412 E_Name := Corresponding_Spec (U);
2415 E_Name := Defining_Entity (U);
2418 if Nkind (Name (N)) = N_Selected_Component then
2420 -- Child unit in a with clause
2422 Change_Selected_Component_To_Expanded_Name (Name (N));
2425 -- Restore style checks and restrictions
2427 Style_Check := Save_Style_Check;
2428 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
2430 -- Record the reference, but do NOT set the unit as referenced, we want
2431 -- to consider the unit as unreferenced if this is the only reference
2434 Set_Entity_With_Style_Check (Name (N), E_Name);
2435 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2437 -- Generate references and check No_Dependence restriction for parents
2439 if Is_Child_Unit (E_Name) then
2440 Pref := Prefix (Name (N));
2441 Par_Name := Scope (E_Name);
2442 while Nkind (Pref) = N_Selected_Component loop
2443 Change_Selected_Component_To_Expanded_Name (Pref);
2444 Set_Entity_With_Style_Check (Pref, Par_Name);
2446 Generate_Reference (Par_Name, Pref);
2447 Check_Restriction_No_Dependence (Pref, N);
2448 Pref := Prefix (Pref);
2450 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2451 -- is set to Standard_Standard, and no attempt should be made to
2452 -- further unwind scopes.
2454 if Par_Name /= Standard_Standard then
2455 Par_Name := Scope (Par_Name);
2459 if Present (Entity (Pref))
2460 and then not Analyzed (Parent (Parent (Entity (Pref))))
2462 -- If the entity is set without its unit being compiled, the
2463 -- original parent is a renaming, and Par_Name is the renamed
2464 -- entity. For visibility purposes, we need the original entity,
2465 -- which must be analyzed now because Load_Unit directly retrieves
2466 -- the renamed unit, and the renaming declaration itself has not
2469 Analyze (Parent (Parent (Entity (Pref))));
2470 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2471 Par_Name := Entity (Pref);
2474 Set_Entity_With_Style_Check (Pref, Par_Name);
2475 Generate_Reference (Par_Name, Pref);
2478 -- If the withed unit is System, and a system extension pragma is
2479 -- present, compile the extension now, rather than waiting for a
2480 -- visibility check on a specific entity.
2482 if Chars (E_Name) = Name_System
2483 and then Scope (E_Name) = Standard_Standard
2484 and then Present (System_Extend_Unit)
2485 and then Present_System_Aux (N)
2487 -- If the extension is not present, an error will have been emitted
2492 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2493 -- to private_with units; they will be made visible later (just before
2494 -- the private part is analyzed)
2496 if Private_Present (N) then
2497 Set_Is_Immediately_Visible (E_Name, False);
2499 end Analyze_With_Clause;
2501 ------------------------------
2502 -- Check_Private_Child_Unit --
2503 ------------------------------
2505 procedure Check_Private_Child_Unit (N : Node_Id) is
2506 Lib_Unit : constant Node_Id := Unit (N);
2508 Curr_Unit : Entity_Id;
2509 Sub_Parent : Node_Id;
2510 Priv_Child : Entity_Id;
2511 Par_Lib : Entity_Id;
2514 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2515 -- Returns true if and only if the library unit is declared with
2516 -- an explicit designation of private.
2518 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2519 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2522 return Private_Present (Comp_Unit);
2523 end Is_Private_Library_Unit;
2525 -- Start of processing for Check_Private_Child_Unit
2528 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2529 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2530 Par_Lib := Curr_Unit;
2532 elsif Nkind (Lib_Unit) = N_Subunit then
2534 -- The parent is itself a body. The parent entity is to be found in
2535 -- the corresponding spec.
2537 Sub_Parent := Library_Unit (N);
2538 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2540 -- If the parent itself is a subunit, Curr_Unit is the entity
2541 -- of the enclosing body, retrieve the spec entity which is
2542 -- the proper ancestor we need for the following tests.
2544 if Ekind (Curr_Unit) = E_Package_Body then
2545 Curr_Unit := Spec_Entity (Curr_Unit);
2548 Par_Lib := Curr_Unit;
2551 Curr_Unit := Defining_Entity (Lib_Unit);
2553 Par_Lib := Curr_Unit;
2554 Par_Spec := Parent_Spec (Lib_Unit);
2556 if No (Par_Spec) then
2559 Par_Lib := Defining_Entity (Unit (Par_Spec));
2563 -- Loop through context items
2565 Item := First (Context_Items (N));
2566 while Present (Item) loop
2568 -- Ada 2005 (AI-262): Allow private_with of a private child package
2569 -- in public siblings
2571 if Nkind (Item) = N_With_Clause
2572 and then not Implicit_With (Item)
2573 and then not Limited_Present (Item)
2574 and then Is_Private_Descendant (Entity (Name (Item)))
2576 Priv_Child := Entity (Name (Item));
2579 Curr_Parent : Entity_Id := Par_Lib;
2580 Child_Parent : Entity_Id := Scope (Priv_Child);
2581 Prv_Ancestor : Entity_Id := Child_Parent;
2582 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2585 -- If the child unit is a public child then locate the nearest
2586 -- private ancestor. Child_Parent will then be set to the
2587 -- parent of that ancestor.
2589 if not Is_Private_Library_Unit (Priv_Child) then
2590 while Present (Prv_Ancestor)
2591 and then not Is_Private_Library_Unit (Prv_Ancestor)
2593 Prv_Ancestor := Scope (Prv_Ancestor);
2596 if Present (Prv_Ancestor) then
2597 Child_Parent := Scope (Prv_Ancestor);
2601 while Present (Curr_Parent)
2602 and then Curr_Parent /= Standard_Standard
2603 and then Curr_Parent /= Child_Parent
2606 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2607 Curr_Parent := Scope (Curr_Parent);
2610 if No (Curr_Parent) then
2611 Curr_Parent := Standard_Standard;
2614 if Curr_Parent /= Child_Parent then
2615 if Ekind (Priv_Child) = E_Generic_Package
2616 and then Chars (Priv_Child) in Text_IO_Package_Name
2617 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2620 ("& is a nested package, not a compilation unit",
2621 Name (Item), Priv_Child);
2625 ("unit in with clause is private child unit!", Item);
2627 ("\current unit must also have parent&!",
2628 Item, Child_Parent);
2632 or else Private_Present (Item)
2633 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2634 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2635 and then not Acts_As_Spec (Parent (Lib_Unit)))
2641 ("current unit must also be private descendant of&",
2642 Item, Child_Parent);
2650 end Check_Private_Child_Unit;
2652 ----------------------
2653 -- Check_Stub_Level --
2654 ----------------------
2656 procedure Check_Stub_Level (N : Node_Id) is
2657 Par : constant Node_Id := Parent (N);
2658 Kind : constant Node_Kind := Nkind (Par);
2661 if Nkind_In (Kind, N_Package_Body,
2665 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
2669 -- In an instance, a missing stub appears at any level. A warning
2670 -- message will have been emitted already for the missing file.
2672 elsif not In_Instance then
2673 Error_Msg_N ("stub cannot appear in an inner scope", N);
2675 elsif Expander_Active then
2676 Error_Msg_N ("missing proper body", N);
2678 end Check_Stub_Level;
2680 ------------------------
2681 -- Expand_With_Clause --
2682 ------------------------
2684 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2685 Loc : constant Source_Ptr := Sloc (Nam);
2686 Ent : constant Entity_Id := Entity (Nam);
2690 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2691 -- Build name to be used in implicit with_clause. In most cases this
2692 -- is the source name, but if renamings are present we must make the
2693 -- original unit visible, not the one it renames. The entity in the
2694 -- with clause is the renamed unit, but the identifier is the one from
2695 -- the source, which allows us to recover the unit renaming.
2697 ---------------------
2698 -- Build_Unit_Name --
2699 ---------------------
2701 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2703 Renaming : Entity_Id;
2707 if Nkind (Nam) = N_Identifier then
2709 -- If the parent unit P in the name of the with_clause for P.Q
2710 -- is a renaming of package R, then the entity of the parent is
2711 -- set to R, but the identifier retains Chars (P) to be consistent
2712 -- with the source (see details in lib-load). However, the
2713 -- implicit_with_clause for the parent must make the entity for
2714 -- P visible, because P.Q may be used as a prefix within the
2715 -- current unit. The entity for P is the current_entity with that
2716 -- name, because the package renaming declaration for it has just
2717 -- been analyzed. Note that this case can only happen if P.Q has
2718 -- already appeared in a previous with_clause in a related unit,
2719 -- such as the library body of the current unit.
2721 if Chars (Nam) /= Chars (Entity (Nam)) then
2722 Renaming := Current_Entity (Nam);
2723 pragma Assert (Renamed_Entity (Renaming) = Entity (Nam));
2724 return New_Occurrence_Of (Renaming, Loc);
2727 return New_Occurrence_Of (Entity (Nam), Loc);
2731 Ent := Entity (Nam);
2733 if Present (Entity (Selector_Name (Nam)))
2734 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
2736 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
2737 = N_Package_Renaming_Declaration
2739 -- The name in the with_clause is of the form A.B.C, and B
2740 -- is given by a renaming declaration. In that case we may
2741 -- not have analyzed the unit for B, but replaced it directly
2742 -- in lib-load with the unit it renames. We have to make A.B
2743 -- visible, so analyze the declaration for B now, in case it
2744 -- has not been done yet.
2746 Ent := Entity (Selector_Name (Nam));
2749 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
2753 Make_Expanded_Name (Loc,
2754 Chars => Chars (Entity (Nam)),
2755 Prefix => Build_Unit_Name (Prefix (Nam)),
2756 Selector_Name => New_Occurrence_Of (Ent, Loc));
2757 Set_Entity (Result, Ent);
2760 end Build_Unit_Name;
2762 -- Start of processing for Expand_With_Clause
2765 New_Nodes_OK := New_Nodes_OK + 1;
2767 Make_With_Clause (Loc,
2768 Name => Build_Unit_Name (Nam));
2770 P := Parent (Unit_Declaration_Node (Ent));
2771 Set_Library_Unit (Withn, P);
2772 Set_Corresponding_Spec (Withn, Ent);
2773 Set_First_Name (Withn, True);
2774 Set_Implicit_With (Withn, True);
2776 -- If the unit is a package declaration, a private_with_clause on a
2777 -- child unit implies that the implicit with on the parent is also
2780 if Nkind (Unit (N)) = N_Package_Declaration then
2781 Set_Private_Present (Withn, Private_Present (Item));
2784 Prepend (Withn, Context_Items (N));
2785 Mark_Rewrite_Insertion (Withn);
2786 Install_Withed_Unit (Withn);
2788 if Nkind (Nam) = N_Expanded_Name then
2789 Expand_With_Clause (Item, Prefix (Nam), N);
2792 New_Nodes_OK := New_Nodes_OK - 1;
2793 end Expand_With_Clause;
2795 -----------------------
2796 -- Get_Parent_Entity --
2797 -----------------------
2799 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2801 if Nkind (Unit) = N_Package_Body
2802 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
2804 return Defining_Entity
2805 (Specification (Instance_Spec (Original_Node (Unit))));
2806 elsif Nkind (Unit) = N_Package_Instantiation then
2807 return Defining_Entity (Specification (Instance_Spec (Unit)));
2809 return Defining_Entity (Unit);
2811 end Get_Parent_Entity;
2813 ---------------------
2814 -- Has_With_Clause --
2815 ---------------------
2817 function Has_With_Clause
2820 Is_Limited : Boolean := False) return Boolean
2824 function Named_Unit (Clause : Node_Id) return Entity_Id;
2825 -- Return the entity for the unit named in a [limited] with clause
2831 function Named_Unit (Clause : Node_Id) return Entity_Id is
2833 if Nkind (Name (Clause)) = N_Selected_Component then
2834 return Entity (Selector_Name (Name (Clause)));
2836 return Entity (Name (Clause));
2840 -- Start of processing for Has_With_Clause
2843 if Present (Context_Items (C_Unit)) then
2844 Item := First (Context_Items (C_Unit));
2845 while Present (Item) loop
2846 if Nkind (Item) = N_With_Clause
2847 and then Limited_Present (Item) = Is_Limited
2848 and then Named_Unit (Item) = Pack
2858 end Has_With_Clause;
2860 -----------------------------
2861 -- Implicit_With_On_Parent --
2862 -----------------------------
2864 procedure Implicit_With_On_Parent
2865 (Child_Unit : Node_Id;
2868 Loc : constant Source_Ptr := Sloc (N);
2869 P : constant Node_Id := Parent_Spec (Child_Unit);
2870 P_Unit : Node_Id := Unit (P);
2871 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
2874 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2875 -- Build prefix of child unit name. Recurse if needed
2877 function Build_Unit_Name return Node_Id;
2878 -- If the unit is a child unit, build qualified name with all ancestors
2880 -------------------------
2881 -- Build_Ancestor_Name --
2882 -------------------------
2884 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
2885 P_Ref : constant Node_Id :=
2886 New_Reference_To (Defining_Entity (P), Loc);
2887 P_Spec : Node_Id := P;
2890 -- Ancestor may have been rewritten as a package body. Retrieve
2891 -- the original spec to trace earlier ancestors.
2893 if Nkind (P) = N_Package_Body
2894 and then Nkind (Original_Node (P)) = N_Package_Instantiation
2896 P_Spec := Original_Node (P);
2899 if No (Parent_Spec (P_Spec)) then
2903 Make_Selected_Component (Loc,
2904 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
2905 Selector_Name => P_Ref);
2907 end Build_Ancestor_Name;
2909 ---------------------
2910 -- Build_Unit_Name --
2911 ---------------------
2913 function Build_Unit_Name return Node_Id is
2916 if No (Parent_Spec (P_Unit)) then
2917 return New_Reference_To (P_Name, Loc);
2920 Make_Expanded_Name (Loc,
2921 Chars => Chars (P_Name),
2922 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
2923 Selector_Name => New_Reference_To (P_Name, Loc));
2924 Set_Entity (Result, P_Name);
2927 end Build_Unit_Name;
2929 -- Start of processing for Implicit_With_On_Parent
2932 -- The unit of the current compilation may be a package body that
2933 -- replaces an instance node. In this case we need the original instance
2934 -- node to construct the proper parent name.
2936 if Nkind (P_Unit) = N_Package_Body
2937 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
2939 P_Unit := Original_Node (P_Unit);
2942 -- We add the implicit with if the child unit is the current unit being
2943 -- compiled. If the current unit is a body, we do not want to add an
2944 -- implicit_with a second time to the corresponding spec.
2946 if Nkind (Child_Unit) = N_Package_Declaration
2947 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
2952 New_Nodes_OK := New_Nodes_OK + 1;
2953 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
2955 Set_Library_Unit (Withn, P);
2956 Set_Corresponding_Spec (Withn, P_Name);
2957 Set_First_Name (Withn, True);
2958 Set_Implicit_With (Withn, True);
2960 -- Node is placed at the beginning of the context items, so that
2961 -- subsequent use clauses on the parent can be validated.
2963 Prepend (Withn, Context_Items (N));
2964 Mark_Rewrite_Insertion (Withn);
2965 Install_Withed_Unit (Withn);
2967 if Is_Child_Spec (P_Unit) then
2968 Implicit_With_On_Parent (P_Unit, N);
2971 New_Nodes_OK := New_Nodes_OK - 1;
2972 end Implicit_With_On_Parent;
2978 function In_Chain (E : Entity_Id) return Boolean is
2982 H := Current_Entity (E);
2983 while Present (H) loop
2994 ---------------------
2995 -- Install_Context --
2996 ---------------------
2998 procedure Install_Context (N : Node_Id) is
2999 Lib_Unit : constant Node_Id := Unit (N);
3002 Install_Context_Clauses (N);
3004 if Is_Child_Spec (Lib_Unit) then
3005 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3008 Install_Limited_Context_Clauses (N);
3009 end Install_Context;
3011 -----------------------------
3012 -- Install_Context_Clauses --
3013 -----------------------------
3015 procedure Install_Context_Clauses (N : Node_Id) is
3016 Lib_Unit : constant Node_Id := Unit (N);
3018 Uname_Node : Entity_Id;
3019 Check_Private : Boolean := False;
3020 Decl_Node : Node_Id;
3021 Lib_Parent : Entity_Id;
3024 -- First skip configuration pragmas at the start of the context. They
3025 -- are not technically part of the context clause, but that's where the
3026 -- parser puts them. Note they were analyzed in Analyze_Context.
3028 Item := First (Context_Items (N));
3029 while Present (Item)
3030 and then Nkind (Item) = N_Pragma
3031 and then Pragma_Name (Item) in Configuration_Pragma_Names
3036 -- Loop through the actual context clause items. We process everything
3037 -- except Limited_With clauses in this routine. Limited_With clauses
3038 -- are separately installed (see Install_Limited_Context_Clauses).
3040 while Present (Item) loop
3042 -- Case of explicit WITH clause
3044 if Nkind (Item) = N_With_Clause
3045 and then not Implicit_With (Item)
3047 if Limited_Present (Item) then
3049 -- Limited withed units will be installed later
3053 -- If Name (Item) is not an entity name, something is wrong, and
3054 -- this will be detected in due course, for now ignore the item
3056 elsif not Is_Entity_Name (Name (Item)) then
3059 elsif No (Entity (Name (Item))) then
3060 Set_Entity (Name (Item), Any_Id);
3064 Uname_Node := Entity (Name (Item));
3066 if Is_Private_Descendant (Uname_Node) then
3067 Check_Private := True;
3070 Install_Withed_Unit (Item);
3072 Decl_Node := Unit_Declaration_Node (Uname_Node);
3074 -- If the unit is a subprogram instance, it appears nested within
3075 -- a package that carries the parent information.
3077 if Is_Generic_Instance (Uname_Node)
3078 and then Ekind (Uname_Node) /= E_Package
3080 Decl_Node := Parent (Parent (Decl_Node));
3083 if Is_Child_Spec (Decl_Node) then
3084 if Nkind (Name (Item)) = N_Expanded_Name then
3085 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3087 -- If not an expanded name, the child unit must be a
3088 -- renaming, nothing to do.
3093 elsif Nkind (Decl_Node) = N_Subprogram_Body
3094 and then not Acts_As_Spec (Parent (Decl_Node))
3095 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3097 Implicit_With_On_Parent
3098 (Unit (Library_Unit (Parent (Decl_Node))), N);
3101 -- Check license conditions unless this is a dummy unit
3103 if Sloc (Library_Unit (Item)) /= No_Location then
3104 License_Check : declare
3106 Withu : constant Unit_Number_Type :=
3107 Get_Source_Unit (Library_Unit (Item));
3109 Withl : constant License_Type :=
3110 License (Source_Index (Withu));
3112 Unitl : constant License_Type :=
3113 License (Source_Index (Current_Sem_Unit));
3115 procedure License_Error;
3116 -- Signal error of bad license
3122 procedure License_Error is
3125 ("?license of with'ed unit & may be inconsistent",
3129 -- Start of processing for License_Check
3132 -- Exclude license check if withed unit is an internal unit.
3133 -- This situation arises e.g. with the GPL version of GNAT.
3135 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3138 -- Otherwise check various cases
3150 if Withl = Restricted then
3154 when Modified_GPL =>
3155 if Withl = Restricted or else Withl = GPL then
3159 when Unrestricted =>
3166 -- Case of USE PACKAGE clause
3168 elsif Nkind (Item) = N_Use_Package_Clause then
3169 Analyze_Use_Package (Item);
3171 -- Case of USE TYPE clause
3173 elsif Nkind (Item) = N_Use_Type_Clause then
3174 Analyze_Use_Type (Item);
3178 elsif Nkind (Item) = N_Pragma then
3186 if Is_Child_Spec (Lib_Unit) then
3188 -- The unit also has implicit with_clauses on its own parents
3190 if No (Context_Items (N)) then
3191 Set_Context_Items (N, New_List);
3194 Implicit_With_On_Parent (Lib_Unit, N);
3197 -- If the unit is a body, the context of the specification must also
3200 if Nkind (Lib_Unit) = N_Package_Body
3201 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3202 and then not Acts_As_Spec (N))
3204 Install_Context (Library_Unit (N));
3206 if Is_Child_Spec (Unit (Library_Unit (N))) then
3208 -- If the unit is the body of a public child unit, the private
3209 -- declarations of the parent must be made visible. If the child
3210 -- unit is private, the private declarations have been installed
3211 -- already in the call to Install_Parents for the spec. Installing
3212 -- private declarations must be done for all ancestors of public
3213 -- child units. In addition, sibling units mentioned in the
3214 -- context clause of the body are directly visible.
3222 Lib_Spec := Unit (Library_Unit (N));
3223 while Is_Child_Spec (Lib_Spec) loop
3224 P := Unit (Parent_Spec (Lib_Spec));
3225 P_Name := Defining_Entity (P);
3227 if not (Private_Present (Parent (Lib_Spec)))
3228 and then not In_Private_Part (P_Name)
3230 Install_Private_Declarations (P_Name);
3231 Install_Private_With_Clauses (P_Name);
3232 Set_Use (Private_Declarations (Specification (P)));
3240 -- For a package body, children in context are immediately visible
3242 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3245 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3246 N_Generic_Subprogram_Declaration,
3247 N_Package_Declaration,
3248 N_Subprogram_Declaration)
3250 if Is_Child_Spec (Lib_Unit) then
3251 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3252 Set_Is_Private_Descendant
3253 (Defining_Entity (Lib_Unit),
3254 Is_Private_Descendant (Lib_Parent)
3255 or else Private_Present (Parent (Lib_Unit)));
3258 Set_Is_Private_Descendant
3259 (Defining_Entity (Lib_Unit),
3260 Private_Present (Parent (Lib_Unit)));
3264 if Check_Private then
3265 Check_Private_Child_Unit (N);
3267 end Install_Context_Clauses;
3269 -------------------------------------
3270 -- Install_Limited_Context_Clauses --
3271 -------------------------------------
3273 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3276 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3277 -- Check that the unlimited view of a given compilation_unit is not
3278 -- already visible through "use + renamings".
3280 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3281 -- Check that if a limited_with clause of a given compilation_unit
3282 -- mentions a descendant of a private child of some library unit,
3283 -- then the given compilation_unit shall be the declaration of a
3284 -- private descendant of that library unit, or a public descendant
3285 -- of such. The code is analogous to that of Check_Private_Child_Unit
3286 -- but we cannot use entities on the limited with_clauses because
3287 -- their units have not been analyzed, so we have to climb the tree
3288 -- of ancestors looking for private keywords.
3290 procedure Expand_Limited_With_Clause
3291 (Comp_Unit : Node_Id;
3294 -- If a child unit appears in a limited_with clause, there are implicit
3295 -- limited_with clauses on all parents that are not already visible
3296 -- through a regular with clause. This procedure creates the implicit
3297 -- limited with_clauses for the parents and loads the corresponding
3298 -- units. The shadow entities are created when the inserted clause is
3299 -- analyzed. Implements Ada 2005 (AI-50217).
3301 ---------------------
3302 -- Check_Renamings --
3303 ---------------------
3305 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3314 pragma Assert (Nkind (W) = N_With_Clause);
3316 -- Protect the frontend against previous critical errors
3318 case Nkind (Unit (Library_Unit (W))) is
3319 when N_Subprogram_Declaration |
3320 N_Package_Declaration |
3321 N_Generic_Subprogram_Declaration |
3322 N_Generic_Package_Declaration =>
3329 -- Check "use + renamings"
3331 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3332 Spec := Specification (Unit (P));
3334 Item := First (Visible_Declarations (Spec));
3335 while Present (Item) loop
3337 -- Look only at use package clauses
3339 if Nkind (Item) = N_Use_Package_Clause then
3341 -- Traverse the list of packages
3343 Nam := First (Names (Item));
3344 while Present (Nam) loop
3347 pragma Assert (Present (Parent (E)));
3349 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3350 and then Renamed_Entity (E) = WEnt
3352 -- The unlimited view is visible through use clause and
3353 -- renamings. There is no need to generate the error
3354 -- message here because Is_Visible_Through_Renamings
3355 -- takes care of generating the precise error message.
3359 elsif Nkind (Parent (E)) = N_Package_Specification then
3361 -- The use clause may refer to a local package.
3362 -- Check all the enclosing scopes.
3365 while E2 /= Standard_Standard
3373 ("unlimited view visible through use clause ", W);
3385 -- Recursive call to check all the ancestors
3387 if Is_Child_Spec (Unit (P)) then
3388 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3390 end Check_Renamings;
3392 ---------------------------------------
3393 -- Check_Private_Limited_Withed_Unit --
3394 ---------------------------------------
3396 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3397 Curr_Parent : Node_Id;
3398 Child_Parent : Node_Id;
3399 Curr_Private : Boolean;
3402 -- Compilation unit of the parent of the withed library unit
3404 Child_Parent := Library_Unit (Item);
3406 -- If the child unit is a public child, then locate its nearest
3407 -- private ancestor, if any; Child_Parent will then be set to
3408 -- the parent of that ancestor.
3410 if not Private_Present (Library_Unit (Item)) then
3411 while Present (Child_Parent)
3412 and then not Private_Present (Child_Parent)
3414 Child_Parent := Parent_Spec (Unit (Child_Parent));
3417 if No (Child_Parent) then
3422 Child_Parent := Parent_Spec (Unit (Child_Parent));
3424 -- Traverse all the ancestors of the current compilation
3425 -- unit to check if it is a descendant of named library unit.
3427 Curr_Parent := Parent (Item);
3428 Curr_Private := Private_Present (Curr_Parent);
3430 while Present (Parent_Spec (Unit (Curr_Parent)))
3431 and then Curr_Parent /= Child_Parent
3433 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3434 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3437 if Curr_Parent /= Child_Parent then
3439 ("unit in with clause is private child unit!", Item);
3441 ("\current unit must also have parent&!",
3442 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3444 elsif Private_Present (Parent (Item))
3445 or else Curr_Private
3446 or else Private_Present (Item)
3447 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3451 -- Current unit is private, of descendant of a private unit
3457 ("current unit must also be private descendant of&",
3458 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3460 end Check_Private_Limited_Withed_Unit;
3462 --------------------------------
3463 -- Expand_Limited_With_Clause --
3464 --------------------------------
3466 procedure Expand_Limited_With_Clause
3467 (Comp_Unit : Node_Id;
3471 Loc : constant Source_Ptr := Sloc (Nam);
3472 Unum : Unit_Number_Type;
3475 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3476 -- Returns true if the context already includes a with_clause for
3477 -- this unit. If the with_clause is non-limited, the unit is fully
3478 -- visible and an implicit limited_with should not be created. If
3479 -- there is already a limited_with clause for W, a second one is
3480 -- simply redundant.
3482 --------------------------
3483 -- Previous_Withed_Unit --
3484 --------------------------
3486 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3490 -- A limited with_clause cannot appear in the same context_clause
3491 -- as a nonlimited with_clause which mentions the same library.
3493 Item := First (Context_Items (Comp_Unit));
3494 while Present (Item) loop
3495 if Nkind (Item) = N_With_Clause
3496 and then Library_Unit (Item) = Library_Unit (W)
3505 end Previous_Withed_Unit;
3507 -- Start of processing for Expand_Limited_With_Clause
3510 New_Nodes_OK := New_Nodes_OK + 1;
3512 if Nkind (Nam) = N_Identifier then
3514 -- Create node for name of withed unit
3517 Make_With_Clause (Loc,
3518 Name => New_Copy (Nam));
3520 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3522 Make_With_Clause (Loc,
3523 Name => Make_Selected_Component (Loc,
3524 Prefix => New_Copy_Tree (Prefix (Nam)),
3525 Selector_Name => New_Copy (Selector_Name (Nam))));
3526 Set_Parent (Withn, Parent (N));
3529 Set_Limited_Present (Withn);
3530 Set_First_Name (Withn);
3531 Set_Implicit_With (Withn);
3535 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3540 -- Do not generate a limited_with_clause on the current unit.
3541 -- This path is taken when a unit has a limited_with clause on
3542 -- one of its child units.
3544 if Unum = Current_Sem_Unit then
3548 Set_Library_Unit (Withn, Cunit (Unum));
3549 Set_Corresponding_Spec
3550 (Withn, Specification (Unit (Cunit (Unum))));
3552 if not Previous_Withed_Unit (Withn) then
3553 Prepend (Withn, Context_Items (Parent (N)));
3554 Mark_Rewrite_Insertion (Withn);
3556 -- Add implicit limited_with_clauses for parents of child units
3557 -- mentioned in limited_with clauses.
3559 if Nkind (Nam) = N_Selected_Component then
3560 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3565 if not Limited_View_Installed (Withn) then
3566 Install_Limited_Withed_Unit (Withn);
3570 New_Nodes_OK := New_Nodes_OK - 1;
3571 end Expand_Limited_With_Clause;
3573 -- Start of processing for Install_Limited_Context_Clauses
3576 Item := First (Context_Items (N));
3577 while Present (Item) loop
3578 if Nkind (Item) = N_With_Clause
3579 and then Limited_Present (Item)
3581 if Nkind (Name (Item)) = N_Selected_Component then
3582 Expand_Limited_With_Clause
3583 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3586 Check_Private_Limited_Withed_Unit (Item);
3588 if not Implicit_With (Item)
3589 and then Is_Child_Spec (Unit (N))
3591 Check_Renamings (Parent_Spec (Unit (N)), Item);
3594 -- A unit may have a limited with on itself if it has a limited
3595 -- with_clause on one of its child units. In that case it is
3596 -- already being compiled and it makes no sense to install its
3599 -- If the item is a limited_private_with_clause, install it if the
3600 -- current unit is a body or if it is a private child. Otherwise
3601 -- the private clause is installed before analyzing the private
3602 -- part of the current unit.
3604 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3605 and then not Limited_View_Installed (Item)
3607 if not Private_Present (Item)
3608 or else Private_Present (N)
3609 or else Nkind_In (Unit (N), N_Package_Body,
3613 Install_Limited_Withed_Unit (Item);
3621 -- Ada 2005 (AI-412): Examine the visible declarations of a package
3622 -- spec, looking for incomplete subtype declarations of incomplete
3623 -- types visible through a limited with clause.
3625 if Ada_Version >= Ada_05
3626 and then Analyzed (N)
3627 and then Nkind (Unit (N)) = N_Package_Declaration
3632 Non_Lim_View : Entity_Id;
3635 Decl := First (Visible_Declarations (Specification (Unit (N))));
3636 while Present (Decl) loop
3637 if Nkind (Decl) = N_Subtype_Declaration
3639 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3641 From_With_Type (Defining_Identifier (Decl))
3643 Def_Id := Defining_Identifier (Decl);
3644 Non_Lim_View := Non_Limited_View (Def_Id);
3646 if not Is_Incomplete_Type (Non_Lim_View) then
3648 -- Convert an incomplete subtype declaration into a
3649 -- corresponding non-limited view subtype declaration.
3650 -- This is usually the case when analyzing a body that
3651 -- has regular with-clauses, when the spec has limited
3654 -- If the non-limited view is still incomplete, it is
3655 -- the dummy entry already created, and the declaration
3656 -- cannot be reanalyzed. This is the case when installing
3657 -- a parent unit that has limited with-clauses.
3659 Set_Subtype_Indication (Decl,
3660 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3661 Set_Etype (Def_Id, Non_Lim_View);
3662 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3663 Set_Analyzed (Decl, False);
3665 -- Reanalyze the declaration, suppressing the call to
3666 -- Enter_Name to avoid duplicate names.
3668 Analyze_Subtype_Declaration
3678 end Install_Limited_Context_Clauses;
3680 ---------------------
3681 -- Install_Parents --
3682 ---------------------
3684 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3691 P := Unit (Parent_Spec (Lib_Unit));
3692 P_Name := Get_Parent_Entity (P);
3694 if Etype (P_Name) = Any_Type then
3698 if Ekind (P_Name) = E_Generic_Package
3699 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3700 N_Generic_Package_Declaration)
3701 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3704 ("child of a generic package must be a generic unit", Lib_Unit);
3706 elsif not Is_Package_Or_Generic_Package (P_Name) then
3708 ("parent unit must be package or generic package", Lib_Unit);
3709 raise Unrecoverable_Error;
3711 elsif Present (Renamed_Object (P_Name)) then
3712 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3713 raise Unrecoverable_Error;
3715 -- Verify that a child of an instance is itself an instance, or the
3716 -- renaming of one. Given that an instance that is a unit is replaced
3717 -- with a package declaration, check against the original node. The
3718 -- parent may be currently being instantiated, in which case it appears
3719 -- as a declaration, but the generic_parent is already established
3720 -- indicating that we deal with an instance.
3722 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3723 if Nkind (Lib_Unit) in N_Renaming_Declaration
3724 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3726 (Nkind (Lib_Unit) = N_Package_Declaration
3727 and then Present (Generic_Parent (Specification (Lib_Unit))))
3732 ("child of an instance must be an instance or renaming",
3737 -- This is the recursive call that ensures all parents are loaded
3739 if Is_Child_Spec (P) then
3741 Is_Private or else Private_Present (Parent (Lib_Unit)));
3744 -- Now we can install the context for this parent
3746 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3747 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3748 Install_Siblings (P_Name, Parent (Lib_Unit));
3750 -- The child unit is in the declarative region of the parent. The parent
3751 -- must therefore appear in the scope stack and be visible, as when
3752 -- compiling the corresponding body. If the child unit is private or it
3753 -- is a package body, private declarations must be accessible as well.
3754 -- Use declarations in the parent must also be installed. Finally, other
3755 -- child units of the same parent that are in the context are
3756 -- immediately visible.
3758 -- Find entity for compilation unit, and set its private descendant
3759 -- status as needed.
3761 E_Name := Defining_Entity (Lib_Unit);
3763 Set_Is_Child_Unit (E_Name);
3765 Set_Is_Private_Descendant (E_Name,
3766 Is_Private_Descendant (P_Name)
3767 or else Private_Present (Parent (Lib_Unit)));
3769 P_Spec := Specification (Unit_Declaration_Node (P_Name));
3770 Push_Scope (P_Name);
3772 -- Save current visibility of unit
3774 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3775 Is_Immediately_Visible (P_Name);
3776 Set_Is_Immediately_Visible (P_Name);
3777 Install_Visible_Declarations (P_Name);
3778 Set_Use (Visible_Declarations (P_Spec));
3780 -- If the parent is a generic unit, its formal part may contain formal
3781 -- packages and use clauses for them.
3783 if Ekind (P_Name) = E_Generic_Package then
3784 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3788 or else Private_Present (Parent (Lib_Unit))
3790 Install_Private_Declarations (P_Name);
3791 Install_Private_With_Clauses (P_Name);
3792 Set_Use (Private_Declarations (P_Spec));
3794 end Install_Parents;
3796 ----------------------------------
3797 -- Install_Private_With_Clauses --
3798 ----------------------------------
3800 procedure Install_Private_With_Clauses (P : Entity_Id) is
3801 Decl : constant Node_Id := Unit_Declaration_Node (P);
3805 if Debug_Flag_I then
3806 Write_Str ("install private with clauses of ");
3807 Write_Name (Chars (P));
3811 if Nkind (Parent (Decl)) = N_Compilation_Unit then
3812 Item := First (Context_Items (Parent (Decl)));
3813 while Present (Item) loop
3814 if Nkind (Item) = N_With_Clause
3815 and then Private_Present (Item)
3817 if Limited_Present (Item) then
3818 if not Limited_View_Installed (Item) then
3819 Install_Limited_Withed_Unit (Item);
3822 Install_Withed_Unit (Item, Private_With_OK => True);
3829 end Install_Private_With_Clauses;
3831 ----------------------
3832 -- Install_Siblings --
3833 ----------------------
3835 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3841 -- Iterate over explicit with clauses, and check whether the scope of
3842 -- each entity is an ancestor of the current unit, in which case it is
3843 -- immediately visible.
3845 Item := First (Context_Items (N));
3846 while Present (Item) loop
3848 -- Do not install private_with_clauses declaration, unless
3849 -- unit is itself a private child unit, or is a body.
3850 -- Note that for a subprogram body the private_with_clause does
3851 -- not take effect until after the specification.
3853 if Nkind (Item) /= N_With_Clause
3854 or else Implicit_With (Item)
3855 or else Limited_Present (Item)
3859 elsif not Private_Present (Item)
3860 or else Private_Present (N)
3861 or else Nkind (Unit (N)) = N_Package_Body
3863 Id := Entity (Name (Item));
3865 if Is_Child_Unit (Id)
3866 and then Is_Ancestor_Package (Scope (Id), U_Name)
3868 Set_Is_Immediately_Visible (Id);
3870 -- Check for the presence of another unit in the context,
3871 -- that may be inadvertently hidden by the child.
3873 Prev := Current_Entity (Id);
3876 and then Is_Immediately_Visible (Prev)
3877 and then not Is_Child_Unit (Prev)
3883 Clause := First (Context_Items (N));
3884 while Present (Clause) loop
3885 if Nkind (Clause) = N_With_Clause
3886 and then Entity (Name (Clause)) = Prev
3889 ("child unit& hides compilation unit " &
3890 "with the same name?",
3900 -- The With_Clause may be on a grand-child or one of its further
3901 -- descendants, which makes a child immediately visible. Examine
3902 -- ancestry to determine whether such a child exists. For example,
3903 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
3904 -- is immediately visible.
3906 elsif Is_Child_Unit (Id) then
3912 while Is_Child_Unit (Par) loop
3913 if Is_Ancestor_Package (Scope (Par), U_Name) then
3914 Set_Is_Immediately_Visible (Par);
3923 -- If the item is a private with-clause on a child unit, the parent
3924 -- may have been installed already, but the child unit must remain
3925 -- invisible until installed in a private part or body.
3927 elsif Private_Present (Item) then
3928 Id := Entity (Name (Item));
3930 if Is_Child_Unit (Id) then
3931 Set_Is_Visible_Child_Unit (Id, False);
3937 end Install_Siblings;
3939 ---------------------------------
3940 -- Install_Limited_Withed_Unit --
3941 ---------------------------------
3943 procedure Install_Limited_Withed_Unit (N : Node_Id) is
3944 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
3947 Is_Child_Package : Boolean := False;
3948 Lim_Header : Entity_Id;
3949 Lim_Typ : Entity_Id;
3951 procedure Check_Body_Required;
3952 -- A unit mentioned in a limited with_clause may not be mentioned in
3953 -- a regular with_clause, but must still be included in the current
3954 -- partition. We need to determine whether the unit needs a body, so
3955 -- that the binder can determine the name of the file to be compiled.
3956 -- Checking whether a unit needs a body can be done without semantic
3957 -- analysis, by examining the nature of the declarations in the package.
3959 function Has_Limited_With_Clause
3960 (C_Unit : Entity_Id;
3961 Pack : Entity_Id) return Boolean;
3962 -- Determine whether any package in the ancestor chain starting with
3963 -- C_Unit has a limited with clause for package Pack.
3965 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
3966 -- Check if some package installed though normal with-clauses has a
3967 -- renaming declaration of package P. AARM 10.1.2(21/2).
3969 -------------------------
3970 -- Check_Body_Required --
3971 -------------------------
3973 procedure Check_Body_Required is
3974 PA : constant List_Id :=
3975 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
3977 procedure Check_Declarations (Spec : Node_Id);
3978 -- Recursive procedure that does the work and checks nested packages
3980 ------------------------
3981 -- Check_Declarations --
3982 ------------------------
3984 procedure Check_Declarations (Spec : Node_Id) is
3986 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
3988 Subp_List : constant Elist_Id := New_Elmt_List;
3990 procedure Check_Pragma_Import (P : Node_Id);
3991 -- If a pragma import applies to a previous subprogram, the
3992 -- enclosing unit may not need a body. The processing is syntactic
3993 -- and does not require a declaration to be analyzed. The code
3994 -- below also handles pragma Import when applied to a subprogram
3995 -- that renames another. In this case the pragma applies to the
3998 -- Chains of multiple renames are not handled by the code below.
3999 -- It is probably impossible to handle all cases without proper
4000 -- name resolution. In such cases the algorithm is conservative
4001 -- and will indicate that a body is needed???
4003 -------------------------
4004 -- Check_Pragma_Import --
4005 -------------------------
4007 procedure Check_Pragma_Import (P : Node_Id) is
4013 procedure Remove_Homonyms (E : Node_Id);
4014 -- Make one pass over list of subprograms. Called again if
4015 -- subprogram is a renaming. E is known to be an identifier.
4017 ---------------------
4018 -- Remove_Homonyms --
4019 ---------------------
4021 procedure Remove_Homonyms (E : Node_Id) is
4022 R : Entity_Id := Empty;
4023 -- Name of renamed entity, if any
4026 Subp_Id := First_Elmt (Subp_List);
4027 while Present (Subp_Id) loop
4028 if Chars (Node (Subp_Id)) = Chars (E) then
4029 if Nkind (Parent (Parent (Node (Subp_Id))))
4030 /= N_Subprogram_Renaming_Declaration
4033 Next_Elmt (Subp_Id);
4034 Remove_Elmt (Subp_List, Prev_Id);
4036 R := Name (Parent (Parent (Node (Subp_Id))));
4040 Next_Elmt (Subp_Id);
4045 if Nkind (R) = N_Identifier then
4046 Remove_Homonyms (R);
4048 elsif Nkind (R) = N_Selected_Component then
4049 Remove_Homonyms (Selector_Name (R));
4051 -- Renaming of attribute
4057 end Remove_Homonyms;
4059 -- Start of processing for Check_Pragma_Import
4062 -- Find name of entity in Import pragma. We have not analyzed
4063 -- the construct, so we must guard against syntax errors.
4065 Arg := Next (First (Pragma_Argument_Associations (P)));
4068 or else Nkind (Expression (Arg)) /= N_Identifier
4072 Imported := Expression (Arg);
4075 Remove_Homonyms (Imported);
4076 end Check_Pragma_Import;
4078 -- Start of processing for Check_Declarations
4081 -- Search for Elaborate Body pragma
4083 Decl := First (Visible_Declarations (Spec));
4084 while Present (Decl)
4085 and then Nkind (Decl) = N_Pragma
4087 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4088 Set_Body_Required (Library_Unit (N));
4095 -- Look for declarations that require the presence of a body
4097 while Present (Decl) loop
4099 -- Subprogram that comes from source means body may be needed.
4100 -- Save for subsequent examination of import pragmas.
4102 if Comes_From_Source (Decl)
4103 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4104 N_Subprogram_Renaming_Declaration,
4105 N_Generic_Subprogram_Declaration))
4107 Append_Elmt (Defining_Entity (Decl), Subp_List);
4109 -- Package declaration of generic package declaration. We need
4110 -- to recursively examine nested declarations.
4112 elsif Nkind_In (Decl, N_Package_Declaration,
4113 N_Generic_Package_Declaration)
4115 Check_Declarations (Specification (Decl));
4117 elsif Nkind (Decl) = N_Pragma
4118 and then Pragma_Name (Decl) = Name_Import
4120 Check_Pragma_Import (Decl);
4126 -- Same set of tests for private part. In addition to subprograms
4127 -- detect the presence of Taft Amendment types (incomplete types
4128 -- completed in the body).
4130 Decl := First (Private_Declarations (Spec));
4131 while Present (Decl) loop
4132 if Comes_From_Source (Decl)
4133 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4134 N_Subprogram_Renaming_Declaration,
4135 N_Generic_Subprogram_Declaration))
4137 Append_Elmt (Defining_Entity (Decl), Subp_List);
4139 elsif Nkind_In (Decl, N_Package_Declaration,
4140 N_Generic_Package_Declaration)
4142 Check_Declarations (Specification (Decl));
4144 -- Collect incomplete type declarations for separate pass
4146 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4147 Append_Elmt (Decl, Incomplete_Decls);
4149 elsif Nkind (Decl) = N_Pragma
4150 and then Pragma_Name (Decl) = Name_Import
4152 Check_Pragma_Import (Decl);
4158 -- Now check incomplete declarations to locate Taft amendment
4159 -- types. This can be done by examining the defining identifiers
4160 -- of type declarations without real semantic analysis.
4166 Inc := First_Elmt (Incomplete_Decls);
4167 while Present (Inc) loop
4168 Decl := Next (Node (Inc));
4169 while Present (Decl) loop
4170 if Nkind (Decl) = N_Full_Type_Declaration
4171 and then Chars (Defining_Identifier (Decl)) =
4172 Chars (Defining_Identifier (Node (Inc)))
4180 -- If no completion, this is a TAT, and a body is needed
4183 Set_Body_Required (Library_Unit (N));
4191 -- Finally, check whether there are subprograms that still
4192 -- require a body, i.e. are not renamings or null.
4194 if not Is_Empty_Elmt_List (Subp_List) then
4200 Subp_Id := First_Elmt (Subp_List);
4201 Spec := Parent (Node (Subp_Id));
4203 while Present (Subp_Id) loop
4204 if Nkind (Parent (Spec))
4205 = N_Subprogram_Renaming_Declaration
4209 elsif Nkind (Spec) = N_Procedure_Specification
4210 and then Null_Present (Spec)
4215 Set_Body_Required (Library_Unit (N));
4219 Next_Elmt (Subp_Id);
4223 end Check_Declarations;
4225 -- Start of processing for Check_Body_Required
4228 -- If this is an imported package (Java and CIL usage) no body is
4229 -- needed. Scan list of pragmas that may follow a compilation unit
4230 -- to look for a relevant pragma Import.
4232 if Present (PA) then
4238 while Present (Prag) loop
4239 if Nkind (Prag) = N_Pragma
4240 and then Get_Pragma_Id (Prag) = Pragma_Import
4250 Check_Declarations (Specification (P_Unit));
4251 end Check_Body_Required;
4253 -----------------------------
4254 -- Has_Limited_With_Clause --
4255 -----------------------------
4257 function Has_Limited_With_Clause
4258 (C_Unit : Entity_Id;
4259 Pack : Entity_Id) return Boolean
4266 while Present (Par) loop
4267 if Ekind (Par) /= E_Package then
4271 -- Retrieve the Compilation_Unit node for Par and determine if
4272 -- its context clauses contain a limited with for Pack.
4274 Par_Unit := Parent (Parent (Parent (Par)));
4276 if Nkind (Par_Unit) = N_Package_Declaration then
4277 Par_Unit := Parent (Par_Unit);
4280 if Has_With_Clause (Par_Unit, Pack, True) then
4284 -- If there are more ancestors, climb up the tree, otherwise
4287 if Is_Child_Unit (Par) then
4295 end Has_Limited_With_Clause;
4297 ----------------------------------
4298 -- Is_Visible_Through_Renamings --
4299 ----------------------------------
4301 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4302 Kind : constant Node_Kind :=
4303 Nkind (Unit (Cunit (Current_Sem_Unit)));
4309 -- Example of the error detected by this subprogram:
4317 -- package Ren_P renames P;
4323 -- limited with P; -- ERROR
4324 -- package R.C is ...
4326 Aux_Unit := Cunit (Current_Sem_Unit);
4329 Item := First (Context_Items (Aux_Unit));
4330 while Present (Item) loop
4331 if Nkind (Item) = N_With_Clause
4332 and then not Limited_Present (Item)
4333 and then Nkind (Unit (Library_Unit (Item))) =
4334 N_Package_Declaration
4337 First (Visible_Declarations
4338 (Specification (Unit (Library_Unit (Item)))));
4339 while Present (Decl) loop
4340 if Nkind (Decl) = N_Package_Renaming_Declaration
4341 and then Entity (Name (Decl)) = P
4343 -- Generate the error message only if the current unit
4344 -- is a package declaration; in case of subprogram
4345 -- bodies and package bodies we just return True to
4346 -- indicate that the limited view must not be
4349 if Kind = N_Package_Declaration then
4351 ("simultaneous visibility of the limited and " &
4352 "unlimited views not allowed", N);
4353 Error_Msg_Sloc := Sloc (Item);
4355 ("\\ unlimited view of & visible through the " &
4356 "context clause #", N, P);
4357 Error_Msg_Sloc := Sloc (Decl);
4358 Error_Msg_NE ("\\ and the renaming #", N, P);
4371 -- If it's a body not acting as spec, follow pointer to
4372 -- corresponding spec, otherwise follow pointer to parent spec.
4374 if Present (Library_Unit (Aux_Unit))
4375 and then Nkind_In (Unit (Aux_Unit),
4376 N_Package_Body, N_Subprogram_Body)
4378 if Aux_Unit = Library_Unit (Aux_Unit) then
4380 -- Aux_Unit is a body that acts as a spec. Clause has
4381 -- already been flagged as illegal.
4386 Aux_Unit := Library_Unit (Aux_Unit);
4390 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4393 exit when No (Aux_Unit);
4397 end Is_Visible_Through_Renamings;
4399 -- Start of processing for Install_Limited_Withed_Unit
4402 pragma Assert (not Limited_View_Installed (N));
4404 -- In case of limited with_clause on subprograms, generics, instances,
4405 -- or renamings, the corresponding error was previously posted and we
4406 -- have nothing to do here. If the file is missing altogether, it has
4407 -- no source location.
4409 if Nkind (P_Unit) /= N_Package_Declaration
4410 or else Sloc (P_Unit) = No_Location
4415 P := Defining_Unit_Name (Specification (P_Unit));
4417 -- Handle child packages
4419 if Nkind (P) = N_Defining_Program_Unit_Name then
4420 Is_Child_Package := True;
4421 P := Defining_Identifier (P);
4424 -- Do not install the limited-view if the context of the unit is already
4425 -- available through a regular with clause.
4427 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4428 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4433 -- Do not install the limited-view if the full-view is already visible
4434 -- through renaming declarations.
4436 if Is_Visible_Through_Renamings (P) then
4440 -- Do not install the limited view if this is the unit being analyzed.
4441 -- This unusual case will happen when a unit has a limited_with clause
4442 -- on one of its children. The compilation of the child forces the
4443 -- load of the parent which tries to install the limited view of the
4444 -- child again. Installing the limited view must also be disabled
4445 -- when compiling the body of the child unit.
4447 if P = Cunit_Entity (Current_Sem_Unit)
4449 (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4450 and then P = Main_Unit_Entity)
4455 -- This scenario is similar to the one above, the difference is that
4456 -- the compilation of sibling Par.Sib forces the load of parent Par
4457 -- which tries to install the limited view of Lim_Pack [1]. However
4458 -- Par.Sib has a with clause for Lim_Pack [2] in its body, and thus
4459 -- needs the non-limited views of all entities from Lim_Pack.
4461 -- limited with Lim_Pack; -- [1]
4462 -- package Par is ... package Lim_Pack is ...
4464 -- with Lim_Pack; -- [2]
4465 -- package Par.Sib is ... package body Par.Sib is ...
4467 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4468 -- Sem_Unit is the body of Par.Sib.
4470 if Ekind (P) = E_Package
4471 and then Ekind (Main_Unit_Entity) = E_Package
4472 and then Is_Child_Unit (Main_Unit_Entity)
4474 -- The body has a regular with clause
4476 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4477 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4479 -- One of the ancestors has a limited with clause
4481 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4482 N_Package_Specification
4483 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4488 -- A common use of the limited-with is to have a limited-with
4489 -- in the package spec, and a normal with in its package body.
4492 -- limited with X; -- [1]
4496 -- package body A is ...
4498 -- The compilation of A's body installs the context clauses found at [2]
4499 -- and then the context clauses of its specification (found at [1]). As
4500 -- a consequence, at [1] the specification of X has been analyzed and it
4501 -- is immediately visible. According to the semantics of limited-with
4502 -- context clauses we don't install the limited view because the full
4503 -- view of X supersedes its limited view.
4505 if Analyzed (P_Unit)
4507 (Is_Immediately_Visible (P)
4509 (Is_Child_Package and then Is_Visible_Child_Unit (P)))
4514 if Debug_Flag_I then
4515 Write_Str ("install limited view of ");
4516 Write_Name (Chars (P));
4520 -- If the unit has not been analyzed and the limited view has not been
4521 -- already installed then we install it.
4523 if not Analyzed (P_Unit) then
4524 if not In_Chain (P) then
4526 -- Minimum decoration
4528 Set_Ekind (P, E_Package);
4529 Set_Etype (P, Standard_Void_Type);
4530 Set_Scope (P, Standard_Standard);
4532 if Is_Child_Package then
4533 Set_Is_Child_Unit (P);
4534 Set_Is_Visible_Child_Unit (P);
4535 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4538 -- Place entity on visibility structure
4540 Set_Homonym (P, Current_Entity (P));
4541 Set_Current_Entity (P);
4543 if Debug_Flag_I then
4544 Write_Str (" (homonym) chain ");
4545 Write_Name (Chars (P));
4549 -- Install the incomplete view. The first element of the limited
4550 -- view is a header (an E_Package entity) used to reference the
4551 -- first shadow entity in the private part of the package.
4553 Lim_Header := Limited_View (P);
4554 Lim_Typ := First_Entity (Lim_Header);
4556 while Present (Lim_Typ)
4557 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4559 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4560 Set_Current_Entity (Lim_Typ);
4562 if Debug_Flag_I then
4563 Write_Str (" (homonym) chain ");
4564 Write_Name (Chars (Lim_Typ));
4568 Next_Entity (Lim_Typ);
4572 -- If the unit appears in a previous regular with_clause, the regular
4573 -- entities of the public part of the withed package must be replaced
4574 -- by the shadow ones.
4576 -- This code must be kept synchronized with the code that replaces the
4577 -- shadow entities by the real entities (see body of Remove_Limited
4578 -- With_Clause); otherwise the contents of the homonym chains are not
4582 -- Hide all the type entities of the public part of the package to
4583 -- avoid its usage. This is needed to cover all the subtype decla-
4584 -- rations because we do not remove them from the homonym chain.
4586 E := First_Entity (P);
4587 while Present (E) and then E /= First_Private_Entity (P) loop
4589 Set_Was_Hidden (E, Is_Hidden (E));
4596 -- Replace the real entities by the shadow entities of the limited
4597 -- view. The first element of the limited view is a header that is
4598 -- used to reference the first shadow entity in the private part
4599 -- of the package. Successive elements are the limited views of the
4600 -- type (including regular incomplete types) declared in the package.
4602 Lim_Header := Limited_View (P);
4604 Lim_Typ := First_Entity (Lim_Header);
4605 while Present (Lim_Typ)
4606 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4608 pragma Assert (not In_Chain (Lim_Typ));
4610 -- Do not unchain nested packages and child units
4612 if Ekind (Lim_Typ) /= E_Package
4613 and then not Is_Child_Unit (Lim_Typ)
4619 Prev := Current_Entity (Lim_Typ);
4622 -- Replace E in the homonyms list, so that the limited
4623 -- view becomes available.
4625 if E = Non_Limited_View (Lim_Typ) then
4626 Set_Homonym (Lim_Typ, Homonym (Prev));
4627 Set_Current_Entity (Lim_Typ);
4631 E := Homonym (Prev);
4633 -- E may have been removed when installing a
4634 -- previous limited_with_clause.
4638 exit when E = Non_Limited_View (Lim_Typ);
4640 Prev := Homonym (Prev);
4644 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4645 Set_Homonym (Prev, Lim_Typ);
4650 if Debug_Flag_I then
4651 Write_Str (" (homonym) chain ");
4652 Write_Name (Chars (Lim_Typ));
4657 Next_Entity (Lim_Typ);
4661 -- The package must be visible while the limited-with clause is active
4662 -- because references to the type P.T must resolve in the usual way.
4663 -- In addition, we remember that the limited-view has been installed to
4664 -- uninstall it at the point of context removal.
4666 Set_Is_Immediately_Visible (P);
4667 Set_Limited_View_Installed (N);
4669 -- If unit has not been analyzed in some previous context, check
4670 -- (imperfectly ???) whether it might need a body.
4672 if not Analyzed (P_Unit) then
4673 Check_Body_Required;
4676 -- If the package in the limited_with clause is a child unit, the
4677 -- clause is unanalyzed and appears as a selected component. Recast
4678 -- it as an expanded name so that the entity can be properly set. Use
4679 -- entity of parent, if available, for higher ancestors in the name.
4681 if Nkind (Name (N)) = N_Selected_Component then
4689 while Nkind (Nam) = N_Selected_Component
4690 and then Present (Ent)
4692 Change_Selected_Component_To_Expanded_Name (Nam);
4694 -- Set entity of parent identifiers if the unit is a child
4695 -- unit. This ensures that the tree is properly formed from
4696 -- semantic point of view (e.g. for ASIS queries).
4698 Set_Entity (Nam, Ent);
4700 Nam := Prefix (Nam);
4703 -- Set entity of last ancestor
4705 if Nkind (Nam) = N_Identifier then
4706 Set_Entity (Nam, Ent);
4712 Set_Entity (Name (N), P);
4713 Set_From_With_Type (P);
4714 end Install_Limited_Withed_Unit;
4716 -------------------------
4717 -- Install_Withed_Unit --
4718 -------------------------
4720 procedure Install_Withed_Unit
4721 (With_Clause : Node_Id;
4722 Private_With_OK : Boolean := False)
4724 Uname : constant Entity_Id := Entity (Name (With_Clause));
4725 P : constant Entity_Id := Scope (Uname);
4728 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
4729 -- compiling a package declaration and the Private_With_OK flag was not
4730 -- set by the caller. These declarations will be installed later (before
4731 -- analyzing the private part of the package).
4733 if Private_Present (With_Clause)
4734 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
4735 and then not (Private_With_OK)
4740 if Debug_Flag_I then
4741 if Private_Present (With_Clause) then
4742 Write_Str ("install private withed unit ");
4744 Write_Str ("install withed unit ");
4747 Write_Name (Chars (Uname));
4751 -- We do not apply the restrictions to an internal unit unless
4752 -- we are compiling the internal unit as a main unit. This check
4753 -- is also skipped for dummy units (for missing packages).
4755 if Sloc (Uname) /= No_Location
4756 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
4757 or else Current_Sem_Unit = Main_Unit)
4759 Check_Restricted_Unit
4760 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
4763 if P /= Standard_Standard then
4765 -- If the unit is not analyzed after analysis of the with clause and
4766 -- it is an instantiation then it awaits a body and is the main unit.
4767 -- Its appearance in the context of some other unit indicates a
4768 -- circular dependency (DEC suite perversity).
4770 if not Analyzed (Uname)
4771 and then Nkind (Parent (Uname)) = N_Package_Instantiation
4774 ("instantiation depends on itself", Name (With_Clause));
4776 elsif not Is_Visible_Child_Unit (Uname) then
4777 Set_Is_Visible_Child_Unit (Uname);
4779 -- If the child unit appears in the context of its parent, it is
4780 -- immediately visible.
4782 if In_Open_Scopes (Scope (Uname)) then
4783 Set_Is_Immediately_Visible (Uname);
4786 if Is_Generic_Instance (Uname)
4787 and then Ekind (Uname) in Subprogram_Kind
4789 -- Set flag as well on the visible entity that denotes the
4790 -- instance, which renames the current one.
4792 Set_Is_Visible_Child_Unit
4794 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
4797 -- The parent unit may have been installed already, and may have
4798 -- appeared in a use clause.
4800 if In_Use (Scope (Uname)) then
4801 Set_Is_Potentially_Use_Visible (Uname);
4804 Set_Context_Installed (With_Clause);
4807 elsif not Is_Immediately_Visible (Uname) then
4808 if not Private_Present (With_Clause)
4809 or else Private_With_OK
4811 Set_Is_Immediately_Visible (Uname);
4814 Set_Context_Installed (With_Clause);
4817 -- A with-clause overrides a with-type clause: there are no restric-
4818 -- tions on the use of package entities.
4820 if Ekind (Uname) = E_Package then
4821 Set_From_With_Type (Uname, False);
4824 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
4825 -- unit if there is a visible homograph for it declared in the same
4826 -- declarative region. This pathological case can only arise when an
4827 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
4828 -- G1 has a generic child also named G2, and the context includes with_
4829 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
4830 -- of I1.G2 visible as well. If the child unit is named Standard, do
4831 -- not apply the check to the Standard package itself.
4833 if Is_Child_Unit (Uname)
4834 and then Is_Visible_Child_Unit (Uname)
4835 and then Ada_Version >= Ada_05
4838 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
4844 U2 := Homonym (Uname);
4846 and then U2 /= Standard_Standard
4849 Decl2 := Unit_Declaration_Node (P2);
4851 if Is_Child_Unit (U2)
4852 and then Is_Visible_Child_Unit (U2)
4854 if Is_Generic_Instance (P)
4855 and then Nkind (Decl1) = N_Package_Declaration
4856 and then Generic_Parent (Specification (Decl1)) = P2
4858 Error_Msg_N ("illegal with_clause", With_Clause);
4860 ("\child unit has visible homograph" &
4861 " (RM 8.3(26), 10.1.1(19))",
4865 elsif Is_Generic_Instance (P2)
4866 and then Nkind (Decl2) = N_Package_Declaration
4867 and then Generic_Parent (Specification (Decl2)) = P
4869 -- With_clause for child unit of instance appears before
4870 -- in the context. We want to place the error message on
4871 -- it, not on the generic child unit itself.
4874 Prev_Clause : Node_Id;
4877 Prev_Clause := First (List_Containing (With_Clause));
4878 while Entity (Name (Prev_Clause)) /= U2 loop
4882 pragma Assert (Present (Prev_Clause));
4883 Error_Msg_N ("illegal with_clause", Prev_Clause);
4885 ("\child unit has visible homograph" &
4886 " (RM 8.3(26), 10.1.1(19))",
4897 end Install_Withed_Unit;
4903 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
4904 K : constant Node_Kind := Nkind (Lib_Unit);
4907 return (K in N_Generic_Declaration or else
4908 K in N_Generic_Instantiation or else
4909 K in N_Generic_Renaming_Declaration or else
4910 K = N_Package_Declaration or else
4911 K = N_Package_Renaming_Declaration or else
4912 K = N_Subprogram_Declaration or else
4913 K = N_Subprogram_Renaming_Declaration)
4914 and then Present (Parent_Spec (Lib_Unit));
4917 ------------------------------------
4918 -- Is_Legal_Shadow_Entity_In_Body --
4919 ------------------------------------
4921 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
4922 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
4924 return Nkind (Unit (C_Unit)) = N_Package_Body
4925 and then Has_With_Clause (C_Unit,
4926 Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
4927 end Is_Legal_Shadow_Entity_In_Body;
4929 -----------------------
4930 -- Load_Needed_Body --
4931 -----------------------
4933 -- N is a generic unit named in a with clause, or else it is a unit that
4934 -- contains a generic unit or an inlined function. In order to perform an
4935 -- instantiation, the body of the unit must be present. If the unit itself
4936 -- is generic, we assume that an instantiation follows, and load & analyze
4937 -- the body unconditionally. This forces analysis of the spec as well.
4939 -- If the unit is not generic, but contains a generic unit, it is loaded on
4940 -- demand, at the point of instantiation (see ch12).
4942 procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
4943 Body_Name : Unit_Name_Type;
4944 Unum : Unit_Number_Type;
4946 Save_Style_Check : constant Boolean := Opt.Style_Check;
4947 -- The loading and analysis is done with style checks off
4950 if not GNAT_Mode then
4951 Style_Check := False;
4954 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
4957 (Load_Name => Body_Name,
4963 if Unum = No_Unit then
4967 Compiler_State := Analyzing; -- reset after load
4969 if not Fatal_Error (Unum) or else Try_Semantics then
4970 if Debug_Flag_L then
4971 Write_Str ("*** Loaded generic body");
4975 Semantics (Cunit (Unum));
4981 Style_Check := Save_Style_Check;
4982 end Load_Needed_Body;
4984 -------------------------
4985 -- Build_Limited_Views --
4986 -------------------------
4988 procedure Build_Limited_Views (N : Node_Id) is
4989 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
4990 P : constant Entity_Id := Cunit_Entity (Unum);
4992 Spec : Node_Id; -- To denote a package specification
4993 Lim_Typ : Entity_Id; -- To denote shadow entities
4994 Comp_Typ : Entity_Id; -- To denote real entities
4996 Lim_Header : Entity_Id; -- Package entity
4997 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
4998 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
5000 procedure Decorate_Incomplete_Type
5003 -- Add attributes of an incomplete type to a shadow entity. The same
5004 -- attributes are placed on the real entity, so that gigi receives
5005 -- a consistent view.
5007 procedure Decorate_Package_Specification (P : Entity_Id);
5008 -- Add attributes of a package entity to the entity in a package
5011 procedure Decorate_Tagged_Type
5015 -- Set basic attributes of tagged type T, including its class_wide type.
5016 -- The parameters Loc, Scope are used to decorate the class_wide type.
5018 procedure Build_Chain
5020 First_Decl : Node_Id);
5021 -- Construct list of shadow entities and attach it to entity of
5022 -- package that is mentioned in a limited_with clause.
5024 function New_Internal_Shadow_Entity
5025 (Kind : Entity_Kind;
5026 Sloc_Value : Source_Ptr;
5027 Id_Char : Character) return Entity_Id;
5028 -- Build a new internal entity and append it to the list of shadow
5029 -- entities available through the limited-header
5031 ------------------------------
5032 -- Decorate_Incomplete_Type --
5033 ------------------------------
5035 procedure Decorate_Incomplete_Type
5040 Set_Ekind (E, E_Incomplete_Type);
5041 Set_Scope (E, Scop);
5043 Set_Is_First_Subtype (E, True);
5044 Set_Stored_Constraint (E, No_Elist);
5045 Set_Full_View (E, Empty);
5046 Init_Size_Align (E);
5047 end Decorate_Incomplete_Type;
5049 --------------------------
5050 -- Decorate_Tagged_Type --
5051 --------------------------
5053 procedure Decorate_Tagged_Type
5061 Decorate_Incomplete_Type (T, Scop);
5062 Set_Is_Tagged_Type (T);
5064 -- Build corresponding class_wide type, if not previously done
5066 -- Note: The class-wide entity is shared by the limited-view
5067 -- and the full-view.
5069 if No (Class_Wide_Type (T)) then
5070 CW := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
5072 -- Set parent to be the same as the parent of the tagged type.
5073 -- We need a parent field set, and it is supposed to point to
5074 -- the declaration of the type. The tagged type declaration
5075 -- essentially declares two separate types, the tagged type
5076 -- itself and the corresponding class-wide type, so it is
5077 -- reasonable for the parent fields to point to the declaration
5080 Set_Parent (CW, Parent (T));
5082 -- Set remaining fields of classwide type
5084 Set_Ekind (CW, E_Class_Wide_Type);
5086 Set_Scope (CW, Scop);
5087 Set_Is_Tagged_Type (CW);
5088 Set_Is_First_Subtype (CW, True);
5089 Init_Size_Align (CW);
5090 Set_Has_Unknown_Discriminants (CW, True);
5091 Set_Class_Wide_Type (CW, CW);
5092 Set_Equivalent_Type (CW, Empty);
5093 Set_From_With_Type (CW, From_With_Type (T));
5095 -- Link type to its class-wide type
5097 Set_Class_Wide_Type (T, CW);
5099 end Decorate_Tagged_Type;
5101 ------------------------------------
5102 -- Decorate_Package_Specification --
5103 ------------------------------------
5105 procedure Decorate_Package_Specification (P : Entity_Id) is
5107 -- Place only the most basic attributes
5109 Set_Ekind (P, E_Package);
5110 Set_Etype (P, Standard_Void_Type);
5111 end Decorate_Package_Specification;
5113 --------------------------------
5114 -- New_Internal_Shadow_Entity --
5115 --------------------------------
5117 function New_Internal_Shadow_Entity
5118 (Kind : Entity_Kind;
5119 Sloc_Value : Source_Ptr;
5120 Id_Char : Character) return Entity_Id
5122 E : constant Entity_Id :=
5123 Make_Defining_Identifier (Sloc_Value,
5124 Chars => New_Internal_Name (Id_Char));
5127 Set_Ekind (E, Kind);
5128 Set_Is_Internal (E, True);
5130 if Kind in Type_Kind then
5131 Init_Size_Align (E);
5134 Append_Entity (E, Lim_Header);
5137 end New_Internal_Shadow_Entity;
5143 procedure Build_Chain
5145 First_Decl : Node_Id)
5147 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
5148 Is_Tagged : Boolean;
5153 while Present (Decl) loop
5155 -- For each library_package_declaration in the environment, there
5156 -- is an implicit declaration of a *limited view* of that library
5157 -- package. The limited view of a package contains:
5159 -- * For each nested package_declaration, a declaration of the
5160 -- limited view of that package, with the same defining-
5161 -- program-unit name.
5163 -- * For each type_declaration in the visible part, an incomplete
5164 -- type-declaration with the same defining_identifier, whose
5165 -- completion is the type_declaration. If the type_declaration
5166 -- is tagged, then the incomplete_type_declaration is tagged
5169 -- The partial view is tagged if the declaration has the
5170 -- explicit keyword, or else if it is a type extension, both
5171 -- of which can be ascertained syntactically.
5173 if Nkind (Decl) = N_Full_Type_Declaration then
5175 (Nkind (Type_Definition (Decl)) = N_Record_Definition
5176 and then Tagged_Present (Type_Definition (Decl)))
5178 (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
5181 (Record_Extension_Part (Type_Definition (Decl))));
5183 Comp_Typ := Defining_Identifier (Decl);
5185 if not Analyzed_Unit then
5187 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5189 Decorate_Incomplete_Type (Comp_Typ, Scope);
5193 -- Create shadow entity for type
5195 Lim_Typ := New_Internal_Shadow_Entity
5196 (Kind => Ekind (Comp_Typ),
5197 Sloc_Value => Sloc (Comp_Typ),
5200 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5201 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5202 Set_From_With_Type (Lim_Typ);
5205 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5207 Decorate_Incomplete_Type (Lim_Typ, Scope);
5210 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5212 elsif Nkind_In (Decl, N_Private_Type_Declaration,
5213 N_Incomplete_Type_Declaration,
5214 N_Task_Type_Declaration,
5215 N_Protected_Type_Declaration)
5217 Comp_Typ := Defining_Identifier (Decl);
5220 Nkind_In (Decl, N_Private_Type_Declaration,
5221 N_Incomplete_Type_Declaration)
5222 and then Tagged_Present (Decl);
5224 if not Analyzed_Unit then
5226 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5228 Decorate_Incomplete_Type (Comp_Typ, Scope);
5232 Lim_Typ := New_Internal_Shadow_Entity
5233 (Kind => Ekind (Comp_Typ),
5234 Sloc_Value => Sloc (Comp_Typ),
5237 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5238 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5239 Set_From_With_Type (Lim_Typ);
5242 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5244 Decorate_Incomplete_Type (Lim_Typ, Scope);
5247 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5249 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5250 Comp_Typ := Defining_Identifier (Decl);
5252 if not Analyzed_Unit then
5253 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5256 -- Create shadow entity for type
5258 Lim_Typ := New_Internal_Shadow_Entity
5259 (Kind => Ekind (Comp_Typ),
5260 Sloc_Value => Sloc (Comp_Typ),
5263 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5264 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5265 Set_From_With_Type (Lim_Typ);
5267 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5268 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5270 elsif Nkind (Decl) = N_Package_Declaration then
5275 Spec : constant Node_Id := Specification (Decl);
5278 Comp_Typ := Defining_Unit_Name (Spec);
5280 if not Analyzed (Cunit (Unum)) then
5281 Decorate_Package_Specification (Comp_Typ);
5282 Set_Scope (Comp_Typ, Scope);
5285 Lim_Typ := New_Internal_Shadow_Entity
5286 (Kind => Ekind (Comp_Typ),
5287 Sloc_Value => Sloc (Comp_Typ),
5290 Decorate_Package_Specification (Lim_Typ);
5291 Set_Scope (Lim_Typ, Scope);
5293 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5294 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5295 Set_From_With_Type (Lim_Typ);
5297 -- Note: The non_limited_view attribute is not used
5298 -- for local packages.
5302 First_Decl => First (Visible_Declarations (Spec)));
5310 -- Start of processing for Build_Limited_Views
5313 pragma Assert (Limited_Present (N));
5315 -- A library_item mentioned in a limited_with_clause is a package
5316 -- declaration, not a subprogram declaration, generic declaration,
5317 -- generic instantiation, or package renaming declaration.
5319 case Nkind (Unit (Library_Unit (N))) is
5320 when N_Package_Declaration =>
5323 when N_Subprogram_Declaration =>
5324 Error_Msg_N ("subprograms not allowed in "
5325 & "limited with_clauses", N);
5328 when N_Generic_Package_Declaration |
5329 N_Generic_Subprogram_Declaration =>
5330 Error_Msg_N ("generics not allowed in "
5331 & "limited with_clauses", N);
5334 when N_Generic_Instantiation =>
5335 Error_Msg_N ("generic instantiations not allowed in "
5336 & "limited with_clauses", N);
5339 when N_Generic_Renaming_Declaration =>
5340 Error_Msg_N ("generic renamings not allowed in "
5341 & "limited with_clauses", N);
5344 when N_Subprogram_Renaming_Declaration =>
5345 Error_Msg_N ("renamed subprograms not allowed in "
5346 & "limited with_clauses", N);
5349 when N_Package_Renaming_Declaration =>
5350 Error_Msg_N ("renamed packages not allowed in "
5351 & "limited with_clauses", N);
5355 raise Program_Error;
5358 -- Check if the chain is already built
5360 Spec := Specification (Unit (Library_Unit (N)));
5362 if Limited_View_Installed (Spec) then
5366 Set_Ekind (P, E_Package);
5368 -- Build the header of the limited_view
5371 Make_Defining_Identifier (Sloc (N),
5372 Chars => New_Internal_Name (Id_Char => 'Z'));
5373 Set_Ekind (Lim_Header, E_Package);
5374 Set_Is_Internal (Lim_Header);
5375 Set_Limited_View (P, Lim_Header);
5377 -- Create the auxiliary chain. All the shadow entities are appended to
5378 -- the list of entities of the limited-view header
5382 First_Decl => First (Visible_Declarations (Spec)));
5384 -- Save the last built shadow entity. It is needed later to set the
5385 -- reference to the first shadow entity in the private part
5387 Last_Pub_Lim_E := Last_Lim_E;
5389 -- Ada 2005 (AI-262): Add the limited view of the private declarations
5390 -- Required to give support to limited-private-with clauses
5392 Build_Chain (Scope => P,
5393 First_Decl => First (Private_Declarations (Spec)));
5395 if Last_Pub_Lim_E /= Empty then
5396 Set_First_Private_Entity (Lim_Header,
5397 Next_Entity (Last_Pub_Lim_E));
5399 Set_First_Private_Entity (Lim_Header,
5403 Set_Limited_View_Installed (Spec);
5404 end Build_Limited_Views;
5406 -------------------------------
5407 -- Check_Body_Needed_For_SAL --
5408 -------------------------------
5410 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5412 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5413 -- Determine whether use of entity E might require the presence of its
5414 -- body. For a package this requires a recursive traversal of all nested
5417 ---------------------------
5418 -- Entity_Needed_For_SAL --
5419 ---------------------------
5421 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5425 if Is_Subprogram (E)
5426 and then Has_Pragma_Inline (E)
5430 elsif Ekind (E) = E_Generic_Function
5431 or else Ekind (E) = E_Generic_Procedure
5435 elsif Ekind (E) = E_Generic_Package
5437 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5438 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5442 elsif Ekind (E) = E_Package
5444 Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5445 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5447 Ent := First_Entity (E);
5448 while Present (Ent) loop
5449 if Entity_Needs_Body (Ent) then
5461 end Entity_Needs_Body;
5463 -- Start of processing for Check_Body_Needed_For_SAL
5466 if Ekind (Unit_Name) = E_Generic_Package
5468 Nkind (Unit_Declaration_Node (Unit_Name)) =
5469 N_Generic_Package_Declaration
5471 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5473 Set_Body_Needed_For_SAL (Unit_Name);
5475 elsif Ekind (Unit_Name) = E_Generic_Procedure
5476 or else Ekind (Unit_Name) = E_Generic_Function
5478 Set_Body_Needed_For_SAL (Unit_Name);
5480 elsif Is_Subprogram (Unit_Name)
5481 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5482 N_Subprogram_Declaration
5483 and then Has_Pragma_Inline (Unit_Name)
5485 Set_Body_Needed_For_SAL (Unit_Name);
5487 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5488 Check_Body_Needed_For_SAL
5489 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5491 elsif Ekind (Unit_Name) = E_Package
5492 and then Entity_Needs_Body (Unit_Name)
5494 Set_Body_Needed_For_SAL (Unit_Name);
5496 elsif Ekind (Unit_Name) = E_Package_Body
5497 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5499 Check_Body_Needed_For_SAL
5500 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5502 end Check_Body_Needed_For_SAL;
5504 --------------------
5505 -- Remove_Context --
5506 --------------------
5508 procedure Remove_Context (N : Node_Id) is
5509 Lib_Unit : constant Node_Id := Unit (N);
5512 -- If this is a child unit, first remove the parent units
5514 if Is_Child_Spec (Lib_Unit) then
5515 Remove_Parents (Lib_Unit);
5518 Remove_Context_Clauses (N);
5521 ----------------------------
5522 -- Remove_Context_Clauses --
5523 ----------------------------
5525 procedure Remove_Context_Clauses (N : Node_Id) is
5527 Unit_Name : Entity_Id;
5530 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5531 -- limited-views first and regular-views later (to maintain the
5534 -- First Phase: Remove limited_with context clauses
5536 Item := First (Context_Items (N));
5537 while Present (Item) loop
5539 -- We are interested only in with clauses which got installed
5542 if Nkind (Item) = N_With_Clause
5543 and then Limited_Present (Item)
5544 and then Limited_View_Installed (Item)
5546 Remove_Limited_With_Clause (Item);
5552 -- Second Phase: Loop through context items and undo regular
5553 -- with_clauses and use_clauses.
5555 Item := First (Context_Items (N));
5556 while Present (Item) loop
5558 -- We are interested only in with clauses which got installed on
5559 -- entry, as indicated by their Context_Installed flag set
5561 if Nkind (Item) = N_With_Clause
5562 and then Limited_Present (Item)
5563 and then Limited_View_Installed (Item)
5567 elsif Nkind (Item) = N_With_Clause
5568 and then Context_Installed (Item)
5570 -- Remove items from one with'ed unit
5572 Unit_Name := Entity (Name (Item));
5573 Remove_Unit_From_Visibility (Unit_Name);
5574 Set_Context_Installed (Item, False);
5576 elsif Nkind (Item) = N_Use_Package_Clause then
5577 End_Use_Package (Item);
5579 elsif Nkind (Item) = N_Use_Type_Clause then
5580 End_Use_Type (Item);
5585 end Remove_Context_Clauses;
5587 --------------------------------
5588 -- Remove_Limited_With_Clause --
5589 --------------------------------
5591 procedure Remove_Limited_With_Clause (N : Node_Id) is
5592 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5595 Lim_Header : Entity_Id;
5596 Lim_Typ : Entity_Id;
5600 pragma Assert (Limited_View_Installed (N));
5602 -- In case of limited with_clause on subprograms, generics, instances,
5603 -- or renamings, the corresponding error was previously posted and we
5604 -- have nothing to do here.
5606 if Nkind (P_Unit) /= N_Package_Declaration then
5610 P := Defining_Unit_Name (Specification (P_Unit));
5612 -- Handle child packages
5614 if Nkind (P) = N_Defining_Program_Unit_Name then
5615 P := Defining_Identifier (P);
5618 if Debug_Flag_I then
5619 Write_Str ("remove limited view of ");
5620 Write_Name (Chars (P));
5621 Write_Str (" from visibility");
5625 -- Prepare the removal of the shadow entities from visibility. The
5626 -- first element of the limited view is a header (an E_Package
5627 -- entity) that is used to reference the first shadow entity in the
5628 -- private part of the package
5630 Lim_Header := Limited_View (P);
5631 Lim_Typ := First_Entity (Lim_Header);
5633 -- Remove package and shadow entities from visibility if it has not
5636 if not Analyzed (P_Unit) then
5638 Set_Is_Immediately_Visible (P, False);
5640 while Present (Lim_Typ) loop
5642 Next_Entity (Lim_Typ);
5645 -- Otherwise this package has already appeared in the closure and its
5646 -- shadow entities must be replaced by its real entities. This code
5647 -- must be kept synchronized with the complementary code in Install
5648 -- Limited_Withed_Unit.
5651 -- Real entities that are type or subtype declarations were hidden
5652 -- from visibility at the point of installation of the limited-view.
5653 -- Now we recover the previous value of the hidden attribute.
5655 E := First_Entity (P);
5656 while Present (E) and then E /= First_Private_Entity (P) loop
5658 Set_Is_Hidden (E, Was_Hidden (E));
5664 while Present (Lim_Typ)
5665 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5667 -- Nested packages and child units were not unchained
5669 if Ekind (Lim_Typ) /= E_Package
5670 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5673 -- If the package has incomplete types, the limited view
5674 -- of the incomplete type is in fact never visible (AI05-129)
5675 -- but we have created a shadow entity E1 for it, that points
5676 -- to E2, a non-limited incomplete type. This in turn has a
5677 -- full view E3 that is the full declaration. There is a
5678 -- corresponding shadow entity E4. When reinstalling the
5679 -- non-limited view, E2 must become the current entity and
5680 -- E3 must be ignored.
5682 E := Non_Limited_View (Lim_Typ);
5684 if Present (Current_Entity (E))
5685 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
5686 and then Full_View (Current_Entity (E)) = E
5689 -- Lim_Typ is the limited view of a full type declaration
5690 -- that has a previous incomplete declaration, i.e. E3
5691 -- from the previous description. Nothing to insert.
5696 pragma Assert (not In_Chain (E));
5698 Prev := Current_Entity (Lim_Typ);
5700 if Prev = Lim_Typ then
5701 Set_Current_Entity (E);
5704 while Present (Prev)
5705 and then Homonym (Prev) /= Lim_Typ
5707 Prev := Homonym (Prev);
5710 if Present (Prev) then
5711 Set_Homonym (Prev, E);
5715 -- Preserve structure of homonym chain.
5717 Set_Homonym (E, Homonym (Lim_Typ));
5721 Next_Entity (Lim_Typ);
5725 -- Indicate that the limited view of the package is not installed
5727 Set_From_With_Type (P, False);
5728 Set_Limited_View_Installed (N, False);
5729 end Remove_Limited_With_Clause;
5731 --------------------
5732 -- Remove_Parents --
5733 --------------------
5735 procedure Remove_Parents (Lib_Unit : Node_Id) is
5738 P_Spec : Node_Id := Empty;
5740 Vis : constant Boolean :=
5741 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
5744 if Is_Child_Spec (Lib_Unit) then
5745 P_Spec := Parent_Spec (Lib_Unit);
5747 elsif Nkind (Lib_Unit) = N_Package_Body
5748 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
5750 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
5753 if Present (P_Spec) then
5756 P_Name := Get_Parent_Entity (P);
5757 Remove_Context_Clauses (P_Spec);
5758 End_Package_Scope (P_Name);
5759 Set_Is_Immediately_Visible (P_Name, Vis);
5761 -- Remove from visibility the siblings as well, which are directly
5762 -- visible while the parent is in scope.
5764 E := First_Entity (P_Name);
5765 while Present (E) loop
5766 if Is_Child_Unit (E) then
5767 Set_Is_Immediately_Visible (E, False);
5773 Set_In_Package_Body (P_Name, False);
5775 -- This is the recursive call to remove the context of any
5776 -- higher level parent. This recursion ensures that all parents
5777 -- are removed in the reverse order of their installation.
5783 ---------------------------------
5784 -- Remove_Private_With_Clauses --
5785 ---------------------------------
5787 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
5790 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
5791 -- Check whether a given unit appears in a regular with_clause.
5792 -- Used to determine whether a private_with_clause, implicit or
5793 -- explicit, should be ignored.
5795 ----------------------------
5796 -- In_Regular_With_Clause --
5797 ----------------------------
5799 function In_Regular_With_Clause (E : Entity_Id) return Boolean
5804 Item := First (Context_Items (Comp_Unit));
5805 while Present (Item) loop
5806 if Nkind (Item) = N_With_Clause
5807 and then Entity (Name (Item)) = E
5808 and then not Private_Present (Item)
5816 end In_Regular_With_Clause;
5818 -- Start of processing for Remove_Private_With_Clauses
5821 Item := First (Context_Items (Comp_Unit));
5822 while Present (Item) loop
5823 if Nkind (Item) = N_With_Clause
5824 and then Private_Present (Item)
5826 -- If private_with_clause is redundant, remove it from
5827 -- context, as a small optimization to subsequent handling
5828 -- of private_with clauses in other nested packages..
5830 if In_Regular_With_Clause (Entity (Name (Item))) then
5832 Nxt : constant Node_Id := Next (Item);
5838 elsif Limited_Present (Item) then
5839 if not Limited_View_Installed (Item) then
5840 Remove_Limited_With_Clause (Item);
5846 Remove_Unit_From_Visibility (Entity (Name (Item)));
5847 Set_Context_Installed (Item, False);
5855 end Remove_Private_With_Clauses;
5857 ---------------------------------
5858 -- Remove_Unit_From_Visibility --
5859 ---------------------------------
5861 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
5862 P : constant Entity_Id := Scope (Unit_Name);
5865 if Debug_Flag_I then
5866 Write_Str ("remove unit ");
5867 Write_Name (Chars (Unit_Name));
5868 Write_Str (" from visibility");
5872 if P /= Standard_Standard then
5873 Set_Is_Visible_Child_Unit (Unit_Name, False);
5876 Set_Is_Potentially_Use_Visible (Unit_Name, False);
5877 Set_Is_Immediately_Visible (Unit_Name, False);
5878 end Remove_Unit_From_Visibility;
5893 procedure Unchain (E : Entity_Id) is
5897 Prev := Current_Entity (E);
5903 Set_Name_Entity_Id (Chars (E), Homonym (E));
5906 while Present (Prev)
5907 and then Homonym (Prev) /= E
5909 Prev := Homonym (Prev);
5912 if Present (Prev) then
5913 Set_Homonym (Prev, Homonym (E));
5917 if Debug_Flag_I then
5918 Write_Str (" (homonym) unchain ");
5919 Write_Name (Chars (E));