PR c/79855: add full stop to store merging param descriptions
[official-gcc.git] / gcc / ada / sem_ch10.adb
blobf4268a0d903a1e8113eed170c9f47527a8c0cd8d
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-2016, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Exp_Util; use Exp_Util;
33 with Elists; use Elists;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Impunit; use Impunit;
39 with Inline; use Inline;
40 with Lib; use Lib;
41 with Lib.Load; use Lib.Load;
42 with Lib.Xref; use Lib.Xref;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Output; use Output;
48 with Par_SCO; use Par_SCO;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch7; use Sem_Ch7;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Prag; use Sem_Prag;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Sinfo.CN; use Sinfo.CN;
65 with Sinput; use Sinput;
66 with Snames; use Snames;
67 with Style; use Style;
68 with Stylesw; use Stylesw;
69 with Tbuild; use Tbuild;
70 with Uname; use Uname;
72 package body Sem_Ch10 is
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 procedure Analyze_Context (N : Node_Id);
79 -- Analyzes items in the context clause of compilation unit
81 procedure Build_Limited_Views (N : Node_Id);
82 -- Build and decorate the list of shadow entities for a package mentioned
83 -- in a limited_with clause. If the package was not previously analyzed
84 -- then it also performs a basic decoration of the real entities. This is
85 -- required in order to avoid passing non-decorated entities to the
86 -- back-end. Implements Ada 2005 (AI-50217).
88 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
89 -- Common processing for all stubs (subprograms, tasks, packages, and
90 -- protected cases). N is the stub to be analyzed. Once the subunit name
91 -- is established, load and analyze. Nam is the non-overloadable entity
92 -- for which the proper body provides a completion. Subprogram stubs are
93 -- handled differently because they can be declarations.
95 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
96 -- Check whether the source for the body of a compilation unit must be
97 -- included in a standalone library.
99 procedure Check_No_Elab_Code_All (N : Node_Id);
100 -- Carries out possible tests for violation of No_Elab_Code all for withed
101 -- units in the Context_Items of unit N.
103 procedure Check_Private_Child_Unit (N : Node_Id);
104 -- If a with_clause mentions a private child unit, the compilation unit
105 -- must be a member of the same family, as described in 10.1.2.
107 procedure Check_Stub_Level (N : Node_Id);
108 -- Verify that a stub is declared immediately within a compilation unit,
109 -- and not in an inner frame.
111 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
112 -- When a child unit appears in a context clause, the implicit withs on
113 -- parents are made explicit, and with clauses are inserted in the context
114 -- clause before the one for the child. If a parent in the with_clause
115 -- is a renaming, the implicit with_clause is on the renaming whose name
116 -- is mentioned in the with_clause, and not on the package it renames.
117 -- N is the compilation unit whose list of context items receives the
118 -- implicit with_clauses.
120 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
121 -- Generate cross-reference information for the parents of child units
122 -- and of subunits. N is a defining_program_unit_name, and P_Id is the
123 -- immediate parent scope.
125 function Has_With_Clause
126 (C_Unit : Node_Id;
127 Pack : Entity_Id;
128 Is_Limited : Boolean := False) return Boolean;
129 -- Determine whether compilation unit C_Unit contains a [limited] with
130 -- clause for package Pack. Use the flag Is_Limited to designate desired
131 -- clause kind.
133 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
134 -- If the main unit is a child unit, implicit withs are also added for
135 -- all its ancestors.
137 function In_Chain (E : Entity_Id) return Boolean;
138 -- Check that the shadow entity is not already in the homonym chain, for
139 -- example through a limited_with clause in a parent unit.
141 procedure Install_Context_Clauses (N : Node_Id);
142 -- Subsidiary to Install_Context and Install_Parents. Process all with
143 -- and use clauses for current unit and its library unit if any.
145 procedure Install_Limited_Context_Clauses (N : Node_Id);
146 -- Subsidiary to Install_Context. Process only limited with_clauses for
147 -- current unit. Implements Ada 2005 (AI-50217).
149 procedure Install_Limited_Withed_Unit (N : Node_Id);
150 -- Place shadow entities for a limited_with package in the visibility
151 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
153 procedure Install_Withed_Unit
154 (With_Clause : Node_Id;
155 Private_With_OK : Boolean := False);
156 -- If the unit is not a child unit, make unit immediately visible. The
157 -- caller ensures that the unit is not already currently installed. The
158 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
159 -- is called when compiling the private part of a package, or installing
160 -- the private declarations of a parent unit.
162 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
163 -- This procedure establishes the context for the compilation of a child
164 -- unit. If Lib_Unit is a child library spec then the context of the parent
165 -- is installed, and the parent itself made immediately visible, so that
166 -- the child unit is processed in the declarative region of the parent.
167 -- Install_Parents makes a recursive call to itself to ensure that all
168 -- parents are loaded in the nested case. If Lib_Unit is a library body,
169 -- the only effect of Install_Parents is to install the private decls of
170 -- the parents, because the visible parent declarations will have been
171 -- installed as part of the context of the corresponding spec.
173 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
174 -- In the compilation of a child unit, a child of any of the ancestor
175 -- units is directly visible if it is visible, because the parent is in
176 -- an enclosing scope. Iterate over context to find child units of U_Name
177 -- or of some ancestor of it.
179 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
180 -- When compiling a unit Q descended from some parent unit P, a limited
181 -- with_clause in the context of P that names some other ancestor of Q
182 -- must not be installed because the ancestor is immediately visible.
184 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
185 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
186 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
187 -- a library spec that has a parent. If the call to Is_Child_Spec returns
188 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
189 -- compilation unit for the parent spec.
191 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
192 -- Parent_Spec is non-empty, this is also a child unit.
194 procedure Remove_Context_Clauses (N : Node_Id);
195 -- Subsidiary of previous one. Remove use_ and with_clauses
197 procedure Remove_Limited_With_Clause (N : Node_Id);
198 -- Remove from visibility the shadow entities introduced for a package
199 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
201 procedure Remove_Parents (Lib_Unit : Node_Id);
202 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
203 -- contexts established by the corresponding call to Install_Parents are
204 -- removed. Remove_Parents contains a recursive call to itself to ensure
205 -- that all parents are removed in the nested case.
207 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
208 -- Reset all visibility flags on unit after compiling it, either as a main
209 -- unit or as a unit in the context.
211 procedure Unchain (E : Entity_Id);
212 -- Remove single entity from visibility list
214 procedure sm;
215 -- A dummy procedure, for debugging use, called just before analyzing the
216 -- main unit (after dealing with any context clauses).
218 --------------------------
219 -- Limited_With_Clauses --
220 --------------------------
222 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
223 -- mutually recursive types declared in different units. A limited_with
224 -- clause that names package P in the context of unit U makes the types
225 -- declared in the visible part of P available within U, but with the
226 -- restriction that these types can only be used as incomplete types.
227 -- The limited_with clause does not impose a semantic dependence on P,
228 -- and it is possible for two packages to have limited_with_clauses on
229 -- each other without creating an elaboration circularity.
231 -- To support this feature, the analysis of a limited_with clause must
232 -- create an abbreviated view of the package, without performing any
233 -- semantic analysis on it. This "package abstract" contains shadow types
234 -- that are in one-one correspondence with the real types in the package,
235 -- and that have the properties of incomplete types.
237 -- The implementation creates two element lists: one to chain the shadow
238 -- entities, and one to chain the corresponding type entities in the tree
239 -- of the package. Links between corresponding entities in both chains
240 -- allow the compiler to select the proper view of a given type, depending
241 -- on the context. Note that in contrast with the handling of private
242 -- types, the limited view and the non-limited view of a type are treated
243 -- as separate entities, and no entity exchange needs to take place, which
244 -- makes the implementation much simpler than could be feared.
246 ------------------------------
247 -- Analyze_Compilation_Unit --
248 ------------------------------
250 procedure Analyze_Compilation_Unit (N : Node_Id) is
251 procedure Check_Redundant_Withs
252 (Context_Items : List_Id;
253 Spec_Context_Items : List_Id := No_List);
254 -- Determine whether the context list of a compilation unit contains
255 -- redundant with clauses. When checking body clauses against spec
256 -- clauses, set Context_Items to the context list of the body and
257 -- Spec_Context_Items to that of the spec. Parent packages are not
258 -- examined for documentation purposes.
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 : out Boolean;
274 Used_Type_Or_Elab : 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 : out Boolean;
284 Withed : 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 : out Boolean;
303 Used_Type_Or_Elab : 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 (Present (Renamed_Object (P))
326 and then Entity (N) = Renamed_Object (P));
327 end Same_Unit;
329 -- Start of processing for Process_Body_Clauses
331 begin
332 Used := False;
333 Used_Type_Or_Elab := False;
335 Cont_Item := First (Context_List);
336 while Present (Cont_Item) loop
338 -- Package use clause
340 if Nkind (Cont_Item) = N_Use_Package_Clause
341 and then not Used
342 then
343 -- Search through use clauses
345 Use_Item := First (Names (Cont_Item));
346 while Present (Use_Item) and then not Used loop
348 -- Case of a direct use of the one we are looking for
350 if Entity (Use_Item) = Nam_Ent then
351 Used := True;
353 -- Handle nested case, as in "with P; use P.Q.R"
355 else
356 declare
357 UE : Node_Id;
359 begin
360 -- Loop through prefixes looking for match
362 UE := Use_Item;
363 while Nkind (UE) = N_Expanded_Name loop
364 if Same_Unit (Prefix (UE), Nam_Ent) then
365 Used := True;
366 exit;
367 end if;
369 UE := Prefix (UE);
370 end loop;
371 end;
372 end if;
374 Next (Use_Item);
375 end loop;
377 -- USE TYPE clause
379 elsif Nkind (Cont_Item) = N_Use_Type_Clause
380 and then not Used_Type_Or_Elab
381 then
382 Subt_Mark := First (Subtype_Marks (Cont_Item));
383 while Present (Subt_Mark)
384 and then not Used_Type_Or_Elab
385 loop
386 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
387 Used_Type_Or_Elab := True;
388 end if;
390 Next (Subt_Mark);
391 end loop;
393 -- Pragma Elaborate or Elaborate_All
395 elsif Nkind (Cont_Item) = N_Pragma
396 and then
397 Nam_In (Pragma_Name_Unmapped (Cont_Item),
398 Name_Elaborate, Name_Elaborate_All)
399 and then not Used_Type_Or_Elab
400 then
401 Prag_Unit :=
402 First (Pragma_Argument_Associations (Cont_Item));
403 while Present (Prag_Unit) and then not Used_Type_Or_Elab loop
404 if Entity (Expression (Prag_Unit)) = Nam_Ent then
405 Used_Type_Or_Elab := True;
406 end if;
408 Next (Prag_Unit);
409 end loop;
410 end if;
412 Next (Cont_Item);
413 end loop;
414 end Process_Body_Clauses;
416 --------------------------
417 -- Process_Spec_Clauses --
418 --------------------------
420 procedure Process_Spec_Clauses
421 (Context_List : List_Id;
422 Clause : Node_Id;
423 Used : out Boolean;
424 Withed : out Boolean;
425 Exit_On_Self : Boolean := False)
427 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
428 Cont_Item : Node_Id;
429 Use_Item : Node_Id;
431 begin
432 Used := False;
433 Withed := False;
435 Cont_Item := First (Context_List);
436 while Present (Cont_Item) loop
438 -- Stop the search since the context items after Cont_Item have
439 -- already been examined in a previous iteration of the reverse
440 -- loop in Check_Redundant_Withs.
442 if Exit_On_Self
443 and Cont_Item = Clause
444 then
445 exit;
446 end if;
448 -- Package use clause
450 if Nkind (Cont_Item) = N_Use_Package_Clause
451 and then not Used
452 then
453 Use_Item := First (Names (Cont_Item));
454 while Present (Use_Item) and then not Used loop
455 if Entity (Use_Item) = Nam_Ent then
456 Used := True;
457 end if;
459 Next (Use_Item);
460 end loop;
462 -- Package with clause. Avoid processing self, implicitly
463 -- generated with clauses or limited with clauses. Note that
464 -- we examine with clauses having pragmas Elaborate or
465 -- Elaborate_All applied to them due to cases such as:
467 -- with Pack;
468 -- with Pack;
469 -- pragma Elaborate (Pack);
471 -- In this case, the second with clause is redundant since
472 -- the pragma applies only to the first "with Pack;".
474 -- Note that we only consider with_clauses that comes from
475 -- source. In the case of renamings used as prefixes of names
476 -- in with_clauses, we generate a with_clause for the prefix,
477 -- which we do not treat as implicit because it is needed for
478 -- visibility analysis, but is also not redundant.
480 elsif Nkind (Cont_Item) = N_With_Clause
481 and then not Implicit_With (Cont_Item)
482 and then Comes_From_Source (Cont_Item)
483 and then not Limited_Present (Cont_Item)
484 and then Cont_Item /= Clause
485 and then Entity (Name (Cont_Item)) = Nam_Ent
486 then
487 Withed := True;
488 end if;
490 Next (Cont_Item);
491 end loop;
492 end Process_Spec_Clauses;
494 -- Start of processing for Check_Redundant_Withs
496 begin
497 Clause := Last (Context_Items);
498 while Present (Clause) loop
500 -- Avoid checking implicitly generated with clauses, limited with
501 -- clauses or withs that have pragma Elaborate or Elaborate_All.
503 if Nkind (Clause) = N_With_Clause
504 and then not Implicit_With (Clause)
505 and then not Limited_Present (Clause)
506 and then not Elaborate_Present (Clause)
508 -- With_clauses introduced for renamings of parent clauses
509 -- are not marked implicit because they need to be properly
510 -- installed, but they do not come from source and do not
511 -- require warnings.
513 and then Comes_From_Source (Clause)
514 then
515 -- Package body-to-spec check
517 if Present (Spec_Context_Items) then
518 declare
519 Used_In_Body : Boolean;
520 Used_In_Spec : Boolean;
521 Used_Type_Or_Elab : Boolean;
522 Withed_In_Spec : Boolean;
524 begin
525 Process_Spec_Clauses
526 (Context_List => Spec_Context_Items,
527 Clause => Clause,
528 Used => Used_In_Spec,
529 Withed => Withed_In_Spec);
531 Process_Body_Clauses
532 (Context_List => Context_Items,
533 Clause => Clause,
534 Used => Used_In_Body,
535 Used_Type_Or_Elab => Used_Type_Or_Elab);
537 -- "Type Elab" refers to the presence of either a use
538 -- type clause, pragmas Elaborate or Elaborate_All.
540 -- +---------------+---------------------------+------+
541 -- | Spec | Body | Warn |
542 -- +--------+------+--------+------+-----------+------+
543 -- | Withed | Used | Withed | Used | Type Elab | |
544 -- | X | | X | | | X |
545 -- | X | | X | X | | |
546 -- | X | | X | | X | |
547 -- | X | | X | X | X | |
548 -- | X | X | X | | | X |
549 -- | X | X | X | | X | |
550 -- | X | X | X | X | | X |
551 -- | X | X | X | X | X | |
552 -- +--------+------+--------+------+-----------+------+
554 if (Withed_In_Spec
555 and then not Used_Type_Or_Elab)
556 and then
557 ((not Used_In_Spec and then not Used_In_Body)
558 or else Used_In_Spec)
559 then
560 Error_Msg_N -- CODEFIX
561 ("redundant with clause in body?r?", Clause);
562 end if;
564 Used_In_Body := False;
565 Used_In_Spec := False;
566 Used_Type_Or_Elab := False;
567 Withed_In_Spec := False;
568 end;
570 -- Standalone package spec or body check
572 else
573 declare
574 Dont_Care : Boolean := False;
575 Withed : Boolean := False;
577 begin
578 -- The mechanism for examining the context clauses of a
579 -- package spec can be applied to package body clauses.
581 Process_Spec_Clauses
582 (Context_List => Context_Items,
583 Clause => Clause,
584 Used => Dont_Care,
585 Withed => Withed,
586 Exit_On_Self => True);
588 if Withed then
589 Error_Msg_N -- CODEFIX
590 ("redundant with clause?r?", Clause);
591 end if;
592 end;
593 end if;
594 end if;
596 Prev (Clause);
597 end loop;
598 end Check_Redundant_Withs;
600 -- Local variables
602 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
603 Unit_Node : constant Node_Id := Unit (N);
604 Lib_Unit : Node_Id := Library_Unit (N);
605 Par_Spec_Name : Unit_Name_Type;
606 Spec_Id : Entity_Id;
607 Unum : Unit_Number_Type;
609 -- Start of processing for Analyze_Compilation_Unit
611 begin
612 Process_Compilation_Unit_Pragmas (N);
614 -- If the unit is a subunit whose parent has not been analyzed (which
615 -- indicates that the main unit is a subunit, either the current one or
616 -- one of its descendants) then the subunit is compiled as part of the
617 -- analysis of the parent, which we proceed to do. Basically this gets
618 -- handled from the top down and we don't want to do anything at this
619 -- level (i.e. this subunit will be handled on the way down from the
620 -- parent), so at this level we immediately return. If the subunit ends
621 -- up not analyzed, it means that the parent did not contain a stub for
622 -- it, or that there errors were detected in some ancestor.
624 if Nkind (Unit_Node) = N_Subunit and then not Analyzed (Lib_Unit) then
625 Semantics (Lib_Unit);
627 if not Analyzed (Proper_Body (Unit_Node)) then
628 if Serious_Errors_Detected > 0 then
629 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
630 else
631 Error_Msg_N ("missing stub for subunit", N);
632 end if;
633 end if;
635 return;
636 end if;
638 -- Analyze context (this will call Sem recursively for with'ed units) To
639 -- detect circularities among with-clauses that are not caught during
640 -- loading, we set the Context_Pending flag on the current unit. If the
641 -- flag is already set there is a potential circularity. We exclude
642 -- predefined units from this check because they are known to be safe.
643 -- We also exclude package bodies that are present because circularities
644 -- between bodies are harmless (and necessary).
646 if Context_Pending (N) then
647 declare
648 Circularity : Boolean := True;
650 begin
651 if Is_Predefined_File_Name
652 (Unit_File_Name (Get_Source_Unit (Unit (N))))
653 then
654 Circularity := False;
656 else
657 for U in Main_Unit + 1 .. Last_Unit loop
658 if Nkind (Unit (Cunit (U))) = N_Package_Body
659 and then not Analyzed (Cunit (U))
660 then
661 Circularity := False;
662 exit;
663 end if;
664 end loop;
665 end if;
667 if Circularity then
668 Error_Msg_N ("circular dependency caused by with_clauses", N);
669 Error_Msg_N
670 ("\possibly missing limited_with clause"
671 & " in one of the following", N);
673 for U in Main_Unit .. Last_Unit loop
674 if Context_Pending (Cunit (U)) then
675 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
676 Error_Msg_N ("\unit$", N);
677 end if;
678 end loop;
680 raise Unrecoverable_Error;
681 end if;
682 end;
683 else
684 Set_Context_Pending (N);
685 end if;
687 Analyze_Context (N);
689 Set_Context_Pending (N, False);
691 -- If the unit is a package body, the spec is already loaded and must be
692 -- analyzed first, before we analyze the body.
694 if Nkind (Unit_Node) = N_Package_Body then
696 -- If no Lib_Unit, then there was a serious previous error, so just
697 -- ignore the entire analysis effort.
699 if No (Lib_Unit) then
700 Check_Error_Detected;
701 return;
703 else
704 -- Analyze the package spec
706 Semantics (Lib_Unit);
708 -- Check for unused with's
710 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
712 -- Verify that the library unit is a package declaration
714 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
715 N_Generic_Package_Declaration)
716 then
717 Error_Msg_N
718 ("no legal package declaration for package body", N);
719 return;
721 -- Otherwise, the entity in the declaration is visible. Update the
722 -- version to reflect dependence of this body on the spec.
724 else
725 Spec_Id := Defining_Entity (Unit (Lib_Unit));
726 Set_Is_Immediately_Visible (Spec_Id, True);
727 Version_Update (N, Lib_Unit);
729 if Nkind (Defining_Unit_Name (Unit_Node)) =
730 N_Defining_Program_Unit_Name
731 then
732 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
733 end if;
734 end if;
735 end if;
737 -- If the unit is a subprogram body, then we similarly need to analyze
738 -- its spec. However, things are a little simpler in this case, because
739 -- here, this analysis is done mostly for error checking and consistency
740 -- purposes (but not only, e.g. there could be a contract on the spec),
741 -- so there's nothing else to be done.
743 elsif Nkind (Unit_Node) = N_Subprogram_Body then
744 if Acts_As_Spec (N) then
746 -- If the subprogram body is a child unit, we must create a
747 -- declaration for it, in order to properly load the parent(s).
748 -- After this, the original unit does not acts as a spec, because
749 -- there is an explicit one. If this unit appears in a context
750 -- clause, then an implicit with on the parent will be added when
751 -- installing the context. If this is the main unit, there is no
752 -- Unit_Table entry for the declaration (it has the unit number
753 -- of the main unit) and code generation is unaffected.
755 Unum := Get_Cunit_Unit_Number (N);
756 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
758 if Par_Spec_Name /= No_Unit_Name then
759 Unum :=
760 Load_Unit
761 (Load_Name => Par_Spec_Name,
762 Required => True,
763 Subunit => False,
764 Error_Node => N);
766 if Unum /= No_Unit then
768 -- Build subprogram declaration and attach parent unit to it
769 -- This subprogram declaration does not come from source,
770 -- Nevertheless the backend must generate debugging info for
771 -- it, and this must be indicated explicitly. We also mark
772 -- the body entity as a child unit now, to prevent a
773 -- cascaded error if the spec entity cannot be entered
774 -- in its scope. Finally we create a Units table entry for
775 -- the subprogram declaration, to maintain a one-to-one
776 -- correspondence with compilation unit nodes. This is
777 -- critical for the tree traversals performed by CodePeer.
779 declare
780 Loc : constant Source_Ptr := Sloc (N);
781 SCS : constant Boolean :=
782 Get_Comes_From_Source_Default;
784 begin
785 Set_Comes_From_Source_Default (False);
787 -- Note: We copy the Context_Items from the explicit body
788 -- to the implicit spec, setting the former to Empty_List
789 -- to preserve the treeish nature of the tree, during
790 -- analysis of the spec. Then we put it back the way it
791 -- was -- copy the Context_Items from the spec to the
792 -- body, and set the spec Context_Items to Empty_List.
793 -- It is necessary to preserve the treeish nature,
794 -- because otherwise we will call End_Use_* twice on the
795 -- same thing.
797 Lib_Unit :=
798 Make_Compilation_Unit (Loc,
799 Context_Items => Context_Items (N),
800 Unit =>
801 Make_Subprogram_Declaration (Sloc (N),
802 Specification =>
803 Copy_Separate_Tree
804 (Specification (Unit_Node))),
805 Aux_Decls_Node =>
806 Make_Compilation_Unit_Aux (Loc));
808 Set_Context_Items (N, Empty_List);
809 Set_Library_Unit (N, Lib_Unit);
810 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
811 Make_Child_Decl_Unit (N);
812 Semantics (Lib_Unit);
814 -- Now that a separate declaration exists, the body
815 -- of the child unit does not act as spec any longer.
817 Set_Acts_As_Spec (N, False);
818 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
819 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
820 Set_Comes_From_Source_Default (SCS);
822 -- Restore Context_Items to the body
824 Set_Context_Items (N, Context_Items (Lib_Unit));
825 Set_Context_Items (Lib_Unit, Empty_List);
826 end;
827 end if;
828 end if;
830 -- Here for subprogram with separate declaration
832 else
833 Semantics (Lib_Unit);
834 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
835 Version_Update (N, Lib_Unit);
836 end if;
838 -- If this is a child unit, generate references to the parents
840 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
841 N_Defining_Program_Unit_Name
842 then
843 Generate_Parent_References
844 (Specification (Unit_Node),
845 Scope (Defining_Entity (Unit (Lib_Unit))));
846 end if;
847 end if;
849 -- If it is a child unit, the parent must be elaborated first and we
850 -- update version, since we are dependent on our parent.
852 if Is_Child_Spec (Unit_Node) then
854 -- The analysis of the parent is done with style checks off
856 declare
857 Save_Style_Check : constant Boolean := Style_Check;
859 begin
860 if not GNAT_Mode then
861 Style_Check := False;
862 end if;
864 Semantics (Parent_Spec (Unit_Node));
865 Version_Update (N, Parent_Spec (Unit_Node));
867 -- Restore style check settings
869 Style_Check := Save_Style_Check;
870 end;
871 end if;
873 -- With the analysis done, install the context. Note that we can't
874 -- install the context from the with clauses as we analyze them, because
875 -- each with clause must be analyzed in a clean visibility context, so
876 -- we have to wait and install them all at once.
878 Install_Context (N);
880 if Is_Child_Spec (Unit_Node) then
882 -- Set the entities of all parents in the program_unit_name
884 Generate_Parent_References
885 (Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
886 end if;
888 -- All components of the context: with-clauses, library unit, ancestors
889 -- if any, (and their context) are analyzed and installed.
891 -- Call special debug routine sm if this is the main unit
893 if Current_Sem_Unit = Main_Unit then
895 end if;
897 -- Now analyze the unit (package, subprogram spec, body) itself
899 Analyze (Unit_Node);
901 if Warn_On_Redundant_Constructs then
902 Check_Redundant_Withs (Context_Items (N));
904 if Nkind (Unit_Node) = N_Package_Body then
905 Check_Redundant_Withs
906 (Context_Items => Context_Items (N),
907 Spec_Context_Items => Context_Items (Lib_Unit));
908 end if;
909 end if;
911 -- The above call might have made Unit_Node an N_Subprogram_Body from
912 -- something else, so propagate any Acts_As_Spec flag.
914 if Nkind (Unit_Node) = N_Subprogram_Body
915 and then Acts_As_Spec (Unit_Node)
916 then
917 Set_Acts_As_Spec (N);
918 end if;
920 -- Register predefined units in Rtsfind
922 declare
923 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
924 begin
925 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
926 Set_RTU_Loaded (Unit_Node);
927 end if;
928 end;
930 -- Treat compilation unit pragmas that appear after the library unit
932 if Present (Pragmas_After (Aux_Decls_Node (N))) then
933 declare
934 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
935 begin
936 while Present (Prag_Node) loop
937 Analyze (Prag_Node);
938 Next (Prag_Node);
939 end loop;
940 end;
941 end if;
943 -- Analyze the contract of a [generic] subprogram that acts as a
944 -- compilation unit after all compilation pragmas have been analyzed.
946 if Nkind_In (Unit_Node, N_Generic_Subprogram_Declaration,
947 N_Subprogram_Declaration)
948 then
949 Analyze_Entry_Or_Subprogram_Contract (Defining_Entity (Unit_Node));
950 end if;
952 -- Generate distribution stubs if requested and no error
954 if N = Main_Cunit
955 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
956 or else
957 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
958 and then Fatal_Error (Main_Unit) /= Error_Detected
959 then
960 if Is_RCI_Pkg_Spec_Or_Body (N) then
962 -- Regular RCI package
964 Add_Stub_Constructs (N);
966 elsif (Nkind (Unit_Node) = N_Package_Declaration
967 and then Is_Shared_Passive (Defining_Entity
968 (Specification (Unit_Node))))
969 or else (Nkind (Unit_Node) = N_Package_Body
970 and then
971 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
972 then
973 -- Shared passive package
975 Add_Stub_Constructs (N);
977 elsif Nkind (Unit_Node) = N_Package_Instantiation
978 and then
979 Is_Remote_Call_Interface
980 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
981 then
982 -- Instantiation of a RCI generic package
984 Add_Stub_Constructs (N);
985 end if;
986 end if;
988 -- Remove unit from visibility, so that environment is clean for the
989 -- next compilation, which is either the main unit or some other unit
990 -- in the context.
992 if Nkind_In (Unit_Node, N_Package_Declaration,
993 N_Package_Renaming_Declaration,
994 N_Subprogram_Declaration)
995 or else Nkind (Unit_Node) in N_Generic_Declaration
996 or else (Nkind (Unit_Node) = N_Subprogram_Body
997 and then Acts_As_Spec (Unit_Node))
998 then
999 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
1001 -- If the unit is an instantiation whose body will be elaborated for
1002 -- inlining purposes, use the proper entity of the instance. The entity
1003 -- may be missing if the instantiation was illegal.
1005 elsif Nkind (Unit_Node) = N_Package_Instantiation
1006 and then not Error_Posted (Unit_Node)
1007 and then Present (Instance_Spec (Unit_Node))
1008 then
1009 Remove_Unit_From_Visibility
1010 (Defining_Entity (Instance_Spec (Unit_Node)));
1012 elsif Nkind (Unit_Node) = N_Package_Body
1013 or else (Nkind (Unit_Node) = N_Subprogram_Body
1014 and then not Acts_As_Spec (Unit_Node))
1015 then
1016 -- Bodies that are not the main unit are compiled if they are generic
1017 -- or contain generic or inlined units. Their analysis brings in the
1018 -- context of the corresponding spec (unit declaration) which must be
1019 -- removed as well, to return the compilation environment to its
1020 -- proper state.
1022 Remove_Context (Lib_Unit);
1023 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1024 end if;
1026 -- Last step is to deinstall the context we just installed as well as
1027 -- the unit just compiled.
1029 Remove_Context (N);
1031 -- When generating code for a non-generic main unit, check that withed
1032 -- generic units have a body if they need it, even if the units have not
1033 -- been instantiated. Force the load of the bodies to produce the proper
1034 -- error if the body is absent. The same applies to GNATprove mode, with
1035 -- the added benefit of capturing global references within the generic.
1036 -- This in turn allows for proper inlining of subprogram bodies without
1037 -- a previous declaration.
1039 if Get_Cunit_Unit_Number (N) = Main_Unit
1040 and then ((Operating_Mode = Generate_Code and then Expander_Active)
1041 or else
1042 (Operating_Mode = Check_Semantics and then GNATprove_Mode))
1043 then
1044 -- Check whether the source for the body of the unit must be included
1045 -- in a standalone library.
1047 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1049 -- Indicate that the main unit is now analyzed, to catch possible
1050 -- circularities between it and generic bodies. Remove main unit from
1051 -- visibility. This might seem superfluous, but the main unit must
1052 -- not be visible in the generic body expansions that follow.
1054 Set_Analyzed (N, True);
1055 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1057 declare
1058 Item : Node_Id;
1059 Nam : Entity_Id;
1060 Un : Unit_Number_Type;
1062 Save_Style_Check : constant Boolean := Style_Check;
1064 begin
1065 Item := First (Context_Items (N));
1066 while Present (Item) loop
1068 -- Check for explicit with clause
1070 if Nkind (Item) = N_With_Clause
1071 and then not Implicit_With (Item)
1073 -- Ada 2005 (AI-50217): Ignore limited-withed units
1075 and then not Limited_Present (Item)
1076 then
1077 Nam := Entity (Name (Item));
1079 -- Compile the generic subprogram, unless it is intrinsic or
1080 -- imported so no body is required, or generic package body
1081 -- if the package spec requires a body.
1083 if (Is_Generic_Subprogram (Nam)
1084 and then not Is_Intrinsic_Subprogram (Nam)
1085 and then not Is_Imported (Nam))
1086 or else (Ekind (Nam) = E_Generic_Package
1087 and then Unit_Requires_Body (Nam))
1088 then
1089 Style_Check := False;
1091 if Present (Renamed_Object (Nam)) then
1092 Un :=
1093 Load_Unit
1094 (Load_Name =>
1095 Get_Body_Name
1096 (Get_Unit_Name
1097 (Unit_Declaration_Node
1098 (Renamed_Object (Nam)))),
1099 Required => False,
1100 Subunit => False,
1101 Error_Node => N,
1102 Renamings => True);
1103 else
1104 Un :=
1105 Load_Unit
1106 (Load_Name =>
1107 Get_Body_Name (Get_Unit_Name (Item)),
1108 Required => False,
1109 Subunit => False,
1110 Error_Node => N,
1111 Renamings => True);
1112 end if;
1114 if Un = No_Unit then
1115 Error_Msg_NE
1116 ("body of generic unit& not found", Item, Nam);
1117 exit;
1119 elsif not Analyzed (Cunit (Un))
1120 and then Un /= Main_Unit
1121 and then Fatal_Error (Un) /= Error_Detected
1122 then
1123 Style_Check := False;
1124 Semantics (Cunit (Un));
1125 end if;
1126 end if;
1127 end if;
1129 Next (Item);
1130 end loop;
1132 -- Restore style checks settings
1134 Style_Check := Save_Style_Check;
1135 end;
1137 -- In GNATprove mode, force the loading of a Interrupt_Priority when
1138 -- processing compilation units with potentially "main" subprograms.
1139 -- This is required for the ceiling priority protocol checks, which
1140 -- are trigerred by these subprograms.
1142 if GNATprove_Mode
1143 and then Nkind_In (Unit_Node, N_Subprogram_Body,
1144 N_Procedure_Instantiation,
1145 N_Function_Instantiation)
1146 then
1147 declare
1148 Spec : Node_Id;
1149 Unused : Entity_Id;
1151 begin
1152 case Nkind (Unit_Node) is
1153 when N_Subprogram_Body =>
1154 Spec := Specification (Unit_Node);
1156 when N_Subprogram_Instantiation =>
1157 Spec :=
1158 Subprogram_Specification (Entity (Name (Unit_Node)));
1160 when others =>
1161 raise Program_Error;
1162 end case;
1164 pragma Assert (Nkind (Spec) in N_Subprogram_Specification);
1166 -- Only subprogram with no parameters can act as "main", and if
1167 -- it is a function, it needs to return an integer.
1169 if No (Parameter_Specifications (Spec))
1170 and then (Nkind (Spec) = N_Procedure_Specification
1171 or else
1172 Is_Integer_Type (Etype (Result_Definition (Spec))))
1173 then
1174 Unused := RTE (RE_Interrupt_Priority);
1175 end if;
1176 end;
1177 end if;
1178 end if;
1180 -- Deal with creating elaboration counter if needed. We create an
1181 -- elaboration counter only for units that come from source since
1182 -- units manufactured by the compiler never need elab checks.
1184 if Comes_From_Source (N)
1185 and then Nkind_In (Unit_Node, N_Package_Declaration,
1186 N_Generic_Package_Declaration,
1187 N_Subprogram_Declaration,
1188 N_Generic_Subprogram_Declaration)
1189 then
1190 declare
1191 Loc : constant Source_Ptr := Sloc (N);
1192 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1194 begin
1195 Spec_Id := Defining_Entity (Unit_Node);
1196 Generate_Definition (Spec_Id);
1198 -- See if an elaboration entity is required for possible access
1199 -- before elaboration checking. Note that we must allow for this
1200 -- even if -gnatE is not set, since a client may be compiled in
1201 -- -gnatE mode and reference the entity.
1203 -- These entities are also used by the binder to prevent multiple
1204 -- attempts to execute the elaboration code for the library case
1205 -- where the elaboration routine might otherwise be called more
1206 -- than once.
1208 -- Case of units which do not require elaboration checks
1211 -- Pure units do not need checks
1213 Is_Pure (Spec_Id)
1215 -- Preelaborated units do not need checks
1217 or else Is_Preelaborated (Spec_Id)
1219 -- No checks needed if pragma Elaborate_Body present
1221 or else Has_Pragma_Elaborate_Body (Spec_Id)
1223 -- No checks needed if unit does not require a body
1225 or else not Unit_Requires_Body (Spec_Id)
1227 -- No checks needed for predefined files
1229 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1231 -- No checks required if no separate spec
1233 or else Acts_As_Spec (N)
1234 then
1235 -- This is a case where we only need the entity for
1236 -- checking to prevent multiple elaboration checks.
1238 Set_Elaboration_Entity_Required (Spec_Id, False);
1240 -- Case of elaboration entity is required for access before
1241 -- elaboration checking (so certainly we must build it).
1243 else
1244 Set_Elaboration_Entity_Required (Spec_Id, True);
1245 end if;
1247 Build_Elaboration_Entity (N, Spec_Id);
1248 end;
1249 end if;
1251 -- Freeze the compilation unit entity. This for sure is needed because
1252 -- of some warnings that can be output (see Freeze_Subprogram), but may
1253 -- in general be required. If freezing actions result, place them in the
1254 -- compilation unit actions list, and analyze them.
1256 declare
1257 L : constant List_Id :=
1258 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1259 begin
1260 while Is_Non_Empty_List (L) loop
1261 Insert_Library_Level_Action (Remove_Head (L));
1262 end loop;
1263 end;
1265 Set_Analyzed (N);
1267 -- Call Check_Package_Body so that a body containing subprograms with
1268 -- Inline_Always can be made available for front end inlining.
1270 if Nkind (Unit_Node) = N_Package_Declaration
1271 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1273 -- We don't need to do this if the Expander is not active, since there
1274 -- is no code to inline.
1276 and then Expander_Active
1277 then
1278 declare
1279 Save_Style_Check : constant Boolean := Style_Check;
1280 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1281 Options : Style_Check_Options;
1283 begin
1284 Save_Style_Check_Options (Options);
1285 Reset_Style_Check_Options;
1286 Opt.Warning_Mode := Suppress;
1288 Check_Package_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1290 Reset_Style_Check_Options;
1291 Set_Style_Check_Options (Options);
1292 Style_Check := Save_Style_Check;
1293 Warning_Mode := Save_Warning;
1294 end;
1295 end if;
1297 -- If we are generating obsolescent warnings, then here is where we
1298 -- generate them for the with'ed items. The reason for this special
1299 -- processing is that the normal mechanism of generating the warnings
1300 -- for referenced entities does not work for context clause references.
1301 -- That's because when we first analyze the context, it is too early to
1302 -- know if the with'ing unit is itself obsolescent (which suppresses
1303 -- the warnings).
1305 if not GNAT_Mode
1306 and then Warn_On_Obsolescent_Feature
1307 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1308 then
1309 -- Push current compilation unit as scope, so that the test for
1310 -- being within an obsolescent unit will work correctly. The check
1311 -- is not performed within an instantiation, because the warning
1312 -- will have been emitted in the corresponding generic unit.
1314 Push_Scope (Defining_Entity (Unit_Node));
1316 -- Loop through context items to deal with with clauses
1318 declare
1319 Item : Node_Id;
1320 Nam : Node_Id;
1321 Ent : Entity_Id;
1323 begin
1324 Item := First (Context_Items (N));
1325 while Present (Item) loop
1326 if Nkind (Item) = N_With_Clause
1328 -- Suppress this check in limited-withed units. Further work
1329 -- needed here if we decide to incorporate this check on
1330 -- limited-withed units.
1332 and then not Limited_Present (Item)
1333 then
1334 Nam := Name (Item);
1335 Ent := Entity (Nam);
1337 if Is_Obsolescent (Ent) then
1338 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1339 end if;
1340 end if;
1342 Next (Item);
1343 end loop;
1344 end;
1346 -- Remove temporary install of current unit as scope
1348 Pop_Scope;
1349 end if;
1351 -- If No_Elaboration_Code_All was encountered, this is where we do the
1352 -- transitive test of with'ed units to make sure they have the aspect.
1353 -- This is delayed till the end of analyzing the compilation unit to
1354 -- ensure that the pragma/aspect, if present, has been analyzed.
1356 Check_No_Elab_Code_All (N);
1357 end Analyze_Compilation_Unit;
1359 ---------------------
1360 -- Analyze_Context --
1361 ---------------------
1363 procedure Analyze_Context (N : Node_Id) is
1364 Ukind : constant Node_Kind := Nkind (Unit (N));
1365 Item : Node_Id;
1367 begin
1368 -- First process all configuration pragmas at the start of the context
1369 -- items. Strictly these are not part of the context clause, but that
1370 -- is where the parser puts them. In any case for sure we must analyze
1371 -- these before analyzing the actual context items, since they can have
1372 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1373 -- be with'ed as a result of changing categorizations in Ada 2005).
1375 Item := First (Context_Items (N));
1376 while Present (Item)
1377 and then Nkind (Item) = N_Pragma
1378 and then Pragma_Name (Item) in Configuration_Pragma_Names
1379 loop
1380 Analyze (Item);
1381 Next (Item);
1382 end loop;
1384 -- This is the point at which we capture the configuration settings
1385 -- for the unit. At the moment only the Optimize_Alignment setting
1386 -- needs to be captured. Probably more later ???
1388 if Optimize_Alignment_Local then
1389 Set_OA_Setting (Current_Sem_Unit, 'L');
1390 else
1391 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1392 end if;
1394 -- Loop through actual context items. This is done in two passes:
1396 -- a) The first pass analyzes non-limited with-clauses and also any
1397 -- configuration pragmas (we need to get the latter analyzed right
1398 -- away, since they can affect processing of subsequent items).
1400 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1402 while Present (Item) loop
1404 -- For with clause, analyze the with clause, and then update the
1405 -- version, since we are dependent on a unit that we with.
1407 if Nkind (Item) = N_With_Clause
1408 and then not Limited_Present (Item)
1409 then
1410 -- Skip analyzing with clause if no unit, nothing to do (this
1411 -- happens for a with that references a non-existent unit).
1413 if Present (Library_Unit (Item)) then
1415 -- Skip analyzing with clause if this is a with_clause for
1416 -- the main unit, which happens if a subunit has a useless
1417 -- with_clause on its parent.
1419 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1420 Analyze (Item);
1422 -- Here for the case of a useless with for the main unit
1424 else
1425 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1426 end if;
1427 end if;
1429 -- Do version update (skipped for implicit with)
1431 if not Implicit_With (Item) then
1432 Version_Update (N, Library_Unit (Item));
1433 end if;
1435 -- Skip pragmas. Configuration pragmas at the start were handled in
1436 -- the loop above, and remaining pragmas are not processed until we
1437 -- actually install the context (see Install_Context). We delay the
1438 -- analysis of these pragmas to make sure that we have installed all
1439 -- the implicit with's on parent units.
1441 -- Skip use clauses at this stage, since we don't want to do any
1442 -- installing of potentially use-visible entities until we
1443 -- actually install the complete context (in Install_Context).
1444 -- Otherwise things can get installed in the wrong context.
1446 else
1447 null;
1448 end if;
1450 Next (Item);
1451 end loop;
1453 -- Second pass: examine all limited_with clauses. All other context
1454 -- items are ignored in this pass.
1456 Item := First (Context_Items (N));
1457 while Present (Item) loop
1458 if Nkind (Item) = N_With_Clause
1459 and then Limited_Present (Item)
1460 then
1461 -- No need to check errors on implicitly generated limited-with
1462 -- clauses.
1464 if not Implicit_With (Item) then
1466 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1467 -- properly rejected, including renaming declarations.
1469 if not Nkind_In (Ukind, N_Package_Declaration,
1470 N_Subprogram_Declaration)
1471 and then Ukind not in N_Generic_Declaration
1472 and then Ukind not in N_Generic_Instantiation
1473 then
1474 Error_Msg_N ("limited with_clause not allowed here", Item);
1476 -- Check wrong use of a limited with clause applied to the
1477 -- compilation unit containing the limited-with clause.
1479 -- limited with P.Q;
1480 -- package P.Q is ...
1482 elsif Unit (Library_Unit (Item)) = Unit (N) then
1483 Error_Msg_N ("wrong use of limited-with clause", Item);
1485 -- Check wrong use of limited-with clause applied to some
1486 -- immediate ancestor.
1488 elsif Is_Child_Spec (Unit (N)) then
1489 declare
1490 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1491 P : Node_Id;
1493 begin
1494 P := Parent_Spec (Unit (N));
1495 loop
1496 if Unit (P) = Lib_U then
1497 Error_Msg_N ("limited with_clause cannot "
1498 & "name ancestor", Item);
1499 exit;
1500 end if;
1502 exit when not Is_Child_Spec (Unit (P));
1503 P := Parent_Spec (Unit (P));
1504 end loop;
1505 end;
1506 end if;
1508 -- Check if the limited-withed unit is already visible through
1509 -- some context clause of the current compilation unit or some
1510 -- ancestor of the current compilation unit.
1512 declare
1513 Lim_Unit_Name : constant Node_Id := Name (Item);
1514 Comp_Unit : Node_Id;
1515 It : Node_Id;
1516 Unit_Name : Node_Id;
1518 begin
1519 Comp_Unit := N;
1520 loop
1521 It := First (Context_Items (Comp_Unit));
1522 while Present (It) loop
1523 if Item /= It
1524 and then Nkind (It) = N_With_Clause
1525 and then not Limited_Present (It)
1526 and then
1527 Nkind_In (Unit (Library_Unit (It)),
1528 N_Package_Declaration,
1529 N_Package_Renaming_Declaration)
1530 then
1531 if Nkind (Unit (Library_Unit (It))) =
1532 N_Package_Declaration
1533 then
1534 Unit_Name := Name (It);
1535 else
1536 Unit_Name := Name (Unit (Library_Unit (It)));
1537 end if;
1539 -- Check if the named package (or some ancestor)
1540 -- leaves visible the full-view of the unit given
1541 -- in the limited-with clause.
1543 loop
1544 if Designate_Same_Unit (Lim_Unit_Name,
1545 Unit_Name)
1546 then
1547 Error_Msg_Sloc := Sloc (It);
1548 Error_Msg_N
1549 ("simultaneous visibility of limited "
1550 & "and unlimited views not allowed",
1551 Item);
1552 Error_Msg_NE
1553 ("\unlimited view visible through "
1554 & "context clause #",
1555 Item, It);
1556 exit;
1558 elsif Nkind (Unit_Name) = N_Identifier then
1559 exit;
1560 end if;
1562 Unit_Name := Prefix (Unit_Name);
1563 end loop;
1564 end if;
1566 Next (It);
1567 end loop;
1569 exit when not Is_Child_Spec (Unit (Comp_Unit));
1571 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1572 end loop;
1573 end;
1574 end if;
1576 -- Skip analyzing with clause if no unit, see above
1578 if Present (Library_Unit (Item)) then
1579 Analyze (Item);
1580 end if;
1582 -- A limited_with does not impose an elaboration order, but
1583 -- there is a semantic dependency for recompilation purposes.
1585 if not Implicit_With (Item) then
1586 Version_Update (N, Library_Unit (Item));
1587 end if;
1589 -- Pragmas and use clauses and with clauses other than limited
1590 -- with's are ignored in this pass through the context items.
1592 else
1593 null;
1594 end if;
1596 Next (Item);
1597 end loop;
1598 end Analyze_Context;
1600 -------------------------------
1601 -- Analyze_Package_Body_Stub --
1602 -------------------------------
1604 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1605 Id : constant Entity_Id := Defining_Identifier (N);
1606 Nam : Entity_Id;
1607 Opts : Config_Switches_Type;
1609 begin
1610 -- The package declaration must be in the current declarative part
1612 Check_Stub_Level (N);
1613 Nam := Current_Entity_In_Scope (Id);
1615 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1616 Error_Msg_N ("missing specification for package stub", N);
1618 elsif Has_Completion (Nam)
1619 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1620 then
1621 Error_Msg_N ("duplicate or redundant stub for package", N);
1623 else
1624 -- Retain and restore the configuration options of the enclosing
1625 -- context as the proper body may introduce a set of its own.
1627 Save_Opt_Config_Switches (Opts);
1629 -- Indicate that the body of the package exists. If we are doing
1630 -- only semantic analysis, the stub stands for the body. If we are
1631 -- generating code, the existence of the body will be confirmed
1632 -- when we load the proper body.
1634 Set_Has_Completion (Nam);
1635 Set_Scope (Defining_Entity (N), Current_Scope);
1636 Set_Ekind (Defining_Entity (N), E_Package_Body);
1637 Set_Corresponding_Spec_Of_Stub (N, Nam);
1638 Generate_Reference (Nam, Id, 'b');
1639 Analyze_Proper_Body (N, Nam);
1641 Restore_Opt_Config_Switches (Opts);
1642 end if;
1643 end Analyze_Package_Body_Stub;
1645 -------------------------
1646 -- Analyze_Proper_Body --
1647 -------------------------
1649 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1650 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1652 procedure Optional_Subunit;
1653 -- This procedure is called when the main unit is a stub, or when we
1654 -- are not generating code. In such a case, we analyze the subunit if
1655 -- present, which is user-friendly and in fact required for ASIS, but we
1656 -- don't complain if the subunit is missing. In GNATprove_Mode, we issue
1657 -- an error to avoid formal verification of a partial unit.
1659 ----------------------
1660 -- Optional_Subunit --
1661 ----------------------
1663 procedure Optional_Subunit is
1664 Comp_Unit : Node_Id;
1665 Unum : Unit_Number_Type;
1667 begin
1668 -- Try to load subunit, but ignore any errors that occur during the
1669 -- loading of the subunit, by using the special feature in Errout to
1670 -- ignore all errors. Note that Fatal_Error will still be set, so we
1671 -- will be able to check for this case below.
1673 if not (ASIS_Mode or GNATprove_Mode) then
1674 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1675 end if;
1677 Unum :=
1678 Load_Unit
1679 (Load_Name => Subunit_Name,
1680 Required => GNATprove_Mode,
1681 Subunit => True,
1682 Error_Node => N);
1684 if not (ASIS_Mode or GNATprove_Mode) then
1685 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1686 end if;
1688 -- All done if we successfully loaded the subunit
1690 if Unum /= No_Unit
1691 and then (Fatal_Error (Unum) /= Error_Detected
1692 or else Try_Semantics)
1693 then
1694 Comp_Unit := Cunit (Unum);
1696 -- If the file was empty or seriously mangled, the unit itself may
1697 -- be missing.
1699 if No (Unit (Comp_Unit)) then
1700 Error_Msg_N
1701 ("subunit does not contain expected proper body", N);
1703 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1704 Error_Msg_N
1705 ("expected SEPARATE subunit, found child unit",
1706 Cunit_Entity (Unum));
1707 else
1708 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1709 Analyze_Subunit (Comp_Unit);
1710 Set_Library_Unit (N, Comp_Unit);
1711 Set_Corresponding_Body (N, Defining_Entity (Unit (Comp_Unit)));
1712 end if;
1714 elsif Unum = No_Unit
1715 and then Present (Nam)
1716 then
1717 if Is_Protected_Type (Nam) then
1718 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1719 else
1720 Set_Corresponding_Body (
1721 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1722 end if;
1723 end if;
1724 end Optional_Subunit;
1726 -- Local variables
1728 Comp_Unit : Node_Id;
1729 Unum : Unit_Number_Type;
1731 -- Start of processing for Analyze_Proper_Body
1733 begin
1734 -- If the subunit is already loaded, it means that the main unit is a
1735 -- subunit, and that the current unit is one of its parents which was
1736 -- being analyzed to provide the needed context for the analysis of the
1737 -- subunit. In this case we analyze the subunit and continue with the
1738 -- parent, without looking at subsequent subunits.
1740 if Is_Loaded (Subunit_Name) then
1742 -- If the proper body is already linked to the stub node, the stub is
1743 -- in a generic unit and just needs analyzing.
1745 if Present (Library_Unit (N)) then
1746 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1748 -- If the subunit has severe errors, the spec of the enclosing
1749 -- body may not be available, in which case do not try analysis.
1751 if Serious_Errors_Detected > 0
1752 and then No (Library_Unit (Library_Unit (N)))
1753 then
1754 return;
1755 end if;
1757 -- Collect SCO information for loaded subunit if we are in the
1758 -- extended main unit.
1760 if Generate_SCO
1761 and then In_Extended_Main_Source_Unit
1762 (Cunit_Entity (Current_Sem_Unit))
1763 then
1764 SCO_Record_Raw (Get_Cunit_Unit_Number (Library_Unit (N)));
1765 end if;
1767 Analyze_Subunit (Library_Unit (N));
1769 -- Otherwise we must load the subunit and link to it
1771 else
1772 -- Load the subunit, this must work, since we originally loaded
1773 -- the subunit earlier on. So this will not really load it, just
1774 -- give access to it.
1776 Unum :=
1777 Load_Unit
1778 (Load_Name => Subunit_Name,
1779 Required => True,
1780 Subunit => False,
1781 Error_Node => N);
1783 -- And analyze the subunit in the parent context (note that we
1784 -- do not call Semantics, since that would remove the parent
1785 -- context). Because of this, we have to manually reset the
1786 -- compiler state to Analyzing since it got destroyed by Load.
1788 if Unum /= No_Unit then
1789 Compiler_State := Analyzing;
1791 -- Check that the proper body is a subunit and not a child
1792 -- unit. If the unit was previously loaded, the error will
1793 -- have been emitted when copying the generic node, so we
1794 -- just return to avoid cascaded errors.
1796 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1797 return;
1798 end if;
1800 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1801 Analyze_Subunit (Cunit (Unum));
1802 Set_Library_Unit (N, Cunit (Unum));
1803 end if;
1804 end if;
1806 -- If the main unit is a subunit, then we are just performing semantic
1807 -- analysis on that subunit, and any other subunits of any parent unit
1808 -- should be ignored, except that if we are building trees for ASIS
1809 -- usage we want to annotate the stub properly. If the main unit is
1810 -- itself a subunit, another subunit is irrelevant unless it is a
1811 -- subunit of the current one, that is to say appears in the current
1812 -- source tree.
1814 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1815 and then Subunit_Name /= Unit_Name (Main_Unit)
1816 then
1817 if ASIS_Mode then
1818 declare
1819 PB : constant Node_Id := Proper_Body (Unit (Cunit (Main_Unit)));
1820 begin
1821 if Nkind_In (PB, N_Package_Body, N_Subprogram_Body)
1822 and then List_Containing (N) = Declarations (PB)
1823 then
1824 Optional_Subunit;
1825 end if;
1826 end;
1827 end if;
1829 -- But before we return, set the flag for unloaded subunits. This
1830 -- will suppress junk warnings of variables in the same declarative
1831 -- part (or a higher level one) that are in danger of looking unused
1832 -- when in fact there might be a declaration in the subunit that we
1833 -- do not intend to load.
1835 Unloaded_Subunits := True;
1836 return;
1838 -- If the subunit is not already loaded, and we are generating code,
1839 -- then this is the case where compilation started from the parent, and
1840 -- we are generating code for an entire subunit tree. In that case we
1841 -- definitely need to load the subunit.
1843 -- In order to continue the analysis with the rest of the parent,
1844 -- and other subunits, we load the unit without requiring its
1845 -- presence, and emit a warning if not found, rather than terminating
1846 -- the compilation abruptly, as for other missing file problems.
1848 elsif Original_Operating_Mode = Generate_Code then
1850 -- If the proper body is already linked to the stub node, the stub is
1851 -- in a generic unit and just needs analyzing.
1853 -- We update the version. Although we are not strictly technically
1854 -- semantically dependent on the subunit, given our approach of macro
1855 -- substitution of subunits, it makes sense to include it in the
1856 -- version identification.
1858 if Present (Library_Unit (N)) then
1859 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1860 Analyze_Subunit (Library_Unit (N));
1861 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1863 -- Otherwise we must load the subunit and link to it
1865 else
1866 -- Make sure that, if the subunit is preprocessed and -gnateG is
1867 -- specified, the preprocessed file will be written.
1869 Lib.Analysing_Subunit_Of_Main := True;
1870 Unum :=
1871 Load_Unit
1872 (Load_Name => Subunit_Name,
1873 Required => False,
1874 Subunit => True,
1875 Error_Node => N);
1876 Lib.Analysing_Subunit_Of_Main := False;
1878 -- Give message if we did not get the unit Emit warning even if
1879 -- missing subunit is not within main unit, to simplify debugging.
1881 pragma Assert (Original_Operating_Mode = Generate_Code);
1882 if Unum = No_Unit then
1883 Error_Msg_Unit_1 := Subunit_Name;
1884 Error_Msg_File_1 :=
1885 Get_File_Name (Subunit_Name, Subunit => True);
1886 Error_Msg_N
1887 ("subunit$$ in file{ not found??!!", N);
1888 Subunits_Missing := True;
1889 end if;
1891 -- Load_Unit may reset Compiler_State, since it may have been
1892 -- necessary to parse an additional units, so we make sure that
1893 -- we reset it to the Analyzing state.
1895 Compiler_State := Analyzing;
1897 if Unum /= No_Unit then
1898 if Debug_Flag_L then
1899 Write_Str ("*** Loaded subunit from stub. Analyze");
1900 Write_Eol;
1901 end if;
1903 Comp_Unit := Cunit (Unum);
1905 -- Check for child unit instead of subunit
1907 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1908 Error_Msg_N
1909 ("expected SEPARATE subunit, found child unit",
1910 Cunit_Entity (Unum));
1912 -- OK, we have a subunit
1914 else
1915 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1916 Set_Library_Unit (N, Comp_Unit);
1918 -- We update the version. Although we are not technically
1919 -- semantically dependent on the subunit, given our approach
1920 -- of macro substitution of subunits, it makes sense to
1921 -- include it in the version identification.
1923 Version_Update (Cunit (Main_Unit), Comp_Unit);
1925 -- Collect SCO information for loaded subunit if we are in
1926 -- the extended main unit.
1928 if Generate_SCO
1929 and then In_Extended_Main_Source_Unit
1930 (Cunit_Entity (Current_Sem_Unit))
1931 then
1932 SCO_Record_Raw (Unum);
1933 end if;
1935 -- Analyze the unit if semantics active
1937 if Fatal_Error (Unum) /= Error_Detected
1938 or else Try_Semantics
1939 then
1940 Analyze_Subunit (Comp_Unit);
1941 end if;
1942 end if;
1943 end if;
1944 end if;
1946 -- The remaining case is when the subunit is not already loaded and we
1947 -- are not generating code. In this case we are just performing semantic
1948 -- analysis on the parent, and we are not interested in the subunit. For
1949 -- subprograms, analyze the stub as a body. For other entities the stub
1950 -- has already been marked as completed.
1952 else
1953 Optional_Subunit;
1954 end if;
1955 end Analyze_Proper_Body;
1957 ----------------------------------
1958 -- Analyze_Protected_Body_Stub --
1959 ----------------------------------
1961 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1962 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1964 begin
1965 Check_Stub_Level (N);
1967 -- First occurrence of name may have been as an incomplete type
1969 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1970 Nam := Full_View (Nam);
1971 end if;
1973 if No (Nam) or else not Is_Protected_Type (Etype (Nam)) then
1974 Error_Msg_N ("missing specification for Protected body", N);
1976 else
1977 Set_Scope (Defining_Entity (N), Current_Scope);
1978 Set_Ekind (Defining_Entity (N), E_Protected_Body);
1979 Set_Has_Completion (Etype (Nam));
1980 Set_Corresponding_Spec_Of_Stub (N, Nam);
1981 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1982 Analyze_Proper_Body (N, Etype (Nam));
1983 end if;
1984 end Analyze_Protected_Body_Stub;
1986 ----------------------------------
1987 -- Analyze_Subprogram_Body_Stub --
1988 ----------------------------------
1990 -- A subprogram body stub can appear with or without a previous spec. If
1991 -- there is one, then the analysis of the body will find it and verify
1992 -- conformance. The formals appearing in the specification of the stub play
1993 -- no role, except for requiring an additional conformance check. If there
1994 -- is no previous subprogram declaration, the stub acts as a spec, and
1995 -- provides the defining entity for the subprogram.
1997 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1998 Decl : Node_Id;
1999 Opts : Config_Switches_Type;
2001 begin
2002 Check_Stub_Level (N);
2004 -- Verify that the identifier for the stub is unique within this
2005 -- declarative part.
2007 if Nkind_In (Parent (N), N_Block_Statement,
2008 N_Package_Body,
2009 N_Subprogram_Body)
2010 then
2011 Decl := First (Declarations (Parent (N)));
2012 while Present (Decl) and then Decl /= N loop
2013 if Nkind (Decl) = N_Subprogram_Body_Stub
2014 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
2015 Chars (Defining_Unit_Name (Specification (N))))
2016 then
2017 Error_Msg_N ("identifier for stub is not unique", N);
2018 end if;
2020 Next (Decl);
2021 end loop;
2022 end if;
2024 -- Retain and restore the configuration options of the enclosing context
2025 -- as the proper body may introduce a set of its own.
2027 Save_Opt_Config_Switches (Opts);
2029 -- Treat stub as a body, which checks conformance if there is a previous
2030 -- declaration, or else introduces entity and its signature.
2032 Analyze_Subprogram_Body (N);
2033 Analyze_Proper_Body (N, Empty);
2035 Restore_Opt_Config_Switches (Opts);
2036 end Analyze_Subprogram_Body_Stub;
2038 ---------------------
2039 -- Analyze_Subunit --
2040 ---------------------
2042 -- A subunit is compiled either by itself (for semantic checking) or as
2043 -- part of compiling the parent (for code generation). In either case, by
2044 -- the time we actually process the subunit, the parent has already been
2045 -- installed and analyzed. The node N is a compilation unit, whose context
2046 -- needs to be treated here, because we come directly here from the parent
2047 -- without calling Analyze_Compilation_Unit.
2049 -- The compilation context includes the explicit context of the subunit,
2050 -- and the context of the parent, together with the parent itself. In order
2051 -- to compile the current context, we remove the one inherited from the
2052 -- parent, in order to have a clean visibility table. We restore the parent
2053 -- context before analyzing the proper body itself. On exit, we remove only
2054 -- the explicit context of the subunit.
2056 procedure Analyze_Subunit (N : Node_Id) is
2057 Lib_Unit : constant Node_Id := Library_Unit (N);
2058 Par_Unit : constant Entity_Id := Current_Scope;
2060 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
2061 Num_Scopes : Nat := 0;
2062 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2063 Enclosing_Child : Entity_Id := Empty;
2064 Svg : constant Suppress_Record := Scope_Suppress;
2066 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
2067 Cunit_Boolean_Restrictions_Save;
2068 -- Save non-partition wide restrictions before processing the subunit.
2069 -- All subunits are analyzed with config restrictions reset and we need
2070 -- to restore these saved values at the end.
2072 procedure Analyze_Subunit_Context;
2073 -- Capture names in use clauses of the subunit. This must be done before
2074 -- re-installing parent declarations, because items in the context must
2075 -- not be hidden by declarations local to the parent.
2077 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
2078 -- Recursive procedure to restore scope of all ancestors of subunit,
2079 -- from outermost in. If parent is not a subunit, the call to install
2080 -- context installs context of spec and (if parent is a child unit) the
2081 -- context of its parents as well. It is confusing that parents should
2082 -- be treated differently in both cases, but the semantics are just not
2083 -- identical.
2085 procedure Re_Install_Use_Clauses;
2086 -- As part of the removal of the parent scope, the use clauses are
2087 -- removed, to be reinstalled when the context of the subunit has been
2088 -- analyzed. Use clauses may also have been affected by the analysis of
2089 -- the context of the subunit, so they have to be applied again, to
2090 -- insure that the compilation environment of the rest of the parent
2091 -- unit is identical.
2093 procedure Remove_Scope;
2094 -- Remove current scope from scope stack, and preserve the list of use
2095 -- clauses in it, to be reinstalled after context is analyzed.
2097 -----------------------------
2098 -- Analyze_Subunit_Context --
2099 -----------------------------
2101 procedure Analyze_Subunit_Context is
2102 Item : Node_Id;
2103 Nam : Node_Id;
2104 Unit_Name : Entity_Id;
2106 begin
2107 Analyze_Context (N);
2108 Check_No_Elab_Code_All (N);
2110 -- Make withed units immediately visible. If child unit, make the
2111 -- ultimate parent immediately visible.
2113 Item := First (Context_Items (N));
2114 while Present (Item) loop
2115 if Nkind (Item) = N_With_Clause then
2117 -- Protect frontend against previous errors in context clauses
2119 if Nkind (Name (Item)) /= N_Selected_Component then
2120 if Error_Posted (Item) then
2121 null;
2123 else
2124 -- If a subunits has serious syntax errors, the context
2125 -- may not have been loaded. Add a harmless unit name to
2126 -- attempt processing.
2128 if Serious_Errors_Detected > 0
2129 and then No (Entity (Name (Item)))
2130 then
2131 Set_Entity (Name (Item), Standard_Standard);
2132 end if;
2134 Unit_Name := Entity (Name (Item));
2135 loop
2136 Set_Is_Visible_Lib_Unit (Unit_Name);
2137 exit when Scope (Unit_Name) = Standard_Standard;
2138 Unit_Name := Scope (Unit_Name);
2140 if No (Unit_Name) then
2141 Check_Error_Detected;
2142 return;
2143 end if;
2144 end loop;
2146 if not Is_Immediately_Visible (Unit_Name) then
2147 Set_Is_Immediately_Visible (Unit_Name);
2148 Set_Context_Installed (Item);
2149 end if;
2150 end if;
2151 end if;
2153 elsif Nkind (Item) = N_Use_Package_Clause then
2154 Nam := First (Names (Item));
2155 while Present (Nam) loop
2156 Analyze (Nam);
2157 Next (Nam);
2158 end loop;
2160 elsif Nkind (Item) = N_Use_Type_Clause then
2161 Nam := First (Subtype_Marks (Item));
2162 while Present (Nam) loop
2163 Analyze (Nam);
2164 Next (Nam);
2165 end loop;
2166 end if;
2168 Next (Item);
2169 end loop;
2171 -- Reset visibility of withed units. They will be made visible again
2172 -- when we install the subunit context.
2174 Item := First (Context_Items (N));
2175 while Present (Item) loop
2176 if Nkind (Item) = N_With_Clause
2178 -- Protect frontend against previous errors in context clauses
2180 and then Nkind (Name (Item)) /= N_Selected_Component
2181 and then not Error_Posted (Item)
2182 then
2183 Unit_Name := Entity (Name (Item));
2184 loop
2185 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2186 exit when Scope (Unit_Name) = Standard_Standard;
2187 Unit_Name := Scope (Unit_Name);
2188 end loop;
2190 if Context_Installed (Item) then
2191 Set_Is_Immediately_Visible (Unit_Name, False);
2192 Set_Context_Installed (Item, False);
2193 end if;
2194 end if;
2196 Next (Item);
2197 end loop;
2198 end Analyze_Subunit_Context;
2200 ------------------------
2201 -- Re_Install_Parents --
2202 ------------------------
2204 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2205 E : Entity_Id;
2207 begin
2208 if Nkind (Unit (L)) = N_Subunit then
2209 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2210 end if;
2212 Install_Context (L);
2214 -- If the subunit occurs within a child unit, we must restore the
2215 -- immediate visibility of any siblings that may occur in context.
2217 if Present (Enclosing_Child) then
2218 Install_Siblings (Enclosing_Child, L);
2219 end if;
2221 Push_Scope (Scop);
2223 if Scop /= Par_Unit then
2224 Set_Is_Immediately_Visible (Scop);
2225 end if;
2227 -- Make entities in scope visible again. For child units, restore
2228 -- visibility only if they are actually in context.
2230 E := First_Entity (Current_Scope);
2231 while Present (E) loop
2232 if not Is_Child_Unit (E) or else Is_Visible_Lib_Unit (E) then
2233 Set_Is_Immediately_Visible (E);
2234 end if;
2236 Next_Entity (E);
2237 end loop;
2239 -- A subunit appears within a body, and for a nested subunits all the
2240 -- parents are bodies. Restore full visibility of their private
2241 -- entities.
2243 if Is_Package_Or_Generic_Package (Scop) then
2244 Set_In_Package_Body (Scop);
2245 Install_Private_Declarations (Scop);
2246 end if;
2247 end Re_Install_Parents;
2249 ----------------------------
2250 -- Re_Install_Use_Clauses --
2251 ----------------------------
2253 procedure Re_Install_Use_Clauses is
2254 U : Node_Id;
2255 begin
2256 for J in reverse 1 .. Num_Scopes loop
2257 U := Use_Clauses (J);
2258 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2259 Install_Use_Clauses (U, Force_Installation => True);
2260 end loop;
2261 end Re_Install_Use_Clauses;
2263 ------------------
2264 -- Remove_Scope --
2265 ------------------
2267 procedure Remove_Scope is
2268 E : Entity_Id;
2270 begin
2271 Num_Scopes := Num_Scopes + 1;
2272 Use_Clauses (Num_Scopes) :=
2273 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2275 E := First_Entity (Current_Scope);
2276 while Present (E) loop
2277 Set_Is_Immediately_Visible (E, False);
2278 Next_Entity (E);
2279 end loop;
2281 if Is_Child_Unit (Current_Scope) then
2282 Enclosing_Child := Current_Scope;
2283 end if;
2285 Pop_Scope;
2286 end Remove_Scope;
2288 -- Start of processing for Analyze_Subunit
2290 begin
2291 -- For subunit in main extended unit, we reset the configuration values
2292 -- for the non-partition-wide restrictions. For other units reset them.
2294 if In_Extended_Main_Source_Unit (N) then
2295 Restore_Config_Cunit_Boolean_Restrictions;
2296 else
2297 Reset_Cunit_Boolean_Restrictions;
2298 end if;
2300 if Style_Check then
2301 declare
2302 Nam : Node_Id := Name (Unit (N));
2304 begin
2305 if Nkind (Nam) = N_Selected_Component then
2306 Nam := Selector_Name (Nam);
2307 end if;
2309 Check_Identifier (Nam, Par_Unit);
2310 end;
2311 end if;
2313 if not Is_Empty_List (Context_Items (N)) then
2315 -- Save current use clauses
2317 Remove_Scope;
2318 Remove_Context (Lib_Unit);
2320 -- Now remove parents and their context, including enclosing subunits
2321 -- and the outer parent body which is not a subunit.
2323 if Present (Lib_Spec) then
2324 Remove_Context (Lib_Spec);
2326 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2327 Lib_Spec := Library_Unit (Lib_Spec);
2328 Remove_Scope;
2329 Remove_Context (Lib_Spec);
2330 end loop;
2332 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2333 Remove_Scope;
2334 end if;
2336 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2337 Remove_Context (Library_Unit (Lib_Spec));
2338 end if;
2339 end if;
2341 Set_Is_Immediately_Visible (Par_Unit, False);
2343 Analyze_Subunit_Context;
2345 Re_Install_Parents (Lib_Unit, Par_Unit);
2346 Set_Is_Immediately_Visible (Par_Unit);
2348 -- If the context includes a child unit of the parent of the subunit,
2349 -- the parent will have been removed from visibility, after compiling
2350 -- that cousin in the context. The visibility of the parent must be
2351 -- restored now. This also applies if the context includes another
2352 -- subunit of the same parent which in turn includes a child unit in
2353 -- its context.
2355 if Is_Package_Or_Generic_Package (Par_Unit) then
2356 if not Is_Immediately_Visible (Par_Unit)
2357 or else (Present (First_Entity (Par_Unit))
2358 and then not
2359 Is_Immediately_Visible (First_Entity (Par_Unit)))
2360 then
2361 Set_Is_Immediately_Visible (Par_Unit);
2362 Install_Visible_Declarations (Par_Unit);
2363 Install_Private_Declarations (Par_Unit);
2364 end if;
2365 end if;
2367 Re_Install_Use_Clauses;
2368 Install_Context (N);
2370 -- Restore state of suppress flags for current body
2372 Scope_Suppress := Svg;
2374 -- If the subunit is within a child unit, then siblings of any parent
2375 -- unit that appear in the context clause of the subunit must also be
2376 -- made immediately visible.
2378 if Present (Enclosing_Child) then
2379 Install_Siblings (Enclosing_Child, N);
2380 end if;
2381 end if;
2383 Generate_Parent_References (Unit (N), Par_Unit);
2384 Analyze (Proper_Body (Unit (N)));
2385 Remove_Context (N);
2387 -- The subunit may contain a with_clause on a sibling of some ancestor.
2388 -- Removing the context will remove from visibility those ancestor child
2389 -- units, which must be restored to the visibility they have in the
2390 -- enclosing body.
2392 if Present (Enclosing_Child) then
2393 declare
2394 C : Entity_Id;
2395 begin
2396 C := Current_Scope;
2397 while Present (C) and then C /= Standard_Standard loop
2398 Set_Is_Immediately_Visible (C);
2399 Set_Is_Visible_Lib_Unit (C);
2400 C := Scope (C);
2401 end loop;
2402 end;
2403 end if;
2405 -- Deal with restore of restrictions
2407 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2408 end Analyze_Subunit;
2410 ----------------------------
2411 -- Analyze_Task_Body_Stub --
2412 ----------------------------
2414 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2415 Loc : constant Source_Ptr := Sloc (N);
2416 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2418 begin
2419 Check_Stub_Level (N);
2421 -- First occurrence of name may have been as an incomplete type
2423 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2424 Nam := Full_View (Nam);
2425 end if;
2427 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2428 Error_Msg_N ("missing specification for task body", N);
2430 else
2431 Set_Scope (Defining_Entity (N), Current_Scope);
2432 Set_Ekind (Defining_Entity (N), E_Task_Body);
2433 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2434 Set_Corresponding_Spec_Of_Stub (N, Nam);
2436 -- Check for duplicate stub, if so give message and terminate
2438 if Has_Completion (Etype (Nam)) then
2439 Error_Msg_N ("duplicate stub for task", N);
2440 return;
2441 else
2442 Set_Has_Completion (Etype (Nam));
2443 end if;
2445 Analyze_Proper_Body (N, Etype (Nam));
2447 -- Set elaboration flag to indicate that entity is callable. This
2448 -- cannot be done in the expansion of the body itself, because the
2449 -- proper body is not in a declarative part. This is only done if
2450 -- expansion is active, because the context may be generic and the
2451 -- flag not defined yet.
2453 if Expander_Active then
2454 Insert_After (N,
2455 Make_Assignment_Statement (Loc,
2456 Name =>
2457 Make_Identifier (Loc,
2458 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2459 Expression => New_Occurrence_Of (Standard_True, Loc)));
2460 end if;
2461 end if;
2462 end Analyze_Task_Body_Stub;
2464 -------------------------
2465 -- Analyze_With_Clause --
2466 -------------------------
2468 -- Analyze the declaration of a unit in a with clause. At end, label the
2469 -- with clause with the defining entity for the unit.
2471 procedure Analyze_With_Clause (N : Node_Id) is
2473 -- Retrieve the original kind of the unit node, before analysis. If it
2474 -- is a subprogram instantiation, its analysis below will rewrite the
2475 -- node as the declaration of the wrapper package. If the same
2476 -- instantiation appears indirectly elsewhere in the context, it will
2477 -- have been analyzed already.
2479 Unit_Kind : constant Node_Kind :=
2480 Nkind (Original_Node (Unit (Library_Unit (N))));
2481 Nam : constant Node_Id := Name (N);
2482 E_Name : Entity_Id;
2483 Par_Name : Entity_Id;
2484 Pref : Node_Id;
2485 U : Node_Id;
2487 Intunit : Boolean;
2488 -- Set True if the unit currently being compiled is an internal unit
2490 Restriction_Violation : Boolean := False;
2491 -- Set True if a with violates a restriction, no point in giving any
2492 -- warnings if we have this definite error.
2494 Save_Style_Check : constant Boolean := Opt.Style_Check;
2496 begin
2497 U := Unit (Library_Unit (N));
2499 -- If this is an internal unit which is a renaming, then this is a
2500 -- violation of No_Obsolescent_Features.
2502 -- Note: this is not quite right if the user defines one of these units
2503 -- himself, but that's a marginal case, and fixing it is hard ???
2505 if Restriction_Check_Required (No_Obsolescent_Features) then
2506 declare
2507 F : constant File_Name_Type :=
2508 Unit_File_Name (Get_Source_Unit (U));
2509 begin
2510 if Is_Predefined_File_Name (F, Renamings_Included => True)
2511 and then not
2512 Is_Predefined_File_Name (F, Renamings_Included => False)
2513 then
2514 Check_Restriction (No_Obsolescent_Features, N);
2515 Restriction_Violation := True;
2516 end if;
2517 end;
2518 end if;
2520 -- Check No_Implementation_Units violation
2522 if Restriction_Check_Required (No_Implementation_Units) then
2523 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2524 null;
2525 else
2526 Check_Restriction (No_Implementation_Units, Nam);
2527 Restriction_Violation := True;
2528 end if;
2529 end if;
2531 -- Several actions are skipped for dummy packages (those supplied for
2532 -- with's where no matching file could be found). Such packages are
2533 -- identified by the Sloc value being set to No_Location.
2535 if Limited_Present (N) then
2537 -- Ada 2005 (AI-50217): Build visibility structures but do not
2538 -- analyze the unit.
2540 -- If the designated unit is a predefined unit, which might be used
2541 -- implicitly through the rtsfind machinery, a limited with clause
2542 -- on such a unit is usually pointless, because run-time units are
2543 -- unlikely to appear in mutually dependent units, and because this
2544 -- disables the rtsfind mechanism. We transform such limited with
2545 -- clauses into regular with clauses.
2547 if Sloc (U) /= No_Location then
2548 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (U)))
2550 -- In ASIS mode the rtsfind mechanism plays no role, and
2551 -- we need to maintain the original tree structure, so
2552 -- this transformation is not performed in this case.
2554 and then not ASIS_Mode
2555 then
2556 Set_Limited_Present (N, False);
2557 Analyze_With_Clause (N);
2558 else
2559 Build_Limited_Views (N);
2560 end if;
2561 end if;
2563 return;
2564 end if;
2566 -- If we are compiling under "don't quit" mode (-gnatq) and we have
2567 -- already detected serious errors then we mark the with-clause nodes as
2568 -- analyzed before the corresponding compilation unit is analyzed. This
2569 -- is done here to protect the frontend against never ending recursion
2570 -- caused by circularities in the sources (because the previous errors
2571 -- may break the regular machine of the compiler implemented in
2572 -- Load_Unit to detect circularities).
2574 if Serious_Errors_Detected > 0 and then Try_Semantics then
2575 Set_Analyzed (N);
2576 end if;
2578 Semantics (Library_Unit (N));
2580 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2582 if Sloc (U) /= No_Location then
2584 -- Check restrictions, except that we skip the check if this is an
2585 -- internal unit unless we are compiling the internal unit as the
2586 -- main unit. We also skip this for dummy packages.
2588 Check_Restriction_No_Dependence (Nam, N);
2590 if not Intunit or else Current_Sem_Unit = Main_Unit then
2591 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2592 end if;
2594 -- Deal with special case of GNAT.Current_Exceptions which interacts
2595 -- with the optimization of local raise statements into gotos.
2597 if Nkind (Nam) = N_Selected_Component
2598 and then Nkind (Prefix (Nam)) = N_Identifier
2599 and then Chars (Prefix (Nam)) = Name_Gnat
2600 and then Nam_In (Chars (Selector_Name (Nam)),
2601 Name_Most_Recent_Exception,
2602 Name_Exception_Traces)
2603 then
2604 Check_Restriction (No_Exception_Propagation, N);
2605 Special_Exception_Package_Used := True;
2606 end if;
2608 -- Check for inappropriate with of internal implementation unit if we
2609 -- are not compiling an internal unit and also check for withing unit
2610 -- in wrong version of Ada. Do not issue these messages for implicit
2611 -- with's generated by the compiler itself.
2613 if Implementation_Unit_Warnings
2614 and then not Intunit
2615 and then not Implicit_With (N)
2616 and then not Restriction_Violation
2617 then
2618 declare
2619 U_Kind : constant Kind_Of_Unit :=
2620 Get_Kind_Of_Unit (Get_Source_Unit (U));
2622 begin
2623 if U_Kind = Implementation_Unit then
2624 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2626 -- Add alternative name if available, otherwise issue a
2627 -- general warning message.
2629 if Error_Msg_Strlen /= 0 then
2630 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2631 else
2632 Error_Msg_F
2633 ("\use of this unit is non-portable " &
2634 "and version-dependent?i?", Name (N));
2635 end if;
2637 elsif U_Kind = Ada_2005_Unit
2638 and then Ada_Version < Ada_2005
2639 and then Warn_On_Ada_2005_Compatibility
2640 then
2641 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2643 elsif U_Kind = Ada_2012_Unit
2644 and then Ada_Version < Ada_2012
2645 and then Warn_On_Ada_2012_Compatibility
2646 then
2647 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2648 end if;
2649 end;
2650 end if;
2651 end if;
2653 -- Semantic analysis of a generic unit is performed on a copy of
2654 -- the original tree. Retrieve the entity on which semantic info
2655 -- actually appears.
2657 if Unit_Kind in N_Generic_Declaration then
2658 E_Name := Defining_Entity (U);
2660 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2661 -- the case of an instantiation, semantic analysis above will have
2662 -- replaced the unit by its instantiated version. If the instance body
2663 -- has been generated, the instance now denotes the body entity. For
2664 -- visibility purposes we need the entity of its spec.
2666 elsif (Unit_Kind = N_Package_Instantiation
2667 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2668 N_Package_Instantiation)
2669 and then Nkind (U) = N_Package_Body
2670 then
2671 E_Name := Corresponding_Spec (U);
2673 elsif Unit_Kind = N_Package_Instantiation
2674 and then Nkind (U) = N_Package_Instantiation
2675 and then Present (Instance_Spec (U))
2676 then
2677 -- If the instance has not been rewritten as a package declaration,
2678 -- then it appeared already in a previous with clause. Retrieve
2679 -- the entity from the previous instance.
2681 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2683 elsif Unit_Kind in N_Subprogram_Instantiation then
2685 -- The visible subprogram is created during instantiation, and is
2686 -- an attribute of the wrapper package. We retrieve the wrapper
2687 -- package directly from the instantiation node. If the instance
2688 -- is inlined the unit is still an instantiation. Otherwise it has
2689 -- been rewritten as the declaration of the wrapper itself.
2691 if Nkind (U) in N_Subprogram_Instantiation then
2692 E_Name :=
2693 Related_Instance
2694 (Defining_Entity (Specification (Instance_Spec (U))));
2695 else
2696 E_Name := Related_Instance (Defining_Entity (U));
2697 end if;
2699 elsif Unit_Kind = N_Package_Renaming_Declaration
2700 or else Unit_Kind in N_Generic_Renaming_Declaration
2701 then
2702 E_Name := Defining_Entity (U);
2704 elsif Unit_Kind = N_Subprogram_Body
2705 and then Nkind (Name (N)) = N_Selected_Component
2706 and then not Acts_As_Spec (Library_Unit (N))
2707 then
2708 -- For a child unit that has no spec, one has been created and
2709 -- analyzed. The entity required is that of the spec.
2711 E_Name := Corresponding_Spec (U);
2713 else
2714 E_Name := Defining_Entity (U);
2715 end if;
2717 if Nkind (Name (N)) = N_Selected_Component then
2719 -- Child unit in a with clause
2721 Change_Selected_Component_To_Expanded_Name (Name (N));
2723 -- If this is a child unit without a spec, and it has been analyzed
2724 -- already, a declaration has been created for it. The with_clause
2725 -- must reflect the actual body, and not the generated declaration,
2726 -- to prevent spurious binding errors involving an out-of-date spec.
2727 -- Note that this can only happen if the unit includes more than one
2728 -- with_clause for the child unit (e.g. in separate subunits).
2730 if Unit_Kind = N_Subprogram_Declaration
2731 and then Analyzed (Library_Unit (N))
2732 and then not Comes_From_Source (Library_Unit (N))
2733 then
2734 Set_Library_Unit (N,
2735 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2736 end if;
2737 end if;
2739 -- Restore style checks
2741 Style_Check := Save_Style_Check;
2743 -- Record the reference, but do NOT set the unit as referenced, we want
2744 -- to consider the unit as unreferenced if this is the only reference
2745 -- that occurs.
2747 Set_Entity_With_Checks (Name (N), E_Name);
2748 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2750 -- Generate references and check No_Dependence restriction for parents
2752 if Is_Child_Unit (E_Name) then
2753 Pref := Prefix (Name (N));
2754 Par_Name := Scope (E_Name);
2755 while Nkind (Pref) = N_Selected_Component loop
2756 Change_Selected_Component_To_Expanded_Name (Pref);
2758 if Present (Entity (Selector_Name (Pref)))
2759 and then
2760 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2761 and then Entity (Selector_Name (Pref)) /= Par_Name
2762 then
2763 -- The prefix is a child unit that denotes a renaming declaration.
2764 -- Replace the prefix directly with the renamed unit, because the
2765 -- rest of the prefix is irrelevant to the visibility of the real
2766 -- unit.
2768 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2769 exit;
2770 end if;
2772 Set_Entity_With_Checks (Pref, Par_Name);
2774 Generate_Reference (Par_Name, Pref);
2775 Check_Restriction_No_Dependence (Pref, N);
2776 Pref := Prefix (Pref);
2778 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2779 -- is set to Standard_Standard, and no attempt should be made to
2780 -- further unwind scopes.
2782 if Par_Name /= Standard_Standard then
2783 Par_Name := Scope (Par_Name);
2784 end if;
2786 -- Abandon processing in case of previous errors
2788 if No (Par_Name) then
2789 Check_Error_Detected;
2790 return;
2791 end if;
2792 end loop;
2794 if Present (Entity (Pref))
2795 and then not Analyzed (Parent (Parent (Entity (Pref))))
2796 then
2797 -- If the entity is set without its unit being compiled, the
2798 -- original parent is a renaming, and Par_Name is the renamed
2799 -- entity. For visibility purposes, we need the original entity,
2800 -- which must be analyzed now because Load_Unit directly retrieves
2801 -- the renamed unit, and the renaming declaration itself has not
2802 -- been analyzed.
2804 Analyze (Parent (Parent (Entity (Pref))));
2805 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2806 Par_Name := Entity (Pref);
2807 end if;
2809 -- Guard against missing or misspelled child units
2811 if Present (Par_Name) then
2812 Set_Entity_With_Checks (Pref, Par_Name);
2813 Generate_Reference (Par_Name, Pref);
2815 else
2816 pragma Assert (Serious_Errors_Detected /= 0);
2818 -- Mark the node to indicate that a related error has been posted.
2819 -- This defends further compilation passes against improper use of
2820 -- the invalid WITH clause node.
2822 Set_Error_Posted (N);
2823 Set_Name (N, Error);
2824 return;
2825 end if;
2826 end if;
2828 -- If the withed unit is System, and a system extension pragma is
2829 -- present, compile the extension now, rather than waiting for a
2830 -- visibility check on a specific entity.
2832 if Chars (E_Name) = Name_System
2833 and then Scope (E_Name) = Standard_Standard
2834 and then Present (System_Extend_Unit)
2835 and then Present_System_Aux (N)
2836 then
2837 -- If the extension is not present, an error will have been emitted
2839 null;
2840 end if;
2842 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2843 -- to private_with units; they will be made visible later (just before
2844 -- the private part is analyzed)
2846 if Private_Present (N) then
2847 Set_Is_Immediately_Visible (E_Name, False);
2848 end if;
2850 -- Propagate Fatal_Error setting from with'ed unit to current unit
2852 case Fatal_Error (Get_Source_Unit (Library_Unit (N))) is
2854 -- Nothing to do if with'ed unit had no error
2856 when None =>
2857 null;
2859 -- If with'ed unit had a detected fatal error, propagate it
2861 when Error_Detected =>
2862 Set_Fatal_Error (Current_Sem_Unit, Error_Detected);
2864 -- If with'ed unit had an ignored error, then propagate it but do not
2865 -- overide an existring setting.
2867 when Error_Ignored =>
2868 if Fatal_Error (Current_Sem_Unit) = None then
2869 Set_Fatal_Error (Current_Sem_Unit, Error_Ignored);
2870 end if;
2871 end case;
2873 Mark_Ghost_Clause (N);
2874 end Analyze_With_Clause;
2876 ------------------------------
2877 -- Check_Private_Child_Unit --
2878 ------------------------------
2880 procedure Check_Private_Child_Unit (N : Node_Id) is
2881 Lib_Unit : constant Node_Id := Unit (N);
2882 Item : Node_Id;
2883 Curr_Unit : Entity_Id;
2884 Sub_Parent : Node_Id;
2885 Priv_Child : Entity_Id;
2886 Par_Lib : Entity_Id;
2887 Par_Spec : Node_Id;
2889 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2890 -- Returns true if and only if the library unit is declared with
2891 -- an explicit designation of private.
2893 -----------------------------
2894 -- Is_Private_Library_Unit --
2895 -----------------------------
2897 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2898 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2900 begin
2901 return Private_Present (Comp_Unit);
2902 end Is_Private_Library_Unit;
2904 -- Start of processing for Check_Private_Child_Unit
2906 begin
2907 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2908 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2909 Par_Lib := Curr_Unit;
2911 elsif Nkind (Lib_Unit) = N_Subunit then
2913 -- The parent is itself a body. The parent entity is to be found in
2914 -- the corresponding spec.
2916 Sub_Parent := Library_Unit (N);
2917 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2919 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2920 -- enclosing body, retrieve the spec entity which is the proper
2921 -- ancestor we need for the following tests.
2923 if Ekind (Curr_Unit) = E_Package_Body then
2924 Curr_Unit := Spec_Entity (Curr_Unit);
2925 end if;
2927 Par_Lib := Curr_Unit;
2929 else
2930 Curr_Unit := Defining_Entity (Lib_Unit);
2932 Par_Lib := Curr_Unit;
2933 Par_Spec := Parent_Spec (Lib_Unit);
2935 if No (Par_Spec) then
2936 Par_Lib := Empty;
2937 else
2938 Par_Lib := Defining_Entity (Unit (Par_Spec));
2939 end if;
2940 end if;
2942 -- Loop through context items
2944 Item := First (Context_Items (N));
2945 while Present (Item) loop
2947 -- Ada 2005 (AI-262): Allow private_with of a private child package
2948 -- in public siblings
2950 if Nkind (Item) = N_With_Clause
2951 and then not Implicit_With (Item)
2952 and then not Limited_Present (Item)
2953 and then Is_Private_Descendant (Entity (Name (Item)))
2954 then
2955 Priv_Child := Entity (Name (Item));
2957 declare
2958 Curr_Parent : Entity_Id := Par_Lib;
2959 Child_Parent : Entity_Id := Scope (Priv_Child);
2960 Prv_Ancestor : Entity_Id := Child_Parent;
2961 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2963 begin
2964 -- If the child unit is a public child then locate the nearest
2965 -- private ancestor. Child_Parent will then be set to the
2966 -- parent of that ancestor.
2968 if not Is_Private_Library_Unit (Priv_Child) then
2969 while Present (Prv_Ancestor)
2970 and then not Is_Private_Library_Unit (Prv_Ancestor)
2971 loop
2972 Prv_Ancestor := Scope (Prv_Ancestor);
2973 end loop;
2975 if Present (Prv_Ancestor) then
2976 Child_Parent := Scope (Prv_Ancestor);
2977 end if;
2978 end if;
2980 while Present (Curr_Parent)
2981 and then Curr_Parent /= Standard_Standard
2982 and then Curr_Parent /= Child_Parent
2983 loop
2984 Curr_Private :=
2985 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2986 Curr_Parent := Scope (Curr_Parent);
2987 end loop;
2989 if No (Curr_Parent) then
2990 Curr_Parent := Standard_Standard;
2991 end if;
2993 if Curr_Parent /= Child_Parent then
2994 if Ekind (Priv_Child) = E_Generic_Package
2995 and then Chars (Priv_Child) in Text_IO_Package_Name
2996 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2997 then
2998 Error_Msg_NE
2999 ("& is a nested package, not a compilation unit",
3000 Name (Item), Priv_Child);
3002 else
3003 Error_Msg_N
3004 ("unit in with clause is private child unit!", Item);
3005 Error_Msg_NE
3006 ("\current unit must also have parent&!",
3007 Item, Child_Parent);
3008 end if;
3010 elsif Curr_Private
3011 or else Private_Present (Item)
3012 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
3013 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3014 and then not Acts_As_Spec (Parent (Lib_Unit)))
3015 then
3016 null;
3018 else
3019 Error_Msg_NE
3020 ("current unit must also be private descendant of&",
3021 Item, Child_Parent);
3022 end if;
3023 end;
3024 end if;
3026 Next (Item);
3027 end loop;
3029 end Check_Private_Child_Unit;
3031 ----------------------
3032 -- Check_Stub_Level --
3033 ----------------------
3035 procedure Check_Stub_Level (N : Node_Id) is
3036 Par : constant Node_Id := Parent (N);
3037 Kind : constant Node_Kind := Nkind (Par);
3039 begin
3040 if Nkind_In (Kind, N_Package_Body,
3041 N_Subprogram_Body,
3042 N_Task_Body,
3043 N_Protected_Body)
3044 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
3045 then
3046 null;
3048 -- In an instance, a missing stub appears at any level. A warning
3049 -- message will have been emitted already for the missing file.
3051 elsif not In_Instance then
3052 Error_Msg_N ("stub cannot appear in an inner scope", N);
3054 elsif Expander_Active then
3055 Error_Msg_N ("missing proper body", N);
3056 end if;
3057 end Check_Stub_Level;
3059 ------------------------
3060 -- Expand_With_Clause --
3061 ------------------------
3063 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
3064 Loc : constant Source_Ptr := Sloc (Nam);
3065 Ent : constant Entity_Id := Entity (Nam);
3066 Withn : Node_Id;
3067 P : Node_Id;
3069 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
3070 -- Build name to be used in implicit with_clause. In most cases this
3071 -- is the source name, but if renamings are present we must make the
3072 -- original unit visible, not the one it renames. The entity in the
3073 -- with clause is the renamed unit, but the identifier is the one from
3074 -- the source, which allows us to recover the unit renaming.
3076 ---------------------
3077 -- Build_Unit_Name --
3078 ---------------------
3080 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
3081 Ent : Entity_Id;
3082 Result : Node_Id;
3084 begin
3085 if Nkind (Nam) = N_Identifier then
3086 return New_Occurrence_Of (Entity (Nam), Loc);
3088 else
3089 Ent := Entity (Nam);
3091 if Present (Entity (Selector_Name (Nam)))
3092 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
3093 and then
3094 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
3095 = N_Package_Renaming_Declaration
3096 then
3097 -- The name in the with_clause is of the form A.B.C, and B is
3098 -- given by a renaming declaration. In that case we may not
3099 -- have analyzed the unit for B, but replaced it directly in
3100 -- lib-load with the unit it renames. We have to make A.B
3101 -- visible, so analyze the declaration for B now, in case it
3102 -- has not been done yet.
3104 Ent := Entity (Selector_Name (Nam));
3105 Analyze
3106 (Parent
3107 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3108 end if;
3110 Result :=
3111 Make_Expanded_Name (Loc,
3112 Chars => Chars (Entity (Nam)),
3113 Prefix => Build_Unit_Name (Prefix (Nam)),
3114 Selector_Name => New_Occurrence_Of (Ent, Loc));
3115 Set_Entity (Result, Ent);
3116 return Result;
3117 end if;
3118 end Build_Unit_Name;
3120 -- Start of processing for Expand_With_Clause
3122 begin
3123 Withn :=
3124 Make_With_Clause (Loc,
3125 Name => Build_Unit_Name (Nam));
3127 P := Parent (Unit_Declaration_Node (Ent));
3128 Set_Library_Unit (Withn, P);
3129 Set_Corresponding_Spec (Withn, Ent);
3130 Set_First_Name (Withn, True);
3131 Set_Implicit_With (Withn, True);
3133 -- If the unit is a package or generic package declaration, a private_
3134 -- with_clause on a child unit implies that the implicit with on the
3135 -- parent is also private.
3137 if Nkind_In (Unit (N), N_Package_Declaration,
3138 N_Generic_Package_Declaration)
3139 then
3140 Set_Private_Present (Withn, Private_Present (Item));
3141 end if;
3143 Prepend (Withn, Context_Items (N));
3144 Mark_Rewrite_Insertion (Withn);
3145 Install_Withed_Unit (Withn);
3147 -- If we have "with X.Y;", we want to recurse on "X", except in the
3148 -- unusual case where X.Y is a renaming of X. In that case, the scope
3149 -- of X will be null.
3151 if Nkind (Nam) = N_Expanded_Name
3152 and then Present (Scope (Entity (Prefix (Nam))))
3153 then
3154 Expand_With_Clause (Item, Prefix (Nam), N);
3155 end if;
3156 end Expand_With_Clause;
3158 --------------------------------
3159 -- Generate_Parent_References --
3160 --------------------------------
3162 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
3163 Pref : Node_Id;
3164 P_Name : Entity_Id := P_Id;
3166 begin
3167 if Nkind (N) = N_Subunit then
3168 Pref := Name (N);
3169 else
3170 Pref := Name (Parent (Defining_Entity (N)));
3171 end if;
3173 if Nkind (Pref) = N_Expanded_Name then
3175 -- Done already, if the unit has been compiled indirectly as
3176 -- part of the closure of its context because of inlining.
3178 return;
3179 end if;
3181 while Nkind (Pref) = N_Selected_Component loop
3182 Change_Selected_Component_To_Expanded_Name (Pref);
3183 Set_Entity (Pref, P_Name);
3184 Set_Etype (Pref, Etype (P_Name));
3185 Generate_Reference (P_Name, Pref, 'r');
3186 Pref := Prefix (Pref);
3187 P_Name := Scope (P_Name);
3188 end loop;
3190 -- The guard here on P_Name is to handle the error condition where
3191 -- the parent unit is missing because the file was not found.
3193 if Present (P_Name) then
3194 Set_Entity (Pref, P_Name);
3195 Set_Etype (Pref, Etype (P_Name));
3196 Generate_Reference (P_Name, Pref, 'r');
3197 Style.Check_Identifier (Pref, P_Name);
3198 end if;
3199 end Generate_Parent_References;
3201 ---------------------
3202 -- Has_With_Clause --
3203 ---------------------
3205 function Has_With_Clause
3206 (C_Unit : Node_Id;
3207 Pack : Entity_Id;
3208 Is_Limited : Boolean := False) return Boolean
3210 Item : Node_Id;
3212 function Named_Unit (Clause : Node_Id) return Entity_Id;
3213 -- Return the entity for the unit named in a [limited] with clause
3215 ----------------
3216 -- Named_Unit --
3217 ----------------
3219 function Named_Unit (Clause : Node_Id) return Entity_Id is
3220 begin
3221 if Nkind (Name (Clause)) = N_Selected_Component then
3222 return Entity (Selector_Name (Name (Clause)));
3223 else
3224 return Entity (Name (Clause));
3225 end if;
3226 end Named_Unit;
3228 -- Start of processing for Has_With_Clause
3230 begin
3231 if Present (Context_Items (C_Unit)) then
3232 Item := First (Context_Items (C_Unit));
3233 while Present (Item) loop
3234 if Nkind (Item) = N_With_Clause
3235 and then Limited_Present (Item) = Is_Limited
3236 and then Named_Unit (Item) = Pack
3237 then
3238 return True;
3239 end if;
3241 Next (Item);
3242 end loop;
3243 end if;
3245 return False;
3246 end Has_With_Clause;
3248 -----------------------------
3249 -- Implicit_With_On_Parent --
3250 -----------------------------
3252 procedure Implicit_With_On_Parent
3253 (Child_Unit : Node_Id;
3254 N : Node_Id)
3256 Loc : constant Source_Ptr := Sloc (N);
3257 P : constant Node_Id := Parent_Spec (Child_Unit);
3258 P_Unit : Node_Id := Unit (P);
3259 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3260 Withn : Node_Id;
3262 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3263 -- Build prefix of child unit name. Recurse if needed
3265 function Build_Unit_Name return Node_Id;
3266 -- If the unit is a child unit, build qualified name with all ancestors
3268 -------------------------
3269 -- Build_Ancestor_Name --
3270 -------------------------
3272 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3273 P_Ref : constant Node_Id :=
3274 New_Occurrence_Of (Defining_Entity (P), Loc);
3275 P_Spec : Node_Id := P;
3277 begin
3278 -- Ancestor may have been rewritten as a package body. Retrieve
3279 -- the original spec to trace earlier ancestors.
3281 if Nkind (P) = N_Package_Body
3282 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3283 then
3284 P_Spec := Original_Node (P);
3285 end if;
3287 if No (Parent_Spec (P_Spec)) then
3288 return P_Ref;
3289 else
3290 return
3291 Make_Selected_Component (Loc,
3292 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3293 Selector_Name => P_Ref);
3294 end if;
3295 end Build_Ancestor_Name;
3297 ---------------------
3298 -- Build_Unit_Name --
3299 ---------------------
3301 function Build_Unit_Name return Node_Id is
3302 Result : Node_Id;
3304 begin
3305 if No (Parent_Spec (P_Unit)) then
3306 return New_Occurrence_Of (P_Name, Loc);
3308 else
3309 Result :=
3310 Make_Expanded_Name (Loc,
3311 Chars => Chars (P_Name),
3312 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3313 Selector_Name => New_Occurrence_Of (P_Name, Loc));
3314 Set_Entity (Result, P_Name);
3315 return Result;
3316 end if;
3317 end Build_Unit_Name;
3319 -- Start of processing for Implicit_With_On_Parent
3321 begin
3322 -- The unit of the current compilation may be a package body that
3323 -- replaces an instance node. In this case we need the original instance
3324 -- node to construct the proper parent name.
3326 if Nkind (P_Unit) = N_Package_Body
3327 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3328 then
3329 P_Unit := Original_Node (P_Unit);
3330 end if;
3332 -- We add the implicit with if the child unit is the current unit being
3333 -- compiled. If the current unit is a body, we do not want to add an
3334 -- implicit_with a second time to the corresponding spec.
3336 if Nkind (Child_Unit) = N_Package_Declaration
3337 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3338 then
3339 return;
3340 end if;
3342 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3344 Set_Library_Unit (Withn, P);
3345 Set_Corresponding_Spec (Withn, P_Name);
3346 Set_First_Name (Withn, True);
3347 Set_Implicit_With (Withn, True);
3349 -- Node is placed at the beginning of the context items, so that
3350 -- subsequent use clauses on the parent can be validated.
3352 Prepend (Withn, Context_Items (N));
3353 Mark_Rewrite_Insertion (Withn);
3354 Install_Withed_Unit (Withn);
3356 if Is_Child_Spec (P_Unit) then
3357 Implicit_With_On_Parent (P_Unit, N);
3358 end if;
3359 end Implicit_With_On_Parent;
3361 --------------
3362 -- In_Chain --
3363 --------------
3365 function In_Chain (E : Entity_Id) return Boolean is
3366 H : Entity_Id;
3368 begin
3369 H := Current_Entity (E);
3370 while Present (H) loop
3371 if H = E then
3372 return True;
3373 else
3374 H := Homonym (H);
3375 end if;
3376 end loop;
3378 return False;
3379 end In_Chain;
3381 ---------------------
3382 -- Install_Context --
3383 ---------------------
3385 procedure Install_Context (N : Node_Id) is
3386 Lib_Unit : constant Node_Id := Unit (N);
3388 begin
3389 Install_Context_Clauses (N);
3391 if Is_Child_Spec (Lib_Unit) then
3392 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3393 end if;
3395 Install_Limited_Context_Clauses (N);
3396 end Install_Context;
3398 -----------------------------
3399 -- Install_Context_Clauses --
3400 -----------------------------
3402 procedure Install_Context_Clauses (N : Node_Id) is
3403 Lib_Unit : constant Node_Id := Unit (N);
3404 Item : Node_Id;
3405 Uname_Node : Entity_Id;
3406 Check_Private : Boolean := False;
3407 Decl_Node : Node_Id;
3408 Lib_Parent : Entity_Id;
3410 begin
3411 -- First skip configuration pragmas at the start of the context. They
3412 -- are not technically part of the context clause, but that's where the
3413 -- parser puts them. Note they were analyzed in Analyze_Context.
3415 Item := First (Context_Items (N));
3416 while Present (Item)
3417 and then Nkind (Item) = N_Pragma
3418 and then Pragma_Name (Item) in Configuration_Pragma_Names
3419 loop
3420 Next (Item);
3421 end loop;
3423 -- Loop through the actual context clause items. We process everything
3424 -- except Limited_With clauses in this routine. Limited_With clauses
3425 -- are separately installed (see Install_Limited_Context_Clauses).
3427 while Present (Item) loop
3429 -- Case of explicit WITH clause
3431 if Nkind (Item) = N_With_Clause
3432 and then not Implicit_With (Item)
3433 then
3434 if Limited_Present (Item) then
3436 -- Limited withed units will be installed later
3438 goto Continue;
3440 -- If Name (Item) is not an entity name, something is wrong, and
3441 -- this will be detected in due course, for now ignore the item
3443 elsif not Is_Entity_Name (Name (Item)) then
3444 goto Continue;
3446 elsif No (Entity (Name (Item))) then
3447 Set_Entity (Name (Item), Any_Id);
3448 goto Continue;
3449 end if;
3451 Uname_Node := Entity (Name (Item));
3453 if Is_Private_Descendant (Uname_Node) then
3454 Check_Private := True;
3455 end if;
3457 Install_Withed_Unit (Item);
3459 Decl_Node := Unit_Declaration_Node (Uname_Node);
3461 -- If the unit is a subprogram instance, it appears nested within
3462 -- a package that carries the parent information.
3464 if Is_Generic_Instance (Uname_Node)
3465 and then Ekind (Uname_Node) /= E_Package
3466 then
3467 Decl_Node := Parent (Parent (Decl_Node));
3468 end if;
3470 if Is_Child_Spec (Decl_Node) then
3471 if Nkind (Name (Item)) = N_Expanded_Name then
3472 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3473 else
3474 -- If not an expanded name, the child unit must be a
3475 -- renaming, nothing to do.
3477 null;
3478 end if;
3480 elsif Nkind (Decl_Node) = N_Subprogram_Body
3481 and then not Acts_As_Spec (Parent (Decl_Node))
3482 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3483 then
3484 Implicit_With_On_Parent
3485 (Unit (Library_Unit (Parent (Decl_Node))), N);
3486 end if;
3488 -- Check license conditions unless this is a dummy unit
3490 if Sloc (Library_Unit (Item)) /= No_Location then
3491 License_Check : declare
3492 Withu : constant Unit_Number_Type :=
3493 Get_Source_Unit (Library_Unit (Item));
3494 Withl : constant License_Type :=
3495 License (Source_Index (Withu));
3496 Unitl : constant License_Type :=
3497 License (Source_Index (Current_Sem_Unit));
3499 procedure License_Error;
3500 -- Signal error of bad license
3502 -------------------
3503 -- License_Error --
3504 -------------------
3506 procedure License_Error is
3507 begin
3508 Error_Msg_N
3509 ("license of withed unit & may be inconsistent??",
3510 Name (Item));
3511 end License_Error;
3513 -- Start of processing for License_Check
3515 begin
3516 -- Exclude license check if withed unit is an internal unit.
3517 -- This situation arises e.g. with the GPL version of GNAT.
3519 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3520 null;
3522 -- Otherwise check various cases
3523 else
3524 case Unitl is
3525 when Unknown =>
3526 null;
3528 when Restricted =>
3529 if Withl = GPL then
3530 License_Error;
3531 end if;
3533 when GPL =>
3534 if Withl = Restricted then
3535 License_Error;
3536 end if;
3538 when Modified_GPL =>
3539 if Withl = Restricted or else Withl = GPL then
3540 License_Error;
3541 end if;
3543 when Unrestricted =>
3544 null;
3545 end case;
3546 end if;
3547 end License_Check;
3548 end if;
3550 -- Case of USE PACKAGE clause
3552 elsif Nkind (Item) = N_Use_Package_Clause then
3553 Analyze_Use_Package (Item);
3555 -- Case of USE TYPE clause
3557 elsif Nkind (Item) = N_Use_Type_Clause then
3558 Analyze_Use_Type (Item);
3560 -- case of PRAGMA
3562 elsif Nkind (Item) = N_Pragma then
3563 Analyze (Item);
3564 end if;
3566 <<Continue>>
3567 Next (Item);
3568 end loop;
3570 if Is_Child_Spec (Lib_Unit) then
3572 -- The unit also has implicit with_clauses on its own parents
3574 if No (Context_Items (N)) then
3575 Set_Context_Items (N, New_List);
3576 end if;
3578 Implicit_With_On_Parent (Lib_Unit, N);
3579 end if;
3581 -- If the unit is a body, the context of the specification must also
3582 -- be installed. That includes private with_clauses in that context.
3584 if Nkind (Lib_Unit) = N_Package_Body
3585 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3586 and then not Acts_As_Spec (N))
3587 then
3588 Install_Context (Library_Unit (N));
3590 -- Only install private with-clauses of a spec that comes from
3591 -- source, excluding specs created for a subprogram body that is
3592 -- a child unit.
3594 if Comes_From_Source (Library_Unit (N)) then
3595 Install_Private_With_Clauses
3596 (Defining_Entity (Unit (Library_Unit (N))));
3597 end if;
3599 if Is_Child_Spec (Unit (Library_Unit (N))) then
3601 -- If the unit is the body of a public child unit, the private
3602 -- declarations of the parent must be made visible. If the child
3603 -- unit is private, the private declarations have been installed
3604 -- already in the call to Install_Parents for the spec. Installing
3605 -- private declarations must be done for all ancestors of public
3606 -- child units. In addition, sibling units mentioned in the
3607 -- context clause of the body are directly visible.
3609 declare
3610 Lib_Spec : Node_Id;
3611 P : Node_Id;
3612 P_Name : Entity_Id;
3614 begin
3615 Lib_Spec := Unit (Library_Unit (N));
3616 while Is_Child_Spec (Lib_Spec) loop
3617 P := Unit (Parent_Spec (Lib_Spec));
3618 P_Name := Defining_Entity (P);
3620 if not (Private_Present (Parent (Lib_Spec)))
3621 and then not In_Private_Part (P_Name)
3622 then
3623 Install_Private_Declarations (P_Name);
3624 Install_Private_With_Clauses (P_Name);
3625 Set_Use (Private_Declarations (Specification (P)));
3626 end if;
3628 Lib_Spec := P;
3629 end loop;
3630 end;
3631 end if;
3633 -- For a package body, children in context are immediately visible
3635 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3636 end if;
3638 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3639 N_Generic_Subprogram_Declaration,
3640 N_Package_Declaration,
3641 N_Subprogram_Declaration)
3642 then
3643 if Is_Child_Spec (Lib_Unit) then
3644 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3645 Set_Is_Private_Descendant
3646 (Defining_Entity (Lib_Unit),
3647 Is_Private_Descendant (Lib_Parent)
3648 or else Private_Present (Parent (Lib_Unit)));
3650 else
3651 Set_Is_Private_Descendant
3652 (Defining_Entity (Lib_Unit),
3653 Private_Present (Parent (Lib_Unit)));
3654 end if;
3655 end if;
3657 if Check_Private then
3658 Check_Private_Child_Unit (N);
3659 end if;
3660 end Install_Context_Clauses;
3662 -------------------------------------
3663 -- Install_Limited_Context_Clauses --
3664 -------------------------------------
3666 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3667 Item : Node_Id;
3669 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3670 -- Check that the unlimited view of a given compilation_unit is not
3671 -- already visible through "use + renamings".
3673 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3674 -- Check that if a limited_with clause of a given compilation_unit
3675 -- mentions a descendant of a private child of some library unit, then
3676 -- the given compilation_unit must be the declaration of a private
3677 -- descendant of that library unit, or a public descendant of such. The
3678 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3679 -- use entities on the limited with_clauses because their units have not
3680 -- been analyzed, so we have to climb the tree of ancestors looking for
3681 -- private keywords.
3683 procedure Expand_Limited_With_Clause
3684 (Comp_Unit : Node_Id;
3685 Nam : Node_Id;
3686 N : Node_Id);
3687 -- If a child unit appears in a limited_with clause, there are implicit
3688 -- limited_with clauses on all parents that are not already visible
3689 -- through a regular with clause. This procedure creates the implicit
3690 -- limited with_clauses for the parents and loads the corresponding
3691 -- units. The shadow entities are created when the inserted clause is
3692 -- analyzed. Implements Ada 2005 (AI-50217).
3694 ---------------------
3695 -- Check_Renamings --
3696 ---------------------
3698 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3699 Item : Node_Id;
3700 Spec : Node_Id;
3701 WEnt : Entity_Id;
3702 Nam : Node_Id;
3703 E : Entity_Id;
3704 E2 : Entity_Id;
3706 begin
3707 pragma Assert (Nkind (W) = N_With_Clause);
3709 -- Protect the frontend against previous critical errors
3711 case Nkind (Unit (Library_Unit (W))) is
3712 when N_Generic_Package_Declaration
3713 | N_Generic_Subprogram_Declaration
3714 | N_Package_Declaration
3715 | N_Subprogram_Declaration
3717 null;
3719 when others =>
3720 return;
3721 end case;
3723 -- Check "use + renamings"
3725 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3726 Spec := Specification (Unit (P));
3728 Item := First (Visible_Declarations (Spec));
3729 while Present (Item) loop
3731 -- Look only at use package clauses
3733 if Nkind (Item) = N_Use_Package_Clause then
3735 -- Traverse the list of packages
3737 Nam := First (Names (Item));
3738 while Present (Nam) loop
3739 E := Entity (Nam);
3741 pragma Assert (Present (Parent (E)));
3743 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3744 and then Renamed_Entity (E) = WEnt
3745 then
3746 -- The unlimited view is visible through use clause and
3747 -- renamings. There is no need to generate the error
3748 -- message here because Is_Visible_Through_Renamings
3749 -- takes care of generating the precise error message.
3751 return;
3753 elsif Nkind (Parent (E)) = N_Package_Specification then
3755 -- The use clause may refer to a local package.
3756 -- Check all the enclosing scopes.
3758 E2 := E;
3759 while E2 /= Standard_Standard and then E2 /= WEnt loop
3760 E2 := Scope (E2);
3761 end loop;
3763 if E2 = WEnt then
3764 Error_Msg_N
3765 ("unlimited view visible through use clause ", W);
3766 return;
3767 end if;
3768 end if;
3770 Next (Nam);
3771 end loop;
3772 end if;
3774 Next (Item);
3775 end loop;
3777 -- Recursive call to check all the ancestors
3779 if Is_Child_Spec (Unit (P)) then
3780 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3781 end if;
3782 end Check_Renamings;
3784 ---------------------------------------
3785 -- Check_Private_Limited_Withed_Unit --
3786 ---------------------------------------
3788 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3789 Curr_Parent : Node_Id;
3790 Child_Parent : Node_Id;
3791 Curr_Private : Boolean;
3793 begin
3794 -- Compilation unit of the parent of the withed library unit
3796 Child_Parent := Library_Unit (Item);
3798 -- If the child unit is a public child, then locate its nearest
3799 -- private ancestor, if any, then Child_Parent will then be set to
3800 -- the parent of that ancestor.
3802 if not Private_Present (Library_Unit (Item)) then
3803 while Present (Child_Parent)
3804 and then not Private_Present (Child_Parent)
3805 loop
3806 Child_Parent := Parent_Spec (Unit (Child_Parent));
3807 end loop;
3809 if No (Child_Parent) then
3810 return;
3811 end if;
3812 end if;
3814 Child_Parent := Parent_Spec (Unit (Child_Parent));
3816 -- Traverse all the ancestors of the current compilation unit to
3817 -- check if it is a descendant of named library unit.
3819 Curr_Parent := Parent (Item);
3820 Curr_Private := Private_Present (Curr_Parent);
3822 while Present (Parent_Spec (Unit (Curr_Parent)))
3823 and then Curr_Parent /= Child_Parent
3824 loop
3825 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3826 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3827 end loop;
3829 if Curr_Parent /= Child_Parent then
3830 Error_Msg_N
3831 ("unit in with clause is private child unit!", Item);
3832 Error_Msg_NE
3833 ("\current unit must also have parent&!",
3834 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3836 elsif Private_Present (Parent (Item))
3837 or else Curr_Private
3838 or else Private_Present (Item)
3839 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3840 N_Subprogram_Body,
3841 N_Subunit)
3842 then
3843 -- Current unit is private, of descendant of a private unit
3845 null;
3847 else
3848 Error_Msg_NE
3849 ("current unit must also be private descendant of&",
3850 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3851 end if;
3852 end Check_Private_Limited_Withed_Unit;
3854 --------------------------------
3855 -- Expand_Limited_With_Clause --
3856 --------------------------------
3858 procedure Expand_Limited_With_Clause
3859 (Comp_Unit : Node_Id;
3860 Nam : Node_Id;
3861 N : Node_Id)
3863 Loc : constant Source_Ptr := Sloc (Nam);
3864 Unum : Unit_Number_Type;
3865 Withn : Node_Id;
3867 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3868 -- Returns true if the context already includes a with_clause for
3869 -- this unit. If the with_clause is non-limited, the unit is fully
3870 -- visible and an implicit limited_with should not be created. If
3871 -- there is already a limited_with clause for W, a second one is
3872 -- simply redundant.
3874 --------------------------
3875 -- Previous_Withed_Unit --
3876 --------------------------
3878 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3879 Item : Node_Id;
3881 begin
3882 -- A limited with_clause cannot appear in the same context_clause
3883 -- as a nonlimited with_clause which mentions the same library.
3885 Item := First (Context_Items (Comp_Unit));
3886 while Present (Item) loop
3887 if Nkind (Item) = N_With_Clause
3888 and then Library_Unit (Item) = Library_Unit (W)
3889 then
3890 return True;
3891 end if;
3893 Next (Item);
3894 end loop;
3896 return False;
3897 end Previous_Withed_Unit;
3899 -- Start of processing for Expand_Limited_With_Clause
3901 begin
3902 if Nkind (Nam) = N_Identifier then
3904 -- Create node for name of withed unit
3906 Withn :=
3907 Make_With_Clause (Loc,
3908 Name => New_Copy (Nam));
3910 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3911 Withn :=
3912 Make_With_Clause (Loc,
3913 Name => Make_Selected_Component (Loc,
3914 Prefix => New_Copy_Tree (Prefix (Nam)),
3915 Selector_Name => New_Copy (Selector_Name (Nam))));
3916 Set_Parent (Withn, Parent (N));
3917 end if;
3919 Set_Limited_Present (Withn);
3920 Set_First_Name (Withn);
3921 Set_Implicit_With (Withn);
3923 Unum :=
3924 Load_Unit
3925 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3926 Required => True,
3927 Subunit => False,
3928 Error_Node => Nam);
3930 -- Do not generate a limited_with_clause on the current unit. This
3931 -- path is taken when a unit has a limited_with clause on one of its
3932 -- child units.
3934 if Unum = Current_Sem_Unit then
3935 return;
3936 end if;
3938 Set_Library_Unit (Withn, Cunit (Unum));
3939 Set_Corresponding_Spec
3940 (Withn, Specification (Unit (Cunit (Unum))));
3942 if not Previous_Withed_Unit (Withn) then
3943 Prepend (Withn, Context_Items (Parent (N)));
3944 Mark_Rewrite_Insertion (Withn);
3946 -- Add implicit limited_with_clauses for parents of child units
3947 -- mentioned in limited_with clauses.
3949 if Nkind (Nam) = N_Selected_Component then
3950 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3951 end if;
3953 Analyze (Withn);
3955 if not Limited_View_Installed (Withn) then
3956 Install_Limited_Withed_Unit (Withn);
3957 end if;
3958 end if;
3959 end Expand_Limited_With_Clause;
3961 -- Start of processing for Install_Limited_Context_Clauses
3963 begin
3964 Item := First (Context_Items (N));
3965 while Present (Item) loop
3966 if Nkind (Item) = N_With_Clause
3967 and then Limited_Present (Item)
3968 and then not Error_Posted (Item)
3969 then
3970 if Nkind (Name (Item)) = N_Selected_Component then
3971 Expand_Limited_With_Clause
3972 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3973 end if;
3975 Check_Private_Limited_Withed_Unit (Item);
3977 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
3978 Check_Renamings (Parent_Spec (Unit (N)), Item);
3979 end if;
3981 -- A unit may have a limited with on itself if it has a limited
3982 -- with_clause on one of its child units. In that case it is
3983 -- already being compiled and it makes no sense to install its
3984 -- limited view.
3986 -- If the item is a limited_private_with_clause, install it if the
3987 -- current unit is a body or if it is a private child. Otherwise
3988 -- the private clause is installed before analyzing the private
3989 -- part of the current unit.
3991 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3992 and then not Limited_View_Installed (Item)
3993 and then
3994 not Is_Ancestor_Unit
3995 (Library_Unit (Item), Cunit (Current_Sem_Unit))
3996 then
3997 if not Private_Present (Item)
3998 or else Private_Present (N)
3999 or else Nkind_In (Unit (N), N_Package_Body,
4000 N_Subprogram_Body,
4001 N_Subunit)
4002 then
4003 Install_Limited_Withed_Unit (Item);
4004 end if;
4005 end if;
4006 end if;
4008 Next (Item);
4009 end loop;
4011 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4012 -- looking for incomplete subtype declarations of incomplete types
4013 -- visible through a limited with clause.
4015 if Ada_Version >= Ada_2005
4016 and then Analyzed (N)
4017 and then Nkind (Unit (N)) = N_Package_Declaration
4018 then
4019 declare
4020 Decl : Node_Id;
4021 Def_Id : Entity_Id;
4022 Non_Lim_View : Entity_Id;
4024 begin
4025 Decl := First (Visible_Declarations (Specification (Unit (N))));
4026 while Present (Decl) loop
4027 if Nkind (Decl) = N_Subtype_Declaration
4028 and then
4029 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
4030 and then
4031 From_Limited_With (Defining_Identifier (Decl))
4032 then
4033 Def_Id := Defining_Identifier (Decl);
4034 Non_Lim_View := Non_Limited_View (Def_Id);
4036 if not Is_Incomplete_Type (Non_Lim_View) then
4038 -- Convert an incomplete subtype declaration into a
4039 -- corresponding non-limited view subtype declaration.
4040 -- This is usually the case when analyzing a body that
4041 -- has regular with clauses, when the spec has limited
4042 -- ones.
4044 -- If the non-limited view is still incomplete, it is
4045 -- the dummy entry already created, and the declaration
4046 -- cannot be reanalyzed. This is the case when installing
4047 -- a parent unit that has limited with-clauses.
4049 Set_Subtype_Indication (Decl,
4050 New_Occurrence_Of (Non_Lim_View, Sloc (Def_Id)));
4051 Set_Etype (Def_Id, Non_Lim_View);
4052 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
4053 Set_Analyzed (Decl, False);
4055 -- Reanalyze the declaration, suppressing the call to
4056 -- Enter_Name to avoid duplicate names.
4058 Analyze_Subtype_Declaration
4059 (N => Decl,
4060 Skip => True);
4061 end if;
4062 end if;
4064 Next (Decl);
4065 end loop;
4066 end;
4067 end if;
4068 end Install_Limited_Context_Clauses;
4070 ---------------------
4071 -- Install_Parents --
4072 ---------------------
4074 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
4075 P : Node_Id;
4076 E_Name : Entity_Id;
4077 P_Name : Entity_Id;
4078 P_Spec : Node_Id;
4080 begin
4081 P := Unit (Parent_Spec (Lib_Unit));
4082 P_Name := Get_Parent_Entity (P);
4084 if Etype (P_Name) = Any_Type then
4085 return;
4086 end if;
4088 if Ekind (P_Name) = E_Generic_Package
4089 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
4090 N_Generic_Package_Declaration)
4091 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
4092 then
4093 Error_Msg_N
4094 ("child of a generic package must be a generic unit", Lib_Unit);
4096 elsif not Is_Package_Or_Generic_Package (P_Name) then
4097 Error_Msg_N
4098 ("parent unit must be package or generic package", Lib_Unit);
4099 raise Unrecoverable_Error;
4101 elsif Present (Renamed_Object (P_Name)) then
4102 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
4103 raise Unrecoverable_Error;
4105 -- Verify that a child of an instance is itself an instance, or the
4106 -- renaming of one. Given that an instance that is a unit is replaced
4107 -- with a package declaration, check against the original node. The
4108 -- parent may be currently being instantiated, in which case it appears
4109 -- as a declaration, but the generic_parent is already established
4110 -- indicating that we deal with an instance.
4112 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
4113 if Nkind (Lib_Unit) in N_Renaming_Declaration
4114 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
4115 or else
4116 (Nkind (Lib_Unit) = N_Package_Declaration
4117 and then Present (Generic_Parent (Specification (Lib_Unit))))
4118 then
4119 null;
4120 else
4121 Error_Msg_N
4122 ("child of an instance must be an instance or renaming",
4123 Lib_Unit);
4124 end if;
4125 end if;
4127 -- This is the recursive call that ensures all parents are loaded
4129 if Is_Child_Spec (P) then
4130 Install_Parents (P,
4131 Is_Private or else Private_Present (Parent (Lib_Unit)));
4132 end if;
4134 -- Now we can install the context for this parent
4136 Install_Context_Clauses (Parent_Spec (Lib_Unit));
4137 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4138 Install_Siblings (P_Name, Parent (Lib_Unit));
4140 -- The child unit is in the declarative region of the parent. The parent
4141 -- must therefore appear in the scope stack and be visible, as when
4142 -- compiling the corresponding body. If the child unit is private or it
4143 -- is a package body, private declarations must be accessible as well.
4144 -- Use declarations in the parent must also be installed. Finally, other
4145 -- child units of the same parent that are in the context are
4146 -- immediately visible.
4148 -- Find entity for compilation unit, and set its private descendant
4149 -- status as needed. Indicate that it is a compilation unit, which is
4150 -- redundant in general, but needed if this is a generated child spec
4151 -- for a child body without previous spec.
4153 E_Name := Defining_Entity (Lib_Unit);
4155 Set_Is_Child_Unit (E_Name);
4156 Set_Is_Compilation_Unit (E_Name);
4158 Set_Is_Private_Descendant (E_Name,
4159 Is_Private_Descendant (P_Name)
4160 or else Private_Present (Parent (Lib_Unit)));
4162 P_Spec := Package_Specification (P_Name);
4163 Push_Scope (P_Name);
4165 -- Save current visibility of unit
4167 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4168 Is_Immediately_Visible (P_Name);
4169 Set_Is_Immediately_Visible (P_Name);
4170 Install_Visible_Declarations (P_Name);
4171 Set_Use (Visible_Declarations (P_Spec));
4173 -- If the parent is a generic unit, its formal part may contain formal
4174 -- packages and use clauses for them.
4176 if Ekind (P_Name) = E_Generic_Package then
4177 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4178 end if;
4180 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4181 Install_Private_Declarations (P_Name);
4182 Install_Private_With_Clauses (P_Name);
4183 Set_Use (Private_Declarations (P_Spec));
4184 end if;
4185 end Install_Parents;
4187 ----------------------------------
4188 -- Install_Private_With_Clauses --
4189 ----------------------------------
4191 procedure Install_Private_With_Clauses (P : Entity_Id) is
4192 Decl : constant Node_Id := Unit_Declaration_Node (P);
4193 Item : Node_Id;
4195 begin
4196 if Debug_Flag_I then
4197 Write_Str ("install private with clauses of ");
4198 Write_Name (Chars (P));
4199 Write_Eol;
4200 end if;
4202 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4203 Item := First (Context_Items (Parent (Decl)));
4204 while Present (Item) loop
4205 if Nkind (Item) = N_With_Clause
4206 and then Private_Present (Item)
4207 then
4208 -- If the unit is an ancestor of the current one, it is the
4209 -- case of a private limited with clause on a child unit, and
4210 -- the compilation of one of its descendants, In that case the
4211 -- limited view is errelevant.
4213 if Limited_Present (Item) then
4214 if not Limited_View_Installed (Item)
4215 and then
4216 not Is_Ancestor_Unit (Library_Unit (Item),
4217 Cunit (Current_Sem_Unit))
4218 then
4219 Install_Limited_Withed_Unit (Item);
4220 end if;
4221 else
4222 Install_Withed_Unit (Item, Private_With_OK => True);
4223 end if;
4224 end if;
4226 Next (Item);
4227 end loop;
4228 end if;
4229 end Install_Private_With_Clauses;
4231 ----------------------
4232 -- Install_Siblings --
4233 ----------------------
4235 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4236 Item : Node_Id;
4237 Id : Entity_Id;
4238 Prev : Entity_Id;
4240 begin
4241 -- Iterate over explicit with clauses, and check whether the scope of
4242 -- each entity is an ancestor of the current unit, in which case it is
4243 -- immediately visible.
4245 Item := First (Context_Items (N));
4246 while Present (Item) loop
4248 -- Do not install private_with_clauses declaration, unless unit
4249 -- is itself a private child unit, or is a body. Note that for a
4250 -- subprogram body the private_with_clause does not take effect
4251 -- until after the specification.
4253 if Nkind (Item) /= N_With_Clause
4254 or else Implicit_With (Item)
4255 or else Limited_Present (Item)
4256 or else Error_Posted (Item)
4258 -- Skip processing malformed trees
4260 or else (Try_Semantics
4261 and then Nkind (Name (Item)) not in N_Has_Entity)
4262 then
4263 null;
4265 elsif not Private_Present (Item)
4266 or else Private_Present (N)
4267 or else Nkind (Unit (N)) = N_Package_Body
4268 then
4269 Id := Entity (Name (Item));
4271 if Is_Child_Unit (Id)
4272 and then Is_Ancestor_Package (Scope (Id), U_Name)
4273 then
4274 Set_Is_Immediately_Visible (Id);
4276 -- Check for the presence of another unit in the context that
4277 -- may be inadvertently hidden by the child.
4279 Prev := Current_Entity (Id);
4281 if Present (Prev)
4282 and then Is_Immediately_Visible (Prev)
4283 and then not Is_Child_Unit (Prev)
4284 then
4285 declare
4286 Clause : Node_Id;
4288 begin
4289 Clause := First (Context_Items (N));
4290 while Present (Clause) loop
4291 if Nkind (Clause) = N_With_Clause
4292 and then Entity (Name (Clause)) = Prev
4293 then
4294 Error_Msg_NE
4295 ("child unit& hides compilation unit " &
4296 "with the same name??",
4297 Name (Item), Id);
4298 exit;
4299 end if;
4301 Next (Clause);
4302 end loop;
4303 end;
4304 end if;
4306 -- The With_Clause may be on a grand-child or one of its further
4307 -- descendants, which makes a child immediately visible. Examine
4308 -- ancestry to determine whether such a child exists. For example,
4309 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4310 -- is immediately visible.
4312 elsif Is_Child_Unit (Id) then
4313 declare
4314 Par : Entity_Id;
4316 begin
4317 Par := Scope (Id);
4318 while Is_Child_Unit (Par) loop
4319 if Is_Ancestor_Package (Scope (Par), U_Name) then
4320 Set_Is_Immediately_Visible (Par);
4321 exit;
4322 end if;
4324 Par := Scope (Par);
4325 end loop;
4326 end;
4327 end if;
4329 -- If the item is a private with-clause on a child unit, the parent
4330 -- may have been installed already, but the child unit must remain
4331 -- invisible until installed in a private part or body, unless there
4332 -- is already a regular with_clause for it in the current unit.
4334 elsif Private_Present (Item) then
4335 Id := Entity (Name (Item));
4337 if Is_Child_Unit (Id) then
4338 declare
4339 Clause : Node_Id;
4341 function In_Context return Boolean;
4342 -- Scan context of current unit, to check whether there is
4343 -- a with_clause on the same unit as a private with-clause
4344 -- on a parent, in which case child unit is visible. If the
4345 -- unit is a grand-child, the same applies to its parent.
4347 ----------------
4348 -- In_Context --
4349 ----------------
4351 function In_Context return Boolean is
4352 begin
4353 Clause :=
4354 First (Context_Items (Cunit (Current_Sem_Unit)));
4355 while Present (Clause) loop
4356 if Nkind (Clause) = N_With_Clause
4357 and then Comes_From_Source (Clause)
4358 and then Is_Entity_Name (Name (Clause))
4359 and then not Private_Present (Clause)
4360 then
4361 if Entity (Name (Clause)) = Id
4362 or else
4363 (Nkind (Name (Clause)) = N_Expanded_Name
4364 and then Entity (Prefix (Name (Clause))) = Id)
4365 then
4366 return True;
4367 end if;
4368 end if;
4370 Next (Clause);
4371 end loop;
4373 return False;
4374 end In_Context;
4376 begin
4377 Set_Is_Visible_Lib_Unit (Id, In_Context);
4378 end;
4379 end if;
4380 end if;
4382 Next (Item);
4383 end loop;
4384 end Install_Siblings;
4386 ---------------------------------
4387 -- Install_Limited_Withed_Unit --
4388 ---------------------------------
4390 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4391 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4392 E : Entity_Id;
4393 P : Entity_Id;
4394 Is_Child_Package : Boolean := False;
4395 Lim_Header : Entity_Id;
4396 Lim_Typ : Entity_Id;
4398 procedure Check_Body_Required;
4399 -- A unit mentioned in a limited with_clause may not be mentioned in
4400 -- a regular with_clause, but must still be included in the current
4401 -- partition. We need to determine whether the unit needs a body, so
4402 -- that the binder can determine the name of the file to be compiled.
4403 -- Checking whether a unit needs a body can be done without semantic
4404 -- analysis, by examining the nature of the declarations in the package.
4406 function Has_Limited_With_Clause
4407 (C_Unit : Entity_Id;
4408 Pack : Entity_Id) return Boolean;
4409 -- Determine whether any package in the ancestor chain starting with
4410 -- C_Unit has a limited with clause for package Pack.
4412 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4413 -- Check if some package installed though normal with-clauses has a
4414 -- renaming declaration of package P. AARM 10.1.2(21/2).
4416 -------------------------
4417 -- Check_Body_Required --
4418 -------------------------
4420 procedure Check_Body_Required is
4421 PA : constant List_Id :=
4422 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4424 procedure Check_Declarations (Spec : Node_Id);
4425 -- Recursive procedure that does the work and checks nested packages
4427 ------------------------
4428 -- Check_Declarations --
4429 ------------------------
4431 procedure Check_Declarations (Spec : Node_Id) is
4432 Decl : Node_Id;
4433 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4435 Subp_List : constant Elist_Id := New_Elmt_List;
4437 procedure Check_Pragma_Import (P : Node_Id);
4438 -- If a pragma import applies to a previous subprogram, the
4439 -- enclosing unit may not need a body. The processing is syntactic
4440 -- and does not require a declaration to be analyzed. The code
4441 -- below also handles pragma Import when applied to a subprogram
4442 -- that renames another. In this case the pragma applies to the
4443 -- renamed entity.
4445 -- Chains of multiple renames are not handled by the code below.
4446 -- It is probably impossible to handle all cases without proper
4447 -- name resolution. In such cases the algorithm is conservative
4448 -- and will indicate that a body is needed???
4450 -------------------------
4451 -- Check_Pragma_Import --
4452 -------------------------
4454 procedure Check_Pragma_Import (P : Node_Id) is
4455 Arg : Node_Id;
4456 Prev_Id : Elmt_Id;
4457 Subp_Id : Elmt_Id;
4458 Imported : Node_Id;
4460 procedure Remove_Homonyms (E : Node_Id);
4461 -- Make one pass over list of subprograms. Called again if
4462 -- subprogram is a renaming. E is known to be an identifier.
4464 ---------------------
4465 -- Remove_Homonyms --
4466 ---------------------
4468 procedure Remove_Homonyms (E : Node_Id) is
4469 R : Entity_Id := Empty;
4470 -- Name of renamed entity, if any
4472 begin
4473 Subp_Id := First_Elmt (Subp_List);
4474 while Present (Subp_Id) loop
4475 if Chars (Node (Subp_Id)) = Chars (E) then
4476 if Nkind (Parent (Parent (Node (Subp_Id))))
4477 /= N_Subprogram_Renaming_Declaration
4478 then
4479 Prev_Id := Subp_Id;
4480 Next_Elmt (Subp_Id);
4481 Remove_Elmt (Subp_List, Prev_Id);
4482 else
4483 R := Name (Parent (Parent (Node (Subp_Id))));
4484 exit;
4485 end if;
4486 else
4487 Next_Elmt (Subp_Id);
4488 end if;
4489 end loop;
4491 if Present (R) then
4492 if Nkind (R) = N_Identifier then
4493 Remove_Homonyms (R);
4495 elsif Nkind (R) = N_Selected_Component then
4496 Remove_Homonyms (Selector_Name (R));
4498 -- Renaming of attribute
4500 else
4501 null;
4502 end if;
4503 end if;
4504 end Remove_Homonyms;
4506 -- Start of processing for Check_Pragma_Import
4508 begin
4509 -- Find name of entity in Import pragma. We have not analyzed
4510 -- the construct, so we must guard against syntax errors.
4512 Arg := Next (First (Pragma_Argument_Associations (P)));
4514 if No (Arg)
4515 or else Nkind (Expression (Arg)) /= N_Identifier
4516 then
4517 return;
4518 else
4519 Imported := Expression (Arg);
4520 end if;
4522 Remove_Homonyms (Imported);
4523 end Check_Pragma_Import;
4525 -- Start of processing for Check_Declarations
4527 begin
4528 -- Search for Elaborate Body pragma
4530 Decl := First (Visible_Declarations (Spec));
4531 while Present (Decl)
4532 and then Nkind (Decl) = N_Pragma
4533 loop
4534 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4535 Set_Body_Required (Library_Unit (N));
4536 return;
4537 end if;
4539 Next (Decl);
4540 end loop;
4542 -- Look for declarations that require the presence of a body. We
4543 -- have already skipped pragmas at the start of the list.
4545 while Present (Decl) loop
4547 -- Subprogram that comes from source means body may be needed.
4548 -- Save for subsequent examination of import pragmas.
4550 if Comes_From_Source (Decl)
4551 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4552 N_Subprogram_Renaming_Declaration,
4553 N_Generic_Subprogram_Declaration))
4554 then
4555 Append_Elmt (Defining_Entity (Decl), Subp_List);
4557 -- Package declaration of generic package declaration. We need
4558 -- to recursively examine nested declarations.
4560 elsif Nkind_In (Decl, N_Package_Declaration,
4561 N_Generic_Package_Declaration)
4562 then
4563 Check_Declarations (Specification (Decl));
4565 elsif Nkind (Decl) = N_Pragma
4566 and then Pragma_Name (Decl) = Name_Import
4567 then
4568 Check_Pragma_Import (Decl);
4569 end if;
4571 Next (Decl);
4572 end loop;
4574 -- Same set of tests for private part. In addition to subprograms
4575 -- detect the presence of Taft Amendment types (incomplete types
4576 -- completed in the body).
4578 Decl := First (Private_Declarations (Spec));
4579 while Present (Decl) loop
4580 if Comes_From_Source (Decl)
4581 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4582 N_Subprogram_Renaming_Declaration,
4583 N_Generic_Subprogram_Declaration))
4584 then
4585 Append_Elmt (Defining_Entity (Decl), Subp_List);
4587 elsif Nkind_In (Decl, N_Package_Declaration,
4588 N_Generic_Package_Declaration)
4589 then
4590 Check_Declarations (Specification (Decl));
4592 -- Collect incomplete type declarations for separate pass
4594 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4595 Append_Elmt (Decl, Incomplete_Decls);
4597 elsif Nkind (Decl) = N_Pragma
4598 and then Pragma_Name (Decl) = Name_Import
4599 then
4600 Check_Pragma_Import (Decl);
4601 end if;
4603 Next (Decl);
4604 end loop;
4606 -- Now check incomplete declarations to locate Taft amendment
4607 -- types. This can be done by examining the defining identifiers
4608 -- of type declarations without real semantic analysis.
4610 declare
4611 Inc : Elmt_Id;
4613 begin
4614 Inc := First_Elmt (Incomplete_Decls);
4615 while Present (Inc) loop
4616 Decl := Next (Node (Inc));
4617 while Present (Decl) loop
4618 if Nkind (Decl) = N_Full_Type_Declaration
4619 and then Chars (Defining_Identifier (Decl)) =
4620 Chars (Defining_Identifier (Node (Inc)))
4621 then
4622 exit;
4623 end if;
4625 Next (Decl);
4626 end loop;
4628 -- If no completion, this is a TAT, and a body is needed
4630 if No (Decl) then
4631 Set_Body_Required (Library_Unit (N));
4632 return;
4633 end if;
4635 Next_Elmt (Inc);
4636 end loop;
4637 end;
4639 -- Finally, check whether there are subprograms that still require
4640 -- a body, i.e. are not renamings or null.
4642 if not Is_Empty_Elmt_List (Subp_List) then
4643 declare
4644 Subp_Id : Elmt_Id;
4645 Spec : Node_Id;
4647 begin
4648 Subp_Id := First_Elmt (Subp_List);
4649 Spec := Parent (Node (Subp_Id));
4651 while Present (Subp_Id) loop
4652 if Nkind (Parent (Spec))
4653 = N_Subprogram_Renaming_Declaration
4654 then
4655 null;
4657 elsif Nkind (Spec) = N_Procedure_Specification
4658 and then Null_Present (Spec)
4659 then
4660 null;
4662 else
4663 Set_Body_Required (Library_Unit (N));
4664 return;
4665 end if;
4667 Next_Elmt (Subp_Id);
4668 end loop;
4669 end;
4670 end if;
4671 end Check_Declarations;
4673 -- Start of processing for Check_Body_Required
4675 begin
4676 -- If this is an imported package (Java and CIL usage) no body is
4677 -- needed. Scan list of pragmas that may follow a compilation unit
4678 -- to look for a relevant pragma Import.
4680 if Present (PA) then
4681 declare
4682 Prag : Node_Id;
4684 begin
4685 Prag := First (PA);
4686 while Present (Prag) loop
4687 if Nkind (Prag) = N_Pragma
4688 and then Get_Pragma_Id (Prag) = Pragma_Import
4689 then
4690 return;
4691 end if;
4693 Next (Prag);
4694 end loop;
4695 end;
4696 end if;
4698 Check_Declarations (Specification (P_Unit));
4699 end Check_Body_Required;
4701 -----------------------------
4702 -- Has_Limited_With_Clause --
4703 -----------------------------
4705 function Has_Limited_With_Clause
4706 (C_Unit : Entity_Id;
4707 Pack : Entity_Id) return Boolean
4709 Par : Entity_Id;
4710 Par_Unit : Node_Id;
4712 begin
4713 Par := C_Unit;
4714 while Present (Par) loop
4715 if Ekind (Par) /= E_Package then
4716 exit;
4717 end if;
4719 -- Retrieve the Compilation_Unit node for Par and determine if
4720 -- its context clauses contain a limited with for Pack.
4722 Par_Unit := Parent (Parent (Parent (Par)));
4724 if Nkind (Par_Unit) = N_Package_Declaration then
4725 Par_Unit := Parent (Par_Unit);
4726 end if;
4728 if Has_With_Clause (Par_Unit, Pack, True) then
4729 return True;
4730 end if;
4732 -- If there are more ancestors, climb up the tree, otherwise we
4733 -- are done.
4735 if Is_Child_Unit (Par) then
4736 Par := Scope (Par);
4737 else
4738 exit;
4739 end if;
4740 end loop;
4742 return False;
4743 end Has_Limited_With_Clause;
4745 ----------------------------------
4746 -- Is_Visible_Through_Renamings --
4747 ----------------------------------
4749 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4750 Kind : constant Node_Kind :=
4751 Nkind (Unit (Cunit (Current_Sem_Unit)));
4752 Aux_Unit : Node_Id;
4753 Item : Node_Id;
4754 Decl : Entity_Id;
4756 begin
4757 -- Example of the error detected by this subprogram:
4759 -- package P is
4760 -- type T is ...
4761 -- end P;
4763 -- with P;
4764 -- package Q is
4765 -- package Ren_P renames P;
4766 -- end Q;
4768 -- with Q;
4769 -- package R is ...
4771 -- limited with P; -- ERROR
4772 -- package R.C is ...
4774 Aux_Unit := Cunit (Current_Sem_Unit);
4776 loop
4777 Item := First (Context_Items (Aux_Unit));
4778 while Present (Item) loop
4779 if Nkind (Item) = N_With_Clause
4780 and then not Limited_Present (Item)
4781 and then Nkind (Unit (Library_Unit (Item))) =
4782 N_Package_Declaration
4783 then
4784 Decl :=
4785 First (Visible_Declarations
4786 (Specification (Unit (Library_Unit (Item)))));
4787 while Present (Decl) loop
4788 if Nkind (Decl) = N_Package_Renaming_Declaration
4789 and then Entity (Name (Decl)) = P
4790 then
4791 -- Generate the error message only if the current unit
4792 -- is a package declaration; in case of subprogram
4793 -- bodies and package bodies we just return True to
4794 -- indicate that the limited view must not be
4795 -- installed.
4797 if Kind = N_Package_Declaration then
4798 Error_Msg_N
4799 ("simultaneous visibility of the limited and " &
4800 "unlimited views not allowed", N);
4801 Error_Msg_Sloc := Sloc (Item);
4802 Error_Msg_NE
4803 ("\\ unlimited view of & visible through the " &
4804 "context clause #", N, P);
4805 Error_Msg_Sloc := Sloc (Decl);
4806 Error_Msg_NE ("\\ and the renaming #", N, P);
4807 end if;
4809 return True;
4810 end if;
4812 Next (Decl);
4813 end loop;
4814 end if;
4816 Next (Item);
4817 end loop;
4819 -- If it is a body not acting as spec, follow pointer to the
4820 -- corresponding spec, otherwise follow pointer to parent spec.
4822 if Present (Library_Unit (Aux_Unit))
4823 and then Nkind_In (Unit (Aux_Unit),
4824 N_Package_Body, N_Subprogram_Body)
4825 then
4826 if Aux_Unit = Library_Unit (Aux_Unit) then
4828 -- Aux_Unit is a body that acts as a spec. Clause has
4829 -- already been flagged as illegal.
4831 return False;
4833 else
4834 Aux_Unit := Library_Unit (Aux_Unit);
4835 end if;
4837 else
4838 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4839 end if;
4841 exit when No (Aux_Unit);
4842 end loop;
4844 return False;
4845 end Is_Visible_Through_Renamings;
4847 -- Start of processing for Install_Limited_Withed_Unit
4849 begin
4850 pragma Assert (not Limited_View_Installed (N));
4852 -- In case of limited with_clause on subprograms, generics, instances,
4853 -- or renamings, the corresponding error was previously posted and we
4854 -- have nothing to do here. If the file is missing altogether, it has
4855 -- no source location.
4857 if Nkind (P_Unit) /= N_Package_Declaration
4858 or else Sloc (P_Unit) = No_Location
4859 then
4860 return;
4861 end if;
4863 P := Defining_Unit_Name (Specification (P_Unit));
4865 -- Handle child packages
4867 if Nkind (P) = N_Defining_Program_Unit_Name then
4868 Is_Child_Package := True;
4869 P := Defining_Identifier (P);
4870 end if;
4872 -- Do not install the limited-view if the context of the unit is already
4873 -- available through a regular with clause.
4875 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4876 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4877 then
4878 return;
4879 end if;
4881 -- Do not install the limited-view if the full-view is already visible
4882 -- through renaming declarations.
4884 if Is_Visible_Through_Renamings (P) then
4885 return;
4886 end if;
4888 -- Do not install the limited view if this is the unit being analyzed.
4889 -- This unusual case will happen when a unit has a limited_with clause
4890 -- on one of its children. The compilation of the child forces the load
4891 -- of the parent which tries to install the limited view of the child
4892 -- again. Installing the limited view must also be disabled when
4893 -- compiling the body of the child unit.
4895 if P = Cunit_Entity (Current_Sem_Unit)
4896 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4897 and then P = Main_Unit_Entity
4898 and then Is_Ancestor_Unit
4899 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
4900 then
4901 return;
4902 end if;
4904 -- This scenario is similar to the one above, the difference is that the
4905 -- compilation of sibling Par.Sib forces the load of parent Par which
4906 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4907 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4908 -- non-limited views of all entities from Lim_Pack.
4910 -- limited with Lim_Pack; -- [1]
4911 -- package Par is ... package Lim_Pack is ...
4913 -- with Lim_Pack; -- [2]
4914 -- package Par.Sib is ... package body Par.Sib is ...
4916 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4917 -- Sem_Unit is the body of Par.Sib.
4919 if Ekind (P) = E_Package
4920 and then Ekind (Main_Unit_Entity) = E_Package
4921 and then Is_Child_Unit (Main_Unit_Entity)
4923 -- The body has a regular with clause
4925 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4926 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4928 -- One of the ancestors has a limited with clause
4930 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4931 N_Package_Specification
4932 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4933 then
4934 return;
4935 end if;
4937 -- A common use of the limited-with is to have a limited-with in the
4938 -- package spec, and a normal with in its package body. For example:
4940 -- limited with X; -- [1]
4941 -- package A is ...
4943 -- with X; -- [2]
4944 -- package body A is ...
4946 -- The compilation of A's body installs the context clauses found at [2]
4947 -- and then the context clauses of its specification (found at [1]). As
4948 -- a consequence, at [1] the specification of X has been analyzed and it
4949 -- is immediately visible. According to the semantics of limited-with
4950 -- context clauses we don't install the limited view because the full
4951 -- view of X supersedes its limited view.
4953 if Analyzed (P_Unit)
4954 and then
4955 (Is_Immediately_Visible (P)
4956 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
4957 then
4959 -- The presence of both the limited and the analyzed nonlimited view
4960 -- may also be an error, such as an illegal context for a limited
4961 -- with_clause. In that case, do not process the context item at all.
4963 if Error_Posted (N) then
4964 return;
4965 end if;
4967 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
4968 declare
4969 Item : Node_Id;
4970 begin
4971 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
4972 while Present (Item) loop
4973 if Nkind (Item) = N_With_Clause
4974 and then Comes_From_Source (Item)
4975 and then Entity (Name (Item)) = P
4976 then
4977 return;
4978 end if;
4980 Next (Item);
4981 end loop;
4982 end;
4984 -- If this is a child body, assume that the nonlimited with_clause
4985 -- appears in an ancestor. Could be refined ???
4987 if Is_Child_Unit
4988 (Defining_Entity
4989 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
4990 then
4991 return;
4992 end if;
4994 else
4996 -- If in package declaration, nonlimited view brought in from
4997 -- parent unit or some error condition.
4999 return;
5000 end if;
5001 end if;
5003 if Debug_Flag_I then
5004 Write_Str ("install limited view of ");
5005 Write_Name (Chars (P));
5006 Write_Eol;
5007 end if;
5009 -- If the unit has not been analyzed and the limited view has not been
5010 -- already installed then we install it.
5012 if not Analyzed (P_Unit) then
5013 if not In_Chain (P) then
5015 -- Minimum decoration
5017 Set_Ekind (P, E_Package);
5018 Set_Etype (P, Standard_Void_Type);
5019 Set_Scope (P, Standard_Standard);
5020 Set_Is_Visible_Lib_Unit (P);
5022 if Is_Child_Package then
5023 Set_Is_Child_Unit (P);
5024 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
5025 end if;
5027 -- Place entity on visibility structure
5029 Set_Homonym (P, Current_Entity (P));
5030 Set_Current_Entity (P);
5032 if Debug_Flag_I then
5033 Write_Str (" (homonym) chain ");
5034 Write_Name (Chars (P));
5035 Write_Eol;
5036 end if;
5038 -- Install the incomplete view. The first element of the limited
5039 -- view is a header (an E_Package entity) used to reference the
5040 -- first shadow entity in the private part of the package.
5042 Lim_Header := Limited_View (P);
5043 Lim_Typ := First_Entity (Lim_Header);
5045 while Present (Lim_Typ)
5046 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5047 loop
5048 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
5049 Set_Current_Entity (Lim_Typ);
5051 if Debug_Flag_I then
5052 Write_Str (" (homonym) chain ");
5053 Write_Name (Chars (Lim_Typ));
5054 Write_Eol;
5055 end if;
5057 Next_Entity (Lim_Typ);
5058 end loop;
5059 end if;
5061 -- If the unit appears in a previous regular with_clause, the regular
5062 -- entities of the public part of the withed package must be replaced
5063 -- by the shadow ones.
5065 -- This code must be kept synchronized with the code that replaces the
5066 -- shadow entities by the real entities (see body of Remove_Limited
5067 -- With_Clause); otherwise the contents of the homonym chains are not
5068 -- consistent.
5070 else
5071 -- Hide all the type entities of the public part of the package to
5072 -- avoid its usage. This is needed to cover all the subtype decla-
5073 -- rations because we do not remove them from the homonym chain.
5075 E := First_Entity (P);
5076 while Present (E) and then E /= First_Private_Entity (P) loop
5077 if Is_Type (E) then
5078 Set_Was_Hidden (E, Is_Hidden (E));
5079 Set_Is_Hidden (E);
5080 end if;
5082 Next_Entity (E);
5083 end loop;
5085 -- Replace the real entities by the shadow entities of the limited
5086 -- view. The first element of the limited view is a header that is
5087 -- used to reference the first shadow entity in the private part
5088 -- of the package. Successive elements are the limited views of the
5089 -- type (including regular incomplete types) declared in the package.
5091 Lim_Header := Limited_View (P);
5093 Lim_Typ := First_Entity (Lim_Header);
5094 while Present (Lim_Typ)
5095 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5096 loop
5097 pragma Assert (not In_Chain (Lim_Typ));
5099 -- Do not unchain nested packages and child units
5101 if Ekind (Lim_Typ) /= E_Package
5102 and then not Is_Child_Unit (Lim_Typ)
5103 then
5104 declare
5105 Prev : Entity_Id;
5107 begin
5108 Prev := Current_Entity (Lim_Typ);
5109 E := Prev;
5111 -- Replace E in the homonyms list, so that the limited view
5112 -- becomes available.
5114 -- If the non-limited view is a record with an anonymous
5115 -- self-referential component, the analysis of the record
5116 -- declaration creates an incomplete type with the same name
5117 -- in order to define an internal access type. The visible
5118 -- entity is now the incomplete type, and that is the one to
5119 -- replace in the visibility structure.
5121 if E = Non_Limited_View (Lim_Typ)
5122 or else
5123 (Ekind (E) = E_Incomplete_Type
5124 and then Full_View (E) = Non_Limited_View (Lim_Typ))
5125 then
5126 Set_Homonym (Lim_Typ, Homonym (Prev));
5127 Set_Current_Entity (Lim_Typ);
5129 else
5130 loop
5131 E := Homonym (Prev);
5133 -- E may have been removed when installing a previous
5134 -- limited_with_clause.
5136 exit when No (E);
5137 exit when E = Non_Limited_View (Lim_Typ);
5138 Prev := Homonym (Prev);
5139 end loop;
5141 if Present (E) then
5142 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
5143 Set_Homonym (Prev, Lim_Typ);
5144 end if;
5145 end if;
5146 end;
5148 if Debug_Flag_I then
5149 Write_Str (" (homonym) chain ");
5150 Write_Name (Chars (Lim_Typ));
5151 Write_Eol;
5152 end if;
5153 end if;
5155 Next_Entity (Lim_Typ);
5156 end loop;
5157 end if;
5159 -- The package must be visible while the limited-with clause is active
5160 -- because references to the type P.T must resolve in the usual way.
5161 -- In addition, we remember that the limited-view has been installed to
5162 -- uninstall it at the point of context removal.
5164 Set_Is_Immediately_Visible (P);
5165 Set_Limited_View_Installed (N);
5167 -- If unit has not been analyzed in some previous context, check
5168 -- (imperfectly ???) whether it might need a body.
5170 if not Analyzed (P_Unit) then
5171 Check_Body_Required;
5172 end if;
5174 -- If the package in the limited_with clause is a child unit, the clause
5175 -- is unanalyzed and appears as a selected component. Recast it as an
5176 -- expanded name so that the entity can be properly set. Use entity of
5177 -- parent, if available, for higher ancestors in the name.
5179 if Nkind (Name (N)) = N_Selected_Component then
5180 declare
5181 Nam : Node_Id;
5182 Ent : Entity_Id;
5184 begin
5185 Nam := Name (N);
5186 Ent := P;
5187 while Nkind (Nam) = N_Selected_Component
5188 and then Present (Ent)
5189 loop
5190 Change_Selected_Component_To_Expanded_Name (Nam);
5192 -- Set entity of parent identifiers if the unit is a child
5193 -- unit. This ensures that the tree is properly formed from
5194 -- semantic point of view (e.g. for ASIS queries). The unit
5195 -- entities are not fully analyzed, so we need to follow unit
5196 -- links in the tree.
5198 Set_Entity (Nam, Ent);
5200 Nam := Prefix (Nam);
5201 Ent :=
5202 Defining_Entity
5203 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5205 -- Set entity of last ancestor
5207 if Nkind (Nam) = N_Identifier then
5208 Set_Entity (Nam, Ent);
5209 end if;
5210 end loop;
5211 end;
5212 end if;
5214 Set_Entity (Name (N), P);
5215 Set_From_Limited_With (P);
5216 end Install_Limited_Withed_Unit;
5218 -------------------------
5219 -- Install_Withed_Unit --
5220 -------------------------
5222 procedure Install_Withed_Unit
5223 (With_Clause : Node_Id;
5224 Private_With_OK : Boolean := False)
5226 Uname : constant Entity_Id := Entity (Name (With_Clause));
5227 P : constant Entity_Id := Scope (Uname);
5229 begin
5230 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5231 -- compiling a package declaration and the Private_With_OK flag was not
5232 -- set by the caller. These declarations will be installed later (before
5233 -- analyzing the private part of the package).
5235 if Private_Present (With_Clause)
5236 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
5237 and then not (Private_With_OK)
5238 then
5239 return;
5240 end if;
5242 if Debug_Flag_I then
5243 if Private_Present (With_Clause) then
5244 Write_Str ("install private withed unit ");
5245 else
5246 Write_Str ("install withed unit ");
5247 end if;
5249 Write_Name (Chars (Uname));
5250 Write_Eol;
5251 end if;
5253 -- We do not apply the restrictions to an internal unit unless we are
5254 -- compiling the internal unit as a main unit. This check is also
5255 -- skipped for dummy units (for missing packages).
5257 if Sloc (Uname) /= No_Location
5258 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
5259 or else Current_Sem_Unit = Main_Unit)
5260 then
5261 Check_Restricted_Unit
5262 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5263 end if;
5265 if P /= Standard_Standard then
5267 -- If the unit is not analyzed after analysis of the with clause and
5268 -- it is an instantiation then it awaits a body and is the main unit.
5269 -- Its appearance in the context of some other unit indicates a
5270 -- circular dependency (DEC suite perversity).
5272 if not Analyzed (Uname)
5273 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5274 then
5275 Error_Msg_N
5276 ("instantiation depends on itself", Name (With_Clause));
5278 elsif not Is_Visible_Lib_Unit (Uname) then
5280 -- Abandon processing in case of previous errors
5282 if No (Scope (Uname)) then
5283 Check_Error_Detected;
5284 return;
5285 end if;
5287 Set_Is_Visible_Lib_Unit (Uname);
5289 -- If the unit is a wrapper package for a compilation unit that is
5290 -- a subprogrm instance, indicate that the instance itself is a
5291 -- visible unit. This is necessary if the instance is inlined.
5293 if Is_Wrapper_Package (Uname) then
5294 Set_Is_Visible_Lib_Unit (Related_Instance (Uname));
5295 end if;
5297 -- If the child unit appears in the context of its parent, it is
5298 -- immediately visible.
5300 if In_Open_Scopes (Scope (Uname)) then
5301 Set_Is_Immediately_Visible (Uname);
5302 end if;
5304 if Is_Generic_Instance (Uname)
5305 and then Ekind (Uname) in Subprogram_Kind
5306 then
5307 -- Set flag as well on the visible entity that denotes the
5308 -- instance, which renames the current one.
5310 Set_Is_Visible_Lib_Unit
5311 (Related_Instance
5312 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5313 end if;
5315 -- The parent unit may have been installed already, and may have
5316 -- appeared in a use clause.
5318 if In_Use (Scope (Uname)) then
5319 Set_Is_Potentially_Use_Visible (Uname);
5320 end if;
5322 Set_Context_Installed (With_Clause);
5323 end if;
5325 elsif not Is_Immediately_Visible (Uname) then
5326 Set_Is_Visible_Lib_Unit (Uname);
5328 if not Private_Present (With_Clause) or else Private_With_OK then
5329 Set_Is_Immediately_Visible (Uname);
5330 end if;
5332 Set_Context_Installed (With_Clause);
5333 end if;
5335 -- A with-clause overrides a with-type clause: there are no restric-
5336 -- tions on the use of package entities.
5338 if Ekind (Uname) = E_Package then
5339 Set_From_Limited_With (Uname, False);
5340 end if;
5342 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5343 -- unit if there is a visible homograph for it declared in the same
5344 -- declarative region. This pathological case can only arise when an
5345 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5346 -- G1 has a generic child also named G2, and the context includes with_
5347 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5348 -- of I1.G2 visible as well. If the child unit is named Standard, do
5349 -- not apply the check to the Standard package itself.
5351 if Is_Child_Unit (Uname)
5352 and then Is_Visible_Lib_Unit (Uname)
5353 and then Ada_Version >= Ada_2005
5354 then
5355 declare
5356 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5357 Decl2 : Node_Id;
5358 P2 : Entity_Id;
5359 U2 : Entity_Id;
5361 begin
5362 U2 := Homonym (Uname);
5363 while Present (U2) and then U2 /= Standard_Standard loop
5364 P2 := Scope (U2);
5365 Decl2 := Unit_Declaration_Node (P2);
5367 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5368 if Is_Generic_Instance (P)
5369 and then Nkind (Decl1) = N_Package_Declaration
5370 and then Generic_Parent (Specification (Decl1)) = P2
5371 then
5372 Error_Msg_N ("illegal with_clause", With_Clause);
5373 Error_Msg_N
5374 ("\child unit has visible homograph" &
5375 " (RM 8.3(26), 10.1.1(19))",
5376 With_Clause);
5377 exit;
5379 elsif Is_Generic_Instance (P2)
5380 and then Nkind (Decl2) = N_Package_Declaration
5381 and then Generic_Parent (Specification (Decl2)) = P
5382 then
5383 -- With_clause for child unit of instance appears before
5384 -- in the context. We want to place the error message on
5385 -- it, not on the generic child unit itself.
5387 declare
5388 Prev_Clause : Node_Id;
5390 begin
5391 Prev_Clause := First (List_Containing (With_Clause));
5392 while Entity (Name (Prev_Clause)) /= U2 loop
5393 Next (Prev_Clause);
5394 end loop;
5396 pragma Assert (Present (Prev_Clause));
5397 Error_Msg_N ("illegal with_clause", Prev_Clause);
5398 Error_Msg_N
5399 ("\child unit has visible homograph" &
5400 " (RM 8.3(26), 10.1.1(19))",
5401 Prev_Clause);
5402 exit;
5403 end;
5404 end if;
5405 end if;
5407 U2 := Homonym (U2);
5408 end loop;
5409 end;
5410 end if;
5411 end Install_Withed_Unit;
5413 -------------------
5414 -- Is_Child_Spec --
5415 -------------------
5417 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5418 K : constant Node_Kind := Nkind (Lib_Unit);
5420 begin
5421 return (K in N_Generic_Declaration or else
5422 K in N_Generic_Instantiation or else
5423 K in N_Generic_Renaming_Declaration or else
5424 K = N_Package_Declaration or else
5425 K = N_Package_Renaming_Declaration or else
5426 K = N_Subprogram_Declaration or else
5427 K = N_Subprogram_Renaming_Declaration)
5428 and then Present (Parent_Spec (Lib_Unit));
5429 end Is_Child_Spec;
5431 ------------------------------------
5432 -- Is_Legal_Shadow_Entity_In_Body --
5433 ------------------------------------
5435 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5436 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5437 begin
5438 return Nkind (Unit (C_Unit)) = N_Package_Body
5439 and then
5440 Has_With_Clause
5441 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5442 end Is_Legal_Shadow_Entity_In_Body;
5444 ----------------------
5445 -- Is_Ancestor_Unit --
5446 ----------------------
5448 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5449 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5450 E2 : Entity_Id;
5451 begin
5452 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
5453 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5454 return Is_Ancestor_Package (E1, E2);
5455 else
5456 return False;
5457 end if;
5458 end Is_Ancestor_Unit;
5460 -----------------------
5461 -- Load_Needed_Body --
5462 -----------------------
5464 -- N is a generic unit named in a with clause, or else it is a unit that
5465 -- contains a generic unit or an inlined function. In order to perform an
5466 -- instantiation, the body of the unit must be present. If the unit itself
5467 -- is generic, we assume that an instantiation follows, and load & analyze
5468 -- the body unconditionally. This forces analysis of the spec as well.
5470 -- If the unit is not generic, but contains a generic unit, it is loaded on
5471 -- demand, at the point of instantiation (see ch12).
5473 procedure Load_Needed_Body
5474 (N : Node_Id;
5475 OK : out Boolean;
5476 Do_Analyze : Boolean := True)
5478 Body_Name : Unit_Name_Type;
5479 Unum : Unit_Number_Type;
5481 Save_Style_Check : constant Boolean := Opt.Style_Check;
5482 -- The loading and analysis is done with style checks off
5484 begin
5485 if not GNAT_Mode then
5486 Style_Check := False;
5487 end if;
5489 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5490 Unum :=
5491 Load_Unit
5492 (Load_Name => Body_Name,
5493 Required => False,
5494 Subunit => False,
5495 Error_Node => N,
5496 Renamings => True);
5498 if Unum = No_Unit then
5499 OK := False;
5501 else
5502 Compiler_State := Analyzing; -- reset after load
5504 if Fatal_Error (Unum) /= Error_Detected or else Try_Semantics then
5505 if Debug_Flag_L then
5506 Write_Str ("*** Loaded generic body");
5507 Write_Eol;
5508 end if;
5510 if Do_Analyze then
5511 Semantics (Cunit (Unum));
5512 end if;
5513 end if;
5515 OK := True;
5516 end if;
5518 Style_Check := Save_Style_Check;
5519 end Load_Needed_Body;
5521 -------------------------
5522 -- Build_Limited_Views --
5523 -------------------------
5525 procedure Build_Limited_Views (N : Node_Id) is
5526 Unum : constant Unit_Number_Type :=
5527 Get_Source_Unit (Library_Unit (N));
5528 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
5530 Shadow_Pack : Entity_Id;
5531 -- The corresponding shadow entity of the withed package. This entity
5532 -- offers incomplete views of packages and types as well as abstract
5533 -- views of states and variables declared within.
5535 Last_Shadow : Entity_Id := Empty;
5536 -- The last shadow entity created by routine Build_Shadow_Entity
5538 procedure Build_Shadow_Entity
5539 (Ent : Entity_Id;
5540 Scop : Entity_Id;
5541 Shadow : out Entity_Id;
5542 Is_Tagged : Boolean := False);
5543 -- Create a shadow entity that hides Ent and offers an abstract or
5544 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5545 -- should be set when Ent is a tagged type. The generated entity is
5546 -- added to Lim_Header. This routine updates the value of Last_Shadow.
5548 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
5549 -- Perform minimal decoration of a package or its corresponding shadow
5550 -- entity denoted by Ent. Scop is the proper scope.
5552 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id);
5553 -- Perform full decoration of an abstract state or its corresponding
5554 -- shadow entity denoted by Ent. Scop is the proper scope.
5556 procedure Decorate_Type
5557 (Ent : Entity_Id;
5558 Scop : Entity_Id;
5559 Is_Tagged : Boolean := False;
5560 Materialize : Boolean := False);
5561 -- Perform minimal decoration of a type or its corresponding shadow
5562 -- entity denoted by Ent. Scop is the proper scope. Flag Is_Tagged
5563 -- should be set when Ent is a tagged type. Flag Materialize should be
5564 -- set when Ent is a tagged type and its class-wide type needs to appear
5565 -- in the tree.
5567 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id);
5568 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5569 -- proper scope.
5571 procedure Process_Declarations_And_States
5572 (Pack : Entity_Id;
5573 Decls : List_Id;
5574 Scop : Entity_Id;
5575 Create_Abstract_Views : Boolean);
5576 -- Inspect the states of package Pack and declarative list Decls. Create
5577 -- shadow entities for all nested packages, states, types and variables
5578 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5579 -- be set when the abstract states and variables need to be processed.
5581 -------------------------
5582 -- Build_Shadow_Entity --
5583 -------------------------
5585 procedure Build_Shadow_Entity
5586 (Ent : Entity_Id;
5587 Scop : Entity_Id;
5588 Shadow : out Entity_Id;
5589 Is_Tagged : Boolean := False)
5591 begin
5592 Shadow := Make_Temporary (Sloc (Ent), 'Z');
5594 -- The shadow entity must share the same name and parent as the
5595 -- entity it hides.
5597 Set_Chars (Shadow, Chars (Ent));
5598 Set_Parent (Shadow, Parent (Ent));
5600 -- The abstract view of a variable is a state, not another variable
5602 if Ekind (Ent) = E_Variable then
5603 Set_Ekind (Shadow, E_Abstract_State);
5604 else
5605 Set_Ekind (Shadow, Ekind (Ent));
5606 end if;
5608 Set_Is_Internal (Shadow);
5609 Set_From_Limited_With (Shadow);
5611 -- Add the new shadow entity to the limited view of the package
5613 Last_Shadow := Shadow;
5614 Append_Entity (Shadow, Shadow_Pack);
5616 -- Perform context-specific decoration of the shadow entity
5618 if Ekind (Ent) = E_Abstract_State then
5619 Decorate_State (Shadow, Scop);
5620 Set_Non_Limited_View (Shadow, Ent);
5622 elsif Ekind (Ent) = E_Package then
5623 Decorate_Package (Shadow, Scop);
5625 elsif Is_Type (Ent) then
5626 Decorate_Type (Shadow, Scop, Is_Tagged);
5627 Set_Non_Limited_View (Shadow, Ent);
5629 if Is_Tagged then
5630 Set_Non_Limited_View
5631 (Class_Wide_Type (Shadow), Class_Wide_Type (Ent));
5632 end if;
5634 if Is_Incomplete_Or_Private_Type (Ent) then
5635 Set_Private_Dependents (Shadow, New_Elmt_List);
5636 end if;
5638 elsif Ekind (Ent) = E_Variable then
5639 Decorate_State (Shadow, Scop);
5640 Set_Non_Limited_View (Shadow, Ent);
5641 end if;
5642 end Build_Shadow_Entity;
5644 ----------------------
5645 -- Decorate_Package --
5646 ----------------------
5648 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
5649 begin
5650 Set_Ekind (Ent, E_Package);
5651 Set_Etype (Ent, Standard_Void_Type);
5652 Set_Scope (Ent, Scop);
5653 end Decorate_Package;
5655 --------------------
5656 -- Decorate_State --
5657 --------------------
5659 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id) is
5660 begin
5661 Set_Ekind (Ent, E_Abstract_State);
5662 Set_Etype (Ent, Standard_Void_Type);
5663 Set_Scope (Ent, Scop);
5664 Set_Encapsulating_State (Ent, Empty);
5665 end Decorate_State;
5667 -------------------
5668 -- Decorate_Type --
5669 -------------------
5671 procedure Decorate_Type
5672 (Ent : Entity_Id;
5673 Scop : Entity_Id;
5674 Is_Tagged : Boolean := False;
5675 Materialize : Boolean := False)
5677 CW_Typ : Entity_Id;
5679 begin
5680 -- An unanalyzed type or a shadow entity of a type is treated as an
5681 -- incomplete type, and carries the corresponding attributes.
5683 Set_Ekind (Ent, E_Incomplete_Type);
5684 Set_Etype (Ent, Ent);
5685 Set_Full_View (Ent, Empty);
5686 Set_Is_First_Subtype (Ent);
5687 Set_Scope (Ent, Scop);
5688 Set_Stored_Constraint (Ent, No_Elist);
5689 Init_Size_Align (Ent);
5691 if From_Limited_With (Ent) then
5692 Set_Private_Dependents (Ent, New_Elmt_List);
5693 end if;
5695 -- A tagged type and its corresponding shadow entity share one common
5696 -- class-wide type. The list of primitive operations for the shadow
5697 -- entity is empty.
5699 if Is_Tagged then
5700 Set_Is_Tagged_Type (Ent);
5701 Set_Direct_Primitive_Operations (Ent, New_Elmt_List);
5703 CW_Typ :=
5704 New_External_Entity
5705 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
5707 Set_Class_Wide_Type (Ent, CW_Typ);
5709 -- Set parent to be the same as the parent of the tagged type.
5710 -- We need a parent field set, and it is supposed to point to
5711 -- the declaration of the type. The tagged type declaration
5712 -- essentially declares two separate types, the tagged type
5713 -- itself and the corresponding class-wide type, so it is
5714 -- reasonable for the parent fields to point to the declaration
5715 -- in both cases.
5717 Set_Parent (CW_Typ, Parent (Ent));
5719 Set_Ekind (CW_Typ, E_Class_Wide_Type);
5720 Set_Class_Wide_Type (CW_Typ, CW_Typ);
5721 Set_Etype (CW_Typ, Ent);
5722 Set_Equivalent_Type (CW_Typ, Empty);
5723 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
5724 Set_Has_Unknown_Discriminants (CW_Typ);
5725 Set_Is_First_Subtype (CW_Typ);
5726 Set_Is_Tagged_Type (CW_Typ);
5727 Set_Materialize_Entity (CW_Typ, Materialize);
5728 Set_Scope (CW_Typ, Scop);
5729 Init_Size_Align (CW_Typ);
5730 end if;
5731 end Decorate_Type;
5733 -----------------------
5734 -- Decorate_Variable --
5735 -----------------------
5737 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id) is
5738 begin
5739 Set_Ekind (Ent, E_Variable);
5740 Set_Etype (Ent, Standard_Void_Type);
5741 Set_Scope (Ent, Scop);
5742 end Decorate_Variable;
5744 -------------------------------------
5745 -- Process_Declarations_And_States --
5746 -------------------------------------
5748 procedure Process_Declarations_And_States
5749 (Pack : Entity_Id;
5750 Decls : List_Id;
5751 Scop : Entity_Id;
5752 Create_Abstract_Views : Boolean)
5754 procedure Find_And_Process_States;
5755 -- Determine whether package Pack defines abstract state either by
5756 -- using an aspect or a pragma. If this is the case, build shadow
5757 -- entities for all abstract states of Pack.
5759 procedure Process_States (States : Elist_Id);
5760 -- Generate shadow entities for all abstract states in list States
5762 -----------------------------
5763 -- Find_And_Process_States --
5764 -----------------------------
5766 procedure Find_And_Process_States is
5767 procedure Process_State (State : Node_Id);
5768 -- Generate shadow entities for a single abstract state or
5769 -- multiple states expressed as an aggregate.
5771 -------------------
5772 -- Process_State --
5773 -------------------
5775 procedure Process_State (State : Node_Id) is
5776 Loc : constant Source_Ptr := Sloc (State);
5777 Decl : Node_Id;
5778 Dummy : Entity_Id;
5779 Elmt : Node_Id;
5780 Id : Entity_Id;
5782 begin
5783 -- Multiple abstract states appear as an aggregate
5785 if Nkind (State) = N_Aggregate then
5786 Elmt := First (Expressions (State));
5787 while Present (Elmt) loop
5788 Process_State (Elmt);
5789 Next (Elmt);
5790 end loop;
5792 return;
5794 -- A null state has no abstract view
5796 elsif Nkind (State) = N_Null then
5797 return;
5799 -- State declaration with various options appears as an
5800 -- extension aggregate.
5802 elsif Nkind (State) = N_Extension_Aggregate then
5803 Decl := Ancestor_Part (State);
5805 -- Simple state declaration
5807 elsif Nkind (State) = N_Identifier then
5808 Decl := State;
5810 -- Possibly an illegal state declaration
5812 else
5813 return;
5814 end if;
5816 -- Abstract states are elaborated when the related pragma is
5817 -- elaborated. Since the withed package is not analyzed yet,
5818 -- the entities of the abstract states are not available. To
5819 -- overcome this complication, create the entities now and
5820 -- store them in their respective declarations. The entities
5821 -- are later used by routine Create_Abstract_State to declare
5822 -- and enter the states into visibility.
5824 if No (Entity (Decl)) then
5825 Id := Make_Defining_Identifier (Loc, Chars (Decl));
5827 Set_Entity (Decl, Id);
5828 Set_Parent (Id, State);
5829 Decorate_State (Id, Scop);
5831 -- Otherwise the package was previously withed
5833 else
5834 Id := Entity (Decl);
5835 end if;
5837 Build_Shadow_Entity (Id, Scop, Dummy);
5838 end Process_State;
5840 -- Local variables
5842 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack);
5843 Asp : Node_Id;
5844 Decl : Node_Id;
5846 -- Start of processing for Find_And_Process_States
5848 begin
5849 -- Find aspect Abstract_State
5851 Asp := First (Aspect_Specifications (Pack_Decl));
5852 while Present (Asp) loop
5853 if Chars (Identifier (Asp)) = Name_Abstract_State then
5854 Process_State (Expression (Asp));
5856 return;
5857 end if;
5859 Next (Asp);
5860 end loop;
5862 -- Find pragma Abstract_State by inspecting the declarations
5864 Decl := First (Decls);
5865 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
5866 if Pragma_Name (Decl) = Name_Abstract_State then
5867 Process_State
5868 (Get_Pragma_Arg
5869 (First (Pragma_Argument_Associations (Decl))));
5871 return;
5872 end if;
5874 Next (Decl);
5875 end loop;
5876 end Find_And_Process_States;
5878 --------------------
5879 -- Process_States --
5880 --------------------
5882 procedure Process_States (States : Elist_Id) is
5883 Dummy : Entity_Id;
5884 Elmt : Elmt_Id;
5886 begin
5887 Elmt := First_Elmt (States);
5888 while Present (Elmt) loop
5889 Build_Shadow_Entity (Node (Elmt), Scop, Dummy);
5891 Next_Elmt (Elmt);
5892 end loop;
5893 end Process_States;
5895 -- Local variables
5897 Is_Tagged : Boolean;
5898 Decl : Node_Id;
5899 Def : Node_Id;
5900 Def_Id : Entity_Id;
5901 Shadow : Entity_Id;
5903 -- Start of processing for Process_Declarations_And_States
5905 begin
5906 -- Build abstract views for all states defined in the package
5908 if Create_Abstract_Views then
5910 -- When a package has been analyzed, all states are stored in list
5911 -- Abstract_States. Generate the shadow entities directly.
5913 if Is_Analyzed then
5914 if Present (Abstract_States (Pack)) then
5915 Process_States (Abstract_States (Pack));
5916 end if;
5918 -- The package may declare abstract states by using an aspect or a
5919 -- pragma. Attempt to locate one of these construct and if found,
5920 -- build the shadow entities.
5922 else
5923 Find_And_Process_States;
5924 end if;
5925 end if;
5927 -- Inspect the declarative list, looking for nested packages, types
5928 -- and variable declarations.
5930 Decl := First (Decls);
5931 while Present (Decl) loop
5933 -- Packages
5935 if Nkind (Decl) = N_Package_Declaration then
5936 Def_Id := Defining_Entity (Decl);
5938 -- Perform minor decoration when the withed package has not
5939 -- been analyzed.
5941 if not Is_Analyzed then
5942 Decorate_Package (Def_Id, Scop);
5943 end if;
5945 -- Create a shadow entity that offers a limited view of all
5946 -- visible types declared within.
5948 Build_Shadow_Entity (Def_Id, Scop, Shadow);
5950 Process_Declarations_And_States
5951 (Pack => Def_Id,
5952 Decls => Visible_Declarations (Specification (Decl)),
5953 Scop => Shadow,
5954 Create_Abstract_Views => Create_Abstract_Views);
5956 -- Types
5958 elsif Nkind_In (Decl, N_Full_Type_Declaration,
5959 N_Incomplete_Type_Declaration,
5960 N_Private_Extension_Declaration,
5961 N_Private_Type_Declaration,
5962 N_Protected_Type_Declaration,
5963 N_Task_Type_Declaration)
5964 then
5965 Def_Id := Defining_Entity (Decl);
5967 -- Determine whether the type is tagged. Note that packages
5968 -- included via a limited with clause are not always analyzed,
5969 -- hence the tree lookup rather than the use of attribute
5970 -- Is_Tagged_Type.
5972 if Nkind (Decl) = N_Full_Type_Declaration then
5973 Def := Type_Definition (Decl);
5975 Is_Tagged :=
5976 (Nkind (Def) = N_Record_Definition
5977 and then Tagged_Present (Def))
5978 or else
5979 (Nkind (Def) = N_Derived_Type_Definition
5980 and then Present (Record_Extension_Part (Def)));
5982 elsif Nkind_In (Decl, N_Incomplete_Type_Declaration,
5983 N_Private_Type_Declaration)
5984 then
5985 Is_Tagged := Tagged_Present (Decl);
5987 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5988 Is_Tagged := True;
5990 else
5991 Is_Tagged := False;
5992 end if;
5994 -- Perform minor decoration when the withed package has not
5995 -- been analyzed.
5997 if not Is_Analyzed then
5998 Decorate_Type (Def_Id, Scop, Is_Tagged, True);
5999 end if;
6001 -- Create a shadow entity that hides the type and offers an
6002 -- incomplete view of the said type.
6004 Build_Shadow_Entity (Def_Id, Scop, Shadow, Is_Tagged);
6006 -- Variables
6008 elsif Create_Abstract_Views
6009 and then Nkind (Decl) = N_Object_Declaration
6010 and then not Constant_Present (Decl)
6011 then
6012 Def_Id := Defining_Entity (Decl);
6014 -- Perform minor decoration when the withed package has not
6015 -- been analyzed.
6017 if not Is_Analyzed then
6018 Decorate_Variable (Def_Id, Scop);
6019 end if;
6021 -- Create a shadow entity that hides the variable and offers an
6022 -- abstract view of the said variable.
6024 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6025 end if;
6027 Next (Decl);
6028 end loop;
6029 end Process_Declarations_And_States;
6031 -- Local variables
6033 Nam : constant Node_Id := Name (N);
6034 Pack : constant Entity_Id := Cunit_Entity (Unum);
6036 Last_Public_Shadow : Entity_Id := Empty;
6037 Private_Shadow : Entity_Id;
6038 Spec : Node_Id;
6040 -- Start of processing for Build_Limited_Views
6042 begin
6043 pragma Assert (Limited_Present (N));
6045 -- A library_item mentioned in a limited_with_clause is a package
6046 -- declaration, not a subprogram declaration, generic declaration,
6047 -- generic instantiation, or package renaming declaration.
6049 case Nkind (Unit (Library_Unit (N))) is
6050 when N_Package_Declaration =>
6051 null;
6053 when N_Subprogram_Declaration =>
6054 Error_Msg_N ("subprograms not allowed in limited with_clauses", N);
6055 return;
6057 when N_Generic_Package_Declaration
6058 | N_Generic_Subprogram_Declaration
6060 Error_Msg_N ("generics not allowed in limited with_clauses", N);
6061 return;
6063 when N_Generic_Instantiation =>
6064 Error_Msg_N
6065 ("generic instantiations not allowed in limited with_clauses",
6067 return;
6069 when N_Generic_Renaming_Declaration =>
6070 Error_Msg_N
6071 ("generic renamings not allowed in limited with_clauses", N);
6072 return;
6074 when N_Subprogram_Renaming_Declaration =>
6075 Error_Msg_N
6076 ("renamed subprograms not allowed in limited with_clauses", N);
6077 return;
6079 when N_Package_Renaming_Declaration =>
6080 Error_Msg_N
6081 ("renamed packages not allowed in limited with_clauses", N);
6082 return;
6084 when others =>
6085 raise Program_Error;
6086 end case;
6088 -- The withed unit may not be analyzed, but the with calause itself
6089 -- must be minimally decorated. This ensures that the checks on unused
6090 -- with clauses also process limieted withs.
6092 Set_Ekind (Pack, E_Package);
6093 Set_Etype (Pack, Standard_Void_Type);
6095 if Is_Entity_Name (Nam) then
6096 Set_Entity (Nam, Pack);
6098 elsif Nkind (Nam) = N_Selected_Component then
6099 Set_Entity (Selector_Name (Nam), Pack);
6100 end if;
6102 -- Check if the chain is already built
6104 Spec := Specification (Unit (Library_Unit (N)));
6106 if Limited_View_Installed (Spec) then
6107 return;
6108 end if;
6110 -- Create the shadow package wich hides the withed unit and provides
6111 -- incomplete view of all types and packages declared within.
6113 Shadow_Pack := Make_Temporary (Sloc (N), 'Z');
6114 Set_Ekind (Shadow_Pack, E_Package);
6115 Set_Is_Internal (Shadow_Pack);
6116 Set_Limited_View (Pack, Shadow_Pack);
6118 -- Inspect the abstract states and visible declarations of the withed
6119 -- unit and create shadow entities that hide existing packages, states,
6120 -- variables and types.
6122 Process_Declarations_And_States
6123 (Pack => Pack,
6124 Decls => Visible_Declarations (Spec),
6125 Scop => Pack,
6126 Create_Abstract_Views => True);
6128 Last_Public_Shadow := Last_Shadow;
6130 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6131 -- to accomodate limited private with clauses.
6133 Process_Declarations_And_States
6134 (Pack => Pack,
6135 Decls => Private_Declarations (Spec),
6136 Scop => Pack,
6137 Create_Abstract_Views => False);
6139 if Present (Last_Public_Shadow) then
6140 Private_Shadow := Next_Entity (Last_Public_Shadow);
6141 else
6142 Private_Shadow := First_Entity (Shadow_Pack);
6143 end if;
6145 Set_First_Private_Entity (Shadow_Pack, Private_Shadow);
6146 Set_Limited_View_Installed (Spec);
6147 end Build_Limited_Views;
6149 ----------------------------
6150 -- Check_No_Elab_Code_All --
6151 ----------------------------
6153 procedure Check_No_Elab_Code_All (N : Node_Id) is
6154 begin
6155 if Present (No_Elab_Code_All_Pragma)
6156 and then In_Extended_Main_Source_Unit (N)
6157 and then Present (Context_Items (N))
6158 then
6159 declare
6160 CL : constant List_Id := Context_Items (N);
6161 CI : Node_Id;
6163 begin
6164 CI := First (CL);
6165 while Present (CI) loop
6166 if Nkind (CI) = N_With_Clause
6167 and then not
6168 No_Elab_Code_All (Get_Source_Unit (Library_Unit (CI)))
6170 -- In GNATprove mode, some runtime units are implicitly
6171 -- loaded to make their entities available for analysis. In
6172 -- this case, ignore violations of No_Elaboration_Code_All
6173 -- for this special analysis mode.
6175 and then not
6176 (GNATprove_Mode and then Implicit_With (CI))
6177 then
6178 Error_Msg_Sloc := Sloc (No_Elab_Code_All_Pragma);
6179 Error_Msg_N
6180 ("violation of No_Elaboration_Code_All#", CI);
6181 Error_Msg_NE
6182 ("\unit& does not have No_Elaboration_Code_All",
6183 CI, Entity (Name (CI)));
6184 end if;
6186 Next (CI);
6187 end loop;
6188 end;
6189 end if;
6190 end Check_No_Elab_Code_All;
6192 -------------------------------
6193 -- Check_Body_Needed_For_SAL --
6194 -------------------------------
6196 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
6197 function Entity_Needs_Body (E : Entity_Id) return Boolean;
6198 -- Determine whether use of entity E might require the presence of its
6199 -- body. For a package this requires a recursive traversal of all nested
6200 -- declarations.
6202 -----------------------
6203 -- Entity_Needs_Body --
6204 -----------------------
6206 function Entity_Needs_Body (E : Entity_Id) return Boolean is
6207 Ent : Entity_Id;
6209 begin
6210 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
6211 return True;
6213 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
6215 -- A generic subprogram always requires the presence of its
6216 -- body because an instantiation needs both templates. The only
6217 -- exceptions is a generic subprogram renaming. In this case the
6218 -- body is needed only when the template is declared outside the
6219 -- compilation unit being checked.
6221 if Present (Renamed_Entity (E)) then
6222 return not Within_Scope (E, Unit_Name);
6223 else
6224 return True;
6225 end if;
6227 elsif Ekind (E) = E_Generic_Package
6228 and then
6229 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
6230 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6231 then
6232 return True;
6234 elsif Ekind (E) = E_Package
6235 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
6236 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6237 then
6238 Ent := First_Entity (E);
6239 while Present (Ent) loop
6240 if Entity_Needs_Body (Ent) then
6241 return True;
6242 end if;
6244 Next_Entity (Ent);
6245 end loop;
6247 return False;
6249 else
6250 return False;
6251 end if;
6252 end Entity_Needs_Body;
6254 -- Start of processing for Check_Body_Needed_For_SAL
6256 begin
6257 if Ekind (Unit_Name) = E_Generic_Package
6258 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6259 N_Generic_Package_Declaration
6260 and then
6261 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
6262 then
6263 Set_Body_Needed_For_SAL (Unit_Name);
6265 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
6266 Set_Body_Needed_For_SAL (Unit_Name);
6268 elsif Is_Subprogram (Unit_Name)
6269 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6270 N_Subprogram_Declaration
6271 and then Has_Pragma_Inline (Unit_Name)
6272 then
6273 Set_Body_Needed_For_SAL (Unit_Name);
6275 elsif Ekind (Unit_Name) = E_Subprogram_Body then
6276 Check_Body_Needed_For_SAL
6277 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6279 elsif Ekind (Unit_Name) = E_Package
6280 and then Entity_Needs_Body (Unit_Name)
6281 then
6282 Set_Body_Needed_For_SAL (Unit_Name);
6284 elsif Ekind (Unit_Name) = E_Package_Body
6285 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
6286 then
6287 Check_Body_Needed_For_SAL
6288 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6289 end if;
6290 end Check_Body_Needed_For_SAL;
6292 --------------------
6293 -- Remove_Context --
6294 --------------------
6296 procedure Remove_Context (N : Node_Id) is
6297 Lib_Unit : constant Node_Id := Unit (N);
6299 begin
6300 -- If this is a child unit, first remove the parent units
6302 if Is_Child_Spec (Lib_Unit) then
6303 Remove_Parents (Lib_Unit);
6304 end if;
6306 Remove_Context_Clauses (N);
6307 end Remove_Context;
6309 ----------------------------
6310 -- Remove_Context_Clauses --
6311 ----------------------------
6313 procedure Remove_Context_Clauses (N : Node_Id) is
6314 Item : Node_Id;
6315 Unit_Name : Entity_Id;
6317 begin
6318 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6319 -- limited-views first and regular-views later (to maintain the
6320 -- stack model).
6322 -- First Phase: Remove limited_with context clauses
6324 Item := First (Context_Items (N));
6325 while Present (Item) loop
6327 -- We are interested only in with clauses which got installed
6328 -- on entry.
6330 if Nkind (Item) = N_With_Clause
6331 and then Limited_Present (Item)
6332 and then Limited_View_Installed (Item)
6333 then
6334 Remove_Limited_With_Clause (Item);
6335 end if;
6337 Next (Item);
6338 end loop;
6340 -- Second Phase: Loop through context items and undo regular
6341 -- with_clauses and use_clauses.
6343 Item := First (Context_Items (N));
6344 while Present (Item) loop
6346 -- We are interested only in with clauses which got installed on
6347 -- entry, as indicated by their Context_Installed flag set
6349 if Nkind (Item) = N_With_Clause
6350 and then Limited_Present (Item)
6351 and then Limited_View_Installed (Item)
6352 then
6353 null;
6355 elsif Nkind (Item) = N_With_Clause
6356 and then Context_Installed (Item)
6357 then
6358 -- Remove items from one with'ed unit
6360 Unit_Name := Entity (Name (Item));
6361 Remove_Unit_From_Visibility (Unit_Name);
6362 Set_Context_Installed (Item, False);
6364 elsif Nkind (Item) = N_Use_Package_Clause then
6365 End_Use_Package (Item);
6367 elsif Nkind (Item) = N_Use_Type_Clause then
6368 End_Use_Type (Item);
6369 end if;
6371 Next (Item);
6372 end loop;
6373 end Remove_Context_Clauses;
6375 --------------------------------
6376 -- Remove_Limited_With_Clause --
6377 --------------------------------
6379 procedure Remove_Limited_With_Clause (N : Node_Id) is
6380 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
6381 E : Entity_Id;
6382 P : Entity_Id;
6383 Lim_Header : Entity_Id;
6384 Lim_Typ : Entity_Id;
6385 Prev : Entity_Id;
6387 begin
6388 pragma Assert (Limited_View_Installed (N));
6390 -- In case of limited with_clause on subprograms, generics, instances,
6391 -- or renamings, the corresponding error was previously posted and we
6392 -- have nothing to do here.
6394 if Nkind (P_Unit) /= N_Package_Declaration then
6395 return;
6396 end if;
6398 P := Defining_Unit_Name (Specification (P_Unit));
6400 -- Handle child packages
6402 if Nkind (P) = N_Defining_Program_Unit_Name then
6403 P := Defining_Identifier (P);
6404 end if;
6406 if Debug_Flag_I then
6407 Write_Str ("remove limited view of ");
6408 Write_Name (Chars (P));
6409 Write_Str (" from visibility");
6410 Write_Eol;
6411 end if;
6413 -- Prepare the removal of the shadow entities from visibility. The first
6414 -- element of the limited view is a header (an E_Package entity) that is
6415 -- used to reference the first shadow entity in the private part of the
6416 -- package
6418 Lim_Header := Limited_View (P);
6419 Lim_Typ := First_Entity (Lim_Header);
6421 -- Remove package and shadow entities from visibility if it has not
6422 -- been analyzed
6424 if not Analyzed (P_Unit) then
6425 Unchain (P);
6426 Set_Is_Immediately_Visible (P, False);
6428 while Present (Lim_Typ) loop
6429 Unchain (Lim_Typ);
6430 Next_Entity (Lim_Typ);
6431 end loop;
6433 -- Otherwise this package has already appeared in the closure and its
6434 -- shadow entities must be replaced by its real entities. This code
6435 -- must be kept synchronized with the complementary code in Install
6436 -- Limited_Withed_Unit.
6438 else
6439 -- If the limited_with_clause is in some other unit in the context
6440 -- then it is not visible in the main unit.
6442 if not In_Extended_Main_Source_Unit (N) then
6443 Set_Is_Immediately_Visible (P, False);
6444 end if;
6446 -- Real entities that are type or subtype declarations were hidden
6447 -- from visibility at the point of installation of the limited-view.
6448 -- Now we recover the previous value of the hidden attribute.
6450 E := First_Entity (P);
6451 while Present (E) and then E /= First_Private_Entity (P) loop
6452 if Is_Type (E) then
6453 Set_Is_Hidden (E, Was_Hidden (E));
6454 end if;
6456 Next_Entity (E);
6457 end loop;
6459 while Present (Lim_Typ)
6460 and then Lim_Typ /= First_Private_Entity (Lim_Header)
6461 loop
6462 -- Nested packages and child units were not unchained
6464 if Ekind (Lim_Typ) /= E_Package
6465 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
6466 then
6467 -- If the package has incomplete types, the limited view of the
6468 -- incomplete type is in fact never visible (AI05-129) but we
6469 -- have created a shadow entity E1 for it, that points to E2,
6470 -- a non-limited incomplete type. This in turn has a full view
6471 -- E3 that is the full declaration. There is a corresponding
6472 -- shadow entity E4. When reinstalling the non-limited view,
6473 -- E2 must become the current entity and E3 must be ignored.
6475 E := Non_Limited_View (Lim_Typ);
6477 if Present (Current_Entity (E))
6478 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
6479 and then Full_View (Current_Entity (E)) = E
6480 then
6482 -- Lim_Typ is the limited view of a full type declaration
6483 -- that has a previous incomplete declaration, i.e. E3 from
6484 -- the previous description. Nothing to insert.
6486 null;
6488 else
6489 pragma Assert (not In_Chain (E));
6491 Prev := Current_Entity (Lim_Typ);
6493 if Prev = Lim_Typ then
6494 Set_Current_Entity (E);
6496 else
6497 while Present (Prev)
6498 and then Homonym (Prev) /= Lim_Typ
6499 loop
6500 Prev := Homonym (Prev);
6501 end loop;
6503 if Present (Prev) then
6504 Set_Homonym (Prev, E);
6505 end if;
6506 end if;
6508 -- Preserve structure of homonym chain
6510 Set_Homonym (E, Homonym (Lim_Typ));
6511 end if;
6512 end if;
6514 Next_Entity (Lim_Typ);
6515 end loop;
6516 end if;
6518 -- Indicate that the limited view of the package is not installed
6520 Set_From_Limited_With (P, False);
6521 Set_Limited_View_Installed (N, False);
6522 end Remove_Limited_With_Clause;
6524 --------------------
6525 -- Remove_Parents --
6526 --------------------
6528 procedure Remove_Parents (Lib_Unit : Node_Id) is
6529 P : Node_Id;
6530 P_Name : Entity_Id;
6531 P_Spec : Node_Id := Empty;
6532 E : Entity_Id;
6533 Vis : constant Boolean :=
6534 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
6536 begin
6537 if Is_Child_Spec (Lib_Unit) then
6538 P_Spec := Parent_Spec (Lib_Unit);
6540 elsif Nkind (Lib_Unit) = N_Package_Body
6541 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6542 then
6543 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6544 end if;
6546 if Present (P_Spec) then
6547 P := Unit (P_Spec);
6548 P_Name := Get_Parent_Entity (P);
6549 Remove_Context_Clauses (P_Spec);
6550 End_Package_Scope (P_Name);
6551 Set_Is_Immediately_Visible (P_Name, Vis);
6553 -- Remove from visibility the siblings as well, which are directly
6554 -- visible while the parent is in scope.
6556 E := First_Entity (P_Name);
6557 while Present (E) loop
6558 if Is_Child_Unit (E) then
6559 Set_Is_Immediately_Visible (E, False);
6560 end if;
6562 Next_Entity (E);
6563 end loop;
6565 Set_In_Package_Body (P_Name, False);
6567 -- This is the recursive call to remove the context of any higher
6568 -- level parent. This recursion ensures that all parents are removed
6569 -- in the reverse order of their installation.
6571 Remove_Parents (P);
6572 end if;
6573 end Remove_Parents;
6575 ---------------------------------
6576 -- Remove_Private_With_Clauses --
6577 ---------------------------------
6579 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6580 Item : Node_Id;
6582 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6583 -- Check whether a given unit appears in a regular with_clause. Used to
6584 -- determine whether a private_with_clause, implicit or explicit, should
6585 -- be ignored.
6587 ----------------------------
6588 -- In_Regular_With_Clause --
6589 ----------------------------
6591 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6593 Item : Node_Id;
6595 begin
6596 Item := First (Context_Items (Comp_Unit));
6597 while Present (Item) loop
6598 if Nkind (Item) = N_With_Clause
6600 -- The following guard is needed to ensure that the name has
6601 -- been properly analyzed before we go fetching its entity.
6603 and then Is_Entity_Name (Name (Item))
6604 and then Entity (Name (Item)) = E
6605 and then not Private_Present (Item)
6606 then
6607 return True;
6608 end if;
6609 Next (Item);
6610 end loop;
6612 return False;
6613 end In_Regular_With_Clause;
6615 -- Start of processing for Remove_Private_With_Clauses
6617 begin
6618 Item := First (Context_Items (Comp_Unit));
6619 while Present (Item) loop
6620 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
6622 -- If private_with_clause is redundant, remove it from context,
6623 -- as a small optimization to subsequent handling of private_with
6624 -- clauses in other nested packages.
6626 if In_Regular_With_Clause (Entity (Name (Item))) then
6627 declare
6628 Nxt : constant Node_Id := Next (Item);
6629 begin
6630 Remove (Item);
6631 Item := Nxt;
6632 end;
6634 elsif Limited_Present (Item) then
6635 if not Limited_View_Installed (Item) then
6636 Remove_Limited_With_Clause (Item);
6637 end if;
6639 Next (Item);
6641 else
6642 Remove_Unit_From_Visibility (Entity (Name (Item)));
6643 Set_Context_Installed (Item, False);
6644 Next (Item);
6645 end if;
6647 else
6648 Next (Item);
6649 end if;
6650 end loop;
6651 end Remove_Private_With_Clauses;
6653 ---------------------------------
6654 -- Remove_Unit_From_Visibility --
6655 ---------------------------------
6657 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6658 begin
6659 if Debug_Flag_I then
6660 Write_Str ("remove unit ");
6661 Write_Name (Chars (Unit_Name));
6662 Write_Str (" from visibility");
6663 Write_Eol;
6664 end if;
6666 Set_Is_Visible_Lib_Unit (Unit_Name, False);
6667 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6668 Set_Is_Immediately_Visible (Unit_Name, False);
6670 -- If the unit is a wrapper package, the subprogram instance is
6671 -- what must be removed from visibility.
6672 -- Should we use Related_Instance instead???
6674 if Is_Wrapper_Package (Unit_Name) then
6675 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
6676 end if;
6677 end Remove_Unit_From_Visibility;
6679 --------
6680 -- sm --
6681 --------
6683 procedure sm is
6684 begin
6685 null;
6686 end sm;
6688 -------------
6689 -- Unchain --
6690 -------------
6692 procedure Unchain (E : Entity_Id) is
6693 Prev : Entity_Id;
6695 begin
6696 Prev := Current_Entity (E);
6698 if No (Prev) then
6699 return;
6701 elsif Prev = E then
6702 Set_Name_Entity_Id (Chars (E), Homonym (E));
6704 else
6705 while Present (Prev) and then Homonym (Prev) /= E loop
6706 Prev := Homonym (Prev);
6707 end loop;
6709 if Present (Prev) then
6710 Set_Homonym (Prev, Homonym (E));
6711 end if;
6712 end if;
6714 if Debug_Flag_I then
6715 Write_Str (" (homonym) unchain ");
6716 Write_Name (Chars (E));
6717 Write_Eol;
6718 end if;
6719 end Unchain;
6721 end Sem_Ch10;