1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Exp_Util; use Exp_Util;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Impunit; use Impunit;
38 with Inline; use Inline;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
46 with Output; use Output;
47 with Restrict; use Restrict;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dist; use Sem_Dist;
53 with Sem_Prag; use Sem_Prag;
54 with Sem_Util; use Sem_Util;
55 with Sem_Warn; use Sem_Warn;
56 with Stand; use Stand;
57 with Sinfo; use Sinfo;
58 with Sinfo.CN; use Sinfo.CN;
59 with Sinput; use Sinput;
60 with Snames; use Snames;
61 with Style; use Style;
62 with 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 Check_With_Type_Clauses (N : Node_Id);
76 -- If N is a body, verify that any with_type clauses on the spec, or
77 -- on the spec of any parent, have a matching with_clause.
79 procedure Check_Private_Child_Unit (N : Node_Id);
80 -- If a with_clause mentions a private child unit, the compilation
81 -- unit must be a member of the same family, as described in 10.1.2 (8).
83 procedure Check_Stub_Level (N : Node_Id);
84 -- Verify that a stub is declared immediately within a compilation unit,
85 -- and not in an inner frame.
87 procedure Expand_With_Clause (Nam : Node_Id; N : Node_Id);
88 -- When a child unit appears in a context clause, the implicit withs on
89 -- parents are made explicit, and with clauses are inserted in the context
90 -- clause before the one for the child. If a parent in the with_clause
91 -- is a renaming, the implicit with_clause is on the renaming whose name
92 -- is mentioned in the with_clause, and not on the package it renames.
93 -- N is the compilation unit whose list of context items receives the
94 -- implicit with_clauses.
96 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
97 -- If the main unit is a child unit, implicit withs are also added for
100 procedure Install_Context_Clauses (N : Node_Id);
101 -- Subsidiary to previous one. Process only with_ and use_clauses for
102 -- current unit and its library unit if any.
104 procedure Install_Withed_Unit (With_Clause : Node_Id);
105 -- If the unit is not a child unit, make unit immediately visible.
106 -- The caller ensures that the unit is not already currently installed.
108 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
109 -- This procedure establishes the context for the compilation of a child
110 -- unit. If Lib_Unit is a child library spec then the context of the parent
111 -- is installed, and the parent itself made immediately visible, so that
112 -- the child unit is processed in the declarative region of the parent.
113 -- Install_Parents makes a recursive call to itself to ensure that all
114 -- parents are loaded in the nested case. If Lib_Unit is a library body,
115 -- the only effect of Install_Parents is to install the private decls of
116 -- the parents, because the visible parent declarations will have been
117 -- installed as part of the context of the corresponding spec.
119 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
120 -- In the compilation of a child unit, a child of any of the ancestor
121 -- units is directly visible if it is visible, because the parent is in
122 -- an enclosing scope. Iterate over context to find child units of U_Name
123 -- or of some ancestor of it.
125 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
126 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
127 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
128 -- a library spec that has a parent. If the call to Is_Child_Spec returns
129 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
130 -- compilation unit for the parent spec.
132 -- Lib_Unit can also be a subprogram body that acts as its own spec. If
133 -- the Parent_Spec is non-empty, this is also a child unit.
135 procedure Remove_With_Type_Clause (Name : Node_Id);
136 -- Remove imported type and its enclosing package from visibility, and
137 -- remove attributes of imported type so they don't interfere with its
138 -- analysis (should it appear otherwise in the context).
140 procedure Remove_Context_Clauses (N : Node_Id);
141 -- Subsidiary of previous one. Remove use_ and with_clauses.
143 procedure Remove_Parents (Lib_Unit : Node_Id);
144 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
145 -- contexts established by the corresponding call to Install_Parents are
146 -- removed. Remove_Parents contains a recursive call to itself to ensure
147 -- that all parents are removed in the nested case.
149 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
150 -- Reset all visibility flags on unit after compiling it, either as a
151 -- main unit or as a unit in the context.
153 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
154 -- Common processing for all stubs (subprograms, tasks, packages, and
155 -- protected cases). N is the stub to be analyzed. Once the subunit
156 -- name is established, load and analyze. Nam is the non-overloadable
157 -- entity for which the proper body provides a completion. Subprogram
158 -- stubs are handled differently because they can be declarations.
160 ------------------------------
161 -- Analyze_Compilation_Unit --
162 ------------------------------
164 procedure Analyze_Compilation_Unit (N : Node_Id) is
165 Unit_Node : constant Node_Id := Unit (N);
166 Lib_Unit : Node_Id := Library_Unit (N);
168 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
169 Par_Spec_Name : Unit_Name_Type;
170 Unum : Unit_Number_Type;
172 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
173 -- Generate cross-reference information for the parents of child units.
174 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
176 --------------------------------
177 -- Generate_Parent_References --
178 --------------------------------
180 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
182 P_Name : Entity_Id := P_Id;
185 Pref := Name (Parent (Defining_Entity (N)));
187 if Nkind (Pref) = N_Expanded_Name then
189 -- Done already, if the unit has been compiled indirectly as
190 -- part of the closure of its context because of inlining.
195 while Nkind (Pref) = N_Selected_Component loop
196 Change_Selected_Component_To_Expanded_Name (Pref);
197 Set_Entity (Pref, P_Name);
198 Set_Etype (Pref, Etype (P_Name));
199 Generate_Reference (P_Name, Pref, 'r');
200 Pref := Prefix (Pref);
201 P_Name := Scope (P_Name);
204 -- The guard here on P_Name is to handle the error condition where
205 -- the parent unit is missing because the file was not found.
207 if Present (P_Name) then
208 Set_Entity (Pref, P_Name);
209 Set_Etype (Pref, Etype (P_Name));
210 Generate_Reference (P_Name, Pref, 'r');
211 Style.Check_Identifier (Pref, P_Name);
213 end Generate_Parent_References;
215 -- Start of processing for Analyze_Compilation_Unit
218 Process_Compilation_Unit_Pragmas (N);
220 -- If the unit is a subunit whose parent has not been analyzed (which
221 -- indicates that the main unit is a subunit, either the current one or
222 -- one of its descendents) then the subunit is compiled as part of the
223 -- analysis of the parent, which we proceed to do. Basically this gets
224 -- handled from the top down and we don't want to do anything at this
225 -- level (i.e. this subunit will be handled on the way down from the
226 -- parent), so at this level we immediately return. If the subunit
227 -- ends up not analyzed, it means that the parent did not contain a
228 -- stub for it, or that there errors were dectected in some ancestor.
230 if Nkind (Unit_Node) = N_Subunit
231 and then not Analyzed (Lib_Unit)
233 Semantics (Lib_Unit);
235 if not Analyzed (Proper_Body (Unit_Node)) then
236 if Errors_Detected > 0 then
237 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
239 Error_Msg_N ("missing stub for subunit", N);
246 -- Analyze context (this will call Sem recursively for with'ed units)
250 -- If the unit is a package body, the spec is already loaded and must
251 -- be analyzed first, before we analyze the body.
253 if Nkind (Unit_Node) = N_Package_Body then
255 -- If no Lib_Unit, then there was a serious previous error, so
256 -- just ignore the entire analysis effort
258 if No (Lib_Unit) then
262 Semantics (Lib_Unit);
263 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
265 -- Verify that the library unit is a package declaration.
267 if Nkind (Unit (Lib_Unit)) /= N_Package_Declaration
269 Nkind (Unit (Lib_Unit)) /= N_Generic_Package_Declaration
272 ("no legal package declaration for package body", N);
275 -- Otherwise, the entity in the declaration is visible. Update
276 -- the version to reflect dependence of this body on the spec.
279 Spec_Id := Defining_Entity (Unit (Lib_Unit));
280 Set_Is_Immediately_Visible (Spec_Id, True);
281 Version_Update (N, Lib_Unit);
283 if Nkind (Defining_Unit_Name (Unit_Node))
284 = N_Defining_Program_Unit_Name
286 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
291 -- If the unit is a subprogram body, then we similarly need to analyze
292 -- its spec. However, things are a little simpler in this case, because
293 -- here, this analysis is done only for error checking and consistency
294 -- purposes, so there's nothing else to be done.
296 elsif Nkind (Unit_Node) = N_Subprogram_Body then
297 if Acts_As_Spec (N) then
299 -- If the subprogram body is a child unit, we must create a
300 -- declaration for it, in order to properly load the parent(s).
301 -- After this, the original unit does not acts as a spec, because
302 -- there is an explicit one. If this unit appears in a context
303 -- clause, then an implicit with on the parent will be added when
304 -- installing the context. If this is the main unit, there is no
305 -- Unit_Table entry for the declaration, (It has the unit number
306 -- of the main unit) and code generation is unaffected.
308 Unum := Get_Cunit_Unit_Number (N);
309 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
311 if Par_Spec_Name /= No_Name then
314 (Load_Name => Par_Spec_Name,
319 if Unum /= No_Unit then
321 -- Build subprogram declaration and attach parent unit to it
322 -- This subprogram declaration does not come from source!
325 Loc : constant Source_Ptr := Sloc (N);
326 SCS : constant Boolean :=
327 Get_Comes_From_Source_Default;
330 Set_Comes_From_Source_Default (False);
332 Make_Compilation_Unit (Loc,
333 Context_Items => New_Copy_List (Context_Items (N)),
335 Make_Subprogram_Declaration (Sloc (N),
338 (Specification (Unit_Node))),
340 Make_Compilation_Unit_Aux (Loc));
342 Set_Library_Unit (N, Lib_Unit);
343 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
344 Semantics (Lib_Unit);
345 Set_Acts_As_Spec (N, False);
346 Set_Comes_From_Source_Default (SCS);
351 -- Here for subprogram with separate declaration
354 Semantics (Lib_Unit);
355 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
356 Version_Update (N, Lib_Unit);
359 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
360 N_Defining_Program_Unit_Name
362 Generate_Parent_References (
363 Specification (Unit_Node),
364 Scope (Defining_Entity (Unit (Lib_Unit))));
368 -- If it is a child unit, the parent must be elaborated first
369 -- and we update version, since we are dependent on our parent.
371 if Is_Child_Spec (Unit_Node) then
373 -- The analysis of the parent is done with style checks off
376 Save_Style_Check : constant Boolean := Opt.Style_Check;
377 Save_C_Restrict : constant Save_Compilation_Unit_Restrictions :=
378 Compilation_Unit_Restrictions_Save;
381 if not GNAT_Mode then
382 Style_Check := False;
385 Semantics (Parent_Spec (Unit_Node));
386 Version_Update (N, Parent_Spec (Unit_Node));
387 Style_Check := Save_Style_Check;
388 Compilation_Unit_Restrictions_Restore (Save_C_Restrict);
392 -- With the analysis done, install the context. Note that we can't
393 -- install the context from the with clauses as we analyze them,
394 -- because each with clause must be analyzed in a clean visibility
395 -- context, so we have to wait and install them all at once.
399 if Is_Child_Spec (Unit_Node) then
401 -- Set the entities of all parents in the program_unit_name.
403 Generate_Parent_References (
404 Unit_Node, Defining_Entity (Unit (Parent_Spec (Unit_Node))));
407 -- All components of the context: with-clauses, library unit, ancestors
408 -- if any, (and their context) are analyzed and installed. Now analyze
409 -- the unit itself, which is either a package, subprogram spec or body.
413 -- The above call might have made Unit_Node an N_Subprogram_Body
414 -- from something else, so propagate any Acts_As_Spec flag.
416 if Nkind (Unit_Node) = N_Subprogram_Body
417 and then Acts_As_Spec (Unit_Node)
419 Set_Acts_As_Spec (N);
422 -- Treat compilation unit pragmas that appear after the library unit
424 if Present (Pragmas_After (Aux_Decls_Node (N))) then
426 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
429 while Present (Prag_Node) loop
436 -- Generate distribution stub files if requested and no error
439 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
441 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
442 and then not Fatal_Error (Main_Unit)
444 if Is_RCI_Pkg_Spec_Or_Body (N) then
446 -- Regular RCI package
448 Add_Stub_Constructs (N);
450 elsif (Nkind (Unit_Node) = N_Package_Declaration
451 and then Is_Shared_Passive (Defining_Entity
452 (Specification (Unit_Node))))
453 or else (Nkind (Unit_Node) = N_Package_Body
455 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
457 -- Shared passive package
459 Add_Stub_Constructs (N);
461 elsif Nkind (Unit_Node) = N_Package_Instantiation
463 Is_Remote_Call_Interface
464 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
466 -- Instantiation of a RCI generic package
468 Add_Stub_Constructs (N);
471 -- Reanalyze the unit with the new constructs
476 if Nkind (Unit_Node) = N_Package_Declaration
477 or else Nkind (Unit_Node) in N_Generic_Declaration
478 or else Nkind (Unit_Node) = N_Package_Renaming_Declaration
479 or else Nkind (Unit_Node) = N_Subprogram_Declaration
481 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
483 elsif Nkind (Unit_Node) = N_Package_Body
484 or else (Nkind (Unit_Node) = N_Subprogram_Body
485 and then not Acts_As_Spec (Unit_Node))
487 -- Bodies that are not the main unit are compiled if they
488 -- are generic or contain generic or inlined units. Their
489 -- analysis brings in the context of the corresponding spec
490 -- (unit declaration) which must be removed as well, to
491 -- return the compilation environment to its proper state.
493 Remove_Context (Lib_Unit);
494 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
497 -- Last step is to deinstall the context we just installed
498 -- as well as the unit just compiled.
502 -- If this is the main unit and we are generating code, we must
503 -- check that all generic units in the context have a body if they
504 -- need it, even if they have not been instantiated. In the absence
505 -- of .ali files for generic units, we must force the load of the body,
506 -- just to produce the proper error if the body is absent. We skip this
507 -- verification if the main unit itself is generic.
509 if Get_Cunit_Unit_Number (N) = Main_Unit
510 and then Operating_Mode = Generate_Code
511 and then Expander_Active
513 -- Indicate that the main unit is now analyzed, to catch possible
514 -- circularities between it and generic bodies. Remove main unit
515 -- from visibility. This might seem superfluous, but the main unit
516 -- must not be visible in the generic body expansions that follow.
518 Set_Analyzed (N, True);
519 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
524 Un : Unit_Number_Type;
526 Save_Style_Check : constant Boolean := Opt.Style_Check;
527 Save_C_Restrict : constant Save_Compilation_Unit_Restrictions :=
528 Compilation_Unit_Restrictions_Save;
531 Item := First (Context_Items (N));
533 while Present (Item) loop
535 if Nkind (Item) = N_With_Clause
536 and then not Implicit_With (Item)
538 Nam := Entity (Name (Item));
540 if (Ekind (Nam) = E_Generic_Procedure
541 and then not Is_Intrinsic_Subprogram (Nam))
542 or else (Ekind (Nam) = E_Generic_Function
543 and then not Is_Intrinsic_Subprogram (Nam))
544 or else (Ekind (Nam) = E_Generic_Package
545 and then Unit_Requires_Body (Nam))
547 Opt.Style_Check := False;
549 if Present (Renamed_Object (Nam)) then
552 (Load_Name => Get_Body_Name
554 (Unit_Declaration_Node
555 (Renamed_Object (Nam)))),
563 (Load_Name => Get_Body_Name
564 (Get_Unit_Name (Item)),
573 ("body of generic unit& not found", Item, Nam);
576 elsif not Analyzed (Cunit (Un))
577 and then Un /= Main_Unit
579 Opt.Style_Check := False;
580 Semantics (Cunit (Un));
588 Style_Check := Save_Style_Check;
589 Compilation_Unit_Restrictions_Restore (Save_C_Restrict);
593 -- Deal with creating elaboration Boolean if needed. We create an
594 -- elaboration boolean only for units that come from source since
595 -- units manufactured by the compiler never need elab checks.
597 if Comes_From_Source (N)
599 (Nkind (Unit (N)) = N_Package_Declaration or else
600 Nkind (Unit (N)) = N_Generic_Package_Declaration or else
601 Nkind (Unit (N)) = N_Subprogram_Declaration or else
602 Nkind (Unit (N)) = N_Generic_Subprogram_Declaration)
605 Loc : constant Source_Ptr := Sloc (N);
606 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
609 Spec_Id := Defining_Entity (Unit (N));
610 Generate_Definition (Spec_Id);
612 -- See if an elaboration entity is required for possible
613 -- access before elaboration checking. Note that we must
614 -- allow for this even if -gnatE is not set, since a client
615 -- may be compiled in -gnatE mode and reference the entity.
617 -- Case of units which do not require elaboration checks
620 -- Pure units do not need checks
624 -- Preelaborated units do not need checks
626 or else Is_Preelaborated (Spec_Id)
628 -- No checks needed if pagma Elaborate_Body present
630 or else Has_Pragma_Elaborate_Body (Spec_Id)
632 -- No checks needed if unit does not require a body
634 or else not Unit_Requires_Body (Spec_Id)
636 -- No checks needed for predefined files
638 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
640 -- No checks required if no separate spec
642 or else Acts_As_Spec (N)
644 -- This is a case where we only need the entity for
645 -- checking to prevent multiple elaboration checks.
647 Set_Elaboration_Entity_Required (Spec_Id, False);
649 -- Case of elaboration entity is required for access before
650 -- elaboration checking (so certainly we must build it!)
653 Set_Elaboration_Entity_Required (Spec_Id, True);
656 Build_Elaboration_Entity (N, Spec_Id);
660 -- Finally, freeze the compilation unit entity. This for sure is needed
661 -- because of some warnings that can be output (see Freeze_Subprogram),
662 -- but may in general be required. If freezing actions result, place
663 -- them in the compilation unit actions list, and analyze them.
666 Loc : constant Source_Ptr := Sloc (N);
667 L : constant List_Id :=
668 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), Loc);
671 while Is_Non_Empty_List (L) loop
672 Insert_Library_Level_Action (Remove_Head (L));
678 if Nkind (Unit_Node) = N_Package_Declaration
679 and then Get_Cunit_Unit_Number (N) /= Main_Unit
680 and then Front_End_Inlining
681 and then Expander_Active
683 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
685 end Analyze_Compilation_Unit;
687 ---------------------
688 -- Analyze_Context --
689 ---------------------
691 procedure Analyze_Context (N : Node_Id) is
695 -- Loop through context items
697 Item := First (Context_Items (N));
698 while Present (Item) loop
700 -- For with clause, analyze the with clause, and then update
701 -- the version, since we are dependent on a unit that we with.
703 if Nkind (Item) = N_With_Clause then
705 -- Skip analyzing with clause if no unit, nothing to do (this
706 -- happens for a with that references a non-existent unit)
708 if Present (Library_Unit (Item)) then
712 if not Implicit_With (Item) then
713 Version_Update (N, Library_Unit (Item));
716 -- But skip use clauses at this stage, since we don't want to do
717 -- any installing of potentially use visible entities until we
718 -- we actually install the complete context (in Install_Context).
719 -- Otherwise things can get installed in the wrong context.
720 -- Similarly, pragmas are analyzed in Install_Context, after all
721 -- the implicit with's on parent units are generated.
731 -------------------------------
732 -- Analyze_Package_Body_Stub --
733 -------------------------------
735 procedure Analyze_Package_Body_Stub (N : Node_Id) is
736 Id : constant Entity_Id := Defining_Identifier (N);
740 -- The package declaration must be in the current declarative part.
742 Check_Stub_Level (N);
743 Nam := Current_Entity_In_Scope (Id);
745 if No (Nam) or else not Is_Package (Nam) then
746 Error_Msg_N ("missing specification for package stub", N);
748 elsif Has_Completion (Nam)
749 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
751 Error_Msg_N ("duplicate or redundant stub for package", N);
754 -- Indicate that the body of the package exists. If we are doing
755 -- only semantic analysis, the stub stands for the body. If we are
756 -- generating code, the existence of the body will be confirmed
757 -- when we load the proper body.
759 Set_Has_Completion (Nam);
760 Set_Scope (Defining_Entity (N), Current_Scope);
761 Analyze_Proper_Body (N, Nam);
763 end Analyze_Package_Body_Stub;
765 -------------------------
766 -- Analyze_Proper_Body --
767 -------------------------
769 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
770 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
771 Unum : Unit_Number_Type;
772 Subunit_Not_Found : Boolean := False;
774 procedure Optional_Subunit;
775 -- This procedure is called when the main unit is a stub, or when we
776 -- are not generating code. In such a case, we analyze the subunit if
777 -- present, which is user-friendly and in fact required for ASIS, but
778 -- we don't complain if the subunit is missing.
780 ----------------------
781 -- Optional_Subunit --
782 ----------------------
784 procedure Optional_Subunit is
788 -- Try to load subunit, but ignore any errors that occur during
789 -- the loading of the subunit, by using the special feature in
790 -- Errout to ignore all errors. Note that Fatal_Error will still
791 -- be set, so we will be able to check for this case below.
793 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
796 (Load_Name => Subunit_Name,
800 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
802 -- All done if we successfully loaded the subunit
804 if Unum /= No_Unit and then not Fatal_Error (Unum) then
805 Comp_Unit := Cunit (Unum);
807 Set_Corresponding_Stub (Unit (Comp_Unit), N);
808 Analyze_Subunit (Comp_Unit);
809 Set_Library_Unit (N, Comp_Unit);
812 and then Present (Nam)
814 if Is_Protected_Type (Nam) then
815 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
817 Set_Corresponding_Body (
818 Unit_Declaration_Node (Nam), Defining_Identifier (N));
821 end Optional_Subunit;
823 -- Start of processing for Analyze_Proper_Body
826 -- If the subunit is already loaded, it means that the main unit
827 -- is a subunit, and that the current unit is one of its parents
828 -- which was being analyzed to provide the needed context for the
829 -- analysis of the subunit. In this case we analyze the subunit and
830 -- continue with the parent, without looking a subsequent subunits.
832 if Is_Loaded (Subunit_Name) then
834 -- If the proper body is already linked to the stub node,
835 -- the stub is in a generic unit and just needs analyzing.
837 if Present (Library_Unit (N)) then
838 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
839 Analyze_Subunit (Library_Unit (N));
841 -- Otherwise we must load the subunit and link to it
844 -- Load the subunit, this must work, since we originally
845 -- loaded the subunit earlier on. So this will not really
846 -- load it, just give access to it.
850 (Load_Name => Subunit_Name,
855 -- And analyze the subunit in the parent context (note that we
856 -- do not call Semantics, since that would remove the parent
857 -- context). Because of this, we have to manually reset the
858 -- compiler state to Analyzing since it got destroyed by Load.
860 if Unum /= No_Unit then
861 Compiler_State := Analyzing;
862 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
863 Analyze_Subunit (Cunit (Unum));
864 Set_Library_Unit (N, Cunit (Unum));
868 -- If the main unit is a subunit, then we are just performing semantic
869 -- analysis on that subunit, and any other subunits of any parent unit
870 -- should be ignored, except that if we are building trees for ASIS
871 -- usage we want to annotate the stub properly.
873 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
874 and then Subunit_Name /= Unit_Name (Main_Unit)
880 -- But before we return, set the flag for unloaded subunits. This
881 -- will suppress junk warnings of variables in the same declarative
882 -- part (or a higher level one) that are in danger of looking unused
883 -- when in fact there might be a declaration in the subunit that we
884 -- do not intend to load.
886 Unloaded_Subunits := True;
889 -- If the subunit is not already loaded, and we are generating code,
890 -- then this is the case where compilation started from the parent,
891 -- and we are generating code for an entire subunit tree. In that
892 -- case we definitely need to load the subunit.
894 -- In order to continue the analysis with the rest of the parent,
895 -- and other subunits, we load the unit without requiring its
896 -- presence, and emit a warning if not found, rather than terminating
897 -- the compilation abruptly, as for other missing file problems.
899 elsif Operating_Mode = Generate_Code then
901 -- If the proper body is already linked to the stub node,
902 -- the stub is in a generic unit and just needs analyzing.
904 -- We update the version. Although we are not technically
905 -- semantically dependent on the subunit, given our approach
906 -- of macro substitution of subunits, it makes sense to
907 -- include it in the version identification.
909 if Present (Library_Unit (N)) then
910 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
911 Analyze_Subunit (Library_Unit (N));
912 Version_Update (Cunit (Main_Unit), Library_Unit (N));
914 -- Otherwise we must load the subunit and link to it
919 (Load_Name => Subunit_Name,
924 if Operating_Mode = Generate_Code
925 and then Unum = No_Unit
927 Error_Msg_Name_1 := Subunit_Name;
929 Get_File_Name (Subunit_Name, Subunit => True);
931 ("subunit% in file{ not found!?", N);
932 Subunits_Missing := True;
933 Subunit_Not_Found := True;
936 -- Load_Unit may reset Compiler_State, since it may have been
937 -- necessary to parse an additional units, so we make sure
938 -- that we reset it to the Analyzing state.
940 Compiler_State := Analyzing;
942 if Unum /= No_Unit and then not Fatal_Error (Unum) then
945 Write_Str ("*** Loaded subunit from stub. Analyze");
950 Comp_Unit : constant Node_Id := Cunit (Unum);
953 -- Check for child unit instead of subunit
955 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
957 ("expected SEPARATE subunit, found child unit",
958 Cunit_Entity (Unum));
960 -- OK, we have a subunit, so go ahead and analyze it,
961 -- and set Scope of entity in stub, for ASIS use.
964 Set_Corresponding_Stub (Unit (Comp_Unit), N);
965 Analyze_Subunit (Comp_Unit);
966 Set_Library_Unit (N, Comp_Unit);
968 -- We update the version. Although we are not technically
969 -- semantically dependent on the subunit, given our
970 -- approach of macro substitution of subunits, it makes
971 -- sense to include it in the version identification.
973 Version_Update (Cunit (Main_Unit), Comp_Unit);
979 -- The remaining case is when the subunit is not already loaded and
980 -- we are not generating code. In this case we are just performing
981 -- semantic analysis on the parent, and we are not interested in
982 -- the subunit. For subprograms, analyze the stub as a body. For
983 -- other entities the stub has already been marked as completed.
989 end Analyze_Proper_Body;
991 ----------------------------------
992 -- Analyze_Protected_Body_Stub --
993 ----------------------------------
995 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
996 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
999 Check_Stub_Level (N);
1001 -- First occurrence of name may have been as an incomplete type.
1003 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1004 Nam := Full_View (Nam);
1008 or else not Is_Protected_Type (Etype (Nam))
1010 Error_Msg_N ("missing specification for Protected body", N);
1012 Set_Scope (Defining_Entity (N), Current_Scope);
1013 Set_Has_Completion (Etype (Nam));
1014 Analyze_Proper_Body (N, Etype (Nam));
1016 end Analyze_Protected_Body_Stub;
1018 ----------------------------------
1019 -- Analyze_Subprogram_Body_Stub --
1020 ----------------------------------
1022 -- A subprogram body stub can appear with or without a previous
1023 -- specification. If there is one, the analysis of the body will
1024 -- find it and verify conformance. The formals appearing in the
1025 -- specification of the stub play no role, except for requiring an
1026 -- additional conformance check. If there is no previous subprogram
1027 -- declaration, the stub acts as a spec, and provides the defining
1028 -- entity for the subprogram.
1030 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1034 Check_Stub_Level (N);
1036 -- Verify that the identifier for the stub is unique within this
1037 -- declarative part.
1039 if Nkind (Parent (N)) = N_Block_Statement
1040 or else Nkind (Parent (N)) = N_Package_Body
1041 or else Nkind (Parent (N)) = N_Subprogram_Body
1043 Decl := First (Declarations (Parent (N)));
1045 while Present (Decl)
1048 if Nkind (Decl) = N_Subprogram_Body_Stub
1049 and then (Chars (Defining_Unit_Name (Specification (Decl)))
1050 = Chars (Defining_Unit_Name (Specification (N))))
1052 Error_Msg_N ("identifier for stub is not unique", N);
1059 -- Treat stub as a body, which checks conformance if there is a previous
1060 -- declaration, or else introduces entity and its signature.
1062 Analyze_Subprogram_Body (N);
1064 if Errors_Detected = 0 then
1065 Analyze_Proper_Body (N, Empty);
1068 end Analyze_Subprogram_Body_Stub;
1070 ---------------------
1071 -- Analyze_Subunit --
1072 ---------------------
1074 -- A subunit is compiled either by itself (for semantic checking)
1075 -- or as part of compiling the parent (for code generation). In
1076 -- either case, by the time we actually process the subunit, the
1077 -- parent has already been installed and analyzed. The node N is
1078 -- a compilation unit, whose context needs to be treated here,
1079 -- because we come directly here from the parent without calling
1080 -- Analyze_Compilation_Unit.
1082 -- The compilation context includes the explicit context of the
1083 -- subunit, and the context of the parent, together with the parent
1084 -- itself. In order to compile the current context, we remove the
1085 -- one inherited from the parent, in order to have a clean visibility
1086 -- table. We restore the parent context before analyzing the proper
1087 -- body itself. On exit, we remove only the explicit context of the
1090 procedure Analyze_Subunit (N : Node_Id) is
1091 Lib_Unit : constant Node_Id := Library_Unit (N);
1092 Par_Unit : constant Entity_Id := Current_Scope;
1094 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1095 Num_Scopes : Int := 0;
1096 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1097 Enclosing_Child : Entity_Id := Empty;
1099 procedure Analyze_Subunit_Context;
1100 -- Capture names in use clauses of the subunit. This must be done
1101 -- before re-installing parent declarations, because items in the
1102 -- context must not be hidden by declarations local to the parent.
1104 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1105 -- Recursive procedure to restore scope of all ancestors of subunit,
1106 -- from outermost in. If parent is not a subunit, the call to install
1107 -- context installs context of spec and (if parent is a child unit)
1108 -- the context of its parents as well. It is confusing that parents
1109 -- should be treated differently in both cases, but the semantics are
1110 -- just not identical.
1112 procedure Re_Install_Use_Clauses;
1113 -- As part of the removal of the parent scope, the use clauses are
1114 -- removed, to be reinstalled when the context of the subunit has
1115 -- been analyzed. Use clauses may also have been affected by the
1116 -- analysis of the context of the subunit, so they have to be applied
1117 -- again, to insure that the compilation environment of the rest of
1118 -- the parent unit is identical.
1120 procedure Remove_Scope;
1121 -- Remove current scope from scope stack, and preserve the list
1122 -- of use clauses in it, to be reinstalled after context is analyzed.
1124 ------------------------------
1125 -- Analyze_Subunit_Context --
1126 ------------------------------
1128 procedure Analyze_Subunit_Context is
1131 Unit_Name : Entity_Id;
1134 Analyze_Context (N);
1135 Item := First (Context_Items (N));
1137 -- make withed units immediately visible. If child unit, make the
1138 -- ultimate parent immediately visible.
1140 while Present (Item) loop
1142 if Nkind (Item) = N_With_Clause then
1143 Unit_Name := Entity (Name (Item));
1145 while Is_Child_Unit (Unit_Name) loop
1146 Set_Is_Visible_Child_Unit (Unit_Name);
1147 Unit_Name := Scope (Unit_Name);
1150 if not Is_Immediately_Visible (Unit_Name) then
1151 Set_Is_Immediately_Visible (Unit_Name);
1152 Set_Context_Installed (Item);
1155 elsif Nkind (Item) = N_Use_Package_Clause then
1156 Nam := First (Names (Item));
1158 while Present (Nam) loop
1163 elsif Nkind (Item) = N_Use_Type_Clause then
1164 Nam := First (Subtype_Marks (Item));
1166 while Present (Nam) loop
1175 Item := First (Context_Items (N));
1177 -- reset visibility of withed units. They will be made visible
1178 -- again when we install the subunit context.
1180 while Present (Item) loop
1182 if Nkind (Item) = N_With_Clause then
1183 Unit_Name := Entity (Name (Item));
1185 while Is_Child_Unit (Unit_Name) loop
1186 Set_Is_Visible_Child_Unit (Unit_Name, False);
1187 Unit_Name := Scope (Unit_Name);
1190 if Context_Installed (Item) then
1191 Set_Is_Immediately_Visible (Unit_Name, False);
1192 Set_Context_Installed (Item, False);
1199 end Analyze_Subunit_Context;
1201 ------------------------
1202 -- Re_Install_Parents --
1203 ------------------------
1205 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
1209 if Nkind (Unit (L)) = N_Subunit then
1210 Re_Install_Parents (Library_Unit (L), Scope (Scop));
1213 Install_Context (L);
1215 -- If the subunit occurs within a child unit, we must restore the
1216 -- immediate visibility of any siblings that may occur in context.
1218 if Present (Enclosing_Child) then
1219 Install_Siblings (Enclosing_Child, L);
1224 if Scop /= Par_Unit then
1225 Set_Is_Immediately_Visible (Scop);
1228 E := First_Entity (Current_Scope);
1230 while Present (E) loop
1231 Set_Is_Immediately_Visible (E);
1235 -- A subunit appears within a body, and for a nested subunits
1236 -- all the parents are bodies. Restore full visibility of their
1237 -- private entities.
1239 if Ekind (Scop) = E_Package then
1240 Set_In_Package_Body (Scop);
1241 Install_Private_Declarations (Scop);
1243 end Re_Install_Parents;
1245 ----------------------------
1246 -- Re_Install_Use_Clauses --
1247 ----------------------------
1249 procedure Re_Install_Use_Clauses is
1253 for J in reverse 1 .. Num_Scopes loop
1254 U := Use_Clauses (J);
1255 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
1256 Install_Use_Clauses (U);
1258 end Re_Install_Use_Clauses;
1264 procedure Remove_Scope is
1268 Num_Scopes := Num_Scopes + 1;
1269 Use_Clauses (Num_Scopes) :=
1270 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
1271 E := First_Entity (Current_Scope);
1273 while Present (E) loop
1274 Set_Is_Immediately_Visible (E, False);
1278 if Is_Child_Unit (Current_Scope) then
1279 Enclosing_Child := Current_Scope;
1285 -- Start of processing for Analyze_Subunit
1288 if not Is_Empty_List (Context_Items (N)) then
1290 -- Save current use clauses.
1293 Remove_Context (Lib_Unit);
1295 -- Now remove parents and their context, including enclosing
1296 -- subunits and the outer parent body which is not a subunit.
1298 if Present (Lib_Spec) then
1299 Remove_Context (Lib_Spec);
1301 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
1302 Lib_Spec := Library_Unit (Lib_Spec);
1304 Remove_Context (Lib_Spec);
1307 if Nkind (Unit (Lib_Unit)) = N_Subunit then
1311 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
1312 Remove_Context (Library_Unit (Lib_Spec));
1316 Analyze_Subunit_Context;
1317 Re_Install_Parents (Lib_Unit, Par_Unit);
1319 -- If the context includes a child unit of the parent of the
1320 -- subunit, the parent will have been removed from visibility,
1321 -- after compiling that cousin in the context. The visibility
1322 -- of the parent must be restored now. This also applies if the
1323 -- context includes another subunit of the same parent which in
1324 -- turn includes a child unit in its context.
1326 if Ekind (Par_Unit) = E_Package then
1327 if not Is_Immediately_Visible (Par_Unit)
1328 or else (Present (First_Entity (Par_Unit))
1329 and then not Is_Immediately_Visible
1330 (First_Entity (Par_Unit)))
1332 Set_Is_Immediately_Visible (Par_Unit);
1333 Install_Visible_Declarations (Par_Unit);
1334 Install_Private_Declarations (Par_Unit);
1338 Re_Install_Use_Clauses;
1339 Install_Context (N);
1341 -- If the subunit is within a child unit, then siblings of any
1342 -- parent unit that appear in the context clause of the subunit
1343 -- must also be made immediately visible.
1345 if Present (Enclosing_Child) then
1346 Install_Siblings (Enclosing_Child, N);
1351 Analyze (Proper_Body (Unit (N)));
1354 end Analyze_Subunit;
1356 ----------------------------
1357 -- Analyze_Task_Body_Stub --
1358 ----------------------------
1360 procedure Analyze_Task_Body_Stub (N : Node_Id) is
1361 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1362 Loc : constant Source_Ptr := Sloc (N);
1365 Check_Stub_Level (N);
1367 -- First occurrence of name may have been as an incomplete type.
1369 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1370 Nam := Full_View (Nam);
1374 or else not Is_Task_Type (Etype (Nam))
1376 Error_Msg_N ("missing specification for task body", N);
1378 Set_Scope (Defining_Entity (N), Current_Scope);
1379 Set_Has_Completion (Etype (Nam));
1380 Analyze_Proper_Body (N, Etype (Nam));
1382 -- Set elaboration flag to indicate that entity is callable.
1383 -- This cannot be done in the expansion of the body itself,
1384 -- because the proper body is not in a declarative part. This
1385 -- is only done if expansion is active, because the context
1386 -- may be generic and the flag not defined yet.
1388 if Expander_Active then
1390 Make_Assignment_Statement (Loc,
1392 Make_Identifier (Loc,
1393 New_External_Name (Chars (Etype (Nam)), 'E')),
1394 Expression => New_Reference_To (Standard_True, Loc)));
1398 end Analyze_Task_Body_Stub;
1400 -------------------------
1401 -- Analyze_With_Clause --
1402 -------------------------
1404 -- Analyze the declaration of a unit in a with clause. At end,
1405 -- label the with clause with the defining entity for the unit.
1407 procedure Analyze_With_Clause (N : Node_Id) is
1408 Unit_Kind : constant Node_Kind := Nkind (Unit (Library_Unit (N)));
1410 Par_Name : Entity_Id;
1415 -- Set True if the unit currently being compiled is an internal unit
1417 Save_Style_Check : constant Boolean := Opt.Style_Check;
1418 Save_C_Restrict : constant Save_Compilation_Unit_Restrictions :=
1419 Compilation_Unit_Restrictions_Save;
1422 -- We reset ordinary style checking during the analysis of a with'ed
1423 -- unit, but we do NOT reset GNAT special analysis mode (the latter
1424 -- definitely *does* apply to with'ed units).
1426 if not GNAT_Mode then
1427 Style_Check := False;
1430 -- If the library unit is a predefined unit, and we are in no
1431 -- run time mode, then temporarily reset No_Run_Time mode for the
1432 -- analysis of the with'ed unit. The No_Run_Time pragma does not
1433 -- prevent explicit with'ing of run-time units.
1437 Is_Predefined_File_Name
1438 (Unit_File_Name (Get_Source_Unit (Unit (Library_Unit (N)))))
1440 No_Run_Time := False;
1441 Semantics (Library_Unit (N));
1442 No_Run_Time := True;
1445 Semantics (Library_Unit (N));
1448 U := Unit (Library_Unit (N));
1449 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
1451 -- Following checks are skipped for dummy packages (those supplied
1452 -- for with's where no matching file could be found). Such packages
1453 -- are identified by the Sloc value being set to No_Location
1455 if Sloc (U) /= No_Location then
1457 -- Check restrictions, except that we skip the check if this
1458 -- is an internal unit unless we are compiling the internal
1459 -- unit as the main unit. We also skip this for dummy packages.
1461 if not Intunit or else Current_Sem_Unit = Main_Unit then
1462 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
1465 -- Check for inappropriate with of internal implementation unit
1466 -- if we are currently compiling the main unit and the main unit
1467 -- is itself not an internal unit.
1469 if Implementation_Unit_Warnings
1470 and then Current_Sem_Unit = Main_Unit
1471 and then Implementation_Unit (Get_Source_Unit (U))
1472 and then not Intunit
1474 Error_Msg_N ("& is an internal 'G'N'A'T unit?", Name (N));
1476 ("\use of this unit is non-portable and version-dependent?",
1481 -- Semantic analysis of a generic unit is performed on a copy of
1482 -- the original tree. Retrieve the entity on which semantic info
1483 -- actually appears.
1485 if Unit_Kind in N_Generic_Declaration then
1486 E_Name := Defining_Entity (U);
1488 -- Note: in the following test, Unit_Kind is the original Nkind, but
1489 -- in the case of an instantiation, the call to Semantics above will
1490 -- have replaced the unit by its instantiated version.
1492 elsif Unit_Kind = N_Package_Instantiation
1493 and then Nkind (U) = N_Package_Body
1495 -- Instantiation node is replaced with body of instance.
1496 -- Unit name is defining unit name in corresponding spec.
1498 E_Name := Corresponding_Spec (U);
1500 elsif Unit_Kind = N_Package_Instantiation
1501 and then Nkind (U) = N_Package_Instantiation
1503 -- If the instance has not been rewritten as a package declaration,
1504 -- then it appeared already in a previous with clause. Retrieve
1505 -- the entity from the previous instance.
1507 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
1509 elsif Unit_Kind = N_Procedure_Instantiation
1510 or else Unit_Kind = N_Function_Instantiation
1512 -- Instantiation node is replaced with a package that contains
1513 -- renaming declarations and instance itself. The subprogram
1514 -- Instance is declared in the visible part of the wrapper package.
1516 E_Name := First_Entity (Defining_Entity (U));
1518 while Present (E_Name) loop
1519 exit when Is_Subprogram (E_Name)
1520 and then Is_Generic_Instance (E_Name);
1521 E_Name := Next_Entity (E_Name);
1524 elsif Unit_Kind = N_Package_Renaming_Declaration
1525 or else Unit_Kind in N_Generic_Renaming_Declaration
1527 E_Name := Defining_Entity (U);
1529 elsif Unit_Kind = N_Subprogram_Body
1530 and then Nkind (Name (N)) = N_Selected_Component
1531 and then not Acts_As_Spec (Library_Unit (N))
1533 -- For a child unit that has no spec, one has been created and
1534 -- analyzed. The entity required is that of the spec.
1536 E_Name := Corresponding_Spec (U);
1539 E_Name := Defining_Entity (U);
1542 if Nkind (Name (N)) = N_Selected_Component then
1544 -- Child unit in a with clause
1546 Change_Selected_Component_To_Expanded_Name (Name (N));
1549 -- Restore style checks and restrictions
1551 Style_Check := Save_Style_Check;
1552 Compilation_Unit_Restrictions_Restore (Save_C_Restrict);
1554 -- Record the reference, but do NOT set the unit as referenced, we
1555 -- want to consider the unit as unreferenced if this is the only
1556 -- reference that occurs.
1558 Set_Entity_With_Style_Check (Name (N), E_Name);
1559 Generate_Reference (E_Name, Name (N), Set_Ref => False);
1561 if Is_Child_Unit (E_Name) then
1562 Pref := Prefix (Name (N));
1563 Par_Name := Scope (E_Name);
1565 while Nkind (Pref) = N_Selected_Component loop
1566 Change_Selected_Component_To_Expanded_Name (Pref);
1567 Set_Entity_With_Style_Check (Pref, Par_Name);
1569 Generate_Reference (Par_Name, Pref);
1570 Pref := Prefix (Pref);
1571 Par_Name := Scope (Par_Name);
1574 if Present (Entity (Pref))
1575 and then not Analyzed (Parent (Parent (Entity (Pref))))
1577 -- If the entity is set without its unit being compiled,
1578 -- the original parent is a renaming, and Par_Name is the
1579 -- renamed entity. For visibility purposes, we need the
1580 -- original entity, which must be analyzed now, because
1581 -- Load_Unit retrieves directly the renamed unit, and the
1582 -- renaming declaration itself has not been analyzed.
1584 Analyze (Parent (Parent (Entity (Pref))));
1585 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
1586 Par_Name := Entity (Pref);
1589 Set_Entity_With_Style_Check (Pref, Par_Name);
1590 Generate_Reference (Par_Name, Pref);
1593 -- If the withed unit is System, and a system extension pragma is
1594 -- present, compile the extension now, rather than waiting for
1595 -- a visibility check on a specific entity.
1597 if Chars (E_Name) = Name_System
1598 and then Scope (E_Name) = Standard_Standard
1599 and then Present (System_Extend_Pragma_Arg)
1600 and then Present_System_Aux (N)
1602 -- If the extension is not present, an error will have been emitted.
1606 end Analyze_With_Clause;
1608 ------------------------------
1609 -- Analyze_With_Type_Clause --
1610 ------------------------------
1612 procedure Analyze_With_Type_Clause (N : Node_Id) is
1613 Loc : constant Source_Ptr := Sloc (N);
1614 Nam : Node_Id := Name (N);
1618 Unum : Unit_Number_Type;
1621 procedure Decorate_Tagged_Type (T : Entity_Id; Kind : Entity_Kind);
1622 -- Set basic attributes of type, including its class_wide type.
1624 function In_Chain (E : Entity_Id) return Boolean;
1625 -- Check that the imported type is not already in the homonym chain,
1626 -- for example through a with_type clause in a parent unit.
1628 --------------------------
1629 -- Decorate_Tagged_Type --
1630 --------------------------
1632 procedure Decorate_Tagged_Type (T : Entity_Id; Kind : Entity_Kind) is
1636 Set_Ekind (T, E_Record_Type);
1637 Set_Is_Tagged_Type (T);
1639 Set_From_With_Type (T);
1642 if not In_Chain (T) then
1643 Set_Homonym (T, Current_Entity (T));
1644 Set_Current_Entity (T);
1647 -- Build bogus class_wide type, if not previously done.
1649 if No (Class_Wide_Type (T)) then
1650 CW := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
1652 Set_Ekind (CW, E_Class_Wide_Type);
1655 Set_Is_Tagged_Type (CW);
1656 Set_Is_First_Subtype (CW, True);
1657 Init_Size_Align (CW);
1658 Set_Has_Unknown_Discriminants
1660 Set_Class_Wide_Type (CW, CW);
1661 Set_Equivalent_Type (CW, Empty);
1662 Set_From_With_Type (CW);
1664 Set_Class_Wide_Type (T, CW);
1666 end Decorate_Tagged_Type;
1672 function In_Chain (E : Entity_Id) return Boolean is
1673 H : Entity_Id := Current_Entity (E);
1676 while Present (H) loop
1688 -- Start of processing for Analyze_With_Type_Clause
1691 if Nkind (Nam) = N_Selected_Component then
1692 Pack := New_Copy_Tree (Prefix (Nam));
1693 Sel := Selector_Name (Nam);
1696 Error_Msg_N ("illegal name for imported type", Nam);
1701 Make_Package_Declaration (Loc,
1703 (Make_Package_Specification (Loc,
1704 Defining_Unit_Name => Pack,
1705 Visible_Declarations => New_List,
1706 End_Label => Empty)));
1710 (Load_Name => Get_Unit_Name (Decl),
1716 or else Nkind (Unit (Cunit (Unum))) /= N_Package_Declaration
1718 Error_Msg_N ("imported type must be declared in package", Nam);
1721 elsif Unum = Current_Sem_Unit then
1723 -- If type is defined in unit being analyzed, then the clause
1729 P := Cunit_Entity (Unum);
1732 -- Find declaration for imported type, and set its basic attributes
1733 -- if it has not been analyzed (which will be the case if there is
1734 -- circular dependence).
1741 if not Analyzed (Cunit (Unum))
1742 and then not From_With_Type (P)
1744 Set_Ekind (P, E_Package);
1745 Set_Etype (P, Standard_Void_Type);
1746 Set_From_With_Type (P);
1747 Set_Scope (P, Standard_Standard);
1748 Set_Homonym (P, Current_Entity (P));
1749 Set_Current_Entity (P);
1751 elsif Analyzed (Cunit (Unum))
1752 and then Is_Child_Unit (P)
1754 -- If the child unit is already in scope, indicate that it is
1755 -- visible, and remains so after intervening calls to rtsfind.
1757 Set_Is_Visible_Child_Unit (P);
1760 if Nkind (Parent (P)) = N_Defining_Program_Unit_Name then
1762 -- Make parent packages visible.
1765 Parent_Comp : Node_Id;
1766 Parent_Id : Entity_Id;
1771 Parent_Comp := Parent_Spec (Unit (Cunit (Unum)));
1774 Parent_Id := Defining_Entity (Unit (Parent_Comp));
1775 Set_Scope (Child, Parent_Id);
1777 -- The type may be imported from a child unit, in which
1778 -- case the current compilation appears in the name. Do
1779 -- not change its visibility here because it will conflict
1780 -- with the subsequent normal processing.
1782 if not Analyzed (Unit_Declaration_Node (Parent_Id))
1783 and then Parent_Id /= Cunit_Entity (Current_Sem_Unit)
1785 Set_Ekind (Parent_Id, E_Package);
1786 Set_Etype (Parent_Id, Standard_Void_Type);
1788 -- The same package may appear is several with_type
1791 if not From_With_Type (Parent_Id) then
1792 Set_Homonym (Parent_Id, Current_Entity (Parent_Id));
1793 Set_Current_Entity (Parent_Id);
1794 Set_From_With_Type (Parent_Id);
1798 Set_Is_Immediately_Visible (Parent_Id);
1801 Parent_Comp := Parent_Spec (Unit (Parent_Comp));
1802 exit when No (Parent_Comp);
1805 Set_Scope (Parent_Id, Standard_Standard);
1809 -- Even if analyzed, the package may not be currently visible. It
1810 -- must be while the with_type clause is active.
1812 Set_Is_Immediately_Visible (P);
1815 First (Visible_Declarations (Specification (Unit (Cunit (Unum)))));
1817 while Present (Decl) loop
1819 if Nkind (Decl) = N_Full_Type_Declaration
1820 and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
1822 Typ := Defining_Identifier (Decl);
1824 if Tagged_Present (N) then
1826 -- The declaration must indicate that this is a tagged
1827 -- type or a type extension.
1829 if (Nkind (Type_Definition (Decl)) = N_Record_Definition
1830 and then Tagged_Present (Type_Definition (Decl)))
1832 (Nkind (Type_Definition (Decl))
1833 = N_Derived_Type_Definition
1835 (Record_Extension_Part (Type_Definition (Decl))))
1839 Error_Msg_N ("imported type is not a tagged type", Nam);
1843 if not Analyzed (Decl) then
1845 -- Unit is not currently visible. Add basic attributes
1846 -- to type and build its class-wide type.
1848 Init_Size_Align (Typ);
1849 Decorate_Tagged_Type (Typ, E_Record_Type);
1853 if Nkind (Type_Definition (Decl))
1854 /= N_Access_To_Object_Definition
1857 ("imported type is not an access type", Nam);
1859 elsif not Analyzed (Decl) then
1860 Set_Ekind (Typ, E_Access_Type);
1861 Set_Etype (Typ, Typ);
1863 Init_Size (Typ, System_Address_Size);
1864 Init_Alignment (Typ);
1865 Set_Directly_Designated_Type (Typ, Standard_Integer);
1866 Set_From_With_Type (Typ);
1868 if not In_Chain (Typ) then
1869 Set_Homonym (Typ, Current_Entity (Typ));
1870 Set_Current_Entity (Typ);
1875 Set_Entity (Sel, Typ);
1878 elsif ((Nkind (Decl) = N_Private_Type_Declaration
1879 and then Tagged_Present (Decl))
1880 or else (Nkind (Decl) = N_Private_Extension_Declaration))
1881 and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
1883 Typ := Defining_Identifier (Decl);
1885 if not Tagged_Present (N) then
1886 Error_Msg_N ("type must be declared tagged", N);
1888 elsif not Analyzed (Decl) then
1889 Decorate_Tagged_Type (Typ, E_Private_Type);
1892 Set_Entity (Sel, Typ);
1893 Set_From_With_Type (Typ);
1897 Decl := Next (Decl);
1900 Error_Msg_NE ("not a visible access or tagged type in&", Nam, P);
1902 end Analyze_With_Type_Clause;
1904 -----------------------------
1905 -- Check_With_Type_Clauses --
1906 -----------------------------
1908 procedure Check_With_Type_Clauses (N : Node_Id) is
1909 Lib_Unit : constant Node_Id := Unit (N);
1911 procedure Check_Parent_Context (U : Node_Id);
1912 -- Examine context items of parent unit to locate with_type clauses.
1914 --------------------------
1915 -- Check_Parent_Context --
1916 --------------------------
1918 procedure Check_Parent_Context (U : Node_Id) is
1922 Item := First (Context_Items (U));
1923 while Present (Item) loop
1924 if Nkind (Item) = N_With_Type_Clause
1925 and then not Error_Posted (Item)
1927 From_With_Type (Scope (Entity (Selector_Name (Name (Item)))))
1929 Error_Msg_Sloc := Sloc (Item);
1930 Error_Msg_N ("Missing With_Clause for With_Type_Clause#", N);
1935 end Check_Parent_Context;
1937 -- Start of processing for Check_With_Type_Clauses
1940 if Extensions_Allowed
1941 and then (Nkind (Lib_Unit) = N_Package_Body
1942 or else Nkind (Lib_Unit) = N_Subprogram_Body)
1944 Check_Parent_Context (Library_Unit (N));
1945 if Is_Child_Spec (Unit (Library_Unit (N))) then
1946 Check_Parent_Context (Parent_Spec (Unit (Library_Unit (N))));
1949 end Check_With_Type_Clauses;
1951 ------------------------------
1952 -- Check_Private_Child_Unit --
1953 ------------------------------
1955 procedure Check_Private_Child_Unit (N : Node_Id) is
1956 Lib_Unit : constant Node_Id := Unit (N);
1958 Curr_Unit : Entity_Id;
1959 Sub_Parent : Node_Id;
1960 Priv_Child : Entity_Id;
1961 Par_Lib : Entity_Id;
1964 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
1965 -- Returns true if and only if the library unit is declared with
1966 -- an explicit designation of private.
1968 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
1970 return Private_Present (Parent (Unit_Declaration_Node (Unit)));
1971 end Is_Private_Library_Unit;
1973 -- Start of processing for Check_Private_Child_Unit
1976 if Nkind (Lib_Unit) = N_Package_Body
1977 or else Nkind (Lib_Unit) = N_Subprogram_Body
1979 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
1980 Par_Lib := Curr_Unit;
1982 elsif Nkind (Lib_Unit) = N_Subunit then
1984 -- The parent is itself a body. The parent entity is to be found
1985 -- in the corresponding spec.
1987 Sub_Parent := Library_Unit (N);
1988 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
1990 -- If the parent itself is a subunit, Curr_Unit is the entity
1991 -- of the enclosing body, retrieve the spec entity which is
1992 -- the proper ancestor we need for the following tests.
1994 if Ekind (Curr_Unit) = E_Package_Body then
1995 Curr_Unit := Spec_Entity (Curr_Unit);
1998 Par_Lib := Curr_Unit;
2001 Curr_Unit := Defining_Entity (Lib_Unit);
2003 Par_Lib := Curr_Unit;
2004 Par_Spec := Parent_Spec (Lib_Unit);
2006 if No (Par_Spec) then
2009 Par_Lib := Defining_Entity (Unit (Par_Spec));
2013 -- Loop through context items
2015 Item := First (Context_Items (N));
2016 while Present (Item) loop
2018 if Nkind (Item) = N_With_Clause
2019 and then not Implicit_With (Item)
2020 and then Is_Private_Descendant (Entity (Name (Item)))
2022 Priv_Child := Entity (Name (Item));
2025 Curr_Parent : Entity_Id := Par_Lib;
2026 Child_Parent : Entity_Id := Scope (Priv_Child);
2027 Prv_Ancestor : Entity_Id := Child_Parent;
2028 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2031 -- If the child unit is a public child then locate
2032 -- the nearest private ancestor; Child_Parent will
2033 -- then be set to the parent of that ancestor.
2035 if not Is_Private_Library_Unit (Priv_Child) then
2036 while Present (Prv_Ancestor)
2037 and then not Is_Private_Library_Unit (Prv_Ancestor)
2039 Prv_Ancestor := Scope (Prv_Ancestor);
2042 if Present (Prv_Ancestor) then
2043 Child_Parent := Scope (Prv_Ancestor);
2047 while Present (Curr_Parent)
2048 and then Curr_Parent /= Standard_Standard
2049 and then Curr_Parent /= Child_Parent
2052 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2053 Curr_Parent := Scope (Curr_Parent);
2056 if not Present (Curr_Parent) then
2057 Curr_Parent := Standard_Standard;
2060 if Curr_Parent /= Child_Parent then
2062 if Ekind (Priv_Child) = E_Generic_Package
2063 and then Chars (Priv_Child) in Text_IO_Package_Name
2064 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2067 ("& is a nested package, not a compilation unit",
2068 Name (Item), Priv_Child);
2072 ("unit in with clause is private child unit!", Item);
2074 ("current unit must also have parent&!",
2075 Item, Child_Parent);
2078 elsif not Curr_Private
2079 and then Nkind (Lib_Unit) /= N_Package_Body
2080 and then Nkind (Lib_Unit) /= N_Subprogram_Body
2081 and then Nkind (Lib_Unit) /= N_Subunit
2084 ("current unit must also be private descendant of&",
2085 Item, Child_Parent);
2093 end Check_Private_Child_Unit;
2095 ----------------------
2096 -- Check_Stub_Level --
2097 ----------------------
2099 procedure Check_Stub_Level (N : Node_Id) is
2100 Par : constant Node_Id := Parent (N);
2101 Kind : constant Node_Kind := Nkind (Par);
2104 if (Kind = N_Package_Body
2105 or else Kind = N_Subprogram_Body
2106 or else Kind = N_Task_Body
2107 or else Kind = N_Protected_Body)
2109 and then (Nkind (Parent (Par)) = N_Compilation_Unit
2110 or else Nkind (Parent (Par)) = N_Subunit)
2114 -- In an instance, a missing stub appears at any level. A warning
2115 -- message will have been emitted already for the missing file.
2117 elsif not In_Instance then
2118 Error_Msg_N ("stub cannot appear in an inner scope", N);
2120 elsif Expander_Active then
2121 Error_Msg_N ("missing proper body", N);
2123 end Check_Stub_Level;
2125 ------------------------
2126 -- Expand_With_Clause --
2127 ------------------------
2129 procedure Expand_With_Clause (Nam : Node_Id; N : Node_Id) is
2130 Loc : constant Source_Ptr := Sloc (Nam);
2131 Ent : constant Entity_Id := Entity (Nam);
2135 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2137 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2141 if Nkind (Nam) = N_Identifier then
2142 return New_Occurrence_Of (Entity (Nam), Loc);
2146 Make_Expanded_Name (Loc,
2147 Chars => Chars (Entity (Nam)),
2148 Prefix => Build_Unit_Name (Prefix (Nam)),
2149 Selector_Name => New_Occurrence_Of (Entity (Nam), Loc));
2150 Set_Entity (Result, Entity (Nam));
2153 end Build_Unit_Name;
2156 New_Nodes_OK := New_Nodes_OK + 1;
2158 Make_With_Clause (Loc, Name => Build_Unit_Name (Nam));
2160 P := Parent (Unit_Declaration_Node (Ent));
2161 Set_Library_Unit (Withn, P);
2162 Set_Corresponding_Spec (Withn, Ent);
2163 Set_First_Name (Withn, True);
2164 Set_Implicit_With (Withn, True);
2166 Prepend (Withn, Context_Items (N));
2167 Mark_Rewrite_Insertion (Withn);
2168 Install_Withed_Unit (Withn);
2170 if Nkind (Nam) = N_Expanded_Name then
2171 Expand_With_Clause (Prefix (Nam), N);
2174 New_Nodes_OK := New_Nodes_OK - 1;
2175 end Expand_With_Clause;
2177 -----------------------------
2178 -- Implicit_With_On_Parent --
2179 -----------------------------
2181 procedure Implicit_With_On_Parent
2182 (Child_Unit : Node_Id;
2185 Loc : constant Source_Ptr := Sloc (N);
2186 P : constant Node_Id := Parent_Spec (Child_Unit);
2187 P_Unit : constant Node_Id := Unit (P);
2189 P_Name : Entity_Id := Defining_Entity (P_Unit);
2192 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2193 -- Build prefix of child unit name. Recurse if needed.
2195 function Build_Unit_Name return Node_Id;
2196 -- If the unit is a child unit, build qualified name with all
2199 -------------------------
2200 -- Build_Ancestor_Name --
2201 -------------------------
2203 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
2204 P_Ref : Node_Id := New_Reference_To (Defining_Entity (P), Loc);
2207 if No (Parent_Spec (P)) then
2211 Make_Selected_Component (Loc,
2212 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P))),
2213 Selector_Name => P_Ref);
2215 end Build_Ancestor_Name;
2217 ---------------------
2218 -- Build_Unit_Name --
2219 ---------------------
2221 function Build_Unit_Name return Node_Id is
2225 if No (Parent_Spec (P_Unit)) then
2226 return New_Reference_To (P_Name, Loc);
2229 Make_Expanded_Name (Loc,
2230 Chars => Chars (P_Name),
2231 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
2232 Selector_Name => New_Reference_To (P_Name, Loc));
2233 Set_Entity (Result, P_Name);
2236 end Build_Unit_Name;
2238 -- Start of processing for Implicit_With_On_Parent
2241 New_Nodes_OK := New_Nodes_OK + 1;
2242 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
2244 Set_Library_Unit (Withn, P);
2245 Set_Corresponding_Spec (Withn, P_Name);
2246 Set_First_Name (Withn, True);
2247 Set_Implicit_With (Withn, True);
2249 -- Node is placed at the beginning of the context items, so that
2250 -- subsequent use clauses on the parent can be validated.
2252 Prepend (Withn, Context_Items (N));
2253 Mark_Rewrite_Insertion (Withn);
2254 Install_Withed_Unit (Withn);
2256 if Is_Child_Spec (P_Unit) then
2257 Implicit_With_On_Parent (P_Unit, N);
2259 New_Nodes_OK := New_Nodes_OK - 1;
2260 end Implicit_With_On_Parent;
2262 ---------------------
2263 -- Install_Context --
2264 ---------------------
2266 procedure Install_Context (N : Node_Id) is
2267 Lib_Unit : Node_Id := Unit (N);
2270 Install_Context_Clauses (N);
2272 if Is_Child_Spec (Lib_Unit) then
2273 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
2276 Check_With_Type_Clauses (N);
2277 end Install_Context;
2279 -----------------------------
2280 -- Install_Context_Clauses --
2281 -----------------------------
2283 procedure Install_Context_Clauses (N : Node_Id) is
2284 Lib_Unit : Node_Id := Unit (N);
2286 Uname_Node : Entity_Id;
2287 Check_Private : Boolean := False;
2288 Decl_Node : Node_Id;
2289 Lib_Parent : Entity_Id;
2292 -- Loop through context clauses to find the with/use clauses
2294 Item := First (Context_Items (N));
2295 while Present (Item) loop
2297 -- Case of explicit WITH clause
2299 if Nkind (Item) = N_With_Clause
2300 and then not Implicit_With (Item)
2302 -- If Name (Item) is not an entity name, something is wrong, and
2303 -- this will be detected in due course, for now ignore the item
2305 if not Is_Entity_Name (Name (Item)) then
2309 Uname_Node := Entity (Name (Item));
2311 if Is_Private_Descendant (Uname_Node) then
2312 Check_Private := True;
2315 Install_Withed_Unit (Item);
2317 Decl_Node := Unit_Declaration_Node (Uname_Node);
2319 -- If the unit is a subprogram instance, it appears nested
2320 -- within a package that carries the parent information.
2322 if Is_Generic_Instance (Uname_Node)
2323 and then Ekind (Uname_Node) /= E_Package
2325 Decl_Node := Parent (Parent (Decl_Node));
2328 if Is_Child_Spec (Decl_Node) then
2329 if Nkind (Name (Item)) = N_Expanded_Name then
2330 Expand_With_Clause (Prefix (Name (Item)), N);
2332 -- if not an expanded name, the child unit must be a
2333 -- renaming, nothing to do.
2338 elsif Nkind (Decl_Node) = N_Subprogram_Body
2339 and then not Acts_As_Spec (Parent (Decl_Node))
2340 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
2342 Implicit_With_On_Parent
2343 (Unit (Library_Unit (Parent (Decl_Node))), N);
2346 -- Check license conditions unless this is a dummy unit
2348 if Sloc (Library_Unit (Item)) /= No_Location then
2349 License_Check : declare
2350 Withl : constant License_Type :=
2351 License (Source_Index
2353 (Library_Unit (Item))));
2355 Unitl : constant License_Type :=
2356 License (Source_Index (Current_Sem_Unit));
2358 procedure License_Error;
2359 -- Signal error of bad license
2365 procedure License_Error is
2368 ("?license of with'ed unit & is incompatible",
2372 -- Start of processing for License_Check
2385 if Withl = Restricted then
2389 when Modified_GPL =>
2390 if Withl = Restricted or else Withl = GPL then
2394 when Unrestricted =>
2400 -- Case of USE PACKAGE clause
2402 elsif Nkind (Item) = N_Use_Package_Clause then
2403 Analyze_Use_Package (Item);
2405 -- Case of USE TYPE clause
2407 elsif Nkind (Item) = N_Use_Type_Clause then
2408 Analyze_Use_Type (Item);
2410 -- Case of WITH TYPE clause
2412 -- A With_Type_Clause is processed when installing the context,
2413 -- because it is a visibility mechanism and does not create a
2414 -- semantic dependence on other units, as a With_Clause does.
2416 elsif Nkind (Item) = N_With_Type_Clause then
2417 Analyze_With_Type_Clause (Item);
2421 elsif Nkind (Item) = N_Pragma then
2429 if Is_Child_Spec (Lib_Unit) then
2431 -- The unit also has implicit withs on its own parents.
2433 if No (Context_Items (N)) then
2434 Set_Context_Items (N, New_List);
2437 Implicit_With_On_Parent (Lib_Unit, N);
2440 -- If the unit is a body, the context of the specification must also
2443 if Nkind (Lib_Unit) = N_Package_Body
2444 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2445 and then not Acts_As_Spec (N))
2447 Install_Context (Library_Unit (N));
2449 if Is_Child_Spec (Unit (Library_Unit (N))) then
2451 -- If the unit is the body of a public child unit, the private
2452 -- declarations of the parent must be made visible. If the child
2453 -- unit is private, the private declarations have been installed
2454 -- already in the call to Install_Parents for the spec. Installing
2455 -- private declarations must be done for all ancestors of public
2456 -- child units. In addition, sibling units mentioned in the
2457 -- context clause of the body are directly visible.
2460 Lib_Spec : Node_Id := Unit (Library_Unit (N));
2465 while Is_Child_Spec (Lib_Spec) loop
2466 P := Unit (Parent_Spec (Lib_Spec));
2468 if not (Private_Present (Parent (Lib_Spec))) then
2469 P_Name := Defining_Entity (P);
2470 Install_Private_Declarations (P_Name);
2471 Set_Use (Private_Declarations (Specification (P)));
2479 -- For a package body, children in context are immediately visible
2481 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
2484 if Nkind (Lib_Unit) = N_Generic_Package_Declaration
2485 or else Nkind (Lib_Unit) = N_Generic_Subprogram_Declaration
2486 or else Nkind (Lib_Unit) = N_Package_Declaration
2487 or else Nkind (Lib_Unit) = N_Subprogram_Declaration
2489 if Is_Child_Spec (Lib_Unit) then
2490 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
2491 Set_Is_Private_Descendant
2492 (Defining_Entity (Lib_Unit),
2493 Is_Private_Descendant (Lib_Parent)
2494 or else Private_Present (Parent (Lib_Unit)));
2497 Set_Is_Private_Descendant
2498 (Defining_Entity (Lib_Unit),
2499 Private_Present (Parent (Lib_Unit)));
2503 if Check_Private then
2504 Check_Private_Child_Unit (N);
2506 end Install_Context_Clauses;
2508 ---------------------
2509 -- Install_Parents --
2510 ---------------------
2512 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
2519 P := Unit (Parent_Spec (Lib_Unit));
2520 P_Name := Defining_Entity (P);
2522 if Etype (P_Name) = Any_Type then
2526 if Ekind (P_Name) = E_Generic_Package
2527 and then Nkind (Lib_Unit) /= N_Generic_Subprogram_Declaration
2528 and then Nkind (Lib_Unit) /= N_Generic_Package_Declaration
2529 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
2532 ("child of a generic package must be a generic unit", Lib_Unit);
2534 elsif not Is_Package (P_Name) then
2536 ("parent unit must be package or generic package", Lib_Unit);
2537 raise Unrecoverable_Error;
2539 elsif Present (Renamed_Object (P_Name)) then
2540 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
2541 raise Unrecoverable_Error;
2543 -- Verify that a child of an instance is itself an instance, or
2544 -- the renaming of one. Given that an instance that is a unit is
2545 -- replaced with a package declaration, check against the original
2548 elsif Nkind (Original_Node (P)) = N_Package_Instantiation
2549 and then Nkind (Lib_Unit)
2550 not in N_Renaming_Declaration
2551 and then Nkind (Original_Node (Lib_Unit))
2552 not in N_Generic_Instantiation
2555 ("child of an instance must be an instance or renaming", Lib_Unit);
2558 -- This is the recursive call that ensures all parents are loaded
2560 if Is_Child_Spec (P) then
2562 Is_Private or else Private_Present (Parent (Lib_Unit)));
2565 -- Now we can install the context for this parent
2567 Install_Context_Clauses (Parent_Spec (Lib_Unit));
2568 Install_Siblings (P_Name, Parent (Lib_Unit));
2570 -- The child unit is in the declarative region of the parent. The
2571 -- parent must therefore appear in the scope stack and be visible,
2572 -- as when compiling the corresponding body. If the child unit is
2573 -- private or it is a package body, private declarations must be
2574 -- accessible as well. Use declarations in the parent must also
2575 -- be installed. Finally, other child units of the same parent that
2576 -- are in the context are immediately visible.
2578 -- Find entity for compilation unit, and set its private descendant
2579 -- status as needed.
2581 E_Name := Defining_Entity (Lib_Unit);
2583 Set_Is_Child_Unit (E_Name);
2585 Set_Is_Private_Descendant (E_Name,
2586 Is_Private_Descendant (P_Name)
2587 or else Private_Present (Parent (Lib_Unit)));
2589 P_Spec := Specification (Unit_Declaration_Node (P_Name));
2592 -- Save current visibility of unit
2594 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
2595 Is_Immediately_Visible (P_Name);
2596 Set_Is_Immediately_Visible (P_Name);
2597 Install_Visible_Declarations (P_Name);
2598 Set_Use (Visible_Declarations (P_Spec));
2601 or else Private_Present (Parent (Lib_Unit))
2603 Install_Private_Declarations (P_Name);
2604 Set_Use (Private_Declarations (P_Spec));
2606 end Install_Parents;
2608 ----------------------
2609 -- Install_Siblings --
2610 ----------------------
2612 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
2617 function Is_Ancestor (E : Entity_Id) return Boolean;
2618 -- Determine whether the scope of a child unit is an ancestor of
2619 -- the current unit.
2620 -- Shouldn't this be somewhere more general ???
2622 function Is_Ancestor (E : Entity_Id) return Boolean is
2629 and then Par /= Standard_Standard
2642 -- Start of processing for Install_Siblings
2645 -- Iterate over explicit with clauses, and check whether the
2646 -- scope of each entity is an ancestor of the current unit.
2648 Item := First (Context_Items (N));
2650 while Present (Item) loop
2652 if Nkind (Item) = N_With_Clause
2653 and then not Implicit_With (Item)
2655 Id := Entity (Name (Item));
2657 if Is_Child_Unit (Id)
2658 and then Is_Ancestor (Scope (Id))
2660 Set_Is_Immediately_Visible (Id);
2661 Prev := Current_Entity (Id);
2663 -- Check for the presence of another unit in the context,
2664 -- that may be inadvertently hidden by the child.
2667 and then Is_Immediately_Visible (Prev)
2668 and then not Is_Child_Unit (Prev)
2674 Clause := First (Context_Items (N));
2676 while Present (Clause) loop
2677 if Nkind (Clause) = N_With_Clause
2678 and then Entity (Name (Clause)) = Prev
2681 ("child unit& hides compilation unit " &
2682 "with the same name?",
2692 -- the With_Clause may be on a grand-child, which makes
2693 -- the child immediately visible.
2695 elsif Is_Child_Unit (Scope (Id))
2696 and then Is_Ancestor (Scope (Scope (Id)))
2698 Set_Is_Immediately_Visible (Scope (Id));
2704 end Install_Siblings;
2706 -------------------------
2707 -- Install_Withed_Unit --
2708 -------------------------
2710 procedure Install_Withed_Unit (With_Clause : Node_Id) is
2711 Uname : Entity_Id := Entity (Name (With_Clause));
2712 P : constant Entity_Id := Scope (Uname);
2715 -- If the unit is a package instantiation, its body may have been
2716 -- generated for an inner instance, and the instance now denotes the
2717 -- body entity. For visibility purposes we need the instance in the
2720 if Ekind (Uname) = E_Package_Body
2721 and then Is_Generic_Instance (Uname)
2723 Uname := Spec_Entity (Uname);
2726 -- We do not apply the restrictions to an internal unit unless
2727 -- we are compiling the internal unit as a main unit. This check
2728 -- is also skipped for dummy units (for missing packages).
2730 if Sloc (Uname) /= No_Location
2731 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
2732 or else Current_Sem_Unit = Main_Unit)
2734 Check_Restricted_Unit
2735 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
2738 if P /= Standard_Standard then
2740 -- If the unit is not analyzed after analysis of the with clause,
2741 -- and it is an instantiation, then it awaits a body and is the main
2742 -- unit. Its appearance in the context of some other unit indicates
2743 -- a circular dependency (DEC suite perversity).
2745 if not Analyzed (Uname)
2746 and then Nkind (Parent (Uname)) = N_Package_Instantiation
2749 ("instantiation depends on itself", Name (With_Clause));
2751 elsif not Is_Visible_Child_Unit (Uname) then
2752 Set_Is_Visible_Child_Unit (Uname);
2754 if Is_Generic_Instance (Uname)
2755 and then Ekind (Uname) in Subprogram_Kind
2757 -- Set flag as well on the visible entity that denotes the
2758 -- instance, which renames the current one.
2760 Set_Is_Visible_Child_Unit
2762 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
2766 -- The parent unit may have been installed already, and
2767 -- may have appeared in a use clause.
2769 if In_Use (Scope (Uname)) then
2770 Set_Is_Potentially_Use_Visible (Uname);
2773 Set_Context_Installed (With_Clause);
2776 elsif not Is_Immediately_Visible (Uname) then
2777 Set_Is_Immediately_Visible (Uname);
2778 Set_Context_Installed (With_Clause);
2781 -- A with-clause overrides a with-type clause: there are no restric-
2782 -- tions on the use of package entities.
2784 if Ekind (Uname) = E_Package then
2785 Set_From_With_Type (Uname, False);
2787 end Install_Withed_Unit;
2793 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
2794 K : constant Node_Kind := Nkind (Lib_Unit);
2797 return (K in N_Generic_Declaration or else
2798 K in N_Generic_Instantiation or else
2799 K in N_Generic_Renaming_Declaration or else
2800 K = N_Package_Declaration or else
2801 K = N_Package_Renaming_Declaration or else
2802 K = N_Subprogram_Declaration or else
2803 K = N_Subprogram_Renaming_Declaration)
2804 and then Present (Parent_Spec (Lib_Unit));
2807 -----------------------
2808 -- Load_Needed_Body --
2809 -----------------------
2811 -- N is a generic unit named in a with clause, or else it is
2812 -- a unit that contains a generic unit or an inlined function.
2813 -- In order to perform an instantiation, the body of the unit
2814 -- must be present. If the unit itself is generic, we assume
2815 -- that an instantiation follows, and load and analyze the body
2816 -- unconditionally. This forces analysis of the spec as well.
2818 -- If the unit is not generic, but contains a generic unit, it
2819 -- is loaded on demand, at the point of instantiation (see ch12).
2821 procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
2822 Body_Name : Unit_Name_Type;
2823 Unum : Unit_Number_Type;
2825 Save_Style_Check : constant Boolean := Opt.Style_Check;
2826 -- The loading and analysis is done with style checks off
2829 if not GNAT_Mode then
2830 Style_Check := False;
2833 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
2836 (Load_Name => Body_Name,
2842 if Unum = No_Unit then
2846 Compiler_State := Analyzing; -- reset after load
2848 if not Fatal_Error (Unum) then
2849 if Debug_Flag_L then
2850 Write_Str ("*** Loaded generic body");
2854 Semantics (Cunit (Unum));
2860 Style_Check := Save_Style_Check;
2861 end Load_Needed_Body;
2863 --------------------
2864 -- Remove_Context --
2865 --------------------
2867 procedure Remove_Context (N : Node_Id) is
2868 Lib_Unit : constant Node_Id := Unit (N);
2871 -- If this is a child unit, first remove the parent units.
2873 if Is_Child_Spec (Lib_Unit) then
2874 Remove_Parents (Lib_Unit);
2877 Remove_Context_Clauses (N);
2880 ----------------------------
2881 -- Remove_Context_Clauses --
2882 ----------------------------
2884 procedure Remove_Context_Clauses (N : Node_Id) is
2886 Unit_Name : Entity_Id;
2890 -- Loop through context items and undo with_clauses and use_clauses.
2892 Item := First (Context_Items (N));
2894 while Present (Item) loop
2896 -- We are interested only in with clauses which got installed
2897 -- on entry, as indicated by their Context_Installed flag set
2899 if Nkind (Item) = N_With_Clause
2900 and then Context_Installed (Item)
2902 -- Remove items from one with'ed unit
2904 Unit_Name := Entity (Name (Item));
2905 Remove_Unit_From_Visibility (Unit_Name);
2906 Set_Context_Installed (Item, False);
2908 elsif Nkind (Item) = N_Use_Package_Clause then
2909 End_Use_Package (Item);
2911 elsif Nkind (Item) = N_Use_Type_Clause then
2912 End_Use_Type (Item);
2914 elsif Nkind (Item) = N_With_Type_Clause then
2915 Remove_With_Type_Clause (Name (Item));
2921 end Remove_Context_Clauses;
2923 --------------------
2924 -- Remove_Parents --
2925 --------------------
2927 procedure Remove_Parents (Lib_Unit : Node_Id) is
2931 Vis : constant Boolean :=
2932 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
2935 if Is_Child_Spec (Lib_Unit) then
2936 P := Unit (Parent_Spec (Lib_Unit));
2937 P_Name := Defining_Entity (P);
2939 Remove_Context_Clauses (Parent_Spec (Lib_Unit));
2940 End_Package_Scope (P_Name);
2941 Set_Is_Immediately_Visible (P_Name, Vis);
2943 -- Remove from visibility the siblings as well, which are directly
2944 -- visible while the parent is in scope.
2946 E := First_Entity (P_Name);
2948 while Present (E) loop
2950 if Is_Child_Unit (E) then
2951 Set_Is_Immediately_Visible (E, False);
2957 Set_In_Package_Body (P_Name, False);
2959 -- This is the recursive call to remove the context of any
2960 -- higher level parent. This recursion ensures that all parents
2961 -- are removed in the reverse order of their installation.
2967 -----------------------------
2968 -- Remove_With_Type_Clause --
2969 -----------------------------
2971 procedure Remove_With_Type_Clause (Name : Node_Id) is
2975 procedure Unchain (E : Entity_Id);
2976 -- Remove entity from visibility list.
2978 procedure Unchain (E : Entity_Id) is
2982 Prev := Current_Entity (E);
2984 -- Package entity may appear is several with_type_clauses, and
2985 -- may have been removed already.
2991 Set_Name_Entity_Id (Chars (E), Homonym (E));
2994 while Present (Prev)
2995 and then Homonym (Prev) /= E
2997 Prev := Homonym (Prev);
3000 if (Present (Prev)) then
3001 Set_Homonym (Prev, Homonym (E));
3007 if Nkind (Name) = N_Selected_Component then
3008 Typ := Entity (Selector_Name (Name));
3010 if No (Typ) then -- error in declaration.
3019 -- If the exporting package has been analyzed, it has appeared in the
3020 -- context already and should be left alone. Otherwise, remove from
3023 if not Analyzed (Unit_Declaration_Node (P)) then
3026 Set_Is_Frozen (Typ, False);
3029 if Ekind (Typ) = E_Record_Type then
3030 Set_From_With_Type (Class_Wide_Type (Typ), False);
3031 Set_From_With_Type (Typ, False);
3034 Set_From_With_Type (P, False);
3036 -- If P is a child unit, remove parents as well.
3041 and then P /= Standard_Standard
3043 Set_From_With_Type (P, False);
3045 if not Analyzed (Unit_Declaration_Node (P)) then
3052 -- The back-end needs to know that an access type is imported, so it
3053 -- does not need elaboration and can appear in a mutually recursive
3054 -- record definition, so the imported flag on an access type is
3057 end Remove_With_Type_Clause;
3059 ---------------------------------
3060 -- Remove_Unit_From_Visibility --
3061 ---------------------------------
3063 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
3064 P : Entity_Id := Scope (Unit_Name);
3068 if Debug_Flag_I then
3069 Write_Str ("remove withed unit ");
3070 Write_Name (Chars (Unit_Name));
3074 if P /= Standard_Standard then
3075 Set_Is_Visible_Child_Unit (Unit_Name, False);
3078 Set_Is_Potentially_Use_Visible (Unit_Name, False);
3079 Set_Is_Immediately_Visible (Unit_Name, False);
3081 end Remove_Unit_From_Visibility;