[NDS32] new attribute no_prologue and new option -mret-in-naked-func.
[official-gcc.git] / gcc / ada / sem_ch10.adb
blob357fbde27b159dbbc754ce673ec60c6e546a554f
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-2018, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Exp_Util; use Exp_Util;
33 with Elists; use Elists;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Impunit; use Impunit;
39 with Inline; use Inline;
40 with Lib; use Lib;
41 with Lib.Load; use Lib.Load;
42 with Lib.Xref; use Lib.Xref;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Output; use Output;
48 with Par_SCO; use Par_SCO;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch7; use Sem_Ch7;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Dist; use Sem_Dist;
60 with Sem_Prag; use Sem_Prag;
61 with Sem_Util; use Sem_Util;
62 with Sem_Warn; use Sem_Warn;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinfo.CN; use Sinfo.CN;
66 with Sinput; use Sinput;
67 with Snames; use Snames;
68 with Style; use Style;
69 with Stylesw; use Stylesw;
70 with Tbuild; use Tbuild;
71 with Uname; use Uname;
73 package body Sem_Ch10 is
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Analyze_Context (N : Node_Id);
80 -- Analyzes items in the context clause of compilation unit
82 procedure Build_Limited_Views (N : Node_Id);
83 -- Build and decorate the list of shadow entities for a package mentioned
84 -- in a limited_with clause. If the package was not previously analyzed
85 -- then it also performs a basic decoration of the real entities. This is
86 -- required in order to avoid passing non-decorated entities to the
87 -- back-end. Implements Ada 2005 (AI-50217).
89 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
90 -- Common processing for all stubs (subprograms, tasks, packages, and
91 -- protected cases). N is the stub to be analyzed. Once the subunit name
92 -- is established, load and analyze. Nam is the non-overloadable entity
93 -- for which the proper body provides a completion. Subprogram stubs are
94 -- handled differently because they can be declarations.
96 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
97 -- Check whether the source for the body of a compilation unit must be
98 -- included in a standalone library.
100 procedure Check_No_Elab_Code_All (N : Node_Id);
101 -- Carries out possible tests for violation of No_Elab_Code all for withed
102 -- units in the Context_Items of unit N.
104 procedure Check_Private_Child_Unit (N : Node_Id);
105 -- If a with_clause mentions a private child unit, the compilation unit
106 -- must be a member of the same family, as described in 10.1.2.
108 procedure Check_Stub_Level (N : Node_Id);
109 -- Verify that a stub is declared immediately within a compilation unit,
110 -- and not in an inner frame.
112 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
113 -- When a child unit appears in a context clause, the implicit withs on
114 -- parents are made explicit, and with clauses are inserted in the context
115 -- clause before the one for the child. If a parent in the with_clause
116 -- is a renaming, the implicit with_clause is on the renaming whose name
117 -- is mentioned in the with_clause, and not on the package it renames.
118 -- N is the compilation unit whose list of context items receives the
119 -- implicit with_clauses.
121 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
122 -- Generate cross-reference information for the parents of child units
123 -- and of subunits. N is a defining_program_unit_name, and P_Id is the
124 -- immediate parent scope.
126 function Has_With_Clause
127 (C_Unit : Node_Id;
128 Pack : Entity_Id;
129 Is_Limited : Boolean := False) return Boolean;
130 -- Determine whether compilation unit C_Unit contains a [limited] with
131 -- clause for package Pack. Use the flag Is_Limited to designate desired
132 -- clause kind.
134 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
135 -- If the main unit is a child unit, implicit withs are also added for
136 -- all its ancestors.
138 function In_Chain (E : Entity_Id) return Boolean;
139 -- Check that the shadow entity is not already in the homonym chain, for
140 -- example through a limited_with clause in a parent unit.
142 procedure Install_Context_Clauses (N : Node_Id; Chain : Boolean := True);
143 -- Subsidiary to Install_Context and Install_Parents. Process all with
144 -- and use clauses for current unit and its library unit if any. The flag
145 -- Chain is used to control the "chaining" or linking together of use-type
146 -- and use-package clauses to avoid circularities with reinstalling
147 -- clauses.
149 procedure Install_Limited_Context_Clauses (N : Node_Id);
150 -- Subsidiary to Install_Context. Process only limited with_clauses for
151 -- current unit. Implements Ada 2005 (AI-50217).
153 procedure Install_Limited_With_Clause (N : Node_Id);
154 -- Place shadow entities for a limited_with package in the visibility
155 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
157 procedure Install_Parents
158 (Lib_Unit : Node_Id;
159 Is_Private : Boolean;
160 Chain : Boolean := True);
161 -- This procedure establishes the context for the compilation of a child
162 -- unit. If Lib_Unit is a child library spec then the context of the parent
163 -- is installed, and the parent itself made immediately visible, so that
164 -- the child unit is processed in the declarative region of the parent.
165 -- Install_Parents makes a recursive call to itself to ensure that all
166 -- parents are loaded in the nested case. If Lib_Unit is a library body,
167 -- the only effect of Install_Parents is to install the private decls of
168 -- the parents, because the visible parent declarations will have been
169 -- installed as part of the context of the corresponding spec. The flag
170 -- Chain is used to control the "chaining" or linking of use-type and
171 -- use-package clauses to avoid circularities when installing context.
173 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
174 -- In the compilation of a child unit, a child of any of the ancestor
175 -- units is directly visible if it is visible, because the parent is in
176 -- an enclosing scope. Iterate over context to find child units of U_Name
177 -- or of some ancestor of it.
179 procedure Install_With_Clause
180 (With_Clause : Node_Id;
181 Private_With_OK : Boolean := False);
182 -- If the unit is not a child unit, make unit immediately visible. The
183 -- caller ensures that the unit is not already currently installed. The
184 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
185 -- is called when compiling the private part of a package, or installing
186 -- the private declarations of a parent unit.
188 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
189 -- When compiling a unit Q descended from some parent unit P, a limited
190 -- with_clause in the context of P that names some other ancestor of Q
191 -- must not be installed because the ancestor is immediately visible.
193 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
194 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
195 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
196 -- a library spec that has a parent. If the call to Is_Child_Spec returns
197 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
198 -- compilation unit for the parent spec.
200 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
201 -- Parent_Spec is non-empty, this is also a child unit.
203 procedure Remove_Context_Clauses (N : Node_Id);
204 -- Subsidiary of previous one. Remove use_ and with_clauses
206 procedure Remove_Limited_With_Clause (N : Node_Id);
207 -- Remove the shadow entities from visibility introduced for a package
208 -- mentioned in limited with clause N. Implements Ada 2005 (AI-50217).
210 procedure Remove_Limited_With_Unit
211 (Pack_Decl : Node_Id;
212 Lim_Clause : Node_Id := Empty);
213 -- Remove the shadow entities from visibility introduced for a package
214 -- denoted by declaration Pack_Decl. Lim_Clause is the related limited
215 -- with clause, if any. Implements Ada 2005 (AI-50217).
217 procedure Remove_Parents (Lib_Unit : Node_Id);
218 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
219 -- contexts established by the corresponding call to Install_Parents are
220 -- removed. Remove_Parents contains a recursive call to itself to ensure
221 -- that all parents are removed in the nested case.
223 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
224 -- Reset all visibility flags on unit after compiling it, either as a main
225 -- unit or as a unit in the context.
227 procedure Unchain (E : Entity_Id);
228 -- Remove single entity from visibility list
230 procedure sm;
231 -- A dummy procedure, for debugging use, called just before analyzing the
232 -- main unit (after dealing with any context clauses).
234 --------------------------
235 -- Limited_With_Clauses --
236 --------------------------
238 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
239 -- mutually recursive types declared in different units. A limited_with
240 -- clause that names package P in the context of unit U makes the types
241 -- declared in the visible part of P available within U, but with the
242 -- restriction that these types can only be used as incomplete types.
243 -- The limited_with clause does not impose a semantic dependence on P,
244 -- and it is possible for two packages to have limited_with_clauses on
245 -- each other without creating an elaboration circularity.
247 -- To support this feature, the analysis of a limited_with clause must
248 -- create an abbreviated view of the package, without performing any
249 -- semantic analysis on it. This "package abstract" contains shadow types
250 -- that are in one-one correspondence with the real types in the package,
251 -- and that have the properties of incomplete types.
253 -- The implementation creates two element lists: one to chain the shadow
254 -- entities, and one to chain the corresponding type entities in the tree
255 -- of the package. Links between corresponding entities in both chains
256 -- allow the compiler to select the proper view of a given type, depending
257 -- on the context. Note that in contrast with the handling of private
258 -- types, the limited view and the nonlimited view of a type are treated
259 -- as separate entities, and no entity exchange needs to take place, which
260 -- makes the implementation much simpler than could be feared.
262 ------------------------------
263 -- Analyze_Compilation_Unit --
264 ------------------------------
266 procedure Analyze_Compilation_Unit (N : Node_Id) is
267 procedure Check_Redundant_Withs
268 (Context_Items : List_Id;
269 Spec_Context_Items : List_Id := No_List);
270 -- Determine whether the context list of a compilation unit contains
271 -- redundant with clauses. When checking body clauses against spec
272 -- clauses, set Context_Items to the context list of the body and
273 -- Spec_Context_Items to that of the spec. Parent packages are not
274 -- examined for documentation purposes.
276 ---------------------------
277 -- Check_Redundant_Withs --
278 ---------------------------
280 procedure Check_Redundant_Withs
281 (Context_Items : List_Id;
282 Spec_Context_Items : List_Id := No_List)
284 Clause : Node_Id;
286 procedure Process_Body_Clauses
287 (Context_List : List_Id;
288 Clause : Node_Id;
289 Used : out Boolean;
290 Used_Type_Or_Elab : out Boolean);
291 -- Examine the context clauses of a package body, trying to match the
292 -- name entity of Clause with any list element. If the match occurs
293 -- on a use package clause set Used to True, for a use type clause or
294 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
296 procedure Process_Spec_Clauses
297 (Context_List : List_Id;
298 Clause : Node_Id;
299 Used : out Boolean;
300 Withed : out Boolean;
301 Exit_On_Self : Boolean := False);
302 -- Examine the context clauses of a package spec, trying to match
303 -- the name entity of Clause with any list element. If the match
304 -- occurs on a use package clause, set Used to True, for a with
305 -- package clause other than Clause, set Withed to True. Limited
306 -- with clauses, implicitly generated with clauses and withs
307 -- having pragmas Elaborate or Elaborate_All applied to them are
308 -- skipped. Exit_On_Self is used to control the search loop and
309 -- force an exit whenever Clause sees itself in the search.
311 --------------------------
312 -- Process_Body_Clauses --
313 --------------------------
315 procedure Process_Body_Clauses
316 (Context_List : List_Id;
317 Clause : Node_Id;
318 Used : out Boolean;
319 Used_Type_Or_Elab : out Boolean)
321 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
322 Cont_Item : Node_Id;
323 Prag_Unit : Node_Id;
324 Subt_Mark : Node_Id;
325 Use_Item : Node_Id;
327 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
328 -- In an expanded name in a use clause, if the prefix is a renamed
329 -- package, the entity is set to the original package as a result,
330 -- when checking whether the package appears in a previous with
331 -- clause, the renaming has to be taken into account, to prevent
332 -- spurious/incorrect warnings. A common case is use of Text_IO.
334 ---------------
335 -- Same_Unit --
336 ---------------
338 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
339 begin
340 return Entity (N) = P
341 or else (Present (Renamed_Object (P))
342 and then Entity (N) = Renamed_Object (P));
343 end Same_Unit;
345 -- Start of processing for Process_Body_Clauses
347 begin
348 Used := False;
349 Used_Type_Or_Elab := False;
351 Cont_Item := First (Context_List);
352 while Present (Cont_Item) loop
354 -- Package use clause
356 if Nkind (Cont_Item) = N_Use_Package_Clause
357 and then not Used
358 then
359 -- Search through use clauses
361 Use_Item := Name (Cont_Item);
363 -- Case of a direct use of the one we are looking for
365 if Entity (Use_Item) = Nam_Ent then
366 Used := True;
368 -- Handle nested case, as in "with P; use P.Q.R"
370 else
371 declare
372 UE : Node_Id;
374 begin
375 -- Loop through prefixes looking for match
377 UE := Use_Item;
378 while Nkind (UE) = N_Expanded_Name loop
379 if Same_Unit (Prefix (UE), Nam_Ent) then
380 Used := True;
381 exit;
382 end if;
384 UE := Prefix (UE);
385 end loop;
386 end;
387 end if;
389 -- USE TYPE clause
391 elsif Nkind (Cont_Item) = N_Use_Type_Clause
392 and then not Used_Type_Or_Elab
393 then
394 Subt_Mark := Subtype_Mark (Cont_Item);
395 if not Used_Type_Or_Elab
396 and then Same_Unit (Prefix (Subt_Mark), Nam_Ent)
397 then
398 Used_Type_Or_Elab := True;
399 end if;
401 -- Pragma Elaborate or Elaborate_All
403 elsif Nkind (Cont_Item) = N_Pragma
404 and then
405 Nam_In (Pragma_Name_Unmapped (Cont_Item),
406 Name_Elaborate, Name_Elaborate_All)
407 and then not Used_Type_Or_Elab
408 then
409 Prag_Unit :=
410 First (Pragma_Argument_Associations (Cont_Item));
411 while Present (Prag_Unit) and then not Used_Type_Or_Elab loop
412 if Entity (Expression (Prag_Unit)) = Nam_Ent then
413 Used_Type_Or_Elab := True;
414 end if;
416 Next (Prag_Unit);
417 end loop;
418 end if;
420 Next (Cont_Item);
421 end loop;
422 end Process_Body_Clauses;
424 --------------------------
425 -- Process_Spec_Clauses --
426 --------------------------
428 procedure Process_Spec_Clauses
429 (Context_List : List_Id;
430 Clause : Node_Id;
431 Used : out Boolean;
432 Withed : out Boolean;
433 Exit_On_Self : Boolean := False)
435 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
436 Cont_Item : Node_Id;
438 begin
439 Used := False;
440 Withed := False;
442 Cont_Item := First (Context_List);
443 while Present (Cont_Item) loop
445 -- Stop the search since the context items after Cont_Item have
446 -- already been examined in a previous iteration of the reverse
447 -- loop in Check_Redundant_Withs.
449 if Exit_On_Self
450 and Cont_Item = Clause
451 then
452 exit;
453 end if;
455 -- Package use clause
457 if Nkind (Cont_Item) = N_Use_Package_Clause
458 and then not Used
459 then
460 if Entity (Name (Cont_Item)) = Nam_Ent then
461 Used := True;
462 end if;
464 -- Package with clause. Avoid processing self, implicitly
465 -- generated with clauses or limited with clauses. Note that
466 -- we examine with clauses having pragmas Elaborate or
467 -- Elaborate_All applied to them due to cases such as:
469 -- with Pack;
470 -- with Pack;
471 -- pragma Elaborate (Pack);
473 -- In this case, the second with clause is redundant since
474 -- the pragma applies only to the first "with Pack;".
476 -- Note that we only consider with_clauses that comes from
477 -- source. In the case of renamings used as prefixes of names
478 -- in with_clauses, we generate a with_clause for the prefix,
479 -- which we do not treat as implicit because it is needed for
480 -- visibility analysis, but is also not redundant.
482 elsif Nkind (Cont_Item) = N_With_Clause
483 and then Comes_From_Source (Cont_Item)
484 and then not Implicit_With (Cont_Item)
485 and then not Limited_Present (Cont_Item)
486 and then Cont_Item /= Clause
487 and then Entity (Name (Cont_Item)) = Nam_Ent
488 then
489 Withed := True;
490 end if;
492 Next (Cont_Item);
493 end loop;
494 end Process_Spec_Clauses;
496 -- Start of processing for Check_Redundant_Withs
498 begin
499 Clause := Last (Context_Items);
500 while Present (Clause) loop
502 -- Avoid checking implicitly generated with clauses, limited with
503 -- clauses or withs that have pragma Elaborate or Elaborate_All.
505 if Nkind (Clause) = N_With_Clause
506 and then not Implicit_With (Clause)
507 and then not Limited_Present (Clause)
508 and then not Elaborate_Present (Clause)
510 -- With_clauses introduced for renamings of parent clauses
511 -- are not marked implicit because they need to be properly
512 -- installed, but they do not come from source and do not
513 -- require warnings.
515 and then Comes_From_Source (Clause)
516 then
517 -- Package body-to-spec check
519 if Present (Spec_Context_Items) then
520 declare
521 Used_In_Body : Boolean;
522 Used_In_Spec : Boolean;
523 Used_Type_Or_Elab : Boolean;
524 Withed_In_Spec : Boolean;
526 begin
527 Process_Spec_Clauses
528 (Context_List => Spec_Context_Items,
529 Clause => Clause,
530 Used => Used_In_Spec,
531 Withed => Withed_In_Spec);
533 Process_Body_Clauses
534 (Context_List => Context_Items,
535 Clause => Clause,
536 Used => Used_In_Body,
537 Used_Type_Or_Elab => Used_Type_Or_Elab);
539 -- "Type Elab" refers to the presence of either a use
540 -- type clause, pragmas Elaborate or Elaborate_All.
542 -- +---------------+---------------------------+------+
543 -- | Spec | Body | Warn |
544 -- +--------+------+--------+------+-----------+------+
545 -- | Withed | Used | Withed | Used | Type Elab | |
546 -- | X | | X | | | X |
547 -- | X | | X | X | | |
548 -- | X | | X | | X | |
549 -- | X | | X | X | X | |
550 -- | X | X | X | | | X |
551 -- | X | X | X | | X | |
552 -- | X | X | X | X | | X |
553 -- | X | X | X | X | X | |
554 -- +--------+------+--------+------+-----------+------+
556 if (Withed_In_Spec
557 and then not Used_Type_Or_Elab)
558 and then
559 ((not Used_In_Spec and then not Used_In_Body)
560 or else Used_In_Spec)
561 then
562 Error_Msg_N -- CODEFIX
563 ("redundant with clause in body?r?", Clause);
564 end if;
566 Used_In_Body := False;
567 Used_In_Spec := False;
568 Used_Type_Or_Elab := False;
569 Withed_In_Spec := False;
570 end;
572 -- Standalone package spec or body check
574 else
575 declare
576 Dummy : Boolean := False;
577 Withed : Boolean := False;
579 begin
580 -- The mechanism for examining the context clauses of a
581 -- package spec can be applied to package body clauses.
583 Process_Spec_Clauses
584 (Context_List => Context_Items,
585 Clause => Clause,
586 Used => Dummy,
587 Withed => Withed,
588 Exit_On_Self => True);
590 if Withed then
591 Error_Msg_N -- CODEFIX
592 ("redundant with clause?r?", Clause);
593 end if;
594 end;
595 end if;
596 end if;
598 Prev (Clause);
599 end loop;
600 end Check_Redundant_Withs;
602 -- Local variables
604 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
605 Unit_Node : constant Node_Id := Unit (N);
606 Lib_Unit : Node_Id := Library_Unit (N);
607 Par_Spec_Name : Unit_Name_Type;
608 Spec_Id : Entity_Id;
609 Unum : Unit_Number_Type;
611 -- Start of processing for Analyze_Compilation_Unit
613 begin
614 Process_Compilation_Unit_Pragmas (N);
616 -- If the unit is a subunit whose parent has not been analyzed (which
617 -- indicates that the main unit is a subunit, either the current one or
618 -- one of its descendants) then the subunit is compiled as part of the
619 -- analysis of the parent, which we proceed to do. Basically this gets
620 -- handled from the top down and we don't want to do anything at this
621 -- level (i.e. this subunit will be handled on the way down from the
622 -- parent), so at this level we immediately return. If the subunit ends
623 -- up not analyzed, it means that the parent did not contain a stub for
624 -- it, or that there errors were detected in some ancestor.
626 if Nkind (Unit_Node) = N_Subunit and then not Analyzed (Lib_Unit) then
627 Semantics (Lib_Unit);
629 if not Analyzed (Proper_Body (Unit_Node)) then
630 if Serious_Errors_Detected > 0 then
631 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
632 else
633 Error_Msg_N ("missing stub for subunit", N);
634 end if;
635 end if;
637 return;
638 end if;
640 -- Analyze context (this will call Sem recursively for with'ed units) To
641 -- detect circularities among with-clauses that are not caught during
642 -- loading, we set the Context_Pending flag on the current unit. If the
643 -- flag is already set there is a potential circularity. We exclude
644 -- predefined units from this check because they are known to be safe.
645 -- We also exclude package bodies that are present because circularities
646 -- between bodies are harmless (and necessary).
648 if Context_Pending (N) then
649 declare
650 Circularity : Boolean := True;
652 begin
653 if In_Predefined_Unit (N) then
654 Circularity := False;
656 else
657 for U in Main_Unit + 1 .. Last_Unit loop
658 if Nkind (Unit (Cunit (U))) = N_Package_Body
659 and then not Analyzed (Cunit (U))
660 then
661 Circularity := False;
662 exit;
663 end if;
664 end loop;
665 end if;
667 if Circularity then
668 Error_Msg_N ("circular dependency caused by with_clauses", N);
669 Error_Msg_N
670 ("\possibly missing limited_with clause"
671 & " in one of the following", N);
673 for U in Main_Unit .. Last_Unit loop
674 if Context_Pending (Cunit (U)) then
675 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
676 Error_Msg_N ("\unit$", N);
677 end if;
678 end loop;
680 raise Unrecoverable_Error;
681 end if;
682 end;
683 else
684 Set_Context_Pending (N);
685 end if;
687 Analyze_Context (N);
689 Set_Context_Pending (N, False);
691 -- If the unit is a package body, the spec is already loaded and must be
692 -- analyzed first, before we analyze the body.
694 if Nkind (Unit_Node) = N_Package_Body then
696 -- If no Lib_Unit, then there was a serious previous error, so just
697 -- ignore the entire analysis effort.
699 if No (Lib_Unit) then
700 Check_Error_Detected;
701 return;
703 else
704 -- Analyze the package spec
706 Semantics (Lib_Unit);
708 -- Check for unused with's
710 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
712 -- Verify that the library unit is a package declaration
714 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
715 N_Generic_Package_Declaration)
716 then
717 Error_Msg_N
718 ("no legal package declaration for package body", N);
719 return;
721 -- Otherwise, the entity in the declaration is visible. Update the
722 -- version to reflect dependence of this body on the spec.
724 else
725 Spec_Id := Defining_Entity (Unit (Lib_Unit));
726 Set_Is_Immediately_Visible (Spec_Id, True);
727 Version_Update (N, Lib_Unit);
729 if Nkind (Defining_Unit_Name (Unit_Node)) =
730 N_Defining_Program_Unit_Name
731 then
732 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
733 end if;
734 end if;
735 end if;
737 -- If the unit is a subprogram body, then we similarly need to analyze
738 -- its spec. However, things are a little simpler in this case, because
739 -- here, this analysis is done mostly for error checking and consistency
740 -- purposes (but not only, e.g. there could be a contract on the spec),
741 -- so there's nothing else to be done.
743 elsif Nkind (Unit_Node) = N_Subprogram_Body then
744 if Acts_As_Spec (N) then
746 -- If the subprogram body is a child unit, we must create a
747 -- declaration for it, in order to properly load the parent(s).
748 -- After this, the original unit does not acts as a spec, because
749 -- there is an explicit one. If this unit appears in a context
750 -- clause, then an implicit with on the parent will be added when
751 -- installing the context. If this is the main unit, there is no
752 -- Unit_Table entry for the declaration (it has the unit number
753 -- of the main unit) and code generation is unaffected.
755 Unum := Get_Cunit_Unit_Number (N);
756 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
758 if Par_Spec_Name /= No_Unit_Name then
759 Unum :=
760 Load_Unit
761 (Load_Name => Par_Spec_Name,
762 Required => True,
763 Subunit => False,
764 Error_Node => N);
766 if Unum /= No_Unit then
768 -- Build subprogram declaration and attach parent unit to it
769 -- This subprogram declaration does not come from source,
770 -- Nevertheless the backend must generate debugging info for
771 -- it, and this must be indicated explicitly. We also mark
772 -- the body entity as a child unit now, to prevent a
773 -- cascaded error if the spec entity cannot be entered
774 -- in its scope. Finally we create a Units table entry for
775 -- the subprogram declaration, to maintain a one-to-one
776 -- correspondence with compilation unit nodes. This is
777 -- critical for the tree traversals performed by CodePeer.
779 declare
780 Loc : constant Source_Ptr := Sloc (N);
781 SCS : constant Boolean :=
782 Get_Comes_From_Source_Default;
784 begin
785 Set_Comes_From_Source_Default (False);
787 -- Note: We copy the Context_Items from the explicit body
788 -- to the implicit spec, setting the former to Empty_List
789 -- to preserve the treeish nature of the tree, during
790 -- analysis of the spec. Then we put it back the way it
791 -- was -- copy the Context_Items from the spec to the
792 -- body, and set the spec Context_Items to Empty_List.
793 -- It is necessary to preserve the treeish nature,
794 -- because otherwise we will call End_Use_* twice on the
795 -- same thing.
797 Lib_Unit :=
798 Make_Compilation_Unit (Loc,
799 Context_Items => Context_Items (N),
800 Unit =>
801 Make_Subprogram_Declaration (Sloc (N),
802 Specification =>
803 Copy_Separate_Tree
804 (Specification (Unit_Node))),
805 Aux_Decls_Node =>
806 Make_Compilation_Unit_Aux (Loc));
808 Set_Context_Items (N, Empty_List);
809 Set_Library_Unit (N, Lib_Unit);
810 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
811 Make_Child_Decl_Unit (N);
812 Semantics (Lib_Unit);
814 -- Now that a separate declaration exists, the body
815 -- of the child unit does not act as spec any longer.
817 Set_Acts_As_Spec (N, False);
818 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
819 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
820 Set_Comes_From_Source_Default (SCS);
822 -- Restore Context_Items to the body
824 Set_Context_Items (N, Context_Items (Lib_Unit));
825 Set_Context_Items (Lib_Unit, Empty_List);
826 end;
827 end if;
828 end if;
830 -- Here for subprogram with separate declaration
832 else
833 Semantics (Lib_Unit);
834 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
835 Version_Update (N, Lib_Unit);
836 end if;
838 -- If this is a child unit, generate references to the parents
840 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
841 N_Defining_Program_Unit_Name
842 then
843 Generate_Parent_References
844 (Specification (Unit_Node),
845 Scope (Defining_Entity (Unit (Lib_Unit))));
846 end if;
847 end if;
849 -- If it is a child unit, the parent must be elaborated first and we
850 -- update version, since we are dependent on our parent.
852 if Is_Child_Spec (Unit_Node) then
854 -- The analysis of the parent is done with style checks off
856 declare
857 Save_Style_Check : constant Boolean := Style_Check;
859 begin
860 if not GNAT_Mode then
861 Style_Check := False;
862 end if;
864 Semantics (Parent_Spec (Unit_Node));
865 Version_Update (N, Parent_Spec (Unit_Node));
867 -- Restore style check settings
869 Style_Check := Save_Style_Check;
870 end;
871 end if;
873 -- With the analysis done, install the context. Note that we can't
874 -- install the context from the with clauses as we analyze them, because
875 -- each with clause must be analyzed in a clean visibility context, so
876 -- we have to wait and install them all at once.
878 Install_Context (N);
880 if Is_Child_Spec (Unit_Node) then
882 -- Set the entities of all parents in the program_unit_name
884 Generate_Parent_References
885 (Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
886 end if;
888 -- All components of the context: with-clauses, library unit, ancestors
889 -- if any, (and their context) are analyzed and installed.
891 -- Call special debug routine sm if this is the main unit
893 if Current_Sem_Unit = Main_Unit then
895 end if;
897 -- Now analyze the unit (package, subprogram spec, body) itself
899 Analyze (Unit_Node);
901 if Warn_On_Redundant_Constructs then
902 Check_Redundant_Withs (Context_Items (N));
904 if Nkind (Unit_Node) = N_Package_Body then
905 Check_Redundant_Withs
906 (Context_Items => Context_Items (N),
907 Spec_Context_Items => Context_Items (Lib_Unit));
908 end if;
909 end if;
911 -- The above call might have made Unit_Node an N_Subprogram_Body from
912 -- something else, so propagate any Acts_As_Spec flag.
914 if Nkind (Unit_Node) = N_Subprogram_Body
915 and then Acts_As_Spec (Unit_Node)
916 then
917 Set_Acts_As_Spec (N);
918 end if;
920 -- Register predefined units in Rtsfind
922 if In_Predefined_Unit (N) then
923 Set_RTU_Loaded (Unit_Node);
924 end if;
926 -- Treat compilation unit pragmas that appear after the library unit
928 if Present (Pragmas_After (Aux_Decls_Node (N))) then
929 declare
930 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
931 begin
932 while Present (Prag_Node) loop
933 Analyze (Prag_Node);
934 Next (Prag_Node);
935 end loop;
936 end;
937 end if;
939 -- Analyze the contract of a [generic] subprogram that acts as a
940 -- compilation unit after all compilation pragmas have been analyzed.
942 if Nkind_In (Unit_Node, N_Generic_Subprogram_Declaration,
943 N_Subprogram_Declaration)
944 then
945 Analyze_Entry_Or_Subprogram_Contract (Defining_Entity (Unit_Node));
946 end if;
948 -- Generate distribution stubs if requested and no error
950 if N = Main_Cunit
951 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
952 or else
953 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
954 and then Fatal_Error (Main_Unit) /= Error_Detected
955 then
956 if Is_RCI_Pkg_Spec_Or_Body (N) then
958 -- Regular RCI package
960 Add_Stub_Constructs (N);
962 elsif (Nkind (Unit_Node) = N_Package_Declaration
963 and then Is_Shared_Passive (Defining_Entity
964 (Specification (Unit_Node))))
965 or else (Nkind (Unit_Node) = N_Package_Body
966 and then
967 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
968 then
969 -- Shared passive package
971 Add_Stub_Constructs (N);
973 elsif Nkind (Unit_Node) = N_Package_Instantiation
974 and then
975 Is_Remote_Call_Interface
976 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
977 then
978 -- Instantiation of a RCI generic package
980 Add_Stub_Constructs (N);
981 end if;
982 end if;
984 -- Remove unit from visibility, so that environment is clean for the
985 -- next compilation, which is either the main unit or some other unit
986 -- in the context.
988 if Nkind_In (Unit_Node, N_Package_Declaration,
989 N_Package_Renaming_Declaration,
990 N_Subprogram_Declaration)
991 or else Nkind (Unit_Node) in N_Generic_Declaration
992 or else (Nkind (Unit_Node) = N_Subprogram_Body
993 and then Acts_As_Spec (Unit_Node))
994 then
995 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
997 -- If the unit is an instantiation whose body will be elaborated for
998 -- inlining purposes, use the proper entity of the instance. The entity
999 -- may be missing if the instantiation was illegal.
1001 elsif Nkind (Unit_Node) = N_Package_Instantiation
1002 and then not Error_Posted (Unit_Node)
1003 and then Present (Instance_Spec (Unit_Node))
1004 then
1005 Remove_Unit_From_Visibility
1006 (Defining_Entity (Instance_Spec (Unit_Node)));
1008 elsif Nkind (Unit_Node) = N_Package_Body
1009 or else (Nkind (Unit_Node) = N_Subprogram_Body
1010 and then not Acts_As_Spec (Unit_Node))
1011 then
1012 -- Bodies that are not the main unit are compiled if they are generic
1013 -- or contain generic or inlined units. Their analysis brings in the
1014 -- context of the corresponding spec (unit declaration) which must be
1015 -- removed as well, to return the compilation environment to its
1016 -- proper state.
1018 Remove_Context (Lib_Unit);
1019 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1020 end if;
1022 -- Last step is to deinstall the context we just installed as well as
1023 -- the unit just compiled.
1025 Remove_Context (N);
1027 -- When generating code for a non-generic main unit, check that withed
1028 -- generic units have a body if they need it, even if the units have not
1029 -- been instantiated. Force the load of the bodies to produce the proper
1030 -- error if the body is absent. The same applies to GNATprove mode, with
1031 -- the added benefit of capturing global references within the generic.
1032 -- This in turn allows for proper inlining of subprogram bodies without
1033 -- a previous declaration.
1035 if Get_Cunit_Unit_Number (N) = Main_Unit
1036 and then ((Operating_Mode = Generate_Code and then Expander_Active)
1037 or else
1038 (Operating_Mode = Check_Semantics and then GNATprove_Mode))
1039 then
1040 -- Check whether the source for the body of the unit must be included
1041 -- in a standalone library.
1043 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1045 -- Indicate that the main unit is now analyzed, to catch possible
1046 -- circularities between it and generic bodies. Remove main unit from
1047 -- visibility. This might seem superfluous, but the main unit must
1048 -- not be visible in the generic body expansions that follow.
1050 Set_Analyzed (N, True);
1051 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1053 declare
1054 Item : Node_Id;
1055 Nam : Entity_Id;
1056 Un : Unit_Number_Type;
1058 Save_Style_Check : constant Boolean := Style_Check;
1060 begin
1061 Item := First (Context_Items (N));
1062 while Present (Item) loop
1064 -- Check for explicit with clause
1066 if Nkind (Item) = N_With_Clause
1067 and then not Implicit_With (Item)
1069 -- Ada 2005 (AI-50217): Ignore limited-withed units
1071 and then not Limited_Present (Item)
1072 then
1073 Nam := Entity (Name (Item));
1075 -- Compile the generic subprogram, unless it is intrinsic or
1076 -- imported so no body is required, or generic package body
1077 -- if the package spec requires a body.
1079 if (Is_Generic_Subprogram (Nam)
1080 and then not Is_Intrinsic_Subprogram (Nam)
1081 and then not Is_Imported (Nam))
1082 or else (Ekind (Nam) = E_Generic_Package
1083 and then Unit_Requires_Body (Nam))
1084 then
1085 Style_Check := False;
1087 if Present (Renamed_Object (Nam)) then
1088 Un :=
1089 Load_Unit
1090 (Load_Name =>
1091 Get_Body_Name
1092 (Get_Unit_Name
1093 (Unit_Declaration_Node
1094 (Renamed_Object (Nam)))),
1095 Required => False,
1096 Subunit => False,
1097 Error_Node => N,
1098 Renamings => True);
1099 else
1100 Un :=
1101 Load_Unit
1102 (Load_Name =>
1103 Get_Body_Name (Get_Unit_Name (Item)),
1104 Required => False,
1105 Subunit => False,
1106 Error_Node => N,
1107 Renamings => True);
1108 end if;
1110 if Un = No_Unit then
1111 Error_Msg_NE
1112 ("body of generic unit& not found", Item, Nam);
1113 exit;
1115 elsif not Analyzed (Cunit (Un))
1116 and then Un /= Main_Unit
1117 and then Fatal_Error (Un) /= Error_Detected
1118 then
1119 Style_Check := False;
1120 Semantics (Cunit (Un));
1121 end if;
1122 end if;
1123 end if;
1125 Next (Item);
1126 end loop;
1128 -- Restore style checks settings
1130 Style_Check := Save_Style_Check;
1131 end;
1133 -- In GNATprove mode, force the loading of an Interrupt_Priority when
1134 -- processing compilation units with potentially "main" subprograms.
1135 -- This is required for the ceiling priority protocol checks, which
1136 -- are triggered by these subprograms.
1138 if GNATprove_Mode
1139 and then Nkind_In (Unit_Node, N_Function_Instantiation,
1140 N_Procedure_Instantiation,
1141 N_Subprogram_Body)
1142 then
1143 declare
1144 Spec : Node_Id;
1146 begin
1147 case Nkind (Unit_Node) is
1148 when N_Subprogram_Body =>
1149 Spec := Specification (Unit_Node);
1151 when N_Subprogram_Instantiation =>
1152 Spec :=
1153 Subprogram_Specification (Entity (Name (Unit_Node)));
1155 when others =>
1156 raise Program_Error;
1157 end case;
1159 pragma Assert (Nkind (Spec) in N_Subprogram_Specification);
1161 -- Main subprogram must have no parameters, and if it is a
1162 -- function, it must return an integer.
1164 if No (Parameter_Specifications (Spec))
1165 and then (Nkind (Spec) = N_Procedure_Specification
1166 or else
1167 Is_Integer_Type (Etype (Result_Definition (Spec))))
1168 then
1169 SPARK_Implicit_Load (RE_Interrupt_Priority);
1170 end if;
1171 end;
1172 end if;
1173 end if;
1175 -- Deal with creating elaboration counter if needed. We create an
1176 -- elaboration counter only for units that come from source since
1177 -- units manufactured by the compiler never need elab checks.
1179 if Comes_From_Source (N)
1180 and then Nkind_In (Unit_Node, N_Package_Declaration,
1181 N_Generic_Package_Declaration,
1182 N_Subprogram_Declaration,
1183 N_Generic_Subprogram_Declaration)
1184 then
1185 declare
1186 Loc : constant Source_Ptr := Sloc (N);
1187 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1189 begin
1190 Spec_Id := Defining_Entity (Unit_Node);
1191 Generate_Definition (Spec_Id);
1193 -- See if an elaboration entity is required for possible access
1194 -- before elaboration checking. Note that we must allow for this
1195 -- even if -gnatE is not set, since a client may be compiled in
1196 -- -gnatE mode and reference the entity.
1198 -- These entities are also used by the binder to prevent multiple
1199 -- attempts to execute the elaboration code for the library case
1200 -- where the elaboration routine might otherwise be called more
1201 -- than once.
1203 -- They are also needed to ensure explicit visibility from the
1204 -- binder generated code of all the units involved in a partition
1205 -- when control-flow preservation is requested.
1207 -- Case of units which do not require an elaboration entity
1209 if not Opt.Suppress_Control_Flow_Optimizations
1210 and then
1211 ( -- Pure units do not need checks
1213 Is_Pure (Spec_Id)
1215 -- Preelaborated units do not need checks
1217 or else Is_Preelaborated (Spec_Id)
1219 -- No checks needed if pragma Elaborate_Body present
1221 or else Has_Pragma_Elaborate_Body (Spec_Id)
1223 -- No checks needed if unit does not require a body
1225 or else not Unit_Requires_Body (Spec_Id)
1227 -- No checks needed for predefined files
1229 or else Is_Predefined_Unit (Unum)
1231 -- No checks required if no separate spec
1233 or else Acts_As_Spec (N)
1235 then
1236 -- This is a case where we only need the entity for
1237 -- checking to prevent multiple elaboration checks.
1239 Set_Elaboration_Entity_Required (Spec_Id, False);
1241 -- Case of elaboration entity is required for access before
1242 -- elaboration checking (so certainly we must build it).
1244 else
1245 Set_Elaboration_Entity_Required (Spec_Id, True);
1246 end if;
1248 Build_Elaboration_Entity (N, Spec_Id);
1249 end;
1250 end if;
1252 -- Freeze the compilation unit entity. This for sure is needed because
1253 -- of some warnings that can be output (see Freeze_Subprogram), but may
1254 -- in general be required. If freezing actions result, place them in the
1255 -- compilation unit actions list, and analyze them.
1257 declare
1258 L : constant List_Id :=
1259 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1260 begin
1261 while Is_Non_Empty_List (L) loop
1262 Insert_Library_Level_Action (Remove_Head (L));
1263 end loop;
1264 end;
1266 Set_Analyzed (N);
1268 -- Call Check_Package_Body so that a body containing subprograms with
1269 -- Inline_Always can be made available for front end inlining.
1271 if Nkind (Unit_Node) = N_Package_Declaration
1272 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1274 -- We don't need to do this if the Expander is not active, since there
1275 -- is no code to inline.
1277 and then Expander_Active
1278 then
1279 declare
1280 Save_Style_Check : constant Boolean := Style_Check;
1281 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1282 Options : Style_Check_Options;
1284 begin
1285 Save_Style_Check_Options (Options);
1286 Reset_Style_Check_Options;
1287 Opt.Warning_Mode := Suppress;
1289 Check_Package_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1291 Reset_Style_Check_Options;
1292 Set_Style_Check_Options (Options);
1293 Style_Check := Save_Style_Check;
1294 Warning_Mode := Save_Warning;
1295 end;
1296 end if;
1298 -- If we are generating obsolescent warnings, then here is where we
1299 -- generate them for the with'ed items. The reason for this special
1300 -- processing is that the normal mechanism of generating the warnings
1301 -- for referenced entities does not work for context clause references.
1302 -- That's because when we first analyze the context, it is too early to
1303 -- know if the with'ing unit is itself obsolescent (which suppresses
1304 -- the warnings).
1306 if not GNAT_Mode
1307 and then Warn_On_Obsolescent_Feature
1308 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1309 then
1310 -- Push current compilation unit as scope, so that the test for
1311 -- being within an obsolescent unit will work correctly. The check
1312 -- is not performed within an instantiation, because the warning
1313 -- will have been emitted in the corresponding generic unit.
1315 Push_Scope (Defining_Entity (Unit_Node));
1317 -- Loop through context items to deal with with clauses
1319 declare
1320 Item : Node_Id;
1321 Nam : Node_Id;
1322 Ent : Entity_Id;
1324 begin
1325 Item := First (Context_Items (N));
1326 while Present (Item) loop
1327 if Nkind (Item) = N_With_Clause
1329 -- Suppress this check in limited-withed units. Further work
1330 -- needed here if we decide to incorporate this check on
1331 -- limited-withed units.
1333 and then not Limited_Present (Item)
1334 then
1335 Nam := Name (Item);
1336 Ent := Entity (Nam);
1338 if Is_Obsolescent (Ent) then
1339 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1340 end if;
1341 end if;
1343 Next (Item);
1344 end loop;
1345 end;
1347 -- Remove temporary install of current unit as scope
1349 Pop_Scope;
1350 end if;
1352 -- If No_Elaboration_Code_All was encountered, this is where we do the
1353 -- transitive test of with'ed units to make sure they have the aspect.
1354 -- This is delayed till the end of analyzing the compilation unit to
1355 -- ensure that the pragma/aspect, if present, has been analyzed.
1357 Check_No_Elab_Code_All (N);
1358 end Analyze_Compilation_Unit;
1360 ---------------------
1361 -- Analyze_Context --
1362 ---------------------
1364 procedure Analyze_Context (N : Node_Id) is
1365 Ukind : constant Node_Kind := Nkind (Unit (N));
1366 Item : Node_Id;
1368 begin
1369 -- First process all configuration pragmas at the start of the context
1370 -- items. Strictly these are not part of the context clause, but that
1371 -- is where the parser puts them. In any case for sure we must analyze
1372 -- these before analyzing the actual context items, since they can have
1373 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1374 -- be with'ed as a result of changing categorizations in Ada 2005).
1376 Item := First (Context_Items (N));
1377 while Present (Item)
1378 and then Nkind (Item) = N_Pragma
1379 and then Pragma_Name (Item) in Configuration_Pragma_Names
1380 loop
1381 Analyze (Item);
1382 Next (Item);
1383 end loop;
1385 -- This is the point at which we capture the configuration settings
1386 -- for the unit. At the moment only the Optimize_Alignment setting
1387 -- needs to be captured. Probably more later ???
1389 if Optimize_Alignment_Local then
1390 Set_OA_Setting (Current_Sem_Unit, 'L');
1391 else
1392 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1393 end if;
1395 -- Loop through actual context items. This is done in two passes:
1397 -- a) The first pass analyzes nonlimited with clauses and also any
1398 -- configuration pragmas (we need to get the latter analyzed right
1399 -- away, since they can affect processing of subsequent items).
1401 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1403 while Present (Item) loop
1405 -- For with clause, analyze the with clause, and then update the
1406 -- version, since we are dependent on a unit that we with.
1408 if Nkind (Item) = N_With_Clause
1409 and then not Limited_Present (Item)
1410 then
1411 -- Skip analyzing with clause if no unit, nothing to do (this
1412 -- happens for a with that references a non-existent unit).
1414 if Present (Library_Unit (Item)) then
1416 -- Skip analyzing with clause if this is a with_clause for
1417 -- the main unit, which happens if a subunit has a useless
1418 -- with_clause on its parent.
1420 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1421 Analyze (Item);
1423 -- Here for the case of a useless with for the main unit
1425 else
1426 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1427 end if;
1428 end if;
1430 -- Do version update (skipped for implicit with)
1432 if not Implicit_With (Item) then
1433 Version_Update (N, Library_Unit (Item));
1434 end if;
1436 -- Skip pragmas. Configuration pragmas at the start were handled in
1437 -- the loop above, and remaining pragmas are not processed until we
1438 -- actually install the context (see Install_Context). We delay the
1439 -- analysis of these pragmas to make sure that we have installed all
1440 -- the implicit with's on parent units.
1442 -- Skip use clauses at this stage, since we don't want to do any
1443 -- installing of potentially use-visible entities until we
1444 -- actually install the complete context (in Install_Context).
1445 -- Otherwise things can get installed in the wrong context.
1447 else
1448 null;
1449 end if;
1451 Next (Item);
1452 end loop;
1454 -- Second pass: examine all limited_with clauses. All other context
1455 -- items are ignored in this pass.
1457 Item := First (Context_Items (N));
1458 while Present (Item) loop
1459 if Nkind (Item) = N_With_Clause
1460 and then Limited_Present (Item)
1461 then
1462 -- No need to check errors on implicitly generated limited-with
1463 -- clauses.
1465 if not Implicit_With (Item) then
1467 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1468 -- properly rejected, including renaming declarations.
1470 if not Nkind_In (Ukind, N_Package_Declaration,
1471 N_Subprogram_Declaration)
1472 and then Ukind not in N_Generic_Declaration
1473 and then Ukind not in N_Generic_Instantiation
1474 then
1475 Error_Msg_N ("limited with_clause not allowed here", Item);
1477 -- Check wrong use of a limited with clause applied to the
1478 -- compilation unit containing the limited-with clause.
1480 -- limited with P.Q;
1481 -- package P.Q is ...
1483 elsif Unit (Library_Unit (Item)) = Unit (N) then
1484 Error_Msg_N ("wrong use of limited-with clause", Item);
1486 -- Check wrong use of limited-with clause applied to some
1487 -- immediate ancestor.
1489 elsif Is_Child_Spec (Unit (N)) then
1490 declare
1491 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1492 P : Node_Id;
1494 begin
1495 P := Parent_Spec (Unit (N));
1496 loop
1497 if Unit (P) = Lib_U then
1498 Error_Msg_N
1499 ("limited with_clause cannot name ancestor",
1500 Item);
1501 exit;
1502 end if;
1504 exit when not Is_Child_Spec (Unit (P));
1505 P := Parent_Spec (Unit (P));
1506 end loop;
1507 end;
1508 end if;
1510 -- Check if the limited-withed unit is already visible through
1511 -- some context clause of the current compilation unit or some
1512 -- ancestor of the current compilation unit.
1514 declare
1515 Lim_Unit_Name : constant Node_Id := Name (Item);
1516 Comp_Unit : Node_Id;
1517 It : Node_Id;
1518 Unit_Name : Node_Id;
1520 begin
1521 Comp_Unit := N;
1522 loop
1523 It := First (Context_Items (Comp_Unit));
1524 while Present (It) loop
1525 if Item /= It
1526 and then Nkind (It) = N_With_Clause
1527 and then not Limited_Present (It)
1528 and then
1529 Nkind_In (Unit (Library_Unit (It)),
1530 N_Package_Declaration,
1531 N_Package_Renaming_Declaration)
1532 then
1533 if Nkind (Unit (Library_Unit (It))) =
1534 N_Package_Declaration
1535 then
1536 Unit_Name := Name (It);
1537 else
1538 Unit_Name := Name (Unit (Library_Unit (It)));
1539 end if;
1541 -- Check if the named package (or some ancestor)
1542 -- leaves visible the full-view of the unit given
1543 -- in the limited-with clause.
1545 loop
1546 if Designate_Same_Unit (Lim_Unit_Name,
1547 Unit_Name)
1548 then
1549 Error_Msg_Sloc := Sloc (It);
1550 Error_Msg_N
1551 ("simultaneous visibility of limited and "
1552 & "unlimited views not allowed", Item);
1553 Error_Msg_NE
1554 ("\unlimited view visible through context "
1555 & "clause #", Item, It);
1556 exit;
1558 elsif Nkind (Unit_Name) = N_Identifier then
1559 exit;
1560 end if;
1562 Unit_Name := Prefix (Unit_Name);
1563 end loop;
1564 end if;
1566 Next (It);
1567 end loop;
1569 exit when not Is_Child_Spec (Unit (Comp_Unit));
1571 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1572 end loop;
1573 end;
1574 end if;
1576 -- Skip analyzing with clause if no unit, see above
1578 if Present (Library_Unit (Item)) then
1579 Analyze (Item);
1580 end if;
1582 -- A limited_with does not impose an elaboration order, but there
1583 -- is a semantic dependency for recompilation purposes.
1585 if not Implicit_With (Item) then
1586 Version_Update (N, Library_Unit (Item));
1587 end if;
1589 -- Pragmas and use clauses and with clauses other than limited with's
1590 -- are ignored in this pass through the context items.
1592 else
1593 null;
1594 end if;
1596 Next (Item);
1597 end loop;
1598 end Analyze_Context;
1600 -------------------------------
1601 -- Analyze_Package_Body_Stub --
1602 -------------------------------
1604 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1605 Id : constant Entity_Id := Defining_Entity (N);
1606 Nam : Entity_Id;
1607 Opts : Config_Switches_Type;
1609 begin
1610 -- The package declaration must be in the current declarative part
1612 Check_Stub_Level (N);
1613 Nam := Current_Entity_In_Scope (Id);
1615 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1616 Error_Msg_N ("missing specification for package stub", N);
1618 elsif Has_Completion (Nam)
1619 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1620 then
1621 Error_Msg_N ("duplicate or redundant stub for package", N);
1623 else
1624 -- Retain and restore the configuration options of the enclosing
1625 -- context as the proper body may introduce a set of its own.
1627 Save_Opt_Config_Switches (Opts);
1629 -- Indicate that the body of the package exists. If we are doing
1630 -- only semantic analysis, the stub stands for the body. If we are
1631 -- generating code, the existence of the body will be confirmed
1632 -- when we load the proper body.
1634 Set_Scope (Id, Current_Scope);
1635 Set_Ekind (Id, E_Package_Body);
1636 Set_Etype (Id, Standard_Void_Type);
1638 if Has_Aspects (N) then
1639 Analyze_Aspect_Specifications (N, Id);
1640 end if;
1642 Set_Has_Completion (Nam);
1643 Set_Corresponding_Spec_Of_Stub (N, Nam);
1644 Generate_Reference (Nam, Id, 'b');
1645 Analyze_Proper_Body (N, Nam);
1647 Restore_Opt_Config_Switches (Opts);
1648 end if;
1649 end Analyze_Package_Body_Stub;
1651 -------------------------
1652 -- Analyze_Proper_Body --
1653 -------------------------
1655 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1656 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1658 procedure Optional_Subunit;
1659 -- This procedure is called when the main unit is a stub, or when we
1660 -- are not generating code. In such a case, we analyze the subunit if
1661 -- present, which is user-friendly and in fact required for ASIS, but we
1662 -- don't complain if the subunit is missing. In GNATprove_Mode, we issue
1663 -- an error to avoid formal verification of a partial unit.
1665 ----------------------
1666 -- Optional_Subunit --
1667 ----------------------
1669 procedure Optional_Subunit is
1670 Comp_Unit : Node_Id;
1671 Unum : Unit_Number_Type;
1673 begin
1674 -- Try to load subunit, but ignore any errors that occur during the
1675 -- loading of the subunit, by using the special feature in Errout to
1676 -- ignore all errors. Note that Fatal_Error will still be set, so we
1677 -- will be able to check for this case below.
1679 if not (ASIS_Mode or GNATprove_Mode) then
1680 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1681 end if;
1683 Unum :=
1684 Load_Unit
1685 (Load_Name => Subunit_Name,
1686 Required => GNATprove_Mode,
1687 Subunit => True,
1688 Error_Node => N);
1690 if not (ASIS_Mode or GNATprove_Mode) then
1691 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1692 end if;
1694 -- All done if we successfully loaded the subunit
1696 if Unum /= No_Unit
1697 and then (Fatal_Error (Unum) /= Error_Detected
1698 or else Try_Semantics)
1699 then
1700 Comp_Unit := Cunit (Unum);
1702 -- If the file was empty or seriously mangled, the unit itself may
1703 -- be missing.
1705 if No (Unit (Comp_Unit)) then
1706 Error_Msg_N
1707 ("subunit does not contain expected proper body", N);
1709 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1710 Error_Msg_N
1711 ("expected SEPARATE subunit, found child unit",
1712 Cunit_Entity (Unum));
1713 else
1714 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1715 Analyze_Subunit (Comp_Unit);
1716 Set_Library_Unit (N, Comp_Unit);
1717 Set_Corresponding_Body (N, Defining_Entity (Unit (Comp_Unit)));
1718 end if;
1720 elsif Unum = No_Unit
1721 and then Present (Nam)
1722 then
1723 if Is_Protected_Type (Nam) then
1724 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1725 else
1726 Set_Corresponding_Body (
1727 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1728 end if;
1729 end if;
1730 end Optional_Subunit;
1732 -- Local variables
1734 Comp_Unit : Node_Id;
1735 Unum : Unit_Number_Type;
1737 -- Start of processing for Analyze_Proper_Body
1739 begin
1740 -- If the subunit is already loaded, it means that the main unit is a
1741 -- subunit, and that the current unit is one of its parents which was
1742 -- being analyzed to provide the needed context for the analysis of the
1743 -- subunit. In this case we analyze the subunit and continue with the
1744 -- parent, without looking at subsequent subunits.
1746 if Is_Loaded (Subunit_Name) then
1748 -- If the proper body is already linked to the stub node, the stub is
1749 -- in a generic unit and just needs analyzing.
1751 if Present (Library_Unit (N)) then
1752 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1754 -- If the subunit has severe errors, the spec of the enclosing
1755 -- body may not be available, in which case do not try analysis.
1757 if Serious_Errors_Detected > 0
1758 and then No (Library_Unit (Library_Unit (N)))
1759 then
1760 return;
1761 end if;
1763 -- Collect SCO information for loaded subunit if we are in the
1764 -- extended main unit.
1766 if Generate_SCO
1767 and then In_Extended_Main_Source_Unit
1768 (Cunit_Entity (Current_Sem_Unit))
1769 then
1770 SCO_Record_Raw (Get_Cunit_Unit_Number (Library_Unit (N)));
1771 end if;
1773 Analyze_Subunit (Library_Unit (N));
1775 -- Otherwise we must load the subunit and link to it
1777 else
1778 -- Load the subunit, this must work, since we originally loaded
1779 -- the subunit earlier on. So this will not really load it, just
1780 -- give access to it.
1782 Unum :=
1783 Load_Unit
1784 (Load_Name => Subunit_Name,
1785 Required => True,
1786 Subunit => False,
1787 Error_Node => N);
1789 -- And analyze the subunit in the parent context (note that we
1790 -- do not call Semantics, since that would remove the parent
1791 -- context). Because of this, we have to manually reset the
1792 -- compiler state to Analyzing since it got destroyed by Load.
1794 if Unum /= No_Unit then
1795 Compiler_State := Analyzing;
1797 -- Check that the proper body is a subunit and not a child
1798 -- unit. If the unit was previously loaded, the error will
1799 -- have been emitted when copying the generic node, so we
1800 -- just return to avoid cascaded errors.
1802 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1803 return;
1804 end if;
1806 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1807 Analyze_Subunit (Cunit (Unum));
1808 Set_Library_Unit (N, Cunit (Unum));
1809 end if;
1810 end if;
1812 -- If the main unit is a subunit, then we are just performing semantic
1813 -- analysis on that subunit, and any other subunits of any parent unit
1814 -- should be ignored, except that if we are building trees for ASIS
1815 -- usage we want to annotate the stub properly. If the main unit is
1816 -- itself a subunit, another subunit is irrelevant unless it is a
1817 -- subunit of the current one, that is to say appears in the current
1818 -- source tree.
1820 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1821 and then Subunit_Name /= Unit_Name (Main_Unit)
1822 then
1823 if ASIS_Mode then
1824 declare
1825 PB : constant Node_Id := Proper_Body (Unit (Cunit (Main_Unit)));
1826 begin
1827 if Nkind_In (PB, N_Package_Body, N_Subprogram_Body)
1828 and then List_Containing (N) = Declarations (PB)
1829 then
1830 Optional_Subunit;
1831 end if;
1832 end;
1833 end if;
1835 -- But before we return, set the flag for unloaded subunits. This
1836 -- will suppress junk warnings of variables in the same declarative
1837 -- part (or a higher level one) that are in danger of looking unused
1838 -- when in fact there might be a declaration in the subunit that we
1839 -- do not intend to load.
1841 Unloaded_Subunits := True;
1842 return;
1844 -- If the subunit is not already loaded, and we are generating code,
1845 -- then this is the case where compilation started from the parent, and
1846 -- we are generating code for an entire subunit tree. In that case we
1847 -- definitely need to load the subunit.
1849 -- In order to continue the analysis with the rest of the parent,
1850 -- and other subunits, we load the unit without requiring its
1851 -- presence, and emit a warning if not found, rather than terminating
1852 -- the compilation abruptly, as for other missing file problems.
1854 elsif Original_Operating_Mode = Generate_Code then
1856 -- If the proper body is already linked to the stub node, the stub is
1857 -- in a generic unit and just needs analyzing.
1859 -- We update the version. Although we are not strictly technically
1860 -- semantically dependent on the subunit, given our approach of macro
1861 -- substitution of subunits, it makes sense to include it in the
1862 -- version identification.
1864 if Present (Library_Unit (N)) then
1865 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1866 Analyze_Subunit (Library_Unit (N));
1867 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1869 -- Otherwise we must load the subunit and link to it
1871 else
1872 -- Make sure that, if the subunit is preprocessed and -gnateG is
1873 -- specified, the preprocessed file will be written.
1875 Lib.Analysing_Subunit_Of_Main := True;
1876 Unum :=
1877 Load_Unit
1878 (Load_Name => Subunit_Name,
1879 Required => False,
1880 Subunit => True,
1881 Error_Node => N);
1882 Lib.Analysing_Subunit_Of_Main := False;
1884 -- Give message if we did not get the unit Emit warning even if
1885 -- missing subunit is not within main unit, to simplify debugging.
1887 pragma Assert (Original_Operating_Mode = Generate_Code);
1888 if Unum = No_Unit then
1889 Error_Msg_Unit_1 := Subunit_Name;
1890 Error_Msg_File_1 :=
1891 Get_File_Name (Subunit_Name, Subunit => True);
1892 Error_Msg_N
1893 ("subunit$$ in file{ not found??!!", N);
1894 Subunits_Missing := True;
1895 end if;
1897 -- Load_Unit may reset Compiler_State, since it may have been
1898 -- necessary to parse an additional units, so we make sure that
1899 -- we reset it to the Analyzing state.
1901 Compiler_State := Analyzing;
1903 if Unum /= No_Unit then
1904 if Debug_Flag_L then
1905 Write_Str ("*** Loaded subunit from stub. Analyze");
1906 Write_Eol;
1907 end if;
1909 Comp_Unit := Cunit (Unum);
1911 -- Check for child unit instead of subunit
1913 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1914 Error_Msg_N
1915 ("expected SEPARATE subunit, found child unit",
1916 Cunit_Entity (Unum));
1918 -- OK, we have a subunit
1920 else
1921 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1922 Set_Library_Unit (N, Comp_Unit);
1924 -- We update the version. Although we are not technically
1925 -- semantically dependent on the subunit, given our approach
1926 -- of macro substitution of subunits, it makes sense to
1927 -- include it in the version identification.
1929 Version_Update (Cunit (Main_Unit), Comp_Unit);
1931 -- Collect SCO information for loaded subunit if we are in
1932 -- the extended main unit.
1934 if Generate_SCO
1935 and then In_Extended_Main_Source_Unit
1936 (Cunit_Entity (Current_Sem_Unit))
1937 then
1938 SCO_Record_Raw (Unum);
1939 end if;
1941 -- Analyze the unit if semantics active
1943 if Fatal_Error (Unum) /= Error_Detected
1944 or else Try_Semantics
1945 then
1946 Analyze_Subunit (Comp_Unit);
1947 end if;
1948 end if;
1949 end if;
1950 end if;
1952 -- The remaining case is when the subunit is not already loaded and we
1953 -- are not generating code. In this case we are just performing semantic
1954 -- analysis on the parent, and we are not interested in the subunit. For
1955 -- subprograms, analyze the stub as a body. For other entities the stub
1956 -- has already been marked as completed.
1958 else
1959 Optional_Subunit;
1960 end if;
1961 end Analyze_Proper_Body;
1963 ----------------------------------
1964 -- Analyze_Protected_Body_Stub --
1965 ----------------------------------
1967 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1968 Id : constant Entity_Id := Defining_Entity (N);
1969 Nam : Entity_Id := Current_Entity_In_Scope (Id);
1970 Opts : Config_Switches_Type;
1972 begin
1973 Check_Stub_Level (N);
1975 -- First occurrence of name may have been as an incomplete type
1977 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1978 Nam := Full_View (Nam);
1979 end if;
1981 if No (Nam) or else not Is_Protected_Type (Etype (Nam)) then
1982 Error_Msg_N ("missing specification for Protected body", N);
1984 else
1985 -- Retain and restore the configuration options of the enclosing
1986 -- context as the proper body may introduce a set of its own.
1988 Save_Opt_Config_Switches (Opts);
1990 Set_Scope (Id, Current_Scope);
1991 Set_Ekind (Id, E_Protected_Body);
1992 Set_Etype (Id, Standard_Void_Type);
1994 if Has_Aspects (N) then
1995 Analyze_Aspect_Specifications (N, Id);
1996 end if;
1998 Set_Has_Completion (Etype (Nam));
1999 Set_Corresponding_Spec_Of_Stub (N, Nam);
2000 Generate_Reference (Nam, Id, 'b');
2001 Analyze_Proper_Body (N, Etype (Nam));
2003 Restore_Opt_Config_Switches (Opts);
2004 end if;
2005 end Analyze_Protected_Body_Stub;
2007 ----------------------------------
2008 -- Analyze_Subprogram_Body_Stub --
2009 ----------------------------------
2011 -- A subprogram body stub can appear with or without a previous spec. If
2012 -- there is one, then the analysis of the body will find it and verify
2013 -- conformance. The formals appearing in the specification of the stub play
2014 -- no role, except for requiring an additional conformance check. If there
2015 -- is no previous subprogram declaration, the stub acts as a spec, and
2016 -- provides the defining entity for the subprogram.
2018 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
2019 Decl : Node_Id;
2020 Opts : Config_Switches_Type;
2022 begin
2023 Check_Stub_Level (N);
2025 -- Verify that the identifier for the stub is unique within this
2026 -- declarative part.
2028 if Nkind_In (Parent (N), N_Block_Statement,
2029 N_Package_Body,
2030 N_Subprogram_Body)
2031 then
2032 Decl := First (Declarations (Parent (N)));
2033 while Present (Decl) and then Decl /= N loop
2034 if Nkind (Decl) = N_Subprogram_Body_Stub
2035 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
2036 Chars (Defining_Unit_Name (Specification (N))))
2037 then
2038 Error_Msg_N ("identifier for stub is not unique", N);
2039 end if;
2041 Next (Decl);
2042 end loop;
2043 end if;
2045 -- Retain and restore the configuration options of the enclosing context
2046 -- as the proper body may introduce a set of its own.
2048 Save_Opt_Config_Switches (Opts);
2050 -- Treat stub as a body, which checks conformance if there is a previous
2051 -- declaration, or else introduces entity and its signature.
2053 Analyze_Subprogram_Body (N);
2054 Analyze_Proper_Body (N, Empty);
2056 Restore_Opt_Config_Switches (Opts);
2057 end Analyze_Subprogram_Body_Stub;
2059 ---------------------
2060 -- Analyze_Subunit --
2061 ---------------------
2063 -- A subunit is compiled either by itself (for semantic checking) or as
2064 -- part of compiling the parent (for code generation). In either case, by
2065 -- the time we actually process the subunit, the parent has already been
2066 -- installed and analyzed. The node N is a compilation unit, whose context
2067 -- needs to be treated here, because we come directly here from the parent
2068 -- without calling Analyze_Compilation_Unit.
2070 -- The compilation context includes the explicit context of the subunit,
2071 -- and the context of the parent, together with the parent itself. In order
2072 -- to compile the current context, we remove the one inherited from the
2073 -- parent, in order to have a clean visibility table. We restore the parent
2074 -- context before analyzing the proper body itself. On exit, we remove only
2075 -- the explicit context of the subunit.
2077 -- WARNING: This routine manages SPARK regions. Return statements must be
2078 -- replaced by gotos which jump to the end of the routine and restore the
2079 -- SPARK mode.
2081 procedure Analyze_Subunit (N : Node_Id) is
2082 Lib_Unit : constant Node_Id := Library_Unit (N);
2083 Par_Unit : constant Entity_Id := Current_Scope;
2085 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
2086 Num_Scopes : Nat := 0;
2087 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2088 Enclosing_Child : Entity_Id := Empty;
2089 Svg : constant Suppress_Record := Scope_Suppress;
2091 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
2092 Cunit_Boolean_Restrictions_Save;
2093 -- Save non-partition wide restrictions before processing the subunit.
2094 -- All subunits are analyzed with config restrictions reset and we need
2095 -- to restore these saved values at the end.
2097 procedure Analyze_Subunit_Context;
2098 -- Capture names in use clauses of the subunit. This must be done before
2099 -- re-installing parent declarations, because items in the context must
2100 -- not be hidden by declarations local to the parent.
2102 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
2103 -- Recursive procedure to restore scope of all ancestors of subunit,
2104 -- from outermost in. If parent is not a subunit, the call to install
2105 -- context installs context of spec and (if parent is a child unit) the
2106 -- context of its parents as well. It is confusing that parents should
2107 -- be treated differently in both cases, but the semantics are just not
2108 -- identical.
2110 procedure Re_Install_Use_Clauses;
2111 -- As part of the removal of the parent scope, the use clauses are
2112 -- removed, to be reinstalled when the context of the subunit has been
2113 -- analyzed. Use clauses may also have been affected by the analysis of
2114 -- the context of the subunit, so they have to be applied again, to
2115 -- insure that the compilation environment of the rest of the parent
2116 -- unit is identical.
2118 procedure Remove_Scope;
2119 -- Remove current scope from scope stack, and preserve the list of use
2120 -- clauses in it, to be reinstalled after context is analyzed.
2122 -----------------------------
2123 -- Analyze_Subunit_Context --
2124 -----------------------------
2126 procedure Analyze_Subunit_Context is
2127 Item : Node_Id;
2128 Unit_Name : Entity_Id;
2130 begin
2131 Analyze_Context (N);
2132 Check_No_Elab_Code_All (N);
2134 -- Make withed units immediately visible. If child unit, make the
2135 -- ultimate parent immediately visible.
2137 Item := First (Context_Items (N));
2138 while Present (Item) loop
2139 if Nkind (Item) = N_With_Clause then
2141 -- Protect frontend against previous errors in context clauses
2143 if Nkind (Name (Item)) /= N_Selected_Component then
2144 if Error_Posted (Item) then
2145 null;
2147 else
2148 -- If a subunits has serious syntax errors, the context
2149 -- may not have been loaded. Add a harmless unit name to
2150 -- attempt processing.
2152 if Serious_Errors_Detected > 0
2153 and then No (Entity (Name (Item)))
2154 then
2155 Set_Entity (Name (Item), Standard_Standard);
2156 end if;
2158 Unit_Name := Entity (Name (Item));
2159 loop
2160 Set_Is_Visible_Lib_Unit (Unit_Name);
2161 exit when Scope (Unit_Name) = Standard_Standard;
2162 Unit_Name := Scope (Unit_Name);
2164 if No (Unit_Name) then
2165 Check_Error_Detected;
2166 return;
2167 end if;
2168 end loop;
2170 if not Is_Immediately_Visible (Unit_Name) then
2171 Set_Is_Immediately_Visible (Unit_Name);
2172 Set_Context_Installed (Item);
2173 end if;
2174 end if;
2175 end if;
2177 elsif Nkind (Item) = N_Use_Package_Clause then
2178 Analyze (Name (Item));
2180 elsif Nkind (Item) = N_Use_Type_Clause then
2181 Analyze (Subtype_Mark (Item));
2182 end if;
2184 Next (Item);
2185 end loop;
2187 -- Reset visibility of withed units. They will be made visible again
2188 -- when we install the subunit context.
2190 Item := First (Context_Items (N));
2191 while Present (Item) loop
2192 if Nkind (Item) = N_With_Clause
2194 -- Protect frontend against previous errors in context clauses
2196 and then Nkind (Name (Item)) /= N_Selected_Component
2197 and then not Error_Posted (Item)
2198 then
2199 Unit_Name := Entity (Name (Item));
2200 loop
2201 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2202 exit when Scope (Unit_Name) = Standard_Standard;
2203 Unit_Name := Scope (Unit_Name);
2204 end loop;
2206 if Context_Installed (Item) then
2207 Set_Is_Immediately_Visible (Unit_Name, False);
2208 Set_Context_Installed (Item, False);
2209 end if;
2210 end if;
2212 Next (Item);
2213 end loop;
2214 end Analyze_Subunit_Context;
2216 ------------------------
2217 -- Re_Install_Parents --
2218 ------------------------
2220 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2221 E : Entity_Id;
2223 begin
2224 if Nkind (Unit (L)) = N_Subunit then
2225 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2226 end if;
2228 Install_Context (L, False);
2230 -- If the subunit occurs within a child unit, we must restore the
2231 -- immediate visibility of any siblings that may occur in context.
2233 if Present (Enclosing_Child) then
2234 Install_Siblings (Enclosing_Child, L);
2235 end if;
2237 Push_Scope (Scop);
2239 if Scop /= Par_Unit then
2240 Set_Is_Immediately_Visible (Scop);
2241 end if;
2243 -- Make entities in scope visible again. For child units, restore
2244 -- visibility only if they are actually in context.
2246 E := First_Entity (Current_Scope);
2247 while Present (E) loop
2248 if not Is_Child_Unit (E) or else Is_Visible_Lib_Unit (E) then
2249 Set_Is_Immediately_Visible (E);
2250 end if;
2252 Next_Entity (E);
2253 end loop;
2255 -- A subunit appears within a body, and for a nested subunits all the
2256 -- parents are bodies. Restore full visibility of their private
2257 -- entities.
2259 if Is_Package_Or_Generic_Package (Scop) then
2260 Set_In_Package_Body (Scop);
2261 Install_Private_Declarations (Scop);
2262 end if;
2263 end Re_Install_Parents;
2265 ----------------------------
2266 -- Re_Install_Use_Clauses --
2267 ----------------------------
2269 procedure Re_Install_Use_Clauses is
2270 U : Node_Id;
2271 begin
2272 for J in reverse 1 .. Num_Scopes loop
2273 U := Use_Clauses (J);
2274 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2275 Install_Use_Clauses (U);
2276 end loop;
2277 end Re_Install_Use_Clauses;
2279 ------------------
2280 -- Remove_Scope --
2281 ------------------
2283 procedure Remove_Scope is
2284 E : Entity_Id;
2286 begin
2287 Num_Scopes := Num_Scopes + 1;
2288 Use_Clauses (Num_Scopes) :=
2289 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2291 E := First_Entity (Current_Scope);
2292 while Present (E) loop
2293 Set_Is_Immediately_Visible (E, False);
2294 Next_Entity (E);
2295 end loop;
2297 if Is_Child_Unit (Current_Scope) then
2298 Enclosing_Child := Current_Scope;
2299 end if;
2301 Pop_Scope;
2302 end Remove_Scope;
2304 Saved_SM : SPARK_Mode_Type := SPARK_Mode;
2305 Saved_SMP : Node_Id := SPARK_Mode_Pragma;
2306 -- Save the SPARK mode-related data to restore on exit. Removing
2307 -- enclosing scopes and contexts to provide a clean environment for the
2308 -- context of the subunit will eliminate any previously set SPARK_Mode.
2310 -- Start of processing for Analyze_Subunit
2312 begin
2313 -- For subunit in main extended unit, we reset the configuration values
2314 -- for the non-partition-wide restrictions. For other units reset them.
2316 if In_Extended_Main_Source_Unit (N) then
2317 Restore_Config_Cunit_Boolean_Restrictions;
2318 else
2319 Reset_Cunit_Boolean_Restrictions;
2320 end if;
2322 if Style_Check then
2323 declare
2324 Nam : Node_Id := Name (Unit (N));
2326 begin
2327 if Nkind (Nam) = N_Selected_Component then
2328 Nam := Selector_Name (Nam);
2329 end if;
2331 Check_Identifier (Nam, Par_Unit);
2332 end;
2333 end if;
2335 if not Is_Empty_List (Context_Items (N)) then
2337 -- Save current use clauses
2339 Remove_Scope;
2340 Remove_Context (Lib_Unit);
2342 -- Now remove parents and their context, including enclosing subunits
2343 -- and the outer parent body which is not a subunit.
2345 if Present (Lib_Spec) then
2346 Remove_Context (Lib_Spec);
2348 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2349 Lib_Spec := Library_Unit (Lib_Spec);
2350 Remove_Scope;
2351 Remove_Context (Lib_Spec);
2352 end loop;
2354 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2355 Remove_Scope;
2356 end if;
2358 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2359 Remove_Context (Library_Unit (Lib_Spec));
2360 end if;
2361 end if;
2363 Set_Is_Immediately_Visible (Par_Unit, False);
2365 Analyze_Subunit_Context;
2367 -- Take into account the effect of any SPARK_Mode configuration
2368 -- pragma, which takes precedence over a different value of
2369 -- SPARK_Mode inherited from the context of the stub.
2371 if SPARK_Mode /= None then
2372 Saved_SM := SPARK_Mode;
2373 Saved_SMP := SPARK_Mode_Pragma;
2374 end if;
2376 Re_Install_Parents (Lib_Unit, Par_Unit);
2377 Set_Is_Immediately_Visible (Par_Unit);
2379 -- If the context includes a child unit of the parent of the subunit,
2380 -- the parent will have been removed from visibility, after compiling
2381 -- that cousin in the context. The visibility of the parent must be
2382 -- restored now. This also applies if the context includes another
2383 -- subunit of the same parent which in turn includes a child unit in
2384 -- its context.
2386 if Is_Package_Or_Generic_Package (Par_Unit) then
2387 if not Is_Immediately_Visible (Par_Unit)
2388 or else (Present (First_Entity (Par_Unit))
2389 and then not
2390 Is_Immediately_Visible (First_Entity (Par_Unit)))
2391 then
2392 Set_Is_Immediately_Visible (Par_Unit);
2393 Install_Visible_Declarations (Par_Unit);
2394 Install_Private_Declarations (Par_Unit);
2395 end if;
2396 end if;
2398 Re_Install_Use_Clauses;
2399 Install_Context (N, Chain => False);
2401 -- Restore state of suppress flags for current body
2403 Scope_Suppress := Svg;
2405 -- If the subunit is within a child unit, then siblings of any parent
2406 -- unit that appear in the context clause of the subunit must also be
2407 -- made immediately visible.
2409 if Present (Enclosing_Child) then
2410 Install_Siblings (Enclosing_Child, N);
2411 end if;
2412 end if;
2414 Generate_Parent_References (Unit (N), Par_Unit);
2416 -- Reinstall the SPARK_Mode which was in effect prior to any scope and
2417 -- context manipulations, taking into account a possible SPARK_Mode
2418 -- configuration pragma if present.
2420 Install_SPARK_Mode (Saved_SM, Saved_SMP);
2422 -- If the subunit is part of a compilation unit which is subject to
2423 -- pragma Elaboration_Checks, set the model specified by the pragma
2424 -- because it applies to all parts of the unit.
2426 Install_Elaboration_Model (Par_Unit);
2428 Analyze (Proper_Body (Unit (N)));
2429 Remove_Context (N);
2431 -- The subunit may contain a with_clause on a sibling of some ancestor.
2432 -- Removing the context will remove from visibility those ancestor child
2433 -- units, which must be restored to the visibility they have in the
2434 -- enclosing body.
2436 if Present (Enclosing_Child) then
2437 declare
2438 C : Entity_Id;
2439 begin
2440 C := Current_Scope;
2441 while Present (C) and then C /= Standard_Standard loop
2442 Set_Is_Immediately_Visible (C);
2443 Set_Is_Visible_Lib_Unit (C);
2444 C := Scope (C);
2445 end loop;
2446 end;
2447 end if;
2449 -- Deal with restore of restrictions
2451 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2452 end Analyze_Subunit;
2454 ----------------------------
2455 -- Analyze_Task_Body_Stub --
2456 ----------------------------
2458 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2459 Id : constant Entity_Id := Defining_Entity (N);
2460 Loc : constant Source_Ptr := Sloc (N);
2461 Nam : Entity_Id := Current_Entity_In_Scope (Id);
2463 begin
2464 Check_Stub_Level (N);
2466 -- First occurrence of name may have been as an incomplete type
2468 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2469 Nam := Full_View (Nam);
2470 end if;
2472 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2473 Error_Msg_N ("missing specification for task body", N);
2475 else
2476 Set_Scope (Id, Current_Scope);
2477 Set_Ekind (Id, E_Task_Body);
2478 Set_Etype (Id, Standard_Void_Type);
2480 if Has_Aspects (N) then
2481 Analyze_Aspect_Specifications (N, Id);
2482 end if;
2484 Generate_Reference (Nam, Id, 'b');
2485 Set_Corresponding_Spec_Of_Stub (N, Nam);
2487 -- Check for duplicate stub, if so give message and terminate
2489 if Has_Completion (Etype (Nam)) then
2490 Error_Msg_N ("duplicate stub for task", N);
2491 return;
2492 else
2493 Set_Has_Completion (Etype (Nam));
2494 end if;
2496 Analyze_Proper_Body (N, Etype (Nam));
2498 -- Set elaboration flag to indicate that entity is callable. This
2499 -- cannot be done in the expansion of the body itself, because the
2500 -- proper body is not in a declarative part. This is only done if
2501 -- expansion is active, because the context may be generic and the
2502 -- flag not defined yet.
2504 if Expander_Active then
2505 Insert_After (N,
2506 Make_Assignment_Statement (Loc,
2507 Name =>
2508 Make_Identifier (Loc,
2509 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2510 Expression => New_Occurrence_Of (Standard_True, Loc)));
2511 end if;
2512 end if;
2513 end Analyze_Task_Body_Stub;
2515 -------------------------
2516 -- Analyze_With_Clause --
2517 -------------------------
2519 -- Analyze the declaration of a unit in a with clause. At end, label the
2520 -- with clause with the defining entity for the unit.
2522 procedure Analyze_With_Clause (N : Node_Id) is
2524 -- Retrieve the original kind of the unit node, before analysis. If it
2525 -- is a subprogram instantiation, its analysis below will rewrite the
2526 -- node as the declaration of the wrapper package. If the same
2527 -- instantiation appears indirectly elsewhere in the context, it will
2528 -- have been analyzed already.
2530 Unit_Kind : constant Node_Kind :=
2531 Nkind (Original_Node (Unit (Library_Unit (N))));
2532 Nam : constant Node_Id := Name (N);
2533 E_Name : Entity_Id;
2534 Par_Name : Entity_Id;
2535 Pref : Node_Id;
2536 U : Node_Id;
2538 Intunit : Boolean;
2539 -- Set True if the unit currently being compiled is an internal unit
2541 Restriction_Violation : Boolean := False;
2542 -- Set True if a with violates a restriction, no point in giving any
2543 -- warnings if we have this definite error.
2545 Save_Style_Check : constant Boolean := Opt.Style_Check;
2547 begin
2548 U := Unit (Library_Unit (N));
2550 -- If this is an internal unit which is a renaming, then this is a
2551 -- violation of No_Obsolescent_Features.
2553 -- Note: this is not quite right if the user defines one of these units
2554 -- himself, but that's a marginal case, and fixing it is hard ???
2556 if Restriction_Check_Required (No_Obsolescent_Features) then
2557 if In_Predefined_Renaming (U) then
2558 Check_Restriction (No_Obsolescent_Features, N);
2559 Restriction_Violation := True;
2560 end if;
2561 end if;
2563 -- Check No_Implementation_Units violation
2565 if Restriction_Check_Required (No_Implementation_Units) then
2566 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2567 null;
2568 else
2569 Check_Restriction (No_Implementation_Units, Nam);
2570 Restriction_Violation := True;
2571 end if;
2572 end if;
2574 -- Several actions are skipped for dummy packages (those supplied for
2575 -- with's where no matching file could be found). Such packages are
2576 -- identified by the Sloc value being set to No_Location.
2578 if Limited_Present (N) then
2580 -- Ada 2005 (AI-50217): Build visibility structures but do not
2581 -- analyze the unit.
2583 -- If the designated unit is a predefined unit, which might be used
2584 -- implicitly through the rtsfind machinery, a limited with clause
2585 -- on such a unit is usually pointless, because run-time units are
2586 -- unlikely to appear in mutually dependent units, and because this
2587 -- disables the rtsfind mechanism. We transform such limited with
2588 -- clauses into regular with clauses.
2590 if Sloc (U) /= No_Location then
2591 if In_Predefined_Unit (U)
2593 -- In ASIS mode the rtsfind mechanism plays no role, and
2594 -- we need to maintain the original tree structure, so
2595 -- this transformation is not performed in this case.
2597 and then not ASIS_Mode
2598 then
2599 Set_Limited_Present (N, False);
2600 Analyze_With_Clause (N);
2601 else
2602 Build_Limited_Views (N);
2603 end if;
2604 end if;
2606 return;
2607 end if;
2609 -- If we are compiling under "don't quit" mode (-gnatq) and we have
2610 -- already detected serious errors then we mark the with-clause nodes as
2611 -- analyzed before the corresponding compilation unit is analyzed. This
2612 -- is done here to protect the frontend against never ending recursion
2613 -- caused by circularities in the sources (because the previous errors
2614 -- may break the regular machine of the compiler implemented in
2615 -- Load_Unit to detect circularities).
2617 if Serious_Errors_Detected > 0 and then Try_Semantics then
2618 Set_Analyzed (N);
2619 end if;
2621 Semantics (Library_Unit (N));
2623 Intunit := Is_Internal_Unit (Current_Sem_Unit);
2625 if Sloc (U) /= No_Location then
2627 -- Check restrictions, except that we skip the check if this is an
2628 -- internal unit unless we are compiling the internal unit as the
2629 -- main unit. We also skip this for dummy packages.
2631 Check_Restriction_No_Dependence (Nam, N);
2633 if not Intunit or else Current_Sem_Unit = Main_Unit then
2634 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2635 end if;
2637 -- Deal with special case of GNAT.Current_Exceptions which interacts
2638 -- with the optimization of local raise statements into gotos.
2640 if Nkind (Nam) = N_Selected_Component
2641 and then Nkind (Prefix (Nam)) = N_Identifier
2642 and then Chars (Prefix (Nam)) = Name_Gnat
2643 and then Nam_In (Chars (Selector_Name (Nam)),
2644 Name_Most_Recent_Exception,
2645 Name_Exception_Traces)
2646 then
2647 Check_Restriction (No_Exception_Propagation, N);
2648 Special_Exception_Package_Used := True;
2649 end if;
2651 -- Check for inappropriate with of internal implementation unit if we
2652 -- are not compiling an internal unit and also check for withing unit
2653 -- in wrong version of Ada. Do not issue these messages for implicit
2654 -- with's generated by the compiler itself.
2656 if Implementation_Unit_Warnings
2657 and then not Intunit
2658 and then not Implicit_With (N)
2659 and then not Restriction_Violation
2660 then
2661 declare
2662 U_Kind : constant Kind_Of_Unit :=
2663 Get_Kind_Of_Unit (Get_Source_Unit (U));
2665 begin
2666 if U_Kind = Implementation_Unit then
2667 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2669 -- Add alternative name if available, otherwise issue a
2670 -- general warning message.
2672 if Error_Msg_Strlen /= 0 then
2673 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2674 else
2675 Error_Msg_F
2676 ("\use of this unit is non-portable and "
2677 & "version-dependent?i?", Name (N));
2678 end if;
2680 elsif U_Kind = Ada_2005_Unit
2681 and then Ada_Version < Ada_2005
2682 and then Warn_On_Ada_2005_Compatibility
2683 then
2684 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2686 elsif U_Kind = Ada_2012_Unit
2687 and then Ada_Version < Ada_2012
2688 and then Warn_On_Ada_2012_Compatibility
2689 then
2690 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2691 end if;
2692 end;
2693 end if;
2694 end if;
2696 -- Semantic analysis of a generic unit is performed on a copy of
2697 -- the original tree. Retrieve the entity on which semantic info
2698 -- actually appears.
2700 if Unit_Kind in N_Generic_Declaration then
2701 E_Name := Defining_Entity (U);
2703 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2704 -- the case of an instantiation, semantic analysis above will have
2705 -- replaced the unit by its instantiated version. If the instance body
2706 -- has been generated, the instance now denotes the body entity. For
2707 -- visibility purposes we need the entity of its spec.
2709 elsif (Unit_Kind = N_Package_Instantiation
2710 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2711 N_Package_Instantiation)
2712 and then Nkind (U) = N_Package_Body
2713 then
2714 E_Name := Corresponding_Spec (U);
2716 elsif Unit_Kind = N_Package_Instantiation
2717 and then Nkind (U) = N_Package_Instantiation
2718 and then Present (Instance_Spec (U))
2719 then
2720 -- If the instance has not been rewritten as a package declaration,
2721 -- then it appeared already in a previous with clause. Retrieve
2722 -- the entity from the previous instance.
2724 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2726 elsif Unit_Kind in N_Subprogram_Instantiation then
2728 -- The visible subprogram is created during instantiation, and is
2729 -- an attribute of the wrapper package. We retrieve the wrapper
2730 -- package directly from the instantiation node. If the instance
2731 -- is inlined the unit is still an instantiation. Otherwise it has
2732 -- been rewritten as the declaration of the wrapper itself.
2734 if Nkind (U) in N_Subprogram_Instantiation then
2735 E_Name :=
2736 Related_Instance
2737 (Defining_Entity (Specification (Instance_Spec (U))));
2738 else
2739 E_Name := Related_Instance (Defining_Entity (U));
2740 end if;
2742 elsif Unit_Kind = N_Package_Renaming_Declaration
2743 or else Unit_Kind in N_Generic_Renaming_Declaration
2744 then
2745 E_Name := Defining_Entity (U);
2747 elsif Unit_Kind = N_Subprogram_Body
2748 and then Nkind (Name (N)) = N_Selected_Component
2749 and then not Acts_As_Spec (Library_Unit (N))
2750 then
2751 -- For a child unit that has no spec, one has been created and
2752 -- analyzed. The entity required is that of the spec.
2754 E_Name := Corresponding_Spec (U);
2756 else
2757 E_Name := Defining_Entity (U);
2758 end if;
2760 if Nkind (Name (N)) = N_Selected_Component then
2762 -- Child unit in a with clause
2764 Change_Selected_Component_To_Expanded_Name (Name (N));
2766 -- If this is a child unit without a spec, and it has been analyzed
2767 -- already, a declaration has been created for it. The with_clause
2768 -- must reflect the actual body, and not the generated declaration,
2769 -- to prevent spurious binding errors involving an out-of-date spec.
2770 -- Note that this can only happen if the unit includes more than one
2771 -- with_clause for the child unit (e.g. in separate subunits).
2773 if Unit_Kind = N_Subprogram_Declaration
2774 and then Analyzed (Library_Unit (N))
2775 and then not Comes_From_Source (Library_Unit (N))
2776 then
2777 Set_Library_Unit (N,
2778 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2779 end if;
2780 end if;
2782 -- Restore style checks
2784 Style_Check := Save_Style_Check;
2786 -- Record the reference, but do NOT set the unit as referenced, we want
2787 -- to consider the unit as unreferenced if this is the only reference
2788 -- that occurs.
2790 Set_Entity_With_Checks (Name (N), E_Name);
2791 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2793 -- Generate references and check No_Dependence restriction for parents
2795 if Is_Child_Unit (E_Name) then
2796 Pref := Prefix (Name (N));
2797 Par_Name := Scope (E_Name);
2798 while Nkind (Pref) = N_Selected_Component loop
2799 Change_Selected_Component_To_Expanded_Name (Pref);
2801 if Present (Entity (Selector_Name (Pref)))
2802 and then
2803 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2804 and then Entity (Selector_Name (Pref)) /= Par_Name
2805 then
2806 -- The prefix is a child unit that denotes a renaming declaration.
2807 -- Replace the prefix directly with the renamed unit, because the
2808 -- rest of the prefix is irrelevant to the visibility of the real
2809 -- unit.
2811 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2812 exit;
2813 end if;
2815 Set_Entity_With_Checks (Pref, Par_Name);
2817 Generate_Reference (Par_Name, Pref);
2818 Check_Restriction_No_Dependence (Pref, N);
2819 Pref := Prefix (Pref);
2821 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2822 -- is set to Standard_Standard, and no attempt should be made to
2823 -- further unwind scopes.
2825 if Par_Name /= Standard_Standard then
2826 Par_Name := Scope (Par_Name);
2827 end if;
2829 -- Abandon processing in case of previous errors
2831 if No (Par_Name) then
2832 Check_Error_Detected;
2833 return;
2834 end if;
2835 end loop;
2837 if Present (Entity (Pref))
2838 and then not Analyzed (Parent (Parent (Entity (Pref))))
2839 then
2840 -- If the entity is set without its unit being compiled, the
2841 -- original parent is a renaming, and Par_Name is the renamed
2842 -- entity. For visibility purposes, we need the original entity,
2843 -- which must be analyzed now because Load_Unit directly retrieves
2844 -- the renamed unit, and the renaming declaration itself has not
2845 -- been analyzed.
2847 Analyze (Parent (Parent (Entity (Pref))));
2848 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2849 Par_Name := Entity (Pref);
2850 end if;
2852 -- Guard against missing or misspelled child units
2854 if Present (Par_Name) then
2855 Set_Entity_With_Checks (Pref, Par_Name);
2856 Generate_Reference (Par_Name, Pref);
2858 else
2859 pragma Assert (Serious_Errors_Detected /= 0);
2861 -- Mark the node to indicate that a related error has been posted.
2862 -- This defends further compilation passes against improper use of
2863 -- the invalid WITH clause node.
2865 Set_Error_Posted (N);
2866 Set_Name (N, Error);
2867 return;
2868 end if;
2869 end if;
2871 -- If the withed unit is System, and a system extension pragma is
2872 -- present, compile the extension now, rather than waiting for a
2873 -- visibility check on a specific entity.
2875 if Chars (E_Name) = Name_System
2876 and then Scope (E_Name) = Standard_Standard
2877 and then Present (System_Extend_Unit)
2878 and then Present_System_Aux (N)
2879 then
2880 -- If the extension is not present, an error will have been emitted
2882 null;
2883 end if;
2885 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2886 -- to private_with units; they will be made visible later (just before
2887 -- the private part is analyzed)
2889 if Private_Present (N) then
2890 Set_Is_Immediately_Visible (E_Name, False);
2891 end if;
2893 -- Propagate Fatal_Error setting from with'ed unit to current unit
2895 case Fatal_Error (Get_Source_Unit (Library_Unit (N))) is
2897 -- Nothing to do if with'ed unit had no error
2899 when None =>
2900 null;
2902 -- If with'ed unit had a detected fatal error, propagate it
2904 when Error_Detected =>
2905 Set_Fatal_Error (Current_Sem_Unit, Error_Detected);
2907 -- If with'ed unit had an ignored error, then propagate it but do not
2908 -- overide an existring setting.
2910 when Error_Ignored =>
2911 if Fatal_Error (Current_Sem_Unit) = None then
2912 Set_Fatal_Error (Current_Sem_Unit, Error_Ignored);
2913 end if;
2914 end case;
2916 Mark_Ghost_Clause (N);
2917 end Analyze_With_Clause;
2919 ------------------------------
2920 -- Check_Private_Child_Unit --
2921 ------------------------------
2923 procedure Check_Private_Child_Unit (N : Node_Id) is
2924 Lib_Unit : constant Node_Id := Unit (N);
2925 Item : Node_Id;
2926 Curr_Unit : Entity_Id;
2927 Sub_Parent : Node_Id;
2928 Priv_Child : Entity_Id;
2929 Par_Lib : Entity_Id;
2930 Par_Spec : Node_Id;
2932 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2933 -- Returns true if and only if the library unit is declared with
2934 -- an explicit designation of private.
2936 -----------------------------
2937 -- Is_Private_Library_Unit --
2938 -----------------------------
2940 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2941 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2943 begin
2944 return Private_Present (Comp_Unit);
2945 end Is_Private_Library_Unit;
2947 -- Start of processing for Check_Private_Child_Unit
2949 begin
2950 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2951 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2952 Par_Lib := Curr_Unit;
2954 elsif Nkind (Lib_Unit) = N_Subunit then
2956 -- The parent is itself a body. The parent entity is to be found in
2957 -- the corresponding spec.
2959 Sub_Parent := Library_Unit (N);
2960 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2962 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2963 -- enclosing body, retrieve the spec entity which is the proper
2964 -- ancestor we need for the following tests.
2966 if Ekind (Curr_Unit) = E_Package_Body then
2967 Curr_Unit := Spec_Entity (Curr_Unit);
2968 end if;
2970 Par_Lib := Curr_Unit;
2972 else
2973 Curr_Unit := Defining_Entity (Lib_Unit);
2975 Par_Lib := Curr_Unit;
2976 Par_Spec := Parent_Spec (Lib_Unit);
2978 if No (Par_Spec) then
2979 Par_Lib := Empty;
2980 else
2981 Par_Lib := Defining_Entity (Unit (Par_Spec));
2982 end if;
2983 end if;
2985 -- Loop through context items
2987 Item := First (Context_Items (N));
2988 while Present (Item) loop
2990 -- Ada 2005 (AI-262): Allow private_with of a private child package
2991 -- in public siblings
2993 if Nkind (Item) = N_With_Clause
2994 and then not Implicit_With (Item)
2995 and then not Limited_Present (Item)
2996 and then Is_Private_Descendant (Entity (Name (Item)))
2997 then
2998 Priv_Child := Entity (Name (Item));
3000 declare
3001 Curr_Parent : Entity_Id := Par_Lib;
3002 Child_Parent : Entity_Id := Scope (Priv_Child);
3003 Prv_Ancestor : Entity_Id := Child_Parent;
3004 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
3006 begin
3007 -- If the child unit is a public child then locate the nearest
3008 -- private ancestor. Child_Parent will then be set to the
3009 -- parent of that ancestor.
3011 if not Is_Private_Library_Unit (Priv_Child) then
3012 while Present (Prv_Ancestor)
3013 and then not Is_Private_Library_Unit (Prv_Ancestor)
3014 loop
3015 Prv_Ancestor := Scope (Prv_Ancestor);
3016 end loop;
3018 if Present (Prv_Ancestor) then
3019 Child_Parent := Scope (Prv_Ancestor);
3020 end if;
3021 end if;
3023 while Present (Curr_Parent)
3024 and then Curr_Parent /= Standard_Standard
3025 and then Curr_Parent /= Child_Parent
3026 loop
3027 Curr_Private :=
3028 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
3029 Curr_Parent := Scope (Curr_Parent);
3030 end loop;
3032 if No (Curr_Parent) then
3033 Curr_Parent := Standard_Standard;
3034 end if;
3036 if Curr_Parent /= Child_Parent then
3037 if Ekind (Priv_Child) = E_Generic_Package
3038 and then Chars (Priv_Child) in Text_IO_Package_Name
3039 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
3040 and then Scope (Scope (Scope (Priv_Child))) =
3041 Standard_Standard
3042 then
3043 Error_Msg_NE
3044 ("& is a nested package, not a compilation unit",
3045 Name (Item), Priv_Child);
3047 else
3048 Error_Msg_N
3049 ("unit in with clause is private child unit!", Item);
3050 Error_Msg_NE
3051 ("\current unit must also have parent&!",
3052 Item, Child_Parent);
3053 end if;
3055 elsif Curr_Private
3056 or else Private_Present (Item)
3057 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
3058 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3059 and then not Acts_As_Spec (Parent (Lib_Unit)))
3060 then
3061 null;
3063 else
3064 Error_Msg_NE
3065 ("current unit must also be private descendant of&",
3066 Item, Child_Parent);
3067 end if;
3068 end;
3069 end if;
3071 Next (Item);
3072 end loop;
3073 end Check_Private_Child_Unit;
3075 ----------------------
3076 -- Check_Stub_Level --
3077 ----------------------
3079 procedure Check_Stub_Level (N : Node_Id) is
3080 Par : constant Node_Id := Parent (N);
3081 Kind : constant Node_Kind := Nkind (Par);
3083 begin
3084 if Nkind_In (Kind, N_Package_Body,
3085 N_Subprogram_Body,
3086 N_Task_Body,
3087 N_Protected_Body)
3088 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
3089 then
3090 null;
3092 -- In an instance, a missing stub appears at any level. A warning
3093 -- message will have been emitted already for the missing file.
3095 elsif not In_Instance then
3096 Error_Msg_N ("stub cannot appear in an inner scope", N);
3098 elsif Expander_Active then
3099 Error_Msg_N ("missing proper body", N);
3100 end if;
3101 end Check_Stub_Level;
3103 ------------------------
3104 -- Expand_With_Clause --
3105 ------------------------
3107 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
3108 Loc : constant Source_Ptr := Sloc (Nam);
3110 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
3111 -- Build name to be used in implicit with_clause. In most cases this
3112 -- is the source name, but if renamings are present we must make the
3113 -- original unit visible, not the one it renames. The entity in the
3114 -- with clause is the renamed unit, but the identifier is the one from
3115 -- the source, which allows us to recover the unit renaming.
3117 ---------------------
3118 -- Build_Unit_Name --
3119 ---------------------
3121 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
3122 Ent : Entity_Id;
3123 Result : Node_Id;
3125 begin
3126 if Nkind (Nam) = N_Identifier then
3127 return New_Occurrence_Of (Entity (Nam), Loc);
3129 else
3130 Ent := Entity (Nam);
3132 if Present (Entity (Selector_Name (Nam)))
3133 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
3134 and then
3135 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam)))) =
3136 N_Package_Renaming_Declaration
3137 then
3138 -- The name in the with_clause is of the form A.B.C, and B is
3139 -- given by a renaming declaration. In that case we may not
3140 -- have analyzed the unit for B, but replaced it directly in
3141 -- lib-load with the unit it renames. We have to make A.B
3142 -- visible, so analyze the declaration for B now, in case it
3143 -- has not been done yet.
3145 Ent := Entity (Selector_Name (Nam));
3146 Analyze
3147 (Parent
3148 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3149 end if;
3151 Result :=
3152 Make_Expanded_Name (Loc,
3153 Chars => Chars (Entity (Nam)),
3154 Prefix => Build_Unit_Name (Prefix (Nam)),
3155 Selector_Name => New_Occurrence_Of (Ent, Loc));
3156 Set_Entity (Result, Ent);
3158 return Result;
3159 end if;
3160 end Build_Unit_Name;
3162 -- Local variables
3164 Ent : constant Entity_Id := Entity (Nam);
3165 Withn : Node_Id;
3167 -- Start of processing for Expand_With_Clause
3169 begin
3170 Withn :=
3171 Make_With_Clause (Loc,
3172 Name => Build_Unit_Name (Nam));
3174 Set_Corresponding_Spec (Withn, Ent);
3175 Set_First_Name (Withn);
3176 Set_Implicit_With (Withn);
3177 Set_Library_Unit (Withn, Parent (Unit_Declaration_Node (Ent)));
3178 Set_Parent_With (Withn);
3180 -- If the unit is a package or generic package declaration, a private_
3181 -- with_clause on a child unit implies that the implicit with on the
3182 -- parent is also private.
3184 if Nkind_In (Unit (N), N_Generic_Package_Declaration,
3185 N_Package_Declaration)
3186 then
3187 Set_Private_Present (Withn, Private_Present (Item));
3188 end if;
3190 Prepend (Withn, Context_Items (N));
3191 Mark_Rewrite_Insertion (Withn);
3193 Install_With_Clause (Withn);
3195 -- If we have "with X.Y;", we want to recurse on "X", except in the
3196 -- unusual case where X.Y is a renaming of X. In that case, the scope
3197 -- of X will be null.
3199 if Nkind (Nam) = N_Expanded_Name
3200 and then Present (Scope (Entity (Prefix (Nam))))
3201 then
3202 Expand_With_Clause (Item, Prefix (Nam), N);
3203 end if;
3204 end Expand_With_Clause;
3206 --------------------------------
3207 -- Generate_Parent_References --
3208 --------------------------------
3210 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
3211 Pref : Node_Id;
3212 P_Name : Entity_Id := P_Id;
3214 begin
3215 if Nkind (N) = N_Subunit then
3216 Pref := Name (N);
3217 else
3218 Pref := Name (Parent (Defining_Entity (N)));
3219 end if;
3221 if Nkind (Pref) = N_Expanded_Name then
3223 -- Done already, if the unit has been compiled indirectly as
3224 -- part of the closure of its context because of inlining.
3226 return;
3227 end if;
3229 while Nkind (Pref) = N_Selected_Component loop
3230 Change_Selected_Component_To_Expanded_Name (Pref);
3231 Set_Entity (Pref, P_Name);
3232 Set_Etype (Pref, Etype (P_Name));
3233 Generate_Reference (P_Name, Pref, 'r');
3234 Pref := Prefix (Pref);
3235 P_Name := Scope (P_Name);
3236 end loop;
3238 -- The guard here on P_Name is to handle the error condition where
3239 -- the parent unit is missing because the file was not found.
3241 if Present (P_Name) then
3242 Set_Entity (Pref, P_Name);
3243 Set_Etype (Pref, Etype (P_Name));
3244 Generate_Reference (P_Name, Pref, 'r');
3245 Style.Check_Identifier (Pref, P_Name);
3246 end if;
3247 end Generate_Parent_References;
3249 ---------------------
3250 -- Has_With_Clause --
3251 ---------------------
3253 function Has_With_Clause
3254 (C_Unit : Node_Id;
3255 Pack : Entity_Id;
3256 Is_Limited : Boolean := False) return Boolean
3258 Item : Node_Id;
3260 function Named_Unit (Clause : Node_Id) return Entity_Id;
3261 -- Return the entity for the unit named in a [limited] with clause
3263 ----------------
3264 -- Named_Unit --
3265 ----------------
3267 function Named_Unit (Clause : Node_Id) return Entity_Id is
3268 begin
3269 if Nkind (Name (Clause)) = N_Selected_Component then
3270 return Entity (Selector_Name (Name (Clause)));
3271 else
3272 return Entity (Name (Clause));
3273 end if;
3274 end Named_Unit;
3276 -- Start of processing for Has_With_Clause
3278 begin
3279 if Present (Context_Items (C_Unit)) then
3280 Item := First (Context_Items (C_Unit));
3281 while Present (Item) loop
3282 if Nkind (Item) = N_With_Clause
3283 and then Limited_Present (Item) = Is_Limited
3284 and then Named_Unit (Item) = Pack
3285 then
3286 return True;
3287 end if;
3289 Next (Item);
3290 end loop;
3291 end if;
3293 return False;
3294 end Has_With_Clause;
3296 -----------------------------
3297 -- Implicit_With_On_Parent --
3298 -----------------------------
3300 procedure Implicit_With_On_Parent
3301 (Child_Unit : Node_Id;
3302 N : Node_Id)
3304 Loc : constant Source_Ptr := Sloc (N);
3305 P : constant Node_Id := Parent_Spec (Child_Unit);
3306 P_Unit : Node_Id := Unit (P);
3307 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3308 Withn : Node_Id;
3310 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3311 -- Build prefix of child unit name. Recurse if needed
3313 function Build_Unit_Name return Node_Id;
3314 -- If the unit is a child unit, build qualified name with all ancestors
3316 -------------------------
3317 -- Build_Ancestor_Name --
3318 -------------------------
3320 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3321 P_Ref : constant Node_Id :=
3322 New_Occurrence_Of (Defining_Entity (P), Loc);
3323 P_Spec : Node_Id := P;
3325 begin
3326 -- Ancestor may have been rewritten as a package body. Retrieve the
3327 -- original spec to trace earlier ancestors.
3329 if Nkind (P) = N_Package_Body
3330 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3331 then
3332 P_Spec := Original_Node (P);
3333 end if;
3335 if No (Parent_Spec (P_Spec)) then
3336 return P_Ref;
3337 else
3338 return
3339 Make_Selected_Component (Loc,
3340 Prefix =>
3341 Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3342 Selector_Name => P_Ref);
3343 end if;
3344 end Build_Ancestor_Name;
3346 ---------------------
3347 -- Build_Unit_Name --
3348 ---------------------
3350 function Build_Unit_Name return Node_Id is
3351 Result : Node_Id;
3353 begin
3354 if No (Parent_Spec (P_Unit)) then
3355 return New_Occurrence_Of (P_Name, Loc);
3357 else
3358 Result :=
3359 Make_Expanded_Name (Loc,
3360 Chars => Chars (P_Name),
3361 Prefix =>
3362 Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3363 Selector_Name => New_Occurrence_Of (P_Name, Loc));
3364 Set_Entity (Result, P_Name);
3366 return Result;
3367 end if;
3368 end Build_Unit_Name;
3370 -- Start of processing for Implicit_With_On_Parent
3372 begin
3373 -- The unit of the current compilation may be a package body that
3374 -- replaces an instance node. In this case we need the original instance
3375 -- node to construct the proper parent name.
3377 if Nkind (P_Unit) = N_Package_Body
3378 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3379 then
3380 P_Unit := Original_Node (P_Unit);
3381 end if;
3383 -- We add the implicit with if the child unit is the current unit being
3384 -- compiled. If the current unit is a body, we do not want to add an
3385 -- implicit_with a second time to the corresponding spec.
3387 if Nkind (Child_Unit) = N_Package_Declaration
3388 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3389 then
3390 return;
3391 end if;
3393 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3395 Set_Corresponding_Spec (Withn, P_Name);
3396 Set_First_Name (Withn);
3397 Set_Implicit_With (Withn);
3398 Set_Library_Unit (Withn, P);
3399 Set_Parent_With (Withn);
3401 -- Node is placed at the beginning of the context items, so that
3402 -- subsequent use clauses on the parent can be validated.
3404 Prepend (Withn, Context_Items (N));
3405 Mark_Rewrite_Insertion (Withn);
3407 Install_With_Clause (Withn);
3409 if Is_Child_Spec (P_Unit) then
3410 Implicit_With_On_Parent (P_Unit, N);
3411 end if;
3412 end Implicit_With_On_Parent;
3414 --------------
3415 -- In_Chain --
3416 --------------
3418 function In_Chain (E : Entity_Id) return Boolean is
3419 H : Entity_Id;
3421 begin
3422 H := Current_Entity (E);
3423 while Present (H) loop
3424 if H = E then
3425 return True;
3426 else
3427 H := Homonym (H);
3428 end if;
3429 end loop;
3431 return False;
3432 end In_Chain;
3434 ---------------------
3435 -- Install_Context --
3436 ---------------------
3438 procedure Install_Context (N : Node_Id; Chain : Boolean := True) is
3439 Lib_Unit : constant Node_Id := Unit (N);
3441 begin
3442 Install_Context_Clauses (N, Chain);
3444 if Is_Child_Spec (Lib_Unit) then
3445 Install_Parents
3446 (Lib_Unit => Lib_Unit,
3447 Is_Private => Private_Present (Parent (Lib_Unit)),
3448 Chain => Chain);
3449 end if;
3451 Install_Limited_Context_Clauses (N);
3452 end Install_Context;
3454 -----------------------------
3455 -- Install_Context_Clauses --
3456 -----------------------------
3458 procedure Install_Context_Clauses (N : Node_Id; Chain : Boolean := True) is
3459 Lib_Unit : constant Node_Id := Unit (N);
3460 Item : Node_Id;
3461 Uname_Node : Entity_Id;
3462 Check_Private : Boolean := False;
3463 Decl_Node : Node_Id;
3464 Lib_Parent : Entity_Id;
3466 begin
3467 -- First skip configuration pragmas at the start of the context. They
3468 -- are not technically part of the context clause, but that's where the
3469 -- parser puts them. Note they were analyzed in Analyze_Context.
3471 Item := First (Context_Items (N));
3472 while Present (Item)
3473 and then Nkind (Item) = N_Pragma
3474 and then Pragma_Name (Item) in Configuration_Pragma_Names
3475 loop
3476 Next (Item);
3477 end loop;
3479 -- Loop through the actual context clause items. We process everything
3480 -- except Limited_With clauses in this routine. Limited_With clauses
3481 -- are separately installed (see Install_Limited_Context_Clauses).
3483 while Present (Item) loop
3485 -- Case of explicit WITH clause
3487 if Nkind (Item) = N_With_Clause
3488 and then not Implicit_With (Item)
3489 then
3490 if Limited_Present (Item) then
3492 -- Limited withed units will be installed later
3494 goto Continue;
3496 -- If Name (Item) is not an entity name, something is wrong, and
3497 -- this will be detected in due course, for now ignore the item
3499 elsif not Is_Entity_Name (Name (Item)) then
3500 goto Continue;
3502 elsif No (Entity (Name (Item))) then
3503 Set_Entity (Name (Item), Any_Id);
3504 goto Continue;
3505 end if;
3507 Uname_Node := Entity (Name (Item));
3509 if Is_Private_Descendant (Uname_Node) then
3510 Check_Private := True;
3511 end if;
3513 Install_With_Clause (Item);
3515 Decl_Node := Unit_Declaration_Node (Uname_Node);
3517 -- If the unit is a subprogram instance, it appears nested within
3518 -- a package that carries the parent information.
3520 if Is_Generic_Instance (Uname_Node)
3521 and then Ekind (Uname_Node) /= E_Package
3522 then
3523 Decl_Node := Parent (Parent (Decl_Node));
3524 end if;
3526 if Is_Child_Spec (Decl_Node) then
3527 if Nkind (Name (Item)) = N_Expanded_Name then
3528 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3529 else
3530 -- If not an expanded name, the child unit must be a
3531 -- renaming, nothing to do.
3533 null;
3534 end if;
3536 elsif Nkind (Decl_Node) = N_Subprogram_Body
3537 and then not Acts_As_Spec (Parent (Decl_Node))
3538 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3539 then
3540 Implicit_With_On_Parent
3541 (Unit (Library_Unit (Parent (Decl_Node))), N);
3542 end if;
3544 -- Check license conditions unless this is a dummy unit
3546 if Sloc (Library_Unit (Item)) /= No_Location then
3547 License_Check : declare
3548 Withu : constant Unit_Number_Type :=
3549 Get_Source_Unit (Library_Unit (Item));
3550 Withl : constant License_Type :=
3551 License (Source_Index (Withu));
3552 Unitl : constant License_Type :=
3553 License (Source_Index (Current_Sem_Unit));
3555 procedure License_Error;
3556 -- Signal error of bad license
3558 -------------------
3559 -- License_Error --
3560 -------------------
3562 procedure License_Error is
3563 begin
3564 Error_Msg_N
3565 ("license of withed unit & may be inconsistent??",
3566 Name (Item));
3567 end License_Error;
3569 -- Start of processing for License_Check
3571 begin
3572 -- Exclude license check if withed unit is an internal unit.
3573 -- This situation arises e.g. with the GPL version of GNAT.
3575 if Is_Internal_Unit (Withu) then
3576 null;
3578 -- Otherwise check various cases
3579 else
3580 case Unitl is
3581 when Unknown =>
3582 null;
3584 when Restricted =>
3585 if Withl = GPL then
3586 License_Error;
3587 end if;
3589 when GPL =>
3590 if Withl = Restricted then
3591 License_Error;
3592 end if;
3594 when Modified_GPL =>
3595 if Withl = Restricted or else Withl = GPL then
3596 License_Error;
3597 end if;
3599 when Unrestricted =>
3600 null;
3601 end case;
3602 end if;
3603 end License_Check;
3604 end if;
3606 -- Case of USE PACKAGE clause
3608 elsif Nkind (Item) = N_Use_Package_Clause then
3609 Analyze_Use_Package (Item, Chain);
3611 -- Case of USE TYPE clause
3613 elsif Nkind (Item) = N_Use_Type_Clause then
3614 Analyze_Use_Type (Item, Chain);
3616 -- case of PRAGMA
3618 elsif Nkind (Item) = N_Pragma then
3619 Analyze (Item);
3620 end if;
3622 <<Continue>>
3623 Next (Item);
3624 end loop;
3626 if Is_Child_Spec (Lib_Unit) then
3628 -- The unit also has implicit with_clauses on its own parents
3630 if No (Context_Items (N)) then
3631 Set_Context_Items (N, New_List);
3632 end if;
3634 Implicit_With_On_Parent (Lib_Unit, N);
3635 end if;
3637 -- If the unit is a body, the context of the specification must also
3638 -- be installed. That includes private with_clauses in that context.
3640 if Nkind (Lib_Unit) = N_Package_Body
3641 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3642 and then not Acts_As_Spec (N))
3643 then
3644 Install_Context (Library_Unit (N), Chain);
3646 -- Only install private with-clauses of a spec that comes from
3647 -- source, excluding specs created for a subprogram body that is
3648 -- a child unit.
3650 if Comes_From_Source (Library_Unit (N)) then
3651 Install_Private_With_Clauses
3652 (Defining_Entity (Unit (Library_Unit (N))));
3653 end if;
3655 if Is_Child_Spec (Unit (Library_Unit (N))) then
3657 -- If the unit is the body of a public child unit, the private
3658 -- declarations of the parent must be made visible. If the child
3659 -- unit is private, the private declarations have been installed
3660 -- already in the call to Install_Parents for the spec. Installing
3661 -- private declarations must be done for all ancestors of public
3662 -- child units. In addition, sibling units mentioned in the
3663 -- context clause of the body are directly visible.
3665 declare
3666 Lib_Spec : Node_Id;
3667 P : Node_Id;
3668 P_Name : Entity_Id;
3670 begin
3671 Lib_Spec := Unit (Library_Unit (N));
3672 while Is_Child_Spec (Lib_Spec) loop
3673 P := Unit (Parent_Spec (Lib_Spec));
3674 P_Name := Defining_Entity (P);
3676 if not (Private_Present (Parent (Lib_Spec)))
3677 and then not In_Private_Part (P_Name)
3678 then
3679 Install_Private_Declarations (P_Name);
3680 Install_Private_With_Clauses (P_Name);
3681 Set_Use (Private_Declarations (Specification (P)));
3682 end if;
3684 Lib_Spec := P;
3685 end loop;
3686 end;
3687 end if;
3689 -- For a package body, children in context are immediately visible
3691 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3692 end if;
3694 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3695 N_Generic_Subprogram_Declaration,
3696 N_Package_Declaration,
3697 N_Subprogram_Declaration)
3698 then
3699 if Is_Child_Spec (Lib_Unit) then
3700 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3701 Set_Is_Private_Descendant
3702 (Defining_Entity (Lib_Unit),
3703 Is_Private_Descendant (Lib_Parent)
3704 or else Private_Present (Parent (Lib_Unit)));
3706 else
3707 Set_Is_Private_Descendant
3708 (Defining_Entity (Lib_Unit),
3709 Private_Present (Parent (Lib_Unit)));
3710 end if;
3711 end if;
3713 if Check_Private then
3714 Check_Private_Child_Unit (N);
3715 end if;
3716 end Install_Context_Clauses;
3718 -------------------------------------
3719 -- Install_Limited_Context_Clauses --
3720 -------------------------------------
3722 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3723 Item : Node_Id;
3725 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3726 -- Check that the unlimited view of a given compilation_unit is not
3727 -- already visible through "use + renamings".
3729 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3730 -- Check that if a limited_with clause of a given compilation_unit
3731 -- mentions a descendant of a private child of some library unit, then
3732 -- the given compilation_unit must be the declaration of a private
3733 -- descendant of that library unit, or a public descendant of such. The
3734 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3735 -- use entities on the limited with_clauses because their units have not
3736 -- been analyzed, so we have to climb the tree of ancestors looking for
3737 -- private keywords.
3739 procedure Expand_Limited_With_Clause
3740 (Comp_Unit : Node_Id;
3741 Nam : Node_Id;
3742 N : Node_Id);
3743 -- If a child unit appears in a limited_with clause, there are implicit
3744 -- limited_with clauses on all parents that are not already visible
3745 -- through a regular with clause. This procedure creates the implicit
3746 -- limited with_clauses for the parents and loads the corresponding
3747 -- units. The shadow entities are created when the inserted clause is
3748 -- analyzed. Implements Ada 2005 (AI-50217).
3750 ---------------------
3751 -- Check_Renamings --
3752 ---------------------
3754 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3755 Item : Node_Id;
3756 Spec : Node_Id;
3757 WEnt : Entity_Id;
3758 E : Entity_Id;
3759 E2 : Entity_Id;
3761 begin
3762 pragma Assert (Nkind (W) = N_With_Clause);
3764 -- Protect the frontend against previous critical errors
3766 case Nkind (Unit (Library_Unit (W))) is
3767 when N_Generic_Package_Declaration
3768 | N_Generic_Subprogram_Declaration
3769 | N_Package_Declaration
3770 | N_Subprogram_Declaration
3772 null;
3774 when others =>
3775 return;
3776 end case;
3778 -- Check "use + renamings"
3780 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3781 Spec := Specification (Unit (P));
3783 Item := First (Visible_Declarations (Spec));
3784 while Present (Item) loop
3786 -- Look only at use package clauses
3788 if Nkind (Item) = N_Use_Package_Clause then
3790 E := Entity (Name (Item));
3792 pragma Assert (Present (Parent (E)));
3794 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3795 and then Renamed_Entity (E) = WEnt
3796 then
3797 -- The unlimited view is visible through use clause and
3798 -- renamings. There is no need to generate the error
3799 -- message here because Is_Visible_Through_Renamings
3800 -- takes care of generating the precise error message.
3802 return;
3804 elsif Nkind (Parent (E)) = N_Package_Specification then
3806 -- The use clause may refer to a local package.
3807 -- Check all the enclosing scopes.
3809 E2 := E;
3810 while E2 /= Standard_Standard and then E2 /= WEnt loop
3811 E2 := Scope (E2);
3812 end loop;
3814 if E2 = WEnt then
3815 Error_Msg_N
3816 ("unlimited view visible through use clause ", W);
3817 return;
3818 end if;
3819 end if;
3820 end if;
3822 Next (Item);
3823 end loop;
3825 -- Recursive call to check all the ancestors
3827 if Is_Child_Spec (Unit (P)) then
3828 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3829 end if;
3830 end Check_Renamings;
3832 ---------------------------------------
3833 -- Check_Private_Limited_Withed_Unit --
3834 ---------------------------------------
3836 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3837 Curr_Parent : Node_Id;
3838 Child_Parent : Node_Id;
3839 Curr_Private : Boolean;
3841 begin
3842 -- Compilation unit of the parent of the withed library unit
3844 Child_Parent := Library_Unit (Item);
3846 -- If the child unit is a public child, then locate its nearest
3847 -- private ancestor, if any, then Child_Parent will then be set to
3848 -- the parent of that ancestor.
3850 if not Private_Present (Library_Unit (Item)) then
3851 while Present (Child_Parent)
3852 and then not Private_Present (Child_Parent)
3853 loop
3854 Child_Parent := Parent_Spec (Unit (Child_Parent));
3855 end loop;
3857 if No (Child_Parent) then
3858 return;
3859 end if;
3860 end if;
3862 Child_Parent := Parent_Spec (Unit (Child_Parent));
3864 -- Traverse all the ancestors of the current compilation unit to
3865 -- check if it is a descendant of named library unit.
3867 Curr_Parent := Parent (Item);
3868 Curr_Private := Private_Present (Curr_Parent);
3870 while Present (Parent_Spec (Unit (Curr_Parent)))
3871 and then Curr_Parent /= Child_Parent
3872 loop
3873 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3874 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3875 end loop;
3877 if Curr_Parent /= Child_Parent then
3878 Error_Msg_N
3879 ("unit in with clause is private child unit!", Item);
3880 Error_Msg_NE
3881 ("\current unit must also have parent&!",
3882 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3884 elsif Private_Present (Parent (Item))
3885 or else Curr_Private
3886 or else Private_Present (Item)
3887 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3888 N_Subprogram_Body,
3889 N_Subunit)
3890 then
3891 -- Current unit is private, of descendant of a private unit
3893 null;
3895 else
3896 Error_Msg_NE
3897 ("current unit must also be private descendant of&",
3898 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3899 end if;
3900 end Check_Private_Limited_Withed_Unit;
3902 --------------------------------
3903 -- Expand_Limited_With_Clause --
3904 --------------------------------
3906 procedure Expand_Limited_With_Clause
3907 (Comp_Unit : Node_Id;
3908 Nam : Node_Id;
3909 N : Node_Id)
3911 Loc : constant Source_Ptr := Sloc (Nam);
3912 Unum : Unit_Number_Type;
3913 Withn : Node_Id;
3915 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3916 -- Returns true if the context already includes a with_clause for
3917 -- this unit. If the with_clause is nonlimited, the unit is fully
3918 -- visible and an implicit limited_with should not be created. If
3919 -- there is already a limited_with clause for W, a second one is
3920 -- simply redundant.
3922 --------------------------
3923 -- Previous_Withed_Unit --
3924 --------------------------
3926 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3927 Item : Node_Id;
3929 begin
3930 -- A limited with_clause cannot appear in the same context_clause
3931 -- as a nonlimited with_clause which mentions the same library.
3933 Item := First (Context_Items (Comp_Unit));
3934 while Present (Item) loop
3935 if Nkind (Item) = N_With_Clause
3936 and then Library_Unit (Item) = Library_Unit (W)
3937 then
3938 return True;
3939 end if;
3941 Next (Item);
3942 end loop;
3944 return False;
3945 end Previous_Withed_Unit;
3947 -- Start of processing for Expand_Limited_With_Clause
3949 begin
3950 if Nkind (Nam) = N_Identifier then
3952 -- Create node for name of withed unit
3954 Withn :=
3955 Make_With_Clause (Loc,
3956 Name => New_Copy (Nam));
3958 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3959 Withn :=
3960 Make_With_Clause (Loc,
3961 Name => Make_Selected_Component (Loc,
3962 Prefix => New_Copy_Tree (Prefix (Nam)),
3963 Selector_Name => New_Copy (Selector_Name (Nam))));
3964 Set_Parent (Withn, Parent (N));
3965 end if;
3967 Set_First_Name (Withn);
3968 Set_Implicit_With (Withn);
3969 Set_Limited_Present (Withn);
3971 Unum :=
3972 Load_Unit
3973 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3974 Required => True,
3975 Subunit => False,
3976 Error_Node => Nam);
3978 -- Do not generate a limited_with_clause on the current unit. This
3979 -- path is taken when a unit has a limited_with clause on one of its
3980 -- child units.
3982 if Unum = Current_Sem_Unit then
3983 return;
3984 end if;
3986 Set_Library_Unit (Withn, Cunit (Unum));
3987 Set_Corresponding_Spec
3988 (Withn, Specification (Unit (Cunit (Unum))));
3990 if not Previous_Withed_Unit (Withn) then
3991 Prepend (Withn, Context_Items (Parent (N)));
3992 Mark_Rewrite_Insertion (Withn);
3994 -- Add implicit limited_with_clauses for parents of child units
3995 -- mentioned in limited_with clauses.
3997 if Nkind (Nam) = N_Selected_Component then
3998 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3999 end if;
4001 Analyze (Withn);
4003 if not Limited_View_Installed (Withn) then
4004 Install_Limited_With_Clause (Withn);
4005 end if;
4006 end if;
4007 end Expand_Limited_With_Clause;
4009 -- Start of processing for Install_Limited_Context_Clauses
4011 begin
4012 Item := First (Context_Items (N));
4013 while Present (Item) loop
4014 if Nkind (Item) = N_With_Clause
4015 and then Limited_Present (Item)
4016 and then not Error_Posted (Item)
4017 then
4018 if Nkind (Name (Item)) = N_Selected_Component then
4019 Expand_Limited_With_Clause
4020 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
4021 end if;
4023 Check_Private_Limited_Withed_Unit (Item);
4025 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
4026 Check_Renamings (Parent_Spec (Unit (N)), Item);
4027 end if;
4029 -- A unit may have a limited with on itself if it has a limited
4030 -- with_clause on one of its child units. In that case it is
4031 -- already being compiled and it makes no sense to install its
4032 -- limited view.
4034 -- If the item is a limited_private_with_clause, install it if the
4035 -- current unit is a body or if it is a private child. Otherwise
4036 -- the private clause is installed before analyzing the private
4037 -- part of the current unit.
4039 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
4040 and then not Limited_View_Installed (Item)
4041 and then
4042 not Is_Ancestor_Unit
4043 (Library_Unit (Item), Cunit (Current_Sem_Unit))
4044 then
4045 if not Private_Present (Item)
4046 or else Private_Present (N)
4047 or else Nkind_In (Unit (N), N_Package_Body,
4048 N_Subprogram_Body,
4049 N_Subunit)
4050 then
4051 Install_Limited_With_Clause (Item);
4052 end if;
4053 end if;
4054 end if;
4056 Next (Item);
4057 end loop;
4059 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4060 -- looking for incomplete subtype declarations of incomplete types
4061 -- visible through a limited with clause.
4063 if Ada_Version >= Ada_2005
4064 and then Analyzed (N)
4065 and then Nkind (Unit (N)) = N_Package_Declaration
4066 then
4067 declare
4068 Decl : Node_Id;
4069 Def_Id : Entity_Id;
4070 Non_Lim_View : Entity_Id;
4072 begin
4073 Decl := First (Visible_Declarations (Specification (Unit (N))));
4074 while Present (Decl) loop
4075 if Nkind (Decl) = N_Subtype_Declaration
4076 and then
4077 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
4078 and then
4079 From_Limited_With (Defining_Identifier (Decl))
4080 then
4081 Def_Id := Defining_Identifier (Decl);
4082 Non_Lim_View := Non_Limited_View (Def_Id);
4084 if not Is_Incomplete_Type (Non_Lim_View) then
4086 -- Convert an incomplete subtype declaration into a
4087 -- corresponding nonlimited view subtype declaration.
4088 -- This is usually the case when analyzing a body that
4089 -- has regular with clauses, when the spec has limited
4090 -- ones.
4092 -- If the nonlimited view is still incomplete, it is
4093 -- the dummy entry already created, and the declaration
4094 -- cannot be reanalyzed. This is the case when installing
4095 -- a parent unit that has limited with-clauses.
4097 Set_Subtype_Indication (Decl,
4098 New_Occurrence_Of (Non_Lim_View, Sloc (Def_Id)));
4099 Set_Etype (Def_Id, Non_Lim_View);
4100 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
4101 Set_Analyzed (Decl, False);
4103 -- Reanalyze the declaration, suppressing the call to
4104 -- Enter_Name to avoid duplicate names.
4106 Analyze_Subtype_Declaration
4107 (N => Decl,
4108 Skip => True);
4109 end if;
4110 end if;
4112 Next (Decl);
4113 end loop;
4114 end;
4115 end if;
4116 end Install_Limited_Context_Clauses;
4118 ---------------------
4119 -- Install_Parents --
4120 ---------------------
4122 procedure Install_Parents
4123 (Lib_Unit : Node_Id;
4124 Is_Private : Boolean;
4125 Chain : Boolean := True)
4127 P : Node_Id;
4128 E_Name : Entity_Id;
4129 P_Name : Entity_Id;
4130 P_Spec : Node_Id;
4132 begin
4133 P := Unit (Parent_Spec (Lib_Unit));
4134 P_Name := Get_Parent_Entity (P);
4136 if Etype (P_Name) = Any_Type then
4137 return;
4138 end if;
4140 if Ekind (P_Name) = E_Generic_Package
4141 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
4142 N_Generic_Package_Declaration)
4143 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
4144 then
4145 Error_Msg_N
4146 ("child of a generic package must be a generic unit", Lib_Unit);
4148 elsif not Is_Package_Or_Generic_Package (P_Name) then
4149 Error_Msg_N
4150 ("parent unit must be package or generic package", Lib_Unit);
4151 raise Unrecoverable_Error;
4153 elsif Present (Renamed_Object (P_Name)) then
4154 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
4155 raise Unrecoverable_Error;
4157 -- Verify that a child of an instance is itself an instance, or the
4158 -- renaming of one. Given that an instance that is a unit is replaced
4159 -- with a package declaration, check against the original node. The
4160 -- parent may be currently being instantiated, in which case it appears
4161 -- as a declaration, but the generic_parent is already established
4162 -- indicating that we deal with an instance.
4164 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
4165 if Nkind (Lib_Unit) in N_Renaming_Declaration
4166 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
4167 or else
4168 (Nkind (Lib_Unit) = N_Package_Declaration
4169 and then Present (Generic_Parent (Specification (Lib_Unit))))
4170 then
4171 null;
4172 else
4173 Error_Msg_N
4174 ("child of an instance must be an instance or renaming",
4175 Lib_Unit);
4176 end if;
4177 end if;
4179 -- This is the recursive call that ensures all parents are loaded
4181 if Is_Child_Spec (P) then
4182 Install_Parents
4183 (Lib_Unit => P,
4184 Is_Private =>
4185 Is_Private or else Private_Present (Parent (Lib_Unit)),
4186 Chain => Chain);
4187 end if;
4189 -- Now we can install the context for this parent
4191 Install_Context_Clauses (Parent_Spec (Lib_Unit), Chain);
4192 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4193 Install_Siblings (P_Name, Parent (Lib_Unit));
4195 -- The child unit is in the declarative region of the parent. The parent
4196 -- must therefore appear in the scope stack and be visible, as when
4197 -- compiling the corresponding body. If the child unit is private or it
4198 -- is a package body, private declarations must be accessible as well.
4199 -- Use declarations in the parent must also be installed. Finally, other
4200 -- child units of the same parent that are in the context are
4201 -- immediately visible.
4203 -- Find entity for compilation unit, and set its private descendant
4204 -- status as needed. Indicate that it is a compilation unit, which is
4205 -- redundant in general, but needed if this is a generated child spec
4206 -- for a child body without previous spec.
4208 E_Name := Defining_Entity (Lib_Unit);
4210 Set_Is_Child_Unit (E_Name);
4211 Set_Is_Compilation_Unit (E_Name);
4213 Set_Is_Private_Descendant (E_Name,
4214 Is_Private_Descendant (P_Name)
4215 or else Private_Present (Parent (Lib_Unit)));
4217 P_Spec := Package_Specification (P_Name);
4218 Push_Scope (P_Name);
4220 -- Save current visibility of unit
4222 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4223 Is_Immediately_Visible (P_Name);
4224 Set_Is_Immediately_Visible (P_Name);
4225 Install_Visible_Declarations (P_Name);
4226 Set_Use (Visible_Declarations (P_Spec));
4228 -- If the parent is a generic unit, its formal part may contain formal
4229 -- packages and use clauses for them.
4231 if Ekind (P_Name) = E_Generic_Package then
4232 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4233 end if;
4235 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4236 Install_Private_Declarations (P_Name);
4237 Install_Private_With_Clauses (P_Name);
4238 Set_Use (Private_Declarations (P_Spec));
4239 end if;
4240 end Install_Parents;
4242 ----------------------------------
4243 -- Install_Private_With_Clauses --
4244 ----------------------------------
4246 procedure Install_Private_With_Clauses (P : Entity_Id) is
4247 Decl : constant Node_Id := Unit_Declaration_Node (P);
4248 Item : Node_Id;
4250 begin
4251 if Debug_Flag_I then
4252 Write_Str ("install private with clauses of ");
4253 Write_Name (Chars (P));
4254 Write_Eol;
4255 end if;
4257 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4258 Item := First (Context_Items (Parent (Decl)));
4259 while Present (Item) loop
4260 if Nkind (Item) = N_With_Clause
4261 and then Private_Present (Item)
4262 then
4263 -- If the unit is an ancestor of the current one, it is the
4264 -- case of a private limited with clause on a child unit, and
4265 -- the compilation of one of its descendants, In that case the
4266 -- limited view is errelevant.
4268 if Limited_Present (Item) then
4269 if not Limited_View_Installed (Item)
4270 and then
4271 not Is_Ancestor_Unit (Library_Unit (Item),
4272 Cunit (Current_Sem_Unit))
4273 then
4274 Install_Limited_With_Clause (Item);
4275 end if;
4276 else
4277 Install_With_Clause (Item, Private_With_OK => True);
4278 end if;
4279 end if;
4281 Next (Item);
4282 end loop;
4283 end if;
4284 end Install_Private_With_Clauses;
4286 ----------------------
4287 -- Install_Siblings --
4288 ----------------------
4290 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4291 Item : Node_Id;
4292 Id : Entity_Id;
4293 Prev : Entity_Id;
4295 begin
4296 -- Iterate over explicit with clauses, and check whether the scope of
4297 -- each entity is an ancestor of the current unit, in which case it is
4298 -- immediately visible.
4300 Item := First (Context_Items (N));
4301 while Present (Item) loop
4303 -- Do not install private_with_clauses declaration, unless unit
4304 -- is itself a private child unit, or is a body. Note that for a
4305 -- subprogram body the private_with_clause does not take effect
4306 -- until after the specification.
4308 if Nkind (Item) /= N_With_Clause
4309 or else Implicit_With (Item)
4310 or else Limited_Present (Item)
4311 or else Error_Posted (Item)
4313 -- Skip processing malformed trees
4315 or else (Try_Semantics
4316 and then Nkind (Name (Item)) not in N_Has_Entity)
4317 then
4318 null;
4320 elsif not Private_Present (Item)
4321 or else Private_Present (N)
4322 or else Nkind (Unit (N)) = N_Package_Body
4323 then
4324 Id := Entity (Name (Item));
4326 if Is_Child_Unit (Id)
4327 and then Is_Ancestor_Package (Scope (Id), U_Name)
4328 then
4329 Set_Is_Immediately_Visible (Id);
4331 -- Check for the presence of another unit in the context that
4332 -- may be inadvertently hidden by the child.
4334 Prev := Current_Entity (Id);
4336 if Present (Prev)
4337 and then Is_Immediately_Visible (Prev)
4338 and then not Is_Child_Unit (Prev)
4339 then
4340 declare
4341 Clause : Node_Id;
4343 begin
4344 Clause := First (Context_Items (N));
4345 while Present (Clause) loop
4346 if Nkind (Clause) = N_With_Clause
4347 and then Entity (Name (Clause)) = Prev
4348 then
4349 Error_Msg_NE
4350 ("child unit& hides compilation unit " &
4351 "with the same name??",
4352 Name (Item), Id);
4353 exit;
4354 end if;
4356 Next (Clause);
4357 end loop;
4358 end;
4359 end if;
4361 -- The With_Clause may be on a grand-child or one of its further
4362 -- descendants, which makes a child immediately visible. Examine
4363 -- ancestry to determine whether such a child exists. For example,
4364 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4365 -- is immediately visible.
4367 elsif Is_Child_Unit (Id) then
4368 declare
4369 Par : Entity_Id;
4371 begin
4372 Par := Scope (Id);
4373 while Is_Child_Unit (Par) loop
4374 if Is_Ancestor_Package (Scope (Par), U_Name) then
4375 Set_Is_Immediately_Visible (Par);
4376 exit;
4377 end if;
4379 Par := Scope (Par);
4380 end loop;
4381 end;
4382 end if;
4384 -- If the item is a private with-clause on a child unit, the parent
4385 -- may have been installed already, but the child unit must remain
4386 -- invisible until installed in a private part or body, unless there
4387 -- is already a regular with_clause for it in the current unit.
4389 elsif Private_Present (Item) then
4390 Id := Entity (Name (Item));
4392 if Is_Child_Unit (Id) then
4393 declare
4394 Clause : Node_Id;
4396 function In_Context return Boolean;
4397 -- Scan context of current unit, to check whether there is
4398 -- a with_clause on the same unit as a private with-clause
4399 -- on a parent, in which case child unit is visible. If the
4400 -- unit is a grand-child, the same applies to its parent.
4402 ----------------
4403 -- In_Context --
4404 ----------------
4406 function In_Context return Boolean is
4407 begin
4408 Clause :=
4409 First (Context_Items (Cunit (Current_Sem_Unit)));
4410 while Present (Clause) loop
4411 if Nkind (Clause) = N_With_Clause
4412 and then Comes_From_Source (Clause)
4413 and then Is_Entity_Name (Name (Clause))
4414 and then not Private_Present (Clause)
4415 then
4416 if Entity (Name (Clause)) = Id
4417 or else
4418 (Nkind (Name (Clause)) = N_Expanded_Name
4419 and then Entity (Prefix (Name (Clause))) = Id)
4420 then
4421 return True;
4422 end if;
4423 end if;
4425 Next (Clause);
4426 end loop;
4428 return False;
4429 end In_Context;
4431 begin
4432 Set_Is_Visible_Lib_Unit (Id, In_Context);
4433 end;
4434 end if;
4435 end if;
4437 Next (Item);
4438 end loop;
4439 end Install_Siblings;
4441 ---------------------------------
4442 -- Install_Limited_With_Clause --
4443 ---------------------------------
4445 procedure Install_Limited_With_Clause (N : Node_Id) is
4446 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4447 E : Entity_Id;
4448 P : Entity_Id;
4449 Is_Child_Package : Boolean := False;
4450 Lim_Header : Entity_Id;
4451 Lim_Typ : Entity_Id;
4453 procedure Check_Body_Required;
4454 -- A unit mentioned in a limited with_clause may not be mentioned in
4455 -- a regular with_clause, but must still be included in the current
4456 -- partition. We need to determine whether the unit needs a body, so
4457 -- that the binder can determine the name of the file to be compiled.
4458 -- Checking whether a unit needs a body can be done without semantic
4459 -- analysis, by examining the nature of the declarations in the package.
4461 function Has_Limited_With_Clause
4462 (C_Unit : Entity_Id;
4463 Pack : Entity_Id) return Boolean;
4464 -- Determine whether any package in the ancestor chain starting with
4465 -- C_Unit has a limited with clause for package Pack.
4467 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4468 -- Check if some package installed though normal with-clauses has a
4469 -- renaming declaration of package P. AARM 10.1.2(21/2).
4471 -------------------------
4472 -- Check_Body_Required --
4473 -------------------------
4475 procedure Check_Body_Required is
4476 PA : constant List_Id :=
4477 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4479 procedure Check_Declarations (Spec : Node_Id);
4480 -- Recursive procedure that does the work and checks nested packages
4482 ------------------------
4483 -- Check_Declarations --
4484 ------------------------
4486 procedure Check_Declarations (Spec : Node_Id) is
4487 Decl : Node_Id;
4488 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4490 Subp_List : constant Elist_Id := New_Elmt_List;
4492 procedure Check_Pragma_Import (P : Node_Id);
4493 -- If a pragma import applies to a previous subprogram, the
4494 -- enclosing unit may not need a body. The processing is syntactic
4495 -- and does not require a declaration to be analyzed. The code
4496 -- below also handles pragma Import when applied to a subprogram
4497 -- that renames another. In this case the pragma applies to the
4498 -- renamed entity.
4500 -- Chains of multiple renames are not handled by the code below.
4501 -- It is probably impossible to handle all cases without proper
4502 -- name resolution. In such cases the algorithm is conservative
4503 -- and will indicate that a body is needed???
4505 -------------------------
4506 -- Check_Pragma_Import --
4507 -------------------------
4509 procedure Check_Pragma_Import (P : Node_Id) is
4510 Arg : Node_Id;
4511 Prev_Id : Elmt_Id;
4512 Subp_Id : Elmt_Id;
4513 Imported : Node_Id;
4515 procedure Remove_Homonyms (E : Node_Id);
4516 -- Make one pass over list of subprograms. Called again if
4517 -- subprogram is a renaming. E is known to be an identifier.
4519 ---------------------
4520 -- Remove_Homonyms --
4521 ---------------------
4523 procedure Remove_Homonyms (E : Node_Id) is
4524 R : Entity_Id := Empty;
4525 -- Name of renamed entity, if any
4527 begin
4528 Subp_Id := First_Elmt (Subp_List);
4529 while Present (Subp_Id) loop
4530 if Chars (Node (Subp_Id)) = Chars (E) then
4531 if Nkind (Parent (Parent (Node (Subp_Id))))
4532 /= N_Subprogram_Renaming_Declaration
4533 then
4534 Prev_Id := Subp_Id;
4535 Next_Elmt (Subp_Id);
4536 Remove_Elmt (Subp_List, Prev_Id);
4537 else
4538 R := Name (Parent (Parent (Node (Subp_Id))));
4539 exit;
4540 end if;
4541 else
4542 Next_Elmt (Subp_Id);
4543 end if;
4544 end loop;
4546 if Present (R) then
4547 if Nkind (R) = N_Identifier then
4548 Remove_Homonyms (R);
4550 elsif Nkind (R) = N_Selected_Component then
4551 Remove_Homonyms (Selector_Name (R));
4553 -- Renaming of attribute
4555 else
4556 null;
4557 end if;
4558 end if;
4559 end Remove_Homonyms;
4561 -- Start of processing for Check_Pragma_Import
4563 begin
4564 -- Find name of entity in Import pragma. We have not analyzed
4565 -- the construct, so we must guard against syntax errors.
4567 Arg := Next (First (Pragma_Argument_Associations (P)));
4569 if No (Arg)
4570 or else Nkind (Expression (Arg)) /= N_Identifier
4571 then
4572 return;
4573 else
4574 Imported := Expression (Arg);
4575 end if;
4577 Remove_Homonyms (Imported);
4578 end Check_Pragma_Import;
4580 -- Start of processing for Check_Declarations
4582 begin
4583 -- Search for Elaborate Body pragma
4585 Decl := First (Visible_Declarations (Spec));
4586 while Present (Decl)
4587 and then Nkind (Decl) = N_Pragma
4588 loop
4589 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4590 Set_Body_Required (Library_Unit (N));
4591 return;
4592 end if;
4594 Next (Decl);
4595 end loop;
4597 -- Look for declarations that require the presence of a body. We
4598 -- have already skipped pragmas at the start of the list.
4600 while Present (Decl) loop
4602 -- Subprogram that comes from source means body may be needed.
4603 -- Save for subsequent examination of import pragmas.
4605 if Comes_From_Source (Decl)
4606 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4607 N_Subprogram_Renaming_Declaration,
4608 N_Generic_Subprogram_Declaration))
4609 then
4610 Append_Elmt (Defining_Entity (Decl), Subp_List);
4612 -- Package declaration of generic package declaration. We need
4613 -- to recursively examine nested declarations.
4615 elsif Nkind_In (Decl, N_Package_Declaration,
4616 N_Generic_Package_Declaration)
4617 then
4618 Check_Declarations (Specification (Decl));
4620 elsif Nkind (Decl) = N_Pragma
4621 and then Pragma_Name (Decl) = Name_Import
4622 then
4623 Check_Pragma_Import (Decl);
4624 end if;
4626 Next (Decl);
4627 end loop;
4629 -- Same set of tests for private part. In addition to subprograms
4630 -- detect the presence of Taft Amendment types (incomplete types
4631 -- completed in the body).
4633 Decl := First (Private_Declarations (Spec));
4634 while Present (Decl) loop
4635 if Comes_From_Source (Decl)
4636 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4637 N_Subprogram_Renaming_Declaration,
4638 N_Generic_Subprogram_Declaration))
4639 then
4640 Append_Elmt (Defining_Entity (Decl), Subp_List);
4642 elsif Nkind_In (Decl, N_Package_Declaration,
4643 N_Generic_Package_Declaration)
4644 then
4645 Check_Declarations (Specification (Decl));
4647 -- Collect incomplete type declarations for separate pass
4649 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4650 Append_Elmt (Decl, Incomplete_Decls);
4652 elsif Nkind (Decl) = N_Pragma
4653 and then Pragma_Name (Decl) = Name_Import
4654 then
4655 Check_Pragma_Import (Decl);
4656 end if;
4658 Next (Decl);
4659 end loop;
4661 -- Now check incomplete declarations to locate Taft amendment
4662 -- types. This can be done by examining the defining identifiers
4663 -- of type declarations without real semantic analysis.
4665 declare
4666 Inc : Elmt_Id;
4668 begin
4669 Inc := First_Elmt (Incomplete_Decls);
4670 while Present (Inc) loop
4671 Decl := Next (Node (Inc));
4672 while Present (Decl) loop
4673 if Nkind (Decl) = N_Full_Type_Declaration
4674 and then Chars (Defining_Identifier (Decl)) =
4675 Chars (Defining_Identifier (Node (Inc)))
4676 then
4677 exit;
4678 end if;
4680 Next (Decl);
4681 end loop;
4683 -- If no completion, this is a TAT, and a body is needed
4685 if No (Decl) then
4686 Set_Body_Required (Library_Unit (N));
4687 return;
4688 end if;
4690 Next_Elmt (Inc);
4691 end loop;
4692 end;
4694 -- Finally, check whether there are subprograms that still require
4695 -- a body, i.e. are not renamings or null.
4697 if not Is_Empty_Elmt_List (Subp_List) then
4698 declare
4699 Subp_Id : Elmt_Id;
4700 Spec : Node_Id;
4702 begin
4703 Subp_Id := First_Elmt (Subp_List);
4704 Spec := Parent (Node (Subp_Id));
4706 while Present (Subp_Id) loop
4707 if Nkind (Parent (Spec))
4708 = N_Subprogram_Renaming_Declaration
4709 then
4710 null;
4712 elsif Nkind (Spec) = N_Procedure_Specification
4713 and then Null_Present (Spec)
4714 then
4715 null;
4717 else
4718 Set_Body_Required (Library_Unit (N));
4719 return;
4720 end if;
4722 Next_Elmt (Subp_Id);
4723 end loop;
4724 end;
4725 end if;
4726 end Check_Declarations;
4728 -- Start of processing for Check_Body_Required
4730 begin
4731 -- If this is an imported package (Java and CIL usage) no body is
4732 -- needed. Scan list of pragmas that may follow a compilation unit
4733 -- to look for a relevant pragma Import.
4735 if Present (PA) then
4736 declare
4737 Prag : Node_Id;
4739 begin
4740 Prag := First (PA);
4741 while Present (Prag) loop
4742 if Nkind (Prag) = N_Pragma
4743 and then Get_Pragma_Id (Prag) = Pragma_Import
4744 then
4745 return;
4746 end if;
4748 Next (Prag);
4749 end loop;
4750 end;
4751 end if;
4753 Check_Declarations (Specification (P_Unit));
4754 end Check_Body_Required;
4756 -----------------------------
4757 -- Has_Limited_With_Clause --
4758 -----------------------------
4760 function Has_Limited_With_Clause
4761 (C_Unit : Entity_Id;
4762 Pack : Entity_Id) return Boolean
4764 Par : Entity_Id;
4765 Par_Unit : Node_Id;
4767 begin
4768 Par := C_Unit;
4769 while Present (Par) loop
4770 if Ekind (Par) /= E_Package then
4771 exit;
4772 end if;
4774 -- Retrieve the Compilation_Unit node for Par and determine if
4775 -- its context clauses contain a limited with for Pack.
4777 Par_Unit := Parent (Parent (Parent (Par)));
4779 if Nkind (Par_Unit) = N_Package_Declaration then
4780 Par_Unit := Parent (Par_Unit);
4781 end if;
4783 if Has_With_Clause (Par_Unit, Pack, True) then
4784 return True;
4785 end if;
4787 -- If there are more ancestors, climb up the tree, otherwise we
4788 -- are done.
4790 if Is_Child_Unit (Par) then
4791 Par := Scope (Par);
4792 else
4793 exit;
4794 end if;
4795 end loop;
4797 return False;
4798 end Has_Limited_With_Clause;
4800 ----------------------------------
4801 -- Is_Visible_Through_Renamings --
4802 ----------------------------------
4804 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4805 Kind : constant Node_Kind :=
4806 Nkind (Unit (Cunit (Current_Sem_Unit)));
4807 Aux_Unit : Node_Id;
4808 Item : Node_Id;
4809 Decl : Entity_Id;
4811 begin
4812 -- Example of the error detected by this subprogram:
4814 -- package P is
4815 -- type T is ...
4816 -- end P;
4818 -- with P;
4819 -- package Q is
4820 -- package Ren_P renames P;
4821 -- end Q;
4823 -- with Q;
4824 -- package R is ...
4826 -- limited with P; -- ERROR
4827 -- package R.C is ...
4829 Aux_Unit := Cunit (Current_Sem_Unit);
4831 loop
4832 Item := First (Context_Items (Aux_Unit));
4833 while Present (Item) loop
4834 if Nkind (Item) = N_With_Clause
4835 and then not Limited_Present (Item)
4836 and then Nkind (Unit (Library_Unit (Item))) =
4837 N_Package_Declaration
4838 then
4839 Decl :=
4840 First (Visible_Declarations
4841 (Specification (Unit (Library_Unit (Item)))));
4842 while Present (Decl) loop
4843 if Nkind (Decl) = N_Package_Renaming_Declaration
4844 and then Entity (Name (Decl)) = P
4845 then
4846 -- Generate the error message only if the current unit
4847 -- is a package declaration; in case of subprogram
4848 -- bodies and package bodies we just return True to
4849 -- indicate that the limited view must not be
4850 -- installed.
4852 if Kind = N_Package_Declaration then
4853 Error_Msg_N
4854 ("simultaneous visibility of the limited and " &
4855 "unlimited views not allowed", N);
4856 Error_Msg_Sloc := Sloc (Item);
4857 Error_Msg_NE
4858 ("\\ unlimited view of & visible through the " &
4859 "context clause #", N, P);
4860 Error_Msg_Sloc := Sloc (Decl);
4861 Error_Msg_NE ("\\ and the renaming #", N, P);
4862 end if;
4864 return True;
4865 end if;
4867 Next (Decl);
4868 end loop;
4869 end if;
4871 Next (Item);
4872 end loop;
4874 -- If it is a body not acting as spec, follow pointer to the
4875 -- corresponding spec, otherwise follow pointer to parent spec.
4877 if Present (Library_Unit (Aux_Unit))
4878 and then Nkind_In (Unit (Aux_Unit),
4879 N_Package_Body, N_Subprogram_Body)
4880 then
4881 if Aux_Unit = Library_Unit (Aux_Unit) then
4883 -- Aux_Unit is a body that acts as a spec. Clause has
4884 -- already been flagged as illegal.
4886 return False;
4888 else
4889 Aux_Unit := Library_Unit (Aux_Unit);
4890 end if;
4892 else
4893 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4894 end if;
4896 exit when No (Aux_Unit);
4897 end loop;
4899 return False;
4900 end Is_Visible_Through_Renamings;
4902 -- Start of processing for Install_Limited_With_Clause
4904 begin
4905 pragma Assert (not Limited_View_Installed (N));
4907 -- In case of limited with_clause on subprograms, generics, instances,
4908 -- or renamings, the corresponding error was previously posted and we
4909 -- have nothing to do here. If the file is missing altogether, it has
4910 -- no source location.
4912 if Nkind (P_Unit) /= N_Package_Declaration
4913 or else Sloc (P_Unit) = No_Location
4914 then
4915 return;
4916 end if;
4918 P := Defining_Unit_Name (Specification (P_Unit));
4920 -- Handle child packages
4922 if Nkind (P) = N_Defining_Program_Unit_Name then
4923 Is_Child_Package := True;
4924 P := Defining_Identifier (P);
4925 end if;
4927 -- Do not install the limited-view if the context of the unit is already
4928 -- available through a regular with clause.
4930 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4931 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4932 then
4933 return;
4934 end if;
4936 -- Do not install the limited-view if the full-view is already visible
4937 -- through renaming declarations.
4939 if Is_Visible_Through_Renamings (P) then
4940 return;
4941 end if;
4943 -- Do not install the limited view if this is the unit being analyzed.
4944 -- This unusual case will happen when a unit has a limited_with clause
4945 -- on one of its children. The compilation of the child forces the load
4946 -- of the parent which tries to install the limited view of the child
4947 -- again. Installing the limited view must also be disabled when
4948 -- compiling the body of the child unit.
4950 if P = Cunit_Entity (Current_Sem_Unit)
4951 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4952 and then P = Main_Unit_Entity
4953 and then Is_Ancestor_Unit
4954 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
4955 then
4956 return;
4957 end if;
4959 -- This scenario is similar to the one above, the difference is that the
4960 -- compilation of sibling Par.Sib forces the load of parent Par which
4961 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4962 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4963 -- nonlimited views of all entities from Lim_Pack.
4965 -- limited with Lim_Pack; -- [1]
4966 -- package Par is ... package Lim_Pack is ...
4968 -- with Lim_Pack; -- [2]
4969 -- package Par.Sib is ... package body Par.Sib is ...
4971 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4972 -- Sem_Unit is the body of Par.Sib.
4974 if Ekind (P) = E_Package
4975 and then Ekind (Main_Unit_Entity) = E_Package
4976 and then Is_Child_Unit (Main_Unit_Entity)
4978 -- The body has a regular with clause
4980 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4981 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4983 -- One of the ancestors has a limited with clause
4985 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4986 N_Package_Specification
4987 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4988 then
4989 return;
4990 end if;
4992 -- A common use of the limited-with is to have a limited-with in the
4993 -- package spec, and a normal with in its package body. For example:
4995 -- limited with X; -- [1]
4996 -- package A is ...
4998 -- with X; -- [2]
4999 -- package body A is ...
5001 -- The compilation of A's body installs the context clauses found at [2]
5002 -- and then the context clauses of its specification (found at [1]). As
5003 -- a consequence, at [1] the specification of X has been analyzed and it
5004 -- is immediately visible. According to the semantics of limited-with
5005 -- context clauses we don't install the limited view because the full
5006 -- view of X supersedes its limited view.
5008 if Analyzed (P_Unit)
5009 and then
5010 (Is_Immediately_Visible (P)
5011 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
5012 then
5014 -- The presence of both the limited and the analyzed nonlimited view
5015 -- may also be an error, such as an illegal context for a limited
5016 -- with_clause. In that case, do not process the context item at all.
5018 if Error_Posted (N) then
5019 return;
5020 end if;
5022 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
5023 declare
5024 Item : Node_Id;
5025 begin
5026 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
5027 while Present (Item) loop
5028 if Nkind (Item) = N_With_Clause
5029 and then Comes_From_Source (Item)
5030 and then Entity (Name (Item)) = P
5031 then
5032 return;
5033 end if;
5035 Next (Item);
5036 end loop;
5037 end;
5039 -- If this is a child body, assume that the nonlimited with_clause
5040 -- appears in an ancestor. Could be refined ???
5042 if Is_Child_Unit
5043 (Defining_Entity
5044 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
5045 then
5046 return;
5047 end if;
5049 else
5051 -- If in package declaration, nonlimited view brought in from
5052 -- parent unit or some error condition.
5054 return;
5055 end if;
5056 end if;
5058 if Debug_Flag_I then
5059 Write_Str ("install limited view of ");
5060 Write_Name (Chars (P));
5061 Write_Eol;
5062 end if;
5064 -- If the unit has not been analyzed and the limited view has not been
5065 -- already installed then we install it.
5067 if not Analyzed (P_Unit) then
5068 if not In_Chain (P) then
5070 -- Minimum decoration
5072 Set_Ekind (P, E_Package);
5073 Set_Etype (P, Standard_Void_Type);
5074 Set_Scope (P, Standard_Standard);
5075 Set_Is_Visible_Lib_Unit (P);
5077 if Is_Child_Package then
5078 Set_Is_Child_Unit (P);
5079 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
5080 end if;
5082 -- Place entity on visibility structure
5084 Set_Homonym (P, Current_Entity (P));
5085 Set_Current_Entity (P);
5087 if Debug_Flag_I then
5088 Write_Str (" (homonym) chain ");
5089 Write_Name (Chars (P));
5090 Write_Eol;
5091 end if;
5093 -- Install the incomplete view. The first element of the limited
5094 -- view is a header (an E_Package entity) used to reference the
5095 -- first shadow entity in the private part of the package.
5097 Lim_Header := Limited_View (P);
5098 Lim_Typ := First_Entity (Lim_Header);
5100 while Present (Lim_Typ)
5101 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5102 loop
5103 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
5104 Set_Current_Entity (Lim_Typ);
5106 if Debug_Flag_I then
5107 Write_Str (" (homonym) chain ");
5108 Write_Name (Chars (Lim_Typ));
5109 Write_Eol;
5110 end if;
5112 Next_Entity (Lim_Typ);
5113 end loop;
5114 end if;
5116 -- If the unit appears in a previous regular with_clause, the regular
5117 -- entities of the public part of the withed package must be replaced
5118 -- by the shadow ones.
5120 -- This code must be kept synchronized with the code that replaces the
5121 -- shadow entities by the real entities (see body of Remove_Limited
5122 -- With_Clause); otherwise the contents of the homonym chains are not
5123 -- consistent.
5125 else
5126 -- Hide all the type entities of the public part of the package to
5127 -- avoid its usage. This is needed to cover all the subtype decla-
5128 -- rations because we do not remove them from the homonym chain.
5130 E := First_Entity (P);
5131 while Present (E) and then E /= First_Private_Entity (P) loop
5132 if Is_Type (E) then
5133 Set_Was_Hidden (E, Is_Hidden (E));
5134 Set_Is_Hidden (E);
5135 end if;
5137 Next_Entity (E);
5138 end loop;
5140 -- Replace the real entities by the shadow entities of the limited
5141 -- view. The first element of the limited view is a header that is
5142 -- used to reference the first shadow entity in the private part
5143 -- of the package. Successive elements are the limited views of the
5144 -- type (including regular incomplete types) declared in the package.
5146 Lim_Header := Limited_View (P);
5148 Lim_Typ := First_Entity (Lim_Header);
5149 while Present (Lim_Typ)
5150 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5151 loop
5152 pragma Assert (not In_Chain (Lim_Typ));
5154 -- Do not unchain nested packages and child units
5156 if Ekind (Lim_Typ) /= E_Package
5157 and then not Is_Child_Unit (Lim_Typ)
5158 then
5159 declare
5160 Prev : Entity_Id;
5162 begin
5163 Prev := Current_Entity (Lim_Typ);
5164 E := Prev;
5166 -- Replace E in the homonyms list, so that the limited view
5167 -- becomes available.
5169 -- If the nonlimited view is a record with an anonymous
5170 -- self-referential component, the analysis of the record
5171 -- declaration creates an incomplete type with the same name
5172 -- in order to define an internal access type. The visible
5173 -- entity is now the incomplete type, and that is the one to
5174 -- replace in the visibility structure.
5176 if E = Non_Limited_View (Lim_Typ)
5177 or else
5178 (Ekind (E) = E_Incomplete_Type
5179 and then Full_View (E) = Non_Limited_View (Lim_Typ))
5180 then
5181 Set_Homonym (Lim_Typ, Homonym (Prev));
5182 Set_Current_Entity (Lim_Typ);
5184 else
5185 loop
5186 E := Homonym (Prev);
5188 -- E may have been removed when installing a previous
5189 -- limited_with_clause.
5191 exit when No (E);
5192 exit when E = Non_Limited_View (Lim_Typ);
5193 Prev := Homonym (Prev);
5194 end loop;
5196 if Present (E) then
5197 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
5198 Set_Homonym (Prev, Lim_Typ);
5199 end if;
5200 end if;
5201 end;
5203 if Debug_Flag_I then
5204 Write_Str (" (homonym) chain ");
5205 Write_Name (Chars (Lim_Typ));
5206 Write_Eol;
5207 end if;
5208 end if;
5210 Next_Entity (Lim_Typ);
5211 end loop;
5212 end if;
5214 -- The package must be visible while the limited-with clause is active
5215 -- because references to the type P.T must resolve in the usual way.
5216 -- In addition, we remember that the limited-view has been installed to
5217 -- uninstall it at the point of context removal.
5219 Set_Is_Immediately_Visible (P);
5220 Set_Limited_View_Installed (N);
5222 -- If unit has not been analyzed in some previous context, check
5223 -- (imperfectly ???) whether it might need a body.
5225 if not Analyzed (P_Unit) then
5226 Check_Body_Required;
5227 end if;
5229 -- If the package in the limited_with clause is a child unit, the clause
5230 -- is unanalyzed and appears as a selected component. Recast it as an
5231 -- expanded name so that the entity can be properly set. Use entity of
5232 -- parent, if available, for higher ancestors in the name.
5234 if Nkind (Name (N)) = N_Selected_Component then
5235 declare
5236 Nam : Node_Id;
5237 Ent : Entity_Id;
5239 begin
5240 Nam := Name (N);
5241 Ent := P;
5242 while Nkind (Nam) = N_Selected_Component
5243 and then Present (Ent)
5244 loop
5245 Change_Selected_Component_To_Expanded_Name (Nam);
5247 -- Set entity of parent identifiers if the unit is a child
5248 -- unit. This ensures that the tree is properly formed from
5249 -- semantic point of view (e.g. for ASIS queries). The unit
5250 -- entities are not fully analyzed, so we need to follow unit
5251 -- links in the tree.
5253 Set_Entity (Nam, Ent);
5255 Nam := Prefix (Nam);
5256 Ent :=
5257 Defining_Entity
5258 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5260 -- Set entity of last ancestor
5262 if Nkind (Nam) = N_Identifier then
5263 Set_Entity (Nam, Ent);
5264 end if;
5265 end loop;
5266 end;
5267 end if;
5269 Set_Entity (Name (N), P);
5270 Set_From_Limited_With (P);
5271 end Install_Limited_With_Clause;
5273 -------------------------
5274 -- Install_With_Clause --
5275 -------------------------
5277 procedure Install_With_Clause
5278 (With_Clause : Node_Id;
5279 Private_With_OK : Boolean := False)
5281 Uname : constant Entity_Id := Entity (Name (With_Clause));
5282 P : constant Entity_Id := Scope (Uname);
5284 begin
5285 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5286 -- compiling a package declaration and the Private_With_OK flag was not
5287 -- set by the caller. These declarations will be installed later (before
5288 -- analyzing the private part of the package).
5290 if Private_Present (With_Clause)
5291 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
5292 and then not (Private_With_OK)
5293 then
5294 return;
5295 end if;
5297 if Debug_Flag_I then
5298 if Private_Present (With_Clause) then
5299 Write_Str ("install private withed unit ");
5300 else
5301 Write_Str ("install withed unit ");
5302 end if;
5304 Write_Name (Chars (Uname));
5305 Write_Eol;
5306 end if;
5308 -- We do not apply the restrictions to an internal unit unless we are
5309 -- compiling the internal unit as a main unit. This check is also
5310 -- skipped for dummy units (for missing packages).
5312 if Sloc (Uname) /= No_Location
5313 and then (not Is_Internal_Unit (Current_Sem_Unit)
5314 or else Current_Sem_Unit = Main_Unit)
5315 then
5316 Check_Restricted_Unit
5317 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5318 end if;
5320 if P /= Standard_Standard then
5322 -- If the unit is not analyzed after analysis of the with clause and
5323 -- it is an instantiation then it awaits a body and is the main unit.
5324 -- Its appearance in the context of some other unit indicates a
5325 -- circular dependency (DEC suite perversity).
5327 if not Analyzed (Uname)
5328 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5329 then
5330 Error_Msg_N
5331 ("instantiation depends on itself", Name (With_Clause));
5333 elsif not Is_Visible_Lib_Unit (Uname) then
5335 -- Abandon processing in case of previous errors
5337 if No (Scope (Uname)) then
5338 Check_Error_Detected;
5339 return;
5340 end if;
5342 Set_Is_Visible_Lib_Unit (Uname);
5344 -- If the unit is a wrapper package for a compilation unit that is
5345 -- a subprogrm instance, indicate that the instance itself is a
5346 -- visible unit. This is necessary if the instance is inlined.
5348 if Is_Wrapper_Package (Uname) then
5349 Set_Is_Visible_Lib_Unit (Related_Instance (Uname));
5350 end if;
5352 -- If the child unit appears in the context of its parent, it is
5353 -- immediately visible.
5355 if In_Open_Scopes (Scope (Uname)) then
5356 Set_Is_Immediately_Visible (Uname);
5357 end if;
5359 if Is_Generic_Instance (Uname)
5360 and then Ekind (Uname) in Subprogram_Kind
5361 then
5362 -- Set flag as well on the visible entity that denotes the
5363 -- instance, which renames the current one.
5365 Set_Is_Visible_Lib_Unit
5366 (Related_Instance
5367 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5368 end if;
5370 -- The parent unit may have been installed already, and may have
5371 -- appeared in a use clause.
5373 if In_Use (Scope (Uname)) then
5374 Set_Is_Potentially_Use_Visible (Uname);
5375 end if;
5377 Set_Context_Installed (With_Clause);
5378 end if;
5380 elsif not Is_Immediately_Visible (Uname) then
5381 Set_Is_Visible_Lib_Unit (Uname);
5383 if not Private_Present (With_Clause) or else Private_With_OK then
5384 Set_Is_Immediately_Visible (Uname);
5385 end if;
5387 Set_Context_Installed (With_Clause);
5388 end if;
5390 -- A [private] with clause overrides a limited with clause. Restore the
5391 -- proper view of the package by performing the following actions:
5393 -- * Remove all shadow entities which hide their corresponding
5394 -- entities from direct visibility by updating the entity and
5395 -- homonym chains.
5397 -- * Enter the corresponding entities back in direct visibility
5399 -- Note that the original limited with clause which installed its view
5400 -- is still marked as "active". This effect is undone when the clause
5401 -- itself is removed, see Remove_Limited_With_Clause.
5403 if Ekind (Uname) = E_Package and then From_Limited_With (Uname) then
5404 Remove_Limited_With_Unit (Unit_Declaration_Node (Uname));
5405 end if;
5407 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5408 -- unit if there is a visible homograph for it declared in the same
5409 -- declarative region. This pathological case can only arise when an
5410 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5411 -- G1 has a generic child also named G2, and the context includes with_
5412 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5413 -- of I1.G2 visible as well. If the child unit is named Standard, do
5414 -- not apply the check to the Standard package itself.
5416 if Is_Child_Unit (Uname)
5417 and then Is_Visible_Lib_Unit (Uname)
5418 and then Ada_Version >= Ada_2005
5419 then
5420 declare
5421 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5422 Decl2 : Node_Id;
5423 P2 : Entity_Id;
5424 U2 : Entity_Id;
5426 begin
5427 U2 := Homonym (Uname);
5428 while Present (U2) and then U2 /= Standard_Standard loop
5429 P2 := Scope (U2);
5430 Decl2 := Unit_Declaration_Node (P2);
5432 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5433 if Is_Generic_Instance (P)
5434 and then Nkind (Decl1) = N_Package_Declaration
5435 and then Generic_Parent (Specification (Decl1)) = P2
5436 then
5437 Error_Msg_N ("illegal with_clause", With_Clause);
5438 Error_Msg_N
5439 ("\child unit has visible homograph" &
5440 " (RM 8.3(26), 10.1.1(19))",
5441 With_Clause);
5442 exit;
5444 elsif Is_Generic_Instance (P2)
5445 and then Nkind (Decl2) = N_Package_Declaration
5446 and then Generic_Parent (Specification (Decl2)) = P
5447 then
5448 -- With_clause for child unit of instance appears before
5449 -- in the context. We want to place the error message on
5450 -- it, not on the generic child unit itself.
5452 declare
5453 Prev_Clause : Node_Id;
5455 begin
5456 Prev_Clause := First (List_Containing (With_Clause));
5457 while Entity (Name (Prev_Clause)) /= U2 loop
5458 Next (Prev_Clause);
5459 end loop;
5461 pragma Assert (Present (Prev_Clause));
5462 Error_Msg_N ("illegal with_clause", Prev_Clause);
5463 Error_Msg_N
5464 ("\child unit has visible homograph" &
5465 " (RM 8.3(26), 10.1.1(19))",
5466 Prev_Clause);
5467 exit;
5468 end;
5469 end if;
5470 end if;
5472 U2 := Homonym (U2);
5473 end loop;
5474 end;
5475 end if;
5476 end Install_With_Clause;
5478 -------------------
5479 -- Is_Child_Spec --
5480 -------------------
5482 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5483 K : constant Node_Kind := Nkind (Lib_Unit);
5485 begin
5486 return (K in N_Generic_Declaration or else
5487 K in N_Generic_Instantiation or else
5488 K in N_Generic_Renaming_Declaration or else
5489 K = N_Package_Declaration or else
5490 K = N_Package_Renaming_Declaration or else
5491 K = N_Subprogram_Declaration or else
5492 K = N_Subprogram_Renaming_Declaration)
5493 and then Present (Parent_Spec (Lib_Unit));
5494 end Is_Child_Spec;
5496 ------------------------------------
5497 -- Is_Legal_Shadow_Entity_In_Body --
5498 ------------------------------------
5500 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5501 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5502 begin
5503 return Nkind (Unit (C_Unit)) = N_Package_Body
5504 and then
5505 Has_With_Clause
5506 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5507 end Is_Legal_Shadow_Entity_In_Body;
5509 ----------------------
5510 -- Is_Ancestor_Unit --
5511 ----------------------
5513 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5514 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5515 E2 : Entity_Id;
5516 begin
5517 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
5518 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5519 return Is_Ancestor_Package (E1, E2);
5520 else
5521 return False;
5522 end if;
5523 end Is_Ancestor_Unit;
5525 -----------------------
5526 -- Load_Needed_Body --
5527 -----------------------
5529 -- N is a generic unit named in a with clause, or else it is a unit that
5530 -- contains a generic unit or an inlined function. In order to perform an
5531 -- instantiation, the body of the unit must be present. If the unit itself
5532 -- is generic, we assume that an instantiation follows, and load & analyze
5533 -- the body unconditionally. This forces analysis of the spec as well.
5535 -- If the unit is not generic, but contains a generic unit, it is loaded on
5536 -- demand, at the point of instantiation (see ch12).
5538 procedure Load_Needed_Body
5539 (N : Node_Id;
5540 OK : out Boolean;
5541 Do_Analyze : Boolean := True)
5543 Body_Name : Unit_Name_Type;
5544 Unum : Unit_Number_Type;
5546 Save_Style_Check : constant Boolean := Opt.Style_Check;
5547 -- The loading and analysis is done with style checks off
5549 begin
5550 if not GNAT_Mode then
5551 Style_Check := False;
5552 end if;
5554 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5555 Unum :=
5556 Load_Unit
5557 (Load_Name => Body_Name,
5558 Required => False,
5559 Subunit => False,
5560 Error_Node => N,
5561 Renamings => True);
5563 if Unum = No_Unit then
5564 OK := False;
5566 else
5567 Compiler_State := Analyzing; -- reset after load
5569 if Fatal_Error (Unum) /= Error_Detected or else Try_Semantics then
5570 if Debug_Flag_L then
5571 Write_Str ("*** Loaded generic body");
5572 Write_Eol;
5573 end if;
5575 if Do_Analyze then
5576 Semantics (Cunit (Unum));
5577 end if;
5578 end if;
5580 OK := True;
5581 end if;
5583 Style_Check := Save_Style_Check;
5584 end Load_Needed_Body;
5586 -------------------------
5587 -- Build_Limited_Views --
5588 -------------------------
5590 procedure Build_Limited_Views (N : Node_Id) is
5591 Unum : constant Unit_Number_Type :=
5592 Get_Source_Unit (Library_Unit (N));
5593 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
5595 Shadow_Pack : Entity_Id;
5596 -- The corresponding shadow entity of the withed package. This entity
5597 -- offers incomplete views of packages and types as well as abstract
5598 -- views of states and variables declared within.
5600 Last_Shadow : Entity_Id := Empty;
5601 -- The last shadow entity created by routine Build_Shadow_Entity
5603 procedure Build_Shadow_Entity
5604 (Ent : Entity_Id;
5605 Scop : Entity_Id;
5606 Shadow : out Entity_Id;
5607 Is_Tagged : Boolean := False);
5608 -- Create a shadow entity that hides Ent and offers an abstract or
5609 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5610 -- should be set when Ent is a tagged type. The generated entity is
5611 -- added to Lim_Header. This routine updates the value of Last_Shadow.
5613 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
5614 -- Perform minimal decoration of a package or its corresponding shadow
5615 -- entity denoted by Ent. Scop is the proper scope.
5617 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id);
5618 -- Perform full decoration of an abstract state or its corresponding
5619 -- shadow entity denoted by Ent. Scop is the proper scope.
5621 procedure Decorate_Type
5622 (Ent : Entity_Id;
5623 Scop : Entity_Id;
5624 Is_Tagged : Boolean := False;
5625 Materialize : Boolean := False);
5626 -- Perform minimal decoration of a type or its corresponding shadow
5627 -- entity denoted by Ent. Scop is the proper scope. Flag Is_Tagged
5628 -- should be set when Ent is a tagged type. Flag Materialize should be
5629 -- set when Ent is a tagged type and its class-wide type needs to appear
5630 -- in the tree.
5632 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id);
5633 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5634 -- proper scope.
5636 procedure Process_Declarations_And_States
5637 (Pack : Entity_Id;
5638 Decls : List_Id;
5639 Scop : Entity_Id;
5640 Create_Abstract_Views : Boolean);
5641 -- Inspect the states of package Pack and declarative list Decls. Create
5642 -- shadow entities for all nested packages, states, types and variables
5643 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5644 -- be set when the abstract states and variables need to be processed.
5646 -------------------------
5647 -- Build_Shadow_Entity --
5648 -------------------------
5650 procedure Build_Shadow_Entity
5651 (Ent : Entity_Id;
5652 Scop : Entity_Id;
5653 Shadow : out Entity_Id;
5654 Is_Tagged : Boolean := False)
5656 begin
5657 Shadow := Make_Temporary (Sloc (Ent), 'Z');
5659 -- The shadow entity must share the same name and parent as the
5660 -- entity it hides.
5662 Set_Chars (Shadow, Chars (Ent));
5663 Set_Parent (Shadow, Parent (Ent));
5665 -- The abstract view of a variable is a state, not another variable
5667 if Ekind (Ent) = E_Variable then
5668 Set_Ekind (Shadow, E_Abstract_State);
5669 else
5670 Set_Ekind (Shadow, Ekind (Ent));
5671 end if;
5673 Set_Is_Internal (Shadow);
5674 Set_From_Limited_With (Shadow);
5676 -- Add the new shadow entity to the limited view of the package
5678 Last_Shadow := Shadow;
5679 Append_Entity (Shadow, Shadow_Pack);
5681 -- Perform context-specific decoration of the shadow entity
5683 if Ekind (Ent) = E_Abstract_State then
5684 Decorate_State (Shadow, Scop);
5685 Set_Non_Limited_View (Shadow, Ent);
5687 elsif Ekind (Ent) = E_Package then
5688 Decorate_Package (Shadow, Scop);
5690 elsif Is_Type (Ent) then
5691 Decorate_Type (Shadow, Scop, Is_Tagged);
5692 Set_Non_Limited_View (Shadow, Ent);
5694 if Is_Tagged then
5695 Set_Non_Limited_View
5696 (Class_Wide_Type (Shadow), Class_Wide_Type (Ent));
5697 end if;
5699 if Is_Incomplete_Or_Private_Type (Ent) then
5700 Set_Private_Dependents (Shadow, New_Elmt_List);
5701 end if;
5703 elsif Ekind (Ent) = E_Variable then
5704 Decorate_State (Shadow, Scop);
5705 Set_Non_Limited_View (Shadow, Ent);
5706 end if;
5707 end Build_Shadow_Entity;
5709 ----------------------
5710 -- Decorate_Package --
5711 ----------------------
5713 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
5714 begin
5715 Set_Ekind (Ent, E_Package);
5716 Set_Etype (Ent, Standard_Void_Type);
5717 Set_Scope (Ent, Scop);
5718 end Decorate_Package;
5720 --------------------
5721 -- Decorate_State --
5722 --------------------
5724 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id) is
5725 begin
5726 Set_Ekind (Ent, E_Abstract_State);
5727 Set_Etype (Ent, Standard_Void_Type);
5728 Set_Scope (Ent, Scop);
5729 Set_Encapsulating_State (Ent, Empty);
5730 end Decorate_State;
5732 -------------------
5733 -- Decorate_Type --
5734 -------------------
5736 procedure Decorate_Type
5737 (Ent : Entity_Id;
5738 Scop : Entity_Id;
5739 Is_Tagged : Boolean := False;
5740 Materialize : Boolean := False)
5742 CW_Typ : Entity_Id;
5744 begin
5745 -- An unanalyzed type or a shadow entity of a type is treated as an
5746 -- incomplete type, and carries the corresponding attributes.
5748 Set_Ekind (Ent, E_Incomplete_Type);
5749 Set_Etype (Ent, Ent);
5750 Set_Full_View (Ent, Empty);
5751 Set_Is_First_Subtype (Ent);
5752 Set_Scope (Ent, Scop);
5753 Set_Stored_Constraint (Ent, No_Elist);
5754 Init_Size_Align (Ent);
5756 if From_Limited_With (Ent) then
5757 Set_Private_Dependents (Ent, New_Elmt_List);
5758 end if;
5760 -- A tagged type and its corresponding shadow entity share one common
5761 -- class-wide type. The list of primitive operations for the shadow
5762 -- entity is empty.
5764 if Is_Tagged then
5765 Set_Is_Tagged_Type (Ent);
5766 Set_Direct_Primitive_Operations (Ent, New_Elmt_List);
5768 CW_Typ :=
5769 New_External_Entity
5770 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
5772 Set_Class_Wide_Type (Ent, CW_Typ);
5774 -- Set parent to be the same as the parent of the tagged type.
5775 -- We need a parent field set, and it is supposed to point to
5776 -- the declaration of the type. The tagged type declaration
5777 -- essentially declares two separate types, the tagged type
5778 -- itself and the corresponding class-wide type, so it is
5779 -- reasonable for the parent fields to point to the declaration
5780 -- in both cases.
5782 Set_Parent (CW_Typ, Parent (Ent));
5784 Set_Ekind (CW_Typ, E_Class_Wide_Type);
5785 Set_Class_Wide_Type (CW_Typ, CW_Typ);
5786 Set_Etype (CW_Typ, Ent);
5787 Set_Equivalent_Type (CW_Typ, Empty);
5788 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
5789 Set_Has_Unknown_Discriminants (CW_Typ);
5790 Set_Is_First_Subtype (CW_Typ);
5791 Set_Is_Tagged_Type (CW_Typ);
5792 Set_Materialize_Entity (CW_Typ, Materialize);
5793 Set_Scope (CW_Typ, Scop);
5794 Init_Size_Align (CW_Typ);
5795 end if;
5796 end Decorate_Type;
5798 -----------------------
5799 -- Decorate_Variable --
5800 -----------------------
5802 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id) is
5803 begin
5804 Set_Ekind (Ent, E_Variable);
5805 Set_Etype (Ent, Standard_Void_Type);
5806 Set_Scope (Ent, Scop);
5807 end Decorate_Variable;
5809 -------------------------------------
5810 -- Process_Declarations_And_States --
5811 -------------------------------------
5813 procedure Process_Declarations_And_States
5814 (Pack : Entity_Id;
5815 Decls : List_Id;
5816 Scop : Entity_Id;
5817 Create_Abstract_Views : Boolean)
5819 procedure Find_And_Process_States;
5820 -- Determine whether package Pack defines abstract state either by
5821 -- using an aspect or a pragma. If this is the case, build shadow
5822 -- entities for all abstract states of Pack.
5824 procedure Process_States (States : Elist_Id);
5825 -- Generate shadow entities for all abstract states in list States
5827 -----------------------------
5828 -- Find_And_Process_States --
5829 -----------------------------
5831 procedure Find_And_Process_States is
5832 procedure Process_State (State : Node_Id);
5833 -- Generate shadow entities for a single abstract state or
5834 -- multiple states expressed as an aggregate.
5836 -------------------
5837 -- Process_State --
5838 -------------------
5840 procedure Process_State (State : Node_Id) is
5841 Loc : constant Source_Ptr := Sloc (State);
5842 Decl : Node_Id;
5843 Dummy : Entity_Id;
5844 Elmt : Node_Id;
5845 Id : Entity_Id;
5847 begin
5848 -- Multiple abstract states appear as an aggregate
5850 if Nkind (State) = N_Aggregate then
5851 Elmt := First (Expressions (State));
5852 while Present (Elmt) loop
5853 Process_State (Elmt);
5854 Next (Elmt);
5855 end loop;
5857 return;
5859 -- A null state has no abstract view
5861 elsif Nkind (State) = N_Null then
5862 return;
5864 -- State declaration with various options appears as an
5865 -- extension aggregate.
5867 elsif Nkind (State) = N_Extension_Aggregate then
5868 Decl := Ancestor_Part (State);
5870 -- Simple state declaration
5872 elsif Nkind (State) = N_Identifier then
5873 Decl := State;
5875 -- Possibly an illegal state declaration
5877 else
5878 return;
5879 end if;
5881 -- Abstract states are elaborated when the related pragma is
5882 -- elaborated. Since the withed package is not analyzed yet,
5883 -- the entities of the abstract states are not available. To
5884 -- overcome this complication, create the entities now and
5885 -- store them in their respective declarations. The entities
5886 -- are later used by routine Create_Abstract_State to declare
5887 -- and enter the states into visibility.
5889 if No (Entity (Decl)) then
5890 Id := Make_Defining_Identifier (Loc, Chars (Decl));
5892 Set_Entity (Decl, Id);
5893 Set_Parent (Id, State);
5894 Decorate_State (Id, Scop);
5896 -- Otherwise the package was previously withed
5898 else
5899 Id := Entity (Decl);
5900 end if;
5902 Build_Shadow_Entity (Id, Scop, Dummy);
5903 end Process_State;
5905 -- Local variables
5907 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack);
5908 Asp : Node_Id;
5909 Decl : Node_Id;
5911 -- Start of processing for Find_And_Process_States
5913 begin
5914 -- Find aspect Abstract_State
5916 Asp := First (Aspect_Specifications (Pack_Decl));
5917 while Present (Asp) loop
5918 if Chars (Identifier (Asp)) = Name_Abstract_State then
5919 Process_State (Expression (Asp));
5921 return;
5922 end if;
5924 Next (Asp);
5925 end loop;
5927 -- Find pragma Abstract_State by inspecting the declarations
5929 Decl := First (Decls);
5930 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
5931 if Pragma_Name (Decl) = Name_Abstract_State then
5932 Process_State
5933 (Get_Pragma_Arg
5934 (First (Pragma_Argument_Associations (Decl))));
5936 return;
5937 end if;
5939 Next (Decl);
5940 end loop;
5941 end Find_And_Process_States;
5943 --------------------
5944 -- Process_States --
5945 --------------------
5947 procedure Process_States (States : Elist_Id) is
5948 Dummy : Entity_Id;
5949 Elmt : Elmt_Id;
5951 begin
5952 Elmt := First_Elmt (States);
5953 while Present (Elmt) loop
5954 Build_Shadow_Entity (Node (Elmt), Scop, Dummy);
5956 Next_Elmt (Elmt);
5957 end loop;
5958 end Process_States;
5960 -- Local variables
5962 Is_Tagged : Boolean;
5963 Decl : Node_Id;
5964 Def : Node_Id;
5965 Def_Id : Entity_Id;
5966 Shadow : Entity_Id;
5968 -- Start of processing for Process_Declarations_And_States
5970 begin
5971 -- Build abstract views for all states defined in the package
5973 if Create_Abstract_Views then
5975 -- When a package has been analyzed, all states are stored in list
5976 -- Abstract_States. Generate the shadow entities directly.
5978 if Is_Analyzed then
5979 if Present (Abstract_States (Pack)) then
5980 Process_States (Abstract_States (Pack));
5981 end if;
5983 -- The package may declare abstract states by using an aspect or a
5984 -- pragma. Attempt to locate one of these construct and if found,
5985 -- build the shadow entities.
5987 else
5988 Find_And_Process_States;
5989 end if;
5990 end if;
5992 -- Inspect the declarative list, looking for nested packages, types
5993 -- and variable declarations.
5995 Decl := First (Decls);
5996 while Present (Decl) loop
5998 -- Packages
6000 if Nkind (Decl) = N_Package_Declaration then
6001 Def_Id := Defining_Entity (Decl);
6003 -- Perform minor decoration when the withed package has not
6004 -- been analyzed.
6006 if not Is_Analyzed then
6007 Decorate_Package (Def_Id, Scop);
6008 end if;
6010 -- Create a shadow entity that offers a limited view of all
6011 -- visible types declared within.
6013 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6015 Process_Declarations_And_States
6016 (Pack => Def_Id,
6017 Decls =>
6018 Visible_Declarations (Specification (Decl)),
6019 Scop => Shadow,
6020 Create_Abstract_Views => Create_Abstract_Views);
6022 -- Types
6024 elsif Nkind_In (Decl, N_Full_Type_Declaration,
6025 N_Incomplete_Type_Declaration,
6026 N_Private_Extension_Declaration,
6027 N_Private_Type_Declaration,
6028 N_Protected_Type_Declaration,
6029 N_Task_Type_Declaration)
6030 then
6031 Def_Id := Defining_Entity (Decl);
6033 -- Determine whether the type is tagged. Note that packages
6034 -- included via a limited with clause are not always analyzed,
6035 -- hence the tree lookup rather than the use of attribute
6036 -- Is_Tagged_Type.
6038 if Nkind (Decl) = N_Full_Type_Declaration then
6039 Def := Type_Definition (Decl);
6041 Is_Tagged :=
6042 (Nkind (Def) = N_Record_Definition
6043 and then Tagged_Present (Def))
6044 or else
6045 (Nkind (Def) = N_Derived_Type_Definition
6046 and then Present (Record_Extension_Part (Def)));
6048 elsif Nkind_In (Decl, N_Incomplete_Type_Declaration,
6049 N_Private_Type_Declaration)
6050 then
6051 Is_Tagged := Tagged_Present (Decl);
6053 elsif Nkind (Decl) = N_Private_Extension_Declaration then
6054 Is_Tagged := True;
6056 else
6057 Is_Tagged := False;
6058 end if;
6060 -- Perform minor decoration when the withed package has not
6061 -- been analyzed.
6063 if not Is_Analyzed then
6064 Decorate_Type (Def_Id, Scop, Is_Tagged, True);
6065 end if;
6067 -- Create a shadow entity that hides the type and offers an
6068 -- incomplete view of the said type.
6070 Build_Shadow_Entity (Def_Id, Scop, Shadow, Is_Tagged);
6072 -- Variables
6074 elsif Create_Abstract_Views
6075 and then Nkind (Decl) = N_Object_Declaration
6076 and then not Constant_Present (Decl)
6077 then
6078 Def_Id := Defining_Entity (Decl);
6080 -- Perform minor decoration when the withed package has not
6081 -- been analyzed.
6083 if not Is_Analyzed then
6084 Decorate_Variable (Def_Id, Scop);
6085 end if;
6087 -- Create a shadow entity that hides the variable and offers an
6088 -- abstract view of the said variable.
6090 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6091 end if;
6093 Next (Decl);
6094 end loop;
6095 end Process_Declarations_And_States;
6097 -- Local variables
6099 Nam : constant Node_Id := Name (N);
6100 Pack : constant Entity_Id := Cunit_Entity (Unum);
6102 Last_Public_Shadow : Entity_Id := Empty;
6103 Private_Shadow : Entity_Id;
6104 Spec : Node_Id;
6106 -- Start of processing for Build_Limited_Views
6108 begin
6109 pragma Assert (Limited_Present (N));
6111 -- A library_item mentioned in a limited_with_clause is a package
6112 -- declaration, not a subprogram declaration, generic declaration,
6113 -- generic instantiation, or package renaming declaration.
6115 case Nkind (Unit (Library_Unit (N))) is
6116 when N_Package_Declaration =>
6117 null;
6119 when N_Subprogram_Declaration =>
6120 Error_Msg_N ("subprograms not allowed in limited with_clauses", N);
6121 return;
6123 when N_Generic_Package_Declaration
6124 | N_Generic_Subprogram_Declaration
6126 Error_Msg_N ("generics not allowed in limited with_clauses", N);
6127 return;
6129 when N_Generic_Instantiation =>
6130 Error_Msg_N
6131 ("generic instantiations not allowed in limited with_clauses",
6133 return;
6135 when N_Generic_Renaming_Declaration =>
6136 Error_Msg_N
6137 ("generic renamings not allowed in limited with_clauses", N);
6138 return;
6140 when N_Subprogram_Renaming_Declaration =>
6141 Error_Msg_N
6142 ("renamed subprograms not allowed in limited with_clauses", N);
6143 return;
6145 when N_Package_Renaming_Declaration =>
6146 Error_Msg_N
6147 ("renamed packages not allowed in limited with_clauses", N);
6148 return;
6150 when others =>
6151 raise Program_Error;
6152 end case;
6154 -- The withed unit may not be analyzed, but the with calause itself
6155 -- must be minimally decorated. This ensures that the checks on unused
6156 -- with clauses also process limieted withs.
6158 Set_Ekind (Pack, E_Package);
6159 Set_Etype (Pack, Standard_Void_Type);
6161 if Is_Entity_Name (Nam) then
6162 Set_Entity (Nam, Pack);
6164 elsif Nkind (Nam) = N_Selected_Component then
6165 Set_Entity (Selector_Name (Nam), Pack);
6166 end if;
6168 -- Check if the chain is already built
6170 Spec := Specification (Unit (Library_Unit (N)));
6172 if Limited_View_Installed (Spec) then
6173 return;
6174 end if;
6176 -- Create the shadow package wich hides the withed unit and provides
6177 -- incomplete view of all types and packages declared within.
6179 Shadow_Pack := Make_Temporary (Sloc (N), 'Z');
6180 Set_Ekind (Shadow_Pack, E_Package);
6181 Set_Is_Internal (Shadow_Pack);
6182 Set_Limited_View (Pack, Shadow_Pack);
6184 -- Inspect the abstract states and visible declarations of the withed
6185 -- unit and create shadow entities that hide existing packages, states,
6186 -- variables and types.
6188 Process_Declarations_And_States
6189 (Pack => Pack,
6190 Decls => Visible_Declarations (Spec),
6191 Scop => Pack,
6192 Create_Abstract_Views => True);
6194 Last_Public_Shadow := Last_Shadow;
6196 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6197 -- to accommodate limited private with clauses.
6199 Process_Declarations_And_States
6200 (Pack => Pack,
6201 Decls => Private_Declarations (Spec),
6202 Scop => Pack,
6203 Create_Abstract_Views => False);
6205 if Present (Last_Public_Shadow) then
6206 Private_Shadow := Next_Entity (Last_Public_Shadow);
6207 else
6208 Private_Shadow := First_Entity (Shadow_Pack);
6209 end if;
6211 Set_First_Private_Entity (Shadow_Pack, Private_Shadow);
6212 Set_Limited_View_Installed (Spec);
6213 end Build_Limited_Views;
6215 ----------------------------
6216 -- Check_No_Elab_Code_All --
6217 ----------------------------
6219 procedure Check_No_Elab_Code_All (N : Node_Id) is
6220 begin
6221 if Present (No_Elab_Code_All_Pragma)
6222 and then In_Extended_Main_Source_Unit (N)
6223 and then Present (Context_Items (N))
6224 then
6225 declare
6226 CL : constant List_Id := Context_Items (N);
6227 CI : Node_Id;
6229 begin
6230 CI := First (CL);
6231 while Present (CI) loop
6232 if Nkind (CI) = N_With_Clause
6233 and then not
6234 No_Elab_Code_All (Get_Source_Unit (Library_Unit (CI)))
6236 -- In GNATprove mode, some runtime units are implicitly
6237 -- loaded to make their entities available for analysis. In
6238 -- this case, ignore violations of No_Elaboration_Code_All
6239 -- for this special analysis mode.
6241 and then not
6242 (GNATprove_Mode and then Implicit_With (CI))
6243 then
6244 Error_Msg_Sloc := Sloc (No_Elab_Code_All_Pragma);
6245 Error_Msg_N
6246 ("violation of No_Elaboration_Code_All#", CI);
6247 Error_Msg_NE
6248 ("\unit& does not have No_Elaboration_Code_All",
6249 CI, Entity (Name (CI)));
6250 end if;
6252 Next (CI);
6253 end loop;
6254 end;
6255 end if;
6256 end Check_No_Elab_Code_All;
6258 -------------------------------
6259 -- Check_Body_Needed_For_SAL --
6260 -------------------------------
6262 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
6263 function Entity_Needs_Body (E : Entity_Id) return Boolean;
6264 -- Determine whether use of entity E might require the presence of its
6265 -- body. For a package this requires a recursive traversal of all nested
6266 -- declarations.
6268 -----------------------
6269 -- Entity_Needs_Body --
6270 -----------------------
6272 function Entity_Needs_Body (E : Entity_Id) return Boolean is
6273 Ent : Entity_Id;
6275 begin
6276 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
6277 return True;
6279 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
6281 -- A generic subprogram always requires the presence of its
6282 -- body because an instantiation needs both templates. The only
6283 -- exceptions is a generic subprogram renaming. In this case the
6284 -- body is needed only when the template is declared outside the
6285 -- compilation unit being checked.
6287 if Present (Renamed_Entity (E)) then
6288 return not Within_Scope (E, Unit_Name);
6289 else
6290 return True;
6291 end if;
6293 elsif Ekind (E) = E_Generic_Package
6294 and then
6295 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
6296 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6297 then
6298 return True;
6300 elsif Ekind (E) = E_Package
6301 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
6302 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6303 then
6304 Ent := First_Entity (E);
6305 while Present (Ent) loop
6306 if Entity_Needs_Body (Ent) then
6307 return True;
6308 end if;
6310 Next_Entity (Ent);
6311 end loop;
6313 return False;
6315 else
6316 return False;
6317 end if;
6318 end Entity_Needs_Body;
6320 -- Start of processing for Check_Body_Needed_For_SAL
6322 begin
6323 if Ekind (Unit_Name) = E_Generic_Package
6324 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6325 N_Generic_Package_Declaration
6326 and then
6327 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
6328 then
6329 Set_Body_Needed_For_SAL (Unit_Name);
6331 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
6332 Set_Body_Needed_For_SAL (Unit_Name);
6334 elsif Is_Subprogram (Unit_Name)
6335 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6336 N_Subprogram_Declaration
6337 and then Has_Pragma_Inline (Unit_Name)
6338 then
6339 Set_Body_Needed_For_SAL (Unit_Name);
6341 elsif Ekind (Unit_Name) = E_Subprogram_Body then
6342 Check_Body_Needed_For_SAL
6343 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6345 elsif Ekind (Unit_Name) = E_Package
6346 and then Entity_Needs_Body (Unit_Name)
6347 then
6348 Set_Body_Needed_For_SAL (Unit_Name);
6350 elsif Ekind (Unit_Name) = E_Package_Body
6351 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
6352 then
6353 Check_Body_Needed_For_SAL
6354 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6355 end if;
6356 end Check_Body_Needed_For_SAL;
6358 --------------------
6359 -- Remove_Context --
6360 --------------------
6362 procedure Remove_Context (N : Node_Id) is
6363 Lib_Unit : constant Node_Id := Unit (N);
6365 begin
6366 -- If this is a child unit, first remove the parent units
6368 if Is_Child_Spec (Lib_Unit) then
6369 Remove_Parents (Lib_Unit);
6370 end if;
6372 Remove_Context_Clauses (N);
6373 end Remove_Context;
6375 ----------------------------
6376 -- Remove_Context_Clauses --
6377 ----------------------------
6379 procedure Remove_Context_Clauses (N : Node_Id) is
6380 Item : Node_Id;
6381 Unit_Name : Entity_Id;
6383 begin
6384 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6385 -- limited-views first and regular-views later (to maintain the
6386 -- stack model).
6388 -- First Phase: Remove limited_with context clauses
6390 Item := First (Context_Items (N));
6391 while Present (Item) loop
6393 -- We are interested only in with clauses which got installed
6394 -- on entry.
6396 if Nkind (Item) = N_With_Clause
6397 and then Limited_Present (Item)
6398 and then Limited_View_Installed (Item)
6399 then
6400 Remove_Limited_With_Clause (Item);
6401 end if;
6403 Next (Item);
6404 end loop;
6406 -- Second Phase: Loop through context items and undo regular
6407 -- with_clauses and use_clauses.
6409 Item := First (Context_Items (N));
6410 while Present (Item) loop
6412 -- We are interested only in with clauses which got installed on
6413 -- entry, as indicated by their Context_Installed flag set
6415 if Nkind (Item) = N_With_Clause
6416 and then Limited_Present (Item)
6417 and then Limited_View_Installed (Item)
6418 then
6419 null;
6421 elsif Nkind (Item) = N_With_Clause
6422 and then Context_Installed (Item)
6423 then
6424 -- Remove items from one with'ed unit
6426 Unit_Name := Entity (Name (Item));
6427 Remove_Unit_From_Visibility (Unit_Name);
6428 Set_Context_Installed (Item, False);
6430 elsif Nkind (Item) = N_Use_Package_Clause then
6431 End_Use_Package (Item);
6433 elsif Nkind (Item) = N_Use_Type_Clause then
6434 End_Use_Type (Item);
6435 end if;
6437 Next (Item);
6438 end loop;
6439 end Remove_Context_Clauses;
6441 --------------------------------
6442 -- Remove_Limited_With_Clause --
6443 --------------------------------
6445 procedure Remove_Limited_With_Clause (N : Node_Id) is
6446 Pack_Decl : constant Entity_Id := Unit (Library_Unit (N));
6448 begin
6449 pragma Assert (Limited_View_Installed (N));
6451 -- Limited with clauses that designate units other than packages are
6452 -- illegal and are never installed.
6454 if Nkind (Pack_Decl) = N_Package_Declaration then
6455 Remove_Limited_With_Unit (Pack_Decl, N);
6456 end if;
6458 -- Indicate that the limited views of the clause have been removed
6460 Set_Limited_View_Installed (N, False);
6461 end Remove_Limited_With_Clause;
6463 ------------------------------
6464 -- Remove_Limited_With_Unit --
6465 ------------------------------
6467 procedure Remove_Limited_With_Unit
6468 (Pack_Decl : Node_Id;
6469 Lim_Clause : Node_Id := Empty)
6471 procedure Remove_Shadow_Entities_From_Visibility (Pack_Id : Entity_Id);
6472 -- Remove the shadow entities of package Pack_Id from direct visibility
6474 procedure Remove_Shadow_Entities_With_Restore (Pack_Id : Entity_Id);
6475 -- Remove the shadow entities of package Pack_Id from direct visibility,
6476 -- restore the corresponding entities they hide into direct visibility,
6477 -- and update the entity and homonym chains.
6479 --------------------------------------------
6480 -- Remove_Shadow_Entities_From_Visibility --
6481 --------------------------------------------
6483 procedure Remove_Shadow_Entities_From_Visibility (Pack_Id : Entity_Id) is
6484 Lim_Header : constant Entity_Id := Limited_View (Pack_Id);
6485 Upto : constant Entity_Id := First_Private_Entity (Lim_Header);
6487 Shadow : Entity_Id;
6489 begin
6490 -- Remove the package from direct visibility
6492 Unchain (Pack_Id);
6493 Set_Is_Immediately_Visible (Pack_Id, False);
6495 -- Remove all shadow entities from direct visibility
6497 Shadow := First_Entity (Lim_Header);
6498 while Present (Shadow) and then Shadow /= Upto loop
6499 Unchain (Shadow);
6500 Next_Entity (Shadow);
6501 end loop;
6502 end Remove_Shadow_Entities_From_Visibility;
6504 -----------------------------------------
6505 -- Remove_Shadow_Entities_With_Restore --
6506 -----------------------------------------
6508 procedure Remove_Shadow_Entities_With_Restore (Pack_Id : Entity_Id) is
6509 procedure Restore_Chain_For_Shadow (Shadow : Entity_Id);
6510 -- Remove shadow entity Shadow by updating the entity and homonym
6511 -- chains.
6513 procedure Restore_Chains
6514 (From : Entity_Id;
6515 Upto : Entity_Id);
6516 -- Remove a sequence of shadow entities starting from From and ending
6517 -- prior to Upto by updating the entity and homonym chains.
6519 procedure Restore_Type_Visibility
6520 (From : Entity_Id;
6521 Upto : Entity_Id);
6522 -- Restore a sequence of types starting from From and ending prior to
6523 -- Upto back in direct visibility.
6525 ------------------------------
6526 -- Restore_Chain_For_Shadow --
6527 ------------------------------
6529 procedure Restore_Chain_For_Shadow (Shadow : Entity_Id) is
6530 Prev : Entity_Id;
6531 Typ : Entity_Id;
6533 begin
6534 -- If the package has incomplete types, the limited view of the
6535 -- incomplete type is in fact never visible (AI05-129) but we
6536 -- have created a shadow entity E1 for it, that points to E2,
6537 -- a nonlimited incomplete type. This in turn has a full view
6538 -- E3 that is the full declaration. There is a corresponding
6539 -- shadow entity E4. When reinstalling the nonlimited view,
6540 -- E2 must become the current entity and E3 must be ignored.
6542 Typ := Non_Limited_View (Shadow);
6544 -- Shadow is the limited view of a full type declaration that has
6545 -- a previous incomplete declaration, i.e. E3 from the previous
6546 -- description. Nothing to insert.
6548 if Present (Current_Entity (Typ))
6549 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
6550 and then Full_View (Current_Entity (Typ)) = Typ
6551 then
6552 return;
6553 end if;
6555 pragma Assert (not In_Chain (Typ));
6557 Prev := Current_Entity (Shadow);
6559 if Prev = Shadow then
6560 Set_Current_Entity (Typ);
6562 else
6563 while Present (Prev) and then Homonym (Prev) /= Shadow loop
6564 Prev := Homonym (Prev);
6565 end loop;
6567 if Present (Prev) then
6568 Set_Homonym (Prev, Typ);
6569 end if;
6570 end if;
6572 Set_Homonym (Typ, Homonym (Shadow));
6573 end Restore_Chain_For_Shadow;
6575 --------------------
6576 -- Restore_Chains --
6577 --------------------
6579 procedure Restore_Chains
6580 (From : Entity_Id;
6581 Upto : Entity_Id)
6583 Shadow : Entity_Id;
6585 begin
6586 Shadow := From;
6587 while Present (Shadow) and then Shadow /= Upto loop
6589 -- Do not unchain nested packages and child units
6591 if Ekind (Shadow) = E_Package then
6592 null;
6594 elsif Is_Child_Unit (Non_Limited_View (Shadow)) then
6595 null;
6597 else
6598 Restore_Chain_For_Shadow (Shadow);
6599 end if;
6601 Next_Entity (Shadow);
6602 end loop;
6603 end Restore_Chains;
6605 -----------------------------
6606 -- Restore_Type_Visibility --
6607 -----------------------------
6609 procedure Restore_Type_Visibility
6610 (From : Entity_Id;
6611 Upto : Entity_Id)
6613 Typ : Entity_Id;
6615 begin
6616 Typ := From;
6617 while Present (Typ) and then Typ /= Upto loop
6618 if Is_Type (Typ) then
6619 Set_Is_Hidden (Typ, Was_Hidden (Typ));
6620 end if;
6622 Next_Entity (Typ);
6623 end loop;
6624 end Restore_Type_Visibility;
6626 -- Local variables
6628 Lim_Header : constant Entity_Id := Limited_View (Pack_Id);
6630 -- Start of processing Remove_Shadow_Entities_With_Restore
6632 begin
6633 -- The limited view of a package is being uninstalled by removing
6634 -- the effects of a limited with clause. If the clause appears in a
6635 -- unit which is not part of the main unit closure, then the related
6636 -- package must not be visible.
6638 if Present (Lim_Clause)
6639 and then not In_Extended_Main_Source_Unit (Lim_Clause)
6640 then
6641 Set_Is_Immediately_Visible (Pack_Id, False);
6643 -- Otherwise a limited view is being overridden by a nonlimited view.
6644 -- Leave the visibility of the package as is because the unit must be
6645 -- visible when the nonlimited view is installed.
6647 else
6648 null;
6649 end if;
6651 -- Remove the shadow entities from visibility by updating the entity
6652 -- and homonym chains.
6654 Restore_Chains
6655 (From => First_Entity (Lim_Header),
6656 Upto => First_Private_Entity (Lim_Header));
6658 -- Reinstate the types that were hidden by the shadow entities back
6659 -- into direct visibility.
6661 Restore_Type_Visibility
6662 (From => First_Entity (Pack_Id),
6663 Upto => First_Private_Entity (Pack_Id));
6664 end Remove_Shadow_Entities_With_Restore;
6666 -- Local variables
6668 Pack_Id : constant Entity_Id := Defining_Entity (Pack_Decl);
6670 -- Start of processing for Remove_Limited_With_Unit
6672 begin
6673 -- Nothing to do when the limited view of the package is not installed
6675 if not From_Limited_With (Pack_Id) then
6676 return;
6677 end if;
6679 if Debug_Flag_I then
6680 Write_Str ("remove limited view of ");
6681 Write_Name (Chars (Pack_Id));
6682 Write_Str (" from visibility");
6683 Write_Eol;
6684 end if;
6686 -- The package already appears in the compilation closure. As a result,
6687 -- its shadow entities must be replaced by the real entities they hide
6688 -- and the previously hidden entities must be entered back into direct
6689 -- visibility.
6691 -- WARNING: This code must be kept synchronized with that of routine
6692 -- Install_Limited_Withed_Clause.
6694 if Analyzed (Pack_Decl) then
6695 Remove_Shadow_Entities_With_Restore (Pack_Id);
6697 -- Otherwise the package is not analyzed and its shadow entities must be
6698 -- removed from direct visibility.
6700 else
6701 Remove_Shadow_Entities_From_Visibility (Pack_Id);
6702 end if;
6704 -- Indicate that the limited view of the package is not installed
6706 Set_From_Limited_With (Pack_Id, False);
6707 end Remove_Limited_With_Unit;
6709 --------------------
6710 -- Remove_Parents --
6711 --------------------
6713 procedure Remove_Parents (Lib_Unit : Node_Id) is
6714 P : Node_Id;
6715 P_Name : Entity_Id;
6716 P_Spec : Node_Id := Empty;
6717 E : Entity_Id;
6718 Vis : constant Boolean :=
6719 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
6721 begin
6722 if Is_Child_Spec (Lib_Unit) then
6723 P_Spec := Parent_Spec (Lib_Unit);
6725 elsif Nkind (Lib_Unit) = N_Package_Body
6726 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6727 then
6728 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6729 end if;
6731 if Present (P_Spec) then
6732 P := Unit (P_Spec);
6733 P_Name := Get_Parent_Entity (P);
6734 Remove_Context_Clauses (P_Spec);
6735 End_Package_Scope (P_Name);
6736 Set_Is_Immediately_Visible (P_Name, Vis);
6738 -- Remove from visibility the siblings as well, which are directly
6739 -- visible while the parent is in scope.
6741 E := First_Entity (P_Name);
6742 while Present (E) loop
6743 if Is_Child_Unit (E) then
6744 Set_Is_Immediately_Visible (E, False);
6745 end if;
6747 Next_Entity (E);
6748 end loop;
6750 Set_In_Package_Body (P_Name, False);
6752 -- This is the recursive call to remove the context of any higher
6753 -- level parent. This recursion ensures that all parents are removed
6754 -- in the reverse order of their installation.
6756 Remove_Parents (P);
6757 end if;
6758 end Remove_Parents;
6760 ---------------------------------
6761 -- Remove_Private_With_Clauses --
6762 ---------------------------------
6764 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6765 Item : Node_Id;
6767 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6768 -- Check whether a given unit appears in a regular with_clause. Used to
6769 -- determine whether a private_with_clause, implicit or explicit, should
6770 -- be ignored.
6772 ----------------------------
6773 -- In_Regular_With_Clause --
6774 ----------------------------
6776 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6778 Item : Node_Id;
6780 begin
6781 Item := First (Context_Items (Comp_Unit));
6782 while Present (Item) loop
6783 if Nkind (Item) = N_With_Clause
6785 -- The following guard is needed to ensure that the name has
6786 -- been properly analyzed before we go fetching its entity.
6788 and then Is_Entity_Name (Name (Item))
6789 and then Entity (Name (Item)) = E
6790 and then not Private_Present (Item)
6791 then
6792 return True;
6793 end if;
6794 Next (Item);
6795 end loop;
6797 return False;
6798 end In_Regular_With_Clause;
6800 -- Start of processing for Remove_Private_With_Clauses
6802 begin
6803 Item := First (Context_Items (Comp_Unit));
6804 while Present (Item) loop
6805 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
6807 -- If private_with_clause is redundant, remove it from context,
6808 -- as a small optimization to subsequent handling of private_with
6809 -- clauses in other nested packages. We replace the clause with
6810 -- a null statement, which is otherwise ignored by the rest of
6811 -- the compiler, so that ASIS tools can reconstruct the source.
6813 if In_Regular_With_Clause (Entity (Name (Item))) then
6814 declare
6815 Nxt : constant Node_Id := Next (Item);
6816 begin
6817 Rewrite (Item, Make_Null_Statement (Sloc (Item)));
6818 Analyze (Item);
6819 Item := Nxt;
6820 end;
6822 elsif Limited_Present (Item) then
6823 if not Limited_View_Installed (Item) then
6824 Remove_Limited_With_Clause (Item);
6825 end if;
6827 Next (Item);
6829 else
6830 Remove_Unit_From_Visibility (Entity (Name (Item)));
6831 Set_Context_Installed (Item, False);
6832 Next (Item);
6833 end if;
6835 else
6836 Next (Item);
6837 end if;
6838 end loop;
6839 end Remove_Private_With_Clauses;
6841 ---------------------------------
6842 -- Remove_Unit_From_Visibility --
6843 ---------------------------------
6845 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6846 begin
6847 if Debug_Flag_I then
6848 Write_Str ("remove unit ");
6849 Write_Name (Chars (Unit_Name));
6850 Write_Str (" from visibility");
6851 Write_Eol;
6852 end if;
6854 Set_Is_Visible_Lib_Unit (Unit_Name, False);
6855 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6856 Set_Is_Immediately_Visible (Unit_Name, False);
6858 -- If the unit is a wrapper package, the subprogram instance is
6859 -- what must be removed from visibility.
6860 -- Should we use Related_Instance instead???
6862 if Is_Wrapper_Package (Unit_Name) then
6863 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
6864 end if;
6865 end Remove_Unit_From_Visibility;
6867 --------
6868 -- sm --
6869 --------
6871 procedure sm is
6872 begin
6873 null;
6874 end sm;
6876 -------------
6877 -- Unchain --
6878 -------------
6880 procedure Unchain (E : Entity_Id) is
6881 Prev : Entity_Id;
6883 begin
6884 Prev := Current_Entity (E);
6886 if No (Prev) then
6887 return;
6889 elsif Prev = E then
6890 Set_Name_Entity_Id (Chars (E), Homonym (E));
6892 else
6893 while Present (Prev) and then Homonym (Prev) /= E loop
6894 Prev := Homonym (Prev);
6895 end loop;
6897 if Present (Prev) then
6898 Set_Homonym (Prev, Homonym (E));
6899 end if;
6900 end if;
6902 if Debug_Flag_I then
6903 Write_Str (" (homonym) unchain ");
6904 Write_Name (Chars (E));
6905 Write_Eol;
6906 end if;
6907 end Unchain;
6909 end Sem_Ch10;