1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Util; use Exp_Util;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Inline; use Inline;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rtsfind; use Rtsfind;
48 with Sem_Ch6; use Sem_Ch6;
49 with Sem_Ch7; use Sem_Ch7;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Dist; use Sem_Dist;
52 with Sem_Prag; use Sem_Prag;
53 with Sem_Util; use Sem_Util;
54 with Sem_Warn; use Sem_Warn;
55 with Stand; use Stand;
56 with Sinfo; use Sinfo;
57 with Sinfo.CN; use Sinfo.CN;
58 with Sinput; use Sinput;
59 with Snames; use Snames;
60 with Style; use Style;
61 with Stylesw; use Stylesw;
62 with Tbuild; use Tbuild;
63 with Ttypes; use Ttypes;
64 with Uname; use Uname;
66 package body Sem_Ch10 is
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Analyze_Context (N : Node_Id);
73 -- Analyzes items in the context clause of compilation unit
75 procedure Build_Limited_Views (N : Node_Id);
76 -- Build and decorate the list of shadow entities for a package mentioned
77 -- in a limited_with clause. If the package was not previously analyzed
78 -- then it also performs a basic decoration of the real entities; this
79 -- is required to do not pass non-decorated entities to the back-end.
80 -- Implements Ada 2005 (AI-50217).
82 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
83 -- Check whether the source for the body of a compilation unit must
84 -- be included in a standalone library.
86 procedure Check_With_Type_Clauses (N : Node_Id);
87 -- If N is a body, verify that any with_type clauses on the spec, or
88 -- on the spec of any parent, have a matching with_clause.
90 procedure Check_Private_Child_Unit (N : Node_Id);
91 -- If a with_clause mentions a private child unit, the compilation
92 -- unit must be a member of the same family, as described in 10.1.2 (8).
94 procedure Check_Stub_Level (N : Node_Id);
95 -- Verify that a stub is declared immediately within a compilation unit,
96 -- and not in an inner frame.
98 procedure Expand_With_Clause (Nam : Node_Id; N : Node_Id);
99 -- When a child unit appears in a context clause, the implicit withs on
100 -- parents are made explicit, and with clauses are inserted in the context
101 -- clause before the one for the child. If a parent in the with_clause
102 -- is a renaming, the implicit with_clause is on the renaming whose name
103 -- is mentioned in the with_clause, and not on the package it renames.
104 -- N is the compilation unit whose list of context items receives the
105 -- implicit with_clauses.
107 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
108 -- Get defining entity of parent unit of a child unit. In most cases this
109 -- is the defining entity of the unit, but for a child instance whose
110 -- parent needs a body for inlining, the instantiation node of the parent
111 -- has not yet been rewritten as a package declaration, and the entity has
112 -- to be retrieved from the Instance_Spec of the unit.
114 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
115 -- If the main unit is a child unit, implicit withs are also added for
116 -- all its ancestors.
118 procedure Install_Context_Clauses (N : Node_Id);
119 -- Subsidiary to Install_Context and Install_Parents. Process only with_
120 -- and use_clauses for current unit and its library unit if any.
122 procedure Install_Limited_Context_Clauses (N : Node_Id);
123 -- Subsidiary to Install_Context. Process only limited with_clauses
124 -- for current unit. Implements Ada 2005 (AI-50217).
126 procedure Install_Limited_Withed_Unit (N : Node_Id);
127 -- Place shadow entities for a limited_with package in the visibility
128 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
130 procedure Install_Withed_Unit
131 (With_Clause : Node_Id;
132 Private_With_OK : Boolean := False);
133 -- If the unit is not a child unit, make unit immediately visible.
134 -- The caller ensures that the unit is not already currently installed.
135 -- The flag Private_With_OK is set true in Install_Private_With_Clauses,
136 -- which is called when compiling the private part of a package, or
137 -- installing the private declarations of a parent unit.
139 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
140 -- This procedure establishes the context for the compilation of a child
141 -- unit. If Lib_Unit is a child library spec then the context of the parent
142 -- is installed, and the parent itself made immediately visible, so that
143 -- the child unit is processed in the declarative region of the parent.
144 -- Install_Parents makes a recursive call to itself to ensure that all
145 -- parents are loaded in the nested case. If Lib_Unit is a library body,
146 -- the only effect of Install_Parents is to install the private decls of
147 -- the parents, because the visible parent declarations will have been
148 -- installed as part of the context of the corresponding spec.
150 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
151 -- In the compilation of a child unit, a child of any of the ancestor
152 -- units is directly visible if it is visible, because the parent is in
153 -- an enclosing scope. Iterate over context to find child units of U_Name
154 -- or of some ancestor of it.
156 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
157 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
158 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
159 -- a library spec that has a parent. If the call to Is_Child_Spec returns
160 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
161 -- compilation unit for the parent spec.
163 -- Lib_Unit can also be a subprogram body that acts as its own spec. If
164 -- the Parent_Spec is non-empty, this is also a child unit.
166 procedure Remove_With_Type_Clause (Name : Node_Id);
167 -- Remove imported type and its enclosing package from visibility, and
168 -- remove attributes of imported type so they don't interfere with its
169 -- analysis (should it appear otherwise in the context).
171 procedure Remove_Context_Clauses (N : Node_Id);
172 -- Subsidiary of previous one. Remove use_ and with_clauses
174 procedure Remove_Limited_With_Clause (N : Node_Id);
175 -- Remove from visibility the shadow entities introduced for a package
176 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
178 procedure Remove_Parents (Lib_Unit : Node_Id);
179 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
180 -- contexts established by the corresponding call to Install_Parents are
181 -- removed. Remove_Parents contains a recursive call to itself to ensure
182 -- that all parents are removed in the nested case.
184 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
185 -- Reset all visibility flags on unit after compiling it, either as a
186 -- main unit or as a unit in the context.
188 procedure Unchain (E : Entity_Id);
189 -- Remove single entity from visibility list
191 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
192 -- Common processing for all stubs (subprograms, tasks, packages, and
193 -- protected cases). N is the stub to be analyzed. Once the subunit
194 -- name is established, load and analyze. Nam is the non-overloadable
195 -- entity for which the proper body provides a completion. Subprogram
196 -- stubs are handled differently because they can be declarations.
198 --------------------------
199 -- Limited_With_Clauses --
200 --------------------------
202 -- Limited_With clauses are the mechanism chosen for Ada05 to support
203 -- mutually recursive types declared in different units. A limited_with
204 -- clause that names package P in the context of unit U makes the types
205 -- declared in the visible part of P available within U, but with the
206 -- restriction that these types can only be used as incomplete types.
207 -- The limited_with clause does not impose a semantic dependence on P,
208 -- and it is possible for two packages to have limited_with_clauses on
209 -- each other without creating an elaboration circularity.
211 -- To support this feature, the analysis of a limited_with clause must
212 -- create an abbreviated view of the package, without performing any
213 -- semantic analysis on it. This "package abstract" contains shadow
214 -- types that are in one-one correspondence with the real types in the
215 -- package, and that have the properties of incomplete types.
217 -- The implementation creates two element lists: one to chain the shadow
218 -- entities, and one to chain the corresponding type entities in the tree
219 -- of the package. Links between corresponding entities in both chains
220 -- allow the compiler to select the proper view of a given type, depending
221 -- on the context. Note that in contrast with the handling of private
222 -- types, the limited view and the non-limited view of a type are treated
223 -- as separate entities, and no entity exchange needs to take place, which
224 -- makes the implementation must simpler than could be feared.
226 ------------------------------
227 -- Analyze_Compilation_Unit --
228 ------------------------------
230 procedure Analyze_Compilation_Unit (N : Node_Id) is
231 Unit_Node : constant Node_Id := Unit (N);
232 Lib_Unit : Node_Id := Library_Unit (N);
234 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
235 Par_Spec_Name : Unit_Name_Type;
236 Unum : Unit_Number_Type;
238 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
239 -- Generate cross-reference information for the parents of child units.
240 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
242 --------------------------------
243 -- Generate_Parent_References --
244 --------------------------------
246 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
248 P_Name : Entity_Id := P_Id;
251 Pref := Name (Parent (Defining_Entity (N)));
253 if Nkind (Pref) = N_Expanded_Name then
255 -- Done already, if the unit has been compiled indirectly as
256 -- part of the closure of its context because of inlining.
261 while Nkind (Pref) = N_Selected_Component loop
262 Change_Selected_Component_To_Expanded_Name (Pref);
263 Set_Entity (Pref, P_Name);
264 Set_Etype (Pref, Etype (P_Name));
265 Generate_Reference (P_Name, Pref, 'r');
266 Pref := Prefix (Pref);
267 P_Name := Scope (P_Name);
270 -- The guard here on P_Name is to handle the error condition where
271 -- the parent unit is missing because the file was not found.
273 if Present (P_Name) then
274 Set_Entity (Pref, P_Name);
275 Set_Etype (Pref, Etype (P_Name));
276 Generate_Reference (P_Name, Pref, 'r');
277 Style.Check_Identifier (Pref, P_Name);
279 end Generate_Parent_References;
281 -- Start of processing for Analyze_Compilation_Unit
284 Process_Compilation_Unit_Pragmas (N);
286 -- If the unit is a subunit whose parent has not been analyzed (which
287 -- indicates that the main unit is a subunit, either the current one or
288 -- one of its descendents) then the subunit is compiled as part of the
289 -- analysis of the parent, which we proceed to do. Basically this gets
290 -- handled from the top down and we don't want to do anything at this
291 -- level (i.e. this subunit will be handled on the way down from the
292 -- parent), so at this level we immediately return. If the subunit
293 -- ends up not analyzed, it means that the parent did not contain a
294 -- stub for it, or that there errors were dectected in some ancestor.
296 if Nkind (Unit_Node) = N_Subunit
297 and then not Analyzed (Lib_Unit)
299 Semantics (Lib_Unit);
301 if not Analyzed (Proper_Body (Unit_Node)) then
302 if Serious_Errors_Detected > 0 then
303 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
305 Error_Msg_N ("missing stub for subunit", N);
312 -- Analyze context (this will call Sem recursively for with'ed units)
316 -- If the unit is a package body, the spec is already loaded and must
317 -- be analyzed first, before we analyze the body.
319 if Nkind (Unit_Node) = N_Package_Body then
321 -- If no Lib_Unit, then there was a serious previous error, so
322 -- just ignore the entire analysis effort
324 if No (Lib_Unit) then
328 Semantics (Lib_Unit);
329 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
331 -- Verify that the library unit is a package declaration
333 if Nkind (Unit (Lib_Unit)) /= N_Package_Declaration
335 Nkind (Unit (Lib_Unit)) /= N_Generic_Package_Declaration
338 ("no legal package declaration for package body", N);
341 -- Otherwise, the entity in the declaration is visible. Update
342 -- the version to reflect dependence of this body on the spec.
345 Spec_Id := Defining_Entity (Unit (Lib_Unit));
346 Set_Is_Immediately_Visible (Spec_Id, True);
347 Version_Update (N, Lib_Unit);
349 if Nkind (Defining_Unit_Name (Unit_Node))
350 = N_Defining_Program_Unit_Name
352 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
357 -- If the unit is a subprogram body, then we similarly need to analyze
358 -- its spec. However, things are a little simpler in this case, because
359 -- here, this analysis is done only for error checking and consistency
360 -- purposes, so there's nothing else to be done.
362 elsif Nkind (Unit_Node) = N_Subprogram_Body then
363 if Acts_As_Spec (N) then
365 -- If the subprogram body is a child unit, we must create a
366 -- declaration for it, in order to properly load the parent(s).
367 -- After this, the original unit does not acts as a spec, because
368 -- there is an explicit one. If this unit appears in a context
369 -- clause, then an implicit with on the parent will be added when
370 -- installing the context. If this is the main unit, there is no
371 -- Unit_Table entry for the declaration, (It has the unit number
372 -- of the main unit) and code generation is unaffected.
374 Unum := Get_Cunit_Unit_Number (N);
375 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
377 if Par_Spec_Name /= No_Name then
380 (Load_Name => Par_Spec_Name,
385 if Unum /= No_Unit then
387 -- Build subprogram declaration and attach parent unit to it
388 -- This subprogram declaration does not come from source,
389 -- Nevertheless the backend must generate debugging info for
390 -- it, and this must be indicated explicitly.
393 Loc : constant Source_Ptr := Sloc (N);
394 SCS : constant Boolean :=
395 Get_Comes_From_Source_Default;
398 Set_Comes_From_Source_Default (False);
400 Make_Compilation_Unit (Loc,
401 Context_Items => New_Copy_List (Context_Items (N)),
403 Make_Subprogram_Declaration (Sloc (N),
406 (Specification (Unit_Node))),
408 Make_Compilation_Unit_Aux (Loc));
410 Set_Library_Unit (N, Lib_Unit);
411 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
412 Semantics (Lib_Unit);
413 Set_Acts_As_Spec (N, False);
414 Set_Needs_Debug_Info (Defining_Entity (Unit (Lib_Unit)));
415 Set_Comes_From_Source_Default (SCS);
420 -- Here for subprogram with separate declaration
423 Semantics (Lib_Unit);
424 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
425 Version_Update (N, Lib_Unit);
428 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
429 N_Defining_Program_Unit_Name
431 Generate_Parent_References (
432 Specification (Unit_Node),
433 Scope (Defining_Entity (Unit (Lib_Unit))));
437 -- If it is a child unit, the parent must be elaborated first
438 -- and we update version, since we are dependent on our parent.
440 if Is_Child_Spec (Unit_Node) then
442 -- The analysis of the parent is done with style checks off
445 Save_Style_Check : constant Boolean := Style_Check;
446 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
447 Cunit_Boolean_Restrictions_Save;
450 if not GNAT_Mode then
451 Style_Check := False;
454 Semantics (Parent_Spec (Unit_Node));
455 Version_Update (N, Parent_Spec (Unit_Node));
456 Style_Check := Save_Style_Check;
457 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
461 -- With the analysis done, install the context. Note that we can't
462 -- install the context from the with clauses as we analyze them,
463 -- because each with clause must be analyzed in a clean visibility
464 -- context, so we have to wait and install them all at once.
468 if Is_Child_Spec (Unit_Node) then
470 -- Set the entities of all parents in the program_unit_name
472 Generate_Parent_References (
473 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
476 -- All components of the context: with-clauses, library unit, ancestors
477 -- if any, (and their context) are analyzed and installed. Now analyze
478 -- the unit itself, which is either a package, subprogram spec or body.
482 -- The above call might have made Unit_Node an N_Subprogram_Body
483 -- from something else, so propagate any Acts_As_Spec flag.
485 if Nkind (Unit_Node) = N_Subprogram_Body
486 and then Acts_As_Spec (Unit_Node)
488 Set_Acts_As_Spec (N);
491 -- Register predefined units in Rtsfind
494 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
496 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
497 Set_RTU_Loaded (Unit_Node);
501 -- Treat compilation unit pragmas that appear after the library unit
503 if Present (Pragmas_After (Aux_Decls_Node (N))) then
505 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
508 while Present (Prag_Node) loop
515 -- Generate distribution stubs if requested and no error
518 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
520 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
521 and then not Fatal_Error (Main_Unit)
523 if Is_RCI_Pkg_Spec_Or_Body (N) then
525 -- Regular RCI package
527 Add_Stub_Constructs (N);
529 elsif (Nkind (Unit_Node) = N_Package_Declaration
530 and then Is_Shared_Passive (Defining_Entity
531 (Specification (Unit_Node))))
532 or else (Nkind (Unit_Node) = N_Package_Body
534 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
536 -- Shared passive package
538 Add_Stub_Constructs (N);
540 elsif Nkind (Unit_Node) = N_Package_Instantiation
542 Is_Remote_Call_Interface
543 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
545 -- Instantiation of a RCI generic package
547 Add_Stub_Constructs (N);
552 if Nkind (Unit_Node) = N_Package_Declaration
553 or else Nkind (Unit_Node) in N_Generic_Declaration
554 or else Nkind (Unit_Node) = N_Package_Renaming_Declaration
555 or else Nkind (Unit_Node) = N_Subprogram_Declaration
557 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
559 -- If the unit is an instantiation whose body will be elaborated
560 -- for inlining purposes, use the the proper entity of the instance.
562 elsif Nkind (Unit_Node) = N_Package_Instantiation
563 and then not Error_Posted (Unit_Node)
565 Remove_Unit_From_Visibility
566 (Defining_Entity (Instance_Spec (Unit_Node)));
568 elsif Nkind (Unit_Node) = N_Package_Body
569 or else (Nkind (Unit_Node) = N_Subprogram_Body
570 and then not Acts_As_Spec (Unit_Node))
572 -- Bodies that are not the main unit are compiled if they
573 -- are generic or contain generic or inlined units. Their
574 -- analysis brings in the context of the corresponding spec
575 -- (unit declaration) which must be removed as well, to
576 -- return the compilation environment to its proper state.
578 Remove_Context (Lib_Unit);
579 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
582 -- Last step is to deinstall the context we just installed
583 -- as well as the unit just compiled.
587 -- If this is the main unit and we are generating code, we must
588 -- check that all generic units in the context have a body if they
589 -- need it, even if they have not been instantiated. In the absence
590 -- of .ali files for generic units, we must force the load of the body,
591 -- just to produce the proper error if the body is absent. We skip this
592 -- verification if the main unit itself is generic.
594 if Get_Cunit_Unit_Number (N) = Main_Unit
595 and then Operating_Mode = Generate_Code
596 and then Expander_Active
598 -- Check whether the source for the body of the unit must be
599 -- included in a standalone library.
601 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
603 -- Indicate that the main unit is now analyzed, to catch possible
604 -- circularities between it and generic bodies. Remove main unit
605 -- from visibility. This might seem superfluous, but the main unit
606 -- must not be visible in the generic body expansions that follow.
608 Set_Analyzed (N, True);
609 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
614 Un : Unit_Number_Type;
616 Save_Style_Check : constant Boolean := Style_Check;
617 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
618 Cunit_Boolean_Restrictions_Save;
621 Item := First (Context_Items (N));
622 while Present (Item) loop
624 -- Ada 2005 (AI-50217): Do not consider limited-withed units
626 if Nkind (Item) = N_With_Clause
627 and then not Implicit_With (Item)
628 and then not Limited_Present (Item)
630 Nam := Entity (Name (Item));
632 if (Is_Generic_Subprogram (Nam)
633 and then not Is_Intrinsic_Subprogram (Nam))
634 or else (Ekind (Nam) = E_Generic_Package
635 and then Unit_Requires_Body (Nam))
637 Style_Check := False;
639 if Present (Renamed_Object (Nam)) then
642 (Load_Name => Get_Body_Name
644 (Unit_Declaration_Node
645 (Renamed_Object (Nam)))),
653 (Load_Name => Get_Body_Name
654 (Get_Unit_Name (Item)),
663 ("body of generic unit& not found", Item, Nam);
666 elsif not Analyzed (Cunit (Un))
667 and then Un /= Main_Unit
668 and then not Fatal_Error (Un)
670 Style_Check := False;
671 Semantics (Cunit (Un));
679 Style_Check := Save_Style_Check;
680 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
684 -- Deal with creating elaboration Boolean if needed. We create an
685 -- elaboration boolean only for units that come from source since
686 -- units manufactured by the compiler never need elab checks.
688 if Comes_From_Source (N)
690 (Nkind (Unit (N)) = N_Package_Declaration or else
691 Nkind (Unit (N)) = N_Generic_Package_Declaration or else
692 Nkind (Unit (N)) = N_Subprogram_Declaration or else
693 Nkind (Unit (N)) = N_Generic_Subprogram_Declaration)
696 Loc : constant Source_Ptr := Sloc (N);
697 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
700 Spec_Id := Defining_Entity (Unit (N));
701 Generate_Definition (Spec_Id);
703 -- See if an elaboration entity is required for possible
704 -- access before elaboration checking. Note that we must
705 -- allow for this even if -gnatE is not set, since a client
706 -- may be compiled in -gnatE mode and reference the entity.
708 -- Case of units which do not require elaboration checks
711 -- Pure units do not need checks
715 -- Preelaborated units do not need checks
717 or else Is_Preelaborated (Spec_Id)
719 -- No checks needed if pagma Elaborate_Body present
721 or else Has_Pragma_Elaborate_Body (Spec_Id)
723 -- No checks needed if unit does not require a body
725 or else not Unit_Requires_Body (Spec_Id)
727 -- No checks needed for predefined files
729 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
731 -- No checks required if no separate spec
733 or else Acts_As_Spec (N)
735 -- This is a case where we only need the entity for
736 -- checking to prevent multiple elaboration checks.
738 Set_Elaboration_Entity_Required (Spec_Id, False);
740 -- Case of elaboration entity is required for access before
741 -- elaboration checking (so certainly we must build it!)
744 Set_Elaboration_Entity_Required (Spec_Id, True);
747 Build_Elaboration_Entity (N, Spec_Id);
751 -- Finally, freeze the compilation unit entity. This for sure is needed
752 -- because of some warnings that can be output (see Freeze_Subprogram),
753 -- but may in general be required. If freezing actions result, place
754 -- them in the compilation unit actions list, and analyze them.
757 Loc : constant Source_Ptr := Sloc (N);
758 L : constant List_Id :=
759 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), Loc);
762 while Is_Non_Empty_List (L) loop
763 Insert_Library_Level_Action (Remove_Head (L));
769 if Nkind (Unit_Node) = N_Package_Declaration
770 and then Get_Cunit_Unit_Number (N) /= Main_Unit
771 and then Expander_Active
774 Save_Style_Check : constant Boolean := Style_Check;
775 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
776 Options : Style_Check_Options;
779 Save_Style_Check_Options (Options);
780 Reset_Style_Check_Options;
781 Opt.Warning_Mode := Suppress;
782 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
784 Reset_Style_Check_Options;
785 Set_Style_Check_Options (Options);
786 Style_Check := Save_Style_Check;
787 Warning_Mode := Save_Warning;
790 end Analyze_Compilation_Unit;
792 ---------------------
793 -- Analyze_Context --
794 ---------------------
796 procedure Analyze_Context (N : Node_Id) is
797 Ukind : constant Node_Kind := Nkind (Unit (N));
801 -- Loop through context items. This is done in two:
802 -- a) The first pass analyzes non-limited with-clauses
803 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
805 Item := First (Context_Items (N));
806 while Present (Item) loop
808 -- For with clause, analyze the with clause, and then update
809 -- the version, since we are dependent on a unit that we with.
811 if Nkind (Item) = N_With_Clause
812 and then not Limited_Present (Item)
815 -- Skip analyzing with clause if no unit, nothing to do (this
816 -- happens for a with that references a non-existant unit)
818 if Present (Library_Unit (Item)) then
822 if not Implicit_With (Item) then
823 Version_Update (N, Library_Unit (Item));
826 -- But skip use clauses at this stage, since we don't want to do
827 -- any installing of potentially use visible entities until we
828 -- we actually install the complete context (in Install_Context).
829 -- Otherwise things can get installed in the wrong context.
830 -- Similarly, pragmas are analyzed in Install_Context, after all
831 -- the implicit with's on parent units are generated.
840 -- Second pass: examine all limited_with clauses
842 Item := First (Context_Items (N));
843 while Present (Item) loop
844 if Nkind (Item) = N_With_Clause
845 and then Limited_Present (Item)
847 -- No need to check errors on implicitly generated limited-with
850 if not Implicit_With (Item) then
852 -- Check compilation unit containing the limited-with clause
854 if Ukind /= N_Package_Declaration
855 and then Ukind /= N_Subprogram_Declaration
856 and then Ukind /= N_Subprogram_Renaming_Declaration
857 and then Ukind /= N_Generic_Package_Declaration
858 and then Ukind /= N_Generic_Package_Renaming_Declaration
859 and then Ukind /= N_Generic_Subprogram_Declaration
860 and then Ukind /= N_Generic_Procedure_Renaming_Declaration
861 and then Ukind /= N_Package_Instantiation
862 and then Ukind /= N_Package_Renaming_Declaration
863 and then Ukind /= N_Procedure_Instantiation
865 Error_Msg_N ("limited with_clause not allowed here", Item);
867 -- Check wrong use of a limited with clause applied to the
868 -- compilation unit containing the limited-with clause.
871 -- package P.Q is ...
873 elsif Unit (Library_Unit (Item)) = Unit (N) then
874 Error_Msg_N ("wrong use of limited-with clause", Item);
876 -- Check wrong use of limited-with clause applied to some
877 -- immediate ancestor.
879 elsif Is_Child_Spec (Unit (N)) then
881 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
885 P := Parent_Spec (Unit (N));
887 if Unit (P) = Lib_U then
888 Error_Msg_N ("limited with_clause of immediate "
889 & "ancestor not allowed", Item);
893 exit when not Is_Child_Spec (Unit (P));
894 P := Parent_Spec (Unit (P));
899 -- Check if the limited-withed unit is already visible through
900 -- some context clause of the current compilation unit or some
901 -- ancestor of the current compilation unit.
904 Lim_Unit_Name : constant Node_Id := Name (Item);
912 It := First (Context_Items (Comp_Unit));
913 while Present (It) loop
915 and then Nkind (It) = N_With_Clause
916 and then not Limited_Present (It)
918 (Nkind (Unit (Library_Unit (It)))
919 = N_Package_Declaration
921 Nkind (Unit (Library_Unit (It)))
922 = N_Package_Renaming_Declaration)
924 if Nkind (Unit (Library_Unit (It)))
925 = N_Package_Declaration
927 Unit_Name := Name (It);
929 Unit_Name := Name (Unit (Library_Unit (It)));
932 -- Check if the named package (or some ancestor)
933 -- leaves visible the full-view of the unit given
934 -- in the limited-with clause
937 if Designate_Same_Unit (Lim_Unit_Name,
940 Error_Msg_Sloc := Sloc (It);
942 ("unlimited view visible through the"
943 & " context clause found #",
946 ("simultaneous visibility of the limited"
947 & " and unlimited views not allowed"
951 elsif Nkind (Unit_Name) = N_Identifier then
955 Unit_Name := Prefix (Unit_Name);
962 exit when not Is_Child_Spec (Unit (Comp_Unit));
964 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
969 -- Skip analyzing with clause if no unit, see above
971 if Present (Library_Unit (Item)) then
975 -- A limited_with does not impose an elaboration order, but
976 -- there is a semantic dependency for recompilation purposes.
978 if not Implicit_With (Item) then
979 Version_Update (N, Library_Unit (Item));
987 -------------------------------
988 -- Analyze_Package_Body_Stub --
989 -------------------------------
991 procedure Analyze_Package_Body_Stub (N : Node_Id) is
992 Id : constant Entity_Id := Defining_Identifier (N);
996 -- The package declaration must be in the current declarative part
998 Check_Stub_Level (N);
999 Nam := Current_Entity_In_Scope (Id);
1001 if No (Nam) or else not Is_Package (Nam) then
1002 Error_Msg_N ("missing specification for package stub", N);
1004 elsif Has_Completion (Nam)
1005 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1007 Error_Msg_N ("duplicate or redundant stub for package", N);
1010 -- Indicate that the body of the package exists. If we are doing
1011 -- only semantic analysis, the stub stands for the body. If we are
1012 -- generating code, the existence of the body will be confirmed
1013 -- when we load the proper body.
1015 Set_Has_Completion (Nam);
1016 Set_Scope (Defining_Entity (N), Current_Scope);
1017 Generate_Reference (Nam, Id, 'b');
1018 Analyze_Proper_Body (N, Nam);
1020 end Analyze_Package_Body_Stub;
1022 -------------------------
1023 -- Analyze_Proper_Body --
1024 -------------------------
1026 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1027 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1028 Unum : Unit_Number_Type;
1030 procedure Optional_Subunit;
1031 -- This procedure is called when the main unit is a stub, or when we
1032 -- are not generating code. In such a case, we analyze the subunit if
1033 -- present, which is user-friendly and in fact required for ASIS, but
1034 -- we don't complain if the subunit is missing.
1036 ----------------------
1037 -- Optional_Subunit --
1038 ----------------------
1040 procedure Optional_Subunit is
1041 Comp_Unit : Node_Id;
1044 -- Try to load subunit, but ignore any errors that occur during
1045 -- the loading of the subunit, by using the special feature in
1046 -- Errout to ignore all errors. Note that Fatal_Error will still
1047 -- be set, so we will be able to check for this case below.
1049 if not ASIS_Mode then
1050 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1055 (Load_Name => Subunit_Name,
1060 if not ASIS_Mode then
1061 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1064 -- All done if we successfully loaded the subunit
1067 and then (not Fatal_Error (Unum) or else Try_Semantics)
1069 Comp_Unit := Cunit (Unum);
1071 -- If the file was empty or seriously mangled, the unit
1072 -- itself may be missing.
1074 if No (Unit (Comp_Unit)) then
1076 ("subunit does not contain expected proper body", N);
1078 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1080 ("expected SEPARATE subunit, found child unit",
1081 Cunit_Entity (Unum));
1083 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1084 Analyze_Subunit (Comp_Unit);
1085 Set_Library_Unit (N, Comp_Unit);
1088 elsif Unum = No_Unit
1089 and then Present (Nam)
1091 if Is_Protected_Type (Nam) then
1092 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1094 Set_Corresponding_Body (
1095 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1098 end Optional_Subunit;
1100 -- Start of processing for Analyze_Proper_Body
1103 -- If the subunit is already loaded, it means that the main unit
1104 -- is a subunit, and that the current unit is one of its parents
1105 -- which was being analyzed to provide the needed context for the
1106 -- analysis of the subunit. In this case we analyze the subunit and
1107 -- continue with the parent, without looking a subsequent subunits.
1109 if Is_Loaded (Subunit_Name) then
1111 -- If the proper body is already linked to the stub node,
1112 -- the stub is in a generic unit and just needs analyzing.
1114 if Present (Library_Unit (N)) then
1115 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1116 Analyze_Subunit (Library_Unit (N));
1118 -- Otherwise we must load the subunit and link to it
1121 -- Load the subunit, this must work, since we originally
1122 -- loaded the subunit earlier on. So this will not really
1123 -- load it, just give access to it.
1127 (Load_Name => Subunit_Name,
1132 -- And analyze the subunit in the parent context (note that we
1133 -- do not call Semantics, since that would remove the parent
1134 -- context). Because of this, we have to manually reset the
1135 -- compiler state to Analyzing since it got destroyed by Load.
1137 if Unum /= No_Unit then
1138 Compiler_State := Analyzing;
1140 -- Check that the proper body is a subunit and not a child
1141 -- unit. If the unit was previously loaded, the error will
1142 -- have been emitted when copying the generic node, so we
1143 -- just return to avoid cascaded errors.
1145 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1149 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1150 Analyze_Subunit (Cunit (Unum));
1151 Set_Library_Unit (N, Cunit (Unum));
1155 -- If the main unit is a subunit, then we are just performing semantic
1156 -- analysis on that subunit, and any other subunits of any parent unit
1157 -- should be ignored, except that if we are building trees for ASIS
1158 -- usage we want to annotate the stub properly.
1160 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1161 and then Subunit_Name /= Unit_Name (Main_Unit)
1167 -- But before we return, set the flag for unloaded subunits. This
1168 -- will suppress junk warnings of variables in the same declarative
1169 -- part (or a higher level one) that are in danger of looking unused
1170 -- when in fact there might be a declaration in the subunit that we
1171 -- do not intend to load.
1173 Unloaded_Subunits := True;
1176 -- If the subunit is not already loaded, and we are generating code,
1177 -- then this is the case where compilation started from the parent,
1178 -- and we are generating code for an entire subunit tree. In that
1179 -- case we definitely need to load the subunit.
1181 -- In order to continue the analysis with the rest of the parent,
1182 -- and other subunits, we load the unit without requiring its
1183 -- presence, and emit a warning if not found, rather than terminating
1184 -- the compilation abruptly, as for other missing file problems.
1186 elsif Original_Operating_Mode = Generate_Code then
1188 -- If the proper body is already linked to the stub node,
1189 -- the stub is in a generic unit and just needs analyzing.
1191 -- We update the version. Although we are not technically
1192 -- semantically dependent on the subunit, given our approach
1193 -- of macro substitution of subunits, it makes sense to
1194 -- include it in the version identification.
1196 if Present (Library_Unit (N)) then
1197 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1198 Analyze_Subunit (Library_Unit (N));
1199 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1201 -- Otherwise we must load the subunit and link to it
1206 (Load_Name => Subunit_Name,
1211 if Original_Operating_Mode = Generate_Code
1212 and then Unum = No_Unit
1214 Error_Msg_Name_1 := Subunit_Name;
1216 Get_File_Name (Subunit_Name, Subunit => True);
1218 ("subunit% in file{ not found!?", N);
1219 Subunits_Missing := True;
1222 -- Load_Unit may reset Compiler_State, since it may have been
1223 -- necessary to parse an additional units, so we make sure
1224 -- that we reset it to the Analyzing state.
1226 Compiler_State := Analyzing;
1229 and then (not Fatal_Error (Unum) or else Try_Semantics)
1231 if Debug_Flag_L then
1232 Write_Str ("*** Loaded subunit from stub. Analyze");
1237 Comp_Unit : constant Node_Id := Cunit (Unum);
1240 -- Check for child unit instead of subunit
1242 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1244 ("expected SEPARATE subunit, found child unit",
1245 Cunit_Entity (Unum));
1247 -- OK, we have a subunit, so go ahead and analyze it,
1248 -- and set Scope of entity in stub, for ASIS use.
1251 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1252 Analyze_Subunit (Comp_Unit);
1253 Set_Library_Unit (N, Comp_Unit);
1255 -- We update the version. Although we are not technically
1256 -- semantically dependent on the subunit, given our
1257 -- approach of macro substitution of subunits, it makes
1258 -- sense to include it in the version identification.
1260 Version_Update (Cunit (Main_Unit), Comp_Unit);
1266 -- The remaining case is when the subunit is not already loaded and
1267 -- we are not generating code. In this case we are just performing
1268 -- semantic analysis on the parent, and we are not interested in
1269 -- the subunit. For subprograms, analyze the stub as a body. For
1270 -- other entities the stub has already been marked as completed.
1276 end Analyze_Proper_Body;
1278 ----------------------------------
1279 -- Analyze_Protected_Body_Stub --
1280 ----------------------------------
1282 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1283 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1286 Check_Stub_Level (N);
1288 -- First occurence of name may have been as an incomplete type
1290 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1291 Nam := Full_View (Nam);
1295 or else not Is_Protected_Type (Etype (Nam))
1297 Error_Msg_N ("missing specification for Protected body", N);
1299 Set_Scope (Defining_Entity (N), Current_Scope);
1300 Set_Has_Completion (Etype (Nam));
1301 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1302 Analyze_Proper_Body (N, Etype (Nam));
1304 end Analyze_Protected_Body_Stub;
1306 ----------------------------------
1307 -- Analyze_Subprogram_Body_Stub --
1308 ----------------------------------
1310 -- A subprogram body stub can appear with or without a previous
1311 -- specification. If there is one, the analysis of the body will
1312 -- find it and verify conformance. The formals appearing in the
1313 -- specification of the stub play no role, except for requiring an
1314 -- additional conformance check. If there is no previous subprogram
1315 -- declaration, the stub acts as a spec, and provides the defining
1316 -- entity for the subprogram.
1318 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1322 Check_Stub_Level (N);
1324 -- Verify that the identifier for the stub is unique within this
1325 -- declarative part.
1327 if Nkind (Parent (N)) = N_Block_Statement
1328 or else Nkind (Parent (N)) = N_Package_Body
1329 or else Nkind (Parent (N)) = N_Subprogram_Body
1331 Decl := First (Declarations (Parent (N)));
1333 while Present (Decl)
1336 if Nkind (Decl) = N_Subprogram_Body_Stub
1337 and then (Chars (Defining_Unit_Name (Specification (Decl)))
1338 = Chars (Defining_Unit_Name (Specification (N))))
1340 Error_Msg_N ("identifier for stub is not unique", N);
1347 -- Treat stub as a body, which checks conformance if there is a previous
1348 -- declaration, or else introduces entity and its signature.
1350 Analyze_Subprogram_Body (N);
1351 Analyze_Proper_Body (N, Empty);
1352 end Analyze_Subprogram_Body_Stub;
1354 ---------------------
1355 -- Analyze_Subunit --
1356 ---------------------
1358 -- A subunit is compiled either by itself (for semantic checking)
1359 -- or as part of compiling the parent (for code generation). In
1360 -- either case, by the time we actually process the subunit, the
1361 -- parent has already been installed and analyzed. The node N is
1362 -- a compilation unit, whose context needs to be treated here,
1363 -- because we come directly here from the parent without calling
1364 -- Analyze_Compilation_Unit.
1366 -- The compilation context includes the explicit context of the
1367 -- subunit, and the context of the parent, together with the parent
1368 -- itself. In order to compile the current context, we remove the
1369 -- one inherited from the parent, in order to have a clean visibility
1370 -- table. We restore the parent context before analyzing the proper
1371 -- body itself. On exit, we remove only the explicit context of the
1374 procedure Analyze_Subunit (N : Node_Id) is
1375 Lib_Unit : constant Node_Id := Library_Unit (N);
1376 Par_Unit : constant Entity_Id := Current_Scope;
1378 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1379 Num_Scopes : Int := 0;
1380 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1381 Enclosing_Child : Entity_Id := Empty;
1382 Svg : constant Suppress_Array := Scope_Suppress;
1384 procedure Analyze_Subunit_Context;
1385 -- Capture names in use clauses of the subunit. This must be done
1386 -- before re-installing parent declarations, because items in the
1387 -- context must not be hidden by declarations local to the parent.
1389 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1390 -- Recursive procedure to restore scope of all ancestors of subunit,
1391 -- from outermost in. If parent is not a subunit, the call to install
1392 -- context installs context of spec and (if parent is a child unit)
1393 -- the context of its parents as well. It is confusing that parents
1394 -- should be treated differently in both cases, but the semantics are
1395 -- just not identical.
1397 procedure Re_Install_Use_Clauses;
1398 -- As part of the removal of the parent scope, the use clauses are
1399 -- removed, to be reinstalled when the context of the subunit has
1400 -- been analyzed. Use clauses may also have been affected by the
1401 -- analysis of the context of the subunit, so they have to be applied
1402 -- again, to insure that the compilation environment of the rest of
1403 -- the parent unit is identical.
1405 procedure Remove_Scope;
1406 -- Remove current scope from scope stack, and preserve the list
1407 -- of use clauses in it, to be reinstalled after context is analyzed.
1409 -----------------------------
1410 -- Analyze_Subunit_Context --
1411 -----------------------------
1413 procedure Analyze_Subunit_Context is
1416 Unit_Name : Entity_Id;
1419 Analyze_Context (N);
1420 Item := First (Context_Items (N));
1422 -- make withed units immediately visible. If child unit, make the
1423 -- ultimate parent immediately visible.
1425 while Present (Item) loop
1427 if Nkind (Item) = N_With_Clause then
1428 -- Protect the frontend against previous errors
1429 -- in context clauses
1431 if Nkind (Name (Item)) /= N_Selected_Component then
1432 Unit_Name := Entity (Name (Item));
1434 while Is_Child_Unit (Unit_Name) loop
1435 Set_Is_Visible_Child_Unit (Unit_Name);
1436 Unit_Name := Scope (Unit_Name);
1439 if not Is_Immediately_Visible (Unit_Name) then
1440 Set_Is_Immediately_Visible (Unit_Name);
1441 Set_Context_Installed (Item);
1445 elsif Nkind (Item) = N_Use_Package_Clause then
1446 Nam := First (Names (Item));
1448 while Present (Nam) loop
1453 elsif Nkind (Item) = N_Use_Type_Clause then
1454 Nam := First (Subtype_Marks (Item));
1456 while Present (Nam) loop
1465 Item := First (Context_Items (N));
1467 -- reset visibility of withed units. They will be made visible
1468 -- again when we install the subunit context.
1470 while Present (Item) loop
1472 if Nkind (Item) = N_With_Clause
1474 -- Protect the frontend against previous errors in context
1477 and then Nkind (Name (Item)) /= N_Selected_Component
1479 Unit_Name := Entity (Name (Item));
1481 while Is_Child_Unit (Unit_Name) loop
1482 Set_Is_Visible_Child_Unit (Unit_Name, False);
1483 Unit_Name := Scope (Unit_Name);
1486 if Context_Installed (Item) then
1487 Set_Is_Immediately_Visible (Unit_Name, False);
1488 Set_Context_Installed (Item, False);
1495 end Analyze_Subunit_Context;
1497 ------------------------
1498 -- Re_Install_Parents --
1499 ------------------------
1501 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
1505 if Nkind (Unit (L)) = N_Subunit then
1506 Re_Install_Parents (Library_Unit (L), Scope (Scop));
1509 Install_Context (L);
1511 -- If the subunit occurs within a child unit, we must restore the
1512 -- immediate visibility of any siblings that may occur in context.
1514 if Present (Enclosing_Child) then
1515 Install_Siblings (Enclosing_Child, L);
1520 if Scop /= Par_Unit then
1521 Set_Is_Immediately_Visible (Scop);
1524 E := First_Entity (Current_Scope);
1526 -- Make entities in scope visible again. For child units, restore
1527 -- visibility only if they are actually in context.
1529 while Present (E) loop
1530 if not Is_Child_Unit (E)
1531 or else Is_Visible_Child_Unit (E)
1533 Set_Is_Immediately_Visible (E);
1539 -- A subunit appears within a body, and for a nested subunits
1540 -- all the parents are bodies. Restore full visibility of their
1541 -- private entities.
1543 if Ekind (Scop) = E_Package then
1544 Set_In_Package_Body (Scop);
1545 Install_Private_Declarations (Scop);
1547 end Re_Install_Parents;
1549 ----------------------------
1550 -- Re_Install_Use_Clauses --
1551 ----------------------------
1553 procedure Re_Install_Use_Clauses is
1557 for J in reverse 1 .. Num_Scopes loop
1558 U := Use_Clauses (J);
1559 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
1560 Install_Use_Clauses (U, Force_Installation => True);
1562 end Re_Install_Use_Clauses;
1568 procedure Remove_Scope is
1572 Num_Scopes := Num_Scopes + 1;
1573 Use_Clauses (Num_Scopes) :=
1574 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
1575 E := First_Entity (Current_Scope);
1577 while Present (E) loop
1578 Set_Is_Immediately_Visible (E, False);
1582 if Is_Child_Unit (Current_Scope) then
1583 Enclosing_Child := Current_Scope;
1589 -- Start of processing for Analyze_Subunit
1592 if not Is_Empty_List (Context_Items (N)) then
1594 -- Save current use clauses
1597 Remove_Context (Lib_Unit);
1599 -- Now remove parents and their context, including enclosing
1600 -- subunits and the outer parent body which is not a subunit.
1602 if Present (Lib_Spec) then
1603 Remove_Context (Lib_Spec);
1605 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
1606 Lib_Spec := Library_Unit (Lib_Spec);
1608 Remove_Context (Lib_Spec);
1611 if Nkind (Unit (Lib_Unit)) = N_Subunit then
1615 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
1616 Remove_Context (Library_Unit (Lib_Spec));
1620 Set_Is_Immediately_Visible (Par_Unit, False);
1622 Analyze_Subunit_Context;
1624 Re_Install_Parents (Lib_Unit, Par_Unit);
1625 Set_Is_Immediately_Visible (Par_Unit);
1627 -- If the context includes a child unit of the parent of the
1628 -- subunit, the parent will have been removed from visibility,
1629 -- after compiling that cousin in the context. The visibility
1630 -- of the parent must be restored now. This also applies if the
1631 -- context includes another subunit of the same parent which in
1632 -- turn includes a child unit in its context.
1634 if Ekind (Par_Unit) = E_Package then
1635 if not Is_Immediately_Visible (Par_Unit)
1636 or else (Present (First_Entity (Par_Unit))
1637 and then not Is_Immediately_Visible
1638 (First_Entity (Par_Unit)))
1640 Set_Is_Immediately_Visible (Par_Unit);
1641 Install_Visible_Declarations (Par_Unit);
1642 Install_Private_Declarations (Par_Unit);
1646 Re_Install_Use_Clauses;
1647 Install_Context (N);
1649 -- Restore state of suppress flags for current body
1651 Scope_Suppress := Svg;
1653 -- If the subunit is within a child unit, then siblings of any
1654 -- parent unit that appear in the context clause of the subunit
1655 -- must also be made immediately visible.
1657 if Present (Enclosing_Child) then
1658 Install_Siblings (Enclosing_Child, N);
1663 Analyze (Proper_Body (Unit (N)));
1665 end Analyze_Subunit;
1667 ----------------------------
1668 -- Analyze_Task_Body_Stub --
1669 ----------------------------
1671 procedure Analyze_Task_Body_Stub (N : Node_Id) is
1672 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1673 Loc : constant Source_Ptr := Sloc (N);
1676 Check_Stub_Level (N);
1678 -- First occurence of name may have been as an incomplete type
1680 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1681 Nam := Full_View (Nam);
1685 or else not Is_Task_Type (Etype (Nam))
1687 Error_Msg_N ("missing specification for task body", N);
1689 Set_Scope (Defining_Entity (N), Current_Scope);
1690 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1691 Set_Has_Completion (Etype (Nam));
1692 Analyze_Proper_Body (N, Etype (Nam));
1694 -- Set elaboration flag to indicate that entity is callable.
1695 -- This cannot be done in the expansion of the body itself,
1696 -- because the proper body is not in a declarative part. This
1697 -- is only done if expansion is active, because the context
1698 -- may be generic and the flag not defined yet.
1700 if Expander_Active then
1702 Make_Assignment_Statement (Loc,
1704 Make_Identifier (Loc,
1705 New_External_Name (Chars (Etype (Nam)), 'E')),
1706 Expression => New_Reference_To (Standard_True, Loc)));
1710 end Analyze_Task_Body_Stub;
1712 -------------------------
1713 -- Analyze_With_Clause --
1714 -------------------------
1716 -- Analyze the declaration of a unit in a with clause. At end,
1717 -- label the with clause with the defining entity for the unit.
1719 procedure Analyze_With_Clause (N : Node_Id) is
1721 -- Retrieve the original kind of the unit node, before analysis.
1722 -- If it is a subprogram instantiation, its analysis below will
1723 -- rewrite as the declaration of the wrapper package. If the same
1724 -- instantiation appears indirectly elsewhere in the context, it
1725 -- will have been analyzed already.
1727 Unit_Kind : constant Node_Kind :=
1728 Nkind (Original_Node (Unit (Library_Unit (N))));
1731 Par_Name : Entity_Id;
1736 -- Set True if the unit currently being compiled is an internal unit
1738 Save_Style_Check : constant Boolean := Opt.Style_Check;
1739 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
1740 Cunit_Boolean_Restrictions_Save;
1743 if Limited_Present (N) then
1744 -- Ada 2005 (AI-50217): Build visibility structures but do not
1747 Build_Limited_Views (N);
1751 -- We reset ordinary style checking during the analysis of a with'ed
1752 -- unit, but we do NOT reset GNAT special analysis mode (the latter
1753 -- definitely *does* apply to with'ed units).
1755 if not GNAT_Mode then
1756 Style_Check := False;
1759 -- If the library unit is a predefined unit, and we are in high
1760 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
1761 -- for the analysis of the with'ed unit. This mode does not prevent
1762 -- explicit with'ing of run-time units.
1764 if Configurable_Run_Time_Mode
1766 Is_Predefined_File_Name
1767 (Unit_File_Name (Get_Source_Unit (Unit (Library_Unit (N)))))
1769 Configurable_Run_Time_Mode := False;
1770 Semantics (Library_Unit (N));
1771 Configurable_Run_Time_Mode := True;
1774 Semantics (Library_Unit (N));
1777 U := Unit (Library_Unit (N));
1778 Check_Restriction_No_Dependence (Name (N), N);
1779 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
1781 -- Following checks are skipped for dummy packages (those supplied
1782 -- for with's where no matching file could be found). Such packages
1783 -- are identified by the Sloc value being set to No_Location
1785 if Sloc (U) /= No_Location then
1787 -- Check restrictions, except that we skip the check if this
1788 -- is an internal unit unless we are compiling the internal
1789 -- unit as the main unit. We also skip this for dummy packages.
1791 if not Intunit or else Current_Sem_Unit = Main_Unit then
1792 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
1795 -- Check for inappropriate with of internal implementation unit
1796 -- if we are currently compiling the main unit and the main unit
1797 -- is itself not an internal unit. We do not issue this message
1798 -- for implicit with's generated by the compiler itself.
1800 if Implementation_Unit_Warnings
1801 and then Current_Sem_Unit = Main_Unit
1802 and then not Intunit
1803 and then not Implicit_With (N)
1804 and then not GNAT_Mode
1807 U_Kind : constant Kind_Of_Unit :=
1808 Get_Kind_Of_Unit (Get_Source_Unit (U));
1811 if U_Kind = Implementation_Unit then
1812 Error_Msg_N ("& is an internal 'G'N'A'T unit?", Name (N));
1814 ("\use of this unit is non-portable " &
1815 "and version-dependent?",
1818 elsif U_Kind = Ada_05_Unit
1819 and then Ada_Version < Ada_05
1820 and then Warn_On_Ada_2005_Compatibility
1822 Error_Msg_N ("& is an Ada 2005 unit?", Name (N));
1828 -- Semantic analysis of a generic unit is performed on a copy of
1829 -- the original tree. Retrieve the entity on which semantic info
1830 -- actually appears.
1832 if Unit_Kind in N_Generic_Declaration then
1833 E_Name := Defining_Entity (U);
1835 -- Note: in the following test, Unit_Kind is the original Nkind, but
1836 -- in the case of an instantiation, semantic analysis above will
1837 -- have replaced the unit by its instantiated version. If the instance
1838 -- body has been generated, the instance now denotes the body entity.
1839 -- For visibility purposes we need the entity of its spec.
1841 elsif (Unit_Kind = N_Package_Instantiation
1842 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
1843 N_Package_Instantiation)
1844 and then Nkind (U) = N_Package_Body
1846 E_Name := Corresponding_Spec (U);
1848 elsif Unit_Kind = N_Package_Instantiation
1849 and then Nkind (U) = N_Package_Instantiation
1851 -- If the instance has not been rewritten as a package declaration,
1852 -- then it appeared already in a previous with clause. Retrieve
1853 -- the entity from the previous instance.
1855 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
1857 elsif Unit_Kind = N_Procedure_Instantiation
1858 or else Unit_Kind = N_Function_Instantiation
1860 -- Instantiation node is replaced with a package that contains
1861 -- renaming declarations and instance itself. The subprogram
1862 -- Instance is declared in the visible part of the wrapper package.
1864 E_Name := First_Entity (Defining_Entity (U));
1866 while Present (E_Name) loop
1867 exit when Is_Subprogram (E_Name)
1868 and then Is_Generic_Instance (E_Name);
1869 E_Name := Next_Entity (E_Name);
1872 elsif Unit_Kind = N_Package_Renaming_Declaration
1873 or else Unit_Kind in N_Generic_Renaming_Declaration
1875 E_Name := Defining_Entity (U);
1877 elsif Unit_Kind = N_Subprogram_Body
1878 and then Nkind (Name (N)) = N_Selected_Component
1879 and then not Acts_As_Spec (Library_Unit (N))
1881 -- For a child unit that has no spec, one has been created and
1882 -- analyzed. The entity required is that of the spec.
1884 E_Name := Corresponding_Spec (U);
1887 E_Name := Defining_Entity (U);
1890 if Nkind (Name (N)) = N_Selected_Component then
1892 -- Child unit in a with clause
1894 Change_Selected_Component_To_Expanded_Name (Name (N));
1897 -- Restore style checks and restrictions
1899 Style_Check := Save_Style_Check;
1900 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1902 -- Record the reference, but do NOT set the unit as referenced, we
1903 -- want to consider the unit as unreferenced if this is the only
1904 -- reference that occurs.
1906 Set_Entity_With_Style_Check (Name (N), E_Name);
1907 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
1909 if Is_Child_Unit (E_Name) then
1910 Pref := Prefix (Name (N));
1911 Par_Name := Scope (E_Name);
1913 while Nkind (Pref) = N_Selected_Component loop
1914 Change_Selected_Component_To_Expanded_Name (Pref);
1915 Set_Entity_With_Style_Check (Pref, Par_Name);
1917 Generate_Reference (Par_Name, Pref);
1918 Pref := Prefix (Pref);
1920 -- If E_Name is the dummy entity for a nonexistent unit,
1921 -- its scope is set to Standard_Standard, and no attempt
1922 -- should be made to further unwind scopes.
1924 if Par_Name /= Standard_Standard then
1925 Par_Name := Scope (Par_Name);
1929 if Present (Entity (Pref))
1930 and then not Analyzed (Parent (Parent (Entity (Pref))))
1932 -- If the entity is set without its unit being compiled,
1933 -- the original parent is a renaming, and Par_Name is the
1934 -- renamed entity. For visibility purposes, we need the
1935 -- original entity, which must be analyzed now, because
1936 -- Load_Unit retrieves directly the renamed unit, and the
1937 -- renaming declaration itself has not been analyzed.
1939 Analyze (Parent (Parent (Entity (Pref))));
1940 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
1941 Par_Name := Entity (Pref);
1944 Set_Entity_With_Style_Check (Pref, Par_Name);
1945 Generate_Reference (Par_Name, Pref);
1948 -- If the withed unit is System, and a system extension pragma is
1949 -- present, compile the extension now, rather than waiting for
1950 -- a visibility check on a specific entity.
1952 if Chars (E_Name) = Name_System
1953 and then Scope (E_Name) = Standard_Standard
1954 and then Present (System_Extend_Unit)
1955 and then Present_System_Aux (N)
1957 -- If the extension is not present, an error will have been emitted
1962 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
1963 -- to private_with units; they will be made visible later (just before
1964 -- the private part is analyzed)
1966 if Private_Present (N) then
1967 Set_Is_Immediately_Visible (E_Name, False);
1969 end Analyze_With_Clause;
1971 ------------------------------
1972 -- Analyze_With_Type_Clause --
1973 ------------------------------
1975 procedure Analyze_With_Type_Clause (N : Node_Id) is
1976 Loc : constant Source_Ptr := Sloc (N);
1977 Nam : constant Node_Id := Name (N);
1981 Unum : Unit_Number_Type;
1984 procedure Decorate_Tagged_Type (T : Entity_Id);
1985 -- Set basic attributes of type, including its class_wide type
1987 function In_Chain (E : Entity_Id) return Boolean;
1988 -- Check that the imported type is not already in the homonym chain,
1989 -- for example through a with_type clause in a parent unit.
1991 --------------------------
1992 -- Decorate_Tagged_Type --
1993 --------------------------
1995 procedure Decorate_Tagged_Type (T : Entity_Id) is
1999 Set_Ekind (T, E_Record_Type);
2000 Set_Is_Tagged_Type (T);
2002 Set_From_With_Type (T);
2005 if not In_Chain (T) then
2006 Set_Homonym (T, Current_Entity (T));
2007 Set_Current_Entity (T);
2010 -- Build bogus class_wide type, if not previously done
2012 if No (Class_Wide_Type (T)) then
2013 CW := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
2015 Set_Ekind (CW, E_Class_Wide_Type);
2018 Set_Is_Tagged_Type (CW);
2019 Set_Is_First_Subtype (CW, True);
2020 Init_Size_Align (CW);
2021 Set_Has_Unknown_Discriminants
2023 Set_Class_Wide_Type (CW, CW);
2024 Set_Equivalent_Type (CW, Empty);
2025 Set_From_With_Type (CW);
2027 Set_Class_Wide_Type (T, CW);
2029 end Decorate_Tagged_Type;
2035 function In_Chain (E : Entity_Id) return Boolean is
2036 H : Entity_Id := Current_Entity (E);
2039 while Present (H) loop
2051 -- Start of processing for Analyze_With_Type_Clause
2054 if Nkind (Nam) = N_Selected_Component then
2055 Pack := New_Copy_Tree (Prefix (Nam));
2056 Sel := Selector_Name (Nam);
2059 Error_Msg_N ("illegal name for imported type", Nam);
2064 Make_Package_Declaration (Loc,
2066 (Make_Package_Specification (Loc,
2067 Defining_Unit_Name => Pack,
2068 Visible_Declarations => New_List,
2069 End_Label => Empty)));
2073 (Load_Name => Get_Unit_Name (Decl),
2079 or else Nkind (Unit (Cunit (Unum))) /= N_Package_Declaration
2081 Error_Msg_N ("imported type must be declared in package", Nam);
2084 elsif Unum = Current_Sem_Unit then
2086 -- If type is defined in unit being analyzed, then the clause
2092 P := Cunit_Entity (Unum);
2095 -- Find declaration for imported type, and set its basic attributes
2096 -- if it has not been analyzed (which will be the case if there is
2097 -- circular dependence).
2104 if not Analyzed (Cunit (Unum))
2105 and then not From_With_Type (P)
2107 Set_Ekind (P, E_Package);
2108 Set_Etype (P, Standard_Void_Type);
2109 Set_From_With_Type (P);
2110 Set_Scope (P, Standard_Standard);
2111 Set_Homonym (P, Current_Entity (P));
2112 Set_Current_Entity (P);
2114 elsif Analyzed (Cunit (Unum))
2115 and then Is_Child_Unit (P)
2117 -- If the child unit is already in scope, indicate that it is
2118 -- visible, and remains so after intervening calls to rtsfind.
2120 Set_Is_Visible_Child_Unit (P);
2123 if Nkind (Parent (P)) = N_Defining_Program_Unit_Name then
2125 -- Make parent packages visible
2128 Parent_Comp : Node_Id;
2129 Parent_Id : Entity_Id;
2134 Parent_Comp := Parent_Spec (Unit (Cunit (Unum)));
2137 Parent_Id := Defining_Entity (Unit (Parent_Comp));
2138 Set_Scope (Child, Parent_Id);
2140 -- The type may be imported from a child unit, in which
2141 -- case the current compilation appears in the name. Do
2142 -- not change its visibility here because it will conflict
2143 -- with the subsequent normal processing.
2145 if not Analyzed (Unit_Declaration_Node (Parent_Id))
2146 and then Parent_Id /= Cunit_Entity (Current_Sem_Unit)
2148 Set_Ekind (Parent_Id, E_Package);
2149 Set_Etype (Parent_Id, Standard_Void_Type);
2151 -- The same package may appear is several with_type
2154 if not From_With_Type (Parent_Id) then
2155 Set_Homonym (Parent_Id, Current_Entity (Parent_Id));
2156 Set_Current_Entity (Parent_Id);
2157 Set_From_With_Type (Parent_Id);
2161 Set_Is_Immediately_Visible (Parent_Id);
2164 Parent_Comp := Parent_Spec (Unit (Parent_Comp));
2165 exit when No (Parent_Comp);
2168 Set_Scope (Parent_Id, Standard_Standard);
2172 -- Even if analyzed, the package may not be currently visible. It
2173 -- must be while the with_type clause is active.
2175 Set_Is_Immediately_Visible (P);
2178 First (Visible_Declarations (Specification (Unit (Cunit (Unum)))));
2180 while Present (Decl) loop
2182 if Nkind (Decl) = N_Full_Type_Declaration
2183 and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
2185 Typ := Defining_Identifier (Decl);
2187 if Tagged_Present (N) then
2189 -- The declaration must indicate that this is a tagged
2190 -- type or a type extension.
2192 if (Nkind (Type_Definition (Decl)) = N_Record_Definition
2193 and then Tagged_Present (Type_Definition (Decl)))
2195 (Nkind (Type_Definition (Decl))
2196 = N_Derived_Type_Definition
2198 (Record_Extension_Part (Type_Definition (Decl))))
2202 Error_Msg_N ("imported type is not a tagged type", Nam);
2206 if not Analyzed (Decl) then
2208 -- Unit is not currently visible. Add basic attributes
2209 -- to type and build its class-wide type.
2211 Init_Size_Align (Typ);
2212 Decorate_Tagged_Type (Typ);
2216 if Nkind (Type_Definition (Decl))
2217 /= N_Access_To_Object_Definition
2220 ("imported type is not an access type", Nam);
2222 elsif not Analyzed (Decl) then
2223 Set_Ekind (Typ, E_Access_Type);
2224 Set_Etype (Typ, Typ);
2226 Init_Size (Typ, System_Address_Size);
2227 Init_Alignment (Typ);
2228 Set_Directly_Designated_Type (Typ, Standard_Integer);
2229 Set_From_With_Type (Typ);
2231 if not In_Chain (Typ) then
2232 Set_Homonym (Typ, Current_Entity (Typ));
2233 Set_Current_Entity (Typ);
2238 Set_Entity (Sel, Typ);
2241 elsif ((Nkind (Decl) = N_Private_Type_Declaration
2242 and then Tagged_Present (Decl))
2243 or else (Nkind (Decl) = N_Private_Extension_Declaration))
2244 and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
2246 Typ := Defining_Identifier (Decl);
2248 if not Tagged_Present (N) then
2249 Error_Msg_N ("type must be declared tagged", N);
2251 elsif not Analyzed (Decl) then
2252 Decorate_Tagged_Type (Typ);
2255 Set_Entity (Sel, Typ);
2256 Set_From_With_Type (Typ);
2260 Decl := Next (Decl);
2263 Error_Msg_NE ("not a visible access or tagged type in&", Nam, P);
2265 end Analyze_With_Type_Clause;
2267 -----------------------------
2268 -- Check_With_Type_Clauses --
2269 -----------------------------
2271 procedure Check_With_Type_Clauses (N : Node_Id) is
2272 Lib_Unit : constant Node_Id := Unit (N);
2274 procedure Check_Parent_Context (U : Node_Id);
2275 -- Examine context items of parent unit to locate with_type clauses
2277 --------------------------
2278 -- Check_Parent_Context --
2279 --------------------------
2281 procedure Check_Parent_Context (U : Node_Id) is
2285 Item := First (Context_Items (U));
2286 while Present (Item) loop
2287 if Nkind (Item) = N_With_Type_Clause
2288 and then not Error_Posted (Item)
2290 From_With_Type (Scope (Entity (Selector_Name (Name (Item)))))
2292 Error_Msg_Sloc := Sloc (Item);
2293 Error_Msg_N ("missing With_Clause for With_Type_Clause#", N);
2298 end Check_Parent_Context;
2300 -- Start of processing for Check_With_Type_Clauses
2303 if Extensions_Allowed
2304 and then (Nkind (Lib_Unit) = N_Package_Body
2305 or else Nkind (Lib_Unit) = N_Subprogram_Body)
2307 Check_Parent_Context (Library_Unit (N));
2309 if Is_Child_Spec (Unit (Library_Unit (N))) then
2310 Check_Parent_Context (Parent_Spec (Unit (Library_Unit (N))));
2313 end Check_With_Type_Clauses;
2315 ------------------------------
2316 -- Check_Private_Child_Unit --
2317 ------------------------------
2319 procedure Check_Private_Child_Unit (N : Node_Id) is
2320 Lib_Unit : constant Node_Id := Unit (N);
2322 Curr_Unit : Entity_Id;
2323 Sub_Parent : Node_Id;
2324 Priv_Child : Entity_Id;
2325 Par_Lib : Entity_Id;
2328 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2329 -- Returns true if and only if the library unit is declared with
2330 -- an explicit designation of private.
2332 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2333 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2336 return Private_Present (Comp_Unit);
2337 end Is_Private_Library_Unit;
2339 -- Start of processing for Check_Private_Child_Unit
2342 if Nkind (Lib_Unit) = N_Package_Body
2343 or else Nkind (Lib_Unit) = N_Subprogram_Body
2345 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2346 Par_Lib := Curr_Unit;
2348 elsif Nkind (Lib_Unit) = N_Subunit then
2350 -- The parent is itself a body. The parent entity is to be found
2351 -- in the corresponding spec.
2353 Sub_Parent := Library_Unit (N);
2354 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2356 -- If the parent itself is a subunit, Curr_Unit is the entity
2357 -- of the enclosing body, retrieve the spec entity which is
2358 -- the proper ancestor we need for the following tests.
2360 if Ekind (Curr_Unit) = E_Package_Body then
2361 Curr_Unit := Spec_Entity (Curr_Unit);
2364 Par_Lib := Curr_Unit;
2367 Curr_Unit := Defining_Entity (Lib_Unit);
2369 Par_Lib := Curr_Unit;
2370 Par_Spec := Parent_Spec (Lib_Unit);
2372 if No (Par_Spec) then
2375 Par_Lib := Defining_Entity (Unit (Par_Spec));
2379 -- Loop through context items
2381 Item := First (Context_Items (N));
2382 while Present (Item) loop
2384 -- Ada 2005 (AI-262): Allow private_with of a private child package
2385 -- in public siblings
2387 if Nkind (Item) = N_With_Clause
2388 and then not Implicit_With (Item)
2389 and then not Private_Present (Item)
2390 and then Is_Private_Descendant (Entity (Name (Item)))
2392 Priv_Child := Entity (Name (Item));
2395 Curr_Parent : Entity_Id := Par_Lib;
2396 Child_Parent : Entity_Id := Scope (Priv_Child);
2397 Prv_Ancestor : Entity_Id := Child_Parent;
2398 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2401 -- If the child unit is a public child then locate
2402 -- the nearest private ancestor; Child_Parent will
2403 -- then be set to the parent of that ancestor.
2405 if not Is_Private_Library_Unit (Priv_Child) then
2406 while Present (Prv_Ancestor)
2407 and then not Is_Private_Library_Unit (Prv_Ancestor)
2409 Prv_Ancestor := Scope (Prv_Ancestor);
2412 if Present (Prv_Ancestor) then
2413 Child_Parent := Scope (Prv_Ancestor);
2417 while Present (Curr_Parent)
2418 and then Curr_Parent /= Standard_Standard
2419 and then Curr_Parent /= Child_Parent
2422 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2423 Curr_Parent := Scope (Curr_Parent);
2426 if not Present (Curr_Parent) then
2427 Curr_Parent := Standard_Standard;
2430 if Curr_Parent /= Child_Parent then
2432 if Ekind (Priv_Child) = E_Generic_Package
2433 and then Chars (Priv_Child) in Text_IO_Package_Name
2434 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2437 ("& is a nested package, not a compilation unit",
2438 Name (Item), Priv_Child);
2442 ("unit in with clause is private child unit!", Item);
2444 ("current unit must also have parent&!",
2445 Item, Child_Parent);
2448 elsif not Curr_Private
2449 and then Nkind (Lib_Unit) /= N_Package_Body
2450 and then Nkind (Lib_Unit) /= N_Subprogram_Body
2451 and then Nkind (Lib_Unit) /= N_Subunit
2454 ("current unit must also be private descendant of&",
2455 Item, Child_Parent);
2463 end Check_Private_Child_Unit;
2465 ----------------------
2466 -- Check_Stub_Level --
2467 ----------------------
2469 procedure Check_Stub_Level (N : Node_Id) is
2470 Par : constant Node_Id := Parent (N);
2471 Kind : constant Node_Kind := Nkind (Par);
2474 if (Kind = N_Package_Body
2475 or else Kind = N_Subprogram_Body
2476 or else Kind = N_Task_Body
2477 or else Kind = N_Protected_Body)
2479 and then (Nkind (Parent (Par)) = N_Compilation_Unit
2480 or else Nkind (Parent (Par)) = N_Subunit)
2484 -- In an instance, a missing stub appears at any level. A warning
2485 -- message will have been emitted already for the missing file.
2487 elsif not In_Instance then
2488 Error_Msg_N ("stub cannot appear in an inner scope", N);
2490 elsif Expander_Active then
2491 Error_Msg_N ("missing proper body", N);
2493 end Check_Stub_Level;
2495 ------------------------
2496 -- Expand_With_Clause --
2497 ------------------------
2499 procedure Expand_With_Clause (Nam : Node_Id; N : Node_Id) is
2500 Loc : constant Source_Ptr := Sloc (Nam);
2501 Ent : constant Entity_Id := Entity (Nam);
2505 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2507 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2511 if Nkind (Nam) = N_Identifier then
2512 return New_Occurrence_Of (Entity (Nam), Loc);
2516 Make_Expanded_Name (Loc,
2517 Chars => Chars (Entity (Nam)),
2518 Prefix => Build_Unit_Name (Prefix (Nam)),
2519 Selector_Name => New_Occurrence_Of (Entity (Nam), Loc));
2520 Set_Entity (Result, Entity (Nam));
2523 end Build_Unit_Name;
2526 New_Nodes_OK := New_Nodes_OK + 1;
2528 Make_With_Clause (Loc, Name => Build_Unit_Name (Nam));
2530 P := Parent (Unit_Declaration_Node (Ent));
2531 Set_Library_Unit (Withn, P);
2532 Set_Corresponding_Spec (Withn, Ent);
2533 Set_First_Name (Withn, True);
2534 Set_Implicit_With (Withn, True);
2536 Prepend (Withn, Context_Items (N));
2537 Mark_Rewrite_Insertion (Withn);
2538 Install_Withed_Unit (Withn);
2540 if Nkind (Nam) = N_Expanded_Name then
2541 Expand_With_Clause (Prefix (Nam), N);
2544 New_Nodes_OK := New_Nodes_OK - 1;
2545 end Expand_With_Clause;
2547 -----------------------
2548 -- Get_Parent_Entity --
2549 -----------------------
2551 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2553 if Nkind (Unit) = N_Package_Body
2554 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
2558 (Specification (Instance_Spec (Original_Node (Unit))));
2560 elsif Nkind (Unit) = N_Package_Instantiation then
2561 return Defining_Entity (Specification (Instance_Spec (Unit)));
2564 return Defining_Entity (Unit);
2566 end Get_Parent_Entity;
2568 -----------------------------
2569 -- Implicit_With_On_Parent --
2570 -----------------------------
2572 procedure Implicit_With_On_Parent
2573 (Child_Unit : Node_Id;
2576 Loc : constant Source_Ptr := Sloc (N);
2577 P : constant Node_Id := Parent_Spec (Child_Unit);
2579 P_Unit : Node_Id := Unit (P);
2581 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
2584 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2585 -- Build prefix of child unit name. Recurse if needed
2587 function Build_Unit_Name return Node_Id;
2588 -- If the unit is a child unit, build qualified name with all
2591 -------------------------
2592 -- Build_Ancestor_Name --
2593 -------------------------
2595 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
2596 P_Ref : constant Node_Id :=
2597 New_Reference_To (Defining_Entity (P), Loc);
2598 P_Spec : Node_Id := P;
2601 -- Ancestor may have been rewritten as a package body. Retrieve
2602 -- the original spec to trace earlier ancestors.
2604 if Nkind (P) = N_Package_Body
2605 and then Nkind (Original_Node (P)) = N_Package_Instantiation
2607 P_Spec := Original_Node (P);
2610 if No (Parent_Spec (P_Spec)) then
2614 Make_Selected_Component (Loc,
2615 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
2616 Selector_Name => P_Ref);
2618 end Build_Ancestor_Name;
2620 ---------------------
2621 -- Build_Unit_Name --
2622 ---------------------
2624 function Build_Unit_Name return Node_Id is
2627 if No (Parent_Spec (P_Unit)) then
2628 return New_Reference_To (P_Name, Loc);
2631 Make_Expanded_Name (Loc,
2632 Chars => Chars (P_Name),
2633 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
2634 Selector_Name => New_Reference_To (P_Name, Loc));
2635 Set_Entity (Result, P_Name);
2638 end Build_Unit_Name;
2640 -- Start of processing for Implicit_With_On_Parent
2643 -- The unit of the current compilation may be a package body
2644 -- that replaces an instance node. In this case we need the
2645 -- original instance node to construct the proper parent name.
2647 if Nkind (P_Unit) = N_Package_Body
2648 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
2650 P_Unit := Original_Node (P_Unit);
2653 New_Nodes_OK := New_Nodes_OK + 1;
2654 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
2656 Set_Library_Unit (Withn, P);
2657 Set_Corresponding_Spec (Withn, P_Name);
2658 Set_First_Name (Withn, True);
2659 Set_Implicit_With (Withn, True);
2661 -- Node is placed at the beginning of the context items, so that
2662 -- subsequent use clauses on the parent can be validated.
2664 Prepend (Withn, Context_Items (N));
2665 Mark_Rewrite_Insertion (Withn);
2666 Install_Withed_Unit (Withn);
2668 if Is_Child_Spec (P_Unit) then
2669 Implicit_With_On_Parent (P_Unit, N);
2672 New_Nodes_OK := New_Nodes_OK - 1;
2673 end Implicit_With_On_Parent;
2675 ---------------------
2676 -- Install_Context --
2677 ---------------------
2679 procedure Install_Context (N : Node_Id) is
2680 Lib_Unit : constant Node_Id := Unit (N);
2683 Install_Context_Clauses (N);
2685 if Is_Child_Spec (Lib_Unit) then
2686 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
2689 Install_Limited_Context_Clauses (N);
2691 Check_With_Type_Clauses (N);
2692 end Install_Context;
2694 -----------------------------
2695 -- Install_Context_Clauses --
2696 -----------------------------
2698 procedure Install_Context_Clauses (N : Node_Id) is
2699 Lib_Unit : constant Node_Id := Unit (N);
2701 Uname_Node : Entity_Id;
2702 Check_Private : Boolean := False;
2703 Decl_Node : Node_Id;
2704 Lib_Parent : Entity_Id;
2707 -- Loop through context clauses to find the with/use clauses.
2708 -- This is done twice, first for everything except limited_with
2709 -- clauses, and then for those, if any are present.
2711 Item := First (Context_Items (N));
2712 while Present (Item) loop
2714 -- Case of explicit WITH clause
2716 if Nkind (Item) = N_With_Clause
2717 and then not Implicit_With (Item)
2719 if Limited_Present (Item) then
2721 -- Limited withed units will be installed later
2725 -- If Name (Item) is not an entity name, something is wrong, and
2726 -- this will be detected in due course, for now ignore the item
2728 elsif not Is_Entity_Name (Name (Item)) then
2731 elsif No (Entity (Name (Item))) then
2732 Set_Entity (Name (Item), Any_Id);
2736 Uname_Node := Entity (Name (Item));
2738 if Is_Private_Descendant (Uname_Node) then
2739 Check_Private := True;
2742 Install_Withed_Unit (Item);
2744 Decl_Node := Unit_Declaration_Node (Uname_Node);
2746 -- If the unit is a subprogram instance, it appears nested
2747 -- within a package that carries the parent information.
2749 if Is_Generic_Instance (Uname_Node)
2750 and then Ekind (Uname_Node) /= E_Package
2752 Decl_Node := Parent (Parent (Decl_Node));
2755 if Is_Child_Spec (Decl_Node) then
2756 if Nkind (Name (Item)) = N_Expanded_Name then
2757 Expand_With_Clause (Prefix (Name (Item)), N);
2759 -- if not an expanded name, the child unit must be a
2760 -- renaming, nothing to do.
2765 elsif Nkind (Decl_Node) = N_Subprogram_Body
2766 and then not Acts_As_Spec (Parent (Decl_Node))
2767 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
2769 Implicit_With_On_Parent
2770 (Unit (Library_Unit (Parent (Decl_Node))), N);
2773 -- Check license conditions unless this is a dummy unit
2775 if Sloc (Library_Unit (Item)) /= No_Location then
2776 License_Check : declare
2777 Withl : constant License_Type :=
2778 License (Source_Index
2780 (Library_Unit (Item))));
2782 Unitl : constant License_Type :=
2783 License (Source_Index (Current_Sem_Unit));
2785 procedure License_Error;
2786 -- Signal error of bad license
2792 procedure License_Error is
2795 ("?license of with'ed unit & is incompatible",
2799 -- Start of processing for License_Check
2812 if Withl = Restricted then
2816 when Modified_GPL =>
2817 if Withl = Restricted or else Withl = GPL then
2821 when Unrestricted =>
2827 -- Case of USE PACKAGE clause
2829 elsif Nkind (Item) = N_Use_Package_Clause then
2830 Analyze_Use_Package (Item);
2832 -- Case of USE TYPE clause
2834 elsif Nkind (Item) = N_Use_Type_Clause then
2835 Analyze_Use_Type (Item);
2837 -- Case of WITH TYPE clause
2839 -- A With_Type_Clause is processed when installing the context,
2840 -- because it is a visibility mechanism and does not create a
2841 -- semantic dependence on other units, as a With_Clause does.
2843 elsif Nkind (Item) = N_With_Type_Clause then
2844 Analyze_With_Type_Clause (Item);
2848 elsif Nkind (Item) = N_Pragma then
2856 if Is_Child_Spec (Lib_Unit) then
2858 -- The unit also has implicit withs on its own parents
2860 if No (Context_Items (N)) then
2861 Set_Context_Items (N, New_List);
2864 Implicit_With_On_Parent (Lib_Unit, N);
2867 -- If the unit is a body, the context of the specification must also
2870 if Nkind (Lib_Unit) = N_Package_Body
2871 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2872 and then not Acts_As_Spec (N))
2874 Install_Context (Library_Unit (N));
2876 if Is_Child_Spec (Unit (Library_Unit (N))) then
2878 -- If the unit is the body of a public child unit, the private
2879 -- declarations of the parent must be made visible. If the child
2880 -- unit is private, the private declarations have been installed
2881 -- already in the call to Install_Parents for the spec. Installing
2882 -- private declarations must be done for all ancestors of public
2883 -- child units. In addition, sibling units mentioned in the
2884 -- context clause of the body are directly visible.
2887 Lib_Spec : Node_Id := Unit (Library_Unit (N));
2892 while Is_Child_Spec (Lib_Spec) loop
2893 P := Unit (Parent_Spec (Lib_Spec));
2895 if not (Private_Present (Parent (Lib_Spec))) then
2896 P_Name := Defining_Entity (P);
2897 Install_Private_Declarations (P_Name);
2898 Install_Private_With_Clauses (P_Name);
2899 Set_Use (Private_Declarations (Specification (P)));
2907 -- For a package body, children in context are immediately visible
2909 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
2912 if Nkind (Lib_Unit) = N_Generic_Package_Declaration
2913 or else Nkind (Lib_Unit) = N_Generic_Subprogram_Declaration
2914 or else Nkind (Lib_Unit) = N_Package_Declaration
2915 or else Nkind (Lib_Unit) = N_Subprogram_Declaration
2917 if Is_Child_Spec (Lib_Unit) then
2918 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
2919 Set_Is_Private_Descendant
2920 (Defining_Entity (Lib_Unit),
2921 Is_Private_Descendant (Lib_Parent)
2922 or else Private_Present (Parent (Lib_Unit)));
2925 Set_Is_Private_Descendant
2926 (Defining_Entity (Lib_Unit),
2927 Private_Present (Parent (Lib_Unit)));
2931 if Check_Private then
2932 Check_Private_Child_Unit (N);
2934 end Install_Context_Clauses;
2936 -------------------------------------
2937 -- Install_Limited_Context_Clauses --
2938 -------------------------------------
2940 procedure Install_Limited_Context_Clauses (N : Node_Id) is
2943 procedure Check_Renamings (P : Node_Id; W : Node_Id);
2944 -- Check that the unlimited view of a given compilation_unit is not
2945 -- already visible through "use + renamings".
2947 procedure Check_Private_Limited_Withed_Unit (N : Node_Id);
2948 -- Check that if a limited_with clause of a given compilation_unit
2949 -- mentions a private child of some library unit, then the given
2950 -- compilation_unit shall be the declaration of a private descendant
2951 -- of that library unit.
2953 procedure Expand_Limited_With_Clause
2954 (Comp_Unit : Node_Id; Nam : Node_Id; N : Node_Id);
2955 -- If a child unit appears in a limited_with clause, there are implicit
2956 -- limited_with clauses on all parents that are not already visible
2957 -- through a regular with clause. This procedure creates the implicit
2958 -- limited with_clauses for the parents and loads the corresponding
2959 -- units. The shadow entities are created when the inserted clause is
2960 -- analyzed. Implements Ada 2005 (AI-50217).
2962 ---------------------
2963 -- Check_Renamings --
2964 ---------------------
2966 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
2975 pragma Assert (Nkind (W) = N_With_Clause);
2977 -- Protect the frontend against previous critical errors
2979 case Nkind (Unit (Library_Unit (W))) is
2980 when N_Subprogram_Declaration |
2981 N_Package_Declaration |
2982 N_Generic_Subprogram_Declaration |
2983 N_Generic_Package_Declaration =>
2990 -- Check "use + renamings"
2992 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
2993 Spec := Specification (Unit (P));
2995 Item := First (Visible_Declarations (Spec));
2996 while Present (Item) loop
2998 if Nkind (Item) = N_Use_Package_Clause then
3000 -- Traverse the list of packages
3002 Nam := First (Names (Item));
3004 while Present (Nam) loop
3007 pragma Assert (Present (Parent (E)));
3009 if Nkind (Parent (E))
3010 = N_Package_Renaming_Declaration
3011 and then Renamed_Entity (E) = WEnt
3013 Error_Msg_N ("unlimited view visible through "
3014 & "use_clause + renamings", W);
3017 elsif Nkind (Parent (E)) = N_Package_Specification then
3019 -- The use clause may refer to a local package.
3020 -- Check all the enclosing scopes.
3023 while E2 /= Standard_Standard
3024 and then E2 /= WEnt loop
3029 Error_Msg_N ("unlimited view visible through "
3030 & "use_clause ", W);
3043 -- Recursive call to check all the ancestors
3045 if Is_Child_Spec (Unit (P)) then
3046 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3048 end Check_Renamings;
3050 ---------------------------------------
3051 -- Check_Private_Limited_Withed_Unit --
3052 ---------------------------------------
3054 procedure Check_Private_Limited_Withed_Unit (N : Node_Id) is
3057 Found : Boolean := False;
3060 -- If the current compilation unit is not private we don't
3061 -- need to check anything else.
3063 if not Private_Present (Parent (N)) then
3067 -- Compilation unit of the parent of the withed library unit
3069 P := Parent_Spec (Unit (Library_Unit (N)));
3071 -- Traverse all the ancestors of the current compilation
3072 -- unit to check if it is a descendant of named library unit.
3075 while Present (Parent_Spec (Unit (C))) loop
3076 C := Parent_Spec (Unit (C));
3086 Error_Msg_N ("current unit is not a private descendant"
3087 & " of the withed unit ('R'M 10.1.2(8)", N);
3089 end Check_Private_Limited_Withed_Unit;
3091 --------------------------------
3092 -- Expand_Limited_With_Clause --
3093 --------------------------------
3095 procedure Expand_Limited_With_Clause
3096 (Comp_Unit : Node_Id;
3100 Loc : constant Source_Ptr := Sloc (Nam);
3101 Unum : Unit_Number_Type;
3104 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3105 -- Returns true if the context already includes a with_clause for
3106 -- this unit. If the with_clause is non-limited, the unit is fully
3107 -- visible and an implicit limited_with should not be created. If
3108 -- there is already a limited_with clause for W, a second one is
3109 -- simply redundant.
3111 --------------------------
3112 -- Previous_Withed_Unit --
3113 --------------------------
3115 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3119 -- A limited with_clause can not appear in the same context_clause
3120 -- as a nonlimited with_clause which mentions the same library.
3122 Item := First (Context_Items (Comp_Unit));
3123 while Present (Item) loop
3124 if Nkind (Item) = N_With_Clause
3125 and then Library_Unit (Item) = Library_Unit (W)
3134 end Previous_Withed_Unit;
3136 -- Start of processing for Expand_Limited_With_Clause
3139 New_Nodes_OK := New_Nodes_OK + 1;
3141 if Nkind (Nam) = N_Identifier then
3142 Withn := Make_With_Clause (Loc, Nam);
3144 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3145 Withn := Make_With_Clause (Loc,
3146 Make_Selected_Component (Loc,
3147 Prefix => Prefix (Nam),
3148 Selector_Name => Selector_Name (Nam)));
3149 Set_Parent (Withn, Parent (N));
3152 Set_Limited_Present (Withn);
3153 Set_First_Name (Withn);
3154 Set_Implicit_With (Withn);
3158 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3163 if not Analyzed (Cunit (Unum)) then
3164 -- Do not generate a limited_with_clause on the current unit.
3165 -- This path is taken when a unit has a limited_with clause on
3166 -- one of its child units.
3168 if Unum = Current_Sem_Unit then
3172 Set_Library_Unit (Withn, Cunit (Unum));
3173 Set_Corresponding_Spec
3174 (Withn, Specification (Unit (Cunit (Unum))));
3176 if not Previous_Withed_Unit (Withn) then
3177 Prepend (Withn, Context_Items (Parent (N)));
3178 Mark_Rewrite_Insertion (Withn);
3180 -- Add implicit limited_with_clauses for parents of child units
3181 -- mentioned in limited_with clauses
3183 if Nkind (Nam) = N_Selected_Component then
3184 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3188 Install_Limited_Withed_Unit (Withn);
3192 New_Nodes_OK := New_Nodes_OK - 1;
3193 end Expand_Limited_With_Clause;
3195 -- Start of processing for Install_Limited_Context_Clauses
3198 Item := First (Context_Items (N));
3199 while Present (Item) loop
3200 if Nkind (Item) = N_With_Clause
3201 and then Limited_Present (Item)
3203 if Nkind (Name (Item)) = N_Selected_Component then
3204 Expand_Limited_With_Clause
3205 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3208 if Private_Present (Library_Unit (Item)) then
3209 Check_Private_Limited_Withed_Unit (Item);
3212 if not Implicit_With (Item)
3213 and then Is_Child_Spec (Unit (N))
3215 Check_Renamings (Parent_Spec (Unit (N)), Item);
3218 -- A unit may have a limited with on itself if it has a
3219 -- limited with_clause on one of its child units. In that
3220 -- case it is already being compiled and it makes no sense
3221 -- to install its limited view.
3223 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
3224 Install_Limited_Withed_Unit (Item);
3230 end Install_Limited_Context_Clauses;
3232 ---------------------
3233 -- Install_Parents --
3234 ---------------------
3236 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3243 P := Unit (Parent_Spec (Lib_Unit));
3244 P_Name := Get_Parent_Entity (P);
3246 if Etype (P_Name) = Any_Type then
3250 if Ekind (P_Name) = E_Generic_Package
3251 and then Nkind (Lib_Unit) /= N_Generic_Subprogram_Declaration
3252 and then Nkind (Lib_Unit) /= N_Generic_Package_Declaration
3253 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3256 ("child of a generic package must be a generic unit", Lib_Unit);
3258 elsif not Is_Package (P_Name) then
3260 ("parent unit must be package or generic package", Lib_Unit);
3261 raise Unrecoverable_Error;
3263 elsif Present (Renamed_Object (P_Name)) then
3264 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3265 raise Unrecoverable_Error;
3267 -- Verify that a child of an instance is itself an instance, or
3268 -- the renaming of one. Given that an instance that is a unit is
3269 -- replaced with a package declaration, check against the original
3270 -- node. The parent may be currently being instantiated, in which
3271 -- case it appears as a declaration, but the generic_parent is
3272 -- already established indicating that we deal with an instance.
3274 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3276 if Nkind (Lib_Unit) in N_Renaming_Declaration
3277 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3279 (Nkind (Lib_Unit) = N_Package_Declaration
3280 and then Present (Generic_Parent (Specification (Lib_Unit))))
3285 ("child of an instance must be an instance or renaming",
3290 -- This is the recursive call that ensures all parents are loaded
3292 if Is_Child_Spec (P) then
3294 Is_Private or else Private_Present (Parent (Lib_Unit)));
3297 -- Now we can install the context for this parent
3299 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3300 Install_Siblings (P_Name, Parent (Lib_Unit));
3302 -- The child unit is in the declarative region of the parent. The
3303 -- parent must therefore appear in the scope stack and be visible,
3304 -- as when compiling the corresponding body. If the child unit is
3305 -- private or it is a package body, private declarations must be
3306 -- accessible as well. Use declarations in the parent must also
3307 -- be installed. Finally, other child units of the same parent that
3308 -- are in the context are immediately visible.
3310 -- Find entity for compilation unit, and set its private descendant
3311 -- status as needed.
3313 E_Name := Defining_Entity (Lib_Unit);
3315 Set_Is_Child_Unit (E_Name);
3317 Set_Is_Private_Descendant (E_Name,
3318 Is_Private_Descendant (P_Name)
3319 or else Private_Present (Parent (Lib_Unit)));
3321 P_Spec := Specification (Unit_Declaration_Node (P_Name));
3324 -- Save current visibility of unit
3326 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3327 Is_Immediately_Visible (P_Name);
3328 Set_Is_Immediately_Visible (P_Name);
3329 Install_Visible_Declarations (P_Name);
3330 Set_Use (Visible_Declarations (P_Spec));
3332 -- If the parent is a generic unit, its formal part may contain
3333 -- formal packages and use clauses for them.
3335 if Ekind (P_Name) = E_Generic_Package then
3336 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3340 or else Private_Present (Parent (Lib_Unit))
3342 Install_Private_Declarations (P_Name);
3343 Install_Private_With_Clauses (P_Name);
3344 Set_Use (Private_Declarations (P_Spec));
3346 end Install_Parents;
3348 ----------------------------------
3349 -- Install_Private_With_Clauses --
3350 ----------------------------------
3352 procedure Install_Private_With_Clauses (P : Entity_Id) is
3353 Decl : constant Node_Id := Unit_Declaration_Node (P);
3357 if Debug_Flag_I then
3358 Write_Str ("install private with clauses of ");
3359 Write_Name (Chars (P));
3363 if Nkind (Parent (Decl)) = N_Compilation_Unit then
3364 Item := First (Context_Items (Parent (Decl)));
3366 while Present (Item) loop
3367 if Nkind (Item) = N_With_Clause
3368 and then Private_Present (Item)
3370 if Limited_Present (Item) then
3371 Install_Limited_Withed_Unit (Item);
3373 Install_Withed_Unit (Item, Private_With_OK => True);
3380 end Install_Private_With_Clauses;
3382 ----------------------
3383 -- Install_Siblings --
3384 ----------------------
3386 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3391 -- Iterate over explicit with clauses, and check whether the
3392 -- scope of each entity is an ancestor of the current unit.
3394 Item := First (Context_Items (N));
3396 -- Do not install private_with_clauses if the unit is a package
3397 -- declaration, unless it is itself a private child unit.
3399 while Present (Item) loop
3400 if Nkind (Item) = N_With_Clause
3401 and then not Implicit_With (Item)
3402 and then not Limited_Present (Item)
3404 (not Private_Present (Item)
3405 or else Nkind (Unit (N)) /= N_Package_Declaration
3406 or else Private_Present (N))
3408 Id := Entity (Name (Item));
3410 if Is_Child_Unit (Id)
3411 and then Is_Ancestor_Package (Scope (Id), U_Name)
3413 Set_Is_Immediately_Visible (Id);
3415 -- Check for the presence of another unit in the context,
3416 -- that may be inadvertently hidden by the child.
3418 Prev := Current_Entity (Id);
3421 and then Is_Immediately_Visible (Prev)
3422 and then not Is_Child_Unit (Prev)
3428 Clause := First (Context_Items (N));
3430 while Present (Clause) loop
3431 if Nkind (Clause) = N_With_Clause
3432 and then Entity (Name (Clause)) = Prev
3435 ("child unit& hides compilation unit " &
3436 "with the same name?",
3446 -- the With_Clause may be on a grand-child, which makes
3447 -- the child immediately visible.
3449 elsif Is_Child_Unit (Scope (Id))
3450 and then Is_Ancestor_Package (Scope (Scope (Id)), U_Name)
3452 Set_Is_Immediately_Visible (Scope (Id));
3458 end Install_Siblings;
3460 -------------------------------
3461 -- Install_Limited_With_Unit --
3462 -------------------------------
3464 procedure Install_Limited_Withed_Unit (N : Node_Id) is
3465 Unum : constant Unit_Number_Type :=
3466 Get_Source_Unit (Library_Unit (N));
3467 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
3469 Is_Child_Package : Boolean := False;
3471 Lim_Header : Entity_Id;
3472 Lim_Typ : Entity_Id;
3474 function In_Chain (E : Entity_Id) return Boolean;
3475 -- Check that the shadow entity is not already in the homonym
3476 -- chain, for example through a limited_with clause in a parent unit.
3478 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
3479 -- Check if some package installed though normal with-clauses has a
3480 -- renaming declaration of package P. AARM 10.1.2(21/2).
3486 function In_Chain (E : Entity_Id) return Boolean is
3487 H : Entity_Id := Current_Entity (E);
3490 while Present (H) loop
3501 ----------------------------------
3502 -- Is_Visible_Through_Renamings --
3503 ----------------------------------
3505 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
3506 Kind : constant Node_Kind := Nkind (Unit (Cunit (Current_Sem_Unit)));
3512 -- Example of the error detected by this subprogram:
3520 -- package Ren_P renames P;
3526 -- limited with P; -- ERROR
3527 -- package R.C is ...
3529 Aux_Unit := Cunit (Current_Sem_Unit);
3531 Item := First (Context_Items (Aux_Unit));
3532 while Present (Item) loop
3533 if Nkind (Item) = N_With_Clause
3534 and then not Limited_Present (Item)
3535 and then Nkind (Unit (Library_Unit (Item)))
3536 = N_Package_Declaration
3539 First (Visible_Declarations
3540 (Specification (Unit (Library_Unit (Item)))));
3541 while Present (Decl) loop
3542 if Nkind (Decl) = N_Package_Renaming_Declaration
3543 and then Entity (Name (Decl)) = P
3545 -- Generate the error message only if the current unit
3546 -- is a package declaration; in case of subprogram
3547 -- bodies and package bodies we just return true to
3548 -- indicate that the limited view must not be
3551 if Kind = N_Package_Declaration then
3552 Error_Msg_Sloc := Sloc (Item);
3554 ("unlimited view of & visible through the context"
3555 & " clause found #", N, P);
3557 Error_Msg_Sloc := Sloc (Decl);
3559 ("unlimited view of & visible through the"
3560 & " renaming found #", N, P);
3563 ("simultaneous visibility of the limited and"
3564 & " unlimited views not allowed", N);
3577 if Present (Library_Unit (Aux_Unit)) then
3578 Aux_Unit := Library_Unit (Aux_Unit);
3580 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
3583 exit when not Present (Aux_Unit);
3587 end Is_Visible_Through_Renamings;
3589 -- Start of processing for Install_Limited_Withed_Unit
3592 -- In case of limited with_clause on subprograms, generics, instances,
3593 -- or renamings, the corresponding error was previously posted and we
3594 -- have nothing to do here.
3596 if Nkind (P_Unit) /= N_Package_Declaration then
3600 P := Defining_Unit_Name (Specification (P_Unit));
3602 if Nkind (P) = N_Defining_Program_Unit_Name then
3604 -- Retrieve entity of child package
3606 Is_Child_Package := True;
3607 P := Defining_Identifier (P);
3610 -- Do not install the limited-view if the full-view is already visible
3611 -- through some renaming declaration
3613 if Is_Visible_Through_Renamings (P) then
3617 -- A common use of the limited-with is to have a limited-with
3618 -- in the package spec, and a normal with in its package body.
3621 -- limited with X; -- [1]
3625 -- package body A is ...
3627 -- The compilation of A's body installs the entities of its
3628 -- withed packages (the context clauses found at [2]) and
3629 -- then the context clauses of its specification (found at [1]).
3631 -- As a consequence, at point [1] the specification of X has been
3632 -- analyzed and it is immediately visible. According to the semantics
3633 -- of the limited-with context clauses we don't install the limited
3634 -- view because the full view of X supersedes its limited view.
3636 if Analyzed (Cunit (Unum))
3637 and then (Is_Immediately_Visible (P)
3638 or else (Is_Child_Package
3639 and then Is_Visible_Child_Unit (P)))
3641 -- Ada 2005 (AI-262): Install the private declarations of P
3643 if Private_Present (N)
3644 and then not In_Private_Part (P)
3649 Id := First_Private_Entity (P);
3651 while Present (Id) loop
3652 if not Is_Internal (Id)
3653 and then not Is_Child_Unit (Id)
3655 if not In_Chain (Id) then
3656 Set_Homonym (Id, Current_Entity (Id));
3657 Set_Current_Entity (Id);
3660 Set_Is_Immediately_Visible (Id);
3666 Set_In_Private_Part (P);
3673 if Debug_Flag_I then
3674 Write_Str ("install limited view of ");
3675 Write_Name (Chars (P));
3679 if not Analyzed (Cunit (Unum)) then
3680 Set_Ekind (P, E_Package);
3681 Set_Etype (P, Standard_Void_Type);
3682 Set_Scope (P, Standard_Standard);
3684 -- Place entity on visibility structure
3686 if Current_Entity (P) /= P then
3687 Set_Homonym (P, Current_Entity (P));
3688 Set_Current_Entity (P);
3690 if Debug_Flag_I then
3691 Write_Str (" (homonym) chain ");
3692 Write_Name (Chars (P));
3698 if Is_Child_Package then
3699 Set_Is_Child_Unit (P);
3700 Set_Is_Visible_Child_Unit (P);
3703 Parent_Comp : Node_Id;
3704 Parent_Id : Entity_Id;
3707 Parent_Comp := Parent_Spec (Unit (Cunit (Unum)));
3708 Parent_Id := Defining_Entity (Unit (Parent_Comp));
3710 Set_Scope (P, Parent_Id);
3715 -- If the unit appears in a previous regular with_clause, the
3716 -- regular entities must be unchained before the shadow ones
3717 -- are made accessible.
3722 Ent := First_Entity (P);
3724 while Present (Ent) loop
3731 -- The package must be visible while the limited-with clause is active,
3732 -- because references to the type P.T must resolve in the usual way.
3734 Set_Is_Immediately_Visible (P);
3736 -- Install each incomplete view. The first element of the limited view
3737 -- is a header (an E_Package entity) that is used to reference the first
3738 -- shadow entity in the private part of the package
3740 Lim_Header := Limited_View (P);
3741 Lim_Typ := First_Entity (Lim_Header);
3743 while Present (Lim_Typ) loop
3745 exit when not Private_Present (N)
3746 and then Lim_Typ = First_Private_Entity (Lim_Header);
3748 if not In_Chain (Lim_Typ) then
3749 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
3750 Set_Current_Entity (Lim_Typ);
3752 if Debug_Flag_I then
3753 Write_Str (" (homonym) chain ");
3754 Write_Name (Chars (Lim_Typ));
3759 Next_Entity (Lim_Typ);
3762 -- The context clause has installed a limited-view, mark it
3763 -- accordingly, to uninstall it when the context is removed.
3765 Set_Limited_View_Installed (N);
3766 Set_From_With_Type (P);
3767 end Install_Limited_Withed_Unit;
3769 -------------------------
3770 -- Install_Withed_Unit --
3771 -------------------------
3773 procedure Install_Withed_Unit
3774 (With_Clause : Node_Id;
3775 Private_With_OK : Boolean := False)
3777 Uname : constant Entity_Id := Entity (Name (With_Clause));
3778 P : constant Entity_Id := Scope (Uname);
3781 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
3782 -- compiling a package declaration and the Private_With_OK flag was not
3783 -- set by the caller. These declarations will be installed later (before
3784 -- analyzing the private part of the package).
3786 if Private_Present (With_Clause)
3787 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
3788 and then not (Private_With_OK)
3793 if Debug_Flag_I then
3794 if Private_Present (With_Clause) then
3795 Write_Str ("install private withed unit ");
3797 Write_Str ("install withed unit ");
3800 Write_Name (Chars (Uname));
3804 -- We do not apply the restrictions to an internal unit unless
3805 -- we are compiling the internal unit as a main unit. This check
3806 -- is also skipped for dummy units (for missing packages).
3808 if Sloc (Uname) /= No_Location
3809 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
3810 or else Current_Sem_Unit = Main_Unit)
3812 Check_Restricted_Unit
3813 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
3816 if P /= Standard_Standard then
3818 -- If the unit is not analyzed after analysis of the with clause,
3819 -- and it is an instantiation, then it awaits a body and is the main
3820 -- unit. Its appearance in the context of some other unit indicates
3821 -- a circular dependency (DEC suite perversity).
3823 if not Analyzed (Uname)
3824 and then Nkind (Parent (Uname)) = N_Package_Instantiation
3827 ("instantiation depends on itself", Name (With_Clause));
3829 elsif not Is_Visible_Child_Unit (Uname) then
3830 Set_Is_Visible_Child_Unit (Uname);
3832 -- If the child unit appears in the context of its parent, it
3833 -- is immediately visible.
3835 if In_Open_Scopes (Scope (Uname)) then
3836 Set_Is_Immediately_Visible (Uname);
3839 if Is_Generic_Instance (Uname)
3840 and then Ekind (Uname) in Subprogram_Kind
3842 -- Set flag as well on the visible entity that denotes the
3843 -- instance, which renames the current one.
3845 Set_Is_Visible_Child_Unit
3847 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
3850 -- The parent unit may have been installed already, and
3851 -- may have appeared in a use clause.
3853 if In_Use (Scope (Uname)) then
3854 Set_Is_Potentially_Use_Visible (Uname);
3857 Set_Context_Installed (With_Clause);
3860 elsif not Is_Immediately_Visible (Uname) then
3861 if not Private_Present (With_Clause)
3862 or else Private_With_OK
3864 Set_Is_Immediately_Visible (Uname);
3867 Set_Context_Installed (With_Clause);
3870 -- A with-clause overrides a with-type clause: there are no restric-
3871 -- tions on the use of package entities.
3873 if Ekind (Uname) = E_Package then
3874 Set_From_With_Type (Uname, False);
3877 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
3878 -- unit if there is a visible homograph for it declared in the same
3879 -- declarative region. This pathological case can only arise when an
3880 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
3881 -- G1 has a generic child also named G2, and the context includes with_
3882 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
3883 -- of I1.G2 visible as well. If the child unit is named Standard, do
3884 -- not apply the check to the Standard package itself.
3886 if Is_Child_Unit (Uname)
3887 and then Is_Visible_Child_Unit (Uname)
3888 and then Ada_Version >= Ada_05
3891 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
3897 U2 := Homonym (Uname);
3899 and U2 /= Standard_Standard
3902 Decl2 := Unit_Declaration_Node (P2);
3904 if Is_Child_Unit (U2)
3905 and then Is_Visible_Child_Unit (U2)
3907 if Is_Generic_Instance (P)
3908 and then Nkind (Decl1) = N_Package_Declaration
3909 and then Generic_Parent (Specification (Decl1)) = P2
3911 Error_Msg_N ("illegal with_clause", With_Clause);
3913 ("\child unit has visible homograph" &
3914 " ('R'M 8.3(26), 10.1.1(19))",
3918 elsif Is_Generic_Instance (P2)
3919 and then Nkind (Decl2) = N_Package_Declaration
3920 and then Generic_Parent (Specification (Decl2)) = P
3922 -- With_clause for child unit of instance appears before
3923 -- in the context. We want to place the error message on
3924 -- it, not on the generic child unit itself.
3927 Prev_Clause : Node_Id;
3930 Prev_Clause := First (List_Containing (With_Clause));
3931 while Entity (Name (Prev_Clause)) /= U2 loop
3935 pragma Assert (Present (Prev_Clause));
3936 Error_Msg_N ("illegal with_clause", Prev_Clause);
3938 ("\child unit has visible homograph" &
3939 " ('R'M 8.3(26), 10.1.1(19))",
3950 end Install_Withed_Unit;
3956 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
3957 K : constant Node_Kind := Nkind (Lib_Unit);
3960 return (K in N_Generic_Declaration or else
3961 K in N_Generic_Instantiation or else
3962 K in N_Generic_Renaming_Declaration or else
3963 K = N_Package_Declaration or else
3964 K = N_Package_Renaming_Declaration or else
3965 K = N_Subprogram_Declaration or else
3966 K = N_Subprogram_Renaming_Declaration)
3967 and then Present (Parent_Spec (Lib_Unit));
3970 -----------------------
3971 -- Load_Needed_Body --
3972 -----------------------
3974 -- N is a generic unit named in a with clause, or else it is
3975 -- a unit that contains a generic unit or an inlined function.
3976 -- In order to perform an instantiation, the body of the unit
3977 -- must be present. If the unit itself is generic, we assume
3978 -- that an instantiation follows, and load and analyze the body
3979 -- unconditionally. This forces analysis of the spec as well.
3981 -- If the unit is not generic, but contains a generic unit, it
3982 -- is loaded on demand, at the point of instantiation (see ch12).
3984 procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
3985 Body_Name : Unit_Name_Type;
3986 Unum : Unit_Number_Type;
3988 Save_Style_Check : constant Boolean := Opt.Style_Check;
3989 -- The loading and analysis is done with style checks off
3992 if not GNAT_Mode then
3993 Style_Check := False;
3996 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
3999 (Load_Name => Body_Name,
4005 if Unum = No_Unit then
4009 Compiler_State := Analyzing; -- reset after load
4011 if not Fatal_Error (Unum) or else Try_Semantics then
4012 if Debug_Flag_L then
4013 Write_Str ("*** Loaded generic body");
4017 Semantics (Cunit (Unum));
4023 Style_Check := Save_Style_Check;
4024 end Load_Needed_Body;
4026 -------------------------
4027 -- Build_Limited_Views --
4028 -------------------------
4030 procedure Build_Limited_Views (N : Node_Id) is
4031 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
4032 P : constant Entity_Id := Cunit_Entity (Unum);
4034 Spec : Node_Id; -- To denote a package specification
4035 Lim_Typ : Entity_Id; -- To denote shadow entities
4036 Comp_Typ : Entity_Id; -- To denote real entities
4038 Lim_Header : Entity_Id; -- Package entity
4039 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
4040 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
4042 procedure Decorate_Incomplete_Type
4045 -- Add attributes of an incomplete type to a shadow entity. The same
4046 -- attributes are placed on the real entity, so that gigi receives
4047 -- a consistent view.
4049 procedure Decorate_Package_Specification (P : Entity_Id);
4050 -- Add attributes of a package entity to the entity in a package
4053 procedure Decorate_Tagged_Type
4057 -- Set basic attributes of tagged type T, including its class_wide type.
4058 -- The parameters Loc, Scope are used to decorate the class_wide type.
4060 procedure Build_Chain
4062 First_Decl : Node_Id);
4063 -- Construct list of shadow entities and attach it to entity of
4064 -- package that is mentioned in a limited_with clause.
4066 function New_Internal_Shadow_Entity
4067 (Kind : Entity_Kind;
4068 Sloc_Value : Source_Ptr;
4069 Id_Char : Character) return Entity_Id;
4070 -- Build a new internal entity and append it to the list of shadow
4071 -- entities available through the limited-header
4073 ------------------------------
4074 -- Decorate_Incomplete_Type --
4075 ------------------------------
4077 procedure Decorate_Incomplete_Type
4082 Set_Ekind (E, E_Incomplete_Type);
4083 Set_Scope (E, Scop);
4085 Set_Is_First_Subtype (E, True);
4086 Set_Stored_Constraint (E, No_Elist);
4087 Set_Full_View (E, Empty);
4088 Init_Size_Align (E);
4089 end Decorate_Incomplete_Type;
4091 --------------------------
4092 -- Decorate_Tagged_Type --
4093 --------------------------
4095 procedure Decorate_Tagged_Type
4103 Decorate_Incomplete_Type (T, Scop);
4104 Set_Is_Tagged_Type (T);
4106 -- Build corresponding class_wide type, if not previously done
4108 if No (Class_Wide_Type (T)) then
4109 CW := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
4111 Set_Ekind (CW, E_Class_Wide_Type);
4113 Set_Scope (CW, Scop);
4114 Set_Is_Tagged_Type (CW);
4115 Set_Is_First_Subtype (CW, True);
4116 Init_Size_Align (CW);
4117 Set_Has_Unknown_Discriminants (CW, True);
4118 Set_Class_Wide_Type (CW, CW);
4119 Set_Equivalent_Type (CW, Empty);
4120 Set_From_With_Type (CW, From_With_Type (T));
4122 Set_Class_Wide_Type (T, CW);
4124 end Decorate_Tagged_Type;
4126 ------------------------------------
4127 -- Decorate_Package_Specification --
4128 ------------------------------------
4130 procedure Decorate_Package_Specification (P : Entity_Id) is
4132 -- Place only the most basic attributes
4134 Set_Ekind (P, E_Package);
4135 Set_Etype (P, Standard_Void_Type);
4136 end Decorate_Package_Specification;
4138 -------------------------
4139 -- New_Internal_Entity --
4140 -------------------------
4142 function New_Internal_Shadow_Entity
4143 (Kind : Entity_Kind;
4144 Sloc_Value : Source_Ptr;
4145 Id_Char : Character) return Entity_Id
4147 E : constant Entity_Id :=
4148 Make_Defining_Identifier (Sloc_Value,
4149 Chars => New_Internal_Name (Id_Char));
4152 Set_Ekind (E, Kind);
4153 Set_Is_Internal (E, True);
4155 if Kind in Type_Kind then
4156 Init_Size_Align (E);
4159 Append_Entity (E, Lim_Header);
4162 end New_Internal_Shadow_Entity;
4168 procedure Build_Chain
4170 First_Decl : Node_Id)
4172 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
4173 Is_Tagged : Boolean;
4179 while Present (Decl) loop
4181 -- For each library_package_declaration in the environment, there
4182 -- is an implicit declaration of a *limited view* of that library
4183 -- package. The limited view of a package contains:
4185 -- * For each nested package_declaration, a declaration of the
4186 -- limited view of that package, with the same defining-
4187 -- program-unit name.
4189 -- * For each type_declaration in the visible part, an incomplete
4190 -- type-declaration with the same defining_identifier, whose
4191 -- completion is the type_declaration. If the type_declaration
4192 -- is tagged, then the incomplete_type_declaration is tagged
4195 if Nkind (Decl) = N_Full_Type_Declaration then
4197 Nkind (Type_Definition (Decl)) = N_Record_Definition
4198 and then Tagged_Present (Type_Definition (Decl));
4200 Comp_Typ := Defining_Identifier (Decl);
4202 if not Analyzed_Unit then
4204 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
4206 Decorate_Incomplete_Type (Comp_Typ, Scope);
4210 -- Create shadow entity for type
4212 Lim_Typ := New_Internal_Shadow_Entity
4213 (Kind => Ekind (Comp_Typ),
4214 Sloc_Value => Sloc (Comp_Typ),
4217 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4218 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4219 Set_From_With_Type (Lim_Typ);
4222 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
4224 Decorate_Incomplete_Type (Lim_Typ, Scope);
4227 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
4229 elsif Nkind (Decl) = N_Private_Type_Declaration then
4230 Comp_Typ := Defining_Identifier (Decl);
4232 if not Analyzed_Unit then
4233 if Tagged_Present (Decl) then
4234 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
4236 Decorate_Incomplete_Type (Comp_Typ, Scope);
4240 Lim_Typ := New_Internal_Shadow_Entity
4241 (Kind => Ekind (Comp_Typ),
4242 Sloc_Value => Sloc (Comp_Typ),
4245 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4246 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4247 Set_From_With_Type (Lim_Typ);
4249 if Tagged_Present (Decl) then
4250 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
4252 Decorate_Incomplete_Type (Lim_Typ, Scope);
4255 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
4257 elsif Nkind (Decl) = N_Private_Extension_Declaration then
4258 Comp_Typ := Defining_Identifier (Decl);
4260 if not Analyzed_Unit then
4261 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
4264 -- Create shadow entity for type
4266 Lim_Typ := New_Internal_Shadow_Entity
4267 (Kind => Ekind (Comp_Typ),
4268 Sloc_Value => Sloc (Comp_Typ),
4271 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4272 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4273 Set_From_With_Type (Lim_Typ);
4275 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
4276 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
4278 elsif Nkind (Decl) = N_Package_Declaration then
4283 Spec : constant Node_Id := Specification (Decl);
4286 Comp_Typ := Defining_Unit_Name (Spec);
4288 if not Analyzed (Cunit (Unum)) then
4289 Decorate_Package_Specification (Comp_Typ);
4290 Set_Scope (Comp_Typ, Scope);
4293 Lim_Typ := New_Internal_Shadow_Entity
4294 (Kind => Ekind (Comp_Typ),
4295 Sloc_Value => Sloc (Comp_Typ),
4298 Decorate_Package_Specification (Lim_Typ);
4299 Set_Scope (Lim_Typ, Scope);
4301 Set_Chars (Lim_Typ, Chars (Comp_Typ));
4302 Set_Parent (Lim_Typ, Parent (Comp_Typ));
4303 Set_From_With_Type (Lim_Typ);
4305 -- Note: The non_limited_view attribute is not used
4306 -- for local packages.
4310 First_Decl => First (Visible_Declarations (Spec)));
4318 -- Start of processing for Build_Limited_Views
4321 pragma Assert (Limited_Present (N));
4323 -- A library_item mentioned in a limited_with_clause shall be
4324 -- a package_declaration, not a subprogram_declaration,
4325 -- generic_declaration, generic_instantiation, or
4326 -- package_renaming_declaration
4328 case Nkind (Unit (Library_Unit (N))) is
4330 when N_Package_Declaration =>
4333 when N_Subprogram_Declaration =>
4334 Error_Msg_N ("subprograms not allowed in "
4335 & "limited with_clauses", N);
4338 when N_Generic_Package_Declaration |
4339 N_Generic_Subprogram_Declaration =>
4340 Error_Msg_N ("generics not allowed in "
4341 & "limited with_clauses", N);
4344 when N_Package_Instantiation |
4345 N_Function_Instantiation |
4346 N_Procedure_Instantiation =>
4347 Error_Msg_N ("generic instantiations not allowed in "
4348 & "limited with_clauses", N);
4351 when N_Generic_Package_Renaming_Declaration |
4352 N_Generic_Procedure_Renaming_Declaration |
4353 N_Generic_Function_Renaming_Declaration =>
4354 Error_Msg_N ("generic renamings not allowed in "
4355 & "limited with_clauses", N);
4358 when N_Subprogram_Renaming_Declaration =>
4359 Error_Msg_N ("renamed subprograms not allowed in "
4360 & "limited with_clauses", N);
4363 when N_Package_Renaming_Declaration =>
4364 Error_Msg_N ("renamed packages not allowed in "
4365 & "limited with_clauses", N);
4369 raise Program_Error;
4372 -- Check if the chain is already built
4374 Spec := Specification (Unit (Library_Unit (N)));
4376 if Limited_View_Installed (Spec) then
4380 Set_Ekind (P, E_Package);
4382 -- Build the header of the limited_view
4384 Lim_Header := Make_Defining_Identifier (Sloc (N),
4385 Chars => New_Internal_Name (Id_Char => 'Z'));
4386 Set_Ekind (Lim_Header, E_Package);
4387 Set_Is_Internal (Lim_Header);
4388 Set_Limited_View (P, Lim_Header);
4390 -- Create the auxiliary chain. All the shadow entities are appended
4391 -- to the list of entities of the limited-view header
4395 First_Decl => First (Visible_Declarations (Spec)));
4397 -- Save the last built shadow entity. It is needed later to set the
4398 -- reference to the first shadow entity in the private part
4400 Last_Pub_Lim_E := Last_Lim_E;
4402 -- Ada 2005 (AI-262): Add the limited view of the private declarations
4403 -- Required to give support to limited-private-with clauses
4405 Build_Chain (Scope => P,
4406 First_Decl => First (Private_Declarations (Spec)));
4408 if Last_Pub_Lim_E /= Empty then
4409 Set_First_Private_Entity (Lim_Header,
4410 Next_Entity (Last_Pub_Lim_E));
4412 Set_First_Private_Entity (Lim_Header,
4416 Set_Limited_View_Installed (Spec);
4417 end Build_Limited_Views;
4419 -------------------------------
4420 -- Check_Body_Needed_For_SAL --
4421 -------------------------------
4423 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
4425 function Entity_Needs_Body (E : Entity_Id) return Boolean;
4426 -- Determine whether use of entity E might require the presence
4427 -- of its body. For a package this requires a recursive traversal
4428 -- of all nested declarations.
4430 ---------------------------
4431 -- Entity_Needed_For_SAL --
4432 ---------------------------
4434 function Entity_Needs_Body (E : Entity_Id) return Boolean is
4438 if Is_Subprogram (E)
4439 and then Has_Pragma_Inline (E)
4443 elsif Ekind (E) = E_Generic_Function
4444 or else Ekind (E) = E_Generic_Procedure
4448 elsif Ekind (E) = E_Generic_Package
4450 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
4451 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
4455 elsif Ekind (E) = E_Package
4457 Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
4458 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
4460 Ent := First_Entity (E);
4462 while Present (Ent) loop
4463 if Entity_Needs_Body (Ent) then
4475 end Entity_Needs_Body;
4477 -- Start of processing for Check_Body_Needed_For_SAL
4480 if Ekind (Unit_Name) = E_Generic_Package
4482 Nkind (Unit_Declaration_Node (Unit_Name)) =
4483 N_Generic_Package_Declaration
4485 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
4487 Set_Body_Needed_For_SAL (Unit_Name);
4489 elsif Ekind (Unit_Name) = E_Generic_Procedure
4490 or else Ekind (Unit_Name) = E_Generic_Function
4492 Set_Body_Needed_For_SAL (Unit_Name);
4494 elsif Is_Subprogram (Unit_Name)
4495 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
4496 N_Subprogram_Declaration
4497 and then Has_Pragma_Inline (Unit_Name)
4499 Set_Body_Needed_For_SAL (Unit_Name);
4501 elsif Ekind (Unit_Name) = E_Subprogram_Body then
4502 Check_Body_Needed_For_SAL
4503 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
4505 elsif Ekind (Unit_Name) = E_Package
4506 and then Entity_Needs_Body (Unit_Name)
4508 Set_Body_Needed_For_SAL (Unit_Name);
4510 elsif Ekind (Unit_Name) = E_Package_Body
4511 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
4513 Check_Body_Needed_For_SAL
4514 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
4516 end Check_Body_Needed_For_SAL;
4518 --------------------
4519 -- Remove_Context --
4520 --------------------
4522 procedure Remove_Context (N : Node_Id) is
4523 Lib_Unit : constant Node_Id := Unit (N);
4526 -- If this is a child unit, first remove the parent units
4528 if Is_Child_Spec (Lib_Unit) then
4529 Remove_Parents (Lib_Unit);
4532 Remove_Context_Clauses (N);
4535 ----------------------------
4536 -- Remove_Context_Clauses --
4537 ----------------------------
4539 procedure Remove_Context_Clauses (N : Node_Id) is
4541 Unit_Name : Entity_Id;
4544 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
4545 -- limited-views first and regular-views later (to maintain the
4548 -- First Phase: Remove limited_with context clauses
4550 Item := First (Context_Items (N));
4551 while Present (Item) loop
4553 -- We are interested only in with clauses which got installed
4556 if Nkind (Item) = N_With_Clause
4557 and then Limited_Present (Item)
4558 and then Limited_View_Installed (Item)
4560 Remove_Limited_With_Clause (Item);
4566 -- Second Phase: Loop through context items and undo regular
4567 -- with_clauses and use_clauses.
4569 Item := First (Context_Items (N));
4570 while Present (Item) loop
4572 -- We are interested only in with clauses which got installed
4573 -- on entry, as indicated by their Context_Installed flag set
4575 if Nkind (Item) = N_With_Clause
4576 and then Limited_Present (Item)
4577 and then Limited_View_Installed (Item)
4581 elsif Nkind (Item) = N_With_Clause
4582 and then Context_Installed (Item)
4584 -- Remove items from one with'ed unit
4586 Unit_Name := Entity (Name (Item));
4587 Remove_Unit_From_Visibility (Unit_Name);
4588 Set_Context_Installed (Item, False);
4590 elsif Nkind (Item) = N_Use_Package_Clause then
4591 End_Use_Package (Item);
4593 elsif Nkind (Item) = N_Use_Type_Clause then
4594 End_Use_Type (Item);
4596 elsif Nkind (Item) = N_With_Type_Clause then
4597 Remove_With_Type_Clause (Name (Item));
4602 end Remove_Context_Clauses;
4604 --------------------------------
4605 -- Remove_Limited_With_Clause --
4606 --------------------------------
4608 procedure Remove_Limited_With_Clause (N : Node_Id) is
4609 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4610 P : Entity_Id := Defining_Unit_Name (Specification (P_Unit));
4611 Lim_Typ : Entity_Id;
4614 if Nkind (P) = N_Defining_Program_Unit_Name then
4616 -- Retrieve entity of Child package
4618 P := Defining_Identifier (P);
4621 if Debug_Flag_I then
4622 Write_Str ("remove limited view of ");
4623 Write_Name (Chars (P));
4624 Write_Str (" from visibility");
4628 -- Remove all shadow entities from visibility. The first element of the
4629 -- limited view is a header (an E_Package entity) that is used to
4630 -- reference the first shadow entity in the private part of the package
4632 Lim_Typ := First_Entity (Limited_View (P));
4634 while Present (Lim_Typ) loop
4636 Next_Entity (Lim_Typ);
4639 -- Indicate that the limited view of the package is not installed
4641 Set_From_With_Type (P, False);
4642 Set_Limited_View_Installed (N, False);
4644 -- If the exporting package has previously been analyzed, it
4645 -- has appeared in the closure already and should be left alone.
4646 -- Otherwise, remove package itself from visibility.
4648 if not Analyzed (P_Unit) then
4650 Set_First_Entity (P, Empty);
4651 Set_Last_Entity (P, Empty);
4652 Set_Ekind (P, E_Void);
4653 Set_Scope (P, Empty);
4654 Set_Is_Immediately_Visible (P, False);
4658 -- Reinstall visible entities (entities removed from visibility in
4659 -- Install_Limited_Withed to install the shadow entities).
4665 Ent := First_Entity (P);
4666 while Present (Ent) and then Ent /= First_Private_Entity (P) loop
4668 -- Shadow entities have not been added to the list of
4669 -- entities associated to the package spec. Therefore we
4670 -- just have to re-chain all its visible entities.
4672 if not Is_Class_Wide_Type (Ent) then
4674 Set_Homonym (Ent, Current_Entity (Ent));
4675 Set_Current_Entity (Ent);
4677 if Debug_Flag_I then
4678 Write_Str (" (homonym) chain ");
4679 Write_Name (Chars (Ent));
4688 end Remove_Limited_With_Clause;
4690 --------------------
4691 -- Remove_Parents --
4692 --------------------
4694 procedure Remove_Parents (Lib_Unit : Node_Id) is
4697 P_Spec : Node_Id := Empty;
4699 Vis : constant Boolean :=
4700 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
4703 if Is_Child_Spec (Lib_Unit) then
4704 P_Spec := Parent_Spec (Lib_Unit);
4706 elsif Nkind (Lib_Unit) = N_Package_Body
4707 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
4709 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
4712 if Present (P_Spec) then
4715 P_Name := Get_Parent_Entity (P);
4716 Remove_Context_Clauses (P_Spec);
4717 End_Package_Scope (P_Name);
4718 Set_Is_Immediately_Visible (P_Name, Vis);
4720 -- Remove from visibility the siblings as well, which are directly
4721 -- visible while the parent is in scope.
4723 E := First_Entity (P_Name);
4725 while Present (E) loop
4727 if Is_Child_Unit (E) then
4728 Set_Is_Immediately_Visible (E, False);
4734 Set_In_Package_Body (P_Name, False);
4736 -- This is the recursive call to remove the context of any
4737 -- higher level parent. This recursion ensures that all parents
4738 -- are removed in the reverse order of their installation.
4744 -----------------------------
4745 -- Remove_With_Type_Clause --
4746 -----------------------------
4748 procedure Remove_With_Type_Clause (Name : Node_Id) is
4752 procedure Unchain (E : Entity_Id);
4753 -- Remove entity from visibility list
4759 procedure Unchain (E : Entity_Id) is
4763 Prev := Current_Entity (E);
4765 -- Package entity may appear is several with_type_clauses, and
4766 -- may have been removed already.
4772 Set_Name_Entity_Id (Chars (E), Homonym (E));
4775 while Present (Prev)
4776 and then Homonym (Prev) /= E
4778 Prev := Homonym (Prev);
4781 if Present (Prev) then
4782 Set_Homonym (Prev, Homonym (E));
4787 -- Start of processing for Remove_With_Type_Clause
4790 if Nkind (Name) = N_Selected_Component then
4791 Typ := Entity (Selector_Name (Name));
4793 -- If no Typ, then error in declaration, ignore
4804 -- If the exporting package has been analyzed, it has appeared in the
4805 -- context already and should be left alone. Otherwise, remove from
4808 if not Analyzed (Unit_Declaration_Node (P)) then
4811 Set_Is_Frozen (Typ, False);
4814 if Ekind (Typ) = E_Record_Type then
4815 Set_From_With_Type (Class_Wide_Type (Typ), False);
4816 Set_From_With_Type (Typ, False);
4819 Set_From_With_Type (P, False);
4821 -- If P is a child unit, remove parents as well
4826 and then P /= Standard_Standard
4828 Set_From_With_Type (P, False);
4830 if not Analyzed (Unit_Declaration_Node (P)) then
4837 -- The back-end needs to know that an access type is imported, so it
4838 -- does not need elaboration and can appear in a mutually recursive
4839 -- record definition, so the imported flag on an access type is
4842 end Remove_With_Type_Clause;
4844 ---------------------------------
4845 -- Remove_Unit_From_Visibility --
4846 ---------------------------------
4848 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
4849 P : constant Entity_Id := Scope (Unit_Name);
4853 if Debug_Flag_I then
4854 Write_Str ("remove unit ");
4855 Write_Name (Chars (Unit_Name));
4856 Write_Str (" from visibility");
4860 if P /= Standard_Standard then
4861 Set_Is_Visible_Child_Unit (Unit_Name, False);
4864 Set_Is_Potentially_Use_Visible (Unit_Name, False);
4865 Set_Is_Immediately_Visible (Unit_Name, False);
4867 end Remove_Unit_From_Visibility;
4873 procedure Unchain (E : Entity_Id) is
4877 Prev := Current_Entity (E);
4883 Set_Name_Entity_Id (Chars (E), Homonym (E));
4886 while Present (Prev)
4887 and then Homonym (Prev) /= E
4889 Prev := Homonym (Prev);
4892 if Present (Prev) then
4893 Set_Homonym (Prev, Homonym (E));
4897 if Debug_Flag_I then
4898 Write_Str (" (homonym) unchain ");
4899 Write_Name (Chars (E));