Fix unused warnings.
[official-gcc/graphite-test-results.git] / gcc / ada / sem_ch10.adb
blob89dda5da36d901a4f3dadbc0039671a32e92cd96
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 unit
90 -- 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 all 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 main
191 -- 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 name
199 -- is established, load and analyze. Nam is the non-overloadable entity
200 -- for which the proper body provides a completion. Subprogram stubs are
201 -- 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 types
223 -- that are in one-one correspondence with the real types in the package,
224 -- 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 the
276 -- name entity of Clause with any list element. If the match occurs
277 -- on a use package clause set Used to True, for a use type clause or
278 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
280 procedure Process_Spec_Clauses
281 (Context_List : List_Id;
282 Clause : Node_Id;
283 Used : in out Boolean;
284 Withed : in out Boolean;
285 Exit_On_Self : Boolean := False);
286 -- Examine the context clauses of a package spec, trying to match
287 -- the name entity of Clause with any list element. If the match
288 -- occurs on a use package clause, set Used to True, for a with
289 -- package clause other than Clause, set Withed to True. Limited
290 -- with clauses, implicitly generated with clauses and withs
291 -- having pragmas Elaborate or Elaborate_All applied to them are
292 -- skipped. Exit_On_Self is used to control the search loop and
293 -- force an exit whenever Clause sees itself in the search.
295 --------------------------
296 -- Process_Body_Clauses --
297 --------------------------
299 procedure Process_Body_Clauses
300 (Context_List : List_Id;
301 Clause : Node_Id;
302 Used : in out Boolean;
303 Used_Type_Or_Elab : in out Boolean)
305 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
306 Cont_Item : Node_Id;
307 Prag_Unit : Node_Id;
308 Subt_Mark : Node_Id;
309 Use_Item : Node_Id;
311 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
312 -- In an expanded name in a use clause, if the prefix is a renamed
313 -- package, the entity is set to the original package as a result,
314 -- when checking whether the package appears in a previous with
315 -- clause, the renaming has to be taken into account, to prevent
316 -- spurious/incorrect warnings. A common case is use of Text_IO.
318 ---------------
319 -- Same_Unit --
320 ---------------
322 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
323 begin
324 return Entity (N) = P
325 or else
326 (Present (Renamed_Object (P))
327 and then Entity (N) = Renamed_Object (P));
328 end Same_Unit;
330 -- Start of processing for Process_Body_Clauses
332 begin
333 Used := False;
334 Used_Type_Or_Elab := False;
336 Cont_Item := First (Context_List);
337 while Present (Cont_Item) loop
339 -- Package use clause
341 if Nkind (Cont_Item) = N_Use_Package_Clause
342 and then not Used
343 then
344 -- Search through use clauses
346 Use_Item := First (Names (Cont_Item));
347 while Present (Use_Item) and then not Used loop
349 -- Case of a direct use of the one we are looking for
351 if Entity (Use_Item) = Nam_Ent then
352 Used := True;
354 -- Handle nested case, as in "with P; use P.Q.R"
356 else
357 declare
358 UE : Node_Id;
360 begin
361 -- Loop through prefixes looking for match
363 UE := Use_Item;
364 while Nkind (UE) = N_Expanded_Name loop
365 if Same_Unit (Prefix (UE), Nam_Ent) then
366 Used := True;
367 exit;
368 end if;
370 UE := Prefix (UE);
371 end loop;
372 end;
373 end if;
375 Next (Use_Item);
376 end loop;
378 -- USE TYPE clause
380 elsif Nkind (Cont_Item) = N_Use_Type_Clause
381 and then not Used_Type_Or_Elab
382 then
383 Subt_Mark := First (Subtype_Marks (Cont_Item));
384 while Present (Subt_Mark)
385 and then not Used_Type_Or_Elab
386 loop
387 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
388 Used_Type_Or_Elab := True;
389 end if;
391 Next (Subt_Mark);
392 end loop;
394 -- Pragma Elaborate or Elaborate_All
396 elsif Nkind (Cont_Item) = N_Pragma
397 and then
398 (Pragma_Name (Cont_Item) = Name_Elaborate
399 or else
400 Pragma_Name (Cont_Item) = Name_Elaborate_All)
401 and then not Used_Type_Or_Elab
402 then
403 Prag_Unit :=
404 First (Pragma_Argument_Associations (Cont_Item));
405 while Present (Prag_Unit)
406 and then not Used_Type_Or_Elab
407 loop
408 if Entity (Expression (Prag_Unit)) = Nam_Ent then
409 Used_Type_Or_Elab := True;
410 end if;
412 Next (Prag_Unit);
413 end loop;
414 end if;
416 Next (Cont_Item);
417 end loop;
418 end Process_Body_Clauses;
420 --------------------------
421 -- Process_Spec_Clauses --
422 --------------------------
424 procedure Process_Spec_Clauses
425 (Context_List : List_Id;
426 Clause : Node_Id;
427 Used : in out Boolean;
428 Withed : in out Boolean;
429 Exit_On_Self : Boolean := False)
431 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
432 Cont_Item : Node_Id;
433 Use_Item : Node_Id;
435 begin
436 Used := False;
437 Withed := False;
439 Cont_Item := First (Context_List);
440 while Present (Cont_Item) loop
442 -- Stop the search since the context items after Cont_Item have
443 -- already been examined in a previous iteration of the reverse
444 -- loop in Check_Redundant_Withs.
446 if Exit_On_Self
447 and Cont_Item = Clause
448 then
449 exit;
450 end if;
452 -- Package use clause
454 if Nkind (Cont_Item) = N_Use_Package_Clause
455 and then not Used
456 then
457 Use_Item := First (Names (Cont_Item));
458 while Present (Use_Item) and then not Used loop
459 if Entity (Use_Item) = Nam_Ent then
460 Used := True;
461 end if;
463 Next (Use_Item);
464 end loop;
466 -- Package with clause. Avoid processing self, implicitly
467 -- generated with clauses or limited with clauses. Note that
468 -- we examine with clauses having pragmas Elaborate or
469 -- Elaborate_All applied to them due to cases such as:
472 -- with Pack;
473 -- with Pack;
474 -- pragma Elaborate (Pack);
476 -- In this case, the second with clause is redundant since
477 -- the pragma applies only to the first "with Pack;".
479 elsif Nkind (Cont_Item) = N_With_Clause
480 and then not Implicit_With (Cont_Item)
481 and then not Limited_Present (Cont_Item)
482 and then Cont_Item /= Clause
483 and then Entity (Name (Cont_Item)) = Nam_Ent
484 then
485 Withed := True;
486 end if;
488 Next (Cont_Item);
489 end loop;
490 end Process_Spec_Clauses;
492 -- Start of processing for Check_Redundant_Withs
494 begin
495 Clause := Last (Context_Items);
496 while Present (Clause) loop
498 -- Avoid checking implicitly generated with clauses, limited with
499 -- clauses or withs that have pragma Elaborate or Elaborate_All.
501 if Nkind (Clause) = N_With_Clause
502 and then not Implicit_With (Clause)
503 and then not Limited_Present (Clause)
504 and then not Elaborate_Present (Clause)
505 then
506 -- Package body-to-spec check
508 if Present (Spec_Context_Items) then
509 declare
510 Used_In_Body : Boolean := False;
511 Used_In_Spec : Boolean := False;
512 Used_Type_Or_Elab : Boolean := False;
513 Withed_In_Spec : Boolean := False;
515 begin
516 Process_Spec_Clauses
517 (Context_List => Spec_Context_Items,
518 Clause => Clause,
519 Used => Used_In_Spec,
520 Withed => Withed_In_Spec);
522 Process_Body_Clauses
523 (Context_List => Context_Items,
524 Clause => Clause,
525 Used => Used_In_Body,
526 Used_Type_Or_Elab => Used_Type_Or_Elab);
528 -- "Type Elab" refers to the presence of either a use
529 -- type clause, pragmas Elaborate or Elaborate_All.
531 -- +---------------+---------------------------+------+
532 -- | Spec | Body | Warn |
533 -- +--------+------+--------+------+-----------+------+
534 -- | Withed | Used | Withed | Used | Type Elab | |
535 -- | X | | X | | | X |
536 -- | X | | X | X | | |
537 -- | X | | X | | X | |
538 -- | X | | X | X | X | |
539 -- | X | X | X | | | X |
540 -- | X | X | X | | X | |
541 -- | X | X | X | X | | X |
542 -- | X | X | X | X | X | |
543 -- +--------+------+--------+------+-----------+------+
545 if (Withed_In_Spec
546 and then not Used_Type_Or_Elab)
547 and then
548 ((not Used_In_Spec
549 and then not Used_In_Body)
550 or else
551 Used_In_Spec)
552 then
553 Error_Msg_N -- CODEFIX
554 ("?redundant with clause in body", Clause);
555 end if;
557 Used_In_Body := False;
558 Used_In_Spec := False;
559 Used_Type_Or_Elab := False;
560 Withed_In_Spec := False;
561 end;
563 -- Standalone package spec or body check
565 else
566 declare
567 Dont_Care : Boolean := False;
568 Withed : Boolean := False;
570 begin
571 -- The mechanism for examining the context clauses of a
572 -- package spec can be applied to package body clauses.
574 Process_Spec_Clauses
575 (Context_List => Context_Items,
576 Clause => Clause,
577 Used => Dont_Care,
578 Withed => Withed,
579 Exit_On_Self => True);
581 if Withed then
582 Error_Msg_N -- CODEFIX
583 ("?redundant with clause", Clause);
584 end if;
585 end;
586 end if;
587 end if;
589 Prev (Clause);
590 end loop;
591 end Check_Redundant_Withs;
593 --------------------------------
594 -- Generate_Parent_References --
595 --------------------------------
597 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
598 Pref : Node_Id;
599 P_Name : Entity_Id := P_Id;
601 begin
602 Pref := Name (Parent (Defining_Entity (N)));
604 if Nkind (Pref) = N_Expanded_Name then
606 -- Done already, if the unit has been compiled indirectly as
607 -- part of the closure of its context because of inlining.
609 return;
610 end if;
612 while Nkind (Pref) = N_Selected_Component loop
613 Change_Selected_Component_To_Expanded_Name (Pref);
614 Set_Entity (Pref, P_Name);
615 Set_Etype (Pref, Etype (P_Name));
616 Generate_Reference (P_Name, Pref, 'r');
617 Pref := Prefix (Pref);
618 P_Name := Scope (P_Name);
619 end loop;
621 -- The guard here on P_Name is to handle the error condition where
622 -- the parent unit is missing because the file was not found.
624 if Present (P_Name) then
625 Set_Entity (Pref, P_Name);
626 Set_Etype (Pref, Etype (P_Name));
627 Generate_Reference (P_Name, Pref, 'r');
628 Style.Check_Identifier (Pref, P_Name);
629 end if;
630 end Generate_Parent_References;
632 -- Start of processing for Analyze_Compilation_Unit
634 begin
635 Process_Compilation_Unit_Pragmas (N);
637 -- If the unit is a subunit whose parent has not been analyzed (which
638 -- indicates that the main unit is a subunit, either the current one or
639 -- one of its descendents) then the subunit is compiled as part of the
640 -- analysis of the parent, which we proceed to do. Basically this gets
641 -- handled from the top down and we don't want to do anything at this
642 -- level (i.e. this subunit will be handled on the way down from the
643 -- parent), so at this level we immediately return. If the subunit ends
644 -- up not analyzed, it means that the parent did not contain a stub for
645 -- it, or that there errors were detected in some ancestor.
647 if Nkind (Unit_Node) = N_Subunit
648 and then not Analyzed (Lib_Unit)
649 then
650 Semantics (Lib_Unit);
652 if not Analyzed (Proper_Body (Unit_Node)) then
653 if Serious_Errors_Detected > 0 then
654 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
655 else
656 Error_Msg_N ("missing stub for subunit", N);
657 end if;
658 end if;
660 return;
661 end if;
663 -- Analyze context (this will call Sem recursively for with'ed units) To
664 -- detect circularities among with-clauses that are not caught during
665 -- loading, we set the Context_Pending flag on the current unit. If the
666 -- flag is already set there is a potential circularity. We exclude
667 -- predefined units from this check because they are known to be safe.
668 -- We also exclude package bodies that are present because circularities
669 -- between bodies are harmless (and necessary).
671 if Context_Pending (N) then
672 declare
673 Circularity : Boolean := True;
675 begin
676 if Is_Predefined_File_Name
677 (Unit_File_Name (Get_Source_Unit (Unit (N))))
678 then
679 Circularity := False;
681 else
682 for U in Main_Unit + 1 .. Last_Unit loop
683 if Nkind (Unit (Cunit (U))) = N_Package_Body
684 and then not Analyzed (Cunit (U))
685 then
686 Circularity := False;
687 exit;
688 end if;
689 end loop;
690 end if;
692 if Circularity then
693 Error_Msg_N ("circular dependency caused by with_clauses", N);
694 Error_Msg_N
695 ("\possibly missing limited_with clause"
696 & " in one of the following", N);
698 for U in Main_Unit .. Last_Unit loop
699 if Context_Pending (Cunit (U)) then
700 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
701 Error_Msg_N ("\unit$", N);
702 end if;
703 end loop;
705 raise Unrecoverable_Error;
706 end if;
707 end;
708 else
709 Set_Context_Pending (N);
710 end if;
712 Analyze_Context (N);
714 Set_Context_Pending (N, False);
716 -- If the unit is a package body, the spec is already loaded and must be
717 -- analyzed first, before we analyze the body.
719 if Nkind (Unit_Node) = N_Package_Body then
721 -- If no Lib_Unit, then there was a serious previous error, so just
722 -- ignore the entire analysis effort
724 if No (Lib_Unit) then
725 return;
727 else
728 Semantics (Lib_Unit);
729 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
731 -- Verify that the library unit is a package declaration
733 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
734 N_Generic_Package_Declaration)
735 then
736 Error_Msg_N
737 ("no legal package declaration for package body", N);
738 return;
740 -- Otherwise, the entity in the declaration is visible. Update the
741 -- version to reflect dependence of this body on the spec.
743 else
744 Spec_Id := Defining_Entity (Unit (Lib_Unit));
745 Set_Is_Immediately_Visible (Spec_Id, True);
746 Version_Update (N, Lib_Unit);
748 if Nkind (Defining_Unit_Name (Unit_Node)) =
749 N_Defining_Program_Unit_Name
750 then
751 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
752 end if;
753 end if;
754 end if;
756 -- If the unit is a subprogram body, then we similarly need to analyze
757 -- its spec. However, things are a little simpler in this case, because
758 -- here, this analysis is done only for error checking and consistency
759 -- purposes, so there's nothing else to be done.
761 elsif Nkind (Unit_Node) = N_Subprogram_Body then
762 if Acts_As_Spec (N) then
764 -- If the subprogram body is a child unit, we must create a
765 -- declaration for it, in order to properly load the parent(s).
766 -- After this, the original unit does not acts as a spec, because
767 -- there is an explicit one. If this unit appears in a context
768 -- clause, then an implicit with on the parent will be added when
769 -- installing the context. If this is the main unit, there is no
770 -- Unit_Table entry for the declaration (it has the unit number
771 -- of the main unit) and code generation is unaffected.
773 Unum := Get_Cunit_Unit_Number (N);
774 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
776 if Par_Spec_Name /= No_Unit_Name then
777 Unum :=
778 Load_Unit
779 (Load_Name => Par_Spec_Name,
780 Required => True,
781 Subunit => False,
782 Error_Node => N);
784 if Unum /= No_Unit then
786 -- Build subprogram declaration and attach parent unit to it
787 -- This subprogram declaration does not come from source,
788 -- Nevertheless the backend must generate debugging info for
789 -- it, and this must be indicated explicitly. We also mark
790 -- the body entity as a child unit now, to prevent a
791 -- cascaded error if the spec entity cannot be entered
792 -- in its scope. Finally we create a Units table entry for
793 -- the subprogram declaration, to maintain a one-to-one
794 -- correspondence with compilation unit nodes. This is
795 -- critical for the tree traversals performed by CodePeer.
797 declare
798 Loc : constant Source_Ptr := Sloc (N);
799 SCS : constant Boolean :=
800 Get_Comes_From_Source_Default;
802 begin
803 Set_Comes_From_Source_Default (False);
804 Lib_Unit :=
805 Make_Compilation_Unit (Loc,
806 Context_Items => New_Copy_List (Context_Items (N)),
807 Unit =>
808 Make_Subprogram_Declaration (Sloc (N),
809 Specification =>
810 Copy_Separate_Tree
811 (Specification (Unit_Node))),
812 Aux_Decls_Node =>
813 Make_Compilation_Unit_Aux (Loc));
815 Set_Library_Unit (N, Lib_Unit);
816 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
817 Make_Child_Decl_Unit (N);
818 Semantics (Lib_Unit);
820 -- Now that a separate declaration exists, the body
821 -- of the child unit does not act as spec any longer.
823 Set_Acts_As_Spec (N, False);
824 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
825 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
826 Set_Comes_From_Source_Default (SCS);
827 end;
828 end if;
829 end if;
831 -- Here for subprogram with separate declaration
833 else
834 Semantics (Lib_Unit);
835 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
836 Version_Update (N, Lib_Unit);
837 end if;
839 -- If this is a child unit, generate references to the parents
841 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
842 N_Defining_Program_Unit_Name
843 then
844 Generate_Parent_References (
845 Specification (Unit_Node),
846 Scope (Defining_Entity (Unit (Lib_Unit))));
847 end if;
848 end if;
850 -- If it is a child unit, the parent must be elaborated first and we
851 -- update version, since we are dependent on our parent.
853 if Is_Child_Spec (Unit_Node) then
855 -- The analysis of the parent is done with style checks off
857 declare
858 Save_Style_Check : constant Boolean := Style_Check;
859 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
860 Cunit_Boolean_Restrictions_Save;
862 begin
863 if not GNAT_Mode then
864 Style_Check := False;
865 end if;
867 Semantics (Parent_Spec (Unit_Node));
868 Version_Update (N, Parent_Spec (Unit_Node));
869 Style_Check := Save_Style_Check;
870 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
871 end;
872 end if;
874 -- With the analysis done, install the context. Note that we can't
875 -- install the context from the with clauses as we analyze them, because
876 -- each with clause must be analyzed in a clean visibility context, so
877 -- we have to wait and install them all at once.
879 Install_Context (N);
881 if Is_Child_Spec (Unit_Node) then
883 -- Set the entities of all parents in the program_unit_name
885 Generate_Parent_References (
886 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
887 end if;
889 -- All components of the context: with-clauses, library unit, ancestors
890 -- if any, (and their context) are analyzed and installed.
892 -- Call special debug routine sm if this is the main unit
894 if Current_Sem_Unit = Main_Unit then
896 end if;
898 -- Now analyze the unit (package, subprogram spec, body) itself
900 Analyze (Unit_Node);
902 if Warn_On_Redundant_Constructs then
903 Check_Redundant_Withs (Context_Items (N));
905 if Nkind (Unit_Node) = N_Package_Body then
906 Check_Redundant_Withs
907 (Context_Items => Context_Items (N),
908 Spec_Context_Items => Context_Items (Lib_Unit));
909 end if;
910 end if;
912 -- The above call might have made Unit_Node an N_Subprogram_Body from
913 -- something else, so propagate any Acts_As_Spec flag.
915 if Nkind (Unit_Node) = N_Subprogram_Body
916 and then Acts_As_Spec (Unit_Node)
917 then
918 Set_Acts_As_Spec (N);
919 end if;
921 -- Register predefined units in Rtsfind
923 declare
924 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
925 begin
926 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
927 Set_RTU_Loaded (Unit_Node);
928 end if;
929 end;
931 -- Treat compilation unit pragmas that appear after the library unit
933 if Present (Pragmas_After (Aux_Decls_Node (N))) then
934 declare
935 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
936 begin
937 while Present (Prag_Node) loop
938 Analyze (Prag_Node);
939 Next (Prag_Node);
940 end loop;
941 end;
942 end if;
944 -- Generate distribution stubs if requested and no error
946 if N = Main_Cunit
947 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
948 or else
949 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
950 and then not Fatal_Error (Main_Unit)
951 then
952 if Is_RCI_Pkg_Spec_Or_Body (N) then
954 -- Regular RCI package
956 Add_Stub_Constructs (N);
958 elsif (Nkind (Unit_Node) = N_Package_Declaration
959 and then Is_Shared_Passive (Defining_Entity
960 (Specification (Unit_Node))))
961 or else (Nkind (Unit_Node) = N_Package_Body
962 and then
963 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
964 then
965 -- Shared passive package
967 Add_Stub_Constructs (N);
969 elsif Nkind (Unit_Node) = N_Package_Instantiation
970 and then
971 Is_Remote_Call_Interface
972 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
973 then
974 -- Instantiation of a RCI generic package
976 Add_Stub_Constructs (N);
977 end if;
978 end if;
980 -- Remove unit from visibility, so that environment is clean for the
981 -- next compilation, which is either the main unit or some other unit
982 -- in the context.
984 if Nkind_In (Unit_Node, N_Package_Declaration,
985 N_Package_Renaming_Declaration,
986 N_Subprogram_Declaration)
987 or else Nkind (Unit_Node) in N_Generic_Declaration
988 or else
989 (Nkind (Unit_Node) = N_Subprogram_Body
990 and then Acts_As_Spec (Unit_Node))
991 then
992 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
994 -- If the unit is an instantiation whose body will be elaborated for
995 -- inlining purposes, use the proper entity of the instance. The entity
996 -- may be missing if the instantiation was illegal.
998 elsif Nkind (Unit_Node) = N_Package_Instantiation
999 and then not Error_Posted (Unit_Node)
1000 and then Present (Instance_Spec (Unit_Node))
1001 then
1002 Remove_Unit_From_Visibility
1003 (Defining_Entity (Instance_Spec (Unit_Node)));
1005 elsif Nkind (Unit_Node) = N_Package_Body
1006 or else (Nkind (Unit_Node) = N_Subprogram_Body
1007 and then not Acts_As_Spec (Unit_Node))
1008 then
1009 -- Bodies that are not the main unit are compiled if they are generic
1010 -- or contain generic or inlined units. Their analysis brings in the
1011 -- context of the corresponding spec (unit declaration) which must be
1012 -- removed as well, to return the compilation environment to its
1013 -- proper state.
1015 Remove_Context (Lib_Unit);
1016 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1017 end if;
1019 -- Last step is to deinstall the context we just installed as well as
1020 -- the unit just compiled.
1022 Remove_Context (N);
1024 -- If this is the main unit and we are generating code, we must check
1025 -- that all generic units in the context have a body if they need it,
1026 -- even if they have not been instantiated. In the absence of .ali files
1027 -- for generic units, we must force the load of the body, just to
1028 -- produce the proper error if the body is absent. We skip this
1029 -- verification if the main unit itself is generic.
1031 if Get_Cunit_Unit_Number (N) = Main_Unit
1032 and then Operating_Mode = Generate_Code
1033 and then Expander_Active
1034 then
1035 -- Check whether the source for the body of the unit must be included
1036 -- in a standalone library.
1038 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1040 -- Indicate that the main unit is now analyzed, to catch possible
1041 -- circularities between it and generic bodies. Remove main unit from
1042 -- visibility. This might seem superfluous, but the main unit must
1043 -- not be visible in the generic body expansions that follow.
1045 Set_Analyzed (N, True);
1046 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1048 declare
1049 Item : Node_Id;
1050 Nam : Entity_Id;
1051 Un : Unit_Number_Type;
1053 Save_Style_Check : constant Boolean := Style_Check;
1054 Save_C_Restrict : constant Save_Cunit_Boolean_Restrictions :=
1055 Cunit_Boolean_Restrictions_Save;
1057 begin
1058 Item := First (Context_Items (N));
1059 while Present (Item) loop
1061 -- Check for explicit with clause
1063 if Nkind (Item) = N_With_Clause
1064 and then not Implicit_With (Item)
1066 -- Ada 2005 (AI-50217): Ignore limited-withed units
1068 and then not Limited_Present (Item)
1069 then
1070 Nam := Entity (Name (Item));
1072 -- Compile generic subprogram, unless it is intrinsic or
1073 -- imported so no body is required, or generic package body
1074 -- if the package spec requires a body.
1076 if (Is_Generic_Subprogram (Nam)
1077 and then not Is_Intrinsic_Subprogram (Nam)
1078 and then not Is_Imported (Nam))
1079 or else (Ekind (Nam) = E_Generic_Package
1080 and then Unit_Requires_Body (Nam))
1081 then
1082 Style_Check := False;
1084 if Present (Renamed_Object (Nam)) then
1085 Un :=
1086 Load_Unit
1087 (Load_Name => Get_Body_Name
1088 (Get_Unit_Name
1089 (Unit_Declaration_Node
1090 (Renamed_Object (Nam)))),
1091 Required => False,
1092 Subunit => False,
1093 Error_Node => N,
1094 Renamings => True);
1095 else
1096 Un :=
1097 Load_Unit
1098 (Load_Name => Get_Body_Name
1099 (Get_Unit_Name (Item)),
1100 Required => False,
1101 Subunit => False,
1102 Error_Node => N,
1103 Renamings => True);
1104 end if;
1106 if Un = No_Unit then
1107 Error_Msg_NE
1108 ("body of generic unit& not found", Item, Nam);
1109 exit;
1111 elsif not Analyzed (Cunit (Un))
1112 and then Un /= Main_Unit
1113 and then not Fatal_Error (Un)
1114 then
1115 Style_Check := False;
1116 Semantics (Cunit (Un));
1117 end if;
1118 end if;
1119 end if;
1121 Next (Item);
1122 end loop;
1124 Style_Check := Save_Style_Check;
1125 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1126 end;
1127 end if;
1129 -- Deal with creating elaboration Boolean if needed. We create an
1130 -- elaboration boolean only for units that come from source since
1131 -- units manufactured by the compiler never need elab checks.
1133 if Comes_From_Source (N)
1134 and then Nkind_In (Unit_Node, N_Package_Declaration,
1135 N_Generic_Package_Declaration,
1136 N_Subprogram_Declaration,
1137 N_Generic_Subprogram_Declaration)
1138 then
1139 declare
1140 Loc : constant Source_Ptr := Sloc (N);
1141 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1143 begin
1144 Spec_Id := Defining_Entity (Unit_Node);
1145 Generate_Definition (Spec_Id);
1147 -- See if an elaboration entity is required for possible access
1148 -- before elaboration checking. Note that we must allow for this
1149 -- even if -gnatE is not set, since a client may be compiled in
1150 -- -gnatE mode and reference the entity.
1152 -- These entities are also used by the binder to prevent multiple
1153 -- attempts to execute the elaboration code for the library case
1154 -- where the elaboration routine might otherwise be called more
1155 -- than once.
1157 -- Case of units which do not require elaboration checks
1160 -- Pure units do not need checks
1162 Is_Pure (Spec_Id)
1164 -- Preelaborated units do not need checks
1166 or else Is_Preelaborated (Spec_Id)
1168 -- No checks needed if pragma Elaborate_Body present
1170 or else Has_Pragma_Elaborate_Body (Spec_Id)
1172 -- No checks needed if unit does not require a body
1174 or else not Unit_Requires_Body (Spec_Id)
1176 -- No checks needed for predefined files
1178 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1180 -- No checks required if no separate spec
1182 or else Acts_As_Spec (N)
1183 then
1184 -- This is a case where we only need the entity for
1185 -- checking to prevent multiple elaboration checks.
1187 Set_Elaboration_Entity_Required (Spec_Id, False);
1189 -- Case of elaboration entity is required for access before
1190 -- elaboration checking (so certainly we must build it!)
1192 else
1193 Set_Elaboration_Entity_Required (Spec_Id, True);
1194 end if;
1196 Build_Elaboration_Entity (N, Spec_Id);
1197 end;
1198 end if;
1200 -- Freeze the compilation unit entity. This for sure is needed because
1201 -- of some warnings that can be output (see Freeze_Subprogram), but may
1202 -- in general be required. If freezing actions result, place them in the
1203 -- compilation unit actions list, and analyze them.
1205 declare
1206 L : constant List_Id :=
1207 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1208 begin
1209 while Is_Non_Empty_List (L) loop
1210 Insert_Library_Level_Action (Remove_Head (L));
1211 end loop;
1212 end;
1214 Set_Analyzed (N);
1216 if Nkind (Unit_Node) = N_Package_Declaration
1217 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1218 and then Expander_Active
1219 then
1220 declare
1221 Save_Style_Check : constant Boolean := Style_Check;
1222 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1223 Options : Style_Check_Options;
1225 begin
1226 Save_Style_Check_Options (Options);
1227 Reset_Style_Check_Options;
1228 Opt.Warning_Mode := Suppress;
1229 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1231 Reset_Style_Check_Options;
1232 Set_Style_Check_Options (Options);
1233 Style_Check := Save_Style_Check;
1234 Warning_Mode := Save_Warning;
1235 end;
1236 end if;
1238 -- If we are generating obsolescent warnings, then here is where we
1239 -- generate them for the with'ed items. The reason for this special
1240 -- processing is that the normal mechanism of generating the warnings
1241 -- for referenced entities does not work for context clause references.
1242 -- That's because when we first analyze the context, it is too early to
1243 -- know if the with'ing unit is itself obsolescent (which suppresses
1244 -- the warnings).
1246 if not GNAT_Mode and then Warn_On_Obsolescent_Feature then
1248 -- Push current compilation unit as scope, so that the test for
1249 -- being within an obsolescent unit will work correctly.
1251 Push_Scope (Defining_Entity (Unit_Node));
1253 -- Loop through context items to deal with with clauses
1255 declare
1256 Item : Node_Id;
1257 Nam : Node_Id;
1258 Ent : Entity_Id;
1260 begin
1261 Item := First (Context_Items (N));
1262 while Present (Item) loop
1263 if Nkind (Item) = N_With_Clause
1265 -- Suppress this check in limited-withed units. Further work
1266 -- needed here if we decide to incorporate this check on
1267 -- limited-withed units.
1269 and then not Limited_Present (Item)
1270 then
1271 Nam := Name (Item);
1272 Ent := Entity (Nam);
1274 if Is_Obsolescent (Ent) then
1275 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1276 end if;
1277 end if;
1279 Next (Item);
1280 end loop;
1281 end;
1283 -- Remove temporary install of current unit as scope
1285 Pop_Scope;
1286 end if;
1287 end Analyze_Compilation_Unit;
1289 ---------------------
1290 -- Analyze_Context --
1291 ---------------------
1293 procedure Analyze_Context (N : Node_Id) is
1294 Ukind : constant Node_Kind := Nkind (Unit (N));
1295 Item : Node_Id;
1297 begin
1298 -- First process all configuration pragmas at the start of the context
1299 -- items. Strictly these are not part of the context clause, but that
1300 -- is where the parser puts them. In any case for sure we must analyze
1301 -- these before analyzing the actual context items, since they can have
1302 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1303 -- be with'ed as a result of changing categorizations in Ada 2005).
1305 Item := First (Context_Items (N));
1306 while Present (Item)
1307 and then Nkind (Item) = N_Pragma
1308 and then Pragma_Name (Item) in Configuration_Pragma_Names
1309 loop
1310 Analyze (Item);
1311 Next (Item);
1312 end loop;
1314 -- This is the point at which we capture the configuration settings
1315 -- for the unit. At the moment only the Optimize_Alignment setting
1316 -- needs to be captured. Probably more later ???
1318 if Optimize_Alignment_Local then
1319 Set_OA_Setting (Current_Sem_Unit, 'L');
1320 else
1321 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1322 end if;
1324 -- Loop through actual context items. This is done in two passes:
1326 -- a) The first pass analyzes non-limited with-clauses and also any
1327 -- configuration pragmas (we need to get the latter analyzed right
1328 -- away, since they can affect processing of subsequent items.
1330 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1332 while Present (Item) loop
1334 -- For with clause, analyze the with clause, and then update the
1335 -- version, since we are dependent on a unit that we with.
1337 if Nkind (Item) = N_With_Clause
1338 and then not Limited_Present (Item)
1339 then
1340 -- Skip analyzing with clause if no unit, nothing to do (this
1341 -- happens for a with that references a non-existent unit). Skip
1342 -- as well if this is a with_clause for the main unit, which
1343 -- happens if a subunit has a useless with_clause on its parent.
1345 if Present (Library_Unit (Item)) then
1346 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1347 Analyze (Item);
1349 else
1350 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1351 end if;
1352 end if;
1354 if not Implicit_With (Item) then
1355 Version_Update (N, Library_Unit (Item));
1356 end if;
1358 -- Skip pragmas. Configuration pragmas at the start were handled in
1359 -- the loop above, and remaining pragmas are not processed until we
1360 -- actually install the context (see Install_Context). We delay the
1361 -- analysis of these pragmas to make sure that we have installed all
1362 -- the implicit with's on parent units.
1364 -- Skip use clauses at this stage, since we don't want to do any
1365 -- installing of potentially use-visible entities until we
1366 -- actually install the complete context (in Install_Context).
1367 -- Otherwise things can get installed in the wrong context.
1369 else
1370 null;
1371 end if;
1373 Next (Item);
1374 end loop;
1376 -- Second pass: examine all limited_with clauses. All other context
1377 -- items are ignored in this pass.
1379 Item := First (Context_Items (N));
1380 while Present (Item) loop
1381 if Nkind (Item) = N_With_Clause
1382 and then Limited_Present (Item)
1383 then
1384 -- No need to check errors on implicitly generated limited-with
1385 -- clauses.
1387 if not Implicit_With (Item) then
1389 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1390 -- properly rejected, including renaming declarations.
1392 if not Nkind_In (Ukind, N_Package_Declaration,
1393 N_Subprogram_Declaration)
1394 and then Ukind not in N_Generic_Declaration
1395 and then Ukind not in N_Generic_Instantiation
1396 then
1397 Error_Msg_N ("limited with_clause not allowed here", Item);
1399 -- Check wrong use of a limited with clause applied to the
1400 -- compilation unit containing the limited-with clause.
1402 -- limited with P.Q;
1403 -- package P.Q is ...
1405 elsif Unit (Library_Unit (Item)) = Unit (N) then
1406 Error_Msg_N ("wrong use of limited-with clause", Item);
1408 -- Check wrong use of limited-with clause applied to some
1409 -- immediate ancestor.
1411 elsif Is_Child_Spec (Unit (N)) then
1412 declare
1413 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1414 P : Node_Id;
1416 begin
1417 P := Parent_Spec (Unit (N));
1418 loop
1419 if Unit (P) = Lib_U then
1420 Error_Msg_N ("limited with_clause cannot "
1421 & "name ancestor", Item);
1422 exit;
1423 end if;
1425 exit when not Is_Child_Spec (Unit (P));
1426 P := Parent_Spec (Unit (P));
1427 end loop;
1428 end;
1429 end if;
1431 -- Check if the limited-withed unit is already visible through
1432 -- some context clause of the current compilation unit or some
1433 -- ancestor of the current compilation unit.
1435 declare
1436 Lim_Unit_Name : constant Node_Id := Name (Item);
1437 Comp_Unit : Node_Id;
1438 It : Node_Id;
1439 Unit_Name : Node_Id;
1441 begin
1442 Comp_Unit := N;
1443 loop
1444 It := First (Context_Items (Comp_Unit));
1445 while Present (It) loop
1446 if Item /= It
1447 and then Nkind (It) = N_With_Clause
1448 and then not Limited_Present (It)
1449 and then
1450 Nkind_In (Unit (Library_Unit (It)),
1451 N_Package_Declaration,
1452 N_Package_Renaming_Declaration)
1453 then
1454 if Nkind (Unit (Library_Unit (It))) =
1455 N_Package_Declaration
1456 then
1457 Unit_Name := Name (It);
1458 else
1459 Unit_Name := Name (Unit (Library_Unit (It)));
1460 end if;
1462 -- Check if the named package (or some ancestor)
1463 -- leaves visible the full-view of the unit given
1464 -- in the limited-with clause
1466 loop
1467 if Designate_Same_Unit (Lim_Unit_Name,
1468 Unit_Name)
1469 then
1470 Error_Msg_Sloc := Sloc (It);
1471 Error_Msg_N
1472 ("simultaneous visibility of limited "
1473 & "and unlimited views not allowed",
1474 Item);
1475 Error_Msg_NE
1476 ("\unlimited view visible through "
1477 & "context clause #",
1478 Item, It);
1479 exit;
1481 elsif Nkind (Unit_Name) = N_Identifier then
1482 exit;
1483 end if;
1485 Unit_Name := Prefix (Unit_Name);
1486 end loop;
1487 end if;
1489 Next (It);
1490 end loop;
1492 exit when not Is_Child_Spec (Unit (Comp_Unit));
1494 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1495 end loop;
1496 end;
1497 end if;
1499 -- Skip analyzing with clause if no unit, see above
1501 if Present (Library_Unit (Item)) then
1502 Analyze (Item);
1503 end if;
1505 -- A limited_with does not impose an elaboration order, but
1506 -- there is a semantic dependency for recompilation purposes.
1508 if not Implicit_With (Item) then
1509 Version_Update (N, Library_Unit (Item));
1510 end if;
1512 -- Pragmas and use clauses and with clauses other than limited
1513 -- with's are ignored in this pass through the context items.
1515 else
1516 null;
1517 end if;
1519 Next (Item);
1520 end loop;
1521 end Analyze_Context;
1523 -------------------------------
1524 -- Analyze_Package_Body_Stub --
1525 -------------------------------
1527 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1528 Id : constant Entity_Id := Defining_Identifier (N);
1529 Nam : Entity_Id;
1531 begin
1532 -- The package declaration must be in the current declarative part
1534 Check_Stub_Level (N);
1535 Nam := Current_Entity_In_Scope (Id);
1537 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1538 Error_Msg_N ("missing specification for package stub", N);
1540 elsif Has_Completion (Nam)
1541 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1542 then
1543 Error_Msg_N ("duplicate or redundant stub for package", N);
1545 else
1546 -- Indicate that the body of the package exists. If we are doing
1547 -- only semantic analysis, the stub stands for the body. If we are
1548 -- generating code, the existence of the body will be confirmed
1549 -- when we load the proper body.
1551 Set_Has_Completion (Nam);
1552 Set_Scope (Defining_Entity (N), Current_Scope);
1553 Generate_Reference (Nam, Id, 'b');
1554 Analyze_Proper_Body (N, Nam);
1555 end if;
1556 end Analyze_Package_Body_Stub;
1558 -------------------------
1559 -- Analyze_Proper_Body --
1560 -------------------------
1562 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1563 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1564 Unum : Unit_Number_Type;
1566 procedure Optional_Subunit;
1567 -- This procedure is called when the main unit is a stub, or when we
1568 -- are not generating code. In such a case, we analyze the subunit if
1569 -- present, which is user-friendly and in fact required for ASIS, but
1570 -- we don't complain if the subunit is missing.
1572 ----------------------
1573 -- Optional_Subunit --
1574 ----------------------
1576 procedure Optional_Subunit is
1577 Comp_Unit : Node_Id;
1579 begin
1580 -- Try to load subunit, but ignore any errors that occur during the
1581 -- loading of the subunit, by using the special feature in Errout to
1582 -- ignore all errors. Note that Fatal_Error will still be set, so we
1583 -- will be able to check for this case below.
1585 if not ASIS_Mode then
1586 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1587 end if;
1589 Unum :=
1590 Load_Unit
1591 (Load_Name => Subunit_Name,
1592 Required => False,
1593 Subunit => True,
1594 Error_Node => N);
1596 if not ASIS_Mode then
1597 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1598 end if;
1600 -- All done if we successfully loaded the subunit
1602 if Unum /= No_Unit
1603 and then (not Fatal_Error (Unum) or else Try_Semantics)
1604 then
1605 Comp_Unit := Cunit (Unum);
1607 -- If the file was empty or seriously mangled, the unit itself may
1608 -- be missing.
1610 if No (Unit (Comp_Unit)) then
1611 Error_Msg_N
1612 ("subunit does not contain expected proper body", N);
1614 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1615 Error_Msg_N
1616 ("expected SEPARATE subunit, found child unit",
1617 Cunit_Entity (Unum));
1618 else
1619 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1620 Analyze_Subunit (Comp_Unit);
1621 Set_Library_Unit (N, Comp_Unit);
1622 end if;
1624 elsif Unum = No_Unit
1625 and then Present (Nam)
1626 then
1627 if Is_Protected_Type (Nam) then
1628 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1629 else
1630 Set_Corresponding_Body (
1631 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1632 end if;
1633 end if;
1634 end Optional_Subunit;
1636 -- Start of processing for Analyze_Proper_Body
1638 begin
1639 -- If the subunit is already loaded, it means that the main unit is a
1640 -- subunit, and that the current unit is one of its parents which was
1641 -- being analyzed to provide the needed context for the analysis of the
1642 -- subunit. In this case we analyze the subunit and continue with the
1643 -- parent, without looking a subsequent subunits.
1645 if Is_Loaded (Subunit_Name) then
1647 -- If the proper body is already linked to the stub node, the stub is
1648 -- in a generic unit and just needs analyzing.
1650 if Present (Library_Unit (N)) then
1651 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1652 Analyze_Subunit (Library_Unit (N));
1654 -- Otherwise we must load the subunit and link to it
1656 else
1657 -- Load the subunit, this must work, since we originally loaded
1658 -- the subunit earlier on. So this will not really load it, just
1659 -- give access to it.
1661 Unum :=
1662 Load_Unit
1663 (Load_Name => Subunit_Name,
1664 Required => True,
1665 Subunit => False,
1666 Error_Node => N);
1668 -- And analyze the subunit in the parent context (note that we
1669 -- do not call Semantics, since that would remove the parent
1670 -- context). Because of this, we have to manually reset the
1671 -- compiler state to Analyzing since it got destroyed by Load.
1673 if Unum /= No_Unit then
1674 Compiler_State := Analyzing;
1676 -- Check that the proper body is a subunit and not a child
1677 -- unit. If the unit was previously loaded, the error will
1678 -- have been emitted when copying the generic node, so we
1679 -- just return to avoid cascaded errors.
1681 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1682 return;
1683 end if;
1685 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1686 Analyze_Subunit (Cunit (Unum));
1687 Set_Library_Unit (N, Cunit (Unum));
1688 end if;
1689 end if;
1691 -- If the main unit is a subunit, then we are just performing semantic
1692 -- analysis on that subunit, and any other subunits of any parent unit
1693 -- should be ignored, except that if we are building trees for ASIS
1694 -- usage we want to annotate the stub properly.
1696 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1697 and then Subunit_Name /= Unit_Name (Main_Unit)
1698 then
1699 if ASIS_Mode then
1700 Optional_Subunit;
1701 end if;
1703 -- But before we return, set the flag for unloaded subunits. This
1704 -- will suppress junk warnings of variables in the same declarative
1705 -- part (or a higher level one) that are in danger of looking unused
1706 -- when in fact there might be a declaration in the subunit that we
1707 -- do not intend to load.
1709 Unloaded_Subunits := True;
1710 return;
1712 -- If the subunit is not already loaded, and we are generating code,
1713 -- then this is the case where compilation started from the parent, and
1714 -- we are generating code for an entire subunit tree. In that case we
1715 -- definitely need to load the subunit.
1717 -- In order to continue the analysis with the rest of the parent,
1718 -- and other subunits, we load the unit without requiring its
1719 -- presence, and emit a warning if not found, rather than terminating
1720 -- the compilation abruptly, as for other missing file problems.
1722 elsif Original_Operating_Mode = Generate_Code then
1724 -- If the proper body is already linked to the stub node, the stub is
1725 -- in a generic unit and just needs analyzing.
1727 -- We update the version. Although we are not strictly technically
1728 -- semantically dependent on the subunit, given our approach of macro
1729 -- substitution of subunits, it makes sense to include it in the
1730 -- version identification.
1732 if Present (Library_Unit (N)) then
1733 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1734 Analyze_Subunit (Library_Unit (N));
1735 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1737 -- Otherwise we must load the subunit and link to it
1739 else
1740 -- Make sure that, if the subunit is preprocessed and -gnateG is
1741 -- specified, the preprocessed file will be written.
1743 Lib.Analysing_Subunit_Of_Main := True;
1744 Unum :=
1745 Load_Unit
1746 (Load_Name => Subunit_Name,
1747 Required => False,
1748 Subunit => True,
1749 Error_Node => N);
1750 Lib.Analysing_Subunit_Of_Main := False;
1752 -- Give message if we did not get the unit Emit warning even if
1753 -- missing subunit is not within main unit, to simplify debugging.
1755 if Original_Operating_Mode = Generate_Code
1756 and then Unum = No_Unit
1757 then
1758 Error_Msg_Unit_1 := Subunit_Name;
1759 Error_Msg_File_1 :=
1760 Get_File_Name (Subunit_Name, Subunit => True);
1761 Error_Msg_N
1762 ("subunit$$ in file{ not found?!!", N);
1763 Subunits_Missing := True;
1764 end if;
1766 -- Load_Unit may reset Compiler_State, since it may have been
1767 -- necessary to parse an additional units, so we make sure that
1768 -- we reset it to the Analyzing state.
1770 Compiler_State := Analyzing;
1772 if Unum /= No_Unit then
1773 if Debug_Flag_L then
1774 Write_Str ("*** Loaded subunit from stub. Analyze");
1775 Write_Eol;
1776 end if;
1778 declare
1779 Comp_Unit : constant Node_Id := Cunit (Unum);
1781 begin
1782 -- Check for child unit instead of subunit
1784 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1785 Error_Msg_N
1786 ("expected SEPARATE subunit, found child unit",
1787 Cunit_Entity (Unum));
1789 -- OK, we have a subunit
1791 else
1792 -- Set corresponding stub (even if errors)
1794 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1796 -- Collect SCO information for loaded subunit if we are
1797 -- in the main unit).
1799 if Generate_SCO
1800 and then
1801 In_Extended_Main_Source_Unit
1802 (Cunit_Entity (Current_Sem_Unit))
1803 then
1804 SCO_Record (Unum);
1805 end if;
1807 -- Analyze the unit if semantics active
1809 if not Fatal_Error (Unum) or else Try_Semantics then
1810 Analyze_Subunit (Comp_Unit);
1811 end if;
1813 -- Set the library unit pointer in any case
1815 Set_Library_Unit (N, Comp_Unit);
1817 -- We update the version. Although we are not technically
1818 -- semantically dependent on the subunit, given our
1819 -- approach of macro substitution of subunits, it makes
1820 -- sense to include it in the version identification.
1822 Version_Update (Cunit (Main_Unit), Comp_Unit);
1823 end if;
1824 end;
1825 end if;
1826 end if;
1828 -- The remaining case is when the subunit is not already loaded and we
1829 -- are not generating code. In this case we are just performing semantic
1830 -- analysis on the parent, and we are not interested in the subunit. For
1831 -- subprograms, analyze the stub as a body. For other entities the stub
1832 -- has already been marked as completed.
1834 else
1835 Optional_Subunit;
1836 end if;
1837 end Analyze_Proper_Body;
1839 ----------------------------------
1840 -- Analyze_Protected_Body_Stub --
1841 ----------------------------------
1843 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1844 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1846 begin
1847 Check_Stub_Level (N);
1849 -- First occurrence of name may have been as an incomplete type
1851 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1852 Nam := Full_View (Nam);
1853 end if;
1855 if No (Nam)
1856 or else not Is_Protected_Type (Etype (Nam))
1857 then
1858 Error_Msg_N ("missing specification for Protected body", N);
1859 else
1860 Set_Scope (Defining_Entity (N), Current_Scope);
1861 Set_Has_Completion (Etype (Nam));
1862 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1863 Analyze_Proper_Body (N, Etype (Nam));
1864 end if;
1865 end Analyze_Protected_Body_Stub;
1867 ----------------------------------
1868 -- Analyze_Subprogram_Body_Stub --
1869 ----------------------------------
1871 -- A subprogram body stub can appear with or without a previous spec. If
1872 -- there is one, then the analysis of the body will find it and verify
1873 -- conformance. The formals appearing in the specification of the stub play
1874 -- no role, except for requiring an additional conformance check. If there
1875 -- is no previous subprogram declaration, the stub acts as a spec, and
1876 -- provides the defining entity for the subprogram.
1878 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1879 Decl : Node_Id;
1881 begin
1882 Check_Stub_Level (N);
1884 -- Verify that the identifier for the stub is unique within this
1885 -- declarative part.
1887 if Nkind_In (Parent (N), N_Block_Statement,
1888 N_Package_Body,
1889 N_Subprogram_Body)
1890 then
1891 Decl := First (Declarations (Parent (N)));
1892 while Present (Decl)
1893 and then Decl /= N
1894 loop
1895 if Nkind (Decl) = N_Subprogram_Body_Stub
1896 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1897 Chars (Defining_Unit_Name (Specification (N))))
1898 then
1899 Error_Msg_N ("identifier for stub is not unique", N);
1900 end if;
1902 Next (Decl);
1903 end loop;
1904 end if;
1906 -- Treat stub as a body, which checks conformance if there is a previous
1907 -- declaration, or else introduces entity and its signature.
1909 Analyze_Subprogram_Body (N);
1910 Analyze_Proper_Body (N, Empty);
1911 end Analyze_Subprogram_Body_Stub;
1913 ---------------------
1914 -- Analyze_Subunit --
1915 ---------------------
1917 -- A subunit is compiled either by itself (for semantic checking) or as
1918 -- part of compiling the parent (for code generation). In either case, by
1919 -- the time we actually process the subunit, the parent has already been
1920 -- installed and analyzed. The node N is a compilation unit, whose context
1921 -- needs to be treated here, because we come directly here from the parent
1922 -- without calling Analyze_Compilation_Unit.
1924 -- The compilation context includes the explicit context of the subunit,
1925 -- and the context of the parent, together with the parent itself. In order
1926 -- to compile the current context, we remove the one inherited from the
1927 -- parent, in order to have a clean visibility table. We restore the parent
1928 -- context before analyzing the proper body itself. On exit, we remove only
1929 -- the explicit context of the subunit.
1931 procedure Analyze_Subunit (N : Node_Id) is
1932 Lib_Unit : constant Node_Id := Library_Unit (N);
1933 Par_Unit : constant Entity_Id := Current_Scope;
1935 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1936 Num_Scopes : Int := 0;
1937 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1938 Enclosing_Child : Entity_Id := Empty;
1939 Svg : constant Suppress_Array := Scope_Suppress;
1941 procedure Analyze_Subunit_Context;
1942 -- Capture names in use clauses of the subunit. This must be done before
1943 -- re-installing parent declarations, because items in the context must
1944 -- not be hidden by declarations local to the parent.
1946 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1947 -- Recursive procedure to restore scope of all ancestors of subunit,
1948 -- from outermost in. If parent is not a subunit, the call to install
1949 -- context installs context of spec and (if parent is a child unit) the
1950 -- context of its parents as well. It is confusing that parents should
1951 -- be treated differently in both cases, but the semantics are just not
1952 -- identical.
1954 procedure Re_Install_Use_Clauses;
1955 -- As part of the removal of the parent scope, the use clauses are
1956 -- removed, to be reinstalled when the context of the subunit has been
1957 -- analyzed. Use clauses may also have been affected by the analysis of
1958 -- the context of the subunit, so they have to be applied again, to
1959 -- insure that the compilation environment of the rest of the parent
1960 -- unit is identical.
1962 procedure Remove_Scope;
1963 -- Remove current scope from scope stack, and preserve the list of use
1964 -- clauses in it, to be reinstalled after context is analyzed.
1966 -----------------------------
1967 -- Analyze_Subunit_Context --
1968 -----------------------------
1970 procedure Analyze_Subunit_Context is
1971 Item : Node_Id;
1972 Nam : Node_Id;
1973 Unit_Name : Entity_Id;
1975 begin
1976 Analyze_Context (N);
1978 -- Make withed units immediately visible. If child unit, make the
1979 -- ultimate parent immediately visible.
1981 Item := First (Context_Items (N));
1982 while Present (Item) loop
1983 if Nkind (Item) = N_With_Clause then
1985 -- Protect frontend against previous errors in context clauses
1987 if Nkind (Name (Item)) /= N_Selected_Component then
1988 if Error_Posted (Item) then
1989 null;
1991 else
1992 Unit_Name := Entity (Name (Item));
1993 while Is_Child_Unit (Unit_Name) loop
1994 Set_Is_Visible_Child_Unit (Unit_Name);
1995 Unit_Name := Scope (Unit_Name);
1996 end loop;
1998 if not Is_Immediately_Visible (Unit_Name) then
1999 Set_Is_Immediately_Visible (Unit_Name);
2000 Set_Context_Installed (Item);
2001 end if;
2002 end if;
2003 end if;
2005 elsif Nkind (Item) = N_Use_Package_Clause then
2006 Nam := First (Names (Item));
2007 while Present (Nam) loop
2008 Analyze (Nam);
2009 Next (Nam);
2010 end loop;
2012 elsif Nkind (Item) = N_Use_Type_Clause then
2013 Nam := First (Subtype_Marks (Item));
2014 while Present (Nam) loop
2015 Analyze (Nam);
2016 Next (Nam);
2017 end loop;
2018 end if;
2020 Next (Item);
2021 end loop;
2023 -- Reset visibility of withed units. They will be made visible again
2024 -- when we install the subunit context.
2026 Item := First (Context_Items (N));
2027 while Present (Item) loop
2028 if Nkind (Item) = N_With_Clause
2030 -- Protect frontend against previous errors in context clauses
2032 and then Nkind (Name (Item)) /= N_Selected_Component
2033 and then not Error_Posted (Item)
2034 then
2035 Unit_Name := Entity (Name (Item));
2036 while Is_Child_Unit (Unit_Name) loop
2037 Set_Is_Visible_Child_Unit (Unit_Name, False);
2038 Unit_Name := Scope (Unit_Name);
2039 end loop;
2041 if Context_Installed (Item) then
2042 Set_Is_Immediately_Visible (Unit_Name, False);
2043 Set_Context_Installed (Item, False);
2044 end if;
2045 end if;
2047 Next (Item);
2048 end loop;
2049 end Analyze_Subunit_Context;
2051 ------------------------
2052 -- Re_Install_Parents --
2053 ------------------------
2055 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2056 E : Entity_Id;
2058 begin
2059 if Nkind (Unit (L)) = N_Subunit then
2060 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2061 end if;
2063 Install_Context (L);
2065 -- If the subunit occurs within a child unit, we must restore the
2066 -- immediate visibility of any siblings that may occur in context.
2068 if Present (Enclosing_Child) then
2069 Install_Siblings (Enclosing_Child, L);
2070 end if;
2072 Push_Scope (Scop);
2074 if Scop /= Par_Unit then
2075 Set_Is_Immediately_Visible (Scop);
2076 end if;
2078 -- Make entities in scope visible again. For child units, restore
2079 -- visibility only if they are actually in context.
2081 E := First_Entity (Current_Scope);
2082 while Present (E) loop
2083 if not Is_Child_Unit (E)
2084 or else Is_Visible_Child_Unit (E)
2085 then
2086 Set_Is_Immediately_Visible (E);
2087 end if;
2089 Next_Entity (E);
2090 end loop;
2092 -- A subunit appears within a body, and for a nested subunits all the
2093 -- parents are bodies. Restore full visibility of their private
2094 -- entities.
2096 if Is_Package_Or_Generic_Package (Scop) then
2097 Set_In_Package_Body (Scop);
2098 Install_Private_Declarations (Scop);
2099 end if;
2100 end Re_Install_Parents;
2102 ----------------------------
2103 -- Re_Install_Use_Clauses --
2104 ----------------------------
2106 procedure Re_Install_Use_Clauses is
2107 U : Node_Id;
2108 begin
2109 for J in reverse 1 .. Num_Scopes loop
2110 U := Use_Clauses (J);
2111 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2112 Install_Use_Clauses (U, Force_Installation => True);
2113 end loop;
2114 end Re_Install_Use_Clauses;
2116 ------------------
2117 -- Remove_Scope --
2118 ------------------
2120 procedure Remove_Scope is
2121 E : Entity_Id;
2123 begin
2124 Num_Scopes := Num_Scopes + 1;
2125 Use_Clauses (Num_Scopes) :=
2126 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2128 E := First_Entity (Current_Scope);
2129 while Present (E) loop
2130 Set_Is_Immediately_Visible (E, False);
2131 Next_Entity (E);
2132 end loop;
2134 if Is_Child_Unit (Current_Scope) then
2135 Enclosing_Child := Current_Scope;
2136 end if;
2138 Pop_Scope;
2139 end Remove_Scope;
2141 -- Start of processing for Analyze_Subunit
2143 begin
2144 if Style_Check then
2145 declare
2146 Nam : Node_Id := Name (Unit (N));
2148 begin
2149 if Nkind (Nam) = N_Selected_Component then
2150 Nam := Selector_Name (Nam);
2151 end if;
2153 Check_Identifier (Nam, Par_Unit);
2154 end;
2155 end if;
2157 if not Is_Empty_List (Context_Items (N)) then
2159 -- Save current use clauses
2161 Remove_Scope;
2162 Remove_Context (Lib_Unit);
2164 -- Now remove parents and their context, including enclosing subunits
2165 -- and the outer parent body which is not a subunit.
2167 if Present (Lib_Spec) then
2168 Remove_Context (Lib_Spec);
2170 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2171 Lib_Spec := Library_Unit (Lib_Spec);
2172 Remove_Scope;
2173 Remove_Context (Lib_Spec);
2174 end loop;
2176 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2177 Remove_Scope;
2178 end if;
2180 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2181 Remove_Context (Library_Unit (Lib_Spec));
2182 end if;
2183 end if;
2185 Set_Is_Immediately_Visible (Par_Unit, False);
2187 Analyze_Subunit_Context;
2189 Re_Install_Parents (Lib_Unit, Par_Unit);
2190 Set_Is_Immediately_Visible (Par_Unit);
2192 -- If the context includes a child unit of the parent of the subunit,
2193 -- the parent will have been removed from visibility, after compiling
2194 -- that cousin in the context. The visibility of the parent must be
2195 -- restored now. This also applies if the context includes another
2196 -- subunit of the same parent which in turn includes a child unit in
2197 -- its context.
2199 if Is_Package_Or_Generic_Package (Par_Unit) then
2200 if not Is_Immediately_Visible (Par_Unit)
2201 or else (Present (First_Entity (Par_Unit))
2202 and then not Is_Immediately_Visible
2203 (First_Entity (Par_Unit)))
2204 then
2205 Set_Is_Immediately_Visible (Par_Unit);
2206 Install_Visible_Declarations (Par_Unit);
2207 Install_Private_Declarations (Par_Unit);
2208 end if;
2209 end if;
2211 Re_Install_Use_Clauses;
2212 Install_Context (N);
2214 -- Restore state of suppress flags for current body
2216 Scope_Suppress := Svg;
2218 -- If the subunit is within a child unit, then siblings of any parent
2219 -- unit that appear in the context clause of the subunit must also be
2220 -- made immediately visible.
2222 if Present (Enclosing_Child) then
2223 Install_Siblings (Enclosing_Child, N);
2224 end if;
2225 end if;
2227 Analyze (Proper_Body (Unit (N)));
2228 Remove_Context (N);
2230 -- The subunit may contain a with_clause on a sibling of some ancestor.
2231 -- Removing the context will remove from visibility those ancestor child
2232 -- units, which must be restored to the visibility they have in the
2233 -- enclosing body.
2235 if Present (Enclosing_Child) then
2236 declare
2237 C : Entity_Id;
2238 begin
2239 C := Current_Scope;
2240 while Present (C)
2241 and then Is_Child_Unit (C)
2242 loop
2243 Set_Is_Immediately_Visible (C);
2244 Set_Is_Visible_Child_Unit (C);
2245 C := Scope (C);
2246 end loop;
2247 end;
2248 end if;
2249 end Analyze_Subunit;
2251 ----------------------------
2252 -- Analyze_Task_Body_Stub --
2253 ----------------------------
2255 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2256 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2257 Loc : constant Source_Ptr := Sloc (N);
2259 begin
2260 Check_Stub_Level (N);
2262 -- First occurrence of name may have been as an incomplete type
2264 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2265 Nam := Full_View (Nam);
2266 end if;
2268 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2269 Error_Msg_N ("missing specification for task body", N);
2270 else
2271 Set_Scope (Defining_Entity (N), Current_Scope);
2272 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2274 -- Check for duplicate stub, if so give message and terminate
2276 if Has_Completion (Etype (Nam)) then
2277 Error_Msg_N ("duplicate stub for task", N);
2278 return;
2279 else
2280 Set_Has_Completion (Etype (Nam));
2281 end if;
2283 Analyze_Proper_Body (N, Etype (Nam));
2285 -- Set elaboration flag to indicate that entity is callable. This
2286 -- cannot be done in the expansion of the body itself, because the
2287 -- proper body is not in a declarative part. This is only done if
2288 -- expansion is active, because the context may be generic and the
2289 -- flag not defined yet.
2291 if Expander_Active then
2292 Insert_After (N,
2293 Make_Assignment_Statement (Loc,
2294 Name =>
2295 Make_Identifier (Loc,
2296 New_External_Name (Chars (Etype (Nam)), 'E')),
2297 Expression => New_Reference_To (Standard_True, Loc)));
2298 end if;
2299 end if;
2300 end Analyze_Task_Body_Stub;
2302 -------------------------
2303 -- Analyze_With_Clause --
2304 -------------------------
2306 -- Analyze the declaration of a unit in a with clause. At end, label the
2307 -- with clause with the defining entity for the unit.
2309 procedure Analyze_With_Clause (N : Node_Id) is
2311 -- Retrieve the original kind of the unit node, before analysis. If it
2312 -- is a subprogram instantiation, its analysis below will rewrite the
2313 -- node as the declaration of the wrapper package. If the same
2314 -- instantiation appears indirectly elsewhere in the context, it will
2315 -- have been analyzed already.
2317 Unit_Kind : constant Node_Kind :=
2318 Nkind (Original_Node (Unit (Library_Unit (N))));
2319 Nam : constant Node_Id := Name (N);
2320 E_Name : Entity_Id;
2321 Par_Name : Entity_Id;
2322 Pref : Node_Id;
2323 U : Node_Id;
2325 Intunit : Boolean;
2326 -- Set True if the unit currently being compiled is an internal unit
2328 Save_Style_Check : constant Boolean := Opt.Style_Check;
2329 Save_C_Restrict : Save_Cunit_Boolean_Restrictions;
2331 begin
2332 U := Unit (Library_Unit (N));
2334 -- If this is an internal unit which is a renaming, then this is a
2335 -- violation of No_Obsolescent_Features.
2337 -- Note: this is not quite right if the user defines one of these units
2338 -- himself, but that's a marginal case, and fixing it is hard ???
2340 if Restriction_Check_Required (No_Obsolescent_Features) then
2341 declare
2342 F : constant File_Name_Type :=
2343 Unit_File_Name (Get_Source_Unit (U));
2344 begin
2345 if Is_Predefined_File_Name (F, Renamings_Included => True)
2346 and then not
2347 Is_Predefined_File_Name (F, Renamings_Included => False)
2348 then
2349 Check_Restriction (No_Obsolescent_Features, N);
2350 end if;
2351 end;
2352 end if;
2354 -- Save current restriction set, does not apply to with'ed unit
2356 Save_C_Restrict := Cunit_Boolean_Restrictions_Save;
2358 -- Several actions are skipped for dummy packages (those supplied for
2359 -- with's where no matching file could be found). Such packages are
2360 -- identified by the Sloc value being set to No_Location.
2362 if Limited_Present (N) then
2364 -- Ada 2005 (AI-50217): Build visibility structures but do not
2365 -- analyze the unit.
2367 if Sloc (U) /= No_Location then
2368 Build_Limited_Views (N);
2369 end if;
2371 return;
2372 end if;
2374 -- We reset ordinary style checking during the analysis of a with'ed
2375 -- unit, but we do NOT reset GNAT special analysis mode (the latter
2376 -- definitely *does* apply to with'ed units).
2378 if not GNAT_Mode then
2379 Style_Check := False;
2380 end if;
2382 -- If the library unit is a predefined unit, and we are in high
2383 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2384 -- for the analysis of the with'ed unit. This mode does not prevent
2385 -- explicit with'ing of run-time units.
2387 if Configurable_Run_Time_Mode
2388 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (U)))
2389 then
2390 Configurable_Run_Time_Mode := False;
2391 Semantics (Library_Unit (N));
2392 Configurable_Run_Time_Mode := True;
2394 else
2395 Semantics (Library_Unit (N));
2396 end if;
2398 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2400 if Sloc (U) /= No_Location then
2402 -- Check restrictions, except that we skip the check if this is an
2403 -- internal unit unless we are compiling the internal unit as the
2404 -- main unit. We also skip this for dummy packages.
2406 Check_Restriction_No_Dependence (Nam, N);
2408 if not Intunit or else Current_Sem_Unit = Main_Unit then
2409 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2410 end if;
2412 -- Deal with special case of GNAT.Current_Exceptions which interacts
2413 -- with the optimization of local raise statements into gotos.
2415 if Nkind (Nam) = N_Selected_Component
2416 and then Nkind (Prefix (Nam)) = N_Identifier
2417 and then Chars (Prefix (Nam)) = Name_Gnat
2418 and then (Chars (Selector_Name (Nam)) = Name_Most_Recent_Exception
2419 or else
2420 Chars (Selector_Name (Nam)) = Name_Exception_Traces)
2421 then
2422 Check_Restriction (No_Exception_Propagation, N);
2423 Special_Exception_Package_Used := True;
2424 end if;
2426 -- Check for inappropriate with of internal implementation unit if we
2427 -- are not compiling an internal unit. We do not issue this message
2428 -- for implicit with's generated by the compiler itself.
2430 if Implementation_Unit_Warnings
2431 and then not Intunit
2432 and then not Implicit_With (N)
2433 then
2434 declare
2435 U_Kind : constant Kind_Of_Unit :=
2436 Get_Kind_Of_Unit (Get_Source_Unit (U));
2438 begin
2439 if U_Kind = Implementation_Unit then
2440 Error_Msg_F ("& is an internal 'G'N'A'T unit?", Name (N));
2442 -- Add alternative name if available, otherwise issue a
2443 -- general warning message.
2445 if Error_Msg_Strlen /= 0 then
2446 Error_Msg_F ("\use ""~"" instead", Name (N));
2447 else
2448 Error_Msg_F
2449 ("\use of this unit is non-portable " &
2450 "and version-dependent?", Name (N));
2451 end if;
2453 elsif U_Kind = Ada_2005_Unit
2454 and then Ada_Version < Ada_2005
2455 and then Warn_On_Ada_2005_Compatibility
2456 then
2457 Error_Msg_N ("& is an Ada 2005 unit?", Name (N));
2459 elsif U_Kind = Ada_2012_Unit
2460 and then Ada_Version < Ada_2012
2461 and then Warn_On_Ada_2012_Compatibility
2462 then
2463 Error_Msg_N ("& is an Ada 2012 unit?", Name (N));
2464 end if;
2465 end;
2466 end if;
2467 end if;
2469 -- Semantic analysis of a generic unit is performed on a copy of
2470 -- the original tree. Retrieve the entity on which semantic info
2471 -- actually appears.
2473 if Unit_Kind in N_Generic_Declaration then
2474 E_Name := Defining_Entity (U);
2476 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2477 -- the case of an instantiation, semantic analysis above will have
2478 -- replaced the unit by its instantiated version. If the instance body
2479 -- has been generated, the instance now denotes the body entity. For
2480 -- visibility purposes we need the entity of its spec.
2482 elsif (Unit_Kind = N_Package_Instantiation
2483 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2484 N_Package_Instantiation)
2485 and then Nkind (U) = N_Package_Body
2486 then
2487 E_Name := Corresponding_Spec (U);
2489 elsif Unit_Kind = N_Package_Instantiation
2490 and then Nkind (U) = N_Package_Instantiation
2491 and then Present (Instance_Spec (U))
2492 then
2493 -- If the instance has not been rewritten as a package declaration,
2494 -- then it appeared already in a previous with clause. Retrieve
2495 -- the entity from the previous instance.
2497 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2499 elsif Unit_Kind in N_Subprogram_Instantiation then
2501 -- The visible subprogram is created during instantiation, and is
2502 -- an attribute of the wrapper package. We retrieve the wrapper
2503 -- package directly from the instantiation node. If the instance
2504 -- is inlined the unit is still an instantiation. Otherwise it has
2505 -- been rewritten as the declaration of the wrapper itself.
2507 if Nkind (U) in N_Subprogram_Instantiation then
2508 E_Name :=
2509 Related_Instance
2510 (Defining_Entity (Specification (Instance_Spec (U))));
2511 else
2512 E_Name := Related_Instance (Defining_Entity (U));
2513 end if;
2515 elsif Unit_Kind = N_Package_Renaming_Declaration
2516 or else Unit_Kind in N_Generic_Renaming_Declaration
2517 then
2518 E_Name := Defining_Entity (U);
2520 elsif Unit_Kind = N_Subprogram_Body
2521 and then Nkind (Name (N)) = N_Selected_Component
2522 and then not Acts_As_Spec (Library_Unit (N))
2523 then
2524 -- For a child unit that has no spec, one has been created and
2525 -- analyzed. The entity required is that of the spec.
2527 E_Name := Corresponding_Spec (U);
2529 else
2530 E_Name := Defining_Entity (U);
2531 end if;
2533 if Nkind (Name (N)) = N_Selected_Component then
2535 -- Child unit in a with clause
2537 Change_Selected_Component_To_Expanded_Name (Name (N));
2538 end if;
2540 -- Restore style checks and restrictions
2542 Style_Check := Save_Style_Check;
2543 Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
2545 -- Record the reference, but do NOT set the unit as referenced, we want
2546 -- to consider the unit as unreferenced if this is the only reference
2547 -- that occurs.
2549 Set_Entity_With_Style_Check (Name (N), E_Name);
2550 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2552 -- Generate references and check No_Dependence restriction for parents
2554 if Is_Child_Unit (E_Name) then
2555 Pref := Prefix (Name (N));
2556 Par_Name := Scope (E_Name);
2557 while Nkind (Pref) = N_Selected_Component loop
2558 Change_Selected_Component_To_Expanded_Name (Pref);
2559 Set_Entity_With_Style_Check (Pref, Par_Name);
2561 Generate_Reference (Par_Name, Pref);
2562 Check_Restriction_No_Dependence (Pref, N);
2563 Pref := Prefix (Pref);
2565 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2566 -- is set to Standard_Standard, and no attempt should be made to
2567 -- further unwind scopes.
2569 if Par_Name /= Standard_Standard then
2570 Par_Name := Scope (Par_Name);
2571 end if;
2572 end loop;
2574 if Present (Entity (Pref))
2575 and then not Analyzed (Parent (Parent (Entity (Pref))))
2576 then
2577 -- If the entity is set without its unit being compiled, the
2578 -- original parent is a renaming, and Par_Name is the renamed
2579 -- entity. For visibility purposes, we need the original entity,
2580 -- which must be analyzed now because Load_Unit directly retrieves
2581 -- the renamed unit, and the renaming declaration itself has not
2582 -- been analyzed.
2584 Analyze (Parent (Parent (Entity (Pref))));
2585 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2586 Par_Name := Entity (Pref);
2587 end if;
2589 Set_Entity_With_Style_Check (Pref, Par_Name);
2590 Generate_Reference (Par_Name, Pref);
2591 end if;
2593 -- If the withed unit is System, and a system extension pragma is
2594 -- present, compile the extension now, rather than waiting for a
2595 -- visibility check on a specific entity.
2597 if Chars (E_Name) = Name_System
2598 and then Scope (E_Name) = Standard_Standard
2599 and then Present (System_Extend_Unit)
2600 and then Present_System_Aux (N)
2601 then
2602 -- If the extension is not present, an error will have been emitted
2604 null;
2605 end if;
2607 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2608 -- to private_with units; they will be made visible later (just before
2609 -- the private part is analyzed)
2611 if Private_Present (N) then
2612 Set_Is_Immediately_Visible (E_Name, False);
2613 end if;
2614 end Analyze_With_Clause;
2616 ------------------------------
2617 -- Check_Private_Child_Unit --
2618 ------------------------------
2620 procedure Check_Private_Child_Unit (N : Node_Id) is
2621 Lib_Unit : constant Node_Id := Unit (N);
2622 Item : Node_Id;
2623 Curr_Unit : Entity_Id;
2624 Sub_Parent : Node_Id;
2625 Priv_Child : Entity_Id;
2626 Par_Lib : Entity_Id;
2627 Par_Spec : Node_Id;
2629 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2630 -- Returns true if and only if the library unit is declared with
2631 -- an explicit designation of private.
2633 -----------------------------
2634 -- Is_Private_Library_Unit --
2635 -----------------------------
2637 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2638 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2640 begin
2641 return Private_Present (Comp_Unit);
2642 end Is_Private_Library_Unit;
2644 -- Start of processing for Check_Private_Child_Unit
2646 begin
2647 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2648 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2649 Par_Lib := Curr_Unit;
2651 elsif Nkind (Lib_Unit) = N_Subunit then
2653 -- The parent is itself a body. The parent entity is to be found in
2654 -- the corresponding spec.
2656 Sub_Parent := Library_Unit (N);
2657 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2659 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2660 -- enclosing body, retrieve the spec entity which is the proper
2661 -- ancestor we need for the following tests.
2663 if Ekind (Curr_Unit) = E_Package_Body then
2664 Curr_Unit := Spec_Entity (Curr_Unit);
2665 end if;
2667 Par_Lib := Curr_Unit;
2669 else
2670 Curr_Unit := Defining_Entity (Lib_Unit);
2672 Par_Lib := Curr_Unit;
2673 Par_Spec := Parent_Spec (Lib_Unit);
2675 if No (Par_Spec) then
2676 Par_Lib := Empty;
2677 else
2678 Par_Lib := Defining_Entity (Unit (Par_Spec));
2679 end if;
2680 end if;
2682 -- Loop through context items
2684 Item := First (Context_Items (N));
2685 while Present (Item) loop
2687 -- Ada 2005 (AI-262): Allow private_with of a private child package
2688 -- in public siblings
2690 if Nkind (Item) = N_With_Clause
2691 and then not Implicit_With (Item)
2692 and then not Limited_Present (Item)
2693 and then Is_Private_Descendant (Entity (Name (Item)))
2694 then
2695 Priv_Child := Entity (Name (Item));
2697 declare
2698 Curr_Parent : Entity_Id := Par_Lib;
2699 Child_Parent : Entity_Id := Scope (Priv_Child);
2700 Prv_Ancestor : Entity_Id := Child_Parent;
2701 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2703 begin
2704 -- If the child unit is a public child then locate the nearest
2705 -- private ancestor. Child_Parent will then be set to the
2706 -- parent of that ancestor.
2708 if not Is_Private_Library_Unit (Priv_Child) then
2709 while Present (Prv_Ancestor)
2710 and then not Is_Private_Library_Unit (Prv_Ancestor)
2711 loop
2712 Prv_Ancestor := Scope (Prv_Ancestor);
2713 end loop;
2715 if Present (Prv_Ancestor) then
2716 Child_Parent := Scope (Prv_Ancestor);
2717 end if;
2718 end if;
2720 while Present (Curr_Parent)
2721 and then Curr_Parent /= Standard_Standard
2722 and then Curr_Parent /= Child_Parent
2723 loop
2724 Curr_Private :=
2725 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2726 Curr_Parent := Scope (Curr_Parent);
2727 end loop;
2729 if No (Curr_Parent) then
2730 Curr_Parent := Standard_Standard;
2731 end if;
2733 if Curr_Parent /= Child_Parent then
2734 if Ekind (Priv_Child) = E_Generic_Package
2735 and then Chars (Priv_Child) in Text_IO_Package_Name
2736 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2737 then
2738 Error_Msg_NE
2739 ("& is a nested package, not a compilation unit",
2740 Name (Item), Priv_Child);
2742 else
2743 Error_Msg_N
2744 ("unit in with clause is private child unit!", Item);
2745 Error_Msg_NE
2746 ("\current unit must also have parent&!",
2747 Item, Child_Parent);
2748 end if;
2750 elsif Curr_Private
2751 or else Private_Present (Item)
2752 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2753 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2754 and then not Acts_As_Spec (Parent (Lib_Unit)))
2755 then
2756 null;
2758 else
2759 Error_Msg_NE
2760 ("current unit must also be private descendant of&",
2761 Item, Child_Parent);
2762 end if;
2763 end;
2764 end if;
2766 Next (Item);
2767 end loop;
2769 end Check_Private_Child_Unit;
2771 ----------------------
2772 -- Check_Stub_Level --
2773 ----------------------
2775 procedure Check_Stub_Level (N : Node_Id) is
2776 Par : constant Node_Id := Parent (N);
2777 Kind : constant Node_Kind := Nkind (Par);
2779 begin
2780 if Nkind_In (Kind, N_Package_Body,
2781 N_Subprogram_Body,
2782 N_Task_Body,
2783 N_Protected_Body)
2784 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
2785 then
2786 null;
2788 -- In an instance, a missing stub appears at any level. A warning
2789 -- message will have been emitted already for the missing file.
2791 elsif not In_Instance then
2792 Error_Msg_N ("stub cannot appear in an inner scope", N);
2794 elsif Expander_Active then
2795 Error_Msg_N ("missing proper body", N);
2796 end if;
2797 end Check_Stub_Level;
2799 ------------------------
2800 -- Expand_With_Clause --
2801 ------------------------
2803 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2804 Loc : constant Source_Ptr := Sloc (Nam);
2805 Ent : constant Entity_Id := Entity (Nam);
2806 Withn : Node_Id;
2807 P : Node_Id;
2809 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2810 -- Build name to be used in implicit with_clause. In most cases this
2811 -- is the source name, but if renamings are present we must make the
2812 -- original unit visible, not the one it renames. The entity in the
2813 -- with clause is the renamed unit, but the identifier is the one from
2814 -- the source, which allows us to recover the unit renaming.
2816 ---------------------
2817 -- Build_Unit_Name --
2818 ---------------------
2820 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2821 Ent : Entity_Id;
2822 Renaming : Entity_Id;
2823 Result : Node_Id;
2825 begin
2826 if Nkind (Nam) = N_Identifier then
2828 -- If the parent unit P in the name of the with_clause for P.Q is
2829 -- a renaming of package R, then the entity of the parent is set
2830 -- to R, but the identifier retains Chars (P) to be consistent
2831 -- with the source (see details in lib-load). However the implicit
2832 -- with_clause for the parent must make the entity for P visible,
2833 -- because P.Q may be used as a prefix within the current unit.
2834 -- The entity for P is the current_entity with that name, because
2835 -- the package renaming declaration for it has just been analyzed.
2836 -- Note that this case can only happen if P.Q has already appeared
2837 -- in a previous with_clause in a related unit, such as the
2838 -- library body of the current unit.
2840 if Chars (Nam) /= Chars (Entity (Nam)) then
2841 Renaming := Current_Entity (Nam);
2842 pragma Assert (Renamed_Entity (Renaming) = Entity (Nam));
2843 return New_Occurrence_Of (Renaming, Loc);
2845 else
2846 return New_Occurrence_Of (Entity (Nam), Loc);
2847 end if;
2849 else
2850 Ent := Entity (Nam);
2852 if Present (Entity (Selector_Name (Nam)))
2853 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
2854 and then
2855 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
2856 = N_Package_Renaming_Declaration
2857 then
2858 -- The name in the with_clause is of the form A.B.C, and B is
2859 -- given by a renaming declaration. In that case we may not
2860 -- have analyzed the unit for B, but replaced it directly in
2861 -- lib-load with the unit it renames. We have to make A.B
2862 -- visible, so analyze the declaration for B now, in case it
2863 -- has not been done yet.
2865 Ent := Entity (Selector_Name (Nam));
2866 Analyze
2867 (Parent
2868 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
2869 end if;
2871 Result :=
2872 Make_Expanded_Name (Loc,
2873 Chars => Chars (Entity (Nam)),
2874 Prefix => Build_Unit_Name (Prefix (Nam)),
2875 Selector_Name => New_Occurrence_Of (Ent, Loc));
2876 Set_Entity (Result, Ent);
2877 return Result;
2878 end if;
2879 end Build_Unit_Name;
2881 -- Start of processing for Expand_With_Clause
2883 begin
2884 New_Nodes_OK := New_Nodes_OK + 1;
2885 Withn :=
2886 Make_With_Clause (Loc,
2887 Name => Build_Unit_Name (Nam));
2889 P := Parent (Unit_Declaration_Node (Ent));
2890 Set_Library_Unit (Withn, P);
2891 Set_Corresponding_Spec (Withn, Ent);
2892 Set_First_Name (Withn, True);
2893 Set_Implicit_With (Withn, True);
2895 -- If the unit is a package declaration, a private_with_clause on a
2896 -- child unit implies the implicit with on the parent is also private.
2898 if Nkind (Unit (N)) = N_Package_Declaration then
2899 Set_Private_Present (Withn, Private_Present (Item));
2900 end if;
2902 Prepend (Withn, Context_Items (N));
2903 Mark_Rewrite_Insertion (Withn);
2904 Install_Withed_Unit (Withn);
2906 if Nkind (Nam) = N_Expanded_Name then
2907 Expand_With_Clause (Item, Prefix (Nam), N);
2908 end if;
2910 New_Nodes_OK := New_Nodes_OK - 1;
2911 end Expand_With_Clause;
2913 -----------------------
2914 -- Get_Parent_Entity --
2915 -----------------------
2917 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2918 begin
2919 if Nkind (Unit) = N_Package_Body
2920 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
2921 then
2922 return Defining_Entity
2923 (Specification (Instance_Spec (Original_Node (Unit))));
2924 elsif Nkind (Unit) = N_Package_Instantiation then
2925 return Defining_Entity (Specification (Instance_Spec (Unit)));
2926 else
2927 return Defining_Entity (Unit);
2928 end if;
2929 end Get_Parent_Entity;
2931 ---------------------
2932 -- Has_With_Clause --
2933 ---------------------
2935 function Has_With_Clause
2936 (C_Unit : Node_Id;
2937 Pack : Entity_Id;
2938 Is_Limited : Boolean := False) return Boolean
2940 Item : Node_Id;
2942 function Named_Unit (Clause : Node_Id) return Entity_Id;
2943 -- Return the entity for the unit named in a [limited] with clause
2945 ----------------
2946 -- Named_Unit --
2947 ----------------
2949 function Named_Unit (Clause : Node_Id) return Entity_Id is
2950 begin
2951 if Nkind (Name (Clause)) = N_Selected_Component then
2952 return Entity (Selector_Name (Name (Clause)));
2953 else
2954 return Entity (Name (Clause));
2955 end if;
2956 end Named_Unit;
2958 -- Start of processing for Has_With_Clause
2960 begin
2961 if Present (Context_Items (C_Unit)) then
2962 Item := First (Context_Items (C_Unit));
2963 while Present (Item) loop
2964 if Nkind (Item) = N_With_Clause
2965 and then Limited_Present (Item) = Is_Limited
2966 and then Named_Unit (Item) = Pack
2967 then
2968 return True;
2969 end if;
2971 Next (Item);
2972 end loop;
2973 end if;
2975 return False;
2976 end Has_With_Clause;
2978 -----------------------------
2979 -- Implicit_With_On_Parent --
2980 -----------------------------
2982 procedure Implicit_With_On_Parent
2983 (Child_Unit : Node_Id;
2984 N : Node_Id)
2986 Loc : constant Source_Ptr := Sloc (N);
2987 P : constant Node_Id := Parent_Spec (Child_Unit);
2988 P_Unit : Node_Id := Unit (P);
2989 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
2990 Withn : Node_Id;
2992 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2993 -- Build prefix of child unit name. Recurse if needed
2995 function Build_Unit_Name return Node_Id;
2996 -- If the unit is a child unit, build qualified name with all ancestors
2998 -------------------------
2999 -- Build_Ancestor_Name --
3000 -------------------------
3002 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3003 P_Ref : constant Node_Id :=
3004 New_Reference_To (Defining_Entity (P), Loc);
3005 P_Spec : Node_Id := P;
3007 begin
3008 -- Ancestor may have been rewritten as a package body. Retrieve
3009 -- the original spec to trace earlier ancestors.
3011 if Nkind (P) = N_Package_Body
3012 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3013 then
3014 P_Spec := Original_Node (P);
3015 end if;
3017 if No (Parent_Spec (P_Spec)) then
3018 return P_Ref;
3019 else
3020 return
3021 Make_Selected_Component (Loc,
3022 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3023 Selector_Name => P_Ref);
3024 end if;
3025 end Build_Ancestor_Name;
3027 ---------------------
3028 -- Build_Unit_Name --
3029 ---------------------
3031 function Build_Unit_Name return Node_Id is
3032 Result : Node_Id;
3034 begin
3035 if No (Parent_Spec (P_Unit)) then
3036 return New_Reference_To (P_Name, Loc);
3038 else
3039 Result :=
3040 Make_Expanded_Name (Loc,
3041 Chars => Chars (P_Name),
3042 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3043 Selector_Name => New_Reference_To (P_Name, Loc));
3044 Set_Entity (Result, P_Name);
3045 return Result;
3046 end if;
3047 end Build_Unit_Name;
3049 -- Start of processing for Implicit_With_On_Parent
3051 begin
3052 -- The unit of the current compilation may be a package body that
3053 -- replaces an instance node. In this case we need the original instance
3054 -- node to construct the proper parent name.
3056 if Nkind (P_Unit) = N_Package_Body
3057 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3058 then
3059 P_Unit := Original_Node (P_Unit);
3060 end if;
3062 -- We add the implicit with if the child unit is the current unit being
3063 -- compiled. If the current unit is a body, we do not want to add an
3064 -- implicit_with a second time to the corresponding spec.
3066 if Nkind (Child_Unit) = N_Package_Declaration
3067 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3068 then
3069 return;
3070 end if;
3072 New_Nodes_OK := New_Nodes_OK + 1;
3073 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3075 Set_Library_Unit (Withn, P);
3076 Set_Corresponding_Spec (Withn, P_Name);
3077 Set_First_Name (Withn, True);
3078 Set_Implicit_With (Withn, True);
3080 -- Node is placed at the beginning of the context items, so that
3081 -- subsequent use clauses on the parent can be validated.
3083 Prepend (Withn, Context_Items (N));
3084 Mark_Rewrite_Insertion (Withn);
3085 Install_Withed_Unit (Withn);
3087 if Is_Child_Spec (P_Unit) then
3088 Implicit_With_On_Parent (P_Unit, N);
3089 end if;
3091 New_Nodes_OK := New_Nodes_OK - 1;
3092 end Implicit_With_On_Parent;
3094 --------------
3095 -- In_Chain --
3096 --------------
3098 function In_Chain (E : Entity_Id) return Boolean is
3099 H : Entity_Id;
3101 begin
3102 H := Current_Entity (E);
3103 while Present (H) loop
3104 if H = E then
3105 return True;
3106 else
3107 H := Homonym (H);
3108 end if;
3109 end loop;
3111 return False;
3112 end In_Chain;
3114 ---------------------
3115 -- Install_Context --
3116 ---------------------
3118 procedure Install_Context (N : Node_Id) is
3119 Lib_Unit : constant Node_Id := Unit (N);
3121 begin
3122 Install_Context_Clauses (N);
3124 if Is_Child_Spec (Lib_Unit) then
3125 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3126 end if;
3128 Install_Limited_Context_Clauses (N);
3129 end Install_Context;
3131 -----------------------------
3132 -- Install_Context_Clauses --
3133 -----------------------------
3135 procedure Install_Context_Clauses (N : Node_Id) is
3136 Lib_Unit : constant Node_Id := Unit (N);
3137 Item : Node_Id;
3138 Uname_Node : Entity_Id;
3139 Check_Private : Boolean := False;
3140 Decl_Node : Node_Id;
3141 Lib_Parent : Entity_Id;
3143 begin
3144 -- First skip configuration pragmas at the start of the context. They
3145 -- are not technically part of the context clause, but that's where the
3146 -- parser puts them. Note they were analyzed in Analyze_Context.
3148 Item := First (Context_Items (N));
3149 while Present (Item)
3150 and then Nkind (Item) = N_Pragma
3151 and then Pragma_Name (Item) in Configuration_Pragma_Names
3152 loop
3153 Next (Item);
3154 end loop;
3156 -- Loop through the actual context clause items. We process everything
3157 -- except Limited_With clauses in this routine. Limited_With clauses
3158 -- are separately installed (see Install_Limited_Context_Clauses).
3160 while Present (Item) loop
3162 -- Case of explicit WITH clause
3164 if Nkind (Item) = N_With_Clause
3165 and then not Implicit_With (Item)
3166 then
3167 if Limited_Present (Item) then
3169 -- Limited withed units will be installed later
3171 goto Continue;
3173 -- If Name (Item) is not an entity name, something is wrong, and
3174 -- this will be detected in due course, for now ignore the item
3176 elsif not Is_Entity_Name (Name (Item)) then
3177 goto Continue;
3179 elsif No (Entity (Name (Item))) then
3180 Set_Entity (Name (Item), Any_Id);
3181 goto Continue;
3182 end if;
3184 Uname_Node := Entity (Name (Item));
3186 if Is_Private_Descendant (Uname_Node) then
3187 Check_Private := True;
3188 end if;
3190 Install_Withed_Unit (Item);
3192 Decl_Node := Unit_Declaration_Node (Uname_Node);
3194 -- If the unit is a subprogram instance, it appears nested within
3195 -- a package that carries the parent information.
3197 if Is_Generic_Instance (Uname_Node)
3198 and then Ekind (Uname_Node) /= E_Package
3199 then
3200 Decl_Node := Parent (Parent (Decl_Node));
3201 end if;
3203 if Is_Child_Spec (Decl_Node) then
3204 if Nkind (Name (Item)) = N_Expanded_Name then
3205 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3206 else
3207 -- If not an expanded name, the child unit must be a
3208 -- renaming, nothing to do.
3210 null;
3211 end if;
3213 elsif Nkind (Decl_Node) = N_Subprogram_Body
3214 and then not Acts_As_Spec (Parent (Decl_Node))
3215 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3216 then
3217 Implicit_With_On_Parent
3218 (Unit (Library_Unit (Parent (Decl_Node))), N);
3219 end if;
3221 -- Check license conditions unless this is a dummy unit
3223 if Sloc (Library_Unit (Item)) /= No_Location then
3224 License_Check : declare
3225 Withu : constant Unit_Number_Type :=
3226 Get_Source_Unit (Library_Unit (Item));
3227 Withl : constant License_Type :=
3228 License (Source_Index (Withu));
3229 Unitl : constant License_Type :=
3230 License (Source_Index (Current_Sem_Unit));
3232 procedure License_Error;
3233 -- Signal error of bad license
3235 -------------------
3236 -- License_Error --
3237 -------------------
3239 procedure License_Error is
3240 begin
3241 Error_Msg_N
3242 ("?license of with'ed unit & may be inconsistent",
3243 Name (Item));
3244 end License_Error;
3246 -- Start of processing for License_Check
3248 begin
3249 -- Exclude license check if withed unit is an internal unit.
3250 -- This situation arises e.g. with the GPL version of GNAT.
3252 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3253 null;
3255 -- Otherwise check various cases
3256 else
3257 case Unitl is
3258 when Unknown =>
3259 null;
3261 when Restricted =>
3262 if Withl = GPL then
3263 License_Error;
3264 end if;
3266 when GPL =>
3267 if Withl = Restricted then
3268 License_Error;
3269 end if;
3271 when Modified_GPL =>
3272 if Withl = Restricted or else Withl = GPL then
3273 License_Error;
3274 end if;
3276 when Unrestricted =>
3277 null;
3278 end case;
3279 end if;
3280 end License_Check;
3281 end if;
3283 -- Case of USE PACKAGE clause
3285 elsif Nkind (Item) = N_Use_Package_Clause then
3286 Analyze_Use_Package (Item);
3288 -- Case of USE TYPE clause
3290 elsif Nkind (Item) = N_Use_Type_Clause then
3291 Analyze_Use_Type (Item);
3293 -- case of PRAGMA
3295 elsif Nkind (Item) = N_Pragma then
3296 Analyze (Item);
3297 end if;
3299 <<Continue>>
3300 Next (Item);
3301 end loop;
3303 if Is_Child_Spec (Lib_Unit) then
3305 -- The unit also has implicit with_clauses on its own parents
3307 if No (Context_Items (N)) then
3308 Set_Context_Items (N, New_List);
3309 end if;
3311 Implicit_With_On_Parent (Lib_Unit, N);
3312 end if;
3314 -- If the unit is a body, the context of the specification must also
3315 -- be installed. That includes private with_clauses in that context.
3317 if Nkind (Lib_Unit) = N_Package_Body
3318 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3319 and then not Acts_As_Spec (N))
3320 then
3321 Install_Context (Library_Unit (N));
3323 -- Only install private with-clauses of a spec that comes from
3324 -- source, excluding specs created for a subprogram body that is
3325 -- a child unit.
3327 if Comes_From_Source (Library_Unit (N)) then
3328 Install_Private_With_Clauses
3329 (Defining_Entity (Unit (Library_Unit (N))));
3330 end if;
3332 if Is_Child_Spec (Unit (Library_Unit (N))) then
3334 -- If the unit is the body of a public child unit, the private
3335 -- declarations of the parent must be made visible. If the child
3336 -- unit is private, the private declarations have been installed
3337 -- already in the call to Install_Parents for the spec. Installing
3338 -- private declarations must be done for all ancestors of public
3339 -- child units. In addition, sibling units mentioned in the
3340 -- context clause of the body are directly visible.
3342 declare
3343 Lib_Spec : Node_Id;
3344 P : Node_Id;
3345 P_Name : Entity_Id;
3347 begin
3348 Lib_Spec := Unit (Library_Unit (N));
3349 while Is_Child_Spec (Lib_Spec) loop
3350 P := Unit (Parent_Spec (Lib_Spec));
3351 P_Name := Defining_Entity (P);
3353 if not (Private_Present (Parent (Lib_Spec)))
3354 and then not In_Private_Part (P_Name)
3355 then
3356 Install_Private_Declarations (P_Name);
3357 Install_Private_With_Clauses (P_Name);
3358 Set_Use (Private_Declarations (Specification (P)));
3359 end if;
3361 Lib_Spec := P;
3362 end loop;
3363 end;
3364 end if;
3366 -- For a package body, children in context are immediately visible
3368 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3369 end if;
3371 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3372 N_Generic_Subprogram_Declaration,
3373 N_Package_Declaration,
3374 N_Subprogram_Declaration)
3375 then
3376 if Is_Child_Spec (Lib_Unit) then
3377 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3378 Set_Is_Private_Descendant
3379 (Defining_Entity (Lib_Unit),
3380 Is_Private_Descendant (Lib_Parent)
3381 or else Private_Present (Parent (Lib_Unit)));
3383 else
3384 Set_Is_Private_Descendant
3385 (Defining_Entity (Lib_Unit),
3386 Private_Present (Parent (Lib_Unit)));
3387 end if;
3388 end if;
3390 if Check_Private then
3391 Check_Private_Child_Unit (N);
3392 end if;
3393 end Install_Context_Clauses;
3395 -------------------------------------
3396 -- Install_Limited_Context_Clauses --
3397 -------------------------------------
3399 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3400 Item : Node_Id;
3402 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3403 -- Check that the unlimited view of a given compilation_unit is not
3404 -- already visible through "use + renamings".
3406 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3407 -- Check that if a limited_with clause of a given compilation_unit
3408 -- mentions a descendant of a private child of some library unit, then
3409 -- the given compilation_unit shall be the declaration of a private
3410 -- descendant of that library unit, or a public descendant of such. The
3411 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3412 -- use entities on the limited with_clauses because their units have not
3413 -- been analyzed, so we have to climb the tree of ancestors looking for
3414 -- private keywords.
3416 procedure Expand_Limited_With_Clause
3417 (Comp_Unit : Node_Id;
3418 Nam : Node_Id;
3419 N : Node_Id);
3420 -- If a child unit appears in a limited_with clause, there are implicit
3421 -- limited_with clauses on all parents that are not already visible
3422 -- through a regular with clause. This procedure creates the implicit
3423 -- limited with_clauses for the parents and loads the corresponding
3424 -- units. The shadow entities are created when the inserted clause is
3425 -- analyzed. Implements Ada 2005 (AI-50217).
3427 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
3428 -- When compiling a unit Q descended from some parent unit P, a limited
3429 -- with_clause in the context of P that names some other ancestor of Q
3430 -- must not be installed because the ancestor is immediately visible.
3432 ---------------------
3433 -- Check_Renamings --
3434 ---------------------
3436 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3437 Item : Node_Id;
3438 Spec : Node_Id;
3439 WEnt : Entity_Id;
3440 Nam : Node_Id;
3441 E : Entity_Id;
3442 E2 : Entity_Id;
3444 begin
3445 pragma Assert (Nkind (W) = N_With_Clause);
3447 -- Protect the frontend against previous critical errors
3449 case Nkind (Unit (Library_Unit (W))) is
3450 when N_Subprogram_Declaration |
3451 N_Package_Declaration |
3452 N_Generic_Subprogram_Declaration |
3453 N_Generic_Package_Declaration =>
3454 null;
3456 when others =>
3457 return;
3458 end case;
3460 -- Check "use + renamings"
3462 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3463 Spec := Specification (Unit (P));
3465 Item := First (Visible_Declarations (Spec));
3466 while Present (Item) loop
3468 -- Look only at use package clauses
3470 if Nkind (Item) = N_Use_Package_Clause then
3472 -- Traverse the list of packages
3474 Nam := First (Names (Item));
3475 while Present (Nam) loop
3476 E := Entity (Nam);
3478 pragma Assert (Present (Parent (E)));
3480 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3481 and then Renamed_Entity (E) = WEnt
3482 then
3483 -- The unlimited view is visible through use clause and
3484 -- renamings. There is no need to generate the error
3485 -- message here because Is_Visible_Through_Renamings
3486 -- takes care of generating the precise error message.
3488 return;
3490 elsif Nkind (Parent (E)) = N_Package_Specification then
3492 -- The use clause may refer to a local package.
3493 -- Check all the enclosing scopes.
3495 E2 := E;
3496 while E2 /= Standard_Standard
3497 and then E2 /= WEnt
3498 loop
3499 E2 := Scope (E2);
3500 end loop;
3502 if E2 = WEnt then
3503 Error_Msg_N
3504 ("unlimited view visible through use clause ", W);
3505 return;
3506 end if;
3507 end if;
3509 Next (Nam);
3510 end loop;
3511 end if;
3513 Next (Item);
3514 end loop;
3516 -- Recursive call to check all the ancestors
3518 if Is_Child_Spec (Unit (P)) then
3519 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3520 end if;
3521 end Check_Renamings;
3523 ---------------------------------------
3524 -- Check_Private_Limited_Withed_Unit --
3525 ---------------------------------------
3527 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3528 Curr_Parent : Node_Id;
3529 Child_Parent : Node_Id;
3530 Curr_Private : Boolean;
3532 begin
3533 -- Compilation unit of the parent of the withed library unit
3535 Child_Parent := Library_Unit (Item);
3537 -- If the child unit is a public child, then locate its nearest
3538 -- private ancestor, if any, then Child_Parent will then be set to
3539 -- the parent of that ancestor.
3541 if not Private_Present (Library_Unit (Item)) then
3542 while Present (Child_Parent)
3543 and then not Private_Present (Child_Parent)
3544 loop
3545 Child_Parent := Parent_Spec (Unit (Child_Parent));
3546 end loop;
3548 if No (Child_Parent) then
3549 return;
3550 end if;
3551 end if;
3553 Child_Parent := Parent_Spec (Unit (Child_Parent));
3555 -- Traverse all the ancestors of the current compilation unit to
3556 -- check if it is a descendant of named library unit.
3558 Curr_Parent := Parent (Item);
3559 Curr_Private := Private_Present (Curr_Parent);
3561 while Present (Parent_Spec (Unit (Curr_Parent)))
3562 and then Curr_Parent /= Child_Parent
3563 loop
3564 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3565 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3566 end loop;
3568 if Curr_Parent /= Child_Parent then
3569 Error_Msg_N
3570 ("unit in with clause is private child unit!", Item);
3571 Error_Msg_NE
3572 ("\current unit must also have parent&!",
3573 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3575 elsif Private_Present (Parent (Item))
3576 or else Curr_Private
3577 or else Private_Present (Item)
3578 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3579 N_Subprogram_Body,
3580 N_Subunit)
3581 then
3582 -- Current unit is private, of descendant of a private unit
3584 null;
3586 else
3587 Error_Msg_NE
3588 ("current unit must also be private descendant of&",
3589 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3590 end if;
3591 end Check_Private_Limited_Withed_Unit;
3593 --------------------------------
3594 -- Expand_Limited_With_Clause --
3595 --------------------------------
3597 procedure Expand_Limited_With_Clause
3598 (Comp_Unit : Node_Id;
3599 Nam : Node_Id;
3600 N : Node_Id)
3602 Loc : constant Source_Ptr := Sloc (Nam);
3603 Unum : Unit_Number_Type;
3604 Withn : Node_Id;
3606 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3607 -- Returns true if the context already includes a with_clause for
3608 -- this unit. If the with_clause is non-limited, the unit is fully
3609 -- visible and an implicit limited_with should not be created. If
3610 -- there is already a limited_with clause for W, a second one is
3611 -- simply redundant.
3613 --------------------------
3614 -- Previous_Withed_Unit --
3615 --------------------------
3617 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3618 Item : Node_Id;
3620 begin
3621 -- A limited with_clause cannot appear in the same context_clause
3622 -- as a nonlimited with_clause which mentions the same library.
3624 Item := First (Context_Items (Comp_Unit));
3625 while Present (Item) loop
3626 if Nkind (Item) = N_With_Clause
3627 and then Library_Unit (Item) = Library_Unit (W)
3628 then
3629 return True;
3630 end if;
3632 Next (Item);
3633 end loop;
3635 return False;
3636 end Previous_Withed_Unit;
3638 -- Start of processing for Expand_Limited_With_Clause
3640 begin
3641 New_Nodes_OK := New_Nodes_OK + 1;
3643 if Nkind (Nam) = N_Identifier then
3645 -- Create node for name of withed unit
3647 Withn :=
3648 Make_With_Clause (Loc,
3649 Name => New_Copy (Nam));
3651 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3652 Withn :=
3653 Make_With_Clause (Loc,
3654 Name => Make_Selected_Component (Loc,
3655 Prefix => New_Copy_Tree (Prefix (Nam)),
3656 Selector_Name => New_Copy (Selector_Name (Nam))));
3657 Set_Parent (Withn, Parent (N));
3658 end if;
3660 Set_Limited_Present (Withn);
3661 Set_First_Name (Withn);
3662 Set_Implicit_With (Withn);
3664 Unum :=
3665 Load_Unit
3666 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3667 Required => True,
3668 Subunit => False,
3669 Error_Node => Nam);
3671 -- Do not generate a limited_with_clause on the current unit. This
3672 -- path is taken when a unit has a limited_with clause on one of its
3673 -- child units.
3675 if Unum = Current_Sem_Unit then
3676 return;
3677 end if;
3679 Set_Library_Unit (Withn, Cunit (Unum));
3680 Set_Corresponding_Spec
3681 (Withn, Specification (Unit (Cunit (Unum))));
3683 if not Previous_Withed_Unit (Withn) then
3684 Prepend (Withn, Context_Items (Parent (N)));
3685 Mark_Rewrite_Insertion (Withn);
3687 -- Add implicit limited_with_clauses for parents of child units
3688 -- mentioned in limited_with clauses.
3690 if Nkind (Nam) = N_Selected_Component then
3691 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3692 end if;
3694 Analyze (Withn);
3696 if not Limited_View_Installed (Withn) then
3697 Install_Limited_Withed_Unit (Withn);
3698 end if;
3699 end if;
3701 New_Nodes_OK := New_Nodes_OK - 1;
3702 end Expand_Limited_With_Clause;
3704 ----------------------
3705 -- Is_Ancestor_Unit --
3706 ----------------------
3708 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
3709 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
3710 E2 : Entity_Id;
3711 begin
3712 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
3713 E2 := Defining_Entity (Unit (Library_Unit (U2)));
3714 return Is_Ancestor_Package (E1, E2);
3715 else
3716 return False;
3717 end if;
3718 end Is_Ancestor_Unit;
3720 -- Start of processing for Install_Limited_Context_Clauses
3722 begin
3723 Item := First (Context_Items (N));
3724 while Present (Item) loop
3725 if Nkind (Item) = N_With_Clause
3726 and then Limited_Present (Item)
3727 and then not Error_Posted (Item)
3728 then
3729 if Nkind (Name (Item)) = N_Selected_Component then
3730 Expand_Limited_With_Clause
3731 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3732 end if;
3734 Check_Private_Limited_Withed_Unit (Item);
3736 if not Implicit_With (Item)
3737 and then Is_Child_Spec (Unit (N))
3738 then
3739 Check_Renamings (Parent_Spec (Unit (N)), Item);
3740 end if;
3742 -- A unit may have a limited with on itself if it has a limited
3743 -- with_clause on one of its child units. In that case it is
3744 -- already being compiled and it makes no sense to install its
3745 -- limited view.
3747 -- If the item is a limited_private_with_clause, install it if the
3748 -- current unit is a body or if it is a private child. Otherwise
3749 -- the private clause is installed before analyzing the private
3750 -- part of the current unit.
3752 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3753 and then not Limited_View_Installed (Item)
3754 and then
3755 not Is_Ancestor_Unit
3756 (Library_Unit (Item), Cunit (Current_Sem_Unit))
3757 then
3758 if not Private_Present (Item)
3759 or else Private_Present (N)
3760 or else Nkind_In (Unit (N), N_Package_Body,
3761 N_Subprogram_Body,
3762 N_Subunit)
3763 then
3764 Install_Limited_Withed_Unit (Item);
3765 end if;
3766 end if;
3767 end if;
3769 Next (Item);
3770 end loop;
3772 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
3773 -- looking for incomplete subtype declarations of incomplete types
3774 -- visible through a limited with clause.
3776 if Ada_Version >= Ada_2005
3777 and then Analyzed (N)
3778 and then Nkind (Unit (N)) = N_Package_Declaration
3779 then
3780 declare
3781 Decl : Node_Id;
3782 Def_Id : Entity_Id;
3783 Non_Lim_View : Entity_Id;
3785 begin
3786 Decl := First (Visible_Declarations (Specification (Unit (N))));
3787 while Present (Decl) loop
3788 if Nkind (Decl) = N_Subtype_Declaration
3789 and then
3790 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3791 and then
3792 From_With_Type (Defining_Identifier (Decl))
3793 then
3794 Def_Id := Defining_Identifier (Decl);
3795 Non_Lim_View := Non_Limited_View (Def_Id);
3797 if not Is_Incomplete_Type (Non_Lim_View) then
3799 -- Convert an incomplete subtype declaration into a
3800 -- corresponding non-limited view subtype declaration.
3801 -- This is usually the case when analyzing a body that
3802 -- has regular with clauses, when the spec has limited
3803 -- ones.
3805 -- If the non-limited view is still incomplete, it is
3806 -- the dummy entry already created, and the declaration
3807 -- cannot be reanalyzed. This is the case when installing
3808 -- a parent unit that has limited with-clauses.
3810 Set_Subtype_Indication (Decl,
3811 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3812 Set_Etype (Def_Id, Non_Lim_View);
3813 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3814 Set_Analyzed (Decl, False);
3816 -- Reanalyze the declaration, suppressing the call to
3817 -- Enter_Name to avoid duplicate names.
3819 Analyze_Subtype_Declaration
3820 (N => Decl,
3821 Skip => True);
3822 end if;
3823 end if;
3825 Next (Decl);
3826 end loop;
3827 end;
3828 end if;
3829 end Install_Limited_Context_Clauses;
3831 ---------------------
3832 -- Install_Parents --
3833 ---------------------
3835 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3836 P : Node_Id;
3837 E_Name : Entity_Id;
3838 P_Name : Entity_Id;
3839 P_Spec : Node_Id;
3841 begin
3842 P := Unit (Parent_Spec (Lib_Unit));
3843 P_Name := Get_Parent_Entity (P);
3845 if Etype (P_Name) = Any_Type then
3846 return;
3847 end if;
3849 if Ekind (P_Name) = E_Generic_Package
3850 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3851 N_Generic_Package_Declaration)
3852 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3853 then
3854 Error_Msg_N
3855 ("child of a generic package must be a generic unit", Lib_Unit);
3857 elsif not Is_Package_Or_Generic_Package (P_Name) then
3858 Error_Msg_N
3859 ("parent unit must be package or generic package", Lib_Unit);
3860 raise Unrecoverable_Error;
3862 elsif Present (Renamed_Object (P_Name)) then
3863 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3864 raise Unrecoverable_Error;
3866 -- Verify that a child of an instance is itself an instance, or the
3867 -- renaming of one. Given that an instance that is a unit is replaced
3868 -- with a package declaration, check against the original node. The
3869 -- parent may be currently being instantiated, in which case it appears
3870 -- as a declaration, but the generic_parent is already established
3871 -- indicating that we deal with an instance.
3873 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3874 if Nkind (Lib_Unit) in N_Renaming_Declaration
3875 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3876 or else
3877 (Nkind (Lib_Unit) = N_Package_Declaration
3878 and then Present (Generic_Parent (Specification (Lib_Unit))))
3879 then
3880 null;
3881 else
3882 Error_Msg_N
3883 ("child of an instance must be an instance or renaming",
3884 Lib_Unit);
3885 end if;
3886 end if;
3888 -- This is the recursive call that ensures all parents are loaded
3890 if Is_Child_Spec (P) then
3891 Install_Parents (P,
3892 Is_Private or else Private_Present (Parent (Lib_Unit)));
3893 end if;
3895 -- Now we can install the context for this parent
3897 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3898 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3899 Install_Siblings (P_Name, Parent (Lib_Unit));
3901 -- The child unit is in the declarative region of the parent. The parent
3902 -- must therefore appear in the scope stack and be visible, as when
3903 -- compiling the corresponding body. If the child unit is private or it
3904 -- is a package body, private declarations must be accessible as well.
3905 -- Use declarations in the parent must also be installed. Finally, other
3906 -- child units of the same parent that are in the context are
3907 -- immediately visible.
3909 -- Find entity for compilation unit, and set its private descendant
3910 -- status as needed. Indicate that it is a compilation unit, which is
3911 -- redundant in general, but needed if this is a generated child spec
3912 -- for a child body without previous spec.
3914 E_Name := Defining_Entity (Lib_Unit);
3916 Set_Is_Child_Unit (E_Name);
3917 Set_Is_Compilation_Unit (E_Name);
3919 Set_Is_Private_Descendant (E_Name,
3920 Is_Private_Descendant (P_Name)
3921 or else Private_Present (Parent (Lib_Unit)));
3923 P_Spec := Specification (Unit_Declaration_Node (P_Name));
3924 Push_Scope (P_Name);
3926 -- Save current visibility of unit
3928 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3929 Is_Immediately_Visible (P_Name);
3930 Set_Is_Immediately_Visible (P_Name);
3931 Install_Visible_Declarations (P_Name);
3932 Set_Use (Visible_Declarations (P_Spec));
3934 -- If the parent is a generic unit, its formal part may contain formal
3935 -- packages and use clauses for them.
3937 if Ekind (P_Name) = E_Generic_Package then
3938 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3939 end if;
3941 if Is_Private
3942 or else Private_Present (Parent (Lib_Unit))
3943 then
3944 Install_Private_Declarations (P_Name);
3945 Install_Private_With_Clauses (P_Name);
3946 Set_Use (Private_Declarations (P_Spec));
3947 end if;
3948 end Install_Parents;
3950 ----------------------------------
3951 -- Install_Private_With_Clauses --
3952 ----------------------------------
3954 procedure Install_Private_With_Clauses (P : Entity_Id) is
3955 Decl : constant Node_Id := Unit_Declaration_Node (P);
3956 Item : Node_Id;
3958 begin
3959 if Debug_Flag_I then
3960 Write_Str ("install private with clauses of ");
3961 Write_Name (Chars (P));
3962 Write_Eol;
3963 end if;
3965 if Nkind (Parent (Decl)) = N_Compilation_Unit then
3966 Item := First (Context_Items (Parent (Decl)));
3967 while Present (Item) loop
3968 if Nkind (Item) = N_With_Clause
3969 and then Private_Present (Item)
3970 then
3971 if Limited_Present (Item) then
3972 if not Limited_View_Installed (Item) then
3973 Install_Limited_Withed_Unit (Item);
3974 end if;
3975 else
3976 Install_Withed_Unit (Item, Private_With_OK => True);
3977 end if;
3978 end if;
3980 Next (Item);
3981 end loop;
3982 end if;
3983 end Install_Private_With_Clauses;
3985 ----------------------
3986 -- Install_Siblings --
3987 ----------------------
3989 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3990 Item : Node_Id;
3991 Id : Entity_Id;
3992 Prev : Entity_Id;
3994 begin
3995 -- Iterate over explicit with clauses, and check whether the scope of
3996 -- each entity is an ancestor of the current unit, in which case it is
3997 -- immediately visible.
3999 Item := First (Context_Items (N));
4000 while Present (Item) loop
4002 -- Do not install private_with_clauses declaration, unless unit
4003 -- is itself a private child unit, or is a body. Note that for a
4004 -- subprogram body the private_with_clause does not take effect until
4005 -- after the specification.
4007 if Nkind (Item) /= N_With_Clause
4008 or else Implicit_With (Item)
4009 or else Limited_Present (Item)
4010 then
4011 null;
4013 elsif not Private_Present (Item)
4014 or else Private_Present (N)
4015 or else Nkind (Unit (N)) = N_Package_Body
4016 then
4017 Id := Entity (Name (Item));
4019 if Is_Child_Unit (Id)
4020 and then Is_Ancestor_Package (Scope (Id), U_Name)
4021 then
4022 Set_Is_Immediately_Visible (Id);
4024 -- Check for the presence of another unit in the context that
4025 -- may be inadvertently hidden by the child.
4027 Prev := Current_Entity (Id);
4029 if Present (Prev)
4030 and then Is_Immediately_Visible (Prev)
4031 and then not Is_Child_Unit (Prev)
4032 then
4033 declare
4034 Clause : Node_Id;
4036 begin
4037 Clause := First (Context_Items (N));
4038 while Present (Clause) loop
4039 if Nkind (Clause) = N_With_Clause
4040 and then Entity (Name (Clause)) = Prev
4041 then
4042 Error_Msg_NE
4043 ("child unit& hides compilation unit " &
4044 "with the same name?",
4045 Name (Item), Id);
4046 exit;
4047 end if;
4049 Next (Clause);
4050 end loop;
4051 end;
4052 end if;
4054 -- The With_Clause may be on a grand-child or one of its further
4055 -- descendants, which makes a child immediately visible. Examine
4056 -- ancestry to determine whether such a child exists. For example,
4057 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4058 -- is immediately visible.
4060 elsif Is_Child_Unit (Id) then
4061 declare
4062 Par : Entity_Id;
4064 begin
4065 Par := Scope (Id);
4066 while Is_Child_Unit (Par) loop
4067 if Is_Ancestor_Package (Scope (Par), U_Name) then
4068 Set_Is_Immediately_Visible (Par);
4069 exit;
4070 end if;
4072 Par := Scope (Par);
4073 end loop;
4074 end;
4075 end if;
4077 -- If the item is a private with-clause on a child unit, the parent
4078 -- may have been installed already, but the child unit must remain
4079 -- invisible until installed in a private part or body, unless there
4080 -- is already a regular with_clause for it in the current unit.
4082 elsif Private_Present (Item) then
4083 Id := Entity (Name (Item));
4085 if Is_Child_Unit (Id) then
4086 declare
4087 Clause : Node_Id;
4089 function In_Context return Boolean;
4090 -- Scan context of current unit, to check whether there is
4091 -- a with_clause on the same unit as a private with-clause
4092 -- on a parent, in which case child unit is visible. If the
4093 -- unit is a grand-child, the same applies to its parent.
4095 ----------------
4096 -- In_Context --
4097 ----------------
4099 function In_Context return Boolean is
4100 begin
4101 Clause :=
4102 First (Context_Items (Cunit (Current_Sem_Unit)));
4103 while Present (Clause) loop
4104 if Nkind (Clause) = N_With_Clause
4105 and then Comes_From_Source (Clause)
4106 and then Is_Entity_Name (Name (Clause))
4107 and then not Private_Present (Clause)
4108 then
4109 if Entity (Name (Clause)) = Id
4110 or else
4111 (Nkind (Name (Clause)) = N_Expanded_Name
4112 and then Entity (Prefix (Name (Clause))) = Id)
4113 then
4114 return True;
4115 end if;
4116 end if;
4118 Next (Clause);
4119 end loop;
4121 return False;
4122 end In_Context;
4124 begin
4125 Set_Is_Visible_Child_Unit (Id, In_Context);
4126 end;
4127 end if;
4128 end if;
4130 Next (Item);
4131 end loop;
4132 end Install_Siblings;
4134 ---------------------------------
4135 -- Install_Limited_Withed_Unit --
4136 ---------------------------------
4138 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4139 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4140 E : Entity_Id;
4141 P : Entity_Id;
4142 Is_Child_Package : Boolean := False;
4143 Lim_Header : Entity_Id;
4144 Lim_Typ : Entity_Id;
4146 procedure Check_Body_Required;
4147 -- A unit mentioned in a limited with_clause may not be mentioned in
4148 -- a regular with_clause, but must still be included in the current
4149 -- partition. We need to determine whether the unit needs a body, so
4150 -- that the binder can determine the name of the file to be compiled.
4151 -- Checking whether a unit needs a body can be done without semantic
4152 -- analysis, by examining the nature of the declarations in the package.
4154 function Has_Limited_With_Clause
4155 (C_Unit : Entity_Id;
4156 Pack : Entity_Id) return Boolean;
4157 -- Determine whether any package in the ancestor chain starting with
4158 -- C_Unit has a limited with clause for package Pack.
4160 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4161 -- Check if some package installed though normal with-clauses has a
4162 -- renaming declaration of package P. AARM 10.1.2(21/2).
4164 -------------------------
4165 -- Check_Body_Required --
4166 -------------------------
4168 procedure Check_Body_Required is
4169 PA : constant List_Id :=
4170 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4172 procedure Check_Declarations (Spec : Node_Id);
4173 -- Recursive procedure that does the work and checks nested packages
4175 ------------------------
4176 -- Check_Declarations --
4177 ------------------------
4179 procedure Check_Declarations (Spec : Node_Id) is
4180 Decl : Node_Id;
4181 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4183 Subp_List : constant Elist_Id := New_Elmt_List;
4185 procedure Check_Pragma_Import (P : Node_Id);
4186 -- If a pragma import applies to a previous subprogram, the
4187 -- enclosing unit may not need a body. The processing is syntactic
4188 -- and does not require a declaration to be analyzed. The code
4189 -- below also handles pragma Import when applied to a subprogram
4190 -- that renames another. In this case the pragma applies to the
4191 -- renamed entity.
4193 -- Chains of multiple renames are not handled by the code below.
4194 -- It is probably impossible to handle all cases without proper
4195 -- name resolution. In such cases the algorithm is conservative
4196 -- and will indicate that a body is needed???
4198 -------------------------
4199 -- Check_Pragma_Import --
4200 -------------------------
4202 procedure Check_Pragma_Import (P : Node_Id) is
4203 Arg : Node_Id;
4204 Prev_Id : Elmt_Id;
4205 Subp_Id : Elmt_Id;
4206 Imported : Node_Id;
4208 procedure Remove_Homonyms (E : Node_Id);
4209 -- Make one pass over list of subprograms. Called again if
4210 -- subprogram is a renaming. E is known to be an identifier.
4212 ---------------------
4213 -- Remove_Homonyms --
4214 ---------------------
4216 procedure Remove_Homonyms (E : Node_Id) is
4217 R : Entity_Id := Empty;
4218 -- Name of renamed entity, if any
4220 begin
4221 Subp_Id := First_Elmt (Subp_List);
4222 while Present (Subp_Id) loop
4223 if Chars (Node (Subp_Id)) = Chars (E) then
4224 if Nkind (Parent (Parent (Node (Subp_Id))))
4225 /= N_Subprogram_Renaming_Declaration
4226 then
4227 Prev_Id := Subp_Id;
4228 Next_Elmt (Subp_Id);
4229 Remove_Elmt (Subp_List, Prev_Id);
4230 else
4231 R := Name (Parent (Parent (Node (Subp_Id))));
4232 exit;
4233 end if;
4234 else
4235 Next_Elmt (Subp_Id);
4236 end if;
4237 end loop;
4239 if Present (R) then
4240 if Nkind (R) = N_Identifier then
4241 Remove_Homonyms (R);
4243 elsif Nkind (R) = N_Selected_Component then
4244 Remove_Homonyms (Selector_Name (R));
4246 -- Renaming of attribute
4248 else
4249 null;
4250 end if;
4251 end if;
4252 end Remove_Homonyms;
4254 -- Start of processing for Check_Pragma_Import
4256 begin
4257 -- Find name of entity in Import pragma. We have not analyzed
4258 -- the construct, so we must guard against syntax errors.
4260 Arg := Next (First (Pragma_Argument_Associations (P)));
4262 if No (Arg)
4263 or else Nkind (Expression (Arg)) /= N_Identifier
4264 then
4265 return;
4266 else
4267 Imported := Expression (Arg);
4268 end if;
4270 Remove_Homonyms (Imported);
4271 end Check_Pragma_Import;
4273 -- Start of processing for Check_Declarations
4275 begin
4276 -- Search for Elaborate Body pragma
4278 Decl := First (Visible_Declarations (Spec));
4279 while Present (Decl)
4280 and then Nkind (Decl) = N_Pragma
4281 loop
4282 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4283 Set_Body_Required (Library_Unit (N));
4284 return;
4285 end if;
4287 Next (Decl);
4288 end loop;
4290 -- Look for declarations that require the presence of a body. We
4291 -- have already skipped pragmas at the start of the list.
4293 while Present (Decl) loop
4295 -- Subprogram that comes from source means body may be needed.
4296 -- Save for subsequent examination of import pragmas.
4298 if Comes_From_Source (Decl)
4299 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4300 N_Subprogram_Renaming_Declaration,
4301 N_Generic_Subprogram_Declaration))
4302 then
4303 Append_Elmt (Defining_Entity (Decl), Subp_List);
4305 -- Package declaration of generic package declaration. We need
4306 -- to recursively examine nested declarations.
4308 elsif Nkind_In (Decl, N_Package_Declaration,
4309 N_Generic_Package_Declaration)
4310 then
4311 Check_Declarations (Specification (Decl));
4313 elsif Nkind (Decl) = N_Pragma
4314 and then Pragma_Name (Decl) = Name_Import
4315 then
4316 Check_Pragma_Import (Decl);
4317 end if;
4319 Next (Decl);
4320 end loop;
4322 -- Same set of tests for private part. In addition to subprograms
4323 -- detect the presence of Taft Amendment types (incomplete types
4324 -- completed in the body).
4326 Decl := First (Private_Declarations (Spec));
4327 while Present (Decl) loop
4328 if Comes_From_Source (Decl)
4329 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4330 N_Subprogram_Renaming_Declaration,
4331 N_Generic_Subprogram_Declaration))
4332 then
4333 Append_Elmt (Defining_Entity (Decl), Subp_List);
4335 elsif Nkind_In (Decl, N_Package_Declaration,
4336 N_Generic_Package_Declaration)
4337 then
4338 Check_Declarations (Specification (Decl));
4340 -- Collect incomplete type declarations for separate pass
4342 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4343 Append_Elmt (Decl, Incomplete_Decls);
4345 elsif Nkind (Decl) = N_Pragma
4346 and then Pragma_Name (Decl) = Name_Import
4347 then
4348 Check_Pragma_Import (Decl);
4349 end if;
4351 Next (Decl);
4352 end loop;
4354 -- Now check incomplete declarations to locate Taft amendment
4355 -- types. This can be done by examining the defining identifiers
4356 -- of type declarations without real semantic analysis.
4358 declare
4359 Inc : Elmt_Id;
4361 begin
4362 Inc := First_Elmt (Incomplete_Decls);
4363 while Present (Inc) loop
4364 Decl := Next (Node (Inc));
4365 while Present (Decl) loop
4366 if Nkind (Decl) = N_Full_Type_Declaration
4367 and then Chars (Defining_Identifier (Decl)) =
4368 Chars (Defining_Identifier (Node (Inc)))
4369 then
4370 exit;
4371 end if;
4373 Next (Decl);
4374 end loop;
4376 -- If no completion, this is a TAT, and a body is needed
4378 if No (Decl) then
4379 Set_Body_Required (Library_Unit (N));
4380 return;
4381 end if;
4383 Next_Elmt (Inc);
4384 end loop;
4385 end;
4387 -- Finally, check whether there are subprograms that still require
4388 -- a body, i.e. are not renamings or null.
4390 if not Is_Empty_Elmt_List (Subp_List) then
4391 declare
4392 Subp_Id : Elmt_Id;
4393 Spec : Node_Id;
4395 begin
4396 Subp_Id := First_Elmt (Subp_List);
4397 Spec := Parent (Node (Subp_Id));
4399 while Present (Subp_Id) loop
4400 if Nkind (Parent (Spec))
4401 = N_Subprogram_Renaming_Declaration
4402 then
4403 null;
4405 elsif Nkind (Spec) = N_Procedure_Specification
4406 and then Null_Present (Spec)
4407 then
4408 null;
4410 else
4411 Set_Body_Required (Library_Unit (N));
4412 return;
4413 end if;
4415 Next_Elmt (Subp_Id);
4416 end loop;
4417 end;
4418 end if;
4419 end Check_Declarations;
4421 -- Start of processing for Check_Body_Required
4423 begin
4424 -- If this is an imported package (Java and CIL usage) no body is
4425 -- needed. Scan list of pragmas that may follow a compilation unit
4426 -- to look for a relevant pragma Import.
4428 if Present (PA) then
4429 declare
4430 Prag : Node_Id;
4432 begin
4433 Prag := First (PA);
4434 while Present (Prag) loop
4435 if Nkind (Prag) = N_Pragma
4436 and then Get_Pragma_Id (Prag) = Pragma_Import
4437 then
4438 return;
4439 end if;
4441 Next (Prag);
4442 end loop;
4443 end;
4444 end if;
4446 Check_Declarations (Specification (P_Unit));
4447 end Check_Body_Required;
4449 -----------------------------
4450 -- Has_Limited_With_Clause --
4451 -----------------------------
4453 function Has_Limited_With_Clause
4454 (C_Unit : Entity_Id;
4455 Pack : Entity_Id) return Boolean
4457 Par : Entity_Id;
4458 Par_Unit : Node_Id;
4460 begin
4461 Par := C_Unit;
4462 while Present (Par) loop
4463 if Ekind (Par) /= E_Package then
4464 exit;
4465 end if;
4467 -- Retrieve the Compilation_Unit node for Par and determine if
4468 -- its context clauses contain a limited with for Pack.
4470 Par_Unit := Parent (Parent (Parent (Par)));
4472 if Nkind (Par_Unit) = N_Package_Declaration then
4473 Par_Unit := Parent (Par_Unit);
4474 end if;
4476 if Has_With_Clause (Par_Unit, Pack, True) then
4477 return True;
4478 end if;
4480 -- If there are more ancestors, climb up the tree, otherwise we
4481 -- are done.
4483 if Is_Child_Unit (Par) then
4484 Par := Scope (Par);
4485 else
4486 exit;
4487 end if;
4488 end loop;
4490 return False;
4491 end Has_Limited_With_Clause;
4493 ----------------------------------
4494 -- Is_Visible_Through_Renamings --
4495 ----------------------------------
4497 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4498 Kind : constant Node_Kind :=
4499 Nkind (Unit (Cunit (Current_Sem_Unit)));
4500 Aux_Unit : Node_Id;
4501 Item : Node_Id;
4502 Decl : Entity_Id;
4504 begin
4505 -- Example of the error detected by this subprogram:
4507 -- package P is
4508 -- type T is ...
4509 -- end P;
4511 -- with P;
4512 -- package Q is
4513 -- package Ren_P renames P;
4514 -- end Q;
4516 -- with Q;
4517 -- package R is ...
4519 -- limited with P; -- ERROR
4520 -- package R.C is ...
4522 Aux_Unit := Cunit (Current_Sem_Unit);
4524 loop
4525 Item := First (Context_Items (Aux_Unit));
4526 while Present (Item) loop
4527 if Nkind (Item) = N_With_Clause
4528 and then not Limited_Present (Item)
4529 and then Nkind (Unit (Library_Unit (Item))) =
4530 N_Package_Declaration
4531 then
4532 Decl :=
4533 First (Visible_Declarations
4534 (Specification (Unit (Library_Unit (Item)))));
4535 while Present (Decl) loop
4536 if Nkind (Decl) = N_Package_Renaming_Declaration
4537 and then Entity (Name (Decl)) = P
4538 then
4539 -- Generate the error message only if the current unit
4540 -- is a package declaration; in case of subprogram
4541 -- bodies and package bodies we just return True to
4542 -- indicate that the limited view must not be
4543 -- installed.
4545 if Kind = N_Package_Declaration then
4546 Error_Msg_N
4547 ("simultaneous visibility of the limited and " &
4548 "unlimited views not allowed", N);
4549 Error_Msg_Sloc := Sloc (Item);
4550 Error_Msg_NE
4551 ("\\ unlimited view of & visible through the " &
4552 "context clause #", N, P);
4553 Error_Msg_Sloc := Sloc (Decl);
4554 Error_Msg_NE ("\\ and the renaming #", N, P);
4555 end if;
4557 return True;
4558 end if;
4560 Next (Decl);
4561 end loop;
4562 end if;
4564 Next (Item);
4565 end loop;
4567 -- If it is a body not acting as spec, follow pointer to the
4568 -- corresponding spec, otherwise follow pointer to parent spec.
4570 if Present (Library_Unit (Aux_Unit))
4571 and then Nkind_In (Unit (Aux_Unit),
4572 N_Package_Body, N_Subprogram_Body)
4573 then
4574 if Aux_Unit = Library_Unit (Aux_Unit) then
4576 -- Aux_Unit is a body that acts as a spec. Clause has
4577 -- already been flagged as illegal.
4579 return False;
4581 else
4582 Aux_Unit := Library_Unit (Aux_Unit);
4583 end if;
4585 else
4586 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4587 end if;
4589 exit when No (Aux_Unit);
4590 end loop;
4592 return False;
4593 end Is_Visible_Through_Renamings;
4595 -- Start of processing for Install_Limited_Withed_Unit
4597 begin
4598 pragma Assert (not Limited_View_Installed (N));
4600 -- In case of limited with_clause on subprograms, generics, instances,
4601 -- or renamings, the corresponding error was previously posted and we
4602 -- have nothing to do here. If the file is missing altogether, it has
4603 -- no source location.
4605 if Nkind (P_Unit) /= N_Package_Declaration
4606 or else Sloc (P_Unit) = No_Location
4607 then
4608 return;
4609 end if;
4611 P := Defining_Unit_Name (Specification (P_Unit));
4613 -- Handle child packages
4615 if Nkind (P) = N_Defining_Program_Unit_Name then
4616 Is_Child_Package := True;
4617 P := Defining_Identifier (P);
4618 end if;
4620 -- Do not install the limited-view if the context of the unit is already
4621 -- available through a regular with clause.
4623 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4624 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4625 then
4626 return;
4627 end if;
4629 -- Do not install the limited-view if the full-view is already visible
4630 -- through renaming declarations.
4632 if Is_Visible_Through_Renamings (P) then
4633 return;
4634 end if;
4636 -- Do not install the limited view if this is the unit being analyzed.
4637 -- This unusual case will happen when a unit has a limited_with clause
4638 -- on one of its children. The compilation of the child forces the load
4639 -- of the parent which tries to install the limited view of the child
4640 -- again. Installing the limited view must also be disabled when
4641 -- compiling the body of the child unit.
4643 if P = Cunit_Entity (Current_Sem_Unit)
4644 or else
4645 (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4646 and then P = Main_Unit_Entity)
4647 then
4648 return;
4649 end if;
4651 -- This scenario is similar to the one above, the difference is that the
4652 -- compilation of sibling Par.Sib forces the load of parent Par which
4653 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4654 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4655 -- non-limited views of all entities from Lim_Pack.
4657 -- limited with Lim_Pack; -- [1]
4658 -- package Par is ... package Lim_Pack is ...
4660 -- with Lim_Pack; -- [2]
4661 -- package Par.Sib is ... package body Par.Sib is ...
4663 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4664 -- Sem_Unit is the body of Par.Sib.
4666 if Ekind (P) = E_Package
4667 and then Ekind (Main_Unit_Entity) = E_Package
4668 and then Is_Child_Unit (Main_Unit_Entity)
4670 -- The body has a regular with clause
4672 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4673 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4675 -- One of the ancestors has a limited with clause
4677 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4678 N_Package_Specification
4679 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4680 then
4681 return;
4682 end if;
4684 -- A common use of the limited-with is to have a limited-with in the
4685 -- package spec, and a normal with in its package body. For example:
4687 -- limited with X; -- [1]
4688 -- package A is ...
4690 -- with X; -- [2]
4691 -- package body A is ...
4693 -- The compilation of A's body installs the context clauses found at [2]
4694 -- and then the context clauses of its specification (found at [1]). As
4695 -- a consequence, at [1] the specification of X has been analyzed and it
4696 -- is immediately visible. According to the semantics of limited-with
4697 -- context clauses we don't install the limited view because the full
4698 -- view of X supersedes its limited view.
4700 if Analyzed (P_Unit)
4701 and then
4702 (Is_Immediately_Visible (P)
4703 or else (Is_Child_Package and then Is_Visible_Child_Unit (P)))
4704 then
4706 -- The presence of both the limited and the analyzed nonlimited view
4707 -- may also be an error, such as an illegal context for a limited
4708 -- with_clause. In that case, do not process the context item at all.
4710 if Error_Posted (N) then
4711 return;
4712 end if;
4714 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
4715 declare
4716 Item : Node_Id;
4717 begin
4718 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
4719 while Present (Item) loop
4720 if Nkind (Item) = N_With_Clause
4721 and then Comes_From_Source (Item)
4722 and then Entity (Name (Item)) = P
4723 then
4724 return;
4725 end if;
4727 Next (Item);
4728 end loop;
4729 end;
4731 -- If this is a child body, assume that the nonlimited with_clause
4732 -- appears in an ancestor. Could be refined ???
4734 if Is_Child_Unit
4735 (Defining_Entity
4736 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
4737 then
4738 return;
4739 end if;
4741 else
4743 -- If in package declaration, nonlimited view brought in from
4744 -- parent unit or some error condition.
4746 return;
4747 end if;
4748 end if;
4750 if Debug_Flag_I then
4751 Write_Str ("install limited view of ");
4752 Write_Name (Chars (P));
4753 Write_Eol;
4754 end if;
4756 -- If the unit has not been analyzed and the limited view has not been
4757 -- already installed then we install it.
4759 if not Analyzed (P_Unit) then
4760 if not In_Chain (P) then
4762 -- Minimum decoration
4764 Set_Ekind (P, E_Package);
4765 Set_Etype (P, Standard_Void_Type);
4766 Set_Scope (P, Standard_Standard);
4768 if Is_Child_Package then
4769 Set_Is_Child_Unit (P);
4770 Set_Is_Visible_Child_Unit (P);
4771 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4772 end if;
4774 -- Place entity on visibility structure
4776 Set_Homonym (P, Current_Entity (P));
4777 Set_Current_Entity (P);
4779 if Debug_Flag_I then
4780 Write_Str (" (homonym) chain ");
4781 Write_Name (Chars (P));
4782 Write_Eol;
4783 end if;
4785 -- Install the incomplete view. The first element of the limited
4786 -- view is a header (an E_Package entity) used to reference the
4787 -- first shadow entity in the private part of the package.
4789 Lim_Header := Limited_View (P);
4790 Lim_Typ := First_Entity (Lim_Header);
4792 while Present (Lim_Typ)
4793 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4794 loop
4795 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4796 Set_Current_Entity (Lim_Typ);
4798 if Debug_Flag_I then
4799 Write_Str (" (homonym) chain ");
4800 Write_Name (Chars (Lim_Typ));
4801 Write_Eol;
4802 end if;
4804 Next_Entity (Lim_Typ);
4805 end loop;
4806 end if;
4808 -- If the unit appears in a previous regular with_clause, the regular
4809 -- entities of the public part of the withed package must be replaced
4810 -- by the shadow ones.
4812 -- This code must be kept synchronized with the code that replaces the
4813 -- shadow entities by the real entities (see body of Remove_Limited
4814 -- With_Clause); otherwise the contents of the homonym chains are not
4815 -- consistent.
4817 else
4818 -- Hide all the type entities of the public part of the package to
4819 -- avoid its usage. This is needed to cover all the subtype decla-
4820 -- rations because we do not remove them from the homonym chain.
4822 E := First_Entity (P);
4823 while Present (E) and then E /= First_Private_Entity (P) loop
4824 if Is_Type (E) then
4825 Set_Was_Hidden (E, Is_Hidden (E));
4826 Set_Is_Hidden (E);
4827 end if;
4829 Next_Entity (E);
4830 end loop;
4832 -- Replace the real entities by the shadow entities of the limited
4833 -- view. The first element of the limited view is a header that is
4834 -- used to reference the first shadow entity in the private part
4835 -- of the package. Successive elements are the limited views of the
4836 -- type (including regular incomplete types) declared in the package.
4838 Lim_Header := Limited_View (P);
4840 Lim_Typ := First_Entity (Lim_Header);
4841 while Present (Lim_Typ)
4842 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4843 loop
4844 pragma Assert (not In_Chain (Lim_Typ));
4846 -- Do not unchain nested packages and child units
4848 if Ekind (Lim_Typ) /= E_Package
4849 and then not Is_Child_Unit (Lim_Typ)
4850 then
4851 declare
4852 Prev : Entity_Id;
4854 begin
4855 Prev := Current_Entity (Lim_Typ);
4856 E := Prev;
4858 -- Replace E in the homonyms list, so that the limited view
4859 -- becomes available.
4861 if E = Non_Limited_View (Lim_Typ) then
4862 Set_Homonym (Lim_Typ, Homonym (Prev));
4863 Set_Current_Entity (Lim_Typ);
4865 else
4866 loop
4867 E := Homonym (Prev);
4869 -- E may have been removed when installing a previous
4870 -- limited_with_clause.
4872 exit when No (E);
4874 exit when E = Non_Limited_View (Lim_Typ);
4876 Prev := Homonym (Prev);
4877 end loop;
4879 if Present (E) then
4880 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4881 Set_Homonym (Prev, Lim_Typ);
4882 end if;
4883 end if;
4884 end;
4886 if Debug_Flag_I then
4887 Write_Str (" (homonym) chain ");
4888 Write_Name (Chars (Lim_Typ));
4889 Write_Eol;
4890 end if;
4891 end if;
4893 Next_Entity (Lim_Typ);
4894 end loop;
4895 end if;
4897 -- The package must be visible while the limited-with clause is active
4898 -- because references to the type P.T must resolve in the usual way.
4899 -- In addition, we remember that the limited-view has been installed to
4900 -- uninstall it at the point of context removal.
4902 Set_Is_Immediately_Visible (P);
4903 Set_Limited_View_Installed (N);
4905 -- If unit has not been analyzed in some previous context, check
4906 -- (imperfectly ???) whether it might need a body.
4908 if not Analyzed (P_Unit) then
4909 Check_Body_Required;
4910 end if;
4912 -- If the package in the limited_with clause is a child unit, the clause
4913 -- is unanalyzed and appears as a selected component. Recast it as an
4914 -- expanded name so that the entity can be properly set. Use entity of
4915 -- parent, if available, for higher ancestors in the name.
4917 if Nkind (Name (N)) = N_Selected_Component then
4918 declare
4919 Nam : Node_Id;
4920 Ent : Entity_Id;
4922 begin
4923 Nam := Name (N);
4924 Ent := P;
4925 while Nkind (Nam) = N_Selected_Component
4926 and then Present (Ent)
4927 loop
4928 Change_Selected_Component_To_Expanded_Name (Nam);
4930 -- Set entity of parent identifiers if the unit is a child
4931 -- unit. This ensures that the tree is properly formed from
4932 -- semantic point of view (e.g. for ASIS queries).
4934 Set_Entity (Nam, Ent);
4936 Nam := Prefix (Nam);
4937 Ent := Scope (Ent);
4939 -- Set entity of last ancestor
4941 if Nkind (Nam) = N_Identifier then
4942 Set_Entity (Nam, Ent);
4943 end if;
4944 end loop;
4945 end;
4946 end if;
4948 Set_Entity (Name (N), P);
4949 Set_From_With_Type (P);
4950 end Install_Limited_Withed_Unit;
4952 -------------------------
4953 -- Install_Withed_Unit --
4954 -------------------------
4956 procedure Install_Withed_Unit
4957 (With_Clause : Node_Id;
4958 Private_With_OK : Boolean := False)
4960 Uname : constant Entity_Id := Entity (Name (With_Clause));
4961 P : constant Entity_Id := Scope (Uname);
4963 begin
4964 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
4965 -- compiling a package declaration and the Private_With_OK flag was not
4966 -- set by the caller. These declarations will be installed later (before
4967 -- analyzing the private part of the package).
4969 if Private_Present (With_Clause)
4970 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
4971 and then not (Private_With_OK)
4972 then
4973 return;
4974 end if;
4976 if Debug_Flag_I then
4977 if Private_Present (With_Clause) then
4978 Write_Str ("install private withed unit ");
4979 else
4980 Write_Str ("install withed unit ");
4981 end if;
4983 Write_Name (Chars (Uname));
4984 Write_Eol;
4985 end if;
4987 -- We do not apply the restrictions to an internal unit unless we are
4988 -- compiling the internal unit as a main unit. This check is also
4989 -- skipped for dummy units (for missing packages).
4991 if Sloc (Uname) /= No_Location
4992 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
4993 or else Current_Sem_Unit = Main_Unit)
4994 then
4995 Check_Restricted_Unit
4996 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
4997 end if;
4999 if P /= Standard_Standard then
5001 -- If the unit is not analyzed after analysis of the with clause and
5002 -- it is an instantiation then it awaits a body and is the main unit.
5003 -- Its appearance in the context of some other unit indicates a
5004 -- circular dependency (DEC suite perversity).
5006 if not Analyzed (Uname)
5007 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5008 then
5009 Error_Msg_N
5010 ("instantiation depends on itself", Name (With_Clause));
5012 elsif not Is_Visible_Child_Unit (Uname) then
5013 Set_Is_Visible_Child_Unit (Uname);
5015 -- If the child unit appears in the context of its parent, it is
5016 -- immediately visible.
5018 if In_Open_Scopes (Scope (Uname)) then
5019 Set_Is_Immediately_Visible (Uname);
5020 end if;
5022 if Is_Generic_Instance (Uname)
5023 and then Ekind (Uname) in Subprogram_Kind
5024 then
5025 -- Set flag as well on the visible entity that denotes the
5026 -- instance, which renames the current one.
5028 Set_Is_Visible_Child_Unit
5029 (Related_Instance
5030 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5031 end if;
5033 -- The parent unit may have been installed already, and may have
5034 -- appeared in a use clause.
5036 if In_Use (Scope (Uname)) then
5037 Set_Is_Potentially_Use_Visible (Uname);
5038 end if;
5040 Set_Context_Installed (With_Clause);
5041 end if;
5043 elsif not Is_Immediately_Visible (Uname) then
5044 if not Private_Present (With_Clause)
5045 or else Private_With_OK
5046 then
5047 Set_Is_Immediately_Visible (Uname);
5048 end if;
5050 Set_Context_Installed (With_Clause);
5051 end if;
5053 -- A with-clause overrides a with-type clause: there are no restric-
5054 -- tions on the use of package entities.
5056 if Ekind (Uname) = E_Package then
5057 Set_From_With_Type (Uname, False);
5058 end if;
5060 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5061 -- unit if there is a visible homograph for it declared in the same
5062 -- declarative region. This pathological case can only arise when an
5063 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5064 -- G1 has a generic child also named G2, and the context includes with_
5065 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5066 -- of I1.G2 visible as well. If the child unit is named Standard, do
5067 -- not apply the check to the Standard package itself.
5069 if Is_Child_Unit (Uname)
5070 and then Is_Visible_Child_Unit (Uname)
5071 and then Ada_Version >= Ada_2005
5072 then
5073 declare
5074 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5075 Decl2 : Node_Id;
5076 P2 : Entity_Id;
5077 U2 : Entity_Id;
5079 begin
5080 U2 := Homonym (Uname);
5081 while Present (U2)
5082 and then U2 /= Standard_Standard
5083 loop
5084 P2 := Scope (U2);
5085 Decl2 := Unit_Declaration_Node (P2);
5087 if Is_Child_Unit (U2)
5088 and then Is_Visible_Child_Unit (U2)
5089 then
5090 if Is_Generic_Instance (P)
5091 and then Nkind (Decl1) = N_Package_Declaration
5092 and then Generic_Parent (Specification (Decl1)) = P2
5093 then
5094 Error_Msg_N ("illegal with_clause", With_Clause);
5095 Error_Msg_N
5096 ("\child unit has visible homograph" &
5097 " (RM 8.3(26), 10.1.1(19))",
5098 With_Clause);
5099 exit;
5101 elsif Is_Generic_Instance (P2)
5102 and then Nkind (Decl2) = N_Package_Declaration
5103 and then Generic_Parent (Specification (Decl2)) = P
5104 then
5105 -- With_clause for child unit of instance appears before
5106 -- in the context. We want to place the error message on
5107 -- it, not on the generic child unit itself.
5109 declare
5110 Prev_Clause : Node_Id;
5112 begin
5113 Prev_Clause := First (List_Containing (With_Clause));
5114 while Entity (Name (Prev_Clause)) /= U2 loop
5115 Next (Prev_Clause);
5116 end loop;
5118 pragma Assert (Present (Prev_Clause));
5119 Error_Msg_N ("illegal with_clause", Prev_Clause);
5120 Error_Msg_N
5121 ("\child unit has visible homograph" &
5122 " (RM 8.3(26), 10.1.1(19))",
5123 Prev_Clause);
5124 exit;
5125 end;
5126 end if;
5127 end if;
5129 U2 := Homonym (U2);
5130 end loop;
5131 end;
5132 end if;
5133 end Install_Withed_Unit;
5135 -------------------
5136 -- Is_Child_Spec --
5137 -------------------
5139 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5140 K : constant Node_Kind := Nkind (Lib_Unit);
5142 begin
5143 return (K in N_Generic_Declaration or else
5144 K in N_Generic_Instantiation or else
5145 K in N_Generic_Renaming_Declaration or else
5146 K = N_Package_Declaration or else
5147 K = N_Package_Renaming_Declaration or else
5148 K = N_Subprogram_Declaration or else
5149 K = N_Subprogram_Renaming_Declaration)
5150 and then Present (Parent_Spec (Lib_Unit));
5151 end Is_Child_Spec;
5153 ------------------------------------
5154 -- Is_Legal_Shadow_Entity_In_Body --
5155 ------------------------------------
5157 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5158 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5159 begin
5160 return Nkind (Unit (C_Unit)) = N_Package_Body
5161 and then
5162 Has_With_Clause
5163 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5164 end Is_Legal_Shadow_Entity_In_Body;
5166 -----------------------
5167 -- Load_Needed_Body --
5168 -----------------------
5170 -- N is a generic unit named in a with clause, or else it is a unit that
5171 -- contains a generic unit or an inlined function. In order to perform an
5172 -- instantiation, the body of the unit must be present. If the unit itself
5173 -- is generic, we assume that an instantiation follows, and load & analyze
5174 -- the body unconditionally. This forces analysis of the spec as well.
5176 -- If the unit is not generic, but contains a generic unit, it is loaded on
5177 -- demand, at the point of instantiation (see ch12).
5179 procedure Load_Needed_Body
5180 (N : Node_Id;
5181 OK : out Boolean;
5182 Do_Analyze : Boolean := True)
5184 Body_Name : Unit_Name_Type;
5185 Unum : Unit_Number_Type;
5187 Save_Style_Check : constant Boolean := Opt.Style_Check;
5188 -- The loading and analysis is done with style checks off
5190 begin
5191 if not GNAT_Mode then
5192 Style_Check := False;
5193 end if;
5195 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5196 Unum :=
5197 Load_Unit
5198 (Load_Name => Body_Name,
5199 Required => False,
5200 Subunit => False,
5201 Error_Node => N,
5202 Renamings => True);
5204 if Unum = No_Unit then
5205 OK := False;
5207 else
5208 Compiler_State := Analyzing; -- reset after load
5210 if not Fatal_Error (Unum) or else Try_Semantics then
5211 if Debug_Flag_L then
5212 Write_Str ("*** Loaded generic body");
5213 Write_Eol;
5214 end if;
5216 if Do_Analyze then
5217 Semantics (Cunit (Unum));
5218 end if;
5219 end if;
5221 OK := True;
5222 end if;
5224 Style_Check := Save_Style_Check;
5225 end Load_Needed_Body;
5227 -------------------------
5228 -- Build_Limited_Views --
5229 -------------------------
5231 procedure Build_Limited_Views (N : Node_Id) is
5232 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
5233 P : constant Entity_Id := Cunit_Entity (Unum);
5235 Spec : Node_Id; -- To denote a package specification
5236 Lim_Typ : Entity_Id; -- To denote shadow entities
5237 Comp_Typ : Entity_Id; -- To denote real entities
5239 Lim_Header : Entity_Id; -- Package entity
5240 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
5241 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
5243 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id);
5244 -- Add attributes of an incomplete type to a shadow entity. The same
5245 -- attributes are placed on the real entity, so that gigi receives
5246 -- a consistent view.
5248 procedure Decorate_Package_Specification (P : Entity_Id);
5249 -- Add attributes of a package entity to the entity in a package
5250 -- declaration
5252 procedure Decorate_Tagged_Type
5253 (Loc : Source_Ptr;
5254 T : Entity_Id;
5255 Scop : Entity_Id);
5256 -- Set basic attributes of tagged type T, including its class_wide type.
5257 -- The parameters Loc, Scope are used to decorate the class_wide type.
5259 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id);
5260 -- Construct list of shadow entities and attach it to entity of
5261 -- package that is mentioned in a limited_with clause.
5263 function New_Internal_Shadow_Entity
5264 (Kind : Entity_Kind;
5265 Sloc_Value : Source_Ptr;
5266 Id_Char : Character) return Entity_Id;
5267 -- Build a new internal entity and append it to the list of shadow
5268 -- entities available through the limited-header
5270 -----------------
5271 -- Build_Chain --
5272 -----------------
5274 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id) is
5275 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
5276 Is_Tagged : Boolean;
5277 Decl : Node_Id;
5279 begin
5280 Decl := First_Decl;
5281 while Present (Decl) loop
5283 -- For each library_package_declaration in the environment, there
5284 -- is an implicit declaration of a *limited view* of that library
5285 -- package. The limited view of a package contains:
5287 -- * For each nested package_declaration, a declaration of the
5288 -- limited view of that package, with the same defining-
5289 -- program-unit name.
5291 -- * For each type_declaration in the visible part, an incomplete
5292 -- type-declaration with the same defining_identifier, whose
5293 -- completion is the type_declaration. If the type_declaration
5294 -- is tagged, then the incomplete_type_declaration is tagged
5295 -- incomplete.
5297 -- The partial view is tagged if the declaration has the
5298 -- explicit keyword, or else if it is a type extension, both
5299 -- of which can be ascertained syntactically.
5301 if Nkind (Decl) = N_Full_Type_Declaration then
5302 Is_Tagged :=
5303 (Nkind (Type_Definition (Decl)) = N_Record_Definition
5304 and then Tagged_Present (Type_Definition (Decl)))
5305 or else
5306 (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
5307 and then
5308 Present
5309 (Record_Extension_Part (Type_Definition (Decl))));
5311 Comp_Typ := Defining_Identifier (Decl);
5313 if not Analyzed_Unit then
5314 if Is_Tagged then
5315 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5316 else
5317 Decorate_Incomplete_Type (Comp_Typ, Scope);
5318 end if;
5319 end if;
5321 -- Create shadow entity for type
5323 Lim_Typ :=
5324 New_Internal_Shadow_Entity
5325 (Kind => Ekind (Comp_Typ),
5326 Sloc_Value => Sloc (Comp_Typ),
5327 Id_Char => 'Z');
5329 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5330 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5331 Set_From_With_Type (Lim_Typ);
5333 if Is_Tagged then
5334 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5335 else
5336 Decorate_Incomplete_Type (Lim_Typ, Scope);
5337 end if;
5339 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5341 elsif Nkind_In (Decl, N_Private_Type_Declaration,
5342 N_Incomplete_Type_Declaration,
5343 N_Task_Type_Declaration,
5344 N_Protected_Type_Declaration)
5345 then
5346 Comp_Typ := Defining_Identifier (Decl);
5348 Is_Tagged :=
5349 Nkind_In (Decl, N_Private_Type_Declaration,
5350 N_Incomplete_Type_Declaration)
5351 and then Tagged_Present (Decl);
5353 if not Analyzed_Unit then
5354 if Is_Tagged then
5355 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5356 else
5357 Decorate_Incomplete_Type (Comp_Typ, Scope);
5358 end if;
5359 end if;
5361 Lim_Typ :=
5362 New_Internal_Shadow_Entity
5363 (Kind => Ekind (Comp_Typ),
5364 Sloc_Value => Sloc (Comp_Typ),
5365 Id_Char => 'Z');
5367 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5368 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5369 Set_From_With_Type (Lim_Typ);
5371 if Is_Tagged then
5372 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5373 else
5374 Decorate_Incomplete_Type (Lim_Typ, Scope);
5375 end if;
5377 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5379 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5380 Comp_Typ := Defining_Identifier (Decl);
5382 if not Analyzed_Unit then
5383 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5384 end if;
5386 -- Create shadow entity for type
5388 Lim_Typ :=
5389 New_Internal_Shadow_Entity
5390 (Kind => Ekind (Comp_Typ),
5391 Sloc_Value => Sloc (Comp_Typ),
5392 Id_Char => 'Z');
5394 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5395 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5396 Set_From_With_Type (Lim_Typ);
5398 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5399 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5401 elsif Nkind (Decl) = N_Package_Declaration then
5403 -- Local package
5405 declare
5406 Spec : constant Node_Id := Specification (Decl);
5408 begin
5409 Comp_Typ := Defining_Unit_Name (Spec);
5411 if not Analyzed (Cunit (Unum)) then
5412 Decorate_Package_Specification (Comp_Typ);
5413 Set_Scope (Comp_Typ, Scope);
5414 end if;
5416 Lim_Typ :=
5417 New_Internal_Shadow_Entity
5418 (Kind => Ekind (Comp_Typ),
5419 Sloc_Value => Sloc (Comp_Typ),
5420 Id_Char => 'Z');
5422 Decorate_Package_Specification (Lim_Typ);
5423 Set_Scope (Lim_Typ, Scope);
5425 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5426 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5427 Set_From_With_Type (Lim_Typ);
5429 -- Note: The non_limited_view attribute is not used
5430 -- for local packages.
5432 Build_Chain
5433 (Scope => Lim_Typ,
5434 First_Decl => First (Visible_Declarations (Spec)));
5435 end;
5436 end if;
5438 Next (Decl);
5439 end loop;
5440 end Build_Chain;
5442 ------------------------------
5443 -- Decorate_Incomplete_Type --
5444 ------------------------------
5446 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id) is
5447 begin
5448 Set_Ekind (E, E_Incomplete_Type);
5449 Set_Scope (E, Scop);
5450 Set_Etype (E, E);
5451 Set_Is_First_Subtype (E, True);
5452 Set_Stored_Constraint (E, No_Elist);
5453 Set_Full_View (E, Empty);
5454 Init_Size_Align (E);
5455 end Decorate_Incomplete_Type;
5457 --------------------------
5458 -- Decorate_Tagged_Type --
5459 --------------------------
5461 procedure Decorate_Tagged_Type
5462 (Loc : Source_Ptr;
5463 T : Entity_Id;
5464 Scop : Entity_Id)
5466 CW : Entity_Id;
5468 begin
5469 Decorate_Incomplete_Type (T, Scop);
5470 Set_Is_Tagged_Type (T);
5472 -- Build corresponding class_wide type, if not previously done
5474 -- Note: The class-wide entity is shared by the limited-view
5475 -- and the full-view.
5477 if No (Class_Wide_Type (T)) then
5478 CW := Make_Temporary (Loc, 'S');
5480 -- Set parent to be the same as the parent of the tagged type.
5481 -- We need a parent field set, and it is supposed to point to
5482 -- the declaration of the type. The tagged type declaration
5483 -- essentially declares two separate types, the tagged type
5484 -- itself and the corresponding class-wide type, so it is
5485 -- reasonable for the parent fields to point to the declaration
5486 -- in both cases.
5488 Set_Parent (CW, Parent (T));
5490 -- Set remaining fields of classwide type
5492 Set_Ekind (CW, E_Class_Wide_Type);
5493 Set_Etype (CW, T);
5494 Set_Scope (CW, Scop);
5495 Set_Is_Tagged_Type (CW);
5496 Set_Is_First_Subtype (CW, True);
5497 Init_Size_Align (CW);
5498 Set_Has_Unknown_Discriminants (CW, True);
5499 Set_Class_Wide_Type (CW, CW);
5500 Set_Equivalent_Type (CW, Empty);
5501 Set_From_With_Type (CW, From_With_Type (T));
5503 -- Link type to its class-wide type
5505 Set_Class_Wide_Type (T, CW);
5506 end if;
5507 end Decorate_Tagged_Type;
5509 ------------------------------------
5510 -- Decorate_Package_Specification --
5511 ------------------------------------
5513 procedure Decorate_Package_Specification (P : Entity_Id) is
5514 begin
5515 -- Place only the most basic attributes
5517 Set_Ekind (P, E_Package);
5518 Set_Etype (P, Standard_Void_Type);
5519 end Decorate_Package_Specification;
5521 --------------------------------
5522 -- New_Internal_Shadow_Entity --
5523 --------------------------------
5525 function New_Internal_Shadow_Entity
5526 (Kind : Entity_Kind;
5527 Sloc_Value : Source_Ptr;
5528 Id_Char : Character) return Entity_Id
5530 E : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
5532 begin
5533 Set_Ekind (E, Kind);
5534 Set_Is_Internal (E, True);
5536 if Kind in Type_Kind then
5537 Init_Size_Align (E);
5538 end if;
5540 Append_Entity (E, Lim_Header);
5541 Last_Lim_E := E;
5542 return E;
5543 end New_Internal_Shadow_Entity;
5545 -- Start of processing for Build_Limited_Views
5547 begin
5548 pragma Assert (Limited_Present (N));
5550 -- A library_item mentioned in a limited_with_clause is a package
5551 -- declaration, not a subprogram declaration, generic declaration,
5552 -- generic instantiation, or package renaming declaration.
5554 case Nkind (Unit (Library_Unit (N))) is
5555 when N_Package_Declaration =>
5556 null;
5558 when N_Subprogram_Declaration =>
5559 Error_Msg_N ("subprograms not allowed in "
5560 & "limited with_clauses", N);
5561 return;
5563 when N_Generic_Package_Declaration |
5564 N_Generic_Subprogram_Declaration =>
5565 Error_Msg_N ("generics not allowed in "
5566 & "limited with_clauses", N);
5567 return;
5569 when N_Generic_Instantiation =>
5570 Error_Msg_N ("generic instantiations not allowed in "
5571 & "limited with_clauses", N);
5572 return;
5574 when N_Generic_Renaming_Declaration =>
5575 Error_Msg_N ("generic renamings not allowed in "
5576 & "limited with_clauses", N);
5577 return;
5579 when N_Subprogram_Renaming_Declaration =>
5580 Error_Msg_N ("renamed subprograms not allowed in "
5581 & "limited with_clauses", N);
5582 return;
5584 when N_Package_Renaming_Declaration =>
5585 Error_Msg_N ("renamed packages not allowed in "
5586 & "limited with_clauses", N);
5587 return;
5589 when others =>
5590 raise Program_Error;
5591 end case;
5593 -- Check if the chain is already built
5595 Spec := Specification (Unit (Library_Unit (N)));
5597 if Limited_View_Installed (Spec) then
5598 return;
5599 end if;
5601 Set_Ekind (P, E_Package);
5603 -- Build the header of the limited_view
5605 Lim_Header := Make_Temporary (Sloc (N), 'Z');
5606 Set_Ekind (Lim_Header, E_Package);
5607 Set_Is_Internal (Lim_Header);
5608 Set_Limited_View (P, Lim_Header);
5610 -- Create the auxiliary chain. All the shadow entities are appended to
5611 -- the list of entities of the limited-view header
5613 Build_Chain
5614 (Scope => P,
5615 First_Decl => First (Visible_Declarations (Spec)));
5617 -- Save the last built shadow entity. It is needed later to set the
5618 -- reference to the first shadow entity in the private part
5620 Last_Pub_Lim_E := Last_Lim_E;
5622 -- Ada 2005 (AI-262): Add the limited view of the private declarations
5623 -- Required to give support to limited-private-with clauses
5625 Build_Chain (Scope => P,
5626 First_Decl => First (Private_Declarations (Spec)));
5628 if Last_Pub_Lim_E /= Empty then
5629 Set_First_Private_Entity
5630 (Lim_Header, Next_Entity (Last_Pub_Lim_E));
5631 else
5632 Set_First_Private_Entity
5633 (Lim_Header, First_Entity (P));
5634 end if;
5636 Set_Limited_View_Installed (Spec);
5637 end Build_Limited_Views;
5639 -------------------------------
5640 -- Check_Body_Needed_For_SAL --
5641 -------------------------------
5643 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5645 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5646 -- Determine whether use of entity E might require the presence of its
5647 -- body. For a package this requires a recursive traversal of all nested
5648 -- declarations.
5650 ---------------------------
5651 -- Entity_Needed_For_SAL --
5652 ---------------------------
5654 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5655 Ent : Entity_Id;
5657 begin
5658 if Is_Subprogram (E)
5659 and then Has_Pragma_Inline (E)
5660 then
5661 return True;
5663 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
5664 return True;
5666 elsif Ekind (E) = E_Generic_Package
5667 and then
5668 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5669 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5670 then
5671 return True;
5673 elsif Ekind (E) = E_Package
5674 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5675 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5676 then
5677 Ent := First_Entity (E);
5678 while Present (Ent) loop
5679 if Entity_Needs_Body (Ent) then
5680 return True;
5681 end if;
5683 Next_Entity (Ent);
5684 end loop;
5686 return False;
5688 else
5689 return False;
5690 end if;
5691 end Entity_Needs_Body;
5693 -- Start of processing for Check_Body_Needed_For_SAL
5695 begin
5696 if Ekind (Unit_Name) = E_Generic_Package
5697 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5698 N_Generic_Package_Declaration
5699 and then
5700 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5701 then
5702 Set_Body_Needed_For_SAL (Unit_Name);
5704 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
5705 Set_Body_Needed_For_SAL (Unit_Name);
5707 elsif Is_Subprogram (Unit_Name)
5708 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5709 N_Subprogram_Declaration
5710 and then Has_Pragma_Inline (Unit_Name)
5711 then
5712 Set_Body_Needed_For_SAL (Unit_Name);
5714 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5715 Check_Body_Needed_For_SAL
5716 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5718 elsif Ekind (Unit_Name) = E_Package
5719 and then Entity_Needs_Body (Unit_Name)
5720 then
5721 Set_Body_Needed_For_SAL (Unit_Name);
5723 elsif Ekind (Unit_Name) = E_Package_Body
5724 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5725 then
5726 Check_Body_Needed_For_SAL
5727 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5728 end if;
5729 end Check_Body_Needed_For_SAL;
5731 --------------------
5732 -- Remove_Context --
5733 --------------------
5735 procedure Remove_Context (N : Node_Id) is
5736 Lib_Unit : constant Node_Id := Unit (N);
5738 begin
5739 -- If this is a child unit, first remove the parent units
5741 if Is_Child_Spec (Lib_Unit) then
5742 Remove_Parents (Lib_Unit);
5743 end if;
5745 Remove_Context_Clauses (N);
5746 end Remove_Context;
5748 ----------------------------
5749 -- Remove_Context_Clauses --
5750 ----------------------------
5752 procedure Remove_Context_Clauses (N : Node_Id) is
5753 Item : Node_Id;
5754 Unit_Name : Entity_Id;
5756 begin
5757 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5758 -- limited-views first and regular-views later (to maintain the
5759 -- stack model).
5761 -- First Phase: Remove limited_with context clauses
5763 Item := First (Context_Items (N));
5764 while Present (Item) loop
5766 -- We are interested only in with clauses which got installed
5767 -- on entry.
5769 if Nkind (Item) = N_With_Clause
5770 and then Limited_Present (Item)
5771 and then Limited_View_Installed (Item)
5772 then
5773 Remove_Limited_With_Clause (Item);
5774 end if;
5776 Next (Item);
5777 end loop;
5779 -- Second Phase: Loop through context items and undo regular
5780 -- with_clauses and use_clauses.
5782 Item := First (Context_Items (N));
5783 while Present (Item) loop
5785 -- We are interested only in with clauses which got installed on
5786 -- entry, as indicated by their Context_Installed flag set
5788 if Nkind (Item) = N_With_Clause
5789 and then Limited_Present (Item)
5790 and then Limited_View_Installed (Item)
5791 then
5792 null;
5794 elsif Nkind (Item) = N_With_Clause
5795 and then Context_Installed (Item)
5796 then
5797 -- Remove items from one with'ed unit
5799 Unit_Name := Entity (Name (Item));
5800 Remove_Unit_From_Visibility (Unit_Name);
5801 Set_Context_Installed (Item, False);
5803 elsif Nkind (Item) = N_Use_Package_Clause then
5804 End_Use_Package (Item);
5806 elsif Nkind (Item) = N_Use_Type_Clause then
5807 End_Use_Type (Item);
5808 end if;
5810 Next (Item);
5811 end loop;
5812 end Remove_Context_Clauses;
5814 --------------------------------
5815 -- Remove_Limited_With_Clause --
5816 --------------------------------
5818 procedure Remove_Limited_With_Clause (N : Node_Id) is
5819 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5820 E : Entity_Id;
5821 P : Entity_Id;
5822 Lim_Header : Entity_Id;
5823 Lim_Typ : Entity_Id;
5824 Prev : Entity_Id;
5826 begin
5827 pragma Assert (Limited_View_Installed (N));
5829 -- In case of limited with_clause on subprograms, generics, instances,
5830 -- or renamings, the corresponding error was previously posted and we
5831 -- have nothing to do here.
5833 if Nkind (P_Unit) /= N_Package_Declaration then
5834 return;
5835 end if;
5837 P := Defining_Unit_Name (Specification (P_Unit));
5839 -- Handle child packages
5841 if Nkind (P) = N_Defining_Program_Unit_Name then
5842 P := Defining_Identifier (P);
5843 end if;
5845 if Debug_Flag_I then
5846 Write_Str ("remove limited view of ");
5847 Write_Name (Chars (P));
5848 Write_Str (" from visibility");
5849 Write_Eol;
5850 end if;
5852 -- Prepare the removal of the shadow entities from visibility. The first
5853 -- element of the limited view is a header (an E_Package entity) that is
5854 -- used to reference the first shadow entity in the private part of the
5855 -- package
5857 Lim_Header := Limited_View (P);
5858 Lim_Typ := First_Entity (Lim_Header);
5860 -- Remove package and shadow entities from visibility if it has not
5861 -- been analyzed
5863 if not Analyzed (P_Unit) then
5864 Unchain (P);
5865 Set_Is_Immediately_Visible (P, False);
5867 while Present (Lim_Typ) loop
5868 Unchain (Lim_Typ);
5869 Next_Entity (Lim_Typ);
5870 end loop;
5872 -- Otherwise this package has already appeared in the closure and its
5873 -- shadow entities must be replaced by its real entities. This code
5874 -- must be kept synchronized with the complementary code in Install
5875 -- Limited_Withed_Unit.
5877 else
5878 -- Real entities that are type or subtype declarations were hidden
5879 -- from visibility at the point of installation of the limited-view.
5880 -- Now we recover the previous value of the hidden attribute.
5882 E := First_Entity (P);
5883 while Present (E) and then E /= First_Private_Entity (P) loop
5884 if Is_Type (E) then
5885 Set_Is_Hidden (E, Was_Hidden (E));
5886 end if;
5888 Next_Entity (E);
5889 end loop;
5891 while Present (Lim_Typ)
5892 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5893 loop
5894 -- Nested packages and child units were not unchained
5896 if Ekind (Lim_Typ) /= E_Package
5897 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5898 then
5899 -- If the package has incomplete types, the limited view of the
5900 -- incomplete type is in fact never visible (AI05-129) but we
5901 -- have created a shadow entity E1 for it, that points to E2,
5902 -- a non-limited incomplete type. This in turn has a full view
5903 -- E3 that is the full declaration. There is a corresponding
5904 -- shadow entity E4. When reinstalling the non-limited view,
5905 -- E2 must become the current entity and E3 must be ignored.
5907 E := Non_Limited_View (Lim_Typ);
5909 if Present (Current_Entity (E))
5910 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
5911 and then Full_View (Current_Entity (E)) = E
5912 then
5914 -- Lim_Typ is the limited view of a full type declaration
5915 -- that has a previous incomplete declaration, i.e. E3 from
5916 -- the previous description. Nothing to insert.
5918 null;
5920 else
5921 pragma Assert (not In_Chain (E));
5923 Prev := Current_Entity (Lim_Typ);
5925 if Prev = Lim_Typ then
5926 Set_Current_Entity (E);
5928 else
5929 while Present (Prev)
5930 and then Homonym (Prev) /= Lim_Typ
5931 loop
5932 Prev := Homonym (Prev);
5933 end loop;
5935 if Present (Prev) then
5936 Set_Homonym (Prev, E);
5937 end if;
5938 end if;
5940 -- Preserve structure of homonym chain
5942 Set_Homonym (E, Homonym (Lim_Typ));
5943 end if;
5944 end if;
5946 Next_Entity (Lim_Typ);
5947 end loop;
5948 end if;
5950 -- Indicate that the limited view of the package is not installed
5952 Set_From_With_Type (P, False);
5953 Set_Limited_View_Installed (N, False);
5954 end Remove_Limited_With_Clause;
5956 --------------------
5957 -- Remove_Parents --
5958 --------------------
5960 procedure Remove_Parents (Lib_Unit : Node_Id) is
5961 P : Node_Id;
5962 P_Name : Entity_Id;
5963 P_Spec : Node_Id := Empty;
5964 E : Entity_Id;
5965 Vis : constant Boolean :=
5966 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
5968 begin
5969 if Is_Child_Spec (Lib_Unit) then
5970 P_Spec := Parent_Spec (Lib_Unit);
5972 elsif Nkind (Lib_Unit) = N_Package_Body
5973 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
5974 then
5975 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
5976 end if;
5978 if Present (P_Spec) then
5979 P := Unit (P_Spec);
5980 P_Name := Get_Parent_Entity (P);
5981 Remove_Context_Clauses (P_Spec);
5982 End_Package_Scope (P_Name);
5983 Set_Is_Immediately_Visible (P_Name, Vis);
5985 -- Remove from visibility the siblings as well, which are directly
5986 -- visible while the parent is in scope.
5988 E := First_Entity (P_Name);
5989 while Present (E) loop
5990 if Is_Child_Unit (E) then
5991 Set_Is_Immediately_Visible (E, False);
5992 end if;
5994 Next_Entity (E);
5995 end loop;
5997 Set_In_Package_Body (P_Name, False);
5999 -- This is the recursive call to remove the context of any higher
6000 -- level parent. This recursion ensures that all parents are removed
6001 -- in the reverse order of their installation.
6003 Remove_Parents (P);
6004 end if;
6005 end Remove_Parents;
6007 ---------------------------------
6008 -- Remove_Private_With_Clauses --
6009 ---------------------------------
6011 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6012 Item : Node_Id;
6014 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6015 -- Check whether a given unit appears in a regular with_clause. Used to
6016 -- determine whether a private_with_clause, implicit or explicit, should
6017 -- be ignored.
6019 ----------------------------
6020 -- In_Regular_With_Clause --
6021 ----------------------------
6023 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6025 Item : Node_Id;
6027 begin
6028 Item := First (Context_Items (Comp_Unit));
6029 while Present (Item) loop
6030 if Nkind (Item) = N_With_Clause
6031 and then Entity (Name (Item)) = E
6032 and then not Private_Present (Item)
6033 then
6034 return True;
6035 end if;
6036 Next (Item);
6037 end loop;
6039 return False;
6040 end In_Regular_With_Clause;
6042 -- Start of processing for Remove_Private_With_Clauses
6044 begin
6045 Item := First (Context_Items (Comp_Unit));
6046 while Present (Item) loop
6047 if Nkind (Item) = N_With_Clause
6048 and then Private_Present (Item)
6049 then
6050 -- If private_with_clause is redundant, remove it from context,
6051 -- as a small optimization to subsequent handling of private_with
6052 -- clauses in other nested packages.
6054 if In_Regular_With_Clause (Entity (Name (Item))) then
6055 declare
6056 Nxt : constant Node_Id := Next (Item);
6057 begin
6058 Remove (Item);
6059 Item := Nxt;
6060 end;
6062 elsif Limited_Present (Item) then
6063 if not Limited_View_Installed (Item) then
6064 Remove_Limited_With_Clause (Item);
6065 end if;
6067 Next (Item);
6069 else
6070 Remove_Unit_From_Visibility (Entity (Name (Item)));
6071 Set_Context_Installed (Item, False);
6072 Next (Item);
6073 end if;
6075 else
6076 Next (Item);
6077 end if;
6078 end loop;
6079 end Remove_Private_With_Clauses;
6081 ---------------------------------
6082 -- Remove_Unit_From_Visibility --
6083 ---------------------------------
6085 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6086 P : constant Entity_Id := Scope (Unit_Name);
6088 begin
6089 if Debug_Flag_I then
6090 Write_Str ("remove unit ");
6091 Write_Name (Chars (Unit_Name));
6092 Write_Str (" from visibility");
6093 Write_Eol;
6094 end if;
6096 if P /= Standard_Standard then
6097 Set_Is_Visible_Child_Unit (Unit_Name, False);
6098 end if;
6100 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6101 Set_Is_Immediately_Visible (Unit_Name, False);
6102 end Remove_Unit_From_Visibility;
6104 --------
6105 -- sm --
6106 --------
6108 procedure sm is
6109 begin
6110 null;
6111 end sm;
6113 -------------
6114 -- Unchain --
6115 -------------
6117 procedure Unchain (E : Entity_Id) is
6118 Prev : Entity_Id;
6120 begin
6121 Prev := Current_Entity (E);
6123 if No (Prev) then
6124 return;
6126 elsif Prev = E then
6127 Set_Name_Entity_Id (Chars (E), Homonym (E));
6129 else
6130 while Present (Prev)
6131 and then Homonym (Prev) /= E
6132 loop
6133 Prev := Homonym (Prev);
6134 end loop;
6136 if Present (Prev) then
6137 Set_Homonym (Prev, Homonym (E));
6138 end if;
6139 end if;
6141 if Debug_Flag_I then
6142 Write_Str (" (homonym) unchain ");
6143 Write_Name (Chars (E));
6144 Write_Eol;
6145 end if;
6146 end Unchain;
6148 end Sem_Ch10;