2015-05-22 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch10.adb
blob55456e6996febc497de0f9962e53c8b298c6cf44
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-2015, 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 Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Util; use Exp_Util;
32 with Elists; use Elists;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Impunit; use Impunit;
37 with Inline; use Inline;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Namet; use Namet;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Par_SCO; use Par_SCO;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch12; use Sem_Ch12;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinfo.CN; use Sinfo.CN;
64 with Sinput; use Sinput;
65 with Snames; use Snames;
66 with Style; use Style;
67 with Stylesw; use Stylesw;
68 with Tbuild; use Tbuild;
69 with Uname; use Uname;
71 package body Sem_Ch10 is
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 procedure Analyze_Context (N : Node_Id);
78 -- Analyzes items in the context clause of compilation unit
80 procedure Build_Limited_Views (N : Node_Id);
81 -- Build and decorate the list of shadow entities for a package mentioned
82 -- in a limited_with clause. If the package was not previously analyzed
83 -- then it also performs a basic decoration of the real entities. This is
84 -- required in order to avoid passing non-decorated entities to the
85 -- back-end. Implements Ada 2005 (AI-50217).
87 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
88 -- Check whether the source for the body of a compilation unit must be
89 -- included in a standalone library.
91 procedure Check_No_Elab_Code_All (N : Node_Id);
92 -- Carries out possible tests for violation of No_Elab_Code all for withed
93 -- units in the Context_Items of unit N.
95 procedure Check_Private_Child_Unit (N : Node_Id);
96 -- If a with_clause mentions a private child unit, the compilation unit
97 -- must be a member of the same family, as described in 10.1.2.
99 procedure Check_Stub_Level (N : Node_Id);
100 -- Verify that a stub is declared immediately within a compilation unit,
101 -- and not in an inner frame.
103 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
104 -- When a child unit appears in a context clause, the implicit withs on
105 -- parents are made explicit, and with clauses are inserted in the context
106 -- clause before the one for the child. If a parent in the with_clause
107 -- is a renaming, the implicit with_clause is on the renaming whose name
108 -- is mentioned in the with_clause, and not on the package it renames.
109 -- N is the compilation unit whose list of context items receives the
110 -- implicit with_clauses.
112 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
113 -- Generate cross-reference information for the parents of child units
114 -- and of subunits. N is a defining_program_unit_name, and P_Id is the
115 -- immediate parent scope.
117 function Has_With_Clause
118 (C_Unit : Node_Id;
119 Pack : Entity_Id;
120 Is_Limited : Boolean := False) return Boolean;
121 -- Determine whether compilation unit C_Unit contains a [limited] with
122 -- clause for package Pack. Use the flag Is_Limited to designate desired
123 -- clause kind.
125 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
126 -- If the main unit is a child unit, implicit withs are also added for
127 -- all its ancestors.
129 function In_Chain (E : Entity_Id) return Boolean;
130 -- Check that the shadow entity is not already in the homonym chain, for
131 -- example through a limited_with clause in a parent unit.
133 procedure Install_Context_Clauses (N : Node_Id);
134 -- Subsidiary to Install_Context and Install_Parents. Process all with
135 -- and use clauses for current unit and its library unit if any.
137 procedure Install_Limited_Context_Clauses (N : Node_Id);
138 -- Subsidiary to Install_Context. Process only limited with_clauses for
139 -- current unit. Implements Ada 2005 (AI-50217).
141 procedure Install_Limited_Withed_Unit (N : Node_Id);
142 -- Place shadow entities for a limited_with package in the visibility
143 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
145 procedure Install_Withed_Unit
146 (With_Clause : Node_Id;
147 Private_With_OK : Boolean := False);
148 -- If the unit is not a child unit, make unit immediately visible. The
149 -- caller ensures that the unit is not already currently installed. The
150 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
151 -- is called when compiling the private part of a package, or installing
152 -- the private declarations of a parent unit.
154 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
155 -- This procedure establishes the context for the compilation of a child
156 -- unit. If Lib_Unit is a child library spec then the context of the parent
157 -- is installed, and the parent itself made immediately visible, so that
158 -- the child unit is processed in the declarative region of the parent.
159 -- Install_Parents makes a recursive call to itself to ensure that all
160 -- parents are loaded in the nested case. If Lib_Unit is a library body,
161 -- the only effect of Install_Parents is to install the private decls of
162 -- the parents, because the visible parent declarations will have been
163 -- installed as part of the context of the corresponding spec.
165 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
166 -- In the compilation of a child unit, a child of any of the ancestor
167 -- units is directly visible if it is visible, because the parent is in
168 -- an enclosing scope. Iterate over context to find child units of U_Name
169 -- or of some ancestor of it.
171 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
172 -- When compiling a unit Q descended from some parent unit P, a limited
173 -- with_clause in the context of P that names some other ancestor of Q
174 -- must not be installed because the ancestor is immediately visible.
176 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
177 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
178 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
179 -- a library spec that has a parent. If the call to Is_Child_Spec returns
180 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
181 -- compilation unit for the parent spec.
183 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
184 -- Parent_Spec is non-empty, this is also a child unit.
186 procedure Remove_Context_Clauses (N : Node_Id);
187 -- Subsidiary of previous one. Remove use_ and with_clauses
189 procedure Remove_Limited_With_Clause (N : Node_Id);
190 -- Remove from visibility the shadow entities introduced for a package
191 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
193 procedure Remove_Parents (Lib_Unit : Node_Id);
194 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
195 -- contexts established by the corresponding call to Install_Parents are
196 -- removed. Remove_Parents contains a recursive call to itself to ensure
197 -- that all parents are removed in the nested case.
199 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
200 -- Reset all visibility flags on unit after compiling it, either as a main
201 -- unit or as a unit in the context.
203 procedure Unchain (E : Entity_Id);
204 -- Remove single entity from visibility list
206 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
207 -- Common processing for all stubs (subprograms, tasks, packages, and
208 -- protected cases). N is the stub to be analyzed. Once the subunit name
209 -- is established, load and analyze. Nam is the non-overloadable entity
210 -- for which the proper body provides a completion. Subprogram stubs are
211 -- handled differently because they can be declarations.
213 procedure sm;
214 -- A dummy procedure, for debugging use, called just before analyzing the
215 -- main unit (after dealing with any context clauses).
217 --------------------------
218 -- Limited_With_Clauses --
219 --------------------------
221 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
222 -- mutually recursive types declared in different units. A limited_with
223 -- clause that names package P in the context of unit U makes the types
224 -- declared in the visible part of P available within U, but with the
225 -- restriction that these types can only be used as incomplete types.
226 -- The limited_with clause does not impose a semantic dependence on P,
227 -- and it is possible for two packages to have limited_with_clauses on
228 -- each other without creating an elaboration circularity.
230 -- To support this feature, the analysis of a limited_with clause must
231 -- create an abbreviated view of the package, without performing any
232 -- semantic analysis on it. This "package abstract" contains shadow types
233 -- that are in one-one correspondence with the real types in the package,
234 -- and that have the properties of incomplete types.
236 -- The implementation creates two element lists: one to chain the shadow
237 -- entities, and one to chain the corresponding type entities in the tree
238 -- of the package. Links between corresponding entities in both chains
239 -- allow the compiler to select the proper view of a given type, depending
240 -- on the context. Note that in contrast with the handling of private
241 -- types, the limited view and the non-limited view of a type are treated
242 -- as separate entities, and no entity exchange needs to take place, which
243 -- makes the implementation much simpler than could be feared.
245 ------------------------------
246 -- Analyze_Compilation_Unit --
247 ------------------------------
249 procedure Analyze_Compilation_Unit (N : Node_Id) is
250 procedure Check_Redundant_Withs
251 (Context_Items : List_Id;
252 Spec_Context_Items : List_Id := No_List);
253 -- Determine whether the context list of a compilation unit contains
254 -- redundant with clauses. When checking body clauses against spec
255 -- clauses, set Context_Items to the context list of the body and
256 -- Spec_Context_Items to that of the spec. Parent packages are not
257 -- examined for documentation purposes.
259 ---------------------------
260 -- Check_Redundant_Withs --
261 ---------------------------
263 procedure Check_Redundant_Withs
264 (Context_Items : List_Id;
265 Spec_Context_Items : List_Id := No_List)
267 Clause : Node_Id;
269 procedure Process_Body_Clauses
270 (Context_List : List_Id;
271 Clause : Node_Id;
272 Used : in out Boolean;
273 Used_Type_Or_Elab : in out Boolean);
274 -- Examine the context clauses of a package body, trying to match the
275 -- name entity of Clause with any list element. If the match occurs
276 -- on a use package clause set Used to True, for a use type clause or
277 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
279 procedure Process_Spec_Clauses
280 (Context_List : List_Id;
281 Clause : Node_Id;
282 Used : in out Boolean;
283 Withed : in out Boolean;
284 Exit_On_Self : Boolean := False);
285 -- Examine the context clauses of a package spec, trying to match
286 -- the name entity of Clause with any list element. If the match
287 -- occurs on a use package clause, set Used to True, for a with
288 -- package clause other than Clause, set Withed to True. Limited
289 -- with clauses, implicitly generated with clauses and withs
290 -- having pragmas Elaborate or Elaborate_All applied to them are
291 -- skipped. Exit_On_Self is used to control the search loop and
292 -- force an exit whenever Clause sees itself in the search.
294 --------------------------
295 -- Process_Body_Clauses --
296 --------------------------
298 procedure Process_Body_Clauses
299 (Context_List : List_Id;
300 Clause : Node_Id;
301 Used : in out Boolean;
302 Used_Type_Or_Elab : in out Boolean)
304 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
305 Cont_Item : Node_Id;
306 Prag_Unit : Node_Id;
307 Subt_Mark : Node_Id;
308 Use_Item : Node_Id;
310 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
311 -- In an expanded name in a use clause, if the prefix is a renamed
312 -- package, the entity is set to the original package as a result,
313 -- when checking whether the package appears in a previous with
314 -- clause, the renaming has to be taken into account, to prevent
315 -- spurious/incorrect warnings. A common case is use of Text_IO.
317 ---------------
318 -- Same_Unit --
319 ---------------
321 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
322 begin
323 return Entity (N) = P
324 or else (Present (Renamed_Object (P))
325 and then Entity (N) = Renamed_Object (P));
326 end Same_Unit;
328 -- Start of processing for Process_Body_Clauses
330 begin
331 Used := False;
332 Used_Type_Or_Elab := False;
334 Cont_Item := First (Context_List);
335 while Present (Cont_Item) loop
337 -- Package use clause
339 if Nkind (Cont_Item) = N_Use_Package_Clause
340 and then not Used
341 then
342 -- Search through use clauses
344 Use_Item := First (Names (Cont_Item));
345 while Present (Use_Item) and then not Used loop
347 -- Case of a direct use of the one we are looking for
349 if Entity (Use_Item) = Nam_Ent then
350 Used := True;
352 -- Handle nested case, as in "with P; use P.Q.R"
354 else
355 declare
356 UE : Node_Id;
358 begin
359 -- Loop through prefixes looking for match
361 UE := Use_Item;
362 while Nkind (UE) = N_Expanded_Name loop
363 if Same_Unit (Prefix (UE), Nam_Ent) then
364 Used := True;
365 exit;
366 end if;
368 UE := Prefix (UE);
369 end loop;
370 end;
371 end if;
373 Next (Use_Item);
374 end loop;
376 -- USE TYPE clause
378 elsif Nkind (Cont_Item) = N_Use_Type_Clause
379 and then not Used_Type_Or_Elab
380 then
381 Subt_Mark := First (Subtype_Marks (Cont_Item));
382 while Present (Subt_Mark)
383 and then not Used_Type_Or_Elab
384 loop
385 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
386 Used_Type_Or_Elab := True;
387 end if;
389 Next (Subt_Mark);
390 end loop;
392 -- Pragma Elaborate or Elaborate_All
394 elsif Nkind (Cont_Item) = N_Pragma
395 and then
396 Nam_In (Pragma_Name (Cont_Item), Name_Elaborate,
397 Name_Elaborate_All)
398 and then not Used_Type_Or_Elab
399 then
400 Prag_Unit :=
401 First (Pragma_Argument_Associations (Cont_Item));
402 while Present (Prag_Unit) and then not Used_Type_Or_Elab loop
403 if Entity (Expression (Prag_Unit)) = Nam_Ent then
404 Used_Type_Or_Elab := True;
405 end if;
407 Next (Prag_Unit);
408 end loop;
409 end if;
411 Next (Cont_Item);
412 end loop;
413 end Process_Body_Clauses;
415 --------------------------
416 -- Process_Spec_Clauses --
417 --------------------------
419 procedure Process_Spec_Clauses
420 (Context_List : List_Id;
421 Clause : Node_Id;
422 Used : in out Boolean;
423 Withed : in out Boolean;
424 Exit_On_Self : Boolean := False)
426 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
427 Cont_Item : Node_Id;
428 Use_Item : Node_Id;
430 begin
431 Used := False;
432 Withed := False;
434 Cont_Item := First (Context_List);
435 while Present (Cont_Item) loop
437 -- Stop the search since the context items after Cont_Item have
438 -- already been examined in a previous iteration of the reverse
439 -- loop in Check_Redundant_Withs.
441 if Exit_On_Self
442 and Cont_Item = Clause
443 then
444 exit;
445 end if;
447 -- Package use clause
449 if Nkind (Cont_Item) = N_Use_Package_Clause
450 and then not Used
451 then
452 Use_Item := First (Names (Cont_Item));
453 while Present (Use_Item) and then not Used loop
454 if Entity (Use_Item) = Nam_Ent then
455 Used := True;
456 end if;
458 Next (Use_Item);
459 end loop;
461 -- Package with clause. Avoid processing self, implicitly
462 -- generated with clauses or limited with clauses. Note that
463 -- we examine with clauses having pragmas Elaborate or
464 -- Elaborate_All applied to them due to cases such as:
466 -- with Pack;
467 -- with Pack;
468 -- pragma Elaborate (Pack);
470 -- In this case, the second with clause is redundant since
471 -- the pragma applies only to the first "with Pack;".
473 -- Note that we only consider with_clauses that comes from
474 -- source. In the case of renamings used as prefixes of names
475 -- in with_clauses, we generate a with_clause for the prefix,
476 -- which we do not treat as implicit because it is needed for
477 -- visibility analysis, but is also not redundant.
479 elsif Nkind (Cont_Item) = N_With_Clause
480 and then not Implicit_With (Cont_Item)
481 and then Comes_From_Source (Cont_Item)
482 and then not Limited_Present (Cont_Item)
483 and then Cont_Item /= Clause
484 and then Entity (Name (Cont_Item)) = Nam_Ent
485 then
486 Withed := True;
487 end if;
489 Next (Cont_Item);
490 end loop;
491 end Process_Spec_Clauses;
493 -- Start of processing for Check_Redundant_Withs
495 begin
496 Clause := Last (Context_Items);
497 while Present (Clause) loop
499 -- Avoid checking implicitly generated with clauses, limited with
500 -- clauses or withs that have pragma Elaborate or Elaborate_All.
502 if Nkind (Clause) = N_With_Clause
503 and then not Implicit_With (Clause)
504 and then not Limited_Present (Clause)
505 and then not Elaborate_Present (Clause)
507 -- With_clauses introduced for renamings of parent clauses
508 -- are not marked implicit because they need to be properly
509 -- installed, but they do not come from source and do not
510 -- require warnings.
512 and then Comes_From_Source (Clause)
513 then
514 -- Package body-to-spec check
516 if Present (Spec_Context_Items) then
517 declare
518 Used_In_Body : Boolean := False;
519 Used_In_Spec : Boolean := False;
520 Used_Type_Or_Elab : Boolean := False;
521 Withed_In_Spec : Boolean := False;
523 begin
524 Process_Spec_Clauses
525 (Context_List => Spec_Context_Items,
526 Clause => Clause,
527 Used => Used_In_Spec,
528 Withed => Withed_In_Spec);
530 Process_Body_Clauses
531 (Context_List => Context_Items,
532 Clause => Clause,
533 Used => Used_In_Body,
534 Used_Type_Or_Elab => Used_Type_Or_Elab);
536 -- "Type Elab" refers to the presence of either a use
537 -- type clause, pragmas Elaborate or Elaborate_All.
539 -- +---------------+---------------------------+------+
540 -- | Spec | Body | Warn |
541 -- +--------+------+--------+------+-----------+------+
542 -- | Withed | Used | Withed | Used | Type Elab | |
543 -- | X | | X | | | X |
544 -- | X | | X | X | | |
545 -- | X | | X | | X | |
546 -- | X | | X | X | X | |
547 -- | X | X | X | | | X |
548 -- | X | X | X | | X | |
549 -- | X | X | X | X | | X |
550 -- | X | X | X | X | X | |
551 -- +--------+------+--------+------+-----------+------+
553 if (Withed_In_Spec
554 and then not Used_Type_Or_Elab)
555 and then
556 ((not Used_In_Spec and then not Used_In_Body)
557 or else Used_In_Spec)
558 then
559 Error_Msg_N -- CODEFIX
560 ("redundant with clause in body??", Clause);
561 end if;
563 Used_In_Body := False;
564 Used_In_Spec := False;
565 Used_Type_Or_Elab := False;
566 Withed_In_Spec := False;
567 end;
569 -- Standalone package spec or body check
571 else
572 declare
573 Dont_Care : Boolean := False;
574 Withed : Boolean := False;
576 begin
577 -- The mechanism for examining the context clauses of a
578 -- package spec can be applied to package body clauses.
580 Process_Spec_Clauses
581 (Context_List => Context_Items,
582 Clause => Clause,
583 Used => Dont_Care,
584 Withed => Withed,
585 Exit_On_Self => True);
587 if Withed then
588 Error_Msg_N -- CODEFIX
589 ("redundant with clause??", Clause);
590 end if;
591 end;
592 end if;
593 end if;
595 Prev (Clause);
596 end loop;
597 end Check_Redundant_Withs;
599 -- Local variables
601 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
602 Unit_Node : constant Node_Id := Unit (N);
603 Lib_Unit : Node_Id := Library_Unit (N);
604 Par_Spec_Name : Unit_Name_Type;
605 Spec_Id : Entity_Id;
606 Unum : Unit_Number_Type;
608 -- Start of processing for Analyze_Compilation_Unit
610 begin
611 Process_Compilation_Unit_Pragmas (N);
613 -- If the unit is a subunit whose parent has not been analyzed (which
614 -- indicates that the main unit is a subunit, either the current one or
615 -- one of its descendents) then the subunit is compiled as part of the
616 -- analysis of the parent, which we proceed to do. Basically this gets
617 -- handled from the top down and we don't want to do anything at this
618 -- level (i.e. this subunit will be handled on the way down from the
619 -- parent), so at this level we immediately return. If the subunit ends
620 -- up not analyzed, it means that the parent did not contain a stub for
621 -- it, or that there errors were detected in some ancestor.
623 if Nkind (Unit_Node) = N_Subunit and then not Analyzed (Lib_Unit) then
624 Semantics (Lib_Unit);
626 if not Analyzed (Proper_Body (Unit_Node)) then
627 if Serious_Errors_Detected > 0 then
628 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
629 else
630 Error_Msg_N ("missing stub for subunit", N);
631 end if;
632 end if;
634 return;
635 end if;
637 -- Analyze context (this will call Sem recursively for with'ed units) To
638 -- detect circularities among with-clauses that are not caught during
639 -- loading, we set the Context_Pending flag on the current unit. If the
640 -- flag is already set there is a potential circularity. We exclude
641 -- predefined units from this check because they are known to be safe.
642 -- We also exclude package bodies that are present because circularities
643 -- between bodies are harmless (and necessary).
645 if Context_Pending (N) then
646 declare
647 Circularity : Boolean := True;
649 begin
650 if Is_Predefined_File_Name
651 (Unit_File_Name (Get_Source_Unit (Unit (N))))
652 then
653 Circularity := False;
655 else
656 for U in Main_Unit + 1 .. Last_Unit loop
657 if Nkind (Unit (Cunit (U))) = N_Package_Body
658 and then not Analyzed (Cunit (U))
659 then
660 Circularity := False;
661 exit;
662 end if;
663 end loop;
664 end if;
666 if Circularity then
667 Error_Msg_N ("circular dependency caused by with_clauses", N);
668 Error_Msg_N
669 ("\possibly missing limited_with clause"
670 & " in one of the following", N);
672 for U in Main_Unit .. Last_Unit loop
673 if Context_Pending (Cunit (U)) then
674 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
675 Error_Msg_N ("\unit$", N);
676 end if;
677 end loop;
679 raise Unrecoverable_Error;
680 end if;
681 end;
682 else
683 Set_Context_Pending (N);
684 end if;
686 Analyze_Context (N);
688 Set_Context_Pending (N, False);
690 -- If the unit is a package body, the spec is already loaded and must be
691 -- analyzed first, before we analyze the body.
693 if Nkind (Unit_Node) = N_Package_Body then
695 -- If no Lib_Unit, then there was a serious previous error, so just
696 -- ignore the entire analysis effort
698 if No (Lib_Unit) then
699 Check_Error_Detected;
700 return;
702 else
703 -- Analyze the package spec
705 Semantics (Lib_Unit);
707 -- Check for unused with's
709 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
711 -- Verify that the library unit is a package declaration
713 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
714 N_Generic_Package_Declaration)
715 then
716 Error_Msg_N
717 ("no legal package declaration for package body", N);
718 return;
720 -- Otherwise, the entity in the declaration is visible. Update the
721 -- version to reflect dependence of this body on the spec.
723 else
724 Spec_Id := Defining_Entity (Unit (Lib_Unit));
725 Set_Is_Immediately_Visible (Spec_Id, True);
726 Version_Update (N, Lib_Unit);
728 if Nkind (Defining_Unit_Name (Unit_Node)) =
729 N_Defining_Program_Unit_Name
730 then
731 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
732 end if;
733 end if;
734 end if;
736 -- If the unit is a subprogram body, then we similarly need to analyze
737 -- its spec. However, things are a little simpler in this case, because
738 -- here, this analysis is done mostly for error checking and consistency
739 -- purposes (but not only, e.g. there could be a contract on the spec),
740 -- so there's nothing else to be done.
742 elsif Nkind (Unit_Node) = N_Subprogram_Body then
743 if Acts_As_Spec (N) then
745 -- If the subprogram body is a child unit, we must create a
746 -- declaration for it, in order to properly load the parent(s).
747 -- After this, the original unit does not acts as a spec, because
748 -- there is an explicit one. If this unit appears in a context
749 -- clause, then an implicit with on the parent will be added when
750 -- installing the context. If this is the main unit, there is no
751 -- Unit_Table entry for the declaration (it has the unit number
752 -- of the main unit) and code generation is unaffected.
754 Unum := Get_Cunit_Unit_Number (N);
755 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
757 if Par_Spec_Name /= No_Unit_Name then
758 Unum :=
759 Load_Unit
760 (Load_Name => Par_Spec_Name,
761 Required => True,
762 Subunit => False,
763 Error_Node => N);
765 if Unum /= No_Unit then
767 -- Build subprogram declaration and attach parent unit to it
768 -- This subprogram declaration does not come from source,
769 -- Nevertheless the backend must generate debugging info for
770 -- it, and this must be indicated explicitly. We also mark
771 -- the body entity as a child unit now, to prevent a
772 -- cascaded error if the spec entity cannot be entered
773 -- in its scope. Finally we create a Units table entry for
774 -- the subprogram declaration, to maintain a one-to-one
775 -- correspondence with compilation unit nodes. This is
776 -- critical for the tree traversals performed by CodePeer.
778 declare
779 Loc : constant Source_Ptr := Sloc (N);
780 SCS : constant Boolean :=
781 Get_Comes_From_Source_Default;
783 begin
784 Set_Comes_From_Source_Default (False);
786 -- Checks for redundant USE TYPE clauses have a special
787 -- exception for the synthetic spec we create here. This
788 -- special case relies on the two compilation units
789 -- sharing the same context clause.
791 -- Note: We used to do a shallow copy (New_Copy_List),
792 -- which defeated those checks and also created malformed
793 -- trees (subtype mark shared by two distinct
794 -- N_Use_Type_Clause nodes) which crashed the compiler.
796 Lib_Unit :=
797 Make_Compilation_Unit (Loc,
798 Context_Items => Context_Items (N),
799 Unit =>
800 Make_Subprogram_Declaration (Sloc (N),
801 Specification =>
802 Copy_Separate_Tree
803 (Specification (Unit_Node))),
804 Aux_Decls_Node =>
805 Make_Compilation_Unit_Aux (Loc));
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);
819 end;
820 end if;
821 end if;
823 -- Here for subprogram with separate declaration
825 else
826 Semantics (Lib_Unit);
827 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
828 Version_Update (N, Lib_Unit);
829 end if;
831 -- If this is a child unit, generate references to the parents
833 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
834 N_Defining_Program_Unit_Name
835 then
836 Generate_Parent_References
837 (Specification (Unit_Node),
838 Scope (Defining_Entity (Unit (Lib_Unit))));
839 end if;
840 end if;
842 -- If it is a child unit, the parent must be elaborated first and we
843 -- update version, since we are dependent on our parent.
845 if Is_Child_Spec (Unit_Node) then
847 -- The analysis of the parent is done with style checks off
849 declare
850 Save_Style_Check : constant Boolean := Style_Check;
852 begin
853 if not GNAT_Mode then
854 Style_Check := False;
855 end if;
857 Semantics (Parent_Spec (Unit_Node));
858 Version_Update (N, Parent_Spec (Unit_Node));
860 -- Restore style check settings
862 Style_Check := Save_Style_Check;
863 end;
864 end if;
866 -- With the analysis done, install the context. Note that we can't
867 -- install the context from the with clauses as we analyze them, because
868 -- each with clause must be analyzed in a clean visibility context, so
869 -- we have to wait and install them all at once.
871 Install_Context (N);
873 if Is_Child_Spec (Unit_Node) then
875 -- Set the entities of all parents in the program_unit_name
877 Generate_Parent_References
878 (Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
879 end if;
881 -- All components of the context: with-clauses, library unit, ancestors
882 -- if any, (and their context) are analyzed and installed.
884 -- Call special debug routine sm if this is the main unit
886 if Current_Sem_Unit = Main_Unit then
888 end if;
890 -- Now analyze the unit (package, subprogram spec, body) itself
892 Analyze (Unit_Node);
894 if Warn_On_Redundant_Constructs then
895 Check_Redundant_Withs (Context_Items (N));
897 if Nkind (Unit_Node) = N_Package_Body then
898 Check_Redundant_Withs
899 (Context_Items => Context_Items (N),
900 Spec_Context_Items => Context_Items (Lib_Unit));
901 end if;
902 end if;
904 -- The above call might have made Unit_Node an N_Subprogram_Body from
905 -- something else, so propagate any Acts_As_Spec flag.
907 if Nkind (Unit_Node) = N_Subprogram_Body
908 and then Acts_As_Spec (Unit_Node)
909 then
910 Set_Acts_As_Spec (N);
911 end if;
913 -- Register predefined units in Rtsfind
915 declare
916 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
917 begin
918 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
919 Set_RTU_Loaded (Unit_Node);
920 end if;
921 end;
923 -- Treat compilation unit pragmas that appear after the library unit
925 if Present (Pragmas_After (Aux_Decls_Node (N))) then
926 declare
927 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
928 begin
929 while Present (Prag_Node) loop
930 Analyze (Prag_Node);
931 Next (Prag_Node);
932 end loop;
933 end;
934 end if;
936 -- Analyze the contract of a [generic] subprogram that acts as a
937 -- compilation unit after all compilation pragmas have been analyzed.
939 if Nkind_In (Unit_Node, N_Generic_Subprogram_Declaration,
940 N_Subprogram_Declaration)
941 then
942 Analyze_Subprogram_Contract (Defining_Entity (Unit_Node));
944 -- Capture all global references in a generic subprogram that acts as
945 -- a compilation unit now that the contract has been analyzed.
947 if Is_Generic_Declaration_Or_Body (Unit_Node) then
948 Save_Global_References_In_Contract
949 (Templ => Original_Node (Unit_Node),
950 Gen_Id => Defining_Entity (Unit_Node));
951 end if;
952 end if;
954 -- Generate distribution stubs if requested and no error
956 if N = Main_Cunit
957 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
958 or else
959 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
960 and then Fatal_Error (Main_Unit) /= Error_Detected
961 then
962 if Is_RCI_Pkg_Spec_Or_Body (N) then
964 -- Regular RCI package
966 Add_Stub_Constructs (N);
968 elsif (Nkind (Unit_Node) = N_Package_Declaration
969 and then Is_Shared_Passive (Defining_Entity
970 (Specification (Unit_Node))))
971 or else (Nkind (Unit_Node) = N_Package_Body
972 and then
973 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
974 then
975 -- Shared passive package
977 Add_Stub_Constructs (N);
979 elsif Nkind (Unit_Node) = N_Package_Instantiation
980 and then
981 Is_Remote_Call_Interface
982 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
983 then
984 -- Instantiation of a RCI generic package
986 Add_Stub_Constructs (N);
987 end if;
988 end if;
990 -- Remove unit from visibility, so that environment is clean for the
991 -- next compilation, which is either the main unit or some other unit
992 -- in the context.
994 if Nkind_In (Unit_Node, N_Package_Declaration,
995 N_Package_Renaming_Declaration,
996 N_Subprogram_Declaration)
997 or else Nkind (Unit_Node) in N_Generic_Declaration
998 or else (Nkind (Unit_Node) = N_Subprogram_Body
999 and then Acts_As_Spec (Unit_Node))
1000 then
1001 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
1003 -- If the unit is an instantiation whose body will be elaborated for
1004 -- inlining purposes, use the proper entity of the instance. The entity
1005 -- may be missing if the instantiation was illegal.
1007 elsif Nkind (Unit_Node) = N_Package_Instantiation
1008 and then not Error_Posted (Unit_Node)
1009 and then Present (Instance_Spec (Unit_Node))
1010 then
1011 Remove_Unit_From_Visibility
1012 (Defining_Entity (Instance_Spec (Unit_Node)));
1014 elsif Nkind (Unit_Node) = N_Package_Body
1015 or else (Nkind (Unit_Node) = N_Subprogram_Body
1016 and then not Acts_As_Spec (Unit_Node))
1017 then
1018 -- Bodies that are not the main unit are compiled if they are generic
1019 -- or contain generic or inlined units. Their analysis brings in the
1020 -- context of the corresponding spec (unit declaration) which must be
1021 -- removed as well, to return the compilation environment to its
1022 -- proper state.
1024 Remove_Context (Lib_Unit);
1025 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1026 end if;
1028 -- Last step is to deinstall the context we just installed as well as
1029 -- the unit just compiled.
1031 Remove_Context (N);
1033 -- When generating code for a non-generic main unit, check that withed
1034 -- generic units have a body if they need it, even if the units have not
1035 -- been instantiated. Force the load of the bodies to produce the proper
1036 -- error if the body is absent. The same applies to GNATprove mode, with
1037 -- the added benefit of capturing global references within the generic.
1038 -- This in turn allows for proper inlining of subprogram bodies without
1039 -- a previous declaration.
1041 if Get_Cunit_Unit_Number (N) = Main_Unit
1042 and then ((Operating_Mode = Generate_Code and then Expander_Active)
1043 or else
1044 (Operating_Mode = Check_Semantics and then GNATprove_Mode))
1045 then
1046 -- Check whether the source for the body of the unit must be included
1047 -- in a standalone library.
1049 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1051 -- Indicate that the main unit is now analyzed, to catch possible
1052 -- circularities between it and generic bodies. Remove main unit from
1053 -- visibility. This might seem superfluous, but the main unit must
1054 -- not be visible in the generic body expansions that follow.
1056 Set_Analyzed (N, True);
1057 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1059 declare
1060 Item : Node_Id;
1061 Nam : Entity_Id;
1062 Un : Unit_Number_Type;
1064 Save_Style_Check : constant Boolean := Style_Check;
1066 begin
1067 Item := First (Context_Items (N));
1068 while Present (Item) loop
1070 -- Check for explicit with clause
1072 if Nkind (Item) = N_With_Clause
1073 and then not Implicit_With (Item)
1075 -- Ada 2005 (AI-50217): Ignore limited-withed units
1077 and then not Limited_Present (Item)
1078 then
1079 Nam := Entity (Name (Item));
1081 -- Compile the generic subprogram, unless it is intrinsic or
1082 -- imported so no body is required, or generic package body
1083 -- if the package spec requires a body.
1085 if (Is_Generic_Subprogram (Nam)
1086 and then not Is_Intrinsic_Subprogram (Nam)
1087 and then not Is_Imported (Nam))
1088 or else (Ekind (Nam) = E_Generic_Package
1089 and then Unit_Requires_Body (Nam))
1090 then
1091 Style_Check := False;
1093 if Present (Renamed_Object (Nam)) then
1094 Un :=
1095 Load_Unit
1096 (Load_Name =>
1097 Get_Body_Name
1098 (Get_Unit_Name
1099 (Unit_Declaration_Node
1100 (Renamed_Object (Nam)))),
1101 Required => False,
1102 Subunit => False,
1103 Error_Node => N,
1104 Renamings => True);
1105 else
1106 Un :=
1107 Load_Unit
1108 (Load_Name =>
1109 Get_Body_Name (Get_Unit_Name (Item)),
1110 Required => False,
1111 Subunit => False,
1112 Error_Node => N,
1113 Renamings => True);
1114 end if;
1116 if Un = No_Unit then
1117 Error_Msg_NE
1118 ("body of generic unit& not found", Item, Nam);
1119 exit;
1121 elsif not Analyzed (Cunit (Un))
1122 and then Un /= Main_Unit
1123 and then Fatal_Error (Un) /= Error_Detected
1124 then
1125 Style_Check := False;
1126 Semantics (Cunit (Un));
1127 end if;
1128 end if;
1129 end if;
1131 Next (Item);
1132 end loop;
1134 -- Restore style checks settings
1136 Style_Check := Save_Style_Check;
1137 end;
1138 end if;
1140 -- Deal with creating elaboration counter if needed. We create an
1141 -- elaboration counter only for units that come from source since
1142 -- units manufactured by the compiler never need elab checks.
1144 if Comes_From_Source (N)
1145 and then Nkind_In (Unit_Node, N_Package_Declaration,
1146 N_Generic_Package_Declaration,
1147 N_Subprogram_Declaration,
1148 N_Generic_Subprogram_Declaration)
1149 then
1150 declare
1151 Loc : constant Source_Ptr := Sloc (N);
1152 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1154 begin
1155 Spec_Id := Defining_Entity (Unit_Node);
1156 Generate_Definition (Spec_Id);
1158 -- See if an elaboration entity is required for possible access
1159 -- before elaboration checking. Note that we must allow for this
1160 -- even if -gnatE is not set, since a client may be compiled in
1161 -- -gnatE mode and reference the entity.
1163 -- These entities are also used by the binder to prevent multiple
1164 -- attempts to execute the elaboration code for the library case
1165 -- where the elaboration routine might otherwise be called more
1166 -- than once.
1168 -- Case of units which do not require elaboration checks
1171 -- Pure units do not need checks
1173 Is_Pure (Spec_Id)
1175 -- Preelaborated units do not need checks
1177 or else Is_Preelaborated (Spec_Id)
1179 -- No checks needed if pragma Elaborate_Body present
1181 or else Has_Pragma_Elaborate_Body (Spec_Id)
1183 -- No checks needed if unit does not require a body
1185 or else not Unit_Requires_Body (Spec_Id)
1187 -- No checks needed for predefined files
1189 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1191 -- No checks required if no separate spec
1193 or else Acts_As_Spec (N)
1194 then
1195 -- This is a case where we only need the entity for
1196 -- checking to prevent multiple elaboration checks.
1198 Set_Elaboration_Entity_Required (Spec_Id, False);
1200 -- Case of elaboration entity is required for access before
1201 -- elaboration checking (so certainly we must build it).
1203 else
1204 Set_Elaboration_Entity_Required (Spec_Id, True);
1205 end if;
1207 Build_Elaboration_Entity (N, Spec_Id);
1208 end;
1209 end if;
1211 -- Freeze the compilation unit entity. This for sure is needed because
1212 -- of some warnings that can be output (see Freeze_Subprogram), but may
1213 -- in general be required. If freezing actions result, place them in the
1214 -- compilation unit actions list, and analyze them.
1216 declare
1217 L : constant List_Id :=
1218 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1219 begin
1220 while Is_Non_Empty_List (L) loop
1221 Insert_Library_Level_Action (Remove_Head (L));
1222 end loop;
1223 end;
1225 Set_Analyzed (N);
1227 -- Call Check_Package_Body so that a body containing subprograms with
1228 -- Inline_Always can be made available for front end inlining.
1230 if Nkind (Unit_Node) = N_Package_Declaration
1231 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1233 -- We don't need to do this if the Expander is not active, since there
1234 -- is no code to inline.
1236 and then Expander_Active
1237 then
1238 declare
1239 Save_Style_Check : constant Boolean := Style_Check;
1240 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1241 Options : Style_Check_Options;
1243 begin
1244 Save_Style_Check_Options (Options);
1245 Reset_Style_Check_Options;
1246 Opt.Warning_Mode := Suppress;
1248 Check_Package_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1250 Reset_Style_Check_Options;
1251 Set_Style_Check_Options (Options);
1252 Style_Check := Save_Style_Check;
1253 Warning_Mode := Save_Warning;
1254 end;
1255 end if;
1257 -- If we are generating obsolescent warnings, then here is where we
1258 -- generate them for the with'ed items. The reason for this special
1259 -- processing is that the normal mechanism of generating the warnings
1260 -- for referenced entities does not work for context clause references.
1261 -- That's because when we first analyze the context, it is too early to
1262 -- know if the with'ing unit is itself obsolescent (which suppresses
1263 -- the warnings).
1265 if not GNAT_Mode
1266 and then Warn_On_Obsolescent_Feature
1267 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1268 then
1269 -- Push current compilation unit as scope, so that the test for
1270 -- being within an obsolescent unit will work correctly. The check
1271 -- is not performed within an instantiation, because the warning
1272 -- will have been emitted in the corresponding generic unit.
1274 Push_Scope (Defining_Entity (Unit_Node));
1276 -- Loop through context items to deal with with clauses
1278 declare
1279 Item : Node_Id;
1280 Nam : Node_Id;
1281 Ent : Entity_Id;
1283 begin
1284 Item := First (Context_Items (N));
1285 while Present (Item) loop
1286 if Nkind (Item) = N_With_Clause
1288 -- Suppress this check in limited-withed units. Further work
1289 -- needed here if we decide to incorporate this check on
1290 -- limited-withed units.
1292 and then not Limited_Present (Item)
1293 then
1294 Nam := Name (Item);
1295 Ent := Entity (Nam);
1297 if Is_Obsolescent (Ent) then
1298 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1299 end if;
1300 end if;
1302 Next (Item);
1303 end loop;
1304 end;
1306 -- Remove temporary install of current unit as scope
1308 Pop_Scope;
1309 end if;
1311 -- If No_Elaboration_Code_All was encountered, this is where we do the
1312 -- transitive test of with'ed units to make sure they have the aspect.
1313 -- This is delayed till the end of analyzing the compilation unit to
1314 -- ensure that the pragma/aspect, if present, has been analyzed.
1316 Check_No_Elab_Code_All (N);
1317 end Analyze_Compilation_Unit;
1319 ---------------------
1320 -- Analyze_Context --
1321 ---------------------
1323 procedure Analyze_Context (N : Node_Id) is
1324 Ukind : constant Node_Kind := Nkind (Unit (N));
1325 Item : Node_Id;
1327 begin
1328 -- First process all configuration pragmas at the start of the context
1329 -- items. Strictly these are not part of the context clause, but that
1330 -- is where the parser puts them. In any case for sure we must analyze
1331 -- these before analyzing the actual context items, since they can have
1332 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1333 -- be with'ed as a result of changing categorizations in Ada 2005).
1335 Item := First (Context_Items (N));
1336 while Present (Item)
1337 and then Nkind (Item) = N_Pragma
1338 and then Pragma_Name (Item) in Configuration_Pragma_Names
1339 loop
1340 Analyze (Item);
1341 Next (Item);
1342 end loop;
1344 -- This is the point at which we capture the configuration settings
1345 -- for the unit. At the moment only the Optimize_Alignment setting
1346 -- needs to be captured. Probably more later ???
1348 if Optimize_Alignment_Local then
1349 Set_OA_Setting (Current_Sem_Unit, 'L');
1350 else
1351 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1352 end if;
1354 -- Loop through actual context items. This is done in two passes:
1356 -- a) The first pass analyzes non-limited with-clauses and also any
1357 -- configuration pragmas (we need to get the latter analyzed right
1358 -- away, since they can affect processing of subsequent items).
1360 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1362 while Present (Item) loop
1364 -- For with clause, analyze the with clause, and then update the
1365 -- version, since we are dependent on a unit that we with.
1367 if Nkind (Item) = N_With_Clause
1368 and then not Limited_Present (Item)
1369 then
1370 -- Skip analyzing with clause if no unit, nothing to do (this
1371 -- happens for a with that references a non-existent unit).
1373 if Present (Library_Unit (Item)) then
1375 -- Skip analyzing with clause if this is a with_clause for
1376 -- the main unit, which happens if a subunit has a useless
1377 -- with_clause on its parent.
1379 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1380 Analyze (Item);
1382 -- Here for the case of a useless with for the main unit
1384 else
1385 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1386 end if;
1387 end if;
1389 -- Do version update (skipped for implicit with)
1391 if not Implicit_With (Item) then
1392 Version_Update (N, Library_Unit (Item));
1393 end if;
1395 -- Skip pragmas. Configuration pragmas at the start were handled in
1396 -- the loop above, and remaining pragmas are not processed until we
1397 -- actually install the context (see Install_Context). We delay the
1398 -- analysis of these pragmas to make sure that we have installed all
1399 -- the implicit with's on parent units.
1401 -- Skip use clauses at this stage, since we don't want to do any
1402 -- installing of potentially use-visible entities until we
1403 -- actually install the complete context (in Install_Context).
1404 -- Otherwise things can get installed in the wrong context.
1406 else
1407 null;
1408 end if;
1410 Next (Item);
1411 end loop;
1413 -- Second pass: examine all limited_with clauses. All other context
1414 -- items are ignored in this pass.
1416 Item := First (Context_Items (N));
1417 while Present (Item) loop
1418 if Nkind (Item) = N_With_Clause
1419 and then Limited_Present (Item)
1420 then
1421 -- No need to check errors on implicitly generated limited-with
1422 -- clauses.
1424 if not Implicit_With (Item) then
1426 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1427 -- properly rejected, including renaming declarations.
1429 if not Nkind_In (Ukind, N_Package_Declaration,
1430 N_Subprogram_Declaration)
1431 and then Ukind not in N_Generic_Declaration
1432 and then Ukind not in N_Generic_Instantiation
1433 then
1434 Error_Msg_N ("limited with_clause not allowed here", Item);
1436 -- Check wrong use of a limited with clause applied to the
1437 -- compilation unit containing the limited-with clause.
1439 -- limited with P.Q;
1440 -- package P.Q is ...
1442 elsif Unit (Library_Unit (Item)) = Unit (N) then
1443 Error_Msg_N ("wrong use of limited-with clause", Item);
1445 -- Check wrong use of limited-with clause applied to some
1446 -- immediate ancestor.
1448 elsif Is_Child_Spec (Unit (N)) then
1449 declare
1450 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1451 P : Node_Id;
1453 begin
1454 P := Parent_Spec (Unit (N));
1455 loop
1456 if Unit (P) = Lib_U then
1457 Error_Msg_N ("limited with_clause cannot "
1458 & "name ancestor", Item);
1459 exit;
1460 end if;
1462 exit when not Is_Child_Spec (Unit (P));
1463 P := Parent_Spec (Unit (P));
1464 end loop;
1465 end;
1466 end if;
1468 -- Check if the limited-withed unit is already visible through
1469 -- some context clause of the current compilation unit or some
1470 -- ancestor of the current compilation unit.
1472 declare
1473 Lim_Unit_Name : constant Node_Id := Name (Item);
1474 Comp_Unit : Node_Id;
1475 It : Node_Id;
1476 Unit_Name : Node_Id;
1478 begin
1479 Comp_Unit := N;
1480 loop
1481 It := First (Context_Items (Comp_Unit));
1482 while Present (It) loop
1483 if Item /= It
1484 and then Nkind (It) = N_With_Clause
1485 and then not Limited_Present (It)
1486 and then
1487 Nkind_In (Unit (Library_Unit (It)),
1488 N_Package_Declaration,
1489 N_Package_Renaming_Declaration)
1490 then
1491 if Nkind (Unit (Library_Unit (It))) =
1492 N_Package_Declaration
1493 then
1494 Unit_Name := Name (It);
1495 else
1496 Unit_Name := Name (Unit (Library_Unit (It)));
1497 end if;
1499 -- Check if the named package (or some ancestor)
1500 -- leaves visible the full-view of the unit given
1501 -- in the limited-with clause
1503 loop
1504 if Designate_Same_Unit (Lim_Unit_Name,
1505 Unit_Name)
1506 then
1507 Error_Msg_Sloc := Sloc (It);
1508 Error_Msg_N
1509 ("simultaneous visibility of limited "
1510 & "and unlimited views not allowed",
1511 Item);
1512 Error_Msg_NE
1513 ("\unlimited view visible through "
1514 & "context clause #",
1515 Item, It);
1516 exit;
1518 elsif Nkind (Unit_Name) = N_Identifier then
1519 exit;
1520 end if;
1522 Unit_Name := Prefix (Unit_Name);
1523 end loop;
1524 end if;
1526 Next (It);
1527 end loop;
1529 exit when not Is_Child_Spec (Unit (Comp_Unit));
1531 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1532 end loop;
1533 end;
1534 end if;
1536 -- Skip analyzing with clause if no unit, see above
1538 if Present (Library_Unit (Item)) then
1539 Analyze (Item);
1540 end if;
1542 -- A limited_with does not impose an elaboration order, but
1543 -- there is a semantic dependency for recompilation purposes.
1545 if not Implicit_With (Item) then
1546 Version_Update (N, Library_Unit (Item));
1547 end if;
1549 -- Pragmas and use clauses and with clauses other than limited
1550 -- with's are ignored in this pass through the context items.
1552 else
1553 null;
1554 end if;
1556 Next (Item);
1557 end loop;
1558 end Analyze_Context;
1560 -------------------------------
1561 -- Analyze_Package_Body_Stub --
1562 -------------------------------
1564 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1565 Id : constant Entity_Id := Defining_Identifier (N);
1566 Nam : Entity_Id;
1567 Opts : Config_Switches_Type;
1569 begin
1570 -- The package declaration must be in the current declarative part
1572 Check_Stub_Level (N);
1573 Nam := Current_Entity_In_Scope (Id);
1575 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1576 Error_Msg_N ("missing specification for package stub", N);
1578 elsif Has_Completion (Nam)
1579 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1580 then
1581 Error_Msg_N ("duplicate or redundant stub for package", N);
1583 else
1584 -- Retain and restore the configuration options of the enclosing
1585 -- context as the proper body may introduce a set of its own.
1587 Save_Opt_Config_Switches (Opts);
1589 -- Indicate that the body of the package exists. If we are doing
1590 -- only semantic analysis, the stub stands for the body. If we are
1591 -- generating code, the existence of the body will be confirmed
1592 -- when we load the proper body.
1594 Set_Has_Completion (Nam);
1595 Set_Scope (Defining_Entity (N), Current_Scope);
1596 Set_Corresponding_Spec_Of_Stub (N, Nam);
1597 Generate_Reference (Nam, Id, 'b');
1598 Analyze_Proper_Body (N, Nam);
1600 Restore_Opt_Config_Switches (Opts);
1601 end if;
1602 end Analyze_Package_Body_Stub;
1604 -------------------------
1605 -- Analyze_Proper_Body --
1606 -------------------------
1608 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1609 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1611 procedure Optional_Subunit;
1612 -- This procedure is called when the main unit is a stub, or when we
1613 -- are not generating code. In such a case, we analyze the subunit if
1614 -- present, which is user-friendly and in fact required for ASIS, but we
1615 -- don't complain if the subunit is missing. In GNATprove_Mode, we issue
1616 -- an error to avoid formal verification of a partial unit.
1618 ----------------------
1619 -- Optional_Subunit --
1620 ----------------------
1622 procedure Optional_Subunit is
1623 Comp_Unit : Node_Id;
1624 Unum : Unit_Number_Type;
1626 begin
1627 -- Try to load subunit, but ignore any errors that occur during the
1628 -- loading of the subunit, by using the special feature in Errout to
1629 -- ignore all errors. Note that Fatal_Error will still be set, so we
1630 -- will be able to check for this case below.
1632 if not (ASIS_Mode or GNATprove_Mode) then
1633 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1634 end if;
1636 Unum :=
1637 Load_Unit
1638 (Load_Name => Subunit_Name,
1639 Required => GNATprove_Mode,
1640 Subunit => True,
1641 Error_Node => N);
1643 if not (ASIS_Mode or GNATprove_Mode) then
1644 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1645 end if;
1647 -- All done if we successfully loaded the subunit
1649 if Unum /= No_Unit
1650 and then (Fatal_Error (Unum) /= Error_Detected
1651 or else Try_Semantics)
1652 then
1653 Comp_Unit := Cunit (Unum);
1655 -- If the file was empty or seriously mangled, the unit itself may
1656 -- be missing.
1658 if No (Unit (Comp_Unit)) then
1659 Error_Msg_N
1660 ("subunit does not contain expected proper body", N);
1662 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1663 Error_Msg_N
1664 ("expected SEPARATE subunit, found child unit",
1665 Cunit_Entity (Unum));
1666 else
1667 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1668 Analyze_Subunit (Comp_Unit);
1669 Set_Library_Unit (N, Comp_Unit);
1670 Set_Corresponding_Body (N, Defining_Entity (Unit (Comp_Unit)));
1671 end if;
1673 elsif Unum = No_Unit
1674 and then Present (Nam)
1675 then
1676 if Is_Protected_Type (Nam) then
1677 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1678 else
1679 Set_Corresponding_Body (
1680 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1681 end if;
1682 end if;
1683 end Optional_Subunit;
1685 -- Local variables
1687 Comp_Unit : Node_Id;
1688 Unum : Unit_Number_Type;
1690 -- Start of processing for Analyze_Proper_Body
1692 begin
1693 -- If the subunit is already loaded, it means that the main unit is a
1694 -- subunit, and that the current unit is one of its parents which was
1695 -- being analyzed to provide the needed context for the analysis of the
1696 -- subunit. In this case we analyze the subunit and continue with the
1697 -- parent, without looking at subsequent subunits.
1699 if Is_Loaded (Subunit_Name) then
1701 -- If the proper body is already linked to the stub node, the stub is
1702 -- in a generic unit and just needs analyzing.
1704 if Present (Library_Unit (N)) then
1705 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1707 -- If the subunit has severe errors, the spec of the enclosing
1708 -- body may not be available, in which case do not try analysis.
1710 if Serious_Errors_Detected > 0
1711 and then No (Library_Unit (Library_Unit (N)))
1712 then
1713 return;
1714 end if;
1716 -- Collect SCO information for loaded subunit if we are in the
1717 -- extended main unit.
1719 if Generate_SCO
1720 and then In_Extended_Main_Source_Unit
1721 (Cunit_Entity (Current_Sem_Unit))
1722 then
1723 SCO_Record_Raw (Get_Cunit_Unit_Number (Library_Unit (N)));
1724 end if;
1726 Analyze_Subunit (Library_Unit (N));
1728 -- Otherwise we must load the subunit and link to it
1730 else
1731 -- Load the subunit, this must work, since we originally loaded
1732 -- the subunit earlier on. So this will not really load it, just
1733 -- give access to it.
1735 Unum :=
1736 Load_Unit
1737 (Load_Name => Subunit_Name,
1738 Required => True,
1739 Subunit => False,
1740 Error_Node => N);
1742 -- And analyze the subunit in the parent context (note that we
1743 -- do not call Semantics, since that would remove the parent
1744 -- context). Because of this, we have to manually reset the
1745 -- compiler state to Analyzing since it got destroyed by Load.
1747 if Unum /= No_Unit then
1748 Compiler_State := Analyzing;
1750 -- Check that the proper body is a subunit and not a child
1751 -- unit. If the unit was previously loaded, the error will
1752 -- have been emitted when copying the generic node, so we
1753 -- just return to avoid cascaded errors.
1755 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1756 return;
1757 end if;
1759 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1760 Analyze_Subunit (Cunit (Unum));
1761 Set_Library_Unit (N, Cunit (Unum));
1762 end if;
1763 end if;
1765 -- If the main unit is a subunit, then we are just performing semantic
1766 -- analysis on that subunit, and any other subunits of any parent unit
1767 -- should be ignored, except that if we are building trees for ASIS
1768 -- usage we want to annotate the stub properly. If the main unit is
1769 -- itself a subunit, another subunit is irrelevant unless it is a
1770 -- subunit of the current one, that is to say appears in the current
1771 -- source tree.
1773 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1774 and then Subunit_Name /= Unit_Name (Main_Unit)
1775 then
1776 if ASIS_Mode then
1777 declare
1778 PB : constant Node_Id := Proper_Body (Unit (Cunit (Main_Unit)));
1779 begin
1780 if Nkind_In (PB, N_Package_Body, N_Subprogram_Body)
1781 and then List_Containing (N) = Declarations (PB)
1782 then
1783 Optional_Subunit;
1784 end if;
1785 end;
1786 end if;
1788 -- But before we return, set the flag for unloaded subunits. This
1789 -- will suppress junk warnings of variables in the same declarative
1790 -- part (or a higher level one) that are in danger of looking unused
1791 -- when in fact there might be a declaration in the subunit that we
1792 -- do not intend to load.
1794 Unloaded_Subunits := True;
1795 return;
1797 -- If the subunit is not already loaded, and we are generating code,
1798 -- then this is the case where compilation started from the parent, and
1799 -- we are generating code for an entire subunit tree. In that case we
1800 -- definitely need to load the subunit.
1802 -- In order to continue the analysis with the rest of the parent,
1803 -- and other subunits, we load the unit without requiring its
1804 -- presence, and emit a warning if not found, rather than terminating
1805 -- the compilation abruptly, as for other missing file problems.
1807 elsif Original_Operating_Mode = Generate_Code then
1809 -- If the proper body is already linked to the stub node, the stub is
1810 -- in a generic unit and just needs analyzing.
1812 -- We update the version. Although we are not strictly technically
1813 -- semantically dependent on the subunit, given our approach of macro
1814 -- substitution of subunits, it makes sense to include it in the
1815 -- version identification.
1817 if Present (Library_Unit (N)) then
1818 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1819 Analyze_Subunit (Library_Unit (N));
1820 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1822 -- Otherwise we must load the subunit and link to it
1824 else
1825 -- Make sure that, if the subunit is preprocessed and -gnateG is
1826 -- specified, the preprocessed file will be written.
1828 Lib.Analysing_Subunit_Of_Main := True;
1829 Unum :=
1830 Load_Unit
1831 (Load_Name => Subunit_Name,
1832 Required => False,
1833 Subunit => True,
1834 Error_Node => N);
1835 Lib.Analysing_Subunit_Of_Main := False;
1837 -- Give message if we did not get the unit Emit warning even if
1838 -- missing subunit is not within main unit, to simplify debugging.
1840 if Original_Operating_Mode = Generate_Code
1841 and then Unum = No_Unit
1842 then
1843 Error_Msg_Unit_1 := Subunit_Name;
1844 Error_Msg_File_1 :=
1845 Get_File_Name (Subunit_Name, Subunit => True);
1846 Error_Msg_N
1847 ("subunit$$ in file{ not found??!!", N);
1848 Subunits_Missing := True;
1849 end if;
1851 -- Load_Unit may reset Compiler_State, since it may have been
1852 -- necessary to parse an additional units, so we make sure that
1853 -- we reset it to the Analyzing state.
1855 Compiler_State := Analyzing;
1857 if Unum /= No_Unit then
1858 if Debug_Flag_L then
1859 Write_Str ("*** Loaded subunit from stub. Analyze");
1860 Write_Eol;
1861 end if;
1863 Comp_Unit := Cunit (Unum);
1865 -- Check for child unit instead of subunit
1867 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1868 Error_Msg_N
1869 ("expected SEPARATE subunit, found child unit",
1870 Cunit_Entity (Unum));
1872 -- OK, we have a subunit
1874 else
1875 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1876 Set_Library_Unit (N, Comp_Unit);
1878 -- We update the version. Although we are not technically
1879 -- semantically dependent on the subunit, given our approach
1880 -- of macro substitution of subunits, it makes sense to
1881 -- include it in the version identification.
1883 Version_Update (Cunit (Main_Unit), Comp_Unit);
1885 -- Collect SCO information for loaded subunit if we are in
1886 -- the extended main unit.
1888 if Generate_SCO
1889 and then
1890 In_Extended_Main_Source_Unit
1891 (Cunit_Entity (Current_Sem_Unit))
1892 then
1893 SCO_Record_Raw (Unum);
1894 end if;
1896 -- Analyze the unit if semantics active
1898 if Fatal_Error (Unum) /= Error_Detected
1899 or else Try_Semantics
1900 then
1901 Analyze_Subunit (Comp_Unit);
1902 end if;
1903 end if;
1904 end if;
1905 end if;
1907 -- The remaining case is when the subunit is not already loaded and we
1908 -- are not generating code. In this case we are just performing semantic
1909 -- analysis on the parent, and we are not interested in the subunit. For
1910 -- subprograms, analyze the stub as a body. For other entities the stub
1911 -- has already been marked as completed.
1913 else
1914 Optional_Subunit;
1915 end if;
1916 end Analyze_Proper_Body;
1918 ----------------------------------
1919 -- Analyze_Protected_Body_Stub --
1920 ----------------------------------
1922 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1923 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1925 begin
1926 Check_Stub_Level (N);
1928 -- First occurrence of name may have been as an incomplete type
1930 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1931 Nam := Full_View (Nam);
1932 end if;
1934 if No (Nam) or else not Is_Protected_Type (Etype (Nam)) then
1935 Error_Msg_N ("missing specification for Protected body", N);
1937 else
1938 -- Currently there are no language-defined aspects that can apply to
1939 -- a protected body stub. Issue an error and remove the aspects to
1940 -- prevent cascaded errors.
1942 if Has_Aspects (N) then
1943 Error_Msg_N
1944 ("aspects on protected bodies are not allowed",
1945 First (Aspect_Specifications (N)));
1946 Remove_Aspects (N);
1947 end if;
1949 Set_Scope (Defining_Entity (N), Current_Scope);
1950 Set_Has_Completion (Etype (Nam));
1951 Set_Corresponding_Spec_Of_Stub (N, Nam);
1952 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1953 Analyze_Proper_Body (N, Etype (Nam));
1954 end if;
1955 end Analyze_Protected_Body_Stub;
1957 ----------------------------------
1958 -- Analyze_Subprogram_Body_Stub --
1959 ----------------------------------
1961 -- A subprogram body stub can appear with or without a previous spec. If
1962 -- there is one, then the analysis of the body will find it and verify
1963 -- conformance. The formals appearing in the specification of the stub play
1964 -- no role, except for requiring an additional conformance check. If there
1965 -- is no previous subprogram declaration, the stub acts as a spec, and
1966 -- provides the defining entity for the subprogram.
1968 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1969 Decl : Node_Id;
1970 Opts : Config_Switches_Type;
1972 begin
1973 Check_Stub_Level (N);
1975 -- Verify that the identifier for the stub is unique within this
1976 -- declarative part.
1978 if Nkind_In (Parent (N), N_Block_Statement,
1979 N_Package_Body,
1980 N_Subprogram_Body)
1981 then
1982 Decl := First (Declarations (Parent (N)));
1983 while Present (Decl) and then Decl /= N loop
1984 if Nkind (Decl) = N_Subprogram_Body_Stub
1985 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1986 Chars (Defining_Unit_Name (Specification (N))))
1987 then
1988 Error_Msg_N ("identifier for stub is not unique", N);
1989 end if;
1991 Next (Decl);
1992 end loop;
1993 end if;
1995 -- Retain and restore the configuration options of the enclosing context
1996 -- as the proper body may introduce a set of its own.
1998 Save_Opt_Config_Switches (Opts);
2000 -- Treat stub as a body, which checks conformance if there is a previous
2001 -- declaration, or else introduces entity and its signature.
2003 Analyze_Subprogram_Body (N);
2004 Analyze_Proper_Body (N, Empty);
2006 Restore_Opt_Config_Switches (Opts);
2007 end Analyze_Subprogram_Body_Stub;
2009 -------------------------------------------
2010 -- Analyze_Subprogram_Body_Stub_Contract --
2011 -------------------------------------------
2013 procedure Analyze_Subprogram_Body_Stub_Contract (Stub_Id : Entity_Id) is
2014 Stub_Decl : constant Node_Id := Parent (Parent (Stub_Id));
2015 Spec_Id : constant Entity_Id := Corresponding_Spec_Of_Stub (Stub_Decl);
2017 begin
2018 -- A subprogram body stub may act as its own spec or as the completion
2019 -- of a previous declaration. Depending on the context, the contract of
2020 -- the stub may contain two sets of pragmas.
2022 -- The stub is a completion, the applicable pragmas are:
2023 -- Refined_Depends
2024 -- Refined_Global
2026 if Present (Spec_Id) then
2027 Analyze_Subprogram_Body_Contract (Stub_Id);
2029 -- The stub acts as its own spec, the applicable pragmas are:
2030 -- Contract_Cases
2031 -- Depends
2032 -- Global
2033 -- Postcondition
2034 -- Precondition
2035 -- Test_Case
2037 else
2038 Analyze_Subprogram_Contract (Stub_Id);
2039 end if;
2040 end Analyze_Subprogram_Body_Stub_Contract;
2042 ---------------------
2043 -- Analyze_Subunit --
2044 ---------------------
2046 -- A subunit is compiled either by itself (for semantic checking) or as
2047 -- part of compiling the parent (for code generation). In either case, by
2048 -- the time we actually process the subunit, the parent has already been
2049 -- installed and analyzed. The node N is a compilation unit, whose context
2050 -- needs to be treated here, because we come directly here from the parent
2051 -- without calling Analyze_Compilation_Unit.
2053 -- The compilation context includes the explicit context of the subunit,
2054 -- and the context of the parent, together with the parent itself. In order
2055 -- to compile the current context, we remove the one inherited from the
2056 -- parent, in order to have a clean visibility table. We restore the parent
2057 -- context before analyzing the proper body itself. On exit, we remove only
2058 -- the explicit context of the subunit.
2060 procedure Analyze_Subunit (N : Node_Id) is
2061 Lib_Unit : constant Node_Id := Library_Unit (N);
2062 Par_Unit : constant Entity_Id := Current_Scope;
2064 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
2065 Num_Scopes : Int := 0;
2066 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2067 Enclosing_Child : Entity_Id := Empty;
2068 Svg : constant Suppress_Record := Scope_Suppress;
2070 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
2071 Cunit_Boolean_Restrictions_Save;
2072 -- Save non-partition wide restrictions before processing the subunit.
2073 -- All subunits are analyzed with config restrictions reset and we need
2074 -- to restore these saved values at the end.
2076 procedure Analyze_Subunit_Context;
2077 -- Capture names in use clauses of the subunit. This must be done before
2078 -- re-installing parent declarations, because items in the context must
2079 -- not be hidden by declarations local to the parent.
2081 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
2082 -- Recursive procedure to restore scope of all ancestors of subunit,
2083 -- from outermost in. If parent is not a subunit, the call to install
2084 -- context installs context of spec and (if parent is a child unit) the
2085 -- context of its parents as well. It is confusing that parents should
2086 -- be treated differently in both cases, but the semantics are just not
2087 -- identical.
2089 procedure Re_Install_Use_Clauses;
2090 -- As part of the removal of the parent scope, the use clauses are
2091 -- removed, to be reinstalled when the context of the subunit has been
2092 -- analyzed. Use clauses may also have been affected by the analysis of
2093 -- the context of the subunit, so they have to be applied again, to
2094 -- insure that the compilation environment of the rest of the parent
2095 -- unit is identical.
2097 procedure Remove_Scope;
2098 -- Remove current scope from scope stack, and preserve the list of use
2099 -- clauses in it, to be reinstalled after context is analyzed.
2101 -----------------------------
2102 -- Analyze_Subunit_Context --
2103 -----------------------------
2105 procedure Analyze_Subunit_Context is
2106 Item : Node_Id;
2107 Nam : Node_Id;
2108 Unit_Name : Entity_Id;
2110 begin
2111 Analyze_Context (N);
2112 Check_No_Elab_Code_All (N);
2114 -- Make withed units immediately visible. If child unit, make the
2115 -- ultimate parent immediately visible.
2117 Item := First (Context_Items (N));
2118 while Present (Item) loop
2119 if Nkind (Item) = N_With_Clause then
2121 -- Protect frontend against previous errors in context clauses
2123 if Nkind (Name (Item)) /= N_Selected_Component then
2124 if Error_Posted (Item) then
2125 null;
2127 else
2128 -- If a subunits has serious syntax errors, the context
2129 -- may not have been loaded. Add a harmless unit name to
2130 -- attempt processing.
2132 if Serious_Errors_Detected > 0
2133 and then No (Entity (Name (Item)))
2134 then
2135 Set_Entity (Name (Item), Standard_Standard);
2136 end if;
2138 Unit_Name := Entity (Name (Item));
2139 loop
2140 Set_Is_Visible_Lib_Unit (Unit_Name);
2141 exit when Scope (Unit_Name) = Standard_Standard;
2142 Unit_Name := Scope (Unit_Name);
2144 if No (Unit_Name) then
2145 Check_Error_Detected;
2146 return;
2147 end if;
2148 end loop;
2150 if not Is_Immediately_Visible (Unit_Name) then
2151 Set_Is_Immediately_Visible (Unit_Name);
2152 Set_Context_Installed (Item);
2153 end if;
2154 end if;
2155 end if;
2157 elsif Nkind (Item) = N_Use_Package_Clause then
2158 Nam := First (Names (Item));
2159 while Present (Nam) loop
2160 Analyze (Nam);
2161 Next (Nam);
2162 end loop;
2164 elsif Nkind (Item) = N_Use_Type_Clause then
2165 Nam := First (Subtype_Marks (Item));
2166 while Present (Nam) loop
2167 Analyze (Nam);
2168 Next (Nam);
2169 end loop;
2170 end if;
2172 Next (Item);
2173 end loop;
2175 -- Reset visibility of withed units. They will be made visible again
2176 -- when we install the subunit context.
2178 Item := First (Context_Items (N));
2179 while Present (Item) loop
2180 if Nkind (Item) = N_With_Clause
2182 -- Protect frontend against previous errors in context clauses
2184 and then Nkind (Name (Item)) /= N_Selected_Component
2185 and then not Error_Posted (Item)
2186 then
2187 Unit_Name := Entity (Name (Item));
2188 loop
2189 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2190 exit when Scope (Unit_Name) = Standard_Standard;
2191 Unit_Name := Scope (Unit_Name);
2192 end loop;
2194 if Context_Installed (Item) then
2195 Set_Is_Immediately_Visible (Unit_Name, False);
2196 Set_Context_Installed (Item, False);
2197 end if;
2198 end if;
2200 Next (Item);
2201 end loop;
2202 end Analyze_Subunit_Context;
2204 ------------------------
2205 -- Re_Install_Parents --
2206 ------------------------
2208 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2209 E : Entity_Id;
2211 begin
2212 if Nkind (Unit (L)) = N_Subunit then
2213 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2214 end if;
2216 Install_Context (L);
2218 -- If the subunit occurs within a child unit, we must restore the
2219 -- immediate visibility of any siblings that may occur in context.
2221 if Present (Enclosing_Child) then
2222 Install_Siblings (Enclosing_Child, L);
2223 end if;
2225 Push_Scope (Scop);
2227 if Scop /= Par_Unit then
2228 Set_Is_Immediately_Visible (Scop);
2229 end if;
2231 -- Make entities in scope visible again. For child units, restore
2232 -- visibility only if they are actually in context.
2234 E := First_Entity (Current_Scope);
2235 while Present (E) loop
2236 if not Is_Child_Unit (E) or else Is_Visible_Lib_Unit (E) then
2237 Set_Is_Immediately_Visible (E);
2238 end if;
2240 Next_Entity (E);
2241 end loop;
2243 -- A subunit appears within a body, and for a nested subunits all the
2244 -- parents are bodies. Restore full visibility of their private
2245 -- entities.
2247 if Is_Package_Or_Generic_Package (Scop) then
2248 Set_In_Package_Body (Scop);
2249 Install_Private_Declarations (Scop);
2250 end if;
2251 end Re_Install_Parents;
2253 ----------------------------
2254 -- Re_Install_Use_Clauses --
2255 ----------------------------
2257 procedure Re_Install_Use_Clauses is
2258 U : Node_Id;
2259 begin
2260 for J in reverse 1 .. Num_Scopes loop
2261 U := Use_Clauses (J);
2262 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2263 Install_Use_Clauses (U, Force_Installation => True);
2264 end loop;
2265 end Re_Install_Use_Clauses;
2267 ------------------
2268 -- Remove_Scope --
2269 ------------------
2271 procedure Remove_Scope is
2272 E : Entity_Id;
2274 begin
2275 Num_Scopes := Num_Scopes + 1;
2276 Use_Clauses (Num_Scopes) :=
2277 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2279 E := First_Entity (Current_Scope);
2280 while Present (E) loop
2281 Set_Is_Immediately_Visible (E, False);
2282 Next_Entity (E);
2283 end loop;
2285 if Is_Child_Unit (Current_Scope) then
2286 Enclosing_Child := Current_Scope;
2287 end if;
2289 Pop_Scope;
2290 end Remove_Scope;
2292 -- Start of processing for Analyze_Subunit
2294 begin
2295 -- For subunit in main extended unit, we reset the configuration values
2296 -- for the non-partition-wide restrictions. For other units reset them.
2298 if In_Extended_Main_Source_Unit (N) then
2299 Restore_Config_Cunit_Boolean_Restrictions;
2300 else
2301 Reset_Cunit_Boolean_Restrictions;
2302 end if;
2304 if Style_Check then
2305 declare
2306 Nam : Node_Id := Name (Unit (N));
2308 begin
2309 if Nkind (Nam) = N_Selected_Component then
2310 Nam := Selector_Name (Nam);
2311 end if;
2313 Check_Identifier (Nam, Par_Unit);
2314 end;
2315 end if;
2317 if not Is_Empty_List (Context_Items (N)) then
2319 -- Save current use clauses
2321 Remove_Scope;
2322 Remove_Context (Lib_Unit);
2324 -- Now remove parents and their context, including enclosing subunits
2325 -- and the outer parent body which is not a subunit.
2327 if Present (Lib_Spec) then
2328 Remove_Context (Lib_Spec);
2330 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2331 Lib_Spec := Library_Unit (Lib_Spec);
2332 Remove_Scope;
2333 Remove_Context (Lib_Spec);
2334 end loop;
2336 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2337 Remove_Scope;
2338 end if;
2340 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2341 Remove_Context (Library_Unit (Lib_Spec));
2342 end if;
2343 end if;
2345 Set_Is_Immediately_Visible (Par_Unit, False);
2347 Analyze_Subunit_Context;
2349 Re_Install_Parents (Lib_Unit, Par_Unit);
2350 Set_Is_Immediately_Visible (Par_Unit);
2352 -- If the context includes a child unit of the parent of the subunit,
2353 -- the parent will have been removed from visibility, after compiling
2354 -- that cousin in the context. The visibility of the parent must be
2355 -- restored now. This also applies if the context includes another
2356 -- subunit of the same parent which in turn includes a child unit in
2357 -- its context.
2359 if Is_Package_Or_Generic_Package (Par_Unit) then
2360 if not Is_Immediately_Visible (Par_Unit)
2361 or else (Present (First_Entity (Par_Unit))
2362 and then not
2363 Is_Immediately_Visible (First_Entity (Par_Unit)))
2364 then
2365 Set_Is_Immediately_Visible (Par_Unit);
2366 Install_Visible_Declarations (Par_Unit);
2367 Install_Private_Declarations (Par_Unit);
2368 end if;
2369 end if;
2371 Re_Install_Use_Clauses;
2372 Install_Context (N);
2374 -- Restore state of suppress flags for current body
2376 Scope_Suppress := Svg;
2378 -- If the subunit is within a child unit, then siblings of any parent
2379 -- unit that appear in the context clause of the subunit must also be
2380 -- made immediately visible.
2382 if Present (Enclosing_Child) then
2383 Install_Siblings (Enclosing_Child, N);
2384 end if;
2385 end if;
2387 Generate_Parent_References (Unit (N), Par_Unit);
2388 Analyze (Proper_Body (Unit (N)));
2389 Remove_Context (N);
2391 -- The subunit may contain a with_clause on a sibling of some ancestor.
2392 -- Removing the context will remove from visibility those ancestor child
2393 -- units, which must be restored to the visibility they have in the
2394 -- enclosing body.
2396 if Present (Enclosing_Child) then
2397 declare
2398 C : Entity_Id;
2399 begin
2400 C := Current_Scope;
2401 while Present (C) and then C /= Standard_Standard loop
2402 Set_Is_Immediately_Visible (C);
2403 Set_Is_Visible_Lib_Unit (C);
2404 C := Scope (C);
2405 end loop;
2406 end;
2407 end if;
2409 -- Deal with restore of restrictions
2411 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2412 end Analyze_Subunit;
2414 ----------------------------
2415 -- Analyze_Task_Body_Stub --
2416 ----------------------------
2418 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2419 Loc : constant Source_Ptr := Sloc (N);
2420 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2422 begin
2423 Check_Stub_Level (N);
2425 -- First occurrence of name may have been as an incomplete type
2427 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2428 Nam := Full_View (Nam);
2429 end if;
2431 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2432 Error_Msg_N ("missing specification for task body", N);
2434 else
2435 -- Currently there are no language-defined aspects that can apply to
2436 -- a task body stub. Issue an error and remove the aspects to prevent
2437 -- cascaded errors.
2439 if Has_Aspects (N) then
2440 Error_Msg_N
2441 ("aspects on task bodies are not allowed",
2442 First (Aspect_Specifications (N)));
2443 Remove_Aspects (N);
2444 end if;
2446 Set_Scope (Defining_Entity (N), Current_Scope);
2447 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2448 Set_Corresponding_Spec_Of_Stub (N, Nam);
2450 -- Check for duplicate stub, if so give message and terminate
2452 if Has_Completion (Etype (Nam)) then
2453 Error_Msg_N ("duplicate stub for task", N);
2454 return;
2455 else
2456 Set_Has_Completion (Etype (Nam));
2457 end if;
2459 Analyze_Proper_Body (N, Etype (Nam));
2461 -- Set elaboration flag to indicate that entity is callable. This
2462 -- cannot be done in the expansion of the body itself, because the
2463 -- proper body is not in a declarative part. This is only done if
2464 -- expansion is active, because the context may be generic and the
2465 -- flag not defined yet.
2467 if Expander_Active then
2468 Insert_After (N,
2469 Make_Assignment_Statement (Loc,
2470 Name =>
2471 Make_Identifier (Loc,
2472 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2473 Expression => New_Occurrence_Of (Standard_True, Loc)));
2474 end if;
2475 end if;
2476 end Analyze_Task_Body_Stub;
2478 -------------------------
2479 -- Analyze_With_Clause --
2480 -------------------------
2482 -- Analyze the declaration of a unit in a with clause. At end, label the
2483 -- with clause with the defining entity for the unit.
2485 procedure Analyze_With_Clause (N : Node_Id) is
2487 -- Retrieve the original kind of the unit node, before analysis. If it
2488 -- is a subprogram instantiation, its analysis below will rewrite the
2489 -- node as the declaration of the wrapper package. If the same
2490 -- instantiation appears indirectly elsewhere in the context, it will
2491 -- have been analyzed already.
2493 Unit_Kind : constant Node_Kind :=
2494 Nkind (Original_Node (Unit (Library_Unit (N))));
2495 Nam : constant Node_Id := Name (N);
2496 E_Name : Entity_Id;
2497 Par_Name : Entity_Id;
2498 Pref : Node_Id;
2499 U : Node_Id;
2501 Intunit : Boolean;
2502 -- Set True if the unit currently being compiled is an internal unit
2504 Restriction_Violation : Boolean := False;
2505 -- Set True if a with violates a restriction, no point in giving any
2506 -- warnings if we have this definite error.
2508 Save_Style_Check : constant Boolean := Opt.Style_Check;
2510 begin
2511 U := Unit (Library_Unit (N));
2513 -- If this is an internal unit which is a renaming, then this is a
2514 -- violation of No_Obsolescent_Features.
2516 -- Note: this is not quite right if the user defines one of these units
2517 -- himself, but that's a marginal case, and fixing it is hard ???
2519 if Restriction_Check_Required (No_Obsolescent_Features) then
2520 declare
2521 F : constant File_Name_Type :=
2522 Unit_File_Name (Get_Source_Unit (U));
2523 begin
2524 if Is_Predefined_File_Name (F, Renamings_Included => True)
2525 and then not
2526 Is_Predefined_File_Name (F, Renamings_Included => False)
2527 then
2528 Check_Restriction (No_Obsolescent_Features, N);
2529 Restriction_Violation := True;
2530 end if;
2531 end;
2532 end if;
2534 -- Check No_Implementation_Units violation
2536 if Restriction_Check_Required (No_Implementation_Units) then
2537 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2538 null;
2539 else
2540 Check_Restriction (No_Implementation_Units, Nam);
2541 Restriction_Violation := True;
2542 end if;
2543 end if;
2545 -- Several actions are skipped for dummy packages (those supplied for
2546 -- with's where no matching file could be found). Such packages are
2547 -- identified by the Sloc value being set to No_Location.
2549 if Limited_Present (N) then
2551 -- Ada 2005 (AI-50217): Build visibility structures but do not
2552 -- analyze the unit.
2554 -- If the designated unit is a predefined unit, which might be used
2555 -- implicitly through the rtsfind machinery, a limited with clause
2556 -- on such a unit is usually pointless, because run-time units are
2557 -- unlikely to appear in mutually dependent units, and because this
2558 -- disables the rtsfind mechanism. We transform such limited with
2559 -- clauses into regular with clauses.
2561 if Sloc (U) /= No_Location then
2562 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (U)))
2564 -- In ASIS mode the rtsfind mechanism plays no role, and
2565 -- we need to maintain the original tree structure, so
2566 -- this transformation is not performed in this case.
2568 and then not ASIS_Mode
2569 then
2570 Set_Limited_Present (N, False);
2571 Analyze_With_Clause (N);
2572 else
2573 Build_Limited_Views (N);
2574 end if;
2575 end if;
2577 return;
2578 end if;
2580 -- If we are compiling under "don't quit" mode (-gnatq) and we have
2581 -- already detected serious errors then we mark the with-clause nodes as
2582 -- analyzed before the corresponding compilation unit is analyzed. This
2583 -- is done here to protect the frontend against never ending recursion
2584 -- caused by circularities in the sources (because the previous errors
2585 -- may break the regular machine of the compiler implemented in
2586 -- Load_Unit to detect circularities).
2588 if Serious_Errors_Detected > 0 and then Try_Semantics then
2589 Set_Analyzed (N);
2590 end if;
2592 -- If the library unit is a predefined unit, and we are in high
2593 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2594 -- for the analysis of the with'ed unit. This mode does not prevent
2595 -- explicit with'ing of run-time units.
2597 if Configurable_Run_Time_Mode
2598 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (U)))
2599 then
2600 Configurable_Run_Time_Mode := False;
2601 Semantics (Library_Unit (N));
2602 Configurable_Run_Time_Mode := True;
2604 else
2605 Semantics (Library_Unit (N));
2606 end if;
2608 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2610 if Sloc (U) /= No_Location then
2612 -- Check restrictions, except that we skip the check if this is an
2613 -- internal unit unless we are compiling the internal unit as the
2614 -- main unit. We also skip this for dummy packages.
2616 Check_Restriction_No_Dependence (Nam, N);
2618 if not Intunit or else Current_Sem_Unit = Main_Unit then
2619 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2620 end if;
2622 -- Deal with special case of GNAT.Current_Exceptions which interacts
2623 -- with the optimization of local raise statements into gotos.
2625 if Nkind (Nam) = N_Selected_Component
2626 and then Nkind (Prefix (Nam)) = N_Identifier
2627 and then Chars (Prefix (Nam)) = Name_Gnat
2628 and then Nam_In (Chars (Selector_Name (Nam)),
2629 Name_Most_Recent_Exception,
2630 Name_Exception_Traces)
2631 then
2632 Check_Restriction (No_Exception_Propagation, N);
2633 Special_Exception_Package_Used := True;
2634 end if;
2636 -- Check for inappropriate with of internal implementation unit if we
2637 -- are not compiling an internal unit and also check for withing unit
2638 -- in wrong version of Ada. Do not issue these messages for implicit
2639 -- with's generated by the compiler itself.
2641 if Implementation_Unit_Warnings
2642 and then not Intunit
2643 and then not Implicit_With (N)
2644 and then not Restriction_Violation
2645 then
2646 declare
2647 U_Kind : constant Kind_Of_Unit :=
2648 Get_Kind_Of_Unit (Get_Source_Unit (U));
2650 begin
2651 if U_Kind = Implementation_Unit then
2652 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2654 -- Add alternative name if available, otherwise issue a
2655 -- general warning message.
2657 if Error_Msg_Strlen /= 0 then
2658 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2659 else
2660 Error_Msg_F
2661 ("\use of this unit is non-portable " &
2662 "and version-dependent?i?", Name (N));
2663 end if;
2665 elsif U_Kind = Ada_2005_Unit
2666 and then Ada_Version < Ada_2005
2667 and then Warn_On_Ada_2005_Compatibility
2668 then
2669 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2671 elsif U_Kind = Ada_2012_Unit
2672 and then Ada_Version < Ada_2012
2673 and then Warn_On_Ada_2012_Compatibility
2674 then
2675 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2676 end if;
2677 end;
2678 end if;
2679 end if;
2681 -- Semantic analysis of a generic unit is performed on a copy of
2682 -- the original tree. Retrieve the entity on which semantic info
2683 -- actually appears.
2685 if Unit_Kind in N_Generic_Declaration then
2686 E_Name := Defining_Entity (U);
2688 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2689 -- the case of an instantiation, semantic analysis above will have
2690 -- replaced the unit by its instantiated version. If the instance body
2691 -- has been generated, the instance now denotes the body entity. For
2692 -- visibility purposes we need the entity of its spec.
2694 elsif (Unit_Kind = N_Package_Instantiation
2695 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2696 N_Package_Instantiation)
2697 and then Nkind (U) = N_Package_Body
2698 then
2699 E_Name := Corresponding_Spec (U);
2701 elsif Unit_Kind = N_Package_Instantiation
2702 and then Nkind (U) = N_Package_Instantiation
2703 and then Present (Instance_Spec (U))
2704 then
2705 -- If the instance has not been rewritten as a package declaration,
2706 -- then it appeared already in a previous with clause. Retrieve
2707 -- the entity from the previous instance.
2709 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2711 elsif Unit_Kind in N_Subprogram_Instantiation then
2713 -- The visible subprogram is created during instantiation, and is
2714 -- an attribute of the wrapper package. We retrieve the wrapper
2715 -- package directly from the instantiation node. If the instance
2716 -- is inlined the unit is still an instantiation. Otherwise it has
2717 -- been rewritten as the declaration of the wrapper itself.
2719 if Nkind (U) in N_Subprogram_Instantiation then
2720 E_Name :=
2721 Related_Instance
2722 (Defining_Entity (Specification (Instance_Spec (U))));
2723 else
2724 E_Name := Related_Instance (Defining_Entity (U));
2725 end if;
2727 elsif Unit_Kind = N_Package_Renaming_Declaration
2728 or else Unit_Kind in N_Generic_Renaming_Declaration
2729 then
2730 E_Name := Defining_Entity (U);
2732 elsif Unit_Kind = N_Subprogram_Body
2733 and then Nkind (Name (N)) = N_Selected_Component
2734 and then not Acts_As_Spec (Library_Unit (N))
2735 then
2736 -- For a child unit that has no spec, one has been created and
2737 -- analyzed. The entity required is that of the spec.
2739 E_Name := Corresponding_Spec (U);
2741 else
2742 E_Name := Defining_Entity (U);
2743 end if;
2745 if Nkind (Name (N)) = N_Selected_Component then
2747 -- Child unit in a with clause
2749 Change_Selected_Component_To_Expanded_Name (Name (N));
2751 -- If this is a child unit without a spec, and it has been analyzed
2752 -- already, a declaration has been created for it. The with_clause
2753 -- must reflect the actual body, and not the generated declaration,
2754 -- to prevent spurious binding errors involving an out-of-date spec.
2755 -- Note that this can only happen if the unit includes more than one
2756 -- with_clause for the child unit (e.g. in separate subunits).
2758 if Unit_Kind = N_Subprogram_Declaration
2759 and then Analyzed (Library_Unit (N))
2760 and then not Comes_From_Source (Library_Unit (N))
2761 then
2762 Set_Library_Unit (N,
2763 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2764 end if;
2765 end if;
2767 -- Restore style checks
2769 Style_Check := Save_Style_Check;
2771 -- Record the reference, but do NOT set the unit as referenced, we want
2772 -- to consider the unit as unreferenced if this is the only reference
2773 -- that occurs.
2775 Set_Entity_With_Checks (Name (N), E_Name);
2776 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2778 -- Generate references and check No_Dependence restriction for parents
2780 if Is_Child_Unit (E_Name) then
2781 Pref := Prefix (Name (N));
2782 Par_Name := Scope (E_Name);
2783 while Nkind (Pref) = N_Selected_Component loop
2784 Change_Selected_Component_To_Expanded_Name (Pref);
2786 if Present (Entity (Selector_Name (Pref)))
2787 and then
2788 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2789 and then Entity (Selector_Name (Pref)) /= Par_Name
2790 then
2791 -- The prefix is a child unit that denotes a renaming declaration.
2792 -- Replace the prefix directly with the renamed unit, because the
2793 -- rest of the prefix is irrelevant to the visibility of the real
2794 -- unit.
2796 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2797 exit;
2798 end if;
2800 Set_Entity_With_Checks (Pref, Par_Name);
2802 Generate_Reference (Par_Name, Pref);
2803 Check_Restriction_No_Dependence (Pref, N);
2804 Pref := Prefix (Pref);
2806 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2807 -- is set to Standard_Standard, and no attempt should be made to
2808 -- further unwind scopes.
2810 if Par_Name /= Standard_Standard then
2811 Par_Name := Scope (Par_Name);
2812 end if;
2814 -- Abandon processing in case of previous errors
2816 if No (Par_Name) then
2817 Check_Error_Detected;
2818 return;
2819 end if;
2820 end loop;
2822 if Present (Entity (Pref))
2823 and then not Analyzed (Parent (Parent (Entity (Pref))))
2824 then
2825 -- If the entity is set without its unit being compiled, the
2826 -- original parent is a renaming, and Par_Name is the renamed
2827 -- entity. For visibility purposes, we need the original entity,
2828 -- which must be analyzed now because Load_Unit directly retrieves
2829 -- the renamed unit, and the renaming declaration itself has not
2830 -- been analyzed.
2832 Analyze (Parent (Parent (Entity (Pref))));
2833 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2834 Par_Name := Entity (Pref);
2835 end if;
2837 -- Guard against missing or misspelled child units
2839 if Present (Par_Name) then
2840 Set_Entity_With_Checks (Pref, Par_Name);
2841 Generate_Reference (Par_Name, Pref);
2843 else
2844 pragma Assert (Serious_Errors_Detected /= 0);
2846 -- Mark the node to indicate that a related error has been posted.
2847 -- This defends further compilation passes against improper use of
2848 -- the invalid WITH clause node.
2850 Set_Error_Posted (N);
2851 Set_Name (N, Error);
2852 return;
2853 end if;
2854 end if;
2856 -- If the withed unit is System, and a system extension pragma is
2857 -- present, compile the extension now, rather than waiting for a
2858 -- visibility check on a specific entity.
2860 if Chars (E_Name) = Name_System
2861 and then Scope (E_Name) = Standard_Standard
2862 and then Present (System_Extend_Unit)
2863 and then Present_System_Aux (N)
2864 then
2865 -- If the extension is not present, an error will have been emitted
2867 null;
2868 end if;
2870 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2871 -- to private_with units; they will be made visible later (just before
2872 -- the private part is analyzed)
2874 if Private_Present (N) then
2875 Set_Is_Immediately_Visible (E_Name, False);
2876 end if;
2878 -- Propagate Fatal_Error setting from with'ed unit to current unit
2880 case Fatal_Error (Get_Source_Unit (Library_Unit (N))) is
2882 -- Nothing to do if with'ed unit had no error
2884 when None =>
2885 null;
2887 -- If with'ed unit had a detected fatal error, propagate it
2889 when Error_Detected =>
2890 Set_Fatal_Error (Current_Sem_Unit, Error_Detected);
2892 -- If with'ed unit had an ignored error, then propagate it but do not
2893 -- overide an existring setting.
2895 when Error_Ignored =>
2896 if Fatal_Error (Current_Sem_Unit) = None then
2897 Set_Fatal_Error (Current_Sem_Unit, Error_Ignored);
2898 end if;
2899 end case;
2900 end Analyze_With_Clause;
2902 ------------------------------
2903 -- Check_Private_Child_Unit --
2904 ------------------------------
2906 procedure Check_Private_Child_Unit (N : Node_Id) is
2907 Lib_Unit : constant Node_Id := Unit (N);
2908 Item : Node_Id;
2909 Curr_Unit : Entity_Id;
2910 Sub_Parent : Node_Id;
2911 Priv_Child : Entity_Id;
2912 Par_Lib : Entity_Id;
2913 Par_Spec : Node_Id;
2915 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2916 -- Returns true if and only if the library unit is declared with
2917 -- an explicit designation of private.
2919 -----------------------------
2920 -- Is_Private_Library_Unit --
2921 -----------------------------
2923 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2924 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2926 begin
2927 return Private_Present (Comp_Unit);
2928 end Is_Private_Library_Unit;
2930 -- Start of processing for Check_Private_Child_Unit
2932 begin
2933 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2934 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2935 Par_Lib := Curr_Unit;
2937 elsif Nkind (Lib_Unit) = N_Subunit then
2939 -- The parent is itself a body. The parent entity is to be found in
2940 -- the corresponding spec.
2942 Sub_Parent := Library_Unit (N);
2943 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2945 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2946 -- enclosing body, retrieve the spec entity which is the proper
2947 -- ancestor we need for the following tests.
2949 if Ekind (Curr_Unit) = E_Package_Body then
2950 Curr_Unit := Spec_Entity (Curr_Unit);
2951 end if;
2953 Par_Lib := Curr_Unit;
2955 else
2956 Curr_Unit := Defining_Entity (Lib_Unit);
2958 Par_Lib := Curr_Unit;
2959 Par_Spec := Parent_Spec (Lib_Unit);
2961 if No (Par_Spec) then
2962 Par_Lib := Empty;
2963 else
2964 Par_Lib := Defining_Entity (Unit (Par_Spec));
2965 end if;
2966 end if;
2968 -- Loop through context items
2970 Item := First (Context_Items (N));
2971 while Present (Item) loop
2973 -- Ada 2005 (AI-262): Allow private_with of a private child package
2974 -- in public siblings
2976 if Nkind (Item) = N_With_Clause
2977 and then not Implicit_With (Item)
2978 and then not Limited_Present (Item)
2979 and then Is_Private_Descendant (Entity (Name (Item)))
2980 then
2981 Priv_Child := Entity (Name (Item));
2983 declare
2984 Curr_Parent : Entity_Id := Par_Lib;
2985 Child_Parent : Entity_Id := Scope (Priv_Child);
2986 Prv_Ancestor : Entity_Id := Child_Parent;
2987 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2989 begin
2990 -- If the child unit is a public child then locate the nearest
2991 -- private ancestor. Child_Parent will then be set to the
2992 -- parent of that ancestor.
2994 if not Is_Private_Library_Unit (Priv_Child) then
2995 while Present (Prv_Ancestor)
2996 and then not Is_Private_Library_Unit (Prv_Ancestor)
2997 loop
2998 Prv_Ancestor := Scope (Prv_Ancestor);
2999 end loop;
3001 if Present (Prv_Ancestor) then
3002 Child_Parent := Scope (Prv_Ancestor);
3003 end if;
3004 end if;
3006 while Present (Curr_Parent)
3007 and then Curr_Parent /= Standard_Standard
3008 and then Curr_Parent /= Child_Parent
3009 loop
3010 Curr_Private :=
3011 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
3012 Curr_Parent := Scope (Curr_Parent);
3013 end loop;
3015 if No (Curr_Parent) then
3016 Curr_Parent := Standard_Standard;
3017 end if;
3019 if Curr_Parent /= Child_Parent then
3020 if Ekind (Priv_Child) = E_Generic_Package
3021 and then Chars (Priv_Child) in Text_IO_Package_Name
3022 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
3023 then
3024 Error_Msg_NE
3025 ("& is a nested package, not a compilation unit",
3026 Name (Item), Priv_Child);
3028 else
3029 Error_Msg_N
3030 ("unit in with clause is private child unit!", Item);
3031 Error_Msg_NE
3032 ("\current unit must also have parent&!",
3033 Item, Child_Parent);
3034 end if;
3036 elsif Curr_Private
3037 or else Private_Present (Item)
3038 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
3039 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3040 and then not Acts_As_Spec (Parent (Lib_Unit)))
3041 then
3042 null;
3044 else
3045 Error_Msg_NE
3046 ("current unit must also be private descendant of&",
3047 Item, Child_Parent);
3048 end if;
3049 end;
3050 end if;
3052 Next (Item);
3053 end loop;
3055 end Check_Private_Child_Unit;
3057 ----------------------
3058 -- Check_Stub_Level --
3059 ----------------------
3061 procedure Check_Stub_Level (N : Node_Id) is
3062 Par : constant Node_Id := Parent (N);
3063 Kind : constant Node_Kind := Nkind (Par);
3065 begin
3066 if Nkind_In (Kind, N_Package_Body,
3067 N_Subprogram_Body,
3068 N_Task_Body,
3069 N_Protected_Body)
3070 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
3071 then
3072 null;
3074 -- In an instance, a missing stub appears at any level. A warning
3075 -- message will have been emitted already for the missing file.
3077 elsif not In_Instance then
3078 Error_Msg_N ("stub cannot appear in an inner scope", N);
3080 elsif Expander_Active then
3081 Error_Msg_N ("missing proper body", N);
3082 end if;
3083 end Check_Stub_Level;
3085 ------------------------
3086 -- Expand_With_Clause --
3087 ------------------------
3089 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
3090 Loc : constant Source_Ptr := Sloc (Nam);
3091 Ent : constant Entity_Id := Entity (Nam);
3092 Withn : Node_Id;
3093 P : Node_Id;
3095 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
3096 -- Build name to be used in implicit with_clause. In most cases this
3097 -- is the source name, but if renamings are present we must make the
3098 -- original unit visible, not the one it renames. The entity in the
3099 -- with clause is the renamed unit, but the identifier is the one from
3100 -- the source, which allows us to recover the unit renaming.
3102 ---------------------
3103 -- Build_Unit_Name --
3104 ---------------------
3106 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
3107 Ent : Entity_Id;
3108 Result : Node_Id;
3110 begin
3111 if Nkind (Nam) = N_Identifier then
3112 return New_Occurrence_Of (Entity (Nam), Loc);
3114 else
3115 Ent := Entity (Nam);
3117 if Present (Entity (Selector_Name (Nam)))
3118 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
3119 and then
3120 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
3121 = N_Package_Renaming_Declaration
3122 then
3123 -- The name in the with_clause is of the form A.B.C, and B is
3124 -- given by a renaming declaration. In that case we may not
3125 -- have analyzed the unit for B, but replaced it directly in
3126 -- lib-load with the unit it renames. We have to make A.B
3127 -- visible, so analyze the declaration for B now, in case it
3128 -- has not been done yet.
3130 Ent := Entity (Selector_Name (Nam));
3131 Analyze
3132 (Parent
3133 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3134 end if;
3136 Result :=
3137 Make_Expanded_Name (Loc,
3138 Chars => Chars (Entity (Nam)),
3139 Prefix => Build_Unit_Name (Prefix (Nam)),
3140 Selector_Name => New_Occurrence_Of (Ent, Loc));
3141 Set_Entity (Result, Ent);
3142 return Result;
3143 end if;
3144 end Build_Unit_Name;
3146 -- Start of processing for Expand_With_Clause
3148 begin
3149 Withn :=
3150 Make_With_Clause (Loc,
3151 Name => Build_Unit_Name (Nam));
3153 P := Parent (Unit_Declaration_Node (Ent));
3154 Set_Library_Unit (Withn, P);
3155 Set_Corresponding_Spec (Withn, Ent);
3156 Set_First_Name (Withn, True);
3157 Set_Implicit_With (Withn, True);
3159 -- If the unit is a package or generic package declaration, a private_
3160 -- with_clause on a child unit implies that the implicit with on the
3161 -- parent is also private.
3163 if Nkind_In (Unit (N), N_Package_Declaration,
3164 N_Generic_Package_Declaration)
3165 then
3166 Set_Private_Present (Withn, Private_Present (Item));
3167 end if;
3169 Prepend (Withn, Context_Items (N));
3170 Mark_Rewrite_Insertion (Withn);
3171 Install_Withed_Unit (Withn);
3173 -- If we have "with X.Y;", we want to recurse on "X", except in the
3174 -- unusual case where X.Y is a renaming of X. In that case, the scope
3175 -- of X will be null.
3177 if Nkind (Nam) = N_Expanded_Name
3178 and then Present (Scope (Entity (Prefix (Nam))))
3179 then
3180 Expand_With_Clause (Item, Prefix (Nam), N);
3181 end if;
3182 end Expand_With_Clause;
3184 --------------------------------
3185 -- Generate_Parent_References --
3186 --------------------------------
3188 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
3189 Pref : Node_Id;
3190 P_Name : Entity_Id := P_Id;
3192 begin
3193 if Nkind (N) = N_Subunit then
3194 Pref := Name (N);
3195 else
3196 Pref := Name (Parent (Defining_Entity (N)));
3197 end if;
3199 if Nkind (Pref) = N_Expanded_Name then
3201 -- Done already, if the unit has been compiled indirectly as
3202 -- part of the closure of its context because of inlining.
3204 return;
3205 end if;
3207 while Nkind (Pref) = N_Selected_Component loop
3208 Change_Selected_Component_To_Expanded_Name (Pref);
3209 Set_Entity (Pref, P_Name);
3210 Set_Etype (Pref, Etype (P_Name));
3211 Generate_Reference (P_Name, Pref, 'r');
3212 Pref := Prefix (Pref);
3213 P_Name := Scope (P_Name);
3214 end loop;
3216 -- The guard here on P_Name is to handle the error condition where
3217 -- the parent unit is missing because the file was not found.
3219 if Present (P_Name) then
3220 Set_Entity (Pref, P_Name);
3221 Set_Etype (Pref, Etype (P_Name));
3222 Generate_Reference (P_Name, Pref, 'r');
3223 Style.Check_Identifier (Pref, P_Name);
3224 end if;
3225 end Generate_Parent_References;
3227 ---------------------
3228 -- Has_With_Clause --
3229 ---------------------
3231 function Has_With_Clause
3232 (C_Unit : Node_Id;
3233 Pack : Entity_Id;
3234 Is_Limited : Boolean := False) return Boolean
3236 Item : Node_Id;
3238 function Named_Unit (Clause : Node_Id) return Entity_Id;
3239 -- Return the entity for the unit named in a [limited] with clause
3241 ----------------
3242 -- Named_Unit --
3243 ----------------
3245 function Named_Unit (Clause : Node_Id) return Entity_Id is
3246 begin
3247 if Nkind (Name (Clause)) = N_Selected_Component then
3248 return Entity (Selector_Name (Name (Clause)));
3249 else
3250 return Entity (Name (Clause));
3251 end if;
3252 end Named_Unit;
3254 -- Start of processing for Has_With_Clause
3256 begin
3257 if Present (Context_Items (C_Unit)) then
3258 Item := First (Context_Items (C_Unit));
3259 while Present (Item) loop
3260 if Nkind (Item) = N_With_Clause
3261 and then Limited_Present (Item) = Is_Limited
3262 and then Named_Unit (Item) = Pack
3263 then
3264 return True;
3265 end if;
3267 Next (Item);
3268 end loop;
3269 end if;
3271 return False;
3272 end Has_With_Clause;
3274 -----------------------------
3275 -- Implicit_With_On_Parent --
3276 -----------------------------
3278 procedure Implicit_With_On_Parent
3279 (Child_Unit : Node_Id;
3280 N : Node_Id)
3282 Loc : constant Source_Ptr := Sloc (N);
3283 P : constant Node_Id := Parent_Spec (Child_Unit);
3284 P_Unit : Node_Id := Unit (P);
3285 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3286 Withn : Node_Id;
3288 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3289 -- Build prefix of child unit name. Recurse if needed
3291 function Build_Unit_Name return Node_Id;
3292 -- If the unit is a child unit, build qualified name with all ancestors
3294 -------------------------
3295 -- Build_Ancestor_Name --
3296 -------------------------
3298 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3299 P_Ref : constant Node_Id :=
3300 New_Occurrence_Of (Defining_Entity (P), Loc);
3301 P_Spec : Node_Id := P;
3303 begin
3304 -- Ancestor may have been rewritten as a package body. Retrieve
3305 -- the original spec to trace earlier ancestors.
3307 if Nkind (P) = N_Package_Body
3308 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3309 then
3310 P_Spec := Original_Node (P);
3311 end if;
3313 if No (Parent_Spec (P_Spec)) then
3314 return P_Ref;
3315 else
3316 return
3317 Make_Selected_Component (Loc,
3318 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3319 Selector_Name => P_Ref);
3320 end if;
3321 end Build_Ancestor_Name;
3323 ---------------------
3324 -- Build_Unit_Name --
3325 ---------------------
3327 function Build_Unit_Name return Node_Id is
3328 Result : Node_Id;
3330 begin
3331 if No (Parent_Spec (P_Unit)) then
3332 return New_Occurrence_Of (P_Name, Loc);
3334 else
3335 Result :=
3336 Make_Expanded_Name (Loc,
3337 Chars => Chars (P_Name),
3338 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3339 Selector_Name => New_Occurrence_Of (P_Name, Loc));
3340 Set_Entity (Result, P_Name);
3341 return Result;
3342 end if;
3343 end Build_Unit_Name;
3345 -- Start of processing for Implicit_With_On_Parent
3347 begin
3348 -- The unit of the current compilation may be a package body that
3349 -- replaces an instance node. In this case we need the original instance
3350 -- node to construct the proper parent name.
3352 if Nkind (P_Unit) = N_Package_Body
3353 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3354 then
3355 P_Unit := Original_Node (P_Unit);
3356 end if;
3358 -- We add the implicit with if the child unit is the current unit being
3359 -- compiled. If the current unit is a body, we do not want to add an
3360 -- implicit_with a second time to the corresponding spec.
3362 if Nkind (Child_Unit) = N_Package_Declaration
3363 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3364 then
3365 return;
3366 end if;
3368 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3370 Set_Library_Unit (Withn, P);
3371 Set_Corresponding_Spec (Withn, P_Name);
3372 Set_First_Name (Withn, True);
3373 Set_Implicit_With (Withn, True);
3375 -- Node is placed at the beginning of the context items, so that
3376 -- subsequent use clauses on the parent can be validated.
3378 Prepend (Withn, Context_Items (N));
3379 Mark_Rewrite_Insertion (Withn);
3380 Install_Withed_Unit (Withn);
3382 if Is_Child_Spec (P_Unit) then
3383 Implicit_With_On_Parent (P_Unit, N);
3384 end if;
3385 end Implicit_With_On_Parent;
3387 --------------
3388 -- In_Chain --
3389 --------------
3391 function In_Chain (E : Entity_Id) return Boolean is
3392 H : Entity_Id;
3394 begin
3395 H := Current_Entity (E);
3396 while Present (H) loop
3397 if H = E then
3398 return True;
3399 else
3400 H := Homonym (H);
3401 end if;
3402 end loop;
3404 return False;
3405 end In_Chain;
3407 ---------------------
3408 -- Install_Context --
3409 ---------------------
3411 procedure Install_Context (N : Node_Id) is
3412 Lib_Unit : constant Node_Id := Unit (N);
3414 begin
3415 Install_Context_Clauses (N);
3417 if Is_Child_Spec (Lib_Unit) then
3418 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3419 end if;
3421 Install_Limited_Context_Clauses (N);
3422 end Install_Context;
3424 -----------------------------
3425 -- Install_Context_Clauses --
3426 -----------------------------
3428 procedure Install_Context_Clauses (N : Node_Id) is
3429 Lib_Unit : constant Node_Id := Unit (N);
3430 Item : Node_Id;
3431 Uname_Node : Entity_Id;
3432 Check_Private : Boolean := False;
3433 Decl_Node : Node_Id;
3434 Lib_Parent : Entity_Id;
3436 begin
3437 -- First skip configuration pragmas at the start of the context. They
3438 -- are not technically part of the context clause, but that's where the
3439 -- parser puts them. Note they were analyzed in Analyze_Context.
3441 Item := First (Context_Items (N));
3442 while Present (Item)
3443 and then Nkind (Item) = N_Pragma
3444 and then Pragma_Name (Item) in Configuration_Pragma_Names
3445 loop
3446 Next (Item);
3447 end loop;
3449 -- Loop through the actual context clause items. We process everything
3450 -- except Limited_With clauses in this routine. Limited_With clauses
3451 -- are separately installed (see Install_Limited_Context_Clauses).
3453 while Present (Item) loop
3455 -- Case of explicit WITH clause
3457 if Nkind (Item) = N_With_Clause
3458 and then not Implicit_With (Item)
3459 then
3460 if Limited_Present (Item) then
3462 -- Limited withed units will be installed later
3464 goto Continue;
3466 -- If Name (Item) is not an entity name, something is wrong, and
3467 -- this will be detected in due course, for now ignore the item
3469 elsif not Is_Entity_Name (Name (Item)) then
3470 goto Continue;
3472 elsif No (Entity (Name (Item))) then
3473 Set_Entity (Name (Item), Any_Id);
3474 goto Continue;
3475 end if;
3477 Uname_Node := Entity (Name (Item));
3479 if Is_Private_Descendant (Uname_Node) then
3480 Check_Private := True;
3481 end if;
3483 Install_Withed_Unit (Item);
3485 Decl_Node := Unit_Declaration_Node (Uname_Node);
3487 -- If the unit is a subprogram instance, it appears nested within
3488 -- a package that carries the parent information.
3490 if Is_Generic_Instance (Uname_Node)
3491 and then Ekind (Uname_Node) /= E_Package
3492 then
3493 Decl_Node := Parent (Parent (Decl_Node));
3494 end if;
3496 if Is_Child_Spec (Decl_Node) then
3497 if Nkind (Name (Item)) = N_Expanded_Name then
3498 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3499 else
3500 -- If not an expanded name, the child unit must be a
3501 -- renaming, nothing to do.
3503 null;
3504 end if;
3506 elsif Nkind (Decl_Node) = N_Subprogram_Body
3507 and then not Acts_As_Spec (Parent (Decl_Node))
3508 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3509 then
3510 Implicit_With_On_Parent
3511 (Unit (Library_Unit (Parent (Decl_Node))), N);
3512 end if;
3514 -- Check license conditions unless this is a dummy unit
3516 if Sloc (Library_Unit (Item)) /= No_Location then
3517 License_Check : declare
3518 Withu : constant Unit_Number_Type :=
3519 Get_Source_Unit (Library_Unit (Item));
3520 Withl : constant License_Type :=
3521 License (Source_Index (Withu));
3522 Unitl : constant License_Type :=
3523 License (Source_Index (Current_Sem_Unit));
3525 procedure License_Error;
3526 -- Signal error of bad license
3528 -------------------
3529 -- License_Error --
3530 -------------------
3532 procedure License_Error is
3533 begin
3534 Error_Msg_N
3535 ("license of withed unit & may be inconsistent??",
3536 Name (Item));
3537 end License_Error;
3539 -- Start of processing for License_Check
3541 begin
3542 -- Exclude license check if withed unit is an internal unit.
3543 -- This situation arises e.g. with the GPL version of GNAT.
3545 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3546 null;
3548 -- Otherwise check various cases
3549 else
3550 case Unitl is
3551 when Unknown =>
3552 null;
3554 when Restricted =>
3555 if Withl = GPL then
3556 License_Error;
3557 end if;
3559 when GPL =>
3560 if Withl = Restricted then
3561 License_Error;
3562 end if;
3564 when Modified_GPL =>
3565 if Withl = Restricted or else Withl = GPL then
3566 License_Error;
3567 end if;
3569 when Unrestricted =>
3570 null;
3571 end case;
3572 end if;
3573 end License_Check;
3574 end if;
3576 -- Case of USE PACKAGE clause
3578 elsif Nkind (Item) = N_Use_Package_Clause then
3579 Analyze_Use_Package (Item);
3581 -- Case of USE TYPE clause
3583 elsif Nkind (Item) = N_Use_Type_Clause then
3584 Analyze_Use_Type (Item);
3586 -- case of PRAGMA
3588 elsif Nkind (Item) = N_Pragma then
3589 Analyze (Item);
3590 end if;
3592 <<Continue>>
3593 Next (Item);
3594 end loop;
3596 if Is_Child_Spec (Lib_Unit) then
3598 -- The unit also has implicit with_clauses on its own parents
3600 if No (Context_Items (N)) then
3601 Set_Context_Items (N, New_List);
3602 end if;
3604 Implicit_With_On_Parent (Lib_Unit, N);
3605 end if;
3607 -- If the unit is a body, the context of the specification must also
3608 -- be installed. That includes private with_clauses in that context.
3610 if Nkind (Lib_Unit) = N_Package_Body
3611 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3612 and then not Acts_As_Spec (N))
3613 then
3614 Install_Context (Library_Unit (N));
3616 -- Only install private with-clauses of a spec that comes from
3617 -- source, excluding specs created for a subprogram body that is
3618 -- a child unit.
3620 if Comes_From_Source (Library_Unit (N)) then
3621 Install_Private_With_Clauses
3622 (Defining_Entity (Unit (Library_Unit (N))));
3623 end if;
3625 if Is_Child_Spec (Unit (Library_Unit (N))) then
3627 -- If the unit is the body of a public child unit, the private
3628 -- declarations of the parent must be made visible. If the child
3629 -- unit is private, the private declarations have been installed
3630 -- already in the call to Install_Parents for the spec. Installing
3631 -- private declarations must be done for all ancestors of public
3632 -- child units. In addition, sibling units mentioned in the
3633 -- context clause of the body are directly visible.
3635 declare
3636 Lib_Spec : Node_Id;
3637 P : Node_Id;
3638 P_Name : Entity_Id;
3640 begin
3641 Lib_Spec := Unit (Library_Unit (N));
3642 while Is_Child_Spec (Lib_Spec) loop
3643 P := Unit (Parent_Spec (Lib_Spec));
3644 P_Name := Defining_Entity (P);
3646 if not (Private_Present (Parent (Lib_Spec)))
3647 and then not In_Private_Part (P_Name)
3648 then
3649 Install_Private_Declarations (P_Name);
3650 Install_Private_With_Clauses (P_Name);
3651 Set_Use (Private_Declarations (Specification (P)));
3652 end if;
3654 Lib_Spec := P;
3655 end loop;
3656 end;
3657 end if;
3659 -- For a package body, children in context are immediately visible
3661 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3662 end if;
3664 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3665 N_Generic_Subprogram_Declaration,
3666 N_Package_Declaration,
3667 N_Subprogram_Declaration)
3668 then
3669 if Is_Child_Spec (Lib_Unit) then
3670 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3671 Set_Is_Private_Descendant
3672 (Defining_Entity (Lib_Unit),
3673 Is_Private_Descendant (Lib_Parent)
3674 or else Private_Present (Parent (Lib_Unit)));
3676 else
3677 Set_Is_Private_Descendant
3678 (Defining_Entity (Lib_Unit),
3679 Private_Present (Parent (Lib_Unit)));
3680 end if;
3681 end if;
3683 if Check_Private then
3684 Check_Private_Child_Unit (N);
3685 end if;
3686 end Install_Context_Clauses;
3688 -------------------------------------
3689 -- Install_Limited_Context_Clauses --
3690 -------------------------------------
3692 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3693 Item : Node_Id;
3695 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3696 -- Check that the unlimited view of a given compilation_unit is not
3697 -- already visible through "use + renamings".
3699 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3700 -- Check that if a limited_with clause of a given compilation_unit
3701 -- mentions a descendant of a private child of some library unit, then
3702 -- the given compilation_unit must be the declaration of a private
3703 -- descendant of that library unit, or a public descendant of such. The
3704 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3705 -- use entities on the limited with_clauses because their units have not
3706 -- been analyzed, so we have to climb the tree of ancestors looking for
3707 -- private keywords.
3709 procedure Expand_Limited_With_Clause
3710 (Comp_Unit : Node_Id;
3711 Nam : Node_Id;
3712 N : Node_Id);
3713 -- If a child unit appears in a limited_with clause, there are implicit
3714 -- limited_with clauses on all parents that are not already visible
3715 -- through a regular with clause. This procedure creates the implicit
3716 -- limited with_clauses for the parents and loads the corresponding
3717 -- units. The shadow entities are created when the inserted clause is
3718 -- analyzed. Implements Ada 2005 (AI-50217).
3720 ---------------------
3721 -- Check_Renamings --
3722 ---------------------
3724 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3725 Item : Node_Id;
3726 Spec : Node_Id;
3727 WEnt : Entity_Id;
3728 Nam : Node_Id;
3729 E : Entity_Id;
3730 E2 : Entity_Id;
3732 begin
3733 pragma Assert (Nkind (W) = N_With_Clause);
3735 -- Protect the frontend against previous critical errors
3737 case Nkind (Unit (Library_Unit (W))) is
3738 when N_Subprogram_Declaration |
3739 N_Package_Declaration |
3740 N_Generic_Subprogram_Declaration |
3741 N_Generic_Package_Declaration =>
3742 null;
3744 when others =>
3745 return;
3746 end case;
3748 -- Check "use + renamings"
3750 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3751 Spec := Specification (Unit (P));
3753 Item := First (Visible_Declarations (Spec));
3754 while Present (Item) loop
3756 -- Look only at use package clauses
3758 if Nkind (Item) = N_Use_Package_Clause then
3760 -- Traverse the list of packages
3762 Nam := First (Names (Item));
3763 while Present (Nam) loop
3764 E := Entity (Nam);
3766 pragma Assert (Present (Parent (E)));
3768 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3769 and then Renamed_Entity (E) = WEnt
3770 then
3771 -- The unlimited view is visible through use clause and
3772 -- renamings. There is no need to generate the error
3773 -- message here because Is_Visible_Through_Renamings
3774 -- takes care of generating the precise error message.
3776 return;
3778 elsif Nkind (Parent (E)) = N_Package_Specification then
3780 -- The use clause may refer to a local package.
3781 -- Check all the enclosing scopes.
3783 E2 := E;
3784 while E2 /= Standard_Standard and then E2 /= WEnt loop
3785 E2 := Scope (E2);
3786 end loop;
3788 if E2 = WEnt then
3789 Error_Msg_N
3790 ("unlimited view visible through use clause ", W);
3791 return;
3792 end if;
3793 end if;
3795 Next (Nam);
3796 end loop;
3797 end if;
3799 Next (Item);
3800 end loop;
3802 -- Recursive call to check all the ancestors
3804 if Is_Child_Spec (Unit (P)) then
3805 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3806 end if;
3807 end Check_Renamings;
3809 ---------------------------------------
3810 -- Check_Private_Limited_Withed_Unit --
3811 ---------------------------------------
3813 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3814 Curr_Parent : Node_Id;
3815 Child_Parent : Node_Id;
3816 Curr_Private : Boolean;
3818 begin
3819 -- Compilation unit of the parent of the withed library unit
3821 Child_Parent := Library_Unit (Item);
3823 -- If the child unit is a public child, then locate its nearest
3824 -- private ancestor, if any, then Child_Parent will then be set to
3825 -- the parent of that ancestor.
3827 if not Private_Present (Library_Unit (Item)) then
3828 while Present (Child_Parent)
3829 and then not Private_Present (Child_Parent)
3830 loop
3831 Child_Parent := Parent_Spec (Unit (Child_Parent));
3832 end loop;
3834 if No (Child_Parent) then
3835 return;
3836 end if;
3837 end if;
3839 Child_Parent := Parent_Spec (Unit (Child_Parent));
3841 -- Traverse all the ancestors of the current compilation unit to
3842 -- check if it is a descendant of named library unit.
3844 Curr_Parent := Parent (Item);
3845 Curr_Private := Private_Present (Curr_Parent);
3847 while Present (Parent_Spec (Unit (Curr_Parent)))
3848 and then Curr_Parent /= Child_Parent
3849 loop
3850 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3851 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3852 end loop;
3854 if Curr_Parent /= Child_Parent then
3855 Error_Msg_N
3856 ("unit in with clause is private child unit!", Item);
3857 Error_Msg_NE
3858 ("\current unit must also have parent&!",
3859 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3861 elsif Private_Present (Parent (Item))
3862 or else Curr_Private
3863 or else Private_Present (Item)
3864 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3865 N_Subprogram_Body,
3866 N_Subunit)
3867 then
3868 -- Current unit is private, of descendant of a private unit
3870 null;
3872 else
3873 Error_Msg_NE
3874 ("current unit must also be private descendant of&",
3875 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3876 end if;
3877 end Check_Private_Limited_Withed_Unit;
3879 --------------------------------
3880 -- Expand_Limited_With_Clause --
3881 --------------------------------
3883 procedure Expand_Limited_With_Clause
3884 (Comp_Unit : Node_Id;
3885 Nam : Node_Id;
3886 N : Node_Id)
3888 Loc : constant Source_Ptr := Sloc (Nam);
3889 Unum : Unit_Number_Type;
3890 Withn : Node_Id;
3892 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3893 -- Returns true if the context already includes a with_clause for
3894 -- this unit. If the with_clause is non-limited, the unit is fully
3895 -- visible and an implicit limited_with should not be created. If
3896 -- there is already a limited_with clause for W, a second one is
3897 -- simply redundant.
3899 --------------------------
3900 -- Previous_Withed_Unit --
3901 --------------------------
3903 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3904 Item : Node_Id;
3906 begin
3907 -- A limited with_clause cannot appear in the same context_clause
3908 -- as a nonlimited with_clause which mentions the same library.
3910 Item := First (Context_Items (Comp_Unit));
3911 while Present (Item) loop
3912 if Nkind (Item) = N_With_Clause
3913 and then Library_Unit (Item) = Library_Unit (W)
3914 then
3915 return True;
3916 end if;
3918 Next (Item);
3919 end loop;
3921 return False;
3922 end Previous_Withed_Unit;
3924 -- Start of processing for Expand_Limited_With_Clause
3926 begin
3927 if Nkind (Nam) = N_Identifier then
3929 -- Create node for name of withed unit
3931 Withn :=
3932 Make_With_Clause (Loc,
3933 Name => New_Copy (Nam));
3935 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3936 Withn :=
3937 Make_With_Clause (Loc,
3938 Name => Make_Selected_Component (Loc,
3939 Prefix => New_Copy_Tree (Prefix (Nam)),
3940 Selector_Name => New_Copy (Selector_Name (Nam))));
3941 Set_Parent (Withn, Parent (N));
3942 end if;
3944 Set_Limited_Present (Withn);
3945 Set_First_Name (Withn);
3946 Set_Implicit_With (Withn);
3948 Unum :=
3949 Load_Unit
3950 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3951 Required => True,
3952 Subunit => False,
3953 Error_Node => Nam);
3955 -- Do not generate a limited_with_clause on the current unit. This
3956 -- path is taken when a unit has a limited_with clause on one of its
3957 -- child units.
3959 if Unum = Current_Sem_Unit then
3960 return;
3961 end if;
3963 Set_Library_Unit (Withn, Cunit (Unum));
3964 Set_Corresponding_Spec
3965 (Withn, Specification (Unit (Cunit (Unum))));
3967 if not Previous_Withed_Unit (Withn) then
3968 Prepend (Withn, Context_Items (Parent (N)));
3969 Mark_Rewrite_Insertion (Withn);
3971 -- Add implicit limited_with_clauses for parents of child units
3972 -- mentioned in limited_with clauses.
3974 if Nkind (Nam) = N_Selected_Component then
3975 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3976 end if;
3978 Analyze (Withn);
3980 if not Limited_View_Installed (Withn) then
3981 Install_Limited_Withed_Unit (Withn);
3982 end if;
3983 end if;
3984 end Expand_Limited_With_Clause;
3986 -- Start of processing for Install_Limited_Context_Clauses
3988 begin
3989 Item := First (Context_Items (N));
3990 while Present (Item) loop
3991 if Nkind (Item) = N_With_Clause
3992 and then Limited_Present (Item)
3993 and then not Error_Posted (Item)
3994 then
3995 if Nkind (Name (Item)) = N_Selected_Component then
3996 Expand_Limited_With_Clause
3997 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3998 end if;
4000 Check_Private_Limited_Withed_Unit (Item);
4002 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
4003 Check_Renamings (Parent_Spec (Unit (N)), Item);
4004 end if;
4006 -- A unit may have a limited with on itself if it has a limited
4007 -- with_clause on one of its child units. In that case it is
4008 -- already being compiled and it makes no sense to install its
4009 -- limited view.
4011 -- If the item is a limited_private_with_clause, install it if the
4012 -- current unit is a body or if it is a private child. Otherwise
4013 -- the private clause is installed before analyzing the private
4014 -- part of the current unit.
4016 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
4017 and then not Limited_View_Installed (Item)
4018 and then
4019 not Is_Ancestor_Unit
4020 (Library_Unit (Item), Cunit (Current_Sem_Unit))
4021 then
4022 if not Private_Present (Item)
4023 or else Private_Present (N)
4024 or else Nkind_In (Unit (N), N_Package_Body,
4025 N_Subprogram_Body,
4026 N_Subunit)
4027 then
4028 Install_Limited_Withed_Unit (Item);
4029 end if;
4030 end if;
4031 end if;
4033 Next (Item);
4034 end loop;
4036 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4037 -- looking for incomplete subtype declarations of incomplete types
4038 -- visible through a limited with clause.
4040 if Ada_Version >= Ada_2005
4041 and then Analyzed (N)
4042 and then Nkind (Unit (N)) = N_Package_Declaration
4043 then
4044 declare
4045 Decl : Node_Id;
4046 Def_Id : Entity_Id;
4047 Non_Lim_View : Entity_Id;
4049 begin
4050 Decl := First (Visible_Declarations (Specification (Unit (N))));
4051 while Present (Decl) loop
4052 if Nkind (Decl) = N_Subtype_Declaration
4053 and then
4054 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
4055 and then
4056 From_Limited_With (Defining_Identifier (Decl))
4057 then
4058 Def_Id := Defining_Identifier (Decl);
4059 Non_Lim_View := Non_Limited_View (Def_Id);
4061 if not Is_Incomplete_Type (Non_Lim_View) then
4063 -- Convert an incomplete subtype declaration into a
4064 -- corresponding non-limited view subtype declaration.
4065 -- This is usually the case when analyzing a body that
4066 -- has regular with clauses, when the spec has limited
4067 -- ones.
4069 -- If the non-limited view is still incomplete, it is
4070 -- the dummy entry already created, and the declaration
4071 -- cannot be reanalyzed. This is the case when installing
4072 -- a parent unit that has limited with-clauses.
4074 Set_Subtype_Indication (Decl,
4075 New_Occurrence_Of (Non_Lim_View, Sloc (Def_Id)));
4076 Set_Etype (Def_Id, Non_Lim_View);
4077 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
4078 Set_Analyzed (Decl, False);
4080 -- Reanalyze the declaration, suppressing the call to
4081 -- Enter_Name to avoid duplicate names.
4083 Analyze_Subtype_Declaration
4084 (N => Decl,
4085 Skip => True);
4086 end if;
4087 end if;
4089 Next (Decl);
4090 end loop;
4091 end;
4092 end if;
4093 end Install_Limited_Context_Clauses;
4095 ---------------------
4096 -- Install_Parents --
4097 ---------------------
4099 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
4100 P : Node_Id;
4101 E_Name : Entity_Id;
4102 P_Name : Entity_Id;
4103 P_Spec : Node_Id;
4105 begin
4106 P := Unit (Parent_Spec (Lib_Unit));
4107 P_Name := Get_Parent_Entity (P);
4109 if Etype (P_Name) = Any_Type then
4110 return;
4111 end if;
4113 if Ekind (P_Name) = E_Generic_Package
4114 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
4115 N_Generic_Package_Declaration)
4116 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
4117 then
4118 Error_Msg_N
4119 ("child of a generic package must be a generic unit", Lib_Unit);
4121 elsif not Is_Package_Or_Generic_Package (P_Name) then
4122 Error_Msg_N
4123 ("parent unit must be package or generic package", Lib_Unit);
4124 raise Unrecoverable_Error;
4126 elsif Present (Renamed_Object (P_Name)) then
4127 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
4128 raise Unrecoverable_Error;
4130 -- Verify that a child of an instance is itself an instance, or the
4131 -- renaming of one. Given that an instance that is a unit is replaced
4132 -- with a package declaration, check against the original node. The
4133 -- parent may be currently being instantiated, in which case it appears
4134 -- as a declaration, but the generic_parent is already established
4135 -- indicating that we deal with an instance.
4137 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
4138 if Nkind (Lib_Unit) in N_Renaming_Declaration
4139 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
4140 or else
4141 (Nkind (Lib_Unit) = N_Package_Declaration
4142 and then Present (Generic_Parent (Specification (Lib_Unit))))
4143 then
4144 null;
4145 else
4146 Error_Msg_N
4147 ("child of an instance must be an instance or renaming",
4148 Lib_Unit);
4149 end if;
4150 end if;
4152 -- This is the recursive call that ensures all parents are loaded
4154 if Is_Child_Spec (P) then
4155 Install_Parents (P,
4156 Is_Private or else Private_Present (Parent (Lib_Unit)));
4157 end if;
4159 -- Now we can install the context for this parent
4161 Install_Context_Clauses (Parent_Spec (Lib_Unit));
4162 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4163 Install_Siblings (P_Name, Parent (Lib_Unit));
4165 -- The child unit is in the declarative region of the parent. The parent
4166 -- must therefore appear in the scope stack and be visible, as when
4167 -- compiling the corresponding body. If the child unit is private or it
4168 -- is a package body, private declarations must be accessible as well.
4169 -- Use declarations in the parent must also be installed. Finally, other
4170 -- child units of the same parent that are in the context are
4171 -- immediately visible.
4173 -- Find entity for compilation unit, and set its private descendant
4174 -- status as needed. Indicate that it is a compilation unit, which is
4175 -- redundant in general, but needed if this is a generated child spec
4176 -- for a child body without previous spec.
4178 E_Name := Defining_Entity (Lib_Unit);
4180 Set_Is_Child_Unit (E_Name);
4181 Set_Is_Compilation_Unit (E_Name);
4183 Set_Is_Private_Descendant (E_Name,
4184 Is_Private_Descendant (P_Name)
4185 or else Private_Present (Parent (Lib_Unit)));
4187 P_Spec := Package_Specification (P_Name);
4188 Push_Scope (P_Name);
4190 -- Save current visibility of unit
4192 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4193 Is_Immediately_Visible (P_Name);
4194 Set_Is_Immediately_Visible (P_Name);
4195 Install_Visible_Declarations (P_Name);
4196 Set_Use (Visible_Declarations (P_Spec));
4198 -- If the parent is a generic unit, its formal part may contain formal
4199 -- packages and use clauses for them.
4201 if Ekind (P_Name) = E_Generic_Package then
4202 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4203 end if;
4205 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4206 Install_Private_Declarations (P_Name);
4207 Install_Private_With_Clauses (P_Name);
4208 Set_Use (Private_Declarations (P_Spec));
4209 end if;
4210 end Install_Parents;
4212 ----------------------------------
4213 -- Install_Private_With_Clauses --
4214 ----------------------------------
4216 procedure Install_Private_With_Clauses (P : Entity_Id) is
4217 Decl : constant Node_Id := Unit_Declaration_Node (P);
4218 Item : Node_Id;
4220 begin
4221 if Debug_Flag_I then
4222 Write_Str ("install private with clauses of ");
4223 Write_Name (Chars (P));
4224 Write_Eol;
4225 end if;
4227 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4228 Item := First (Context_Items (Parent (Decl)));
4229 while Present (Item) loop
4230 if Nkind (Item) = N_With_Clause
4231 and then Private_Present (Item)
4232 then
4233 -- If the unit is an ancestor of the current one, it is the
4234 -- case of a private limited with clause on a child unit, and
4235 -- the compilation of one of its descendants, In that case the
4236 -- limited view is errelevant.
4238 if Limited_Present (Item) then
4239 if not Limited_View_Installed (Item)
4240 and then
4241 not Is_Ancestor_Unit (Library_Unit (Item),
4242 Cunit (Current_Sem_Unit))
4243 then
4244 Install_Limited_Withed_Unit (Item);
4245 end if;
4246 else
4247 Install_Withed_Unit (Item, Private_With_OK => True);
4248 end if;
4249 end if;
4251 Next (Item);
4252 end loop;
4253 end if;
4254 end Install_Private_With_Clauses;
4256 ----------------------
4257 -- Install_Siblings --
4258 ----------------------
4260 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4261 Item : Node_Id;
4262 Id : Entity_Id;
4263 Prev : Entity_Id;
4265 begin
4266 -- Iterate over explicit with clauses, and check whether the scope of
4267 -- each entity is an ancestor of the current unit, in which case it is
4268 -- immediately visible.
4270 Item := First (Context_Items (N));
4271 while Present (Item) loop
4273 -- Do not install private_with_clauses declaration, unless unit
4274 -- is itself a private child unit, or is a body. Note that for a
4275 -- subprogram body the private_with_clause does not take effect until
4276 -- after the specification.
4278 if Nkind (Item) /= N_With_Clause
4279 or else Implicit_With (Item)
4280 or else Limited_Present (Item)
4281 or else Error_Posted (Item)
4282 then
4283 null;
4285 elsif not Private_Present (Item)
4286 or else Private_Present (N)
4287 or else Nkind (Unit (N)) = N_Package_Body
4288 then
4289 Id := Entity (Name (Item));
4291 if Is_Child_Unit (Id)
4292 and then Is_Ancestor_Package (Scope (Id), U_Name)
4293 then
4294 Set_Is_Immediately_Visible (Id);
4296 -- Check for the presence of another unit in the context that
4297 -- may be inadvertently hidden by the child.
4299 Prev := Current_Entity (Id);
4301 if Present (Prev)
4302 and then Is_Immediately_Visible (Prev)
4303 and then not Is_Child_Unit (Prev)
4304 then
4305 declare
4306 Clause : Node_Id;
4308 begin
4309 Clause := First (Context_Items (N));
4310 while Present (Clause) loop
4311 if Nkind (Clause) = N_With_Clause
4312 and then Entity (Name (Clause)) = Prev
4313 then
4314 Error_Msg_NE
4315 ("child unit& hides compilation unit " &
4316 "with the same name??",
4317 Name (Item), Id);
4318 exit;
4319 end if;
4321 Next (Clause);
4322 end loop;
4323 end;
4324 end if;
4326 -- The With_Clause may be on a grand-child or one of its further
4327 -- descendants, which makes a child immediately visible. Examine
4328 -- ancestry to determine whether such a child exists. For example,
4329 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4330 -- is immediately visible.
4332 elsif Is_Child_Unit (Id) then
4333 declare
4334 Par : Entity_Id;
4336 begin
4337 Par := Scope (Id);
4338 while Is_Child_Unit (Par) loop
4339 if Is_Ancestor_Package (Scope (Par), U_Name) then
4340 Set_Is_Immediately_Visible (Par);
4341 exit;
4342 end if;
4344 Par := Scope (Par);
4345 end loop;
4346 end;
4347 end if;
4349 -- If the item is a private with-clause on a child unit, the parent
4350 -- may have been installed already, but the child unit must remain
4351 -- invisible until installed in a private part or body, unless there
4352 -- is already a regular with_clause for it in the current unit.
4354 elsif Private_Present (Item) then
4355 Id := Entity (Name (Item));
4357 if Is_Child_Unit (Id) then
4358 declare
4359 Clause : Node_Id;
4361 function In_Context return Boolean;
4362 -- Scan context of current unit, to check whether there is
4363 -- a with_clause on the same unit as a private with-clause
4364 -- on a parent, in which case child unit is visible. If the
4365 -- unit is a grand-child, the same applies to its parent.
4367 ----------------
4368 -- In_Context --
4369 ----------------
4371 function In_Context return Boolean is
4372 begin
4373 Clause :=
4374 First (Context_Items (Cunit (Current_Sem_Unit)));
4375 while Present (Clause) loop
4376 if Nkind (Clause) = N_With_Clause
4377 and then Comes_From_Source (Clause)
4378 and then Is_Entity_Name (Name (Clause))
4379 and then not Private_Present (Clause)
4380 then
4381 if Entity (Name (Clause)) = Id
4382 or else
4383 (Nkind (Name (Clause)) = N_Expanded_Name
4384 and then Entity (Prefix (Name (Clause))) = Id)
4385 then
4386 return True;
4387 end if;
4388 end if;
4390 Next (Clause);
4391 end loop;
4393 return False;
4394 end In_Context;
4396 begin
4397 Set_Is_Visible_Lib_Unit (Id, In_Context);
4398 end;
4399 end if;
4400 end if;
4402 Next (Item);
4403 end loop;
4404 end Install_Siblings;
4406 ---------------------------------
4407 -- Install_Limited_Withed_Unit --
4408 ---------------------------------
4410 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4411 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4412 E : Entity_Id;
4413 P : Entity_Id;
4414 Is_Child_Package : Boolean := False;
4415 Lim_Header : Entity_Id;
4416 Lim_Typ : Entity_Id;
4418 procedure Check_Body_Required;
4419 -- A unit mentioned in a limited with_clause may not be mentioned in
4420 -- a regular with_clause, but must still be included in the current
4421 -- partition. We need to determine whether the unit needs a body, so
4422 -- that the binder can determine the name of the file to be compiled.
4423 -- Checking whether a unit needs a body can be done without semantic
4424 -- analysis, by examining the nature of the declarations in the package.
4426 function Has_Limited_With_Clause
4427 (C_Unit : Entity_Id;
4428 Pack : Entity_Id) return Boolean;
4429 -- Determine whether any package in the ancestor chain starting with
4430 -- C_Unit has a limited with clause for package Pack.
4432 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4433 -- Check if some package installed though normal with-clauses has a
4434 -- renaming declaration of package P. AARM 10.1.2(21/2).
4436 -------------------------
4437 -- Check_Body_Required --
4438 -------------------------
4440 procedure Check_Body_Required is
4441 PA : constant List_Id :=
4442 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4444 procedure Check_Declarations (Spec : Node_Id);
4445 -- Recursive procedure that does the work and checks nested packages
4447 ------------------------
4448 -- Check_Declarations --
4449 ------------------------
4451 procedure Check_Declarations (Spec : Node_Id) is
4452 Decl : Node_Id;
4453 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4455 Subp_List : constant Elist_Id := New_Elmt_List;
4457 procedure Check_Pragma_Import (P : Node_Id);
4458 -- If a pragma import applies to a previous subprogram, the
4459 -- enclosing unit may not need a body. The processing is syntactic
4460 -- and does not require a declaration to be analyzed. The code
4461 -- below also handles pragma Import when applied to a subprogram
4462 -- that renames another. In this case the pragma applies to the
4463 -- renamed entity.
4465 -- Chains of multiple renames are not handled by the code below.
4466 -- It is probably impossible to handle all cases without proper
4467 -- name resolution. In such cases the algorithm is conservative
4468 -- and will indicate that a body is needed???
4470 -------------------------
4471 -- Check_Pragma_Import --
4472 -------------------------
4474 procedure Check_Pragma_Import (P : Node_Id) is
4475 Arg : Node_Id;
4476 Prev_Id : Elmt_Id;
4477 Subp_Id : Elmt_Id;
4478 Imported : Node_Id;
4480 procedure Remove_Homonyms (E : Node_Id);
4481 -- Make one pass over list of subprograms. Called again if
4482 -- subprogram is a renaming. E is known to be an identifier.
4484 ---------------------
4485 -- Remove_Homonyms --
4486 ---------------------
4488 procedure Remove_Homonyms (E : Node_Id) is
4489 R : Entity_Id := Empty;
4490 -- Name of renamed entity, if any
4492 begin
4493 Subp_Id := First_Elmt (Subp_List);
4494 while Present (Subp_Id) loop
4495 if Chars (Node (Subp_Id)) = Chars (E) then
4496 if Nkind (Parent (Parent (Node (Subp_Id))))
4497 /= N_Subprogram_Renaming_Declaration
4498 then
4499 Prev_Id := Subp_Id;
4500 Next_Elmt (Subp_Id);
4501 Remove_Elmt (Subp_List, Prev_Id);
4502 else
4503 R := Name (Parent (Parent (Node (Subp_Id))));
4504 exit;
4505 end if;
4506 else
4507 Next_Elmt (Subp_Id);
4508 end if;
4509 end loop;
4511 if Present (R) then
4512 if Nkind (R) = N_Identifier then
4513 Remove_Homonyms (R);
4515 elsif Nkind (R) = N_Selected_Component then
4516 Remove_Homonyms (Selector_Name (R));
4518 -- Renaming of attribute
4520 else
4521 null;
4522 end if;
4523 end if;
4524 end Remove_Homonyms;
4526 -- Start of processing for Check_Pragma_Import
4528 begin
4529 -- Find name of entity in Import pragma. We have not analyzed
4530 -- the construct, so we must guard against syntax errors.
4532 Arg := Next (First (Pragma_Argument_Associations (P)));
4534 if No (Arg)
4535 or else Nkind (Expression (Arg)) /= N_Identifier
4536 then
4537 return;
4538 else
4539 Imported := Expression (Arg);
4540 end if;
4542 Remove_Homonyms (Imported);
4543 end Check_Pragma_Import;
4545 -- Start of processing for Check_Declarations
4547 begin
4548 -- Search for Elaborate Body pragma
4550 Decl := First (Visible_Declarations (Spec));
4551 while Present (Decl)
4552 and then Nkind (Decl) = N_Pragma
4553 loop
4554 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4555 Set_Body_Required (Library_Unit (N));
4556 return;
4557 end if;
4559 Next (Decl);
4560 end loop;
4562 -- Look for declarations that require the presence of a body. We
4563 -- have already skipped pragmas at the start of the list.
4565 while Present (Decl) loop
4567 -- Subprogram that comes from source means body may be needed.
4568 -- Save for subsequent examination of import pragmas.
4570 if Comes_From_Source (Decl)
4571 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4572 N_Subprogram_Renaming_Declaration,
4573 N_Generic_Subprogram_Declaration))
4574 then
4575 Append_Elmt (Defining_Entity (Decl), Subp_List);
4577 -- Package declaration of generic package declaration. We need
4578 -- to recursively examine nested declarations.
4580 elsif Nkind_In (Decl, N_Package_Declaration,
4581 N_Generic_Package_Declaration)
4582 then
4583 Check_Declarations (Specification (Decl));
4585 elsif Nkind (Decl) = N_Pragma
4586 and then Pragma_Name (Decl) = Name_Import
4587 then
4588 Check_Pragma_Import (Decl);
4589 end if;
4591 Next (Decl);
4592 end loop;
4594 -- Same set of tests for private part. In addition to subprograms
4595 -- detect the presence of Taft Amendment types (incomplete types
4596 -- completed in the body).
4598 Decl := First (Private_Declarations (Spec));
4599 while Present (Decl) loop
4600 if Comes_From_Source (Decl)
4601 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4602 N_Subprogram_Renaming_Declaration,
4603 N_Generic_Subprogram_Declaration))
4604 then
4605 Append_Elmt (Defining_Entity (Decl), Subp_List);
4607 elsif Nkind_In (Decl, N_Package_Declaration,
4608 N_Generic_Package_Declaration)
4609 then
4610 Check_Declarations (Specification (Decl));
4612 -- Collect incomplete type declarations for separate pass
4614 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4615 Append_Elmt (Decl, Incomplete_Decls);
4617 elsif Nkind (Decl) = N_Pragma
4618 and then Pragma_Name (Decl) = Name_Import
4619 then
4620 Check_Pragma_Import (Decl);
4621 end if;
4623 Next (Decl);
4624 end loop;
4626 -- Now check incomplete declarations to locate Taft amendment
4627 -- types. This can be done by examining the defining identifiers
4628 -- of type declarations without real semantic analysis.
4630 declare
4631 Inc : Elmt_Id;
4633 begin
4634 Inc := First_Elmt (Incomplete_Decls);
4635 while Present (Inc) loop
4636 Decl := Next (Node (Inc));
4637 while Present (Decl) loop
4638 if Nkind (Decl) = N_Full_Type_Declaration
4639 and then Chars (Defining_Identifier (Decl)) =
4640 Chars (Defining_Identifier (Node (Inc)))
4641 then
4642 exit;
4643 end if;
4645 Next (Decl);
4646 end loop;
4648 -- If no completion, this is a TAT, and a body is needed
4650 if No (Decl) then
4651 Set_Body_Required (Library_Unit (N));
4652 return;
4653 end if;
4655 Next_Elmt (Inc);
4656 end loop;
4657 end;
4659 -- Finally, check whether there are subprograms that still require
4660 -- a body, i.e. are not renamings or null.
4662 if not Is_Empty_Elmt_List (Subp_List) then
4663 declare
4664 Subp_Id : Elmt_Id;
4665 Spec : Node_Id;
4667 begin
4668 Subp_Id := First_Elmt (Subp_List);
4669 Spec := Parent (Node (Subp_Id));
4671 while Present (Subp_Id) loop
4672 if Nkind (Parent (Spec))
4673 = N_Subprogram_Renaming_Declaration
4674 then
4675 null;
4677 elsif Nkind (Spec) = N_Procedure_Specification
4678 and then Null_Present (Spec)
4679 then
4680 null;
4682 else
4683 Set_Body_Required (Library_Unit (N));
4684 return;
4685 end if;
4687 Next_Elmt (Subp_Id);
4688 end loop;
4689 end;
4690 end if;
4691 end Check_Declarations;
4693 -- Start of processing for Check_Body_Required
4695 begin
4696 -- If this is an imported package (Java and CIL usage) no body is
4697 -- needed. Scan list of pragmas that may follow a compilation unit
4698 -- to look for a relevant pragma Import.
4700 if Present (PA) then
4701 declare
4702 Prag : Node_Id;
4704 begin
4705 Prag := First (PA);
4706 while Present (Prag) loop
4707 if Nkind (Prag) = N_Pragma
4708 and then Get_Pragma_Id (Prag) = Pragma_Import
4709 then
4710 return;
4711 end if;
4713 Next (Prag);
4714 end loop;
4715 end;
4716 end if;
4718 Check_Declarations (Specification (P_Unit));
4719 end Check_Body_Required;
4721 -----------------------------
4722 -- Has_Limited_With_Clause --
4723 -----------------------------
4725 function Has_Limited_With_Clause
4726 (C_Unit : Entity_Id;
4727 Pack : Entity_Id) return Boolean
4729 Par : Entity_Id;
4730 Par_Unit : Node_Id;
4732 begin
4733 Par := C_Unit;
4734 while Present (Par) loop
4735 if Ekind (Par) /= E_Package then
4736 exit;
4737 end if;
4739 -- Retrieve the Compilation_Unit node for Par and determine if
4740 -- its context clauses contain a limited with for Pack.
4742 Par_Unit := Parent (Parent (Parent (Par)));
4744 if Nkind (Par_Unit) = N_Package_Declaration then
4745 Par_Unit := Parent (Par_Unit);
4746 end if;
4748 if Has_With_Clause (Par_Unit, Pack, True) then
4749 return True;
4750 end if;
4752 -- If there are more ancestors, climb up the tree, otherwise we
4753 -- are done.
4755 if Is_Child_Unit (Par) then
4756 Par := Scope (Par);
4757 else
4758 exit;
4759 end if;
4760 end loop;
4762 return False;
4763 end Has_Limited_With_Clause;
4765 ----------------------------------
4766 -- Is_Visible_Through_Renamings --
4767 ----------------------------------
4769 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4770 Kind : constant Node_Kind :=
4771 Nkind (Unit (Cunit (Current_Sem_Unit)));
4772 Aux_Unit : Node_Id;
4773 Item : Node_Id;
4774 Decl : Entity_Id;
4776 begin
4777 -- Example of the error detected by this subprogram:
4779 -- package P is
4780 -- type T is ...
4781 -- end P;
4783 -- with P;
4784 -- package Q is
4785 -- package Ren_P renames P;
4786 -- end Q;
4788 -- with Q;
4789 -- package R is ...
4791 -- limited with P; -- ERROR
4792 -- package R.C is ...
4794 Aux_Unit := Cunit (Current_Sem_Unit);
4796 loop
4797 Item := First (Context_Items (Aux_Unit));
4798 while Present (Item) loop
4799 if Nkind (Item) = N_With_Clause
4800 and then not Limited_Present (Item)
4801 and then Nkind (Unit (Library_Unit (Item))) =
4802 N_Package_Declaration
4803 then
4804 Decl :=
4805 First (Visible_Declarations
4806 (Specification (Unit (Library_Unit (Item)))));
4807 while Present (Decl) loop
4808 if Nkind (Decl) = N_Package_Renaming_Declaration
4809 and then Entity (Name (Decl)) = P
4810 then
4811 -- Generate the error message only if the current unit
4812 -- is a package declaration; in case of subprogram
4813 -- bodies and package bodies we just return True to
4814 -- indicate that the limited view must not be
4815 -- installed.
4817 if Kind = N_Package_Declaration then
4818 Error_Msg_N
4819 ("simultaneous visibility of the limited and " &
4820 "unlimited views not allowed", N);
4821 Error_Msg_Sloc := Sloc (Item);
4822 Error_Msg_NE
4823 ("\\ unlimited view of & visible through the " &
4824 "context clause #", N, P);
4825 Error_Msg_Sloc := Sloc (Decl);
4826 Error_Msg_NE ("\\ and the renaming #", N, P);
4827 end if;
4829 return True;
4830 end if;
4832 Next (Decl);
4833 end loop;
4834 end if;
4836 Next (Item);
4837 end loop;
4839 -- If it is a body not acting as spec, follow pointer to the
4840 -- corresponding spec, otherwise follow pointer to parent spec.
4842 if Present (Library_Unit (Aux_Unit))
4843 and then Nkind_In (Unit (Aux_Unit),
4844 N_Package_Body, N_Subprogram_Body)
4845 then
4846 if Aux_Unit = Library_Unit (Aux_Unit) then
4848 -- Aux_Unit is a body that acts as a spec. Clause has
4849 -- already been flagged as illegal.
4851 return False;
4853 else
4854 Aux_Unit := Library_Unit (Aux_Unit);
4855 end if;
4857 else
4858 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4859 end if;
4861 exit when No (Aux_Unit);
4862 end loop;
4864 return False;
4865 end Is_Visible_Through_Renamings;
4867 -- Start of processing for Install_Limited_Withed_Unit
4869 begin
4870 pragma Assert (not Limited_View_Installed (N));
4872 -- In case of limited with_clause on subprograms, generics, instances,
4873 -- or renamings, the corresponding error was previously posted and we
4874 -- have nothing to do here. If the file is missing altogether, it has
4875 -- no source location.
4877 if Nkind (P_Unit) /= N_Package_Declaration
4878 or else Sloc (P_Unit) = No_Location
4879 then
4880 return;
4881 end if;
4883 P := Defining_Unit_Name (Specification (P_Unit));
4885 -- Handle child packages
4887 if Nkind (P) = N_Defining_Program_Unit_Name then
4888 Is_Child_Package := True;
4889 P := Defining_Identifier (P);
4890 end if;
4892 -- Do not install the limited-view if the context of the unit is already
4893 -- available through a regular with clause.
4895 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4896 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4897 then
4898 return;
4899 end if;
4901 -- Do not install the limited-view if the full-view is already visible
4902 -- through renaming declarations.
4904 if Is_Visible_Through_Renamings (P) then
4905 return;
4906 end if;
4908 -- Do not install the limited view if this is the unit being analyzed.
4909 -- This unusual case will happen when a unit has a limited_with clause
4910 -- on one of its children. The compilation of the child forces the load
4911 -- of the parent which tries to install the limited view of the child
4912 -- again. Installing the limited view must also be disabled when
4913 -- compiling the body of the child unit.
4915 if P = Cunit_Entity (Current_Sem_Unit)
4916 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4917 and then P = Main_Unit_Entity
4918 and then Is_Ancestor_Unit
4919 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
4920 then
4921 return;
4922 end if;
4924 -- This scenario is similar to the one above, the difference is that the
4925 -- compilation of sibling Par.Sib forces the load of parent Par which
4926 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4927 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4928 -- non-limited views of all entities from Lim_Pack.
4930 -- limited with Lim_Pack; -- [1]
4931 -- package Par is ... package Lim_Pack is ...
4933 -- with Lim_Pack; -- [2]
4934 -- package Par.Sib is ... package body Par.Sib is ...
4936 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4937 -- Sem_Unit is the body of Par.Sib.
4939 if Ekind (P) = E_Package
4940 and then Ekind (Main_Unit_Entity) = E_Package
4941 and then Is_Child_Unit (Main_Unit_Entity)
4943 -- The body has a regular with clause
4945 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4946 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4948 -- One of the ancestors has a limited with clause
4950 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4951 N_Package_Specification
4952 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4953 then
4954 return;
4955 end if;
4957 -- A common use of the limited-with is to have a limited-with in the
4958 -- package spec, and a normal with in its package body. For example:
4960 -- limited with X; -- [1]
4961 -- package A is ...
4963 -- with X; -- [2]
4964 -- package body A is ...
4966 -- The compilation of A's body installs the context clauses found at [2]
4967 -- and then the context clauses of its specification (found at [1]). As
4968 -- a consequence, at [1] the specification of X has been analyzed and it
4969 -- is immediately visible. According to the semantics of limited-with
4970 -- context clauses we don't install the limited view because the full
4971 -- view of X supersedes its limited view.
4973 if Analyzed (P_Unit)
4974 and then
4975 (Is_Immediately_Visible (P)
4976 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
4977 then
4979 -- The presence of both the limited and the analyzed nonlimited view
4980 -- may also be an error, such as an illegal context for a limited
4981 -- with_clause. In that case, do not process the context item at all.
4983 if Error_Posted (N) then
4984 return;
4985 end if;
4987 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
4988 declare
4989 Item : Node_Id;
4990 begin
4991 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
4992 while Present (Item) loop
4993 if Nkind (Item) = N_With_Clause
4994 and then Comes_From_Source (Item)
4995 and then Entity (Name (Item)) = P
4996 then
4997 return;
4998 end if;
5000 Next (Item);
5001 end loop;
5002 end;
5004 -- If this is a child body, assume that the nonlimited with_clause
5005 -- appears in an ancestor. Could be refined ???
5007 if Is_Child_Unit
5008 (Defining_Entity
5009 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
5010 then
5011 return;
5012 end if;
5014 else
5016 -- If in package declaration, nonlimited view brought in from
5017 -- parent unit or some error condition.
5019 return;
5020 end if;
5021 end if;
5023 if Debug_Flag_I then
5024 Write_Str ("install limited view of ");
5025 Write_Name (Chars (P));
5026 Write_Eol;
5027 end if;
5029 -- If the unit has not been analyzed and the limited view has not been
5030 -- already installed then we install it.
5032 if not Analyzed (P_Unit) then
5033 if not In_Chain (P) then
5035 -- Minimum decoration
5037 Set_Ekind (P, E_Package);
5038 Set_Etype (P, Standard_Void_Type);
5039 Set_Scope (P, Standard_Standard);
5040 Set_Is_Visible_Lib_Unit (P);
5042 if Is_Child_Package then
5043 Set_Is_Child_Unit (P);
5044 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
5045 end if;
5047 -- Place entity on visibility structure
5049 Set_Homonym (P, Current_Entity (P));
5050 Set_Current_Entity (P);
5052 if Debug_Flag_I then
5053 Write_Str (" (homonym) chain ");
5054 Write_Name (Chars (P));
5055 Write_Eol;
5056 end if;
5058 -- Install the incomplete view. The first element of the limited
5059 -- view is a header (an E_Package entity) used to reference the
5060 -- first shadow entity in the private part of the package.
5062 Lim_Header := Limited_View (P);
5063 Lim_Typ := First_Entity (Lim_Header);
5065 while Present (Lim_Typ)
5066 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5067 loop
5068 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
5069 Set_Current_Entity (Lim_Typ);
5071 if Debug_Flag_I then
5072 Write_Str (" (homonym) chain ");
5073 Write_Name (Chars (Lim_Typ));
5074 Write_Eol;
5075 end if;
5077 Next_Entity (Lim_Typ);
5078 end loop;
5079 end if;
5081 -- If the unit appears in a previous regular with_clause, the regular
5082 -- entities of the public part of the withed package must be replaced
5083 -- by the shadow ones.
5085 -- This code must be kept synchronized with the code that replaces the
5086 -- shadow entities by the real entities (see body of Remove_Limited
5087 -- With_Clause); otherwise the contents of the homonym chains are not
5088 -- consistent.
5090 else
5091 -- Hide all the type entities of the public part of the package to
5092 -- avoid its usage. This is needed to cover all the subtype decla-
5093 -- rations because we do not remove them from the homonym chain.
5095 E := First_Entity (P);
5096 while Present (E) and then E /= First_Private_Entity (P) loop
5097 if Is_Type (E) then
5098 Set_Was_Hidden (E, Is_Hidden (E));
5099 Set_Is_Hidden (E);
5100 end if;
5102 Next_Entity (E);
5103 end loop;
5105 -- Replace the real entities by the shadow entities of the limited
5106 -- view. The first element of the limited view is a header that is
5107 -- used to reference the first shadow entity in the private part
5108 -- of the package. Successive elements are the limited views of the
5109 -- type (including regular incomplete types) declared in the package.
5111 Lim_Header := Limited_View (P);
5113 Lim_Typ := First_Entity (Lim_Header);
5114 while Present (Lim_Typ)
5115 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5116 loop
5117 pragma Assert (not In_Chain (Lim_Typ));
5119 -- Do not unchain nested packages and child units
5121 if Ekind (Lim_Typ) /= E_Package
5122 and then not Is_Child_Unit (Lim_Typ)
5123 then
5124 declare
5125 Prev : Entity_Id;
5127 begin
5128 Prev := Current_Entity (Lim_Typ);
5129 E := Prev;
5131 -- Replace E in the homonyms list, so that the limited view
5132 -- becomes available.
5134 -- If the non-limited view is a record with an anonymous
5135 -- self-referential component, the analysis of the record
5136 -- declaration creates an incomplete type with the same name
5137 -- in order to define an internal access type. The visible
5138 -- entity is now the incomplete type, and that is the one to
5139 -- replace in the visibility structure.
5141 if E = Non_Limited_View (Lim_Typ)
5142 or else
5143 (Ekind (E) = E_Incomplete_Type
5144 and then Full_View (E) = Non_Limited_View (Lim_Typ))
5145 then
5146 Set_Homonym (Lim_Typ, Homonym (Prev));
5147 Set_Current_Entity (Lim_Typ);
5149 else
5150 loop
5151 E := Homonym (Prev);
5153 -- E may have been removed when installing a previous
5154 -- limited_with_clause.
5156 exit when No (E);
5157 exit when E = Non_Limited_View (Lim_Typ);
5158 Prev := Homonym (Prev);
5159 end loop;
5161 if Present (E) then
5162 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
5163 Set_Homonym (Prev, Lim_Typ);
5164 end if;
5165 end if;
5166 end;
5168 if Debug_Flag_I then
5169 Write_Str (" (homonym) chain ");
5170 Write_Name (Chars (Lim_Typ));
5171 Write_Eol;
5172 end if;
5173 end if;
5175 Next_Entity (Lim_Typ);
5176 end loop;
5177 end if;
5179 -- The package must be visible while the limited-with clause is active
5180 -- because references to the type P.T must resolve in the usual way.
5181 -- In addition, we remember that the limited-view has been installed to
5182 -- uninstall it at the point of context removal.
5184 Set_Is_Immediately_Visible (P);
5185 Set_Limited_View_Installed (N);
5187 -- If unit has not been analyzed in some previous context, check
5188 -- (imperfectly ???) whether it might need a body.
5190 if not Analyzed (P_Unit) then
5191 Check_Body_Required;
5192 end if;
5194 -- If the package in the limited_with clause is a child unit, the clause
5195 -- is unanalyzed and appears as a selected component. Recast it as an
5196 -- expanded name so that the entity can be properly set. Use entity of
5197 -- parent, if available, for higher ancestors in the name.
5199 if Nkind (Name (N)) = N_Selected_Component then
5200 declare
5201 Nam : Node_Id;
5202 Ent : Entity_Id;
5204 begin
5205 Nam := Name (N);
5206 Ent := P;
5207 while Nkind (Nam) = N_Selected_Component
5208 and then Present (Ent)
5209 loop
5210 Change_Selected_Component_To_Expanded_Name (Nam);
5212 -- Set entity of parent identifiers if the unit is a child
5213 -- unit. This ensures that the tree is properly formed from
5214 -- semantic point of view (e.g. for ASIS queries). The unit
5215 -- entities are not fully analyzed, so we need to follow unit
5216 -- links in the tree.
5218 Set_Entity (Nam, Ent);
5220 Nam := Prefix (Nam);
5221 Ent :=
5222 Defining_Entity
5223 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5225 -- Set entity of last ancestor
5227 if Nkind (Nam) = N_Identifier then
5228 Set_Entity (Nam, Ent);
5229 end if;
5230 end loop;
5231 end;
5232 end if;
5234 Set_Entity (Name (N), P);
5235 Set_From_Limited_With (P);
5236 end Install_Limited_Withed_Unit;
5238 -------------------------
5239 -- Install_Withed_Unit --
5240 -------------------------
5242 procedure Install_Withed_Unit
5243 (With_Clause : Node_Id;
5244 Private_With_OK : Boolean := False)
5246 Uname : constant Entity_Id := Entity (Name (With_Clause));
5247 P : constant Entity_Id := Scope (Uname);
5249 begin
5250 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5251 -- compiling a package declaration and the Private_With_OK flag was not
5252 -- set by the caller. These declarations will be installed later (before
5253 -- analyzing the private part of the package).
5255 if Private_Present (With_Clause)
5256 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
5257 and then not (Private_With_OK)
5258 then
5259 return;
5260 end if;
5262 if Debug_Flag_I then
5263 if Private_Present (With_Clause) then
5264 Write_Str ("install private withed unit ");
5265 else
5266 Write_Str ("install withed unit ");
5267 end if;
5269 Write_Name (Chars (Uname));
5270 Write_Eol;
5271 end if;
5273 -- We do not apply the restrictions to an internal unit unless we are
5274 -- compiling the internal unit as a main unit. This check is also
5275 -- skipped for dummy units (for missing packages).
5277 if Sloc (Uname) /= No_Location
5278 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
5279 or else Current_Sem_Unit = Main_Unit)
5280 then
5281 Check_Restricted_Unit
5282 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5283 end if;
5285 if P /= Standard_Standard then
5287 -- If the unit is not analyzed after analysis of the with clause and
5288 -- it is an instantiation then it awaits a body and is the main unit.
5289 -- Its appearance in the context of some other unit indicates a
5290 -- circular dependency (DEC suite perversity).
5292 if not Analyzed (Uname)
5293 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5294 then
5295 Error_Msg_N
5296 ("instantiation depends on itself", Name (With_Clause));
5298 elsif not Is_Visible_Lib_Unit (Uname) then
5300 -- Abandon processing in case of previous errors
5302 if No (Scope (Uname)) then
5303 Check_Error_Detected;
5304 return;
5305 end if;
5307 Set_Is_Visible_Lib_Unit (Uname);
5309 -- If the unit is a wrapper package for a compilation unit that is
5310 -- a subprogrm instance, indicate that the instance itself is a
5311 -- visible unit. This is necessary if the instance is inlined.
5313 if Is_Wrapper_Package (Uname) then
5314 Set_Is_Visible_Lib_Unit (Related_Instance (Uname));
5315 end if;
5317 -- If the child unit appears in the context of its parent, it is
5318 -- immediately visible.
5320 if In_Open_Scopes (Scope (Uname)) then
5321 Set_Is_Immediately_Visible (Uname);
5322 end if;
5324 if Is_Generic_Instance (Uname)
5325 and then Ekind (Uname) in Subprogram_Kind
5326 then
5327 -- Set flag as well on the visible entity that denotes the
5328 -- instance, which renames the current one.
5330 Set_Is_Visible_Lib_Unit
5331 (Related_Instance
5332 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5333 end if;
5335 -- The parent unit may have been installed already, and may have
5336 -- appeared in a use clause.
5338 if In_Use (Scope (Uname)) then
5339 Set_Is_Potentially_Use_Visible (Uname);
5340 end if;
5342 Set_Context_Installed (With_Clause);
5343 end if;
5345 elsif not Is_Immediately_Visible (Uname) then
5346 Set_Is_Visible_Lib_Unit (Uname);
5348 if not Private_Present (With_Clause) or else Private_With_OK then
5349 Set_Is_Immediately_Visible (Uname);
5350 end if;
5352 Set_Context_Installed (With_Clause);
5353 end if;
5355 -- A with-clause overrides a with-type clause: there are no restric-
5356 -- tions on the use of package entities.
5358 if Ekind (Uname) = E_Package then
5359 Set_From_Limited_With (Uname, False);
5360 end if;
5362 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5363 -- unit if there is a visible homograph for it declared in the same
5364 -- declarative region. This pathological case can only arise when an
5365 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5366 -- G1 has a generic child also named G2, and the context includes with_
5367 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5368 -- of I1.G2 visible as well. If the child unit is named Standard, do
5369 -- not apply the check to the Standard package itself.
5371 if Is_Child_Unit (Uname)
5372 and then Is_Visible_Lib_Unit (Uname)
5373 and then Ada_Version >= Ada_2005
5374 then
5375 declare
5376 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5377 Decl2 : Node_Id;
5378 P2 : Entity_Id;
5379 U2 : Entity_Id;
5381 begin
5382 U2 := Homonym (Uname);
5383 while Present (U2) and then U2 /= Standard_Standard loop
5384 P2 := Scope (U2);
5385 Decl2 := Unit_Declaration_Node (P2);
5387 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5388 if Is_Generic_Instance (P)
5389 and then Nkind (Decl1) = N_Package_Declaration
5390 and then Generic_Parent (Specification (Decl1)) = P2
5391 then
5392 Error_Msg_N ("illegal with_clause", With_Clause);
5393 Error_Msg_N
5394 ("\child unit has visible homograph" &
5395 " (RM 8.3(26), 10.1.1(19))",
5396 With_Clause);
5397 exit;
5399 elsif Is_Generic_Instance (P2)
5400 and then Nkind (Decl2) = N_Package_Declaration
5401 and then Generic_Parent (Specification (Decl2)) = P
5402 then
5403 -- With_clause for child unit of instance appears before
5404 -- in the context. We want to place the error message on
5405 -- it, not on the generic child unit itself.
5407 declare
5408 Prev_Clause : Node_Id;
5410 begin
5411 Prev_Clause := First (List_Containing (With_Clause));
5412 while Entity (Name (Prev_Clause)) /= U2 loop
5413 Next (Prev_Clause);
5414 end loop;
5416 pragma Assert (Present (Prev_Clause));
5417 Error_Msg_N ("illegal with_clause", Prev_Clause);
5418 Error_Msg_N
5419 ("\child unit has visible homograph" &
5420 " (RM 8.3(26), 10.1.1(19))",
5421 Prev_Clause);
5422 exit;
5423 end;
5424 end if;
5425 end if;
5427 U2 := Homonym (U2);
5428 end loop;
5429 end;
5430 end if;
5431 end Install_Withed_Unit;
5433 -------------------
5434 -- Is_Child_Spec --
5435 -------------------
5437 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5438 K : constant Node_Kind := Nkind (Lib_Unit);
5440 begin
5441 return (K in N_Generic_Declaration or else
5442 K in N_Generic_Instantiation or else
5443 K in N_Generic_Renaming_Declaration or else
5444 K = N_Package_Declaration or else
5445 K = N_Package_Renaming_Declaration or else
5446 K = N_Subprogram_Declaration or else
5447 K = N_Subprogram_Renaming_Declaration)
5448 and then Present (Parent_Spec (Lib_Unit));
5449 end Is_Child_Spec;
5451 ------------------------------------
5452 -- Is_Legal_Shadow_Entity_In_Body --
5453 ------------------------------------
5455 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5456 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5457 begin
5458 return Nkind (Unit (C_Unit)) = N_Package_Body
5459 and then
5460 Has_With_Clause
5461 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5462 end Is_Legal_Shadow_Entity_In_Body;
5464 ----------------------
5465 -- Is_Ancestor_Unit --
5466 ----------------------
5468 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5469 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5470 E2 : Entity_Id;
5471 begin
5472 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
5473 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5474 return Is_Ancestor_Package (E1, E2);
5475 else
5476 return False;
5477 end if;
5478 end Is_Ancestor_Unit;
5480 -----------------------
5481 -- Load_Needed_Body --
5482 -----------------------
5484 -- N is a generic unit named in a with clause, or else it is a unit that
5485 -- contains a generic unit or an inlined function. In order to perform an
5486 -- instantiation, the body of the unit must be present. If the unit itself
5487 -- is generic, we assume that an instantiation follows, and load & analyze
5488 -- the body unconditionally. This forces analysis of the spec as well.
5490 -- If the unit is not generic, but contains a generic unit, it is loaded on
5491 -- demand, at the point of instantiation (see ch12).
5493 procedure Load_Needed_Body
5494 (N : Node_Id;
5495 OK : out Boolean;
5496 Do_Analyze : Boolean := True)
5498 Body_Name : Unit_Name_Type;
5499 Unum : Unit_Number_Type;
5501 Save_Style_Check : constant Boolean := Opt.Style_Check;
5502 -- The loading and analysis is done with style checks off
5504 begin
5505 if not GNAT_Mode then
5506 Style_Check := False;
5507 end if;
5509 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5510 Unum :=
5511 Load_Unit
5512 (Load_Name => Body_Name,
5513 Required => False,
5514 Subunit => False,
5515 Error_Node => N,
5516 Renamings => True);
5518 if Unum = No_Unit then
5519 OK := False;
5521 else
5522 Compiler_State := Analyzing; -- reset after load
5524 if Fatal_Error (Unum) /= Error_Detected or else Try_Semantics then
5525 if Debug_Flag_L then
5526 Write_Str ("*** Loaded generic body");
5527 Write_Eol;
5528 end if;
5530 if Do_Analyze then
5531 Semantics (Cunit (Unum));
5532 end if;
5533 end if;
5535 OK := True;
5536 end if;
5538 Style_Check := Save_Style_Check;
5539 end Load_Needed_Body;
5541 -------------------------
5542 -- Build_Limited_Views --
5543 -------------------------
5545 procedure Build_Limited_Views (N : Node_Id) is
5546 Unum : constant Unit_Number_Type :=
5547 Get_Source_Unit (Library_Unit (N));
5548 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
5550 Shadow_Pack : Entity_Id;
5551 -- The corresponding shadow entity of the withed package. This entity
5552 -- offers incomplete views of packages and types as well as abstract
5553 -- views of states and variables declared within.
5555 Last_Shadow : Entity_Id := Empty;
5556 -- The last shadow entity created by routine Build_Shadow_Entity
5558 procedure Build_Shadow_Entity
5559 (Ent : Entity_Id;
5560 Scop : Entity_Id;
5561 Shadow : out Entity_Id;
5562 Is_Tagged : Boolean := False);
5563 -- Create a shadow entity that hides Ent and offers an abstract or
5564 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5565 -- should be set when Ent is a tagged type. The generated entity is
5566 -- added to Lim_Header. This routine updates the value of Last_Shadow.
5568 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
5569 -- Perform minimal decoration of a package or its corresponding shadow
5570 -- entity denoted by Ent. Scop is the proper scope.
5572 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id);
5573 -- Perform full decoration of an abstract state or its corresponding
5574 -- shadow entity denoted by Ent. Scop is the proper scope.
5576 procedure Decorate_Type
5577 (Ent : Entity_Id;
5578 Scop : Entity_Id;
5579 Is_Tagged : Boolean := False;
5580 Materialize : Boolean := False);
5581 -- Perform minimal decoration of a type or its corresponding shadow
5582 -- entity denoted by Ent. Scop is the proper scope. Flag Is_Tagged
5583 -- should be set when Ent is a tagged type. Flag Materialize should be
5584 -- set when Ent is a tagged type and its class-wide type needs to appear
5585 -- in the tree.
5587 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id);
5588 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5589 -- proper scope.
5591 procedure Process_Declarations_And_States
5592 (Pack : Entity_Id;
5593 Decls : List_Id;
5594 Scop : Entity_Id;
5595 Create_Abstract_Views : Boolean);
5596 -- Inspect the states of package Pack and declarative list Decls. Create
5597 -- shadow entities for all nested packages, states, types and variables
5598 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5599 -- be set when the abstract states and variables need to be processed.
5601 -------------------------
5602 -- Build_Shadow_Entity --
5603 -------------------------
5605 procedure Build_Shadow_Entity
5606 (Ent : Entity_Id;
5607 Scop : Entity_Id;
5608 Shadow : out Entity_Id;
5609 Is_Tagged : Boolean := False)
5611 begin
5612 Shadow := Make_Temporary (Sloc (Ent), 'Z');
5614 -- The shadow entity must share the same name and parent as the
5615 -- entity it hides.
5617 Set_Chars (Shadow, Chars (Ent));
5618 Set_Parent (Shadow, Parent (Ent));
5620 -- The abstract view of a variable is a state, not another variable
5622 if Ekind (Ent) = E_Variable then
5623 Set_Ekind (Shadow, E_Abstract_State);
5624 else
5625 Set_Ekind (Shadow, Ekind (Ent));
5626 end if;
5628 Set_Is_Internal (Shadow);
5629 Set_From_Limited_With (Shadow);
5631 -- Add the new shadow entity to the limited view of the package
5633 Last_Shadow := Shadow;
5634 Append_Entity (Shadow, Shadow_Pack);
5636 -- Perform context-specific decoration of the shadow entity
5638 if Ekind (Ent) = E_Abstract_State then
5639 Decorate_State (Shadow, Scop);
5640 Set_Non_Limited_View (Shadow, Ent);
5642 elsif Ekind (Ent) = E_Package then
5643 Decorate_Package (Shadow, Scop);
5645 elsif Is_Type (Ent) then
5646 Decorate_Type (Shadow, Scop, Is_Tagged);
5647 Set_Non_Limited_View (Shadow, Ent);
5649 if Is_Tagged then
5650 Set_Non_Limited_View
5651 (Class_Wide_Type (Shadow), Class_Wide_Type (Ent));
5652 end if;
5654 if Is_Incomplete_Or_Private_Type (Ent) then
5655 Set_Private_Dependents (Shadow, New_Elmt_List);
5656 end if;
5658 elsif Ekind (Ent) = E_Variable then
5659 Decorate_State (Shadow, Scop);
5660 Set_Non_Limited_View (Shadow, Ent);
5661 end if;
5662 end Build_Shadow_Entity;
5664 ----------------------
5665 -- Decorate_Package --
5666 ----------------------
5668 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
5669 begin
5670 Set_Ekind (Ent, E_Package);
5671 Set_Etype (Ent, Standard_Void_Type);
5672 Set_Scope (Ent, Scop);
5673 end Decorate_Package;
5675 --------------------
5676 -- Decorate_State --
5677 --------------------
5679 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id) is
5680 begin
5681 Set_Ekind (Ent, E_Abstract_State);
5682 Set_Etype (Ent, Standard_Void_Type);
5683 Set_Scope (Ent, Scop);
5684 Set_Encapsulating_State (Ent, Empty);
5685 Set_Refinement_Constituents (Ent, New_Elmt_List);
5686 Set_Part_Of_Constituents (Ent, New_Elmt_List);
5687 end Decorate_State;
5689 -------------------
5690 -- Decorate_Type --
5691 -------------------
5693 procedure Decorate_Type
5694 (Ent : Entity_Id;
5695 Scop : Entity_Id;
5696 Is_Tagged : Boolean := False;
5697 Materialize : Boolean := False)
5699 CW_Typ : Entity_Id;
5701 begin
5702 -- An unanalyzed type or a shadow entity of a type is treated as an
5703 -- incomplete type.
5705 Set_Ekind (Ent, E_Incomplete_Type);
5706 Set_Etype (Ent, Ent);
5707 Set_Scope (Ent, Scop);
5708 Set_Is_First_Subtype (Ent);
5709 Set_Stored_Constraint (Ent, No_Elist);
5710 Set_Full_View (Ent, Empty);
5711 Init_Size_Align (Ent);
5713 -- A tagged type and its corresponding shadow entity share one common
5714 -- class-wide type. The list of primitive operations for the shadow
5715 -- entity is empty.
5717 if Is_Tagged then
5718 Set_Is_Tagged_Type (Ent);
5719 Set_Direct_Primitive_Operations (Ent, New_Elmt_List);
5721 CW_Typ :=
5722 New_External_Entity
5723 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
5725 Set_Class_Wide_Type (Ent, CW_Typ);
5727 -- Set parent to be the same as the parent of the tagged type.
5728 -- We need a parent field set, and it is supposed to point to
5729 -- the declaration of the type. The tagged type declaration
5730 -- essentially declares two separate types, the tagged type
5731 -- itself and the corresponding class-wide type, so it is
5732 -- reasonable for the parent fields to point to the declaration
5733 -- in both cases.
5735 Set_Parent (CW_Typ, Parent (Ent));
5737 Set_Ekind (CW_Typ, E_Class_Wide_Type);
5738 Set_Etype (CW_Typ, Ent);
5739 Set_Scope (CW_Typ, Scop);
5740 Set_Is_Tagged_Type (CW_Typ);
5741 Set_Is_First_Subtype (CW_Typ);
5742 Init_Size_Align (CW_Typ);
5743 Set_Has_Unknown_Discriminants (CW_Typ);
5744 Set_Class_Wide_Type (CW_Typ, CW_Typ);
5745 Set_Equivalent_Type (CW_Typ, Empty);
5746 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
5747 Set_Materialize_Entity (CW_Typ, Materialize);
5748 end if;
5749 end Decorate_Type;
5751 -----------------------
5752 -- Decorate_Variable --
5753 -----------------------
5755 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id) is
5756 begin
5757 Set_Ekind (Ent, E_Variable);
5758 Set_Etype (Ent, Standard_Void_Type);
5759 Set_Scope (Ent, Scop);
5760 end Decorate_Variable;
5762 -------------------------------------
5763 -- Process_Declarations_And_States --
5764 -------------------------------------
5766 procedure Process_Declarations_And_States
5767 (Pack : Entity_Id;
5768 Decls : List_Id;
5769 Scop : Entity_Id;
5770 Create_Abstract_Views : Boolean)
5772 procedure Find_And_Process_States;
5773 -- Determine whether package Pack defines abstract state either by
5774 -- using an aspect or a pragma. If this is the case, build shadow
5775 -- entities for all abstract states of Pack.
5777 procedure Process_States (States : Elist_Id);
5778 -- Generate shadow entities for all abstract states in list States
5780 -----------------------------
5781 -- Find_And_Process_States --
5782 -----------------------------
5784 procedure Find_And_Process_States is
5785 procedure Process_State (State : Node_Id);
5786 -- Generate shadow entities for a single abstract state or
5787 -- multiple states expressed as an aggregate.
5789 -------------------
5790 -- Process_State --
5791 -------------------
5793 procedure Process_State (State : Node_Id) is
5794 Loc : constant Source_Ptr := Sloc (State);
5795 Decl : Node_Id;
5796 Dummy : Entity_Id;
5797 Elmt : Node_Id;
5798 Id : Entity_Id;
5800 begin
5801 -- Multiple abstract states appear as an aggregate
5803 if Nkind (State) = N_Aggregate then
5804 Elmt := First (Expressions (State));
5805 while Present (Elmt) loop
5806 Process_State (Elmt);
5807 Next (Elmt);
5808 end loop;
5810 return;
5812 -- A null state has no abstract view
5814 elsif Nkind (State) = N_Null then
5815 return;
5817 -- State declaration with various options appears as an
5818 -- extension aggregate.
5820 elsif Nkind (State) = N_Extension_Aggregate then
5821 Decl := Ancestor_Part (State);
5823 -- Simple state declaration
5825 elsif Nkind (State) = N_Identifier then
5826 Decl := State;
5828 -- Possibly an illegal state declaration
5830 else
5831 return;
5832 end if;
5834 -- Abstract states are elaborated when the related pragma is
5835 -- elaborated. Since the withed package is not analyzed yet,
5836 -- the entities of the abstract states are not available. To
5837 -- overcome this complication, create the entities now and
5838 -- store them in their respective declarations. The entities
5839 -- are later used by routine Create_Abstract_State to declare
5840 -- and enter the states into visibility.
5842 if No (Entity (Decl)) then
5843 Id := Make_Defining_Identifier (Loc, Chars (Decl));
5845 Set_Entity (Decl, Id);
5846 Set_Parent (Id, State);
5847 Decorate_State (Id, Scop);
5849 -- Otherwise the package was previously withed
5851 else
5852 Id := Entity (Decl);
5853 end if;
5855 Build_Shadow_Entity (Id, Scop, Dummy);
5856 end Process_State;
5858 -- Local variables
5860 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack);
5861 Asp : Node_Id;
5862 Decl : Node_Id;
5864 -- Start of processing for Find_And_Process_States
5866 begin
5867 -- Find aspect Abstract_State
5869 Asp := First (Aspect_Specifications (Pack_Decl));
5870 while Present (Asp) loop
5871 if Chars (Identifier (Asp)) = Name_Abstract_State then
5872 Process_State (Expression (Asp));
5874 return;
5875 end if;
5877 Next (Asp);
5878 end loop;
5880 -- Find pragma Abstract_State by inspecting the declarations
5882 Decl := First (Decls);
5883 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
5884 if Pragma_Name (Decl) = Name_Abstract_State then
5885 Process_State
5886 (Get_Pragma_Arg
5887 (First (Pragma_Argument_Associations (Decl))));
5889 return;
5890 end if;
5892 Next (Decl);
5893 end loop;
5894 end Find_And_Process_States;
5896 --------------------
5897 -- Process_States --
5898 --------------------
5900 procedure Process_States (States : Elist_Id) is
5901 Dummy : Entity_Id;
5902 Elmt : Elmt_Id;
5904 begin
5905 Elmt := First_Elmt (States);
5906 while Present (Elmt) loop
5907 Build_Shadow_Entity (Node (Elmt), Scop, Dummy);
5909 Next_Elmt (Elmt);
5910 end loop;
5911 end Process_States;
5913 -- Local variables
5915 Is_Tagged : Boolean;
5916 Decl : Node_Id;
5917 Def : Node_Id;
5918 Def_Id : Entity_Id;
5919 Shadow : Entity_Id;
5921 -- Start of processing for Process_Declarations_And_States
5923 begin
5924 -- Build abstract views for all states defined in the package
5926 if Create_Abstract_Views then
5928 -- When a package has been analyzed, all states are stored in list
5929 -- Abstract_States. Generate the shadow entities directly.
5931 if Is_Analyzed then
5932 if Present (Abstract_States (Pack)) then
5933 Process_States (Abstract_States (Pack));
5934 end if;
5936 -- The package may declare abstract states by using an aspect or a
5937 -- pragma. Attempt to locate one of these construct and if found,
5938 -- build the shadow entities.
5940 else
5941 Find_And_Process_States;
5942 end if;
5943 end if;
5945 -- Inspect the declarative list, looking for nested packages, types
5946 -- and variable declarations.
5948 Decl := First (Decls);
5949 while Present (Decl) loop
5951 -- Packages
5953 if Nkind (Decl) = N_Package_Declaration then
5954 Def_Id := Defining_Entity (Decl);
5956 -- Perform minor decoration when the withed package has not
5957 -- been analyzed.
5959 if not Is_Analyzed then
5960 Decorate_Package (Def_Id, Scop);
5961 end if;
5963 -- Create a shadow entity that offers a limited view of all
5964 -- visible types declared within.
5966 Build_Shadow_Entity (Def_Id, Scop, Shadow);
5968 Process_Declarations_And_States
5969 (Pack => Def_Id,
5970 Decls => Visible_Declarations (Specification (Decl)),
5971 Scop => Shadow,
5972 Create_Abstract_Views => Create_Abstract_Views);
5974 -- Types
5976 elsif Nkind_In (Decl, N_Full_Type_Declaration,
5977 N_Incomplete_Type_Declaration,
5978 N_Private_Extension_Declaration,
5979 N_Private_Type_Declaration,
5980 N_Protected_Type_Declaration,
5981 N_Task_Type_Declaration)
5982 then
5983 Def_Id := Defining_Entity (Decl);
5985 -- Determine whether the type is tagged. Note that packages
5986 -- included via a limited with clause are not always analyzed,
5987 -- hence the tree lookup rather than the use of attribute
5988 -- Is_Tagged_Type.
5990 if Nkind (Decl) = N_Full_Type_Declaration then
5991 Def := Type_Definition (Decl);
5993 Is_Tagged :=
5994 (Nkind (Def) = N_Record_Definition
5995 and then Tagged_Present (Def))
5996 or else
5997 (Nkind (Def) = N_Derived_Type_Definition
5998 and then Present (Record_Extension_Part (Def)));
6000 elsif Nkind_In (Decl, N_Incomplete_Type_Declaration,
6001 N_Private_Type_Declaration)
6002 then
6003 Is_Tagged := Tagged_Present (Decl);
6005 elsif Nkind (Decl) = N_Private_Extension_Declaration then
6006 Is_Tagged := True;
6008 else
6009 Is_Tagged := False;
6010 end if;
6012 -- Perform minor decoration when the withed package has not
6013 -- been analyzed.
6015 if not Is_Analyzed then
6016 Decorate_Type (Def_Id, Scop, Is_Tagged, True);
6017 end if;
6019 -- Create a shadow entity that hides the type and offers an
6020 -- incomplete view of the said type.
6022 Build_Shadow_Entity (Def_Id, Scop, Shadow, Is_Tagged);
6024 -- Variables
6026 elsif Create_Abstract_Views
6027 and then Nkind (Decl) = N_Object_Declaration
6028 and then not Constant_Present (Decl)
6029 then
6030 Def_Id := Defining_Entity (Decl);
6032 -- Perform minor decoration when the withed package has not
6033 -- been analyzed.
6035 if not Is_Analyzed then
6036 Decorate_Variable (Def_Id, Scop);
6037 end if;
6039 -- Create a shadow entity that hides the variable and offers an
6040 -- abstract view of the said variable.
6042 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6043 end if;
6045 Next (Decl);
6046 end loop;
6047 end Process_Declarations_And_States;
6049 -- Local variables
6051 Nam : constant Node_Id := Name (N);
6052 Pack : constant Entity_Id := Cunit_Entity (Unum);
6054 Last_Public_Shadow : Entity_Id := Empty;
6055 Private_Shadow : Entity_Id;
6056 Spec : Node_Id;
6058 -- Start of processing for Build_Limited_Views
6060 begin
6061 pragma Assert (Limited_Present (N));
6063 -- A library_item mentioned in a limited_with_clause is a package
6064 -- declaration, not a subprogram declaration, generic declaration,
6065 -- generic instantiation, or package renaming declaration.
6067 case Nkind (Unit (Library_Unit (N))) is
6068 when N_Package_Declaration =>
6069 null;
6071 when N_Subprogram_Declaration =>
6072 Error_Msg_N ("subprograms not allowed in limited with_clauses", N);
6073 return;
6075 when N_Generic_Package_Declaration |
6076 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 accomodate 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)))
6186 then
6187 Error_Msg_Sloc := Sloc (No_Elab_Code_All_Pragma);
6188 Error_Msg_N
6189 ("violation of No_Elaboration_Code_All#", CI);
6190 Error_Msg_NE
6191 ("\unit& does not have No_Elaboration_Code_All",
6192 CI, Entity (Name (CI)));
6193 end if;
6195 Next (CI);
6196 end loop;
6197 end;
6198 end if;
6199 end Check_No_Elab_Code_All;
6201 -------------------------------
6202 -- Check_Body_Needed_For_SAL --
6203 -------------------------------
6205 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
6207 function Entity_Needs_Body (E : Entity_Id) return Boolean;
6208 -- Determine whether use of entity E might require the presence of its
6209 -- body. For a package this requires a recursive traversal of all nested
6210 -- declarations.
6212 ---------------------------
6213 -- Entity_Needed_For_SAL --
6214 ---------------------------
6216 function Entity_Needs_Body (E : Entity_Id) return Boolean is
6217 Ent : Entity_Id;
6219 begin
6220 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
6221 return True;
6223 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
6224 return True;
6226 elsif Ekind (E) = E_Generic_Package
6227 and then
6228 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
6229 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6230 then
6231 return True;
6233 elsif Ekind (E) = E_Package
6234 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
6235 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6236 then
6237 Ent := First_Entity (E);
6238 while Present (Ent) loop
6239 if Entity_Needs_Body (Ent) then
6240 return True;
6241 end if;
6243 Next_Entity (Ent);
6244 end loop;
6246 return False;
6248 else
6249 return False;
6250 end if;
6251 end Entity_Needs_Body;
6253 -- Start of processing for Check_Body_Needed_For_SAL
6255 begin
6256 if Ekind (Unit_Name) = E_Generic_Package
6257 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6258 N_Generic_Package_Declaration
6259 and then
6260 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
6261 then
6262 Set_Body_Needed_For_SAL (Unit_Name);
6264 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
6265 Set_Body_Needed_For_SAL (Unit_Name);
6267 elsif Is_Subprogram (Unit_Name)
6268 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6269 N_Subprogram_Declaration
6270 and then Has_Pragma_Inline (Unit_Name)
6271 then
6272 Set_Body_Needed_For_SAL (Unit_Name);
6274 elsif Ekind (Unit_Name) = E_Subprogram_Body then
6275 Check_Body_Needed_For_SAL
6276 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6278 elsif Ekind (Unit_Name) = E_Package
6279 and then Entity_Needs_Body (Unit_Name)
6280 then
6281 Set_Body_Needed_For_SAL (Unit_Name);
6283 elsif Ekind (Unit_Name) = E_Package_Body
6284 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
6285 then
6286 Check_Body_Needed_For_SAL
6287 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6288 end if;
6289 end Check_Body_Needed_For_SAL;
6291 --------------------
6292 -- Remove_Context --
6293 --------------------
6295 procedure Remove_Context (N : Node_Id) is
6296 Lib_Unit : constant Node_Id := Unit (N);
6298 begin
6299 -- If this is a child unit, first remove the parent units
6301 if Is_Child_Spec (Lib_Unit) then
6302 Remove_Parents (Lib_Unit);
6303 end if;
6305 Remove_Context_Clauses (N);
6306 end Remove_Context;
6308 ----------------------------
6309 -- Remove_Context_Clauses --
6310 ----------------------------
6312 procedure Remove_Context_Clauses (N : Node_Id) is
6313 Item : Node_Id;
6314 Unit_Name : Entity_Id;
6316 begin
6317 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6318 -- limited-views first and regular-views later (to maintain the
6319 -- stack model).
6321 -- First Phase: Remove limited_with context clauses
6323 Item := First (Context_Items (N));
6324 while Present (Item) loop
6326 -- We are interested only in with clauses which got installed
6327 -- on entry.
6329 if Nkind (Item) = N_With_Clause
6330 and then Limited_Present (Item)
6331 and then Limited_View_Installed (Item)
6332 then
6333 Remove_Limited_With_Clause (Item);
6334 end if;
6336 Next (Item);
6337 end loop;
6339 -- Second Phase: Loop through context items and undo regular
6340 -- with_clauses and use_clauses.
6342 Item := First (Context_Items (N));
6343 while Present (Item) loop
6345 -- We are interested only in with clauses which got installed on
6346 -- entry, as indicated by their Context_Installed flag set
6348 if Nkind (Item) = N_With_Clause
6349 and then Limited_Present (Item)
6350 and then Limited_View_Installed (Item)
6351 then
6352 null;
6354 elsif Nkind (Item) = N_With_Clause
6355 and then Context_Installed (Item)
6356 then
6357 -- Remove items from one with'ed unit
6359 Unit_Name := Entity (Name (Item));
6360 Remove_Unit_From_Visibility (Unit_Name);
6361 Set_Context_Installed (Item, False);
6363 elsif Nkind (Item) = N_Use_Package_Clause then
6364 End_Use_Package (Item);
6366 elsif Nkind (Item) = N_Use_Type_Clause then
6367 End_Use_Type (Item);
6368 end if;
6370 Next (Item);
6371 end loop;
6372 end Remove_Context_Clauses;
6374 --------------------------------
6375 -- Remove_Limited_With_Clause --
6376 --------------------------------
6378 procedure Remove_Limited_With_Clause (N : Node_Id) is
6379 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
6380 E : Entity_Id;
6381 P : Entity_Id;
6382 Lim_Header : Entity_Id;
6383 Lim_Typ : Entity_Id;
6384 Prev : Entity_Id;
6386 begin
6387 pragma Assert (Limited_View_Installed (N));
6389 -- In case of limited with_clause on subprograms, generics, instances,
6390 -- or renamings, the corresponding error was previously posted and we
6391 -- have nothing to do here.
6393 if Nkind (P_Unit) /= N_Package_Declaration then
6394 return;
6395 end if;
6397 P := Defining_Unit_Name (Specification (P_Unit));
6399 -- Handle child packages
6401 if Nkind (P) = N_Defining_Program_Unit_Name then
6402 P := Defining_Identifier (P);
6403 end if;
6405 if Debug_Flag_I then
6406 Write_Str ("remove limited view of ");
6407 Write_Name (Chars (P));
6408 Write_Str (" from visibility");
6409 Write_Eol;
6410 end if;
6412 -- Prepare the removal of the shadow entities from visibility. The first
6413 -- element of the limited view is a header (an E_Package entity) that is
6414 -- used to reference the first shadow entity in the private part of the
6415 -- package
6417 Lim_Header := Limited_View (P);
6418 Lim_Typ := First_Entity (Lim_Header);
6420 -- Remove package and shadow entities from visibility if it has not
6421 -- been analyzed
6423 if not Analyzed (P_Unit) then
6424 Unchain (P);
6425 Set_Is_Immediately_Visible (P, False);
6427 while Present (Lim_Typ) loop
6428 Unchain (Lim_Typ);
6429 Next_Entity (Lim_Typ);
6430 end loop;
6432 -- Otherwise this package has already appeared in the closure and its
6433 -- shadow entities must be replaced by its real entities. This code
6434 -- must be kept synchronized with the complementary code in Install
6435 -- Limited_Withed_Unit.
6437 else
6438 -- Real entities that are type or subtype declarations were hidden
6439 -- from visibility at the point of installation of the limited-view.
6440 -- Now we recover the previous value of the hidden attribute.
6442 E := First_Entity (P);
6443 while Present (E) and then E /= First_Private_Entity (P) loop
6444 if Is_Type (E) then
6445 Set_Is_Hidden (E, Was_Hidden (E));
6446 end if;
6448 Next_Entity (E);
6449 end loop;
6451 while Present (Lim_Typ)
6452 and then Lim_Typ /= First_Private_Entity (Lim_Header)
6453 loop
6454 -- Nested packages and child units were not unchained
6456 if Ekind (Lim_Typ) /= E_Package
6457 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
6458 then
6459 -- If the package has incomplete types, the limited view of the
6460 -- incomplete type is in fact never visible (AI05-129) but we
6461 -- have created a shadow entity E1 for it, that points to E2,
6462 -- a non-limited incomplete type. This in turn has a full view
6463 -- E3 that is the full declaration. There is a corresponding
6464 -- shadow entity E4. When reinstalling the non-limited view,
6465 -- E2 must become the current entity and E3 must be ignored.
6467 E := Non_Limited_View (Lim_Typ);
6469 if Present (Current_Entity (E))
6470 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
6471 and then Full_View (Current_Entity (E)) = E
6472 then
6474 -- Lim_Typ is the limited view of a full type declaration
6475 -- that has a previous incomplete declaration, i.e. E3 from
6476 -- the previous description. Nothing to insert.
6478 null;
6480 else
6481 pragma Assert (not In_Chain (E));
6483 Prev := Current_Entity (Lim_Typ);
6485 if Prev = Lim_Typ then
6486 Set_Current_Entity (E);
6488 else
6489 while Present (Prev)
6490 and then Homonym (Prev) /= Lim_Typ
6491 loop
6492 Prev := Homonym (Prev);
6493 end loop;
6495 if Present (Prev) then
6496 Set_Homonym (Prev, E);
6497 end if;
6498 end if;
6500 -- Preserve structure of homonym chain
6502 Set_Homonym (E, Homonym (Lim_Typ));
6503 end if;
6504 end if;
6506 Next_Entity (Lim_Typ);
6507 end loop;
6508 end if;
6510 -- Indicate that the limited view of the package is not installed
6512 Set_From_Limited_With (P, False);
6513 Set_Limited_View_Installed (N, False);
6514 end Remove_Limited_With_Clause;
6516 --------------------
6517 -- Remove_Parents --
6518 --------------------
6520 procedure Remove_Parents (Lib_Unit : Node_Id) is
6521 P : Node_Id;
6522 P_Name : Entity_Id;
6523 P_Spec : Node_Id := Empty;
6524 E : Entity_Id;
6525 Vis : constant Boolean :=
6526 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
6528 begin
6529 if Is_Child_Spec (Lib_Unit) then
6530 P_Spec := Parent_Spec (Lib_Unit);
6532 elsif Nkind (Lib_Unit) = N_Package_Body
6533 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6534 then
6535 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6536 end if;
6538 if Present (P_Spec) then
6539 P := Unit (P_Spec);
6540 P_Name := Get_Parent_Entity (P);
6541 Remove_Context_Clauses (P_Spec);
6542 End_Package_Scope (P_Name);
6543 Set_Is_Immediately_Visible (P_Name, Vis);
6545 -- Remove from visibility the siblings as well, which are directly
6546 -- visible while the parent is in scope.
6548 E := First_Entity (P_Name);
6549 while Present (E) loop
6550 if Is_Child_Unit (E) then
6551 Set_Is_Immediately_Visible (E, False);
6552 end if;
6554 Next_Entity (E);
6555 end loop;
6557 Set_In_Package_Body (P_Name, False);
6559 -- This is the recursive call to remove the context of any higher
6560 -- level parent. This recursion ensures that all parents are removed
6561 -- in the reverse order of their installation.
6563 Remove_Parents (P);
6564 end if;
6565 end Remove_Parents;
6567 ---------------------------------
6568 -- Remove_Private_With_Clauses --
6569 ---------------------------------
6571 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6572 Item : Node_Id;
6574 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6575 -- Check whether a given unit appears in a regular with_clause. Used to
6576 -- determine whether a private_with_clause, implicit or explicit, should
6577 -- be ignored.
6579 ----------------------------
6580 -- In_Regular_With_Clause --
6581 ----------------------------
6583 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6585 Item : Node_Id;
6587 begin
6588 Item := First (Context_Items (Comp_Unit));
6589 while Present (Item) loop
6590 if Nkind (Item) = N_With_Clause
6592 -- The following guard is needed to ensure that the name has
6593 -- been properly analyzed before we go fetching its entity.
6595 and then Is_Entity_Name (Name (Item))
6596 and then Entity (Name (Item)) = E
6597 and then not Private_Present (Item)
6598 then
6599 return True;
6600 end if;
6601 Next (Item);
6602 end loop;
6604 return False;
6605 end In_Regular_With_Clause;
6607 -- Start of processing for Remove_Private_With_Clauses
6609 begin
6610 Item := First (Context_Items (Comp_Unit));
6611 while Present (Item) loop
6612 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
6614 -- If private_with_clause is redundant, remove it from context,
6615 -- as a small optimization to subsequent handling of private_with
6616 -- clauses in other nested packages.
6618 if In_Regular_With_Clause (Entity (Name (Item))) then
6619 declare
6620 Nxt : constant Node_Id := Next (Item);
6621 begin
6622 Remove (Item);
6623 Item := Nxt;
6624 end;
6626 elsif Limited_Present (Item) then
6627 if not Limited_View_Installed (Item) then
6628 Remove_Limited_With_Clause (Item);
6629 end if;
6631 Next (Item);
6633 else
6634 Remove_Unit_From_Visibility (Entity (Name (Item)));
6635 Set_Context_Installed (Item, False);
6636 Next (Item);
6637 end if;
6639 else
6640 Next (Item);
6641 end if;
6642 end loop;
6643 end Remove_Private_With_Clauses;
6645 ---------------------------------
6646 -- Remove_Unit_From_Visibility --
6647 ---------------------------------
6649 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6650 begin
6651 if Debug_Flag_I then
6652 Write_Str ("remove unit ");
6653 Write_Name (Chars (Unit_Name));
6654 Write_Str (" from visibility");
6655 Write_Eol;
6656 end if;
6658 Set_Is_Visible_Lib_Unit (Unit_Name, False);
6659 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6660 Set_Is_Immediately_Visible (Unit_Name, False);
6662 -- If the unit is a wrapper package, the subprogram instance is
6663 -- what must be removed from visibility.
6664 -- Should we use Related_Instance instead???
6666 if Is_Wrapper_Package (Unit_Name) then
6667 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
6668 end if;
6669 end Remove_Unit_From_Visibility;
6671 --------
6672 -- sm --
6673 --------
6675 procedure sm is
6676 begin
6677 null;
6678 end sm;
6680 -------------
6681 -- Unchain --
6682 -------------
6684 procedure Unchain (E : Entity_Id) is
6685 Prev : Entity_Id;
6687 begin
6688 Prev := Current_Entity (E);
6690 if No (Prev) then
6691 return;
6693 elsif Prev = E then
6694 Set_Name_Entity_Id (Chars (E), Homonym (E));
6696 else
6697 while Present (Prev) and then Homonym (Prev) /= E loop
6698 Prev := Homonym (Prev);
6699 end loop;
6701 if Present (Prev) then
6702 Set_Homonym (Prev, Homonym (E));
6703 end if;
6704 end if;
6706 if Debug_Flag_I then
6707 Write_Str (" (homonym) unchain ");
6708 Write_Name (Chars (E));
6709 Write_Eol;
6710 end if;
6711 end Unchain;
6713 end Sem_Ch10;