OSDN Git Service

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