2010-07-22 Andi Kleen <ak@linux.intel.com>
[official-gcc.git] / gcc / ada / sem_ch10.adb
blob1ce76e89c25a00d77abccaaaaa8ab77e092f8cde
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 0 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Util; use Exp_Util;
31 with Elists; use Elists;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Inline; use Inline;
37 with Lib; use Lib;
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;
43 with Opt; use Opt;
44 with Output; use Output;
45 with Par_SCO; use Par_SCO;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch7; use Sem_Ch7;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Util; use Sem_Util;
57 with Sem_Warn; use Sem_Warn;
58 with Stand; use Stand;
59 with Sinfo; use Sinfo;
60 with Sinfo.CN; use Sinfo.CN;
61 with Sinput; use Sinput;
62 with Snames; use Snames;
63 with Style; use Style;
64 with Stylesw; use Stylesw;
65 with Tbuild; use Tbuild;
66 with Uname; use Uname;
68 package body Sem_Ch10 is
70 -----------------------
71 -- Local Subprograms --
72 -----------------------
74 procedure Analyze_Context (N : Node_Id);
75 -- Analyzes items in the context clause of compilation unit
77 procedure Build_Limited_Views (N : Node_Id);
78 -- Build and decorate the list of shadow entities for a package mentioned
79 -- in a limited_with clause. If the package was not previously analyzed
80 -- then it also performs a basic decoration of the real entities. This is
81 -- required to do not pass non-decorated entities to the back-end.
82 -- Implements Ada 2005 (AI-50217).
84 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
85 -- Check whether the source for the body of a compilation unit must be
86 -- included in a standalone library.
88 procedure Check_Private_Child_Unit (N : Node_Id);
89 -- If a with_clause mentions a private child unit, the compilation
90 -- unit must be a member of the same family, as described in 10.1.2.
92 procedure Check_Stub_Level (N : Node_Id);
93 -- Verify that a stub is declared immediately within a compilation unit,
94 -- and not in an inner frame.
96 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
97 -- When a child unit appears in a context clause, the implicit withs on
98 -- parents are made explicit, and with clauses are inserted in the context
99 -- clause before the one for the child. If a parent in the with_clause
100 -- is a renaming, the implicit with_clause is on the renaming whose name
101 -- is mentioned in the with_clause, and not on the package it renames.
102 -- N is the compilation unit whose list of context items receives the
103 -- implicit with_clauses.
105 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
106 -- Get defining entity of parent unit of a child unit. In most cases this
107 -- is the defining entity of the unit, but for a child instance whose
108 -- parent needs a body for inlining, the instantiation node of the parent
109 -- has not yet been rewritten as a package declaration, and the entity has
110 -- to be retrieved from the Instance_Spec of the unit.
112 function Has_With_Clause
113 (C_Unit : Node_Id;
114 Pack : Entity_Id;
115 Is_Limited : Boolean := False) return Boolean;
116 -- Determine whether compilation unit C_Unit contains a [limited] with
117 -- clause for package Pack. Use the flag Is_Limited to designate desired
118 -- clause kind.
120 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
121 -- If the main unit is a child unit, implicit withs are also added for
122 -- all its ancestors.
124 function In_Chain (E : Entity_Id) return Boolean;
125 -- Check that the shadow entity is not already in the homonym chain, for
126 -- example through a limited_with clause in a parent unit.
128 procedure Install_Context_Clauses (N : Node_Id);
129 -- Subsidiary to Install_Context and Install_Parents. Process only with_
130 -- and use_clauses for current unit and its library unit if any.
132 procedure Install_Limited_Context_Clauses (N : Node_Id);
133 -- Subsidiary to Install_Context. Process only limited with_clauses for
134 -- current unit. Implements Ada 2005 (AI-50217).
136 procedure Install_Limited_Withed_Unit (N : Node_Id);
137 -- Place shadow entities for a limited_with package in the visibility
138 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
140 procedure Install_Withed_Unit
141 (With_Clause : Node_Id;
142 Private_With_OK : Boolean := False);
143 -- If the unit is not a child unit, make unit immediately visible. The
144 -- caller ensures that the unit is not already currently installed. The
145 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
146 -- is called when compiling the private part of a package, or installing
147 -- the private declarations of a parent unit.
149 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
150 -- This procedure establishes the context for the compilation of a child
151 -- unit. If Lib_Unit is a child library spec then the context of the parent
152 -- is installed, and the parent itself made immediately visible, so that
153 -- the child unit is processed in the declarative region of the parent.
154 -- Install_Parents makes a recursive call to itself to ensure that all
155 -- parents are loaded in the nested case. If Lib_Unit is a library body,
156 -- the only effect of Install_Parents is to install the private decls of
157 -- the parents, because the visible parent declarations will have been
158 -- installed as part of the context of the corresponding spec.
160 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
161 -- In the compilation of a child unit, a child of any of the ancestor
162 -- units is directly visible if it is visible, because the parent is in
163 -- an enclosing scope. Iterate over context to find child units of U_Name
164 -- or of some ancestor of it.
166 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
167 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
168 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
169 -- a library spec that has a parent. If the call to Is_Child_Spec returns
170 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
171 -- compilation unit for the parent spec.
173 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
174 -- Parent_Spec is non-empty, this is also a child unit.
176 procedure Remove_Context_Clauses (N : Node_Id);
177 -- Subsidiary of previous one. Remove use_ and with_clauses
179 procedure Remove_Limited_With_Clause (N : Node_Id);
180 -- Remove from visibility the shadow entities introduced for a package
181 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
183 procedure Remove_Parents (Lib_Unit : Node_Id);
184 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
185 -- contexts established by the corresponding call to Install_Parents are
186 -- removed. Remove_Parents contains a recursive call to itself to ensure
187 -- that all parents are removed in the nested case.
189 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
190 -- Reset all visibility flags on unit after compiling it, either as a
191 -- main unit or as a unit in the context.
193 procedure Unchain (E : Entity_Id);
194 -- Remove single entity from visibility list
196 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
197 -- Common processing for all stubs (subprograms, tasks, packages, and
198 -- protected cases). N is the stub to be analyzed. Once the subunit
199 -- name is established, load and analyze. Nam is the non-overloadable
200 -- entity for which the proper body provides a completion. Subprogram
201 -- stubs are handled differently because they can be declarations.
203 procedure sm;
204 -- A dummy procedure, for debugging use, called just before analyzing the
205 -- main unit (after dealing with any context clauses).
207 --------------------------
208 -- Limited_With_Clauses --
209 --------------------------
211 -- Limited_With clauses are the mechanism chosen for Ada05 to support
212 -- mutually recursive types declared in different units. A limited_with
213 -- clause that names package P in the context of unit U makes the types
214 -- declared in the visible part of P available within U, but with the
215 -- restriction that these types can only be used as incomplete types.
216 -- The limited_with clause does not impose a semantic dependence on P,
217 -- and it is possible for two packages to have limited_with_clauses on
218 -- each other without creating an elaboration circularity.
220 -- To support this feature, the analysis of a limited_with clause must
221 -- create an abbreviated view of the package, without performing any
222 -- semantic analysis on it. This "package abstract" contains shadow
223 -- types that are in one-one correspondence with the real types in the
224 -- package, and that have the properties of incomplete types.
226 -- The implementation creates two element lists: one to chain the shadow
227 -- entities, and one to chain the corresponding type entities in the tree
228 -- of the package. Links between corresponding entities in both chains
229 -- allow the compiler to select the proper view of a given type, depending
230 -- on the context. Note that in contrast with the handling of private
231 -- types, the limited view and the non-limited view of a type are treated
232 -- as separate entities, and no entity exchange needs to take place, which
233 -- makes the implementation must simpler than could be feared.
235 ------------------------------
236 -- Analyze_Compilation_Unit --
237 ------------------------------
239 procedure Analyze_Compilation_Unit (N : Node_Id) is
240 Unit_Node : constant Node_Id := Unit (N);
241 Lib_Unit : Node_Id := Library_Unit (N);
242 Spec_Id : Entity_Id;
243 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
244 Par_Spec_Name : Unit_Name_Type;
245 Unum : Unit_Number_Type;
247 procedure Check_Redundant_Withs
248 (Context_Items : List_Id;
249 Spec_Context_Items : List_Id := No_List);
250 -- Determine whether the context list of a compilation unit contains
251 -- redundant with clauses. When checking body clauses against spec
252 -- clauses, set Context_Items to the context list of the body and
253 -- Spec_Context_Items to that of the spec. Parent packages are not
254 -- examined for documentation purposes.
256 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
257 -- Generate cross-reference information for the parents of child units.
258 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
260 ---------------------------
261 -- Check_Redundant_Withs --
262 ---------------------------
264 procedure Check_Redundant_Withs
265 (Context_Items : List_Id;
266 Spec_Context_Items : List_Id := No_List)
268 Clause : Node_Id;
270 procedure Process_Body_Clauses
271 (Context_List : List_Id;
272 Clause : Node_Id;
273 Used : in out Boolean;
274 Used_Type_Or_Elab : in out Boolean);
275 -- Examine the context clauses of a package body, trying to match
276 -- the name entity of Clause with any list element. If the match
277 -- occurs on a use package clause, set Used to True, for a use
278 -- type clause, pragma Elaborate or pragma Elaborate_All, set
279 -- Used_Type_Or_Elab to True.
281 procedure Process_Spec_Clauses
282 (Context_List : List_Id;
283 Clause : Node_Id;
284 Used : in out Boolean;
285 Withed : in out Boolean;
286 Exit_On_Self : Boolean := False);
287 -- Examine the context clauses of a package spec, trying to match
288 -- the name entity of Clause with any list element. If the match
289 -- occurs on a use package clause, set Used to True, for a with
290 -- package clause other than Clause, set Withed to True. Limited
291 -- with clauses, implicitly generated with clauses and withs
292 -- having pragmas Elaborate or Elaborate_All applied to them are
293 -- skipped. Exit_On_Self is used to control the search loop and
294 -- force an exit whenever Clause sees itself in the search.
296 --------------------------
297 -- Process_Body_Clauses --
298 --------------------------
300 procedure Process_Body_Clauses
301 (Context_List : List_Id;
302 Clause : Node_Id;
303 Used : in out Boolean;
304 Used_Type_Or_Elab : in out Boolean)
306 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
307 Cont_Item : Node_Id;
308 Prag_Unit : Node_Id;
309 Subt_Mark : Node_Id;
310 Use_Item : Node_Id;
312 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
313 -- In an expanded name in a use clause, if the prefix is a
314 -- renamed package, the entity is set to the original package
315 -- as a result, when checking whether the package appears in a
316 -- previous with_clause, the renaming has to be taken into
317 -- account, to prevent spurious or incorrect warnings. The
318 -- common case is the use of Text_IO.
320 ---------------
321 -- Same_Unit --
322 ---------------
324 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
325 begin
326 return Entity (N) = P
327 or else
328 (Present (Renamed_Object (P))
329 and then Entity (N) = Renamed_Object (P));
330 end Same_Unit;
332 -- Start of processing for Process_Body_Clauses
334 begin
335 Used := False;
336 Used_Type_Or_Elab := False;
338 Cont_Item := First (Context_List);
339 while Present (Cont_Item) loop
341 -- Package use clause
343 if Nkind (Cont_Item) = N_Use_Package_Clause
344 and then not Used
345 then
346 -- Search through use clauses
348 Use_Item := First (Names (Cont_Item));
349 while Present (Use_Item) and then not Used loop
351 -- Case of a direct use of the one we are looking for
353 if Entity (Use_Item) = Nam_Ent then
354 Used := True;
356 -- Handle nested case, as in "with P; use P.Q.R"
358 else
359 declare
360 UE : Node_Id;
362 begin
363 -- Loop through prefixes looking for match
365 UE := Use_Item;
366 while Nkind (UE) = N_Expanded_Name loop
367 if Same_Unit (Prefix (UE), Nam_Ent) then
368 Used := True;
369 exit;
370 end if;
372 UE := Prefix (UE);
373 end loop;
374 end;
375 end if;
377 Next (Use_Item);
378 end loop;
380 -- USE TYPE clause
382 elsif Nkind (Cont_Item) = N_Use_Type_Clause
383 and then not Used_Type_Or_Elab
384 then
385 Subt_Mark := First (Subtype_Marks (Cont_Item));
386 while Present (Subt_Mark)
387 and then not Used_Type_Or_Elab
388 loop
389 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
390 Used_Type_Or_Elab := True;
391 end if;
393 Next (Subt_Mark);
394 end loop;
396 -- Pragma Elaborate or Elaborate_All
398 elsif Nkind (Cont_Item) = N_Pragma
399 and then
400 (Pragma_Name (Cont_Item) = Name_Elaborate
401 or else
402 Pragma_Name (Cont_Item) = Name_Elaborate_All)
403 and then not Used_Type_Or_Elab
404 then
405 Prag_Unit :=
406 First (Pragma_Argument_Associations (Cont_Item));
407 while Present (Prag_Unit)
408 and then not Used_Type_Or_Elab
409 loop
410 if Entity (Expression (Prag_Unit)) = Nam_Ent then
411 Used_Type_Or_Elab := True;
412 end if;
414 Next (Prag_Unit);
415 end loop;
416 end if;
418 Next (Cont_Item);
419 end loop;
420 end Process_Body_Clauses;
422 --------------------------
423 -- Process_Spec_Clauses --
424 --------------------------
426 procedure Process_Spec_Clauses
427 (Context_List : List_Id;
428 Clause : Node_Id;
429 Used : in out Boolean;
430 Withed : in out Boolean;
431 Exit_On_Self : Boolean := False)
433 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
434 Cont_Item : Node_Id;
435 Use_Item : Node_Id;
437 begin
438 Used := False;
439 Withed := False;
441 Cont_Item := First (Context_List);
442 while Present (Cont_Item) loop
444 -- Stop the search since the context items after Cont_Item
445 -- have already been examined in a previous iteration of
446 -- the reverse loop in Check_Redundant_Withs.
448 if Exit_On_Self
449 and Cont_Item = Clause
450 then
451 exit;
452 end if;
454 -- Package use clause
456 if Nkind (Cont_Item) = N_Use_Package_Clause
457 and then not Used
458 then
459 Use_Item := First (Names (Cont_Item));
460 while Present (Use_Item) and then not Used loop
461 if Entity (Use_Item) = Nam_Ent then
462 Used := True;
463 end if;
465 Next (Use_Item);
466 end loop;
468 -- Package with clause. Avoid processing self, implicitly
469 -- generated with clauses or limited with clauses. Note
470 -- that we examine with clauses having pragmas Elaborate
471 -- or Elaborate_All applied to them due to cases such as:
473 -- with Pack;
474 -- with Pack;
475 -- pragma Elaborate (Pack);
477 -- In this case, the second with clause is redundant since
478 -- the pragma applies only to the first "with Pack;".
480 elsif Nkind (Cont_Item) = N_With_Clause
481 and then not Implicit_With (Cont_Item)
482 and then not Limited_Present (Cont_Item)
483 and then Cont_Item /= Clause
484 and then Entity (Name (Cont_Item)) = Nam_Ent
485 then
486 Withed := True;
487 end if;
489 Next (Cont_Item);
490 end loop;
491 end Process_Spec_Clauses;
493 -- Start of processing for Check_Redundant_Withs
495 begin
496 Clause := Last (Context_Items);
497 while Present (Clause) loop
499 -- Avoid checking implicitly generated with clauses, limited
500 -- with clauses or withs that have pragma Elaborate or
501 -- Elaborate_All applied.
503 if Nkind (Clause) = N_With_Clause
504 and then not Implicit_With (Clause)
505 and then not Limited_Present (Clause)
506 and then not Elaborate_Present (Clause)
507 then
508 -- Package body-to-spec check
510 if Present (Spec_Context_Items) then
511 declare
512 Used_In_Body : Boolean := False;
513 Used_In_Spec : Boolean := False;
514 Used_Type_Or_Elab : Boolean := False;
515 Withed_In_Spec : Boolean := False;
517 begin
518 Process_Spec_Clauses
519 (Context_List => Spec_Context_Items,
520 Clause => Clause,
521 Used => Used_In_Spec,
522 Withed => Withed_In_Spec);
524 Process_Body_Clauses
525 (Context_List => Context_Items,
526 Clause => Clause,
527 Used => Used_In_Body,
528 Used_Type_Or_Elab => Used_Type_Or_Elab);
530 -- "Type Elab" refers to the presence of either a use
531 -- type clause, pragmas Elaborate or Elaborate_All.
533 -- +---------------+---------------------------+------+
534 -- | Spec | Body | Warn |
535 -- +--------+------+--------+------+-----------+------+
536 -- | Withed | Used | Withed | Used | Type Elab | |
537 -- | X | | X | | | X |
538 -- | X | | X | X | | |
539 -- | X | | X | | X | |
540 -- | X | | X | X | X | |
541 -- | X | X | X | | | X |
542 -- | X | X | X | | X | |
543 -- | X | X | X | X | | X |
544 -- | X | X | X | X | X | |
545 -- +--------+------+--------+------+-----------+------+
547 if (Withed_In_Spec
548 and then not Used_Type_Or_Elab)
549 and then
550 ((not Used_In_Spec
551 and then not Used_In_Body)
552 or else
553 Used_In_Spec)
554 then
555 Error_Msg_N -- CODEFIX
556 ("?redundant with clause in body", Clause);
557 end if;
559 Used_In_Body := False;
560 Used_In_Spec := False;
561 Used_Type_Or_Elab := False;
562 Withed_In_Spec := False;
563 end;
565 -- Standalone package spec or body check
567 else
568 declare
569 Dont_Care : Boolean := False;
570 Withed : Boolean := False;
572 begin
573 -- The mechanism for examining the context clauses of a
574 -- package spec can be applied to package body clauses.
576 Process_Spec_Clauses
577 (Context_List => Context_Items,
578 Clause => Clause,
579 Used => Dont_Care,
580 Withed => Withed,
581 Exit_On_Self => True);
583 if Withed then
584 Error_Msg_N -- CODEFIX
585 ("?redundant with clause", Clause);
586 end if;
587 end;
588 end if;
589 end if;
591 Prev (Clause);
592 end loop;
593 end Check_Redundant_Withs;
595 --------------------------------
596 -- Generate_Parent_References --
597 --------------------------------
599 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
600 Pref : Node_Id;
601 P_Name : Entity_Id := P_Id;
603 begin
604 Pref := Name (Parent (Defining_Entity (N)));
606 if Nkind (Pref) = N_Expanded_Name then
608 -- Done already, if the unit has been compiled indirectly as
609 -- part of the closure of its context because of inlining.
611 return;
612 end if;
614 while Nkind (Pref) = N_Selected_Component loop
615 Change_Selected_Component_To_Expanded_Name (Pref);
616 Set_Entity (Pref, P_Name);
617 Set_Etype (Pref, Etype (P_Name));
618 Generate_Reference (P_Name, Pref, 'r');
619 Pref := Prefix (Pref);
620 P_Name := Scope (P_Name);
621 end loop;
623 -- The guard here on P_Name is to handle the error condition where
624 -- the parent unit is missing because the file was not found.
626 if Present (P_Name) then
627 Set_Entity (Pref, P_Name);
628 Set_Etype (Pref, Etype (P_Name));
629 Generate_Reference (P_Name, Pref, 'r');
630 Style.Check_Identifier (Pref, P_Name);
631 end if;
632 end Generate_Parent_References;
634 -- Start of processing for Analyze_Compilation_Unit
636 begin
637 Process_Compilation_Unit_Pragmas (N);
639 -- If the unit is a subunit whose parent has not been analyzed (which
640 -- indicates that the main unit is a subunit, either the current one or
641 -- one of its descendents) then the subunit is compiled as part of the
642 -- analysis of the parent, which we proceed to do. Basically this gets
643 -- handled from the top down and we don't want to do anything at this
644 -- level (i.e. this subunit will be handled on the way down from the
645 -- parent), so at this level we immediately return. If the subunit
646 -- ends up not analyzed, it means that the parent did not contain a
647 -- stub for it, or that there errors were detected in some ancestor.
649 if Nkind (Unit_Node) = N_Subunit
650 and then not Analyzed (Lib_Unit)
651 then
652 Semantics (Lib_Unit);
654 if not Analyzed (Proper_Body (Unit_Node)) then
655 if Serious_Errors_Detected > 0 then
656 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
657 else
658 Error_Msg_N ("missing stub for subunit", N);
659 end if;
660 end if;
662 return;
663 end if;
665 -- Analyze context (this will call Sem recursively for with'ed units)
666 -- To detect circularities among with-clauses that are not caught during
667 -- loading, we set the Context_Pending flag on the current unit. If the
668 -- flag is already set there is a potential circularity.
669 -- We exclude predefined units from this check because they are known
670 -- to be safe. We also exclude package bodies that are present because
671 -- circularities between bodies are harmless (and necessary).
673 if Context_Pending (N) then
674 declare
675 Circularity : Boolean := True;
677 begin
678 if Is_Predefined_File_Name
679 (Unit_File_Name (Get_Source_Unit (Unit (N))))
680 then
681 Circularity := False;
683 else
684 for U in Main_Unit + 1 .. Last_Unit loop
685 if Nkind (Unit (Cunit (U))) = N_Package_Body
686 and then not Analyzed (Cunit (U))
687 then
688 Circularity := False;
689 exit;
690 end if;
691 end loop;
692 end if;
694 if Circularity then
695 Error_Msg_N ("circular dependency caused by with_clauses", N);
696 Error_Msg_N
697 ("\possibly missing limited_with clause"
698 & " in one of the following", N);
700 for U in Main_Unit .. Last_Unit loop
701 if Context_Pending (Cunit (U)) then
702 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
703 Error_Msg_N ("\unit$", N);
704 end if;
705 end loop;
707 raise Unrecoverable_Error;
708 end if;
709 end;
710 else
711 Set_Context_Pending (N);
712 end if;
714 Analyze_Context (N);
716 Set_Context_Pending (N, False);
718 -- If the unit is a package body, the spec is already loaded and must be
719 -- analyzed first, before we analyze the body.
721 if Nkind (Unit_Node) = N_Package_Body then
723 -- If no Lib_Unit, then there was a serious previous error, so just
724 -- ignore the entire analysis effort
726 if No (Lib_Unit) then
727 return;
729 else
730 Semantics (Lib_Unit);
731 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
733 -- Verify that the library unit is a package declaration
735 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
736 N_Generic_Package_Declaration)
737 then
738 Error_Msg_N
739 ("no legal package declaration for package body", N);
740 return;
742 -- Otherwise, the entity in the declaration is visible. Update the
743 -- version to reflect dependence of this body on the spec.
745 else
746 Spec_Id := Defining_Entity (Unit (Lib_Unit));
747 Set_Is_Immediately_Visible (Spec_Id, True);
748 Version_Update (N, Lib_Unit);
750 if Nkind (Defining_Unit_Name (Unit_Node)) =
751 N_Defining_Program_Unit_Name
752 then
753 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
754 end if;
755 end if;
756 end if;
758 -- If the unit is a subprogram body, then we similarly need to analyze
759 -- its spec. However, things are a little simpler in this case, because
760 -- here, this analysis is done only for error checking and consistency
761 -- purposes, so there's nothing else to be done.
763 elsif Nkind (Unit_Node) = N_Subprogram_Body then
764 if Acts_As_Spec (N) then
766 -- If the subprogram body is a child unit, we must create a
767 -- declaration for it, in order to properly load the parent(s).
768 -- After this, the original unit does not acts as a spec, because
769 -- there is an explicit one. If this unit appears in a context
770 -- clause, then an implicit with on the parent will be added when
771 -- installing the context. If this is the main unit, there is no
772 -- Unit_Table entry for the declaration (it has the unit number
773 -- of the main unit) and code generation is unaffected.
775 Unum := Get_Cunit_Unit_Number (N);
776 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
778 if Par_Spec_Name /= No_Unit_Name then
779 Unum :=
780 Load_Unit
781 (Load_Name => Par_Spec_Name,
782 Required => True,
783 Subunit => False,
784 Error_Node => N);
786 if Unum /= No_Unit then
788 -- Build subprogram declaration and attach parent unit to it
789 -- This subprogram declaration does not come from source,
790 -- Nevertheless the backend must generate debugging info for
791 -- it, and this must be indicated explicitly. We also mark
792 -- the body entity as a child unit now, to prevent a
793 -- cascaded error if the spec entity cannot be entered
794 -- in its scope. Finally we create a Units table entry for
795 -- the subprogram declaration, to maintain a one-to-one
796 -- correspondence with compilation unit nodes. This is
797 -- critical for the tree traversals performed by CodePeer.
799 declare
800 Loc : constant Source_Ptr := Sloc (N);
801 SCS : constant Boolean :=
802 Get_Comes_From_Source_Default;
804 begin
805 Set_Comes_From_Source_Default (False);
806 Lib_Unit :=
807 Make_Compilation_Unit (Loc,
808 Context_Items => New_Copy_List (Context_Items (N)),
809 Unit =>
810 Make_Subprogram_Declaration (Sloc (N),
811 Specification =>
812 Copy_Separate_Tree
813 (Specification (Unit_Node))),
814 Aux_Decls_Node =>
815 Make_Compilation_Unit_Aux (Loc));
817 Set_Library_Unit (N, Lib_Unit);
818 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
819 Make_Child_Decl_Unit (N);
820 Semantics (Lib_Unit);
822 -- Now that a separate declaration exists, the body
823 -- of the child unit does not act as spec any longer.
825 Set_Acts_As_Spec (N, False);
826 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
827 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
828 Set_Comes_From_Source_Default (SCS);
829 end;
830 end if;
831 end if;
833 -- Here for subprogram with separate declaration
835 else
836 Semantics (Lib_Unit);
837 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
838 Version_Update (N, Lib_Unit);
839 end if;
841 -- If this is a child unit, generate references to the parents
843 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
844 N_Defining_Program_Unit_Name
845 then
846 Generate_Parent_References (
847 Specification (Unit_Node),
848 Scope (Defining_Entity (Unit (Lib_Unit))));
849 end if;
850 end if;
852 -- If it is a child unit, the parent must be elaborated first and we
853 -- update version, since we are dependent on our parent.
855 if Is_Child_Spec (Unit_Node) then
857 -- The analysis of the parent is done with style checks off
859 declare
860 Save_Style_Check : constant Boolean := Style_Check;
861 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
862 Cunit_Boolean_Restrictions_Save;
864 begin
865 if not GNAT_Mode then
866 Style_Check := False;
867 end if;
869 Semantics (Parent_Spec (Unit_Node));
870 Version_Update (N, Parent_Spec (Unit_Node));
871 Style_Check := Save_Style_Check;
872 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
873 end;
874 end if;
876 -- With the analysis done, install the context. Note that we can't
877 -- install the context from the with clauses as we analyze them, because
878 -- each with clause must be analyzed in a clean visibility context, so
879 -- we have to wait and install them all at once.
881 Install_Context (N);
883 if Is_Child_Spec (Unit_Node) then
885 -- Set the entities of all parents in the program_unit_name
887 Generate_Parent_References (
888 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
889 end if;
891 -- All components of the context: with-clauses, library unit, ancestors
892 -- if any, (and their context) are analyzed and installed.
894 -- Call special debug routine sm if this is the main unit
896 if Current_Sem_Unit = Main_Unit then
898 end if;
900 -- Now analyze the unit (package, subprogram spec, body) itself
902 Analyze (Unit_Node);
904 if Warn_On_Redundant_Constructs then
905 Check_Redundant_Withs (Context_Items (N));
907 if Nkind (Unit_Node) = N_Package_Body then
908 Check_Redundant_Withs
909 (Context_Items => Context_Items (N),
910 Spec_Context_Items => Context_Items (Lib_Unit));
911 end if;
912 end if;
914 -- The above call might have made Unit_Node an N_Subprogram_Body from
915 -- something else, so propagate any Acts_As_Spec flag.
917 if Nkind (Unit_Node) = N_Subprogram_Body
918 and then Acts_As_Spec (Unit_Node)
919 then
920 Set_Acts_As_Spec (N);
921 end if;
923 -- Register predefined units in Rtsfind
925 declare
926 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
927 begin
928 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
929 Set_RTU_Loaded (Unit_Node);
930 end if;
931 end;
933 -- Treat compilation unit pragmas that appear after the library unit
935 if Present (Pragmas_After (Aux_Decls_Node (N))) then
936 declare
937 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
938 begin
939 while Present (Prag_Node) loop
940 Analyze (Prag_Node);
941 Next (Prag_Node);
942 end loop;
943 end;
944 end if;
946 -- Generate distribution stubs if requested and no error
948 if N = Main_Cunit
949 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
950 or else
951 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
952 and then not Fatal_Error (Main_Unit)
953 then
954 if Is_RCI_Pkg_Spec_Or_Body (N) then
956 -- Regular RCI package
958 Add_Stub_Constructs (N);
960 elsif (Nkind (Unit_Node) = N_Package_Declaration
961 and then Is_Shared_Passive (Defining_Entity
962 (Specification (Unit_Node))))
963 or else (Nkind (Unit_Node) = N_Package_Body
964 and then
965 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
966 then
967 -- Shared passive package
969 Add_Stub_Constructs (N);
971 elsif Nkind (Unit_Node) = N_Package_Instantiation
972 and then
973 Is_Remote_Call_Interface
974 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
975 then
976 -- Instantiation of a RCI generic package
978 Add_Stub_Constructs (N);
979 end if;
980 end if;
982 -- Remove unit from visibility, so that environment is clean for
983 -- the next compilation, which is either the main unit or some
984 -- other unit in the context.
986 if Nkind_In (Unit_Node, N_Package_Declaration,
987 N_Package_Renaming_Declaration,
988 N_Subprogram_Declaration)
989 or else Nkind (Unit_Node) in N_Generic_Declaration
990 or else
991 (Nkind (Unit_Node) = N_Subprogram_Body
992 and then Acts_As_Spec (Unit_Node))
993 then
994 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
996 -- If the unit is an instantiation whose body will be elaborated for
997 -- inlining purposes, use the proper entity of the instance. The
998 -- entity may be missing if the instantiation was illegal.
1000 elsif Nkind (Unit_Node) = N_Package_Instantiation
1001 and then not Error_Posted (Unit_Node)
1002 and then Present (Instance_Spec (Unit_Node))
1003 then
1004 Remove_Unit_From_Visibility
1005 (Defining_Entity (Instance_Spec (Unit_Node)));
1007 elsif Nkind (Unit_Node) = N_Package_Body
1008 or else (Nkind (Unit_Node) = N_Subprogram_Body
1009 and then not Acts_As_Spec (Unit_Node))
1010 then
1011 -- Bodies that are not the main unit are compiled if they are generic
1012 -- or contain generic or inlined units. Their analysis brings in the
1013 -- context of the corresponding spec (unit declaration) which must be
1014 -- removed as well, to return the compilation environment to its
1015 -- proper state.
1017 Remove_Context (Lib_Unit);
1018 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1019 end if;
1021 -- Last step is to deinstall the context we just installed as well as
1022 -- the unit just compiled.
1024 Remove_Context (N);
1026 -- If this is the main unit and we are generating code, we must check
1027 -- that all generic units in the context have a body if they need it,
1028 -- even if they have not been instantiated. In the absence of .ali files
1029 -- for generic units, we must force the load of the body, just to
1030 -- produce the proper error if the body is absent. We skip this
1031 -- verification if the main unit itself is generic.
1033 if Get_Cunit_Unit_Number (N) = Main_Unit
1034 and then Operating_Mode = Generate_Code
1035 and then Expander_Active
1036 then
1037 -- Check whether the source for the body of the unit must be included
1038 -- in a standalone library.
1040 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1042 -- Indicate that the main unit is now analyzed, to catch possible
1043 -- circularities between it and generic bodies. Remove main unit from
1044 -- visibility. This might seem superfluous, but the main unit must
1045 -- not be visible in the generic body expansions that follow.
1047 Set_Analyzed (N, True);
1048 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1050 declare
1051 Item : Node_Id;
1052 Nam : Entity_Id;
1053 Un : Unit_Number_Type;
1055 Save_Style_Check : constant Boolean := Style_Check;
1056 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
1057 Cunit_Boolean_Restrictions_Save;
1059 begin
1060 Item := First (Context_Items (N));
1061 while Present (Item) loop
1063 -- Check for explicit with clause
1065 if Nkind (Item) = N_With_Clause
1066 and then not Implicit_With (Item)
1068 -- Ada 2005 (AI-50217): Ignore limited-withed units
1070 and then not Limited_Present (Item)
1071 then
1072 Nam := Entity (Name (Item));
1074 -- Compile generic subprogram, unless it is intrinsic or
1075 -- imported so no body is required, or generic package body
1076 -- if the package spec requires a body.
1078 if (Is_Generic_Subprogram (Nam)
1079 and then not Is_Intrinsic_Subprogram (Nam)
1080 and then not Is_Imported (Nam))
1081 or else (Ekind (Nam) = E_Generic_Package
1082 and then Unit_Requires_Body (Nam))
1083 then
1084 Style_Check := False;
1086 if Present (Renamed_Object (Nam)) then
1087 Un :=
1088 Load_Unit
1089 (Load_Name => Get_Body_Name
1090 (Get_Unit_Name
1091 (Unit_Declaration_Node
1092 (Renamed_Object (Nam)))),
1093 Required => False,
1094 Subunit => False,
1095 Error_Node => N,
1096 Renamings => True);
1097 else
1098 Un :=
1099 Load_Unit
1100 (Load_Name => Get_Body_Name
1101 (Get_Unit_Name (Item)),
1102 Required => False,
1103 Subunit => False,
1104 Error_Node => N,
1105 Renamings => True);
1106 end if;
1108 if Un = No_Unit then
1109 Error_Msg_NE
1110 ("body of generic unit& not found", Item, Nam);
1111 exit;
1113 elsif not Analyzed (Cunit (Un))
1114 and then Un /= Main_Unit
1115 and then not Fatal_Error (Un)
1116 then
1117 Style_Check := False;
1118 Semantics (Cunit (Un));
1119 end if;
1120 end if;
1121 end if;
1123 Next (Item);
1124 end loop;
1126 Style_Check := Save_Style_Check;
1127 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1128 end;
1129 end if;
1131 -- Deal with creating elaboration Boolean if needed. We create an
1132 -- elaboration boolean only for units that come from source since
1133 -- units manufactured by the compiler never need elab checks.
1135 if Comes_From_Source (N)
1136 and then Nkind_In (Unit_Node, N_Package_Declaration,
1137 N_Generic_Package_Declaration,
1138 N_Subprogram_Declaration,
1139 N_Generic_Subprogram_Declaration)
1140 then
1141 declare
1142 Loc : constant Source_Ptr := Sloc (N);
1143 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1145 begin
1146 Spec_Id := Defining_Entity (Unit_Node);
1147 Generate_Definition (Spec_Id);
1149 -- See if an elaboration entity is required for possible access
1150 -- before elaboration checking. Note that we must allow for this
1151 -- even if -gnatE is not set, since a client may be compiled in
1152 -- -gnatE mode and reference the entity.
1154 -- These entities are also used by the binder to prevent multiple
1155 -- attempts to execute the elaboration code for the library case
1156 -- where the elaboration routine might otherwise be called more
1157 -- than once.
1159 -- Case of units which do not require elaboration checks
1162 -- Pure units do not need checks
1164 Is_Pure (Spec_Id)
1166 -- Preelaborated units do not need checks
1168 or else Is_Preelaborated (Spec_Id)
1170 -- No checks needed if pragma Elaborate_Body present
1172 or else Has_Pragma_Elaborate_Body (Spec_Id)
1174 -- No checks needed if unit does not require a body
1176 or else not Unit_Requires_Body (Spec_Id)
1178 -- No checks needed for predefined files
1180 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1182 -- No checks required if no separate spec
1184 or else Acts_As_Spec (N)
1185 then
1186 -- This is a case where we only need the entity for
1187 -- checking to prevent multiple elaboration checks.
1189 Set_Elaboration_Entity_Required (Spec_Id, False);
1191 -- Case of elaboration entity is required for access before
1192 -- elaboration checking (so certainly we must build it!)
1194 else
1195 Set_Elaboration_Entity_Required (Spec_Id, True);
1196 end if;
1198 Build_Elaboration_Entity (N, Spec_Id);
1199 end;
1200 end if;
1202 -- Freeze the compilation unit entity. This for sure is needed because
1203 -- of some warnings that can be output (see Freeze_Subprogram), but may
1204 -- in general be required. If freezing actions result, place them in the
1205 -- compilation unit actions list, and analyze them.
1207 declare
1208 Loc : constant Source_Ptr := Sloc (N);
1209 L : constant List_Id :=
1210 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), Loc);
1211 begin
1212 while Is_Non_Empty_List (L) loop
1213 Insert_Library_Level_Action (Remove_Head (L));
1214 end loop;
1215 end;
1217 Set_Analyzed (N);
1219 if Nkind (Unit_Node) = N_Package_Declaration
1220 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1221 and then Expander_Active
1222 then
1223 declare
1224 Save_Style_Check : constant Boolean := Style_Check;
1225 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1226 Options : Style_Check_Options;
1228 begin
1229 Save_Style_Check_Options (Options);
1230 Reset_Style_Check_Options;
1231 Opt.Warning_Mode := Suppress;
1232 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1234 Reset_Style_Check_Options;
1235 Set_Style_Check_Options (Options);
1236 Style_Check := Save_Style_Check;
1237 Warning_Mode := Save_Warning;
1238 end;
1239 end if;
1241 -- If we are generating obsolescent warnings, then here is where we
1242 -- generate them for the with'ed items. The reason for this special
1243 -- processing is that the normal mechanism of generating the warnings
1244 -- for referenced entities does not work for context clause references.
1245 -- That's because when we first analyze the context, it is too early to
1246 -- know if the with'ing unit is itself obsolescent (which suppresses
1247 -- the warnings).
1249 if not GNAT_Mode and then Warn_On_Obsolescent_Feature then
1251 -- Push current compilation unit as scope, so that the test for
1252 -- being within an obsolescent unit will work correctly.
1254 Push_Scope (Defining_Entity (Unit_Node));
1256 -- Loop through context items to deal with with clauses
1258 declare
1259 Item : Node_Id;
1260 Nam : Node_Id;
1261 Ent : Entity_Id;
1263 begin
1264 Item := First (Context_Items (N));
1265 while Present (Item) loop
1266 if Nkind (Item) = N_With_Clause
1268 -- Suppress this check in limited-withed units. Further work
1269 -- needed here if we decide to incorporate this check on
1270 -- limited-withed units.
1272 and then not Limited_Present (Item)
1273 then
1274 Nam := Name (Item);
1275 Ent := Entity (Nam);
1277 if Is_Obsolescent (Ent) then
1278 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1279 end if;
1280 end if;
1282 Next (Item);
1283 end loop;
1284 end;
1286 -- Remove temporary install of current unit as scope
1288 Pop_Scope;
1289 end if;
1290 end Analyze_Compilation_Unit;
1292 ---------------------
1293 -- Analyze_Context --
1294 ---------------------
1296 procedure Analyze_Context (N : Node_Id) is
1297 Ukind : constant Node_Kind := Nkind (Unit (N));
1298 Item : Node_Id;
1300 begin
1301 -- First process all configuration pragmas at the start of the context
1302 -- items. Strictly these are not part of the context clause, but that
1303 -- is where the parser puts them. In any case for sure we must analyze
1304 -- these before analyzing the actual context items, since they can have
1305 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1306 -- be with'ed as a result of changing categorizations in Ada 2005).
1308 Item := First (Context_Items (N));
1309 while Present (Item)
1310 and then Nkind (Item) = N_Pragma
1311 and then Pragma_Name (Item) in Configuration_Pragma_Names
1312 loop
1313 Analyze (Item);
1314 Next (Item);
1315 end loop;
1317 -- This is the point at which we capture the configuration settings
1318 -- for the unit. At the moment only the Optimize_Alignment setting
1319 -- needs to be captured. Probably more later ???
1321 if Optimize_Alignment_Local then
1322 Set_OA_Setting (Current_Sem_Unit, 'L');
1323 else
1324 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1325 end if;
1327 -- Loop through actual context items. This is done in two passes:
1329 -- a) The first pass analyzes non-limited with-clauses and also any
1330 -- configuration pragmas (we need to get the latter analyzed right
1331 -- away, since they can affect processing of subsequent items.
1333 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1335 while Present (Item) loop
1337 -- For with clause, analyze the with clause, and then update the
1338 -- version, since we are dependent on a unit that we with.
1340 if Nkind (Item) = N_With_Clause
1341 and then not Limited_Present (Item)
1342 then
1343 -- Skip analyzing with clause if no unit, nothing to do (this
1344 -- happens for a with that references a non-existent unit). Skip
1345 -- as well if this is a with_clause for the main unit, which
1346 -- happens if a subunit has a useless with_clause on its parent.
1348 if Present (Library_Unit (Item)) then
1349 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1350 Analyze (Item);
1352 else
1353 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1354 end if;
1355 end if;
1357 if not Implicit_With (Item) then
1358 Version_Update (N, Library_Unit (Item));
1359 end if;
1361 -- Skip pragmas. Configuration pragmas at the start were handled in
1362 -- the loop above, and remaining pragmas are not processed until we
1363 -- actually install the context (see Install_Context). We delay the
1364 -- analysis of these pragmas to make sure that we have installed all
1365 -- the implicit with's on parent units.
1367 -- Skip use clauses at this stage, since we don't want to do any
1368 -- installing of potentially use-visible entities until we
1369 -- actually install the complete context (in Install_Context).
1370 -- Otherwise things can get installed in the wrong context.
1372 else
1373 null;
1374 end if;
1376 Next (Item);
1377 end loop;
1379 -- Second pass: examine all limited_with clauses. All other context
1380 -- items are ignored in this pass.
1382 Item := First (Context_Items (N));
1383 while Present (Item) loop
1384 if Nkind (Item) = N_With_Clause
1385 and then Limited_Present (Item)
1386 then
1387 -- No need to check errors on implicitly generated limited-with
1388 -- clauses.
1390 if not Implicit_With (Item) then
1392 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1393 -- properly rejected, including renaming declarations.
1395 if not Nkind_In (Ukind, N_Package_Declaration,
1396 N_Subprogram_Declaration)
1397 and then Ukind not in N_Generic_Declaration
1398 and then Ukind not in N_Generic_Instantiation
1399 then
1400 Error_Msg_N ("limited with_clause not allowed here", Item);
1402 -- Check wrong use of a limited with clause applied to the
1403 -- compilation unit containing the limited-with clause.
1405 -- limited with P.Q;
1406 -- package P.Q is ...
1408 elsif Unit (Library_Unit (Item)) = Unit (N) then
1409 Error_Msg_N ("wrong use of limited-with clause", Item);
1411 -- Check wrong use of limited-with clause applied to some
1412 -- immediate ancestor.
1414 elsif Is_Child_Spec (Unit (N)) then
1415 declare
1416 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1417 P : Node_Id;
1419 begin
1420 P := Parent_Spec (Unit (N));
1421 loop
1422 if Unit (P) = Lib_U then
1423 Error_Msg_N ("limited with_clause of immediate "
1424 & "ancestor not allowed", Item);
1425 exit;
1426 end if;
1428 exit when not Is_Child_Spec (Unit (P));
1429 P := Parent_Spec (Unit (P));
1430 end loop;
1431 end;
1432 end if;
1434 -- Check if the limited-withed unit is already visible through
1435 -- some context clause of the current compilation unit or some
1436 -- ancestor of the current compilation unit.
1438 declare
1439 Lim_Unit_Name : constant Node_Id := Name (Item);
1440 Comp_Unit : Node_Id;
1441 It : Node_Id;
1442 Unit_Name : Node_Id;
1444 begin
1445 Comp_Unit := N;
1446 loop
1447 It := First (Context_Items (Comp_Unit));
1448 while Present (It) loop
1449 if Item /= It
1450 and then Nkind (It) = N_With_Clause
1451 and then not Limited_Present (It)
1452 and then
1453 Nkind_In (Unit (Library_Unit (It)),
1454 N_Package_Declaration,
1455 N_Package_Renaming_Declaration)
1456 then
1457 if Nkind (Unit (Library_Unit (It))) =
1458 N_Package_Declaration
1459 then
1460 Unit_Name := Name (It);
1461 else
1462 Unit_Name := Name (Unit (Library_Unit (It)));
1463 end if;
1465 -- Check if the named package (or some ancestor)
1466 -- leaves visible the full-view of the unit given
1467 -- in the limited-with clause
1469 loop
1470 if Designate_Same_Unit (Lim_Unit_Name,
1471 Unit_Name)
1472 then
1473 Error_Msg_Sloc := Sloc (It);
1474 Error_Msg_N
1475 ("simultaneous visibility of limited "
1476 & "and unlimited views not allowed",
1477 Item);
1478 Error_Msg_NE
1479 ("\unlimited view visible through "
1480 & "context clause #",
1481 Item, It);
1482 exit;
1484 elsif Nkind (Unit_Name) = N_Identifier then
1485 exit;
1486 end if;
1488 Unit_Name := Prefix (Unit_Name);
1489 end loop;
1490 end if;
1492 Next (It);
1493 end loop;
1495 exit when not Is_Child_Spec (Unit (Comp_Unit));
1497 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1498 end loop;
1499 end;
1500 end if;
1502 -- Skip analyzing with clause if no unit, see above
1504 if Present (Library_Unit (Item)) then
1505 Analyze (Item);
1506 end if;
1508 -- A limited_with does not impose an elaboration order, but
1509 -- there is a semantic dependency for recompilation purposes.
1511 if not Implicit_With (Item) then
1512 Version_Update (N, Library_Unit (Item));
1513 end if;
1515 -- Pragmas and use clauses and with clauses other than limited
1516 -- with's are ignored in this pass through the context items.
1518 else
1519 null;
1520 end if;
1522 Next (Item);
1523 end loop;
1524 end Analyze_Context;
1526 -------------------------------
1527 -- Analyze_Package_Body_Stub --
1528 -------------------------------
1530 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1531 Id : constant Entity_Id := Defining_Identifier (N);
1532 Nam : Entity_Id;
1534 begin
1535 -- The package declaration must be in the current declarative part
1537 Check_Stub_Level (N);
1538 Nam := Current_Entity_In_Scope (Id);
1540 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1541 Error_Msg_N ("missing specification for package stub", N);
1543 elsif Has_Completion (Nam)
1544 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1545 then
1546 Error_Msg_N ("duplicate or redundant stub for package", N);
1548 else
1549 -- Indicate that the body of the package exists. If we are doing
1550 -- only semantic analysis, the stub stands for the body. If we are
1551 -- generating code, the existence of the body will be confirmed
1552 -- when we load the proper body.
1554 Set_Has_Completion (Nam);
1555 Set_Scope (Defining_Entity (N), Current_Scope);
1556 Generate_Reference (Nam, Id, 'b');
1557 Analyze_Proper_Body (N, Nam);
1558 end if;
1559 end Analyze_Package_Body_Stub;
1561 -------------------------
1562 -- Analyze_Proper_Body --
1563 -------------------------
1565 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1566 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1567 Unum : Unit_Number_Type;
1569 procedure Optional_Subunit;
1570 -- This procedure is called when the main unit is a stub, or when we
1571 -- are not generating code. In such a case, we analyze the subunit if
1572 -- present, which is user-friendly and in fact required for ASIS, but
1573 -- we don't complain if the subunit is missing.
1575 ----------------------
1576 -- Optional_Subunit --
1577 ----------------------
1579 procedure Optional_Subunit is
1580 Comp_Unit : Node_Id;
1582 begin
1583 -- Try to load subunit, but ignore any errors that occur during
1584 -- the loading of the subunit, by using the special feature in
1585 -- Errout to ignore all errors. Note that Fatal_Error will still
1586 -- be set, so we will be able to check for this case below.
1588 if not ASIS_Mode then
1589 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1590 end if;
1592 Unum :=
1593 Load_Unit
1594 (Load_Name => Subunit_Name,
1595 Required => False,
1596 Subunit => True,
1597 Error_Node => N);
1599 if not ASIS_Mode then
1600 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1601 end if;
1603 -- All done if we successfully loaded the subunit
1605 if Unum /= No_Unit
1606 and then (not Fatal_Error (Unum) or else Try_Semantics)
1607 then
1608 Comp_Unit := Cunit (Unum);
1610 -- If the file was empty or seriously mangled, the unit itself may
1611 -- be missing.
1613 if No (Unit (Comp_Unit)) then
1614 Error_Msg_N
1615 ("subunit does not contain expected proper body", N);
1617 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1618 Error_Msg_N
1619 ("expected SEPARATE subunit, found child unit",
1620 Cunit_Entity (Unum));
1621 else
1622 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1623 Analyze_Subunit (Comp_Unit);
1624 Set_Library_Unit (N, Comp_Unit);
1625 end if;
1627 elsif Unum = No_Unit
1628 and then Present (Nam)
1629 then
1630 if Is_Protected_Type (Nam) then
1631 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1632 else
1633 Set_Corresponding_Body (
1634 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1635 end if;
1636 end if;
1637 end Optional_Subunit;
1639 -- Start of processing for Analyze_Proper_Body
1641 begin
1642 -- If the subunit is already loaded, it means that the main unit is a
1643 -- subunit, and that the current unit is one of its parents which was
1644 -- being analyzed to provide the needed context for the analysis of the
1645 -- subunit. In this case we analyze the subunit and continue with the
1646 -- parent, without looking a subsequent subunits.
1648 if Is_Loaded (Subunit_Name) then
1650 -- If the proper body is already linked to the stub node, the stub is
1651 -- in a generic unit and just needs analyzing.
1653 if Present (Library_Unit (N)) then
1654 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1655 Analyze_Subunit (Library_Unit (N));
1657 -- Otherwise we must load the subunit and link to it
1659 else
1660 -- Load the subunit, this must work, since we originally loaded
1661 -- the subunit earlier on. So this will not really load it, just
1662 -- give access to it.
1664 Unum :=
1665 Load_Unit
1666 (Load_Name => Subunit_Name,
1667 Required => True,
1668 Subunit => False,
1669 Error_Node => N);
1671 -- And analyze the subunit in the parent context (note that we
1672 -- do not call Semantics, since that would remove the parent
1673 -- context). Because of this, we have to manually reset the
1674 -- compiler state to Analyzing since it got destroyed by Load.
1676 if Unum /= No_Unit then
1677 Compiler_State := Analyzing;
1679 -- Check that the proper body is a subunit and not a child
1680 -- unit. If the unit was previously loaded, the error will
1681 -- have been emitted when copying the generic node, so we
1682 -- just return to avoid cascaded errors.
1684 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1685 return;
1686 end if;
1688 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1689 Analyze_Subunit (Cunit (Unum));
1690 Set_Library_Unit (N, Cunit (Unum));
1691 end if;
1692 end if;
1694 -- If the main unit is a subunit, then we are just performing semantic
1695 -- analysis on that subunit, and any other subunits of any parent unit
1696 -- should be ignored, except that if we are building trees for ASIS
1697 -- usage we want to annotate the stub properly.
1699 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1700 and then Subunit_Name /= Unit_Name (Main_Unit)
1701 then
1702 if ASIS_Mode then
1703 Optional_Subunit;
1704 end if;
1706 -- But before we return, set the flag for unloaded subunits. This
1707 -- will suppress junk warnings of variables in the same declarative
1708 -- part (or a higher level one) that are in danger of looking unused
1709 -- when in fact there might be a declaration in the subunit that we
1710 -- do not intend to load.
1712 Unloaded_Subunits := True;
1713 return;
1715 -- If the subunit is not already loaded, and we are generating code,
1716 -- then this is the case where compilation started from the parent,
1717 -- and we are generating code for an entire subunit tree. In that
1718 -- case we definitely need to load the subunit.
1720 -- In order to continue the analysis with the rest of the parent,
1721 -- and other subunits, we load the unit without requiring its
1722 -- presence, and emit a warning if not found, rather than terminating
1723 -- the compilation abruptly, as for other missing file problems.
1725 elsif Original_Operating_Mode = Generate_Code then
1727 -- If the proper body is already linked to the stub node,
1728 -- the stub is in a generic unit and just needs analyzing.
1730 -- We update the version. Although we are not technically
1731 -- semantically dependent on the subunit, given our approach
1732 -- of macro substitution of subunits, it makes sense to
1733 -- include it in the version identification.
1735 if Present (Library_Unit (N)) then
1736 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1737 Analyze_Subunit (Library_Unit (N));
1738 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1740 -- Otherwise we must load the subunit and link to it
1742 else
1743 Unum :=
1744 Load_Unit
1745 (Load_Name => Subunit_Name,
1746 Required => False,
1747 Subunit => True,
1748 Error_Node => N);
1750 -- Give message if we did not get the unit
1751 -- Emit warning even if missing subunit is not
1752 -- within main unit, to simplify debugging.
1754 if Original_Operating_Mode = Generate_Code
1755 and then Unum = No_Unit
1756 then
1757 Error_Msg_Unit_1 := Subunit_Name;
1758 Error_Msg_File_1 :=
1759 Get_File_Name (Subunit_Name, Subunit => True);
1760 Error_Msg_N
1761 ("subunit$$ in file{ not found?!!", N);
1762 Subunits_Missing := True;
1763 end if;
1765 -- Load_Unit may reset Compiler_State, since it may have been
1766 -- necessary to parse an additional units, so we make sure
1767 -- that we reset it to the Analyzing state.
1769 Compiler_State := Analyzing;
1771 if Unum /= No_Unit then
1772 if Debug_Flag_L then
1773 Write_Str ("*** Loaded subunit from stub. Analyze");
1774 Write_Eol;
1775 end if;
1777 declare
1778 Comp_Unit : constant Node_Id := Cunit (Unum);
1780 begin
1781 -- Check for child unit instead of subunit
1783 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1784 Error_Msg_N
1785 ("expected SEPARATE subunit, found child unit",
1786 Cunit_Entity (Unum));
1788 -- OK, we have a subunit
1790 else
1791 -- Set corresponding stub (even if errors)
1793 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1795 -- Collect SCO information for loaded subunit if we are
1796 -- in the main unit).
1798 if Generate_SCO
1799 and then
1800 In_Extended_Main_Source_Unit
1801 (Cunit_Entity (Current_Sem_Unit))
1802 then
1803 SCO_Record (Unum);
1804 end if;
1806 -- Analyze the unit if semantics active
1808 if not Fatal_Error (Unum) or else Try_Semantics then
1809 Analyze_Subunit (Comp_Unit);
1810 end if;
1812 -- Set the library unit pointer in any case
1814 Set_Library_Unit (N, Comp_Unit);
1816 -- We update the version. Although we are not technically
1817 -- semantically dependent on the subunit, given our
1818 -- approach of macro substitution of subunits, it makes
1819 -- sense to include it in the version identification.
1821 Version_Update (Cunit (Main_Unit), Comp_Unit);
1822 end if;
1823 end;
1824 end if;
1825 end if;
1827 -- The remaining case is when the subunit is not already loaded and
1828 -- we are not generating code. In this case we are just performing
1829 -- semantic analysis on the parent, and we are not interested in
1830 -- the subunit. For subprograms, analyze the stub as a body. For
1831 -- other entities the stub has already been marked as completed.
1833 else
1834 Optional_Subunit;
1835 end if;
1836 end Analyze_Proper_Body;
1838 ----------------------------------
1839 -- Analyze_Protected_Body_Stub --
1840 ----------------------------------
1842 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1843 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1845 begin
1846 Check_Stub_Level (N);
1848 -- First occurrence of name may have been as an incomplete type
1850 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1851 Nam := Full_View (Nam);
1852 end if;
1854 if No (Nam)
1855 or else not Is_Protected_Type (Etype (Nam))
1856 then
1857 Error_Msg_N ("missing specification for Protected body", N);
1858 else
1859 Set_Scope (Defining_Entity (N), Current_Scope);
1860 Set_Has_Completion (Etype (Nam));
1861 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1862 Analyze_Proper_Body (N, Etype (Nam));
1863 end if;
1864 end Analyze_Protected_Body_Stub;
1866 ----------------------------------
1867 -- Analyze_Subprogram_Body_Stub --
1868 ----------------------------------
1870 -- A subprogram body stub can appear with or without a previous spec. If
1871 -- there is one, then the analysis of the body will find it and verify
1872 -- conformance. The formals appearing in the specification of the stub play
1873 -- no role, except for requiring an additional conformance check. If there
1874 -- is no previous subprogram declaration, the stub acts as a spec, and
1875 -- provides the defining entity for the subprogram.
1877 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1878 Decl : Node_Id;
1880 begin
1881 Check_Stub_Level (N);
1883 -- Verify that the identifier for the stub is unique within this
1884 -- declarative part.
1886 if Nkind_In (Parent (N), N_Block_Statement,
1887 N_Package_Body,
1888 N_Subprogram_Body)
1889 then
1890 Decl := First (Declarations (Parent (N)));
1891 while Present (Decl)
1892 and then Decl /= N
1893 loop
1894 if Nkind (Decl) = N_Subprogram_Body_Stub
1895 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1896 Chars (Defining_Unit_Name (Specification (N))))
1897 then
1898 Error_Msg_N ("identifier for stub is not unique", N);
1899 end if;
1901 Next (Decl);
1902 end loop;
1903 end if;
1905 -- Treat stub as a body, which checks conformance if there is a previous
1906 -- declaration, or else introduces entity and its signature.
1908 Analyze_Subprogram_Body (N);
1909 Analyze_Proper_Body (N, Empty);
1910 end Analyze_Subprogram_Body_Stub;
1912 ---------------------
1913 -- Analyze_Subunit --
1914 ---------------------
1916 -- A subunit is compiled either by itself (for semantic checking) or as
1917 -- part of compiling the parent (for code generation). In either case, by
1918 -- the time we actually process the subunit, the parent has already been
1919 -- installed and analyzed. The node N is a compilation unit, whose context
1920 -- needs to be treated here, because we come directly here from the parent
1921 -- without calling Analyze_Compilation_Unit.
1923 -- The compilation context includes the explicit context of the subunit,
1924 -- and the context of the parent, together with the parent itself. In order
1925 -- to compile the current context, we remove the one inherited from the
1926 -- parent, in order to have a clean visibility table. We restore the parent
1927 -- context before analyzing the proper body itself. On exit, we remove only
1928 -- the explicit context of the subunit.
1930 procedure Analyze_Subunit (N : Node_Id) is
1931 Lib_Unit : constant Node_Id := Library_Unit (N);
1932 Par_Unit : constant Entity_Id := Current_Scope;
1934 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1935 Num_Scopes : Int := 0;
1936 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1937 Enclosing_Child : Entity_Id := Empty;
1938 Svg : constant Suppress_Array := Scope_Suppress;
1940 procedure Analyze_Subunit_Context;
1941 -- Capture names in use clauses of the subunit. This must be done before
1942 -- re-installing parent declarations, because items in the context must
1943 -- not be hidden by declarations local to the parent.
1945 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1946 -- Recursive procedure to restore scope of all ancestors of subunit,
1947 -- from outermost in. If parent is not a subunit, the call to install
1948 -- context installs context of spec and (if parent is a child unit) the
1949 -- context of its parents as well. It is confusing that parents should
1950 -- be treated differently in both cases, but the semantics are just not
1951 -- identical.
1953 procedure Re_Install_Use_Clauses;
1954 -- As part of the removal of the parent scope, the use clauses are
1955 -- removed, to be reinstalled when the context of the subunit has been
1956 -- analyzed. Use clauses may also have been affected by the analysis of
1957 -- the context of the subunit, so they have to be applied again, to
1958 -- insure that the compilation environment of the rest of the parent
1959 -- unit is identical.
1961 procedure Remove_Scope;
1962 -- Remove current scope from scope stack, and preserve the list of use
1963 -- clauses in it, to be reinstalled after context is analyzed.
1965 -----------------------------
1966 -- Analyze_Subunit_Context --
1967 -----------------------------
1969 procedure Analyze_Subunit_Context is
1970 Item : Node_Id;
1971 Nam : Node_Id;
1972 Unit_Name : Entity_Id;
1974 begin
1975 Analyze_Context (N);
1977 -- Make withed units immediately visible. If child unit, make the
1978 -- ultimate parent immediately visible.
1980 Item := First (Context_Items (N));
1981 while Present (Item) loop
1982 if Nkind (Item) = N_With_Clause then
1984 -- Protect frontend against previous errors in context clauses
1986 if Nkind (Name (Item)) /= N_Selected_Component then
1987 if Error_Posted (Item) then
1988 null;
1990 else
1991 Unit_Name := Entity (Name (Item));
1992 while Is_Child_Unit (Unit_Name) loop
1993 Set_Is_Visible_Child_Unit (Unit_Name);
1994 Unit_Name := Scope (Unit_Name);
1995 end loop;
1997 if not Is_Immediately_Visible (Unit_Name) then
1998 Set_Is_Immediately_Visible (Unit_Name);
1999 Set_Context_Installed (Item);
2000 end if;
2001 end if;
2002 end if;
2004 elsif Nkind (Item) = N_Use_Package_Clause then
2005 Nam := First (Names (Item));
2006 while Present (Nam) loop
2007 Analyze (Nam);
2008 Next (Nam);
2009 end loop;
2011 elsif Nkind (Item) = N_Use_Type_Clause then
2012 Nam := First (Subtype_Marks (Item));
2013 while Present (Nam) loop
2014 Analyze (Nam);
2015 Next (Nam);
2016 end loop;
2017 end if;
2019 Next (Item);
2020 end loop;
2022 -- Reset visibility of withed units. They will be made visible again
2023 -- when we install the subunit context.
2025 Item := First (Context_Items (N));
2026 while Present (Item) loop
2027 if Nkind (Item) = N_With_Clause
2029 -- Protect frontend against previous errors in context clauses
2031 and then Nkind (Name (Item)) /= N_Selected_Component
2032 and then not Error_Posted (Item)
2033 then
2034 Unit_Name := Entity (Name (Item));
2035 while Is_Child_Unit (Unit_Name) loop
2036 Set_Is_Visible_Child_Unit (Unit_Name, False);
2037 Unit_Name := Scope (Unit_Name);
2038 end loop;
2040 if Context_Installed (Item) then
2041 Set_Is_Immediately_Visible (Unit_Name, False);
2042 Set_Context_Installed (Item, False);
2043 end if;
2044 end if;
2046 Next (Item);
2047 end loop;
2048 end Analyze_Subunit_Context;
2050 ------------------------
2051 -- Re_Install_Parents --
2052 ------------------------
2054 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2055 E : Entity_Id;
2057 begin
2058 if Nkind (Unit (L)) = N_Subunit then
2059 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2060 end if;
2062 Install_Context (L);
2064 -- If the subunit occurs within a child unit, we must restore the
2065 -- immediate visibility of any siblings that may occur in context.
2067 if Present (Enclosing_Child) then
2068 Install_Siblings (Enclosing_Child, L);
2069 end if;
2071 Push_Scope (Scop);
2073 if Scop /= Par_Unit then
2074 Set_Is_Immediately_Visible (Scop);
2075 end if;
2077 -- Make entities in scope visible again. For child units, restore
2078 -- visibility only if they are actually in context.
2080 E := First_Entity (Current_Scope);
2081 while Present (E) loop
2082 if not Is_Child_Unit (E)
2083 or else Is_Visible_Child_Unit (E)
2084 then
2085 Set_Is_Immediately_Visible (E);
2086 end if;
2088 Next_Entity (E);
2089 end loop;
2091 -- A subunit appears within a body, and for a nested subunits all the
2092 -- parents are bodies. Restore full visibility of their private
2093 -- entities.
2095 if Is_Package_Or_Generic_Package (Scop) then
2096 Set_In_Package_Body (Scop);
2097 Install_Private_Declarations (Scop);
2098 end if;
2099 end Re_Install_Parents;
2101 ----------------------------
2102 -- Re_Install_Use_Clauses --
2103 ----------------------------
2105 procedure Re_Install_Use_Clauses is
2106 U : Node_Id;
2107 begin
2108 for J in reverse 1 .. Num_Scopes loop
2109 U := Use_Clauses (J);
2110 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2111 Install_Use_Clauses (U, Force_Installation => True);
2112 end loop;
2113 end Re_Install_Use_Clauses;
2115 ------------------
2116 -- Remove_Scope --
2117 ------------------
2119 procedure Remove_Scope is
2120 E : Entity_Id;
2122 begin
2123 Num_Scopes := Num_Scopes + 1;
2124 Use_Clauses (Num_Scopes) :=
2125 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2127 E := First_Entity (Current_Scope);
2128 while Present (E) loop
2129 Set_Is_Immediately_Visible (E, False);
2130 Next_Entity (E);
2131 end loop;
2133 if Is_Child_Unit (Current_Scope) then
2134 Enclosing_Child := Current_Scope;
2135 end if;
2137 Pop_Scope;
2138 end Remove_Scope;
2140 -- Start of processing for Analyze_Subunit
2142 begin
2143 if Style_Check then
2144 declare
2145 Nam : Node_Id := Name (Unit (N));
2147 begin
2148 if Nkind (Nam) = N_Selected_Component then
2149 Nam := Selector_Name (Nam);
2150 end if;
2152 Check_Identifier (Nam, Par_Unit);
2153 end;
2154 end if;
2156 if not Is_Empty_List (Context_Items (N)) then
2158 -- Save current use clauses
2160 Remove_Scope;
2161 Remove_Context (Lib_Unit);
2163 -- Now remove parents and their context, including enclosing subunits
2164 -- and the outer parent body which is not a subunit.
2166 if Present (Lib_Spec) then
2167 Remove_Context (Lib_Spec);
2169 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2170 Lib_Spec := Library_Unit (Lib_Spec);
2171 Remove_Scope;
2172 Remove_Context (Lib_Spec);
2173 end loop;
2175 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2176 Remove_Scope;
2177 end if;
2179 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2180 Remove_Context (Library_Unit (Lib_Spec));
2181 end if;
2182 end if;
2184 Set_Is_Immediately_Visible (Par_Unit, False);
2186 Analyze_Subunit_Context;
2188 Re_Install_Parents (Lib_Unit, Par_Unit);
2189 Set_Is_Immediately_Visible (Par_Unit);
2191 -- If the context includes a child unit of the parent of the subunit,
2192 -- the parent will have been removed from visibility, after compiling
2193 -- that cousin in the context. The visibility of the parent must be
2194 -- restored now. This also applies if the context includes another
2195 -- subunit of the same parent which in turn includes a child unit in
2196 -- its context.
2198 if Is_Package_Or_Generic_Package (Par_Unit) then
2199 if not Is_Immediately_Visible (Par_Unit)
2200 or else (Present (First_Entity (Par_Unit))
2201 and then not Is_Immediately_Visible
2202 (First_Entity (Par_Unit)))
2203 then
2204 Set_Is_Immediately_Visible (Par_Unit);
2205 Install_Visible_Declarations (Par_Unit);
2206 Install_Private_Declarations (Par_Unit);
2207 end if;
2208 end if;
2210 Re_Install_Use_Clauses;
2211 Install_Context (N);
2213 -- Restore state of suppress flags for current body
2215 Scope_Suppress := Svg;
2217 -- If the subunit is within a child unit, then siblings of any parent
2218 -- unit that appear in the context clause of the subunit must also be
2219 -- made immediately visible.
2221 if Present (Enclosing_Child) then
2222 Install_Siblings (Enclosing_Child, N);
2223 end if;
2224 end if;
2226 Analyze (Proper_Body (Unit (N)));
2227 Remove_Context (N);
2229 -- The subunit may contain a with_clause on a sibling of some ancestor.
2230 -- Removing the context will remove from visibility those ancestor child
2231 -- units, which must be restored to the visibility they have in the
2232 -- enclosing body.
2234 if Present (Enclosing_Child) then
2235 declare
2236 C : Entity_Id;
2237 begin
2238 C := Current_Scope;
2239 while Present (C)
2240 and then Is_Child_Unit (C)
2241 loop
2242 Set_Is_Immediately_Visible (C);
2243 Set_Is_Visible_Child_Unit (C);
2244 C := Scope (C);
2245 end loop;
2246 end;
2247 end if;
2248 end Analyze_Subunit;
2250 ----------------------------
2251 -- Analyze_Task_Body_Stub --
2252 ----------------------------
2254 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2255 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2256 Loc : constant Source_Ptr := Sloc (N);
2258 begin
2259 Check_Stub_Level (N);
2261 -- First occurrence of name may have been as an incomplete type
2263 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2264 Nam := Full_View (Nam);
2265 end if;
2267 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2268 Error_Msg_N ("missing specification for task body", N);
2269 else
2270 Set_Scope (Defining_Entity (N), Current_Scope);
2271 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2272 Set_Has_Completion (Etype (Nam));
2273 Analyze_Proper_Body (N, Etype (Nam));
2275 -- Set elaboration flag to indicate that entity is callable. This
2276 -- cannot be done in the expansion of the body itself, because the
2277 -- proper body is not in a declarative part. This is only done if
2278 -- expansion is active, because the context may be generic and the
2279 -- flag not defined yet.
2281 if Expander_Active then
2282 Insert_After (N,
2283 Make_Assignment_Statement (Loc,
2284 Name =>
2285 Make_Identifier (Loc,
2286 New_External_Name (Chars (Etype (Nam)), 'E')),
2287 Expression => New_Reference_To (Standard_True, Loc)));
2288 end if;
2289 end if;
2290 end Analyze_Task_Body_Stub;
2292 -------------------------
2293 -- Analyze_With_Clause --
2294 -------------------------
2296 -- Analyze the declaration of a unit in a with clause. At end, label the
2297 -- with clause with the defining entity for the unit.
2299 procedure Analyze_With_Clause (N : Node_Id) is
2301 -- Retrieve the original kind of the unit node, before analysis. If it
2302 -- is a subprogram instantiation, its analysis below will rewrite the
2303 -- node as the declaration of the wrapper package. If the same
2304 -- instantiation appears indirectly elsewhere in the context, it will
2305 -- have been analyzed already.
2307 Unit_Kind : constant Node_Kind :=
2308 Nkind (Original_Node (Unit (Library_Unit (N))));
2309 Nam : constant Node_Id := Name (N);
2310 E_Name : Entity_Id;
2311 Par_Name : Entity_Id;
2312 Pref : Node_Id;
2313 U : Node_Id;
2315 Intunit : Boolean;
2316 -- Set True if the unit currently being compiled is an internal unit
2318 Save_Style_Check : constant Boolean := Opt.Style_Check;
2319 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
2320 Cunit_Boolean_Restrictions_Save;
2322 begin
2323 U := Unit (Library_Unit (N));
2325 -- Several actions are skipped for dummy packages (those supplied for
2326 -- with's where no matching file could be found). Such packages are
2327 -- identified by the Sloc value being set to No_Location.
2329 if Limited_Present (N) then
2331 -- Ada 2005 (AI-50217): Build visibility structures but do not
2332 -- analyze the unit.
2334 if Sloc (U) /= No_Location then
2335 Build_Limited_Views (N);
2336 end if;
2338 return;
2339 end if;
2341 -- We reset ordinary style checking during the analysis of a with'ed
2342 -- unit, but we do NOT reset GNAT special analysis mode (the latter
2343 -- definitely *does* apply to with'ed units).
2345 if not GNAT_Mode then
2346 Style_Check := False;
2347 end if;
2349 -- If the library unit is a predefined unit, and we are in high
2350 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2351 -- for the analysis of the with'ed unit. This mode does not prevent
2352 -- explicit with'ing of run-time units.
2354 if Configurable_Run_Time_Mode
2355 and then
2356 Is_Predefined_File_Name
2357 (Unit_File_Name (Get_Source_Unit (Unit (Library_Unit (N)))))
2358 then
2359 Configurable_Run_Time_Mode := False;
2360 Semantics (Library_Unit (N));
2361 Configurable_Run_Time_Mode := True;
2363 else
2364 Semantics (Library_Unit (N));
2365 end if;
2367 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2369 if Sloc (U) /= No_Location then
2371 -- Check restrictions, except that we skip the check if this is an
2372 -- internal unit unless we are compiling the internal unit as the
2373 -- main unit. We also skip this for dummy packages.
2375 Check_Restriction_No_Dependence (Nam, N);
2377 if not Intunit or else Current_Sem_Unit = Main_Unit then
2378 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2379 end if;
2381 -- Deal with special case of GNAT.Current_Exceptions which interacts
2382 -- with the optimization of local raise statements into gotos.
2384 if Nkind (Nam) = N_Selected_Component
2385 and then Nkind (Prefix (Nam)) = N_Identifier
2386 and then Chars (Prefix (Nam)) = Name_Gnat
2387 and then (Chars (Selector_Name (Nam)) = Name_Most_Recent_Exception
2388 or else
2389 Chars (Selector_Name (Nam)) = Name_Exception_Traces)
2390 then
2391 Check_Restriction (No_Exception_Propagation, N);
2392 Special_Exception_Package_Used := True;
2393 end if;
2395 -- Check for inappropriate with of internal implementation unit if we
2396 -- are not compiling an internal unit. We do not issue this message
2397 -- for implicit with's generated by the compiler itself.
2399 if Implementation_Unit_Warnings
2400 and then not Intunit
2401 and then not Implicit_With (N)
2402 then
2403 declare
2404 U_Kind : constant Kind_Of_Unit :=
2405 Get_Kind_Of_Unit (Get_Source_Unit (U));
2407 begin
2408 if U_Kind = Implementation_Unit then
2409 Error_Msg_F ("& is an internal 'G'N'A'T unit?", Name (N));
2411 -- Add alternative name if available, otherwise issue a
2412 -- general warning message.
2414 if Error_Msg_Strlen /= 0 then
2415 Error_Msg_F ("\use ""~"" instead", Name (N));
2416 else
2417 Error_Msg_F
2418 ("\use of this unit is non-portable " &
2419 "and version-dependent?", Name (N));
2420 end if;
2422 elsif U_Kind = Ada_05_Unit
2423 and then Ada_Version < Ada_05
2424 and then Warn_On_Ada_2005_Compatibility
2425 then
2426 Error_Msg_N ("& is an Ada 2005 unit?", Name (N));
2427 end if;
2428 end;
2429 end if;
2430 end if;
2432 -- Semantic analysis of a generic unit is performed on a copy of
2433 -- the original tree. Retrieve the entity on which semantic info
2434 -- actually appears.
2436 if Unit_Kind in N_Generic_Declaration then
2437 E_Name := Defining_Entity (U);
2439 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2440 -- the case of an instantiation, semantic analysis above will have
2441 -- replaced the unit by its instantiated version. If the instance body
2442 -- has been generated, the instance now denotes the body entity. For
2443 -- visibility purposes we need the entity of its spec.
2445 elsif (Unit_Kind = N_Package_Instantiation
2446 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2447 N_Package_Instantiation)
2448 and then Nkind (U) = N_Package_Body
2449 then
2450 E_Name := Corresponding_Spec (U);
2452 elsif Unit_Kind = N_Package_Instantiation
2453 and then Nkind (U) = N_Package_Instantiation
2454 then
2455 -- If the instance has not been rewritten as a package declaration,
2456 -- then it appeared already in a previous with clause. Retrieve
2457 -- the entity from the previous instance.
2459 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2461 elsif Unit_Kind in N_Subprogram_Instantiation then
2463 -- The visible subprogram is created during instantiation, and is
2464 -- an attribute of the wrapper package. We retrieve the wrapper
2465 -- package directly from the instantiation node. If the instance
2466 -- is inlined the unit is still an instantiation. Otherwise it has
2467 -- been rewritten as the declaration of the wrapper itself.
2469 if Nkind (U) in N_Subprogram_Instantiation then
2470 E_Name :=
2471 Related_Instance
2472 (Defining_Entity (Specification (Instance_Spec (U))));
2473 else
2474 E_Name := Related_Instance (Defining_Entity (U));
2475 end if;
2477 elsif Unit_Kind = N_Package_Renaming_Declaration
2478 or else Unit_Kind in N_Generic_Renaming_Declaration
2479 then
2480 E_Name := Defining_Entity (U);
2482 elsif Unit_Kind = N_Subprogram_Body
2483 and then Nkind (Name (N)) = N_Selected_Component
2484 and then not Acts_As_Spec (Library_Unit (N))
2485 then
2486 -- For a child unit that has no spec, one has been created and
2487 -- analyzed. The entity required is that of the spec.
2489 E_Name := Corresponding_Spec (U);
2491 else
2492 E_Name := Defining_Entity (U);
2493 end if;
2495 if Nkind (Name (N)) = N_Selected_Component then
2497 -- Child unit in a with clause
2499 Change_Selected_Component_To_Expanded_Name (Name (N));
2500 end if;
2502 -- Restore style checks and restrictions
2504 Style_Check := Save_Style_Check;
2505 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
2507 -- Record the reference, but do NOT set the unit as referenced, we want
2508 -- to consider the unit as unreferenced if this is the only reference
2509 -- that occurs.
2511 Set_Entity_With_Style_Check (Name (N), E_Name);
2512 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2514 -- Generate references and check No_Dependence restriction for parents
2516 if Is_Child_Unit (E_Name) then
2517 Pref := Prefix (Name (N));
2518 Par_Name := Scope (E_Name);
2519 while Nkind (Pref) = N_Selected_Component loop
2520 Change_Selected_Component_To_Expanded_Name (Pref);
2521 Set_Entity_With_Style_Check (Pref, Par_Name);
2523 Generate_Reference (Par_Name, Pref);
2524 Check_Restriction_No_Dependence (Pref, N);
2525 Pref := Prefix (Pref);
2527 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2528 -- is set to Standard_Standard, and no attempt should be made to
2529 -- further unwind scopes.
2531 if Par_Name /= Standard_Standard then
2532 Par_Name := Scope (Par_Name);
2533 end if;
2534 end loop;
2536 if Present (Entity (Pref))
2537 and then not Analyzed (Parent (Parent (Entity (Pref))))
2538 then
2539 -- If the entity is set without its unit being compiled, the
2540 -- original parent is a renaming, and Par_Name is the renamed
2541 -- entity. For visibility purposes, we need the original entity,
2542 -- which must be analyzed now because Load_Unit directly retrieves
2543 -- the renamed unit, and the renaming declaration itself has not
2544 -- been analyzed.
2546 Analyze (Parent (Parent (Entity (Pref))));
2547 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2548 Par_Name := Entity (Pref);
2549 end if;
2551 Set_Entity_With_Style_Check (Pref, Par_Name);
2552 Generate_Reference (Par_Name, Pref);
2553 end if;
2555 -- If the withed unit is System, and a system extension pragma is
2556 -- present, compile the extension now, rather than waiting for a
2557 -- visibility check on a specific entity.
2559 if Chars (E_Name) = Name_System
2560 and then Scope (E_Name) = Standard_Standard
2561 and then Present (System_Extend_Unit)
2562 and then Present_System_Aux (N)
2563 then
2564 -- If the extension is not present, an error will have been emitted
2566 null;
2567 end if;
2569 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2570 -- to private_with units; they will be made visible later (just before
2571 -- the private part is analyzed)
2573 if Private_Present (N) then
2574 Set_Is_Immediately_Visible (E_Name, False);
2575 end if;
2576 end Analyze_With_Clause;
2578 ------------------------------
2579 -- Check_Private_Child_Unit --
2580 ------------------------------
2582 procedure Check_Private_Child_Unit (N : Node_Id) is
2583 Lib_Unit : constant Node_Id := Unit (N);
2584 Item : Node_Id;
2585 Curr_Unit : Entity_Id;
2586 Sub_Parent : Node_Id;
2587 Priv_Child : Entity_Id;
2588 Par_Lib : Entity_Id;
2589 Par_Spec : Node_Id;
2591 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2592 -- Returns true if and only if the library unit is declared with
2593 -- an explicit designation of private.
2595 -----------------------------
2596 -- Is_Private_Library_Unit --
2597 -----------------------------
2599 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2600 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2602 begin
2603 return Private_Present (Comp_Unit);
2604 end Is_Private_Library_Unit;
2606 -- Start of processing for Check_Private_Child_Unit
2608 begin
2609 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2610 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2611 Par_Lib := Curr_Unit;
2613 elsif Nkind (Lib_Unit) = N_Subunit then
2615 -- The parent is itself a body. The parent entity is to be found in
2616 -- the corresponding spec.
2618 Sub_Parent := Library_Unit (N);
2619 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2621 -- If the parent itself is a subunit, Curr_Unit is the entity
2622 -- of the enclosing body, retrieve the spec entity which is
2623 -- the proper ancestor we need for the following tests.
2625 if Ekind (Curr_Unit) = E_Package_Body then
2626 Curr_Unit := Spec_Entity (Curr_Unit);
2627 end if;
2629 Par_Lib := Curr_Unit;
2631 else
2632 Curr_Unit := Defining_Entity (Lib_Unit);
2634 Par_Lib := Curr_Unit;
2635 Par_Spec := Parent_Spec (Lib_Unit);
2637 if No (Par_Spec) then
2638 Par_Lib := Empty;
2639 else
2640 Par_Lib := Defining_Entity (Unit (Par_Spec));
2641 end if;
2642 end if;
2644 -- Loop through context items
2646 Item := First (Context_Items (N));
2647 while Present (Item) loop
2649 -- Ada 2005 (AI-262): Allow private_with of a private child package
2650 -- in public siblings
2652 if Nkind (Item) = N_With_Clause
2653 and then not Implicit_With (Item)
2654 and then not Limited_Present (Item)
2655 and then Is_Private_Descendant (Entity (Name (Item)))
2656 then
2657 Priv_Child := Entity (Name (Item));
2659 declare
2660 Curr_Parent : Entity_Id := Par_Lib;
2661 Child_Parent : Entity_Id := Scope (Priv_Child);
2662 Prv_Ancestor : Entity_Id := Child_Parent;
2663 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2665 begin
2666 -- If the child unit is a public child then locate the nearest
2667 -- private ancestor. Child_Parent will then be set to the
2668 -- parent of that ancestor.
2670 if not Is_Private_Library_Unit (Priv_Child) then
2671 while Present (Prv_Ancestor)
2672 and then not Is_Private_Library_Unit (Prv_Ancestor)
2673 loop
2674 Prv_Ancestor := Scope (Prv_Ancestor);
2675 end loop;
2677 if Present (Prv_Ancestor) then
2678 Child_Parent := Scope (Prv_Ancestor);
2679 end if;
2680 end if;
2682 while Present (Curr_Parent)
2683 and then Curr_Parent /= Standard_Standard
2684 and then Curr_Parent /= Child_Parent
2685 loop
2686 Curr_Private :=
2687 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2688 Curr_Parent := Scope (Curr_Parent);
2689 end loop;
2691 if No (Curr_Parent) then
2692 Curr_Parent := Standard_Standard;
2693 end if;
2695 if Curr_Parent /= Child_Parent then
2696 if Ekind (Priv_Child) = E_Generic_Package
2697 and then Chars (Priv_Child) in Text_IO_Package_Name
2698 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2699 then
2700 Error_Msg_NE
2701 ("& is a nested package, not a compilation unit",
2702 Name (Item), Priv_Child);
2704 else
2705 Error_Msg_N
2706 ("unit in with clause is private child unit!", Item);
2707 Error_Msg_NE
2708 ("\current unit must also have parent&!",
2709 Item, Child_Parent);
2710 end if;
2712 elsif Curr_Private
2713 or else Private_Present (Item)
2714 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2715 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2716 and then not Acts_As_Spec (Parent (Lib_Unit)))
2717 then
2718 null;
2720 else
2721 Error_Msg_NE
2722 ("current unit must also be private descendant of&",
2723 Item, Child_Parent);
2724 end if;
2725 end;
2726 end if;
2728 Next (Item);
2729 end loop;
2731 end Check_Private_Child_Unit;
2733 ----------------------
2734 -- Check_Stub_Level --
2735 ----------------------
2737 procedure Check_Stub_Level (N : Node_Id) is
2738 Par : constant Node_Id := Parent (N);
2739 Kind : constant Node_Kind := Nkind (Par);
2741 begin
2742 if Nkind_In (Kind, N_Package_Body,
2743 N_Subprogram_Body,
2744 N_Task_Body,
2745 N_Protected_Body)
2746 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
2747 then
2748 null;
2750 -- In an instance, a missing stub appears at any level. A warning
2751 -- message will have been emitted already for the missing file.
2753 elsif not In_Instance then
2754 Error_Msg_N ("stub cannot appear in an inner scope", N);
2756 elsif Expander_Active then
2757 Error_Msg_N ("missing proper body", N);
2758 end if;
2759 end Check_Stub_Level;
2761 ------------------------
2762 -- Expand_With_Clause --
2763 ------------------------
2765 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2766 Loc : constant Source_Ptr := Sloc (Nam);
2767 Ent : constant Entity_Id := Entity (Nam);
2768 Withn : Node_Id;
2769 P : Node_Id;
2771 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2772 -- Build name to be used in implicit with_clause. In most cases this
2773 -- is the source name, but if renamings are present we must make the
2774 -- original unit visible, not the one it renames. The entity in the
2775 -- with clause is the renamed unit, but the identifier is the one from
2776 -- the source, which allows us to recover the unit renaming.
2778 ---------------------
2779 -- Build_Unit_Name --
2780 ---------------------
2782 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2783 Ent : Entity_Id;
2784 Renaming : Entity_Id;
2785 Result : Node_Id;
2787 begin
2788 if Nkind (Nam) = N_Identifier then
2790 -- If the parent unit P in the name of the with_clause for P.Q
2791 -- is a renaming of package R, then the entity of the parent is
2792 -- set to R, but the identifier retains Chars (P) to be consistent
2793 -- with the source (see details in lib-load). However, the
2794 -- implicit_with_clause for the parent must make the entity for
2795 -- P visible, because P.Q may be used as a prefix within the
2796 -- current unit. The entity for P is the current_entity with that
2797 -- name, because the package renaming declaration for it has just
2798 -- been analyzed. Note that this case can only happen if P.Q has
2799 -- already appeared in a previous with_clause in a related unit,
2800 -- such as the library body of the current unit.
2802 if Chars (Nam) /= Chars (Entity (Nam)) then
2803 Renaming := Current_Entity (Nam);
2804 pragma Assert (Renamed_Entity (Renaming) = Entity (Nam));
2805 return New_Occurrence_Of (Renaming, Loc);
2807 else
2808 return New_Occurrence_Of (Entity (Nam), Loc);
2809 end if;
2811 else
2812 Ent := Entity (Nam);
2814 if Present (Entity (Selector_Name (Nam)))
2815 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
2816 and then
2817 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
2818 = N_Package_Renaming_Declaration
2819 then
2820 -- The name in the with_clause is of the form A.B.C, and B
2821 -- is given by a renaming declaration. In that case we may
2822 -- not have analyzed the unit for B, but replaced it directly
2823 -- in lib-load with the unit it renames. We have to make A.B
2824 -- visible, so analyze the declaration for B now, in case it
2825 -- has not been done yet.
2827 Ent := Entity (Selector_Name (Nam));
2828 Analyze
2829 (Parent
2830 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
2831 end if;
2833 Result :=
2834 Make_Expanded_Name (Loc,
2835 Chars => Chars (Entity (Nam)),
2836 Prefix => Build_Unit_Name (Prefix (Nam)),
2837 Selector_Name => New_Occurrence_Of (Ent, Loc));
2838 Set_Entity (Result, Ent);
2839 return Result;
2840 end if;
2841 end Build_Unit_Name;
2843 -- Start of processing for Expand_With_Clause
2845 begin
2846 New_Nodes_OK := New_Nodes_OK + 1;
2847 Withn :=
2848 Make_With_Clause (Loc,
2849 Name => Build_Unit_Name (Nam));
2851 P := Parent (Unit_Declaration_Node (Ent));
2852 Set_Library_Unit (Withn, P);
2853 Set_Corresponding_Spec (Withn, Ent);
2854 Set_First_Name (Withn, True);
2855 Set_Implicit_With (Withn, True);
2857 -- If the unit is a package declaration, a private_with_clause on a
2858 -- child unit implies the implicit with on the parent is also private.
2860 if Nkind (Unit (N)) = N_Package_Declaration then
2861 Set_Private_Present (Withn, Private_Present (Item));
2862 end if;
2864 Prepend (Withn, Context_Items (N));
2865 Mark_Rewrite_Insertion (Withn);
2866 Install_Withed_Unit (Withn);
2868 if Nkind (Nam) = N_Expanded_Name then
2869 Expand_With_Clause (Item, Prefix (Nam), N);
2870 end if;
2872 New_Nodes_OK := New_Nodes_OK - 1;
2873 end Expand_With_Clause;
2875 -----------------------
2876 -- Get_Parent_Entity --
2877 -----------------------
2879 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2880 begin
2881 if Nkind (Unit) = N_Package_Body
2882 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
2883 then
2884 return Defining_Entity
2885 (Specification (Instance_Spec (Original_Node (Unit))));
2886 elsif Nkind (Unit) = N_Package_Instantiation then
2887 return Defining_Entity (Specification (Instance_Spec (Unit)));
2888 else
2889 return Defining_Entity (Unit);
2890 end if;
2891 end Get_Parent_Entity;
2893 ---------------------
2894 -- Has_With_Clause --
2895 ---------------------
2897 function Has_With_Clause
2898 (C_Unit : Node_Id;
2899 Pack : Entity_Id;
2900 Is_Limited : Boolean := False) return Boolean
2902 Item : Node_Id;
2904 function Named_Unit (Clause : Node_Id) return Entity_Id;
2905 -- Return the entity for the unit named in a [limited] with clause
2907 ----------------
2908 -- Named_Unit --
2909 ----------------
2911 function Named_Unit (Clause : Node_Id) return Entity_Id is
2912 begin
2913 if Nkind (Name (Clause)) = N_Selected_Component then
2914 return Entity (Selector_Name (Name (Clause)));
2915 else
2916 return Entity (Name (Clause));
2917 end if;
2918 end Named_Unit;
2920 -- Start of processing for Has_With_Clause
2922 begin
2923 if Present (Context_Items (C_Unit)) then
2924 Item := First (Context_Items (C_Unit));
2925 while Present (Item) loop
2926 if Nkind (Item) = N_With_Clause
2927 and then Limited_Present (Item) = Is_Limited
2928 and then Named_Unit (Item) = Pack
2929 then
2930 return True;
2931 end if;
2933 Next (Item);
2934 end loop;
2935 end if;
2937 return False;
2938 end Has_With_Clause;
2940 -----------------------------
2941 -- Implicit_With_On_Parent --
2942 -----------------------------
2944 procedure Implicit_With_On_Parent
2945 (Child_Unit : Node_Id;
2946 N : Node_Id)
2948 Loc : constant Source_Ptr := Sloc (N);
2949 P : constant Node_Id := Parent_Spec (Child_Unit);
2950 P_Unit : Node_Id := Unit (P);
2951 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
2952 Withn : Node_Id;
2954 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2955 -- Build prefix of child unit name. Recurse if needed
2957 function Build_Unit_Name return Node_Id;
2958 -- If the unit is a child unit, build qualified name with all ancestors
2960 -------------------------
2961 -- Build_Ancestor_Name --
2962 -------------------------
2964 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
2965 P_Ref : constant Node_Id :=
2966 New_Reference_To (Defining_Entity (P), Loc);
2967 P_Spec : Node_Id := P;
2969 begin
2970 -- Ancestor may have been rewritten as a package body. Retrieve
2971 -- the original spec to trace earlier ancestors.
2973 if Nkind (P) = N_Package_Body
2974 and then Nkind (Original_Node (P)) = N_Package_Instantiation
2975 then
2976 P_Spec := Original_Node (P);
2977 end if;
2979 if No (Parent_Spec (P_Spec)) then
2980 return P_Ref;
2981 else
2982 return
2983 Make_Selected_Component (Loc,
2984 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
2985 Selector_Name => P_Ref);
2986 end if;
2987 end Build_Ancestor_Name;
2989 ---------------------
2990 -- Build_Unit_Name --
2991 ---------------------
2993 function Build_Unit_Name return Node_Id is
2994 Result : Node_Id;
2996 begin
2997 if No (Parent_Spec (P_Unit)) then
2998 return New_Reference_To (P_Name, Loc);
3000 else
3001 Result :=
3002 Make_Expanded_Name (Loc,
3003 Chars => Chars (P_Name),
3004 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3005 Selector_Name => New_Reference_To (P_Name, Loc));
3006 Set_Entity (Result, P_Name);
3007 return Result;
3008 end if;
3009 end Build_Unit_Name;
3011 -- Start of processing for Implicit_With_On_Parent
3013 begin
3014 -- The unit of the current compilation may be a package body that
3015 -- replaces an instance node. In this case we need the original instance
3016 -- node to construct the proper parent name.
3018 if Nkind (P_Unit) = N_Package_Body
3019 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3020 then
3021 P_Unit := Original_Node (P_Unit);
3022 end if;
3024 -- We add the implicit with if the child unit is the current unit being
3025 -- compiled. If the current unit is a body, we do not want to add an
3026 -- implicit_with a second time to the corresponding spec.
3028 if Nkind (Child_Unit) = N_Package_Declaration
3029 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3030 then
3031 return;
3032 end if;
3034 New_Nodes_OK := New_Nodes_OK + 1;
3035 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3037 Set_Library_Unit (Withn, P);
3038 Set_Corresponding_Spec (Withn, P_Name);
3039 Set_First_Name (Withn, True);
3040 Set_Implicit_With (Withn, True);
3042 -- Node is placed at the beginning of the context items, so that
3043 -- subsequent use clauses on the parent can be validated.
3045 Prepend (Withn, Context_Items (N));
3046 Mark_Rewrite_Insertion (Withn);
3047 Install_Withed_Unit (Withn);
3049 if Is_Child_Spec (P_Unit) then
3050 Implicit_With_On_Parent (P_Unit, N);
3051 end if;
3053 New_Nodes_OK := New_Nodes_OK - 1;
3054 end Implicit_With_On_Parent;
3056 --------------
3057 -- In_Chain --
3058 --------------
3060 function In_Chain (E : Entity_Id) return Boolean is
3061 H : Entity_Id;
3063 begin
3064 H := Current_Entity (E);
3065 while Present (H) loop
3066 if H = E then
3067 return True;
3068 else
3069 H := Homonym (H);
3070 end if;
3071 end loop;
3073 return False;
3074 end In_Chain;
3076 ---------------------
3077 -- Install_Context --
3078 ---------------------
3080 procedure Install_Context (N : Node_Id) is
3081 Lib_Unit : constant Node_Id := Unit (N);
3083 begin
3084 Install_Context_Clauses (N);
3086 if Is_Child_Spec (Lib_Unit) then
3087 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3088 end if;
3090 Install_Limited_Context_Clauses (N);
3091 end Install_Context;
3093 -----------------------------
3094 -- Install_Context_Clauses --
3095 -----------------------------
3097 procedure Install_Context_Clauses (N : Node_Id) is
3098 Lib_Unit : constant Node_Id := Unit (N);
3099 Item : Node_Id;
3100 Uname_Node : Entity_Id;
3101 Check_Private : Boolean := False;
3102 Decl_Node : Node_Id;
3103 Lib_Parent : Entity_Id;
3105 begin
3106 -- First skip configuration pragmas at the start of the context. They
3107 -- are not technically part of the context clause, but that's where the
3108 -- parser puts them. Note they were analyzed in Analyze_Context.
3110 Item := First (Context_Items (N));
3111 while Present (Item)
3112 and then Nkind (Item) = N_Pragma
3113 and then Pragma_Name (Item) in Configuration_Pragma_Names
3114 loop
3115 Next (Item);
3116 end loop;
3118 -- Loop through the actual context clause items. We process everything
3119 -- except Limited_With clauses in this routine. Limited_With clauses
3120 -- are separately installed (see Install_Limited_Context_Clauses).
3122 while Present (Item) loop
3124 -- Case of explicit WITH clause
3126 if Nkind (Item) = N_With_Clause
3127 and then not Implicit_With (Item)
3128 then
3129 if Limited_Present (Item) then
3131 -- Limited withed units will be installed later
3133 goto Continue;
3135 -- If Name (Item) is not an entity name, something is wrong, and
3136 -- this will be detected in due course, for now ignore the item
3138 elsif not Is_Entity_Name (Name (Item)) then
3139 goto Continue;
3141 elsif No (Entity (Name (Item))) then
3142 Set_Entity (Name (Item), Any_Id);
3143 goto Continue;
3144 end if;
3146 Uname_Node := Entity (Name (Item));
3148 if Is_Private_Descendant (Uname_Node) then
3149 Check_Private := True;
3150 end if;
3152 Install_Withed_Unit (Item);
3154 Decl_Node := Unit_Declaration_Node (Uname_Node);
3156 -- If the unit is a subprogram instance, it appears nested within
3157 -- a package that carries the parent information.
3159 if Is_Generic_Instance (Uname_Node)
3160 and then Ekind (Uname_Node) /= E_Package
3161 then
3162 Decl_Node := Parent (Parent (Decl_Node));
3163 end if;
3165 if Is_Child_Spec (Decl_Node) then
3166 if Nkind (Name (Item)) = N_Expanded_Name then
3167 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3168 else
3169 -- If not an expanded name, the child unit must be a
3170 -- renaming, nothing to do.
3172 null;
3173 end if;
3175 elsif Nkind (Decl_Node) = N_Subprogram_Body
3176 and then not Acts_As_Spec (Parent (Decl_Node))
3177 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3178 then
3179 Implicit_With_On_Parent
3180 (Unit (Library_Unit (Parent (Decl_Node))), N);
3181 end if;
3183 -- Check license conditions unless this is a dummy unit
3185 if Sloc (Library_Unit (Item)) /= No_Location then
3186 License_Check : declare
3187 Withu : constant Unit_Number_Type :=
3188 Get_Source_Unit (Library_Unit (Item));
3189 Withl : constant License_Type :=
3190 License (Source_Index (Withu));
3191 Unitl : constant License_Type :=
3192 License (Source_Index (Current_Sem_Unit));
3194 procedure License_Error;
3195 -- Signal error of bad license
3197 -------------------
3198 -- License_Error --
3199 -------------------
3201 procedure License_Error is
3202 begin
3203 Error_Msg_N
3204 ("?license of with'ed unit & may be inconsistent",
3205 Name (Item));
3206 end License_Error;
3208 -- Start of processing for License_Check
3210 begin
3211 -- Exclude license check if withed unit is an internal unit.
3212 -- This situation arises e.g. with the GPL version of GNAT.
3214 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3215 null;
3217 -- Otherwise check various cases
3218 else
3219 case Unitl is
3220 when Unknown =>
3221 null;
3223 when Restricted =>
3224 if Withl = GPL then
3225 License_Error;
3226 end if;
3228 when GPL =>
3229 if Withl = Restricted then
3230 License_Error;
3231 end if;
3233 when Modified_GPL =>
3234 if Withl = Restricted or else Withl = GPL then
3235 License_Error;
3236 end if;
3238 when Unrestricted =>
3239 null;
3240 end case;
3241 end if;
3242 end License_Check;
3243 end if;
3245 -- Case of USE PACKAGE clause
3247 elsif Nkind (Item) = N_Use_Package_Clause then
3248 Analyze_Use_Package (Item);
3250 -- Case of USE TYPE clause
3252 elsif Nkind (Item) = N_Use_Type_Clause then
3253 Analyze_Use_Type (Item);
3255 -- case of PRAGMA
3257 elsif Nkind (Item) = N_Pragma then
3258 Analyze (Item);
3259 end if;
3261 <<Continue>>
3262 Next (Item);
3263 end loop;
3265 if Is_Child_Spec (Lib_Unit) then
3267 -- The unit also has implicit with_clauses on its own parents
3269 if No (Context_Items (N)) then
3270 Set_Context_Items (N, New_List);
3271 end if;
3273 Implicit_With_On_Parent (Lib_Unit, N);
3274 end if;
3276 -- If the unit is a body, the context of the specification must also
3277 -- be installed. That includes private with_clauses in that context.
3279 if Nkind (Lib_Unit) = N_Package_Body
3280 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3281 and then not Acts_As_Spec (N))
3282 then
3283 Install_Context (Library_Unit (N));
3285 -- Only install private with-clauses of a spec that comes from
3286 -- source, excluding specs created for a subprogram body that is
3287 -- a child unit.
3289 if Comes_From_Source (Library_Unit (N)) then
3290 Install_Private_With_Clauses
3291 (Defining_Entity (Unit (Library_Unit (N))));
3292 end if;
3294 if Is_Child_Spec (Unit (Library_Unit (N))) then
3296 -- If the unit is the body of a public child unit, the private
3297 -- declarations of the parent must be made visible. If the child
3298 -- unit is private, the private declarations have been installed
3299 -- already in the call to Install_Parents for the spec. Installing
3300 -- private declarations must be done for all ancestors of public
3301 -- child units. In addition, sibling units mentioned in the
3302 -- context clause of the body are directly visible.
3304 declare
3305 Lib_Spec : Node_Id;
3306 P : Node_Id;
3307 P_Name : Entity_Id;
3309 begin
3310 Lib_Spec := Unit (Library_Unit (N));
3311 while Is_Child_Spec (Lib_Spec) loop
3312 P := Unit (Parent_Spec (Lib_Spec));
3313 P_Name := Defining_Entity (P);
3315 if not (Private_Present (Parent (Lib_Spec)))
3316 and then not In_Private_Part (P_Name)
3317 then
3318 Install_Private_Declarations (P_Name);
3319 Install_Private_With_Clauses (P_Name);
3320 Set_Use (Private_Declarations (Specification (P)));
3321 end if;
3323 Lib_Spec := P;
3324 end loop;
3325 end;
3326 end if;
3328 -- For a package body, children in context are immediately visible
3330 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3331 end if;
3333 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3334 N_Generic_Subprogram_Declaration,
3335 N_Package_Declaration,
3336 N_Subprogram_Declaration)
3337 then
3338 if Is_Child_Spec (Lib_Unit) then
3339 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3340 Set_Is_Private_Descendant
3341 (Defining_Entity (Lib_Unit),
3342 Is_Private_Descendant (Lib_Parent)
3343 or else Private_Present (Parent (Lib_Unit)));
3345 else
3346 Set_Is_Private_Descendant
3347 (Defining_Entity (Lib_Unit),
3348 Private_Present (Parent (Lib_Unit)));
3349 end if;
3350 end if;
3352 if Check_Private then
3353 Check_Private_Child_Unit (N);
3354 end if;
3355 end Install_Context_Clauses;
3357 -------------------------------------
3358 -- Install_Limited_Context_Clauses --
3359 -------------------------------------
3361 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3362 Item : Node_Id;
3364 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3365 -- Check that the unlimited view of a given compilation_unit is not
3366 -- already visible through "use + renamings".
3368 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3369 -- Check that if a limited_with clause of a given compilation_unit
3370 -- mentions a descendant of a private child of some library unit, then
3371 -- the given compilation_unit shall be the declaration of a private
3372 -- descendant of that library unit, or a public descendant of such. The
3373 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3374 -- use entities on the limited with_clauses because their units have not
3375 -- been analyzed, so we have to climb the tree of ancestors looking for
3376 -- private keywords.
3378 procedure Expand_Limited_With_Clause
3379 (Comp_Unit : Node_Id;
3380 Nam : Node_Id;
3381 N : Node_Id);
3382 -- If a child unit appears in a limited_with clause, there are implicit
3383 -- limited_with clauses on all parents that are not already visible
3384 -- through a regular with clause. This procedure creates the implicit
3385 -- limited with_clauses for the parents and loads the corresponding
3386 -- units. The shadow entities are created when the inserted clause is
3387 -- analyzed. Implements Ada 2005 (AI-50217).
3389 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
3390 -- When compiling a unit Q descended from some parent unit P, a limited
3391 -- with_clause in the context of P that names some other ancestor of Q
3392 -- must not be installed because the ancestor is immediately visible.
3394 ---------------------
3395 -- Check_Renamings --
3396 ---------------------
3398 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3399 Item : Node_Id;
3400 Spec : Node_Id;
3401 WEnt : Entity_Id;
3402 Nam : Node_Id;
3403 E : Entity_Id;
3404 E2 : Entity_Id;
3406 begin
3407 pragma Assert (Nkind (W) = N_With_Clause);
3409 -- Protect the frontend against previous critical errors
3411 case Nkind (Unit (Library_Unit (W))) is
3412 when N_Subprogram_Declaration |
3413 N_Package_Declaration |
3414 N_Generic_Subprogram_Declaration |
3415 N_Generic_Package_Declaration =>
3416 null;
3418 when others =>
3419 return;
3420 end case;
3422 -- Check "use + renamings"
3424 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3425 Spec := Specification (Unit (P));
3427 Item := First (Visible_Declarations (Spec));
3428 while Present (Item) loop
3430 -- Look only at use package clauses
3432 if Nkind (Item) = N_Use_Package_Clause then
3434 -- Traverse the list of packages
3436 Nam := First (Names (Item));
3437 while Present (Nam) loop
3438 E := Entity (Nam);
3440 pragma Assert (Present (Parent (E)));
3442 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3443 and then Renamed_Entity (E) = WEnt
3444 then
3445 -- The unlimited view is visible through use clause and
3446 -- renamings. There is no need to generate the error
3447 -- message here because Is_Visible_Through_Renamings
3448 -- takes care of generating the precise error message.
3450 return;
3452 elsif Nkind (Parent (E)) = N_Package_Specification then
3454 -- The use clause may refer to a local package.
3455 -- Check all the enclosing scopes.
3457 E2 := E;
3458 while E2 /= Standard_Standard
3459 and then E2 /= WEnt
3460 loop
3461 E2 := Scope (E2);
3462 end loop;
3464 if E2 = WEnt then
3465 Error_Msg_N
3466 ("unlimited view visible through use clause ", W);
3467 return;
3468 end if;
3469 end if;
3471 Next (Nam);
3472 end loop;
3473 end if;
3475 Next (Item);
3476 end loop;
3478 -- Recursive call to check all the ancestors
3480 if Is_Child_Spec (Unit (P)) then
3481 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3482 end if;
3483 end Check_Renamings;
3485 ---------------------------------------
3486 -- Check_Private_Limited_Withed_Unit --
3487 ---------------------------------------
3489 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3490 Curr_Parent : Node_Id;
3491 Child_Parent : Node_Id;
3492 Curr_Private : Boolean;
3494 begin
3495 -- Compilation unit of the parent of the withed library unit
3497 Child_Parent := Library_Unit (Item);
3499 -- If the child unit is a public child, then locate its nearest
3500 -- private ancestor, if any, then Child_Parent will then be set to
3501 -- the parent of that ancestor.
3503 if not Private_Present (Library_Unit (Item)) then
3504 while Present (Child_Parent)
3505 and then not Private_Present (Child_Parent)
3506 loop
3507 Child_Parent := Parent_Spec (Unit (Child_Parent));
3508 end loop;
3510 if No (Child_Parent) then
3511 return;
3512 end if;
3513 end if;
3515 Child_Parent := Parent_Spec (Unit (Child_Parent));
3517 -- Traverse all the ancestors of the current compilation unit to
3518 -- check if it is a descendant of named library unit.
3520 Curr_Parent := Parent (Item);
3521 Curr_Private := Private_Present (Curr_Parent);
3523 while Present (Parent_Spec (Unit (Curr_Parent)))
3524 and then Curr_Parent /= Child_Parent
3525 loop
3526 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3527 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3528 end loop;
3530 if Curr_Parent /= Child_Parent then
3531 Error_Msg_N
3532 ("unit in with clause is private child unit!", Item);
3533 Error_Msg_NE
3534 ("\current unit must also have parent&!",
3535 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3537 elsif Private_Present (Parent (Item))
3538 or else Curr_Private
3539 or else Private_Present (Item)
3540 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3541 N_Subprogram_Body,
3542 N_Subunit)
3543 then
3544 -- Current unit is private, of descendant of a private unit
3546 null;
3548 else
3549 Error_Msg_NE
3550 ("current unit must also be private descendant of&",
3551 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3552 end if;
3553 end Check_Private_Limited_Withed_Unit;
3555 --------------------------------
3556 -- Expand_Limited_With_Clause --
3557 --------------------------------
3559 procedure Expand_Limited_With_Clause
3560 (Comp_Unit : Node_Id;
3561 Nam : Node_Id;
3562 N : Node_Id)
3564 Loc : constant Source_Ptr := Sloc (Nam);
3565 Unum : Unit_Number_Type;
3566 Withn : Node_Id;
3568 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3569 -- Returns true if the context already includes a with_clause for
3570 -- this unit. If the with_clause is non-limited, the unit is fully
3571 -- visible and an implicit limited_with should not be created. If
3572 -- there is already a limited_with clause for W, a second one is
3573 -- simply redundant.
3575 --------------------------
3576 -- Previous_Withed_Unit --
3577 --------------------------
3579 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3580 Item : Node_Id;
3582 begin
3583 -- A limited with_clause cannot appear in the same context_clause
3584 -- as a nonlimited with_clause which mentions the same library.
3586 Item := First (Context_Items (Comp_Unit));
3587 while Present (Item) loop
3588 if Nkind (Item) = N_With_Clause
3589 and then Library_Unit (Item) = Library_Unit (W)
3590 then
3591 return True;
3592 end if;
3594 Next (Item);
3595 end loop;
3597 return False;
3598 end Previous_Withed_Unit;
3600 -- Start of processing for Expand_Limited_With_Clause
3602 begin
3603 New_Nodes_OK := New_Nodes_OK + 1;
3605 if Nkind (Nam) = N_Identifier then
3607 -- Create node for name of withed unit
3609 Withn :=
3610 Make_With_Clause (Loc,
3611 Name => New_Copy (Nam));
3613 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3614 Withn :=
3615 Make_With_Clause (Loc,
3616 Name => Make_Selected_Component (Loc,
3617 Prefix => New_Copy_Tree (Prefix (Nam)),
3618 Selector_Name => New_Copy (Selector_Name (Nam))));
3619 Set_Parent (Withn, Parent (N));
3620 end if;
3622 Set_Limited_Present (Withn);
3623 Set_First_Name (Withn);
3624 Set_Implicit_With (Withn);
3626 Unum :=
3627 Load_Unit
3628 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3629 Required => True,
3630 Subunit => False,
3631 Error_Node => Nam);
3633 -- Do not generate a limited_with_clause on the current unit.
3634 -- This path is taken when a unit has a limited_with clause on
3635 -- one of its child units.
3637 if Unum = Current_Sem_Unit then
3638 return;
3639 end if;
3641 Set_Library_Unit (Withn, Cunit (Unum));
3642 Set_Corresponding_Spec
3643 (Withn, Specification (Unit (Cunit (Unum))));
3645 if not Previous_Withed_Unit (Withn) then
3646 Prepend (Withn, Context_Items (Parent (N)));
3647 Mark_Rewrite_Insertion (Withn);
3649 -- Add implicit limited_with_clauses for parents of child units
3650 -- mentioned in limited_with clauses.
3652 if Nkind (Nam) = N_Selected_Component then
3653 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3654 end if;
3656 Analyze (Withn);
3658 if not Limited_View_Installed (Withn) then
3659 Install_Limited_Withed_Unit (Withn);
3660 end if;
3661 end if;
3663 New_Nodes_OK := New_Nodes_OK - 1;
3664 end Expand_Limited_With_Clause;
3666 ----------------------
3667 -- Is_Ancestor_Unit --
3668 ----------------------
3670 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
3671 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
3672 E2 : Entity_Id;
3673 begin
3674 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
3675 E2 := Defining_Entity (Unit (Library_Unit (U2)));
3676 return Is_Ancestor_Package (E1, E2);
3677 else
3678 return False;
3679 end if;
3680 end Is_Ancestor_Unit;
3682 -- Start of processing for Install_Limited_Context_Clauses
3684 begin
3685 Item := First (Context_Items (N));
3686 while Present (Item) loop
3687 if Nkind (Item) = N_With_Clause
3688 and then Limited_Present (Item)
3689 then
3690 if Nkind (Name (Item)) = N_Selected_Component then
3691 Expand_Limited_With_Clause
3692 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3693 end if;
3695 Check_Private_Limited_Withed_Unit (Item);
3697 if not Implicit_With (Item)
3698 and then Is_Child_Spec (Unit (N))
3699 then
3700 Check_Renamings (Parent_Spec (Unit (N)), Item);
3701 end if;
3703 -- A unit may have a limited with on itself if it has a limited
3704 -- with_clause on one of its child units. In that case it is
3705 -- already being compiled and it makes no sense to install its
3706 -- limited view.
3708 -- If the item is a limited_private_with_clause, install it if the
3709 -- current unit is a body or if it is a private child. Otherwise
3710 -- the private clause is installed before analyzing the private
3711 -- part of the current unit.
3713 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3714 and then not Limited_View_Installed (Item)
3715 and then
3716 not Is_Ancestor_Unit
3717 (Library_Unit (Item), Cunit (Current_Sem_Unit))
3718 then
3719 if not Private_Present (Item)
3720 or else Private_Present (N)
3721 or else Nkind_In (Unit (N), N_Package_Body,
3722 N_Subprogram_Body,
3723 N_Subunit)
3724 then
3725 Install_Limited_Withed_Unit (Item);
3726 end if;
3727 end if;
3728 end if;
3730 Next (Item);
3731 end loop;
3733 -- Ada 2005 (AI-412): Examine the visible declarations of a package
3734 -- spec, looking for incomplete subtype declarations of incomplete types
3735 -- visible through a limited with clause.
3737 if Ada_Version >= Ada_05
3738 and then Analyzed (N)
3739 and then Nkind (Unit (N)) = N_Package_Declaration
3740 then
3741 declare
3742 Decl : Node_Id;
3743 Def_Id : Entity_Id;
3744 Non_Lim_View : Entity_Id;
3746 begin
3747 Decl := First (Visible_Declarations (Specification (Unit (N))));
3748 while Present (Decl) loop
3749 if Nkind (Decl) = N_Subtype_Declaration
3750 and then
3751 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3752 and then
3753 From_With_Type (Defining_Identifier (Decl))
3754 then
3755 Def_Id := Defining_Identifier (Decl);
3756 Non_Lim_View := Non_Limited_View (Def_Id);
3758 if not Is_Incomplete_Type (Non_Lim_View) then
3760 -- Convert an incomplete subtype declaration into a
3761 -- corresponding non-limited view subtype declaration.
3762 -- This is usually the case when analyzing a body that
3763 -- has regular with-clauses, when the spec has limited
3764 -- ones.
3766 -- If the non-limited view is still incomplete, it is
3767 -- the dummy entry already created, and the declaration
3768 -- cannot be reanalyzed. This is the case when installing
3769 -- a parent unit that has limited with-clauses.
3771 Set_Subtype_Indication (Decl,
3772 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3773 Set_Etype (Def_Id, Non_Lim_View);
3774 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3775 Set_Analyzed (Decl, False);
3777 -- Reanalyze the declaration, suppressing the call to
3778 -- Enter_Name to avoid duplicate names.
3780 Analyze_Subtype_Declaration
3781 (N => Decl,
3782 Skip => True);
3783 end if;
3784 end if;
3786 Next (Decl);
3787 end loop;
3788 end;
3789 end if;
3790 end Install_Limited_Context_Clauses;
3792 ---------------------
3793 -- Install_Parents --
3794 ---------------------
3796 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3797 P : Node_Id;
3798 E_Name : Entity_Id;
3799 P_Name : Entity_Id;
3800 P_Spec : Node_Id;
3802 begin
3803 P := Unit (Parent_Spec (Lib_Unit));
3804 P_Name := Get_Parent_Entity (P);
3806 if Etype (P_Name) = Any_Type then
3807 return;
3808 end if;
3810 if Ekind (P_Name) = E_Generic_Package
3811 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3812 N_Generic_Package_Declaration)
3813 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3814 then
3815 Error_Msg_N
3816 ("child of a generic package must be a generic unit", Lib_Unit);
3818 elsif not Is_Package_Or_Generic_Package (P_Name) then
3819 Error_Msg_N
3820 ("parent unit must be package or generic package", Lib_Unit);
3821 raise Unrecoverable_Error;
3823 elsif Present (Renamed_Object (P_Name)) then
3824 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3825 raise Unrecoverable_Error;
3827 -- Verify that a child of an instance is itself an instance, or the
3828 -- renaming of one. Given that an instance that is a unit is replaced
3829 -- with a package declaration, check against the original node. The
3830 -- parent may be currently being instantiated, in which case it appears
3831 -- as a declaration, but the generic_parent is already established
3832 -- indicating that we deal with an instance.
3834 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3835 if Nkind (Lib_Unit) in N_Renaming_Declaration
3836 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3837 or else
3838 (Nkind (Lib_Unit) = N_Package_Declaration
3839 and then Present (Generic_Parent (Specification (Lib_Unit))))
3840 then
3841 null;
3842 else
3843 Error_Msg_N
3844 ("child of an instance must be an instance or renaming",
3845 Lib_Unit);
3846 end if;
3847 end if;
3849 -- This is the recursive call that ensures all parents are loaded
3851 if Is_Child_Spec (P) then
3852 Install_Parents (P,
3853 Is_Private or else Private_Present (Parent (Lib_Unit)));
3854 end if;
3856 -- Now we can install the context for this parent
3858 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3859 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3860 Install_Siblings (P_Name, Parent (Lib_Unit));
3862 -- The child unit is in the declarative region of the parent. The parent
3863 -- must therefore appear in the scope stack and be visible, as when
3864 -- compiling the corresponding body. If the child unit is private or it
3865 -- is a package body, private declarations must be accessible as well.
3866 -- Use declarations in the parent must also be installed. Finally, other
3867 -- child units of the same parent that are in the context are
3868 -- immediately visible.
3870 -- Find entity for compilation unit, and set its private descendant
3871 -- status as needed. Indicate that it is a compilation unit, which is
3872 -- redundant in general, but needed if this is a generated child spec
3873 -- for a child body without previous spec.
3875 E_Name := Defining_Entity (Lib_Unit);
3877 Set_Is_Child_Unit (E_Name);
3878 Set_Is_Compilation_Unit (E_Name);
3880 Set_Is_Private_Descendant (E_Name,
3881 Is_Private_Descendant (P_Name)
3882 or else Private_Present (Parent (Lib_Unit)));
3884 P_Spec := Specification (Unit_Declaration_Node (P_Name));
3885 Push_Scope (P_Name);
3887 -- Save current visibility of unit
3889 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3890 Is_Immediately_Visible (P_Name);
3891 Set_Is_Immediately_Visible (P_Name);
3892 Install_Visible_Declarations (P_Name);
3893 Set_Use (Visible_Declarations (P_Spec));
3895 -- If the parent is a generic unit, its formal part may contain formal
3896 -- packages and use clauses for them.
3898 if Ekind (P_Name) = E_Generic_Package then
3899 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3900 end if;
3902 if Is_Private
3903 or else Private_Present (Parent (Lib_Unit))
3904 then
3905 Install_Private_Declarations (P_Name);
3906 Install_Private_With_Clauses (P_Name);
3907 Set_Use (Private_Declarations (P_Spec));
3908 end if;
3909 end Install_Parents;
3911 ----------------------------------
3912 -- Install_Private_With_Clauses --
3913 ----------------------------------
3915 procedure Install_Private_With_Clauses (P : Entity_Id) is
3916 Decl : constant Node_Id := Unit_Declaration_Node (P);
3917 Item : Node_Id;
3919 begin
3920 if Debug_Flag_I then
3921 Write_Str ("install private with clauses of ");
3922 Write_Name (Chars (P));
3923 Write_Eol;
3924 end if;
3926 if Nkind (Parent (Decl)) = N_Compilation_Unit then
3927 Item := First (Context_Items (Parent (Decl)));
3928 while Present (Item) loop
3929 if Nkind (Item) = N_With_Clause
3930 and then Private_Present (Item)
3931 then
3932 if Limited_Present (Item) then
3933 if not Limited_View_Installed (Item) then
3934 Install_Limited_Withed_Unit (Item);
3935 end if;
3936 else
3937 Install_Withed_Unit (Item, Private_With_OK => True);
3938 end if;
3939 end if;
3941 Next (Item);
3942 end loop;
3943 end if;
3944 end Install_Private_With_Clauses;
3946 ----------------------
3947 -- Install_Siblings --
3948 ----------------------
3950 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3951 Item : Node_Id;
3952 Id : Entity_Id;
3953 Prev : Entity_Id;
3955 begin
3956 -- Iterate over explicit with clauses, and check whether the scope of
3957 -- each entity is an ancestor of the current unit, in which case it is
3958 -- immediately visible.
3960 Item := First (Context_Items (N));
3961 while Present (Item) loop
3963 -- Do not install private_with_clauses declaration, unless unit
3964 -- is itself a private child unit, or is a body. Note that for a
3965 -- subprogram body the private_with_clause does not take effect until
3966 -- after the specification.
3968 if Nkind (Item) /= N_With_Clause
3969 or else Implicit_With (Item)
3970 or else Limited_Present (Item)
3971 then
3972 null;
3974 elsif not Private_Present (Item)
3975 or else Private_Present (N)
3976 or else Nkind (Unit (N)) = N_Package_Body
3977 then
3978 Id := Entity (Name (Item));
3980 if Is_Child_Unit (Id)
3981 and then Is_Ancestor_Package (Scope (Id), U_Name)
3982 then
3983 Set_Is_Immediately_Visible (Id);
3985 -- Check for the presence of another unit in the context that
3986 -- may be inadvertently hidden by the child.
3988 Prev := Current_Entity (Id);
3990 if Present (Prev)
3991 and then Is_Immediately_Visible (Prev)
3992 and then not Is_Child_Unit (Prev)
3993 then
3994 declare
3995 Clause : Node_Id;
3997 begin
3998 Clause := First (Context_Items (N));
3999 while Present (Clause) loop
4000 if Nkind (Clause) = N_With_Clause
4001 and then Entity (Name (Clause)) = Prev
4002 then
4003 Error_Msg_NE
4004 ("child unit& hides compilation unit " &
4005 "with the same name?",
4006 Name (Item), Id);
4007 exit;
4008 end if;
4010 Next (Clause);
4011 end loop;
4012 end;
4013 end if;
4015 -- The With_Clause may be on a grand-child or one of its further
4016 -- descendants, which makes a child immediately visible. Examine
4017 -- ancestry to determine whether such a child exists. For example,
4018 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4019 -- is immediately visible.
4021 elsif Is_Child_Unit (Id) then
4022 declare
4023 Par : Entity_Id;
4025 begin
4026 Par := Scope (Id);
4027 while Is_Child_Unit (Par) loop
4028 if Is_Ancestor_Package (Scope (Par), U_Name) then
4029 Set_Is_Immediately_Visible (Par);
4030 exit;
4031 end if;
4033 Par := Scope (Par);
4034 end loop;
4035 end;
4036 end if;
4038 -- If the item is a private with-clause on a child unit, the parent
4039 -- may have been installed already, but the child unit must remain
4040 -- invisible until installed in a private part or body, unless there
4041 -- is already a regular with_clause for it in the current unit.
4043 elsif Private_Present (Item) then
4044 Id := Entity (Name (Item));
4046 if Is_Child_Unit (Id) then
4047 declare
4048 Clause : Node_Id;
4050 function In_Context return Boolean;
4051 -- Scan context of current unit, to check whether there is
4052 -- a with_clause on the same unit as a private with-clause
4053 -- on a parent, in which case child unit is visible. If the
4054 -- unit is a grand-child, the same applies to its parent.
4056 ----------------
4057 -- In_Context --
4058 ----------------
4060 function In_Context return Boolean is
4061 begin
4062 Clause :=
4063 First (Context_Items (Cunit (Current_Sem_Unit)));
4064 while Present (Clause) loop
4065 if Nkind (Clause) = N_With_Clause
4066 and then Comes_From_Source (Clause)
4067 and then Is_Entity_Name (Name (Clause))
4068 and then not Private_Present (Clause)
4069 then
4070 if Entity (Name (Clause)) = Id
4071 or else
4072 (Nkind (Name (Clause)) = N_Expanded_Name
4073 and then Entity (Prefix (Name (Clause))) = Id)
4074 then
4075 return True;
4076 end if;
4077 end if;
4079 Next (Clause);
4080 end loop;
4082 return False;
4083 end In_Context;
4085 begin
4086 Set_Is_Visible_Child_Unit (Id, In_Context);
4087 end;
4088 end if;
4089 end if;
4091 Next (Item);
4092 end loop;
4093 end Install_Siblings;
4095 ---------------------------------
4096 -- Install_Limited_Withed_Unit --
4097 ---------------------------------
4099 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4100 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4101 E : Entity_Id;
4102 P : Entity_Id;
4103 Is_Child_Package : Boolean := False;
4104 Lim_Header : Entity_Id;
4105 Lim_Typ : Entity_Id;
4107 procedure Check_Body_Required;
4108 -- A unit mentioned in a limited with_clause may not be mentioned in
4109 -- a regular with_clause, but must still be included in the current
4110 -- partition. We need to determine whether the unit needs a body, so
4111 -- that the binder can determine the name of the file to be compiled.
4112 -- Checking whether a unit needs a body can be done without semantic
4113 -- analysis, by examining the nature of the declarations in the package.
4115 function Has_Limited_With_Clause
4116 (C_Unit : Entity_Id;
4117 Pack : Entity_Id) return Boolean;
4118 -- Determine whether any package in the ancestor chain starting with
4119 -- C_Unit has a limited with clause for package Pack.
4121 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4122 -- Check if some package installed though normal with-clauses has a
4123 -- renaming declaration of package P. AARM 10.1.2(21/2).
4125 -------------------------
4126 -- Check_Body_Required --
4127 -------------------------
4129 procedure Check_Body_Required is
4130 PA : constant List_Id :=
4131 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4133 procedure Check_Declarations (Spec : Node_Id);
4134 -- Recursive procedure that does the work and checks nested packages
4136 ------------------------
4137 -- Check_Declarations --
4138 ------------------------
4140 procedure Check_Declarations (Spec : Node_Id) is
4141 Decl : Node_Id;
4142 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4144 Subp_List : constant Elist_Id := New_Elmt_List;
4146 procedure Check_Pragma_Import (P : Node_Id);
4147 -- If a pragma import applies to a previous subprogram, the
4148 -- enclosing unit may not need a body. The processing is syntactic
4149 -- and does not require a declaration to be analyzed. The code
4150 -- below also handles pragma Import when applied to a subprogram
4151 -- that renames another. In this case the pragma applies to the
4152 -- renamed entity.
4154 -- Chains of multiple renames are not handled by the code below.
4155 -- It is probably impossible to handle all cases without proper
4156 -- name resolution. In such cases the algorithm is conservative
4157 -- and will indicate that a body is needed???
4159 -------------------------
4160 -- Check_Pragma_Import --
4161 -------------------------
4163 procedure Check_Pragma_Import (P : Node_Id) is
4164 Arg : Node_Id;
4165 Prev_Id : Elmt_Id;
4166 Subp_Id : Elmt_Id;
4167 Imported : Node_Id;
4169 procedure Remove_Homonyms (E : Node_Id);
4170 -- Make one pass over list of subprograms. Called again if
4171 -- subprogram is a renaming. E is known to be an identifier.
4173 ---------------------
4174 -- Remove_Homonyms --
4175 ---------------------
4177 procedure Remove_Homonyms (E : Node_Id) is
4178 R : Entity_Id := Empty;
4179 -- Name of renamed entity, if any
4181 begin
4182 Subp_Id := First_Elmt (Subp_List);
4183 while Present (Subp_Id) loop
4184 if Chars (Node (Subp_Id)) = Chars (E) then
4185 if Nkind (Parent (Parent (Node (Subp_Id))))
4186 /= N_Subprogram_Renaming_Declaration
4187 then
4188 Prev_Id := Subp_Id;
4189 Next_Elmt (Subp_Id);
4190 Remove_Elmt (Subp_List, Prev_Id);
4191 else
4192 R := Name (Parent (Parent (Node (Subp_Id))));
4193 exit;
4194 end if;
4195 else
4196 Next_Elmt (Subp_Id);
4197 end if;
4198 end loop;
4200 if Present (R) then
4201 if Nkind (R) = N_Identifier then
4202 Remove_Homonyms (R);
4204 elsif Nkind (R) = N_Selected_Component then
4205 Remove_Homonyms (Selector_Name (R));
4207 -- Renaming of attribute
4209 else
4210 null;
4211 end if;
4212 end if;
4213 end Remove_Homonyms;
4215 -- Start of processing for Check_Pragma_Import
4217 begin
4218 -- Find name of entity in Import pragma. We have not analyzed
4219 -- the construct, so we must guard against syntax errors.
4221 Arg := Next (First (Pragma_Argument_Associations (P)));
4223 if No (Arg)
4224 or else Nkind (Expression (Arg)) /= N_Identifier
4225 then
4226 return;
4227 else
4228 Imported := Expression (Arg);
4229 end if;
4231 Remove_Homonyms (Imported);
4232 end Check_Pragma_Import;
4234 -- Start of processing for Check_Declarations
4236 begin
4237 -- Search for Elaborate Body pragma
4239 Decl := First (Visible_Declarations (Spec));
4240 while Present (Decl)
4241 and then Nkind (Decl) = N_Pragma
4242 loop
4243 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4244 Set_Body_Required (Library_Unit (N));
4245 return;
4246 end if;
4248 Next (Decl);
4249 end loop;
4251 -- Look for declarations that require the presence of a body. We
4252 -- have already skipped pragmas at the start of the list.
4254 while Present (Decl) loop
4256 -- Subprogram that comes from source means body may be needed.
4257 -- Save for subsequent examination of import pragmas.
4259 if Comes_From_Source (Decl)
4260 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4261 N_Subprogram_Renaming_Declaration,
4262 N_Generic_Subprogram_Declaration))
4263 then
4264 Append_Elmt (Defining_Entity (Decl), Subp_List);
4266 -- Package declaration of generic package declaration. We need
4267 -- to recursively examine nested declarations.
4269 elsif Nkind_In (Decl, N_Package_Declaration,
4270 N_Generic_Package_Declaration)
4271 then
4272 Check_Declarations (Specification (Decl));
4274 elsif Nkind (Decl) = N_Pragma
4275 and then Pragma_Name (Decl) = Name_Import
4276 then
4277 Check_Pragma_Import (Decl);
4278 end if;
4280 Next (Decl);
4281 end loop;
4283 -- Same set of tests for private part. In addition to subprograms
4284 -- detect the presence of Taft Amendment types (incomplete types
4285 -- completed in the body).
4287 Decl := First (Private_Declarations (Spec));
4288 while Present (Decl) loop
4289 if Comes_From_Source (Decl)
4290 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4291 N_Subprogram_Renaming_Declaration,
4292 N_Generic_Subprogram_Declaration))
4293 then
4294 Append_Elmt (Defining_Entity (Decl), Subp_List);
4296 elsif Nkind_In (Decl, N_Package_Declaration,
4297 N_Generic_Package_Declaration)
4298 then
4299 Check_Declarations (Specification (Decl));
4301 -- Collect incomplete type declarations for separate pass
4303 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4304 Append_Elmt (Decl, Incomplete_Decls);
4306 elsif Nkind (Decl) = N_Pragma
4307 and then Pragma_Name (Decl) = Name_Import
4308 then
4309 Check_Pragma_Import (Decl);
4310 end if;
4312 Next (Decl);
4313 end loop;
4315 -- Now check incomplete declarations to locate Taft amendment
4316 -- types. This can be done by examining the defining identifiers
4317 -- of type declarations without real semantic analysis.
4319 declare
4320 Inc : Elmt_Id;
4322 begin
4323 Inc := First_Elmt (Incomplete_Decls);
4324 while Present (Inc) loop
4325 Decl := Next (Node (Inc));
4326 while Present (Decl) loop
4327 if Nkind (Decl) = N_Full_Type_Declaration
4328 and then Chars (Defining_Identifier (Decl)) =
4329 Chars (Defining_Identifier (Node (Inc)))
4330 then
4331 exit;
4332 end if;
4334 Next (Decl);
4335 end loop;
4337 -- If no completion, this is a TAT, and a body is needed
4339 if No (Decl) then
4340 Set_Body_Required (Library_Unit (N));
4341 return;
4342 end if;
4344 Next_Elmt (Inc);
4345 end loop;
4346 end;
4348 -- Finally, check whether there are subprograms that still
4349 -- require a body, i.e. are not renamings or null.
4351 if not Is_Empty_Elmt_List (Subp_List) then
4352 declare
4353 Subp_Id : Elmt_Id;
4354 Spec : Node_Id;
4356 begin
4357 Subp_Id := First_Elmt (Subp_List);
4358 Spec := Parent (Node (Subp_Id));
4360 while Present (Subp_Id) loop
4361 if Nkind (Parent (Spec))
4362 = N_Subprogram_Renaming_Declaration
4363 then
4364 null;
4366 elsif Nkind (Spec) = N_Procedure_Specification
4367 and then Null_Present (Spec)
4368 then
4369 null;
4371 else
4372 Set_Body_Required (Library_Unit (N));
4373 return;
4374 end if;
4376 Next_Elmt (Subp_Id);
4377 end loop;
4378 end;
4379 end if;
4380 end Check_Declarations;
4382 -- Start of processing for Check_Body_Required
4384 begin
4385 -- If this is an imported package (Java and CIL usage) no body is
4386 -- needed. Scan list of pragmas that may follow a compilation unit
4387 -- to look for a relevant pragma Import.
4389 if Present (PA) then
4390 declare
4391 Prag : Node_Id;
4393 begin
4394 Prag := First (PA);
4395 while Present (Prag) loop
4396 if Nkind (Prag) = N_Pragma
4397 and then Get_Pragma_Id (Prag) = Pragma_Import
4398 then
4399 return;
4400 end if;
4402 Next (Prag);
4403 end loop;
4404 end;
4405 end if;
4407 Check_Declarations (Specification (P_Unit));
4408 end Check_Body_Required;
4410 -----------------------------
4411 -- Has_Limited_With_Clause --
4412 -----------------------------
4414 function Has_Limited_With_Clause
4415 (C_Unit : Entity_Id;
4416 Pack : Entity_Id) return Boolean
4418 Par : Entity_Id;
4419 Par_Unit : Node_Id;
4421 begin
4422 Par := C_Unit;
4423 while Present (Par) loop
4424 if Ekind (Par) /= E_Package then
4425 exit;
4426 end if;
4428 -- Retrieve the Compilation_Unit node for Par and determine if
4429 -- its context clauses contain a limited with for Pack.
4431 Par_Unit := Parent (Parent (Parent (Par)));
4433 if Nkind (Par_Unit) = N_Package_Declaration then
4434 Par_Unit := Parent (Par_Unit);
4435 end if;
4437 if Has_With_Clause (Par_Unit, Pack, True) then
4438 return True;
4439 end if;
4441 -- If there are more ancestors, climb up the tree, otherwise
4442 -- we are done.
4444 if Is_Child_Unit (Par) then
4445 Par := Scope (Par);
4446 else
4447 exit;
4448 end if;
4449 end loop;
4451 return False;
4452 end Has_Limited_With_Clause;
4454 ----------------------------------
4455 -- Is_Visible_Through_Renamings --
4456 ----------------------------------
4458 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4459 Kind : constant Node_Kind :=
4460 Nkind (Unit (Cunit (Current_Sem_Unit)));
4461 Aux_Unit : Node_Id;
4462 Item : Node_Id;
4463 Decl : Entity_Id;
4465 begin
4466 -- Example of the error detected by this subprogram:
4468 -- package P is
4469 -- type T is ...
4470 -- end P;
4472 -- with P;
4473 -- package Q is
4474 -- package Ren_P renames P;
4475 -- end Q;
4477 -- with Q;
4478 -- package R is ...
4480 -- limited with P; -- ERROR
4481 -- package R.C is ...
4483 Aux_Unit := Cunit (Current_Sem_Unit);
4485 loop
4486 Item := First (Context_Items (Aux_Unit));
4487 while Present (Item) loop
4488 if Nkind (Item) = N_With_Clause
4489 and then not Limited_Present (Item)
4490 and then Nkind (Unit (Library_Unit (Item))) =
4491 N_Package_Declaration
4492 then
4493 Decl :=
4494 First (Visible_Declarations
4495 (Specification (Unit (Library_Unit (Item)))));
4496 while Present (Decl) loop
4497 if Nkind (Decl) = N_Package_Renaming_Declaration
4498 and then Entity (Name (Decl)) = P
4499 then
4500 -- Generate the error message only if the current unit
4501 -- is a package declaration; in case of subprogram
4502 -- bodies and package bodies we just return True to
4503 -- indicate that the limited view must not be
4504 -- installed.
4506 if Kind = N_Package_Declaration then
4507 Error_Msg_N
4508 ("simultaneous visibility of the limited and " &
4509 "unlimited views not allowed", N);
4510 Error_Msg_Sloc := Sloc (Item);
4511 Error_Msg_NE
4512 ("\\ unlimited view of & visible through the " &
4513 "context clause #", N, P);
4514 Error_Msg_Sloc := Sloc (Decl);
4515 Error_Msg_NE ("\\ and the renaming #", N, P);
4516 end if;
4518 return True;
4519 end if;
4521 Next (Decl);
4522 end loop;
4523 end if;
4525 Next (Item);
4526 end loop;
4528 -- If it is a body not acting as spec, follow pointer to the
4529 -- corresponding spec, otherwise follow pointer to parent spec.
4531 if Present (Library_Unit (Aux_Unit))
4532 and then Nkind_In (Unit (Aux_Unit),
4533 N_Package_Body, N_Subprogram_Body)
4534 then
4535 if Aux_Unit = Library_Unit (Aux_Unit) then
4537 -- Aux_Unit is a body that acts as a spec. Clause has
4538 -- already been flagged as illegal.
4540 return False;
4542 else
4543 Aux_Unit := Library_Unit (Aux_Unit);
4544 end if;
4546 else
4547 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4548 end if;
4550 exit when No (Aux_Unit);
4551 end loop;
4553 return False;
4554 end Is_Visible_Through_Renamings;
4556 -- Start of processing for Install_Limited_Withed_Unit
4558 begin
4559 pragma Assert (not Limited_View_Installed (N));
4561 -- In case of limited with_clause on subprograms, generics, instances,
4562 -- or renamings, the corresponding error was previously posted and we
4563 -- have nothing to do here. If the file is missing altogether, it has
4564 -- no source location.
4566 if Nkind (P_Unit) /= N_Package_Declaration
4567 or else Sloc (P_Unit) = No_Location
4568 then
4569 return;
4570 end if;
4572 P := Defining_Unit_Name (Specification (P_Unit));
4574 -- Handle child packages
4576 if Nkind (P) = N_Defining_Program_Unit_Name then
4577 Is_Child_Package := True;
4578 P := Defining_Identifier (P);
4579 end if;
4581 -- Do not install the limited-view if the context of the unit is already
4582 -- available through a regular with clause.
4584 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4585 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4586 then
4587 return;
4588 end if;
4590 -- Do not install the limited-view if the full-view is already visible
4591 -- through renaming declarations.
4593 if Is_Visible_Through_Renamings (P) then
4594 return;
4595 end if;
4597 -- Do not install the limited view if this is the unit being analyzed.
4598 -- This unusual case will happen when a unit has a limited_with clause
4599 -- on one of its children. The compilation of the child forces the
4600 -- load of the parent which tries to install the limited view of the
4601 -- child again. Installing the limited view must also be disabled
4602 -- when compiling the body of the child unit.
4604 if P = Cunit_Entity (Current_Sem_Unit)
4605 or else
4606 (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4607 and then P = Main_Unit_Entity)
4608 then
4609 return;
4610 end if;
4612 -- This scenario is similar to the one above, the difference is that
4613 -- the compilation of sibling Par.Sib forces the load of parent Par
4614 -- which tries to install the limited view of Lim_Pack [1]. However
4615 -- Par.Sib has a with clause for Lim_Pack [2] in its body, and thus
4616 -- needs the non-limited views of all entities from Lim_Pack.
4618 -- limited with Lim_Pack; -- [1]
4619 -- package Par is ... package Lim_Pack is ...
4621 -- with Lim_Pack; -- [2]
4622 -- package Par.Sib is ... package body Par.Sib is ...
4624 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4625 -- Sem_Unit is the body of Par.Sib.
4627 if Ekind (P) = E_Package
4628 and then Ekind (Main_Unit_Entity) = E_Package
4629 and then Is_Child_Unit (Main_Unit_Entity)
4631 -- The body has a regular with clause
4633 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4634 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4636 -- One of the ancestors has a limited with clause
4638 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4639 N_Package_Specification
4640 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4641 then
4642 return;
4643 end if;
4645 -- A common use of the limited-with is to have a limited-with
4646 -- in the package spec, and a normal with in its package body.
4647 -- For example:
4649 -- limited with X; -- [1]
4650 -- package A is ...
4652 -- with X; -- [2]
4653 -- package body A is ...
4655 -- The compilation of A's body installs the context clauses found at [2]
4656 -- and then the context clauses of its specification (found at [1]). As
4657 -- a consequence, at [1] the specification of X has been analyzed and it
4658 -- is immediately visible. According to the semantics of limited-with
4659 -- context clauses we don't install the limited view because the full
4660 -- view of X supersedes its limited view.
4662 if Analyzed (P_Unit)
4663 and then
4664 (Is_Immediately_Visible (P)
4665 or else (Is_Child_Package and then Is_Visible_Child_Unit (P)))
4666 then
4667 return;
4668 end if;
4670 if Debug_Flag_I then
4671 Write_Str ("install limited view of ");
4672 Write_Name (Chars (P));
4673 Write_Eol;
4674 end if;
4676 -- If the unit has not been analyzed and the limited view has not been
4677 -- already installed then we install it.
4679 if not Analyzed (P_Unit) then
4680 if not In_Chain (P) then
4682 -- Minimum decoration
4684 Set_Ekind (P, E_Package);
4685 Set_Etype (P, Standard_Void_Type);
4686 Set_Scope (P, Standard_Standard);
4688 if Is_Child_Package then
4689 Set_Is_Child_Unit (P);
4690 Set_Is_Visible_Child_Unit (P);
4691 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4692 end if;
4694 -- Place entity on visibility structure
4696 Set_Homonym (P, Current_Entity (P));
4697 Set_Current_Entity (P);
4699 if Debug_Flag_I then
4700 Write_Str (" (homonym) chain ");
4701 Write_Name (Chars (P));
4702 Write_Eol;
4703 end if;
4705 -- Install the incomplete view. The first element of the limited
4706 -- view is a header (an E_Package entity) used to reference the
4707 -- first shadow entity in the private part of the package.
4709 Lim_Header := Limited_View (P);
4710 Lim_Typ := First_Entity (Lim_Header);
4712 while Present (Lim_Typ)
4713 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4714 loop
4715 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4716 Set_Current_Entity (Lim_Typ);
4718 if Debug_Flag_I then
4719 Write_Str (" (homonym) chain ");
4720 Write_Name (Chars (Lim_Typ));
4721 Write_Eol;
4722 end if;
4724 Next_Entity (Lim_Typ);
4725 end loop;
4726 end if;
4728 -- If the unit appears in a previous regular with_clause, the regular
4729 -- entities of the public part of the withed package must be replaced
4730 -- by the shadow ones.
4732 -- This code must be kept synchronized with the code that replaces the
4733 -- shadow entities by the real entities (see body of Remove_Limited
4734 -- With_Clause); otherwise the contents of the homonym chains are not
4735 -- consistent.
4737 else
4738 -- Hide all the type entities of the public part of the package to
4739 -- avoid its usage. This is needed to cover all the subtype decla-
4740 -- rations because we do not remove them from the homonym chain.
4742 E := First_Entity (P);
4743 while Present (E) and then E /= First_Private_Entity (P) loop
4744 if Is_Type (E) then
4745 Set_Was_Hidden (E, Is_Hidden (E));
4746 Set_Is_Hidden (E);
4747 end if;
4749 Next_Entity (E);
4750 end loop;
4752 -- Replace the real entities by the shadow entities of the limited
4753 -- view. The first element of the limited view is a header that is
4754 -- used to reference the first shadow entity in the private part
4755 -- of the package. Successive elements are the limited views of the
4756 -- type (including regular incomplete types) declared in the package.
4758 Lim_Header := Limited_View (P);
4760 Lim_Typ := First_Entity (Lim_Header);
4761 while Present (Lim_Typ)
4762 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4763 loop
4764 pragma Assert (not In_Chain (Lim_Typ));
4766 -- Do not unchain nested packages and child units
4768 if Ekind (Lim_Typ) /= E_Package
4769 and then not Is_Child_Unit (Lim_Typ)
4770 then
4771 declare
4772 Prev : Entity_Id;
4774 begin
4775 Prev := Current_Entity (Lim_Typ);
4776 E := Prev;
4778 -- Replace E in the homonyms list, so that the limited
4779 -- view becomes available.
4781 if E = Non_Limited_View (Lim_Typ) then
4782 Set_Homonym (Lim_Typ, Homonym (Prev));
4783 Set_Current_Entity (Lim_Typ);
4785 else
4786 loop
4787 E := Homonym (Prev);
4789 -- E may have been removed when installing a
4790 -- previous limited_with_clause.
4792 exit when No (E);
4794 exit when E = Non_Limited_View (Lim_Typ);
4796 Prev := Homonym (Prev);
4797 end loop;
4799 if Present (E) then
4800 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4801 Set_Homonym (Prev, Lim_Typ);
4802 end if;
4803 end if;
4804 end;
4806 if Debug_Flag_I then
4807 Write_Str (" (homonym) chain ");
4808 Write_Name (Chars (Lim_Typ));
4809 Write_Eol;
4810 end if;
4811 end if;
4813 Next_Entity (Lim_Typ);
4814 end loop;
4815 end if;
4817 -- The package must be visible while the limited-with clause is active
4818 -- because references to the type P.T must resolve in the usual way.
4819 -- In addition, we remember that the limited-view has been installed to
4820 -- uninstall it at the point of context removal.
4822 Set_Is_Immediately_Visible (P);
4823 Set_Limited_View_Installed (N);
4825 -- If unit has not been analyzed in some previous context, check
4826 -- (imperfectly ???) whether it might need a body.
4828 if not Analyzed (P_Unit) then
4829 Check_Body_Required;
4830 end if;
4832 -- If the package in the limited_with clause is a child unit, the
4833 -- clause is unanalyzed and appears as a selected component. Recast
4834 -- it as an expanded name so that the entity can be properly set. Use
4835 -- entity of parent, if available, for higher ancestors in the name.
4837 if Nkind (Name (N)) = N_Selected_Component then
4838 declare
4839 Nam : Node_Id;
4840 Ent : Entity_Id;
4842 begin
4843 Nam := Name (N);
4844 Ent := P;
4845 while Nkind (Nam) = N_Selected_Component
4846 and then Present (Ent)
4847 loop
4848 Change_Selected_Component_To_Expanded_Name (Nam);
4850 -- Set entity of parent identifiers if the unit is a child
4851 -- unit. This ensures that the tree is properly formed from
4852 -- semantic point of view (e.g. for ASIS queries).
4854 Set_Entity (Nam, Ent);
4856 Nam := Prefix (Nam);
4857 Ent := Scope (Ent);
4859 -- Set entity of last ancestor
4861 if Nkind (Nam) = N_Identifier then
4862 Set_Entity (Nam, Ent);
4863 end if;
4864 end loop;
4865 end;
4866 end if;
4868 Set_Entity (Name (N), P);
4869 Set_From_With_Type (P);
4870 end Install_Limited_Withed_Unit;
4872 -------------------------
4873 -- Install_Withed_Unit --
4874 -------------------------
4876 procedure Install_Withed_Unit
4877 (With_Clause : Node_Id;
4878 Private_With_OK : Boolean := False)
4880 Uname : constant Entity_Id := Entity (Name (With_Clause));
4881 P : constant Entity_Id := Scope (Uname);
4883 begin
4884 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
4885 -- compiling a package declaration and the Private_With_OK flag was not
4886 -- set by the caller. These declarations will be installed later (before
4887 -- analyzing the private part of the package).
4889 if Private_Present (With_Clause)
4890 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
4891 and then not (Private_With_OK)
4892 then
4893 return;
4894 end if;
4896 if Debug_Flag_I then
4897 if Private_Present (With_Clause) then
4898 Write_Str ("install private withed unit ");
4899 else
4900 Write_Str ("install withed unit ");
4901 end if;
4903 Write_Name (Chars (Uname));
4904 Write_Eol;
4905 end if;
4907 -- We do not apply the restrictions to an internal unit unless we are
4908 -- compiling the internal unit as a main unit. This check is also
4909 -- skipped for dummy units (for missing packages).
4911 if Sloc (Uname) /= No_Location
4912 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
4913 or else Current_Sem_Unit = Main_Unit)
4914 then
4915 Check_Restricted_Unit
4916 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
4917 end if;
4919 if P /= Standard_Standard then
4921 -- If the unit is not analyzed after analysis of the with clause and
4922 -- it is an instantiation then it awaits a body and is the main unit.
4923 -- Its appearance in the context of some other unit indicates a
4924 -- circular dependency (DEC suite perversity).
4926 if not Analyzed (Uname)
4927 and then Nkind (Parent (Uname)) = N_Package_Instantiation
4928 then
4929 Error_Msg_N
4930 ("instantiation depends on itself", Name (With_Clause));
4932 elsif not Is_Visible_Child_Unit (Uname) then
4933 Set_Is_Visible_Child_Unit (Uname);
4935 -- If the child unit appears in the context of its parent, it is
4936 -- immediately visible.
4938 if In_Open_Scopes (Scope (Uname)) then
4939 Set_Is_Immediately_Visible (Uname);
4940 end if;
4942 if Is_Generic_Instance (Uname)
4943 and then Ekind (Uname) in Subprogram_Kind
4944 then
4945 -- Set flag as well on the visible entity that denotes the
4946 -- instance, which renames the current one.
4948 Set_Is_Visible_Child_Unit
4949 (Related_Instance
4950 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
4951 end if;
4953 -- The parent unit may have been installed already, and may have
4954 -- appeared in a use clause.
4956 if In_Use (Scope (Uname)) then
4957 Set_Is_Potentially_Use_Visible (Uname);
4958 end if;
4960 Set_Context_Installed (With_Clause);
4961 end if;
4963 elsif not Is_Immediately_Visible (Uname) then
4964 if not Private_Present (With_Clause)
4965 or else Private_With_OK
4966 then
4967 Set_Is_Immediately_Visible (Uname);
4968 end if;
4970 Set_Context_Installed (With_Clause);
4971 end if;
4973 -- A with-clause overrides a with-type clause: there are no restric-
4974 -- tions on the use of package entities.
4976 if Ekind (Uname) = E_Package then
4977 Set_From_With_Type (Uname, False);
4978 end if;
4980 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
4981 -- unit if there is a visible homograph for it declared in the same
4982 -- declarative region. This pathological case can only arise when an
4983 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
4984 -- G1 has a generic child also named G2, and the context includes with_
4985 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
4986 -- of I1.G2 visible as well. If the child unit is named Standard, do
4987 -- not apply the check to the Standard package itself.
4989 if Is_Child_Unit (Uname)
4990 and then Is_Visible_Child_Unit (Uname)
4991 and then Ada_Version >= Ada_05
4992 then
4993 declare
4994 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
4995 Decl2 : Node_Id;
4996 P2 : Entity_Id;
4997 U2 : Entity_Id;
4999 begin
5000 U2 := Homonym (Uname);
5001 while Present (U2)
5002 and then U2 /= Standard_Standard
5003 loop
5004 P2 := Scope (U2);
5005 Decl2 := Unit_Declaration_Node (P2);
5007 if Is_Child_Unit (U2)
5008 and then Is_Visible_Child_Unit (U2)
5009 then
5010 if Is_Generic_Instance (P)
5011 and then Nkind (Decl1) = N_Package_Declaration
5012 and then Generic_Parent (Specification (Decl1)) = P2
5013 then
5014 Error_Msg_N ("illegal with_clause", With_Clause);
5015 Error_Msg_N
5016 ("\child unit has visible homograph" &
5017 " (RM 8.3(26), 10.1.1(19))",
5018 With_Clause);
5019 exit;
5021 elsif Is_Generic_Instance (P2)
5022 and then Nkind (Decl2) = N_Package_Declaration
5023 and then Generic_Parent (Specification (Decl2)) = P
5024 then
5025 -- With_clause for child unit of instance appears before
5026 -- in the context. We want to place the error message on
5027 -- it, not on the generic child unit itself.
5029 declare
5030 Prev_Clause : Node_Id;
5032 begin
5033 Prev_Clause := First (List_Containing (With_Clause));
5034 while Entity (Name (Prev_Clause)) /= U2 loop
5035 Next (Prev_Clause);
5036 end loop;
5038 pragma Assert (Present (Prev_Clause));
5039 Error_Msg_N ("illegal with_clause", Prev_Clause);
5040 Error_Msg_N
5041 ("\child unit has visible homograph" &
5042 " (RM 8.3(26), 10.1.1(19))",
5043 Prev_Clause);
5044 exit;
5045 end;
5046 end if;
5047 end if;
5049 U2 := Homonym (U2);
5050 end loop;
5051 end;
5052 end if;
5053 end Install_Withed_Unit;
5055 -------------------
5056 -- Is_Child_Spec --
5057 -------------------
5059 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5060 K : constant Node_Kind := Nkind (Lib_Unit);
5062 begin
5063 return (K in N_Generic_Declaration or else
5064 K in N_Generic_Instantiation or else
5065 K in N_Generic_Renaming_Declaration or else
5066 K = N_Package_Declaration or else
5067 K = N_Package_Renaming_Declaration or else
5068 K = N_Subprogram_Declaration or else
5069 K = N_Subprogram_Renaming_Declaration)
5070 and then Present (Parent_Spec (Lib_Unit));
5071 end Is_Child_Spec;
5073 ------------------------------------
5074 -- Is_Legal_Shadow_Entity_In_Body --
5075 ------------------------------------
5077 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5078 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5079 begin
5080 return Nkind (Unit (C_Unit)) = N_Package_Body
5081 and then
5082 Has_With_Clause
5083 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5084 end Is_Legal_Shadow_Entity_In_Body;
5086 -----------------------
5087 -- Load_Needed_Body --
5088 -----------------------
5090 -- N is a generic unit named in a with clause, or else it is a unit that
5091 -- contains a generic unit or an inlined function. In order to perform an
5092 -- instantiation, the body of the unit must be present. If the unit itself
5093 -- is generic, we assume that an instantiation follows, and load & analyze
5094 -- the body unconditionally. This forces analysis of the spec as well.
5096 -- If the unit is not generic, but contains a generic unit, it is loaded on
5097 -- demand, at the point of instantiation (see ch12).
5099 procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
5100 Body_Name : Unit_Name_Type;
5101 Unum : Unit_Number_Type;
5103 Save_Style_Check : constant Boolean := Opt.Style_Check;
5104 -- The loading and analysis is done with style checks off
5106 begin
5107 if not GNAT_Mode then
5108 Style_Check := False;
5109 end if;
5111 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5112 Unum :=
5113 Load_Unit
5114 (Load_Name => Body_Name,
5115 Required => False,
5116 Subunit => False,
5117 Error_Node => N,
5118 Renamings => True);
5120 if Unum = No_Unit then
5121 OK := False;
5123 else
5124 Compiler_State := Analyzing; -- reset after load
5126 if not Fatal_Error (Unum) or else Try_Semantics then
5127 if Debug_Flag_L then
5128 Write_Str ("*** Loaded generic body");
5129 Write_Eol;
5130 end if;
5132 Semantics (Cunit (Unum));
5133 end if;
5135 OK := True;
5136 end if;
5138 Style_Check := Save_Style_Check;
5139 end Load_Needed_Body;
5141 -------------------------
5142 -- Build_Limited_Views --
5143 -------------------------
5145 procedure Build_Limited_Views (N : Node_Id) is
5146 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
5147 P : constant Entity_Id := Cunit_Entity (Unum);
5149 Spec : Node_Id; -- To denote a package specification
5150 Lim_Typ : Entity_Id; -- To denote shadow entities
5151 Comp_Typ : Entity_Id; -- To denote real entities
5153 Lim_Header : Entity_Id; -- Package entity
5154 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
5155 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
5157 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id);
5158 -- Add attributes of an incomplete type to a shadow entity. The same
5159 -- attributes are placed on the real entity, so that gigi receives
5160 -- a consistent view.
5162 procedure Decorate_Package_Specification (P : Entity_Id);
5163 -- Add attributes of a package entity to the entity in a package
5164 -- declaration
5166 procedure Decorate_Tagged_Type
5167 (Loc : Source_Ptr;
5168 T : Entity_Id;
5169 Scop : Entity_Id);
5170 -- Set basic attributes of tagged type T, including its class_wide type.
5171 -- The parameters Loc, Scope are used to decorate the class_wide type.
5173 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id);
5174 -- Construct list of shadow entities and attach it to entity of
5175 -- package that is mentioned in a limited_with clause.
5177 function New_Internal_Shadow_Entity
5178 (Kind : Entity_Kind;
5179 Sloc_Value : Source_Ptr;
5180 Id_Char : Character) return Entity_Id;
5181 -- Build a new internal entity and append it to the list of shadow
5182 -- entities available through the limited-header
5184 -----------------
5185 -- Build_Chain --
5186 -----------------
5188 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id) is
5189 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
5190 Is_Tagged : Boolean;
5191 Decl : Node_Id;
5193 begin
5194 Decl := First_Decl;
5195 while Present (Decl) loop
5197 -- For each library_package_declaration in the environment, there
5198 -- is an implicit declaration of a *limited view* of that library
5199 -- package. The limited view of a package contains:
5201 -- * For each nested package_declaration, a declaration of the
5202 -- limited view of that package, with the same defining-
5203 -- program-unit name.
5205 -- * For each type_declaration in the visible part, an incomplete
5206 -- type-declaration with the same defining_identifier, whose
5207 -- completion is the type_declaration. If the type_declaration
5208 -- is tagged, then the incomplete_type_declaration is tagged
5209 -- incomplete.
5211 -- The partial view is tagged if the declaration has the
5212 -- explicit keyword, or else if it is a type extension, both
5213 -- of which can be ascertained syntactically.
5215 if Nkind (Decl) = N_Full_Type_Declaration then
5216 Is_Tagged :=
5217 (Nkind (Type_Definition (Decl)) = N_Record_Definition
5218 and then Tagged_Present (Type_Definition (Decl)))
5219 or else
5220 (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
5221 and then
5222 Present
5223 (Record_Extension_Part (Type_Definition (Decl))));
5225 Comp_Typ := Defining_Identifier (Decl);
5227 if not Analyzed_Unit then
5228 if Is_Tagged then
5229 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5230 else
5231 Decorate_Incomplete_Type (Comp_Typ, Scope);
5232 end if;
5233 end if;
5235 -- Create shadow entity for type
5237 Lim_Typ :=
5238 New_Internal_Shadow_Entity
5239 (Kind => Ekind (Comp_Typ),
5240 Sloc_Value => Sloc (Comp_Typ),
5241 Id_Char => 'Z');
5243 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5244 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5245 Set_From_With_Type (Lim_Typ);
5247 if Is_Tagged then
5248 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5249 else
5250 Decorate_Incomplete_Type (Lim_Typ, Scope);
5251 end if;
5253 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5255 elsif Nkind_In (Decl, N_Private_Type_Declaration,
5256 N_Incomplete_Type_Declaration,
5257 N_Task_Type_Declaration,
5258 N_Protected_Type_Declaration)
5259 then
5260 Comp_Typ := Defining_Identifier (Decl);
5262 Is_Tagged :=
5263 Nkind_In (Decl, N_Private_Type_Declaration,
5264 N_Incomplete_Type_Declaration)
5265 and then Tagged_Present (Decl);
5267 if not Analyzed_Unit then
5268 if Is_Tagged then
5269 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5270 else
5271 Decorate_Incomplete_Type (Comp_Typ, Scope);
5272 end if;
5273 end if;
5275 Lim_Typ :=
5276 New_Internal_Shadow_Entity
5277 (Kind => Ekind (Comp_Typ),
5278 Sloc_Value => Sloc (Comp_Typ),
5279 Id_Char => 'Z');
5281 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5282 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5283 Set_From_With_Type (Lim_Typ);
5285 if Is_Tagged then
5286 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5287 else
5288 Decorate_Incomplete_Type (Lim_Typ, Scope);
5289 end if;
5291 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5293 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5294 Comp_Typ := Defining_Identifier (Decl);
5296 if not Analyzed_Unit then
5297 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5298 end if;
5300 -- Create shadow entity for type
5302 Lim_Typ :=
5303 New_Internal_Shadow_Entity
5304 (Kind => Ekind (Comp_Typ),
5305 Sloc_Value => Sloc (Comp_Typ),
5306 Id_Char => 'Z');
5308 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5309 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5310 Set_From_With_Type (Lim_Typ);
5312 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5313 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5315 elsif Nkind (Decl) = N_Package_Declaration then
5317 -- Local package
5319 declare
5320 Spec : constant Node_Id := Specification (Decl);
5322 begin
5323 Comp_Typ := Defining_Unit_Name (Spec);
5325 if not Analyzed (Cunit (Unum)) then
5326 Decorate_Package_Specification (Comp_Typ);
5327 Set_Scope (Comp_Typ, Scope);
5328 end if;
5330 Lim_Typ :=
5331 New_Internal_Shadow_Entity
5332 (Kind => Ekind (Comp_Typ),
5333 Sloc_Value => Sloc (Comp_Typ),
5334 Id_Char => 'Z');
5336 Decorate_Package_Specification (Lim_Typ);
5337 Set_Scope (Lim_Typ, Scope);
5339 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5340 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5341 Set_From_With_Type (Lim_Typ);
5343 -- Note: The non_limited_view attribute is not used
5344 -- for local packages.
5346 Build_Chain
5347 (Scope => Lim_Typ,
5348 First_Decl => First (Visible_Declarations (Spec)));
5349 end;
5350 end if;
5352 Next (Decl);
5353 end loop;
5354 end Build_Chain;
5356 ------------------------------
5357 -- Decorate_Incomplete_Type --
5358 ------------------------------
5360 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id) is
5361 begin
5362 Set_Ekind (E, E_Incomplete_Type);
5363 Set_Scope (E, Scop);
5364 Set_Etype (E, E);
5365 Set_Is_First_Subtype (E, True);
5366 Set_Stored_Constraint (E, No_Elist);
5367 Set_Full_View (E, Empty);
5368 Init_Size_Align (E);
5369 end Decorate_Incomplete_Type;
5371 --------------------------
5372 -- Decorate_Tagged_Type --
5373 --------------------------
5375 procedure Decorate_Tagged_Type
5376 (Loc : Source_Ptr;
5377 T : Entity_Id;
5378 Scop : Entity_Id)
5380 CW : Entity_Id;
5382 begin
5383 Decorate_Incomplete_Type (T, Scop);
5384 Set_Is_Tagged_Type (T);
5386 -- Build corresponding class_wide type, if not previously done
5388 -- Note: The class-wide entity is shared by the limited-view
5389 -- and the full-view.
5391 if No (Class_Wide_Type (T)) then
5392 CW := Make_Temporary (Loc, 'S');
5394 -- Set parent to be the same as the parent of the tagged type.
5395 -- We need a parent field set, and it is supposed to point to
5396 -- the declaration of the type. The tagged type declaration
5397 -- essentially declares two separate types, the tagged type
5398 -- itself and the corresponding class-wide type, so it is
5399 -- reasonable for the parent fields to point to the declaration
5400 -- in both cases.
5402 Set_Parent (CW, Parent (T));
5404 -- Set remaining fields of classwide type
5406 Set_Ekind (CW, E_Class_Wide_Type);
5407 Set_Etype (CW, T);
5408 Set_Scope (CW, Scop);
5409 Set_Is_Tagged_Type (CW);
5410 Set_Is_First_Subtype (CW, True);
5411 Init_Size_Align (CW);
5412 Set_Has_Unknown_Discriminants (CW, True);
5413 Set_Class_Wide_Type (CW, CW);
5414 Set_Equivalent_Type (CW, Empty);
5415 Set_From_With_Type (CW, From_With_Type (T));
5417 -- Link type to its class-wide type
5419 Set_Class_Wide_Type (T, CW);
5420 end if;
5421 end Decorate_Tagged_Type;
5423 ------------------------------------
5424 -- Decorate_Package_Specification --
5425 ------------------------------------
5427 procedure Decorate_Package_Specification (P : Entity_Id) is
5428 begin
5429 -- Place only the most basic attributes
5431 Set_Ekind (P, E_Package);
5432 Set_Etype (P, Standard_Void_Type);
5433 end Decorate_Package_Specification;
5435 --------------------------------
5436 -- New_Internal_Shadow_Entity --
5437 --------------------------------
5439 function New_Internal_Shadow_Entity
5440 (Kind : Entity_Kind;
5441 Sloc_Value : Source_Ptr;
5442 Id_Char : Character) return Entity_Id
5444 E : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
5446 begin
5447 Set_Ekind (E, Kind);
5448 Set_Is_Internal (E, True);
5450 if Kind in Type_Kind then
5451 Init_Size_Align (E);
5452 end if;
5454 Append_Entity (E, Lim_Header);
5455 Last_Lim_E := E;
5456 return E;
5457 end New_Internal_Shadow_Entity;
5459 -- Start of processing for Build_Limited_Views
5461 begin
5462 pragma Assert (Limited_Present (N));
5464 -- A library_item mentioned in a limited_with_clause is a package
5465 -- declaration, not a subprogram declaration, generic declaration,
5466 -- generic instantiation, or package renaming declaration.
5468 case Nkind (Unit (Library_Unit (N))) is
5469 when N_Package_Declaration =>
5470 null;
5472 when N_Subprogram_Declaration =>
5473 Error_Msg_N ("subprograms not allowed in "
5474 & "limited with_clauses", N);
5475 return;
5477 when N_Generic_Package_Declaration |
5478 N_Generic_Subprogram_Declaration =>
5479 Error_Msg_N ("generics not allowed in "
5480 & "limited with_clauses", N);
5481 return;
5483 when N_Generic_Instantiation =>
5484 Error_Msg_N ("generic instantiations not allowed in "
5485 & "limited with_clauses", N);
5486 return;
5488 when N_Generic_Renaming_Declaration =>
5489 Error_Msg_N ("generic renamings not allowed in "
5490 & "limited with_clauses", N);
5491 return;
5493 when N_Subprogram_Renaming_Declaration =>
5494 Error_Msg_N ("renamed subprograms not allowed in "
5495 & "limited with_clauses", N);
5496 return;
5498 when N_Package_Renaming_Declaration =>
5499 Error_Msg_N ("renamed packages not allowed in "
5500 & "limited with_clauses", N);
5501 return;
5503 when others =>
5504 raise Program_Error;
5505 end case;
5507 -- Check if the chain is already built
5509 Spec := Specification (Unit (Library_Unit (N)));
5511 if Limited_View_Installed (Spec) then
5512 return;
5513 end if;
5515 Set_Ekind (P, E_Package);
5517 -- Build the header of the limited_view
5519 Lim_Header := Make_Temporary (Sloc (N), 'Z');
5520 Set_Ekind (Lim_Header, E_Package);
5521 Set_Is_Internal (Lim_Header);
5522 Set_Limited_View (P, Lim_Header);
5524 -- Create the auxiliary chain. All the shadow entities are appended to
5525 -- the list of entities of the limited-view header
5527 Build_Chain
5528 (Scope => P,
5529 First_Decl => First (Visible_Declarations (Spec)));
5531 -- Save the last built shadow entity. It is needed later to set the
5532 -- reference to the first shadow entity in the private part
5534 Last_Pub_Lim_E := Last_Lim_E;
5536 -- Ada 2005 (AI-262): Add the limited view of the private declarations
5537 -- Required to give support to limited-private-with clauses
5539 Build_Chain (Scope => P,
5540 First_Decl => First (Private_Declarations (Spec)));
5542 if Last_Pub_Lim_E /= Empty then
5543 Set_First_Private_Entity
5544 (Lim_Header, Next_Entity (Last_Pub_Lim_E));
5545 else
5546 Set_First_Private_Entity
5547 (Lim_Header, First_Entity (P));
5548 end if;
5550 Set_Limited_View_Installed (Spec);
5551 end Build_Limited_Views;
5553 -------------------------------
5554 -- Check_Body_Needed_For_SAL --
5555 -------------------------------
5557 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5559 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5560 -- Determine whether use of entity E might require the presence of its
5561 -- body. For a package this requires a recursive traversal of all nested
5562 -- declarations.
5564 ---------------------------
5565 -- Entity_Needed_For_SAL --
5566 ---------------------------
5568 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5569 Ent : Entity_Id;
5571 begin
5572 if Is_Subprogram (E)
5573 and then Has_Pragma_Inline (E)
5574 then
5575 return True;
5577 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
5578 return True;
5580 elsif Ekind (E) = E_Generic_Package
5581 and then
5582 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5583 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5584 then
5585 return True;
5587 elsif Ekind (E) = E_Package
5588 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5589 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5590 then
5591 Ent := First_Entity (E);
5592 while Present (Ent) loop
5593 if Entity_Needs_Body (Ent) then
5594 return True;
5595 end if;
5597 Next_Entity (Ent);
5598 end loop;
5600 return False;
5602 else
5603 return False;
5604 end if;
5605 end Entity_Needs_Body;
5607 -- Start of processing for Check_Body_Needed_For_SAL
5609 begin
5610 if Ekind (Unit_Name) = E_Generic_Package
5611 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5612 N_Generic_Package_Declaration
5613 and then
5614 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5615 then
5616 Set_Body_Needed_For_SAL (Unit_Name);
5618 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
5619 Set_Body_Needed_For_SAL (Unit_Name);
5621 elsif Is_Subprogram (Unit_Name)
5622 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5623 N_Subprogram_Declaration
5624 and then Has_Pragma_Inline (Unit_Name)
5625 then
5626 Set_Body_Needed_For_SAL (Unit_Name);
5628 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5629 Check_Body_Needed_For_SAL
5630 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5632 elsif Ekind (Unit_Name) = E_Package
5633 and then Entity_Needs_Body (Unit_Name)
5634 then
5635 Set_Body_Needed_For_SAL (Unit_Name);
5637 elsif Ekind (Unit_Name) = E_Package_Body
5638 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5639 then
5640 Check_Body_Needed_For_SAL
5641 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5642 end if;
5643 end Check_Body_Needed_For_SAL;
5645 --------------------
5646 -- Remove_Context --
5647 --------------------
5649 procedure Remove_Context (N : Node_Id) is
5650 Lib_Unit : constant Node_Id := Unit (N);
5652 begin
5653 -- If this is a child unit, first remove the parent units
5655 if Is_Child_Spec (Lib_Unit) then
5656 Remove_Parents (Lib_Unit);
5657 end if;
5659 Remove_Context_Clauses (N);
5660 end Remove_Context;
5662 ----------------------------
5663 -- Remove_Context_Clauses --
5664 ----------------------------
5666 procedure Remove_Context_Clauses (N : Node_Id) is
5667 Item : Node_Id;
5668 Unit_Name : Entity_Id;
5670 begin
5671 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5672 -- limited-views first and regular-views later (to maintain the
5673 -- stack model).
5675 -- First Phase: Remove limited_with context clauses
5677 Item := First (Context_Items (N));
5678 while Present (Item) loop
5680 -- We are interested only in with clauses which got installed
5681 -- on entry.
5683 if Nkind (Item) = N_With_Clause
5684 and then Limited_Present (Item)
5685 and then Limited_View_Installed (Item)
5686 then
5687 Remove_Limited_With_Clause (Item);
5688 end if;
5690 Next (Item);
5691 end loop;
5693 -- Second Phase: Loop through context items and undo regular
5694 -- with_clauses and use_clauses.
5696 Item := First (Context_Items (N));
5697 while Present (Item) loop
5699 -- We are interested only in with clauses which got installed on
5700 -- entry, as indicated by their Context_Installed flag set
5702 if Nkind (Item) = N_With_Clause
5703 and then Limited_Present (Item)
5704 and then Limited_View_Installed (Item)
5705 then
5706 null;
5708 elsif Nkind (Item) = N_With_Clause
5709 and then Context_Installed (Item)
5710 then
5711 -- Remove items from one with'ed unit
5713 Unit_Name := Entity (Name (Item));
5714 Remove_Unit_From_Visibility (Unit_Name);
5715 Set_Context_Installed (Item, False);
5717 elsif Nkind (Item) = N_Use_Package_Clause then
5718 End_Use_Package (Item);
5720 elsif Nkind (Item) = N_Use_Type_Clause then
5721 End_Use_Type (Item);
5722 end if;
5724 Next (Item);
5725 end loop;
5726 end Remove_Context_Clauses;
5728 --------------------------------
5729 -- Remove_Limited_With_Clause --
5730 --------------------------------
5732 procedure Remove_Limited_With_Clause (N : Node_Id) is
5733 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5734 E : Entity_Id;
5735 P : Entity_Id;
5736 Lim_Header : Entity_Id;
5737 Lim_Typ : Entity_Id;
5738 Prev : Entity_Id;
5740 begin
5741 pragma Assert (Limited_View_Installed (N));
5743 -- In case of limited with_clause on subprograms, generics, instances,
5744 -- or renamings, the corresponding error was previously posted and we
5745 -- have nothing to do here.
5747 if Nkind (P_Unit) /= N_Package_Declaration then
5748 return;
5749 end if;
5751 P := Defining_Unit_Name (Specification (P_Unit));
5753 -- Handle child packages
5755 if Nkind (P) = N_Defining_Program_Unit_Name then
5756 P := Defining_Identifier (P);
5757 end if;
5759 if Debug_Flag_I then
5760 Write_Str ("remove limited view of ");
5761 Write_Name (Chars (P));
5762 Write_Str (" from visibility");
5763 Write_Eol;
5764 end if;
5766 -- Prepare the removal of the shadow entities from visibility. The
5767 -- first element of the limited view is a header (an E_Package
5768 -- entity) that is used to reference the first shadow entity in the
5769 -- private part of the package
5771 Lim_Header := Limited_View (P);
5772 Lim_Typ := First_Entity (Lim_Header);
5774 -- Remove package and shadow entities from visibility if it has not
5775 -- been analyzed
5777 if not Analyzed (P_Unit) then
5778 Unchain (P);
5779 Set_Is_Immediately_Visible (P, False);
5781 while Present (Lim_Typ) loop
5782 Unchain (Lim_Typ);
5783 Next_Entity (Lim_Typ);
5784 end loop;
5786 -- Otherwise this package has already appeared in the closure and its
5787 -- shadow entities must be replaced by its real entities. This code
5788 -- must be kept synchronized with the complementary code in Install
5789 -- Limited_Withed_Unit.
5791 else
5792 -- Real entities that are type or subtype declarations were hidden
5793 -- from visibility at the point of installation of the limited-view.
5794 -- Now we recover the previous value of the hidden attribute.
5796 E := First_Entity (P);
5797 while Present (E) and then E /= First_Private_Entity (P) loop
5798 if Is_Type (E) then
5799 Set_Is_Hidden (E, Was_Hidden (E));
5800 end if;
5802 Next_Entity (E);
5803 end loop;
5805 while Present (Lim_Typ)
5806 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5807 loop
5808 -- Nested packages and child units were not unchained
5810 if Ekind (Lim_Typ) /= E_Package
5811 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5812 then
5813 -- If the package has incomplete types, the limited view of the
5814 -- incomplete type is in fact never visible (AI05-129) but we
5815 -- have created a shadow entity E1 for it, that points to E2,
5816 -- a non-limited incomplete type. This in turn has a full view
5817 -- E3 that is the full declaration. There is a corresponding
5818 -- shadow entity E4. When reinstalling the non-limited view,
5819 -- E2 must become the current entity and E3 must be ignored.
5821 E := Non_Limited_View (Lim_Typ);
5823 if Present (Current_Entity (E))
5824 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
5825 and then Full_View (Current_Entity (E)) = E
5826 then
5828 -- Lim_Typ is the limited view of a full type declaration
5829 -- that has a previous incomplete declaration, i.e. E3 from
5830 -- the previous description. Nothing to insert.
5832 null;
5834 else
5835 pragma Assert (not In_Chain (E));
5837 Prev := Current_Entity (Lim_Typ);
5839 if Prev = Lim_Typ then
5840 Set_Current_Entity (E);
5842 else
5843 while Present (Prev)
5844 and then Homonym (Prev) /= Lim_Typ
5845 loop
5846 Prev := Homonym (Prev);
5847 end loop;
5849 if Present (Prev) then
5850 Set_Homonym (Prev, E);
5851 end if;
5852 end if;
5854 -- Preserve structure of homonym chain
5856 Set_Homonym (E, Homonym (Lim_Typ));
5857 end if;
5858 end if;
5860 Next_Entity (Lim_Typ);
5861 end loop;
5862 end if;
5864 -- Indicate that the limited view of the package is not installed
5866 Set_From_With_Type (P, False);
5867 Set_Limited_View_Installed (N, False);
5868 end Remove_Limited_With_Clause;
5870 --------------------
5871 -- Remove_Parents --
5872 --------------------
5874 procedure Remove_Parents (Lib_Unit : Node_Id) is
5875 P : Node_Id;
5876 P_Name : Entity_Id;
5877 P_Spec : Node_Id := Empty;
5878 E : Entity_Id;
5879 Vis : constant Boolean :=
5880 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
5882 begin
5883 if Is_Child_Spec (Lib_Unit) then
5884 P_Spec := Parent_Spec (Lib_Unit);
5886 elsif Nkind (Lib_Unit) = N_Package_Body
5887 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
5888 then
5889 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
5890 end if;
5892 if Present (P_Spec) then
5893 P := Unit (P_Spec);
5894 P_Name := Get_Parent_Entity (P);
5895 Remove_Context_Clauses (P_Spec);
5896 End_Package_Scope (P_Name);
5897 Set_Is_Immediately_Visible (P_Name, Vis);
5899 -- Remove from visibility the siblings as well, which are directly
5900 -- visible while the parent is in scope.
5902 E := First_Entity (P_Name);
5903 while Present (E) loop
5904 if Is_Child_Unit (E) then
5905 Set_Is_Immediately_Visible (E, False);
5906 end if;
5908 Next_Entity (E);
5909 end loop;
5911 Set_In_Package_Body (P_Name, False);
5913 -- This is the recursive call to remove the context of any higher
5914 -- level parent. This recursion ensures that all parents are removed
5915 -- in the reverse order of their installation.
5917 Remove_Parents (P);
5918 end if;
5919 end Remove_Parents;
5921 ---------------------------------
5922 -- Remove_Private_With_Clauses --
5923 ---------------------------------
5925 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
5926 Item : Node_Id;
5928 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
5929 -- Check whether a given unit appears in a regular with_clause. Used to
5930 -- determine whether a private_with_clause, implicit or explicit, should
5931 -- be ignored.
5933 ----------------------------
5934 -- In_Regular_With_Clause --
5935 ----------------------------
5937 function In_Regular_With_Clause (E : Entity_Id) return Boolean
5939 Item : Node_Id;
5941 begin
5942 Item := First (Context_Items (Comp_Unit));
5943 while Present (Item) loop
5944 if Nkind (Item) = N_With_Clause
5945 and then Entity (Name (Item)) = E
5946 and then not Private_Present (Item)
5947 then
5948 return True;
5949 end if;
5950 Next (Item);
5951 end loop;
5953 return False;
5954 end In_Regular_With_Clause;
5956 -- Start of processing for Remove_Private_With_Clauses
5958 begin
5959 Item := First (Context_Items (Comp_Unit));
5960 while Present (Item) loop
5961 if Nkind (Item) = N_With_Clause
5962 and then Private_Present (Item)
5963 then
5964 -- If private_with_clause is redundant, remove it from context,
5965 -- as a small optimization to subsequent handling of private_with
5966 -- clauses in other nested packages.
5968 if In_Regular_With_Clause (Entity (Name (Item))) then
5969 declare
5970 Nxt : constant Node_Id := Next (Item);
5971 begin
5972 Remove (Item);
5973 Item := Nxt;
5974 end;
5976 elsif Limited_Present (Item) then
5977 if not Limited_View_Installed (Item) then
5978 Remove_Limited_With_Clause (Item);
5979 end if;
5981 Next (Item);
5983 else
5984 Remove_Unit_From_Visibility (Entity (Name (Item)));
5985 Set_Context_Installed (Item, False);
5986 Next (Item);
5987 end if;
5989 else
5990 Next (Item);
5991 end if;
5992 end loop;
5993 end Remove_Private_With_Clauses;
5995 ---------------------------------
5996 -- Remove_Unit_From_Visibility --
5997 ---------------------------------
5999 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6000 P : constant Entity_Id := Scope (Unit_Name);
6002 begin
6003 if Debug_Flag_I then
6004 Write_Str ("remove unit ");
6005 Write_Name (Chars (Unit_Name));
6006 Write_Str (" from visibility");
6007 Write_Eol;
6008 end if;
6010 if P /= Standard_Standard then
6011 Set_Is_Visible_Child_Unit (Unit_Name, False);
6012 end if;
6014 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6015 Set_Is_Immediately_Visible (Unit_Name, False);
6016 end Remove_Unit_From_Visibility;
6018 --------
6019 -- sm --
6020 --------
6022 procedure sm is
6023 begin
6024 null;
6025 end sm;
6027 -------------
6028 -- Unchain --
6029 -------------
6031 procedure Unchain (E : Entity_Id) is
6032 Prev : Entity_Id;
6034 begin
6035 Prev := Current_Entity (E);
6037 if No (Prev) then
6038 return;
6040 elsif Prev = E then
6041 Set_Name_Entity_Id (Chars (E), Homonym (E));
6043 else
6044 while Present (Prev)
6045 and then Homonym (Prev) /= E
6046 loop
6047 Prev := Homonym (Prev);
6048 end loop;
6050 if Present (Prev) then
6051 Set_Homonym (Prev, Homonym (E));
6052 end if;
6053 end if;
6055 if Debug_Flag_I then
6056 Write_Str (" (homonym) unchain ");
6057 Write_Name (Chars (E));
6058 Write_Eol;
6059 end if;
6060 end Unchain;
6062 end Sem_Ch10;