OSDN Git Service

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