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