OSDN Git Service

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