PR testsuite/79036 - gcc.dg/tree-ssa/builtin-sprintf.c fails starting with r244037
[official-gcc.git] / gcc / ada / sem_ch10.adb
blob9cd1489eef172a04255c905a07d66c54566d076b
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 (Cont_Item), Name_Elaborate,
397 Name_Elaborate_All)
398 and then not Used_Type_Or_Elab
399 then
400 Prag_Unit :=
401 First (Pragma_Argument_Associations (Cont_Item));
402 while Present (Prag_Unit) and then not Used_Type_Or_Elab loop
403 if Entity (Expression (Prag_Unit)) = Nam_Ent then
404 Used_Type_Or_Elab := True;
405 end if;
407 Next (Prag_Unit);
408 end loop;
409 end if;
411 Next (Cont_Item);
412 end loop;
413 end Process_Body_Clauses;
415 --------------------------
416 -- Process_Spec_Clauses --
417 --------------------------
419 procedure Process_Spec_Clauses
420 (Context_List : List_Id;
421 Clause : Node_Id;
422 Used : 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_Mapped (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 -- If the library unit is a predefined unit, and we are in high
2536 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2537 -- for the analysis of the with'ed unit. This mode does not prevent
2538 -- explicit with'ing of run-time units.
2540 if Configurable_Run_Time_Mode
2541 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (U)))
2542 then
2543 Configurable_Run_Time_Mode := False;
2544 Semantics (Library_Unit (N));
2545 Configurable_Run_Time_Mode := True;
2547 else
2548 Semantics (Library_Unit (N));
2549 end if;
2551 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2553 if Sloc (U) /= No_Location then
2555 -- Check restrictions, except that we skip the check if this is an
2556 -- internal unit unless we are compiling the internal unit as the
2557 -- main unit. We also skip this for dummy packages.
2559 Check_Restriction_No_Dependence (Nam, N);
2561 if not Intunit or else Current_Sem_Unit = Main_Unit then
2562 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2563 end if;
2565 -- Deal with special case of GNAT.Current_Exceptions which interacts
2566 -- with the optimization of local raise statements into gotos.
2568 if Nkind (Nam) = N_Selected_Component
2569 and then Nkind (Prefix (Nam)) = N_Identifier
2570 and then Chars (Prefix (Nam)) = Name_Gnat
2571 and then Nam_In (Chars (Selector_Name (Nam)),
2572 Name_Most_Recent_Exception,
2573 Name_Exception_Traces)
2574 then
2575 Check_Restriction (No_Exception_Propagation, N);
2576 Special_Exception_Package_Used := True;
2577 end if;
2579 -- Check for inappropriate with of internal implementation unit if we
2580 -- are not compiling an internal unit and also check for withing unit
2581 -- in wrong version of Ada. Do not issue these messages for implicit
2582 -- with's generated by the compiler itself.
2584 if Implementation_Unit_Warnings
2585 and then not Intunit
2586 and then not Implicit_With (N)
2587 and then not Restriction_Violation
2588 then
2589 declare
2590 U_Kind : constant Kind_Of_Unit :=
2591 Get_Kind_Of_Unit (Get_Source_Unit (U));
2593 begin
2594 if U_Kind = Implementation_Unit then
2595 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2597 -- Add alternative name if available, otherwise issue a
2598 -- general warning message.
2600 if Error_Msg_Strlen /= 0 then
2601 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2602 else
2603 Error_Msg_F
2604 ("\use of this unit is non-portable " &
2605 "and version-dependent?i?", Name (N));
2606 end if;
2608 elsif U_Kind = Ada_2005_Unit
2609 and then Ada_Version < Ada_2005
2610 and then Warn_On_Ada_2005_Compatibility
2611 then
2612 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2614 elsif U_Kind = Ada_2012_Unit
2615 and then Ada_Version < Ada_2012
2616 and then Warn_On_Ada_2012_Compatibility
2617 then
2618 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2619 end if;
2620 end;
2621 end if;
2622 end if;
2624 -- Semantic analysis of a generic unit is performed on a copy of
2625 -- the original tree. Retrieve the entity on which semantic info
2626 -- actually appears.
2628 if Unit_Kind in N_Generic_Declaration then
2629 E_Name := Defining_Entity (U);
2631 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2632 -- the case of an instantiation, semantic analysis above will have
2633 -- replaced the unit by its instantiated version. If the instance body
2634 -- has been generated, the instance now denotes the body entity. For
2635 -- visibility purposes we need the entity of its spec.
2637 elsif (Unit_Kind = N_Package_Instantiation
2638 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2639 N_Package_Instantiation)
2640 and then Nkind (U) = N_Package_Body
2641 then
2642 E_Name := Corresponding_Spec (U);
2644 elsif Unit_Kind = N_Package_Instantiation
2645 and then Nkind (U) = N_Package_Instantiation
2646 and then Present (Instance_Spec (U))
2647 then
2648 -- If the instance has not been rewritten as a package declaration,
2649 -- then it appeared already in a previous with clause. Retrieve
2650 -- the entity from the previous instance.
2652 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2654 elsif Unit_Kind in N_Subprogram_Instantiation then
2656 -- The visible subprogram is created during instantiation, and is
2657 -- an attribute of the wrapper package. We retrieve the wrapper
2658 -- package directly from the instantiation node. If the instance
2659 -- is inlined the unit is still an instantiation. Otherwise it has
2660 -- been rewritten as the declaration of the wrapper itself.
2662 if Nkind (U) in N_Subprogram_Instantiation then
2663 E_Name :=
2664 Related_Instance
2665 (Defining_Entity (Specification (Instance_Spec (U))));
2666 else
2667 E_Name := Related_Instance (Defining_Entity (U));
2668 end if;
2670 elsif Unit_Kind = N_Package_Renaming_Declaration
2671 or else Unit_Kind in N_Generic_Renaming_Declaration
2672 then
2673 E_Name := Defining_Entity (U);
2675 elsif Unit_Kind = N_Subprogram_Body
2676 and then Nkind (Name (N)) = N_Selected_Component
2677 and then not Acts_As_Spec (Library_Unit (N))
2678 then
2679 -- For a child unit that has no spec, one has been created and
2680 -- analyzed. The entity required is that of the spec.
2682 E_Name := Corresponding_Spec (U);
2684 else
2685 E_Name := Defining_Entity (U);
2686 end if;
2688 if Nkind (Name (N)) = N_Selected_Component then
2690 -- Child unit in a with clause
2692 Change_Selected_Component_To_Expanded_Name (Name (N));
2694 -- If this is a child unit without a spec, and it has been analyzed
2695 -- already, a declaration has been created for it. The with_clause
2696 -- must reflect the actual body, and not the generated declaration,
2697 -- to prevent spurious binding errors involving an out-of-date spec.
2698 -- Note that this can only happen if the unit includes more than one
2699 -- with_clause for the child unit (e.g. in separate subunits).
2701 if Unit_Kind = N_Subprogram_Declaration
2702 and then Analyzed (Library_Unit (N))
2703 and then not Comes_From_Source (Library_Unit (N))
2704 then
2705 Set_Library_Unit (N,
2706 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2707 end if;
2708 end if;
2710 -- Restore style checks
2712 Style_Check := Save_Style_Check;
2714 -- Record the reference, but do NOT set the unit as referenced, we want
2715 -- to consider the unit as unreferenced if this is the only reference
2716 -- that occurs.
2718 Set_Entity_With_Checks (Name (N), E_Name);
2719 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2721 -- Generate references and check No_Dependence restriction for parents
2723 if Is_Child_Unit (E_Name) then
2724 Pref := Prefix (Name (N));
2725 Par_Name := Scope (E_Name);
2726 while Nkind (Pref) = N_Selected_Component loop
2727 Change_Selected_Component_To_Expanded_Name (Pref);
2729 if Present (Entity (Selector_Name (Pref)))
2730 and then
2731 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2732 and then Entity (Selector_Name (Pref)) /= Par_Name
2733 then
2734 -- The prefix is a child unit that denotes a renaming declaration.
2735 -- Replace the prefix directly with the renamed unit, because the
2736 -- rest of the prefix is irrelevant to the visibility of the real
2737 -- unit.
2739 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2740 exit;
2741 end if;
2743 Set_Entity_With_Checks (Pref, Par_Name);
2745 Generate_Reference (Par_Name, Pref);
2746 Check_Restriction_No_Dependence (Pref, N);
2747 Pref := Prefix (Pref);
2749 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2750 -- is set to Standard_Standard, and no attempt should be made to
2751 -- further unwind scopes.
2753 if Par_Name /= Standard_Standard then
2754 Par_Name := Scope (Par_Name);
2755 end if;
2757 -- Abandon processing in case of previous errors
2759 if No (Par_Name) then
2760 Check_Error_Detected;
2761 return;
2762 end if;
2763 end loop;
2765 if Present (Entity (Pref))
2766 and then not Analyzed (Parent (Parent (Entity (Pref))))
2767 then
2768 -- If the entity is set without its unit being compiled, the
2769 -- original parent is a renaming, and Par_Name is the renamed
2770 -- entity. For visibility purposes, we need the original entity,
2771 -- which must be analyzed now because Load_Unit directly retrieves
2772 -- the renamed unit, and the renaming declaration itself has not
2773 -- been analyzed.
2775 Analyze (Parent (Parent (Entity (Pref))));
2776 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2777 Par_Name := Entity (Pref);
2778 end if;
2780 -- Guard against missing or misspelled child units
2782 if Present (Par_Name) then
2783 Set_Entity_With_Checks (Pref, Par_Name);
2784 Generate_Reference (Par_Name, Pref);
2786 else
2787 pragma Assert (Serious_Errors_Detected /= 0);
2789 -- Mark the node to indicate that a related error has been posted.
2790 -- This defends further compilation passes against improper use of
2791 -- the invalid WITH clause node.
2793 Set_Error_Posted (N);
2794 Set_Name (N, Error);
2795 return;
2796 end if;
2797 end if;
2799 -- If the withed unit is System, and a system extension pragma is
2800 -- present, compile the extension now, rather than waiting for a
2801 -- visibility check on a specific entity.
2803 if Chars (E_Name) = Name_System
2804 and then Scope (E_Name) = Standard_Standard
2805 and then Present (System_Extend_Unit)
2806 and then Present_System_Aux (N)
2807 then
2808 -- If the extension is not present, an error will have been emitted
2810 null;
2811 end if;
2813 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2814 -- to private_with units; they will be made visible later (just before
2815 -- the private part is analyzed)
2817 if Private_Present (N) then
2818 Set_Is_Immediately_Visible (E_Name, False);
2819 end if;
2821 -- Propagate Fatal_Error setting from with'ed unit to current unit
2823 case Fatal_Error (Get_Source_Unit (Library_Unit (N))) is
2825 -- Nothing to do if with'ed unit had no error
2827 when None =>
2828 null;
2830 -- If with'ed unit had a detected fatal error, propagate it
2832 when Error_Detected =>
2833 Set_Fatal_Error (Current_Sem_Unit, Error_Detected);
2835 -- If with'ed unit had an ignored error, then propagate it but do not
2836 -- overide an existring setting.
2838 when Error_Ignored =>
2839 if Fatal_Error (Current_Sem_Unit) = None then
2840 Set_Fatal_Error (Current_Sem_Unit, Error_Ignored);
2841 end if;
2842 end case;
2843 end Analyze_With_Clause;
2845 ------------------------------
2846 -- Check_Private_Child_Unit --
2847 ------------------------------
2849 procedure Check_Private_Child_Unit (N : Node_Id) is
2850 Lib_Unit : constant Node_Id := Unit (N);
2851 Item : Node_Id;
2852 Curr_Unit : Entity_Id;
2853 Sub_Parent : Node_Id;
2854 Priv_Child : Entity_Id;
2855 Par_Lib : Entity_Id;
2856 Par_Spec : Node_Id;
2858 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2859 -- Returns true if and only if the library unit is declared with
2860 -- an explicit designation of private.
2862 -----------------------------
2863 -- Is_Private_Library_Unit --
2864 -----------------------------
2866 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2867 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2869 begin
2870 return Private_Present (Comp_Unit);
2871 end Is_Private_Library_Unit;
2873 -- Start of processing for Check_Private_Child_Unit
2875 begin
2876 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2877 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2878 Par_Lib := Curr_Unit;
2880 elsif Nkind (Lib_Unit) = N_Subunit then
2882 -- The parent is itself a body. The parent entity is to be found in
2883 -- the corresponding spec.
2885 Sub_Parent := Library_Unit (N);
2886 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2888 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2889 -- enclosing body, retrieve the spec entity which is the proper
2890 -- ancestor we need for the following tests.
2892 if Ekind (Curr_Unit) = E_Package_Body then
2893 Curr_Unit := Spec_Entity (Curr_Unit);
2894 end if;
2896 Par_Lib := Curr_Unit;
2898 else
2899 Curr_Unit := Defining_Entity (Lib_Unit);
2901 Par_Lib := Curr_Unit;
2902 Par_Spec := Parent_Spec (Lib_Unit);
2904 if No (Par_Spec) then
2905 Par_Lib := Empty;
2906 else
2907 Par_Lib := Defining_Entity (Unit (Par_Spec));
2908 end if;
2909 end if;
2911 -- Loop through context items
2913 Item := First (Context_Items (N));
2914 while Present (Item) loop
2916 -- Ada 2005 (AI-262): Allow private_with of a private child package
2917 -- in public siblings
2919 if Nkind (Item) = N_With_Clause
2920 and then not Implicit_With (Item)
2921 and then not Limited_Present (Item)
2922 and then Is_Private_Descendant (Entity (Name (Item)))
2923 then
2924 Priv_Child := Entity (Name (Item));
2926 declare
2927 Curr_Parent : Entity_Id := Par_Lib;
2928 Child_Parent : Entity_Id := Scope (Priv_Child);
2929 Prv_Ancestor : Entity_Id := Child_Parent;
2930 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2932 begin
2933 -- If the child unit is a public child then locate the nearest
2934 -- private ancestor. Child_Parent will then be set to the
2935 -- parent of that ancestor.
2937 if not Is_Private_Library_Unit (Priv_Child) then
2938 while Present (Prv_Ancestor)
2939 and then not Is_Private_Library_Unit (Prv_Ancestor)
2940 loop
2941 Prv_Ancestor := Scope (Prv_Ancestor);
2942 end loop;
2944 if Present (Prv_Ancestor) then
2945 Child_Parent := Scope (Prv_Ancestor);
2946 end if;
2947 end if;
2949 while Present (Curr_Parent)
2950 and then Curr_Parent /= Standard_Standard
2951 and then Curr_Parent /= Child_Parent
2952 loop
2953 Curr_Private :=
2954 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2955 Curr_Parent := Scope (Curr_Parent);
2956 end loop;
2958 if No (Curr_Parent) then
2959 Curr_Parent := Standard_Standard;
2960 end if;
2962 if Curr_Parent /= Child_Parent then
2963 if Ekind (Priv_Child) = E_Generic_Package
2964 and then Chars (Priv_Child) in Text_IO_Package_Name
2965 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2966 then
2967 Error_Msg_NE
2968 ("& is a nested package, not a compilation unit",
2969 Name (Item), Priv_Child);
2971 else
2972 Error_Msg_N
2973 ("unit in with clause is private child unit!", Item);
2974 Error_Msg_NE
2975 ("\current unit must also have parent&!",
2976 Item, Child_Parent);
2977 end if;
2979 elsif Curr_Private
2980 or else Private_Present (Item)
2981 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2982 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2983 and then not Acts_As_Spec (Parent (Lib_Unit)))
2984 then
2985 null;
2987 else
2988 Error_Msg_NE
2989 ("current unit must also be private descendant of&",
2990 Item, Child_Parent);
2991 end if;
2992 end;
2993 end if;
2995 Next (Item);
2996 end loop;
2998 end Check_Private_Child_Unit;
3000 ----------------------
3001 -- Check_Stub_Level --
3002 ----------------------
3004 procedure Check_Stub_Level (N : Node_Id) is
3005 Par : constant Node_Id := Parent (N);
3006 Kind : constant Node_Kind := Nkind (Par);
3008 begin
3009 if Nkind_In (Kind, N_Package_Body,
3010 N_Subprogram_Body,
3011 N_Task_Body,
3012 N_Protected_Body)
3013 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
3014 then
3015 null;
3017 -- In an instance, a missing stub appears at any level. A warning
3018 -- message will have been emitted already for the missing file.
3020 elsif not In_Instance then
3021 Error_Msg_N ("stub cannot appear in an inner scope", N);
3023 elsif Expander_Active then
3024 Error_Msg_N ("missing proper body", N);
3025 end if;
3026 end Check_Stub_Level;
3028 ------------------------
3029 -- Expand_With_Clause --
3030 ------------------------
3032 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
3033 Loc : constant Source_Ptr := Sloc (Nam);
3034 Ent : constant Entity_Id := Entity (Nam);
3035 Withn : Node_Id;
3036 P : Node_Id;
3038 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
3039 -- Build name to be used in implicit with_clause. In most cases this
3040 -- is the source name, but if renamings are present we must make the
3041 -- original unit visible, not the one it renames. The entity in the
3042 -- with clause is the renamed unit, but the identifier is the one from
3043 -- the source, which allows us to recover the unit renaming.
3045 ---------------------
3046 -- Build_Unit_Name --
3047 ---------------------
3049 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
3050 Ent : Entity_Id;
3051 Result : Node_Id;
3053 begin
3054 if Nkind (Nam) = N_Identifier then
3055 return New_Occurrence_Of (Entity (Nam), Loc);
3057 else
3058 Ent := Entity (Nam);
3060 if Present (Entity (Selector_Name (Nam)))
3061 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
3062 and then
3063 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
3064 = N_Package_Renaming_Declaration
3065 then
3066 -- The name in the with_clause is of the form A.B.C, and B is
3067 -- given by a renaming declaration. In that case we may not
3068 -- have analyzed the unit for B, but replaced it directly in
3069 -- lib-load with the unit it renames. We have to make A.B
3070 -- visible, so analyze the declaration for B now, in case it
3071 -- has not been done yet.
3073 Ent := Entity (Selector_Name (Nam));
3074 Analyze
3075 (Parent
3076 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3077 end if;
3079 Result :=
3080 Make_Expanded_Name (Loc,
3081 Chars => Chars (Entity (Nam)),
3082 Prefix => Build_Unit_Name (Prefix (Nam)),
3083 Selector_Name => New_Occurrence_Of (Ent, Loc));
3084 Set_Entity (Result, Ent);
3085 return Result;
3086 end if;
3087 end Build_Unit_Name;
3089 -- Start of processing for Expand_With_Clause
3091 begin
3092 Withn :=
3093 Make_With_Clause (Loc,
3094 Name => Build_Unit_Name (Nam));
3096 P := Parent (Unit_Declaration_Node (Ent));
3097 Set_Library_Unit (Withn, P);
3098 Set_Corresponding_Spec (Withn, Ent);
3099 Set_First_Name (Withn, True);
3100 Set_Implicit_With (Withn, True);
3102 -- If the unit is a package or generic package declaration, a private_
3103 -- with_clause on a child unit implies that the implicit with on the
3104 -- parent is also private.
3106 if Nkind_In (Unit (N), N_Package_Declaration,
3107 N_Generic_Package_Declaration)
3108 then
3109 Set_Private_Present (Withn, Private_Present (Item));
3110 end if;
3112 Prepend (Withn, Context_Items (N));
3113 Mark_Rewrite_Insertion (Withn);
3114 Install_Withed_Unit (Withn);
3116 -- If we have "with X.Y;", we want to recurse on "X", except in the
3117 -- unusual case where X.Y is a renaming of X. In that case, the scope
3118 -- of X will be null.
3120 if Nkind (Nam) = N_Expanded_Name
3121 and then Present (Scope (Entity (Prefix (Nam))))
3122 then
3123 Expand_With_Clause (Item, Prefix (Nam), N);
3124 end if;
3125 end Expand_With_Clause;
3127 --------------------------------
3128 -- Generate_Parent_References --
3129 --------------------------------
3131 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
3132 Pref : Node_Id;
3133 P_Name : Entity_Id := P_Id;
3135 begin
3136 if Nkind (N) = N_Subunit then
3137 Pref := Name (N);
3138 else
3139 Pref := Name (Parent (Defining_Entity (N)));
3140 end if;
3142 if Nkind (Pref) = N_Expanded_Name then
3144 -- Done already, if the unit has been compiled indirectly as
3145 -- part of the closure of its context because of inlining.
3147 return;
3148 end if;
3150 while Nkind (Pref) = N_Selected_Component loop
3151 Change_Selected_Component_To_Expanded_Name (Pref);
3152 Set_Entity (Pref, P_Name);
3153 Set_Etype (Pref, Etype (P_Name));
3154 Generate_Reference (P_Name, Pref, 'r');
3155 Pref := Prefix (Pref);
3156 P_Name := Scope (P_Name);
3157 end loop;
3159 -- The guard here on P_Name is to handle the error condition where
3160 -- the parent unit is missing because the file was not found.
3162 if Present (P_Name) then
3163 Set_Entity (Pref, P_Name);
3164 Set_Etype (Pref, Etype (P_Name));
3165 Generate_Reference (P_Name, Pref, 'r');
3166 Style.Check_Identifier (Pref, P_Name);
3167 end if;
3168 end Generate_Parent_References;
3170 ---------------------
3171 -- Has_With_Clause --
3172 ---------------------
3174 function Has_With_Clause
3175 (C_Unit : Node_Id;
3176 Pack : Entity_Id;
3177 Is_Limited : Boolean := False) return Boolean
3179 Item : Node_Id;
3181 function Named_Unit (Clause : Node_Id) return Entity_Id;
3182 -- Return the entity for the unit named in a [limited] with clause
3184 ----------------
3185 -- Named_Unit --
3186 ----------------
3188 function Named_Unit (Clause : Node_Id) return Entity_Id is
3189 begin
3190 if Nkind (Name (Clause)) = N_Selected_Component then
3191 return Entity (Selector_Name (Name (Clause)));
3192 else
3193 return Entity (Name (Clause));
3194 end if;
3195 end Named_Unit;
3197 -- Start of processing for Has_With_Clause
3199 begin
3200 if Present (Context_Items (C_Unit)) then
3201 Item := First (Context_Items (C_Unit));
3202 while Present (Item) loop
3203 if Nkind (Item) = N_With_Clause
3204 and then Limited_Present (Item) = Is_Limited
3205 and then Named_Unit (Item) = Pack
3206 then
3207 return True;
3208 end if;
3210 Next (Item);
3211 end loop;
3212 end if;
3214 return False;
3215 end Has_With_Clause;
3217 -----------------------------
3218 -- Implicit_With_On_Parent --
3219 -----------------------------
3221 procedure Implicit_With_On_Parent
3222 (Child_Unit : Node_Id;
3223 N : Node_Id)
3225 Loc : constant Source_Ptr := Sloc (N);
3226 P : constant Node_Id := Parent_Spec (Child_Unit);
3227 P_Unit : Node_Id := Unit (P);
3228 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3229 Withn : Node_Id;
3231 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3232 -- Build prefix of child unit name. Recurse if needed
3234 function Build_Unit_Name return Node_Id;
3235 -- If the unit is a child unit, build qualified name with all ancestors
3237 -------------------------
3238 -- Build_Ancestor_Name --
3239 -------------------------
3241 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3242 P_Ref : constant Node_Id :=
3243 New_Occurrence_Of (Defining_Entity (P), Loc);
3244 P_Spec : Node_Id := P;
3246 begin
3247 -- Ancestor may have been rewritten as a package body. Retrieve
3248 -- the original spec to trace earlier ancestors.
3250 if Nkind (P) = N_Package_Body
3251 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3252 then
3253 P_Spec := Original_Node (P);
3254 end if;
3256 if No (Parent_Spec (P_Spec)) then
3257 return P_Ref;
3258 else
3259 return
3260 Make_Selected_Component (Loc,
3261 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3262 Selector_Name => P_Ref);
3263 end if;
3264 end Build_Ancestor_Name;
3266 ---------------------
3267 -- Build_Unit_Name --
3268 ---------------------
3270 function Build_Unit_Name return Node_Id is
3271 Result : Node_Id;
3273 begin
3274 if No (Parent_Spec (P_Unit)) then
3275 return New_Occurrence_Of (P_Name, Loc);
3277 else
3278 Result :=
3279 Make_Expanded_Name (Loc,
3280 Chars => Chars (P_Name),
3281 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3282 Selector_Name => New_Occurrence_Of (P_Name, Loc));
3283 Set_Entity (Result, P_Name);
3284 return Result;
3285 end if;
3286 end Build_Unit_Name;
3288 -- Start of processing for Implicit_With_On_Parent
3290 begin
3291 -- The unit of the current compilation may be a package body that
3292 -- replaces an instance node. In this case we need the original instance
3293 -- node to construct the proper parent name.
3295 if Nkind (P_Unit) = N_Package_Body
3296 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3297 then
3298 P_Unit := Original_Node (P_Unit);
3299 end if;
3301 -- We add the implicit with if the child unit is the current unit being
3302 -- compiled. If the current unit is a body, we do not want to add an
3303 -- implicit_with a second time to the corresponding spec.
3305 if Nkind (Child_Unit) = N_Package_Declaration
3306 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3307 then
3308 return;
3309 end if;
3311 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3313 Set_Library_Unit (Withn, P);
3314 Set_Corresponding_Spec (Withn, P_Name);
3315 Set_First_Name (Withn, True);
3316 Set_Implicit_With (Withn, True);
3318 -- Node is placed at the beginning of the context items, so that
3319 -- subsequent use clauses on the parent can be validated.
3321 Prepend (Withn, Context_Items (N));
3322 Mark_Rewrite_Insertion (Withn);
3323 Install_Withed_Unit (Withn);
3325 if Is_Child_Spec (P_Unit) then
3326 Implicit_With_On_Parent (P_Unit, N);
3327 end if;
3328 end Implicit_With_On_Parent;
3330 --------------
3331 -- In_Chain --
3332 --------------
3334 function In_Chain (E : Entity_Id) return Boolean is
3335 H : Entity_Id;
3337 begin
3338 H := Current_Entity (E);
3339 while Present (H) loop
3340 if H = E then
3341 return True;
3342 else
3343 H := Homonym (H);
3344 end if;
3345 end loop;
3347 return False;
3348 end In_Chain;
3350 ---------------------
3351 -- Install_Context --
3352 ---------------------
3354 procedure Install_Context (N : Node_Id) is
3355 Lib_Unit : constant Node_Id := Unit (N);
3357 begin
3358 Install_Context_Clauses (N);
3360 if Is_Child_Spec (Lib_Unit) then
3361 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3362 end if;
3364 Install_Limited_Context_Clauses (N);
3365 end Install_Context;
3367 -----------------------------
3368 -- Install_Context_Clauses --
3369 -----------------------------
3371 procedure Install_Context_Clauses (N : Node_Id) is
3372 Lib_Unit : constant Node_Id := Unit (N);
3373 Item : Node_Id;
3374 Uname_Node : Entity_Id;
3375 Check_Private : Boolean := False;
3376 Decl_Node : Node_Id;
3377 Lib_Parent : Entity_Id;
3379 begin
3380 -- First skip configuration pragmas at the start of the context. They
3381 -- are not technically part of the context clause, but that's where the
3382 -- parser puts them. Note they were analyzed in Analyze_Context.
3384 Item := First (Context_Items (N));
3385 while Present (Item)
3386 and then Nkind (Item) = N_Pragma
3387 and then Pragma_Name_Mapped (Item) in Configuration_Pragma_Names
3388 loop
3389 Next (Item);
3390 end loop;
3392 -- Loop through the actual context clause items. We process everything
3393 -- except Limited_With clauses in this routine. Limited_With clauses
3394 -- are separately installed (see Install_Limited_Context_Clauses).
3396 while Present (Item) loop
3398 -- Case of explicit WITH clause
3400 if Nkind (Item) = N_With_Clause
3401 and then not Implicit_With (Item)
3402 then
3403 if Limited_Present (Item) then
3405 -- Limited withed units will be installed later
3407 goto Continue;
3409 -- If Name (Item) is not an entity name, something is wrong, and
3410 -- this will be detected in due course, for now ignore the item
3412 elsif not Is_Entity_Name (Name (Item)) then
3413 goto Continue;
3415 elsif No (Entity (Name (Item))) then
3416 Set_Entity (Name (Item), Any_Id);
3417 goto Continue;
3418 end if;
3420 Uname_Node := Entity (Name (Item));
3422 if Is_Private_Descendant (Uname_Node) then
3423 Check_Private := True;
3424 end if;
3426 Install_Withed_Unit (Item);
3428 Decl_Node := Unit_Declaration_Node (Uname_Node);
3430 -- If the unit is a subprogram instance, it appears nested within
3431 -- a package that carries the parent information.
3433 if Is_Generic_Instance (Uname_Node)
3434 and then Ekind (Uname_Node) /= E_Package
3435 then
3436 Decl_Node := Parent (Parent (Decl_Node));
3437 end if;
3439 if Is_Child_Spec (Decl_Node) then
3440 if Nkind (Name (Item)) = N_Expanded_Name then
3441 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3442 else
3443 -- If not an expanded name, the child unit must be a
3444 -- renaming, nothing to do.
3446 null;
3447 end if;
3449 elsif Nkind (Decl_Node) = N_Subprogram_Body
3450 and then not Acts_As_Spec (Parent (Decl_Node))
3451 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3452 then
3453 Implicit_With_On_Parent
3454 (Unit (Library_Unit (Parent (Decl_Node))), N);
3455 end if;
3457 -- Check license conditions unless this is a dummy unit
3459 if Sloc (Library_Unit (Item)) /= No_Location then
3460 License_Check : declare
3461 Withu : constant Unit_Number_Type :=
3462 Get_Source_Unit (Library_Unit (Item));
3463 Withl : constant License_Type :=
3464 License (Source_Index (Withu));
3465 Unitl : constant License_Type :=
3466 License (Source_Index (Current_Sem_Unit));
3468 procedure License_Error;
3469 -- Signal error of bad license
3471 -------------------
3472 -- License_Error --
3473 -------------------
3475 procedure License_Error is
3476 begin
3477 Error_Msg_N
3478 ("license of withed unit & may be inconsistent??",
3479 Name (Item));
3480 end License_Error;
3482 -- Start of processing for License_Check
3484 begin
3485 -- Exclude license check if withed unit is an internal unit.
3486 -- This situation arises e.g. with the GPL version of GNAT.
3488 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3489 null;
3491 -- Otherwise check various cases
3492 else
3493 case Unitl is
3494 when Unknown =>
3495 null;
3497 when Restricted =>
3498 if Withl = GPL then
3499 License_Error;
3500 end if;
3502 when GPL =>
3503 if Withl = Restricted then
3504 License_Error;
3505 end if;
3507 when Modified_GPL =>
3508 if Withl = Restricted or else Withl = GPL then
3509 License_Error;
3510 end if;
3512 when Unrestricted =>
3513 null;
3514 end case;
3515 end if;
3516 end License_Check;
3517 end if;
3519 -- Case of USE PACKAGE clause
3521 elsif Nkind (Item) = N_Use_Package_Clause then
3522 Analyze_Use_Package (Item);
3524 -- Case of USE TYPE clause
3526 elsif Nkind (Item) = N_Use_Type_Clause then
3527 Analyze_Use_Type (Item);
3529 -- case of PRAGMA
3531 elsif Nkind (Item) = N_Pragma then
3532 Analyze (Item);
3533 end if;
3535 <<Continue>>
3536 Next (Item);
3537 end loop;
3539 if Is_Child_Spec (Lib_Unit) then
3541 -- The unit also has implicit with_clauses on its own parents
3543 if No (Context_Items (N)) then
3544 Set_Context_Items (N, New_List);
3545 end if;
3547 Implicit_With_On_Parent (Lib_Unit, N);
3548 end if;
3550 -- If the unit is a body, the context of the specification must also
3551 -- be installed. That includes private with_clauses in that context.
3553 if Nkind (Lib_Unit) = N_Package_Body
3554 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3555 and then not Acts_As_Spec (N))
3556 then
3557 Install_Context (Library_Unit (N));
3559 -- Only install private with-clauses of a spec that comes from
3560 -- source, excluding specs created for a subprogram body that is
3561 -- a child unit.
3563 if Comes_From_Source (Library_Unit (N)) then
3564 Install_Private_With_Clauses
3565 (Defining_Entity (Unit (Library_Unit (N))));
3566 end if;
3568 if Is_Child_Spec (Unit (Library_Unit (N))) then
3570 -- If the unit is the body of a public child unit, the private
3571 -- declarations of the parent must be made visible. If the child
3572 -- unit is private, the private declarations have been installed
3573 -- already in the call to Install_Parents for the spec. Installing
3574 -- private declarations must be done for all ancestors of public
3575 -- child units. In addition, sibling units mentioned in the
3576 -- context clause of the body are directly visible.
3578 declare
3579 Lib_Spec : Node_Id;
3580 P : Node_Id;
3581 P_Name : Entity_Id;
3583 begin
3584 Lib_Spec := Unit (Library_Unit (N));
3585 while Is_Child_Spec (Lib_Spec) loop
3586 P := Unit (Parent_Spec (Lib_Spec));
3587 P_Name := Defining_Entity (P);
3589 if not (Private_Present (Parent (Lib_Spec)))
3590 and then not In_Private_Part (P_Name)
3591 then
3592 Install_Private_Declarations (P_Name);
3593 Install_Private_With_Clauses (P_Name);
3594 Set_Use (Private_Declarations (Specification (P)));
3595 end if;
3597 Lib_Spec := P;
3598 end loop;
3599 end;
3600 end if;
3602 -- For a package body, children in context are immediately visible
3604 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3605 end if;
3607 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3608 N_Generic_Subprogram_Declaration,
3609 N_Package_Declaration,
3610 N_Subprogram_Declaration)
3611 then
3612 if Is_Child_Spec (Lib_Unit) then
3613 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3614 Set_Is_Private_Descendant
3615 (Defining_Entity (Lib_Unit),
3616 Is_Private_Descendant (Lib_Parent)
3617 or else Private_Present (Parent (Lib_Unit)));
3619 else
3620 Set_Is_Private_Descendant
3621 (Defining_Entity (Lib_Unit),
3622 Private_Present (Parent (Lib_Unit)));
3623 end if;
3624 end if;
3626 if Check_Private then
3627 Check_Private_Child_Unit (N);
3628 end if;
3629 end Install_Context_Clauses;
3631 -------------------------------------
3632 -- Install_Limited_Context_Clauses --
3633 -------------------------------------
3635 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3636 Item : Node_Id;
3638 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3639 -- Check that the unlimited view of a given compilation_unit is not
3640 -- already visible through "use + renamings".
3642 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3643 -- Check that if a limited_with clause of a given compilation_unit
3644 -- mentions a descendant of a private child of some library unit, then
3645 -- the given compilation_unit must be the declaration of a private
3646 -- descendant of that library unit, or a public descendant of such. The
3647 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3648 -- use entities on the limited with_clauses because their units have not
3649 -- been analyzed, so we have to climb the tree of ancestors looking for
3650 -- private keywords.
3652 procedure Expand_Limited_With_Clause
3653 (Comp_Unit : Node_Id;
3654 Nam : Node_Id;
3655 N : Node_Id);
3656 -- If a child unit appears in a limited_with clause, there are implicit
3657 -- limited_with clauses on all parents that are not already visible
3658 -- through a regular with clause. This procedure creates the implicit
3659 -- limited with_clauses for the parents and loads the corresponding
3660 -- units. The shadow entities are created when the inserted clause is
3661 -- analyzed. Implements Ada 2005 (AI-50217).
3663 ---------------------
3664 -- Check_Renamings --
3665 ---------------------
3667 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3668 Item : Node_Id;
3669 Spec : Node_Id;
3670 WEnt : Entity_Id;
3671 Nam : Node_Id;
3672 E : Entity_Id;
3673 E2 : Entity_Id;
3675 begin
3676 pragma Assert (Nkind (W) = N_With_Clause);
3678 -- Protect the frontend against previous critical errors
3680 case Nkind (Unit (Library_Unit (W))) is
3681 when N_Subprogram_Declaration |
3682 N_Package_Declaration |
3683 N_Generic_Subprogram_Declaration |
3684 N_Generic_Package_Declaration =>
3685 null;
3687 when others =>
3688 return;
3689 end case;
3691 -- Check "use + renamings"
3693 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3694 Spec := Specification (Unit (P));
3696 Item := First (Visible_Declarations (Spec));
3697 while Present (Item) loop
3699 -- Look only at use package clauses
3701 if Nkind (Item) = N_Use_Package_Clause then
3703 -- Traverse the list of packages
3705 Nam := First (Names (Item));
3706 while Present (Nam) loop
3707 E := Entity (Nam);
3709 pragma Assert (Present (Parent (E)));
3711 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3712 and then Renamed_Entity (E) = WEnt
3713 then
3714 -- The unlimited view is visible through use clause and
3715 -- renamings. There is no need to generate the error
3716 -- message here because Is_Visible_Through_Renamings
3717 -- takes care of generating the precise error message.
3719 return;
3721 elsif Nkind (Parent (E)) = N_Package_Specification then
3723 -- The use clause may refer to a local package.
3724 -- Check all the enclosing scopes.
3726 E2 := E;
3727 while E2 /= Standard_Standard and then E2 /= WEnt loop
3728 E2 := Scope (E2);
3729 end loop;
3731 if E2 = WEnt then
3732 Error_Msg_N
3733 ("unlimited view visible through use clause ", W);
3734 return;
3735 end if;
3736 end if;
3738 Next (Nam);
3739 end loop;
3740 end if;
3742 Next (Item);
3743 end loop;
3745 -- Recursive call to check all the ancestors
3747 if Is_Child_Spec (Unit (P)) then
3748 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3749 end if;
3750 end Check_Renamings;
3752 ---------------------------------------
3753 -- Check_Private_Limited_Withed_Unit --
3754 ---------------------------------------
3756 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3757 Curr_Parent : Node_Id;
3758 Child_Parent : Node_Id;
3759 Curr_Private : Boolean;
3761 begin
3762 -- Compilation unit of the parent of the withed library unit
3764 Child_Parent := Library_Unit (Item);
3766 -- If the child unit is a public child, then locate its nearest
3767 -- private ancestor, if any, then Child_Parent will then be set to
3768 -- the parent of that ancestor.
3770 if not Private_Present (Library_Unit (Item)) then
3771 while Present (Child_Parent)
3772 and then not Private_Present (Child_Parent)
3773 loop
3774 Child_Parent := Parent_Spec (Unit (Child_Parent));
3775 end loop;
3777 if No (Child_Parent) then
3778 return;
3779 end if;
3780 end if;
3782 Child_Parent := Parent_Spec (Unit (Child_Parent));
3784 -- Traverse all the ancestors of the current compilation unit to
3785 -- check if it is a descendant of named library unit.
3787 Curr_Parent := Parent (Item);
3788 Curr_Private := Private_Present (Curr_Parent);
3790 while Present (Parent_Spec (Unit (Curr_Parent)))
3791 and then Curr_Parent /= Child_Parent
3792 loop
3793 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3794 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3795 end loop;
3797 if Curr_Parent /= Child_Parent then
3798 Error_Msg_N
3799 ("unit in with clause is private child unit!", Item);
3800 Error_Msg_NE
3801 ("\current unit must also have parent&!",
3802 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3804 elsif Private_Present (Parent (Item))
3805 or else Curr_Private
3806 or else Private_Present (Item)
3807 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3808 N_Subprogram_Body,
3809 N_Subunit)
3810 then
3811 -- Current unit is private, of descendant of a private unit
3813 null;
3815 else
3816 Error_Msg_NE
3817 ("current unit must also be private descendant of&",
3818 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3819 end if;
3820 end Check_Private_Limited_Withed_Unit;
3822 --------------------------------
3823 -- Expand_Limited_With_Clause --
3824 --------------------------------
3826 procedure Expand_Limited_With_Clause
3827 (Comp_Unit : Node_Id;
3828 Nam : Node_Id;
3829 N : Node_Id)
3831 Loc : constant Source_Ptr := Sloc (Nam);
3832 Unum : Unit_Number_Type;
3833 Withn : Node_Id;
3835 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3836 -- Returns true if the context already includes a with_clause for
3837 -- this unit. If the with_clause is non-limited, the unit is fully
3838 -- visible and an implicit limited_with should not be created. If
3839 -- there is already a limited_with clause for W, a second one is
3840 -- simply redundant.
3842 --------------------------
3843 -- Previous_Withed_Unit --
3844 --------------------------
3846 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3847 Item : Node_Id;
3849 begin
3850 -- A limited with_clause cannot appear in the same context_clause
3851 -- as a nonlimited with_clause which mentions the same library.
3853 Item := First (Context_Items (Comp_Unit));
3854 while Present (Item) loop
3855 if Nkind (Item) = N_With_Clause
3856 and then Library_Unit (Item) = Library_Unit (W)
3857 then
3858 return True;
3859 end if;
3861 Next (Item);
3862 end loop;
3864 return False;
3865 end Previous_Withed_Unit;
3867 -- Start of processing for Expand_Limited_With_Clause
3869 begin
3870 if Nkind (Nam) = N_Identifier then
3872 -- Create node for name of withed unit
3874 Withn :=
3875 Make_With_Clause (Loc,
3876 Name => New_Copy (Nam));
3878 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3879 Withn :=
3880 Make_With_Clause (Loc,
3881 Name => Make_Selected_Component (Loc,
3882 Prefix => New_Copy_Tree (Prefix (Nam)),
3883 Selector_Name => New_Copy (Selector_Name (Nam))));
3884 Set_Parent (Withn, Parent (N));
3885 end if;
3887 Set_Limited_Present (Withn);
3888 Set_First_Name (Withn);
3889 Set_Implicit_With (Withn);
3891 Unum :=
3892 Load_Unit
3893 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3894 Required => True,
3895 Subunit => False,
3896 Error_Node => Nam);
3898 -- Do not generate a limited_with_clause on the current unit. This
3899 -- path is taken when a unit has a limited_with clause on one of its
3900 -- child units.
3902 if Unum = Current_Sem_Unit then
3903 return;
3904 end if;
3906 Set_Library_Unit (Withn, Cunit (Unum));
3907 Set_Corresponding_Spec
3908 (Withn, Specification (Unit (Cunit (Unum))));
3910 if not Previous_Withed_Unit (Withn) then
3911 Prepend (Withn, Context_Items (Parent (N)));
3912 Mark_Rewrite_Insertion (Withn);
3914 -- Add implicit limited_with_clauses for parents of child units
3915 -- mentioned in limited_with clauses.
3917 if Nkind (Nam) = N_Selected_Component then
3918 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3919 end if;
3921 Analyze (Withn);
3923 if not Limited_View_Installed (Withn) then
3924 Install_Limited_Withed_Unit (Withn);
3925 end if;
3926 end if;
3927 end Expand_Limited_With_Clause;
3929 -- Start of processing for Install_Limited_Context_Clauses
3931 begin
3932 Item := First (Context_Items (N));
3933 while Present (Item) loop
3934 if Nkind (Item) = N_With_Clause
3935 and then Limited_Present (Item)
3936 and then not Error_Posted (Item)
3937 then
3938 if Nkind (Name (Item)) = N_Selected_Component then
3939 Expand_Limited_With_Clause
3940 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3941 end if;
3943 Check_Private_Limited_Withed_Unit (Item);
3945 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
3946 Check_Renamings (Parent_Spec (Unit (N)), Item);
3947 end if;
3949 -- A unit may have a limited with on itself if it has a limited
3950 -- with_clause on one of its child units. In that case it is
3951 -- already being compiled and it makes no sense to install its
3952 -- limited view.
3954 -- If the item is a limited_private_with_clause, install it if the
3955 -- current unit is a body or if it is a private child. Otherwise
3956 -- the private clause is installed before analyzing the private
3957 -- part of the current unit.
3959 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3960 and then not Limited_View_Installed (Item)
3961 and then
3962 not Is_Ancestor_Unit
3963 (Library_Unit (Item), Cunit (Current_Sem_Unit))
3964 then
3965 if not Private_Present (Item)
3966 or else Private_Present (N)
3967 or else Nkind_In (Unit (N), N_Package_Body,
3968 N_Subprogram_Body,
3969 N_Subunit)
3970 then
3971 Install_Limited_Withed_Unit (Item);
3972 end if;
3973 end if;
3974 end if;
3976 Next (Item);
3977 end loop;
3979 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
3980 -- looking for incomplete subtype declarations of incomplete types
3981 -- visible through a limited with clause.
3983 if Ada_Version >= Ada_2005
3984 and then Analyzed (N)
3985 and then Nkind (Unit (N)) = N_Package_Declaration
3986 then
3987 declare
3988 Decl : Node_Id;
3989 Def_Id : Entity_Id;
3990 Non_Lim_View : Entity_Id;
3992 begin
3993 Decl := First (Visible_Declarations (Specification (Unit (N))));
3994 while Present (Decl) loop
3995 if Nkind (Decl) = N_Subtype_Declaration
3996 and then
3997 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3998 and then
3999 From_Limited_With (Defining_Identifier (Decl))
4000 then
4001 Def_Id := Defining_Identifier (Decl);
4002 Non_Lim_View := Non_Limited_View (Def_Id);
4004 if not Is_Incomplete_Type (Non_Lim_View) then
4006 -- Convert an incomplete subtype declaration into a
4007 -- corresponding non-limited view subtype declaration.
4008 -- This is usually the case when analyzing a body that
4009 -- has regular with clauses, when the spec has limited
4010 -- ones.
4012 -- If the non-limited view is still incomplete, it is
4013 -- the dummy entry already created, and the declaration
4014 -- cannot be reanalyzed. This is the case when installing
4015 -- a parent unit that has limited with-clauses.
4017 Set_Subtype_Indication (Decl,
4018 New_Occurrence_Of (Non_Lim_View, Sloc (Def_Id)));
4019 Set_Etype (Def_Id, Non_Lim_View);
4020 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
4021 Set_Analyzed (Decl, False);
4023 -- Reanalyze the declaration, suppressing the call to
4024 -- Enter_Name to avoid duplicate names.
4026 Analyze_Subtype_Declaration
4027 (N => Decl,
4028 Skip => True);
4029 end if;
4030 end if;
4032 Next (Decl);
4033 end loop;
4034 end;
4035 end if;
4036 end Install_Limited_Context_Clauses;
4038 ---------------------
4039 -- Install_Parents --
4040 ---------------------
4042 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
4043 P : Node_Id;
4044 E_Name : Entity_Id;
4045 P_Name : Entity_Id;
4046 P_Spec : Node_Id;
4048 begin
4049 P := Unit (Parent_Spec (Lib_Unit));
4050 P_Name := Get_Parent_Entity (P);
4052 if Etype (P_Name) = Any_Type then
4053 return;
4054 end if;
4056 if Ekind (P_Name) = E_Generic_Package
4057 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
4058 N_Generic_Package_Declaration)
4059 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
4060 then
4061 Error_Msg_N
4062 ("child of a generic package must be a generic unit", Lib_Unit);
4064 elsif not Is_Package_Or_Generic_Package (P_Name) then
4065 Error_Msg_N
4066 ("parent unit must be package or generic package", Lib_Unit);
4067 raise Unrecoverable_Error;
4069 elsif Present (Renamed_Object (P_Name)) then
4070 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
4071 raise Unrecoverable_Error;
4073 -- Verify that a child of an instance is itself an instance, or the
4074 -- renaming of one. Given that an instance that is a unit is replaced
4075 -- with a package declaration, check against the original node. The
4076 -- parent may be currently being instantiated, in which case it appears
4077 -- as a declaration, but the generic_parent is already established
4078 -- indicating that we deal with an instance.
4080 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
4081 if Nkind (Lib_Unit) in N_Renaming_Declaration
4082 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
4083 or else
4084 (Nkind (Lib_Unit) = N_Package_Declaration
4085 and then Present (Generic_Parent (Specification (Lib_Unit))))
4086 then
4087 null;
4088 else
4089 Error_Msg_N
4090 ("child of an instance must be an instance or renaming",
4091 Lib_Unit);
4092 end if;
4093 end if;
4095 -- This is the recursive call that ensures all parents are loaded
4097 if Is_Child_Spec (P) then
4098 Install_Parents (P,
4099 Is_Private or else Private_Present (Parent (Lib_Unit)));
4100 end if;
4102 -- Now we can install the context for this parent
4104 Install_Context_Clauses (Parent_Spec (Lib_Unit));
4105 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4106 Install_Siblings (P_Name, Parent (Lib_Unit));
4108 -- The child unit is in the declarative region of the parent. The parent
4109 -- must therefore appear in the scope stack and be visible, as when
4110 -- compiling the corresponding body. If the child unit is private or it
4111 -- is a package body, private declarations must be accessible as well.
4112 -- Use declarations in the parent must also be installed. Finally, other
4113 -- child units of the same parent that are in the context are
4114 -- immediately visible.
4116 -- Find entity for compilation unit, and set its private descendant
4117 -- status as needed. Indicate that it is a compilation unit, which is
4118 -- redundant in general, but needed if this is a generated child spec
4119 -- for a child body without previous spec.
4121 E_Name := Defining_Entity (Lib_Unit);
4123 Set_Is_Child_Unit (E_Name);
4124 Set_Is_Compilation_Unit (E_Name);
4126 Set_Is_Private_Descendant (E_Name,
4127 Is_Private_Descendant (P_Name)
4128 or else Private_Present (Parent (Lib_Unit)));
4130 P_Spec := Package_Specification (P_Name);
4131 Push_Scope (P_Name);
4133 -- Save current visibility of unit
4135 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4136 Is_Immediately_Visible (P_Name);
4137 Set_Is_Immediately_Visible (P_Name);
4138 Install_Visible_Declarations (P_Name);
4139 Set_Use (Visible_Declarations (P_Spec));
4141 -- If the parent is a generic unit, its formal part may contain formal
4142 -- packages and use clauses for them.
4144 if Ekind (P_Name) = E_Generic_Package then
4145 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4146 end if;
4148 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4149 Install_Private_Declarations (P_Name);
4150 Install_Private_With_Clauses (P_Name);
4151 Set_Use (Private_Declarations (P_Spec));
4152 end if;
4153 end Install_Parents;
4155 ----------------------------------
4156 -- Install_Private_With_Clauses --
4157 ----------------------------------
4159 procedure Install_Private_With_Clauses (P : Entity_Id) is
4160 Decl : constant Node_Id := Unit_Declaration_Node (P);
4161 Item : Node_Id;
4163 begin
4164 if Debug_Flag_I then
4165 Write_Str ("install private with clauses of ");
4166 Write_Name (Chars (P));
4167 Write_Eol;
4168 end if;
4170 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4171 Item := First (Context_Items (Parent (Decl)));
4172 while Present (Item) loop
4173 if Nkind (Item) = N_With_Clause
4174 and then Private_Present (Item)
4175 then
4176 -- If the unit is an ancestor of the current one, it is the
4177 -- case of a private limited with clause on a child unit, and
4178 -- the compilation of one of its descendants, In that case the
4179 -- limited view is errelevant.
4181 if Limited_Present (Item) then
4182 if not Limited_View_Installed (Item)
4183 and then
4184 not Is_Ancestor_Unit (Library_Unit (Item),
4185 Cunit (Current_Sem_Unit))
4186 then
4187 Install_Limited_Withed_Unit (Item);
4188 end if;
4189 else
4190 Install_Withed_Unit (Item, Private_With_OK => True);
4191 end if;
4192 end if;
4194 Next (Item);
4195 end loop;
4196 end if;
4197 end Install_Private_With_Clauses;
4199 ----------------------
4200 -- Install_Siblings --
4201 ----------------------
4203 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4204 Item : Node_Id;
4205 Id : Entity_Id;
4206 Prev : Entity_Id;
4208 begin
4209 -- Iterate over explicit with clauses, and check whether the scope of
4210 -- each entity is an ancestor of the current unit, in which case it is
4211 -- immediately visible.
4213 Item := First (Context_Items (N));
4214 while Present (Item) loop
4216 -- Do not install private_with_clauses declaration, unless unit
4217 -- is itself a private child unit, or is a body. Note that for a
4218 -- subprogram body the private_with_clause does not take effect until
4219 -- after the specification.
4221 if Nkind (Item) /= N_With_Clause
4222 or else Implicit_With (Item)
4223 or else Limited_Present (Item)
4224 or else Error_Posted (Item)
4225 then
4226 null;
4228 elsif not Private_Present (Item)
4229 or else Private_Present (N)
4230 or else Nkind (Unit (N)) = N_Package_Body
4231 then
4232 Id := Entity (Name (Item));
4234 if Is_Child_Unit (Id)
4235 and then Is_Ancestor_Package (Scope (Id), U_Name)
4236 then
4237 Set_Is_Immediately_Visible (Id);
4239 -- Check for the presence of another unit in the context that
4240 -- may be inadvertently hidden by the child.
4242 Prev := Current_Entity (Id);
4244 if Present (Prev)
4245 and then Is_Immediately_Visible (Prev)
4246 and then not Is_Child_Unit (Prev)
4247 then
4248 declare
4249 Clause : Node_Id;
4251 begin
4252 Clause := First (Context_Items (N));
4253 while Present (Clause) loop
4254 if Nkind (Clause) = N_With_Clause
4255 and then Entity (Name (Clause)) = Prev
4256 then
4257 Error_Msg_NE
4258 ("child unit& hides compilation unit " &
4259 "with the same name??",
4260 Name (Item), Id);
4261 exit;
4262 end if;
4264 Next (Clause);
4265 end loop;
4266 end;
4267 end if;
4269 -- The With_Clause may be on a grand-child or one of its further
4270 -- descendants, which makes a child immediately visible. Examine
4271 -- ancestry to determine whether such a child exists. For example,
4272 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4273 -- is immediately visible.
4275 elsif Is_Child_Unit (Id) then
4276 declare
4277 Par : Entity_Id;
4279 begin
4280 Par := Scope (Id);
4281 while Is_Child_Unit (Par) loop
4282 if Is_Ancestor_Package (Scope (Par), U_Name) then
4283 Set_Is_Immediately_Visible (Par);
4284 exit;
4285 end if;
4287 Par := Scope (Par);
4288 end loop;
4289 end;
4290 end if;
4292 -- If the item is a private with-clause on a child unit, the parent
4293 -- may have been installed already, but the child unit must remain
4294 -- invisible until installed in a private part or body, unless there
4295 -- is already a regular with_clause for it in the current unit.
4297 elsif Private_Present (Item) then
4298 Id := Entity (Name (Item));
4300 if Is_Child_Unit (Id) then
4301 declare
4302 Clause : Node_Id;
4304 function In_Context return Boolean;
4305 -- Scan context of current unit, to check whether there is
4306 -- a with_clause on the same unit as a private with-clause
4307 -- on a parent, in which case child unit is visible. If the
4308 -- unit is a grand-child, the same applies to its parent.
4310 ----------------
4311 -- In_Context --
4312 ----------------
4314 function In_Context return Boolean is
4315 begin
4316 Clause :=
4317 First (Context_Items (Cunit (Current_Sem_Unit)));
4318 while Present (Clause) loop
4319 if Nkind (Clause) = N_With_Clause
4320 and then Comes_From_Source (Clause)
4321 and then Is_Entity_Name (Name (Clause))
4322 and then not Private_Present (Clause)
4323 then
4324 if Entity (Name (Clause)) = Id
4325 or else
4326 (Nkind (Name (Clause)) = N_Expanded_Name
4327 and then Entity (Prefix (Name (Clause))) = Id)
4328 then
4329 return True;
4330 end if;
4331 end if;
4333 Next (Clause);
4334 end loop;
4336 return False;
4337 end In_Context;
4339 begin
4340 Set_Is_Visible_Lib_Unit (Id, In_Context);
4341 end;
4342 end if;
4343 end if;
4345 Next (Item);
4346 end loop;
4347 end Install_Siblings;
4349 ---------------------------------
4350 -- Install_Limited_Withed_Unit --
4351 ---------------------------------
4353 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4354 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4355 E : Entity_Id;
4356 P : Entity_Id;
4357 Is_Child_Package : Boolean := False;
4358 Lim_Header : Entity_Id;
4359 Lim_Typ : Entity_Id;
4361 procedure Check_Body_Required;
4362 -- A unit mentioned in a limited with_clause may not be mentioned in
4363 -- a regular with_clause, but must still be included in the current
4364 -- partition. We need to determine whether the unit needs a body, so
4365 -- that the binder can determine the name of the file to be compiled.
4366 -- Checking whether a unit needs a body can be done without semantic
4367 -- analysis, by examining the nature of the declarations in the package.
4369 function Has_Limited_With_Clause
4370 (C_Unit : Entity_Id;
4371 Pack : Entity_Id) return Boolean;
4372 -- Determine whether any package in the ancestor chain starting with
4373 -- C_Unit has a limited with clause for package Pack.
4375 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4376 -- Check if some package installed though normal with-clauses has a
4377 -- renaming declaration of package P. AARM 10.1.2(21/2).
4379 -------------------------
4380 -- Check_Body_Required --
4381 -------------------------
4383 procedure Check_Body_Required is
4384 PA : constant List_Id :=
4385 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4387 procedure Check_Declarations (Spec : Node_Id);
4388 -- Recursive procedure that does the work and checks nested packages
4390 ------------------------
4391 -- Check_Declarations --
4392 ------------------------
4394 procedure Check_Declarations (Spec : Node_Id) is
4395 Decl : Node_Id;
4396 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4398 Subp_List : constant Elist_Id := New_Elmt_List;
4400 procedure Check_Pragma_Import (P : Node_Id);
4401 -- If a pragma import applies to a previous subprogram, the
4402 -- enclosing unit may not need a body. The processing is syntactic
4403 -- and does not require a declaration to be analyzed. The code
4404 -- below also handles pragma Import when applied to a subprogram
4405 -- that renames another. In this case the pragma applies to the
4406 -- renamed entity.
4408 -- Chains of multiple renames are not handled by the code below.
4409 -- It is probably impossible to handle all cases without proper
4410 -- name resolution. In such cases the algorithm is conservative
4411 -- and will indicate that a body is needed???
4413 -------------------------
4414 -- Check_Pragma_Import --
4415 -------------------------
4417 procedure Check_Pragma_Import (P : Node_Id) is
4418 Arg : Node_Id;
4419 Prev_Id : Elmt_Id;
4420 Subp_Id : Elmt_Id;
4421 Imported : Node_Id;
4423 procedure Remove_Homonyms (E : Node_Id);
4424 -- Make one pass over list of subprograms. Called again if
4425 -- subprogram is a renaming. E is known to be an identifier.
4427 ---------------------
4428 -- Remove_Homonyms --
4429 ---------------------
4431 procedure Remove_Homonyms (E : Node_Id) is
4432 R : Entity_Id := Empty;
4433 -- Name of renamed entity, if any
4435 begin
4436 Subp_Id := First_Elmt (Subp_List);
4437 while Present (Subp_Id) loop
4438 if Chars (Node (Subp_Id)) = Chars (E) then
4439 if Nkind (Parent (Parent (Node (Subp_Id))))
4440 /= N_Subprogram_Renaming_Declaration
4441 then
4442 Prev_Id := Subp_Id;
4443 Next_Elmt (Subp_Id);
4444 Remove_Elmt (Subp_List, Prev_Id);
4445 else
4446 R := Name (Parent (Parent (Node (Subp_Id))));
4447 exit;
4448 end if;
4449 else
4450 Next_Elmt (Subp_Id);
4451 end if;
4452 end loop;
4454 if Present (R) then
4455 if Nkind (R) = N_Identifier then
4456 Remove_Homonyms (R);
4458 elsif Nkind (R) = N_Selected_Component then
4459 Remove_Homonyms (Selector_Name (R));
4461 -- Renaming of attribute
4463 else
4464 null;
4465 end if;
4466 end if;
4467 end Remove_Homonyms;
4469 -- Start of processing for Check_Pragma_Import
4471 begin
4472 -- Find name of entity in Import pragma. We have not analyzed
4473 -- the construct, so we must guard against syntax errors.
4475 Arg := Next (First (Pragma_Argument_Associations (P)));
4477 if No (Arg)
4478 or else Nkind (Expression (Arg)) /= N_Identifier
4479 then
4480 return;
4481 else
4482 Imported := Expression (Arg);
4483 end if;
4485 Remove_Homonyms (Imported);
4486 end Check_Pragma_Import;
4488 -- Start of processing for Check_Declarations
4490 begin
4491 -- Search for Elaborate Body pragma
4493 Decl := First (Visible_Declarations (Spec));
4494 while Present (Decl)
4495 and then Nkind (Decl) = N_Pragma
4496 loop
4497 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4498 Set_Body_Required (Library_Unit (N));
4499 return;
4500 end if;
4502 Next (Decl);
4503 end loop;
4505 -- Look for declarations that require the presence of a body. We
4506 -- have already skipped pragmas at the start of the list.
4508 while Present (Decl) loop
4510 -- Subprogram that comes from source means body may be needed.
4511 -- Save for subsequent examination of import pragmas.
4513 if Comes_From_Source (Decl)
4514 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4515 N_Subprogram_Renaming_Declaration,
4516 N_Generic_Subprogram_Declaration))
4517 then
4518 Append_Elmt (Defining_Entity (Decl), Subp_List);
4520 -- Package declaration of generic package declaration. We need
4521 -- to recursively examine nested declarations.
4523 elsif Nkind_In (Decl, N_Package_Declaration,
4524 N_Generic_Package_Declaration)
4525 then
4526 Check_Declarations (Specification (Decl));
4528 elsif Nkind (Decl) = N_Pragma
4529 and then Pragma_Name_Mapped (Decl) = Name_Import
4530 then
4531 Check_Pragma_Import (Decl);
4532 end if;
4534 Next (Decl);
4535 end loop;
4537 -- Same set of tests for private part. In addition to subprograms
4538 -- detect the presence of Taft Amendment types (incomplete types
4539 -- completed in the body).
4541 Decl := First (Private_Declarations (Spec));
4542 while Present (Decl) loop
4543 if Comes_From_Source (Decl)
4544 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4545 N_Subprogram_Renaming_Declaration,
4546 N_Generic_Subprogram_Declaration))
4547 then
4548 Append_Elmt (Defining_Entity (Decl), Subp_List);
4550 elsif Nkind_In (Decl, N_Package_Declaration,
4551 N_Generic_Package_Declaration)
4552 then
4553 Check_Declarations (Specification (Decl));
4555 -- Collect incomplete type declarations for separate pass
4557 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4558 Append_Elmt (Decl, Incomplete_Decls);
4560 elsif Nkind (Decl) = N_Pragma
4561 and then Pragma_Name_Mapped (Decl) = Name_Import
4562 then
4563 Check_Pragma_Import (Decl);
4564 end if;
4566 Next (Decl);
4567 end loop;
4569 -- Now check incomplete declarations to locate Taft amendment
4570 -- types. This can be done by examining the defining identifiers
4571 -- of type declarations without real semantic analysis.
4573 declare
4574 Inc : Elmt_Id;
4576 begin
4577 Inc := First_Elmt (Incomplete_Decls);
4578 while Present (Inc) loop
4579 Decl := Next (Node (Inc));
4580 while Present (Decl) loop
4581 if Nkind (Decl) = N_Full_Type_Declaration
4582 and then Chars (Defining_Identifier (Decl)) =
4583 Chars (Defining_Identifier (Node (Inc)))
4584 then
4585 exit;
4586 end if;
4588 Next (Decl);
4589 end loop;
4591 -- If no completion, this is a TAT, and a body is needed
4593 if No (Decl) then
4594 Set_Body_Required (Library_Unit (N));
4595 return;
4596 end if;
4598 Next_Elmt (Inc);
4599 end loop;
4600 end;
4602 -- Finally, check whether there are subprograms that still require
4603 -- a body, i.e. are not renamings or null.
4605 if not Is_Empty_Elmt_List (Subp_List) then
4606 declare
4607 Subp_Id : Elmt_Id;
4608 Spec : Node_Id;
4610 begin
4611 Subp_Id := First_Elmt (Subp_List);
4612 Spec := Parent (Node (Subp_Id));
4614 while Present (Subp_Id) loop
4615 if Nkind (Parent (Spec))
4616 = N_Subprogram_Renaming_Declaration
4617 then
4618 null;
4620 elsif Nkind (Spec) = N_Procedure_Specification
4621 and then Null_Present (Spec)
4622 then
4623 null;
4625 else
4626 Set_Body_Required (Library_Unit (N));
4627 return;
4628 end if;
4630 Next_Elmt (Subp_Id);
4631 end loop;
4632 end;
4633 end if;
4634 end Check_Declarations;
4636 -- Start of processing for Check_Body_Required
4638 begin
4639 -- If this is an imported package (Java and CIL usage) no body is
4640 -- needed. Scan list of pragmas that may follow a compilation unit
4641 -- to look for a relevant pragma Import.
4643 if Present (PA) then
4644 declare
4645 Prag : Node_Id;
4647 begin
4648 Prag := First (PA);
4649 while Present (Prag) loop
4650 if Nkind (Prag) = N_Pragma
4651 and then Get_Pragma_Id (Prag) = Pragma_Import
4652 then
4653 return;
4654 end if;
4656 Next (Prag);
4657 end loop;
4658 end;
4659 end if;
4661 Check_Declarations (Specification (P_Unit));
4662 end Check_Body_Required;
4664 -----------------------------
4665 -- Has_Limited_With_Clause --
4666 -----------------------------
4668 function Has_Limited_With_Clause
4669 (C_Unit : Entity_Id;
4670 Pack : Entity_Id) return Boolean
4672 Par : Entity_Id;
4673 Par_Unit : Node_Id;
4675 begin
4676 Par := C_Unit;
4677 while Present (Par) loop
4678 if Ekind (Par) /= E_Package then
4679 exit;
4680 end if;
4682 -- Retrieve the Compilation_Unit node for Par and determine if
4683 -- its context clauses contain a limited with for Pack.
4685 Par_Unit := Parent (Parent (Parent (Par)));
4687 if Nkind (Par_Unit) = N_Package_Declaration then
4688 Par_Unit := Parent (Par_Unit);
4689 end if;
4691 if Has_With_Clause (Par_Unit, Pack, True) then
4692 return True;
4693 end if;
4695 -- If there are more ancestors, climb up the tree, otherwise we
4696 -- are done.
4698 if Is_Child_Unit (Par) then
4699 Par := Scope (Par);
4700 else
4701 exit;
4702 end if;
4703 end loop;
4705 return False;
4706 end Has_Limited_With_Clause;
4708 ----------------------------------
4709 -- Is_Visible_Through_Renamings --
4710 ----------------------------------
4712 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4713 Kind : constant Node_Kind :=
4714 Nkind (Unit (Cunit (Current_Sem_Unit)));
4715 Aux_Unit : Node_Id;
4716 Item : Node_Id;
4717 Decl : Entity_Id;
4719 begin
4720 -- Example of the error detected by this subprogram:
4722 -- package P is
4723 -- type T is ...
4724 -- end P;
4726 -- with P;
4727 -- package Q is
4728 -- package Ren_P renames P;
4729 -- end Q;
4731 -- with Q;
4732 -- package R is ...
4734 -- limited with P; -- ERROR
4735 -- package R.C is ...
4737 Aux_Unit := Cunit (Current_Sem_Unit);
4739 loop
4740 Item := First (Context_Items (Aux_Unit));
4741 while Present (Item) loop
4742 if Nkind (Item) = N_With_Clause
4743 and then not Limited_Present (Item)
4744 and then Nkind (Unit (Library_Unit (Item))) =
4745 N_Package_Declaration
4746 then
4747 Decl :=
4748 First (Visible_Declarations
4749 (Specification (Unit (Library_Unit (Item)))));
4750 while Present (Decl) loop
4751 if Nkind (Decl) = N_Package_Renaming_Declaration
4752 and then Entity (Name (Decl)) = P
4753 then
4754 -- Generate the error message only if the current unit
4755 -- is a package declaration; in case of subprogram
4756 -- bodies and package bodies we just return True to
4757 -- indicate that the limited view must not be
4758 -- installed.
4760 if Kind = N_Package_Declaration then
4761 Error_Msg_N
4762 ("simultaneous visibility of the limited and " &
4763 "unlimited views not allowed", N);
4764 Error_Msg_Sloc := Sloc (Item);
4765 Error_Msg_NE
4766 ("\\ unlimited view of & visible through the " &
4767 "context clause #", N, P);
4768 Error_Msg_Sloc := Sloc (Decl);
4769 Error_Msg_NE ("\\ and the renaming #", N, P);
4770 end if;
4772 return True;
4773 end if;
4775 Next (Decl);
4776 end loop;
4777 end if;
4779 Next (Item);
4780 end loop;
4782 -- If it is a body not acting as spec, follow pointer to the
4783 -- corresponding spec, otherwise follow pointer to parent spec.
4785 if Present (Library_Unit (Aux_Unit))
4786 and then Nkind_In (Unit (Aux_Unit),
4787 N_Package_Body, N_Subprogram_Body)
4788 then
4789 if Aux_Unit = Library_Unit (Aux_Unit) then
4791 -- Aux_Unit is a body that acts as a spec. Clause has
4792 -- already been flagged as illegal.
4794 return False;
4796 else
4797 Aux_Unit := Library_Unit (Aux_Unit);
4798 end if;
4800 else
4801 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4802 end if;
4804 exit when No (Aux_Unit);
4805 end loop;
4807 return False;
4808 end Is_Visible_Through_Renamings;
4810 -- Start of processing for Install_Limited_Withed_Unit
4812 begin
4813 pragma Assert (not Limited_View_Installed (N));
4815 -- In case of limited with_clause on subprograms, generics, instances,
4816 -- or renamings, the corresponding error was previously posted and we
4817 -- have nothing to do here. If the file is missing altogether, it has
4818 -- no source location.
4820 if Nkind (P_Unit) /= N_Package_Declaration
4821 or else Sloc (P_Unit) = No_Location
4822 then
4823 return;
4824 end if;
4826 P := Defining_Unit_Name (Specification (P_Unit));
4828 -- Handle child packages
4830 if Nkind (P) = N_Defining_Program_Unit_Name then
4831 Is_Child_Package := True;
4832 P := Defining_Identifier (P);
4833 end if;
4835 -- Do not install the limited-view if the context of the unit is already
4836 -- available through a regular with clause.
4838 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4839 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4840 then
4841 return;
4842 end if;
4844 -- Do not install the limited-view if the full-view is already visible
4845 -- through renaming declarations.
4847 if Is_Visible_Through_Renamings (P) then
4848 return;
4849 end if;
4851 -- Do not install the limited view if this is the unit being analyzed.
4852 -- This unusual case will happen when a unit has a limited_with clause
4853 -- on one of its children. The compilation of the child forces the load
4854 -- of the parent which tries to install the limited view of the child
4855 -- again. Installing the limited view must also be disabled when
4856 -- compiling the body of the child unit.
4858 if P = Cunit_Entity (Current_Sem_Unit)
4859 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4860 and then P = Main_Unit_Entity
4861 and then Is_Ancestor_Unit
4862 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
4863 then
4864 return;
4865 end if;
4867 -- This scenario is similar to the one above, the difference is that the
4868 -- compilation of sibling Par.Sib forces the load of parent Par which
4869 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4870 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4871 -- non-limited views of all entities from Lim_Pack.
4873 -- limited with Lim_Pack; -- [1]
4874 -- package Par is ... package Lim_Pack is ...
4876 -- with Lim_Pack; -- [2]
4877 -- package Par.Sib is ... package body Par.Sib is ...
4879 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4880 -- Sem_Unit is the body of Par.Sib.
4882 if Ekind (P) = E_Package
4883 and then Ekind (Main_Unit_Entity) = E_Package
4884 and then Is_Child_Unit (Main_Unit_Entity)
4886 -- The body has a regular with clause
4888 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4889 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4891 -- One of the ancestors has a limited with clause
4893 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4894 N_Package_Specification
4895 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4896 then
4897 return;
4898 end if;
4900 -- A common use of the limited-with is to have a limited-with in the
4901 -- package spec, and a normal with in its package body. For example:
4903 -- limited with X; -- [1]
4904 -- package A is ...
4906 -- with X; -- [2]
4907 -- package body A is ...
4909 -- The compilation of A's body installs the context clauses found at [2]
4910 -- and then the context clauses of its specification (found at [1]). As
4911 -- a consequence, at [1] the specification of X has been analyzed and it
4912 -- is immediately visible. According to the semantics of limited-with
4913 -- context clauses we don't install the limited view because the full
4914 -- view of X supersedes its limited view.
4916 if Analyzed (P_Unit)
4917 and then
4918 (Is_Immediately_Visible (P)
4919 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
4920 then
4922 -- The presence of both the limited and the analyzed nonlimited view
4923 -- may also be an error, such as an illegal context for a limited
4924 -- with_clause. In that case, do not process the context item at all.
4926 if Error_Posted (N) then
4927 return;
4928 end if;
4930 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
4931 declare
4932 Item : Node_Id;
4933 begin
4934 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
4935 while Present (Item) loop
4936 if Nkind (Item) = N_With_Clause
4937 and then Comes_From_Source (Item)
4938 and then Entity (Name (Item)) = P
4939 then
4940 return;
4941 end if;
4943 Next (Item);
4944 end loop;
4945 end;
4947 -- If this is a child body, assume that the nonlimited with_clause
4948 -- appears in an ancestor. Could be refined ???
4950 if Is_Child_Unit
4951 (Defining_Entity
4952 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
4953 then
4954 return;
4955 end if;
4957 else
4959 -- If in package declaration, nonlimited view brought in from
4960 -- parent unit or some error condition.
4962 return;
4963 end if;
4964 end if;
4966 if Debug_Flag_I then
4967 Write_Str ("install limited view of ");
4968 Write_Name (Chars (P));
4969 Write_Eol;
4970 end if;
4972 -- If the unit has not been analyzed and the limited view has not been
4973 -- already installed then we install it.
4975 if not Analyzed (P_Unit) then
4976 if not In_Chain (P) then
4978 -- Minimum decoration
4980 Set_Ekind (P, E_Package);
4981 Set_Etype (P, Standard_Void_Type);
4982 Set_Scope (P, Standard_Standard);
4983 Set_Is_Visible_Lib_Unit (P);
4985 if Is_Child_Package then
4986 Set_Is_Child_Unit (P);
4987 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4988 end if;
4990 -- Place entity on visibility structure
4992 Set_Homonym (P, Current_Entity (P));
4993 Set_Current_Entity (P);
4995 if Debug_Flag_I then
4996 Write_Str (" (homonym) chain ");
4997 Write_Name (Chars (P));
4998 Write_Eol;
4999 end if;
5001 -- Install the incomplete view. The first element of the limited
5002 -- view is a header (an E_Package entity) used to reference the
5003 -- first shadow entity in the private part of the package.
5005 Lim_Header := Limited_View (P);
5006 Lim_Typ := First_Entity (Lim_Header);
5008 while Present (Lim_Typ)
5009 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5010 loop
5011 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
5012 Set_Current_Entity (Lim_Typ);
5014 if Debug_Flag_I then
5015 Write_Str (" (homonym) chain ");
5016 Write_Name (Chars (Lim_Typ));
5017 Write_Eol;
5018 end if;
5020 Next_Entity (Lim_Typ);
5021 end loop;
5022 end if;
5024 -- If the unit appears in a previous regular with_clause, the regular
5025 -- entities of the public part of the withed package must be replaced
5026 -- by the shadow ones.
5028 -- This code must be kept synchronized with the code that replaces the
5029 -- shadow entities by the real entities (see body of Remove_Limited
5030 -- With_Clause); otherwise the contents of the homonym chains are not
5031 -- consistent.
5033 else
5034 -- Hide all the type entities of the public part of the package to
5035 -- avoid its usage. This is needed to cover all the subtype decla-
5036 -- rations because we do not remove them from the homonym chain.
5038 E := First_Entity (P);
5039 while Present (E) and then E /= First_Private_Entity (P) loop
5040 if Is_Type (E) then
5041 Set_Was_Hidden (E, Is_Hidden (E));
5042 Set_Is_Hidden (E);
5043 end if;
5045 Next_Entity (E);
5046 end loop;
5048 -- Replace the real entities by the shadow entities of the limited
5049 -- view. The first element of the limited view is a header that is
5050 -- used to reference the first shadow entity in the private part
5051 -- of the package. Successive elements are the limited views of the
5052 -- type (including regular incomplete types) declared in the package.
5054 Lim_Header := Limited_View (P);
5056 Lim_Typ := First_Entity (Lim_Header);
5057 while Present (Lim_Typ)
5058 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5059 loop
5060 pragma Assert (not In_Chain (Lim_Typ));
5062 -- Do not unchain nested packages and child units
5064 if Ekind (Lim_Typ) /= E_Package
5065 and then not Is_Child_Unit (Lim_Typ)
5066 then
5067 declare
5068 Prev : Entity_Id;
5070 begin
5071 Prev := Current_Entity (Lim_Typ);
5072 E := Prev;
5074 -- Replace E in the homonyms list, so that the limited view
5075 -- becomes available.
5077 -- If the non-limited view is a record with an anonymous
5078 -- self-referential component, the analysis of the record
5079 -- declaration creates an incomplete type with the same name
5080 -- in order to define an internal access type. The visible
5081 -- entity is now the incomplete type, and that is the one to
5082 -- replace in the visibility structure.
5084 if E = Non_Limited_View (Lim_Typ)
5085 or else
5086 (Ekind (E) = E_Incomplete_Type
5087 and then Full_View (E) = Non_Limited_View (Lim_Typ))
5088 then
5089 Set_Homonym (Lim_Typ, Homonym (Prev));
5090 Set_Current_Entity (Lim_Typ);
5092 else
5093 loop
5094 E := Homonym (Prev);
5096 -- E may have been removed when installing a previous
5097 -- limited_with_clause.
5099 exit when No (E);
5100 exit when E = Non_Limited_View (Lim_Typ);
5101 Prev := Homonym (Prev);
5102 end loop;
5104 if Present (E) then
5105 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
5106 Set_Homonym (Prev, Lim_Typ);
5107 end if;
5108 end if;
5109 end;
5111 if Debug_Flag_I then
5112 Write_Str (" (homonym) chain ");
5113 Write_Name (Chars (Lim_Typ));
5114 Write_Eol;
5115 end if;
5116 end if;
5118 Next_Entity (Lim_Typ);
5119 end loop;
5120 end if;
5122 -- The package must be visible while the limited-with clause is active
5123 -- because references to the type P.T must resolve in the usual way.
5124 -- In addition, we remember that the limited-view has been installed to
5125 -- uninstall it at the point of context removal.
5127 Set_Is_Immediately_Visible (P);
5128 Set_Limited_View_Installed (N);
5130 -- If unit has not been analyzed in some previous context, check
5131 -- (imperfectly ???) whether it might need a body.
5133 if not Analyzed (P_Unit) then
5134 Check_Body_Required;
5135 end if;
5137 -- If the package in the limited_with clause is a child unit, the clause
5138 -- is unanalyzed and appears as a selected component. Recast it as an
5139 -- expanded name so that the entity can be properly set. Use entity of
5140 -- parent, if available, for higher ancestors in the name.
5142 if Nkind (Name (N)) = N_Selected_Component then
5143 declare
5144 Nam : Node_Id;
5145 Ent : Entity_Id;
5147 begin
5148 Nam := Name (N);
5149 Ent := P;
5150 while Nkind (Nam) = N_Selected_Component
5151 and then Present (Ent)
5152 loop
5153 Change_Selected_Component_To_Expanded_Name (Nam);
5155 -- Set entity of parent identifiers if the unit is a child
5156 -- unit. This ensures that the tree is properly formed from
5157 -- semantic point of view (e.g. for ASIS queries). The unit
5158 -- entities are not fully analyzed, so we need to follow unit
5159 -- links in the tree.
5161 Set_Entity (Nam, Ent);
5163 Nam := Prefix (Nam);
5164 Ent :=
5165 Defining_Entity
5166 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5168 -- Set entity of last ancestor
5170 if Nkind (Nam) = N_Identifier then
5171 Set_Entity (Nam, Ent);
5172 end if;
5173 end loop;
5174 end;
5175 end if;
5177 Set_Entity (Name (N), P);
5178 Set_From_Limited_With (P);
5179 end Install_Limited_Withed_Unit;
5181 -------------------------
5182 -- Install_Withed_Unit --
5183 -------------------------
5185 procedure Install_Withed_Unit
5186 (With_Clause : Node_Id;
5187 Private_With_OK : Boolean := False)
5189 Uname : constant Entity_Id := Entity (Name (With_Clause));
5190 P : constant Entity_Id := Scope (Uname);
5192 begin
5193 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5194 -- compiling a package declaration and the Private_With_OK flag was not
5195 -- set by the caller. These declarations will be installed later (before
5196 -- analyzing the private part of the package).
5198 if Private_Present (With_Clause)
5199 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
5200 and then not (Private_With_OK)
5201 then
5202 return;
5203 end if;
5205 if Debug_Flag_I then
5206 if Private_Present (With_Clause) then
5207 Write_Str ("install private withed unit ");
5208 else
5209 Write_Str ("install withed unit ");
5210 end if;
5212 Write_Name (Chars (Uname));
5213 Write_Eol;
5214 end if;
5216 -- We do not apply the restrictions to an internal unit unless we are
5217 -- compiling the internal unit as a main unit. This check is also
5218 -- skipped for dummy units (for missing packages).
5220 if Sloc (Uname) /= No_Location
5221 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
5222 or else Current_Sem_Unit = Main_Unit)
5223 then
5224 Check_Restricted_Unit
5225 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5226 end if;
5228 if P /= Standard_Standard then
5230 -- If the unit is not analyzed after analysis of the with clause and
5231 -- it is an instantiation then it awaits a body and is the main unit.
5232 -- Its appearance in the context of some other unit indicates a
5233 -- circular dependency (DEC suite perversity).
5235 if not Analyzed (Uname)
5236 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5237 then
5238 Error_Msg_N
5239 ("instantiation depends on itself", Name (With_Clause));
5241 elsif not Is_Visible_Lib_Unit (Uname) then
5243 -- Abandon processing in case of previous errors
5245 if No (Scope (Uname)) then
5246 Check_Error_Detected;
5247 return;
5248 end if;
5250 Set_Is_Visible_Lib_Unit (Uname);
5252 -- If the unit is a wrapper package for a compilation unit that is
5253 -- a subprogrm instance, indicate that the instance itself is a
5254 -- visible unit. This is necessary if the instance is inlined.
5256 if Is_Wrapper_Package (Uname) then
5257 Set_Is_Visible_Lib_Unit (Related_Instance (Uname));
5258 end if;
5260 -- If the child unit appears in the context of its parent, it is
5261 -- immediately visible.
5263 if In_Open_Scopes (Scope (Uname)) then
5264 Set_Is_Immediately_Visible (Uname);
5265 end if;
5267 if Is_Generic_Instance (Uname)
5268 and then Ekind (Uname) in Subprogram_Kind
5269 then
5270 -- Set flag as well on the visible entity that denotes the
5271 -- instance, which renames the current one.
5273 Set_Is_Visible_Lib_Unit
5274 (Related_Instance
5275 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5276 end if;
5278 -- The parent unit may have been installed already, and may have
5279 -- appeared in a use clause.
5281 if In_Use (Scope (Uname)) then
5282 Set_Is_Potentially_Use_Visible (Uname);
5283 end if;
5285 Set_Context_Installed (With_Clause);
5286 end if;
5288 elsif not Is_Immediately_Visible (Uname) then
5289 Set_Is_Visible_Lib_Unit (Uname);
5291 if not Private_Present (With_Clause) or else Private_With_OK then
5292 Set_Is_Immediately_Visible (Uname);
5293 end if;
5295 Set_Context_Installed (With_Clause);
5296 end if;
5298 -- A with-clause overrides a with-type clause: there are no restric-
5299 -- tions on the use of package entities.
5301 if Ekind (Uname) = E_Package then
5302 Set_From_Limited_With (Uname, False);
5303 end if;
5305 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5306 -- unit if there is a visible homograph for it declared in the same
5307 -- declarative region. This pathological case can only arise when an
5308 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5309 -- G1 has a generic child also named G2, and the context includes with_
5310 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5311 -- of I1.G2 visible as well. If the child unit is named Standard, do
5312 -- not apply the check to the Standard package itself.
5314 if Is_Child_Unit (Uname)
5315 and then Is_Visible_Lib_Unit (Uname)
5316 and then Ada_Version >= Ada_2005
5317 then
5318 declare
5319 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5320 Decl2 : Node_Id;
5321 P2 : Entity_Id;
5322 U2 : Entity_Id;
5324 begin
5325 U2 := Homonym (Uname);
5326 while Present (U2) and then U2 /= Standard_Standard loop
5327 P2 := Scope (U2);
5328 Decl2 := Unit_Declaration_Node (P2);
5330 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5331 if Is_Generic_Instance (P)
5332 and then Nkind (Decl1) = N_Package_Declaration
5333 and then Generic_Parent (Specification (Decl1)) = P2
5334 then
5335 Error_Msg_N ("illegal with_clause", With_Clause);
5336 Error_Msg_N
5337 ("\child unit has visible homograph" &
5338 " (RM 8.3(26), 10.1.1(19))",
5339 With_Clause);
5340 exit;
5342 elsif Is_Generic_Instance (P2)
5343 and then Nkind (Decl2) = N_Package_Declaration
5344 and then Generic_Parent (Specification (Decl2)) = P
5345 then
5346 -- With_clause for child unit of instance appears before
5347 -- in the context. We want to place the error message on
5348 -- it, not on the generic child unit itself.
5350 declare
5351 Prev_Clause : Node_Id;
5353 begin
5354 Prev_Clause := First (List_Containing (With_Clause));
5355 while Entity (Name (Prev_Clause)) /= U2 loop
5356 Next (Prev_Clause);
5357 end loop;
5359 pragma Assert (Present (Prev_Clause));
5360 Error_Msg_N ("illegal with_clause", Prev_Clause);
5361 Error_Msg_N
5362 ("\child unit has visible homograph" &
5363 " (RM 8.3(26), 10.1.1(19))",
5364 Prev_Clause);
5365 exit;
5366 end;
5367 end if;
5368 end if;
5370 U2 := Homonym (U2);
5371 end loop;
5372 end;
5373 end if;
5374 end Install_Withed_Unit;
5376 -------------------
5377 -- Is_Child_Spec --
5378 -------------------
5380 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5381 K : constant Node_Kind := Nkind (Lib_Unit);
5383 begin
5384 return (K in N_Generic_Declaration or else
5385 K in N_Generic_Instantiation or else
5386 K in N_Generic_Renaming_Declaration or else
5387 K = N_Package_Declaration or else
5388 K = N_Package_Renaming_Declaration or else
5389 K = N_Subprogram_Declaration or else
5390 K = N_Subprogram_Renaming_Declaration)
5391 and then Present (Parent_Spec (Lib_Unit));
5392 end Is_Child_Spec;
5394 ------------------------------------
5395 -- Is_Legal_Shadow_Entity_In_Body --
5396 ------------------------------------
5398 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5399 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5400 begin
5401 return Nkind (Unit (C_Unit)) = N_Package_Body
5402 and then
5403 Has_With_Clause
5404 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5405 end Is_Legal_Shadow_Entity_In_Body;
5407 ----------------------
5408 -- Is_Ancestor_Unit --
5409 ----------------------
5411 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5412 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5413 E2 : Entity_Id;
5414 begin
5415 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
5416 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5417 return Is_Ancestor_Package (E1, E2);
5418 else
5419 return False;
5420 end if;
5421 end Is_Ancestor_Unit;
5423 -----------------------
5424 -- Load_Needed_Body --
5425 -----------------------
5427 -- N is a generic unit named in a with clause, or else it is a unit that
5428 -- contains a generic unit or an inlined function. In order to perform an
5429 -- instantiation, the body of the unit must be present. If the unit itself
5430 -- is generic, we assume that an instantiation follows, and load & analyze
5431 -- the body unconditionally. This forces analysis of the spec as well.
5433 -- If the unit is not generic, but contains a generic unit, it is loaded on
5434 -- demand, at the point of instantiation (see ch12).
5436 procedure Load_Needed_Body
5437 (N : Node_Id;
5438 OK : out Boolean;
5439 Do_Analyze : Boolean := True)
5441 Body_Name : Unit_Name_Type;
5442 Unum : Unit_Number_Type;
5444 Save_Style_Check : constant Boolean := Opt.Style_Check;
5445 -- The loading and analysis is done with style checks off
5447 begin
5448 if not GNAT_Mode then
5449 Style_Check := False;
5450 end if;
5452 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5453 Unum :=
5454 Load_Unit
5455 (Load_Name => Body_Name,
5456 Required => False,
5457 Subunit => False,
5458 Error_Node => N,
5459 Renamings => True);
5461 if Unum = No_Unit then
5462 OK := False;
5464 else
5465 Compiler_State := Analyzing; -- reset after load
5467 if Fatal_Error (Unum) /= Error_Detected or else Try_Semantics then
5468 if Debug_Flag_L then
5469 Write_Str ("*** Loaded generic body");
5470 Write_Eol;
5471 end if;
5473 if Do_Analyze then
5474 Semantics (Cunit (Unum));
5475 end if;
5476 end if;
5478 OK := True;
5479 end if;
5481 Style_Check := Save_Style_Check;
5482 end Load_Needed_Body;
5484 -------------------------
5485 -- Build_Limited_Views --
5486 -------------------------
5488 procedure Build_Limited_Views (N : Node_Id) is
5489 Unum : constant Unit_Number_Type :=
5490 Get_Source_Unit (Library_Unit (N));
5491 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
5493 Shadow_Pack : Entity_Id;
5494 -- The corresponding shadow entity of the withed package. This entity
5495 -- offers incomplete views of packages and types as well as abstract
5496 -- views of states and variables declared within.
5498 Last_Shadow : Entity_Id := Empty;
5499 -- The last shadow entity created by routine Build_Shadow_Entity
5501 procedure Build_Shadow_Entity
5502 (Ent : Entity_Id;
5503 Scop : Entity_Id;
5504 Shadow : out Entity_Id;
5505 Is_Tagged : Boolean := False);
5506 -- Create a shadow entity that hides Ent and offers an abstract or
5507 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5508 -- should be set when Ent is a tagged type. The generated entity is
5509 -- added to Lim_Header. This routine updates the value of Last_Shadow.
5511 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
5512 -- Perform minimal decoration of a package or its corresponding shadow
5513 -- entity denoted by Ent. Scop is the proper scope.
5515 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id);
5516 -- Perform full decoration of an abstract state or its corresponding
5517 -- shadow entity denoted by Ent. Scop is the proper scope.
5519 procedure Decorate_Type
5520 (Ent : Entity_Id;
5521 Scop : Entity_Id;
5522 Is_Tagged : Boolean := False;
5523 Materialize : Boolean := False);
5524 -- Perform minimal decoration of a type or its corresponding shadow
5525 -- entity denoted by Ent. Scop is the proper scope. Flag Is_Tagged
5526 -- should be set when Ent is a tagged type. Flag Materialize should be
5527 -- set when Ent is a tagged type and its class-wide type needs to appear
5528 -- in the tree.
5530 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id);
5531 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5532 -- proper scope.
5534 procedure Process_Declarations_And_States
5535 (Pack : Entity_Id;
5536 Decls : List_Id;
5537 Scop : Entity_Id;
5538 Create_Abstract_Views : Boolean);
5539 -- Inspect the states of package Pack and declarative list Decls. Create
5540 -- shadow entities for all nested packages, states, types and variables
5541 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5542 -- be set when the abstract states and variables need to be processed.
5544 -------------------------
5545 -- Build_Shadow_Entity --
5546 -------------------------
5548 procedure Build_Shadow_Entity
5549 (Ent : Entity_Id;
5550 Scop : Entity_Id;
5551 Shadow : out Entity_Id;
5552 Is_Tagged : Boolean := False)
5554 begin
5555 Shadow := Make_Temporary (Sloc (Ent), 'Z');
5557 -- The shadow entity must share the same name and parent as the
5558 -- entity it hides.
5560 Set_Chars (Shadow, Chars (Ent));
5561 Set_Parent (Shadow, Parent (Ent));
5563 -- The abstract view of a variable is a state, not another variable
5565 if Ekind (Ent) = E_Variable then
5566 Set_Ekind (Shadow, E_Abstract_State);
5567 else
5568 Set_Ekind (Shadow, Ekind (Ent));
5569 end if;
5571 Set_Is_Internal (Shadow);
5572 Set_From_Limited_With (Shadow);
5574 -- Add the new shadow entity to the limited view of the package
5576 Last_Shadow := Shadow;
5577 Append_Entity (Shadow, Shadow_Pack);
5579 -- Perform context-specific decoration of the shadow entity
5581 if Ekind (Ent) = E_Abstract_State then
5582 Decorate_State (Shadow, Scop);
5583 Set_Non_Limited_View (Shadow, Ent);
5585 elsif Ekind (Ent) = E_Package then
5586 Decorate_Package (Shadow, Scop);
5588 elsif Is_Type (Ent) then
5589 Decorate_Type (Shadow, Scop, Is_Tagged);
5590 Set_Non_Limited_View (Shadow, Ent);
5592 if Is_Tagged then
5593 Set_Non_Limited_View
5594 (Class_Wide_Type (Shadow), Class_Wide_Type (Ent));
5595 end if;
5597 if Is_Incomplete_Or_Private_Type (Ent) then
5598 Set_Private_Dependents (Shadow, New_Elmt_List);
5599 end if;
5601 elsif Ekind (Ent) = E_Variable then
5602 Decorate_State (Shadow, Scop);
5603 Set_Non_Limited_View (Shadow, Ent);
5604 end if;
5605 end Build_Shadow_Entity;
5607 ----------------------
5608 -- Decorate_Package --
5609 ----------------------
5611 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
5612 begin
5613 Set_Ekind (Ent, E_Package);
5614 Set_Etype (Ent, Standard_Void_Type);
5615 Set_Scope (Ent, Scop);
5616 end Decorate_Package;
5618 --------------------
5619 -- Decorate_State --
5620 --------------------
5622 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id) is
5623 begin
5624 Set_Ekind (Ent, E_Abstract_State);
5625 Set_Etype (Ent, Standard_Void_Type);
5626 Set_Scope (Ent, Scop);
5627 Set_Encapsulating_State (Ent, Empty);
5628 end Decorate_State;
5630 -------------------
5631 -- Decorate_Type --
5632 -------------------
5634 procedure Decorate_Type
5635 (Ent : Entity_Id;
5636 Scop : Entity_Id;
5637 Is_Tagged : Boolean := False;
5638 Materialize : Boolean := False)
5640 CW_Typ : Entity_Id;
5642 begin
5643 -- An unanalyzed type or a shadow entity of a type is treated as an
5644 -- incomplete type, and carries the corresponding attributes.
5646 Set_Ekind (Ent, E_Incomplete_Type);
5647 Set_Etype (Ent, Ent);
5648 Set_Full_View (Ent, Empty);
5649 Set_Is_First_Subtype (Ent);
5650 Set_Scope (Ent, Scop);
5651 Set_Stored_Constraint (Ent, No_Elist);
5652 Init_Size_Align (Ent);
5654 if From_Limited_With (Ent) then
5655 Set_Private_Dependents (Ent, New_Elmt_List);
5656 end if;
5658 -- A tagged type and its corresponding shadow entity share one common
5659 -- class-wide type. The list of primitive operations for the shadow
5660 -- entity is empty.
5662 if Is_Tagged then
5663 Set_Is_Tagged_Type (Ent);
5664 Set_Direct_Primitive_Operations (Ent, New_Elmt_List);
5666 CW_Typ :=
5667 New_External_Entity
5668 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
5670 Set_Class_Wide_Type (Ent, CW_Typ);
5672 -- Set parent to be the same as the parent of the tagged type.
5673 -- We need a parent field set, and it is supposed to point to
5674 -- the declaration of the type. The tagged type declaration
5675 -- essentially declares two separate types, the tagged type
5676 -- itself and the corresponding class-wide type, so it is
5677 -- reasonable for the parent fields to point to the declaration
5678 -- in both cases.
5680 Set_Parent (CW_Typ, Parent (Ent));
5682 Set_Ekind (CW_Typ, E_Class_Wide_Type);
5683 Set_Class_Wide_Type (CW_Typ, CW_Typ);
5684 Set_Etype (CW_Typ, Ent);
5685 Set_Equivalent_Type (CW_Typ, Empty);
5686 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
5687 Set_Has_Unknown_Discriminants (CW_Typ);
5688 Set_Is_First_Subtype (CW_Typ);
5689 Set_Is_Tagged_Type (CW_Typ);
5690 Set_Materialize_Entity (CW_Typ, Materialize);
5691 Set_Scope (CW_Typ, Scop);
5692 Init_Size_Align (CW_Typ);
5693 end if;
5694 end Decorate_Type;
5696 -----------------------
5697 -- Decorate_Variable --
5698 -----------------------
5700 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id) is
5701 begin
5702 Set_Ekind (Ent, E_Variable);
5703 Set_Etype (Ent, Standard_Void_Type);
5704 Set_Scope (Ent, Scop);
5705 end Decorate_Variable;
5707 -------------------------------------
5708 -- Process_Declarations_And_States --
5709 -------------------------------------
5711 procedure Process_Declarations_And_States
5712 (Pack : Entity_Id;
5713 Decls : List_Id;
5714 Scop : Entity_Id;
5715 Create_Abstract_Views : Boolean)
5717 procedure Find_And_Process_States;
5718 -- Determine whether package Pack defines abstract state either by
5719 -- using an aspect or a pragma. If this is the case, build shadow
5720 -- entities for all abstract states of Pack.
5722 procedure Process_States (States : Elist_Id);
5723 -- Generate shadow entities for all abstract states in list States
5725 -----------------------------
5726 -- Find_And_Process_States --
5727 -----------------------------
5729 procedure Find_And_Process_States is
5730 procedure Process_State (State : Node_Id);
5731 -- Generate shadow entities for a single abstract state or
5732 -- multiple states expressed as an aggregate.
5734 -------------------
5735 -- Process_State --
5736 -------------------
5738 procedure Process_State (State : Node_Id) is
5739 Loc : constant Source_Ptr := Sloc (State);
5740 Decl : Node_Id;
5741 Dummy : Entity_Id;
5742 Elmt : Node_Id;
5743 Id : Entity_Id;
5745 begin
5746 -- Multiple abstract states appear as an aggregate
5748 if Nkind (State) = N_Aggregate then
5749 Elmt := First (Expressions (State));
5750 while Present (Elmt) loop
5751 Process_State (Elmt);
5752 Next (Elmt);
5753 end loop;
5755 return;
5757 -- A null state has no abstract view
5759 elsif Nkind (State) = N_Null then
5760 return;
5762 -- State declaration with various options appears as an
5763 -- extension aggregate.
5765 elsif Nkind (State) = N_Extension_Aggregate then
5766 Decl := Ancestor_Part (State);
5768 -- Simple state declaration
5770 elsif Nkind (State) = N_Identifier then
5771 Decl := State;
5773 -- Possibly an illegal state declaration
5775 else
5776 return;
5777 end if;
5779 -- Abstract states are elaborated when the related pragma is
5780 -- elaborated. Since the withed package is not analyzed yet,
5781 -- the entities of the abstract states are not available. To
5782 -- overcome this complication, create the entities now and
5783 -- store them in their respective declarations. The entities
5784 -- are later used by routine Create_Abstract_State to declare
5785 -- and enter the states into visibility.
5787 if No (Entity (Decl)) then
5788 Id := Make_Defining_Identifier (Loc, Chars (Decl));
5790 Set_Entity (Decl, Id);
5791 Set_Parent (Id, State);
5792 Decorate_State (Id, Scop);
5794 -- Otherwise the package was previously withed
5796 else
5797 Id := Entity (Decl);
5798 end if;
5800 Build_Shadow_Entity (Id, Scop, Dummy);
5801 end Process_State;
5803 -- Local variables
5805 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack);
5806 Asp : Node_Id;
5807 Decl : Node_Id;
5809 -- Start of processing for Find_And_Process_States
5811 begin
5812 -- Find aspect Abstract_State
5814 Asp := First (Aspect_Specifications (Pack_Decl));
5815 while Present (Asp) loop
5816 if Chars (Identifier (Asp)) = Name_Abstract_State then
5817 Process_State (Expression (Asp));
5819 return;
5820 end if;
5822 Next (Asp);
5823 end loop;
5825 -- Find pragma Abstract_State by inspecting the declarations
5827 Decl := First (Decls);
5828 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
5829 if Pragma_Name_Mapped (Decl) = Name_Abstract_State then
5830 Process_State
5831 (Get_Pragma_Arg
5832 (First (Pragma_Argument_Associations (Decl))));
5834 return;
5835 end if;
5837 Next (Decl);
5838 end loop;
5839 end Find_And_Process_States;
5841 --------------------
5842 -- Process_States --
5843 --------------------
5845 procedure Process_States (States : Elist_Id) is
5846 Dummy : Entity_Id;
5847 Elmt : Elmt_Id;
5849 begin
5850 Elmt := First_Elmt (States);
5851 while Present (Elmt) loop
5852 Build_Shadow_Entity (Node (Elmt), Scop, Dummy);
5854 Next_Elmt (Elmt);
5855 end loop;
5856 end Process_States;
5858 -- Local variables
5860 Is_Tagged : Boolean;
5861 Decl : Node_Id;
5862 Def : Node_Id;
5863 Def_Id : Entity_Id;
5864 Shadow : Entity_Id;
5866 -- Start of processing for Process_Declarations_And_States
5868 begin
5869 -- Build abstract views for all states defined in the package
5871 if Create_Abstract_Views then
5873 -- When a package has been analyzed, all states are stored in list
5874 -- Abstract_States. Generate the shadow entities directly.
5876 if Is_Analyzed then
5877 if Present (Abstract_States (Pack)) then
5878 Process_States (Abstract_States (Pack));
5879 end if;
5881 -- The package may declare abstract states by using an aspect or a
5882 -- pragma. Attempt to locate one of these construct and if found,
5883 -- build the shadow entities.
5885 else
5886 Find_And_Process_States;
5887 end if;
5888 end if;
5890 -- Inspect the declarative list, looking for nested packages, types
5891 -- and variable declarations.
5893 Decl := First (Decls);
5894 while Present (Decl) loop
5896 -- Packages
5898 if Nkind (Decl) = N_Package_Declaration then
5899 Def_Id := Defining_Entity (Decl);
5901 -- Perform minor decoration when the withed package has not
5902 -- been analyzed.
5904 if not Is_Analyzed then
5905 Decorate_Package (Def_Id, Scop);
5906 end if;
5908 -- Create a shadow entity that offers a limited view of all
5909 -- visible types declared within.
5911 Build_Shadow_Entity (Def_Id, Scop, Shadow);
5913 Process_Declarations_And_States
5914 (Pack => Def_Id,
5915 Decls => Visible_Declarations (Specification (Decl)),
5916 Scop => Shadow,
5917 Create_Abstract_Views => Create_Abstract_Views);
5919 -- Types
5921 elsif Nkind_In (Decl, N_Full_Type_Declaration,
5922 N_Incomplete_Type_Declaration,
5923 N_Private_Extension_Declaration,
5924 N_Private_Type_Declaration,
5925 N_Protected_Type_Declaration,
5926 N_Task_Type_Declaration)
5927 then
5928 Def_Id := Defining_Entity (Decl);
5930 -- Determine whether the type is tagged. Note that packages
5931 -- included via a limited with clause are not always analyzed,
5932 -- hence the tree lookup rather than the use of attribute
5933 -- Is_Tagged_Type.
5935 if Nkind (Decl) = N_Full_Type_Declaration then
5936 Def := Type_Definition (Decl);
5938 Is_Tagged :=
5939 (Nkind (Def) = N_Record_Definition
5940 and then Tagged_Present (Def))
5941 or else
5942 (Nkind (Def) = N_Derived_Type_Definition
5943 and then Present (Record_Extension_Part (Def)));
5945 elsif Nkind_In (Decl, N_Incomplete_Type_Declaration,
5946 N_Private_Type_Declaration)
5947 then
5948 Is_Tagged := Tagged_Present (Decl);
5950 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5951 Is_Tagged := True;
5953 else
5954 Is_Tagged := False;
5955 end if;
5957 -- Perform minor decoration when the withed package has not
5958 -- been analyzed.
5960 if not Is_Analyzed then
5961 Decorate_Type (Def_Id, Scop, Is_Tagged, True);
5962 end if;
5964 -- Create a shadow entity that hides the type and offers an
5965 -- incomplete view of the said type.
5967 Build_Shadow_Entity (Def_Id, Scop, Shadow, Is_Tagged);
5969 -- Variables
5971 elsif Create_Abstract_Views
5972 and then Nkind (Decl) = N_Object_Declaration
5973 and then not Constant_Present (Decl)
5974 then
5975 Def_Id := Defining_Entity (Decl);
5977 -- Perform minor decoration when the withed package has not
5978 -- been analyzed.
5980 if not Is_Analyzed then
5981 Decorate_Variable (Def_Id, Scop);
5982 end if;
5984 -- Create a shadow entity that hides the variable and offers an
5985 -- abstract view of the said variable.
5987 Build_Shadow_Entity (Def_Id, Scop, Shadow);
5988 end if;
5990 Next (Decl);
5991 end loop;
5992 end Process_Declarations_And_States;
5994 -- Local variables
5996 Nam : constant Node_Id := Name (N);
5997 Pack : constant Entity_Id := Cunit_Entity (Unum);
5999 Last_Public_Shadow : Entity_Id := Empty;
6000 Private_Shadow : Entity_Id;
6001 Spec : Node_Id;
6003 -- Start of processing for Build_Limited_Views
6005 begin
6006 pragma Assert (Limited_Present (N));
6008 -- A library_item mentioned in a limited_with_clause is a package
6009 -- declaration, not a subprogram declaration, generic declaration,
6010 -- generic instantiation, or package renaming declaration.
6012 case Nkind (Unit (Library_Unit (N))) is
6013 when N_Package_Declaration =>
6014 null;
6016 when N_Subprogram_Declaration =>
6017 Error_Msg_N ("subprograms not allowed in limited with_clauses", N);
6018 return;
6020 when N_Generic_Package_Declaration |
6021 N_Generic_Subprogram_Declaration =>
6022 Error_Msg_N ("generics not allowed in limited with_clauses", N);
6023 return;
6025 when N_Generic_Instantiation =>
6026 Error_Msg_N
6027 ("generic instantiations not allowed in limited with_clauses",
6029 return;
6031 when N_Generic_Renaming_Declaration =>
6032 Error_Msg_N
6033 ("generic renamings not allowed in limited with_clauses", N);
6034 return;
6036 when N_Subprogram_Renaming_Declaration =>
6037 Error_Msg_N
6038 ("renamed subprograms not allowed in limited with_clauses", N);
6039 return;
6041 when N_Package_Renaming_Declaration =>
6042 Error_Msg_N
6043 ("renamed packages not allowed in limited with_clauses", N);
6044 return;
6046 when others =>
6047 raise Program_Error;
6048 end case;
6050 -- The withed unit may not be analyzed, but the with calause itself
6051 -- must be minimally decorated. This ensures that the checks on unused
6052 -- with clauses also process limieted withs.
6054 Set_Ekind (Pack, E_Package);
6055 Set_Etype (Pack, Standard_Void_Type);
6057 if Is_Entity_Name (Nam) then
6058 Set_Entity (Nam, Pack);
6060 elsif Nkind (Nam) = N_Selected_Component then
6061 Set_Entity (Selector_Name (Nam), Pack);
6062 end if;
6064 -- Check if the chain is already built
6066 Spec := Specification (Unit (Library_Unit (N)));
6068 if Limited_View_Installed (Spec) then
6069 return;
6070 end if;
6072 -- Create the shadow package wich hides the withed unit and provides
6073 -- incomplete view of all types and packages declared within.
6075 Shadow_Pack := Make_Temporary (Sloc (N), 'Z');
6076 Set_Ekind (Shadow_Pack, E_Package);
6077 Set_Is_Internal (Shadow_Pack);
6078 Set_Limited_View (Pack, Shadow_Pack);
6080 -- Inspect the abstract states and visible declarations of the withed
6081 -- unit and create shadow entities that hide existing packages, states,
6082 -- variables and types.
6084 Process_Declarations_And_States
6085 (Pack => Pack,
6086 Decls => Visible_Declarations (Spec),
6087 Scop => Pack,
6088 Create_Abstract_Views => True);
6090 Last_Public_Shadow := Last_Shadow;
6092 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6093 -- to accomodate limited private with clauses.
6095 Process_Declarations_And_States
6096 (Pack => Pack,
6097 Decls => Private_Declarations (Spec),
6098 Scop => Pack,
6099 Create_Abstract_Views => False);
6101 if Present (Last_Public_Shadow) then
6102 Private_Shadow := Next_Entity (Last_Public_Shadow);
6103 else
6104 Private_Shadow := First_Entity (Shadow_Pack);
6105 end if;
6107 Set_First_Private_Entity (Shadow_Pack, Private_Shadow);
6108 Set_Limited_View_Installed (Spec);
6109 end Build_Limited_Views;
6111 ----------------------------
6112 -- Check_No_Elab_Code_All --
6113 ----------------------------
6115 procedure Check_No_Elab_Code_All (N : Node_Id) is
6116 begin
6117 if Present (No_Elab_Code_All_Pragma)
6118 and then In_Extended_Main_Source_Unit (N)
6119 and then Present (Context_Items (N))
6120 then
6121 declare
6122 CL : constant List_Id := Context_Items (N);
6123 CI : Node_Id;
6125 begin
6126 CI := First (CL);
6127 while Present (CI) loop
6128 if Nkind (CI) = N_With_Clause
6129 and then not
6130 No_Elab_Code_All (Get_Source_Unit (Library_Unit (CI)))
6131 then
6132 Error_Msg_Sloc := Sloc (No_Elab_Code_All_Pragma);
6133 Error_Msg_N
6134 ("violation of No_Elaboration_Code_All#", CI);
6135 Error_Msg_NE
6136 ("\unit& does not have No_Elaboration_Code_All",
6137 CI, Entity (Name (CI)));
6138 end if;
6140 Next (CI);
6141 end loop;
6142 end;
6143 end if;
6144 end Check_No_Elab_Code_All;
6146 -------------------------------
6147 -- Check_Body_Needed_For_SAL --
6148 -------------------------------
6150 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
6151 function Entity_Needs_Body (E : Entity_Id) return Boolean;
6152 -- Determine whether use of entity E might require the presence of its
6153 -- body. For a package this requires a recursive traversal of all nested
6154 -- declarations.
6156 -----------------------
6157 -- Entity_Needs_Body --
6158 -----------------------
6160 function Entity_Needs_Body (E : Entity_Id) return Boolean is
6161 Ent : Entity_Id;
6163 begin
6164 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
6165 return True;
6167 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
6169 -- A generic subprogram always requires the presence of its
6170 -- body because an instantiation needs both templates. The only
6171 -- exceptions is a generic subprogram renaming. In this case the
6172 -- body is needed only when the template is declared outside the
6173 -- compilation unit being checked.
6175 if Present (Renamed_Entity (E)) then
6176 return not Within_Scope (E, Unit_Name);
6177 else
6178 return True;
6179 end if;
6181 elsif Ekind (E) = E_Generic_Package
6182 and then
6183 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
6184 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6185 then
6186 return True;
6188 elsif Ekind (E) = E_Package
6189 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
6190 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6191 then
6192 Ent := First_Entity (E);
6193 while Present (Ent) loop
6194 if Entity_Needs_Body (Ent) then
6195 return True;
6196 end if;
6198 Next_Entity (Ent);
6199 end loop;
6201 return False;
6203 else
6204 return False;
6205 end if;
6206 end Entity_Needs_Body;
6208 -- Start of processing for Check_Body_Needed_For_SAL
6210 begin
6211 if Ekind (Unit_Name) = E_Generic_Package
6212 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6213 N_Generic_Package_Declaration
6214 and then
6215 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
6216 then
6217 Set_Body_Needed_For_SAL (Unit_Name);
6219 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
6220 Set_Body_Needed_For_SAL (Unit_Name);
6222 elsif Is_Subprogram (Unit_Name)
6223 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6224 N_Subprogram_Declaration
6225 and then Has_Pragma_Inline (Unit_Name)
6226 then
6227 Set_Body_Needed_For_SAL (Unit_Name);
6229 elsif Ekind (Unit_Name) = E_Subprogram_Body then
6230 Check_Body_Needed_For_SAL
6231 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6233 elsif Ekind (Unit_Name) = E_Package
6234 and then Entity_Needs_Body (Unit_Name)
6235 then
6236 Set_Body_Needed_For_SAL (Unit_Name);
6238 elsif Ekind (Unit_Name) = E_Package_Body
6239 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
6240 then
6241 Check_Body_Needed_For_SAL
6242 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6243 end if;
6244 end Check_Body_Needed_For_SAL;
6246 --------------------
6247 -- Remove_Context --
6248 --------------------
6250 procedure Remove_Context (N : Node_Id) is
6251 Lib_Unit : constant Node_Id := Unit (N);
6253 begin
6254 -- If this is a child unit, first remove the parent units
6256 if Is_Child_Spec (Lib_Unit) then
6257 Remove_Parents (Lib_Unit);
6258 end if;
6260 Remove_Context_Clauses (N);
6261 end Remove_Context;
6263 ----------------------------
6264 -- Remove_Context_Clauses --
6265 ----------------------------
6267 procedure Remove_Context_Clauses (N : Node_Id) is
6268 Item : Node_Id;
6269 Unit_Name : Entity_Id;
6271 begin
6272 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6273 -- limited-views first and regular-views later (to maintain the
6274 -- stack model).
6276 -- First Phase: Remove limited_with context clauses
6278 Item := First (Context_Items (N));
6279 while Present (Item) loop
6281 -- We are interested only in with clauses which got installed
6282 -- on entry.
6284 if Nkind (Item) = N_With_Clause
6285 and then Limited_Present (Item)
6286 and then Limited_View_Installed (Item)
6287 then
6288 Remove_Limited_With_Clause (Item);
6289 end if;
6291 Next (Item);
6292 end loop;
6294 -- Second Phase: Loop through context items and undo regular
6295 -- with_clauses and use_clauses.
6297 Item := First (Context_Items (N));
6298 while Present (Item) loop
6300 -- We are interested only in with clauses which got installed on
6301 -- entry, as indicated by their Context_Installed flag set
6303 if Nkind (Item) = N_With_Clause
6304 and then Limited_Present (Item)
6305 and then Limited_View_Installed (Item)
6306 then
6307 null;
6309 elsif Nkind (Item) = N_With_Clause
6310 and then Context_Installed (Item)
6311 then
6312 -- Remove items from one with'ed unit
6314 Unit_Name := Entity (Name (Item));
6315 Remove_Unit_From_Visibility (Unit_Name);
6316 Set_Context_Installed (Item, False);
6318 elsif Nkind (Item) = N_Use_Package_Clause then
6319 End_Use_Package (Item);
6321 elsif Nkind (Item) = N_Use_Type_Clause then
6322 End_Use_Type (Item);
6323 end if;
6325 Next (Item);
6326 end loop;
6327 end Remove_Context_Clauses;
6329 --------------------------------
6330 -- Remove_Limited_With_Clause --
6331 --------------------------------
6333 procedure Remove_Limited_With_Clause (N : Node_Id) is
6334 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
6335 E : Entity_Id;
6336 P : Entity_Id;
6337 Lim_Header : Entity_Id;
6338 Lim_Typ : Entity_Id;
6339 Prev : Entity_Id;
6341 begin
6342 pragma Assert (Limited_View_Installed (N));
6344 -- In case of limited with_clause on subprograms, generics, instances,
6345 -- or renamings, the corresponding error was previously posted and we
6346 -- have nothing to do here.
6348 if Nkind (P_Unit) /= N_Package_Declaration then
6349 return;
6350 end if;
6352 P := Defining_Unit_Name (Specification (P_Unit));
6354 -- Handle child packages
6356 if Nkind (P) = N_Defining_Program_Unit_Name then
6357 P := Defining_Identifier (P);
6358 end if;
6360 if Debug_Flag_I then
6361 Write_Str ("remove limited view of ");
6362 Write_Name (Chars (P));
6363 Write_Str (" from visibility");
6364 Write_Eol;
6365 end if;
6367 -- Prepare the removal of the shadow entities from visibility. The first
6368 -- element of the limited view is a header (an E_Package entity) that is
6369 -- used to reference the first shadow entity in the private part of the
6370 -- package
6372 Lim_Header := Limited_View (P);
6373 Lim_Typ := First_Entity (Lim_Header);
6375 -- Remove package and shadow entities from visibility if it has not
6376 -- been analyzed
6378 if not Analyzed (P_Unit) then
6379 Unchain (P);
6380 Set_Is_Immediately_Visible (P, False);
6382 while Present (Lim_Typ) loop
6383 Unchain (Lim_Typ);
6384 Next_Entity (Lim_Typ);
6385 end loop;
6387 -- Otherwise this package has already appeared in the closure and its
6388 -- shadow entities must be replaced by its real entities. This code
6389 -- must be kept synchronized with the complementary code in Install
6390 -- Limited_Withed_Unit.
6392 else
6393 -- If the limited_with_clause is in some other unit in the context
6394 -- then it is not visible in the main unit.
6396 if not In_Extended_Main_Source_Unit (N) then
6397 Set_Is_Immediately_Visible (P, False);
6398 end if;
6400 -- Real entities that are type or subtype declarations were hidden
6401 -- from visibility at the point of installation of the limited-view.
6402 -- Now we recover the previous value of the hidden attribute.
6404 E := First_Entity (P);
6405 while Present (E) and then E /= First_Private_Entity (P) loop
6406 if Is_Type (E) then
6407 Set_Is_Hidden (E, Was_Hidden (E));
6408 end if;
6410 Next_Entity (E);
6411 end loop;
6413 while Present (Lim_Typ)
6414 and then Lim_Typ /= First_Private_Entity (Lim_Header)
6415 loop
6416 -- Nested packages and child units were not unchained
6418 if Ekind (Lim_Typ) /= E_Package
6419 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
6420 then
6421 -- If the package has incomplete types, the limited view of the
6422 -- incomplete type is in fact never visible (AI05-129) but we
6423 -- have created a shadow entity E1 for it, that points to E2,
6424 -- a non-limited incomplete type. This in turn has a full view
6425 -- E3 that is the full declaration. There is a corresponding
6426 -- shadow entity E4. When reinstalling the non-limited view,
6427 -- E2 must become the current entity and E3 must be ignored.
6429 E := Non_Limited_View (Lim_Typ);
6431 if Present (Current_Entity (E))
6432 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
6433 and then Full_View (Current_Entity (E)) = E
6434 then
6436 -- Lim_Typ is the limited view of a full type declaration
6437 -- that has a previous incomplete declaration, i.e. E3 from
6438 -- the previous description. Nothing to insert.
6440 null;
6442 else
6443 pragma Assert (not In_Chain (E));
6445 Prev := Current_Entity (Lim_Typ);
6447 if Prev = Lim_Typ then
6448 Set_Current_Entity (E);
6450 else
6451 while Present (Prev)
6452 and then Homonym (Prev) /= Lim_Typ
6453 loop
6454 Prev := Homonym (Prev);
6455 end loop;
6457 if Present (Prev) then
6458 Set_Homonym (Prev, E);
6459 end if;
6460 end if;
6462 -- Preserve structure of homonym chain
6464 Set_Homonym (E, Homonym (Lim_Typ));
6465 end if;
6466 end if;
6468 Next_Entity (Lim_Typ);
6469 end loop;
6470 end if;
6472 -- Indicate that the limited view of the package is not installed
6474 Set_From_Limited_With (P, False);
6475 Set_Limited_View_Installed (N, False);
6476 end Remove_Limited_With_Clause;
6478 --------------------
6479 -- Remove_Parents --
6480 --------------------
6482 procedure Remove_Parents (Lib_Unit : Node_Id) is
6483 P : Node_Id;
6484 P_Name : Entity_Id;
6485 P_Spec : Node_Id := Empty;
6486 E : Entity_Id;
6487 Vis : constant Boolean :=
6488 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
6490 begin
6491 if Is_Child_Spec (Lib_Unit) then
6492 P_Spec := Parent_Spec (Lib_Unit);
6494 elsif Nkind (Lib_Unit) = N_Package_Body
6495 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6496 then
6497 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6498 end if;
6500 if Present (P_Spec) then
6501 P := Unit (P_Spec);
6502 P_Name := Get_Parent_Entity (P);
6503 Remove_Context_Clauses (P_Spec);
6504 End_Package_Scope (P_Name);
6505 Set_Is_Immediately_Visible (P_Name, Vis);
6507 -- Remove from visibility the siblings as well, which are directly
6508 -- visible while the parent is in scope.
6510 E := First_Entity (P_Name);
6511 while Present (E) loop
6512 if Is_Child_Unit (E) then
6513 Set_Is_Immediately_Visible (E, False);
6514 end if;
6516 Next_Entity (E);
6517 end loop;
6519 Set_In_Package_Body (P_Name, False);
6521 -- This is the recursive call to remove the context of any higher
6522 -- level parent. This recursion ensures that all parents are removed
6523 -- in the reverse order of their installation.
6525 Remove_Parents (P);
6526 end if;
6527 end Remove_Parents;
6529 ---------------------------------
6530 -- Remove_Private_With_Clauses --
6531 ---------------------------------
6533 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6534 Item : Node_Id;
6536 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6537 -- Check whether a given unit appears in a regular with_clause. Used to
6538 -- determine whether a private_with_clause, implicit or explicit, should
6539 -- be ignored.
6541 ----------------------------
6542 -- In_Regular_With_Clause --
6543 ----------------------------
6545 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6547 Item : Node_Id;
6549 begin
6550 Item := First (Context_Items (Comp_Unit));
6551 while Present (Item) loop
6552 if Nkind (Item) = N_With_Clause
6554 -- The following guard is needed to ensure that the name has
6555 -- been properly analyzed before we go fetching its entity.
6557 and then Is_Entity_Name (Name (Item))
6558 and then Entity (Name (Item)) = E
6559 and then not Private_Present (Item)
6560 then
6561 return True;
6562 end if;
6563 Next (Item);
6564 end loop;
6566 return False;
6567 end In_Regular_With_Clause;
6569 -- Start of processing for Remove_Private_With_Clauses
6571 begin
6572 Item := First (Context_Items (Comp_Unit));
6573 while Present (Item) loop
6574 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
6576 -- If private_with_clause is redundant, remove it from context,
6577 -- as a small optimization to subsequent handling of private_with
6578 -- clauses in other nested packages.
6580 if In_Regular_With_Clause (Entity (Name (Item))) then
6581 declare
6582 Nxt : constant Node_Id := Next (Item);
6583 begin
6584 Remove (Item);
6585 Item := Nxt;
6586 end;
6588 elsif Limited_Present (Item) then
6589 if not Limited_View_Installed (Item) then
6590 Remove_Limited_With_Clause (Item);
6591 end if;
6593 Next (Item);
6595 else
6596 Remove_Unit_From_Visibility (Entity (Name (Item)));
6597 Set_Context_Installed (Item, False);
6598 Next (Item);
6599 end if;
6601 else
6602 Next (Item);
6603 end if;
6604 end loop;
6605 end Remove_Private_With_Clauses;
6607 ---------------------------------
6608 -- Remove_Unit_From_Visibility --
6609 ---------------------------------
6611 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6612 begin
6613 if Debug_Flag_I then
6614 Write_Str ("remove unit ");
6615 Write_Name (Chars (Unit_Name));
6616 Write_Str (" from visibility");
6617 Write_Eol;
6618 end if;
6620 Set_Is_Visible_Lib_Unit (Unit_Name, False);
6621 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6622 Set_Is_Immediately_Visible (Unit_Name, False);
6624 -- If the unit is a wrapper package, the subprogram instance is
6625 -- what must be removed from visibility.
6626 -- Should we use Related_Instance instead???
6628 if Is_Wrapper_Package (Unit_Name) then
6629 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
6630 end if;
6631 end Remove_Unit_From_Visibility;
6633 --------
6634 -- sm --
6635 --------
6637 procedure sm is
6638 begin
6639 null;
6640 end sm;
6642 -------------
6643 -- Unchain --
6644 -------------
6646 procedure Unchain (E : Entity_Id) is
6647 Prev : Entity_Id;
6649 begin
6650 Prev := Current_Entity (E);
6652 if No (Prev) then
6653 return;
6655 elsif Prev = E then
6656 Set_Name_Entity_Id (Chars (E), Homonym (E));
6658 else
6659 while Present (Prev) and then Homonym (Prev) /= E loop
6660 Prev := Homonym (Prev);
6661 end loop;
6663 if Present (Prev) then
6664 Set_Homonym (Prev, Homonym (E));
6665 end if;
6666 end if;
6668 if Debug_Flag_I then
6669 Write_Str (" (homonym) unchain ");
6670 Write_Name (Chars (E));
6671 Write_Eol;
6672 end if;
6673 end Unchain;
6675 end Sem_Ch10;