OSDN Git Service

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