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