[multiple changes]
[platform/upstream/gcc.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
1816       --  Ada 0Y (AI-262): Remove from visibility the entity corresponding to
1817       --  private_with units; they will be made visible later (just before the
1818       --  private part is analyzed)
1819
1820       if Private_Present (N) then
1821          Set_Is_Immediately_Visible (E_Name, False);
1822       end if;
1823    end Analyze_With_Clause;
1824
1825    ------------------------------
1826    -- Analyze_With_Type_Clause --
1827    ------------------------------
1828
1829    procedure Analyze_With_Type_Clause (N : Node_Id) is
1830       Loc  : constant Source_Ptr := Sloc (N);
1831       Nam  : constant Node_Id    := Name (N);
1832       Pack : Node_Id;
1833       Decl : Node_Id;
1834       P    : Entity_Id;
1835       Unum : Unit_Number_Type;
1836       Sel  : Node_Id;
1837
1838       procedure Decorate_Tagged_Type (T : Entity_Id);
1839       --  Set basic attributes of type, including its class_wide type.
1840
1841       function In_Chain (E : Entity_Id) return Boolean;
1842       --  Check that the imported type is not already in the homonym chain,
1843       --  for example through a with_type clause in a parent unit.
1844
1845       --------------------------
1846       -- Decorate_Tagged_Type --
1847       --------------------------
1848
1849       procedure Decorate_Tagged_Type (T : Entity_Id) is
1850          CW : Entity_Id;
1851
1852       begin
1853          Set_Ekind (T, E_Record_Type);
1854          Set_Is_Tagged_Type (T);
1855          Set_Etype (T, T);
1856          Set_From_With_Type (T);
1857          Set_Scope (T, P);
1858
1859          if not In_Chain (T) then
1860             Set_Homonym (T, Current_Entity (T));
1861             Set_Current_Entity (T);
1862          end if;
1863
1864          --  Build bogus class_wide type, if not previously done.
1865
1866          if No (Class_Wide_Type (T)) then
1867             CW := Make_Defining_Identifier (Loc,  New_Internal_Name ('S'));
1868
1869             Set_Ekind            (CW, E_Class_Wide_Type);
1870             Set_Etype            (CW, T);
1871             Set_Scope            (CW, P);
1872             Set_Is_Tagged_Type   (CW);
1873             Set_Is_First_Subtype (CW, True);
1874             Init_Size_Align      (CW);
1875             Set_Has_Unknown_Discriminants
1876                                  (CW, True);
1877             Set_Class_Wide_Type  (CW, CW);
1878             Set_Equivalent_Type  (CW, Empty);
1879             Set_From_With_Type   (CW);
1880
1881             Set_Class_Wide_Type (T, CW);
1882          end if;
1883       end Decorate_Tagged_Type;
1884
1885       --------------
1886       -- In_Chain --
1887       --------------
1888
1889       function In_Chain (E : Entity_Id) return Boolean is
1890          H : Entity_Id := Current_Entity (E);
1891
1892       begin
1893          while Present (H) loop
1894
1895             if H = E then
1896                return True;
1897             else
1898                H := Homonym (H);
1899             end if;
1900          end loop;
1901
1902          return False;
1903       end In_Chain;
1904
1905    --  Start of processing for Analyze_With_Type_Clause
1906
1907    begin
1908       if Nkind (Nam) = N_Selected_Component then
1909          Pack := New_Copy_Tree (Prefix (Nam));
1910          Sel  := Selector_Name (Nam);
1911
1912       else
1913          Error_Msg_N ("illegal name for imported type", Nam);
1914          return;
1915       end if;
1916
1917       Decl :=
1918         Make_Package_Declaration (Loc,
1919           Specification =>
1920              (Make_Package_Specification (Loc,
1921                Defining_Unit_Name   => Pack,
1922                Visible_Declarations => New_List,
1923                End_Label            => Empty)));
1924
1925       Unum :=
1926         Load_Unit
1927           (Load_Name  => Get_Unit_Name (Decl),
1928            Required   => True,
1929            Subunit    => False,
1930            Error_Node => Nam);
1931
1932       if Unum = No_Unit
1933          or else Nkind (Unit (Cunit (Unum))) /= N_Package_Declaration
1934       then
1935          Error_Msg_N ("imported type must be declared in package", Nam);
1936          return;
1937
1938       elsif Unum = Current_Sem_Unit then
1939
1940          --  If type is defined in unit being analyzed, then the clause
1941          --  is redundant.
1942
1943          return;
1944
1945       else
1946          P := Cunit_Entity (Unum);
1947       end if;
1948
1949       --  Find declaration for imported type, and set its basic attributes
1950       --  if it has not been analyzed (which will be the case if there is
1951       --  circular dependence).
1952
1953       declare
1954          Decl : Node_Id;
1955          Typ  : Entity_Id;
1956
1957       begin
1958          if not Analyzed (Cunit (Unum))
1959            and then not From_With_Type (P)
1960          then
1961             Set_Ekind (P, E_Package);
1962             Set_Etype (P, Standard_Void_Type);
1963             Set_From_With_Type (P);
1964             Set_Scope (P, Standard_Standard);
1965             Set_Homonym (P, Current_Entity (P));
1966             Set_Current_Entity (P);
1967
1968          elsif Analyzed (Cunit (Unum))
1969            and then Is_Child_Unit (P)
1970          then
1971             --  If the child unit is already in scope, indicate that it is
1972             --  visible, and remains so after intervening calls to rtsfind.
1973
1974             Set_Is_Visible_Child_Unit (P);
1975          end if;
1976
1977          if Nkind (Parent (P)) = N_Defining_Program_Unit_Name then
1978
1979             --  Make parent packages visible.
1980
1981             declare
1982                Parent_Comp : Node_Id;
1983                Parent_Id   : Entity_Id;
1984                Child       : Entity_Id;
1985
1986             begin
1987                Child   := P;
1988                Parent_Comp := Parent_Spec (Unit (Cunit (Unum)));
1989
1990                loop
1991                   Parent_Id := Defining_Entity (Unit (Parent_Comp));
1992                   Set_Scope (Child, Parent_Id);
1993
1994                   --  The type may be imported from a child unit, in which
1995                   --  case the current compilation appears in the name. Do
1996                   --  not change its visibility here because it will conflict
1997                   --  with the subsequent normal processing.
1998
1999                   if not Analyzed (Unit_Declaration_Node (Parent_Id))
2000                     and then Parent_Id /= Cunit_Entity (Current_Sem_Unit)
2001                   then
2002                      Set_Ekind (Parent_Id, E_Package);
2003                      Set_Etype (Parent_Id, Standard_Void_Type);
2004
2005                      --  The same package may appear is several with_type
2006                      --  clauses.
2007
2008                      if not From_With_Type (Parent_Id) then
2009                         Set_Homonym (Parent_Id, Current_Entity (Parent_Id));
2010                         Set_Current_Entity (Parent_Id);
2011                         Set_From_With_Type (Parent_Id);
2012                      end if;
2013                   end if;
2014
2015                   Set_Is_Immediately_Visible (Parent_Id);
2016
2017                   Child := Parent_Id;
2018                   Parent_Comp := Parent_Spec (Unit (Parent_Comp));
2019                   exit when No (Parent_Comp);
2020                end loop;
2021
2022                Set_Scope (Parent_Id, Standard_Standard);
2023             end;
2024          end if;
2025
2026          --  Even if analyzed, the package may not be currently visible. It
2027          --  must be while the with_type clause is active.
2028
2029          Set_Is_Immediately_Visible (P);
2030
2031          Decl :=
2032            First (Visible_Declarations (Specification (Unit (Cunit (Unum)))));
2033
2034          while Present (Decl) loop
2035
2036             if Nkind (Decl) = N_Full_Type_Declaration
2037               and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
2038             then
2039                Typ := Defining_Identifier (Decl);
2040
2041                if Tagged_Present (N) then
2042
2043                   --  The declaration must indicate that this is a tagged
2044                   --  type or a type extension.
2045
2046                   if (Nkind (Type_Definition (Decl)) = N_Record_Definition
2047                        and then Tagged_Present (Type_Definition (Decl)))
2048                     or else
2049                       (Nkind (Type_Definition (Decl))
2050                           = N_Derived_Type_Definition
2051                          and then Present
2052                            (Record_Extension_Part (Type_Definition (Decl))))
2053                   then
2054                      null;
2055                   else
2056                      Error_Msg_N ("imported type is not a tagged type", Nam);
2057                      return;
2058                   end if;
2059
2060                   if not Analyzed (Decl) then
2061
2062                      --  Unit is not currently visible. Add basic attributes
2063                      --  to type and build its class-wide type.
2064
2065                      Init_Size_Align (Typ);
2066                      Decorate_Tagged_Type (Typ);
2067                   end if;
2068
2069                else
2070                   if Nkind (Type_Definition (Decl))
2071                      /= N_Access_To_Object_Definition
2072                   then
2073                      Error_Msg_N
2074                       ("imported type is not an access type", Nam);
2075
2076                   elsif not Analyzed (Decl) then
2077                      Set_Ekind                    (Typ, E_Access_Type);
2078                      Set_Etype                    (Typ, Typ);
2079                      Set_Scope                    (Typ, P);
2080                      Init_Size                    (Typ, System_Address_Size);
2081                      Init_Alignment               (Typ);
2082                      Set_Directly_Designated_Type (Typ, Standard_Integer);
2083                      Set_From_With_Type           (Typ);
2084
2085                      if not In_Chain (Typ) then
2086                         Set_Homonym               (Typ, Current_Entity (Typ));
2087                         Set_Current_Entity        (Typ);
2088                      end if;
2089                   end if;
2090                end if;
2091
2092                Set_Entity (Sel, Typ);
2093                return;
2094
2095             elsif ((Nkind (Decl) = N_Private_Type_Declaration
2096                       and then Tagged_Present (Decl))
2097                 or else (Nkind (Decl) = N_Private_Extension_Declaration))
2098               and then Chars (Defining_Identifier (Decl)) = Chars (Sel)
2099             then
2100                Typ := Defining_Identifier (Decl);
2101
2102                if not Tagged_Present (N) then
2103                   Error_Msg_N ("type must be declared tagged", N);
2104
2105                elsif not Analyzed (Decl) then
2106                   Decorate_Tagged_Type (Typ);
2107                end if;
2108
2109                Set_Entity (Sel, Typ);
2110                Set_From_With_Type (Typ);
2111                return;
2112             end if;
2113
2114             Decl := Next (Decl);
2115          end loop;
2116
2117          Error_Msg_NE ("not a visible access or tagged type in&", Nam, P);
2118       end;
2119    end Analyze_With_Type_Clause;
2120
2121    -----------------------------
2122    -- Check_With_Type_Clauses --
2123    -----------------------------
2124
2125    procedure Check_With_Type_Clauses (N : Node_Id) is
2126       Lib_Unit : constant Node_Id := Unit (N);
2127
2128       procedure Check_Parent_Context (U : Node_Id);
2129       --  Examine context items of parent unit to locate with_type clauses.
2130
2131       --------------------------
2132       -- Check_Parent_Context --
2133       --------------------------
2134
2135       procedure Check_Parent_Context (U : Node_Id) is
2136          Item : Node_Id;
2137
2138       begin
2139          Item := First (Context_Items (U));
2140          while Present (Item) loop
2141             if Nkind (Item) = N_With_Type_Clause
2142               and then not Error_Posted (Item)
2143               and then
2144                 From_With_Type (Scope (Entity (Selector_Name (Name (Item)))))
2145             then
2146                Error_Msg_Sloc := Sloc (Item);
2147                Error_Msg_N ("Missing With_Clause for With_Type_Clause#", N);
2148             end if;
2149
2150             Next (Item);
2151          end loop;
2152       end Check_Parent_Context;
2153
2154    --  Start of processing for Check_With_Type_Clauses
2155
2156    begin
2157       if Extensions_Allowed
2158         and then (Nkind (Lib_Unit) = N_Package_Body
2159                    or else Nkind (Lib_Unit) = N_Subprogram_Body)
2160       then
2161          Check_Parent_Context (Library_Unit (N));
2162          if Is_Child_Spec (Unit (Library_Unit (N))) then
2163             Check_Parent_Context (Parent_Spec (Unit (Library_Unit (N))));
2164          end if;
2165       end if;
2166    end Check_With_Type_Clauses;
2167
2168    ------------------------------
2169    -- Check_Private_Child_Unit --
2170    ------------------------------
2171
2172    procedure Check_Private_Child_Unit (N : Node_Id) is
2173       Lib_Unit   : constant Node_Id := Unit (N);
2174       Item       : Node_Id;
2175       Curr_Unit  : Entity_Id;
2176       Sub_Parent : Node_Id;
2177       Priv_Child : Entity_Id;
2178       Par_Lib    : Entity_Id;
2179       Par_Spec   : Node_Id;
2180
2181       function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean;
2182       --  Returns true if and only if the library unit is declared with
2183       --  an explicit designation of private.
2184
2185       function Is_Private_Library_Unit (Unit : Entity_Id) return Boolean is
2186          Comp_Unit : constant Node_Id := Parent (Unit_Declaration_Node (Unit));
2187
2188       begin
2189          return Private_Present (Comp_Unit);
2190       end Is_Private_Library_Unit;
2191
2192    --  Start of processing for Check_Private_Child_Unit
2193
2194    begin
2195       if Nkind (Lib_Unit) = N_Package_Body
2196         or else Nkind (Lib_Unit) = N_Subprogram_Body
2197       then
2198          Curr_Unit := Defining_Entity (Unit (Library_Unit (N)));
2199          Par_Lib   := Curr_Unit;
2200
2201       elsif Nkind (Lib_Unit) = N_Subunit then
2202
2203          --  The parent is itself a body. The parent entity is to be found
2204          --  in the corresponding spec.
2205
2206          Sub_Parent := Library_Unit (N);
2207          Curr_Unit  := Defining_Entity (Unit (Library_Unit (Sub_Parent)));
2208
2209          --  If the parent itself is a subunit, Curr_Unit is the entity
2210          --  of the enclosing body, retrieve the spec entity which is
2211          --  the proper ancestor we need for the following tests.
2212
2213          if Ekind (Curr_Unit) = E_Package_Body then
2214             Curr_Unit := Spec_Entity (Curr_Unit);
2215          end if;
2216
2217          Par_Lib    := Curr_Unit;
2218
2219       else
2220          Curr_Unit := Defining_Entity (Lib_Unit);
2221
2222          Par_Lib := Curr_Unit;
2223          Par_Spec  := Parent_Spec (Lib_Unit);
2224
2225          if No (Par_Spec) then
2226             Par_Lib := Empty;
2227          else
2228             Par_Lib := Defining_Entity (Unit (Par_Spec));
2229          end if;
2230       end if;
2231
2232       --  Loop through context items
2233
2234       Item := First (Context_Items (N));
2235       while Present (Item) loop
2236
2237          --  Ada 0Y (AI-262): Allow private_with of a private child package in
2238          --  public siblings
2239
2240          if Nkind (Item) = N_With_Clause
2241             and then not Implicit_With (Item)
2242             and then not Private_Present (Item)
2243             and then Is_Private_Descendant (Entity (Name (Item)))
2244          then
2245             Priv_Child := Entity (Name (Item));
2246
2247             declare
2248                Curr_Parent  : Entity_Id := Par_Lib;
2249                Child_Parent : Entity_Id := Scope (Priv_Child);
2250                Prv_Ancestor : Entity_Id := Child_Parent;
2251                Curr_Private : Boolean   := Is_Private_Library_Unit (Curr_Unit);
2252
2253             begin
2254                --  If the child unit is a public child then locate
2255                --  the nearest private ancestor; Child_Parent will
2256                --  then be set to the parent of that ancestor.
2257
2258                if not Is_Private_Library_Unit (Priv_Child) then
2259                   while Present (Prv_Ancestor)
2260                     and then not Is_Private_Library_Unit (Prv_Ancestor)
2261                   loop
2262                      Prv_Ancestor := Scope (Prv_Ancestor);
2263                   end loop;
2264
2265                   if Present (Prv_Ancestor) then
2266                      Child_Parent := Scope (Prv_Ancestor);
2267                   end if;
2268                end if;
2269
2270                while Present (Curr_Parent)
2271                  and then Curr_Parent /= Standard_Standard
2272                  and then Curr_Parent /= Child_Parent
2273                loop
2274                   Curr_Private :=
2275                     Curr_Private or else Is_Private_Library_Unit (Curr_Parent);
2276                   Curr_Parent := Scope (Curr_Parent);
2277                end loop;
2278
2279                if not Present (Curr_Parent) then
2280                   Curr_Parent := Standard_Standard;
2281                end if;
2282
2283                if Curr_Parent /= Child_Parent then
2284
2285                   if Ekind (Priv_Child) = E_Generic_Package
2286                     and then Chars (Priv_Child) in Text_IO_Package_Name
2287                     and then Chars (Scope (Scope (Priv_Child))) = Name_Ada
2288                   then
2289                      Error_Msg_NE
2290                        ("& is a nested package, not a compilation unit",
2291                        Name (Item), Priv_Child);
2292
2293                   else
2294                      Error_Msg_N
2295                        ("unit in with clause is private child unit!", Item);
2296                      Error_Msg_NE
2297                        ("current unit must also have parent&!",
2298                         Item, Child_Parent);
2299                   end if;
2300
2301                elsif not Curr_Private
2302                  and then Nkind (Lib_Unit) /= N_Package_Body
2303                  and then Nkind (Lib_Unit) /= N_Subprogram_Body
2304                  and then Nkind (Lib_Unit) /= N_Subunit
2305                then
2306                   Error_Msg_NE
2307                     ("current unit must also be private descendant of&",
2308                      Item, Child_Parent);
2309                end if;
2310             end;
2311          end if;
2312
2313          Next (Item);
2314       end loop;
2315
2316    end Check_Private_Child_Unit;
2317
2318    ----------------------
2319    -- Check_Stub_Level --
2320    ----------------------
2321
2322    procedure Check_Stub_Level (N : Node_Id) is
2323       Par  : constant Node_Id   := Parent (N);
2324       Kind : constant Node_Kind := Nkind (Par);
2325
2326    begin
2327       if (Kind = N_Package_Body
2328            or else Kind = N_Subprogram_Body
2329            or else Kind = N_Task_Body
2330            or else Kind = N_Protected_Body)
2331
2332         and then (Nkind (Parent (Par)) = N_Compilation_Unit
2333                    or else Nkind (Parent (Par)) = N_Subunit)
2334       then
2335          null;
2336
2337       --  In an instance, a missing stub appears at any level. A warning
2338       --  message will have been emitted already for the missing file.
2339
2340       elsif not In_Instance then
2341          Error_Msg_N ("stub cannot appear in an inner scope", N);
2342
2343       elsif Expander_Active then
2344          Error_Msg_N ("missing proper body", N);
2345       end if;
2346    end Check_Stub_Level;
2347
2348    ------------------------
2349    -- Expand_With_Clause --
2350    ------------------------
2351
2352    procedure Expand_With_Clause (Nam : Node_Id; N : Node_Id) is
2353       Loc   : constant Source_Ptr := Sloc (Nam);
2354       Ent   : constant Entity_Id := Entity (Nam);
2355       Withn : Node_Id;
2356       P     : Node_Id;
2357
2358       function Build_Unit_Name (Nam : Node_Id) return Node_Id;
2359
2360       function Build_Unit_Name (Nam : Node_Id) return Node_Id is
2361          Result : Node_Id;
2362
2363       begin
2364          if Nkind (Nam) = N_Identifier then
2365             return New_Occurrence_Of (Entity (Nam), Loc);
2366
2367          else
2368             Result :=
2369               Make_Expanded_Name (Loc,
2370                 Chars  => Chars (Entity (Nam)),
2371                 Prefix => Build_Unit_Name (Prefix (Nam)),
2372                 Selector_Name => New_Occurrence_Of (Entity (Nam), Loc));
2373             Set_Entity (Result, Entity (Nam));
2374             return Result;
2375          end if;
2376       end Build_Unit_Name;
2377
2378    begin
2379       New_Nodes_OK := New_Nodes_OK + 1;
2380       Withn :=
2381         Make_With_Clause (Loc, Name => Build_Unit_Name (Nam));
2382
2383       P := Parent (Unit_Declaration_Node (Ent));
2384       Set_Library_Unit          (Withn, P);
2385       Set_Corresponding_Spec    (Withn, Ent);
2386       Set_First_Name            (Withn, True);
2387       Set_Implicit_With         (Withn, True);
2388
2389       Prepend (Withn, Context_Items (N));
2390       Mark_Rewrite_Insertion (Withn);
2391       Install_Withed_Unit (Withn);
2392
2393       if Nkind (Nam) = N_Expanded_Name then
2394          Expand_With_Clause (Prefix (Nam), N);
2395       end if;
2396
2397       New_Nodes_OK := New_Nodes_OK - 1;
2398    end Expand_With_Clause;
2399
2400    --------------------------------
2401    -- Expand_Limited_With_Clause --
2402    --------------------------------
2403
2404    procedure Expand_Limited_With_Clause (Nam : Node_Id; N : Node_Id) is
2405       Loc   : constant Source_Ptr := Sloc (Nam);
2406       Unum  : Unit_Number_Type;
2407       Withn : Node_Id;
2408
2409    begin
2410       New_Nodes_OK := New_Nodes_OK + 1;
2411
2412       if Nkind (Nam) = N_Identifier then
2413          Withn :=
2414            Make_With_Clause (Loc, Name => Nam);
2415          Set_Limited_Present (Withn);
2416          Set_First_Name      (Withn);
2417          Set_Implicit_With   (Withn);
2418
2419          --  Load the corresponding parent unit
2420
2421          Unum :=
2422            Load_Unit
2423            (Load_Name  => Get_Spec_Name (Get_Unit_Name (Nam)),
2424             Required   => True,
2425             Subunit    => False,
2426             Error_Node => Nam);
2427
2428          if not Analyzed (Cunit (Unum)) then
2429             Set_Library_Unit (Withn, Cunit (Unum));
2430             Set_Corresponding_Spec
2431               (Withn, Specification (Unit (Cunit (Unum))));
2432
2433             Prepend (Withn, Context_Items (Parent (N)));
2434             Mark_Rewrite_Insertion (Withn);
2435          end if;
2436
2437       else pragma Assert (Nkind (Nam) = N_Selected_Component);
2438          Withn :=
2439            Make_With_Clause
2440            (Loc,
2441             Name =>
2442               Make_Selected_Component
2443                 (Loc,
2444                  Prefix        => Prefix (Nam),
2445                  Selector_Name => Selector_Name (Nam)));
2446
2447          Set_Parent (Withn, Parent (N));
2448          Set_Limited_Present (Withn);
2449          Set_First_Name      (Withn);
2450          Set_Implicit_With   (Withn);
2451
2452          Unum :=
2453            Load_Unit
2454              (Load_Name  => Get_Spec_Name (Get_Unit_Name (Nam)),
2455               Required   => True,
2456               Subunit    => False,
2457               Error_Node => Nam);
2458
2459          if not Analyzed (Cunit (Unum)) then
2460             Set_Library_Unit (Withn, Cunit (Unum));
2461             Set_Corresponding_Spec
2462               (Withn, Specification (Unit (Cunit (Unum))));
2463             Prepend (Withn, Context_Items (Parent (N)));
2464             Mark_Rewrite_Insertion (Withn);
2465
2466             Expand_Limited_With_Clause (Prefix (Nam), N);
2467          end if;
2468       end if;
2469
2470       New_Nodes_OK := New_Nodes_OK - 1;
2471    end Expand_Limited_With_Clause;
2472
2473    -----------------------
2474    -- Get_Parent_Entity --
2475    -----------------------
2476
2477    function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
2478    begin
2479       if Nkind (Unit) = N_Package_Instantiation then
2480          return Defining_Entity (Specification (Instance_Spec (Unit)));
2481       else
2482          return Defining_Entity (Unit);
2483       end if;
2484    end Get_Parent_Entity;
2485
2486    -----------------------------
2487    -- Implicit_With_On_Parent --
2488    -----------------------------
2489
2490    procedure Implicit_With_On_Parent
2491      (Child_Unit : Node_Id;
2492       N          : Node_Id)
2493    is
2494       Loc    : constant Source_Ptr := Sloc (N);
2495       P      : constant Node_Id    := Parent_Spec (Child_Unit);
2496       P_Unit : constant Node_Id    := Unit (P);
2497       P_Name : constant Entity_Id  := Get_Parent_Entity (P_Unit);
2498       Withn  : Node_Id;
2499
2500       function Build_Ancestor_Name (P : Node_Id) return Node_Id;
2501       --  Build prefix of child unit name. Recurse if needed.
2502
2503       function Build_Unit_Name return Node_Id;
2504       --  If the unit is a child unit, build qualified name with all
2505       --  ancestors.
2506
2507       -------------------------
2508       -- Build_Ancestor_Name --
2509       -------------------------
2510
2511       function Build_Ancestor_Name (P : Node_Id) return Node_Id is
2512          P_Ref : constant Node_Id :=
2513                    New_Reference_To (Defining_Entity (P), Loc);
2514       begin
2515          if No (Parent_Spec (P)) then
2516             return P_Ref;
2517          else
2518             return
2519               Make_Selected_Component (Loc,
2520                 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P))),
2521                 Selector_Name => P_Ref);
2522          end if;
2523       end Build_Ancestor_Name;
2524
2525       ---------------------
2526       -- Build_Unit_Name --
2527       ---------------------
2528
2529       function Build_Unit_Name return Node_Id is
2530          Result : Node_Id;
2531       begin
2532          if No (Parent_Spec (P_Unit)) then
2533             return New_Reference_To (P_Name, Loc);
2534          else
2535             Result :=
2536               Make_Expanded_Name (Loc,
2537                 Chars  => Chars (P_Name),
2538                 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
2539                 Selector_Name => New_Reference_To (P_Name, Loc));
2540             Set_Entity (Result, P_Name);
2541             return Result;
2542          end if;
2543       end Build_Unit_Name;
2544
2545    --  Start of processing for Implicit_With_On_Parent
2546
2547    begin
2548       New_Nodes_OK := New_Nodes_OK + 1;
2549       Withn := Make_With_Clause (Loc, Name => Build_Unit_Name);
2550
2551       Set_Library_Unit          (Withn, P);
2552       Set_Corresponding_Spec    (Withn, P_Name);
2553       Set_First_Name            (Withn, True);
2554       Set_Implicit_With         (Withn, True);
2555
2556       --  Node is placed at the beginning of the context items, so that
2557       --  subsequent use clauses on the parent can be validated.
2558
2559       Prepend (Withn, Context_Items (N));
2560       Mark_Rewrite_Insertion (Withn);
2561       Install_Withed_Unit (Withn);
2562
2563       if Is_Child_Spec (P_Unit) then
2564          Implicit_With_On_Parent (P_Unit, N);
2565       end if;
2566
2567       New_Nodes_OK := New_Nodes_OK - 1;
2568    end Implicit_With_On_Parent;
2569
2570    ---------------------
2571    -- Install_Context --
2572    ---------------------
2573
2574    procedure Install_Context (N : Node_Id) is
2575       Lib_Unit : constant Node_Id := Unit (N);
2576
2577    begin
2578       Install_Context_Clauses (N);
2579
2580       if Is_Child_Spec (Lib_Unit) then
2581          Install_Parents (Lib_Unit, Private_Present (Parent (Lib_Unit)));
2582       end if;
2583
2584       Install_Limited_Context_Clauses (N);
2585
2586       Check_With_Type_Clauses (N);
2587    end Install_Context;
2588
2589    -----------------------------
2590    -- Install_Context_Clauses --
2591    -----------------------------
2592
2593    procedure Install_Context_Clauses (N : Node_Id) is
2594       Lib_Unit      : constant Node_Id := Unit (N);
2595       Item          : Node_Id;
2596       Uname_Node    : Entity_Id;
2597       Check_Private : Boolean := False;
2598       Decl_Node     : Node_Id;
2599       Lib_Parent    : Entity_Id;
2600
2601    begin
2602       --  Loop through context clauses to find the with/use clauses.
2603       --  This is done twice, first for everything except limited_with
2604       --  clauses, and then for those, if any are present.
2605
2606       Item := First (Context_Items (N));
2607       while Present (Item) loop
2608
2609          --  Case of explicit WITH clause
2610
2611          if Nkind (Item) = N_With_Clause
2612            and then not Implicit_With (Item)
2613          then
2614             if Limited_Present (Item) then
2615
2616                --  Limited withed units will be installed later.
2617
2618                goto Continue;
2619
2620             --  If Name (Item) is not an entity name, something is wrong, and
2621             --  this will be detected in due course, for now ignore the item
2622
2623             elsif not Is_Entity_Name (Name (Item)) then
2624                goto Continue;
2625
2626             elsif No (Entity (Name (Item))) then
2627                Set_Entity (Name (Item), Any_Id);
2628                goto Continue;
2629             end if;
2630
2631             Uname_Node := Entity (Name (Item));
2632
2633             if Is_Private_Descendant (Uname_Node) then
2634                Check_Private := True;
2635             end if;
2636
2637             Install_Withed_Unit (Item);
2638
2639             Decl_Node := Unit_Declaration_Node (Uname_Node);
2640
2641             --  If the unit is a subprogram instance, it appears nested
2642             --  within a package that carries the parent information.
2643
2644             if Is_Generic_Instance (Uname_Node)
2645               and then Ekind (Uname_Node) /= E_Package
2646             then
2647                Decl_Node := Parent (Parent (Decl_Node));
2648             end if;
2649
2650             if Is_Child_Spec (Decl_Node) then
2651                if Nkind (Name (Item)) = N_Expanded_Name then
2652                   Expand_With_Clause (Prefix (Name (Item)), N);
2653                else
2654                   --  if not an expanded name, the child unit must be a
2655                   --  renaming, nothing to do.
2656
2657                   null;
2658                end if;
2659
2660             elsif Nkind (Decl_Node) = N_Subprogram_Body
2661               and then not Acts_As_Spec (Parent (Decl_Node))
2662               and then Is_Child_Spec (Unit (Library_Unit (Parent (Decl_Node))))
2663             then
2664                Implicit_With_On_Parent
2665                  (Unit (Library_Unit (Parent (Decl_Node))), N);
2666             end if;
2667
2668             --  Check license conditions unless this is a dummy unit
2669
2670             if Sloc (Library_Unit (Item)) /= No_Location then
2671                License_Check : declare
2672                   Withl : constant License_Type :=
2673                             License (Source_Index
2674                                        (Get_Source_Unit
2675                                          (Library_Unit (Item))));
2676
2677                   Unitl : constant License_Type :=
2678                            License (Source_Index (Current_Sem_Unit));
2679
2680                   procedure License_Error;
2681                   --  Signal error of bad license
2682
2683                   -------------------
2684                   -- License_Error --
2685                   -------------------
2686
2687                   procedure License_Error is
2688                   begin
2689                      Error_Msg_N
2690                        ("?license of with'ed unit & is incompatible",
2691                         Name (Item));
2692                   end License_Error;
2693
2694                --  Start of processing for License_Check
2695
2696                begin
2697                   case Unitl is
2698                      when Unknown =>
2699                         null;
2700
2701                      when Restricted =>
2702                         if Withl = GPL then
2703                            License_Error;
2704                         end if;
2705
2706                      when GPL =>
2707                         if Withl = Restricted then
2708                            License_Error;
2709                         end if;
2710
2711                      when Modified_GPL =>
2712                         if Withl = Restricted or else Withl = GPL then
2713                            License_Error;
2714                         end if;
2715
2716                      when Unrestricted =>
2717                         null;
2718                   end case;
2719                end License_Check;
2720             end if;
2721
2722          --  Case of USE PACKAGE clause
2723
2724          elsif Nkind (Item) = N_Use_Package_Clause then
2725             Analyze_Use_Package (Item);
2726
2727          --  Case of USE TYPE clause
2728
2729          elsif Nkind (Item) = N_Use_Type_Clause then
2730             Analyze_Use_Type (Item);
2731
2732          --  Case of WITH TYPE clause
2733
2734          --  A With_Type_Clause is processed when installing the context,
2735          --  because it is a visibility mechanism and does not create a
2736          --  semantic dependence on other units, as a With_Clause does.
2737
2738          elsif Nkind (Item) = N_With_Type_Clause then
2739             Analyze_With_Type_Clause (Item);
2740
2741          --  case of PRAGMA
2742
2743          elsif Nkind (Item) = N_Pragma then
2744             Analyze (Item);
2745          end if;
2746
2747       <<Continue>>
2748          Next (Item);
2749       end loop;
2750
2751       if Is_Child_Spec (Lib_Unit) then
2752
2753          --  The unit also has implicit withs on its own parents
2754
2755          if No (Context_Items (N)) then
2756             Set_Context_Items (N, New_List);
2757          end if;
2758
2759          Implicit_With_On_Parent (Lib_Unit, N);
2760       end if;
2761
2762       --  If the unit is a body, the context of the specification must also
2763       --  be installed.
2764
2765       if Nkind (Lib_Unit) = N_Package_Body
2766         or else (Nkind (Lib_Unit) = N_Subprogram_Body
2767                   and then not Acts_As_Spec (N))
2768       then
2769          Install_Context (Library_Unit (N));
2770
2771          if Is_Child_Spec (Unit (Library_Unit (N))) then
2772
2773             --  If the unit is the body of a public child unit, the private
2774             --  declarations of the parent must be made visible. If the child
2775             --  unit is private, the private declarations have been installed
2776             --  already in the call to Install_Parents for the spec. Installing
2777             --  private declarations must be done for all ancestors of public
2778             --  child units. In addition, sibling units mentioned in the
2779             --  context clause of the body are directly visible.
2780
2781             declare
2782                Lib_Spec : Node_Id := Unit (Library_Unit (N));
2783                P        : Node_Id;
2784                P_Name   : Entity_Id;
2785
2786             begin
2787                while Is_Child_Spec (Lib_Spec) loop
2788                   P := Unit (Parent_Spec (Lib_Spec));
2789
2790                   if not (Private_Present (Parent (Lib_Spec))) then
2791                      P_Name := Defining_Entity (P);
2792                      Install_Private_Declarations (P_Name);
2793                      Install_Private_With_Clauses (P_Name);
2794                      Set_Use (Private_Declarations (Specification (P)));
2795                   end if;
2796
2797                   Lib_Spec := P;
2798                end loop;
2799             end;
2800          end if;
2801
2802          --  For a package body, children in context are immediately visible
2803
2804          Install_Siblings (Defining_Entity (Unit (Library_Unit (N))), N);
2805       end if;
2806
2807       if Nkind (Lib_Unit) = N_Generic_Package_Declaration
2808         or else Nkind (Lib_Unit) = N_Generic_Subprogram_Declaration
2809         or else Nkind (Lib_Unit) = N_Package_Declaration
2810         or else Nkind (Lib_Unit) = N_Subprogram_Declaration
2811       then
2812          if Is_Child_Spec (Lib_Unit) then
2813             Lib_Parent := Defining_Entity (Unit (Parent_Spec (Lib_Unit)));
2814             Set_Is_Private_Descendant
2815               (Defining_Entity (Lib_Unit),
2816                Is_Private_Descendant (Lib_Parent)
2817                  or else Private_Present (Parent (Lib_Unit)));
2818
2819          else
2820             Set_Is_Private_Descendant
2821               (Defining_Entity (Lib_Unit),
2822                Private_Present (Parent (Lib_Unit)));
2823          end if;
2824       end if;
2825
2826       if Check_Private then
2827          Check_Private_Child_Unit (N);
2828       end if;
2829    end Install_Context_Clauses;
2830
2831    -------------------------------------
2832    -- Install_Limited_Context_Clauses --
2833    -------------------------------------
2834
2835    procedure Install_Limited_Context_Clauses (N : Node_Id) is
2836       Item : Node_Id;
2837
2838       procedure Check_Parent (P : Node_Id; W : Node_Id);
2839       --  Check that the unlimited view of a given compilation_unit is not
2840       --  already visible in the parents (neither immediately through the
2841       --  context clauses, nor indirectly through "use + renamings").
2842
2843       procedure Check_Private_Limited_Withed_Unit (N : Node_Id);
2844       --  Check that if a limited_with clause of a given compilation_unit
2845       --  mentions a private child of some library unit, then the given
2846       --  compilation_unit shall be the declaration of a private descendant
2847       --  of that library unit.
2848
2849       procedure Check_Withed_Unit (W : Node_Id);
2850       --  Check that a limited with_clause does not appear in the same
2851       --  context_clause as a nonlimited with_clause that mentions
2852       --  the same library.
2853
2854       --------------------
2855       --  Check_Parent  --
2856       --------------------
2857
2858       procedure Check_Parent (P : Node_Id; W : Node_Id) is
2859          Item   : Node_Id;
2860          Spec   : Node_Id;
2861          WEnt   : Entity_Id;
2862          Nam    : Node_Id;
2863          E      : Entity_Id;
2864          E2     : Entity_Id;
2865
2866       begin
2867          pragma Assert (Nkind (W) = N_With_Clause);
2868
2869          --  Step 1: Check if the unlimited view is installed in the parent
2870
2871          Item := First (Context_Items (P));
2872          while Present (Item) loop
2873             if Nkind (Item) = N_With_Clause
2874               and then not Limited_Present (Item)
2875               and then not Implicit_With (Item)
2876               and then Library_Unit (Item) = Library_Unit (W)
2877             then
2878                Error_Msg_N ("unlimited view visible in ancestor", W);
2879                return;
2880             end if;
2881
2882             Next (Item);
2883          end loop;
2884
2885          --  Step 2: Check "use + renamings"
2886
2887          WEnt := Defining_Unit_Name (Specification (Unit (Library_Unit (W))));
2888          Spec := Specification (Unit (P));
2889
2890          --  We tried to traverse the list of entities corresponding to the
2891          --  defining entity of the package spec. However, first_entity was
2892          --  found to be 'empty'. Don't know why???
2893
2894          --          Def  := Defining_Unit_Name (Spec);
2895          --          Ent  := First_Entity (Def);
2896
2897          --  As a workaround we traverse the list of visible declarations ???
2898
2899          Item := First (Visible_Declarations (Spec));
2900          while Present (Item) loop
2901
2902             if Nkind (Item) = N_Use_Package_Clause then
2903
2904                --  Traverse the list of packages
2905
2906                Nam := First (Names (Item));
2907
2908                while Present (Nam) loop
2909                   E := Entity (Nam);
2910
2911                   pragma Assert (Present (Parent (E)));
2912
2913                   if Nkind (Parent (E))
2914                     = N_Package_Renaming_Declaration
2915                     and then Renamed_Entity (E) = WEnt
2916                   then
2917                      Error_Msg_N ("unlimited view visible through "
2918                                   & "use_clause + renamings", W);
2919                      return;
2920
2921                   elsif Nkind (Parent (E)) = N_Package_Specification then
2922
2923                      --  The use clause may refer to a local package.
2924                      --  Check all the enclosing scopes.
2925
2926                      E2 := E;
2927                      while E2 /= Standard_Standard
2928                        and then E2 /= WEnt loop
2929                         E2 := Scope (E2);
2930                      end loop;
2931
2932                      if E2 = WEnt then
2933                         Error_Msg_N ("unlimited view visible through "
2934                                      & "use_clause ", W);
2935                         return;
2936                      end if;
2937
2938                   end if;
2939                   Next (Nam);
2940                end loop;
2941
2942             end if;
2943
2944             Next (Item);
2945          end loop;
2946
2947          --  Recursive call to check all the ancestors
2948
2949          if Is_Child_Spec (Unit (P)) then
2950             Check_Parent (P => Parent_Spec (Unit (P)), W => W);
2951          end if;
2952       end Check_Parent;
2953
2954       ---------------------------------------
2955       -- Check_Private_Limited_Withed_Unit --
2956       ---------------------------------------
2957
2958       procedure Check_Private_Limited_Withed_Unit (N : Node_Id) is
2959          C     : Node_Id;
2960          P     : Node_Id;
2961          Found : Boolean := False;
2962
2963       begin
2964          --  If the current compilation unit is not private we don't
2965          --  need to check anything else.
2966
2967          if not Private_Present (Parent (N)) then
2968             Found := False;
2969
2970          else
2971             --  Compilation unit of the parent of the withed library unit
2972
2973             P := Parent_Spec (Unit (Library_Unit (N)));
2974
2975             --  Traverse all the ancestors of the current compilation
2976             --  unit to check if it is a descendant of named library unit.
2977
2978             C := Parent (N);
2979             while Present (Parent_Spec (Unit (C))) loop
2980                C := Parent_Spec (Unit (C));
2981
2982                if C = P then
2983                   Found := True;
2984                   exit;
2985                end if;
2986             end loop;
2987          end if;
2988
2989          if not Found then
2990             Error_Msg_N ("current unit is not a private descendant"
2991                          & " of the withed unit ('R'M 10.1.2(8)", N);
2992          end if;
2993       end Check_Private_Limited_Withed_Unit;
2994
2995       -----------------------
2996       -- Check_Withed_Unit --
2997       -----------------------
2998
2999       procedure Check_Withed_Unit (W : Node_Id) is
3000          Item : Node_Id;
3001
3002       begin
3003          --  A limited with_clause can not appear in the same context_clause
3004          --  as a nonlimited with_clause which mentions the same library.
3005
3006          Item := First (Context_Items (N));
3007          while Present (Item) loop
3008             if Nkind (Item) = N_With_Clause
3009               and then not Limited_Present (Item)
3010               and then not Implicit_With (Item)
3011               and then Library_Unit (Item) = Library_Unit (W)
3012             then
3013                Error_Msg_N ("limited and unlimited view "
3014                             & "not allowed in the same context clauses", W);
3015                return;
3016             end if;
3017
3018             Next (Item);
3019          end loop;
3020       end Check_Withed_Unit;
3021
3022    --  Start of processing for Install_Limited_Context_Clauses
3023
3024    begin
3025       Item := First (Context_Items (N));
3026       while Present (Item) loop
3027          if Nkind (Item) = N_With_Clause
3028            and then Limited_Present (Item)
3029          then
3030
3031             Check_Withed_Unit (Item);
3032
3033             if Private_Present (Library_Unit (Item)) then
3034                Check_Private_Limited_Withed_Unit (Item);
3035             end if;
3036
3037             if Is_Child_Spec (Unit (N)) then
3038                Check_Parent (Parent_Spec (Unit (N)), Item);
3039             end if;
3040
3041             Install_Limited_Withed_Unit (Item);
3042          end if;
3043
3044          Next (Item);
3045       end loop;
3046    end Install_Limited_Context_Clauses;
3047
3048    ---------------------
3049    -- Install_Parents --
3050    ---------------------
3051
3052    procedure Install_Parents (Lib_Unit : Node_Id; Is_Private : Boolean) is
3053       P      : Node_Id;
3054       E_Name : Entity_Id;
3055       P_Name : Entity_Id;
3056       P_Spec : Node_Id;
3057
3058    begin
3059       P := Unit (Parent_Spec (Lib_Unit));
3060       P_Name := Get_Parent_Entity (P);
3061
3062       if Etype (P_Name) = Any_Type then
3063          return;
3064       end if;
3065
3066       if Ekind (P_Name) = E_Generic_Package
3067         and then Nkind (Lib_Unit) /= N_Generic_Subprogram_Declaration
3068         and then Nkind (Lib_Unit) /= N_Generic_Package_Declaration
3069         and then Nkind (Lib_Unit) not in N_Generic_Renaming_Declaration
3070       then
3071          Error_Msg_N
3072            ("child of a generic package must be a generic unit", Lib_Unit);
3073
3074       elsif not Is_Package (P_Name) then
3075          Error_Msg_N
3076            ("parent unit must be package or generic package", Lib_Unit);
3077          raise Unrecoverable_Error;
3078
3079       elsif Present (Renamed_Object (P_Name)) then
3080          Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
3081          raise Unrecoverable_Error;
3082
3083       --  Verify that a child of an instance is itself an instance, or
3084       --  the renaming of one. Given that an instance that is a unit is
3085       --  replaced with a package declaration, check against the original
3086       --  node.
3087
3088       elsif Nkind (Original_Node (P)) = N_Package_Instantiation
3089         and then Nkind (Lib_Unit)
3090                    not in N_Renaming_Declaration
3091         and then Nkind (Original_Node (Lib_Unit))
3092                    not in N_Generic_Instantiation
3093       then
3094          Error_Msg_N
3095            ("child of an instance must be an instance or renaming", Lib_Unit);
3096       end if;
3097
3098       --  This is the recursive call that ensures all parents are loaded
3099
3100       if Is_Child_Spec (P) then
3101          Install_Parents (P,
3102            Is_Private or else Private_Present (Parent (Lib_Unit)));
3103       end if;
3104
3105       --  Now we can install the context for this parent
3106
3107       Install_Context_Clauses (Parent_Spec (Lib_Unit));
3108       Install_Siblings (P_Name, Parent (Lib_Unit));
3109
3110       --  The child unit is in the declarative region of the parent. The
3111       --  parent must therefore appear in the scope stack and be visible,
3112       --  as when compiling the corresponding body. If the child unit is
3113       --  private or it is a package body, private declarations must be
3114       --  accessible as well. Use declarations in the parent must also
3115       --  be installed. Finally, other child units of the same parent that
3116       --  are in the context are immediately visible.
3117
3118       --  Find entity for compilation unit, and set its private descendant
3119       --  status as needed.
3120
3121       E_Name := Defining_Entity (Lib_Unit);
3122
3123       Set_Is_Child_Unit (E_Name);
3124
3125       Set_Is_Private_Descendant (E_Name,
3126          Is_Private_Descendant (P_Name)
3127            or else Private_Present (Parent (Lib_Unit)));
3128
3129       P_Spec := Specification (Unit_Declaration_Node (P_Name));
3130       New_Scope (P_Name);
3131
3132       --  Save current visibility of unit
3133
3134       Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility :=
3135         Is_Immediately_Visible (P_Name);
3136       Set_Is_Immediately_Visible (P_Name);
3137       Install_Visible_Declarations (P_Name);
3138       Set_Use (Visible_Declarations (P_Spec));
3139
3140       --  If the parent is a generic unit, its formal part may contain
3141       --  formal packages and use clauses for them.
3142
3143       if Ekind (P_Name) = E_Generic_Package then
3144          Set_Use (Generic_Formal_Declarations (Parent (P_Spec)));
3145       end if;
3146
3147       if Is_Private
3148         or else Private_Present (Parent (Lib_Unit))
3149       then
3150          Install_Private_Declarations (P_Name);
3151          Install_Private_With_Clauses (P_Name);
3152          Set_Use (Private_Declarations (P_Spec));
3153       end if;
3154    end Install_Parents;
3155
3156    ----------------------------------
3157    -- Install_Private_With_Clauses --
3158    ----------------------------------
3159
3160    procedure Install_Private_With_Clauses (P : Entity_Id) is
3161       Decl   : constant Node_Id := Unit_Declaration_Node (P);
3162       Clause : Node_Id;
3163
3164    begin
3165       if Debug_Flag_I then
3166          Write_Str ("install private with clauses of ");
3167          Write_Name (Chars (P));
3168          Write_Eol;
3169       end if;
3170
3171       if Nkind (Parent (Decl)) = N_Compilation_Unit then
3172          Clause := First (Context_Items (Parent (Decl)));
3173          while Present (Clause) loop
3174             if Nkind (Clause) = N_With_Clause
3175               and then Private_Present (Clause)
3176             then
3177                Install_Withed_Unit (Clause, Private_With_OK => True);
3178             end if;
3179
3180             Next (Clause);
3181          end loop;
3182       end if;
3183    end Install_Private_With_Clauses;
3184
3185    ----------------------
3186    -- Install_Siblings --
3187    ----------------------
3188
3189    procedure Install_Siblings (U_Name : Entity_Id; N : Node_Id) is
3190       Item : Node_Id;
3191       Id   : Entity_Id;
3192       Prev : Entity_Id;
3193    begin
3194       --  Iterate over explicit with clauses, and check whether the
3195       --  scope of each entity is an ancestor of the current unit.
3196
3197       Item := First (Context_Items (N));
3198       while Present (Item) loop
3199          if Nkind (Item) = N_With_Clause
3200            and then not Implicit_With (Item)
3201            and then not Limited_Present (Item)
3202          then
3203             Id := Entity (Name (Item));
3204
3205             if Is_Child_Unit (Id)
3206               and then Is_Ancestor_Package (Scope (Id), U_Name)
3207             then
3208                Set_Is_Immediately_Visible (Id);
3209
3210                --  Ada 0Y (AI-262): Make visible the private entities of
3211                --  private-withed siblings
3212
3213                if Private_Present (Item) then
3214                   Install_Private_Declarations (Id);
3215                end if;
3216
3217                --  Check for the presence of another unit in the context,
3218                --  that may be inadvertently hidden by the child.
3219
3220                Prev := Current_Entity (Id);
3221
3222                if Present (Prev)
3223                  and then Is_Immediately_Visible (Prev)
3224                  and then not Is_Child_Unit (Prev)
3225                then
3226                   declare
3227                      Clause : Node_Id;
3228
3229                   begin
3230                      Clause := First (Context_Items (N));
3231
3232                      while Present (Clause) loop
3233                         if Nkind (Clause) = N_With_Clause
3234                           and then Entity (Name (Clause)) = Prev
3235                         then
3236                            Error_Msg_NE
3237                               ("child unit& hides compilation unit " &
3238                                "with the same name?",
3239                                  Name (Item), Id);
3240                            exit;
3241                         end if;
3242
3243                         Next (Clause);
3244                      end loop;
3245                   end;
3246                end if;
3247
3248             --  the With_Clause may be on a grand-child, which makes
3249             --  the child immediately visible.
3250
3251             elsif Is_Child_Unit (Scope (Id))
3252               and then Is_Ancestor_Package (Scope (Scope (Id)), U_Name)
3253             then
3254                Set_Is_Immediately_Visible (Scope (Id));
3255             end if;
3256          end if;
3257
3258          Next (Item);
3259       end loop;
3260    end Install_Siblings;
3261
3262    -------------------------------
3263    -- Install_Limited_With_Unit --
3264    -------------------------------
3265
3266    procedure Install_Limited_Withed_Unit (N : Node_Id) is
3267       Unum             : constant Unit_Number_Type :=
3268                            Get_Source_Unit (Library_Unit (N));
3269       P_Unit           : constant Entity_Id := Unit (Library_Unit (N));
3270       P                : Entity_Id;
3271       Lim_Elmt         : Elmt_Id;
3272       Lim_Typ          : Entity_Id;
3273       Is_Child_Package : Boolean := False;
3274
3275       function In_Chain (E : Entity_Id) return Boolean;
3276       --  Check that the shadow entity is not already in the homonym
3277       --  chain, for example through a limited_with clause in a parent unit.
3278
3279       --------------
3280       -- In_Chain --
3281       --------------
3282
3283       function In_Chain (E : Entity_Id) return Boolean is
3284          H : Entity_Id := Current_Entity (E);
3285
3286       begin
3287          while Present (H) loop
3288             if H = E then
3289                return True;
3290             else
3291                H := Homonym (H);
3292             end if;
3293          end loop;
3294
3295          return False;
3296       end In_Chain;
3297
3298    --  Start of processing for Install_Limited_Withed_Unit
3299
3300    begin
3301       --  In case of limited with_clause on subprograms, generics, instances,
3302       --  or generic renamings, the corresponding error was previously posted
3303       --  and we have nothing to do here.
3304
3305       case Nkind (P_Unit) is
3306
3307          when N_Package_Declaration =>
3308             null;
3309
3310          when N_Subprogram_Declaration                 |
3311               N_Generic_Package_Declaration            |
3312               N_Generic_Subprogram_Declaration         |
3313               N_Package_Instantiation                  |
3314               N_Function_Instantiation                 |
3315               N_Procedure_Instantiation                |
3316               N_Generic_Package_Renaming_Declaration   |
3317               N_Generic_Procedure_Renaming_Declaration |
3318               N_Generic_Function_Renaming_Declaration =>
3319             return;
3320
3321          when others =>
3322             raise Program_Error;
3323       end case;
3324
3325       P := Defining_Unit_Name (Specification (P_Unit));
3326
3327       if Nkind (P) = N_Defining_Program_Unit_Name then
3328
3329          --  Retrieve entity of child package
3330
3331          Is_Child_Package := True;
3332          P := Defining_Identifier (P);
3333       end if;
3334
3335       --  A common usage of the limited-with is to have a limited-with
3336       --  in the package spec, and a normal with in its package body.
3337       --  For example:
3338
3339       --       limited with X;  -- [1]
3340       --       package A is ...
3341
3342       --       with X;          -- [2]
3343       --       package body A is ...
3344
3345       --  The compilation of A's body installs the entities of its
3346       --  withed packages (the context clauses found at [2]) and
3347       --  then the context clauses of its specification (found at [1]).
3348
3349       --  As a consequence, at point [1] the specification of X has been
3350       --  analyzed and it is immediately visible. According to the semantics
3351       --  of the limited-with context clauses we don't install the limited
3352       --  view because the full view of X supersedes its limited view.
3353
3354       if Analyzed (Cunit (Unum))
3355         and then (Is_Immediately_Visible (P)
3356                    or else (Is_Child_Package
3357                              and then Is_Visible_Child_Unit (P)))
3358       then
3359          return;
3360       end if;
3361
3362       if Debug_Flag_I then
3363          Write_Str ("install limited view of ");
3364          Write_Name (Chars (P));
3365          Write_Eol;
3366       end if;
3367
3368       if not Analyzed (Cunit (Unum)) then
3369          Set_Ekind (P, E_Package);
3370          Set_Etype (P, Standard_Void_Type);
3371          Set_Scope (P, Standard_Standard);
3372
3373          --  Place entity on visibility structure
3374
3375          if Current_Entity (P) /= P then
3376             Set_Homonym (P, Current_Entity (P));
3377             Set_Current_Entity (P);
3378
3379             if Debug_Flag_I then
3380                Write_Str ("   (homonym) chain ");
3381                Write_Name (Chars (P));
3382                Write_Eol;
3383             end if;
3384
3385          end if;
3386
3387          if Is_Child_Package then
3388             Set_Is_Child_Unit (P);
3389             Set_Is_Visible_Child_Unit (P);
3390
3391             declare
3392                Parent_Comp : Node_Id;
3393                Parent_Id   : Entity_Id;
3394
3395             begin
3396                Parent_Comp := Parent_Spec (Unit (Cunit (Unum)));
3397                Parent_Id   := Defining_Entity (Unit (Parent_Comp));
3398
3399                Set_Scope (P, Parent_Id);
3400             end;
3401          end if;
3402
3403       else
3404
3405          --  If the unit appears in a previous regular with_clause, the
3406          --  regular entities must be unchained before the shadow ones
3407          --  are made accessible.
3408
3409          declare
3410             Ent : Entity_Id;
3411          begin
3412             Ent := First_Entity (P);
3413
3414             while Present (Ent) loop
3415                Unchain (Ent);
3416                Next_Entity (Ent);
3417             end loop;
3418          end;
3419
3420       end if;
3421
3422       --  The package must be visible while the with_type clause is active,
3423       --  because references to the type P.T must resolve in the usual way.
3424
3425       Set_Is_Immediately_Visible (P);
3426
3427       --  Install each incomplete view
3428
3429       Lim_Elmt   := First_Elmt (Limited_Views (P));
3430
3431       while Present (Lim_Elmt) loop
3432          Lim_Typ  := Node (Lim_Elmt);
3433
3434          if not In_Chain (Lim_Typ) then
3435             Set_Homonym (Lim_Typ, Current_Entity (Lim_Typ));
3436             Set_Current_Entity (Lim_Typ);
3437
3438             if Debug_Flag_I then
3439                Write_Str ("   (homonym) chain ");
3440                Write_Name (Chars (Lim_Typ));
3441                Write_Eol;
3442             end if;
3443
3444          end if;
3445
3446          Next_Elmt (Lim_Elmt);
3447       end loop;
3448
3449       --  The context clause has installed a limited-view, mark it
3450       --  accordingly, to uninstall it when the context is removed.
3451
3452       Set_Limited_View_Installed (N);
3453       Set_From_With_Type (P);
3454    end Install_Limited_Withed_Unit;
3455
3456    -------------------------
3457    -- Install_Withed_Unit --
3458    -------------------------
3459
3460    procedure Install_Withed_Unit
3461      (With_Clause     : Node_Id;
3462       Private_With_OK : Boolean := False)
3463    is
3464       Uname : constant Entity_Id := Entity (Name (With_Clause));
3465       P     : constant Entity_Id := Scope (Uname);
3466
3467    begin
3468       --  Ada 0Y (AI-262): Do not install the private withed unit if we are
3469       --  compiling a package declaration and the Private_With_OK flag was not
3470       --  set by the caller. These declarations will be installed later (before
3471       --  analyzing the private part of the package).
3472
3473       if Private_Present (With_Clause)
3474         and then Nkind (Cunit (Current_Sem_Unit)) = N_Package_Declaration
3475         and then not (Private_With_OK)
3476       then
3477          return;
3478       end if;
3479
3480       if Debug_Flag_I then
3481          if Private_Present (With_Clause) then
3482             Write_Str ("install private withed unit ");
3483          else
3484             Write_Str ("install withed unit ");
3485          end if;
3486
3487          Write_Name (Chars (Uname));
3488          Write_Eol;
3489       end if;
3490
3491       --  We do not apply the restrictions to an internal unit unless
3492       --  we are compiling the internal unit as a main unit. This check
3493       --  is also skipped for dummy units (for missing packages).
3494
3495       if Sloc (Uname) /= No_Location
3496         and then (not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
3497                     or else Current_Sem_Unit = Main_Unit)
3498       then
3499          Check_Restricted_Unit
3500            (Unit_Name (Get_Source_Unit (Uname)), With_Clause);
3501       end if;
3502
3503       if P /= Standard_Standard then
3504
3505          --  If the unit is not analyzed after analysis of the with clause,
3506          --  and it is an instantiation, then it awaits a body and is the main
3507          --  unit. Its appearance in the context of some other unit indicates
3508          --  a circular dependency (DEC suite perversity).
3509
3510          if not Analyzed (Uname)
3511            and then Nkind (Parent (Uname)) = N_Package_Instantiation
3512          then
3513             Error_Msg_N
3514               ("instantiation depends on itself", Name (With_Clause));
3515
3516          elsif not Is_Visible_Child_Unit (Uname) then
3517             Set_Is_Visible_Child_Unit (Uname);
3518
3519             if Is_Generic_Instance (Uname)
3520               and then Ekind (Uname) in Subprogram_Kind
3521             then
3522                --  Set flag as well on the visible entity that denotes the
3523                --  instance, which renames the current one.
3524
3525                Set_Is_Visible_Child_Unit
3526                  (Related_Instance
3527                    (Defining_Entity (Unit (Library_Unit (With_Clause)))));
3528             end if;
3529
3530             --  The parent unit may have been installed already, and
3531             --  may have appeared in a use clause.
3532
3533             if In_Use (Scope (Uname)) then
3534                Set_Is_Potentially_Use_Visible (Uname);
3535             end if;
3536
3537             Set_Context_Installed (With_Clause);
3538          end if;
3539
3540       elsif not Is_Immediately_Visible (Uname) then
3541          if not Private_Present (With_Clause)
3542            or else Private_With_OK
3543          then
3544             Set_Is_Immediately_Visible (Uname);
3545          end if;
3546
3547          Set_Context_Installed (With_Clause);
3548       end if;
3549
3550       --   A with-clause overrides a with-type clause: there are no restric-
3551       --   tions on the use of package entities.
3552
3553       if Ekind (Uname) = E_Package then
3554          Set_From_With_Type (Uname, False);
3555       end if;
3556    end Install_Withed_Unit;
3557
3558    -------------------
3559    -- Is_Child_Spec --
3560    -------------------
3561
3562    function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
3563       K : constant Node_Kind := Nkind (Lib_Unit);
3564
3565    begin
3566       return (K in N_Generic_Declaration              or else
3567               K in N_Generic_Instantiation            or else
3568               K in N_Generic_Renaming_Declaration     or else
3569               K =  N_Package_Declaration              or else
3570               K =  N_Package_Renaming_Declaration     or else
3571               K =  N_Subprogram_Declaration           or else
3572               K =  N_Subprogram_Renaming_Declaration)
3573         and then Present (Parent_Spec (Lib_Unit));
3574    end Is_Child_Spec;
3575
3576    -----------------------
3577    -- Load_Needed_Body --
3578    -----------------------
3579
3580    --  N is a generic unit named in a with clause, or else it is
3581    --  a unit that contains a generic unit or an inlined function.
3582    --  In order to perform an instantiation, the body of the unit
3583    --  must be present. If the unit itself is generic, we assume
3584    --  that an instantiation follows, and  load and analyze the body
3585    --  unconditionally. This forces analysis of the spec as well.
3586
3587    --  If the unit is not generic, but contains a generic unit, it
3588    --  is loaded on demand, at the point of instantiation (see ch12).
3589
3590    procedure Load_Needed_Body (N : Node_Id; OK : out Boolean) is
3591       Body_Name : Unit_Name_Type;
3592       Unum      : Unit_Number_Type;
3593
3594       Save_Style_Check : constant Boolean := Opt.Style_Check;
3595       --  The loading and analysis is done with style checks off
3596
3597    begin
3598       if not GNAT_Mode then
3599          Style_Check := False;
3600       end if;
3601
3602       Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
3603       Unum :=
3604         Load_Unit
3605           (Load_Name  => Body_Name,
3606            Required   => False,
3607            Subunit    => False,
3608            Error_Node => N,
3609            Renamings  => True);
3610
3611       if Unum = No_Unit then
3612          OK := False;
3613
3614       else
3615          Compiler_State := Analyzing; -- reset after load
3616
3617          if not Fatal_Error (Unum) or else Try_Semantics then
3618             if Debug_Flag_L then
3619                Write_Str ("*** Loaded generic body");
3620                Write_Eol;
3621             end if;
3622
3623             Semantics (Cunit (Unum));
3624          end if;
3625
3626          OK := True;
3627       end if;
3628
3629       Style_Check := Save_Style_Check;
3630    end Load_Needed_Body;
3631
3632    -------------------------
3633    -- Build_Limited_Views --
3634    -------------------------
3635
3636    procedure Build_Limited_Views (N : Node_Id) is
3637       Unum : constant Unit_Number_Type := Get_Source_Unit (Library_Unit (N));
3638       P    : constant Entity_Id        := Cunit_Entity (Unum);
3639
3640       Spec        : Node_Id;         --  To denote a package specification
3641       Lim_Typ     : Entity_Id;       --  To denote shadow entities.
3642       Comp_Typ    : Entity_Id;       --  To denote real entities.
3643
3644       procedure Decorate_Incomplete_Type
3645         (E    : Entity_Id;
3646          Scop : Entity_Id);
3647       --  Add attributes of an incomplete type to a shadow entity. The same
3648       --  attributes are placed on the real entity, so that gigi receives
3649       --  a consistent view.
3650
3651       procedure Decorate_Package_Specification (P : Entity_Id);
3652       --  Add attributes of a package entity to the entity in a package
3653       --  declaration
3654
3655       procedure Decorate_Tagged_Type
3656         (Loc  : Source_Ptr;
3657          T    : Entity_Id;
3658          Scop : Entity_Id);
3659       --  Set basic attributes of tagged type T, including its class_wide type.
3660       --  The parameters Loc, Scope are used to decorate the class_wide type.
3661
3662       procedure Build_Chain (Spec : Node_Id; Scope : Entity_Id);
3663       --  Construct list of shadow entities and attach it to entity of
3664       --  package that is mentioned in a limited_with clause.
3665
3666       function New_Internal_Shadow_Entity
3667         (Kind       : Entity_Kind;
3668          Sloc_Value : Source_Ptr;
3669          Id_Char    : Character) return Entity_Id;
3670       --  This function is similar to New_Internal_Entity, except that the
3671       --  entity is not added to the scope's list of entities.
3672
3673       ------------------------------
3674       -- Decorate_Incomplete_Type --
3675       ------------------------------
3676
3677       procedure Decorate_Incomplete_Type
3678         (E    : Entity_Id;
3679          Scop : Entity_Id)
3680       is
3681       begin
3682          Set_Ekind                     (E, E_Incomplete_Type);
3683          Set_Scope                     (E, Scop);
3684          Set_Etype                     (E, E);
3685          Set_Is_First_Subtype          (E, True);
3686          Set_Stored_Constraint         (E, No_Elist);
3687          Set_Full_View                 (E, Empty);
3688          Init_Size_Align               (E);
3689       end Decorate_Incomplete_Type;
3690
3691       --------------------------
3692       -- Decorate_Tagged_Type --
3693       --------------------------
3694
3695       procedure Decorate_Tagged_Type
3696         (Loc  : Source_Ptr;
3697          T    : Entity_Id;
3698          Scop : Entity_Id)
3699       is
3700          CW : Entity_Id;
3701
3702       begin
3703          Decorate_Incomplete_Type (T, Scop);
3704          Set_Is_Tagged_Type (T);
3705
3706          --  Build corresponding class_wide type, if not previously done
3707
3708          if No (Class_Wide_Type (T)) then
3709             CW := Make_Defining_Identifier (Loc,  New_Internal_Name ('S'));
3710
3711             Set_Ekind                     (CW, E_Class_Wide_Type);
3712             Set_Etype                     (CW, T);
3713             Set_Scope                     (CW, Scop);
3714             Set_Is_Tagged_Type            (CW);
3715             Set_Is_First_Subtype          (CW, True);
3716             Init_Size_Align               (CW);
3717             Set_Has_Unknown_Discriminants (CW, True);
3718             Set_Class_Wide_Type           (CW, CW);
3719             Set_Equivalent_Type           (CW, Empty);
3720             Set_From_With_Type            (CW, From_With_Type (T));
3721
3722             Set_Class_Wide_Type (T, CW);
3723          end if;
3724       end Decorate_Tagged_Type;
3725
3726       ------------------------------------
3727       -- Decorate_Package_Specification --
3728       ------------------------------------
3729
3730       procedure Decorate_Package_Specification (P : Entity_Id) is
3731       begin
3732          --  Place only the most basic attributes
3733
3734          Set_Ekind (P, E_Package);
3735          Set_Etype (P, Standard_Void_Type);
3736       end Decorate_Package_Specification;
3737
3738       -------------------------
3739       -- New_Internal_Entity --
3740       -------------------------
3741
3742       function New_Internal_Shadow_Entity
3743         (Kind       : Entity_Kind;
3744          Sloc_Value : Source_Ptr;
3745          Id_Char    : Character) return Entity_Id
3746       is
3747          N : constant Entity_Id :=
3748                Make_Defining_Identifier (Sloc_Value,
3749                  Chars => New_Internal_Name (Id_Char));
3750
3751       begin
3752          Set_Ekind          (N, Kind);
3753          Set_Is_Internal    (N, True);
3754
3755          if Kind in Type_Kind then
3756             Init_Size_Align (N);
3757          end if;
3758
3759          return N;
3760       end New_Internal_Shadow_Entity;
3761
3762       -----------------
3763       -- Build_Chain --
3764       -----------------
3765
3766       --  Could use more comments below ???
3767
3768       procedure Build_Chain (Spec : Node_Id; Scope : Entity_Id) is
3769          Analyzed_Unit : constant Boolean := Analyzed (Cunit (Unum));
3770          Is_Tagged     : Boolean;
3771          Decl          : Node_Id;
3772
3773       begin
3774          Decl := First (Visible_Declarations (Spec));
3775
3776          while Present (Decl) loop
3777             if Nkind (Decl) = N_Full_Type_Declaration then
3778                Is_Tagged :=
3779                   Nkind (Type_Definition (Decl)) = N_Record_Definition
3780                   and then Tagged_Present (Type_Definition (Decl));
3781
3782                Comp_Typ := Defining_Identifier (Decl);
3783
3784                if not Analyzed_Unit then
3785                   if Is_Tagged then
3786                      Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
3787                   else
3788                      Decorate_Incomplete_Type (Comp_Typ, Scope);
3789                   end if;
3790                end if;
3791
3792                --  Create shadow entity for type
3793
3794                Lim_Typ  := New_Internal_Shadow_Entity
3795                  (Kind       => Ekind (Comp_Typ),
3796                   Sloc_Value => Sloc (Comp_Typ),
3797                   Id_Char    => 'Z');
3798
3799                Set_Chars  (Lim_Typ, Chars (Comp_Typ));
3800                Set_Parent (Lim_Typ, Parent (Comp_Typ));
3801                Set_From_With_Type (Lim_Typ);
3802
3803                if Is_Tagged then
3804                   Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
3805                else
3806                   Decorate_Incomplete_Type (Lim_Typ, Scope);
3807                end if;
3808
3809                Set_Non_Limited_View (Lim_Typ, Comp_Typ);
3810                Append_Elmt (Lim_Typ,  To => Limited_Views (P));
3811
3812             elsif Nkind (Decl) = N_Private_Type_Declaration
3813               and then Tagged_Present (Decl)
3814             then
3815                Comp_Typ := Defining_Identifier (Decl);
3816
3817                if not Analyzed_Unit then
3818                   Decorate_Tagged_Type (Sloc (Decl), Comp_Typ, Scope);
3819                end if;
3820
3821                Lim_Typ  := New_Internal_Shadow_Entity
3822                  (Kind       => Ekind (Comp_Typ),
3823                   Sloc_Value => Sloc (Comp_Typ),
3824                   Id_Char    => 'Z');
3825
3826                Set_Chars  (Lim_Typ, Chars (Comp_Typ));
3827                Set_Parent (Lim_Typ, Parent (Comp_Typ));
3828                Set_From_With_Type (Lim_Typ);
3829
3830                Decorate_Tagged_Type (Sloc (Decl), Lim_Typ, Scope);
3831
3832                Set_Non_Limited_View (Lim_Typ, Comp_Typ);
3833                Append_Elmt (Lim_Typ,  To => Limited_Views (P));
3834
3835             elsif Nkind (Decl) = N_Package_Declaration then
3836
3837                --  Local package
3838
3839                declare
3840                   Spec : constant Node_Id := Specification (Decl);
3841
3842                begin
3843                   Comp_Typ := Defining_Unit_Name (Spec);
3844
3845                   if not Analyzed (Cunit (Unum)) then
3846                      Decorate_Package_Specification (Comp_Typ);
3847                      Set_Scope (Comp_Typ, Scope);
3848                   end if;
3849
3850                   Lim_Typ  := New_Internal_Shadow_Entity
3851                     (Kind       => Ekind (Comp_Typ),
3852                      Sloc_Value => Sloc (Comp_Typ),
3853                      Id_Char    => 'Z');
3854
3855                   Decorate_Package_Specification (Lim_Typ);
3856                   Set_Scope (Lim_Typ, Scope);
3857
3858                   Set_Chars (Lim_Typ, Chars (Comp_Typ));
3859                   Set_Parent (Lim_Typ, Parent (Comp_Typ));
3860                   Set_From_With_Type (Lim_Typ);
3861
3862                   --  Note: The non_limited_view attribute is not used
3863                   --  for local packages.
3864
3865                   Append_Elmt (Lim_Typ,  To => Limited_Views (P));
3866
3867                   Build_Chain (Spec, Scope => Lim_Typ);
3868                end;
3869             end if;
3870
3871             Next (Decl);
3872          end loop;
3873       end Build_Chain;
3874
3875    --  Start of processing for Build_Limited_Views
3876
3877    begin
3878       pragma Assert (Limited_Present (N));
3879
3880       --  A library_item mentioned in a limited_with_clause shall be
3881       --  a package_declaration, not a subprogram_declaration,
3882       --  generic_declaration, generic_instantiation, or
3883       --  package_renaming_declaration
3884
3885       case Nkind (Unit (Library_Unit (N))) is
3886
3887          when N_Package_Declaration =>
3888             null;
3889
3890          when N_Subprogram_Declaration =>
3891             Error_Msg_N ("subprograms not allowed in "
3892                          & "limited with_clauses", N);
3893             return;
3894
3895          when N_Generic_Package_Declaration |
3896               N_Generic_Subprogram_Declaration =>
3897             Error_Msg_N ("generics not allowed in "
3898                          & "limited with_clauses", N);
3899             return;
3900
3901          when N_Package_Instantiation |
3902               N_Function_Instantiation |
3903               N_Procedure_Instantiation =>
3904             Error_Msg_N ("generic instantiations not allowed in "
3905                          & "limited with_clauses", N);
3906             return;
3907
3908          when N_Generic_Package_Renaming_Declaration |
3909               N_Generic_Procedure_Renaming_Declaration |
3910               N_Generic_Function_Renaming_Declaration =>
3911             Error_Msg_N ("generic renamings not allowed in "
3912                          & "limited with_clauses", N);
3913             return;
3914
3915          when others =>
3916             raise Program_Error;
3917       end case;
3918
3919       --  Check if the chain is already built
3920
3921       Spec := Specification (Unit (Library_Unit (N)));
3922
3923       if Limited_View_Installed (Spec) then
3924          return;
3925       end if;
3926
3927       Set_Ekind (P, E_Package);
3928       Set_Limited_Views     (P, New_Elmt_List);
3929       --  Set_Entity (Name (N), P);
3930
3931       --  Create the auxiliary chain
3932
3933       Build_Chain (Spec, Scope => P);
3934       Set_Limited_View_Installed (Spec);
3935    end Build_Limited_Views;
3936
3937    -------------------------------
3938    -- Check_Body_Needed_For_SAL --
3939    -------------------------------
3940
3941    procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id) is
3942
3943       function Entity_Needs_Body (E : Entity_Id) return Boolean;
3944       --  Determine whether use of entity E might require the presence
3945       --  of its body. For a package this requires a recursive traversal
3946       --  of all nested declarations.
3947
3948       ---------------------------
3949       -- Entity_Needed_For_SAL --
3950       ---------------------------
3951
3952       function Entity_Needs_Body (E : Entity_Id) return Boolean is
3953          Ent : Entity_Id;
3954
3955       begin
3956          if Is_Subprogram (E)
3957            and then Has_Pragma_Inline (E)
3958          then
3959             return True;
3960
3961          elsif Ekind (E) = E_Generic_Function
3962            or else Ekind (E) = E_Generic_Procedure
3963          then
3964             return True;
3965
3966          elsif Ekind (E) = E_Generic_Package
3967            and then
3968              Nkind (Unit_Declaration_Node (E)) = N_Generic_Package_Declaration
3969            and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
3970          then
3971             return True;
3972
3973          elsif Ekind (E) = E_Package
3974            and then
3975              Nkind (Unit_Declaration_Node (E)) = N_Package_Declaration
3976            and then Present (Corresponding_Body (Unit_Declaration_Node (E)))
3977          then
3978             Ent := First_Entity (E);
3979
3980             while Present (Ent) loop
3981                if Entity_Needs_Body (Ent) then
3982                   return True;
3983                end if;
3984
3985                Next_Entity (Ent);
3986             end loop;
3987
3988             return False;
3989
3990          else
3991             return False;
3992          end if;
3993       end Entity_Needs_Body;
3994
3995    --  Start of processing for Check_Body_Needed_For_SAL
3996
3997    begin
3998       if Ekind (Unit_Name) = E_Generic_Package
3999         and then
4000           Nkind (Unit_Declaration_Node (Unit_Name)) =
4001                                             N_Generic_Package_Declaration
4002         and then
4003           Present (Corresponding_Body (Unit_Declaration_Node (Unit_Name)))
4004       then
4005          Set_Body_Needed_For_SAL (Unit_Name);
4006
4007       elsif Ekind (Unit_Name) = E_Generic_Procedure
4008         or else Ekind (Unit_Name) = E_Generic_Function
4009       then
4010          Set_Body_Needed_For_SAL (Unit_Name);
4011
4012       elsif Is_Subprogram (Unit_Name)
4013         and then Nkind (Unit_Declaration_Node (Unit_Name)) =
4014                                             N_Subprogram_Declaration
4015         and then Has_Pragma_Inline (Unit_Name)
4016       then
4017          Set_Body_Needed_For_SAL (Unit_Name);
4018
4019       elsif Ekind (Unit_Name) = E_Subprogram_Body then
4020          Check_Body_Needed_For_SAL
4021            (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
4022
4023       elsif Ekind (Unit_Name) = E_Package
4024         and then Entity_Needs_Body (Unit_Name)
4025       then
4026          Set_Body_Needed_For_SAL (Unit_Name);
4027
4028       elsif Ekind (Unit_Name) = E_Package_Body
4029         and then Nkind (Unit_Declaration_Node (Unit_Name)) = N_Package_Body
4030       then
4031          Check_Body_Needed_For_SAL
4032            (Corresponding_Spec (Unit_Declaration_Node (Unit_Name)));
4033       end if;
4034    end Check_Body_Needed_For_SAL;
4035
4036    --------------------
4037    -- Remove_Context --
4038    --------------------
4039
4040    procedure Remove_Context (N : Node_Id) is
4041       Lib_Unit : constant Node_Id := Unit (N);
4042
4043    begin
4044       --  If this is a child unit, first remove the parent units.
4045
4046       if Is_Child_Spec (Lib_Unit) then
4047          Remove_Parents (Lib_Unit);
4048       end if;
4049
4050       Remove_Context_Clauses (N);
4051    end Remove_Context;
4052
4053    ----------------------------
4054    -- Remove_Context_Clauses --
4055    ----------------------------
4056
4057    procedure Remove_Context_Clauses (N : Node_Id) is
4058       Item      : Node_Id;
4059       Unit_Name : Entity_Id;
4060
4061    begin
4062       --  Ada0Y (AI-50217): We remove the context clauses in two phases:
4063       --  limited-views first and regular-views later (to maintain the
4064       --  stack model).
4065
4066       --  First Phase: Remove limited_with context clauses
4067
4068       Item := First (Context_Items (N));
4069       while Present (Item) loop
4070
4071          --  We are interested only in with clauses which got installed
4072          --  on entry.
4073
4074          if Nkind (Item) = N_With_Clause
4075            and then Limited_Present (Item)
4076            and then Limited_View_Installed (Item)
4077          then
4078             Remove_Limited_With_Clause (Item);
4079
4080          end if;
4081
4082          Next (Item);
4083       end loop;
4084
4085       --  Second Phase: Loop through context items and undo regular
4086       --  with_clauses and use_clauses.
4087
4088       Item := First (Context_Items (N));
4089       while Present (Item) loop
4090
4091          --  We are interested only in with clauses which got installed
4092          --  on entry, as indicated by their Context_Installed flag set
4093
4094          if Nkind (Item) = N_With_Clause
4095            and then Limited_Present (Item)
4096            and then Limited_View_Installed (Item)
4097          then
4098             null;
4099
4100          elsif Nkind (Item) = N_With_Clause
4101             and then Context_Installed (Item)
4102          then
4103             --  Remove items from one with'ed unit
4104
4105             Unit_Name := Entity (Name (Item));
4106             Remove_Unit_From_Visibility (Unit_Name);
4107             Set_Context_Installed (Item, False);
4108
4109          elsif Nkind (Item) = N_Use_Package_Clause then
4110             End_Use_Package (Item);
4111
4112          elsif Nkind (Item) = N_Use_Type_Clause then
4113             End_Use_Type (Item);
4114
4115          elsif Nkind (Item) = N_With_Type_Clause then
4116             Remove_With_Type_Clause (Name (Item));
4117          end if;
4118
4119          Next (Item);
4120       end loop;
4121    end Remove_Context_Clauses;
4122
4123    --------------------------------
4124    -- Remove_Limited_With_Clause --
4125    --------------------------------
4126
4127    procedure Remove_Limited_With_Clause (N : Node_Id) is
4128       P_Unit    : constant Entity_Id := Unit (Library_Unit (N));
4129       P         : Entity_Id := Defining_Unit_Name (Specification (P_Unit));
4130       Lim_Elmt  : Elmt_Id;
4131       Lim_Typ   : Entity_Id;
4132
4133    begin
4134       if Nkind (P) = N_Defining_Program_Unit_Name then
4135
4136          --  Retrieve entity of Child package
4137
4138          P := Defining_Identifier (P);
4139       end if;
4140
4141       if Debug_Flag_I then
4142          Write_Str ("remove limited view of ");
4143          Write_Name (Chars (P));
4144          Write_Str (" from visibility");
4145          Write_Eol;
4146       end if;
4147
4148       --  Remove all shadow entities from visibility
4149
4150       Lim_Elmt  := First_Elmt (Limited_Views (P));
4151
4152       while Present (Lim_Elmt) loop
4153          Lim_Typ  := Node (Lim_Elmt);
4154
4155          Unchain (Lim_Typ);
4156          Next_Elmt (Lim_Elmt);
4157       end loop;
4158
4159       --  Indicate that the limited view of the package is not installed
4160
4161       Set_From_With_Type (P, False);
4162       Set_Limited_View_Installed (N, False);
4163
4164       --  If the exporting package has previously been analyzed, it
4165       --  has appeared in the closure already and should be left alone.
4166       --  Otherwise, remove package itself from visibility.
4167
4168       if not Analyzed (P_Unit) then
4169          Unchain (P);
4170          Set_First_Entity (P, Empty);
4171          Set_Last_Entity (P, Empty);
4172          Set_Ekind (P, E_Void);
4173          Set_Scope (P, Empty);
4174          Set_Is_Immediately_Visible (P, False);
4175
4176       else
4177
4178          --  Reinstall visible entities (entities removed from visibility in
4179          --  Install_Limited_Withed to install the shadow entities).
4180
4181          declare
4182             Ent : Entity_Id;
4183
4184          begin
4185             Ent := First_Entity (P);
4186             while Present (Ent) and then Ent /= First_Private_Entity (P) loop
4187
4188                --  Shadow entities have not been added to the list of
4189                --  entities associated to the package spec. Therefore we
4190                --  just have to re-chain all its visible entities.
4191
4192                if not Is_Class_Wide_Type (Ent) then
4193
4194                   Set_Homonym (Ent, Current_Entity (Ent));
4195                   Set_Current_Entity (Ent);
4196
4197                   if Debug_Flag_I then
4198                      Write_Str ("   (homonym) chain ");
4199                      Write_Name (Chars (Ent));
4200                      Write_Eol;
4201                   end if;
4202
4203                end if;
4204
4205                Next_Entity (Ent);
4206             end loop;
4207          end;
4208       end if;
4209    end Remove_Limited_With_Clause;
4210
4211    --------------------
4212    -- Remove_Parents --
4213    --------------------
4214
4215    procedure Remove_Parents (Lib_Unit : Node_Id) is
4216       P      : Node_Id;
4217       P_Name : Entity_Id;
4218       E      : Entity_Id;
4219       Vis    : constant Boolean :=
4220                  Scope_Stack.Table (Scope_Stack.Last).Previous_Visibility;
4221
4222    begin
4223       if Is_Child_Spec (Lib_Unit) then
4224          P := Unit (Parent_Spec (Lib_Unit));
4225          P_Name := Get_Parent_Entity (P);
4226
4227          Remove_Context_Clauses (Parent_Spec (Lib_Unit));
4228          End_Package_Scope (P_Name);
4229          Set_Is_Immediately_Visible (P_Name, Vis);
4230
4231          --  Remove from visibility the siblings as well, which are directly
4232          --  visible while the parent is in scope.
4233
4234          E := First_Entity (P_Name);
4235
4236          while Present (E) loop
4237
4238             if Is_Child_Unit (E) then
4239                Set_Is_Immediately_Visible (E, False);
4240             end if;
4241
4242             Next_Entity (E);
4243          end loop;
4244
4245          Set_In_Package_Body (P_Name, False);
4246
4247          --  This is the recursive call to remove the context of any
4248          --  higher level parent. This recursion ensures that all parents
4249          --  are removed in the reverse order of their installation.
4250
4251          Remove_Parents (P);
4252       end if;
4253    end Remove_Parents;
4254
4255    -----------------------------
4256    -- Remove_With_Type_Clause --
4257    -----------------------------
4258
4259    procedure Remove_With_Type_Clause (Name : Node_Id) is
4260       Typ : Entity_Id;
4261       P   : Entity_Id;
4262
4263       procedure Unchain (E : Entity_Id);
4264       --  Remove entity from visibility list.
4265
4266       procedure Unchain (E : Entity_Id) is
4267          Prev : Entity_Id;
4268
4269       begin
4270          Prev := Current_Entity (E);
4271
4272          --  Package entity may appear is several with_type_clauses, and
4273          --  may have been removed already.
4274
4275          if No (Prev) then
4276             return;
4277
4278          elsif Prev = E then
4279             Set_Name_Entity_Id (Chars (E), Homonym (E));
4280
4281          else
4282             while Present (Prev)
4283               and then Homonym (Prev) /= E
4284             loop
4285                Prev := Homonym (Prev);
4286             end loop;
4287
4288             if Present (Prev) then
4289                Set_Homonym (Prev, Homonym (E));
4290             end if;
4291          end if;
4292       end Unchain;
4293
4294       --  Start of Remove_With_Type_Clause
4295
4296    begin
4297       if Nkind (Name) = N_Selected_Component then
4298          Typ := Entity (Selector_Name (Name));
4299
4300          if No (Typ) then    --  error in declaration.
4301             return;
4302          end if;
4303       else
4304          return;
4305       end if;
4306
4307       P := Scope (Typ);
4308
4309       --  If the exporting package has been analyzed, it has appeared in the
4310       --  context already and should be left alone. Otherwise, remove from
4311       --  visibility.
4312
4313       if not Analyzed (Unit_Declaration_Node (P)) then
4314          Unchain (P);
4315          Unchain (Typ);
4316          Set_Is_Frozen (Typ, False);
4317       end if;
4318
4319       if Ekind (Typ) = E_Record_Type then
4320          Set_From_With_Type (Class_Wide_Type (Typ), False);
4321          Set_From_With_Type (Typ, False);
4322       end if;
4323
4324       Set_From_With_Type (P, False);
4325
4326       --  If P is a child unit, remove parents as well.
4327
4328       P := Scope (P);
4329
4330       while Present (P)
4331         and then P /= Standard_Standard
4332       loop
4333          Set_From_With_Type (P, False);
4334
4335          if not Analyzed (Unit_Declaration_Node (P)) then
4336             Unchain (P);
4337          end if;
4338
4339          P := Scope (P);
4340       end loop;
4341
4342       --  The back-end needs to know that an access type is imported, so it
4343       --  does not need elaboration and can appear in a mutually recursive
4344       --  record definition, so the imported flag on an access  type is
4345       --  preserved.
4346
4347    end Remove_With_Type_Clause;
4348
4349    ---------------------------------
4350    -- Remove_Unit_From_Visibility --
4351    ---------------------------------
4352
4353    procedure Remove_Unit_From_Visibility (Unit_Name : Entity_Id) is
4354       P : constant Entity_Id := Scope (Unit_Name);
4355
4356    begin
4357
4358       if Debug_Flag_I then
4359          Write_Str ("remove unit ");
4360          Write_Name (Chars (Unit_Name));
4361          Write_Str (" from visibility");
4362          Write_Eol;
4363       end if;
4364
4365       if P /= Standard_Standard then
4366          Set_Is_Visible_Child_Unit (Unit_Name, False);
4367       end if;
4368
4369       Set_Is_Potentially_Use_Visible (Unit_Name, False);
4370       Set_Is_Immediately_Visible     (Unit_Name, False);
4371
4372    end Remove_Unit_From_Visibility;
4373
4374    -------------
4375    -- Unchain --
4376    -------------
4377
4378    procedure Unchain (E : Entity_Id) is
4379       Prev : Entity_Id;
4380
4381    begin
4382       Prev := Current_Entity (E);
4383
4384       if No (Prev) then
4385          return;
4386
4387       elsif Prev = E then
4388          Set_Name_Entity_Id (Chars (E), Homonym (E));
4389
4390       else
4391          while Present (Prev)
4392            and then Homonym (Prev) /= E
4393          loop
4394             Prev := Homonym (Prev);
4395          end loop;
4396
4397          if Present (Prev) then
4398             Set_Homonym (Prev, Homonym (E));
4399          end if;
4400       end if;
4401
4402       if Debug_Flag_I then
4403          Write_Str ("   (homonym) unchain ");
4404          Write_Name (Chars (E));
4405          Write_Eol;
4406       end if;
4407
4408    end Unchain;
4409 end Sem_Ch10;