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