OSDN Git Service

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