Reverting merge from trunk
[official-gcc.git] / gcc / ada / sem_ch10.adb
blob78520f8b0a163a6276f17eaadeff1a230726d6a1
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-2013, 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 Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Util; use Exp_Util;
32 with Elists; use Elists;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Impunit; use Impunit;
37 with Inline; use Inline;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Namet; use Namet;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Par_SCO; use Par_SCO;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinfo.CN; use Sinfo.CN;
64 with Sinput; use Sinput;
65 with Snames; use Snames;
66 with Style; use Style;
67 with Stylesw; use Stylesw;
68 with Tbuild; use Tbuild;
69 with Uname; use Uname;
71 package body Sem_Ch10 is
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 procedure Analyze_Context (N : Node_Id);
78 -- Analyzes items in the context clause of compilation unit
80 procedure Build_Limited_Views (N : Node_Id);
81 -- Build and decorate the list of shadow entities for a package mentioned
82 -- in a limited_with clause. If the package was not previously analyzed
83 -- then it also performs a basic decoration of the real entities. This is
84 -- required in order to avoid passing non-decorated entities to the
85 -- back-end. Implements Ada 2005 (AI-50217).
87 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
88 -- Check whether the source for the body of a compilation unit must be
89 -- included in a standalone library.
91 procedure Check_Private_Child_Unit (N : Node_Id);
92 -- If a with_clause mentions a private child unit, the compilation unit
93 -- must be a member of the same family, as described in 10.1.2.
95 procedure Check_Stub_Level (N : Node_Id);
96 -- Verify that a stub is declared immediately within a compilation unit,
97 -- and not in an inner frame.
99 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
100 -- When a child unit appears in a context clause, the implicit withs on
101 -- parents are made explicit, and with clauses are inserted in the context
102 -- clause before the one for the child. If a parent in the with_clause
103 -- is a renaming, the implicit with_clause is on the renaming whose name
104 -- is mentioned in the with_clause, and not on the package it renames.
105 -- N is the compilation unit whose list of context items receives the
106 -- implicit with_clauses.
108 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
109 -- Get defining entity of parent unit of a child unit. In most cases this
110 -- is the defining entity of the unit, but for a child instance whose
111 -- parent needs a body for inlining, the instantiation node of the parent
112 -- has not yet been rewritten as a package declaration, and the entity has
113 -- to be retrieved from the Instance_Spec of the unit.
115 function Has_With_Clause
116 (C_Unit : Node_Id;
117 Pack : Entity_Id;
118 Is_Limited : Boolean := False) return Boolean;
119 -- Determine whether compilation unit C_Unit contains a [limited] with
120 -- clause for package Pack. Use the flag Is_Limited to designate desired
121 -- clause kind.
123 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
124 -- If the main unit is a child unit, implicit withs are also added for
125 -- all its ancestors.
127 function In_Chain (E : Entity_Id) return Boolean;
128 -- Check that the shadow entity is not already in the homonym chain, for
129 -- example through a limited_with clause in a parent unit.
131 procedure Install_Context_Clauses (N : Node_Id);
132 -- Subsidiary to Install_Context and Install_Parents. Process all with
133 -- and use clauses for current unit and its library unit if any.
135 procedure Install_Limited_Context_Clauses (N : Node_Id);
136 -- Subsidiary to Install_Context. Process only limited with_clauses for
137 -- current unit. Implements Ada 2005 (AI-50217).
139 procedure Install_Limited_Withed_Unit (N : Node_Id);
140 -- Place shadow entities for a limited_with package in the visibility
141 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
143 procedure Install_Withed_Unit
144 (With_Clause : Node_Id;
145 Private_With_OK : Boolean := False);
146 -- If the unit is not a child unit, make unit immediately visible. The
147 -- caller ensures that the unit is not already currently installed. The
148 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
149 -- is called when compiling the private part of a package, or installing
150 -- the private declarations of a parent unit.
152 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
153 -- This procedure establishes the context for the compilation of a child
154 -- unit. If Lib_Unit is a child library spec then the context of the parent
155 -- is installed, and the parent itself made immediately visible, so that
156 -- the child unit is processed in the declarative region of the parent.
157 -- Install_Parents makes a recursive call to itself to ensure that all
158 -- parents are loaded in the nested case. If Lib_Unit is a library body,
159 -- the only effect of Install_Parents is to install the private decls of
160 -- the parents, because the visible parent declarations will have been
161 -- installed as part of the context of the corresponding spec.
163 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
164 -- In the compilation of a child unit, a child of any of the ancestor
165 -- units is directly visible if it is visible, because the parent is in
166 -- an enclosing scope. Iterate over context to find child units of U_Name
167 -- or of some ancestor of it.
169 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
170 -- When compiling a unit Q descended from some parent unit P, a limited
171 -- with_clause in the context of P that names some other ancestor of Q
172 -- must not be installed because the ancestor is immediately visible.
174 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
175 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
176 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
177 -- a library spec that has a parent. If the call to Is_Child_Spec returns
178 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
179 -- compilation unit for the parent spec.
181 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
182 -- Parent_Spec is non-empty, this is also a child unit.
184 procedure Remove_Context_Clauses (N : Node_Id);
185 -- Subsidiary of previous one. Remove use_ and with_clauses
187 procedure Remove_Limited_With_Clause (N : Node_Id);
188 -- Remove from visibility the shadow entities introduced for a package
189 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
191 procedure Remove_Parents (Lib_Unit : Node_Id);
192 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
193 -- contexts established by the corresponding call to Install_Parents are
194 -- removed. Remove_Parents contains a recursive call to itself to ensure
195 -- that all parents are removed in the nested case.
197 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
198 -- Reset all visibility flags on unit after compiling it, either as a main
199 -- unit or as a unit in the context.
201 procedure Unchain (E : Entity_Id);
202 -- Remove single entity from visibility list
204 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
205 -- Common processing for all stubs (subprograms, tasks, packages, and
206 -- protected cases). N is the stub to be analyzed. Once the subunit name
207 -- is established, load and analyze. Nam is the non-overloadable entity
208 -- for which the proper body provides a completion. Subprogram stubs are
209 -- handled differently because they can be declarations.
211 procedure sm;
212 -- A dummy procedure, for debugging use, called just before analyzing the
213 -- main unit (after dealing with any context clauses).
215 --------------------------
216 -- Limited_With_Clauses --
217 --------------------------
219 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
220 -- mutually recursive types declared in different units. A limited_with
221 -- clause that names package P in the context of unit U makes the types
222 -- declared in the visible part of P available within U, but with the
223 -- restriction that these types can only be used as incomplete types.
224 -- The limited_with clause does not impose a semantic dependence on P,
225 -- and it is possible for two packages to have limited_with_clauses on
226 -- each other without creating an elaboration circularity.
228 -- To support this feature, the analysis of a limited_with clause must
229 -- create an abbreviated view of the package, without performing any
230 -- semantic analysis on it. This "package abstract" contains shadow types
231 -- that are in one-one correspondence with the real types in the package,
232 -- and that have the properties of incomplete types.
234 -- The implementation creates two element lists: one to chain the shadow
235 -- entities, and one to chain the corresponding type entities in the tree
236 -- of the package. Links between corresponding entities in both chains
237 -- allow the compiler to select the proper view of a given type, depending
238 -- on the context. Note that in contrast with the handling of private
239 -- types, the limited view and the non-limited view of a type are treated
240 -- as separate entities, and no entity exchange needs to take place, which
241 -- makes the implementation must simpler than could be feared.
243 ------------------------------
244 -- Analyze_Compilation_Unit --
245 ------------------------------
247 procedure Analyze_Compilation_Unit (N : Node_Id) is
248 Unit_Node : constant Node_Id := Unit (N);
249 Lib_Unit : Node_Id := Library_Unit (N);
250 Spec_Id : Entity_Id;
251 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
252 Par_Spec_Name : Unit_Name_Type;
253 Unum : Unit_Number_Type;
255 procedure Check_Redundant_Withs
256 (Context_Items : List_Id;
257 Spec_Context_Items : List_Id := No_List);
258 -- Determine whether the context list of a compilation unit contains
259 -- redundant with clauses. When checking body clauses against spec
260 -- clauses, set Context_Items to the context list of the body and
261 -- Spec_Context_Items to that of the spec. Parent packages are not
262 -- examined for documentation purposes.
264 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
265 -- Generate cross-reference information for the parents of child units.
266 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
268 ---------------------------
269 -- Check_Redundant_Withs --
270 ---------------------------
272 procedure Check_Redundant_Withs
273 (Context_Items : List_Id;
274 Spec_Context_Items : List_Id := No_List)
276 Clause : Node_Id;
278 procedure Process_Body_Clauses
279 (Context_List : List_Id;
280 Clause : Node_Id;
281 Used : in out Boolean;
282 Used_Type_Or_Elab : in out Boolean);
283 -- Examine the context clauses of a package body, trying to match the
284 -- name entity of Clause with any list element. If the match occurs
285 -- on a use package clause set Used to True, for a use type clause or
286 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
288 procedure Process_Spec_Clauses
289 (Context_List : List_Id;
290 Clause : Node_Id;
291 Used : in out Boolean;
292 Withed : in out Boolean;
293 Exit_On_Self : Boolean := False);
294 -- Examine the context clauses of a package spec, trying to match
295 -- the name entity of Clause with any list element. If the match
296 -- occurs on a use package clause, set Used to True, for a with
297 -- package clause other than Clause, set Withed to True. Limited
298 -- with clauses, implicitly generated with clauses and withs
299 -- having pragmas Elaborate or Elaborate_All applied to them are
300 -- skipped. Exit_On_Self is used to control the search loop and
301 -- force an exit whenever Clause sees itself in the search.
303 --------------------------
304 -- Process_Body_Clauses --
305 --------------------------
307 procedure Process_Body_Clauses
308 (Context_List : List_Id;
309 Clause : Node_Id;
310 Used : in out Boolean;
311 Used_Type_Or_Elab : in out Boolean)
313 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
314 Cont_Item : Node_Id;
315 Prag_Unit : Node_Id;
316 Subt_Mark : Node_Id;
317 Use_Item : Node_Id;
319 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
320 -- In an expanded name in a use clause, if the prefix is a renamed
321 -- package, the entity is set to the original package as a result,
322 -- when checking whether the package appears in a previous with
323 -- clause, the renaming has to be taken into account, to prevent
324 -- spurious/incorrect warnings. A common case is use of Text_IO.
326 ---------------
327 -- Same_Unit --
328 ---------------
330 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
331 begin
332 return Entity (N) = P
333 or else (Present (Renamed_Object (P))
334 and then Entity (N) = Renamed_Object (P));
335 end Same_Unit;
337 -- Start of processing for Process_Body_Clauses
339 begin
340 Used := False;
341 Used_Type_Or_Elab := False;
343 Cont_Item := First (Context_List);
344 while Present (Cont_Item) loop
346 -- Package use clause
348 if Nkind (Cont_Item) = N_Use_Package_Clause
349 and then not Used
350 then
351 -- Search through use clauses
353 Use_Item := First (Names (Cont_Item));
354 while Present (Use_Item) and then not Used loop
356 -- Case of a direct use of the one we are looking for
358 if Entity (Use_Item) = Nam_Ent then
359 Used := True;
361 -- Handle nested case, as in "with P; use P.Q.R"
363 else
364 declare
365 UE : Node_Id;
367 begin
368 -- Loop through prefixes looking for match
370 UE := Use_Item;
371 while Nkind (UE) = N_Expanded_Name loop
372 if Same_Unit (Prefix (UE), Nam_Ent) then
373 Used := True;
374 exit;
375 end if;
377 UE := Prefix (UE);
378 end loop;
379 end;
380 end if;
382 Next (Use_Item);
383 end loop;
385 -- USE TYPE clause
387 elsif Nkind (Cont_Item) = N_Use_Type_Clause
388 and then not Used_Type_Or_Elab
389 then
390 Subt_Mark := First (Subtype_Marks (Cont_Item));
391 while Present (Subt_Mark)
392 and then not Used_Type_Or_Elab
393 loop
394 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
395 Used_Type_Or_Elab := True;
396 end if;
398 Next (Subt_Mark);
399 end loop;
401 -- Pragma Elaborate or Elaborate_All
403 elsif Nkind (Cont_Item) = N_Pragma
404 and then
405 Nam_In (Pragma_Name (Cont_Item), Name_Elaborate,
406 Name_Elaborate_All)
407 and then not Used_Type_Or_Elab
408 then
409 Prag_Unit :=
410 First (Pragma_Argument_Associations (Cont_Item));
411 while Present (Prag_Unit) and then not Used_Type_Or_Elab loop
412 if Entity (Expression (Prag_Unit)) = Nam_Ent then
413 Used_Type_Or_Elab := True;
414 end if;
416 Next (Prag_Unit);
417 end loop;
418 end if;
420 Next (Cont_Item);
421 end loop;
422 end Process_Body_Clauses;
424 --------------------------
425 -- Process_Spec_Clauses --
426 --------------------------
428 procedure Process_Spec_Clauses
429 (Context_List : List_Id;
430 Clause : Node_Id;
431 Used : in out Boolean;
432 Withed : in out Boolean;
433 Exit_On_Self : Boolean := False)
435 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
436 Cont_Item : Node_Id;
437 Use_Item : Node_Id;
439 begin
440 Used := False;
441 Withed := False;
443 Cont_Item := First (Context_List);
444 while Present (Cont_Item) loop
446 -- Stop the search since the context items after Cont_Item have
447 -- already been examined in a previous iteration of the reverse
448 -- loop in Check_Redundant_Withs.
450 if Exit_On_Self
451 and Cont_Item = Clause
452 then
453 exit;
454 end if;
456 -- Package use clause
458 if Nkind (Cont_Item) = N_Use_Package_Clause
459 and then not Used
460 then
461 Use_Item := First (Names (Cont_Item));
462 while Present (Use_Item) and then not Used loop
463 if Entity (Use_Item) = Nam_Ent then
464 Used := True;
465 end if;
467 Next (Use_Item);
468 end loop;
470 -- Package with clause. Avoid processing self, implicitly
471 -- generated with clauses or limited with clauses. Note that
472 -- we examine with clauses having pragmas Elaborate or
473 -- Elaborate_All applied to them due to cases such as:
475 -- with Pack;
476 -- with Pack;
477 -- pragma Elaborate (Pack);
479 -- In this case, the second with clause is redundant since
480 -- the pragma applies only to the first "with Pack;".
482 -- Note that we only consider with_clauses that comes from
483 -- source. In the case of renamings used as prefixes of names
484 -- in with_clauses, we generate a with_clause for the prefix,
485 -- which we do not treat as implicit because it is needed for
486 -- visibility analysis, but is also not redundant.
488 elsif Nkind (Cont_Item) = N_With_Clause
489 and then not Implicit_With (Cont_Item)
490 and then Comes_From_Source (Cont_Item)
491 and then not Limited_Present (Cont_Item)
492 and then Cont_Item /= Clause
493 and then Entity (Name (Cont_Item)) = Nam_Ent
494 then
495 Withed := True;
496 end if;
498 Next (Cont_Item);
499 end loop;
500 end Process_Spec_Clauses;
502 -- Start of processing for Check_Redundant_Withs
504 begin
505 Clause := Last (Context_Items);
506 while Present (Clause) loop
508 -- Avoid checking implicitly generated with clauses, limited with
509 -- clauses or withs that have pragma Elaborate or Elaborate_All.
511 if Nkind (Clause) = N_With_Clause
512 and then not Implicit_With (Clause)
513 and then not Limited_Present (Clause)
514 and then not Elaborate_Present (Clause)
515 then
516 -- Package body-to-spec check
518 if Present (Spec_Context_Items) then
519 declare
520 Used_In_Body : Boolean := False;
521 Used_In_Spec : Boolean := False;
522 Used_Type_Or_Elab : Boolean := False;
523 Withed_In_Spec : Boolean := False;
525 begin
526 Process_Spec_Clauses
527 (Context_List => Spec_Context_Items,
528 Clause => Clause,
529 Used => Used_In_Spec,
530 Withed => Withed_In_Spec);
532 Process_Body_Clauses
533 (Context_List => Context_Items,
534 Clause => Clause,
535 Used => Used_In_Body,
536 Used_Type_Or_Elab => Used_Type_Or_Elab);
538 -- "Type Elab" refers to the presence of either a use
539 -- type clause, pragmas Elaborate or Elaborate_All.
541 -- +---------------+---------------------------+------+
542 -- | Spec | Body | Warn |
543 -- +--------+------+--------+------+-----------+------+
544 -- | Withed | Used | Withed | Used | Type Elab | |
545 -- | X | | X | | | X |
546 -- | X | | X | X | | |
547 -- | X | | X | | X | |
548 -- | X | | X | X | X | |
549 -- | X | X | X | | | X |
550 -- | X | X | X | | X | |
551 -- | X | X | X | X | | X |
552 -- | X | X | X | X | X | |
553 -- +--------+------+--------+------+-----------+------+
555 if (Withed_In_Spec
556 and then not Used_Type_Or_Elab)
557 and then
558 ((not Used_In_Spec and then not Used_In_Body)
559 or else Used_In_Spec)
560 then
561 Error_Msg_N -- CODEFIX
562 ("redundant with clause in body??", Clause);
563 end if;
565 Used_In_Body := False;
566 Used_In_Spec := False;
567 Used_Type_Or_Elab := False;
568 Withed_In_Spec := False;
569 end;
571 -- Standalone package spec or body check
573 else
574 declare
575 Dont_Care : Boolean := False;
576 Withed : Boolean := False;
578 begin
579 -- The mechanism for examining the context clauses of a
580 -- package spec can be applied to package body clauses.
582 Process_Spec_Clauses
583 (Context_List => Context_Items,
584 Clause => Clause,
585 Used => Dont_Care,
586 Withed => Withed,
587 Exit_On_Self => True);
589 if Withed then
590 Error_Msg_N -- CODEFIX
591 ("redundant with clause??", Clause);
592 end if;
593 end;
594 end if;
595 end if;
597 Prev (Clause);
598 end loop;
599 end Check_Redundant_Withs;
601 --------------------------------
602 -- Generate_Parent_References --
603 --------------------------------
605 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
606 Pref : Node_Id;
607 P_Name : Entity_Id := P_Id;
609 begin
610 Pref := Name (Parent (Defining_Entity (N)));
612 if Nkind (Pref) = N_Expanded_Name then
614 -- Done already, if the unit has been compiled indirectly as
615 -- part of the closure of its context because of inlining.
617 return;
618 end if;
620 while Nkind (Pref) = N_Selected_Component loop
621 Change_Selected_Component_To_Expanded_Name (Pref);
622 Set_Entity (Pref, P_Name);
623 Set_Etype (Pref, Etype (P_Name));
624 Generate_Reference (P_Name, Pref, 'r');
625 Pref := Prefix (Pref);
626 P_Name := Scope (P_Name);
627 end loop;
629 -- The guard here on P_Name is to handle the error condition where
630 -- the parent unit is missing because the file was not found.
632 if Present (P_Name) then
633 Set_Entity (Pref, P_Name);
634 Set_Etype (Pref, Etype (P_Name));
635 Generate_Reference (P_Name, Pref, 'r');
636 Style.Check_Identifier (Pref, P_Name);
637 end if;
638 end Generate_Parent_References;
640 -- Start of processing for Analyze_Compilation_Unit
642 begin
643 Process_Compilation_Unit_Pragmas (N);
645 -- If the unit is a subunit whose parent has not been analyzed (which
646 -- indicates that the main unit is a subunit, either the current one or
647 -- one of its descendents) then the subunit is compiled as part of the
648 -- analysis of the parent, which we proceed to do. Basically this gets
649 -- handled from the top down and we don't want to do anything at this
650 -- level (i.e. this subunit will be handled on the way down from the
651 -- parent), so at this level we immediately return. If the subunit ends
652 -- up not analyzed, it means that the parent did not contain a stub for
653 -- it, or that there errors were detected in some ancestor.
655 if Nkind (Unit_Node) = N_Subunit and then not Analyzed (Lib_Unit) then
656 Semantics (Lib_Unit);
658 if not Analyzed (Proper_Body (Unit_Node)) then
659 if Serious_Errors_Detected > 0 then
660 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
661 else
662 Error_Msg_N ("missing stub for subunit", N);
663 end if;
664 end if;
666 return;
667 end if;
669 -- Analyze context (this will call Sem recursively for with'ed units) To
670 -- detect circularities among with-clauses that are not caught during
671 -- loading, we set the Context_Pending flag on the current unit. If the
672 -- flag is already set there is a potential circularity. We exclude
673 -- predefined units from this check because they are known to be safe.
674 -- We also exclude package bodies that are present because circularities
675 -- between bodies are harmless (and necessary).
677 if Context_Pending (N) then
678 declare
679 Circularity : Boolean := True;
681 begin
682 if Is_Predefined_File_Name
683 (Unit_File_Name (Get_Source_Unit (Unit (N))))
684 then
685 Circularity := False;
687 else
688 for U in Main_Unit + 1 .. Last_Unit loop
689 if Nkind (Unit (Cunit (U))) = N_Package_Body
690 and then not Analyzed (Cunit (U))
691 then
692 Circularity := False;
693 exit;
694 end if;
695 end loop;
696 end if;
698 if Circularity then
699 Error_Msg_N ("circular dependency caused by with_clauses", N);
700 Error_Msg_N
701 ("\possibly missing limited_with clause"
702 & " in one of the following", N);
704 for U in Main_Unit .. Last_Unit loop
705 if Context_Pending (Cunit (U)) then
706 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
707 Error_Msg_N ("\unit$", N);
708 end if;
709 end loop;
711 raise Unrecoverable_Error;
712 end if;
713 end;
714 else
715 Set_Context_Pending (N);
716 end if;
718 Analyze_Context (N);
720 Set_Context_Pending (N, False);
722 -- If the unit is a package body, the spec is already loaded and must be
723 -- analyzed first, before we analyze the body.
725 if Nkind (Unit_Node) = N_Package_Body then
727 -- If no Lib_Unit, then there was a serious previous error, so just
728 -- ignore the entire analysis effort
730 if No (Lib_Unit) then
731 Check_Error_Detected;
732 return;
734 else
735 -- Analyze the package spec
737 Semantics (Lib_Unit);
739 -- Check for unused with's
741 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
743 -- Verify that the library unit is a package declaration
745 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
746 N_Generic_Package_Declaration)
747 then
748 Error_Msg_N
749 ("no legal package declaration for package body", N);
750 return;
752 -- Otherwise, the entity in the declaration is visible. Update the
753 -- version to reflect dependence of this body on the spec.
755 else
756 Spec_Id := Defining_Entity (Unit (Lib_Unit));
757 Set_Is_Immediately_Visible (Spec_Id, True);
758 Version_Update (N, Lib_Unit);
760 if Nkind (Defining_Unit_Name (Unit_Node)) =
761 N_Defining_Program_Unit_Name
762 then
763 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
764 end if;
765 end if;
766 end if;
768 -- If the unit is a subprogram body, then we similarly need to analyze
769 -- its spec. However, things are a little simpler in this case, because
770 -- here, this analysis is done mostly for error checking and consistency
771 -- purposes (but not only, e.g. there could be a contract on the spec),
772 -- so there's nothing else to be done.
774 elsif Nkind (Unit_Node) = N_Subprogram_Body then
775 if Acts_As_Spec (N) then
777 -- If the subprogram body is a child unit, we must create a
778 -- declaration for it, in order to properly load the parent(s).
779 -- After this, the original unit does not acts as a spec, because
780 -- there is an explicit one. If this unit appears in a context
781 -- clause, then an implicit with on the parent will be added when
782 -- installing the context. If this is the main unit, there is no
783 -- Unit_Table entry for the declaration (it has the unit number
784 -- of the main unit) and code generation is unaffected.
786 Unum := Get_Cunit_Unit_Number (N);
787 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
789 if Par_Spec_Name /= No_Unit_Name then
790 Unum :=
791 Load_Unit
792 (Load_Name => Par_Spec_Name,
793 Required => True,
794 Subunit => False,
795 Error_Node => N);
797 if Unum /= No_Unit then
799 -- Build subprogram declaration and attach parent unit to it
800 -- This subprogram declaration does not come from source,
801 -- Nevertheless the backend must generate debugging info for
802 -- it, and this must be indicated explicitly. We also mark
803 -- the body entity as a child unit now, to prevent a
804 -- cascaded error if the spec entity cannot be entered
805 -- in its scope. Finally we create a Units table entry for
806 -- the subprogram declaration, to maintain a one-to-one
807 -- correspondence with compilation unit nodes. This is
808 -- critical for the tree traversals performed by CodePeer.
810 declare
811 Loc : constant Source_Ptr := Sloc (N);
812 SCS : constant Boolean :=
813 Get_Comes_From_Source_Default;
815 begin
816 Set_Comes_From_Source_Default (False);
818 -- Checks for redundant USE TYPE clauses have a special
819 -- exception for the synthetic spec we create here. This
820 -- special case relies on the two compilation units
821 -- sharing the same context clause.
823 -- Note: We used to do a shallow copy (New_Copy_List),
824 -- which defeated those checks and also created malformed
825 -- trees (subtype mark shared by two distinct
826 -- N_Use_Type_Clause nodes) which crashed the compiler.
828 Lib_Unit :=
829 Make_Compilation_Unit (Loc,
830 Context_Items => Context_Items (N),
831 Unit =>
832 Make_Subprogram_Declaration (Sloc (N),
833 Specification =>
834 Copy_Separate_Tree
835 (Specification (Unit_Node))),
836 Aux_Decls_Node =>
837 Make_Compilation_Unit_Aux (Loc));
839 Set_Library_Unit (N, Lib_Unit);
840 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
841 Make_Child_Decl_Unit (N);
842 Semantics (Lib_Unit);
844 -- Now that a separate declaration exists, the body
845 -- of the child unit does not act as spec any longer.
847 Set_Acts_As_Spec (N, False);
848 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
849 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
850 Set_Comes_From_Source_Default (SCS);
851 end;
852 end if;
853 end if;
855 -- Here for subprogram with separate declaration
857 else
858 Semantics (Lib_Unit);
859 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
860 Version_Update (N, Lib_Unit);
861 end if;
863 -- If this is a child unit, generate references to the parents
865 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
866 N_Defining_Program_Unit_Name
867 then
868 Generate_Parent_References (
869 Specification (Unit_Node),
870 Scope (Defining_Entity (Unit (Lib_Unit))));
871 end if;
872 end if;
874 -- If it is a child unit, the parent must be elaborated first and we
875 -- update version, since we are dependent on our parent.
877 if Is_Child_Spec (Unit_Node) then
879 -- The analysis of the parent is done with style checks off
881 declare
882 Save_Style_Check : constant Boolean := Style_Check;
884 begin
885 if not GNAT_Mode then
886 Style_Check := False;
887 end if;
889 Semantics (Parent_Spec (Unit_Node));
890 Version_Update (N, Parent_Spec (Unit_Node));
892 -- Restore style check settings
894 Style_Check := Save_Style_Check;
895 end;
896 end if;
898 -- With the analysis done, install the context. Note that we can't
899 -- install the context from the with clauses as we analyze them, because
900 -- each with clause must be analyzed in a clean visibility context, so
901 -- we have to wait and install them all at once.
903 Install_Context (N);
905 if Is_Child_Spec (Unit_Node) then
907 -- Set the entities of all parents in the program_unit_name
909 Generate_Parent_References (
910 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
911 end if;
913 -- All components of the context: with-clauses, library unit, ancestors
914 -- if any, (and their context) are analyzed and installed.
916 -- Call special debug routine sm if this is the main unit
918 if Current_Sem_Unit = Main_Unit then
920 end if;
922 -- Now analyze the unit (package, subprogram spec, body) itself
924 Analyze (Unit_Node);
926 if Warn_On_Redundant_Constructs then
927 Check_Redundant_Withs (Context_Items (N));
929 if Nkind (Unit_Node) = N_Package_Body then
930 Check_Redundant_Withs
931 (Context_Items => Context_Items (N),
932 Spec_Context_Items => Context_Items (Lib_Unit));
933 end if;
934 end if;
936 -- The above call might have made Unit_Node an N_Subprogram_Body from
937 -- something else, so propagate any Acts_As_Spec flag.
939 if Nkind (Unit_Node) = N_Subprogram_Body
940 and then Acts_As_Spec (Unit_Node)
941 then
942 Set_Acts_As_Spec (N);
943 end if;
945 -- Register predefined units in Rtsfind
947 declare
948 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
949 begin
950 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
951 Set_RTU_Loaded (Unit_Node);
952 end if;
953 end;
955 -- Treat compilation unit pragmas that appear after the library unit
957 if Present (Pragmas_After (Aux_Decls_Node (N))) then
958 declare
959 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
960 begin
961 while Present (Prag_Node) loop
962 Analyze (Prag_Node);
963 Next (Prag_Node);
964 end loop;
965 end;
966 end if;
968 -- Generate distribution stubs if requested and no error
970 if N = Main_Cunit
971 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
972 or else
973 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
974 and then not Fatal_Error (Main_Unit)
975 then
976 if Is_RCI_Pkg_Spec_Or_Body (N) then
978 -- Regular RCI package
980 Add_Stub_Constructs (N);
982 elsif (Nkind (Unit_Node) = N_Package_Declaration
983 and then Is_Shared_Passive (Defining_Entity
984 (Specification (Unit_Node))))
985 or else (Nkind (Unit_Node) = N_Package_Body
986 and then
987 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
988 then
989 -- Shared passive package
991 Add_Stub_Constructs (N);
993 elsif Nkind (Unit_Node) = N_Package_Instantiation
994 and then
995 Is_Remote_Call_Interface
996 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
997 then
998 -- Instantiation of a RCI generic package
1000 Add_Stub_Constructs (N);
1001 end if;
1002 end if;
1004 -- Remove unit from visibility, so that environment is clean for the
1005 -- next compilation, which is either the main unit or some other unit
1006 -- in the context.
1008 if Nkind_In (Unit_Node, N_Package_Declaration,
1009 N_Package_Renaming_Declaration,
1010 N_Subprogram_Declaration)
1011 or else Nkind (Unit_Node) in N_Generic_Declaration
1012 or else (Nkind (Unit_Node) = N_Subprogram_Body
1013 and then Acts_As_Spec (Unit_Node))
1014 then
1015 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
1017 -- If the unit is an instantiation whose body will be elaborated for
1018 -- inlining purposes, use the proper entity of the instance. The entity
1019 -- may be missing if the instantiation was illegal.
1021 elsif Nkind (Unit_Node) = N_Package_Instantiation
1022 and then not Error_Posted (Unit_Node)
1023 and then Present (Instance_Spec (Unit_Node))
1024 then
1025 Remove_Unit_From_Visibility
1026 (Defining_Entity (Instance_Spec (Unit_Node)));
1028 elsif Nkind (Unit_Node) = N_Package_Body
1029 or else (Nkind (Unit_Node) = N_Subprogram_Body
1030 and then not Acts_As_Spec (Unit_Node))
1031 then
1032 -- Bodies that are not the main unit are compiled if they are generic
1033 -- or contain generic or inlined units. Their analysis brings in the
1034 -- context of the corresponding spec (unit declaration) which must be
1035 -- removed as well, to return the compilation environment to its
1036 -- proper state.
1038 Remove_Context (Lib_Unit);
1039 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1040 end if;
1042 -- Last step is to deinstall the context we just installed as well as
1043 -- the unit just compiled.
1045 Remove_Context (N);
1047 -- If this is the main unit and we are generating code, we must check
1048 -- that all generic units in the context have a body if they need it,
1049 -- even if they have not been instantiated. In the absence of .ali files
1050 -- for generic units, we must force the load of the body, just to
1051 -- produce the proper error if the body is absent. We skip this
1052 -- verification if the main unit itself is generic.
1054 if Get_Cunit_Unit_Number (N) = Main_Unit
1055 and then Operating_Mode = Generate_Code
1056 and then Expander_Active
1057 then
1058 -- Check whether the source for the body of the unit must be included
1059 -- in a standalone library.
1061 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1063 -- Indicate that the main unit is now analyzed, to catch possible
1064 -- circularities between it and generic bodies. Remove main unit from
1065 -- visibility. This might seem superfluous, but the main unit must
1066 -- not be visible in the generic body expansions that follow.
1068 Set_Analyzed (N, True);
1069 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1071 declare
1072 Item : Node_Id;
1073 Nam : Entity_Id;
1074 Un : Unit_Number_Type;
1076 Save_Style_Check : constant Boolean := Style_Check;
1078 begin
1079 Item := First (Context_Items (N));
1080 while Present (Item) loop
1082 -- Check for explicit with clause
1084 if Nkind (Item) = N_With_Clause
1085 and then not Implicit_With (Item)
1087 -- Ada 2005 (AI-50217): Ignore limited-withed units
1089 and then not Limited_Present (Item)
1090 then
1091 Nam := Entity (Name (Item));
1093 -- Compile generic subprogram, unless it is intrinsic or
1094 -- imported so no body is required, or generic package body
1095 -- if the package spec requires a body.
1097 if (Is_Generic_Subprogram (Nam)
1098 and then not Is_Intrinsic_Subprogram (Nam)
1099 and then not Is_Imported (Nam))
1100 or else (Ekind (Nam) = E_Generic_Package
1101 and then Unit_Requires_Body (Nam))
1102 then
1103 Style_Check := False;
1105 if Present (Renamed_Object (Nam)) then
1106 Un :=
1107 Load_Unit
1108 (Load_Name => Get_Body_Name
1109 (Get_Unit_Name
1110 (Unit_Declaration_Node
1111 (Renamed_Object (Nam)))),
1112 Required => False,
1113 Subunit => False,
1114 Error_Node => N,
1115 Renamings => True);
1116 else
1117 Un :=
1118 Load_Unit
1119 (Load_Name => Get_Body_Name
1120 (Get_Unit_Name (Item)),
1121 Required => False,
1122 Subunit => False,
1123 Error_Node => N,
1124 Renamings => True);
1125 end if;
1127 if Un = No_Unit then
1128 Error_Msg_NE
1129 ("body of generic unit& not found", Item, Nam);
1130 exit;
1132 elsif not Analyzed (Cunit (Un))
1133 and then Un /= Main_Unit
1134 and then not Fatal_Error (Un)
1135 then
1136 Style_Check := False;
1137 Semantics (Cunit (Un));
1138 end if;
1139 end if;
1140 end if;
1142 Next (Item);
1143 end loop;
1145 -- Restore style checks settings
1147 Style_Check := Save_Style_Check;
1148 end;
1149 end if;
1151 -- Deal with creating elaboration Boolean if needed. We create an
1152 -- elaboration boolean only for units that come from source since
1153 -- units manufactured by the compiler never need elab checks.
1155 if Comes_From_Source (N)
1156 and then Nkind_In (Unit_Node, N_Package_Declaration,
1157 N_Generic_Package_Declaration,
1158 N_Subprogram_Declaration,
1159 N_Generic_Subprogram_Declaration)
1160 then
1161 declare
1162 Loc : constant Source_Ptr := Sloc (N);
1163 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1165 begin
1166 Spec_Id := Defining_Entity (Unit_Node);
1167 Generate_Definition (Spec_Id);
1169 -- See if an elaboration entity is required for possible access
1170 -- before elaboration checking. Note that we must allow for this
1171 -- even if -gnatE is not set, since a client may be compiled in
1172 -- -gnatE mode and reference the entity.
1174 -- These entities are also used by the binder to prevent multiple
1175 -- attempts to execute the elaboration code for the library case
1176 -- where the elaboration routine might otherwise be called more
1177 -- than once.
1179 -- Case of units which do not require elaboration checks
1182 -- Pure units do not need checks
1184 Is_Pure (Spec_Id)
1186 -- Preelaborated units do not need checks
1188 or else Is_Preelaborated (Spec_Id)
1190 -- No checks needed if pragma Elaborate_Body present
1192 or else Has_Pragma_Elaborate_Body (Spec_Id)
1194 -- No checks needed if unit does not require a body
1196 or else not Unit_Requires_Body (Spec_Id)
1198 -- No checks needed for predefined files
1200 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1202 -- No checks required if no separate spec
1204 or else Acts_As_Spec (N)
1205 then
1206 -- This is a case where we only need the entity for
1207 -- checking to prevent multiple elaboration checks.
1209 Set_Elaboration_Entity_Required (Spec_Id, False);
1211 -- Case of elaboration entity is required for access before
1212 -- elaboration checking (so certainly we must build it!)
1214 else
1215 Set_Elaboration_Entity_Required (Spec_Id, True);
1216 end if;
1218 Build_Elaboration_Entity (N, Spec_Id);
1219 end;
1220 end if;
1222 -- Freeze the compilation unit entity. This for sure is needed because
1223 -- of some warnings that can be output (see Freeze_Subprogram), but may
1224 -- in general be required. If freezing actions result, place them in the
1225 -- compilation unit actions list, and analyze them.
1227 declare
1228 L : constant List_Id :=
1229 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1230 begin
1231 while Is_Non_Empty_List (L) loop
1232 Insert_Library_Level_Action (Remove_Head (L));
1233 end loop;
1234 end;
1236 Set_Analyzed (N);
1238 if Nkind (Unit_Node) = N_Package_Declaration
1239 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1240 and then Expander_Active
1241 then
1242 declare
1243 Save_Style_Check : constant Boolean := Style_Check;
1244 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1245 Options : Style_Check_Options;
1247 begin
1248 Save_Style_Check_Options (Options);
1249 Reset_Style_Check_Options;
1250 Opt.Warning_Mode := Suppress;
1251 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1253 Reset_Style_Check_Options;
1254 Set_Style_Check_Options (Options);
1255 Style_Check := Save_Style_Check;
1256 Warning_Mode := Save_Warning;
1257 end;
1258 end if;
1260 -- If we are generating obsolescent warnings, then here is where we
1261 -- generate them for the with'ed items. The reason for this special
1262 -- processing is that the normal mechanism of generating the warnings
1263 -- for referenced entities does not work for context clause references.
1264 -- That's because when we first analyze the context, it is too early to
1265 -- know if the with'ing unit is itself obsolescent (which suppresses
1266 -- the warnings).
1268 if not GNAT_Mode
1269 and then Warn_On_Obsolescent_Feature
1270 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1271 then
1272 -- Push current compilation unit as scope, so that the test for
1273 -- being within an obsolescent unit will work correctly. The check
1274 -- is not performed within an instantiation, because the warning
1275 -- will have been emitted in the corresponding generic unit.
1277 Push_Scope (Defining_Entity (Unit_Node));
1279 -- Loop through context items to deal with with clauses
1281 declare
1282 Item : Node_Id;
1283 Nam : Node_Id;
1284 Ent : Entity_Id;
1286 begin
1287 Item := First (Context_Items (N));
1288 while Present (Item) loop
1289 if Nkind (Item) = N_With_Clause
1291 -- Suppress this check in limited-withed units. Further work
1292 -- needed here if we decide to incorporate this check on
1293 -- limited-withed units.
1295 and then not Limited_Present (Item)
1296 then
1297 Nam := Name (Item);
1298 Ent := Entity (Nam);
1300 if Is_Obsolescent (Ent) then
1301 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1302 end if;
1303 end if;
1305 Next (Item);
1306 end loop;
1307 end;
1309 -- Remove temporary install of current unit as scope
1311 Pop_Scope;
1312 end if;
1313 end Analyze_Compilation_Unit;
1315 ---------------------
1316 -- Analyze_Context --
1317 ---------------------
1319 procedure Analyze_Context (N : Node_Id) is
1320 Ukind : constant Node_Kind := Nkind (Unit (N));
1321 Item : Node_Id;
1323 begin
1324 -- First process all configuration pragmas at the start of the context
1325 -- items. Strictly these are not part of the context clause, but that
1326 -- is where the parser puts them. In any case for sure we must analyze
1327 -- these before analyzing the actual context items, since they can have
1328 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1329 -- be with'ed as a result of changing categorizations in Ada 2005).
1331 Item := First (Context_Items (N));
1332 while Present (Item)
1333 and then Nkind (Item) = N_Pragma
1334 and then Pragma_Name (Item) in Configuration_Pragma_Names
1335 loop
1336 Analyze (Item);
1337 Next (Item);
1338 end loop;
1340 -- This is the point at which we capture the configuration settings
1341 -- for the unit. At the moment only the Optimize_Alignment setting
1342 -- needs to be captured. Probably more later ???
1344 if Optimize_Alignment_Local then
1345 Set_OA_Setting (Current_Sem_Unit, 'L');
1346 else
1347 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1348 end if;
1350 -- Loop through actual context items. This is done in two passes:
1352 -- a) The first pass analyzes non-limited with-clauses and also any
1353 -- configuration pragmas (we need to get the latter analyzed right
1354 -- away, since they can affect processing of subsequent items.
1356 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1358 while Present (Item) loop
1360 -- For with clause, analyze the with clause, and then update the
1361 -- version, since we are dependent on a unit that we with.
1363 if Nkind (Item) = N_With_Clause
1364 and then not Limited_Present (Item)
1365 then
1366 -- Skip analyzing with clause if no unit, nothing to do (this
1367 -- happens for a with that references a non-existent unit). Skip
1368 -- as well if this is a with_clause for the main unit, which
1369 -- happens if a subunit has a useless with_clause on its parent.
1371 if Present (Library_Unit (Item)) then
1372 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1373 Analyze (Item);
1375 else
1376 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1377 end if;
1378 end if;
1380 if not Implicit_With (Item) then
1381 Version_Update (N, Library_Unit (Item));
1382 end if;
1384 -- Skip pragmas. Configuration pragmas at the start were handled in
1385 -- the loop above, and remaining pragmas are not processed until we
1386 -- actually install the context (see Install_Context). We delay the
1387 -- analysis of these pragmas to make sure that we have installed all
1388 -- the implicit with's on parent units.
1390 -- Skip use clauses at this stage, since we don't want to do any
1391 -- installing of potentially use-visible entities until we
1392 -- actually install the complete context (in Install_Context).
1393 -- Otherwise things can get installed in the wrong context.
1395 else
1396 null;
1397 end if;
1399 Next (Item);
1400 end loop;
1402 -- Second pass: examine all limited_with clauses. All other context
1403 -- items are ignored in this pass.
1405 Item := First (Context_Items (N));
1406 while Present (Item) loop
1407 if Nkind (Item) = N_With_Clause
1408 and then Limited_Present (Item)
1409 then
1410 -- No need to check errors on implicitly generated limited-with
1411 -- clauses.
1413 if not Implicit_With (Item) then
1415 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1416 -- properly rejected, including renaming declarations.
1418 if not Nkind_In (Ukind, N_Package_Declaration,
1419 N_Subprogram_Declaration)
1420 and then Ukind not in N_Generic_Declaration
1421 and then Ukind not in N_Generic_Instantiation
1422 then
1423 Error_Msg_N ("limited with_clause not allowed here", Item);
1425 -- Check wrong use of a limited with clause applied to the
1426 -- compilation unit containing the limited-with clause.
1428 -- limited with P.Q;
1429 -- package P.Q is ...
1431 elsif Unit (Library_Unit (Item)) = Unit (N) then
1432 Error_Msg_N ("wrong use of limited-with clause", Item);
1434 -- Check wrong use of limited-with clause applied to some
1435 -- immediate ancestor.
1437 elsif Is_Child_Spec (Unit (N)) then
1438 declare
1439 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1440 P : Node_Id;
1442 begin
1443 P := Parent_Spec (Unit (N));
1444 loop
1445 if Unit (P) = Lib_U then
1446 Error_Msg_N ("limited with_clause cannot "
1447 & "name ancestor", Item);
1448 exit;
1449 end if;
1451 exit when not Is_Child_Spec (Unit (P));
1452 P := Parent_Spec (Unit (P));
1453 end loop;
1454 end;
1455 end if;
1457 -- Check if the limited-withed unit is already visible through
1458 -- some context clause of the current compilation unit or some
1459 -- ancestor of the current compilation unit.
1461 declare
1462 Lim_Unit_Name : constant Node_Id := Name (Item);
1463 Comp_Unit : Node_Id;
1464 It : Node_Id;
1465 Unit_Name : Node_Id;
1467 begin
1468 Comp_Unit := N;
1469 loop
1470 It := First (Context_Items (Comp_Unit));
1471 while Present (It) loop
1472 if Item /= It
1473 and then Nkind (It) = N_With_Clause
1474 and then not Limited_Present (It)
1475 and then
1476 Nkind_In (Unit (Library_Unit (It)),
1477 N_Package_Declaration,
1478 N_Package_Renaming_Declaration)
1479 then
1480 if Nkind (Unit (Library_Unit (It))) =
1481 N_Package_Declaration
1482 then
1483 Unit_Name := Name (It);
1484 else
1485 Unit_Name := Name (Unit (Library_Unit (It)));
1486 end if;
1488 -- Check if the named package (or some ancestor)
1489 -- leaves visible the full-view of the unit given
1490 -- in the limited-with clause
1492 loop
1493 if Designate_Same_Unit (Lim_Unit_Name,
1494 Unit_Name)
1495 then
1496 Error_Msg_Sloc := Sloc (It);
1497 Error_Msg_N
1498 ("simultaneous visibility of limited "
1499 & "and unlimited views not allowed",
1500 Item);
1501 Error_Msg_NE
1502 ("\unlimited view visible through "
1503 & "context clause #",
1504 Item, It);
1505 exit;
1507 elsif Nkind (Unit_Name) = N_Identifier then
1508 exit;
1509 end if;
1511 Unit_Name := Prefix (Unit_Name);
1512 end loop;
1513 end if;
1515 Next (It);
1516 end loop;
1518 exit when not Is_Child_Spec (Unit (Comp_Unit));
1520 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1521 end loop;
1522 end;
1523 end if;
1525 -- Skip analyzing with clause if no unit, see above
1527 if Present (Library_Unit (Item)) then
1528 Analyze (Item);
1529 end if;
1531 -- A limited_with does not impose an elaboration order, but
1532 -- there is a semantic dependency for recompilation purposes.
1534 if not Implicit_With (Item) then
1535 Version_Update (N, Library_Unit (Item));
1536 end if;
1538 -- Pragmas and use clauses and with clauses other than limited
1539 -- with's are ignored in this pass through the context items.
1541 else
1542 null;
1543 end if;
1545 Next (Item);
1546 end loop;
1547 end Analyze_Context;
1549 -------------------------------
1550 -- Analyze_Package_Body_Stub --
1551 -------------------------------
1553 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1554 Id : constant Entity_Id := Defining_Identifier (N);
1555 Nam : Entity_Id;
1557 begin
1558 -- The package declaration must be in the current declarative part
1560 Check_Stub_Level (N);
1561 Nam := Current_Entity_In_Scope (Id);
1563 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1564 Error_Msg_N ("missing specification for package stub", N);
1566 elsif Has_Completion (Nam)
1567 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1568 then
1569 Error_Msg_N ("duplicate or redundant stub for package", N);
1571 else
1572 -- Indicate that the body of the package exists. If we are doing
1573 -- only semantic analysis, the stub stands for the body. If we are
1574 -- generating code, the existence of the body will be confirmed
1575 -- when we load the proper body.
1577 Set_Has_Completion (Nam);
1578 Set_Scope (Defining_Entity (N), Current_Scope);
1579 Set_Corresponding_Spec_Of_Stub (N, Nam);
1580 Generate_Reference (Nam, Id, 'b');
1581 Analyze_Proper_Body (N, Nam);
1582 end if;
1583 end Analyze_Package_Body_Stub;
1585 -------------------------
1586 -- Analyze_Proper_Body --
1587 -------------------------
1589 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1590 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1591 Unum : Unit_Number_Type;
1593 procedure Optional_Subunit;
1594 -- This procedure is called when the main unit is a stub, or when we
1595 -- are not generating code. In such a case, we analyze the subunit if
1596 -- present, which is user-friendly and in fact required for ASIS, but
1597 -- we don't complain if the subunit is missing.
1599 ----------------------
1600 -- Optional_Subunit --
1601 ----------------------
1603 procedure Optional_Subunit is
1604 Comp_Unit : Node_Id;
1606 begin
1607 -- Try to load subunit, but ignore any errors that occur during the
1608 -- loading of the subunit, by using the special feature in Errout to
1609 -- ignore all errors. Note that Fatal_Error will still be set, so we
1610 -- will be able to check for this case below.
1612 if not ASIS_Mode then
1613 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1614 end if;
1616 Unum :=
1617 Load_Unit
1618 (Load_Name => Subunit_Name,
1619 Required => False,
1620 Subunit => True,
1621 Error_Node => N);
1623 if not ASIS_Mode then
1624 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1625 end if;
1627 -- All done if we successfully loaded the subunit
1629 if Unum /= No_Unit
1630 and then (not Fatal_Error (Unum) or else Try_Semantics)
1631 then
1632 Comp_Unit := Cunit (Unum);
1634 -- If the file was empty or seriously mangled, the unit itself may
1635 -- be missing.
1637 if No (Unit (Comp_Unit)) then
1638 Error_Msg_N
1639 ("subunit does not contain expected proper body", N);
1641 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1642 Error_Msg_N
1643 ("expected SEPARATE subunit, found child unit",
1644 Cunit_Entity (Unum));
1645 else
1646 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1647 Analyze_Subunit (Comp_Unit);
1648 Set_Library_Unit (N, Comp_Unit);
1649 end if;
1651 elsif Unum = No_Unit
1652 and then Present (Nam)
1653 then
1654 if Is_Protected_Type (Nam) then
1655 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1656 else
1657 Set_Corresponding_Body (
1658 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1659 end if;
1660 end if;
1661 end Optional_Subunit;
1663 -- Local variables
1665 Stub_Id : Entity_Id;
1667 -- Start of processing for Analyze_Proper_Body
1669 begin
1670 -- If the subunit is already loaded, it means that the main unit is a
1671 -- subunit, and that the current unit is one of its parents which was
1672 -- being analyzed to provide the needed context for the analysis of the
1673 -- subunit. In this case we analyze the subunit and continue with the
1674 -- parent, without looking at subsequent subunits.
1676 if Is_Loaded (Subunit_Name) then
1678 -- If the proper body is already linked to the stub node, the stub is
1679 -- in a generic unit and just needs analyzing.
1681 if Present (Library_Unit (N)) then
1682 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1684 -- If the subunit has severe errors, the spec of the enclosing
1685 -- body may not be available, in which case do not try analysis.
1687 if Serious_Errors_Detected > 0
1688 and then No (Library_Unit (Library_Unit (N)))
1689 then
1690 return;
1691 end if;
1693 Analyze_Subunit (Library_Unit (N));
1695 -- Otherwise we must load the subunit and link to it
1697 else
1698 -- Load the subunit, this must work, since we originally loaded
1699 -- the subunit earlier on. So this will not really load it, just
1700 -- give access to it.
1702 Unum :=
1703 Load_Unit
1704 (Load_Name => Subunit_Name,
1705 Required => True,
1706 Subunit => False,
1707 Error_Node => N);
1709 -- And analyze the subunit in the parent context (note that we
1710 -- do not call Semantics, since that would remove the parent
1711 -- context). Because of this, we have to manually reset the
1712 -- compiler state to Analyzing since it got destroyed by Load.
1714 if Unum /= No_Unit then
1715 Compiler_State := Analyzing;
1717 -- Check that the proper body is a subunit and not a child
1718 -- unit. If the unit was previously loaded, the error will
1719 -- have been emitted when copying the generic node, so we
1720 -- just return to avoid cascaded errors.
1722 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1723 return;
1724 end if;
1726 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1727 Analyze_Subunit (Cunit (Unum));
1728 Set_Library_Unit (N, Cunit (Unum));
1729 end if;
1730 end if;
1732 -- If the main unit is a subunit, then we are just performing semantic
1733 -- analysis on that subunit, and any other subunits of any parent unit
1734 -- should be ignored, except that if we are building trees for ASIS
1735 -- usage we want to annotate the stub properly.
1737 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1738 and then Subunit_Name /= Unit_Name (Main_Unit)
1739 then
1740 if ASIS_Mode then
1741 Optional_Subunit;
1742 end if;
1744 -- But before we return, set the flag for unloaded subunits. This
1745 -- will suppress junk warnings of variables in the same declarative
1746 -- part (or a higher level one) that are in danger of looking unused
1747 -- when in fact there might be a declaration in the subunit that we
1748 -- do not intend to load.
1750 Unloaded_Subunits := True;
1751 return;
1753 -- If the subunit is not already loaded, and we are generating code,
1754 -- then this is the case where compilation started from the parent, and
1755 -- we are generating code for an entire subunit tree. In that case we
1756 -- definitely need to load the subunit.
1758 -- In order to continue the analysis with the rest of the parent,
1759 -- and other subunits, we load the unit without requiring its
1760 -- presence, and emit a warning if not found, rather than terminating
1761 -- the compilation abruptly, as for other missing file problems.
1763 elsif Original_Operating_Mode = Generate_Code then
1765 -- If the proper body is already linked to the stub node, the stub is
1766 -- in a generic unit and just needs analyzing.
1768 -- We update the version. Although we are not strictly technically
1769 -- semantically dependent on the subunit, given our approach of macro
1770 -- substitution of subunits, it makes sense to include it in the
1771 -- version identification.
1773 if Present (Library_Unit (N)) then
1774 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1775 Analyze_Subunit (Library_Unit (N));
1776 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1778 -- Otherwise we must load the subunit and link to it
1780 else
1781 -- Make sure that, if the subunit is preprocessed and -gnateG is
1782 -- specified, the preprocessed file will be written.
1784 Lib.Analysing_Subunit_Of_Main := True;
1785 Unum :=
1786 Load_Unit
1787 (Load_Name => Subunit_Name,
1788 Required => False,
1789 Subunit => True,
1790 Error_Node => N);
1791 Lib.Analysing_Subunit_Of_Main := False;
1793 -- Give message if we did not get the unit Emit warning even if
1794 -- missing subunit is not within main unit, to simplify debugging.
1796 if Original_Operating_Mode = Generate_Code
1797 and then Unum = No_Unit
1798 then
1799 Error_Msg_Unit_1 := Subunit_Name;
1800 Error_Msg_File_1 :=
1801 Get_File_Name (Subunit_Name, Subunit => True);
1802 Error_Msg_N
1803 ("subunit$$ in file{ not found??!!", N);
1804 Subunits_Missing := True;
1805 end if;
1807 -- Load_Unit may reset Compiler_State, since it may have been
1808 -- necessary to parse an additional units, so we make sure that
1809 -- we reset it to the Analyzing state.
1811 Compiler_State := Analyzing;
1813 if Unum /= No_Unit then
1814 if Debug_Flag_L then
1815 Write_Str ("*** Loaded subunit from stub. Analyze");
1816 Write_Eol;
1817 end if;
1819 declare
1820 Comp_Unit : constant Node_Id := Cunit (Unum);
1821 Prop_Body : Node_Id;
1823 begin
1824 -- Check for child unit instead of subunit
1826 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1827 Error_Msg_N
1828 ("expected SEPARATE subunit, found child unit",
1829 Cunit_Entity (Unum));
1831 -- OK, we have a subunit
1833 else
1834 Prop_Body := Proper_Body (Unit (Comp_Unit));
1836 -- Set corresponding stub (even if errors)
1838 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1840 -- Collect SCO information for loaded subunit if we are
1841 -- in the main unit.
1843 if Generate_SCO
1844 and then
1845 In_Extended_Main_Source_Unit
1846 (Cunit_Entity (Current_Sem_Unit))
1847 then
1848 SCO_Record (Unum);
1849 end if;
1851 -- Propagate all aspect specifications associated with
1852 -- the stub to the proper body.
1854 Move_Or_Merge_Aspects (From => N, To => Prop_Body);
1856 -- Move all source pragmas that follow the body stub and
1857 -- apply to it to the declarations of the proper body.
1859 if Nkind (N) = N_Subprogram_Body_Stub then
1860 Relocate_Pragmas_To_Body (N, Target_Body => Prop_Body);
1861 end if;
1863 -- Analyze the unit if semantics active
1865 if not Fatal_Error (Unum) or else Try_Semantics then
1866 Analyze_Subunit (Comp_Unit);
1867 end if;
1869 -- Set the library unit pointer in any case
1871 Set_Library_Unit (N, Comp_Unit);
1873 -- We update the version. Although we are not technically
1874 -- semantically dependent on the subunit, given our
1875 -- approach of macro substitution of subunits, it makes
1876 -- sense to include it in the version identification.
1878 Version_Update (Cunit (Main_Unit), Comp_Unit);
1879 end if;
1880 end;
1882 -- The unit which should contain the proper subprogram body does
1883 -- not exist. Analyze the aspect specifications of the stub (if
1884 -- any).
1886 elsif Nkind (N) = N_Subprogram_Body_Stub
1887 and then Has_Aspects (N)
1888 then
1889 Stub_Id := Defining_Unit_Name (Specification (N));
1891 -- Restore the proper visibility of the stub and its formals
1893 Push_Scope (Stub_Id);
1894 Install_Formals (Stub_Id);
1896 Analyze_Aspect_Specifications (N, Stub_Id);
1898 Pop_Scope;
1899 end if;
1900 end if;
1902 -- The remaining case is when the subunit is not already loaded and we
1903 -- are not generating code. In this case we are just performing semantic
1904 -- analysis on the parent, and we are not interested in the subunit. For
1905 -- subprograms, analyze the stub as a body. For other entities the stub
1906 -- has already been marked as completed.
1908 else
1909 Optional_Subunit;
1910 end if;
1911 end Analyze_Proper_Body;
1913 ----------------------------------
1914 -- Analyze_Protected_Body_Stub --
1915 ----------------------------------
1917 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1918 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1920 begin
1921 Check_Stub_Level (N);
1923 -- First occurrence of name may have been as an incomplete type
1925 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1926 Nam := Full_View (Nam);
1927 end if;
1929 if No (Nam) or else not Is_Protected_Type (Etype (Nam)) then
1930 Error_Msg_N ("missing specification for Protected body", N);
1932 else
1933 Set_Scope (Defining_Entity (N), Current_Scope);
1934 Set_Has_Completion (Etype (Nam));
1935 Set_Corresponding_Spec_Of_Stub (N, Nam);
1936 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1937 Analyze_Proper_Body (N, Etype (Nam));
1938 end if;
1939 end Analyze_Protected_Body_Stub;
1941 ----------------------------------
1942 -- Analyze_Subprogram_Body_Stub --
1943 ----------------------------------
1945 -- A subprogram body stub can appear with or without a previous spec. If
1946 -- there is one, then the analysis of the body will find it and verify
1947 -- conformance. The formals appearing in the specification of the stub play
1948 -- no role, except for requiring an additional conformance check. If there
1949 -- is no previous subprogram declaration, the stub acts as a spec, and
1950 -- provides the defining entity for the subprogram.
1952 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1953 Decl : Node_Id;
1955 begin
1956 Check_Stub_Level (N);
1958 -- Verify that the identifier for the stub is unique within this
1959 -- declarative part.
1961 if Nkind_In (Parent (N), N_Block_Statement,
1962 N_Package_Body,
1963 N_Subprogram_Body)
1964 then
1965 Decl := First (Declarations (Parent (N)));
1966 while Present (Decl) and then Decl /= N loop
1967 if Nkind (Decl) = N_Subprogram_Body_Stub
1968 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1969 Chars (Defining_Unit_Name (Specification (N))))
1970 then
1971 Error_Msg_N ("identifier for stub is not unique", N);
1972 end if;
1974 Next (Decl);
1975 end loop;
1976 end if;
1978 -- Treat stub as a body, which checks conformance if there is a previous
1979 -- declaration, or else introduces entity and its signature.
1981 Analyze_Subprogram_Body (N);
1982 Analyze_Proper_Body (N, Empty);
1983 end Analyze_Subprogram_Body_Stub;
1985 ---------------------
1986 -- Analyze_Subunit --
1987 ---------------------
1989 -- A subunit is compiled either by itself (for semantic checking) or as
1990 -- part of compiling the parent (for code generation). In either case, by
1991 -- the time we actually process the subunit, the parent has already been
1992 -- installed and analyzed. The node N is a compilation unit, whose context
1993 -- needs to be treated here, because we come directly here from the parent
1994 -- without calling Analyze_Compilation_Unit.
1996 -- The compilation context includes the explicit context of the subunit,
1997 -- and the context of the parent, together with the parent itself. In order
1998 -- to compile the current context, we remove the one inherited from the
1999 -- parent, in order to have a clean visibility table. We restore the parent
2000 -- context before analyzing the proper body itself. On exit, we remove only
2001 -- the explicit context of the subunit.
2003 procedure Analyze_Subunit (N : Node_Id) is
2004 Lib_Unit : constant Node_Id := Library_Unit (N);
2005 Par_Unit : constant Entity_Id := Current_Scope;
2007 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
2008 Num_Scopes : Int := 0;
2009 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2010 Enclosing_Child : Entity_Id := Empty;
2011 Svg : constant Suppress_Record := Scope_Suppress;
2013 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
2014 Cunit_Boolean_Restrictions_Save;
2015 -- Save non-partition wide restrictions before processing the subunit.
2016 -- All subunits are analyzed with config restrictions reset and we need
2017 -- to restore these saved values at the end.
2019 procedure Analyze_Subunit_Context;
2020 -- Capture names in use clauses of the subunit. This must be done before
2021 -- re-installing parent declarations, because items in the context must
2022 -- not be hidden by declarations local to the parent.
2024 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
2025 -- Recursive procedure to restore scope of all ancestors of subunit,
2026 -- from outermost in. If parent is not a subunit, the call to install
2027 -- context installs context of spec and (if parent is a child unit) the
2028 -- context of its parents as well. It is confusing that parents should
2029 -- be treated differently in both cases, but the semantics are just not
2030 -- identical.
2032 procedure Re_Install_Use_Clauses;
2033 -- As part of the removal of the parent scope, the use clauses are
2034 -- removed, to be reinstalled when the context of the subunit has been
2035 -- analyzed. Use clauses may also have been affected by the analysis of
2036 -- the context of the subunit, so they have to be applied again, to
2037 -- insure that the compilation environment of the rest of the parent
2038 -- unit is identical.
2040 procedure Remove_Scope;
2041 -- Remove current scope from scope stack, and preserve the list of use
2042 -- clauses in it, to be reinstalled after context is analyzed.
2044 -----------------------------
2045 -- Analyze_Subunit_Context --
2046 -----------------------------
2048 procedure Analyze_Subunit_Context is
2049 Item : Node_Id;
2050 Nam : Node_Id;
2051 Unit_Name : Entity_Id;
2053 begin
2054 Analyze_Context (N);
2056 -- Make withed units immediately visible. If child unit, make the
2057 -- ultimate parent immediately visible.
2059 Item := First (Context_Items (N));
2060 while Present (Item) loop
2061 if Nkind (Item) = N_With_Clause then
2063 -- Protect frontend against previous errors in context clauses
2065 if Nkind (Name (Item)) /= N_Selected_Component then
2066 if Error_Posted (Item) then
2067 null;
2069 else
2070 -- If a subunits has serious syntax errors, the context
2071 -- may not have been loaded. Add a harmless unit name to
2072 -- attempt processing.
2074 if Serious_Errors_Detected > 0
2075 and then No (Entity (Name (Item)))
2076 then
2077 Set_Entity (Name (Item), Standard_Standard);
2078 end if;
2080 Unit_Name := Entity (Name (Item));
2081 loop
2082 Set_Is_Visible_Lib_Unit (Unit_Name);
2083 exit when Scope (Unit_Name) = Standard_Standard;
2084 Unit_Name := Scope (Unit_Name);
2086 if No (Unit_Name) then
2087 Check_Error_Detected;
2088 return;
2089 end if;
2090 end loop;
2092 if not Is_Immediately_Visible (Unit_Name) then
2093 Set_Is_Immediately_Visible (Unit_Name);
2094 Set_Context_Installed (Item);
2095 end if;
2096 end if;
2097 end if;
2099 elsif Nkind (Item) = N_Use_Package_Clause then
2100 Nam := First (Names (Item));
2101 while Present (Nam) loop
2102 Analyze (Nam);
2103 Next (Nam);
2104 end loop;
2106 elsif Nkind (Item) = N_Use_Type_Clause then
2107 Nam := First (Subtype_Marks (Item));
2108 while Present (Nam) loop
2109 Analyze (Nam);
2110 Next (Nam);
2111 end loop;
2112 end if;
2114 Next (Item);
2115 end loop;
2117 -- Reset visibility of withed units. They will be made visible again
2118 -- when we install the subunit context.
2120 Item := First (Context_Items (N));
2121 while Present (Item) loop
2122 if Nkind (Item) = N_With_Clause
2124 -- Protect frontend against previous errors in context clauses
2126 and then Nkind (Name (Item)) /= N_Selected_Component
2127 and then not Error_Posted (Item)
2128 then
2129 Unit_Name := Entity (Name (Item));
2130 loop
2131 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2132 exit when Scope (Unit_Name) = Standard_Standard;
2133 Unit_Name := Scope (Unit_Name);
2134 end loop;
2136 if Context_Installed (Item) then
2137 Set_Is_Immediately_Visible (Unit_Name, False);
2138 Set_Context_Installed (Item, False);
2139 end if;
2140 end if;
2142 Next (Item);
2143 end loop;
2144 end Analyze_Subunit_Context;
2146 ------------------------
2147 -- Re_Install_Parents --
2148 ------------------------
2150 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2151 E : Entity_Id;
2153 begin
2154 if Nkind (Unit (L)) = N_Subunit then
2155 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2156 end if;
2158 Install_Context (L);
2160 -- If the subunit occurs within a child unit, we must restore the
2161 -- immediate visibility of any siblings that may occur in context.
2163 if Present (Enclosing_Child) then
2164 Install_Siblings (Enclosing_Child, L);
2165 end if;
2167 Push_Scope (Scop);
2169 if Scop /= Par_Unit then
2170 Set_Is_Immediately_Visible (Scop);
2171 end if;
2173 -- Make entities in scope visible again. For child units, restore
2174 -- visibility only if they are actually in context.
2176 E := First_Entity (Current_Scope);
2177 while Present (E) loop
2178 if not Is_Child_Unit (E) or else Is_Visible_Lib_Unit (E) then
2179 Set_Is_Immediately_Visible (E);
2180 end if;
2182 Next_Entity (E);
2183 end loop;
2185 -- A subunit appears within a body, and for a nested subunits all the
2186 -- parents are bodies. Restore full visibility of their private
2187 -- entities.
2189 if Is_Package_Or_Generic_Package (Scop) then
2190 Set_In_Package_Body (Scop);
2191 Install_Private_Declarations (Scop);
2192 end if;
2193 end Re_Install_Parents;
2195 ----------------------------
2196 -- Re_Install_Use_Clauses --
2197 ----------------------------
2199 procedure Re_Install_Use_Clauses is
2200 U : Node_Id;
2201 begin
2202 for J in reverse 1 .. Num_Scopes loop
2203 U := Use_Clauses (J);
2204 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2205 Install_Use_Clauses (U, Force_Installation => True);
2206 end loop;
2207 end Re_Install_Use_Clauses;
2209 ------------------
2210 -- Remove_Scope --
2211 ------------------
2213 procedure Remove_Scope is
2214 E : Entity_Id;
2216 begin
2217 Num_Scopes := Num_Scopes + 1;
2218 Use_Clauses (Num_Scopes) :=
2219 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2221 E := First_Entity (Current_Scope);
2222 while Present (E) loop
2223 Set_Is_Immediately_Visible (E, False);
2224 Next_Entity (E);
2225 end loop;
2227 if Is_Child_Unit (Current_Scope) then
2228 Enclosing_Child := Current_Scope;
2229 end if;
2231 Pop_Scope;
2232 end Remove_Scope;
2234 -- Start of processing for Analyze_Subunit
2236 begin
2237 -- For subunit in main extended unit, we reset the configuration values
2238 -- for the non-partition-wide restrictions. For other units reset them.
2240 if In_Extended_Main_Source_Unit (N) then
2241 Restore_Config_Cunit_Boolean_Restrictions;
2242 else
2243 Reset_Cunit_Boolean_Restrictions;
2244 end if;
2246 if Style_Check then
2247 declare
2248 Nam : Node_Id := Name (Unit (N));
2250 begin
2251 if Nkind (Nam) = N_Selected_Component then
2252 Nam := Selector_Name (Nam);
2253 end if;
2255 Check_Identifier (Nam, Par_Unit);
2256 end;
2257 end if;
2259 if not Is_Empty_List (Context_Items (N)) then
2261 -- Save current use clauses
2263 Remove_Scope;
2264 Remove_Context (Lib_Unit);
2266 -- Now remove parents and their context, including enclosing subunits
2267 -- and the outer parent body which is not a subunit.
2269 if Present (Lib_Spec) then
2270 Remove_Context (Lib_Spec);
2272 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2273 Lib_Spec := Library_Unit (Lib_Spec);
2274 Remove_Scope;
2275 Remove_Context (Lib_Spec);
2276 end loop;
2278 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2279 Remove_Scope;
2280 end if;
2282 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2283 Remove_Context (Library_Unit (Lib_Spec));
2284 end if;
2285 end if;
2287 Set_Is_Immediately_Visible (Par_Unit, False);
2289 Analyze_Subunit_Context;
2291 Re_Install_Parents (Lib_Unit, Par_Unit);
2292 Set_Is_Immediately_Visible (Par_Unit);
2294 -- If the context includes a child unit of the parent of the subunit,
2295 -- the parent will have been removed from visibility, after compiling
2296 -- that cousin in the context. The visibility of the parent must be
2297 -- restored now. This also applies if the context includes another
2298 -- subunit of the same parent which in turn includes a child unit in
2299 -- its context.
2301 if Is_Package_Or_Generic_Package (Par_Unit) then
2302 if not Is_Immediately_Visible (Par_Unit)
2303 or else (Present (First_Entity (Par_Unit))
2304 and then not
2305 Is_Immediately_Visible (First_Entity (Par_Unit)))
2306 then
2307 Set_Is_Immediately_Visible (Par_Unit);
2308 Install_Visible_Declarations (Par_Unit);
2309 Install_Private_Declarations (Par_Unit);
2310 end if;
2311 end if;
2313 Re_Install_Use_Clauses;
2314 Install_Context (N);
2316 -- Restore state of suppress flags for current body
2318 Scope_Suppress := Svg;
2320 -- If the subunit is within a child unit, then siblings of any parent
2321 -- unit that appear in the context clause of the subunit must also be
2322 -- made immediately visible.
2324 if Present (Enclosing_Child) then
2325 Install_Siblings (Enclosing_Child, N);
2326 end if;
2327 end if;
2329 Analyze (Proper_Body (Unit (N)));
2330 Remove_Context (N);
2332 -- The subunit may contain a with_clause on a sibling of some ancestor.
2333 -- Removing the context will remove from visibility those ancestor child
2334 -- units, which must be restored to the visibility they have in the
2335 -- enclosing body.
2337 if Present (Enclosing_Child) then
2338 declare
2339 C : Entity_Id;
2340 begin
2341 C := Current_Scope;
2342 while Present (C) and then C /= Standard_Standard loop
2343 Set_Is_Immediately_Visible (C);
2344 Set_Is_Visible_Lib_Unit (C);
2345 C := Scope (C);
2346 end loop;
2347 end;
2348 end if;
2350 -- Deal with restore of restrictions
2352 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2353 end Analyze_Subunit;
2355 ----------------------------
2356 -- Analyze_Task_Body_Stub --
2357 ----------------------------
2359 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2360 Loc : constant Source_Ptr := Sloc (N);
2361 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2363 begin
2364 Check_Stub_Level (N);
2366 -- First occurrence of name may have been as an incomplete type
2368 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2369 Nam := Full_View (Nam);
2370 end if;
2372 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2373 Error_Msg_N ("missing specification for task body", N);
2374 else
2375 Set_Scope (Defining_Entity (N), Current_Scope);
2376 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2377 Set_Corresponding_Spec_Of_Stub (N, Nam);
2379 -- Check for duplicate stub, if so give message and terminate
2381 if Has_Completion (Etype (Nam)) then
2382 Error_Msg_N ("duplicate stub for task", N);
2383 return;
2384 else
2385 Set_Has_Completion (Etype (Nam));
2386 end if;
2388 Analyze_Proper_Body (N, Etype (Nam));
2390 -- Set elaboration flag to indicate that entity is callable. This
2391 -- cannot be done in the expansion of the body itself, because the
2392 -- proper body is not in a declarative part. This is only done if
2393 -- expansion is active, because the context may be generic and the
2394 -- flag not defined yet.
2396 if Full_Expander_Active then
2397 Insert_After (N,
2398 Make_Assignment_Statement (Loc,
2399 Name =>
2400 Make_Identifier (Loc,
2401 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2402 Expression => New_Reference_To (Standard_True, Loc)));
2403 end if;
2404 end if;
2405 end Analyze_Task_Body_Stub;
2407 -------------------------
2408 -- Analyze_With_Clause --
2409 -------------------------
2411 -- Analyze the declaration of a unit in a with clause. At end, label the
2412 -- with clause with the defining entity for the unit.
2414 procedure Analyze_With_Clause (N : Node_Id) is
2416 -- Retrieve the original kind of the unit node, before analysis. If it
2417 -- is a subprogram instantiation, its analysis below will rewrite the
2418 -- node as the declaration of the wrapper package. If the same
2419 -- instantiation appears indirectly elsewhere in the context, it will
2420 -- have been analyzed already.
2422 Unit_Kind : constant Node_Kind :=
2423 Nkind (Original_Node (Unit (Library_Unit (N))));
2424 Nam : constant Node_Id := Name (N);
2425 E_Name : Entity_Id;
2426 Par_Name : Entity_Id;
2427 Pref : Node_Id;
2428 U : Node_Id;
2430 Intunit : Boolean;
2431 -- Set True if the unit currently being compiled is an internal unit
2433 Restriction_Violation : Boolean := False;
2434 -- Set True if a with violates a restriction, no point in giving any
2435 -- warnings if we have this definite error.
2437 Save_Style_Check : constant Boolean := Opt.Style_Check;
2439 begin
2440 U := Unit (Library_Unit (N));
2442 -- If this is an internal unit which is a renaming, then this is a
2443 -- violation of No_Obsolescent_Features.
2445 -- Note: this is not quite right if the user defines one of these units
2446 -- himself, but that's a marginal case, and fixing it is hard ???
2448 if Restriction_Check_Required (No_Obsolescent_Features) then
2449 declare
2450 F : constant File_Name_Type :=
2451 Unit_File_Name (Get_Source_Unit (U));
2452 begin
2453 if Is_Predefined_File_Name (F, Renamings_Included => True)
2454 and then not
2455 Is_Predefined_File_Name (F, Renamings_Included => False)
2456 then
2457 Check_Restriction (No_Obsolescent_Features, N);
2458 Restriction_Violation := True;
2459 end if;
2460 end;
2461 end if;
2463 -- Check No_Implementation_Units violation
2465 if Restriction_Check_Required (No_Implementation_Units) then
2466 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2467 null;
2468 else
2469 Check_Restriction (No_Implementation_Units, Nam);
2470 Restriction_Violation := True;
2471 end if;
2472 end if;
2474 -- Several actions are skipped for dummy packages (those supplied for
2475 -- with's where no matching file could be found). Such packages are
2476 -- identified by the Sloc value being set to No_Location.
2478 if Limited_Present (N) then
2480 -- Ada 2005 (AI-50217): Build visibility structures but do not
2481 -- analyze the unit.
2483 if Sloc (U) /= No_Location then
2484 Build_Limited_Views (N);
2485 end if;
2487 return;
2488 end if;
2490 -- If the library unit is a predefined unit, and we are in high
2491 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2492 -- for the analysis of the with'ed unit. This mode does not prevent
2493 -- explicit with'ing of run-time units.
2495 if Configurable_Run_Time_Mode
2496 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (U)))
2497 then
2498 Configurable_Run_Time_Mode := False;
2499 Semantics (Library_Unit (N));
2500 Configurable_Run_Time_Mode := True;
2502 else
2503 Semantics (Library_Unit (N));
2504 end if;
2506 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2508 if Sloc (U) /= No_Location then
2510 -- Check restrictions, except that we skip the check if this is an
2511 -- internal unit unless we are compiling the internal unit as the
2512 -- main unit. We also skip this for dummy packages.
2514 Check_Restriction_No_Dependence (Nam, N);
2516 if not Intunit or else Current_Sem_Unit = Main_Unit then
2517 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2518 end if;
2520 -- Deal with special case of GNAT.Current_Exceptions which interacts
2521 -- with the optimization of local raise statements into gotos.
2523 if Nkind (Nam) = N_Selected_Component
2524 and then Nkind (Prefix (Nam)) = N_Identifier
2525 and then Chars (Prefix (Nam)) = Name_Gnat
2526 and then Nam_In (Chars (Selector_Name (Nam)),
2527 Name_Most_Recent_Exception,
2528 Name_Exception_Traces)
2529 then
2530 Check_Restriction (No_Exception_Propagation, N);
2531 Special_Exception_Package_Used := True;
2532 end if;
2534 -- Check for inappropriate with of internal implementation unit if we
2535 -- are not compiling an internal unit and also check for withing unit
2536 -- in wrong version of Ada. Do not issue these messages for implicit
2537 -- with's generated by the compiler itself.
2539 if Implementation_Unit_Warnings
2540 and then not Intunit
2541 and then not Implicit_With (N)
2542 and then not Restriction_Violation
2543 then
2544 declare
2545 U_Kind : constant Kind_Of_Unit :=
2546 Get_Kind_Of_Unit (Get_Source_Unit (U));
2548 begin
2549 if U_Kind = Implementation_Unit then
2550 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2552 -- Add alternative name if available, otherwise issue a
2553 -- general warning message.
2555 if Error_Msg_Strlen /= 0 then
2556 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2557 else
2558 Error_Msg_F
2559 ("\use of this unit is non-portable " &
2560 "and version-dependent?i?", Name (N));
2561 end if;
2563 elsif U_Kind = Ada_2005_Unit
2564 and then Ada_Version < Ada_2005
2565 and then Warn_On_Ada_2005_Compatibility
2566 then
2567 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2569 elsif U_Kind = Ada_2012_Unit
2570 and then Ada_Version < Ada_2012
2571 and then Warn_On_Ada_2012_Compatibility
2572 then
2573 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2574 end if;
2575 end;
2576 end if;
2577 end if;
2579 -- Semantic analysis of a generic unit is performed on a copy of
2580 -- the original tree. Retrieve the entity on which semantic info
2581 -- actually appears.
2583 if Unit_Kind in N_Generic_Declaration then
2584 E_Name := Defining_Entity (U);
2586 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2587 -- the case of an instantiation, semantic analysis above will have
2588 -- replaced the unit by its instantiated version. If the instance body
2589 -- has been generated, the instance now denotes the body entity. For
2590 -- visibility purposes we need the entity of its spec.
2592 elsif (Unit_Kind = N_Package_Instantiation
2593 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2594 N_Package_Instantiation)
2595 and then Nkind (U) = N_Package_Body
2596 then
2597 E_Name := Corresponding_Spec (U);
2599 elsif Unit_Kind = N_Package_Instantiation
2600 and then Nkind (U) = N_Package_Instantiation
2601 and then Present (Instance_Spec (U))
2602 then
2603 -- If the instance has not been rewritten as a package declaration,
2604 -- then it appeared already in a previous with clause. Retrieve
2605 -- the entity from the previous instance.
2607 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2609 elsif Unit_Kind in N_Subprogram_Instantiation then
2611 -- The visible subprogram is created during instantiation, and is
2612 -- an attribute of the wrapper package. We retrieve the wrapper
2613 -- package directly from the instantiation node. If the instance
2614 -- is inlined the unit is still an instantiation. Otherwise it has
2615 -- been rewritten as the declaration of the wrapper itself.
2617 if Nkind (U) in N_Subprogram_Instantiation then
2618 E_Name :=
2619 Related_Instance
2620 (Defining_Entity (Specification (Instance_Spec (U))));
2621 else
2622 E_Name := Related_Instance (Defining_Entity (U));
2623 end if;
2625 elsif Unit_Kind = N_Package_Renaming_Declaration
2626 or else Unit_Kind in N_Generic_Renaming_Declaration
2627 then
2628 E_Name := Defining_Entity (U);
2630 elsif Unit_Kind = N_Subprogram_Body
2631 and then Nkind (Name (N)) = N_Selected_Component
2632 and then not Acts_As_Spec (Library_Unit (N))
2633 then
2634 -- For a child unit that has no spec, one has been created and
2635 -- analyzed. The entity required is that of the spec.
2637 E_Name := Corresponding_Spec (U);
2639 else
2640 E_Name := Defining_Entity (U);
2641 end if;
2643 if Nkind (Name (N)) = N_Selected_Component then
2645 -- Child unit in a with clause
2647 Change_Selected_Component_To_Expanded_Name (Name (N));
2649 -- If this is a child unit without a spec, and it has been analyzed
2650 -- already, a declaration has been created for it. The with_clause
2651 -- must reflect the actual body, and not the generated declaration,
2652 -- to prevent spurious binding errors involving an out-of-date spec.
2653 -- Note that this can only happen if the unit includes more than one
2654 -- with_clause for the child unit (e.g. in separate subunits).
2656 if Unit_Kind = N_Subprogram_Declaration
2657 and then Analyzed (Library_Unit (N))
2658 and then not Comes_From_Source (Library_Unit (N))
2659 then
2660 Set_Library_Unit (N,
2661 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2662 end if;
2663 end if;
2665 -- Restore style checks
2667 Style_Check := Save_Style_Check;
2669 -- Record the reference, but do NOT set the unit as referenced, we want
2670 -- to consider the unit as unreferenced if this is the only reference
2671 -- that occurs.
2673 Set_Entity_With_Style_Check (Name (N), E_Name);
2674 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2676 -- Generate references and check No_Dependence restriction for parents
2678 if Is_Child_Unit (E_Name) then
2679 Pref := Prefix (Name (N));
2680 Par_Name := Scope (E_Name);
2681 while Nkind (Pref) = N_Selected_Component loop
2682 Change_Selected_Component_To_Expanded_Name (Pref);
2684 if Present (Entity (Selector_Name (Pref)))
2685 and then
2686 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2687 and then Entity (Selector_Name (Pref)) /= Par_Name
2688 then
2689 -- The prefix is a child unit that denotes a renaming declaration.
2690 -- Replace the prefix directly with the renamed unit, because the
2691 -- rest of the prefix is irrelevant to the visibility of the real
2692 -- unit.
2694 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2695 exit;
2696 end if;
2698 Set_Entity_With_Style_Check (Pref, Par_Name);
2700 Generate_Reference (Par_Name, Pref);
2701 Check_Restriction_No_Dependence (Pref, N);
2702 Pref := Prefix (Pref);
2704 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2705 -- is set to Standard_Standard, and no attempt should be made to
2706 -- further unwind scopes.
2708 if Par_Name /= Standard_Standard then
2709 Par_Name := Scope (Par_Name);
2710 end if;
2712 -- Abandon processing in case of previous errors
2714 if No (Par_Name) then
2715 Check_Error_Detected;
2716 return;
2717 end if;
2718 end loop;
2720 if Present (Entity (Pref))
2721 and then not Analyzed (Parent (Parent (Entity (Pref))))
2722 then
2723 -- If the entity is set without its unit being compiled, the
2724 -- original parent is a renaming, and Par_Name is the renamed
2725 -- entity. For visibility purposes, we need the original entity,
2726 -- which must be analyzed now because Load_Unit directly retrieves
2727 -- the renamed unit, and the renaming declaration itself has not
2728 -- been analyzed.
2730 Analyze (Parent (Parent (Entity (Pref))));
2731 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2732 Par_Name := Entity (Pref);
2733 end if;
2735 -- Guard against missing or misspelled child units
2737 if Present (Par_Name) then
2738 Set_Entity_With_Style_Check (Pref, Par_Name);
2739 Generate_Reference (Par_Name, Pref);
2741 else
2742 pragma Assert (Serious_Errors_Detected /= 0);
2744 -- Mark the node to indicate that a related error has been posted.
2745 -- This defends further compilation passes against improper use of
2746 -- the invalid WITH clause node.
2748 Set_Error_Posted (N);
2749 Set_Name (N, Error);
2750 return;
2751 end if;
2752 end if;
2754 -- If the withed unit is System, and a system extension pragma is
2755 -- present, compile the extension now, rather than waiting for a
2756 -- visibility check on a specific entity.
2758 if Chars (E_Name) = Name_System
2759 and then Scope (E_Name) = Standard_Standard
2760 and then Present (System_Extend_Unit)
2761 and then Present_System_Aux (N)
2762 then
2763 -- If the extension is not present, an error will have been emitted
2765 null;
2766 end if;
2768 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2769 -- to private_with units; they will be made visible later (just before
2770 -- the private part is analyzed)
2772 if Private_Present (N) then
2773 Set_Is_Immediately_Visible (E_Name, False);
2774 end if;
2775 end Analyze_With_Clause;
2777 ------------------------------
2778 -- Check_Private_Child_Unit --
2779 ------------------------------
2781 procedure Check_Private_Child_Unit (N : Node_Id) is
2782 Lib_Unit : constant Node_Id := Unit (N);
2783 Item : Node_Id;
2784 Curr_Unit : Entity_Id;
2785 Sub_Parent : Node_Id;
2786 Priv_Child : Entity_Id;
2787 Par_Lib : Entity_Id;
2788 Par_Spec : Node_Id;
2790 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2791 -- Returns true if and only if the library unit is declared with
2792 -- an explicit designation of private.
2794 -----------------------------
2795 -- Is_Private_Library_Unit --
2796 -----------------------------
2798 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2799 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2801 begin
2802 return Private_Present (Comp_Unit);
2803 end Is_Private_Library_Unit;
2805 -- Start of processing for Check_Private_Child_Unit
2807 begin
2808 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2809 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2810 Par_Lib := Curr_Unit;
2812 elsif Nkind (Lib_Unit) = N_Subunit then
2814 -- The parent is itself a body. The parent entity is to be found in
2815 -- the corresponding spec.
2817 Sub_Parent := Library_Unit (N);
2818 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2820 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2821 -- enclosing body, retrieve the spec entity which is the proper
2822 -- ancestor we need for the following tests.
2824 if Ekind (Curr_Unit) = E_Package_Body then
2825 Curr_Unit := Spec_Entity (Curr_Unit);
2826 end if;
2828 Par_Lib := Curr_Unit;
2830 else
2831 Curr_Unit := Defining_Entity (Lib_Unit);
2833 Par_Lib := Curr_Unit;
2834 Par_Spec := Parent_Spec (Lib_Unit);
2836 if No (Par_Spec) then
2837 Par_Lib := Empty;
2838 else
2839 Par_Lib := Defining_Entity (Unit (Par_Spec));
2840 end if;
2841 end if;
2843 -- Loop through context items
2845 Item := First (Context_Items (N));
2846 while Present (Item) loop
2848 -- Ada 2005 (AI-262): Allow private_with of a private child package
2849 -- in public siblings
2851 if Nkind (Item) = N_With_Clause
2852 and then not Implicit_With (Item)
2853 and then not Limited_Present (Item)
2854 and then Is_Private_Descendant (Entity (Name (Item)))
2855 then
2856 Priv_Child := Entity (Name (Item));
2858 declare
2859 Curr_Parent : Entity_Id := Par_Lib;
2860 Child_Parent : Entity_Id := Scope (Priv_Child);
2861 Prv_Ancestor : Entity_Id := Child_Parent;
2862 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2864 begin
2865 -- If the child unit is a public child then locate the nearest
2866 -- private ancestor. Child_Parent will then be set to the
2867 -- parent of that ancestor.
2869 if not Is_Private_Library_Unit (Priv_Child) then
2870 while Present (Prv_Ancestor)
2871 and then not Is_Private_Library_Unit (Prv_Ancestor)
2872 loop
2873 Prv_Ancestor := Scope (Prv_Ancestor);
2874 end loop;
2876 if Present (Prv_Ancestor) then
2877 Child_Parent := Scope (Prv_Ancestor);
2878 end if;
2879 end if;
2881 while Present (Curr_Parent)
2882 and then Curr_Parent /= Standard_Standard
2883 and then Curr_Parent /= Child_Parent
2884 loop
2885 Curr_Private :=
2886 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2887 Curr_Parent := Scope (Curr_Parent);
2888 end loop;
2890 if No (Curr_Parent) then
2891 Curr_Parent := Standard_Standard;
2892 end if;
2894 if Curr_Parent /= Child_Parent then
2895 if Ekind (Priv_Child) = E_Generic_Package
2896 and then Chars (Priv_Child) in Text_IO_Package_Name
2897 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2898 then
2899 Error_Msg_NE
2900 ("& is a nested package, not a compilation unit",
2901 Name (Item), Priv_Child);
2903 else
2904 Error_Msg_N
2905 ("unit in with clause is private child unit!", Item);
2906 Error_Msg_NE
2907 ("\current unit must also have parent&!",
2908 Item, Child_Parent);
2909 end if;
2911 elsif Curr_Private
2912 or else Private_Present (Item)
2913 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2914 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2915 and then not Acts_As_Spec (Parent (Lib_Unit)))
2916 then
2917 null;
2919 else
2920 Error_Msg_NE
2921 ("current unit must also be private descendant of&",
2922 Item, Child_Parent);
2923 end if;
2924 end;
2925 end if;
2927 Next (Item);
2928 end loop;
2930 end Check_Private_Child_Unit;
2932 ----------------------
2933 -- Check_Stub_Level --
2934 ----------------------
2936 procedure Check_Stub_Level (N : Node_Id) is
2937 Par : constant Node_Id := Parent (N);
2938 Kind : constant Node_Kind := Nkind (Par);
2940 begin
2941 if Nkind_In (Kind, N_Package_Body,
2942 N_Subprogram_Body,
2943 N_Task_Body,
2944 N_Protected_Body)
2945 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
2946 then
2947 null;
2949 -- In an instance, a missing stub appears at any level. A warning
2950 -- message will have been emitted already for the missing file.
2952 elsif not In_Instance then
2953 Error_Msg_N ("stub cannot appear in an inner scope", N);
2955 elsif Expander_Active then
2956 Error_Msg_N ("missing proper body", N);
2957 end if;
2958 end Check_Stub_Level;
2960 ------------------------
2961 -- Expand_With_Clause --
2962 ------------------------
2964 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2965 Loc : constant Source_Ptr := Sloc (Nam);
2966 Ent : constant Entity_Id := Entity (Nam);
2967 Withn : Node_Id;
2968 P : Node_Id;
2970 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2971 -- Build name to be used in implicit with_clause. In most cases this
2972 -- is the source name, but if renamings are present we must make the
2973 -- original unit visible, not the one it renames. The entity in the
2974 -- with clause is the renamed unit, but the identifier is the one from
2975 -- the source, which allows us to recover the unit renaming.
2977 ---------------------
2978 -- Build_Unit_Name --
2979 ---------------------
2981 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2982 Ent : Entity_Id;
2983 Result : Node_Id;
2985 begin
2986 if Nkind (Nam) = N_Identifier then
2987 return New_Occurrence_Of (Entity (Nam), Loc);
2989 else
2990 Ent := Entity (Nam);
2992 if Present (Entity (Selector_Name (Nam)))
2993 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
2994 and then
2995 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
2996 = N_Package_Renaming_Declaration
2997 then
2998 -- The name in the with_clause is of the form A.B.C, and B is
2999 -- given by a renaming declaration. In that case we may not
3000 -- have analyzed the unit for B, but replaced it directly in
3001 -- lib-load with the unit it renames. We have to make A.B
3002 -- visible, so analyze the declaration for B now, in case it
3003 -- has not been done yet.
3005 Ent := Entity (Selector_Name (Nam));
3006 Analyze
3007 (Parent
3008 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3009 end if;
3011 Result :=
3012 Make_Expanded_Name (Loc,
3013 Chars => Chars (Entity (Nam)),
3014 Prefix => Build_Unit_Name (Prefix (Nam)),
3015 Selector_Name => New_Occurrence_Of (Ent, Loc));
3016 Set_Entity (Result, Ent);
3017 return Result;
3018 end if;
3019 end Build_Unit_Name;
3021 -- Start of processing for Expand_With_Clause
3023 begin
3024 Withn :=
3025 Make_With_Clause (Loc,
3026 Name => Build_Unit_Name (Nam));
3028 P := Parent (Unit_Declaration_Node (Ent));
3029 Set_Library_Unit (Withn, P);
3030 Set_Corresponding_Spec (Withn, Ent);
3031 Set_First_Name (Withn, True);
3032 Set_Implicit_With (Withn, True);
3034 -- If the unit is a package or generic package declaration, a private_
3035 -- with_clause on a child unit implies that the implicit with on the
3036 -- parent is also private.
3038 if Nkind_In (Unit (N), N_Package_Declaration,
3039 N_Generic_Package_Declaration)
3040 then
3041 Set_Private_Present (Withn, Private_Present (Item));
3042 end if;
3044 Prepend (Withn, Context_Items (N));
3045 Mark_Rewrite_Insertion (Withn);
3046 Install_Withed_Unit (Withn);
3048 if Nkind (Nam) = N_Expanded_Name then
3049 Expand_With_Clause (Item, Prefix (Nam), N);
3050 end if;
3051 end Expand_With_Clause;
3053 -----------------------
3054 -- Get_Parent_Entity --
3055 -----------------------
3057 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
3058 begin
3059 if Nkind (Unit) = N_Package_Body
3060 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
3061 then
3062 return Defining_Entity
3063 (Specification (Instance_Spec (Original_Node (Unit))));
3064 elsif Nkind (Unit) = N_Package_Instantiation then
3065 return Defining_Entity (Specification (Instance_Spec (Unit)));
3066 else
3067 return Defining_Entity (Unit);
3068 end if;
3069 end Get_Parent_Entity;
3071 ---------------------
3072 -- Has_With_Clause --
3073 ---------------------
3075 function Has_With_Clause
3076 (C_Unit : Node_Id;
3077 Pack : Entity_Id;
3078 Is_Limited : Boolean := False) return Boolean
3080 Item : Node_Id;
3082 function Named_Unit (Clause : Node_Id) return Entity_Id;
3083 -- Return the entity for the unit named in a [limited] with clause
3085 ----------------
3086 -- Named_Unit --
3087 ----------------
3089 function Named_Unit (Clause : Node_Id) return Entity_Id is
3090 begin
3091 if Nkind (Name (Clause)) = N_Selected_Component then
3092 return Entity (Selector_Name (Name (Clause)));
3093 else
3094 return Entity (Name (Clause));
3095 end if;
3096 end Named_Unit;
3098 -- Start of processing for Has_With_Clause
3100 begin
3101 if Present (Context_Items (C_Unit)) then
3102 Item := First (Context_Items (C_Unit));
3103 while Present (Item) loop
3104 if Nkind (Item) = N_With_Clause
3105 and then Limited_Present (Item) = Is_Limited
3106 and then Named_Unit (Item) = Pack
3107 then
3108 return True;
3109 end if;
3111 Next (Item);
3112 end loop;
3113 end if;
3115 return False;
3116 end Has_With_Clause;
3118 -----------------------------
3119 -- Implicit_With_On_Parent --
3120 -----------------------------
3122 procedure Implicit_With_On_Parent
3123 (Child_Unit : Node_Id;
3124 N : Node_Id)
3126 Loc : constant Source_Ptr := Sloc (N);
3127 P : constant Node_Id := Parent_Spec (Child_Unit);
3128 P_Unit : Node_Id := Unit (P);
3129 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3130 Withn : Node_Id;
3132 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3133 -- Build prefix of child unit name. Recurse if needed
3135 function Build_Unit_Name return Node_Id;
3136 -- If the unit is a child unit, build qualified name with all ancestors
3138 -------------------------
3139 -- Build_Ancestor_Name --
3140 -------------------------
3142 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3143 P_Ref : constant Node_Id :=
3144 New_Reference_To (Defining_Entity (P), Loc);
3145 P_Spec : Node_Id := P;
3147 begin
3148 -- Ancestor may have been rewritten as a package body. Retrieve
3149 -- the original spec to trace earlier ancestors.
3151 if Nkind (P) = N_Package_Body
3152 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3153 then
3154 P_Spec := Original_Node (P);
3155 end if;
3157 if No (Parent_Spec (P_Spec)) then
3158 return P_Ref;
3159 else
3160 return
3161 Make_Selected_Component (Loc,
3162 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3163 Selector_Name => P_Ref);
3164 end if;
3165 end Build_Ancestor_Name;
3167 ---------------------
3168 -- Build_Unit_Name --
3169 ---------------------
3171 function Build_Unit_Name return Node_Id is
3172 Result : Node_Id;
3174 begin
3175 if No (Parent_Spec (P_Unit)) then
3176 return New_Reference_To (P_Name, Loc);
3178 else
3179 Result :=
3180 Make_Expanded_Name (Loc,
3181 Chars => Chars (P_Name),
3182 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3183 Selector_Name => New_Reference_To (P_Name, Loc));
3184 Set_Entity (Result, P_Name);
3185 return Result;
3186 end if;
3187 end Build_Unit_Name;
3189 -- Start of processing for Implicit_With_On_Parent
3191 begin
3192 -- The unit of the current compilation may be a package body that
3193 -- replaces an instance node. In this case we need the original instance
3194 -- node to construct the proper parent name.
3196 if Nkind (P_Unit) = N_Package_Body
3197 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3198 then
3199 P_Unit := Original_Node (P_Unit);
3200 end if;
3202 -- We add the implicit with if the child unit is the current unit being
3203 -- compiled. If the current unit is a body, we do not want to add an
3204 -- implicit_with a second time to the corresponding spec.
3206 if Nkind (Child_Unit) = N_Package_Declaration
3207 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3208 then
3209 return;
3210 end if;
3212 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3214 Set_Library_Unit (Withn, P);
3215 Set_Corresponding_Spec (Withn, P_Name);
3216 Set_First_Name (Withn, True);
3217 Set_Implicit_With (Withn, True);
3219 -- Node is placed at the beginning of the context items, so that
3220 -- subsequent use clauses on the parent can be validated.
3222 Prepend (Withn, Context_Items (N));
3223 Mark_Rewrite_Insertion (Withn);
3224 Install_Withed_Unit (Withn);
3226 if Is_Child_Spec (P_Unit) then
3227 Implicit_With_On_Parent (P_Unit, N);
3228 end if;
3229 end Implicit_With_On_Parent;
3231 --------------
3232 -- In_Chain --
3233 --------------
3235 function In_Chain (E : Entity_Id) return Boolean is
3236 H : Entity_Id;
3238 begin
3239 H := Current_Entity (E);
3240 while Present (H) loop
3241 if H = E then
3242 return True;
3243 else
3244 H := Homonym (H);
3245 end if;
3246 end loop;
3248 return False;
3249 end In_Chain;
3251 ---------------------
3252 -- Install_Context --
3253 ---------------------
3255 procedure Install_Context (N : Node_Id) is
3256 Lib_Unit : constant Node_Id := Unit (N);
3258 begin
3259 Install_Context_Clauses (N);
3261 if Is_Child_Spec (Lib_Unit) then
3262 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3263 end if;
3265 Install_Limited_Context_Clauses (N);
3266 end Install_Context;
3268 -----------------------------
3269 -- Install_Context_Clauses --
3270 -----------------------------
3272 procedure Install_Context_Clauses (N : Node_Id) is
3273 Lib_Unit : constant Node_Id := Unit (N);
3274 Item : Node_Id;
3275 Uname_Node : Entity_Id;
3276 Check_Private : Boolean := False;
3277 Decl_Node : Node_Id;
3278 Lib_Parent : Entity_Id;
3280 begin
3281 -- First skip configuration pragmas at the start of the context. They
3282 -- are not technically part of the context clause, but that's where the
3283 -- parser puts them. Note they were analyzed in Analyze_Context.
3285 Item := First (Context_Items (N));
3286 while Present (Item)
3287 and then Nkind (Item) = N_Pragma
3288 and then Pragma_Name (Item) in Configuration_Pragma_Names
3289 loop
3290 Next (Item);
3291 end loop;
3293 -- Loop through the actual context clause items. We process everything
3294 -- except Limited_With clauses in this routine. Limited_With clauses
3295 -- are separately installed (see Install_Limited_Context_Clauses).
3297 while Present (Item) loop
3299 -- Case of explicit WITH clause
3301 if Nkind (Item) = N_With_Clause
3302 and then not Implicit_With (Item)
3303 then
3304 if Limited_Present (Item) then
3306 -- Limited withed units will be installed later
3308 goto Continue;
3310 -- If Name (Item) is not an entity name, something is wrong, and
3311 -- this will be detected in due course, for now ignore the item
3313 elsif not Is_Entity_Name (Name (Item)) then
3314 goto Continue;
3316 elsif No (Entity (Name (Item))) then
3317 Set_Entity (Name (Item), Any_Id);
3318 goto Continue;
3319 end if;
3321 Uname_Node := Entity (Name (Item));
3323 if Is_Private_Descendant (Uname_Node) then
3324 Check_Private := True;
3325 end if;
3327 Install_Withed_Unit (Item);
3329 Decl_Node := Unit_Declaration_Node (Uname_Node);
3331 -- If the unit is a subprogram instance, it appears nested within
3332 -- a package that carries the parent information.
3334 if Is_Generic_Instance (Uname_Node)
3335 and then Ekind (Uname_Node) /= E_Package
3336 then
3337 Decl_Node := Parent (Parent (Decl_Node));
3338 end if;
3340 if Is_Child_Spec (Decl_Node) then
3341 if Nkind (Name (Item)) = N_Expanded_Name then
3342 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3343 else
3344 -- If not an expanded name, the child unit must be a
3345 -- renaming, nothing to do.
3347 null;
3348 end if;
3350 elsif Nkind (Decl_Node) = N_Subprogram_Body
3351 and then not Acts_As_Spec (Parent (Decl_Node))
3352 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3353 then
3354 Implicit_With_On_Parent
3355 (Unit (Library_Unit (Parent (Decl_Node))), N);
3356 end if;
3358 -- Check license conditions unless this is a dummy unit
3360 if Sloc (Library_Unit (Item)) /= No_Location then
3361 License_Check : declare
3362 Withu : constant Unit_Number_Type :=
3363 Get_Source_Unit (Library_Unit (Item));
3364 Withl : constant License_Type :=
3365 License (Source_Index (Withu));
3366 Unitl : constant License_Type :=
3367 License (Source_Index (Current_Sem_Unit));
3369 procedure License_Error;
3370 -- Signal error of bad license
3372 -------------------
3373 -- License_Error --
3374 -------------------
3376 procedure License_Error is
3377 begin
3378 Error_Msg_N
3379 ("license of withed unit & may be inconsistent??",
3380 Name (Item));
3381 end License_Error;
3383 -- Start of processing for License_Check
3385 begin
3386 -- Exclude license check if withed unit is an internal unit.
3387 -- This situation arises e.g. with the GPL version of GNAT.
3389 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3390 null;
3392 -- Otherwise check various cases
3393 else
3394 case Unitl is
3395 when Unknown =>
3396 null;
3398 when Restricted =>
3399 if Withl = GPL then
3400 License_Error;
3401 end if;
3403 when GPL =>
3404 if Withl = Restricted then
3405 License_Error;
3406 end if;
3408 when Modified_GPL =>
3409 if Withl = Restricted or else Withl = GPL then
3410 License_Error;
3411 end if;
3413 when Unrestricted =>
3414 null;
3415 end case;
3416 end if;
3417 end License_Check;
3418 end if;
3420 -- Case of USE PACKAGE clause
3422 elsif Nkind (Item) = N_Use_Package_Clause then
3423 Analyze_Use_Package (Item);
3425 -- Case of USE TYPE clause
3427 elsif Nkind (Item) = N_Use_Type_Clause then
3428 Analyze_Use_Type (Item);
3430 -- case of PRAGMA
3432 elsif Nkind (Item) = N_Pragma then
3433 Analyze (Item);
3434 end if;
3436 <<Continue>>
3437 Next (Item);
3438 end loop;
3440 if Is_Child_Spec (Lib_Unit) then
3442 -- The unit also has implicit with_clauses on its own parents
3444 if No (Context_Items (N)) then
3445 Set_Context_Items (N, New_List);
3446 end if;
3448 Implicit_With_On_Parent (Lib_Unit, N);
3449 end if;
3451 -- If the unit is a body, the context of the specification must also
3452 -- be installed. That includes private with_clauses in that context.
3454 if Nkind (Lib_Unit) = N_Package_Body
3455 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3456 and then not Acts_As_Spec (N))
3457 then
3458 Install_Context (Library_Unit (N));
3460 -- Only install private with-clauses of a spec that comes from
3461 -- source, excluding specs created for a subprogram body that is
3462 -- a child unit.
3464 if Comes_From_Source (Library_Unit (N)) then
3465 Install_Private_With_Clauses
3466 (Defining_Entity (Unit (Library_Unit (N))));
3467 end if;
3469 if Is_Child_Spec (Unit (Library_Unit (N))) then
3471 -- If the unit is the body of a public child unit, the private
3472 -- declarations of the parent must be made visible. If the child
3473 -- unit is private, the private declarations have been installed
3474 -- already in the call to Install_Parents for the spec. Installing
3475 -- private declarations must be done for all ancestors of public
3476 -- child units. In addition, sibling units mentioned in the
3477 -- context clause of the body are directly visible.
3479 declare
3480 Lib_Spec : Node_Id;
3481 P : Node_Id;
3482 P_Name : Entity_Id;
3484 begin
3485 Lib_Spec := Unit (Library_Unit (N));
3486 while Is_Child_Spec (Lib_Spec) loop
3487 P := Unit (Parent_Spec (Lib_Spec));
3488 P_Name := Defining_Entity (P);
3490 if not (Private_Present (Parent (Lib_Spec)))
3491 and then not In_Private_Part (P_Name)
3492 then
3493 Install_Private_Declarations (P_Name);
3494 Install_Private_With_Clauses (P_Name);
3495 Set_Use (Private_Declarations (Specification (P)));
3496 end if;
3498 Lib_Spec := P;
3499 end loop;
3500 end;
3501 end if;
3503 -- For a package body, children in context are immediately visible
3505 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3506 end if;
3508 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3509 N_Generic_Subprogram_Declaration,
3510 N_Package_Declaration,
3511 N_Subprogram_Declaration)
3512 then
3513 if Is_Child_Spec (Lib_Unit) then
3514 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3515 Set_Is_Private_Descendant
3516 (Defining_Entity (Lib_Unit),
3517 Is_Private_Descendant (Lib_Parent)
3518 or else Private_Present (Parent (Lib_Unit)));
3520 else
3521 Set_Is_Private_Descendant
3522 (Defining_Entity (Lib_Unit),
3523 Private_Present (Parent (Lib_Unit)));
3524 end if;
3525 end if;
3527 if Check_Private then
3528 Check_Private_Child_Unit (N);
3529 end if;
3530 end Install_Context_Clauses;
3532 -------------------------------------
3533 -- Install_Limited_Context_Clauses --
3534 -------------------------------------
3536 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3537 Item : Node_Id;
3539 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3540 -- Check that the unlimited view of a given compilation_unit is not
3541 -- already visible through "use + renamings".
3543 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3544 -- Check that if a limited_with clause of a given compilation_unit
3545 -- mentions a descendant of a private child of some library unit, then
3546 -- the given compilation_unit shall be the declaration of a private
3547 -- descendant of that library unit, or a public descendant of such. The
3548 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3549 -- use entities on the limited with_clauses because their units have not
3550 -- been analyzed, so we have to climb the tree of ancestors looking for
3551 -- private keywords.
3553 procedure Expand_Limited_With_Clause
3554 (Comp_Unit : Node_Id;
3555 Nam : Node_Id;
3556 N : Node_Id);
3557 -- If a child unit appears in a limited_with clause, there are implicit
3558 -- limited_with clauses on all parents that are not already visible
3559 -- through a regular with clause. This procedure creates the implicit
3560 -- limited with_clauses for the parents and loads the corresponding
3561 -- units. The shadow entities are created when the inserted clause is
3562 -- analyzed. Implements Ada 2005 (AI-50217).
3564 ---------------------
3565 -- Check_Renamings --
3566 ---------------------
3568 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3569 Item : Node_Id;
3570 Spec : Node_Id;
3571 WEnt : Entity_Id;
3572 Nam : Node_Id;
3573 E : Entity_Id;
3574 E2 : Entity_Id;
3576 begin
3577 pragma Assert (Nkind (W) = N_With_Clause);
3579 -- Protect the frontend against previous critical errors
3581 case Nkind (Unit (Library_Unit (W))) is
3582 when N_Subprogram_Declaration |
3583 N_Package_Declaration |
3584 N_Generic_Subprogram_Declaration |
3585 N_Generic_Package_Declaration =>
3586 null;
3588 when others =>
3589 return;
3590 end case;
3592 -- Check "use + renamings"
3594 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3595 Spec := Specification (Unit (P));
3597 Item := First (Visible_Declarations (Spec));
3598 while Present (Item) loop
3600 -- Look only at use package clauses
3602 if Nkind (Item) = N_Use_Package_Clause then
3604 -- Traverse the list of packages
3606 Nam := First (Names (Item));
3607 while Present (Nam) loop
3608 E := Entity (Nam);
3610 pragma Assert (Present (Parent (E)));
3612 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3613 and then Renamed_Entity (E) = WEnt
3614 then
3615 -- The unlimited view is visible through use clause and
3616 -- renamings. There is no need to generate the error
3617 -- message here because Is_Visible_Through_Renamings
3618 -- takes care of generating the precise error message.
3620 return;
3622 elsif Nkind (Parent (E)) = N_Package_Specification then
3624 -- The use clause may refer to a local package.
3625 -- Check all the enclosing scopes.
3627 E2 := E;
3628 while E2 /= Standard_Standard and then E2 /= WEnt loop
3629 E2 := Scope (E2);
3630 end loop;
3632 if E2 = WEnt then
3633 Error_Msg_N
3634 ("unlimited view visible through use clause ", W);
3635 return;
3636 end if;
3637 end if;
3639 Next (Nam);
3640 end loop;
3641 end if;
3643 Next (Item);
3644 end loop;
3646 -- Recursive call to check all the ancestors
3648 if Is_Child_Spec (Unit (P)) then
3649 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3650 end if;
3651 end Check_Renamings;
3653 ---------------------------------------
3654 -- Check_Private_Limited_Withed_Unit --
3655 ---------------------------------------
3657 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3658 Curr_Parent : Node_Id;
3659 Child_Parent : Node_Id;
3660 Curr_Private : Boolean;
3662 begin
3663 -- Compilation unit of the parent of the withed library unit
3665 Child_Parent := Library_Unit (Item);
3667 -- If the child unit is a public child, then locate its nearest
3668 -- private ancestor, if any, then Child_Parent will then be set to
3669 -- the parent of that ancestor.
3671 if not Private_Present (Library_Unit (Item)) then
3672 while Present (Child_Parent)
3673 and then not Private_Present (Child_Parent)
3674 loop
3675 Child_Parent := Parent_Spec (Unit (Child_Parent));
3676 end loop;
3678 if No (Child_Parent) then
3679 return;
3680 end if;
3681 end if;
3683 Child_Parent := Parent_Spec (Unit (Child_Parent));
3685 -- Traverse all the ancestors of the current compilation unit to
3686 -- check if it is a descendant of named library unit.
3688 Curr_Parent := Parent (Item);
3689 Curr_Private := Private_Present (Curr_Parent);
3691 while Present (Parent_Spec (Unit (Curr_Parent)))
3692 and then Curr_Parent /= Child_Parent
3693 loop
3694 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3695 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3696 end loop;
3698 if Curr_Parent /= Child_Parent then
3699 Error_Msg_N
3700 ("unit in with clause is private child unit!", Item);
3701 Error_Msg_NE
3702 ("\current unit must also have parent&!",
3703 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3705 elsif Private_Present (Parent (Item))
3706 or else Curr_Private
3707 or else Private_Present (Item)
3708 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3709 N_Subprogram_Body,
3710 N_Subunit)
3711 then
3712 -- Current unit is private, of descendant of a private unit
3714 null;
3716 else
3717 Error_Msg_NE
3718 ("current unit must also be private descendant of&",
3719 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3720 end if;
3721 end Check_Private_Limited_Withed_Unit;
3723 --------------------------------
3724 -- Expand_Limited_With_Clause --
3725 --------------------------------
3727 procedure Expand_Limited_With_Clause
3728 (Comp_Unit : Node_Id;
3729 Nam : Node_Id;
3730 N : Node_Id)
3732 Loc : constant Source_Ptr := Sloc (Nam);
3733 Unum : Unit_Number_Type;
3734 Withn : Node_Id;
3736 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3737 -- Returns true if the context already includes a with_clause for
3738 -- this unit. If the with_clause is non-limited, the unit is fully
3739 -- visible and an implicit limited_with should not be created. If
3740 -- there is already a limited_with clause for W, a second one is
3741 -- simply redundant.
3743 --------------------------
3744 -- Previous_Withed_Unit --
3745 --------------------------
3747 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3748 Item : Node_Id;
3750 begin
3751 -- A limited with_clause cannot appear in the same context_clause
3752 -- as a nonlimited with_clause which mentions the same library.
3754 Item := First (Context_Items (Comp_Unit));
3755 while Present (Item) loop
3756 if Nkind (Item) = N_With_Clause
3757 and then Library_Unit (Item) = Library_Unit (W)
3758 then
3759 return True;
3760 end if;
3762 Next (Item);
3763 end loop;
3765 return False;
3766 end Previous_Withed_Unit;
3768 -- Start of processing for Expand_Limited_With_Clause
3770 begin
3771 if Nkind (Nam) = N_Identifier then
3773 -- Create node for name of withed unit
3775 Withn :=
3776 Make_With_Clause (Loc,
3777 Name => New_Copy (Nam));
3779 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3780 Withn :=
3781 Make_With_Clause (Loc,
3782 Name => Make_Selected_Component (Loc,
3783 Prefix => New_Copy_Tree (Prefix (Nam)),
3784 Selector_Name => New_Copy (Selector_Name (Nam))));
3785 Set_Parent (Withn, Parent (N));
3786 end if;
3788 Set_Limited_Present (Withn);
3789 Set_First_Name (Withn);
3790 Set_Implicit_With (Withn);
3792 Unum :=
3793 Load_Unit
3794 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3795 Required => True,
3796 Subunit => False,
3797 Error_Node => Nam);
3799 -- Do not generate a limited_with_clause on the current unit. This
3800 -- path is taken when a unit has a limited_with clause on one of its
3801 -- child units.
3803 if Unum = Current_Sem_Unit then
3804 return;
3805 end if;
3807 Set_Library_Unit (Withn, Cunit (Unum));
3808 Set_Corresponding_Spec
3809 (Withn, Specification (Unit (Cunit (Unum))));
3811 if not Previous_Withed_Unit (Withn) then
3812 Prepend (Withn, Context_Items (Parent (N)));
3813 Mark_Rewrite_Insertion (Withn);
3815 -- Add implicit limited_with_clauses for parents of child units
3816 -- mentioned in limited_with clauses.
3818 if Nkind (Nam) = N_Selected_Component then
3819 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3820 end if;
3822 Analyze (Withn);
3824 if not Limited_View_Installed (Withn) then
3825 Install_Limited_Withed_Unit (Withn);
3826 end if;
3827 end if;
3828 end Expand_Limited_With_Clause;
3830 -- Start of processing for Install_Limited_Context_Clauses
3832 begin
3833 Item := First (Context_Items (N));
3834 while Present (Item) loop
3835 if Nkind (Item) = N_With_Clause
3836 and then Limited_Present (Item)
3837 and then not Error_Posted (Item)
3838 then
3839 if Nkind (Name (Item)) = N_Selected_Component then
3840 Expand_Limited_With_Clause
3841 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3842 end if;
3844 Check_Private_Limited_Withed_Unit (Item);
3846 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
3847 Check_Renamings (Parent_Spec (Unit (N)), Item);
3848 end if;
3850 -- A unit may have a limited with on itself if it has a limited
3851 -- with_clause on one of its child units. In that case it is
3852 -- already being compiled and it makes no sense to install its
3853 -- limited view.
3855 -- If the item is a limited_private_with_clause, install it if the
3856 -- current unit is a body or if it is a private child. Otherwise
3857 -- the private clause is installed before analyzing the private
3858 -- part of the current unit.
3860 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3861 and then not Limited_View_Installed (Item)
3862 and then
3863 not Is_Ancestor_Unit
3864 (Library_Unit (Item), Cunit (Current_Sem_Unit))
3865 then
3866 if not Private_Present (Item)
3867 or else Private_Present (N)
3868 or else Nkind_In (Unit (N), N_Package_Body,
3869 N_Subprogram_Body,
3870 N_Subunit)
3871 then
3872 Install_Limited_Withed_Unit (Item);
3873 end if;
3874 end if;
3875 end if;
3877 Next (Item);
3878 end loop;
3880 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
3881 -- looking for incomplete subtype declarations of incomplete types
3882 -- visible through a limited with clause.
3884 if Ada_Version >= Ada_2005
3885 and then Analyzed (N)
3886 and then Nkind (Unit (N)) = N_Package_Declaration
3887 then
3888 declare
3889 Decl : Node_Id;
3890 Def_Id : Entity_Id;
3891 Non_Lim_View : Entity_Id;
3893 begin
3894 Decl := First (Visible_Declarations (Specification (Unit (N))));
3895 while Present (Decl) loop
3896 if Nkind (Decl) = N_Subtype_Declaration
3897 and then
3898 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3899 and then
3900 From_Limited_With (Defining_Identifier (Decl))
3901 then
3902 Def_Id := Defining_Identifier (Decl);
3903 Non_Lim_View := Non_Limited_View (Def_Id);
3905 if not Is_Incomplete_Type (Non_Lim_View) then
3907 -- Convert an incomplete subtype declaration into a
3908 -- corresponding non-limited view subtype declaration.
3909 -- This is usually the case when analyzing a body that
3910 -- has regular with clauses, when the spec has limited
3911 -- ones.
3913 -- If the non-limited view is still incomplete, it is
3914 -- the dummy entry already created, and the declaration
3915 -- cannot be reanalyzed. This is the case when installing
3916 -- a parent unit that has limited with-clauses.
3918 Set_Subtype_Indication (Decl,
3919 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3920 Set_Etype (Def_Id, Non_Lim_View);
3921 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3922 Set_Analyzed (Decl, False);
3924 -- Reanalyze the declaration, suppressing the call to
3925 -- Enter_Name to avoid duplicate names.
3927 Analyze_Subtype_Declaration
3928 (N => Decl,
3929 Skip => True);
3930 end if;
3931 end if;
3933 Next (Decl);
3934 end loop;
3935 end;
3936 end if;
3937 end Install_Limited_Context_Clauses;
3939 ---------------------
3940 -- Install_Parents --
3941 ---------------------
3943 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3944 P : Node_Id;
3945 E_Name : Entity_Id;
3946 P_Name : Entity_Id;
3947 P_Spec : Node_Id;
3949 begin
3950 P := Unit (Parent_Spec (Lib_Unit));
3951 P_Name := Get_Parent_Entity (P);
3953 if Etype (P_Name) = Any_Type then
3954 return;
3955 end if;
3957 if Ekind (P_Name) = E_Generic_Package
3958 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3959 N_Generic_Package_Declaration)
3960 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3961 then
3962 Error_Msg_N
3963 ("child of a generic package must be a generic unit", Lib_Unit);
3965 elsif not Is_Package_Or_Generic_Package (P_Name) then
3966 Error_Msg_N
3967 ("parent unit must be package or generic package", Lib_Unit);
3968 raise Unrecoverable_Error;
3970 elsif Present (Renamed_Object (P_Name)) then
3971 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3972 raise Unrecoverable_Error;
3974 -- Verify that a child of an instance is itself an instance, or the
3975 -- renaming of one. Given that an instance that is a unit is replaced
3976 -- with a package declaration, check against the original node. The
3977 -- parent may be currently being instantiated, in which case it appears
3978 -- as a declaration, but the generic_parent is already established
3979 -- indicating that we deal with an instance.
3981 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3982 if Nkind (Lib_Unit) in N_Renaming_Declaration
3983 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3984 or else
3985 (Nkind (Lib_Unit) = N_Package_Declaration
3986 and then Present (Generic_Parent (Specification (Lib_Unit))))
3987 then
3988 null;
3989 else
3990 Error_Msg_N
3991 ("child of an instance must be an instance or renaming",
3992 Lib_Unit);
3993 end if;
3994 end if;
3996 -- This is the recursive call that ensures all parents are loaded
3998 if Is_Child_Spec (P) then
3999 Install_Parents (P,
4000 Is_Private or else Private_Present (Parent (Lib_Unit)));
4001 end if;
4003 -- Now we can install the context for this parent
4005 Install_Context_Clauses (Parent_Spec (Lib_Unit));
4006 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4007 Install_Siblings (P_Name, Parent (Lib_Unit));
4009 -- The child unit is in the declarative region of the parent. The parent
4010 -- must therefore appear in the scope stack and be visible, as when
4011 -- compiling the corresponding body. If the child unit is private or it
4012 -- is a package body, private declarations must be accessible as well.
4013 -- Use declarations in the parent must also be installed. Finally, other
4014 -- child units of the same parent that are in the context are
4015 -- immediately visible.
4017 -- Find entity for compilation unit, and set its private descendant
4018 -- status as needed. Indicate that it is a compilation unit, which is
4019 -- redundant in general, but needed if this is a generated child spec
4020 -- for a child body without previous spec.
4022 E_Name := Defining_Entity (Lib_Unit);
4024 Set_Is_Child_Unit (E_Name);
4025 Set_Is_Compilation_Unit (E_Name);
4027 Set_Is_Private_Descendant (E_Name,
4028 Is_Private_Descendant (P_Name)
4029 or else Private_Present (Parent (Lib_Unit)));
4031 P_Spec := Package_Specification (P_Name);
4032 Push_Scope (P_Name);
4034 -- Save current visibility of unit
4036 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4037 Is_Immediately_Visible (P_Name);
4038 Set_Is_Immediately_Visible (P_Name);
4039 Install_Visible_Declarations (P_Name);
4040 Set_Use (Visible_Declarations (P_Spec));
4042 -- If the parent is a generic unit, its formal part may contain formal
4043 -- packages and use clauses for them.
4045 if Ekind (P_Name) = E_Generic_Package then
4046 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4047 end if;
4049 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4050 Install_Private_Declarations (P_Name);
4051 Install_Private_With_Clauses (P_Name);
4052 Set_Use (Private_Declarations (P_Spec));
4053 end if;
4054 end Install_Parents;
4056 ----------------------------------
4057 -- Install_Private_With_Clauses --
4058 ----------------------------------
4060 procedure Install_Private_With_Clauses (P : Entity_Id) is
4061 Decl : constant Node_Id := Unit_Declaration_Node (P);
4062 Item : Node_Id;
4064 begin
4065 if Debug_Flag_I then
4066 Write_Str ("install private with clauses of ");
4067 Write_Name (Chars (P));
4068 Write_Eol;
4069 end if;
4071 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4072 Item := First (Context_Items (Parent (Decl)));
4073 while Present (Item) loop
4074 if Nkind (Item) = N_With_Clause
4075 and then Private_Present (Item)
4076 then
4077 -- If the unit is an ancestor of the current one, it is the
4078 -- case of a private limited with clause on a child unit, and
4079 -- the compilation of one of its descendants, In that case the
4080 -- limited view is errelevant.
4082 if Limited_Present (Item) then
4083 if not Limited_View_Installed (Item)
4084 and then
4085 not Is_Ancestor_Unit (Library_Unit (Item),
4086 Cunit (Current_Sem_Unit))
4087 then
4088 Install_Limited_Withed_Unit (Item);
4089 end if;
4090 else
4091 Install_Withed_Unit (Item, Private_With_OK => True);
4092 end if;
4093 end if;
4095 Next (Item);
4096 end loop;
4097 end if;
4098 end Install_Private_With_Clauses;
4100 ----------------------
4101 -- Install_Siblings --
4102 ----------------------
4104 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4105 Item : Node_Id;
4106 Id : Entity_Id;
4107 Prev : Entity_Id;
4109 begin
4110 -- Iterate over explicit with clauses, and check whether the scope of
4111 -- each entity is an ancestor of the current unit, in which case it is
4112 -- immediately visible.
4114 Item := First (Context_Items (N));
4115 while Present (Item) loop
4117 -- Do not install private_with_clauses declaration, unless unit
4118 -- is itself a private child unit, or is a body. Note that for a
4119 -- subprogram body the private_with_clause does not take effect until
4120 -- after the specification.
4122 if Nkind (Item) /= N_With_Clause
4123 or else Implicit_With (Item)
4124 or else Limited_Present (Item)
4125 or else Error_Posted (Item)
4126 then
4127 null;
4129 elsif not Private_Present (Item)
4130 or else Private_Present (N)
4131 or else Nkind (Unit (N)) = N_Package_Body
4132 then
4133 Id := Entity (Name (Item));
4135 if Is_Child_Unit (Id)
4136 and then Is_Ancestor_Package (Scope (Id), U_Name)
4137 then
4138 Set_Is_Immediately_Visible (Id);
4140 -- Check for the presence of another unit in the context that
4141 -- may be inadvertently hidden by the child.
4143 Prev := Current_Entity (Id);
4145 if Present (Prev)
4146 and then Is_Immediately_Visible (Prev)
4147 and then not Is_Child_Unit (Prev)
4148 then
4149 declare
4150 Clause : Node_Id;
4152 begin
4153 Clause := First (Context_Items (N));
4154 while Present (Clause) loop
4155 if Nkind (Clause) = N_With_Clause
4156 and then Entity (Name (Clause)) = Prev
4157 then
4158 Error_Msg_NE
4159 ("child unit& hides compilation unit " &
4160 "with the same name??",
4161 Name (Item), Id);
4162 exit;
4163 end if;
4165 Next (Clause);
4166 end loop;
4167 end;
4168 end if;
4170 -- The With_Clause may be on a grand-child or one of its further
4171 -- descendants, which makes a child immediately visible. Examine
4172 -- ancestry to determine whether such a child exists. For example,
4173 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4174 -- is immediately visible.
4176 elsif Is_Child_Unit (Id) then
4177 declare
4178 Par : Entity_Id;
4180 begin
4181 Par := Scope (Id);
4182 while Is_Child_Unit (Par) loop
4183 if Is_Ancestor_Package (Scope (Par), U_Name) then
4184 Set_Is_Immediately_Visible (Par);
4185 exit;
4186 end if;
4188 Par := Scope (Par);
4189 end loop;
4190 end;
4191 end if;
4193 -- If the item is a private with-clause on a child unit, the parent
4194 -- may have been installed already, but the child unit must remain
4195 -- invisible until installed in a private part or body, unless there
4196 -- is already a regular with_clause for it in the current unit.
4198 elsif Private_Present (Item) then
4199 Id := Entity (Name (Item));
4201 if Is_Child_Unit (Id) then
4202 declare
4203 Clause : Node_Id;
4205 function In_Context return Boolean;
4206 -- Scan context of current unit, to check whether there is
4207 -- a with_clause on the same unit as a private with-clause
4208 -- on a parent, in which case child unit is visible. If the
4209 -- unit is a grand-child, the same applies to its parent.
4211 ----------------
4212 -- In_Context --
4213 ----------------
4215 function In_Context return Boolean is
4216 begin
4217 Clause :=
4218 First (Context_Items (Cunit (Current_Sem_Unit)));
4219 while Present (Clause) loop
4220 if Nkind (Clause) = N_With_Clause
4221 and then Comes_From_Source (Clause)
4222 and then Is_Entity_Name (Name (Clause))
4223 and then not Private_Present (Clause)
4224 then
4225 if Entity (Name (Clause)) = Id
4226 or else
4227 (Nkind (Name (Clause)) = N_Expanded_Name
4228 and then Entity (Prefix (Name (Clause))) = Id)
4229 then
4230 return True;
4231 end if;
4232 end if;
4234 Next (Clause);
4235 end loop;
4237 return False;
4238 end In_Context;
4240 begin
4241 Set_Is_Visible_Lib_Unit (Id, In_Context);
4242 end;
4243 end if;
4244 end if;
4246 Next (Item);
4247 end loop;
4248 end Install_Siblings;
4250 ---------------------------------
4251 -- Install_Limited_Withed_Unit --
4252 ---------------------------------
4254 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4255 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4256 E : Entity_Id;
4257 P : Entity_Id;
4258 Is_Child_Package : Boolean := False;
4259 Lim_Header : Entity_Id;
4260 Lim_Typ : Entity_Id;
4262 procedure Check_Body_Required;
4263 -- A unit mentioned in a limited with_clause may not be mentioned in
4264 -- a regular with_clause, but must still be included in the current
4265 -- partition. We need to determine whether the unit needs a body, so
4266 -- that the binder can determine the name of the file to be compiled.
4267 -- Checking whether a unit needs a body can be done without semantic
4268 -- analysis, by examining the nature of the declarations in the package.
4270 function Has_Limited_With_Clause
4271 (C_Unit : Entity_Id;
4272 Pack : Entity_Id) return Boolean;
4273 -- Determine whether any package in the ancestor chain starting with
4274 -- C_Unit has a limited with clause for package Pack.
4276 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4277 -- Check if some package installed though normal with-clauses has a
4278 -- renaming declaration of package P. AARM 10.1.2(21/2).
4280 -------------------------
4281 -- Check_Body_Required --
4282 -------------------------
4284 procedure Check_Body_Required is
4285 PA : constant List_Id :=
4286 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4288 procedure Check_Declarations (Spec : Node_Id);
4289 -- Recursive procedure that does the work and checks nested packages
4291 ------------------------
4292 -- Check_Declarations --
4293 ------------------------
4295 procedure Check_Declarations (Spec : Node_Id) is
4296 Decl : Node_Id;
4297 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4299 Subp_List : constant Elist_Id := New_Elmt_List;
4301 procedure Check_Pragma_Import (P : Node_Id);
4302 -- If a pragma import applies to a previous subprogram, the
4303 -- enclosing unit may not need a body. The processing is syntactic
4304 -- and does not require a declaration to be analyzed. The code
4305 -- below also handles pragma Import when applied to a subprogram
4306 -- that renames another. In this case the pragma applies to the
4307 -- renamed entity.
4309 -- Chains of multiple renames are not handled by the code below.
4310 -- It is probably impossible to handle all cases without proper
4311 -- name resolution. In such cases the algorithm is conservative
4312 -- and will indicate that a body is needed???
4314 -------------------------
4315 -- Check_Pragma_Import --
4316 -------------------------
4318 procedure Check_Pragma_Import (P : Node_Id) is
4319 Arg : Node_Id;
4320 Prev_Id : Elmt_Id;
4321 Subp_Id : Elmt_Id;
4322 Imported : Node_Id;
4324 procedure Remove_Homonyms (E : Node_Id);
4325 -- Make one pass over list of subprograms. Called again if
4326 -- subprogram is a renaming. E is known to be an identifier.
4328 ---------------------
4329 -- Remove_Homonyms --
4330 ---------------------
4332 procedure Remove_Homonyms (E : Node_Id) is
4333 R : Entity_Id := Empty;
4334 -- Name of renamed entity, if any
4336 begin
4337 Subp_Id := First_Elmt (Subp_List);
4338 while Present (Subp_Id) loop
4339 if Chars (Node (Subp_Id)) = Chars (E) then
4340 if Nkind (Parent (Parent (Node (Subp_Id))))
4341 /= N_Subprogram_Renaming_Declaration
4342 then
4343 Prev_Id := Subp_Id;
4344 Next_Elmt (Subp_Id);
4345 Remove_Elmt (Subp_List, Prev_Id);
4346 else
4347 R := Name (Parent (Parent (Node (Subp_Id))));
4348 exit;
4349 end if;
4350 else
4351 Next_Elmt (Subp_Id);
4352 end if;
4353 end loop;
4355 if Present (R) then
4356 if Nkind (R) = N_Identifier then
4357 Remove_Homonyms (R);
4359 elsif Nkind (R) = N_Selected_Component then
4360 Remove_Homonyms (Selector_Name (R));
4362 -- Renaming of attribute
4364 else
4365 null;
4366 end if;
4367 end if;
4368 end Remove_Homonyms;
4370 -- Start of processing for Check_Pragma_Import
4372 begin
4373 -- Find name of entity in Import pragma. We have not analyzed
4374 -- the construct, so we must guard against syntax errors.
4376 Arg := Next (First (Pragma_Argument_Associations (P)));
4378 if No (Arg)
4379 or else Nkind (Expression (Arg)) /= N_Identifier
4380 then
4381 return;
4382 else
4383 Imported := Expression (Arg);
4384 end if;
4386 Remove_Homonyms (Imported);
4387 end Check_Pragma_Import;
4389 -- Start of processing for Check_Declarations
4391 begin
4392 -- Search for Elaborate Body pragma
4394 Decl := First (Visible_Declarations (Spec));
4395 while Present (Decl)
4396 and then Nkind (Decl) = N_Pragma
4397 loop
4398 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4399 Set_Body_Required (Library_Unit (N));
4400 return;
4401 end if;
4403 Next (Decl);
4404 end loop;
4406 -- Look for declarations that require the presence of a body. We
4407 -- have already skipped pragmas at the start of the list.
4409 while Present (Decl) loop
4411 -- Subprogram that comes from source means body may be needed.
4412 -- Save for subsequent examination of import pragmas.
4414 if Comes_From_Source (Decl)
4415 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4416 N_Subprogram_Renaming_Declaration,
4417 N_Generic_Subprogram_Declaration))
4418 then
4419 Append_Elmt (Defining_Entity (Decl), Subp_List);
4421 -- Package declaration of generic package declaration. We need
4422 -- to recursively examine nested declarations.
4424 elsif Nkind_In (Decl, N_Package_Declaration,
4425 N_Generic_Package_Declaration)
4426 then
4427 Check_Declarations (Specification (Decl));
4429 elsif Nkind (Decl) = N_Pragma
4430 and then Pragma_Name (Decl) = Name_Import
4431 then
4432 Check_Pragma_Import (Decl);
4433 end if;
4435 Next (Decl);
4436 end loop;
4438 -- Same set of tests for private part. In addition to subprograms
4439 -- detect the presence of Taft Amendment types (incomplete types
4440 -- completed in the body).
4442 Decl := First (Private_Declarations (Spec));
4443 while Present (Decl) loop
4444 if Comes_From_Source (Decl)
4445 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4446 N_Subprogram_Renaming_Declaration,
4447 N_Generic_Subprogram_Declaration))
4448 then
4449 Append_Elmt (Defining_Entity (Decl), Subp_List);
4451 elsif Nkind_In (Decl, N_Package_Declaration,
4452 N_Generic_Package_Declaration)
4453 then
4454 Check_Declarations (Specification (Decl));
4456 -- Collect incomplete type declarations for separate pass
4458 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4459 Append_Elmt (Decl, Incomplete_Decls);
4461 elsif Nkind (Decl) = N_Pragma
4462 and then Pragma_Name (Decl) = Name_Import
4463 then
4464 Check_Pragma_Import (Decl);
4465 end if;
4467 Next (Decl);
4468 end loop;
4470 -- Now check incomplete declarations to locate Taft amendment
4471 -- types. This can be done by examining the defining identifiers
4472 -- of type declarations without real semantic analysis.
4474 declare
4475 Inc : Elmt_Id;
4477 begin
4478 Inc := First_Elmt (Incomplete_Decls);
4479 while Present (Inc) loop
4480 Decl := Next (Node (Inc));
4481 while Present (Decl) loop
4482 if Nkind (Decl) = N_Full_Type_Declaration
4483 and then Chars (Defining_Identifier (Decl)) =
4484 Chars (Defining_Identifier (Node (Inc)))
4485 then
4486 exit;
4487 end if;
4489 Next (Decl);
4490 end loop;
4492 -- If no completion, this is a TAT, and a body is needed
4494 if No (Decl) then
4495 Set_Body_Required (Library_Unit (N));
4496 return;
4497 end if;
4499 Next_Elmt (Inc);
4500 end loop;
4501 end;
4503 -- Finally, check whether there are subprograms that still require
4504 -- a body, i.e. are not renamings or null.
4506 if not Is_Empty_Elmt_List (Subp_List) then
4507 declare
4508 Subp_Id : Elmt_Id;
4509 Spec : Node_Id;
4511 begin
4512 Subp_Id := First_Elmt (Subp_List);
4513 Spec := Parent (Node (Subp_Id));
4515 while Present (Subp_Id) loop
4516 if Nkind (Parent (Spec))
4517 = N_Subprogram_Renaming_Declaration
4518 then
4519 null;
4521 elsif Nkind (Spec) = N_Procedure_Specification
4522 and then Null_Present (Spec)
4523 then
4524 null;
4526 else
4527 Set_Body_Required (Library_Unit (N));
4528 return;
4529 end if;
4531 Next_Elmt (Subp_Id);
4532 end loop;
4533 end;
4534 end if;
4535 end Check_Declarations;
4537 -- Start of processing for Check_Body_Required
4539 begin
4540 -- If this is an imported package (Java and CIL usage) no body is
4541 -- needed. Scan list of pragmas that may follow a compilation unit
4542 -- to look for a relevant pragma Import.
4544 if Present (PA) then
4545 declare
4546 Prag : Node_Id;
4548 begin
4549 Prag := First (PA);
4550 while Present (Prag) loop
4551 if Nkind (Prag) = N_Pragma
4552 and then Get_Pragma_Id (Prag) = Pragma_Import
4553 then
4554 return;
4555 end if;
4557 Next (Prag);
4558 end loop;
4559 end;
4560 end if;
4562 Check_Declarations (Specification (P_Unit));
4563 end Check_Body_Required;
4565 -----------------------------
4566 -- Has_Limited_With_Clause --
4567 -----------------------------
4569 function Has_Limited_With_Clause
4570 (C_Unit : Entity_Id;
4571 Pack : Entity_Id) return Boolean
4573 Par : Entity_Id;
4574 Par_Unit : Node_Id;
4576 begin
4577 Par := C_Unit;
4578 while Present (Par) loop
4579 if Ekind (Par) /= E_Package then
4580 exit;
4581 end if;
4583 -- Retrieve the Compilation_Unit node for Par and determine if
4584 -- its context clauses contain a limited with for Pack.
4586 Par_Unit := Parent (Parent (Parent (Par)));
4588 if Nkind (Par_Unit) = N_Package_Declaration then
4589 Par_Unit := Parent (Par_Unit);
4590 end if;
4592 if Has_With_Clause (Par_Unit, Pack, True) then
4593 return True;
4594 end if;
4596 -- If there are more ancestors, climb up the tree, otherwise we
4597 -- are done.
4599 if Is_Child_Unit (Par) then
4600 Par := Scope (Par);
4601 else
4602 exit;
4603 end if;
4604 end loop;
4606 return False;
4607 end Has_Limited_With_Clause;
4609 ----------------------------------
4610 -- Is_Visible_Through_Renamings --
4611 ----------------------------------
4613 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4614 Kind : constant Node_Kind :=
4615 Nkind (Unit (Cunit (Current_Sem_Unit)));
4616 Aux_Unit : Node_Id;
4617 Item : Node_Id;
4618 Decl : Entity_Id;
4620 begin
4621 -- Example of the error detected by this subprogram:
4623 -- package P is
4624 -- type T is ...
4625 -- end P;
4627 -- with P;
4628 -- package Q is
4629 -- package Ren_P renames P;
4630 -- end Q;
4632 -- with Q;
4633 -- package R is ...
4635 -- limited with P; -- ERROR
4636 -- package R.C is ...
4638 Aux_Unit := Cunit (Current_Sem_Unit);
4640 loop
4641 Item := First (Context_Items (Aux_Unit));
4642 while Present (Item) loop
4643 if Nkind (Item) = N_With_Clause
4644 and then not Limited_Present (Item)
4645 and then Nkind (Unit (Library_Unit (Item))) =
4646 N_Package_Declaration
4647 then
4648 Decl :=
4649 First (Visible_Declarations
4650 (Specification (Unit (Library_Unit (Item)))));
4651 while Present (Decl) loop
4652 if Nkind (Decl) = N_Package_Renaming_Declaration
4653 and then Entity (Name (Decl)) = P
4654 then
4655 -- Generate the error message only if the current unit
4656 -- is a package declaration; in case of subprogram
4657 -- bodies and package bodies we just return True to
4658 -- indicate that the limited view must not be
4659 -- installed.
4661 if Kind = N_Package_Declaration then
4662 Error_Msg_N
4663 ("simultaneous visibility of the limited and " &
4664 "unlimited views not allowed", N);
4665 Error_Msg_Sloc := Sloc (Item);
4666 Error_Msg_NE
4667 ("\\ unlimited view of & visible through the " &
4668 "context clause #", N, P);
4669 Error_Msg_Sloc := Sloc (Decl);
4670 Error_Msg_NE ("\\ and the renaming #", N, P);
4671 end if;
4673 return True;
4674 end if;
4676 Next (Decl);
4677 end loop;
4678 end if;
4680 Next (Item);
4681 end loop;
4683 -- If it is a body not acting as spec, follow pointer to the
4684 -- corresponding spec, otherwise follow pointer to parent spec.
4686 if Present (Library_Unit (Aux_Unit))
4687 and then Nkind_In (Unit (Aux_Unit),
4688 N_Package_Body, N_Subprogram_Body)
4689 then
4690 if Aux_Unit = Library_Unit (Aux_Unit) then
4692 -- Aux_Unit is a body that acts as a spec. Clause has
4693 -- already been flagged as illegal.
4695 return False;
4697 else
4698 Aux_Unit := Library_Unit (Aux_Unit);
4699 end if;
4701 else
4702 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4703 end if;
4705 exit when No (Aux_Unit);
4706 end loop;
4708 return False;
4709 end Is_Visible_Through_Renamings;
4711 -- Start of processing for Install_Limited_Withed_Unit
4713 begin
4714 pragma Assert (not Limited_View_Installed (N));
4716 -- In case of limited with_clause on subprograms, generics, instances,
4717 -- or renamings, the corresponding error was previously posted and we
4718 -- have nothing to do here. If the file is missing altogether, it has
4719 -- no source location.
4721 if Nkind (P_Unit) /= N_Package_Declaration
4722 or else Sloc (P_Unit) = No_Location
4723 then
4724 return;
4725 end if;
4727 P := Defining_Unit_Name (Specification (P_Unit));
4729 -- Handle child packages
4731 if Nkind (P) = N_Defining_Program_Unit_Name then
4732 Is_Child_Package := True;
4733 P := Defining_Identifier (P);
4734 end if;
4736 -- Do not install the limited-view if the context of the unit is already
4737 -- available through a regular with clause.
4739 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4740 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4741 then
4742 return;
4743 end if;
4745 -- Do not install the limited-view if the full-view is already visible
4746 -- through renaming declarations.
4748 if Is_Visible_Through_Renamings (P) then
4749 return;
4750 end if;
4752 -- Do not install the limited view if this is the unit being analyzed.
4753 -- This unusual case will happen when a unit has a limited_with clause
4754 -- on one of its children. The compilation of the child forces the load
4755 -- of the parent which tries to install the limited view of the child
4756 -- again. Installing the limited view must also be disabled when
4757 -- compiling the body of the child unit.
4759 if P = Cunit_Entity (Current_Sem_Unit)
4760 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4761 and then P = Main_Unit_Entity
4762 and then Is_Ancestor_Unit
4763 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
4764 then
4765 return;
4766 end if;
4768 -- This scenario is similar to the one above, the difference is that the
4769 -- compilation of sibling Par.Sib forces the load of parent Par which
4770 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4771 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4772 -- non-limited views of all entities from Lim_Pack.
4774 -- limited with Lim_Pack; -- [1]
4775 -- package Par is ... package Lim_Pack is ...
4777 -- with Lim_Pack; -- [2]
4778 -- package Par.Sib is ... package body Par.Sib is ...
4780 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4781 -- Sem_Unit is the body of Par.Sib.
4783 if Ekind (P) = E_Package
4784 and then Ekind (Main_Unit_Entity) = E_Package
4785 and then Is_Child_Unit (Main_Unit_Entity)
4787 -- The body has a regular with clause
4789 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4790 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4792 -- One of the ancestors has a limited with clause
4794 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4795 N_Package_Specification
4796 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4797 then
4798 return;
4799 end if;
4801 -- A common use of the limited-with is to have a limited-with in the
4802 -- package spec, and a normal with in its package body. For example:
4804 -- limited with X; -- [1]
4805 -- package A is ...
4807 -- with X; -- [2]
4808 -- package body A is ...
4810 -- The compilation of A's body installs the context clauses found at [2]
4811 -- and then the context clauses of its specification (found at [1]). As
4812 -- a consequence, at [1] the specification of X has been analyzed and it
4813 -- is immediately visible. According to the semantics of limited-with
4814 -- context clauses we don't install the limited view because the full
4815 -- view of X supersedes its limited view.
4817 if Analyzed (P_Unit)
4818 and then
4819 (Is_Immediately_Visible (P)
4820 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
4821 then
4823 -- The presence of both the limited and the analyzed nonlimited view
4824 -- may also be an error, such as an illegal context for a limited
4825 -- with_clause. In that case, do not process the context item at all.
4827 if Error_Posted (N) then
4828 return;
4829 end if;
4831 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
4832 declare
4833 Item : Node_Id;
4834 begin
4835 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
4836 while Present (Item) loop
4837 if Nkind (Item) = N_With_Clause
4838 and then Comes_From_Source (Item)
4839 and then Entity (Name (Item)) = P
4840 then
4841 return;
4842 end if;
4844 Next (Item);
4845 end loop;
4846 end;
4848 -- If this is a child body, assume that the nonlimited with_clause
4849 -- appears in an ancestor. Could be refined ???
4851 if Is_Child_Unit
4852 (Defining_Entity
4853 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
4854 then
4855 return;
4856 end if;
4858 else
4860 -- If in package declaration, nonlimited view brought in from
4861 -- parent unit or some error condition.
4863 return;
4864 end if;
4865 end if;
4867 if Debug_Flag_I then
4868 Write_Str ("install limited view of ");
4869 Write_Name (Chars (P));
4870 Write_Eol;
4871 end if;
4873 -- If the unit has not been analyzed and the limited view has not been
4874 -- already installed then we install it.
4876 if not Analyzed (P_Unit) then
4877 if not In_Chain (P) then
4879 -- Minimum decoration
4881 Set_Ekind (P, E_Package);
4882 Set_Etype (P, Standard_Void_Type);
4883 Set_Scope (P, Standard_Standard);
4884 Set_Is_Visible_Lib_Unit (P);
4886 if Is_Child_Package then
4887 Set_Is_Child_Unit (P);
4888 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4889 end if;
4891 -- Place entity on visibility structure
4893 Set_Homonym (P, Current_Entity (P));
4894 Set_Current_Entity (P);
4896 if Debug_Flag_I then
4897 Write_Str (" (homonym) chain ");
4898 Write_Name (Chars (P));
4899 Write_Eol;
4900 end if;
4902 -- Install the incomplete view. The first element of the limited
4903 -- view is a header (an E_Package entity) used to reference the
4904 -- first shadow entity in the private part of the package.
4906 Lim_Header := Limited_View (P);
4907 Lim_Typ := First_Entity (Lim_Header);
4909 while Present (Lim_Typ)
4910 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4911 loop
4912 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4913 Set_Current_Entity (Lim_Typ);
4915 if Debug_Flag_I then
4916 Write_Str (" (homonym) chain ");
4917 Write_Name (Chars (Lim_Typ));
4918 Write_Eol;
4919 end if;
4921 Next_Entity (Lim_Typ);
4922 end loop;
4923 end if;
4925 -- If the unit appears in a previous regular with_clause, the regular
4926 -- entities of the public part of the withed package must be replaced
4927 -- by the shadow ones.
4929 -- This code must be kept synchronized with the code that replaces the
4930 -- shadow entities by the real entities (see body of Remove_Limited
4931 -- With_Clause); otherwise the contents of the homonym chains are not
4932 -- consistent.
4934 else
4935 -- Hide all the type entities of the public part of the package to
4936 -- avoid its usage. This is needed to cover all the subtype decla-
4937 -- rations because we do not remove them from the homonym chain.
4939 E := First_Entity (P);
4940 while Present (E) and then E /= First_Private_Entity (P) loop
4941 if Is_Type (E) then
4942 Set_Was_Hidden (E, Is_Hidden (E));
4943 Set_Is_Hidden (E);
4944 end if;
4946 Next_Entity (E);
4947 end loop;
4949 -- Replace the real entities by the shadow entities of the limited
4950 -- view. The first element of the limited view is a header that is
4951 -- used to reference the first shadow entity in the private part
4952 -- of the package. Successive elements are the limited views of the
4953 -- type (including regular incomplete types) declared in the package.
4955 Lim_Header := Limited_View (P);
4957 Lim_Typ := First_Entity (Lim_Header);
4958 while Present (Lim_Typ)
4959 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4960 loop
4961 pragma Assert (not In_Chain (Lim_Typ));
4963 -- Do not unchain nested packages and child units
4965 if Ekind (Lim_Typ) /= E_Package
4966 and then not Is_Child_Unit (Lim_Typ)
4967 then
4968 declare
4969 Prev : Entity_Id;
4971 begin
4972 Prev := Current_Entity (Lim_Typ);
4973 E := Prev;
4975 -- Replace E in the homonyms list, so that the limited view
4976 -- becomes available.
4978 -- If the non-limited view is a record with an anonymous
4979 -- self-referential component, the analysis of the record
4980 -- declaration creates an incomplete type with the same name
4981 -- in order to define an internal access type. The visible
4982 -- entity is now the incomplete type, and that is the one to
4983 -- replace in the visibility structure.
4985 if E = Non_Limited_View (Lim_Typ)
4986 or else
4987 (Ekind (E) = E_Incomplete_Type
4988 and then Full_View (E) = Non_Limited_View (Lim_Typ))
4989 then
4990 Set_Homonym (Lim_Typ, Homonym (Prev));
4991 Set_Current_Entity (Lim_Typ);
4993 else
4994 loop
4995 E := Homonym (Prev);
4997 -- E may have been removed when installing a previous
4998 -- limited_with_clause.
5000 exit when No (E);
5001 exit when E = Non_Limited_View (Lim_Typ);
5002 Prev := Homonym (Prev);
5003 end loop;
5005 if Present (E) then
5006 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
5007 Set_Homonym (Prev, Lim_Typ);
5008 end if;
5009 end if;
5010 end;
5012 if Debug_Flag_I then
5013 Write_Str (" (homonym) chain ");
5014 Write_Name (Chars (Lim_Typ));
5015 Write_Eol;
5016 end if;
5017 end if;
5019 Next_Entity (Lim_Typ);
5020 end loop;
5021 end if;
5023 -- The package must be visible while the limited-with clause is active
5024 -- because references to the type P.T must resolve in the usual way.
5025 -- In addition, we remember that the limited-view has been installed to
5026 -- uninstall it at the point of context removal.
5028 Set_Is_Immediately_Visible (P);
5029 Set_Limited_View_Installed (N);
5031 -- If unit has not been analyzed in some previous context, check
5032 -- (imperfectly ???) whether it might need a body.
5034 if not Analyzed (P_Unit) then
5035 Check_Body_Required;
5036 end if;
5038 -- If the package in the limited_with clause is a child unit, the clause
5039 -- is unanalyzed and appears as a selected component. Recast it as an
5040 -- expanded name so that the entity can be properly set. Use entity of
5041 -- parent, if available, for higher ancestors in the name.
5043 if Nkind (Name (N)) = N_Selected_Component then
5044 declare
5045 Nam : Node_Id;
5046 Ent : Entity_Id;
5048 begin
5049 Nam := Name (N);
5050 Ent := P;
5051 while Nkind (Nam) = N_Selected_Component
5052 and then Present (Ent)
5053 loop
5054 Change_Selected_Component_To_Expanded_Name (Nam);
5056 -- Set entity of parent identifiers if the unit is a child
5057 -- unit. This ensures that the tree is properly formed from
5058 -- semantic point of view (e.g. for ASIS queries). The unit
5059 -- entities are not fully analyzed, so we need to follow unit
5060 -- links in the tree.
5062 Set_Entity (Nam, Ent);
5064 Nam := Prefix (Nam);
5065 Ent :=
5066 Defining_Entity
5067 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5069 -- Set entity of last ancestor
5071 if Nkind (Nam) = N_Identifier then
5072 Set_Entity (Nam, Ent);
5073 end if;
5074 end loop;
5075 end;
5076 end if;
5078 Set_Entity (Name (N), P);
5079 Set_From_Limited_With (P);
5080 end Install_Limited_Withed_Unit;
5082 -------------------------
5083 -- Install_Withed_Unit --
5084 -------------------------
5086 procedure Install_Withed_Unit
5087 (With_Clause : Node_Id;
5088 Private_With_OK : Boolean := False)
5090 Uname : constant Entity_Id := Entity (Name (With_Clause));
5091 P : constant Entity_Id := Scope (Uname);
5093 begin
5094 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5095 -- compiling a package declaration and the Private_With_OK flag was not
5096 -- set by the caller. These declarations will be installed later (before
5097 -- analyzing the private part of the package).
5099 if Private_Present (With_Clause)
5100 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
5101 and then not (Private_With_OK)
5102 then
5103 return;
5104 end if;
5106 if Debug_Flag_I then
5107 if Private_Present (With_Clause) then
5108 Write_Str ("install private withed unit ");
5109 else
5110 Write_Str ("install withed unit ");
5111 end if;
5113 Write_Name (Chars (Uname));
5114 Write_Eol;
5115 end if;
5117 -- We do not apply the restrictions to an internal unit unless we are
5118 -- compiling the internal unit as a main unit. This check is also
5119 -- skipped for dummy units (for missing packages).
5121 if Sloc (Uname) /= No_Location
5122 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
5123 or else Current_Sem_Unit = Main_Unit)
5124 then
5125 Check_Restricted_Unit
5126 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5127 end if;
5129 if P /= Standard_Standard then
5131 -- If the unit is not analyzed after analysis of the with clause and
5132 -- it is an instantiation then it awaits a body and is the main unit.
5133 -- Its appearance in the context of some other unit indicates a
5134 -- circular dependency (DEC suite perversity).
5136 if not Analyzed (Uname)
5137 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5138 then
5139 Error_Msg_N
5140 ("instantiation depends on itself", Name (With_Clause));
5142 elsif not Is_Visible_Lib_Unit (Uname) then
5144 -- Abandon processing in case of previous errors
5146 if No (Scope (Uname)) then
5147 Check_Error_Detected;
5148 return;
5149 end if;
5151 Set_Is_Visible_Lib_Unit (Uname);
5153 -- If the child unit appears in the context of its parent, it is
5154 -- immediately visible.
5156 if In_Open_Scopes (Scope (Uname)) then
5157 Set_Is_Immediately_Visible (Uname);
5158 end if;
5160 if Is_Generic_Instance (Uname)
5161 and then Ekind (Uname) in Subprogram_Kind
5162 then
5163 -- Set flag as well on the visible entity that denotes the
5164 -- instance, which renames the current one.
5166 Set_Is_Visible_Lib_Unit
5167 (Related_Instance
5168 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5169 end if;
5171 -- The parent unit may have been installed already, and may have
5172 -- appeared in a use clause.
5174 if In_Use (Scope (Uname)) then
5175 Set_Is_Potentially_Use_Visible (Uname);
5176 end if;
5178 Set_Context_Installed (With_Clause);
5179 end if;
5181 elsif not Is_Immediately_Visible (Uname) then
5182 Set_Is_Visible_Lib_Unit (Uname);
5184 if not Private_Present (With_Clause) or else Private_With_OK then
5185 Set_Is_Immediately_Visible (Uname);
5186 end if;
5188 Set_Context_Installed (With_Clause);
5189 end if;
5191 -- A with-clause overrides a with-type clause: there are no restric-
5192 -- tions on the use of package entities.
5194 if Ekind (Uname) = E_Package then
5195 Set_From_Limited_With (Uname, False);
5196 end if;
5198 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5199 -- unit if there is a visible homograph for it declared in the same
5200 -- declarative region. This pathological case can only arise when an
5201 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5202 -- G1 has a generic child also named G2, and the context includes with_
5203 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5204 -- of I1.G2 visible as well. If the child unit is named Standard, do
5205 -- not apply the check to the Standard package itself.
5207 if Is_Child_Unit (Uname)
5208 and then Is_Visible_Lib_Unit (Uname)
5209 and then Ada_Version >= Ada_2005
5210 then
5211 declare
5212 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5213 Decl2 : Node_Id;
5214 P2 : Entity_Id;
5215 U2 : Entity_Id;
5217 begin
5218 U2 := Homonym (Uname);
5219 while Present (U2) and then U2 /= Standard_Standard loop
5220 P2 := Scope (U2);
5221 Decl2 := Unit_Declaration_Node (P2);
5223 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5224 if Is_Generic_Instance (P)
5225 and then Nkind (Decl1) = N_Package_Declaration
5226 and then Generic_Parent (Specification (Decl1)) = P2
5227 then
5228 Error_Msg_N ("illegal with_clause", With_Clause);
5229 Error_Msg_N
5230 ("\child unit has visible homograph" &
5231 " (RM 8.3(26), 10.1.1(19))",
5232 With_Clause);
5233 exit;
5235 elsif Is_Generic_Instance (P2)
5236 and then Nkind (Decl2) = N_Package_Declaration
5237 and then Generic_Parent (Specification (Decl2)) = P
5238 then
5239 -- With_clause for child unit of instance appears before
5240 -- in the context. We want to place the error message on
5241 -- it, not on the generic child unit itself.
5243 declare
5244 Prev_Clause : Node_Id;
5246 begin
5247 Prev_Clause := First (List_Containing (With_Clause));
5248 while Entity (Name (Prev_Clause)) /= U2 loop
5249 Next (Prev_Clause);
5250 end loop;
5252 pragma Assert (Present (Prev_Clause));
5253 Error_Msg_N ("illegal with_clause", Prev_Clause);
5254 Error_Msg_N
5255 ("\child unit has visible homograph" &
5256 " (RM 8.3(26), 10.1.1(19))",
5257 Prev_Clause);
5258 exit;
5259 end;
5260 end if;
5261 end if;
5263 U2 := Homonym (U2);
5264 end loop;
5265 end;
5266 end if;
5267 end Install_Withed_Unit;
5269 -------------------
5270 -- Is_Child_Spec --
5271 -------------------
5273 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5274 K : constant Node_Kind := Nkind (Lib_Unit);
5276 begin
5277 return (K in N_Generic_Declaration or else
5278 K in N_Generic_Instantiation or else
5279 K in N_Generic_Renaming_Declaration or else
5280 K = N_Package_Declaration or else
5281 K = N_Package_Renaming_Declaration or else
5282 K = N_Subprogram_Declaration or else
5283 K = N_Subprogram_Renaming_Declaration)
5284 and then Present (Parent_Spec (Lib_Unit));
5285 end Is_Child_Spec;
5287 ------------------------------------
5288 -- Is_Legal_Shadow_Entity_In_Body --
5289 ------------------------------------
5291 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5292 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5293 begin
5294 return Nkind (Unit (C_Unit)) = N_Package_Body
5295 and then
5296 Has_With_Clause
5297 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5298 end Is_Legal_Shadow_Entity_In_Body;
5300 ----------------------
5301 -- Is_Ancestor_Unit --
5302 ----------------------
5304 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5305 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5306 E2 : Entity_Id;
5307 begin
5308 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
5309 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5310 return Is_Ancestor_Package (E1, E2);
5311 else
5312 return False;
5313 end if;
5314 end Is_Ancestor_Unit;
5316 -----------------------
5317 -- Load_Needed_Body --
5318 -----------------------
5320 -- N is a generic unit named in a with clause, or else it is a unit that
5321 -- contains a generic unit or an inlined function. In order to perform an
5322 -- instantiation, the body of the unit must be present. If the unit itself
5323 -- is generic, we assume that an instantiation follows, and load & analyze
5324 -- the body unconditionally. This forces analysis of the spec as well.
5326 -- If the unit is not generic, but contains a generic unit, it is loaded on
5327 -- demand, at the point of instantiation (see ch12).
5329 procedure Load_Needed_Body
5330 (N : Node_Id;
5331 OK : out Boolean;
5332 Do_Analyze : Boolean := True)
5334 Body_Name : Unit_Name_Type;
5335 Unum : Unit_Number_Type;
5337 Save_Style_Check : constant Boolean := Opt.Style_Check;
5338 -- The loading and analysis is done with style checks off
5340 begin
5341 if not GNAT_Mode then
5342 Style_Check := False;
5343 end if;
5345 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5346 Unum :=
5347 Load_Unit
5348 (Load_Name => Body_Name,
5349 Required => False,
5350 Subunit => False,
5351 Error_Node => N,
5352 Renamings => True);
5354 if Unum = No_Unit then
5355 OK := False;
5357 else
5358 Compiler_State := Analyzing; -- reset after load
5360 if not Fatal_Error (Unum) or else Try_Semantics then
5361 if Debug_Flag_L then
5362 Write_Str ("*** Loaded generic body");
5363 Write_Eol;
5364 end if;
5366 if Do_Analyze then
5367 Semantics (Cunit (Unum));
5368 end if;
5369 end if;
5371 OK := True;
5372 end if;
5374 Style_Check := Save_Style_Check;
5375 end Load_Needed_Body;
5377 -------------------------
5378 -- Build_Limited_Views --
5379 -------------------------
5381 procedure Build_Limited_Views (N : Node_Id) is
5382 Nam : constant Node_Id := Name (N);
5383 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
5384 Pack : constant Entity_Id := Cunit_Entity (Unum);
5386 Shadow_Pack : Entity_Id;
5387 -- The corresponding shadow entity of the withed package. This entity
5388 -- offers incomplete views of all types and visible packages declared
5389 -- within.
5391 Last_Shadow : Entity_Id := Empty;
5392 -- The last shadow entity created by routine Build_Shadow_Entity
5394 function Build_Shadow_Entity
5395 (Ent : Entity_Id;
5396 Scop : Entity_Id;
5397 Is_Tagged : Boolean := False) return Entity_Id;
5398 -- Create a shadow entity that hides Ent and offers an incomplete view
5399 -- of Ent. Scop is the proper scope. Flag Is_Tagged should be set when
5400 -- Ent is a tagged type. The generated entity is added to Lim_Header.
5401 -- This routine updates the value of Last_Shadow.
5403 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
5404 -- Perform minimal decoration of a package or its corresponding shadow
5405 -- entity denoted by Ent. Scop is the proper scope.
5407 procedure Decorate_Type
5408 (Ent : Entity_Id;
5409 Scop : Entity_Id;
5410 Is_Tagged : Boolean := False;
5411 Materialize : Boolean := False);
5412 -- Perform minimal decoration of a type or its corresponding shadow
5413 -- entity denoted by Ent. Scop is the proper scope. Flag Is_Tagged
5414 -- should be set when Ent is a tagged type. Flag Materialize should be
5415 -- set when Ent is a tagged type and its class-wide type needs to appear
5416 -- in the tree.
5418 procedure Process_Declarations (Decls : List_Id; Scop : Entity_Id);
5419 -- Inspect declarative list Decls and create shadow entities for all
5420 -- types and packages encountered. Scop is the proper scope.
5422 -------------------------
5423 -- Build_Shadow_Entity --
5424 -------------------------
5426 function Build_Shadow_Entity
5427 (Ent : Entity_Id;
5428 Scop : Entity_Id;
5429 Is_Tagged : Boolean := False) return Entity_Id
5431 Shadow : constant Entity_Id := Make_Temporary (Sloc (Ent), 'Z');
5433 begin
5434 -- The shadow entity must share the same name and parent as the
5435 -- entity it hides.
5437 Set_Chars (Shadow, Chars (Ent));
5438 Set_Parent (Shadow, Parent (Ent));
5439 Set_Ekind (Shadow, Ekind (Ent));
5440 Set_Is_Internal (Shadow);
5441 Set_From_Limited_With (Shadow);
5443 -- Add the new shadow entity to the limited view of the package
5445 Last_Shadow := Shadow;
5446 Append_Entity (Shadow, Shadow_Pack);
5448 if Is_Type (Ent) then
5449 Decorate_Type (Shadow, Scop, Is_Tagged);
5451 if Is_Incomplete_Or_Private_Type (Ent) then
5452 Set_Private_Dependents (Shadow, New_Elmt_List);
5453 end if;
5455 Set_Non_Limited_View (Shadow, Ent);
5457 elsif Ekind (Ent) = E_Package then
5458 Decorate_Package (Shadow, Scop);
5459 end if;
5461 return Shadow;
5462 end Build_Shadow_Entity;
5464 ----------------------
5465 -- Decorate_Package --
5466 ----------------------
5468 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
5469 begin
5470 Set_Ekind (Ent, E_Package);
5471 Set_Etype (Ent, Standard_Void_Type);
5472 Set_Scope (Ent, Scop);
5473 end Decorate_Package;
5475 -------------------
5476 -- Decorate_Type --
5477 -------------------
5479 procedure Decorate_Type
5480 (Ent : Entity_Id;
5481 Scop : Entity_Id;
5482 Is_Tagged : Boolean := False;
5483 Materialize : Boolean := False)
5485 CW_Typ : Entity_Id;
5487 begin
5488 -- An unanalyzed type or a shadow entity of a type is treated as an
5489 -- incomplete type.
5491 Set_Ekind (Ent, E_Incomplete_Type);
5492 Set_Etype (Ent, Ent);
5493 Set_Scope (Ent, Scop);
5494 Set_Is_First_Subtype (Ent);
5495 Set_Stored_Constraint (Ent, No_Elist);
5496 Set_Full_View (Ent, Empty);
5497 Init_Size_Align (Ent);
5499 -- A tagged type and its corresponding shadow entity share one common
5500 -- class-wide type.
5502 if Is_Tagged then
5503 Set_Is_Tagged_Type (Ent);
5505 if No (Class_Wide_Type (Ent)) then
5506 CW_Typ :=
5507 New_External_Entity
5508 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
5510 Set_Class_Wide_Type (Ent, CW_Typ);
5512 -- Set parent to be the same as the parent of the tagged type.
5513 -- We need a parent field set, and it is supposed to point to
5514 -- the declaration of the type. The tagged type declaration
5515 -- essentially declares two separate types, the tagged type
5516 -- itself and the corresponding class-wide type, so it is
5517 -- reasonable for the parent fields to point to the declaration
5518 -- in both cases.
5520 Set_Parent (CW_Typ, Parent (Ent));
5522 Set_Ekind (CW_Typ, E_Class_Wide_Type);
5523 Set_Etype (CW_Typ, Ent);
5524 Set_Scope (CW_Typ, Scop);
5525 Set_Is_Tagged_Type (CW_Typ);
5526 Set_Is_First_Subtype (CW_Typ);
5527 Init_Size_Align (CW_Typ);
5528 Set_Has_Unknown_Discriminants (CW_Typ);
5529 Set_Class_Wide_Type (CW_Typ, CW_Typ);
5530 Set_Equivalent_Type (CW_Typ, Empty);
5531 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
5532 Set_Materialize_Entity (CW_Typ, Materialize);
5533 end if;
5534 end if;
5535 end Decorate_Type;
5537 --------------------------
5538 -- Process_Declarations --
5539 --------------------------
5541 procedure Process_Declarations (Decls : List_Id; Scop : Entity_Id) is
5542 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
5543 Is_Tagged : Boolean;
5544 Decl : Node_Id;
5545 Def : Node_Id;
5546 Pack : Entity_Id;
5547 Shadow : Entity_Id;
5548 Typ : Entity_Id;
5550 begin
5551 -- Inspect the declarative list, looking for type declarations and
5552 -- nested packages.
5554 Decl := First (Decls);
5555 while Present (Decl) loop
5557 -- Types
5559 if Nkind_In (Decl, N_Full_Type_Declaration,
5560 N_Incomplete_Type_Declaration,
5561 N_Private_Extension_Declaration,
5562 N_Private_Type_Declaration,
5563 N_Protected_Type_Declaration,
5564 N_Task_Type_Declaration)
5565 then
5566 Typ := Defining_Entity (Decl);
5568 -- Determine whether the type is tagged. Note that packages
5569 -- included via a limited with clause are not always analyzed,
5570 -- hence the tree lookup rather than the use of attribute
5571 -- Is_Tagged_Type.
5573 if Nkind (Decl) = N_Full_Type_Declaration then
5574 Def := Type_Definition (Decl);
5576 Is_Tagged :=
5577 (Nkind (Def) = N_Record_Definition
5578 and then Tagged_Present (Def))
5579 or else
5580 (Nkind (Def) = N_Derived_Type_Definition
5581 and then Present (Record_Extension_Part (Def)));
5583 elsif Nkind_In (Decl, N_Incomplete_Type_Declaration,
5584 N_Private_Type_Declaration)
5585 then
5586 Is_Tagged := Tagged_Present (Decl);
5588 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5589 Is_Tagged := True;
5591 else
5592 Is_Tagged := False;
5593 end if;
5595 -- Perform minor decoration when the withed package has not
5596 -- been analyzed.
5598 if not Is_Analyzed then
5599 Decorate_Type (Typ, Scop, Is_Tagged, True);
5600 end if;
5602 -- Create a shadow entity that hides the type and offers an
5603 -- incomplete view of the said type.
5605 Shadow := Build_Shadow_Entity (Typ, Scop, Is_Tagged);
5607 -- Packages
5609 elsif Nkind (Decl) = N_Package_Declaration then
5610 Pack := Defining_Entity (Decl);
5612 -- Perform minor decoration when the withed package has not
5613 -- been analyzed.
5615 if not Is_Analyzed then
5616 Decorate_Package (Pack, Scop);
5617 end if;
5619 -- Create a shadow entity that offers a limited view of all
5620 -- visible types declared within.
5622 Shadow := Build_Shadow_Entity (Pack, Scop);
5624 Process_Declarations
5625 (Decls => Visible_Declarations (Specification (Decl)),
5626 Scop => Shadow);
5627 end if;
5629 Next (Decl);
5630 end loop;
5631 end Process_Declarations;
5633 -- Local variables
5635 Last_Public_Shadow : Entity_Id := Empty;
5636 Private_Shadow : Entity_Id;
5637 Spec : Node_Id;
5639 -- Start of processing for Build_Limited_Views
5641 begin
5642 pragma Assert (Limited_Present (N));
5644 -- A library_item mentioned in a limited_with_clause is a package
5645 -- declaration, not a subprogram declaration, generic declaration,
5646 -- generic instantiation, or package renaming declaration.
5648 case Nkind (Unit (Library_Unit (N))) is
5649 when N_Package_Declaration =>
5650 null;
5652 when N_Subprogram_Declaration =>
5653 Error_Msg_N ("subprograms not allowed in limited with_clauses", N);
5654 return;
5656 when N_Generic_Package_Declaration |
5657 N_Generic_Subprogram_Declaration =>
5658 Error_Msg_N ("generics not allowed in limited with_clauses", N);
5659 return;
5661 when N_Generic_Instantiation =>
5662 Error_Msg_N
5663 ("generic instantiations not allowed in limited with_clauses",
5665 return;
5667 when N_Generic_Renaming_Declaration =>
5668 Error_Msg_N
5669 ("generic renamings not allowed in limited with_clauses", N);
5670 return;
5672 when N_Subprogram_Renaming_Declaration =>
5673 Error_Msg_N
5674 ("renamed subprograms not allowed in limited with_clauses", N);
5675 return;
5677 when N_Package_Renaming_Declaration =>
5678 Error_Msg_N
5679 ("renamed packages not allowed in limited with_clauses", N);
5680 return;
5682 when others =>
5683 raise Program_Error;
5684 end case;
5686 -- The withed unit may not be analyzed, but the with calause itself
5687 -- must be minimally decorated. This ensures that the checks on unused
5688 -- with clauses also process limieted withs.
5690 Set_Ekind (Pack, E_Package);
5691 Set_Etype (Pack, Standard_Void_Type);
5693 if Is_Entity_Name (Nam) then
5694 Set_Entity (Nam, Pack);
5696 elsif Nkind (Nam) = N_Selected_Component then
5697 Set_Entity (Selector_Name (Nam), Pack);
5698 end if;
5700 -- Check if the chain is already built
5702 Spec := Specification (Unit (Library_Unit (N)));
5704 if Limited_View_Installed (Spec) then
5705 return;
5706 end if;
5708 -- Create the shadow package wich hides the withed unit and provides
5709 -- incomplete view of all types and packages declared within.
5711 Shadow_Pack := Make_Temporary (Sloc (N), 'Z');
5712 Set_Ekind (Shadow_Pack, E_Package);
5713 Set_Is_Internal (Shadow_Pack);
5714 Set_Limited_View (Pack, Shadow_Pack);
5716 -- Inspect the visible declarations of the withed unit and create shadow
5717 -- entities that hide existing types and packages.
5719 Process_Declarations
5720 (Decls => Visible_Declarations (Spec),
5721 Scop => Pack);
5723 Last_Public_Shadow := Last_Shadow;
5725 -- Ada 2005 (AI-262): Build the limited view of the private declarations
5726 -- to accomodate limited private with clauses.
5728 Process_Declarations
5729 (Decls => Private_Declarations (Spec),
5730 Scop => Pack);
5732 if Present (Last_Public_Shadow) then
5733 Private_Shadow := Next_Entity (Last_Public_Shadow);
5734 else
5735 Private_Shadow := First_Entity (Shadow_Pack);
5736 end if;
5738 Set_First_Private_Entity (Shadow_Pack, Private_Shadow);
5739 Set_Limited_View_Installed (Spec);
5740 end Build_Limited_Views;
5742 -------------------------------
5743 -- Check_Body_Needed_For_SAL --
5744 -------------------------------
5746 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5748 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5749 -- Determine whether use of entity E might require the presence of its
5750 -- body. For a package this requires a recursive traversal of all nested
5751 -- declarations.
5753 ---------------------------
5754 -- Entity_Needed_For_SAL --
5755 ---------------------------
5757 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5758 Ent : Entity_Id;
5760 begin
5761 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
5762 return True;
5764 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
5765 return True;
5767 elsif Ekind (E) = E_Generic_Package
5768 and then
5769 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5770 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5771 then
5772 return True;
5774 elsif Ekind (E) = E_Package
5775 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5776 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5777 then
5778 Ent := First_Entity (E);
5779 while Present (Ent) loop
5780 if Entity_Needs_Body (Ent) then
5781 return True;
5782 end if;
5784 Next_Entity (Ent);
5785 end loop;
5787 return False;
5789 else
5790 return False;
5791 end if;
5792 end Entity_Needs_Body;
5794 -- Start of processing for Check_Body_Needed_For_SAL
5796 begin
5797 if Ekind (Unit_Name) = E_Generic_Package
5798 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5799 N_Generic_Package_Declaration
5800 and then
5801 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5802 then
5803 Set_Body_Needed_For_SAL (Unit_Name);
5805 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
5806 Set_Body_Needed_For_SAL (Unit_Name);
5808 elsif Is_Subprogram (Unit_Name)
5809 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5810 N_Subprogram_Declaration
5811 and then Has_Pragma_Inline (Unit_Name)
5812 then
5813 Set_Body_Needed_For_SAL (Unit_Name);
5815 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5816 Check_Body_Needed_For_SAL
5817 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5819 elsif Ekind (Unit_Name) = E_Package
5820 and then Entity_Needs_Body (Unit_Name)
5821 then
5822 Set_Body_Needed_For_SAL (Unit_Name);
5824 elsif Ekind (Unit_Name) = E_Package_Body
5825 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5826 then
5827 Check_Body_Needed_For_SAL
5828 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5829 end if;
5830 end Check_Body_Needed_For_SAL;
5832 --------------------
5833 -- Remove_Context --
5834 --------------------
5836 procedure Remove_Context (N : Node_Id) is
5837 Lib_Unit : constant Node_Id := Unit (N);
5839 begin
5840 -- If this is a child unit, first remove the parent units
5842 if Is_Child_Spec (Lib_Unit) then
5843 Remove_Parents (Lib_Unit);
5844 end if;
5846 Remove_Context_Clauses (N);
5847 end Remove_Context;
5849 ----------------------------
5850 -- Remove_Context_Clauses --
5851 ----------------------------
5853 procedure Remove_Context_Clauses (N : Node_Id) is
5854 Item : Node_Id;
5855 Unit_Name : Entity_Id;
5857 begin
5858 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5859 -- limited-views first and regular-views later (to maintain the
5860 -- stack model).
5862 -- First Phase: Remove limited_with context clauses
5864 Item := First (Context_Items (N));
5865 while Present (Item) loop
5867 -- We are interested only in with clauses which got installed
5868 -- on entry.
5870 if Nkind (Item) = N_With_Clause
5871 and then Limited_Present (Item)
5872 and then Limited_View_Installed (Item)
5873 then
5874 Remove_Limited_With_Clause (Item);
5875 end if;
5877 Next (Item);
5878 end loop;
5880 -- Second Phase: Loop through context items and undo regular
5881 -- with_clauses and use_clauses.
5883 Item := First (Context_Items (N));
5884 while Present (Item) loop
5886 -- We are interested only in with clauses which got installed on
5887 -- entry, as indicated by their Context_Installed flag set
5889 if Nkind (Item) = N_With_Clause
5890 and then Limited_Present (Item)
5891 and then Limited_View_Installed (Item)
5892 then
5893 null;
5895 elsif Nkind (Item) = N_With_Clause
5896 and then Context_Installed (Item)
5897 then
5898 -- Remove items from one with'ed unit
5900 Unit_Name := Entity (Name (Item));
5901 Remove_Unit_From_Visibility (Unit_Name);
5902 Set_Context_Installed (Item, False);
5904 elsif Nkind (Item) = N_Use_Package_Clause then
5905 End_Use_Package (Item);
5907 elsif Nkind (Item) = N_Use_Type_Clause then
5908 End_Use_Type (Item);
5909 end if;
5911 Next (Item);
5912 end loop;
5913 end Remove_Context_Clauses;
5915 --------------------------------
5916 -- Remove_Limited_With_Clause --
5917 --------------------------------
5919 procedure Remove_Limited_With_Clause (N : Node_Id) is
5920 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5921 E : Entity_Id;
5922 P : Entity_Id;
5923 Lim_Header : Entity_Id;
5924 Lim_Typ : Entity_Id;
5925 Prev : Entity_Id;
5927 begin
5928 pragma Assert (Limited_View_Installed (N));
5930 -- In case of limited with_clause on subprograms, generics, instances,
5931 -- or renamings, the corresponding error was previously posted and we
5932 -- have nothing to do here.
5934 if Nkind (P_Unit) /= N_Package_Declaration then
5935 return;
5936 end if;
5938 P := Defining_Unit_Name (Specification (P_Unit));
5940 -- Handle child packages
5942 if Nkind (P) = N_Defining_Program_Unit_Name then
5943 P := Defining_Identifier (P);
5944 end if;
5946 if Debug_Flag_I then
5947 Write_Str ("remove limited view of ");
5948 Write_Name (Chars (P));
5949 Write_Str (" from visibility");
5950 Write_Eol;
5951 end if;
5953 -- Prepare the removal of the shadow entities from visibility. The first
5954 -- element of the limited view is a header (an E_Package entity) that is
5955 -- used to reference the first shadow entity in the private part of the
5956 -- package
5958 Lim_Header := Limited_View (P);
5959 Lim_Typ := First_Entity (Lim_Header);
5961 -- Remove package and shadow entities from visibility if it has not
5962 -- been analyzed
5964 if not Analyzed (P_Unit) then
5965 Unchain (P);
5966 Set_Is_Immediately_Visible (P, False);
5968 while Present (Lim_Typ) loop
5969 Unchain (Lim_Typ);
5970 Next_Entity (Lim_Typ);
5971 end loop;
5973 -- Otherwise this package has already appeared in the closure and its
5974 -- shadow entities must be replaced by its real entities. This code
5975 -- must be kept synchronized with the complementary code in Install
5976 -- Limited_Withed_Unit.
5978 else
5979 -- Real entities that are type or subtype declarations were hidden
5980 -- from visibility at the point of installation of the limited-view.
5981 -- Now we recover the previous value of the hidden attribute.
5983 E := First_Entity (P);
5984 while Present (E) and then E /= First_Private_Entity (P) loop
5985 if Is_Type (E) then
5986 Set_Is_Hidden (E, Was_Hidden (E));
5987 end if;
5989 Next_Entity (E);
5990 end loop;
5992 while Present (Lim_Typ)
5993 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5994 loop
5995 -- Nested packages and child units were not unchained
5997 if Ekind (Lim_Typ) /= E_Package
5998 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5999 then
6000 -- If the package has incomplete types, the limited view of the
6001 -- incomplete type is in fact never visible (AI05-129) but we
6002 -- have created a shadow entity E1 for it, that points to E2,
6003 -- a non-limited incomplete type. This in turn has a full view
6004 -- E3 that is the full declaration. There is a corresponding
6005 -- shadow entity E4. When reinstalling the non-limited view,
6006 -- E2 must become the current entity and E3 must be ignored.
6008 E := Non_Limited_View (Lim_Typ);
6010 if Present (Current_Entity (E))
6011 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
6012 and then Full_View (Current_Entity (E)) = E
6013 then
6015 -- Lim_Typ is the limited view of a full type declaration
6016 -- that has a previous incomplete declaration, i.e. E3 from
6017 -- the previous description. Nothing to insert.
6019 null;
6021 else
6022 pragma Assert (not In_Chain (E));
6024 Prev := Current_Entity (Lim_Typ);
6026 if Prev = Lim_Typ then
6027 Set_Current_Entity (E);
6029 else
6030 while Present (Prev)
6031 and then Homonym (Prev) /= Lim_Typ
6032 loop
6033 Prev := Homonym (Prev);
6034 end loop;
6036 if Present (Prev) then
6037 Set_Homonym (Prev, E);
6038 end if;
6039 end if;
6041 -- Preserve structure of homonym chain
6043 Set_Homonym (E, Homonym (Lim_Typ));
6044 end if;
6045 end if;
6047 Next_Entity (Lim_Typ);
6048 end loop;
6049 end if;
6051 -- Indicate that the limited view of the package is not installed
6053 Set_From_Limited_With (P, False);
6054 Set_Limited_View_Installed (N, False);
6055 end Remove_Limited_With_Clause;
6057 --------------------
6058 -- Remove_Parents --
6059 --------------------
6061 procedure Remove_Parents (Lib_Unit : Node_Id) is
6062 P : Node_Id;
6063 P_Name : Entity_Id;
6064 P_Spec : Node_Id := Empty;
6065 E : Entity_Id;
6066 Vis : constant Boolean :=
6067 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
6069 begin
6070 if Is_Child_Spec (Lib_Unit) then
6071 P_Spec := Parent_Spec (Lib_Unit);
6073 elsif Nkind (Lib_Unit) = N_Package_Body
6074 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6075 then
6076 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6077 end if;
6079 if Present (P_Spec) then
6080 P := Unit (P_Spec);
6081 P_Name := Get_Parent_Entity (P);
6082 Remove_Context_Clauses (P_Spec);
6083 End_Package_Scope (P_Name);
6084 Set_Is_Immediately_Visible (P_Name, Vis);
6086 -- Remove from visibility the siblings as well, which are directly
6087 -- visible while the parent is in scope.
6089 E := First_Entity (P_Name);
6090 while Present (E) loop
6091 if Is_Child_Unit (E) then
6092 Set_Is_Immediately_Visible (E, False);
6093 end if;
6095 Next_Entity (E);
6096 end loop;
6098 Set_In_Package_Body (P_Name, False);
6100 -- This is the recursive call to remove the context of any higher
6101 -- level parent. This recursion ensures that all parents are removed
6102 -- in the reverse order of their installation.
6104 Remove_Parents (P);
6105 end if;
6106 end Remove_Parents;
6108 ---------------------------------
6109 -- Remove_Private_With_Clauses --
6110 ---------------------------------
6112 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6113 Item : Node_Id;
6115 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6116 -- Check whether a given unit appears in a regular with_clause. Used to
6117 -- determine whether a private_with_clause, implicit or explicit, should
6118 -- be ignored.
6120 ----------------------------
6121 -- In_Regular_With_Clause --
6122 ----------------------------
6124 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6126 Item : Node_Id;
6128 begin
6129 Item := First (Context_Items (Comp_Unit));
6130 while Present (Item) loop
6131 if Nkind (Item) = N_With_Clause
6132 and then Entity (Name (Item)) = E
6133 and then not Private_Present (Item)
6134 then
6135 return True;
6136 end if;
6137 Next (Item);
6138 end loop;
6140 return False;
6141 end In_Regular_With_Clause;
6143 -- Start of processing for Remove_Private_With_Clauses
6145 begin
6146 Item := First (Context_Items (Comp_Unit));
6147 while Present (Item) loop
6148 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
6150 -- If private_with_clause is redundant, remove it from context,
6151 -- as a small optimization to subsequent handling of private_with
6152 -- clauses in other nested packages.
6154 if In_Regular_With_Clause (Entity (Name (Item))) then
6155 declare
6156 Nxt : constant Node_Id := Next (Item);
6157 begin
6158 Remove (Item);
6159 Item := Nxt;
6160 end;
6162 elsif Limited_Present (Item) then
6163 if not Limited_View_Installed (Item) then
6164 Remove_Limited_With_Clause (Item);
6165 end if;
6167 Next (Item);
6169 else
6170 Remove_Unit_From_Visibility (Entity (Name (Item)));
6171 Set_Context_Installed (Item, False);
6172 Next (Item);
6173 end if;
6175 else
6176 Next (Item);
6177 end if;
6178 end loop;
6179 end Remove_Private_With_Clauses;
6181 ---------------------------------
6182 -- Remove_Unit_From_Visibility --
6183 ---------------------------------
6185 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6186 begin
6187 if Debug_Flag_I then
6188 Write_Str ("remove unit ");
6189 Write_Name (Chars (Unit_Name));
6190 Write_Str (" from visibility");
6191 Write_Eol;
6192 end if;
6194 Set_Is_Visible_Lib_Unit (Unit_Name, False);
6195 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6196 Set_Is_Immediately_Visible (Unit_Name, False);
6198 -- If the unit is a wrapper package, the subprogram instance is
6199 -- what must be removed from visibility.
6201 if Is_Wrapper_Package (Unit_Name) then
6202 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
6203 end if;
6204 end Remove_Unit_From_Visibility;
6206 --------
6207 -- sm --
6208 --------
6210 procedure sm is
6211 begin
6212 null;
6213 end sm;
6215 -------------
6216 -- Unchain --
6217 -------------
6219 procedure Unchain (E : Entity_Id) is
6220 Prev : Entity_Id;
6222 begin
6223 Prev := Current_Entity (E);
6225 if No (Prev) then
6226 return;
6228 elsif Prev = E then
6229 Set_Name_Entity_Id (Chars (E), Homonym (E));
6231 else
6232 while Present (Prev) and then Homonym (Prev) /= E loop
6233 Prev := Homonym (Prev);
6234 end loop;
6236 if Present (Prev) then
6237 Set_Homonym (Prev, Homonym (E));
6238 end if;
6239 end if;
6241 if Debug_Flag_I then
6242 Write_Str (" (homonym) unchain ");
6243 Write_Name (Chars (E));
6244 Write_Eol;
6245 end if;
6246 end Unchain;
6248 end Sem_Ch10;