OSDN Git Service

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