hppa: Revise REG+D address support to allow long displacements before reload
[official-gcc.git] / gcc / ada / sem_ch10.adb
blob7cca555f27670329865c73e6a371b9f0d943ea48
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-2023, 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 ------------------------------------------------------------------------------
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;
79 package body Sem_Ch10 is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Analyze_Context (N : Node_Id);
86 -- Analyzes items in the context clause of compilation unit
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.
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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).
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).
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.
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.
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.
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.
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.
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.
217 procedure Remove_Context_Clauses (N : Node_Id);
218 -- Subsidiary of previous one. Remove use_ and with_clauses
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).
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).
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.
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.
241 procedure Replace (Old_E, New_E : Entity_Id);
242 -- Replace Old_E by New_E on visibility list
244 procedure Unchain (E : Entity_Id);
245 -- Remove single entity from visibility list
247 procedure sm;
248 -- A dummy procedure, for debugging use, called just before analyzing the
249 -- main unit (after dealing with any context clauses).
251 --------------------------
252 -- Limited_With_Clauses --
253 --------------------------
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.
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.
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.
279 ------------------------------
280 -- Analyze_Compilation_Unit --
281 ------------------------------
283 procedure Analyze_Compilation_Unit (N : Node_Id) is
284 Unit_Node : constant Node_Id := Unit (N);
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.
295 ---------------------------
296 -- Check_Redundant_Withs --
297 ---------------------------
299 procedure Check_Redundant_Withs
300 (Context_Items : List_Id;
301 Spec_Context_Items : List_Id := No_List)
303 Clause : Node_Id;
305 procedure Process_Body_Clauses
306 (Context_List : List_Id;
307 Clause : Node_Id;
308 Used : out Boolean;
309 Used_Type_Or_Elab : out Boolean);
310 -- Examine the context clauses of a package body, trying to match the
311 -- name entity of Clause with any list element. If the match occurs
312 -- on a use package clause set Used to True, for a use type clause or
313 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
315 procedure Process_Spec_Clauses
316 (Context_List : List_Id;
317 Clause : Node_Id;
318 Used : out Boolean;
319 Withed : out Boolean;
320 Exit_On_Self : Boolean := False);
321 -- Examine the context clauses of a package spec, trying to match
322 -- the name entity of Clause with any list element. If the match
323 -- occurs on a use package clause, set Used to True, for a with
324 -- package clause other than Clause, set Withed to True. Limited
325 -- with clauses, implicitly generated with clauses and withs
326 -- having pragmas Elaborate or Elaborate_All applied to them are
327 -- skipped. Exit_On_Self is used to control the search loop and
328 -- force an exit whenever Clause sees itself in the search.
330 --------------------------
331 -- Process_Body_Clauses --
332 --------------------------
334 procedure Process_Body_Clauses
335 (Context_List : List_Id;
336 Clause : Node_Id;
337 Used : out Boolean;
338 Used_Type_Or_Elab : out Boolean)
340 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
341 Cont_Item : Node_Id;
342 Prag_Unit : Node_Id;
343 Use_Item : Node_Id;
345 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean;
346 -- In an expanded name in a use clause, if the prefix is a renamed
347 -- package, the entity is set to the original package as a result,
348 -- when checking whether the package appears in a previous with
349 -- clause, the renaming has to be taken into account, to prevent
350 -- spurious/incorrect warnings. A common case is use of Text_IO.
352 ---------------
353 -- Same_Unit --
354 ---------------
356 function Same_Unit (N : Node_Id; P : Entity_Id) return Boolean is
357 begin
358 return Entity (N) = P
359 or else (Present (Renamed_Entity (P))
360 and then Entity (N) = Renamed_Entity (P));
361 end Same_Unit;
363 -- Start of processing for Process_Body_Clauses
365 begin
366 Used := False;
367 Used_Type_Or_Elab := False;
369 Cont_Item := First (Context_List);
370 while Present (Cont_Item) loop
372 -- Package use clause
374 if Nkind (Cont_Item) = N_Use_Package_Clause
375 and then not Used
376 then
377 -- Search through use clauses
379 Use_Item := Name (Cont_Item);
381 -- Case of a direct use of the one we are looking for
383 if Entity (Use_Item) = Nam_Ent then
384 Used := True;
386 -- Handle nested case, as in "with P; use P.Q.R"
388 else
389 declare
390 UE : Node_Id;
392 begin
393 -- Loop through prefixes looking for match
395 UE := Use_Item;
396 while Nkind (UE) = N_Expanded_Name loop
397 if Same_Unit (Prefix (UE), Nam_Ent) then
398 Used := True;
399 exit;
400 end if;
402 UE := Prefix (UE);
403 end loop;
404 end;
405 end if;
407 -- USE TYPE clause
409 elsif Nkind (Cont_Item) = N_Use_Type_Clause
410 and then not Used_Type_Or_Elab
411 then
412 declare
413 UE : Node_Id;
415 begin
416 -- Loop through prefixes looking for a match
418 UE := Prefix (Subtype_Mark (Cont_Item));
419 loop
420 if not Used_Type_Or_Elab
421 and then Same_Unit (UE, Nam_Ent)
422 then
423 Used_Type_Or_Elab := True;
424 end if;
426 exit when Nkind (UE) /= N_Expanded_Name;
427 UE := Prefix (UE);
428 end loop;
429 end;
431 -- Pragma Elaborate or Elaborate_All
433 elsif Nkind (Cont_Item) = N_Pragma
434 and then
435 Pragma_Name_Unmapped (Cont_Item)
436 in Name_Elaborate | Name_Elaborate_All
437 and then not Used_Type_Or_Elab
438 then
439 Prag_Unit :=
440 First (Pragma_Argument_Associations (Cont_Item));
441 while Present (Prag_Unit) and then not Used_Type_Or_Elab loop
442 if Entity (Expression (Prag_Unit)) = Nam_Ent then
443 Used_Type_Or_Elab := True;
444 end if;
446 Next (Prag_Unit);
447 end loop;
448 end if;
450 Next (Cont_Item);
451 end loop;
452 end Process_Body_Clauses;
454 --------------------------
455 -- Process_Spec_Clauses --
456 --------------------------
458 procedure Process_Spec_Clauses
459 (Context_List : List_Id;
460 Clause : Node_Id;
461 Used : out Boolean;
462 Withed : out Boolean;
463 Exit_On_Self : Boolean := False)
465 Nam_Ent : constant Entity_Id := Entity (Name (Clause));
466 Cont_Item : Node_Id;
468 begin
469 Used := False;
470 Withed := False;
472 Cont_Item := First (Context_List);
473 while Present (Cont_Item) loop
475 -- Stop the search since the context items after Cont_Item have
476 -- already been examined in a previous iteration of the reverse
477 -- loop in Check_Redundant_Withs.
479 if Exit_On_Self
480 and Cont_Item = Clause
481 then
482 exit;
483 end if;
485 -- Package use clause
487 if Nkind (Cont_Item) = N_Use_Package_Clause
488 and then not Used
489 then
490 if Entity (Name (Cont_Item)) = Nam_Ent then
491 Used := True;
492 end if;
494 -- Package with clause. Avoid processing self, implicitly
495 -- generated with clauses or limited with clauses. Note that
496 -- we examine with clauses having pragmas Elaborate or
497 -- Elaborate_All applied to them due to cases such as:
499 -- with Pack;
500 -- with Pack;
501 -- pragma Elaborate (Pack);
503 -- In this case, the second with clause is redundant since
504 -- the pragma applies only to the first "with Pack;".
506 -- Note that we only consider with_clauses that comes from
507 -- source. In the case of renamings used as prefixes of names
508 -- in with_clauses, we generate a with_clause for the prefix,
509 -- which we do not treat as implicit because it is needed for
510 -- visibility analysis, but is also not redundant.
512 elsif Nkind (Cont_Item) = N_With_Clause
513 and then Comes_From_Source (Cont_Item)
514 and then not Implicit_With (Cont_Item)
515 and then not Limited_Present (Cont_Item)
516 and then Cont_Item /= Clause
517 and then Entity (Name (Cont_Item)) = Nam_Ent
518 then
519 Withed := True;
520 end if;
522 Next (Cont_Item);
523 end loop;
524 end Process_Spec_Clauses;
526 -- Start of processing for Check_Redundant_Withs
528 begin
529 Clause := Last (Context_Items);
530 while Present (Clause) loop
532 -- Avoid checking implicitly generated with clauses, limited with
533 -- clauses or withs that have pragma Elaborate or Elaborate_All.
535 if Nkind (Clause) = N_With_Clause
536 and then not Implicit_With (Clause)
537 and then not Limited_Present (Clause)
538 and then not Elaborate_Present (Clause)
540 -- With_clauses introduced for renamings of parent clauses
541 -- are not marked implicit because they need to be properly
542 -- installed, but they do not come from source and do not
543 -- require warnings.
545 and then Comes_From_Source (Clause)
546 then
547 -- Package body-to-spec check
549 if Present (Spec_Context_Items) then
550 declare
551 Used_In_Body : Boolean;
552 Used_In_Spec : Boolean;
553 Used_Type_Or_Elab : Boolean;
554 Withed_In_Spec : Boolean;
556 begin
557 Process_Spec_Clauses
558 (Context_List => Spec_Context_Items,
559 Clause => Clause,
560 Used => Used_In_Spec,
561 Withed => Withed_In_Spec);
563 Process_Body_Clauses
564 (Context_List => Context_Items,
565 Clause => Clause,
566 Used => Used_In_Body,
567 Used_Type_Or_Elab => Used_Type_Or_Elab);
569 -- "Type Elab" refers to the presence of either a use
570 -- type clause, pragmas Elaborate or Elaborate_All.
572 -- +---------------+---------------------------+------+
573 -- | Spec | Body | Warn |
574 -- +--------+------+--------+------+-----------+------+
575 -- | Withed | Used | Withed | Used | Type Elab | |
576 -- | X | | X | | | X |
577 -- | X | | X | X | | |
578 -- | X | | X | | X | |
579 -- | X | | X | X | X | |
580 -- | X | X | X | | | X |
581 -- | X | X | X | | X | |
582 -- | X | X | X | X | | X |
583 -- | X | X | X | X | X | |
584 -- +--------+------+--------+------+-----------+------+
586 if (Withed_In_Spec
587 and then not Used_Type_Or_Elab)
588 and then
589 ((not Used_In_Spec and then not Used_In_Body)
590 or else Used_In_Spec)
591 then
592 Error_Msg_N -- CODEFIX
593 ("redundant with clause in body?r?", Clause);
594 end if;
595 end;
597 -- Standalone package spec or body check
599 else
600 if Is_Ancestor_Package (Entity (Name (Clause)),
601 Defining_Entity (Unit_Node))
602 then
603 Error_Msg_N
604 ("unnecessary with of ancestor?r?", Clause);
605 end if;
607 declare
608 Dummy : Boolean := False;
609 Withed : Boolean := False;
611 begin
612 -- The mechanism for examining the context clauses of a
613 -- package spec can be applied to package body clauses.
615 Process_Spec_Clauses
616 (Context_List => Context_Items,
617 Clause => Clause,
618 Used => Dummy,
619 Withed => Withed,
620 Exit_On_Self => True);
622 if Withed then
623 Error_Msg_N -- CODEFIX
624 ("redundant with clause?r?", Clause);
625 end if;
626 end;
627 end if;
628 end if;
630 Prev (Clause);
631 end loop;
632 end Check_Redundant_Withs;
634 -- Local variables
636 Main_Cunit : constant Node_Id := Cunit (Main_Unit);
637 Lib_Unit : Node_Id := Library_Unit (N);
638 Par_Spec_Name : Unit_Name_Type;
639 Spec_Id : Entity_Id;
640 Unum : Unit_Number_Type;
641 Options : Style_Check_Options;
643 -- Start of processing for Analyze_Compilation_Unit
645 begin
646 Exp_Put_Image.Preload_Root_Buffer_Type (N);
648 Process_Compilation_Unit_Pragmas (N);
650 -- If the unit is a subunit whose parent has not been analyzed (which
651 -- indicates that the main unit is a subunit, either the current one or
652 -- one of its descendants) then the subunit is compiled as part of the
653 -- analysis of the parent, which we proceed to do. Basically this gets
654 -- handled from the top down and we don't want to do anything at this
655 -- level (i.e. this subunit will be handled on the way down from the
656 -- parent), so at this level we immediately return. If the subunit ends
657 -- up not analyzed, it means that the parent did not contain a stub for
658 -- it, or that there errors were detected in some ancestor.
660 if Nkind (Unit_Node) = N_Subunit and then not Analyzed (Lib_Unit) then
661 Semantics (Lib_Unit);
663 if not Analyzed (Proper_Body (Unit_Node)) then
664 if Serious_Errors_Detected > 0 then
665 Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
666 else
667 Error_Msg_N ("missing stub for subunit", N);
668 end if;
669 end if;
671 return;
672 end if;
674 -- Analyze context (this will call Sem recursively for with'ed units) To
675 -- detect circularities among with-clauses that are not caught during
676 -- loading, we set the Context_Pending flag on the current unit. If the
677 -- flag is already set there is a potential circularity. We exclude
678 -- predefined units from this check because they are known to be safe.
679 -- We also exclude package bodies that are present because circularities
680 -- between bodies are harmless (and necessary).
682 if Context_Pending (N) then
683 declare
684 Circularity : Boolean := True;
686 begin
687 if In_Predefined_Unit (N) then
688 Circularity := False;
690 else
691 for U in Main_Unit + 1 .. Last_Unit loop
692 if Nkind (Unit (Cunit (U))) = N_Package_Body
693 and then not Analyzed (Cunit (U))
694 then
695 Circularity := False;
696 exit;
697 end if;
698 end loop;
699 end if;
701 if Circularity then
702 Error_Msg_N ("circular dependency caused by with_clauses", N);
703 Error_Msg_N
704 ("\possibly missing limited_with clause"
705 & " in one of the following", N);
707 for U in Main_Unit .. Last_Unit loop
708 if Context_Pending (Cunit (U)) then
709 Error_Msg_Unit_1 := Get_Unit_Name (Unit (Cunit (U)));
710 Error_Msg_N ("\unit$", N);
711 end if;
712 end loop;
714 raise Unrecoverable_Error;
715 end if;
716 end;
717 else
718 Set_Context_Pending (N);
719 end if;
721 -- Store the style check options before analyzing context pragmas that
722 -- might change them for this compilation unit.
724 Save_Style_Check_Options (Options);
726 Analyze_Context (N);
728 Set_Context_Pending (N, False);
730 -- If the unit is a package body, the spec is already loaded and must be
731 -- analyzed first, before we analyze the body.
733 if Nkind (Unit_Node) = N_Package_Body then
735 -- If no Lib_Unit, then there was a serious previous error, so just
736 -- ignore the entire analysis effort.
738 if No (Lib_Unit) then
739 Check_Error_Detected;
740 return;
742 else
743 -- Analyze the package spec
745 Semantics (Lib_Unit);
747 -- Check for unused with's
749 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
751 -- Verify that the library unit is a package declaration
753 if Nkind (Unit (Lib_Unit)) not in
754 N_Package_Declaration | N_Generic_Package_Declaration
755 then
756 Error_Msg_N
757 ("no legal package declaration for package body", N);
758 return;
760 -- Otherwise, the entity in the declaration is visible. Update the
761 -- version to reflect dependence of this body on the spec.
763 else
764 Spec_Id := Defining_Entity (Unit (Lib_Unit));
765 Set_Is_Immediately_Visible (Spec_Id, True);
766 Version_Update (N, Lib_Unit);
768 if Nkind (Defining_Unit_Name (Unit_Node)) =
769 N_Defining_Program_Unit_Name
770 then
771 Generate_Parent_References (Unit_Node, Scope (Spec_Id));
772 end if;
773 end if;
774 end if;
776 -- If the unit is a subprogram body, then we similarly need to analyze
777 -- its spec. However, things are a little simpler in this case, because
778 -- here, this analysis is done mostly for error checking and consistency
779 -- purposes (but not only, e.g. there could be a contract on the spec),
780 -- so there's nothing else to be done.
782 elsif Nkind (Unit_Node) = N_Subprogram_Body then
783 if Acts_As_Spec (N) then
785 -- If the subprogram body is a child unit, we must create a
786 -- declaration for it, in order to properly load the parent(s).
787 -- After this, the original unit does not acts as a spec, because
788 -- there is an explicit one. If this unit appears in a context
789 -- clause, then an implicit with on the parent will be added when
790 -- installing the context. If this is the main unit, there is no
791 -- Unit_Table entry for the declaration (it has the unit number
792 -- of the main unit) and code generation is unaffected.
794 Unum := Get_Cunit_Unit_Number (N);
795 Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
797 if Present (Par_Spec_Name) then
798 Unum :=
799 Load_Unit
800 (Load_Name => Par_Spec_Name,
801 Required => True,
802 Subunit => False,
803 Error_Node => N);
805 if Unum /= No_Unit then
807 -- Build subprogram declaration and attach parent unit to it
808 -- This subprogram declaration does not come from source,
809 -- Nevertheless the backend must generate debugging info for
810 -- it, and this must be indicated explicitly. We also mark
811 -- the body entity as a child unit now, to prevent a
812 -- cascaded error if the spec entity cannot be entered
813 -- in its scope. Finally we create a Units table entry for
814 -- the subprogram declaration, to maintain a one-to-one
815 -- correspondence with compilation unit nodes. This is
816 -- critical for the tree traversals performed by CodePeer.
818 declare
819 Loc : constant Source_Ptr := Sloc (N);
820 SCS : constant Boolean :=
821 Get_Comes_From_Source_Default;
823 begin
824 Set_Comes_From_Source_Default (False);
826 -- Note: We copy the Context_Items from the explicit body
827 -- to the implicit spec, setting the former to Empty_List
828 -- to preserve the treeish nature of the tree, during
829 -- analysis of the spec. Then we put it back the way it
830 -- was -- copy the Context_Items from the spec to the
831 -- body, and set the spec Context_Items to Empty_List.
832 -- It is necessary to preserve the treeish nature,
833 -- because otherwise we will call End_Use_* twice on the
834 -- same thing.
836 Lib_Unit :=
837 Make_Compilation_Unit (Loc,
838 Context_Items => Context_Items (N),
839 Unit =>
840 Make_Subprogram_Declaration (Sloc (N),
841 Specification =>
842 Copy_Separate_Tree
843 (Specification (Unit_Node))),
844 Aux_Decls_Node =>
845 Make_Compilation_Unit_Aux (Loc));
847 Set_Context_Items (N, Empty_List);
848 Set_Library_Unit (N, Lib_Unit);
849 Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
850 Make_Child_Decl_Unit (N);
851 Semantics (Lib_Unit);
853 -- Now that a separate declaration exists, the body
854 -- of the child unit does not act as spec any longer.
856 Set_Acts_As_Spec (N, False);
857 Move_Aspects (From => Unit_Node, To => Unit (Lib_Unit));
858 Set_Is_Child_Unit (Defining_Entity (Unit_Node));
859 Set_Debug_Info_Needed (Defining_Entity (Unit (Lib_Unit)));
860 Set_Comes_From_Source_Default (SCS);
862 -- Restore Context_Items to the body
864 Set_Context_Items (N, Context_Items (Lib_Unit));
865 Set_Context_Items (Lib_Unit, Empty_List);
866 end;
867 end if;
868 end if;
870 -- Here for subprogram with separate declaration
872 else
873 Semantics (Lib_Unit);
874 Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
875 Version_Update (N, Lib_Unit);
876 end if;
878 -- If this is a child unit, generate references to the parents
880 if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
881 N_Defining_Program_Unit_Name
882 then
883 Generate_Parent_References
884 (Specification (Unit_Node),
885 Scope (Defining_Entity (Unit (Lib_Unit))));
886 end if;
887 end if;
889 -- If it is a child unit, the parent must be elaborated first and we
890 -- update version, since we are dependent on our parent.
892 if Is_Child_Spec (Unit_Node) then
894 -- The analysis of the parent is done with style checks off
896 declare
897 Save_Style_Check : constant Boolean := Style_Check;
899 begin
900 if not GNAT_Mode then
901 Style_Check := False;
902 end if;
904 Semantics (Parent_Spec (Unit_Node));
905 Version_Update (N, Parent_Spec (Unit_Node));
907 -- Restore style check settings
909 Style_Check := Save_Style_Check;
910 end;
911 end if;
913 -- With the analysis done, install the context. Note that we can't
914 -- install the context from the with clauses as we analyze them, because
915 -- each with clause must be analyzed in a clean visibility context, so
916 -- we have to wait and install them all at once.
918 Install_Context (N);
920 if Is_Child_Spec (Unit_Node) then
922 -- Set the entities of all parents in the program_unit_name
924 Generate_Parent_References
925 (Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
926 end if;
928 -- All components of the context: with-clauses, library unit, ancestors
929 -- if any, (and their context) are analyzed and installed.
931 -- Call special debug routine sm if this is the main unit
933 if Current_Sem_Unit = Main_Unit then
935 end if;
937 -- Now analyze the unit (package, subprogram spec, body) itself
939 Analyze (Unit_Node);
941 if Warn_On_Redundant_Constructs then
942 Check_Redundant_Withs (Context_Items (N));
944 if Nkind (Unit_Node) = N_Package_Body then
945 Check_Redundant_Withs
946 (Context_Items => Context_Items (N),
947 Spec_Context_Items => Context_Items (Lib_Unit));
948 end if;
949 end if;
951 -- The above call might have made Unit_Node an N_Subprogram_Body from
952 -- something else, so propagate any Acts_As_Spec flag.
954 if Nkind (Unit_Node) = N_Subprogram_Body
955 and then Acts_As_Spec (Unit_Node)
956 then
957 Set_Acts_As_Spec (N);
958 end if;
960 -- Register predefined units in Rtsfind
962 if In_Predefined_Unit (N) then
963 Set_RTU_Loaded (Unit_Node);
964 end if;
966 -- Treat compilation unit pragmas that appear after the library unit
968 declare
969 Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
970 begin
971 while Present (Prag_Node) loop
972 Analyze (Prag_Node);
973 Next (Prag_Node);
974 end loop;
975 end;
977 -- Analyze the contract of a [generic] subprogram that acts as a
978 -- compilation unit after all compilation pragmas have been analyzed.
980 if Nkind (Unit_Node) in
981 N_Generic_Subprogram_Declaration | N_Subprogram_Declaration
982 then
983 Analyze_Entry_Or_Subprogram_Contract (Defining_Entity (Unit_Node));
984 end if;
986 -- Generate distribution stubs if requested and no error
988 if N = Main_Cunit
989 and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
990 or else
991 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
992 and then Fatal_Error (Main_Unit) /= Error_Detected
993 then
994 if Is_RCI_Pkg_Spec_Or_Body (N) then
996 -- Regular RCI package
998 Add_Stub_Constructs (N);
1000 elsif (Nkind (Unit_Node) = N_Package_Declaration
1001 and then Is_Shared_Passive (Defining_Entity
1002 (Specification (Unit_Node))))
1003 or else (Nkind (Unit_Node) = N_Package_Body
1004 and then
1005 Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
1006 then
1007 -- Shared passive package
1009 Add_Stub_Constructs (N);
1011 elsif Nkind (Unit_Node) = N_Package_Instantiation
1012 and then
1013 Is_Remote_Call_Interface
1014 (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
1015 then
1016 -- Instantiation of a RCI generic package
1018 Add_Stub_Constructs (N);
1019 end if;
1020 end if;
1022 -- Build dispatch tables of library-level tagged types only now because
1023 -- the generation of distribution stubs above may create some of them.
1025 if Expander_Active and then Tagged_Type_Expansion then
1026 case Nkind (Unit_Node) is
1027 when N_Package_Declaration | N_Package_Body =>
1028 Build_Static_Dispatch_Tables (Unit_Node);
1030 when N_Package_Instantiation =>
1031 Build_Static_Dispatch_Tables (Instance_Spec (Unit_Node));
1033 when others =>
1034 null;
1035 end case;
1036 end if;
1038 -- Remove unit from visibility, so that environment is clean for the
1039 -- next compilation, which is either the main unit or some other unit
1040 -- in the context.
1042 if Nkind (Unit_Node) in N_Package_Declaration
1043 | N_Package_Renaming_Declaration
1044 | N_Subprogram_Declaration
1045 | N_Generic_Declaration
1046 or else (Nkind (Unit_Node) = N_Subprogram_Body
1047 and then Acts_As_Spec (Unit_Node))
1048 then
1049 Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
1051 -- If the unit is an instantiation whose body will be elaborated for
1052 -- inlining purposes, use the proper entity of the instance. The entity
1053 -- may be missing if the instantiation was illegal.
1055 elsif Nkind (Unit_Node) = N_Package_Instantiation
1056 and then not Error_Posted (Unit_Node)
1057 and then Present (Instance_Spec (Unit_Node))
1058 then
1059 Remove_Unit_From_Visibility
1060 (Defining_Entity (Instance_Spec (Unit_Node)));
1062 elsif Nkind (Unit_Node) = N_Package_Body
1063 or else (Nkind (Unit_Node) = N_Subprogram_Body
1064 and then not Acts_As_Spec (Unit_Node))
1065 then
1066 -- Bodies that are not the main unit are compiled if they are generic
1067 -- or contain generic or inlined units. Their analysis brings in the
1068 -- context of the corresponding spec (unit declaration) which must be
1069 -- removed as well, to return the compilation environment to its
1070 -- proper state.
1072 Remove_Context (Lib_Unit);
1073 Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
1074 end if;
1076 -- Last step is to deinstall the context we just installed as well as
1077 -- the unit just compiled.
1079 Remove_Context (N);
1081 -- When generating code for a non-generic main unit, check that withed
1082 -- generic units have a body if they need it, even if the units have not
1083 -- been instantiated. Force the load of the bodies to produce the proper
1084 -- error if the body is absent. The same applies to GNATprove mode, with
1085 -- the added benefit of capturing global references within the generic.
1086 -- This in turn allows for proper inlining of subprogram bodies without
1087 -- a previous declaration.
1089 if Get_Cunit_Unit_Number (N) = Main_Unit
1090 and then ((Operating_Mode = Generate_Code and then Expander_Active)
1091 or else
1092 (Operating_Mode = Check_Semantics and then GNATprove_Mode))
1093 then
1094 -- Check whether the source for the body of the unit must be included
1095 -- in a standalone library.
1097 Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
1099 -- Indicate that the main unit is now analyzed, to catch possible
1100 -- circularities between it and generic bodies. Remove main unit from
1101 -- visibility. This might seem superfluous, but the main unit must
1102 -- not be visible in the generic body expansions that follow.
1104 Set_Analyzed (N, True);
1105 Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
1107 declare
1108 Item : Node_Id;
1109 Nam : Entity_Id;
1110 Un : Unit_Number_Type;
1112 Save_Style_Check : constant Boolean := Style_Check;
1114 begin
1115 Item := First (Context_Items (N));
1116 while Present (Item) loop
1118 -- Check for explicit with clause
1120 if Nkind (Item) = N_With_Clause
1121 and then not Implicit_With (Item)
1123 -- Ada 2005 (AI-50217): Ignore limited-withed units
1125 and then not Limited_Present (Item)
1126 then
1127 Nam := Entity (Name (Item));
1129 -- Compile the generic subprogram, unless it is intrinsic or
1130 -- imported so no body is required, or generic package body
1131 -- if the package spec requires a body.
1133 if (Is_Generic_Subprogram (Nam)
1134 and then not Is_Intrinsic_Subprogram (Nam)
1135 and then not Is_Imported (Nam))
1136 or else (Ekind (Nam) = E_Generic_Package
1137 and then Unit_Requires_Body (Nam))
1138 then
1139 Style_Check := False;
1141 if Present (Renamed_Entity (Nam)) then
1142 Un :=
1143 Load_Unit
1144 (Load_Name =>
1145 Get_Body_Name
1146 (Get_Unit_Name
1147 (Unit_Declaration_Node
1148 (Renamed_Entity (Nam)))),
1149 Required => False,
1150 Subunit => False,
1151 Error_Node => N,
1152 Renamings => True);
1153 else
1154 Un :=
1155 Load_Unit
1156 (Load_Name =>
1157 Get_Body_Name (Get_Unit_Name (Item)),
1158 Required => False,
1159 Subunit => False,
1160 Error_Node => N,
1161 Renamings => True);
1162 end if;
1164 if Un = No_Unit then
1165 Error_Msg_NE
1166 ("body of generic unit& not found", Item, Nam);
1167 exit;
1169 elsif not Analyzed (Cunit (Un))
1170 and then Un /= Main_Unit
1171 and then Fatal_Error (Un) /= Error_Detected
1172 then
1173 Style_Check := False;
1174 Semantics (Cunit (Un));
1175 end if;
1176 end if;
1177 end if;
1179 Next (Item);
1180 end loop;
1182 -- Restore style checks settings
1184 Style_Check := Save_Style_Check;
1185 end;
1187 -- In GNATprove mode, force the loading of an Interrupt_Priority when
1188 -- processing compilation units with potentially "main" subprograms.
1189 -- This is required for the ceiling priority protocol checks, which
1190 -- are triggered by these subprograms.
1192 if GNATprove_Mode
1193 and then Nkind (Unit_Node) in N_Function_Instantiation
1194 | N_Procedure_Instantiation
1195 | N_Subprogram_Body
1196 then
1197 declare
1198 Spec : Node_Id;
1200 begin
1201 case Nkind (Unit_Node) is
1202 when N_Subprogram_Body =>
1203 Spec := Specification (Unit_Node);
1205 when N_Subprogram_Instantiation =>
1206 Spec :=
1207 Subprogram_Specification (Entity (Name (Unit_Node)));
1209 when others =>
1210 raise Program_Error;
1211 end case;
1213 pragma Assert (Nkind (Spec) in N_Subprogram_Specification);
1215 -- Main subprogram must have no parameters, and if it is a
1216 -- function, it must return an integer.
1218 if No (Parameter_Specifications (Spec))
1219 and then (Nkind (Spec) = N_Procedure_Specification
1220 or else
1221 Is_Integer_Type (Etype (Result_Definition (Spec))))
1222 then
1223 SPARK_Implicit_Load (RE_Interrupt_Priority);
1224 end if;
1225 end;
1226 end if;
1227 end if;
1229 -- Deal with creating elaboration counter if needed. We create an
1230 -- elaboration counter only for units that come from source since
1231 -- units manufactured by the compiler never need elab checks.
1233 if Comes_From_Source (N)
1234 and then Nkind (Unit_Node) in N_Package_Declaration
1235 | N_Generic_Package_Declaration
1236 | N_Subprogram_Declaration
1237 | N_Generic_Subprogram_Declaration
1238 then
1239 declare
1240 Loc : constant Source_Ptr := Sloc (N);
1241 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
1243 begin
1244 Spec_Id := Defining_Entity (Unit_Node);
1245 Generate_Definition (Spec_Id);
1247 -- See if an elaboration entity is required for possible access
1248 -- before elaboration checking. Note that we must allow for this
1249 -- even if -gnatE is not set, since a client may be compiled in
1250 -- -gnatE mode and reference the entity.
1252 -- These entities are also used by the binder to prevent multiple
1253 -- attempts to execute the elaboration code for the library case
1254 -- where the elaboration routine might otherwise be called more
1255 -- than once.
1257 -- They are also needed to ensure explicit visibility from the
1258 -- binder generated code of all the units involved in a partition
1259 -- when control-flow preservation is requested.
1261 if not Opt.Suppress_Control_Flow_Optimizations
1262 and then
1263 ( -- Pure units do not need checks
1265 Is_Pure (Spec_Id)
1267 -- Preelaborated units do not need checks
1269 or else Is_Preelaborated (Spec_Id)
1271 -- No checks needed if pragma Elaborate_Body present
1273 or else Has_Pragma_Elaborate_Body (Spec_Id)
1275 -- No checks needed if unit does not require a body
1277 or else not Unit_Requires_Body (Spec_Id)
1279 -- No checks needed for predefined files
1281 or else Is_Predefined_Unit (Unum)
1283 -- No checks required if no separate spec
1285 or else Acts_As_Spec (N)
1287 then
1288 -- This is a case where we only need the entity for checking to
1289 -- prevent multiple elaboration checks.
1291 Set_Elaboration_Entity_Required (Spec_Id, False);
1293 -- Otherwise the unit requires an elaboration entity because it
1294 -- carries a body.
1296 else
1297 Set_Elaboration_Entity_Required (Spec_Id);
1298 end if;
1300 Build_Elaboration_Entity (N, Spec_Id);
1301 end;
1302 end if;
1304 -- Freeze the compilation unit entity. This for sure is needed because
1305 -- of some warnings that can be output (see Freeze_Subprogram), but may
1306 -- in general be required. If freezing actions result, place them in the
1307 -- compilation unit actions list, and analyze them.
1309 declare
1310 L : constant List_Id :=
1311 Freeze_Entity (Cunit_Entity (Current_Sem_Unit), N);
1312 begin
1313 while Is_Non_Empty_List (L) loop
1314 Insert_Library_Level_Action (Remove_Head (L));
1315 end loop;
1316 end;
1318 Set_Analyzed (N);
1320 -- Call Check_Package_Body so that a body containing subprograms with
1321 -- Inline_Always can be made available for front end inlining.
1323 if Nkind (Unit_Node) = N_Package_Declaration
1324 and then Get_Cunit_Unit_Number (N) /= Main_Unit
1326 -- We don't need to do this if the Expander is not active, since there
1327 -- is no code to inline.
1329 and then Expander_Active
1330 then
1331 declare
1332 Save_Style_Check : constant Boolean := Style_Check;
1333 Save_Warning : constant Warning_Mode_Type := Warning_Mode;
1334 Options : Style_Check_Options;
1336 begin
1337 Save_Style_Check_Options (Options);
1338 Reset_Style_Check_Options;
1339 Opt.Warning_Mode := Suppress;
1341 Check_Package_Body_For_Inlining (N, Defining_Entity (Unit_Node));
1343 Reset_Style_Check_Options;
1344 Set_Style_Check_Options (Options);
1345 Style_Check := Save_Style_Check;
1346 Warning_Mode := Save_Warning;
1347 end;
1348 end if;
1350 -- If we are generating obsolescent warnings, then here is where we
1351 -- generate them for the with'ed items. The reason for this special
1352 -- processing is that the normal mechanism of generating the warnings
1353 -- for referenced entities does not work for context clause references.
1354 -- That's because when we first analyze the context, it is too early to
1355 -- know if the with'ing unit is itself obsolescent (which suppresses
1356 -- the warnings).
1358 if not GNAT_Mode
1359 and then Warn_On_Obsolescent_Feature
1360 and then Nkind (Unit_Node) not in N_Generic_Instantiation
1361 then
1362 -- Push current compilation unit as scope, so that the test for
1363 -- being within an obsolescent unit will work correctly. The check
1364 -- is not performed within an instantiation, because the warning
1365 -- will have been emitted in the corresponding generic unit.
1367 Push_Scope (Defining_Entity (Unit_Node));
1369 -- Loop through context items to deal with with clauses
1371 declare
1372 Item : Node_Id;
1373 Nam : Node_Id;
1374 Ent : Entity_Id;
1376 begin
1377 Item := First (Context_Items (N));
1378 while Present (Item) loop
1379 if Nkind (Item) = N_With_Clause
1381 -- Suppress this check in limited-withed units. Further work
1382 -- needed here if we decide to incorporate this check on
1383 -- limited-withed units.
1385 and then not Limited_Present (Item)
1386 then
1387 Nam := Name (Item);
1388 Ent := Entity (Nam);
1390 if Is_Obsolescent (Ent) then
1391 Output_Obsolescent_Entity_Warnings (Nam, Ent);
1392 end if;
1393 end if;
1395 Next (Item);
1396 end loop;
1397 end;
1399 -- Remove temporary install of current unit as scope
1401 Pop_Scope;
1402 end if;
1404 -- Finally restore all the original style check options
1406 Set_Style_Check_Options (Options);
1408 -- If No_Elaboration_Code_All was encountered, this is where we do the
1409 -- transitive test of with'ed units to make sure they have the aspect.
1410 -- This is delayed till the end of analyzing the compilation unit to
1411 -- ensure that the pragma/aspect, if present, has been analyzed.
1413 Check_No_Elab_Code_All (N);
1415 -- If this is a main compilation containing a package declaration that
1416 -- requires no package body, and the profile of some subprogram depends
1417 -- on shadow incomplete entities then perform full analysis of its
1418 -- limited-with units.
1420 Analyze_Required_Limited_With_Units (N);
1421 end Analyze_Compilation_Unit;
1423 ---------------------
1424 -- Analyze_Context --
1425 ---------------------
1427 procedure Analyze_Context (N : Node_Id) is
1428 Ukind : constant Node_Kind := Nkind (Unit (N));
1429 Item : Node_Id;
1431 begin
1432 -- First process all configuration pragmas at the start of the context
1433 -- items. Strictly these are not part of the context clause, but that
1434 -- is where the parser puts them. In any case for sure we must analyze
1435 -- these before analyzing the actual context items, since they can have
1436 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1437 -- be with'ed as a result of changing categorizations in Ada 2005).
1439 Item := First (Context_Items (N));
1440 while Present (Item)
1441 and then Nkind (Item) = N_Pragma
1442 and then Pragma_Name (Item) in Configuration_Pragma_Names
1443 loop
1444 Analyze (Item);
1445 Next (Item);
1446 end loop;
1448 -- This is the point at which we capture the configuration settings
1449 -- for the unit. At the moment only the Optimize_Alignment setting
1450 -- needs to be captured. Probably more later ???
1452 if Optimize_Alignment_Local then
1453 Set_OA_Setting (Current_Sem_Unit, 'L');
1454 else
1455 Set_OA_Setting (Current_Sem_Unit, Optimize_Alignment);
1456 end if;
1458 -- Loop through actual context items. This is done in two passes:
1460 -- a) The first pass analyzes nonlimited with clauses and also any
1461 -- configuration pragmas (we need to get the latter analyzed right
1462 -- away, since they can affect processing of subsequent items).
1464 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1466 while Present (Item) loop
1468 -- For with clause, analyze the with clause, and then update the
1469 -- version, since we are dependent on a unit that we with.
1471 if Nkind (Item) = N_With_Clause
1472 and then not Limited_Present (Item)
1473 then
1474 -- Skip analyzing with clause if no unit, nothing to do (this
1475 -- happens for a with that references a non-existent unit).
1477 if Present (Library_Unit (Item)) then
1479 -- Skip analyzing with clause if this is a with_clause for
1480 -- the main unit, which happens if a subunit has a useless
1481 -- with_clause on its parent.
1483 if Library_Unit (Item) /= Cunit (Current_Sem_Unit) then
1484 Analyze (Item);
1486 -- Here for the case of a useless with for the main unit
1488 else
1489 Set_Entity (Name (Item), Cunit_Entity (Current_Sem_Unit));
1490 end if;
1491 end if;
1493 -- Do version update (skipped for implicit with)
1495 if not Implicit_With (Item) then
1496 Version_Update (N, Library_Unit (Item));
1497 end if;
1499 -- Skip pragmas. Configuration pragmas at the start were handled in
1500 -- the loop above, and remaining pragmas are not processed until we
1501 -- actually install the context (see Install_Context). We delay the
1502 -- analysis of these pragmas to make sure that we have installed all
1503 -- the implicit with's on parent units.
1505 -- Skip use clauses at this stage, since we don't want to do any
1506 -- installing of potentially use-visible entities until we
1507 -- actually install the complete context (in Install_Context).
1508 -- Otherwise things can get installed in the wrong context.
1510 else
1511 null;
1512 end if;
1514 Next (Item);
1515 end loop;
1517 -- Second pass: examine all limited_with clauses. All other context
1518 -- items are ignored in this pass.
1520 Item := First (Context_Items (N));
1521 while Present (Item) loop
1522 if Nkind (Item) = N_With_Clause
1523 and then Limited_Present (Item)
1524 then
1525 -- No need to check errors on implicitly generated limited-with
1526 -- clauses.
1528 if not Implicit_With (Item) then
1530 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1531 -- properly rejected, including renaming declarations.
1533 if Ukind not in N_Package_Declaration
1534 | N_Subprogram_Declaration
1535 | N_Generic_Declaration
1536 | N_Generic_Instantiation
1537 then
1538 Error_Msg_N ("limited with_clause not allowed here", Item);
1540 -- Check wrong use of a limited with clause applied to the
1541 -- compilation unit containing the limited-with clause.
1543 -- limited with P.Q;
1544 -- package P.Q is ...
1546 elsif Unit (Library_Unit (Item)) = Unit (N) then
1547 Error_Msg_N ("wrong use of limited-with clause", Item);
1549 -- Check wrong use of limited-with clause applied to some
1550 -- immediate ancestor.
1552 elsif Is_Child_Spec (Unit (N)) then
1553 declare
1554 Lib_U : constant Entity_Id := Unit (Library_Unit (Item));
1555 P : Node_Id;
1557 begin
1558 P := Parent_Spec (Unit (N));
1559 loop
1560 if Unit (P) = Lib_U then
1561 Error_Msg_N
1562 ("limited with_clause cannot name ancestor",
1563 Item);
1564 exit;
1565 end if;
1567 exit when not Is_Child_Spec (Unit (P));
1568 P := Parent_Spec (Unit (P));
1569 end loop;
1570 end;
1571 end if;
1573 -- Check if the limited-withed unit is already visible through
1574 -- some context clause of the current compilation unit or some
1575 -- ancestor of the current compilation unit.
1577 declare
1578 Lim_Unit_Name : constant Node_Id := Name (Item);
1579 Comp_Unit : Node_Id;
1580 It : Node_Id;
1581 Unit_Name : Node_Id;
1583 begin
1584 Comp_Unit := N;
1585 loop
1586 It := First (Context_Items (Comp_Unit));
1587 while Present (It) loop
1588 if Item /= It
1589 and then Nkind (It) = N_With_Clause
1590 and then not Limited_Present (It)
1591 and then Nkind (Unit (Library_Unit (It))) in
1592 N_Package_Declaration |
1593 N_Package_Renaming_Declaration
1594 then
1595 if Nkind (Unit (Library_Unit (It))) =
1596 N_Package_Declaration
1597 then
1598 Unit_Name := Name (It);
1599 else
1600 Unit_Name := Name (Unit (Library_Unit (It)));
1601 end if;
1603 -- Check if the named package (or some ancestor)
1604 -- leaves visible the full-view of the unit given
1605 -- in the limited-with clause.
1607 loop
1608 if Designate_Same_Unit (Lim_Unit_Name,
1609 Unit_Name)
1610 then
1611 Error_Msg_Sloc := Sloc (It);
1612 Error_Msg_N
1613 ("simultaneous visibility of limited and "
1614 & "unlimited views not allowed", Item);
1615 Error_Msg_N
1616 ("\unlimited view visible through context "
1617 & "clause #", Item);
1618 exit;
1620 elsif Nkind (Unit_Name) = N_Identifier then
1621 exit;
1622 end if;
1624 Unit_Name := Prefix (Unit_Name);
1625 end loop;
1626 end if;
1628 Next (It);
1629 end loop;
1631 exit when not Is_Child_Spec (Unit (Comp_Unit));
1633 Comp_Unit := Parent_Spec (Unit (Comp_Unit));
1634 end loop;
1635 end;
1636 end if;
1638 -- Skip analyzing with clause if no unit, see above
1640 if Present (Library_Unit (Item)) then
1641 Analyze (Item);
1642 end if;
1644 -- A limited_with does not impose an elaboration order, but there
1645 -- is a semantic dependency for recompilation purposes.
1647 if not Implicit_With (Item) then
1648 Version_Update (N, Library_Unit (Item));
1649 end if;
1651 -- Pragmas and use clauses and with clauses other than limited with's
1652 -- are ignored in this pass through the context items.
1654 else
1655 null;
1656 end if;
1658 Next (Item);
1659 end loop;
1660 end Analyze_Context;
1662 -------------------------------
1663 -- Analyze_Package_Body_Stub --
1664 -------------------------------
1666 procedure Analyze_Package_Body_Stub (N : Node_Id) is
1667 Id : constant Entity_Id := Defining_Entity (N);
1668 Nam : Entity_Id;
1669 Opts : Config_Switches_Type;
1671 begin
1672 -- The package declaration must be in the current declarative part
1674 Check_Stub_Level (N);
1675 Nam := Current_Entity_In_Scope (Id);
1677 if No (Nam) or else not Is_Package_Or_Generic_Package (Nam) then
1678 Error_Msg_N ("missing specification for package stub", N);
1680 elsif Has_Completion (Nam)
1681 and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
1682 then
1683 Error_Msg_N ("duplicate or redundant stub for package", N);
1685 else
1686 -- Retain and restore the configuration options of the enclosing
1687 -- context as the proper body may introduce a set of its own.
1689 Opts := Save_Config_Switches;
1691 -- Indicate that the body of the package exists. If we are doing
1692 -- only semantic analysis, the stub stands for the body. If we are
1693 -- generating code, the existence of the body will be confirmed
1694 -- when we load the proper body.
1696 Set_Scope (Id, Current_Scope);
1697 Mutate_Ekind (Id, E_Package_Body);
1698 Set_Etype (Id, Standard_Void_Type);
1700 Analyze_Aspect_Specifications (N, Id);
1702 Set_Has_Completion (Nam);
1703 Set_Corresponding_Spec_Of_Stub (N, Nam);
1704 Generate_Reference (Nam, Id, 'b');
1705 Analyze_Proper_Body (N, Nam);
1707 Restore_Config_Switches (Opts);
1708 end if;
1709 end Analyze_Package_Body_Stub;
1711 -------------------------
1712 -- Analyze_Proper_Body --
1713 -------------------------
1715 procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
1716 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
1718 procedure Optional_Subunit;
1719 -- This procedure is called when the main unit is a stub, or when we
1720 -- are not generating code. In such a case, we analyze the subunit if
1721 -- present, which is user-friendly, but we don't complain if the subunit
1722 -- is missing. In GNATprove_Mode, we issue an error to avoid formal
1723 -- verification of a partial unit.
1725 ----------------------
1726 -- Optional_Subunit --
1727 ----------------------
1729 procedure Optional_Subunit is
1730 Comp_Unit : Node_Id;
1731 Unum : Unit_Number_Type;
1733 begin
1734 -- Try to load subunit, but ignore any errors that occur during the
1735 -- loading of the subunit, by using the special feature in Errout to
1736 -- ignore all errors. Note that Fatal_Error will still be set, so we
1737 -- will be able to check for this case below.
1739 if not GNATprove_Mode then
1740 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
1741 end if;
1743 Unum :=
1744 Load_Unit
1745 (Load_Name => Subunit_Name,
1746 Required => GNATprove_Mode,
1747 Subunit => True,
1748 Error_Node => N);
1750 if not GNATprove_Mode then
1751 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
1752 end if;
1754 -- All done if we successfully loaded the subunit
1756 if Unum /= No_Unit
1757 and then (Fatal_Error (Unum) /= Error_Detected
1758 or else Try_Semantics)
1759 then
1760 Comp_Unit := Cunit (Unum);
1762 -- If the file was empty or seriously mangled, the unit itself may
1763 -- be missing.
1765 if No (Unit (Comp_Unit)) then
1766 Error_Msg_N
1767 ("subunit does not contain expected proper body", N);
1769 elsif Nkind (Unit (Comp_Unit)) /= N_Subunit then
1770 Error_Msg_N
1771 ("expected SEPARATE subunit, found child unit",
1772 Cunit_Entity (Unum));
1773 else
1774 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1775 Analyze_Subunit (Comp_Unit);
1776 Set_Library_Unit (N, Comp_Unit);
1777 Set_Corresponding_Body (N, Defining_Entity (Unit (Comp_Unit)));
1778 end if;
1780 elsif Unum = No_Unit
1781 and then Present (Nam)
1782 then
1783 if Is_Protected_Type (Nam) then
1784 Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
1785 else
1786 Set_Corresponding_Body (
1787 Unit_Declaration_Node (Nam), Defining_Identifier (N));
1788 end if;
1789 end if;
1790 end Optional_Subunit;
1792 -- Local variables
1794 Comp_Unit : Node_Id;
1795 Unum : Unit_Number_Type;
1797 -- Start of processing for Analyze_Proper_Body
1799 begin
1800 -- If the subunit is already loaded, it means that the main unit is a
1801 -- subunit, and that the current unit is one of its parents which was
1802 -- being analyzed to provide the needed context for the analysis of the
1803 -- subunit. In this case we analyze the subunit and continue with the
1804 -- parent, without looking at subsequent subunits.
1806 if Is_Loaded (Subunit_Name) then
1808 -- If the proper body is already linked to the stub node, the stub is
1809 -- in a generic unit and just needs analyzing.
1811 if Present (Library_Unit (N)) then
1812 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1814 -- If the subunit has severe errors, the spec of the enclosing
1815 -- body may not be available, in which case do not try analysis.
1817 if Serious_Errors_Detected > 0
1818 and then No (Library_Unit (Library_Unit (N)))
1819 then
1820 return;
1821 end if;
1823 -- Collect SCO information for loaded subunit if we are in the
1824 -- extended main unit.
1826 if Generate_SCO
1827 and then In_Extended_Main_Source_Unit
1828 (Cunit_Entity (Current_Sem_Unit))
1829 then
1830 SCO_Record_Raw (Get_Cunit_Unit_Number (Library_Unit (N)));
1831 end if;
1833 Analyze_Subunit (Library_Unit (N));
1835 -- Otherwise we must load the subunit and link to it
1837 else
1838 -- Load the subunit, this must work, since we originally loaded
1839 -- the subunit earlier on. So this will not really load it, just
1840 -- give access to it.
1842 Unum :=
1843 Load_Unit
1844 (Load_Name => Subunit_Name,
1845 Required => True,
1846 Subunit => False,
1847 Error_Node => N);
1849 -- And analyze the subunit in the parent context (note that we
1850 -- do not call Semantics, since that would remove the parent
1851 -- context). Because of this, we have to manually reset the
1852 -- compiler state to Analyzing since it got destroyed by Load.
1854 if Unum /= No_Unit then
1855 Compiler_State := Analyzing;
1857 -- Check that the proper body is a subunit and not a child
1858 -- unit. If the unit was previously loaded, the error will
1859 -- have been emitted when copying the generic node, so we
1860 -- just return to avoid cascaded errors.
1862 if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1863 return;
1864 end if;
1866 Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1867 Analyze_Subunit (Cunit (Unum));
1868 Set_Library_Unit (N, Cunit (Unum));
1869 end if;
1870 end if;
1872 -- If the main unit is a subunit, then we are just performing semantic
1873 -- analysis on that subunit, and any other subunits of any parent unit
1874 -- should be ignored. If the main unit is itself a subunit, another
1875 -- subunit is irrelevant unless it is a subunit of the current one, that
1876 -- is to say appears in the current source tree.
1878 elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1879 and then Subunit_Name /= Unit_Name (Main_Unit)
1880 then
1881 -- But before we return, set the flag for unloaded subunits. This
1882 -- will suppress junk warnings of variables in the same declarative
1883 -- part (or a higher level one) that are in danger of looking unused
1884 -- when in fact there might be a declaration in the subunit that we
1885 -- do not intend to load.
1887 Unloaded_Subunits := True;
1888 return;
1890 -- If the subunit is not already loaded, and we are generating code,
1891 -- then this is the case where compilation started from the parent, and
1892 -- we are generating code for an entire subunit tree. In that case we
1893 -- definitely need to load the subunit.
1895 -- In order to continue the analysis with the rest of the parent,
1896 -- and other subunits, we load the unit without requiring its
1897 -- presence, and emit a warning if not found, rather than terminating
1898 -- the compilation abruptly, as for other missing file problems.
1900 elsif Original_Operating_Mode = Generate_Code then
1902 -- If the proper body is already linked to the stub node, the stub is
1903 -- in a generic unit and just needs analyzing.
1905 -- We update the version. Although we are not strictly technically
1906 -- semantically dependent on the subunit, given our approach of macro
1907 -- substitution of subunits, it makes sense to include it in the
1908 -- version identification.
1910 if Present (Library_Unit (N)) then
1911 Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1912 Analyze_Subunit (Library_Unit (N));
1913 Version_Update (Cunit (Main_Unit), Library_Unit (N));
1915 -- Otherwise we must load the subunit and link to it
1917 else
1918 -- Make sure that, if the subunit is preprocessed and -gnateG is
1919 -- specified, the preprocessed file will be written.
1921 Lib.Analysing_Subunit_Of_Main := True;
1922 Unum :=
1923 Load_Unit
1924 (Load_Name => Subunit_Name,
1925 Required => False,
1926 Subunit => True,
1927 Error_Node => N);
1928 Lib.Analysing_Subunit_Of_Main := False;
1930 -- Give message if we did not get the unit Emit warning even if
1931 -- missing subunit is not within main unit, to simplify debugging.
1933 pragma Assert (Original_Operating_Mode = Generate_Code);
1934 if Unum = No_Unit then
1935 Error_Msg_Unit_1 := Subunit_Name;
1936 Error_Msg_File_1 :=
1937 Get_File_Name (Subunit_Name, Subunit => True);
1938 Error_Msg_N
1939 ("subunit$$ in file{ not found??!!", N);
1940 Subunits_Missing := True;
1941 end if;
1943 -- Load_Unit may reset Compiler_State, since it may have been
1944 -- necessary to parse an additional units, so we make sure that
1945 -- we reset it to the Analyzing state.
1947 Compiler_State := Analyzing;
1949 if Unum /= No_Unit then
1950 if Debug_Flag_L then
1951 Write_Str ("*** Loaded subunit from stub. Analyze");
1952 Write_Eol;
1953 end if;
1955 Comp_Unit := Cunit (Unum);
1957 -- Check for child unit instead of subunit
1959 if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1960 Error_Msg_N
1961 ("expected SEPARATE subunit, found child unit",
1962 Cunit_Entity (Unum));
1964 -- OK, we have a subunit
1966 else
1967 Set_Corresponding_Stub (Unit (Comp_Unit), N);
1968 Set_Library_Unit (N, Comp_Unit);
1970 -- We update the version. Although we are not technically
1971 -- semantically dependent on the subunit, given our approach
1972 -- of macro substitution of subunits, it makes sense to
1973 -- include it in the version identification.
1975 Version_Update (Cunit (Main_Unit), Comp_Unit);
1977 -- Collect SCO information for loaded subunit if we are in
1978 -- the extended main unit.
1980 if Generate_SCO
1981 and then In_Extended_Main_Source_Unit
1982 (Cunit_Entity (Current_Sem_Unit))
1983 then
1984 SCO_Record_Raw (Unum);
1985 end if;
1987 -- Analyze the unit if semantics active
1989 if Fatal_Error (Unum) /= Error_Detected
1990 or else Try_Semantics
1991 then
1992 Analyze_Subunit (Comp_Unit);
1993 end if;
1994 end if;
1995 end if;
1996 end if;
1998 -- The remaining case is when the subunit is not already loaded and we
1999 -- are not generating code. In this case we are just performing semantic
2000 -- analysis on the parent, and we are not interested in the subunit. For
2001 -- subprograms, analyze the stub as a body. For other entities the stub
2002 -- has already been marked as completed.
2004 else
2005 Optional_Subunit;
2006 end if;
2007 end Analyze_Proper_Body;
2009 ----------------------------------
2010 -- Analyze_Protected_Body_Stub --
2011 ----------------------------------
2013 procedure Analyze_Protected_Body_Stub (N : Node_Id) is
2014 Id : constant Entity_Id := Defining_Entity (N);
2015 Nam : Entity_Id := Current_Entity_In_Scope (Id);
2016 Opts : Config_Switches_Type;
2018 begin
2019 Check_Stub_Level (N);
2021 -- First occurrence of name may have been as an incomplete type
2023 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2024 Nam := Full_View (Nam);
2025 end if;
2027 if No (Nam) or else not Is_Protected_Type (Etype (Nam)) then
2028 Error_Msg_N ("missing specification for Protected body", N);
2030 else
2031 -- Retain and restore the configuration options of the enclosing
2032 -- context as the proper body may introduce a set of its own.
2034 Opts := Save_Config_Switches;
2036 Set_Scope (Id, Current_Scope);
2037 Mutate_Ekind (Id, E_Protected_Body);
2038 Set_Etype (Id, Standard_Void_Type);
2040 Analyze_Aspect_Specifications (N, Id);
2042 Set_Has_Completion (Etype (Nam));
2043 Set_Corresponding_Spec_Of_Stub (N, Nam);
2044 Generate_Reference (Nam, Id, 'b');
2045 Analyze_Proper_Body (N, Etype (Nam));
2047 Restore_Config_Switches (Opts);
2048 end if;
2049 end Analyze_Protected_Body_Stub;
2051 -----------------------------------------
2052 -- Analyze_Required_Limited_With_Units --
2053 -----------------------------------------
2055 procedure Analyze_Required_Limited_With_Units (N : Node_Id) is
2056 Unit_Node : constant Node_Id := Unit (N);
2057 Spec_Id : constant Entity_Id := Defining_Entity (Unit_Node);
2059 function Depends_On_Limited_Views (Pkg_Id : Entity_Id) return Boolean;
2060 -- Determines whether the given package has some subprogram with a
2061 -- profile that depends on shadow incomplete type entities of a
2062 -- limited-with unit.
2064 function Has_Limited_With_Clauses return Boolean;
2065 -- Determines whether the compilation unit N has limited-with context
2066 -- clauses.
2068 ------------------------------
2069 -- Has_Limited_With_Clauses --
2070 ------------------------------
2072 function Has_Limited_With_Clauses return Boolean is
2073 Item : Node_Id := First (Context_Items (N));
2075 begin
2076 while Present (Item) loop
2077 if Nkind (Item) = N_With_Clause
2078 and then Limited_Present (Item)
2079 and then not Implicit_With (Item)
2080 then
2081 return True;
2082 end if;
2084 Next (Item);
2085 end loop;
2087 return False;
2088 end Has_Limited_With_Clauses;
2090 ------------------------------
2091 -- Depends_On_Limited_Views --
2092 ------------------------------
2094 function Depends_On_Limited_Views (Pkg_Id : Entity_Id) return Boolean is
2096 function Has_Limited_View_Types (Subp : Entity_Id) return Boolean;
2097 -- Determines whether the type of some formal of Subp, or its return
2098 -- type, is a shadow incomplete entity of a limited-with unit.
2100 ----------------------------
2101 -- Has_Limited_View_Types --
2102 ----------------------------
2104 function Has_Limited_View_Types (Subp : Entity_Id) return Boolean is
2105 Formal : Entity_Id := First_Formal (Subp);
2107 begin
2108 while Present (Formal) loop
2109 if From_Limited_With (Etype (Formal))
2110 and then Has_Non_Limited_View (Etype (Formal))
2111 and then Ekind (Non_Limited_View (Etype (Formal)))
2112 = E_Incomplete_Type
2113 then
2114 return True;
2115 end if;
2117 Formal := Next_Formal (Formal);
2118 end loop;
2120 if Ekind (Subp) = E_Function
2121 and then From_Limited_With (Etype (Subp))
2122 and then Has_Non_Limited_View (Etype (Subp))
2123 and then Ekind (Non_Limited_View (Etype (Subp)))
2124 = E_Incomplete_Type
2125 then
2126 return True;
2127 end if;
2129 return False;
2130 end Has_Limited_View_Types;
2132 -- Local variables
2134 E : Entity_Id := First_Entity (Pkg_Id);
2136 begin
2137 while Present (E) loop
2138 if Is_Subprogram (E)
2139 and then Has_Limited_View_Types (E)
2140 then
2141 return True;
2143 -- Recursion on nested packages skipping package renamings
2145 elsif Ekind (E) = E_Package
2146 and then No (Renamed_Entity (E))
2147 and then Depends_On_Limited_Views (E)
2148 then
2149 return True;
2150 end if;
2152 Next_Entity (E);
2153 end loop;
2155 return False;
2156 end Depends_On_Limited_Views;
2158 -- Local variables
2160 Item : Node_Id;
2162 -- Start of processing for Analyze_Required_Limited_With_Units
2164 begin
2165 -- Cases where no action is required
2167 if not Expander_Active
2168 or else Nkind (Unit_Node) /= N_Package_Declaration
2169 or else Main_Unit_Entity /= Spec_Id
2170 or else Is_Generic_Unit (Spec_Id)
2171 or else Unit_Requires_Body (Spec_Id)
2172 or else not Has_Limited_With_Clauses
2173 or else not Depends_On_Limited_Views (Spec_Id)
2174 then
2175 return;
2176 end if;
2178 -- Perform full analyis of limited-with units to provide the backend
2179 -- with the full-view of shadow entities.
2181 Item := First (Context_Items (N));
2182 while Present (Item) loop
2183 if Nkind (Item) = N_With_Clause
2184 and then Limited_Present (Item)
2185 and then not Implicit_With (Item)
2186 then
2187 Semantics (Library_Unit (Item));
2188 end if;
2190 Next (Item);
2191 end loop;
2192 end Analyze_Required_Limited_With_Units;
2194 ----------------------------------
2195 -- Analyze_Subprogram_Body_Stub --
2196 ----------------------------------
2198 -- A subprogram body stub can appear with or without a previous spec. If
2199 -- there is one, then the analysis of the body will find it and verify
2200 -- conformance. The formals appearing in the specification of the stub play
2201 -- no role, except for requiring an additional conformance check. If there
2202 -- is no previous subprogram declaration, the stub acts as a spec, and
2203 -- provides the defining entity for the subprogram.
2205 procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
2206 Decl : Node_Id;
2207 Opts : Config_Switches_Type;
2209 begin
2210 Check_Stub_Level (N);
2212 -- Verify that the identifier for the stub is unique within this
2213 -- declarative part.
2215 if Nkind (Parent (N)) in
2216 N_Block_Statement | N_Package_Body | N_Subprogram_Body
2217 then
2218 Decl := First (Declarations (Parent (N)));
2219 while Present (Decl) and then Decl /= N loop
2220 if Nkind (Decl) = N_Subprogram_Body_Stub
2221 and then Chars (Defining_Unit_Name (Specification (Decl))) =
2222 Chars (Defining_Unit_Name (Specification (N)))
2223 then
2224 Error_Msg_N ("identifier for stub is not unique", N);
2225 end if;
2227 Next (Decl);
2228 end loop;
2229 end if;
2231 -- Retain and restore the configuration options of the enclosing context
2232 -- as the proper body may introduce a set of its own.
2234 Opts := Save_Config_Switches;
2236 -- Treat stub as a body, which checks conformance if there is a previous
2237 -- declaration, or else introduces entity and its signature.
2239 Analyze_Subprogram_Body (N);
2240 Analyze_Proper_Body (N, Empty);
2242 Restore_Config_Switches (Opts);
2243 end Analyze_Subprogram_Body_Stub;
2245 ---------------------
2246 -- Analyze_Subunit --
2247 ---------------------
2249 -- A subunit is compiled either by itself (for semantic checking) or as
2250 -- part of compiling the parent (for code generation). In either case, by
2251 -- the time we actually process the subunit, the parent has already been
2252 -- installed and analyzed. The node N is a compilation unit, whose context
2253 -- needs to be treated here, because we come directly here from the parent
2254 -- without calling Analyze_Compilation_Unit.
2256 -- The compilation context includes the explicit context of the subunit,
2257 -- and the context of the parent, together with the parent itself. In order
2258 -- to compile the current context, we remove the one inherited from the
2259 -- parent, in order to have a clean visibility table. We restore the parent
2260 -- context before analyzing the proper body itself. On exit, we remove only
2261 -- the explicit context of the subunit.
2263 -- WARNING: This routine manages SPARK regions. Return statements must be
2264 -- replaced by gotos which jump to the end of the routine and restore the
2265 -- SPARK mode.
2267 procedure Analyze_Subunit (N : Node_Id) is
2268 Lib_Unit : constant Node_Id := Library_Unit (N);
2269 Par_Unit : constant Entity_Id := Current_Scope;
2271 Lib_Spec : Node_Id := Library_Unit (Lib_Unit);
2272 Num_Scopes : Nat := 0;
2273 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2274 Enclosing_Child : Entity_Id := Empty;
2275 Svg : constant Suppress_Record := Scope_Suppress;
2277 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
2278 Cunit_Boolean_Restrictions_Save;
2279 -- Save non-partition wide restrictions before processing the subunit.
2280 -- All subunits are analyzed with config restrictions reset and we need
2281 -- to restore these saved values at the end.
2283 procedure Analyze_Subunit_Context;
2284 -- Capture names in use clauses of the subunit. This must be done before
2285 -- re-installing parent declarations, because items in the context must
2286 -- not be hidden by declarations local to the parent.
2288 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
2289 -- Recursive procedure to restore scope of all ancestors of subunit,
2290 -- from outermost in. If parent is not a subunit, the call to install
2291 -- context installs context of spec and (if parent is a child unit) the
2292 -- context of its parents as well. It is confusing that parents should
2293 -- be treated differently in both cases, but the semantics are just not
2294 -- identical.
2296 procedure Re_Install_Use_Clauses;
2297 -- As part of the removal of the parent scope, the use clauses are
2298 -- removed, to be reinstalled when the context of the subunit has been
2299 -- analyzed. Use clauses may also have been affected by the analysis of
2300 -- the context of the subunit, so they have to be applied again, to
2301 -- insure that the compilation environment of the rest of the parent
2302 -- unit is identical.
2304 procedure Remove_Scope;
2305 -- Remove current scope from scope stack, and preserve the list of use
2306 -- clauses in it, to be reinstalled after context is analyzed.
2308 -----------------------------
2309 -- Analyze_Subunit_Context --
2310 -----------------------------
2312 procedure Analyze_Subunit_Context is
2313 Item : Node_Id;
2314 Unit_Name : Entity_Id;
2316 begin
2317 Analyze_Context (N);
2318 Check_No_Elab_Code_All (N);
2320 -- Make withed units immediately visible. If child unit, make the
2321 -- ultimate parent immediately visible.
2323 Item := First (Context_Items (N));
2324 while Present (Item) loop
2325 if Nkind (Item) = N_With_Clause then
2327 -- Protect frontend against previous errors in context clauses
2329 if Nkind (Name (Item)) /= N_Selected_Component then
2330 if Error_Posted (Item) then
2331 null;
2333 else
2334 -- If a subunits has serious syntax errors, the context
2335 -- may not have been loaded. Add a harmless unit name to
2336 -- attempt processing.
2338 if Serious_Errors_Detected > 0
2339 and then No (Entity (Name (Item)))
2340 then
2341 Set_Entity (Name (Item), Standard_Standard);
2342 end if;
2344 Unit_Name := Entity (Name (Item));
2345 loop
2346 Set_Is_Visible_Lib_Unit (Unit_Name);
2347 exit when Scope (Unit_Name) = Standard_Standard;
2348 Unit_Name := Scope (Unit_Name);
2350 if No (Unit_Name) then
2351 Check_Error_Detected;
2352 return;
2353 end if;
2354 end loop;
2356 if not Is_Immediately_Visible (Unit_Name) then
2357 Set_Is_Immediately_Visible (Unit_Name);
2358 Set_Context_Installed (Item);
2359 end if;
2360 end if;
2361 end if;
2363 elsif Nkind (Item) = N_Use_Package_Clause then
2364 Analyze (Name (Item));
2366 elsif Nkind (Item) = N_Use_Type_Clause then
2367 Analyze (Subtype_Mark (Item));
2368 end if;
2370 Next (Item);
2371 end loop;
2373 -- Reset visibility of withed units. They will be made visible again
2374 -- when we install the subunit context.
2376 Item := First (Context_Items (N));
2377 while Present (Item) loop
2378 if Nkind (Item) = N_With_Clause
2380 -- Protect frontend against previous errors in context clauses
2382 and then Nkind (Name (Item)) /= N_Selected_Component
2383 and then not Error_Posted (Item)
2384 then
2385 Unit_Name := Entity (Name (Item));
2386 loop
2387 Set_Is_Visible_Lib_Unit (Unit_Name, False);
2388 exit when Scope (Unit_Name) = Standard_Standard;
2389 Unit_Name := Scope (Unit_Name);
2390 end loop;
2392 if Context_Installed (Item) then
2393 Set_Is_Immediately_Visible (Unit_Name, False);
2394 Set_Context_Installed (Item, False);
2395 end if;
2396 end if;
2398 Next (Item);
2399 end loop;
2400 end Analyze_Subunit_Context;
2402 ------------------------
2403 -- Re_Install_Parents --
2404 ------------------------
2406 procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
2407 E : Entity_Id;
2409 begin
2410 if Nkind (Unit (L)) = N_Subunit then
2411 Re_Install_Parents (Library_Unit (L), Scope (Scop));
2412 end if;
2414 Install_Context (L, False);
2416 -- If the subunit occurs within a child unit, we must restore the
2417 -- immediate visibility of any siblings that may occur in context.
2418 -- In addition, we must reset the previous visibility of the
2419 -- parent unit which is now on the scope stack. This is because
2420 -- the Previous_Visibility was previously set when removing the
2421 -- context. This is necessary to prevent the parent entity from
2422 -- remaining visible after the subunit is compiled. This only
2423 -- has an effect if a homonym exists in a body to be processed
2424 -- later if inlining is enabled.
2426 if Present (Enclosing_Child) then
2427 Install_Siblings (Enclosing_Child, L);
2428 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
2429 False;
2430 end if;
2432 Push_Scope (Scop);
2434 if Scop /= Par_Unit then
2435 Set_Is_Immediately_Visible (Scop);
2436 end if;
2438 -- Make entities in scope visible again. For child units, restore
2439 -- visibility only if they are actually in context.
2441 E := First_Entity (Current_Scope);
2442 while Present (E) loop
2443 if not Is_Child_Unit (E) or else Is_Visible_Lib_Unit (E) then
2444 Set_Is_Immediately_Visible (E);
2445 end if;
2447 Next_Entity (E);
2448 end loop;
2450 -- A subunit appears within a body, and for a nested subunits all the
2451 -- parents are bodies. Restore full visibility of their private
2452 -- entities.
2454 if Is_Package_Or_Generic_Package (Scop) then
2455 Set_In_Package_Body (Scop);
2456 Install_Private_Declarations (Scop);
2457 end if;
2458 end Re_Install_Parents;
2460 ----------------------------
2461 -- Re_Install_Use_Clauses --
2462 ----------------------------
2464 procedure Re_Install_Use_Clauses is
2465 U : Node_Id;
2466 begin
2467 for J in reverse 1 .. Num_Scopes loop
2468 U := Use_Clauses (J);
2469 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
2470 Install_Use_Clauses (U);
2471 end loop;
2472 end Re_Install_Use_Clauses;
2474 ------------------
2475 -- Remove_Scope --
2476 ------------------
2478 procedure Remove_Scope is
2479 E : Entity_Id;
2481 begin
2482 Num_Scopes := Num_Scopes + 1;
2483 Use_Clauses (Num_Scopes) :=
2484 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
2486 E := First_Entity (Current_Scope);
2487 while Present (E) loop
2488 Set_Is_Immediately_Visible (E, False);
2489 Next_Entity (E);
2490 end loop;
2492 if Is_Child_Unit (Current_Scope) then
2493 Enclosing_Child := Current_Scope;
2494 end if;
2496 Pop_Scope;
2497 end Remove_Scope;
2499 Saved_SM : SPARK_Mode_Type := SPARK_Mode;
2500 Saved_SMP : Node_Id := SPARK_Mode_Pragma;
2501 -- Save the SPARK mode-related data to restore on exit. Removing
2502 -- enclosing scopes and contexts to provide a clean environment for the
2503 -- context of the subunit will eliminate any previously set SPARK_Mode.
2505 -- Start of processing for Analyze_Subunit
2507 begin
2508 -- For subunit in main extended unit, we reset the configuration values
2509 -- for the non-partition-wide restrictions. For other units reset them.
2511 if In_Extended_Main_Source_Unit (N) then
2512 Restore_Config_Cunit_Boolean_Restrictions;
2513 else
2514 Reset_Cunit_Boolean_Restrictions;
2515 end if;
2517 if Style_Check then
2518 declare
2519 Nam : Node_Id := Name (Unit (N));
2521 begin
2522 if Nkind (Nam) = N_Selected_Component then
2523 Nam := Selector_Name (Nam);
2524 end if;
2526 Check_Identifier (Nam, Par_Unit);
2527 end;
2528 end if;
2530 if not Is_Empty_List (Context_Items (N)) then
2532 -- Save current use clauses
2534 Remove_Scope;
2535 Remove_Context (Lib_Unit);
2537 -- Now remove parents and their context, including enclosing subunits
2538 -- and the outer parent body which is not a subunit.
2540 if Present (Lib_Spec) then
2541 Remove_Context (Lib_Spec);
2543 while Nkind (Unit (Lib_Spec)) = N_Subunit loop
2544 Lib_Spec := Library_Unit (Lib_Spec);
2545 Remove_Scope;
2546 Remove_Context (Lib_Spec);
2547 end loop;
2549 if Nkind (Unit (Lib_Unit)) = N_Subunit then
2550 Remove_Scope;
2551 end if;
2553 if Nkind (Unit (Lib_Spec)) in N_Package_Body | N_Subprogram_Body
2554 then
2555 Remove_Context (Library_Unit (Lib_Spec));
2556 end if;
2557 end if;
2559 Set_Is_Immediately_Visible (Par_Unit, False);
2561 Analyze_Subunit_Context;
2563 -- Take into account the effect of any SPARK_Mode configuration
2564 -- pragma, which takes precedence over a different value of
2565 -- SPARK_Mode inherited from the context of the stub.
2567 if SPARK_Mode /= None then
2568 Saved_SM := SPARK_Mode;
2569 Saved_SMP := SPARK_Mode_Pragma;
2570 end if;
2572 Re_Install_Parents (Lib_Unit, Par_Unit);
2573 Set_Is_Immediately_Visible (Par_Unit);
2575 -- If the context includes a child unit of the parent of the subunit,
2576 -- the parent will have been removed from visibility, after compiling
2577 -- that cousin in the context. The visibility of the parent must be
2578 -- restored now. This also applies if the context includes another
2579 -- subunit of the same parent which in turn includes a child unit in
2580 -- its context.
2582 if Is_Package_Or_Generic_Package (Par_Unit) then
2583 if not Is_Immediately_Visible (Par_Unit)
2584 or else (Present (First_Entity (Par_Unit))
2585 and then not
2586 Is_Immediately_Visible (First_Entity (Par_Unit)))
2587 then
2588 Set_Is_Immediately_Visible (Par_Unit);
2589 Install_Visible_Declarations (Par_Unit);
2590 Install_Private_Declarations (Par_Unit);
2591 end if;
2592 end if;
2594 Re_Install_Use_Clauses;
2595 Install_Context (N, Chain => False);
2597 -- Restore state of suppress flags for current body
2599 Scope_Suppress := Svg;
2601 -- If the subunit is within a child unit, then siblings of any parent
2602 -- unit that appear in the context clause of the subunit must also be
2603 -- made immediately visible.
2605 if Present (Enclosing_Child) then
2606 Install_Siblings (Enclosing_Child, N);
2607 end if;
2608 end if;
2610 Generate_Parent_References (Unit (N), Par_Unit);
2612 -- Reinstall the SPARK_Mode which was in effect prior to any scope and
2613 -- context manipulations, taking into account a possible SPARK_Mode
2614 -- configuration pragma if present.
2616 Install_SPARK_Mode (Saved_SM, Saved_SMP);
2618 -- If the subunit is part of a compilation unit which is subject to
2619 -- pragma Elaboration_Checks, set the model specified by the pragma
2620 -- because it applies to all parts of the unit.
2622 Install_Elaboration_Model (Par_Unit);
2624 -- The syntax rules require a proper body for a subprogram subunit
2626 if Nkind (Proper_Body (Sinfo.Nodes.Unit (N))) = N_Subprogram_Declaration
2627 then
2628 if Null_Present (Specification (Proper_Body (Sinfo.Nodes.Unit (N))))
2629 then
2630 Error_Msg_N
2631 ("null procedure not allowed as subunit",
2632 Proper_Body (Unit (N)));
2633 else
2634 Error_Msg_N
2635 ("subprogram declaration not allowed as subunit",
2636 Defining_Unit_Name (Specification (Proper_Body (Unit (N)))));
2637 end if;
2638 end if;
2640 Analyze (Proper_Body (Unit (N)));
2641 Remove_Context (N);
2643 -- The subunit may contain a with_clause on a sibling of some ancestor.
2644 -- Removing the context will remove from visibility those ancestor child
2645 -- units, which must be restored to the visibility they have in the
2646 -- enclosing body.
2648 if Present (Enclosing_Child) then
2649 declare
2650 C : Entity_Id;
2651 begin
2652 C := Current_Scope;
2653 while Present (C) and then C /= Standard_Standard loop
2654 Set_Is_Immediately_Visible (C);
2655 Set_Is_Visible_Lib_Unit (C);
2656 C := Scope (C);
2657 end loop;
2658 end;
2659 end if;
2661 -- Deal with restore of restrictions
2663 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
2664 end Analyze_Subunit;
2666 ----------------------------
2667 -- Analyze_Task_Body_Stub --
2668 ----------------------------
2670 procedure Analyze_Task_Body_Stub (N : Node_Id) is
2671 Id : constant Entity_Id := Defining_Entity (N);
2672 Loc : constant Source_Ptr := Sloc (N);
2673 Nam : Entity_Id := Current_Entity_In_Scope (Id);
2675 begin
2676 Check_Stub_Level (N);
2678 -- First occurrence of name may have been as an incomplete type
2680 if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
2681 Nam := Full_View (Nam);
2682 end if;
2684 if No (Nam) or else not Is_Task_Type (Etype (Nam)) then
2685 Error_Msg_N ("missing specification for task body", N);
2687 else
2688 Set_Scope (Id, Current_Scope);
2689 Mutate_Ekind (Id, E_Task_Body);
2690 Set_Etype (Id, Standard_Void_Type);
2692 Analyze_Aspect_Specifications (N, Id);
2694 Generate_Reference (Nam, Id, 'b');
2695 Set_Corresponding_Spec_Of_Stub (N, Nam);
2697 -- Check for duplicate stub, if so give message and terminate
2699 if Has_Completion (Etype (Nam)) then
2700 Error_Msg_N ("duplicate stub for task", N);
2701 return;
2702 else
2703 Set_Has_Completion (Etype (Nam));
2704 end if;
2706 Analyze_Proper_Body (N, Etype (Nam));
2708 -- Set elaboration flag to indicate that entity is callable. This
2709 -- cannot be done in the expansion of the body itself, because the
2710 -- proper body is not in a declarative part. This is only done if
2711 -- expansion is active, because the context may be generic and the
2712 -- flag not defined yet.
2714 if Expander_Active then
2715 Insert_After (N,
2716 Make_Assignment_Statement (Loc,
2717 Name =>
2718 Make_Identifier (Loc,
2719 Chars => New_External_Name (Chars (Etype (Nam)), 'E')),
2720 Expression => New_Occurrence_Of (Standard_True, Loc)));
2721 end if;
2722 end if;
2723 end Analyze_Task_Body_Stub;
2725 -------------------------
2726 -- Analyze_With_Clause --
2727 -------------------------
2729 -- Analyze the declaration of a unit in a with clause. At end, label the
2730 -- with clause with the defining entity for the unit.
2732 procedure Analyze_With_Clause (N : Node_Id) is
2734 -- Retrieve the original kind of the unit node, before analysis. If it
2735 -- is a subprogram instantiation, its analysis below will rewrite the
2736 -- node as the declaration of the wrapper package. If the same
2737 -- instantiation appears indirectly elsewhere in the context, it will
2738 -- have been analyzed already.
2740 Unit_Kind : constant Node_Kind :=
2741 Nkind (Original_Node (Unit (Library_Unit (N))));
2742 Nam : constant Node_Id := Name (N);
2743 E_Name : Entity_Id;
2744 Par_Name : Entity_Id;
2745 Pref : Node_Id;
2746 U : Node_Id;
2748 Intunit : Boolean;
2749 -- Set True if the unit currently being compiled is an internal unit
2751 Restriction_Violation : Boolean := False;
2752 -- Set True if a with violates a restriction, no point in giving any
2753 -- warnings if we have this definite error.
2755 Save_Style_Check : constant Boolean := Opt.Style_Check;
2757 begin
2758 U := Unit (Library_Unit (N));
2760 -- If this is an internal unit which is a renaming, then this is a
2761 -- violation of No_Obsolescent_Features.
2763 -- Note: this is not quite right if the user defines one of these units
2764 -- himself, but that's a marginal case, and fixing it is hard ???
2766 if Ada_Version >= Ada_95
2767 and then In_Predefined_Renaming (U)
2768 then
2769 if Restriction_Check_Required (No_Obsolescent_Features) then
2770 Check_Restriction (No_Obsolescent_Features, N);
2771 Restriction_Violation := True;
2772 end if;
2774 if Warn_On_Obsolescent_Feature then
2775 Error_Msg_N
2776 ("renamed predefined unit is an obsolescent feature "
2777 & "(RM J.1)?j?", N);
2778 end if;
2779 end if;
2781 -- Check No_Implementation_Units violation
2783 if Restriction_Check_Required (No_Implementation_Units) then
2784 if Not_Impl_Defined_Unit (Get_Source_Unit (U)) then
2785 null;
2786 else
2787 Check_Restriction (No_Implementation_Units, Nam);
2788 Restriction_Violation := True;
2789 end if;
2790 end if;
2792 -- Several actions are skipped for dummy packages (those supplied for
2793 -- with's where no matching file could be found). Such packages are
2794 -- identified by the Sloc value being set to No_Location.
2796 if Limited_Present (N) then
2798 -- Ada 2005 (AI-50217): Build visibility structures but do not
2799 -- analyze the unit.
2801 -- If the designated unit is a predefined unit, which might be used
2802 -- implicitly through the rtsfind machinery, a limited with clause
2803 -- on such a unit is usually pointless, because run-time units are
2804 -- unlikely to appear in mutually dependent units, and because this
2805 -- disables the rtsfind mechanism. We transform such limited with
2806 -- clauses into regular with clauses.
2808 if Sloc (U) /= No_Location then
2809 if In_Predefined_Unit (U) then
2810 Set_Limited_Present (N, False);
2811 Analyze_With_Clause (N);
2812 else
2813 Build_Limited_Views (N);
2814 end if;
2815 end if;
2817 return;
2818 end if;
2820 -- If we are compiling under "don't quit" mode (-gnatq) and we have
2821 -- already detected serious errors then we mark the with-clause nodes as
2822 -- analyzed before the corresponding compilation unit is analyzed. This
2823 -- is done here to protect the frontend against never ending recursion
2824 -- caused by circularities in the sources (because the previous errors
2825 -- may break the regular machine of the compiler implemented in
2826 -- Load_Unit to detect circularities).
2828 if Serious_Errors_Detected > 0 and then Try_Semantics then
2829 Set_Analyzed (N);
2830 end if;
2832 Semantics (Library_Unit (N));
2834 Intunit := Is_Internal_Unit (Current_Sem_Unit);
2836 if Sloc (U) /= No_Location then
2838 -- Check restrictions, except that we skip the check if this is an
2839 -- internal unit unless we are compiling the internal unit as the
2840 -- main unit. We also skip this for dummy packages.
2842 Check_Restriction_No_Dependence (Nam, N);
2844 if not Intunit or else Current_Sem_Unit = Main_Unit then
2845 Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
2846 end if;
2848 -- Deal with special case of GNAT.Current_Exceptions which interacts
2849 -- with the optimization of local raise statements into gotos.
2851 if Nkind (Nam) = N_Selected_Component
2852 and then Nkind (Prefix (Nam)) = N_Identifier
2853 and then Chars (Prefix (Nam)) = Name_Gnat
2854 and then Chars (Selector_Name (Nam))
2855 in Name_Most_Recent_Exception | Name_Exception_Traces
2856 then
2857 Check_Restriction (No_Exception_Propagation, N);
2858 Special_Exception_Package_Used := True;
2859 end if;
2861 -- Check for inappropriate with of internal implementation unit if we
2862 -- are not compiling an internal unit and also check for withing unit
2863 -- in wrong version of Ada. Do not issue these messages for implicit
2864 -- with's generated by the compiler itself.
2866 if Implementation_Unit_Warnings
2867 and then not Intunit
2868 and then not Implicit_With (N)
2869 and then not Restriction_Violation
2870 then
2871 case Get_Kind_Of_Unit (Get_Source_Unit (U)) is
2872 when Implementation_Unit =>
2873 Error_Msg_F ("& is an internal 'G'N'A'T unit?i?", Name (N));
2875 -- Add alternative name if available, otherwise issue a
2876 -- general warning message.
2878 if Error_Msg_Strlen /= 0 then
2879 Error_Msg_F ("\use ""~"" instead?i?", Name (N));
2880 else
2881 Error_Msg_F
2882 ("\use of this unit is non-portable and "
2883 & "version-dependent?i?", Name (N));
2884 end if;
2886 when Not_Predefined_Unit | Ada_95_Unit =>
2887 null; -- no checks needed
2889 when Ada_2005_Unit =>
2890 if Ada_Version < Ada_2005
2891 and then Warn_On_Ada_2005_Compatibility
2892 then
2893 Error_Msg_N ("& is an Ada 2005 unit?i?", Name (N));
2894 end if;
2896 when Ada_2012_Unit =>
2897 if Ada_Version < Ada_2012
2898 and then Warn_On_Ada_2012_Compatibility
2899 then
2900 Error_Msg_N ("& is an Ada 2012 unit?i?", Name (N));
2901 end if;
2903 when Ada_2022_Unit =>
2904 if Ada_Version < Ada_2022
2905 and then Warn_On_Ada_2022_Compatibility
2906 then
2907 Error_Msg_N ("& is an Ada 2022 unit?i?", Name (N));
2908 end if;
2909 end case;
2910 end if;
2911 end if;
2913 -- Semantic analysis of a generic unit is performed on a copy of
2914 -- the original tree. Retrieve the entity on which semantic info
2915 -- actually appears.
2917 if Unit_Kind in N_Generic_Declaration then
2918 E_Name := Defining_Entity (U);
2920 -- Note: in the following test, Unit_Kind is the original Nkind, but in
2921 -- the case of an instantiation, semantic analysis above will have
2922 -- replaced the unit by its instantiated version. If the instance body
2923 -- has been generated, the instance now denotes the body entity. For
2924 -- visibility purposes we need the entity of its spec.
2926 elsif (Unit_Kind = N_Package_Instantiation
2927 or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
2928 N_Package_Instantiation)
2929 and then Nkind (U) = N_Package_Body
2930 then
2931 E_Name := Corresponding_Spec (U);
2933 elsif Unit_Kind = N_Package_Instantiation
2934 and then Nkind (U) = N_Package_Instantiation
2935 and then Present (Instance_Spec (U))
2936 then
2937 -- If the instance has not been rewritten as a package declaration,
2938 -- then it appeared already in a previous with clause. Retrieve
2939 -- the entity from the previous instance.
2941 E_Name := Defining_Entity (Specification (Instance_Spec (U)));
2943 elsif Unit_Kind in N_Subprogram_Instantiation then
2945 -- The visible subprogram is created during instantiation, and is
2946 -- an attribute of the wrapper package. We retrieve the wrapper
2947 -- package directly from the instantiation node. If the instance
2948 -- is inlined the unit is still an instantiation. Otherwise it has
2949 -- been rewritten as the declaration of the wrapper itself.
2951 if Nkind (U) in N_Subprogram_Instantiation then
2952 E_Name :=
2953 Related_Instance
2954 (Defining_Entity (Specification (Instance_Spec (U))));
2955 else
2956 E_Name := Related_Instance (Defining_Entity (U));
2957 end if;
2959 elsif Unit_Kind = N_Package_Renaming_Declaration
2960 or else Unit_Kind in N_Generic_Renaming_Declaration
2961 then
2962 E_Name := Defining_Entity (U);
2964 elsif Unit_Kind = N_Subprogram_Body
2965 and then Nkind (Name (N)) = N_Selected_Component
2966 and then not Acts_As_Spec (Library_Unit (N))
2967 then
2968 -- For a child unit that has no spec, one has been created and
2969 -- analyzed. The entity required is that of the spec.
2971 E_Name := Corresponding_Spec (U);
2973 else
2974 E_Name := Defining_Entity (U);
2975 end if;
2977 if Nkind (Name (N)) = N_Selected_Component then
2979 -- Child unit in a with clause
2981 Change_Selected_Component_To_Expanded_Name (Name (N));
2983 -- If this is a child unit without a spec, and it has been analyzed
2984 -- already, a declaration has been created for it. The with_clause
2985 -- must reflect the actual body, and not the generated declaration,
2986 -- to prevent spurious binding errors involving an out-of-date spec.
2987 -- Note that this can only happen if the unit includes more than one
2988 -- with_clause for the child unit (e.g. in separate subunits).
2990 if Unit_Kind = N_Subprogram_Declaration
2991 and then Analyzed (Library_Unit (N))
2992 and then not Comes_From_Source (Library_Unit (N))
2993 then
2994 Set_Library_Unit (N,
2995 Cunit (Get_Source_Unit (Corresponding_Body (U))));
2996 end if;
2997 end if;
2999 -- Restore style checks
3001 Style_Check := Save_Style_Check;
3003 -- Record the reference, but do NOT set the unit as referenced, we want
3004 -- to consider the unit as unreferenced if this is the only reference
3005 -- that occurs.
3007 Set_Entity_With_Checks (Name (N), E_Name);
3008 Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
3010 -- Generate references and check No_Dependence restriction for parents
3012 if Is_Child_Unit (E_Name) then
3013 Pref := Prefix (Name (N));
3014 Par_Name := Scope (E_Name);
3015 while Nkind (Pref) = N_Selected_Component loop
3016 Change_Selected_Component_To_Expanded_Name (Pref);
3018 if Present (Entity (Selector_Name (Pref)))
3019 and then
3020 Present (Renamed_Entity (Entity (Selector_Name (Pref))))
3021 and then Entity (Selector_Name (Pref)) /= Par_Name
3022 then
3023 -- The prefix is a child unit that denotes a renaming declaration.
3024 -- Replace the prefix directly with the renamed unit, because the
3025 -- rest of the prefix is irrelevant to the visibility of the real
3026 -- unit.
3028 Rewrite (Pref, New_Occurrence_Of (Par_Name, Sloc (Pref)));
3029 exit;
3030 end if;
3032 Set_Entity_With_Checks (Pref, Par_Name);
3034 Generate_Reference (Par_Name, Pref);
3035 Check_Restriction_No_Dependence (Pref, N);
3036 Pref := Prefix (Pref);
3038 -- If E_Name is the dummy entity for a nonexistent unit, its scope
3039 -- is set to Standard_Standard, and no attempt should be made to
3040 -- further unwind scopes.
3042 if Par_Name /= Standard_Standard then
3043 Par_Name := Scope (Par_Name);
3044 end if;
3046 -- Abandon processing in case of previous errors
3048 if No (Par_Name) then
3049 Check_Error_Detected;
3050 return;
3051 end if;
3052 end loop;
3054 if Present (Entity (Pref))
3055 and then not Analyzed (Parent (Parent (Entity (Pref))))
3056 then
3057 -- If the entity is set without its unit being compiled, the
3058 -- original parent is a renaming, and Par_Name is the renamed
3059 -- entity. For visibility purposes, we need the original entity,
3060 -- which must be analyzed now because Load_Unit directly retrieves
3061 -- the renamed unit, and the renaming declaration itself has not
3062 -- been analyzed.
3064 Analyze (Parent (Parent (Entity (Pref))));
3065 pragma Assert (Renamed_Entity (Entity (Pref)) = Par_Name);
3066 Par_Name := Entity (Pref);
3067 end if;
3069 -- Guard against missing or misspelled child units
3071 if Present (Par_Name) then
3072 Set_Entity_With_Checks (Pref, Par_Name);
3073 Generate_Reference (Par_Name, Pref);
3075 else
3076 pragma Assert (Serious_Errors_Detected /= 0);
3078 -- Mark the node to indicate that a related error has been posted.
3079 -- This defends further compilation passes against improper use of
3080 -- the invalid WITH clause node.
3082 Set_Error_Posted (N);
3083 Set_Name (N, Error);
3084 return;
3085 end if;
3086 end if;
3088 -- If the withed unit is System, and a system extension pragma is
3089 -- present, compile the extension now, rather than waiting for a
3090 -- visibility check on a specific entity.
3092 if Chars (E_Name) = Name_System
3093 and then Scope (E_Name) = Standard_Standard
3094 and then Present (System_Extend_Unit)
3095 and then Present_System_Aux (N)
3096 then
3097 -- If the extension is not present, an error will have been emitted
3099 null;
3100 end if;
3102 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
3103 -- to private_with units; they will be made visible later (just before
3104 -- the private part is analyzed)
3106 if Private_Present (N) then
3107 Set_Is_Immediately_Visible (E_Name, False);
3108 end if;
3110 -- Propagate Fatal_Error setting from with'ed unit to current unit
3112 case Fatal_Error (Get_Source_Unit (Library_Unit (N))) is
3114 -- Nothing to do if with'ed unit had no error
3116 when None =>
3117 null;
3119 -- If with'ed unit had a detected fatal error, propagate it
3121 when Error_Detected =>
3122 Set_Fatal_Error (Current_Sem_Unit, Error_Detected);
3124 -- If with'ed unit had an ignored error, then propagate it but do not
3125 -- overide an existring setting.
3127 when Error_Ignored =>
3128 if Fatal_Error (Current_Sem_Unit) = None then
3129 Set_Fatal_Error (Current_Sem_Unit, Error_Ignored);
3130 end if;
3131 end case;
3132 end Analyze_With_Clause;
3134 ------------------------------
3135 -- Check_Private_Child_Unit --
3136 ------------------------------
3138 procedure Check_Private_Child_Unit (N : Node_Id) is
3139 Lib_Unit : constant Node_Id := Unit (N);
3140 Item : Node_Id;
3141 Curr_Unit : Entity_Id;
3142 Sub_Parent : Node_Id;
3143 Priv_Child : Entity_Id;
3144 Par_Lib : Entity_Id;
3145 Par_Spec : Node_Id;
3147 begin
3148 if Nkind (Lib_Unit) in N_Package_Body | N_Subprogram_Body then
3149 Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
3150 Par_Lib := Curr_Unit;
3152 elsif Nkind (Lib_Unit) = N_Subunit then
3154 -- The parent is itself a body. The parent entity is to be found in
3155 -- the corresponding spec.
3157 Sub_Parent := Library_Unit (N);
3158 Curr_Unit := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
3160 -- If the parent itself is a subunit, Curr_Unit is the entity of the
3161 -- enclosing body, retrieve the spec entity which is the proper
3162 -- ancestor we need for the following tests.
3164 if Ekind (Curr_Unit) = E_Package_Body then
3165 Curr_Unit := Spec_Entity (Curr_Unit);
3166 end if;
3168 Par_Lib := Curr_Unit;
3170 else
3171 Curr_Unit := Defining_Entity (Lib_Unit);
3173 Par_Lib := Curr_Unit;
3174 Par_Spec := Parent_Spec (Lib_Unit);
3176 if No (Par_Spec) then
3177 Par_Lib := Empty;
3178 else
3179 Par_Lib := Defining_Entity (Unit (Par_Spec));
3180 end if;
3181 end if;
3183 -- Loop through context items
3185 Item := First (Context_Items (N));
3186 while Present (Item) loop
3188 -- Ada 2005 (AI-262): Allow private_with of a private child package
3189 -- in public siblings
3191 if Nkind (Item) = N_With_Clause
3192 and then not Implicit_With (Item)
3193 and then not Limited_Present (Item)
3194 and then Is_Private_Descendant (Entity (Name (Item)))
3195 then
3196 Priv_Child := Entity (Name (Item));
3198 declare
3199 Curr_Parent : Entity_Id := Par_Lib;
3200 Child_Parent : Entity_Id := Scope (Priv_Child);
3201 Prv_Ancestor : Entity_Id := Child_Parent;
3202 Curr_Private : Boolean := Is_Private_Library_Unit (Curr_Unit);
3204 begin
3205 -- If the child unit is a public child then locate the nearest
3206 -- private ancestor. Child_Parent will then be set to the
3207 -- parent of that ancestor.
3209 if not Is_Private_Library_Unit (Priv_Child) then
3210 while Present (Prv_Ancestor)
3211 and then not Is_Private_Library_Unit (Prv_Ancestor)
3212 loop
3213 Prv_Ancestor := Scope (Prv_Ancestor);
3214 end loop;
3216 if Present (Prv_Ancestor) then
3217 Child_Parent := Scope (Prv_Ancestor);
3218 end if;
3219 end if;
3221 while Present (Curr_Parent)
3222 and then Curr_Parent /= Standard_Standard
3223 and then Curr_Parent /= Child_Parent
3224 loop
3225 Curr_Private :=
3226 Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
3227 Curr_Parent := Scope (Curr_Parent);
3228 end loop;
3230 if No (Curr_Parent) then
3231 Curr_Parent := Standard_Standard;
3232 end if;
3234 if Curr_Parent /= Child_Parent then
3235 if Ekind (Priv_Child) = E_Generic_Package
3236 and then Chars (Priv_Child) in Text_IO_Package_Name
3237 and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
3238 and then Scope (Scope (Scope (Priv_Child))) =
3239 Standard_Standard
3240 then
3241 Error_Msg_NE
3242 ("& is a nested package, not a compilation unit",
3243 Name (Item), Priv_Child);
3245 else
3246 Error_Msg_N
3247 ("unit in with clause is private child unit!", Item);
3248 Error_Msg_NE
3249 ("\current unit must also have parent&!",
3250 Item, Child_Parent);
3251 end if;
3253 elsif Curr_Private
3254 or else Private_Present (Item)
3255 or else Nkind (Lib_Unit) in N_Package_Body | N_Subunit
3256 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3257 and then not Acts_As_Spec (Parent (Lib_Unit)))
3258 then
3259 null;
3261 else
3262 Error_Msg_NE
3263 ("current unit must also be private descendant of&",
3264 Item, Child_Parent);
3265 end if;
3266 end;
3267 end if;
3269 Next (Item);
3270 end loop;
3271 end Check_Private_Child_Unit;
3273 ----------------------
3274 -- Check_Stub_Level --
3275 ----------------------
3277 procedure Check_Stub_Level (N : Node_Id) is
3278 Par : constant Node_Id := Parent (N);
3279 Kind : constant Node_Kind := Nkind (Par);
3281 begin
3282 if Kind in
3283 N_Package_Body | N_Subprogram_Body | N_Task_Body | N_Protected_Body
3284 and then Nkind (Parent (Par)) in N_Compilation_Unit | N_Subunit
3285 then
3286 null;
3288 -- In an instance, a missing stub appears at any level. A warning
3289 -- message will have been emitted already for the missing file.
3291 elsif not In_Instance then
3292 Error_Msg_N ("stub cannot appear in an inner scope", N);
3294 elsif Expander_Active then
3295 Error_Msg_N ("missing proper body", N);
3296 end if;
3297 end Check_Stub_Level;
3299 -------------------
3300 -- Decorate_Type --
3301 -------------------
3303 procedure Decorate_Type
3304 (Ent : Entity_Id;
3305 Scop : Entity_Id;
3306 Is_Tagged : Boolean := False;
3307 Materialize : Boolean := False)
3309 CW_Typ : Entity_Id;
3311 begin
3312 -- An unanalyzed type or a shadow entity of a type is treated as an
3313 -- incomplete type, and carries the corresponding attributes.
3315 Mutate_Ekind (Ent, E_Incomplete_Type);
3316 Set_Is_Not_Self_Hidden (Ent);
3317 Set_Etype (Ent, Ent);
3318 Set_Full_View (Ent, Empty);
3319 Set_Is_First_Subtype (Ent);
3320 Set_Scope (Ent, Scop);
3321 Set_Stored_Constraint (Ent, No_Elist);
3322 Reinit_Size_Align (Ent);
3324 if From_Limited_With (Ent) then
3325 Set_Private_Dependents (Ent, New_Elmt_List);
3326 end if;
3328 -- A tagged type and its corresponding shadow entity share one common
3329 -- class-wide type. The list of primitive operations for the shadow
3330 -- entity is empty.
3332 if Is_Tagged then
3333 Set_Is_Tagged_Type (Ent);
3334 Set_Direct_Primitive_Operations (Ent, New_Elmt_List);
3336 CW_Typ :=
3337 New_External_Entity
3338 (E_Void, Scope (Ent), Sloc (Ent), Ent, 'C', 0, 'T');
3340 Set_Class_Wide_Type (Ent, CW_Typ);
3342 -- Set parent to be the same as the parent of the tagged type.
3343 -- We need a parent field set, and it is supposed to point to
3344 -- the declaration of the type. The tagged type declaration
3345 -- essentially declares two separate types, the tagged type
3346 -- itself and the corresponding class-wide type, so it is
3347 -- reasonable for the parent fields to point to the declaration
3348 -- in both cases.
3350 Set_Parent (CW_Typ, Parent (Ent));
3352 Mutate_Ekind (CW_Typ, E_Class_Wide_Type);
3353 Set_Class_Wide_Type (CW_Typ, CW_Typ);
3354 Set_Etype (CW_Typ, Ent);
3355 Set_Equivalent_Type (CW_Typ, Empty);
3356 Set_From_Limited_With (CW_Typ, From_Limited_With (Ent));
3357 Set_Has_Unknown_Discriminants (CW_Typ);
3358 Set_Is_First_Subtype (CW_Typ);
3359 Set_Is_Tagged_Type (CW_Typ);
3360 Set_Materialize_Entity (CW_Typ, Materialize);
3361 Set_Scope (CW_Typ, Scop);
3362 Reinit_Size_Align (CW_Typ);
3363 end if;
3364 end Decorate_Type;
3366 ------------------------
3367 -- Expand_With_Clause --
3368 ------------------------
3370 procedure Expand_With_Clause (Item : Node_Id; Nam : Node_Id; N : Node_Id) is
3371 Loc : constant Source_Ptr := Sloc (Nam);
3373 function Build_Unit_Name (Nam : Node_Id) return Node_Id;
3374 -- Build name to be used in implicit with_clause. In most cases this
3375 -- is the source name, but if renamings are present we must make the
3376 -- original unit visible, not the one it renames. The entity in the
3377 -- with clause is the renamed unit, but the identifier is the one from
3378 -- the source, which allows us to recover the unit renaming.
3380 ---------------------
3381 -- Build_Unit_Name --
3382 ---------------------
3384 function Build_Unit_Name (Nam : Node_Id) return Node_Id is
3385 Ent : Entity_Id;
3386 Result : Node_Id;
3388 begin
3389 if Nkind (Nam) = N_Identifier then
3390 return New_Occurrence_Of (Entity (Nam), Loc);
3392 else
3393 Ent := Entity (Nam);
3395 if Present (Entity (Selector_Name (Nam)))
3396 and then Chars (Entity (Selector_Name (Nam))) /= Chars (Ent)
3397 and then
3398 Nkind (Unit_Declaration_Node (Entity (Selector_Name (Nam)))) =
3399 N_Package_Renaming_Declaration
3400 then
3401 -- The name in the with_clause is of the form A.B.C, and B is
3402 -- given by a renaming declaration. In that case we may not
3403 -- have analyzed the unit for B, but replaced it directly in
3404 -- lib-load with the unit it renames. We have to make A.B
3405 -- visible, so analyze the declaration for B now, in case it
3406 -- has not been done yet.
3408 Ent := Entity (Selector_Name (Nam));
3409 Analyze
3410 (Parent
3411 (Unit_Declaration_Node (Entity (Selector_Name (Nam)))));
3412 end if;
3414 Result :=
3415 Make_Expanded_Name (Loc,
3416 Chars => Chars (Entity (Nam)),
3417 Prefix => Build_Unit_Name (Prefix (Nam)),
3418 Selector_Name => New_Occurrence_Of (Ent, Loc));
3419 Set_Entity (Result, Ent);
3421 return Result;
3422 end if;
3423 end Build_Unit_Name;
3425 -- Local variables
3427 Ent : constant Entity_Id := Entity (Nam);
3428 Withn : Node_Id;
3430 -- Start of processing for Expand_With_Clause
3432 begin
3433 Withn :=
3434 Make_With_Clause (Loc,
3435 Name => Build_Unit_Name (Nam));
3437 Set_Corresponding_Spec (Withn, Ent);
3438 Set_First_Name (Withn);
3439 Set_Implicit_With (Withn);
3440 Set_Library_Unit (Withn, Parent (Unit_Declaration_Node (Ent)));
3441 Set_Parent_With (Withn);
3443 -- If the unit is a [generic] package or subprogram declaration
3444 -- (including a subprogram body acting as spec), a private_with_clause
3445 -- on a child unit implies that the implicit with on the parent is also
3446 -- private.
3448 if Nkind (Unit (N)) in N_Generic_Package_Declaration
3449 | N_Package_Declaration
3450 | N_Generic_Subprogram_Declaration
3451 | N_Subprogram_Declaration
3452 | N_Subprogram_Body
3453 then
3454 Set_Private_Present (Withn, Private_Present (Item));
3455 end if;
3457 Prepend (Withn, Context_Items (N));
3458 Mark_Rewrite_Insertion (Withn);
3460 Install_With_Clause (Withn);
3462 -- If we have "with X.Y;", we want to recurse on "X", except in the
3463 -- unusual case where X.Y is a renaming of X. In that case, the scope
3464 -- of X will be null.
3466 if Nkind (Nam) = N_Expanded_Name
3467 and then Present (Scope (Entity (Prefix (Nam))))
3468 then
3469 Expand_With_Clause (Item, Prefix (Nam), N);
3470 end if;
3471 end Expand_With_Clause;
3473 --------------------------------
3474 -- Generate_Parent_References --
3475 --------------------------------
3477 procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
3478 Pref : Node_Id;
3479 P_Name : Entity_Id := P_Id;
3481 begin
3482 if Nkind (N) = N_Subunit then
3483 Pref := Name (N);
3484 else
3485 Pref := Name (Parent (Defining_Entity (N)));
3486 end if;
3488 if Nkind (Pref) = N_Expanded_Name then
3490 -- Done already, if the unit has been compiled indirectly as
3491 -- part of the closure of its context because of inlining.
3493 return;
3494 end if;
3496 while Nkind (Pref) = N_Selected_Component loop
3497 Change_Selected_Component_To_Expanded_Name (Pref);
3498 Set_Entity (Pref, P_Name);
3499 Set_Etype (Pref, Etype (P_Name));
3500 Generate_Reference (P_Name, Pref, 'r');
3501 Pref := Prefix (Pref);
3502 P_Name := Scope (P_Name);
3503 end loop;
3505 -- The guard here on P_Name is to handle the error condition where
3506 -- the parent unit is missing because the file was not found.
3508 if Present (P_Name) then
3509 Set_Entity (Pref, P_Name);
3510 Set_Etype (Pref, Etype (P_Name));
3511 Generate_Reference (P_Name, Pref, 'r');
3512 Style.Check_Identifier (Pref, P_Name);
3513 end if;
3514 end Generate_Parent_References;
3516 ---------------------
3517 -- Has_With_Clause --
3518 ---------------------
3520 function Has_With_Clause
3521 (C_Unit : Node_Id;
3522 Pack : Entity_Id;
3523 Is_Limited : Boolean := False) return Boolean
3525 Item : Node_Id;
3527 function Named_Unit (Clause : Node_Id) return Entity_Id;
3528 -- Return the entity for the unit named in a [limited] with clause
3530 ----------------
3531 -- Named_Unit --
3532 ----------------
3534 function Named_Unit (Clause : Node_Id) return Entity_Id is
3535 begin
3536 if Nkind (Name (Clause)) = N_Selected_Component then
3537 return Entity (Selector_Name (Name (Clause)));
3538 else
3539 return Entity (Name (Clause));
3540 end if;
3541 end Named_Unit;
3543 -- Start of processing for Has_With_Clause
3545 begin
3546 Item := First (Context_Items (C_Unit));
3547 while Present (Item) loop
3548 if Nkind (Item) = N_With_Clause
3549 and then Limited_Present (Item) = Is_Limited
3550 and then Named_Unit (Item) = Pack
3551 then
3552 return True;
3553 end if;
3555 Next (Item);
3556 end loop;
3558 return False;
3559 end Has_With_Clause;
3561 -----------------------------
3562 -- Implicit_With_On_Parent --
3563 -----------------------------
3565 procedure Implicit_With_On_Parent
3566 (Child_Unit : Node_Id;
3567 N : Node_Id)
3569 Loc : constant Source_Ptr := Sloc (N);
3570 P : constant Node_Id := Parent_Spec (Child_Unit);
3571 P_Unit : Node_Id := Unit (P);
3572 P_Name : constant Entity_Id := Get_Parent_Entity (P_Unit);
3573 Withn : Node_Id;
3575 function Build_Ancestor_Name (P : Node_Id) return Node_Id;
3576 -- Build prefix of child unit name. Recurse if needed
3578 function Build_Unit_Name return Node_Id;
3579 -- If the unit is a child unit, build qualified name with all ancestors
3581 -------------------------
3582 -- Build_Ancestor_Name --
3583 -------------------------
3585 function Build_Ancestor_Name (P : Node_Id) return Node_Id is
3586 P_Ref : constant Node_Id :=
3587 New_Occurrence_Of (Defining_Entity (P), Loc);
3588 P_Spec : Node_Id := P;
3590 begin
3591 -- Ancestor may have been rewritten as a package body. Retrieve the
3592 -- original spec to trace earlier ancestors.
3594 if Nkind (P) = N_Package_Body
3595 and then Nkind (Original_Node (P)) = N_Package_Instantiation
3596 then
3597 P_Spec := Original_Node (P);
3598 end if;
3600 if No (Parent_Spec (P_Spec)) then
3601 return P_Ref;
3602 else
3603 return
3604 Make_Selected_Component (Loc,
3605 Prefix =>
3606 Build_Ancestor_Name (Unit (Parent_Spec (P_Spec))),
3607 Selector_Name => P_Ref);
3608 end if;
3609 end Build_Ancestor_Name;
3611 ---------------------
3612 -- Build_Unit_Name --
3613 ---------------------
3615 function Build_Unit_Name return Node_Id is
3616 Result : Node_Id;
3618 begin
3619 if No (Parent_Spec (P_Unit)) then
3620 return New_Occurrence_Of (P_Name, Loc);
3622 else
3623 Result :=
3624 Make_Expanded_Name (Loc,
3625 Chars => Chars (P_Name),
3626 Prefix =>
3627 Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
3628 Selector_Name => New_Occurrence_Of (P_Name, Loc));
3629 Set_Entity (Result, P_Name);
3631 return Result;
3632 end if;
3633 end Build_Unit_Name;
3635 -- Start of processing for Implicit_With_On_Parent
3637 begin
3638 -- The unit of the current compilation may be a package body that
3639 -- replaces an instance node. In this case we need the original instance
3640 -- node to construct the proper parent name.
3642 if Nkind (P_Unit) = N_Package_Body
3643 and then Nkind (Original_Node (P_Unit)) = N_Package_Instantiation
3644 then
3645 P_Unit := Original_Node (P_Unit);
3646 end if;
3648 -- We add the implicit with if the child unit is the current unit being
3649 -- compiled. If the current unit is a body, we do not want to add an
3650 -- implicit_with a second time to the corresponding spec.
3652 if Nkind (Child_Unit) = N_Package_Declaration
3653 and then Child_Unit /= Unit (Cunit (Current_Sem_Unit))
3654 then
3655 return;
3656 end if;
3658 Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
3660 Set_Corresponding_Spec (Withn, P_Name);
3661 Set_First_Name (Withn);
3662 Set_Implicit_With (Withn);
3663 Set_Library_Unit (Withn, P);
3664 Set_Parent_With (Withn);
3666 -- Node is placed at the beginning of the context items, so that
3667 -- subsequent use clauses on the parent can be validated.
3669 Prepend (Withn, Context_Items (N));
3670 Mark_Rewrite_Insertion (Withn);
3672 Install_With_Clause (Withn);
3674 if Is_Child_Spec (P_Unit) then
3675 Implicit_With_On_Parent (P_Unit, N);
3676 end if;
3677 end Implicit_With_On_Parent;
3679 --------------
3680 -- In_Chain --
3681 --------------
3683 function In_Chain (E : Entity_Id) return Boolean is
3684 H : Entity_Id;
3686 begin
3687 H := Current_Entity (E);
3688 while Present (H) loop
3689 if H = E then
3690 return True;
3691 else
3692 H := Homonym (H);
3693 end if;
3694 end loop;
3696 return False;
3697 end In_Chain;
3699 ---------------------
3700 -- Install_Context --
3701 ---------------------
3703 procedure Install_Context (N : Node_Id; Chain : Boolean := True) is
3704 Lib_Unit : constant Node_Id := Unit (N);
3706 begin
3707 Install_Context_Clauses (N, Chain);
3709 if Is_Child_Spec (Lib_Unit) then
3710 Install_Parents
3711 (Lib_Unit => Lib_Unit,
3712 Is_Private => Private_Present (Parent (Lib_Unit)),
3713 Chain => Chain);
3714 end if;
3716 Install_Limited_Context_Clauses (N);
3717 end Install_Context;
3719 -----------------------------
3720 -- Install_Context_Clauses --
3721 -----------------------------
3723 procedure Install_Context_Clauses (N : Node_Id; Chain : Boolean := True) is
3724 Lib_Unit : constant Node_Id := Unit (N);
3725 Item : Node_Id;
3726 Uname_Node : Entity_Id;
3727 Check_Private : Boolean := False;
3728 Decl_Node : Node_Id;
3729 Lib_Parent : Entity_Id;
3731 begin
3732 -- First skip configuration pragmas at the start of the context. They
3733 -- are not technically part of the context clause, but that's where the
3734 -- parser puts them. Note they were analyzed in Analyze_Context.
3736 Item := First (Context_Items (N));
3737 while Present (Item)
3738 and then Nkind (Item) = N_Pragma
3739 and then Pragma_Name (Item) in Configuration_Pragma_Names
3740 loop
3741 Next (Item);
3742 end loop;
3744 -- Loop through the actual context clause items. We process everything
3745 -- except Limited_With clauses in this routine. Limited_With clauses
3746 -- are separately installed (see Install_Limited_Context_Clauses).
3748 while Present (Item) loop
3750 -- Case of explicit WITH clause
3752 if Nkind (Item) = N_With_Clause
3753 and then not Implicit_With (Item)
3754 then
3755 if Limited_Present (Item) then
3757 -- Limited withed units will be installed later
3759 goto Continue;
3761 -- If Name (Item) is not an entity name, something is wrong, and
3762 -- this will be detected in due course, for now ignore the item
3764 elsif not Is_Entity_Name (Name (Item)) then
3765 goto Continue;
3767 elsif No (Entity (Name (Item))) then
3768 Set_Entity (Name (Item), Any_Id);
3769 goto Continue;
3770 end if;
3772 Uname_Node := Entity (Name (Item));
3774 if Is_Private_Descendant (Uname_Node) then
3775 Check_Private := True;
3776 end if;
3778 Install_With_Clause (Item);
3780 Decl_Node := Unit_Declaration_Node (Uname_Node);
3782 -- If the unit is a subprogram instance, it appears nested within
3783 -- a package that carries the parent information.
3785 if Is_Generic_Instance (Uname_Node)
3786 and then Ekind (Uname_Node) /= E_Package
3787 then
3788 Decl_Node := Parent (Parent (Decl_Node));
3789 end if;
3791 if Is_Child_Spec (Decl_Node) then
3792 if Nkind (Name (Item)) = N_Expanded_Name then
3793 Expand_With_Clause (Item, Prefix (Name (Item)), N);
3794 else
3795 -- If not an expanded name, the child unit must be a
3796 -- renaming, nothing to do.
3798 null;
3799 end if;
3801 elsif Nkind (Decl_Node) = N_Subprogram_Body
3802 and then not Acts_As_Spec (Parent (Decl_Node))
3803 and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
3804 then
3805 Implicit_With_On_Parent
3806 (Unit (Library_Unit (Parent (Decl_Node))), N);
3807 end if;
3809 -- Check license conditions unless this is a dummy unit
3811 if Sloc (Library_Unit (Item)) /= No_Location then
3812 License_Check : declare
3813 Withu : constant Unit_Number_Type :=
3814 Get_Source_Unit (Library_Unit (Item));
3815 Withl : constant License_Type :=
3816 License (Source_Index (Withu));
3817 Unitl : constant License_Type :=
3818 License (Source_Index (Current_Sem_Unit));
3820 procedure License_Error;
3821 -- Signal error of bad license
3823 -------------------
3824 -- License_Error --
3825 -------------------
3827 procedure License_Error is
3828 begin
3829 Error_Msg_N
3830 ("license of withed unit & may be inconsistent??",
3831 Name (Item));
3832 end License_Error;
3834 -- Start of processing for License_Check
3836 begin
3837 -- Exclude license check if withed unit is an internal unit.
3838 -- This situation arises e.g. with the GPL version of GNAT.
3840 if Is_Internal_Unit (Withu) then
3841 null;
3843 -- Otherwise check various cases
3844 else
3845 case Unitl is
3846 when Unknown =>
3847 null;
3849 when Restricted =>
3850 if Withl = GPL then
3851 License_Error;
3852 end if;
3854 when GPL =>
3855 if Withl = Restricted then
3856 License_Error;
3857 end if;
3859 when Modified_GPL =>
3860 if Withl = Restricted or else Withl = GPL then
3861 License_Error;
3862 end if;
3864 when Unrestricted =>
3865 null;
3866 end case;
3867 end if;
3868 end License_Check;
3869 end if;
3871 -- Case of USE PACKAGE clause
3873 elsif Nkind (Item) = N_Use_Package_Clause then
3874 Analyze_Use_Package (Item, Chain);
3876 -- Case of USE TYPE clause
3878 elsif Nkind (Item) = N_Use_Type_Clause then
3879 Analyze_Use_Type (Item, Chain);
3881 -- case of PRAGMA
3883 elsif Nkind (Item) = N_Pragma then
3884 Analyze (Item);
3885 end if;
3887 <<Continue>>
3888 Next (Item);
3889 end loop;
3891 if Is_Child_Spec (Lib_Unit) then
3893 -- The unit also has implicit with_clauses on its own parents
3895 if No (Context_Items (N)) then
3896 Set_Context_Items (N, New_List);
3897 end if;
3899 Implicit_With_On_Parent (Lib_Unit, N);
3900 end if;
3902 -- If the unit is a body, the context of the specification must also
3903 -- be installed. That includes private with_clauses in that context.
3905 if Nkind (Lib_Unit) = N_Package_Body
3906 or else (Nkind (Lib_Unit) = N_Subprogram_Body
3907 and then not Acts_As_Spec (N))
3908 then
3909 Install_Context (Library_Unit (N), Chain);
3911 -- Only install private with-clauses of a spec that comes from
3912 -- source, excluding specs created for a subprogram body that is
3913 -- a child unit.
3915 if Comes_From_Source (Library_Unit (N)) then
3916 Install_Private_With_Clauses
3917 (Defining_Entity (Unit (Library_Unit (N))));
3918 end if;
3920 if Is_Child_Spec (Unit (Library_Unit (N))) then
3922 -- If the unit is the body of a public child unit, the private
3923 -- declarations of the parent must be made visible. If the child
3924 -- unit is private, the private declarations have been installed
3925 -- already in the call to Install_Parents for the spec. Installing
3926 -- private declarations must be done for all ancestors of public
3927 -- child units. In addition, sibling units mentioned in the
3928 -- context clause of the body are directly visible.
3930 declare
3931 Lib_Spec : Node_Id;
3932 P : Node_Id;
3933 P_Name : Entity_Id;
3935 begin
3936 Lib_Spec := Unit (Library_Unit (N));
3937 while Is_Child_Spec (Lib_Spec) loop
3938 P := Unit (Parent_Spec (Lib_Spec));
3939 P_Name := Defining_Entity (P);
3941 if not (Private_Present (Parent (Lib_Spec)))
3942 and then not In_Private_Part (P_Name)
3943 then
3944 Install_Private_Declarations (P_Name);
3945 Install_Private_With_Clauses (P_Name);
3946 Set_Use (Private_Declarations (Specification (P)));
3947 end if;
3949 Lib_Spec := P;
3950 end loop;
3951 end;
3952 end if;
3954 -- For a package body, children in context are immediately visible
3956 Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3957 end if;
3959 if Nkind (Lib_Unit) in N_Generic_Package_Declaration
3960 | N_Generic_Subprogram_Declaration
3961 | N_Package_Declaration
3962 | N_Subprogram_Declaration
3963 then
3964 if Is_Child_Spec (Lib_Unit) then
3965 Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3966 Set_Is_Private_Descendant
3967 (Defining_Entity (Lib_Unit),
3968 Is_Private_Descendant (Lib_Parent)
3969 or else Private_Present (Parent (Lib_Unit)));
3971 else
3972 Set_Is_Private_Descendant
3973 (Defining_Entity (Lib_Unit),
3974 Private_Present (Parent (Lib_Unit)));
3975 end if;
3976 end if;
3978 if Check_Private then
3979 Check_Private_Child_Unit (N);
3980 end if;
3981 end Install_Context_Clauses;
3983 -------------------------------------
3984 -- Install_Limited_Context_Clauses --
3985 -------------------------------------
3987 procedure Install_Limited_Context_Clauses (N : Node_Id) is
3988 Item : Node_Id;
3990 procedure Check_Renamings (P : Node_Id; W : Node_Id);
3991 -- Check that the unlimited view of a given compilation_unit is not
3992 -- already visible through "use + renamings".
3994 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3995 -- Check that if a limited_with clause of a given compilation_unit
3996 -- mentions a descendant of a private child of some library unit, then
3997 -- the given compilation_unit must be the declaration of a private
3998 -- descendant of that library unit, or a public descendant of such. The
3999 -- code is analogous to that of Check_Private_Child_Unit but we cannot
4000 -- use entities on the limited with_clauses because their units have not
4001 -- been analyzed, so we have to climb the tree of ancestors looking for
4002 -- private keywords.
4004 procedure Expand_Limited_With_Clause
4005 (Comp_Unit : Node_Id;
4006 Nam : Node_Id;
4007 N : Node_Id);
4008 -- If a child unit appears in a limited_with clause, there are implicit
4009 -- limited_with clauses on all parents that are not already visible
4010 -- through a regular with clause. This procedure creates the implicit
4011 -- limited with_clauses for the parents and loads the corresponding
4012 -- units. The shadow entities are created when the inserted clause is
4013 -- analyzed. Implements Ada 2005 (AI-50217).
4015 ---------------------
4016 -- Check_Renamings --
4017 ---------------------
4019 procedure Check_Renamings (P : Node_Id; W : Node_Id) is
4020 Item : Node_Id;
4021 Spec : Node_Id;
4022 WEnt : Entity_Id;
4023 E : Entity_Id;
4024 E2 : Entity_Id;
4026 begin
4027 pragma Assert (Nkind (W) = N_With_Clause);
4029 -- Protect the frontend against previous critical errors
4031 case Nkind (Unit (Library_Unit (W))) is
4032 when N_Generic_Package_Declaration
4033 | N_Generic_Subprogram_Declaration
4034 | N_Package_Declaration
4035 | N_Subprogram_Declaration
4037 null;
4039 when others =>
4040 return;
4041 end case;
4043 -- Check "use + renamings"
4045 WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
4046 Spec := Specification (Unit (P));
4048 Item := First (Visible_Declarations (Spec));
4049 while Present (Item) loop
4051 -- Look only at use package clauses
4053 if Nkind (Item) = N_Use_Package_Clause then
4055 E := Entity (Name (Item));
4057 pragma Assert (Present (Parent (E)));
4059 if Nkind (Parent (E)) = N_Package_Renaming_Declaration
4060 and then Renamed_Entity (E) = WEnt
4061 then
4062 -- The unlimited view is visible through use clause and
4063 -- renamings. There is no need to generate the error
4064 -- message here because Is_Visible_Through_Renamings
4065 -- takes care of generating the precise error message.
4067 return;
4069 elsif Nkind (Parent (E)) = N_Package_Specification then
4071 -- The use clause may refer to a local package.
4072 -- Check all the enclosing scopes.
4074 E2 := E;
4075 while E2 /= Standard_Standard and then E2 /= WEnt loop
4076 E2 := Scope (E2);
4077 end loop;
4079 if E2 = WEnt then
4080 Error_Msg_N
4081 ("unlimited view visible through use clause", W);
4082 return;
4083 end if;
4084 end if;
4085 end if;
4087 Next (Item);
4088 end loop;
4090 -- Recursive call to check all the ancestors
4092 if Is_Child_Spec (Unit (P)) then
4093 Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
4094 end if;
4095 end Check_Renamings;
4097 ---------------------------------------
4098 -- Check_Private_Limited_Withed_Unit --
4099 ---------------------------------------
4101 procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
4102 Curr_Parent : Node_Id;
4103 Child_Parent : Node_Id;
4104 Curr_Private : Boolean;
4106 begin
4107 -- Compilation unit of the parent of the withed library unit
4109 Child_Parent := Library_Unit (Item);
4111 -- If the child unit is a public child, then locate its nearest
4112 -- private ancestor, if any, then Child_Parent will then be set to
4113 -- the parent of that ancestor.
4115 if not Private_Present (Library_Unit (Item)) then
4116 while Present (Child_Parent)
4117 and then not Private_Present (Child_Parent)
4118 loop
4119 Child_Parent := Parent_Spec (Unit (Child_Parent));
4120 end loop;
4122 if No (Child_Parent) then
4123 return;
4124 end if;
4125 end if;
4127 Child_Parent := Parent_Spec (Unit (Child_Parent));
4129 -- Traverse all the ancestors of the current compilation unit to
4130 -- check if it is a descendant of named library unit.
4132 Curr_Parent := Parent (Item);
4133 Curr_Private := Private_Present (Curr_Parent);
4135 while Present (Parent_Spec (Unit (Curr_Parent)))
4136 and then Curr_Parent /= Child_Parent
4137 loop
4138 Curr_Parent := Parent_Spec (Unit (Curr_Parent));
4139 Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
4140 end loop;
4142 if Curr_Parent /= Child_Parent then
4143 Error_Msg_N
4144 ("unit in with clause is private child unit!", Item);
4145 Error_Msg_NE
4146 ("\current unit must also have parent&!",
4147 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
4149 elsif Private_Present (Parent (Item))
4150 or else Curr_Private
4151 or else Private_Present (Item)
4152 or else Nkind (Unit (Parent (Item))) in
4153 N_Package_Body | N_Subprogram_Body | N_Subunit
4154 then
4155 -- Current unit is private, of descendant of a private unit
4157 null;
4159 else
4160 Error_Msg_NE
4161 ("current unit must also be private descendant of&",
4162 Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
4163 end if;
4164 end Check_Private_Limited_Withed_Unit;
4166 --------------------------------
4167 -- Expand_Limited_With_Clause --
4168 --------------------------------
4170 procedure Expand_Limited_With_Clause
4171 (Comp_Unit : Node_Id;
4172 Nam : Node_Id;
4173 N : Node_Id)
4175 Loc : constant Source_Ptr := Sloc (Nam);
4176 Unum : Unit_Number_Type;
4177 Withn : Node_Id;
4179 function Previous_Withed_Unit (W : Node_Id) return Boolean;
4180 -- Returns true if the context already includes a with_clause for
4181 -- this unit. If the with_clause is nonlimited, the unit is fully
4182 -- visible and an implicit limited_with should not be created. If
4183 -- there is already a limited_with clause for W, a second one is
4184 -- simply redundant.
4186 --------------------------
4187 -- Previous_Withed_Unit --
4188 --------------------------
4190 function Previous_Withed_Unit (W : Node_Id) return Boolean is
4191 Item : Node_Id;
4193 begin
4194 -- A limited with_clause cannot appear in the same context_clause
4195 -- as a nonlimited with_clause which mentions the same library.
4197 Item := First (Context_Items (Comp_Unit));
4198 while Present (Item) loop
4199 if Nkind (Item) = N_With_Clause
4200 and then Library_Unit (Item) = Library_Unit (W)
4201 then
4202 return True;
4203 end if;
4205 Next (Item);
4206 end loop;
4208 return False;
4209 end Previous_Withed_Unit;
4211 -- Start of processing for Expand_Limited_With_Clause
4213 begin
4214 if Nkind (Nam) = N_Identifier then
4216 -- Create node for name of withed unit
4218 Withn :=
4219 Make_With_Clause (Loc,
4220 Name => New_Copy (Nam));
4222 else pragma Assert (Nkind (Nam) = N_Selected_Component);
4223 Withn :=
4224 Make_With_Clause (Loc,
4225 Name => Make_Selected_Component (Loc,
4226 Prefix => New_Copy_Tree (Prefix (Nam)),
4227 Selector_Name => New_Copy (Selector_Name (Nam))));
4228 Set_Parent (Withn, Parent (N));
4229 end if;
4231 Set_First_Name (Withn);
4232 Set_Implicit_With (Withn);
4233 Set_Limited_Present (Withn);
4235 Unum :=
4236 Load_Unit
4237 (Load_Name => Get_Spec_Name (Get_Unit_Name (Nam)),
4238 Required => True,
4239 Subunit => False,
4240 Error_Node => Nam);
4242 -- Do not generate a limited_with_clause on the current unit. This
4243 -- path is taken when a unit has a limited_with clause on one of its
4244 -- child units.
4246 if Unum = Current_Sem_Unit then
4247 return;
4248 end if;
4250 Set_Library_Unit (Withn, Cunit (Unum));
4251 Set_Corresponding_Spec
4252 (Withn, Specification (Unit (Cunit (Unum))));
4254 if not Previous_Withed_Unit (Withn) then
4255 Prepend (Withn, Context_Items (Parent (N)));
4256 Mark_Rewrite_Insertion (Withn);
4258 -- Add implicit limited_with_clauses for parents of child units
4259 -- mentioned in limited_with clauses.
4261 if Nkind (Nam) = N_Selected_Component then
4262 Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
4263 end if;
4265 Analyze (Withn);
4267 if not Limited_View_Installed (Withn) then
4268 Install_Limited_With_Clause (Withn);
4269 end if;
4270 end if;
4271 end Expand_Limited_With_Clause;
4273 -- Start of processing for Install_Limited_Context_Clauses
4275 begin
4276 Item := First (Context_Items (N));
4277 while Present (Item) loop
4278 if Nkind (Item) = N_With_Clause
4279 and then Limited_Present (Item)
4280 and then not Error_Posted (Item)
4281 then
4282 if Nkind (Name (Item)) = N_Selected_Component then
4283 Expand_Limited_With_Clause
4284 (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
4285 end if;
4287 Check_Private_Limited_Withed_Unit (Item);
4289 if not Implicit_With (Item) and then Is_Child_Spec (Unit (N)) then
4290 Check_Renamings (Parent_Spec (Unit (N)), Item);
4291 end if;
4293 -- A unit may have a limited with on itself if it has a limited
4294 -- with_clause on one of its child units. In that case it is
4295 -- already being compiled and it makes no sense to install its
4296 -- limited view.
4298 -- If the item is a limited_private_with_clause, install it if the
4299 -- current unit is a body or if it is a private child. Otherwise
4300 -- the private clause is installed before analyzing the private
4301 -- part of the current unit.
4303 if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
4304 and then not Limited_View_Installed (Item)
4305 and then
4306 not Is_Ancestor_Unit
4307 (Library_Unit (Item), Cunit (Current_Sem_Unit))
4308 then
4309 if not Private_Present (Item)
4310 or else Private_Present (N)
4311 or else Nkind (Unit (N)) in
4312 N_Package_Body | N_Subprogram_Body | N_Subunit
4313 then
4314 Install_Limited_With_Clause (Item);
4315 end if;
4316 end if;
4317 end if;
4319 Next (Item);
4320 end loop;
4322 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4323 -- looking for incomplete subtype declarations of incomplete types
4324 -- visible through a limited with clause.
4326 if Ada_Version >= Ada_2005
4327 and then Analyzed (N)
4328 and then Nkind (Unit (N)) = N_Package_Declaration
4329 then
4330 declare
4331 Decl : Node_Id;
4332 Def_Id : Entity_Id;
4333 Non_Lim_View : Entity_Id;
4335 begin
4336 Decl := First (Visible_Declarations (Specification (Unit (N))));
4337 while Present (Decl) loop
4338 if Nkind (Decl) = N_Subtype_Declaration
4339 and then
4340 Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
4341 and then
4342 From_Limited_With (Defining_Identifier (Decl))
4343 then
4344 Def_Id := Defining_Identifier (Decl);
4345 Non_Lim_View := Non_Limited_View (Def_Id);
4347 if not Is_Incomplete_Type (Non_Lim_View) then
4349 -- Convert an incomplete subtype declaration into a
4350 -- corresponding nonlimited view subtype declaration.
4351 -- This is usually the case when analyzing a body that
4352 -- has regular with clauses, when the spec has limited
4353 -- ones.
4355 -- If the nonlimited view is still incomplete, it is
4356 -- the dummy entry already created, and the declaration
4357 -- cannot be reanalyzed. This is the case when installing
4358 -- a parent unit that has limited with-clauses.
4360 Set_Subtype_Indication (Decl,
4361 New_Occurrence_Of (Non_Lim_View, Sloc (Def_Id)));
4362 Set_Etype (Def_Id, Non_Lim_View);
4363 Reinit_Field_To_Zero (Def_Id, F_Non_Limited_View,
4364 Old_Ekind => (E_Incomplete_Subtype => True,
4365 others => False));
4366 Reinit_Field_To_Zero (Def_Id, F_Private_Dependents);
4367 Mutate_Ekind
4368 (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
4369 Set_Analyzed (Decl, False);
4371 -- Reanalyze the declaration, suppressing the call to
4372 -- Enter_Name to avoid duplicate names.
4374 Analyze_Subtype_Declaration
4375 (N => Decl,
4376 Skip => True);
4377 end if;
4378 end if;
4380 Next (Decl);
4381 end loop;
4382 end;
4383 end if;
4384 end Install_Limited_Context_Clauses;
4386 ---------------------
4387 -- Install_Parents --
4388 ---------------------
4390 procedure Install_Parents
4391 (Lib_Unit : Node_Id;
4392 Is_Private : Boolean;
4393 Chain : Boolean := True)
4395 P : Node_Id;
4396 E_Name : Entity_Id;
4397 P_Name : Entity_Id;
4398 P_Spec : Node_Id;
4400 begin
4401 P := Unit (Parent_Spec (Lib_Unit));
4402 P_Name := Get_Parent_Entity (P);
4404 if Etype (P_Name) = Any_Type then
4405 return;
4406 end if;
4408 if Ekind (P_Name) = E_Generic_Package
4409 and then Nkind (Lib_Unit) not in N_Generic_Declaration
4410 | N_Generic_Renaming_Declaration
4411 then
4412 Error_Msg_N
4413 ("child of a generic package must be a generic unit", Lib_Unit);
4415 elsif not Is_Package_Or_Generic_Package (P_Name) then
4416 Error_Msg_N
4417 ("parent unit must be package or generic package", Lib_Unit);
4418 raise Unrecoverable_Error;
4420 elsif Present (Renamed_Entity (P_Name)) then
4421 Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
4422 raise Unrecoverable_Error;
4424 -- Verify that a child of an instance is itself an instance, or the
4425 -- renaming of one. Given that an instance that is a unit is replaced
4426 -- with a package declaration, check against the original node. The
4427 -- parent may be currently being instantiated, in which case it appears
4428 -- as a declaration, but the generic_parent is already established
4429 -- indicating that we deal with an instance.
4431 elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
4432 if Nkind (Lib_Unit) in N_Renaming_Declaration
4433 or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
4434 or else
4435 (Nkind (Lib_Unit) = N_Package_Declaration
4436 and then Present (Generic_Parent (Specification (Lib_Unit))))
4437 then
4438 null;
4439 else
4440 Error_Msg_N
4441 ("child of an instance must be an instance or renaming",
4442 Lib_Unit);
4443 end if;
4444 end if;
4446 -- This is the recursive call that ensures all parents are loaded
4448 if Is_Child_Spec (P) then
4449 Install_Parents
4450 (Lib_Unit => P,
4451 Is_Private =>
4452 Is_Private or else Private_Present (Parent (Lib_Unit)),
4453 Chain => Chain);
4454 end if;
4456 -- Now we can install the context for this parent
4458 Install_Context_Clauses (Parent_Spec (Lib_Unit), Chain);
4459 Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
4460 Install_Siblings (P_Name, Parent (Lib_Unit));
4462 -- The child unit is in the declarative region of the parent. The parent
4463 -- must therefore appear in the scope stack and be visible, as when
4464 -- compiling the corresponding body. If the child unit is private or it
4465 -- is a package body, private declarations must be accessible as well.
4466 -- Use declarations in the parent must also be installed. Finally, other
4467 -- child units of the same parent that are in the context are
4468 -- immediately visible.
4470 -- Find entity for compilation unit, and set its private descendant
4471 -- status as needed. Indicate that it is a compilation unit, which is
4472 -- redundant in general, but needed if this is a generated child spec
4473 -- for a child body without previous spec.
4475 E_Name := Defining_Entity (Lib_Unit);
4477 Set_Is_Child_Unit (E_Name);
4478 Set_Is_Compilation_Unit (E_Name);
4480 Set_Is_Private_Descendant (E_Name,
4481 Is_Private_Descendant (P_Name)
4482 or else Private_Present (Parent (Lib_Unit)));
4484 P_Spec := Package_Specification (P_Name);
4485 Push_Scope (P_Name);
4487 -- Save current visibility of unit
4489 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
4490 Is_Immediately_Visible (P_Name);
4491 Set_Is_Immediately_Visible (P_Name);
4492 Install_Visible_Declarations (P_Name);
4493 Set_Use (Visible_Declarations (P_Spec));
4495 -- If the parent is a generic unit, its formal part may contain formal
4496 -- packages and use clauses for them.
4498 if Ekind (P_Name) = E_Generic_Package then
4499 Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
4500 end if;
4502 if Is_Private or else Private_Present (Parent (Lib_Unit)) then
4503 Install_Private_Declarations (P_Name);
4504 Install_Private_With_Clauses (P_Name);
4505 Set_Use (Private_Declarations (P_Spec));
4506 end if;
4507 end Install_Parents;
4509 ----------------------------------
4510 -- Install_Private_With_Clauses --
4511 ----------------------------------
4513 procedure Install_Private_With_Clauses (P : Entity_Id) is
4514 Decl : constant Node_Id := Unit_Declaration_Node (P);
4515 Item : Node_Id;
4517 begin
4518 if Debug_Flag_I then
4519 Write_Str ("install private with clauses of ");
4520 Write_Name (Chars (P));
4521 Write_Eol;
4522 end if;
4524 if Nkind (Parent (Decl)) = N_Compilation_Unit then
4525 Item := First (Context_Items (Parent (Decl)));
4526 while Present (Item) loop
4527 if Nkind (Item) = N_With_Clause
4528 and then Private_Present (Item)
4529 then
4530 -- If the unit is an ancestor of the current one, it is the
4531 -- case of a private limited with clause on a child unit, and
4532 -- the compilation of one of its descendants, In that case the
4533 -- limited view is errelevant.
4535 if Limited_Present (Item) then
4536 if not Limited_View_Installed (Item)
4537 and then
4538 not Is_Ancestor_Unit (Library_Unit (Item),
4539 Cunit (Current_Sem_Unit))
4540 then
4541 Install_Limited_With_Clause (Item);
4542 end if;
4543 else
4544 Install_With_Clause (Item, Private_With_OK => True);
4545 end if;
4546 end if;
4548 Next (Item);
4549 end loop;
4550 end if;
4551 end Install_Private_With_Clauses;
4553 ----------------------
4554 -- Install_Siblings --
4555 ----------------------
4557 procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
4558 Item : Node_Id;
4559 Id : Entity_Id;
4560 Prev : Entity_Id;
4562 begin
4563 -- Iterate over explicit with clauses, and check whether the scope of
4564 -- each entity is an ancestor of the current unit, in which case it is
4565 -- immediately visible.
4567 Item := First (Context_Items (N));
4568 while Present (Item) loop
4570 -- Do not install private_with_clauses declaration, unless unit
4571 -- is itself a private child unit, or is a body. Note that for a
4572 -- subprogram body the private_with_clause does not take effect
4573 -- until after the specification.
4575 if Nkind (Item) /= N_With_Clause
4576 or else Implicit_With (Item)
4577 or else Limited_Present (Item)
4578 or else Error_Posted (Item)
4580 -- Skip processing malformed trees
4582 or else (Try_Semantics
4583 and then Nkind (Name (Item)) not in N_Has_Entity)
4584 then
4585 null;
4587 elsif not Private_Present (Item)
4588 or else Private_Present (N)
4589 or else Nkind (Unit (N)) = N_Package_Body
4590 then
4591 Id := Entity (Name (Item));
4593 if Is_Child_Unit (Id)
4594 and then Is_Ancestor_Package (Scope (Id), U_Name)
4595 then
4596 Set_Is_Immediately_Visible (Id);
4598 -- Check for the presence of another unit in the context that
4599 -- may be inadvertently hidden by the child.
4601 Prev := Current_Entity (Id);
4603 if Present (Prev)
4604 and then Is_Immediately_Visible (Prev)
4605 and then not Is_Child_Unit (Prev)
4606 then
4607 declare
4608 Clause : Node_Id;
4610 begin
4611 Clause := First (Context_Items (N));
4612 while Present (Clause) loop
4613 if Nkind (Clause) = N_With_Clause
4614 and then Entity (Name (Clause)) = Prev
4615 then
4616 Error_Msg_NE
4617 ("child unit& hides compilation unit " &
4618 "with the same name??",
4619 Name (Item), Id);
4620 exit;
4621 end if;
4623 Next (Clause);
4624 end loop;
4625 end;
4626 end if;
4628 -- The With_Clause may be on a grandchild or one of its further
4629 -- descendants, which makes a child immediately visible. Examine
4630 -- ancestry to determine whether such a child exists. For example,
4631 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4632 -- is immediately visible.
4634 elsif Is_Child_Unit (Id) then
4635 declare
4636 Par : Entity_Id;
4638 begin
4639 Par := Scope (Id);
4640 while Is_Child_Unit (Par) loop
4641 if Is_Ancestor_Package (Scope (Par), U_Name) then
4642 Set_Is_Immediately_Visible (Par);
4643 exit;
4644 end if;
4646 Par := Scope (Par);
4647 end loop;
4648 end;
4649 end if;
4651 -- If the item is a private with-clause on a child unit, the parent
4652 -- may have been installed already, but the child unit must remain
4653 -- invisible until installed in a private part or body, unless there
4654 -- is already a regular with_clause for it in the current unit.
4656 elsif Private_Present (Item) then
4657 Id := Entity (Name (Item));
4659 if Is_Child_Unit (Id) then
4660 declare
4661 Clause : Node_Id;
4663 function In_Context return Boolean;
4664 -- Scan context of current unit, to check whether there is
4665 -- a with_clause on the same unit as a private with-clause
4666 -- on a parent, in which case child unit is visible. If the
4667 -- unit is a grandchild, the same applies to its parent.
4669 ----------------
4670 -- In_Context --
4671 ----------------
4673 function In_Context return Boolean is
4674 begin
4675 Clause :=
4676 First (Context_Items (Cunit (Current_Sem_Unit)));
4677 while Present (Clause) loop
4678 if Nkind (Clause) = N_With_Clause
4679 and then Comes_From_Source (Clause)
4680 and then Is_Entity_Name (Name (Clause))
4681 and then not Private_Present (Clause)
4682 then
4683 if Entity (Name (Clause)) = Id
4684 or else
4685 (Nkind (Name (Clause)) = N_Expanded_Name
4686 and then Entity (Prefix (Name (Clause))) = Id)
4687 then
4688 return True;
4689 end if;
4690 end if;
4692 Next (Clause);
4693 end loop;
4695 return False;
4696 end In_Context;
4698 begin
4699 Set_Is_Visible_Lib_Unit (Id, In_Context);
4700 end;
4701 end if;
4702 end if;
4704 Next (Item);
4705 end loop;
4706 end Install_Siblings;
4708 ---------------------------------
4709 -- Install_Limited_With_Clause --
4710 ---------------------------------
4712 procedure Install_Limited_With_Clause (N : Node_Id) is
4713 P_Unit : constant Entity_Id := Unit (Library_Unit (N));
4714 E : Entity_Id;
4715 P : Entity_Id;
4716 Is_Child_Package : Boolean := False;
4717 Lim_Header : Entity_Id;
4718 Lim_Typ : Entity_Id;
4720 procedure Check_Body_Required;
4721 -- A unit mentioned in a limited with_clause may not be mentioned in
4722 -- a regular with_clause, but must still be included in the current
4723 -- partition. We need to determine whether the unit needs a body, so
4724 -- that the binder can determine the name of the file to be compiled.
4725 -- Checking whether a unit needs a body can be done without semantic
4726 -- analysis, by examining the nature of the declarations in the package.
4728 function Has_Limited_With_Clause
4729 (C_Unit : Entity_Id;
4730 Pack : Entity_Id) return Boolean;
4731 -- Determine whether any package in the ancestor chain starting with
4732 -- C_Unit has a limited with clause for package Pack.
4734 -------------------------
4735 -- Check_Body_Required --
4736 -------------------------
4738 procedure Check_Body_Required is
4739 PA : constant List_Id :=
4740 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
4742 procedure Check_Declarations (Spec : Node_Id);
4743 -- Recursive procedure that does the work and checks nested packages
4745 ------------------------
4746 -- Check_Declarations --
4747 ------------------------
4749 procedure Check_Declarations (Spec : Node_Id) is
4750 Decl : Node_Id;
4751 Incomplete_Decls : constant Elist_Id := New_Elmt_List;
4753 Subp_List : constant Elist_Id := New_Elmt_List;
4755 procedure Check_Pragma_Import (P : Node_Id);
4756 -- If a pragma import applies to a previous subprogram, the
4757 -- enclosing unit may not need a body. The processing is syntactic
4758 -- and does not require a declaration to be analyzed. The code
4759 -- below also handles pragma Import when applied to a subprogram
4760 -- that renames another. In this case the pragma applies to the
4761 -- renamed entity.
4763 -- Chains of multiple renames are not handled by the code below.
4764 -- It is probably impossible to handle all cases without proper
4765 -- name resolution. In such cases the algorithm is conservative
4766 -- and will indicate that a body is needed???
4768 -------------------------
4769 -- Check_Pragma_Import --
4770 -------------------------
4772 procedure Check_Pragma_Import (P : Node_Id) is
4773 Arg : Node_Id;
4774 Prev_Id : Elmt_Id;
4775 Subp_Id : Elmt_Id;
4776 Imported : Node_Id;
4778 procedure Remove_Homonyms (E : Node_Id);
4779 -- Make one pass over list of subprograms. Called again if
4780 -- subprogram is a renaming. E is known to be an identifier.
4782 ---------------------
4783 -- Remove_Homonyms --
4784 ---------------------
4786 procedure Remove_Homonyms (E : Node_Id) is
4787 R : Entity_Id := Empty;
4788 -- Name of renamed entity, if any
4790 begin
4791 Subp_Id := First_Elmt (Subp_List);
4792 while Present (Subp_Id) loop
4793 if Chars (Node (Subp_Id)) = Chars (E) then
4794 if Nkind (Parent (Parent (Node (Subp_Id))))
4795 /= N_Subprogram_Renaming_Declaration
4796 then
4797 Prev_Id := Subp_Id;
4798 Next_Elmt (Subp_Id);
4799 Remove_Elmt (Subp_List, Prev_Id);
4800 else
4801 R := Name (Parent (Parent (Node (Subp_Id))));
4802 exit;
4803 end if;
4804 else
4805 Next_Elmt (Subp_Id);
4806 end if;
4807 end loop;
4809 if Present (R) then
4810 if Nkind (R) = N_Identifier then
4811 Remove_Homonyms (R);
4813 elsif Nkind (R) = N_Selected_Component then
4814 Remove_Homonyms (Selector_Name (R));
4816 -- Renaming of attribute
4818 else
4819 null;
4820 end if;
4821 end if;
4822 end Remove_Homonyms;
4824 -- Start of processing for Check_Pragma_Import
4826 begin
4827 -- Find name of entity in Import pragma. We have not analyzed
4828 -- the construct, so we must guard against syntax errors.
4830 Arg := Next (First (Pragma_Argument_Associations (P)));
4832 if No (Arg)
4833 or else Nkind (Expression (Arg)) /= N_Identifier
4834 then
4835 return;
4836 else
4837 Imported := Expression (Arg);
4838 end if;
4840 Remove_Homonyms (Imported);
4841 end Check_Pragma_Import;
4843 -- Start of processing for Check_Declarations
4845 begin
4846 -- Search for Elaborate Body pragma
4848 Decl := First (Visible_Declarations (Spec));
4849 while Present (Decl)
4850 and then Nkind (Decl) = N_Pragma
4851 loop
4852 if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4853 Set_Body_Required (Library_Unit (N));
4854 return;
4855 end if;
4857 Next (Decl);
4858 end loop;
4860 -- Look for declarations that require the presence of a body. We
4861 -- have already skipped pragmas at the start of the list.
4863 while Present (Decl) loop
4865 -- Subprogram that comes from source means body may be needed.
4866 -- Save for subsequent examination of import pragmas.
4868 if Comes_From_Source (Decl)
4869 and then Nkind (Decl) in N_Subprogram_Declaration
4870 | N_Subprogram_Renaming_Declaration
4871 | N_Generic_Subprogram_Declaration
4872 then
4873 Append_Elmt (Defining_Entity (Decl), Subp_List);
4875 -- Package declaration of generic package declaration. We need
4876 -- to recursively examine nested declarations.
4878 elsif Nkind (Decl) in N_Package_Declaration
4879 | N_Generic_Package_Declaration
4880 then
4881 Check_Declarations (Specification (Decl));
4883 elsif Nkind (Decl) = N_Pragma
4884 and then Pragma_Name (Decl) = Name_Import
4885 then
4886 Check_Pragma_Import (Decl);
4887 end if;
4889 Next (Decl);
4890 end loop;
4892 -- Same set of tests for private part. In addition to subprograms
4893 -- detect the presence of Taft Amendment types (incomplete types
4894 -- completed in the body).
4896 Decl := First (Private_Declarations (Spec));
4897 while Present (Decl) loop
4898 if Comes_From_Source (Decl)
4899 and then Nkind (Decl) in N_Subprogram_Declaration
4900 | N_Subprogram_Renaming_Declaration
4901 | N_Generic_Subprogram_Declaration
4902 then
4903 Append_Elmt (Defining_Entity (Decl), Subp_List);
4905 elsif Nkind (Decl) in N_Package_Declaration
4906 | N_Generic_Package_Declaration
4907 then
4908 Check_Declarations (Specification (Decl));
4910 -- Collect incomplete type declarations for separate pass
4912 elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4913 Append_Elmt (Decl, Incomplete_Decls);
4915 elsif Nkind (Decl) = N_Pragma
4916 and then Pragma_Name (Decl) = Name_Import
4917 then
4918 Check_Pragma_Import (Decl);
4919 end if;
4921 Next (Decl);
4922 end loop;
4924 -- Now check incomplete declarations to locate Taft amendment
4925 -- types. This can be done by examining the defining identifiers
4926 -- of type declarations without real semantic analysis.
4928 declare
4929 Inc : Elmt_Id;
4931 begin
4932 Inc := First_Elmt (Incomplete_Decls);
4933 while Present (Inc) loop
4934 Decl := Next (Node (Inc));
4935 while Present (Decl) loop
4936 if Nkind (Decl) = N_Full_Type_Declaration
4937 and then Chars (Defining_Identifier (Decl)) =
4938 Chars (Defining_Identifier (Node (Inc)))
4939 then
4940 exit;
4941 end if;
4943 Next (Decl);
4944 end loop;
4946 -- If no completion, this is a TAT, and a body is needed
4948 if No (Decl) then
4949 Set_Body_Required (Library_Unit (N));
4950 return;
4951 end if;
4953 Next_Elmt (Inc);
4954 end loop;
4955 end;
4957 -- Finally, check whether there are subprograms that still require
4958 -- a body, i.e. are not renamings or null.
4960 if not Is_Empty_Elmt_List (Subp_List) then
4961 declare
4962 Subp_Id : Elmt_Id;
4963 Spec : Node_Id;
4965 begin
4966 Subp_Id := First_Elmt (Subp_List);
4967 Spec := Parent (Node (Subp_Id));
4969 while Present (Subp_Id) loop
4970 if Nkind (Parent (Spec))
4971 = N_Subprogram_Renaming_Declaration
4972 then
4973 null;
4975 elsif Nkind (Spec) = N_Procedure_Specification
4976 and then Null_Present (Spec)
4977 then
4978 null;
4980 else
4981 Set_Body_Required (Library_Unit (N));
4982 return;
4983 end if;
4985 Next_Elmt (Subp_Id);
4986 end loop;
4987 end;
4988 end if;
4989 end Check_Declarations;
4991 -- Start of processing for Check_Body_Required
4993 begin
4994 -- If this is an imported package (Java and CIL usage) no body is
4995 -- needed. Scan list of pragmas that may follow a compilation unit
4996 -- to look for a relevant pragma Import.
4998 if Present (PA) then
4999 declare
5000 Prag : Node_Id;
5002 begin
5003 Prag := First (PA);
5004 while Present (Prag) loop
5005 if Nkind (Prag) = N_Pragma
5006 and then Get_Pragma_Id (Prag) = Pragma_Import
5007 then
5008 return;
5009 end if;
5011 Next (Prag);
5012 end loop;
5013 end;
5014 end if;
5016 Check_Declarations (Specification (P_Unit));
5017 end Check_Body_Required;
5019 -----------------------------
5020 -- Has_Limited_With_Clause --
5021 -----------------------------
5023 function Has_Limited_With_Clause
5024 (C_Unit : Entity_Id;
5025 Pack : Entity_Id) return Boolean
5027 Par : Entity_Id;
5028 Par_Unit : Node_Id;
5030 begin
5031 Par := C_Unit;
5032 while Present (Par) loop
5033 if Ekind (Par) /= E_Package then
5034 exit;
5035 end if;
5037 -- Retrieve the Compilation_Unit node for Par and determine if
5038 -- its context clauses contain a limited with for Pack.
5040 Par_Unit := Parent (Parent (Parent (Par)));
5042 if Nkind (Par_Unit) = N_Package_Declaration then
5043 Par_Unit := Parent (Par_Unit);
5044 end if;
5046 if Has_With_Clause (Par_Unit, Pack, True) then
5047 return True;
5048 end if;
5050 -- If there are more ancestors, climb up the tree, otherwise we
5051 -- are done.
5053 if Is_Child_Unit (Par) then
5054 Par := Scope (Par);
5055 else
5056 exit;
5057 end if;
5058 end loop;
5060 return False;
5061 end Has_Limited_With_Clause;
5063 -- Start of processing for Install_Limited_With_Clause
5065 begin
5066 pragma Assert (not Limited_View_Installed (N));
5068 -- In case of limited with_clause on subprograms, generics, instances,
5069 -- or renamings, the corresponding error was previously posted and we
5070 -- have nothing to do here. If the file is missing altogether, it has
5071 -- no source location.
5073 if Nkind (P_Unit) /= N_Package_Declaration
5074 or else Sloc (P_Unit) = No_Location
5075 then
5076 return;
5077 end if;
5079 P := Defining_Unit_Name (Specification (P_Unit));
5081 -- Handle child packages
5083 if Nkind (P) = N_Defining_Program_Unit_Name then
5084 Is_Child_Package := True;
5085 P := Defining_Identifier (P);
5086 end if;
5088 -- Do not install the limited-view if the context of the unit is already
5089 -- available through a regular with clause.
5091 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
5092 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
5093 then
5094 return;
5095 end if;
5097 -- Do not install the limited-view if the full-view is already visible
5098 -- through renaming declarations.
5100 if Is_Visible_Through_Renamings (P, N) then
5101 return;
5102 end if;
5104 -- Do not install the limited view if this is the unit being analyzed.
5105 -- This unusual case will happen when a unit has a limited_with clause
5106 -- on one of its children. The compilation of the child forces the load
5107 -- of the parent which tries to install the limited view of the child
5108 -- again. Installing the limited view must also be disabled when
5109 -- compiling the body of the child unit.
5111 if P = Cunit_Entity (Current_Sem_Unit)
5112 or else (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
5113 and then P = Main_Unit_Entity
5114 and then Is_Ancestor_Unit
5115 (Cunit (Main_Unit), Cunit (Current_Sem_Unit)))
5116 then
5117 return;
5118 end if;
5120 -- This scenario is similar to the one above, the difference is that the
5121 -- compilation of sibling Par.Sib forces the load of parent Par which
5122 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
5123 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
5124 -- nonlimited views of all entities from Lim_Pack.
5126 -- limited with Lim_Pack; -- [1]
5127 -- package Par is ... package Lim_Pack is ...
5129 -- with Lim_Pack; -- [2]
5130 -- package Par.Sib is ... package body Par.Sib is ...
5132 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
5133 -- Sem_Unit is the body of Par.Sib.
5135 if Ekind (P) = E_Package
5136 and then Ekind (Main_Unit_Entity) = E_Package
5137 and then Is_Child_Unit (Main_Unit_Entity)
5139 -- The body has a regular with clause
5141 and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
5142 and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
5144 -- One of the ancestors has a limited with clause
5146 and then Nkind (Parent (Parent (Main_Unit_Entity))) =
5147 N_Package_Specification
5148 and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
5149 then
5150 return;
5151 end if;
5153 -- A common use of the limited-with is to have a limited-with in the
5154 -- package spec, and a normal with in its package body. For example:
5156 -- limited with X; -- [1]
5157 -- package A is ...
5159 -- with X; -- [2]
5160 -- package body A is ...
5162 -- The compilation of A's body installs the context clauses found at [2]
5163 -- and then the context clauses of its specification (found at [1]). As
5164 -- a consequence, at [1] the specification of X has been analyzed and it
5165 -- is immediately visible. According to the semantics of limited-with
5166 -- context clauses we don't install the limited view because the full
5167 -- view of X supersedes its limited view.
5169 if Analyzed (P_Unit)
5170 and then
5171 (Is_Immediately_Visible (P)
5172 or else (Is_Child_Package and then Is_Visible_Lib_Unit (P)))
5173 then
5175 -- The presence of both the limited and the analyzed nonlimited view
5176 -- may also be an error, such as an illegal context for a limited
5177 -- with_clause. In that case, do not process the context item at all.
5179 if Error_Posted (N) then
5180 return;
5181 end if;
5183 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
5184 declare
5185 Item : Node_Id;
5186 begin
5187 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
5188 while Present (Item) loop
5189 if Nkind (Item) = N_With_Clause
5190 and then Comes_From_Source (Item)
5191 and then Entity (Name (Item)) = P
5192 then
5193 return;
5194 end if;
5196 Next (Item);
5197 end loop;
5198 end;
5200 -- If this is a child body, assume that the nonlimited with_clause
5201 -- appears in an ancestor. Could be refined ???
5203 if Is_Child_Unit
5204 (Defining_Entity
5205 (Unit (Library_Unit (Cunit (Current_Sem_Unit)))))
5206 then
5207 return;
5208 end if;
5210 else
5212 -- If in package declaration, nonlimited view brought in from
5213 -- parent unit or some error condition.
5215 return;
5216 end if;
5217 end if;
5219 if Debug_Flag_I then
5220 Write_Str ("install limited view of ");
5221 Write_Name (Chars (P));
5222 Write_Eol;
5223 end if;
5225 -- If the unit has not been analyzed and the limited view has not been
5226 -- already installed then we install it.
5228 if not Analyzed (P_Unit) then
5229 if not In_Chain (P) then
5231 -- Minimum decoration
5233 Mutate_Ekind (P, E_Package);
5234 Set_Etype (P, Standard_Void_Type);
5235 Set_Scope (P, Standard_Standard);
5236 Set_Is_Visible_Lib_Unit (P);
5238 if Is_Child_Package then
5239 Set_Is_Child_Unit (P);
5240 Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
5241 end if;
5243 -- Place entity on visibility structure
5245 Set_Homonym (P, Current_Entity (P));
5246 Set_Current_Entity (P);
5248 if Debug_Flag_I then
5249 Write_Str (" (homonym) chain ");
5250 Write_Name (Chars (P));
5251 Write_Eol;
5252 end if;
5254 -- Install the incomplete view. The first element of the limited
5255 -- view is a header (an E_Package entity) used to reference the
5256 -- first shadow entity in the private part of the package.
5258 Lim_Header := Limited_View (P);
5259 Lim_Typ := First_Entity (Lim_Header);
5261 while Present (Lim_Typ)
5262 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5263 loop
5264 Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
5265 Set_Current_Entity (Lim_Typ);
5267 if Debug_Flag_I then
5268 Write_Str (" (homonym) chain ");
5269 Write_Name (Chars (Lim_Typ));
5270 Write_Eol;
5271 end if;
5273 Next_Entity (Lim_Typ);
5274 end loop;
5275 end if;
5277 -- If the unit appears in a previous regular with_clause, the regular
5278 -- entities of the public part of the withed package must be replaced
5279 -- by the shadow ones.
5281 -- This code must be kept synchronized with the code that replaces the
5282 -- shadow entities by the real entities in Remove_Limited_With_Unit,
5283 -- otherwise the contents of the homonym chains are not consistent.
5285 else
5286 -- Hide all the type entities of the public part of the package to
5287 -- avoid its usage. This is needed to cover all the subtype decla-
5288 -- rations because we do not remove them from the homonym chain.
5290 E := First_Entity (P);
5291 while Present (E) and then E /= First_Private_Entity (P) loop
5292 if Is_Type (E) then
5293 Set_Was_Hidden (E, Is_Hidden (E));
5294 Set_Is_Hidden (E);
5295 end if;
5297 Next_Entity (E);
5298 end loop;
5300 -- Replace the real entities by the shadow entities of the limited
5301 -- view. The first element of the limited view is a header that is
5302 -- used to reference the first shadow entity in the private part
5303 -- of the package. Successive elements are the limited views of the
5304 -- type (including regular incomplete types) declared in the package.
5306 Lim_Header := Limited_View (P);
5308 Lim_Typ := First_Entity (Lim_Header);
5309 while Present (Lim_Typ)
5310 and then Lim_Typ /= First_Private_Entity (Lim_Header)
5311 loop
5312 pragma Assert (not In_Chain (Lim_Typ));
5314 -- Do not unchain nested packages and child units
5316 if Ekind (Lim_Typ) /= E_Package
5317 and then not Is_Child_Unit (Lim_Typ)
5318 then
5319 declare
5320 Typ : constant Entity_Id := Non_Limited_View (Lim_Typ);
5322 Prev : Entity_Id;
5324 begin
5325 -- Replace Typ by Lim_Typ in the homonyms list, so that the
5326 -- limited view becomes available.
5328 -- If the nonlimited view is a record with an anonymous
5329 -- self-referential component, the analysis of the record
5330 -- declaration creates an incomplete type with the same name
5331 -- in order to define an internal access type. The visible
5332 -- entity is now the incomplete type, and that is the one to
5333 -- replace in the visibility structure.
5335 -- Similarly, if the source already contains the incomplete
5336 -- type declaration, the limited view of the incomplete type
5337 -- is in fact never visible (AI05-129) but we have created a
5338 -- shadow entity E1 for it that points to E2, the incomplete
5339 -- type at stake. This in turn has full view E3 that is the
5340 -- full declaration, with a corresponding shadow entity E4.
5341 -- When reinstalling the limited view, the visible entity E2
5342 -- is first replaced with E1, but E4 must eventually become
5343 -- the visible entity as per the AI and thus displace E1, as
5344 -- it is replacing E3 in the homonyms list.
5346 -- regular views limited views
5348 -- * E2 (incomplete) <-- E1 (shadow)
5350 -- |
5351 -- V
5353 -- E3 (full) <-- E4 (shadow) *
5355 -- [*] denotes the visible entity (Current_Entity)
5357 Prev := Current_Entity (Lim_Typ);
5359 while Present (Prev) loop
5360 -- This is a regular replacement
5362 if Prev = Typ
5363 or else (Ekind (Prev) = E_Incomplete_Type
5364 and then Full_View (Prev) = Typ)
5365 then
5366 Replace (Prev, Lim_Typ);
5368 if Debug_Flag_I then
5369 Write_Str (" (homonym) replace ");
5370 Write_Name (Chars (Typ));
5371 Write_Eol;
5372 end if;
5374 exit;
5376 -- This is where E1 is replaced with E4
5378 elsif Ekind (Prev) = E_Incomplete_Type
5379 and then From_Limited_With (Prev)
5380 and then
5381 Ekind (Non_Limited_View (Prev)) = E_Incomplete_Type
5382 and then Full_View (Non_Limited_View (Prev)) = Typ
5383 then
5384 Replace (Prev, Lim_Typ);
5386 if Debug_Flag_I then
5387 Write_Str (" (homonym) E1 -> E4 ");
5388 Write_Name (Chars (Typ));
5389 Write_Eol;
5390 end if;
5392 exit;
5393 end if;
5395 Prev := Homonym (Prev);
5396 end loop;
5397 end;
5398 end if;
5400 Next_Entity (Lim_Typ);
5401 end loop;
5402 end if;
5404 -- The package must be visible while the limited-with clause is active
5405 -- because references to the type P.T must resolve in the usual way.
5406 -- In addition, we remember that the limited-view has been installed to
5407 -- uninstall it at the point of context removal.
5409 Set_Is_Immediately_Visible (P);
5410 Set_Limited_View_Installed (N);
5412 -- If unit has not been analyzed in some previous context, check
5413 -- (imperfectly ???) whether it might need a body.
5415 if not Analyzed (P_Unit) then
5416 Check_Body_Required;
5417 end if;
5419 -- If the package in the limited_with clause is a child unit, the clause
5420 -- is unanalyzed and appears as a selected component. Recast it as an
5421 -- expanded name so that the entity can be properly set. Use entity of
5422 -- parent, if available, for higher ancestors in the name.
5424 if Nkind (Name (N)) = N_Selected_Component then
5425 declare
5426 Nam : Node_Id;
5427 Ent : Entity_Id;
5429 begin
5430 Nam := Name (N);
5431 Ent := P;
5432 while Nkind (Nam) = N_Selected_Component
5433 and then Present (Ent)
5434 loop
5435 Change_Selected_Component_To_Expanded_Name (Nam);
5437 -- Set entity of parent identifiers if the unit is a child
5438 -- unit. This ensures that the tree is properly formed from
5439 -- semantic point of view. The unit entities are not fully
5440 -- analyzed, so we need to follow unit links in the tree.
5442 Set_Entity (Nam, Ent);
5444 Nam := Prefix (Nam);
5445 Ent :=
5446 Defining_Entity
5447 (Unit (Parent_Spec (Unit_Declaration_Node (Ent))));
5449 -- Set entity of last ancestor
5451 if Nkind (Nam) = N_Identifier then
5452 Set_Entity (Nam, Ent);
5453 end if;
5454 end loop;
5455 end;
5456 end if;
5458 Set_Entity (Name (N), P);
5459 Set_From_Limited_With (P);
5460 end Install_Limited_With_Clause;
5462 -------------------------
5463 -- Install_With_Clause --
5464 -------------------------
5466 procedure Install_With_Clause
5467 (With_Clause : Node_Id;
5468 Private_With_OK : Boolean := False)
5470 Uname : constant Entity_Id := Entity (Name (With_Clause));
5471 P : constant Entity_Id := Scope (Uname);
5473 begin
5474 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5475 -- compiling a package declaration and the Private_With_OK flag was not
5476 -- set by the caller. These declarations will be installed later (before
5477 -- analyzing the private part of the package).
5479 if Private_Present (With_Clause)
5480 and then Nkind (Unit (Parent (With_Clause)))
5481 in N_Package_Declaration | N_Generic_Package_Declaration
5482 and then not Private_With_OK
5483 then
5484 return;
5485 end if;
5487 if Debug_Flag_I then
5488 if Private_Present (With_Clause) then
5489 Write_Str ("install private withed unit ");
5490 elsif Parent_With (With_Clause) then
5491 Write_Str ("install parent withed unit ");
5492 elsif Implicit_With (With_Clause) then
5493 Write_Str ("install implicit withed unit ");
5494 else
5495 Write_Str ("install withed unit ");
5496 end if;
5498 Write_Name (Chars (Uname));
5499 Write_Eol;
5500 end if;
5502 -- We do not apply the restrictions to an internal unit unless we are
5503 -- compiling the internal unit as a main unit. This check is also
5504 -- skipped for dummy units (for missing packages).
5506 if Sloc (Uname) /= No_Location
5507 and then (not Is_Internal_Unit (Current_Sem_Unit)
5508 or else Current_Sem_Unit = Main_Unit)
5509 then
5510 Check_Restricted_Unit
5511 (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
5512 end if;
5514 if P /= Standard_Standard then
5516 -- If the unit is not analyzed after analysis of the with clause and
5517 -- it is an instantiation then it awaits a body and is the main unit.
5518 -- Its appearance in the context of some other unit indicates a
5519 -- circular dependency (DEC suite perversity).
5521 if not Analyzed (Uname)
5522 and then Nkind (Parent (Uname)) = N_Package_Instantiation
5523 then
5524 Error_Msg_N
5525 ("instantiation depends on itself", Name (With_Clause));
5527 elsif not Analyzed (Uname)
5528 and then Is_Internal_Unit (Current_Sem_Unit)
5529 and then not Is_Visible_Lib_Unit (Uname)
5530 and then No (Scope (Uname))
5531 then
5532 if Is_Predefined_Unit (Current_Sem_Unit) then
5533 Error_Msg_N
5534 ("predefined unit depends on itself", Name (With_Clause));
5535 else
5536 Error_Msg_N
5537 ("GNAT-defined unit depends on itself", Name (With_Clause));
5538 end if;
5539 return;
5541 elsif not Is_Visible_Lib_Unit (Uname) then
5543 -- Abandon processing in case of previous errors
5545 if No (Scope (Uname)) then
5546 Check_Error_Detected;
5547 return;
5548 end if;
5550 Set_Is_Visible_Lib_Unit (Uname);
5552 -- If the unit is a wrapper package for a compilation unit that is
5553 -- a subprogram instance, indicate that the instance itself is a
5554 -- visible unit. This is necessary if the instance is inlined.
5556 if Is_Wrapper_Package (Uname) then
5557 Set_Is_Visible_Lib_Unit (Related_Instance (Uname));
5558 end if;
5560 -- If the child unit appears in the context of its parent, it is
5561 -- immediately visible.
5563 if In_Open_Scopes (Scope (Uname)) then
5564 Set_Is_Immediately_Visible (Uname);
5565 end if;
5567 if Is_Generic_Instance (Uname)
5568 and then Is_Subprogram (Uname)
5569 then
5570 -- Set flag as well on the visible entity that denotes the
5571 -- instance, which renames the current one.
5573 Set_Is_Visible_Lib_Unit
5574 (Related_Instance
5575 (Defining_Entity (Unit (Library_Unit (With_Clause)))));
5576 end if;
5578 -- The parent unit may have been installed already, and may have
5579 -- appeared in a use clause.
5581 if In_Use (Scope (Uname)) then
5582 Set_Is_Potentially_Use_Visible (Uname);
5583 end if;
5585 Set_Context_Installed (With_Clause);
5586 end if;
5588 elsif not Is_Immediately_Visible (Uname) then
5589 Set_Is_Visible_Lib_Unit (Uname);
5591 if not Private_Present (With_Clause) or else Private_With_OK then
5592 Set_Is_Immediately_Visible (Uname);
5593 end if;
5595 Set_Context_Installed (With_Clause);
5596 end if;
5598 -- A [private] with clause overrides a limited with clause. Restore the
5599 -- proper view of the package by performing the following actions:
5601 -- * Remove all shadow entities which hide their corresponding
5602 -- entities from direct visibility by updating the entity and
5603 -- homonym chains.
5605 -- * Enter the corresponding entities back in direct visibility
5607 -- Note that the original limited with clause which installed its view
5608 -- is still marked as "active". This effect is undone when the clause
5609 -- itself is removed, see Remove_Limited_With_Clause.
5611 if Ekind (Uname) = E_Package and then From_Limited_With (Uname) then
5612 Remove_Limited_With_Unit (Unit_Declaration_Node (Uname));
5613 end if;
5615 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5616 -- unit if there is a visible homograph for it declared in the same
5617 -- declarative region. This pathological case can only arise when an
5618 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5619 -- G1 has a generic child also named G2, and the context includes with_
5620 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5621 -- of I1.G2 visible as well. If the child unit is named Standard, do
5622 -- not apply the check to the Standard package itself.
5624 if Is_Child_Unit (Uname)
5625 and then Is_Visible_Lib_Unit (Uname)
5626 and then Ada_Version >= Ada_2005
5627 then
5628 declare
5629 Decl1 : constant Node_Id := Unit_Declaration_Node (P);
5630 Decl2 : Node_Id;
5631 P2 : Entity_Id;
5632 U2 : Entity_Id;
5634 begin
5635 U2 := Homonym (Uname);
5636 while Present (U2) and then U2 /= Standard_Standard loop
5637 P2 := Scope (U2);
5638 Decl2 := Unit_Declaration_Node (P2);
5640 if Is_Child_Unit (U2) and then Is_Visible_Lib_Unit (U2) then
5641 if Is_Generic_Instance (P)
5642 and then Nkind (Decl1) = N_Package_Declaration
5643 and then Generic_Parent (Specification (Decl1)) = P2
5644 then
5645 Error_Msg_N ("illegal with_clause", With_Clause);
5646 Error_Msg_N
5647 ("\child unit has visible homograph" &
5648 " (RM 8.3(26), 10.1.1(19))",
5649 With_Clause);
5650 exit;
5652 elsif Is_Generic_Instance (P2)
5653 and then Nkind (Decl2) = N_Package_Declaration
5654 and then Generic_Parent (Specification (Decl2)) = P
5655 then
5656 -- With_clause for child unit of instance appears before
5657 -- in the context. We want to place the error message on
5658 -- it, not on the generic child unit itself.
5660 declare
5661 Prev_Clause : Node_Id;
5663 begin
5664 Prev_Clause := First (List_Containing (With_Clause));
5665 while Entity (Name (Prev_Clause)) /= U2 loop
5666 Next (Prev_Clause);
5667 end loop;
5669 pragma Assert (Present (Prev_Clause));
5670 Error_Msg_N ("illegal with_clause", Prev_Clause);
5671 Error_Msg_N
5672 ("\child unit has visible homograph" &
5673 " (RM 8.3(26), 10.1.1(19))",
5674 Prev_Clause);
5675 exit;
5676 end;
5677 end if;
5678 end if;
5680 U2 := Homonym (U2);
5681 end loop;
5682 end;
5683 end if;
5684 end Install_With_Clause;
5686 -------------------
5687 -- Is_Child_Spec --
5688 -------------------
5690 function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
5691 K : constant Node_Kind := Nkind (Lib_Unit);
5693 begin
5694 return (K in N_Generic_Declaration or else
5695 K in N_Generic_Instantiation or else
5696 K in N_Generic_Renaming_Declaration or else
5697 K = N_Package_Declaration or else
5698 K = N_Package_Renaming_Declaration or else
5699 K = N_Subprogram_Declaration or else
5700 K = N_Subprogram_Renaming_Declaration)
5701 and then Present (Parent_Spec (Lib_Unit));
5702 end Is_Child_Spec;
5704 ------------------------------------
5705 -- Is_Legal_Shadow_Entity_In_Body --
5706 ------------------------------------
5708 function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
5709 C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5710 begin
5711 return Nkind (Unit (C_Unit)) = N_Package_Body
5712 and then
5713 Has_With_Clause
5714 (C_Unit, Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
5715 end Is_Legal_Shadow_Entity_In_Body;
5717 ----------------------
5718 -- Is_Ancestor_Unit --
5719 ----------------------
5721 function Is_Ancestor_Unit (U1 : Node_Id; U2 : Node_Id) return Boolean is
5722 E1 : constant Entity_Id := Defining_Entity (Unit (U1));
5723 E2 : Entity_Id;
5724 begin
5725 if Nkind (Unit (U2)) in N_Package_Body | N_Subprogram_Body then
5726 E2 := Defining_Entity (Unit (Library_Unit (U2)));
5727 return Is_Ancestor_Package (E1, E2);
5728 else
5729 return False;
5730 end if;
5731 end Is_Ancestor_Unit;
5733 ----------------------------------
5734 -- Is_Visible_Through_Renamings --
5735 ----------------------------------
5737 function Is_Visible_Through_Renamings
5738 (P : Entity_Id;
5739 Error_Node : Node_Id := Empty) return Boolean
5741 function Is_Limited_Withed_Unit
5742 (Lib_Unit : Node_Id;
5743 Pkg_Ent : Entity_Id) return Boolean;
5744 -- Return True if Pkg_Ent is a limited-withed package of the given
5745 -- library unit.
5747 ----------------------------
5748 -- Is_Limited_Withed_Unit --
5749 ----------------------------
5751 function Is_Limited_Withed_Unit
5752 (Lib_Unit : Node_Id;
5753 Pkg_Ent : Entity_Id) return Boolean
5755 Item : Node_Id := First (Context_Items (Lib_Unit));
5757 begin
5758 while Present (Item) loop
5759 if Nkind (Item) = N_With_Clause
5760 and then Limited_Present (Item)
5761 and then Entity (Name (Item)) = Pkg_Ent
5762 then
5763 return True;
5764 end if;
5766 Next (Item);
5767 end loop;
5769 return False;
5770 end Is_Limited_Withed_Unit;
5772 -- Local variables
5774 Kind : constant Node_Kind := Nkind (Unit (Cunit (Current_Sem_Unit)));
5775 Aux_Unit : Node_Id;
5776 Item : Node_Id;
5777 Decl : Entity_Id;
5779 begin
5780 -- Example of the error detected by this subprogram:
5782 -- package P is
5783 -- type T is ...
5784 -- end P;
5786 -- with P;
5787 -- package Q is
5788 -- package Ren_P renames P;
5789 -- end Q;
5791 -- with Q;
5792 -- package R is ...
5794 -- limited with P; -- ERROR
5795 -- package R.C is ...
5797 Aux_Unit := Cunit (Current_Sem_Unit);
5799 loop
5800 Item := First (Context_Items (Aux_Unit));
5801 while Present (Item) loop
5802 if Nkind (Item) = N_With_Clause
5803 and then not Limited_Present (Item)
5804 and then Nkind (Unit (Library_Unit (Item))) =
5805 N_Package_Declaration
5806 then
5807 Decl :=
5808 First (Visible_Declarations
5809 (Specification (Unit (Library_Unit (Item)))));
5810 while Present (Decl) loop
5811 if Nkind (Decl) = N_Package_Renaming_Declaration
5812 and then Entity (Name (Decl)) = P
5813 and then not Is_Limited_Withed_Unit
5814 (Lib_Unit => Library_Unit (Item),
5815 Pkg_Ent => Entity (Name (Decl)))
5816 then
5817 -- Generate the error message only if the current unit
5818 -- is a package declaration; in case of subprogram
5819 -- bodies and package bodies we just return True to
5820 -- indicate that the limited view must not be
5821 -- installed.
5823 if Kind = N_Package_Declaration
5824 and then Present (Error_Node)
5825 then
5826 Error_Msg_N
5827 ("simultaneous visibility of the limited and " &
5828 "unlimited views not allowed", Error_Node);
5829 Error_Msg_Sloc := Sloc (Item);
5830 Error_Msg_NE
5831 ("\\ unlimited view of & visible through the " &
5832 "context clause #", Error_Node, P);
5833 Error_Msg_Sloc := Sloc (Decl);
5834 Error_Msg_NE ("\\ and the renaming #", Error_Node, P);
5835 end if;
5837 return True;
5838 end if;
5840 Next (Decl);
5841 end loop;
5842 end if;
5844 Next (Item);
5845 end loop;
5847 -- If it is a body not acting as spec, follow pointer to the
5848 -- corresponding spec, otherwise follow pointer to parent spec.
5850 if Present (Library_Unit (Aux_Unit))
5851 and then Nkind (Unit (Aux_Unit)) in
5852 N_Package_Body | N_Subprogram_Body
5853 then
5854 if Aux_Unit = Library_Unit (Aux_Unit) then
5856 -- Aux_Unit is a body that acts as a spec. Clause has
5857 -- already been flagged as illegal.
5859 return False;
5861 else
5862 Aux_Unit := Library_Unit (Aux_Unit);
5863 end if;
5865 else
5866 Aux_Unit := Parent_Spec (Unit (Aux_Unit));
5867 end if;
5869 exit when No (Aux_Unit);
5870 end loop;
5872 return False;
5873 end Is_Visible_Through_Renamings;
5875 -----------------------
5876 -- Load_Needed_Body --
5877 -----------------------
5879 -- N is a generic unit named in a with clause, or else it is a unit that
5880 -- contains a generic unit or an inlined function. In order to perform an
5881 -- instantiation, the body of the unit must be present. If the unit itself
5882 -- is generic, we assume that an instantiation follows, and load & analyze
5883 -- the body unconditionally. This forces analysis of the spec as well.
5885 -- If the unit is not generic, but contains a generic unit, it is loaded on
5886 -- demand, at the point of instantiation (see ch12).
5888 procedure Load_Needed_Body
5889 (N : Node_Id;
5890 OK : out Boolean)
5892 Body_Name : Unit_Name_Type;
5893 Unum : Unit_Number_Type;
5895 Save_Style_Check : constant Boolean := Opt.Style_Check;
5896 -- The loading and analysis is done with style checks off
5898 begin
5899 if not GNAT_Mode then
5900 Style_Check := False;
5901 end if;
5903 Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
5904 Unum :=
5905 Load_Unit
5906 (Load_Name => Body_Name,
5907 Required => False,
5908 Subunit => False,
5909 Error_Node => N,
5910 Renamings => True);
5912 if Unum = No_Unit then
5913 OK := False;
5915 else
5916 Compiler_State := Analyzing; -- reset after load
5918 if Fatal_Error (Unum) /= Error_Detected or else Try_Semantics then
5919 if Debug_Flag_L then
5920 Write_Str ("*** Loaded generic body");
5921 Write_Eol;
5922 end if;
5924 -- We always perform analyses
5925 Semantics (Cunit (Unum));
5926 end if;
5928 OK := True;
5929 end if;
5931 Style_Check := Save_Style_Check;
5932 end Load_Needed_Body;
5934 -------------------------
5935 -- Build_Limited_Views --
5936 -------------------------
5938 procedure Build_Limited_Views (N : Node_Id) is
5939 Unum : constant Unit_Number_Type :=
5940 Get_Source_Unit (Library_Unit (N));
5941 Is_Analyzed : constant Boolean := Analyzed (Cunit (Unum));
5943 Shadow_Pack : Entity_Id;
5944 -- The corresponding shadow entity of the withed package. This entity
5945 -- offers incomplete views of packages and types as well as abstract
5946 -- views of states and variables declared within.
5948 Last_Shadow : Entity_Id := Empty;
5949 -- The last shadow entity created by routine Build_Shadow_Entity
5951 procedure Build_Shadow_Entity
5952 (Ent : Entity_Id;
5953 Scop : Entity_Id;
5954 Shadow : out Entity_Id;
5955 Is_Tagged : Boolean := False);
5956 -- Create a shadow entity that hides Ent and offers an abstract or
5957 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
5958 -- should be set when Ent is a tagged type. The generated entity is
5959 -- added to Shadow_Pack. The routine updates the value of Last_Shadow.
5961 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id);
5962 -- Perform minimal decoration of a package or its corresponding shadow
5963 -- entity denoted by Ent. Scop is the proper scope.
5965 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id);
5966 -- Perform full decoration of an abstract state or its corresponding
5967 -- shadow entity denoted by Ent. Scop is the proper scope.
5969 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id);
5970 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
5971 -- proper scope.
5973 procedure Process_Declarations_And_States
5974 (Pack : Entity_Id;
5975 Decls : List_Id;
5976 Scop : Entity_Id;
5977 Create_Abstract_Views : Boolean);
5978 -- Inspect the states of package Pack and declarative list Decls. Create
5979 -- shadow entities for all nested packages, states, types and variables
5980 -- encountered. Scop is the proper scope. Create_Abstract_Views should
5981 -- be set when the abstract states and variables need to be processed.
5983 -------------------------
5984 -- Build_Shadow_Entity --
5985 -------------------------
5987 procedure Build_Shadow_Entity
5988 (Ent : Entity_Id;
5989 Scop : Entity_Id;
5990 Shadow : out Entity_Id;
5991 Is_Tagged : Boolean := False)
5993 begin
5994 Shadow := Make_Temporary (Sloc (Ent), 'Z');
5996 -- The shadow entity must share the same name and parent as the
5997 -- entity it hides.
5999 Set_Chars (Shadow, Chars (Ent));
6000 Set_Parent (Shadow, Parent (Ent));
6002 -- The abstract view of a variable is a state, not another variable
6004 if Ekind (Ent) = E_Variable then
6005 Mutate_Ekind (Shadow, E_Abstract_State);
6006 else
6007 Mutate_Ekind (Shadow, Ekind (Ent));
6008 end if;
6010 Set_Is_Not_Self_Hidden (Shadow);
6011 Set_Is_Internal (Shadow);
6012 Set_From_Limited_With (Shadow);
6014 -- Add the new shadow entity to the limited view of the package
6016 Last_Shadow := Shadow;
6017 Append_Entity (Shadow, Shadow_Pack);
6019 -- Perform context-specific decoration of the shadow entity
6021 if Ekind (Ent) = E_Abstract_State then
6022 Decorate_State (Shadow, Scop);
6023 Set_Non_Limited_View (Shadow, Ent);
6025 elsif Ekind (Ent) = E_Package then
6026 Decorate_Package (Shadow, Scop);
6028 elsif Is_Type (Ent) then
6029 Decorate_Type (Shadow, Scop, Is_Tagged);
6031 -- If Ent is a private type and we are analyzing the body of its
6032 -- scope, its private and full views are swapped and, therefore,
6033 -- we need to undo this swapping in order to build the same shadow
6034 -- entity as we would have in other contexts.
6036 if Is_Private_Type (Ent)
6037 and then Present (Full_View (Ent))
6038 and then In_Package_Body (Scop)
6039 then
6040 Set_Non_Limited_View (Shadow, Full_View (Ent));
6041 else
6042 Set_Non_Limited_View (Shadow, Ent);
6043 end if;
6045 if Is_Tagged then
6046 Set_Non_Limited_View
6047 (Class_Wide_Type (Shadow), Class_Wide_Type (Ent));
6048 end if;
6050 if Is_Incomplete_Or_Private_Type (Ent) then
6051 Set_Private_Dependents (Shadow, New_Elmt_List);
6052 end if;
6054 elsif Ekind (Ent) = E_Variable then
6055 Decorate_State (Shadow, Scop);
6056 Set_Non_Limited_View (Shadow, Ent);
6057 end if;
6058 end Build_Shadow_Entity;
6060 ----------------------
6061 -- Decorate_Package --
6062 ----------------------
6064 procedure Decorate_Package (Ent : Entity_Id; Scop : Entity_Id) is
6065 begin
6066 Mutate_Ekind (Ent, E_Package);
6067 Set_Etype (Ent, Standard_Void_Type);
6068 Set_Scope (Ent, Scop);
6069 end Decorate_Package;
6071 --------------------
6072 -- Decorate_State --
6073 --------------------
6075 procedure Decorate_State (Ent : Entity_Id; Scop : Entity_Id) is
6076 begin
6077 Mutate_Ekind (Ent, E_Abstract_State);
6078 Set_Is_Not_Self_Hidden (Ent);
6079 Set_Etype (Ent, Standard_Void_Type);
6080 Set_Scope (Ent, Scop);
6081 Set_Encapsulating_State (Ent, Empty);
6082 end Decorate_State;
6084 -----------------------
6085 -- Decorate_Variable --
6086 -----------------------
6088 procedure Decorate_Variable (Ent : Entity_Id; Scop : Entity_Id) is
6089 begin
6090 Mutate_Ekind (Ent, E_Variable);
6091 Set_Etype (Ent, Standard_Void_Type);
6092 Set_Scope (Ent, Scop);
6093 end Decorate_Variable;
6095 -------------------------------------
6096 -- Process_Declarations_And_States --
6097 -------------------------------------
6099 procedure Process_Declarations_And_States
6100 (Pack : Entity_Id;
6101 Decls : List_Id;
6102 Scop : Entity_Id;
6103 Create_Abstract_Views : Boolean)
6105 procedure Find_And_Process_States;
6106 -- Determine whether package Pack defines abstract state either by
6107 -- using an aspect or a pragma. If this is the case, build shadow
6108 -- entities for all abstract states of Pack.
6110 procedure Process_States (States : Elist_Id);
6111 -- Generate shadow entities for all abstract states in list States
6113 -----------------------------
6114 -- Find_And_Process_States --
6115 -----------------------------
6117 procedure Find_And_Process_States is
6118 procedure Process_State (State : Node_Id);
6119 -- Generate shadow entities for a single abstract state or
6120 -- multiple states expressed as an aggregate.
6122 -------------------
6123 -- Process_State --
6124 -------------------
6126 procedure Process_State (State : Node_Id) is
6127 Loc : constant Source_Ptr := Sloc (State);
6128 Decl : Node_Id;
6129 Dummy : Entity_Id;
6130 Elmt : Node_Id;
6131 Id : Entity_Id;
6133 begin
6134 -- Multiple abstract states appear as an aggregate
6136 if Nkind (State) = N_Aggregate then
6137 Elmt := First (Expressions (State));
6138 while Present (Elmt) loop
6139 Process_State (Elmt);
6140 Next (Elmt);
6141 end loop;
6143 return;
6145 -- A null state has no abstract view
6147 elsif Nkind (State) = N_Null then
6148 return;
6150 -- State declaration with various options appears as an
6151 -- extension aggregate.
6153 elsif Nkind (State) = N_Extension_Aggregate then
6154 Decl := Ancestor_Part (State);
6156 -- Simple state declaration
6158 elsif Nkind (State) = N_Identifier then
6159 Decl := State;
6161 -- Possibly an illegal state declaration
6163 else
6164 return;
6165 end if;
6167 -- Abstract states are elaborated when the related pragma is
6168 -- elaborated. Since the withed package is not analyzed yet,
6169 -- the entities of the abstract states are not available. To
6170 -- overcome this complication, create the entities now and
6171 -- store them in their respective declarations. The entities
6172 -- are later used by routine Create_Abstract_State to declare
6173 -- and enter the states into visibility.
6175 if No (Entity (Decl)) then
6176 Id := Make_Defining_Identifier (Loc, Chars (Decl));
6178 Set_Entity (Decl, Id);
6179 Set_Parent (Id, State);
6180 Decorate_State (Id, Scop);
6182 -- Otherwise the package was previously withed
6184 else
6185 Id := Entity (Decl);
6186 end if;
6188 Build_Shadow_Entity (Id, Scop, Dummy);
6189 end Process_State;
6191 -- Local variables
6193 Pack_Decl : constant Node_Id := Unit_Declaration_Node (Pack);
6194 Asp : Node_Id;
6195 Decl : Node_Id;
6197 -- Start of processing for Find_And_Process_States
6199 begin
6200 -- Find aspect Abstract_State
6202 Asp := First (Aspect_Specifications (Pack_Decl));
6203 while Present (Asp) loop
6204 if Chars (Identifier (Asp)) = Name_Abstract_State then
6205 Process_State (Expression (Asp));
6207 return;
6208 end if;
6210 Next (Asp);
6211 end loop;
6213 -- Find pragma Abstract_State by inspecting the declarations
6215 Decl := First (Decls);
6216 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
6217 if Pragma_Name (Decl) = Name_Abstract_State then
6218 Process_State
6219 (Get_Pragma_Arg
6220 (First (Pragma_Argument_Associations (Decl))));
6222 return;
6223 end if;
6225 Next (Decl);
6226 end loop;
6227 end Find_And_Process_States;
6229 --------------------
6230 -- Process_States --
6231 --------------------
6233 procedure Process_States (States : Elist_Id) is
6234 Dummy : Entity_Id;
6235 Elmt : Elmt_Id;
6237 begin
6238 Elmt := First_Elmt (States);
6239 while Present (Elmt) loop
6240 Build_Shadow_Entity (Node (Elmt), Scop, Dummy);
6242 Next_Elmt (Elmt);
6243 end loop;
6244 end Process_States;
6246 -- Local variables
6248 Is_Tagged : Boolean;
6249 Decl : Node_Id;
6250 Def : Node_Id;
6251 Def_Id : Entity_Id;
6252 Shadow : Entity_Id;
6254 -- Start of processing for Process_Declarations_And_States
6256 begin
6257 -- Build abstract views for all states defined in the package
6259 if Create_Abstract_Views then
6261 -- When a package has been analyzed, all states are stored in list
6262 -- Abstract_States. Generate the shadow entities directly.
6264 if Is_Analyzed then
6265 if Present (Abstract_States (Pack)) then
6266 Process_States (Abstract_States (Pack));
6267 end if;
6269 -- The package may declare abstract states by using an aspect or a
6270 -- pragma. Attempt to locate one of these construct and if found,
6271 -- build the shadow entities.
6273 else
6274 Find_And_Process_States;
6275 end if;
6276 end if;
6278 -- Inspect the declarative list, looking for nested packages, types
6279 -- and variable declarations.
6281 Decl := First (Decls);
6282 while Present (Decl) loop
6284 -- Packages
6286 if Nkind (Decl) = N_Package_Declaration then
6287 Def_Id := Defining_Entity (Decl);
6289 -- Perform minor decoration when the withed package has not
6290 -- been analyzed.
6292 if not Is_Analyzed then
6293 Decorate_Package (Def_Id, Scop);
6294 end if;
6296 -- Create a shadow entity that offers a limited view of all
6297 -- visible types declared within.
6299 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6301 Process_Declarations_And_States
6302 (Pack => Def_Id,
6303 Decls =>
6304 Visible_Declarations (Specification (Decl)),
6305 Scop => Shadow,
6306 Create_Abstract_Views => Create_Abstract_Views);
6308 -- Types
6310 elsif Nkind (Decl) in N_Full_Type_Declaration
6311 | N_Incomplete_Type_Declaration
6312 | N_Private_Extension_Declaration
6313 | N_Private_Type_Declaration
6314 | N_Protected_Type_Declaration
6315 | N_Task_Type_Declaration
6316 then
6317 Def_Id := Defining_Entity (Decl);
6319 -- Determine whether the type is tagged. Note that packages
6320 -- included via a limited with clause are not always analyzed,
6321 -- hence the tree lookup rather than the use of attribute
6322 -- Is_Tagged_Type.
6324 if Nkind (Decl) = N_Full_Type_Declaration then
6325 Def := Type_Definition (Decl);
6327 Is_Tagged :=
6328 (Nkind (Def) = N_Record_Definition
6329 and then Tagged_Present (Def))
6330 or else
6331 (Nkind (Def) = N_Derived_Type_Definition
6332 and then Present (Record_Extension_Part (Def)));
6334 elsif Nkind (Decl) in N_Incomplete_Type_Declaration
6335 | N_Private_Type_Declaration
6336 then
6337 Is_Tagged := Tagged_Present (Decl);
6339 elsif Nkind (Decl) = N_Private_Extension_Declaration then
6340 Is_Tagged := True;
6342 else
6343 Is_Tagged := False;
6344 end if;
6346 -- Perform minor decoration when the withed package has not
6347 -- been analyzed.
6349 if not Is_Analyzed then
6350 Decorate_Type (Def_Id, Scop, Is_Tagged, True);
6351 end if;
6353 -- Create a shadow entity that hides the type and offers an
6354 -- incomplete view of the said type.
6356 Build_Shadow_Entity (Def_Id, Scop, Shadow, Is_Tagged);
6358 -- Variables
6360 elsif Create_Abstract_Views
6361 and then Nkind (Decl) = N_Object_Declaration
6362 and then not Constant_Present (Decl)
6363 then
6364 Def_Id := Defining_Entity (Decl);
6366 -- Perform minor decoration when the withed package has not
6367 -- been analyzed.
6369 if not Is_Analyzed then
6370 Decorate_Variable (Def_Id, Scop);
6371 end if;
6373 -- Create a shadow entity that hides the variable and offers an
6374 -- abstract view of the said variable.
6376 Build_Shadow_Entity (Def_Id, Scop, Shadow);
6377 end if;
6379 Next (Decl);
6380 end loop;
6381 end Process_Declarations_And_States;
6383 -- Local variables
6385 Nam : constant Node_Id := Name (N);
6386 Pack : constant Entity_Id := Cunit_Entity (Unum);
6388 Last_Public_Shadow : Entity_Id := Empty;
6389 Private_Shadow : Entity_Id;
6390 Spec : Node_Id;
6392 -- Start of processing for Build_Limited_Views
6394 begin
6395 pragma Assert (Limited_Present (N));
6397 -- A library_item mentioned in a limited_with_clause is a package
6398 -- declaration, not a subprogram declaration, generic declaration,
6399 -- generic instantiation, or package renaming declaration.
6401 case Nkind (Unit (Library_Unit (N))) is
6402 when N_Package_Declaration =>
6403 null;
6405 when N_Subprogram_Declaration =>
6406 Error_Msg_N
6407 ("subprogram not allowed in `LIMITED WITH` clause", N);
6408 return;
6410 when N_Generic_Declaration =>
6411 Error_Msg_N ("generic not allowed in `LIMITED WITH` clause", N);
6412 return;
6414 when N_Generic_Instantiation =>
6415 Error_Msg_N
6416 ("generic instantiation not allowed in `LIMITED WITH` clause",
6418 return;
6420 when N_Generic_Renaming_Declaration =>
6421 Error_Msg_N
6422 ("generic renaming not allowed in `LIMITED WITH` clause", N);
6423 return;
6425 when N_Subprogram_Renaming_Declaration =>
6426 Error_Msg_N
6427 ("renamed subprogram not allowed in `LIMITED WITH` clause", N);
6428 return;
6430 when N_Package_Renaming_Declaration =>
6431 Error_Msg_N
6432 ("renamed package not allowed in `LIMITED WITH` clause", N);
6433 return;
6435 when others =>
6436 raise Program_Error;
6437 end case;
6439 -- The withed unit may not be analyzed, but the with clause itself
6440 -- must be minimally decorated. This ensures that the checks on unused
6441 -- with clauses also process limieted withs.
6443 Mutate_Ekind (Pack, E_Package);
6444 Set_Is_Not_Self_Hidden (Pack);
6445 Set_Etype (Pack, Standard_Void_Type);
6447 if Is_Entity_Name (Nam) then
6448 Set_Entity (Nam, Pack);
6450 elsif Nkind (Nam) = N_Selected_Component then
6451 Set_Entity (Selector_Name (Nam), Pack);
6452 end if;
6454 -- Check if the chain is already built
6456 Spec := Specification (Unit (Library_Unit (N)));
6458 if Limited_View_Installed (Spec) then
6459 return;
6460 end if;
6462 -- Create the shadow package wich hides the withed unit and provides
6463 -- incomplete view of all types and packages declared within.
6465 Shadow_Pack := Make_Temporary (Sloc (N), 'Z');
6466 Mutate_Ekind (Shadow_Pack, E_Package);
6467 Set_Is_Internal (Shadow_Pack);
6468 Set_Limited_View (Pack, Shadow_Pack);
6470 -- Inspect the abstract states and visible declarations of the withed
6471 -- unit and create shadow entities that hide existing packages, states,
6472 -- variables and types.
6474 Process_Declarations_And_States
6475 (Pack => Pack,
6476 Decls => Visible_Declarations (Spec),
6477 Scop => Pack,
6478 Create_Abstract_Views => True);
6480 Last_Public_Shadow := Last_Shadow;
6482 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6483 -- to accommodate limited private with clauses.
6485 Process_Declarations_And_States
6486 (Pack => Pack,
6487 Decls => Private_Declarations (Spec),
6488 Scop => Pack,
6489 Create_Abstract_Views => False);
6491 if Present (Last_Public_Shadow) then
6492 Private_Shadow := Next_Entity (Last_Public_Shadow);
6493 else
6494 Private_Shadow := First_Entity (Shadow_Pack);
6495 end if;
6497 Set_First_Private_Entity (Shadow_Pack, Private_Shadow);
6498 Set_Limited_View_Installed (Spec);
6499 end Build_Limited_Views;
6501 ----------------------------
6502 -- Check_No_Elab_Code_All --
6503 ----------------------------
6505 procedure Check_No_Elab_Code_All (N : Node_Id) is
6506 begin
6507 if Present (No_Elab_Code_All_Pragma)
6508 and then In_Extended_Main_Source_Unit (N)
6509 and then Present (Context_Items (N))
6510 then
6511 declare
6512 CL : constant List_Id := Context_Items (N);
6513 CI : Node_Id;
6515 begin
6516 CI := First (CL);
6517 while Present (CI) loop
6518 if Nkind (CI) = N_With_Clause
6519 and then not
6520 No_Elab_Code_All (Get_Source_Unit (Library_Unit (CI)))
6522 -- In GNATprove mode, some runtime units are implicitly
6523 -- loaded to make their entities available for analysis. In
6524 -- this case, ignore violations of No_Elaboration_Code_All
6525 -- for this special analysis mode.
6527 and then not
6528 (GNATprove_Mode and then Implicit_With (CI))
6529 then
6530 Error_Msg_Sloc := Sloc (No_Elab_Code_All_Pragma);
6531 Error_Msg_N
6532 ("violation of No_Elaboration_Code_All#", CI);
6533 Error_Msg_NE
6534 ("\unit& does not have No_Elaboration_Code_All",
6535 CI, Entity (Name (CI)));
6536 end if;
6538 Next (CI);
6539 end loop;
6540 end;
6541 end if;
6542 end Check_No_Elab_Code_All;
6544 -------------------------------
6545 -- Check_Body_Needed_For_SAL --
6546 -------------------------------
6548 procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
6549 function Entity_Needs_Body (E : Entity_Id) return Boolean;
6550 -- Determine whether use of entity E might require the presence of its
6551 -- body. For a package this requires a recursive traversal of all nested
6552 -- declarations.
6554 -----------------------
6555 -- Entity_Needs_Body --
6556 -----------------------
6558 function Entity_Needs_Body (E : Entity_Id) return Boolean is
6559 Ent : Entity_Id;
6561 begin
6562 if Is_Subprogram (E) and then Has_Pragma_Inline (E) then
6563 return True;
6565 elsif Is_Generic_Subprogram (E) then
6567 -- A generic subprogram always requires the presence of its
6568 -- body because an instantiation needs both templates. The only
6569 -- exceptions is a generic subprogram renaming. In this case the
6570 -- body is needed only when the template is declared outside the
6571 -- compilation unit being checked.
6573 if Present (Renamed_Entity (E)) then
6574 return not Within_Scope (E, Unit_Name);
6575 else
6576 return True;
6577 end if;
6579 elsif Ekind (E) = E_Generic_Package
6580 and then
6581 Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
6582 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6583 then
6584 return True;
6586 elsif Ekind (E) = E_Package
6587 and then Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
6588 and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
6589 then
6590 Ent := First_Entity (E);
6591 while Present (Ent) loop
6592 if Entity_Needs_Body (Ent) then
6593 return True;
6594 end if;
6596 Next_Entity (Ent);
6597 end loop;
6599 return False;
6601 else
6602 return False;
6603 end if;
6604 end Entity_Needs_Body;
6606 -- Start of processing for Check_Body_Needed_For_SAL
6608 begin
6609 if Ekind (Unit_Name) = E_Generic_Package
6610 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6611 N_Generic_Package_Declaration
6612 and then
6613 Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
6614 then
6615 Set_Body_Needed_For_SAL (Unit_Name);
6617 elsif Ekind (Unit_Name) in E_Generic_Procedure | E_Generic_Function then
6618 Set_Body_Needed_For_SAL (Unit_Name);
6620 elsif Is_Subprogram (Unit_Name)
6621 and then Nkind (Unit_Declaration_Node (Unit_Name)) =
6622 N_Subprogram_Declaration
6623 and then Has_Pragma_Inline (Unit_Name)
6624 then
6625 Set_Body_Needed_For_SAL (Unit_Name);
6627 elsif Ekind (Unit_Name) = E_Subprogram_Body then
6628 Check_Body_Needed_For_SAL
6629 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6631 elsif Ekind (Unit_Name) = E_Package
6632 and then Entity_Needs_Body (Unit_Name)
6633 then
6634 Set_Body_Needed_For_SAL (Unit_Name);
6636 elsif Ekind (Unit_Name) = E_Package_Body
6637 and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
6638 then
6639 Check_Body_Needed_For_SAL
6640 (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
6641 end if;
6642 end Check_Body_Needed_For_SAL;
6644 --------------------
6645 -- Remove_Context --
6646 --------------------
6648 procedure Remove_Context (N : Node_Id) is
6649 Lib_Unit : constant Node_Id := Unit (N);
6651 begin
6652 -- If this is a child unit, first remove the parent units
6654 if Is_Child_Spec (Lib_Unit) then
6655 Remove_Parents (Lib_Unit);
6656 end if;
6658 Remove_Context_Clauses (N);
6659 end Remove_Context;
6661 ----------------------------
6662 -- Remove_Context_Clauses --
6663 ----------------------------
6665 procedure Remove_Context_Clauses (N : Node_Id) is
6666 Item : Node_Id;
6667 Unit_Name : Entity_Id;
6669 begin
6670 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6671 -- limited-views first and regular-views later (to maintain the stack
6672 -- model).
6674 -- First Phase: Remove limited_with context clauses
6676 Item := First (Context_Items (N));
6677 while Present (Item) loop
6679 -- We are interested only in with clauses that got installed on entry
6681 if Nkind (Item) = N_With_Clause
6682 and then Limited_Present (Item)
6683 then
6684 if Limited_View_Installed (Item) then
6685 Remove_Limited_With_Clause (Item);
6687 -- An unusual case: If the library unit of the Main_Unit has a
6688 -- limited with_clause on some unit P and the context somewhere
6689 -- includes a with_clause on P, P has been analyzed. The entity
6690 -- for P is still visible, which in general is harmless because
6691 -- this is the end of the compilation, but it can affect pending
6692 -- instantiations that may have been generated elsewhere, so it
6693 -- it is necessary to remove U from visibility so that inlining
6694 -- and the analysis of instance bodies can proceed cleanly.
6696 elsif Current_Sem_Unit = Main_Unit
6697 and then Serious_Errors_Detected = 0
6698 and then not Implicit_With (Item)
6699 then
6700 Set_Is_Immediately_Visible
6701 (Defining_Entity (Unit (Library_Unit (Item))), False);
6702 end if;
6703 end if;
6705 Next (Item);
6706 end loop;
6708 -- Second Phase: Loop through context items and undo regular
6709 -- with_clauses and use_clauses.
6711 Item := First (Context_Items (N));
6712 while Present (Item) loop
6714 -- We are interested only in with clauses which got installed on
6715 -- entry, as indicated by their Context_Installed flag set
6717 if Nkind (Item) = N_With_Clause
6718 and then Limited_Present (Item)
6719 and then Limited_View_Installed (Item)
6720 then
6721 null;
6723 elsif Nkind (Item) = N_With_Clause
6724 and then Context_Installed (Item)
6725 then
6726 -- Remove items from one with'ed unit
6728 Unit_Name := Entity (Name (Item));
6729 Remove_Unit_From_Visibility (Unit_Name);
6730 Set_Context_Installed (Item, False);
6732 elsif Nkind (Item) = N_Use_Package_Clause then
6733 End_Use_Package (Item);
6735 elsif Nkind (Item) = N_Use_Type_Clause then
6736 End_Use_Type (Item);
6737 end if;
6739 Next (Item);
6740 end loop;
6741 end Remove_Context_Clauses;
6743 --------------------------------
6744 -- Remove_Limited_With_Clause --
6745 --------------------------------
6747 procedure Remove_Limited_With_Clause (N : Node_Id) is
6748 Pack_Decl : constant Entity_Id := Unit (Library_Unit (N));
6750 begin
6751 pragma Assert (Limited_View_Installed (N));
6753 -- Limited with clauses that designate units other than packages are
6754 -- illegal and are never installed.
6756 if Nkind (Pack_Decl) = N_Package_Declaration then
6757 Remove_Limited_With_Unit (Pack_Decl, N);
6758 end if;
6760 -- Indicate that the limited views of the clause have been removed
6762 Set_Limited_View_Installed (N, False);
6763 end Remove_Limited_With_Clause;
6765 ------------------------------
6766 -- Remove_Limited_With_Unit --
6767 ------------------------------
6769 procedure Remove_Limited_With_Unit
6770 (Pack_Decl : Node_Id;
6771 Lim_Clause : Node_Id := Empty)
6773 procedure Remove_Shadow_Entities_From_Visibility (Pack_Id : Entity_Id);
6774 -- Remove the shadow entities of package Pack_Id from direct visibility
6776 procedure Remove_Shadow_Entities_With_Restore (Pack_Id : Entity_Id);
6777 -- Remove the shadow entities of package Pack_Id from direct visibility,
6778 -- restore the corresponding entities they hide into direct visibility,
6779 -- and update the entity and homonym chains.
6781 --------------------------------------------
6782 -- Remove_Shadow_Entities_From_Visibility --
6783 --------------------------------------------
6785 procedure Remove_Shadow_Entities_From_Visibility (Pack_Id : Entity_Id) is
6786 Lim_Header : constant Entity_Id := Limited_View (Pack_Id);
6787 Upto : constant Entity_Id := First_Private_Entity (Lim_Header);
6789 Shadow : Entity_Id;
6791 begin
6792 -- Remove the package from direct visibility
6794 Unchain (Pack_Id);
6795 Set_Is_Immediately_Visible (Pack_Id, False);
6797 -- Remove all shadow entities from direct visibility
6799 Shadow := First_Entity (Lim_Header);
6800 while Present (Shadow) and then Shadow /= Upto loop
6801 Unchain (Shadow);
6802 Next_Entity (Shadow);
6803 end loop;
6804 end Remove_Shadow_Entities_From_Visibility;
6806 -----------------------------------------
6807 -- Remove_Shadow_Entities_With_Restore --
6808 -----------------------------------------
6810 -- This code must be kept synchronized with the code that replaces the
6811 -- real entities by the shadow entities in Install_Limited_With_Clause,
6812 -- otherwise the contents of the homonym chains are not consistent.
6814 procedure Remove_Shadow_Entities_With_Restore (Pack_Id : Entity_Id) is
6815 procedure Restore_Chain_For_Shadow (Shadow : Entity_Id);
6816 -- Remove shadow entity Shadow by updating the entity and homonym
6817 -- chains.
6819 procedure Restore_Chains
6820 (From : Entity_Id;
6821 Upto : Entity_Id);
6822 -- Remove a sequence of shadow entities starting from From and ending
6823 -- prior to Upto by updating the entity and homonym chains.
6825 procedure Restore_Type_Visibility
6826 (From : Entity_Id;
6827 Upto : Entity_Id);
6828 -- Restore a sequence of types starting from From and ending prior to
6829 -- Upto back in direct visibility.
6831 ------------------------------
6832 -- Restore_Chain_For_Shadow --
6833 ------------------------------
6835 procedure Restore_Chain_For_Shadow (Shadow : Entity_Id) is
6836 Typ : constant Entity_Id := Non_Limited_View (Shadow);
6837 pragma Assert (not In_Chain (Typ));
6839 Prev : Entity_Id;
6841 begin
6842 -- If the package has incomplete types, the limited view of the
6843 -- incomplete type is in fact never visible (AI05-129) but we
6844 -- have created a shadow entity E1 for it, that points to E2,
6845 -- the incomplete type at stake. This in turn has a full view
6846 -- E3 that is the full declaration, with a corresponding
6847 -- shadow entity E4. When reinstalling the nonlimited view,
6848 -- the visible entity E4 is replaced directly with E2 in the
6849 -- the homonyms list and E3 is simply ignored.
6851 -- regular views limited views
6853 -- * E2 (incomplete) <-- E1 (shadow)
6855 -- |
6856 -- V
6858 -- E3 (full) <-- E4 (shadow) *
6860 -- [*] denotes the visible entity (Current_Entity)
6862 Prev := Current_Entity (Shadow);
6864 while Present (Prev) loop
6865 -- This is a regular replacement
6867 if Prev = Shadow then
6868 Replace (Prev, Typ);
6870 if Debug_Flag_I then
6871 Write_Str (" (homonym) replace ");
6872 Write_Name (Chars (Typ));
6873 Write_Eol;
6874 end if;
6876 exit;
6878 -- This is where E4 is replaced with E2
6880 elsif Ekind (Prev) = E_Incomplete_Type
6881 and then From_Limited_With (Prev)
6882 and then Ekind (Typ) = E_Incomplete_Type
6883 and then Full_View (Typ) = Non_Limited_View (Prev)
6884 then
6885 Replace (Prev, Typ);
6887 if Debug_Flag_I then
6888 Write_Str (" (homonym) E4 -> E2 ");
6889 Write_Name (Chars (Typ));
6890 Write_Eol;
6891 end if;
6893 exit;
6894 end if;
6896 Prev := Homonym (Prev);
6897 end loop;
6898 end Restore_Chain_For_Shadow;
6900 --------------------
6901 -- Restore_Chains --
6902 --------------------
6904 procedure Restore_Chains
6905 (From : Entity_Id;
6906 Upto : Entity_Id)
6908 Shadow : Entity_Id;
6910 begin
6911 Shadow := From;
6912 while Present (Shadow) and then Shadow /= Upto loop
6914 -- Do not unchain nested packages and child units
6916 if Ekind (Shadow) = E_Package then
6917 null;
6919 elsif Is_Child_Unit (Non_Limited_View (Shadow)) then
6920 null;
6922 else
6923 Restore_Chain_For_Shadow (Shadow);
6924 end if;
6926 Next_Entity (Shadow);
6927 end loop;
6928 end Restore_Chains;
6930 -----------------------------
6931 -- Restore_Type_Visibility --
6932 -----------------------------
6934 procedure Restore_Type_Visibility
6935 (From : Entity_Id;
6936 Upto : Entity_Id)
6938 Typ : Entity_Id;
6940 begin
6941 Typ := From;
6942 while Present (Typ) and then Typ /= Upto loop
6943 if Is_Type (Typ) then
6944 Set_Is_Hidden (Typ, Was_Hidden (Typ));
6945 end if;
6947 Next_Entity (Typ);
6948 end loop;
6949 end Restore_Type_Visibility;
6951 -- Local variables
6953 Lim_Header : constant Entity_Id := Limited_View (Pack_Id);
6955 -- Start of processing Remove_Shadow_Entities_With_Restore
6957 begin
6958 -- The limited view of a package is being uninstalled by removing
6959 -- the effects of a limited with clause. If the clause appears in a
6960 -- unit which is not part of the main unit closure, then the related
6961 -- package must not be visible.
6963 if Present (Lim_Clause)
6964 and then not In_Extended_Main_Source_Unit (Lim_Clause)
6965 then
6966 Set_Is_Immediately_Visible (Pack_Id, False);
6968 -- Otherwise a limited view is being overridden by a nonlimited view.
6969 -- Leave the visibility of the package as is because the unit must be
6970 -- visible when the nonlimited view is installed.
6972 else
6973 null;
6974 end if;
6976 -- Remove the shadow entities from visibility by updating the entity
6977 -- and homonym chains.
6979 Restore_Chains
6980 (From => First_Entity (Lim_Header),
6981 Upto => First_Private_Entity (Lim_Header));
6983 -- Reinstate the types that were hidden by the shadow entities back
6984 -- into direct visibility.
6986 Restore_Type_Visibility
6987 (From => First_Entity (Pack_Id),
6988 Upto => First_Private_Entity (Pack_Id));
6989 end Remove_Shadow_Entities_With_Restore;
6991 -- Local variables
6993 Pack_Id : constant Entity_Id := Defining_Entity (Pack_Decl);
6995 -- Start of processing for Remove_Limited_With_Unit
6997 begin
6998 -- Nothing to do when the limited view of the package is not installed
7000 if not From_Limited_With (Pack_Id) then
7001 return;
7002 end if;
7004 if Debug_Flag_I then
7005 Write_Str ("remove limited view of ");
7006 Write_Name (Chars (Pack_Id));
7007 Write_Str (" from visibility");
7008 Write_Eol;
7009 end if;
7011 -- The package already appears in the compilation closure. As a result,
7012 -- its shadow entities must be replaced by the real entities they hide
7013 -- and the previously hidden entities must be entered back into direct
7014 -- visibility.
7016 if Analyzed (Pack_Decl) then
7017 Remove_Shadow_Entities_With_Restore (Pack_Id);
7019 -- Otherwise the package is not analyzed and its shadow entities must be
7020 -- removed from direct visibility.
7022 else
7023 Remove_Shadow_Entities_From_Visibility (Pack_Id);
7024 end if;
7026 -- Indicate that the limited view of the package is not installed
7028 Set_From_Limited_With (Pack_Id, False);
7029 end Remove_Limited_With_Unit;
7031 --------------------
7032 -- Remove_Parents --
7033 --------------------
7035 procedure Remove_Parents (Lib_Unit : Node_Id) is
7036 P : Node_Id;
7037 P_Name : Entity_Id;
7038 P_Spec : Node_Id := Empty;
7039 E : Entity_Id;
7040 Vis : constant Boolean :=
7041 Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
7043 begin
7044 if Is_Child_Spec (Lib_Unit) then
7045 P_Spec := Parent_Spec (Lib_Unit);
7047 elsif Nkind (Lib_Unit) = N_Package_Body
7048 and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
7049 then
7050 P_Spec := Parent_Spec (Original_Node (Lib_Unit));
7051 end if;
7053 if Present (P_Spec) then
7054 P := Unit (P_Spec);
7055 P_Name := Get_Parent_Entity (P);
7056 Remove_Context_Clauses (P_Spec);
7057 End_Package_Scope (P_Name);
7058 Set_Is_Immediately_Visible (P_Name, Vis);
7060 -- Remove from visibility the siblings as well, which are directly
7061 -- visible while the parent is in scope.
7063 E := First_Entity (P_Name);
7064 while Present (E) loop
7065 if Is_Child_Unit (E) then
7066 Set_Is_Immediately_Visible (E, False);
7067 end if;
7069 Next_Entity (E);
7070 end loop;
7072 Set_In_Package_Body (P_Name, False);
7074 -- This is the recursive call to remove the context of any higher
7075 -- level parent. This recursion ensures that all parents are removed
7076 -- in the reverse order of their installation.
7078 Remove_Parents (P);
7079 end if;
7080 end Remove_Parents;
7082 ---------------------------------
7083 -- Remove_Private_With_Clauses --
7084 ---------------------------------
7086 procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
7087 Item : Node_Id;
7089 function In_Regular_With_Clause (E : Entity_Id) return Boolean;
7090 -- Check whether a given unit appears in a regular with_clause. Used to
7091 -- determine whether a private_with_clause, implicit or explicit, should
7092 -- be ignored.
7094 ----------------------------
7095 -- In_Regular_With_Clause --
7096 ----------------------------
7098 function In_Regular_With_Clause (E : Entity_Id) return Boolean is
7099 Item : Node_Id;
7101 begin
7102 Item := First (Context_Items (Comp_Unit));
7104 while Present (Item) loop
7105 if Nkind (Item) = N_With_Clause
7107 -- The following guard is needed to ensure that the name has
7108 -- been properly analyzed before we go fetching its entity.
7110 and then Is_Entity_Name (Name (Item))
7111 and then Entity (Name (Item)) = E
7112 and then not Private_Present (Item)
7113 then
7114 return True;
7115 end if;
7117 Next (Item);
7118 end loop;
7120 return False;
7121 end In_Regular_With_Clause;
7123 -- Start of processing for Remove_Private_With_Clauses
7125 begin
7126 Item := First (Context_Items (Comp_Unit));
7127 while Present (Item) loop
7128 if Nkind (Item) = N_With_Clause and then Private_Present (Item) then
7130 -- If private_with_clause is redundant, remove it from context,
7131 -- as a small optimization to subsequent handling of private_with
7132 -- clauses in other nested packages. We replace the clause with
7133 -- a null statement, which is otherwise ignored by the rest of
7134 -- the compiler.
7136 if In_Regular_With_Clause (Entity (Name (Item))) then
7137 declare
7138 Nxt : constant Node_Id := Next (Item);
7139 begin
7140 Rewrite (Item, Make_Null_Statement (Sloc (Item)));
7141 Analyze (Item);
7142 Item := Nxt;
7143 end;
7145 elsif Limited_Present (Item) then
7146 if not Limited_View_Installed (Item) then
7147 Remove_Limited_With_Clause (Item);
7148 end if;
7150 Next (Item);
7152 else
7153 Remove_Unit_From_Visibility (Entity (Name (Item)));
7154 Set_Context_Installed (Item, False);
7155 Next (Item);
7156 end if;
7158 else
7159 Next (Item);
7160 end if;
7161 end loop;
7162 end Remove_Private_With_Clauses;
7164 ---------------------------------
7165 -- Remove_Unit_From_Visibility --
7166 ---------------------------------
7168 procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
7169 begin
7170 if Debug_Flag_I then
7171 Write_Str ("remove unit ");
7172 Write_Name (Chars (Unit_Name));
7173 Write_Str (" from visibility");
7174 Write_Eol;
7175 end if;
7177 Set_Is_Visible_Lib_Unit (Unit_Name, False);
7178 Set_Is_Potentially_Use_Visible (Unit_Name, False);
7179 Set_Is_Immediately_Visible (Unit_Name, False);
7181 -- If the unit is a wrapper package, the subprogram instance is
7182 -- what must be removed from visibility.
7183 -- Should we use Related_Instance instead???
7185 if Is_Wrapper_Package (Unit_Name) then
7186 Set_Is_Immediately_Visible (Current_Entity (Unit_Name), False);
7187 end if;
7188 end Remove_Unit_From_Visibility;
7190 --------
7191 -- sm --
7192 --------
7194 procedure sm is
7195 begin
7196 null;
7197 end sm;
7199 -------------
7200 -- Replace --
7201 -------------
7203 procedure Replace (Old_E, New_E : Entity_Id) is
7204 Prev : Entity_Id;
7206 begin
7207 Prev := Current_Entity (Old_E);
7209 if No (Prev) then
7210 return;
7212 elsif Prev = Old_E then
7213 Set_Current_Entity (New_E);
7214 Set_Homonym (New_E, Homonym (Old_E));
7216 else
7217 while Present (Prev) and then Homonym (Prev) /= Old_E loop
7218 Prev := Homonym (Prev);
7219 end loop;
7221 if Present (Prev) then
7222 Set_Homonym (Prev, New_E);
7223 Set_Homonym (New_E, Homonym (Old_E));
7224 end if;
7225 end if;
7226 end Replace;
7228 -------------
7229 -- Unchain --
7230 -------------
7232 procedure Unchain (E : Entity_Id) is
7233 Prev : Entity_Id;
7235 begin
7236 Prev := Current_Entity (E);
7238 if No (Prev) then
7239 return;
7241 elsif Prev = E then
7242 Set_Name_Entity_Id (Chars (E), Homonym (E));
7244 else
7245 while Present (Prev) and then Homonym (Prev) /= E loop
7246 Prev := Homonym (Prev);
7247 end loop;
7249 if Present (Prev) then
7250 Set_Homonym (Prev, Homonym (E));
7251 end if;
7252 end if;
7254 if Debug_Flag_I then
7255 Write_Str (" (homonym) unchain ");
7256 Write_Name (Chars (E));
7257 Write_Eol;
7258 end if;
7259 end Unchain;
7261 end Sem_Ch10;