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