OSDN Git Service

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