PR target/79080
[official-gcc.git] / gcc / ada / sem_ch10.adb
blobc1f671fb43a99b36b7911c1b086f6c8c58046ed3
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 0 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Exp_Util; use Exp_Util;
33 with Elists; use Elists;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Impunit; use Impunit;
38 with Inline; use Inline;
39 with Lib; use Lib;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Output; use Output;
47 with Par_SCO; use Par_SCO;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch7; use Sem_Ch7;
56 with Sem_Ch8; use Sem_Ch8;
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 Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
88 -- Common processing for all stubs (subprograms, tasks, packages, and
89 -- protected cases). N is the stub to be analyzed. Once the subunit name
90 -- is established, load and analyze. Nam is the non-overloadable entity
91 -- for which the proper body provides a completion. Subprogram stubs are
92 -- handled differently because they can be declarations.
94 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
95 -- Check whether the source for the body of a compilation unit must be
96 -- included in a standalone library.
98 procedure Check_No_Elab_Code_All (N : Node_Id);
99 -- Carries out possible tests for violation of No_Elab_Code all for withed
100 -- units in the Context_Items of unit N.
102 procedure Check_Private_Child_Unit (N : Node_Id);
103 -- If a with_clause mentions a private child unit, the compilation unit
104 -- must be a member of the same family, as described in 10.1.2.
106 procedure Check_Stub_Level (N : Node_Id);
107 -- Verify that a stub is declared immediately within a compilation unit,
108 -- and not in an inner frame.
110 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
111 -- When a child unit appears in a context clause, the implicit withs on
112 -- parents are made explicit, and with clauses are inserted in the context
113 -- clause before the one for the child. If a parent in the with_clause
114 -- is a renaming, the implicit with_clause is on the renaming whose name
115 -- is mentioned in the with_clause, and not on the package it renames.
116 -- N is the compilation unit whose list of context items receives the
117 -- implicit with_clauses.
119 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
120 -- Generate cross-reference information for the parents of child units
121 -- and of subunits. N is a defining_program_unit_name, and P_Id is the
122 -- immediate parent scope.
124 function Has_With_Clause
125 (C_Unit : Node_Id;
126 Pack : Entity_Id;
127 Is_Limited : Boolean := False) return Boolean;
128 -- Determine whether compilation unit C_Unit contains a [limited] with
129 -- clause for package Pack. Use the flag Is_Limited to designate desired
130 -- clause kind.
132 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
133 -- If the main unit is a child unit, implicit withs are also added for
134 -- all its ancestors.
136 function In_Chain (E : Entity_Id) return Boolean;
137 -- Check that the shadow entity is not already in the homonym chain, for
138 -- example through a limited_with clause in a parent unit.
140 procedure Install_Context_Clauses (N : Node_Id);
141 -- Subsidiary to Install_Context and Install_Parents. Process all with
142 -- and use clauses for current unit and its library unit if any.
144 procedure Install_Limited_Context_Clauses (N : Node_Id);
145 -- Subsidiary to Install_Context. Process only limited with_clauses for
146 -- current unit. Implements Ada 2005 (AI-50217).
148 procedure Install_Limited_Withed_Unit (N : Node_Id);
149 -- Place shadow entities for a limited_with package in the visibility
150 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
152 procedure Install_Withed_Unit
153 (With_Clause : Node_Id;
154 Private_With_OK : Boolean := False);
155 -- If the unit is not a child unit, make unit immediately visible. The
156 -- caller ensures that the unit is not already currently installed. The
157 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
158 -- is called when compiling the private part of a package, or installing
159 -- the private declarations of a parent unit.
161 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
162 -- This procedure establishes the context for the compilation of a child
163 -- unit. If Lib_Unit is a child library spec then the context of the parent
164 -- is installed, and the parent itself made immediately visible, so that
165 -- the child unit is processed in the declarative region of the parent.
166 -- Install_Parents makes a recursive call to itself to ensure that all
167 -- parents are loaded in the nested case. If Lib_Unit is a library body,
168 -- the only effect of Install_Parents is to install the private decls of
169 -- the parents, because the visible parent declarations will have been
170 -- installed as part of the context of the corresponding spec.
172 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
173 -- In the compilation of a child unit, a child of any of the ancestor
174 -- units is directly visible if it is visible, because the parent is in
175 -- an enclosing scope. Iterate over context to find child units of U_Name
176 -- or of some ancestor of it.
178 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
179 -- When compiling a unit Q descended from some parent unit P, a limited
180 -- with_clause in the context of P that names some other ancestor of Q
181 -- must not be installed because the ancestor is immediately visible.
183 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
184 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
185 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
186 -- a library spec that has a parent. If the call to Is_Child_Spec returns
187 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
188 -- compilation unit for the parent spec.
190 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
191 -- Parent_Spec is non-empty, this is also a child unit.
193 procedure Remove_Context_Clauses (N : Node_Id);
194 -- Subsidiary of previous one. Remove use_ and with_clauses
196 procedure Remove_Limited_With_Clause (N : Node_Id);
197 -- Remove from visibility the shadow entities introduced for a package
198 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
200 procedure Remove_Parents (Lib_Unit : Node_Id);
201 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
202 -- contexts established by the corresponding call to Install_Parents are
203 -- removed. Remove_Parents contains a recursive call to itself to ensure
204 -- that all parents are removed in the nested case.
206 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
207 -- Reset all visibility flags on unit after compiling it, either as a main
208 -- unit or as a unit in the context.
210 procedure Unchain (E : Entity_Id);
211 -- Remove single entity from visibility list
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 : out Boolean;
273 Used_Type_Or_Elab : 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 : out Boolean;
283 Withed : 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 : out Boolean;
302 Used_Type_Or_Elab : 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_Unmapped (Cont_Item),
397 Name_Elaborate, 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 : out Boolean;
423 Withed : 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;
519 Used_In_Spec : Boolean;
520 Used_Type_Or_Elab : Boolean;
521 Withed_In_Spec : Boolean;
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?r?", 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?r?", 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 descendants) 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 -- Note: We copy the Context_Items from the explicit body
787 -- to the implicit spec, setting the former to Empty_List
788 -- to preserve the treeish nature of the tree, during
789 -- analysis of the spec. Then we put it back the way it
790 -- was -- copy the Context_Items from the spec to the
791 -- body, and set the spec Context_Items to Empty_List.
792 -- It is necessary to preserve the treeish nature,
793 -- because otherwise we will call End_Use_* twice on the
794 -- same thing.
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_Context_Items (N, Empty_List);
808 Set_Library_Unit (N, Lib_Unit);
809 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
810 Make_Child_Decl_Unit (N);
811 Semantics (Lib_Unit);
813 -- Now that a separate declaration exists, the body
814 -- of the child unit does not act as spec any longer.
816 Set_Acts_As_Spec (N, False);
817 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
818 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
819 Set_Comes_From_Source_Default (SCS);
821 -- Restore Context_Items to the body
823 Set_Context_Items (N, Context_Items (Lib_Unit));
824 Set_Context_Items (Lib_Unit, Empty_List);
825 end;
826 end if;
827 end if;
829 -- Here for subprogram with separate declaration
831 else
832 Semantics (Lib_Unit);
833 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
834 Version_Update (N, Lib_Unit);
835 end if;
837 -- If this is a child unit, generate references to the parents
839 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
840 N_Defining_Program_Unit_Name
841 then
842 Generate_Parent_References
843 (Specification (Unit_Node),
844 Scope (Defining_Entity (Unit (Lib_Unit))));
845 end if;
846 end if;
848 -- If it is a child unit, the parent must be elaborated first and we
849 -- update version, since we are dependent on our parent.
851 if Is_Child_Spec (Unit_Node) then
853 -- The analysis of the parent is done with style checks off
855 declare
856 Save_Style_Check : constant Boolean := Style_Check;
858 begin
859 if not GNAT_Mode then
860 Style_Check := False;
861 end if;
863 Semantics (Parent_Spec (Unit_Node));
864 Version_Update (N, Parent_Spec (Unit_Node));
866 -- Restore style check settings
868 Style_Check := Save_Style_Check;
869 end;
870 end if;
872 -- With the analysis done, install the context. Note that we can't
873 -- install the context from the with clauses as we analyze them, because
874 -- each with clause must be analyzed in a clean visibility context, so
875 -- we have to wait and install them all at once.
877 Install_Context (N);
879 if Is_Child_Spec (Unit_Node) then
881 -- Set the entities of all parents in the program_unit_name
883 Generate_Parent_References
884 (Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
885 end if;
887 -- All components of the context: with-clauses, library unit, ancestors
888 -- if any, (and their context) are analyzed and installed.
890 -- Call special debug routine sm if this is the main unit
892 if Current_Sem_Unit = Main_Unit then
894 end if;
896 -- Now analyze the unit (package, subprogram spec, body) itself
898 Analyze (Unit_Node);
900 if Warn_On_Redundant_Constructs then
901 Check_Redundant_Withs (Context_Items (N));
903 if Nkind (Unit_Node) = N_Package_Body then
904 Check_Redundant_Withs
905 (Context_Items => Context_Items (N),
906 Spec_Context_Items => Context_Items (Lib_Unit));
907 end if;
908 end if;
910 -- The above call might have made Unit_Node an N_Subprogram_Body from
911 -- something else, so propagate any Acts_As_Spec flag.
913 if Nkind (Unit_Node) = N_Subprogram_Body
914 and then Acts_As_Spec (Unit_Node)
915 then
916 Set_Acts_As_Spec (N);
917 end if;
919 -- Register predefined units in Rtsfind
921 declare
922 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
923 begin
924 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
925 Set_RTU_Loaded (Unit_Node);
926 end if;
927 end;
929 -- Treat compilation unit pragmas that appear after the library unit
931 if Present (Pragmas_After (Aux_Decls_Node (N))) then
932 declare
933 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
934 begin
935 while Present (Prag_Node) loop
936 Analyze (Prag_Node);
937 Next (Prag_Node);
938 end loop;
939 end;
940 end if;
942 -- Analyze the contract of a [generic] subprogram that acts as a
943 -- compilation unit after all compilation pragmas have been analyzed.
945 if Nkind_In (Unit_Node, N_Generic_Subprogram_Declaration,
946 N_Subprogram_Declaration)
947 then
948 Analyze_Entry_Or_Subprogram_Contract (Defining_Entity (Unit_Node));
949 end if;
951 -- Generate distribution stubs if requested and no error
953 if N = Main_Cunit
954 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
955 or else
956 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
957 and then Fatal_Error (Main_Unit) /= Error_Detected
958 then
959 if Is_RCI_Pkg_Spec_Or_Body (N) then
961 -- Regular RCI package
963 Add_Stub_Constructs (N);
965 elsif (Nkind (Unit_Node) = N_Package_Declaration
966 and then Is_Shared_Passive (Defining_Entity
967 (Specification (Unit_Node))))
968 or else (Nkind (Unit_Node) = N_Package_Body
969 and then
970 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
971 then
972 -- Shared passive package
974 Add_Stub_Constructs (N);
976 elsif Nkind (Unit_Node) = N_Package_Instantiation
977 and then
978 Is_Remote_Call_Interface
979 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
980 then
981 -- Instantiation of a RCI generic package
983 Add_Stub_Constructs (N);
984 end if;
985 end if;
987 -- Remove unit from visibility, so that environment is clean for the
988 -- next compilation, which is either the main unit or some other unit
989 -- in the context.
991 if Nkind_In (Unit_Node, N_Package_Declaration,
992 N_Package_Renaming_Declaration,
993 N_Subprogram_Declaration)
994 or else Nkind (Unit_Node) in N_Generic_Declaration
995 or else (Nkind (Unit_Node) = N_Subprogram_Body
996 and then Acts_As_Spec (Unit_Node))
997 then
998 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
1000 -- If the unit is an instantiation whose body will be elaborated for
1001 -- inlining purposes, use the proper entity of the instance. The entity
1002 -- may be missing if the instantiation was illegal.
1004 elsif Nkind (Unit_Node) = N_Package_Instantiation
1005 and then not Error_Posted (Unit_Node)
1006 and then Present (Instance_Spec (Unit_Node))
1007 then
1008 Remove_Unit_From_Visibility
1009 (Defining_Entity (Instance_Spec (Unit_Node)));
1011 elsif Nkind (Unit_Node) = N_Package_Body
1012 or else (Nkind (Unit_Node) = N_Subprogram_Body
1013 and then not Acts_As_Spec (Unit_Node))
1014 then
1015 -- Bodies that are not the main unit are compiled if they are generic
1016 -- or contain generic or inlined units. Their analysis brings in the
1017 -- context of the corresponding spec (unit declaration) which must be
1018 -- removed as well, to return the compilation environment to its
1019 -- proper state.
1021 Remove_Context (Lib_Unit);
1022 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1023 end if;
1025 -- Last step is to deinstall the context we just installed as well as
1026 -- the unit just compiled.
1028 Remove_Context (N);
1030 -- When generating code for a non-generic main unit, check that withed
1031 -- generic units have a body if they need it, even if the units have not
1032 -- been instantiated. Force the load of the bodies to produce the proper
1033 -- error if the body is absent. The same applies to GNATprove mode, with
1034 -- the added benefit of capturing global references within the generic.
1035 -- This in turn allows for proper inlining of subprogram bodies without
1036 -- a previous declaration.
1038 if Get_Cunit_Unit_Number (N) = Main_Unit
1039 and then ((Operating_Mode = Generate_Code and then Expander_Active)
1040 or else
1041 (Operating_Mode = Check_Semantics and then GNATprove_Mode))
1042 then
1043 -- Check whether the source for the body of the unit must be included
1044 -- in a standalone library.
1046 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1048 -- Indicate that the main unit is now analyzed, to catch possible
1049 -- circularities between it and generic bodies. Remove main unit from
1050 -- visibility. This might seem superfluous, but the main unit must
1051 -- not be visible in the generic body expansions that follow.
1053 Set_Analyzed (N, True);
1054 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1056 declare
1057 Item : Node_Id;
1058 Nam : Entity_Id;
1059 Un : Unit_Number_Type;
1061 Save_Style_Check : constant Boolean := Style_Check;
1063 begin
1064 Item := First (Context_Items (N));
1065 while Present (Item) loop
1067 -- Check for explicit with clause
1069 if Nkind (Item) = N_With_Clause
1070 and then not Implicit_With (Item)
1072 -- Ada 2005 (AI-50217): Ignore limited-withed units
1074 and then not Limited_Present (Item)
1075 then
1076 Nam := Entity (Name (Item));
1078 -- Compile the generic subprogram, unless it is intrinsic or
1079 -- imported so no body is required, or generic package body
1080 -- if the package spec requires a body.
1082 if (Is_Generic_Subprogram (Nam)
1083 and then not Is_Intrinsic_Subprogram (Nam)
1084 and then not Is_Imported (Nam))
1085 or else (Ekind (Nam) = E_Generic_Package
1086 and then Unit_Requires_Body (Nam))
1087 then
1088 Style_Check := False;
1090 if Present (Renamed_Object (Nam)) then
1091 Un :=
1092 Load_Unit
1093 (Load_Name =>
1094 Get_Body_Name
1095 (Get_Unit_Name
1096 (Unit_Declaration_Node
1097 (Renamed_Object (Nam)))),
1098 Required => False,
1099 Subunit => False,
1100 Error_Node => N,
1101 Renamings => True);
1102 else
1103 Un :=
1104 Load_Unit
1105 (Load_Name =>
1106 Get_Body_Name (Get_Unit_Name (Item)),
1107 Required => False,
1108 Subunit => False,
1109 Error_Node => N,
1110 Renamings => True);
1111 end if;
1113 if Un = No_Unit then
1114 Error_Msg_NE
1115 ("body of generic unit& not found", Item, Nam);
1116 exit;
1118 elsif not Analyzed (Cunit (Un))
1119 and then Un /= Main_Unit
1120 and then Fatal_Error (Un) /= Error_Detected
1121 then
1122 Style_Check := False;
1123 Semantics (Cunit (Un));
1124 end if;
1125 end if;
1126 end if;
1128 Next (Item);
1129 end loop;
1131 -- Restore style checks settings
1133 Style_Check := Save_Style_Check;
1134 end;
1135 end if;
1137 -- Deal with creating elaboration counter if needed. We create an
1138 -- elaboration counter only for units that come from source since
1139 -- units manufactured by the compiler never need elab checks.
1141 if Comes_From_Source (N)
1142 and then Nkind_In (Unit_Node, N_Package_Declaration,
1143 N_Generic_Package_Declaration,
1144 N_Subprogram_Declaration,
1145 N_Generic_Subprogram_Declaration)
1146 then
1147 declare
1148 Loc : constant Source_Ptr := Sloc (N);
1149 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1151 begin
1152 Spec_Id := Defining_Entity (Unit_Node);
1153 Generate_Definition (Spec_Id);
1155 -- See if an elaboration entity is required for possible access
1156 -- before elaboration checking. Note that we must allow for this
1157 -- even if -gnatE is not set, since a client may be compiled in
1158 -- -gnatE mode and reference the entity.
1160 -- These entities are also used by the binder to prevent multiple
1161 -- attempts to execute the elaboration code for the library case
1162 -- where the elaboration routine might otherwise be called more
1163 -- than once.
1165 -- Case of units which do not require elaboration checks
1168 -- Pure units do not need checks
1170 Is_Pure (Spec_Id)
1172 -- Preelaborated units do not need checks
1174 or else Is_Preelaborated (Spec_Id)
1176 -- No checks needed if pragma Elaborate_Body present
1178 or else Has_Pragma_Elaborate_Body (Spec_Id)
1180 -- No checks needed if unit does not require a body
1182 or else not Unit_Requires_Body (Spec_Id)
1184 -- No checks needed for predefined files
1186 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1188 -- No checks required if no separate spec
1190 or else Acts_As_Spec (N)
1191 then
1192 -- This is a case where we only need the entity for
1193 -- checking to prevent multiple elaboration checks.
1195 Set_Elaboration_Entity_Required (Spec_Id, False);
1197 -- Case of elaboration entity is required for access before
1198 -- elaboration checking (so certainly we must build it).
1200 else
1201 Set_Elaboration_Entity_Required (Spec_Id, True);
1202 end if;
1204 Build_Elaboration_Entity (N, Spec_Id);
1205 end;
1206 end if;
1208 -- Freeze the compilation unit entity. This for sure is needed because
1209 -- of some warnings that can be output (see Freeze_Subprogram), but may
1210 -- in general be required. If freezing actions result, place them in the
1211 -- compilation unit actions list, and analyze them.
1213 declare
1214 L : constant List_Id :=
1215 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1216 begin
1217 while Is_Non_Empty_List (L) loop
1218 Insert_Library_Level_Action (Remove_Head (L));
1219 end loop;
1220 end;
1222 Set_Analyzed (N);
1224 -- Call Check_Package_Body so that a body containing subprograms with
1225 -- Inline_Always can be made available for front end inlining.
1227 if Nkind (Unit_Node) = N_Package_Declaration
1228 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1230 -- We don't need to do this if the Expander is not active, since there
1231 -- is no code to inline.
1233 and then Expander_Active
1234 then
1235 declare
1236 Save_Style_Check : constant Boolean := Style_Check;
1237 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1238 Options : Style_Check_Options;
1240 begin
1241 Save_Style_Check_Options (Options);
1242 Reset_Style_Check_Options;
1243 Opt.Warning_Mode := Suppress;
1245 Check_Package_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1247 Reset_Style_Check_Options;
1248 Set_Style_Check_Options (Options);
1249 Style_Check := Save_Style_Check;
1250 Warning_Mode := Save_Warning;
1251 end;
1252 end if;
1254 -- If we are generating obsolescent warnings, then here is where we
1255 -- generate them for the with'ed items. The reason for this special
1256 -- processing is that the normal mechanism of generating the warnings
1257 -- for referenced entities does not work for context clause references.
1258 -- That's because when we first analyze the context, it is too early to
1259 -- know if the with'ing unit is itself obsolescent (which suppresses
1260 -- the warnings).
1262 if not GNAT_Mode
1263 and then Warn_On_Obsolescent_Feature
1264 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1265 then
1266 -- Push current compilation unit as scope, so that the test for
1267 -- being within an obsolescent unit will work correctly. The check
1268 -- is not performed within an instantiation, because the warning
1269 -- will have been emitted in the corresponding generic unit.
1271 Push_Scope (Defining_Entity (Unit_Node));
1273 -- Loop through context items to deal with with clauses
1275 declare
1276 Item : Node_Id;
1277 Nam : Node_Id;
1278 Ent : Entity_Id;
1280 begin
1281 Item := First (Context_Items (N));
1282 while Present (Item) loop
1283 if Nkind (Item) = N_With_Clause
1285 -- Suppress this check in limited-withed units. Further work
1286 -- needed here if we decide to incorporate this check on
1287 -- limited-withed units.
1289 and then not Limited_Present (Item)
1290 then
1291 Nam := Name (Item);
1292 Ent := Entity (Nam);
1294 if Is_Obsolescent (Ent) then
1295 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1296 end if;
1297 end if;
1299 Next (Item);
1300 end loop;
1301 end;
1303 -- Remove temporary install of current unit as scope
1305 Pop_Scope;
1306 end if;
1308 -- If No_Elaboration_Code_All was encountered, this is where we do the
1309 -- transitive test of with'ed units to make sure they have the aspect.
1310 -- This is delayed till the end of analyzing the compilation unit to
1311 -- ensure that the pragma/aspect, if present, has been analyzed.
1313 Check_No_Elab_Code_All (N);
1314 end Analyze_Compilation_Unit;
1316 ---------------------
1317 -- Analyze_Context --
1318 ---------------------
1320 procedure Analyze_Context (N : Node_Id) is
1321 Ukind : constant Node_Kind := Nkind (Unit (N));
1322 Item : Node_Id;
1324 begin
1325 -- First process all configuration pragmas at the start of the context
1326 -- items. Strictly these are not part of the context clause, but that
1327 -- is where the parser puts them. In any case for sure we must analyze
1328 -- these before analyzing the actual context items, since they can have
1329 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1330 -- be with'ed as a result of changing categorizations in Ada 2005).
1332 Item := First (Context_Items (N));
1333 while Present (Item)
1334 and then Nkind (Item) = N_Pragma
1335 and then Pragma_Name (Item) in Configuration_Pragma_Names
1336 loop
1337 Analyze (Item);
1338 Next (Item);
1339 end loop;
1341 -- This is the point at which we capture the configuration settings
1342 -- for the unit. At the moment only the Optimize_Alignment setting
1343 -- needs to be captured. Probably more later ???
1345 if Optimize_Alignment_Local then
1346 Set_OA_Setting (Current_Sem_Unit, 'L');
1347 else
1348 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1349 end if;
1351 -- Loop through actual context items. This is done in two passes:
1353 -- a) The first pass analyzes non-limited with-clauses and also any
1354 -- configuration pragmas (we need to get the latter analyzed right
1355 -- away, since they can affect processing of subsequent items).
1357 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1359 while Present (Item) loop
1361 -- For with clause, analyze the with clause, and then update the
1362 -- version, since we are dependent on a unit that we with.
1364 if Nkind (Item) = N_With_Clause
1365 and then not Limited_Present (Item)
1366 then
1367 -- Skip analyzing with clause if no unit, nothing to do (this
1368 -- happens for a with that references a non-existent unit).
1370 if Present (Library_Unit (Item)) then
1372 -- Skip analyzing with clause if this is a with_clause for
1373 -- the main unit, which happens if a subunit has a useless
1374 -- with_clause on its parent.
1376 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1377 Analyze (Item);
1379 -- Here for the case of a useless with for the main unit
1381 else
1382 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1383 end if;
1384 end if;
1386 -- Do version update (skipped for implicit with)
1388 if not Implicit_With (Item) then
1389 Version_Update (N, Library_Unit (Item));
1390 end if;
1392 -- Skip pragmas. Configuration pragmas at the start were handled in
1393 -- the loop above, and remaining pragmas are not processed until we
1394 -- actually install the context (see Install_Context). We delay the
1395 -- analysis of these pragmas to make sure that we have installed all
1396 -- the implicit with's on parent units.
1398 -- Skip use clauses at this stage, since we don't want to do any
1399 -- installing of potentially use-visible entities until we
1400 -- actually install the complete context (in Install_Context).
1401 -- Otherwise things can get installed in the wrong context.
1403 else
1404 null;
1405 end if;
1407 Next (Item);
1408 end loop;
1410 -- Second pass: examine all limited_with clauses. All other context
1411 -- items are ignored in this pass.
1413 Item := First (Context_Items (N));
1414 while Present (Item) loop
1415 if Nkind (Item) = N_With_Clause
1416 and then Limited_Present (Item)
1417 then
1418 -- No need to check errors on implicitly generated limited-with
1419 -- clauses.
1421 if not Implicit_With (Item) then
1423 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1424 -- properly rejected, including renaming declarations.
1426 if not Nkind_In (Ukind, N_Package_Declaration,
1427 N_Subprogram_Declaration)
1428 and then Ukind not in N_Generic_Declaration
1429 and then Ukind not in N_Generic_Instantiation
1430 then
1431 Error_Msg_N ("limited with_clause not allowed here", Item);
1433 -- Check wrong use of a limited with clause applied to the
1434 -- compilation unit containing the limited-with clause.
1436 -- limited with P.Q;
1437 -- package P.Q is ...
1439 elsif Unit (Library_Unit (Item)) = Unit (N) then
1440 Error_Msg_N ("wrong use of limited-with clause", Item);
1442 -- Check wrong use of limited-with clause applied to some
1443 -- immediate ancestor.
1445 elsif Is_Child_Spec (Unit (N)) then
1446 declare
1447 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1448 P : Node_Id;
1450 begin
1451 P := Parent_Spec (Unit (N));
1452 loop
1453 if Unit (P) = Lib_U then
1454 Error_Msg_N ("limited with_clause cannot "
1455 & "name ancestor", Item);
1456 exit;
1457 end if;
1459 exit when not Is_Child_Spec (Unit (P));
1460 P := Parent_Spec (Unit (P));
1461 end loop;
1462 end;
1463 end if;
1465 -- Check if the limited-withed unit is already visible through
1466 -- some context clause of the current compilation unit or some
1467 -- ancestor of the current compilation unit.
1469 declare
1470 Lim_Unit_Name : constant Node_Id := Name (Item);
1471 Comp_Unit : Node_Id;
1472 It : Node_Id;
1473 Unit_Name : Node_Id;
1475 begin
1476 Comp_Unit := N;
1477 loop
1478 It := First (Context_Items (Comp_Unit));
1479 while Present (It) loop
1480 if Item /= It
1481 and then Nkind (It) = N_With_Clause
1482 and then not Limited_Present (It)
1483 and then
1484 Nkind_In (Unit (Library_Unit (It)),
1485 N_Package_Declaration,
1486 N_Package_Renaming_Declaration)
1487 then
1488 if Nkind (Unit (Library_Unit (It))) =
1489 N_Package_Declaration
1490 then
1491 Unit_Name := Name (It);
1492 else
1493 Unit_Name := Name (Unit (Library_Unit (It)));
1494 end if;
1496 -- Check if the named package (or some ancestor)
1497 -- leaves visible the full-view of the unit given
1498 -- in the limited-with clause.
1500 loop
1501 if Designate_Same_Unit (Lim_Unit_Name,
1502 Unit_Name)
1503 then
1504 Error_Msg_Sloc := Sloc (It);
1505 Error_Msg_N
1506 ("simultaneous visibility of limited "
1507 & "and unlimited views not allowed",
1508 Item);
1509 Error_Msg_NE
1510 ("\unlimited view visible through "
1511 & "context clause #",
1512 Item, It);
1513 exit;
1515 elsif Nkind (Unit_Name) = N_Identifier then
1516 exit;
1517 end if;
1519 Unit_Name := Prefix (Unit_Name);
1520 end loop;
1521 end if;
1523 Next (It);
1524 end loop;
1526 exit when not Is_Child_Spec (Unit (Comp_Unit));
1528 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1529 end loop;
1530 end;
1531 end if;
1533 -- Skip analyzing with clause if no unit, see above
1535 if Present (Library_Unit (Item)) then
1536 Analyze (Item);
1537 end if;
1539 -- A limited_with does not impose an elaboration order, but
1540 -- there is a semantic dependency for recompilation purposes.
1542 if not Implicit_With (Item) then
1543 Version_Update (N, Library_Unit (Item));
1544 end if;
1546 -- Pragmas and use clauses and with clauses other than limited
1547 -- with's are ignored in this pass through the context items.
1549 else
1550 null;
1551 end if;
1553 Next (Item);
1554 end loop;
1555 end Analyze_Context;
1557 -------------------------------
1558 -- Analyze_Package_Body_Stub --
1559 -------------------------------
1561 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1562 Id : constant Entity_Id := Defining_Identifier (N);
1563 Nam : Entity_Id;
1564 Opts : Config_Switches_Type;
1566 begin
1567 -- The package declaration must be in the current declarative part
1569 Check_Stub_Level (N);
1570 Nam := Current_Entity_In_Scope (Id);
1572 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1573 Error_Msg_N ("missing specification for package stub", N);
1575 elsif Has_Completion (Nam)
1576 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1577 then
1578 Error_Msg_N ("duplicate or redundant stub for package", N);
1580 else
1581 -- Retain and restore the configuration options of the enclosing
1582 -- context as the proper body may introduce a set of its own.
1584 Save_Opt_Config_Switches (Opts);
1586 -- Indicate that the body of the package exists. If we are doing
1587 -- only semantic analysis, the stub stands for the body. If we are
1588 -- generating code, the existence of the body will be confirmed
1589 -- when we load the proper body.
1591 Set_Has_Completion (Nam);
1592 Set_Scope (Defining_Entity (N), Current_Scope);
1593 Set_Ekind (Defining_Entity (N), E_Package_Body);
1594 Set_Corresponding_Spec_Of_Stub (N, Nam);
1595 Generate_Reference (Nam, Id, 'b');
1596 Analyze_Proper_Body (N, Nam);
1598 Restore_Opt_Config_Switches (Opts);
1599 end if;
1600 end Analyze_Package_Body_Stub;
1602 -------------------------
1603 -- Analyze_Proper_Body --
1604 -------------------------
1606 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1607 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1609 procedure Optional_Subunit;
1610 -- This procedure is called when the main unit is a stub, or when we
1611 -- are not generating code. In such a case, we analyze the subunit if
1612 -- present, which is user-friendly and in fact required for ASIS, but we
1613 -- don't complain if the subunit is missing. In GNATprove_Mode, we issue
1614 -- an error to avoid formal verification of a partial unit.
1616 ----------------------
1617 -- Optional_Subunit --
1618 ----------------------
1620 procedure Optional_Subunit is
1621 Comp_Unit : Node_Id;
1622 Unum : Unit_Number_Type;
1624 begin
1625 -- Try to load subunit, but ignore any errors that occur during the
1626 -- loading of the subunit, by using the special feature in Errout to
1627 -- ignore all errors. Note that Fatal_Error will still be set, so we
1628 -- will be able to check for this case below.
1630 if not (ASIS_Mode or GNATprove_Mode) then
1631 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1632 end if;
1634 Unum :=
1635 Load_Unit
1636 (Load_Name => Subunit_Name,
1637 Required => GNATprove_Mode,
1638 Subunit => True,
1639 Error_Node => N);
1641 if not (ASIS_Mode or GNATprove_Mode) then
1642 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1643 end if;
1645 -- All done if we successfully loaded the subunit
1647 if Unum /= No_Unit
1648 and then (Fatal_Error (Unum) /= Error_Detected
1649 or else Try_Semantics)
1650 then
1651 Comp_Unit := Cunit (Unum);
1653 -- If the file was empty or seriously mangled, the unit itself may
1654 -- be missing.
1656 if No (Unit (Comp_Unit)) then
1657 Error_Msg_N
1658 ("subunit does not contain expected proper body", N);
1660 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1661 Error_Msg_N
1662 ("expected SEPARATE subunit, found child unit",
1663 Cunit_Entity (Unum));
1664 else
1665 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1666 Analyze_Subunit (Comp_Unit);
1667 Set_Library_Unit (N, Comp_Unit);
1668 Set_Corresponding_Body (N, Defining_Entity (Unit (Comp_Unit)));
1669 end if;
1671 elsif Unum = No_Unit
1672 and then Present (Nam)
1673 then
1674 if Is_Protected_Type (Nam) then
1675 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1676 else
1677 Set_Corresponding_Body (
1678 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1679 end if;
1680 end if;
1681 end Optional_Subunit;
1683 -- Local variables
1685 Comp_Unit : Node_Id;
1686 Unum : Unit_Number_Type;
1688 -- Start of processing for Analyze_Proper_Body
1690 begin
1691 -- If the subunit is already loaded, it means that the main unit is a
1692 -- subunit, and that the current unit is one of its parents which was
1693 -- being analyzed to provide the needed context for the analysis of the
1694 -- subunit. In this case we analyze the subunit and continue with the
1695 -- parent, without looking at subsequent subunits.
1697 if Is_Loaded (Subunit_Name) then
1699 -- If the proper body is already linked to the stub node, the stub is
1700 -- in a generic unit and just needs analyzing.
1702 if Present (Library_Unit (N)) then
1703 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1705 -- If the subunit has severe errors, the spec of the enclosing
1706 -- body may not be available, in which case do not try analysis.
1708 if Serious_Errors_Detected > 0
1709 and then No (Library_Unit (Library_Unit (N)))
1710 then
1711 return;
1712 end if;
1714 -- Collect SCO information for loaded subunit if we are in the
1715 -- extended main unit.
1717 if Generate_SCO
1718 and then In_Extended_Main_Source_Unit
1719 (Cunit_Entity (Current_Sem_Unit))
1720 then
1721 SCO_Record_Raw (Get_Cunit_Unit_Number (Library_Unit (N)));
1722 end if;
1724 Analyze_Subunit (Library_Unit (N));
1726 -- Otherwise we must load the subunit and link to it
1728 else
1729 -- Load the subunit, this must work, since we originally loaded
1730 -- the subunit earlier on. So this will not really load it, just
1731 -- give access to it.
1733 Unum :=
1734 Load_Unit
1735 (Load_Name => Subunit_Name,
1736 Required => True,
1737 Subunit => False,
1738 Error_Node => N);
1740 -- And analyze the subunit in the parent context (note that we
1741 -- do not call Semantics, since that would remove the parent
1742 -- context). Because of this, we have to manually reset the
1743 -- compiler state to Analyzing since it got destroyed by Load.
1745 if Unum /= No_Unit then
1746 Compiler_State := Analyzing;
1748 -- Check that the proper body is a subunit and not a child
1749 -- unit. If the unit was previously loaded, the error will
1750 -- have been emitted when copying the generic node, so we
1751 -- just return to avoid cascaded errors.
1753 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1754 return;
1755 end if;
1757 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1758 Analyze_Subunit (Cunit (Unum));
1759 Set_Library_Unit (N, Cunit (Unum));
1760 end if;
1761 end if;
1763 -- If the main unit is a subunit, then we are just performing semantic
1764 -- analysis on that subunit, and any other subunits of any parent unit
1765 -- should be ignored, except that if we are building trees for ASIS
1766 -- usage we want to annotate the stub properly. If the main unit is
1767 -- itself a subunit, another subunit is irrelevant unless it is a
1768 -- subunit of the current one, that is to say appears in the current
1769 -- source tree.
1771 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1772 and then Subunit_Name /= Unit_Name (Main_Unit)
1773 then
1774 if ASIS_Mode then
1775 declare
1776 PB : constant Node_Id := Proper_Body (Unit (Cunit (Main_Unit)));
1777 begin
1778 if Nkind_In (PB, N_Package_Body, N_Subprogram_Body)
1779 and then List_Containing (N) = Declarations (PB)
1780 then
1781 Optional_Subunit;
1782 end if;
1783 end;
1784 end if;
1786 -- But before we return, set the flag for unloaded subunits. This
1787 -- will suppress junk warnings of variables in the same declarative
1788 -- part (or a higher level one) that are in danger of looking unused
1789 -- when in fact there might be a declaration in the subunit that we
1790 -- do not intend to load.
1792 Unloaded_Subunits := True;
1793 return;
1795 -- If the subunit is not already loaded, and we are generating code,
1796 -- then this is the case where compilation started from the parent, and
1797 -- we are generating code for an entire subunit tree. In that case we
1798 -- definitely need to load the subunit.
1800 -- In order to continue the analysis with the rest of the parent,
1801 -- and other subunits, we load the unit without requiring its
1802 -- presence, and emit a warning if not found, rather than terminating
1803 -- the compilation abruptly, as for other missing file problems.
1805 elsif Original_Operating_Mode = Generate_Code then
1807 -- If the proper body is already linked to the stub node, the stub is
1808 -- in a generic unit and just needs analyzing.
1810 -- We update the version. Although we are not strictly technically
1811 -- semantically dependent on the subunit, given our approach of macro
1812 -- substitution of subunits, it makes sense to include it in the
1813 -- version identification.
1815 if Present (Library_Unit (N)) then
1816 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1817 Analyze_Subunit (Library_Unit (N));
1818 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1820 -- Otherwise we must load the subunit and link to it
1822 else
1823 -- Make sure that, if the subunit is preprocessed and -gnateG is
1824 -- specified, the preprocessed file will be written.
1826 Lib.Analysing_Subunit_Of_Main := True;
1827 Unum :=
1828 Load_Unit
1829 (Load_Name => Subunit_Name,
1830 Required => False,
1831 Subunit => True,
1832 Error_Node => N);
1833 Lib.Analysing_Subunit_Of_Main := False;
1835 -- Give message if we did not get the unit Emit warning even if
1836 -- missing subunit is not within main unit, to simplify debugging.
1838 pragma Assert (Original_Operating_Mode = Generate_Code);
1839 if Unum = No_Unit then
1840 Error_Msg_Unit_1 := Subunit_Name;
1841 Error_Msg_File_1 :=
1842 Get_File_Name (Subunit_Name, Subunit => True);
1843 Error_Msg_N
1844 ("subunit$$ in file{ not found??!!", N);
1845 Subunits_Missing := True;
1846 end if;
1848 -- Load_Unit may reset Compiler_State, since it may have been
1849 -- necessary to parse an additional units, so we make sure that
1850 -- we reset it to the Analyzing state.
1852 Compiler_State := Analyzing;
1854 if Unum /= No_Unit then
1855 if Debug_Flag_L then
1856 Write_Str ("*** Loaded subunit from stub. Analyze");
1857 Write_Eol;
1858 end if;
1860 Comp_Unit := Cunit (Unum);
1862 -- Check for child unit instead of subunit
1864 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1865 Error_Msg_N
1866 ("expected SEPARATE subunit, found child unit",
1867 Cunit_Entity (Unum));
1869 -- OK, we have a subunit
1871 else
1872 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1873 Set_Library_Unit (N, Comp_Unit);
1875 -- We update the version. Although we are not technically
1876 -- semantically dependent on the subunit, given our approach
1877 -- of macro substitution of subunits, it makes sense to
1878 -- include it in the version identification.
1880 Version_Update (Cunit (Main_Unit), Comp_Unit);
1882 -- Collect SCO information for loaded subunit if we are in
1883 -- the extended main unit.
1885 if Generate_SCO
1886 and then In_Extended_Main_Source_Unit
1887 (Cunit_Entity (Current_Sem_Unit))
1888 then
1889 SCO_Record_Raw (Unum);
1890 end if;
1892 -- Analyze the unit if semantics active
1894 if Fatal_Error (Unum) /= Error_Detected
1895 or else Try_Semantics
1896 then
1897 Analyze_Subunit (Comp_Unit);
1898 end if;
1899 end if;
1900 end if;
1901 end if;
1903 -- The remaining case is when the subunit is not already loaded and we
1904 -- are not generating code. In this case we are just performing semantic
1905 -- analysis on the parent, and we are not interested in the subunit. For
1906 -- subprograms, analyze the stub as a body. For other entities the stub
1907 -- has already been marked as completed.
1909 else
1910 Optional_Subunit;
1911 end if;
1912 end Analyze_Proper_Body;
1914 ----------------------------------
1915 -- Analyze_Protected_Body_Stub --
1916 ----------------------------------
1918 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1919 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1921 begin
1922 Check_Stub_Level (N);
1924 -- First occurrence of name may have been as an incomplete type
1926 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1927 Nam := Full_View (Nam);
1928 end if;
1930 if No (Nam) or else not Is_Protected_Type (Etype (Nam)) then
1931 Error_Msg_N ("missing specification for Protected body", N);
1933 else
1934 Set_Scope (Defining_Entity (N), Current_Scope);
1935 Set_Ekind (Defining_Entity (N), E_Protected_Body);
1936 Set_Has_Completion (Etype (Nam));
1937 Set_Corresponding_Spec_Of_Stub (N, Nam);
1938 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1939 Analyze_Proper_Body (N, Etype (Nam));
1940 end if;
1941 end Analyze_Protected_Body_Stub;
1943 ----------------------------------
1944 -- Analyze_Subprogram_Body_Stub --
1945 ----------------------------------
1947 -- A subprogram body stub can appear with or without a previous spec. If
1948 -- there is one, then the analysis of the body will find it and verify
1949 -- conformance. The formals appearing in the specification of the stub play
1950 -- no role, except for requiring an additional conformance check. If there
1951 -- is no previous subprogram declaration, the stub acts as a spec, and
1952 -- provides the defining entity for the subprogram.
1954 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1955 Decl : Node_Id;
1956 Opts : Config_Switches_Type;
1958 begin
1959 Check_Stub_Level (N);
1961 -- Verify that the identifier for the stub is unique within this
1962 -- declarative part.
1964 if Nkind_In (Parent (N), N_Block_Statement,
1965 N_Package_Body,
1966 N_Subprogram_Body)
1967 then
1968 Decl := First (Declarations (Parent (N)));
1969 while Present (Decl) and then Decl /= N loop
1970 if Nkind (Decl) = N_Subprogram_Body_Stub
1971 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1972 Chars (Defining_Unit_Name (Specification (N))))
1973 then
1974 Error_Msg_N ("identifier for stub is not unique", N);
1975 end if;
1977 Next (Decl);
1978 end loop;
1979 end if;
1981 -- Retain and restore the configuration options of the enclosing context
1982 -- as the proper body may introduce a set of its own.
1984 Save_Opt_Config_Switches (Opts);
1986 -- Treat stub as a body, which checks conformance if there is a previous
1987 -- declaration, or else introduces entity and its signature.
1989 Analyze_Subprogram_Body (N);
1990 Analyze_Proper_Body (N, Empty);
1992 Restore_Opt_Config_Switches (Opts);
1993 end Analyze_Subprogram_Body_Stub;
1995 ---------------------
1996 -- Analyze_Subunit --
1997 ---------------------
1999 -- A subunit is compiled either by itself (for semantic checking) or as
2000 -- part of compiling the parent (for code generation). In either case, by
2001 -- the time we actually process the subunit, the parent has already been
2002 -- installed and analyzed. The node N is a compilation unit, whose context
2003 -- needs to be treated here, because we come directly here from the parent
2004 -- without calling Analyze_Compilation_Unit.
2006 -- The compilation context includes the explicit context of the subunit,
2007 -- and the context of the parent, together with the parent itself. In order
2008 -- to compile the current context, we remove the one inherited from the
2009 -- parent, in order to have a clean visibility table. We restore the parent
2010 -- context before analyzing the proper body itself. On exit, we remove only
2011 -- the explicit context of the subunit.
2013 procedure Analyze_Subunit (N : Node_Id) is
2014 Lib_Unit : constant Node_Id := Library_Unit (N);
2015 Par_Unit : constant Entity_Id := Current_Scope;
2017 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
2018 Num_Scopes : Nat := 0;
2019 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2020 Enclosing_Child : Entity_Id := Empty;
2021 Svg : constant Suppress_Record := Scope_Suppress;
2023 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
2024 Cunit_Boolean_Restrictions_Save;
2025 -- Save non-partition wide restrictions before processing the subunit.
2026 -- All subunits are analyzed with config restrictions reset and we need
2027 -- to restore these saved values at the end.
2029 procedure Analyze_Subunit_Context;
2030 -- Capture names in use clauses of the subunit. This must be done before
2031 -- re-installing parent declarations, because items in the context must
2032 -- not be hidden by declarations local to the parent.
2034 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
2035 -- Recursive procedure to restore scope of all ancestors of subunit,
2036 -- from outermost in. If parent is not a subunit, the call to install
2037 -- context installs context of spec and (if parent is a child unit) the
2038 -- context of its parents as well. It is confusing that parents should
2039 -- be treated differently in both cases, but the semantics are just not
2040 -- identical.
2042 procedure Re_Install_Use_Clauses;
2043 -- As part of the removal of the parent scope, the use clauses are
2044 -- removed, to be reinstalled when the context of the subunit has been
2045 -- analyzed. Use clauses may also have been affected by the analysis of
2046 -- the context of the subunit, so they have to be applied again, to
2047 -- insure that the compilation environment of the rest of the parent
2048 -- unit is identical.
2050 procedure Remove_Scope;
2051 -- Remove current scope from scope stack, and preserve the list of use
2052 -- clauses in it, to be reinstalled after context is analyzed.
2054 -----------------------------
2055 -- Analyze_Subunit_Context --
2056 -----------------------------
2058 procedure Analyze_Subunit_Context is
2059 Item : Node_Id;
2060 Nam : Node_Id;
2061 Unit_Name : Entity_Id;
2063 begin
2064 Analyze_Context (N);
2065 Check_No_Elab_Code_All (N);
2067 -- Make withed units immediately visible. If child unit, make the
2068 -- ultimate parent immediately visible.
2070 Item := First (Context_Items (N));
2071 while Present (Item) loop
2072 if Nkind (Item) = N_With_Clause then
2074 -- Protect frontend against previous errors in context clauses
2076 if Nkind (Name (Item)) /= N_Selected_Component then
2077 if Error_Posted (Item) then
2078 null;
2080 else
2081 -- If a subunits has serious syntax errors, the context
2082 -- may not have been loaded. Add a harmless unit name to
2083 -- attempt processing.
2085 if Serious_Errors_Detected > 0
2086 and then No (Entity (Name (Item)))
2087 then
2088 Set_Entity (Name (Item), Standard_Standard);
2089 end if;
2091 Unit_Name := Entity (Name (Item));
2092 loop
2093 Set_Is_Visible_Lib_Unit (Unit_Name);
2094 exit when Scope (Unit_Name) = Standard_Standard;
2095 Unit_Name := Scope (Unit_Name);
2097 if No (Unit_Name) then
2098 Check_Error_Detected;
2099 return;
2100 end if;
2101 end loop;
2103 if not Is_Immediately_Visible (Unit_Name) then
2104 Set_Is_Immediately_Visible (Unit_Name);
2105 Set_Context_Installed (Item);
2106 end if;
2107 end if;
2108 end if;
2110 elsif Nkind (Item) = N_Use_Package_Clause then
2111 Nam := First (Names (Item));
2112 while Present (Nam) loop
2113 Analyze (Nam);
2114 Next (Nam);
2115 end loop;
2117 elsif Nkind (Item) = N_Use_Type_Clause then
2118 Nam := First (Subtype_Marks (Item));
2119 while Present (Nam) loop
2120 Analyze (Nam);
2121 Next (Nam);
2122 end loop;
2123 end if;
2125 Next (Item);
2126 end loop;
2128 -- Reset visibility of withed units. They will be made visible again
2129 -- when we install the subunit context.
2131 Item := First (Context_Items (N));
2132 while Present (Item) loop
2133 if Nkind (Item) = N_With_Clause
2135 -- Protect frontend against previous errors in context clauses
2137 and then Nkind (Name (Item)) /= N_Selected_Component
2138 and then not Error_Posted (Item)
2139 then
2140 Unit_Name := Entity (Name (Item));
2141 loop
2142 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2143 exit when Scope (Unit_Name) = Standard_Standard;
2144 Unit_Name := Scope (Unit_Name);
2145 end loop;
2147 if Context_Installed (Item) then
2148 Set_Is_Immediately_Visible (Unit_Name, False);
2149 Set_Context_Installed (Item, False);
2150 end if;
2151 end if;
2153 Next (Item);
2154 end loop;
2155 end Analyze_Subunit_Context;
2157 ------------------------
2158 -- Re_Install_Parents --
2159 ------------------------
2161 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2162 E : Entity_Id;
2164 begin
2165 if Nkind (Unit (L)) = N_Subunit then
2166 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2167 end if;
2169 Install_Context (L);
2171 -- If the subunit occurs within a child unit, we must restore the
2172 -- immediate visibility of any siblings that may occur in context.
2174 if Present (Enclosing_Child) then
2175 Install_Siblings (Enclosing_Child, L);
2176 end if;
2178 Push_Scope (Scop);
2180 if Scop /= Par_Unit then
2181 Set_Is_Immediately_Visible (Scop);
2182 end if;
2184 -- Make entities in scope visible again. For child units, restore
2185 -- visibility only if they are actually in context.
2187 E := First_Entity (Current_Scope);
2188 while Present (E) loop
2189 if not Is_Child_Unit (E) or else Is_Visible_Lib_Unit (E) then
2190 Set_Is_Immediately_Visible (E);
2191 end if;
2193 Next_Entity (E);
2194 end loop;
2196 -- A subunit appears within a body, and for a nested subunits all the
2197 -- parents are bodies. Restore full visibility of their private
2198 -- entities.
2200 if Is_Package_Or_Generic_Package (Scop) then
2201 Set_In_Package_Body (Scop);
2202 Install_Private_Declarations (Scop);
2203 end if;
2204 end Re_Install_Parents;
2206 ----------------------------
2207 -- Re_Install_Use_Clauses --
2208 ----------------------------
2210 procedure Re_Install_Use_Clauses is
2211 U : Node_Id;
2212 begin
2213 for J in reverse 1 .. Num_Scopes loop
2214 U := Use_Clauses (J);
2215 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2216 Install_Use_Clauses (U, Force_Installation => True);
2217 end loop;
2218 end Re_Install_Use_Clauses;
2220 ------------------
2221 -- Remove_Scope --
2222 ------------------
2224 procedure Remove_Scope is
2225 E : Entity_Id;
2227 begin
2228 Num_Scopes := Num_Scopes + 1;
2229 Use_Clauses (Num_Scopes) :=
2230 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2232 E := First_Entity (Current_Scope);
2233 while Present (E) loop
2234 Set_Is_Immediately_Visible (E, False);
2235 Next_Entity (E);
2236 end loop;
2238 if Is_Child_Unit (Current_Scope) then
2239 Enclosing_Child := Current_Scope;
2240 end if;
2242 Pop_Scope;
2243 end Remove_Scope;
2245 -- Start of processing for Analyze_Subunit
2247 begin
2248 -- For subunit in main extended unit, we reset the configuration values
2249 -- for the non-partition-wide restrictions. For other units reset them.
2251 if In_Extended_Main_Source_Unit (N) then
2252 Restore_Config_Cunit_Boolean_Restrictions;
2253 else
2254 Reset_Cunit_Boolean_Restrictions;
2255 end if;
2257 if Style_Check then
2258 declare
2259 Nam : Node_Id := Name (Unit (N));
2261 begin
2262 if Nkind (Nam) = N_Selected_Component then
2263 Nam := Selector_Name (Nam);
2264 end if;
2266 Check_Identifier (Nam, Par_Unit);
2267 end;
2268 end if;
2270 if not Is_Empty_List (Context_Items (N)) then
2272 -- Save current use clauses
2274 Remove_Scope;
2275 Remove_Context (Lib_Unit);
2277 -- Now remove parents and their context, including enclosing subunits
2278 -- and the outer parent body which is not a subunit.
2280 if Present (Lib_Spec) then
2281 Remove_Context (Lib_Spec);
2283 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2284 Lib_Spec := Library_Unit (Lib_Spec);
2285 Remove_Scope;
2286 Remove_Context (Lib_Spec);
2287 end loop;
2289 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2290 Remove_Scope;
2291 end if;
2293 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2294 Remove_Context (Library_Unit (Lib_Spec));
2295 end if;
2296 end if;
2298 Set_Is_Immediately_Visible (Par_Unit, False);
2300 Analyze_Subunit_Context;
2302 Re_Install_Parents (Lib_Unit, Par_Unit);
2303 Set_Is_Immediately_Visible (Par_Unit);
2305 -- If the context includes a child unit of the parent of the subunit,
2306 -- the parent will have been removed from visibility, after compiling
2307 -- that cousin in the context. The visibility of the parent must be
2308 -- restored now. This also applies if the context includes another
2309 -- subunit of the same parent which in turn includes a child unit in
2310 -- its context.
2312 if Is_Package_Or_Generic_Package (Par_Unit) then
2313 if not Is_Immediately_Visible (Par_Unit)
2314 or else (Present (First_Entity (Par_Unit))
2315 and then not
2316 Is_Immediately_Visible (First_Entity (Par_Unit)))
2317 then
2318 Set_Is_Immediately_Visible (Par_Unit);
2319 Install_Visible_Declarations (Par_Unit);
2320 Install_Private_Declarations (Par_Unit);
2321 end if;
2322 end if;
2324 Re_Install_Use_Clauses;
2325 Install_Context (N);
2327 -- Restore state of suppress flags for current body
2329 Scope_Suppress := Svg;
2331 -- If the subunit is within a child unit, then siblings of any parent
2332 -- unit that appear in the context clause of the subunit must also be
2333 -- made immediately visible.
2335 if Present (Enclosing_Child) then
2336 Install_Siblings (Enclosing_Child, N);
2337 end if;
2338 end if;
2340 Generate_Parent_References (Unit (N), Par_Unit);
2341 Analyze (Proper_Body (Unit (N)));
2342 Remove_Context (N);
2344 -- The subunit may contain a with_clause on a sibling of some ancestor.
2345 -- Removing the context will remove from visibility those ancestor child
2346 -- units, which must be restored to the visibility they have in the
2347 -- enclosing body.
2349 if Present (Enclosing_Child) then
2350 declare
2351 C : Entity_Id;
2352 begin
2353 C := Current_Scope;
2354 while Present (C) and then C /= Standard_Standard loop
2355 Set_Is_Immediately_Visible (C);
2356 Set_Is_Visible_Lib_Unit (C);
2357 C := Scope (C);
2358 end loop;
2359 end;
2360 end if;
2362 -- Deal with restore of restrictions
2364 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2365 end Analyze_Subunit;
2367 ----------------------------
2368 -- Analyze_Task_Body_Stub --
2369 ----------------------------
2371 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2372 Loc : constant Source_Ptr := Sloc (N);
2373 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2375 begin
2376 Check_Stub_Level (N);
2378 -- First occurrence of name may have been as an incomplete type
2380 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2381 Nam := Full_View (Nam);
2382 end if;
2384 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2385 Error_Msg_N ("missing specification for task body", N);
2387 else
2388 Set_Scope (Defining_Entity (N), Current_Scope);
2389 Set_Ekind (Defining_Entity (N), E_Task_Body);
2390 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2391 Set_Corresponding_Spec_Of_Stub (N, Nam);
2393 -- Check for duplicate stub, if so give message and terminate
2395 if Has_Completion (Etype (Nam)) then
2396 Error_Msg_N ("duplicate stub for task", N);
2397 return;
2398 else
2399 Set_Has_Completion (Etype (Nam));
2400 end if;
2402 Analyze_Proper_Body (N, Etype (Nam));
2404 -- Set elaboration flag to indicate that entity is callable. This
2405 -- cannot be done in the expansion of the body itself, because the
2406 -- proper body is not in a declarative part. This is only done if
2407 -- expansion is active, because the context may be generic and the
2408 -- flag not defined yet.
2410 if Expander_Active then
2411 Insert_After (N,
2412 Make_Assignment_Statement (Loc,
2413 Name =>
2414 Make_Identifier (Loc,
2415 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2416 Expression => New_Occurrence_Of (Standard_True, Loc)));
2417 end if;
2418 end if;
2419 end Analyze_Task_Body_Stub;
2421 -------------------------
2422 -- Analyze_With_Clause --
2423 -------------------------
2425 -- Analyze the declaration of a unit in a with clause. At end, label the
2426 -- with clause with the defining entity for the unit.
2428 procedure Analyze_With_Clause (N : Node_Id) is
2430 -- Retrieve the original kind of the unit node, before analysis. If it
2431 -- is a subprogram instantiation, its analysis below will rewrite the
2432 -- node as the declaration of the wrapper package. If the same
2433 -- instantiation appears indirectly elsewhere in the context, it will
2434 -- have been analyzed already.
2436 Unit_Kind : constant Node_Kind :=
2437 Nkind (Original_Node (Unit (Library_Unit (N))));
2438 Nam : constant Node_Id := Name (N);
2439 E_Name : Entity_Id;
2440 Par_Name : Entity_Id;
2441 Pref : Node_Id;
2442 U : Node_Id;
2444 Intunit : Boolean;
2445 -- Set True if the unit currently being compiled is an internal unit
2447 Restriction_Violation : Boolean := False;
2448 -- Set True if a with violates a restriction, no point in giving any
2449 -- warnings if we have this definite error.
2451 Save_Style_Check : constant Boolean := Opt.Style_Check;
2453 begin
2454 U := Unit (Library_Unit (N));
2456 -- If this is an internal unit which is a renaming, then this is a
2457 -- violation of No_Obsolescent_Features.
2459 -- Note: this is not quite right if the user defines one of these units
2460 -- himself, but that's a marginal case, and fixing it is hard ???
2462 if Restriction_Check_Required (No_Obsolescent_Features) then
2463 declare
2464 F : constant File_Name_Type :=
2465 Unit_File_Name (Get_Source_Unit (U));
2466 begin
2467 if Is_Predefined_File_Name (F, Renamings_Included => True)
2468 and then not
2469 Is_Predefined_File_Name (F, Renamings_Included => False)
2470 then
2471 Check_Restriction (No_Obsolescent_Features, N);
2472 Restriction_Violation := True;
2473 end if;
2474 end;
2475 end if;
2477 -- Check No_Implementation_Units violation
2479 if Restriction_Check_Required (No_Implementation_Units) then
2480 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2481 null;
2482 else
2483 Check_Restriction (No_Implementation_Units, Nam);
2484 Restriction_Violation := True;
2485 end if;
2486 end if;
2488 -- Several actions are skipped for dummy packages (those supplied for
2489 -- with's where no matching file could be found). Such packages are
2490 -- identified by the Sloc value being set to No_Location.
2492 if Limited_Present (N) then
2494 -- Ada 2005 (AI-50217): Build visibility structures but do not
2495 -- analyze the unit.
2497 -- If the designated unit is a predefined unit, which might be used
2498 -- implicitly through the rtsfind machinery, a limited with clause
2499 -- on such a unit is usually pointless, because run-time units are
2500 -- unlikely to appear in mutually dependent units, and because this
2501 -- disables the rtsfind mechanism. We transform such limited with
2502 -- clauses into regular with clauses.
2504 if Sloc (U) /= No_Location then
2505 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (U)))
2507 -- In ASIS mode the rtsfind mechanism plays no role, and
2508 -- we need to maintain the original tree structure, so
2509 -- this transformation is not performed in this case.
2511 and then not ASIS_Mode
2512 then
2513 Set_Limited_Present (N, False);
2514 Analyze_With_Clause (N);
2515 else
2516 Build_Limited_Views (N);
2517 end if;
2518 end if;
2520 return;
2521 end if;
2523 -- If we are compiling under "don't quit" mode (-gnatq) and we have
2524 -- already detected serious errors then we mark the with-clause nodes as
2525 -- analyzed before the corresponding compilation unit is analyzed. This
2526 -- is done here to protect the frontend against never ending recursion
2527 -- caused by circularities in the sources (because the previous errors
2528 -- may break the regular machine of the compiler implemented in
2529 -- Load_Unit to detect circularities).
2531 if Serious_Errors_Detected > 0 and then Try_Semantics then
2532 Set_Analyzed (N);
2533 end if;
2535 Semantics (Library_Unit (N));
2537 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2539 if Sloc (U) /= No_Location then
2541 -- Check restrictions, except that we skip the check if this is an
2542 -- internal unit unless we are compiling the internal unit as the
2543 -- main unit. We also skip this for dummy packages.
2545 Check_Restriction_No_Dependence (Nam, N);
2547 if not Intunit or else Current_Sem_Unit = Main_Unit then
2548 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2549 end if;
2551 -- Deal with special case of GNAT.Current_Exceptions which interacts
2552 -- with the optimization of local raise statements into gotos.
2554 if Nkind (Nam) = N_Selected_Component
2555 and then Nkind (Prefix (Nam)) = N_Identifier
2556 and then Chars (Prefix (Nam)) = Name_Gnat
2557 and then Nam_In (Chars (Selector_Name (Nam)),
2558 Name_Most_Recent_Exception,
2559 Name_Exception_Traces)
2560 then
2561 Check_Restriction (No_Exception_Propagation, N);
2562 Special_Exception_Package_Used := True;
2563 end if;
2565 -- Check for inappropriate with of internal implementation unit if we
2566 -- are not compiling an internal unit and also check for withing unit
2567 -- in wrong version of Ada. Do not issue these messages for implicit
2568 -- with's generated by the compiler itself.
2570 if Implementation_Unit_Warnings
2571 and then not Intunit
2572 and then not Implicit_With (N)
2573 and then not Restriction_Violation
2574 then
2575 declare
2576 U_Kind : constant Kind_Of_Unit :=
2577 Get_Kind_Of_Unit (Get_Source_Unit (U));
2579 begin
2580 if U_Kind = Implementation_Unit then
2581 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2583 -- Add alternative name if available, otherwise issue a
2584 -- general warning message.
2586 if Error_Msg_Strlen /= 0 then
2587 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2588 else
2589 Error_Msg_F
2590 ("\use of this unit is non-portable " &
2591 "and version-dependent?i?", Name (N));
2592 end if;
2594 elsif U_Kind = Ada_2005_Unit
2595 and then Ada_Version < Ada_2005
2596 and then Warn_On_Ada_2005_Compatibility
2597 then
2598 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2600 elsif U_Kind = Ada_2012_Unit
2601 and then Ada_Version < Ada_2012
2602 and then Warn_On_Ada_2012_Compatibility
2603 then
2604 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2605 end if;
2606 end;
2607 end if;
2608 end if;
2610 -- Semantic analysis of a generic unit is performed on a copy of
2611 -- the original tree. Retrieve the entity on which semantic info
2612 -- actually appears.
2614 if Unit_Kind in N_Generic_Declaration then
2615 E_Name := Defining_Entity (U);
2617 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2618 -- the case of an instantiation, semantic analysis above will have
2619 -- replaced the unit by its instantiated version. If the instance body
2620 -- has been generated, the instance now denotes the body entity. For
2621 -- visibility purposes we need the entity of its spec.
2623 elsif (Unit_Kind = N_Package_Instantiation
2624 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2625 N_Package_Instantiation)
2626 and then Nkind (U) = N_Package_Body
2627 then
2628 E_Name := Corresponding_Spec (U);
2630 elsif Unit_Kind = N_Package_Instantiation
2631 and then Nkind (U) = N_Package_Instantiation
2632 and then Present (Instance_Spec (U))
2633 then
2634 -- If the instance has not been rewritten as a package declaration,
2635 -- then it appeared already in a previous with clause. Retrieve
2636 -- the entity from the previous instance.
2638 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2640 elsif Unit_Kind in N_Subprogram_Instantiation then
2642 -- The visible subprogram is created during instantiation, and is
2643 -- an attribute of the wrapper package. We retrieve the wrapper
2644 -- package directly from the instantiation node. If the instance
2645 -- is inlined the unit is still an instantiation. Otherwise it has
2646 -- been rewritten as the declaration of the wrapper itself.
2648 if Nkind (U) in N_Subprogram_Instantiation then
2649 E_Name :=
2650 Related_Instance
2651 (Defining_Entity (Specification (Instance_Spec (U))));
2652 else
2653 E_Name := Related_Instance (Defining_Entity (U));
2654 end if;
2656 elsif Unit_Kind = N_Package_Renaming_Declaration
2657 or else Unit_Kind in N_Generic_Renaming_Declaration
2658 then
2659 E_Name := Defining_Entity (U);
2661 elsif Unit_Kind = N_Subprogram_Body
2662 and then Nkind (Name (N)) = N_Selected_Component
2663 and then not Acts_As_Spec (Library_Unit (N))
2664 then
2665 -- For a child unit that has no spec, one has been created and
2666 -- analyzed. The entity required is that of the spec.
2668 E_Name := Corresponding_Spec (U);
2670 else
2671 E_Name := Defining_Entity (U);
2672 end if;
2674 if Nkind (Name (N)) = N_Selected_Component then
2676 -- Child unit in a with clause
2678 Change_Selected_Component_To_Expanded_Name (Name (N));
2680 -- If this is a child unit without a spec, and it has been analyzed
2681 -- already, a declaration has been created for it. The with_clause
2682 -- must reflect the actual body, and not the generated declaration,
2683 -- to prevent spurious binding errors involving an out-of-date spec.
2684 -- Note that this can only happen if the unit includes more than one
2685 -- with_clause for the child unit (e.g. in separate subunits).
2687 if Unit_Kind = N_Subprogram_Declaration
2688 and then Analyzed (Library_Unit (N))
2689 and then not Comes_From_Source (Library_Unit (N))
2690 then
2691 Set_Library_Unit (N,
2692 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2693 end if;
2694 end if;
2696 -- Restore style checks
2698 Style_Check := Save_Style_Check;
2700 -- Record the reference, but do NOT set the unit as referenced, we want
2701 -- to consider the unit as unreferenced if this is the only reference
2702 -- that occurs.
2704 Set_Entity_With_Checks (Name (N), E_Name);
2705 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2707 -- Generate references and check No_Dependence restriction for parents
2709 if Is_Child_Unit (E_Name) then
2710 Pref := Prefix (Name (N));
2711 Par_Name := Scope (E_Name);
2712 while Nkind (Pref) = N_Selected_Component loop
2713 Change_Selected_Component_To_Expanded_Name (Pref);
2715 if Present (Entity (Selector_Name (Pref)))
2716 and then
2717 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2718 and then Entity (Selector_Name (Pref)) /= Par_Name
2719 then
2720 -- The prefix is a child unit that denotes a renaming declaration.
2721 -- Replace the prefix directly with the renamed unit, because the
2722 -- rest of the prefix is irrelevant to the visibility of the real
2723 -- unit.
2725 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2726 exit;
2727 end if;
2729 Set_Entity_With_Checks (Pref, Par_Name);
2731 Generate_Reference (Par_Name, Pref);
2732 Check_Restriction_No_Dependence (Pref, N);
2733 Pref := Prefix (Pref);
2735 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2736 -- is set to Standard_Standard, and no attempt should be made to
2737 -- further unwind scopes.
2739 if Par_Name /= Standard_Standard then
2740 Par_Name := Scope (Par_Name);
2741 end if;
2743 -- Abandon processing in case of previous errors
2745 if No (Par_Name) then
2746 Check_Error_Detected;
2747 return;
2748 end if;
2749 end loop;
2751 if Present (Entity (Pref))
2752 and then not Analyzed (Parent (Parent (Entity (Pref))))
2753 then
2754 -- If the entity is set without its unit being compiled, the
2755 -- original parent is a renaming, and Par_Name is the renamed
2756 -- entity. For visibility purposes, we need the original entity,
2757 -- which must be analyzed now because Load_Unit directly retrieves
2758 -- the renamed unit, and the renaming declaration itself has not
2759 -- been analyzed.
2761 Analyze (Parent (Parent (Entity (Pref))));
2762 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2763 Par_Name := Entity (Pref);
2764 end if;
2766 -- Guard against missing or misspelled child units
2768 if Present (Par_Name) then
2769 Set_Entity_With_Checks (Pref, Par_Name);
2770 Generate_Reference (Par_Name, Pref);
2772 else
2773 pragma Assert (Serious_Errors_Detected /= 0);
2775 -- Mark the node to indicate that a related error has been posted.
2776 -- This defends further compilation passes against improper use of
2777 -- the invalid WITH clause node.
2779 Set_Error_Posted (N);
2780 Set_Name (N, Error);
2781 return;
2782 end if;
2783 end if;
2785 -- If the withed unit is System, and a system extension pragma is
2786 -- present, compile the extension now, rather than waiting for a
2787 -- visibility check on a specific entity.
2789 if Chars (E_Name) = Name_System
2790 and then Scope (E_Name) = Standard_Standard
2791 and then Present (System_Extend_Unit)
2792 and then Present_System_Aux (N)
2793 then
2794 -- If the extension is not present, an error will have been emitted
2796 null;
2797 end if;
2799 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2800 -- to private_with units; they will be made visible later (just before
2801 -- the private part is analyzed)
2803 if Private_Present (N) then
2804 Set_Is_Immediately_Visible (E_Name, False);
2805 end if;
2807 -- Propagate Fatal_Error setting from with'ed unit to current unit
2809 case Fatal_Error (Get_Source_Unit (Library_Unit (N))) is
2811 -- Nothing to do if with'ed unit had no error
2813 when None =>
2814 null;
2816 -- If with'ed unit had a detected fatal error, propagate it
2818 when Error_Detected =>
2819 Set_Fatal_Error (Current_Sem_Unit, Error_Detected);
2821 -- If with'ed unit had an ignored error, then propagate it but do not
2822 -- overide an existring setting.
2824 when Error_Ignored =>
2825 if Fatal_Error (Current_Sem_Unit) = None then
2826 Set_Fatal_Error (Current_Sem_Unit, Error_Ignored);
2827 end if;
2828 end case;
2829 end Analyze_With_Clause;
2831 ------------------------------
2832 -- Check_Private_Child_Unit --
2833 ------------------------------
2835 procedure Check_Private_Child_Unit (N : Node_Id) is
2836 Lib_Unit : constant Node_Id := Unit (N);
2837 Item : Node_Id;
2838 Curr_Unit : Entity_Id;
2839 Sub_Parent : Node_Id;
2840 Priv_Child : Entity_Id;
2841 Par_Lib : Entity_Id;
2842 Par_Spec : Node_Id;
2844 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2845 -- Returns true if and only if the library unit is declared with
2846 -- an explicit designation of private.
2848 -----------------------------
2849 -- Is_Private_Library_Unit --
2850 -----------------------------
2852 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2853 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2855 begin
2856 return Private_Present (Comp_Unit);
2857 end Is_Private_Library_Unit;
2859 -- Start of processing for Check_Private_Child_Unit
2861 begin
2862 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2863 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2864 Par_Lib := Curr_Unit;
2866 elsif Nkind (Lib_Unit) = N_Subunit then
2868 -- The parent is itself a body. The parent entity is to be found in
2869 -- the corresponding spec.
2871 Sub_Parent := Library_Unit (N);
2872 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2874 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2875 -- enclosing body, retrieve the spec entity which is the proper
2876 -- ancestor we need for the following tests.
2878 if Ekind (Curr_Unit) = E_Package_Body then
2879 Curr_Unit := Spec_Entity (Curr_Unit);
2880 end if;
2882 Par_Lib := Curr_Unit;
2884 else
2885 Curr_Unit := Defining_Entity (Lib_Unit);
2887 Par_Lib := Curr_Unit;
2888 Par_Spec := Parent_Spec (Lib_Unit);
2890 if No (Par_Spec) then
2891 Par_Lib := Empty;
2892 else
2893 Par_Lib := Defining_Entity (Unit (Par_Spec));
2894 end if;
2895 end if;
2897 -- Loop through context items
2899 Item := First (Context_Items (N));
2900 while Present (Item) loop
2902 -- Ada 2005 (AI-262): Allow private_with of a private child package
2903 -- in public siblings
2905 if Nkind (Item) = N_With_Clause
2906 and then not Implicit_With (Item)
2907 and then not Limited_Present (Item)
2908 and then Is_Private_Descendant (Entity (Name (Item)))
2909 then
2910 Priv_Child := Entity (Name (Item));
2912 declare
2913 Curr_Parent : Entity_Id := Par_Lib;
2914 Child_Parent : Entity_Id := Scope (Priv_Child);
2915 Prv_Ancestor : Entity_Id := Child_Parent;
2916 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2918 begin
2919 -- If the child unit is a public child then locate the nearest
2920 -- private ancestor. Child_Parent will then be set to the
2921 -- parent of that ancestor.
2923 if not Is_Private_Library_Unit (Priv_Child) then
2924 while Present (Prv_Ancestor)
2925 and then not Is_Private_Library_Unit (Prv_Ancestor)
2926 loop
2927 Prv_Ancestor := Scope (Prv_Ancestor);
2928 end loop;
2930 if Present (Prv_Ancestor) then
2931 Child_Parent := Scope (Prv_Ancestor);
2932 end if;
2933 end if;
2935 while Present (Curr_Parent)
2936 and then Curr_Parent /= Standard_Standard
2937 and then Curr_Parent /= Child_Parent
2938 loop
2939 Curr_Private :=
2940 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2941 Curr_Parent := Scope (Curr_Parent);
2942 end loop;
2944 if No (Curr_Parent) then
2945 Curr_Parent := Standard_Standard;
2946 end if;
2948 if Curr_Parent /= Child_Parent then
2949 if Ekind (Priv_Child) = E_Generic_Package
2950 and then Chars (Priv_Child) in Text_IO_Package_Name
2951 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2952 then
2953 Error_Msg_NE
2954 ("& is a nested package, not a compilation unit",
2955 Name (Item), Priv_Child);
2957 else
2958 Error_Msg_N
2959 ("unit in with clause is private child unit!", Item);
2960 Error_Msg_NE
2961 ("\current unit must also have parent&!",
2962 Item, Child_Parent);
2963 end if;
2965 elsif Curr_Private
2966 or else Private_Present (Item)
2967 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2968 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2969 and then not Acts_As_Spec (Parent (Lib_Unit)))
2970 then
2971 null;
2973 else
2974 Error_Msg_NE
2975 ("current unit must also be private descendant of&",
2976 Item, Child_Parent);
2977 end if;
2978 end;
2979 end if;
2981 Next (Item);
2982 end loop;
2984 end Check_Private_Child_Unit;
2986 ----------------------
2987 -- Check_Stub_Level --
2988 ----------------------
2990 procedure Check_Stub_Level (N : Node_Id) is
2991 Par : constant Node_Id := Parent (N);
2992 Kind : constant Node_Kind := Nkind (Par);
2994 begin
2995 if Nkind_In (Kind, N_Package_Body,
2996 N_Subprogram_Body,
2997 N_Task_Body,
2998 N_Protected_Body)
2999 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
3000 then
3001 null;
3003 -- In an instance, a missing stub appears at any level. A warning
3004 -- message will have been emitted already for the missing file.
3006 elsif not In_Instance then
3007 Error_Msg_N ("stub cannot appear in an inner scope", N);
3009 elsif Expander_Active then
3010 Error_Msg_N ("missing proper body", N);
3011 end if;
3012 end Check_Stub_Level;
3014 ------------------------
3015 -- Expand_With_Clause --
3016 ------------------------
3018 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
3019 Loc : constant Source_Ptr := Sloc (Nam);
3020 Ent : constant Entity_Id := Entity (Nam);
3021 Withn : Node_Id;
3022 P : Node_Id;
3024 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
3025 -- Build name to be used in implicit with_clause. In most cases this
3026 -- is the source name, but if renamings are present we must make the
3027 -- original unit visible, not the one it renames. The entity in the
3028 -- with clause is the renamed unit, but the identifier is the one from
3029 -- the source, which allows us to recover the unit renaming.
3031 ---------------------
3032 -- Build_Unit_Name --
3033 ---------------------
3035 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
3036 Ent : Entity_Id;
3037 Result : Node_Id;
3039 begin
3040 if Nkind (Nam) = N_Identifier then
3041 return New_Occurrence_Of (Entity (Nam), Loc);
3043 else
3044 Ent := Entity (Nam);
3046 if Present (Entity (Selector_Name (Nam)))
3047 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
3048 and then
3049 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
3050 = N_Package_Renaming_Declaration
3051 then
3052 -- The name in the with_clause is of the form A.B.C, and B is
3053 -- given by a renaming declaration. In that case we may not
3054 -- have analyzed the unit for B, but replaced it directly in
3055 -- lib-load with the unit it renames. We have to make A.B
3056 -- visible, so analyze the declaration for B now, in case it
3057 -- has not been done yet.
3059 Ent := Entity (Selector_Name (Nam));
3060 Analyze
3061 (Parent
3062 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3063 end if;
3065 Result :=
3066 Make_Expanded_Name (Loc,
3067 Chars => Chars (Entity (Nam)),
3068 Prefix => Build_Unit_Name (Prefix (Nam)),
3069 Selector_Name => New_Occurrence_Of (Ent, Loc));
3070 Set_Entity (Result, Ent);
3071 return Result;
3072 end if;
3073 end Build_Unit_Name;
3075 -- Start of processing for Expand_With_Clause
3077 begin
3078 Withn :=
3079 Make_With_Clause (Loc,
3080 Name => Build_Unit_Name (Nam));
3082 P := Parent (Unit_Declaration_Node (Ent));
3083 Set_Library_Unit (Withn, P);
3084 Set_Corresponding_Spec (Withn, Ent);
3085 Set_First_Name (Withn, True);
3086 Set_Implicit_With (Withn, True);
3088 -- If the unit is a package or generic package declaration, a private_
3089 -- with_clause on a child unit implies that the implicit with on the
3090 -- parent is also private.
3092 if Nkind_In (Unit (N), N_Package_Declaration,
3093 N_Generic_Package_Declaration)
3094 then
3095 Set_Private_Present (Withn, Private_Present (Item));
3096 end if;
3098 Prepend (Withn, Context_Items (N));
3099 Mark_Rewrite_Insertion (Withn);
3100 Install_Withed_Unit (Withn);
3102 -- If we have "with X.Y;", we want to recurse on "X", except in the
3103 -- unusual case where X.Y is a renaming of X. In that case, the scope
3104 -- of X will be null.
3106 if Nkind (Nam) = N_Expanded_Name
3107 and then Present (Scope (Entity (Prefix (Nam))))
3108 then
3109 Expand_With_Clause (Item, Prefix (Nam), N);
3110 end if;
3111 end Expand_With_Clause;
3113 --------------------------------
3114 -- Generate_Parent_References --
3115 --------------------------------
3117 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
3118 Pref : Node_Id;
3119 P_Name : Entity_Id := P_Id;
3121 begin
3122 if Nkind (N) = N_Subunit then
3123 Pref := Name (N);
3124 else
3125 Pref := Name (Parent (Defining_Entity (N)));
3126 end if;
3128 if Nkind (Pref) = N_Expanded_Name then
3130 -- Done already, if the unit has been compiled indirectly as
3131 -- part of the closure of its context because of inlining.
3133 return;
3134 end if;
3136 while Nkind (Pref) = N_Selected_Component loop
3137 Change_Selected_Component_To_Expanded_Name (Pref);
3138 Set_Entity (Pref, P_Name);
3139 Set_Etype (Pref, Etype (P_Name));
3140 Generate_Reference (P_Name, Pref, 'r');
3141 Pref := Prefix (Pref);
3142 P_Name := Scope (P_Name);
3143 end loop;
3145 -- The guard here on P_Name is to handle the error condition where
3146 -- the parent unit is missing because the file was not found.
3148 if Present (P_Name) then
3149 Set_Entity (Pref, P_Name);
3150 Set_Etype (Pref, Etype (P_Name));
3151 Generate_Reference (P_Name, Pref, 'r');
3152 Style.Check_Identifier (Pref, P_Name);
3153 end if;
3154 end Generate_Parent_References;
3156 ---------------------
3157 -- Has_With_Clause --
3158 ---------------------
3160 function Has_With_Clause
3161 (C_Unit : Node_Id;
3162 Pack : Entity_Id;
3163 Is_Limited : Boolean := False) return Boolean
3165 Item : Node_Id;
3167 function Named_Unit (Clause : Node_Id) return Entity_Id;
3168 -- Return the entity for the unit named in a [limited] with clause
3170 ----------------
3171 -- Named_Unit --
3172 ----------------
3174 function Named_Unit (Clause : Node_Id) return Entity_Id is
3175 begin
3176 if Nkind (Name (Clause)) = N_Selected_Component then
3177 return Entity (Selector_Name (Name (Clause)));
3178 else
3179 return Entity (Name (Clause));
3180 end if;
3181 end Named_Unit;
3183 -- Start of processing for Has_With_Clause
3185 begin
3186 if Present (Context_Items (C_Unit)) then
3187 Item := First (Context_Items (C_Unit));
3188 while Present (Item) loop
3189 if Nkind (Item) = N_With_Clause
3190 and then Limited_Present (Item) = Is_Limited
3191 and then Named_Unit (Item) = Pack
3192 then
3193 return True;
3194 end if;
3196 Next (Item);
3197 end loop;
3198 end if;
3200 return False;
3201 end Has_With_Clause;
3203 -----------------------------
3204 -- Implicit_With_On_Parent --
3205 -----------------------------
3207 procedure Implicit_With_On_Parent
3208 (Child_Unit : Node_Id;
3209 N : Node_Id)
3211 Loc : constant Source_Ptr := Sloc (N);
3212 P : constant Node_Id := Parent_Spec (Child_Unit);
3213 P_Unit : Node_Id := Unit (P);
3214 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3215 Withn : Node_Id;
3217 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3218 -- Build prefix of child unit name. Recurse if needed
3220 function Build_Unit_Name return Node_Id;
3221 -- If the unit is a child unit, build qualified name with all ancestors
3223 -------------------------
3224 -- Build_Ancestor_Name --
3225 -------------------------
3227 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3228 P_Ref : constant Node_Id :=
3229 New_Occurrence_Of (Defining_Entity (P), Loc);
3230 P_Spec : Node_Id := P;
3232 begin
3233 -- Ancestor may have been rewritten as a package body. Retrieve
3234 -- the original spec to trace earlier ancestors.
3236 if Nkind (P) = N_Package_Body
3237 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3238 then
3239 P_Spec := Original_Node (P);
3240 end if;
3242 if No (Parent_Spec (P_Spec)) then
3243 return P_Ref;
3244 else
3245 return
3246 Make_Selected_Component (Loc,
3247 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3248 Selector_Name => P_Ref);
3249 end if;
3250 end Build_Ancestor_Name;
3252 ---------------------
3253 -- Build_Unit_Name --
3254 ---------------------
3256 function Build_Unit_Name return Node_Id is
3257 Result : Node_Id;
3259 begin
3260 if No (Parent_Spec (P_Unit)) then
3261 return New_Occurrence_Of (P_Name, Loc);
3263 else
3264 Result :=
3265 Make_Expanded_Name (Loc,
3266 Chars => Chars (P_Name),
3267 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3268 Selector_Name => New_Occurrence_Of (P_Name, Loc));
3269 Set_Entity (Result, P_Name);
3270 return Result;
3271 end if;
3272 end Build_Unit_Name;
3274 -- Start of processing for Implicit_With_On_Parent
3276 begin
3277 -- The unit of the current compilation may be a package body that
3278 -- replaces an instance node. In this case we need the original instance
3279 -- node to construct the proper parent name.
3281 if Nkind (P_Unit) = N_Package_Body
3282 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3283 then
3284 P_Unit := Original_Node (P_Unit);
3285 end if;
3287 -- We add the implicit with if the child unit is the current unit being
3288 -- compiled. If the current unit is a body, we do not want to add an
3289 -- implicit_with a second time to the corresponding spec.
3291 if Nkind (Child_Unit) = N_Package_Declaration
3292 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3293 then
3294 return;
3295 end if;
3297 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3299 Set_Library_Unit (Withn, P);
3300 Set_Corresponding_Spec (Withn, P_Name);
3301 Set_First_Name (Withn, True);
3302 Set_Implicit_With (Withn, True);
3304 -- Node is placed at the beginning of the context items, so that
3305 -- subsequent use clauses on the parent can be validated.
3307 Prepend (Withn, Context_Items (N));
3308 Mark_Rewrite_Insertion (Withn);
3309 Install_Withed_Unit (Withn);
3311 if Is_Child_Spec (P_Unit) then
3312 Implicit_With_On_Parent (P_Unit, N);
3313 end if;
3314 end Implicit_With_On_Parent;
3316 --------------
3317 -- In_Chain --
3318 --------------
3320 function In_Chain (E : Entity_Id) return Boolean is
3321 H : Entity_Id;
3323 begin
3324 H := Current_Entity (E);
3325 while Present (H) loop
3326 if H = E then
3327 return True;
3328 else
3329 H := Homonym (H);
3330 end if;
3331 end loop;
3333 return False;
3334 end In_Chain;
3336 ---------------------
3337 -- Install_Context --
3338 ---------------------
3340 procedure Install_Context (N : Node_Id) is
3341 Lib_Unit : constant Node_Id := Unit (N);
3343 begin
3344 Install_Context_Clauses (N);
3346 if Is_Child_Spec (Lib_Unit) then
3347 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3348 end if;
3350 Install_Limited_Context_Clauses (N);
3351 end Install_Context;
3353 -----------------------------
3354 -- Install_Context_Clauses --
3355 -----------------------------
3357 procedure Install_Context_Clauses (N : Node_Id) is
3358 Lib_Unit : constant Node_Id := Unit (N);
3359 Item : Node_Id;
3360 Uname_Node : Entity_Id;
3361 Check_Private : Boolean := False;
3362 Decl_Node : Node_Id;
3363 Lib_Parent : Entity_Id;
3365 begin
3366 -- First skip configuration pragmas at the start of the context. They
3367 -- are not technically part of the context clause, but that's where the
3368 -- parser puts them. Note they were analyzed in Analyze_Context.
3370 Item := First (Context_Items (N));
3371 while Present (Item)
3372 and then Nkind (Item) = N_Pragma
3373 and then Pragma_Name (Item) in Configuration_Pragma_Names
3374 loop
3375 Next (Item);
3376 end loop;
3378 -- Loop through the actual context clause items. We process everything
3379 -- except Limited_With clauses in this routine. Limited_With clauses
3380 -- are separately installed (see Install_Limited_Context_Clauses).
3382 while Present (Item) loop
3384 -- Case of explicit WITH clause
3386 if Nkind (Item) = N_With_Clause
3387 and then not Implicit_With (Item)
3388 then
3389 if Limited_Present (Item) then
3391 -- Limited withed units will be installed later
3393 goto Continue;
3395 -- If Name (Item) is not an entity name, something is wrong, and
3396 -- this will be detected in due course, for now ignore the item
3398 elsif not Is_Entity_Name (Name (Item)) then
3399 goto Continue;
3401 elsif No (Entity (Name (Item))) then
3402 Set_Entity (Name (Item), Any_Id);
3403 goto Continue;
3404 end if;
3406 Uname_Node := Entity (Name (Item));
3408 if Is_Private_Descendant (Uname_Node) then
3409 Check_Private := True;
3410 end if;
3412 Install_Withed_Unit (Item);
3414 Decl_Node := Unit_Declaration_Node (Uname_Node);
3416 -- If the unit is a subprogram instance, it appears nested within
3417 -- a package that carries the parent information.
3419 if Is_Generic_Instance (Uname_Node)
3420 and then Ekind (Uname_Node) /= E_Package
3421 then
3422 Decl_Node := Parent (Parent (Decl_Node));
3423 end if;
3425 if Is_Child_Spec (Decl_Node) then
3426 if Nkind (Name (Item)) = N_Expanded_Name then
3427 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3428 else
3429 -- If not an expanded name, the child unit must be a
3430 -- renaming, nothing to do.
3432 null;
3433 end if;
3435 elsif Nkind (Decl_Node) = N_Subprogram_Body
3436 and then not Acts_As_Spec (Parent (Decl_Node))
3437 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3438 then
3439 Implicit_With_On_Parent
3440 (Unit (Library_Unit (Parent (Decl_Node))), N);
3441 end if;
3443 -- Check license conditions unless this is a dummy unit
3445 if Sloc (Library_Unit (Item)) /= No_Location then
3446 License_Check : declare
3447 Withu : constant Unit_Number_Type :=
3448 Get_Source_Unit (Library_Unit (Item));
3449 Withl : constant License_Type :=
3450 License (Source_Index (Withu));
3451 Unitl : constant License_Type :=
3452 License (Source_Index (Current_Sem_Unit));
3454 procedure License_Error;
3455 -- Signal error of bad license
3457 -------------------
3458 -- License_Error --
3459 -------------------
3461 procedure License_Error is
3462 begin
3463 Error_Msg_N
3464 ("license of withed unit & may be inconsistent??",
3465 Name (Item));
3466 end License_Error;
3468 -- Start of processing for License_Check
3470 begin
3471 -- Exclude license check if withed unit is an internal unit.
3472 -- This situation arises e.g. with the GPL version of GNAT.
3474 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3475 null;
3477 -- Otherwise check various cases
3478 else
3479 case Unitl is
3480 when Unknown =>
3481 null;
3483 when Restricted =>
3484 if Withl = GPL then
3485 License_Error;
3486 end if;
3488 when GPL =>
3489 if Withl = Restricted then
3490 License_Error;
3491 end if;
3493 when Modified_GPL =>
3494 if Withl = Restricted or else Withl = GPL then
3495 License_Error;
3496 end if;
3498 when Unrestricted =>
3499 null;
3500 end case;
3501 end if;
3502 end License_Check;
3503 end if;
3505 -- Case of USE PACKAGE clause
3507 elsif Nkind (Item) = N_Use_Package_Clause then
3508 Analyze_Use_Package (Item);
3510 -- Case of USE TYPE clause
3512 elsif Nkind (Item) = N_Use_Type_Clause then
3513 Analyze_Use_Type (Item);
3515 -- case of PRAGMA
3517 elsif Nkind (Item) = N_Pragma then
3518 Analyze (Item);
3519 end if;
3521 <<Continue>>
3522 Next (Item);
3523 end loop;
3525 if Is_Child_Spec (Lib_Unit) then
3527 -- The unit also has implicit with_clauses on its own parents
3529 if No (Context_Items (N)) then
3530 Set_Context_Items (N, New_List);
3531 end if;
3533 Implicit_With_On_Parent (Lib_Unit, N);
3534 end if;
3536 -- If the unit is a body, the context of the specification must also
3537 -- be installed. That includes private with_clauses in that context.
3539 if Nkind (Lib_Unit) = N_Package_Body
3540 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3541 and then not Acts_As_Spec (N))
3542 then
3543 Install_Context (Library_Unit (N));
3545 -- Only install private with-clauses of a spec that comes from
3546 -- source, excluding specs created for a subprogram body that is
3547 -- a child unit.
3549 if Comes_From_Source (Library_Unit (N)) then
3550 Install_Private_With_Clauses
3551 (Defining_Entity (Unit (Library_Unit (N))));
3552 end if;
3554 if Is_Child_Spec (Unit (Library_Unit (N))) then
3556 -- If the unit is the body of a public child unit, the private
3557 -- declarations of the parent must be made visible. If the child
3558 -- unit is private, the private declarations have been installed
3559 -- already in the call to Install_Parents for the spec. Installing
3560 -- private declarations must be done for all ancestors of public
3561 -- child units. In addition, sibling units mentioned in the
3562 -- context clause of the body are directly visible.
3564 declare
3565 Lib_Spec : Node_Id;
3566 P : Node_Id;
3567 P_Name : Entity_Id;
3569 begin
3570 Lib_Spec := Unit (Library_Unit (N));
3571 while Is_Child_Spec (Lib_Spec) loop
3572 P := Unit (Parent_Spec (Lib_Spec));
3573 P_Name := Defining_Entity (P);
3575 if not (Private_Present (Parent (Lib_Spec)))
3576 and then not In_Private_Part (P_Name)
3577 then
3578 Install_Private_Declarations (P_Name);
3579 Install_Private_With_Clauses (P_Name);
3580 Set_Use (Private_Declarations (Specification (P)));
3581 end if;
3583 Lib_Spec := P;
3584 end loop;
3585 end;
3586 end if;
3588 -- For a package body, children in context are immediately visible
3590 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3591 end if;
3593 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3594 N_Generic_Subprogram_Declaration,
3595 N_Package_Declaration,
3596 N_Subprogram_Declaration)
3597 then
3598 if Is_Child_Spec (Lib_Unit) then
3599 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3600 Set_Is_Private_Descendant
3601 (Defining_Entity (Lib_Unit),
3602 Is_Private_Descendant (Lib_Parent)
3603 or else Private_Present (Parent (Lib_Unit)));
3605 else
3606 Set_Is_Private_Descendant
3607 (Defining_Entity (Lib_Unit),
3608 Private_Present (Parent (Lib_Unit)));
3609 end if;
3610 end if;
3612 if Check_Private then
3613 Check_Private_Child_Unit (N);
3614 end if;
3615 end Install_Context_Clauses;
3617 -------------------------------------
3618 -- Install_Limited_Context_Clauses --
3619 -------------------------------------
3621 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3622 Item : Node_Id;
3624 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3625 -- Check that the unlimited view of a given compilation_unit is not
3626 -- already visible through "use + renamings".
3628 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3629 -- Check that if a limited_with clause of a given compilation_unit
3630 -- mentions a descendant of a private child of some library unit, then
3631 -- the given compilation_unit must be the declaration of a private
3632 -- descendant of that library unit, or a public descendant of such. The
3633 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3634 -- use entities on the limited with_clauses because their units have not
3635 -- been analyzed, so we have to climb the tree of ancestors looking for
3636 -- private keywords.
3638 procedure Expand_Limited_With_Clause
3639 (Comp_Unit : Node_Id;
3640 Nam : Node_Id;
3641 N : Node_Id);
3642 -- If a child unit appears in a limited_with clause, there are implicit
3643 -- limited_with clauses on all parents that are not already visible
3644 -- through a regular with clause. This procedure creates the implicit
3645 -- limited with_clauses for the parents and loads the corresponding
3646 -- units. The shadow entities are created when the inserted clause is
3647 -- analyzed. Implements Ada 2005 (AI-50217).
3649 ---------------------
3650 -- Check_Renamings --
3651 ---------------------
3653 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3654 Item : Node_Id;
3655 Spec : Node_Id;
3656 WEnt : Entity_Id;
3657 Nam : Node_Id;
3658 E : Entity_Id;
3659 E2 : Entity_Id;
3661 begin
3662 pragma Assert (Nkind (W) = N_With_Clause);
3664 -- Protect the frontend against previous critical errors
3666 case Nkind (Unit (Library_Unit (W))) is
3667 when N_Generic_Package_Declaration
3668 | N_Generic_Subprogram_Declaration
3669 | N_Package_Declaration
3670 | N_Subprogram_Declaration
3672 null;
3674 when others =>
3675 return;
3676 end case;
3678 -- Check "use + renamings"
3680 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3681 Spec := Specification (Unit (P));
3683 Item := First (Visible_Declarations (Spec));
3684 while Present (Item) loop
3686 -- Look only at use package clauses
3688 if Nkind (Item) = N_Use_Package_Clause then
3690 -- Traverse the list of packages
3692 Nam := First (Names (Item));
3693 while Present (Nam) loop
3694 E := Entity (Nam);
3696 pragma Assert (Present (Parent (E)));
3698 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3699 and then Renamed_Entity (E) = WEnt
3700 then
3701 -- The unlimited view is visible through use clause and
3702 -- renamings. There is no need to generate the error
3703 -- message here because Is_Visible_Through_Renamings
3704 -- takes care of generating the precise error message.
3706 return;
3708 elsif Nkind (Parent (E)) = N_Package_Specification then
3710 -- The use clause may refer to a local package.
3711 -- Check all the enclosing scopes.
3713 E2 := E;
3714 while E2 /= Standard_Standard and then E2 /= WEnt loop
3715 E2 := Scope (E2);
3716 end loop;
3718 if E2 = WEnt then
3719 Error_Msg_N
3720 ("unlimited view visible through use clause ", W);
3721 return;
3722 end if;
3723 end if;
3725 Next (Nam);
3726 end loop;
3727 end if;
3729 Next (Item);
3730 end loop;
3732 -- Recursive call to check all the ancestors
3734 if Is_Child_Spec (Unit (P)) then
3735 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3736 end if;
3737 end Check_Renamings;
3739 ---------------------------------------
3740 -- Check_Private_Limited_Withed_Unit --
3741 ---------------------------------------
3743 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3744 Curr_Parent : Node_Id;
3745 Child_Parent : Node_Id;
3746 Curr_Private : Boolean;
3748 begin
3749 -- Compilation unit of the parent of the withed library unit
3751 Child_Parent := Library_Unit (Item);
3753 -- If the child unit is a public child, then locate its nearest
3754 -- private ancestor, if any, then Child_Parent will then be set to
3755 -- the parent of that ancestor.
3757 if not Private_Present (Library_Unit (Item)) then
3758 while Present (Child_Parent)
3759 and then not Private_Present (Child_Parent)
3760 loop
3761 Child_Parent := Parent_Spec (Unit (Child_Parent));
3762 end loop;
3764 if No (Child_Parent) then
3765 return;
3766 end if;
3767 end if;
3769 Child_Parent := Parent_Spec (Unit (Child_Parent));
3771 -- Traverse all the ancestors of the current compilation unit to
3772 -- check if it is a descendant of named library unit.
3774 Curr_Parent := Parent (Item);
3775 Curr_Private := Private_Present (Curr_Parent);
3777 while Present (Parent_Spec (Unit (Curr_Parent)))
3778 and then Curr_Parent /= Child_Parent
3779 loop
3780 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3781 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3782 end loop;
3784 if Curr_Parent /= Child_Parent then
3785 Error_Msg_N
3786 ("unit in with clause is private child unit!", Item);
3787 Error_Msg_NE
3788 ("\current unit must also have parent&!",
3789 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3791 elsif Private_Present (Parent (Item))
3792 or else Curr_Private
3793 or else Private_Present (Item)
3794 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3795 N_Subprogram_Body,
3796 N_Subunit)
3797 then
3798 -- Current unit is private, of descendant of a private unit
3800 null;
3802 else
3803 Error_Msg_NE
3804 ("current unit must also be private descendant of&",
3805 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3806 end if;
3807 end Check_Private_Limited_Withed_Unit;
3809 --------------------------------
3810 -- Expand_Limited_With_Clause --
3811 --------------------------------
3813 procedure Expand_Limited_With_Clause
3814 (Comp_Unit : Node_Id;
3815 Nam : Node_Id;
3816 N : Node_Id)
3818 Loc : constant Source_Ptr := Sloc (Nam);
3819 Unum : Unit_Number_Type;
3820 Withn : Node_Id;
3822 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3823 -- Returns true if the context already includes a with_clause for
3824 -- this unit. If the with_clause is non-limited, the unit is fully
3825 -- visible and an implicit limited_with should not be created. If
3826 -- there is already a limited_with clause for W, a second one is
3827 -- simply redundant.
3829 --------------------------
3830 -- Previous_Withed_Unit --
3831 --------------------------
3833 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3834 Item : Node_Id;
3836 begin
3837 -- A limited with_clause cannot appear in the same context_clause
3838 -- as a nonlimited with_clause which mentions the same library.
3840 Item := First (Context_Items (Comp_Unit));
3841 while Present (Item) loop
3842 if Nkind (Item) = N_With_Clause
3843 and then Library_Unit (Item) = Library_Unit (W)
3844 then
3845 return True;
3846 end if;
3848 Next (Item);
3849 end loop;
3851 return False;
3852 end Previous_Withed_Unit;
3854 -- Start of processing for Expand_Limited_With_Clause
3856 begin
3857 if Nkind (Nam) = N_Identifier then
3859 -- Create node for name of withed unit
3861 Withn :=
3862 Make_With_Clause (Loc,
3863 Name => New_Copy (Nam));
3865 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3866 Withn :=
3867 Make_With_Clause (Loc,
3868 Name => Make_Selected_Component (Loc,
3869 Prefix => New_Copy_Tree (Prefix (Nam)),
3870 Selector_Name => New_Copy (Selector_Name (Nam))));
3871 Set_Parent (Withn, Parent (N));
3872 end if;
3874 Set_Limited_Present (Withn);
3875 Set_First_Name (Withn);
3876 Set_Implicit_With (Withn);
3878 Unum :=
3879 Load_Unit
3880 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3881 Required => True,
3882 Subunit => False,
3883 Error_Node => Nam);
3885 -- Do not generate a limited_with_clause on the current unit. This
3886 -- path is taken when a unit has a limited_with clause on one of its
3887 -- child units.
3889 if Unum = Current_Sem_Unit then
3890 return;
3891 end if;
3893 Set_Library_Unit (Withn, Cunit (Unum));
3894 Set_Corresponding_Spec
3895 (Withn, Specification (Unit (Cunit (Unum))));
3897 if not Previous_Withed_Unit (Withn) then
3898 Prepend (Withn, Context_Items (Parent (N)));
3899 Mark_Rewrite_Insertion (Withn);
3901 -- Add implicit limited_with_clauses for parents of child units
3902 -- mentioned in limited_with clauses.
3904 if Nkind (Nam) = N_Selected_Component then
3905 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3906 end if;
3908 Analyze (Withn);
3910 if not Limited_View_Installed (Withn) then
3911 Install_Limited_Withed_Unit (Withn);
3912 end if;
3913 end if;
3914 end Expand_Limited_With_Clause;
3916 -- Start of processing for Install_Limited_Context_Clauses
3918 begin
3919 Item := First (Context_Items (N));
3920 while Present (Item) loop
3921 if Nkind (Item) = N_With_Clause
3922 and then Limited_Present (Item)
3923 and then not Error_Posted (Item)
3924 then
3925 if Nkind (Name (Item)) = N_Selected_Component then
3926 Expand_Limited_With_Clause
3927 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3928 end if;
3930 Check_Private_Limited_Withed_Unit (Item);
3932 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
3933 Check_Renamings (Parent_Spec (Unit (N)), Item);
3934 end if;
3936 -- A unit may have a limited with on itself if it has a limited
3937 -- with_clause on one of its child units. In that case it is
3938 -- already being compiled and it makes no sense to install its
3939 -- limited view.
3941 -- If the item is a limited_private_with_clause, install it if the
3942 -- current unit is a body or if it is a private child. Otherwise
3943 -- the private clause is installed before analyzing the private
3944 -- part of the current unit.
3946 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3947 and then not Limited_View_Installed (Item)
3948 and then
3949 not Is_Ancestor_Unit
3950 (Library_Unit (Item), Cunit (Current_Sem_Unit))
3951 then
3952 if not Private_Present (Item)
3953 or else Private_Present (N)
3954 or else Nkind_In (Unit (N), N_Package_Body,
3955 N_Subprogram_Body,
3956 N_Subunit)
3957 then
3958 Install_Limited_Withed_Unit (Item);
3959 end if;
3960 end if;
3961 end if;
3963 Next (Item);
3964 end loop;
3966 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
3967 -- looking for incomplete subtype declarations of incomplete types
3968 -- visible through a limited with clause.
3970 if Ada_Version >= Ada_2005
3971 and then Analyzed (N)
3972 and then Nkind (Unit (N)) = N_Package_Declaration
3973 then
3974 declare
3975 Decl : Node_Id;
3976 Def_Id : Entity_Id;
3977 Non_Lim_View : Entity_Id;
3979 begin
3980 Decl := First (Visible_Declarations (Specification (Unit (N))));
3981 while Present (Decl) loop
3982 if Nkind (Decl) = N_Subtype_Declaration
3983 and then
3984 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3985 and then
3986 From_Limited_With (Defining_Identifier (Decl))
3987 then
3988 Def_Id := Defining_Identifier (Decl);
3989 Non_Lim_View := Non_Limited_View (Def_Id);
3991 if not Is_Incomplete_Type (Non_Lim_View) then
3993 -- Convert an incomplete subtype declaration into a
3994 -- corresponding non-limited view subtype declaration.
3995 -- This is usually the case when analyzing a body that
3996 -- has regular with clauses, when the spec has limited
3997 -- ones.
3999 -- If the non-limited view is still incomplete, it is
4000 -- the dummy entry already created, and the declaration
4001 -- cannot be reanalyzed. This is the case when installing
4002 -- a parent unit that has limited with-clauses.
4004 Set_Subtype_Indication (Decl,
4005 New_Occurrence_Of (Non_Lim_View, Sloc (Def_Id)));
4006 Set_Etype (Def_Id, Non_Lim_View);
4007 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
4008 Set_Analyzed (Decl, False);
4010 -- Reanalyze the declaration, suppressing the call to
4011 -- Enter_Name to avoid duplicate names.
4013 Analyze_Subtype_Declaration
4014 (N => Decl,
4015 Skip => True);
4016 end if;
4017 end if;
4019 Next (Decl);
4020 end loop;
4021 end;
4022 end if;
4023 end Install_Limited_Context_Clauses;
4025 ---------------------
4026 -- Install_Parents --
4027 ---------------------
4029 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
4030 P : Node_Id;
4031 E_Name : Entity_Id;
4032 P_Name : Entity_Id;
4033 P_Spec : Node_Id;
4035 begin
4036 P := Unit (Parent_Spec (Lib_Unit));
4037 P_Name := Get_Parent_Entity (P);
4039 if Etype (P_Name) = Any_Type then
4040 return;
4041 end if;
4043 if Ekind (P_Name) = E_Generic_Package
4044 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
4045 N_Generic_Package_Declaration)
4046 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
4047 then
4048 Error_Msg_N
4049 ("child of a generic package must be a generic unit", Lib_Unit);
4051 elsif not Is_Package_Or_Generic_Package (P_Name) then
4052 Error_Msg_N
4053 ("parent unit must be package or generic package", Lib_Unit);
4054 raise Unrecoverable_Error;
4056 elsif Present (Renamed_Object (P_Name)) then
4057 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
4058 raise Unrecoverable_Error;
4060 -- Verify that a child of an instance is itself an instance, or the
4061 -- renaming of one. Given that an instance that is a unit is replaced
4062 -- with a package declaration, check against the original node. The
4063 -- parent may be currently being instantiated, in which case it appears
4064 -- as a declaration, but the generic_parent is already established
4065 -- indicating that we deal with an instance.
4067 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
4068 if Nkind (Lib_Unit) in N_Renaming_Declaration
4069 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
4070 or else
4071 (Nkind (Lib_Unit) = N_Package_Declaration
4072 and then Present (Generic_Parent (Specification (Lib_Unit))))
4073 then
4074 null;
4075 else
4076 Error_Msg_N
4077 ("child of an instance must be an instance or renaming",
4078 Lib_Unit);
4079 end if;
4080 end if;
4082 -- This is the recursive call that ensures all parents are loaded
4084 if Is_Child_Spec (P) then
4085 Install_Parents (P,
4086 Is_Private or else Private_Present (Parent (Lib_Unit)));
4087 end if;
4089 -- Now we can install the context for this parent
4091 Install_Context_Clauses (Parent_Spec (Lib_Unit));
4092 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4093 Install_Siblings (P_Name, Parent (Lib_Unit));
4095 -- The child unit is in the declarative region of the parent. The parent
4096 -- must therefore appear in the scope stack and be visible, as when
4097 -- compiling the corresponding body. If the child unit is private or it
4098 -- is a package body, private declarations must be accessible as well.
4099 -- Use declarations in the parent must also be installed. Finally, other
4100 -- child units of the same parent that are in the context are
4101 -- immediately visible.
4103 -- Find entity for compilation unit, and set its private descendant
4104 -- status as needed. Indicate that it is a compilation unit, which is
4105 -- redundant in general, but needed if this is a generated child spec
4106 -- for a child body without previous spec.
4108 E_Name := Defining_Entity (Lib_Unit);
4110 Set_Is_Child_Unit (E_Name);
4111 Set_Is_Compilation_Unit (E_Name);
4113 Set_Is_Private_Descendant (E_Name,
4114 Is_Private_Descendant (P_Name)
4115 or else Private_Present (Parent (Lib_Unit)));
4117 P_Spec := Package_Specification (P_Name);
4118 Push_Scope (P_Name);
4120 -- Save current visibility of unit
4122 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4123 Is_Immediately_Visible (P_Name);
4124 Set_Is_Immediately_Visible (P_Name);
4125 Install_Visible_Declarations (P_Name);
4126 Set_Use (Visible_Declarations (P_Spec));
4128 -- If the parent is a generic unit, its formal part may contain formal
4129 -- packages and use clauses for them.
4131 if Ekind (P_Name) = E_Generic_Package then
4132 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4133 end if;
4135 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4136 Install_Private_Declarations (P_Name);
4137 Install_Private_With_Clauses (P_Name);
4138 Set_Use (Private_Declarations (P_Spec));
4139 end if;
4140 end Install_Parents;
4142 ----------------------------------
4143 -- Install_Private_With_Clauses --
4144 ----------------------------------
4146 procedure Install_Private_With_Clauses (P : Entity_Id) is
4147 Decl : constant Node_Id := Unit_Declaration_Node (P);
4148 Item : Node_Id;
4150 begin
4151 if Debug_Flag_I then
4152 Write_Str ("install private with clauses of ");
4153 Write_Name (Chars (P));
4154 Write_Eol;
4155 end if;
4157 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4158 Item := First (Context_Items (Parent (Decl)));
4159 while Present (Item) loop
4160 if Nkind (Item) = N_With_Clause
4161 and then Private_Present (Item)
4162 then
4163 -- If the unit is an ancestor of the current one, it is the
4164 -- case of a private limited with clause on a child unit, and
4165 -- the compilation of one of its descendants, In that case the
4166 -- limited view is errelevant.
4168 if Limited_Present (Item) then
4169 if not Limited_View_Installed (Item)
4170 and then
4171 not Is_Ancestor_Unit (Library_Unit (Item),
4172 Cunit (Current_Sem_Unit))
4173 then
4174 Install_Limited_Withed_Unit (Item);
4175 end if;
4176 else
4177 Install_Withed_Unit (Item, Private_With_OK => True);
4178 end if;
4179 end if;
4181 Next (Item);
4182 end loop;
4183 end if;
4184 end Install_Private_With_Clauses;
4186 ----------------------
4187 -- Install_Siblings --
4188 ----------------------
4190 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4191 Item : Node_Id;
4192 Id : Entity_Id;
4193 Prev : Entity_Id;
4195 begin
4196 -- Iterate over explicit with clauses, and check whether the scope of
4197 -- each entity is an ancestor of the current unit, in which case it is
4198 -- immediately visible.
4200 Item := First (Context_Items (N));
4201 while Present (Item) loop
4203 -- Do not install private_with_clauses declaration, unless unit
4204 -- is itself a private child unit, or is a body. Note that for a
4205 -- subprogram body the private_with_clause does not take effect until
4206 -- after the specification.
4208 if Nkind (Item) /= N_With_Clause
4209 or else Implicit_With (Item)
4210 or else Limited_Present (Item)
4211 or else Error_Posted (Item)
4212 then
4213 null;
4215 elsif not Private_Present (Item)
4216 or else Private_Present (N)
4217 or else Nkind (Unit (N)) = N_Package_Body
4218 then
4219 Id := Entity (Name (Item));
4221 if Is_Child_Unit (Id)
4222 and then Is_Ancestor_Package (Scope (Id), U_Name)
4223 then
4224 Set_Is_Immediately_Visible (Id);
4226 -- Check for the presence of another unit in the context that
4227 -- may be inadvertently hidden by the child.
4229 Prev := Current_Entity (Id);
4231 if Present (Prev)
4232 and then Is_Immediately_Visible (Prev)
4233 and then not Is_Child_Unit (Prev)
4234 then
4235 declare
4236 Clause : Node_Id;
4238 begin
4239 Clause := First (Context_Items (N));
4240 while Present (Clause) loop
4241 if Nkind (Clause) = N_With_Clause
4242 and then Entity (Name (Clause)) = Prev
4243 then
4244 Error_Msg_NE
4245 ("child unit& hides compilation unit " &
4246 "with the same name??",
4247 Name (Item), Id);
4248 exit;
4249 end if;
4251 Next (Clause);
4252 end loop;
4253 end;
4254 end if;
4256 -- The With_Clause may be on a grand-child or one of its further
4257 -- descendants, which makes a child immediately visible. Examine
4258 -- ancestry to determine whether such a child exists. For example,
4259 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4260 -- is immediately visible.
4262 elsif Is_Child_Unit (Id) then
4263 declare
4264 Par : Entity_Id;
4266 begin
4267 Par := Scope (Id);
4268 while Is_Child_Unit (Par) loop
4269 if Is_Ancestor_Package (Scope (Par), U_Name) then
4270 Set_Is_Immediately_Visible (Par);
4271 exit;
4272 end if;
4274 Par := Scope (Par);
4275 end loop;
4276 end;
4277 end if;
4279 -- If the item is a private with-clause on a child unit, the parent
4280 -- may have been installed already, but the child unit must remain
4281 -- invisible until installed in a private part or body, unless there
4282 -- is already a regular with_clause for it in the current unit.
4284 elsif Private_Present (Item) then
4285 Id := Entity (Name (Item));
4287 if Is_Child_Unit (Id) then
4288 declare
4289 Clause : Node_Id;
4291 function In_Context return Boolean;
4292 -- Scan context of current unit, to check whether there is
4293 -- a with_clause on the same unit as a private with-clause
4294 -- on a parent, in which case child unit is visible. If the
4295 -- unit is a grand-child, the same applies to its parent.
4297 ----------------
4298 -- In_Context --
4299 ----------------
4301 function In_Context return Boolean is
4302 begin
4303 Clause :=
4304 First (Context_Items (Cunit (Current_Sem_Unit)));
4305 while Present (Clause) loop
4306 if Nkind (Clause) = N_With_Clause
4307 and then Comes_From_Source (Clause)
4308 and then Is_Entity_Name (Name (Clause))
4309 and then not Private_Present (Clause)
4310 then
4311 if Entity (Name (Clause)) = Id
4312 or else
4313 (Nkind (Name (Clause)) = N_Expanded_Name
4314 and then Entity (Prefix (Name (Clause))) = Id)
4315 then
4316 return True;
4317 end if;
4318 end if;
4320 Next (Clause);
4321 end loop;
4323 return False;
4324 end In_Context;
4326 begin
4327 Set_Is_Visible_Lib_Unit (Id, In_Context);
4328 end;
4329 end if;
4330 end if;
4332 Next (Item);
4333 end loop;
4334 end Install_Siblings;
4336 ---------------------------------
4337 -- Install_Limited_Withed_Unit --
4338 ---------------------------------
4340 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4341 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4342 E : Entity_Id;
4343 P : Entity_Id;
4344 Is_Child_Package : Boolean := False;
4345 Lim_Header : Entity_Id;
4346 Lim_Typ : Entity_Id;
4348 procedure Check_Body_Required;
4349 -- A unit mentioned in a limited with_clause may not be mentioned in
4350 -- a regular with_clause, but must still be included in the current
4351 -- partition. We need to determine whether the unit needs a body, so
4352 -- that the binder can determine the name of the file to be compiled.
4353 -- Checking whether a unit needs a body can be done without semantic
4354 -- analysis, by examining the nature of the declarations in the package.
4356 function Has_Limited_With_Clause
4357 (C_Unit : Entity_Id;
4358 Pack : Entity_Id) return Boolean;
4359 -- Determine whether any package in the ancestor chain starting with
4360 -- C_Unit has a limited with clause for package Pack.
4362 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4363 -- Check if some package installed though normal with-clauses has a
4364 -- renaming declaration of package P. AARM 10.1.2(21/2).
4366 -------------------------
4367 -- Check_Body_Required --
4368 -------------------------
4370 procedure Check_Body_Required is
4371 PA : constant List_Id :=
4372 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4374 procedure Check_Declarations (Spec : Node_Id);
4375 -- Recursive procedure that does the work and checks nested packages
4377 ------------------------
4378 -- Check_Declarations --
4379 ------------------------
4381 procedure Check_Declarations (Spec : Node_Id) is
4382 Decl : Node_Id;
4383 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4385 Subp_List : constant Elist_Id := New_Elmt_List;
4387 procedure Check_Pragma_Import (P : Node_Id);
4388 -- If a pragma import applies to a previous subprogram, the
4389 -- enclosing unit may not need a body. The processing is syntactic
4390 -- and does not require a declaration to be analyzed. The code
4391 -- below also handles pragma Import when applied to a subprogram
4392 -- that renames another. In this case the pragma applies to the
4393 -- renamed entity.
4395 -- Chains of multiple renames are not handled by the code below.
4396 -- It is probably impossible to handle all cases without proper
4397 -- name resolution. In such cases the algorithm is conservative
4398 -- and will indicate that a body is needed???
4400 -------------------------
4401 -- Check_Pragma_Import --
4402 -------------------------
4404 procedure Check_Pragma_Import (P : Node_Id) is
4405 Arg : Node_Id;
4406 Prev_Id : Elmt_Id;
4407 Subp_Id : Elmt_Id;
4408 Imported : Node_Id;
4410 procedure Remove_Homonyms (E : Node_Id);
4411 -- Make one pass over list of subprograms. Called again if
4412 -- subprogram is a renaming. E is known to be an identifier.
4414 ---------------------
4415 -- Remove_Homonyms --
4416 ---------------------
4418 procedure Remove_Homonyms (E : Node_Id) is
4419 R : Entity_Id := Empty;
4420 -- Name of renamed entity, if any
4422 begin
4423 Subp_Id := First_Elmt (Subp_List);
4424 while Present (Subp_Id) loop
4425 if Chars (Node (Subp_Id)) = Chars (E) then
4426 if Nkind (Parent (Parent (Node (Subp_Id))))
4427 /= N_Subprogram_Renaming_Declaration
4428 then
4429 Prev_Id := Subp_Id;
4430 Next_Elmt (Subp_Id);
4431 Remove_Elmt (Subp_List, Prev_Id);
4432 else
4433 R := Name (Parent (Parent (Node (Subp_Id))));
4434 exit;
4435 end if;
4436 else
4437 Next_Elmt (Subp_Id);
4438 end if;
4439 end loop;
4441 if Present (R) then
4442 if Nkind (R) = N_Identifier then
4443 Remove_Homonyms (R);
4445 elsif Nkind (R) = N_Selected_Component then
4446 Remove_Homonyms (Selector_Name (R));
4448 -- Renaming of attribute
4450 else
4451 null;
4452 end if;
4453 end if;
4454 end Remove_Homonyms;
4456 -- Start of processing for Check_Pragma_Import
4458 begin
4459 -- Find name of entity in Import pragma. We have not analyzed
4460 -- the construct, so we must guard against syntax errors.
4462 Arg := Next (First (Pragma_Argument_Associations (P)));
4464 if No (Arg)
4465 or else Nkind (Expression (Arg)) /= N_Identifier
4466 then
4467 return;
4468 else
4469 Imported := Expression (Arg);
4470 end if;
4472 Remove_Homonyms (Imported);
4473 end Check_Pragma_Import;
4475 -- Start of processing for Check_Declarations
4477 begin
4478 -- Search for Elaborate Body pragma
4480 Decl := First (Visible_Declarations (Spec));
4481 while Present (Decl)
4482 and then Nkind (Decl) = N_Pragma
4483 loop
4484 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4485 Set_Body_Required (Library_Unit (N));
4486 return;
4487 end if;
4489 Next (Decl);
4490 end loop;
4492 -- Look for declarations that require the presence of a body. We
4493 -- have already skipped pragmas at the start of the list.
4495 while Present (Decl) loop
4497 -- Subprogram that comes from source means body may be needed.
4498 -- Save for subsequent examination of import pragmas.
4500 if Comes_From_Source (Decl)
4501 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4502 N_Subprogram_Renaming_Declaration,
4503 N_Generic_Subprogram_Declaration))
4504 then
4505 Append_Elmt (Defining_Entity (Decl), Subp_List);
4507 -- Package declaration of generic package declaration. We need
4508 -- to recursively examine nested declarations.
4510 elsif Nkind_In (Decl, N_Package_Declaration,
4511 N_Generic_Package_Declaration)
4512 then
4513 Check_Declarations (Specification (Decl));
4515 elsif Nkind (Decl) = N_Pragma
4516 and then Pragma_Name (Decl) = Name_Import
4517 then
4518 Check_Pragma_Import (Decl);
4519 end if;
4521 Next (Decl);
4522 end loop;
4524 -- Same set of tests for private part. In addition to subprograms
4525 -- detect the presence of Taft Amendment types (incomplete types
4526 -- completed in the body).
4528 Decl := First (Private_Declarations (Spec));
4529 while Present (Decl) loop
4530 if Comes_From_Source (Decl)
4531 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4532 N_Subprogram_Renaming_Declaration,
4533 N_Generic_Subprogram_Declaration))
4534 then
4535 Append_Elmt (Defining_Entity (Decl), Subp_List);
4537 elsif Nkind_In (Decl, N_Package_Declaration,
4538 N_Generic_Package_Declaration)
4539 then
4540 Check_Declarations (Specification (Decl));
4542 -- Collect incomplete type declarations for separate pass
4544 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4545 Append_Elmt (Decl, Incomplete_Decls);
4547 elsif Nkind (Decl) = N_Pragma
4548 and then Pragma_Name (Decl) = Name_Import
4549 then
4550 Check_Pragma_Import (Decl);
4551 end if;
4553 Next (Decl);
4554 end loop;
4556 -- Now check incomplete declarations to locate Taft amendment
4557 -- types. This can be done by examining the defining identifiers
4558 -- of type declarations without real semantic analysis.
4560 declare
4561 Inc : Elmt_Id;
4563 begin
4564 Inc := First_Elmt (Incomplete_Decls);
4565 while Present (Inc) loop
4566 Decl := Next (Node (Inc));
4567 while Present (Decl) loop
4568 if Nkind (Decl) = N_Full_Type_Declaration
4569 and then Chars (Defining_Identifier (Decl)) =
4570 Chars (Defining_Identifier (Node (Inc)))
4571 then
4572 exit;
4573 end if;
4575 Next (Decl);
4576 end loop;
4578 -- If no completion, this is a TAT, and a body is needed
4580 if No (Decl) then
4581 Set_Body_Required (Library_Unit (N));
4582 return;
4583 end if;
4585 Next_Elmt (Inc);
4586 end loop;
4587 end;
4589 -- Finally, check whether there are subprograms that still require
4590 -- a body, i.e. are not renamings or null.
4592 if not Is_Empty_Elmt_List (Subp_List) then
4593 declare
4594 Subp_Id : Elmt_Id;
4595 Spec : Node_Id;
4597 begin
4598 Subp_Id := First_Elmt (Subp_List);
4599 Spec := Parent (Node (Subp_Id));
4601 while Present (Subp_Id) loop
4602 if Nkind (Parent (Spec))
4603 = N_Subprogram_Renaming_Declaration
4604 then
4605 null;
4607 elsif Nkind (Spec) = N_Procedure_Specification
4608 and then Null_Present (Spec)
4609 then
4610 null;
4612 else
4613 Set_Body_Required (Library_Unit (N));
4614 return;
4615 end if;
4617 Next_Elmt (Subp_Id);
4618 end loop;
4619 end;
4620 end if;
4621 end Check_Declarations;
4623 -- Start of processing for Check_Body_Required
4625 begin
4626 -- If this is an imported package (Java and CIL usage) no body is
4627 -- needed. Scan list of pragmas that may follow a compilation unit
4628 -- to look for a relevant pragma Import.
4630 if Present (PA) then
4631 declare
4632 Prag : Node_Id;
4634 begin
4635 Prag := First (PA);
4636 while Present (Prag) loop
4637 if Nkind (Prag) = N_Pragma
4638 and then Get_Pragma_Id (Prag) = Pragma_Import
4639 then
4640 return;
4641 end if;
4643 Next (Prag);
4644 end loop;
4645 end;
4646 end if;
4648 Check_Declarations (Specification (P_Unit));
4649 end Check_Body_Required;
4651 -----------------------------
4652 -- Has_Limited_With_Clause --
4653 -----------------------------
4655 function Has_Limited_With_Clause
4656 (C_Unit : Entity_Id;
4657 Pack : Entity_Id) return Boolean
4659 Par : Entity_Id;
4660 Par_Unit : Node_Id;
4662 begin
4663 Par := C_Unit;
4664 while Present (Par) loop
4665 if Ekind (Par) /= E_Package then
4666 exit;
4667 end if;
4669 -- Retrieve the Compilation_Unit node for Par and determine if
4670 -- its context clauses contain a limited with for Pack.
4672 Par_Unit := Parent (Parent (Parent (Par)));
4674 if Nkind (Par_Unit) = N_Package_Declaration then
4675 Par_Unit := Parent (Par_Unit);
4676 end if;
4678 if Has_With_Clause (Par_Unit, Pack, True) then
4679 return True;
4680 end if;
4682 -- If there are more ancestors, climb up the tree, otherwise we
4683 -- are done.
4685 if Is_Child_Unit (Par) then
4686 Par := Scope (Par);
4687 else
4688 exit;
4689 end if;
4690 end loop;
4692 return False;
4693 end Has_Limited_With_Clause;
4695 ----------------------------------
4696 -- Is_Visible_Through_Renamings --
4697 ----------------------------------
4699 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4700 Kind : constant Node_Kind :=
4701 Nkind (Unit (Cunit (Current_Sem_Unit)));
4702 Aux_Unit : Node_Id;
4703 Item : Node_Id;
4704 Decl : Entity_Id;
4706 begin
4707 -- Example of the error detected by this subprogram:
4709 -- package P is
4710 -- type T is ...
4711 -- end P;
4713 -- with P;
4714 -- package Q is
4715 -- package Ren_P renames P;
4716 -- end Q;
4718 -- with Q;
4719 -- package R is ...
4721 -- limited with P; -- ERROR
4722 -- package R.C is ...
4724 Aux_Unit := Cunit (Current_Sem_Unit);
4726 loop
4727 Item := First (Context_Items (Aux_Unit));
4728 while Present (Item) loop
4729 if Nkind (Item) = N_With_Clause
4730 and then not Limited_Present (Item)
4731 and then Nkind (Unit (Library_Unit (Item))) =
4732 N_Package_Declaration
4733 then
4734 Decl :=
4735 First (Visible_Declarations
4736 (Specification (Unit (Library_Unit (Item)))));
4737 while Present (Decl) loop
4738 if Nkind (Decl) = N_Package_Renaming_Declaration
4739 and then Entity (Name (Decl)) = P
4740 then
4741 -- Generate the error message only if the current unit
4742 -- is a package declaration; in case of subprogram
4743 -- bodies and package bodies we just return True to
4744 -- indicate that the limited view must not be
4745 -- installed.
4747 if Kind = N_Package_Declaration then
4748 Error_Msg_N
4749 ("simultaneous visibility of the limited and " &
4750 "unlimited views not allowed", N);
4751 Error_Msg_Sloc := Sloc (Item);
4752 Error_Msg_NE
4753 ("\\ unlimited view of & visible through the " &
4754 "context clause #", N, P);
4755 Error_Msg_Sloc := Sloc (Decl);
4756 Error_Msg_NE ("\\ and the renaming #", N, P);
4757 end if;
4759 return True;
4760 end if;
4762 Next (Decl);
4763 end loop;
4764 end if;
4766 Next (Item);
4767 end loop;
4769 -- If it is a body not acting as spec, follow pointer to the
4770 -- corresponding spec, otherwise follow pointer to parent spec.
4772 if Present (Library_Unit (Aux_Unit))
4773 and then Nkind_In (Unit (Aux_Unit),
4774 N_Package_Body, N_Subprogram_Body)
4775 then
4776 if Aux_Unit = Library_Unit (Aux_Unit) then
4778 -- Aux_Unit is a body that acts as a spec. Clause has
4779 -- already been flagged as illegal.
4781 return False;
4783 else
4784 Aux_Unit := Library_Unit (Aux_Unit);
4785 end if;
4787 else
4788 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4789 end if;
4791 exit when No (Aux_Unit);
4792 end loop;
4794 return False;
4795 end Is_Visible_Through_Renamings;
4797 -- Start of processing for Install_Limited_Withed_Unit
4799 begin
4800 pragma Assert (not Limited_View_Installed (N));
4802 -- In case of limited with_clause on subprograms, generics, instances,
4803 -- or renamings, the corresponding error was previously posted and we
4804 -- have nothing to do here. If the file is missing altogether, it has
4805 -- no source location.
4807 if Nkind (P_Unit) /= N_Package_Declaration
4808 or else Sloc (P_Unit) = No_Location
4809 then
4810 return;
4811 end if;
4813 P := Defining_Unit_Name (Specification (P_Unit));
4815 -- Handle child packages
4817 if Nkind (P) = N_Defining_Program_Unit_Name then
4818 Is_Child_Package := True;
4819 P := Defining_Identifier (P);
4820 end if;
4822 -- Do not install the limited-view if the context of the unit is already
4823 -- available through a regular with clause.
4825 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4826 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4827 then
4828 return;
4829 end if;
4831 -- Do not install the limited-view if the full-view is already visible
4832 -- through renaming declarations.
4834 if Is_Visible_Through_Renamings (P) then
4835 return;
4836 end if;
4838 -- Do not install the limited view if this is the unit being analyzed.
4839 -- This unusual case will happen when a unit has a limited_with clause
4840 -- on one of its children. The compilation of the child forces the load
4841 -- of the parent which tries to install the limited view of the child
4842 -- again. Installing the limited view must also be disabled when
4843 -- compiling the body of the child unit.
4845 if P = Cunit_Entity (Current_Sem_Unit)
4846 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4847 and then P = Main_Unit_Entity
4848 and then Is_Ancestor_Unit
4849 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
4850 then
4851 return;
4852 end if;
4854 -- This scenario is similar to the one above, the difference is that the
4855 -- compilation of sibling Par.Sib forces the load of parent Par which
4856 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4857 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4858 -- non-limited views of all entities from Lim_Pack.
4860 -- limited with Lim_Pack; -- [1]
4861 -- package Par is ... package Lim_Pack is ...
4863 -- with Lim_Pack; -- [2]
4864 -- package Par.Sib is ... package body Par.Sib is ...
4866 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4867 -- Sem_Unit is the body of Par.Sib.
4869 if Ekind (P) = E_Package
4870 and then Ekind (Main_Unit_Entity) = E_Package
4871 and then Is_Child_Unit (Main_Unit_Entity)
4873 -- The body has a regular with clause
4875 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4876 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4878 -- One of the ancestors has a limited with clause
4880 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4881 N_Package_Specification
4882 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4883 then
4884 return;
4885 end if;
4887 -- A common use of the limited-with is to have a limited-with in the
4888 -- package spec, and a normal with in its package body. For example:
4890 -- limited with X; -- [1]
4891 -- package A is ...
4893 -- with X; -- [2]
4894 -- package body A is ...
4896 -- The compilation of A's body installs the context clauses found at [2]
4897 -- and then the context clauses of its specification (found at [1]). As
4898 -- a consequence, at [1] the specification of X has been analyzed and it
4899 -- is immediately visible. According to the semantics of limited-with
4900 -- context clauses we don't install the limited view because the full
4901 -- view of X supersedes its limited view.
4903 if Analyzed (P_Unit)
4904 and then
4905 (Is_Immediately_Visible (P)
4906 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
4907 then
4909 -- The presence of both the limited and the analyzed nonlimited view
4910 -- may also be an error, such as an illegal context for a limited
4911 -- with_clause. In that case, do not process the context item at all.
4913 if Error_Posted (N) then
4914 return;
4915 end if;
4917 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
4918 declare
4919 Item : Node_Id;
4920 begin
4921 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
4922 while Present (Item) loop
4923 if Nkind (Item) = N_With_Clause
4924 and then Comes_From_Source (Item)
4925 and then Entity (Name (Item)) = P
4926 then
4927 return;
4928 end if;
4930 Next (Item);
4931 end loop;
4932 end;
4934 -- If this is a child body, assume that the nonlimited with_clause
4935 -- appears in an ancestor. Could be refined ???
4937 if Is_Child_Unit
4938 (Defining_Entity
4939 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
4940 then
4941 return;
4942 end if;
4944 else
4946 -- If in package declaration, nonlimited view brought in from
4947 -- parent unit or some error condition.
4949 return;
4950 end if;
4951 end if;
4953 if Debug_Flag_I then
4954 Write_Str ("install limited view of ");
4955 Write_Name (Chars (P));
4956 Write_Eol;
4957 end if;
4959 -- If the unit has not been analyzed and the limited view has not been
4960 -- already installed then we install it.
4962 if not Analyzed (P_Unit) then
4963 if not In_Chain (P) then
4965 -- Minimum decoration
4967 Set_Ekind (P, E_Package);
4968 Set_Etype (P, Standard_Void_Type);
4969 Set_Scope (P, Standard_Standard);
4970 Set_Is_Visible_Lib_Unit (P);
4972 if Is_Child_Package then
4973 Set_Is_Child_Unit (P);
4974 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4975 end if;
4977 -- Place entity on visibility structure
4979 Set_Homonym (P, Current_Entity (P));
4980 Set_Current_Entity (P);
4982 if Debug_Flag_I then
4983 Write_Str (" (homonym) chain ");
4984 Write_Name (Chars (P));
4985 Write_Eol;
4986 end if;
4988 -- Install the incomplete view. The first element of the limited
4989 -- view is a header (an E_Package entity) used to reference the
4990 -- first shadow entity in the private part of the package.
4992 Lim_Header := Limited_View (P);
4993 Lim_Typ := First_Entity (Lim_Header);
4995 while Present (Lim_Typ)
4996 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4997 loop
4998 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4999 Set_Current_Entity (Lim_Typ);
5001 if Debug_Flag_I then
5002 Write_Str (" (homonym) chain ");
5003 Write_Name (Chars (Lim_Typ));
5004 Write_Eol;
5005 end if;
5007 Next_Entity (Lim_Typ);
5008 end loop;
5009 end if;
5011 -- If the unit appears in a previous regular with_clause, the regular
5012 -- entities of the public part of the withed package must be replaced
5013 -- by the shadow ones.
5015 -- This code must be kept synchronized with the code that replaces the
5016 -- shadow entities by the real entities (see body of Remove_Limited
5017 -- With_Clause); otherwise the contents of the homonym chains are not
5018 -- consistent.
5020 else
5021 -- Hide all the type entities of the public part of the package to
5022 -- avoid its usage. This is needed to cover all the subtype decla-
5023 -- rations because we do not remove them from the homonym chain.
5025 E := First_Entity (P);
5026 while Present (E) and then E /= First_Private_Entity (P) loop
5027 if Is_Type (E) then
5028 Set_Was_Hidden (E, Is_Hidden (E));
5029 Set_Is_Hidden (E);
5030 end if;
5032 Next_Entity (E);
5033 end loop;
5035 -- Replace the real entities by the shadow entities of the limited
5036 -- view. The first element of the limited view is a header that is
5037 -- used to reference the first shadow entity in the private part
5038 -- of the package. Successive elements are the limited views of the
5039 -- type (including regular incomplete types) declared in the package.
5041 Lim_Header := Limited_View (P);
5043 Lim_Typ := First_Entity (Lim_Header);
5044 while Present (Lim_Typ)
5045 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5046 loop
5047 pragma Assert (not In_Chain (Lim_Typ));
5049 -- Do not unchain nested packages and child units
5051 if Ekind (Lim_Typ) /= E_Package
5052 and then not Is_Child_Unit (Lim_Typ)
5053 then
5054 declare
5055 Prev : Entity_Id;
5057 begin
5058 Prev := Current_Entity (Lim_Typ);
5059 E := Prev;
5061 -- Replace E in the homonyms list, so that the limited view
5062 -- becomes available.
5064 -- If the non-limited view is a record with an anonymous
5065 -- self-referential component, the analysis of the record
5066 -- declaration creates an incomplete type with the same name
5067 -- in order to define an internal access type. The visible
5068 -- entity is now the incomplete type, and that is the one to
5069 -- replace in the visibility structure.
5071 if E = Non_Limited_View (Lim_Typ)
5072 or else
5073 (Ekind (E) = E_Incomplete_Type
5074 and then Full_View (E) = Non_Limited_View (Lim_Typ))
5075 then
5076 Set_Homonym (Lim_Typ, Homonym (Prev));
5077 Set_Current_Entity (Lim_Typ);
5079 else
5080 loop
5081 E := Homonym (Prev);
5083 -- E may have been removed when installing a previous
5084 -- limited_with_clause.
5086 exit when No (E);
5087 exit when E = Non_Limited_View (Lim_Typ);
5088 Prev := Homonym (Prev);
5089 end loop;
5091 if Present (E) then
5092 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
5093 Set_Homonym (Prev, Lim_Typ);
5094 end if;
5095 end if;
5096 end;
5098 if Debug_Flag_I then
5099 Write_Str (" (homonym) chain ");
5100 Write_Name (Chars (Lim_Typ));
5101 Write_Eol;
5102 end if;
5103 end if;
5105 Next_Entity (Lim_Typ);
5106 end loop;
5107 end if;
5109 -- The package must be visible while the limited-with clause is active
5110 -- because references to the type P.T must resolve in the usual way.
5111 -- In addition, we remember that the limited-view has been installed to
5112 -- uninstall it at the point of context removal.
5114 Set_Is_Immediately_Visible (P);
5115 Set_Limited_View_Installed (N);
5117 -- If unit has not been analyzed in some previous context, check
5118 -- (imperfectly ???) whether it might need a body.
5120 if not Analyzed (P_Unit) then
5121 Check_Body_Required;
5122 end if;
5124 -- If the package in the limited_with clause is a child unit, the clause
5125 -- is unanalyzed and appears as a selected component. Recast it as an
5126 -- expanded name so that the entity can be properly set. Use entity of
5127 -- parent, if available, for higher ancestors in the name.
5129 if Nkind (Name (N)) = N_Selected_Component then
5130 declare
5131 Nam : Node_Id;
5132 Ent : Entity_Id;
5134 begin
5135 Nam := Name (N);
5136 Ent := P;
5137 while Nkind (Nam) = N_Selected_Component
5138 and then Present (Ent)
5139 loop
5140 Change_Selected_Component_To_Expanded_Name (Nam);
5142 -- Set entity of parent identifiers if the unit is a child
5143 -- unit. This ensures that the tree is properly formed from
5144 -- semantic point of view (e.g. for ASIS queries). The unit
5145 -- entities are not fully analyzed, so we need to follow unit
5146 -- links in the tree.
5148 Set_Entity (Nam, Ent);
5150 Nam := Prefix (Nam);
5151 Ent :=
5152 Defining_Entity
5153 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5155 -- Set entity of last ancestor
5157 if Nkind (Nam) = N_Identifier then
5158 Set_Entity (Nam, Ent);
5159 end if;
5160 end loop;
5161 end;
5162 end if;
5164 Set_Entity (Name (N), P);
5165 Set_From_Limited_With (P);
5166 end Install_Limited_Withed_Unit;
5168 -------------------------
5169 -- Install_Withed_Unit --
5170 -------------------------
5172 procedure Install_Withed_Unit
5173 (With_Clause : Node_Id;
5174 Private_With_OK : Boolean := False)
5176 Uname : constant Entity_Id := Entity (Name (With_Clause));
5177 P : constant Entity_Id := Scope (Uname);
5179 begin
5180 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5181 -- compiling a package declaration and the Private_With_OK flag was not
5182 -- set by the caller. These declarations will be installed later (before
5183 -- analyzing the private part of the package).
5185 if Private_Present (With_Clause)
5186 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
5187 and then not (Private_With_OK)
5188 then
5189 return;
5190 end if;
5192 if Debug_Flag_I then
5193 if Private_Present (With_Clause) then
5194 Write_Str ("install private withed unit ");
5195 else
5196 Write_Str ("install withed unit ");
5197 end if;
5199 Write_Name (Chars (Uname));
5200 Write_Eol;
5201 end if;
5203 -- We do not apply the restrictions to an internal unit unless we are
5204 -- compiling the internal unit as a main unit. This check is also
5205 -- skipped for dummy units (for missing packages).
5207 if Sloc (Uname) /= No_Location
5208 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
5209 or else Current_Sem_Unit = Main_Unit)
5210 then
5211 Check_Restricted_Unit
5212 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5213 end if;
5215 if P /= Standard_Standard then
5217 -- If the unit is not analyzed after analysis of the with clause and
5218 -- it is an instantiation then it awaits a body and is the main unit.
5219 -- Its appearance in the context of some other unit indicates a
5220 -- circular dependency (DEC suite perversity).
5222 if not Analyzed (Uname)
5223 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5224 then
5225 Error_Msg_N
5226 ("instantiation depends on itself", Name (With_Clause));
5228 elsif not Is_Visible_Lib_Unit (Uname) then
5230 -- Abandon processing in case of previous errors
5232 if No (Scope (Uname)) then
5233 Check_Error_Detected;
5234 return;
5235 end if;
5237 Set_Is_Visible_Lib_Unit (Uname);
5239 -- If the unit is a wrapper package for a compilation unit that is
5240 -- a subprogrm instance, indicate that the instance itself is a
5241 -- visible unit. This is necessary if the instance is inlined.
5243 if Is_Wrapper_Package (Uname) then
5244 Set_Is_Visible_Lib_Unit (Related_Instance (Uname));
5245 end if;
5247 -- If the child unit appears in the context of its parent, it is
5248 -- immediately visible.
5250 if In_Open_Scopes (Scope (Uname)) then
5251 Set_Is_Immediately_Visible (Uname);
5252 end if;
5254 if Is_Generic_Instance (Uname)
5255 and then Ekind (Uname) in Subprogram_Kind
5256 then
5257 -- Set flag as well on the visible entity that denotes the
5258 -- instance, which renames the current one.
5260 Set_Is_Visible_Lib_Unit
5261 (Related_Instance
5262 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5263 end if;
5265 -- The parent unit may have been installed already, and may have
5266 -- appeared in a use clause.
5268 if In_Use (Scope (Uname)) then
5269 Set_Is_Potentially_Use_Visible (Uname);
5270 end if;
5272 Set_Context_Installed (With_Clause);
5273 end if;
5275 elsif not Is_Immediately_Visible (Uname) then
5276 Set_Is_Visible_Lib_Unit (Uname);
5278 if not Private_Present (With_Clause) or else Private_With_OK then
5279 Set_Is_Immediately_Visible (Uname);
5280 end if;
5282 Set_Context_Installed (With_Clause);
5283 end if;
5285 -- A with-clause overrides a with-type clause: there are no restric-
5286 -- tions on the use of package entities.
5288 if Ekind (Uname) = E_Package then
5289 Set_From_Limited_With (Uname, False);
5290 end if;
5292 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5293 -- unit if there is a visible homograph for it declared in the same
5294 -- declarative region. This pathological case can only arise when an
5295 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5296 -- G1 has a generic child also named G2, and the context includes with_
5297 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5298 -- of I1.G2 visible as well. If the child unit is named Standard, do
5299 -- not apply the check to the Standard package itself.
5301 if Is_Child_Unit (Uname)
5302 and then Is_Visible_Lib_Unit (Uname)
5303 and then Ada_Version >= Ada_2005
5304 then
5305 declare
5306 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5307 Decl2 : Node_Id;
5308 P2 : Entity_Id;
5309 U2 : Entity_Id;
5311 begin
5312 U2 := Homonym (Uname);
5313 while Present (U2) and then U2 /= Standard_Standard loop
5314 P2 := Scope (U2);
5315 Decl2 := Unit_Declaration_Node (P2);
5317 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5318 if Is_Generic_Instance (P)
5319 and then Nkind (Decl1) = N_Package_Declaration
5320 and then Generic_Parent (Specification (Decl1)) = P2
5321 then
5322 Error_Msg_N ("illegal with_clause", With_Clause);
5323 Error_Msg_N
5324 ("\child unit has visible homograph" &
5325 " (RM 8.3(26), 10.1.1(19))",
5326 With_Clause);
5327 exit;
5329 elsif Is_Generic_Instance (P2)
5330 and then Nkind (Decl2) = N_Package_Declaration
5331 and then Generic_Parent (Specification (Decl2)) = P
5332 then
5333 -- With_clause for child unit of instance appears before
5334 -- in the context. We want to place the error message on
5335 -- it, not on the generic child unit itself.
5337 declare
5338 Prev_Clause : Node_Id;
5340 begin
5341 Prev_Clause := First (List_Containing (With_Clause));
5342 while Entity (Name (Prev_Clause)) /= U2 loop
5343 Next (Prev_Clause);
5344 end loop;
5346 pragma Assert (Present (Prev_Clause));
5347 Error_Msg_N ("illegal with_clause", Prev_Clause);
5348 Error_Msg_N
5349 ("\child unit has visible homograph" &
5350 " (RM 8.3(26), 10.1.1(19))",
5351 Prev_Clause);
5352 exit;
5353 end;
5354 end if;
5355 end if;
5357 U2 := Homonym (U2);
5358 end loop;
5359 end;
5360 end if;
5361 end Install_Withed_Unit;
5363 -------------------
5364 -- Is_Child_Spec --
5365 -------------------
5367 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5368 K : constant Node_Kind := Nkind (Lib_Unit);
5370 begin
5371 return (K in N_Generic_Declaration or else
5372 K in N_Generic_Instantiation or else
5373 K in N_Generic_Renaming_Declaration or else
5374 K = N_Package_Declaration or else
5375 K = N_Package_Renaming_Declaration or else
5376 K = N_Subprogram_Declaration or else
5377 K = N_Subprogram_Renaming_Declaration)
5378 and then Present (Parent_Spec (Lib_Unit));
5379 end Is_Child_Spec;
5381 ------------------------------------
5382 -- Is_Legal_Shadow_Entity_In_Body --
5383 ------------------------------------
5385 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5386 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5387 begin
5388 return Nkind (Unit (C_Unit)) = N_Package_Body
5389 and then
5390 Has_With_Clause
5391 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5392 end Is_Legal_Shadow_Entity_In_Body;
5394 ----------------------
5395 -- Is_Ancestor_Unit --
5396 ----------------------
5398 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5399 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5400 E2 : Entity_Id;
5401 begin
5402 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
5403 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5404 return Is_Ancestor_Package (E1, E2);
5405 else
5406 return False;
5407 end if;
5408 end Is_Ancestor_Unit;
5410 -----------------------
5411 -- Load_Needed_Body --
5412 -----------------------
5414 -- N is a generic unit named in a with clause, or else it is a unit that
5415 -- contains a generic unit or an inlined function. In order to perform an
5416 -- instantiation, the body of the unit must be present. If the unit itself
5417 -- is generic, we assume that an instantiation follows, and load & analyze
5418 -- the body unconditionally. This forces analysis of the spec as well.
5420 -- If the unit is not generic, but contains a generic unit, it is loaded on
5421 -- demand, at the point of instantiation (see ch12).
5423 procedure Load_Needed_Body
5424 (N : Node_Id;
5425 OK : out Boolean;
5426 Do_Analyze : Boolean := True)
5428 Body_Name : Unit_Name_Type;
5429 Unum : Unit_Number_Type;
5431 Save_Style_Check : constant Boolean := Opt.Style_Check;
5432 -- The loading and analysis is done with style checks off
5434 begin
5435 if not GNAT_Mode then
5436 Style_Check := False;
5437 end if;
5439 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5440 Unum :=
5441 Load_Unit
5442 (Load_Name => Body_Name,
5443 Required => False,
5444 Subunit => False,
5445 Error_Node => N,
5446 Renamings => True);
5448 if Unum = No_Unit then
5449 OK := False;
5451 else
5452 Compiler_State := Analyzing; -- reset after load
5454 if Fatal_Error (Unum) /= Error_Detected or else Try_Semantics then
5455 if Debug_Flag_L then
5456 Write_Str ("*** Loaded generic body");
5457 Write_Eol;
5458 end if;
5460 if Do_Analyze then
5461 Semantics (Cunit (Unum));
5462 end if;
5463 end if;
5465 OK := True;
5466 end if;
5468 Style_Check := Save_Style_Check;
5469 end Load_Needed_Body;
5471 -------------------------
5472 -- Build_Limited_Views --
5473 -------------------------
5475 procedure Build_Limited_Views (N : Node_Id) is
5476 Unum : constant Unit_Number_Type :=
5477 Get_Source_Unit (Library_Unit (N));
5478 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
5480 Shadow_Pack : Entity_Id;
5481 -- The corresponding shadow entity of the withed package. This entity
5482 -- offers incomplete views of packages and types as well as abstract
5483 -- views of states and variables declared within.
5485 Last_Shadow : Entity_Id := Empty;
5486 -- The last shadow entity created by routine Build_Shadow_Entity
5488 procedure Build_Shadow_Entity
5489 (Ent : Entity_Id;
5490 Scop : Entity_Id;
5491 Shadow : out Entity_Id;
5492 Is_Tagged : Boolean := False);
5493 -- Create a shadow entity that hides Ent and offers an abstract or
5494 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5495 -- should be set when Ent is a tagged type. The generated entity is
5496 -- added to Lim_Header. This routine updates the value of Last_Shadow.
5498 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
5499 -- Perform minimal decoration of a package or its corresponding shadow
5500 -- entity denoted by Ent. Scop is the proper scope.
5502 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id);
5503 -- Perform full decoration of an abstract state or its corresponding
5504 -- shadow entity denoted by Ent. Scop is the proper scope.
5506 procedure Decorate_Type
5507 (Ent : Entity_Id;
5508 Scop : Entity_Id;
5509 Is_Tagged : Boolean := False;
5510 Materialize : Boolean := False);
5511 -- Perform minimal decoration of a type or its corresponding shadow
5512 -- entity denoted by Ent. Scop is the proper scope. Flag Is_Tagged
5513 -- should be set when Ent is a tagged type. Flag Materialize should be
5514 -- set when Ent is a tagged type and its class-wide type needs to appear
5515 -- in the tree.
5517 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id);
5518 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5519 -- proper scope.
5521 procedure Process_Declarations_And_States
5522 (Pack : Entity_Id;
5523 Decls : List_Id;
5524 Scop : Entity_Id;
5525 Create_Abstract_Views : Boolean);
5526 -- Inspect the states of package Pack and declarative list Decls. Create
5527 -- shadow entities for all nested packages, states, types and variables
5528 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5529 -- be set when the abstract states and variables need to be processed.
5531 -------------------------
5532 -- Build_Shadow_Entity --
5533 -------------------------
5535 procedure Build_Shadow_Entity
5536 (Ent : Entity_Id;
5537 Scop : Entity_Id;
5538 Shadow : out Entity_Id;
5539 Is_Tagged : Boolean := False)
5541 begin
5542 Shadow := Make_Temporary (Sloc (Ent), 'Z');
5544 -- The shadow entity must share the same name and parent as the
5545 -- entity it hides.
5547 Set_Chars (Shadow, Chars (Ent));
5548 Set_Parent (Shadow, Parent (Ent));
5550 -- The abstract view of a variable is a state, not another variable
5552 if Ekind (Ent) = E_Variable then
5553 Set_Ekind (Shadow, E_Abstract_State);
5554 else
5555 Set_Ekind (Shadow, Ekind (Ent));
5556 end if;
5558 Set_Is_Internal (Shadow);
5559 Set_From_Limited_With (Shadow);
5561 -- Add the new shadow entity to the limited view of the package
5563 Last_Shadow := Shadow;
5564 Append_Entity (Shadow, Shadow_Pack);
5566 -- Perform context-specific decoration of the shadow entity
5568 if Ekind (Ent) = E_Abstract_State then
5569 Decorate_State (Shadow, Scop);
5570 Set_Non_Limited_View (Shadow, Ent);
5572 elsif Ekind (Ent) = E_Package then
5573 Decorate_Package (Shadow, Scop);
5575 elsif Is_Type (Ent) then
5576 Decorate_Type (Shadow, Scop, Is_Tagged);
5577 Set_Non_Limited_View (Shadow, Ent);
5579 if Is_Tagged then
5580 Set_Non_Limited_View
5581 (Class_Wide_Type (Shadow), Class_Wide_Type (Ent));
5582 end if;
5584 if Is_Incomplete_Or_Private_Type (Ent) then
5585 Set_Private_Dependents (Shadow, New_Elmt_List);
5586 end if;
5588 elsif Ekind (Ent) = E_Variable then
5589 Decorate_State (Shadow, Scop);
5590 Set_Non_Limited_View (Shadow, Ent);
5591 end if;
5592 end Build_Shadow_Entity;
5594 ----------------------
5595 -- Decorate_Package --
5596 ----------------------
5598 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
5599 begin
5600 Set_Ekind (Ent, E_Package);
5601 Set_Etype (Ent, Standard_Void_Type);
5602 Set_Scope (Ent, Scop);
5603 end Decorate_Package;
5605 --------------------
5606 -- Decorate_State --
5607 --------------------
5609 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id) is
5610 begin
5611 Set_Ekind (Ent, E_Abstract_State);
5612 Set_Etype (Ent, Standard_Void_Type);
5613 Set_Scope (Ent, Scop);
5614 Set_Encapsulating_State (Ent, Empty);
5615 end Decorate_State;
5617 -------------------
5618 -- Decorate_Type --
5619 -------------------
5621 procedure Decorate_Type
5622 (Ent : Entity_Id;
5623 Scop : Entity_Id;
5624 Is_Tagged : Boolean := False;
5625 Materialize : Boolean := False)
5627 CW_Typ : Entity_Id;
5629 begin
5630 -- An unanalyzed type or a shadow entity of a type is treated as an
5631 -- incomplete type, and carries the corresponding attributes.
5633 Set_Ekind (Ent, E_Incomplete_Type);
5634 Set_Etype (Ent, Ent);
5635 Set_Full_View (Ent, Empty);
5636 Set_Is_First_Subtype (Ent);
5637 Set_Scope (Ent, Scop);
5638 Set_Stored_Constraint (Ent, No_Elist);
5639 Init_Size_Align (Ent);
5641 if From_Limited_With (Ent) then
5642 Set_Private_Dependents (Ent, New_Elmt_List);
5643 end if;
5645 -- A tagged type and its corresponding shadow entity share one common
5646 -- class-wide type. The list of primitive operations for the shadow
5647 -- entity is empty.
5649 if Is_Tagged then
5650 Set_Is_Tagged_Type (Ent);
5651 Set_Direct_Primitive_Operations (Ent, New_Elmt_List);
5653 CW_Typ :=
5654 New_External_Entity
5655 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
5657 Set_Class_Wide_Type (Ent, CW_Typ);
5659 -- Set parent to be the same as the parent of the tagged type.
5660 -- We need a parent field set, and it is supposed to point to
5661 -- the declaration of the type. The tagged type declaration
5662 -- essentially declares two separate types, the tagged type
5663 -- itself and the corresponding class-wide type, so it is
5664 -- reasonable for the parent fields to point to the declaration
5665 -- in both cases.
5667 Set_Parent (CW_Typ, Parent (Ent));
5669 Set_Ekind (CW_Typ, E_Class_Wide_Type);
5670 Set_Class_Wide_Type (CW_Typ, CW_Typ);
5671 Set_Etype (CW_Typ, Ent);
5672 Set_Equivalent_Type (CW_Typ, Empty);
5673 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
5674 Set_Has_Unknown_Discriminants (CW_Typ);
5675 Set_Is_First_Subtype (CW_Typ);
5676 Set_Is_Tagged_Type (CW_Typ);
5677 Set_Materialize_Entity (CW_Typ, Materialize);
5678 Set_Scope (CW_Typ, Scop);
5679 Init_Size_Align (CW_Typ);
5680 end if;
5681 end Decorate_Type;
5683 -----------------------
5684 -- Decorate_Variable --
5685 -----------------------
5687 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id) is
5688 begin
5689 Set_Ekind (Ent, E_Variable);
5690 Set_Etype (Ent, Standard_Void_Type);
5691 Set_Scope (Ent, Scop);
5692 end Decorate_Variable;
5694 -------------------------------------
5695 -- Process_Declarations_And_States --
5696 -------------------------------------
5698 procedure Process_Declarations_And_States
5699 (Pack : Entity_Id;
5700 Decls : List_Id;
5701 Scop : Entity_Id;
5702 Create_Abstract_Views : Boolean)
5704 procedure Find_And_Process_States;
5705 -- Determine whether package Pack defines abstract state either by
5706 -- using an aspect or a pragma. If this is the case, build shadow
5707 -- entities for all abstract states of Pack.
5709 procedure Process_States (States : Elist_Id);
5710 -- Generate shadow entities for all abstract states in list States
5712 -----------------------------
5713 -- Find_And_Process_States --
5714 -----------------------------
5716 procedure Find_And_Process_States is
5717 procedure Process_State (State : Node_Id);
5718 -- Generate shadow entities for a single abstract state or
5719 -- multiple states expressed as an aggregate.
5721 -------------------
5722 -- Process_State --
5723 -------------------
5725 procedure Process_State (State : Node_Id) is
5726 Loc : constant Source_Ptr := Sloc (State);
5727 Decl : Node_Id;
5728 Dummy : Entity_Id;
5729 Elmt : Node_Id;
5730 Id : Entity_Id;
5732 begin
5733 -- Multiple abstract states appear as an aggregate
5735 if Nkind (State) = N_Aggregate then
5736 Elmt := First (Expressions (State));
5737 while Present (Elmt) loop
5738 Process_State (Elmt);
5739 Next (Elmt);
5740 end loop;
5742 return;
5744 -- A null state has no abstract view
5746 elsif Nkind (State) = N_Null then
5747 return;
5749 -- State declaration with various options appears as an
5750 -- extension aggregate.
5752 elsif Nkind (State) = N_Extension_Aggregate then
5753 Decl := Ancestor_Part (State);
5755 -- Simple state declaration
5757 elsif Nkind (State) = N_Identifier then
5758 Decl := State;
5760 -- Possibly an illegal state declaration
5762 else
5763 return;
5764 end if;
5766 -- Abstract states are elaborated when the related pragma is
5767 -- elaborated. Since the withed package is not analyzed yet,
5768 -- the entities of the abstract states are not available. To
5769 -- overcome this complication, create the entities now and
5770 -- store them in their respective declarations. The entities
5771 -- are later used by routine Create_Abstract_State to declare
5772 -- and enter the states into visibility.
5774 if No (Entity (Decl)) then
5775 Id := Make_Defining_Identifier (Loc, Chars (Decl));
5777 Set_Entity (Decl, Id);
5778 Set_Parent (Id, State);
5779 Decorate_State (Id, Scop);
5781 -- Otherwise the package was previously withed
5783 else
5784 Id := Entity (Decl);
5785 end if;
5787 Build_Shadow_Entity (Id, Scop, Dummy);
5788 end Process_State;
5790 -- Local variables
5792 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack);
5793 Asp : Node_Id;
5794 Decl : Node_Id;
5796 -- Start of processing for Find_And_Process_States
5798 begin
5799 -- Find aspect Abstract_State
5801 Asp := First (Aspect_Specifications (Pack_Decl));
5802 while Present (Asp) loop
5803 if Chars (Identifier (Asp)) = Name_Abstract_State then
5804 Process_State (Expression (Asp));
5806 return;
5807 end if;
5809 Next (Asp);
5810 end loop;
5812 -- Find pragma Abstract_State by inspecting the declarations
5814 Decl := First (Decls);
5815 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
5816 if Pragma_Name (Decl) = Name_Abstract_State then
5817 Process_State
5818 (Get_Pragma_Arg
5819 (First (Pragma_Argument_Associations (Decl))));
5821 return;
5822 end if;
5824 Next (Decl);
5825 end loop;
5826 end Find_And_Process_States;
5828 --------------------
5829 -- Process_States --
5830 --------------------
5832 procedure Process_States (States : Elist_Id) is
5833 Dummy : Entity_Id;
5834 Elmt : Elmt_Id;
5836 begin
5837 Elmt := First_Elmt (States);
5838 while Present (Elmt) loop
5839 Build_Shadow_Entity (Node (Elmt), Scop, Dummy);
5841 Next_Elmt (Elmt);
5842 end loop;
5843 end Process_States;
5845 -- Local variables
5847 Is_Tagged : Boolean;
5848 Decl : Node_Id;
5849 Def : Node_Id;
5850 Def_Id : Entity_Id;
5851 Shadow : Entity_Id;
5853 -- Start of processing for Process_Declarations_And_States
5855 begin
5856 -- Build abstract views for all states defined in the package
5858 if Create_Abstract_Views then
5860 -- When a package has been analyzed, all states are stored in list
5861 -- Abstract_States. Generate the shadow entities directly.
5863 if Is_Analyzed then
5864 if Present (Abstract_States (Pack)) then
5865 Process_States (Abstract_States (Pack));
5866 end if;
5868 -- The package may declare abstract states by using an aspect or a
5869 -- pragma. Attempt to locate one of these construct and if found,
5870 -- build the shadow entities.
5872 else
5873 Find_And_Process_States;
5874 end if;
5875 end if;
5877 -- Inspect the declarative list, looking for nested packages, types
5878 -- and variable declarations.
5880 Decl := First (Decls);
5881 while Present (Decl) loop
5883 -- Packages
5885 if Nkind (Decl) = N_Package_Declaration then
5886 Def_Id := Defining_Entity (Decl);
5888 -- Perform minor decoration when the withed package has not
5889 -- been analyzed.
5891 if not Is_Analyzed then
5892 Decorate_Package (Def_Id, Scop);
5893 end if;
5895 -- Create a shadow entity that offers a limited view of all
5896 -- visible types declared within.
5898 Build_Shadow_Entity (Def_Id, Scop, Shadow);
5900 Process_Declarations_And_States
5901 (Pack => Def_Id,
5902 Decls => Visible_Declarations (Specification (Decl)),
5903 Scop => Shadow,
5904 Create_Abstract_Views => Create_Abstract_Views);
5906 -- Types
5908 elsif Nkind_In (Decl, N_Full_Type_Declaration,
5909 N_Incomplete_Type_Declaration,
5910 N_Private_Extension_Declaration,
5911 N_Private_Type_Declaration,
5912 N_Protected_Type_Declaration,
5913 N_Task_Type_Declaration)
5914 then
5915 Def_Id := Defining_Entity (Decl);
5917 -- Determine whether the type is tagged. Note that packages
5918 -- included via a limited with clause are not always analyzed,
5919 -- hence the tree lookup rather than the use of attribute
5920 -- Is_Tagged_Type.
5922 if Nkind (Decl) = N_Full_Type_Declaration then
5923 Def := Type_Definition (Decl);
5925 Is_Tagged :=
5926 (Nkind (Def) = N_Record_Definition
5927 and then Tagged_Present (Def))
5928 or else
5929 (Nkind (Def) = N_Derived_Type_Definition
5930 and then Present (Record_Extension_Part (Def)));
5932 elsif Nkind_In (Decl, N_Incomplete_Type_Declaration,
5933 N_Private_Type_Declaration)
5934 then
5935 Is_Tagged := Tagged_Present (Decl);
5937 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5938 Is_Tagged := True;
5940 else
5941 Is_Tagged := False;
5942 end if;
5944 -- Perform minor decoration when the withed package has not
5945 -- been analyzed.
5947 if not Is_Analyzed then
5948 Decorate_Type (Def_Id, Scop, Is_Tagged, True);
5949 end if;
5951 -- Create a shadow entity that hides the type and offers an
5952 -- incomplete view of the said type.
5954 Build_Shadow_Entity (Def_Id, Scop, Shadow, Is_Tagged);
5956 -- Variables
5958 elsif Create_Abstract_Views
5959 and then Nkind (Decl) = N_Object_Declaration
5960 and then not Constant_Present (Decl)
5961 then
5962 Def_Id := Defining_Entity (Decl);
5964 -- Perform minor decoration when the withed package has not
5965 -- been analyzed.
5967 if not Is_Analyzed then
5968 Decorate_Variable (Def_Id, Scop);
5969 end if;
5971 -- Create a shadow entity that hides the variable and offers an
5972 -- abstract view of the said variable.
5974 Build_Shadow_Entity (Def_Id, Scop, Shadow);
5975 end if;
5977 Next (Decl);
5978 end loop;
5979 end Process_Declarations_And_States;
5981 -- Local variables
5983 Nam : constant Node_Id := Name (N);
5984 Pack : constant Entity_Id := Cunit_Entity (Unum);
5986 Last_Public_Shadow : Entity_Id := Empty;
5987 Private_Shadow : Entity_Id;
5988 Spec : Node_Id;
5990 -- Start of processing for Build_Limited_Views
5992 begin
5993 pragma Assert (Limited_Present (N));
5995 -- A library_item mentioned in a limited_with_clause is a package
5996 -- declaration, not a subprogram declaration, generic declaration,
5997 -- generic instantiation, or package renaming declaration.
5999 case Nkind (Unit (Library_Unit (N))) is
6000 when N_Package_Declaration =>
6001 null;
6003 when N_Subprogram_Declaration =>
6004 Error_Msg_N ("subprograms not allowed in limited with_clauses", N);
6005 return;
6007 when N_Generic_Package_Declaration
6008 | N_Generic_Subprogram_Declaration
6010 Error_Msg_N ("generics not allowed in limited with_clauses", N);
6011 return;
6013 when N_Generic_Instantiation =>
6014 Error_Msg_N
6015 ("generic instantiations not allowed in limited with_clauses",
6017 return;
6019 when N_Generic_Renaming_Declaration =>
6020 Error_Msg_N
6021 ("generic renamings not allowed in limited with_clauses", N);
6022 return;
6024 when N_Subprogram_Renaming_Declaration =>
6025 Error_Msg_N
6026 ("renamed subprograms not allowed in limited with_clauses", N);
6027 return;
6029 when N_Package_Renaming_Declaration =>
6030 Error_Msg_N
6031 ("renamed packages not allowed in limited with_clauses", N);
6032 return;
6034 when others =>
6035 raise Program_Error;
6036 end case;
6038 -- The withed unit may not be analyzed, but the with calause itself
6039 -- must be minimally decorated. This ensures that the checks on unused
6040 -- with clauses also process limieted withs.
6042 Set_Ekind (Pack, E_Package);
6043 Set_Etype (Pack, Standard_Void_Type);
6045 if Is_Entity_Name (Nam) then
6046 Set_Entity (Nam, Pack);
6048 elsif Nkind (Nam) = N_Selected_Component then
6049 Set_Entity (Selector_Name (Nam), Pack);
6050 end if;
6052 -- Check if the chain is already built
6054 Spec := Specification (Unit (Library_Unit (N)));
6056 if Limited_View_Installed (Spec) then
6057 return;
6058 end if;
6060 -- Create the shadow package wich hides the withed unit and provides
6061 -- incomplete view of all types and packages declared within.
6063 Shadow_Pack := Make_Temporary (Sloc (N), 'Z');
6064 Set_Ekind (Shadow_Pack, E_Package);
6065 Set_Is_Internal (Shadow_Pack);
6066 Set_Limited_View (Pack, Shadow_Pack);
6068 -- Inspect the abstract states and visible declarations of the withed
6069 -- unit and create shadow entities that hide existing packages, states,
6070 -- variables and types.
6072 Process_Declarations_And_States
6073 (Pack => Pack,
6074 Decls => Visible_Declarations (Spec),
6075 Scop => Pack,
6076 Create_Abstract_Views => True);
6078 Last_Public_Shadow := Last_Shadow;
6080 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6081 -- to accomodate limited private with clauses.
6083 Process_Declarations_And_States
6084 (Pack => Pack,
6085 Decls => Private_Declarations (Spec),
6086 Scop => Pack,
6087 Create_Abstract_Views => False);
6089 if Present (Last_Public_Shadow) then
6090 Private_Shadow := Next_Entity (Last_Public_Shadow);
6091 else
6092 Private_Shadow := First_Entity (Shadow_Pack);
6093 end if;
6095 Set_First_Private_Entity (Shadow_Pack, Private_Shadow);
6096 Set_Limited_View_Installed (Spec);
6097 end Build_Limited_Views;
6099 ----------------------------
6100 -- Check_No_Elab_Code_All --
6101 ----------------------------
6103 procedure Check_No_Elab_Code_All (N : Node_Id) is
6104 begin
6105 if Present (No_Elab_Code_All_Pragma)
6106 and then In_Extended_Main_Source_Unit (N)
6107 and then Present (Context_Items (N))
6108 then
6109 declare
6110 CL : constant List_Id := Context_Items (N);
6111 CI : Node_Id;
6113 begin
6114 CI := First (CL);
6115 while Present (CI) loop
6116 if Nkind (CI) = N_With_Clause
6117 and then not
6118 No_Elab_Code_All (Get_Source_Unit (Library_Unit (CI)))
6119 then
6120 Error_Msg_Sloc := Sloc (No_Elab_Code_All_Pragma);
6121 Error_Msg_N
6122 ("violation of No_Elaboration_Code_All#", CI);
6123 Error_Msg_NE
6124 ("\unit& does not have No_Elaboration_Code_All",
6125 CI, Entity (Name (CI)));
6126 end if;
6128 Next (CI);
6129 end loop;
6130 end;
6131 end if;
6132 end Check_No_Elab_Code_All;
6134 -------------------------------
6135 -- Check_Body_Needed_For_SAL --
6136 -------------------------------
6138 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
6139 function Entity_Needs_Body (E : Entity_Id) return Boolean;
6140 -- Determine whether use of entity E might require the presence of its
6141 -- body. For a package this requires a recursive traversal of all nested
6142 -- declarations.
6144 -----------------------
6145 -- Entity_Needs_Body --
6146 -----------------------
6148 function Entity_Needs_Body (E : Entity_Id) return Boolean is
6149 Ent : Entity_Id;
6151 begin
6152 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
6153 return True;
6155 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
6157 -- A generic subprogram always requires the presence of its
6158 -- body because an instantiation needs both templates. The only
6159 -- exceptions is a generic subprogram renaming. In this case the
6160 -- body is needed only when the template is declared outside the
6161 -- compilation unit being checked.
6163 if Present (Renamed_Entity (E)) then
6164 return not Within_Scope (E, Unit_Name);
6165 else
6166 return True;
6167 end if;
6169 elsif Ekind (E) = E_Generic_Package
6170 and then
6171 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
6172 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6173 then
6174 return True;
6176 elsif Ekind (E) = E_Package
6177 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
6178 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6179 then
6180 Ent := First_Entity (E);
6181 while Present (Ent) loop
6182 if Entity_Needs_Body (Ent) then
6183 return True;
6184 end if;
6186 Next_Entity (Ent);
6187 end loop;
6189 return False;
6191 else
6192 return False;
6193 end if;
6194 end Entity_Needs_Body;
6196 -- Start of processing for Check_Body_Needed_For_SAL
6198 begin
6199 if Ekind (Unit_Name) = E_Generic_Package
6200 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6201 N_Generic_Package_Declaration
6202 and then
6203 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
6204 then
6205 Set_Body_Needed_For_SAL (Unit_Name);
6207 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
6208 Set_Body_Needed_For_SAL (Unit_Name);
6210 elsif Is_Subprogram (Unit_Name)
6211 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6212 N_Subprogram_Declaration
6213 and then Has_Pragma_Inline (Unit_Name)
6214 then
6215 Set_Body_Needed_For_SAL (Unit_Name);
6217 elsif Ekind (Unit_Name) = E_Subprogram_Body then
6218 Check_Body_Needed_For_SAL
6219 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6221 elsif Ekind (Unit_Name) = E_Package
6222 and then Entity_Needs_Body (Unit_Name)
6223 then
6224 Set_Body_Needed_For_SAL (Unit_Name);
6226 elsif Ekind (Unit_Name) = E_Package_Body
6227 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
6228 then
6229 Check_Body_Needed_For_SAL
6230 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6231 end if;
6232 end Check_Body_Needed_For_SAL;
6234 --------------------
6235 -- Remove_Context --
6236 --------------------
6238 procedure Remove_Context (N : Node_Id) is
6239 Lib_Unit : constant Node_Id := Unit (N);
6241 begin
6242 -- If this is a child unit, first remove the parent units
6244 if Is_Child_Spec (Lib_Unit) then
6245 Remove_Parents (Lib_Unit);
6246 end if;
6248 Remove_Context_Clauses (N);
6249 end Remove_Context;
6251 ----------------------------
6252 -- Remove_Context_Clauses --
6253 ----------------------------
6255 procedure Remove_Context_Clauses (N : Node_Id) is
6256 Item : Node_Id;
6257 Unit_Name : Entity_Id;
6259 begin
6260 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6261 -- limited-views first and regular-views later (to maintain the
6262 -- stack model).
6264 -- First Phase: Remove limited_with context clauses
6266 Item := First (Context_Items (N));
6267 while Present (Item) loop
6269 -- We are interested only in with clauses which got installed
6270 -- on entry.
6272 if Nkind (Item) = N_With_Clause
6273 and then Limited_Present (Item)
6274 and then Limited_View_Installed (Item)
6275 then
6276 Remove_Limited_With_Clause (Item);
6277 end if;
6279 Next (Item);
6280 end loop;
6282 -- Second Phase: Loop through context items and undo regular
6283 -- with_clauses and use_clauses.
6285 Item := First (Context_Items (N));
6286 while Present (Item) loop
6288 -- We are interested only in with clauses which got installed on
6289 -- entry, as indicated by their Context_Installed flag set
6291 if Nkind (Item) = N_With_Clause
6292 and then Limited_Present (Item)
6293 and then Limited_View_Installed (Item)
6294 then
6295 null;
6297 elsif Nkind (Item) = N_With_Clause
6298 and then Context_Installed (Item)
6299 then
6300 -- Remove items from one with'ed unit
6302 Unit_Name := Entity (Name (Item));
6303 Remove_Unit_From_Visibility (Unit_Name);
6304 Set_Context_Installed (Item, False);
6306 elsif Nkind (Item) = N_Use_Package_Clause then
6307 End_Use_Package (Item);
6309 elsif Nkind (Item) = N_Use_Type_Clause then
6310 End_Use_Type (Item);
6311 end if;
6313 Next (Item);
6314 end loop;
6315 end Remove_Context_Clauses;
6317 --------------------------------
6318 -- Remove_Limited_With_Clause --
6319 --------------------------------
6321 procedure Remove_Limited_With_Clause (N : Node_Id) is
6322 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
6323 E : Entity_Id;
6324 P : Entity_Id;
6325 Lim_Header : Entity_Id;
6326 Lim_Typ : Entity_Id;
6327 Prev : Entity_Id;
6329 begin
6330 pragma Assert (Limited_View_Installed (N));
6332 -- In case of limited with_clause on subprograms, generics, instances,
6333 -- or renamings, the corresponding error was previously posted and we
6334 -- have nothing to do here.
6336 if Nkind (P_Unit) /= N_Package_Declaration then
6337 return;
6338 end if;
6340 P := Defining_Unit_Name (Specification (P_Unit));
6342 -- Handle child packages
6344 if Nkind (P) = N_Defining_Program_Unit_Name then
6345 P := Defining_Identifier (P);
6346 end if;
6348 if Debug_Flag_I then
6349 Write_Str ("remove limited view of ");
6350 Write_Name (Chars (P));
6351 Write_Str (" from visibility");
6352 Write_Eol;
6353 end if;
6355 -- Prepare the removal of the shadow entities from visibility. The first
6356 -- element of the limited view is a header (an E_Package entity) that is
6357 -- used to reference the first shadow entity in the private part of the
6358 -- package
6360 Lim_Header := Limited_View (P);
6361 Lim_Typ := First_Entity (Lim_Header);
6363 -- Remove package and shadow entities from visibility if it has not
6364 -- been analyzed
6366 if not Analyzed (P_Unit) then
6367 Unchain (P);
6368 Set_Is_Immediately_Visible (P, False);
6370 while Present (Lim_Typ) loop
6371 Unchain (Lim_Typ);
6372 Next_Entity (Lim_Typ);
6373 end loop;
6375 -- Otherwise this package has already appeared in the closure and its
6376 -- shadow entities must be replaced by its real entities. This code
6377 -- must be kept synchronized with the complementary code in Install
6378 -- Limited_Withed_Unit.
6380 else
6381 -- If the limited_with_clause is in some other unit in the context
6382 -- then it is not visible in the main unit.
6384 if not In_Extended_Main_Source_Unit (N) then
6385 Set_Is_Immediately_Visible (P, False);
6386 end if;
6388 -- Real entities that are type or subtype declarations were hidden
6389 -- from visibility at the point of installation of the limited-view.
6390 -- Now we recover the previous value of the hidden attribute.
6392 E := First_Entity (P);
6393 while Present (E) and then E /= First_Private_Entity (P) loop
6394 if Is_Type (E) then
6395 Set_Is_Hidden (E, Was_Hidden (E));
6396 end if;
6398 Next_Entity (E);
6399 end loop;
6401 while Present (Lim_Typ)
6402 and then Lim_Typ /= First_Private_Entity (Lim_Header)
6403 loop
6404 -- Nested packages and child units were not unchained
6406 if Ekind (Lim_Typ) /= E_Package
6407 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
6408 then
6409 -- If the package has incomplete types, the limited view of the
6410 -- incomplete type is in fact never visible (AI05-129) but we
6411 -- have created a shadow entity E1 for it, that points to E2,
6412 -- a non-limited incomplete type. This in turn has a full view
6413 -- E3 that is the full declaration. There is a corresponding
6414 -- shadow entity E4. When reinstalling the non-limited view,
6415 -- E2 must become the current entity and E3 must be ignored.
6417 E := Non_Limited_View (Lim_Typ);
6419 if Present (Current_Entity (E))
6420 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
6421 and then Full_View (Current_Entity (E)) = E
6422 then
6424 -- Lim_Typ is the limited view of a full type declaration
6425 -- that has a previous incomplete declaration, i.e. E3 from
6426 -- the previous description. Nothing to insert.
6428 null;
6430 else
6431 pragma Assert (not In_Chain (E));
6433 Prev := Current_Entity (Lim_Typ);
6435 if Prev = Lim_Typ then
6436 Set_Current_Entity (E);
6438 else
6439 while Present (Prev)
6440 and then Homonym (Prev) /= Lim_Typ
6441 loop
6442 Prev := Homonym (Prev);
6443 end loop;
6445 if Present (Prev) then
6446 Set_Homonym (Prev, E);
6447 end if;
6448 end if;
6450 -- Preserve structure of homonym chain
6452 Set_Homonym (E, Homonym (Lim_Typ));
6453 end if;
6454 end if;
6456 Next_Entity (Lim_Typ);
6457 end loop;
6458 end if;
6460 -- Indicate that the limited view of the package is not installed
6462 Set_From_Limited_With (P, False);
6463 Set_Limited_View_Installed (N, False);
6464 end Remove_Limited_With_Clause;
6466 --------------------
6467 -- Remove_Parents --
6468 --------------------
6470 procedure Remove_Parents (Lib_Unit : Node_Id) is
6471 P : Node_Id;
6472 P_Name : Entity_Id;
6473 P_Spec : Node_Id := Empty;
6474 E : Entity_Id;
6475 Vis : constant Boolean :=
6476 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
6478 begin
6479 if Is_Child_Spec (Lib_Unit) then
6480 P_Spec := Parent_Spec (Lib_Unit);
6482 elsif Nkind (Lib_Unit) = N_Package_Body
6483 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6484 then
6485 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6486 end if;
6488 if Present (P_Spec) then
6489 P := Unit (P_Spec);
6490 P_Name := Get_Parent_Entity (P);
6491 Remove_Context_Clauses (P_Spec);
6492 End_Package_Scope (P_Name);
6493 Set_Is_Immediately_Visible (P_Name, Vis);
6495 -- Remove from visibility the siblings as well, which are directly
6496 -- visible while the parent is in scope.
6498 E := First_Entity (P_Name);
6499 while Present (E) loop
6500 if Is_Child_Unit (E) then
6501 Set_Is_Immediately_Visible (E, False);
6502 end if;
6504 Next_Entity (E);
6505 end loop;
6507 Set_In_Package_Body (P_Name, False);
6509 -- This is the recursive call to remove the context of any higher
6510 -- level parent. This recursion ensures that all parents are removed
6511 -- in the reverse order of their installation.
6513 Remove_Parents (P);
6514 end if;
6515 end Remove_Parents;
6517 ---------------------------------
6518 -- Remove_Private_With_Clauses --
6519 ---------------------------------
6521 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6522 Item : Node_Id;
6524 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6525 -- Check whether a given unit appears in a regular with_clause. Used to
6526 -- determine whether a private_with_clause, implicit or explicit, should
6527 -- be ignored.
6529 ----------------------------
6530 -- In_Regular_With_Clause --
6531 ----------------------------
6533 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6535 Item : Node_Id;
6537 begin
6538 Item := First (Context_Items (Comp_Unit));
6539 while Present (Item) loop
6540 if Nkind (Item) = N_With_Clause
6542 -- The following guard is needed to ensure that the name has
6543 -- been properly analyzed before we go fetching its entity.
6545 and then Is_Entity_Name (Name (Item))
6546 and then Entity (Name (Item)) = E
6547 and then not Private_Present (Item)
6548 then
6549 return True;
6550 end if;
6551 Next (Item);
6552 end loop;
6554 return False;
6555 end In_Regular_With_Clause;
6557 -- Start of processing for Remove_Private_With_Clauses
6559 begin
6560 Item := First (Context_Items (Comp_Unit));
6561 while Present (Item) loop
6562 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
6564 -- If private_with_clause is redundant, remove it from context,
6565 -- as a small optimization to subsequent handling of private_with
6566 -- clauses in other nested packages.
6568 if In_Regular_With_Clause (Entity (Name (Item))) then
6569 declare
6570 Nxt : constant Node_Id := Next (Item);
6571 begin
6572 Remove (Item);
6573 Item := Nxt;
6574 end;
6576 elsif Limited_Present (Item) then
6577 if not Limited_View_Installed (Item) then
6578 Remove_Limited_With_Clause (Item);
6579 end if;
6581 Next (Item);
6583 else
6584 Remove_Unit_From_Visibility (Entity (Name (Item)));
6585 Set_Context_Installed (Item, False);
6586 Next (Item);
6587 end if;
6589 else
6590 Next (Item);
6591 end if;
6592 end loop;
6593 end Remove_Private_With_Clauses;
6595 ---------------------------------
6596 -- Remove_Unit_From_Visibility --
6597 ---------------------------------
6599 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6600 begin
6601 if Debug_Flag_I then
6602 Write_Str ("remove unit ");
6603 Write_Name (Chars (Unit_Name));
6604 Write_Str (" from visibility");
6605 Write_Eol;
6606 end if;
6608 Set_Is_Visible_Lib_Unit (Unit_Name, False);
6609 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6610 Set_Is_Immediately_Visible (Unit_Name, False);
6612 -- If the unit is a wrapper package, the subprogram instance is
6613 -- what must be removed from visibility.
6614 -- Should we use Related_Instance instead???
6616 if Is_Wrapper_Package (Unit_Name) then
6617 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
6618 end if;
6619 end Remove_Unit_From_Visibility;
6621 --------
6622 -- sm --
6623 --------
6625 procedure sm is
6626 begin
6627 null;
6628 end sm;
6630 -------------
6631 -- Unchain --
6632 -------------
6634 procedure Unchain (E : Entity_Id) is
6635 Prev : Entity_Id;
6637 begin
6638 Prev := Current_Entity (E);
6640 if No (Prev) then
6641 return;
6643 elsif Prev = E then
6644 Set_Name_Entity_Id (Chars (E), Homonym (E));
6646 else
6647 while Present (Prev) and then Homonym (Prev) /= E loop
6648 Prev := Homonym (Prev);
6649 end loop;
6651 if Present (Prev) then
6652 Set_Homonym (Prev, Homonym (E));
6653 end if;
6654 end if;
6656 if Debug_Flag_I then
6657 Write_Str (" (homonym) unchain ");
6658 Write_Name (Chars (E));
6659 Write_Eol;
6660 end if;
6661 end Unchain;
6663 end Sem_Ch10;