fold: fix use of protected_set_expr_location_unshare
[official-gcc.git] / gcc / ada / sem_ch10.adb
bloba93808370c20e1759f42126410f01daf2f0be29e
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-2022, 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 Build_Limited_Views (N : Node_Id);
89 -- Build and decorate the list of shadow entities for a package mentioned
90 -- in a limited_with clause. If the package was not previously analyzed
91 -- then it also performs a basic decoration of the real entities. This is
92 -- required in order to avoid passing non-decorated entities to the
93 -- back-end. Implements Ada 2005 (AI-50217).
95 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
96 -- Common processing for all stubs (subprograms, tasks, packages, and
97 -- protected cases). N is the stub to be analyzed. Once the subunit name
98 -- is established, load and analyze. Nam is the non-overloadable entity
99 -- for which the proper body provides a completion. Subprogram stubs are
100 -- handled differently because they can be declarations.
102 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
103 -- Check whether the source for the body of a compilation unit must be
104 -- included in a standalone library.
106 procedure Check_No_Elab_Code_All (N : Node_Id);
107 -- Carries out possible tests for violation of No_Elab_Code all for withed
108 -- units in the Context_Items of unit N.
110 procedure Check_Private_Child_Unit (N : Node_Id);
111 -- If a with_clause mentions a private child unit, the compilation unit
112 -- must be a member of the same family, as described in 10.1.2.
114 procedure Check_Stub_Level (N : Node_Id);
115 -- Verify that a stub is declared immediately within a compilation unit,
116 -- and not in an inner frame.
118 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
119 -- When a child unit appears in a context clause, the implicit withs on
120 -- parents are made explicit, and with clauses are inserted in the context
121 -- clause before the one for the child. If a parent in the with_clause
122 -- is a renaming, the implicit with_clause is on the renaming whose name
123 -- is mentioned in the with_clause, and not on the package it renames.
124 -- N is the compilation unit whose list of context items receives the
125 -- implicit with_clauses.
127 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
128 -- Generate cross-reference information for the parents of child units
129 -- and of subunits. N is a defining_program_unit_name, and P_Id is the
130 -- immediate parent scope.
132 function Has_With_Clause
133 (C_Unit : Node_Id;
134 Pack : Entity_Id;
135 Is_Limited : Boolean := False) return Boolean;
136 -- Determine whether compilation unit C_Unit contains a [limited] with
137 -- clause for package Pack. Use the flag Is_Limited to designate desired
138 -- clause kind.
140 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
141 -- If the main unit is a child unit, implicit withs are also added for
142 -- all its ancestors.
144 function In_Chain (E : Entity_Id) return Boolean;
145 -- Check that the shadow entity is not already in the homonym chain, for
146 -- example through a limited_with clause in a parent unit.
148 procedure Install_Context_Clauses (N : Node_Id; Chain : Boolean := True);
149 -- Subsidiary to Install_Context and Install_Parents. Process all with
150 -- and use clauses for current unit and its library unit if any. The flag
151 -- Chain is used to control the "chaining" or linking together of use-type
152 -- and use-package clauses to avoid circularities with reinstalling
153 -- clauses.
155 procedure Install_Limited_Context_Clauses (N : Node_Id);
156 -- Subsidiary to Install_Context. Process only limited with_clauses for
157 -- current unit. Implements Ada 2005 (AI-50217).
159 procedure Install_Limited_With_Clause (N : Node_Id);
160 -- Place shadow entities for a limited_with package in the visibility
161 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
163 procedure Install_Parents
164 (Lib_Unit : Node_Id;
165 Is_Private : Boolean;
166 Chain : Boolean := True);
167 -- This procedure establishes the context for the compilation of a child
168 -- unit. If Lib_Unit is a child library spec then the context of the parent
169 -- is installed, and the parent itself made immediately visible, so that
170 -- the child unit is processed in the declarative region of the parent.
171 -- Install_Parents makes a recursive call to itself to ensure that all
172 -- parents are loaded in the nested case. If Lib_Unit is a library body,
173 -- the only effect of Install_Parents is to install the private decls of
174 -- the parents, because the visible parent declarations will have been
175 -- installed as part of the context of the corresponding spec. The flag
176 -- Chain is used to control the "chaining" or linking of use-type and
177 -- use-package clauses to avoid circularities when installing context.
179 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
180 -- In the compilation of a child unit, a child of any of the ancestor
181 -- units is directly visible if it is visible, because the parent is in
182 -- an enclosing scope. Iterate over context to find child units of U_Name
183 -- or of some ancestor of it.
185 procedure Install_With_Clause
186 (With_Clause : Node_Id;
187 Private_With_OK : Boolean := False);
188 -- If the unit is not a child unit, make unit immediately visible. The
189 -- caller ensures that the unit is not already currently installed. The
190 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
191 -- is called when compiling the private part of a package, or installing
192 -- the private declarations of a parent unit.
194 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
195 -- When compiling a unit Q descended from some parent unit P, a limited
196 -- with_clause in the context of P that names some other ancestor of Q
197 -- must not be installed because the ancestor is immediately visible.
199 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
200 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
201 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
202 -- a library spec that has a parent. If the call to Is_Child_Spec returns
203 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
204 -- compilation unit for the parent spec.
206 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
207 -- Parent_Spec is non-empty, this is also a child unit.
209 procedure Remove_Context_Clauses (N : Node_Id);
210 -- Subsidiary of previous one. Remove use_ and with_clauses
212 procedure Remove_Limited_With_Clause (N : Node_Id);
213 -- Remove the shadow entities from visibility introduced for a package
214 -- mentioned in limited with clause N. Implements Ada 2005 (AI-50217).
216 procedure Remove_Limited_With_Unit
217 (Pack_Decl : Node_Id;
218 Lim_Clause : Node_Id := Empty);
219 -- Remove the shadow entities from visibility introduced for a package
220 -- denoted by declaration Pack_Decl. Lim_Clause is the related limited
221 -- with clause, if any. Implements Ada 2005 (AI-50217).
223 procedure Remove_Parents (Lib_Unit : Node_Id);
224 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
225 -- contexts established by the corresponding call to Install_Parents are
226 -- removed. Remove_Parents contains a recursive call to itself to ensure
227 -- that all parents are removed in the nested case.
229 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
230 -- Reset all visibility flags on unit after compiling it, either as a main
231 -- unit or as a unit in the context.
233 procedure Unchain (E : Entity_Id);
234 -- Remove single entity from visibility list
236 procedure sm;
237 -- A dummy procedure, for debugging use, called just before analyzing the
238 -- main unit (after dealing with any context clauses).
240 --------------------------
241 -- Limited_With_Clauses --
242 --------------------------
244 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
245 -- mutually recursive types declared in different units. A limited_with
246 -- clause that names package P in the context of unit U makes the types
247 -- declared in the visible part of P available within U, but with the
248 -- restriction that these types can only be used as incomplete types.
249 -- The limited_with clause does not impose a semantic dependence on P,
250 -- and it is possible for two packages to have limited_with_clauses on
251 -- each other without creating an elaboration circularity.
253 -- To support this feature, the analysis of a limited_with clause must
254 -- create an abbreviated view of the package, without performing any
255 -- semantic analysis on it. This "package abstract" contains shadow types
256 -- that are in one-one correspondence with the real types in the package,
257 -- and that have the properties of incomplete types.
259 -- The implementation creates two element lists: one to chain the shadow
260 -- entities, and one to chain the corresponding type entities in the tree
261 -- of the package. Links between corresponding entities in both chains
262 -- allow the compiler to select the proper view of a given type, depending
263 -- on the context. Note that in contrast with the handling of private
264 -- types, the limited view and the nonlimited view of a type are treated
265 -- as separate entities, and no entity exchange needs to take place, which
266 -- makes the implementation much simpler than could be feared.
268 ------------------------------
269 -- Analyze_Compilation_Unit --
270 ------------------------------
272 procedure Analyze_Compilation_Unit (N : Node_Id) is
273 Unit_Node : constant Node_Id := Unit (N);
275 procedure Check_Redundant_Withs
276 (Context_Items : List_Id;
277 Spec_Context_Items : List_Id := No_List);
278 -- Determine whether the context list of a compilation unit contains
279 -- redundant with clauses. When checking body clauses against spec
280 -- clauses, set Context_Items to the context list of the body and
281 -- Spec_Context_Items to that of the spec. Parent packages are not
282 -- examined for documentation purposes.
284 ---------------------------
285 -- Check_Redundant_Withs --
286 ---------------------------
288 procedure Check_Redundant_Withs
289 (Context_Items : List_Id;
290 Spec_Context_Items : List_Id := No_List)
292 Clause : Node_Id;
294 procedure Process_Body_Clauses
295 (Context_List : List_Id;
296 Clause : Node_Id;
297 Used : out Boolean;
298 Used_Type_Or_Elab : out Boolean);
299 -- Examine the context clauses of a package body, trying to match the
300 -- name entity of Clause with any list element. If the match occurs
301 -- on a use package clause set Used to True, for a use type clause or
302 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
304 procedure Process_Spec_Clauses
305 (Context_List : List_Id;
306 Clause : Node_Id;
307 Used : out Boolean;
308 Withed : out Boolean;
309 Exit_On_Self : Boolean := False);
310 -- Examine the context clauses of a package spec, trying to match
311 -- the name entity of Clause with any list element. If the match
312 -- occurs on a use package clause, set Used to True, for a with
313 -- package clause other than Clause, set Withed to True. Limited
314 -- with clauses, implicitly generated with clauses and withs
315 -- having pragmas Elaborate or Elaborate_All applied to them are
316 -- skipped. Exit_On_Self is used to control the search loop and
317 -- force an exit whenever Clause sees itself in the search.
319 --------------------------
320 -- Process_Body_Clauses --
321 --------------------------
323 procedure Process_Body_Clauses
324 (Context_List : List_Id;
325 Clause : Node_Id;
326 Used : out Boolean;
327 Used_Type_Or_Elab : out Boolean)
329 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
330 Cont_Item : Node_Id;
331 Prag_Unit : Node_Id;
332 Use_Item : Node_Id;
334 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
335 -- In an expanded name in a use clause, if the prefix is a renamed
336 -- package, the entity is set to the original package as a result,
337 -- when checking whether the package appears in a previous with
338 -- clause, the renaming has to be taken into account, to prevent
339 -- spurious/incorrect warnings. A common case is use of Text_IO.
341 ---------------
342 -- Same_Unit --
343 ---------------
345 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
346 begin
347 return Entity (N) = P
348 or else (Present (Renamed_Entity (P))
349 and then Entity (N) = Renamed_Entity (P));
350 end Same_Unit;
352 -- Start of processing for Process_Body_Clauses
354 begin
355 Used := False;
356 Used_Type_Or_Elab := False;
358 Cont_Item := First (Context_List);
359 while Present (Cont_Item) loop
361 -- Package use clause
363 if Nkind (Cont_Item) = N_Use_Package_Clause
364 and then not Used
365 then
366 -- Search through use clauses
368 Use_Item := Name (Cont_Item);
370 -- Case of a direct use of the one we are looking for
372 if Entity (Use_Item) = Nam_Ent then
373 Used := True;
375 -- Handle nested case, as in "with P; use P.Q.R"
377 else
378 declare
379 UE : Node_Id;
381 begin
382 -- Loop through prefixes looking for match
384 UE := Use_Item;
385 while Nkind (UE) = N_Expanded_Name loop
386 if Same_Unit (Prefix (UE), Nam_Ent) then
387 Used := True;
388 exit;
389 end if;
391 UE := Prefix (UE);
392 end loop;
393 end;
394 end if;
396 -- USE TYPE clause
398 elsif Nkind (Cont_Item) = N_Use_Type_Clause
399 and then not Used_Type_Or_Elab
400 then
401 declare
402 UE : Node_Id;
404 begin
405 -- Loop through prefixes looking for a match
407 UE := Prefix (Subtype_Mark (Cont_Item));
408 loop
409 if not Used_Type_Or_Elab
410 and then Same_Unit (UE, Nam_Ent)
411 then
412 Used_Type_Or_Elab := True;
413 end if;
415 exit when Nkind (UE) /= N_Expanded_Name;
416 UE := Prefix (UE);
417 end loop;
418 end;
420 -- Pragma Elaborate or Elaborate_All
422 elsif Nkind (Cont_Item) = N_Pragma
423 and then
424 Pragma_Name_Unmapped (Cont_Item)
425 in Name_Elaborate | Name_Elaborate_All
426 and then not Used_Type_Or_Elab
427 then
428 Prag_Unit :=
429 First (Pragma_Argument_Associations (Cont_Item));
430 while Present (Prag_Unit) and then not Used_Type_Or_Elab loop
431 if Entity (Expression (Prag_Unit)) = Nam_Ent then
432 Used_Type_Or_Elab := True;
433 end if;
435 Next (Prag_Unit);
436 end loop;
437 end if;
439 Next (Cont_Item);
440 end loop;
441 end Process_Body_Clauses;
443 --------------------------
444 -- Process_Spec_Clauses --
445 --------------------------
447 procedure Process_Spec_Clauses
448 (Context_List : List_Id;
449 Clause : Node_Id;
450 Used : out Boolean;
451 Withed : out Boolean;
452 Exit_On_Self : Boolean := False)
454 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
455 Cont_Item : Node_Id;
457 begin
458 Used := False;
459 Withed := False;
461 Cont_Item := First (Context_List);
462 while Present (Cont_Item) loop
464 -- Stop the search since the context items after Cont_Item have
465 -- already been examined in a previous iteration of the reverse
466 -- loop in Check_Redundant_Withs.
468 if Exit_On_Self
469 and Cont_Item = Clause
470 then
471 exit;
472 end if;
474 -- Package use clause
476 if Nkind (Cont_Item) = N_Use_Package_Clause
477 and then not Used
478 then
479 if Entity (Name (Cont_Item)) = Nam_Ent then
480 Used := True;
481 end if;
483 -- Package with clause. Avoid processing self, implicitly
484 -- generated with clauses or limited with clauses. Note that
485 -- we examine with clauses having pragmas Elaborate or
486 -- Elaborate_All applied to them due to cases such as:
488 -- with Pack;
489 -- with Pack;
490 -- pragma Elaborate (Pack);
492 -- In this case, the second with clause is redundant since
493 -- the pragma applies only to the first "with Pack;".
495 -- Note that we only consider with_clauses that comes from
496 -- source. In the case of renamings used as prefixes of names
497 -- in with_clauses, we generate a with_clause for the prefix,
498 -- which we do not treat as implicit because it is needed for
499 -- visibility analysis, but is also not redundant.
501 elsif Nkind (Cont_Item) = N_With_Clause
502 and then Comes_From_Source (Cont_Item)
503 and then not Implicit_With (Cont_Item)
504 and then not Limited_Present (Cont_Item)
505 and then Cont_Item /= Clause
506 and then Entity (Name (Cont_Item)) = Nam_Ent
507 then
508 Withed := True;
509 end if;
511 Next (Cont_Item);
512 end loop;
513 end Process_Spec_Clauses;
515 -- Start of processing for Check_Redundant_Withs
517 begin
518 Clause := Last (Context_Items);
519 while Present (Clause) loop
521 -- Avoid checking implicitly generated with clauses, limited with
522 -- clauses or withs that have pragma Elaborate or Elaborate_All.
524 if Nkind (Clause) = N_With_Clause
525 and then not Implicit_With (Clause)
526 and then not Limited_Present (Clause)
527 and then not Elaborate_Present (Clause)
529 -- With_clauses introduced for renamings of parent clauses
530 -- are not marked implicit because they need to be properly
531 -- installed, but they do not come from source and do not
532 -- require warnings.
534 and then Comes_From_Source (Clause)
535 then
536 -- Package body-to-spec check
538 if Present (Spec_Context_Items) then
539 declare
540 Used_In_Body : Boolean;
541 Used_In_Spec : Boolean;
542 Used_Type_Or_Elab : Boolean;
543 Withed_In_Spec : Boolean;
545 begin
546 Process_Spec_Clauses
547 (Context_List => Spec_Context_Items,
548 Clause => Clause,
549 Used => Used_In_Spec,
550 Withed => Withed_In_Spec);
552 Process_Body_Clauses
553 (Context_List => Context_Items,
554 Clause => Clause,
555 Used => Used_In_Body,
556 Used_Type_Or_Elab => Used_Type_Or_Elab);
558 -- "Type Elab" refers to the presence of either a use
559 -- type clause, pragmas Elaborate or Elaborate_All.
561 -- +---------------+---------------------------+------+
562 -- | Spec | Body | Warn |
563 -- +--------+------+--------+------+-----------+------+
564 -- | Withed | Used | Withed | Used | Type Elab | |
565 -- | X | | X | | | X |
566 -- | X | | X | X | | |
567 -- | X | | X | | X | |
568 -- | X | | X | X | X | |
569 -- | X | X | X | | | X |
570 -- | X | X | X | | X | |
571 -- | X | X | X | X | | X |
572 -- | X | X | X | X | X | |
573 -- +--------+------+--------+------+-----------+------+
575 if (Withed_In_Spec
576 and then not Used_Type_Or_Elab)
577 and then
578 ((not Used_In_Spec and then not Used_In_Body)
579 or else Used_In_Spec)
580 then
581 Error_Msg_N -- CODEFIX
582 ("redundant with clause in body?r?", Clause);
583 end if;
584 end;
586 -- Standalone package spec or body check
588 else
589 if Is_Ancestor_Package (Entity (Name (Clause)),
590 Defining_Entity (Unit_Node))
591 then
592 Error_Msg_N
593 ("unnecessary with of ancestor?r?", Clause);
594 end if;
596 declare
597 Dummy : Boolean := False;
598 Withed : Boolean := False;
600 begin
601 -- The mechanism for examining the context clauses of a
602 -- package spec can be applied to package body clauses.
604 Process_Spec_Clauses
605 (Context_List => Context_Items,
606 Clause => Clause,
607 Used => Dummy,
608 Withed => Withed,
609 Exit_On_Self => True);
611 if Withed then
612 Error_Msg_N -- CODEFIX
613 ("redundant with clause?r?", Clause);
614 end if;
615 end;
616 end if;
617 end if;
619 Prev (Clause);
620 end loop;
621 end Check_Redundant_Withs;
623 -- Local variables
625 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
626 Lib_Unit : Node_Id := Library_Unit (N);
627 Par_Spec_Name : Unit_Name_Type;
628 Spec_Id : Entity_Id;
629 Unum : Unit_Number_Type;
631 -- Start of processing for Analyze_Compilation_Unit
633 begin
634 Exp_Put_Image.Preload_Root_Buffer_Type (N);
636 Process_Compilation_Unit_Pragmas (N);
638 -- If the unit is a subunit whose parent has not been analyzed (which
639 -- indicates that the main unit is a subunit, either the current one or
640 -- one of its descendants) then the subunit is compiled as part of the
641 -- analysis of the parent, which we proceed to do. Basically this gets
642 -- handled from the top down and we don't want to do anything at this
643 -- level (i.e. this subunit will be handled on the way down from the
644 -- parent), so at this level we immediately return. If the subunit ends
645 -- up not analyzed, it means that the parent did not contain a stub for
646 -- it, or that there errors were detected in some ancestor.
648 if Nkind (Unit_Node) = N_Subunit and then not Analyzed (Lib_Unit) then
649 Semantics (Lib_Unit);
651 if not Analyzed (Proper_Body (Unit_Node)) then
652 if Serious_Errors_Detected > 0 then
653 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
654 else
655 Error_Msg_N ("missing stub for subunit", N);
656 end if;
657 end if;
659 return;
660 end if;
662 -- Analyze context (this will call Sem recursively for with'ed units) To
663 -- detect circularities among with-clauses that are not caught during
664 -- loading, we set the Context_Pending flag on the current unit. If the
665 -- flag is already set there is a potential circularity. We exclude
666 -- predefined units from this check because they are known to be safe.
667 -- We also exclude package bodies that are present because circularities
668 -- between bodies are harmless (and necessary).
670 if Context_Pending (N) then
671 declare
672 Circularity : Boolean := True;
674 begin
675 if In_Predefined_Unit (N) then
676 Circularity := False;
678 else
679 for U in Main_Unit + 1 .. Last_Unit loop
680 if Nkind (Unit (Cunit (U))) = N_Package_Body
681 and then not Analyzed (Cunit (U))
682 then
683 Circularity := False;
684 exit;
685 end if;
686 end loop;
687 end if;
689 if Circularity then
690 Error_Msg_N ("circular dependency caused by with_clauses", N);
691 Error_Msg_N
692 ("\possibly missing limited_with clause"
693 & " in one of the following", N);
695 for U in Main_Unit .. Last_Unit loop
696 if Context_Pending (Cunit (U)) then
697 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
698 Error_Msg_N ("\unit$", N);
699 end if;
700 end loop;
702 raise Unrecoverable_Error;
703 end if;
704 end;
705 else
706 Set_Context_Pending (N);
707 end if;
709 Analyze_Context (N);
711 Set_Context_Pending (N, False);
713 -- If the unit is a package body, the spec is already loaded and must be
714 -- analyzed first, before we analyze the body.
716 if Nkind (Unit_Node) = N_Package_Body then
718 -- If no Lib_Unit, then there was a serious previous error, so just
719 -- ignore the entire analysis effort.
721 if No (Lib_Unit) then
722 Check_Error_Detected;
723 return;
725 else
726 -- Analyze the package spec
728 Semantics (Lib_Unit);
730 -- Check for unused with's
732 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
734 -- Verify that the library unit is a package declaration
736 if Nkind (Unit (Lib_Unit)) not in
737 N_Package_Declaration | N_Generic_Package_Declaration
738 then
739 Error_Msg_N
740 ("no legal package declaration for package body", N);
741 return;
743 -- Otherwise, the entity in the declaration is visible. Update the
744 -- version to reflect dependence of this body on the spec.
746 else
747 Spec_Id := Defining_Entity (Unit (Lib_Unit));
748 Set_Is_Immediately_Visible (Spec_Id, True);
749 Version_Update (N, Lib_Unit);
751 if Nkind (Defining_Unit_Name (Unit_Node)) =
752 N_Defining_Program_Unit_Name
753 then
754 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
755 end if;
756 end if;
757 end if;
759 -- If the unit is a subprogram body, then we similarly need to analyze
760 -- its spec. However, things are a little simpler in this case, because
761 -- here, this analysis is done mostly for error checking and consistency
762 -- purposes (but not only, e.g. there could be a contract on the spec),
763 -- so there's nothing else to be done.
765 elsif Nkind (Unit_Node) = N_Subprogram_Body then
766 if Acts_As_Spec (N) then
768 -- If the subprogram body is a child unit, we must create a
769 -- declaration for it, in order to properly load the parent(s).
770 -- After this, the original unit does not acts as a spec, because
771 -- there is an explicit one. If this unit appears in a context
772 -- clause, then an implicit with on the parent will be added when
773 -- installing the context. If this is the main unit, there is no
774 -- Unit_Table entry for the declaration (it has the unit number
775 -- of the main unit) and code generation is unaffected.
777 Unum := Get_Cunit_Unit_Number (N);
778 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
780 if Present (Par_Spec_Name) then
781 Unum :=
782 Load_Unit
783 (Load_Name => Par_Spec_Name,
784 Required => True,
785 Subunit => False,
786 Error_Node => N);
788 if Unum /= No_Unit then
790 -- Build subprogram declaration and attach parent unit to it
791 -- This subprogram declaration does not come from source,
792 -- Nevertheless the backend must generate debugging info for
793 -- it, and this must be indicated explicitly. We also mark
794 -- the body entity as a child unit now, to prevent a
795 -- cascaded error if the spec entity cannot be entered
796 -- in its scope. Finally we create a Units table entry for
797 -- the subprogram declaration, to maintain a one-to-one
798 -- correspondence with compilation unit nodes. This is
799 -- critical for the tree traversals performed by CodePeer.
801 declare
802 Loc : constant Source_Ptr := Sloc (N);
803 SCS : constant Boolean :=
804 Get_Comes_From_Source_Default;
806 begin
807 Set_Comes_From_Source_Default (False);
809 -- Note: We copy the Context_Items from the explicit body
810 -- to the implicit spec, setting the former to Empty_List
811 -- to preserve the treeish nature of the tree, during
812 -- analysis of the spec. Then we put it back the way it
813 -- was -- copy the Context_Items from the spec to the
814 -- body, and set the spec Context_Items to Empty_List.
815 -- It is necessary to preserve the treeish nature,
816 -- because otherwise we will call End_Use_* twice on the
817 -- same thing.
819 Lib_Unit :=
820 Make_Compilation_Unit (Loc,
821 Context_Items => Context_Items (N),
822 Unit =>
823 Make_Subprogram_Declaration (Sloc (N),
824 Specification =>
825 Copy_Separate_Tree
826 (Specification (Unit_Node))),
827 Aux_Decls_Node =>
828 Make_Compilation_Unit_Aux (Loc));
830 Set_Context_Items (N, Empty_List);
831 Set_Library_Unit (N, Lib_Unit);
832 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
833 Make_Child_Decl_Unit (N);
834 Semantics (Lib_Unit);
836 -- Now that a separate declaration exists, the body
837 -- of the child unit does not act as spec any longer.
839 Set_Acts_As_Spec (N, False);
840 Move_Aspects (From => Unit_Node, To => Unit (Lib_Unit));
841 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
842 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
843 Set_Comes_From_Source_Default (SCS);
845 -- Restore Context_Items to the body
847 Set_Context_Items (N, Context_Items (Lib_Unit));
848 Set_Context_Items (Lib_Unit, Empty_List);
849 end;
850 end if;
851 end if;
853 -- Here for subprogram with separate declaration
855 else
856 Semantics (Lib_Unit);
857 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
858 Version_Update (N, Lib_Unit);
859 end if;
861 -- If this is a child unit, generate references to the parents
863 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
864 N_Defining_Program_Unit_Name
865 then
866 Generate_Parent_References
867 (Specification (Unit_Node),
868 Scope (Defining_Entity (Unit (Lib_Unit))));
869 end if;
870 end if;
872 -- If it is a child unit, the parent must be elaborated first and we
873 -- update version, since we are dependent on our parent.
875 if Is_Child_Spec (Unit_Node) then
877 -- The analysis of the parent is done with style checks off
879 declare
880 Save_Style_Check : constant Boolean := Style_Check;
882 begin
883 if not GNAT_Mode then
884 Style_Check := False;
885 end if;
887 Semantics (Parent_Spec (Unit_Node));
888 Version_Update (N, Parent_Spec (Unit_Node));
890 -- Restore style check settings
892 Style_Check := Save_Style_Check;
893 end;
894 end if;
896 -- With the analysis done, install the context. Note that we can't
897 -- install the context from the with clauses as we analyze them, because
898 -- each with clause must be analyzed in a clean visibility context, so
899 -- we have to wait and install them all at once.
901 Install_Context (N);
903 if Is_Child_Spec (Unit_Node) then
905 -- Set the entities of all parents in the program_unit_name
907 Generate_Parent_References
908 (Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
909 end if;
911 -- All components of the context: with-clauses, library unit, ancestors
912 -- if any, (and their context) are analyzed and installed.
914 -- Call special debug routine sm if this is the main unit
916 if Current_Sem_Unit = Main_Unit then
918 end if;
920 -- Now analyze the unit (package, subprogram spec, body) itself
922 Analyze (Unit_Node);
924 if Warn_On_Redundant_Constructs then
925 Check_Redundant_Withs (Context_Items (N));
927 if Nkind (Unit_Node) = N_Package_Body then
928 Check_Redundant_Withs
929 (Context_Items => Context_Items (N),
930 Spec_Context_Items => Context_Items (Lib_Unit));
931 end if;
932 end if;
934 -- The above call might have made Unit_Node an N_Subprogram_Body from
935 -- something else, so propagate any Acts_As_Spec flag.
937 if Nkind (Unit_Node) = N_Subprogram_Body
938 and then Acts_As_Spec (Unit_Node)
939 then
940 Set_Acts_As_Spec (N);
941 end if;
943 -- Register predefined units in Rtsfind
945 if In_Predefined_Unit (N) then
946 Set_RTU_Loaded (Unit_Node);
947 end if;
949 -- Treat compilation unit pragmas that appear after the library unit
951 declare
952 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
953 begin
954 while Present (Prag_Node) loop
955 Analyze (Prag_Node);
956 Next (Prag_Node);
957 end loop;
958 end;
960 -- Analyze the contract of a [generic] subprogram that acts as a
961 -- compilation unit after all compilation pragmas have been analyzed.
963 if Nkind (Unit_Node) in
964 N_Generic_Subprogram_Declaration | N_Subprogram_Declaration
965 then
966 Analyze_Entry_Or_Subprogram_Contract (Defining_Entity (Unit_Node));
967 end if;
969 -- Generate distribution stubs if requested and no error
971 if N = Main_Cunit
972 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
973 or else
974 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
975 and then Fatal_Error (Main_Unit) /= Error_Detected
976 then
977 if Is_RCI_Pkg_Spec_Or_Body (N) then
979 -- Regular RCI package
981 Add_Stub_Constructs (N);
983 elsif (Nkind (Unit_Node) = N_Package_Declaration
984 and then Is_Shared_Passive (Defining_Entity
985 (Specification (Unit_Node))))
986 or else (Nkind (Unit_Node) = N_Package_Body
987 and then
988 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
989 then
990 -- Shared passive package
992 Add_Stub_Constructs (N);
994 elsif Nkind (Unit_Node) = N_Package_Instantiation
995 and then
996 Is_Remote_Call_Interface
997 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
998 then
999 -- Instantiation of a RCI generic package
1001 Add_Stub_Constructs (N);
1002 end if;
1003 end if;
1005 -- Build dispatch tables of library-level tagged types only now because
1006 -- the generation of distribution stubs above may create some of them.
1008 if Expander_Active and then Tagged_Type_Expansion then
1009 case Nkind (Unit_Node) is
1010 when N_Package_Declaration | N_Package_Body =>
1011 Build_Static_Dispatch_Tables (Unit_Node);
1013 when N_Package_Instantiation =>
1014 Build_Static_Dispatch_Tables (Instance_Spec (Unit_Node));
1016 when others =>
1017 null;
1018 end case;
1019 end if;
1021 -- Remove unit from visibility, so that environment is clean for the
1022 -- next compilation, which is either the main unit or some other unit
1023 -- in the context.
1025 if Nkind (Unit_Node) in N_Package_Declaration
1026 | N_Package_Renaming_Declaration
1027 | N_Subprogram_Declaration
1028 | N_Generic_Declaration
1029 or else (Nkind (Unit_Node) = N_Subprogram_Body
1030 and then Acts_As_Spec (Unit_Node))
1031 then
1032 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
1034 -- If the unit is an instantiation whose body will be elaborated for
1035 -- inlining purposes, use the proper entity of the instance. The entity
1036 -- may be missing if the instantiation was illegal.
1038 elsif Nkind (Unit_Node) = N_Package_Instantiation
1039 and then not Error_Posted (Unit_Node)
1040 and then Present (Instance_Spec (Unit_Node))
1041 then
1042 Remove_Unit_From_Visibility
1043 (Defining_Entity (Instance_Spec (Unit_Node)));
1045 elsif Nkind (Unit_Node) = N_Package_Body
1046 or else (Nkind (Unit_Node) = N_Subprogram_Body
1047 and then not Acts_As_Spec (Unit_Node))
1048 then
1049 -- Bodies that are not the main unit are compiled if they are generic
1050 -- or contain generic or inlined units. Their analysis brings in the
1051 -- context of the corresponding spec (unit declaration) which must be
1052 -- removed as well, to return the compilation environment to its
1053 -- proper state.
1055 Remove_Context (Lib_Unit);
1056 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1057 end if;
1059 -- Last step is to deinstall the context we just installed as well as
1060 -- the unit just compiled.
1062 Remove_Context (N);
1064 -- When generating code for a non-generic main unit, check that withed
1065 -- generic units have a body if they need it, even if the units have not
1066 -- been instantiated. Force the load of the bodies to produce the proper
1067 -- error if the body is absent. The same applies to GNATprove mode, with
1068 -- the added benefit of capturing global references within the generic.
1069 -- This in turn allows for proper inlining of subprogram bodies without
1070 -- a previous declaration.
1072 if Get_Cunit_Unit_Number (N) = Main_Unit
1073 and then ((Operating_Mode = Generate_Code and then Expander_Active)
1074 or else
1075 (Operating_Mode = Check_Semantics and then GNATprove_Mode))
1076 then
1077 -- Check whether the source for the body of the unit must be included
1078 -- in a standalone library.
1080 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1082 -- Indicate that the main unit is now analyzed, to catch possible
1083 -- circularities between it and generic bodies. Remove main unit from
1084 -- visibility. This might seem superfluous, but the main unit must
1085 -- not be visible in the generic body expansions that follow.
1087 Set_Analyzed (N, True);
1088 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1090 declare
1091 Item : Node_Id;
1092 Nam : Entity_Id;
1093 Un : Unit_Number_Type;
1095 Save_Style_Check : constant Boolean := Style_Check;
1097 begin
1098 Item := First (Context_Items (N));
1099 while Present (Item) loop
1101 -- Check for explicit with clause
1103 if Nkind (Item) = N_With_Clause
1104 and then not Implicit_With (Item)
1106 -- Ada 2005 (AI-50217): Ignore limited-withed units
1108 and then not Limited_Present (Item)
1109 then
1110 Nam := Entity (Name (Item));
1112 -- Compile the generic subprogram, unless it is intrinsic or
1113 -- imported so no body is required, or generic package body
1114 -- if the package spec requires a body.
1116 if (Is_Generic_Subprogram (Nam)
1117 and then not Is_Intrinsic_Subprogram (Nam)
1118 and then not Is_Imported (Nam))
1119 or else (Ekind (Nam) = E_Generic_Package
1120 and then Unit_Requires_Body (Nam))
1121 then
1122 Style_Check := False;
1124 if Present (Renamed_Entity (Nam)) then
1125 Un :=
1126 Load_Unit
1127 (Load_Name =>
1128 Get_Body_Name
1129 (Get_Unit_Name
1130 (Unit_Declaration_Node
1131 (Renamed_Entity (Nam)))),
1132 Required => False,
1133 Subunit => False,
1134 Error_Node => N,
1135 Renamings => True);
1136 else
1137 Un :=
1138 Load_Unit
1139 (Load_Name =>
1140 Get_Body_Name (Get_Unit_Name (Item)),
1141 Required => False,
1142 Subunit => False,
1143 Error_Node => N,
1144 Renamings => True);
1145 end if;
1147 if Un = No_Unit then
1148 Error_Msg_NE
1149 ("body of generic unit& not found", Item, Nam);
1150 exit;
1152 elsif not Analyzed (Cunit (Un))
1153 and then Un /= Main_Unit
1154 and then Fatal_Error (Un) /= Error_Detected
1155 then
1156 Style_Check := False;
1157 Semantics (Cunit (Un));
1158 end if;
1159 end if;
1160 end if;
1162 Next (Item);
1163 end loop;
1165 -- Restore style checks settings
1167 Style_Check := Save_Style_Check;
1168 end;
1170 -- In GNATprove mode, force the loading of an Interrupt_Priority when
1171 -- processing compilation units with potentially "main" subprograms.
1172 -- This is required for the ceiling priority protocol checks, which
1173 -- are triggered by these subprograms.
1175 if GNATprove_Mode
1176 and then Nkind (Unit_Node) in N_Function_Instantiation
1177 | N_Procedure_Instantiation
1178 | N_Subprogram_Body
1179 then
1180 declare
1181 Spec : Node_Id;
1183 begin
1184 case Nkind (Unit_Node) is
1185 when N_Subprogram_Body =>
1186 Spec := Specification (Unit_Node);
1188 when N_Subprogram_Instantiation =>
1189 Spec :=
1190 Subprogram_Specification (Entity (Name (Unit_Node)));
1192 when others =>
1193 raise Program_Error;
1194 end case;
1196 pragma Assert (Nkind (Spec) in N_Subprogram_Specification);
1198 -- Main subprogram must have no parameters, and if it is a
1199 -- function, it must return an integer.
1201 if No (Parameter_Specifications (Spec))
1202 and then (Nkind (Spec) = N_Procedure_Specification
1203 or else
1204 Is_Integer_Type (Etype (Result_Definition (Spec))))
1205 then
1206 SPARK_Implicit_Load (RE_Interrupt_Priority);
1207 end if;
1208 end;
1209 end if;
1210 end if;
1212 -- Deal with creating elaboration counter if needed. We create an
1213 -- elaboration counter only for units that come from source since
1214 -- units manufactured by the compiler never need elab checks.
1216 if Comes_From_Source (N)
1217 and then Nkind (Unit_Node) in N_Package_Declaration
1218 | N_Generic_Package_Declaration
1219 | N_Subprogram_Declaration
1220 | N_Generic_Subprogram_Declaration
1221 then
1222 declare
1223 Loc : constant Source_Ptr := Sloc (N);
1224 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1226 begin
1227 Spec_Id := Defining_Entity (Unit_Node);
1228 Generate_Definition (Spec_Id);
1230 -- See if an elaboration entity is required for possible access
1231 -- before elaboration checking. Note that we must allow for this
1232 -- even if -gnatE is not set, since a client may be compiled in
1233 -- -gnatE mode and reference the entity.
1235 -- These entities are also used by the binder to prevent multiple
1236 -- attempts to execute the elaboration code for the library case
1237 -- where the elaboration routine might otherwise be called more
1238 -- than once.
1240 -- They are also needed to ensure explicit visibility from the
1241 -- binder generated code of all the units involved in a partition
1242 -- when control-flow preservation is requested.
1244 if not Opt.Suppress_Control_Flow_Optimizations
1245 and then
1246 ( -- Pure units do not need checks
1248 Is_Pure (Spec_Id)
1250 -- Preelaborated units do not need checks
1252 or else Is_Preelaborated (Spec_Id)
1254 -- No checks needed if pragma Elaborate_Body present
1256 or else Has_Pragma_Elaborate_Body (Spec_Id)
1258 -- No checks needed if unit does not require a body
1260 or else not Unit_Requires_Body (Spec_Id)
1262 -- No checks needed for predefined files
1264 or else Is_Predefined_Unit (Unum)
1266 -- No checks required if no separate spec
1268 or else Acts_As_Spec (N)
1270 then
1271 -- This is a case where we only need the entity for checking to
1272 -- prevent multiple elaboration checks.
1274 Set_Elaboration_Entity_Required (Spec_Id, False);
1276 -- Otherwise the unit requires an elaboration entity because it
1277 -- carries a body.
1279 else
1280 Set_Elaboration_Entity_Required (Spec_Id);
1281 end if;
1283 Build_Elaboration_Entity (N, Spec_Id);
1284 end;
1285 end if;
1287 -- Freeze the compilation unit entity. This for sure is needed because
1288 -- of some warnings that can be output (see Freeze_Subprogram), but may
1289 -- in general be required. If freezing actions result, place them in the
1290 -- compilation unit actions list, and analyze them.
1292 declare
1293 L : constant List_Id :=
1294 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1295 begin
1296 while Is_Non_Empty_List (L) loop
1297 Insert_Library_Level_Action (Remove_Head (L));
1298 end loop;
1299 end;
1301 Set_Analyzed (N);
1303 -- Call Check_Package_Body so that a body containing subprograms with
1304 -- Inline_Always can be made available for front end inlining.
1306 if Nkind (Unit_Node) = N_Package_Declaration
1307 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1309 -- We don't need to do this if the Expander is not active, since there
1310 -- is no code to inline.
1312 and then Expander_Active
1313 then
1314 declare
1315 Save_Style_Check : constant Boolean := Style_Check;
1316 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1317 Options : Style_Check_Options;
1319 begin
1320 Save_Style_Check_Options (Options);
1321 Reset_Style_Check_Options;
1322 Opt.Warning_Mode := Suppress;
1324 Check_Package_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1326 Reset_Style_Check_Options;
1327 Set_Style_Check_Options (Options);
1328 Style_Check := Save_Style_Check;
1329 Warning_Mode := Save_Warning;
1330 end;
1331 end if;
1333 -- If we are generating obsolescent warnings, then here is where we
1334 -- generate them for the with'ed items. The reason for this special
1335 -- processing is that the normal mechanism of generating the warnings
1336 -- for referenced entities does not work for context clause references.
1337 -- That's because when we first analyze the context, it is too early to
1338 -- know if the with'ing unit is itself obsolescent (which suppresses
1339 -- the warnings).
1341 if not GNAT_Mode
1342 and then Warn_On_Obsolescent_Feature
1343 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1344 then
1345 -- Push current compilation unit as scope, so that the test for
1346 -- being within an obsolescent unit will work correctly. The check
1347 -- is not performed within an instantiation, because the warning
1348 -- will have been emitted in the corresponding generic unit.
1350 Push_Scope (Defining_Entity (Unit_Node));
1352 -- Loop through context items to deal with with clauses
1354 declare
1355 Item : Node_Id;
1356 Nam : Node_Id;
1357 Ent : Entity_Id;
1359 begin
1360 Item := First (Context_Items (N));
1361 while Present (Item) loop
1362 if Nkind (Item) = N_With_Clause
1364 -- Suppress this check in limited-withed units. Further work
1365 -- needed here if we decide to incorporate this check on
1366 -- limited-withed units.
1368 and then not Limited_Present (Item)
1369 then
1370 Nam := Name (Item);
1371 Ent := Entity (Nam);
1373 if Is_Obsolescent (Ent) then
1374 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1375 end if;
1376 end if;
1378 Next (Item);
1379 end loop;
1380 end;
1382 -- Remove temporary install of current unit as scope
1384 Pop_Scope;
1385 end if;
1387 -- If No_Elaboration_Code_All was encountered, this is where we do the
1388 -- transitive test of with'ed units to make sure they have the aspect.
1389 -- This is delayed till the end of analyzing the compilation unit to
1390 -- ensure that the pragma/aspect, if present, has been analyzed.
1392 Check_No_Elab_Code_All (N);
1393 end Analyze_Compilation_Unit;
1395 ---------------------
1396 -- Analyze_Context --
1397 ---------------------
1399 procedure Analyze_Context (N : Node_Id) is
1400 Ukind : constant Node_Kind := Nkind (Unit (N));
1401 Item : Node_Id;
1403 begin
1404 -- First process all configuration pragmas at the start of the context
1405 -- items. Strictly these are not part of the context clause, but that
1406 -- is where the parser puts them. In any case for sure we must analyze
1407 -- these before analyzing the actual context items, since they can have
1408 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1409 -- be with'ed as a result of changing categorizations in Ada 2005).
1411 Item := First (Context_Items (N));
1412 while Present (Item)
1413 and then Nkind (Item) = N_Pragma
1414 and then Pragma_Name (Item) in Configuration_Pragma_Names
1415 loop
1416 Analyze (Item);
1417 Next (Item);
1418 end loop;
1420 -- This is the point at which we capture the configuration settings
1421 -- for the unit. At the moment only the Optimize_Alignment setting
1422 -- needs to be captured. Probably more later ???
1424 if Optimize_Alignment_Local then
1425 Set_OA_Setting (Current_Sem_Unit, 'L');
1426 else
1427 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1428 end if;
1430 -- Loop through actual context items. This is done in two passes:
1432 -- a) The first pass analyzes nonlimited with clauses and also any
1433 -- configuration pragmas (we need to get the latter analyzed right
1434 -- away, since they can affect processing of subsequent items).
1436 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1438 while Present (Item) loop
1440 -- For with clause, analyze the with clause, and then update the
1441 -- version, since we are dependent on a unit that we with.
1443 if Nkind (Item) = N_With_Clause
1444 and then not Limited_Present (Item)
1445 then
1446 -- Skip analyzing with clause if no unit, nothing to do (this
1447 -- happens for a with that references a non-existent unit).
1449 if Present (Library_Unit (Item)) then
1451 -- Skip analyzing with clause if this is a with_clause for
1452 -- the main unit, which happens if a subunit has a useless
1453 -- with_clause on its parent.
1455 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1456 Analyze (Item);
1458 -- Here for the case of a useless with for the main unit
1460 else
1461 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1462 end if;
1463 end if;
1465 -- Do version update (skipped for implicit with)
1467 if not Implicit_With (Item) then
1468 Version_Update (N, Library_Unit (Item));
1469 end if;
1471 -- Skip pragmas. Configuration pragmas at the start were handled in
1472 -- the loop above, and remaining pragmas are not processed until we
1473 -- actually install the context (see Install_Context). We delay the
1474 -- analysis of these pragmas to make sure that we have installed all
1475 -- the implicit with's on parent units.
1477 -- Skip use clauses at this stage, since we don't want to do any
1478 -- installing of potentially use-visible entities until we
1479 -- actually install the complete context (in Install_Context).
1480 -- Otherwise things can get installed in the wrong context.
1482 else
1483 null;
1484 end if;
1486 Next (Item);
1487 end loop;
1489 -- Second pass: examine all limited_with clauses. All other context
1490 -- items are ignored in this pass.
1492 Item := First (Context_Items (N));
1493 while Present (Item) loop
1494 if Nkind (Item) = N_With_Clause
1495 and then Limited_Present (Item)
1496 then
1497 -- No need to check errors on implicitly generated limited-with
1498 -- clauses.
1500 if not Implicit_With (Item) then
1502 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1503 -- properly rejected, including renaming declarations.
1505 if Ukind not in N_Package_Declaration
1506 | N_Subprogram_Declaration
1507 | N_Generic_Declaration
1508 | N_Generic_Instantiation
1509 then
1510 Error_Msg_N ("limited with_clause not allowed here", Item);
1512 -- Check wrong use of a limited with clause applied to the
1513 -- compilation unit containing the limited-with clause.
1515 -- limited with P.Q;
1516 -- package P.Q is ...
1518 elsif Unit (Library_Unit (Item)) = Unit (N) then
1519 Error_Msg_N ("wrong use of limited-with clause", Item);
1521 -- Check wrong use of limited-with clause applied to some
1522 -- immediate ancestor.
1524 elsif Is_Child_Spec (Unit (N)) then
1525 declare
1526 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1527 P : Node_Id;
1529 begin
1530 P := Parent_Spec (Unit (N));
1531 loop
1532 if Unit (P) = Lib_U then
1533 Error_Msg_N
1534 ("limited with_clause cannot name ancestor",
1535 Item);
1536 exit;
1537 end if;
1539 exit when not Is_Child_Spec (Unit (P));
1540 P := Parent_Spec (Unit (P));
1541 end loop;
1542 end;
1543 end if;
1545 -- Check if the limited-withed unit is already visible through
1546 -- some context clause of the current compilation unit or some
1547 -- ancestor of the current compilation unit.
1549 declare
1550 Lim_Unit_Name : constant Node_Id := Name (Item);
1551 Comp_Unit : Node_Id;
1552 It : Node_Id;
1553 Unit_Name : Node_Id;
1555 begin
1556 Comp_Unit := N;
1557 loop
1558 It := First (Context_Items (Comp_Unit));
1559 while Present (It) loop
1560 if Item /= It
1561 and then Nkind (It) = N_With_Clause
1562 and then not Limited_Present (It)
1563 and then Nkind (Unit (Library_Unit (It))) in
1564 N_Package_Declaration |
1565 N_Package_Renaming_Declaration
1566 then
1567 if Nkind (Unit (Library_Unit (It))) =
1568 N_Package_Declaration
1569 then
1570 Unit_Name := Name (It);
1571 else
1572 Unit_Name := Name (Unit (Library_Unit (It)));
1573 end if;
1575 -- Check if the named package (or some ancestor)
1576 -- leaves visible the full-view of the unit given
1577 -- in the limited-with clause.
1579 loop
1580 if Designate_Same_Unit (Lim_Unit_Name,
1581 Unit_Name)
1582 then
1583 Error_Msg_Sloc := Sloc (It);
1584 Error_Msg_N
1585 ("simultaneous visibility of limited and "
1586 & "unlimited views not allowed", Item);
1587 Error_Msg_N
1588 ("\unlimited view visible through context "
1589 & "clause #", Item);
1590 exit;
1592 elsif Nkind (Unit_Name) = N_Identifier then
1593 exit;
1594 end if;
1596 Unit_Name := Prefix (Unit_Name);
1597 end loop;
1598 end if;
1600 Next (It);
1601 end loop;
1603 exit when not Is_Child_Spec (Unit (Comp_Unit));
1605 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1606 end loop;
1607 end;
1608 end if;
1610 -- Skip analyzing with clause if no unit, see above
1612 if Present (Library_Unit (Item)) then
1613 Analyze (Item);
1614 end if;
1616 -- A limited_with does not impose an elaboration order, but there
1617 -- is a semantic dependency for recompilation purposes.
1619 if not Implicit_With (Item) then
1620 Version_Update (N, Library_Unit (Item));
1621 end if;
1623 -- Pragmas and use clauses and with clauses other than limited with's
1624 -- are ignored in this pass through the context items.
1626 else
1627 null;
1628 end if;
1630 Next (Item);
1631 end loop;
1632 end Analyze_Context;
1634 -------------------------------
1635 -- Analyze_Package_Body_Stub --
1636 -------------------------------
1638 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1639 Id : constant Entity_Id := Defining_Entity (N);
1640 Nam : Entity_Id;
1641 Opts : Config_Switches_Type;
1643 begin
1644 -- The package declaration must be in the current declarative part
1646 Check_Stub_Level (N);
1647 Nam := Current_Entity_In_Scope (Id);
1649 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1650 Error_Msg_N ("missing specification for package stub", N);
1652 elsif Has_Completion (Nam)
1653 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1654 then
1655 Error_Msg_N ("duplicate or redundant stub for package", N);
1657 else
1658 -- Retain and restore the configuration options of the enclosing
1659 -- context as the proper body may introduce a set of its own.
1661 Opts := Save_Config_Switches;
1663 -- Indicate that the body of the package exists. If we are doing
1664 -- only semantic analysis, the stub stands for the body. If we are
1665 -- generating code, the existence of the body will be confirmed
1666 -- when we load the proper body.
1668 Set_Scope (Id, Current_Scope);
1669 Mutate_Ekind (Id, E_Package_Body);
1670 Set_Etype (Id, Standard_Void_Type);
1672 if Has_Aspects (N) then
1673 Analyze_Aspect_Specifications (N, Id);
1674 end if;
1676 Set_Has_Completion (Nam);
1677 Set_Corresponding_Spec_Of_Stub (N, Nam);
1678 Generate_Reference (Nam, Id, 'b');
1679 Analyze_Proper_Body (N, Nam);
1681 Restore_Config_Switches (Opts);
1682 end if;
1683 end Analyze_Package_Body_Stub;
1685 -------------------------
1686 -- Analyze_Proper_Body --
1687 -------------------------
1689 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1690 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1692 procedure Optional_Subunit;
1693 -- This procedure is called when the main unit is a stub, or when we
1694 -- are not generating code. In such a case, we analyze the subunit if
1695 -- present, which is user-friendly, but we don't complain if the subunit
1696 -- is missing. In GNATprove_Mode, we issue an error to avoid formal
1697 -- verification of a partial unit.
1699 ----------------------
1700 -- Optional_Subunit --
1701 ----------------------
1703 procedure Optional_Subunit is
1704 Comp_Unit : Node_Id;
1705 Unum : Unit_Number_Type;
1707 begin
1708 -- Try to load subunit, but ignore any errors that occur during the
1709 -- loading of the subunit, by using the special feature in Errout to
1710 -- ignore all errors. Note that Fatal_Error will still be set, so we
1711 -- will be able to check for this case below.
1713 if not GNATprove_Mode then
1714 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1715 end if;
1717 Unum :=
1718 Load_Unit
1719 (Load_Name => Subunit_Name,
1720 Required => GNATprove_Mode,
1721 Subunit => True,
1722 Error_Node => N);
1724 if not GNATprove_Mode then
1725 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1726 end if;
1728 -- All done if we successfully loaded the subunit
1730 if Unum /= No_Unit
1731 and then (Fatal_Error (Unum) /= Error_Detected
1732 or else Try_Semantics)
1733 then
1734 Comp_Unit := Cunit (Unum);
1736 -- If the file was empty or seriously mangled, the unit itself may
1737 -- be missing.
1739 if No (Unit (Comp_Unit)) then
1740 Error_Msg_N
1741 ("subunit does not contain expected proper body", N);
1743 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1744 Error_Msg_N
1745 ("expected SEPARATE subunit, found child unit",
1746 Cunit_Entity (Unum));
1747 else
1748 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1749 Analyze_Subunit (Comp_Unit);
1750 Set_Library_Unit (N, Comp_Unit);
1751 Set_Corresponding_Body (N, Defining_Entity (Unit (Comp_Unit)));
1752 end if;
1754 elsif Unum = No_Unit
1755 and then Present (Nam)
1756 then
1757 if Is_Protected_Type (Nam) then
1758 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1759 else
1760 Set_Corresponding_Body (
1761 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1762 end if;
1763 end if;
1764 end Optional_Subunit;
1766 -- Local variables
1768 Comp_Unit : Node_Id;
1769 Unum : Unit_Number_Type;
1771 -- Start of processing for Analyze_Proper_Body
1773 begin
1774 -- If the subunit is already loaded, it means that the main unit is a
1775 -- subunit, and that the current unit is one of its parents which was
1776 -- being analyzed to provide the needed context for the analysis of the
1777 -- subunit. In this case we analyze the subunit and continue with the
1778 -- parent, without looking at subsequent subunits.
1780 if Is_Loaded (Subunit_Name) then
1782 -- If the proper body is already linked to the stub node, the stub is
1783 -- in a generic unit and just needs analyzing.
1785 if Present (Library_Unit (N)) then
1786 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1788 -- If the subunit has severe errors, the spec of the enclosing
1789 -- body may not be available, in which case do not try analysis.
1791 if Serious_Errors_Detected > 0
1792 and then No (Library_Unit (Library_Unit (N)))
1793 then
1794 return;
1795 end if;
1797 -- Collect SCO information for loaded subunit if we are in the
1798 -- extended main unit.
1800 if Generate_SCO
1801 and then In_Extended_Main_Source_Unit
1802 (Cunit_Entity (Current_Sem_Unit))
1803 then
1804 SCO_Record_Raw (Get_Cunit_Unit_Number (Library_Unit (N)));
1805 end if;
1807 Analyze_Subunit (Library_Unit (N));
1809 -- Otherwise we must load the subunit and link to it
1811 else
1812 -- Load the subunit, this must work, since we originally loaded
1813 -- the subunit earlier on. So this will not really load it, just
1814 -- give access to it.
1816 Unum :=
1817 Load_Unit
1818 (Load_Name => Subunit_Name,
1819 Required => True,
1820 Subunit => False,
1821 Error_Node => N);
1823 -- And analyze the subunit in the parent context (note that we
1824 -- do not call Semantics, since that would remove the parent
1825 -- context). Because of this, we have to manually reset the
1826 -- compiler state to Analyzing since it got destroyed by Load.
1828 if Unum /= No_Unit then
1829 Compiler_State := Analyzing;
1831 -- Check that the proper body is a subunit and not a child
1832 -- unit. If the unit was previously loaded, the error will
1833 -- have been emitted when copying the generic node, so we
1834 -- just return to avoid cascaded errors.
1836 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1837 return;
1838 end if;
1840 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1841 Analyze_Subunit (Cunit (Unum));
1842 Set_Library_Unit (N, Cunit (Unum));
1843 end if;
1844 end if;
1846 -- If the main unit is a subunit, then we are just performing semantic
1847 -- analysis on that subunit, and any other subunits of any parent unit
1848 -- should be ignored. If the main unit is itself a subunit, another
1849 -- subunit is irrelevant unless it is a subunit of the current one, that
1850 -- is to say appears in the current source tree.
1852 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1853 and then Subunit_Name /= Unit_Name (Main_Unit)
1854 then
1855 -- But before we return, set the flag for unloaded subunits. This
1856 -- will suppress junk warnings of variables in the same declarative
1857 -- part (or a higher level one) that are in danger of looking unused
1858 -- when in fact there might be a declaration in the subunit that we
1859 -- do not intend to load.
1861 Unloaded_Subunits := True;
1862 return;
1864 -- If the subunit is not already loaded, and we are generating code,
1865 -- then this is the case where compilation started from the parent, and
1866 -- we are generating code for an entire subunit tree. In that case we
1867 -- definitely need to load the subunit.
1869 -- In order to continue the analysis with the rest of the parent,
1870 -- and other subunits, we load the unit without requiring its
1871 -- presence, and emit a warning if not found, rather than terminating
1872 -- the compilation abruptly, as for other missing file problems.
1874 elsif Original_Operating_Mode = Generate_Code then
1876 -- If the proper body is already linked to the stub node, the stub is
1877 -- in a generic unit and just needs analyzing.
1879 -- We update the version. Although we are not strictly technically
1880 -- semantically dependent on the subunit, given our approach of macro
1881 -- substitution of subunits, it makes sense to include it in the
1882 -- version identification.
1884 if Present (Library_Unit (N)) then
1885 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1886 Analyze_Subunit (Library_Unit (N));
1887 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1889 -- Otherwise we must load the subunit and link to it
1891 else
1892 -- Make sure that, if the subunit is preprocessed and -gnateG is
1893 -- specified, the preprocessed file will be written.
1895 Lib.Analysing_Subunit_Of_Main := True;
1896 Unum :=
1897 Load_Unit
1898 (Load_Name => Subunit_Name,
1899 Required => False,
1900 Subunit => True,
1901 Error_Node => N);
1902 Lib.Analysing_Subunit_Of_Main := False;
1904 -- Give message if we did not get the unit Emit warning even if
1905 -- missing subunit is not within main unit, to simplify debugging.
1907 pragma Assert (Original_Operating_Mode = Generate_Code);
1908 if Unum = No_Unit then
1909 Error_Msg_Unit_1 := Subunit_Name;
1910 Error_Msg_File_1 :=
1911 Get_File_Name (Subunit_Name, Subunit => True);
1912 Error_Msg_N
1913 ("subunit$$ in file{ not found??!!", N);
1914 Subunits_Missing := True;
1915 end if;
1917 -- Load_Unit may reset Compiler_State, since it may have been
1918 -- necessary to parse an additional units, so we make sure that
1919 -- we reset it to the Analyzing state.
1921 Compiler_State := Analyzing;
1923 if Unum /= No_Unit then
1924 if Debug_Flag_L then
1925 Write_Str ("*** Loaded subunit from stub. Analyze");
1926 Write_Eol;
1927 end if;
1929 Comp_Unit := Cunit (Unum);
1931 -- Check for child unit instead of subunit
1933 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1934 Error_Msg_N
1935 ("expected SEPARATE subunit, found child unit",
1936 Cunit_Entity (Unum));
1938 -- OK, we have a subunit
1940 else
1941 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1942 Set_Library_Unit (N, Comp_Unit);
1944 -- We update the version. Although we are not technically
1945 -- semantically dependent on the subunit, given our approach
1946 -- of macro substitution of subunits, it makes sense to
1947 -- include it in the version identification.
1949 Version_Update (Cunit (Main_Unit), Comp_Unit);
1951 -- Collect SCO information for loaded subunit if we are in
1952 -- the extended main unit.
1954 if Generate_SCO
1955 and then In_Extended_Main_Source_Unit
1956 (Cunit_Entity (Current_Sem_Unit))
1957 then
1958 SCO_Record_Raw (Unum);
1959 end if;
1961 -- Analyze the unit if semantics active
1963 if Fatal_Error (Unum) /= Error_Detected
1964 or else Try_Semantics
1965 then
1966 Analyze_Subunit (Comp_Unit);
1967 end if;
1968 end if;
1969 end if;
1970 end if;
1972 -- The remaining case is when the subunit is not already loaded and we
1973 -- are not generating code. In this case we are just performing semantic
1974 -- analysis on the parent, and we are not interested in the subunit. For
1975 -- subprograms, analyze the stub as a body. For other entities the stub
1976 -- has already been marked as completed.
1978 else
1979 Optional_Subunit;
1980 end if;
1981 end Analyze_Proper_Body;
1983 ----------------------------------
1984 -- Analyze_Protected_Body_Stub --
1985 ----------------------------------
1987 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1988 Id : constant Entity_Id := Defining_Entity (N);
1989 Nam : Entity_Id := Current_Entity_In_Scope (Id);
1990 Opts : Config_Switches_Type;
1992 begin
1993 Check_Stub_Level (N);
1995 -- First occurrence of name may have been as an incomplete type
1997 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1998 Nam := Full_View (Nam);
1999 end if;
2001 if No (Nam) or else not Is_Protected_Type (Etype (Nam)) then
2002 Error_Msg_N ("missing specification for Protected body", N);
2004 else
2005 -- Retain and restore the configuration options of the enclosing
2006 -- context as the proper body may introduce a set of its own.
2008 Opts := Save_Config_Switches;
2010 Set_Scope (Id, Current_Scope);
2011 Mutate_Ekind (Id, E_Protected_Body);
2012 Set_Etype (Id, Standard_Void_Type);
2014 if Has_Aspects (N) then
2015 Analyze_Aspect_Specifications (N, Id);
2016 end if;
2018 Set_Has_Completion (Etype (Nam));
2019 Set_Corresponding_Spec_Of_Stub (N, Nam);
2020 Generate_Reference (Nam, Id, 'b');
2021 Analyze_Proper_Body (N, Etype (Nam));
2023 Restore_Config_Switches (Opts);
2024 end if;
2025 end Analyze_Protected_Body_Stub;
2027 ----------------------------------
2028 -- Analyze_Subprogram_Body_Stub --
2029 ----------------------------------
2031 -- A subprogram body stub can appear with or without a previous spec. If
2032 -- there is one, then the analysis of the body will find it and verify
2033 -- conformance. The formals appearing in the specification of the stub play
2034 -- no role, except for requiring an additional conformance check. If there
2035 -- is no previous subprogram declaration, the stub acts as a spec, and
2036 -- provides the defining entity for the subprogram.
2038 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
2039 Decl : Node_Id;
2040 Opts : Config_Switches_Type;
2042 begin
2043 Check_Stub_Level (N);
2045 -- Verify that the identifier for the stub is unique within this
2046 -- declarative part.
2048 if Nkind (Parent (N)) in
2049 N_Block_Statement | N_Package_Body | N_Subprogram_Body
2050 then
2051 Decl := First (Declarations (Parent (N)));
2052 while Present (Decl) and then Decl /= N loop
2053 if Nkind (Decl) = N_Subprogram_Body_Stub
2054 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
2055 Chars (Defining_Unit_Name (Specification (N))))
2056 then
2057 Error_Msg_N ("identifier for stub is not unique", N);
2058 end if;
2060 Next (Decl);
2061 end loop;
2062 end if;
2064 -- Retain and restore the configuration options of the enclosing context
2065 -- as the proper body may introduce a set of its own.
2067 Opts := Save_Config_Switches;
2069 -- Treat stub as a body, which checks conformance if there is a previous
2070 -- declaration, or else introduces entity and its signature.
2072 Analyze_Subprogram_Body (N);
2073 Analyze_Proper_Body (N, Empty);
2075 Restore_Config_Switches (Opts);
2076 end Analyze_Subprogram_Body_Stub;
2078 ---------------------
2079 -- Analyze_Subunit --
2080 ---------------------
2082 -- A subunit is compiled either by itself (for semantic checking) or as
2083 -- part of compiling the parent (for code generation). In either case, by
2084 -- the time we actually process the subunit, the parent has already been
2085 -- installed and analyzed. The node N is a compilation unit, whose context
2086 -- needs to be treated here, because we come directly here from the parent
2087 -- without calling Analyze_Compilation_Unit.
2089 -- The compilation context includes the explicit context of the subunit,
2090 -- and the context of the parent, together with the parent itself. In order
2091 -- to compile the current context, we remove the one inherited from the
2092 -- parent, in order to have a clean visibility table. We restore the parent
2093 -- context before analyzing the proper body itself. On exit, we remove only
2094 -- the explicit context of the subunit.
2096 -- WARNING: This routine manages SPARK regions. Return statements must be
2097 -- replaced by gotos which jump to the end of the routine and restore the
2098 -- SPARK mode.
2100 procedure Analyze_Subunit (N : Node_Id) is
2101 Lib_Unit : constant Node_Id := Library_Unit (N);
2102 Par_Unit : constant Entity_Id := Current_Scope;
2104 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
2105 Num_Scopes : Nat := 0;
2106 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2107 Enclosing_Child : Entity_Id := Empty;
2108 Svg : constant Suppress_Record := Scope_Suppress;
2110 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
2111 Cunit_Boolean_Restrictions_Save;
2112 -- Save non-partition wide restrictions before processing the subunit.
2113 -- All subunits are analyzed with config restrictions reset and we need
2114 -- to restore these saved values at the end.
2116 procedure Analyze_Subunit_Context;
2117 -- Capture names in use clauses of the subunit. This must be done before
2118 -- re-installing parent declarations, because items in the context must
2119 -- not be hidden by declarations local to the parent.
2121 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
2122 -- Recursive procedure to restore scope of all ancestors of subunit,
2123 -- from outermost in. If parent is not a subunit, the call to install
2124 -- context installs context of spec and (if parent is a child unit) the
2125 -- context of its parents as well. It is confusing that parents should
2126 -- be treated differently in both cases, but the semantics are just not
2127 -- identical.
2129 procedure Re_Install_Use_Clauses;
2130 -- As part of the removal of the parent scope, the use clauses are
2131 -- removed, to be reinstalled when the context of the subunit has been
2132 -- analyzed. Use clauses may also have been affected by the analysis of
2133 -- the context of the subunit, so they have to be applied again, to
2134 -- insure that the compilation environment of the rest of the parent
2135 -- unit is identical.
2137 procedure Remove_Scope;
2138 -- Remove current scope from scope stack, and preserve the list of use
2139 -- clauses in it, to be reinstalled after context is analyzed.
2141 -----------------------------
2142 -- Analyze_Subunit_Context --
2143 -----------------------------
2145 procedure Analyze_Subunit_Context is
2146 Item : Node_Id;
2147 Unit_Name : Entity_Id;
2149 begin
2150 Analyze_Context (N);
2151 Check_No_Elab_Code_All (N);
2153 -- Make withed units immediately visible. If child unit, make the
2154 -- ultimate parent immediately visible.
2156 Item := First (Context_Items (N));
2157 while Present (Item) loop
2158 if Nkind (Item) = N_With_Clause then
2160 -- Protect frontend against previous errors in context clauses
2162 if Nkind (Name (Item)) /= N_Selected_Component then
2163 if Error_Posted (Item) then
2164 null;
2166 else
2167 -- If a subunits has serious syntax errors, the context
2168 -- may not have been loaded. Add a harmless unit name to
2169 -- attempt processing.
2171 if Serious_Errors_Detected > 0
2172 and then No (Entity (Name (Item)))
2173 then
2174 Set_Entity (Name (Item), Standard_Standard);
2175 end if;
2177 Unit_Name := Entity (Name (Item));
2178 loop
2179 Set_Is_Visible_Lib_Unit (Unit_Name);
2180 exit when Scope (Unit_Name) = Standard_Standard;
2181 Unit_Name := Scope (Unit_Name);
2183 if No (Unit_Name) then
2184 Check_Error_Detected;
2185 return;
2186 end if;
2187 end loop;
2189 if not Is_Immediately_Visible (Unit_Name) then
2190 Set_Is_Immediately_Visible (Unit_Name);
2191 Set_Context_Installed (Item);
2192 end if;
2193 end if;
2194 end if;
2196 elsif Nkind (Item) = N_Use_Package_Clause then
2197 Analyze (Name (Item));
2199 elsif Nkind (Item) = N_Use_Type_Clause then
2200 Analyze (Subtype_Mark (Item));
2201 end if;
2203 Next (Item);
2204 end loop;
2206 -- Reset visibility of withed units. They will be made visible again
2207 -- when we install the subunit context.
2209 Item := First (Context_Items (N));
2210 while Present (Item) loop
2211 if Nkind (Item) = N_With_Clause
2213 -- Protect frontend against previous errors in context clauses
2215 and then Nkind (Name (Item)) /= N_Selected_Component
2216 and then not Error_Posted (Item)
2217 then
2218 Unit_Name := Entity (Name (Item));
2219 loop
2220 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2221 exit when Scope (Unit_Name) = Standard_Standard;
2222 Unit_Name := Scope (Unit_Name);
2223 end loop;
2225 if Context_Installed (Item) then
2226 Set_Is_Immediately_Visible (Unit_Name, False);
2227 Set_Context_Installed (Item, False);
2228 end if;
2229 end if;
2231 Next (Item);
2232 end loop;
2233 end Analyze_Subunit_Context;
2235 ------------------------
2236 -- Re_Install_Parents --
2237 ------------------------
2239 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2240 E : Entity_Id;
2242 begin
2243 if Nkind (Unit (L)) = N_Subunit then
2244 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2245 end if;
2247 Install_Context (L, False);
2249 -- If the subunit occurs within a child unit, we must restore the
2250 -- immediate visibility of any siblings that may occur in context.
2251 -- In addition, we must reset the previous visibility of the
2252 -- parent unit which is now on the scope stack. This is because
2253 -- the Previous_Visibility was previously set when removing the
2254 -- context. This is necessary to prevent the parent entity from
2255 -- remaining visible after the subunit is compiled. This only
2256 -- has an effect if a homonym exists in a body to be processed
2257 -- later if inlining is enabled.
2259 if Present (Enclosing_Child) then
2260 Install_Siblings (Enclosing_Child, L);
2261 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
2262 False;
2263 end if;
2265 Push_Scope (Scop);
2267 if Scop /= Par_Unit then
2268 Set_Is_Immediately_Visible (Scop);
2269 end if;
2271 -- Make entities in scope visible again. For child units, restore
2272 -- visibility only if they are actually in context.
2274 E := First_Entity (Current_Scope);
2275 while Present (E) loop
2276 if not Is_Child_Unit (E) or else Is_Visible_Lib_Unit (E) then
2277 Set_Is_Immediately_Visible (E);
2278 end if;
2280 Next_Entity (E);
2281 end loop;
2283 -- A subunit appears within a body, and for a nested subunits all the
2284 -- parents are bodies. Restore full visibility of their private
2285 -- entities.
2287 if Is_Package_Or_Generic_Package (Scop) then
2288 Set_In_Package_Body (Scop);
2289 Install_Private_Declarations (Scop);
2290 end if;
2291 end Re_Install_Parents;
2293 ----------------------------
2294 -- Re_Install_Use_Clauses --
2295 ----------------------------
2297 procedure Re_Install_Use_Clauses is
2298 U : Node_Id;
2299 begin
2300 for J in reverse 1 .. Num_Scopes loop
2301 U := Use_Clauses (J);
2302 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2303 Install_Use_Clauses (U);
2304 end loop;
2305 end Re_Install_Use_Clauses;
2307 ------------------
2308 -- Remove_Scope --
2309 ------------------
2311 procedure Remove_Scope is
2312 E : Entity_Id;
2314 begin
2315 Num_Scopes := Num_Scopes + 1;
2316 Use_Clauses (Num_Scopes) :=
2317 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2319 E := First_Entity (Current_Scope);
2320 while Present (E) loop
2321 Set_Is_Immediately_Visible (E, False);
2322 Next_Entity (E);
2323 end loop;
2325 if Is_Child_Unit (Current_Scope) then
2326 Enclosing_Child := Current_Scope;
2327 end if;
2329 Pop_Scope;
2330 end Remove_Scope;
2332 Saved_SM : SPARK_Mode_Type := SPARK_Mode;
2333 Saved_SMP : Node_Id := SPARK_Mode_Pragma;
2334 -- Save the SPARK mode-related data to restore on exit. Removing
2335 -- enclosing scopes and contexts to provide a clean environment for the
2336 -- context of the subunit will eliminate any previously set SPARK_Mode.
2338 -- Start of processing for Analyze_Subunit
2340 begin
2341 -- For subunit in main extended unit, we reset the configuration values
2342 -- for the non-partition-wide restrictions. For other units reset them.
2344 if In_Extended_Main_Source_Unit (N) then
2345 Restore_Config_Cunit_Boolean_Restrictions;
2346 else
2347 Reset_Cunit_Boolean_Restrictions;
2348 end if;
2350 if Style_Check then
2351 declare
2352 Nam : Node_Id := Name (Unit (N));
2354 begin
2355 if Nkind (Nam) = N_Selected_Component then
2356 Nam := Selector_Name (Nam);
2357 end if;
2359 Check_Identifier (Nam, Par_Unit);
2360 end;
2361 end if;
2363 if not Is_Empty_List (Context_Items (N)) then
2365 -- Save current use clauses
2367 Remove_Scope;
2368 Remove_Context (Lib_Unit);
2370 -- Now remove parents and their context, including enclosing subunits
2371 -- and the outer parent body which is not a subunit.
2373 if Present (Lib_Spec) then
2374 Remove_Context (Lib_Spec);
2376 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2377 Lib_Spec := Library_Unit (Lib_Spec);
2378 Remove_Scope;
2379 Remove_Context (Lib_Spec);
2380 end loop;
2382 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2383 Remove_Scope;
2384 end if;
2386 if Nkind (Unit (Lib_Spec)) in N_Package_Body | N_Subprogram_Body
2387 then
2388 Remove_Context (Library_Unit (Lib_Spec));
2389 end if;
2390 end if;
2392 Set_Is_Immediately_Visible (Par_Unit, False);
2394 Analyze_Subunit_Context;
2396 -- Take into account the effect of any SPARK_Mode configuration
2397 -- pragma, which takes precedence over a different value of
2398 -- SPARK_Mode inherited from the context of the stub.
2400 if SPARK_Mode /= None then
2401 Saved_SM := SPARK_Mode;
2402 Saved_SMP := SPARK_Mode_Pragma;
2403 end if;
2405 Re_Install_Parents (Lib_Unit, Par_Unit);
2406 Set_Is_Immediately_Visible (Par_Unit);
2408 -- If the context includes a child unit of the parent of the subunit,
2409 -- the parent will have been removed from visibility, after compiling
2410 -- that cousin in the context. The visibility of the parent must be
2411 -- restored now. This also applies if the context includes another
2412 -- subunit of the same parent which in turn includes a child unit in
2413 -- its context.
2415 if Is_Package_Or_Generic_Package (Par_Unit) then
2416 if not Is_Immediately_Visible (Par_Unit)
2417 or else (Present (First_Entity (Par_Unit))
2418 and then not
2419 Is_Immediately_Visible (First_Entity (Par_Unit)))
2420 then
2421 Set_Is_Immediately_Visible (Par_Unit);
2422 Install_Visible_Declarations (Par_Unit);
2423 Install_Private_Declarations (Par_Unit);
2424 end if;
2425 end if;
2427 Re_Install_Use_Clauses;
2428 Install_Context (N, Chain => False);
2430 -- Restore state of suppress flags for current body
2432 Scope_Suppress := Svg;
2434 -- If the subunit is within a child unit, then siblings of any parent
2435 -- unit that appear in the context clause of the subunit must also be
2436 -- made immediately visible.
2438 if Present (Enclosing_Child) then
2439 Install_Siblings (Enclosing_Child, N);
2440 end if;
2441 end if;
2443 Generate_Parent_References (Unit (N), Par_Unit);
2445 -- Reinstall the SPARK_Mode which was in effect prior to any scope and
2446 -- context manipulations, taking into account a possible SPARK_Mode
2447 -- configuration pragma if present.
2449 Install_SPARK_Mode (Saved_SM, Saved_SMP);
2451 -- If the subunit is part of a compilation unit which is subject to
2452 -- pragma Elaboration_Checks, set the model specified by the pragma
2453 -- because it applies to all parts of the unit.
2455 Install_Elaboration_Model (Par_Unit);
2457 -- The syntax rules require a proper body for a subprogram subunit
2459 if Nkind (Proper_Body (Sinfo.Nodes.Unit (N))) = N_Subprogram_Declaration
2460 then
2461 if Null_Present (Specification (Proper_Body (Sinfo.Nodes.Unit (N))))
2462 then
2463 Error_Msg_N
2464 ("null procedure not allowed as subunit",
2465 Proper_Body (Unit (N)));
2466 else
2467 Error_Msg_N
2468 ("subprogram declaration not allowed as subunit",
2469 Defining_Unit_Name (Specification (Proper_Body (Unit (N)))));
2470 end if;
2471 end if;
2473 Analyze (Proper_Body (Unit (N)));
2474 Remove_Context (N);
2476 -- The subunit may contain a with_clause on a sibling of some ancestor.
2477 -- Removing the context will remove from visibility those ancestor child
2478 -- units, which must be restored to the visibility they have in the
2479 -- enclosing body.
2481 if Present (Enclosing_Child) then
2482 declare
2483 C : Entity_Id;
2484 begin
2485 C := Current_Scope;
2486 while Present (C) and then C /= Standard_Standard loop
2487 Set_Is_Immediately_Visible (C);
2488 Set_Is_Visible_Lib_Unit (C);
2489 C := Scope (C);
2490 end loop;
2491 end;
2492 end if;
2494 -- Deal with restore of restrictions
2496 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2497 end Analyze_Subunit;
2499 ----------------------------
2500 -- Analyze_Task_Body_Stub --
2501 ----------------------------
2503 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2504 Id : constant Entity_Id := Defining_Entity (N);
2505 Loc : constant Source_Ptr := Sloc (N);
2506 Nam : Entity_Id := Current_Entity_In_Scope (Id);
2508 begin
2509 Check_Stub_Level (N);
2511 -- First occurrence of name may have been as an incomplete type
2513 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2514 Nam := Full_View (Nam);
2515 end if;
2517 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2518 Error_Msg_N ("missing specification for task body", N);
2520 else
2521 Set_Scope (Id, Current_Scope);
2522 Mutate_Ekind (Id, E_Task_Body);
2523 Set_Etype (Id, Standard_Void_Type);
2525 if Has_Aspects (N) then
2526 Analyze_Aspect_Specifications (N, Id);
2527 end if;
2529 Generate_Reference (Nam, Id, 'b');
2530 Set_Corresponding_Spec_Of_Stub (N, Nam);
2532 -- Check for duplicate stub, if so give message and terminate
2534 if Has_Completion (Etype (Nam)) then
2535 Error_Msg_N ("duplicate stub for task", N);
2536 return;
2537 else
2538 Set_Has_Completion (Etype (Nam));
2539 end if;
2541 Analyze_Proper_Body (N, Etype (Nam));
2543 -- Set elaboration flag to indicate that entity is callable. This
2544 -- cannot be done in the expansion of the body itself, because the
2545 -- proper body is not in a declarative part. This is only done if
2546 -- expansion is active, because the context may be generic and the
2547 -- flag not defined yet.
2549 if Expander_Active then
2550 Insert_After (N,
2551 Make_Assignment_Statement (Loc,
2552 Name =>
2553 Make_Identifier (Loc,
2554 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2555 Expression => New_Occurrence_Of (Standard_True, Loc)));
2556 end if;
2557 end if;
2558 end Analyze_Task_Body_Stub;
2560 -------------------------
2561 -- Analyze_With_Clause --
2562 -------------------------
2564 -- Analyze the declaration of a unit in a with clause. At end, label the
2565 -- with clause with the defining entity for the unit.
2567 procedure Analyze_With_Clause (N : Node_Id) is
2569 -- Retrieve the original kind of the unit node, before analysis. If it
2570 -- is a subprogram instantiation, its analysis below will rewrite the
2571 -- node as the declaration of the wrapper package. If the same
2572 -- instantiation appears indirectly elsewhere in the context, it will
2573 -- have been analyzed already.
2575 Unit_Kind : constant Node_Kind :=
2576 Nkind (Original_Node (Unit (Library_Unit (N))));
2577 Nam : constant Node_Id := Name (N);
2578 E_Name : Entity_Id;
2579 Par_Name : Entity_Id;
2580 Pref : Node_Id;
2581 U : Node_Id;
2583 Intunit : Boolean;
2584 -- Set True if the unit currently being compiled is an internal unit
2586 Restriction_Violation : Boolean := False;
2587 -- Set True if a with violates a restriction, no point in giving any
2588 -- warnings if we have this definite error.
2590 Save_Style_Check : constant Boolean := Opt.Style_Check;
2592 begin
2593 U := Unit (Library_Unit (N));
2595 -- If this is an internal unit which is a renaming, then this is a
2596 -- violation of No_Obsolescent_Features.
2598 -- Note: this is not quite right if the user defines one of these units
2599 -- himself, but that's a marginal case, and fixing it is hard ???
2601 if Ada_Version >= Ada_95
2602 and then In_Predefined_Renaming (U)
2603 then
2604 if Restriction_Check_Required (No_Obsolescent_Features) then
2605 Check_Restriction (No_Obsolescent_Features, N);
2606 Restriction_Violation := True;
2607 end if;
2609 if Warn_On_Obsolescent_Feature then
2610 Error_Msg_N
2611 ("renamed predefined unit is an obsolescent feature "
2612 & "(RM J.1)?j?", N);
2613 end if;
2614 end if;
2616 -- Check No_Implementation_Units violation
2618 if Restriction_Check_Required (No_Implementation_Units) then
2619 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2620 null;
2621 else
2622 Check_Restriction (No_Implementation_Units, Nam);
2623 Restriction_Violation := True;
2624 end if;
2625 end if;
2627 -- Several actions are skipped for dummy packages (those supplied for
2628 -- with's where no matching file could be found). Such packages are
2629 -- identified by the Sloc value being set to No_Location.
2631 if Limited_Present (N) then
2633 -- Ada 2005 (AI-50217): Build visibility structures but do not
2634 -- analyze the unit.
2636 -- If the designated unit is a predefined unit, which might be used
2637 -- implicitly through the rtsfind machinery, a limited with clause
2638 -- on such a unit is usually pointless, because run-time units are
2639 -- unlikely to appear in mutually dependent units, and because this
2640 -- disables the rtsfind mechanism. We transform such limited with
2641 -- clauses into regular with clauses.
2643 if Sloc (U) /= No_Location then
2644 if In_Predefined_Unit (U) then
2645 Set_Limited_Present (N, False);
2646 Analyze_With_Clause (N);
2647 else
2648 Build_Limited_Views (N);
2649 end if;
2650 end if;
2652 return;
2653 end if;
2655 -- If we are compiling under "don't quit" mode (-gnatq) and we have
2656 -- already detected serious errors then we mark the with-clause nodes as
2657 -- analyzed before the corresponding compilation unit is analyzed. This
2658 -- is done here to protect the frontend against never ending recursion
2659 -- caused by circularities in the sources (because the previous errors
2660 -- may break the regular machine of the compiler implemented in
2661 -- Load_Unit to detect circularities).
2663 if Serious_Errors_Detected > 0 and then Try_Semantics then
2664 Set_Analyzed (N);
2665 end if;
2667 Semantics (Library_Unit (N));
2669 Intunit := Is_Internal_Unit (Current_Sem_Unit);
2671 if Sloc (U) /= No_Location then
2673 -- Check restrictions, except that we skip the check if this is an
2674 -- internal unit unless we are compiling the internal unit as the
2675 -- main unit. We also skip this for dummy packages.
2677 Check_Restriction_No_Dependence (Nam, N);
2679 if not Intunit or else Current_Sem_Unit = Main_Unit then
2680 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2681 end if;
2683 -- Deal with special case of GNAT.Current_Exceptions which interacts
2684 -- with the optimization of local raise statements into gotos.
2686 if Nkind (Nam) = N_Selected_Component
2687 and then Nkind (Prefix (Nam)) = N_Identifier
2688 and then Chars (Prefix (Nam)) = Name_Gnat
2689 and then Chars (Selector_Name (Nam))
2690 in Name_Most_Recent_Exception | Name_Exception_Traces
2691 then
2692 Check_Restriction (No_Exception_Propagation, N);
2693 Special_Exception_Package_Used := True;
2694 end if;
2696 -- Check for inappropriate with of internal implementation unit if we
2697 -- are not compiling an internal unit and also check for withing unit
2698 -- in wrong version of Ada. Do not issue these messages for implicit
2699 -- with's generated by the compiler itself.
2701 if Implementation_Unit_Warnings
2702 and then not Intunit
2703 and then not Implicit_With (N)
2704 and then not Restriction_Violation
2705 then
2706 case Get_Kind_Of_Unit (Get_Source_Unit (U)) is
2707 when Implementation_Unit =>
2708 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2710 -- Add alternative name if available, otherwise issue a
2711 -- general warning message.
2713 if Error_Msg_Strlen /= 0 then
2714 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2715 else
2716 Error_Msg_F
2717 ("\use of this unit is non-portable and "
2718 & "version-dependent?i?", Name (N));
2719 end if;
2721 when Not_Predefined_Unit | Ada_95_Unit =>
2722 null; -- no checks needed
2724 when Ada_2005_Unit =>
2725 if Ada_Version < Ada_2005
2726 and then Warn_On_Ada_2005_Compatibility
2727 then
2728 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2729 end if;
2731 when Ada_2012_Unit =>
2732 if Ada_Version < Ada_2012
2733 and then Warn_On_Ada_2012_Compatibility
2734 then
2735 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2736 end if;
2738 when Ada_2022_Unit =>
2739 if Ada_Version < Ada_2022
2740 and then Warn_On_Ada_2022_Compatibility
2741 then
2742 Error_Msg_N ("& is an Ada 2022 unit?i?", Name (N));
2743 end if;
2744 end case;
2745 end if;
2746 end if;
2748 -- Semantic analysis of a generic unit is performed on a copy of
2749 -- the original tree. Retrieve the entity on which semantic info
2750 -- actually appears.
2752 if Unit_Kind in N_Generic_Declaration then
2753 E_Name := Defining_Entity (U);
2755 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2756 -- the case of an instantiation, semantic analysis above will have
2757 -- replaced the unit by its instantiated version. If the instance body
2758 -- has been generated, the instance now denotes the body entity. For
2759 -- visibility purposes we need the entity of its spec.
2761 elsif (Unit_Kind = N_Package_Instantiation
2762 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2763 N_Package_Instantiation)
2764 and then Nkind (U) = N_Package_Body
2765 then
2766 E_Name := Corresponding_Spec (U);
2768 elsif Unit_Kind = N_Package_Instantiation
2769 and then Nkind (U) = N_Package_Instantiation
2770 and then Present (Instance_Spec (U))
2771 then
2772 -- If the instance has not been rewritten as a package declaration,
2773 -- then it appeared already in a previous with clause. Retrieve
2774 -- the entity from the previous instance.
2776 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2778 elsif Unit_Kind in N_Subprogram_Instantiation then
2780 -- The visible subprogram is created during instantiation, and is
2781 -- an attribute of the wrapper package. We retrieve the wrapper
2782 -- package directly from the instantiation node. If the instance
2783 -- is inlined the unit is still an instantiation. Otherwise it has
2784 -- been rewritten as the declaration of the wrapper itself.
2786 if Nkind (U) in N_Subprogram_Instantiation then
2787 E_Name :=
2788 Related_Instance
2789 (Defining_Entity (Specification (Instance_Spec (U))));
2790 else
2791 E_Name := Related_Instance (Defining_Entity (U));
2792 end if;
2794 elsif Unit_Kind = N_Package_Renaming_Declaration
2795 or else Unit_Kind in N_Generic_Renaming_Declaration
2796 then
2797 E_Name := Defining_Entity (U);
2799 elsif Unit_Kind = N_Subprogram_Body
2800 and then Nkind (Name (N)) = N_Selected_Component
2801 and then not Acts_As_Spec (Library_Unit (N))
2802 then
2803 -- For a child unit that has no spec, one has been created and
2804 -- analyzed. The entity required is that of the spec.
2806 E_Name := Corresponding_Spec (U);
2808 else
2809 E_Name := Defining_Entity (U);
2810 end if;
2812 if Nkind (Name (N)) = N_Selected_Component then
2814 -- Child unit in a with clause
2816 Change_Selected_Component_To_Expanded_Name (Name (N));
2818 -- If this is a child unit without a spec, and it has been analyzed
2819 -- already, a declaration has been created for it. The with_clause
2820 -- must reflect the actual body, and not the generated declaration,
2821 -- to prevent spurious binding errors involving an out-of-date spec.
2822 -- Note that this can only happen if the unit includes more than one
2823 -- with_clause for the child unit (e.g. in separate subunits).
2825 if Unit_Kind = N_Subprogram_Declaration
2826 and then Analyzed (Library_Unit (N))
2827 and then not Comes_From_Source (Library_Unit (N))
2828 then
2829 Set_Library_Unit (N,
2830 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2831 end if;
2832 end if;
2834 -- Restore style checks
2836 Style_Check := Save_Style_Check;
2838 -- Record the reference, but do NOT set the unit as referenced, we want
2839 -- to consider the unit as unreferenced if this is the only reference
2840 -- that occurs.
2842 Set_Entity_With_Checks (Name (N), E_Name);
2843 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2845 -- Generate references and check No_Dependence restriction for parents
2847 if Is_Child_Unit (E_Name) then
2848 Pref := Prefix (Name (N));
2849 Par_Name := Scope (E_Name);
2850 while Nkind (Pref) = N_Selected_Component loop
2851 Change_Selected_Component_To_Expanded_Name (Pref);
2853 if Present (Entity (Selector_Name (Pref)))
2854 and then
2855 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2856 and then Entity (Selector_Name (Pref)) /= Par_Name
2857 then
2858 -- The prefix is a child unit that denotes a renaming declaration.
2859 -- Replace the prefix directly with the renamed unit, because the
2860 -- rest of the prefix is irrelevant to the visibility of the real
2861 -- unit.
2863 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2864 exit;
2865 end if;
2867 Set_Entity_With_Checks (Pref, Par_Name);
2869 Generate_Reference (Par_Name, Pref);
2870 Check_Restriction_No_Dependence (Pref, N);
2871 Pref := Prefix (Pref);
2873 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2874 -- is set to Standard_Standard, and no attempt should be made to
2875 -- further unwind scopes.
2877 if Par_Name /= Standard_Standard then
2878 Par_Name := Scope (Par_Name);
2879 end if;
2881 -- Abandon processing in case of previous errors
2883 if No (Par_Name) then
2884 Check_Error_Detected;
2885 return;
2886 end if;
2887 end loop;
2889 if Present (Entity (Pref))
2890 and then not Analyzed (Parent (Parent (Entity (Pref))))
2891 then
2892 -- If the entity is set without its unit being compiled, the
2893 -- original parent is a renaming, and Par_Name is the renamed
2894 -- entity. For visibility purposes, we need the original entity,
2895 -- which must be analyzed now because Load_Unit directly retrieves
2896 -- the renamed unit, and the renaming declaration itself has not
2897 -- been analyzed.
2899 Analyze (Parent (Parent (Entity (Pref))));
2900 pragma Assert (Renamed_Entity (Entity (Pref)) = Par_Name);
2901 Par_Name := Entity (Pref);
2902 end if;
2904 -- Guard against missing or misspelled child units
2906 if Present (Par_Name) then
2907 Set_Entity_With_Checks (Pref, Par_Name);
2908 Generate_Reference (Par_Name, Pref);
2910 else
2911 pragma Assert (Serious_Errors_Detected /= 0);
2913 -- Mark the node to indicate that a related error has been posted.
2914 -- This defends further compilation passes against improper use of
2915 -- the invalid WITH clause node.
2917 Set_Error_Posted (N);
2918 Set_Name (N, Error);
2919 return;
2920 end if;
2921 end if;
2923 -- If the withed unit is System, and a system extension pragma is
2924 -- present, compile the extension now, rather than waiting for a
2925 -- visibility check on a specific entity.
2927 if Chars (E_Name) = Name_System
2928 and then Scope (E_Name) = Standard_Standard
2929 and then Present (System_Extend_Unit)
2930 and then Present_System_Aux (N)
2931 then
2932 -- If the extension is not present, an error will have been emitted
2934 null;
2935 end if;
2937 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2938 -- to private_with units; they will be made visible later (just before
2939 -- the private part is analyzed)
2941 if Private_Present (N) then
2942 Set_Is_Immediately_Visible (E_Name, False);
2943 end if;
2945 -- Propagate Fatal_Error setting from with'ed unit to current unit
2947 case Fatal_Error (Get_Source_Unit (Library_Unit (N))) is
2949 -- Nothing to do if with'ed unit had no error
2951 when None =>
2952 null;
2954 -- If with'ed unit had a detected fatal error, propagate it
2956 when Error_Detected =>
2957 Set_Fatal_Error (Current_Sem_Unit, Error_Detected);
2959 -- If with'ed unit had an ignored error, then propagate it but do not
2960 -- overide an existring setting.
2962 when Error_Ignored =>
2963 if Fatal_Error (Current_Sem_Unit) = None then
2964 Set_Fatal_Error (Current_Sem_Unit, Error_Ignored);
2965 end if;
2966 end case;
2967 end Analyze_With_Clause;
2969 ------------------------------
2970 -- Check_Private_Child_Unit --
2971 ------------------------------
2973 procedure Check_Private_Child_Unit (N : Node_Id) is
2974 Lib_Unit : constant Node_Id := Unit (N);
2975 Item : Node_Id;
2976 Curr_Unit : Entity_Id;
2977 Sub_Parent : Node_Id;
2978 Priv_Child : Entity_Id;
2979 Par_Lib : Entity_Id;
2980 Par_Spec : Node_Id;
2982 begin
2983 if Nkind (Lib_Unit) in N_Package_Body | N_Subprogram_Body then
2984 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2985 Par_Lib := Curr_Unit;
2987 elsif Nkind (Lib_Unit) = N_Subunit then
2989 -- The parent is itself a body. The parent entity is to be found in
2990 -- the corresponding spec.
2992 Sub_Parent := Library_Unit (N);
2993 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2995 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2996 -- enclosing body, retrieve the spec entity which is the proper
2997 -- ancestor we need for the following tests.
2999 if Ekind (Curr_Unit) = E_Package_Body then
3000 Curr_Unit := Spec_Entity (Curr_Unit);
3001 end if;
3003 Par_Lib := Curr_Unit;
3005 else
3006 Curr_Unit := Defining_Entity (Lib_Unit);
3008 Par_Lib := Curr_Unit;
3009 Par_Spec := Parent_Spec (Lib_Unit);
3011 if No (Par_Spec) then
3012 Par_Lib := Empty;
3013 else
3014 Par_Lib := Defining_Entity (Unit (Par_Spec));
3015 end if;
3016 end if;
3018 -- Loop through context items
3020 Item := First (Context_Items (N));
3021 while Present (Item) loop
3023 -- Ada 2005 (AI-262): Allow private_with of a private child package
3024 -- in public siblings
3026 if Nkind (Item) = N_With_Clause
3027 and then not Implicit_With (Item)
3028 and then not Limited_Present (Item)
3029 and then Is_Private_Descendant (Entity (Name (Item)))
3030 then
3031 Priv_Child := Entity (Name (Item));
3033 declare
3034 Curr_Parent : Entity_Id := Par_Lib;
3035 Child_Parent : Entity_Id := Scope (Priv_Child);
3036 Prv_Ancestor : Entity_Id := Child_Parent;
3037 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
3039 begin
3040 -- If the child unit is a public child then locate the nearest
3041 -- private ancestor. Child_Parent will then be set to the
3042 -- parent of that ancestor.
3044 if not Is_Private_Library_Unit (Priv_Child) then
3045 while Present (Prv_Ancestor)
3046 and then not Is_Private_Library_Unit (Prv_Ancestor)
3047 loop
3048 Prv_Ancestor := Scope (Prv_Ancestor);
3049 end loop;
3051 if Present (Prv_Ancestor) then
3052 Child_Parent := Scope (Prv_Ancestor);
3053 end if;
3054 end if;
3056 while Present (Curr_Parent)
3057 and then Curr_Parent /= Standard_Standard
3058 and then Curr_Parent /= Child_Parent
3059 loop
3060 Curr_Private :=
3061 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
3062 Curr_Parent := Scope (Curr_Parent);
3063 end loop;
3065 if No (Curr_Parent) then
3066 Curr_Parent := Standard_Standard;
3067 end if;
3069 if Curr_Parent /= Child_Parent then
3070 if Ekind (Priv_Child) = E_Generic_Package
3071 and then Chars (Priv_Child) in Text_IO_Package_Name
3072 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
3073 and then Scope (Scope (Scope (Priv_Child))) =
3074 Standard_Standard
3075 then
3076 Error_Msg_NE
3077 ("& is a nested package, not a compilation unit",
3078 Name (Item), Priv_Child);
3080 else
3081 Error_Msg_N
3082 ("unit in with clause is private child unit!", Item);
3083 Error_Msg_NE
3084 ("\current unit must also have parent&!",
3085 Item, Child_Parent);
3086 end if;
3088 elsif Curr_Private
3089 or else Private_Present (Item)
3090 or else Nkind (Lib_Unit) in N_Package_Body | N_Subunit
3091 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3092 and then not Acts_As_Spec (Parent (Lib_Unit)))
3093 then
3094 null;
3096 else
3097 Error_Msg_NE
3098 ("current unit must also be private descendant of&",
3099 Item, Child_Parent);
3100 end if;
3101 end;
3102 end if;
3104 Next (Item);
3105 end loop;
3106 end Check_Private_Child_Unit;
3108 ----------------------
3109 -- Check_Stub_Level --
3110 ----------------------
3112 procedure Check_Stub_Level (N : Node_Id) is
3113 Par : constant Node_Id := Parent (N);
3114 Kind : constant Node_Kind := Nkind (Par);
3116 begin
3117 if Kind in
3118 N_Package_Body | N_Subprogram_Body | N_Task_Body | N_Protected_Body
3119 and then Nkind (Parent (Par)) in N_Compilation_Unit | N_Subunit
3120 then
3121 null;
3123 -- In an instance, a missing stub appears at any level. A warning
3124 -- message will have been emitted already for the missing file.
3126 elsif not In_Instance then
3127 Error_Msg_N ("stub cannot appear in an inner scope", N);
3129 elsif Expander_Active then
3130 Error_Msg_N ("missing proper body", N);
3131 end if;
3132 end Check_Stub_Level;
3134 -------------------
3135 -- Decorate_Type --
3136 -------------------
3138 procedure Decorate_Type
3139 (Ent : Entity_Id;
3140 Scop : Entity_Id;
3141 Is_Tagged : Boolean := False;
3142 Materialize : Boolean := False)
3144 CW_Typ : Entity_Id;
3146 begin
3147 -- An unanalyzed type or a shadow entity of a type is treated as an
3148 -- incomplete type, and carries the corresponding attributes.
3150 Mutate_Ekind (Ent, E_Incomplete_Type);
3151 Set_Etype (Ent, Ent);
3152 Set_Full_View (Ent, Empty);
3153 Set_Is_First_Subtype (Ent);
3154 Set_Scope (Ent, Scop);
3155 Set_Stored_Constraint (Ent, No_Elist);
3156 Reinit_Size_Align (Ent);
3158 if From_Limited_With (Ent) then
3159 Set_Private_Dependents (Ent, New_Elmt_List);
3160 end if;
3162 -- A tagged type and its corresponding shadow entity share one common
3163 -- class-wide type. The list of primitive operations for the shadow
3164 -- entity is empty.
3166 if Is_Tagged then
3167 Set_Is_Tagged_Type (Ent);
3168 Set_Direct_Primitive_Operations (Ent, New_Elmt_List);
3170 CW_Typ :=
3171 New_External_Entity
3172 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
3174 Set_Class_Wide_Type (Ent, CW_Typ);
3176 -- Set parent to be the same as the parent of the tagged type.
3177 -- We need a parent field set, and it is supposed to point to
3178 -- the declaration of the type. The tagged type declaration
3179 -- essentially declares two separate types, the tagged type
3180 -- itself and the corresponding class-wide type, so it is
3181 -- reasonable for the parent fields to point to the declaration
3182 -- in both cases.
3184 Set_Parent (CW_Typ, Parent (Ent));
3186 Mutate_Ekind (CW_Typ, E_Class_Wide_Type);
3187 Set_Class_Wide_Type (CW_Typ, CW_Typ);
3188 Set_Etype (CW_Typ, Ent);
3189 Set_Equivalent_Type (CW_Typ, Empty);
3190 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
3191 Set_Has_Unknown_Discriminants (CW_Typ);
3192 Set_Is_First_Subtype (CW_Typ);
3193 Set_Is_Tagged_Type (CW_Typ);
3194 Set_Materialize_Entity (CW_Typ, Materialize);
3195 Set_Scope (CW_Typ, Scop);
3196 Reinit_Size_Align (CW_Typ);
3197 end if;
3198 end Decorate_Type;
3200 ------------------------
3201 -- Expand_With_Clause --
3202 ------------------------
3204 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
3205 Loc : constant Source_Ptr := Sloc (Nam);
3207 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
3208 -- Build name to be used in implicit with_clause. In most cases this
3209 -- is the source name, but if renamings are present we must make the
3210 -- original unit visible, not the one it renames. The entity in the
3211 -- with clause is the renamed unit, but the identifier is the one from
3212 -- the source, which allows us to recover the unit renaming.
3214 ---------------------
3215 -- Build_Unit_Name --
3216 ---------------------
3218 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
3219 Ent : Entity_Id;
3220 Result : Node_Id;
3222 begin
3223 if Nkind (Nam) = N_Identifier then
3224 return New_Occurrence_Of (Entity (Nam), Loc);
3226 else
3227 Ent := Entity (Nam);
3229 if Present (Entity (Selector_Name (Nam)))
3230 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
3231 and then
3232 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam)))) =
3233 N_Package_Renaming_Declaration
3234 then
3235 -- The name in the with_clause is of the form A.B.C, and B is
3236 -- given by a renaming declaration. In that case we may not
3237 -- have analyzed the unit for B, but replaced it directly in
3238 -- lib-load with the unit it renames. We have to make A.B
3239 -- visible, so analyze the declaration for B now, in case it
3240 -- has not been done yet.
3242 Ent := Entity (Selector_Name (Nam));
3243 Analyze
3244 (Parent
3245 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3246 end if;
3248 Result :=
3249 Make_Expanded_Name (Loc,
3250 Chars => Chars (Entity (Nam)),
3251 Prefix => Build_Unit_Name (Prefix (Nam)),
3252 Selector_Name => New_Occurrence_Of (Ent, Loc));
3253 Set_Entity (Result, Ent);
3255 return Result;
3256 end if;
3257 end Build_Unit_Name;
3259 -- Local variables
3261 Ent : constant Entity_Id := Entity (Nam);
3262 Withn : Node_Id;
3264 -- Start of processing for Expand_With_Clause
3266 begin
3267 Withn :=
3268 Make_With_Clause (Loc,
3269 Name => Build_Unit_Name (Nam));
3271 Set_Corresponding_Spec (Withn, Ent);
3272 Set_First_Name (Withn);
3273 Set_Implicit_With (Withn);
3274 Set_Library_Unit (Withn, Parent (Unit_Declaration_Node (Ent)));
3275 Set_Parent_With (Withn);
3277 -- If the unit is a [generic] package or subprogram declaration
3278 -- (including a subprogram body acting as spec), a private_with_clause
3279 -- on a child unit implies that the implicit with on the parent is also
3280 -- private.
3282 if Nkind (Unit (N)) in N_Generic_Package_Declaration
3283 | N_Package_Declaration
3284 | N_Generic_Subprogram_Declaration
3285 | N_Subprogram_Declaration
3286 | N_Subprogram_Body
3287 then
3288 Set_Private_Present (Withn, Private_Present (Item));
3289 end if;
3291 Prepend (Withn, Context_Items (N));
3292 Mark_Rewrite_Insertion (Withn);
3294 Install_With_Clause (Withn);
3296 -- If we have "with X.Y;", we want to recurse on "X", except in the
3297 -- unusual case where X.Y is a renaming of X. In that case, the scope
3298 -- of X will be null.
3300 if Nkind (Nam) = N_Expanded_Name
3301 and then Present (Scope (Entity (Prefix (Nam))))
3302 then
3303 Expand_With_Clause (Item, Prefix (Nam), N);
3304 end if;
3305 end Expand_With_Clause;
3307 --------------------------------
3308 -- Generate_Parent_References --
3309 --------------------------------
3311 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
3312 Pref : Node_Id;
3313 P_Name : Entity_Id := P_Id;
3315 begin
3316 if Nkind (N) = N_Subunit then
3317 Pref := Name (N);
3318 else
3319 Pref := Name (Parent (Defining_Entity (N)));
3320 end if;
3322 if Nkind (Pref) = N_Expanded_Name then
3324 -- Done already, if the unit has been compiled indirectly as
3325 -- part of the closure of its context because of inlining.
3327 return;
3328 end if;
3330 while Nkind (Pref) = N_Selected_Component loop
3331 Change_Selected_Component_To_Expanded_Name (Pref);
3332 Set_Entity (Pref, P_Name);
3333 Set_Etype (Pref, Etype (P_Name));
3334 Generate_Reference (P_Name, Pref, 'r');
3335 Pref := Prefix (Pref);
3336 P_Name := Scope (P_Name);
3337 end loop;
3339 -- The guard here on P_Name is to handle the error condition where
3340 -- the parent unit is missing because the file was not found.
3342 if Present (P_Name) then
3343 Set_Entity (Pref, P_Name);
3344 Set_Etype (Pref, Etype (P_Name));
3345 Generate_Reference (P_Name, Pref, 'r');
3346 Style.Check_Identifier (Pref, P_Name);
3347 end if;
3348 end Generate_Parent_References;
3350 ---------------------
3351 -- Has_With_Clause --
3352 ---------------------
3354 function Has_With_Clause
3355 (C_Unit : Node_Id;
3356 Pack : Entity_Id;
3357 Is_Limited : Boolean := False) return Boolean
3359 Item : Node_Id;
3361 function Named_Unit (Clause : Node_Id) return Entity_Id;
3362 -- Return the entity for the unit named in a [limited] with clause
3364 ----------------
3365 -- Named_Unit --
3366 ----------------
3368 function Named_Unit (Clause : Node_Id) return Entity_Id is
3369 begin
3370 if Nkind (Name (Clause)) = N_Selected_Component then
3371 return Entity (Selector_Name (Name (Clause)));
3372 else
3373 return Entity (Name (Clause));
3374 end if;
3375 end Named_Unit;
3377 -- Start of processing for Has_With_Clause
3379 begin
3380 Item := First (Context_Items (C_Unit));
3381 while Present (Item) loop
3382 if Nkind (Item) = N_With_Clause
3383 and then Limited_Present (Item) = Is_Limited
3384 and then Named_Unit (Item) = Pack
3385 then
3386 return True;
3387 end if;
3389 Next (Item);
3390 end loop;
3392 return False;
3393 end Has_With_Clause;
3395 -----------------------------
3396 -- Implicit_With_On_Parent --
3397 -----------------------------
3399 procedure Implicit_With_On_Parent
3400 (Child_Unit : Node_Id;
3401 N : Node_Id)
3403 Loc : constant Source_Ptr := Sloc (N);
3404 P : constant Node_Id := Parent_Spec (Child_Unit);
3405 P_Unit : Node_Id := Unit (P);
3406 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3407 Withn : Node_Id;
3409 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3410 -- Build prefix of child unit name. Recurse if needed
3412 function Build_Unit_Name return Node_Id;
3413 -- If the unit is a child unit, build qualified name with all ancestors
3415 -------------------------
3416 -- Build_Ancestor_Name --
3417 -------------------------
3419 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3420 P_Ref : constant Node_Id :=
3421 New_Occurrence_Of (Defining_Entity (P), Loc);
3422 P_Spec : Node_Id := P;
3424 begin
3425 -- Ancestor may have been rewritten as a package body. Retrieve the
3426 -- original spec to trace earlier ancestors.
3428 if Nkind (P) = N_Package_Body
3429 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3430 then
3431 P_Spec := Original_Node (P);
3432 end if;
3434 if No (Parent_Spec (P_Spec)) then
3435 return P_Ref;
3436 else
3437 return
3438 Make_Selected_Component (Loc,
3439 Prefix =>
3440 Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3441 Selector_Name => P_Ref);
3442 end if;
3443 end Build_Ancestor_Name;
3445 ---------------------
3446 -- Build_Unit_Name --
3447 ---------------------
3449 function Build_Unit_Name return Node_Id is
3450 Result : Node_Id;
3452 begin
3453 if No (Parent_Spec (P_Unit)) then
3454 return New_Occurrence_Of (P_Name, Loc);
3456 else
3457 Result :=
3458 Make_Expanded_Name (Loc,
3459 Chars => Chars (P_Name),
3460 Prefix =>
3461 Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3462 Selector_Name => New_Occurrence_Of (P_Name, Loc));
3463 Set_Entity (Result, P_Name);
3465 return Result;
3466 end if;
3467 end Build_Unit_Name;
3469 -- Start of processing for Implicit_With_On_Parent
3471 begin
3472 -- The unit of the current compilation may be a package body that
3473 -- replaces an instance node. In this case we need the original instance
3474 -- node to construct the proper parent name.
3476 if Nkind (P_Unit) = N_Package_Body
3477 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3478 then
3479 P_Unit := Original_Node (P_Unit);
3480 end if;
3482 -- We add the implicit with if the child unit is the current unit being
3483 -- compiled. If the current unit is a body, we do not want to add an
3484 -- implicit_with a second time to the corresponding spec.
3486 if Nkind (Child_Unit) = N_Package_Declaration
3487 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3488 then
3489 return;
3490 end if;
3492 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3494 Set_Corresponding_Spec (Withn, P_Name);
3495 Set_First_Name (Withn);
3496 Set_Implicit_With (Withn);
3497 Set_Library_Unit (Withn, P);
3498 Set_Parent_With (Withn);
3500 -- Node is placed at the beginning of the context items, so that
3501 -- subsequent use clauses on the parent can be validated.
3503 Prepend (Withn, Context_Items (N));
3504 Mark_Rewrite_Insertion (Withn);
3506 Install_With_Clause (Withn);
3508 if Is_Child_Spec (P_Unit) then
3509 Implicit_With_On_Parent (P_Unit, N);
3510 end if;
3511 end Implicit_With_On_Parent;
3513 --------------
3514 -- In_Chain --
3515 --------------
3517 function In_Chain (E : Entity_Id) return Boolean is
3518 H : Entity_Id;
3520 begin
3521 H := Current_Entity (E);
3522 while Present (H) loop
3523 if H = E then
3524 return True;
3525 else
3526 H := Homonym (H);
3527 end if;
3528 end loop;
3530 return False;
3531 end In_Chain;
3533 ---------------------
3534 -- Install_Context --
3535 ---------------------
3537 procedure Install_Context (N : Node_Id; Chain : Boolean := True) is
3538 Lib_Unit : constant Node_Id := Unit (N);
3540 begin
3541 Install_Context_Clauses (N, Chain);
3543 if Is_Child_Spec (Lib_Unit) then
3544 Install_Parents
3545 (Lib_Unit => Lib_Unit,
3546 Is_Private => Private_Present (Parent (Lib_Unit)),
3547 Chain => Chain);
3548 end if;
3550 Install_Limited_Context_Clauses (N);
3551 end Install_Context;
3553 -----------------------------
3554 -- Install_Context_Clauses --
3555 -----------------------------
3557 procedure Install_Context_Clauses (N : Node_Id; Chain : Boolean := True) is
3558 Lib_Unit : constant Node_Id := Unit (N);
3559 Item : Node_Id;
3560 Uname_Node : Entity_Id;
3561 Check_Private : Boolean := False;
3562 Decl_Node : Node_Id;
3563 Lib_Parent : Entity_Id;
3565 begin
3566 -- First skip configuration pragmas at the start of the context. They
3567 -- are not technically part of the context clause, but that's where the
3568 -- parser puts them. Note they were analyzed in Analyze_Context.
3570 Item := First (Context_Items (N));
3571 while Present (Item)
3572 and then Nkind (Item) = N_Pragma
3573 and then Pragma_Name (Item) in Configuration_Pragma_Names
3574 loop
3575 Next (Item);
3576 end loop;
3578 -- Loop through the actual context clause items. We process everything
3579 -- except Limited_With clauses in this routine. Limited_With clauses
3580 -- are separately installed (see Install_Limited_Context_Clauses).
3582 while Present (Item) loop
3584 -- Case of explicit WITH clause
3586 if Nkind (Item) = N_With_Clause
3587 and then not Implicit_With (Item)
3588 then
3589 if Limited_Present (Item) then
3591 -- Limited withed units will be installed later
3593 goto Continue;
3595 -- If Name (Item) is not an entity name, something is wrong, and
3596 -- this will be detected in due course, for now ignore the item
3598 elsif not Is_Entity_Name (Name (Item)) then
3599 goto Continue;
3601 elsif No (Entity (Name (Item))) then
3602 Set_Entity (Name (Item), Any_Id);
3603 goto Continue;
3604 end if;
3606 Uname_Node := Entity (Name (Item));
3608 if Is_Private_Descendant (Uname_Node) then
3609 Check_Private := True;
3610 end if;
3612 Install_With_Clause (Item);
3614 Decl_Node := Unit_Declaration_Node (Uname_Node);
3616 -- If the unit is a subprogram instance, it appears nested within
3617 -- a package that carries the parent information.
3619 if Is_Generic_Instance (Uname_Node)
3620 and then Ekind (Uname_Node) /= E_Package
3621 then
3622 Decl_Node := Parent (Parent (Decl_Node));
3623 end if;
3625 if Is_Child_Spec (Decl_Node) then
3626 if Nkind (Name (Item)) = N_Expanded_Name then
3627 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3628 else
3629 -- If not an expanded name, the child unit must be a
3630 -- renaming, nothing to do.
3632 null;
3633 end if;
3635 elsif Nkind (Decl_Node) = N_Subprogram_Body
3636 and then not Acts_As_Spec (Parent (Decl_Node))
3637 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3638 then
3639 Implicit_With_On_Parent
3640 (Unit (Library_Unit (Parent (Decl_Node))), N);
3641 end if;
3643 -- Check license conditions unless this is a dummy unit
3645 if Sloc (Library_Unit (Item)) /= No_Location then
3646 License_Check : declare
3647 Withu : constant Unit_Number_Type :=
3648 Get_Source_Unit (Library_Unit (Item));
3649 Withl : constant License_Type :=
3650 License (Source_Index (Withu));
3651 Unitl : constant License_Type :=
3652 License (Source_Index (Current_Sem_Unit));
3654 procedure License_Error;
3655 -- Signal error of bad license
3657 -------------------
3658 -- License_Error --
3659 -------------------
3661 procedure License_Error is
3662 begin
3663 Error_Msg_N
3664 ("license of withed unit & may be inconsistent??",
3665 Name (Item));
3666 end License_Error;
3668 -- Start of processing for License_Check
3670 begin
3671 -- Exclude license check if withed unit is an internal unit.
3672 -- This situation arises e.g. with the GPL version of GNAT.
3674 if Is_Internal_Unit (Withu) then
3675 null;
3677 -- Otherwise check various cases
3678 else
3679 case Unitl is
3680 when Unknown =>
3681 null;
3683 when Restricted =>
3684 if Withl = GPL then
3685 License_Error;
3686 end if;
3688 when GPL =>
3689 if Withl = Restricted then
3690 License_Error;
3691 end if;
3693 when Modified_GPL =>
3694 if Withl = Restricted or else Withl = GPL then
3695 License_Error;
3696 end if;
3698 when Unrestricted =>
3699 null;
3700 end case;
3701 end if;
3702 end License_Check;
3703 end if;
3705 -- Case of USE PACKAGE clause
3707 elsif Nkind (Item) = N_Use_Package_Clause then
3708 Analyze_Use_Package (Item, Chain);
3710 -- Case of USE TYPE clause
3712 elsif Nkind (Item) = N_Use_Type_Clause then
3713 Analyze_Use_Type (Item, Chain);
3715 -- case of PRAGMA
3717 elsif Nkind (Item) = N_Pragma then
3718 Analyze (Item);
3719 end if;
3721 <<Continue>>
3722 Next (Item);
3723 end loop;
3725 if Is_Child_Spec (Lib_Unit) then
3727 -- The unit also has implicit with_clauses on its own parents
3729 if No (Context_Items (N)) then
3730 Set_Context_Items (N, New_List);
3731 end if;
3733 Implicit_With_On_Parent (Lib_Unit, N);
3734 end if;
3736 -- If the unit is a body, the context of the specification must also
3737 -- be installed. That includes private with_clauses in that context.
3739 if Nkind (Lib_Unit) = N_Package_Body
3740 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3741 and then not Acts_As_Spec (N))
3742 then
3743 Install_Context (Library_Unit (N), Chain);
3745 -- Only install private with-clauses of a spec that comes from
3746 -- source, excluding specs created for a subprogram body that is
3747 -- a child unit.
3749 if Comes_From_Source (Library_Unit (N)) then
3750 Install_Private_With_Clauses
3751 (Defining_Entity (Unit (Library_Unit (N))));
3752 end if;
3754 if Is_Child_Spec (Unit (Library_Unit (N))) then
3756 -- If the unit is the body of a public child unit, the private
3757 -- declarations of the parent must be made visible. If the child
3758 -- unit is private, the private declarations have been installed
3759 -- already in the call to Install_Parents for the spec. Installing
3760 -- private declarations must be done for all ancestors of public
3761 -- child units. In addition, sibling units mentioned in the
3762 -- context clause of the body are directly visible.
3764 declare
3765 Lib_Spec : Node_Id;
3766 P : Node_Id;
3767 P_Name : Entity_Id;
3769 begin
3770 Lib_Spec := Unit (Library_Unit (N));
3771 while Is_Child_Spec (Lib_Spec) loop
3772 P := Unit (Parent_Spec (Lib_Spec));
3773 P_Name := Defining_Entity (P);
3775 if not (Private_Present (Parent (Lib_Spec)))
3776 and then not In_Private_Part (P_Name)
3777 then
3778 Install_Private_Declarations (P_Name);
3779 Install_Private_With_Clauses (P_Name);
3780 Set_Use (Private_Declarations (Specification (P)));
3781 end if;
3783 Lib_Spec := P;
3784 end loop;
3785 end;
3786 end if;
3788 -- For a package body, children in context are immediately visible
3790 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3791 end if;
3793 if Nkind (Lib_Unit) in N_Generic_Package_Declaration
3794 | N_Generic_Subprogram_Declaration
3795 | N_Package_Declaration
3796 | N_Subprogram_Declaration
3797 then
3798 if Is_Child_Spec (Lib_Unit) then
3799 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3800 Set_Is_Private_Descendant
3801 (Defining_Entity (Lib_Unit),
3802 Is_Private_Descendant (Lib_Parent)
3803 or else Private_Present (Parent (Lib_Unit)));
3805 else
3806 Set_Is_Private_Descendant
3807 (Defining_Entity (Lib_Unit),
3808 Private_Present (Parent (Lib_Unit)));
3809 end if;
3810 end if;
3812 if Check_Private then
3813 Check_Private_Child_Unit (N);
3814 end if;
3815 end Install_Context_Clauses;
3817 -------------------------------------
3818 -- Install_Limited_Context_Clauses --
3819 -------------------------------------
3821 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3822 Item : Node_Id;
3824 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3825 -- Check that the unlimited view of a given compilation_unit is not
3826 -- already visible through "use + renamings".
3828 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3829 -- Check that if a limited_with clause of a given compilation_unit
3830 -- mentions a descendant of a private child of some library unit, then
3831 -- the given compilation_unit must be the declaration of a private
3832 -- descendant of that library unit, or a public descendant of such. The
3833 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3834 -- use entities on the limited with_clauses because their units have not
3835 -- been analyzed, so we have to climb the tree of ancestors looking for
3836 -- private keywords.
3838 procedure Expand_Limited_With_Clause
3839 (Comp_Unit : Node_Id;
3840 Nam : Node_Id;
3841 N : Node_Id);
3842 -- If a child unit appears in a limited_with clause, there are implicit
3843 -- limited_with clauses on all parents that are not already visible
3844 -- through a regular with clause. This procedure creates the implicit
3845 -- limited with_clauses for the parents and loads the corresponding
3846 -- units. The shadow entities are created when the inserted clause is
3847 -- analyzed. Implements Ada 2005 (AI-50217).
3849 ---------------------
3850 -- Check_Renamings --
3851 ---------------------
3853 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3854 Item : Node_Id;
3855 Spec : Node_Id;
3856 WEnt : Entity_Id;
3857 E : Entity_Id;
3858 E2 : Entity_Id;
3860 begin
3861 pragma Assert (Nkind (W) = N_With_Clause);
3863 -- Protect the frontend against previous critical errors
3865 case Nkind (Unit (Library_Unit (W))) is
3866 when N_Generic_Package_Declaration
3867 | N_Generic_Subprogram_Declaration
3868 | N_Package_Declaration
3869 | N_Subprogram_Declaration
3871 null;
3873 when others =>
3874 return;
3875 end case;
3877 -- Check "use + renamings"
3879 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3880 Spec := Specification (Unit (P));
3882 Item := First (Visible_Declarations (Spec));
3883 while Present (Item) loop
3885 -- Look only at use package clauses
3887 if Nkind (Item) = N_Use_Package_Clause then
3889 E := Entity (Name (Item));
3891 pragma Assert (Present (Parent (E)));
3893 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3894 and then Renamed_Entity (E) = WEnt
3895 then
3896 -- The unlimited view is visible through use clause and
3897 -- renamings. There is no need to generate the error
3898 -- message here because Is_Visible_Through_Renamings
3899 -- takes care of generating the precise error message.
3901 return;
3903 elsif Nkind (Parent (E)) = N_Package_Specification then
3905 -- The use clause may refer to a local package.
3906 -- Check all the enclosing scopes.
3908 E2 := E;
3909 while E2 /= Standard_Standard and then E2 /= WEnt loop
3910 E2 := Scope (E2);
3911 end loop;
3913 if E2 = WEnt then
3914 Error_Msg_N
3915 ("unlimited view visible through use clause", W);
3916 return;
3917 end if;
3918 end if;
3919 end if;
3921 Next (Item);
3922 end loop;
3924 -- Recursive call to check all the ancestors
3926 if Is_Child_Spec (Unit (P)) then
3927 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3928 end if;
3929 end Check_Renamings;
3931 ---------------------------------------
3932 -- Check_Private_Limited_Withed_Unit --
3933 ---------------------------------------
3935 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3936 Curr_Parent : Node_Id;
3937 Child_Parent : Node_Id;
3938 Curr_Private : Boolean;
3940 begin
3941 -- Compilation unit of the parent of the withed library unit
3943 Child_Parent := Library_Unit (Item);
3945 -- If the child unit is a public child, then locate its nearest
3946 -- private ancestor, if any, then Child_Parent will then be set to
3947 -- the parent of that ancestor.
3949 if not Private_Present (Library_Unit (Item)) then
3950 while Present (Child_Parent)
3951 and then not Private_Present (Child_Parent)
3952 loop
3953 Child_Parent := Parent_Spec (Unit (Child_Parent));
3954 end loop;
3956 if No (Child_Parent) then
3957 return;
3958 end if;
3959 end if;
3961 Child_Parent := Parent_Spec (Unit (Child_Parent));
3963 -- Traverse all the ancestors of the current compilation unit to
3964 -- check if it is a descendant of named library unit.
3966 Curr_Parent := Parent (Item);
3967 Curr_Private := Private_Present (Curr_Parent);
3969 while Present (Parent_Spec (Unit (Curr_Parent)))
3970 and then Curr_Parent /= Child_Parent
3971 loop
3972 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3973 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3974 end loop;
3976 if Curr_Parent /= Child_Parent then
3977 Error_Msg_N
3978 ("unit in with clause is private child unit!", Item);
3979 Error_Msg_NE
3980 ("\current unit must also have parent&!",
3981 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3983 elsif Private_Present (Parent (Item))
3984 or else Curr_Private
3985 or else Private_Present (Item)
3986 or else Nkind (Unit (Parent (Item))) in
3987 N_Package_Body | N_Subprogram_Body | N_Subunit
3988 then
3989 -- Current unit is private, of descendant of a private unit
3991 null;
3993 else
3994 Error_Msg_NE
3995 ("current unit must also be private descendant of&",
3996 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3997 end if;
3998 end Check_Private_Limited_Withed_Unit;
4000 --------------------------------
4001 -- Expand_Limited_With_Clause --
4002 --------------------------------
4004 procedure Expand_Limited_With_Clause
4005 (Comp_Unit : Node_Id;
4006 Nam : Node_Id;
4007 N : Node_Id)
4009 Loc : constant Source_Ptr := Sloc (Nam);
4010 Unum : Unit_Number_Type;
4011 Withn : Node_Id;
4013 function Previous_Withed_Unit (W : Node_Id) return Boolean;
4014 -- Returns true if the context already includes a with_clause for
4015 -- this unit. If the with_clause is nonlimited, the unit is fully
4016 -- visible and an implicit limited_with should not be created. If
4017 -- there is already a limited_with clause for W, a second one is
4018 -- simply redundant.
4020 --------------------------
4021 -- Previous_Withed_Unit --
4022 --------------------------
4024 function Previous_Withed_Unit (W : Node_Id) return Boolean is
4025 Item : Node_Id;
4027 begin
4028 -- A limited with_clause cannot appear in the same context_clause
4029 -- as a nonlimited with_clause which mentions the same library.
4031 Item := First (Context_Items (Comp_Unit));
4032 while Present (Item) loop
4033 if Nkind (Item) = N_With_Clause
4034 and then Library_Unit (Item) = Library_Unit (W)
4035 then
4036 return True;
4037 end if;
4039 Next (Item);
4040 end loop;
4042 return False;
4043 end Previous_Withed_Unit;
4045 -- Start of processing for Expand_Limited_With_Clause
4047 begin
4048 if Nkind (Nam) = N_Identifier then
4050 -- Create node for name of withed unit
4052 Withn :=
4053 Make_With_Clause (Loc,
4054 Name => New_Copy (Nam));
4056 else pragma Assert (Nkind (Nam) = N_Selected_Component);
4057 Withn :=
4058 Make_With_Clause (Loc,
4059 Name => Make_Selected_Component (Loc,
4060 Prefix => New_Copy_Tree (Prefix (Nam)),
4061 Selector_Name => New_Copy (Selector_Name (Nam))));
4062 Set_Parent (Withn, Parent (N));
4063 end if;
4065 Set_First_Name (Withn);
4066 Set_Implicit_With (Withn);
4067 Set_Limited_Present (Withn);
4069 Unum :=
4070 Load_Unit
4071 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
4072 Required => True,
4073 Subunit => False,
4074 Error_Node => Nam);
4076 -- Do not generate a limited_with_clause on the current unit. This
4077 -- path is taken when a unit has a limited_with clause on one of its
4078 -- child units.
4080 if Unum = Current_Sem_Unit then
4081 return;
4082 end if;
4084 Set_Library_Unit (Withn, Cunit (Unum));
4085 Set_Corresponding_Spec
4086 (Withn, Specification (Unit (Cunit (Unum))));
4088 if not Previous_Withed_Unit (Withn) then
4089 Prepend (Withn, Context_Items (Parent (N)));
4090 Mark_Rewrite_Insertion (Withn);
4092 -- Add implicit limited_with_clauses for parents of child units
4093 -- mentioned in limited_with clauses.
4095 if Nkind (Nam) = N_Selected_Component then
4096 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
4097 end if;
4099 Analyze (Withn);
4101 if not Limited_View_Installed (Withn) then
4102 Install_Limited_With_Clause (Withn);
4103 end if;
4104 end if;
4105 end Expand_Limited_With_Clause;
4107 -- Start of processing for Install_Limited_Context_Clauses
4109 begin
4110 Item := First (Context_Items (N));
4111 while Present (Item) loop
4112 if Nkind (Item) = N_With_Clause
4113 and then Limited_Present (Item)
4114 and then not Error_Posted (Item)
4115 then
4116 if Nkind (Name (Item)) = N_Selected_Component then
4117 Expand_Limited_With_Clause
4118 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
4119 end if;
4121 Check_Private_Limited_Withed_Unit (Item);
4123 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
4124 Check_Renamings (Parent_Spec (Unit (N)), Item);
4125 end if;
4127 -- A unit may have a limited with on itself if it has a limited
4128 -- with_clause on one of its child units. In that case it is
4129 -- already being compiled and it makes no sense to install its
4130 -- limited view.
4132 -- If the item is a limited_private_with_clause, install it if the
4133 -- current unit is a body or if it is a private child. Otherwise
4134 -- the private clause is installed before analyzing the private
4135 -- part of the current unit.
4137 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
4138 and then not Limited_View_Installed (Item)
4139 and then
4140 not Is_Ancestor_Unit
4141 (Library_Unit (Item), Cunit (Current_Sem_Unit))
4142 then
4143 if not Private_Present (Item)
4144 or else Private_Present (N)
4145 or else Nkind (Unit (N)) in
4146 N_Package_Body | N_Subprogram_Body | N_Subunit
4147 then
4148 Install_Limited_With_Clause (Item);
4149 end if;
4150 end if;
4151 end if;
4153 Next (Item);
4154 end loop;
4156 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4157 -- looking for incomplete subtype declarations of incomplete types
4158 -- visible through a limited with clause.
4160 if Ada_Version >= Ada_2005
4161 and then Analyzed (N)
4162 and then Nkind (Unit (N)) = N_Package_Declaration
4163 then
4164 declare
4165 Decl : Node_Id;
4166 Def_Id : Entity_Id;
4167 Non_Lim_View : Entity_Id;
4169 begin
4170 Decl := First (Visible_Declarations (Specification (Unit (N))));
4171 while Present (Decl) loop
4172 if Nkind (Decl) = N_Subtype_Declaration
4173 and then
4174 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
4175 and then
4176 From_Limited_With (Defining_Identifier (Decl))
4177 then
4178 Def_Id := Defining_Identifier (Decl);
4179 Non_Lim_View := Non_Limited_View (Def_Id);
4181 if not Is_Incomplete_Type (Non_Lim_View) then
4183 -- Convert an incomplete subtype declaration into a
4184 -- corresponding nonlimited view subtype declaration.
4185 -- This is usually the case when analyzing a body that
4186 -- has regular with clauses, when the spec has limited
4187 -- ones.
4189 -- If the nonlimited view is still incomplete, it is
4190 -- the dummy entry already created, and the declaration
4191 -- cannot be reanalyzed. This is the case when installing
4192 -- a parent unit that has limited with-clauses.
4194 Set_Subtype_Indication (Decl,
4195 New_Occurrence_Of (Non_Lim_View, Sloc (Def_Id)));
4196 Set_Etype (Def_Id, Non_Lim_View);
4197 Mutate_Ekind
4198 (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
4199 Set_Analyzed (Decl, False);
4201 -- Reanalyze the declaration, suppressing the call to
4202 -- Enter_Name to avoid duplicate names.
4204 Analyze_Subtype_Declaration
4205 (N => Decl,
4206 Skip => True);
4207 end if;
4208 end if;
4210 Next (Decl);
4211 end loop;
4212 end;
4213 end if;
4214 end Install_Limited_Context_Clauses;
4216 ---------------------
4217 -- Install_Parents --
4218 ---------------------
4220 procedure Install_Parents
4221 (Lib_Unit : Node_Id;
4222 Is_Private : Boolean;
4223 Chain : Boolean := True)
4225 P : Node_Id;
4226 E_Name : Entity_Id;
4227 P_Name : Entity_Id;
4228 P_Spec : Node_Id;
4230 begin
4231 P := Unit (Parent_Spec (Lib_Unit));
4232 P_Name := Get_Parent_Entity (P);
4234 if Etype (P_Name) = Any_Type then
4235 return;
4236 end if;
4238 if Ekind (P_Name) = E_Generic_Package
4239 and then Nkind (Lib_Unit) not in N_Generic_Declaration
4240 | N_Generic_Renaming_Declaration
4241 then
4242 Error_Msg_N
4243 ("child of a generic package must be a generic unit", Lib_Unit);
4245 elsif not Is_Package_Or_Generic_Package (P_Name) then
4246 Error_Msg_N
4247 ("parent unit must be package or generic package", Lib_Unit);
4248 raise Unrecoverable_Error;
4250 elsif Present (Renamed_Entity (P_Name)) then
4251 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
4252 raise Unrecoverable_Error;
4254 -- Verify that a child of an instance is itself an instance, or the
4255 -- renaming of one. Given that an instance that is a unit is replaced
4256 -- with a package declaration, check against the original node. The
4257 -- parent may be currently being instantiated, in which case it appears
4258 -- as a declaration, but the generic_parent is already established
4259 -- indicating that we deal with an instance.
4261 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
4262 if Nkind (Lib_Unit) in N_Renaming_Declaration
4263 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
4264 or else
4265 (Nkind (Lib_Unit) = N_Package_Declaration
4266 and then Present (Generic_Parent (Specification (Lib_Unit))))
4267 then
4268 null;
4269 else
4270 Error_Msg_N
4271 ("child of an instance must be an instance or renaming",
4272 Lib_Unit);
4273 end if;
4274 end if;
4276 -- This is the recursive call that ensures all parents are loaded
4278 if Is_Child_Spec (P) then
4279 Install_Parents
4280 (Lib_Unit => P,
4281 Is_Private =>
4282 Is_Private or else Private_Present (Parent (Lib_Unit)),
4283 Chain => Chain);
4284 end if;
4286 -- Now we can install the context for this parent
4288 Install_Context_Clauses (Parent_Spec (Lib_Unit), Chain);
4289 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4290 Install_Siblings (P_Name, Parent (Lib_Unit));
4292 -- The child unit is in the declarative region of the parent. The parent
4293 -- must therefore appear in the scope stack and be visible, as when
4294 -- compiling the corresponding body. If the child unit is private or it
4295 -- is a package body, private declarations must be accessible as well.
4296 -- Use declarations in the parent must also be installed. Finally, other
4297 -- child units of the same parent that are in the context are
4298 -- immediately visible.
4300 -- Find entity for compilation unit, and set its private descendant
4301 -- status as needed. Indicate that it is a compilation unit, which is
4302 -- redundant in general, but needed if this is a generated child spec
4303 -- for a child body without previous spec.
4305 E_Name := Defining_Entity (Lib_Unit);
4307 Set_Is_Child_Unit (E_Name);
4308 Set_Is_Compilation_Unit (E_Name);
4310 Set_Is_Private_Descendant (E_Name,
4311 Is_Private_Descendant (P_Name)
4312 or else Private_Present (Parent (Lib_Unit)));
4314 P_Spec := Package_Specification (P_Name);
4315 Push_Scope (P_Name);
4317 -- Save current visibility of unit
4319 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4320 Is_Immediately_Visible (P_Name);
4321 Set_Is_Immediately_Visible (P_Name);
4322 Install_Visible_Declarations (P_Name);
4323 Set_Use (Visible_Declarations (P_Spec));
4325 -- If the parent is a generic unit, its formal part may contain formal
4326 -- packages and use clauses for them.
4328 if Ekind (P_Name) = E_Generic_Package then
4329 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4330 end if;
4332 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4333 Install_Private_Declarations (P_Name);
4334 Install_Private_With_Clauses (P_Name);
4335 Set_Use (Private_Declarations (P_Spec));
4336 end if;
4337 end Install_Parents;
4339 ----------------------------------
4340 -- Install_Private_With_Clauses --
4341 ----------------------------------
4343 procedure Install_Private_With_Clauses (P : Entity_Id) is
4344 Decl : constant Node_Id := Unit_Declaration_Node (P);
4345 Item : Node_Id;
4347 begin
4348 if Debug_Flag_I then
4349 Write_Str ("install private with clauses of ");
4350 Write_Name (Chars (P));
4351 Write_Eol;
4352 end if;
4354 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4355 Item := First (Context_Items (Parent (Decl)));
4356 while Present (Item) loop
4357 if Nkind (Item) = N_With_Clause
4358 and then Private_Present (Item)
4359 then
4360 -- If the unit is an ancestor of the current one, it is the
4361 -- case of a private limited with clause on a child unit, and
4362 -- the compilation of one of its descendants, In that case the
4363 -- limited view is errelevant.
4365 if Limited_Present (Item) then
4366 if not Limited_View_Installed (Item)
4367 and then
4368 not Is_Ancestor_Unit (Library_Unit (Item),
4369 Cunit (Current_Sem_Unit))
4370 then
4371 Install_Limited_With_Clause (Item);
4372 end if;
4373 else
4374 Install_With_Clause (Item, Private_With_OK => True);
4375 end if;
4376 end if;
4378 Next (Item);
4379 end loop;
4380 end if;
4381 end Install_Private_With_Clauses;
4383 ----------------------
4384 -- Install_Siblings --
4385 ----------------------
4387 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4388 Item : Node_Id;
4389 Id : Entity_Id;
4390 Prev : Entity_Id;
4392 begin
4393 -- Iterate over explicit with clauses, and check whether the scope of
4394 -- each entity is an ancestor of the current unit, in which case it is
4395 -- immediately visible.
4397 Item := First (Context_Items (N));
4398 while Present (Item) loop
4400 -- Do not install private_with_clauses declaration, unless unit
4401 -- is itself a private child unit, or is a body. Note that for a
4402 -- subprogram body the private_with_clause does not take effect
4403 -- until after the specification.
4405 if Nkind (Item) /= N_With_Clause
4406 or else Implicit_With (Item)
4407 or else Limited_Present (Item)
4408 or else Error_Posted (Item)
4410 -- Skip processing malformed trees
4412 or else (Try_Semantics
4413 and then Nkind (Name (Item)) not in N_Has_Entity)
4414 then
4415 null;
4417 elsif not Private_Present (Item)
4418 or else Private_Present (N)
4419 or else Nkind (Unit (N)) = N_Package_Body
4420 then
4421 Id := Entity (Name (Item));
4423 if Is_Child_Unit (Id)
4424 and then Is_Ancestor_Package (Scope (Id), U_Name)
4425 then
4426 Set_Is_Immediately_Visible (Id);
4428 -- Check for the presence of another unit in the context that
4429 -- may be inadvertently hidden by the child.
4431 Prev := Current_Entity (Id);
4433 if Present (Prev)
4434 and then Is_Immediately_Visible (Prev)
4435 and then not Is_Child_Unit (Prev)
4436 then
4437 declare
4438 Clause : Node_Id;
4440 begin
4441 Clause := First (Context_Items (N));
4442 while Present (Clause) loop
4443 if Nkind (Clause) = N_With_Clause
4444 and then Entity (Name (Clause)) = Prev
4445 then
4446 Error_Msg_NE
4447 ("child unit& hides compilation unit " &
4448 "with the same name??",
4449 Name (Item), Id);
4450 exit;
4451 end if;
4453 Next (Clause);
4454 end loop;
4455 end;
4456 end if;
4458 -- The With_Clause may be on a grandchild or one of its further
4459 -- descendants, which makes a child immediately visible. Examine
4460 -- ancestry to determine whether such a child exists. For example,
4461 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4462 -- is immediately visible.
4464 elsif Is_Child_Unit (Id) then
4465 declare
4466 Par : Entity_Id;
4468 begin
4469 Par := Scope (Id);
4470 while Is_Child_Unit (Par) loop
4471 if Is_Ancestor_Package (Scope (Par), U_Name) then
4472 Set_Is_Immediately_Visible (Par);
4473 exit;
4474 end if;
4476 Par := Scope (Par);
4477 end loop;
4478 end;
4479 end if;
4481 -- If the item is a private with-clause on a child unit, the parent
4482 -- may have been installed already, but the child unit must remain
4483 -- invisible until installed in a private part or body, unless there
4484 -- is already a regular with_clause for it in the current unit.
4486 elsif Private_Present (Item) then
4487 Id := Entity (Name (Item));
4489 if Is_Child_Unit (Id) then
4490 declare
4491 Clause : Node_Id;
4493 function In_Context return Boolean;
4494 -- Scan context of current unit, to check whether there is
4495 -- a with_clause on the same unit as a private with-clause
4496 -- on a parent, in which case child unit is visible. If the
4497 -- unit is a grandchild, the same applies to its parent.
4499 ----------------
4500 -- In_Context --
4501 ----------------
4503 function In_Context return Boolean is
4504 begin
4505 Clause :=
4506 First (Context_Items (Cunit (Current_Sem_Unit)));
4507 while Present (Clause) loop
4508 if Nkind (Clause) = N_With_Clause
4509 and then Comes_From_Source (Clause)
4510 and then Is_Entity_Name (Name (Clause))
4511 and then not Private_Present (Clause)
4512 then
4513 if Entity (Name (Clause)) = Id
4514 or else
4515 (Nkind (Name (Clause)) = N_Expanded_Name
4516 and then Entity (Prefix (Name (Clause))) = Id)
4517 then
4518 return True;
4519 end if;
4520 end if;
4522 Next (Clause);
4523 end loop;
4525 return False;
4526 end In_Context;
4528 begin
4529 Set_Is_Visible_Lib_Unit (Id, In_Context);
4530 end;
4531 end if;
4532 end if;
4534 Next (Item);
4535 end loop;
4536 end Install_Siblings;
4538 ---------------------------------
4539 -- Install_Limited_With_Clause --
4540 ---------------------------------
4542 procedure Install_Limited_With_Clause (N : Node_Id) is
4543 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4544 E : Entity_Id;
4545 P : Entity_Id;
4546 Is_Child_Package : Boolean := False;
4547 Lim_Header : Entity_Id;
4548 Lim_Typ : Entity_Id;
4550 procedure Check_Body_Required;
4551 -- A unit mentioned in a limited with_clause may not be mentioned in
4552 -- a regular with_clause, but must still be included in the current
4553 -- partition. We need to determine whether the unit needs a body, so
4554 -- that the binder can determine the name of the file to be compiled.
4555 -- Checking whether a unit needs a body can be done without semantic
4556 -- analysis, by examining the nature of the declarations in the package.
4558 function Has_Limited_With_Clause
4559 (C_Unit : Entity_Id;
4560 Pack : Entity_Id) return Boolean;
4561 -- Determine whether any package in the ancestor chain starting with
4562 -- C_Unit has a limited with clause for package Pack.
4564 -------------------------
4565 -- Check_Body_Required --
4566 -------------------------
4568 procedure Check_Body_Required is
4569 PA : constant List_Id :=
4570 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4572 procedure Check_Declarations (Spec : Node_Id);
4573 -- Recursive procedure that does the work and checks nested packages
4575 ------------------------
4576 -- Check_Declarations --
4577 ------------------------
4579 procedure Check_Declarations (Spec : Node_Id) is
4580 Decl : Node_Id;
4581 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4583 Subp_List : constant Elist_Id := New_Elmt_List;
4585 procedure Check_Pragma_Import (P : Node_Id);
4586 -- If a pragma import applies to a previous subprogram, the
4587 -- enclosing unit may not need a body. The processing is syntactic
4588 -- and does not require a declaration to be analyzed. The code
4589 -- below also handles pragma Import when applied to a subprogram
4590 -- that renames another. In this case the pragma applies to the
4591 -- renamed entity.
4593 -- Chains of multiple renames are not handled by the code below.
4594 -- It is probably impossible to handle all cases without proper
4595 -- name resolution. In such cases the algorithm is conservative
4596 -- and will indicate that a body is needed???
4598 -------------------------
4599 -- Check_Pragma_Import --
4600 -------------------------
4602 procedure Check_Pragma_Import (P : Node_Id) is
4603 Arg : Node_Id;
4604 Prev_Id : Elmt_Id;
4605 Subp_Id : Elmt_Id;
4606 Imported : Node_Id;
4608 procedure Remove_Homonyms (E : Node_Id);
4609 -- Make one pass over list of subprograms. Called again if
4610 -- subprogram is a renaming. E is known to be an identifier.
4612 ---------------------
4613 -- Remove_Homonyms --
4614 ---------------------
4616 procedure Remove_Homonyms (E : Node_Id) is
4617 R : Entity_Id := Empty;
4618 -- Name of renamed entity, if any
4620 begin
4621 Subp_Id := First_Elmt (Subp_List);
4622 while Present (Subp_Id) loop
4623 if Chars (Node (Subp_Id)) = Chars (E) then
4624 if Nkind (Parent (Parent (Node (Subp_Id))))
4625 /= N_Subprogram_Renaming_Declaration
4626 then
4627 Prev_Id := Subp_Id;
4628 Next_Elmt (Subp_Id);
4629 Remove_Elmt (Subp_List, Prev_Id);
4630 else
4631 R := Name (Parent (Parent (Node (Subp_Id))));
4632 exit;
4633 end if;
4634 else
4635 Next_Elmt (Subp_Id);
4636 end if;
4637 end loop;
4639 if Present (R) then
4640 if Nkind (R) = N_Identifier then
4641 Remove_Homonyms (R);
4643 elsif Nkind (R) = N_Selected_Component then
4644 Remove_Homonyms (Selector_Name (R));
4646 -- Renaming of attribute
4648 else
4649 null;
4650 end if;
4651 end if;
4652 end Remove_Homonyms;
4654 -- Start of processing for Check_Pragma_Import
4656 begin
4657 -- Find name of entity in Import pragma. We have not analyzed
4658 -- the construct, so we must guard against syntax errors.
4660 Arg := Next (First (Pragma_Argument_Associations (P)));
4662 if No (Arg)
4663 or else Nkind (Expression (Arg)) /= N_Identifier
4664 then
4665 return;
4666 else
4667 Imported := Expression (Arg);
4668 end if;
4670 Remove_Homonyms (Imported);
4671 end Check_Pragma_Import;
4673 -- Start of processing for Check_Declarations
4675 begin
4676 -- Search for Elaborate Body pragma
4678 Decl := First (Visible_Declarations (Spec));
4679 while Present (Decl)
4680 and then Nkind (Decl) = N_Pragma
4681 loop
4682 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4683 Set_Body_Required (Library_Unit (N));
4684 return;
4685 end if;
4687 Next (Decl);
4688 end loop;
4690 -- Look for declarations that require the presence of a body. We
4691 -- have already skipped pragmas at the start of the list.
4693 while Present (Decl) loop
4695 -- Subprogram that comes from source means body may be needed.
4696 -- Save for subsequent examination of import pragmas.
4698 if Comes_From_Source (Decl)
4699 and then (Nkind (Decl) in N_Subprogram_Declaration
4700 | N_Subprogram_Renaming_Declaration
4701 | N_Generic_Subprogram_Declaration)
4702 then
4703 Append_Elmt (Defining_Entity (Decl), Subp_List);
4705 -- Package declaration of generic package declaration. We need
4706 -- to recursively examine nested declarations.
4708 elsif Nkind (Decl) in N_Package_Declaration
4709 | N_Generic_Package_Declaration
4710 then
4711 Check_Declarations (Specification (Decl));
4713 elsif Nkind (Decl) = N_Pragma
4714 and then Pragma_Name (Decl) = Name_Import
4715 then
4716 Check_Pragma_Import (Decl);
4717 end if;
4719 Next (Decl);
4720 end loop;
4722 -- Same set of tests for private part. In addition to subprograms
4723 -- detect the presence of Taft Amendment types (incomplete types
4724 -- completed in the body).
4726 Decl := First (Private_Declarations (Spec));
4727 while Present (Decl) loop
4728 if Comes_From_Source (Decl)
4729 and then Nkind (Decl) in N_Subprogram_Declaration
4730 | N_Subprogram_Renaming_Declaration
4731 | N_Generic_Subprogram_Declaration
4732 then
4733 Append_Elmt (Defining_Entity (Decl), Subp_List);
4735 elsif Nkind (Decl) in N_Package_Declaration
4736 | N_Generic_Package_Declaration
4737 then
4738 Check_Declarations (Specification (Decl));
4740 -- Collect incomplete type declarations for separate pass
4742 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4743 Append_Elmt (Decl, Incomplete_Decls);
4745 elsif Nkind (Decl) = N_Pragma
4746 and then Pragma_Name (Decl) = Name_Import
4747 then
4748 Check_Pragma_Import (Decl);
4749 end if;
4751 Next (Decl);
4752 end loop;
4754 -- Now check incomplete declarations to locate Taft amendment
4755 -- types. This can be done by examining the defining identifiers
4756 -- of type declarations without real semantic analysis.
4758 declare
4759 Inc : Elmt_Id;
4761 begin
4762 Inc := First_Elmt (Incomplete_Decls);
4763 while Present (Inc) loop
4764 Decl := Next (Node (Inc));
4765 while Present (Decl) loop
4766 if Nkind (Decl) = N_Full_Type_Declaration
4767 and then Chars (Defining_Identifier (Decl)) =
4768 Chars (Defining_Identifier (Node (Inc)))
4769 then
4770 exit;
4771 end if;
4773 Next (Decl);
4774 end loop;
4776 -- If no completion, this is a TAT, and a body is needed
4778 if No (Decl) then
4779 Set_Body_Required (Library_Unit (N));
4780 return;
4781 end if;
4783 Next_Elmt (Inc);
4784 end loop;
4785 end;
4787 -- Finally, check whether there are subprograms that still require
4788 -- a body, i.e. are not renamings or null.
4790 if not Is_Empty_Elmt_List (Subp_List) then
4791 declare
4792 Subp_Id : Elmt_Id;
4793 Spec : Node_Id;
4795 begin
4796 Subp_Id := First_Elmt (Subp_List);
4797 Spec := Parent (Node (Subp_Id));
4799 while Present (Subp_Id) loop
4800 if Nkind (Parent (Spec))
4801 = N_Subprogram_Renaming_Declaration
4802 then
4803 null;
4805 elsif Nkind (Spec) = N_Procedure_Specification
4806 and then Null_Present (Spec)
4807 then
4808 null;
4810 else
4811 Set_Body_Required (Library_Unit (N));
4812 return;
4813 end if;
4815 Next_Elmt (Subp_Id);
4816 end loop;
4817 end;
4818 end if;
4819 end Check_Declarations;
4821 -- Start of processing for Check_Body_Required
4823 begin
4824 -- If this is an imported package (Java and CIL usage) no body is
4825 -- needed. Scan list of pragmas that may follow a compilation unit
4826 -- to look for a relevant pragma Import.
4828 if Present (PA) then
4829 declare
4830 Prag : Node_Id;
4832 begin
4833 Prag := First (PA);
4834 while Present (Prag) loop
4835 if Nkind (Prag) = N_Pragma
4836 and then Get_Pragma_Id (Prag) = Pragma_Import
4837 then
4838 return;
4839 end if;
4841 Next (Prag);
4842 end loop;
4843 end;
4844 end if;
4846 Check_Declarations (Specification (P_Unit));
4847 end Check_Body_Required;
4849 -----------------------------
4850 -- Has_Limited_With_Clause --
4851 -----------------------------
4853 function Has_Limited_With_Clause
4854 (C_Unit : Entity_Id;
4855 Pack : Entity_Id) return Boolean
4857 Par : Entity_Id;
4858 Par_Unit : Node_Id;
4860 begin
4861 Par := C_Unit;
4862 while Present (Par) loop
4863 if Ekind (Par) /= E_Package then
4864 exit;
4865 end if;
4867 -- Retrieve the Compilation_Unit node for Par and determine if
4868 -- its context clauses contain a limited with for Pack.
4870 Par_Unit := Parent (Parent (Parent (Par)));
4872 if Nkind (Par_Unit) = N_Package_Declaration then
4873 Par_Unit := Parent (Par_Unit);
4874 end if;
4876 if Has_With_Clause (Par_Unit, Pack, True) then
4877 return True;
4878 end if;
4880 -- If there are more ancestors, climb up the tree, otherwise we
4881 -- are done.
4883 if Is_Child_Unit (Par) then
4884 Par := Scope (Par);
4885 else
4886 exit;
4887 end if;
4888 end loop;
4890 return False;
4891 end Has_Limited_With_Clause;
4893 -- Start of processing for Install_Limited_With_Clause
4895 begin
4896 pragma Assert (not Limited_View_Installed (N));
4898 -- In case of limited with_clause on subprograms, generics, instances,
4899 -- or renamings, the corresponding error was previously posted and we
4900 -- have nothing to do here. If the file is missing altogether, it has
4901 -- no source location.
4903 if Nkind (P_Unit) /= N_Package_Declaration
4904 or else Sloc (P_Unit) = No_Location
4905 then
4906 return;
4907 end if;
4909 P := Defining_Unit_Name (Specification (P_Unit));
4911 -- Handle child packages
4913 if Nkind (P) = N_Defining_Program_Unit_Name then
4914 Is_Child_Package := True;
4915 P := Defining_Identifier (P);
4916 end if;
4918 -- Do not install the limited-view if the context of the unit is already
4919 -- available through a regular with clause.
4921 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4922 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4923 then
4924 return;
4925 end if;
4927 -- Do not install the limited-view if the full-view is already visible
4928 -- through renaming declarations.
4930 if Is_Visible_Through_Renamings (P, N) then
4931 return;
4932 end if;
4934 -- Do not install the limited view if this is the unit being analyzed.
4935 -- This unusual case will happen when a unit has a limited_with clause
4936 -- on one of its children. The compilation of the child forces the load
4937 -- of the parent which tries to install the limited view of the child
4938 -- again. Installing the limited view must also be disabled when
4939 -- compiling the body of the child unit.
4941 if P = Cunit_Entity (Current_Sem_Unit)
4942 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4943 and then P = Main_Unit_Entity
4944 and then Is_Ancestor_Unit
4945 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
4946 then
4947 return;
4948 end if;
4950 -- This scenario is similar to the one above, the difference is that the
4951 -- compilation of sibling Par.Sib forces the load of parent Par which
4952 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4953 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4954 -- nonlimited views of all entities from Lim_Pack.
4956 -- limited with Lim_Pack; -- [1]
4957 -- package Par is ... package Lim_Pack is ...
4959 -- with Lim_Pack; -- [2]
4960 -- package Par.Sib is ... package body Par.Sib is ...
4962 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4963 -- Sem_Unit is the body of Par.Sib.
4965 if Ekind (P) = E_Package
4966 and then Ekind (Main_Unit_Entity) = E_Package
4967 and then Is_Child_Unit (Main_Unit_Entity)
4969 -- The body has a regular with clause
4971 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4972 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4974 -- One of the ancestors has a limited with clause
4976 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4977 N_Package_Specification
4978 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4979 then
4980 return;
4981 end if;
4983 -- A common use of the limited-with is to have a limited-with in the
4984 -- package spec, and a normal with in its package body. For example:
4986 -- limited with X; -- [1]
4987 -- package A is ...
4989 -- with X; -- [2]
4990 -- package body A is ...
4992 -- The compilation of A's body installs the context clauses found at [2]
4993 -- and then the context clauses of its specification (found at [1]). As
4994 -- a consequence, at [1] the specification of X has been analyzed and it
4995 -- is immediately visible. According to the semantics of limited-with
4996 -- context clauses we don't install the limited view because the full
4997 -- view of X supersedes its limited view.
4999 if Analyzed (P_Unit)
5000 and then
5001 (Is_Immediately_Visible (P)
5002 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
5003 then
5005 -- The presence of both the limited and the analyzed nonlimited view
5006 -- may also be an error, such as an illegal context for a limited
5007 -- with_clause. In that case, do not process the context item at all.
5009 if Error_Posted (N) then
5010 return;
5011 end if;
5013 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
5014 declare
5015 Item : Node_Id;
5016 begin
5017 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
5018 while Present (Item) loop
5019 if Nkind (Item) = N_With_Clause
5020 and then Comes_From_Source (Item)
5021 and then Entity (Name (Item)) = P
5022 then
5023 return;
5024 end if;
5026 Next (Item);
5027 end loop;
5028 end;
5030 -- If this is a child body, assume that the nonlimited with_clause
5031 -- appears in an ancestor. Could be refined ???
5033 if Is_Child_Unit
5034 (Defining_Entity
5035 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
5036 then
5037 return;
5038 end if;
5040 else
5042 -- If in package declaration, nonlimited view brought in from
5043 -- parent unit or some error condition.
5045 return;
5046 end if;
5047 end if;
5049 if Debug_Flag_I then
5050 Write_Str ("install limited view of ");
5051 Write_Name (Chars (P));
5052 Write_Eol;
5053 end if;
5055 -- If the unit has not been analyzed and the limited view has not been
5056 -- already installed then we install it.
5058 if not Analyzed (P_Unit) then
5059 if not In_Chain (P) then
5061 -- Minimum decoration
5063 Mutate_Ekind (P, E_Package);
5064 Set_Etype (P, Standard_Void_Type);
5065 Set_Scope (P, Standard_Standard);
5066 Set_Is_Visible_Lib_Unit (P);
5068 if Is_Child_Package then
5069 Set_Is_Child_Unit (P);
5070 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
5071 end if;
5073 -- Place entity on visibility structure
5075 Set_Homonym (P, Current_Entity (P));
5076 Set_Current_Entity (P);
5078 if Debug_Flag_I then
5079 Write_Str (" (homonym) chain ");
5080 Write_Name (Chars (P));
5081 Write_Eol;
5082 end if;
5084 -- Install the incomplete view. The first element of the limited
5085 -- view is a header (an E_Package entity) used to reference the
5086 -- first shadow entity in the private part of the package.
5088 Lim_Header := Limited_View (P);
5089 Lim_Typ := First_Entity (Lim_Header);
5091 while Present (Lim_Typ)
5092 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5093 loop
5094 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
5095 Set_Current_Entity (Lim_Typ);
5097 if Debug_Flag_I then
5098 Write_Str (" (homonym) chain ");
5099 Write_Name (Chars (Lim_Typ));
5100 Write_Eol;
5101 end if;
5103 Next_Entity (Lim_Typ);
5104 end loop;
5105 end if;
5107 -- If the unit appears in a previous regular with_clause, the regular
5108 -- entities of the public part of the withed package must be replaced
5109 -- by the shadow ones.
5111 -- This code must be kept synchronized with the code that replaces the
5112 -- shadow entities by the real entities in Remove_Limited_With_Unit,
5113 -- otherwise the contents of the homonym chains are not consistent.
5115 else
5116 -- Hide all the type entities of the public part of the package to
5117 -- avoid its usage. This is needed to cover all the subtype decla-
5118 -- rations because we do not remove them from the homonym chain.
5120 E := First_Entity (P);
5121 while Present (E) and then E /= First_Private_Entity (P) loop
5122 if Is_Type (E) then
5123 Set_Was_Hidden (E, Is_Hidden (E));
5124 Set_Is_Hidden (E);
5125 end if;
5127 Next_Entity (E);
5128 end loop;
5130 -- Replace the real entities by the shadow entities of the limited
5131 -- view. The first element of the limited view is a header that is
5132 -- used to reference the first shadow entity in the private part
5133 -- of the package. Successive elements are the limited views of the
5134 -- type (including regular incomplete types) declared in the package.
5136 Lim_Header := Limited_View (P);
5138 Lim_Typ := First_Entity (Lim_Header);
5139 while Present (Lim_Typ)
5140 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5141 loop
5142 pragma Assert (not In_Chain (Lim_Typ));
5144 -- Do not unchain nested packages and child units
5146 if Ekind (Lim_Typ) /= E_Package
5147 and then not Is_Child_Unit (Lim_Typ)
5148 then
5149 declare
5150 Non_Lim_View : constant Entity_Id :=
5151 Non_Limited_View (Lim_Typ);
5153 Prev : Entity_Id;
5155 begin
5156 Prev := Current_Entity (Lim_Typ);
5158 -- Replace Non_Lim_View in the homonyms list, so that the
5159 -- limited view becomes available.
5161 -- If the nonlimited view is a record with an anonymous
5162 -- self-referential component, the analysis of the record
5163 -- declaration creates an incomplete type with the same name
5164 -- in order to define an internal access type. The visible
5165 -- entity is now the incomplete type, and that is the one to
5166 -- replace in the visibility structure.
5168 -- Similarly, if the source already contains the incomplete
5169 -- type declaration, the limited view of the incomplete type
5170 -- is in fact never visible (AI05-129) but we have created a
5171 -- shadow entity E1 for it that points to E2, the incomplete
5172 -- type at stake. This in turn has full view E3 that is the
5173 -- full declaration, with a corresponding shadow entity E4.
5174 -- When reinstalling the limited view, the visible entity E2
5175 -- is first replaced with E1, but E4 must eventually become
5176 -- the visible entity as per the AI and thus displace E1, as
5177 -- it is replacing E3 in the homonyms list.
5179 -- regular views limited views
5181 -- * E2 (incomplete) <-- E1 (shadow)
5183 -- |
5184 -- V
5186 -- E3 (full) <-- E4 (shadow) *
5188 -- [*] denotes the visible entity (Current_Entity)
5190 if Prev = Non_Lim_View
5191 or else
5192 (Ekind (Prev) = E_Incomplete_Type
5193 and then Full_View (Prev) = Non_Lim_View)
5194 or else
5195 (Ekind (Prev) = E_Incomplete_Type
5196 and then From_Limited_With (Prev)
5197 and then
5198 Ekind (Non_Limited_View (Prev)) = E_Incomplete_Type
5199 and then
5200 Full_View (Non_Limited_View (Prev)) = Non_Lim_View)
5201 then
5202 Set_Current_Entity (Lim_Typ);
5204 else
5205 while Present (Homonym (Prev))
5206 and then Homonym (Prev) /= Non_Lim_View
5207 loop
5208 Prev := Homonym (Prev);
5209 end loop;
5211 Set_Homonym (Prev, Lim_Typ);
5212 end if;
5214 Set_Homonym (Lim_Typ, Homonym (Non_Lim_View));
5215 end;
5217 if Debug_Flag_I then
5218 Write_Str (" (homonym) chain ");
5219 Write_Name (Chars (Lim_Typ));
5220 Write_Eol;
5221 end if;
5222 end if;
5224 Next_Entity (Lim_Typ);
5225 end loop;
5226 end if;
5228 -- The package must be visible while the limited-with clause is active
5229 -- because references to the type P.T must resolve in the usual way.
5230 -- In addition, we remember that the limited-view has been installed to
5231 -- uninstall it at the point of context removal.
5233 Set_Is_Immediately_Visible (P);
5234 Set_Limited_View_Installed (N);
5236 -- If unit has not been analyzed in some previous context, check
5237 -- (imperfectly ???) whether it might need a body.
5239 if not Analyzed (P_Unit) then
5240 Check_Body_Required;
5241 end if;
5243 -- If the package in the limited_with clause is a child unit, the clause
5244 -- is unanalyzed and appears as a selected component. Recast it as an
5245 -- expanded name so that the entity can be properly set. Use entity of
5246 -- parent, if available, for higher ancestors in the name.
5248 if Nkind (Name (N)) = N_Selected_Component then
5249 declare
5250 Nam : Node_Id;
5251 Ent : Entity_Id;
5253 begin
5254 Nam := Name (N);
5255 Ent := P;
5256 while Nkind (Nam) = N_Selected_Component
5257 and then Present (Ent)
5258 loop
5259 Change_Selected_Component_To_Expanded_Name (Nam);
5261 -- Set entity of parent identifiers if the unit is a child
5262 -- unit. This ensures that the tree is properly formed from
5263 -- semantic point of view. The unit entities are not fully
5264 -- analyzed, so we need to follow unit links in the tree.
5266 Set_Entity (Nam, Ent);
5268 Nam := Prefix (Nam);
5269 Ent :=
5270 Defining_Entity
5271 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5273 -- Set entity of last ancestor
5275 if Nkind (Nam) = N_Identifier then
5276 Set_Entity (Nam, Ent);
5277 end if;
5278 end loop;
5279 end;
5280 end if;
5282 Set_Entity (Name (N), P);
5283 Set_From_Limited_With (P);
5284 end Install_Limited_With_Clause;
5286 -------------------------
5287 -- Install_With_Clause --
5288 -------------------------
5290 procedure Install_With_Clause
5291 (With_Clause : Node_Id;
5292 Private_With_OK : Boolean := False)
5294 Uname : constant Entity_Id := Entity (Name (With_Clause));
5295 P : constant Entity_Id := Scope (Uname);
5297 begin
5298 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5299 -- compiling a package declaration and the Private_With_OK flag was not
5300 -- set by the caller. These declarations will be installed later (before
5301 -- analyzing the private part of the package).
5303 if Private_Present (With_Clause)
5304 and then Nkind (Unit (Parent (With_Clause)))
5305 in N_Package_Declaration | N_Generic_Package_Declaration
5306 and then not Private_With_OK
5307 then
5308 return;
5309 end if;
5311 if Debug_Flag_I then
5312 if Private_Present (With_Clause) then
5313 Write_Str ("install private withed unit ");
5314 else
5315 Write_Str ("install withed unit ");
5316 end if;
5318 Write_Name (Chars (Uname));
5319 Write_Eol;
5320 end if;
5322 -- We do not apply the restrictions to an internal unit unless we are
5323 -- compiling the internal unit as a main unit. This check is also
5324 -- skipped for dummy units (for missing packages).
5326 if Sloc (Uname) /= No_Location
5327 and then (not Is_Internal_Unit (Current_Sem_Unit)
5328 or else Current_Sem_Unit = Main_Unit)
5329 then
5330 Check_Restricted_Unit
5331 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5332 end if;
5334 if P /= Standard_Standard then
5336 -- If the unit is not analyzed after analysis of the with clause and
5337 -- it is an instantiation then it awaits a body and is the main unit.
5338 -- Its appearance in the context of some other unit indicates a
5339 -- circular dependency (DEC suite perversity).
5341 if not Analyzed (Uname)
5342 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5343 then
5344 Error_Msg_N
5345 ("instantiation depends on itself", Name (With_Clause));
5347 elsif not Analyzed (Uname)
5348 and then Is_Internal_Unit (Current_Sem_Unit)
5349 and then not Is_Visible_Lib_Unit (Uname)
5350 and then No (Scope (Uname))
5351 then
5352 if Is_Predefined_Unit (Current_Sem_Unit) then
5353 Error_Msg_N
5354 ("predefined unit depends on itself", Name (With_Clause));
5355 else
5356 Error_Msg_N
5357 ("GNAT-defined unit depends on itself", Name (With_Clause));
5358 end if;
5359 return;
5361 elsif not Is_Visible_Lib_Unit (Uname) then
5363 -- Abandon processing in case of previous errors
5365 if No (Scope (Uname)) then
5366 Check_Error_Detected;
5367 return;
5368 end if;
5370 Set_Is_Visible_Lib_Unit (Uname);
5372 -- If the unit is a wrapper package for a compilation unit that is
5373 -- a subprogram instance, indicate that the instance itself is a
5374 -- visible unit. This is necessary if the instance is inlined.
5376 if Is_Wrapper_Package (Uname) then
5377 Set_Is_Visible_Lib_Unit (Related_Instance (Uname));
5378 end if;
5380 -- If the child unit appears in the context of its parent, it is
5381 -- immediately visible.
5383 if In_Open_Scopes (Scope (Uname)) then
5384 Set_Is_Immediately_Visible (Uname);
5385 end if;
5387 if Is_Generic_Instance (Uname)
5388 and then Is_Subprogram (Uname)
5389 then
5390 -- Set flag as well on the visible entity that denotes the
5391 -- instance, which renames the current one.
5393 Set_Is_Visible_Lib_Unit
5394 (Related_Instance
5395 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5396 end if;
5398 -- The parent unit may have been installed already, and may have
5399 -- appeared in a use clause.
5401 if In_Use (Scope (Uname)) then
5402 Set_Is_Potentially_Use_Visible (Uname);
5403 end if;
5405 Set_Context_Installed (With_Clause);
5406 end if;
5408 elsif not Is_Immediately_Visible (Uname) then
5409 Set_Is_Visible_Lib_Unit (Uname);
5411 if not Private_Present (With_Clause) or else Private_With_OK then
5412 Set_Is_Immediately_Visible (Uname);
5413 end if;
5415 Set_Context_Installed (With_Clause);
5416 end if;
5418 -- A [private] with clause overrides a limited with clause. Restore the
5419 -- proper view of the package by performing the following actions:
5421 -- * Remove all shadow entities which hide their corresponding
5422 -- entities from direct visibility by updating the entity and
5423 -- homonym chains.
5425 -- * Enter the corresponding entities back in direct visibility
5427 -- Note that the original limited with clause which installed its view
5428 -- is still marked as "active". This effect is undone when the clause
5429 -- itself is removed, see Remove_Limited_With_Clause.
5431 if Ekind (Uname) = E_Package and then From_Limited_With (Uname) then
5432 Remove_Limited_With_Unit (Unit_Declaration_Node (Uname));
5433 end if;
5435 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5436 -- unit if there is a visible homograph for it declared in the same
5437 -- declarative region. This pathological case can only arise when an
5438 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5439 -- G1 has a generic child also named G2, and the context includes with_
5440 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5441 -- of I1.G2 visible as well. If the child unit is named Standard, do
5442 -- not apply the check to the Standard package itself.
5444 if Is_Child_Unit (Uname)
5445 and then Is_Visible_Lib_Unit (Uname)
5446 and then Ada_Version >= Ada_2005
5447 then
5448 declare
5449 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5450 Decl2 : Node_Id;
5451 P2 : Entity_Id;
5452 U2 : Entity_Id;
5454 begin
5455 U2 := Homonym (Uname);
5456 while Present (U2) and then U2 /= Standard_Standard loop
5457 P2 := Scope (U2);
5458 Decl2 := Unit_Declaration_Node (P2);
5460 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5461 if Is_Generic_Instance (P)
5462 and then Nkind (Decl1) = N_Package_Declaration
5463 and then Generic_Parent (Specification (Decl1)) = P2
5464 then
5465 Error_Msg_N ("illegal with_clause", With_Clause);
5466 Error_Msg_N
5467 ("\child unit has visible homograph" &
5468 " (RM 8.3(26), 10.1.1(19))",
5469 With_Clause);
5470 exit;
5472 elsif Is_Generic_Instance (P2)
5473 and then Nkind (Decl2) = N_Package_Declaration
5474 and then Generic_Parent (Specification (Decl2)) = P
5475 then
5476 -- With_clause for child unit of instance appears before
5477 -- in the context. We want to place the error message on
5478 -- it, not on the generic child unit itself.
5480 declare
5481 Prev_Clause : Node_Id;
5483 begin
5484 Prev_Clause := First (List_Containing (With_Clause));
5485 while Entity (Name (Prev_Clause)) /= U2 loop
5486 Next (Prev_Clause);
5487 end loop;
5489 pragma Assert (Present (Prev_Clause));
5490 Error_Msg_N ("illegal with_clause", Prev_Clause);
5491 Error_Msg_N
5492 ("\child unit has visible homograph" &
5493 " (RM 8.3(26), 10.1.1(19))",
5494 Prev_Clause);
5495 exit;
5496 end;
5497 end if;
5498 end if;
5500 U2 := Homonym (U2);
5501 end loop;
5502 end;
5503 end if;
5504 end Install_With_Clause;
5506 -------------------
5507 -- Is_Child_Spec --
5508 -------------------
5510 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5511 K : constant Node_Kind := Nkind (Lib_Unit);
5513 begin
5514 return (K in N_Generic_Declaration or else
5515 K in N_Generic_Instantiation or else
5516 K in N_Generic_Renaming_Declaration or else
5517 K = N_Package_Declaration or else
5518 K = N_Package_Renaming_Declaration or else
5519 K = N_Subprogram_Declaration or else
5520 K = N_Subprogram_Renaming_Declaration)
5521 and then Present (Parent_Spec (Lib_Unit));
5522 end Is_Child_Spec;
5524 ------------------------------------
5525 -- Is_Legal_Shadow_Entity_In_Body --
5526 ------------------------------------
5528 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5529 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5530 begin
5531 return Nkind (Unit (C_Unit)) = N_Package_Body
5532 and then
5533 Has_With_Clause
5534 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5535 end Is_Legal_Shadow_Entity_In_Body;
5537 ----------------------
5538 -- Is_Ancestor_Unit --
5539 ----------------------
5541 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5542 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5543 E2 : Entity_Id;
5544 begin
5545 if Nkind (Unit (U2)) in N_Package_Body | N_Subprogram_Body then
5546 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5547 return Is_Ancestor_Package (E1, E2);
5548 else
5549 return False;
5550 end if;
5551 end Is_Ancestor_Unit;
5553 ----------------------------------
5554 -- Is_Visible_Through_Renamings --
5555 ----------------------------------
5557 function Is_Visible_Through_Renamings
5558 (P : Entity_Id;
5559 Error_Node : Node_Id := Empty) return Boolean
5561 function Is_Limited_Withed_Unit
5562 (Lib_Unit : Node_Id;
5563 Pkg_Ent : Entity_Id) return Boolean;
5564 -- Return True if Pkg_Ent is a limited-withed package of the given
5565 -- library unit.
5567 ----------------------------
5568 -- Is_Limited_Withed_Unit --
5569 ----------------------------
5571 function Is_Limited_Withed_Unit
5572 (Lib_Unit : Node_Id;
5573 Pkg_Ent : Entity_Id) return Boolean
5575 Item : Node_Id := First (Context_Items (Lib_Unit));
5577 begin
5578 while Present (Item) loop
5579 if Nkind (Item) = N_With_Clause
5580 and then Limited_Present (Item)
5581 and then Entity (Name (Item)) = Pkg_Ent
5582 then
5583 return True;
5584 end if;
5586 Next (Item);
5587 end loop;
5589 return False;
5590 end Is_Limited_Withed_Unit;
5592 -- Local variables
5594 Kind : constant Node_Kind := Nkind (Unit (Cunit (Current_Sem_Unit)));
5595 Aux_Unit : Node_Id;
5596 Item : Node_Id;
5597 Decl : Entity_Id;
5599 begin
5600 -- Example of the error detected by this subprogram:
5602 -- package P is
5603 -- type T is ...
5604 -- end P;
5606 -- with P;
5607 -- package Q is
5608 -- package Ren_P renames P;
5609 -- end Q;
5611 -- with Q;
5612 -- package R is ...
5614 -- limited with P; -- ERROR
5615 -- package R.C is ...
5617 Aux_Unit := Cunit (Current_Sem_Unit);
5619 loop
5620 Item := First (Context_Items (Aux_Unit));
5621 while Present (Item) loop
5622 if Nkind (Item) = N_With_Clause
5623 and then not Limited_Present (Item)
5624 and then Nkind (Unit (Library_Unit (Item))) =
5625 N_Package_Declaration
5626 then
5627 Decl :=
5628 First (Visible_Declarations
5629 (Specification (Unit (Library_Unit (Item)))));
5630 while Present (Decl) loop
5631 if Nkind (Decl) = N_Package_Renaming_Declaration
5632 and then Entity (Name (Decl)) = P
5633 and then not Is_Limited_Withed_Unit
5634 (Lib_Unit => Library_Unit (Item),
5635 Pkg_Ent => Entity (Name (Decl)))
5636 then
5637 -- Generate the error message only if the current unit
5638 -- is a package declaration; in case of subprogram
5639 -- bodies and package bodies we just return True to
5640 -- indicate that the limited view must not be
5641 -- installed.
5643 if Kind = N_Package_Declaration
5644 and then Present (Error_Node)
5645 then
5646 Error_Msg_N
5647 ("simultaneous visibility of the limited and " &
5648 "unlimited views not allowed", Error_Node);
5649 Error_Msg_Sloc := Sloc (Item);
5650 Error_Msg_NE
5651 ("\\ unlimited view of & visible through the " &
5652 "context clause #", Error_Node, P);
5653 Error_Msg_Sloc := Sloc (Decl);
5654 Error_Msg_NE ("\\ and the renaming #", Error_Node, P);
5655 end if;
5657 return True;
5658 end if;
5660 Next (Decl);
5661 end loop;
5662 end if;
5664 Next (Item);
5665 end loop;
5667 -- If it is a body not acting as spec, follow pointer to the
5668 -- corresponding spec, otherwise follow pointer to parent spec.
5670 if Present (Library_Unit (Aux_Unit))
5671 and then Nkind (Unit (Aux_Unit)) in
5672 N_Package_Body | N_Subprogram_Body
5673 then
5674 if Aux_Unit = Library_Unit (Aux_Unit) then
5676 -- Aux_Unit is a body that acts as a spec. Clause has
5677 -- already been flagged as illegal.
5679 return False;
5681 else
5682 Aux_Unit := Library_Unit (Aux_Unit);
5683 end if;
5685 else
5686 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
5687 end if;
5689 exit when No (Aux_Unit);
5690 end loop;
5692 return False;
5693 end Is_Visible_Through_Renamings;
5695 -----------------------
5696 -- Load_Needed_Body --
5697 -----------------------
5699 -- N is a generic unit named in a with clause, or else it is a unit that
5700 -- contains a generic unit or an inlined function. In order to perform an
5701 -- instantiation, the body of the unit must be present. If the unit itself
5702 -- is generic, we assume that an instantiation follows, and load & analyze
5703 -- the body unconditionally. This forces analysis of the spec as well.
5705 -- If the unit is not generic, but contains a generic unit, it is loaded on
5706 -- demand, at the point of instantiation (see ch12).
5708 procedure Load_Needed_Body
5709 (N : Node_Id;
5710 OK : out Boolean)
5712 Body_Name : Unit_Name_Type;
5713 Unum : Unit_Number_Type;
5715 Save_Style_Check : constant Boolean := Opt.Style_Check;
5716 -- The loading and analysis is done with style checks off
5718 begin
5719 if not GNAT_Mode then
5720 Style_Check := False;
5721 end if;
5723 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5724 Unum :=
5725 Load_Unit
5726 (Load_Name => Body_Name,
5727 Required => False,
5728 Subunit => False,
5729 Error_Node => N,
5730 Renamings => True);
5732 if Unum = No_Unit then
5733 OK := False;
5735 else
5736 Compiler_State := Analyzing; -- reset after load
5738 if Fatal_Error (Unum) /= Error_Detected or else Try_Semantics then
5739 if Debug_Flag_L then
5740 Write_Str ("*** Loaded generic body");
5741 Write_Eol;
5742 end if;
5744 -- We always perform analyses
5745 Semantics (Cunit (Unum));
5746 end if;
5748 OK := True;
5749 end if;
5751 Style_Check := Save_Style_Check;
5752 end Load_Needed_Body;
5754 -------------------------
5755 -- Build_Limited_Views --
5756 -------------------------
5758 procedure Build_Limited_Views (N : Node_Id) is
5759 Unum : constant Unit_Number_Type :=
5760 Get_Source_Unit (Library_Unit (N));
5761 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
5763 Shadow_Pack : Entity_Id;
5764 -- The corresponding shadow entity of the withed package. This entity
5765 -- offers incomplete views of packages and types as well as abstract
5766 -- views of states and variables declared within.
5768 Last_Shadow : Entity_Id := Empty;
5769 -- The last shadow entity created by routine Build_Shadow_Entity
5771 procedure Build_Shadow_Entity
5772 (Ent : Entity_Id;
5773 Scop : Entity_Id;
5774 Shadow : out Entity_Id;
5775 Is_Tagged : Boolean := False);
5776 -- Create a shadow entity that hides Ent and offers an abstract or
5777 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5778 -- should be set when Ent is a tagged type. The generated entity is
5779 -- added to Shadow_Pack. The routine updates the value of Last_Shadow.
5781 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
5782 -- Perform minimal decoration of a package or its corresponding shadow
5783 -- entity denoted by Ent. Scop is the proper scope.
5785 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id);
5786 -- Perform full decoration of an abstract state or its corresponding
5787 -- shadow entity denoted by Ent. Scop is the proper scope.
5789 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id);
5790 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5791 -- proper scope.
5793 procedure Process_Declarations_And_States
5794 (Pack : Entity_Id;
5795 Decls : List_Id;
5796 Scop : Entity_Id;
5797 Create_Abstract_Views : Boolean);
5798 -- Inspect the states of package Pack and declarative list Decls. Create
5799 -- shadow entities for all nested packages, states, types and variables
5800 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5801 -- be set when the abstract states and variables need to be processed.
5803 -------------------------
5804 -- Build_Shadow_Entity --
5805 -------------------------
5807 procedure Build_Shadow_Entity
5808 (Ent : Entity_Id;
5809 Scop : Entity_Id;
5810 Shadow : out Entity_Id;
5811 Is_Tagged : Boolean := False)
5813 begin
5814 Shadow := Make_Temporary (Sloc (Ent), 'Z');
5816 -- The shadow entity must share the same name and parent as the
5817 -- entity it hides.
5819 Set_Chars (Shadow, Chars (Ent));
5820 Set_Parent (Shadow, Parent (Ent));
5822 -- The abstract view of a variable is a state, not another variable
5824 if Ekind (Ent) = E_Variable then
5825 Mutate_Ekind (Shadow, E_Abstract_State);
5826 else
5827 Mutate_Ekind (Shadow, Ekind (Ent));
5828 end if;
5830 Set_Is_Internal (Shadow);
5831 Set_From_Limited_With (Shadow);
5833 -- Add the new shadow entity to the limited view of the package
5835 Last_Shadow := Shadow;
5836 Append_Entity (Shadow, Shadow_Pack);
5838 -- Perform context-specific decoration of the shadow entity
5840 if Ekind (Ent) = E_Abstract_State then
5841 Decorate_State (Shadow, Scop);
5842 Set_Non_Limited_View (Shadow, Ent);
5844 elsif Ekind (Ent) = E_Package then
5845 Decorate_Package (Shadow, Scop);
5847 elsif Is_Type (Ent) then
5848 Decorate_Type (Shadow, Scop, Is_Tagged);
5850 -- If Ent is a private type and we are analyzing the body of its
5851 -- scope, its private and full views are swapped and, therefore,
5852 -- we need to undo this swapping in order to build the same shadow
5853 -- entity as we would have in other contexts.
5855 if Is_Private_Type (Ent)
5856 and then Present (Full_View (Ent))
5857 and then In_Package_Body (Scop)
5858 then
5859 Set_Non_Limited_View (Shadow, Full_View (Ent));
5860 else
5861 Set_Non_Limited_View (Shadow, Ent);
5862 end if;
5864 if Is_Tagged then
5865 Set_Non_Limited_View
5866 (Class_Wide_Type (Shadow), Class_Wide_Type (Ent));
5867 end if;
5869 if Is_Incomplete_Or_Private_Type (Ent) then
5870 Set_Private_Dependents (Shadow, New_Elmt_List);
5871 end if;
5873 elsif Ekind (Ent) = E_Variable then
5874 Decorate_State (Shadow, Scop);
5875 Set_Non_Limited_View (Shadow, Ent);
5876 end if;
5877 end Build_Shadow_Entity;
5879 ----------------------
5880 -- Decorate_Package --
5881 ----------------------
5883 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
5884 begin
5885 Mutate_Ekind (Ent, E_Package);
5886 Set_Etype (Ent, Standard_Void_Type);
5887 Set_Scope (Ent, Scop);
5888 end Decorate_Package;
5890 --------------------
5891 -- Decorate_State --
5892 --------------------
5894 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id) is
5895 begin
5896 Mutate_Ekind (Ent, E_Abstract_State);
5897 Set_Etype (Ent, Standard_Void_Type);
5898 Set_Scope (Ent, Scop);
5899 Set_Encapsulating_State (Ent, Empty);
5900 end Decorate_State;
5902 -----------------------
5903 -- Decorate_Variable --
5904 -----------------------
5906 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id) is
5907 begin
5908 Mutate_Ekind (Ent, E_Variable);
5909 Set_Etype (Ent, Standard_Void_Type);
5910 Set_Scope (Ent, Scop);
5911 end Decorate_Variable;
5913 -------------------------------------
5914 -- Process_Declarations_And_States --
5915 -------------------------------------
5917 procedure Process_Declarations_And_States
5918 (Pack : Entity_Id;
5919 Decls : List_Id;
5920 Scop : Entity_Id;
5921 Create_Abstract_Views : Boolean)
5923 procedure Find_And_Process_States;
5924 -- Determine whether package Pack defines abstract state either by
5925 -- using an aspect or a pragma. If this is the case, build shadow
5926 -- entities for all abstract states of Pack.
5928 procedure Process_States (States : Elist_Id);
5929 -- Generate shadow entities for all abstract states in list States
5931 -----------------------------
5932 -- Find_And_Process_States --
5933 -----------------------------
5935 procedure Find_And_Process_States is
5936 procedure Process_State (State : Node_Id);
5937 -- Generate shadow entities for a single abstract state or
5938 -- multiple states expressed as an aggregate.
5940 -------------------
5941 -- Process_State --
5942 -------------------
5944 procedure Process_State (State : Node_Id) is
5945 Loc : constant Source_Ptr := Sloc (State);
5946 Decl : Node_Id;
5947 Dummy : Entity_Id;
5948 Elmt : Node_Id;
5949 Id : Entity_Id;
5951 begin
5952 -- Multiple abstract states appear as an aggregate
5954 if Nkind (State) = N_Aggregate then
5955 Elmt := First (Expressions (State));
5956 while Present (Elmt) loop
5957 Process_State (Elmt);
5958 Next (Elmt);
5959 end loop;
5961 return;
5963 -- A null state has no abstract view
5965 elsif Nkind (State) = N_Null then
5966 return;
5968 -- State declaration with various options appears as an
5969 -- extension aggregate.
5971 elsif Nkind (State) = N_Extension_Aggregate then
5972 Decl := Ancestor_Part (State);
5974 -- Simple state declaration
5976 elsif Nkind (State) = N_Identifier then
5977 Decl := State;
5979 -- Possibly an illegal state declaration
5981 else
5982 return;
5983 end if;
5985 -- Abstract states are elaborated when the related pragma is
5986 -- elaborated. Since the withed package is not analyzed yet,
5987 -- the entities of the abstract states are not available. To
5988 -- overcome this complication, create the entities now and
5989 -- store them in their respective declarations. The entities
5990 -- are later used by routine Create_Abstract_State to declare
5991 -- and enter the states into visibility.
5993 if No (Entity (Decl)) then
5994 Id := Make_Defining_Identifier (Loc, Chars (Decl));
5996 Set_Entity (Decl, Id);
5997 Set_Parent (Id, State);
5998 Decorate_State (Id, Scop);
6000 -- Otherwise the package was previously withed
6002 else
6003 Id := Entity (Decl);
6004 end if;
6006 Build_Shadow_Entity (Id, Scop, Dummy);
6007 end Process_State;
6009 -- Local variables
6011 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack);
6012 Asp : Node_Id;
6013 Decl : Node_Id;
6015 -- Start of processing for Find_And_Process_States
6017 begin
6018 -- Find aspect Abstract_State
6020 Asp := First (Aspect_Specifications (Pack_Decl));
6021 while Present (Asp) loop
6022 if Chars (Identifier (Asp)) = Name_Abstract_State then
6023 Process_State (Expression (Asp));
6025 return;
6026 end if;
6028 Next (Asp);
6029 end loop;
6031 -- Find pragma Abstract_State by inspecting the declarations
6033 Decl := First (Decls);
6034 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
6035 if Pragma_Name (Decl) = Name_Abstract_State then
6036 Process_State
6037 (Get_Pragma_Arg
6038 (First (Pragma_Argument_Associations (Decl))));
6040 return;
6041 end if;
6043 Next (Decl);
6044 end loop;
6045 end Find_And_Process_States;
6047 --------------------
6048 -- Process_States --
6049 --------------------
6051 procedure Process_States (States : Elist_Id) is
6052 Dummy : Entity_Id;
6053 Elmt : Elmt_Id;
6055 begin
6056 Elmt := First_Elmt (States);
6057 while Present (Elmt) loop
6058 Build_Shadow_Entity (Node (Elmt), Scop, Dummy);
6060 Next_Elmt (Elmt);
6061 end loop;
6062 end Process_States;
6064 -- Local variables
6066 Is_Tagged : Boolean;
6067 Decl : Node_Id;
6068 Def : Node_Id;
6069 Def_Id : Entity_Id;
6070 Shadow : Entity_Id;
6072 -- Start of processing for Process_Declarations_And_States
6074 begin
6075 -- Build abstract views for all states defined in the package
6077 if Create_Abstract_Views then
6079 -- When a package has been analyzed, all states are stored in list
6080 -- Abstract_States. Generate the shadow entities directly.
6082 if Is_Analyzed then
6083 if Present (Abstract_States (Pack)) then
6084 Process_States (Abstract_States (Pack));
6085 end if;
6087 -- The package may declare abstract states by using an aspect or a
6088 -- pragma. Attempt to locate one of these construct and if found,
6089 -- build the shadow entities.
6091 else
6092 Find_And_Process_States;
6093 end if;
6094 end if;
6096 -- Inspect the declarative list, looking for nested packages, types
6097 -- and variable declarations.
6099 Decl := First (Decls);
6100 while Present (Decl) loop
6102 -- Packages
6104 if Nkind (Decl) = N_Package_Declaration then
6105 Def_Id := Defining_Entity (Decl);
6107 -- Perform minor decoration when the withed package has not
6108 -- been analyzed.
6110 if not Is_Analyzed then
6111 Decorate_Package (Def_Id, Scop);
6112 end if;
6114 -- Create a shadow entity that offers a limited view of all
6115 -- visible types declared within.
6117 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6119 Process_Declarations_And_States
6120 (Pack => Def_Id,
6121 Decls =>
6122 Visible_Declarations (Specification (Decl)),
6123 Scop => Shadow,
6124 Create_Abstract_Views => Create_Abstract_Views);
6126 -- Types
6128 elsif Nkind (Decl) in N_Full_Type_Declaration
6129 | N_Incomplete_Type_Declaration
6130 | N_Private_Extension_Declaration
6131 | N_Private_Type_Declaration
6132 | N_Protected_Type_Declaration
6133 | N_Task_Type_Declaration
6134 then
6135 Def_Id := Defining_Entity (Decl);
6137 -- Determine whether the type is tagged. Note that packages
6138 -- included via a limited with clause are not always analyzed,
6139 -- hence the tree lookup rather than the use of attribute
6140 -- Is_Tagged_Type.
6142 if Nkind (Decl) = N_Full_Type_Declaration then
6143 Def := Type_Definition (Decl);
6145 Is_Tagged :=
6146 (Nkind (Def) = N_Record_Definition
6147 and then Tagged_Present (Def))
6148 or else
6149 (Nkind (Def) = N_Derived_Type_Definition
6150 and then Present (Record_Extension_Part (Def)));
6152 elsif Nkind (Decl) in N_Incomplete_Type_Declaration
6153 | N_Private_Type_Declaration
6154 then
6155 Is_Tagged := Tagged_Present (Decl);
6157 elsif Nkind (Decl) = N_Private_Extension_Declaration then
6158 Is_Tagged := True;
6160 else
6161 Is_Tagged := False;
6162 end if;
6164 -- Perform minor decoration when the withed package has not
6165 -- been analyzed.
6167 if not Is_Analyzed then
6168 Decorate_Type (Def_Id, Scop, Is_Tagged, True);
6169 end if;
6171 -- Create a shadow entity that hides the type and offers an
6172 -- incomplete view of the said type.
6174 Build_Shadow_Entity (Def_Id, Scop, Shadow, Is_Tagged);
6176 -- Variables
6178 elsif Create_Abstract_Views
6179 and then Nkind (Decl) = N_Object_Declaration
6180 and then not Constant_Present (Decl)
6181 then
6182 Def_Id := Defining_Entity (Decl);
6184 -- Perform minor decoration when the withed package has not
6185 -- been analyzed.
6187 if not Is_Analyzed then
6188 Decorate_Variable (Def_Id, Scop);
6189 end if;
6191 -- Create a shadow entity that hides the variable and offers an
6192 -- abstract view of the said variable.
6194 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6195 end if;
6197 Next (Decl);
6198 end loop;
6199 end Process_Declarations_And_States;
6201 -- Local variables
6203 Nam : constant Node_Id := Name (N);
6204 Pack : constant Entity_Id := Cunit_Entity (Unum);
6206 Last_Public_Shadow : Entity_Id := Empty;
6207 Private_Shadow : Entity_Id;
6208 Spec : Node_Id;
6210 -- Start of processing for Build_Limited_Views
6212 begin
6213 pragma Assert (Limited_Present (N));
6215 -- A library_item mentioned in a limited_with_clause is a package
6216 -- declaration, not a subprogram declaration, generic declaration,
6217 -- generic instantiation, or package renaming declaration.
6219 case Nkind (Unit (Library_Unit (N))) is
6220 when N_Package_Declaration =>
6221 null;
6223 when N_Subprogram_Declaration =>
6224 Error_Msg_N
6225 ("subprogram not allowed in `LIMITED WITH` clause", N);
6226 return;
6228 when N_Generic_Declaration =>
6229 Error_Msg_N ("generic not allowed in `LIMITED WITH` clause", N);
6230 return;
6232 when N_Generic_Instantiation =>
6233 Error_Msg_N
6234 ("generic instantiation not allowed in `LIMITED WITH` clause",
6236 return;
6238 when N_Generic_Renaming_Declaration =>
6239 Error_Msg_N
6240 ("generic renaming not allowed in `LIMITED WITH` clause", N);
6241 return;
6243 when N_Subprogram_Renaming_Declaration =>
6244 Error_Msg_N
6245 ("renamed subprogram not allowed in `LIMITED WITH` clause", N);
6246 return;
6248 when N_Package_Renaming_Declaration =>
6249 Error_Msg_N
6250 ("renamed package not allowed in `LIMITED WITH` clause", N);
6251 return;
6253 when others =>
6254 raise Program_Error;
6255 end case;
6257 -- The withed unit may not be analyzed, but the with calause itself
6258 -- must be minimally decorated. This ensures that the checks on unused
6259 -- with clauses also process limieted withs.
6261 Mutate_Ekind (Pack, E_Package);
6262 Set_Etype (Pack, Standard_Void_Type);
6264 if Is_Entity_Name (Nam) then
6265 Set_Entity (Nam, Pack);
6267 elsif Nkind (Nam) = N_Selected_Component then
6268 Set_Entity (Selector_Name (Nam), Pack);
6269 end if;
6271 -- Check if the chain is already built
6273 Spec := Specification (Unit (Library_Unit (N)));
6275 if Limited_View_Installed (Spec) then
6276 return;
6277 end if;
6279 -- Create the shadow package wich hides the withed unit and provides
6280 -- incomplete view of all types and packages declared within.
6282 Shadow_Pack := Make_Temporary (Sloc (N), 'Z');
6283 Mutate_Ekind (Shadow_Pack, E_Package);
6284 Set_Is_Internal (Shadow_Pack);
6285 Set_Limited_View (Pack, Shadow_Pack);
6287 -- Inspect the abstract states and visible declarations of the withed
6288 -- unit and create shadow entities that hide existing packages, states,
6289 -- variables and types.
6291 Process_Declarations_And_States
6292 (Pack => Pack,
6293 Decls => Visible_Declarations (Spec),
6294 Scop => Pack,
6295 Create_Abstract_Views => True);
6297 Last_Public_Shadow := Last_Shadow;
6299 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6300 -- to accommodate limited private with clauses.
6302 Process_Declarations_And_States
6303 (Pack => Pack,
6304 Decls => Private_Declarations (Spec),
6305 Scop => Pack,
6306 Create_Abstract_Views => False);
6308 if Present (Last_Public_Shadow) then
6309 Private_Shadow := Next_Entity (Last_Public_Shadow);
6310 else
6311 Private_Shadow := First_Entity (Shadow_Pack);
6312 end if;
6314 Set_First_Private_Entity (Shadow_Pack, Private_Shadow);
6315 Set_Limited_View_Installed (Spec);
6316 end Build_Limited_Views;
6318 ----------------------------
6319 -- Check_No_Elab_Code_All --
6320 ----------------------------
6322 procedure Check_No_Elab_Code_All (N : Node_Id) is
6323 begin
6324 if Present (No_Elab_Code_All_Pragma)
6325 and then In_Extended_Main_Source_Unit (N)
6326 and then Present (Context_Items (N))
6327 then
6328 declare
6329 CL : constant List_Id := Context_Items (N);
6330 CI : Node_Id;
6332 begin
6333 CI := First (CL);
6334 while Present (CI) loop
6335 if Nkind (CI) = N_With_Clause
6336 and then not
6337 No_Elab_Code_All (Get_Source_Unit (Library_Unit (CI)))
6339 -- In GNATprove mode, some runtime units are implicitly
6340 -- loaded to make their entities available for analysis. In
6341 -- this case, ignore violations of No_Elaboration_Code_All
6342 -- for this special analysis mode.
6344 and then not
6345 (GNATprove_Mode and then Implicit_With (CI))
6346 then
6347 Error_Msg_Sloc := Sloc (No_Elab_Code_All_Pragma);
6348 Error_Msg_N
6349 ("violation of No_Elaboration_Code_All#", CI);
6350 Error_Msg_NE
6351 ("\unit& does not have No_Elaboration_Code_All",
6352 CI, Entity (Name (CI)));
6353 end if;
6355 Next (CI);
6356 end loop;
6357 end;
6358 end if;
6359 end Check_No_Elab_Code_All;
6361 -------------------------------
6362 -- Check_Body_Needed_For_SAL --
6363 -------------------------------
6365 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
6366 function Entity_Needs_Body (E : Entity_Id) return Boolean;
6367 -- Determine whether use of entity E might require the presence of its
6368 -- body. For a package this requires a recursive traversal of all nested
6369 -- declarations.
6371 -----------------------
6372 -- Entity_Needs_Body --
6373 -----------------------
6375 function Entity_Needs_Body (E : Entity_Id) return Boolean is
6376 Ent : Entity_Id;
6378 begin
6379 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
6380 return True;
6382 elsif Is_Generic_Subprogram (E) then
6384 -- A generic subprogram always requires the presence of its
6385 -- body because an instantiation needs both templates. The only
6386 -- exceptions is a generic subprogram renaming. In this case the
6387 -- body is needed only when the template is declared outside the
6388 -- compilation unit being checked.
6390 if Present (Renamed_Entity (E)) then
6391 return not Within_Scope (E, Unit_Name);
6392 else
6393 return True;
6394 end if;
6396 elsif Ekind (E) = E_Generic_Package
6397 and then
6398 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
6399 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6400 then
6401 return True;
6403 elsif Ekind (E) = E_Package
6404 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
6405 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6406 then
6407 Ent := First_Entity (E);
6408 while Present (Ent) loop
6409 if Entity_Needs_Body (Ent) then
6410 return True;
6411 end if;
6413 Next_Entity (Ent);
6414 end loop;
6416 return False;
6418 else
6419 return False;
6420 end if;
6421 end Entity_Needs_Body;
6423 -- Start of processing for Check_Body_Needed_For_SAL
6425 begin
6426 if Ekind (Unit_Name) = E_Generic_Package
6427 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6428 N_Generic_Package_Declaration
6429 and then
6430 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
6431 then
6432 Set_Body_Needed_For_SAL (Unit_Name);
6434 elsif Ekind (Unit_Name) in E_Generic_Procedure | E_Generic_Function then
6435 Set_Body_Needed_For_SAL (Unit_Name);
6437 elsif Is_Subprogram (Unit_Name)
6438 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6439 N_Subprogram_Declaration
6440 and then Has_Pragma_Inline (Unit_Name)
6441 then
6442 Set_Body_Needed_For_SAL (Unit_Name);
6444 elsif Ekind (Unit_Name) = E_Subprogram_Body then
6445 Check_Body_Needed_For_SAL
6446 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6448 elsif Ekind (Unit_Name) = E_Package
6449 and then Entity_Needs_Body (Unit_Name)
6450 then
6451 Set_Body_Needed_For_SAL (Unit_Name);
6453 elsif Ekind (Unit_Name) = E_Package_Body
6454 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
6455 then
6456 Check_Body_Needed_For_SAL
6457 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6458 end if;
6459 end Check_Body_Needed_For_SAL;
6461 --------------------
6462 -- Remove_Context --
6463 --------------------
6465 procedure Remove_Context (N : Node_Id) is
6466 Lib_Unit : constant Node_Id := Unit (N);
6468 begin
6469 -- If this is a child unit, first remove the parent units
6471 if Is_Child_Spec (Lib_Unit) then
6472 Remove_Parents (Lib_Unit);
6473 end if;
6475 Remove_Context_Clauses (N);
6476 end Remove_Context;
6478 ----------------------------
6479 -- Remove_Context_Clauses --
6480 ----------------------------
6482 procedure Remove_Context_Clauses (N : Node_Id) is
6483 Item : Node_Id;
6484 Unit_Name : Entity_Id;
6486 begin
6487 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6488 -- limited-views first and regular-views later (to maintain the stack
6489 -- model).
6491 -- First Phase: Remove limited_with context clauses
6493 Item := First (Context_Items (N));
6494 while Present (Item) loop
6496 -- We are interested only in with clauses that got installed on entry
6498 if Nkind (Item) = N_With_Clause
6499 and then Limited_Present (Item)
6500 then
6501 if Limited_View_Installed (Item) then
6502 Remove_Limited_With_Clause (Item);
6504 -- An unusual case: If the library unit of the Main_Unit has a
6505 -- limited with_clause on some unit P and the context somewhere
6506 -- includes a with_clause on P, P has been analyzed. The entity
6507 -- for P is still visible, which in general is harmless because
6508 -- this is the end of the compilation, but it can affect pending
6509 -- instantiations that may have been generated elsewhere, so it
6510 -- it is necessary to remove U from visibility so that inlining
6511 -- and the analysis of instance bodies can proceed cleanly.
6513 elsif Current_Sem_Unit = Main_Unit
6514 and then Serious_Errors_Detected = 0
6515 and then not Implicit_With (Item)
6516 then
6517 Set_Is_Immediately_Visible
6518 (Defining_Entity (Unit (Library_Unit (Item))), False);
6519 end if;
6520 end if;
6522 Next (Item);
6523 end loop;
6525 -- Second Phase: Loop through context items and undo regular
6526 -- with_clauses and use_clauses.
6528 Item := First (Context_Items (N));
6529 while Present (Item) loop
6531 -- We are interested only in with clauses which got installed on
6532 -- entry, as indicated by their Context_Installed flag set
6534 if Nkind (Item) = N_With_Clause
6535 and then Limited_Present (Item)
6536 and then Limited_View_Installed (Item)
6537 then
6538 null;
6540 elsif Nkind (Item) = N_With_Clause
6541 and then Context_Installed (Item)
6542 then
6543 -- Remove items from one with'ed unit
6545 Unit_Name := Entity (Name (Item));
6546 Remove_Unit_From_Visibility (Unit_Name);
6547 Set_Context_Installed (Item, False);
6549 elsif Nkind (Item) = N_Use_Package_Clause then
6550 End_Use_Package (Item);
6552 elsif Nkind (Item) = N_Use_Type_Clause then
6553 End_Use_Type (Item);
6554 end if;
6556 Next (Item);
6557 end loop;
6558 end Remove_Context_Clauses;
6560 --------------------------------
6561 -- Remove_Limited_With_Clause --
6562 --------------------------------
6564 procedure Remove_Limited_With_Clause (N : Node_Id) is
6565 Pack_Decl : constant Entity_Id := Unit (Library_Unit (N));
6567 begin
6568 pragma Assert (Limited_View_Installed (N));
6570 -- Limited with clauses that designate units other than packages are
6571 -- illegal and are never installed.
6573 if Nkind (Pack_Decl) = N_Package_Declaration then
6574 Remove_Limited_With_Unit (Pack_Decl, N);
6575 end if;
6577 -- Indicate that the limited views of the clause have been removed
6579 Set_Limited_View_Installed (N, False);
6580 end Remove_Limited_With_Clause;
6582 ------------------------------
6583 -- Remove_Limited_With_Unit --
6584 ------------------------------
6586 procedure Remove_Limited_With_Unit
6587 (Pack_Decl : Node_Id;
6588 Lim_Clause : Node_Id := Empty)
6590 procedure Remove_Shadow_Entities_From_Visibility (Pack_Id : Entity_Id);
6591 -- Remove the shadow entities of package Pack_Id from direct visibility
6593 procedure Remove_Shadow_Entities_With_Restore (Pack_Id : Entity_Id);
6594 -- Remove the shadow entities of package Pack_Id from direct visibility,
6595 -- restore the corresponding entities they hide into direct visibility,
6596 -- and update the entity and homonym chains.
6598 --------------------------------------------
6599 -- Remove_Shadow_Entities_From_Visibility --
6600 --------------------------------------------
6602 procedure Remove_Shadow_Entities_From_Visibility (Pack_Id : Entity_Id) is
6603 Lim_Header : constant Entity_Id := Limited_View (Pack_Id);
6604 Upto : constant Entity_Id := First_Private_Entity (Lim_Header);
6606 Shadow : Entity_Id;
6608 begin
6609 -- Remove the package from direct visibility
6611 Unchain (Pack_Id);
6612 Set_Is_Immediately_Visible (Pack_Id, False);
6614 -- Remove all shadow entities from direct visibility
6616 Shadow := First_Entity (Lim_Header);
6617 while Present (Shadow) and then Shadow /= Upto loop
6618 Unchain (Shadow);
6619 Next_Entity (Shadow);
6620 end loop;
6621 end Remove_Shadow_Entities_From_Visibility;
6623 -----------------------------------------
6624 -- Remove_Shadow_Entities_With_Restore --
6625 -----------------------------------------
6627 -- This code must be kept synchronized with the code that replaces the
6628 -- real entities by the shadow entities in Install_Limited_With_Clause,
6629 -- otherwise the contents of the homonym chains are not consistent.
6631 procedure Remove_Shadow_Entities_With_Restore (Pack_Id : Entity_Id) is
6632 procedure Restore_Chain_For_Shadow (Shadow : Entity_Id);
6633 -- Remove shadow entity Shadow by updating the entity and homonym
6634 -- chains.
6636 procedure Restore_Chains
6637 (From : Entity_Id;
6638 Upto : Entity_Id);
6639 -- Remove a sequence of shadow entities starting from From and ending
6640 -- prior to Upto by updating the entity and homonym chains.
6642 procedure Restore_Type_Visibility
6643 (From : Entity_Id;
6644 Upto : Entity_Id);
6645 -- Restore a sequence of types starting from From and ending prior to
6646 -- Upto back in direct visibility.
6648 ------------------------------
6649 -- Restore_Chain_For_Shadow --
6650 ------------------------------
6652 procedure Restore_Chain_For_Shadow (Shadow : Entity_Id) is
6653 Is_E3 : Boolean;
6654 Prev : Entity_Id;
6655 Typ : Entity_Id;
6657 begin
6658 -- If the package has incomplete types, the limited view of the
6659 -- incomplete type is in fact never visible (AI05-129) but we
6660 -- have created a shadow entity E1 for it, that points to E2,
6661 -- the incomplete type at stake. This in turn has a full view
6662 -- E3 that is the full declaration, with a corresponding
6663 -- shadow entity E4. When reinstalling the nonlimited view,
6664 -- the nonvisible entity E1 is first replaced with E2, but then
6665 -- E3 must *not* become the visible entity as it is replacing E4
6666 -- in the homonyms list and simply be ignored.
6668 -- regular views limited views
6670 -- * E2 (incomplete) <-- E1 (shadow)
6672 -- |
6673 -- V
6675 -- E3 (full) <-- E4 (shadow) *
6677 -- [*] denotes the visible entity (Current_Entity)
6679 Typ := Non_Limited_View (Shadow);
6680 pragma Assert (not In_Chain (Typ));
6682 Is_E3 := Nkind (Parent (Typ)) = N_Full_Type_Declaration
6683 and then Present (Incomplete_View (Parent (Typ)));
6685 Prev := Current_Entity (Shadow);
6687 if Prev = Shadow then
6688 if Is_E3 then
6689 Set_Name_Entity_Id (Chars (Prev), Homonym (Prev));
6690 return;
6692 else
6693 Set_Current_Entity (Typ);
6694 end if;
6696 else
6697 while Present (Homonym (Prev))
6698 and then Homonym (Prev) /= Shadow
6699 loop
6700 Prev := Homonym (Prev);
6701 end loop;
6703 if Is_E3 then
6704 Set_Homonym (Prev, Homonym (Shadow));
6705 return;
6707 else
6708 Set_Homonym (Prev, Typ);
6709 end if;
6710 end if;
6712 Set_Homonym (Typ, Homonym (Shadow));
6713 end Restore_Chain_For_Shadow;
6715 --------------------
6716 -- Restore_Chains --
6717 --------------------
6719 procedure Restore_Chains
6720 (From : Entity_Id;
6721 Upto : Entity_Id)
6723 Shadow : Entity_Id;
6725 begin
6726 Shadow := From;
6727 while Present (Shadow) and then Shadow /= Upto loop
6729 -- Do not unchain nested packages and child units
6731 if Ekind (Shadow) = E_Package then
6732 null;
6734 elsif Is_Child_Unit (Non_Limited_View (Shadow)) then
6735 null;
6737 else
6738 Restore_Chain_For_Shadow (Shadow);
6739 end if;
6741 Next_Entity (Shadow);
6742 end loop;
6743 end Restore_Chains;
6745 -----------------------------
6746 -- Restore_Type_Visibility --
6747 -----------------------------
6749 procedure Restore_Type_Visibility
6750 (From : Entity_Id;
6751 Upto : Entity_Id)
6753 Typ : Entity_Id;
6755 begin
6756 Typ := From;
6757 while Present (Typ) and then Typ /= Upto loop
6758 if Is_Type (Typ) then
6759 Set_Is_Hidden (Typ, Was_Hidden (Typ));
6760 end if;
6762 Next_Entity (Typ);
6763 end loop;
6764 end Restore_Type_Visibility;
6766 -- Local variables
6768 Lim_Header : constant Entity_Id := Limited_View (Pack_Id);
6770 -- Start of processing Remove_Shadow_Entities_With_Restore
6772 begin
6773 -- The limited view of a package is being uninstalled by removing
6774 -- the effects of a limited with clause. If the clause appears in a
6775 -- unit which is not part of the main unit closure, then the related
6776 -- package must not be visible.
6778 if Present (Lim_Clause)
6779 and then not In_Extended_Main_Source_Unit (Lim_Clause)
6780 then
6781 Set_Is_Immediately_Visible (Pack_Id, False);
6783 -- Otherwise a limited view is being overridden by a nonlimited view.
6784 -- Leave the visibility of the package as is because the unit must be
6785 -- visible when the nonlimited view is installed.
6787 else
6788 null;
6789 end if;
6791 -- Remove the shadow entities from visibility by updating the entity
6792 -- and homonym chains.
6794 Restore_Chains
6795 (From => First_Entity (Lim_Header),
6796 Upto => First_Private_Entity (Lim_Header));
6798 -- Reinstate the types that were hidden by the shadow entities back
6799 -- into direct visibility.
6801 Restore_Type_Visibility
6802 (From => First_Entity (Pack_Id),
6803 Upto => First_Private_Entity (Pack_Id));
6804 end Remove_Shadow_Entities_With_Restore;
6806 -- Local variables
6808 Pack_Id : constant Entity_Id := Defining_Entity (Pack_Decl);
6810 -- Start of processing for Remove_Limited_With_Unit
6812 begin
6813 -- Nothing to do when the limited view of the package is not installed
6815 if not From_Limited_With (Pack_Id) then
6816 return;
6817 end if;
6819 if Debug_Flag_I then
6820 Write_Str ("remove limited view of ");
6821 Write_Name (Chars (Pack_Id));
6822 Write_Str (" from visibility");
6823 Write_Eol;
6824 end if;
6826 -- The package already appears in the compilation closure. As a result,
6827 -- its shadow entities must be replaced by the real entities they hide
6828 -- and the previously hidden entities must be entered back into direct
6829 -- visibility.
6831 if Analyzed (Pack_Decl) then
6832 Remove_Shadow_Entities_With_Restore (Pack_Id);
6834 -- Otherwise the package is not analyzed and its shadow entities must be
6835 -- removed from direct visibility.
6837 else
6838 Remove_Shadow_Entities_From_Visibility (Pack_Id);
6839 end if;
6841 -- Indicate that the limited view of the package is not installed
6843 Set_From_Limited_With (Pack_Id, False);
6844 end Remove_Limited_With_Unit;
6846 --------------------
6847 -- Remove_Parents --
6848 --------------------
6850 procedure Remove_Parents (Lib_Unit : Node_Id) is
6851 P : Node_Id;
6852 P_Name : Entity_Id;
6853 P_Spec : Node_Id := Empty;
6854 E : Entity_Id;
6855 Vis : constant Boolean :=
6856 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
6858 begin
6859 if Is_Child_Spec (Lib_Unit) then
6860 P_Spec := Parent_Spec (Lib_Unit);
6862 elsif Nkind (Lib_Unit) = N_Package_Body
6863 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6864 then
6865 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6866 end if;
6868 if Present (P_Spec) then
6869 P := Unit (P_Spec);
6870 P_Name := Get_Parent_Entity (P);
6871 Remove_Context_Clauses (P_Spec);
6872 End_Package_Scope (P_Name);
6873 Set_Is_Immediately_Visible (P_Name, Vis);
6875 -- Remove from visibility the siblings as well, which are directly
6876 -- visible while the parent is in scope.
6878 E := First_Entity (P_Name);
6879 while Present (E) loop
6880 if Is_Child_Unit (E) then
6881 Set_Is_Immediately_Visible (E, False);
6882 end if;
6884 Next_Entity (E);
6885 end loop;
6887 Set_In_Package_Body (P_Name, False);
6889 -- This is the recursive call to remove the context of any higher
6890 -- level parent. This recursion ensures that all parents are removed
6891 -- in the reverse order of their installation.
6893 Remove_Parents (P);
6894 end if;
6895 end Remove_Parents;
6897 ---------------------------------
6898 -- Remove_Private_With_Clauses --
6899 ---------------------------------
6901 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6902 Item : Node_Id;
6904 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6905 -- Check whether a given unit appears in a regular with_clause. Used to
6906 -- determine whether a private_with_clause, implicit or explicit, should
6907 -- be ignored.
6909 ----------------------------
6910 -- In_Regular_With_Clause --
6911 ----------------------------
6913 function In_Regular_With_Clause (E : Entity_Id) return Boolean is
6914 Item : Node_Id;
6916 begin
6917 Item := First (Context_Items (Comp_Unit));
6919 while Present (Item) loop
6920 if Nkind (Item) = N_With_Clause
6922 -- The following guard is needed to ensure that the name has
6923 -- been properly analyzed before we go fetching its entity.
6925 and then Is_Entity_Name (Name (Item))
6926 and then Entity (Name (Item)) = E
6927 and then not Private_Present (Item)
6928 then
6929 return True;
6930 end if;
6932 Next (Item);
6933 end loop;
6935 return False;
6936 end In_Regular_With_Clause;
6938 -- Start of processing for Remove_Private_With_Clauses
6940 begin
6941 Item := First (Context_Items (Comp_Unit));
6942 while Present (Item) loop
6943 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
6945 -- If private_with_clause is redundant, remove it from context,
6946 -- as a small optimization to subsequent handling of private_with
6947 -- clauses in other nested packages. We replace the clause with
6948 -- a null statement, which is otherwise ignored by the rest of
6949 -- the compiler.
6951 if In_Regular_With_Clause (Entity (Name (Item))) then
6952 declare
6953 Nxt : constant Node_Id := Next (Item);
6954 begin
6955 Rewrite (Item, Make_Null_Statement (Sloc (Item)));
6956 Analyze (Item);
6957 Item := Nxt;
6958 end;
6960 elsif Limited_Present (Item) then
6961 if not Limited_View_Installed (Item) then
6962 Remove_Limited_With_Clause (Item);
6963 end if;
6965 Next (Item);
6967 else
6968 Remove_Unit_From_Visibility (Entity (Name (Item)));
6969 Set_Context_Installed (Item, False);
6970 Next (Item);
6971 end if;
6973 else
6974 Next (Item);
6975 end if;
6976 end loop;
6977 end Remove_Private_With_Clauses;
6979 ---------------------------------
6980 -- Remove_Unit_From_Visibility --
6981 ---------------------------------
6983 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6984 begin
6985 if Debug_Flag_I then
6986 Write_Str ("remove unit ");
6987 Write_Name (Chars (Unit_Name));
6988 Write_Str (" from visibility");
6989 Write_Eol;
6990 end if;
6992 Set_Is_Visible_Lib_Unit (Unit_Name, False);
6993 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6994 Set_Is_Immediately_Visible (Unit_Name, False);
6996 -- If the unit is a wrapper package, the subprogram instance is
6997 -- what must be removed from visibility.
6998 -- Should we use Related_Instance instead???
7000 if Is_Wrapper_Package (Unit_Name) then
7001 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
7002 end if;
7003 end Remove_Unit_From_Visibility;
7005 --------
7006 -- sm --
7007 --------
7009 procedure sm is
7010 begin
7011 null;
7012 end sm;
7014 -------------
7015 -- Unchain --
7016 -------------
7018 procedure Unchain (E : Entity_Id) is
7019 Prev : Entity_Id;
7021 begin
7022 Prev := Current_Entity (E);
7024 if No (Prev) then
7025 return;
7027 elsif Prev = E then
7028 Set_Name_Entity_Id (Chars (E), Homonym (E));
7030 else
7031 while Present (Prev) and then Homonym (Prev) /= E loop
7032 Prev := Homonym (Prev);
7033 end loop;
7035 if Present (Prev) then
7036 Set_Homonym (Prev, Homonym (E));
7037 end if;
7038 end if;
7040 if Debug_Flag_I then
7041 Write_Str (" (homonym) unchain ");
7042 Write_Name (Chars (E));
7043 Write_Eol;
7044 end if;
7045 end Unchain;
7047 end Sem_Ch10;