OSDN Git Service

2009-06-24 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.
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          if Is_Child_Spec (Unit (Library_Unit (N))) then
3207
3208             --  If the unit is the body of a public child unit, the private
3209             --  declarations of the parent must be made visible. If the child
3210             --  unit is private, the private declarations have been installed
3211             --  already in the call to Install_Parents for the spec. Installing
3212             --  private declarations must be done for all ancestors of public
3213             --  child units. In addition, sibling units mentioned in the
3214             --  context clause of the body are directly visible.
3215
3216             declare
3217                Lib_Spec : Node_Id;
3218                P        : Node_Id;
3219                P_Name   : Entity_Id;
3220
3221             begin
3222                Lib_Spec := Unit (Library_Unit (N));
3223                while Is_Child_Spec (Lib_Spec) loop
3224                   P      := Unit (Parent_Spec (Lib_Spec));
3225                   P_Name := Defining_Entity (P);
3226
3227                   if not (Private_Present (Parent (Lib_Spec)))
3228                     and then not In_Private_Part (P_Name)
3229                   then
3230                      Install_Private_Declarations (P_Name);
3231                      Install_Private_With_Clauses (P_Name);
3232                      Set_Use (Private_Declarations (Specification (P)));
3233                   end if;
3234
3235                   Lib_Spec := P;
3236                end loop;
3237             end;
3238          end if;
3239
3240          --  For a package body, children in context are immediately visible
3241
3242          Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
3243       end if;
3244
3245       if Nkind_In (Lib_Unit, N_Generic_Package_Declaration,
3246                              N_Generic_Subprogram_Declaration,
3247                              N_Package_Declaration,
3248                              N_Subprogram_Declaration)
3249       then
3250          if Is_Child_Spec (Lib_Unit) then
3251             Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
3252             Set_Is_Private_Descendant
3253               (Defining_Entity (Lib_Unit),
3254                Is_Private_Descendant (Lib_Parent)
3255                  or else Private_Present (Parent (Lib_Unit)));
3256
3257          else
3258             Set_Is_Private_Descendant
3259               (Defining_Entity (Lib_Unit),
3260                Private_Present (Parent (Lib_Unit)));
3261          end if;
3262       end if;
3263
3264       if Check_Private then
3265          Check_Private_Child_Unit (N);
3266       end if;
3267    end Install_Context_Clauses;
3268
3269    -------------------------------------
3270    -- Install_Limited_Context_Clauses --
3271    -------------------------------------
3272
3273    procedure Install_Limited_Context_Clauses (N : Node_Id) is
3274       Item : Node_Id;
3275
3276       procedure Check_Renamings (P : Node_Id; W : Node_Id);
3277       --  Check that the unlimited view of a given compilation_unit is not
3278       --  already visible through "use + renamings".
3279
3280       procedure Check_Private_Limited_Withed_Unit (Item : Node_Id);
3281       --  Check that if a limited_with clause of a given compilation_unit
3282       --  mentions a descendant of a private child of some library unit,
3283       --  then the given compilation_unit shall be the declaration of a
3284       --  private descendant of that library unit, or a public descendant
3285       --  of such. The code is analogous to that of Check_Private_Child_Unit
3286       --  but we cannot use entities on the limited with_clauses because
3287       --  their units have not been analyzed, so we have to climb the tree
3288       --  of ancestors looking for private keywords.
3289
3290       procedure Expand_Limited_With_Clause
3291         (Comp_Unit : Node_Id;
3292          Nam       : Node_Id;
3293          N         : Node_Id);
3294       --  If a child unit appears in a limited_with clause, there are implicit
3295       --  limited_with clauses on all parents that are not already visible
3296       --  through a regular with clause. This procedure creates the implicit
3297       --  limited with_clauses for the parents and loads the corresponding
3298       --  units. The shadow entities are created when the inserted clause is
3299       --  analyzed. Implements Ada 2005 (AI-50217).
3300
3301       ---------------------
3302       -- Check_Renamings --
3303       ---------------------
3304
3305       procedure Check_Renamings (P : Node_Id; W : Node_Id) is
3306          Item   : Node_Id;
3307          Spec   : Node_Id;
3308          WEnt   : Entity_Id;
3309          Nam    : Node_Id;
3310          E      : Entity_Id;
3311          E2     : Entity_Id;
3312
3313       begin
3314          pragma Assert (Nkind (W) = N_With_Clause);
3315
3316          --  Protect the frontend against previous critical errors
3317
3318          case Nkind (Unit (Library_Unit (W))) is
3319             when N_Subprogram_Declaration         |
3320                  N_Package_Declaration            |
3321                  N_Generic_Subprogram_Declaration |
3322                  N_Generic_Package_Declaration    =>
3323                null;
3324
3325             when others =>
3326                return;
3327          end case;
3328
3329          --  Check "use + renamings"
3330
3331          WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
3332          Spec := Specification (Unit (P));
3333
3334          Item := First (Visible_Declarations (Spec));
3335          while Present (Item) loop
3336
3337             --  Look only at use package clauses
3338
3339             if Nkind (Item) = N_Use_Package_Clause then
3340
3341                --  Traverse the list of packages
3342
3343                Nam := First (Names (Item));
3344                while Present (Nam) loop
3345                   E := Entity (Nam);
3346
3347                   pragma Assert (Present (Parent (E)));
3348
3349                   if Nkind (Parent (E)) = N_Package_Renaming_Declaration
3350                     and then Renamed_Entity (E) = WEnt
3351                   then
3352                      --  The unlimited view is visible through use clause and
3353                      --  renamings. There is no need to generate the error
3354                      --  message here because Is_Visible_Through_Renamings
3355                      --  takes care of generating the precise error message.
3356
3357                      return;
3358
3359                   elsif Nkind (Parent (E)) = N_Package_Specification then
3360
3361                      --  The use clause may refer to a local package.
3362                      --  Check all the enclosing scopes.
3363
3364                      E2 := E;
3365                      while E2 /= Standard_Standard
3366                        and then E2 /= WEnt
3367                      loop
3368                         E2 := Scope (E2);
3369                      end loop;
3370
3371                      if E2 = WEnt then
3372                         Error_Msg_N
3373                           ("unlimited view visible through use clause ", W);
3374                         return;
3375                      end if;
3376                   end if;
3377
3378                   Next (Nam);
3379                end loop;
3380             end if;
3381
3382             Next (Item);
3383          end loop;
3384
3385          --  Recursive call to check all the ancestors
3386
3387          if Is_Child_Spec (Unit (P)) then
3388             Check_Renamings (P => Parent_Spec (Unit (P)), W => W);
3389          end if;
3390       end Check_Renamings;
3391
3392       ---------------------------------------
3393       -- Check_Private_Limited_Withed_Unit --
3394       ---------------------------------------
3395
3396       procedure Check_Private_Limited_Withed_Unit (Item : Node_Id) is
3397          Curr_Parent  : Node_Id;
3398          Child_Parent : Node_Id;
3399          Curr_Private : Boolean;
3400
3401       begin
3402          --  Compilation unit of the parent of the withed library unit
3403
3404          Child_Parent := Library_Unit (Item);
3405
3406          --  If the child unit is a public child, then locate its nearest
3407          --  private ancestor, if any; Child_Parent will then be set to
3408          --  the parent of that ancestor.
3409
3410          if not Private_Present (Library_Unit (Item)) then
3411             while Present (Child_Parent)
3412               and then not Private_Present (Child_Parent)
3413             loop
3414                Child_Parent := Parent_Spec (Unit (Child_Parent));
3415             end loop;
3416
3417             if No (Child_Parent) then
3418                return;
3419             end if;
3420          end if;
3421
3422          Child_Parent := Parent_Spec (Unit (Child_Parent));
3423
3424          --  Traverse all the ancestors of the current compilation
3425          --  unit to check if it is a descendant of named library unit.
3426
3427          Curr_Parent := Parent (Item);
3428          Curr_Private := Private_Present (Curr_Parent);
3429
3430          while Present (Parent_Spec (Unit (Curr_Parent)))
3431            and then Curr_Parent /= Child_Parent
3432          loop
3433             Curr_Parent := Parent_Spec (Unit (Curr_Parent));
3434             Curr_Private := Curr_Private or else Private_Present (Curr_Parent);
3435          end loop;
3436
3437          if Curr_Parent /= Child_Parent then
3438             Error_Msg_N
3439               ("unit in with clause is private child unit!", Item);
3440             Error_Msg_NE
3441               ("\current unit must also have parent&!",
3442                Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3443
3444          elsif Private_Present (Parent (Item))
3445             or else Curr_Private
3446             or else Private_Present (Item)
3447             or else Nkind_In (Unit (Parent (Item)), N_Package_Body,
3448                                                         N_Subprogram_Body,
3449                                                         N_Subunit)
3450          then
3451             --  Current unit is private, of descendant of a private unit
3452
3453             null;
3454
3455          else
3456             Error_Msg_NE
3457               ("current unit must also be private descendant of&",
3458                Item, Defining_Unit_Name (Specification (Unit (Child_Parent))));
3459          end if;
3460       end Check_Private_Limited_Withed_Unit;
3461
3462       --------------------------------
3463       -- Expand_Limited_With_Clause --
3464       --------------------------------
3465
3466       procedure Expand_Limited_With_Clause
3467         (Comp_Unit : Node_Id;
3468          Nam       : Node_Id;
3469          N         : Node_Id)
3470       is
3471          Loc   : constant Source_Ptr := Sloc (Nam);
3472          Unum  : Unit_Number_Type;
3473          Withn : Node_Id;
3474
3475          function Previous_Withed_Unit (W : Node_Id) return Boolean;
3476          --  Returns true if the context already includes a with_clause for
3477          --  this unit. If the with_clause is non-limited, the unit is fully
3478          --  visible and an implicit limited_with should not be created. If
3479          --  there is already a limited_with clause for W, a second one is
3480          --  simply redundant.
3481
3482          --------------------------
3483          -- Previous_Withed_Unit --
3484          --------------------------
3485
3486          function Previous_Withed_Unit (W : Node_Id) return Boolean is
3487             Item : Node_Id;
3488
3489          begin
3490             --  A limited with_clause cannot appear in the same context_clause
3491             --  as a nonlimited with_clause which mentions the same library.
3492
3493             Item := First (Context_Items (Comp_Unit));
3494             while Present (Item) loop
3495                if Nkind (Item) = N_With_Clause
3496                  and then Library_Unit (Item) = Library_Unit (W)
3497                then
3498                   return True;
3499                end if;
3500
3501                Next (Item);
3502             end loop;
3503
3504             return False;
3505          end Previous_Withed_Unit;
3506
3507       --  Start of processing for Expand_Limited_With_Clause
3508
3509       begin
3510          New_Nodes_OK := New_Nodes_OK + 1;
3511
3512          if Nkind (Nam) = N_Identifier then
3513
3514             --  Create node for name of withed unit
3515
3516             Withn :=
3517               Make_With_Clause (Loc,
3518                 Name => New_Copy (Nam));
3519
3520          else pragma Assert (Nkind (Nam) = N_Selected_Component);
3521             Withn :=
3522               Make_With_Clause (Loc,
3523                 Name => Make_Selected_Component (Loc,
3524                   Prefix        => New_Copy_Tree (Prefix (Nam)),
3525                   Selector_Name => New_Copy (Selector_Name (Nam))));
3526             Set_Parent (Withn, Parent (N));
3527          end if;
3528
3529          Set_Limited_Present (Withn);
3530          Set_First_Name      (Withn);
3531          Set_Implicit_With   (Withn);
3532
3533          Unum :=
3534            Load_Unit
3535              (Load_Name  => Get_Spec_Name (Get_Unit_Name (Nam)),
3536               Required   => True,
3537               Subunit    => False,
3538               Error_Node => Nam);
3539
3540          --  Do not generate a limited_with_clause on the current unit.
3541          --  This path is taken when a unit has a limited_with clause on
3542          --  one of its child units.
3543
3544          if Unum = Current_Sem_Unit then
3545             return;
3546          end if;
3547
3548          Set_Library_Unit (Withn, Cunit (Unum));
3549          Set_Corresponding_Spec
3550            (Withn, Specification (Unit (Cunit (Unum))));
3551
3552          if not Previous_Withed_Unit (Withn) then
3553             Prepend (Withn, Context_Items (Parent (N)));
3554             Mark_Rewrite_Insertion (Withn);
3555
3556             --  Add implicit limited_with_clauses for parents of child units
3557             --  mentioned in limited_with clauses.
3558
3559             if Nkind (Nam) = N_Selected_Component then
3560                Expand_Limited_With_Clause (Comp_Unit, Prefix (Nam), N);
3561             end if;
3562
3563             Analyze (Withn);
3564
3565             if not Limited_View_Installed (Withn) then
3566                Install_Limited_Withed_Unit (Withn);
3567             end if;
3568          end if;
3569
3570          New_Nodes_OK := New_Nodes_OK - 1;
3571       end Expand_Limited_With_Clause;
3572
3573    --  Start of processing for Install_Limited_Context_Clauses
3574
3575    begin
3576       Item := First (Context_Items (N));
3577       while Present (Item) loop
3578          if Nkind (Item) = N_With_Clause
3579            and then Limited_Present (Item)
3580          then
3581             if Nkind (Name (Item)) = N_Selected_Component then
3582                Expand_Limited_With_Clause
3583                  (Comp_Unit => N, Nam => Prefix (Name (Item)), N => Item);
3584             end if;
3585
3586             Check_Private_Limited_Withed_Unit (Item);
3587
3588             if not Implicit_With (Item)
3589               and then Is_Child_Spec (Unit (N))
3590             then
3591                Check_Renamings (Parent_Spec (Unit (N)), Item);
3592             end if;
3593
3594             --  A unit may have a limited with on itself if it has a limited
3595             --  with_clause on one of its child units. In that case it is
3596             --  already being compiled and it makes no sense to install its
3597             --  limited view.
3598
3599             --  If the item is a limited_private_with_clause, install it if the
3600             --  current unit is a body or if it is a private child. Otherwise
3601             --  the private clause is installed before analyzing the private
3602             --  part of the current unit.
3603
3604             if Library_Unit (Item) /= Cunit (Current_Sem_Unit)
3605               and then not Limited_View_Installed (Item)
3606             then
3607                if not Private_Present (Item)
3608                  or else Private_Present (N)
3609                  or else Nkind_In (Unit (N), N_Package_Body,
3610                                              N_Subprogram_Body,
3611                                              N_Subunit)
3612                then
3613                   Install_Limited_Withed_Unit (Item);
3614                end if;
3615             end if;
3616          end if;
3617
3618          Next (Item);
3619       end loop;
3620
3621       --  Ada 2005 (AI-412): Examine the visible declarations of a package
3622       --  spec, looking for incomplete subtype declarations of incomplete
3623       --  types visible through a limited with clause.
3624
3625       if Ada_Version >= Ada_05
3626         and then Analyzed (N)
3627         and then Nkind (Unit (N)) = N_Package_Declaration
3628       then
3629          declare
3630             Decl         : Node_Id;
3631             Def_Id       : Entity_Id;
3632             Non_Lim_View : Entity_Id;
3633
3634          begin
3635             Decl := First (Visible_Declarations (Specification (Unit (N))));
3636             while Present (Decl) loop
3637                if Nkind (Decl) = N_Subtype_Declaration
3638                  and then
3639                    Ekind (Defining_Identifier (Decl)) = E_Incomplete_Subtype
3640                  and then
3641                    From_With_Type (Defining_Identifier (Decl))
3642                then
3643                   Def_Id := Defining_Identifier (Decl);
3644                   Non_Lim_View := Non_Limited_View (Def_Id);
3645
3646                   if not Is_Incomplete_Type (Non_Lim_View) then
3647
3648                      --  Convert an incomplete subtype declaration into a
3649                      --  corresponding non-limited view subtype declaration.
3650                      --  This is usually the case when analyzing a body that
3651                      --  has regular with-clauses, when the spec has limited
3652                      --  ones.
3653
3654                      --  If the non-limited view is still incomplete, it is
3655                      --  the dummy entry already created, and the declaration
3656                      --  cannot be reanalyzed. This is the case when installing
3657                      --  a parent unit that has limited with-clauses.
3658
3659                      Set_Subtype_Indication (Decl,
3660                        New_Reference_To (Non_Lim_View, Sloc (Def_Id)));
3661                      Set_Etype (Def_Id, Non_Lim_View);
3662                      Set_Ekind (Def_Id, Subtype_Kind (Ekind (Non_Lim_View)));
3663                      Set_Analyzed (Decl, False);
3664
3665                      --  Reanalyze the declaration, suppressing the call to
3666                      --  Enter_Name to avoid duplicate names.
3667
3668                      Analyze_Subtype_Declaration
3669                       (N    => Decl,
3670                        Skip => True);
3671                   end if;
3672                end if;
3673
3674                Next (Decl);
3675             end loop;
3676          end;
3677       end if;
3678    end Install_Limited_Context_Clauses;
3679
3680    ---------------------
3681    -- Install_Parents --
3682    ---------------------
3683
3684    procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3685       P      : Node_Id;
3686       E_Name : Entity_Id;
3687       P_Name : Entity_Id;
3688       P_Spec : Node_Id;
3689
3690    begin
3691       P := Unit (Parent_Spec (Lib_Unit));
3692       P_Name := Get_Parent_Entity (P);
3693
3694       if Etype (P_Name) = Any_Type then
3695          return;
3696       end if;
3697
3698       if Ekind (P_Name) = E_Generic_Package
3699         and then not Nkind_In (Lib_Unit, N_Generic_Subprogram_Declaration,
3700                                          N_Generic_Package_Declaration)
3701         and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3702       then
3703          Error_Msg_N
3704            ("child of a generic package must be a generic unit", Lib_Unit);
3705
3706       elsif not Is_Package_Or_Generic_Package (P_Name) then
3707          Error_Msg_N
3708            ("parent unit must be package or generic package", Lib_Unit);
3709          raise Unrecoverable_Error;
3710
3711       elsif Present (Renamed_Object (P_Name)) then
3712          Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3713          raise Unrecoverable_Error;
3714
3715       --  Verify that a child of an instance is itself an instance, or the
3716       --  renaming of one. Given that an instance that is a unit is replaced
3717       --  with a package declaration, check against the original node. The
3718       --  parent may be currently being instantiated, in which case it appears
3719       --  as a declaration, but the generic_parent is already established
3720       --  indicating that we deal with an instance.
3721
3722       elsif Nkind (Original_Node (P)) = N_Package_Instantiation then
3723          if Nkind (Lib_Unit) in N_Renaming_Declaration
3724            or else Nkind (Original_Node (Lib_Unit)) in N_Generic_Instantiation
3725            or else
3726              (Nkind (Lib_Unit) = N_Package_Declaration
3727                 and then Present (Generic_Parent (Specification (Lib_Unit))))
3728          then
3729             null;
3730          else
3731             Error_Msg_N
3732               ("child of an instance must be an instance or renaming",
3733                 Lib_Unit);
3734          end if;
3735       end if;
3736
3737       --  This is the recursive call that ensures all parents are loaded
3738
3739       if Is_Child_Spec (P) then
3740          Install_Parents (P,
3741            Is_Private or else Private_Present (Parent (Lib_Unit)));
3742       end if;
3743
3744       --  Now we can install the context for this parent
3745
3746       Install_Context_Clauses (Parent_Spec (Lib_Unit));
3747       Install_Limited_Context_Clauses (Parent_Spec (Lib_Unit));
3748       Install_Siblings (P_Name, Parent (Lib_Unit));
3749
3750       --  The child unit is in the declarative region of the parent. The parent
3751       --  must therefore appear in the scope stack and be visible, as when
3752       --  compiling the corresponding body. If the child unit is private or it
3753       --  is a package body, private declarations must be accessible as well.
3754       --  Use declarations in the parent must also be installed. Finally, other
3755       --  child units of the same parent that are in the context are
3756       --  immediately visible.
3757
3758       --  Find entity for compilation unit, and set its private descendant
3759       --  status as needed.
3760
3761       E_Name := Defining_Entity (Lib_Unit);
3762
3763       Set_Is_Child_Unit (E_Name);
3764
3765       Set_Is_Private_Descendant (E_Name,
3766          Is_Private_Descendant (P_Name)
3767            or else Private_Present (Parent (Lib_Unit)));
3768
3769       P_Spec := Specification (Unit_Declaration_Node (P_Name));
3770       Push_Scope (P_Name);
3771
3772       --  Save current visibility of unit
3773
3774       Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3775         Is_Immediately_Visible (P_Name);
3776       Set_Is_Immediately_Visible (P_Name);
3777       Install_Visible_Declarations (P_Name);
3778       Set_Use (Visible_Declarations (P_Spec));
3779
3780       --  If the parent is a generic unit, its formal part may contain formal
3781       --  packages and use clauses for them.
3782
3783       if Ekind (P_Name) = E_Generic_Package then
3784          Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3785       end if;
3786
3787       if Is_Private
3788         or else Private_Present (Parent (Lib_Unit))
3789       then
3790          Install_Private_Declarations (P_Name);
3791          Install_Private_With_Clauses (P_Name);
3792          Set_Use (Private_Declarations (P_Spec));
3793       end if;
3794    end Install_Parents;
3795
3796    ----------------------------------
3797    -- Install_Private_With_Clauses --
3798    ----------------------------------
3799
3800    procedure Install_Private_With_Clauses (P : Entity_Id) is
3801       Decl   : constant Node_Id := Unit_Declaration_Node (P);
3802       Item   : Node_Id;
3803
3804    begin
3805       if Debug_Flag_I then
3806          Write_Str ("install private with clauses of ");
3807          Write_Name (Chars (P));
3808          Write_Eol;
3809       end if;
3810
3811       if Nkind (Parent (Decl)) = N_Compilation_Unit then
3812          Item := First (Context_Items (Parent (Decl)));
3813          while Present (Item) loop
3814             if Nkind (Item) = N_With_Clause
3815               and then Private_Present (Item)
3816             then
3817                if Limited_Present (Item) then
3818                   if not Limited_View_Installed (Item) then
3819                      Install_Limited_Withed_Unit (Item);
3820                   end if;
3821                else
3822                   Install_Withed_Unit (Item, Private_With_OK => True);
3823                end if;
3824             end if;
3825
3826             Next (Item);
3827          end loop;
3828       end if;
3829    end Install_Private_With_Clauses;
3830
3831    ----------------------
3832    -- Install_Siblings --
3833    ----------------------
3834
3835    procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3836       Item : Node_Id;
3837       Id   : Entity_Id;
3838       Prev : Entity_Id;
3839
3840    begin
3841       --  Iterate over explicit with clauses, and check whether the scope of
3842       --  each entity is an ancestor of the current unit, in which case it is
3843       --  immediately visible.
3844
3845       Item := First (Context_Items (N));
3846       while Present (Item) loop
3847
3848          --  Do not install private_with_clauses declaration, unless
3849          --  unit is itself a private child unit, or is a body.
3850          --  Note that for a subprogram body the private_with_clause does
3851          --  not take effect until after the specification.
3852
3853          if Nkind (Item) /= N_With_Clause
3854            or else Implicit_With (Item)
3855            or else Limited_Present (Item)
3856          then
3857             null;
3858
3859          elsif not Private_Present (Item)
3860            or else Private_Present (N)
3861            or else Nkind (Unit (N)) = N_Package_Body
3862          then
3863             Id := Entity (Name (Item));
3864
3865             if Is_Child_Unit (Id)
3866               and then Is_Ancestor_Package (Scope (Id), U_Name)
3867             then
3868                Set_Is_Immediately_Visible (Id);
3869
3870                --  Check for the presence of another unit in the context,
3871                --  that may be inadvertently hidden by the child.
3872
3873                Prev := Current_Entity (Id);
3874
3875                if Present (Prev)
3876                  and then Is_Immediately_Visible (Prev)
3877                  and then not Is_Child_Unit (Prev)
3878                then
3879                   declare
3880                      Clause : Node_Id;
3881
3882                   begin
3883                      Clause := First (Context_Items (N));
3884                      while Present (Clause) loop
3885                         if Nkind (Clause) = N_With_Clause
3886                           and then Entity (Name (Clause)) = Prev
3887                         then
3888                            Error_Msg_NE
3889                               ("child unit& hides compilation unit " &
3890                                "with the same name?",
3891                                  Name (Item), Id);
3892                            exit;
3893                         end if;
3894
3895                         Next (Clause);
3896                      end loop;
3897                   end;
3898                end if;
3899
3900             --  The With_Clause may be on a grand-child or one of its further
3901             --  descendants, which makes a child immediately visible. Examine
3902             --  ancestry to determine whether such a child exists. For example,
3903             --  if current unit is A.C, and with_clause is on A.X.Y.Z, then X
3904             --  is immediately visible.
3905
3906             elsif Is_Child_Unit (Id) then
3907                declare
3908                   Par : Entity_Id;
3909
3910                begin
3911                   Par := Scope (Id);
3912                   while Is_Child_Unit (Par) loop
3913                      if Is_Ancestor_Package (Scope (Par), U_Name) then
3914                         Set_Is_Immediately_Visible (Par);
3915                         exit;
3916                      end if;
3917
3918                      Par := Scope (Par);
3919                   end loop;
3920                end;
3921             end if;
3922
3923          --  If the item is a private with-clause on a child unit, the parent
3924          --  may have been installed already, but the child unit must remain
3925          --  invisible until installed in a private part or body.
3926
3927          elsif Private_Present (Item) then
3928             Id := Entity (Name (Item));
3929
3930             if Is_Child_Unit (Id) then
3931                Set_Is_Visible_Child_Unit (Id, False);
3932             end if;
3933          end if;
3934
3935          Next (Item);
3936       end loop;
3937    end Install_Siblings;
3938
3939    ---------------------------------
3940    -- Install_Limited_Withed_Unit --
3941    ---------------------------------
3942
3943    procedure Install_Limited_Withed_Unit (N : Node_Id) is
3944       P_Unit           : constant Entity_Id := Unit (Library_Unit (N));
3945       E                : Entity_Id;
3946       P                : Entity_Id;
3947       Is_Child_Package : Boolean := False;
3948       Lim_Header       : Entity_Id;
3949       Lim_Typ          : Entity_Id;
3950
3951       procedure Check_Body_Required;
3952       --  A unit mentioned in a limited with_clause may not be mentioned in
3953       --  a regular with_clause, but must still be included in the current
3954       --  partition. We need to determine whether the unit needs a body, so
3955       --  that the binder can determine the name of the file to be compiled.
3956       --  Checking whether a unit needs a body can be done without semantic
3957       --  analysis, by examining the nature of the declarations in the package.
3958
3959       function Has_Limited_With_Clause
3960         (C_Unit : Entity_Id;
3961          Pack   : Entity_Id) return Boolean;
3962       --  Determine whether any package in the ancestor chain starting with
3963       --  C_Unit has a limited with clause for package Pack.
3964
3965       function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean;
3966       --  Check if some package installed though normal with-clauses has a
3967       --  renaming declaration of package P. AARM 10.1.2(21/2).
3968
3969       -------------------------
3970       -- Check_Body_Required --
3971       -------------------------
3972
3973       procedure Check_Body_Required is
3974          PA : constant List_Id :=
3975                 Pragmas_After (Aux_Decls_Node (Parent (P_Unit)));
3976
3977          procedure Check_Declarations (Spec : Node_Id);
3978          --  Recursive procedure that does the work and checks nested packages
3979
3980          ------------------------
3981          -- Check_Declarations --
3982          ------------------------
3983
3984          procedure Check_Declarations (Spec : Node_Id) is
3985             Decl             : Node_Id;
3986             Incomplete_Decls : constant Elist_Id := New_Elmt_List;
3987
3988             Subp_List        : constant Elist_Id := New_Elmt_List;
3989
3990             procedure Check_Pragma_Import (P : Node_Id);
3991             --  If a pragma import applies to a previous subprogram, the
3992             --  enclosing unit may not need a body. The processing is syntactic
3993             --  and does not require a declaration to be analyzed. The code
3994             --  below also handles pragma Import when applied to a subprogram
3995             --  that renames another. In this case the pragma applies to the
3996             --  renamed entity.
3997             --
3998             --  Chains of multiple renames are not handled by the code below.
3999             --  It is probably impossible to handle all cases without proper
4000             --  name resolution. In such cases the algorithm is conservative
4001             --  and will indicate that a body is needed???
4002
4003             -------------------------
4004             -- Check_Pragma_Import --
4005             -------------------------
4006
4007             procedure Check_Pragma_Import (P : Node_Id) is
4008                Arg      : Node_Id;
4009                Prev_Id  : Elmt_Id;
4010                Subp_Id  : Elmt_Id;
4011                Imported : Node_Id;
4012
4013                procedure Remove_Homonyms (E : Node_Id);
4014                --  Make one pass over list of subprograms. Called again if
4015                --  subprogram is a renaming. E is known to be an identifier.
4016
4017                ---------------------
4018                -- Remove_Homonyms --
4019                ---------------------
4020
4021                procedure Remove_Homonyms (E : Node_Id) is
4022                   R : Entity_Id := Empty;
4023                   --  Name of renamed entity, if any
4024
4025                begin
4026                   Subp_Id := First_Elmt (Subp_List);
4027                   while Present (Subp_Id) loop
4028                      if Chars (Node (Subp_Id)) = Chars (E) then
4029                         if Nkind (Parent (Parent (Node (Subp_Id))))
4030                           /=  N_Subprogram_Renaming_Declaration
4031                         then
4032                            Prev_Id := Subp_Id;
4033                            Next_Elmt (Subp_Id);
4034                            Remove_Elmt (Subp_List, Prev_Id);
4035                         else
4036                            R := Name (Parent (Parent (Node (Subp_Id))));
4037                            exit;
4038                         end if;
4039                      else
4040                         Next_Elmt (Subp_Id);
4041                      end if;
4042                   end loop;
4043
4044                   if Present (R) then
4045                      if Nkind (R) = N_Identifier then
4046                         Remove_Homonyms (R);
4047
4048                      elsif Nkind (R) = N_Selected_Component then
4049                         Remove_Homonyms (Selector_Name (R));
4050
4051                      --  Renaming of attribute
4052
4053                      else
4054                         null;
4055                      end if;
4056                   end if;
4057                end Remove_Homonyms;
4058
4059             --  Start of processing for Check_Pragma_Import
4060
4061             begin
4062                --  Find name of entity in Import pragma. We have not analyzed
4063                --  the construct, so we must guard against syntax errors.
4064
4065                Arg := Next (First (Pragma_Argument_Associations (P)));
4066
4067                if No (Arg)
4068                  or else Nkind (Expression (Arg)) /= N_Identifier
4069                then
4070                   return;
4071                else
4072                   Imported := Expression (Arg);
4073                end if;
4074
4075                Remove_Homonyms (Imported);
4076             end Check_Pragma_Import;
4077
4078          --  Start of processing for Check_Declarations
4079
4080          begin
4081             --  Search for Elaborate Body pragma
4082
4083             Decl := First (Visible_Declarations (Spec));
4084             while Present (Decl)
4085               and then Nkind (Decl) = N_Pragma
4086             loop
4087                if Get_Pragma_Id (Decl) = Pragma_Elaborate_Body then
4088                   Set_Body_Required (Library_Unit (N));
4089                   return;
4090                end if;
4091
4092                Next (Decl);
4093             end loop;
4094
4095             --  Look for declarations that require the presence of a body
4096
4097             while Present (Decl) loop
4098
4099                --  Subprogram that comes from source means body may be needed.
4100                --  Save for subsequent examination of import pragmas.
4101
4102                if Comes_From_Source (Decl)
4103                  and then (Nkind_In (Decl, N_Subprogram_Declaration,
4104                                            N_Subprogram_Renaming_Declaration,
4105                                            N_Generic_Subprogram_Declaration))
4106                then
4107                   Append_Elmt (Defining_Entity (Decl), Subp_List);
4108
4109                --  Package declaration of generic package declaration. We need
4110                --  to recursively examine nested declarations.
4111
4112                elsif Nkind_In (Decl, N_Package_Declaration,
4113                                      N_Generic_Package_Declaration)
4114                then
4115                   Check_Declarations (Specification (Decl));
4116
4117                elsif Nkind (Decl) = N_Pragma
4118                  and then Pragma_Name (Decl) = Name_Import
4119                then
4120                   Check_Pragma_Import (Decl);
4121                end if;
4122
4123                Next (Decl);
4124             end loop;
4125
4126             --  Same set of tests for private part. In addition to subprograms
4127             --  detect the presence of Taft Amendment types (incomplete types
4128             --  completed in the body).
4129
4130             Decl := First (Private_Declarations (Spec));
4131             while Present (Decl) loop
4132                if Comes_From_Source (Decl)
4133                  and then (Nkind_In (Decl, N_Subprogram_Declaration,
4134                                            N_Subprogram_Renaming_Declaration,
4135                                            N_Generic_Subprogram_Declaration))
4136                then
4137                   Append_Elmt (Defining_Entity (Decl), Subp_List);
4138
4139                elsif Nkind_In (Decl, N_Package_Declaration,
4140                                      N_Generic_Package_Declaration)
4141                then
4142                   Check_Declarations (Specification (Decl));
4143
4144                --  Collect incomplete type declarations for separate pass
4145
4146                elsif Nkind (Decl) = N_Incomplete_Type_Declaration then
4147                   Append_Elmt (Decl, Incomplete_Decls);
4148
4149                elsif Nkind (Decl) = N_Pragma
4150                  and then Pragma_Name (Decl) = Name_Import
4151                then
4152                   Check_Pragma_Import (Decl);
4153                end if;
4154
4155                Next (Decl);
4156             end loop;
4157
4158             --  Now check incomplete declarations to locate Taft amendment
4159             --  types. This can be done by examining the defining identifiers
4160             --  of  type declarations without real semantic analysis.
4161
4162             declare
4163                Inc : Elmt_Id;
4164
4165             begin
4166                Inc := First_Elmt (Incomplete_Decls);
4167                while Present (Inc) loop
4168                   Decl := Next (Node (Inc));
4169                   while Present (Decl) loop
4170                      if Nkind (Decl) = N_Full_Type_Declaration
4171                        and then Chars (Defining_Identifier (Decl)) =
4172                                 Chars (Defining_Identifier (Node (Inc)))
4173                      then
4174                         exit;
4175                      end if;
4176
4177                      Next (Decl);
4178                   end loop;
4179
4180                   --  If no completion, this is a TAT, and a body is needed
4181
4182                   if No (Decl) then
4183                      Set_Body_Required (Library_Unit (N));
4184                      return;
4185                   end if;
4186
4187                   Next_Elmt (Inc);
4188                end loop;
4189             end;
4190
4191             --  Finally, check whether there are subprograms that still
4192             --  require a body, i.e. are not renamings or null.
4193
4194             if not Is_Empty_Elmt_List (Subp_List) then
4195                declare
4196                   Subp_Id : Elmt_Id;
4197                   Spec    : Node_Id;
4198
4199                begin
4200                   Subp_Id := First_Elmt (Subp_List);
4201                   Spec    := Parent (Node (Subp_Id));
4202
4203                   while Present (Subp_Id) loop
4204                      if Nkind (Parent (Spec))
4205                         = N_Subprogram_Renaming_Declaration
4206                      then
4207                         null;
4208
4209                      elsif Nkind (Spec) = N_Procedure_Specification
4210                        and then Null_Present (Spec)
4211                      then
4212                         null;
4213
4214                      else
4215                         Set_Body_Required (Library_Unit (N));
4216                         return;
4217                      end if;
4218
4219                      Next_Elmt (Subp_Id);
4220                   end loop;
4221                end;
4222             end if;
4223          end Check_Declarations;
4224
4225       --  Start of processing for Check_Body_Required
4226
4227       begin
4228          --  If this is an imported package (Java and CIL usage) no body is
4229          --  needed. Scan list of pragmas that may follow a compilation unit
4230          --  to look for a relevant pragma Import.
4231
4232          if Present (PA) then
4233             declare
4234                Prag : Node_Id;
4235
4236             begin
4237                Prag := First (PA);
4238                while Present (Prag) loop
4239                   if Nkind (Prag) = N_Pragma
4240                     and then Get_Pragma_Id (Prag) = Pragma_Import
4241                   then
4242                      return;
4243                   end if;
4244
4245                   Next (Prag);
4246                end loop;
4247             end;
4248          end if;
4249
4250          Check_Declarations (Specification (P_Unit));
4251       end Check_Body_Required;
4252
4253       -----------------------------
4254       -- Has_Limited_With_Clause --
4255       -----------------------------
4256
4257       function Has_Limited_With_Clause
4258         (C_Unit : Entity_Id;
4259          Pack   : Entity_Id) return Boolean
4260       is
4261          Par      : Entity_Id;
4262          Par_Unit : Node_Id;
4263
4264       begin
4265          Par := C_Unit;
4266          while Present (Par) loop
4267             if Ekind (Par) /= E_Package then
4268                exit;
4269             end if;
4270
4271             --  Retrieve the Compilation_Unit node for Par and determine if
4272             --  its context clauses contain a limited with for Pack.
4273
4274             Par_Unit := Parent (Parent (Parent (Par)));
4275
4276             if Nkind (Par_Unit) = N_Package_Declaration then
4277                Par_Unit := Parent (Par_Unit);
4278             end if;
4279
4280             if Has_With_Clause (Par_Unit, Pack, True) then
4281                return True;
4282             end if;
4283
4284             --  If there are more ancestors, climb up the tree, otherwise
4285             --  we are done.
4286
4287             if Is_Child_Unit (Par) then
4288                Par := Scope (Par);
4289             else
4290                exit;
4291             end if;
4292          end loop;
4293
4294          return False;
4295       end Has_Limited_With_Clause;
4296
4297       ----------------------------------
4298       -- Is_Visible_Through_Renamings --
4299       ----------------------------------
4300
4301       function Is_Visible_Through_Renamings (P : Entity_Id) return Boolean is
4302          Kind     : constant Node_Kind :=
4303                       Nkind (Unit (Cunit (Current_Sem_Unit)));
4304          Aux_Unit : Node_Id;
4305          Item     : Node_Id;
4306          Decl     : Entity_Id;
4307
4308       begin
4309          --  Example of the error detected by this subprogram:
4310
4311          --  package P is
4312          --    type T is ...
4313          --  end P;
4314
4315          --  with P;
4316          --  package Q is
4317          --     package Ren_P renames P;
4318          --  end Q;
4319
4320          --  with Q;
4321          --  package R is ...
4322
4323          --  limited with P; -- ERROR
4324          --  package R.C is ...
4325
4326          Aux_Unit := Cunit (Current_Sem_Unit);
4327
4328          loop
4329             Item := First (Context_Items (Aux_Unit));
4330             while Present (Item) loop
4331                if Nkind (Item) = N_With_Clause
4332                  and then not Limited_Present (Item)
4333                  and then Nkind (Unit (Library_Unit (Item))) =
4334                                                   N_Package_Declaration
4335                then
4336                   Decl :=
4337                     First (Visible_Declarations
4338                             (Specification (Unit (Library_Unit (Item)))));
4339                   while Present (Decl) loop
4340                      if Nkind (Decl) = N_Package_Renaming_Declaration
4341                        and then Entity (Name (Decl)) = P
4342                      then
4343                         --  Generate the error message only if the current unit
4344                         --  is a package declaration; in case of subprogram
4345                         --  bodies and package bodies we just return True to
4346                         --  indicate that the limited view must not be
4347                         --  installed.
4348
4349                         if Kind = N_Package_Declaration then
4350                            Error_Msg_N
4351                              ("simultaneous visibility of the limited and " &
4352                               "unlimited views not allowed", N);
4353                            Error_Msg_Sloc := Sloc (Item);
4354                            Error_Msg_NE
4355                              ("\\  unlimited view of & visible through the " &
4356                               "context clause #", N, P);
4357                            Error_Msg_Sloc := Sloc (Decl);
4358                            Error_Msg_NE ("\\  and the renaming #", N, P);
4359                         end if;
4360
4361                         return True;
4362                      end if;
4363
4364                      Next (Decl);
4365                   end loop;
4366                end if;
4367
4368                Next (Item);
4369             end loop;
4370
4371             --  If it's a body not acting as spec, follow pointer to
4372             --  corresponding spec, otherwise follow pointer to parent spec.
4373
4374             if Present (Library_Unit (Aux_Unit))
4375               and then Nkind_In (Unit (Aux_Unit),
4376                                  N_Package_Body, N_Subprogram_Body)
4377             then
4378                if Aux_Unit = Library_Unit (Aux_Unit) then
4379
4380                   --  Aux_Unit is a body that acts as a spec. Clause has
4381                   --  already been flagged as illegal.
4382
4383                   return False;
4384
4385                else
4386                   Aux_Unit := Library_Unit (Aux_Unit);
4387                end if;
4388
4389             else
4390                Aux_Unit := Parent_Spec (Unit (Aux_Unit));
4391             end if;
4392
4393             exit when No (Aux_Unit);
4394          end loop;
4395
4396          return False;
4397       end Is_Visible_Through_Renamings;
4398
4399    --  Start of processing for Install_Limited_Withed_Unit
4400
4401    begin
4402       pragma Assert (not Limited_View_Installed (N));
4403
4404       --  In case of limited with_clause on subprograms, generics, instances,
4405       --  or renamings, the corresponding error was previously posted and we
4406       --  have nothing to do here. If the file is missing altogether, it has
4407       --  no source location.
4408
4409       if Nkind (P_Unit) /= N_Package_Declaration
4410         or else Sloc (P_Unit) = No_Location
4411       then
4412          return;
4413       end if;
4414
4415       P := Defining_Unit_Name (Specification (P_Unit));
4416
4417       --  Handle child packages
4418
4419       if Nkind (P) = N_Defining_Program_Unit_Name then
4420          Is_Child_Package := True;
4421          P := Defining_Identifier (P);
4422       end if;
4423
4424       --  Do not install the limited-view if the context of the unit is already
4425       --  available through a regular with clause.
4426
4427       if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4428         and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4429       then
4430          return;
4431       end if;
4432
4433       --  Do not install the limited-view if the full-view is already visible
4434       --  through renaming declarations.
4435
4436       if Is_Visible_Through_Renamings (P) then
4437          return;
4438       end if;
4439
4440       --  Do not install the limited view if this is the unit being analyzed.
4441       --  This unusual case will happen when a unit has a limited_with clause
4442       --  on one of its children. The compilation of the child forces the
4443       --  load of the parent which tries to install the limited view of the
4444       --  child again. Installing the limited view must also be disabled
4445       --  when compiling the body of the child unit.
4446
4447       if P = Cunit_Entity (Current_Sem_Unit)
4448         or else
4449          (Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4450             and then P = Main_Unit_Entity)
4451       then
4452          return;
4453       end if;
4454
4455       --  This scenario is similar to the one above, the difference is that
4456       --  the compilation of sibling Par.Sib forces the load of parent Par
4457       --  which tries to install the limited view of Lim_Pack [1]. However
4458       --  Par.Sib has a with clause for Lim_Pack [2] in its body, and thus
4459       --  needs the non-limited views of all entities from Lim_Pack.
4460
4461       --     limited with Lim_Pack;   --  [1]
4462       --     package Par is ...           package Lim_Pack is ...
4463
4464       --                                  with Lim_Pack;  --  [2]
4465       --     package Par.Sib is ...       package body Par.Sib is ...
4466
4467       --  In this case Main_Unit_Entity is the spec of Par.Sib and Current_
4468       --  Sem_Unit is the body of Par.Sib.
4469
4470       if Ekind (P) = E_Package
4471         and then Ekind (Main_Unit_Entity) = E_Package
4472         and then Is_Child_Unit (Main_Unit_Entity)
4473
4474          --  The body has a regular with clause
4475
4476         and then Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body
4477         and then Has_With_Clause (Cunit (Current_Sem_Unit), P)
4478
4479          --  One of the ancestors has a limited with clause
4480
4481         and then Nkind (Parent (Parent (Main_Unit_Entity))) =
4482                    N_Package_Specification
4483         and then Has_Limited_With_Clause (Scope (Main_Unit_Entity), P)
4484       then
4485          return;
4486       end if;
4487
4488       --  A common use of the limited-with is to have a limited-with
4489       --  in the package spec, and a normal with in its package body.
4490       --  For example:
4491
4492       --       limited with X;  -- [1]
4493       --       package A is ...
4494
4495       --       with X;          -- [2]
4496       --       package body A is ...
4497
4498       --  The compilation of A's body installs the context clauses found at [2]
4499       --  and then the context clauses of its specification (found at [1]). As
4500       --  a consequence, at [1] the specification of X has been analyzed and it
4501       --  is immediately visible. According to the semantics of limited-with
4502       --  context clauses we don't install the limited view because the full
4503       --  view of X supersedes its limited view.
4504
4505       if Analyzed (P_Unit)
4506         and then
4507           (Is_Immediately_Visible (P)
4508             or else
4509               (Is_Child_Package and then Is_Visible_Child_Unit (P)))
4510       then
4511          return;
4512       end if;
4513
4514       if Debug_Flag_I then
4515          Write_Str ("install limited view of ");
4516          Write_Name (Chars (P));
4517          Write_Eol;
4518       end if;
4519
4520       --  If the unit has not been analyzed and the limited view has not been
4521       --  already installed then we install it.
4522
4523       if not Analyzed (P_Unit) then
4524          if not In_Chain (P) then
4525
4526             --  Minimum decoration
4527
4528             Set_Ekind (P, E_Package);
4529             Set_Etype (P, Standard_Void_Type);
4530             Set_Scope (P, Standard_Standard);
4531
4532             if Is_Child_Package then
4533                Set_Is_Child_Unit (P);
4534                Set_Is_Visible_Child_Unit (P);
4535                Set_Scope (P, Defining_Entity (Unit (Parent_Spec (P_Unit))));
4536             end if;
4537
4538             --  Place entity on visibility structure
4539
4540             Set_Homonym (P, Current_Entity (P));
4541             Set_Current_Entity (P);
4542
4543             if Debug_Flag_I then
4544                Write_Str ("   (homonym) chain ");
4545                Write_Name (Chars (P));
4546                Write_Eol;
4547             end if;
4548
4549             --  Install the incomplete view. The first element of the limited
4550             --  view is a header (an E_Package entity) used to reference the
4551             --  first shadow entity in the private part of the package.
4552
4553             Lim_Header := Limited_View (P);
4554             Lim_Typ    := First_Entity (Lim_Header);
4555
4556             while Present (Lim_Typ)
4557               and then Lim_Typ /= First_Private_Entity (Lim_Header)
4558             loop
4559                Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
4560                Set_Current_Entity (Lim_Typ);
4561
4562                if Debug_Flag_I then
4563                   Write_Str ("   (homonym) chain ");
4564                   Write_Name (Chars (Lim_Typ));
4565                   Write_Eol;
4566                end if;
4567
4568                Next_Entity (Lim_Typ);
4569             end loop;
4570          end if;
4571
4572       --  If the unit appears in a previous regular with_clause, the regular
4573       --  entities of the public part of the withed package must be replaced
4574       --  by the shadow ones.
4575
4576       --  This code must be kept synchronized with the code that replaces the
4577       --  shadow entities by the real entities (see body of Remove_Limited
4578       --  With_Clause); otherwise the contents of the homonym chains are not
4579       --  consistent.
4580
4581       else
4582          --  Hide all the type entities of the public part of the package to
4583          --  avoid its usage. This is needed to cover all the subtype decla-
4584          --  rations because we do not remove them from the homonym chain.
4585
4586          E := First_Entity (P);
4587          while Present (E) and then E /= First_Private_Entity (P) loop
4588             if Is_Type (E) then
4589                Set_Was_Hidden (E, Is_Hidden (E));
4590                Set_Is_Hidden (E);
4591             end if;
4592
4593             Next_Entity (E);
4594          end loop;
4595
4596          --  Replace the real entities by the shadow entities of the limited
4597          --  view. The first element of the limited view is a header that is
4598          --  used to reference the first shadow entity in the private part
4599          --  of the package. Successive elements are the limited views of the
4600          --  type (including regular incomplete types) declared in the package.
4601
4602          Lim_Header := Limited_View (P);
4603
4604          Lim_Typ := First_Entity (Lim_Header);
4605          while Present (Lim_Typ)
4606            and then Lim_Typ /= First_Private_Entity (Lim_Header)
4607          loop
4608             pragma Assert (not In_Chain (Lim_Typ));
4609
4610             --  Do not unchain nested packages and child units
4611
4612             if Ekind (Lim_Typ) /= E_Package
4613               and then not Is_Child_Unit (Lim_Typ)
4614             then
4615                declare
4616                   Prev : Entity_Id;
4617
4618                begin
4619                   Prev := Current_Entity (Lim_Typ);
4620                   E := Prev;
4621
4622                   --  Replace E in the homonyms list, so that the limited
4623                   --  view becomes available.
4624
4625                   if E = Non_Limited_View (Lim_Typ) then
4626                      Set_Homonym (Lim_Typ, Homonym (Prev));
4627                      Set_Current_Entity (Lim_Typ);
4628
4629                   else
4630                      loop
4631                         E := Homonym (Prev);
4632
4633                         --  E may have been removed when installing a
4634                         --  previous limited_with_clause.
4635
4636                         exit when No (E);
4637
4638                         exit when E = Non_Limited_View (Lim_Typ);
4639
4640                         Prev := Homonym (Prev);
4641                      end loop;
4642
4643                      if Present (E) then
4644                         Set_Homonym (Lim_Typ, Homonym (Homonym (Prev)));
4645                         Set_Homonym (Prev, Lim_Typ);
4646                      end if;
4647                   end if;
4648                end;
4649
4650                if Debug_Flag_I then
4651                   Write_Str ("   (homonym) chain ");
4652                   Write_Name (Chars (Lim_Typ));
4653                   Write_Eol;
4654                end if;
4655             end if;
4656
4657             Next_Entity (Lim_Typ);
4658          end loop;
4659       end if;
4660
4661       --  The package must be visible while the limited-with clause is active
4662       --  because references to the type P.T must resolve in the usual way.
4663       --  In addition, we remember that the limited-view has been installed to
4664       --  uninstall it at the point of context removal.
4665
4666       Set_Is_Immediately_Visible (P);
4667       Set_Limited_View_Installed (N);
4668
4669       --  If unit has not been analyzed in some previous context, check
4670       --  (imperfectly ???) whether it might need a body.
4671
4672       if not Analyzed (P_Unit) then
4673          Check_Body_Required;
4674       end if;
4675
4676       --  If the package in the limited_with clause is a child unit, the
4677       --  clause is unanalyzed and appears as a selected component. Recast
4678       --  it as an expanded name so that the entity can be properly set. Use
4679       --  entity of parent, if available, for higher ancestors in the name.
4680
4681       if Nkind (Name (N)) = N_Selected_Component then
4682          declare
4683             Nam : Node_Id;
4684             Ent : Entity_Id;
4685
4686          begin
4687             Nam := Name (N);
4688             Ent := P;
4689             while Nkind (Nam) = N_Selected_Component
4690               and then Present (Ent)
4691             loop
4692                Change_Selected_Component_To_Expanded_Name (Nam);
4693
4694                --  Set entity of parent identifiers if the unit is a child
4695                --  unit. This ensures that the tree is properly formed from
4696                --  semantic point of view (e.g. for ASIS queries).
4697
4698                Set_Entity (Nam, Ent);
4699
4700                Nam := Prefix (Nam);
4701                Ent := Scope (Ent);
4702
4703                --  Set entity of last ancestor
4704
4705                if Nkind (Nam) = N_Identifier then
4706                   Set_Entity (Nam, Ent);
4707                end if;
4708             end loop;
4709          end;
4710       end if;
4711
4712       Set_Entity (Name (N), P);
4713       Set_From_With_Type (P);
4714    end Install_Limited_Withed_Unit;
4715
4716    -------------------------
4717    -- Install_Withed_Unit --
4718    -------------------------
4719
4720    procedure Install_Withed_Unit
4721      (With_Clause     : Node_Id;
4722       Private_With_OK : Boolean := False)
4723    is
4724       Uname : constant Entity_Id := Entity (Name (With_Clause));
4725       P     : constant Entity_Id := Scope (Uname);
4726
4727    begin
4728       --  Ada 2005 (AI-262): Do not install the private withed unit if we are
4729       --  compiling a package declaration and the Private_With_OK flag was not
4730       --  set by the caller. These declarations will be installed later (before
4731       --  analyzing the private part of the package).
4732
4733       if Private_Present (With_Clause)
4734         and then Nkind (Unit (Parent (With_Clause))) = N_Package_Declaration
4735         and then not (Private_With_OK)
4736       then
4737          return;
4738       end if;
4739
4740       if Debug_Flag_I then
4741          if Private_Present (With_Clause) then
4742             Write_Str ("install private withed unit ");
4743          else
4744             Write_Str ("install withed unit ");
4745          end if;
4746
4747          Write_Name (Chars (Uname));
4748          Write_Eol;
4749       end if;
4750
4751       --  We do not apply the restrictions to an internal unit unless
4752       --  we are compiling the internal unit as a main unit. This check
4753       --  is also skipped for dummy units (for missing packages).
4754
4755       if Sloc (Uname) /= No_Location
4756         and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
4757                     or else Current_Sem_Unit = Main_Unit)
4758       then
4759          Check_Restricted_Unit
4760            (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
4761       end if;
4762
4763       if P /= Standard_Standard then
4764
4765          --  If the unit is not analyzed after analysis of the with clause and
4766          --  it is an instantiation then it awaits a body and is the main unit.
4767          --  Its appearance in the context of some other unit indicates a
4768          --  circular dependency (DEC suite perversity).
4769
4770          if not Analyzed (Uname)
4771            and then Nkind (Parent (Uname)) = N_Package_Instantiation
4772          then
4773             Error_Msg_N
4774               ("instantiation depends on itself", Name (With_Clause));
4775
4776          elsif not Is_Visible_Child_Unit (Uname) then
4777             Set_Is_Visible_Child_Unit (Uname);
4778
4779             --  If the child unit appears in the context of its parent, it is
4780             --  immediately visible.
4781
4782             if In_Open_Scopes (Scope (Uname)) then
4783                Set_Is_Immediately_Visible (Uname);
4784             end if;
4785
4786             if Is_Generic_Instance (Uname)
4787               and then Ekind (Uname) in Subprogram_Kind
4788             then
4789                --  Set flag as well on the visible entity that denotes the
4790                --  instance, which renames the current one.
4791
4792                Set_Is_Visible_Child_Unit
4793                  (Related_Instance
4794                    (Defining_Entity (Unit (Library_Unit (With_Clause)))));
4795             end if;
4796
4797             --  The parent unit may have been installed already, and may have
4798             --  appeared in a use clause.
4799
4800             if In_Use (Scope (Uname)) then
4801                Set_Is_Potentially_Use_Visible (Uname);
4802             end if;
4803
4804             Set_Context_Installed (With_Clause);
4805          end if;
4806
4807       elsif not Is_Immediately_Visible (Uname) then
4808          if not Private_Present (With_Clause)
4809            or else Private_With_OK
4810          then
4811             Set_Is_Immediately_Visible (Uname);
4812          end if;
4813
4814          Set_Context_Installed (With_Clause);
4815       end if;
4816
4817       --   A with-clause overrides a with-type clause: there are no restric-
4818       --   tions on the use of package entities.
4819
4820       if Ekind (Uname) = E_Package then
4821          Set_From_With_Type (Uname, False);
4822       end if;
4823
4824       --  Ada 2005 (AI-377): it is illegal for a with_clause to name a child
4825       --  unit if there is a visible homograph for it declared in the same
4826       --  declarative region. This pathological case can only arise when an
4827       --  instance I1 of a generic unit G1 has an explicit child unit I1.G2,
4828       --  G1 has a generic child also named G2, and the context includes with_
4829       --  clauses for both I1.G2 and for G1.G2, making an implicit declaration
4830       --  of I1.G2 visible as well. If the child unit is named Standard, do
4831       --  not apply the check to the Standard package itself.
4832
4833       if Is_Child_Unit (Uname)
4834         and then Is_Visible_Child_Unit (Uname)
4835         and then Ada_Version >= Ada_05
4836       then
4837          declare
4838             Decl1 : constant Node_Id  := Unit_Declaration_Node (P);
4839             Decl2 : Node_Id;
4840             P2    : Entity_Id;
4841             U2    : Entity_Id;
4842
4843          begin
4844             U2 := Homonym (Uname);
4845             while Present (U2)
4846               and then U2 /= Standard_Standard
4847            loop
4848                P2 := Scope (U2);
4849                Decl2  := Unit_Declaration_Node (P2);
4850
4851                if Is_Child_Unit (U2)
4852                  and then Is_Visible_Child_Unit (U2)
4853                then
4854                   if Is_Generic_Instance (P)
4855                     and then Nkind (Decl1) = N_Package_Declaration
4856                     and then Generic_Parent (Specification (Decl1)) = P2
4857                   then
4858                      Error_Msg_N ("illegal with_clause", With_Clause);
4859                      Error_Msg_N
4860                        ("\child unit has visible homograph" &
4861                            " (RM 8.3(26), 10.1.1(19))",
4862                          With_Clause);
4863                      exit;
4864
4865                   elsif Is_Generic_Instance (P2)
4866                     and then Nkind (Decl2) = N_Package_Declaration
4867                     and then Generic_Parent (Specification (Decl2)) = P
4868                   then
4869                      --  With_clause for child unit of instance appears before
4870                      --  in the context. We want to place the error message on
4871                      --  it, not on the generic child unit itself.
4872
4873                      declare
4874                         Prev_Clause : Node_Id;
4875
4876                      begin
4877                         Prev_Clause := First (List_Containing (With_Clause));
4878                         while Entity (Name (Prev_Clause)) /= U2 loop
4879                            Next (Prev_Clause);
4880                         end loop;
4881
4882                         pragma Assert (Present (Prev_Clause));
4883                         Error_Msg_N ("illegal with_clause", Prev_Clause);
4884                         Error_Msg_N
4885                           ("\child unit has visible homograph" &
4886                               " (RM 8.3(26), 10.1.1(19))",
4887                             Prev_Clause);
4888                         exit;
4889                      end;
4890                   end if;
4891                end if;
4892
4893                U2 := Homonym (U2);
4894             end loop;
4895          end;
4896       end if;
4897    end Install_Withed_Unit;
4898
4899    -------------------
4900    -- Is_Child_Spec --
4901    -------------------
4902
4903    function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
4904       K : constant Node_Kind := Nkind (Lib_Unit);
4905
4906    begin
4907       return (K in N_Generic_Declaration              or else
4908               K in N_Generic_Instantiation            or else
4909               K in N_Generic_Renaming_Declaration     or else
4910               K =  N_Package_Declaration              or else
4911               K =  N_Package_Renaming_Declaration     or else
4912               K =  N_Subprogram_Declaration           or else
4913               K =  N_Subprogram_Renaming_Declaration)
4914         and then Present (Parent_Spec (Lib_Unit));
4915    end Is_Child_Spec;
4916
4917    ------------------------------------
4918    -- Is_Legal_Shadow_Entity_In_Body --
4919    ------------------------------------
4920
4921    function Is_Legal_Shadow_Entity_In_Body (T : Entity_Id) return Boolean is
4922       C_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
4923    begin
4924       return Nkind (Unit (C_Unit)) = N_Package_Body
4925         and then Has_With_Clause (C_Unit,
4926                    Cunit_Entity (Get_Source_Unit (Non_Limited_View (T))));
4927    end Is_Legal_Shadow_Entity_In_Body;
4928
4929    -----------------------
4930    -- Load_Needed_Body --
4931    -----------------------
4932
4933    --  N is a generic unit named in a with clause, or else it is a unit that
4934    --  contains a generic unit or an inlined function. In order to perform an
4935    --  instantiation, the body of the unit must be present. If the unit itself
4936    --  is generic, we assume that an instantiation follows, and load & analyze
4937    --  the body unconditionally. This forces analysis of the spec as well.
4938
4939    --  If the unit is not generic, but contains a generic unit, it is loaded on
4940    --  demand, at the point of instantiation (see ch12).
4941
4942    procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
4943       Body_Name : Unit_Name_Type;
4944       Unum      : Unit_Number_Type;
4945
4946       Save_Style_Check : constant Boolean := Opt.Style_Check;
4947       --  The loading and analysis is done with style checks off
4948
4949    begin
4950       if not GNAT_Mode then
4951          Style_Check := False;
4952       end if;
4953
4954       Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
4955       Unum :=
4956         Load_Unit
4957           (Load_Name  => Body_Name,
4958            Required   => False,
4959            Subunit    => False,
4960            Error_Node => N,
4961            Renamings  => True);
4962
4963       if Unum = No_Unit then
4964          OK := False;
4965
4966       else
4967          Compiler_State := Analyzing; -- reset after load
4968
4969          if not Fatal_Error (Unum) or else Try_Semantics then
4970             if Debug_Flag_L then
4971                Write_Str ("*** Loaded generic body");
4972                Write_Eol;
4973             end if;
4974
4975             Semantics (Cunit (Unum));
4976          end if;
4977
4978          OK := True;
4979       end if;
4980
4981       Style_Check := Save_Style_Check;
4982    end Load_Needed_Body;
4983
4984    -------------------------
4985    -- Build_Limited_Views --
4986    -------------------------
4987
4988    procedure Build_Limited_Views (N : Node_Id) is
4989       Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
4990       P    : constant Entity_Id        := Cunit_Entity (Unum);
4991
4992       Spec     : Node_Id;            --  To denote a package specification
4993       Lim_Typ  : Entity_Id;          --  To denote shadow entities
4994       Comp_Typ : Entity_Id;          --  To denote real entities
4995
4996       Lim_Header     : Entity_Id;          --  Package entity
4997       Last_Lim_E     : Entity_Id := Empty; --  Last limited entity built
4998       Last_Pub_Lim_E : Entity_Id;          --  To set the first private entity
4999
5000       procedure Decorate_Incomplete_Type
5001         (E    : Entity_Id;
5002          Scop : Entity_Id);
5003       --  Add attributes of an incomplete type to a shadow entity. The same
5004       --  attributes are placed on the real entity, so that gigi receives
5005       --  a consistent view.
5006
5007       procedure Decorate_Package_Specification (P : Entity_Id);
5008       --  Add attributes of a package entity to the entity in a package
5009       --  declaration
5010
5011       procedure Decorate_Tagged_Type
5012         (Loc  : Source_Ptr;
5013          T    : Entity_Id;
5014          Scop : Entity_Id);
5015       --  Set basic attributes of tagged type T, including its class_wide type.
5016       --  The parameters Loc, Scope are used to decorate the class_wide type.
5017
5018       procedure Build_Chain
5019         (Scope      : Entity_Id;
5020          First_Decl : Node_Id);
5021       --  Construct list of shadow entities and attach it to entity of
5022       --  package that is mentioned in a limited_with clause.
5023
5024       function New_Internal_Shadow_Entity
5025         (Kind       : Entity_Kind;
5026          Sloc_Value : Source_Ptr;
5027          Id_Char    : Character) return Entity_Id;
5028       --  Build a new internal entity and append it to the list of shadow
5029       --  entities available through the limited-header
5030
5031       ------------------------------
5032       -- Decorate_Incomplete_Type --
5033       ------------------------------
5034
5035       procedure Decorate_Incomplete_Type
5036         (E    : Entity_Id;
5037          Scop : Entity_Id)
5038       is
5039       begin
5040          Set_Ekind             (E, E_Incomplete_Type);
5041          Set_Scope             (E, Scop);
5042          Set_Etype             (E, E);
5043          Set_Is_First_Subtype  (E, True);
5044          Set_Stored_Constraint (E, No_Elist);
5045          Set_Full_View         (E, Empty);
5046          Init_Size_Align       (E);
5047       end Decorate_Incomplete_Type;
5048
5049       --------------------------
5050       -- Decorate_Tagged_Type --
5051       --------------------------
5052
5053       procedure Decorate_Tagged_Type
5054         (Loc  : Source_Ptr;
5055          T    : Entity_Id;
5056          Scop : Entity_Id)
5057       is
5058          CW : Entity_Id;
5059
5060       begin
5061          Decorate_Incomplete_Type (T, Scop);
5062          Set_Is_Tagged_Type (T);
5063
5064          --  Build corresponding class_wide type, if not previously done
5065
5066          --  Note: The class-wide entity is shared by the limited-view
5067          --  and the full-view.
5068
5069          if No (Class_Wide_Type (T)) then
5070             CW := Make_Defining_Identifier (Loc,  New_Internal_Name ('S'));
5071
5072             --  Set parent to be the same as the parent of the tagged type.
5073             --  We need a parent field set, and it is supposed to point to
5074             --  the declaration of the type. The tagged type declaration
5075             --  essentially declares two separate types, the tagged type
5076             --  itself and the corresponding class-wide type, so it is
5077             --  reasonable for the parent fields to point to the declaration
5078             --  in both cases.
5079
5080             Set_Parent (CW, Parent (T));
5081
5082             --  Set remaining fields of classwide type
5083
5084             Set_Ekind                     (CW, E_Class_Wide_Type);
5085             Set_Etype                     (CW, T);
5086             Set_Scope                     (CW, Scop);
5087             Set_Is_Tagged_Type            (CW);
5088             Set_Is_First_Subtype          (CW, True);
5089             Init_Size_Align               (CW);
5090             Set_Has_Unknown_Discriminants (CW, True);
5091             Set_Class_Wide_Type           (CW, CW);
5092             Set_Equivalent_Type           (CW, Empty);
5093             Set_From_With_Type            (CW, From_With_Type (T));
5094
5095             --  Link type to its class-wide type
5096
5097             Set_Class_Wide_Type           (T, CW);
5098          end if;
5099       end Decorate_Tagged_Type;
5100
5101       ------------------------------------
5102       -- Decorate_Package_Specification --
5103       ------------------------------------
5104
5105       procedure Decorate_Package_Specification (P : Entity_Id) is
5106       begin
5107          --  Place only the most basic attributes
5108
5109          Set_Ekind (P, E_Package);
5110          Set_Etype (P, Standard_Void_Type);
5111       end Decorate_Package_Specification;
5112
5113       --------------------------------
5114       -- New_Internal_Shadow_Entity --
5115       --------------------------------
5116
5117       function New_Internal_Shadow_Entity
5118         (Kind       : Entity_Kind;
5119          Sloc_Value : Source_Ptr;
5120          Id_Char    : Character) return Entity_Id
5121       is
5122          E : constant Entity_Id :=
5123                Make_Defining_Identifier (Sloc_Value,
5124                  Chars => New_Internal_Name (Id_Char));
5125
5126       begin
5127          Set_Ekind       (E, Kind);
5128          Set_Is_Internal (E, True);
5129
5130          if Kind in Type_Kind then
5131             Init_Size_Align (E);
5132          end if;
5133
5134          Append_Entity (E, Lim_Header);
5135          Last_Lim_E := E;
5136          return E;
5137       end New_Internal_Shadow_Entity;
5138
5139       -----------------
5140       -- Build_Chain --
5141       -----------------
5142
5143       procedure Build_Chain
5144         (Scope         : Entity_Id;
5145          First_Decl    : Node_Id)
5146       is
5147          Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
5148          Is_Tagged     : Boolean;
5149          Decl          : Node_Id;
5150
5151       begin
5152          Decl := First_Decl;
5153          while Present (Decl) loop
5154
5155             --  For each library_package_declaration in the environment, there
5156             --  is an implicit declaration of a *limited view* of that library
5157             --  package. The limited view of a package contains:
5158
5159             --   * For each nested package_declaration, a declaration of the
5160             --     limited view of that package, with the same defining-
5161             --     program-unit name.
5162
5163             --   * For each type_declaration in the visible part, an incomplete
5164             --     type-declaration with the same defining_identifier, whose
5165             --     completion is the type_declaration. If the type_declaration
5166             --     is tagged, then the incomplete_type_declaration is tagged
5167             --     incomplete.
5168
5169             --     The partial view is tagged if the declaration has the
5170             --     explicit keyword, or else if it is a type extension, both
5171             --     of which can be ascertained syntactically.
5172
5173             if Nkind (Decl) = N_Full_Type_Declaration then
5174                Is_Tagged :=
5175                   (Nkind (Type_Definition (Decl)) = N_Record_Definition
5176                     and then Tagged_Present (Type_Definition (Decl)))
5177                  or else
5178                    (Nkind (Type_Definition (Decl)) = N_Derived_Type_Definition
5179                      and then
5180                        Present
5181                          (Record_Extension_Part (Type_Definition (Decl))));
5182
5183                Comp_Typ := Defining_Identifier (Decl);
5184
5185                if not Analyzed_Unit then
5186                   if Is_Tagged then
5187                      Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5188                   else
5189                      Decorate_Incomplete_Type (Comp_Typ, Scope);
5190                   end if;
5191                end if;
5192
5193                --  Create shadow entity for type
5194
5195                Lim_Typ := New_Internal_Shadow_Entity
5196                  (Kind       => Ekind (Comp_Typ),
5197                   Sloc_Value => Sloc (Comp_Typ),
5198                   Id_Char    => 'Z');
5199
5200                Set_Chars  (Lim_Typ, Chars (Comp_Typ));
5201                Set_Parent (Lim_Typ, Parent (Comp_Typ));
5202                Set_From_With_Type (Lim_Typ);
5203
5204                if Is_Tagged then
5205                   Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5206                else
5207                   Decorate_Incomplete_Type (Lim_Typ, Scope);
5208                end if;
5209
5210                Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5211
5212             elsif Nkind_In (Decl, N_Private_Type_Declaration,
5213                                   N_Incomplete_Type_Declaration,
5214                                   N_Task_Type_Declaration,
5215                                   N_Protected_Type_Declaration)
5216             then
5217                Comp_Typ := Defining_Identifier (Decl);
5218
5219                Is_Tagged :=
5220                  Nkind_In (Decl, N_Private_Type_Declaration,
5221                                  N_Incomplete_Type_Declaration)
5222                  and then Tagged_Present (Decl);
5223
5224                if not Analyzed_Unit then
5225                   if Is_Tagged then
5226                      Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5227                   else
5228                      Decorate_Incomplete_Type (Comp_Typ, Scope);
5229                   end if;
5230                end if;
5231
5232                Lim_Typ  := New_Internal_Shadow_Entity
5233                  (Kind       => Ekind (Comp_Typ),
5234                   Sloc_Value => Sloc (Comp_Typ),
5235                   Id_Char    => 'Z');
5236
5237                Set_Chars  (Lim_Typ, Chars (Comp_Typ));
5238                Set_Parent (Lim_Typ, Parent (Comp_Typ));
5239                Set_From_With_Type (Lim_Typ);
5240
5241                if Is_Tagged then
5242                   Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5243                else
5244                   Decorate_Incomplete_Type (Lim_Typ, Scope);
5245                end if;
5246
5247                Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5248
5249             elsif Nkind (Decl) = N_Private_Extension_Declaration then
5250                Comp_Typ := Defining_Identifier (Decl);
5251
5252                if not Analyzed_Unit then
5253                   Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
5254                end if;
5255
5256                --  Create shadow entity for type
5257
5258                Lim_Typ := New_Internal_Shadow_Entity
5259                  (Kind       => Ekind (Comp_Typ),
5260                   Sloc_Value => Sloc (Comp_Typ),
5261                   Id_Char    => 'Z');
5262
5263                Set_Chars  (Lim_Typ, Chars (Comp_Typ));
5264                Set_Parent (Lim_Typ, Parent (Comp_Typ));
5265                Set_From_With_Type (Lim_Typ);
5266
5267                Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
5268                Set_Non_Limited_View (Lim_Typ, Comp_Typ);
5269
5270             elsif Nkind (Decl) = N_Package_Declaration then
5271
5272                --  Local package
5273
5274                declare
5275                   Spec : constant Node_Id := Specification (Decl);
5276
5277                begin
5278                   Comp_Typ := Defining_Unit_Name (Spec);
5279
5280                   if not Analyzed (Cunit (Unum)) then
5281                      Decorate_Package_Specification (Comp_Typ);
5282                      Set_Scope (Comp_Typ, Scope);
5283                   end if;
5284
5285                   Lim_Typ  := New_Internal_Shadow_Entity
5286                     (Kind       => Ekind (Comp_Typ),
5287                      Sloc_Value => Sloc (Comp_Typ),
5288                      Id_Char    => 'Z');
5289
5290                   Decorate_Package_Specification (Lim_Typ);
5291                   Set_Scope (Lim_Typ, Scope);
5292
5293                   Set_Chars  (Lim_Typ, Chars (Comp_Typ));
5294                   Set_Parent (Lim_Typ, Parent (Comp_Typ));
5295                   Set_From_With_Type (Lim_Typ);
5296
5297                   --  Note: The non_limited_view attribute is not used
5298                   --  for local packages.
5299
5300                   Build_Chain
5301                     (Scope      => Lim_Typ,
5302                      First_Decl => First (Visible_Declarations (Spec)));
5303                end;
5304             end if;
5305
5306             Next (Decl);
5307          end loop;
5308       end Build_Chain;
5309
5310    --  Start of processing for Build_Limited_Views
5311
5312    begin
5313       pragma Assert (Limited_Present (N));
5314
5315       --  A library_item mentioned in a limited_with_clause is a package
5316       --  declaration, not a subprogram declaration, generic declaration,
5317       --  generic instantiation, or package renaming declaration.
5318
5319       case Nkind (Unit (Library_Unit (N))) is
5320          when N_Package_Declaration =>
5321             null;
5322
5323          when N_Subprogram_Declaration =>
5324             Error_Msg_N ("subprograms not allowed in "
5325                          & "limited with_clauses", N);
5326             return;
5327
5328          when N_Generic_Package_Declaration |
5329               N_Generic_Subprogram_Declaration =>
5330             Error_Msg_N ("generics not allowed in "
5331                          & "limited with_clauses", N);
5332             return;
5333
5334          when N_Generic_Instantiation =>
5335             Error_Msg_N ("generic instantiations not allowed in "
5336                          & "limited with_clauses", N);
5337             return;
5338
5339          when N_Generic_Renaming_Declaration =>
5340             Error_Msg_N ("generic renamings not allowed in "
5341                          & "limited with_clauses", N);
5342             return;
5343
5344          when N_Subprogram_Renaming_Declaration =>
5345             Error_Msg_N ("renamed subprograms not allowed in "
5346                          & "limited with_clauses", N);
5347             return;
5348
5349          when N_Package_Renaming_Declaration =>
5350             Error_Msg_N ("renamed packages not allowed in "
5351                          & "limited with_clauses", N);
5352             return;
5353
5354          when others =>
5355             raise Program_Error;
5356       end case;
5357
5358       --  Check if the chain is already built
5359
5360       Spec := Specification (Unit (Library_Unit (N)));
5361
5362       if Limited_View_Installed (Spec) then
5363          return;
5364       end if;
5365
5366       Set_Ekind (P, E_Package);
5367
5368       --  Build the header of the limited_view
5369
5370       Lim_Header :=
5371         Make_Defining_Identifier (Sloc (N),
5372           Chars => New_Internal_Name (Id_Char => 'Z'));
5373       Set_Ekind (Lim_Header, E_Package);
5374       Set_Is_Internal (Lim_Header);
5375       Set_Limited_View (P, Lim_Header);
5376
5377       --  Create the auxiliary chain. All the shadow entities are appended to
5378       --  the list of entities of the limited-view header
5379
5380       Build_Chain
5381         (Scope      => P,
5382          First_Decl => First (Visible_Declarations (Spec)));
5383
5384       --  Save the last built shadow entity. It is needed later to set the
5385       --  reference to the first shadow entity in the private part
5386
5387       Last_Pub_Lim_E := Last_Lim_E;
5388
5389       --  Ada 2005 (AI-262): Add the limited view of the private declarations
5390       --  Required to give support to limited-private-with clauses
5391
5392       Build_Chain (Scope      => P,
5393                    First_Decl => First (Private_Declarations (Spec)));
5394
5395       if Last_Pub_Lim_E /= Empty then
5396          Set_First_Private_Entity (Lim_Header,
5397                                    Next_Entity (Last_Pub_Lim_E));
5398       else
5399          Set_First_Private_Entity (Lim_Header,
5400                                    First_Entity (P));
5401       end if;
5402
5403       Set_Limited_View_Installed (Spec);
5404    end Build_Limited_Views;
5405
5406    -------------------------------
5407    -- Check_Body_Needed_For_SAL --
5408    -------------------------------
5409
5410    procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
5411
5412       function Entity_Needs_Body (E : Entity_Id) return Boolean;
5413       --  Determine whether use of entity E might require the presence of its
5414       --  body. For a package this requires a recursive traversal of all nested
5415       --  declarations.
5416
5417       ---------------------------
5418       -- Entity_Needed_For_SAL --
5419       ---------------------------
5420
5421       function Entity_Needs_Body (E : Entity_Id) return Boolean is
5422          Ent : Entity_Id;
5423
5424       begin
5425          if Is_Subprogram (E)
5426            and then Has_Pragma_Inline (E)
5427          then
5428             return True;
5429
5430          elsif Ekind (E) = E_Generic_Function
5431            or else Ekind (E) = E_Generic_Procedure
5432          then
5433             return True;
5434
5435          elsif Ekind (E) = E_Generic_Package
5436            and then
5437              Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
5438            and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5439          then
5440             return True;
5441
5442          elsif Ekind (E) = E_Package
5443            and then
5444              Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
5445            and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
5446          then
5447             Ent := First_Entity (E);
5448             while Present (Ent) loop
5449                if Entity_Needs_Body (Ent) then
5450                   return True;
5451                end if;
5452
5453                Next_Entity (Ent);
5454             end loop;
5455
5456             return False;
5457
5458          else
5459             return False;
5460          end if;
5461       end Entity_Needs_Body;
5462
5463    --  Start of processing for Check_Body_Needed_For_SAL
5464
5465    begin
5466       if Ekind (Unit_Name) = E_Generic_Package
5467         and then
5468           Nkind (Unit_Declaration_Node (Unit_Name)) =
5469                                             N_Generic_Package_Declaration
5470         and then
5471           Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
5472       then
5473          Set_Body_Needed_For_SAL (Unit_Name);
5474
5475       elsif Ekind (Unit_Name) = E_Generic_Procedure
5476         or else Ekind (Unit_Name) = E_Generic_Function
5477       then
5478          Set_Body_Needed_For_SAL (Unit_Name);
5479
5480       elsif Is_Subprogram (Unit_Name)
5481         and then Nkind (Unit_Declaration_Node (Unit_Name)) =
5482                                             N_Subprogram_Declaration
5483         and then Has_Pragma_Inline (Unit_Name)
5484       then
5485          Set_Body_Needed_For_SAL (Unit_Name);
5486
5487       elsif Ekind (Unit_Name) = E_Subprogram_Body then
5488          Check_Body_Needed_For_SAL
5489            (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5490
5491       elsif Ekind (Unit_Name) = E_Package
5492         and then Entity_Needs_Body (Unit_Name)
5493       then
5494          Set_Body_Needed_For_SAL (Unit_Name);
5495
5496       elsif Ekind (Unit_Name) = E_Package_Body
5497         and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
5498       then
5499          Check_Body_Needed_For_SAL
5500            (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
5501       end if;
5502    end Check_Body_Needed_For_SAL;
5503
5504    --------------------
5505    -- Remove_Context --
5506    --------------------
5507
5508    procedure Remove_Context (N : Node_Id) is
5509       Lib_Unit : constant Node_Id := Unit (N);
5510
5511    begin
5512       --  If this is a child unit, first remove the parent units
5513
5514       if Is_Child_Spec (Lib_Unit) then
5515          Remove_Parents (Lib_Unit);
5516       end if;
5517
5518       Remove_Context_Clauses (N);
5519    end Remove_Context;
5520
5521    ----------------------------
5522    -- Remove_Context_Clauses --
5523    ----------------------------
5524
5525    procedure Remove_Context_Clauses (N : Node_Id) is
5526       Item      : Node_Id;
5527       Unit_Name : Entity_Id;
5528
5529    begin
5530       --  Ada 2005 (AI-50217): We remove the context clauses in two phases:
5531       --  limited-views first and regular-views later (to maintain the
5532       --  stack model).
5533
5534       --  First Phase: Remove limited_with context clauses
5535
5536       Item := First (Context_Items (N));
5537       while Present (Item) loop
5538
5539          --  We are interested only in with clauses which got installed
5540          --  on entry.
5541
5542          if Nkind (Item) = N_With_Clause
5543            and then Limited_Present (Item)
5544            and then Limited_View_Installed (Item)
5545          then
5546             Remove_Limited_With_Clause (Item);
5547          end if;
5548
5549          Next (Item);
5550       end loop;
5551
5552       --  Second Phase: Loop through context items and undo regular
5553       --  with_clauses and use_clauses.
5554
5555       Item := First (Context_Items (N));
5556       while Present (Item) loop
5557
5558          --  We are interested only in with clauses which got installed on
5559          --  entry, as indicated by their Context_Installed flag set
5560
5561          if Nkind (Item) = N_With_Clause
5562            and then Limited_Present (Item)
5563            and then Limited_View_Installed (Item)
5564          then
5565             null;
5566
5567          elsif Nkind (Item) = N_With_Clause
5568             and then Context_Installed (Item)
5569          then
5570             --  Remove items from one with'ed unit
5571
5572             Unit_Name := Entity (Name (Item));
5573             Remove_Unit_From_Visibility (Unit_Name);
5574             Set_Context_Installed (Item, False);
5575
5576          elsif Nkind (Item) = N_Use_Package_Clause then
5577             End_Use_Package (Item);
5578
5579          elsif Nkind (Item) = N_Use_Type_Clause then
5580             End_Use_Type (Item);
5581          end if;
5582
5583          Next (Item);
5584       end loop;
5585    end Remove_Context_Clauses;
5586
5587    --------------------------------
5588    -- Remove_Limited_With_Clause --
5589    --------------------------------
5590
5591    procedure Remove_Limited_With_Clause (N : Node_Id) is
5592       P_Unit     : constant Entity_Id := Unit (Library_Unit (N));
5593       E          : Entity_Id;
5594       P          : Entity_Id;
5595       Lim_Header : Entity_Id;
5596       Lim_Typ    : Entity_Id;
5597       Prev       : Entity_Id;
5598
5599    begin
5600       pragma Assert (Limited_View_Installed (N));
5601
5602       --  In case of limited with_clause on subprograms, generics, instances,
5603       --  or renamings, the corresponding error was previously posted and we
5604       --  have nothing to do here.
5605
5606       if Nkind (P_Unit) /= N_Package_Declaration then
5607          return;
5608       end if;
5609
5610       P := Defining_Unit_Name (Specification (P_Unit));
5611
5612       --  Handle child packages
5613
5614       if Nkind (P) = N_Defining_Program_Unit_Name then
5615          P := Defining_Identifier (P);
5616       end if;
5617
5618       if Debug_Flag_I then
5619          Write_Str ("remove limited view of ");
5620          Write_Name (Chars (P));
5621          Write_Str (" from visibility");
5622          Write_Eol;
5623       end if;
5624
5625       --  Prepare the removal of the shadow entities from visibility. The
5626       --  first element of the limited view is a header (an E_Package
5627       --  entity) that is used to reference the first shadow entity in the
5628       --  private part of the package
5629
5630       Lim_Header := Limited_View (P);
5631       Lim_Typ    := First_Entity (Lim_Header);
5632
5633       --  Remove package and shadow entities from visibility if it has not
5634       --  been analyzed
5635
5636       if not Analyzed (P_Unit) then
5637          Unchain (P);
5638          Set_Is_Immediately_Visible (P, False);
5639
5640          while Present (Lim_Typ) loop
5641             Unchain (Lim_Typ);
5642             Next_Entity (Lim_Typ);
5643          end loop;
5644
5645       --  Otherwise this package has already appeared in the closure and its
5646       --  shadow entities must be replaced by its real entities. This code
5647       --  must be kept synchronized with the complementary code in Install
5648       --  Limited_Withed_Unit.
5649
5650       else
5651          --  Real entities that are type or subtype declarations were hidden
5652          --  from visibility at the point of installation of the limited-view.
5653          --  Now we recover the previous value of the hidden attribute.
5654
5655          E := First_Entity (P);
5656          while Present (E) and then E /= First_Private_Entity (P) loop
5657             if Is_Type (E) then
5658                Set_Is_Hidden (E, Was_Hidden (E));
5659             end if;
5660
5661             Next_Entity (E);
5662          end loop;
5663
5664          while Present (Lim_Typ)
5665            and then Lim_Typ /= First_Private_Entity (Lim_Header)
5666          loop
5667             --  Nested packages and child units were not unchained
5668
5669             if Ekind (Lim_Typ) /= E_Package
5670               and then not Is_Child_Unit (Non_Limited_View (Lim_Typ))
5671             then
5672
5673                --  If the package has incomplete types, the limited view
5674                --  of the incomplete type is in fact never visible (AI05-129)
5675                --  but we have created a shadow entity E1 for it, that points
5676                --  to E2, a non-limited incomplete type. This in turn has a
5677                --  full view E3 that is the full declaration. There is a
5678                --  corresponding shadow entity E4. When reinstalling the
5679                --  non-limited view, E2 must become the current entity and
5680                --  E3 must be ignored.
5681
5682                E := Non_Limited_View (Lim_Typ);
5683
5684                if Present (Current_Entity (E))
5685                  and then Ekind (Current_Entity (E)) = E_Incomplete_Type
5686                  and then Full_View (Current_Entity (E)) = E
5687                then
5688
5689                   --  Lim_Typ is the limited view of a full type declaration
5690                   --  that has a previous incomplete declaration, i.e. E3
5691                   --  from the previous description. Nothing to insert.
5692
5693                   null;
5694
5695                else
5696                   pragma Assert (not In_Chain (E));
5697
5698                   Prev := Current_Entity (Lim_Typ);
5699
5700                   if Prev = Lim_Typ then
5701                      Set_Current_Entity (E);
5702
5703                   else
5704                      while Present (Prev)
5705                        and then Homonym (Prev) /= Lim_Typ
5706                      loop
5707                         Prev := Homonym (Prev);
5708                      end loop;
5709
5710                      if Present (Prev) then
5711                         Set_Homonym (Prev, E);
5712                      end if;
5713                   end if;
5714
5715                   --  Preserve structure of homonym chain.
5716
5717                   Set_Homonym (E, Homonym (Lim_Typ));
5718                end if;
5719             end if;
5720
5721             Next_Entity (Lim_Typ);
5722          end loop;
5723       end if;
5724
5725       --  Indicate that the limited view of the package is not installed
5726
5727       Set_From_With_Type         (P, False);
5728       Set_Limited_View_Installed (N, False);
5729    end Remove_Limited_With_Clause;
5730
5731    --------------------
5732    -- Remove_Parents --
5733    --------------------
5734
5735    procedure Remove_Parents (Lib_Unit : Node_Id) is
5736       P      : Node_Id;
5737       P_Name : Entity_Id;
5738       P_Spec : Node_Id := Empty;
5739       E      : Entity_Id;
5740       Vis    : constant Boolean :=
5741                  Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
5742
5743    begin
5744       if Is_Child_Spec (Lib_Unit) then
5745          P_Spec := Parent_Spec (Lib_Unit);
5746
5747       elsif Nkind (Lib_Unit) = N_Package_Body
5748         and then Nkind (Original_Node (Lib_Unit)) = N_Package_Instantiation
5749       then
5750          P_Spec := Parent_Spec (Original_Node (Lib_Unit));
5751       end if;
5752
5753       if Present (P_Spec) then
5754
5755          P := Unit (P_Spec);
5756          P_Name := Get_Parent_Entity (P);
5757          Remove_Context_Clauses (P_Spec);
5758          End_Package_Scope (P_Name);
5759          Set_Is_Immediately_Visible (P_Name, Vis);
5760
5761          --  Remove from visibility the siblings as well, which are directly
5762          --  visible while the parent is in scope.
5763
5764          E := First_Entity (P_Name);
5765          while Present (E) loop
5766             if Is_Child_Unit (E) then
5767                Set_Is_Immediately_Visible (E, False);
5768             end if;
5769
5770             Next_Entity (E);
5771          end loop;
5772
5773          Set_In_Package_Body (P_Name, False);
5774
5775          --  This is the recursive call to remove the context of any
5776          --  higher level parent. This recursion ensures that all parents
5777          --  are removed in the reverse order of their installation.
5778
5779          Remove_Parents (P);
5780       end if;
5781    end Remove_Parents;
5782
5783    ---------------------------------
5784    -- Remove_Private_With_Clauses --
5785    ---------------------------------
5786
5787    procedure Remove_Private_With_Clauses (Comp_Unit : Node_Id) is
5788       Item : Node_Id;
5789
5790       function In_Regular_With_Clause (E : Entity_Id) return Boolean;
5791       --  Check whether a given unit appears in a regular with_clause.
5792       --  Used to determine whether a private_with_clause, implicit or
5793       --  explicit, should be ignored.
5794
5795       ----------------------------
5796       -- In_Regular_With_Clause --
5797       ----------------------------
5798
5799       function In_Regular_With_Clause (E : Entity_Id) return Boolean
5800       is
5801          Item : Node_Id;
5802
5803       begin
5804          Item := First (Context_Items (Comp_Unit));
5805          while Present (Item) loop
5806             if Nkind (Item) = N_With_Clause
5807               and then Entity (Name (Item)) = E
5808               and then not Private_Present (Item)
5809             then
5810                return True;
5811             end if;
5812             Next (Item);
5813          end loop;
5814
5815          return False;
5816       end In_Regular_With_Clause;
5817
5818    --  Start of processing for Remove_Private_With_Clauses
5819
5820    begin
5821       Item := First (Context_Items (Comp_Unit));
5822       while Present (Item) loop
5823          if Nkind (Item) = N_With_Clause
5824            and then Private_Present (Item)
5825          then
5826             --  If private_with_clause is redundant, remove it from
5827             --  context, as a small optimization to subsequent handling
5828             --  of private_with clauses in other nested packages..
5829
5830             if In_Regular_With_Clause (Entity (Name (Item))) then
5831                declare
5832                   Nxt : constant Node_Id := Next (Item);
5833                begin
5834                   Remove (Item);
5835                   Item := Nxt;
5836                end;
5837
5838             elsif Limited_Present (Item) then
5839                if not Limited_View_Installed (Item) then
5840                   Remove_Limited_With_Clause (Item);
5841                end if;
5842
5843                Next (Item);
5844
5845             else
5846                Remove_Unit_From_Visibility (Entity (Name (Item)));
5847                Set_Context_Installed (Item, False);
5848                Next (Item);
5849             end if;
5850
5851          else
5852             Next (Item);
5853          end if;
5854       end loop;
5855    end Remove_Private_With_Clauses;
5856
5857    ---------------------------------
5858    -- Remove_Unit_From_Visibility --
5859    ---------------------------------
5860
5861    procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
5862       P : constant Entity_Id := Scope (Unit_Name);
5863
5864    begin
5865       if Debug_Flag_I then
5866          Write_Str ("remove unit ");
5867          Write_Name (Chars (Unit_Name));
5868          Write_Str (" from visibility");
5869          Write_Eol;
5870       end if;
5871
5872       if P /= Standard_Standard then
5873          Set_Is_Visible_Child_Unit (Unit_Name, False);
5874       end if;
5875
5876       Set_Is_Potentially_Use_Visible (Unit_Name, False);
5877       Set_Is_Immediately_Visible     (Unit_Name, False);
5878    end Remove_Unit_From_Visibility;
5879
5880    --------
5881    -- sm --
5882    --------
5883
5884    procedure sm is
5885    begin
5886       null;
5887    end sm;
5888
5889    -------------
5890    -- Unchain --
5891    -------------
5892
5893    procedure Unchain (E : Entity_Id) is
5894       Prev : Entity_Id;
5895
5896    begin
5897       Prev := Current_Entity (E);
5898
5899       if No (Prev) then
5900          return;
5901
5902       elsif Prev = E then
5903          Set_Name_Entity_Id (Chars (E), Homonym (E));
5904
5905       else
5906          while Present (Prev)
5907            and then Homonym (Prev) /= E
5908          loop
5909             Prev := Homonym (Prev);
5910          end loop;
5911
5912          if Present (Prev) then
5913             Set_Homonym (Prev, Homonym (E));
5914          end if;
5915       end if;
5916
5917       if Debug_Flag_I then
5918          Write_Str ("   (homonym) unchain ");
5919          Write_Name (Chars (E));
5920          Write_Eol;
5921       end if;
5922    end Unchain;
5923
5924 end Sem_Ch10;