testsuite: Fix up pr111150* tests on i686-linux [PR111150]
[official-gcc.git] / gcc / ada / sem_ch10.adb
blob73e5388affdc08f805dfd25756c35315b83299d6
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-2024, 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 Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Errout; use Errout;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Put_Image;
36 with Exp_Util; use Exp_Util;
37 with Elists; use Elists;
38 with Fname; use Fname;
39 with Fname.UF; use Fname.UF;
40 with Freeze; use Freeze;
41 with Impunit; use Impunit;
42 with Inline; use Inline;
43 with Lib; use Lib;
44 with Lib.Load; use Lib.Load;
45 with Lib.Xref; use Lib.Xref;
46 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Output; use Output;
51 with Par_SCO; use Par_SCO;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
55 with Sem; use Sem;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Ch3; use Sem_Ch3;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch7; use Sem_Ch7;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinfo.Nodes; use Sinfo.Nodes;
69 with Sinfo.Utils; use Sinfo.Utils;
70 with Sinfo.CN; use Sinfo.CN;
71 with Sinput; use Sinput;
72 with Snames; use Snames;
73 with Style; use Style;
74 with Stylesw; use Stylesw;
75 with Tbuild; use Tbuild;
76 with Uname; use Uname;
77 with Warnsw; use Warnsw;
79 package body Sem_Ch10 is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Analyze_Context (N : Node_Id);
86 -- Analyzes items in the context clause of compilation unit
88 procedure Analyze_Required_Limited_With_Units (N : Node_Id);
89 -- Subsidiary of Analyze_Compilation_Unit. Perform full analysis of the
90 -- limited-with units of N when it is a package declaration that does not
91 -- require a package body, and the profile of some subprogram defined in N
92 -- depends on shadow incomplete type entities visible through limited-with
93 -- context clauses. This analysis is required to provide the backend with
94 -- the non-limited view of these shadow entities.
96 procedure Build_Limited_Views (N : Node_Id);
97 -- Build and decorate the list of shadow entities for a package mentioned
98 -- in a limited_with clause. If the package was not previously analyzed
99 -- then it also performs a basic decoration of the real entities. This is
100 -- required in order to avoid passing non-decorated entities to the
101 -- back-end. Implements Ada 2005 (AI-50217).
103 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
104 -- Common processing for all stubs (subprograms, tasks, packages, and
105 -- protected cases). N is the stub to be analyzed. Once the subunit name
106 -- is established, load and analyze. Nam is the non-overloadable entity
107 -- for which the proper body provides a completion. Subprogram stubs are
108 -- handled differently because they can be declarations.
110 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
111 -- Check whether the source for the body of a compilation unit must be
112 -- included in a standalone library.
114 procedure Check_No_Elab_Code_All (N : Node_Id);
115 -- Carries out possible tests for violation of No_Elab_Code all for withed
116 -- units in the Context_Items of unit N.
118 procedure Check_Private_Child_Unit (N : Node_Id);
119 -- If a with_clause mentions a private child unit, the compilation unit
120 -- must be a member of the same family, as described in 10.1.2.
122 procedure Check_Stub_Level (N : Node_Id);
123 -- Verify that a stub is declared immediately within a compilation unit,
124 -- and not in an inner frame.
126 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
127 -- When a child unit appears in a context clause, the implicit withs on
128 -- parents are made explicit, and with clauses are inserted in the context
129 -- clause before the one for the child. If a parent in the with_clause
130 -- is a renaming, the implicit with_clause is on the renaming whose name
131 -- is mentioned in the with_clause, and not on the package it renames.
132 -- N is the compilation unit whose list of context items receives the
133 -- implicit with_clauses.
135 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
136 -- Generate cross-reference information for the parents of child units
137 -- and of subunits. N is a defining_program_unit_name, and P_Id is the
138 -- immediate parent scope.
140 function Has_With_Clause
141 (C_Unit : Node_Id;
142 Pack : Entity_Id;
143 Is_Limited : Boolean := False) return Boolean;
144 -- Determine whether compilation unit C_Unit contains a [limited] with
145 -- clause for package Pack. Use the flag Is_Limited to designate desired
146 -- clause kind.
148 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
149 -- If the main unit is a child unit, implicit withs are also added for
150 -- all its ancestors.
152 function In_Chain (E : Entity_Id) return Boolean;
153 -- Check that the shadow entity is not already in the homonym chain, for
154 -- example through a limited_with clause in a parent unit.
156 procedure Install_Context_Clauses (N : Node_Id; Chain : Boolean := True);
157 -- Subsidiary to Install_Context and Install_Parents. Process all with
158 -- and use clauses for current unit and its library unit if any. The flag
159 -- Chain is used to control the "chaining" or linking together of use-type
160 -- and use-package clauses to avoid circularities with reinstalling
161 -- clauses.
163 procedure Install_Limited_Context_Clauses (N : Node_Id);
164 -- Subsidiary to Install_Context. Process only limited with_clauses for
165 -- current unit. Implements Ada 2005 (AI-50217).
167 procedure Install_Limited_With_Clause (N : Node_Id);
168 -- Place shadow entities for a limited_with package in the visibility
169 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
171 procedure Install_Parents
172 (Lib_Unit : Node_Id;
173 Is_Private : Boolean;
174 Chain : Boolean := True);
175 -- This procedure establishes the context for the compilation of a child
176 -- unit. If Lib_Unit is a child library spec then the context of the parent
177 -- is installed, and the parent itself made immediately visible, so that
178 -- the child unit is processed in the declarative region of the parent.
179 -- Install_Parents makes a recursive call to itself to ensure that all
180 -- parents are loaded in the nested case. If Lib_Unit is a library body,
181 -- the only effect of Install_Parents is to install the private decls of
182 -- the parents, because the visible parent declarations will have been
183 -- installed as part of the context of the corresponding spec. The flag
184 -- Chain is used to control the "chaining" or linking of use-type and
185 -- use-package clauses to avoid circularities when installing context.
187 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
188 -- In the compilation of a child unit, a child of any of the ancestor
189 -- units is directly visible if it is visible, because the parent is in
190 -- an enclosing scope. Iterate over context to find child units of U_Name
191 -- or of some ancestor of it.
193 procedure Install_With_Clause
194 (With_Clause : Node_Id;
195 Private_With_OK : Boolean := False);
196 -- If the unit is not a child unit, make unit immediately visible. The
197 -- caller ensures that the unit is not already currently installed. The
198 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
199 -- is called when compiling the private part of a package, or installing
200 -- the private declarations of a parent unit.
202 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
203 -- When compiling a unit Q descended from some parent unit P, a limited
204 -- with_clause in the context of P that names some other ancestor of Q
205 -- must not be installed because the ancestor is immediately visible.
207 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
208 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
209 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
210 -- a library spec that has a parent. If the call to Is_Child_Spec returns
211 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
212 -- compilation unit for the parent spec.
214 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
215 -- Parent_Spec is non-empty, this is also a child unit.
217 procedure Remove_Context_Clauses (N : Node_Id);
218 -- Subsidiary of previous one. Remove use_ and with_clauses
220 procedure Remove_Limited_With_Clause (N : Node_Id);
221 -- Remove the shadow entities from visibility introduced for a package
222 -- mentioned in limited with clause N. Implements Ada 2005 (AI-50217).
224 procedure Remove_Limited_With_Unit
225 (Pack_Decl : Node_Id;
226 Lim_Clause : Node_Id := Empty);
227 -- Remove the shadow entities from visibility introduced for a package
228 -- denoted by declaration Pack_Decl. Lim_Clause is the related limited
229 -- with clause, if any. Implements Ada 2005 (AI-50217).
231 procedure Remove_Parents (Lib_Unit : Node_Id);
232 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
233 -- contexts established by the corresponding call to Install_Parents are
234 -- removed. Remove_Parents contains a recursive call to itself to ensure
235 -- that all parents are removed in the nested case.
237 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
238 -- Reset all visibility flags on unit after compiling it, either as a main
239 -- unit or as a unit in the context.
241 procedure Replace (Old_E, New_E : Entity_Id);
242 -- Replace Old_E by New_E on visibility list
244 procedure Unchain (E : Entity_Id);
245 -- Remove single entity from visibility list
247 procedure sm;
248 -- A dummy procedure, for debugging use, called just before analyzing the
249 -- main unit (after dealing with any context clauses).
251 --------------------------
252 -- Limited_With_Clauses --
253 --------------------------
255 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
256 -- mutually recursive types declared in different units. A limited_with
257 -- clause that names package P in the context of unit U makes the types
258 -- declared in the visible part of P available within U, but with the
259 -- restriction that these types can only be used as incomplete types.
260 -- The limited_with clause does not impose a semantic dependence on P,
261 -- and it is possible for two packages to have limited_with_clauses on
262 -- each other without creating an elaboration circularity.
264 -- To support this feature, the analysis of a limited_with clause must
265 -- create an abbreviated view of the package, without performing any
266 -- semantic analysis on it. This "package abstract" contains shadow types
267 -- that are in one-one correspondence with the real types in the package,
268 -- and that have the properties of incomplete types.
270 -- The implementation creates two element lists: one to chain the shadow
271 -- entities, and one to chain the corresponding type entities in the tree
272 -- of the package. Links between corresponding entities in both chains
273 -- allow the compiler to select the proper view of a given type, depending
274 -- on the context. Note that in contrast with the handling of private
275 -- types, the limited view and the nonlimited view of a type are treated
276 -- as separate entities, and no entity exchange needs to take place, which
277 -- makes the implementation much simpler than could be feared.
279 ------------------------------
280 -- Analyze_Compilation_Unit --
281 ------------------------------
283 procedure Analyze_Compilation_Unit (N : Node_Id) is
284 Unit_Node : constant Node_Id := Unit (N);
286 procedure Check_Redundant_Withs
287 (Context_Items : List_Id;
288 Spec_Context_Items : List_Id := No_List);
289 -- Determine whether the context list of a compilation unit contains
290 -- redundant with clauses. When checking body clauses against spec
291 -- clauses, set Context_Items to the context list of the body and
292 -- Spec_Context_Items to that of the spec. Parent packages are not
293 -- examined for documentation purposes.
295 function Install_Inherited_Policy_Pragmas
296 (Comp_Unit : Node_Id) return Node_Id;
297 -- Install assertion_policy pragmas placed at the start of the spec of
298 -- the given compilation unit (and the spec of its parent units). Return
299 -- the last pragma found in the check policy list before installing
300 -- these pragmas; used to remove the installed pragmas.
302 procedure Remove_Inherited_Policy_Pragmas (Last_Pragma : Node_Id);
303 -- Remove assertion_policy pragmas installed after the given pragma. If
304 -- Last_Pragma is empty then remove all the pragmas installed in the
305 -- check policy list (if any).
307 ---------------------------
308 -- Check_Redundant_Withs --
309 ---------------------------
311 procedure Check_Redundant_Withs
312 (Context_Items : List_Id;
313 Spec_Context_Items : List_Id := No_List)
315 Clause : Node_Id;
317 procedure Process_Body_Clauses
318 (Context_List : List_Id;
319 Clause : Node_Id;
320 Used : out Boolean;
321 Used_Type_Or_Elab : out Boolean);
322 -- Examine the context clauses of a package body, trying to match the
323 -- name entity of Clause with any list element. If the match occurs
324 -- on a use package clause set Used to True, for a use type clause or
325 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
327 procedure Process_Spec_Clauses
328 (Context_List : List_Id;
329 Clause : Node_Id;
330 Used : out Boolean;
331 Withed : out Boolean;
332 Exit_On_Self : Boolean := False);
333 -- Examine the context clauses of a package spec, trying to match
334 -- the name entity of Clause with any list element. If the match
335 -- occurs on a use package clause, set Used to True, for a with
336 -- package clause other than Clause, set Withed to True. Limited
337 -- with clauses, implicitly generated with clauses and withs
338 -- having pragmas Elaborate or Elaborate_All applied to them are
339 -- skipped. Exit_On_Self is used to control the search loop and
340 -- force an exit whenever Clause sees itself in the search.
342 --------------------------
343 -- Process_Body_Clauses --
344 --------------------------
346 procedure Process_Body_Clauses
347 (Context_List : List_Id;
348 Clause : Node_Id;
349 Used : out Boolean;
350 Used_Type_Or_Elab : out Boolean)
352 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
353 Cont_Item : Node_Id;
354 Prag_Unit : Node_Id;
355 Use_Item : Node_Id;
357 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
358 -- In an expanded name in a use clause, if the prefix is a renamed
359 -- package, the entity is set to the original package as a result,
360 -- when checking whether the package appears in a previous with
361 -- clause, the renaming has to be taken into account, to prevent
362 -- spurious/incorrect warnings. A common case is use of Text_IO.
364 ---------------
365 -- Same_Unit --
366 ---------------
368 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
369 begin
370 return Entity (N) = P
371 or else (Present (Renamed_Entity (P))
372 and then Entity (N) = Renamed_Entity (P));
373 end Same_Unit;
375 -- Start of processing for Process_Body_Clauses
377 begin
378 Used := False;
379 Used_Type_Or_Elab := False;
381 Cont_Item := First (Context_List);
382 while Present (Cont_Item) loop
384 -- Package use clause
386 if Nkind (Cont_Item) = N_Use_Package_Clause
387 and then not Used
388 then
389 -- Search through use clauses
391 Use_Item := Name (Cont_Item);
393 -- Case of a direct use of the one we are looking for
395 if Entity (Use_Item) = Nam_Ent then
396 Used := True;
398 -- Handle nested case, as in "with P; use P.Q.R"
400 else
401 declare
402 UE : Node_Id;
404 begin
405 -- Loop through prefixes looking for match
407 UE := Use_Item;
408 while Nkind (UE) = N_Expanded_Name loop
409 if Same_Unit (Prefix (UE), Nam_Ent) then
410 Used := True;
411 exit;
412 end if;
414 UE := Prefix (UE);
415 end loop;
416 end;
417 end if;
419 -- USE TYPE clause
421 elsif Nkind (Cont_Item) = N_Use_Type_Clause
422 and then not Used_Type_Or_Elab
423 then
424 declare
425 UE : Node_Id;
427 begin
428 -- Loop through prefixes looking for a match
430 UE := Prefix (Subtype_Mark (Cont_Item));
431 loop
432 if not Used_Type_Or_Elab
433 and then Same_Unit (UE, Nam_Ent)
434 then
435 Used_Type_Or_Elab := True;
436 end if;
438 exit when Nkind (UE) /= N_Expanded_Name;
439 UE := Prefix (UE);
440 end loop;
441 end;
443 -- Pragma Elaborate or Elaborate_All
445 elsif Nkind (Cont_Item) = N_Pragma
446 and then
447 Pragma_Name_Unmapped (Cont_Item)
448 in Name_Elaborate | Name_Elaborate_All
449 and then not Used_Type_Or_Elab
450 then
451 Prag_Unit :=
452 First (Pragma_Argument_Associations (Cont_Item));
453 while Present (Prag_Unit) and then not Used_Type_Or_Elab loop
454 if Entity (Expression (Prag_Unit)) = Nam_Ent then
455 Used_Type_Or_Elab := True;
456 end if;
458 Next (Prag_Unit);
459 end loop;
460 end if;
462 Next (Cont_Item);
463 end loop;
464 end Process_Body_Clauses;
466 --------------------------
467 -- Process_Spec_Clauses --
468 --------------------------
470 procedure Process_Spec_Clauses
471 (Context_List : List_Id;
472 Clause : Node_Id;
473 Used : out Boolean;
474 Withed : out Boolean;
475 Exit_On_Self : Boolean := False)
477 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
478 Cont_Item : Node_Id;
480 begin
481 Used := False;
482 Withed := False;
484 Cont_Item := First (Context_List);
485 while Present (Cont_Item) loop
487 -- Stop the search since the context items after Cont_Item have
488 -- already been examined in a previous iteration of the reverse
489 -- loop in Check_Redundant_Withs.
491 if Exit_On_Self
492 and Cont_Item = Clause
493 then
494 exit;
495 end if;
497 -- Package use clause
499 if Nkind (Cont_Item) = N_Use_Package_Clause
500 and then not Used
501 then
502 if Entity (Name (Cont_Item)) = Nam_Ent then
503 Used := True;
504 end if;
506 -- Package with clause. Avoid processing self, implicitly
507 -- generated with clauses or limited with clauses. Note that
508 -- we examine with clauses having pragmas Elaborate or
509 -- Elaborate_All applied to them due to cases such as:
511 -- with Pack;
512 -- with Pack;
513 -- pragma Elaborate (Pack);
515 -- In this case, the second with clause is redundant since
516 -- the pragma applies only to the first "with Pack;".
518 -- Note that we only consider with_clauses that comes from
519 -- source. In the case of renamings used as prefixes of names
520 -- in with_clauses, we generate a with_clause for the prefix,
521 -- which we do not treat as implicit because it is needed for
522 -- visibility analysis, but is also not redundant.
524 elsif Nkind (Cont_Item) = N_With_Clause
525 and then Comes_From_Source (Cont_Item)
526 and then not Implicit_With (Cont_Item)
527 and then not Limited_Present (Cont_Item)
528 and then Cont_Item /= Clause
529 and then Entity (Name (Cont_Item)) = Nam_Ent
530 then
531 Withed := True;
532 end if;
534 Next (Cont_Item);
535 end loop;
536 end Process_Spec_Clauses;
538 -- Start of processing for Check_Redundant_Withs
540 begin
541 Clause := Last (Context_Items);
542 while Present (Clause) loop
544 -- Avoid checking implicitly generated with clauses, limited with
545 -- clauses or withs that have pragma Elaborate or Elaborate_All.
547 if Nkind (Clause) = N_With_Clause
548 and then not Implicit_With (Clause)
549 and then not Limited_Present (Clause)
550 and then not Elaborate_Present (Clause)
552 -- With_clauses introduced for renamings of parent clauses
553 -- are not marked implicit because they need to be properly
554 -- installed, but they do not come from source and do not
555 -- require warnings.
557 and then Comes_From_Source (Clause)
558 then
559 -- Package body-to-spec check
561 if Present (Spec_Context_Items) then
562 declare
563 Used_In_Body : Boolean;
564 Used_In_Spec : Boolean;
565 Used_Type_Or_Elab : Boolean;
566 Withed_In_Spec : Boolean;
568 begin
569 Process_Spec_Clauses
570 (Context_List => Spec_Context_Items,
571 Clause => Clause,
572 Used => Used_In_Spec,
573 Withed => Withed_In_Spec);
575 Process_Body_Clauses
576 (Context_List => Context_Items,
577 Clause => Clause,
578 Used => Used_In_Body,
579 Used_Type_Or_Elab => Used_Type_Or_Elab);
581 -- "Type Elab" refers to the presence of either a use
582 -- type clause, pragmas Elaborate or Elaborate_All.
584 -- +---------------+---------------------------+------+
585 -- | Spec | Body | Warn |
586 -- +--------+------+--------+------+-----------+------+
587 -- | Withed | Used | Withed | Used | Type Elab | |
588 -- | X | | X | | | X |
589 -- | X | | X | X | | |
590 -- | X | | X | | X | |
591 -- | X | | X | X | X | |
592 -- | X | X | X | | | X |
593 -- | X | X | X | | X | |
594 -- | X | X | X | X | | X |
595 -- | X | X | X | X | X | |
596 -- +--------+------+--------+------+-----------+------+
598 if (Withed_In_Spec
599 and then not Used_Type_Or_Elab)
600 and then
601 ((not Used_In_Spec and then not Used_In_Body)
602 or else Used_In_Spec)
603 then
604 Error_Msg_N -- CODEFIX
605 ("redundant with clause in body?r?", Clause);
606 end if;
607 end;
609 -- Standalone package spec or body check
611 else
612 if Is_Ancestor_Package (Entity (Name (Clause)),
613 Defining_Entity (Unit_Node))
614 then
615 Error_Msg_N
616 ("unnecessary with of ancestor?r?", Clause);
617 end if;
619 declare
620 Dummy : Boolean := False;
621 Withed : Boolean := False;
623 begin
624 -- The mechanism for examining the context clauses of a
625 -- package spec can be applied to package body clauses.
627 Process_Spec_Clauses
628 (Context_List => Context_Items,
629 Clause => Clause,
630 Used => Dummy,
631 Withed => Withed,
632 Exit_On_Self => True);
634 if Withed then
635 Error_Msg_N -- CODEFIX
636 ("redundant with clause?r?", Clause);
637 end if;
638 end;
639 end if;
640 end if;
642 Prev (Clause);
643 end loop;
644 end Check_Redundant_Withs;
646 --------------------------------------
647 -- Install_Inherited_Policy_Pragmas --
648 --------------------------------------
650 -- Opt.Check_Policy_List is handled as a stack; assertion policy
651 -- pragmas defined at inner scopes are placed at the beginning of
652 -- the list. Therefore, policy pragmas defined at the start of
653 -- parent units must be appended to the end of this list.
655 -- When the compilation unit is a package body (or a subprogram body
656 -- that does not act as its spec) we recursively traverse to its spec
657 -- (and from there to its ultimate parent); when the compilation unit
658 -- is a child package (or subprogram) spec we recursively climb until
659 -- its ultimate parent. In both cases policy pragmas defined at the
660 -- beginning of all these traversed units are appended to the check
661 -- policy list in the way back to the current compilation unit (and
662 -- they are left installed in reverse order). For example:
664 -- pragma Assertion_Policy (...) -- [policy-1]
665 -- package Pkg is ...
667 -- pragma Assertion_Policy (...) -- [policy-2]
668 -- package Pkg.Child is ...
670 -- pragma Assertion_Policy (...) -- [policy-3]
671 -- package body Pkg.Child is ...
673 -- When the compilation unit Pkg.Child is analyzed, and its context
674 -- clauses are analyzed, these are the contents of Check_Policy_List:
676 -- Opt.Check_Policy_List -> [policy-3]
677 -- ^
678 -- last_policy_pragma
680 -- After climbing to the ultimate parent spec, these are the contents
681 -- of Check_Policy_List:
683 -- Opt.Check_Policy_List -> [policy-3] -> [policy-2] -> [policy-1]
684 -- ^
685 -- last_policy_pragma
687 -- The reference to the last policy pragma in the initial contents of
688 -- the list is used later to remove installed inherited pragmas.
690 function Install_Inherited_Policy_Pragmas
691 (Comp_Unit : Node_Id) return Node_Id
693 Last_Policy_Pragma : Node_Id;
695 procedure Install_Parent_Policy_Pragmas (N : Node_Id);
696 -- Recursively climb to the ultimate parent and install their policy
697 -- pragmas after Last_Policy_Pragma.
699 -----------------------------------
700 -- Install_Parent_Policy_Pragmas --
701 -----------------------------------
703 procedure Install_Parent_Policy_Pragmas (N : Node_Id) is
704 Lib_Unit : constant Node_Id := Unit (N);
705 Item : Node_Id;
707 begin
708 if Is_Child_Spec (Lib_Unit) then
709 Install_Parent_Policy_Pragmas (Parent_Spec (Lib_Unit));
711 elsif Nkind (Lib_Unit) = N_Package_Body then
712 Install_Parent_Policy_Pragmas (Library_Unit (N));
714 elsif Nkind (Lib_Unit) = N_Subprogram_Body
715 and then not Acts_As_Spec (N)
716 then
717 Install_Parent_Policy_Pragmas (Library_Unit (N));
718 end if;
720 -- Search for check policy pragmas defined at the start of the
721 -- context items. They are not part of the context clause, but
722 -- that is where the parser places them.
724 Item := First (Context_Items (N));
725 while Present (Item)
726 and then Nkind (Item) = N_Pragma
727 and then Pragma_Name (Item) in Configuration_Pragma_Names
728 loop
729 if Pragma_Name (Item) = Name_Check_Policy then
730 if No (Last_Policy_Pragma) then
731 Set_Next_Pragma (Item, Opt.Check_Policy_List);
732 Opt.Check_Policy_List := Item;
734 else
735 Set_Next_Pragma (Item, Next_Pragma (Last_Policy_Pragma));
736 Set_Next_Pragma (Last_Policy_Pragma, Item);
737 end if;
738 end if;
740 Next (Item);
741 end loop;
742 end Install_Parent_Policy_Pragmas;
744 -- Local variables
746 Lib_Unit : constant Node_Id := Unit (Comp_Unit);
748 -- Start of processing for Install_Inherited_Policy_Pragmas
750 begin
751 -- Search for the last configuration pragma of the current
752 -- compilation unit in the check policy list. These pragmas were
753 -- added to the ckeck policy list as part of the analysis of the
754 -- context of the current compilation unit (because, although
755 -- configuration pragmas are not part of the context clauses,
756 -- they are placed there by the parser).
758 Last_Policy_Pragma := Opt.Check_Policy_List;
760 if Present (Last_Policy_Pragma) then
761 while Present (Next_Pragma (Last_Policy_Pragma)) loop
762 Last_Policy_Pragma := Next_Pragma (Last_Policy_Pragma);
763 end loop;
764 end if;
766 -- We must not install configuration pragmas of the current unit
767 -- because they have been installed by Analyze_Context (see previous
768 -- comment).
770 if Is_Child_Spec (Lib_Unit) then
771 Install_Parent_Policy_Pragmas (Parent_Spec (Lib_Unit));
773 elsif Nkind (Lib_Unit) = N_Package_Body then
774 Install_Parent_Policy_Pragmas (Library_Unit (Comp_Unit));
776 elsif Nkind (Lib_Unit) = N_Subprogram_Body
777 and then not Acts_As_Spec (Comp_Unit)
778 then
779 Install_Parent_Policy_Pragmas (Library_Unit (Comp_Unit));
780 end if;
782 return Last_Policy_Pragma;
783 end Install_Inherited_Policy_Pragmas;
785 -------------------------------------
786 -- Remove_Inherited_Policy_Pragmas --
787 -------------------------------------
789 procedure Remove_Inherited_Policy_Pragmas (Last_Pragma : Node_Id) is
790 Curr_Prag : Node_Id;
791 Next_Prag : Node_Id;
793 begin
794 if No (Opt.Check_Policy_List) then
795 return;
796 end if;
798 -- If this unit does not have assertion_policy pragmas, then all the
799 -- pragmas installed in the check policy list were inherited and must
800 -- be removed from the list.
802 if No (Last_Pragma) then
803 Curr_Prag := Opt.Check_Policy_List;
805 -- Otherwise, pragmas installed after Last_Pragma must be removed.
807 else
808 Curr_Prag := Last_Pragma;
809 end if;
811 -- Remove pragmas from the list
813 Next_Prag := Next_Pragma (Curr_Prag);
814 while Present (Next_Prag) loop
815 Set_Next_Pragma (Curr_Prag, Empty);
817 Curr_Prag := Next_Prag;
818 Next_Prag := Next_Pragma (Curr_Prag);
819 end loop;
821 if No (Last_Pragma) then
822 Opt.Check_Policy_List := Empty;
823 end if;
824 end Remove_Inherited_Policy_Pragmas;
826 -- Local variables
828 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
829 Lib_Unit : Node_Id := Library_Unit (N);
830 Par_Spec_Name : Unit_Name_Type;
831 Spec_Id : Entity_Id;
832 Unum : Unit_Number_Type;
833 Options : Style_Check_Options;
835 Last_Policy_Pragma : Node_Id;
836 -- Last policy pragma of this compilation unit installed in the check
837 -- policy list when its context is analyzed (see Analyze_Context); this
838 -- node is used as a reference to remove from this list policy pragmas
839 -- inherited from parent units.
841 -- Start of processing for Analyze_Compilation_Unit
843 begin
844 Exp_Put_Image.Preload_Root_Buffer_Type (N);
846 Process_Compilation_Unit_Pragmas (N);
848 -- If the unit is a subunit whose parent has not been analyzed (which
849 -- indicates that the main unit is a subunit, either the current one or
850 -- one of its descendants) then the subunit is compiled as part of the
851 -- analysis of the parent, which we proceed to do. Basically this gets
852 -- handled from the top down and we don't want to do anything at this
853 -- level (i.e. this subunit will be handled on the way down from the
854 -- parent), so at this level we immediately return. If the subunit ends
855 -- up not analyzed, it means that the parent did not contain a stub for
856 -- it, or that there errors were detected in some ancestor.
858 if Nkind (Unit_Node) = N_Subunit and then not Analyzed (Lib_Unit) then
859 Semantics (Lib_Unit);
861 if not Analyzed (Proper_Body (Unit_Node)) then
862 if Serious_Errors_Detected > 0 then
863 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
864 else
865 Error_Msg_N ("missing stub for subunit", N);
866 end if;
867 end if;
869 return;
870 end if;
872 -- Analyze context (this will call Sem recursively for with'ed units) To
873 -- detect circularities among with-clauses that are not caught during
874 -- loading, we set the Context_Pending flag on the current unit. If the
875 -- flag is already set there is a potential circularity. We exclude
876 -- predefined units from this check because they are known to be safe.
877 -- We also exclude package bodies that are present because circularities
878 -- between bodies are harmless (and necessary).
880 if Context_Pending (N) then
881 declare
882 Circularity : Boolean := True;
884 begin
885 if In_Predefined_Unit (N) then
886 Circularity := False;
888 else
889 for U in Main_Unit + 1 .. Last_Unit loop
890 if Nkind (Unit (Cunit (U))) = N_Package_Body
891 and then not Analyzed (Cunit (U))
892 then
893 Circularity := False;
894 exit;
895 end if;
896 end loop;
897 end if;
899 if Circularity then
900 Error_Msg_N ("circular dependency caused by with_clauses", N);
901 Error_Msg_N
902 ("\possibly missing limited_with clause"
903 & " in one of the following", N);
905 for U in Main_Unit .. Last_Unit loop
906 if Context_Pending (Cunit (U)) then
907 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
908 Error_Msg_N ("\unit$", N);
909 end if;
910 end loop;
912 raise Unrecoverable_Error;
913 end if;
914 end;
915 else
916 Set_Context_Pending (N);
917 end if;
919 -- Store the style check options before analyzing context pragmas that
920 -- might change them for this compilation unit.
922 Save_Style_Check_Options (Options);
924 Analyze_Context (N);
926 Set_Context_Pending (N, False);
928 -- If the unit is a package body, the spec is already loaded and must be
929 -- analyzed first, before we analyze the body.
931 if Nkind (Unit_Node) = N_Package_Body then
933 -- If no Lib_Unit, then there was a serious previous error, so just
934 -- ignore the entire analysis effort.
936 if No (Lib_Unit) then
937 Check_Error_Detected;
938 return;
940 else
941 -- Analyze the package spec
943 Semantics (Lib_Unit);
945 -- Check for unused with's
947 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
949 -- Verify that the library unit is a package declaration
951 if Nkind (Unit (Lib_Unit)) not in
952 N_Package_Declaration | N_Generic_Package_Declaration
953 then
954 Error_Msg_N
955 ("no legal package declaration for package body", N);
956 return;
958 -- Otherwise, the entity in the declaration is visible. Update the
959 -- version to reflect dependence of this body on the spec.
961 else
962 Spec_Id := Defining_Entity (Unit (Lib_Unit));
963 Set_Is_Immediately_Visible (Spec_Id, True);
964 Version_Update (N, Lib_Unit);
966 if Nkind (Defining_Unit_Name (Unit_Node)) =
967 N_Defining_Program_Unit_Name
968 then
969 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
970 end if;
971 end if;
972 end if;
974 -- If the unit is a subprogram body, then we similarly need to analyze
975 -- its spec. However, things are a little simpler in this case, because
976 -- here, this analysis is done mostly for error checking and consistency
977 -- purposes (but not only, e.g. there could be a contract on the spec),
978 -- so there's nothing else to be done.
980 elsif Nkind (Unit_Node) = N_Subprogram_Body then
981 if Acts_As_Spec (N) then
983 -- If the subprogram body is a child unit, we must create a
984 -- declaration for it, in order to properly load the parent(s).
985 -- After this, the original unit does not acts as a spec, because
986 -- there is an explicit one. If this unit appears in a context
987 -- clause, then an implicit with on the parent will be added when
988 -- installing the context. If this is the main unit, there is no
989 -- Unit_Table entry for the declaration (it has the unit number
990 -- of the main unit) and code generation is unaffected.
992 Unum := Get_Cunit_Unit_Number (N);
993 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
995 if Present (Par_Spec_Name) then
996 Unum :=
997 Load_Unit
998 (Load_Name => Par_Spec_Name,
999 Required => True,
1000 Subunit => False,
1001 Error_Node => N);
1003 if Unum /= No_Unit then
1005 -- Build subprogram declaration and attach parent unit to it
1006 -- This subprogram declaration does not come from source,
1007 -- Nevertheless the backend must generate debugging info for
1008 -- it, and this must be indicated explicitly. We also mark
1009 -- the body entity as a child unit now, to prevent a
1010 -- cascaded error if the spec entity cannot be entered
1011 -- in its scope. Finally we create a Units table entry for
1012 -- the subprogram declaration, to maintain a one-to-one
1013 -- correspondence with compilation unit nodes. This is
1014 -- critical for the tree traversals performed by CodePeer.
1016 declare
1017 Loc : constant Source_Ptr := Sloc (N);
1018 SCS : constant Boolean :=
1019 Get_Comes_From_Source_Default;
1021 begin
1022 Set_Comes_From_Source_Default (False);
1024 -- Note: We copy the Context_Items from the explicit body
1025 -- to the implicit spec, setting the former to Empty_List
1026 -- to preserve the treeish nature of the tree, during
1027 -- analysis of the spec. Then we put it back the way it
1028 -- was -- copy the Context_Items from the spec to the
1029 -- body, and set the spec Context_Items to Empty_List.
1030 -- It is necessary to preserve the treeish nature,
1031 -- because otherwise we will call End_Use_* twice on the
1032 -- same thing.
1034 Lib_Unit :=
1035 Make_Compilation_Unit (Loc,
1036 Context_Items => Context_Items (N),
1037 Unit =>
1038 Make_Subprogram_Declaration (Sloc (N),
1039 Specification =>
1040 Copy_Separate_Tree
1041 (Specification (Unit_Node))),
1042 Aux_Decls_Node =>
1043 Make_Compilation_Unit_Aux (Loc));
1045 Set_Context_Items (N, Empty_List);
1046 Set_Library_Unit (N, Lib_Unit);
1047 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
1048 Make_Child_Decl_Unit (N);
1049 Semantics (Lib_Unit);
1051 -- Now that a separate declaration exists, the body
1052 -- of the child unit does not act as spec any longer.
1054 Set_Acts_As_Spec (N, False);
1055 Move_Aspects (From => Unit_Node, To => Unit (Lib_Unit));
1056 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
1057 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
1058 Set_Comes_From_Source_Default (SCS);
1060 -- Restore Context_Items to the body
1062 Set_Context_Items (N, Context_Items (Lib_Unit));
1063 Set_Context_Items (Lib_Unit, Empty_List);
1064 end;
1065 end if;
1066 end if;
1068 -- Here for subprogram with separate declaration
1070 else
1071 Semantics (Lib_Unit);
1072 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
1073 Version_Update (N, Lib_Unit);
1074 end if;
1076 -- If this is a child unit, generate references to the parents
1078 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
1079 N_Defining_Program_Unit_Name
1080 then
1081 Generate_Parent_References
1082 (Specification (Unit_Node),
1083 Scope (Defining_Entity (Unit (Lib_Unit))));
1084 end if;
1085 end if;
1087 -- If it is a child unit, the parent must be elaborated first and we
1088 -- update version, since we are dependent on our parent.
1090 if Is_Child_Spec (Unit_Node) then
1092 -- The analysis of the parent is done with style checks off
1094 declare
1095 Save_Style_Check : constant Boolean := Style_Check;
1097 begin
1098 if not GNAT_Mode then
1099 Style_Check := False;
1100 end if;
1102 Semantics (Parent_Spec (Unit_Node));
1103 Version_Update (N, Parent_Spec (Unit_Node));
1105 -- Restore style check settings
1107 Style_Check := Save_Style_Check;
1108 end;
1109 end if;
1111 -- With the analysis done, install assertion_policy pragmas defined at
1112 -- the start of the specification of this unit (and recursively the
1113 -- assertion policy pragmas defined at the start of the specification
1114 -- of its parent units); install also the context of this compilation
1115 -- unit. Note that we can't install the context from the with clauses
1116 -- as we analyze them, because each with clause must be analyzed in a
1117 -- clean visibility context, so we have to wait and install them all
1118 -- at once.
1120 Last_Policy_Pragma := Install_Inherited_Policy_Pragmas (N);
1121 Install_Context (N);
1123 if Is_Child_Spec (Unit_Node) then
1125 -- Set the entities of all parents in the program_unit_name
1127 Generate_Parent_References
1128 (Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
1129 end if;
1131 -- All components of the context: with-clauses, library unit, ancestors
1132 -- if any, (and their context) are analyzed and installed.
1134 -- Call special debug routine sm if this is the main unit
1136 if Current_Sem_Unit = Main_Unit then
1138 end if;
1140 -- Now analyze the unit (package, subprogram spec, body) itself
1142 Analyze (Unit_Node);
1144 if Warn_On_Redundant_Constructs then
1145 Check_Redundant_Withs (Context_Items (N));
1147 if Nkind (Unit_Node) = N_Package_Body then
1148 Check_Redundant_Withs
1149 (Context_Items => Context_Items (N),
1150 Spec_Context_Items => Context_Items (Lib_Unit));
1151 end if;
1152 end if;
1154 -- The above call might have made Unit_Node an N_Subprogram_Body from
1155 -- something else, so propagate any Acts_As_Spec flag.
1157 if Nkind (Unit_Node) = N_Subprogram_Body
1158 and then Acts_As_Spec (Unit_Node)
1159 then
1160 Set_Acts_As_Spec (N);
1161 end if;
1163 -- Register predefined units in Rtsfind
1165 if In_Predefined_Unit (N) then
1166 Set_RTU_Loaded (Unit_Node);
1167 end if;
1169 -- Treat compilation unit pragmas that appear after the library unit
1171 declare
1172 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
1173 begin
1174 while Present (Prag_Node) loop
1175 Analyze (Prag_Node);
1176 Next (Prag_Node);
1177 end loop;
1178 end;
1180 -- Analyze the contract of a [generic] subprogram that acts as a
1181 -- compilation unit after all compilation pragmas have been analyzed.
1183 if Nkind (Unit_Node) in
1184 N_Generic_Subprogram_Declaration | N_Subprogram_Declaration
1185 then
1186 Analyze_Entry_Or_Subprogram_Contract (Defining_Entity (Unit_Node));
1187 end if;
1189 -- Generate distribution stubs if requested and no error
1191 if N = Main_Cunit
1192 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
1193 or else
1194 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
1195 and then Fatal_Error (Main_Unit) /= Error_Detected
1196 then
1197 if Is_RCI_Pkg_Spec_Or_Body (N) then
1199 -- Regular RCI package
1201 Add_Stub_Constructs (N);
1203 elsif (Nkind (Unit_Node) = N_Package_Declaration
1204 and then Is_Shared_Passive (Defining_Entity
1205 (Specification (Unit_Node))))
1206 or else (Nkind (Unit_Node) = N_Package_Body
1207 and then
1208 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
1209 then
1210 -- Shared passive package
1212 Add_Stub_Constructs (N);
1214 elsif Nkind (Unit_Node) = N_Package_Instantiation
1215 and then
1216 Is_Remote_Call_Interface
1217 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
1218 then
1219 -- Instantiation of a RCI generic package
1221 Add_Stub_Constructs (N);
1222 end if;
1223 end if;
1225 -- Build dispatch tables of library-level tagged types only now because
1226 -- the generation of distribution stubs above may create some of them.
1228 if Expander_Active and then Tagged_Type_Expansion then
1229 case Nkind (Unit_Node) is
1230 when N_Package_Declaration | N_Package_Body =>
1231 Build_Static_Dispatch_Tables (Unit_Node);
1233 when N_Package_Instantiation =>
1234 Build_Static_Dispatch_Tables (Instance_Spec (Unit_Node));
1236 when others =>
1237 null;
1238 end case;
1239 end if;
1241 -- Remove unit from visibility, so that environment is clean for the
1242 -- next compilation, which is either the main unit or some other unit
1243 -- in the context.
1245 if Nkind (Unit_Node) in N_Package_Declaration
1246 | N_Package_Renaming_Declaration
1247 | N_Subprogram_Declaration
1248 | N_Generic_Declaration
1249 or else (Nkind (Unit_Node) = N_Subprogram_Body
1250 and then Acts_As_Spec (Unit_Node))
1251 then
1252 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
1254 -- If the unit is an instantiation whose body will be elaborated for
1255 -- inlining purposes, use the proper entity of the instance. The entity
1256 -- may be missing if the instantiation was illegal.
1258 elsif Nkind (Unit_Node) = N_Package_Instantiation
1259 and then not Error_Posted (Unit_Node)
1260 and then Present (Instance_Spec (Unit_Node))
1261 then
1262 Remove_Unit_From_Visibility
1263 (Defining_Entity (Instance_Spec (Unit_Node)));
1265 elsif Nkind (Unit_Node) = N_Package_Body
1266 or else (Nkind (Unit_Node) = N_Subprogram_Body
1267 and then not Acts_As_Spec (Unit_Node))
1268 then
1269 -- Bodies that are not the main unit are compiled if they are generic
1270 -- or contain generic or inlined units. Their analysis brings in the
1271 -- context of the corresponding spec (unit declaration) which must be
1272 -- removed as well, to return the compilation environment to its
1273 -- proper state.
1275 Remove_Context (Lib_Unit);
1276 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1277 end if;
1279 -- Last step is to deinstall the context we just installed as well as
1280 -- the unit just compiled.
1282 Remove_Context (N);
1283 Remove_Inherited_Policy_Pragmas (Last_Policy_Pragma);
1285 -- When generating code for a non-generic main unit, check that withed
1286 -- generic units have a body if they need it, even if the units have not
1287 -- been instantiated. Force the load of the bodies to produce the proper
1288 -- error if the body is absent. The same applies to GNATprove mode, with
1289 -- the added benefit of capturing global references within the generic.
1290 -- This in turn allows for proper inlining of subprogram bodies without
1291 -- a previous declaration.
1293 if Get_Cunit_Unit_Number (N) = Main_Unit
1294 and then ((Operating_Mode = Generate_Code and then Expander_Active)
1295 or else
1296 (Operating_Mode = Check_Semantics and then GNATprove_Mode))
1297 then
1298 -- Check whether the source for the body of the unit must be included
1299 -- in a standalone library.
1301 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1303 -- Indicate that the main unit is now analyzed, to catch possible
1304 -- circularities between it and generic bodies. Remove main unit from
1305 -- visibility. This might seem superfluous, but the main unit must
1306 -- not be visible in the generic body expansions that follow.
1308 Set_Analyzed (N, True);
1309 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1311 declare
1312 Item : Node_Id;
1313 Nam : Entity_Id;
1314 Un : Unit_Number_Type;
1316 Save_Style_Check : constant Boolean := Style_Check;
1318 begin
1319 Item := First (Context_Items (N));
1320 while Present (Item) loop
1322 -- Check for explicit with clause
1324 if Nkind (Item) = N_With_Clause
1325 and then not Implicit_With (Item)
1327 -- Ada 2005 (AI-50217): Ignore limited-withed units
1329 and then not Limited_Present (Item)
1330 then
1331 Nam := Entity (Name (Item));
1333 -- Compile the generic subprogram, unless it is intrinsic or
1334 -- imported so no body is required, or generic package body
1335 -- if the package spec requires a body.
1337 if (Is_Generic_Subprogram (Nam)
1338 and then not Is_Intrinsic_Subprogram (Nam)
1339 and then not Is_Imported (Nam))
1340 or else (Ekind (Nam) = E_Generic_Package
1341 and then Unit_Requires_Body (Nam))
1342 then
1343 Style_Check := False;
1345 if Present (Renamed_Entity (Nam)) then
1346 Un :=
1347 Load_Unit
1348 (Load_Name =>
1349 Get_Body_Name
1350 (Get_Unit_Name
1351 (Unit_Declaration_Node
1352 (Renamed_Entity (Nam)))),
1353 Required => False,
1354 Subunit => False,
1355 Error_Node => N,
1356 Renamings => True);
1357 else
1358 Un :=
1359 Load_Unit
1360 (Load_Name =>
1361 Get_Body_Name (Get_Unit_Name (Item)),
1362 Required => False,
1363 Subunit => False,
1364 Error_Node => N,
1365 Renamings => True);
1366 end if;
1368 if Un = No_Unit then
1369 Error_Msg_NE
1370 ("body of generic unit& not found", Item, Nam);
1371 exit;
1373 elsif not Analyzed (Cunit (Un))
1374 and then Un /= Main_Unit
1375 and then Fatal_Error (Un) /= Error_Detected
1376 then
1377 Style_Check := False;
1378 Semantics (Cunit (Un));
1379 end if;
1380 end if;
1381 end if;
1383 Next (Item);
1384 end loop;
1386 -- Restore style checks settings
1388 Style_Check := Save_Style_Check;
1389 end;
1391 -- In GNATprove mode, force the loading of an Interrupt_Priority when
1392 -- processing compilation units with potentially "main" subprograms.
1393 -- This is required for the ceiling priority protocol checks, which
1394 -- are triggered by these subprograms.
1396 if GNATprove_Mode
1397 and then Nkind (Unit_Node) in N_Function_Instantiation
1398 | N_Procedure_Instantiation
1399 | N_Subprogram_Body
1400 then
1401 declare
1402 Spec : Node_Id;
1404 begin
1405 case Nkind (Unit_Node) is
1406 when N_Subprogram_Body =>
1407 Spec := Specification (Unit_Node);
1409 when N_Subprogram_Instantiation =>
1410 Spec :=
1411 Subprogram_Specification (Entity (Name (Unit_Node)));
1413 when others =>
1414 raise Program_Error;
1415 end case;
1417 pragma Assert (Nkind (Spec) in N_Subprogram_Specification);
1419 -- Main subprogram must have no parameters, and if it is a
1420 -- function, it must return an integer.
1422 if No (Parameter_Specifications (Spec))
1423 and then (Nkind (Spec) = N_Procedure_Specification
1424 or else
1425 Is_Integer_Type (Etype (Result_Definition (Spec))))
1426 then
1427 SPARK_Implicit_Load (RE_Interrupt_Priority);
1428 end if;
1429 end;
1430 end if;
1431 end if;
1433 -- Deal with creating elaboration counter if needed. We create an
1434 -- elaboration counter only for units that come from source since
1435 -- units manufactured by the compiler never need elab checks.
1437 if Comes_From_Source (N)
1438 and then Nkind (Unit_Node) in N_Package_Declaration
1439 | N_Generic_Package_Declaration
1440 | N_Subprogram_Declaration
1441 | N_Generic_Subprogram_Declaration
1442 then
1443 declare
1444 Loc : constant Source_Ptr := Sloc (N);
1445 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1447 begin
1448 Spec_Id := Defining_Entity (Unit_Node);
1449 Generate_Definition (Spec_Id);
1451 -- See if an elaboration entity is required for possible access
1452 -- before elaboration checking. Note that we must allow for this
1453 -- even if -gnatE is not set, since a client may be compiled in
1454 -- -gnatE mode and reference the entity.
1456 -- These entities are also used by the binder to prevent multiple
1457 -- attempts to execute the elaboration code for the library case
1458 -- where the elaboration routine might otherwise be called more
1459 -- than once.
1461 -- They are also needed to ensure explicit visibility from the
1462 -- binder generated code of all the units involved in a partition
1463 -- when control-flow preservation is requested.
1465 if not Opt.Suppress_Control_Flow_Optimizations
1466 and then
1467 ( -- Pure units do not need checks
1469 Is_Pure (Spec_Id)
1471 -- Preelaborated units do not need checks
1473 or else Is_Preelaborated (Spec_Id)
1475 -- No checks needed if pragma Elaborate_Body present
1477 or else Has_Pragma_Elaborate_Body (Spec_Id)
1479 -- No checks needed if unit does not require a body
1481 or else not Unit_Requires_Body (Spec_Id)
1483 -- No checks needed for predefined files
1485 or else Is_Predefined_Unit (Unum)
1487 -- No checks required if no separate spec
1489 or else Acts_As_Spec (N)
1491 then
1492 -- This is a case where we only need the entity for checking to
1493 -- prevent multiple elaboration checks.
1495 Set_Elaboration_Entity_Required (Spec_Id, False);
1497 -- Otherwise the unit requires an elaboration entity because it
1498 -- carries a body.
1500 else
1501 Set_Elaboration_Entity_Required (Spec_Id);
1502 end if;
1504 Build_Elaboration_Entity (N, Spec_Id);
1505 end;
1506 end if;
1508 -- Freeze the compilation unit entity. This for sure is needed because
1509 -- of some warnings that can be output (see Freeze_Subprogram), but may
1510 -- in general be required. If freezing actions result, place them in the
1511 -- compilation unit actions list, and analyze them.
1513 declare
1514 L : constant List_Id :=
1515 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1516 begin
1517 while Is_Non_Empty_List (L) loop
1518 Insert_Library_Level_Action (Remove_Head (L));
1519 end loop;
1520 end;
1522 Set_Analyzed (N);
1524 -- Call Check_Package_Body so that a body containing subprograms with
1525 -- Inline_Always can be made available for front end inlining.
1527 if Nkind (Unit_Node) = N_Package_Declaration
1528 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1530 -- We don't need to do this if the Expander is not active, since there
1531 -- is no code to inline.
1533 and then Expander_Active
1534 then
1535 declare
1536 Save_Style_Check : constant Boolean := Style_Check;
1537 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1538 Options : Style_Check_Options;
1540 begin
1541 Save_Style_Check_Options (Options);
1542 Reset_Style_Check_Options;
1543 Opt.Warning_Mode := Suppress;
1545 Check_Package_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1547 Reset_Style_Check_Options;
1548 Set_Style_Check_Options (Options);
1549 Style_Check := Save_Style_Check;
1550 Warning_Mode := Save_Warning;
1551 end;
1552 end if;
1554 -- If we are generating obsolescent warnings, then here is where we
1555 -- generate them for the with'ed items. The reason for this special
1556 -- processing is that the normal mechanism of generating the warnings
1557 -- for referenced entities does not work for context clause references.
1558 -- That's because when we first analyze the context, it is too early to
1559 -- know if the with'ing unit is itself obsolescent (which suppresses
1560 -- the warnings).
1562 if not GNAT_Mode
1563 and then Warn_On_Obsolescent_Feature
1564 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1565 then
1566 -- Push current compilation unit as scope, so that the test for
1567 -- being within an obsolescent unit will work correctly. The check
1568 -- is not performed within an instantiation, because the warning
1569 -- will have been emitted in the corresponding generic unit.
1571 Push_Scope (Defining_Entity (Unit_Node));
1573 -- Loop through context items to deal with with clauses
1575 declare
1576 Item : Node_Id;
1577 Nam : Node_Id;
1578 Ent : Entity_Id;
1580 begin
1581 Item := First (Context_Items (N));
1582 while Present (Item) loop
1583 if Nkind (Item) = N_With_Clause
1585 -- Suppress this check in limited-withed units. Further work
1586 -- needed here if we decide to incorporate this check on
1587 -- limited-withed units.
1589 and then not Limited_Present (Item)
1590 then
1591 Nam := Name (Item);
1592 Ent := Entity (Nam);
1594 if Is_Obsolescent (Ent) then
1595 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1596 end if;
1597 end if;
1599 Next (Item);
1600 end loop;
1601 end;
1603 -- Remove temporary install of current unit as scope
1605 Pop_Scope;
1606 end if;
1608 -- Finally restore all the original style check options
1610 Set_Style_Check_Options (Options);
1612 -- If No_Elaboration_Code_All was encountered, this is where we do the
1613 -- transitive test of with'ed units to make sure they have the aspect.
1614 -- This is delayed till the end of analyzing the compilation unit to
1615 -- ensure that the pragma/aspect, if present, has been analyzed.
1617 Check_No_Elab_Code_All (N);
1619 -- If this is a main compilation containing a package declaration that
1620 -- requires no package body, and the profile of some subprogram depends
1621 -- on shadow incomplete entities then perform full analysis of its
1622 -- limited-with units.
1624 Analyze_Required_Limited_With_Units (N);
1625 end Analyze_Compilation_Unit;
1627 ---------------------
1628 -- Analyze_Context --
1629 ---------------------
1631 procedure Analyze_Context (N : Node_Id) is
1632 Ukind : constant Node_Kind := Nkind (Unit (N));
1633 Item : Node_Id;
1635 begin
1636 -- First process all configuration pragmas at the start of the context
1637 -- items. Strictly these are not part of the context clause, but that
1638 -- is where the parser puts them. In any case for sure we must analyze
1639 -- these before analyzing the actual context items, since they can have
1640 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1641 -- be with'ed as a result of changing categorizations in Ada 2005).
1643 Item := First (Context_Items (N));
1644 while Present (Item)
1645 and then Nkind (Item) = N_Pragma
1646 and then Pragma_Name (Item) in Configuration_Pragma_Names
1647 loop
1648 Analyze (Item);
1649 Next (Item);
1650 end loop;
1652 -- This is the point at which we capture the configuration settings
1653 -- for the unit. At the moment only the Optimize_Alignment setting
1654 -- needs to be captured. Probably more later ???
1656 if Optimize_Alignment_Local then
1657 Set_OA_Setting (Current_Sem_Unit, 'L');
1658 else
1659 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1660 end if;
1662 -- Loop through actual context items. This is done in two passes:
1664 -- a) The first pass analyzes nonlimited with clauses and also any
1665 -- configuration pragmas (we need to get the latter analyzed right
1666 -- away, since they can affect processing of subsequent items).
1668 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1670 while Present (Item) loop
1672 -- For with clause, analyze the with clause, and then update the
1673 -- version, since we are dependent on a unit that we with.
1675 if Nkind (Item) = N_With_Clause
1676 and then not Limited_Present (Item)
1677 then
1678 -- Skip analyzing with clause if no unit, nothing to do (this
1679 -- happens for a with that references a non-existent unit).
1681 if Present (Library_Unit (Item)) then
1683 -- Skip analyzing with clause if this is a with_clause for
1684 -- the main unit, which happens if a subunit has a useless
1685 -- with_clause on its parent.
1687 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1688 Analyze (Item);
1690 -- Here for the case of a useless with for the main unit
1692 else
1693 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1694 end if;
1695 end if;
1697 -- Do version update (skipped for implicit with)
1699 if not Implicit_With (Item) then
1700 Version_Update (N, Library_Unit (Item));
1701 end if;
1703 -- Skip pragmas. Configuration pragmas at the start were handled in
1704 -- the loop above, and remaining pragmas are not processed until we
1705 -- actually install the context (see Install_Context). We delay the
1706 -- analysis of these pragmas to make sure that we have installed all
1707 -- the implicit with's on parent units.
1709 -- Skip use clauses at this stage, since we don't want to do any
1710 -- installing of potentially use-visible entities until we
1711 -- actually install the complete context (in Install_Context).
1712 -- Otherwise things can get installed in the wrong context.
1714 else
1715 null;
1716 end if;
1718 Next (Item);
1719 end loop;
1721 -- Second pass: examine all limited_with clauses. All other context
1722 -- items are ignored in this pass.
1724 Item := First (Context_Items (N));
1725 while Present (Item) loop
1726 if Nkind (Item) = N_With_Clause
1727 and then Limited_Present (Item)
1728 then
1729 -- No need to check errors on implicitly generated limited-with
1730 -- clauses.
1732 if not Implicit_With (Item) then
1734 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1735 -- properly rejected, including renaming declarations.
1737 if Ukind not in N_Package_Declaration
1738 | N_Subprogram_Declaration
1739 | N_Generic_Declaration
1740 | N_Generic_Instantiation
1741 then
1742 Error_Msg_N ("limited with_clause not allowed here", Item);
1744 -- Check wrong use of a limited with clause applied to the
1745 -- compilation unit containing the limited-with clause.
1747 -- limited with P.Q;
1748 -- package P.Q is ...
1750 elsif Unit (Library_Unit (Item)) = Unit (N) then
1751 Error_Msg_N ("wrong use of limited-with clause", Item);
1753 -- Check wrong use of limited-with clause applied to some
1754 -- immediate ancestor.
1756 elsif Is_Child_Spec (Unit (N)) then
1757 declare
1758 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1759 P : Node_Id;
1761 begin
1762 P := Parent_Spec (Unit (N));
1763 loop
1764 if Unit (P) = Lib_U then
1765 Error_Msg_N
1766 ("limited with_clause cannot name ancestor",
1767 Item);
1768 exit;
1769 end if;
1771 exit when not Is_Child_Spec (Unit (P));
1772 P := Parent_Spec (Unit (P));
1773 end loop;
1774 end;
1775 end if;
1777 -- Check if the limited-withed unit is already visible through
1778 -- some context clause of the current compilation unit or some
1779 -- ancestor of the current compilation unit.
1781 declare
1782 Lim_Unit_Name : constant Node_Id := Name (Item);
1783 Comp_Unit : Node_Id;
1784 It : Node_Id;
1785 Unit_Name : Node_Id;
1787 begin
1788 Comp_Unit := N;
1789 loop
1790 It := First (Context_Items (Comp_Unit));
1791 while Present (It) loop
1792 if Item /= It
1793 and then Nkind (It) = N_With_Clause
1794 and then not Limited_Present (It)
1795 and then Nkind (Unit (Library_Unit (It))) in
1796 N_Package_Declaration |
1797 N_Package_Renaming_Declaration
1798 then
1799 if Nkind (Unit (Library_Unit (It))) =
1800 N_Package_Declaration
1801 then
1802 Unit_Name := Name (It);
1803 else
1804 Unit_Name := Name (Unit (Library_Unit (It)));
1805 end if;
1807 -- Check if the named package (or some ancestor)
1808 -- leaves visible the full-view of the unit given
1809 -- in the limited-with clause.
1811 loop
1812 if Designate_Same_Unit (Lim_Unit_Name,
1813 Unit_Name)
1814 then
1815 Error_Msg_Sloc := Sloc (It);
1816 Error_Msg_N
1817 ("simultaneous visibility of limited and "
1818 & "unlimited views not allowed", Item);
1819 Error_Msg_N
1820 ("\unlimited view visible through context "
1821 & "clause #", Item);
1822 exit;
1824 elsif Nkind (Unit_Name) = N_Identifier then
1825 exit;
1826 end if;
1828 Unit_Name := Prefix (Unit_Name);
1829 end loop;
1830 end if;
1832 Next (It);
1833 end loop;
1835 exit when not Is_Child_Spec (Unit (Comp_Unit));
1837 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1838 end loop;
1839 end;
1840 end if;
1842 -- Skip analyzing with clause if no unit, see above
1844 if Present (Library_Unit (Item)) then
1845 Analyze (Item);
1846 end if;
1848 -- A limited_with does not impose an elaboration order, but there
1849 -- is a semantic dependency for recompilation purposes.
1851 if not Implicit_With (Item) then
1852 Version_Update (N, Library_Unit (Item));
1853 end if;
1855 -- Pragmas and use clauses and with clauses other than limited with's
1856 -- are ignored in this pass through the context items.
1858 else
1859 null;
1860 end if;
1862 Next (Item);
1863 end loop;
1864 end Analyze_Context;
1866 -------------------------------
1867 -- Analyze_Package_Body_Stub --
1868 -------------------------------
1870 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1871 Id : constant Entity_Id := Defining_Entity (N);
1872 Nam : Entity_Id;
1873 Opts : Config_Switches_Type;
1875 begin
1876 -- The package declaration must be in the current declarative part
1878 Check_Stub_Level (N);
1879 Nam := Current_Entity_In_Scope (Id);
1881 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1882 Error_Msg_N ("missing specification for package stub", N);
1884 elsif Has_Completion (Nam)
1885 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1886 then
1887 Error_Msg_N ("duplicate or redundant stub for package", N);
1889 else
1890 -- Retain and restore the configuration options of the enclosing
1891 -- context as the proper body may introduce a set of its own.
1893 Opts := Save_Config_Switches;
1895 -- Indicate that the body of the package exists. If we are doing
1896 -- only semantic analysis, the stub stands for the body. If we are
1897 -- generating code, the existence of the body will be confirmed
1898 -- when we load the proper body.
1900 Set_Scope (Id, Current_Scope);
1901 Mutate_Ekind (Id, E_Package_Body);
1902 Set_Etype (Id, Standard_Void_Type);
1904 Analyze_Aspect_Specifications (N, Id);
1906 Set_Has_Completion (Nam);
1907 Set_Corresponding_Spec_Of_Stub (N, Nam);
1908 Generate_Reference (Nam, Id, 'b');
1909 Analyze_Proper_Body (N, Nam);
1911 Restore_Config_Switches (Opts);
1912 end if;
1913 end Analyze_Package_Body_Stub;
1915 -------------------------
1916 -- Analyze_Proper_Body --
1917 -------------------------
1919 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1920 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1922 procedure Optional_Subunit;
1923 -- This procedure is called when the main unit is a stub, or when we
1924 -- are not generating code. In such a case, we analyze the subunit if
1925 -- present, which is user-friendly, but we don't complain if the subunit
1926 -- is missing. In GNATprove_Mode, we issue an error to avoid formal
1927 -- verification of a partial unit.
1929 ----------------------
1930 -- Optional_Subunit --
1931 ----------------------
1933 procedure Optional_Subunit is
1934 Comp_Unit : Node_Id;
1935 Unum : Unit_Number_Type;
1937 begin
1938 -- Try to load subunit, but ignore any errors that occur during the
1939 -- loading of the subunit, by using the special feature in Errout to
1940 -- ignore all errors. Note that Fatal_Error will still be set, so we
1941 -- will be able to check for this case below.
1943 if not GNATprove_Mode then
1944 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1945 end if;
1947 Unum :=
1948 Load_Unit
1949 (Load_Name => Subunit_Name,
1950 Required => GNATprove_Mode,
1951 Subunit => True,
1952 Error_Node => N);
1954 if not GNATprove_Mode then
1955 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1956 end if;
1958 -- All done if we successfully loaded the subunit
1960 if Unum /= No_Unit
1961 and then (Fatal_Error (Unum) /= Error_Detected
1962 or else Try_Semantics)
1963 then
1964 Comp_Unit := Cunit (Unum);
1966 -- If the file was empty or seriously mangled, the unit itself may
1967 -- be missing.
1969 if No (Unit (Comp_Unit)) then
1970 Error_Msg_N
1971 ("subunit does not contain expected proper body", N);
1973 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1974 Error_Msg_N
1975 ("expected SEPARATE subunit, found child unit",
1976 Cunit_Entity (Unum));
1977 else
1978 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1979 Analyze_Subunit (Comp_Unit);
1980 Set_Library_Unit (N, Comp_Unit);
1981 Set_Corresponding_Body (N, Defining_Entity (Unit (Comp_Unit)));
1982 end if;
1984 elsif Unum = No_Unit
1985 and then Present (Nam)
1986 then
1987 if Is_Protected_Type (Nam) then
1988 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1989 else
1990 Set_Corresponding_Body (
1991 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1992 end if;
1993 end if;
1994 end Optional_Subunit;
1996 -- Local variables
1998 Comp_Unit : Node_Id;
1999 Unum : Unit_Number_Type;
2001 -- Start of processing for Analyze_Proper_Body
2003 begin
2004 -- If the subunit is already loaded, it means that the main unit is a
2005 -- subunit, and that the current unit is one of its parents which was
2006 -- being analyzed to provide the needed context for the analysis of the
2007 -- subunit. In this case we analyze the subunit and continue with the
2008 -- parent, without looking at subsequent subunits.
2010 if Is_Loaded (Subunit_Name) then
2012 -- If the proper body is already linked to the stub node, the stub is
2013 -- in a generic unit and just needs analyzing.
2015 if Present (Library_Unit (N)) then
2016 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
2018 -- If the subunit has severe errors, the spec of the enclosing
2019 -- body may not be available, in which case do not try analysis.
2021 if Serious_Errors_Detected > 0
2022 and then No (Library_Unit (Library_Unit (N)))
2023 then
2024 return;
2025 end if;
2027 -- Collect SCO information for loaded subunit if we are in the
2028 -- extended main unit.
2030 if Generate_SCO
2031 and then In_Extended_Main_Source_Unit
2032 (Cunit_Entity (Current_Sem_Unit))
2033 then
2034 SCO_Record_Raw (Get_Cunit_Unit_Number (Library_Unit (N)));
2035 end if;
2037 Analyze_Subunit (Library_Unit (N));
2039 -- Otherwise we must load the subunit and link to it
2041 else
2042 -- Load the subunit, this must work, since we originally loaded
2043 -- the subunit earlier on. So this will not really load it, just
2044 -- give access to it.
2046 Unum :=
2047 Load_Unit
2048 (Load_Name => Subunit_Name,
2049 Required => True,
2050 Subunit => False,
2051 Error_Node => N);
2053 -- And analyze the subunit in the parent context (note that we
2054 -- do not call Semantics, since that would remove the parent
2055 -- context). Because of this, we have to manually reset the
2056 -- compiler state to Analyzing since it got destroyed by Load.
2058 if Unum /= No_Unit then
2059 Compiler_State := Analyzing;
2061 -- Check that the proper body is a subunit and not a child
2062 -- unit. If the unit was previously loaded, the error will
2063 -- have been emitted when copying the generic node, so we
2064 -- just return to avoid cascaded errors.
2066 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
2067 return;
2068 end if;
2070 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
2071 Analyze_Subunit (Cunit (Unum));
2072 Set_Library_Unit (N, Cunit (Unum));
2073 end if;
2074 end if;
2076 -- If the main unit is a subunit, then we are just performing semantic
2077 -- analysis on that subunit, and any other subunits of any parent unit
2078 -- should be ignored. If the main unit is itself a subunit, another
2079 -- subunit is irrelevant unless it is a subunit of the current one, that
2080 -- is to say appears in the current source tree.
2082 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
2083 and then Subunit_Name /= Unit_Name (Main_Unit)
2084 then
2085 -- But before we return, set the flag for unloaded subunits. This
2086 -- will suppress junk warnings of variables in the same declarative
2087 -- part (or a higher level one) that are in danger of looking unused
2088 -- when in fact there might be a declaration in the subunit that we
2089 -- do not intend to load.
2091 Unloaded_Subunits := True;
2092 return;
2094 -- If the subunit is not already loaded, and we are generating code,
2095 -- then this is the case where compilation started from the parent, and
2096 -- we are generating code for an entire subunit tree. In that case we
2097 -- definitely need to load the subunit.
2099 -- In order to continue the analysis with the rest of the parent,
2100 -- and other subunits, we load the unit without requiring its
2101 -- presence, and emit a warning if not found, rather than terminating
2102 -- the compilation abruptly, as for other missing file problems.
2104 elsif Original_Operating_Mode = Generate_Code then
2106 -- If the proper body is already linked to the stub node, the stub is
2107 -- in a generic unit and just needs analyzing.
2109 -- We update the version. Although we are not strictly technically
2110 -- semantically dependent on the subunit, given our approach of macro
2111 -- substitution of subunits, it makes sense to include it in the
2112 -- version identification.
2114 if Present (Library_Unit (N)) then
2115 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
2116 Analyze_Subunit (Library_Unit (N));
2117 Version_Update (Cunit (Main_Unit), Library_Unit (N));
2119 -- Otherwise we must load the subunit and link to it
2121 else
2122 -- Make sure that, if the subunit is preprocessed and -gnateG is
2123 -- specified, the preprocessed file will be written.
2125 Lib.Analysing_Subunit_Of_Main := True;
2126 Unum :=
2127 Load_Unit
2128 (Load_Name => Subunit_Name,
2129 Required => False,
2130 Subunit => True,
2131 Error_Node => N);
2132 Lib.Analysing_Subunit_Of_Main := False;
2134 -- Give message if we did not get the unit Emit warning even if
2135 -- missing subunit is not within main unit, to simplify debugging.
2137 pragma Assert (Original_Operating_Mode = Generate_Code);
2138 if Unum = No_Unit then
2139 Error_Msg_Unit_1 := Subunit_Name;
2140 Error_Msg_File_1 :=
2141 Get_File_Name (Subunit_Name, Subunit => True);
2142 Error_Msg_N
2143 ("subunit$$ in file{ not found??!!", N);
2144 Subunits_Missing := True;
2145 end if;
2147 -- Load_Unit may reset Compiler_State, since it may have been
2148 -- necessary to parse an additional units, so we make sure that
2149 -- we reset it to the Analyzing state.
2151 Compiler_State := Analyzing;
2153 if Unum /= No_Unit then
2154 if Debug_Flag_L then
2155 Write_Str ("*** Loaded subunit from stub. Analyze");
2156 Write_Eol;
2157 end if;
2159 Comp_Unit := Cunit (Unum);
2161 -- Check for child unit instead of subunit
2163 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
2164 Error_Msg_N
2165 ("expected SEPARATE subunit, found child unit",
2166 Cunit_Entity (Unum));
2168 -- OK, we have a subunit
2170 else
2171 Set_Corresponding_Stub (Unit (Comp_Unit), N);
2172 Set_Library_Unit (N, Comp_Unit);
2174 -- We update the version. Although we are not technically
2175 -- semantically dependent on the subunit, given our approach
2176 -- of macro substitution of subunits, it makes sense to
2177 -- include it in the version identification.
2179 Version_Update (Cunit (Main_Unit), Comp_Unit);
2181 -- Collect SCO information for loaded subunit if we are in
2182 -- the extended main unit.
2184 if Generate_SCO
2185 and then In_Extended_Main_Source_Unit
2186 (Cunit_Entity (Current_Sem_Unit))
2187 then
2188 SCO_Record_Raw (Unum);
2189 end if;
2191 -- Analyze the unit if semantics active
2193 if Fatal_Error (Unum) /= Error_Detected
2194 or else Try_Semantics
2195 then
2196 Analyze_Subunit (Comp_Unit);
2197 end if;
2198 end if;
2199 end if;
2200 end if;
2202 -- The remaining case is when the subunit is not already loaded and we
2203 -- are not generating code. In this case we are just performing semantic
2204 -- analysis on the parent, and we are not interested in the subunit. For
2205 -- subprograms, analyze the stub as a body. For other entities the stub
2206 -- has already been marked as completed.
2208 else
2209 Optional_Subunit;
2210 end if;
2211 end Analyze_Proper_Body;
2213 ----------------------------------
2214 -- Analyze_Protected_Body_Stub --
2215 ----------------------------------
2217 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
2218 Id : constant Entity_Id := Defining_Entity (N);
2219 Nam : Entity_Id := Current_Entity_In_Scope (Id);
2220 Opts : Config_Switches_Type;
2222 begin
2223 Check_Stub_Level (N);
2225 -- First occurrence of name may have been as an incomplete type
2227 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2228 Nam := Full_View (Nam);
2229 end if;
2231 if No (Nam) or else not Is_Protected_Type (Etype (Nam)) then
2232 Error_Msg_N ("missing specification for Protected body", N);
2234 else
2235 -- Retain and restore the configuration options of the enclosing
2236 -- context as the proper body may introduce a set of its own.
2238 Opts := Save_Config_Switches;
2240 Set_Scope (Id, Current_Scope);
2241 Mutate_Ekind (Id, E_Protected_Body);
2242 Set_Etype (Id, Standard_Void_Type);
2244 Analyze_Aspect_Specifications (N, Id);
2246 Set_Has_Completion (Etype (Nam));
2247 Set_Corresponding_Spec_Of_Stub (N, Nam);
2248 Generate_Reference (Nam, Id, 'b');
2249 Analyze_Proper_Body (N, Etype (Nam));
2251 Restore_Config_Switches (Opts);
2252 end if;
2253 end Analyze_Protected_Body_Stub;
2255 -----------------------------------------
2256 -- Analyze_Required_Limited_With_Units --
2257 -----------------------------------------
2259 procedure Analyze_Required_Limited_With_Units (N : Node_Id) is
2260 Unit_Node : constant Node_Id := Unit (N);
2261 Spec_Id : constant Entity_Id := Defining_Entity (Unit_Node);
2263 function Depends_On_Limited_Views (Pkg_Id : Entity_Id) return Boolean;
2264 -- Determines whether the given package has some subprogram with a
2265 -- profile that depends on shadow incomplete type entities of a
2266 -- limited-with unit.
2268 function Has_Limited_With_Clauses return Boolean;
2269 -- Determines whether the compilation unit N has limited-with context
2270 -- clauses.
2272 ------------------------------
2273 -- Has_Limited_With_Clauses --
2274 ------------------------------
2276 function Has_Limited_With_Clauses return Boolean is
2277 Item : Node_Id := First (Context_Items (N));
2279 begin
2280 while Present (Item) loop
2281 if Nkind (Item) = N_With_Clause
2282 and then Limited_Present (Item)
2283 and then not Implicit_With (Item)
2284 then
2285 return True;
2286 end if;
2288 Next (Item);
2289 end loop;
2291 return False;
2292 end Has_Limited_With_Clauses;
2294 ------------------------------
2295 -- Depends_On_Limited_Views --
2296 ------------------------------
2298 function Depends_On_Limited_Views (Pkg_Id : Entity_Id) return Boolean is
2300 function Has_Limited_View_Types (Subp : Entity_Id) return Boolean;
2301 -- Determines whether the type of some formal of Subp, or its return
2302 -- type, is a shadow incomplete entity of a limited-with unit.
2304 ----------------------------
2305 -- Has_Limited_View_Types --
2306 ----------------------------
2308 function Has_Limited_View_Types (Subp : Entity_Id) return Boolean is
2309 Formal : Entity_Id := First_Formal (Subp);
2311 begin
2312 while Present (Formal) loop
2313 if From_Limited_With (Etype (Formal))
2314 and then Has_Non_Limited_View (Etype (Formal))
2315 and then Ekind (Non_Limited_View (Etype (Formal)))
2316 = E_Incomplete_Type
2317 then
2318 return True;
2319 end if;
2321 Formal := Next_Formal (Formal);
2322 end loop;
2324 if Ekind (Subp) = E_Function
2325 and then From_Limited_With (Etype (Subp))
2326 and then Has_Non_Limited_View (Etype (Subp))
2327 and then Ekind (Non_Limited_View (Etype (Subp)))
2328 = E_Incomplete_Type
2329 then
2330 return True;
2331 end if;
2333 return False;
2334 end Has_Limited_View_Types;
2336 -- Local variables
2338 E : Entity_Id := First_Entity (Pkg_Id);
2340 begin
2341 while Present (E) loop
2342 if Is_Subprogram (E)
2343 and then Has_Limited_View_Types (E)
2344 then
2345 return True;
2347 -- Recursion on nested packages skipping package renamings
2349 elsif Ekind (E) = E_Package
2350 and then No (Renamed_Entity (E))
2351 and then Depends_On_Limited_Views (E)
2352 then
2353 return True;
2354 end if;
2356 Next_Entity (E);
2357 end loop;
2359 return False;
2360 end Depends_On_Limited_Views;
2362 -- Local variables
2364 Item : Node_Id;
2366 -- Start of processing for Analyze_Required_Limited_With_Units
2368 begin
2369 -- Cases where no action is required
2371 if not Expander_Active
2372 or else Nkind (Unit_Node) /= N_Package_Declaration
2373 or else Main_Unit_Entity /= Spec_Id
2374 or else Is_Generic_Unit (Spec_Id)
2375 or else Unit_Requires_Body (Spec_Id)
2376 or else not Has_Limited_With_Clauses
2377 or else not Depends_On_Limited_Views (Spec_Id)
2378 then
2379 return;
2380 end if;
2382 -- Perform full analyis of limited-with units to provide the backend
2383 -- with the full-view of shadow entities.
2385 Item := First (Context_Items (N));
2386 while Present (Item) loop
2387 if Nkind (Item) = N_With_Clause
2388 and then Limited_Present (Item)
2389 and then not Implicit_With (Item)
2390 then
2391 Semantics (Library_Unit (Item));
2392 end if;
2394 Next (Item);
2395 end loop;
2396 end Analyze_Required_Limited_With_Units;
2398 ----------------------------------
2399 -- Analyze_Subprogram_Body_Stub --
2400 ----------------------------------
2402 -- A subprogram body stub can appear with or without a previous spec. If
2403 -- there is one, then the analysis of the body will find it and verify
2404 -- conformance. The formals appearing in the specification of the stub play
2405 -- no role, except for requiring an additional conformance check. If there
2406 -- is no previous subprogram declaration, the stub acts as a spec, and
2407 -- provides the defining entity for the subprogram.
2409 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
2410 Decl : Node_Id;
2411 Opts : Config_Switches_Type;
2413 begin
2414 Check_Stub_Level (N);
2416 -- Verify that the identifier for the stub is unique within this
2417 -- declarative part.
2419 if Nkind (Parent (N)) in
2420 N_Block_Statement | N_Package_Body | N_Subprogram_Body
2421 then
2422 Decl := First (Declarations (Parent (N)));
2423 while Present (Decl) and then Decl /= N loop
2424 if Nkind (Decl) = N_Subprogram_Body_Stub
2425 and then Chars (Defining_Unit_Name (Specification (Decl))) =
2426 Chars (Defining_Unit_Name (Specification (N)))
2427 then
2428 Error_Msg_N ("identifier for stub is not unique", N);
2429 end if;
2431 Next (Decl);
2432 end loop;
2433 end if;
2435 -- Retain and restore the configuration options of the enclosing context
2436 -- as the proper body may introduce a set of its own.
2438 Opts := Save_Config_Switches;
2440 -- Treat stub as a body, which checks conformance if there is a previous
2441 -- declaration, or else introduces entity and its signature.
2443 Analyze_Subprogram_Body (N);
2444 Analyze_Proper_Body (N, Empty);
2446 Restore_Config_Switches (Opts);
2447 end Analyze_Subprogram_Body_Stub;
2449 ---------------------
2450 -- Analyze_Subunit --
2451 ---------------------
2453 -- A subunit is compiled either by itself (for semantic checking) or as
2454 -- part of compiling the parent (for code generation). In either case, by
2455 -- the time we actually process the subunit, the parent has already been
2456 -- installed and analyzed. The node N is a compilation unit, whose context
2457 -- needs to be treated here, because we come directly here from the parent
2458 -- without calling Analyze_Compilation_Unit.
2460 -- The compilation context includes the explicit context of the subunit,
2461 -- and the context of the parent, together with the parent itself. In order
2462 -- to compile the current context, we remove the one inherited from the
2463 -- parent, in order to have a clean visibility table. We restore the parent
2464 -- context before analyzing the proper body itself. On exit, we remove only
2465 -- the explicit context of the subunit.
2467 -- WARNING: This routine manages SPARK regions. Return statements must be
2468 -- replaced by gotos which jump to the end of the routine and restore the
2469 -- SPARK mode.
2471 procedure Analyze_Subunit (N : Node_Id) is
2472 Lib_Unit : constant Node_Id := Library_Unit (N);
2473 Par_Unit : constant Entity_Id := Current_Scope;
2475 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
2476 Num_Scopes : Nat := 0;
2477 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2478 Enclosing_Child : Entity_Id := Empty;
2479 Svg : constant Suppress_Record := Scope_Suppress;
2481 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
2482 Cunit_Boolean_Restrictions_Save;
2483 -- Save non-partition wide restrictions before processing the subunit.
2484 -- All subunits are analyzed with config restrictions reset and we need
2485 -- to restore these saved values at the end.
2487 procedure Analyze_Subunit_Context;
2488 -- Capture names in use clauses of the subunit. This must be done before
2489 -- re-installing parent declarations, because items in the context must
2490 -- not be hidden by declarations local to the parent.
2492 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
2493 -- Recursive procedure to restore scope of all ancestors of subunit,
2494 -- from outermost in. If parent is not a subunit, the call to install
2495 -- context installs context of spec and (if parent is a child unit) the
2496 -- context of its parents as well. It is confusing that parents should
2497 -- be treated differently in both cases, but the semantics are just not
2498 -- identical.
2500 procedure Re_Install_Use_Clauses;
2501 -- As part of the removal of the parent scope, the use clauses are
2502 -- removed, to be reinstalled when the context of the subunit has been
2503 -- analyzed. Use clauses may also have been affected by the analysis of
2504 -- the context of the subunit, so they have to be applied again, to
2505 -- insure that the compilation environment of the rest of the parent
2506 -- unit is identical.
2508 procedure Remove_Scope;
2509 -- Remove current scope from scope stack, and preserve the list of use
2510 -- clauses in it, to be reinstalled after context is analyzed.
2512 -----------------------------
2513 -- Analyze_Subunit_Context --
2514 -----------------------------
2516 procedure Analyze_Subunit_Context is
2517 Item : Node_Id;
2518 Unit_Name : Entity_Id;
2520 begin
2521 Analyze_Context (N);
2522 Check_No_Elab_Code_All (N);
2524 -- Make withed units immediately visible. If child unit, make the
2525 -- ultimate parent immediately visible.
2527 Item := First (Context_Items (N));
2528 while Present (Item) loop
2529 if Nkind (Item) = N_With_Clause then
2531 -- Protect frontend against previous errors in context clauses
2533 if Nkind (Name (Item)) /= N_Selected_Component then
2534 if Error_Posted (Item) then
2535 null;
2537 else
2538 -- If a subunits has serious syntax errors, the context
2539 -- may not have been loaded. Add a harmless unit name to
2540 -- attempt processing.
2542 if Serious_Errors_Detected > 0
2543 and then No (Entity (Name (Item)))
2544 then
2545 Set_Entity (Name (Item), Standard_Standard);
2546 end if;
2548 Unit_Name := Entity (Name (Item));
2549 loop
2550 Set_Is_Visible_Lib_Unit (Unit_Name);
2551 exit when Scope (Unit_Name) = Standard_Standard;
2552 Unit_Name := Scope (Unit_Name);
2554 if No (Unit_Name) then
2555 Check_Error_Detected;
2556 return;
2557 end if;
2558 end loop;
2560 if not Is_Immediately_Visible (Unit_Name) then
2561 Set_Is_Immediately_Visible (Unit_Name);
2562 Set_Context_Installed (Item);
2563 end if;
2564 end if;
2565 end if;
2567 elsif Nkind (Item) = N_Use_Package_Clause then
2568 Analyze (Name (Item));
2570 elsif Nkind (Item) = N_Use_Type_Clause then
2571 Analyze (Subtype_Mark (Item));
2572 end if;
2574 Next (Item);
2575 end loop;
2577 -- Reset visibility of withed units. They will be made visible again
2578 -- when we install the subunit context.
2580 Item := First (Context_Items (N));
2581 while Present (Item) loop
2582 if Nkind (Item) = N_With_Clause
2584 -- Protect frontend against previous errors in context clauses
2586 and then Nkind (Name (Item)) /= N_Selected_Component
2587 and then not Error_Posted (Item)
2588 then
2589 Unit_Name := Entity (Name (Item));
2590 loop
2591 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2592 exit when Scope (Unit_Name) = Standard_Standard;
2593 Unit_Name := Scope (Unit_Name);
2594 end loop;
2596 if Context_Installed (Item) then
2597 Set_Is_Immediately_Visible (Unit_Name, False);
2598 Set_Context_Installed (Item, False);
2599 end if;
2600 end if;
2602 Next (Item);
2603 end loop;
2604 end Analyze_Subunit_Context;
2606 ------------------------
2607 -- Re_Install_Parents --
2608 ------------------------
2610 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2611 E : Entity_Id;
2613 begin
2614 if Nkind (Unit (L)) = N_Subunit then
2615 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2616 end if;
2618 Install_Context (L, False);
2620 -- If the subunit occurs within a child unit, we must restore the
2621 -- immediate visibility of any siblings that may occur in context.
2622 -- In addition, we must reset the previous visibility of the
2623 -- parent unit which is now on the scope stack. This is because
2624 -- the Previous_Visibility was previously set when removing the
2625 -- context. This is necessary to prevent the parent entity from
2626 -- remaining visible after the subunit is compiled. This only
2627 -- has an effect if a homonym exists in a body to be processed
2628 -- later if inlining is enabled.
2630 if Present (Enclosing_Child) then
2631 Install_Siblings (Enclosing_Child, L);
2632 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
2633 False;
2634 end if;
2636 Push_Scope (Scop);
2638 if Scop /= Par_Unit then
2639 Set_Is_Immediately_Visible (Scop);
2640 end if;
2642 -- Make entities in scope visible again. For child units, restore
2643 -- visibility only if they are actually in context.
2645 E := First_Entity (Current_Scope);
2646 while Present (E) loop
2647 if not Is_Child_Unit (E) or else Is_Visible_Lib_Unit (E) then
2648 Set_Is_Immediately_Visible (E);
2649 end if;
2651 Next_Entity (E);
2652 end loop;
2654 -- A subunit appears within a body, and for a nested subunits all the
2655 -- parents are bodies. Restore full visibility of their private
2656 -- entities.
2658 if Is_Package_Or_Generic_Package (Scop) then
2659 Set_In_Package_Body (Scop);
2660 Install_Private_Declarations (Scop);
2661 end if;
2662 end Re_Install_Parents;
2664 ----------------------------
2665 -- Re_Install_Use_Clauses --
2666 ----------------------------
2668 procedure Re_Install_Use_Clauses is
2669 U : Node_Id;
2670 begin
2671 for J in reverse 1 .. Num_Scopes loop
2672 U := Use_Clauses (J);
2673 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2674 Install_Use_Clauses (U);
2675 end loop;
2676 end Re_Install_Use_Clauses;
2678 ------------------
2679 -- Remove_Scope --
2680 ------------------
2682 procedure Remove_Scope is
2683 E : Entity_Id;
2685 begin
2686 Num_Scopes := Num_Scopes + 1;
2687 Use_Clauses (Num_Scopes) :=
2688 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2690 E := First_Entity (Current_Scope);
2691 while Present (E) loop
2692 Set_Is_Immediately_Visible (E, False);
2693 Next_Entity (E);
2694 end loop;
2696 if Is_Child_Unit (Current_Scope) then
2697 Enclosing_Child := Current_Scope;
2698 end if;
2700 Pop_Scope;
2701 end Remove_Scope;
2703 Saved_SM : SPARK_Mode_Type := SPARK_Mode;
2704 Saved_SMP : Node_Id := SPARK_Mode_Pragma;
2705 -- Save the SPARK mode-related data to restore on exit. Removing
2706 -- enclosing scopes and contexts to provide a clean environment for the
2707 -- context of the subunit will eliminate any previously set SPARK_Mode.
2709 -- Start of processing for Analyze_Subunit
2711 begin
2712 -- For subunit in main extended unit, we reset the configuration values
2713 -- for the non-partition-wide restrictions. For other units reset them.
2715 if In_Extended_Main_Source_Unit (N) then
2716 Restore_Config_Cunit_Boolean_Restrictions;
2717 else
2718 Reset_Cunit_Boolean_Restrictions;
2719 end if;
2721 if Style_Check then
2722 declare
2723 Nam : Node_Id := Name (Unit (N));
2725 begin
2726 if Nkind (Nam) = N_Selected_Component then
2727 Nam := Selector_Name (Nam);
2728 end if;
2730 Check_Identifier (Nam, Par_Unit);
2731 end;
2732 end if;
2734 if not Is_Empty_List (Context_Items (N)) then
2736 -- Save current use clauses
2738 Remove_Scope;
2739 Remove_Context (Lib_Unit);
2741 -- Now remove parents and their context, including enclosing subunits
2742 -- and the outer parent body which is not a subunit.
2744 if Present (Lib_Spec) then
2745 Remove_Context (Lib_Spec);
2747 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2748 Lib_Spec := Library_Unit (Lib_Spec);
2749 Remove_Scope;
2750 Remove_Context (Lib_Spec);
2751 end loop;
2753 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2754 Remove_Scope;
2755 end if;
2757 if Nkind (Unit (Lib_Spec)) in N_Package_Body | N_Subprogram_Body
2758 then
2759 Remove_Context (Library_Unit (Lib_Spec));
2760 end if;
2761 end if;
2763 Set_Is_Immediately_Visible (Par_Unit, False);
2765 Analyze_Subunit_Context;
2767 -- Take into account the effect of any SPARK_Mode configuration
2768 -- pragma, which takes precedence over a different value of
2769 -- SPARK_Mode inherited from the context of the stub.
2771 if SPARK_Mode /= None then
2772 Saved_SM := SPARK_Mode;
2773 Saved_SMP := SPARK_Mode_Pragma;
2774 end if;
2776 Re_Install_Parents (Lib_Unit, Par_Unit);
2777 Set_Is_Immediately_Visible (Par_Unit);
2779 -- If the context includes a child unit of the parent of the subunit,
2780 -- the parent will have been removed from visibility, after compiling
2781 -- that cousin in the context. The visibility of the parent must be
2782 -- restored now. This also applies if the context includes another
2783 -- subunit of the same parent which in turn includes a child unit in
2784 -- its context.
2786 if Is_Package_Or_Generic_Package (Par_Unit) then
2787 if not Is_Immediately_Visible (Par_Unit)
2788 or else (Present (First_Entity (Par_Unit))
2789 and then not
2790 Is_Immediately_Visible (First_Entity (Par_Unit)))
2791 then
2792 Set_Is_Immediately_Visible (Par_Unit);
2793 Install_Visible_Declarations (Par_Unit);
2794 Install_Private_Declarations (Par_Unit);
2795 end if;
2796 end if;
2798 Re_Install_Use_Clauses;
2799 Install_Context (N, Chain => False);
2801 -- Restore state of suppress flags for current body
2803 Scope_Suppress := Svg;
2805 -- If the subunit is within a child unit, then siblings of any parent
2806 -- unit that appear in the context clause of the subunit must also be
2807 -- made immediately visible.
2809 if Present (Enclosing_Child) then
2810 Install_Siblings (Enclosing_Child, N);
2811 end if;
2812 end if;
2814 Generate_Parent_References (Unit (N), Par_Unit);
2816 -- Reinstall the SPARK_Mode which was in effect prior to any scope and
2817 -- context manipulations, taking into account a possible SPARK_Mode
2818 -- configuration pragma if present.
2820 Install_SPARK_Mode (Saved_SM, Saved_SMP);
2822 -- If the subunit is part of a compilation unit which is subject to
2823 -- pragma Elaboration_Checks, set the model specified by the pragma
2824 -- because it applies to all parts of the unit.
2826 Install_Elaboration_Model (Par_Unit);
2828 -- The syntax rules require a proper body for a subprogram subunit
2830 if Nkind (Proper_Body (Sinfo.Nodes.Unit (N))) = N_Subprogram_Declaration
2831 then
2832 if Null_Present (Specification (Proper_Body (Sinfo.Nodes.Unit (N))))
2833 then
2834 Error_Msg_N
2835 ("null procedure not allowed as subunit",
2836 Proper_Body (Unit (N)));
2837 else
2838 Error_Msg_N
2839 ("subprogram declaration not allowed as subunit",
2840 Defining_Unit_Name (Specification (Proper_Body (Unit (N)))));
2841 end if;
2842 end if;
2844 Analyze (Proper_Body (Unit (N)));
2845 Remove_Context (N);
2847 -- The subunit may contain a with_clause on a sibling of some ancestor.
2848 -- Removing the context will remove from visibility those ancestor child
2849 -- units, which must be restored to the visibility they have in the
2850 -- enclosing body.
2852 if Present (Enclosing_Child) then
2853 declare
2854 C : Entity_Id;
2855 begin
2856 C := Current_Scope;
2857 while Present (C) and then C /= Standard_Standard loop
2858 Set_Is_Immediately_Visible (C);
2859 Set_Is_Visible_Lib_Unit (C);
2860 C := Scope (C);
2861 end loop;
2862 end;
2863 end if;
2865 -- Deal with restore of restrictions
2867 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2868 end Analyze_Subunit;
2870 ----------------------------
2871 -- Analyze_Task_Body_Stub --
2872 ----------------------------
2874 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2875 Id : constant Entity_Id := Defining_Entity (N);
2876 Loc : constant Source_Ptr := Sloc (N);
2877 Nam : Entity_Id := Current_Entity_In_Scope (Id);
2879 begin
2880 Check_Stub_Level (N);
2882 -- First occurrence of name may have been as an incomplete type
2884 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2885 Nam := Full_View (Nam);
2886 end if;
2888 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2889 Error_Msg_N ("missing specification for task body", N);
2891 else
2892 Set_Scope (Id, Current_Scope);
2893 Mutate_Ekind (Id, E_Task_Body);
2894 Set_Etype (Id, Standard_Void_Type);
2896 Analyze_Aspect_Specifications (N, Id);
2898 Generate_Reference (Nam, Id, 'b');
2899 Set_Corresponding_Spec_Of_Stub (N, Nam);
2901 -- Check for duplicate stub, if so give message and terminate
2903 if Has_Completion (Etype (Nam)) then
2904 Error_Msg_N ("duplicate stub for task", N);
2905 return;
2906 else
2907 Set_Has_Completion (Etype (Nam));
2908 end if;
2910 Analyze_Proper_Body (N, Etype (Nam));
2912 -- Set elaboration flag to indicate that entity is callable. This
2913 -- cannot be done in the expansion of the body itself, because the
2914 -- proper body is not in a declarative part. This is only done if
2915 -- expansion is active, because the context may be generic and the
2916 -- flag not defined yet.
2918 if Expander_Active then
2919 Insert_After (N,
2920 Make_Assignment_Statement (Loc,
2921 Name =>
2922 Make_Identifier (Loc,
2923 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2924 Expression => New_Occurrence_Of (Standard_True, Loc)));
2925 end if;
2926 end if;
2927 end Analyze_Task_Body_Stub;
2929 -------------------------
2930 -- Analyze_With_Clause --
2931 -------------------------
2933 -- Analyze the declaration of a unit in a with clause. At end, label the
2934 -- with clause with the defining entity for the unit.
2936 procedure Analyze_With_Clause (N : Node_Id) is
2938 -- Retrieve the original kind of the unit node, before analysis. If it
2939 -- is a subprogram instantiation, its analysis below will rewrite the
2940 -- node as the declaration of the wrapper package. If the same
2941 -- instantiation appears indirectly elsewhere in the context, it will
2942 -- have been analyzed already.
2944 Unit_Kind : constant Node_Kind :=
2945 Nkind (Original_Node (Unit (Library_Unit (N))));
2946 Nam : constant Node_Id := Name (N);
2947 E_Name : Entity_Id;
2948 Par_Name : Entity_Id;
2949 Pref : Node_Id;
2950 U : Node_Id;
2952 Intunit : Boolean;
2953 -- Set True if the unit currently being compiled is an internal unit
2955 Restriction_Violation : Boolean := False;
2956 -- Set True if a with violates a restriction, no point in giving any
2957 -- warnings if we have this definite error.
2959 Save_Style_Check : constant Boolean := Opt.Style_Check;
2961 begin
2962 U := Unit (Library_Unit (N));
2964 -- If this is an internal unit which is a renaming, then this is a
2965 -- violation of No_Obsolescent_Features.
2967 -- Note: this is not quite right if the user defines one of these units
2968 -- himself, but that's a marginal case, and fixing it is hard ???
2970 if Ada_Version >= Ada_95
2971 and then In_Predefined_Renaming (U)
2972 then
2973 if Restriction_Check_Required (No_Obsolescent_Features) then
2974 Check_Restriction (No_Obsolescent_Features, N);
2975 Restriction_Violation := True;
2976 end if;
2978 if Warn_On_Obsolescent_Feature then
2979 Error_Msg_N
2980 ("renamed predefined unit is an obsolescent feature "
2981 & "(RM J.1)?j?", N);
2982 end if;
2983 end if;
2985 -- Check No_Implementation_Units violation
2987 if Restriction_Check_Required (No_Implementation_Units) then
2988 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2989 null;
2990 else
2991 Check_Restriction (No_Implementation_Units, Nam);
2992 Restriction_Violation := True;
2993 end if;
2994 end if;
2996 -- Several actions are skipped for dummy packages (those supplied for
2997 -- with's where no matching file could be found). Such packages are
2998 -- identified by the Sloc value being set to No_Location.
3000 if Limited_Present (N) then
3002 -- Ada 2005 (AI-50217): Build visibility structures but do not
3003 -- analyze the unit.
3005 -- If the designated unit is a predefined unit, which might be used
3006 -- implicitly through the rtsfind machinery, a limited with clause
3007 -- on such a unit is usually pointless, because run-time units are
3008 -- unlikely to appear in mutually dependent units, and because this
3009 -- disables the rtsfind mechanism. We transform such limited with
3010 -- clauses into regular with clauses.
3012 if Sloc (U) /= No_Location then
3013 if In_Predefined_Unit (U) then
3014 Set_Limited_Present (N, False);
3015 Analyze_With_Clause (N);
3016 else
3017 Build_Limited_Views (N);
3018 end if;
3019 end if;
3021 return;
3022 end if;
3024 -- If we are compiling under "don't quit" mode (-gnatq) and we have
3025 -- already detected serious errors then we mark the with-clause nodes as
3026 -- analyzed before the corresponding compilation unit is analyzed. This
3027 -- is done here to protect the frontend against never ending recursion
3028 -- caused by circularities in the sources (because the previous errors
3029 -- may break the regular machine of the compiler implemented in
3030 -- Load_Unit to detect circularities).
3032 if Serious_Errors_Detected > 0 and then Try_Semantics then
3033 Set_Analyzed (N);
3034 end if;
3036 Semantics (Library_Unit (N));
3038 Intunit := Is_Internal_Unit (Current_Sem_Unit);
3040 if Sloc (U) /= No_Location then
3042 -- Check restrictions, except that we skip the check if this is an
3043 -- internal unit unless we are compiling the internal unit as the
3044 -- main unit. We also skip this for dummy packages.
3046 Check_Restriction_No_Dependence (Nam, N);
3048 if not Intunit or else Current_Sem_Unit = Main_Unit then
3049 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
3050 end if;
3052 -- Deal with special case of GNAT.Current_Exceptions which interacts
3053 -- with the optimization of local raise statements into gotos.
3055 if Nkind (Nam) = N_Selected_Component
3056 and then Nkind (Prefix (Nam)) = N_Identifier
3057 and then Chars (Prefix (Nam)) = Name_Gnat
3058 and then Chars (Selector_Name (Nam))
3059 in Name_Most_Recent_Exception | Name_Exception_Traces
3060 then
3061 Check_Restriction (No_Exception_Propagation, N);
3062 Special_Exception_Package_Used := True;
3063 end if;
3065 -- Check for inappropriate with of internal implementation unit if we
3066 -- are not compiling an internal unit and also check for withing unit
3067 -- in wrong version of Ada. Do not issue these messages for implicit
3068 -- with's generated by the compiler itself.
3070 if Implementation_Unit_Warnings
3071 and then not Intunit
3072 and then not Implicit_With (N)
3073 and then not Restriction_Violation
3074 then
3075 case Get_Kind_Of_Unit (Get_Source_Unit (U)) is
3076 when Implementation_Unit =>
3077 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
3079 -- Add alternative name if available, otherwise issue a
3080 -- general warning message.
3082 if Error_Msg_Strlen /= 0 then
3083 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
3084 else
3085 Error_Msg_F
3086 ("\use of this unit is non-portable and "
3087 & "version-dependent?i?", Name (N));
3088 end if;
3090 when Not_Predefined_Unit | Ada_95_Unit =>
3091 null; -- no checks needed
3093 when Ada_2005_Unit =>
3094 if Ada_Version < Ada_2005
3095 and then Warn_On_Ada_2005_Compatibility
3096 then
3097 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
3098 end if;
3100 when Ada_2012_Unit =>
3101 if Ada_Version < Ada_2012
3102 and then Warn_On_Ada_2012_Compatibility
3103 then
3104 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
3105 end if;
3107 when Ada_2022_Unit =>
3108 if Ada_Version < Ada_2022
3109 and then Warn_On_Ada_2022_Compatibility
3110 then
3111 Error_Msg_N ("& is an Ada 2022 unit?i?", Name (N));
3112 end if;
3113 end case;
3114 end if;
3115 end if;
3117 -- Semantic analysis of a generic unit is performed on a copy of
3118 -- the original tree. Retrieve the entity on which semantic info
3119 -- actually appears.
3121 if Unit_Kind in N_Generic_Declaration then
3122 E_Name := Defining_Entity (U);
3124 -- Note: in the following test, Unit_Kind is the original Nkind, but in
3125 -- the case of an instantiation, semantic analysis above will have
3126 -- replaced the unit by its instantiated version. If the instance body
3127 -- has been generated, the instance now denotes the body entity. For
3128 -- visibility purposes we need the entity of its spec.
3130 elsif (Unit_Kind = N_Package_Instantiation
3131 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
3132 N_Package_Instantiation)
3133 and then Nkind (U) = N_Package_Body
3134 then
3135 E_Name := Corresponding_Spec (U);
3137 elsif Unit_Kind = N_Package_Instantiation
3138 and then Nkind (U) = N_Package_Instantiation
3139 and then Present (Instance_Spec (U))
3140 then
3141 -- If the instance has not been rewritten as a package declaration,
3142 -- then it appeared already in a previous with clause. Retrieve
3143 -- the entity from the previous instance.
3145 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
3147 elsif Unit_Kind in N_Subprogram_Instantiation then
3149 -- The visible subprogram is created during instantiation, and is
3150 -- an attribute of the wrapper package. We retrieve the wrapper
3151 -- package directly from the instantiation node. If the instance
3152 -- is inlined the unit is still an instantiation. Otherwise it has
3153 -- been rewritten as the declaration of the wrapper itself.
3155 if Nkind (U) in N_Subprogram_Instantiation then
3156 E_Name :=
3157 Related_Instance
3158 (Defining_Entity (Specification (Instance_Spec (U))));
3159 else
3160 E_Name := Related_Instance (Defining_Entity (U));
3161 end if;
3163 elsif Unit_Kind = N_Package_Renaming_Declaration
3164 or else Unit_Kind in N_Generic_Renaming_Declaration
3165 then
3166 E_Name := Defining_Entity (U);
3168 elsif Unit_Kind = N_Subprogram_Body
3169 and then Nkind (Name (N)) = N_Selected_Component
3170 and then not Acts_As_Spec (Library_Unit (N))
3171 then
3172 -- For a child unit that has no spec, one has been created and
3173 -- analyzed. The entity required is that of the spec.
3175 E_Name := Corresponding_Spec (U);
3177 else
3178 E_Name := Defining_Entity (U);
3179 end if;
3181 if Nkind (Name (N)) = N_Selected_Component then
3183 -- Child unit in a with clause
3185 Change_Selected_Component_To_Expanded_Name (Name (N));
3187 -- If this is a child unit without a spec, and it has been analyzed
3188 -- already, a declaration has been created for it. The with_clause
3189 -- must reflect the actual body, and not the generated declaration,
3190 -- to prevent spurious binding errors involving an out-of-date spec.
3191 -- Note that this can only happen if the unit includes more than one
3192 -- with_clause for the child unit (e.g. in separate subunits).
3194 if Unit_Kind = N_Subprogram_Declaration
3195 and then Analyzed (Library_Unit (N))
3196 and then not Comes_From_Source (Library_Unit (N))
3197 then
3198 Set_Library_Unit (N,
3199 Cunit (Get_Source_Unit (Corresponding_Body (U))));
3200 end if;
3201 end if;
3203 -- Restore style checks
3205 Style_Check := Save_Style_Check;
3207 -- Record the reference, but do NOT set the unit as referenced, we want
3208 -- to consider the unit as unreferenced if this is the only reference
3209 -- that occurs.
3211 Set_Entity_With_Checks (Name (N), E_Name);
3212 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
3214 -- Generate references and check No_Dependence restriction for parents
3216 if Is_Child_Unit (E_Name) then
3217 Pref := Prefix (Name (N));
3218 Par_Name := Scope (E_Name);
3219 while Nkind (Pref) = N_Selected_Component loop
3220 Change_Selected_Component_To_Expanded_Name (Pref);
3222 if Present (Entity (Selector_Name (Pref)))
3223 and then
3224 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
3225 and then Entity (Selector_Name (Pref)) /= Par_Name
3226 then
3227 -- The prefix is a child unit that denotes a renaming declaration.
3228 -- Replace the prefix directly with the renamed unit, because the
3229 -- rest of the prefix is irrelevant to the visibility of the real
3230 -- unit.
3232 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
3233 exit;
3234 end if;
3236 Set_Entity_With_Checks (Pref, Par_Name);
3238 Generate_Reference (Par_Name, Pref);
3239 Check_Restriction_No_Dependence (Pref, N);
3240 Pref := Prefix (Pref);
3242 -- If E_Name is the dummy entity for a nonexistent unit, its scope
3243 -- is set to Standard_Standard, and no attempt should be made to
3244 -- further unwind scopes.
3246 if Par_Name /= Standard_Standard then
3247 Par_Name := Scope (Par_Name);
3248 end if;
3250 -- Abandon processing in case of previous errors
3252 if No (Par_Name) then
3253 Check_Error_Detected;
3254 return;
3255 end if;
3256 end loop;
3258 if Present (Entity (Pref))
3259 and then not Analyzed (Parent (Parent (Entity (Pref))))
3260 then
3261 -- If the entity is set without its unit being compiled, the
3262 -- original parent is a renaming, and Par_Name is the renamed
3263 -- entity. For visibility purposes, we need the original entity,
3264 -- which must be analyzed now because Load_Unit directly retrieves
3265 -- the renamed unit, and the renaming declaration itself has not
3266 -- been analyzed.
3268 Analyze (Parent (Parent (Entity (Pref))));
3269 pragma Assert (Renamed_Entity (Entity (Pref)) = Par_Name);
3270 Par_Name := Entity (Pref);
3271 end if;
3273 -- Guard against missing or misspelled child units
3275 if Present (Par_Name) then
3276 Set_Entity_With_Checks (Pref, Par_Name);
3277 Generate_Reference (Par_Name, Pref);
3279 else
3280 pragma Assert (Serious_Errors_Detected /= 0);
3282 -- Mark the node to indicate that a related error has been posted.
3283 -- This defends further compilation passes against improper use of
3284 -- the invalid WITH clause node.
3286 Set_Error_Posted (N);
3287 Set_Name (N, Error);
3288 return;
3289 end if;
3290 end if;
3292 -- If the withed unit is System, and a system extension pragma is
3293 -- present, compile the extension now, rather than waiting for a
3294 -- visibility check on a specific entity.
3296 if Chars (E_Name) = Name_System
3297 and then Scope (E_Name) = Standard_Standard
3298 and then Present (System_Extend_Unit)
3299 and then Present_System_Aux (N)
3300 then
3301 -- If the extension is not present, an error will have been emitted
3303 null;
3304 end if;
3306 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
3307 -- to private_with units; they will be made visible later (just before
3308 -- the private part is analyzed)
3310 if Private_Present (N) then
3311 Set_Is_Immediately_Visible (E_Name, False);
3312 end if;
3314 -- Propagate Fatal_Error setting from with'ed unit to current unit
3316 case Fatal_Error (Get_Source_Unit (Library_Unit (N))) is
3318 -- Nothing to do if with'ed unit had no error
3320 when None =>
3321 null;
3323 -- If with'ed unit had a detected fatal error, propagate it
3325 when Error_Detected =>
3326 Set_Fatal_Error (Current_Sem_Unit, Error_Detected);
3328 -- If with'ed unit had an ignored error, then propagate it but do not
3329 -- overide an existring setting.
3331 when Error_Ignored =>
3332 if Fatal_Error (Current_Sem_Unit) = None then
3333 Set_Fatal_Error (Current_Sem_Unit, Error_Ignored);
3334 end if;
3335 end case;
3336 end Analyze_With_Clause;
3338 ------------------------------
3339 -- Check_Private_Child_Unit --
3340 ------------------------------
3342 procedure Check_Private_Child_Unit (N : Node_Id) is
3343 Lib_Unit : constant Node_Id := Unit (N);
3344 Item : Node_Id;
3345 Curr_Unit : Entity_Id;
3346 Sub_Parent : Node_Id;
3347 Priv_Child : Entity_Id;
3348 Par_Lib : Entity_Id;
3349 Par_Spec : Node_Id;
3351 begin
3352 if Nkind (Lib_Unit) in N_Package_Body | N_Subprogram_Body then
3353 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
3354 Par_Lib := Curr_Unit;
3356 elsif Nkind (Lib_Unit) = N_Subunit then
3358 -- The parent is itself a body. The parent entity is to be found in
3359 -- the corresponding spec.
3361 Sub_Parent := Library_Unit (N);
3362 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
3364 -- If the parent itself is a subunit, Curr_Unit is the entity of the
3365 -- enclosing body, retrieve the spec entity which is the proper
3366 -- ancestor we need for the following tests.
3368 if Ekind (Curr_Unit) = E_Package_Body then
3369 Curr_Unit := Spec_Entity (Curr_Unit);
3370 end if;
3372 Par_Lib := Curr_Unit;
3374 else
3375 Curr_Unit := Defining_Entity (Lib_Unit);
3377 Par_Lib := Curr_Unit;
3378 Par_Spec := Parent_Spec (Lib_Unit);
3380 if No (Par_Spec) then
3381 Par_Lib := Empty;
3382 else
3383 Par_Lib := Defining_Entity (Unit (Par_Spec));
3384 end if;
3385 end if;
3387 -- Loop through context items
3389 Item := First (Context_Items (N));
3390 while Present (Item) loop
3392 -- Ada 2005 (AI-262): Allow private_with of a private child package
3393 -- in public siblings
3395 if Nkind (Item) = N_With_Clause
3396 and then not Implicit_With (Item)
3397 and then not Limited_Present (Item)
3398 and then Is_Private_Descendant (Entity (Name (Item)))
3399 then
3400 Priv_Child := Entity (Name (Item));
3402 declare
3403 Curr_Parent : Entity_Id := Par_Lib;
3404 Child_Parent : Entity_Id := Scope (Priv_Child);
3405 Prv_Ancestor : Entity_Id := Child_Parent;
3406 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
3408 begin
3409 -- If the child unit is a public child then locate the nearest
3410 -- private ancestor. Child_Parent will then be set to the
3411 -- parent of that ancestor.
3413 if not Is_Private_Library_Unit (Priv_Child) then
3414 while Present (Prv_Ancestor)
3415 and then not Is_Private_Library_Unit (Prv_Ancestor)
3416 loop
3417 Prv_Ancestor := Scope (Prv_Ancestor);
3418 end loop;
3420 if Present (Prv_Ancestor) then
3421 Child_Parent := Scope (Prv_Ancestor);
3422 end if;
3423 end if;
3425 while Present (Curr_Parent)
3426 and then Curr_Parent /= Standard_Standard
3427 and then Curr_Parent /= Child_Parent
3428 loop
3429 Curr_Private :=
3430 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
3431 Curr_Parent := Scope (Curr_Parent);
3432 end loop;
3434 if No (Curr_Parent) then
3435 Curr_Parent := Standard_Standard;
3436 end if;
3438 if Curr_Parent /= Child_Parent then
3439 if Ekind (Priv_Child) = E_Generic_Package
3440 and then Chars (Priv_Child) in Text_IO_Package_Name
3441 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
3442 and then Scope (Scope (Scope (Priv_Child))) =
3443 Standard_Standard
3444 then
3445 Error_Msg_NE
3446 ("& is a nested package, not a compilation unit",
3447 Name (Item), Priv_Child);
3449 else
3450 Error_Msg_N
3451 ("unit in with clause is private child unit!", Item);
3452 Error_Msg_NE
3453 ("\current unit must also have parent&!",
3454 Item, Child_Parent);
3455 end if;
3457 elsif Curr_Private
3458 or else Private_Present (Item)
3459 or else Nkind (Lib_Unit) in N_Package_Body | N_Subunit
3460 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3461 and then not Acts_As_Spec (Parent (Lib_Unit)))
3462 then
3463 null;
3465 else
3466 Error_Msg_NE
3467 ("current unit must also be private descendant of&",
3468 Item, Child_Parent);
3469 end if;
3470 end;
3471 end if;
3473 Next (Item);
3474 end loop;
3475 end Check_Private_Child_Unit;
3477 ----------------------
3478 -- Check_Stub_Level --
3479 ----------------------
3481 procedure Check_Stub_Level (N : Node_Id) is
3482 Par : constant Node_Id := Parent (N);
3483 Kind : constant Node_Kind := Nkind (Par);
3485 begin
3486 if Kind in
3487 N_Package_Body | N_Subprogram_Body | N_Task_Body | N_Protected_Body
3488 and then Nkind (Parent (Par)) in N_Compilation_Unit | N_Subunit
3489 then
3490 null;
3492 -- In an instance, a missing stub appears at any level. A warning
3493 -- message will have been emitted already for the missing file.
3495 elsif not In_Instance then
3496 Error_Msg_N ("stub cannot appear in an inner scope", N);
3498 elsif Expander_Active then
3499 Error_Msg_N ("missing proper body", N);
3500 end if;
3501 end Check_Stub_Level;
3503 -------------------
3504 -- Decorate_Type --
3505 -------------------
3507 procedure Decorate_Type
3508 (Ent : Entity_Id;
3509 Scop : Entity_Id;
3510 Is_Tagged : Boolean := False;
3511 Materialize : Boolean := False)
3513 CW_Typ : Entity_Id;
3515 begin
3516 -- An unanalyzed type or a shadow entity of a type is treated as an
3517 -- incomplete type, and carries the corresponding attributes.
3519 Mutate_Ekind (Ent, E_Incomplete_Type);
3520 Set_Is_Not_Self_Hidden (Ent);
3521 Set_Etype (Ent, Ent);
3522 Set_Full_View (Ent, Empty);
3523 Set_Is_First_Subtype (Ent);
3524 Set_Scope (Ent, Scop);
3525 Set_Stored_Constraint (Ent, No_Elist);
3526 Reinit_Size_Align (Ent);
3528 if From_Limited_With (Ent) then
3529 Set_Private_Dependents (Ent, New_Elmt_List);
3530 end if;
3532 -- A tagged type and its corresponding shadow entity share one common
3533 -- class-wide type. The list of primitive operations for the shadow
3534 -- entity is empty.
3536 if Is_Tagged then
3537 Set_Is_Tagged_Type (Ent);
3538 Set_Direct_Primitive_Operations (Ent, New_Elmt_List);
3540 CW_Typ :=
3541 New_External_Entity
3542 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
3544 Set_Class_Wide_Type (Ent, CW_Typ);
3546 -- Set parent to be the same as the parent of the tagged type.
3547 -- We need a parent field set, and it is supposed to point to
3548 -- the declaration of the type. The tagged type declaration
3549 -- essentially declares two separate types, the tagged type
3550 -- itself and the corresponding class-wide type, so it is
3551 -- reasonable for the parent fields to point to the declaration
3552 -- in both cases.
3554 Set_Parent (CW_Typ, Parent (Ent));
3556 Mutate_Ekind (CW_Typ, E_Class_Wide_Type);
3557 Set_Class_Wide_Type (CW_Typ, CW_Typ);
3558 Set_Etype (CW_Typ, Ent);
3559 Set_Equivalent_Type (CW_Typ, Empty);
3560 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
3561 Set_Has_Unknown_Discriminants (CW_Typ);
3562 Set_Is_First_Subtype (CW_Typ);
3563 Set_Is_Tagged_Type (CW_Typ);
3564 Set_Materialize_Entity (CW_Typ, Materialize);
3565 Set_Scope (CW_Typ, Scop);
3566 Reinit_Size_Align (CW_Typ);
3567 end if;
3568 end Decorate_Type;
3570 ------------------------
3571 -- Expand_With_Clause --
3572 ------------------------
3574 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
3575 Loc : constant Source_Ptr := Sloc (Nam);
3577 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
3578 -- Build name to be used in implicit with_clause. In most cases this
3579 -- is the source name, but if renamings are present we must make the
3580 -- original unit visible, not the one it renames. The entity in the
3581 -- with clause is the renamed unit, but the identifier is the one from
3582 -- the source, which allows us to recover the unit renaming.
3584 ---------------------
3585 -- Build_Unit_Name --
3586 ---------------------
3588 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
3589 Ent : Entity_Id;
3590 Result : Node_Id;
3592 begin
3593 if Nkind (Nam) = N_Identifier then
3594 return New_Occurrence_Of (Entity (Nam), Loc);
3596 else
3597 Ent := Entity (Nam);
3599 if Present (Entity (Selector_Name (Nam)))
3600 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
3601 and then
3602 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam)))) =
3603 N_Package_Renaming_Declaration
3604 then
3605 -- The name in the with_clause is of the form A.B.C, and B is
3606 -- given by a renaming declaration. In that case we may not
3607 -- have analyzed the unit for B, but replaced it directly in
3608 -- lib-load with the unit it renames. We have to make A.B
3609 -- visible, so analyze the declaration for B now, in case it
3610 -- has not been done yet.
3612 Ent := Entity (Selector_Name (Nam));
3613 Analyze
3614 (Parent
3615 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3616 end if;
3618 Result :=
3619 Make_Expanded_Name (Loc,
3620 Chars => Chars (Entity (Nam)),
3621 Prefix => Build_Unit_Name (Prefix (Nam)),
3622 Selector_Name => New_Occurrence_Of (Ent, Loc));
3623 Set_Entity (Result, Ent);
3625 return Result;
3626 end if;
3627 end Build_Unit_Name;
3629 -- Local variables
3631 Ent : constant Entity_Id := Entity (Nam);
3632 Withn : constant Node_Id :=
3633 Make_With_Clause
3634 (Loc, Name => Build_Unit_Name (Nam),
3635 First_Name => True, Last_Name => True);
3637 -- Start of processing for Expand_With_Clause
3639 begin
3640 Set_Corresponding_Spec (Withn, Ent);
3641 Set_Implicit_With (Withn);
3642 Set_Library_Unit (Withn, Parent (Unit_Declaration_Node (Ent)));
3643 Set_Parent_With (Withn);
3645 -- If the unit is a [generic] package or subprogram declaration
3646 -- (including a subprogram body acting as spec), a private_with_clause
3647 -- on a child unit implies that the implicit with on the parent is also
3648 -- private.
3650 if Nkind (Unit (N)) in N_Generic_Package_Declaration
3651 | N_Package_Declaration
3652 | N_Generic_Subprogram_Declaration
3653 | N_Subprogram_Declaration
3654 | N_Subprogram_Body
3655 then
3656 Set_Private_Present (Withn, Private_Present (Item));
3657 end if;
3659 Prepend (Withn, Context_Items (N));
3660 Mark_Rewrite_Insertion (Withn);
3662 Install_With_Clause (Withn);
3664 -- If we have "with X.Y;", we want to recurse on "X", except in the
3665 -- unusual case where X.Y is a renaming of X. In that case, the scope
3666 -- of X will be null.
3668 if Nkind (Nam) = N_Expanded_Name
3669 and then Present (Scope (Entity (Prefix (Nam))))
3670 then
3671 Expand_With_Clause (Item, Prefix (Nam), N);
3672 end if;
3673 end Expand_With_Clause;
3675 --------------------------------
3676 -- Generate_Parent_References --
3677 --------------------------------
3679 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
3680 Pref : Node_Id;
3681 P_Name : Entity_Id := P_Id;
3683 begin
3684 if Nkind (N) = N_Subunit then
3685 Pref := Name (N);
3686 else
3687 Pref := Name (Parent (Defining_Entity (N)));
3688 end if;
3690 if Nkind (Pref) = N_Expanded_Name then
3692 -- Done already, if the unit has been compiled indirectly as
3693 -- part of the closure of its context because of inlining.
3695 return;
3696 end if;
3698 while Nkind (Pref) = N_Selected_Component loop
3699 Change_Selected_Component_To_Expanded_Name (Pref);
3700 Set_Entity (Pref, P_Name);
3701 Set_Etype (Pref, Etype (P_Name));
3702 Generate_Reference (P_Name, Pref, 'r');
3703 Pref := Prefix (Pref);
3704 P_Name := Scope (P_Name);
3705 end loop;
3707 -- The guard here on P_Name is to handle the error condition where
3708 -- the parent unit is missing because the file was not found.
3710 if Present (P_Name) then
3711 Set_Entity (Pref, P_Name);
3712 Set_Etype (Pref, Etype (P_Name));
3713 Generate_Reference (P_Name, Pref, 'r');
3714 Style.Check_Identifier (Pref, P_Name);
3715 end if;
3716 end Generate_Parent_References;
3718 ---------------------
3719 -- Has_With_Clause --
3720 ---------------------
3722 function Has_With_Clause
3723 (C_Unit : Node_Id;
3724 Pack : Entity_Id;
3725 Is_Limited : Boolean := False) return Boolean
3727 Item : Node_Id;
3729 function Named_Unit (Clause : Node_Id) return Entity_Id;
3730 -- Return the entity for the unit named in a [limited] with clause
3732 ----------------
3733 -- Named_Unit --
3734 ----------------
3736 function Named_Unit (Clause : Node_Id) return Entity_Id is
3737 begin
3738 if Nkind (Name (Clause)) = N_Selected_Component then
3739 return Entity (Selector_Name (Name (Clause)));
3740 else
3741 return Entity (Name (Clause));
3742 end if;
3743 end Named_Unit;
3745 -- Start of processing for Has_With_Clause
3747 begin
3748 Item := First (Context_Items (C_Unit));
3749 while Present (Item) loop
3750 if Nkind (Item) = N_With_Clause
3751 and then Limited_Present (Item) = Is_Limited
3752 and then Named_Unit (Item) = Pack
3753 then
3754 return True;
3755 end if;
3757 Next (Item);
3758 end loop;
3760 return False;
3761 end Has_With_Clause;
3763 -----------------------------
3764 -- Implicit_With_On_Parent --
3765 -----------------------------
3767 procedure Implicit_With_On_Parent
3768 (Child_Unit : Node_Id;
3769 N : Node_Id)
3771 Loc : constant Source_Ptr := Sloc (N);
3772 P : constant Node_Id := Parent_Spec (Child_Unit);
3773 P_Unit : Node_Id := Unit (P);
3774 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3776 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3777 -- Build prefix of child unit name. Recurse if needed
3779 function Build_Unit_Name return Node_Id;
3780 -- If the unit is a child unit, build qualified name with all ancestors
3782 -------------------------
3783 -- Build_Ancestor_Name --
3784 -------------------------
3786 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3787 P_Ref : constant Node_Id :=
3788 New_Occurrence_Of (Defining_Entity (P), Loc);
3789 P_Spec : Node_Id := P;
3791 begin
3792 -- Ancestor may have been rewritten as a package body. Retrieve the
3793 -- original spec to trace earlier ancestors.
3795 if Nkind (P) = N_Package_Body
3796 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3797 then
3798 P_Spec := Original_Node (P);
3799 end if;
3801 if No (Parent_Spec (P_Spec)) then
3802 return P_Ref;
3803 else
3804 return
3805 Make_Selected_Component (Loc,
3806 Prefix =>
3807 Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3808 Selector_Name => P_Ref);
3809 end if;
3810 end Build_Ancestor_Name;
3812 ---------------------
3813 -- Build_Unit_Name --
3814 ---------------------
3816 function Build_Unit_Name return Node_Id is
3817 Result : Node_Id;
3819 begin
3820 if No (Parent_Spec (P_Unit)) then
3821 return New_Occurrence_Of (P_Name, Loc);
3823 else
3824 Result :=
3825 Make_Expanded_Name (Loc,
3826 Chars => Chars (P_Name),
3827 Prefix =>
3828 Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3829 Selector_Name => New_Occurrence_Of (P_Name, Loc));
3830 Set_Entity (Result, P_Name);
3832 return Result;
3833 end if;
3834 end Build_Unit_Name;
3836 -- Start of processing for Implicit_With_On_Parent
3838 begin
3839 -- The unit of the current compilation may be a package body that
3840 -- replaces an instance node. In this case we need the original instance
3841 -- node to construct the proper parent name.
3843 if Nkind (P_Unit) = N_Package_Body
3844 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3845 then
3846 P_Unit := Original_Node (P_Unit);
3847 end if;
3849 -- We add the implicit with if the child unit is the current unit being
3850 -- compiled. If the current unit is a body, we do not want to add an
3851 -- implicit_with a second time to the corresponding spec.
3853 if Nkind (Child_Unit) = N_Package_Declaration
3854 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3855 then
3856 return;
3857 end if;
3859 declare
3860 Withn : constant Node_Id :=
3861 Make_With_Clause
3862 (Loc, Name => Build_Unit_Name,
3863 First_Name => True, Last_Name => True);
3864 begin
3865 Set_Corresponding_Spec (Withn, P_Name);
3866 Set_Implicit_With (Withn);
3867 Set_Library_Unit (Withn, P);
3868 Set_Parent_With (Withn);
3870 -- Node is placed at the beginning of the context items, so that
3871 -- subsequent use clauses on the parent can be validated.
3873 Prepend (Withn, Context_Items (N));
3874 Mark_Rewrite_Insertion (Withn);
3876 Install_With_Clause (Withn);
3877 end;
3879 if Is_Child_Spec (P_Unit) then
3880 Implicit_With_On_Parent (P_Unit, N);
3881 end if;
3882 end Implicit_With_On_Parent;
3884 --------------
3885 -- In_Chain --
3886 --------------
3888 function In_Chain (E : Entity_Id) return Boolean is
3889 H : Entity_Id;
3891 begin
3892 H := Current_Entity (E);
3893 while Present (H) loop
3894 if H = E then
3895 return True;
3896 else
3897 H := Homonym (H);
3898 end if;
3899 end loop;
3901 return False;
3902 end In_Chain;
3904 ---------------------
3905 -- Install_Context --
3906 ---------------------
3908 procedure Install_Context (N : Node_Id; Chain : Boolean := True) is
3909 Lib_Unit : constant Node_Id := Unit (N);
3911 begin
3912 Install_Context_Clauses (N, Chain);
3914 if Is_Child_Spec (Lib_Unit) then
3915 Install_Parents
3916 (Lib_Unit => Lib_Unit,
3917 Is_Private => Private_Present (Parent (Lib_Unit)),
3918 Chain => Chain);
3919 end if;
3921 Install_Limited_Context_Clauses (N);
3922 end Install_Context;
3924 -----------------------------
3925 -- Install_Context_Clauses --
3926 -----------------------------
3928 procedure Install_Context_Clauses (N : Node_Id; Chain : Boolean := True) is
3929 Lib_Unit : constant Node_Id := Unit (N);
3930 Item : Node_Id;
3931 Uname_Node : Entity_Id;
3932 Check_Private : Boolean := False;
3933 Decl_Node : Node_Id;
3934 Lib_Parent : Entity_Id;
3936 begin
3937 -- First skip configuration pragmas at the start of the context. They
3938 -- are not technically part of the context clause, but that's where the
3939 -- parser puts them. Note they were analyzed in Analyze_Context.
3941 Item := First (Context_Items (N));
3942 while Present (Item)
3943 and then Nkind (Item) = N_Pragma
3944 and then Pragma_Name (Item) in Configuration_Pragma_Names
3945 loop
3946 Next (Item);
3947 end loop;
3949 -- Loop through the actual context clause items. We process everything
3950 -- except Limited_With clauses in this routine. Limited_With clauses
3951 -- are separately installed (see Install_Limited_Context_Clauses).
3953 while Present (Item) loop
3955 -- Case of explicit WITH clause
3957 if Nkind (Item) = N_With_Clause
3958 and then not Implicit_With (Item)
3959 then
3960 if Limited_Present (Item) then
3962 -- Limited withed units will be installed later
3964 goto Continue;
3966 -- If Name (Item) is not an entity name, something is wrong, and
3967 -- this will be detected in due course, for now ignore the item
3969 elsif not Is_Entity_Name (Name (Item)) then
3970 goto Continue;
3972 elsif No (Entity (Name (Item))) then
3973 Set_Entity (Name (Item), Any_Id);
3974 goto Continue;
3975 end if;
3977 Uname_Node := Entity (Name (Item));
3979 if Is_Private_Descendant (Uname_Node) then
3980 Check_Private := True;
3981 end if;
3983 Install_With_Clause (Item);
3985 Decl_Node := Unit_Declaration_Node (Uname_Node);
3987 -- If the unit is a subprogram instance, it appears nested within
3988 -- a package that carries the parent information.
3990 if Is_Generic_Instance (Uname_Node)
3991 and then Ekind (Uname_Node) /= E_Package
3992 then
3993 Decl_Node := Parent (Parent (Decl_Node));
3994 end if;
3996 if Is_Child_Spec (Decl_Node) then
3997 if Nkind (Name (Item)) = N_Expanded_Name then
3998 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3999 else
4000 -- If not an expanded name, the child unit must be a
4001 -- renaming, nothing to do.
4003 null;
4004 end if;
4006 elsif Nkind (Decl_Node) = N_Subprogram_Body
4007 and then not Acts_As_Spec (Parent (Decl_Node))
4008 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
4009 then
4010 Implicit_With_On_Parent
4011 (Unit (Library_Unit (Parent (Decl_Node))), N);
4012 end if;
4014 -- Check license conditions unless this is a dummy unit
4016 if Sloc (Library_Unit (Item)) /= No_Location then
4017 License_Check : declare
4018 Withu : constant Unit_Number_Type :=
4019 Get_Source_Unit (Library_Unit (Item));
4020 Withl : constant License_Type :=
4021 License (Source_Index (Withu));
4022 Unitl : constant License_Type :=
4023 License (Source_Index (Current_Sem_Unit));
4025 procedure License_Error;
4026 -- Signal error of bad license
4028 -------------------
4029 -- License_Error --
4030 -------------------
4032 procedure License_Error is
4033 begin
4034 Error_Msg_N
4035 ("license of withed unit & may be inconsistent??",
4036 Name (Item));
4037 end License_Error;
4039 -- Start of processing for License_Check
4041 begin
4042 -- Exclude license check if withed unit is an internal unit.
4043 -- This situation arises e.g. with the GPL version of GNAT.
4045 if Is_Internal_Unit (Withu) then
4046 null;
4048 -- Otherwise check various cases
4049 else
4050 case Unitl is
4051 when Unknown =>
4052 null;
4054 when Restricted =>
4055 if Withl = GPL then
4056 License_Error;
4057 end if;
4059 when GPL =>
4060 if Withl = Restricted then
4061 License_Error;
4062 end if;
4064 when Modified_GPL =>
4065 if Withl = Restricted or else Withl = GPL then
4066 License_Error;
4067 end if;
4069 when Unrestricted =>
4070 null;
4071 end case;
4072 end if;
4073 end License_Check;
4074 end if;
4076 -- Case of USE PACKAGE clause
4078 elsif Nkind (Item) = N_Use_Package_Clause then
4079 Analyze_Use_Package (Item, Chain);
4081 -- Case of USE TYPE clause
4083 elsif Nkind (Item) = N_Use_Type_Clause then
4084 Analyze_Use_Type (Item, Chain);
4086 -- case of PRAGMA
4088 elsif Nkind (Item) = N_Pragma then
4089 Analyze (Item);
4090 end if;
4092 <<Continue>>
4093 Next (Item);
4094 end loop;
4096 if Is_Child_Spec (Lib_Unit) then
4098 -- The unit also has implicit with_clauses on its own parents
4100 if No (Context_Items (N)) then
4101 Set_Context_Items (N, New_List);
4102 end if;
4104 Implicit_With_On_Parent (Lib_Unit, N);
4105 end if;
4107 -- If the unit is a body, the context of the specification must also
4108 -- be installed. That includes private with_clauses in that context.
4110 if Nkind (Lib_Unit) = N_Package_Body
4111 or else (Nkind (Lib_Unit) = N_Subprogram_Body
4112 and then not Acts_As_Spec (N))
4113 then
4114 Install_Context (Library_Unit (N), Chain);
4116 -- Only install private with-clauses of a spec that comes from
4117 -- source, excluding specs created for a subprogram body that is
4118 -- a child unit.
4120 if Comes_From_Source (Library_Unit (N)) then
4121 Install_Private_With_Clauses
4122 (Defining_Entity (Unit (Library_Unit (N))));
4123 end if;
4125 if Is_Child_Spec (Unit (Library_Unit (N))) then
4127 -- If the unit is the body of a public child unit, the private
4128 -- declarations of the parent must be made visible. If the child
4129 -- unit is private, the private declarations have been installed
4130 -- already in the call to Install_Parents for the spec. Installing
4131 -- private declarations must be done for all ancestors of public
4132 -- child units. In addition, sibling units mentioned in the
4133 -- context clause of the body are directly visible.
4135 declare
4136 Lib_Spec : Node_Id;
4137 P : Node_Id;
4138 P_Name : Entity_Id;
4140 begin
4141 Lib_Spec := Unit (Library_Unit (N));
4142 while Is_Child_Spec (Lib_Spec) loop
4143 P := Unit (Parent_Spec (Lib_Spec));
4144 P_Name := Defining_Entity (P);
4146 if not (Private_Present (Parent (Lib_Spec)))
4147 and then not In_Private_Part (P_Name)
4148 then
4149 Install_Private_Declarations (P_Name);
4150 Install_Private_With_Clauses (P_Name);
4151 Set_Use (Private_Declarations (Specification (P)));
4152 end if;
4154 Lib_Spec := P;
4155 end loop;
4156 end;
4157 end if;
4159 -- For a package body, children in context are immediately visible
4161 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
4162 end if;
4164 if Nkind (Lib_Unit) in N_Generic_Package_Declaration
4165 | N_Generic_Subprogram_Declaration
4166 | N_Package_Declaration
4167 | N_Subprogram_Declaration
4168 then
4169 if Is_Child_Spec (Lib_Unit) then
4170 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
4171 Set_Is_Private_Descendant
4172 (Defining_Entity (Lib_Unit),
4173 Is_Private_Descendant (Lib_Parent)
4174 or else Private_Present (Parent (Lib_Unit)));
4176 else
4177 Set_Is_Private_Descendant
4178 (Defining_Entity (Lib_Unit),
4179 Private_Present (Parent (Lib_Unit)));
4180 end if;
4181 end if;
4183 if Check_Private then
4184 Check_Private_Child_Unit (N);
4185 end if;
4186 end Install_Context_Clauses;
4188 -------------------------------------
4189 -- Install_Limited_Context_Clauses --
4190 -------------------------------------
4192 procedure Install_Limited_Context_Clauses (N : Node_Id) is
4193 Item : Node_Id;
4195 procedure Check_Renamings (P : Node_Id; W : Node_Id);
4196 -- Check that the unlimited view of a given compilation_unit is not
4197 -- already visible through "use + renamings".
4199 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
4200 -- Check that if a limited_with clause of a given compilation_unit
4201 -- mentions a descendant of a private child of some library unit, then
4202 -- the given compilation_unit must be the declaration of a private
4203 -- descendant of that library unit, or a public descendant of such. The
4204 -- code is analogous to that of Check_Private_Child_Unit but we cannot
4205 -- use entities on the limited with_clauses because their units have not
4206 -- been analyzed, so we have to climb the tree of ancestors looking for
4207 -- private keywords.
4209 procedure Expand_Limited_With_Clause
4210 (Comp_Unit : Node_Id;
4211 Nam : Node_Id;
4212 N : Node_Id);
4213 -- If a child unit appears in a limited_with clause, there are implicit
4214 -- limited_with clauses on all parents that are not already visible
4215 -- through a regular with clause. This procedure creates the implicit
4216 -- limited with_clauses for the parents and loads the corresponding
4217 -- units. The shadow entities are created when the inserted clause is
4218 -- analyzed. Implements Ada 2005 (AI-50217).
4220 ---------------------
4221 -- Check_Renamings --
4222 ---------------------
4224 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
4225 Item : Node_Id;
4226 Spec : Node_Id;
4227 WEnt : Entity_Id;
4228 E : Entity_Id;
4229 E2 : Entity_Id;
4231 begin
4232 pragma Assert (Nkind (W) = N_With_Clause);
4234 -- Protect the frontend against previous critical errors
4236 case Nkind (Unit (Library_Unit (W))) is
4237 when N_Generic_Package_Declaration
4238 | N_Generic_Subprogram_Declaration
4239 | N_Package_Declaration
4240 | N_Subprogram_Declaration
4242 null;
4244 when others =>
4245 return;
4246 end case;
4248 -- Check "use + renamings"
4250 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
4251 Spec := Specification (Unit (P));
4253 Item := First (Visible_Declarations (Spec));
4254 while Present (Item) loop
4256 -- Look only at use package clauses
4258 if Nkind (Item) = N_Use_Package_Clause then
4260 E := Entity (Name (Item));
4262 pragma Assert (Present (Parent (E)));
4264 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
4265 and then Renamed_Entity (E) = WEnt
4266 then
4267 -- The unlimited view is visible through use clause and
4268 -- renamings. There is no need to generate the error
4269 -- message here because Is_Visible_Through_Renamings
4270 -- takes care of generating the precise error message.
4272 return;
4274 elsif Nkind (Parent (E)) = N_Package_Specification then
4276 -- The use clause may refer to a local package.
4277 -- Check all the enclosing scopes.
4279 E2 := E;
4280 while E2 /= Standard_Standard and then E2 /= WEnt loop
4281 E2 := Scope (E2);
4282 end loop;
4284 if E2 = WEnt then
4285 Error_Msg_N
4286 ("unlimited view visible through use clause", W);
4287 return;
4288 end if;
4289 end if;
4290 end if;
4292 Next (Item);
4293 end loop;
4295 -- Recursive call to check all the ancestors
4297 if Is_Child_Spec (Unit (P)) then
4298 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
4299 end if;
4300 end Check_Renamings;
4302 ---------------------------------------
4303 -- Check_Private_Limited_Withed_Unit --
4304 ---------------------------------------
4306 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
4307 Curr_Parent : Node_Id;
4308 Child_Parent : Node_Id;
4309 Curr_Private : Boolean;
4311 begin
4312 -- Compilation unit of the parent of the withed library unit
4314 Child_Parent := Library_Unit (Item);
4316 -- If the child unit is a public child, then locate its nearest
4317 -- private ancestor, if any, then Child_Parent will then be set to
4318 -- the parent of that ancestor.
4320 if not Private_Present (Library_Unit (Item)) then
4321 while Present (Child_Parent)
4322 and then not Private_Present (Child_Parent)
4323 loop
4324 Child_Parent := Parent_Spec (Unit (Child_Parent));
4325 end loop;
4327 if No (Child_Parent) then
4328 return;
4329 end if;
4330 end if;
4332 Child_Parent := Parent_Spec (Unit (Child_Parent));
4334 -- Traverse all the ancestors of the current compilation unit to
4335 -- check if it is a descendant of named library unit.
4337 Curr_Parent := Parent (Item);
4338 Curr_Private := Private_Present (Curr_Parent);
4340 while Present (Parent_Spec (Unit (Curr_Parent)))
4341 and then Curr_Parent /= Child_Parent
4342 loop
4343 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
4344 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
4345 end loop;
4347 if Curr_Parent /= Child_Parent then
4348 Error_Msg_N
4349 ("unit in with clause is private child unit!", Item);
4350 Error_Msg_NE
4351 ("\current unit must also have parent&!",
4352 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
4354 elsif Private_Present (Parent (Item))
4355 or else Curr_Private
4356 or else Private_Present (Item)
4357 or else Nkind (Unit (Parent (Item))) in
4358 N_Package_Body | N_Subprogram_Body | N_Subunit
4359 then
4360 -- Current unit is private, of descendant of a private unit
4362 null;
4364 else
4365 Error_Msg_NE
4366 ("current unit must also be private descendant of&",
4367 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
4368 end if;
4369 end Check_Private_Limited_Withed_Unit;
4371 --------------------------------
4372 -- Expand_Limited_With_Clause --
4373 --------------------------------
4375 procedure Expand_Limited_With_Clause
4376 (Comp_Unit : Node_Id;
4377 Nam : Node_Id;
4378 N : Node_Id)
4380 Loc : constant Source_Ptr := Sloc (Nam);
4381 Unum : Unit_Number_Type;
4382 Withn : Node_Id;
4384 function Previous_Withed_Unit (W : Node_Id) return Boolean;
4385 -- Returns true if the context already includes a with_clause for
4386 -- this unit. If the with_clause is nonlimited, the unit is fully
4387 -- visible and an implicit limited_with should not be created. If
4388 -- there is already a limited_with clause for W, a second one is
4389 -- simply redundant.
4391 --------------------------
4392 -- Previous_Withed_Unit --
4393 --------------------------
4395 function Previous_Withed_Unit (W : Node_Id) return Boolean is
4396 Item : Node_Id;
4398 begin
4399 -- A limited with_clause cannot appear in the same context_clause
4400 -- as a nonlimited with_clause which mentions the same library.
4402 Item := First (Context_Items (Comp_Unit));
4403 while Present (Item) loop
4404 if Nkind (Item) = N_With_Clause
4405 and then Library_Unit (Item) = Library_Unit (W)
4406 then
4407 return True;
4408 end if;
4410 Next (Item);
4411 end loop;
4413 return False;
4414 end Previous_Withed_Unit;
4416 -- Start of processing for Expand_Limited_With_Clause
4418 begin
4419 if Nkind (Nam) = N_Identifier then
4421 -- Create node for name of withed unit
4423 Withn :=
4424 Make_With_Clause (Loc,
4425 Name => New_Copy (Nam));
4427 else pragma Assert (Nkind (Nam) = N_Selected_Component);
4428 Withn :=
4429 Make_With_Clause (Loc,
4430 Name => Make_Selected_Component (Loc,
4431 Prefix => New_Copy_Tree (Prefix (Nam)),
4432 Selector_Name => New_Copy (Selector_Name (Nam))));
4433 Set_Parent (Withn, Parent (N));
4434 end if;
4436 Set_First_Name (Withn);
4437 Set_Implicit_With (Withn);
4438 Set_Limited_Present (Withn);
4440 Unum :=
4441 Load_Unit
4442 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
4443 Required => True,
4444 Subunit => False,
4445 Error_Node => Nam);
4447 -- Do not generate a limited_with_clause on the current unit. This
4448 -- path is taken when a unit has a limited_with clause on one of its
4449 -- child units.
4451 if Unum = Current_Sem_Unit then
4452 return;
4453 end if;
4455 Set_Library_Unit (Withn, Cunit (Unum));
4456 Set_Corresponding_Spec
4457 (Withn, Specification (Unit (Cunit (Unum))));
4459 if not Previous_Withed_Unit (Withn) then
4460 Prepend (Withn, Context_Items (Parent (N)));
4461 Mark_Rewrite_Insertion (Withn);
4463 -- Add implicit limited_with_clauses for parents of child units
4464 -- mentioned in limited_with clauses.
4466 if Nkind (Nam) = N_Selected_Component then
4467 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
4468 end if;
4470 Analyze (Withn);
4472 if not Limited_View_Installed (Withn) then
4473 Install_Limited_With_Clause (Withn);
4474 end if;
4475 end if;
4476 end Expand_Limited_With_Clause;
4478 -- Start of processing for Install_Limited_Context_Clauses
4480 begin
4481 Item := First (Context_Items (N));
4482 while Present (Item) loop
4483 if Nkind (Item) = N_With_Clause
4484 and then Limited_Present (Item)
4485 and then not Error_Posted (Item)
4486 then
4487 if Nkind (Name (Item)) = N_Selected_Component then
4488 Expand_Limited_With_Clause
4489 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
4490 end if;
4492 Check_Private_Limited_Withed_Unit (Item);
4494 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
4495 Check_Renamings (Parent_Spec (Unit (N)), Item);
4496 end if;
4498 -- A unit may have a limited with on itself if it has a limited
4499 -- with_clause on one of its child units. In that case it is
4500 -- already being compiled and it makes no sense to install its
4501 -- limited view.
4503 -- If the item is a limited_private_with_clause, install it if the
4504 -- current unit is a body or if it is a private child. Otherwise
4505 -- the private clause is installed before analyzing the private
4506 -- part of the current unit.
4508 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
4509 and then not Limited_View_Installed (Item)
4510 and then
4511 not Is_Ancestor_Unit
4512 (Library_Unit (Item), Cunit (Current_Sem_Unit))
4513 then
4514 if not Private_Present (Item)
4515 or else Private_Present (N)
4516 or else Nkind (Unit (N)) in
4517 N_Package_Body | N_Subprogram_Body | N_Subunit
4518 then
4519 Install_Limited_With_Clause (Item);
4520 end if;
4521 end if;
4522 end if;
4524 Next (Item);
4525 end loop;
4527 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4528 -- looking for incomplete subtype declarations of incomplete types
4529 -- visible through a limited with clause.
4531 if Ada_Version >= Ada_2005
4532 and then Analyzed (N)
4533 and then Nkind (Unit (N)) = N_Package_Declaration
4534 then
4535 declare
4536 Decl : Node_Id;
4537 Def_Id : Entity_Id;
4538 Non_Lim_View : Entity_Id;
4540 begin
4541 Decl := First (Visible_Declarations (Specification (Unit (N))));
4542 while Present (Decl) loop
4543 if Nkind (Decl) = N_Subtype_Declaration
4544 and then
4545 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
4546 and then
4547 From_Limited_With (Defining_Identifier (Decl))
4548 then
4549 Def_Id := Defining_Identifier (Decl);
4550 Non_Lim_View := Non_Limited_View (Def_Id);
4552 if not Is_Incomplete_Type (Non_Lim_View) then
4554 -- Convert an incomplete subtype declaration into a
4555 -- corresponding nonlimited view subtype declaration.
4556 -- This is usually the case when analyzing a body that
4557 -- has regular with clauses, when the spec has limited
4558 -- ones.
4560 -- If the nonlimited view is still incomplete, it is
4561 -- the dummy entry already created, and the declaration
4562 -- cannot be reanalyzed. This is the case when installing
4563 -- a parent unit that has limited with-clauses.
4565 Set_Subtype_Indication (Decl,
4566 New_Occurrence_Of (Non_Lim_View, Sloc (Def_Id)));
4567 Set_Etype (Def_Id, Non_Lim_View);
4568 Reinit_Field_To_Zero (Def_Id, F_Non_Limited_View,
4569 Old_Ekind => (E_Incomplete_Subtype => True,
4570 others => False));
4571 Reinit_Field_To_Zero (Def_Id, F_Private_Dependents);
4572 Mutate_Ekind
4573 (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
4574 Set_Analyzed (Decl, False);
4576 -- Reanalyze the declaration, suppressing the call to
4577 -- Enter_Name to avoid duplicate names.
4579 Analyze_Subtype_Declaration
4580 (N => Decl,
4581 Skip => True);
4582 end if;
4583 end if;
4585 Next (Decl);
4586 end loop;
4587 end;
4588 end if;
4589 end Install_Limited_Context_Clauses;
4591 ---------------------
4592 -- Install_Parents --
4593 ---------------------
4595 procedure Install_Parents
4596 (Lib_Unit : Node_Id;
4597 Is_Private : Boolean;
4598 Chain : Boolean := True)
4600 P : Node_Id;
4601 E_Name : Entity_Id;
4602 P_Name : Entity_Id;
4603 P_Spec : Node_Id;
4605 begin
4606 P := Unit (Parent_Spec (Lib_Unit));
4607 P_Name := Get_Parent_Entity (P);
4609 if Etype (P_Name) = Any_Type then
4610 return;
4611 end if;
4613 if Ekind (P_Name) = E_Generic_Package
4614 and then Nkind (Lib_Unit) not in N_Generic_Declaration
4615 | N_Generic_Renaming_Declaration
4616 then
4617 Error_Msg_N
4618 ("child of a generic package must be a generic unit", Lib_Unit);
4620 elsif not Is_Package_Or_Generic_Package (P_Name) then
4621 Error_Msg_N
4622 ("parent unit must be package or generic package", Lib_Unit);
4623 raise Unrecoverable_Error;
4625 elsif Present (Renamed_Entity (P_Name)) then
4626 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
4627 raise Unrecoverable_Error;
4629 -- Verify that a child of an instance is itself an instance, or the
4630 -- renaming of one. Given that an instance that is a unit is replaced
4631 -- with a package declaration, check against the original node. The
4632 -- parent may be currently being instantiated, in which case it appears
4633 -- as a declaration, but the generic_parent is already established
4634 -- indicating that we deal with an instance.
4636 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
4637 if Nkind (Lib_Unit) in N_Renaming_Declaration
4638 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
4639 or else
4640 (Nkind (Lib_Unit) = N_Package_Declaration
4641 and then Present (Generic_Parent (Specification (Lib_Unit))))
4642 then
4643 null;
4644 else
4645 Error_Msg_N
4646 ("child of an instance must be an instance or renaming",
4647 Lib_Unit);
4648 end if;
4649 end if;
4651 -- This is the recursive call that ensures all parents are loaded
4653 if Is_Child_Spec (P) then
4654 Install_Parents
4655 (Lib_Unit => P,
4656 Is_Private =>
4657 Is_Private or else Private_Present (Parent (Lib_Unit)),
4658 Chain => Chain);
4659 end if;
4661 -- Now we can install the context for this parent
4663 Install_Context_Clauses (Parent_Spec (Lib_Unit), Chain);
4664 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4665 Install_Siblings (P_Name, Parent (Lib_Unit));
4667 -- The child unit is in the declarative region of the parent. The parent
4668 -- must therefore appear in the scope stack and be visible, as when
4669 -- compiling the corresponding body. If the child unit is private or it
4670 -- is a package body, private declarations must be accessible as well.
4671 -- Use declarations in the parent must also be installed. Finally, other
4672 -- child units of the same parent that are in the context are
4673 -- immediately visible.
4675 -- Find entity for compilation unit, and set its private descendant
4676 -- status as needed. Indicate that it is a compilation unit, which is
4677 -- redundant in general, but needed if this is a generated child spec
4678 -- for a child body without previous spec.
4680 E_Name := Defining_Entity (Lib_Unit);
4682 Set_Is_Child_Unit (E_Name);
4683 Set_Is_Compilation_Unit (E_Name);
4685 Set_Is_Private_Descendant (E_Name,
4686 Is_Private_Descendant (P_Name)
4687 or else Private_Present (Parent (Lib_Unit)));
4689 P_Spec := Package_Specification (P_Name);
4690 Push_Scope (P_Name);
4692 -- Save current visibility of unit
4694 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4695 Is_Immediately_Visible (P_Name);
4696 Set_Is_Immediately_Visible (P_Name);
4697 Install_Visible_Declarations (P_Name);
4698 Set_Use (Visible_Declarations (P_Spec));
4700 -- If the parent is a generic unit, its formal part may contain formal
4701 -- packages and use clauses for them.
4703 if Ekind (P_Name) = E_Generic_Package then
4704 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4705 end if;
4707 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4708 Install_Private_Declarations (P_Name);
4709 Install_Private_With_Clauses (P_Name);
4710 Set_Use (Private_Declarations (P_Spec));
4711 end if;
4712 end Install_Parents;
4714 ----------------------------------
4715 -- Install_Private_With_Clauses --
4716 ----------------------------------
4718 procedure Install_Private_With_Clauses (P : Entity_Id) is
4719 Decl : constant Node_Id := Unit_Declaration_Node (P);
4720 Item : Node_Id;
4722 begin
4723 if Debug_Flag_I then
4724 Write_Str ("install private with clauses of ");
4725 Write_Name (Chars (P));
4726 Write_Eol;
4727 end if;
4729 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4730 Item := First (Context_Items (Parent (Decl)));
4731 while Present (Item) loop
4732 -- If Item is a private with clause, install it, but do not
4733 -- install implicit private with's that come from (for example)
4734 -- with's on instantiated generics. DO install implicit private
4735 -- with's that come from parents, which is necessary in general,
4736 -- but ???not quite right if the former (generic) case also
4737 -- applies.
4739 if Nkind (Item) = N_With_Clause
4740 and then Private_Present (Item)
4741 and then (not Implicit_With (Item) or else Parent_With (Item))
4742 then
4743 -- If the unit is an ancestor of the current one, it is the
4744 -- case of a private limited with clause on a child unit, and
4745 -- the compilation of one of its descendants, in that case the
4746 -- limited view is irrelevant.
4748 if Limited_Present (Item) then
4749 if not Limited_View_Installed (Item)
4750 and then
4751 not Is_Ancestor_Unit (Library_Unit (Item),
4752 Cunit (Current_Sem_Unit))
4753 then
4754 Install_Limited_With_Clause (Item);
4755 end if;
4756 else
4757 Install_With_Clause (Item, Private_With_OK => True);
4758 end if;
4759 end if;
4761 Next (Item);
4762 end loop;
4763 end if;
4764 end Install_Private_With_Clauses;
4766 ----------------------
4767 -- Install_Siblings --
4768 ----------------------
4770 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4771 Item : Node_Id;
4772 Id : Entity_Id;
4773 Prev : Entity_Id;
4775 begin
4776 -- Iterate over explicit with clauses, and check whether the scope of
4777 -- each entity is an ancestor of the current unit, in which case it is
4778 -- immediately visible.
4780 Item := First (Context_Items (N));
4781 while Present (Item) loop
4783 -- Do not install private_with_clauses declaration, unless unit
4784 -- is itself a private child unit, or is a body. Note that for a
4785 -- subprogram body the private_with_clause does not take effect
4786 -- until after the specification.
4788 if Nkind (Item) /= N_With_Clause
4789 or else Implicit_With (Item)
4790 or else Limited_Present (Item)
4791 or else Error_Posted (Item)
4793 -- Skip processing malformed trees
4795 or else (Try_Semantics
4796 and then Nkind (Name (Item)) not in N_Has_Entity)
4797 then
4798 null;
4800 elsif not Private_Present (Item)
4801 or else Private_Present (N)
4802 or else Nkind (Unit (N)) = N_Package_Body
4803 then
4804 Id := Entity (Name (Item));
4806 if Is_Child_Unit (Id)
4807 and then Is_Ancestor_Package (Scope (Id), U_Name)
4808 then
4809 Set_Is_Immediately_Visible (Id);
4811 -- Check for the presence of another unit in the context that
4812 -- may be inadvertently hidden by the child.
4814 Prev := Current_Entity (Id);
4816 if Present (Prev)
4817 and then Is_Immediately_Visible (Prev)
4818 and then not Is_Child_Unit (Prev)
4819 then
4820 declare
4821 Clause : Node_Id;
4823 begin
4824 Clause := First (Context_Items (N));
4825 while Present (Clause) loop
4826 if Nkind (Clause) = N_With_Clause
4827 and then Entity (Name (Clause)) = Prev
4828 then
4829 Error_Msg_NE
4830 ("child unit& hides compilation unit " &
4831 "with the same name??",
4832 Name (Item), Id);
4833 exit;
4834 end if;
4836 Next (Clause);
4837 end loop;
4838 end;
4839 end if;
4841 -- The With_Clause may be on a grandchild or one of its further
4842 -- descendants, which makes a child immediately visible. Examine
4843 -- ancestry to determine whether such a child exists. For example,
4844 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4845 -- is immediately visible.
4847 elsif Is_Child_Unit (Id) then
4848 declare
4849 Par : Entity_Id;
4851 begin
4852 Par := Scope (Id);
4853 while Is_Child_Unit (Par) loop
4854 if Is_Ancestor_Package (Scope (Par), U_Name) then
4855 Set_Is_Immediately_Visible (Par);
4856 exit;
4857 end if;
4859 Par := Scope (Par);
4860 end loop;
4861 end;
4862 end if;
4864 -- If the item is a private with-clause on a child unit, the parent
4865 -- may have been installed already, but the child unit must remain
4866 -- invisible until installed in a private part or body, unless there
4867 -- is already a regular with_clause for it in the current unit.
4869 elsif Private_Present (Item) then
4870 Id := Entity (Name (Item));
4872 if Is_Child_Unit (Id) then
4873 declare
4874 Clause : Node_Id;
4876 function In_Context return Boolean;
4877 -- Scan context of current unit, to check whether there is
4878 -- a with_clause on the same unit as a private with-clause
4879 -- on a parent, in which case child unit is visible. If the
4880 -- unit is a grandchild, the same applies to its parent.
4882 ----------------
4883 -- In_Context --
4884 ----------------
4886 function In_Context return Boolean is
4887 begin
4888 Clause :=
4889 First (Context_Items (Cunit (Current_Sem_Unit)));
4890 while Present (Clause) loop
4891 if Nkind (Clause) = N_With_Clause
4892 and then Comes_From_Source (Clause)
4893 and then Is_Entity_Name (Name (Clause))
4894 and then not Private_Present (Clause)
4895 then
4896 if Entity (Name (Clause)) = Id
4897 or else
4898 (Nkind (Name (Clause)) = N_Expanded_Name
4899 and then Entity (Prefix (Name (Clause))) = Id)
4900 then
4901 return True;
4902 end if;
4903 end if;
4905 Next (Clause);
4906 end loop;
4908 return False;
4909 end In_Context;
4911 begin
4912 Set_Is_Visible_Lib_Unit (Id, In_Context);
4913 end;
4914 end if;
4915 end if;
4917 Next (Item);
4918 end loop;
4919 end Install_Siblings;
4921 ---------------------------------
4922 -- Install_Limited_With_Clause --
4923 ---------------------------------
4925 procedure Install_Limited_With_Clause (N : Node_Id) is
4926 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4927 E : Entity_Id;
4928 P : Entity_Id;
4929 Is_Child_Package : Boolean := False;
4930 Lim_Header : Entity_Id;
4931 Lim_Typ : Entity_Id;
4933 procedure Check_Body_Required;
4934 -- A unit mentioned in a limited with_clause may not be mentioned in
4935 -- a regular with_clause, but must still be included in the current
4936 -- partition. We need to determine whether the unit needs a body, so
4937 -- that the binder can determine the name of the file to be compiled.
4938 -- Checking whether a unit needs a body can be done without semantic
4939 -- analysis, by examining the nature of the declarations in the package.
4941 function Has_Limited_With_Clause
4942 (C_Unit : Entity_Id;
4943 Pack : Entity_Id) return Boolean;
4944 -- Determine whether any package in the ancestor chain starting with
4945 -- C_Unit has a limited with clause for package Pack.
4947 -------------------------
4948 -- Check_Body_Required --
4949 -------------------------
4951 procedure Check_Body_Required is
4952 PA : constant List_Id :=
4953 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4955 procedure Check_Declarations (Spec : Node_Id);
4956 -- Recursive procedure that does the work and checks nested packages
4958 ------------------------
4959 -- Check_Declarations --
4960 ------------------------
4962 procedure Check_Declarations (Spec : Node_Id) is
4963 Decl : Node_Id;
4964 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4966 Subp_List : constant Elist_Id := New_Elmt_List;
4968 procedure Check_Pragma_Import (P : Node_Id);
4969 -- If a pragma import applies to a previous subprogram, the
4970 -- enclosing unit may not need a body. The processing is syntactic
4971 -- and does not require a declaration to be analyzed. The code
4972 -- below also handles pragma Import when applied to a subprogram
4973 -- that renames another. In this case the pragma applies to the
4974 -- renamed entity.
4976 -- Chains of multiple renames are not handled by the code below.
4977 -- It is probably impossible to handle all cases without proper
4978 -- name resolution. In such cases the algorithm is conservative
4979 -- and will indicate that a body is needed???
4981 -------------------------
4982 -- Check_Pragma_Import --
4983 -------------------------
4985 procedure Check_Pragma_Import (P : Node_Id) is
4986 Arg : Node_Id;
4987 Prev_Id : Elmt_Id;
4988 Subp_Id : Elmt_Id;
4989 Imported : Node_Id;
4991 procedure Remove_Homonyms (E : Node_Id);
4992 -- Make one pass over list of subprograms. Called again if
4993 -- subprogram is a renaming. E is known to be an identifier.
4995 ---------------------
4996 -- Remove_Homonyms --
4997 ---------------------
4999 procedure Remove_Homonyms (E : Node_Id) is
5000 R : Entity_Id := Empty;
5001 -- Name of renamed entity, if any
5003 begin
5004 Subp_Id := First_Elmt (Subp_List);
5005 while Present (Subp_Id) loop
5006 if Chars (Node (Subp_Id)) = Chars (E) then
5007 if Nkind (Parent (Parent (Node (Subp_Id))))
5008 /= N_Subprogram_Renaming_Declaration
5009 then
5010 Prev_Id := Subp_Id;
5011 Next_Elmt (Subp_Id);
5012 Remove_Elmt (Subp_List, Prev_Id);
5013 else
5014 R := Name (Parent (Parent (Node (Subp_Id))));
5015 exit;
5016 end if;
5017 else
5018 Next_Elmt (Subp_Id);
5019 end if;
5020 end loop;
5022 if Present (R) then
5023 if Nkind (R) = N_Identifier then
5024 Remove_Homonyms (R);
5026 elsif Nkind (R) = N_Selected_Component then
5027 Remove_Homonyms (Selector_Name (R));
5029 -- Renaming of attribute
5031 else
5032 null;
5033 end if;
5034 end if;
5035 end Remove_Homonyms;
5037 -- Start of processing for Check_Pragma_Import
5039 begin
5040 -- Find name of entity in Import pragma. We have not analyzed
5041 -- the construct, so we must guard against syntax errors.
5043 Arg := Next (First (Pragma_Argument_Associations (P)));
5045 if No (Arg)
5046 or else Nkind (Expression (Arg)) /= N_Identifier
5047 then
5048 return;
5049 else
5050 Imported := Expression (Arg);
5051 end if;
5053 Remove_Homonyms (Imported);
5054 end Check_Pragma_Import;
5056 -- Start of processing for Check_Declarations
5058 begin
5059 -- Search for Elaborate Body pragma
5061 Decl := First (Visible_Declarations (Spec));
5062 while Present (Decl)
5063 and then Nkind (Decl) = N_Pragma
5064 loop
5065 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
5066 Set_Body_Required (Library_Unit (N));
5067 return;
5068 end if;
5070 Next (Decl);
5071 end loop;
5073 -- Look for declarations that require the presence of a body. We
5074 -- have already skipped pragmas at the start of the list.
5076 while Present (Decl) loop
5078 -- Subprogram that comes from source means body may be needed.
5079 -- Save for subsequent examination of import pragmas.
5081 if Comes_From_Source (Decl)
5082 and then Nkind (Decl) in N_Subprogram_Declaration
5083 | N_Subprogram_Renaming_Declaration
5084 | N_Generic_Subprogram_Declaration
5085 then
5086 Append_Elmt (Defining_Entity (Decl), Subp_List);
5088 -- Package declaration of generic package declaration. We need
5089 -- to recursively examine nested declarations.
5091 elsif Nkind (Decl) in N_Package_Declaration
5092 | N_Generic_Package_Declaration
5093 then
5094 Check_Declarations (Specification (Decl));
5096 elsif Nkind (Decl) = N_Pragma
5097 and then Pragma_Name (Decl) = Name_Import
5098 then
5099 Check_Pragma_Import (Decl);
5100 end if;
5102 Next (Decl);
5103 end loop;
5105 -- Same set of tests for private part. In addition to subprograms
5106 -- detect the presence of Taft Amendment types (incomplete types
5107 -- completed in the body).
5109 Decl := First (Private_Declarations (Spec));
5110 while Present (Decl) loop
5111 if Comes_From_Source (Decl)
5112 and then Nkind (Decl) in N_Subprogram_Declaration
5113 | N_Subprogram_Renaming_Declaration
5114 | N_Generic_Subprogram_Declaration
5115 then
5116 Append_Elmt (Defining_Entity (Decl), Subp_List);
5118 elsif Nkind (Decl) in N_Package_Declaration
5119 | N_Generic_Package_Declaration
5120 then
5121 Check_Declarations (Specification (Decl));
5123 -- Collect incomplete type declarations for separate pass
5125 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
5126 Append_Elmt (Decl, Incomplete_Decls);
5128 elsif Nkind (Decl) = N_Pragma
5129 and then Pragma_Name (Decl) = Name_Import
5130 then
5131 Check_Pragma_Import (Decl);
5132 end if;
5134 Next (Decl);
5135 end loop;
5137 -- Now check incomplete declarations to locate Taft amendment
5138 -- types. This can be done by examining the defining identifiers
5139 -- of type declarations without real semantic analysis.
5141 declare
5142 Inc : Elmt_Id;
5144 begin
5145 Inc := First_Elmt (Incomplete_Decls);
5146 while Present (Inc) loop
5147 Decl := Next (Node (Inc));
5148 while Present (Decl) loop
5149 if Nkind (Decl) = N_Full_Type_Declaration
5150 and then Chars (Defining_Identifier (Decl)) =
5151 Chars (Defining_Identifier (Node (Inc)))
5152 then
5153 exit;
5154 end if;
5156 Next (Decl);
5157 end loop;
5159 -- If no completion, this is a TAT, and a body is needed
5161 if No (Decl) then
5162 Set_Body_Required (Library_Unit (N));
5163 return;
5164 end if;
5166 Next_Elmt (Inc);
5167 end loop;
5168 end;
5170 -- Finally, check whether there are subprograms that still require
5171 -- a body, i.e. are not renamings or null.
5173 if not Is_Empty_Elmt_List (Subp_List) then
5174 declare
5175 Subp_Id : Elmt_Id;
5176 Spec : Node_Id;
5178 begin
5179 Subp_Id := First_Elmt (Subp_List);
5180 Spec := Parent (Node (Subp_Id));
5182 while Present (Subp_Id) loop
5183 if Nkind (Parent (Spec))
5184 = N_Subprogram_Renaming_Declaration
5185 then
5186 null;
5188 elsif Nkind (Spec) = N_Procedure_Specification
5189 and then Null_Present (Spec)
5190 then
5191 null;
5193 else
5194 Set_Body_Required (Library_Unit (N));
5195 return;
5196 end if;
5198 Next_Elmt (Subp_Id);
5199 end loop;
5200 end;
5201 end if;
5202 end Check_Declarations;
5204 -- Start of processing for Check_Body_Required
5206 begin
5207 -- If this is an imported package (Java and CIL usage) no body is
5208 -- needed. Scan list of pragmas that may follow a compilation unit
5209 -- to look for a relevant pragma Import.
5211 if Present (PA) then
5212 declare
5213 Prag : Node_Id;
5215 begin
5216 Prag := First (PA);
5217 while Present (Prag) loop
5218 if Nkind (Prag) = N_Pragma
5219 and then Get_Pragma_Id (Prag) = Pragma_Import
5220 then
5221 return;
5222 end if;
5224 Next (Prag);
5225 end loop;
5226 end;
5227 end if;
5229 Check_Declarations (Specification (P_Unit));
5230 end Check_Body_Required;
5232 -----------------------------
5233 -- Has_Limited_With_Clause --
5234 -----------------------------
5236 function Has_Limited_With_Clause
5237 (C_Unit : Entity_Id;
5238 Pack : Entity_Id) return Boolean
5240 Par : Entity_Id;
5241 Par_Unit : Node_Id;
5243 begin
5244 Par := C_Unit;
5245 while Present (Par) loop
5246 if Ekind (Par) /= E_Package then
5247 exit;
5248 end if;
5250 -- Retrieve the Compilation_Unit node for Par and determine if
5251 -- its context clauses contain a limited with for Pack.
5253 Par_Unit := Parent (Parent (Parent (Par)));
5255 if Nkind (Par_Unit) = N_Package_Declaration then
5256 Par_Unit := Parent (Par_Unit);
5257 end if;
5259 if Has_With_Clause (Par_Unit, Pack, True) then
5260 return True;
5261 end if;
5263 -- If there are more ancestors, climb up the tree, otherwise we
5264 -- are done.
5266 if Is_Child_Unit (Par) then
5267 Par := Scope (Par);
5268 else
5269 exit;
5270 end if;
5271 end loop;
5273 return False;
5274 end Has_Limited_With_Clause;
5276 -- Start of processing for Install_Limited_With_Clause
5278 begin
5279 pragma Assert (not Limited_View_Installed (N));
5281 -- In case of limited with_clause on subprograms, generics, instances,
5282 -- or renamings, the corresponding error was previously posted and we
5283 -- have nothing to do here. If the file is missing altogether, it has
5284 -- no source location.
5286 if Nkind (P_Unit) /= N_Package_Declaration
5287 or else Sloc (P_Unit) = No_Location
5288 then
5289 return;
5290 end if;
5292 P := Defining_Unit_Name (Specification (P_Unit));
5294 -- Handle child packages
5296 if Nkind (P) = N_Defining_Program_Unit_Name then
5297 Is_Child_Package := True;
5298 P := Defining_Identifier (P);
5299 end if;
5301 -- Do not install the limited-view if the context of the unit is already
5302 -- available through a regular with clause.
5304 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
5305 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
5306 then
5307 return;
5308 end if;
5310 -- Do not install the limited-view if the full-view is already visible
5311 -- through renaming declarations.
5313 if Is_Visible_Through_Renamings (P, N) then
5314 return;
5315 end if;
5317 -- Do not install the limited view if this is the unit being analyzed.
5318 -- This unusual case will happen when a unit has a limited_with clause
5319 -- on one of its children. The compilation of the child forces the load
5320 -- of the parent which tries to install the limited view of the child
5321 -- again. Installing the limited view must also be disabled when
5322 -- compiling the body of the child unit.
5324 if P = Cunit_Entity (Current_Sem_Unit)
5325 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
5326 and then P = Main_Unit_Entity
5327 and then Is_Ancestor_Unit
5328 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
5329 then
5330 return;
5331 end if;
5333 -- This scenario is similar to the one above, the difference is that the
5334 -- compilation of sibling Par.Sib forces the load of parent Par which
5335 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
5336 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
5337 -- nonlimited views of all entities from Lim_Pack.
5339 -- limited with Lim_Pack; -- [1]
5340 -- package Par is ... package Lim_Pack is ...
5342 -- with Lim_Pack; -- [2]
5343 -- package Par.Sib is ... package body Par.Sib is ...
5345 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
5346 -- Sem_Unit is the body of Par.Sib.
5348 if Ekind (P) = E_Package
5349 and then Ekind (Main_Unit_Entity) = E_Package
5350 and then Is_Child_Unit (Main_Unit_Entity)
5352 -- The body has a regular with clause
5354 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
5355 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
5357 -- One of the ancestors has a limited with clause
5359 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
5360 N_Package_Specification
5361 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
5362 then
5363 return;
5364 end if;
5366 -- A common use of the limited-with is to have a limited-with in the
5367 -- package spec, and a normal with in its package body. For example:
5369 -- limited with X; -- [1]
5370 -- package A is ...
5372 -- with X; -- [2]
5373 -- package body A is ...
5375 -- The compilation of A's body installs the context clauses found at [2]
5376 -- and then the context clauses of its specification (found at [1]). As
5377 -- a consequence, at [1] the specification of X has been analyzed and it
5378 -- is immediately visible. According to the semantics of limited-with
5379 -- context clauses we don't install the limited view because the full
5380 -- view of X supersedes its limited view.
5382 if Analyzed (P_Unit)
5383 and then
5384 (Is_Immediately_Visible (P)
5385 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
5386 then
5388 -- The presence of both the limited and the analyzed nonlimited view
5389 -- may also be an error, such as an illegal context for a limited
5390 -- with_clause. In that case, do not process the context item at all.
5392 if Error_Posted (N) then
5393 return;
5394 end if;
5396 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
5397 declare
5398 Item : Node_Id;
5399 begin
5400 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
5401 while Present (Item) loop
5402 if Nkind (Item) = N_With_Clause
5403 and then Comes_From_Source (Item)
5404 and then Entity (Name (Item)) = P
5405 then
5406 return;
5407 end if;
5409 Next (Item);
5410 end loop;
5411 end;
5413 -- If this is a child body, assume that the nonlimited with_clause
5414 -- appears in an ancestor. Could be refined ???
5416 if Is_Child_Unit
5417 (Defining_Entity
5418 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
5419 then
5420 return;
5421 end if;
5423 else
5425 -- If in package declaration, nonlimited view brought in from
5426 -- parent unit or some error condition.
5428 return;
5429 end if;
5430 end if;
5432 if Debug_Flag_I then
5433 Write_Str ("install limited view of ");
5434 Write_Name (Chars (P));
5435 Write_Eol;
5436 end if;
5438 -- If the unit has not been analyzed and the limited view has not been
5439 -- already installed then we install it.
5441 if not Analyzed (P_Unit) then
5442 if not In_Chain (P) then
5444 -- Minimum decoration
5446 Mutate_Ekind (P, E_Package);
5447 Set_Etype (P, Standard_Void_Type);
5448 Set_Scope (P, Standard_Standard);
5449 Set_Is_Visible_Lib_Unit (P);
5451 if Is_Child_Package then
5452 Set_Is_Child_Unit (P);
5453 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
5454 end if;
5456 -- Place entity on visibility structure
5458 Set_Homonym (P, Current_Entity (P));
5459 Set_Current_Entity (P);
5461 if Debug_Flag_I then
5462 Write_Str (" (homonym) chain ");
5463 Write_Name (Chars (P));
5464 Write_Eol;
5465 end if;
5467 -- Install the incomplete view. The first element of the limited
5468 -- view is a header (an E_Package entity) used to reference the
5469 -- first shadow entity in the private part of the package.
5471 Lim_Header := Limited_View (P);
5472 Lim_Typ := First_Entity (Lim_Header);
5474 while Present (Lim_Typ)
5475 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5476 loop
5477 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
5478 Set_Current_Entity (Lim_Typ);
5480 if Debug_Flag_I then
5481 Write_Str (" (homonym) chain ");
5482 Write_Name (Chars (Lim_Typ));
5483 Write_Eol;
5484 end if;
5486 Next_Entity (Lim_Typ);
5487 end loop;
5488 end if;
5490 -- If the unit appears in a previous regular with_clause, the regular
5491 -- entities of the public part of the withed package must be replaced
5492 -- by the shadow ones.
5494 -- This code must be kept synchronized with the code that replaces the
5495 -- shadow entities by the real entities in Remove_Limited_With_Unit,
5496 -- otherwise the contents of the homonym chains are not consistent.
5498 else
5499 -- Hide all the type entities of the public part of the package to
5500 -- avoid its usage. This is needed to cover all the subtype decla-
5501 -- rations because we do not remove them from the homonym chain.
5503 E := First_Entity (P);
5504 while Present (E) and then E /= First_Private_Entity (P) loop
5505 if Is_Type (E) then
5506 Set_Was_Hidden (E, Is_Hidden (E));
5507 Set_Is_Hidden (E);
5508 end if;
5510 Next_Entity (E);
5511 end loop;
5513 -- Replace the real entities by the shadow entities of the limited
5514 -- view. The first element of the limited view is a header that is
5515 -- used to reference the first shadow entity in the private part
5516 -- of the package. Successive elements are the limited views of the
5517 -- type (including regular incomplete types) declared in the package.
5519 Lim_Header := Limited_View (P);
5521 Lim_Typ := First_Entity (Lim_Header);
5522 while Present (Lim_Typ)
5523 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5524 loop
5525 pragma Assert (not In_Chain (Lim_Typ));
5527 -- Do not unchain nested packages and child units
5529 if Ekind (Lim_Typ) /= E_Package
5530 and then not Is_Child_Unit (Lim_Typ)
5531 then
5532 declare
5533 Typ : constant Entity_Id := Non_Limited_View (Lim_Typ);
5535 Prev : Entity_Id;
5537 begin
5538 -- Replace Typ by Lim_Typ in the homonyms list, so that the
5539 -- limited view becomes available.
5541 -- If the nonlimited view is a record with an anonymous
5542 -- self-referential component, the analysis of the record
5543 -- declaration creates an incomplete type with the same name
5544 -- in order to define an internal access type. The visible
5545 -- entity is now the incomplete type, and that is the one to
5546 -- replace in the visibility structure.
5548 -- Similarly, if the source already contains the incomplete
5549 -- type declaration, the limited view of the incomplete type
5550 -- is in fact never visible (AI05-129) but we have created a
5551 -- shadow entity E1 for it that points to E2, the incomplete
5552 -- type at stake. This in turn has full view E3 that is the
5553 -- full declaration, with a corresponding shadow entity E4.
5554 -- When reinstalling the limited view, the visible entity E2
5555 -- is first replaced with E1, but E4 must eventually become
5556 -- the visible entity as per the AI and thus displace E1, as
5557 -- it is replacing E3 in the homonyms list.
5559 -- regular views limited views
5561 -- * E2 (incomplete) <-- E1 (shadow)
5563 -- |
5564 -- V
5566 -- E3 (full) <-- E4 (shadow) *
5568 -- [*] denotes the visible entity (Current_Entity)
5570 Prev := Current_Entity (Lim_Typ);
5572 while Present (Prev) loop
5573 -- This is a regular replacement
5575 if Prev = Typ
5576 or else (Ekind (Prev) = E_Incomplete_Type
5577 and then Full_View (Prev) = Typ)
5578 then
5579 Replace (Prev, Lim_Typ);
5581 if Debug_Flag_I then
5582 Write_Str (" (homonym) replace ");
5583 Write_Name (Chars (Typ));
5584 Write_Eol;
5585 end if;
5587 exit;
5589 -- This is where E1 is replaced with E4
5591 elsif Ekind (Prev) = E_Incomplete_Type
5592 and then From_Limited_With (Prev)
5593 and then
5594 Ekind (Non_Limited_View (Prev)) = E_Incomplete_Type
5595 and then Full_View (Non_Limited_View (Prev)) = Typ
5596 then
5597 Replace (Prev, Lim_Typ);
5599 if Debug_Flag_I then
5600 Write_Str (" (homonym) E1 -> E4 ");
5601 Write_Name (Chars (Typ));
5602 Write_Eol;
5603 end if;
5605 exit;
5606 end if;
5608 Prev := Homonym (Prev);
5609 end loop;
5610 end;
5611 end if;
5613 Next_Entity (Lim_Typ);
5614 end loop;
5615 end if;
5617 -- The package must be visible while the limited-with clause is active
5618 -- because references to the type P.T must resolve in the usual way.
5619 -- In addition, we remember that the limited-view has been installed to
5620 -- uninstall it at the point of context removal.
5622 Set_Is_Immediately_Visible (P);
5623 Set_Limited_View_Installed (N);
5625 -- If unit has not been analyzed in some previous context, check
5626 -- (imperfectly ???) whether it might need a body.
5628 if not Analyzed (P_Unit) then
5629 Check_Body_Required;
5630 end if;
5632 -- If the package in the limited_with clause is a child unit, the clause
5633 -- is unanalyzed and appears as a selected component. Recast it as an
5634 -- expanded name so that the entity can be properly set. Use entity of
5635 -- parent, if available, for higher ancestors in the name.
5637 if Nkind (Name (N)) = N_Selected_Component then
5638 declare
5639 Nam : Node_Id;
5640 Ent : Entity_Id;
5642 begin
5643 Nam := Name (N);
5644 Ent := P;
5645 while Nkind (Nam) = N_Selected_Component
5646 and then Present (Ent)
5647 loop
5648 Change_Selected_Component_To_Expanded_Name (Nam);
5650 -- Set entity of parent identifiers if the unit is a child
5651 -- unit. This ensures that the tree is properly formed from
5652 -- semantic point of view. The unit entities are not fully
5653 -- analyzed, so we need to follow unit links in the tree.
5655 Set_Entity (Nam, Ent);
5657 Nam := Prefix (Nam);
5658 Ent :=
5659 Defining_Entity
5660 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5662 -- Set entity of last ancestor
5664 if Nkind (Nam) = N_Identifier then
5665 Set_Entity (Nam, Ent);
5666 end if;
5667 end loop;
5668 end;
5669 end if;
5671 Set_Entity (Name (N), P);
5672 Set_From_Limited_With (P);
5673 end Install_Limited_With_Clause;
5675 -------------------------
5676 -- Install_With_Clause --
5677 -------------------------
5679 procedure Install_With_Clause
5680 (With_Clause : Node_Id;
5681 Private_With_OK : Boolean := False)
5683 Uname : constant Entity_Id := Entity (Name (With_Clause));
5684 P : constant Entity_Id := Scope (Uname);
5686 begin
5687 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5688 -- compiling a package declaration and the Private_With_OK flag was not
5689 -- set by the caller. These declarations will be installed later (before
5690 -- analyzing the private part of the package).
5692 if Private_Present (With_Clause)
5693 and then Nkind (Unit (Parent (With_Clause)))
5694 in N_Package_Declaration | N_Generic_Package_Declaration
5695 and then not Private_With_OK
5696 then
5697 return;
5698 end if;
5700 if Debug_Flag_I then
5701 if Private_Present (With_Clause) then
5702 Write_Str ("install private withed unit ");
5703 elsif Parent_With (With_Clause) then
5704 Write_Str ("install parent withed unit ");
5705 elsif Implicit_With (With_Clause) then
5706 Write_Str ("install implicit withed unit ");
5707 else
5708 Write_Str ("install withed unit ");
5709 end if;
5711 Write_Name (Chars (Uname));
5712 Write_Eol;
5713 end if;
5715 -- We do not apply the restrictions to an internal unit unless we are
5716 -- compiling the internal unit as a main unit. This check is also
5717 -- skipped for dummy units (for missing packages).
5719 if Sloc (Uname) /= No_Location
5720 and then (not Is_Internal_Unit (Current_Sem_Unit)
5721 or else Current_Sem_Unit = Main_Unit)
5722 then
5723 Check_Restricted_Unit
5724 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5725 end if;
5727 if P /= Standard_Standard then
5729 -- If the unit is not analyzed after analysis of the with clause and
5730 -- it is an instantiation then it awaits a body and is the main unit.
5731 -- Its appearance in the context of some other unit indicates a
5732 -- circular dependency (DEC suite perversity).
5734 if not Analyzed (Uname)
5735 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5736 then
5737 Error_Msg_N
5738 ("instantiation depends on itself", Name (With_Clause));
5740 elsif not Analyzed (Uname)
5741 and then Is_Internal_Unit (Current_Sem_Unit)
5742 and then not Is_Visible_Lib_Unit (Uname)
5743 and then No (Scope (Uname))
5744 then
5745 if Is_Predefined_Unit (Current_Sem_Unit) then
5746 Error_Msg_N
5747 ("predefined unit depends on itself", Name (With_Clause));
5748 else
5749 Error_Msg_N
5750 ("GNAT-defined unit depends on itself", Name (With_Clause));
5751 end if;
5752 return;
5754 elsif not Is_Visible_Lib_Unit (Uname) then
5756 -- Abandon processing in case of previous errors
5758 if No (Scope (Uname)) then
5759 Check_Error_Detected;
5760 return;
5761 end if;
5763 Set_Is_Visible_Lib_Unit (Uname);
5765 -- If the unit is a wrapper package for a compilation unit that is
5766 -- a subprogram instance, indicate that the instance itself is a
5767 -- visible unit. This is necessary if the instance is inlined.
5769 if Is_Wrapper_Package (Uname) then
5770 Set_Is_Visible_Lib_Unit (Related_Instance (Uname));
5771 end if;
5773 -- If the child unit appears in the context of its parent, it is
5774 -- immediately visible.
5776 if In_Open_Scopes (Scope (Uname)) then
5777 Set_Is_Immediately_Visible (Uname);
5778 end if;
5780 if Is_Generic_Instance (Uname)
5781 and then Is_Subprogram (Uname)
5782 then
5783 -- Set flag as well on the visible entity that denotes the
5784 -- instance, which renames the current one.
5786 Set_Is_Visible_Lib_Unit
5787 (Related_Instance
5788 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5789 end if;
5791 -- The parent unit may have been installed already, and may have
5792 -- appeared in a use clause.
5794 if In_Use (Scope (Uname)) then
5795 Set_Is_Potentially_Use_Visible (Uname);
5796 end if;
5798 Set_Context_Installed (With_Clause);
5799 end if;
5801 elsif not Is_Immediately_Visible (Uname) then
5802 Set_Is_Visible_Lib_Unit (Uname);
5804 if not Private_Present (With_Clause) or else Private_With_OK then
5805 Set_Is_Immediately_Visible (Uname);
5806 end if;
5808 Set_Context_Installed (With_Clause);
5809 end if;
5811 -- A [private] with clause overrides a limited with clause. Restore the
5812 -- proper view of the package by performing the following actions:
5814 -- * Remove all shadow entities which hide their corresponding
5815 -- entities from direct visibility by updating the entity and
5816 -- homonym chains.
5818 -- * Enter the corresponding entities back in direct visibility
5820 -- Note that the original limited with clause which installed its view
5821 -- is still marked as "active". This effect is undone when the clause
5822 -- itself is removed, see Remove_Limited_With_Clause.
5824 if Ekind (Uname) = E_Package and then From_Limited_With (Uname) then
5825 Remove_Limited_With_Unit (Unit_Declaration_Node (Uname));
5826 end if;
5828 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5829 -- unit if there is a visible homograph for it declared in the same
5830 -- declarative region. This pathological case can only arise when an
5831 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5832 -- G1 has a generic child also named G2, and the context includes with_
5833 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5834 -- of I1.G2 visible as well. If the child unit is named Standard, do
5835 -- not apply the check to the Standard package itself.
5837 if Is_Child_Unit (Uname)
5838 and then Is_Visible_Lib_Unit (Uname)
5839 and then Ada_Version >= Ada_2005
5840 then
5841 declare
5842 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5843 Decl2 : Node_Id;
5844 P2 : Entity_Id;
5845 U2 : Entity_Id;
5847 begin
5848 U2 := Homonym (Uname);
5849 while Present (U2) and then U2 /= Standard_Standard loop
5850 P2 := Scope (U2);
5851 Decl2 := Unit_Declaration_Node (P2);
5853 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5854 if Is_Generic_Instance (P)
5855 and then Nkind (Decl1) = N_Package_Declaration
5856 and then Generic_Parent (Specification (Decl1)) = P2
5857 then
5858 Error_Msg_N ("illegal with_clause", With_Clause);
5859 Error_Msg_N
5860 ("\child unit has visible homograph" &
5861 " (RM 8.3(26), 10.1.1(19))",
5862 With_Clause);
5863 exit;
5865 elsif Is_Generic_Instance (P2)
5866 and then Nkind (Decl2) = N_Package_Declaration
5867 and then Generic_Parent (Specification (Decl2)) = P
5868 then
5869 -- With_clause for child unit of instance appears before
5870 -- in the context. We want to place the error message on
5871 -- it, not on the generic child unit itself.
5873 declare
5874 Prev_Clause : Node_Id;
5876 begin
5877 Prev_Clause := First (List_Containing (With_Clause));
5878 while Entity (Name (Prev_Clause)) /= U2 loop
5879 Next (Prev_Clause);
5880 end loop;
5882 pragma Assert (Present (Prev_Clause));
5883 Error_Msg_N ("illegal with_clause", Prev_Clause);
5884 Error_Msg_N
5885 ("\child unit has visible homograph" &
5886 " (RM 8.3(26), 10.1.1(19))",
5887 Prev_Clause);
5888 exit;
5889 end;
5890 end if;
5891 end if;
5893 U2 := Homonym (U2);
5894 end loop;
5895 end;
5896 end if;
5897 end Install_With_Clause;
5899 -------------------
5900 -- Is_Child_Spec --
5901 -------------------
5903 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5904 K : constant Node_Kind := Nkind (Lib_Unit);
5906 begin
5907 return (K in N_Generic_Declaration or else
5908 K in N_Generic_Instantiation or else
5909 K in N_Generic_Renaming_Declaration or else
5910 K = N_Package_Declaration or else
5911 K = N_Package_Renaming_Declaration or else
5912 K = N_Subprogram_Declaration or else
5913 K = N_Subprogram_Renaming_Declaration)
5914 and then Present (Parent_Spec (Lib_Unit));
5915 end Is_Child_Spec;
5917 ------------------------------------
5918 -- Is_Legal_Shadow_Entity_In_Body --
5919 ------------------------------------
5921 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5922 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5923 begin
5924 return Nkind (Unit (C_Unit)) = N_Package_Body
5925 and then
5926 Has_With_Clause
5927 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5928 end Is_Legal_Shadow_Entity_In_Body;
5930 ----------------------
5931 -- Is_Ancestor_Unit --
5932 ----------------------
5934 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5935 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5936 E2 : Entity_Id;
5937 begin
5938 if Nkind (Unit (U2)) in N_Package_Body | N_Subprogram_Body then
5939 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5940 return Is_Ancestor_Package (E1, E2);
5941 else
5942 return False;
5943 end if;
5944 end Is_Ancestor_Unit;
5946 ----------------------------------
5947 -- Is_Visible_Through_Renamings --
5948 ----------------------------------
5950 function Is_Visible_Through_Renamings
5951 (P : Entity_Id;
5952 Error_Node : Node_Id := Empty) return Boolean
5954 function Is_Limited_Withed_Unit
5955 (Lib_Unit : Node_Id;
5956 Pkg_Ent : Entity_Id) return Boolean;
5957 -- Return True if Pkg_Ent is a limited-withed package of the given
5958 -- library unit.
5960 ----------------------------
5961 -- Is_Limited_Withed_Unit --
5962 ----------------------------
5964 function Is_Limited_Withed_Unit
5965 (Lib_Unit : Node_Id;
5966 Pkg_Ent : Entity_Id) return Boolean
5968 Item : Node_Id := First (Context_Items (Lib_Unit));
5970 begin
5971 while Present (Item) loop
5972 if Nkind (Item) = N_With_Clause
5973 and then Limited_Present (Item)
5974 and then Entity (Name (Item)) = Pkg_Ent
5975 then
5976 return True;
5977 end if;
5979 Next (Item);
5980 end loop;
5982 return False;
5983 end Is_Limited_Withed_Unit;
5985 -- Local variables
5987 Kind : constant Node_Kind := Nkind (Unit (Cunit (Current_Sem_Unit)));
5988 Aux_Unit : Node_Id;
5989 Item : Node_Id;
5990 Decl : Entity_Id;
5992 begin
5993 -- Example of the error detected by this subprogram:
5995 -- package P is
5996 -- type T is ...
5997 -- end P;
5999 -- with P;
6000 -- package Q is
6001 -- package Ren_P renames P;
6002 -- end Q;
6004 -- with Q;
6005 -- package R is ...
6007 -- limited with P; -- ERROR
6008 -- package R.C is ...
6010 Aux_Unit := Cunit (Current_Sem_Unit);
6012 loop
6013 Item := First (Context_Items (Aux_Unit));
6014 while Present (Item) loop
6015 if Nkind (Item) = N_With_Clause
6016 and then not Limited_Present (Item)
6017 and then Nkind (Unit (Library_Unit (Item))) =
6018 N_Package_Declaration
6019 then
6020 Decl :=
6021 First (Visible_Declarations
6022 (Specification (Unit (Library_Unit (Item)))));
6023 while Present (Decl) loop
6024 if Nkind (Decl) = N_Package_Renaming_Declaration
6025 and then Entity (Name (Decl)) = P
6026 and then not Is_Limited_Withed_Unit
6027 (Lib_Unit => Library_Unit (Item),
6028 Pkg_Ent => Entity (Name (Decl)))
6029 then
6030 -- Generate the error message only if the current unit
6031 -- is a package declaration; in case of subprogram
6032 -- bodies and package bodies we just return True to
6033 -- indicate that the limited view must not be
6034 -- installed.
6036 if Kind = N_Package_Declaration
6037 and then Present (Error_Node)
6038 then
6039 Error_Msg_N
6040 ("simultaneous visibility of the limited and " &
6041 "unlimited views not allowed", Error_Node);
6042 Error_Msg_Sloc := Sloc (Item);
6043 Error_Msg_NE
6044 ("\\ unlimited view of & visible through the " &
6045 "context clause #", Error_Node, P);
6046 Error_Msg_Sloc := Sloc (Decl);
6047 Error_Msg_NE ("\\ and the renaming #", Error_Node, P);
6048 end if;
6050 return True;
6051 end if;
6053 Next (Decl);
6054 end loop;
6055 end if;
6057 Next (Item);
6058 end loop;
6060 -- If it is a body not acting as spec, follow pointer to the
6061 -- corresponding spec, otherwise follow pointer to parent spec.
6063 if Present (Library_Unit (Aux_Unit))
6064 and then Nkind (Unit (Aux_Unit)) in
6065 N_Package_Body | N_Subprogram_Body
6066 then
6067 if Aux_Unit = Library_Unit (Aux_Unit) then
6069 -- Aux_Unit is a body that acts as a spec. Clause has
6070 -- already been flagged as illegal.
6072 return False;
6074 else
6075 Aux_Unit := Library_Unit (Aux_Unit);
6076 end if;
6078 else
6079 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
6080 end if;
6082 exit when No (Aux_Unit);
6083 end loop;
6085 return False;
6086 end Is_Visible_Through_Renamings;
6088 -----------------------
6089 -- Load_Needed_Body --
6090 -----------------------
6092 -- N is a generic unit named in a with clause, or else it is a unit that
6093 -- contains a generic unit or an inlined function. In order to perform an
6094 -- instantiation, the body of the unit must be present. If the unit itself
6095 -- is generic, we assume that an instantiation follows, and load & analyze
6096 -- the body unconditionally. This forces analysis of the spec as well.
6098 -- If the unit is not generic, but contains a generic unit, it is loaded on
6099 -- demand, at the point of instantiation (see ch12).
6101 procedure Load_Needed_Body
6102 (N : Node_Id;
6103 OK : out Boolean)
6105 Body_Name : Unit_Name_Type;
6106 Unum : Unit_Number_Type;
6108 Save_Style_Check : constant Boolean := Opt.Style_Check;
6109 -- The loading and analysis is done with style checks off
6111 begin
6112 if not GNAT_Mode then
6113 Style_Check := False;
6114 end if;
6116 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
6117 Unum :=
6118 Load_Unit
6119 (Load_Name => Body_Name,
6120 Required => False,
6121 Subunit => False,
6122 Error_Node => N,
6123 Renamings => True);
6125 if Unum = No_Unit then
6126 OK := False;
6128 else
6129 Compiler_State := Analyzing; -- reset after load
6131 if Fatal_Error (Unum) /= Error_Detected or else Try_Semantics then
6132 if Debug_Flag_L then
6133 Write_Str ("*** Loaded generic body");
6134 Write_Eol;
6135 end if;
6137 -- We always perform analyses
6138 Semantics (Cunit (Unum));
6139 end if;
6141 OK := True;
6142 end if;
6144 Style_Check := Save_Style_Check;
6145 end Load_Needed_Body;
6147 -------------------------
6148 -- Build_Limited_Views --
6149 -------------------------
6151 procedure Build_Limited_Views (N : Node_Id) is
6152 Unum : constant Unit_Number_Type :=
6153 Get_Source_Unit (Library_Unit (N));
6154 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
6156 Shadow_Pack : Entity_Id;
6157 -- The corresponding shadow entity of the withed package. This entity
6158 -- offers incomplete views of packages and types as well as abstract
6159 -- views of states and variables declared within.
6161 Last_Shadow : Entity_Id := Empty;
6162 -- The last shadow entity created by routine Build_Shadow_Entity
6164 procedure Build_Shadow_Entity
6165 (Ent : Entity_Id;
6166 Scop : Entity_Id;
6167 Shadow : out Entity_Id;
6168 Is_Tagged : Boolean := False);
6169 -- Create a shadow entity that hides Ent and offers an abstract or
6170 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
6171 -- should be set when Ent is a tagged type. The generated entity is
6172 -- added to Shadow_Pack. The routine updates the value of Last_Shadow.
6174 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
6175 -- Perform minimal decoration of a package or its corresponding shadow
6176 -- entity denoted by Ent. Scop is the proper scope.
6178 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id);
6179 -- Perform full decoration of an abstract state or its corresponding
6180 -- shadow entity denoted by Ent. Scop is the proper scope.
6182 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id);
6183 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
6184 -- proper scope.
6186 procedure Process_Declarations_And_States
6187 (Pack : Entity_Id;
6188 Decls : List_Id;
6189 Scop : Entity_Id;
6190 Create_Abstract_Views : Boolean);
6191 -- Inspect the states of package Pack and declarative list Decls. Create
6192 -- shadow entities for all nested packages, states, types and variables
6193 -- encountered. Scop is the proper scope. Create_Abstract_Views should
6194 -- be set when the abstract states and variables need to be processed.
6196 -------------------------
6197 -- Build_Shadow_Entity --
6198 -------------------------
6200 procedure Build_Shadow_Entity
6201 (Ent : Entity_Id;
6202 Scop : Entity_Id;
6203 Shadow : out Entity_Id;
6204 Is_Tagged : Boolean := False)
6206 begin
6207 Shadow := Make_Temporary (Sloc (Ent), 'Z');
6209 -- The shadow entity must share the same name and parent as the
6210 -- entity it hides.
6212 Set_Chars (Shadow, Chars (Ent));
6213 Set_Parent (Shadow, Parent (Ent));
6215 -- The abstract view of a variable is a state, not another variable
6217 if Ekind (Ent) = E_Variable then
6218 Mutate_Ekind (Shadow, E_Abstract_State);
6219 else
6220 Mutate_Ekind (Shadow, Ekind (Ent));
6221 end if;
6223 Set_Is_Not_Self_Hidden (Shadow);
6224 Set_Is_Internal (Shadow);
6225 Set_From_Limited_With (Shadow);
6227 -- Add the new shadow entity to the limited view of the package
6229 Last_Shadow := Shadow;
6230 Append_Entity (Shadow, Shadow_Pack);
6232 -- Perform context-specific decoration of the shadow entity
6234 if Ekind (Ent) = E_Abstract_State then
6235 Decorate_State (Shadow, Scop);
6236 Set_Non_Limited_View (Shadow, Ent);
6238 elsif Ekind (Ent) = E_Package then
6239 Decorate_Package (Shadow, Scop);
6241 elsif Is_Type (Ent) then
6242 Decorate_Type (Shadow, Scop, Is_Tagged);
6244 -- If Ent is a private type and we are analyzing the body of its
6245 -- scope, its private and full views are swapped and, therefore,
6246 -- we need to undo this swapping in order to build the same shadow
6247 -- entity as we would have in other contexts.
6249 if Is_Private_Type (Ent)
6250 and then Present (Full_View (Ent))
6251 and then In_Package_Body (Scop)
6252 then
6253 Set_Non_Limited_View (Shadow, Full_View (Ent));
6254 else
6255 Set_Non_Limited_View (Shadow, Ent);
6256 end if;
6258 if Is_Tagged then
6259 Set_Non_Limited_View
6260 (Class_Wide_Type (Shadow), Class_Wide_Type (Ent));
6261 end if;
6263 if Is_Incomplete_Or_Private_Type (Ent) then
6264 Set_Private_Dependents (Shadow, New_Elmt_List);
6265 end if;
6267 elsif Ekind (Ent) = E_Variable then
6268 Decorate_State (Shadow, Scop);
6269 Set_Non_Limited_View (Shadow, Ent);
6270 end if;
6271 end Build_Shadow_Entity;
6273 ----------------------
6274 -- Decorate_Package --
6275 ----------------------
6277 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
6278 begin
6279 Mutate_Ekind (Ent, E_Package);
6280 Set_Etype (Ent, Standard_Void_Type);
6281 Set_Scope (Ent, Scop);
6282 end Decorate_Package;
6284 --------------------
6285 -- Decorate_State --
6286 --------------------
6288 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id) is
6289 begin
6290 Mutate_Ekind (Ent, E_Abstract_State);
6291 Set_Is_Not_Self_Hidden (Ent);
6292 Set_Etype (Ent, Standard_Void_Type);
6293 Set_Scope (Ent, Scop);
6294 Set_Encapsulating_State (Ent, Empty);
6295 end Decorate_State;
6297 -----------------------
6298 -- Decorate_Variable --
6299 -----------------------
6301 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id) is
6302 begin
6303 Mutate_Ekind (Ent, E_Variable);
6304 Set_Etype (Ent, Standard_Void_Type);
6305 Set_Scope (Ent, Scop);
6306 end Decorate_Variable;
6308 -------------------------------------
6309 -- Process_Declarations_And_States --
6310 -------------------------------------
6312 procedure Process_Declarations_And_States
6313 (Pack : Entity_Id;
6314 Decls : List_Id;
6315 Scop : Entity_Id;
6316 Create_Abstract_Views : Boolean)
6318 procedure Find_And_Process_States;
6319 -- Determine whether package Pack defines abstract state either by
6320 -- using an aspect or a pragma. If this is the case, build shadow
6321 -- entities for all abstract states of Pack.
6323 procedure Process_States (States : Elist_Id);
6324 -- Generate shadow entities for all abstract states in list States
6326 -----------------------------
6327 -- Find_And_Process_States --
6328 -----------------------------
6330 procedure Find_And_Process_States is
6331 procedure Process_State (State : Node_Id);
6332 -- Generate shadow entities for a single abstract state or
6333 -- multiple states expressed as an aggregate.
6335 -------------------
6336 -- Process_State --
6337 -------------------
6339 procedure Process_State (State : Node_Id) is
6340 Loc : constant Source_Ptr := Sloc (State);
6341 Decl : Node_Id;
6342 Dummy : Entity_Id;
6343 Elmt : Node_Id;
6344 Id : Entity_Id;
6346 begin
6347 -- Multiple abstract states appear as an aggregate
6349 if Nkind (State) = N_Aggregate then
6350 Elmt := First (Expressions (State));
6351 while Present (Elmt) loop
6352 Process_State (Elmt);
6353 Next (Elmt);
6354 end loop;
6356 return;
6358 -- A null state has no abstract view
6360 elsif Nkind (State) = N_Null then
6361 return;
6363 -- State declaration with various options appears as an
6364 -- extension aggregate.
6366 elsif Nkind (State) = N_Extension_Aggregate then
6367 Decl := Ancestor_Part (State);
6369 -- Simple state declaration
6371 elsif Nkind (State) = N_Identifier then
6372 Decl := State;
6374 -- Possibly an illegal state declaration
6376 else
6377 return;
6378 end if;
6380 -- Abstract states are elaborated when the related pragma is
6381 -- elaborated. Since the withed package is not analyzed yet,
6382 -- the entities of the abstract states are not available. To
6383 -- overcome this complication, create the entities now and
6384 -- store them in their respective declarations. The entities
6385 -- are later used by routine Create_Abstract_State to declare
6386 -- and enter the states into visibility.
6388 if No (Entity (Decl)) then
6389 Id := Make_Defining_Identifier (Loc, Chars (Decl));
6391 Set_Entity (Decl, Id);
6392 Set_Parent (Id, State);
6393 Decorate_State (Id, Scop);
6395 -- Otherwise the package was previously withed
6397 else
6398 Id := Entity (Decl);
6399 end if;
6401 Build_Shadow_Entity (Id, Scop, Dummy);
6402 end Process_State;
6404 -- Local variables
6406 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack);
6407 Asp : Node_Id;
6408 Decl : Node_Id;
6410 -- Start of processing for Find_And_Process_States
6412 begin
6413 -- Find aspect Abstract_State
6415 Asp := First (Aspect_Specifications (Pack_Decl));
6416 while Present (Asp) loop
6417 if Chars (Identifier (Asp)) = Name_Abstract_State then
6418 Process_State (Expression (Asp));
6420 return;
6421 end if;
6423 Next (Asp);
6424 end loop;
6426 -- Find pragma Abstract_State by inspecting the declarations
6428 Decl := First (Decls);
6429 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
6430 if Pragma_Name (Decl) = Name_Abstract_State then
6431 Process_State
6432 (Get_Pragma_Arg
6433 (First (Pragma_Argument_Associations (Decl))));
6435 return;
6436 end if;
6438 Next (Decl);
6439 end loop;
6440 end Find_And_Process_States;
6442 --------------------
6443 -- Process_States --
6444 --------------------
6446 procedure Process_States (States : Elist_Id) is
6447 Dummy : Entity_Id;
6448 Elmt : Elmt_Id;
6450 begin
6451 Elmt := First_Elmt (States);
6452 while Present (Elmt) loop
6453 Build_Shadow_Entity (Node (Elmt), Scop, Dummy);
6455 Next_Elmt (Elmt);
6456 end loop;
6457 end Process_States;
6459 -- Local variables
6461 Is_Tagged : Boolean;
6462 Decl : Node_Id;
6463 Def : Node_Id;
6464 Def_Id : Entity_Id;
6465 Shadow : Entity_Id;
6467 -- Start of processing for Process_Declarations_And_States
6469 begin
6470 -- Build abstract views for all states defined in the package
6472 if Create_Abstract_Views then
6474 -- When a package has been analyzed, all states are stored in list
6475 -- Abstract_States. Generate the shadow entities directly.
6477 if Is_Analyzed then
6478 if Present (Abstract_States (Pack)) then
6479 Process_States (Abstract_States (Pack));
6480 end if;
6482 -- The package may declare abstract states by using an aspect or a
6483 -- pragma. Attempt to locate one of these construct and if found,
6484 -- build the shadow entities.
6486 else
6487 Find_And_Process_States;
6488 end if;
6489 end if;
6491 -- Inspect the declarative list, looking for nested packages, types
6492 -- and variable declarations.
6494 Decl := First (Decls);
6495 while Present (Decl) loop
6497 -- Packages
6499 if Nkind (Decl) = N_Package_Declaration then
6500 Def_Id := Defining_Entity (Decl);
6502 -- Perform minor decoration when the withed package has not
6503 -- been analyzed.
6505 if not Is_Analyzed then
6506 Decorate_Package (Def_Id, Scop);
6507 end if;
6509 -- Create a shadow entity that offers a limited view of all
6510 -- visible types declared within.
6512 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6514 Process_Declarations_And_States
6515 (Pack => Def_Id,
6516 Decls =>
6517 Visible_Declarations (Specification (Decl)),
6518 Scop => Shadow,
6519 Create_Abstract_Views => Create_Abstract_Views);
6521 -- Types
6523 elsif Nkind (Decl) in N_Full_Type_Declaration
6524 | N_Incomplete_Type_Declaration
6525 | N_Private_Extension_Declaration
6526 | N_Private_Type_Declaration
6527 | N_Protected_Type_Declaration
6528 | N_Task_Type_Declaration
6529 then
6530 Def_Id := Defining_Entity (Decl);
6532 -- Determine whether the type is tagged. Note that packages
6533 -- included via a limited with clause are not always analyzed,
6534 -- hence the tree lookup rather than the use of attribute
6535 -- Is_Tagged_Type.
6537 if Nkind (Decl) = N_Full_Type_Declaration then
6538 Def := Type_Definition (Decl);
6540 Is_Tagged :=
6541 (Nkind (Def) = N_Record_Definition
6542 and then Tagged_Present (Def))
6543 or else
6544 (Nkind (Def) = N_Derived_Type_Definition
6545 and then Present (Record_Extension_Part (Def)));
6547 elsif Nkind (Decl) in N_Incomplete_Type_Declaration
6548 | N_Private_Type_Declaration
6549 then
6550 Is_Tagged := Tagged_Present (Decl);
6552 elsif Nkind (Decl) = N_Private_Extension_Declaration then
6553 Is_Tagged := True;
6555 else
6556 Is_Tagged := False;
6557 end if;
6559 -- Perform minor decoration when the withed package has not
6560 -- been analyzed.
6562 if not Is_Analyzed then
6563 Decorate_Type (Def_Id, Scop, Is_Tagged, True);
6564 end if;
6566 -- Create a shadow entity that hides the type and offers an
6567 -- incomplete view of the said type.
6569 Build_Shadow_Entity (Def_Id, Scop, Shadow, Is_Tagged);
6571 -- Variables
6573 elsif Create_Abstract_Views
6574 and then Nkind (Decl) = N_Object_Declaration
6575 and then not Constant_Present (Decl)
6576 then
6577 Def_Id := Defining_Entity (Decl);
6579 -- Perform minor decoration when the withed package has not
6580 -- been analyzed.
6582 if not Is_Analyzed then
6583 Decorate_Variable (Def_Id, Scop);
6584 end if;
6586 -- Create a shadow entity that hides the variable and offers an
6587 -- abstract view of the said variable.
6589 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6590 end if;
6592 Next (Decl);
6593 end loop;
6594 end Process_Declarations_And_States;
6596 -- Local variables
6598 Nam : constant Node_Id := Name (N);
6599 Pack : constant Entity_Id := Cunit_Entity (Unum);
6601 Last_Public_Shadow : Entity_Id := Empty;
6602 Private_Shadow : Entity_Id;
6603 Spec : Node_Id;
6605 -- Start of processing for Build_Limited_Views
6607 begin
6608 pragma Assert (Limited_Present (N));
6610 -- A library_item mentioned in a limited_with_clause is a package
6611 -- declaration, not a subprogram declaration, generic declaration,
6612 -- generic instantiation, or package renaming declaration.
6614 case Nkind (Unit (Library_Unit (N))) is
6615 when N_Package_Declaration =>
6616 null;
6618 when N_Subprogram_Declaration =>
6619 Error_Msg_N
6620 ("subprogram not allowed in `LIMITED WITH` clause", N);
6621 return;
6623 when N_Generic_Declaration =>
6624 Error_Msg_N ("generic not allowed in `LIMITED WITH` clause", N);
6625 return;
6627 when N_Generic_Instantiation =>
6628 Error_Msg_N
6629 ("generic instantiation not allowed in `LIMITED WITH` clause",
6631 return;
6633 when N_Generic_Renaming_Declaration =>
6634 Error_Msg_N
6635 ("generic renaming not allowed in `LIMITED WITH` clause", N);
6636 return;
6638 when N_Subprogram_Renaming_Declaration =>
6639 Error_Msg_N
6640 ("renamed subprogram not allowed in `LIMITED WITH` clause", N);
6641 return;
6643 when N_Package_Renaming_Declaration =>
6644 Error_Msg_N
6645 ("renamed package not allowed in `LIMITED WITH` clause", N);
6646 return;
6648 when others =>
6649 raise Program_Error;
6650 end case;
6652 -- The withed unit may not be analyzed, but the with clause itself
6653 -- must be minimally decorated. This ensures that the checks on unused
6654 -- with clauses also process limieted withs.
6656 Mutate_Ekind (Pack, E_Package);
6657 Set_Is_Not_Self_Hidden (Pack);
6658 Set_Etype (Pack, Standard_Void_Type);
6660 if Is_Entity_Name (Nam) then
6661 Set_Entity (Nam, Pack);
6663 elsif Nkind (Nam) = N_Selected_Component then
6664 Set_Entity (Selector_Name (Nam), Pack);
6665 end if;
6667 -- Check if the chain is already built
6669 Spec := Specification (Unit (Library_Unit (N)));
6671 if Limited_View_Installed (Spec) then
6672 return;
6673 end if;
6675 -- Create the shadow package wich hides the withed unit and provides
6676 -- incomplete view of all types and packages declared within.
6678 Shadow_Pack := Make_Temporary (Sloc (N), 'Z');
6679 Mutate_Ekind (Shadow_Pack, E_Package);
6680 Set_Is_Internal (Shadow_Pack);
6681 Set_Limited_View (Pack, Shadow_Pack);
6683 -- Inspect the abstract states and visible declarations of the withed
6684 -- unit and create shadow entities that hide existing packages, states,
6685 -- variables and types.
6687 Process_Declarations_And_States
6688 (Pack => Pack,
6689 Decls => Visible_Declarations (Spec),
6690 Scop => Pack,
6691 Create_Abstract_Views => True);
6693 Last_Public_Shadow := Last_Shadow;
6695 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6696 -- to accommodate limited private with clauses.
6698 Process_Declarations_And_States
6699 (Pack => Pack,
6700 Decls => Private_Declarations (Spec),
6701 Scop => Pack,
6702 Create_Abstract_Views => False);
6704 if Present (Last_Public_Shadow) then
6705 Private_Shadow := Next_Entity (Last_Public_Shadow);
6706 else
6707 Private_Shadow := First_Entity (Shadow_Pack);
6708 end if;
6710 Set_First_Private_Entity (Shadow_Pack, Private_Shadow);
6711 Set_Limited_View_Installed (Spec);
6712 end Build_Limited_Views;
6714 ----------------------------
6715 -- Check_No_Elab_Code_All --
6716 ----------------------------
6718 procedure Check_No_Elab_Code_All (N : Node_Id) is
6719 begin
6720 if Present (No_Elab_Code_All_Pragma)
6721 and then In_Extended_Main_Source_Unit (N)
6722 and then Present (Context_Items (N))
6723 then
6724 declare
6725 CL : constant List_Id := Context_Items (N);
6726 CI : Node_Id;
6728 begin
6729 CI := First (CL);
6730 while Present (CI) loop
6731 if Nkind (CI) = N_With_Clause
6732 and then not
6733 No_Elab_Code_All (Get_Source_Unit (Library_Unit (CI)))
6735 -- In GNATprove mode, some runtime units are implicitly
6736 -- loaded to make their entities available for analysis. In
6737 -- this case, ignore violations of No_Elaboration_Code_All
6738 -- for this special analysis mode.
6740 and then not
6741 (GNATprove_Mode and then Implicit_With (CI))
6742 then
6743 Error_Msg_Sloc := Sloc (No_Elab_Code_All_Pragma);
6744 Error_Msg_N
6745 ("violation of No_Elaboration_Code_All#", CI);
6746 Error_Msg_NE
6747 ("\unit& does not have No_Elaboration_Code_All",
6748 CI, Entity (Name (CI)));
6749 end if;
6751 Next (CI);
6752 end loop;
6753 end;
6754 end if;
6755 end Check_No_Elab_Code_All;
6757 -------------------------------
6758 -- Check_Body_Needed_For_SAL --
6759 -------------------------------
6761 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
6762 function Entity_Needs_Body (E : Entity_Id) return Boolean;
6763 -- Determine whether use of entity E might require the presence of its
6764 -- body. For a package this requires a recursive traversal of all nested
6765 -- declarations.
6767 -----------------------
6768 -- Entity_Needs_Body --
6769 -----------------------
6771 function Entity_Needs_Body (E : Entity_Id) return Boolean is
6772 Ent : Entity_Id;
6774 begin
6775 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
6776 return True;
6778 elsif Is_Generic_Subprogram (E) then
6780 -- A generic subprogram always requires the presence of its
6781 -- body because an instantiation needs both templates. The only
6782 -- exceptions is a generic subprogram renaming. In this case the
6783 -- body is needed only when the template is declared outside the
6784 -- compilation unit being checked.
6786 if Present (Renamed_Entity (E)) then
6787 return not Within_Scope (E, Unit_Name);
6788 else
6789 return True;
6790 end if;
6792 elsif Ekind (E) = E_Generic_Package
6793 and then
6794 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
6795 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6796 then
6797 return True;
6799 elsif Ekind (E) = E_Package
6800 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
6801 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6802 then
6803 Ent := First_Entity (E);
6804 while Present (Ent) loop
6805 if Entity_Needs_Body (Ent) then
6806 return True;
6807 end if;
6809 Next_Entity (Ent);
6810 end loop;
6812 return False;
6814 else
6815 return False;
6816 end if;
6817 end Entity_Needs_Body;
6819 -- Start of processing for Check_Body_Needed_For_SAL
6821 begin
6822 if Ekind (Unit_Name) = E_Generic_Package
6823 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6824 N_Generic_Package_Declaration
6825 and then
6826 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
6827 then
6828 Set_Body_Needed_For_SAL (Unit_Name);
6830 elsif Ekind (Unit_Name) in E_Generic_Procedure | E_Generic_Function then
6831 Set_Body_Needed_For_SAL (Unit_Name);
6833 elsif Is_Subprogram (Unit_Name)
6834 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6835 N_Subprogram_Declaration
6836 and then Has_Pragma_Inline (Unit_Name)
6837 then
6838 Set_Body_Needed_For_SAL (Unit_Name);
6840 elsif Ekind (Unit_Name) = E_Subprogram_Body then
6841 Check_Body_Needed_For_SAL
6842 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6844 elsif Ekind (Unit_Name) = E_Package
6845 and then Entity_Needs_Body (Unit_Name)
6846 then
6847 Set_Body_Needed_For_SAL (Unit_Name);
6849 elsif Ekind (Unit_Name) = E_Package_Body
6850 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
6851 then
6852 Check_Body_Needed_For_SAL
6853 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6854 end if;
6855 end Check_Body_Needed_For_SAL;
6857 --------------------
6858 -- Remove_Context --
6859 --------------------
6861 procedure Remove_Context (N : Node_Id) is
6862 Lib_Unit : constant Node_Id := Unit (N);
6864 begin
6865 -- If this is a child unit, first remove the parent units
6867 if Is_Child_Spec (Lib_Unit) then
6868 Remove_Parents (Lib_Unit);
6869 end if;
6871 Remove_Context_Clauses (N);
6872 end Remove_Context;
6874 ----------------------------
6875 -- Remove_Context_Clauses --
6876 ----------------------------
6878 procedure Remove_Context_Clauses (N : Node_Id) is
6879 Item : Node_Id;
6880 Unit_Name : Entity_Id;
6882 begin
6883 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6884 -- limited-views first and regular-views later (to maintain the stack
6885 -- model).
6887 -- First Phase: Remove limited_with context clauses
6889 Item := First (Context_Items (N));
6890 while Present (Item) loop
6892 -- We are interested only in with clauses that got installed on entry
6894 if Nkind (Item) = N_With_Clause
6895 and then Limited_Present (Item)
6896 then
6897 if Limited_View_Installed (Item) then
6898 Remove_Limited_With_Clause (Item);
6900 -- An unusual case: If the library unit of the Main_Unit has a
6901 -- limited with_clause on some unit P and the context somewhere
6902 -- includes a with_clause on P, P has been analyzed. The entity
6903 -- for P is still visible, which in general is harmless because
6904 -- this is the end of the compilation, but it can affect pending
6905 -- instantiations that may have been generated elsewhere, so it
6906 -- it is necessary to remove U from visibility so that inlining
6907 -- and the analysis of instance bodies can proceed cleanly.
6909 elsif Current_Sem_Unit = Main_Unit
6910 and then Serious_Errors_Detected = 0
6911 and then not Implicit_With (Item)
6912 then
6913 Set_Is_Immediately_Visible
6914 (Defining_Entity (Unit (Library_Unit (Item))), False);
6915 end if;
6916 end if;
6918 Next (Item);
6919 end loop;
6921 -- Second Phase: Loop through context items and undo regular
6922 -- with_clauses and use_clauses.
6924 Item := First (Context_Items (N));
6925 while Present (Item) loop
6927 -- We are interested only in with clauses which got installed on
6928 -- entry, as indicated by their Context_Installed flag set
6930 if Nkind (Item) = N_With_Clause
6931 and then Limited_Present (Item)
6932 and then Limited_View_Installed (Item)
6933 then
6934 null;
6936 elsif Nkind (Item) = N_With_Clause
6937 and then Context_Installed (Item)
6938 then
6939 -- Remove items from one with'ed unit
6941 Unit_Name := Entity (Name (Item));
6942 Remove_Unit_From_Visibility (Unit_Name);
6943 Set_Context_Installed (Item, False);
6945 elsif Nkind (Item) = N_Use_Package_Clause then
6946 End_Use_Package (Item);
6948 elsif Nkind (Item) = N_Use_Type_Clause then
6949 End_Use_Type (Item);
6950 end if;
6952 Next (Item);
6953 end loop;
6954 end Remove_Context_Clauses;
6956 --------------------------------
6957 -- Remove_Limited_With_Clause --
6958 --------------------------------
6960 procedure Remove_Limited_With_Clause (N : Node_Id) is
6961 Pack_Decl : constant Entity_Id := Unit (Library_Unit (N));
6963 begin
6964 pragma Assert (Limited_View_Installed (N));
6966 -- Limited with clauses that designate units other than packages are
6967 -- illegal and are never installed.
6969 if Nkind (Pack_Decl) = N_Package_Declaration then
6970 Remove_Limited_With_Unit (Pack_Decl, N);
6971 end if;
6973 -- Indicate that the limited views of the clause have been removed
6975 Set_Limited_View_Installed (N, False);
6976 end Remove_Limited_With_Clause;
6978 ------------------------------
6979 -- Remove_Limited_With_Unit --
6980 ------------------------------
6982 procedure Remove_Limited_With_Unit
6983 (Pack_Decl : Node_Id;
6984 Lim_Clause : Node_Id := Empty)
6986 procedure Remove_Shadow_Entities_From_Visibility (Pack_Id : Entity_Id);
6987 -- Remove the shadow entities of package Pack_Id from direct visibility
6989 procedure Remove_Shadow_Entities_With_Restore (Pack_Id : Entity_Id);
6990 -- Remove the shadow entities of package Pack_Id from direct visibility,
6991 -- restore the corresponding entities they hide into direct visibility,
6992 -- and update the entity and homonym chains.
6994 --------------------------------------------
6995 -- Remove_Shadow_Entities_From_Visibility --
6996 --------------------------------------------
6998 procedure Remove_Shadow_Entities_From_Visibility (Pack_Id : Entity_Id) is
6999 Lim_Header : constant Entity_Id := Limited_View (Pack_Id);
7000 Upto : constant Entity_Id := First_Private_Entity (Lim_Header);
7002 Shadow : Entity_Id;
7004 begin
7005 -- Remove the package from direct visibility
7007 Unchain (Pack_Id);
7008 Set_Is_Immediately_Visible (Pack_Id, False);
7010 -- Remove all shadow entities from direct visibility
7012 Shadow := First_Entity (Lim_Header);
7013 while Present (Shadow) and then Shadow /= Upto loop
7014 Unchain (Shadow);
7015 Next_Entity (Shadow);
7016 end loop;
7017 end Remove_Shadow_Entities_From_Visibility;
7019 -----------------------------------------
7020 -- Remove_Shadow_Entities_With_Restore --
7021 -----------------------------------------
7023 -- This code must be kept synchronized with the code that replaces the
7024 -- real entities by the shadow entities in Install_Limited_With_Clause,
7025 -- otherwise the contents of the homonym chains are not consistent.
7027 procedure Remove_Shadow_Entities_With_Restore (Pack_Id : Entity_Id) is
7028 procedure Restore_Chain_For_Shadow (Shadow : Entity_Id);
7029 -- Remove shadow entity Shadow by updating the entity and homonym
7030 -- chains.
7032 procedure Restore_Chains
7033 (From : Entity_Id;
7034 Upto : Entity_Id);
7035 -- Remove a sequence of shadow entities starting from From and ending
7036 -- prior to Upto by updating the entity and homonym chains.
7038 procedure Restore_Type_Visibility
7039 (From : Entity_Id;
7040 Upto : Entity_Id);
7041 -- Restore a sequence of types starting from From and ending prior to
7042 -- Upto back in direct visibility.
7044 ------------------------------
7045 -- Restore_Chain_For_Shadow --
7046 ------------------------------
7048 procedure Restore_Chain_For_Shadow (Shadow : Entity_Id) is
7049 Typ : constant Entity_Id := Non_Limited_View (Shadow);
7050 pragma Assert (not In_Chain (Typ));
7052 Prev : Entity_Id;
7054 begin
7055 -- If the package has incomplete types, the limited view of the
7056 -- incomplete type is in fact never visible (AI05-129) but we
7057 -- have created a shadow entity E1 for it, that points to E2,
7058 -- the incomplete type at stake. This in turn has a full view
7059 -- E3 that is the full declaration, with a corresponding
7060 -- shadow entity E4. When reinstalling the nonlimited view,
7061 -- the visible entity E4 is replaced directly with E2 in the
7062 -- the homonyms list and E3 is simply ignored.
7064 -- regular views limited views
7066 -- * E2 (incomplete) <-- E1 (shadow)
7068 -- |
7069 -- V
7071 -- E3 (full) <-- E4 (shadow) *
7073 -- [*] denotes the visible entity (Current_Entity)
7075 Prev := Current_Entity (Shadow);
7077 while Present (Prev) loop
7078 -- This is a regular replacement
7080 if Prev = Shadow then
7081 Replace (Prev, Typ);
7083 if Debug_Flag_I then
7084 Write_Str (" (homonym) replace ");
7085 Write_Name (Chars (Typ));
7086 Write_Eol;
7087 end if;
7089 exit;
7091 -- This is where E4 is replaced with E2
7093 elsif Ekind (Prev) = E_Incomplete_Type
7094 and then From_Limited_With (Prev)
7095 and then Ekind (Typ) = E_Incomplete_Type
7096 and then Full_View (Typ) = Non_Limited_View (Prev)
7097 then
7098 Replace (Prev, Typ);
7100 if Debug_Flag_I then
7101 Write_Str (" (homonym) E4 -> E2 ");
7102 Write_Name (Chars (Typ));
7103 Write_Eol;
7104 end if;
7106 exit;
7107 end if;
7109 Prev := Homonym (Prev);
7110 end loop;
7111 end Restore_Chain_For_Shadow;
7113 --------------------
7114 -- Restore_Chains --
7115 --------------------
7117 procedure Restore_Chains
7118 (From : Entity_Id;
7119 Upto : Entity_Id)
7121 Shadow : Entity_Id;
7123 begin
7124 Shadow := From;
7125 while Present (Shadow) and then Shadow /= Upto loop
7127 -- Do not unchain nested packages and child units
7129 if Ekind (Shadow) = E_Package then
7130 null;
7132 elsif Is_Child_Unit (Non_Limited_View (Shadow)) then
7133 null;
7135 else
7136 Restore_Chain_For_Shadow (Shadow);
7137 end if;
7139 Next_Entity (Shadow);
7140 end loop;
7141 end Restore_Chains;
7143 -----------------------------
7144 -- Restore_Type_Visibility --
7145 -----------------------------
7147 procedure Restore_Type_Visibility
7148 (From : Entity_Id;
7149 Upto : Entity_Id)
7151 Typ : Entity_Id;
7153 begin
7154 Typ := From;
7155 while Present (Typ) and then Typ /= Upto loop
7156 if Is_Type (Typ) then
7157 Set_Is_Hidden (Typ, Was_Hidden (Typ));
7158 end if;
7160 Next_Entity (Typ);
7161 end loop;
7162 end Restore_Type_Visibility;
7164 -- Local variables
7166 Lim_Header : constant Entity_Id := Limited_View (Pack_Id);
7168 -- Start of processing Remove_Shadow_Entities_With_Restore
7170 begin
7171 -- The limited view of a package is being uninstalled by removing
7172 -- the effects of a limited with clause. If the clause appears in a
7173 -- unit which is not part of the main unit closure, then the related
7174 -- package must not be visible.
7176 if Present (Lim_Clause)
7177 and then not In_Extended_Main_Source_Unit (Lim_Clause)
7178 then
7179 Set_Is_Immediately_Visible (Pack_Id, False);
7181 -- Otherwise a limited view is being overridden by a nonlimited view.
7182 -- Leave the visibility of the package as is because the unit must be
7183 -- visible when the nonlimited view is installed.
7185 else
7186 null;
7187 end if;
7189 -- Remove the shadow entities from visibility by updating the entity
7190 -- and homonym chains.
7192 Restore_Chains
7193 (From => First_Entity (Lim_Header),
7194 Upto => First_Private_Entity (Lim_Header));
7196 -- Reinstate the types that were hidden by the shadow entities back
7197 -- into direct visibility.
7199 Restore_Type_Visibility
7200 (From => First_Entity (Pack_Id),
7201 Upto => First_Private_Entity (Pack_Id));
7202 end Remove_Shadow_Entities_With_Restore;
7204 -- Local variables
7206 Pack_Id : constant Entity_Id := Defining_Entity (Pack_Decl);
7208 -- Start of processing for Remove_Limited_With_Unit
7210 begin
7211 -- Nothing to do when the limited view of the package is not installed
7213 if not From_Limited_With (Pack_Id) then
7214 return;
7215 end if;
7217 if Debug_Flag_I then
7218 Write_Str ("remove limited view of ");
7219 Write_Name (Chars (Pack_Id));
7220 Write_Str (" from visibility");
7221 Write_Eol;
7222 end if;
7224 -- The package already appears in the compilation closure. As a result,
7225 -- its shadow entities must be replaced by the real entities they hide
7226 -- and the previously hidden entities must be entered back into direct
7227 -- visibility.
7229 if Analyzed (Pack_Decl) then
7230 Remove_Shadow_Entities_With_Restore (Pack_Id);
7232 -- Otherwise the package is not analyzed and its shadow entities must be
7233 -- removed from direct visibility.
7235 else
7236 Remove_Shadow_Entities_From_Visibility (Pack_Id);
7237 end if;
7239 -- Indicate that the limited view of the package is not installed
7241 Set_From_Limited_With (Pack_Id, False);
7242 end Remove_Limited_With_Unit;
7244 --------------------
7245 -- Remove_Parents --
7246 --------------------
7248 procedure Remove_Parents (Lib_Unit : Node_Id) is
7249 P : Node_Id;
7250 P_Name : Entity_Id;
7251 P_Spec : Node_Id := Empty;
7252 E : Entity_Id;
7253 Vis : constant Boolean :=
7254 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
7256 begin
7257 if Is_Child_Spec (Lib_Unit) then
7258 P_Spec := Parent_Spec (Lib_Unit);
7260 elsif Nkind (Lib_Unit) = N_Package_Body
7261 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
7262 then
7263 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
7264 end if;
7266 if Present (P_Spec) then
7267 P := Unit (P_Spec);
7268 P_Name := Get_Parent_Entity (P);
7269 Remove_Context_Clauses (P_Spec);
7270 End_Package_Scope (P_Name);
7271 Set_Is_Immediately_Visible (P_Name, Vis);
7273 -- Remove from visibility the siblings as well, which are directly
7274 -- visible while the parent is in scope.
7276 E := First_Entity (P_Name);
7277 while Present (E) loop
7278 if Is_Child_Unit (E) then
7279 Set_Is_Immediately_Visible (E, False);
7280 end if;
7282 Next_Entity (E);
7283 end loop;
7285 Set_In_Package_Body (P_Name, False);
7287 -- This is the recursive call to remove the context of any higher
7288 -- level parent. This recursion ensures that all parents are removed
7289 -- in the reverse order of their installation.
7291 Remove_Parents (P);
7292 end if;
7293 end Remove_Parents;
7295 ---------------------------------
7296 -- Remove_Private_With_Clauses --
7297 ---------------------------------
7299 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
7300 Item : Node_Id;
7302 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
7303 -- Check whether a given unit appears in a regular with_clause. Used to
7304 -- determine whether a private_with_clause, implicit or explicit, should
7305 -- be ignored.
7307 ----------------------------
7308 -- In_Regular_With_Clause --
7309 ----------------------------
7311 function In_Regular_With_Clause (E : Entity_Id) return Boolean is
7312 Item : Node_Id;
7314 begin
7315 Item := First (Context_Items (Comp_Unit));
7317 while Present (Item) loop
7318 if Nkind (Item) = N_With_Clause
7320 -- The following guard is needed to ensure that the name has
7321 -- been properly analyzed before we go fetching its entity.
7323 and then Is_Entity_Name (Name (Item))
7324 and then Entity (Name (Item)) = E
7325 and then not Private_Present (Item)
7326 then
7327 return True;
7328 end if;
7330 Next (Item);
7331 end loop;
7333 return False;
7334 end In_Regular_With_Clause;
7336 -- Start of processing for Remove_Private_With_Clauses
7338 begin
7339 Item := First (Context_Items (Comp_Unit));
7340 while Present (Item) loop
7341 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
7343 -- If private_with_clause is redundant, remove it from context,
7344 -- as a small optimization to subsequent handling of private_with
7345 -- clauses in other nested packages. We replace the clause with
7346 -- a null statement, which is otherwise ignored by the rest of
7347 -- the compiler.
7349 if In_Regular_With_Clause (Entity (Name (Item))) then
7350 declare
7351 Nxt : constant Node_Id := Next (Item);
7352 begin
7353 Rewrite (Item, Make_Null_Statement (Sloc (Item)));
7354 Analyze (Item);
7355 Item := Nxt;
7356 end;
7358 elsif Limited_Present (Item) then
7359 if not Limited_View_Installed (Item) then
7360 Remove_Limited_With_Clause (Item);
7361 end if;
7363 Next (Item);
7365 else
7366 Remove_Unit_From_Visibility (Entity (Name (Item)));
7367 Set_Context_Installed (Item, False);
7368 Next (Item);
7369 end if;
7371 else
7372 Next (Item);
7373 end if;
7374 end loop;
7375 end Remove_Private_With_Clauses;
7377 ---------------------------------
7378 -- Remove_Unit_From_Visibility --
7379 ---------------------------------
7381 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
7382 begin
7383 if Debug_Flag_I then
7384 Write_Str ("remove unit ");
7385 Write_Name (Chars (Unit_Name));
7386 Write_Str (" from visibility");
7387 Write_Eol;
7388 end if;
7390 Set_Is_Visible_Lib_Unit (Unit_Name, False);
7391 Set_Is_Potentially_Use_Visible (Unit_Name, False);
7392 Set_Is_Immediately_Visible (Unit_Name, False);
7394 -- If the unit is a wrapper package, the subprogram instance is
7395 -- what must be removed from visibility.
7396 -- Should we use Related_Instance instead???
7398 if Is_Wrapper_Package (Unit_Name) then
7399 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
7400 end if;
7401 end Remove_Unit_From_Visibility;
7403 --------
7404 -- sm --
7405 --------
7407 procedure sm is
7408 begin
7409 null;
7410 end sm;
7412 -------------
7413 -- Replace --
7414 -------------
7416 procedure Replace (Old_E, New_E : Entity_Id) is
7417 Prev : Entity_Id;
7419 begin
7420 Prev := Current_Entity (Old_E);
7422 if No (Prev) then
7423 return;
7425 elsif Prev = Old_E then
7426 Set_Current_Entity (New_E);
7427 Set_Homonym (New_E, Homonym (Old_E));
7429 else
7430 while Present (Prev) and then Homonym (Prev) /= Old_E loop
7431 Prev := Homonym (Prev);
7432 end loop;
7434 if Present (Prev) then
7435 Set_Homonym (Prev, New_E);
7436 Set_Homonym (New_E, Homonym (Old_E));
7437 end if;
7438 end if;
7439 end Replace;
7441 -------------
7442 -- Unchain --
7443 -------------
7445 procedure Unchain (E : Entity_Id) is
7446 Prev : Entity_Id;
7448 begin
7449 Prev := Current_Entity (E);
7451 if No (Prev) then
7452 return;
7454 elsif Prev = E then
7455 Set_Name_Entity_Id (Chars (E), Homonym (E));
7457 else
7458 while Present (Prev) and then Homonym (Prev) /= E loop
7459 Prev := Homonym (Prev);
7460 end loop;
7462 if Present (Prev) then
7463 Set_Homonym (Prev, Homonym (E));
7464 end if;
7465 end if;
7467 if Debug_Flag_I then
7468 Write_Str (" (homonym) unchain ");
7469 Write_Name (Chars (E));
7470 Write_Eol;
7471 end if;
7472 end Unchain;
7474 end Sem_Ch10;