Debian Codegraph @

gcc/ada/sem_ch10.adb

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