OSDN Git Service

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