2013-05-03 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / sem_ch10.adb
blob98b0d5795aedef6af40abb238cda0f13bf743abb
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 Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Util; use Exp_Util;
31 with Elists; use Elists;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Inline; use Inline;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Output; use Output;
45 with Par_SCO; use Par_SCO;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Dist; use Sem_Dist;
56 with Sem_Prag; use Sem_Prag;
57 with Sem_Util; use Sem_Util;
58 with Sem_Warn; use Sem_Warn;
59 with Stand; use Stand;
60 with Sinfo; use Sinfo;
61 with Sinfo.CN; use Sinfo.CN;
62 with Sinput; use Sinput;
63 with Snames; use Snames;
64 with Style; use Style;
65 with Stylesw; use Stylesw;
66 with Tbuild; use Tbuild;
67 with Uname; use Uname;
69 package body Sem_Ch10 is
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
75 procedure Analyze_Context (N : Node_Id);
76 -- Analyzes items in the context clause of compilation unit
78 procedure Build_Limited_Views (N : Node_Id);
79 -- Build and decorate the list of shadow entities for a package mentioned
80 -- in a limited_with clause. If the package was not previously analyzed
81 -- then it also performs a basic decoration of the real entities. This is
82 -- required in order to avoid passing non-decorated entities to the
83 -- back-end. Implements Ada 2005 (AI-50217).
85 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
86 -- Check whether the source for the body of a compilation unit must be
87 -- included in a standalone library.
89 procedure Check_Private_Child_Unit (N : Node_Id);
90 -- If a with_clause mentions a private child unit, the compilation unit
91 -- must be a member of the same family, as described in 10.1.2.
93 procedure Check_Stub_Level (N : Node_Id);
94 -- Verify that a stub is declared immediately within a compilation unit,
95 -- and not in an inner frame.
97 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id);
98 -- When a child unit appears in a context clause, the implicit withs on
99 -- parents are made explicit, and with clauses are inserted in the context
100 -- clause before the one for the child. If a parent in the with_clause
101 -- is a renaming, the implicit with_clause is on the renaming whose name
102 -- is mentioned in the with_clause, and not on the package it renames.
103 -- N is the compilation unit whose list of context items receives the
104 -- implicit with_clauses.
106 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
107 -- Get defining entity of parent unit of a child unit. In most cases this
108 -- is the defining entity of the unit, but for a child instance whose
109 -- parent needs a body for inlining, the instantiation node of the parent
110 -- has not yet been rewritten as a package declaration, and the entity has
111 -- to be retrieved from the Instance_Spec of the unit.
113 function Has_With_Clause
114 (C_Unit : Node_Id;
115 Pack : Entity_Id;
116 Is_Limited : Boolean := False) return Boolean;
117 -- Determine whether compilation unit C_Unit contains a [limited] with
118 -- clause for package Pack. Use the flag Is_Limited to designate desired
119 -- clause kind.
121 procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
122 -- If the main unit is a child unit, implicit withs are also added for
123 -- all its ancestors.
125 function In_Chain (E : Entity_Id) return Boolean;
126 -- Check that the shadow entity is not already in the homonym chain, for
127 -- example through a limited_with clause in a parent unit.
129 procedure Install_Context_Clauses (N : Node_Id);
130 -- Subsidiary to Install_Context and Install_Parents. Process all with
131 -- and use clauses for current unit and its library unit if any.
133 procedure Install_Limited_Context_Clauses (N : Node_Id);
134 -- Subsidiary to Install_Context. Process only limited with_clauses for
135 -- current unit. Implements Ada 2005 (AI-50217).
137 procedure Install_Limited_Withed_Unit (N : Node_Id);
138 -- Place shadow entities for a limited_with package in the visibility
139 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
141 procedure Install_Withed_Unit
142 (With_Clause : Node_Id;
143 Private_With_OK : Boolean := False);
144 -- If the unit is not a child unit, make unit immediately visible. The
145 -- caller ensures that the unit is not already currently installed. The
146 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
147 -- is called when compiling the private part of a package, or installing
148 -- the private declarations of a parent unit.
150 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
151 -- This procedure establishes the context for the compilation of a child
152 -- unit. If Lib_Unit is a child library spec then the context of the parent
153 -- is installed, and the parent itself made immediately visible, so that
154 -- the child unit is processed in the declarative region of the parent.
155 -- Install_Parents makes a recursive call to itself to ensure that all
156 -- parents are loaded in the nested case. If Lib_Unit is a library body,
157 -- the only effect of Install_Parents is to install the private decls of
158 -- the parents, because the visible parent declarations will have been
159 -- installed as part of the context of the corresponding spec.
161 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
162 -- In the compilation of a child unit, a child of any of the ancestor
163 -- units is directly visible if it is visible, because the parent is in
164 -- an enclosing scope. Iterate over context to find child units of U_Name
165 -- or of some ancestor of it.
167 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean;
168 -- When compiling a unit Q descended from some parent unit P, a limited
169 -- with_clause in the context of P that names some other ancestor of Q
170 -- must not be installed because the ancestor is immediately visible.
172 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
173 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
174 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
175 -- a library spec that has a parent. If the call to Is_Child_Spec returns
176 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
177 -- compilation unit for the parent spec.
179 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
180 -- Parent_Spec is non-empty, this is also a child unit.
182 procedure Remove_Context_Clauses (N : Node_Id);
183 -- Subsidiary of previous one. Remove use_ and with_clauses
185 procedure Remove_Limited_With_Clause (N : Node_Id);
186 -- Remove from visibility the shadow entities introduced for a package
187 -- mentioned in a limited_with clause. Implements Ada 2005 (AI-50217).
189 procedure Remove_Parents (Lib_Unit : Node_Id);
190 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
191 -- contexts established by the corresponding call to Install_Parents are
192 -- removed. Remove_Parents contains a recursive call to itself to ensure
193 -- that all parents are removed in the nested case.
195 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
196 -- Reset all visibility flags on unit after compiling it, either as a main
197 -- unit or as a unit in the context.
199 procedure Unchain (E : Entity_Id);
200 -- Remove single entity from visibility list
202 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
203 -- Common processing for all stubs (subprograms, tasks, packages, and
204 -- protected cases). N is the stub to be analyzed. Once the subunit name
205 -- is established, load and analyze. Nam is the non-overloadable entity
206 -- for which the proper body provides a completion. Subprogram stubs are
207 -- handled differently because they can be declarations.
209 procedure sm;
210 -- A dummy procedure, for debugging use, called just before analyzing the
211 -- main unit (after dealing with any context clauses).
213 --------------------------
214 -- Limited_With_Clauses --
215 --------------------------
217 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
218 -- mutually recursive types declared in different units. A limited_with
219 -- clause that names package P in the context of unit U makes the types
220 -- declared in the visible part of P available within U, but with the
221 -- restriction that these types can only be used as incomplete types.
222 -- The limited_with clause does not impose a semantic dependence on P,
223 -- and it is possible for two packages to have limited_with_clauses on
224 -- each other without creating an elaboration circularity.
226 -- To support this feature, the analysis of a limited_with clause must
227 -- create an abbreviated view of the package, without performing any
228 -- semantic analysis on it. This "package abstract" contains shadow types
229 -- that are in one-one correspondence with the real types in the package,
230 -- and that have the properties of incomplete types.
232 -- The implementation creates two element lists: one to chain the shadow
233 -- entities, and one to chain the corresponding type entities in the tree
234 -- of the package. Links between corresponding entities in both chains
235 -- allow the compiler to select the proper view of a given type, depending
236 -- on the context. Note that in contrast with the handling of private
237 -- types, the limited view and the non-limited view of a type are treated
238 -- as separate entities, and no entity exchange needs to take place, which
239 -- makes the implementation must simpler than could be feared.
241 ------------------------------
242 -- Analyze_Compilation_Unit --
243 ------------------------------
245 procedure Analyze_Compilation_Unit (N : Node_Id) is
246 Unit_Node : constant Node_Id := Unit (N);
247 Lib_Unit : Node_Id := Library_Unit (N);
248 Spec_Id : Entity_Id;
249 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
250 Par_Spec_Name : Unit_Name_Type;
251 Unum : Unit_Number_Type;
253 procedure Check_Redundant_Withs
254 (Context_Items : List_Id;
255 Spec_Context_Items : List_Id := No_List);
256 -- Determine whether the context list of a compilation unit contains
257 -- redundant with clauses. When checking body clauses against spec
258 -- clauses, set Context_Items to the context list of the body and
259 -- Spec_Context_Items to that of the spec. Parent packages are not
260 -- examined for documentation purposes.
262 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
263 -- Generate cross-reference information for the parents of child units.
264 -- N is a defining_program_unit_name, and P_Id is the immediate parent.
266 ---------------------------
267 -- Check_Redundant_Withs --
268 ---------------------------
270 procedure Check_Redundant_Withs
271 (Context_Items : List_Id;
272 Spec_Context_Items : List_Id := No_List)
274 Clause : Node_Id;
276 procedure Process_Body_Clauses
277 (Context_List : List_Id;
278 Clause : Node_Id;
279 Used : in out Boolean;
280 Used_Type_Or_Elab : in out Boolean);
281 -- Examine the context clauses of a package body, trying to match the
282 -- name entity of Clause with any list element. If the match occurs
283 -- on a use package clause set Used to True, for a use type clause or
284 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
286 procedure Process_Spec_Clauses
287 (Context_List : List_Id;
288 Clause : Node_Id;
289 Used : in out Boolean;
290 Withed : in out Boolean;
291 Exit_On_Self : Boolean := False);
292 -- Examine the context clauses of a package spec, trying to match
293 -- the name entity of Clause with any list element. If the match
294 -- occurs on a use package clause, set Used to True, for a with
295 -- package clause other than Clause, set Withed to True. Limited
296 -- with clauses, implicitly generated with clauses and withs
297 -- having pragmas Elaborate or Elaborate_All applied to them are
298 -- skipped. Exit_On_Self is used to control the search loop and
299 -- force an exit whenever Clause sees itself in the search.
301 --------------------------
302 -- Process_Body_Clauses --
303 --------------------------
305 procedure Process_Body_Clauses
306 (Context_List : List_Id;
307 Clause : Node_Id;
308 Used : in out Boolean;
309 Used_Type_Or_Elab : in out Boolean)
311 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
312 Cont_Item : Node_Id;
313 Prag_Unit : Node_Id;
314 Subt_Mark : Node_Id;
315 Use_Item : Node_Id;
317 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
318 -- In an expanded name in a use clause, if the prefix is a renamed
319 -- package, the entity is set to the original package as a result,
320 -- when checking whether the package appears in a previous with
321 -- clause, the renaming has to be taken into account, to prevent
322 -- spurious/incorrect warnings. A common case is use of Text_IO.
324 ---------------
325 -- Same_Unit --
326 ---------------
328 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
329 begin
330 return Entity (N) = P
331 or else
332 (Present (Renamed_Object (P))
333 and then Entity (N) = Renamed_Object (P));
334 end Same_Unit;
336 -- Start of processing for Process_Body_Clauses
338 begin
339 Used := False;
340 Used_Type_Or_Elab := False;
342 Cont_Item := First (Context_List);
343 while Present (Cont_Item) loop
345 -- Package use clause
347 if Nkind (Cont_Item) = N_Use_Package_Clause
348 and then not Used
349 then
350 -- Search through use clauses
352 Use_Item := First (Names (Cont_Item));
353 while Present (Use_Item) and then not Used loop
355 -- Case of a direct use of the one we are looking for
357 if Entity (Use_Item) = Nam_Ent then
358 Used := True;
360 -- Handle nested case, as in "with P; use P.Q.R"
362 else
363 declare
364 UE : Node_Id;
366 begin
367 -- Loop through prefixes looking for match
369 UE := Use_Item;
370 while Nkind (UE) = N_Expanded_Name loop
371 if Same_Unit (Prefix (UE), Nam_Ent) then
372 Used := True;
373 exit;
374 end if;
376 UE := Prefix (UE);
377 end loop;
378 end;
379 end if;
381 Next (Use_Item);
382 end loop;
384 -- USE TYPE clause
386 elsif Nkind (Cont_Item) = N_Use_Type_Clause
387 and then not Used_Type_Or_Elab
388 then
389 Subt_Mark := First (Subtype_Marks (Cont_Item));
390 while Present (Subt_Mark)
391 and then not Used_Type_Or_Elab
392 loop
393 if Same_Unit (Prefix (Subt_Mark), Nam_Ent) then
394 Used_Type_Or_Elab := True;
395 end if;
397 Next (Subt_Mark);
398 end loop;
400 -- Pragma Elaborate or Elaborate_All
402 elsif Nkind (Cont_Item) = N_Pragma
403 and then
404 Nam_In (Pragma_Name (Cont_Item), Name_Elaborate,
405 Name_Elaborate_All)
406 and then not Used_Type_Or_Elab
407 then
408 Prag_Unit :=
409 First (Pragma_Argument_Associations (Cont_Item));
410 while Present (Prag_Unit)
411 and then not Used_Type_Or_Elab
412 loop
413 if Entity (Expression (Prag_Unit)) = Nam_Ent then
414 Used_Type_Or_Elab := True;
415 end if;
417 Next (Prag_Unit);
418 end loop;
419 end if;
421 Next (Cont_Item);
422 end loop;
423 end Process_Body_Clauses;
425 --------------------------
426 -- Process_Spec_Clauses --
427 --------------------------
429 procedure Process_Spec_Clauses
430 (Context_List : List_Id;
431 Clause : Node_Id;
432 Used : in out Boolean;
433 Withed : in out Boolean;
434 Exit_On_Self : Boolean := False)
436 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
437 Cont_Item : Node_Id;
438 Use_Item : Node_Id;
440 begin
441 Used := False;
442 Withed := False;
444 Cont_Item := First (Context_List);
445 while Present (Cont_Item) loop
447 -- Stop the search since the context items after Cont_Item have
448 -- already been examined in a previous iteration of the reverse
449 -- loop in Check_Redundant_Withs.
451 if Exit_On_Self
452 and Cont_Item = Clause
453 then
454 exit;
455 end if;
457 -- Package use clause
459 if Nkind (Cont_Item) = N_Use_Package_Clause
460 and then not Used
461 then
462 Use_Item := First (Names (Cont_Item));
463 while Present (Use_Item) and then not Used loop
464 if Entity (Use_Item) = Nam_Ent then
465 Used := True;
466 end if;
468 Next (Use_Item);
469 end loop;
471 -- Package with clause. Avoid processing self, implicitly
472 -- generated with clauses or limited with clauses. Note that
473 -- we examine with clauses having pragmas Elaborate or
474 -- Elaborate_All applied to them due to cases such as:
476 -- with Pack;
477 -- with Pack;
478 -- pragma Elaborate (Pack);
480 -- In this case, the second with clause is redundant since
481 -- the pragma applies only to the first "with Pack;".
483 -- Note that we only consider with_clauses that comes from
484 -- source. In the case of renamings used as prefixes of names
485 -- in with_clauses, we generate a with_clause for the prefix,
486 -- which we do not treat as implicit because it is needed for
487 -- visibility analysis, but is also not redundant.
489 elsif Nkind (Cont_Item) = N_With_Clause
490 and then not Implicit_With (Cont_Item)
491 and then Comes_From_Source (Cont_Item)
492 and then not Limited_Present (Cont_Item)
493 and then Cont_Item /= Clause
494 and then Entity (Name (Cont_Item)) = Nam_Ent
495 then
496 Withed := True;
497 end if;
499 Next (Cont_Item);
500 end loop;
501 end Process_Spec_Clauses;
503 -- Start of processing for Check_Redundant_Withs
505 begin
506 Clause := Last (Context_Items);
507 while Present (Clause) loop
509 -- Avoid checking implicitly generated with clauses, limited with
510 -- clauses or withs that have pragma Elaborate or Elaborate_All.
512 if Nkind (Clause) = N_With_Clause
513 and then not Implicit_With (Clause)
514 and then not Limited_Present (Clause)
515 and then not Elaborate_Present (Clause)
516 then
517 -- Package body-to-spec check
519 if Present (Spec_Context_Items) then
520 declare
521 Used_In_Body : Boolean := False;
522 Used_In_Spec : Boolean := False;
523 Used_Type_Or_Elab : Boolean := False;
524 Withed_In_Spec : Boolean := False;
526 begin
527 Process_Spec_Clauses
528 (Context_List => Spec_Context_Items,
529 Clause => Clause,
530 Used => Used_In_Spec,
531 Withed => Withed_In_Spec);
533 Process_Body_Clauses
534 (Context_List => Context_Items,
535 Clause => Clause,
536 Used => Used_In_Body,
537 Used_Type_Or_Elab => Used_Type_Or_Elab);
539 -- "Type Elab" refers to the presence of either a use
540 -- type clause, pragmas Elaborate or Elaborate_All.
542 -- +---------------+---------------------------+------+
543 -- | Spec | Body | Warn |
544 -- +--------+------+--------+------+-----------+------+
545 -- | Withed | Used | Withed | Used | Type Elab | |
546 -- | X | | X | | | X |
547 -- | X | | X | X | | |
548 -- | X | | X | | X | |
549 -- | X | | X | X | X | |
550 -- | X | X | X | | | X |
551 -- | X | X | X | | X | |
552 -- | X | X | X | X | | X |
553 -- | X | X | X | X | X | |
554 -- +--------+------+--------+------+-----------+------+
556 if (Withed_In_Spec
557 and then not Used_Type_Or_Elab)
558 and then
559 ((not Used_In_Spec
560 and then not Used_In_Body)
561 or else
562 Used_In_Spec)
563 then
564 Error_Msg_N -- CODEFIX
565 ("redundant with clause in body??", Clause);
566 end if;
568 Used_In_Body := False;
569 Used_In_Spec := False;
570 Used_Type_Or_Elab := False;
571 Withed_In_Spec := False;
572 end;
574 -- Standalone package spec or body check
576 else
577 declare
578 Dont_Care : Boolean := False;
579 Withed : Boolean := False;
581 begin
582 -- The mechanism for examining the context clauses of a
583 -- package spec can be applied to package body clauses.
585 Process_Spec_Clauses
586 (Context_List => Context_Items,
587 Clause => Clause,
588 Used => Dont_Care,
589 Withed => Withed,
590 Exit_On_Self => True);
592 if Withed then
593 Error_Msg_N -- CODEFIX
594 ("redundant with clause??", Clause);
595 end if;
596 end;
597 end if;
598 end if;
600 Prev (Clause);
601 end loop;
602 end Check_Redundant_Withs;
604 --------------------------------
605 -- Generate_Parent_References --
606 --------------------------------
608 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
609 Pref : Node_Id;
610 P_Name : Entity_Id := P_Id;
612 begin
613 Pref := Name (Parent (Defining_Entity (N)));
615 if Nkind (Pref) = N_Expanded_Name then
617 -- Done already, if the unit has been compiled indirectly as
618 -- part of the closure of its context because of inlining.
620 return;
621 end if;
623 while Nkind (Pref) = N_Selected_Component loop
624 Change_Selected_Component_To_Expanded_Name (Pref);
625 Set_Entity (Pref, P_Name);
626 Set_Etype (Pref, Etype (P_Name));
627 Generate_Reference (P_Name, Pref, 'r');
628 Pref := Prefix (Pref);
629 P_Name := Scope (P_Name);
630 end loop;
632 -- The guard here on P_Name is to handle the error condition where
633 -- the parent unit is missing because the file was not found.
635 if Present (P_Name) then
636 Set_Entity (Pref, P_Name);
637 Set_Etype (Pref, Etype (P_Name));
638 Generate_Reference (P_Name, Pref, 'r');
639 Style.Check_Identifier (Pref, P_Name);
640 end if;
641 end Generate_Parent_References;
643 -- Start of processing for Analyze_Compilation_Unit
645 begin
646 Process_Compilation_Unit_Pragmas (N);
648 -- If the unit is a subunit whose parent has not been analyzed (which
649 -- indicates that the main unit is a subunit, either the current one or
650 -- one of its descendents) then the subunit is compiled as part of the
651 -- analysis of the parent, which we proceed to do. Basically this gets
652 -- handled from the top down and we don't want to do anything at this
653 -- level (i.e. this subunit will be handled on the way down from the
654 -- parent), so at this level we immediately return. If the subunit ends
655 -- up not analyzed, it means that the parent did not contain a stub for
656 -- it, or that there errors were detected in some ancestor.
658 if Nkind (Unit_Node) = N_Subunit and then not Analyzed (Lib_Unit) then
659 Semantics (Lib_Unit);
661 if not Analyzed (Proper_Body (Unit_Node)) then
662 if Serious_Errors_Detected > 0 then
663 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
664 else
665 Error_Msg_N ("missing stub for subunit", N);
666 end if;
667 end if;
669 return;
670 end if;
672 -- Analyze context (this will call Sem recursively for with'ed units) To
673 -- detect circularities among with-clauses that are not caught during
674 -- loading, we set the Context_Pending flag on the current unit. If the
675 -- flag is already set there is a potential circularity. We exclude
676 -- predefined units from this check because they are known to be safe.
677 -- We also exclude package bodies that are present because circularities
678 -- between bodies are harmless (and necessary).
680 if Context_Pending (N) then
681 declare
682 Circularity : Boolean := True;
684 begin
685 if Is_Predefined_File_Name
686 (Unit_File_Name (Get_Source_Unit (Unit (N))))
687 then
688 Circularity := False;
690 else
691 for U in Main_Unit + 1 .. Last_Unit loop
692 if Nkind (Unit (Cunit (U))) = N_Package_Body
693 and then not Analyzed (Cunit (U))
694 then
695 Circularity := False;
696 exit;
697 end if;
698 end loop;
699 end if;
701 if Circularity then
702 Error_Msg_N ("circular dependency caused by with_clauses", N);
703 Error_Msg_N
704 ("\possibly missing limited_with clause"
705 & " in one of the following", N);
707 for U in Main_Unit .. Last_Unit loop
708 if Context_Pending (Cunit (U)) then
709 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
710 Error_Msg_N ("\unit$", N);
711 end if;
712 end loop;
714 raise Unrecoverable_Error;
715 end if;
716 end;
717 else
718 Set_Context_Pending (N);
719 end if;
721 Analyze_Context (N);
723 Set_Context_Pending (N, False);
725 -- If the unit is a package body, the spec is already loaded and must be
726 -- analyzed first, before we analyze the body.
728 if Nkind (Unit_Node) = N_Package_Body then
730 -- If no Lib_Unit, then there was a serious previous error, so just
731 -- ignore the entire analysis effort
733 if No (Lib_Unit) then
734 Check_Error_Detected;
735 return;
737 else
738 -- Analyze the package spec
740 Semantics (Lib_Unit);
742 -- Check for unused with's
744 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
746 -- Verify that the library unit is a package declaration
748 if not Nkind_In (Unit (Lib_Unit), N_Package_Declaration,
749 N_Generic_Package_Declaration)
750 then
751 Error_Msg_N
752 ("no legal package declaration for package body", N);
753 return;
755 -- Otherwise, the entity in the declaration is visible. Update the
756 -- version to reflect dependence of this body on the spec.
758 else
759 Spec_Id := Defining_Entity (Unit (Lib_Unit));
760 Set_Is_Immediately_Visible (Spec_Id, True);
761 Version_Update (N, Lib_Unit);
763 if Nkind (Defining_Unit_Name (Unit_Node)) =
764 N_Defining_Program_Unit_Name
765 then
766 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
767 end if;
768 end if;
769 end if;
771 -- If the unit is a subprogram body, then we similarly need to analyze
772 -- its spec. However, things are a little simpler in this case, because
773 -- here, this analysis is done mostly for error checking and consistency
774 -- purposes (but not only, e.g. there could be a contract on the spec),
775 -- so there's nothing else to be done.
777 elsif Nkind (Unit_Node) = N_Subprogram_Body then
778 if Acts_As_Spec (N) then
780 -- If the subprogram body is a child unit, we must create a
781 -- declaration for it, in order to properly load the parent(s).
782 -- After this, the original unit does not acts as a spec, because
783 -- there is an explicit one. If this unit appears in a context
784 -- clause, then an implicit with on the parent will be added when
785 -- installing the context. If this is the main unit, there is no
786 -- Unit_Table entry for the declaration (it has the unit number
787 -- of the main unit) and code generation is unaffected.
789 Unum := Get_Cunit_Unit_Number (N);
790 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
792 if Par_Spec_Name /= No_Unit_Name then
793 Unum :=
794 Load_Unit
795 (Load_Name => Par_Spec_Name,
796 Required => True,
797 Subunit => False,
798 Error_Node => N);
800 if Unum /= No_Unit then
802 -- Build subprogram declaration and attach parent unit to it
803 -- This subprogram declaration does not come from source,
804 -- Nevertheless the backend must generate debugging info for
805 -- it, and this must be indicated explicitly. We also mark
806 -- the body entity as a child unit now, to prevent a
807 -- cascaded error if the spec entity cannot be entered
808 -- in its scope. Finally we create a Units table entry for
809 -- the subprogram declaration, to maintain a one-to-one
810 -- correspondence with compilation unit nodes. This is
811 -- critical for the tree traversals performed by CodePeer.
813 declare
814 Loc : constant Source_Ptr := Sloc (N);
815 SCS : constant Boolean :=
816 Get_Comes_From_Source_Default;
818 begin
819 Set_Comes_From_Source_Default (False);
821 -- Checks for redundant USE TYPE clauses have a special
822 -- exception for the synthetic spec we create here. This
823 -- special case relies on the two compilation units
824 -- sharing the same context clause.
826 -- Note: We used to do a shallow copy (New_Copy_List),
827 -- which defeated those checks and also created malformed
828 -- trees (subtype mark shared by two distinct
829 -- N_Use_Type_Clause nodes) which crashed the compiler.
831 Lib_Unit :=
832 Make_Compilation_Unit (Loc,
833 Context_Items => Context_Items (N),
834 Unit =>
835 Make_Subprogram_Declaration (Sloc (N),
836 Specification =>
837 Copy_Separate_Tree
838 (Specification (Unit_Node))),
839 Aux_Decls_Node =>
840 Make_Compilation_Unit_Aux (Loc));
842 Set_Library_Unit (N, Lib_Unit);
843 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
844 Make_Child_Decl_Unit (N);
845 Semantics (Lib_Unit);
847 -- Now that a separate declaration exists, the body
848 -- of the child unit does not act as spec any longer.
850 Set_Acts_As_Spec (N, False);
851 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
852 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
853 Set_Comes_From_Source_Default (SCS);
854 end;
855 end if;
856 end if;
858 -- Here for subprogram with separate declaration
860 else
861 Semantics (Lib_Unit);
862 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
863 Version_Update (N, Lib_Unit);
864 end if;
866 -- If this is a child unit, generate references to the parents
868 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
869 N_Defining_Program_Unit_Name
870 then
871 Generate_Parent_References (
872 Specification (Unit_Node),
873 Scope (Defining_Entity (Unit (Lib_Unit))));
874 end if;
875 end if;
877 -- If it is a child unit, the parent must be elaborated first and we
878 -- update version, since we are dependent on our parent.
880 if Is_Child_Spec (Unit_Node) then
882 -- The analysis of the parent is done with style checks off
884 declare
885 Save_Style_Check : constant Boolean := Style_Check;
887 begin
888 if not GNAT_Mode then
889 Style_Check := False;
890 end if;
892 Semantics (Parent_Spec (Unit_Node));
893 Version_Update (N, Parent_Spec (Unit_Node));
895 -- Restore style check settings
897 Style_Check := Save_Style_Check;
898 end;
899 end if;
901 -- With the analysis done, install the context. Note that we can't
902 -- install the context from the with clauses as we analyze them, because
903 -- each with clause must be analyzed in a clean visibility context, so
904 -- we have to wait and install them all at once.
906 Install_Context (N);
908 if Is_Child_Spec (Unit_Node) then
910 -- Set the entities of all parents in the program_unit_name
912 Generate_Parent_References (
913 Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
914 end if;
916 -- All components of the context: with-clauses, library unit, ancestors
917 -- if any, (and their context) are analyzed and installed.
919 -- Call special debug routine sm if this is the main unit
921 if Current_Sem_Unit = Main_Unit then
923 end if;
925 -- Now analyze the unit (package, subprogram spec, body) itself
927 Analyze (Unit_Node);
929 if Warn_On_Redundant_Constructs then
930 Check_Redundant_Withs (Context_Items (N));
932 if Nkind (Unit_Node) = N_Package_Body then
933 Check_Redundant_Withs
934 (Context_Items => Context_Items (N),
935 Spec_Context_Items => Context_Items (Lib_Unit));
936 end if;
937 end if;
939 -- The above call might have made Unit_Node an N_Subprogram_Body from
940 -- something else, so propagate any Acts_As_Spec flag.
942 if Nkind (Unit_Node) = N_Subprogram_Body
943 and then Acts_As_Spec (Unit_Node)
944 then
945 Set_Acts_As_Spec (N);
946 end if;
948 -- Register predefined units in Rtsfind
950 declare
951 Unum : constant Unit_Number_Type := Get_Source_Unit (Sloc (N));
952 begin
953 if Is_Predefined_File_Name (Unit_File_Name (Unum)) then
954 Set_RTU_Loaded (Unit_Node);
955 end if;
956 end;
958 -- Treat compilation unit pragmas that appear after the library unit
960 if Present (Pragmas_After (Aux_Decls_Node (N))) then
961 declare
962 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
963 begin
964 while Present (Prag_Node) loop
965 Analyze (Prag_Node);
966 Next (Prag_Node);
967 end loop;
968 end;
969 end if;
971 -- Generate distribution stubs if requested and no error
973 if N = Main_Cunit
974 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
975 or else
976 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
977 and then not Fatal_Error (Main_Unit)
978 then
979 if Is_RCI_Pkg_Spec_Or_Body (N) then
981 -- Regular RCI package
983 Add_Stub_Constructs (N);
985 elsif (Nkind (Unit_Node) = N_Package_Declaration
986 and then Is_Shared_Passive (Defining_Entity
987 (Specification (Unit_Node))))
988 or else (Nkind (Unit_Node) = N_Package_Body
989 and then
990 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
991 then
992 -- Shared passive package
994 Add_Stub_Constructs (N);
996 elsif Nkind (Unit_Node) = N_Package_Instantiation
997 and then
998 Is_Remote_Call_Interface
999 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
1000 then
1001 -- Instantiation of a RCI generic package
1003 Add_Stub_Constructs (N);
1004 end if;
1005 end if;
1007 -- Remove unit from visibility, so that environment is clean for the
1008 -- next compilation, which is either the main unit or some other unit
1009 -- in the context.
1011 if Nkind_In (Unit_Node, N_Package_Declaration,
1012 N_Package_Renaming_Declaration,
1013 N_Subprogram_Declaration)
1014 or else Nkind (Unit_Node) in N_Generic_Declaration
1015 or else
1016 (Nkind (Unit_Node) = N_Subprogram_Body
1017 and then Acts_As_Spec (Unit_Node))
1018 then
1019 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
1021 -- If the unit is an instantiation whose body will be elaborated for
1022 -- inlining purposes, use the proper entity of the instance. The entity
1023 -- may be missing if the instantiation was illegal.
1025 elsif Nkind (Unit_Node) = N_Package_Instantiation
1026 and then not Error_Posted (Unit_Node)
1027 and then Present (Instance_Spec (Unit_Node))
1028 then
1029 Remove_Unit_From_Visibility
1030 (Defining_Entity (Instance_Spec (Unit_Node)));
1032 elsif Nkind (Unit_Node) = N_Package_Body
1033 or else (Nkind (Unit_Node) = N_Subprogram_Body
1034 and then not Acts_As_Spec (Unit_Node))
1035 then
1036 -- Bodies that are not the main unit are compiled if they are generic
1037 -- or contain generic or inlined units. Their analysis brings in the
1038 -- context of the corresponding spec (unit declaration) which must be
1039 -- removed as well, to return the compilation environment to its
1040 -- proper state.
1042 Remove_Context (Lib_Unit);
1043 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1044 end if;
1046 -- Last step is to deinstall the context we just installed as well as
1047 -- the unit just compiled.
1049 Remove_Context (N);
1051 -- If this is the main unit and we are generating code, we must check
1052 -- that all generic units in the context have a body if they need it,
1053 -- even if they have not been instantiated. In the absence of .ali files
1054 -- for generic units, we must force the load of the body, just to
1055 -- produce the proper error if the body is absent. We skip this
1056 -- verification if the main unit itself is generic.
1058 if Get_Cunit_Unit_Number (N) = Main_Unit
1059 and then Operating_Mode = Generate_Code
1060 and then Expander_Active
1061 then
1062 -- Check whether the source for the body of the unit must be included
1063 -- in a standalone library.
1065 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1067 -- Indicate that the main unit is now analyzed, to catch possible
1068 -- circularities between it and generic bodies. Remove main unit from
1069 -- visibility. This might seem superfluous, but the main unit must
1070 -- not be visible in the generic body expansions that follow.
1072 Set_Analyzed (N, True);
1073 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1075 declare
1076 Item : Node_Id;
1077 Nam : Entity_Id;
1078 Un : Unit_Number_Type;
1080 Save_Style_Check : constant Boolean := Style_Check;
1082 begin
1083 Item := First (Context_Items (N));
1084 while Present (Item) loop
1086 -- Check for explicit with clause
1088 if Nkind (Item) = N_With_Clause
1089 and then not Implicit_With (Item)
1091 -- Ada 2005 (AI-50217): Ignore limited-withed units
1093 and then not Limited_Present (Item)
1094 then
1095 Nam := Entity (Name (Item));
1097 -- Compile generic subprogram, unless it is intrinsic or
1098 -- imported so no body is required, or generic package body
1099 -- if the package spec requires a body.
1101 if (Is_Generic_Subprogram (Nam)
1102 and then not Is_Intrinsic_Subprogram (Nam)
1103 and then not Is_Imported (Nam))
1104 or else (Ekind (Nam) = E_Generic_Package
1105 and then Unit_Requires_Body (Nam))
1106 then
1107 Style_Check := False;
1109 if Present (Renamed_Object (Nam)) then
1110 Un :=
1111 Load_Unit
1112 (Load_Name => Get_Body_Name
1113 (Get_Unit_Name
1114 (Unit_Declaration_Node
1115 (Renamed_Object (Nam)))),
1116 Required => False,
1117 Subunit => False,
1118 Error_Node => N,
1119 Renamings => True);
1120 else
1121 Un :=
1122 Load_Unit
1123 (Load_Name => Get_Body_Name
1124 (Get_Unit_Name (Item)),
1125 Required => False,
1126 Subunit => False,
1127 Error_Node => N,
1128 Renamings => True);
1129 end if;
1131 if Un = No_Unit then
1132 Error_Msg_NE
1133 ("body of generic unit& not found", Item, Nam);
1134 exit;
1136 elsif not Analyzed (Cunit (Un))
1137 and then Un /= Main_Unit
1138 and then not Fatal_Error (Un)
1139 then
1140 Style_Check := False;
1141 Semantics (Cunit (Un));
1142 end if;
1143 end if;
1144 end if;
1146 Next (Item);
1147 end loop;
1149 -- Restore style checks settings
1151 Style_Check := Save_Style_Check;
1152 end;
1153 end if;
1155 -- Deal with creating elaboration Boolean if needed. We create an
1156 -- elaboration boolean only for units that come from source since
1157 -- units manufactured by the compiler never need elab checks.
1159 if Comes_From_Source (N)
1160 and then Nkind_In (Unit_Node, N_Package_Declaration,
1161 N_Generic_Package_Declaration,
1162 N_Subprogram_Declaration,
1163 N_Generic_Subprogram_Declaration)
1164 then
1165 declare
1166 Loc : constant Source_Ptr := Sloc (N);
1167 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1169 begin
1170 Spec_Id := Defining_Entity (Unit_Node);
1171 Generate_Definition (Spec_Id);
1173 -- See if an elaboration entity is required for possible access
1174 -- before elaboration checking. Note that we must allow for this
1175 -- even if -gnatE is not set, since a client may be compiled in
1176 -- -gnatE mode and reference the entity.
1178 -- These entities are also used by the binder to prevent multiple
1179 -- attempts to execute the elaboration code for the library case
1180 -- where the elaboration routine might otherwise be called more
1181 -- than once.
1183 -- Case of units which do not require elaboration checks
1186 -- Pure units do not need checks
1188 Is_Pure (Spec_Id)
1190 -- Preelaborated units do not need checks
1192 or else Is_Preelaborated (Spec_Id)
1194 -- No checks needed if pragma Elaborate_Body present
1196 or else Has_Pragma_Elaborate_Body (Spec_Id)
1198 -- No checks needed if unit does not require a body
1200 or else not Unit_Requires_Body (Spec_Id)
1202 -- No checks needed for predefined files
1204 or else Is_Predefined_File_Name (Unit_File_Name (Unum))
1206 -- No checks required if no separate spec
1208 or else Acts_As_Spec (N)
1209 then
1210 -- This is a case where we only need the entity for
1211 -- checking to prevent multiple elaboration checks.
1213 Set_Elaboration_Entity_Required (Spec_Id, False);
1215 -- Case of elaboration entity is required for access before
1216 -- elaboration checking (so certainly we must build it!)
1218 else
1219 Set_Elaboration_Entity_Required (Spec_Id, True);
1220 end if;
1222 Build_Elaboration_Entity (N, Spec_Id);
1223 end;
1224 end if;
1226 -- Freeze the compilation unit entity. This for sure is needed because
1227 -- of some warnings that can be output (see Freeze_Subprogram), but may
1228 -- in general be required. If freezing actions result, place them in the
1229 -- compilation unit actions list, and analyze them.
1231 declare
1232 L : constant List_Id :=
1233 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1234 begin
1235 while Is_Non_Empty_List (L) loop
1236 Insert_Library_Level_Action (Remove_Head (L));
1237 end loop;
1238 end;
1240 Set_Analyzed (N);
1242 if Nkind (Unit_Node) = N_Package_Declaration
1243 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1244 and then Expander_Active
1245 then
1246 declare
1247 Save_Style_Check : constant Boolean := Style_Check;
1248 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1249 Options : Style_Check_Options;
1251 begin
1252 Save_Style_Check_Options (Options);
1253 Reset_Style_Check_Options;
1254 Opt.Warning_Mode := Suppress;
1255 Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1257 Reset_Style_Check_Options;
1258 Set_Style_Check_Options (Options);
1259 Style_Check := Save_Style_Check;
1260 Warning_Mode := Save_Warning;
1261 end;
1262 end if;
1264 -- If we are generating obsolescent warnings, then here is where we
1265 -- generate them for the with'ed items. The reason for this special
1266 -- processing is that the normal mechanism of generating the warnings
1267 -- for referenced entities does not work for context clause references.
1268 -- That's because when we first analyze the context, it is too early to
1269 -- know if the with'ing unit is itself obsolescent (which suppresses
1270 -- the warnings).
1272 if not GNAT_Mode
1273 and then Warn_On_Obsolescent_Feature
1274 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1275 then
1276 -- Push current compilation unit as scope, so that the test for
1277 -- being within an obsolescent unit will work correctly. The check
1278 -- is not performed within an instantiation, because the warning
1279 -- will have been emitted in the corresponding generic unit.
1281 Push_Scope (Defining_Entity (Unit_Node));
1283 -- Loop through context items to deal with with clauses
1285 declare
1286 Item : Node_Id;
1287 Nam : Node_Id;
1288 Ent : Entity_Id;
1290 begin
1291 Item := First (Context_Items (N));
1292 while Present (Item) loop
1293 if Nkind (Item) = N_With_Clause
1295 -- Suppress this check in limited-withed units. Further work
1296 -- needed here if we decide to incorporate this check on
1297 -- limited-withed units.
1299 and then not Limited_Present (Item)
1300 then
1301 Nam := Name (Item);
1302 Ent := Entity (Nam);
1304 if Is_Obsolescent (Ent) then
1305 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1306 end if;
1307 end if;
1309 Next (Item);
1310 end loop;
1311 end;
1313 -- Remove temporary install of current unit as scope
1315 Pop_Scope;
1316 end if;
1317 end Analyze_Compilation_Unit;
1319 ---------------------
1320 -- Analyze_Context --
1321 ---------------------
1323 procedure Analyze_Context (N : Node_Id) is
1324 Ukind : constant Node_Kind := Nkind (Unit (N));
1325 Item : Node_Id;
1327 begin
1328 -- First process all configuration pragmas at the start of the context
1329 -- items. Strictly these are not part of the context clause, but that
1330 -- is where the parser puts them. In any case for sure we must analyze
1331 -- these before analyzing the actual context items, since they can have
1332 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1333 -- be with'ed as a result of changing categorizations in Ada 2005).
1335 Item := First (Context_Items (N));
1336 while Present (Item)
1337 and then Nkind (Item) = N_Pragma
1338 and then Pragma_Name (Item) in Configuration_Pragma_Names
1339 loop
1340 Analyze (Item);
1341 Next (Item);
1342 end loop;
1344 -- This is the point at which we capture the configuration settings
1345 -- for the unit. At the moment only the Optimize_Alignment setting
1346 -- needs to be captured. Probably more later ???
1348 if Optimize_Alignment_Local then
1349 Set_OA_Setting (Current_Sem_Unit, 'L');
1350 else
1351 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1352 end if;
1354 -- Loop through actual context items. This is done in two passes:
1356 -- a) The first pass analyzes non-limited with-clauses and also any
1357 -- configuration pragmas (we need to get the latter analyzed right
1358 -- away, since they can affect processing of subsequent items.
1360 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1362 while Present (Item) loop
1364 -- For with clause, analyze the with clause, and then update the
1365 -- version, since we are dependent on a unit that we with.
1367 if Nkind (Item) = N_With_Clause
1368 and then not Limited_Present (Item)
1369 then
1370 -- Skip analyzing with clause if no unit, nothing to do (this
1371 -- happens for a with that references a non-existent unit). Skip
1372 -- as well if this is a with_clause for the main unit, which
1373 -- happens if a subunit has a useless with_clause on its parent.
1375 if Present (Library_Unit (Item)) then
1376 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1377 Analyze (Item);
1379 else
1380 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1381 end if;
1382 end if;
1384 if not Implicit_With (Item) then
1385 Version_Update (N, Library_Unit (Item));
1386 end if;
1388 -- Skip pragmas. Configuration pragmas at the start were handled in
1389 -- the loop above, and remaining pragmas are not processed until we
1390 -- actually install the context (see Install_Context). We delay the
1391 -- analysis of these pragmas to make sure that we have installed all
1392 -- the implicit with's on parent units.
1394 -- Skip use clauses at this stage, since we don't want to do any
1395 -- installing of potentially use-visible entities until we
1396 -- actually install the complete context (in Install_Context).
1397 -- Otherwise things can get installed in the wrong context.
1399 else
1400 null;
1401 end if;
1403 Next (Item);
1404 end loop;
1406 -- Second pass: examine all limited_with clauses. All other context
1407 -- items are ignored in this pass.
1409 Item := First (Context_Items (N));
1410 while Present (Item) loop
1411 if Nkind (Item) = N_With_Clause
1412 and then Limited_Present (Item)
1413 then
1414 -- No need to check errors on implicitly generated limited-with
1415 -- clauses.
1417 if not Implicit_With (Item) then
1419 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1420 -- properly rejected, including renaming declarations.
1422 if not Nkind_In (Ukind, N_Package_Declaration,
1423 N_Subprogram_Declaration)
1424 and then Ukind not in N_Generic_Declaration
1425 and then Ukind not in N_Generic_Instantiation
1426 then
1427 Error_Msg_N ("limited with_clause not allowed here", Item);
1429 -- Check wrong use of a limited with clause applied to the
1430 -- compilation unit containing the limited-with clause.
1432 -- limited with P.Q;
1433 -- package P.Q is ...
1435 elsif Unit (Library_Unit (Item)) = Unit (N) then
1436 Error_Msg_N ("wrong use of limited-with clause", Item);
1438 -- Check wrong use of limited-with clause applied to some
1439 -- immediate ancestor.
1441 elsif Is_Child_Spec (Unit (N)) then
1442 declare
1443 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1444 P : Node_Id;
1446 begin
1447 P := Parent_Spec (Unit (N));
1448 loop
1449 if Unit (P) = Lib_U then
1450 Error_Msg_N ("limited with_clause cannot "
1451 & "name ancestor", Item);
1452 exit;
1453 end if;
1455 exit when not Is_Child_Spec (Unit (P));
1456 P := Parent_Spec (Unit (P));
1457 end loop;
1458 end;
1459 end if;
1461 -- Check if the limited-withed unit is already visible through
1462 -- some context clause of the current compilation unit or some
1463 -- ancestor of the current compilation unit.
1465 declare
1466 Lim_Unit_Name : constant Node_Id := Name (Item);
1467 Comp_Unit : Node_Id;
1468 It : Node_Id;
1469 Unit_Name : Node_Id;
1471 begin
1472 Comp_Unit := N;
1473 loop
1474 It := First (Context_Items (Comp_Unit));
1475 while Present (It) loop
1476 if Item /= It
1477 and then Nkind (It) = N_With_Clause
1478 and then not Limited_Present (It)
1479 and then
1480 Nkind_In (Unit (Library_Unit (It)),
1481 N_Package_Declaration,
1482 N_Package_Renaming_Declaration)
1483 then
1484 if Nkind (Unit (Library_Unit (It))) =
1485 N_Package_Declaration
1486 then
1487 Unit_Name := Name (It);
1488 else
1489 Unit_Name := Name (Unit (Library_Unit (It)));
1490 end if;
1492 -- Check if the named package (or some ancestor)
1493 -- leaves visible the full-view of the unit given
1494 -- in the limited-with clause
1496 loop
1497 if Designate_Same_Unit (Lim_Unit_Name,
1498 Unit_Name)
1499 then
1500 Error_Msg_Sloc := Sloc (It);
1501 Error_Msg_N
1502 ("simultaneous visibility of limited "
1503 & "and unlimited views not allowed",
1504 Item);
1505 Error_Msg_NE
1506 ("\unlimited view visible through "
1507 & "context clause #",
1508 Item, It);
1509 exit;
1511 elsif Nkind (Unit_Name) = N_Identifier then
1512 exit;
1513 end if;
1515 Unit_Name := Prefix (Unit_Name);
1516 end loop;
1517 end if;
1519 Next (It);
1520 end loop;
1522 exit when not Is_Child_Spec (Unit (Comp_Unit));
1524 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1525 end loop;
1526 end;
1527 end if;
1529 -- Skip analyzing with clause if no unit, see above
1531 if Present (Library_Unit (Item)) then
1532 Analyze (Item);
1533 end if;
1535 -- A limited_with does not impose an elaboration order, but
1536 -- there is a semantic dependency for recompilation purposes.
1538 if not Implicit_With (Item) then
1539 Version_Update (N, Library_Unit (Item));
1540 end if;
1542 -- Pragmas and use clauses and with clauses other than limited
1543 -- with's are ignored in this pass through the context items.
1545 else
1546 null;
1547 end if;
1549 Next (Item);
1550 end loop;
1551 end Analyze_Context;
1553 -------------------------------
1554 -- Analyze_Package_Body_Stub --
1555 -------------------------------
1557 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1558 Id : constant Entity_Id := Defining_Identifier (N);
1559 Nam : Entity_Id;
1561 begin
1562 -- The package declaration must be in the current declarative part
1564 Check_Stub_Level (N);
1565 Nam := Current_Entity_In_Scope (Id);
1567 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1568 Error_Msg_N ("missing specification for package stub", N);
1570 elsif Has_Completion (Nam)
1571 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1572 then
1573 Error_Msg_N ("duplicate or redundant stub for package", N);
1575 else
1576 -- Indicate that the body of the package exists. If we are doing
1577 -- only semantic analysis, the stub stands for the body. If we are
1578 -- generating code, the existence of the body will be confirmed
1579 -- when we load the proper body.
1581 Set_Has_Completion (Nam);
1582 Set_Scope (Defining_Entity (N), Current_Scope);
1583 Generate_Reference (Nam, Id, 'b');
1584 Analyze_Proper_Body (N, Nam);
1585 end if;
1586 end Analyze_Package_Body_Stub;
1588 -------------------------
1589 -- Analyze_Proper_Body --
1590 -------------------------
1592 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1593 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1594 Unum : Unit_Number_Type;
1596 procedure Optional_Subunit;
1597 -- This procedure is called when the main unit is a stub, or when we
1598 -- are not generating code. In such a case, we analyze the subunit if
1599 -- present, which is user-friendly and in fact required for ASIS, but
1600 -- we don't complain if the subunit is missing.
1602 ----------------------
1603 -- Optional_Subunit --
1604 ----------------------
1606 procedure Optional_Subunit is
1607 Comp_Unit : Node_Id;
1609 begin
1610 -- Try to load subunit, but ignore any errors that occur during the
1611 -- loading of the subunit, by using the special feature in Errout to
1612 -- ignore all errors. Note that Fatal_Error will still be set, so we
1613 -- will be able to check for this case below.
1615 if not ASIS_Mode then
1616 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1617 end if;
1619 Unum :=
1620 Load_Unit
1621 (Load_Name => Subunit_Name,
1622 Required => False,
1623 Subunit => True,
1624 Error_Node => N);
1626 if not ASIS_Mode then
1627 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1628 end if;
1630 -- All done if we successfully loaded the subunit
1632 if Unum /= No_Unit
1633 and then (not Fatal_Error (Unum) or else Try_Semantics)
1634 then
1635 Comp_Unit := Cunit (Unum);
1637 -- If the file was empty or seriously mangled, the unit itself may
1638 -- be missing.
1640 if No (Unit (Comp_Unit)) then
1641 Error_Msg_N
1642 ("subunit does not contain expected proper body", N);
1644 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1645 Error_Msg_N
1646 ("expected SEPARATE subunit, found child unit",
1647 Cunit_Entity (Unum));
1648 else
1649 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1650 Analyze_Subunit (Comp_Unit);
1651 Set_Library_Unit (N, Comp_Unit);
1652 end if;
1654 elsif Unum = No_Unit
1655 and then Present (Nam)
1656 then
1657 if Is_Protected_Type (Nam) then
1658 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1659 else
1660 Set_Corresponding_Body (
1661 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1662 end if;
1663 end if;
1664 end Optional_Subunit;
1666 -- Start of processing for Analyze_Proper_Body
1668 begin
1669 -- If the subunit is already loaded, it means that the main unit is a
1670 -- subunit, and that the current unit is one of its parents which was
1671 -- being analyzed to provide the needed context for the analysis of the
1672 -- subunit. In this case we analyze the subunit and continue with the
1673 -- parent, without looking at subsequent subunits.
1675 if Is_Loaded (Subunit_Name) then
1677 -- If the proper body is already linked to the stub node, the stub is
1678 -- in a generic unit and just needs analyzing.
1680 if Present (Library_Unit (N)) then
1681 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1683 -- If the subunit has severe errors, the spec of the enclosing
1684 -- body may not be available, in which case do not try analysis.
1686 if Serious_Errors_Detected > 0
1687 and then No (Library_Unit (Library_Unit (N)))
1688 then
1689 return;
1690 end if;
1692 Analyze_Subunit (Library_Unit (N));
1694 -- Otherwise we must load the subunit and link to it
1696 else
1697 -- Load the subunit, this must work, since we originally loaded
1698 -- the subunit earlier on. So this will not really load it, just
1699 -- give access to it.
1701 Unum :=
1702 Load_Unit
1703 (Load_Name => Subunit_Name,
1704 Required => True,
1705 Subunit => False,
1706 Error_Node => N);
1708 -- And analyze the subunit in the parent context (note that we
1709 -- do not call Semantics, since that would remove the parent
1710 -- context). Because of this, we have to manually reset the
1711 -- compiler state to Analyzing since it got destroyed by Load.
1713 if Unum /= No_Unit then
1714 Compiler_State := Analyzing;
1716 -- Check that the proper body is a subunit and not a child
1717 -- unit. If the unit was previously loaded, the error will
1718 -- have been emitted when copying the generic node, so we
1719 -- just return to avoid cascaded errors.
1721 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1722 return;
1723 end if;
1725 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1726 Analyze_Subunit (Cunit (Unum));
1727 Set_Library_Unit (N, Cunit (Unum));
1728 end if;
1729 end if;
1731 -- If the main unit is a subunit, then we are just performing semantic
1732 -- analysis on that subunit, and any other subunits of any parent unit
1733 -- should be ignored, except that if we are building trees for ASIS
1734 -- usage we want to annotate the stub properly.
1736 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1737 and then Subunit_Name /= Unit_Name (Main_Unit)
1738 then
1739 if ASIS_Mode then
1740 Optional_Subunit;
1741 end if;
1743 -- But before we return, set the flag for unloaded subunits. This
1744 -- will suppress junk warnings of variables in the same declarative
1745 -- part (or a higher level one) that are in danger of looking unused
1746 -- when in fact there might be a declaration in the subunit that we
1747 -- do not intend to load.
1749 Unloaded_Subunits := True;
1750 return;
1752 -- If the subunit is not already loaded, and we are generating code,
1753 -- then this is the case where compilation started from the parent, and
1754 -- we are generating code for an entire subunit tree. In that case we
1755 -- definitely need to load the subunit.
1757 -- In order to continue the analysis with the rest of the parent,
1758 -- and other subunits, we load the unit without requiring its
1759 -- presence, and emit a warning if not found, rather than terminating
1760 -- the compilation abruptly, as for other missing file problems.
1762 elsif Original_Operating_Mode = Generate_Code then
1764 -- If the proper body is already linked to the stub node, the stub is
1765 -- in a generic unit and just needs analyzing.
1767 -- We update the version. Although we are not strictly technically
1768 -- semantically dependent on the subunit, given our approach of macro
1769 -- substitution of subunits, it makes sense to include it in the
1770 -- version identification.
1772 if Present (Library_Unit (N)) then
1773 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1774 Analyze_Subunit (Library_Unit (N));
1775 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1777 -- Otherwise we must load the subunit and link to it
1779 else
1780 -- Make sure that, if the subunit is preprocessed and -gnateG is
1781 -- specified, the preprocessed file will be written.
1783 Lib.Analysing_Subunit_Of_Main := True;
1784 Unum :=
1785 Load_Unit
1786 (Load_Name => Subunit_Name,
1787 Required => False,
1788 Subunit => True,
1789 Error_Node => N);
1790 Lib.Analysing_Subunit_Of_Main := False;
1792 -- Give message if we did not get the unit Emit warning even if
1793 -- missing subunit is not within main unit, to simplify debugging.
1795 if Original_Operating_Mode = Generate_Code
1796 and then Unum = No_Unit
1797 then
1798 Error_Msg_Unit_1 := Subunit_Name;
1799 Error_Msg_File_1 :=
1800 Get_File_Name (Subunit_Name, Subunit => True);
1801 Error_Msg_N
1802 ("subunit$$ in file{ not found??!!", N);
1803 Subunits_Missing := True;
1804 end if;
1806 -- Load_Unit may reset Compiler_State, since it may have been
1807 -- necessary to parse an additional units, so we make sure that
1808 -- we reset it to the Analyzing state.
1810 Compiler_State := Analyzing;
1812 if Unum /= No_Unit then
1813 if Debug_Flag_L then
1814 Write_Str ("*** Loaded subunit from stub. Analyze");
1815 Write_Eol;
1816 end if;
1818 declare
1819 Comp_Unit : constant Node_Id := Cunit (Unum);
1821 begin
1822 -- Check for child unit instead of subunit
1824 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1825 Error_Msg_N
1826 ("expected SEPARATE subunit, found child unit",
1827 Cunit_Entity (Unum));
1829 -- OK, we have a subunit
1831 else
1832 -- Set corresponding stub (even if errors)
1834 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1836 -- Collect SCO information for loaded subunit if we are
1837 -- in the main unit.
1839 if Generate_SCO
1840 and then
1841 In_Extended_Main_Source_Unit
1842 (Cunit_Entity (Current_Sem_Unit))
1843 then
1844 SCO_Record (Unum);
1845 end if;
1847 -- Analyze the unit if semantics active
1849 if not Fatal_Error (Unum) or else Try_Semantics then
1850 Analyze_Subunit (Comp_Unit);
1851 end if;
1853 -- Set the library unit pointer in any case
1855 Set_Library_Unit (N, Comp_Unit);
1857 -- We update the version. Although we are not technically
1858 -- semantically dependent on the subunit, given our
1859 -- approach of macro substitution of subunits, it makes
1860 -- sense to include it in the version identification.
1862 Version_Update (Cunit (Main_Unit), Comp_Unit);
1863 end if;
1864 end;
1865 end if;
1866 end if;
1868 -- The remaining case is when the subunit is not already loaded and we
1869 -- are not generating code. In this case we are just performing semantic
1870 -- analysis on the parent, and we are not interested in the subunit. For
1871 -- subprograms, analyze the stub as a body. For other entities the stub
1872 -- has already been marked as completed.
1874 else
1875 Optional_Subunit;
1876 end if;
1877 end Analyze_Proper_Body;
1879 ----------------------------------
1880 -- Analyze_Protected_Body_Stub --
1881 ----------------------------------
1883 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1884 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1886 begin
1887 Check_Stub_Level (N);
1889 -- First occurrence of name may have been as an incomplete type
1891 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1892 Nam := Full_View (Nam);
1893 end if;
1895 if No (Nam)
1896 or else not Is_Protected_Type (Etype (Nam))
1897 then
1898 Error_Msg_N ("missing specification for Protected body", N);
1899 else
1900 Set_Scope (Defining_Entity (N), Current_Scope);
1901 Set_Has_Completion (Etype (Nam));
1902 Generate_Reference (Nam, Defining_Identifier (N), 'b');
1903 Analyze_Proper_Body (N, Etype (Nam));
1904 end if;
1905 end Analyze_Protected_Body_Stub;
1907 ----------------------------------
1908 -- Analyze_Subprogram_Body_Stub --
1909 ----------------------------------
1911 -- A subprogram body stub can appear with or without a previous spec. If
1912 -- there is one, then the analysis of the body will find it and verify
1913 -- conformance. The formals appearing in the specification of the stub play
1914 -- no role, except for requiring an additional conformance check. If there
1915 -- is no previous subprogram declaration, the stub acts as a spec, and
1916 -- provides the defining entity for the subprogram.
1918 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1919 Decl : Node_Id;
1921 begin
1922 Check_Stub_Level (N);
1924 -- Verify that the identifier for the stub is unique within this
1925 -- declarative part.
1927 if Nkind_In (Parent (N), N_Block_Statement,
1928 N_Package_Body,
1929 N_Subprogram_Body)
1930 then
1931 Decl := First (Declarations (Parent (N)));
1932 while Present (Decl)
1933 and then Decl /= N
1934 loop
1935 if Nkind (Decl) = N_Subprogram_Body_Stub
1936 and then (Chars (Defining_Unit_Name (Specification (Decl))) =
1937 Chars (Defining_Unit_Name (Specification (N))))
1938 then
1939 Error_Msg_N ("identifier for stub is not unique", N);
1940 end if;
1942 Next (Decl);
1943 end loop;
1944 end if;
1946 -- Treat stub as a body, which checks conformance if there is a previous
1947 -- declaration, or else introduces entity and its signature.
1949 Analyze_Subprogram_Body (N);
1950 Analyze_Proper_Body (N, Empty);
1951 end Analyze_Subprogram_Body_Stub;
1953 ---------------------
1954 -- Analyze_Subunit --
1955 ---------------------
1957 -- A subunit is compiled either by itself (for semantic checking) or as
1958 -- part of compiling the parent (for code generation). In either case, by
1959 -- the time we actually process the subunit, the parent has already been
1960 -- installed and analyzed. The node N is a compilation unit, whose context
1961 -- needs to be treated here, because we come directly here from the parent
1962 -- without calling Analyze_Compilation_Unit.
1964 -- The compilation context includes the explicit context of the subunit,
1965 -- and the context of the parent, together with the parent itself. In order
1966 -- to compile the current context, we remove the one inherited from the
1967 -- parent, in order to have a clean visibility table. We restore the parent
1968 -- context before analyzing the proper body itself. On exit, we remove only
1969 -- the explicit context of the subunit.
1971 procedure Analyze_Subunit (N : Node_Id) is
1972 Lib_Unit : constant Node_Id := Library_Unit (N);
1973 Par_Unit : constant Entity_Id := Current_Scope;
1975 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
1976 Num_Scopes : Int := 0;
1977 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
1978 Enclosing_Child : Entity_Id := Empty;
1979 Svg : constant Suppress_Record := Scope_Suppress;
1981 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
1982 Cunit_Boolean_Restrictions_Save;
1983 -- Save non-partition wide restrictions before processing the subunit.
1984 -- All subunits are analyzed with config restrictions reset and we need
1985 -- to restore these saved values at the end.
1987 procedure Analyze_Subunit_Context;
1988 -- Capture names in use clauses of the subunit. This must be done before
1989 -- re-installing parent declarations, because items in the context must
1990 -- not be hidden by declarations local to the parent.
1992 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1993 -- Recursive procedure to restore scope of all ancestors of subunit,
1994 -- from outermost in. If parent is not a subunit, the call to install
1995 -- context installs context of spec and (if parent is a child unit) the
1996 -- context of its parents as well. It is confusing that parents should
1997 -- be treated differently in both cases, but the semantics are just not
1998 -- identical.
2000 procedure Re_Install_Use_Clauses;
2001 -- As part of the removal of the parent scope, the use clauses are
2002 -- removed, to be reinstalled when the context of the subunit has been
2003 -- analyzed. Use clauses may also have been affected by the analysis of
2004 -- the context of the subunit, so they have to be applied again, to
2005 -- insure that the compilation environment of the rest of the parent
2006 -- unit is identical.
2008 procedure Remove_Scope;
2009 -- Remove current scope from scope stack, and preserve the list of use
2010 -- clauses in it, to be reinstalled after context is analyzed.
2012 -----------------------------
2013 -- Analyze_Subunit_Context --
2014 -----------------------------
2016 procedure Analyze_Subunit_Context is
2017 Item : Node_Id;
2018 Nam : Node_Id;
2019 Unit_Name : Entity_Id;
2021 begin
2022 Analyze_Context (N);
2024 -- Make withed units immediately visible. If child unit, make the
2025 -- ultimate parent immediately visible.
2027 Item := First (Context_Items (N));
2028 while Present (Item) loop
2029 if Nkind (Item) = N_With_Clause then
2031 -- Protect frontend against previous errors in context clauses
2033 if Nkind (Name (Item)) /= N_Selected_Component then
2034 if Error_Posted (Item) then
2035 null;
2037 else
2038 -- If a subunits has serious syntax errors, the context
2039 -- may not have been loaded. Add a harmless unit name to
2040 -- attempt processing.
2042 if Serious_Errors_Detected > 0
2043 and then No (Entity (Name (Item)))
2044 then
2045 Set_Entity (Name (Item), Standard_Standard);
2046 end if;
2048 Unit_Name := Entity (Name (Item));
2049 loop
2050 Set_Is_Visible_Lib_Unit (Unit_Name);
2051 exit when Scope (Unit_Name) = Standard_Standard;
2052 Unit_Name := Scope (Unit_Name);
2054 if No (Unit_Name) then
2055 Check_Error_Detected;
2056 return;
2057 end if;
2058 end loop;
2060 if not Is_Immediately_Visible (Unit_Name) then
2061 Set_Is_Immediately_Visible (Unit_Name);
2062 Set_Context_Installed (Item);
2063 end if;
2064 end if;
2065 end if;
2067 elsif Nkind (Item) = N_Use_Package_Clause then
2068 Nam := First (Names (Item));
2069 while Present (Nam) loop
2070 Analyze (Nam);
2071 Next (Nam);
2072 end loop;
2074 elsif Nkind (Item) = N_Use_Type_Clause then
2075 Nam := First (Subtype_Marks (Item));
2076 while Present (Nam) loop
2077 Analyze (Nam);
2078 Next (Nam);
2079 end loop;
2080 end if;
2082 Next (Item);
2083 end loop;
2085 -- Reset visibility of withed units. They will be made visible again
2086 -- when we install the subunit context.
2088 Item := First (Context_Items (N));
2089 while Present (Item) loop
2090 if Nkind (Item) = N_With_Clause
2092 -- Protect frontend against previous errors in context clauses
2094 and then Nkind (Name (Item)) /= N_Selected_Component
2095 and then not Error_Posted (Item)
2096 then
2097 Unit_Name := Entity (Name (Item));
2098 loop
2099 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2100 exit when Scope (Unit_Name) = Standard_Standard;
2101 Unit_Name := Scope (Unit_Name);
2102 end loop;
2104 if Context_Installed (Item) then
2105 Set_Is_Immediately_Visible (Unit_Name, False);
2106 Set_Context_Installed (Item, False);
2107 end if;
2108 end if;
2110 Next (Item);
2111 end loop;
2112 end Analyze_Subunit_Context;
2114 ------------------------
2115 -- Re_Install_Parents --
2116 ------------------------
2118 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2119 E : Entity_Id;
2121 begin
2122 if Nkind (Unit (L)) = N_Subunit then
2123 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2124 end if;
2126 Install_Context (L);
2128 -- If the subunit occurs within a child unit, we must restore the
2129 -- immediate visibility of any siblings that may occur in context.
2131 if Present (Enclosing_Child) then
2132 Install_Siblings (Enclosing_Child, L);
2133 end if;
2135 Push_Scope (Scop);
2137 if Scop /= Par_Unit then
2138 Set_Is_Immediately_Visible (Scop);
2139 end if;
2141 -- Make entities in scope visible again. For child units, restore
2142 -- visibility only if they are actually in context.
2144 E := First_Entity (Current_Scope);
2145 while Present (E) loop
2146 if not Is_Child_Unit (E)
2147 or else Is_Visible_Lib_Unit (E)
2148 then
2149 Set_Is_Immediately_Visible (E);
2150 end if;
2152 Next_Entity (E);
2153 end loop;
2155 -- A subunit appears within a body, and for a nested subunits all the
2156 -- parents are bodies. Restore full visibility of their private
2157 -- entities.
2159 if Is_Package_Or_Generic_Package (Scop) then
2160 Set_In_Package_Body (Scop);
2161 Install_Private_Declarations (Scop);
2162 end if;
2163 end Re_Install_Parents;
2165 ----------------------------
2166 -- Re_Install_Use_Clauses --
2167 ----------------------------
2169 procedure Re_Install_Use_Clauses is
2170 U : Node_Id;
2171 begin
2172 for J in reverse 1 .. Num_Scopes loop
2173 U := Use_Clauses (J);
2174 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2175 Install_Use_Clauses (U, Force_Installation => True);
2176 end loop;
2177 end Re_Install_Use_Clauses;
2179 ------------------
2180 -- Remove_Scope --
2181 ------------------
2183 procedure Remove_Scope is
2184 E : Entity_Id;
2186 begin
2187 Num_Scopes := Num_Scopes + 1;
2188 Use_Clauses (Num_Scopes) :=
2189 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2191 E := First_Entity (Current_Scope);
2192 while Present (E) loop
2193 Set_Is_Immediately_Visible (E, False);
2194 Next_Entity (E);
2195 end loop;
2197 if Is_Child_Unit (Current_Scope) then
2198 Enclosing_Child := Current_Scope;
2199 end if;
2201 Pop_Scope;
2202 end Remove_Scope;
2204 -- Start of processing for Analyze_Subunit
2206 begin
2207 -- For subunit in main extended unit, we reset the configuration values
2208 -- for the non-partition-wide restrictions. For other units reset them.
2210 if In_Extended_Main_Source_Unit (N) then
2211 Restore_Config_Cunit_Boolean_Restrictions;
2212 else
2213 Reset_Cunit_Boolean_Restrictions;
2214 end if;
2216 if Style_Check then
2217 declare
2218 Nam : Node_Id := Name (Unit (N));
2220 begin
2221 if Nkind (Nam) = N_Selected_Component then
2222 Nam := Selector_Name (Nam);
2223 end if;
2225 Check_Identifier (Nam, Par_Unit);
2226 end;
2227 end if;
2229 if not Is_Empty_List (Context_Items (N)) then
2231 -- Save current use clauses
2233 Remove_Scope;
2234 Remove_Context (Lib_Unit);
2236 -- Now remove parents and their context, including enclosing subunits
2237 -- and the outer parent body which is not a subunit.
2239 if Present (Lib_Spec) then
2240 Remove_Context (Lib_Spec);
2242 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2243 Lib_Spec := Library_Unit (Lib_Spec);
2244 Remove_Scope;
2245 Remove_Context (Lib_Spec);
2246 end loop;
2248 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2249 Remove_Scope;
2250 end if;
2252 if Nkind (Unit (Lib_Spec)) = N_Package_Body then
2253 Remove_Context (Library_Unit (Lib_Spec));
2254 end if;
2255 end if;
2257 Set_Is_Immediately_Visible (Par_Unit, False);
2259 Analyze_Subunit_Context;
2261 Re_Install_Parents (Lib_Unit, Par_Unit);
2262 Set_Is_Immediately_Visible (Par_Unit);
2264 -- If the context includes a child unit of the parent of the subunit,
2265 -- the parent will have been removed from visibility, after compiling
2266 -- that cousin in the context. The visibility of the parent must be
2267 -- restored now. This also applies if the context includes another
2268 -- subunit of the same parent which in turn includes a child unit in
2269 -- its context.
2271 if Is_Package_Or_Generic_Package (Par_Unit) then
2272 if not Is_Immediately_Visible (Par_Unit)
2273 or else (Present (First_Entity (Par_Unit))
2274 and then not Is_Immediately_Visible
2275 (First_Entity (Par_Unit)))
2276 then
2277 Set_Is_Immediately_Visible (Par_Unit);
2278 Install_Visible_Declarations (Par_Unit);
2279 Install_Private_Declarations (Par_Unit);
2280 end if;
2281 end if;
2283 Re_Install_Use_Clauses;
2284 Install_Context (N);
2286 -- Restore state of suppress flags for current body
2288 Scope_Suppress := Svg;
2290 -- If the subunit is within a child unit, then siblings of any parent
2291 -- unit that appear in the context clause of the subunit must also be
2292 -- made immediately visible.
2294 if Present (Enclosing_Child) then
2295 Install_Siblings (Enclosing_Child, N);
2296 end if;
2297 end if;
2299 Analyze (Proper_Body (Unit (N)));
2300 Remove_Context (N);
2302 -- The subunit may contain a with_clause on a sibling of some ancestor.
2303 -- Removing the context will remove from visibility those ancestor child
2304 -- units, which must be restored to the visibility they have in the
2305 -- enclosing body.
2307 if Present (Enclosing_Child) then
2308 declare
2309 C : Entity_Id;
2310 begin
2311 C := Current_Scope;
2312 while Present (C) and then C /= Standard_Standard loop
2313 Set_Is_Immediately_Visible (C);
2314 Set_Is_Visible_Lib_Unit (C);
2315 C := Scope (C);
2316 end loop;
2317 end;
2318 end if;
2320 -- Deal with restore of restrictions
2322 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2323 end Analyze_Subunit;
2325 ----------------------------
2326 -- Analyze_Task_Body_Stub --
2327 ----------------------------
2329 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2330 Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
2331 Loc : constant Source_Ptr := Sloc (N);
2333 begin
2334 Check_Stub_Level (N);
2336 -- First occurrence of name may have been as an incomplete type
2338 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2339 Nam := Full_View (Nam);
2340 end if;
2342 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2343 Error_Msg_N ("missing specification for task body", N);
2344 else
2345 Set_Scope (Defining_Entity (N), Current_Scope);
2346 Generate_Reference (Nam, Defining_Identifier (N), 'b');
2348 -- Check for duplicate stub, if so give message and terminate
2350 if Has_Completion (Etype (Nam)) then
2351 Error_Msg_N ("duplicate stub for task", N);
2352 return;
2353 else
2354 Set_Has_Completion (Etype (Nam));
2355 end if;
2357 Analyze_Proper_Body (N, Etype (Nam));
2359 -- Set elaboration flag to indicate that entity is callable. This
2360 -- cannot be done in the expansion of the body itself, because the
2361 -- proper body is not in a declarative part. This is only done if
2362 -- expansion is active, because the context may be generic and the
2363 -- flag not defined yet.
2365 if Full_Expander_Active then
2366 Insert_After (N,
2367 Make_Assignment_Statement (Loc,
2368 Name =>
2369 Make_Identifier (Loc,
2370 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2371 Expression => New_Reference_To (Standard_True, Loc)));
2372 end if;
2373 end if;
2374 end Analyze_Task_Body_Stub;
2376 -------------------------
2377 -- Analyze_With_Clause --
2378 -------------------------
2380 -- Analyze the declaration of a unit in a with clause. At end, label the
2381 -- with clause with the defining entity for the unit.
2383 procedure Analyze_With_Clause (N : Node_Id) is
2385 -- Retrieve the original kind of the unit node, before analysis. If it
2386 -- is a subprogram instantiation, its analysis below will rewrite the
2387 -- node as the declaration of the wrapper package. If the same
2388 -- instantiation appears indirectly elsewhere in the context, it will
2389 -- have been analyzed already.
2391 Unit_Kind : constant Node_Kind :=
2392 Nkind (Original_Node (Unit (Library_Unit (N))));
2393 Nam : constant Node_Id := Name (N);
2394 E_Name : Entity_Id;
2395 Par_Name : Entity_Id;
2396 Pref : Node_Id;
2397 U : Node_Id;
2399 Intunit : Boolean;
2400 -- Set True if the unit currently being compiled is an internal unit
2402 Restriction_Violation : Boolean := False;
2403 -- Set True if a with violates a restriction, no point in giving any
2404 -- warnings if we have this definite error.
2406 Save_Style_Check : constant Boolean := Opt.Style_Check;
2408 begin
2409 U := Unit (Library_Unit (N));
2411 -- If this is an internal unit which is a renaming, then this is a
2412 -- violation of No_Obsolescent_Features.
2414 -- Note: this is not quite right if the user defines one of these units
2415 -- himself, but that's a marginal case, and fixing it is hard ???
2417 if Restriction_Check_Required (No_Obsolescent_Features) then
2418 declare
2419 F : constant File_Name_Type :=
2420 Unit_File_Name (Get_Source_Unit (U));
2421 begin
2422 if Is_Predefined_File_Name (F, Renamings_Included => True)
2423 and then not
2424 Is_Predefined_File_Name (F, Renamings_Included => False)
2425 then
2426 Check_Restriction (No_Obsolescent_Features, N);
2427 Restriction_Violation := True;
2428 end if;
2429 end;
2430 end if;
2432 -- Check No_Implementation_Units violation
2434 if Restriction_Check_Required (No_Implementation_Units) then
2435 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2436 null;
2437 else
2438 Check_Restriction (No_Implementation_Units, Nam);
2439 Restriction_Violation := True;
2440 end if;
2441 end if;
2443 -- Several actions are skipped for dummy packages (those supplied for
2444 -- with's where no matching file could be found). Such packages are
2445 -- identified by the Sloc value being set to No_Location.
2447 if Limited_Present (N) then
2449 -- Ada 2005 (AI-50217): Build visibility structures but do not
2450 -- analyze the unit.
2452 if Sloc (U) /= No_Location then
2453 Build_Limited_Views (N);
2454 end if;
2456 return;
2457 end if;
2459 -- If the library unit is a predefined unit, and we are in high
2460 -- integrity mode, then temporarily reset Configurable_Run_Time_Mode
2461 -- for the analysis of the with'ed unit. This mode does not prevent
2462 -- explicit with'ing of run-time units.
2464 if Configurable_Run_Time_Mode
2465 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (U)))
2466 then
2467 Configurable_Run_Time_Mode := False;
2468 Semantics (Library_Unit (N));
2469 Configurable_Run_Time_Mode := True;
2471 else
2472 Semantics (Library_Unit (N));
2473 end if;
2475 Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
2477 if Sloc (U) /= No_Location then
2479 -- Check restrictions, except that we skip the check if this is an
2480 -- internal unit unless we are compiling the internal unit as the
2481 -- main unit. We also skip this for dummy packages.
2483 Check_Restriction_No_Dependence (Nam, N);
2485 if not Intunit or else Current_Sem_Unit = Main_Unit then
2486 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2487 end if;
2489 -- Deal with special case of GNAT.Current_Exceptions which interacts
2490 -- with the optimization of local raise statements into gotos.
2492 if Nkind (Nam) = N_Selected_Component
2493 and then Nkind (Prefix (Nam)) = N_Identifier
2494 and then Chars (Prefix (Nam)) = Name_Gnat
2495 and then Nam_In (Chars (Selector_Name (Nam)),
2496 Name_Most_Recent_Exception,
2497 Name_Exception_Traces)
2498 then
2499 Check_Restriction (No_Exception_Propagation, N);
2500 Special_Exception_Package_Used := True;
2501 end if;
2503 -- Check for inappropriate with of internal implementation unit if we
2504 -- are not compiling an internal unit and also check for withing unit
2505 -- in wrong version of Ada. Do not issue these messages for implicit
2506 -- with's generated by the compiler itself.
2508 if Implementation_Unit_Warnings
2509 and then not Intunit
2510 and then not Implicit_With (N)
2511 and then not Restriction_Violation
2512 then
2513 declare
2514 U_Kind : constant Kind_Of_Unit :=
2515 Get_Kind_Of_Unit (Get_Source_Unit (U));
2517 begin
2518 if U_Kind = Implementation_Unit then
2519 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2521 -- Add alternative name if available, otherwise issue a
2522 -- general warning message.
2524 if Error_Msg_Strlen /= 0 then
2525 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2526 else
2527 Error_Msg_F
2528 ("\use of this unit is non-portable " &
2529 "and version-dependent?i?", Name (N));
2530 end if;
2532 elsif U_Kind = Ada_2005_Unit
2533 and then Ada_Version < Ada_2005
2534 and then Warn_On_Ada_2005_Compatibility
2535 then
2536 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2538 elsif U_Kind = Ada_2012_Unit
2539 and then Ada_Version < Ada_2012
2540 and then Warn_On_Ada_2012_Compatibility
2541 then
2542 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2543 end if;
2544 end;
2545 end if;
2546 end if;
2548 -- Semantic analysis of a generic unit is performed on a copy of
2549 -- the original tree. Retrieve the entity on which semantic info
2550 -- actually appears.
2552 if Unit_Kind in N_Generic_Declaration then
2553 E_Name := Defining_Entity (U);
2555 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2556 -- the case of an instantiation, semantic analysis above will have
2557 -- replaced the unit by its instantiated version. If the instance body
2558 -- has been generated, the instance now denotes the body entity. For
2559 -- visibility purposes we need the entity of its spec.
2561 elsif (Unit_Kind = N_Package_Instantiation
2562 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2563 N_Package_Instantiation)
2564 and then Nkind (U) = N_Package_Body
2565 then
2566 E_Name := Corresponding_Spec (U);
2568 elsif Unit_Kind = N_Package_Instantiation
2569 and then Nkind (U) = N_Package_Instantiation
2570 and then Present (Instance_Spec (U))
2571 then
2572 -- If the instance has not been rewritten as a package declaration,
2573 -- then it appeared already in a previous with clause. Retrieve
2574 -- the entity from the previous instance.
2576 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2578 elsif Unit_Kind in N_Subprogram_Instantiation then
2580 -- The visible subprogram is created during instantiation, and is
2581 -- an attribute of the wrapper package. We retrieve the wrapper
2582 -- package directly from the instantiation node. If the instance
2583 -- is inlined the unit is still an instantiation. Otherwise it has
2584 -- been rewritten as the declaration of the wrapper itself.
2586 if Nkind (U) in N_Subprogram_Instantiation then
2587 E_Name :=
2588 Related_Instance
2589 (Defining_Entity (Specification (Instance_Spec (U))));
2590 else
2591 E_Name := Related_Instance (Defining_Entity (U));
2592 end if;
2594 elsif Unit_Kind = N_Package_Renaming_Declaration
2595 or else Unit_Kind in N_Generic_Renaming_Declaration
2596 then
2597 E_Name := Defining_Entity (U);
2599 elsif Unit_Kind = N_Subprogram_Body
2600 and then Nkind (Name (N)) = N_Selected_Component
2601 and then not Acts_As_Spec (Library_Unit (N))
2602 then
2603 -- For a child unit that has no spec, one has been created and
2604 -- analyzed. The entity required is that of the spec.
2606 E_Name := Corresponding_Spec (U);
2608 else
2609 E_Name := Defining_Entity (U);
2610 end if;
2612 if Nkind (Name (N)) = N_Selected_Component then
2614 -- Child unit in a with clause
2616 Change_Selected_Component_To_Expanded_Name (Name (N));
2618 -- If this is a child unit without a spec, and it has been analyzed
2619 -- already, a declaration has been created for it. The with_clause
2620 -- must reflect the actual body, and not the generated declaration,
2621 -- to prevent spurious binding errors involving an out-of-date spec.
2622 -- Note that this can only happen if the unit includes more than one
2623 -- with_clause for the child unit (e.g. in separate subunits).
2625 if Unit_Kind = N_Subprogram_Declaration
2626 and then Analyzed (Library_Unit (N))
2627 and then not Comes_From_Source (Library_Unit (N))
2628 then
2629 Set_Library_Unit (N,
2630 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2631 end if;
2632 end if;
2634 -- Restore style checks
2636 Style_Check := Save_Style_Check;
2638 -- Record the reference, but do NOT set the unit as referenced, we want
2639 -- to consider the unit as unreferenced if this is the only reference
2640 -- that occurs.
2642 Set_Entity_With_Style_Check (Name (N), E_Name);
2643 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
2645 -- Generate references and check No_Dependence restriction for parents
2647 if Is_Child_Unit (E_Name) then
2648 Pref := Prefix (Name (N));
2649 Par_Name := Scope (E_Name);
2650 while Nkind (Pref) = N_Selected_Component loop
2651 Change_Selected_Component_To_Expanded_Name (Pref);
2653 if Present (Entity (Selector_Name (Pref)))
2654 and then
2655 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
2656 and then Entity (Selector_Name (Pref)) /= Par_Name
2657 then
2658 -- The prefix is a child unit that denotes a renaming declaration.
2659 -- Replace the prefix directly with the renamed unit, because the
2660 -- rest of the prefix is irrelevant to the visibility of the real
2661 -- unit.
2663 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
2664 exit;
2665 end if;
2667 Set_Entity_With_Style_Check (Pref, Par_Name);
2669 Generate_Reference (Par_Name, Pref);
2670 Check_Restriction_No_Dependence (Pref, N);
2671 Pref := Prefix (Pref);
2673 -- If E_Name is the dummy entity for a nonexistent unit, its scope
2674 -- is set to Standard_Standard, and no attempt should be made to
2675 -- further unwind scopes.
2677 if Par_Name /= Standard_Standard then
2678 Par_Name := Scope (Par_Name);
2679 end if;
2681 -- Abandon processing in case of previous errors
2683 if No (Par_Name) then
2684 Check_Error_Detected;
2685 return;
2686 end if;
2687 end loop;
2689 if Present (Entity (Pref))
2690 and then not Analyzed (Parent (Parent (Entity (Pref))))
2691 then
2692 -- If the entity is set without its unit being compiled, the
2693 -- original parent is a renaming, and Par_Name is the renamed
2694 -- entity. For visibility purposes, we need the original entity,
2695 -- which must be analyzed now because Load_Unit directly retrieves
2696 -- the renamed unit, and the renaming declaration itself has not
2697 -- been analyzed.
2699 Analyze (Parent (Parent (Entity (Pref))));
2700 pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
2701 Par_Name := Entity (Pref);
2702 end if;
2704 -- Guard against missing or misspelled child units
2706 if Present (Par_Name) then
2707 Set_Entity_With_Style_Check (Pref, Par_Name);
2708 Generate_Reference (Par_Name, Pref);
2710 else
2711 pragma Assert (Serious_Errors_Detected /= 0);
2713 -- Mark the node to indicate that a related error has been posted.
2714 -- This defends further compilation passes against improper use of
2715 -- the invalid WITH clause node.
2717 Set_Error_Posted (N);
2718 Set_Name (N, Error);
2719 return;
2720 end if;
2721 end if;
2723 -- If the withed unit is System, and a system extension pragma is
2724 -- present, compile the extension now, rather than waiting for a
2725 -- visibility check on a specific entity.
2727 if Chars (E_Name) = Name_System
2728 and then Scope (E_Name) = Standard_Standard
2729 and then Present (System_Extend_Unit)
2730 and then Present_System_Aux (N)
2731 then
2732 -- If the extension is not present, an error will have been emitted
2734 null;
2735 end if;
2737 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
2738 -- to private_with units; they will be made visible later (just before
2739 -- the private part is analyzed)
2741 if Private_Present (N) then
2742 Set_Is_Immediately_Visible (E_Name, False);
2743 end if;
2744 end Analyze_With_Clause;
2746 ------------------------------
2747 -- Check_Private_Child_Unit --
2748 ------------------------------
2750 procedure Check_Private_Child_Unit (N : Node_Id) is
2751 Lib_Unit : constant Node_Id := Unit (N);
2752 Item : Node_Id;
2753 Curr_Unit : Entity_Id;
2754 Sub_Parent : Node_Id;
2755 Priv_Child : Entity_Id;
2756 Par_Lib : Entity_Id;
2757 Par_Spec : Node_Id;
2759 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2760 -- Returns true if and only if the library unit is declared with
2761 -- an explicit designation of private.
2763 -----------------------------
2764 -- Is_Private_Library_Unit --
2765 -----------------------------
2767 function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2768 Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2770 begin
2771 return Private_Present (Comp_Unit);
2772 end Is_Private_Library_Unit;
2774 -- Start of processing for Check_Private_Child_Unit
2776 begin
2777 if Nkind_In (Lib_Unit, N_Package_Body, N_Subprogram_Body) then
2778 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2779 Par_Lib := Curr_Unit;
2781 elsif Nkind (Lib_Unit) = N_Subunit then
2783 -- The parent is itself a body. The parent entity is to be found in
2784 -- the corresponding spec.
2786 Sub_Parent := Library_Unit (N);
2787 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2789 -- If the parent itself is a subunit, Curr_Unit is the entity of the
2790 -- enclosing body, retrieve the spec entity which is the proper
2791 -- ancestor we need for the following tests.
2793 if Ekind (Curr_Unit) = E_Package_Body then
2794 Curr_Unit := Spec_Entity (Curr_Unit);
2795 end if;
2797 Par_Lib := Curr_Unit;
2799 else
2800 Curr_Unit := Defining_Entity (Lib_Unit);
2802 Par_Lib := Curr_Unit;
2803 Par_Spec := Parent_Spec (Lib_Unit);
2805 if No (Par_Spec) then
2806 Par_Lib := Empty;
2807 else
2808 Par_Lib := Defining_Entity (Unit (Par_Spec));
2809 end if;
2810 end if;
2812 -- Loop through context items
2814 Item := First (Context_Items (N));
2815 while Present (Item) loop
2817 -- Ada 2005 (AI-262): Allow private_with of a private child package
2818 -- in public siblings
2820 if Nkind (Item) = N_With_Clause
2821 and then not Implicit_With (Item)
2822 and then not Limited_Present (Item)
2823 and then Is_Private_Descendant (Entity (Name (Item)))
2824 then
2825 Priv_Child := Entity (Name (Item));
2827 declare
2828 Curr_Parent : Entity_Id := Par_Lib;
2829 Child_Parent : Entity_Id := Scope (Priv_Child);
2830 Prv_Ancestor : Entity_Id := Child_Parent;
2831 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
2833 begin
2834 -- If the child unit is a public child then locate the nearest
2835 -- private ancestor. Child_Parent will then be set to the
2836 -- parent of that ancestor.
2838 if not Is_Private_Library_Unit (Priv_Child) then
2839 while Present (Prv_Ancestor)
2840 and then not Is_Private_Library_Unit (Prv_Ancestor)
2841 loop
2842 Prv_Ancestor := Scope (Prv_Ancestor);
2843 end loop;
2845 if Present (Prv_Ancestor) then
2846 Child_Parent := Scope (Prv_Ancestor);
2847 end if;
2848 end if;
2850 while Present (Curr_Parent)
2851 and then Curr_Parent /= Standard_Standard
2852 and then Curr_Parent /= Child_Parent
2853 loop
2854 Curr_Private :=
2855 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2856 Curr_Parent := Scope (Curr_Parent);
2857 end loop;
2859 if No (Curr_Parent) then
2860 Curr_Parent := Standard_Standard;
2861 end if;
2863 if Curr_Parent /= Child_Parent then
2864 if Ekind (Priv_Child) = E_Generic_Package
2865 and then Chars (Priv_Child) in Text_IO_Package_Name
2866 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2867 then
2868 Error_Msg_NE
2869 ("& is a nested package, not a compilation unit",
2870 Name (Item), Priv_Child);
2872 else
2873 Error_Msg_N
2874 ("unit in with clause is private child unit!", Item);
2875 Error_Msg_NE
2876 ("\current unit must also have parent&!",
2877 Item, Child_Parent);
2878 end if;
2880 elsif Curr_Private
2881 or else Private_Present (Item)
2882 or else Nkind_In (Lib_Unit, N_Package_Body, N_Subunit)
2883 or else (Nkind (Lib_Unit) = N_Subprogram_Body
2884 and then not Acts_As_Spec (Parent (Lib_Unit)))
2885 then
2886 null;
2888 else
2889 Error_Msg_NE
2890 ("current unit must also be private descendant of&",
2891 Item, Child_Parent);
2892 end if;
2893 end;
2894 end if;
2896 Next (Item);
2897 end loop;
2899 end Check_Private_Child_Unit;
2901 ----------------------
2902 -- Check_Stub_Level --
2903 ----------------------
2905 procedure Check_Stub_Level (N : Node_Id) is
2906 Par : constant Node_Id := Parent (N);
2907 Kind : constant Node_Kind := Nkind (Par);
2909 begin
2910 if Nkind_In (Kind, N_Package_Body,
2911 N_Subprogram_Body,
2912 N_Task_Body,
2913 N_Protected_Body)
2914 and then Nkind_In (Parent (Par), N_Compilation_Unit, N_Subunit)
2915 then
2916 null;
2918 -- In an instance, a missing stub appears at any level. A warning
2919 -- message will have been emitted already for the missing file.
2921 elsif not In_Instance then
2922 Error_Msg_N ("stub cannot appear in an inner scope", N);
2924 elsif Expander_Active then
2925 Error_Msg_N ("missing proper body", N);
2926 end if;
2927 end Check_Stub_Level;
2929 ------------------------
2930 -- Expand_With_Clause --
2931 ------------------------
2933 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
2934 Loc : constant Source_Ptr := Sloc (Nam);
2935 Ent : constant Entity_Id := Entity (Nam);
2936 Withn : Node_Id;
2937 P : Node_Id;
2939 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2940 -- Build name to be used in implicit with_clause. In most cases this
2941 -- is the source name, but if renamings are present we must make the
2942 -- original unit visible, not the one it renames. The entity in the
2943 -- with clause is the renamed unit, but the identifier is the one from
2944 -- the source, which allows us to recover the unit renaming.
2946 ---------------------
2947 -- Build_Unit_Name --
2948 ---------------------
2950 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2951 Ent : Entity_Id;
2952 Result : Node_Id;
2954 begin
2955 if Nkind (Nam) = N_Identifier then
2956 return New_Occurrence_Of (Entity (Nam), Loc);
2958 else
2959 Ent := Entity (Nam);
2961 if Present (Entity (Selector_Name (Nam)))
2962 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
2963 and then
2964 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam))))
2965 = N_Package_Renaming_Declaration
2966 then
2967 -- The name in the with_clause is of the form A.B.C, and B is
2968 -- given by a renaming declaration. In that case we may not
2969 -- have analyzed the unit for B, but replaced it directly in
2970 -- lib-load with the unit it renames. We have to make A.B
2971 -- visible, so analyze the declaration for B now, in case it
2972 -- has not been done yet.
2974 Ent := Entity (Selector_Name (Nam));
2975 Analyze
2976 (Parent
2977 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
2978 end if;
2980 Result :=
2981 Make_Expanded_Name (Loc,
2982 Chars => Chars (Entity (Nam)),
2983 Prefix => Build_Unit_Name (Prefix (Nam)),
2984 Selector_Name => New_Occurrence_Of (Ent, Loc));
2985 Set_Entity (Result, Ent);
2986 return Result;
2987 end if;
2988 end Build_Unit_Name;
2990 -- Start of processing for Expand_With_Clause
2992 begin
2993 Withn :=
2994 Make_With_Clause (Loc,
2995 Name => Build_Unit_Name (Nam));
2997 P := Parent (Unit_Declaration_Node (Ent));
2998 Set_Library_Unit (Withn, P);
2999 Set_Corresponding_Spec (Withn, Ent);
3000 Set_First_Name (Withn, True);
3001 Set_Implicit_With (Withn, True);
3003 -- If the unit is a package or generic package declaration, a private_
3004 -- with_clause on a child unit implies that the implicit with on the
3005 -- parent is also private.
3007 if Nkind_In (Unit (N), N_Package_Declaration,
3008 N_Generic_Package_Declaration)
3009 then
3010 Set_Private_Present (Withn, Private_Present (Item));
3011 end if;
3013 Prepend (Withn, Context_Items (N));
3014 Mark_Rewrite_Insertion (Withn);
3015 Install_Withed_Unit (Withn);
3017 if Nkind (Nam) = N_Expanded_Name then
3018 Expand_With_Clause (Item, Prefix (Nam), N);
3019 end if;
3020 end Expand_With_Clause;
3022 -----------------------
3023 -- Get_Parent_Entity --
3024 -----------------------
3026 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
3027 begin
3028 if Nkind (Unit) = N_Package_Body
3029 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
3030 then
3031 return Defining_Entity
3032 (Specification (Instance_Spec (Original_Node (Unit))));
3033 elsif Nkind (Unit) = N_Package_Instantiation then
3034 return Defining_Entity (Specification (Instance_Spec (Unit)));
3035 else
3036 return Defining_Entity (Unit);
3037 end if;
3038 end Get_Parent_Entity;
3040 ---------------------
3041 -- Has_With_Clause --
3042 ---------------------
3044 function Has_With_Clause
3045 (C_Unit : Node_Id;
3046 Pack : Entity_Id;
3047 Is_Limited : Boolean := False) return Boolean
3049 Item : Node_Id;
3051 function Named_Unit (Clause : Node_Id) return Entity_Id;
3052 -- Return the entity for the unit named in a [limited] with clause
3054 ----------------
3055 -- Named_Unit --
3056 ----------------
3058 function Named_Unit (Clause : Node_Id) return Entity_Id is
3059 begin
3060 if Nkind (Name (Clause)) = N_Selected_Component then
3061 return Entity (Selector_Name (Name (Clause)));
3062 else
3063 return Entity (Name (Clause));
3064 end if;
3065 end Named_Unit;
3067 -- Start of processing for Has_With_Clause
3069 begin
3070 if Present (Context_Items (C_Unit)) then
3071 Item := First (Context_Items (C_Unit));
3072 while Present (Item) loop
3073 if Nkind (Item) = N_With_Clause
3074 and then Limited_Present (Item) = Is_Limited
3075 and then Named_Unit (Item) = Pack
3076 then
3077 return True;
3078 end if;
3080 Next (Item);
3081 end loop;
3082 end if;
3084 return False;
3085 end Has_With_Clause;
3087 -----------------------------
3088 -- Implicit_With_On_Parent --
3089 -----------------------------
3091 procedure Implicit_With_On_Parent
3092 (Child_Unit : Node_Id;
3093 N : Node_Id)
3095 Loc : constant Source_Ptr := Sloc (N);
3096 P : constant Node_Id := Parent_Spec (Child_Unit);
3097 P_Unit : Node_Id := Unit (P);
3098 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3099 Withn : Node_Id;
3101 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3102 -- Build prefix of child unit name. Recurse if needed
3104 function Build_Unit_Name return Node_Id;
3105 -- If the unit is a child unit, build qualified name with all ancestors
3107 -------------------------
3108 -- Build_Ancestor_Name --
3109 -------------------------
3111 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3112 P_Ref : constant Node_Id :=
3113 New_Reference_To (Defining_Entity (P), Loc);
3114 P_Spec : Node_Id := P;
3116 begin
3117 -- Ancestor may have been rewritten as a package body. Retrieve
3118 -- the original spec to trace earlier ancestors.
3120 if Nkind (P) = N_Package_Body
3121 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3122 then
3123 P_Spec := Original_Node (P);
3124 end if;
3126 if No (Parent_Spec (P_Spec)) then
3127 return P_Ref;
3128 else
3129 return
3130 Make_Selected_Component (Loc,
3131 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3132 Selector_Name => P_Ref);
3133 end if;
3134 end Build_Ancestor_Name;
3136 ---------------------
3137 -- Build_Unit_Name --
3138 ---------------------
3140 function Build_Unit_Name return Node_Id is
3141 Result : Node_Id;
3143 begin
3144 if No (Parent_Spec (P_Unit)) then
3145 return New_Reference_To (P_Name, Loc);
3147 else
3148 Result :=
3149 Make_Expanded_Name (Loc,
3150 Chars => Chars (P_Name),
3151 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3152 Selector_Name => New_Reference_To (P_Name, Loc));
3153 Set_Entity (Result, P_Name);
3154 return Result;
3155 end if;
3156 end Build_Unit_Name;
3158 -- Start of processing for Implicit_With_On_Parent
3160 begin
3161 -- The unit of the current compilation may be a package body that
3162 -- replaces an instance node. In this case we need the original instance
3163 -- node to construct the proper parent name.
3165 if Nkind (P_Unit) = N_Package_Body
3166 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3167 then
3168 P_Unit := Original_Node (P_Unit);
3169 end if;
3171 -- We add the implicit with if the child unit is the current unit being
3172 -- compiled. If the current unit is a body, we do not want to add an
3173 -- implicit_with a second time to the corresponding spec.
3175 if Nkind (Child_Unit) = N_Package_Declaration
3176 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3177 then
3178 return;
3179 end if;
3181 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3183 Set_Library_Unit (Withn, P);
3184 Set_Corresponding_Spec (Withn, P_Name);
3185 Set_First_Name (Withn, True);
3186 Set_Implicit_With (Withn, True);
3188 -- Node is placed at the beginning of the context items, so that
3189 -- subsequent use clauses on the parent can be validated.
3191 Prepend (Withn, Context_Items (N));
3192 Mark_Rewrite_Insertion (Withn);
3193 Install_Withed_Unit (Withn);
3195 if Is_Child_Spec (P_Unit) then
3196 Implicit_With_On_Parent (P_Unit, N);
3197 end if;
3198 end Implicit_With_On_Parent;
3200 --------------
3201 -- In_Chain --
3202 --------------
3204 function In_Chain (E : Entity_Id) return Boolean is
3205 H : Entity_Id;
3207 begin
3208 H := Current_Entity (E);
3209 while Present (H) loop
3210 if H = E then
3211 return True;
3212 else
3213 H := Homonym (H);
3214 end if;
3215 end loop;
3217 return False;
3218 end In_Chain;
3220 ---------------------
3221 -- Install_Context --
3222 ---------------------
3224 procedure Install_Context (N : Node_Id) is
3225 Lib_Unit : constant Node_Id := Unit (N);
3227 begin
3228 Install_Context_Clauses (N);
3230 if Is_Child_Spec (Lib_Unit) then
3231 Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
3232 end if;
3234 Install_Limited_Context_Clauses (N);
3235 end Install_Context;
3237 -----------------------------
3238 -- Install_Context_Clauses --
3239 -----------------------------
3241 procedure Install_Context_Clauses (N : Node_Id) is
3242 Lib_Unit : constant Node_Id := Unit (N);
3243 Item : Node_Id;
3244 Uname_Node : Entity_Id;
3245 Check_Private : Boolean := False;
3246 Decl_Node : Node_Id;
3247 Lib_Parent : Entity_Id;
3249 begin
3250 -- First skip configuration pragmas at the start of the context. They
3251 -- are not technically part of the context clause, but that's where the
3252 -- parser puts them. Note they were analyzed in Analyze_Context.
3254 Item := First (Context_Items (N));
3255 while Present (Item)
3256 and then Nkind (Item) = N_Pragma
3257 and then Pragma_Name (Item) in Configuration_Pragma_Names
3258 loop
3259 Next (Item);
3260 end loop;
3262 -- Loop through the actual context clause items. We process everything
3263 -- except Limited_With clauses in this routine. Limited_With clauses
3264 -- are separately installed (see Install_Limited_Context_Clauses).
3266 while Present (Item) loop
3268 -- Case of explicit WITH clause
3270 if Nkind (Item) = N_With_Clause
3271 and then not Implicit_With (Item)
3272 then
3273 if Limited_Present (Item) then
3275 -- Limited withed units will be installed later
3277 goto Continue;
3279 -- If Name (Item) is not an entity name, something is wrong, and
3280 -- this will be detected in due course, for now ignore the item
3282 elsif not Is_Entity_Name (Name (Item)) then
3283 goto Continue;
3285 elsif No (Entity (Name (Item))) then
3286 Set_Entity (Name (Item), Any_Id);
3287 goto Continue;
3288 end if;
3290 Uname_Node := Entity (Name (Item));
3292 if Is_Private_Descendant (Uname_Node) then
3293 Check_Private := True;
3294 end if;
3296 Install_Withed_Unit (Item);
3298 Decl_Node := Unit_Declaration_Node (Uname_Node);
3300 -- If the unit is a subprogram instance, it appears nested within
3301 -- a package that carries the parent information.
3303 if Is_Generic_Instance (Uname_Node)
3304 and then Ekind (Uname_Node) /= E_Package
3305 then
3306 Decl_Node := Parent (Parent (Decl_Node));
3307 end if;
3309 if Is_Child_Spec (Decl_Node) then
3310 if Nkind (Name (Item)) = N_Expanded_Name then
3311 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3312 else
3313 -- If not an expanded name, the child unit must be a
3314 -- renaming, nothing to do.
3316 null;
3317 end if;
3319 elsif Nkind (Decl_Node) = N_Subprogram_Body
3320 and then not Acts_As_Spec (Parent (Decl_Node))
3321 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3322 then
3323 Implicit_With_On_Parent
3324 (Unit (Library_Unit (Parent (Decl_Node))), N);
3325 end if;
3327 -- Check license conditions unless this is a dummy unit
3329 if Sloc (Library_Unit (Item)) /= No_Location then
3330 License_Check : declare
3331 Withu : constant Unit_Number_Type :=
3332 Get_Source_Unit (Library_Unit (Item));
3333 Withl : constant License_Type :=
3334 License (Source_Index (Withu));
3335 Unitl : constant License_Type :=
3336 License (Source_Index (Current_Sem_Unit));
3338 procedure License_Error;
3339 -- Signal error of bad license
3341 -------------------
3342 -- License_Error --
3343 -------------------
3345 procedure License_Error is
3346 begin
3347 Error_Msg_N
3348 ("license of withed unit & may be inconsistent??",
3349 Name (Item));
3350 end License_Error;
3352 -- Start of processing for License_Check
3354 begin
3355 -- Exclude license check if withed unit is an internal unit.
3356 -- This situation arises e.g. with the GPL version of GNAT.
3358 if Is_Internal_File_Name (Unit_File_Name (Withu)) then
3359 null;
3361 -- Otherwise check various cases
3362 else
3363 case Unitl is
3364 when Unknown =>
3365 null;
3367 when Restricted =>
3368 if Withl = GPL then
3369 License_Error;
3370 end if;
3372 when GPL =>
3373 if Withl = Restricted then
3374 License_Error;
3375 end if;
3377 when Modified_GPL =>
3378 if Withl = Restricted or else Withl = GPL then
3379 License_Error;
3380 end if;
3382 when Unrestricted =>
3383 null;
3384 end case;
3385 end if;
3386 end License_Check;
3387 end if;
3389 -- Case of USE PACKAGE clause
3391 elsif Nkind (Item) = N_Use_Package_Clause then
3392 Analyze_Use_Package (Item);
3394 -- Case of USE TYPE clause
3396 elsif Nkind (Item) = N_Use_Type_Clause then
3397 Analyze_Use_Type (Item);
3399 -- case of PRAGMA
3401 elsif Nkind (Item) = N_Pragma then
3402 Analyze (Item);
3403 end if;
3405 <<Continue>>
3406 Next (Item);
3407 end loop;
3409 if Is_Child_Spec (Lib_Unit) then
3411 -- The unit also has implicit with_clauses on its own parents
3413 if No (Context_Items (N)) then
3414 Set_Context_Items (N, New_List);
3415 end if;
3417 Implicit_With_On_Parent (Lib_Unit, N);
3418 end if;
3420 -- If the unit is a body, the context of the specification must also
3421 -- be installed. That includes private with_clauses in that context.
3423 if Nkind (Lib_Unit) = N_Package_Body
3424 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3425 and then not Acts_As_Spec (N))
3426 then
3427 Install_Context (Library_Unit (N));
3429 -- Only install private with-clauses of a spec that comes from
3430 -- source, excluding specs created for a subprogram body that is
3431 -- a child unit.
3433 if Comes_From_Source (Library_Unit (N)) then
3434 Install_Private_With_Clauses
3435 (Defining_Entity (Unit (Library_Unit (N))));
3436 end if;
3438 if Is_Child_Spec (Unit (Library_Unit (N))) then
3440 -- If the unit is the body of a public child unit, the private
3441 -- declarations of the parent must be made visible. If the child
3442 -- unit is private, the private declarations have been installed
3443 -- already in the call to Install_Parents for the spec. Installing
3444 -- private declarations must be done for all ancestors of public
3445 -- child units. In addition, sibling units mentioned in the
3446 -- context clause of the body are directly visible.
3448 declare
3449 Lib_Spec : Node_Id;
3450 P : Node_Id;
3451 P_Name : Entity_Id;
3453 begin
3454 Lib_Spec := Unit (Library_Unit (N));
3455 while Is_Child_Spec (Lib_Spec) loop
3456 P := Unit (Parent_Spec (Lib_Spec));
3457 P_Name := Defining_Entity (P);
3459 if not (Private_Present (Parent (Lib_Spec)))
3460 and then not In_Private_Part (P_Name)
3461 then
3462 Install_Private_Declarations (P_Name);
3463 Install_Private_With_Clauses (P_Name);
3464 Set_Use (Private_Declarations (Specification (P)));
3465 end if;
3467 Lib_Spec := P;
3468 end loop;
3469 end;
3470 end if;
3472 -- For a package body, children in context are immediately visible
3474 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3475 end if;
3477 if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3478 N_Generic_Subprogram_Declaration,
3479 N_Package_Declaration,
3480 N_Subprogram_Declaration)
3481 then
3482 if Is_Child_Spec (Lib_Unit) then
3483 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3484 Set_Is_Private_Descendant
3485 (Defining_Entity (Lib_Unit),
3486 Is_Private_Descendant (Lib_Parent)
3487 or else Private_Present (Parent (Lib_Unit)));
3489 else
3490 Set_Is_Private_Descendant
3491 (Defining_Entity (Lib_Unit),
3492 Private_Present (Parent (Lib_Unit)));
3493 end if;
3494 end if;
3496 if Check_Private then
3497 Check_Private_Child_Unit (N);
3498 end if;
3499 end Install_Context_Clauses;
3501 -------------------------------------
3502 -- Install_Limited_Context_Clauses --
3503 -------------------------------------
3505 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3506 Item : Node_Id;
3508 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3509 -- Check that the unlimited view of a given compilation_unit is not
3510 -- already visible through "use + renamings".
3512 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3513 -- Check that if a limited_with clause of a given compilation_unit
3514 -- mentions a descendant of a private child of some library unit, then
3515 -- the given compilation_unit shall be the declaration of a private
3516 -- descendant of that library unit, or a public descendant of such. The
3517 -- code is analogous to that of Check_Private_Child_Unit but we cannot
3518 -- use entities on the limited with_clauses because their units have not
3519 -- been analyzed, so we have to climb the tree of ancestors looking for
3520 -- private keywords.
3522 procedure Expand_Limited_With_Clause
3523 (Comp_Unit : Node_Id;
3524 Nam : Node_Id;
3525 N : Node_Id);
3526 -- If a child unit appears in a limited_with clause, there are implicit
3527 -- limited_with clauses on all parents that are not already visible
3528 -- through a regular with clause. This procedure creates the implicit
3529 -- limited with_clauses for the parents and loads the corresponding
3530 -- units. The shadow entities are created when the inserted clause is
3531 -- analyzed. Implements Ada 2005 (AI-50217).
3533 ---------------------
3534 -- Check_Renamings --
3535 ---------------------
3537 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3538 Item : Node_Id;
3539 Spec : Node_Id;
3540 WEnt : Entity_Id;
3541 Nam : Node_Id;
3542 E : Entity_Id;
3543 E2 : Entity_Id;
3545 begin
3546 pragma Assert (Nkind (W) = N_With_Clause);
3548 -- Protect the frontend against previous critical errors
3550 case Nkind (Unit (Library_Unit (W))) is
3551 when N_Subprogram_Declaration |
3552 N_Package_Declaration |
3553 N_Generic_Subprogram_Declaration |
3554 N_Generic_Package_Declaration =>
3555 null;
3557 when others =>
3558 return;
3559 end case;
3561 -- Check "use + renamings"
3563 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3564 Spec := Specification (Unit (P));
3566 Item := First (Visible_Declarations (Spec));
3567 while Present (Item) loop
3569 -- Look only at use package clauses
3571 if Nkind (Item) = N_Use_Package_Clause then
3573 -- Traverse the list of packages
3575 Nam := First (Names (Item));
3576 while Present (Nam) loop
3577 E := Entity (Nam);
3579 pragma Assert (Present (Parent (E)));
3581 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3582 and then Renamed_Entity (E) = WEnt
3583 then
3584 -- The unlimited view is visible through use clause and
3585 -- renamings. There is no need to generate the error
3586 -- message here because Is_Visible_Through_Renamings
3587 -- takes care of generating the precise error message.
3589 return;
3591 elsif Nkind (Parent (E)) = N_Package_Specification then
3593 -- The use clause may refer to a local package.
3594 -- Check all the enclosing scopes.
3596 E2 := E;
3597 while E2 /= Standard_Standard
3598 and then E2 /= WEnt
3599 loop
3600 E2 := Scope (E2);
3601 end loop;
3603 if E2 = WEnt then
3604 Error_Msg_N
3605 ("unlimited view visible through use clause ", W);
3606 return;
3607 end if;
3608 end if;
3610 Next (Nam);
3611 end loop;
3612 end if;
3614 Next (Item);
3615 end loop;
3617 -- Recursive call to check all the ancestors
3619 if Is_Child_Spec (Unit (P)) then
3620 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3621 end if;
3622 end Check_Renamings;
3624 ---------------------------------------
3625 -- Check_Private_Limited_Withed_Unit --
3626 ---------------------------------------
3628 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3629 Curr_Parent : Node_Id;
3630 Child_Parent : Node_Id;
3631 Curr_Private : Boolean;
3633 begin
3634 -- Compilation unit of the parent of the withed library unit
3636 Child_Parent := Library_Unit (Item);
3638 -- If the child unit is a public child, then locate its nearest
3639 -- private ancestor, if any, then Child_Parent will then be set to
3640 -- the parent of that ancestor.
3642 if not Private_Present (Library_Unit (Item)) then
3643 while Present (Child_Parent)
3644 and then not Private_Present (Child_Parent)
3645 loop
3646 Child_Parent := Parent_Spec (Unit (Child_Parent));
3647 end loop;
3649 if No (Child_Parent) then
3650 return;
3651 end if;
3652 end if;
3654 Child_Parent := Parent_Spec (Unit (Child_Parent));
3656 -- Traverse all the ancestors of the current compilation unit to
3657 -- check if it is a descendant of named library unit.
3659 Curr_Parent := Parent (Item);
3660 Curr_Private := Private_Present (Curr_Parent);
3662 while Present (Parent_Spec (Unit (Curr_Parent)))
3663 and then Curr_Parent /= Child_Parent
3664 loop
3665 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3666 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3667 end loop;
3669 if Curr_Parent /= Child_Parent then
3670 Error_Msg_N
3671 ("unit in with clause is private child unit!", Item);
3672 Error_Msg_NE
3673 ("\current unit must also have parent&!",
3674 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3676 elsif Private_Present (Parent (Item))
3677 or else Curr_Private
3678 or else Private_Present (Item)
3679 or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3680 N_Subprogram_Body,
3681 N_Subunit)
3682 then
3683 -- Current unit is private, of descendant of a private unit
3685 null;
3687 else
3688 Error_Msg_NE
3689 ("current unit must also be private descendant of&",
3690 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3691 end if;
3692 end Check_Private_Limited_Withed_Unit;
3694 --------------------------------
3695 -- Expand_Limited_With_Clause --
3696 --------------------------------
3698 procedure Expand_Limited_With_Clause
3699 (Comp_Unit : Node_Id;
3700 Nam : Node_Id;
3701 N : Node_Id)
3703 Loc : constant Source_Ptr := Sloc (Nam);
3704 Unum : Unit_Number_Type;
3705 Withn : Node_Id;
3707 function Previous_Withed_Unit (W : Node_Id) return Boolean;
3708 -- Returns true if the context already includes a with_clause for
3709 -- this unit. If the with_clause is non-limited, the unit is fully
3710 -- visible and an implicit limited_with should not be created. If
3711 -- there is already a limited_with clause for W, a second one is
3712 -- simply redundant.
3714 --------------------------
3715 -- Previous_Withed_Unit --
3716 --------------------------
3718 function Previous_Withed_Unit (W : Node_Id) return Boolean is
3719 Item : Node_Id;
3721 begin
3722 -- A limited with_clause cannot appear in the same context_clause
3723 -- as a nonlimited with_clause which mentions the same library.
3725 Item := First (Context_Items (Comp_Unit));
3726 while Present (Item) loop
3727 if Nkind (Item) = N_With_Clause
3728 and then Library_Unit (Item) = Library_Unit (W)
3729 then
3730 return True;
3731 end if;
3733 Next (Item);
3734 end loop;
3736 return False;
3737 end Previous_Withed_Unit;
3739 -- Start of processing for Expand_Limited_With_Clause
3741 begin
3742 if Nkind (Nam) = N_Identifier then
3744 -- Create node for name of withed unit
3746 Withn :=
3747 Make_With_Clause (Loc,
3748 Name => New_Copy (Nam));
3750 else pragma Assert (Nkind (Nam) = N_Selected_Component);
3751 Withn :=
3752 Make_With_Clause (Loc,
3753 Name => Make_Selected_Component (Loc,
3754 Prefix => New_Copy_Tree (Prefix (Nam)),
3755 Selector_Name => New_Copy (Selector_Name (Nam))));
3756 Set_Parent (Withn, Parent (N));
3757 end if;
3759 Set_Limited_Present (Withn);
3760 Set_First_Name (Withn);
3761 Set_Implicit_With (Withn);
3763 Unum :=
3764 Load_Unit
3765 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
3766 Required => True,
3767 Subunit => False,
3768 Error_Node => Nam);
3770 -- Do not generate a limited_with_clause on the current unit. This
3771 -- path is taken when a unit has a limited_with clause on one of its
3772 -- child units.
3774 if Unum = Current_Sem_Unit then
3775 return;
3776 end if;
3778 Set_Library_Unit (Withn, Cunit (Unum));
3779 Set_Corresponding_Spec
3780 (Withn, Specification (Unit (Cunit (Unum))));
3782 if not Previous_Withed_Unit (Withn) then
3783 Prepend (Withn, Context_Items (Parent (N)));
3784 Mark_Rewrite_Insertion (Withn);
3786 -- Add implicit limited_with_clauses for parents of child units
3787 -- mentioned in limited_with clauses.
3789 if Nkind (Nam) = N_Selected_Component then
3790 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3791 end if;
3793 Analyze (Withn);
3795 if not Limited_View_Installed (Withn) then
3796 Install_Limited_Withed_Unit (Withn);
3797 end if;
3798 end if;
3799 end Expand_Limited_With_Clause;
3801 -- Start of processing for Install_Limited_Context_Clauses
3803 begin
3804 Item := First (Context_Items (N));
3805 while Present (Item) loop
3806 if Nkind (Item) = N_With_Clause
3807 and then Limited_Present (Item)
3808 and then not Error_Posted (Item)
3809 then
3810 if Nkind (Name (Item)) = N_Selected_Component then
3811 Expand_Limited_With_Clause
3812 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3813 end if;
3815 Check_Private_Limited_Withed_Unit (Item);
3817 if not Implicit_With (Item)
3818 and then Is_Child_Spec (Unit (N))
3819 then
3820 Check_Renamings (Parent_Spec (Unit (N)), Item);
3821 end if;
3823 -- A unit may have a limited with on itself if it has a limited
3824 -- with_clause on one of its child units. In that case it is
3825 -- already being compiled and it makes no sense to install its
3826 -- limited view.
3828 -- If the item is a limited_private_with_clause, install it if the
3829 -- current unit is a body or if it is a private child. Otherwise
3830 -- the private clause is installed before analyzing the private
3831 -- part of the current unit.
3833 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3834 and then not Limited_View_Installed (Item)
3835 and then
3836 not Is_Ancestor_Unit
3837 (Library_Unit (Item), Cunit (Current_Sem_Unit))
3838 then
3839 if not Private_Present (Item)
3840 or else Private_Present (N)
3841 or else Nkind_In (Unit (N), N_Package_Body,
3842 N_Subprogram_Body,
3843 N_Subunit)
3844 then
3845 Install_Limited_Withed_Unit (Item);
3846 end if;
3847 end if;
3848 end if;
3850 Next (Item);
3851 end loop;
3853 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
3854 -- looking for incomplete subtype declarations of incomplete types
3855 -- visible through a limited with clause.
3857 if Ada_Version >= Ada_2005
3858 and then Analyzed (N)
3859 and then Nkind (Unit (N)) = N_Package_Declaration
3860 then
3861 declare
3862 Decl : Node_Id;
3863 Def_Id : Entity_Id;
3864 Non_Lim_View : Entity_Id;
3866 begin
3867 Decl := First (Visible_Declarations (Specification (Unit (N))));
3868 while Present (Decl) loop
3869 if Nkind (Decl) = N_Subtype_Declaration
3870 and then
3871 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3872 and then
3873 From_With_Type (Defining_Identifier (Decl))
3874 then
3875 Def_Id := Defining_Identifier (Decl);
3876 Non_Lim_View := Non_Limited_View (Def_Id);
3878 if not Is_Incomplete_Type (Non_Lim_View) then
3880 -- Convert an incomplete subtype declaration into a
3881 -- corresponding non-limited view subtype declaration.
3882 -- This is usually the case when analyzing a body that
3883 -- has regular with clauses, when the spec has limited
3884 -- ones.
3886 -- If the non-limited view is still incomplete, it is
3887 -- the dummy entry already created, and the declaration
3888 -- cannot be reanalyzed. This is the case when installing
3889 -- a parent unit that has limited with-clauses.
3891 Set_Subtype_Indication (Decl,
3892 New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3893 Set_Etype (Def_Id, Non_Lim_View);
3894 Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3895 Set_Analyzed (Decl, False);
3897 -- Reanalyze the declaration, suppressing the call to
3898 -- Enter_Name to avoid duplicate names.
3900 Analyze_Subtype_Declaration
3901 (N => Decl,
3902 Skip => True);
3903 end if;
3904 end if;
3906 Next (Decl);
3907 end loop;
3908 end;
3909 end if;
3910 end Install_Limited_Context_Clauses;
3912 ---------------------
3913 -- Install_Parents --
3914 ---------------------
3916 procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3917 P : Node_Id;
3918 E_Name : Entity_Id;
3919 P_Name : Entity_Id;
3920 P_Spec : Node_Id;
3922 begin
3923 P := Unit (Parent_Spec (Lib_Unit));
3924 P_Name := Get_Parent_Entity (P);
3926 if Etype (P_Name) = Any_Type then
3927 return;
3928 end if;
3930 if Ekind (P_Name) = E_Generic_Package
3931 and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3932 N_Generic_Package_Declaration)
3933 and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3934 then
3935 Error_Msg_N
3936 ("child of a generic package must be a generic unit", Lib_Unit);
3938 elsif not Is_Package_Or_Generic_Package (P_Name) then
3939 Error_Msg_N
3940 ("parent unit must be package or generic package", Lib_Unit);
3941 raise Unrecoverable_Error;
3943 elsif Present (Renamed_Object (P_Name)) then
3944 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3945 raise Unrecoverable_Error;
3947 -- Verify that a child of an instance is itself an instance, or the
3948 -- renaming of one. Given that an instance that is a unit is replaced
3949 -- with a package declaration, check against the original node. The
3950 -- parent may be currently being instantiated, in which case it appears
3951 -- as a declaration, but the generic_parent is already established
3952 -- indicating that we deal with an instance.
3954 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3955 if Nkind (Lib_Unit) in N_Renaming_Declaration
3956 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3957 or else
3958 (Nkind (Lib_Unit) = N_Package_Declaration
3959 and then Present (Generic_Parent (Specification (Lib_Unit))))
3960 then
3961 null;
3962 else
3963 Error_Msg_N
3964 ("child of an instance must be an instance or renaming",
3965 Lib_Unit);
3966 end if;
3967 end if;
3969 -- This is the recursive call that ensures all parents are loaded
3971 if Is_Child_Spec (P) then
3972 Install_Parents (P,
3973 Is_Private or else Private_Present (Parent (Lib_Unit)));
3974 end if;
3976 -- Now we can install the context for this parent
3978 Install_Context_Clauses (Parent_Spec (Lib_Unit));
3979 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3980 Install_Siblings (P_Name, Parent (Lib_Unit));
3982 -- The child unit is in the declarative region of the parent. The parent
3983 -- must therefore appear in the scope stack and be visible, as when
3984 -- compiling the corresponding body. If the child unit is private or it
3985 -- is a package body, private declarations must be accessible as well.
3986 -- Use declarations in the parent must also be installed. Finally, other
3987 -- child units of the same parent that are in the context are
3988 -- immediately visible.
3990 -- Find entity for compilation unit, and set its private descendant
3991 -- status as needed. Indicate that it is a compilation unit, which is
3992 -- redundant in general, but needed if this is a generated child spec
3993 -- for a child body without previous spec.
3995 E_Name := Defining_Entity (Lib_Unit);
3997 Set_Is_Child_Unit (E_Name);
3998 Set_Is_Compilation_Unit (E_Name);
4000 Set_Is_Private_Descendant (E_Name,
4001 Is_Private_Descendant (P_Name)
4002 or else Private_Present (Parent (Lib_Unit)));
4004 P_Spec := Specification (Unit_Declaration_Node (P_Name));
4005 Push_Scope (P_Name);
4007 -- Save current visibility of unit
4009 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4010 Is_Immediately_Visible (P_Name);
4011 Set_Is_Immediately_Visible (P_Name);
4012 Install_Visible_Declarations (P_Name);
4013 Set_Use (Visible_Declarations (P_Spec));
4015 -- If the parent is a generic unit, its formal part may contain formal
4016 -- packages and use clauses for them.
4018 if Ekind (P_Name) = E_Generic_Package then
4019 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4020 end if;
4022 if Is_Private
4023 or else Private_Present (Parent (Lib_Unit))
4024 then
4025 Install_Private_Declarations (P_Name);
4026 Install_Private_With_Clauses (P_Name);
4027 Set_Use (Private_Declarations (P_Spec));
4028 end if;
4029 end Install_Parents;
4031 ----------------------------------
4032 -- Install_Private_With_Clauses --
4033 ----------------------------------
4035 procedure Install_Private_With_Clauses (P : Entity_Id) is
4036 Decl : constant Node_Id := Unit_Declaration_Node (P);
4037 Item : Node_Id;
4039 begin
4040 if Debug_Flag_I then
4041 Write_Str ("install private with clauses of ");
4042 Write_Name (Chars (P));
4043 Write_Eol;
4044 end if;
4046 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4047 Item := First (Context_Items (Parent (Decl)));
4048 while Present (Item) loop
4049 if Nkind (Item) = N_With_Clause
4050 and then Private_Present (Item)
4051 then
4052 -- If the unit is an ancestor of the current one, it is the
4053 -- case of a private limited with clause on a child unit, and
4054 -- the compilation of one of its descendants, In that case the
4055 -- limited view is errelevant.
4057 if Limited_Present (Item) then
4058 if not Limited_View_Installed (Item)
4059 and then
4060 not Is_Ancestor_Unit (Library_Unit (Item),
4061 Cunit (Current_Sem_Unit))
4062 then
4063 Install_Limited_Withed_Unit (Item);
4064 end if;
4065 else
4066 Install_Withed_Unit (Item, Private_With_OK => True);
4067 end if;
4068 end if;
4070 Next (Item);
4071 end loop;
4072 end if;
4073 end Install_Private_With_Clauses;
4075 ----------------------
4076 -- Install_Siblings --
4077 ----------------------
4079 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4080 Item : Node_Id;
4081 Id : Entity_Id;
4082 Prev : Entity_Id;
4084 begin
4085 -- Iterate over explicit with clauses, and check whether the scope of
4086 -- each entity is an ancestor of the current unit, in which case it is
4087 -- immediately visible.
4089 Item := First (Context_Items (N));
4090 while Present (Item) loop
4092 -- Do not install private_with_clauses declaration, unless unit
4093 -- is itself a private child unit, or is a body. Note that for a
4094 -- subprogram body the private_with_clause does not take effect until
4095 -- after the specification.
4097 if Nkind (Item) /= N_With_Clause
4098 or else Implicit_With (Item)
4099 or else Limited_Present (Item)
4100 or else Error_Posted (Item)
4101 then
4102 null;
4104 elsif not Private_Present (Item)
4105 or else Private_Present (N)
4106 or else Nkind (Unit (N)) = N_Package_Body
4107 then
4108 Id := Entity (Name (Item));
4110 if Is_Child_Unit (Id)
4111 and then Is_Ancestor_Package (Scope (Id), U_Name)
4112 then
4113 Set_Is_Immediately_Visible (Id);
4115 -- Check for the presence of another unit in the context that
4116 -- may be inadvertently hidden by the child.
4118 Prev := Current_Entity (Id);
4120 if Present (Prev)
4121 and then Is_Immediately_Visible (Prev)
4122 and then not Is_Child_Unit (Prev)
4123 then
4124 declare
4125 Clause : Node_Id;
4127 begin
4128 Clause := First (Context_Items (N));
4129 while Present (Clause) loop
4130 if Nkind (Clause) = N_With_Clause
4131 and then Entity (Name (Clause)) = Prev
4132 then
4133 Error_Msg_NE
4134 ("child unit& hides compilation unit " &
4135 "with the same name??",
4136 Name (Item), Id);
4137 exit;
4138 end if;
4140 Next (Clause);
4141 end loop;
4142 end;
4143 end if;
4145 -- The With_Clause may be on a grand-child or one of its further
4146 -- descendants, which makes a child immediately visible. Examine
4147 -- ancestry to determine whether such a child exists. For example,
4148 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4149 -- is immediately visible.
4151 elsif Is_Child_Unit (Id) then
4152 declare
4153 Par : Entity_Id;
4155 begin
4156 Par := Scope (Id);
4157 while Is_Child_Unit (Par) loop
4158 if Is_Ancestor_Package (Scope (Par), U_Name) then
4159 Set_Is_Immediately_Visible (Par);
4160 exit;
4161 end if;
4163 Par := Scope (Par);
4164 end loop;
4165 end;
4166 end if;
4168 -- If the item is a private with-clause on a child unit, the parent
4169 -- may have been installed already, but the child unit must remain
4170 -- invisible until installed in a private part or body, unless there
4171 -- is already a regular with_clause for it in the current unit.
4173 elsif Private_Present (Item) then
4174 Id := Entity (Name (Item));
4176 if Is_Child_Unit (Id) then
4177 declare
4178 Clause : Node_Id;
4180 function In_Context return Boolean;
4181 -- Scan context of current unit, to check whether there is
4182 -- a with_clause on the same unit as a private with-clause
4183 -- on a parent, in which case child unit is visible. If the
4184 -- unit is a grand-child, the same applies to its parent.
4186 ----------------
4187 -- In_Context --
4188 ----------------
4190 function In_Context return Boolean is
4191 begin
4192 Clause :=
4193 First (Context_Items (Cunit (Current_Sem_Unit)));
4194 while Present (Clause) loop
4195 if Nkind (Clause) = N_With_Clause
4196 and then Comes_From_Source (Clause)
4197 and then Is_Entity_Name (Name (Clause))
4198 and then not Private_Present (Clause)
4199 then
4200 if Entity (Name (Clause)) = Id
4201 or else
4202 (Nkind (Name (Clause)) = N_Expanded_Name
4203 and then Entity (Prefix (Name (Clause))) = Id)
4204 then
4205 return True;
4206 end if;
4207 end if;
4209 Next (Clause);
4210 end loop;
4212 return False;
4213 end In_Context;
4215 begin
4216 Set_Is_Visible_Lib_Unit (Id, In_Context);
4217 end;
4218 end if;
4219 end if;
4221 Next (Item);
4222 end loop;
4223 end Install_Siblings;
4225 ---------------------------------
4226 -- Install_Limited_Withed_Unit --
4227 ---------------------------------
4229 procedure Install_Limited_Withed_Unit (N : Node_Id) is
4230 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4231 E : Entity_Id;
4232 P : Entity_Id;
4233 Is_Child_Package : Boolean := False;
4234 Lim_Header : Entity_Id;
4235 Lim_Typ : Entity_Id;
4237 procedure Check_Body_Required;
4238 -- A unit mentioned in a limited with_clause may not be mentioned in
4239 -- a regular with_clause, but must still be included in the current
4240 -- partition. We need to determine whether the unit needs a body, so
4241 -- that the binder can determine the name of the file to be compiled.
4242 -- Checking whether a unit needs a body can be done without semantic
4243 -- analysis, by examining the nature of the declarations in the package.
4245 function Has_Limited_With_Clause
4246 (C_Unit : Entity_Id;
4247 Pack : Entity_Id) return Boolean;
4248 -- Determine whether any package in the ancestor chain starting with
4249 -- C_Unit has a limited with clause for package Pack.
4251 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
4252 -- Check if some package installed though normal with-clauses has a
4253 -- renaming declaration of package P. AARM 10.1.2(21/2).
4255 -------------------------
4256 -- Check_Body_Required --
4257 -------------------------
4259 procedure Check_Body_Required is
4260 PA : constant List_Id :=
4261 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4263 procedure Check_Declarations (Spec : Node_Id);
4264 -- Recursive procedure that does the work and checks nested packages
4266 ------------------------
4267 -- Check_Declarations --
4268 ------------------------
4270 procedure Check_Declarations (Spec : Node_Id) is
4271 Decl : Node_Id;
4272 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4274 Subp_List : constant Elist_Id := New_Elmt_List;
4276 procedure Check_Pragma_Import (P : Node_Id);
4277 -- If a pragma import applies to a previous subprogram, the
4278 -- enclosing unit may not need a body. The processing is syntactic
4279 -- and does not require a declaration to be analyzed. The code
4280 -- below also handles pragma Import when applied to a subprogram
4281 -- that renames another. In this case the pragma applies to the
4282 -- renamed entity.
4284 -- Chains of multiple renames are not handled by the code below.
4285 -- It is probably impossible to handle all cases without proper
4286 -- name resolution. In such cases the algorithm is conservative
4287 -- and will indicate that a body is needed???
4289 -------------------------
4290 -- Check_Pragma_Import --
4291 -------------------------
4293 procedure Check_Pragma_Import (P : Node_Id) is
4294 Arg : Node_Id;
4295 Prev_Id : Elmt_Id;
4296 Subp_Id : Elmt_Id;
4297 Imported : Node_Id;
4299 procedure Remove_Homonyms (E : Node_Id);
4300 -- Make one pass over list of subprograms. Called again if
4301 -- subprogram is a renaming. E is known to be an identifier.
4303 ---------------------
4304 -- Remove_Homonyms --
4305 ---------------------
4307 procedure Remove_Homonyms (E : Node_Id) is
4308 R : Entity_Id := Empty;
4309 -- Name of renamed entity, if any
4311 begin
4312 Subp_Id := First_Elmt (Subp_List);
4313 while Present (Subp_Id) loop
4314 if Chars (Node (Subp_Id)) = Chars (E) then
4315 if Nkind (Parent (Parent (Node (Subp_Id))))
4316 /= N_Subprogram_Renaming_Declaration
4317 then
4318 Prev_Id := Subp_Id;
4319 Next_Elmt (Subp_Id);
4320 Remove_Elmt (Subp_List, Prev_Id);
4321 else
4322 R := Name (Parent (Parent (Node (Subp_Id))));
4323 exit;
4324 end if;
4325 else
4326 Next_Elmt (Subp_Id);
4327 end if;
4328 end loop;
4330 if Present (R) then
4331 if Nkind (R) = N_Identifier then
4332 Remove_Homonyms (R);
4334 elsif Nkind (R) = N_Selected_Component then
4335 Remove_Homonyms (Selector_Name (R));
4337 -- Renaming of attribute
4339 else
4340 null;
4341 end if;
4342 end if;
4343 end Remove_Homonyms;
4345 -- Start of processing for Check_Pragma_Import
4347 begin
4348 -- Find name of entity in Import pragma. We have not analyzed
4349 -- the construct, so we must guard against syntax errors.
4351 Arg := Next (First (Pragma_Argument_Associations (P)));
4353 if No (Arg)
4354 or else Nkind (Expression (Arg)) /= N_Identifier
4355 then
4356 return;
4357 else
4358 Imported := Expression (Arg);
4359 end if;
4361 Remove_Homonyms (Imported);
4362 end Check_Pragma_Import;
4364 -- Start of processing for Check_Declarations
4366 begin
4367 -- Search for Elaborate Body pragma
4369 Decl := First (Visible_Declarations (Spec));
4370 while Present (Decl)
4371 and then Nkind (Decl) = N_Pragma
4372 loop
4373 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4374 Set_Body_Required (Library_Unit (N));
4375 return;
4376 end if;
4378 Next (Decl);
4379 end loop;
4381 -- Look for declarations that require the presence of a body. We
4382 -- have already skipped pragmas at the start of the list.
4384 while Present (Decl) loop
4386 -- Subprogram that comes from source means body may be needed.
4387 -- Save for subsequent examination of import pragmas.
4389 if Comes_From_Source (Decl)
4390 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4391 N_Subprogram_Renaming_Declaration,
4392 N_Generic_Subprogram_Declaration))
4393 then
4394 Append_Elmt (Defining_Entity (Decl), Subp_List);
4396 -- Package declaration of generic package declaration. We need
4397 -- to recursively examine nested declarations.
4399 elsif Nkind_In (Decl, N_Package_Declaration,
4400 N_Generic_Package_Declaration)
4401 then
4402 Check_Declarations (Specification (Decl));
4404 elsif Nkind (Decl) = N_Pragma
4405 and then Pragma_Name (Decl) = Name_Import
4406 then
4407 Check_Pragma_Import (Decl);
4408 end if;
4410 Next (Decl);
4411 end loop;
4413 -- Same set of tests for private part. In addition to subprograms
4414 -- detect the presence of Taft Amendment types (incomplete types
4415 -- completed in the body).
4417 Decl := First (Private_Declarations (Spec));
4418 while Present (Decl) loop
4419 if Comes_From_Source (Decl)
4420 and then (Nkind_In (Decl, N_Subprogram_Declaration,
4421 N_Subprogram_Renaming_Declaration,
4422 N_Generic_Subprogram_Declaration))
4423 then
4424 Append_Elmt (Defining_Entity (Decl), Subp_List);
4426 elsif Nkind_In (Decl, N_Package_Declaration,
4427 N_Generic_Package_Declaration)
4428 then
4429 Check_Declarations (Specification (Decl));
4431 -- Collect incomplete type declarations for separate pass
4433 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4434 Append_Elmt (Decl, Incomplete_Decls);
4436 elsif Nkind (Decl) = N_Pragma
4437 and then Pragma_Name (Decl) = Name_Import
4438 then
4439 Check_Pragma_Import (Decl);
4440 end if;
4442 Next (Decl);
4443 end loop;
4445 -- Now check incomplete declarations to locate Taft amendment
4446 -- types. This can be done by examining the defining identifiers
4447 -- of type declarations without real semantic analysis.
4449 declare
4450 Inc : Elmt_Id;
4452 begin
4453 Inc := First_Elmt (Incomplete_Decls);
4454 while Present (Inc) loop
4455 Decl := Next (Node (Inc));
4456 while Present (Decl) loop
4457 if Nkind (Decl) = N_Full_Type_Declaration
4458 and then Chars (Defining_Identifier (Decl)) =
4459 Chars (Defining_Identifier (Node (Inc)))
4460 then
4461 exit;
4462 end if;
4464 Next (Decl);
4465 end loop;
4467 -- If no completion, this is a TAT, and a body is needed
4469 if No (Decl) then
4470 Set_Body_Required (Library_Unit (N));
4471 return;
4472 end if;
4474 Next_Elmt (Inc);
4475 end loop;
4476 end;
4478 -- Finally, check whether there are subprograms that still require
4479 -- a body, i.e. are not renamings or null.
4481 if not Is_Empty_Elmt_List (Subp_List) then
4482 declare
4483 Subp_Id : Elmt_Id;
4484 Spec : Node_Id;
4486 begin
4487 Subp_Id := First_Elmt (Subp_List);
4488 Spec := Parent (Node (Subp_Id));
4490 while Present (Subp_Id) loop
4491 if Nkind (Parent (Spec))
4492 = N_Subprogram_Renaming_Declaration
4493 then
4494 null;
4496 elsif Nkind (Spec) = N_Procedure_Specification
4497 and then Null_Present (Spec)
4498 then
4499 null;
4501 else
4502 Set_Body_Required (Library_Unit (N));
4503 return;
4504 end if;
4506 Next_Elmt (Subp_Id);
4507 end loop;
4508 end;
4509 end if;
4510 end Check_Declarations;
4512 -- Start of processing for Check_Body_Required
4514 begin
4515 -- If this is an imported package (Java and CIL usage) no body is
4516 -- needed. Scan list of pragmas that may follow a compilation unit
4517 -- to look for a relevant pragma Import.
4519 if Present (PA) then
4520 declare
4521 Prag : Node_Id;
4523 begin
4524 Prag := First (PA);
4525 while Present (Prag) loop
4526 if Nkind (Prag) = N_Pragma
4527 and then Get_Pragma_Id (Prag) = Pragma_Import
4528 then
4529 return;
4530 end if;
4532 Next (Prag);
4533 end loop;
4534 end;
4535 end if;
4537 Check_Declarations (Specification (P_Unit));
4538 end Check_Body_Required;
4540 -----------------------------
4541 -- Has_Limited_With_Clause --
4542 -----------------------------
4544 function Has_Limited_With_Clause
4545 (C_Unit : Entity_Id;
4546 Pack : Entity_Id) return Boolean
4548 Par : Entity_Id;
4549 Par_Unit : Node_Id;
4551 begin
4552 Par := C_Unit;
4553 while Present (Par) loop
4554 if Ekind (Par) /= E_Package then
4555 exit;
4556 end if;
4558 -- Retrieve the Compilation_Unit node for Par and determine if
4559 -- its context clauses contain a limited with for Pack.
4561 Par_Unit := Parent (Parent (Parent (Par)));
4563 if Nkind (Par_Unit) = N_Package_Declaration then
4564 Par_Unit := Parent (Par_Unit);
4565 end if;
4567 if Has_With_Clause (Par_Unit, Pack, True) then
4568 return True;
4569 end if;
4571 -- If there are more ancestors, climb up the tree, otherwise we
4572 -- are done.
4574 if Is_Child_Unit (Par) then
4575 Par := Scope (Par);
4576 else
4577 exit;
4578 end if;
4579 end loop;
4581 return False;
4582 end Has_Limited_With_Clause;
4584 ----------------------------------
4585 -- Is_Visible_Through_Renamings --
4586 ----------------------------------
4588 function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4589 Kind : constant Node_Kind :=
4590 Nkind (Unit (Cunit (Current_Sem_Unit)));
4591 Aux_Unit : Node_Id;
4592 Item : Node_Id;
4593 Decl : Entity_Id;
4595 begin
4596 -- Example of the error detected by this subprogram:
4598 -- package P is
4599 -- type T is ...
4600 -- end P;
4602 -- with P;
4603 -- package Q is
4604 -- package Ren_P renames P;
4605 -- end Q;
4607 -- with Q;
4608 -- package R is ...
4610 -- limited with P; -- ERROR
4611 -- package R.C is ...
4613 Aux_Unit := Cunit (Current_Sem_Unit);
4615 loop
4616 Item := First (Context_Items (Aux_Unit));
4617 while Present (Item) loop
4618 if Nkind (Item) = N_With_Clause
4619 and then not Limited_Present (Item)
4620 and then Nkind (Unit (Library_Unit (Item))) =
4621 N_Package_Declaration
4622 then
4623 Decl :=
4624 First (Visible_Declarations
4625 (Specification (Unit (Library_Unit (Item)))));
4626 while Present (Decl) loop
4627 if Nkind (Decl) = N_Package_Renaming_Declaration
4628 and then Entity (Name (Decl)) = P
4629 then
4630 -- Generate the error message only if the current unit
4631 -- is a package declaration; in case of subprogram
4632 -- bodies and package bodies we just return True to
4633 -- indicate that the limited view must not be
4634 -- installed.
4636 if Kind = N_Package_Declaration then
4637 Error_Msg_N
4638 ("simultaneous visibility of the limited and " &
4639 "unlimited views not allowed", N);
4640 Error_Msg_Sloc := Sloc (Item);
4641 Error_Msg_NE
4642 ("\\ unlimited view of & visible through the " &
4643 "context clause #", N, P);
4644 Error_Msg_Sloc := Sloc (Decl);
4645 Error_Msg_NE ("\\ and the renaming #", N, P);
4646 end if;
4648 return True;
4649 end if;
4651 Next (Decl);
4652 end loop;
4653 end if;
4655 Next (Item);
4656 end loop;
4658 -- If it is a body not acting as spec, follow pointer to the
4659 -- corresponding spec, otherwise follow pointer to parent spec.
4661 if Present (Library_Unit (Aux_Unit))
4662 and then Nkind_In (Unit (Aux_Unit),
4663 N_Package_Body, N_Subprogram_Body)
4664 then
4665 if Aux_Unit = Library_Unit (Aux_Unit) then
4667 -- Aux_Unit is a body that acts as a spec. Clause has
4668 -- already been flagged as illegal.
4670 return False;
4672 else
4673 Aux_Unit := Library_Unit (Aux_Unit);
4674 end if;
4676 else
4677 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4678 end if;
4680 exit when No (Aux_Unit);
4681 end loop;
4683 return False;
4684 end Is_Visible_Through_Renamings;
4686 -- Start of processing for Install_Limited_Withed_Unit
4688 begin
4689 pragma Assert (not Limited_View_Installed (N));
4691 -- In case of limited with_clause on subprograms, generics, instances,
4692 -- or renamings, the corresponding error was previously posted and we
4693 -- have nothing to do here. If the file is missing altogether, it has
4694 -- no source location.
4696 if Nkind (P_Unit) /= N_Package_Declaration
4697 or else Sloc (P_Unit) = No_Location
4698 then
4699 return;
4700 end if;
4702 P := Defining_Unit_Name (Specification (P_Unit));
4704 -- Handle child packages
4706 if Nkind (P) = N_Defining_Program_Unit_Name then
4707 Is_Child_Package := True;
4708 P := Defining_Identifier (P);
4709 end if;
4711 -- Do not install the limited-view if the context of the unit is already
4712 -- available through a regular with clause.
4714 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4715 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4716 then
4717 return;
4718 end if;
4720 -- Do not install the limited-view if the full-view is already visible
4721 -- through renaming declarations.
4723 if Is_Visible_Through_Renamings (P) then
4724 return;
4725 end if;
4727 -- Do not install the limited view if this is the unit being analyzed.
4728 -- This unusual case will happen when a unit has a limited_with clause
4729 -- on one of its children. The compilation of the child forces the load
4730 -- of the parent which tries to install the limited view of the child
4731 -- again. Installing the limited view must also be disabled when
4732 -- compiling the body of the child unit.
4734 if P = Cunit_Entity (Current_Sem_Unit)
4735 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4736 and then P = Main_Unit_Entity
4737 and then Is_Ancestor_Unit
4738 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
4739 then
4740 return;
4741 end if;
4743 -- This scenario is similar to the one above, the difference is that the
4744 -- compilation of sibling Par.Sib forces the load of parent Par which
4745 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
4746 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
4747 -- non-limited views of all entities from Lim_Pack.
4749 -- limited with Lim_Pack; -- [1]
4750 -- package Par is ... package Lim_Pack is ...
4752 -- with Lim_Pack; -- [2]
4753 -- package Par.Sib is ... package body Par.Sib is ...
4755 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4756 -- Sem_Unit is the body of Par.Sib.
4758 if Ekind (P) = E_Package
4759 and then Ekind (Main_Unit_Entity) = E_Package
4760 and then Is_Child_Unit (Main_Unit_Entity)
4762 -- The body has a regular with clause
4764 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4765 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4767 -- One of the ancestors has a limited with clause
4769 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4770 N_Package_Specification
4771 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4772 then
4773 return;
4774 end if;
4776 -- A common use of the limited-with is to have a limited-with in the
4777 -- package spec, and a normal with in its package body. For example:
4779 -- limited with X; -- [1]
4780 -- package A is ...
4782 -- with X; -- [2]
4783 -- package body A is ...
4785 -- The compilation of A's body installs the context clauses found at [2]
4786 -- and then the context clauses of its specification (found at [1]). As
4787 -- a consequence, at [1] the specification of X has been analyzed and it
4788 -- is immediately visible. According to the semantics of limited-with
4789 -- context clauses we don't install the limited view because the full
4790 -- view of X supersedes its limited view.
4792 if Analyzed (P_Unit)
4793 and then
4794 (Is_Immediately_Visible (P)
4795 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
4796 then
4798 -- The presence of both the limited and the analyzed nonlimited view
4799 -- may also be an error, such as an illegal context for a limited
4800 -- with_clause. In that case, do not process the context item at all.
4802 if Error_Posted (N) then
4803 return;
4804 end if;
4806 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
4807 declare
4808 Item : Node_Id;
4809 begin
4810 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
4811 while Present (Item) loop
4812 if Nkind (Item) = N_With_Clause
4813 and then Comes_From_Source (Item)
4814 and then Entity (Name (Item)) = P
4815 then
4816 return;
4817 end if;
4819 Next (Item);
4820 end loop;
4821 end;
4823 -- If this is a child body, assume that the nonlimited with_clause
4824 -- appears in an ancestor. Could be refined ???
4826 if Is_Child_Unit
4827 (Defining_Entity
4828 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
4829 then
4830 return;
4831 end if;
4833 else
4835 -- If in package declaration, nonlimited view brought in from
4836 -- parent unit or some error condition.
4838 return;
4839 end if;
4840 end if;
4842 if Debug_Flag_I then
4843 Write_Str ("install limited view of ");
4844 Write_Name (Chars (P));
4845 Write_Eol;
4846 end if;
4848 -- If the unit has not been analyzed and the limited view has not been
4849 -- already installed then we install it.
4851 if not Analyzed (P_Unit) then
4852 if not In_Chain (P) then
4854 -- Minimum decoration
4856 Set_Ekind (P, E_Package);
4857 Set_Etype (P, Standard_Void_Type);
4858 Set_Scope (P, Standard_Standard);
4859 Set_Is_Visible_Lib_Unit (P);
4861 if Is_Child_Package then
4862 Set_Is_Child_Unit (P);
4863 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4864 end if;
4866 -- Place entity on visibility structure
4868 Set_Homonym (P, Current_Entity (P));
4869 Set_Current_Entity (P);
4871 if Debug_Flag_I then
4872 Write_Str (" (homonym) chain ");
4873 Write_Name (Chars (P));
4874 Write_Eol;
4875 end if;
4877 -- Install the incomplete view. The first element of the limited
4878 -- view is a header (an E_Package entity) used to reference the
4879 -- first shadow entity in the private part of the package.
4881 Lim_Header := Limited_View (P);
4882 Lim_Typ := First_Entity (Lim_Header);
4884 while Present (Lim_Typ)
4885 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4886 loop
4887 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4888 Set_Current_Entity (Lim_Typ);
4890 if Debug_Flag_I then
4891 Write_Str (" (homonym) chain ");
4892 Write_Name (Chars (Lim_Typ));
4893 Write_Eol;
4894 end if;
4896 Next_Entity (Lim_Typ);
4897 end loop;
4898 end if;
4900 -- If the unit appears in a previous regular with_clause, the regular
4901 -- entities of the public part of the withed package must be replaced
4902 -- by the shadow ones.
4904 -- This code must be kept synchronized with the code that replaces the
4905 -- shadow entities by the real entities (see body of Remove_Limited
4906 -- With_Clause); otherwise the contents of the homonym chains are not
4907 -- consistent.
4909 else
4910 -- Hide all the type entities of the public part of the package to
4911 -- avoid its usage. This is needed to cover all the subtype decla-
4912 -- rations because we do not remove them from the homonym chain.
4914 E := First_Entity (P);
4915 while Present (E) and then E /= First_Private_Entity (P) loop
4916 if Is_Type (E) then
4917 Set_Was_Hidden (E, Is_Hidden (E));
4918 Set_Is_Hidden (E);
4919 end if;
4921 Next_Entity (E);
4922 end loop;
4924 -- Replace the real entities by the shadow entities of the limited
4925 -- view. The first element of the limited view is a header that is
4926 -- used to reference the first shadow entity in the private part
4927 -- of the package. Successive elements are the limited views of the
4928 -- type (including regular incomplete types) declared in the package.
4930 Lim_Header := Limited_View (P);
4932 Lim_Typ := First_Entity (Lim_Header);
4933 while Present (Lim_Typ)
4934 and then Lim_Typ /= First_Private_Entity (Lim_Header)
4935 loop
4936 pragma Assert (not In_Chain (Lim_Typ));
4938 -- Do not unchain nested packages and child units
4940 if Ekind (Lim_Typ) /= E_Package
4941 and then not Is_Child_Unit (Lim_Typ)
4942 then
4943 declare
4944 Prev : Entity_Id;
4946 begin
4947 Prev := Current_Entity (Lim_Typ);
4948 E := Prev;
4950 -- Replace E in the homonyms list, so that the limited view
4951 -- becomes available.
4953 if E = Non_Limited_View (Lim_Typ) then
4954 Set_Homonym (Lim_Typ, Homonym (Prev));
4955 Set_Current_Entity (Lim_Typ);
4957 else
4958 loop
4959 E := Homonym (Prev);
4961 -- E may have been removed when installing a previous
4962 -- limited_with_clause.
4964 exit when No (E);
4966 exit when E = Non_Limited_View (Lim_Typ);
4968 Prev := Homonym (Prev);
4969 end loop;
4971 if Present (E) then
4972 Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4973 Set_Homonym (Prev, Lim_Typ);
4974 end if;
4975 end if;
4976 end;
4978 if Debug_Flag_I then
4979 Write_Str (" (homonym) chain ");
4980 Write_Name (Chars (Lim_Typ));
4981 Write_Eol;
4982 end if;
4983 end if;
4985 Next_Entity (Lim_Typ);
4986 end loop;
4987 end if;
4989 -- The package must be visible while the limited-with clause is active
4990 -- because references to the type P.T must resolve in the usual way.
4991 -- In addition, we remember that the limited-view has been installed to
4992 -- uninstall it at the point of context removal.
4994 Set_Is_Immediately_Visible (P);
4995 Set_Limited_View_Installed (N);
4997 -- If unit has not been analyzed in some previous context, check
4998 -- (imperfectly ???) whether it might need a body.
5000 if not Analyzed (P_Unit) then
5001 Check_Body_Required;
5002 end if;
5004 -- If the package in the limited_with clause is a child unit, the clause
5005 -- is unanalyzed and appears as a selected component. Recast it as an
5006 -- expanded name so that the entity can be properly set. Use entity of
5007 -- parent, if available, for higher ancestors in the name.
5009 if Nkind (Name (N)) = N_Selected_Component then
5010 declare
5011 Nam : Node_Id;
5012 Ent : Entity_Id;
5014 begin
5015 Nam := Name (N);
5016 Ent := P;
5017 while Nkind (Nam) = N_Selected_Component
5018 and then Present (Ent)
5019 loop
5020 Change_Selected_Component_To_Expanded_Name (Nam);
5022 -- Set entity of parent identifiers if the unit is a child
5023 -- unit. This ensures that the tree is properly formed from
5024 -- semantic point of view (e.g. for ASIS queries). The unit
5025 -- entities are not fully analyzed, so we need to follow unit
5026 -- links in the tree.
5028 Set_Entity (Nam, Ent);
5030 Nam := Prefix (Nam);
5031 Ent :=
5032 Defining_Entity
5033 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5035 -- Set entity of last ancestor
5037 if Nkind (Nam) = N_Identifier then
5038 Set_Entity (Nam, Ent);
5039 end if;
5040 end loop;
5041 end;
5042 end if;
5044 Set_Entity (Name (N), P);
5045 Set_From_With_Type (P);
5046 end Install_Limited_Withed_Unit;
5048 -------------------------
5049 -- Install_Withed_Unit --
5050 -------------------------
5052 procedure Install_Withed_Unit
5053 (With_Clause : Node_Id;
5054 Private_With_OK : Boolean := False)
5056 Uname : constant Entity_Id := Entity (Name (With_Clause));
5057 P : constant Entity_Id := Scope (Uname);
5059 begin
5060 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5061 -- compiling a package declaration and the Private_With_OK flag was not
5062 -- set by the caller. These declarations will be installed later (before
5063 -- analyzing the private part of the package).
5065 if Private_Present (With_Clause)
5066 and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
5067 and then not (Private_With_OK)
5068 then
5069 return;
5070 end if;
5072 if Debug_Flag_I then
5073 if Private_Present (With_Clause) then
5074 Write_Str ("install private withed unit ");
5075 else
5076 Write_Str ("install withed unit ");
5077 end if;
5079 Write_Name (Chars (Uname));
5080 Write_Eol;
5081 end if;
5083 -- We do not apply the restrictions to an internal unit unless we are
5084 -- compiling the internal unit as a main unit. This check is also
5085 -- skipped for dummy units (for missing packages).
5087 if Sloc (Uname) /= No_Location
5088 and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
5089 or else Current_Sem_Unit = Main_Unit)
5090 then
5091 Check_Restricted_Unit
5092 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5093 end if;
5095 if P /= Standard_Standard then
5097 -- If the unit is not analyzed after analysis of the with clause and
5098 -- it is an instantiation then it awaits a body and is the main unit.
5099 -- Its appearance in the context of some other unit indicates a
5100 -- circular dependency (DEC suite perversity).
5102 if not Analyzed (Uname)
5103 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5104 then
5105 Error_Msg_N
5106 ("instantiation depends on itself", Name (With_Clause));
5108 elsif not Is_Visible_Lib_Unit (Uname) then
5110 -- Abandon processing in case of previous errors
5112 if No (Scope (Uname)) then
5113 Check_Error_Detected;
5114 return;
5115 end if;
5117 Set_Is_Visible_Lib_Unit (Uname);
5119 -- If the child unit appears in the context of its parent, it is
5120 -- immediately visible.
5122 if In_Open_Scopes (Scope (Uname)) then
5123 Set_Is_Immediately_Visible (Uname);
5124 end if;
5126 if Is_Generic_Instance (Uname)
5127 and then Ekind (Uname) in Subprogram_Kind
5128 then
5129 -- Set flag as well on the visible entity that denotes the
5130 -- instance, which renames the current one.
5132 Set_Is_Visible_Lib_Unit
5133 (Related_Instance
5134 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5135 end if;
5137 -- The parent unit may have been installed already, and may have
5138 -- appeared in a use clause.
5140 if In_Use (Scope (Uname)) then
5141 Set_Is_Potentially_Use_Visible (Uname);
5142 end if;
5144 Set_Context_Installed (With_Clause);
5145 end if;
5147 elsif not Is_Immediately_Visible (Uname) then
5148 Set_Is_Visible_Lib_Unit (Uname);
5150 if not Private_Present (With_Clause) or else Private_With_OK then
5151 Set_Is_Immediately_Visible (Uname);
5152 end if;
5154 Set_Context_Installed (With_Clause);
5155 end if;
5157 -- A with-clause overrides a with-type clause: there are no restric-
5158 -- tions on the use of package entities.
5160 if Ekind (Uname) = E_Package then
5161 Set_From_With_Type (Uname, False);
5162 end if;
5164 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5165 -- unit if there is a visible homograph for it declared in the same
5166 -- declarative region. This pathological case can only arise when an
5167 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5168 -- G1 has a generic child also named G2, and the context includes with_
5169 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5170 -- of I1.G2 visible as well. If the child unit is named Standard, do
5171 -- not apply the check to the Standard package itself.
5173 if Is_Child_Unit (Uname)
5174 and then Is_Visible_Lib_Unit (Uname)
5175 and then Ada_Version >= Ada_2005
5176 then
5177 declare
5178 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5179 Decl2 : Node_Id;
5180 P2 : Entity_Id;
5181 U2 : Entity_Id;
5183 begin
5184 U2 := Homonym (Uname);
5185 while Present (U2)
5186 and then U2 /= Standard_Standard
5187 loop
5188 P2 := Scope (U2);
5189 Decl2 := Unit_Declaration_Node (P2);
5191 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5192 if Is_Generic_Instance (P)
5193 and then Nkind (Decl1) = N_Package_Declaration
5194 and then Generic_Parent (Specification (Decl1)) = P2
5195 then
5196 Error_Msg_N ("illegal with_clause", With_Clause);
5197 Error_Msg_N
5198 ("\child unit has visible homograph" &
5199 " (RM 8.3(26), 10.1.1(19))",
5200 With_Clause);
5201 exit;
5203 elsif Is_Generic_Instance (P2)
5204 and then Nkind (Decl2) = N_Package_Declaration
5205 and then Generic_Parent (Specification (Decl2)) = P
5206 then
5207 -- With_clause for child unit of instance appears before
5208 -- in the context. We want to place the error message on
5209 -- it, not on the generic child unit itself.
5211 declare
5212 Prev_Clause : Node_Id;
5214 begin
5215 Prev_Clause := First (List_Containing (With_Clause));
5216 while Entity (Name (Prev_Clause)) /= U2 loop
5217 Next (Prev_Clause);
5218 end loop;
5220 pragma Assert (Present (Prev_Clause));
5221 Error_Msg_N ("illegal with_clause", Prev_Clause);
5222 Error_Msg_N
5223 ("\child unit has visible homograph" &
5224 " (RM 8.3(26), 10.1.1(19))",
5225 Prev_Clause);
5226 exit;
5227 end;
5228 end if;
5229 end if;
5231 U2 := Homonym (U2);
5232 end loop;
5233 end;
5234 end if;
5235 end Install_Withed_Unit;
5237 -------------------
5238 -- Is_Child_Spec --
5239 -------------------
5241 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5242 K : constant Node_Kind := Nkind (Lib_Unit);
5244 begin
5245 return (K in N_Generic_Declaration or else
5246 K in N_Generic_Instantiation or else
5247 K in N_Generic_Renaming_Declaration or else
5248 K = N_Package_Declaration or else
5249 K = N_Package_Renaming_Declaration or else
5250 K = N_Subprogram_Declaration or else
5251 K = N_Subprogram_Renaming_Declaration)
5252 and then Present (Parent_Spec (Lib_Unit));
5253 end Is_Child_Spec;
5255 ------------------------------------
5256 -- Is_Legal_Shadow_Entity_In_Body --
5257 ------------------------------------
5259 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5260 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5261 begin
5262 return Nkind (Unit (C_Unit)) = N_Package_Body
5263 and then
5264 Has_With_Clause
5265 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5266 end Is_Legal_Shadow_Entity_In_Body;
5268 ----------------------
5269 -- Is_Ancestor_Unit --
5270 ----------------------
5272 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5273 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5274 E2 : Entity_Id;
5275 begin
5276 if Nkind_In (Unit (U2), N_Package_Body, N_Subprogram_Body) then
5277 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5278 return Is_Ancestor_Package (E1, E2);
5279 else
5280 return False;
5281 end if;
5282 end Is_Ancestor_Unit;
5284 -----------------------
5285 -- Load_Needed_Body --
5286 -----------------------
5288 -- N is a generic unit named in a with clause, or else it is a unit that
5289 -- contains a generic unit or an inlined function. In order to perform an
5290 -- instantiation, the body of the unit must be present. If the unit itself
5291 -- is generic, we assume that an instantiation follows, and load & analyze
5292 -- the body unconditionally. This forces analysis of the spec as well.
5294 -- If the unit is not generic, but contains a generic unit, it is loaded on
5295 -- demand, at the point of instantiation (see ch12).
5297 procedure Load_Needed_Body
5298 (N : Node_Id;
5299 OK : out Boolean;
5300 Do_Analyze : Boolean := True)
5302 Body_Name : Unit_Name_Type;
5303 Unum : Unit_Number_Type;
5305 Save_Style_Check : constant Boolean := Opt.Style_Check;
5306 -- The loading and analysis is done with style checks off
5308 begin
5309 if not GNAT_Mode then
5310 Style_Check := False;
5311 end if;
5313 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5314 Unum :=
5315 Load_Unit
5316 (Load_Name => Body_Name,
5317 Required => False,
5318 Subunit => False,
5319 Error_Node => N,
5320 Renamings => True);
5322 if Unum = No_Unit then
5323 OK := False;
5325 else
5326 Compiler_State := Analyzing; -- reset after load
5328 if not Fatal_Error (Unum) or else Try_Semantics then
5329 if Debug_Flag_L then
5330 Write_Str ("*** Loaded generic body");
5331 Write_Eol;
5332 end if;
5334 if Do_Analyze then
5335 Semantics (Cunit (Unum));
5336 end if;
5337 end if;
5339 OK := True;
5340 end if;
5342 Style_Check := Save_Style_Check;
5343 end Load_Needed_Body;
5345 -------------------------
5346 -- Build_Limited_Views --
5347 -------------------------
5349 procedure Build_Limited_Views (N : Node_Id) is
5350 Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
5351 P : constant Entity_Id := Cunit_Entity (Unum);
5353 Spec : Node_Id; -- To denote a package specification
5354 Lim_Typ : Entity_Id; -- To denote shadow entities
5355 Comp_Typ : Entity_Id; -- To denote real entities
5357 Lim_Header : Entity_Id; -- Package entity
5358 Last_Lim_E : Entity_Id := Empty; -- Last limited entity built
5359 Last_Pub_Lim_E : Entity_Id; -- To set the first private entity
5361 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id);
5362 -- Add attributes of an incomplete type to a shadow entity. The same
5363 -- attributes are placed on the real entity, so that gigi receives
5364 -- a consistent view.
5366 procedure Decorate_Package_Specification (P : Entity_Id);
5367 -- Add attributes of a package entity to the entity in a package
5368 -- declaration
5370 procedure Decorate_Tagged_Type
5371 (Loc : Source_Ptr;
5372 T : Entity_Id;
5373 Scop : Entity_Id;
5374 Mark : Boolean := False);
5375 -- Set basic attributes of tagged type T, including its class-wide type.
5376 -- The parameters Loc, Scope are used to decorate the class-wide type.
5377 -- Use flag Mark to label the class-wide type as Materialize_Entity.
5379 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id);
5380 -- Construct list of shadow entities and attach it to entity of
5381 -- package that is mentioned in a limited_with clause.
5383 function New_Internal_Shadow_Entity
5384 (Kind : Entity_Kind;
5385 Sloc_Value : Source_Ptr;
5386 Id_Char : Character) return Entity_Id;
5387 -- Build a new internal entity and append it to the list of shadow
5388 -- entities available through the limited-header
5390 -----------------
5391 -- Build_Chain --
5392 -----------------
5394 procedure Build_Chain (Scope : Entity_Id; First_Decl : Node_Id) is
5395 Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
5396 Is_Tagged : Boolean;
5397 Decl : Node_Id;
5399 begin
5400 Decl := First_Decl;
5401 while Present (Decl) loop
5403 -- For each library_package_declaration in the environment, there
5404 -- is an implicit declaration of a *limited view* of that library
5405 -- package. The limited view of a package contains:
5407 -- * For each nested package_declaration, a declaration of the
5408 -- limited view of that package, with the same defining-
5409 -- program-unit name.
5411 -- * For each type_declaration in the visible part, an incomplete
5412 -- type-declaration with the same defining_identifier, whose
5413 -- completion is the type_declaration. If the type_declaration
5414 -- is tagged, then the incomplete_type_declaration is tagged
5415 -- incomplete.
5417 -- The partial view is tagged if the declaration has the
5418 -- explicit keyword, or else if it is a type extension, both
5419 -- of which can be ascertained syntactically.
5421 if Nkind (Decl) = N_Full_Type_Declaration then
5422 Is_Tagged :=
5423 (Nkind (Type_Definition (Decl)) = N_Record_Definition
5424 and then Tagged_Present (Type_Definition (Decl)))
5425 or else
5426 (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
5427 and then
5428 Present
5429 (Record_Extension_Part (Type_Definition (Decl))));
5431 Comp_Typ := Defining_Identifier (Decl);
5433 if not Analyzed_Unit then
5434 if Is_Tagged then
5435 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope, True);
5436 else
5437 Decorate_Incomplete_Type (Comp_Typ, Scope);
5438 end if;
5439 end if;
5441 -- Create shadow entity for type
5443 Lim_Typ :=
5444 New_Internal_Shadow_Entity
5445 (Kind => Ekind (Comp_Typ),
5446 Sloc_Value => Sloc (Comp_Typ),
5447 Id_Char => 'Z');
5449 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5450 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5451 Set_From_With_Type (Lim_Typ);
5453 if Is_Tagged then
5454 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5455 else
5456 Decorate_Incomplete_Type (Lim_Typ, Scope);
5457 end if;
5459 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5460 Set_Private_Dependents (Lim_Typ, New_Elmt_List);
5462 elsif Nkind_In (Decl, N_Private_Type_Declaration,
5463 N_Incomplete_Type_Declaration,
5464 N_Task_Type_Declaration,
5465 N_Protected_Type_Declaration)
5466 then
5467 Comp_Typ := Defining_Identifier (Decl);
5469 Is_Tagged :=
5470 Nkind_In (Decl, N_Private_Type_Declaration,
5471 N_Incomplete_Type_Declaration)
5472 and then Tagged_Present (Decl);
5474 if not Analyzed_Unit then
5475 if Is_Tagged then
5476 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope, True);
5477 else
5478 Decorate_Incomplete_Type (Comp_Typ, Scope);
5479 end if;
5480 end if;
5482 Lim_Typ :=
5483 New_Internal_Shadow_Entity
5484 (Kind => Ekind (Comp_Typ),
5485 Sloc_Value => Sloc (Comp_Typ),
5486 Id_Char => 'Z');
5488 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5489 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5490 Set_From_With_Type (Lim_Typ);
5492 if Is_Tagged then
5493 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5494 else
5495 Decorate_Incomplete_Type (Lim_Typ, Scope);
5496 end if;
5498 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5500 -- Initialize Private_Depedents, so the field has the proper
5501 -- type, even though the list will remain empty.
5503 Set_Private_Dependents (Lim_Typ, New_Elmt_List);
5505 elsif Nkind (Decl) = N_Private_Extension_Declaration then
5506 Comp_Typ := Defining_Identifier (Decl);
5508 if not Analyzed_Unit then
5509 Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope, True);
5510 end if;
5512 -- Create shadow entity for type
5514 Lim_Typ :=
5515 New_Internal_Shadow_Entity
5516 (Kind => Ekind (Comp_Typ),
5517 Sloc_Value => Sloc (Comp_Typ),
5518 Id_Char => 'Z');
5520 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5521 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5522 Set_From_With_Type (Lim_Typ);
5524 Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5525 Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5527 elsif Nkind (Decl) = N_Package_Declaration then
5529 -- Local package
5531 declare
5532 Spec : constant Node_Id := Specification (Decl);
5534 begin
5535 Comp_Typ := Defining_Unit_Name (Spec);
5537 if not Analyzed (Cunit (Unum)) then
5538 Decorate_Package_Specification (Comp_Typ);
5539 Set_Scope (Comp_Typ, Scope);
5540 end if;
5542 Lim_Typ :=
5543 New_Internal_Shadow_Entity
5544 (Kind => Ekind (Comp_Typ),
5545 Sloc_Value => Sloc (Comp_Typ),
5546 Id_Char => 'Z');
5548 Decorate_Package_Specification (Lim_Typ);
5549 Set_Scope (Lim_Typ, Scope);
5551 Set_Chars (Lim_Typ, Chars (Comp_Typ));
5552 Set_Parent (Lim_Typ, Parent (Comp_Typ));
5553 Set_From_With_Type (Lim_Typ);
5555 -- Note: The non_limited_view attribute is not used
5556 -- for local packages.
5558 Build_Chain
5559 (Scope => Lim_Typ,
5560 First_Decl => First (Visible_Declarations (Spec)));
5561 end;
5562 end if;
5564 Next (Decl);
5565 end loop;
5566 end Build_Chain;
5568 ------------------------------
5569 -- Decorate_Incomplete_Type --
5570 ------------------------------
5572 procedure Decorate_Incomplete_Type (E : Entity_Id; Scop : Entity_Id) is
5573 begin
5574 Set_Ekind (E, E_Incomplete_Type);
5575 Set_Scope (E, Scop);
5576 Set_Etype (E, E);
5577 Set_Is_First_Subtype (E, True);
5578 Set_Stored_Constraint (E, No_Elist);
5579 Set_Full_View (E, Empty);
5580 Init_Size_Align (E);
5581 end Decorate_Incomplete_Type;
5583 --------------------------
5584 -- Decorate_Tagged_Type --
5585 --------------------------
5587 procedure Decorate_Tagged_Type
5588 (Loc : Source_Ptr;
5589 T : Entity_Id;
5590 Scop : Entity_Id;
5591 Mark : Boolean := False)
5593 CW : Entity_Id;
5595 begin
5596 Decorate_Incomplete_Type (T, Scop);
5597 Set_Is_Tagged_Type (T);
5599 -- Build corresponding class_wide type, if not previously done
5601 -- Note: The class-wide entity is shared by the limited-view
5602 -- and the full-view.
5604 if No (Class_Wide_Type (T)) then
5605 CW := New_External_Entity (E_Void, Scope (T), Loc, T, 'C', 0, 'T');
5607 -- Set parent to be the same as the parent of the tagged type.
5608 -- We need a parent field set, and it is supposed to point to
5609 -- the declaration of the type. The tagged type declaration
5610 -- essentially declares two separate types, the tagged type
5611 -- itself and the corresponding class-wide type, so it is
5612 -- reasonable for the parent fields to point to the declaration
5613 -- in both cases.
5615 Set_Parent (CW, Parent (T));
5617 -- Set remaining fields of classwide type
5619 Set_Ekind (CW, E_Class_Wide_Type);
5620 Set_Etype (CW, T);
5621 Set_Scope (CW, Scop);
5622 Set_Is_Tagged_Type (CW);
5623 Set_Is_First_Subtype (CW, True);
5624 Init_Size_Align (CW);
5625 Set_Has_Unknown_Discriminants (CW, True);
5626 Set_Class_Wide_Type (CW, CW);
5627 Set_Equivalent_Type (CW, Empty);
5628 Set_From_With_Type (CW, From_With_Type (T));
5629 Set_Materialize_Entity (CW, Mark);
5631 -- Link type to its class-wide type
5633 Set_Class_Wide_Type (T, CW);
5634 end if;
5635 end Decorate_Tagged_Type;
5637 ------------------------------------
5638 -- Decorate_Package_Specification --
5639 ------------------------------------
5641 procedure Decorate_Package_Specification (P : Entity_Id) is
5642 begin
5643 -- Place only the most basic attributes
5645 Set_Ekind (P, E_Package);
5646 Set_Etype (P, Standard_Void_Type);
5647 end Decorate_Package_Specification;
5649 --------------------------------
5650 -- New_Internal_Shadow_Entity --
5651 --------------------------------
5653 function New_Internal_Shadow_Entity
5654 (Kind : Entity_Kind;
5655 Sloc_Value : Source_Ptr;
5656 Id_Char : Character) return Entity_Id
5658 E : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
5660 begin
5661 Set_Ekind (E, Kind);
5662 Set_Is_Internal (E, True);
5664 if Kind in Type_Kind then
5665 Init_Size_Align (E);
5666 end if;
5668 Append_Entity (E, Lim_Header);
5669 Last_Lim_E := E;
5670 return E;
5671 end New_Internal_Shadow_Entity;
5673 -- Start of processing for Build_Limited_Views
5675 begin
5676 pragma Assert (Limited_Present (N));
5678 -- A library_item mentioned in a limited_with_clause is a package
5679 -- declaration, not a subprogram declaration, generic declaration,
5680 -- generic instantiation, or package renaming declaration.
5682 case Nkind (Unit (Library_Unit (N))) is
5683 when N_Package_Declaration =>
5684 null;
5686 when N_Subprogram_Declaration =>
5687 Error_Msg_N ("subprograms not allowed in "
5688 & "limited with_clauses", N);
5689 return;
5691 when N_Generic_Package_Declaration |
5692 N_Generic_Subprogram_Declaration =>
5693 Error_Msg_N ("generics not allowed in "
5694 & "limited with_clauses", N);
5695 return;
5697 when N_Generic_Instantiation =>
5698 Error_Msg_N ("generic instantiations not allowed in "
5699 & "limited with_clauses", N);
5700 return;
5702 when N_Generic_Renaming_Declaration =>
5703 Error_Msg_N ("generic renamings not allowed in "
5704 & "limited with_clauses", N);
5705 return;
5707 when N_Subprogram_Renaming_Declaration =>
5708 Error_Msg_N ("renamed subprograms not allowed in "
5709 & "limited with_clauses", N);
5710 return;
5712 when N_Package_Renaming_Declaration =>
5713 Error_Msg_N ("renamed packages not allowed in "
5714 & "limited with_clauses", N);
5715 return;
5717 when others =>
5718 raise Program_Error;
5719 end case;
5721 -- The limited unit is not analyzed but the with clause must be
5722 -- minimally decorated so that checks on unused with clause also work
5723 -- with limited with clauses.
5725 if Is_Entity_Name (Name (N)) then
5726 Set_Entity (Name (N), P);
5728 elsif Nkind (Name (N)) = N_Selected_Component then
5729 Set_Entity (Selector_Name (Name (N)), P);
5730 end if;
5732 -- Check if the chain is already built
5734 Spec := Specification (Unit (Library_Unit (N)));
5736 if Limited_View_Installed (Spec) then
5737 return;
5738 end if;
5740 Set_Ekind (P, E_Package);
5742 -- Build the header of the limited_view
5744 Lim_Header := Make_Temporary (Sloc (N), 'Z');
5745 Set_Ekind (Lim_Header, E_Package);
5746 Set_Is_Internal (Lim_Header);
5747 Set_Limited_View (P, Lim_Header);
5749 -- Create the auxiliary chain. All the shadow entities are appended to
5750 -- the list of entities of the limited-view header
5752 Build_Chain
5753 (Scope => P,
5754 First_Decl => First (Visible_Declarations (Spec)));
5756 -- Save the last built shadow entity. It is needed later to set the
5757 -- reference to the first shadow entity in the private part
5759 Last_Pub_Lim_E := Last_Lim_E;
5761 -- Ada 2005 (AI-262): Add the limited view of the private declarations
5762 -- Required to give support to limited-private-with clauses
5764 Build_Chain (Scope => P,
5765 First_Decl => First (Private_Declarations (Spec)));
5767 if Last_Pub_Lim_E /= Empty then
5768 Set_First_Private_Entity
5769 (Lim_Header, Next_Entity (Last_Pub_Lim_E));
5770 else
5771 Set_First_Private_Entity
5772 (Lim_Header, First_Entity (P));
5773 end if;
5775 Set_Limited_View_Installed (Spec);
5776 end Build_Limited_Views;
5778 -------------------------------
5779 -- Check_Body_Needed_For_SAL --
5780 -------------------------------
5782 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5784 function Entity_Needs_Body (E : Entity_Id) return Boolean;
5785 -- Determine whether use of entity E might require the presence of its
5786 -- body. For a package this requires a recursive traversal of all nested
5787 -- declarations.
5789 ---------------------------
5790 -- Entity_Needed_For_SAL --
5791 ---------------------------
5793 function Entity_Needs_Body (E : Entity_Id) return Boolean is
5794 Ent : Entity_Id;
5796 begin
5797 if Is_Subprogram (E)
5798 and then Has_Pragma_Inline (E)
5799 then
5800 return True;
5802 elsif Ekind_In (E, E_Generic_Function, E_Generic_Procedure) then
5803 return True;
5805 elsif Ekind (E) = E_Generic_Package
5806 and then
5807 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5808 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5809 then
5810 return True;
5812 elsif Ekind (E) = E_Package
5813 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5814 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5815 then
5816 Ent := First_Entity (E);
5817 while Present (Ent) loop
5818 if Entity_Needs_Body (Ent) then
5819 return True;
5820 end if;
5822 Next_Entity (Ent);
5823 end loop;
5825 return False;
5827 else
5828 return False;
5829 end if;
5830 end Entity_Needs_Body;
5832 -- Start of processing for Check_Body_Needed_For_SAL
5834 begin
5835 if Ekind (Unit_Name) = E_Generic_Package
5836 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5837 N_Generic_Package_Declaration
5838 and then
5839 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5840 then
5841 Set_Body_Needed_For_SAL (Unit_Name);
5843 elsif Ekind_In (Unit_Name, E_Generic_Procedure, E_Generic_Function) then
5844 Set_Body_Needed_For_SAL (Unit_Name);
5846 elsif Is_Subprogram (Unit_Name)
5847 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5848 N_Subprogram_Declaration
5849 and then Has_Pragma_Inline (Unit_Name)
5850 then
5851 Set_Body_Needed_For_SAL (Unit_Name);
5853 elsif Ekind (Unit_Name) = E_Subprogram_Body then
5854 Check_Body_Needed_For_SAL
5855 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5857 elsif Ekind (Unit_Name) = E_Package
5858 and then Entity_Needs_Body (Unit_Name)
5859 then
5860 Set_Body_Needed_For_SAL (Unit_Name);
5862 elsif Ekind (Unit_Name) = E_Package_Body
5863 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5864 then
5865 Check_Body_Needed_For_SAL
5866 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5867 end if;
5868 end Check_Body_Needed_For_SAL;
5870 --------------------
5871 -- Remove_Context --
5872 --------------------
5874 procedure Remove_Context (N : Node_Id) is
5875 Lib_Unit : constant Node_Id := Unit (N);
5877 begin
5878 -- If this is a child unit, first remove the parent units
5880 if Is_Child_Spec (Lib_Unit) then
5881 Remove_Parents (Lib_Unit);
5882 end if;
5884 Remove_Context_Clauses (N);
5885 end Remove_Context;
5887 ----------------------------
5888 -- Remove_Context_Clauses --
5889 ----------------------------
5891 procedure Remove_Context_Clauses (N : Node_Id) is
5892 Item : Node_Id;
5893 Unit_Name : Entity_Id;
5895 begin
5896 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
5897 -- limited-views first and regular-views later (to maintain the
5898 -- stack model).
5900 -- First Phase: Remove limited_with context clauses
5902 Item := First (Context_Items (N));
5903 while Present (Item) loop
5905 -- We are interested only in with clauses which got installed
5906 -- on entry.
5908 if Nkind (Item) = N_With_Clause
5909 and then Limited_Present (Item)
5910 and then Limited_View_Installed (Item)
5911 then
5912 Remove_Limited_With_Clause (Item);
5913 end if;
5915 Next (Item);
5916 end loop;
5918 -- Second Phase: Loop through context items and undo regular
5919 -- with_clauses and use_clauses.
5921 Item := First (Context_Items (N));
5922 while Present (Item) loop
5924 -- We are interested only in with clauses which got installed on
5925 -- entry, as indicated by their Context_Installed flag set
5927 if Nkind (Item) = N_With_Clause
5928 and then Limited_Present (Item)
5929 and then Limited_View_Installed (Item)
5930 then
5931 null;
5933 elsif Nkind (Item) = N_With_Clause
5934 and then Context_Installed (Item)
5935 then
5936 -- Remove items from one with'ed unit
5938 Unit_Name := Entity (Name (Item));
5939 Remove_Unit_From_Visibility (Unit_Name);
5940 Set_Context_Installed (Item, False);
5942 elsif Nkind (Item) = N_Use_Package_Clause then
5943 End_Use_Package (Item);
5945 elsif Nkind (Item) = N_Use_Type_Clause then
5946 End_Use_Type (Item);
5947 end if;
5949 Next (Item);
5950 end loop;
5951 end Remove_Context_Clauses;
5953 --------------------------------
5954 -- Remove_Limited_With_Clause --
5955 --------------------------------
5957 procedure Remove_Limited_With_Clause (N : Node_Id) is
5958 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
5959 E : Entity_Id;
5960 P : Entity_Id;
5961 Lim_Header : Entity_Id;
5962 Lim_Typ : Entity_Id;
5963 Prev : Entity_Id;
5965 begin
5966 pragma Assert (Limited_View_Installed (N));
5968 -- In case of limited with_clause on subprograms, generics, instances,
5969 -- or renamings, the corresponding error was previously posted and we
5970 -- have nothing to do here.
5972 if Nkind (P_Unit) /= N_Package_Declaration then
5973 return;
5974 end if;
5976 P := Defining_Unit_Name (Specification (P_Unit));
5978 -- Handle child packages
5980 if Nkind (P) = N_Defining_Program_Unit_Name then
5981 P := Defining_Identifier (P);
5982 end if;
5984 if Debug_Flag_I then
5985 Write_Str ("remove limited view of ");
5986 Write_Name (Chars (P));
5987 Write_Str (" from visibility");
5988 Write_Eol;
5989 end if;
5991 -- Prepare the removal of the shadow entities from visibility. The first
5992 -- element of the limited view is a header (an E_Package entity) that is
5993 -- used to reference the first shadow entity in the private part of the
5994 -- package
5996 Lim_Header := Limited_View (P);
5997 Lim_Typ := First_Entity (Lim_Header);
5999 -- Remove package and shadow entities from visibility if it has not
6000 -- been analyzed
6002 if not Analyzed (P_Unit) then
6003 Unchain (P);
6004 Set_Is_Immediately_Visible (P, False);
6006 while Present (Lim_Typ) loop
6007 Unchain (Lim_Typ);
6008 Next_Entity (Lim_Typ);
6009 end loop;
6011 -- Otherwise this package has already appeared in the closure and its
6012 -- shadow entities must be replaced by its real entities. This code
6013 -- must be kept synchronized with the complementary code in Install
6014 -- Limited_Withed_Unit.
6016 else
6017 -- Real entities that are type or subtype declarations were hidden
6018 -- from visibility at the point of installation of the limited-view.
6019 -- Now we recover the previous value of the hidden attribute.
6021 E := First_Entity (P);
6022 while Present (E) and then E /= First_Private_Entity (P) loop
6023 if Is_Type (E) then
6024 Set_Is_Hidden (E, Was_Hidden (E));
6025 end if;
6027 Next_Entity (E);
6028 end loop;
6030 while Present (Lim_Typ)
6031 and then Lim_Typ /= First_Private_Entity (Lim_Header)
6032 loop
6033 -- Nested packages and child units were not unchained
6035 if Ekind (Lim_Typ) /= E_Package
6036 and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
6037 then
6038 -- If the package has incomplete types, the limited view of the
6039 -- incomplete type is in fact never visible (AI05-129) but we
6040 -- have created a shadow entity E1 for it, that points to E2,
6041 -- a non-limited incomplete type. This in turn has a full view
6042 -- E3 that is the full declaration. There is a corresponding
6043 -- shadow entity E4. When reinstalling the non-limited view,
6044 -- E2 must become the current entity and E3 must be ignored.
6046 E := Non_Limited_View (Lim_Typ);
6048 if Present (Current_Entity (E))
6049 and then Ekind (Current_Entity (E)) = E_Incomplete_Type
6050 and then Full_View (Current_Entity (E)) = E
6051 then
6053 -- Lim_Typ is the limited view of a full type declaration
6054 -- that has a previous incomplete declaration, i.e. E3 from
6055 -- the previous description. Nothing to insert.
6057 null;
6059 else
6060 pragma Assert (not In_Chain (E));
6062 Prev := Current_Entity (Lim_Typ);
6064 if Prev = Lim_Typ then
6065 Set_Current_Entity (E);
6067 else
6068 while Present (Prev)
6069 and then Homonym (Prev) /= Lim_Typ
6070 loop
6071 Prev := Homonym (Prev);
6072 end loop;
6074 if Present (Prev) then
6075 Set_Homonym (Prev, E);
6076 end if;
6077 end if;
6079 -- Preserve structure of homonym chain
6081 Set_Homonym (E, Homonym (Lim_Typ));
6082 end if;
6083 end if;
6085 Next_Entity (Lim_Typ);
6086 end loop;
6087 end if;
6089 -- Indicate that the limited view of the package is not installed
6091 Set_From_With_Type (P, False);
6092 Set_Limited_View_Installed (N, False);
6093 end Remove_Limited_With_Clause;
6095 --------------------
6096 -- Remove_Parents --
6097 --------------------
6099 procedure Remove_Parents (Lib_Unit : Node_Id) is
6100 P : Node_Id;
6101 P_Name : Entity_Id;
6102 P_Spec : Node_Id := Empty;
6103 E : Entity_Id;
6104 Vis : constant Boolean :=
6105 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
6107 begin
6108 if Is_Child_Spec (Lib_Unit) then
6109 P_Spec := Parent_Spec (Lib_Unit);
6111 elsif Nkind (Lib_Unit) = N_Package_Body
6112 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
6113 then
6114 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
6115 end if;
6117 if Present (P_Spec) then
6118 P := Unit (P_Spec);
6119 P_Name := Get_Parent_Entity (P);
6120 Remove_Context_Clauses (P_Spec);
6121 End_Package_Scope (P_Name);
6122 Set_Is_Immediately_Visible (P_Name, Vis);
6124 -- Remove from visibility the siblings as well, which are directly
6125 -- visible while the parent is in scope.
6127 E := First_Entity (P_Name);
6128 while Present (E) loop
6129 if Is_Child_Unit (E) then
6130 Set_Is_Immediately_Visible (E, False);
6131 end if;
6133 Next_Entity (E);
6134 end loop;
6136 Set_In_Package_Body (P_Name, False);
6138 -- This is the recursive call to remove the context of any higher
6139 -- level parent. This recursion ensures that all parents are removed
6140 -- in the reverse order of their installation.
6142 Remove_Parents (P);
6143 end if;
6144 end Remove_Parents;
6146 ---------------------------------
6147 -- Remove_Private_With_Clauses --
6148 ---------------------------------
6150 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
6151 Item : Node_Id;
6153 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
6154 -- Check whether a given unit appears in a regular with_clause. Used to
6155 -- determine whether a private_with_clause, implicit or explicit, should
6156 -- be ignored.
6158 ----------------------------
6159 -- In_Regular_With_Clause --
6160 ----------------------------
6162 function In_Regular_With_Clause (E : Entity_Id) return Boolean
6164 Item : Node_Id;
6166 begin
6167 Item := First (Context_Items (Comp_Unit));
6168 while Present (Item) loop
6169 if Nkind (Item) = N_With_Clause
6170 and then Entity (Name (Item)) = E
6171 and then not Private_Present (Item)
6172 then
6173 return True;
6174 end if;
6175 Next (Item);
6176 end loop;
6178 return False;
6179 end In_Regular_With_Clause;
6181 -- Start of processing for Remove_Private_With_Clauses
6183 begin
6184 Item := First (Context_Items (Comp_Unit));
6185 while Present (Item) loop
6186 if Nkind (Item) = N_With_Clause
6187 and then Private_Present (Item)
6188 then
6189 -- If private_with_clause is redundant, remove it from context,
6190 -- as a small optimization to subsequent handling of private_with
6191 -- clauses in other nested packages.
6193 if In_Regular_With_Clause (Entity (Name (Item))) then
6194 declare
6195 Nxt : constant Node_Id := Next (Item);
6196 begin
6197 Remove (Item);
6198 Item := Nxt;
6199 end;
6201 elsif Limited_Present (Item) then
6202 if not Limited_View_Installed (Item) then
6203 Remove_Limited_With_Clause (Item);
6204 end if;
6206 Next (Item);
6208 else
6209 Remove_Unit_From_Visibility (Entity (Name (Item)));
6210 Set_Context_Installed (Item, False);
6211 Next (Item);
6212 end if;
6214 else
6215 Next (Item);
6216 end if;
6217 end loop;
6218 end Remove_Private_With_Clauses;
6220 ---------------------------------
6221 -- Remove_Unit_From_Visibility --
6222 ---------------------------------
6224 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
6225 begin
6226 if Debug_Flag_I then
6227 Write_Str ("remove unit ");
6228 Write_Name (Chars (Unit_Name));
6229 Write_Str (" from visibility");
6230 Write_Eol;
6231 end if;
6233 Set_Is_Visible_Lib_Unit (Unit_Name, False);
6234 Set_Is_Potentially_Use_Visible (Unit_Name, False);
6235 Set_Is_Immediately_Visible (Unit_Name, False);
6237 -- If the unit is a wrapper package, the subprogram instance is
6238 -- what must be removed from visibility.
6240 if Is_Wrapper_Package (Unit_Name) then
6241 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
6242 end if;
6243 end Remove_Unit_From_Visibility;
6245 --------
6246 -- sm --
6247 --------
6249 procedure sm is
6250 begin
6251 null;
6252 end sm;
6254 -------------
6255 -- Unchain --
6256 -------------
6258 procedure Unchain (E : Entity_Id) is
6259 Prev : Entity_Id;
6261 begin
6262 Prev := Current_Entity (E);
6264 if No (Prev) then
6265 return;
6267 elsif Prev = E then
6268 Set_Name_Entity_Id (Chars (E), Homonym (E));
6270 else
6271 while Present (Prev)
6272 and then Homonym (Prev) /= E
6273 loop
6274 Prev := Homonym (Prev);
6275 end loop;
6277 if Present (Prev) then
6278 Set_Homonym (Prev, Homonym (E));
6279 end if;
6280 end if;
6282 if Debug_Flag_I then
6283 Write_Str (" (homonym) unchain ");
6284 Write_Name (Chars (E));
6285 Write_Eol;
6286 end if;
6287 end Unchain;
6289 end Sem_Ch10;