OSDN Git Service

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