OSDN Git Service

2004-05-10 Doug Rupp <rupp@gnat.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-2004 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,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, 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 Elists;   use Elists;
32 with Exp_Util; use Exp_Util;
33 with Fname;    use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze;   use Freeze;
36 with Impunit;  use Impunit;
37 with Inline;   use Inline;
38 with Lib;      use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Namet;    use Namet;
42 with Nlists;   use Nlists;
43 with Nmake;    use Nmake;
44 with Opt;      use Opt;
45 with Output;   use Output;
46 with Restrict; use Restrict;
47 with Sem;      use Sem;
48 with Sem_Ch6;  use Sem_Ch6;
49 with Sem_Ch7;  use Sem_Ch7;
50 with Sem_Ch8;  use Sem_Ch8;
51 with Sem_Dist; use Sem_Dist;
52 with Sem_Prag; use Sem_Prag;
53 with Sem_Util; use Sem_Util;
54 with Sem_Warn; use Sem_Warn;
55 with Stand;    use Stand;
56 with Sinfo;    use Sinfo;
57 with Sinfo.CN; use Sinfo.CN;
58 with Sinput;   use Sinput;
59 with Snames;   use Snames;
60 with Style;    use Style;
61 with Stylesw;  use Stylesw;
62 with Tbuild;   use Tbuild;
63 with Ttypes;   use Ttypes;
64 with Uname;    use Uname;
65
66 package body Sem_Ch10 is
67
68    -----------------------
69    -- Local Subprograms --
70    -----------------------
71
72    procedure Analyze_Context (N : Node_Id);
73    --  Analyzes items in the context clause of compilation unit
74
75    procedure Build_Limited_Views (N : Node_Id);
76    --  Build and decorate the list of shadow entities for a package mentioned
77    --  in a limited_with clause. If the package was not previously analyzed
78    --  then it also performs a basic decoration of the real entities; this
79    --  is required to do not pass non-decorated entities to the back-end.
80    --  Implements Ada0Y (AI-50217).
81
82    procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
83    --  Check whether the source for the body of a compilation unit must
84    --  be included in a standalone library.
85
86    procedure Check_With_Type_Clauses (N : Node_Id);
87    --  If N is a body, verify that any with_type clauses on the spec, or
88    --  on the spec of any parent, have a matching with_clause.
89
90    procedure Check_Private_Child_Unit (N : Node_Id);
91    --  If a with_clause mentions a private child unit, the compilation
92    --  unit must be a member of the same family, as described in 10.1.2 (8).
93
94    procedure Check_Stub_Level (N : Node_Id);
95    --  Verify that a stub is declared immediately within a compilation unit,
96    --  and not in an inner frame.
97
98    procedure Expand_Limited_With_Clause (Nam : Node_Id; N : Node_Id);
99    --  If a child unit appears in a limited_with clause, there are implicit
100    --  limited_with clauses on all parents that are not already visible
101    --  through a regular with clause. This procedure creates the implicit
102    --  limited with_clauses for the parents and loads the corresponding units.
103    --  The shadow entities are created when the inserted clause is analyzed.
104    --  Implements Ada0Y (AI-50217).
105
106    procedure Expand_With_Clause (Nam : Node_Id; N : Node_Id);
107    --  When a child unit appears in a context clause, the implicit withs on
108    --  parents are made explicit, and with clauses are inserted in the context
109    --  clause before the one for the child. If a parent in the with_clause
110    --  is a renaming, the implicit with_clause is on the renaming whose name
111    --  is mentioned in the with_clause, and not on the package it renames.
112    --  N is the compilation unit whose list of context items receives the
113    --  implicit with_clauses.
114
115    function Get_Parent_Entity (Unit : Node_Id) return Entity_Id;
116    --  Get defining entity of parent unit of a child unit. In most cases this
117    --  is the defining entity of the unit, but for a child instance whose
118    --  parent needs a body for inlining, the instantiation node of the parent
119    --  has not yet been rewritten as a package declaration, and the entity has
120    --  to be retrieved from the Instance_Spec of the unit.
121
122    procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
123    --  If the main unit is a child unit, implicit withs are also added for
124    --  all its ancestors.
125
126    procedure Install_Context_Clauses (N : Node_Id);
127    --  Subsidiary to previous one. Process only with_ and use_clauses for
128    --  current unit and its library unit if any.
129
130    procedure Install_Limited_Context_Clauses (N : Node_Id);
131    --  Subsidiary to Install_Context. Process only limited with_clauses
132    --  for current unit. Implements Ada0Y (AI-50217).
133
134    procedure Install_Limited_Withed_Unit (N : Node_Id);
135    --  Place shadow entities for a limited_with package in the visibility
136    --  structures for the current compilation. Implements Ada0Y (AI-50217).
137
138    procedure Install_Withed_Unit
139      (With_Clause     : Node_Id;
140       Private_With_OK : Boolean := False);
141
142    --  If the unit is not a child unit, make unit immediately visible.
143    --  The caller ensures that the unit is not already currently installed.
144    --  The flag Private_With_OK is set true in Install_Private_With_Clauses,
145    --  which is called when compiling the private part of a package, or
146    --  installing the private declarations of a parent unit.
147
148    procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean);
149    --  This procedure establishes the context for the compilation of a child
150    --  unit. If Lib_Unit is a child library spec then the context of the parent
151    --  is installed, and the parent itself made immediately visible, so that
152    --  the child unit is processed in the declarative region of the parent.
153    --  Install_Parents makes a recursive call to itself to ensure that all
154    --  parents are loaded in the nested case. If Lib_Unit is a library body,
155    --  the only effect of Install_Parents is to install the private decls of
156    --  the parents, because the visible parent declarations will have been
157    --  installed as part of the context of the corresponding spec.
158
159    procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id);
160    --  In the compilation of a child unit, a child of any of the  ancestor
161    --  units is directly visible if it is visible, because the parent is in
162    --  an enclosing scope. Iterate over context to find child units of U_Name
163    --  or of some ancestor of it.
164
165    function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
166    --  Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
167    --  returns True if Lib_Unit is a library spec which is a child spec, i.e.
168    --  a library spec that has a parent. If the call to Is_Child_Spec returns
169    --  True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
170    --  compilation unit for the parent spec.
171    --
172    --  Lib_Unit can also be a subprogram body that acts as its own spec. If
173    --  the Parent_Spec is  non-empty, this is also a child unit.
174
175    procedure Remove_With_Type_Clause (Name : Node_Id);
176    --  Remove imported type and its enclosing package from visibility, and
177    --  remove attributes of imported type so they don't interfere with its
178    --  analysis (should it appear otherwise in the context).
179
180    procedure Remove_Context_Clauses (N : Node_Id);
181    --  Subsidiary of previous one. Remove use_ and with_clauses.
182
183    procedure Remove_Limited_With_Clause (N : Node_Id);
184    --  Remove from visibility the shadow entities introduced for a package
185    --  mentioned in a limited_with clause. Implements Ada0Y (AI-50217).
186
187    procedure Remove_Parents (Lib_Unit : Node_Id);
188    --  Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
189    --  contexts established by the corresponding call to Install_Parents are
190    --  removed. Remove_Parents contains a recursive call to itself to ensure
191    --  that all parents are removed in the nested case.
192
193    procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id);
194    --  Reset all visibility flags on unit after compiling it, either as a
195    --  main unit or as a unit in the context.
196
197    procedure Unchain (E : Entity_Id);
198    --  Remove single entity from visibility list
199
200    procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
201    --  Common processing for all stubs (subprograms, tasks, packages, and
202    --  protected cases). N is the stub to be analyzed. Once the subunit
203    --  name is established, load and analyze. Nam is the non-overloadable
204    --  entity for which the proper body provides a completion. Subprogram
205    --  stubs are handled differently because they can be declarations.
206
207    --------------------------
208    -- Limited_With_Clauses --
209    --------------------------
210
211    --  Limited_With clauses are the mechanism chosen for Ada05 to support
212    --  mutually recursive types declared in different units. A limited_with
213    --  clause that names package P in the context of unit U makes the types
214    --  declared in the visible part of P available within U, but with the
215    --  restriction that these types can only be used as incomplete types.
216    --  The limited_with clause does not impose a semantic dependence on P,
217    --  and it is possible for two packages to have limited_with_clauses on
218    --  each other without creating an elaboration circularity.
219
220    --  To support this feature, the analysis of a limited_with clause must
221    --  create an abbreviated view of the package, without performing any
222    --  semantic analysis on it. This "package abstract" contains shadow
223    --  types that are in one-one correspondence with the real types in the
224    --  package, and that have the properties of incomplete types.
225
226    --  The implementation creates two element lists: one to chain the shadow
227    --  entities, and one to chain the corresponding type entities in the tree
228    --  of the package. Links between corresponding entities in both chains
229    --  allow the compiler to select the proper view of a given type, depending
230    --  on the context. Note that in contrast with the handling of private
231    --  types, the limited view and the non-limited view of a type are treated
232    --  as separate entities, and no entity exchange needs to take place, which
233    --  makes the implementation must simpler than could be feared.
234
235    ------------------------------
236    -- Analyze_Compilation_Unit --
237    ------------------------------
238
239    procedure Analyze_Compilation_Unit (N : Node_Id) is
240       Unit_Node     : constant Node_Id := Unit (N);
241       Lib_Unit      : Node_Id          := Library_Unit (N);
242       Spec_Id       : Node_Id;
243       Main_Cunit    : constant Node_Id := Cunit (Main_Unit);
244       Par_Spec_Name : Unit_Name_Type;
245       Unum          : Unit_Number_Type;
246
247       procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id);
248       --  Generate cross-reference information for the parents of child units.
249       --  N is a defining_program_unit_name, and P_Id is the immediate parent.
250
251       --------------------------------
252       -- Generate_Parent_References --
253       --------------------------------
254
255       procedure Generate_Parent_References (N : Node_Id; P_Id : Entity_Id) is
256          Pref   : Node_Id;
257          P_Name : Entity_Id := P_Id;
258
259       begin
260          Pref   := Name (Parent (Defining_Entity (N)));
261
262          if Nkind (Pref) = N_Expanded_Name then
263
264             --  Done already, if the unit has been compiled indirectly as
265             --  part of the closure of its context because of inlining.
266
267             return;
268          end if;
269
270          while Nkind (Pref) = N_Selected_Component loop
271             Change_Selected_Component_To_Expanded_Name (Pref);
272             Set_Entity (Pref, P_Name);
273             Set_Etype (Pref, Etype (P_Name));
274             Generate_Reference (P_Name, Pref, 'r');
275             Pref   := Prefix (Pref);
276             P_Name := Scope (P_Name);
277          end loop;
278
279          --  The guard here on P_Name is to handle the error condition where
280          --  the parent unit is missing because the file was not found.
281
282          if Present (P_Name) then
283             Set_Entity (Pref, P_Name);
284             Set_Etype (Pref, Etype (P_Name));
285             Generate_Reference (P_Name, Pref, 'r');
286             Style.Check_Identifier (Pref, P_Name);
287          end if;
288       end Generate_Parent_References;
289
290    --  Start of processing for Analyze_Compilation_Unit
291
292    begin
293       Process_Compilation_Unit_Pragmas (N);
294
295       --  If the unit is a subunit whose parent has not been analyzed (which
296       --  indicates that the main unit is a subunit, either the current one or
297       --  one of its descendents) then the subunit is compiled as part of the
298       --  analysis of the parent, which we proceed to do. Basically this gets
299       --  handled from the top down and we don't want to do anything at this
300       --  level (i.e. this subunit will be handled on the way down from the
301       --  parent), so at this level we immediately return. If the subunit
302       --  ends up not analyzed, it means that the parent did not contain a
303       --  stub for it, or that there errors were dectected in some ancestor.
304
305       if Nkind (Unit_Node) = N_Subunit
306         and then not Analyzed (Lib_Unit)
307       then
308          Semantics (Lib_Unit);
309
310          if not Analyzed (Proper_Body (Unit_Node)) then
311             if Serious_Errors_Detected > 0 then
312                Error_Msg_N ("subunit not analyzed (errors in parent unit)", N);
313             else
314                Error_Msg_N ("missing stub for subunit", N);
315             end if;
316          end if;
317
318          return;
319       end if;
320
321       --  Analyze context (this will call Sem recursively for with'ed units)
322
323       Analyze_Context (N);
324
325       --  If the unit is a package body, the spec is already loaded and must
326       --  be analyzed first, before we analyze the body.
327
328       if Nkind (Unit_Node) = N_Package_Body then
329
330          --  If no Lib_Unit, then there was a serious previous error, so
331          --  just ignore the entire analysis effort
332
333          if No (Lib_Unit) then
334             return;
335
336          else
337             Semantics (Lib_Unit);
338             Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
339
340             --  Verify that the library unit is a package declaration.
341
342             if Nkind (Unit (Lib_Unit)) /= N_Package_Declaration
343                  and then
344                Nkind (Unit (Lib_Unit)) /= N_Generic_Package_Declaration
345             then
346                Error_Msg_N
347                  ("no legal package declaration for package body", N);
348                return;
349
350             --  Otherwise, the entity in the declaration is visible. Update
351             --  the version to reflect dependence of this body on the spec.
352
353             else
354                Spec_Id := Defining_Entity (Unit (Lib_Unit));
355                Set_Is_Immediately_Visible (Spec_Id, True);
356                Version_Update (N, Lib_Unit);
357
358                if Nkind (Defining_Unit_Name (Unit_Node))
359                  = N_Defining_Program_Unit_Name
360                then
361                   Generate_Parent_References (Unit_Node, Scope (Spec_Id));
362                end if;
363             end if;
364          end if;
365
366       --  If the unit is a subprogram body, then we similarly need to analyze
367       --  its spec. However, things are a little simpler in this case, because
368       --  here, this analysis is done only for error checking and consistency
369       --  purposes, so there's nothing else to be done.
370
371       elsif Nkind (Unit_Node) = N_Subprogram_Body then
372          if Acts_As_Spec (N) then
373
374             --  If the subprogram body is a child unit, we must create a
375             --  declaration for it, in order to properly load the parent(s).
376             --  After this, the original unit does not acts as a spec, because
377             --  there is an explicit one. If this  unit appears in a context
378             --  clause, then an implicit with on the parent will be added when
379             --  installing the context. If this is the main unit, there is no
380             --  Unit_Table entry for the declaration, (It has the unit number
381             --  of the main unit) and code generation is unaffected.
382
383             Unum := Get_Cunit_Unit_Number (N);
384             Par_Spec_Name := Get_Parent_Spec_Name (Unit_Name (Unum));
385
386             if Par_Spec_Name /= No_Name then
387                Unum :=
388                  Load_Unit
389                    (Load_Name  => Par_Spec_Name,
390                     Required   => True,
391                     Subunit    => False,
392                     Error_Node => N);
393
394                if Unum /= No_Unit then
395
396                   --  Build subprogram declaration and attach parent unit to it
397                   --  This subprogram declaration does not come from source,
398                   --  Nevertheless the backend must generate debugging info for
399                   --  it, and this must be indicated explicitly.
400
401                   declare
402                      Loc : constant Source_Ptr := Sloc (N);
403                      SCS : constant Boolean :=
404                              Get_Comes_From_Source_Default;
405
406                   begin
407                      Set_Comes_From_Source_Default (False);
408                      Lib_Unit :=
409                        Make_Compilation_Unit (Loc,
410                          Context_Items => New_Copy_List (Context_Items (N)),
411                          Unit =>
412                            Make_Subprogram_Declaration (Sloc (N),
413                              Specification =>
414                                Copy_Separate_Tree
415                                  (Specification (Unit_Node))),
416                          Aux_Decls_Node =>
417                            Make_Compilation_Unit_Aux (Loc));
418
419                      Set_Library_Unit (N, Lib_Unit);
420                      Set_Parent_Spec (Unit (Lib_Unit), Cunit (Unum));
421                      Semantics (Lib_Unit);
422                      Set_Acts_As_Spec (N, False);
423                      Set_Needs_Debug_Info (Defining_Entity (Unit (Lib_Unit)));
424                      Set_Comes_From_Source_Default (SCS);
425                   end;
426                end if;
427             end if;
428
429          --  Here for subprogram with separate declaration
430
431          else
432             Semantics (Lib_Unit);
433             Check_Unused_Withs (Get_Cunit_Unit_Number (Lib_Unit));
434             Version_Update (N, Lib_Unit);
435          end if;
436
437          if Nkind (Defining_Unit_Name (Specification (Unit_Node))) =
438                                              N_Defining_Program_Unit_Name
439          then
440             Generate_Parent_References (
441               Specification (Unit_Node),
442                 Scope (Defining_Entity (Unit (Lib_Unit))));
443          end if;
444       end if;
445
446       --  If it is a child unit, the parent must be elaborated first
447       --  and we update version, since we are dependent on our parent.
448
449       if Is_Child_Spec (Unit_Node) then
450
451          --  The analysis of the parent is done with style checks off
452
453          declare
454             Save_Style_Check : constant Boolean := Style_Check;
455             Save_C_Restrict  : constant Save_Cunit_Boolean_Restrictions :=
456                                  Cunit_Boolean_Restrictions_Save;
457
458          begin
459             if not GNAT_Mode then
460                Style_Check := False;
461             end if;
462
463             Semantics (Parent_Spec (Unit_Node));
464             Version_Update (N, Parent_Spec (Unit_Node));
465             Style_Check := Save_Style_Check;
466             Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
467          end;
468       end if;
469
470       --  With the analysis done, install the context. Note that we can't
471       --  install the context from the with clauses as we analyze them,
472       --  because each with clause must be analyzed in a clean visibility
473       --  context, so we have to wait and install them all at once.
474
475       Install_Context (N);
476
477       if Is_Child_Spec (Unit_Node) then
478
479          --  Set the entities of all parents in the program_unit_name.
480
481          Generate_Parent_References (
482            Unit_Node, Get_Parent_Entity (Unit (Parent_Spec (Unit_Node))));
483       end if;
484
485       --  All components of the context: with-clauses, library unit, ancestors
486       --  if any, (and their context)  are analyzed and installed. Now analyze
487       --  the unit itself, which is either a package, subprogram spec or body.
488
489       Analyze (Unit_Node);
490
491       --  The above call might have made Unit_Node an N_Subprogram_Body
492       --  from something else, so propagate any Acts_As_Spec flag.
493
494       if Nkind (Unit_Node) = N_Subprogram_Body
495         and then Acts_As_Spec (Unit_Node)
496       then
497          Set_Acts_As_Spec (N);
498       end if;
499
500       --  Treat compilation unit pragmas that appear after the library unit
501
502       if Present (Pragmas_After (Aux_Decls_Node (N))) then
503          declare
504             Prag_Node : Node_Id := First (Pragmas_After (Aux_Decls_Node (N)));
505
506          begin
507             while Present (Prag_Node) loop
508                Analyze (Prag_Node);
509                Next (Prag_Node);
510             end loop;
511          end;
512       end if;
513
514       --  Generate distribution stubs if requested and no error
515
516       if N = Main_Cunit
517         and then (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
518                     or else
519                   Distribution_Stub_Mode = Generate_Caller_Stub_Body)
520         and then not Fatal_Error (Main_Unit)
521       then
522          if Is_RCI_Pkg_Spec_Or_Body (N) then
523
524             --  Regular RCI package
525
526             Add_Stub_Constructs (N);
527
528          elsif (Nkind (Unit_Node) = N_Package_Declaration
529                  and then Is_Shared_Passive (Defining_Entity
530                                               (Specification (Unit_Node))))
531            or else (Nkind (Unit_Node) = N_Package_Body
532                      and then
533                        Is_Shared_Passive (Corresponding_Spec (Unit_Node)))
534          then
535             --  Shared passive package
536
537             Add_Stub_Constructs (N);
538
539          elsif Nkind (Unit_Node) = N_Package_Instantiation
540            and then
541              Is_Remote_Call_Interface
542                (Defining_Entity (Specification (Instance_Spec (Unit_Node))))
543          then
544             --  Instantiation of a RCI generic package
545
546             Add_Stub_Constructs (N);
547          end if;
548
549       end if;
550
551       if Nkind (Unit_Node) = N_Package_Declaration
552         or else Nkind (Unit_Node) in N_Generic_Declaration
553         or else Nkind (Unit_Node) = N_Package_Renaming_Declaration
554         or else Nkind (Unit_Node) = N_Subprogram_Declaration
555       then
556          Remove_Unit_From_Visibility (Defining_Entity (Unit_Node));
557
558       --  If the unit is an instantiation whose body will be elaborated
559       --  for inlining purposes, use the the proper entity of the instance.
560
561       elsif Nkind (Unit_Node) = N_Package_Instantiation
562         and then not Error_Posted (Unit_Node)
563       then
564          Remove_Unit_From_Visibility
565            (Defining_Entity (Instance_Spec (Unit_Node)));
566
567       elsif Nkind (Unit_Node) = N_Package_Body
568         or else (Nkind (Unit_Node) = N_Subprogram_Body
569                   and then not Acts_As_Spec (Unit_Node))
570       then
571          --  Bodies that are not the main unit are compiled if they
572          --  are generic or contain generic or inlined units. Their
573          --  analysis brings in the context of the corresponding spec
574          --  (unit declaration) which must be removed as well, to
575          --  return the compilation environment to its proper state.
576
577          Remove_Context (Lib_Unit);
578          Set_Is_Immediately_Visible (Defining_Entity (Unit (Lib_Unit)), False);
579       end if;
580
581       --  Last step is to deinstall the context we just installed
582       --  as well as the unit just compiled.
583
584       Remove_Context (N);
585
586       --  If this is the main unit and we are generating code, we must
587       --  check that all generic units in the context have a body if they
588       --  need it, even if they have not been instantiated. In the absence
589       --  of .ali files for generic units, we must force the load of the body,
590       --  just to produce the proper error if the body is absent. We skip this
591       --  verification if the main unit itself is generic.
592
593       if Get_Cunit_Unit_Number (N) = Main_Unit
594         and then Operating_Mode = Generate_Code
595         and then Expander_Active
596       then
597          --  Check whether the source for the body of the unit must be
598          --  included in a standalone library.
599
600          Check_Body_Needed_For_SAL (Cunit_Entity (Main_Unit));
601
602          --  Indicate that the main unit is now analyzed, to catch possible
603          --  circularities between it and generic bodies. Remove main unit
604          --  from visibility. This might seem superfluous, but the main unit
605          --  must not be visible in the generic body expansions that follow.
606
607          Set_Analyzed (N, True);
608          Set_Is_Immediately_Visible (Cunit_Entity (Main_Unit), False);
609
610          declare
611             Item  : Node_Id;
612             Nam   : Entity_Id;
613             Un    : Unit_Number_Type;
614
615             Save_Style_Check : constant Boolean := Style_Check;
616             Save_C_Restrict  : constant Save_Cunit_Boolean_Restrictions :=
617                                  Cunit_Boolean_Restrictions_Save;
618
619          begin
620             Item := First (Context_Items (N));
621             while Present (Item) loop
622
623                --  Ada0Y (AI-50217): Do not consider limited-withed units
624
625                if Nkind (Item) = N_With_Clause
626                   and then not Implicit_With (Item)
627                   and then not Limited_Present (Item)
628                then
629                   Nam := Entity (Name (Item));
630
631                   if (Is_Generic_Subprogram (Nam)
632                        and then not Is_Intrinsic_Subprogram (Nam))
633                     or else (Ekind (Nam) = E_Generic_Package
634                               and then Unit_Requires_Body (Nam))
635                   then
636                      Style_Check := False;
637
638                      if Present (Renamed_Object (Nam)) then
639                         Un :=
640                            Load_Unit
641                              (Load_Name  => Get_Body_Name
642                                               (Get_Unit_Name
643                                                 (Unit_Declaration_Node
644                                                   (Renamed_Object (Nam)))),
645                               Required   => False,
646                               Subunit    => False,
647                               Error_Node => N,
648                               Renamings  => True);
649                      else
650                         Un :=
651                           Load_Unit
652                             (Load_Name  => Get_Body_Name
653                                              (Get_Unit_Name (Item)),
654                              Required   => False,
655                              Subunit    => False,
656                              Error_Node => N,
657                              Renamings  => True);
658                      end if;
659
660                      if Un = No_Unit then
661                         Error_Msg_NE
662                           ("body of generic unit& not found", Item, Nam);
663                         exit;
664
665                      elsif not Analyzed (Cunit (Un))
666                        and then Un /= Main_Unit
667                        and then not Fatal_Error (Un)
668                      then
669                         Style_Check := False;
670                         Semantics (Cunit (Un));
671                      end if;
672                   end if;
673                end if;
674
675                Next (Item);
676             end loop;
677
678             Style_Check := Save_Style_Check;
679             Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
680          end;
681       end if;
682
683       --  Deal with creating elaboration Boolean if needed. We create an
684       --  elaboration boolean only for units that come from source since
685       --  units manufactured by the compiler never need elab checks.
686
687       if Comes_From_Source (N)
688         and then
689           (Nkind (Unit (N)) =  N_Package_Declaration         or else
690            Nkind (Unit (N)) =  N_Generic_Package_Declaration or else
691            Nkind (Unit (N)) =  N_Subprogram_Declaration      or else
692            Nkind (Unit (N)) =  N_Generic_Subprogram_Declaration)
693       then
694          declare
695             Loc  : constant Source_Ptr := Sloc (N);
696             Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
697
698          begin
699             Spec_Id := Defining_Entity (Unit (N));
700             Generate_Definition (Spec_Id);
701
702             --  See if an elaboration entity is required for possible
703             --  access before elaboration checking. Note that we must
704             --  allow for this even if -gnatE is not set, since a client
705             --  may be compiled in -gnatE mode and reference the entity.
706
707             --  Case of units which do not require elaboration checks
708
709             if
710                --  Pure units do not need checks
711
712                  Is_Pure (Spec_Id)
713
714                --  Preelaborated units do not need checks
715
716                  or else Is_Preelaborated (Spec_Id)
717
718                --  No checks needed if pagma Elaborate_Body present
719
720                  or else Has_Pragma_Elaborate_Body (Spec_Id)
721
722                --  No checks needed if unit does not require a body
723
724                  or else not Unit_Requires_Body (Spec_Id)
725
726                --  No checks needed for predefined files
727
728                  or else Is_Predefined_File_Name (Unit_File_Name (Unum))
729
730                --  No checks required if no separate spec
731
732                  or else Acts_As_Spec (N)
733             then
734                --  This is a case where we only need the entity for
735                --  checking to prevent multiple elaboration checks.
736
737                Set_Elaboration_Entity_Required (Spec_Id, False);
738
739             --  Case of elaboration entity is required for access before
740             --  elaboration checking (so certainly we must build it!)
741
742             else
743                Set_Elaboration_Entity_Required (Spec_Id, True);
744             end if;
745
746             Build_Elaboration_Entity (N, Spec_Id);
747          end;
748       end if;
749
750       --  Finally, freeze the compilation unit entity. This for sure is needed
751       --  because of some warnings that can be output (see Freeze_Subprogram),
752       --  but may in general be required. If freezing actions result, place
753       --  them in the compilation unit actions list, and analyze them.
754
755       declare
756          Loc : constant Source_Ptr := Sloc (N);
757          L   : constant List_Id :=
758                  Freeze_Entity (Cunit_Entity (Current_Sem_Unit), Loc);
759
760       begin
761          while Is_Non_Empty_List (L) loop
762             Insert_Library_Level_Action (Remove_Head (L));
763          end loop;
764       end;
765
766       Set_Analyzed (N);
767
768       if Nkind (Unit_Node) = N_Package_Declaration
769         and then Get_Cunit_Unit_Number (N) /= Main_Unit
770         and then Expander_Active
771       then
772          declare
773             Save_Style_Check : constant Boolean := Style_Check;
774             Save_Warning     : constant Warning_Mode_Type := Warning_Mode;
775             Options : Style_Check_Options;
776
777          begin
778             Save_Style_Check_Options (Options);
779             Reset_Style_Check_Options;
780             Opt.Warning_Mode := Suppress;
781             Check_Body_For_Inlining (N, Defining_Entity (Unit_Node));
782
783             Reset_Style_Check_Options;
784             Set_Style_Check_Options (Options);
785             Style_Check := Save_Style_Check;
786             Warning_Mode := Save_Warning;
787          end;
788       end if;
789    end Analyze_Compilation_Unit;
790
791    ---------------------
792    -- Analyze_Context --
793    ---------------------
794
795    procedure Analyze_Context (N : Node_Id) is
796       Item  : Node_Id;
797
798    begin
799       --  Loop through context items. This is done is three passes:
800       --  a) The first pass analyze non-limited with-clauses.
801       --  b) The second pass add implicit limited_with clauses for
802       --     the parents of child units (Ada0Y: AI-50217)
803       --  c) The third pass analyzes limited_with clauses (Ada0Y: AI-50217)
804
805       Item := First (Context_Items (N));
806       while Present (Item) loop
807
808          --  For with clause, analyze the with clause, and then update
809          --  the version, since we are dependent on a unit that we with.
810
811          if Nkind (Item) = N_With_Clause
812            and then not Limited_Present (Item)
813          then
814
815             --  Skip analyzing with clause if no unit, nothing to do (this
816             --  happens for a with that references a non-existant unit)
817
818             if Present (Library_Unit (Item)) then
819                Analyze (Item);
820             end if;
821
822             if not Implicit_With (Item) then
823                Version_Update (N, Library_Unit (Item));
824             end if;
825
826          --  But skip use clauses at this stage, since we don't want to do
827          --  any installing of potentially use visible entities until we
828          --  we actually install the complete context (in Install_Context).
829          --  Otherwise things can get installed in the wrong context.
830          --  Similarly, pragmas are analyzed in Install_Context, after all
831          --  the implicit with's on parent units are generated.
832
833          else
834             null;
835          end if;
836
837          Next (Item);
838       end loop;
839
840       --  Second pass: add implicit limited_with_clauses for parents of
841       --  child units mentioned in limited_with clauses.
842
843       Item := First (Context_Items (N));
844
845       while Present (Item) loop
846          if Nkind (Item) = N_With_Clause
847            and then Limited_Present (Item)
848            and then  Nkind (Name (Item)) = N_Selected_Component
849          then
850             Expand_Limited_With_Clause
851               (Nam => Prefix (Name (Item)), N  => Item);
852          end if;
853
854          Next (Item);
855       end loop;
856
857       --  Third pass: examine all limited_with clauses.
858
859       Item := First (Context_Items (N));
860
861       while Present (Item) loop
862          if Nkind (Item) = N_With_Clause
863            and then Limited_Present (Item)
864          then
865
866             if Nkind (Unit (N)) /= N_Package_Declaration then
867                Error_Msg_N ("limited with_clause only allowed in"
868                             & " package specification", Item);
869             end if;
870
871             --  Skip analyzing with clause if no unit, see above.
872
873             if Present (Library_Unit (Item)) then
874                Analyze (Item);
875             end if;
876
877             --  A limited_with does not impose an elaboration order, but
878             --  there is a semantic dependency for recompilation purposes.
879
880             if not Implicit_With (Item) then
881                Version_Update (N, Library_Unit (Item));
882             end if;
883          end if;
884
885          Next (Item);
886       end loop;
887    end Analyze_Context;
888
889    -------------------------------
890    -- Analyze_Package_Body_Stub --
891    -------------------------------
892
893    procedure Analyze_Package_Body_Stub (N : Node_Id) is
894       Id   : constant Entity_Id := Defining_Identifier (N);
895       Nam  : Entity_Id;
896
897    begin
898       --  The package declaration must be in the current declarative part.
899
900       Check_Stub_Level (N);
901       Nam := Current_Entity_In_Scope (Id);
902
903       if No (Nam) or else not Is_Package (Nam) then
904          Error_Msg_N ("missing specification for package stub", N);
905
906       elsif Has_Completion (Nam)
907         and then Present (Corresponding_Body (Unit_Declaration_Node (Nam)))
908       then
909          Error_Msg_N ("duplicate or redundant stub for package", N);
910
911       else
912          --  Indicate that the body of the package exists. If we are doing
913          --  only semantic analysis, the stub stands for the body. If we are
914          --  generating code, the existence of the body will be confirmed
915          --  when we load the proper body.
916
917          Set_Has_Completion (Nam);
918          Set_Scope (Defining_Entity (N), Current_Scope);
919          Generate_Reference (Nam, Id, 'b');
920          Analyze_Proper_Body (N, Nam);
921       end if;
922    end Analyze_Package_Body_Stub;
923
924    -------------------------
925    -- Analyze_Proper_Body --
926    -------------------------
927
928    procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id) is
929       Subunit_Name      : constant Unit_Name_Type := Get_Unit_Name (N);
930       Unum              : Unit_Number_Type;
931
932       procedure Optional_Subunit;
933       --  This procedure is called when the main unit is a stub, or when we
934       --  are not generating code. In such a case, we analyze the subunit if
935       --  present, which is user-friendly and in fact required for ASIS, but
936       --  we don't complain if the subunit is missing.
937
938       ----------------------
939       -- Optional_Subunit --
940       ----------------------
941
942       procedure Optional_Subunit is
943          Comp_Unit : Node_Id;
944
945       begin
946          --  Try to load subunit, but ignore any errors that occur during
947          --  the loading of the subunit, by using the special feature in
948          --  Errout to ignore all errors. Note that Fatal_Error will still
949          --  be set, so we will be able to check for this case below.
950
951          Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
952          Unum :=
953            Load_Unit
954              (Load_Name  => Subunit_Name,
955               Required   => False,
956               Subunit    => True,
957               Error_Node => N);
958          Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
959
960          --  All done if we successfully loaded the subunit
961
962          if Unum /= No_Unit
963            and then (not Fatal_Error (Unum) or else Try_Semantics)
964          then
965             Comp_Unit := Cunit (Unum);
966
967             if Nkind (Unit (Comp_Unit)) /= N_Subunit then
968                Error_Msg_N
969                  ("expected SEPARATE subunit, found child unit",
970                   Cunit_Entity (Unum));
971             else
972                Set_Corresponding_Stub (Unit (Comp_Unit), N);
973                Analyze_Subunit (Comp_Unit);
974                Set_Library_Unit (N, Comp_Unit);
975             end if;
976
977          elsif Unum = No_Unit
978            and then Present (Nam)
979          then
980             if Is_Protected_Type (Nam) then
981                Set_Corresponding_Body (Parent (Nam), Defining_Identifier (N));
982             else
983                Set_Corresponding_Body (
984                  Unit_Declaration_Node (Nam), Defining_Identifier (N));
985             end if;
986          end if;
987       end Optional_Subunit;
988
989    --  Start of processing for Analyze_Proper_Body
990
991    begin
992       --  If the subunit is already loaded, it means that the main unit
993       --  is a subunit, and that the current unit is one of its parents
994       --  which was being analyzed to provide the needed context for the
995       --  analysis of the subunit. In this case we analyze the subunit and
996       --  continue with the parent, without looking a subsequent subunits.
997
998       if Is_Loaded (Subunit_Name) then
999
1000          --  If the proper body is already linked to the stub node,
1001          --  the stub is in a generic unit and just needs analyzing.
1002
1003          if Present (Library_Unit (N)) then
1004             Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1005             Analyze_Subunit (Library_Unit (N));
1006
1007          --  Otherwise we must load the subunit and link to it
1008
1009          else
1010             --  Load the subunit, this must work, since we originally
1011             --  loaded the subunit earlier on. So this will not really
1012             --  load it, just give access to it.
1013
1014             Unum :=
1015               Load_Unit
1016                 (Load_Name  => Subunit_Name,
1017                  Required   => True,
1018                  Subunit    => False,
1019                  Error_Node => N);
1020
1021             --  And analyze the subunit in the parent context (note that we
1022             --  do not call Semantics, since that would remove the parent
1023             --  context). Because of this, we have to manually reset the
1024             --  compiler state to Analyzing since it got destroyed by Load.
1025
1026             if Unum /= No_Unit then
1027                Compiler_State := Analyzing;
1028
1029                --  Check that the proper body is a subunit and not a child
1030                --  unit. If the unit was previously loaded, the error will
1031                --  have been emitted when copying the generic node, so we
1032                --  just return to avoid cascaded errors.
1033
1034                if Nkind (Unit (Cunit (Unum))) /= N_Subunit then
1035                   return;
1036                end if;
1037
1038                Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
1039                Analyze_Subunit (Cunit (Unum));
1040                Set_Library_Unit (N, Cunit (Unum));
1041             end if;
1042          end if;
1043
1044       --  If the main unit is a subunit, then we are just performing semantic
1045       --  analysis on that subunit, and any other subunits of any parent unit
1046       --  should be ignored, except that if we are building trees for ASIS
1047       --  usage we want to annotate the stub properly.
1048
1049       elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
1050         and then Subunit_Name /= Unit_Name (Main_Unit)
1051       then
1052          if ASIS_Mode then
1053             Optional_Subunit;
1054          end if;
1055
1056          --  But before we return, set the flag for unloaded subunits. This
1057          --  will suppress junk warnings of variables in the same declarative
1058          --  part (or a higher level one) that are in danger of looking unused
1059          --  when in fact there might be a declaration in the subunit that we
1060          --  do not intend to load.
1061
1062          Unloaded_Subunits := True;
1063          return;
1064
1065       --  If the subunit is not already loaded, and we are generating code,
1066       --  then this is the case where compilation started from the parent,
1067       --  and we are generating code for an entire subunit tree. In that
1068       --  case we definitely need to load the subunit.
1069
1070       --  In order to continue the analysis with the rest of the parent,
1071       --  and other subunits, we load the unit without requiring its
1072       --  presence, and emit a warning if not found, rather than terminating
1073       --  the compilation abruptly, as for other missing file problems.
1074
1075       elsif Original_Operating_Mode = Generate_Code then
1076
1077          --  If the proper body is already linked to the stub node,
1078          --  the stub is in a generic unit and just needs analyzing.
1079
1080          --  We update the version. Although we are not technically
1081          --  semantically dependent on the subunit, given our approach
1082          --  of macro substitution of subunits, it makes sense to
1083          --  include it in the version identification.
1084
1085          if Present (Library_Unit (N)) then
1086             Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
1087             Analyze_Subunit (Library_Unit (N));
1088             Version_Update (Cunit (Main_Unit), Library_Unit (N));
1089
1090          --  Otherwise we must load the subunit and link to it
1091
1092          else
1093             Unum :=
1094               Load_Unit
1095                 (Load_Name  => Subunit_Name,
1096                  Required   => False,
1097                  Subunit    => True,
1098                  Error_Node => N);
1099
1100             if Original_Operating_Mode = Generate_Code
1101               and then Unum = No_Unit
1102             then
1103                Error_Msg_Name_1 := Subunit_Name;
1104                Error_Msg_Name_2 :=
1105                  Get_File_Name (Subunit_Name, Subunit => True);
1106                Error_Msg_N
1107                  ("subunit% in file{ not found!?", N);
1108                Subunits_Missing := True;
1109             end if;
1110
1111             --  Load_Unit may reset Compiler_State, since it may have been
1112             --  necessary to parse an additional units, so we make sure
1113             --  that we reset it to the Analyzing state.
1114
1115             Compiler_State := Analyzing;
1116
1117             if Unum /= No_Unit
1118               and then (not Fatal_Error (Unum) or else Try_Semantics)
1119             then
1120                if Debug_Flag_L then
1121                   Write_Str ("*** Loaded subunit from stub. Analyze");
1122                   Write_Eol;
1123                end if;
1124
1125                declare
1126                   Comp_Unit : constant Node_Id := Cunit (Unum);
1127
1128                begin
1129                   --  Check for child unit instead of subunit
1130
1131                   if Nkind (Unit (Comp_Unit)) /= N_Subunit then
1132                      Error_Msg_N
1133                        ("expected SEPARATE subunit, found child unit",
1134                         Cunit_Entity (Unum));
1135
1136                   --  OK, we have a subunit, so go ahead and analyze it,
1137                   --  and set Scope of entity in stub, for ASIS use.
1138
1139                   else
1140                      Set_Corresponding_Stub (Unit (Comp_Unit), N);
1141                      Analyze_Subunit (Comp_Unit);
1142                      Set_Library_Unit (N, Comp_Unit);
1143
1144                      --  We update the version. Although we are not technically
1145                      --  semantically dependent on the subunit, given our
1146                      --  approach of macro substitution of subunits, it makes
1147                      --  sense to include it in the version identification.
1148
1149                      Version_Update (Cunit (Main_Unit), Comp_Unit);
1150                   end if;
1151                end;
1152             end if;
1153          end if;
1154
1155          --  The remaining case is when the subunit is not already loaded and
1156          --  we are not generating code. In this case we are just performing
1157          --  semantic analysis on the parent, and we are not interested in
1158          --  the subunit. For subprograms, analyze the stub as a body. For
1159          --  other entities the stub has already been marked as completed.
1160
1161       else
1162          Optional_Subunit;
1163       end if;
1164
1165    end Analyze_Proper_Body;
1166
1167    ----------------------------------
1168    -- Analyze_Protected_Body_Stub --
1169    ----------------------------------
1170
1171    procedure Analyze_Protected_Body_Stub (N : Node_Id) is
1172       Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1173
1174    begin
1175       Check_Stub_Level (N);
1176
1177       --  First occurence of name may have been as an incomplete type.
1178
1179       if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1180          Nam := Full_View (Nam);
1181       end if;
1182
1183       if No (Nam)
1184         or else not Is_Protected_Type (Etype (Nam))
1185       then
1186          Error_Msg_N ("missing specification for Protected body", N);
1187       else
1188          Set_Scope (Defining_Entity (N), Current_Scope);
1189          Set_Has_Completion (Etype (Nam));
1190          Generate_Reference (Nam, Defining_Identifier (N), 'b');
1191          Analyze_Proper_Body (N, Etype (Nam));
1192       end if;
1193    end Analyze_Protected_Body_Stub;
1194
1195    ----------------------------------
1196    -- Analyze_Subprogram_Body_Stub --
1197    ----------------------------------
1198
1199    --  A subprogram body stub can appear with or without a previous
1200    --  specification. If there is one, the analysis of the body will
1201    --  find it and verify conformance.  The formals appearing in the
1202    --  specification of the stub play no role, except for requiring an
1203    --  additional conformance check. If there is no previous subprogram
1204    --  declaration, the stub acts as a spec, and provides the defining
1205    --  entity for the subprogram.
1206
1207    procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
1208       Decl : Node_Id;
1209
1210    begin
1211       Check_Stub_Level (N);
1212
1213       --  Verify that the identifier for the stub is unique within this
1214       --  declarative part.
1215
1216       if Nkind (Parent (N)) = N_Block_Statement
1217         or else Nkind (Parent (N)) = N_Package_Body
1218         or else Nkind (Parent (N)) = N_Subprogram_Body
1219       then
1220          Decl := First (Declarations (Parent (N)));
1221
1222          while Present (Decl)
1223            and then Decl /= N
1224          loop
1225             if Nkind (Decl) = N_Subprogram_Body_Stub
1226               and then (Chars (Defining_Unit_Name (Specification (Decl)))
1227                       = Chars (Defining_Unit_Name (Specification (N))))
1228             then
1229                Error_Msg_N ("identifier for stub is not unique", N);
1230             end if;
1231
1232             Next (Decl);
1233          end loop;
1234       end if;
1235
1236       --  Treat stub as a body, which checks conformance if there is a previous
1237       --  declaration, or else introduces entity and its signature.
1238
1239       Analyze_Subprogram_Body (N);
1240       Analyze_Proper_Body (N, Empty);
1241    end Analyze_Subprogram_Body_Stub;
1242
1243    ---------------------
1244    -- Analyze_Subunit --
1245    ---------------------
1246
1247    --  A subunit is compiled either by itself (for semantic checking)
1248    --  or as part of compiling the parent (for code generation). In
1249    --  either case, by the time we actually process the subunit, the
1250    --  parent has already been installed and analyzed. The node N is
1251    --  a compilation unit, whose context needs to be treated here,
1252    --  because we come directly here from the parent without calling
1253    --  Analyze_Compilation_Unit.
1254
1255    --  The compilation context includes the explicit context of the
1256    --  subunit, and the context of the parent, together with the parent
1257    --  itself. In order to compile the current context, we remove the
1258    --  one inherited from the parent, in order to have a clean visibility
1259    --  table. We restore the parent context before analyzing the proper
1260    --  body itself. On exit, we remove only the explicit context of the
1261    --  subunit.
1262
1263    procedure Analyze_Subunit (N : Node_Id) is
1264       Lib_Unit : constant Node_Id   := Library_Unit (N);
1265       Par_Unit : constant Entity_Id := Current_Scope;
1266
1267       Lib_Spec        : Node_Id := Library_Unit (Lib_Unit);
1268       Num_Scopes      : Int := 0;
1269       Use_Clauses     : array (1 .. Scope_Stack.Last) of Node_Id;
1270       Enclosing_Child : Entity_Id := Empty;
1271       Svg             : constant Suppress_Array := Scope_Suppress;
1272
1273       procedure Analyze_Subunit_Context;
1274       --  Capture names in use clauses of the subunit. This must be done
1275       --  before re-installing parent declarations, because items in the
1276       --  context must not be hidden by declarations local to the parent.
1277
1278       procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id);
1279       --  Recursive procedure to restore scope of all ancestors of subunit,
1280       --  from outermost in. If parent is not a subunit, the call to install
1281       --  context installs context of spec and (if parent is a child unit)
1282       --  the context of its parents as well. It is confusing that parents
1283       --  should be treated differently in both cases, but the semantics are
1284       --  just not identical.
1285
1286       procedure Re_Install_Use_Clauses;
1287       --  As part of the removal of the parent scope, the use clauses are
1288       --  removed, to be reinstalled when the context of the subunit has
1289       --  been analyzed. Use clauses may also have been affected by the
1290       --  analysis of the context of the subunit, so they have to be applied
1291       --  again, to insure that the compilation environment of the rest of
1292       --  the parent unit is identical.
1293
1294       procedure Remove_Scope;
1295       --  Remove current scope from scope stack, and preserve the list
1296       --  of use clauses in it, to be reinstalled after context is analyzed.
1297
1298       ------------------------------
1299       --  Analyze_Subunit_Context --
1300       ------------------------------
1301
1302       procedure Analyze_Subunit_Context is
1303          Item      :  Node_Id;
1304          Nam       :  Node_Id;
1305          Unit_Name : Entity_Id;
1306
1307       begin
1308          Analyze_Context (N);
1309          Item := First (Context_Items (N));
1310
1311          --  make withed units immediately visible. If child unit, make the
1312          --  ultimate parent immediately visible.
1313
1314          while Present (Item) loop
1315
1316             if Nkind (Item) = N_With_Clause then
1317                Unit_Name := Entity (Name (Item));
1318
1319                while Is_Child_Unit (Unit_Name) loop
1320                   Set_Is_Visible_Child_Unit (Unit_Name);
1321                   Unit_Name := Scope (Unit_Name);
1322                end loop;
1323
1324                if not Is_Immediately_Visible (Unit_Name) then
1325                   Set_Is_Immediately_Visible (Unit_Name);
1326                   Set_Context_Installed (Item);
1327                end if;
1328
1329             elsif Nkind (Item) = N_Use_Package_Clause then
1330                Nam := First (Names (Item));
1331
1332                while Present (Nam) loop
1333                   Analyze (Nam);
1334                   Next (Nam);
1335                end loop;
1336
1337             elsif Nkind (Item) = N_Use_Type_Clause then
1338                Nam := First (Subtype_Marks (Item));
1339
1340                while Present (Nam) loop
1341                   Analyze (Nam);
1342                   Next (Nam);
1343                end loop;
1344             end if;
1345
1346             Next (Item);
1347          end loop;
1348
1349          Item := First (Context_Items (N));
1350
1351          --  reset visibility of withed units. They will be made visible
1352          --  again when we install the subunit context.
1353
1354          while Present (Item) loop
1355
1356             if Nkind (Item) = N_With_Clause then
1357                Unit_Name := Entity (Name (Item));
1358
1359                while Is_Child_Unit (Unit_Name) loop
1360                   Set_Is_Visible_Child_Unit (Unit_Name, False);
1361                   Unit_Name := Scope (Unit_Name);
1362                end loop;
1363
1364                if Context_Installed (Item) then
1365                   Set_Is_Immediately_Visible (Unit_Name, False);
1366                   Set_Context_Installed (Item, False);
1367                end if;
1368             end if;
1369
1370             Next (Item);
1371          end loop;
1372
1373       end Analyze_Subunit_Context;
1374
1375       ------------------------
1376       -- Re_Install_Parents --
1377       ------------------------
1378
1379       procedure Re_Install_Parents (L : Node_Id; Scop : Entity_Id) is
1380          E : Entity_Id;
1381
1382       begin
1383          if Nkind (Unit (L)) = N_Subunit then
1384             Re_Install_Parents (Library_Unit (L), Scope (Scop));
1385          end if;
1386
1387          Install_Context (L);
1388
1389          --  If the subunit occurs within a child unit, we must restore the
1390          --  immediate visibility of any siblings that may occur in context.
1391
1392          if Present (Enclosing_Child) then
1393             Install_Siblings (Enclosing_Child, L);
1394          end if;
1395
1396          New_Scope (Scop);
1397
1398          if Scop /= Par_Unit then
1399             Set_Is_Immediately_Visible (Scop);
1400          end if;
1401
1402          E := First_Entity (Current_Scope);
1403
1404          while Present (E) loop
1405             Set_Is_Immediately_Visible (E);
1406             Next_Entity (E);
1407          end loop;
1408
1409          --  A subunit appears within a body, and for a nested subunits
1410          --  all the parents are bodies. Restore full visibility of their
1411          --  private entities.
1412
1413          if Ekind (Scop) = E_Package then
1414             Set_In_Package_Body (Scop);
1415             Install_Private_Declarations (Scop);
1416          end if;
1417       end Re_Install_Parents;
1418
1419       ----------------------------
1420       -- Re_Install_Use_Clauses --
1421       ----------------------------
1422
1423       procedure Re_Install_Use_Clauses is
1424          U  : Node_Id;
1425
1426       begin
1427          for J in reverse 1 .. Num_Scopes loop
1428             U := Use_Clauses (J);
1429             Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause := U;
1430             Install_Use_Clauses (U);
1431          end loop;
1432       end Re_Install_Use_Clauses;
1433
1434       ------------------
1435       -- Remove_Scope --
1436       ------------------
1437
1438       procedure Remove_Scope is
1439          E : Entity_Id;
1440
1441       begin
1442          Num_Scopes := Num_Scopes + 1;
1443          Use_Clauses (Num_Scopes) :=
1444                Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
1445          E := First_Entity (Current_Scope);
1446
1447          while Present (E) loop
1448             Set_Is_Immediately_Visible (E, False);
1449             Next_Entity (E);
1450          end loop;
1451
1452          if Is_Child_Unit (Current_Scope) then
1453             Enclosing_Child := Current_Scope;
1454          end if;
1455
1456          Pop_Scope;
1457       end Remove_Scope;
1458
1459    --  Start of processing for Analyze_Subunit
1460
1461    begin
1462       if not Is_Empty_List (Context_Items (N)) then
1463
1464          --  Save current use clauses.
1465
1466          Remove_Scope;
1467          Remove_Context (Lib_Unit);
1468
1469          --  Now remove parents and their context, including enclosing
1470          --  subunits and the outer parent body which is not a subunit.
1471
1472          if Present (Lib_Spec) then
1473             Remove_Context (Lib_Spec);
1474
1475             while Nkind (Unit (Lib_Spec)) = N_Subunit loop
1476                Lib_Spec := Library_Unit (Lib_Spec);
1477                Remove_Scope;
1478                Remove_Context (Lib_Spec);
1479             end loop;
1480
1481             if Nkind (Unit (Lib_Unit)) = N_Subunit then
1482                Remove_Scope;
1483             end if;
1484
1485             if Nkind (Unit (Lib_Spec)) = N_Package_Body then
1486                Remove_Context (Library_Unit (Lib_Spec));
1487             end if;
1488          end if;
1489
1490          Set_Is_Immediately_Visible (Par_Unit, False);
1491
1492          Analyze_Subunit_Context;
1493
1494          Re_Install_Parents (Lib_Unit, Par_Unit);
1495          Set_Is_Immediately_Visible (Par_Unit);
1496
1497          --  If the context includes a child unit of the parent of the
1498          --  subunit, the parent will have been removed from visibility,
1499          --  after compiling that cousin in the context. The visibility
1500          --  of the parent must be restored now. This also applies if the
1501          --  context includes another subunit of the same parent which in
1502          --  turn includes a child unit in its context.
1503
1504          if Ekind (Par_Unit) = E_Package then
1505             if not Is_Immediately_Visible (Par_Unit)
1506               or else (Present (First_Entity (Par_Unit))
1507                         and then not Is_Immediately_Visible
1508                                       (First_Entity (Par_Unit)))
1509             then
1510                Set_Is_Immediately_Visible   (Par_Unit);
1511                Install_Visible_Declarations (Par_Unit);
1512                Install_Private_Declarations (Par_Unit);
1513             end if;
1514          end if;
1515
1516          Re_Install_Use_Clauses;
1517          Install_Context (N);
1518
1519          --  Restore state of suppress flags for current body.
1520
1521          Scope_Suppress := Svg;
1522
1523          --  If the subunit is within a child unit, then siblings of any
1524          --  parent unit that appear in the context clause of the subunit
1525          --  must also be made immediately visible.
1526
1527          if Present (Enclosing_Child) then
1528             Install_Siblings (Enclosing_Child, N);
1529          end if;
1530
1531       end if;
1532
1533       Analyze (Proper_Body (Unit (N)));
1534       Remove_Context (N);
1535    end Analyze_Subunit;
1536
1537    ----------------------------
1538    -- Analyze_Task_Body_Stub --
1539    ----------------------------
1540
1541    procedure Analyze_Task_Body_Stub (N : Node_Id) is
1542       Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
1543       Loc : constant Source_Ptr := Sloc (N);
1544
1545    begin
1546       Check_Stub_Level (N);
1547
1548       --  First occurence of name may have been as an incomplete type.
1549
1550       if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
1551          Nam := Full_View (Nam);
1552       end if;
1553
1554       if No (Nam)
1555         or else not Is_Task_Type (Etype (Nam))
1556       then
1557          Error_Msg_N ("missing specification for task body", N);
1558       else
1559          Set_Scope (Defining_Entity (N), Current_Scope);
1560          Generate_Reference (Nam, Defining_Identifier (N), 'b');
1561          Set_Has_Completion (Etype (Nam));
1562          Analyze_Proper_Body (N, Etype (Nam));
1563
1564          --  Set elaboration flag to indicate that entity is callable.
1565          --  This cannot be done in the expansion of the body  itself,
1566          --  because the proper body is not in a declarative part. This
1567          --  is only done if expansion is active, because the context
1568          --  may be generic and the flag not defined yet.
1569
1570          if Expander_Active then
1571             Insert_After (N,
1572               Make_Assignment_Statement (Loc,
1573                 Name =>
1574                   Make_Identifier (Loc,
1575                     New_External_Name (Chars (Etype (Nam)), 'E')),
1576                  Expression => New_Reference_To (Standard_True, Loc)));
1577          end if;
1578
1579       end if;
1580    end Analyze_Task_Body_Stub;
1581
1582    -------------------------
1583    -- Analyze_With_Clause --
1584    -------------------------
1585
1586    --  Analyze the declaration of a unit in a with clause. At end,
1587    --  label the with clause with the defining entity for the unit.
1588
1589    procedure Analyze_With_Clause (N : Node_Id) is
1590
1591       --  Retrieve the original kind of the unit node, before analysis.
1592       --  If it is a subprogram instantiation, its analysis below will
1593       --  rewrite as the declaration of the wrapper package. If the same
1594       --  instantiation appears indirectly elsewhere in the context, it
1595       --  will have been analyzed already.
1596
1597       Unit_Kind : constant Node_Kind :=
1598                     Nkind (Original_Node (Unit (Library_Unit (N))));
1599
1600       E_Name    : Entity_Id;
1601       Par_Name  : Entity_Id;
1602       Pref      : Node_Id;
1603       U         : Node_Id;
1604
1605       Intunit : Boolean;
1606       --  Set True if the unit currently being compiled is an internal unit
1607
1608       Save_Style_Check : constant Boolean := Opt.Style_Check;
1609       Save_C_Restrict  : constant Save_Cunit_Boolean_Restrictions :=
1610                            Cunit_Boolean_Restrictions_Save;
1611
1612    begin
1613       if Limited_Present (N) then
1614          --  Ada0Y (AI-50217): Build visibility structures but do not
1615          --  analyze unit
1616
1617          Build_Limited_Views (N);
1618          return;
1619       end if;
1620
1621       --  We reset ordinary style checking during the analysis of a with'ed
1622       --  unit, but we do NOT reset GNAT special analysis mode (the latter
1623       --  definitely *does* apply to with'ed units).
1624
1625       if not GNAT_Mode then
1626          Style_Check := False;
1627       end if;
1628
1629       --  If the library unit is a predefined unit, and we are in high
1630       --  integrity mode, then temporarily reset Configurable_Run_Time_Mode
1631       --  for the analysis of the with'ed unit. This mode does not prevent
1632       --  explicit with'ing of run-time units.
1633
1634       if Configurable_Run_Time_Mode
1635         and then
1636           Is_Predefined_File_Name
1637             (Unit_File_Name (Get_Source_Unit (Unit (Library_Unit (N)))))
1638       then
1639          Configurable_Run_Time_Mode := False;
1640          Semantics (Library_Unit (N));
1641          Configurable_Run_Time_Mode := True;
1642
1643       else
1644          Semantics (Library_Unit (N));
1645       end if;
1646
1647       U := Unit (Library_Unit (N));
1648       Intunit := Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit));
1649
1650       --  Following checks are skipped for dummy packages (those supplied
1651       --  for with's where no matching file could be found). Such packages
1652       --  are identified by the Sloc value being set to No_Location
1653
1654       if Sloc (U) /= No_Location then
1655
1656          --  Check restrictions, except that we skip the check if this
1657          --  is an internal unit unless we are compiling the internal
1658          --  unit as the main unit. We also skip this for dummy packages.
1659
1660          if not Intunit or else Current_Sem_Unit = Main_Unit then
1661             Check_Restricted_Unit (Unit_Name (Get_Source_Unit (U)), N);
1662          end if;
1663
1664          --  Check for inappropriate with of internal implementation unit
1665          --  if we are currently compiling the main unit and the main unit
1666          --  is itself not an internal unit. We do not issue this message
1667          --  for implicit with's generated by the compiler itself.
1668
1669          if Implementation_Unit_Warnings
1670            and then Current_Sem_Unit = Main_Unit
1671            and then Implementation_Unit (Get_Source_Unit (U))
1672            and then not Intunit
1673            and then not Implicit_With (N)
1674          then
1675             Error_Msg_N ("& is an internal 'G'N'A'T unit?", Name (N));
1676             Error_Msg_N
1677               ("\use of this unit is non-portable and version-dependent?",
1678                Name (N));
1679          end if;
1680       end if;
1681
1682       --  Semantic analysis of a generic unit is performed on a copy of
1683       --  the original tree. Retrieve the entity on  which semantic info
1684       --  actually appears.
1685
1686       if Unit_Kind in N_Generic_Declaration then
1687          E_Name := Defining_Entity (U);
1688
1689       --  Note: in the following test, Unit_Kind is the original Nkind, but
1690       --  in the case of an instantiation, semantic analysis above will
1691       --  have replaced the unit by its instantiated version. If the instance
1692       --  body has been generated, the instance now denotes the body entity.
1693       --  For visibility purposes we need the entity of its spec.
1694
1695       elsif (Unit_Kind = N_Package_Instantiation
1696               or else Nkind (Original_Node (Unit (Library_Unit (N)))) =
1697                 N_Package_Instantiation)
1698         and then Nkind (U) = N_Package_Body
1699       then
1700          E_Name := Corresponding_Spec (U);
1701
1702       elsif Unit_Kind = N_Package_Instantiation
1703         and then Nkind (U) = N_Package_Instantiation
1704       then
1705          --  If the instance has not been rewritten as a package declaration,
1706          --  then it appeared already in a previous with clause. Retrieve
1707          --  the entity from the previous instance.
1708
1709          E_Name := Defining_Entity (Specification (Instance_Spec (U)));
1710
1711       elsif Unit_Kind = N_Procedure_Instantiation
1712         or else Unit_Kind = N_Function_Instantiation
1713       then
1714          --  Instantiation node is replaced with a package that contains
1715          --  renaming declarations and instance itself. The subprogram
1716          --  Instance is declared in the visible part of the wrapper package.
1717
1718          E_Name := First_Entity (Defining_Entity (U));
1719
1720          while Present (E_Name) loop
1721             exit when Is_Subprogram (E_Name)
1722               and then Is_Generic_Instance (E_Name);
1723             E_Name := Next_Entity (E_Name);
1724          end loop;
1725
1726       elsif Unit_Kind = N_Package_Renaming_Declaration
1727         or else Unit_Kind in N_Generic_Renaming_Declaration
1728       then
1729          E_Name := Defining_Entity (U);
1730
1731       elsif Unit_Kind = N_Subprogram_Body
1732         and then Nkind (Name (N)) = N_Selected_Component
1733         and then not Acts_As_Spec (Library_Unit (N))
1734       then
1735          --  For a child unit that has no spec, one has been created and
1736          --  analyzed. The entity required is that of the spec.
1737
1738          E_Name := Corresponding_Spec (U);
1739
1740       else
1741          E_Name := Defining_Entity (U);
1742       end if;
1743
1744       if Nkind (Name (N)) = N_Selected_Component then
1745
1746          --  Child unit in a with clause
1747
1748          Change_Selected_Component_To_Expanded_Name (Name (N));
1749       end if;
1750
1751       --  Restore style checks and restrictions
1752
1753       Style_Check := Save_Style_Check;
1754       Cunit_Boolean_Restrictions_Restore (Save_C_Restrict);
1755
1756       --  Record the reference, but do NOT set the unit as referenced, we
1757       --  want to consider the unit as unreferenced if this is the only
1758       --  reference that occurs.
1759
1760       Set_Entity_With_Style_Check (Name (N), E_Name);
1761       Generate_Reference (E_Name, Name (N), 'w', Set_Ref => False);
1762
1763       if Is_Child_Unit (E_Name) then
1764          Pref     := Prefix (Name (N));
1765          Par_Name := Scope (E_Name);
1766
1767          while Nkind (Pref) = N_Selected_Component loop
1768             Change_Selected_Component_To_Expanded_Name (Pref);
1769             Set_Entity_With_Style_Check (Pref, Par_Name);
1770
1771             Generate_Reference (Par_Name, Pref);
1772             Pref := Prefix (Pref);
1773
1774             --  If E_Name is the dummy entity for a nonexistent unit,
1775             --  its scope is set to Standard_Standard, and no attempt
1776             --  should be made to further unwind scopes.
1777
1778             if Par_Name /= Standard_Standard then
1779                Par_Name := Scope (Par_Name);
1780             end if;
1781          end loop;
1782
1783          if Present (Entity (Pref))
1784            and then not Analyzed (Parent (Parent (Entity (Pref))))
1785          then
1786             --  If the entity is set without its unit being compiled,
1787             --  the original parent is a renaming, and Par_Name is the
1788             --  renamed entity. For visibility purposes, we need the
1789             --  original entity, which must be analyzed now, because
1790             --  Load_Unit retrieves directly the renamed unit, and the
1791             --  renaming declaration itself has not been analyzed.
1792
1793             Analyze (Parent (Parent (Entity (Pref))));
1794             pragma Assert (Renamed_Object (Entity (Pref)) = Par_Name);
1795             Par_Name := Entity (Pref);
1796          end if;
1797
1798          Set_Entity_With_Style_Check (Pref, Par_Name);
1799          Generate_Reference (Par_Name, Pref);
1800       end if;
1801
1802       --  If the withed unit is System, and a system extension pragma is
1803       --  present, compile the extension now, rather than waiting for
1804       --  a visibility check on a specific entity.
1805
1806       if Chars (E_Name) = Name_System
1807         and then Scope (E_Name) = Standard_Standard
1808         and then Present (System_Extend_Unit)
1809         and then Present_System_Aux (N)
1810       then
1811          --  If the extension is not present, an error will have been emitted.
1812
1813          null;
1814       end if;
1815    end Analyze_With_Clause;
1816
1817    ------------------------------
1818    -- Analyze_With_Type_Clause --
1819    ------------------------------
1820
1821    procedure Analyze_With_Type_Clause (N : Node_Id) is
1822       Loc  : constant Source_Ptr := Sloc (N);
1823       Nam  : constant Node_Id    := Name (N);
1824       Pack : Node_Id;
1825       Decl : Node_Id;
1826       P    : Entity_Id;
1827       Unum : Unit_Number_Type;
1828       Sel  : Node_Id;
1829
1830       procedure Decorate_Tagged_Type (T : Entity_Id);
1831       --  Set basic attributes of type, including its class_wide type.
1832
1833       function In_Chain (E : Entity_Id) return Boolean;
1834       --  Check that the imported type is not already in the homonym chain,
1835       --  for example through a with_type clause in a parent unit.
1836
1837       --------------------------
1838       -- Decorate_Tagged_Type --
1839       --------------------------
1840
1841       procedure Decorate_Tagged_Type (T : Entity_Id) is
1842          CW : Entity_Id;
1843
1844       begin
1845          Set_Ekind (T, E_Record_Type);
1846          Set_Is_Tagged_Type (T);
1847          Set_Etype (T, T);
1848          Set_From_With_Type (T);
1849          Set_Scope (T, P);
1850
1851          if not In_Chain (T) then
1852             Set_Homonym (T, Current_Entity (T));
1853             Set_Current_Entity (T);
1854          end if;
1855
1856          --  Build bogus class_wide type, if not previously done.
1857
1858          if No (Class_Wide_Type (T)) then
1859             CW := Make_Defining_Identifier (Loc,  New_Internal_Name ('S'));
1860
1861             Set_Ekind            (CW, E_Class_Wide_Type);
1862             Set_Etype            (CW, T);
1863             Set_Scope            (CW, P);
1864             Set_Is_Tagged_Type   (CW);
1865             Set_Is_First_Subtype (CW, True);
1866             Init_Size_Align      (CW);
1867             Set_Has_Unknown_Discriminants
1868                                  (CW, True);
1869             Set_Class_Wide_Type  (CW, CW);
1870             Set_Equivalent_Type  (CW, Empty);
1871             Set_From_With_Type   (CW);
1872
1873             Set_Class_Wide_Type (T, CW);
1874          end if;
1875       end Decorate_Tagged_Type;
1876
1877       --------------
1878       -- In_Chain --
1879       --------------
1880
1881       function In_Chain (E : Entity_Id) return Boolean is
1882          H : Entity_Id := Current_Entity (E);
1883
1884       begin
1885          while Present (H) loop
1886
1887             if H = E then
1888                return True;
1889             else
1890                H := Homonym (H);
1891             end if;
1892          end loop;
1893
1894          return False;
1895       end In_Chain;
1896
1897    --  Start of processing for Analyze_With_Type_Clause
1898
1899    begin
1900       if Nkind (Nam) = N_Selected_Component then
1901          Pack := New_Copy_Tree (Prefix (Nam));
1902          Sel  := Selector_Name (Nam);
1903
1904       else
1905          Error_Msg_N ("illegal name for imported type", Nam);
1906          return;
1907       end if;
1908
1909       Decl :=
1910         Make_Package_Declaration (Loc,
1911           Specification =>
1912              (Make_Package_Specification (Loc,
1913                Defining_Unit_Name   => Pack,
1914                Visible_Declarations => New_List,
1915                End_Label            => Empty)));
1916
1917       Unum :=
1918         Load_Unit
1919           (Load_Name  => Get_Unit_Name (Decl),
1920            Required   => True,
1921            Subunit    => False,
1922            Error_Node => Nam);
1923
1924       if Unum = No_Unit
1925          or else Nkind (Unit (Cunit (Unum))) /= N_Package_Declaration
1926       then
1927          Error_Msg_N ("imported type must be declared in package", Nam);
1928          return;
1929
1930       elsif Unum = Current_Sem_Unit then
1931
1932          --  If type is defined in unit being analyzed, then the clause
1933          --  is redundant.
1934
1935          return;
1936
1937       else
1938          P := Cunit_Entity (Unum);
1939       end if;
1940
1941       --  Find declaration for imported type, and set its basic attributes
1942       --  if it has not been analyzed (which will be the case if there is
1943       --  circular dependence).
1944
1945       declare
1946          Decl : Node_Id;
1947          Typ  : Entity_Id;
1948
1949       begin
1950          if not Analyzed (Cunit (Unum))
1951            and then not From_With_Type (P)
1952          then
1953             Set_Ekind (P, E_Package);
1954             Set_Etype (P, Standard_Void_Type);
1955             Set_From_With_Type (P);
1956             Set_Scope (P, Standard_Standard);
1957             Set_Homonym (P, Current_Entity (P));
1958             Set_Current_Entity (P);
1959
1960          elsif Analyzed (Cunit (Unum))
1961            and then Is_Child_Unit (P)
1962          then
1963             --  If the child unit is already in scope, indicate that it is
1964             --  visible, and remains so after intervening calls to rtsfind.
1965
1966             Set_Is_Visible_Child_Unit (P);
1967          end if;
1968
1969          if Nkind (Parent (P)) = N_Defining_Program_Unit_Name then
1970
1971             --  Make parent packages visible.
1972
1973             declare
1974                Parent_Comp : Node_Id;
1975                Parent_Id   : Entity_Id;
1976                Child       : Entity_Id;
1977
1978             begin
1979                Child   := P;
1980                Parent_Comp := Parent_Spec (Unit (Cunit (Unum)));
1981
1982                loop
1983                   Parent_Id := Defining_Entity (Unit (Parent_Comp));
1984                   Set_Scope (Child, Parent_Id);
1985
1986                   --  The type may be imported from a child unit, in which
1987                   --  case the current compilation appears in the name. Do
1988                   --  not change its visibility here because it will conflict
1989                   --  with the subsequent normal processing.
1990
1991                   if not Analyzed (Unit_Declaration_Node (Parent_Id))
1992                     and then Parent_Id /= Cunit_Entity (Current_Sem_Unit)
1993                   then
1994                      Set_Ekind (Parent_Id, E_Package);
1995                      Set_Etype (Parent_Id, Standard_Void_Type);
1996
1997                      --  The same package may appear is several with_type
1998                      --  clauses.
1999
2000                      if not From_With_Type (Parent_Id) then
2001                         Set_Homonym (Parent_Id, Current_Entity (Parent_Id));
2002                         Set_Current_Entity (Parent_Id);
2003                         Set_From_With_Type (Parent_Id);
2004                      end if;
2005                   end if;
2006
2007                   Set_Is_Immediately_Visible (Parent_Id);
2008
2009                   Child := Parent_Id;
2010                   Parent_Comp := Parent_Spec (Unit (Parent_Comp));
2011                   exit when No (Parent_Comp);
2012                end loop;
2013
2014                Set_Scope (Parent_Id, Standard_Standard);
2015             end;
2016          end if;
2017
2018          --  Even if analyzed, the package may not be currently visible. It
2019          --  must be while the with_type clause is active.
2020
2021          Set_Is_Immediately_Visible (P);
2022
2023          Decl :=
2024            First (Visible_Declarations (Specification (Unit (Cunit (Unum)))));
2025
2026          while Present (Decl) loop
2027
2028             if Nkind (Decl) = N_Full_Type_Declaration
2029               and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
2030             then
2031                Typ := Defining_Identifier (Decl);
2032
2033                if Tagged_Present (N) then
2034
2035                   --  The declaration must indicate that this is a tagged
2036                   --  type or a type extension.
2037
2038                   if (Nkind (Type_Definition (Decl)) = N_Record_Definition
2039                        and then Tagged_Present (Type_Definition (Decl)))
2040                     or else
2041                       (Nkind (Type_Definition (Decl))
2042                           = N_Derived_Type_Definition
2043                          and then Present
2044                            (Record_Extension_Part (Type_Definition (Decl))))
2045                   then
2046                      null;
2047                   else
2048                      Error_Msg_N ("imported type is not a tagged type", Nam);
2049                      return;
2050                   end if;
2051
2052                   if not Analyzed (Decl) then
2053
2054                      --  Unit is not currently visible. Add basic attributes
2055                      --  to type and build its class-wide type.
2056
2057                      Init_Size_Align (Typ);
2058                      Decorate_Tagged_Type (Typ);
2059                   end if;
2060
2061                else
2062                   if Nkind (Type_Definition (Decl))
2063                      /= N_Access_To_Object_Definition
2064                   then
2065                      Error_Msg_N
2066                       ("imported type is not an access type", Nam);
2067
2068                   elsif not Analyzed (Decl) then
2069                      Set_Ekind                    (Typ, E_Access_Type);
2070                      Set_Etype                    (Typ, Typ);
2071                      Set_Scope                    (Typ, P);
2072                      Init_Size                    (Typ, System_Address_Size);
2073                      Init_Alignment               (Typ);
2074                      Set_Directly_Designated_Type (Typ, Standard_Integer);
2075                      Set_From_With_Type           (Typ);
2076
2077                      if not In_Chain (Typ) then
2078                         Set_Homonym               (Typ, Current_Entity (Typ));
2079                         Set_Current_Entity        (Typ);
2080                      end if;
2081                   end if;
2082                end if;
2083
2084                Set_Entity (Sel, Typ);
2085                return;
2086
2087             elsif ((Nkind (Decl) = N_Private_Type_Declaration
2088                       and then Tagged_Present (Decl))
2089                 or else (Nkind (Decl) = N_Private_Extension_Declaration))
2090               and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
2091             then
2092                Typ := Defining_Identifier (Decl);
2093
2094                if not Tagged_Present (N) then
2095                   Error_Msg_N ("type must be declared tagged", N);
2096
2097                elsif not Analyzed (Decl) then
2098                   Decorate_Tagged_Type (Typ);
2099                end if;
2100
2101                Set_Entity (Sel, Typ);
2102                Set_From_With_Type (Typ);
2103                return;
2104             end if;
2105
2106             Decl := Next (Decl);
2107          end loop;
2108
2109          Error_Msg_NE ("not a visible access or tagged type in&", Nam, P);
2110       end;
2111    end Analyze_With_Type_Clause;
2112
2113    -----------------------------
2114    -- Check_With_Type_Clauses --
2115    -----------------------------
2116
2117    procedure Check_With_Type_Clauses (N : Node_Id) is
2118       Lib_Unit : constant Node_Id := Unit (N);
2119
2120       procedure Check_Parent_Context (U : Node_Id);
2121       --  Examine context items of parent unit to locate with_type clauses.
2122
2123       --------------------------
2124       -- Check_Parent_Context --
2125       --------------------------
2126
2127       procedure Check_Parent_Context (U : Node_Id) is
2128          Item : Node_Id;
2129
2130       begin
2131          Item := First (Context_Items (U));
2132          while Present (Item) loop
2133             if Nkind (Item) = N_With_Type_Clause
2134               and then not Error_Posted (Item)
2135               and then
2136                 From_With_Type (Scope (Entity (Selector_Name (Name (Item)))))
2137             then
2138                Error_Msg_Sloc := Sloc (Item);
2139                Error_Msg_N ("Missing With_Clause for With_Type_Clause#", N);
2140             end if;
2141
2142             Next (Item);
2143          end loop;
2144       end Check_Parent_Context;
2145
2146    --  Start of processing for Check_With_Type_Clauses
2147
2148    begin
2149       if Extensions_Allowed
2150         and then (Nkind (Lib_Unit) = N_Package_Body
2151                    or else Nkind (Lib_Unit) = N_Subprogram_Body)
2152       then
2153          Check_Parent_Context (Library_Unit (N));
2154          if Is_Child_Spec (Unit (Library_Unit (N))) then
2155             Check_Parent_Context (Parent_Spec (Unit (Library_Unit (N))));
2156          end if;
2157       end if;
2158    end Check_With_Type_Clauses;
2159
2160    ------------------------------
2161    -- Check_Private_Child_Unit --
2162    ------------------------------
2163
2164    procedure Check_Private_Child_Unit (N : Node_Id) is
2165       Lib_Unit   : constant Node_Id := Unit (N);
2166       Item       : Node_Id;
2167       Curr_Unit  : Entity_Id;
2168       Sub_Parent : Node_Id;
2169       Priv_Child : Entity_Id;
2170       Par_Lib    : Entity_Id;
2171       Par_Spec   : Node_Id;
2172
2173       function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2174       --  Returns true if and only if the library unit is declared with
2175       --  an explicit designation of private.
2176
2177       function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2178          Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2179
2180       begin
2181          return Private_Present (Comp_Unit);
2182       end Is_Private_Library_Unit;
2183
2184    --  Start of processing for Check_Private_Child_Unit
2185
2186    begin
2187       if Nkind (Lib_Unit) = N_Package_Body
2188         or else Nkind (Lib_Unit) = N_Subprogram_Body
2189       then
2190          Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2191          Par_Lib   := Curr_Unit;
2192
2193       elsif Nkind (Lib_Unit) = N_Subunit then
2194
2195          --  The parent is itself a body. The parent entity is to be found
2196          --  in the corresponding spec.
2197
2198          Sub_Parent := Library_Unit (N);
2199          Curr_Unit  := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2200
2201          --  If the parent itself is a subunit, Curr_Unit is the entity
2202          --  of the enclosing body, retrieve the spec entity which is
2203          --  the proper ancestor we need for the following tests.
2204
2205          if Ekind (Curr_Unit) = E_Package_Body then
2206             Curr_Unit := Spec_Entity (Curr_Unit);
2207          end if;
2208
2209          Par_Lib    := Curr_Unit;
2210
2211       else
2212          Curr_Unit := Defining_Entity (Lib_Unit);
2213
2214          Par_Lib := Curr_Unit;
2215          Par_Spec  := Parent_Spec (Lib_Unit);
2216
2217          if No (Par_Spec) then
2218             Par_Lib := Empty;
2219          else
2220             Par_Lib := Defining_Entity (Unit (Par_Spec));
2221          end if;
2222       end if;
2223
2224       --  Loop through context items
2225
2226       Item := First (Context_Items (N));
2227       while Present (Item) loop
2228
2229          if Nkind (Item) = N_With_Clause
2230             and then not Implicit_With (Item)
2231             and then Is_Private_Descendant (Entity (Name (Item)))
2232          then
2233             Priv_Child := Entity (Name (Item));
2234
2235             declare
2236                Curr_Parent  : Entity_Id := Par_Lib;
2237                Child_Parent : Entity_Id := Scope (Priv_Child);
2238                Prv_Ancestor : Entity_Id := Child_Parent;
2239                Curr_Private : Boolean   := Is_Private_Library_Unit (Curr_Unit);
2240
2241             begin
2242                --  If the child unit is a public child then locate
2243                --  the nearest private ancestor; Child_Parent will
2244                --  then be set to the parent of that ancestor.
2245
2246                if not Is_Private_Library_Unit (Priv_Child) then
2247                   while Present (Prv_Ancestor)
2248                     and then not Is_Private_Library_Unit (Prv_Ancestor)
2249                   loop
2250                      Prv_Ancestor := Scope (Prv_Ancestor);
2251                   end loop;
2252
2253                   if Present (Prv_Ancestor) then
2254                      Child_Parent := Scope (Prv_Ancestor);
2255                   end if;
2256                end if;
2257
2258                while Present (Curr_Parent)
2259                  and then Curr_Parent /= Standard_Standard
2260                  and then Curr_Parent /= Child_Parent
2261                loop
2262                   Curr_Private :=
2263                     Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2264                   Curr_Parent := Scope (Curr_Parent);
2265                end loop;
2266
2267                if not Present (Curr_Parent) then
2268                   Curr_Parent := Standard_Standard;
2269                end if;
2270
2271                if Curr_Parent /= Child_Parent then
2272
2273                   if Ekind (Priv_Child) = E_Generic_Package
2274                     and then Chars (Priv_Child) in Text_IO_Package_Name
2275                     and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2276                   then
2277                      Error_Msg_NE
2278                        ("& is a nested package, not a compilation unit",
2279                        Name (Item), Priv_Child);
2280
2281                   else
2282                      Error_Msg_N
2283                        ("unit in with clause is private child unit!", Item);
2284                      Error_Msg_NE
2285                        ("current unit must also have parent&!",
2286                         Item, Child_Parent);
2287                   end if;
2288
2289                elsif not Curr_Private
2290                  and then Nkind (Lib_Unit) /= N_Package_Body
2291                  and then Nkind (Lib_Unit) /= N_Subprogram_Body
2292                  and then Nkind (Lib_Unit) /= N_Subunit
2293                then
2294                   Error_Msg_NE
2295                     ("current unit must also be private descendant of&",
2296                      Item, Child_Parent);
2297                end if;
2298             end;
2299          end if;
2300
2301          Next (Item);
2302       end loop;
2303
2304    end Check_Private_Child_Unit;
2305
2306    ----------------------
2307    -- Check_Stub_Level --
2308    ----------------------
2309
2310    procedure Check_Stub_Level (N : Node_Id) is
2311       Par  : constant Node_Id   := Parent (N);
2312       Kind : constant Node_Kind := Nkind (Par);
2313
2314    begin
2315       if (Kind = N_Package_Body
2316            or else Kind = N_Subprogram_Body
2317            or else Kind = N_Task_Body
2318            or else Kind = N_Protected_Body)
2319
2320         and then (Nkind (Parent (Par)) = N_Compilation_Unit
2321                    or else Nkind (Parent (Par)) = N_Subunit)
2322       then
2323          null;
2324
2325       --  In an instance, a missing stub appears at any level. A warning
2326       --  message will have been emitted already for the missing file.
2327
2328       elsif not In_Instance then
2329          Error_Msg_N ("stub cannot appear in an inner scope", N);
2330
2331       elsif Expander_Active then
2332          Error_Msg_N ("missing proper body", N);
2333       end if;
2334    end Check_Stub_Level;
2335
2336    ------------------------
2337    -- Expand_With_Clause --
2338    ------------------------
2339
2340    procedure Expand_With_Clause (Nam : Node_Id; N : Node_Id) is
2341       Loc   : constant Source_Ptr := Sloc (Nam);
2342       Ent   : constant Entity_Id := Entity (Nam);
2343       Withn : Node_Id;
2344       P     : Node_Id;
2345
2346       function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2347
2348       function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2349          Result : Node_Id;
2350
2351       begin
2352          if Nkind (Nam) = N_Identifier then
2353             return New_Occurrence_Of (Entity (Nam), Loc);
2354
2355          else
2356             Result :=
2357               Make_Expanded_Name (Loc,
2358                 Chars  => Chars (Entity (Nam)),
2359                 Prefix => Build_Unit_Name (Prefix (Nam)),
2360                 Selector_Name => New_Occurrence_Of (Entity (Nam), Loc));
2361             Set_Entity (Result, Entity (Nam));
2362             return Result;
2363          end if;
2364       end Build_Unit_Name;
2365
2366    begin
2367       New_Nodes_OK := New_Nodes_OK + 1;
2368       Withn :=
2369         Make_With_Clause (Loc, Name => Build_Unit_Name (Nam));
2370
2371       P := Parent (Unit_Declaration_Node (Ent));
2372       Set_Library_Unit          (Withn, P);
2373       Set_Corresponding_Spec    (Withn, Ent);
2374       Set_First_Name            (Withn, True);
2375       Set_Implicit_With         (Withn, True);
2376
2377       Prepend (Withn, Context_Items (N));
2378       Mark_Rewrite_Insertion (Withn);
2379       Install_Withed_Unit (Withn);
2380
2381       if Nkind (Nam) = N_Expanded_Name then
2382          Expand_With_Clause (Prefix (Nam), N);
2383       end if;
2384
2385       New_Nodes_OK := New_Nodes_OK - 1;
2386    end Expand_With_Clause;
2387
2388    --------------------------------
2389    -- Expand_Limited_With_Clause --
2390    --------------------------------
2391
2392    procedure Expand_Limited_With_Clause (Nam : Node_Id; N : Node_Id) is
2393       Loc   : constant Source_Ptr := Sloc (Nam);
2394       Unum  : Unit_Number_Type;
2395       Withn : Node_Id;
2396
2397    begin
2398       New_Nodes_OK := New_Nodes_OK + 1;
2399
2400       if Nkind (Nam) = N_Identifier then
2401          Withn :=
2402            Make_With_Clause (Loc, Name => Nam);
2403          Set_Limited_Present (Withn);
2404          Set_First_Name      (Withn);
2405          Set_Implicit_With   (Withn);
2406
2407          --  Load the corresponding parent unit
2408
2409          Unum :=
2410            Load_Unit
2411            (Load_Name  => Get_Spec_Name (Get_Unit_Name (Nam)),
2412             Required   => True,
2413             Subunit    => False,
2414             Error_Node => Nam);
2415
2416          if not Analyzed (Cunit (Unum)) then
2417             Set_Library_Unit (Withn, Cunit (Unum));
2418             Set_Corresponding_Spec
2419               (Withn, Specification (Unit (Cunit (Unum))));
2420
2421             Prepend (Withn, Context_Items (Parent (N)));
2422             Mark_Rewrite_Insertion (Withn);
2423          end if;
2424
2425       elsif Nkind (Nam) = N_Selected_Component then
2426          Withn :=
2427            Make_With_Clause
2428            (Loc,
2429             Name =>
2430               Make_Selected_Component
2431                 (Loc,
2432                  Prefix        => Prefix (Nam),
2433                  Selector_Name => Selector_Name (Nam)));
2434
2435          Set_Parent (Withn, Parent (N));
2436          Set_Limited_Present (Withn);
2437          Set_First_Name      (Withn);
2438          Set_Implicit_With   (Withn);
2439
2440          Unum :=
2441            Load_Unit
2442              (Load_Name  => Get_Spec_Name (Get_Unit_Name (Nam)),
2443               Required   => True,
2444               Subunit    => False,
2445               Error_Node => Nam);
2446
2447          if not Analyzed (Cunit (Unum)) then
2448             Set_Library_Unit (Withn, Cunit (Unum));
2449             Set_Corresponding_Spec
2450               (Withn, Specification (Unit (Cunit (Unum))));
2451             Prepend (Withn, Context_Items (Parent (N)));
2452             Mark_Rewrite_Insertion (Withn);
2453
2454             Expand_Limited_With_Clause (Prefix (Nam), N);
2455          end if;
2456
2457       else
2458          null;
2459          pragma Assert (False);
2460       end if;
2461
2462       New_Nodes_OK := New_Nodes_OK - 1;
2463    end Expand_Limited_With_Clause;
2464
2465    -----------------------
2466    -- Get_Parent_Entity --
2467    -----------------------
2468
2469    function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2470    begin
2471       if Nkind (Unit) = N_Package_Instantiation then
2472          return Defining_Entity (Specification (Instance_Spec (Unit)));
2473       else
2474          return Defining_Entity (Unit);
2475       end if;
2476    end Get_Parent_Entity;
2477
2478    -----------------------------
2479    -- Implicit_With_On_Parent --
2480    -----------------------------
2481
2482    procedure Implicit_With_On_Parent
2483      (Child_Unit : Node_Id;
2484       N          : Node_Id)
2485    is
2486       Loc    : constant Source_Ptr := Sloc (N);
2487       P      : constant Node_Id    := Parent_Spec (Child_Unit);
2488       P_Unit : constant Node_Id    := Unit (P);
2489       P_Name : constant Entity_Id  := Get_Parent_Entity (P_Unit);
2490       Withn  : Node_Id;
2491
2492       function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2493       --  Build prefix of child unit name. Recurse if needed.
2494
2495       function Build_Unit_Name return Node_Id;
2496       --  If the unit is a child unit, build qualified name with all
2497       --  ancestors.
2498
2499       -------------------------
2500       -- Build_Ancestor_Name --
2501       -------------------------
2502
2503       function Build_Ancestor_Name (P : Node_Id) return Node_Id is
2504          P_Ref : constant Node_Id :=
2505                    New_Reference_To (Defining_Entity (P), Loc);
2506       begin
2507          if No (Parent_Spec (P)) then
2508             return P_Ref;
2509          else
2510             return
2511               Make_Selected_Component (Loc,
2512                 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P))),
2513                 Selector_Name => P_Ref);
2514          end if;
2515       end Build_Ancestor_Name;
2516
2517       ---------------------
2518       -- Build_Unit_Name --
2519       ---------------------
2520
2521       function Build_Unit_Name return Node_Id is
2522          Result : Node_Id;
2523       begin
2524          if No (Parent_Spec (P_Unit)) then
2525             return New_Reference_To (P_Name, Loc);
2526          else
2527             Result :=
2528               Make_Expanded_Name (Loc,
2529                 Chars  => Chars (P_Name),
2530                 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
2531                 Selector_Name => New_Reference_To (P_Name, Loc));
2532             Set_Entity (Result, P_Name);
2533             return Result;
2534          end if;
2535       end Build_Unit_Name;
2536
2537    --  Start of processing for Implicit_With_On_Parent
2538
2539    begin
2540       New_Nodes_OK := New_Nodes_OK + 1;
2541       Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
2542
2543       Set_Library_Unit          (Withn, P);
2544       Set_Corresponding_Spec    (Withn, P_Name);
2545       Set_First_Name            (Withn, True);
2546       Set_Implicit_With         (Withn, True);
2547
2548       --  Node is placed at the beginning of the context items, so that
2549       --  subsequent use clauses on the parent can be validated.
2550
2551       Prepend (Withn, Context_Items (N));
2552       Mark_Rewrite_Insertion (Withn);
2553       Install_Withed_Unit (Withn);
2554
2555       if Is_Child_Spec (P_Unit) then
2556          Implicit_With_On_Parent (P_Unit, N);
2557       end if;
2558
2559       New_Nodes_OK := New_Nodes_OK - 1;
2560    end Implicit_With_On_Parent;
2561
2562    ---------------------
2563    -- Install_Context --
2564    ---------------------
2565
2566    procedure Install_Context (N : Node_Id) is
2567       Lib_Unit : constant Node_Id := Unit (N);
2568
2569    begin
2570       Install_Context_Clauses (N);
2571
2572       if Is_Child_Spec (Lib_Unit) then
2573          Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
2574       end if;
2575
2576       Install_Limited_Context_Clauses (N);
2577
2578       Check_With_Type_Clauses (N);
2579    end Install_Context;
2580
2581    -----------------------------
2582    -- Install_Context_Clauses --
2583    -----------------------------
2584
2585    procedure Install_Context_Clauses (N : Node_Id) is
2586       Lib_Unit      : constant Node_Id := Unit (N);
2587       Item          : Node_Id;
2588       Uname_Node    : Entity_Id;
2589       Check_Private : Boolean := False;
2590       Decl_Node     : Node_Id;
2591       Lib_Parent    : Entity_Id;
2592
2593    begin
2594       --  Loop through context clauses to find the with/use clauses.
2595       --  This is done twice, first for everything except limited_with
2596       --  clauses, and then for those, if any are present.
2597
2598       Item := First (Context_Items (N));
2599       while Present (Item) loop
2600
2601          --  Case of explicit WITH clause
2602
2603          if Nkind (Item) = N_With_Clause
2604            and then not Implicit_With (Item)
2605          then
2606             if Limited_Present (Item) then
2607
2608                --  Limited withed units will be installed later.
2609
2610                goto Continue;
2611
2612             --  If Name (Item) is not an entity name, something is wrong, and
2613             --  this will be detected in due course, for now ignore the item
2614
2615             elsif not Is_Entity_Name (Name (Item)) then
2616                goto Continue;
2617
2618             elsif No (Entity (Name (Item))) then
2619                Set_Entity (Name (Item), Any_Id);
2620                goto Continue;
2621             end if;
2622
2623             Uname_Node := Entity (Name (Item));
2624
2625             if Is_Private_Descendant (Uname_Node) then
2626                Check_Private := True;
2627             end if;
2628
2629             Install_Withed_Unit (Item);
2630
2631             Decl_Node := Unit_Declaration_Node (Uname_Node);
2632
2633             --  If the unit is a subprogram instance, it appears nested
2634             --  within a package that carries the parent information.
2635
2636             if Is_Generic_Instance (Uname_Node)
2637               and then Ekind (Uname_Node) /= E_Package
2638             then
2639                Decl_Node := Parent (Parent (Decl_Node));
2640             end if;
2641
2642             if Is_Child_Spec (Decl_Node) then
2643                if Nkind (Name (Item)) = N_Expanded_Name then
2644                   Expand_With_Clause (Prefix (Name (Item)), N);
2645                else
2646                   --  if not an expanded name, the child unit must be a
2647                   --  renaming, nothing to do.
2648
2649                   null;
2650                end if;
2651
2652             elsif Nkind (Decl_Node) = N_Subprogram_Body
2653               and then not Acts_As_Spec (Parent (Decl_Node))
2654               and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
2655             then
2656                Implicit_With_On_Parent
2657                  (Unit (Library_Unit (Parent (Decl_Node))), N);
2658             end if;
2659
2660             --  Check license conditions unless this is a dummy unit
2661
2662             if Sloc (Library_Unit (Item)) /= No_Location then
2663                License_Check : declare
2664                   Withl : constant License_Type :=
2665                             License (Source_Index
2666                                        (Get_Source_Unit
2667                                          (Library_Unit (Item))));
2668
2669                   Unitl : constant License_Type :=
2670                            License (Source_Index (Current_Sem_Unit));
2671
2672                   procedure License_Error;
2673                   --  Signal error of bad license
2674
2675                   -------------------
2676                   -- License_Error --
2677                   -------------------
2678
2679                   procedure License_Error is
2680                   begin
2681                      Error_Msg_N
2682                        ("?license of with'ed unit & is incompatible",
2683                         Name (Item));
2684                   end License_Error;
2685
2686                --  Start of processing for License_Check
2687
2688                begin
2689                   case Unitl is
2690                      when Unknown =>
2691                         null;
2692
2693                      when Restricted =>
2694                         if Withl = GPL then
2695                            License_Error;
2696                         end if;
2697
2698                      when GPL =>
2699                         if Withl = Restricted then
2700                            License_Error;
2701                         end if;
2702
2703                      when Modified_GPL =>
2704                         if Withl = Restricted or else Withl = GPL then
2705                            License_Error;
2706                         end if;
2707
2708                      when Unrestricted =>
2709                         null;
2710                   end case;
2711                end License_Check;
2712             end if;
2713
2714          --  Case of USE PACKAGE clause
2715
2716          elsif Nkind (Item) = N_Use_Package_Clause then
2717             Analyze_Use_Package (Item);
2718
2719          --  Case of USE TYPE clause
2720
2721          elsif Nkind (Item) = N_Use_Type_Clause then
2722             Analyze_Use_Type (Item);
2723
2724          --  Case of WITH TYPE clause
2725
2726          --  A With_Type_Clause is processed when installing the context,
2727          --  because it is a visibility mechanism and does not create a
2728          --  semantic dependence on other units, as a With_Clause does.
2729
2730          elsif Nkind (Item) = N_With_Type_Clause then
2731             Analyze_With_Type_Clause (Item);
2732
2733          --  case of PRAGMA
2734
2735          elsif Nkind (Item) = N_Pragma then
2736             Analyze (Item);
2737          end if;
2738
2739       <<Continue>>
2740          Next (Item);
2741       end loop;
2742
2743       if Is_Child_Spec (Lib_Unit) then
2744
2745          --  The unit also has implicit withs on its own parents
2746
2747          if No (Context_Items (N)) then
2748             Set_Context_Items (N, New_List);
2749          end if;
2750
2751          Implicit_With_On_Parent (Lib_Unit, N);
2752       end if;
2753
2754       --  If the unit is a body, the context of the specification must also
2755       --  be installed.
2756
2757       if Nkind (Lib_Unit) = N_Package_Body
2758         or else (Nkind (Lib_Unit) = N_Subprogram_Body
2759                   and then not Acts_As_Spec (N))
2760       then
2761          Install_Context (Library_Unit (N));
2762
2763          if Is_Child_Spec (Unit (Library_Unit (N))) then
2764
2765             --  If the unit is the body of a public child unit, the private
2766             --  declarations of the parent must be made visible. If the child
2767             --  unit is private, the private declarations have been installed
2768             --  already in the call to Install_Parents for the spec. Installing
2769             --  private declarations must be done for all ancestors of public
2770             --  child units. In addition, sibling units mentioned in the
2771             --  context clause of the body are directly visible.
2772
2773             declare
2774                Lib_Spec : Node_Id := Unit (Library_Unit (N));
2775                P        : Node_Id;
2776                P_Name   : Entity_Id;
2777
2778             begin
2779                while Is_Child_Spec (Lib_Spec) loop
2780                   P := Unit (Parent_Spec (Lib_Spec));
2781
2782                   if not (Private_Present (Parent (Lib_Spec))) then
2783                      P_Name := Defining_Entity (P);
2784                      Install_Private_Declarations (P_Name);
2785                      Install_Private_With_Clauses (P_Name);
2786                      Set_Use (Private_Declarations (Specification (P)));
2787                   end if;
2788
2789                   Lib_Spec := P;
2790                end loop;
2791             end;
2792          end if;
2793
2794          --  For a package body, children in context are immediately visible
2795
2796          Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
2797       end if;
2798
2799       if Nkind (Lib_Unit) = N_Generic_Package_Declaration
2800         or else Nkind (Lib_Unit) = N_Generic_Subprogram_Declaration
2801         or else Nkind (Lib_Unit) = N_Package_Declaration
2802         or else Nkind (Lib_Unit) = N_Subprogram_Declaration
2803       then
2804          if Is_Child_Spec (Lib_Unit) then
2805             Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
2806             Set_Is_Private_Descendant
2807               (Defining_Entity (Lib_Unit),
2808                Is_Private_Descendant (Lib_Parent)
2809                  or else Private_Present (Parent (Lib_Unit)));
2810
2811          else
2812             Set_Is_Private_Descendant
2813               (Defining_Entity (Lib_Unit),
2814                Private_Present (Parent (Lib_Unit)));
2815          end if;
2816       end if;
2817
2818       if Check_Private then
2819          Check_Private_Child_Unit (N);
2820       end if;
2821    end Install_Context_Clauses;
2822
2823    -------------------------------------
2824    -- Install_Limited_Context_Clauses --
2825    -------------------------------------
2826
2827    procedure Install_Limited_Context_Clauses (N : Node_Id) is
2828       Item : Node_Id;
2829
2830       procedure Check_Parent (P : Node_Id; W : Node_Id);
2831       --  Check that the unlimited view of a given compilation_unit is not
2832       --  already visible in the parents (neither immediately through the
2833       --  context clauses, nor indirectly through "use + renamings").
2834
2835       procedure Check_Private_Limited_Withed_Unit (N : Node_Id);
2836       --  Check that if a limited_with clause of a given compilation_unit
2837       --  mentions a private child of some library unit, then the given
2838       --  compilation_unit shall be the declaration of a private descendant
2839       --  of that library unit.
2840
2841       procedure Check_Withed_Unit (W : Node_Id);
2842       --  Check that a limited with_clause does not appear in the same
2843       --  context_clause as a nonlimited with_clause that mentions
2844       --  the same library.
2845
2846       --------------------
2847       --  Check_Parent  --
2848       --------------------
2849
2850       procedure Check_Parent (P : Node_Id; W : Node_Id) is
2851          Item   : Node_Id;
2852          Spec   : Node_Id;
2853          WEnt   : Entity_Id;
2854          Nam    : Node_Id;
2855          E      : Entity_Id;
2856          E2     : Entity_Id;
2857
2858       begin
2859          pragma Assert (Nkind (W) = N_With_Clause);
2860
2861          --  Step 1: Check if the unlimited view is installed in the parent
2862
2863          Item := First (Context_Items (P));
2864          while Present (Item) loop
2865             if Nkind (Item) = N_With_Clause
2866               and then not Limited_Present (Item)
2867               and then not Implicit_With (Item)
2868               and then Library_Unit (Item) = Library_Unit (W)
2869             then
2870                Error_Msg_N ("unlimited view visible in ancestor", W);
2871                return;
2872             end if;
2873
2874             Next (Item);
2875          end loop;
2876
2877          --  Step 2: Check "use + renamings"
2878
2879          WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
2880          Spec := Specification (Unit (P));
2881
2882          --  We tried to traverse the list of entities corresponding to the
2883          --  defining entity of the package spec. However, first_entity was
2884          --  found to be 'empty'. Don't know why???
2885
2886          --          Def  := Defining_Unit_Name (Spec);
2887          --          Ent  := First_Entity (Def);
2888
2889          --  As a workaround we traverse the list of visible declarations ???
2890
2891          Item := First (Visible_Declarations (Spec));
2892          while Present (Item) loop
2893
2894             if Nkind (Item) = N_Use_Package_Clause then
2895
2896                --  Traverse the list of packages
2897
2898                Nam := First (Names (Item));
2899
2900                while Present (Nam) loop
2901                   E := Entity (Nam);
2902
2903                   pragma Assert (Present (Parent (E)));
2904
2905                   if Nkind (Parent (E))
2906                     = N_Package_Renaming_Declaration
2907                     and then Renamed_Entity (E) = WEnt
2908                   then
2909                      Error_Msg_N ("unlimited view visible through "
2910                                   & "use_clause + renamings", W);
2911                      return;
2912
2913                   elsif Nkind (Parent (E)) = N_Package_Specification then
2914
2915                      --  The use clause may refer to a local package.
2916                      --  Check all the enclosing scopes.
2917
2918                      E2 := E;
2919                      while E2 /= Standard_Standard
2920                        and then E2 /= WEnt loop
2921                         E2 := Scope (E2);
2922                      end loop;
2923
2924                      if E2 = WEnt then
2925                         Error_Msg_N ("unlimited view visible through "
2926                                      & "use_clause ", W);
2927                         return;
2928                      end if;
2929
2930                   end if;
2931                   Next (Nam);
2932                end loop;
2933
2934             end if;
2935
2936             Next (Item);
2937          end loop;
2938
2939          --  Recursive call to check all the ancestors
2940
2941          if Is_Child_Spec (Unit (P)) then
2942             Check_Parent (P => Parent_Spec (Unit (P)), W => W);
2943          end if;
2944       end Check_Parent;
2945
2946       ---------------------------------------
2947       -- Check_Private_Limited_Withed_Unit --
2948       ---------------------------------------
2949
2950       procedure Check_Private_Limited_Withed_Unit (N : Node_Id) is
2951          C     : Node_Id;
2952          P     : Node_Id;
2953          Found : Boolean := False;
2954
2955       begin
2956          --  If the current compilation unit is not private we don't
2957          --  need to check anything else.
2958
2959          if not Private_Present (Parent (N)) then
2960             Found := False;
2961
2962          else
2963             --  Compilation unit of the parent of the withed library unit
2964
2965             P := Parent_Spec (Unit (Library_Unit (N)));
2966
2967             --  Traverse all the ancestors of the current compilation
2968             --  unit to check if it is a descendant of named library unit.
2969
2970             C := Parent (N);
2971             while Present (Parent_Spec (Unit (C))) loop
2972                C := Parent_Spec (Unit (C));
2973
2974                if C = P then
2975                   Found := True;
2976                   exit;
2977                end if;
2978             end loop;
2979          end if;
2980
2981          if not Found then
2982             Error_Msg_N ("current unit is not a private descendant"
2983                          & " of the withed unit ('R'M 10.1.2(8)", N);
2984          end if;
2985       end Check_Private_Limited_Withed_Unit;
2986
2987       -----------------------
2988       -- Check_Withed_Unit --
2989       -----------------------
2990
2991       procedure Check_Withed_Unit (W : Node_Id) is
2992          Item : Node_Id;
2993
2994       begin
2995          --  A limited with_clause can not appear in the same context_clause
2996          --  as a nonlimited with_clause which mentions the same library.
2997
2998          Item := First (Context_Items (N));
2999          while Present (Item) loop
3000             if Nkind (Item) = N_With_Clause
3001               and then not Limited_Present (Item)
3002               and then not Implicit_With (Item)
3003               and then Library_Unit (Item) = Library_Unit (W)
3004             then
3005                Error_Msg_N ("limited and unlimited view "
3006                             & "not allowed in the same context clauses", W);
3007                return;
3008             end if;
3009
3010             Next (Item);
3011          end loop;
3012       end Check_Withed_Unit;
3013
3014    --  Start of processing for Install_Limited_Context_Clauses
3015
3016    begin
3017       Item := First (Context_Items (N));
3018       while Present (Item) loop
3019          if Nkind (Item) = N_With_Clause
3020            and then Limited_Present (Item)
3021          then
3022
3023             Check_Withed_Unit (Item);
3024
3025             if Private_Present (Library_Unit (Item)) then
3026                Check_Private_Limited_Withed_Unit (Item);
3027             end if;
3028
3029             if Is_Child_Spec (Unit (N)) then
3030                Check_Parent (Parent_Spec (Unit (N)), Item);
3031             end if;
3032
3033             Install_Limited_Withed_Unit (Item);
3034          end if;
3035
3036          Next (Item);
3037       end loop;
3038    end Install_Limited_Context_Clauses;
3039
3040    ---------------------
3041    -- Install_Parents --
3042    ---------------------
3043
3044    procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3045       P      : Node_Id;
3046       E_Name : Entity_Id;
3047       P_Name : Entity_Id;
3048       P_Spec : Node_Id;
3049
3050    begin
3051       P := Unit (Parent_Spec (Lib_Unit));
3052       P_Name := Get_Parent_Entity (P);
3053
3054       if Etype (P_Name) = Any_Type then
3055          return;
3056       end if;
3057
3058       if Ekind (P_Name) = E_Generic_Package
3059         and then Nkind (Lib_Unit) /= N_Generic_Subprogram_Declaration
3060         and then Nkind (Lib_Unit) /= N_Generic_Package_Declaration
3061         and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3062       then
3063          Error_Msg_N
3064            ("child of a generic package must be a generic unit", Lib_Unit);
3065
3066       elsif not Is_Package (P_Name) then
3067          Error_Msg_N
3068            ("parent unit must be package or generic package", Lib_Unit);
3069          raise Unrecoverable_Error;
3070
3071       elsif Present (Renamed_Object (P_Name)) then
3072          Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3073          raise Unrecoverable_Error;
3074
3075       --  Verify that a child of an instance is itself an instance, or
3076       --  the renaming of one. Given that an instance that is a unit is
3077       --  replaced with a package declaration, check against the original
3078       --  node.
3079
3080       elsif Nkind (Original_Node (P)) = N_Package_Instantiation
3081         and then Nkind (Lib_Unit)
3082                    not in N_Renaming_Declaration
3083         and then Nkind (Original_Node (Lib_Unit))
3084                    not in N_Generic_Instantiation
3085       then
3086          Error_Msg_N
3087            ("child of an instance must be an instance or renaming", Lib_Unit);
3088       end if;
3089
3090       --  This is the recursive call that ensures all parents are loaded
3091
3092       if Is_Child_Spec (P) then
3093          Install_Parents (P,
3094            Is_Private or else Private_Present (Parent (Lib_Unit)));
3095       end if;
3096
3097       --  Now we can install the context for this parent
3098
3099       Install_Context_Clauses (Parent_Spec (Lib_Unit));
3100       Install_Siblings (P_Name, Parent (Lib_Unit));
3101
3102       --  The child unit is in the declarative region of the parent. The
3103       --  parent must therefore appear in the scope stack and be visible,
3104       --  as when compiling the corresponding body. If the child unit is
3105       --  private or it is a package body, private declarations must be
3106       --  accessible as well. Use declarations in the parent must also
3107       --  be installed. Finally, other child units of the same parent that
3108       --  are in the context are immediately visible.
3109
3110       --  Find entity for compilation unit, and set its private descendant
3111       --  status as needed.
3112
3113       E_Name := Defining_Entity (Lib_Unit);
3114
3115       Set_Is_Child_Unit (E_Name);
3116
3117       Set_Is_Private_Descendant (E_Name,
3118          Is_Private_Descendant (P_Name)
3119            or else Private_Present (Parent (Lib_Unit)));
3120
3121       P_Spec := Specification (Unit_Declaration_Node (P_Name));
3122       New_Scope (P_Name);
3123
3124       --  Save current visibility of unit
3125
3126       Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3127         Is_Immediately_Visible (P_Name);
3128       Set_Is_Immediately_Visible (P_Name);
3129       Install_Visible_Declarations (P_Name);
3130       Set_Use (Visible_Declarations (P_Spec));
3131
3132       --  If the parent is a generic unit, its formal part may contain
3133       --  formal packages and use clauses for them.
3134
3135       if Ekind (P_Name) = E_Generic_Package then
3136          Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3137       end if;
3138
3139       if Is_Private
3140         or else Private_Present (Parent (Lib_Unit))
3141       then
3142          Install_Private_Declarations (P_Name);
3143          Install_Private_With_Clauses (P_Name);
3144          Set_Use (Private_Declarations (P_Spec));
3145       end if;
3146    end Install_Parents;
3147
3148    ----------------------------------
3149    -- Install_Private_With_Clauses --
3150    ----------------------------------
3151
3152    procedure Install_Private_With_Clauses (P : Entity_Id) is
3153       Decl   : constant Node_Id := Unit_Declaration_Node (P);
3154       Clause : Node_Id;
3155
3156    begin
3157       if Nkind (Parent (Decl)) = N_Compilation_Unit then
3158          Clause := First (Context_Items (Parent (Decl)));
3159          while Present (Clause) loop
3160             if Nkind (Clause) = N_With_Clause
3161               and then Private_Present (Clause)
3162             then
3163                Install_Withed_Unit (Clause, Private_With_OK => True);
3164             end if;
3165
3166             Next (Clause);
3167          end loop;
3168       end if;
3169    end Install_Private_With_Clauses;
3170
3171    ----------------------
3172    -- Install_Siblings --
3173    ----------------------
3174
3175    procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3176       Item : Node_Id;
3177       Id   : Entity_Id;
3178       Prev : Entity_Id;
3179
3180       function Is_Ancestor (E : Entity_Id) return Boolean;
3181       --  Determine whether the scope of a child unit is an ancestor of
3182       --  the current unit.
3183       --  Shouldn't this be somewhere more general ???
3184
3185       -----------------
3186       -- Is_Ancestor --
3187       -----------------
3188
3189       function Is_Ancestor (E : Entity_Id) return Boolean is
3190          Par : Entity_Id;
3191
3192       begin
3193          Par := U_Name;
3194          while Present (Par)
3195            and then Par /= Standard_Standard
3196          loop
3197             if Par = E then
3198                return True;
3199             end if;
3200
3201             Par := Scope (Par);
3202          end loop;
3203
3204          return False;
3205       end Is_Ancestor;
3206
3207    --  Start of processing for Install_Siblings
3208
3209    begin
3210       --  Iterate over explicit with clauses, and check whether the
3211       --  scope of each entity is an ancestor of the current unit.
3212
3213       Item := First (Context_Items (N));
3214       while Present (Item) loop
3215          if Nkind (Item) = N_With_Clause
3216            and then not Implicit_With (Item)
3217            and then not Limited_Present (Item)
3218          then
3219             Id := Entity (Name (Item));
3220
3221             if Is_Child_Unit (Id)
3222               and then Is_Ancestor (Scope (Id))
3223             then
3224                Set_Is_Immediately_Visible (Id);
3225                Prev := Current_Entity (Id);
3226
3227                --  Check for the presence of another unit in the context,
3228                --  that may be inadvertently hidden by the child.
3229
3230                if Present (Prev)
3231                  and then Is_Immediately_Visible (Prev)
3232                  and then not Is_Child_Unit (Prev)
3233                then
3234                   declare
3235                      Clause : Node_Id;
3236
3237                   begin
3238                      Clause := First (Context_Items (N));
3239
3240                      while Present (Clause) loop
3241                         if Nkind (Clause) = N_With_Clause
3242                           and then Entity (Name (Clause)) = Prev
3243                         then
3244                            Error_Msg_NE
3245                               ("child unit& hides compilation unit " &
3246                                "with the same name?",
3247                                  Name (Item), Id);
3248                            exit;
3249                         end if;
3250
3251                         Next (Clause);
3252                      end loop;
3253                   end;
3254                end if;
3255
3256             --  the With_Clause may be on a grand-child, which makes
3257             --  the child immediately visible.
3258
3259             elsif Is_Child_Unit (Scope (Id))
3260               and then Is_Ancestor (Scope (Scope (Id)))
3261             then
3262                Set_Is_Immediately_Visible (Scope (Id));
3263             end if;
3264          end if;
3265
3266          Next (Item);
3267       end loop;
3268    end Install_Siblings;
3269
3270    -------------------------------
3271    -- Install_Limited_With_Unit --
3272    -------------------------------
3273
3274    procedure Install_Limited_Withed_Unit (N : Node_Id) is
3275       Unum             : constant Unit_Number_Type :=
3276                            Get_Source_Unit (Library_Unit (N));
3277       P_Unit           : constant Entity_Id := Unit (Library_Unit (N));
3278       P                : Entity_Id;
3279       Lim_Elmt         : Elmt_Id;
3280       Lim_Typ          : Entity_Id;
3281       Is_Child_Package : Boolean := False;
3282
3283       function In_Chain (E : Entity_Id) return Boolean;
3284       --  Check that the shadow entity is not already in the homonym
3285       --  chain, for example through a limited_with clause in a parent unit.
3286
3287       --------------
3288       -- In_Chain --
3289       --------------
3290
3291       function In_Chain (E : Entity_Id) return Boolean is
3292          H : Entity_Id := Current_Entity (E);
3293
3294       begin
3295          while Present (H) loop
3296             if H = E then
3297                return True;
3298             else
3299                H := Homonym (H);
3300             end if;
3301          end loop;
3302
3303          return False;
3304       end In_Chain;
3305
3306    --  Start of processing for Install_Limited_Withed_Unit
3307
3308    begin
3309       --  In case of limited with_clause on subprograms, generics, instances,
3310       --  or generic renamings, the corresponding error was previously posted
3311       --  and we have nothing to do here.
3312
3313       case Nkind (P_Unit) is
3314
3315          when N_Package_Declaration =>
3316             null;
3317
3318          when N_Subprogram_Declaration                 |
3319               N_Generic_Package_Declaration            |
3320               N_Generic_Subprogram_Declaration         |
3321               N_Package_Instantiation                  |
3322               N_Function_Instantiation                 |
3323               N_Procedure_Instantiation                |
3324               N_Generic_Package_Renaming_Declaration   |
3325               N_Generic_Procedure_Renaming_Declaration |
3326               N_Generic_Function_Renaming_Declaration =>
3327             return;
3328
3329          when others =>
3330             pragma Assert (False);
3331             null;
3332       end case;
3333
3334       P := Defining_Unit_Name (Specification (P_Unit));
3335
3336       if Nkind (P) = N_Defining_Program_Unit_Name then
3337
3338          --  Retrieve entity of child package
3339
3340          Is_Child_Package := True;
3341          P := Defining_Identifier (P);
3342       end if;
3343
3344       --  A common usage of the limited-with is to have a limited-with
3345       --  in the package spec, and a normal with in its package body.
3346       --  For example:
3347
3348       --       limited with X;  -- [1]
3349       --       package A is ...
3350
3351       --       with X;          -- [2]
3352       --       package body A is ...
3353
3354       --  The compilation of A's body installs the entities of its
3355       --  withed packages (the context clauses found at [2]) and
3356       --  then the context clauses of its specification (found at [1]).
3357
3358       --  As a consequence, at point [1] the specification of X has been
3359       --  analyzed and it is immediately visible. According to the semantics
3360       --  of the limited-with context clauses we don't install the limited
3361       --  view because the full view of X supersedes its limited view.
3362
3363       if Analyzed (Cunit (Unum))
3364         and then Is_Immediately_Visible (P)
3365       then
3366          return;
3367       end if;
3368
3369       if Debug_Flag_I then
3370          Write_Str ("install limited view of ");
3371          Write_Name (Chars (P));
3372          Write_Eol;
3373       end if;
3374
3375       if not Analyzed (Cunit (Unum)) then
3376          Set_Ekind (P, E_Package);
3377          Set_Etype (P, Standard_Void_Type);
3378          Set_Scope (P, Standard_Standard);
3379
3380          --  Place entity on visibility structure
3381
3382          if Current_Entity (P) /= P then
3383             Set_Homonym (P, Current_Entity (P));
3384             Set_Current_Entity (P);
3385
3386             if Debug_Flag_I then
3387                Write_Str ("   (homonym) chain ");
3388                Write_Name (Chars (P));
3389                Write_Eol;
3390             end if;
3391
3392          end if;
3393
3394          if Is_Child_Package then
3395             Set_Is_Child_Unit (P);
3396             Set_Is_Visible_Child_Unit (P);
3397
3398             declare
3399                Parent_Comp : Node_Id;
3400                Parent_Id   : Entity_Id;
3401
3402             begin
3403                Parent_Comp := Parent_Spec (Unit (Cunit (Unum)));
3404                Parent_Id   := Defining_Entity (Unit (Parent_Comp));
3405
3406                Set_Scope (P, Parent_Id);
3407             end;
3408          end if;
3409
3410       else
3411
3412          --  If the unit appears in a previous regular with_clause, the
3413          --  regular entities must be unchained before the shadow ones
3414          --  are made accessible.
3415
3416          declare
3417             Ent : Entity_Id;
3418          begin
3419             Ent := First_Entity (P);
3420
3421             while Present (Ent) loop
3422                Unchain (Ent);
3423                Next_Entity (Ent);
3424             end loop;
3425          end;
3426
3427       end if;
3428
3429       --  The package must be visible while the with_type clause is active,
3430       --  because references to the type P.T must resolve in the usual way.
3431
3432       Set_Is_Immediately_Visible (P);
3433
3434       --  Install each incomplete view
3435
3436       Lim_Elmt   := First_Elmt (Limited_Views (P));
3437
3438       while Present (Lim_Elmt) loop
3439          Lim_Typ  := Node (Lim_Elmt);
3440
3441          if not In_Chain (Lim_Typ) then
3442             Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
3443             Set_Current_Entity (Lim_Typ);
3444
3445             if Debug_Flag_I then
3446                Write_Str ("   (homonym) chain ");
3447                Write_Name (Chars (Lim_Typ));
3448                Write_Eol;
3449             end if;
3450
3451          end if;
3452
3453          Next_Elmt (Lim_Elmt);
3454       end loop;
3455
3456       --  The context clause has installed a limited-view, mark it
3457       --  accordingly, to uninstall it when the context is removed.
3458
3459       Set_Limited_View_Installed (N);
3460       Set_From_With_Type (P);
3461    end Install_Limited_Withed_Unit;
3462
3463    -------------------------
3464    -- Install_Withed_Unit --
3465    -------------------------
3466
3467    procedure Install_Withed_Unit
3468      (With_Clause     : Node_Id;
3469       Private_With_OK : Boolean := False)
3470    is
3471       Uname : constant Entity_Id := Entity (Name (With_Clause));
3472       P     : constant Entity_Id := Scope (Uname);
3473
3474    begin
3475
3476       if Debug_Flag_I then
3477          Write_Str ("install withed unit ");
3478          Write_Name (Chars (Uname));
3479          Write_Eol;
3480       end if;
3481
3482       --  We do not apply the restrictions to an internal unit unless
3483       --  we are compiling the internal unit as a main unit. This check
3484       --  is also skipped for dummy units (for missing packages).
3485
3486       if Sloc (Uname) /= No_Location
3487         and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
3488                     or else Current_Sem_Unit = Main_Unit)
3489       then
3490          Check_Restricted_Unit
3491            (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
3492       end if;
3493
3494       if P /= Standard_Standard then
3495          if Private_Present (With_Clause)
3496            and then not (Private_With_OK)
3497          then
3498             return;
3499
3500          --  If the unit is not analyzed after analysis of the with clause,
3501          --  and it is an instantiation, then it awaits a body and is the main
3502          --  unit. Its appearance in the context of some other unit indicates
3503          --  a circular dependency (DEC suite perversity).
3504
3505          elsif not Analyzed (Uname)
3506            and then Nkind (Parent (Uname)) = N_Package_Instantiation
3507          then
3508             Error_Msg_N
3509               ("instantiation depends on itself", Name (With_Clause));
3510
3511          elsif not Is_Visible_Child_Unit (Uname) then
3512             Set_Is_Visible_Child_Unit (Uname);
3513
3514             if Is_Generic_Instance (Uname)
3515               and then Ekind (Uname) in Subprogram_Kind
3516             then
3517                --  Set flag as well on the visible entity that denotes the
3518                --  instance, which renames the current one.
3519
3520                Set_Is_Visible_Child_Unit
3521                  (Related_Instance
3522                    (Defining_Entity (Unit (Library_Unit (With_Clause)))));
3523                null;
3524             end if;
3525
3526             --  The parent unit may have been installed already, and
3527             --  may have appeared in a use clause.
3528
3529             if In_Use (Scope (Uname)) then
3530                Set_Is_Potentially_Use_Visible (Uname);
3531             end if;
3532
3533             Set_Context_Installed (With_Clause);
3534          end if;
3535
3536       elsif not Is_Immediately_Visible (Uname) then
3537          if not Private_Present (With_Clause)
3538            or else Private_With_OK
3539          then
3540             Set_Is_Immediately_Visible (Uname);
3541          end if;
3542
3543          Set_Context_Installed (With_Clause);
3544       end if;
3545
3546       --   A with-clause overrides a with-type clause: there are no restric-
3547       --   tions on the use of package entities.
3548
3549       if Ekind (Uname) = E_Package then
3550          Set_From_With_Type (Uname, False);
3551       end if;
3552    end Install_Withed_Unit;
3553
3554    -------------------
3555    -- Is_Child_Spec --
3556    -------------------
3557
3558    function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
3559       K : constant Node_Kind := Nkind (Lib_Unit);
3560
3561    begin
3562       return (K in N_Generic_Declaration              or else
3563               K in N_Generic_Instantiation            or else
3564               K in N_Generic_Renaming_Declaration     or else
3565               K =  N_Package_Declaration              or else
3566               K =  N_Package_Renaming_Declaration     or else
3567               K =  N_Subprogram_Declaration           or else
3568               K =  N_Subprogram_Renaming_Declaration)
3569         and then Present (Parent_Spec (Lib_Unit));
3570    end Is_Child_Spec;
3571
3572    -----------------------
3573    -- Load_Needed_Body --
3574    -----------------------
3575
3576    --  N is a generic unit named in a with clause, or else it is
3577    --  a unit that contains a generic unit or an inlined function.
3578    --  In order to perform an instantiation, the body of the unit
3579    --  must be present. If the unit itself is generic, we assume
3580    --  that an instantiation follows, and  load and analyze the body
3581    --  unconditionally. This forces analysis of the spec as well.
3582
3583    --  If the unit is not generic, but contains a generic unit, it
3584    --  is loaded on demand, at the point of instantiation (see ch12).
3585
3586    procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
3587       Body_Name : Unit_Name_Type;
3588       Unum      : Unit_Number_Type;
3589
3590       Save_Style_Check : constant Boolean := Opt.Style_Check;
3591       --  The loading and analysis is done with style checks off
3592
3593    begin
3594       if not GNAT_Mode then
3595          Style_Check := False;
3596       end if;
3597
3598       Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
3599       Unum :=
3600         Load_Unit
3601           (Load_Name  => Body_Name,
3602            Required   => False,
3603            Subunit    => False,
3604            Error_Node => N,
3605            Renamings  => True);
3606
3607       if Unum = No_Unit then
3608          OK := False;
3609
3610       else
3611          Compiler_State := Analyzing; -- reset after load
3612
3613          if not Fatal_Error (Unum) or else Try_Semantics then
3614             if Debug_Flag_L then
3615                Write_Str ("*** Loaded generic body");
3616                Write_Eol;
3617             end if;
3618
3619             Semantics (Cunit (Unum));
3620          end if;
3621
3622          OK := True;
3623       end if;
3624
3625       Style_Check := Save_Style_Check;
3626    end Load_Needed_Body;
3627
3628    -------------------------
3629    -- Build_Limited_Views --
3630    -------------------------
3631
3632    procedure Build_Limited_Views (N : Node_Id) is
3633       Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
3634       P    : constant Entity_Id        := Cunit_Entity (Unum);
3635
3636       Spec        : Node_Id;         --  To denote a package specification
3637       Lim_Typ     : Entity_Id;       --  To denote shadow entities.
3638       Comp_Typ    : Entity_Id;       --  To denote real entities.
3639
3640       procedure Decorate_Incomplete_Type
3641         (E    : Entity_Id;
3642          Scop : Entity_Id);
3643       --  Add attributes of an incomplete type to a shadow entity. The same
3644       --  attributes are placed on the real entity, so that gigi receives
3645       --  a consistent view.
3646
3647       procedure Decorate_Package_Specification (P : Entity_Id);
3648       --  Add attributes of a package entity to the entity in a package
3649       --  declaration
3650
3651       procedure Decorate_Tagged_Type
3652         (Loc  : Source_Ptr;
3653          T    : Entity_Id;
3654          Scop : Entity_Id);
3655       --  Set basic attributes of tagged type T, including its class_wide type.
3656       --  The parameters Loc, Scope are used to decorate the class_wide type.
3657
3658       procedure Build_Chain (Spec : Node_Id; Scope : Entity_Id);
3659       --  Construct list of shadow entities and attach it to entity of
3660       --  package that is mentioned in a limited_with clause.
3661
3662       function New_Internal_Shadow_Entity
3663         (Kind       : Entity_Kind;
3664          Sloc_Value : Source_Ptr;
3665          Id_Char    : Character) return Entity_Id;
3666       --  This function is similar to New_Internal_Entity, except that the
3667       --  entity is not added to the scope's list of entities.
3668
3669       ------------------------------
3670       -- Decorate_Incomplete_Type --
3671       ------------------------------
3672
3673       procedure Decorate_Incomplete_Type
3674         (E    : Entity_Id;
3675          Scop : Entity_Id)
3676       is
3677       begin
3678          Set_Ekind                     (E, E_Incomplete_Type);
3679          Set_Scope                     (E, Scop);
3680          Set_Etype                     (E, E);
3681          Set_Is_First_Subtype          (E, True);
3682          Set_Stored_Constraint         (E, No_Elist);
3683          Set_Full_View                 (E, Empty);
3684          Init_Size_Align               (E);
3685       end Decorate_Incomplete_Type;
3686
3687       --------------------------
3688       -- Decorate_Tagged_Type --
3689       --------------------------
3690
3691       procedure Decorate_Tagged_Type
3692         (Loc  : Source_Ptr;
3693          T    : Entity_Id;
3694          Scop : Entity_Id)
3695       is
3696          CW : Entity_Id;
3697
3698       begin
3699          Decorate_Incomplete_Type (T, Scop);
3700          Set_Is_Tagged_Type (T);
3701
3702          --  Build corresponding class_wide type, if not previously done
3703
3704          if No (Class_Wide_Type (T)) then
3705             CW := Make_Defining_Identifier (Loc,  New_Internal_Name ('S'));
3706
3707             Set_Ekind                     (CW, E_Class_Wide_Type);
3708             Set_Etype                     (CW, T);
3709             Set_Scope                     (CW, Scop);
3710             Set_Is_Tagged_Type            (CW);
3711             Set_Is_First_Subtype          (CW, True);
3712             Init_Size_Align               (CW);
3713             Set_Has_Unknown_Discriminants (CW, True);
3714             Set_Class_Wide_Type           (CW, CW);
3715             Set_Equivalent_Type           (CW, Empty);
3716             Set_From_With_Type            (CW, From_With_Type (T));
3717
3718             Set_Class_Wide_Type (T, CW);
3719          end if;
3720       end Decorate_Tagged_Type;
3721
3722       ------------------------------------
3723       -- Decorate_Package_Specification --
3724       ------------------------------------
3725
3726       procedure Decorate_Package_Specification (P : Entity_Id) is
3727       begin
3728          --  Place only the most basic attributes
3729
3730          Set_Ekind (P, E_Package);
3731          Set_Etype (P, Standard_Void_Type);
3732       end Decorate_Package_Specification;
3733
3734       -------------------------
3735       -- New_Internal_Entity --
3736       -------------------------
3737
3738       function New_Internal_Shadow_Entity
3739         (Kind       : Entity_Kind;
3740          Sloc_Value : Source_Ptr;
3741          Id_Char    : Character) return Entity_Id
3742       is
3743          N : constant Entity_Id :=
3744                Make_Defining_Identifier (Sloc_Value,
3745                  Chars => New_Internal_Name (Id_Char));
3746
3747       begin
3748          Set_Ekind          (N, Kind);
3749          Set_Is_Internal    (N, True);
3750
3751          if Kind in Type_Kind then
3752             Init_Size_Align (N);
3753          end if;
3754
3755          return N;
3756       end New_Internal_Shadow_Entity;
3757
3758       -----------------
3759       -- Build_Chain --
3760       -----------------
3761
3762       --  Could use more comments below ???
3763
3764       procedure Build_Chain (Spec : Node_Id; Scope : Entity_Id) is
3765          Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
3766          Is_Tagged     : Boolean;
3767          Decl          : Node_Id;
3768
3769       begin
3770          Decl := First (Visible_Declarations (Spec));
3771
3772          while Present (Decl) loop
3773             if Nkind (Decl) = N_Full_Type_Declaration then
3774                Is_Tagged :=
3775                   Nkind (Type_Definition (Decl)) = N_Record_Definition
3776                   and then Tagged_Present (Type_Definition (Decl));
3777
3778                Comp_Typ := Defining_Identifier (Decl);
3779
3780                if not Analyzed_Unit then
3781                   if Is_Tagged then
3782                      Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
3783                   else
3784                      Decorate_Incomplete_Type (Comp_Typ, Scope);
3785                   end if;
3786                end if;
3787
3788                --  Create shadow entity for type
3789
3790                Lim_Typ  := New_Internal_Shadow_Entity
3791                  (Kind       => Ekind (Comp_Typ),
3792                   Sloc_Value => Sloc (Comp_Typ),
3793                   Id_Char    => 'Z');
3794
3795                Set_Chars  (Lim_Typ, Chars (Comp_Typ));
3796                Set_Parent (Lim_Typ, Parent (Comp_Typ));
3797                Set_From_With_Type (Lim_Typ);
3798
3799                if Is_Tagged then
3800                   Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
3801                else
3802                   Decorate_Incomplete_Type (Lim_Typ, Scope);
3803                end if;
3804
3805                Set_Non_Limited_View (Lim_Typ, Comp_Typ);
3806                Append_Elmt (Lim_Typ,  To => Limited_Views (P));
3807
3808             elsif Nkind (Decl) = N_Private_Type_Declaration
3809               and then Tagged_Present (Decl)
3810             then
3811                Comp_Typ := Defining_Identifier (Decl);
3812
3813                if not Analyzed_Unit then
3814                   Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
3815                end if;
3816
3817                Lim_Typ  := New_Internal_Shadow_Entity
3818                  (Kind       => Ekind (Comp_Typ),
3819                   Sloc_Value => Sloc (Comp_Typ),
3820                   Id_Char    => 'Z');
3821
3822                Set_Chars  (Lim_Typ, Chars (Comp_Typ));
3823                Set_Parent (Lim_Typ, Parent (Comp_Typ));
3824                Set_From_With_Type (Lim_Typ);
3825
3826                Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
3827
3828                Set_Non_Limited_View (Lim_Typ, Comp_Typ);
3829                Append_Elmt (Lim_Typ,  To => Limited_Views (P));
3830
3831             elsif Nkind (Decl) = N_Package_Declaration then
3832
3833                --  Local package
3834
3835                declare
3836                   Spec : constant Node_Id := Specification (Decl);
3837
3838                begin
3839                   Comp_Typ := Defining_Unit_Name (Spec);
3840
3841                   if not Analyzed (Cunit (Unum)) then
3842                      Decorate_Package_Specification (Comp_Typ);
3843                      Set_Scope (Comp_Typ, Scope);
3844                   end if;
3845
3846                   Lim_Typ  := New_Internal_Shadow_Entity
3847                     (Kind       => Ekind (Comp_Typ),
3848                      Sloc_Value => Sloc (Comp_Typ),
3849                      Id_Char    => 'Z');
3850
3851                   Decorate_Package_Specification (Lim_Typ);
3852                   Set_Scope (Lim_Typ, Scope);
3853
3854                   Set_Chars (Lim_Typ, Chars (Comp_Typ));
3855                   Set_Parent (Lim_Typ, Parent (Comp_Typ));
3856                   Set_From_With_Type (Lim_Typ);
3857
3858                   --  Note: The non_limited_view attribute is not used
3859                   --  for local packages.
3860
3861                   Append_Elmt (Lim_Typ,  To => Limited_Views (P));
3862
3863                   Build_Chain (Spec, Scope => Lim_Typ);
3864                end;
3865             end if;
3866
3867             Next (Decl);
3868          end loop;
3869       end Build_Chain;
3870
3871    --  Start of processing for Build_Limited_Views
3872
3873    begin
3874       pragma Assert (Limited_Present (N));
3875
3876       --  A library_item mentioned in a limited_with_clause shall be
3877       --  a package_declaration, not a subprogram_declaration,
3878       --  generic_declaration, generic_instantiation, or
3879       --  package_renaming_declaration
3880
3881       case Nkind (Unit (Library_Unit (N))) is
3882
3883          when N_Package_Declaration =>
3884             null;
3885
3886          when N_Subprogram_Declaration =>
3887             Error_Msg_N ("subprograms not allowed in "
3888                          & "limited with_clauses", N);
3889             return;
3890
3891          when N_Generic_Package_Declaration |
3892               N_Generic_Subprogram_Declaration =>
3893             Error_Msg_N ("generics not allowed in "
3894                          & "limited with_clauses", N);
3895             return;
3896
3897          when N_Package_Instantiation |
3898               N_Function_Instantiation |
3899               N_Procedure_Instantiation =>
3900             Error_Msg_N ("generic instantiations not allowed in "
3901                          & "limited with_clauses", N);
3902             return;
3903
3904          when N_Generic_Package_Renaming_Declaration |
3905               N_Generic_Procedure_Renaming_Declaration |
3906               N_Generic_Function_Renaming_Declaration =>
3907             Error_Msg_N ("generic renamings not allowed in "
3908                          & "limited with_clauses", N);
3909             return;
3910
3911          when others =>
3912             pragma Assert (False);
3913             null;
3914       end case;
3915
3916       --  Check if the chain is already built
3917
3918       Spec := Specification (Unit (Library_Unit (N)));
3919
3920       if Limited_View_Installed (Spec) then
3921          return;
3922       end if;
3923
3924       Set_Ekind (P, E_Package);
3925       Set_Limited_Views     (P, New_Elmt_List);
3926       --  Set_Entity (Name (N), P);
3927
3928       --  Create the auxiliary chain
3929
3930       Build_Chain (Spec, Scope => P);
3931       Set_Limited_View_Installed (Spec);
3932    end Build_Limited_Views;
3933
3934    -------------------------------
3935    -- Check_Body_Needed_For_SAL --
3936    -------------------------------
3937
3938    procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
3939
3940       function Entity_Needs_Body (E : Entity_Id) return Boolean;
3941       --  Determine whether use of entity E might require the presence
3942       --  of its body. For a package this requires a recursive traversal
3943       --  of all nested declarations.
3944
3945       ---------------------------
3946       -- Entity_Needed_For_SAL --
3947       ---------------------------
3948
3949       function Entity_Needs_Body (E : Entity_Id) return Boolean is
3950          Ent : Entity_Id;
3951
3952       begin
3953          if Is_Subprogram (E)
3954            and then Has_Pragma_Inline (E)
3955          then
3956             return True;
3957
3958          elsif Ekind (E) = E_Generic_Function
3959            or else Ekind (E) = E_Generic_Procedure
3960          then
3961             return True;
3962
3963          elsif Ekind (E) = E_Generic_Package
3964            and then
3965              Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
3966            and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
3967          then
3968             return True;
3969
3970          elsif Ekind (E) = E_Package
3971            and then
3972              Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
3973            and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
3974          then
3975             Ent := First_Entity (E);
3976
3977             while Present (Ent) loop
3978                if Entity_Needs_Body (Ent) then
3979                   return True;
3980                end if;
3981
3982                Next_Entity (Ent);
3983             end loop;
3984
3985             return False;
3986
3987          else
3988             return False;
3989          end if;
3990       end Entity_Needs_Body;
3991
3992    --  Start of processing for Check_Body_Needed_For_SAL
3993
3994    begin
3995       if Ekind (Unit_Name) = E_Generic_Package
3996         and then
3997           Nkind (Unit_Declaration_Node (Unit_Name)) =
3998                                             N_Generic_Package_Declaration
3999         and then
4000           Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
4001       then
4002          Set_Body_Needed_For_SAL (Unit_Name);
4003
4004       elsif Ekind (Unit_Name) = E_Generic_Procedure
4005         or else Ekind (Unit_Name) = E_Generic_Function
4006       then
4007          Set_Body_Needed_For_SAL (Unit_Name);
4008
4009       elsif Is_Subprogram (Unit_Name)
4010         and then Nkind (Unit_Declaration_Node (Unit_Name)) =
4011                                             N_Subprogram_Declaration
4012         and then Has_Pragma_Inline (Unit_Name)
4013       then
4014          Set_Body_Needed_For_SAL (Unit_Name);
4015
4016       elsif Ekind (Unit_Name) = E_Subprogram_Body then
4017          Check_Body_Needed_For_SAL
4018            (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
4019
4020       elsif Ekind (Unit_Name) = E_Package
4021         and then Entity_Needs_Body (Unit_Name)
4022       then
4023          Set_Body_Needed_For_SAL (Unit_Name);
4024
4025       elsif Ekind (Unit_Name) = E_Package_Body
4026         and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
4027       then
4028          Check_Body_Needed_For_SAL
4029            (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
4030       end if;
4031    end Check_Body_Needed_For_SAL;
4032
4033    --------------------
4034    -- Remove_Context --
4035    --------------------
4036
4037    procedure Remove_Context (N : Node_Id) is
4038       Lib_Unit : constant Node_Id := Unit (N);
4039
4040    begin
4041       --  If this is a child unit, first remove the parent units.
4042
4043       if Is_Child_Spec (Lib_Unit) then
4044          Remove_Parents (Lib_Unit);
4045       end if;
4046
4047       Remove_Context_Clauses (N);
4048    end Remove_Context;
4049
4050    ----------------------------
4051    -- Remove_Context_Clauses --
4052    ----------------------------
4053
4054    procedure Remove_Context_Clauses (N : Node_Id) is
4055       Item      : Node_Id;
4056       Unit_Name : Entity_Id;
4057
4058    begin
4059       --  Ada0Y (AI-50217): We remove the context clauses in two phases:
4060       --  limited-views first and regular-views later (to maintain the
4061       --  stack model).
4062
4063       --  First Phase: Remove limited_with context clauses
4064
4065       Item := First (Context_Items (N));
4066       while Present (Item) loop
4067
4068          --  We are interested only in with clauses which got installed
4069          --  on entry.
4070
4071          if Nkind (Item) = N_With_Clause
4072            and then Limited_Present (Item)
4073            and then Limited_View_Installed (Item)
4074          then
4075             Remove_Limited_With_Clause (Item);
4076
4077          end if;
4078
4079          Next (Item);
4080       end loop;
4081
4082       --  Second Phase: Loop through context items and undo regular
4083       --  with_clauses and use_clauses.
4084
4085       Item := First (Context_Items (N));
4086       while Present (Item) loop
4087
4088          --  We are interested only in with clauses which got installed
4089          --  on entry, as indicated by their Context_Installed flag set
4090
4091          if Nkind (Item) = N_With_Clause
4092            and then Limited_Present (Item)
4093            and then Limited_View_Installed (Item)
4094          then
4095             null;
4096
4097          elsif Nkind (Item) = N_With_Clause
4098             and then Context_Installed (Item)
4099          then
4100             --  Remove items from one with'ed unit
4101
4102             Unit_Name := Entity (Name (Item));
4103             Remove_Unit_From_Visibility (Unit_Name);
4104             Set_Context_Installed (Item, False);
4105
4106          elsif Nkind (Item) = N_Use_Package_Clause then
4107             End_Use_Package (Item);
4108
4109          elsif Nkind (Item) = N_Use_Type_Clause then
4110             End_Use_Type (Item);
4111
4112          elsif Nkind (Item) = N_With_Type_Clause then
4113             Remove_With_Type_Clause (Name (Item));
4114          end if;
4115
4116          Next (Item);
4117       end loop;
4118    end Remove_Context_Clauses;
4119
4120    --------------------------------
4121    -- Remove_Limited_With_Clause --
4122    --------------------------------
4123
4124    procedure Remove_Limited_With_Clause (N : Node_Id) is
4125       P_Unit    : constant Entity_Id := Unit (Library_Unit (N));
4126       P         : Entity_Id := Defining_Unit_Name (Specification (P_Unit));
4127       Lim_Elmt  : Elmt_Id;
4128       Lim_Typ   : Entity_Id;
4129
4130    begin
4131       if Nkind (P) = N_Defining_Program_Unit_Name then
4132
4133          --  Retrieve entity of Child package
4134
4135          P := Defining_Identifier (P);
4136       end if;
4137
4138       if Debug_Flag_I then
4139          Write_Str ("remove limited view of ");
4140          Write_Name (Chars (P));
4141          Write_Str (" from visibility");
4142          Write_Eol;
4143       end if;
4144
4145       --  Remove all shadow entities from visibility
4146
4147       Lim_Elmt  := First_Elmt (Limited_Views (P));
4148
4149       while Present (Lim_Elmt) loop
4150          Lim_Typ  := Node (Lim_Elmt);
4151
4152          Unchain (Lim_Typ);
4153          Next_Elmt (Lim_Elmt);
4154       end loop;
4155
4156       --  Indicate that the limited view of the package is not installed
4157
4158       Set_From_With_Type (P, False);
4159       Set_Limited_View_Installed (N, False);
4160
4161       --  If the exporting package has previously been analyzed, it
4162       --  has appeared in the closure already and should be left alone.
4163       --  Otherwise, remove package itself from visibility.
4164
4165       if not Analyzed (P_Unit) then
4166          Unchain (P);
4167          Set_First_Entity (P, Empty);
4168          Set_Last_Entity (P, Empty);
4169          Set_Ekind (P, E_Void);
4170          Set_Scope (P, Empty);
4171          Set_Is_Immediately_Visible (P, False);
4172
4173       else
4174
4175          --  Reinstall visible entities (entities removed from visibility in
4176          --  Install_Limited_Withed to install the shadow entities).
4177
4178          declare
4179             Ent : Entity_Id;
4180
4181          begin
4182             Ent := First_Entity (P);
4183             while Present (Ent) and then Ent /= First_Private_Entity (P) loop
4184
4185                --  Shadow entities have not been added to the list of
4186                --  entities associated to the package spec. Therefore we
4187                --  just have to re-chain all its visible entities.
4188
4189                if not Is_Class_Wide_Type (Ent) then
4190
4191                   Set_Homonym (Ent, Current_Entity (Ent));
4192                   Set_Current_Entity (Ent);
4193
4194                   if Debug_Flag_I then
4195                      Write_Str ("   (homonym) chain ");
4196                      Write_Name (Chars (Ent));
4197                      Write_Eol;
4198                   end if;
4199
4200                end if;
4201
4202                Next_Entity (Ent);
4203             end loop;
4204          end;
4205       end if;
4206    end Remove_Limited_With_Clause;
4207
4208    --------------------
4209    -- Remove_Parents --
4210    --------------------
4211
4212    procedure Remove_Parents (Lib_Unit : Node_Id) is
4213       P      : Node_Id;
4214       P_Name : Entity_Id;
4215       E      : Entity_Id;
4216       Vis    : constant Boolean :=
4217                  Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
4218
4219    begin
4220       if Is_Child_Spec (Lib_Unit) then
4221          P := Unit (Parent_Spec (Lib_Unit));
4222          P_Name := Get_Parent_Entity (P);
4223
4224          Remove_Context_Clauses (Parent_Spec (Lib_Unit));
4225          End_Package_Scope (P_Name);
4226          Set_Is_Immediately_Visible (P_Name, Vis);
4227
4228          --  Remove from visibility the siblings as well, which are directly
4229          --  visible while the parent is in scope.
4230
4231          E := First_Entity (P_Name);
4232
4233          while Present (E) loop
4234
4235             if Is_Child_Unit (E) then
4236                Set_Is_Immediately_Visible (E, False);
4237             end if;
4238
4239             Next_Entity (E);
4240          end loop;
4241
4242          Set_In_Package_Body (P_Name, False);
4243
4244          --  This is the recursive call to remove the context of any
4245          --  higher level parent. This recursion ensures that all parents
4246          --  are removed in the reverse order of their installation.
4247
4248          Remove_Parents (P);
4249       end if;
4250    end Remove_Parents;
4251
4252    -----------------------------
4253    -- Remove_With_Type_Clause --
4254    -----------------------------
4255
4256    procedure Remove_With_Type_Clause (Name : Node_Id) is
4257       Typ : Entity_Id;
4258       P   : Entity_Id;
4259
4260       procedure Unchain (E : Entity_Id);
4261       --  Remove entity from visibility list.
4262
4263       procedure Unchain (E : Entity_Id) is
4264          Prev : Entity_Id;
4265
4266       begin
4267          Prev := Current_Entity (E);
4268
4269          --  Package entity may appear is several with_type_clauses, and
4270          --  may have been removed already.
4271
4272          if No (Prev) then
4273             return;
4274
4275          elsif Prev = E then
4276             Set_Name_Entity_Id (Chars (E), Homonym (E));
4277
4278          else
4279             while Present (Prev)
4280               and then Homonym (Prev) /= E
4281             loop
4282                Prev := Homonym (Prev);
4283             end loop;
4284
4285             if Present (Prev) then
4286                Set_Homonym (Prev, Homonym (E));
4287             end if;
4288          end if;
4289       end Unchain;
4290
4291       --  Start of Remove_With_Type_Clause
4292
4293    begin
4294       if Nkind (Name) = N_Selected_Component then
4295          Typ := Entity (Selector_Name (Name));
4296
4297          if No (Typ) then    --  error in declaration.
4298             return;
4299          end if;
4300       else
4301          return;
4302       end if;
4303
4304       P := Scope (Typ);
4305
4306       --  If the exporting package has been analyzed, it has appeared in the
4307       --  context already and should be left alone. Otherwise, remove from
4308       --  visibility.
4309
4310       if not Analyzed (Unit_Declaration_Node (P)) then
4311          Unchain (P);
4312          Unchain (Typ);
4313          Set_Is_Frozen (Typ, False);
4314       end if;
4315
4316       if Ekind (Typ) = E_Record_Type then
4317          Set_From_With_Type (Class_Wide_Type (Typ), False);
4318          Set_From_With_Type (Typ, False);
4319       end if;
4320
4321       Set_From_With_Type (P, False);
4322
4323       --  If P is a child unit, remove parents as well.
4324
4325       P := Scope (P);
4326
4327       while Present (P)
4328         and then P /= Standard_Standard
4329       loop
4330          Set_From_With_Type (P, False);
4331
4332          if not Analyzed (Unit_Declaration_Node (P)) then
4333             Unchain (P);
4334          end if;
4335
4336          P := Scope (P);
4337       end loop;
4338
4339       --  The back-end needs to know that an access type is imported, so it
4340       --  does not need elaboration and can appear in a mutually recursive
4341       --  record definition, so the imported flag on an access  type is
4342       --  preserved.
4343
4344    end Remove_With_Type_Clause;
4345
4346    ---------------------------------
4347    -- Remove_Unit_From_Visibility --
4348    ---------------------------------
4349
4350    procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
4351       P : constant Entity_Id := Scope (Unit_Name);
4352
4353    begin
4354
4355       if Debug_Flag_I then
4356          Write_Str ("remove unit ");
4357          Write_Name (Chars (Unit_Name));
4358          Write_Str (" from visibility");
4359          Write_Eol;
4360       end if;
4361
4362       if P /= Standard_Standard then
4363          Set_Is_Visible_Child_Unit (Unit_Name, False);
4364       end if;
4365
4366       Set_Is_Potentially_Use_Visible (Unit_Name, False);
4367       Set_Is_Immediately_Visible     (Unit_Name, False);
4368
4369    end Remove_Unit_From_Visibility;
4370
4371    -------------
4372    -- Unchain --
4373    -------------
4374
4375    procedure Unchain (E : Entity_Id) is
4376       Prev : Entity_Id;
4377
4378    begin
4379       Prev := Current_Entity (E);
4380
4381       if No (Prev) then
4382          return;
4383
4384       elsif Prev = E then
4385          Set_Name_Entity_Id (Chars (E), Homonym (E));
4386
4387       else
4388          while Present (Prev)
4389            and then Homonym (Prev) /= E
4390          loop
4391             Prev := Homonym (Prev);
4392          end loop;
4393
4394          if Present (Prev) then
4395             Set_Homonym (Prev, Homonym (E));
4396          end if;
4397       end if;
4398
4399       if Debug_Flag_I then
4400          Write_Str ("   (homonym) unchain ");
4401          Write_Name (Chars (E));
4402          Write_Eol;
4403       end if;
4404
4405    end Unchain;
4406 end Sem_Ch10;