Daily bump.
[official-gcc.git] / gcc / ada / sem_ch10.adb
blob358b20a83bfded9777ee8999c01783593cf2c00c
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-2017, 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 In_Predefined_Unit (N) then
652 Circularity := False;
654 else
655 for U in Main_Unit + 1 .. Last_Unit loop
656 if Nkind (Unit (Cunit (U))) = N_Package_Body
657 and then not Analyzed (Cunit (U))
658 then
659 Circularity := False;
660 exit;
661 end if;
662 end loop;
663 end if;
665 if Circularity then
666 Error_Msg_N ("circular dependency caused by with_clauses", N);
667 Error_Msg_N
668 ("\possibly missing limited_with clause"
669 & " in one of the following", N);
671 for U in Main_Unit .. Last_Unit loop
672 if Context_Pending (Cunit (U)) then
673 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
674 Error_Msg_N ("\unit$", N);
675 end if;
676 end loop;
678 raise Unrecoverable_Error;
679 end if;
680 end;
681 else
682 Set_Context_Pending (N);
683 end if;
685 Analyze_Context (N);
687 Set_Context_Pending (N, False);
689 -- If the unit is a package body, the spec is already loaded and must be
690 -- analyzed first, before we analyze the body.
692 if Nkind (Unit_Node) = N_Package_Body then
694 -- If no Lib_Unit, then there was a serious previous error, so just
695 -- ignore the entire analysis effort.
697 if No (Lib_Unit) then
698 Check_Error_Detected;
699 return;
701 else
702 -- Analyze the package spec
704 Semantics (Lib_Unit);
706 -- Check for unused with's
708 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
710 -- Verify that the library unit is a package declaration
712 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
713 N_Generic_Package_Declaration)
714 then
715 Error_Msg_N
716 ("no legal package declaration for package body", N);
717 return;
719 -- Otherwise, the entity in the declaration is visible. Update the
720 -- version to reflect dependence of this body on the spec.
722 else
723 Spec_Id := Defining_Entity (Unit (Lib_Unit));
724 Set_Is_Immediately_Visible (Spec_Id, True);
725 Version_Update (N, Lib_Unit);
727 if Nkind (Defining_Unit_Name (Unit_Node)) =
728 N_Defining_Program_Unit_Name
729 then
730 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
731 end if;
732 end if;
733 end if;
735 -- If the unit is a subprogram body, then we similarly need to analyze
736 -- its spec. However, things are a little simpler in this case, because
737 -- here, this analysis is done mostly for error checking and consistency
738 -- purposes (but not only, e.g. there could be a contract on the spec),
739 -- so there's nothing else to be done.
741 elsif Nkind (Unit_Node) = N_Subprogram_Body then
742 if Acts_As_Spec (N) then
744 -- If the subprogram body is a child unit, we must create a
745 -- declaration for it, in order to properly load the parent(s).
746 -- After this, the original unit does not acts as a spec, because
747 -- there is an explicit one. If this unit appears in a context
748 -- clause, then an implicit with on the parent will be added when
749 -- installing the context. If this is the main unit, there is no
750 -- Unit_Table entry for the declaration (it has the unit number
751 -- of the main unit) and code generation is unaffected.
753 Unum := Get_Cunit_Unit_Number (N);
754 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
756 if Par_Spec_Name /= No_Unit_Name then
757 Unum :=
758 Load_Unit
759 (Load_Name => Par_Spec_Name,
760 Required => True,
761 Subunit => False,
762 Error_Node => N);
764 if Unum /= No_Unit then
766 -- Build subprogram declaration and attach parent unit to it
767 -- This subprogram declaration does not come from source,
768 -- Nevertheless the backend must generate debugging info for
769 -- it, and this must be indicated explicitly. We also mark
770 -- the body entity as a child unit now, to prevent a
771 -- cascaded error if the spec entity cannot be entered
772 -- in its scope. Finally we create a Units table entry for
773 -- the subprogram declaration, to maintain a one-to-one
774 -- correspondence with compilation unit nodes. This is
775 -- critical for the tree traversals performed by CodePeer.
777 declare
778 Loc : constant Source_Ptr := Sloc (N);
779 SCS : constant Boolean :=
780 Get_Comes_From_Source_Default;
782 begin
783 Set_Comes_From_Source_Default (False);
785 -- Note: We copy the Context_Items from the explicit body
786 -- to the implicit spec, setting the former to Empty_List
787 -- to preserve the treeish nature of the tree, during
788 -- analysis of the spec. Then we put it back the way it
789 -- was -- copy the Context_Items from the spec to the
790 -- body, and set the spec Context_Items to Empty_List.
791 -- It is necessary to preserve the treeish nature,
792 -- because otherwise we will call End_Use_* twice on the
793 -- same thing.
795 Lib_Unit :=
796 Make_Compilation_Unit (Loc,
797 Context_Items => Context_Items (N),
798 Unit =>
799 Make_Subprogram_Declaration (Sloc (N),
800 Specification =>
801 Copy_Separate_Tree
802 (Specification (Unit_Node))),
803 Aux_Decls_Node =>
804 Make_Compilation_Unit_Aux (Loc));
806 Set_Context_Items (N, Empty_List);
807 Set_Library_Unit (N, Lib_Unit);
808 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
809 Make_Child_Decl_Unit (N);
810 Semantics (Lib_Unit);
812 -- Now that a separate declaration exists, the body
813 -- of the child unit does not act as spec any longer.
815 Set_Acts_As_Spec (N, False);
816 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
817 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
818 Set_Comes_From_Source_Default (SCS);
820 -- Restore Context_Items to the body
822 Set_Context_Items (N, Context_Items (Lib_Unit));
823 Set_Context_Items (Lib_Unit, Empty_List);
824 end;
825 end if;
826 end if;
828 -- Here for subprogram with separate declaration
830 else
831 Semantics (Lib_Unit);
832 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
833 Version_Update (N, Lib_Unit);
834 end if;
836 -- If this is a child unit, generate references to the parents
838 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
839 N_Defining_Program_Unit_Name
840 then
841 Generate_Parent_References
842 (Specification (Unit_Node),
843 Scope (Defining_Entity (Unit (Lib_Unit))));
844 end if;
845 end if;
847 -- If it is a child unit, the parent must be elaborated first and we
848 -- update version, since we are dependent on our parent.
850 if Is_Child_Spec (Unit_Node) then
852 -- The analysis of the parent is done with style checks off
854 declare
855 Save_Style_Check : constant Boolean := Style_Check;
857 begin
858 if not GNAT_Mode then
859 Style_Check := False;
860 end if;
862 Semantics (Parent_Spec (Unit_Node));
863 Version_Update (N, Parent_Spec (Unit_Node));
865 -- Restore style check settings
867 Style_Check := Save_Style_Check;
868 end;
869 end if;
871 -- With the analysis done, install the context. Note that we can't
872 -- install the context from the with clauses as we analyze them, because
873 -- each with clause must be analyzed in a clean visibility context, so
874 -- we have to wait and install them all at once.
876 Install_Context (N);
878 if Is_Child_Spec (Unit_Node) then
880 -- Set the entities of all parents in the program_unit_name
882 Generate_Parent_References
883 (Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
884 end if;
886 -- All components of the context: with-clauses, library unit, ancestors
887 -- if any, (and their context) are analyzed and installed.
889 -- Call special debug routine sm if this is the main unit
891 if Current_Sem_Unit = Main_Unit then
893 end if;
895 -- Now analyze the unit (package, subprogram spec, body) itself
897 Analyze (Unit_Node);
899 if Warn_On_Redundant_Constructs then
900 Check_Redundant_Withs (Context_Items (N));
902 if Nkind (Unit_Node) = N_Package_Body then
903 Check_Redundant_Withs
904 (Context_Items => Context_Items (N),
905 Spec_Context_Items => Context_Items (Lib_Unit));
906 end if;
907 end if;
909 -- The above call might have made Unit_Node an N_Subprogram_Body from
910 -- something else, so propagate any Acts_As_Spec flag.
912 if Nkind (Unit_Node) = N_Subprogram_Body
913 and then Acts_As_Spec (Unit_Node)
914 then
915 Set_Acts_As_Spec (N);
916 end if;
918 -- Register predefined units in Rtsfind
920 if In_Predefined_Unit (N) then
921 Set_RTU_Loaded (Unit_Node);
922 end if;
924 -- Treat compilation unit pragmas that appear after the library unit
926 if Present (Pragmas_After (Aux_Decls_Node (N))) then
927 declare
928 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
929 begin
930 while Present (Prag_Node) loop
931 Analyze (Prag_Node);
932 Next (Prag_Node);
933 end loop;
934 end;
935 end if;
937 -- Analyze the contract of a [generic] subprogram that acts as a
938 -- compilation unit after all compilation pragmas have been analyzed.
940 if Nkind_In (Unit_Node, N_Generic_Subprogram_Declaration,
941 N_Subprogram_Declaration)
942 then
943 Analyze_Entry_Or_Subprogram_Contract (Defining_Entity (Unit_Node));
944 end if;
946 -- Generate distribution stubs if requested and no error
948 if N = Main_Cunit
949 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
950 or else
951 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
952 and then Fatal_Error (Main_Unit) /= Error_Detected
953 then
954 if Is_RCI_Pkg_Spec_Or_Body (N) then
956 -- Regular RCI package
958 Add_Stub_Constructs (N);
960 elsif (Nkind (Unit_Node) = N_Package_Declaration
961 and then Is_Shared_Passive (Defining_Entity
962 (Specification (Unit_Node))))
963 or else (Nkind (Unit_Node) = N_Package_Body
964 and then
965 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
966 then
967 -- Shared passive package
969 Add_Stub_Constructs (N);
971 elsif Nkind (Unit_Node) = N_Package_Instantiation
972 and then
973 Is_Remote_Call_Interface
974 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
975 then
976 -- Instantiation of a RCI generic package
978 Add_Stub_Constructs (N);
979 end if;
980 end if;
982 -- Remove unit from visibility, so that environment is clean for the
983 -- next compilation, which is either the main unit or some other unit
984 -- in the context.
986 if Nkind_In (Unit_Node, N_Package_Declaration,
987 N_Package_Renaming_Declaration,
988 N_Subprogram_Declaration)
989 or else Nkind (Unit_Node) in N_Generic_Declaration
990 or else (Nkind (Unit_Node) = N_Subprogram_Body
991 and then Acts_As_Spec (Unit_Node))
992 then
993 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
995 -- If the unit is an instantiation whose body will be elaborated for
996 -- inlining purposes, use the proper entity of the instance. The entity
997 -- may be missing if the instantiation was illegal.
999 elsif Nkind (Unit_Node) = N_Package_Instantiation
1000 and then not Error_Posted (Unit_Node)
1001 and then Present (Instance_Spec (Unit_Node))
1002 then
1003 Remove_Unit_From_Visibility
1004 (Defining_Entity (Instance_Spec (Unit_Node)));
1006 elsif Nkind (Unit_Node) = N_Package_Body
1007 or else (Nkind (Unit_Node) = N_Subprogram_Body
1008 and then not Acts_As_Spec (Unit_Node))
1009 then
1010 -- Bodies that are not the main unit are compiled if they are generic
1011 -- or contain generic or inlined units. Their analysis brings in the
1012 -- context of the corresponding spec (unit declaration) which must be
1013 -- removed as well, to return the compilation environment to its
1014 -- proper state.
1016 Remove_Context (Lib_Unit);
1017 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1018 end if;
1020 -- Last step is to deinstall the context we just installed as well as
1021 -- the unit just compiled.
1023 Remove_Context (N);
1025 -- When generating code for a non-generic main unit, check that withed
1026 -- generic units have a body if they need it, even if the units have not
1027 -- been instantiated. Force the load of the bodies to produce the proper
1028 -- error if the body is absent. The same applies to GNATprove mode, with
1029 -- the added benefit of capturing global references within the generic.
1030 -- This in turn allows for proper inlining of subprogram bodies without
1031 -- a previous declaration.
1033 if Get_Cunit_Unit_Number (N) = Main_Unit
1034 and then ((Operating_Mode = Generate_Code and then Expander_Active)
1035 or else
1036 (Operating_Mode = Check_Semantics and then GNATprove_Mode))
1037 then
1038 -- Check whether the source for the body of the unit must be included
1039 -- in a standalone library.
1041 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1043 -- Indicate that the main unit is now analyzed, to catch possible
1044 -- circularities between it and generic bodies. Remove main unit from
1045 -- visibility. This might seem superfluous, but the main unit must
1046 -- not be visible in the generic body expansions that follow.
1048 Set_Analyzed (N, True);
1049 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1051 declare
1052 Item : Node_Id;
1053 Nam : Entity_Id;
1054 Un : Unit_Number_Type;
1056 Save_Style_Check : constant Boolean := Style_Check;
1058 begin
1059 Item := First (Context_Items (N));
1060 while Present (Item) loop
1062 -- Check for explicit with clause
1064 if Nkind (Item) = N_With_Clause
1065 and then not Implicit_With (Item)
1067 -- Ada 2005 (AI-50217): Ignore limited-withed units
1069 and then not Limited_Present (Item)
1070 then
1071 Nam := Entity (Name (Item));
1073 -- Compile the generic subprogram, unless it is intrinsic or
1074 -- imported so no body is required, or generic package body
1075 -- if the package spec requires a body.
1077 if (Is_Generic_Subprogram (Nam)
1078 and then not Is_Intrinsic_Subprogram (Nam)
1079 and then not Is_Imported (Nam))
1080 or else (Ekind (Nam) = E_Generic_Package
1081 and then Unit_Requires_Body (Nam))
1082 then
1083 Style_Check := False;
1085 if Present (Renamed_Object (Nam)) then
1086 Un :=
1087 Load_Unit
1088 (Load_Name =>
1089 Get_Body_Name
1090 (Get_Unit_Name
1091 (Unit_Declaration_Node
1092 (Renamed_Object (Nam)))),
1093 Required => False,
1094 Subunit => False,
1095 Error_Node => N,
1096 Renamings => True);
1097 else
1098 Un :=
1099 Load_Unit
1100 (Load_Name =>
1101 Get_Body_Name (Get_Unit_Name (Item)),
1102 Required => False,
1103 Subunit => False,
1104 Error_Node => N,
1105 Renamings => True);
1106 end if;
1108 if Un = No_Unit then
1109 Error_Msg_NE
1110 ("body of generic unit& not found", Item, Nam);
1111 exit;
1113 elsif not Analyzed (Cunit (Un))
1114 and then Un /= Main_Unit
1115 and then Fatal_Error (Un) /= Error_Detected
1116 then
1117 Style_Check := False;
1118 Semantics (Cunit (Un));
1119 end if;
1120 end if;
1121 end if;
1123 Next (Item);
1124 end loop;
1126 -- Restore style checks settings
1128 Style_Check := Save_Style_Check;
1129 end;
1131 -- In GNATprove mode, force the loading of an Interrupt_Priority when
1132 -- processing compilation units with potentially "main" subprograms.
1133 -- This is required for the ceiling priority protocol checks, which
1134 -- are triggered by these subprograms.
1136 if GNATprove_Mode
1137 and then Nkind_In (Unit_Node, N_Function_Instantiation,
1138 N_Procedure_Instantiation,
1139 N_Subprogram_Body)
1140 then
1141 declare
1142 Spec : Node_Id;
1144 begin
1145 case Nkind (Unit_Node) is
1146 when N_Subprogram_Body =>
1147 Spec := Specification (Unit_Node);
1149 when N_Subprogram_Instantiation =>
1150 Spec :=
1151 Subprogram_Specification (Entity (Name (Unit_Node)));
1153 when others =>
1154 raise Program_Error;
1155 end case;
1157 pragma Assert (Nkind (Spec) in N_Subprogram_Specification);
1159 -- Main subprogram must have no parameters, and if it is a
1160 -- function, it must return an integer.
1162 if No (Parameter_Specifications (Spec))
1163 and then (Nkind (Spec) = N_Procedure_Specification
1164 or else
1165 Is_Integer_Type (Etype (Result_Definition (Spec))))
1166 then
1167 SPARK_Implicit_Load (RE_Interrupt_Priority);
1168 end if;
1169 end;
1170 end if;
1171 end if;
1173 -- Deal with creating elaboration counter if needed. We create an
1174 -- elaboration counter only for units that come from source since
1175 -- units manufactured by the compiler never need elab checks.
1177 if Comes_From_Source (N)
1178 and then Nkind_In (Unit_Node, N_Package_Declaration,
1179 N_Generic_Package_Declaration,
1180 N_Subprogram_Declaration,
1181 N_Generic_Subprogram_Declaration)
1182 then
1183 declare
1184 Loc : constant Source_Ptr := Sloc (N);
1185 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1187 begin
1188 Spec_Id := Defining_Entity (Unit_Node);
1189 Generate_Definition (Spec_Id);
1191 -- See if an elaboration entity is required for possible access
1192 -- before elaboration checking. Note that we must allow for this
1193 -- even if -gnatE is not set, since a client may be compiled in
1194 -- -gnatE mode and reference the entity.
1196 -- These entities are also used by the binder to prevent multiple
1197 -- attempts to execute the elaboration code for the library case
1198 -- where the elaboration routine might otherwise be called more
1199 -- than once.
1201 -- They are also needed to ensure explicit visibility from the
1202 -- binder generated code of all the units involved in a partition
1203 -- when control-flow preservation is requested.
1205 -- Case of units which do not require an elaboration entity
1207 if not Opt.Suppress_Control_Flow_Optimizations
1208 and then
1209 ( -- Pure units do not need checks
1211 Is_Pure (Spec_Id)
1213 -- Preelaborated units do not need checks
1215 or else Is_Preelaborated (Spec_Id)
1217 -- No checks needed if pragma Elaborate_Body present
1219 or else Has_Pragma_Elaborate_Body (Spec_Id)
1221 -- No checks needed if unit does not require a body
1223 or else not Unit_Requires_Body (Spec_Id)
1225 -- No checks needed for predefined files
1227 or else Is_Predefined_Unit (Unum)
1229 -- No checks required if no separate spec
1231 or else Acts_As_Spec (N)
1233 then
1234 -- This is a case where we only need the entity for
1235 -- checking to prevent multiple elaboration checks.
1237 Set_Elaboration_Entity_Required (Spec_Id, False);
1239 -- Case of elaboration entity is required for access before
1240 -- elaboration checking (so certainly we must build it).
1242 else
1243 Set_Elaboration_Entity_Required (Spec_Id, True);
1244 end if;
1246 Build_Elaboration_Entity (N, Spec_Id);
1247 end;
1248 end if;
1250 -- Freeze the compilation unit entity. This for sure is needed because
1251 -- of some warnings that can be output (see Freeze_Subprogram), but may
1252 -- in general be required. If freezing actions result, place them in the
1253 -- compilation unit actions list, and analyze them.
1255 declare
1256 L : constant List_Id :=
1257 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1258 begin
1259 while Is_Non_Empty_List (L) loop
1260 Insert_Library_Level_Action (Remove_Head (L));
1261 end loop;
1262 end;
1264 Set_Analyzed (N);
1266 -- Call Check_Package_Body so that a body containing subprograms with
1267 -- Inline_Always can be made available for front end inlining.
1269 if Nkind (Unit_Node) = N_Package_Declaration
1270 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1272 -- We don't need to do this if the Expander is not active, since there
1273 -- is no code to inline.
1275 and then Expander_Active
1276 then
1277 declare
1278 Save_Style_Check : constant Boolean := Style_Check;
1279 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1280 Options : Style_Check_Options;
1282 begin
1283 Save_Style_Check_Options (Options);
1284 Reset_Style_Check_Options;
1285 Opt.Warning_Mode := Suppress;
1287 Check_Package_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1289 Reset_Style_Check_Options;
1290 Set_Style_Check_Options (Options);
1291 Style_Check := Save_Style_Check;
1292 Warning_Mode := Save_Warning;
1293 end;
1294 end if;
1296 -- If we are generating obsolescent warnings, then here is where we
1297 -- generate them for the with'ed items. The reason for this special
1298 -- processing is that the normal mechanism of generating the warnings
1299 -- for referenced entities does not work for context clause references.
1300 -- That's because when we first analyze the context, it is too early to
1301 -- know if the with'ing unit is itself obsolescent (which suppresses
1302 -- the warnings).
1304 if not GNAT_Mode
1305 and then Warn_On_Obsolescent_Feature
1306 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1307 then
1308 -- Push current compilation unit as scope, so that the test for
1309 -- being within an obsolescent unit will work correctly. The check
1310 -- is not performed within an instantiation, because the warning
1311 -- will have been emitted in the corresponding generic unit.
1313 Push_Scope (Defining_Entity (Unit_Node));
1315 -- Loop through context items to deal with with clauses
1317 declare
1318 Item : Node_Id;
1319 Nam : Node_Id;
1320 Ent : Entity_Id;
1322 begin
1323 Item := First (Context_Items (N));
1324 while Present (Item) loop
1325 if Nkind (Item) = N_With_Clause
1327 -- Suppress this check in limited-withed units. Further work
1328 -- needed here if we decide to incorporate this check on
1329 -- limited-withed units.
1331 and then not Limited_Present (Item)
1332 then
1333 Nam := Name (Item);
1334 Ent := Entity (Nam);
1336 if Is_Obsolescent (Ent) then
1337 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1338 end if;
1339 end if;
1341 Next (Item);
1342 end loop;
1343 end;
1345 -- Remove temporary install of current unit as scope
1347 Pop_Scope;
1348 end if;
1350 -- If No_Elaboration_Code_All was encountered, this is where we do the
1351 -- transitive test of with'ed units to make sure they have the aspect.
1352 -- This is delayed till the end of analyzing the compilation unit to
1353 -- ensure that the pragma/aspect, if present, has been analyzed.
1355 Check_No_Elab_Code_All (N);
1356 end Analyze_Compilation_Unit;
1358 ---------------------
1359 -- Analyze_Context --
1360 ---------------------
1362 procedure Analyze_Context (N : Node_Id) is
1363 Ukind : constant Node_Kind := Nkind (Unit (N));
1364 Item : Node_Id;
1366 begin
1367 -- First process all configuration pragmas at the start of the context
1368 -- items. Strictly these are not part of the context clause, but that
1369 -- is where the parser puts them. In any case for sure we must analyze
1370 -- these before analyzing the actual context items, since they can have
1371 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1372 -- be with'ed as a result of changing categorizations in Ada 2005).
1374 Item := First (Context_Items (N));
1375 while Present (Item)
1376 and then Nkind (Item) = N_Pragma
1377 and then Pragma_Name (Item) in Configuration_Pragma_Names
1378 loop
1379 Analyze (Item);
1380 Next (Item);
1381 end loop;
1383 -- This is the point at which we capture the configuration settings
1384 -- for the unit. At the moment only the Optimize_Alignment setting
1385 -- needs to be captured. Probably more later ???
1387 if Optimize_Alignment_Local then
1388 Set_OA_Setting (Current_Sem_Unit, 'L');
1389 else
1390 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1391 end if;
1393 -- Loop through actual context items. This is done in two passes:
1395 -- a) The first pass analyzes non-limited with-clauses and also any
1396 -- configuration pragmas (we need to get the latter analyzed right
1397 -- away, since they can affect processing of subsequent items).
1399 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1401 while Present (Item) loop
1403 -- For with clause, analyze the with clause, and then update the
1404 -- version, since we are dependent on a unit that we with.
1406 if Nkind (Item) = N_With_Clause
1407 and then not Limited_Present (Item)
1408 then
1409 -- Skip analyzing with clause if no unit, nothing to do (this
1410 -- happens for a with that references a non-existent unit).
1412 if Present (Library_Unit (Item)) then
1414 -- Skip analyzing with clause if this is a with_clause for
1415 -- the main unit, which happens if a subunit has a useless
1416 -- with_clause on its parent.
1418 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1419 Analyze (Item);
1421 -- Here for the case of a useless with for the main unit
1423 else
1424 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1425 end if;
1426 end if;
1428 -- Do version update (skipped for implicit with)
1430 if not Implicit_With (Item) then
1431 Version_Update (N, Library_Unit (Item));
1432 end if;
1434 -- Skip pragmas. Configuration pragmas at the start were handled in
1435 -- the loop above, and remaining pragmas are not processed until we
1436 -- actually install the context (see Install_Context). We delay the
1437 -- analysis of these pragmas to make sure that we have installed all
1438 -- the implicit with's on parent units.
1440 -- Skip use clauses at this stage, since we don't want to do any
1441 -- installing of potentially use-visible entities until we
1442 -- actually install the complete context (in Install_Context).
1443 -- Otherwise things can get installed in the wrong context.
1445 else
1446 null;
1447 end if;
1449 Next (Item);
1450 end loop;
1452 -- Second pass: examine all limited_with clauses. All other context
1453 -- items are ignored in this pass.
1455 Item := First (Context_Items (N));
1456 while Present (Item) loop
1457 if Nkind (Item) = N_With_Clause
1458 and then Limited_Present (Item)
1459 then
1460 -- No need to check errors on implicitly generated limited-with
1461 -- clauses.
1463 if not Implicit_With (Item) then
1465 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1466 -- properly rejected, including renaming declarations.
1468 if not Nkind_In (Ukind, N_Package_Declaration,
1469 N_Subprogram_Declaration)
1470 and then Ukind not in N_Generic_Declaration
1471 and then Ukind not in N_Generic_Instantiation
1472 then
1473 Error_Msg_N ("limited with_clause not allowed here", Item);
1475 -- Check wrong use of a limited with clause applied to the
1476 -- compilation unit containing the limited-with clause.
1478 -- limited with P.Q;
1479 -- package P.Q is ...
1481 elsif Unit (Library_Unit (Item)) = Unit (N) then
1482 Error_Msg_N ("wrong use of limited-with clause", Item);
1484 -- Check wrong use of limited-with clause applied to some
1485 -- immediate ancestor.
1487 elsif Is_Child_Spec (Unit (N)) then
1488 declare
1489 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1490 P : Node_Id;
1492 begin
1493 P := Parent_Spec (Unit (N));
1494 loop
1495 if Unit (P) = Lib_U then
1496 Error_Msg_N ("limited with_clause cannot "
1497 & "name ancestor", Item);
1498 exit;
1499 end if;
1501 exit when not Is_Child_Spec (Unit (P));
1502 P := Parent_Spec (Unit (P));
1503 end loop;
1504 end;
1505 end if;
1507 -- Check if the limited-withed unit is already visible through
1508 -- some context clause of the current compilation unit or some
1509 -- ancestor of the current compilation unit.
1511 declare
1512 Lim_Unit_Name : constant Node_Id := Name (Item);
1513 Comp_Unit : Node_Id;
1514 It : Node_Id;
1515 Unit_Name : Node_Id;
1517 begin
1518 Comp_Unit := N;
1519 loop
1520 It := First (Context_Items (Comp_Unit));
1521 while Present (It) loop
1522 if Item /= It
1523 and then Nkind (It) = N_With_Clause
1524 and then not Limited_Present (It)
1525 and then
1526 Nkind_In (Unit (Library_Unit (It)),
1527 N_Package_Declaration,
1528 N_Package_Renaming_Declaration)
1529 then
1530 if Nkind (Unit (Library_Unit (It))) =
1531 N_Package_Declaration
1532 then
1533 Unit_Name := Name (It);
1534 else
1535 Unit_Name := Name (Unit (Library_Unit (It)));
1536 end if;
1538 -- Check if the named package (or some ancestor)
1539 -- leaves visible the full-view of the unit given
1540 -- in the limited-with clause.
1542 loop
1543 if Designate_Same_Unit (Lim_Unit_Name,
1544 Unit_Name)
1545 then
1546 Error_Msg_Sloc := Sloc (It);
1547 Error_Msg_N
1548 ("simultaneous visibility of limited "
1549 & "and unlimited views not allowed",
1550 Item);
1551 Error_Msg_NE
1552 ("\unlimited view visible through "
1553 & "context clause #",
1554 Item, It);
1555 exit;
1557 elsif Nkind (Unit_Name) = N_Identifier then
1558 exit;
1559 end if;
1561 Unit_Name := Prefix (Unit_Name);
1562 end loop;
1563 end if;
1565 Next (It);
1566 end loop;
1568 exit when not Is_Child_Spec (Unit (Comp_Unit));
1570 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1571 end loop;
1572 end;
1573 end if;
1575 -- Skip analyzing with clause if no unit, see above
1577 if Present (Library_Unit (Item)) then
1578 Analyze (Item);
1579 end if;
1581 -- A limited_with does not impose an elaboration order, but
1582 -- there is a semantic dependency for recompilation purposes.
1584 if not Implicit_With (Item) then
1585 Version_Update (N, Library_Unit (Item));
1586 end if;
1588 -- Pragmas and use clauses and with clauses other than limited
1589 -- with's are ignored in this pass through the context items.
1591 else
1592 null;
1593 end if;
1595 Next (Item);
1596 end loop;
1597 end Analyze_Context;
1599 -------------------------------
1600 -- Analyze_Package_Body_Stub --
1601 -------------------------------
1603 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1604 Id : constant Entity_Id := Defining_Identifier (N);
1605 Nam : Entity_Id;
1606 Opts : Config_Switches_Type;
1608 begin
1609 -- The package declaration must be in the current declarative part
1611 Check_Stub_Level (N);
1612 Nam := Current_Entity_In_Scope (Id);
1614 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1615 Error_Msg_N ("missing specification for package stub", N);
1617 elsif Has_Completion (Nam)
1618 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1619 then
1620 Error_Msg_N ("duplicate or redundant stub for package", N);
1622 else
1623 -- Retain and restore the configuration options of the enclosing
1624 -- context as the proper body may introduce a set of its own.
1626 Save_Opt_Config_Switches (Opts);
1628 -- Indicate that the body of the package exists. If we are doing
1629 -- only semantic analysis, the stub stands for the body. If we are
1630 -- generating code, the existence of the body will be confirmed
1631 -- when we load the proper body.
1633 Set_Has_Completion (Nam);
1634 Set_Scope (Defining_Entity (N), Current_Scope);
1635 Set_Ekind (Defining_Entity (N), E_Package_Body);
1636 Set_Corresponding_Spec_Of_Stub (N, Nam);
1637 Generate_Reference (Nam, Id, 'b');
1638 Analyze_Proper_Body (N, Nam);
1640 Restore_Opt_Config_Switches (Opts);
1641 end if;
1642 end Analyze_Package_Body_Stub;
1644 -------------------------
1645 -- Analyze_Proper_Body --
1646 -------------------------
1648 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1649 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1651 procedure Optional_Subunit;
1652 -- This procedure is called when the main unit is a stub, or when we
1653 -- are not generating code. In such a case, we analyze the subunit if
1654 -- present, which is user-friendly and in fact required for ASIS, but we
1655 -- don't complain if the subunit is missing. In GNATprove_Mode, we issue
1656 -- an error to avoid formal verification of a partial unit.
1658 ----------------------
1659 -- Optional_Subunit --
1660 ----------------------
1662 procedure Optional_Subunit is
1663 Comp_Unit : Node_Id;
1664 Unum : Unit_Number_Type;
1666 begin
1667 -- Try to load subunit, but ignore any errors that occur during the
1668 -- loading of the subunit, by using the special feature in Errout to
1669 -- ignore all errors. Note that Fatal_Error will still be set, so we
1670 -- will be able to check for this case below.
1672 if not (ASIS_Mode or GNATprove_Mode) then
1673 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1674 end if;
1676 Unum :=
1677 Load_Unit
1678 (Load_Name => Subunit_Name,
1679 Required => GNATprove_Mode,
1680 Subunit => True,
1681 Error_Node => N);
1683 if not (ASIS_Mode or GNATprove_Mode) then
1684 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1685 end if;
1687 -- All done if we successfully loaded the subunit
1689 if Unum /= No_Unit
1690 and then (Fatal_Error (Unum) /= Error_Detected
1691 or else Try_Semantics)
1692 then
1693 Comp_Unit := Cunit (Unum);
1695 -- If the file was empty or seriously mangled, the unit itself may
1696 -- be missing.
1698 if No (Unit (Comp_Unit)) then
1699 Error_Msg_N
1700 ("subunit does not contain expected proper body", N);
1702 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1703 Error_Msg_N
1704 ("expected SEPARATE subunit, found child unit",
1705 Cunit_Entity (Unum));
1706 else
1707 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1708 Analyze_Subunit (Comp_Unit);
1709 Set_Library_Unit (N, Comp_Unit);
1710 Set_Corresponding_Body (N, Defining_Entity (Unit (Comp_Unit)));
1711 end if;
1713 elsif Unum = No_Unit
1714 and then Present (Nam)
1715 then
1716 if Is_Protected_Type (Nam) then
1717 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1718 else
1719 Set_Corresponding_Body (
1720 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1721 end if;
1722 end if;
1723 end Optional_Subunit;
1725 -- Local variables
1727 Comp_Unit : Node_Id;
1728 Unum : Unit_Number_Type;
1730 -- Start of processing for Analyze_Proper_Body
1732 begin
1733 -- If the subunit is already loaded, it means that the main unit is a
1734 -- subunit, and that the current unit is one of its parents which was
1735 -- being analyzed to provide the needed context for the analysis of the
1736 -- subunit. In this case we analyze the subunit and continue with the
1737 -- parent, without looking at subsequent subunits.
1739 if Is_Loaded (Subunit_Name) then
1741 -- If the proper body is already linked to the stub node, the stub is
1742 -- in a generic unit and just needs analyzing.
1744 if Present (Library_Unit (N)) then
1745 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1747 -- If the subunit has severe errors, the spec of the enclosing
1748 -- body may not be available, in which case do not try analysis.
1750 if Serious_Errors_Detected > 0
1751 and then No (Library_Unit (Library_Unit (N)))
1752 then
1753 return;
1754 end if;
1756 -- Collect SCO information for loaded subunit if we are in the
1757 -- extended main unit.
1759 if Generate_SCO
1760 and then In_Extended_Main_Source_Unit
1761 (Cunit_Entity (Current_Sem_Unit))
1762 then
1763 SCO_Record_Raw (Get_Cunit_Unit_Number (Library_Unit (N)));
1764 end if;
1766 Analyze_Subunit (Library_Unit (N));
1768 -- Otherwise we must load the subunit and link to it
1770 else
1771 -- Load the subunit, this must work, since we originally loaded
1772 -- the subunit earlier on. So this will not really load it, just
1773 -- give access to it.
1775 Unum :=
1776 Load_Unit
1777 (Load_Name => Subunit_Name,
1778 Required => True,
1779 Subunit => False,
1780 Error_Node => N);
1782 -- And analyze the subunit in the parent context (note that we
1783 -- do not call Semantics, since that would remove the parent
1784 -- context). Because of this, we have to manually reset the
1785 -- compiler state to Analyzing since it got destroyed by Load.
1787 if Unum /= No_Unit then
1788 Compiler_State := Analyzing;
1790 -- Check that the proper body is a subunit and not a child
1791 -- unit. If the unit was previously loaded, the error will
1792 -- have been emitted when copying the generic node, so we
1793 -- just return to avoid cascaded errors.
1795 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1796 return;
1797 end if;
1799 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1800 Analyze_Subunit (Cunit (Unum));
1801 Set_Library_Unit (N, Cunit (Unum));
1802 end if;
1803 end if;
1805 -- If the main unit is a subunit, then we are just performing semantic
1806 -- analysis on that subunit, and any other subunits of any parent unit
1807 -- should be ignored, except that if we are building trees for ASIS
1808 -- usage we want to annotate the stub properly. If the main unit is
1809 -- itself a subunit, another subunit is irrelevant unless it is a
1810 -- subunit of the current one, that is to say appears in the current
1811 -- source tree.
1813 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1814 and then Subunit_Name /= Unit_Name (Main_Unit)
1815 then
1816 if ASIS_Mode then
1817 declare
1818 PB : constant Node_Id := Proper_Body (Unit (Cunit (Main_Unit)));
1819 begin
1820 if Nkind_In (PB, N_Package_Body, N_Subprogram_Body)
1821 and then List_Containing (N) = Declarations (PB)
1822 then
1823 Optional_Subunit;
1824 end if;
1825 end;
1826 end if;
1828 -- But before we return, set the flag for unloaded subunits. This
1829 -- will suppress junk warnings of variables in the same declarative
1830 -- part (or a higher level one) that are in danger of looking unused
1831 -- when in fact there might be a declaration in the subunit that we
1832 -- do not intend to load.
1834 Unloaded_Subunits := True;
1835 return;
1837 -- If the subunit is not already loaded, and we are generating code,
1838 -- then this is the case where compilation started from the parent, and
1839 -- we are generating code for an entire subunit tree. In that case we
1840 -- definitely need to load the subunit.
1842 -- In order to continue the analysis with the rest of the parent,
1843 -- and other subunits, we load the unit without requiring its
1844 -- presence, and emit a warning if not found, rather than terminating
1845 -- the compilation abruptly, as for other missing file problems.
1847 elsif Original_Operating_Mode = Generate_Code then
1849 -- If the proper body is already linked to the stub node, the stub is
1850 -- in a generic unit and just needs analyzing.
1852 -- We update the version. Although we are not strictly technically
1853 -- semantically dependent on the subunit, given our approach of macro
1854 -- substitution of subunits, it makes sense to include it in the
1855 -- version identification.
1857 if Present (Library_Unit (N)) then
1858 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1859 Analyze_Subunit (Library_Unit (N));
1860 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1862 -- Otherwise we must load the subunit and link to it
1864 else
1865 -- Make sure that, if the subunit is preprocessed and -gnateG is
1866 -- specified, the preprocessed file will be written.
1868 Lib.Analysing_Subunit_Of_Main := True;
1869 Unum :=
1870 Load_Unit
1871 (Load_Name => Subunit_Name,
1872 Required => False,
1873 Subunit => True,
1874 Error_Node => N);
1875 Lib.Analysing_Subunit_Of_Main := False;
1877 -- Give message if we did not get the unit Emit warning even if
1878 -- missing subunit is not within main unit, to simplify debugging.
1880 pragma Assert (Original_Operating_Mode = Generate_Code);
1881 if Unum = No_Unit then
1882 Error_Msg_Unit_1 := Subunit_Name;
1883 Error_Msg_File_1 :=
1884 Get_File_Name (Subunit_Name, Subunit => True);
1885 Error_Msg_N
1886 ("subunit$$ in file{ not found??!!", N);
1887 Subunits_Missing := True;
1888 end if;
1890 -- Load_Unit may reset Compiler_State, since it may have been
1891 -- necessary to parse an additional units, so we make sure that
1892 -- we reset it to the Analyzing state.
1894 Compiler_State := Analyzing;
1896 if Unum /= No_Unit then
1897 if Debug_Flag_L then
1898 Write_Str ("*** Loaded subunit from stub. Analyze");
1899 Write_Eol;
1900 end if;
1902 Comp_Unit := Cunit (Unum);
1904 -- Check for child unit instead of subunit
1906 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1907 Error_Msg_N
1908 ("expected SEPARATE subunit, found child unit",
1909 Cunit_Entity (Unum));
1911 -- OK, we have a subunit
1913 else
1914 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1915 Set_Library_Unit (N, Comp_Unit);
1917 -- We update the version. Although we are not technically
1918 -- semantically dependent on the subunit, given our approach
1919 -- of macro substitution of subunits, it makes sense to
1920 -- include it in the version identification.
1922 Version_Update (Cunit (Main_Unit), Comp_Unit);
1924 -- Collect SCO information for loaded subunit if we are in
1925 -- the extended main unit.
1927 if Generate_SCO
1928 and then In_Extended_Main_Source_Unit
1929 (Cunit_Entity (Current_Sem_Unit))
1930 then
1931 SCO_Record_Raw (Unum);
1932 end if;
1934 -- Analyze the unit if semantics active
1936 if Fatal_Error (Unum) /= Error_Detected
1937 or else Try_Semantics
1938 then
1939 Analyze_Subunit (Comp_Unit);
1940 end if;
1941 end if;
1942 end if;
1943 end if;
1945 -- The remaining case is when the subunit is not already loaded and we
1946 -- are not generating code. In this case we are just performing semantic
1947 -- analysis on the parent, and we are not interested in the subunit. For
1948 -- subprograms, analyze the stub as a body. For other entities the stub
1949 -- has already been marked as completed.
1951 else
1952 Optional_Subunit;
1953 end if;
1954 end Analyze_Proper_Body;
1956 ----------------------------------
1957 -- Analyze_Protected_Body_Stub --
1958 ----------------------------------
1960 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1961 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1963 begin
1964 Check_Stub_Level (N);
1966 -- First occurrence of name may have been as an incomplete type
1968 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1969 Nam := Full_View (Nam);
1970 end if;
1972 if No (Nam) or else not Is_Protected_Type (Etype (Nam)) then
1973 Error_Msg_N ("missing specification for Protected body", N);
1975 else
1976 Set_Scope (Defining_Entity (N), Current_Scope);
1977 Set_Ekind (Defining_Entity (N), E_Protected_Body);
1978 Set_Has_Completion (Etype (Nam));
1979 Set_Corresponding_Spec_Of_Stub (N, Nam);
1980 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1981 Analyze_Proper_Body (N, Etype (Nam));
1982 end if;
1983 end Analyze_Protected_Body_Stub;
1985 ----------------------------------
1986 -- Analyze_Subprogram_Body_Stub --
1987 ----------------------------------
1989 -- A subprogram body stub can appear with or without a previous spec. If
1990 -- there is one, then the analysis of the body will find it and verify
1991 -- conformance. The formals appearing in the specification of the stub play
1992 -- no role, except for requiring an additional conformance check. If there
1993 -- is no previous subprogram declaration, the stub acts as a spec, and
1994 -- provides the defining entity for the subprogram.
1996 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1997 Decl : Node_Id;
1998 Opts : Config_Switches_Type;
2000 begin
2001 Check_Stub_Level (N);
2003 -- Verify that the identifier for the stub is unique within this
2004 -- declarative part.
2006 if Nkind_In (Parent (N), N_Block_Statement,
2007 N_Package_Body,
2008 N_Subprogram_Body)
2009 then
2010 Decl := First (Declarations (Parent (N)));
2011 while Present (Decl) and then Decl /= N loop
2012 if Nkind (Decl) = N_Subprogram_Body_Stub
2013 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
2014 Chars (Defining_Unit_Name (Specification (N))))
2015 then
2016 Error_Msg_N ("identifier for stub is not unique", N);
2017 end if;
2019 Next (Decl);
2020 end loop;
2021 end if;
2023 -- Retain and restore the configuration options of the enclosing context
2024 -- as the proper body may introduce a set of its own.
2026 Save_Opt_Config_Switches (Opts);
2028 -- Treat stub as a body, which checks conformance if there is a previous
2029 -- declaration, or else introduces entity and its signature.
2031 Analyze_Subprogram_Body (N);
2032 Analyze_Proper_Body (N, Empty);
2034 Restore_Opt_Config_Switches (Opts);
2035 end Analyze_Subprogram_Body_Stub;
2037 ---------------------
2038 -- Analyze_Subunit --
2039 ---------------------
2041 -- A subunit is compiled either by itself (for semantic checking) or as
2042 -- part of compiling the parent (for code generation). In either case, by
2043 -- the time we actually process the subunit, the parent has already been
2044 -- installed and analyzed. The node N is a compilation unit, whose context
2045 -- needs to be treated here, because we come directly here from the parent
2046 -- without calling Analyze_Compilation_Unit.
2048 -- The compilation context includes the explicit context of the subunit,
2049 -- and the context of the parent, together with the parent itself. In order
2050 -- to compile the current context, we remove the one inherited from the
2051 -- parent, in order to have a clean visibility table. We restore the parent
2052 -- context before analyzing the proper body itself. On exit, we remove only
2053 -- the explicit context of the subunit.
2055 -- WARNING: This routine manages SPARK regions. Return statements must be
2056 -- replaced by gotos which jump to the end of the routine and restore the
2057 -- SPARK mode.
2059 procedure Analyze_Subunit (N : Node_Id) is
2060 Lib_Unit : constant Node_Id := Library_Unit (N);
2061 Par_Unit : constant Entity_Id := Current_Scope;
2063 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
2064 Num_Scopes : Nat := 0;
2065 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2066 Enclosing_Child : Entity_Id := Empty;
2067 Svg : constant Suppress_Record := Scope_Suppress;
2069 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
2070 Cunit_Boolean_Restrictions_Save;
2071 -- Save non-partition wide restrictions before processing the subunit.
2072 -- All subunits are analyzed with config restrictions reset and we need
2073 -- to restore these saved values at the end.
2075 procedure Analyze_Subunit_Context;
2076 -- Capture names in use clauses of the subunit. This must be done before
2077 -- re-installing parent declarations, because items in the context must
2078 -- not be hidden by declarations local to the parent.
2080 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
2081 -- Recursive procedure to restore scope of all ancestors of subunit,
2082 -- from outermost in. If parent is not a subunit, the call to install
2083 -- context installs context of spec and (if parent is a child unit) the
2084 -- context of its parents as well. It is confusing that parents should
2085 -- be treated differently in both cases, but the semantics are just not
2086 -- identical.
2088 procedure Re_Install_Use_Clauses;
2089 -- As part of the removal of the parent scope, the use clauses are
2090 -- removed, to be reinstalled when the context of the subunit has been
2091 -- analyzed. Use clauses may also have been affected by the analysis of
2092 -- the context of the subunit, so they have to be applied again, to
2093 -- insure that the compilation environment of the rest of the parent
2094 -- unit is identical.
2096 procedure Remove_Scope;
2097 -- Remove current scope from scope stack, and preserve the list of use
2098 -- clauses in it, to be reinstalled after context is analyzed.
2100 -----------------------------
2101 -- Analyze_Subunit_Context --
2102 -----------------------------
2104 procedure Analyze_Subunit_Context is
2105 Item : Node_Id;
2106 Nam : Node_Id;
2107 Unit_Name : Entity_Id;
2109 begin
2110 Analyze_Context (N);
2111 Check_No_Elab_Code_All (N);
2113 -- Make withed units immediately visible. If child unit, make the
2114 -- ultimate parent immediately visible.
2116 Item := First (Context_Items (N));
2117 while Present (Item) loop
2118 if Nkind (Item) = N_With_Clause then
2120 -- Protect frontend against previous errors in context clauses
2122 if Nkind (Name (Item)) /= N_Selected_Component then
2123 if Error_Posted (Item) then
2124 null;
2126 else
2127 -- If a subunits has serious syntax errors, the context
2128 -- may not have been loaded. Add a harmless unit name to
2129 -- attempt processing.
2131 if Serious_Errors_Detected > 0
2132 and then No (Entity (Name (Item)))
2133 then
2134 Set_Entity (Name (Item), Standard_Standard);
2135 end if;
2137 Unit_Name := Entity (Name (Item));
2138 loop
2139 Set_Is_Visible_Lib_Unit (Unit_Name);
2140 exit when Scope (Unit_Name) = Standard_Standard;
2141 Unit_Name := Scope (Unit_Name);
2143 if No (Unit_Name) then
2144 Check_Error_Detected;
2145 return;
2146 end if;
2147 end loop;
2149 if not Is_Immediately_Visible (Unit_Name) then
2150 Set_Is_Immediately_Visible (Unit_Name);
2151 Set_Context_Installed (Item);
2152 end if;
2153 end if;
2154 end if;
2156 elsif Nkind (Item) = N_Use_Package_Clause then
2157 Nam := First (Names (Item));
2158 while Present (Nam) loop
2159 Analyze (Nam);
2160 Next (Nam);
2161 end loop;
2163 elsif Nkind (Item) = N_Use_Type_Clause then
2164 Nam := First (Subtype_Marks (Item));
2165 while Present (Nam) loop
2166 Analyze (Nam);
2167 Next (Nam);
2168 end loop;
2169 end if;
2171 Next (Item);
2172 end loop;
2174 -- Reset visibility of withed units. They will be made visible again
2175 -- when we install the subunit context.
2177 Item := First (Context_Items (N));
2178 while Present (Item) loop
2179 if Nkind (Item) = N_With_Clause
2181 -- Protect frontend against previous errors in context clauses
2183 and then Nkind (Name (Item)) /= N_Selected_Component
2184 and then not Error_Posted (Item)
2185 then
2186 Unit_Name := Entity (Name (Item));
2187 loop
2188 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2189 exit when Scope (Unit_Name) = Standard_Standard;
2190 Unit_Name := Scope (Unit_Name);
2191 end loop;
2193 if Context_Installed (Item) then
2194 Set_Is_Immediately_Visible (Unit_Name, False);
2195 Set_Context_Installed (Item, False);
2196 end if;
2197 end if;
2199 Next (Item);
2200 end loop;
2201 end Analyze_Subunit_Context;
2203 ------------------------
2204 -- Re_Install_Parents --
2205 ------------------------
2207 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2208 E : Entity_Id;
2210 begin
2211 if Nkind (Unit (L)) = N_Subunit then
2212 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2213 end if;
2215 Install_Context (L);
2217 -- If the subunit occurs within a child unit, we must restore the
2218 -- immediate visibility of any siblings that may occur in context.
2220 if Present (Enclosing_Child) then
2221 Install_Siblings (Enclosing_Child, L);
2222 end if;
2224 Push_Scope (Scop);
2226 if Scop /= Par_Unit then
2227 Set_Is_Immediately_Visible (Scop);
2228 end if;
2230 -- Make entities in scope visible again. For child units, restore
2231 -- visibility only if they are actually in context.
2233 E := First_Entity (Current_Scope);
2234 while Present (E) loop
2235 if not Is_Child_Unit (E) or else Is_Visible_Lib_Unit (E) then
2236 Set_Is_Immediately_Visible (E);
2237 end if;
2239 Next_Entity (E);
2240 end loop;
2242 -- A subunit appears within a body, and for a nested subunits all the
2243 -- parents are bodies. Restore full visibility of their private
2244 -- entities.
2246 if Is_Package_Or_Generic_Package (Scop) then
2247 Set_In_Package_Body (Scop);
2248 Install_Private_Declarations (Scop);
2249 end if;
2250 end Re_Install_Parents;
2252 ----------------------------
2253 -- Re_Install_Use_Clauses --
2254 ----------------------------
2256 procedure Re_Install_Use_Clauses is
2257 U : Node_Id;
2258 begin
2259 for J in reverse 1 .. Num_Scopes loop
2260 U := Use_Clauses (J);
2261 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2262 Install_Use_Clauses (U, Force_Installation => True);
2263 end loop;
2264 end Re_Install_Use_Clauses;
2266 ------------------
2267 -- Remove_Scope --
2268 ------------------
2270 procedure Remove_Scope is
2271 E : Entity_Id;
2273 begin
2274 Num_Scopes := Num_Scopes + 1;
2275 Use_Clauses (Num_Scopes) :=
2276 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2278 E := First_Entity (Current_Scope);
2279 while Present (E) loop
2280 Set_Is_Immediately_Visible (E, False);
2281 Next_Entity (E);
2282 end loop;
2284 if Is_Child_Unit (Current_Scope) then
2285 Enclosing_Child := Current_Scope;
2286 end if;
2288 Pop_Scope;
2289 end Remove_Scope;
2291 Saved_SM : SPARK_Mode_Type := SPARK_Mode;
2292 Saved_SMP : Node_Id := SPARK_Mode_Pragma;
2293 -- Save the SPARK mode-related data to restore on exit. Removing
2294 -- enclosing scopes and contexts to provide a clean environment for the
2295 -- context of the subunit will eliminate any previously set SPARK_Mode.
2297 -- Start of processing for Analyze_Subunit
2299 begin
2300 -- For subunit in main extended unit, we reset the configuration values
2301 -- for the non-partition-wide restrictions. For other units reset them.
2303 if In_Extended_Main_Source_Unit (N) then
2304 Restore_Config_Cunit_Boolean_Restrictions;
2305 else
2306 Reset_Cunit_Boolean_Restrictions;
2307 end if;
2309 if Style_Check then
2310 declare
2311 Nam : Node_Id := Name (Unit (N));
2313 begin
2314 if Nkind (Nam) = N_Selected_Component then
2315 Nam := Selector_Name (Nam);
2316 end if;
2318 Check_Identifier (Nam, Par_Unit);
2319 end;
2320 end if;
2322 if not Is_Empty_List (Context_Items (N)) then
2324 -- Save current use clauses
2326 Remove_Scope;
2327 Remove_Context (Lib_Unit);
2329 -- Now remove parents and their context, including enclosing subunits
2330 -- and the outer parent body which is not a subunit.
2332 if Present (Lib_Spec) then
2333 Remove_Context (Lib_Spec);
2335 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2336 Lib_Spec := Library_Unit (Lib_Spec);
2337 Remove_Scope;
2338 Remove_Context (Lib_Spec);
2339 end loop;
2341 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2342 Remove_Scope;
2343 end if;
2345 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2346 Remove_Context (Library_Unit (Lib_Spec));
2347 end if;
2348 end if;
2350 Set_Is_Immediately_Visible (Par_Unit, False);
2352 Analyze_Subunit_Context;
2354 -- Take into account the effect of any SPARK_Mode configuration
2355 -- pragma, which takes precedence over a different value of
2356 -- SPARK_Mode inherited from the context of the stub.
2358 if SPARK_Mode /= None then
2359 Saved_SM := SPARK_Mode;
2360 Saved_SMP := SPARK_Mode_Pragma;
2361 end if;
2363 Re_Install_Parents (Lib_Unit, Par_Unit);
2364 Set_Is_Immediately_Visible (Par_Unit);
2366 -- If the context includes a child unit of the parent of the subunit,
2367 -- the parent will have been removed from visibility, after compiling
2368 -- that cousin in the context. The visibility of the parent must be
2369 -- restored now. This also applies if the context includes another
2370 -- subunit of the same parent which in turn includes a child unit in
2371 -- its context.
2373 if Is_Package_Or_Generic_Package (Par_Unit) then
2374 if not Is_Immediately_Visible (Par_Unit)
2375 or else (Present (First_Entity (Par_Unit))
2376 and then not
2377 Is_Immediately_Visible (First_Entity (Par_Unit)))
2378 then
2379 Set_Is_Immediately_Visible (Par_Unit);
2380 Install_Visible_Declarations (Par_Unit);
2381 Install_Private_Declarations (Par_Unit);
2382 end if;
2383 end if;
2385 Re_Install_Use_Clauses;
2386 Install_Context (N);
2388 -- Restore state of suppress flags for current body
2390 Scope_Suppress := Svg;
2392 -- If the subunit is within a child unit, then siblings of any parent
2393 -- unit that appear in the context clause of the subunit must also be
2394 -- made immediately visible.
2396 if Present (Enclosing_Child) then
2397 Install_Siblings (Enclosing_Child, N);
2398 end if;
2399 end if;
2401 Generate_Parent_References (Unit (N), Par_Unit);
2403 -- Reinstall the SPARK_Mode which was in effect prior to any scope and
2404 -- context manipulations, taking into account a possible SPARK_Mode
2405 -- configuration pragma if present.
2407 Install_SPARK_Mode (Saved_SM, Saved_SMP);
2409 Analyze (Proper_Body (Unit (N)));
2410 Remove_Context (N);
2412 -- The subunit may contain a with_clause on a sibling of some ancestor.
2413 -- Removing the context will remove from visibility those ancestor child
2414 -- units, which must be restored to the visibility they have in the
2415 -- enclosing body.
2417 if Present (Enclosing_Child) then
2418 declare
2419 C : Entity_Id;
2420 begin
2421 C := Current_Scope;
2422 while Present (C) and then C /= Standard_Standard loop
2423 Set_Is_Immediately_Visible (C);
2424 Set_Is_Visible_Lib_Unit (C);
2425 C := Scope (C);
2426 end loop;
2427 end;
2428 end if;
2430 -- Deal with restore of restrictions
2432 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2433 end Analyze_Subunit;
2435 ----------------------------
2436 -- Analyze_Task_Body_Stub --
2437 ----------------------------
2439 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2440 Loc : constant Source_Ptr := Sloc (N);
2441 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2443 begin
2444 Check_Stub_Level (N);
2446 -- First occurrence of name may have been as an incomplete type
2448 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2449 Nam := Full_View (Nam);
2450 end if;
2452 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2453 Error_Msg_N ("missing specification for task body", N);
2455 else
2456 Set_Scope (Defining_Entity (N), Current_Scope);
2457 Set_Ekind (Defining_Entity (N), E_Task_Body);
2458 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2459 Set_Corresponding_Spec_Of_Stub (N, Nam);
2461 -- Check for duplicate stub, if so give message and terminate
2463 if Has_Completion (Etype (Nam)) then
2464 Error_Msg_N ("duplicate stub for task", N);
2465 return;
2466 else
2467 Set_Has_Completion (Etype (Nam));
2468 end if;
2470 Analyze_Proper_Body (N, Etype (Nam));
2472 -- Set elaboration flag to indicate that entity is callable. This
2473 -- cannot be done in the expansion of the body itself, because the
2474 -- proper body is not in a declarative part. This is only done if
2475 -- expansion is active, because the context may be generic and the
2476 -- flag not defined yet.
2478 if Expander_Active then
2479 Insert_After (N,
2480 Make_Assignment_Statement (Loc,
2481 Name =>
2482 Make_Identifier (Loc,
2483 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2484 Expression => New_Occurrence_Of (Standard_True, Loc)));
2485 end if;
2486 end if;
2487 end Analyze_Task_Body_Stub;
2489 -------------------------
2490 -- Analyze_With_Clause --
2491 -------------------------
2493 -- Analyze the declaration of a unit in a with clause. At end, label the
2494 -- with clause with the defining entity for the unit.
2496 procedure Analyze_With_Clause (N : Node_Id) is
2498 -- Retrieve the original kind of the unit node, before analysis. If it
2499 -- is a subprogram instantiation, its analysis below will rewrite the
2500 -- node as the declaration of the wrapper package. If the same
2501 -- instantiation appears indirectly elsewhere in the context, it will
2502 -- have been analyzed already.
2504 Unit_Kind : constant Node_Kind :=
2505 Nkind (Original_Node (Unit (Library_Unit (N))));
2506 Nam : constant Node_Id := Name (N);
2507 E_Name : Entity_Id;
2508 Par_Name : Entity_Id;
2509 Pref : Node_Id;
2510 U : Node_Id;
2512 Intunit : Boolean;
2513 -- Set True if the unit currently being compiled is an internal unit
2515 Restriction_Violation : Boolean := False;
2516 -- Set True if a with violates a restriction, no point in giving any
2517 -- warnings if we have this definite error.
2519 Save_Style_Check : constant Boolean := Opt.Style_Check;
2521 begin
2522 U := Unit (Library_Unit (N));
2524 -- If this is an internal unit which is a renaming, then this is a
2525 -- violation of No_Obsolescent_Features.
2527 -- Note: this is not quite right if the user defines one of these units
2528 -- himself, but that's a marginal case, and fixing it is hard ???
2530 if Restriction_Check_Required (No_Obsolescent_Features) then
2531 if In_Predefined_Renaming (U) then
2532 Check_Restriction (No_Obsolescent_Features, N);
2533 Restriction_Violation := True;
2534 end if;
2535 end if;
2537 -- Check No_Implementation_Units violation
2539 if Restriction_Check_Required (No_Implementation_Units) then
2540 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2541 null;
2542 else
2543 Check_Restriction (No_Implementation_Units, Nam);
2544 Restriction_Violation := True;
2545 end if;
2546 end if;
2548 -- Several actions are skipped for dummy packages (those supplied for
2549 -- with's where no matching file could be found). Such packages are
2550 -- identified by the Sloc value being set to No_Location.
2552 if Limited_Present (N) then
2554 -- Ada 2005 (AI-50217): Build visibility structures but do not
2555 -- analyze the unit.
2557 -- If the designated unit is a predefined unit, which might be used
2558 -- implicitly through the rtsfind machinery, a limited with clause
2559 -- on such a unit is usually pointless, because run-time units are
2560 -- unlikely to appear in mutually dependent units, and because this
2561 -- disables the rtsfind mechanism. We transform such limited with
2562 -- clauses into regular with clauses.
2564 if Sloc (U) /= No_Location then
2565 if In_Predefined_Unit (U)
2567 -- In ASIS mode the rtsfind mechanism plays no role, and
2568 -- we need to maintain the original tree structure, so
2569 -- this transformation is not performed in this case.
2571 and then not ASIS_Mode
2572 then
2573 Set_Limited_Present (N, False);
2574 Analyze_With_Clause (N);
2575 else
2576 Build_Limited_Views (N);
2577 end if;
2578 end if;
2580 return;
2581 end if;
2583 -- If we are compiling under "don't quit" mode (-gnatq) and we have
2584 -- already detected serious errors then we mark the with-clause nodes as
2585 -- analyzed before the corresponding compilation unit is analyzed. This
2586 -- is done here to protect the frontend against never ending recursion
2587 -- caused by circularities in the sources (because the previous errors
2588 -- may break the regular machine of the compiler implemented in
2589 -- Load_Unit to detect circularities).
2591 if Serious_Errors_Detected > 0 and then Try_Semantics then
2592 Set_Analyzed (N);
2593 end if;
2595 Semantics (Library_Unit (N));
2597 Intunit := Is_Internal_Unit (Current_Sem_Unit);
2599 if Sloc (U) /= No_Location then
2601 -- Check restrictions, except that we skip the check if this is an
2602 -- internal unit unless we are compiling the internal unit as the
2603 -- main unit. We also skip this for dummy packages.
2605 Check_Restriction_No_Dependence (Nam, N);
2607 if not Intunit or else Current_Sem_Unit = Main_Unit then
2608 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2609 end if;
2611 -- Deal with special case of GNAT.Current_Exceptions which interacts
2612 -- with the optimization of local raise statements into gotos.
2614 if Nkind (Nam) = N_Selected_Component
2615 and then Nkind (Prefix (Nam)) = N_Identifier
2616 and then Chars (Prefix (Nam)) = Name_Gnat
2617 and then Nam_In (Chars (Selector_Name (Nam)),
2618 Name_Most_Recent_Exception,
2619 Name_Exception_Traces)
2620 then
2621 Check_Restriction (No_Exception_Propagation, N);
2622 Special_Exception_Package_Used := True;
2623 end if;
2625 -- Check for inappropriate with of internal implementation unit if we
2626 -- are not compiling an internal unit and also check for withing unit
2627 -- in wrong version of Ada. Do not issue these messages for implicit
2628 -- with's generated by the compiler itself.
2630 if Implementation_Unit_Warnings
2631 and then not Intunit
2632 and then not Implicit_With (N)
2633 and then not Restriction_Violation
2634 then
2635 declare
2636 U_Kind : constant Kind_Of_Unit :=
2637 Get_Kind_Of_Unit (Get_Source_Unit (U));
2639 begin
2640 if U_Kind = Implementation_Unit then
2641 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2643 -- Add alternative name if available, otherwise issue a
2644 -- general warning message.
2646 if Error_Msg_Strlen /= 0 then
2647 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2648 else
2649 Error_Msg_F
2650 ("\use of this unit is non-portable " &
2651 "and version-dependent?i?", Name (N));
2652 end if;
2654 elsif U_Kind = Ada_2005_Unit
2655 and then Ada_Version < Ada_2005
2656 and then Warn_On_Ada_2005_Compatibility
2657 then
2658 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2660 elsif U_Kind = Ada_2012_Unit
2661 and then Ada_Version < Ada_2012
2662 and then Warn_On_Ada_2012_Compatibility
2663 then
2664 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2665 end if;
2666 end;
2667 end if;
2668 end if;
2670 -- Semantic analysis of a generic unit is performed on a copy of
2671 -- the original tree. Retrieve the entity on which semantic info
2672 -- actually appears.
2674 if Unit_Kind in N_Generic_Declaration then
2675 E_Name := Defining_Entity (U);
2677 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2678 -- the case of an instantiation, semantic analysis above will have
2679 -- replaced the unit by its instantiated version. If the instance body
2680 -- has been generated, the instance now denotes the body entity. For
2681 -- visibility purposes we need the entity of its spec.
2683 elsif (Unit_Kind = N_Package_Instantiation
2684 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2685 N_Package_Instantiation)
2686 and then Nkind (U) = N_Package_Body
2687 then
2688 E_Name := Corresponding_Spec (U);
2690 elsif Unit_Kind = N_Package_Instantiation
2691 and then Nkind (U) = N_Package_Instantiation
2692 and then Present (Instance_Spec (U))
2693 then
2694 -- If the instance has not been rewritten as a package declaration,
2695 -- then it appeared already in a previous with clause. Retrieve
2696 -- the entity from the previous instance.
2698 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2700 elsif Unit_Kind in N_Subprogram_Instantiation then
2702 -- The visible subprogram is created during instantiation, and is
2703 -- an attribute of the wrapper package. We retrieve the wrapper
2704 -- package directly from the instantiation node. If the instance
2705 -- is inlined the unit is still an instantiation. Otherwise it has
2706 -- been rewritten as the declaration of the wrapper itself.
2708 if Nkind (U) in N_Subprogram_Instantiation then
2709 E_Name :=
2710 Related_Instance
2711 (Defining_Entity (Specification (Instance_Spec (U))));
2712 else
2713 E_Name := Related_Instance (Defining_Entity (U));
2714 end if;
2716 elsif Unit_Kind = N_Package_Renaming_Declaration
2717 or else Unit_Kind in N_Generic_Renaming_Declaration
2718 then
2719 E_Name := Defining_Entity (U);
2721 elsif Unit_Kind = N_Subprogram_Body
2722 and then Nkind (Name (N)) = N_Selected_Component
2723 and then not Acts_As_Spec (Library_Unit (N))
2724 then
2725 -- For a child unit that has no spec, one has been created and
2726 -- analyzed. The entity required is that of the spec.
2728 E_Name := Corresponding_Spec (U);
2730 else
2731 E_Name := Defining_Entity (U);
2732 end if;
2734 if Nkind (Name (N)) = N_Selected_Component then
2736 -- Child unit in a with clause
2738 Change_Selected_Component_To_Expanded_Name (Name (N));
2740 -- If this is a child unit without a spec, and it has been analyzed
2741 -- already, a declaration has been created for it. The with_clause
2742 -- must reflect the actual body, and not the generated declaration,
2743 -- to prevent spurious binding errors involving an out-of-date spec.
2744 -- Note that this can only happen if the unit includes more than one
2745 -- with_clause for the child unit (e.g. in separate subunits).
2747 if Unit_Kind = N_Subprogram_Declaration
2748 and then Analyzed (Library_Unit (N))
2749 and then not Comes_From_Source (Library_Unit (N))
2750 then
2751 Set_Library_Unit (N,
2752 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2753 end if;
2754 end if;
2756 -- Restore style checks
2758 Style_Check := Save_Style_Check;
2760 -- Record the reference, but do NOT set the unit as referenced, we want
2761 -- to consider the unit as unreferenced if this is the only reference
2762 -- that occurs.
2764 Set_Entity_With_Checks (Name (N), E_Name);
2765 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2767 -- Generate references and check No_Dependence restriction for parents
2769 if Is_Child_Unit (E_Name) then
2770 Pref := Prefix (Name (N));
2771 Par_Name := Scope (E_Name);
2772 while Nkind (Pref) = N_Selected_Component loop
2773 Change_Selected_Component_To_Expanded_Name (Pref);
2775 if Present (Entity (Selector_Name (Pref)))
2776 and then
2777 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2778 and then Entity (Selector_Name (Pref)) /= Par_Name
2779 then
2780 -- The prefix is a child unit that denotes a renaming declaration.
2781 -- Replace the prefix directly with the renamed unit, because the
2782 -- rest of the prefix is irrelevant to the visibility of the real
2783 -- unit.
2785 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2786 exit;
2787 end if;
2789 Set_Entity_With_Checks (Pref, Par_Name);
2791 Generate_Reference (Par_Name, Pref);
2792 Check_Restriction_No_Dependence (Pref, N);
2793 Pref := Prefix (Pref);
2795 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2796 -- is set to Standard_Standard, and no attempt should be made to
2797 -- further unwind scopes.
2799 if Par_Name /= Standard_Standard then
2800 Par_Name := Scope (Par_Name);
2801 end if;
2803 -- Abandon processing in case of previous errors
2805 if No (Par_Name) then
2806 Check_Error_Detected;
2807 return;
2808 end if;
2809 end loop;
2811 if Present (Entity (Pref))
2812 and then not Analyzed (Parent (Parent (Entity (Pref))))
2813 then
2814 -- If the entity is set without its unit being compiled, the
2815 -- original parent is a renaming, and Par_Name is the renamed
2816 -- entity. For visibility purposes, we need the original entity,
2817 -- which must be analyzed now because Load_Unit directly retrieves
2818 -- the renamed unit, and the renaming declaration itself has not
2819 -- been analyzed.
2821 Analyze (Parent (Parent (Entity (Pref))));
2822 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2823 Par_Name := Entity (Pref);
2824 end if;
2826 -- Guard against missing or misspelled child units
2828 if Present (Par_Name) then
2829 Set_Entity_With_Checks (Pref, Par_Name);
2830 Generate_Reference (Par_Name, Pref);
2832 else
2833 pragma Assert (Serious_Errors_Detected /= 0);
2835 -- Mark the node to indicate that a related error has been posted.
2836 -- This defends further compilation passes against improper use of
2837 -- the invalid WITH clause node.
2839 Set_Error_Posted (N);
2840 Set_Name (N, Error);
2841 return;
2842 end if;
2843 end if;
2845 -- If the withed unit is System, and a system extension pragma is
2846 -- present, compile the extension now, rather than waiting for a
2847 -- visibility check on a specific entity.
2849 if Chars (E_Name) = Name_System
2850 and then Scope (E_Name) = Standard_Standard
2851 and then Present (System_Extend_Unit)
2852 and then Present_System_Aux (N)
2853 then
2854 -- If the extension is not present, an error will have been emitted
2856 null;
2857 end if;
2859 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2860 -- to private_with units; they will be made visible later (just before
2861 -- the private part is analyzed)
2863 if Private_Present (N) then
2864 Set_Is_Immediately_Visible (E_Name, False);
2865 end if;
2867 -- Propagate Fatal_Error setting from with'ed unit to current unit
2869 case Fatal_Error (Get_Source_Unit (Library_Unit (N))) is
2871 -- Nothing to do if with'ed unit had no error
2873 when None =>
2874 null;
2876 -- If with'ed unit had a detected fatal error, propagate it
2878 when Error_Detected =>
2879 Set_Fatal_Error (Current_Sem_Unit, Error_Detected);
2881 -- If with'ed unit had an ignored error, then propagate it but do not
2882 -- overide an existring setting.
2884 when Error_Ignored =>
2885 if Fatal_Error (Current_Sem_Unit) = None then
2886 Set_Fatal_Error (Current_Sem_Unit, Error_Ignored);
2887 end if;
2888 end case;
2890 Mark_Ghost_Clause (N);
2891 end Analyze_With_Clause;
2893 ------------------------------
2894 -- Check_Private_Child_Unit --
2895 ------------------------------
2897 procedure Check_Private_Child_Unit (N : Node_Id) is
2898 Lib_Unit : constant Node_Id := Unit (N);
2899 Item : Node_Id;
2900 Curr_Unit : Entity_Id;
2901 Sub_Parent : Node_Id;
2902 Priv_Child : Entity_Id;
2903 Par_Lib : Entity_Id;
2904 Par_Spec : Node_Id;
2906 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2907 -- Returns true if and only if the library unit is declared with
2908 -- an explicit designation of private.
2910 -----------------------------
2911 -- Is_Private_Library_Unit --
2912 -----------------------------
2914 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2915 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2917 begin
2918 return Private_Present (Comp_Unit);
2919 end Is_Private_Library_Unit;
2921 -- Start of processing for Check_Private_Child_Unit
2923 begin
2924 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2925 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2926 Par_Lib := Curr_Unit;
2928 elsif Nkind (Lib_Unit) = N_Subunit then
2930 -- The parent is itself a body. The parent entity is to be found in
2931 -- the corresponding spec.
2933 Sub_Parent := Library_Unit (N);
2934 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2936 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2937 -- enclosing body, retrieve the spec entity which is the proper
2938 -- ancestor we need for the following tests.
2940 if Ekind (Curr_Unit) = E_Package_Body then
2941 Curr_Unit := Spec_Entity (Curr_Unit);
2942 end if;
2944 Par_Lib := Curr_Unit;
2946 else
2947 Curr_Unit := Defining_Entity (Lib_Unit);
2949 Par_Lib := Curr_Unit;
2950 Par_Spec := Parent_Spec (Lib_Unit);
2952 if No (Par_Spec) then
2953 Par_Lib := Empty;
2954 else
2955 Par_Lib := Defining_Entity (Unit (Par_Spec));
2956 end if;
2957 end if;
2959 -- Loop through context items
2961 Item := First (Context_Items (N));
2962 while Present (Item) loop
2964 -- Ada 2005 (AI-262): Allow private_with of a private child package
2965 -- in public siblings
2967 if Nkind (Item) = N_With_Clause
2968 and then not Implicit_With (Item)
2969 and then not Limited_Present (Item)
2970 and then Is_Private_Descendant (Entity (Name (Item)))
2971 then
2972 Priv_Child := Entity (Name (Item));
2974 declare
2975 Curr_Parent : Entity_Id := Par_Lib;
2976 Child_Parent : Entity_Id := Scope (Priv_Child);
2977 Prv_Ancestor : Entity_Id := Child_Parent;
2978 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2980 begin
2981 -- If the child unit is a public child then locate the nearest
2982 -- private ancestor. Child_Parent will then be set to the
2983 -- parent of that ancestor.
2985 if not Is_Private_Library_Unit (Priv_Child) then
2986 while Present (Prv_Ancestor)
2987 and then not Is_Private_Library_Unit (Prv_Ancestor)
2988 loop
2989 Prv_Ancestor := Scope (Prv_Ancestor);
2990 end loop;
2992 if Present (Prv_Ancestor) then
2993 Child_Parent := Scope (Prv_Ancestor);
2994 end if;
2995 end if;
2997 while Present (Curr_Parent)
2998 and then Curr_Parent /= Standard_Standard
2999 and then Curr_Parent /= Child_Parent
3000 loop
3001 Curr_Private :=
3002 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
3003 Curr_Parent := Scope (Curr_Parent);
3004 end loop;
3006 if No (Curr_Parent) then
3007 Curr_Parent := Standard_Standard;
3008 end if;
3010 if Curr_Parent /= Child_Parent then
3011 if Ekind (Priv_Child) = E_Generic_Package
3012 and then Chars (Priv_Child) in Text_IO_Package_Name
3013 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
3014 then
3015 Error_Msg_NE
3016 ("& is a nested package, not a compilation unit",
3017 Name (Item), Priv_Child);
3019 else
3020 Error_Msg_N
3021 ("unit in with clause is private child unit!", Item);
3022 Error_Msg_NE
3023 ("\current unit must also have parent&!",
3024 Item, Child_Parent);
3025 end if;
3027 elsif Curr_Private
3028 or else Private_Present (Item)
3029 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
3030 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3031 and then not Acts_As_Spec (Parent (Lib_Unit)))
3032 then
3033 null;
3035 else
3036 Error_Msg_NE
3037 ("current unit must also be private descendant of&",
3038 Item, Child_Parent);
3039 end if;
3040 end;
3041 end if;
3043 Next (Item);
3044 end loop;
3046 end Check_Private_Child_Unit;
3048 ----------------------
3049 -- Check_Stub_Level --
3050 ----------------------
3052 procedure Check_Stub_Level (N : Node_Id) is
3053 Par : constant Node_Id := Parent (N);
3054 Kind : constant Node_Kind := Nkind (Par);
3056 begin
3057 if Nkind_In (Kind, N_Package_Body,
3058 N_Subprogram_Body,
3059 N_Task_Body,
3060 N_Protected_Body)
3061 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
3062 then
3063 null;
3065 -- In an instance, a missing stub appears at any level. A warning
3066 -- message will have been emitted already for the missing file.
3068 elsif not In_Instance then
3069 Error_Msg_N ("stub cannot appear in an inner scope", N);
3071 elsif Expander_Active then
3072 Error_Msg_N ("missing proper body", N);
3073 end if;
3074 end Check_Stub_Level;
3076 ------------------------
3077 -- Expand_With_Clause --
3078 ------------------------
3080 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
3081 Loc : constant Source_Ptr := Sloc (Nam);
3082 Ent : constant Entity_Id := Entity (Nam);
3083 Withn : Node_Id;
3084 P : Node_Id;
3086 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
3087 -- Build name to be used in implicit with_clause. In most cases this
3088 -- is the source name, but if renamings are present we must make the
3089 -- original unit visible, not the one it renames. The entity in the
3090 -- with clause is the renamed unit, but the identifier is the one from
3091 -- the source, which allows us to recover the unit renaming.
3093 ---------------------
3094 -- Build_Unit_Name --
3095 ---------------------
3097 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
3098 Ent : Entity_Id;
3099 Result : Node_Id;
3101 begin
3102 if Nkind (Nam) = N_Identifier then
3103 return New_Occurrence_Of (Entity (Nam), Loc);
3105 else
3106 Ent := Entity (Nam);
3108 if Present (Entity (Selector_Name (Nam)))
3109 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
3110 and then
3111 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
3112 = N_Package_Renaming_Declaration
3113 then
3114 -- The name in the with_clause is of the form A.B.C, and B is
3115 -- given by a renaming declaration. In that case we may not
3116 -- have analyzed the unit for B, but replaced it directly in
3117 -- lib-load with the unit it renames. We have to make A.B
3118 -- visible, so analyze the declaration for B now, in case it
3119 -- has not been done yet.
3121 Ent := Entity (Selector_Name (Nam));
3122 Analyze
3123 (Parent
3124 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3125 end if;
3127 Result :=
3128 Make_Expanded_Name (Loc,
3129 Chars => Chars (Entity (Nam)),
3130 Prefix => Build_Unit_Name (Prefix (Nam)),
3131 Selector_Name => New_Occurrence_Of (Ent, Loc));
3132 Set_Entity (Result, Ent);
3133 return Result;
3134 end if;
3135 end Build_Unit_Name;
3137 -- Start of processing for Expand_With_Clause
3139 begin
3140 Withn :=
3141 Make_With_Clause (Loc,
3142 Name => Build_Unit_Name (Nam));
3144 P := Parent (Unit_Declaration_Node (Ent));
3145 Set_Library_Unit (Withn, P);
3146 Set_Corresponding_Spec (Withn, Ent);
3147 Set_First_Name (Withn, True);
3148 Set_Implicit_With (Withn, True);
3150 -- If the unit is a package or generic package declaration, a private_
3151 -- with_clause on a child unit implies that the implicit with on the
3152 -- parent is also private.
3154 if Nkind_In (Unit (N), N_Package_Declaration,
3155 N_Generic_Package_Declaration)
3156 then
3157 Set_Private_Present (Withn, Private_Present (Item));
3158 end if;
3160 Prepend (Withn, Context_Items (N));
3161 Mark_Rewrite_Insertion (Withn);
3162 Install_Withed_Unit (Withn);
3164 -- If we have "with X.Y;", we want to recurse on "X", except in the
3165 -- unusual case where X.Y is a renaming of X. In that case, the scope
3166 -- of X will be null.
3168 if Nkind (Nam) = N_Expanded_Name
3169 and then Present (Scope (Entity (Prefix (Nam))))
3170 then
3171 Expand_With_Clause (Item, Prefix (Nam), N);
3172 end if;
3173 end Expand_With_Clause;
3175 --------------------------------
3176 -- Generate_Parent_References --
3177 --------------------------------
3179 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
3180 Pref : Node_Id;
3181 P_Name : Entity_Id := P_Id;
3183 begin
3184 if Nkind (N) = N_Subunit then
3185 Pref := Name (N);
3186 else
3187 Pref := Name (Parent (Defining_Entity (N)));
3188 end if;
3190 if Nkind (Pref) = N_Expanded_Name then
3192 -- Done already, if the unit has been compiled indirectly as
3193 -- part of the closure of its context because of inlining.
3195 return;
3196 end if;
3198 while Nkind (Pref) = N_Selected_Component loop
3199 Change_Selected_Component_To_Expanded_Name (Pref);
3200 Set_Entity (Pref, P_Name);
3201 Set_Etype (Pref, Etype (P_Name));
3202 Generate_Reference (P_Name, Pref, 'r');
3203 Pref := Prefix (Pref);
3204 P_Name := Scope (P_Name);
3205 end loop;
3207 -- The guard here on P_Name is to handle the error condition where
3208 -- the parent unit is missing because the file was not found.
3210 if Present (P_Name) then
3211 Set_Entity (Pref, P_Name);
3212 Set_Etype (Pref, Etype (P_Name));
3213 Generate_Reference (P_Name, Pref, 'r');
3214 Style.Check_Identifier (Pref, P_Name);
3215 end if;
3216 end Generate_Parent_References;
3218 ---------------------
3219 -- Has_With_Clause --
3220 ---------------------
3222 function Has_With_Clause
3223 (C_Unit : Node_Id;
3224 Pack : Entity_Id;
3225 Is_Limited : Boolean := False) return Boolean
3227 Item : Node_Id;
3229 function Named_Unit (Clause : Node_Id) return Entity_Id;
3230 -- Return the entity for the unit named in a [limited] with clause
3232 ----------------
3233 -- Named_Unit --
3234 ----------------
3236 function Named_Unit (Clause : Node_Id) return Entity_Id is
3237 begin
3238 if Nkind (Name (Clause)) = N_Selected_Component then
3239 return Entity (Selector_Name (Name (Clause)));
3240 else
3241 return Entity (Name (Clause));
3242 end if;
3243 end Named_Unit;
3245 -- Start of processing for Has_With_Clause
3247 begin
3248 if Present (Context_Items (C_Unit)) then
3249 Item := First (Context_Items (C_Unit));
3250 while Present (Item) loop
3251 if Nkind (Item) = N_With_Clause
3252 and then Limited_Present (Item) = Is_Limited
3253 and then Named_Unit (Item) = Pack
3254 then
3255 return True;
3256 end if;
3258 Next (Item);
3259 end loop;
3260 end if;
3262 return False;
3263 end Has_With_Clause;
3265 -----------------------------
3266 -- Implicit_With_On_Parent --
3267 -----------------------------
3269 procedure Implicit_With_On_Parent
3270 (Child_Unit : Node_Id;
3271 N : Node_Id)
3273 Loc : constant Source_Ptr := Sloc (N);
3274 P : constant Node_Id := Parent_Spec (Child_Unit);
3275 P_Unit : Node_Id := Unit (P);
3276 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3277 Withn : Node_Id;
3279 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3280 -- Build prefix of child unit name. Recurse if needed
3282 function Build_Unit_Name return Node_Id;
3283 -- If the unit is a child unit, build qualified name with all ancestors
3285 -------------------------
3286 -- Build_Ancestor_Name --
3287 -------------------------
3289 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3290 P_Ref : constant Node_Id :=
3291 New_Occurrence_Of (Defining_Entity (P), Loc);
3292 P_Spec : Node_Id := P;
3294 begin
3295 -- Ancestor may have been rewritten as a package body. Retrieve
3296 -- the original spec to trace earlier ancestors.
3298 if Nkind (P) = N_Package_Body
3299 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3300 then
3301 P_Spec := Original_Node (P);
3302 end if;
3304 if No (Parent_Spec (P_Spec)) then
3305 return P_Ref;
3306 else
3307 return
3308 Make_Selected_Component (Loc,
3309 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3310 Selector_Name => P_Ref);
3311 end if;
3312 end Build_Ancestor_Name;
3314 ---------------------
3315 -- Build_Unit_Name --
3316 ---------------------
3318 function Build_Unit_Name return Node_Id is
3319 Result : Node_Id;
3321 begin
3322 if No (Parent_Spec (P_Unit)) then
3323 return New_Occurrence_Of (P_Name, Loc);
3325 else
3326 Result :=
3327 Make_Expanded_Name (Loc,
3328 Chars => Chars (P_Name),
3329 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3330 Selector_Name => New_Occurrence_Of (P_Name, Loc));
3331 Set_Entity (Result, P_Name);
3332 return Result;
3333 end if;
3334 end Build_Unit_Name;
3336 -- Start of processing for Implicit_With_On_Parent
3338 begin
3339 -- The unit of the current compilation may be a package body that
3340 -- replaces an instance node. In this case we need the original instance
3341 -- node to construct the proper parent name.
3343 if Nkind (P_Unit) = N_Package_Body
3344 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3345 then
3346 P_Unit := Original_Node (P_Unit);
3347 end if;
3349 -- We add the implicit with if the child unit is the current unit being
3350 -- compiled. If the current unit is a body, we do not want to add an
3351 -- implicit_with a second time to the corresponding spec.
3353 if Nkind (Child_Unit) = N_Package_Declaration
3354 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3355 then
3356 return;
3357 end if;
3359 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3361 Set_Library_Unit (Withn, P);
3362 Set_Corresponding_Spec (Withn, P_Name);
3363 Set_First_Name (Withn, True);
3364 Set_Implicit_With (Withn, True);
3366 -- Node is placed at the beginning of the context items, so that
3367 -- subsequent use clauses on the parent can be validated.
3369 Prepend (Withn, Context_Items (N));
3370 Mark_Rewrite_Insertion (Withn);
3371 Install_Withed_Unit (Withn);
3373 if Is_Child_Spec (P_Unit) then
3374 Implicit_With_On_Parent (P_Unit, N);
3375 end if;
3376 end Implicit_With_On_Parent;
3378 --------------
3379 -- In_Chain --
3380 --------------
3382 function In_Chain (E : Entity_Id) return Boolean is
3383 H : Entity_Id;
3385 begin
3386 H := Current_Entity (E);
3387 while Present (H) loop
3388 if H = E then
3389 return True;
3390 else
3391 H := Homonym (H);
3392 end if;
3393 end loop;
3395 return False;
3396 end In_Chain;
3398 ---------------------
3399 -- Install_Context --
3400 ---------------------
3402 procedure Install_Context (N : Node_Id) is
3403 Lib_Unit : constant Node_Id := Unit (N);
3405 begin
3406 Install_Context_Clauses (N);
3408 if Is_Child_Spec (Lib_Unit) then
3409 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3410 end if;
3412 Install_Limited_Context_Clauses (N);
3413 end Install_Context;
3415 -----------------------------
3416 -- Install_Context_Clauses --
3417 -----------------------------
3419 procedure Install_Context_Clauses (N : Node_Id) is
3420 Lib_Unit : constant Node_Id := Unit (N);
3421 Item : Node_Id;
3422 Uname_Node : Entity_Id;
3423 Check_Private : Boolean := False;
3424 Decl_Node : Node_Id;
3425 Lib_Parent : Entity_Id;
3427 begin
3428 -- First skip configuration pragmas at the start of the context. They
3429 -- are not technically part of the context clause, but that's where the
3430 -- parser puts them. Note they were analyzed in Analyze_Context.
3432 Item := First (Context_Items (N));
3433 while Present (Item)
3434 and then Nkind (Item) = N_Pragma
3435 and then Pragma_Name (Item) in Configuration_Pragma_Names
3436 loop
3437 Next (Item);
3438 end loop;
3440 -- Loop through the actual context clause items. We process everything
3441 -- except Limited_With clauses in this routine. Limited_With clauses
3442 -- are separately installed (see Install_Limited_Context_Clauses).
3444 while Present (Item) loop
3446 -- Case of explicit WITH clause
3448 if Nkind (Item) = N_With_Clause
3449 and then not Implicit_With (Item)
3450 then
3451 if Limited_Present (Item) then
3453 -- Limited withed units will be installed later
3455 goto Continue;
3457 -- If Name (Item) is not an entity name, something is wrong, and
3458 -- this will be detected in due course, for now ignore the item
3460 elsif not Is_Entity_Name (Name (Item)) then
3461 goto Continue;
3463 elsif No (Entity (Name (Item))) then
3464 Set_Entity (Name (Item), Any_Id);
3465 goto Continue;
3466 end if;
3468 Uname_Node := Entity (Name (Item));
3470 if Is_Private_Descendant (Uname_Node) then
3471 Check_Private := True;
3472 end if;
3474 Install_Withed_Unit (Item);
3476 Decl_Node := Unit_Declaration_Node (Uname_Node);
3478 -- If the unit is a subprogram instance, it appears nested within
3479 -- a package that carries the parent information.
3481 if Is_Generic_Instance (Uname_Node)
3482 and then Ekind (Uname_Node) /= E_Package
3483 then
3484 Decl_Node := Parent (Parent (Decl_Node));
3485 end if;
3487 if Is_Child_Spec (Decl_Node) then
3488 if Nkind (Name (Item)) = N_Expanded_Name then
3489 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3490 else
3491 -- If not an expanded name, the child unit must be a
3492 -- renaming, nothing to do.
3494 null;
3495 end if;
3497 elsif Nkind (Decl_Node) = N_Subprogram_Body
3498 and then not Acts_As_Spec (Parent (Decl_Node))
3499 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3500 then
3501 Implicit_With_On_Parent
3502 (Unit (Library_Unit (Parent (Decl_Node))), N);
3503 end if;
3505 -- Check license conditions unless this is a dummy unit
3507 if Sloc (Library_Unit (Item)) /= No_Location then
3508 License_Check : declare
3509 Withu : constant Unit_Number_Type :=
3510 Get_Source_Unit (Library_Unit (Item));
3511 Withl : constant License_Type :=
3512 License (Source_Index (Withu));
3513 Unitl : constant License_Type :=
3514 License (Source_Index (Current_Sem_Unit));
3516 procedure License_Error;
3517 -- Signal error of bad license
3519 -------------------
3520 -- License_Error --
3521 -------------------
3523 procedure License_Error is
3524 begin
3525 Error_Msg_N
3526 ("license of withed unit & may be inconsistent??",
3527 Name (Item));
3528 end License_Error;
3530 -- Start of processing for License_Check
3532 begin
3533 -- Exclude license check if withed unit is an internal unit.
3534 -- This situation arises e.g. with the GPL version of GNAT.
3536 if Is_Internal_Unit (Withu) then
3537 null;
3539 -- Otherwise check various cases
3540 else
3541 case Unitl is
3542 when Unknown =>
3543 null;
3545 when Restricted =>
3546 if Withl = GPL then
3547 License_Error;
3548 end if;
3550 when GPL =>
3551 if Withl = Restricted then
3552 License_Error;
3553 end if;
3555 when Modified_GPL =>
3556 if Withl = Restricted or else Withl = GPL then
3557 License_Error;
3558 end if;
3560 when Unrestricted =>
3561 null;
3562 end case;
3563 end if;
3564 end License_Check;
3565 end if;
3567 -- Case of USE PACKAGE clause
3569 elsif Nkind (Item) = N_Use_Package_Clause then
3570 Analyze_Use_Package (Item);
3572 -- Case of USE TYPE clause
3574 elsif Nkind (Item) = N_Use_Type_Clause then
3575 Analyze_Use_Type (Item);
3577 -- case of PRAGMA
3579 elsif Nkind (Item) = N_Pragma then
3580 Analyze (Item);
3581 end if;
3583 <<Continue>>
3584 Next (Item);
3585 end loop;
3587 if Is_Child_Spec (Lib_Unit) then
3589 -- The unit also has implicit with_clauses on its own parents
3591 if No (Context_Items (N)) then
3592 Set_Context_Items (N, New_List);
3593 end if;
3595 Implicit_With_On_Parent (Lib_Unit, N);
3596 end if;
3598 -- If the unit is a body, the context of the specification must also
3599 -- be installed. That includes private with_clauses in that context.
3601 if Nkind (Lib_Unit) = N_Package_Body
3602 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3603 and then not Acts_As_Spec (N))
3604 then
3605 Install_Context (Library_Unit (N));
3607 -- Only install private with-clauses of a spec that comes from
3608 -- source, excluding specs created for a subprogram body that is
3609 -- a child unit.
3611 if Comes_From_Source (Library_Unit (N)) then
3612 Install_Private_With_Clauses
3613 (Defining_Entity (Unit (Library_Unit (N))));
3614 end if;
3616 if Is_Child_Spec (Unit (Library_Unit (N))) then
3618 -- If the unit is the body of a public child unit, the private
3619 -- declarations of the parent must be made visible. If the child
3620 -- unit is private, the private declarations have been installed
3621 -- already in the call to Install_Parents for the spec. Installing
3622 -- private declarations must be done for all ancestors of public
3623 -- child units. In addition, sibling units mentioned in the
3624 -- context clause of the body are directly visible.
3626 declare
3627 Lib_Spec : Node_Id;
3628 P : Node_Id;
3629 P_Name : Entity_Id;
3631 begin
3632 Lib_Spec := Unit (Library_Unit (N));
3633 while Is_Child_Spec (Lib_Spec) loop
3634 P := Unit (Parent_Spec (Lib_Spec));
3635 P_Name := Defining_Entity (P);
3637 if not (Private_Present (Parent (Lib_Spec)))
3638 and then not In_Private_Part (P_Name)
3639 then
3640 Install_Private_Declarations (P_Name);
3641 Install_Private_With_Clauses (P_Name);
3642 Set_Use (Private_Declarations (Specification (P)));
3643 end if;
3645 Lib_Spec := P;
3646 end loop;
3647 end;
3648 end if;
3650 -- For a package body, children in context are immediately visible
3652 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3653 end if;
3655 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3656 N_Generic_Subprogram_Declaration,
3657 N_Package_Declaration,
3658 N_Subprogram_Declaration)
3659 then
3660 if Is_Child_Spec (Lib_Unit) then
3661 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3662 Set_Is_Private_Descendant
3663 (Defining_Entity (Lib_Unit),
3664 Is_Private_Descendant (Lib_Parent)
3665 or else Private_Present (Parent (Lib_Unit)));
3667 else
3668 Set_Is_Private_Descendant
3669 (Defining_Entity (Lib_Unit),
3670 Private_Present (Parent (Lib_Unit)));
3671 end if;
3672 end if;
3674 if Check_Private then
3675 Check_Private_Child_Unit (N);
3676 end if;
3677 end Install_Context_Clauses;
3679 -------------------------------------
3680 -- Install_Limited_Context_Clauses --
3681 -------------------------------------
3683 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3684 Item : Node_Id;
3686 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3687 -- Check that the unlimited view of a given compilation_unit is not
3688 -- already visible through "use + renamings".
3690 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3691 -- Check that if a limited_with clause of a given compilation_unit
3692 -- mentions a descendant of a private child of some library unit, then
3693 -- the given compilation_unit must be the declaration of a private
3694 -- descendant of that library unit, or a public descendant of such. The
3695 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3696 -- use entities on the limited with_clauses because their units have not
3697 -- been analyzed, so we have to climb the tree of ancestors looking for
3698 -- private keywords.
3700 procedure Expand_Limited_With_Clause
3701 (Comp_Unit : Node_Id;
3702 Nam : Node_Id;
3703 N : Node_Id);
3704 -- If a child unit appears in a limited_with clause, there are implicit
3705 -- limited_with clauses on all parents that are not already visible
3706 -- through a regular with clause. This procedure creates the implicit
3707 -- limited with_clauses for the parents and loads the corresponding
3708 -- units. The shadow entities are created when the inserted clause is
3709 -- analyzed. Implements Ada 2005 (AI-50217).
3711 ---------------------
3712 -- Check_Renamings --
3713 ---------------------
3715 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3716 Item : Node_Id;
3717 Spec : Node_Id;
3718 WEnt : Entity_Id;
3719 Nam : Node_Id;
3720 E : Entity_Id;
3721 E2 : Entity_Id;
3723 begin
3724 pragma Assert (Nkind (W) = N_With_Clause);
3726 -- Protect the frontend against previous critical errors
3728 case Nkind (Unit (Library_Unit (W))) is
3729 when N_Generic_Package_Declaration
3730 | N_Generic_Subprogram_Declaration
3731 | N_Package_Declaration
3732 | N_Subprogram_Declaration
3734 null;
3736 when others =>
3737 return;
3738 end case;
3740 -- Check "use + renamings"
3742 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3743 Spec := Specification (Unit (P));
3745 Item := First (Visible_Declarations (Spec));
3746 while Present (Item) loop
3748 -- Look only at use package clauses
3750 if Nkind (Item) = N_Use_Package_Clause then
3752 -- Traverse the list of packages
3754 Nam := First (Names (Item));
3755 while Present (Nam) loop
3756 E := Entity (Nam);
3758 pragma Assert (Present (Parent (E)));
3760 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3761 and then Renamed_Entity (E) = WEnt
3762 then
3763 -- The unlimited view is visible through use clause and
3764 -- renamings. There is no need to generate the error
3765 -- message here because Is_Visible_Through_Renamings
3766 -- takes care of generating the precise error message.
3768 return;
3770 elsif Nkind (Parent (E)) = N_Package_Specification then
3772 -- The use clause may refer to a local package.
3773 -- Check all the enclosing scopes.
3775 E2 := E;
3776 while E2 /= Standard_Standard and then E2 /= WEnt loop
3777 E2 := Scope (E2);
3778 end loop;
3780 if E2 = WEnt then
3781 Error_Msg_N
3782 ("unlimited view visible through use clause ", W);
3783 return;
3784 end if;
3785 end if;
3787 Next (Nam);
3788 end loop;
3789 end if;
3791 Next (Item);
3792 end loop;
3794 -- Recursive call to check all the ancestors
3796 if Is_Child_Spec (Unit (P)) then
3797 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3798 end if;
3799 end Check_Renamings;
3801 ---------------------------------------
3802 -- Check_Private_Limited_Withed_Unit --
3803 ---------------------------------------
3805 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3806 Curr_Parent : Node_Id;
3807 Child_Parent : Node_Id;
3808 Curr_Private : Boolean;
3810 begin
3811 -- Compilation unit of the parent of the withed library unit
3813 Child_Parent := Library_Unit (Item);
3815 -- If the child unit is a public child, then locate its nearest
3816 -- private ancestor, if any, then Child_Parent will then be set to
3817 -- the parent of that ancestor.
3819 if not Private_Present (Library_Unit (Item)) then
3820 while Present (Child_Parent)
3821 and then not Private_Present (Child_Parent)
3822 loop
3823 Child_Parent := Parent_Spec (Unit (Child_Parent));
3824 end loop;
3826 if No (Child_Parent) then
3827 return;
3828 end if;
3829 end if;
3831 Child_Parent := Parent_Spec (Unit (Child_Parent));
3833 -- Traverse all the ancestors of the current compilation unit to
3834 -- check if it is a descendant of named library unit.
3836 Curr_Parent := Parent (Item);
3837 Curr_Private := Private_Present (Curr_Parent);
3839 while Present (Parent_Spec (Unit (Curr_Parent)))
3840 and then Curr_Parent /= Child_Parent
3841 loop
3842 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3843 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3844 end loop;
3846 if Curr_Parent /= Child_Parent then
3847 Error_Msg_N
3848 ("unit in with clause is private child unit!", Item);
3849 Error_Msg_NE
3850 ("\current unit must also have parent&!",
3851 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3853 elsif Private_Present (Parent (Item))
3854 or else Curr_Private
3855 or else Private_Present (Item)
3856 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3857 N_Subprogram_Body,
3858 N_Subunit)
3859 then
3860 -- Current unit is private, of descendant of a private unit
3862 null;
3864 else
3865 Error_Msg_NE
3866 ("current unit must also be private descendant of&",
3867 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3868 end if;
3869 end Check_Private_Limited_Withed_Unit;
3871 --------------------------------
3872 -- Expand_Limited_With_Clause --
3873 --------------------------------
3875 procedure Expand_Limited_With_Clause
3876 (Comp_Unit : Node_Id;
3877 Nam : Node_Id;
3878 N : Node_Id)
3880 Loc : constant Source_Ptr := Sloc (Nam);
3881 Unum : Unit_Number_Type;
3882 Withn : Node_Id;
3884 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3885 -- Returns true if the context already includes a with_clause for
3886 -- this unit. If the with_clause is non-limited, the unit is fully
3887 -- visible and an implicit limited_with should not be created. If
3888 -- there is already a limited_with clause for W, a second one is
3889 -- simply redundant.
3891 --------------------------
3892 -- Previous_Withed_Unit --
3893 --------------------------
3895 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3896 Item : Node_Id;
3898 begin
3899 -- A limited with_clause cannot appear in the same context_clause
3900 -- as a nonlimited with_clause which mentions the same library.
3902 Item := First (Context_Items (Comp_Unit));
3903 while Present (Item) loop
3904 if Nkind (Item) = N_With_Clause
3905 and then Library_Unit (Item) = Library_Unit (W)
3906 then
3907 return True;
3908 end if;
3910 Next (Item);
3911 end loop;
3913 return False;
3914 end Previous_Withed_Unit;
3916 -- Start of processing for Expand_Limited_With_Clause
3918 begin
3919 if Nkind (Nam) = N_Identifier then
3921 -- Create node for name of withed unit
3923 Withn :=
3924 Make_With_Clause (Loc,
3925 Name => New_Copy (Nam));
3927 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3928 Withn :=
3929 Make_With_Clause (Loc,
3930 Name => Make_Selected_Component (Loc,
3931 Prefix => New_Copy_Tree (Prefix (Nam)),
3932 Selector_Name => New_Copy (Selector_Name (Nam))));
3933 Set_Parent (Withn, Parent (N));
3934 end if;
3936 Set_Limited_Present (Withn);
3937 Set_First_Name (Withn);
3938 Set_Implicit_With (Withn);
3940 Unum :=
3941 Load_Unit
3942 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3943 Required => True,
3944 Subunit => False,
3945 Error_Node => Nam);
3947 -- Do not generate a limited_with_clause on the current unit. This
3948 -- path is taken when a unit has a limited_with clause on one of its
3949 -- child units.
3951 if Unum = Current_Sem_Unit then
3952 return;
3953 end if;
3955 Set_Library_Unit (Withn, Cunit (Unum));
3956 Set_Corresponding_Spec
3957 (Withn, Specification (Unit (Cunit (Unum))));
3959 if not Previous_Withed_Unit (Withn) then
3960 Prepend (Withn, Context_Items (Parent (N)));
3961 Mark_Rewrite_Insertion (Withn);
3963 -- Add implicit limited_with_clauses for parents of child units
3964 -- mentioned in limited_with clauses.
3966 if Nkind (Nam) = N_Selected_Component then
3967 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3968 end if;
3970 Analyze (Withn);
3972 if not Limited_View_Installed (Withn) then
3973 Install_Limited_Withed_Unit (Withn);
3974 end if;
3975 end if;
3976 end Expand_Limited_With_Clause;
3978 -- Start of processing for Install_Limited_Context_Clauses
3980 begin
3981 Item := First (Context_Items (N));
3982 while Present (Item) loop
3983 if Nkind (Item) = N_With_Clause
3984 and then Limited_Present (Item)
3985 and then not Error_Posted (Item)
3986 then
3987 if Nkind (Name (Item)) = N_Selected_Component then
3988 Expand_Limited_With_Clause
3989 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3990 end if;
3992 Check_Private_Limited_Withed_Unit (Item);
3994 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
3995 Check_Renamings (Parent_Spec (Unit (N)), Item);
3996 end if;
3998 -- A unit may have a limited with on itself if it has a limited
3999 -- with_clause on one of its child units. In that case it is
4000 -- already being compiled and it makes no sense to install its
4001 -- limited view.
4003 -- If the item is a limited_private_with_clause, install it if the
4004 -- current unit is a body or if it is a private child. Otherwise
4005 -- the private clause is installed before analyzing the private
4006 -- part of the current unit.
4008 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
4009 and then not Limited_View_Installed (Item)
4010 and then
4011 not Is_Ancestor_Unit
4012 (Library_Unit (Item), Cunit (Current_Sem_Unit))
4013 then
4014 if not Private_Present (Item)
4015 or else Private_Present (N)
4016 or else Nkind_In (Unit (N), N_Package_Body,
4017 N_Subprogram_Body,
4018 N_Subunit)
4019 then
4020 Install_Limited_Withed_Unit (Item);
4021 end if;
4022 end if;
4023 end if;
4025 Next (Item);
4026 end loop;
4028 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4029 -- looking for incomplete subtype declarations of incomplete types
4030 -- visible through a limited with clause.
4032 if Ada_Version >= Ada_2005
4033 and then Analyzed (N)
4034 and then Nkind (Unit (N)) = N_Package_Declaration
4035 then
4036 declare
4037 Decl : Node_Id;
4038 Def_Id : Entity_Id;
4039 Non_Lim_View : Entity_Id;
4041 begin
4042 Decl := First (Visible_Declarations (Specification (Unit (N))));
4043 while Present (Decl) loop
4044 if Nkind (Decl) = N_Subtype_Declaration
4045 and then
4046 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
4047 and then
4048 From_Limited_With (Defining_Identifier (Decl))
4049 then
4050 Def_Id := Defining_Identifier (Decl);
4051 Non_Lim_View := Non_Limited_View (Def_Id);
4053 if not Is_Incomplete_Type (Non_Lim_View) then
4055 -- Convert an incomplete subtype declaration into a
4056 -- corresponding non-limited view subtype declaration.
4057 -- This is usually the case when analyzing a body that
4058 -- has regular with clauses, when the spec has limited
4059 -- ones.
4061 -- If the non-limited view is still incomplete, it is
4062 -- the dummy entry already created, and the declaration
4063 -- cannot be reanalyzed. This is the case when installing
4064 -- a parent unit that has limited with-clauses.
4066 Set_Subtype_Indication (Decl,
4067 New_Occurrence_Of (Non_Lim_View, Sloc (Def_Id)));
4068 Set_Etype (Def_Id, Non_Lim_View);
4069 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
4070 Set_Analyzed (Decl, False);
4072 -- Reanalyze the declaration, suppressing the call to
4073 -- Enter_Name to avoid duplicate names.
4075 Analyze_Subtype_Declaration
4076 (N => Decl,
4077 Skip => True);
4078 end if;
4079 end if;
4081 Next (Decl);
4082 end loop;
4083 end;
4084 end if;
4085 end Install_Limited_Context_Clauses;
4087 ---------------------
4088 -- Install_Parents --
4089 ---------------------
4091 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
4092 P : Node_Id;
4093 E_Name : Entity_Id;
4094 P_Name : Entity_Id;
4095 P_Spec : Node_Id;
4097 begin
4098 P := Unit (Parent_Spec (Lib_Unit));
4099 P_Name := Get_Parent_Entity (P);
4101 if Etype (P_Name) = Any_Type then
4102 return;
4103 end if;
4105 if Ekind (P_Name) = E_Generic_Package
4106 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
4107 N_Generic_Package_Declaration)
4108 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
4109 then
4110 Error_Msg_N
4111 ("child of a generic package must be a generic unit", Lib_Unit);
4113 elsif not Is_Package_Or_Generic_Package (P_Name) then
4114 Error_Msg_N
4115 ("parent unit must be package or generic package", Lib_Unit);
4116 raise Unrecoverable_Error;
4118 elsif Present (Renamed_Object (P_Name)) then
4119 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
4120 raise Unrecoverable_Error;
4122 -- Verify that a child of an instance is itself an instance, or the
4123 -- renaming of one. Given that an instance that is a unit is replaced
4124 -- with a package declaration, check against the original node. The
4125 -- parent may be currently being instantiated, in which case it appears
4126 -- as a declaration, but the generic_parent is already established
4127 -- indicating that we deal with an instance.
4129 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
4130 if Nkind (Lib_Unit) in N_Renaming_Declaration
4131 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
4132 or else
4133 (Nkind (Lib_Unit) = N_Package_Declaration
4134 and then Present (Generic_Parent (Specification (Lib_Unit))))
4135 then
4136 null;
4137 else
4138 Error_Msg_N
4139 ("child of an instance must be an instance or renaming",
4140 Lib_Unit);
4141 end if;
4142 end if;
4144 -- This is the recursive call that ensures all parents are loaded
4146 if Is_Child_Spec (P) then
4147 Install_Parents (P,
4148 Is_Private or else Private_Present (Parent (Lib_Unit)));
4149 end if;
4151 -- Now we can install the context for this parent
4153 Install_Context_Clauses (Parent_Spec (Lib_Unit));
4154 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4155 Install_Siblings (P_Name, Parent (Lib_Unit));
4157 -- The child unit is in the declarative region of the parent. The parent
4158 -- must therefore appear in the scope stack and be visible, as when
4159 -- compiling the corresponding body. If the child unit is private or it
4160 -- is a package body, private declarations must be accessible as well.
4161 -- Use declarations in the parent must also be installed. Finally, other
4162 -- child units of the same parent that are in the context are
4163 -- immediately visible.
4165 -- Find entity for compilation unit, and set its private descendant
4166 -- status as needed. Indicate that it is a compilation unit, which is
4167 -- redundant in general, but needed if this is a generated child spec
4168 -- for a child body without previous spec.
4170 E_Name := Defining_Entity (Lib_Unit);
4172 Set_Is_Child_Unit (E_Name);
4173 Set_Is_Compilation_Unit (E_Name);
4175 Set_Is_Private_Descendant (E_Name,
4176 Is_Private_Descendant (P_Name)
4177 or else Private_Present (Parent (Lib_Unit)));
4179 P_Spec := Package_Specification (P_Name);
4180 Push_Scope (P_Name);
4182 -- Save current visibility of unit
4184 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4185 Is_Immediately_Visible (P_Name);
4186 Set_Is_Immediately_Visible (P_Name);
4187 Install_Visible_Declarations (P_Name);
4188 Set_Use (Visible_Declarations (P_Spec));
4190 -- If the parent is a generic unit, its formal part may contain formal
4191 -- packages and use clauses for them.
4193 if Ekind (P_Name) = E_Generic_Package then
4194 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4195 end if;
4197 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4198 Install_Private_Declarations (P_Name);
4199 Install_Private_With_Clauses (P_Name);
4200 Set_Use (Private_Declarations (P_Spec));
4201 end if;
4202 end Install_Parents;
4204 ----------------------------------
4205 -- Install_Private_With_Clauses --
4206 ----------------------------------
4208 procedure Install_Private_With_Clauses (P : Entity_Id) is
4209 Decl : constant Node_Id := Unit_Declaration_Node (P);
4210 Item : Node_Id;
4212 begin
4213 if Debug_Flag_I then
4214 Write_Str ("install private with clauses of ");
4215 Write_Name (Chars (P));
4216 Write_Eol;
4217 end if;
4219 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4220 Item := First (Context_Items (Parent (Decl)));
4221 while Present (Item) loop
4222 if Nkind (Item) = N_With_Clause
4223 and then Private_Present (Item)
4224 then
4225 -- If the unit is an ancestor of the current one, it is the
4226 -- case of a private limited with clause on a child unit, and
4227 -- the compilation of one of its descendants, In that case the
4228 -- limited view is errelevant.
4230 if Limited_Present (Item) then
4231 if not Limited_View_Installed (Item)
4232 and then
4233 not Is_Ancestor_Unit (Library_Unit (Item),
4234 Cunit (Current_Sem_Unit))
4235 then
4236 Install_Limited_Withed_Unit (Item);
4237 end if;
4238 else
4239 Install_Withed_Unit (Item, Private_With_OK => True);
4240 end if;
4241 end if;
4243 Next (Item);
4244 end loop;
4245 end if;
4246 end Install_Private_With_Clauses;
4248 ----------------------
4249 -- Install_Siblings --
4250 ----------------------
4252 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4253 Item : Node_Id;
4254 Id : Entity_Id;
4255 Prev : Entity_Id;
4257 begin
4258 -- Iterate over explicit with clauses, and check whether the scope of
4259 -- each entity is an ancestor of the current unit, in which case it is
4260 -- immediately visible.
4262 Item := First (Context_Items (N));
4263 while Present (Item) loop
4265 -- Do not install private_with_clauses declaration, unless unit
4266 -- is itself a private child unit, or is a body. Note that for a
4267 -- subprogram body the private_with_clause does not take effect
4268 -- until after the specification.
4270 if Nkind (Item) /= N_With_Clause
4271 or else Implicit_With (Item)
4272 or else Limited_Present (Item)
4273 or else Error_Posted (Item)
4275 -- Skip processing malformed trees
4277 or else (Try_Semantics
4278 and then Nkind (Name (Item)) not in N_Has_Entity)
4279 then
4280 null;
4282 elsif not Private_Present (Item)
4283 or else Private_Present (N)
4284 or else Nkind (Unit (N)) = N_Package_Body
4285 then
4286 Id := Entity (Name (Item));
4288 if Is_Child_Unit (Id)
4289 and then Is_Ancestor_Package (Scope (Id), U_Name)
4290 then
4291 Set_Is_Immediately_Visible (Id);
4293 -- Check for the presence of another unit in the context that
4294 -- may be inadvertently hidden by the child.
4296 Prev := Current_Entity (Id);
4298 if Present (Prev)
4299 and then Is_Immediately_Visible (Prev)
4300 and then not Is_Child_Unit (Prev)
4301 then
4302 declare
4303 Clause : Node_Id;
4305 begin
4306 Clause := First (Context_Items (N));
4307 while Present (Clause) loop
4308 if Nkind (Clause) = N_With_Clause
4309 and then Entity (Name (Clause)) = Prev
4310 then
4311 Error_Msg_NE
4312 ("child unit& hides compilation unit " &
4313 "with the same name??",
4314 Name (Item), Id);
4315 exit;
4316 end if;
4318 Next (Clause);
4319 end loop;
4320 end;
4321 end if;
4323 -- The With_Clause may be on a grand-child or one of its further
4324 -- descendants, which makes a child immediately visible. Examine
4325 -- ancestry to determine whether such a child exists. For example,
4326 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4327 -- is immediately visible.
4329 elsif Is_Child_Unit (Id) then
4330 declare
4331 Par : Entity_Id;
4333 begin
4334 Par := Scope (Id);
4335 while Is_Child_Unit (Par) loop
4336 if Is_Ancestor_Package (Scope (Par), U_Name) then
4337 Set_Is_Immediately_Visible (Par);
4338 exit;
4339 end if;
4341 Par := Scope (Par);
4342 end loop;
4343 end;
4344 end if;
4346 -- If the item is a private with-clause on a child unit, the parent
4347 -- may have been installed already, but the child unit must remain
4348 -- invisible until installed in a private part or body, unless there
4349 -- is already a regular with_clause for it in the current unit.
4351 elsif Private_Present (Item) then
4352 Id := Entity (Name (Item));
4354 if Is_Child_Unit (Id) then
4355 declare
4356 Clause : Node_Id;
4358 function In_Context return Boolean;
4359 -- Scan context of current unit, to check whether there is
4360 -- a with_clause on the same unit as a private with-clause
4361 -- on a parent, in which case child unit is visible. If the
4362 -- unit is a grand-child, the same applies to its parent.
4364 ----------------
4365 -- In_Context --
4366 ----------------
4368 function In_Context return Boolean is
4369 begin
4370 Clause :=
4371 First (Context_Items (Cunit (Current_Sem_Unit)));
4372 while Present (Clause) loop
4373 if Nkind (Clause) = N_With_Clause
4374 and then Comes_From_Source (Clause)
4375 and then Is_Entity_Name (Name (Clause))
4376 and then not Private_Present (Clause)
4377 then
4378 if Entity (Name (Clause)) = Id
4379 or else
4380 (Nkind (Name (Clause)) = N_Expanded_Name
4381 and then Entity (Prefix (Name (Clause))) = Id)
4382 then
4383 return True;
4384 end if;
4385 end if;
4387 Next (Clause);
4388 end loop;
4390 return False;
4391 end In_Context;
4393 begin
4394 Set_Is_Visible_Lib_Unit (Id, In_Context);
4395 end;
4396 end if;
4397 end if;
4399 Next (Item);
4400 end loop;
4401 end Install_Siblings;
4403 ---------------------------------
4404 -- Install_Limited_Withed_Unit --
4405 ---------------------------------
4407 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4408 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4409 E : Entity_Id;
4410 P : Entity_Id;
4411 Is_Child_Package : Boolean := False;
4412 Lim_Header : Entity_Id;
4413 Lim_Typ : Entity_Id;
4415 procedure Check_Body_Required;
4416 -- A unit mentioned in a limited with_clause may not be mentioned in
4417 -- a regular with_clause, but must still be included in the current
4418 -- partition. We need to determine whether the unit needs a body, so
4419 -- that the binder can determine the name of the file to be compiled.
4420 -- Checking whether a unit needs a body can be done without semantic
4421 -- analysis, by examining the nature of the declarations in the package.
4423 function Has_Limited_With_Clause
4424 (C_Unit : Entity_Id;
4425 Pack : Entity_Id) return Boolean;
4426 -- Determine whether any package in the ancestor chain starting with
4427 -- C_Unit has a limited with clause for package Pack.
4429 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4430 -- Check if some package installed though normal with-clauses has a
4431 -- renaming declaration of package P. AARM 10.1.2(21/2).
4433 -------------------------
4434 -- Check_Body_Required --
4435 -------------------------
4437 procedure Check_Body_Required is
4438 PA : constant List_Id :=
4439 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4441 procedure Check_Declarations (Spec : Node_Id);
4442 -- Recursive procedure that does the work and checks nested packages
4444 ------------------------
4445 -- Check_Declarations --
4446 ------------------------
4448 procedure Check_Declarations (Spec : Node_Id) is
4449 Decl : Node_Id;
4450 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4452 Subp_List : constant Elist_Id := New_Elmt_List;
4454 procedure Check_Pragma_Import (P : Node_Id);
4455 -- If a pragma import applies to a previous subprogram, the
4456 -- enclosing unit may not need a body. The processing is syntactic
4457 -- and does not require a declaration to be analyzed. The code
4458 -- below also handles pragma Import when applied to a subprogram
4459 -- that renames another. In this case the pragma applies to the
4460 -- renamed entity.
4462 -- Chains of multiple renames are not handled by the code below.
4463 -- It is probably impossible to handle all cases without proper
4464 -- name resolution. In such cases the algorithm is conservative
4465 -- and will indicate that a body is needed???
4467 -------------------------
4468 -- Check_Pragma_Import --
4469 -------------------------
4471 procedure Check_Pragma_Import (P : Node_Id) is
4472 Arg : Node_Id;
4473 Prev_Id : Elmt_Id;
4474 Subp_Id : Elmt_Id;
4475 Imported : Node_Id;
4477 procedure Remove_Homonyms (E : Node_Id);
4478 -- Make one pass over list of subprograms. Called again if
4479 -- subprogram is a renaming. E is known to be an identifier.
4481 ---------------------
4482 -- Remove_Homonyms --
4483 ---------------------
4485 procedure Remove_Homonyms (E : Node_Id) is
4486 R : Entity_Id := Empty;
4487 -- Name of renamed entity, if any
4489 begin
4490 Subp_Id := First_Elmt (Subp_List);
4491 while Present (Subp_Id) loop
4492 if Chars (Node (Subp_Id)) = Chars (E) then
4493 if Nkind (Parent (Parent (Node (Subp_Id))))
4494 /= N_Subprogram_Renaming_Declaration
4495 then
4496 Prev_Id := Subp_Id;
4497 Next_Elmt (Subp_Id);
4498 Remove_Elmt (Subp_List, Prev_Id);
4499 else
4500 R := Name (Parent (Parent (Node (Subp_Id))));
4501 exit;
4502 end if;
4503 else
4504 Next_Elmt (Subp_Id);
4505 end if;
4506 end loop;
4508 if Present (R) then
4509 if Nkind (R) = N_Identifier then
4510 Remove_Homonyms (R);
4512 elsif Nkind (R) = N_Selected_Component then
4513 Remove_Homonyms (Selector_Name (R));
4515 -- Renaming of attribute
4517 else
4518 null;
4519 end if;
4520 end if;
4521 end Remove_Homonyms;
4523 -- Start of processing for Check_Pragma_Import
4525 begin
4526 -- Find name of entity in Import pragma. We have not analyzed
4527 -- the construct, so we must guard against syntax errors.
4529 Arg := Next (First (Pragma_Argument_Associations (P)));
4531 if No (Arg)
4532 or else Nkind (Expression (Arg)) /= N_Identifier
4533 then
4534 return;
4535 else
4536 Imported := Expression (Arg);
4537 end if;
4539 Remove_Homonyms (Imported);
4540 end Check_Pragma_Import;
4542 -- Start of processing for Check_Declarations
4544 begin
4545 -- Search for Elaborate Body pragma
4547 Decl := First (Visible_Declarations (Spec));
4548 while Present (Decl)
4549 and then Nkind (Decl) = N_Pragma
4550 loop
4551 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4552 Set_Body_Required (Library_Unit (N));
4553 return;
4554 end if;
4556 Next (Decl);
4557 end loop;
4559 -- Look for declarations that require the presence of a body. We
4560 -- have already skipped pragmas at the start of the list.
4562 while Present (Decl) loop
4564 -- Subprogram that comes from source means body may be needed.
4565 -- Save for subsequent examination of import pragmas.
4567 if Comes_From_Source (Decl)
4568 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4569 N_Subprogram_Renaming_Declaration,
4570 N_Generic_Subprogram_Declaration))
4571 then
4572 Append_Elmt (Defining_Entity (Decl), Subp_List);
4574 -- Package declaration of generic package declaration. We need
4575 -- to recursively examine nested declarations.
4577 elsif Nkind_In (Decl, N_Package_Declaration,
4578 N_Generic_Package_Declaration)
4579 then
4580 Check_Declarations (Specification (Decl));
4582 elsif Nkind (Decl) = N_Pragma
4583 and then Pragma_Name (Decl) = Name_Import
4584 then
4585 Check_Pragma_Import (Decl);
4586 end if;
4588 Next (Decl);
4589 end loop;
4591 -- Same set of tests for private part. In addition to subprograms
4592 -- detect the presence of Taft Amendment types (incomplete types
4593 -- completed in the body).
4595 Decl := First (Private_Declarations (Spec));
4596 while Present (Decl) loop
4597 if Comes_From_Source (Decl)
4598 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4599 N_Subprogram_Renaming_Declaration,
4600 N_Generic_Subprogram_Declaration))
4601 then
4602 Append_Elmt (Defining_Entity (Decl), Subp_List);
4604 elsif Nkind_In (Decl, N_Package_Declaration,
4605 N_Generic_Package_Declaration)
4606 then
4607 Check_Declarations (Specification (Decl));
4609 -- Collect incomplete type declarations for separate pass
4611 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4612 Append_Elmt (Decl, Incomplete_Decls);
4614 elsif Nkind (Decl) = N_Pragma
4615 and then Pragma_Name (Decl) = Name_Import
4616 then
4617 Check_Pragma_Import (Decl);
4618 end if;
4620 Next (Decl);
4621 end loop;
4623 -- Now check incomplete declarations to locate Taft amendment
4624 -- types. This can be done by examining the defining identifiers
4625 -- of type declarations without real semantic analysis.
4627 declare
4628 Inc : Elmt_Id;
4630 begin
4631 Inc := First_Elmt (Incomplete_Decls);
4632 while Present (Inc) loop
4633 Decl := Next (Node (Inc));
4634 while Present (Decl) loop
4635 if Nkind (Decl) = N_Full_Type_Declaration
4636 and then Chars (Defining_Identifier (Decl)) =
4637 Chars (Defining_Identifier (Node (Inc)))
4638 then
4639 exit;
4640 end if;
4642 Next (Decl);
4643 end loop;
4645 -- If no completion, this is a TAT, and a body is needed
4647 if No (Decl) then
4648 Set_Body_Required (Library_Unit (N));
4649 return;
4650 end if;
4652 Next_Elmt (Inc);
4653 end loop;
4654 end;
4656 -- Finally, check whether there are subprograms that still require
4657 -- a body, i.e. are not renamings or null.
4659 if not Is_Empty_Elmt_List (Subp_List) then
4660 declare
4661 Subp_Id : Elmt_Id;
4662 Spec : Node_Id;
4664 begin
4665 Subp_Id := First_Elmt (Subp_List);
4666 Spec := Parent (Node (Subp_Id));
4668 while Present (Subp_Id) loop
4669 if Nkind (Parent (Spec))
4670 = N_Subprogram_Renaming_Declaration
4671 then
4672 null;
4674 elsif Nkind (Spec) = N_Procedure_Specification
4675 and then Null_Present (Spec)
4676 then
4677 null;
4679 else
4680 Set_Body_Required (Library_Unit (N));
4681 return;
4682 end if;
4684 Next_Elmt (Subp_Id);
4685 end loop;
4686 end;
4687 end if;
4688 end Check_Declarations;
4690 -- Start of processing for Check_Body_Required
4692 begin
4693 -- If this is an imported package (Java and CIL usage) no body is
4694 -- needed. Scan list of pragmas that may follow a compilation unit
4695 -- to look for a relevant pragma Import.
4697 if Present (PA) then
4698 declare
4699 Prag : Node_Id;
4701 begin
4702 Prag := First (PA);
4703 while Present (Prag) loop
4704 if Nkind (Prag) = N_Pragma
4705 and then Get_Pragma_Id (Prag) = Pragma_Import
4706 then
4707 return;
4708 end if;
4710 Next (Prag);
4711 end loop;
4712 end;
4713 end if;
4715 Check_Declarations (Specification (P_Unit));
4716 end Check_Body_Required;
4718 -----------------------------
4719 -- Has_Limited_With_Clause --
4720 -----------------------------
4722 function Has_Limited_With_Clause
4723 (C_Unit : Entity_Id;
4724 Pack : Entity_Id) return Boolean
4726 Par : Entity_Id;
4727 Par_Unit : Node_Id;
4729 begin
4730 Par := C_Unit;
4731 while Present (Par) loop
4732 if Ekind (Par) /= E_Package then
4733 exit;
4734 end if;
4736 -- Retrieve the Compilation_Unit node for Par and determine if
4737 -- its context clauses contain a limited with for Pack.
4739 Par_Unit := Parent (Parent (Parent (Par)));
4741 if Nkind (Par_Unit) = N_Package_Declaration then
4742 Par_Unit := Parent (Par_Unit);
4743 end if;
4745 if Has_With_Clause (Par_Unit, Pack, True) then
4746 return True;
4747 end if;
4749 -- If there are more ancestors, climb up the tree, otherwise we
4750 -- are done.
4752 if Is_Child_Unit (Par) then
4753 Par := Scope (Par);
4754 else
4755 exit;
4756 end if;
4757 end loop;
4759 return False;
4760 end Has_Limited_With_Clause;
4762 ----------------------------------
4763 -- Is_Visible_Through_Renamings --
4764 ----------------------------------
4766 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4767 Kind : constant Node_Kind :=
4768 Nkind (Unit (Cunit (Current_Sem_Unit)));
4769 Aux_Unit : Node_Id;
4770 Item : Node_Id;
4771 Decl : Entity_Id;
4773 begin
4774 -- Example of the error detected by this subprogram:
4776 -- package P is
4777 -- type T is ...
4778 -- end P;
4780 -- with P;
4781 -- package Q is
4782 -- package Ren_P renames P;
4783 -- end Q;
4785 -- with Q;
4786 -- package R is ...
4788 -- limited with P; -- ERROR
4789 -- package R.C is ...
4791 Aux_Unit := Cunit (Current_Sem_Unit);
4793 loop
4794 Item := First (Context_Items (Aux_Unit));
4795 while Present (Item) loop
4796 if Nkind (Item) = N_With_Clause
4797 and then not Limited_Present (Item)
4798 and then Nkind (Unit (Library_Unit (Item))) =
4799 N_Package_Declaration
4800 then
4801 Decl :=
4802 First (Visible_Declarations
4803 (Specification (Unit (Library_Unit (Item)))));
4804 while Present (Decl) loop
4805 if Nkind (Decl) = N_Package_Renaming_Declaration
4806 and then Entity (Name (Decl)) = P
4807 then
4808 -- Generate the error message only if the current unit
4809 -- is a package declaration; in case of subprogram
4810 -- bodies and package bodies we just return True to
4811 -- indicate that the limited view must not be
4812 -- installed.
4814 if Kind = N_Package_Declaration then
4815 Error_Msg_N
4816 ("simultaneous visibility of the limited and " &
4817 "unlimited views not allowed", N);
4818 Error_Msg_Sloc := Sloc (Item);
4819 Error_Msg_NE
4820 ("\\ unlimited view of & visible through the " &
4821 "context clause #", N, P);
4822 Error_Msg_Sloc := Sloc (Decl);
4823 Error_Msg_NE ("\\ and the renaming #", N, P);
4824 end if;
4826 return True;
4827 end if;
4829 Next (Decl);
4830 end loop;
4831 end if;
4833 Next (Item);
4834 end loop;
4836 -- If it is a body not acting as spec, follow pointer to the
4837 -- corresponding spec, otherwise follow pointer to parent spec.
4839 if Present (Library_Unit (Aux_Unit))
4840 and then Nkind_In (Unit (Aux_Unit),
4841 N_Package_Body, N_Subprogram_Body)
4842 then
4843 if Aux_Unit = Library_Unit (Aux_Unit) then
4845 -- Aux_Unit is a body that acts as a spec. Clause has
4846 -- already been flagged as illegal.
4848 return False;
4850 else
4851 Aux_Unit := Library_Unit (Aux_Unit);
4852 end if;
4854 else
4855 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4856 end if;
4858 exit when No (Aux_Unit);
4859 end loop;
4861 return False;
4862 end Is_Visible_Through_Renamings;
4864 -- Start of processing for Install_Limited_Withed_Unit
4866 begin
4867 pragma Assert (not Limited_View_Installed (N));
4869 -- In case of limited with_clause on subprograms, generics, instances,
4870 -- or renamings, the corresponding error was previously posted and we
4871 -- have nothing to do here. If the file is missing altogether, it has
4872 -- no source location.
4874 if Nkind (P_Unit) /= N_Package_Declaration
4875 or else Sloc (P_Unit) = No_Location
4876 then
4877 return;
4878 end if;
4880 P := Defining_Unit_Name (Specification (P_Unit));
4882 -- Handle child packages
4884 if Nkind (P) = N_Defining_Program_Unit_Name then
4885 Is_Child_Package := True;
4886 P := Defining_Identifier (P);
4887 end if;
4889 -- Do not install the limited-view if the context of the unit is already
4890 -- available through a regular with clause.
4892 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4893 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4894 then
4895 return;
4896 end if;
4898 -- Do not install the limited-view if the full-view is already visible
4899 -- through renaming declarations.
4901 if Is_Visible_Through_Renamings (P) then
4902 return;
4903 end if;
4905 -- Do not install the limited view if this is the unit being analyzed.
4906 -- This unusual case will happen when a unit has a limited_with clause
4907 -- on one of its children. The compilation of the child forces the load
4908 -- of the parent which tries to install the limited view of the child
4909 -- again. Installing the limited view must also be disabled when
4910 -- compiling the body of the child unit.
4912 if P = Cunit_Entity (Current_Sem_Unit)
4913 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4914 and then P = Main_Unit_Entity
4915 and then Is_Ancestor_Unit
4916 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
4917 then
4918 return;
4919 end if;
4921 -- This scenario is similar to the one above, the difference is that the
4922 -- compilation of sibling Par.Sib forces the load of parent Par which
4923 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4924 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4925 -- non-limited views of all entities from Lim_Pack.
4927 -- limited with Lim_Pack; -- [1]
4928 -- package Par is ... package Lim_Pack is ...
4930 -- with Lim_Pack; -- [2]
4931 -- package Par.Sib is ... package body Par.Sib is ...
4933 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4934 -- Sem_Unit is the body of Par.Sib.
4936 if Ekind (P) = E_Package
4937 and then Ekind (Main_Unit_Entity) = E_Package
4938 and then Is_Child_Unit (Main_Unit_Entity)
4940 -- The body has a regular with clause
4942 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4943 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4945 -- One of the ancestors has a limited with clause
4947 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4948 N_Package_Specification
4949 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4950 then
4951 return;
4952 end if;
4954 -- A common use of the limited-with is to have a limited-with in the
4955 -- package spec, and a normal with in its package body. For example:
4957 -- limited with X; -- [1]
4958 -- package A is ...
4960 -- with X; -- [2]
4961 -- package body A is ...
4963 -- The compilation of A's body installs the context clauses found at [2]
4964 -- and then the context clauses of its specification (found at [1]). As
4965 -- a consequence, at [1] the specification of X has been analyzed and it
4966 -- is immediately visible. According to the semantics of limited-with
4967 -- context clauses we don't install the limited view because the full
4968 -- view of X supersedes its limited view.
4970 if Analyzed (P_Unit)
4971 and then
4972 (Is_Immediately_Visible (P)
4973 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
4974 then
4976 -- The presence of both the limited and the analyzed nonlimited view
4977 -- may also be an error, such as an illegal context for a limited
4978 -- with_clause. In that case, do not process the context item at all.
4980 if Error_Posted (N) then
4981 return;
4982 end if;
4984 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
4985 declare
4986 Item : Node_Id;
4987 begin
4988 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
4989 while Present (Item) loop
4990 if Nkind (Item) = N_With_Clause
4991 and then Comes_From_Source (Item)
4992 and then Entity (Name (Item)) = P
4993 then
4994 return;
4995 end if;
4997 Next (Item);
4998 end loop;
4999 end;
5001 -- If this is a child body, assume that the nonlimited with_clause
5002 -- appears in an ancestor. Could be refined ???
5004 if Is_Child_Unit
5005 (Defining_Entity
5006 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
5007 then
5008 return;
5009 end if;
5011 else
5013 -- If in package declaration, nonlimited view brought in from
5014 -- parent unit or some error condition.
5016 return;
5017 end if;
5018 end if;
5020 if Debug_Flag_I then
5021 Write_Str ("install limited view of ");
5022 Write_Name (Chars (P));
5023 Write_Eol;
5024 end if;
5026 -- If the unit has not been analyzed and the limited view has not been
5027 -- already installed then we install it.
5029 if not Analyzed (P_Unit) then
5030 if not In_Chain (P) then
5032 -- Minimum decoration
5034 Set_Ekind (P, E_Package);
5035 Set_Etype (P, Standard_Void_Type);
5036 Set_Scope (P, Standard_Standard);
5037 Set_Is_Visible_Lib_Unit (P);
5039 if Is_Child_Package then
5040 Set_Is_Child_Unit (P);
5041 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
5042 end if;
5044 -- Place entity on visibility structure
5046 Set_Homonym (P, Current_Entity (P));
5047 Set_Current_Entity (P);
5049 if Debug_Flag_I then
5050 Write_Str (" (homonym) chain ");
5051 Write_Name (Chars (P));
5052 Write_Eol;
5053 end if;
5055 -- Install the incomplete view. The first element of the limited
5056 -- view is a header (an E_Package entity) used to reference the
5057 -- first shadow entity in the private part of the package.
5059 Lim_Header := Limited_View (P);
5060 Lim_Typ := First_Entity (Lim_Header);
5062 while Present (Lim_Typ)
5063 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5064 loop
5065 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
5066 Set_Current_Entity (Lim_Typ);
5068 if Debug_Flag_I then
5069 Write_Str (" (homonym) chain ");
5070 Write_Name (Chars (Lim_Typ));
5071 Write_Eol;
5072 end if;
5074 Next_Entity (Lim_Typ);
5075 end loop;
5076 end if;
5078 -- If the unit appears in a previous regular with_clause, the regular
5079 -- entities of the public part of the withed package must be replaced
5080 -- by the shadow ones.
5082 -- This code must be kept synchronized with the code that replaces the
5083 -- shadow entities by the real entities (see body of Remove_Limited
5084 -- With_Clause); otherwise the contents of the homonym chains are not
5085 -- consistent.
5087 else
5088 -- Hide all the type entities of the public part of the package to
5089 -- avoid its usage. This is needed to cover all the subtype decla-
5090 -- rations because we do not remove them from the homonym chain.
5092 E := First_Entity (P);
5093 while Present (E) and then E /= First_Private_Entity (P) loop
5094 if Is_Type (E) then
5095 Set_Was_Hidden (E, Is_Hidden (E));
5096 Set_Is_Hidden (E);
5097 end if;
5099 Next_Entity (E);
5100 end loop;
5102 -- Replace the real entities by the shadow entities of the limited
5103 -- view. The first element of the limited view is a header that is
5104 -- used to reference the first shadow entity in the private part
5105 -- of the package. Successive elements are the limited views of the
5106 -- type (including regular incomplete types) declared in the package.
5108 Lim_Header := Limited_View (P);
5110 Lim_Typ := First_Entity (Lim_Header);
5111 while Present (Lim_Typ)
5112 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5113 loop
5114 pragma Assert (not In_Chain (Lim_Typ));
5116 -- Do not unchain nested packages and child units
5118 if Ekind (Lim_Typ) /= E_Package
5119 and then not Is_Child_Unit (Lim_Typ)
5120 then
5121 declare
5122 Prev : Entity_Id;
5124 begin
5125 Prev := Current_Entity (Lim_Typ);
5126 E := Prev;
5128 -- Replace E in the homonyms list, so that the limited view
5129 -- becomes available.
5131 -- If the non-limited view is a record with an anonymous
5132 -- self-referential component, the analysis of the record
5133 -- declaration creates an incomplete type with the same name
5134 -- in order to define an internal access type. The visible
5135 -- entity is now the incomplete type, and that is the one to
5136 -- replace in the visibility structure.
5138 if E = Non_Limited_View (Lim_Typ)
5139 or else
5140 (Ekind (E) = E_Incomplete_Type
5141 and then Full_View (E) = Non_Limited_View (Lim_Typ))
5142 then
5143 Set_Homonym (Lim_Typ, Homonym (Prev));
5144 Set_Current_Entity (Lim_Typ);
5146 else
5147 loop
5148 E := Homonym (Prev);
5150 -- E may have been removed when installing a previous
5151 -- limited_with_clause.
5153 exit when No (E);
5154 exit when E = Non_Limited_View (Lim_Typ);
5155 Prev := Homonym (Prev);
5156 end loop;
5158 if Present (E) then
5159 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
5160 Set_Homonym (Prev, Lim_Typ);
5161 end if;
5162 end if;
5163 end;
5165 if Debug_Flag_I then
5166 Write_Str (" (homonym) chain ");
5167 Write_Name (Chars (Lim_Typ));
5168 Write_Eol;
5169 end if;
5170 end if;
5172 Next_Entity (Lim_Typ);
5173 end loop;
5174 end if;
5176 -- The package must be visible while the limited-with clause is active
5177 -- because references to the type P.T must resolve in the usual way.
5178 -- In addition, we remember that the limited-view has been installed to
5179 -- uninstall it at the point of context removal.
5181 Set_Is_Immediately_Visible (P);
5182 Set_Limited_View_Installed (N);
5184 -- If unit has not been analyzed in some previous context, check
5185 -- (imperfectly ???) whether it might need a body.
5187 if not Analyzed (P_Unit) then
5188 Check_Body_Required;
5189 end if;
5191 -- If the package in the limited_with clause is a child unit, the clause
5192 -- is unanalyzed and appears as a selected component. Recast it as an
5193 -- expanded name so that the entity can be properly set. Use entity of
5194 -- parent, if available, for higher ancestors in the name.
5196 if Nkind (Name (N)) = N_Selected_Component then
5197 declare
5198 Nam : Node_Id;
5199 Ent : Entity_Id;
5201 begin
5202 Nam := Name (N);
5203 Ent := P;
5204 while Nkind (Nam) = N_Selected_Component
5205 and then Present (Ent)
5206 loop
5207 Change_Selected_Component_To_Expanded_Name (Nam);
5209 -- Set entity of parent identifiers if the unit is a child
5210 -- unit. This ensures that the tree is properly formed from
5211 -- semantic point of view (e.g. for ASIS queries). The unit
5212 -- entities are not fully analyzed, so we need to follow unit
5213 -- links in the tree.
5215 Set_Entity (Nam, Ent);
5217 Nam := Prefix (Nam);
5218 Ent :=
5219 Defining_Entity
5220 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5222 -- Set entity of last ancestor
5224 if Nkind (Nam) = N_Identifier then
5225 Set_Entity (Nam, Ent);
5226 end if;
5227 end loop;
5228 end;
5229 end if;
5231 Set_Entity (Name (N), P);
5232 Set_From_Limited_With (P);
5233 end Install_Limited_Withed_Unit;
5235 -------------------------
5236 -- Install_Withed_Unit --
5237 -------------------------
5239 procedure Install_Withed_Unit
5240 (With_Clause : Node_Id;
5241 Private_With_OK : Boolean := False)
5243 Uname : constant Entity_Id := Entity (Name (With_Clause));
5244 P : constant Entity_Id := Scope (Uname);
5246 begin
5247 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5248 -- compiling a package declaration and the Private_With_OK flag was not
5249 -- set by the caller. These declarations will be installed later (before
5250 -- analyzing the private part of the package).
5252 if Private_Present (With_Clause)
5253 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
5254 and then not (Private_With_OK)
5255 then
5256 return;
5257 end if;
5259 if Debug_Flag_I then
5260 if Private_Present (With_Clause) then
5261 Write_Str ("install private withed unit ");
5262 else
5263 Write_Str ("install withed unit ");
5264 end if;
5266 Write_Name (Chars (Uname));
5267 Write_Eol;
5268 end if;
5270 -- We do not apply the restrictions to an internal unit unless we are
5271 -- compiling the internal unit as a main unit. This check is also
5272 -- skipped for dummy units (for missing packages).
5274 if Sloc (Uname) /= No_Location
5275 and then (not Is_Internal_Unit (Current_Sem_Unit)
5276 or else Current_Sem_Unit = Main_Unit)
5277 then
5278 Check_Restricted_Unit
5279 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5280 end if;
5282 if P /= Standard_Standard then
5284 -- If the unit is not analyzed after analysis of the with clause and
5285 -- it is an instantiation then it awaits a body and is the main unit.
5286 -- Its appearance in the context of some other unit indicates a
5287 -- circular dependency (DEC suite perversity).
5289 if not Analyzed (Uname)
5290 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5291 then
5292 Error_Msg_N
5293 ("instantiation depends on itself", Name (With_Clause));
5295 elsif not Is_Visible_Lib_Unit (Uname) then
5297 -- Abandon processing in case of previous errors
5299 if No (Scope (Uname)) then
5300 Check_Error_Detected;
5301 return;
5302 end if;
5304 Set_Is_Visible_Lib_Unit (Uname);
5306 -- If the unit is a wrapper package for a compilation unit that is
5307 -- a subprogrm instance, indicate that the instance itself is a
5308 -- visible unit. This is necessary if the instance is inlined.
5310 if Is_Wrapper_Package (Uname) then
5311 Set_Is_Visible_Lib_Unit (Related_Instance (Uname));
5312 end if;
5314 -- If the child unit appears in the context of its parent, it is
5315 -- immediately visible.
5317 if In_Open_Scopes (Scope (Uname)) then
5318 Set_Is_Immediately_Visible (Uname);
5319 end if;
5321 if Is_Generic_Instance (Uname)
5322 and then Ekind (Uname) in Subprogram_Kind
5323 then
5324 -- Set flag as well on the visible entity that denotes the
5325 -- instance, which renames the current one.
5327 Set_Is_Visible_Lib_Unit
5328 (Related_Instance
5329 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5330 end if;
5332 -- The parent unit may have been installed already, and may have
5333 -- appeared in a use clause.
5335 if In_Use (Scope (Uname)) then
5336 Set_Is_Potentially_Use_Visible (Uname);
5337 end if;
5339 Set_Context_Installed (With_Clause);
5340 end if;
5342 elsif not Is_Immediately_Visible (Uname) then
5343 Set_Is_Visible_Lib_Unit (Uname);
5345 if not Private_Present (With_Clause) or else Private_With_OK then
5346 Set_Is_Immediately_Visible (Uname);
5347 end if;
5349 Set_Context_Installed (With_Clause);
5350 end if;
5352 -- A with-clause overrides a with-type clause: there are no restric-
5353 -- tions on the use of package entities.
5355 if Ekind (Uname) = E_Package then
5356 Set_From_Limited_With (Uname, False);
5357 end if;
5359 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5360 -- unit if there is a visible homograph for it declared in the same
5361 -- declarative region. This pathological case can only arise when an
5362 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5363 -- G1 has a generic child also named G2, and the context includes with_
5364 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5365 -- of I1.G2 visible as well. If the child unit is named Standard, do
5366 -- not apply the check to the Standard package itself.
5368 if Is_Child_Unit (Uname)
5369 and then Is_Visible_Lib_Unit (Uname)
5370 and then Ada_Version >= Ada_2005
5371 then
5372 declare
5373 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5374 Decl2 : Node_Id;
5375 P2 : Entity_Id;
5376 U2 : Entity_Id;
5378 begin
5379 U2 := Homonym (Uname);
5380 while Present (U2) and then U2 /= Standard_Standard loop
5381 P2 := Scope (U2);
5382 Decl2 := Unit_Declaration_Node (P2);
5384 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5385 if Is_Generic_Instance (P)
5386 and then Nkind (Decl1) = N_Package_Declaration
5387 and then Generic_Parent (Specification (Decl1)) = P2
5388 then
5389 Error_Msg_N ("illegal with_clause", With_Clause);
5390 Error_Msg_N
5391 ("\child unit has visible homograph" &
5392 " (RM 8.3(26), 10.1.1(19))",
5393 With_Clause);
5394 exit;
5396 elsif Is_Generic_Instance (P2)
5397 and then Nkind (Decl2) = N_Package_Declaration
5398 and then Generic_Parent (Specification (Decl2)) = P
5399 then
5400 -- With_clause for child unit of instance appears before
5401 -- in the context. We want to place the error message on
5402 -- it, not on the generic child unit itself.
5404 declare
5405 Prev_Clause : Node_Id;
5407 begin
5408 Prev_Clause := First (List_Containing (With_Clause));
5409 while Entity (Name (Prev_Clause)) /= U2 loop
5410 Next (Prev_Clause);
5411 end loop;
5413 pragma Assert (Present (Prev_Clause));
5414 Error_Msg_N ("illegal with_clause", Prev_Clause);
5415 Error_Msg_N
5416 ("\child unit has visible homograph" &
5417 " (RM 8.3(26), 10.1.1(19))",
5418 Prev_Clause);
5419 exit;
5420 end;
5421 end if;
5422 end if;
5424 U2 := Homonym (U2);
5425 end loop;
5426 end;
5427 end if;
5428 end Install_Withed_Unit;
5430 -------------------
5431 -- Is_Child_Spec --
5432 -------------------
5434 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5435 K : constant Node_Kind := Nkind (Lib_Unit);
5437 begin
5438 return (K in N_Generic_Declaration or else
5439 K in N_Generic_Instantiation or else
5440 K in N_Generic_Renaming_Declaration or else
5441 K = N_Package_Declaration or else
5442 K = N_Package_Renaming_Declaration or else
5443 K = N_Subprogram_Declaration or else
5444 K = N_Subprogram_Renaming_Declaration)
5445 and then Present (Parent_Spec (Lib_Unit));
5446 end Is_Child_Spec;
5448 ------------------------------------
5449 -- Is_Legal_Shadow_Entity_In_Body --
5450 ------------------------------------
5452 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5453 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5454 begin
5455 return Nkind (Unit (C_Unit)) = N_Package_Body
5456 and then
5457 Has_With_Clause
5458 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5459 end Is_Legal_Shadow_Entity_In_Body;
5461 ----------------------
5462 -- Is_Ancestor_Unit --
5463 ----------------------
5465 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5466 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5467 E2 : Entity_Id;
5468 begin
5469 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
5470 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5471 return Is_Ancestor_Package (E1, E2);
5472 else
5473 return False;
5474 end if;
5475 end Is_Ancestor_Unit;
5477 -----------------------
5478 -- Load_Needed_Body --
5479 -----------------------
5481 -- N is a generic unit named in a with clause, or else it is a unit that
5482 -- contains a generic unit or an inlined function. In order to perform an
5483 -- instantiation, the body of the unit must be present. If the unit itself
5484 -- is generic, we assume that an instantiation follows, and load & analyze
5485 -- the body unconditionally. This forces analysis of the spec as well.
5487 -- If the unit is not generic, but contains a generic unit, it is loaded on
5488 -- demand, at the point of instantiation (see ch12).
5490 procedure Load_Needed_Body
5491 (N : Node_Id;
5492 OK : out Boolean;
5493 Do_Analyze : Boolean := True)
5495 Body_Name : Unit_Name_Type;
5496 Unum : Unit_Number_Type;
5498 Save_Style_Check : constant Boolean := Opt.Style_Check;
5499 -- The loading and analysis is done with style checks off
5501 begin
5502 if not GNAT_Mode then
5503 Style_Check := False;
5504 end if;
5506 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5507 Unum :=
5508 Load_Unit
5509 (Load_Name => Body_Name,
5510 Required => False,
5511 Subunit => False,
5512 Error_Node => N,
5513 Renamings => True);
5515 if Unum = No_Unit then
5516 OK := False;
5518 else
5519 Compiler_State := Analyzing; -- reset after load
5521 if Fatal_Error (Unum) /= Error_Detected or else Try_Semantics then
5522 if Debug_Flag_L then
5523 Write_Str ("*** Loaded generic body");
5524 Write_Eol;
5525 end if;
5527 if Do_Analyze then
5528 Semantics (Cunit (Unum));
5529 end if;
5530 end if;
5532 OK := True;
5533 end if;
5535 Style_Check := Save_Style_Check;
5536 end Load_Needed_Body;
5538 -------------------------
5539 -- Build_Limited_Views --
5540 -------------------------
5542 procedure Build_Limited_Views (N : Node_Id) is
5543 Unum : constant Unit_Number_Type :=
5544 Get_Source_Unit (Library_Unit (N));
5545 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
5547 Shadow_Pack : Entity_Id;
5548 -- The corresponding shadow entity of the withed package. This entity
5549 -- offers incomplete views of packages and types as well as abstract
5550 -- views of states and variables declared within.
5552 Last_Shadow : Entity_Id := Empty;
5553 -- The last shadow entity created by routine Build_Shadow_Entity
5555 procedure Build_Shadow_Entity
5556 (Ent : Entity_Id;
5557 Scop : Entity_Id;
5558 Shadow : out Entity_Id;
5559 Is_Tagged : Boolean := False);
5560 -- Create a shadow entity that hides Ent and offers an abstract or
5561 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5562 -- should be set when Ent is a tagged type. The generated entity is
5563 -- added to Lim_Header. This routine updates the value of Last_Shadow.
5565 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
5566 -- Perform minimal decoration of a package or its corresponding shadow
5567 -- entity denoted by Ent. Scop is the proper scope.
5569 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id);
5570 -- Perform full decoration of an abstract state or its corresponding
5571 -- shadow entity denoted by Ent. Scop is the proper scope.
5573 procedure Decorate_Type
5574 (Ent : Entity_Id;
5575 Scop : Entity_Id;
5576 Is_Tagged : Boolean := False;
5577 Materialize : Boolean := False);
5578 -- Perform minimal decoration of a type or its corresponding shadow
5579 -- entity denoted by Ent. Scop is the proper scope. Flag Is_Tagged
5580 -- should be set when Ent is a tagged type. Flag Materialize should be
5581 -- set when Ent is a tagged type and its class-wide type needs to appear
5582 -- in the tree.
5584 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id);
5585 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5586 -- proper scope.
5588 procedure Process_Declarations_And_States
5589 (Pack : Entity_Id;
5590 Decls : List_Id;
5591 Scop : Entity_Id;
5592 Create_Abstract_Views : Boolean);
5593 -- Inspect the states of package Pack and declarative list Decls. Create
5594 -- shadow entities for all nested packages, states, types and variables
5595 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5596 -- be set when the abstract states and variables need to be processed.
5598 -------------------------
5599 -- Build_Shadow_Entity --
5600 -------------------------
5602 procedure Build_Shadow_Entity
5603 (Ent : Entity_Id;
5604 Scop : Entity_Id;
5605 Shadow : out Entity_Id;
5606 Is_Tagged : Boolean := False)
5608 begin
5609 Shadow := Make_Temporary (Sloc (Ent), 'Z');
5611 -- The shadow entity must share the same name and parent as the
5612 -- entity it hides.
5614 Set_Chars (Shadow, Chars (Ent));
5615 Set_Parent (Shadow, Parent (Ent));
5617 -- The abstract view of a variable is a state, not another variable
5619 if Ekind (Ent) = E_Variable then
5620 Set_Ekind (Shadow, E_Abstract_State);
5621 else
5622 Set_Ekind (Shadow, Ekind (Ent));
5623 end if;
5625 Set_Is_Internal (Shadow);
5626 Set_From_Limited_With (Shadow);
5628 -- Add the new shadow entity to the limited view of the package
5630 Last_Shadow := Shadow;
5631 Append_Entity (Shadow, Shadow_Pack);
5633 -- Perform context-specific decoration of the shadow entity
5635 if Ekind (Ent) = E_Abstract_State then
5636 Decorate_State (Shadow, Scop);
5637 Set_Non_Limited_View (Shadow, Ent);
5639 elsif Ekind (Ent) = E_Package then
5640 Decorate_Package (Shadow, Scop);
5642 elsif Is_Type (Ent) then
5643 Decorate_Type (Shadow, Scop, Is_Tagged);
5644 Set_Non_Limited_View (Shadow, Ent);
5646 if Is_Tagged then
5647 Set_Non_Limited_View
5648 (Class_Wide_Type (Shadow), Class_Wide_Type (Ent));
5649 end if;
5651 if Is_Incomplete_Or_Private_Type (Ent) then
5652 Set_Private_Dependents (Shadow, New_Elmt_List);
5653 end if;
5655 elsif Ekind (Ent) = E_Variable then
5656 Decorate_State (Shadow, Scop);
5657 Set_Non_Limited_View (Shadow, Ent);
5658 end if;
5659 end Build_Shadow_Entity;
5661 ----------------------
5662 -- Decorate_Package --
5663 ----------------------
5665 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
5666 begin
5667 Set_Ekind (Ent, E_Package);
5668 Set_Etype (Ent, Standard_Void_Type);
5669 Set_Scope (Ent, Scop);
5670 end Decorate_Package;
5672 --------------------
5673 -- Decorate_State --
5674 --------------------
5676 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id) is
5677 begin
5678 Set_Ekind (Ent, E_Abstract_State);
5679 Set_Etype (Ent, Standard_Void_Type);
5680 Set_Scope (Ent, Scop);
5681 Set_Encapsulating_State (Ent, Empty);
5682 end Decorate_State;
5684 -------------------
5685 -- Decorate_Type --
5686 -------------------
5688 procedure Decorate_Type
5689 (Ent : Entity_Id;
5690 Scop : Entity_Id;
5691 Is_Tagged : Boolean := False;
5692 Materialize : Boolean := False)
5694 CW_Typ : Entity_Id;
5696 begin
5697 -- An unanalyzed type or a shadow entity of a type is treated as an
5698 -- incomplete type, and carries the corresponding attributes.
5700 Set_Ekind (Ent, E_Incomplete_Type);
5701 Set_Etype (Ent, Ent);
5702 Set_Full_View (Ent, Empty);
5703 Set_Is_First_Subtype (Ent);
5704 Set_Scope (Ent, Scop);
5705 Set_Stored_Constraint (Ent, No_Elist);
5706 Init_Size_Align (Ent);
5708 if From_Limited_With (Ent) then
5709 Set_Private_Dependents (Ent, New_Elmt_List);
5710 end if;
5712 -- A tagged type and its corresponding shadow entity share one common
5713 -- class-wide type. The list of primitive operations for the shadow
5714 -- entity is empty.
5716 if Is_Tagged then
5717 Set_Is_Tagged_Type (Ent);
5718 Set_Direct_Primitive_Operations (Ent, New_Elmt_List);
5720 CW_Typ :=
5721 New_External_Entity
5722 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
5724 Set_Class_Wide_Type (Ent, CW_Typ);
5726 -- Set parent to be the same as the parent of the tagged type.
5727 -- We need a parent field set, and it is supposed to point to
5728 -- the declaration of the type. The tagged type declaration
5729 -- essentially declares two separate types, the tagged type
5730 -- itself and the corresponding class-wide type, so it is
5731 -- reasonable for the parent fields to point to the declaration
5732 -- in both cases.
5734 Set_Parent (CW_Typ, Parent (Ent));
5736 Set_Ekind (CW_Typ, E_Class_Wide_Type);
5737 Set_Class_Wide_Type (CW_Typ, CW_Typ);
5738 Set_Etype (CW_Typ, Ent);
5739 Set_Equivalent_Type (CW_Typ, Empty);
5740 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
5741 Set_Has_Unknown_Discriminants (CW_Typ);
5742 Set_Is_First_Subtype (CW_Typ);
5743 Set_Is_Tagged_Type (CW_Typ);
5744 Set_Materialize_Entity (CW_Typ, Materialize);
5745 Set_Scope (CW_Typ, Scop);
5746 Init_Size_Align (CW_Typ);
5747 end if;
5748 end Decorate_Type;
5750 -----------------------
5751 -- Decorate_Variable --
5752 -----------------------
5754 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id) is
5755 begin
5756 Set_Ekind (Ent, E_Variable);
5757 Set_Etype (Ent, Standard_Void_Type);
5758 Set_Scope (Ent, Scop);
5759 end Decorate_Variable;
5761 -------------------------------------
5762 -- Process_Declarations_And_States --
5763 -------------------------------------
5765 procedure Process_Declarations_And_States
5766 (Pack : Entity_Id;
5767 Decls : List_Id;
5768 Scop : Entity_Id;
5769 Create_Abstract_Views : Boolean)
5771 procedure Find_And_Process_States;
5772 -- Determine whether package Pack defines abstract state either by
5773 -- using an aspect or a pragma. If this is the case, build shadow
5774 -- entities for all abstract states of Pack.
5776 procedure Process_States (States : Elist_Id);
5777 -- Generate shadow entities for all abstract states in list States
5779 -----------------------------
5780 -- Find_And_Process_States --
5781 -----------------------------
5783 procedure Find_And_Process_States is
5784 procedure Process_State (State : Node_Id);
5785 -- Generate shadow entities for a single abstract state or
5786 -- multiple states expressed as an aggregate.
5788 -------------------
5789 -- Process_State --
5790 -------------------
5792 procedure Process_State (State : Node_Id) is
5793 Loc : constant Source_Ptr := Sloc (State);
5794 Decl : Node_Id;
5795 Dummy : Entity_Id;
5796 Elmt : Node_Id;
5797 Id : Entity_Id;
5799 begin
5800 -- Multiple abstract states appear as an aggregate
5802 if Nkind (State) = N_Aggregate then
5803 Elmt := First (Expressions (State));
5804 while Present (Elmt) loop
5805 Process_State (Elmt);
5806 Next (Elmt);
5807 end loop;
5809 return;
5811 -- A null state has no abstract view
5813 elsif Nkind (State) = N_Null then
5814 return;
5816 -- State declaration with various options appears as an
5817 -- extension aggregate.
5819 elsif Nkind (State) = N_Extension_Aggregate then
5820 Decl := Ancestor_Part (State);
5822 -- Simple state declaration
5824 elsif Nkind (State) = N_Identifier then
5825 Decl := State;
5827 -- Possibly an illegal state declaration
5829 else
5830 return;
5831 end if;
5833 -- Abstract states are elaborated when the related pragma is
5834 -- elaborated. Since the withed package is not analyzed yet,
5835 -- the entities of the abstract states are not available. To
5836 -- overcome this complication, create the entities now and
5837 -- store them in their respective declarations. The entities
5838 -- are later used by routine Create_Abstract_State to declare
5839 -- and enter the states into visibility.
5841 if No (Entity (Decl)) then
5842 Id := Make_Defining_Identifier (Loc, Chars (Decl));
5844 Set_Entity (Decl, Id);
5845 Set_Parent (Id, State);
5846 Decorate_State (Id, Scop);
5848 -- Otherwise the package was previously withed
5850 else
5851 Id := Entity (Decl);
5852 end if;
5854 Build_Shadow_Entity (Id, Scop, Dummy);
5855 end Process_State;
5857 -- Local variables
5859 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack);
5860 Asp : Node_Id;
5861 Decl : Node_Id;
5863 -- Start of processing for Find_And_Process_States
5865 begin
5866 -- Find aspect Abstract_State
5868 Asp := First (Aspect_Specifications (Pack_Decl));
5869 while Present (Asp) loop
5870 if Chars (Identifier (Asp)) = Name_Abstract_State then
5871 Process_State (Expression (Asp));
5873 return;
5874 end if;
5876 Next (Asp);
5877 end loop;
5879 -- Find pragma Abstract_State by inspecting the declarations
5881 Decl := First (Decls);
5882 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
5883 if Pragma_Name (Decl) = Name_Abstract_State then
5884 Process_State
5885 (Get_Pragma_Arg
5886 (First (Pragma_Argument_Associations (Decl))));
5888 return;
5889 end if;
5891 Next (Decl);
5892 end loop;
5893 end Find_And_Process_States;
5895 --------------------
5896 -- Process_States --
5897 --------------------
5899 procedure Process_States (States : Elist_Id) is
5900 Dummy : Entity_Id;
5901 Elmt : Elmt_Id;
5903 begin
5904 Elmt := First_Elmt (States);
5905 while Present (Elmt) loop
5906 Build_Shadow_Entity (Node (Elmt), Scop, Dummy);
5908 Next_Elmt (Elmt);
5909 end loop;
5910 end Process_States;
5912 -- Local variables
5914 Is_Tagged : Boolean;
5915 Decl : Node_Id;
5916 Def : Node_Id;
5917 Def_Id : Entity_Id;
5918 Shadow : Entity_Id;
5920 -- Start of processing for Process_Declarations_And_States
5922 begin
5923 -- Build abstract views for all states defined in the package
5925 if Create_Abstract_Views then
5927 -- When a package has been analyzed, all states are stored in list
5928 -- Abstract_States. Generate the shadow entities directly.
5930 if Is_Analyzed then
5931 if Present (Abstract_States (Pack)) then
5932 Process_States (Abstract_States (Pack));
5933 end if;
5935 -- The package may declare abstract states by using an aspect or a
5936 -- pragma. Attempt to locate one of these construct and if found,
5937 -- build the shadow entities.
5939 else
5940 Find_And_Process_States;
5941 end if;
5942 end if;
5944 -- Inspect the declarative list, looking for nested packages, types
5945 -- and variable declarations.
5947 Decl := First (Decls);
5948 while Present (Decl) loop
5950 -- Packages
5952 if Nkind (Decl) = N_Package_Declaration then
5953 Def_Id := Defining_Entity (Decl);
5955 -- Perform minor decoration when the withed package has not
5956 -- been analyzed.
5958 if not Is_Analyzed then
5959 Decorate_Package (Def_Id, Scop);
5960 end if;
5962 -- Create a shadow entity that offers a limited view of all
5963 -- visible types declared within.
5965 Build_Shadow_Entity (Def_Id, Scop, Shadow);
5967 Process_Declarations_And_States
5968 (Pack => Def_Id,
5969 Decls => Visible_Declarations (Specification (Decl)),
5970 Scop => Shadow,
5971 Create_Abstract_Views => Create_Abstract_Views);
5973 -- Types
5975 elsif Nkind_In (Decl, N_Full_Type_Declaration,
5976 N_Incomplete_Type_Declaration,
5977 N_Private_Extension_Declaration,
5978 N_Private_Type_Declaration,
5979 N_Protected_Type_Declaration,
5980 N_Task_Type_Declaration)
5981 then
5982 Def_Id := Defining_Entity (Decl);
5984 -- Determine whether the type is tagged. Note that packages
5985 -- included via a limited with clause are not always analyzed,
5986 -- hence the tree lookup rather than the use of attribute
5987 -- Is_Tagged_Type.
5989 if Nkind (Decl) = N_Full_Type_Declaration then
5990 Def := Type_Definition (Decl);
5992 Is_Tagged :=
5993 (Nkind (Def) = N_Record_Definition
5994 and then Tagged_Present (Def))
5995 or else
5996 (Nkind (Def) = N_Derived_Type_Definition
5997 and then Present (Record_Extension_Part (Def)));
5999 elsif Nkind_In (Decl, N_Incomplete_Type_Declaration,
6000 N_Private_Type_Declaration)
6001 then
6002 Is_Tagged := Tagged_Present (Decl);
6004 elsif Nkind (Decl) = N_Private_Extension_Declaration then
6005 Is_Tagged := True;
6007 else
6008 Is_Tagged := False;
6009 end if;
6011 -- Perform minor decoration when the withed package has not
6012 -- been analyzed.
6014 if not Is_Analyzed then
6015 Decorate_Type (Def_Id, Scop, Is_Tagged, True);
6016 end if;
6018 -- Create a shadow entity that hides the type and offers an
6019 -- incomplete view of the said type.
6021 Build_Shadow_Entity (Def_Id, Scop, Shadow, Is_Tagged);
6023 -- Variables
6025 elsif Create_Abstract_Views
6026 and then Nkind (Decl) = N_Object_Declaration
6027 and then not Constant_Present (Decl)
6028 then
6029 Def_Id := Defining_Entity (Decl);
6031 -- Perform minor decoration when the withed package has not
6032 -- been analyzed.
6034 if not Is_Analyzed then
6035 Decorate_Variable (Def_Id, Scop);
6036 end if;
6038 -- Create a shadow entity that hides the variable and offers an
6039 -- abstract view of the said variable.
6041 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6042 end if;
6044 Next (Decl);
6045 end loop;
6046 end Process_Declarations_And_States;
6048 -- Local variables
6050 Nam : constant Node_Id := Name (N);
6051 Pack : constant Entity_Id := Cunit_Entity (Unum);
6053 Last_Public_Shadow : Entity_Id := Empty;
6054 Private_Shadow : Entity_Id;
6055 Spec : Node_Id;
6057 -- Start of processing for Build_Limited_Views
6059 begin
6060 pragma Assert (Limited_Present (N));
6062 -- A library_item mentioned in a limited_with_clause is a package
6063 -- declaration, not a subprogram declaration, generic declaration,
6064 -- generic instantiation, or package renaming declaration.
6066 case Nkind (Unit (Library_Unit (N))) is
6067 when N_Package_Declaration =>
6068 null;
6070 when N_Subprogram_Declaration =>
6071 Error_Msg_N ("subprograms not allowed in limited with_clauses", N);
6072 return;
6074 when N_Generic_Package_Declaration
6075 | N_Generic_Subprogram_Declaration
6077 Error_Msg_N ("generics not allowed in limited with_clauses", N);
6078 return;
6080 when N_Generic_Instantiation =>
6081 Error_Msg_N
6082 ("generic instantiations not allowed in limited with_clauses",
6084 return;
6086 when N_Generic_Renaming_Declaration =>
6087 Error_Msg_N
6088 ("generic renamings not allowed in limited with_clauses", N);
6089 return;
6091 when N_Subprogram_Renaming_Declaration =>
6092 Error_Msg_N
6093 ("renamed subprograms not allowed in limited with_clauses", N);
6094 return;
6096 when N_Package_Renaming_Declaration =>
6097 Error_Msg_N
6098 ("renamed packages not allowed in limited with_clauses", N);
6099 return;
6101 when others =>
6102 raise Program_Error;
6103 end case;
6105 -- The withed unit may not be analyzed, but the with calause itself
6106 -- must be minimally decorated. This ensures that the checks on unused
6107 -- with clauses also process limieted withs.
6109 Set_Ekind (Pack, E_Package);
6110 Set_Etype (Pack, Standard_Void_Type);
6112 if Is_Entity_Name (Nam) then
6113 Set_Entity (Nam, Pack);
6115 elsif Nkind (Nam) = N_Selected_Component then
6116 Set_Entity (Selector_Name (Nam), Pack);
6117 end if;
6119 -- Check if the chain is already built
6121 Spec := Specification (Unit (Library_Unit (N)));
6123 if Limited_View_Installed (Spec) then
6124 return;
6125 end if;
6127 -- Create the shadow package wich hides the withed unit and provides
6128 -- incomplete view of all types and packages declared within.
6130 Shadow_Pack := Make_Temporary (Sloc (N), 'Z');
6131 Set_Ekind (Shadow_Pack, E_Package);
6132 Set_Is_Internal (Shadow_Pack);
6133 Set_Limited_View (Pack, Shadow_Pack);
6135 -- Inspect the abstract states and visible declarations of the withed
6136 -- unit and create shadow entities that hide existing packages, states,
6137 -- variables and types.
6139 Process_Declarations_And_States
6140 (Pack => Pack,
6141 Decls => Visible_Declarations (Spec),
6142 Scop => Pack,
6143 Create_Abstract_Views => True);
6145 Last_Public_Shadow := Last_Shadow;
6147 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6148 -- to accommodate limited private with clauses.
6150 Process_Declarations_And_States
6151 (Pack => Pack,
6152 Decls => Private_Declarations (Spec),
6153 Scop => Pack,
6154 Create_Abstract_Views => False);
6156 if Present (Last_Public_Shadow) then
6157 Private_Shadow := Next_Entity (Last_Public_Shadow);
6158 else
6159 Private_Shadow := First_Entity (Shadow_Pack);
6160 end if;
6162 Set_First_Private_Entity (Shadow_Pack, Private_Shadow);
6163 Set_Limited_View_Installed (Spec);
6164 end Build_Limited_Views;
6166 ----------------------------
6167 -- Check_No_Elab_Code_All --
6168 ----------------------------
6170 procedure Check_No_Elab_Code_All (N : Node_Id) is
6171 begin
6172 if Present (No_Elab_Code_All_Pragma)
6173 and then In_Extended_Main_Source_Unit (N)
6174 and then Present (Context_Items (N))
6175 then
6176 declare
6177 CL : constant List_Id := Context_Items (N);
6178 CI : Node_Id;
6180 begin
6181 CI := First (CL);
6182 while Present (CI) loop
6183 if Nkind (CI) = N_With_Clause
6184 and then not
6185 No_Elab_Code_All (Get_Source_Unit (Library_Unit (CI)))
6187 -- In GNATprove mode, some runtime units are implicitly
6188 -- loaded to make their entities available for analysis. In
6189 -- this case, ignore violations of No_Elaboration_Code_All
6190 -- for this special analysis mode.
6192 and then not
6193 (GNATprove_Mode and then Implicit_With (CI))
6194 then
6195 Error_Msg_Sloc := Sloc (No_Elab_Code_All_Pragma);
6196 Error_Msg_N
6197 ("violation of No_Elaboration_Code_All#", CI);
6198 Error_Msg_NE
6199 ("\unit& does not have No_Elaboration_Code_All",
6200 CI, Entity (Name (CI)));
6201 end if;
6203 Next (CI);
6204 end loop;
6205 end;
6206 end if;
6207 end Check_No_Elab_Code_All;
6209 -------------------------------
6210 -- Check_Body_Needed_For_SAL --
6211 -------------------------------
6213 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
6214 function Entity_Needs_Body (E : Entity_Id) return Boolean;
6215 -- Determine whether use of entity E might require the presence of its
6216 -- body. For a package this requires a recursive traversal of all nested
6217 -- declarations.
6219 -----------------------
6220 -- Entity_Needs_Body --
6221 -----------------------
6223 function Entity_Needs_Body (E : Entity_Id) return Boolean is
6224 Ent : Entity_Id;
6226 begin
6227 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
6228 return True;
6230 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
6232 -- A generic subprogram always requires the presence of its
6233 -- body because an instantiation needs both templates. The only
6234 -- exceptions is a generic subprogram renaming. In this case the
6235 -- body is needed only when the template is declared outside the
6236 -- compilation unit being checked.
6238 if Present (Renamed_Entity (E)) then
6239 return not Within_Scope (E, Unit_Name);
6240 else
6241 return True;
6242 end if;
6244 elsif Ekind (E) = E_Generic_Package
6245 and then
6246 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
6247 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6248 then
6249 return True;
6251 elsif Ekind (E) = E_Package
6252 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
6253 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6254 then
6255 Ent := First_Entity (E);
6256 while Present (Ent) loop
6257 if Entity_Needs_Body (Ent) then
6258 return True;
6259 end if;
6261 Next_Entity (Ent);
6262 end loop;
6264 return False;
6266 else
6267 return False;
6268 end if;
6269 end Entity_Needs_Body;
6271 -- Start of processing for Check_Body_Needed_For_SAL
6273 begin
6274 if Ekind (Unit_Name) = E_Generic_Package
6275 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6276 N_Generic_Package_Declaration
6277 and then
6278 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
6279 then
6280 Set_Body_Needed_For_SAL (Unit_Name);
6282 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
6283 Set_Body_Needed_For_SAL (Unit_Name);
6285 elsif Is_Subprogram (Unit_Name)
6286 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6287 N_Subprogram_Declaration
6288 and then Has_Pragma_Inline (Unit_Name)
6289 then
6290 Set_Body_Needed_For_SAL (Unit_Name);
6292 elsif Ekind (Unit_Name) = E_Subprogram_Body then
6293 Check_Body_Needed_For_SAL
6294 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6296 elsif Ekind (Unit_Name) = E_Package
6297 and then Entity_Needs_Body (Unit_Name)
6298 then
6299 Set_Body_Needed_For_SAL (Unit_Name);
6301 elsif Ekind (Unit_Name) = E_Package_Body
6302 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
6303 then
6304 Check_Body_Needed_For_SAL
6305 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6306 end if;
6307 end Check_Body_Needed_For_SAL;
6309 --------------------
6310 -- Remove_Context --
6311 --------------------
6313 procedure Remove_Context (N : Node_Id) is
6314 Lib_Unit : constant Node_Id := Unit (N);
6316 begin
6317 -- If this is a child unit, first remove the parent units
6319 if Is_Child_Spec (Lib_Unit) then
6320 Remove_Parents (Lib_Unit);
6321 end if;
6323 Remove_Context_Clauses (N);
6324 end Remove_Context;
6326 ----------------------------
6327 -- Remove_Context_Clauses --
6328 ----------------------------
6330 procedure Remove_Context_Clauses (N : Node_Id) is
6331 Item : Node_Id;
6332 Unit_Name : Entity_Id;
6334 begin
6335 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6336 -- limited-views first and regular-views later (to maintain the
6337 -- stack model).
6339 -- First Phase: Remove limited_with context clauses
6341 Item := First (Context_Items (N));
6342 while Present (Item) loop
6344 -- We are interested only in with clauses which got installed
6345 -- on entry.
6347 if Nkind (Item) = N_With_Clause
6348 and then Limited_Present (Item)
6349 and then Limited_View_Installed (Item)
6350 then
6351 Remove_Limited_With_Clause (Item);
6352 end if;
6354 Next (Item);
6355 end loop;
6357 -- Second Phase: Loop through context items and undo regular
6358 -- with_clauses and use_clauses.
6360 Item := First (Context_Items (N));
6361 while Present (Item) loop
6363 -- We are interested only in with clauses which got installed on
6364 -- entry, as indicated by their Context_Installed flag set
6366 if Nkind (Item) = N_With_Clause
6367 and then Limited_Present (Item)
6368 and then Limited_View_Installed (Item)
6369 then
6370 null;
6372 elsif Nkind (Item) = N_With_Clause
6373 and then Context_Installed (Item)
6374 then
6375 -- Remove items from one with'ed unit
6377 Unit_Name := Entity (Name (Item));
6378 Remove_Unit_From_Visibility (Unit_Name);
6379 Set_Context_Installed (Item, False);
6381 elsif Nkind (Item) = N_Use_Package_Clause then
6382 End_Use_Package (Item);
6384 elsif Nkind (Item) = N_Use_Type_Clause then
6385 End_Use_Type (Item);
6386 end if;
6388 Next (Item);
6389 end loop;
6390 end Remove_Context_Clauses;
6392 --------------------------------
6393 -- Remove_Limited_With_Clause --
6394 --------------------------------
6396 procedure Remove_Limited_With_Clause (N : Node_Id) is
6397 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
6398 E : Entity_Id;
6399 P : Entity_Id;
6400 Lim_Header : Entity_Id;
6401 Lim_Typ : Entity_Id;
6402 Prev : Entity_Id;
6404 begin
6405 pragma Assert (Limited_View_Installed (N));
6407 -- In case of limited with_clause on subprograms, generics, instances,
6408 -- or renamings, the corresponding error was previously posted and we
6409 -- have nothing to do here.
6411 if Nkind (P_Unit) /= N_Package_Declaration then
6412 return;
6413 end if;
6415 P := Defining_Unit_Name (Specification (P_Unit));
6417 -- Handle child packages
6419 if Nkind (P) = N_Defining_Program_Unit_Name then
6420 P := Defining_Identifier (P);
6421 end if;
6423 if Debug_Flag_I then
6424 Write_Str ("remove limited view of ");
6425 Write_Name (Chars (P));
6426 Write_Str (" from visibility");
6427 Write_Eol;
6428 end if;
6430 -- Prepare the removal of the shadow entities from visibility. The first
6431 -- element of the limited view is a header (an E_Package entity) that is
6432 -- used to reference the first shadow entity in the private part of the
6433 -- package
6435 Lim_Header := Limited_View (P);
6436 Lim_Typ := First_Entity (Lim_Header);
6438 -- Remove package and shadow entities from visibility if it has not
6439 -- been analyzed
6441 if not Analyzed (P_Unit) then
6442 Unchain (P);
6443 Set_Is_Immediately_Visible (P, False);
6445 while Present (Lim_Typ) loop
6446 Unchain (Lim_Typ);
6447 Next_Entity (Lim_Typ);
6448 end loop;
6450 -- Otherwise this package has already appeared in the closure and its
6451 -- shadow entities must be replaced by its real entities. This code
6452 -- must be kept synchronized with the complementary code in Install
6453 -- Limited_Withed_Unit.
6455 else
6456 -- If the limited_with_clause is in some other unit in the context
6457 -- then it is not visible in the main unit.
6459 if not In_Extended_Main_Source_Unit (N) then
6460 Set_Is_Immediately_Visible (P, False);
6461 end if;
6463 -- Real entities that are type or subtype declarations were hidden
6464 -- from visibility at the point of installation of the limited-view.
6465 -- Now we recover the previous value of the hidden attribute.
6467 E := First_Entity (P);
6468 while Present (E) and then E /= First_Private_Entity (P) loop
6469 if Is_Type (E) then
6470 Set_Is_Hidden (E, Was_Hidden (E));
6471 end if;
6473 Next_Entity (E);
6474 end loop;
6476 while Present (Lim_Typ)
6477 and then Lim_Typ /= First_Private_Entity (Lim_Header)
6478 loop
6479 -- Nested packages and child units were not unchained
6481 if Ekind (Lim_Typ) /= E_Package
6482 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
6483 then
6484 -- If the package has incomplete types, the limited view of the
6485 -- incomplete type is in fact never visible (AI05-129) but we
6486 -- have created a shadow entity E1 for it, that points to E2,
6487 -- a non-limited incomplete type. This in turn has a full view
6488 -- E3 that is the full declaration. There is a corresponding
6489 -- shadow entity E4. When reinstalling the non-limited view,
6490 -- E2 must become the current entity and E3 must be ignored.
6492 E := Non_Limited_View (Lim_Typ);
6494 if Present (Current_Entity (E))
6495 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
6496 and then Full_View (Current_Entity (E)) = E
6497 then
6499 -- Lim_Typ is the limited view of a full type declaration
6500 -- that has a previous incomplete declaration, i.e. E3 from
6501 -- the previous description. Nothing to insert.
6503 null;
6505 else
6506 pragma Assert (not In_Chain (E));
6508 Prev := Current_Entity (Lim_Typ);
6510 if Prev = Lim_Typ then
6511 Set_Current_Entity (E);
6513 else
6514 while Present (Prev)
6515 and then Homonym (Prev) /= Lim_Typ
6516 loop
6517 Prev := Homonym (Prev);
6518 end loop;
6520 if Present (Prev) then
6521 Set_Homonym (Prev, E);
6522 end if;
6523 end if;
6525 -- Preserve structure of homonym chain
6527 Set_Homonym (E, Homonym (Lim_Typ));
6528 end if;
6529 end if;
6531 Next_Entity (Lim_Typ);
6532 end loop;
6533 end if;
6535 -- Indicate that the limited view of the package is not installed
6537 Set_From_Limited_With (P, False);
6538 Set_Limited_View_Installed (N, False);
6539 end Remove_Limited_With_Clause;
6541 --------------------
6542 -- Remove_Parents --
6543 --------------------
6545 procedure Remove_Parents (Lib_Unit : Node_Id) is
6546 P : Node_Id;
6547 P_Name : Entity_Id;
6548 P_Spec : Node_Id := Empty;
6549 E : Entity_Id;
6550 Vis : constant Boolean :=
6551 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
6553 begin
6554 if Is_Child_Spec (Lib_Unit) then
6555 P_Spec := Parent_Spec (Lib_Unit);
6557 elsif Nkind (Lib_Unit) = N_Package_Body
6558 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6559 then
6560 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6561 end if;
6563 if Present (P_Spec) then
6564 P := Unit (P_Spec);
6565 P_Name := Get_Parent_Entity (P);
6566 Remove_Context_Clauses (P_Spec);
6567 End_Package_Scope (P_Name);
6568 Set_Is_Immediately_Visible (P_Name, Vis);
6570 -- Remove from visibility the siblings as well, which are directly
6571 -- visible while the parent is in scope.
6573 E := First_Entity (P_Name);
6574 while Present (E) loop
6575 if Is_Child_Unit (E) then
6576 Set_Is_Immediately_Visible (E, False);
6577 end if;
6579 Next_Entity (E);
6580 end loop;
6582 Set_In_Package_Body (P_Name, False);
6584 -- This is the recursive call to remove the context of any higher
6585 -- level parent. This recursion ensures that all parents are removed
6586 -- in the reverse order of their installation.
6588 Remove_Parents (P);
6589 end if;
6590 end Remove_Parents;
6592 ---------------------------------
6593 -- Remove_Private_With_Clauses --
6594 ---------------------------------
6596 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6597 Item : Node_Id;
6599 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6600 -- Check whether a given unit appears in a regular with_clause. Used to
6601 -- determine whether a private_with_clause, implicit or explicit, should
6602 -- be ignored.
6604 ----------------------------
6605 -- In_Regular_With_Clause --
6606 ----------------------------
6608 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6610 Item : Node_Id;
6612 begin
6613 Item := First (Context_Items (Comp_Unit));
6614 while Present (Item) loop
6615 if Nkind (Item) = N_With_Clause
6617 -- The following guard is needed to ensure that the name has
6618 -- been properly analyzed before we go fetching its entity.
6620 and then Is_Entity_Name (Name (Item))
6621 and then Entity (Name (Item)) = E
6622 and then not Private_Present (Item)
6623 then
6624 return True;
6625 end if;
6626 Next (Item);
6627 end loop;
6629 return False;
6630 end In_Regular_With_Clause;
6632 -- Start of processing for Remove_Private_With_Clauses
6634 begin
6635 Item := First (Context_Items (Comp_Unit));
6636 while Present (Item) loop
6637 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
6639 -- If private_with_clause is redundant, remove it from context,
6640 -- as a small optimization to subsequent handling of private_with
6641 -- clauses in other nested packages.
6643 if In_Regular_With_Clause (Entity (Name (Item))) then
6644 declare
6645 Nxt : constant Node_Id := Next (Item);
6646 begin
6647 Remove (Item);
6648 Item := Nxt;
6649 end;
6651 elsif Limited_Present (Item) then
6652 if not Limited_View_Installed (Item) then
6653 Remove_Limited_With_Clause (Item);
6654 end if;
6656 Next (Item);
6658 else
6659 Remove_Unit_From_Visibility (Entity (Name (Item)));
6660 Set_Context_Installed (Item, False);
6661 Next (Item);
6662 end if;
6664 else
6665 Next (Item);
6666 end if;
6667 end loop;
6668 end Remove_Private_With_Clauses;
6670 ---------------------------------
6671 -- Remove_Unit_From_Visibility --
6672 ---------------------------------
6674 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6675 begin
6676 if Debug_Flag_I then
6677 Write_Str ("remove unit ");
6678 Write_Name (Chars (Unit_Name));
6679 Write_Str (" from visibility");
6680 Write_Eol;
6681 end if;
6683 Set_Is_Visible_Lib_Unit (Unit_Name, False);
6684 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6685 Set_Is_Immediately_Visible (Unit_Name, False);
6687 -- If the unit is a wrapper package, the subprogram instance is
6688 -- what must be removed from visibility.
6689 -- Should we use Related_Instance instead???
6691 if Is_Wrapper_Package (Unit_Name) then
6692 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
6693 end if;
6694 end Remove_Unit_From_Visibility;
6696 --------
6697 -- sm --
6698 --------
6700 procedure sm is
6701 begin
6702 null;
6703 end sm;
6705 -------------
6706 -- Unchain --
6707 -------------
6709 procedure Unchain (E : Entity_Id) is
6710 Prev : Entity_Id;
6712 begin
6713 Prev := Current_Entity (E);
6715 if No (Prev) then
6716 return;
6718 elsif Prev = E then
6719 Set_Name_Entity_Id (Chars (E), Homonym (E));
6721 else
6722 while Present (Prev) and then Homonym (Prev) /= E loop
6723 Prev := Homonym (Prev);
6724 end loop;
6726 if Present (Prev) then
6727 Set_Homonym (Prev, Homonym (E));
6728 end if;
6729 end if;
6731 if Debug_Flag_I then
6732 Write_Str (" (homonym) unchain ");
6733 Write_Name (Chars (E));
6734 Write_Eol;
6735 end if;
6736 end Unchain;
6738 end Sem_Ch10;