OSDN Git Service

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