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