OSDN Git Service

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