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