OSDN Git Service

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