OSDN Git Service

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