OSDN Git Service

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