OSDN Git Service

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