OSDN Git Service

2009-05-06 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch8.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M . C H 8                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2009, 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 3,  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 COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Debug;    use Debug;
28 with Einfo;    use Einfo;
29 with Elists;   use Elists;
30 with Errout;   use Errout;
31 with Exp_Tss;  use Exp_Tss;
32 with Exp_Util; use Exp_Util;
33 with Fname;    use Fname;
34 with Freeze;   use Freeze;
35 with Impunit;  use Impunit;
36 with Lib;      use Lib;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Namet;    use Namet;
40 with Namet.Sp; use Namet.Sp;
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 Rident;   use Rident;
47 with Rtsfind;  use Rtsfind;
48 with Sem;      use Sem;
49 with Sem_Aux;  use Sem_Aux;
50 with Sem_Cat;  use Sem_Cat;
51 with Sem_Ch3;  use Sem_Ch3;
52 with Sem_Ch4;  use Sem_Ch4;
53 with Sem_Ch6;  use Sem_Ch6;
54 with Sem_Ch12; use Sem_Ch12;
55 with Sem_Disp; use Sem_Disp;
56 with Sem_Dist; use Sem_Dist;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res;  use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sem_Type; use Sem_Type;
61 with Stand;    use Stand;
62 with Sinfo;    use Sinfo;
63 with Sinfo.CN; use Sinfo.CN;
64 with Snames;   use Snames;
65 with Style;    use Style;
66 with Table;
67 with Tbuild;   use Tbuild;
68 with Uintp;    use Uintp;
69
70 package body Sem_Ch8 is
71
72    ------------------------------------
73    -- Visibility and Name Resolution --
74    ------------------------------------
75
76    --  This package handles name resolution and the collection of
77    --  interpretations for overloaded names, prior to overload resolution.
78
79    --  Name resolution is the process that establishes a mapping between source
80    --  identifiers and the entities they denote at each point in the program.
81    --  Each entity is represented by a defining occurrence. Each identifier
82    --  that denotes an entity points to the corresponding defining occurrence.
83    --  This is the entity of the applied occurrence. Each occurrence holds
84    --  an index into the names table, where source identifiers are stored.
85
86    --  Each entry in the names table for an identifier or designator uses the
87    --  Info pointer to hold a link to the currently visible entity that has
88    --  this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
89    --  in package Sem_Util). The visibility is initialized at the beginning of
90    --  semantic processing to make entities in package Standard immediately
91    --  visible. The visibility table is used in a more subtle way when
92    --  compiling subunits (see below).
93
94    --  Entities that have the same name (i.e. homonyms) are chained. In the
95    --  case of overloaded entities, this chain holds all the possible meanings
96    --  of a given identifier. The process of overload resolution uses type
97    --  information to select from this chain the unique meaning of a given
98    --  identifier.
99
100    --  Entities are also chained in their scope, through the Next_Entity link.
101    --  As a consequence, the name space is organized as a sparse matrix, where
102    --  each row corresponds to a scope, and each column to a source identifier.
103    --  Open scopes, that is to say scopes currently being compiled, have their
104    --  corresponding rows of entities in order, innermost scope first.
105
106    --  The scopes of packages that are mentioned in  context clauses appear in
107    --  no particular order, interspersed among open scopes. This is because
108    --  in the course of analyzing the context of a compilation, a package
109    --  declaration is first an open scope, and subsequently an element of the
110    --  context. If subunits or child units are present, a parent unit may
111    --  appear under various guises at various times in the compilation.
112
113    --  When the compilation of the innermost scope is complete, the entities
114    --  defined therein are no longer visible. If the scope is not a package
115    --  declaration, these entities are never visible subsequently, and can be
116    --  removed from visibility chains. If the scope is a package declaration,
117    --  its visible declarations may still be accessible. Therefore the entities
118    --  defined in such a scope are left on the visibility chains, and only
119    --  their visibility (immediately visibility or potential use-visibility)
120    --  is affected.
121
122    --  The ordering of homonyms on their chain does not necessarily follow
123    --  the order of their corresponding scopes on the scope stack. For
124    --  example, if package P and the enclosing scope both contain entities
125    --  named E, then when compiling the package body the chain for E will
126    --  hold the global entity first,  and the local one (corresponding to
127    --  the current inner scope) next. As a result, name resolution routines
128    --  do not assume any relative ordering of the homonym chains, either
129    --  for scope nesting or to order of appearance of context clauses.
130
131    --  When compiling a child unit, entities in the parent scope are always
132    --  immediately visible. When compiling the body of a child unit, private
133    --  entities in the parent must also be made immediately visible. There
134    --  are separate routines to make the visible and private declarations
135    --  visible at various times (see package Sem_Ch7).
136
137    --              +--------+         +-----+
138    --              | In use |-------->| EU1 |-------------------------->
139    --              +--------+         +-----+
140    --                                    |                      |
141    --      +--------+                 +-----+                +-----+
142    --      | Stand. |---------------->| ES1 |--------------->| ES2 |--->
143    --      +--------+                 +-----+                +-----+
144    --                                    |                      |
145    --              +---------+           |                   +-----+
146    --              | with'ed |------------------------------>| EW2 |--->
147    --              +---------+           |                   +-----+
148    --                                    |                      |
149    --      +--------+                 +-----+                +-----+
150    --      | Scope2 |---------------->| E12 |--------------->| E22 |--->
151    --      +--------+                 +-----+                +-----+
152    --                                    |                      |
153    --      +--------+                 +-----+                +-----+
154    --      | Scope1 |---------------->| E11 |--------------->| E12 |--->
155    --      +--------+                 +-----+                +-----+
156    --          ^                         |                      |
157    --          |                         |                      |
158    --          |   +---------+           |                      |
159    --          |   | with'ed |----------------------------------------->
160    --          |   +---------+           |                      |
161    --          |                         |                      |
162    --      Scope stack                   |                      |
163    --      (innermost first)             |                      |
164    --                                 +----------------------------+
165    --      Names  table =>            | Id1 |     |    |     | Id2 |
166    --                                 +----------------------------+
167
168    --  Name resolution must deal with several syntactic forms: simple names,
169    --  qualified names, indexed names, and various forms of calls.
170
171    --  Each identifier points to an entry in the names table. The resolution
172    --  of a simple name consists in traversing the homonym chain, starting
173    --  from the names table. If an entry is immediately visible, it is the one
174    --  designated by the identifier. If only potentially use-visible entities
175    --  are on the chain, we must verify that they do not hide each other. If
176    --  the entity we find is overloadable, we collect all other overloadable
177    --  entities on the chain as long as they are not hidden.
178    --
179    --  To resolve expanded names, we must find the entity at the intersection
180    --  of the entity chain for the scope (the prefix) and the homonym chain
181    --  for the selector. In general, homonym chains will be much shorter than
182    --  entity chains, so it is preferable to start from the names table as
183    --  well. If the entity found is overloadable, we must collect all other
184    --  interpretations that are defined in the scope denoted by the prefix.
185
186    --  For records, protected types, and tasks, their local entities are
187    --  removed from visibility chains on exit from the corresponding scope.
188    --  From the outside, these entities are always accessed by selected
189    --  notation, and the entity chain for the record type, protected type,
190    --  etc. is traversed sequentially in  order to find the designated entity.
191
192    --  The discriminants of a type and the operations of a protected type or
193    --  task are unchained on  exit from the first view of the type, (such as
194    --  a private or incomplete type declaration, or a protected type speci-
195    --  fication) and re-chained when compiling the second view.
196
197    --  In the case of operators,  we do not make operators on derived types
198    --  explicit. As a result, the notation P."+" may denote either a user-
199    --  defined function with name "+", or else an implicit declaration of the
200    --  operator "+" in package P. The resolution of expanded names always
201    --  tries to resolve an operator name as such an implicitly defined entity,
202    --  in addition to looking for explicit declarations.
203
204    --  All forms of names that denote entities (simple names, expanded names,
205    --  character literals in some cases) have a Entity attribute, which
206    --  identifies the entity denoted by the name.
207
208    ---------------------
209    -- The Scope Stack --
210    ---------------------
211
212    --  The Scope stack keeps track of the scopes currently been compiled.
213    --  Every entity that contains declarations (including records) is placed
214    --  on the scope stack while it is being processed, and removed at the end.
215    --  Whenever a non-package scope is exited, the entities defined therein
216    --  are removed from the visibility table, so that entities in outer scopes
217    --  become visible (see previous description). On entry to Sem, the scope
218    --  stack only contains the package Standard. As usual, subunits complicate
219    --  this picture ever so slightly.
220
221    --  The Rtsfind mechanism can force a call to Semantics while another
222    --  compilation is in progress. The unit retrieved by Rtsfind must be
223    --  compiled in  its own context, and has no access to the visibility of
224    --  the unit currently being compiled. The procedures Save_Scope_Stack and
225    --  Restore_Scope_Stack make entities in current open scopes invisible
226    --  before compiling the retrieved unit, and restore the compilation
227    --  environment afterwards.
228
229    ------------------------
230    -- Compiling subunits --
231    ------------------------
232
233    --  Subunits must be compiled in the environment of the corresponding stub,
234    --  that is to say with the same visibility into the parent (and its
235    --  context) that is available at the point of the stub declaration, but
236    --  with the additional visibility provided by the context clause of the
237    --  subunit itself. As a result, compilation of a subunit forces compilation
238    --  of the parent (see description in lib-). At the point of the stub
239    --  declaration, Analyze is called recursively to compile the proper body of
240    --  the subunit, but without reinitializing the names table, nor the scope
241    --  stack (i.e. standard is not pushed on the stack). In this fashion the
242    --  context of the subunit is added to the context of the parent, and the
243    --  subunit is compiled in the correct environment. Note that in the course
244    --  of processing the context of a subunit, Standard will appear twice on
245    --  the scope stack: once for the parent of the subunit, and once for the
246    --  unit in the context clause being compiled. However, the two sets of
247    --  entities are not linked by homonym chains, so that the compilation of
248    --  any context unit happens in a fresh visibility environment.
249
250    -------------------------------
251    -- Processing of USE Clauses --
252    -------------------------------
253
254    --  Every defining occurrence has a flag indicating if it is potentially use
255    --  visible. Resolution of simple names examines this flag. The processing
256    --  of use clauses consists in setting this flag on all visible entities
257    --  defined in the corresponding package. On exit from the scope of the use
258    --  clause, the corresponding flag must be reset. However, a package may
259    --  appear in several nested use clauses (pathological but legal, alas!)
260    --  which forces us to use a slightly more involved scheme:
261
262    --    a) The defining occurrence for a package holds a flag -In_Use- to
263    --    indicate that it is currently in the scope of a use clause. If a
264    --    redundant use clause is encountered, then the corresponding occurrence
265    --    of the package name is flagged -Redundant_Use-.
266
267    --    b) On exit from a scope, the use clauses in its declarative part are
268    --    scanned. The visibility flag is reset in all entities declared in
269    --    package named in a use clause, as long as the package is not flagged
270    --    as being in a redundant use clause (in which case the outer use
271    --    clause is still in effect, and the direct visibility of its entities
272    --    must be retained).
273
274    --  Note that entities are not removed from their homonym chains on exit
275    --  from the package specification. A subsequent use clause does not need
276    --  to rechain the visible entities, but only to establish their direct
277    --  visibility.
278
279    -----------------------------------
280    -- Handling private declarations --
281    -----------------------------------
282
283    --  The principle that each entity has a single defining occurrence clashes
284    --  with the presence of two separate definitions for private types: the
285    --  first is the private type declaration, and second is the full type
286    --  declaration. It is important that all references to the type point to
287    --  the same defining occurrence, namely the first one. To enforce the two
288    --  separate views of the entity, the corresponding information is swapped
289    --  between the two declarations. Outside of the package, the defining
290    --  occurrence only contains the private declaration information, while in
291    --  the private part and the body of the package the defining occurrence
292    --  contains the full declaration. To simplify the swap, the defining
293    --  occurrence that currently holds the private declaration points to the
294    --  full declaration. During semantic processing the defining occurrence
295    --  also points to a list of private dependents, that is to say access types
296    --  or composite types whose designated types or component types are
297    --  subtypes or derived types of the private type in question. After the
298    --  full declaration has been seen, the private dependents are updated to
299    --  indicate that they have full definitions.
300
301    ------------------------------------
302    -- Handling of Undefined Messages --
303    ------------------------------------
304
305    --  In normal mode, only the first use of an undefined identifier generates
306    --  a message. The table Urefs is used to record error messages that have
307    --  been issued so that second and subsequent ones do not generate further
308    --  messages. However, the second reference causes text to be added to the
309    --  original undefined message noting "(more references follow)". The
310    --  full error list option (-gnatf) forces messages to be generated for
311    --  every reference and disconnects the use of this table.
312
313    type Uref_Entry is record
314       Node : Node_Id;
315       --  Node for identifier for which original message was posted. The
316       --  Chars field of this identifier is used to detect later references
317       --  to the same identifier.
318
319       Err : Error_Msg_Id;
320       --  Records error message Id of original undefined message. Reset to
321       --  No_Error_Msg after the second occurrence, where it is used to add
322       --  text to the original message as described above.
323
324       Nvis : Boolean;
325       --  Set if the message is not visible rather than undefined
326
327       Loc : Source_Ptr;
328       --  Records location of error message. Used to make sure that we do
329       --  not consider a, b : undefined as two separate instances, which
330       --  would otherwise happen, since the parser converts this sequence
331       --  to a : undefined; b : undefined.
332
333    end record;
334
335    package Urefs is new Table.Table (
336      Table_Component_Type => Uref_Entry,
337      Table_Index_Type     => Nat,
338      Table_Low_Bound      => 1,
339      Table_Initial        => 10,
340      Table_Increment      => 100,
341      Table_Name           => "Urefs");
342
343    Candidate_Renaming : Entity_Id;
344    --  Holds a candidate interpretation that appears in a subprogram renaming
345    --  declaration and does not match the given specification, but matches at
346    --  least on the first formal. Allows better error message when given
347    --  specification omits defaulted parameters, a common error.
348
349    -----------------------
350    -- Local Subprograms --
351    -----------------------
352
353    procedure Analyze_Generic_Renaming
354      (N : Node_Id;
355       K : Entity_Kind);
356    --  Common processing for all three kinds of generic renaming declarations.
357    --  Enter new name and indicate that it renames the generic unit.
358
359    procedure Analyze_Renamed_Character
360      (N       : Node_Id;
361       New_S   : Entity_Id;
362       Is_Body : Boolean);
363    --  Renamed entity is given by a character literal, which must belong
364    --  to the return type of the new entity. Is_Body indicates whether the
365    --  declaration is a renaming_as_body. If the original declaration has
366    --  already been frozen (because of an intervening body, e.g.) the body of
367    --  the function must be built now. The same applies to the following
368    --  various renaming procedures.
369
370    procedure Analyze_Renamed_Dereference
371      (N       : Node_Id;
372       New_S   : Entity_Id;
373       Is_Body : Boolean);
374    --  Renamed entity is given by an explicit dereference. Prefix must be a
375    --  conformant access_to_subprogram type.
376
377    procedure Analyze_Renamed_Entry
378      (N       : Node_Id;
379       New_S   : Entity_Id;
380       Is_Body : Boolean);
381    --  If the renamed entity in a subprogram renaming is an entry or protected
382    --  subprogram, build a body for the new entity whose only statement is a
383    --  call to the renamed entity.
384
385    procedure Analyze_Renamed_Family_Member
386      (N       : Node_Id;
387       New_S   : Entity_Id;
388       Is_Body : Boolean);
389    --  Used when the renamed entity is an indexed component. The prefix must
390    --  denote an entry family.
391
392    procedure Analyze_Renamed_Primitive_Operation
393      (N       : Node_Id;
394       New_S   : Entity_Id;
395       Is_Body : Boolean);
396    --  If the renamed entity in a subprogram renaming is a primitive operation
397    --  or a class-wide operation in prefix form, save the target object, which
398    --  must be added to the list of actuals in any subsequent call.
399
400    function Applicable_Use (Pack_Name : Node_Id) return Boolean;
401    --  Common code to Use_One_Package and Set_Use, to determine whether
402    --  use clause must be processed. Pack_Name is an entity name that
403    --  references the package in question.
404
405    procedure Attribute_Renaming (N : Node_Id);
406    --  Analyze renaming of attribute as subprogram. The renaming declaration N
407    --  is rewritten as a subprogram body that returns the attribute reference
408    --  applied to the formals of the function.
409
410    procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
411    --  A renaming_as_body may occur after the entity of the original decla-
412    --  ration has been frozen. In that case, the body of the new entity must
413    --  be built now, because the usual mechanism of building the renamed
414    --  body at the point of freezing will not work. Subp is the subprogram
415    --  for which N provides the Renaming_As_Body.
416
417    procedure Check_In_Previous_With_Clause
418      (N   : Node_Id;
419       Nam : Node_Id);
420    --  N is a use_package clause and Nam the package name, or N is a use_type
421    --  clause and Nam is the prefix of the type name. In either case, verify
422    --  that the package is visible at that point in the context: either  it
423    --  appears in a previous with_clause, or because it is a fully qualified
424    --  name and the root ancestor appears in a previous with_clause.
425
426    procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
427    --  Verify that the entity in a renaming declaration that is a library unit
428    --  is itself a library unit and not a nested unit or subunit. Also check
429    --  that if the renaming is a child unit of a generic parent, then the
430    --  renamed unit must also be a child unit of that parent. Finally, verify
431    --  that a renamed generic unit is not an implicit child declared within
432    --  an instance of the parent.
433
434    procedure Chain_Use_Clause (N : Node_Id);
435    --  Chain use clause onto list of uses clauses headed by First_Use_Clause in
436    --  the proper scope table entry. This is usually the current scope, but it
437    --  will be an inner scope when installing the use clauses of the private
438    --  declarations of a parent unit prior to compiling the private part of a
439    --  child unit. This chain is traversed when installing/removing use clauses
440    --  when compiling a subunit or instantiating a generic body on the fly,
441    --  when it is necessary to save and restore full environments.
442
443    function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
444    --  Find a type derived from Character or Wide_Character in the prefix of N.
445    --  Used to resolved qualified names whose selector is a character literal.
446
447    function Has_Private_With (E : Entity_Id) return Boolean;
448    --  Ada 2005 (AI-262): Determines if the current compilation unit has a
449    --  private with on E.
450
451    procedure Find_Expanded_Name (N : Node_Id);
452    --  Selected component is known to be expanded name. Verify legality of
453    --  selector given the scope denoted by prefix.
454
455    function Find_Renamed_Entity
456      (N         : Node_Id;
457       Nam       : Node_Id;
458       New_S     : Entity_Id;
459       Is_Actual : Boolean := False) return Entity_Id;
460    --  Find the renamed entity that corresponds to the given parameter profile
461    --  in a subprogram renaming declaration. The renamed entity may be an
462    --  operator, a subprogram, an entry, or a protected operation. Is_Actual
463    --  indicates that the renaming is the one generated for an actual subpro-
464    --  gram in an instance, for which special visibility checks apply.
465
466    function Has_Implicit_Operator (N : Node_Id) return Boolean;
467    --  N is an expanded name whose selector is an operator name (e.g. P."+").
468    --  declarative part contains an implicit declaration of an operator if it
469    --  has a declaration of a type to which one of the predefined operators
470    --  apply. The existence of this routine is an implementation artifact. A
471    --  more straightforward but more space-consuming choice would be to make
472    --  all inherited operators explicit in the symbol table.
473
474    procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
475    --  A subprogram defined by a renaming declaration inherits the parameter
476    --  profile of the renamed entity. The subtypes given in the subprogram
477    --  specification are discarded and replaced with those of the renamed
478    --  subprogram, which are then used to recheck the default values.
479
480    function Is_Appropriate_For_Record (T : Entity_Id) return Boolean;
481    --  Prefix is appropriate for record if it is of a record type, or an access
482    --  to such.
483
484    function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean;
485    --  True if it is of a task type, a protected type, or else an access to one
486    --  of these types.
487
488    procedure Note_Redundant_Use (Clause : Node_Id);
489    --  Mark the name in a use clause as redundant if the corresponding entity
490    --  is already use-visible. Emit a warning if the use clause comes from
491    --  source and the proper warnings are enabled.
492
493    procedure Premature_Usage (N : Node_Id);
494    --  Diagnose usage of an entity before it is visible
495
496    procedure Use_One_Package (P : Entity_Id; N : Node_Id);
497    --  Make visible entities declared in package P potentially use-visible
498    --  in the current context. Also used in the analysis of subunits, when
499    --  re-installing use clauses of parent units. N is the use_clause that
500    --  names P (and possibly other packages).
501
502    procedure Use_One_Type (Id : Node_Id);
503    --  Id is the subtype mark from a use type clause. This procedure makes
504    --  the primitive operators of the type potentially use-visible.
505
506    procedure Write_Info;
507    --  Write debugging information on entities declared in current scope
508
509    procedure Write_Scopes;
510    pragma Warnings (Off, Write_Scopes);
511    --  Debugging information: dump all entities on scope stack
512
513    --------------------------------
514    -- Analyze_Exception_Renaming --
515    --------------------------------
516
517    --  The language only allows a single identifier, but the tree holds an
518    --  identifier list. The parser has already issued an error message if
519    --  there is more than one element in the list.
520
521    procedure Analyze_Exception_Renaming (N : Node_Id) is
522       Id  : constant Node_Id := Defining_Identifier (N);
523       Nam : constant Node_Id := Name (N);
524
525    begin
526       Enter_Name (Id);
527       Analyze (Nam);
528
529       Set_Ekind          (Id, E_Exception);
530       Set_Exception_Code (Id, Uint_0);
531       Set_Etype          (Id, Standard_Exception_Type);
532       Set_Is_Pure        (Id, Is_Pure (Current_Scope));
533
534       if not Is_Entity_Name (Nam) or else
535         Ekind (Entity (Nam)) /= E_Exception
536       then
537          Error_Msg_N ("invalid exception name in renaming", Nam);
538       else
539          if Present (Renamed_Object (Entity (Nam))) then
540             Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
541          else
542             Set_Renamed_Object (Id, Entity (Nam));
543          end if;
544       end if;
545    end Analyze_Exception_Renaming;
546
547    ---------------------------
548    -- Analyze_Expanded_Name --
549    ---------------------------
550
551    procedure Analyze_Expanded_Name (N : Node_Id) is
552    begin
553       --  If the entity pointer is already set, this is an internal node, or a
554       --  node that is analyzed more than once, after a tree modification. In
555       --  such a case there is no resolution to perform, just set the type. For
556       --  completeness, analyze prefix as well.
557
558       if Present (Entity (N)) then
559          if Is_Type (Entity (N)) then
560             Set_Etype (N, Entity (N));
561          else
562             Set_Etype (N, Etype (Entity (N)));
563          end if;
564
565          Analyze (Prefix (N));
566          return;
567       else
568          Find_Expanded_Name (N);
569       end if;
570    end Analyze_Expanded_Name;
571
572    ---------------------------------------
573    -- Analyze_Generic_Function_Renaming --
574    ---------------------------------------
575
576    procedure Analyze_Generic_Function_Renaming  (N : Node_Id) is
577    begin
578       Analyze_Generic_Renaming (N, E_Generic_Function);
579    end Analyze_Generic_Function_Renaming;
580
581    --------------------------------------
582    -- Analyze_Generic_Package_Renaming --
583    --------------------------------------
584
585    procedure Analyze_Generic_Package_Renaming   (N : Node_Id) is
586    begin
587       --  Apply the Text_IO Kludge here, since we may be renaming one of the
588       --  subpackages of Text_IO, then join common routine.
589
590       Text_IO_Kludge (Name (N));
591
592       Analyze_Generic_Renaming (N, E_Generic_Package);
593    end Analyze_Generic_Package_Renaming;
594
595    ----------------------------------------
596    -- Analyze_Generic_Procedure_Renaming --
597    ----------------------------------------
598
599    procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
600    begin
601       Analyze_Generic_Renaming (N, E_Generic_Procedure);
602    end Analyze_Generic_Procedure_Renaming;
603
604    ------------------------------
605    -- Analyze_Generic_Renaming --
606    ------------------------------
607
608    procedure Analyze_Generic_Renaming
609      (N : Node_Id;
610       K : Entity_Kind)
611    is
612       New_P : constant Entity_Id := Defining_Entity (N);
613       Old_P : Entity_Id;
614       Inst  : Boolean   := False; -- prevent junk warning
615
616    begin
617       if Name (N) = Error then
618          return;
619       end if;
620
621       Generate_Definition (New_P);
622
623       if Current_Scope /= Standard_Standard then
624          Set_Is_Pure (New_P, Is_Pure (Current_Scope));
625       end if;
626
627       if Nkind (Name (N)) = N_Selected_Component then
628          Check_Generic_Child_Unit (Name (N), Inst);
629       else
630          Analyze (Name (N));
631       end if;
632
633       if not Is_Entity_Name (Name (N)) then
634          Error_Msg_N ("expect entity name in renaming declaration", Name (N));
635          Old_P := Any_Id;
636       else
637          Old_P := Entity (Name (N));
638       end if;
639
640       Enter_Name (New_P);
641       Set_Ekind (New_P, K);
642
643       if Etype (Old_P) = Any_Type then
644          null;
645
646       elsif Ekind (Old_P) /= K then
647          Error_Msg_N ("invalid generic unit name", Name (N));
648
649       else
650          if Present (Renamed_Object (Old_P)) then
651             Set_Renamed_Object (New_P,  Renamed_Object (Old_P));
652          else
653             Set_Renamed_Object (New_P, Old_P);
654          end if;
655
656          Set_Is_Pure          (New_P, Is_Pure          (Old_P));
657          Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
658
659          Set_Etype (New_P, Etype (Old_P));
660          Set_Has_Completion (New_P);
661
662          if In_Open_Scopes (Old_P) then
663             Error_Msg_N ("within its scope, generic denotes its instance", N);
664          end if;
665
666          Check_Library_Unit_Renaming (N, Old_P);
667       end if;
668    end Analyze_Generic_Renaming;
669
670    -----------------------------
671    -- Analyze_Object_Renaming --
672    -----------------------------
673
674    procedure Analyze_Object_Renaming (N : Node_Id) is
675       Id  : constant Entity_Id := Defining_Identifier (N);
676       Dec : Node_Id;
677       Nam : constant Node_Id   := Name (N);
678       T   : Entity_Id;
679       T2  : Entity_Id;
680
681       function In_Generic_Scope (E : Entity_Id) return Boolean;
682       --  Determine whether entity E is inside a generic cope
683
684       ----------------------
685       -- In_Generic_Scope --
686       ----------------------
687
688       function In_Generic_Scope (E : Entity_Id) return Boolean is
689          S : Entity_Id;
690
691       begin
692          S := Scope (E);
693          while Present (S) and then S /= Standard_Standard loop
694             if Is_Generic_Unit (S) then
695                return True;
696             end if;
697
698             S := Scope (S);
699          end loop;
700
701          return False;
702       end In_Generic_Scope;
703
704    --  Start of processing for Analyze_Object_Renaming
705
706    begin
707       if Nam = Error then
708          return;
709       end if;
710
711       Set_Is_Pure (Id, Is_Pure (Current_Scope));
712       Enter_Name (Id);
713
714       --  The renaming of a component that depends on a discriminant requires
715       --  an actual subtype, because in subsequent use of the object Gigi will
716       --  be unable to locate the actual bounds. This explicit step is required
717       --  when the renaming is generated in removing side effects of an
718       --  already-analyzed expression.
719
720       if Nkind (Nam) = N_Selected_Component
721         and then Analyzed (Nam)
722       then
723          T := Etype (Nam);
724          Dec :=  Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
725
726          if Present (Dec) then
727             Insert_Action (N, Dec);
728             T := Defining_Identifier (Dec);
729             Set_Etype (Nam, T);
730          end if;
731
732          --  Complete analysis of the subtype mark in any case, for ASIS use
733
734          if Present (Subtype_Mark (N)) then
735             Find_Type (Subtype_Mark (N));
736          end if;
737
738       elsif Present (Subtype_Mark (N)) then
739          Find_Type (Subtype_Mark (N));
740          T := Entity (Subtype_Mark (N));
741          Analyze (Nam);
742
743          if Nkind (Nam) = N_Type_Conversion
744             and then not Is_Tagged_Type (T)
745          then
746             Error_Msg_N
747               ("renaming of conversion only allowed for tagged types", Nam);
748          end if;
749
750          Resolve (Nam, T);
751
752          --  Check that a class-wide object is not being renamed as an object
753          --  of a specific type. The test for access types is needed to exclude
754          --  cases where the renamed object is a dynamically tagged access
755          --  result, such as occurs in certain expansions.
756
757          if (Is_Class_Wide_Type (Etype (Nam))
758               or else (Is_Dynamically_Tagged (Nam)
759                         and then not Is_Access_Type (T)))
760            and then not Is_Class_Wide_Type (T)
761          then
762             Error_Msg_N ("dynamically tagged expression not allowed!", Nam);
763          end if;
764
765       --  Ada 2005 (AI-230/AI-254): Access renaming
766
767       else pragma Assert (Present (Access_Definition (N)));
768          T := Access_Definition
769                 (Related_Nod => N,
770                  N           => Access_Definition (N));
771
772          Analyze (Nam);
773
774          --  Ada 2005 AI05-105: if the declaration has an anonymous access
775          --  type, the renamed object must also have an anonymous type, and
776          --  this is a name resolution rule. This was implicit in the last
777          --  part of the first sentence in 8.5.1.(3/2), and is made explicit
778          --  by this recent AI.
779
780          if not Is_Overloaded (Nam) then
781             if Ekind (Etype (Nam)) /= Ekind (T) then
782                Error_Msg_N
783                  ("expect anonymous access type in object renaming", N);
784             end if;
785
786          else
787             declare
788                I    : Interp_Index;
789                It   : Interp;
790                Typ  : Entity_Id := Empty;
791                Seen : Boolean   := False;
792
793             begin
794                Get_First_Interp (Nam, I, It);
795                while Present (It.Typ) loop
796
797                   --  Renaming is ambiguous if more than one candidate
798                   --  interpretation is type-conformant with the context.
799
800                   if Ekind (It.Typ) = Ekind (T) then
801                      if Ekind (T) = E_Anonymous_Access_Subprogram_Type
802                        and then
803                          Type_Conformant
804                            (Designated_Type (T), Designated_Type (It.Typ))
805                      then
806                         if not Seen then
807                            Seen := True;
808                         else
809                            Error_Msg_N
810                              ("ambiguous expression in renaming", Nam);
811                         end if;
812
813                      elsif Ekind (T) = E_Anonymous_Access_Type
814                        and then
815                          Covers (Designated_Type (T), Designated_Type (It.Typ))
816                      then
817                         if not Seen then
818                            Seen := True;
819                         else
820                            Error_Msg_N
821                              ("ambiguous expression in renaming", Nam);
822                         end if;
823                      end if;
824
825                      if Covers (T, It.Typ) then
826                         Typ := It.Typ;
827                         Set_Etype (Nam, Typ);
828                         Set_Is_Overloaded (Nam, False);
829                      end if;
830                   end if;
831
832                   Get_Next_Interp (I, It);
833                end loop;
834             end;
835          end if;
836
837          Resolve (Nam, T);
838
839          --  Ada 2005 (AI-231): "In the case where the type is defined by an
840          --  access_definition, the renamed entity shall be of an access-to-
841          --  constant type if and only if the access_definition defines an
842          --  access-to-constant type" ARM 8.5.1(4)
843
844          if Constant_Present (Access_Definition (N))
845            and then not Is_Access_Constant (Etype (Nam))
846          then
847             Error_Msg_N ("(Ada 2005): the renamed object is not "
848                          & "access-to-constant (RM 8.5.1(6))", N);
849
850          elsif not Constant_Present (Access_Definition (N))
851            and then Is_Access_Constant (Etype (Nam))
852          then
853             Error_Msg_N ("(Ada 2005): the renamed object is not "
854                          & "access-to-variable (RM 8.5.1(6))", N);
855          end if;
856
857          if Is_Access_Subprogram_Type (Etype (Nam)) then
858             Check_Subtype_Conformant
859               (Designated_Type (T), Designated_Type (Etype (Nam)));
860
861          elsif not Subtypes_Statically_Match
862                      (Designated_Type (T), Designated_Type (Etype (Nam)))
863          then
864             Error_Msg_N
865               ("subtype of renamed object does not statically match", N);
866          end if;
867       end if;
868
869       --  Special processing for renaming function return object. Some errors
870       --  and warnings are produced only for calls that come from source.
871
872       if Nkind (Nam) = N_Function_Call then
873          case Ada_Version is
874
875             --  Usage is illegal in Ada 83
876
877             when Ada_83 =>
878                if Comes_From_Source (Nam) then
879                   Error_Msg_N
880                     ("(Ada 83) cannot rename function return object", Nam);
881                end if;
882
883             --  In Ada 95, warn for odd case of renaming parameterless function
884             --  call if this is not a limited type (where this is useful).
885
886             when others =>
887                if Warn_On_Object_Renames_Function
888                  and then No (Parameter_Associations (Nam))
889                  and then not Is_Limited_Type (Etype (Nam))
890                  and then Comes_From_Source (Nam)
891                then
892                   Error_Msg_N
893                     ("?renaming function result object is suspicious", Nam);
894                   Error_Msg_NE
895                     ("\?function & will be called only once", Nam,
896                      Entity (Name (Nam)));
897                   Error_Msg_N
898                     ("\?suggest using an initialized constant object instead",
899                      Nam);
900                end if;
901
902                --  If the function call returns an unconstrained type, we must
903                --  build a constrained subtype for the new entity, in a way
904                --  similar to what is done for an object declaration with an
905                --  unconstrained nominal type.
906
907                if Is_Composite_Type (Etype (Nam))
908                  and then not Is_Constrained (Etype (Nam))
909                  and then not Has_Unknown_Discriminants (Etype (Nam))
910                  and then Expander_Active
911                then
912                   declare
913                      Loc  : constant Source_Ptr := Sloc (N);
914                      Subt : constant Entity_Id :=
915                               Make_Defining_Identifier (Loc,
916                                 Chars => New_Internal_Name ('T'));
917                   begin
918                      Remove_Side_Effects (Nam);
919                      Insert_Action (N,
920                        Make_Subtype_Declaration (Loc,
921                          Defining_Identifier => Subt,
922                          Subtype_Indication  =>
923                            Make_Subtype_From_Expr (Nam, Etype (Nam))));
924                      Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
925                      Set_Etype (Nam, Subt);
926                   end;
927                end if;
928          end case;
929       end if;
930
931       --  An object renaming requires an exact match of the type. Class-wide
932       --  matching is not allowed.
933
934       if Is_Class_Wide_Type (T)
935         and then Base_Type (Etype (Nam)) /= Base_Type (T)
936       then
937          Wrong_Type (Nam, T);
938       end if;
939
940       T2 := Etype (Nam);
941
942       --  (Ada 2005: AI-326): Handle wrong use of incomplete type
943
944       if Nkind (Nam) = N_Explicit_Dereference
945         and then Ekind (Etype (T2)) = E_Incomplete_Type
946       then
947          Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
948          return;
949
950       elsif Ekind (Etype (T)) = E_Incomplete_Type then
951          Error_Msg_NE ("invalid use of incomplete type&", Id, T);
952          return;
953       end if;
954
955       --  Ada 2005 (AI-327)
956
957       if Ada_Version >= Ada_05
958         and then Nkind (Nam) = N_Attribute_Reference
959         and then Attribute_Name (Nam) = Name_Priority
960       then
961          null;
962
963       elsif Ada_Version >= Ada_05
964         and then Nkind (Nam) in N_Has_Entity
965       then
966          declare
967             Nam_Decl : Node_Id;
968             Nam_Ent  : Entity_Id;
969
970          begin
971             if Nkind (Nam) = N_Attribute_Reference then
972                Nam_Ent := Entity (Prefix (Nam));
973             else
974                Nam_Ent := Entity (Nam);
975             end if;
976
977             Nam_Decl := Parent (Nam_Ent);
978
979             if Has_Null_Exclusion (N)
980               and then not Has_Null_Exclusion (Nam_Decl)
981             then
982                --  Ada 2005 (AI-423): If the object name denotes a generic
983                --  formal object of a generic unit G, and the object renaming
984                --  declaration occurs within the body of G or within the body
985                --  of a generic unit declared within the declarative region
986                --  of G, then the declaration of the formal object of G must
987                --  have a null exclusion or a null-excluding subtype.
988
989                if Is_Formal_Object (Nam_Ent)
990                     and then In_Generic_Scope (Id)
991                then
992                   if not Can_Never_Be_Null (Etype (Nam_Ent)) then
993                      Error_Msg_N
994                        ("renamed formal does not exclude `NULL` "
995                         & "(RM 8.5.1(4.6/2))", N);
996
997                   elsif In_Package_Body (Scope (Id)) then
998                      Error_Msg_N
999                        ("formal object does not have a null exclusion"
1000                         & "(RM 8.5.1(4.6/2))", N);
1001                   end if;
1002
1003                --  Ada 2005 (AI-423): Otherwise, the subtype of the object name
1004                --  shall exclude null.
1005
1006                elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1007                   Error_Msg_N
1008                     ("renamed object does not exclude `NULL` "
1009                      & "(RM 8.5.1(4.6/2))", N);
1010
1011                --  An instance is illegal if it contains a renaming that
1012                --  excludes null, and the actual does not. The renaming
1013                --  declaration has already indicated that the declaration
1014                --  of the renamed actual in the instance will raise
1015                --  constraint_error.
1016
1017                elsif Nkind (Nam_Decl) = N_Object_Declaration
1018                  and then In_Instance
1019                  and then Present
1020                    (Corresponding_Generic_Association (Nam_Decl))
1021                  and then Nkind (Expression (Nam_Decl))
1022                    = N_Raise_Constraint_Error
1023                then
1024                   Error_Msg_N
1025                     ("renamed actual does not exclude `NULL` "
1026                      & "(RM 8.5.1(4.6/2))", N);
1027
1028                --  Finally, if there is a null exclusion, the subtype mark
1029                --  must not be null-excluding.
1030
1031                elsif No (Access_Definition (N))
1032                  and then Can_Never_Be_Null (T)
1033                then
1034                   Error_Msg_NE
1035                     ("`NOT NULL` not allowed (& already excludes null)",
1036                       N, T);
1037
1038                end if;
1039
1040             elsif Can_Never_Be_Null (T)
1041               and then not Can_Never_Be_Null (Etype (Nam_Ent))
1042             then
1043                Error_Msg_N
1044                  ("renamed object does not exclude `NULL` "
1045                   & "(RM 8.5.1(4.6/2))", N);
1046
1047             elsif Has_Null_Exclusion (N)
1048               and then No (Access_Definition (N))
1049               and then Can_Never_Be_Null (T)
1050             then
1051                Error_Msg_NE
1052                  ("`NOT NULL` not allowed (& already excludes null)", N, T);
1053             end if;
1054          end;
1055       end if;
1056
1057       Set_Ekind (Id, E_Variable);
1058       Init_Size_Align (Id);
1059
1060       if T = Any_Type or else Etype (Nam) = Any_Type then
1061          return;
1062
1063       --  Verify that the renamed entity is an object or a function call. It
1064       --  may have been rewritten in several ways.
1065
1066       elsif Is_Object_Reference (Nam) then
1067          if Comes_From_Source (N)
1068            and then Is_Dependent_Component_Of_Mutable_Object (Nam)
1069          then
1070             Error_Msg_N
1071               ("illegal renaming of discriminant-dependent component", Nam);
1072          end if;
1073
1074       --  A static function call may have been folded into a literal
1075
1076       elsif Nkind (Original_Node (Nam)) = N_Function_Call
1077
1078             --  When expansion is disabled, attribute reference is not
1079             --  rewritten as function call. Otherwise it may be rewritten
1080             --  as a conversion, so check original node.
1081
1082         or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference
1083                   and then Is_Function_Attribute_Name
1084                              (Attribute_Name (Original_Node (Nam))))
1085
1086             --  Weird but legal, equivalent to renaming a function call.
1087             --  Illegal if the literal is the result of constant-folding an
1088             --  attribute reference that is not a function.
1089
1090         or else (Is_Entity_Name (Nam)
1091                   and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1092                   and then
1093                     Nkind (Original_Node (Nam)) /= N_Attribute_Reference)
1094
1095         or else (Nkind (Nam) = N_Type_Conversion
1096                     and then Is_Tagged_Type (Entity (Subtype_Mark (Nam))))
1097       then
1098          null;
1099
1100       elsif Nkind (Nam) = N_Type_Conversion then
1101          Error_Msg_N
1102            ("renaming of conversion only allowed for tagged types", Nam);
1103
1104       --  Ada 2005 (AI-327)
1105
1106       elsif Ada_Version >= Ada_05
1107         and then Nkind (Nam) = N_Attribute_Reference
1108         and then Attribute_Name (Nam) = Name_Priority
1109       then
1110          null;
1111
1112       --  Allow internally generated x'Reference expression
1113
1114       elsif Nkind (Nam) = N_Reference then
1115          null;
1116
1117       else
1118          Error_Msg_N ("expect object name in renaming", Nam);
1119       end if;
1120
1121       Set_Etype (Id, T2);
1122
1123       if not Is_Variable (Nam) then
1124          Set_Ekind               (Id, E_Constant);
1125          Set_Never_Set_In_Source (Id, True);
1126          Set_Is_True_Constant    (Id, True);
1127       end if;
1128
1129       Set_Renamed_Object (Id, Nam);
1130    end Analyze_Object_Renaming;
1131
1132    ------------------------------
1133    -- Analyze_Package_Renaming --
1134    ------------------------------
1135
1136    procedure Analyze_Package_Renaming (N : Node_Id) is
1137       New_P : constant Entity_Id := Defining_Entity (N);
1138       Old_P : Entity_Id;
1139       Spec  : Node_Id;
1140
1141    begin
1142       if Name (N) = Error then
1143          return;
1144       end if;
1145
1146       --  Apply Text_IO kludge here since we may be renaming a child of Text_IO
1147
1148       Text_IO_Kludge (Name (N));
1149
1150       if Current_Scope /= Standard_Standard then
1151          Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1152       end if;
1153
1154       Enter_Name (New_P);
1155       Analyze (Name (N));
1156
1157       if Is_Entity_Name (Name (N)) then
1158          Old_P := Entity (Name (N));
1159       else
1160          Old_P := Any_Id;
1161       end if;
1162
1163       if Etype (Old_P) = Any_Type then
1164          Error_Msg_N ("expect package name in renaming", Name (N));
1165
1166       elsif Ekind (Old_P) /= E_Package
1167         and then not (Ekind (Old_P) = E_Generic_Package
1168                        and then In_Open_Scopes (Old_P))
1169       then
1170          if Ekind (Old_P) = E_Generic_Package then
1171             Error_Msg_N
1172                ("generic package cannot be renamed as a package", Name (N));
1173          else
1174             Error_Msg_Sloc := Sloc (Old_P);
1175             Error_Msg_NE
1176              ("expect package name in renaming, found& declared#",
1177                Name (N), Old_P);
1178          end if;
1179
1180          --  Set basic attributes to minimize cascaded errors
1181
1182          Set_Ekind (New_P, E_Package);
1183          Set_Etype (New_P, Standard_Void_Type);
1184
1185       --  Here for OK package renaming
1186
1187       else
1188          --  Entities in the old package are accessible through the renaming
1189          --  entity. The simplest implementation is to have both packages share
1190          --  the entity list.
1191
1192          Set_Ekind (New_P, E_Package);
1193          Set_Etype (New_P, Standard_Void_Type);
1194
1195          if Present (Renamed_Object (Old_P)) then
1196             Set_Renamed_Object (New_P,  Renamed_Object (Old_P));
1197          else
1198             Set_Renamed_Object (New_P, Old_P);
1199          end if;
1200
1201          Set_Has_Completion (New_P);
1202
1203          Set_First_Entity (New_P,  First_Entity (Old_P));
1204          Set_Last_Entity  (New_P,  Last_Entity  (Old_P));
1205          Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1206          Check_Library_Unit_Renaming (N, Old_P);
1207          Generate_Reference (Old_P, Name (N));
1208
1209          --  If the renaming is in the visible part of a package, then we set
1210          --  Renamed_In_Spec for the renamed package, to prevent giving
1211          --  warnings about no entities referenced. Such a warning would be
1212          --  overenthusiastic, since clients can see entities in the renamed
1213          --  package via the visible package renaming.
1214
1215          declare
1216             Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1217          begin
1218             if Ekind (Ent) = E_Package
1219               and then not In_Private_Part (Ent)
1220               and then In_Extended_Main_Source_Unit (N)
1221               and then Ekind (Old_P) = E_Package
1222             then
1223                Set_Renamed_In_Spec (Old_P);
1224             end if;
1225          end;
1226
1227          --  If this is the renaming declaration of a package instantiation
1228          --  within itself, it is the declaration that ends the list of actuals
1229          --  for the instantiation. At this point, the subtypes that rename
1230          --  the actuals are flagged as generic, to avoid spurious ambiguities
1231          --  if the actuals for two distinct formals happen to coincide. If
1232          --  the actual is a private type, the subtype has a private completion
1233          --  that is flagged in the same fashion.
1234
1235          --  Resolution is identical to what is was in the original generic.
1236          --  On exit from the generic instance, these are turned into regular
1237          --  subtypes again, so they are compatible with types in their class.
1238
1239          if not Is_Generic_Instance (Old_P) then
1240             return;
1241          else
1242             Spec := Specification (Unit_Declaration_Node (Old_P));
1243          end if;
1244
1245          if Nkind (Spec) = N_Package_Specification
1246            and then Present (Generic_Parent (Spec))
1247            and then Old_P = Current_Scope
1248            and then Chars (New_P) = Chars (Generic_Parent (Spec))
1249          then
1250             declare
1251                E : Entity_Id;
1252
1253             begin
1254                E := First_Entity (Old_P);
1255                while Present (E)
1256                  and then E /= New_P
1257                loop
1258                   if Is_Type (E)
1259                     and then Nkind (Parent (E)) = N_Subtype_Declaration
1260                   then
1261                      Set_Is_Generic_Actual_Type (E);
1262
1263                      if Is_Private_Type (E)
1264                        and then Present (Full_View (E))
1265                      then
1266                         Set_Is_Generic_Actual_Type (Full_View (E));
1267                      end if;
1268                   end if;
1269
1270                   Next_Entity (E);
1271                end loop;
1272             end;
1273          end if;
1274       end if;
1275    end Analyze_Package_Renaming;
1276
1277    -------------------------------
1278    -- Analyze_Renamed_Character --
1279    -------------------------------
1280
1281    procedure Analyze_Renamed_Character
1282      (N       : Node_Id;
1283       New_S   : Entity_Id;
1284       Is_Body : Boolean)
1285    is
1286       C : constant Node_Id := Name (N);
1287
1288    begin
1289       if Ekind (New_S) = E_Function then
1290          Resolve (C, Etype (New_S));
1291
1292          if Is_Body then
1293             Check_Frozen_Renaming (N, New_S);
1294          end if;
1295
1296       else
1297          Error_Msg_N ("character literal can only be renamed as function", N);
1298       end if;
1299    end Analyze_Renamed_Character;
1300
1301    ---------------------------------
1302    -- Analyze_Renamed_Dereference --
1303    ---------------------------------
1304
1305    procedure Analyze_Renamed_Dereference
1306      (N       : Node_Id;
1307       New_S   : Entity_Id;
1308       Is_Body : Boolean)
1309    is
1310       Nam : constant Node_Id := Name (N);
1311       P   : constant Node_Id := Prefix (Nam);
1312       Typ : Entity_Id;
1313       Ind : Interp_Index;
1314       It  : Interp;
1315
1316    begin
1317       if not Is_Overloaded (P) then
1318          if Ekind (Etype (Nam)) /= E_Subprogram_Type
1319            or else not Type_Conformant (Etype (Nam), New_S) then
1320             Error_Msg_N ("designated type does not match specification", P);
1321          else
1322             Resolve (P);
1323          end if;
1324
1325          return;
1326
1327       else
1328          Typ := Any_Type;
1329          Get_First_Interp (Nam, Ind, It);
1330
1331          while Present (It.Nam) loop
1332
1333             if Ekind (It.Nam) = E_Subprogram_Type
1334               and then Type_Conformant (It.Nam, New_S) then
1335
1336                if Typ /= Any_Id then
1337                   Error_Msg_N ("ambiguous renaming", P);
1338                   return;
1339                else
1340                   Typ := It.Nam;
1341                end if;
1342             end if;
1343
1344             Get_Next_Interp (Ind, It);
1345          end loop;
1346
1347          if Typ = Any_Type then
1348             Error_Msg_N ("designated type does not match specification", P);
1349          else
1350             Resolve (N, Typ);
1351
1352             if Is_Body then
1353                Check_Frozen_Renaming (N, New_S);
1354             end if;
1355          end if;
1356       end if;
1357    end Analyze_Renamed_Dereference;
1358
1359    ---------------------------
1360    -- Analyze_Renamed_Entry --
1361    ---------------------------
1362
1363    procedure Analyze_Renamed_Entry
1364      (N       : Node_Id;
1365       New_S   : Entity_Id;
1366       Is_Body : Boolean)
1367    is
1368       Nam   : constant Node_Id := Name (N);
1369       Sel   : constant Node_Id := Selector_Name (Nam);
1370       Old_S : Entity_Id;
1371
1372    begin
1373       if Entity (Sel) = Any_Id then
1374
1375          --  Selector is undefined on prefix. Error emitted already
1376
1377          Set_Has_Completion (New_S);
1378          return;
1379       end if;
1380
1381       --  Otherwise find renamed entity and build body of New_S as a call to it
1382
1383       Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1384
1385       if Old_S = Any_Id then
1386          Error_Msg_N (" no subprogram or entry matches specification",  N);
1387       else
1388          if Is_Body then
1389             Check_Subtype_Conformant (New_S, Old_S, N);
1390             Generate_Reference (New_S, Defining_Entity (N), 'b');
1391             Style.Check_Identifier (Defining_Entity (N), New_S);
1392
1393          else
1394             --  Only mode conformance required for a renaming_as_declaration
1395
1396             Check_Mode_Conformant (New_S, Old_S, N);
1397          end if;
1398
1399          Inherit_Renamed_Profile (New_S, Old_S);
1400
1401          --  The prefix can be an arbitrary expression that yields a task type,
1402          --  so it must be resolved.
1403
1404          Resolve (Prefix (Nam), Scope (Old_S));
1405       end if;
1406
1407       Set_Convention (New_S, Convention (Old_S));
1408       Set_Has_Completion (New_S, Inside_A_Generic);
1409
1410       if Is_Body then
1411          Check_Frozen_Renaming (N, New_S);
1412       end if;
1413    end Analyze_Renamed_Entry;
1414
1415    -----------------------------------
1416    -- Analyze_Renamed_Family_Member --
1417    -----------------------------------
1418
1419    procedure Analyze_Renamed_Family_Member
1420      (N       : Node_Id;
1421       New_S   : Entity_Id;
1422       Is_Body : Boolean)
1423    is
1424       Nam   : constant Node_Id := Name (N);
1425       P     : constant Node_Id := Prefix (Nam);
1426       Old_S : Entity_Id;
1427
1428    begin
1429       if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1430         or else (Nkind (P) = N_Selected_Component
1431                    and then
1432                  Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1433       then
1434          if Is_Entity_Name (P) then
1435             Old_S := Entity (P);
1436          else
1437             Old_S := Entity (Selector_Name (P));
1438          end if;
1439
1440          if not Entity_Matches_Spec (Old_S, New_S) then
1441             Error_Msg_N ("entry family does not match specification", N);
1442
1443          elsif Is_Body then
1444             Check_Subtype_Conformant (New_S, Old_S, N);
1445             Generate_Reference (New_S, Defining_Entity (N), 'b');
1446             Style.Check_Identifier (Defining_Entity (N), New_S);
1447          end if;
1448
1449       else
1450          Error_Msg_N ("no entry family matches specification", N);
1451       end if;
1452
1453       Set_Has_Completion (New_S, Inside_A_Generic);
1454
1455       if Is_Body then
1456          Check_Frozen_Renaming (N, New_S);
1457       end if;
1458    end Analyze_Renamed_Family_Member;
1459
1460    -----------------------------------------
1461    -- Analyze_Renamed_Primitive_Operation --
1462    -----------------------------------------
1463
1464    procedure Analyze_Renamed_Primitive_Operation
1465      (N       : Node_Id;
1466       New_S   : Entity_Id;
1467       Is_Body : Boolean)
1468    is
1469       Old_S : Entity_Id;
1470
1471       function Conforms
1472         (Subp : Entity_Id;
1473          Ctyp : Conformance_Type) return Boolean;
1474       --  Verify that the signatures of the renamed entity and the new entity
1475       --  match. The first formal of the renamed entity is skipped because it
1476       --  is the target object in any subsequent call.
1477
1478       function Conforms
1479         (Subp : Entity_Id;
1480          Ctyp : Conformance_Type) return Boolean
1481       is
1482          Old_F : Entity_Id;
1483          New_F : Entity_Id;
1484
1485       begin
1486          if Ekind (Subp) /= Ekind (New_S) then
1487             return False;
1488          end if;
1489
1490          Old_F := Next_Formal (First_Formal (Subp));
1491          New_F := First_Formal (New_S);
1492          while Present (Old_F) and then Present (New_F) loop
1493             if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
1494                return False;
1495             end if;
1496
1497             if Ctyp >= Mode_Conformant
1498               and then Ekind (Old_F) /= Ekind (New_F)
1499             then
1500                return False;
1501             end if;
1502
1503             Next_Formal (New_F);
1504             Next_Formal (Old_F);
1505          end loop;
1506
1507          return True;
1508       end Conforms;
1509
1510    begin
1511       if not Is_Overloaded (Selector_Name (Name (N))) then
1512          Old_S := Entity (Selector_Name (Name (N)));
1513
1514          if not Conforms (Old_S, Type_Conformant) then
1515             Old_S := Any_Id;
1516          end if;
1517
1518       else
1519          --  Find the operation that matches the given signature
1520
1521          declare
1522             It  : Interp;
1523             Ind : Interp_Index;
1524
1525          begin
1526             Old_S := Any_Id;
1527             Get_First_Interp (Selector_Name (Name (N)), Ind, It);
1528
1529             while Present (It.Nam) loop
1530                if Conforms (It.Nam, Type_Conformant) then
1531                   Old_S := It.Nam;
1532                end if;
1533
1534                Get_Next_Interp (Ind, It);
1535             end loop;
1536          end;
1537       end if;
1538
1539       if Old_S = Any_Id then
1540          Error_Msg_N (" no subprogram or entry matches specification",  N);
1541
1542       else
1543          if Is_Body then
1544             if not Conforms (Old_S, Subtype_Conformant) then
1545                Error_Msg_N ("subtype conformance error in renaming", N);
1546             end if;
1547
1548             Generate_Reference (New_S, Defining_Entity (N), 'b');
1549             Style.Check_Identifier (Defining_Entity (N), New_S);
1550
1551          else
1552             --  Only mode conformance required for a renaming_as_declaration
1553
1554             if not Conforms (Old_S, Mode_Conformant) then
1555                Error_Msg_N ("mode conformance error in renaming", N);
1556             end if;
1557          end if;
1558
1559          --  Inherit_Renamed_Profile (New_S, Old_S);
1560
1561          --  The prefix can be an arbitrary expression that yields an
1562          --  object, so it must be resolved.
1563
1564          Resolve (Prefix (Name (N)));
1565       end if;
1566    end Analyze_Renamed_Primitive_Operation;
1567
1568    ---------------------------------
1569    -- Analyze_Subprogram_Renaming --
1570    ---------------------------------
1571
1572    procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1573       Formal_Spec : constant Node_Id          := Corresponding_Formal_Spec (N);
1574       Is_Actual   : constant Boolean          := Present (Formal_Spec);
1575       Inst_Node   : Node_Id                   := Empty;
1576       Nam         : constant Node_Id          := Name (N);
1577       New_S       : Entity_Id;
1578       Old_S       : Entity_Id                 := Empty;
1579       Rename_Spec : Entity_Id;
1580       Save_AV     : constant Ada_Version_Type := Ada_Version;
1581       Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
1582       Spec        : constant Node_Id          := Specification (N);
1583
1584       procedure Check_Null_Exclusion
1585         (Ren : Entity_Id;
1586          Sub : Entity_Id);
1587       --  Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
1588       --  following AI rules:
1589       --
1590       --    If Ren is a renaming of a formal subprogram and one of its
1591       --    parameters has a null exclusion, then the corresponding formal
1592       --    in Sub must also have one. Otherwise the subtype of the Sub's
1593       --    formal parameter must exclude null.
1594       --
1595       --    If Ren is a renaming of a formal function and its return
1596       --    profile has a null exclusion, then Sub's return profile must
1597       --    have one. Otherwise the subtype of Sub's return profile must
1598       --    exclude null.
1599
1600       function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
1601       --  Find renamed entity when the declaration is a renaming_as_body and
1602       --  the renamed entity may itself be a renaming_as_body. Used to enforce
1603       --  rule that a renaming_as_body is illegal if the declaration occurs
1604       --  before the subprogram it completes is frozen, and renaming indirectly
1605       --  renames the subprogram itself.(Defect Report 8652/0027).
1606
1607       --------------------------
1608       -- Check_Null_Exclusion --
1609       --------------------------
1610
1611       procedure Check_Null_Exclusion
1612         (Ren : Entity_Id;
1613          Sub : Entity_Id)
1614       is
1615          Ren_Formal : Entity_Id;
1616          Sub_Formal : Entity_Id;
1617
1618       begin
1619          --  Parameter check
1620
1621          Ren_Formal := First_Formal (Ren);
1622          Sub_Formal := First_Formal (Sub);
1623          while Present (Ren_Formal)
1624            and then Present (Sub_Formal)
1625          loop
1626             if Has_Null_Exclusion (Parent (Ren_Formal))
1627               and then
1628                 not (Has_Null_Exclusion (Parent (Sub_Formal))
1629                        or else Can_Never_Be_Null (Etype (Sub_Formal)))
1630             then
1631                Error_Msg_NE
1632                  ("`NOT NULL` required for parameter &",
1633                   Parent (Sub_Formal), Sub_Formal);
1634             end if;
1635
1636             Next_Formal (Ren_Formal);
1637             Next_Formal (Sub_Formal);
1638          end loop;
1639
1640          --  Return profile check
1641
1642          if Nkind (Parent (Ren)) = N_Function_Specification
1643            and then Nkind (Parent (Sub)) = N_Function_Specification
1644            and then Has_Null_Exclusion (Parent (Ren))
1645            and then
1646              not (Has_Null_Exclusion (Parent (Sub))
1647                     or else Can_Never_Be_Null (Etype (Sub)))
1648          then
1649             Error_Msg_N
1650               ("return must specify `NOT NULL`",
1651                Result_Definition (Parent (Sub)));
1652          end if;
1653       end Check_Null_Exclusion;
1654
1655       -------------------------
1656       -- Original_Subprogram --
1657       -------------------------
1658
1659       function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
1660          Orig_Decl : Node_Id;
1661          Orig_Subp : Entity_Id;
1662
1663       begin
1664          --  First case: renamed entity is itself a renaming
1665
1666          if Present (Alias (Subp)) then
1667             return Alias (Subp);
1668
1669          elsif
1670            Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1671              and then Present
1672               (Corresponding_Body (Unit_Declaration_Node (Subp)))
1673          then
1674             --  Check if renamed entity is a renaming_as_body
1675
1676             Orig_Decl :=
1677               Unit_Declaration_Node
1678                 (Corresponding_Body (Unit_Declaration_Node (Subp)));
1679
1680             if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
1681                Orig_Subp := Entity (Name (Orig_Decl));
1682
1683                if Orig_Subp = Rename_Spec then
1684
1685                   --  Circularity detected
1686
1687                   return Orig_Subp;
1688
1689                else
1690                   return (Original_Subprogram (Orig_Subp));
1691                end if;
1692             else
1693                return Subp;
1694             end if;
1695          else
1696             return Subp;
1697          end if;
1698       end Original_Subprogram;
1699
1700    --  Start of processing for Analyze_Subprogram_Renaming
1701
1702    begin
1703       --  We must test for the attribute renaming case before the Analyze
1704       --  call because otherwise Sem_Attr will complain that the attribute
1705       --  is missing an argument when it is analyzed.
1706
1707       if Nkind (Nam) = N_Attribute_Reference then
1708
1709          --  In the case of an abstract formal subprogram association, rewrite
1710          --  an actual given by a stream attribute as the name of the
1711          --  corresponding stream primitive of the type.
1712
1713          --  In a generic context the stream operations are not generated, and
1714          --  this must be treated as a normal attribute reference, to be
1715          --  expanded in subsequent instantiations.
1716
1717          if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec)
1718            and then Expander_Active
1719          then
1720             declare
1721                Stream_Prim : Entity_Id;
1722                Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
1723
1724             begin
1725                --  The class-wide forms of the stream attributes are not
1726                --  primitive dispatching operations (even though they
1727                --  internally dispatch to a stream attribute).
1728
1729                if Is_Class_Wide_Type (Prefix_Type) then
1730                   Error_Msg_N
1731                     ("attribute must be a primitive dispatching operation",
1732                      Nam);
1733                   return;
1734                end if;
1735
1736                --  Retrieve the primitive subprogram associated with the
1737                --  attribute. This can only be a stream attribute, since those
1738                --  are the only ones that are dispatching (and the actual for
1739                --  an abstract formal subprogram must be dispatching
1740                --  operation).
1741
1742                begin
1743                   case Attribute_Name (Nam) is
1744                      when Name_Input  =>
1745                         Stream_Prim :=
1746                           Find_Prim_Op (Prefix_Type, TSS_Stream_Input);
1747                      when Name_Output =>
1748                         Stream_Prim :=
1749                           Find_Prim_Op (Prefix_Type, TSS_Stream_Output);
1750                      when Name_Read   =>
1751                         Stream_Prim :=
1752                           Find_Prim_Op (Prefix_Type, TSS_Stream_Read);
1753                      when Name_Write  =>
1754                         Stream_Prim :=
1755                           Find_Prim_Op (Prefix_Type, TSS_Stream_Write);
1756                      when others      =>
1757                         Error_Msg_N
1758                           ("attribute must be a primitive"
1759                             & " dispatching operation", Nam);
1760                         return;
1761                   end case;
1762
1763                exception
1764
1765                   --  If no operation was found, and the type is limited,
1766                   --  the user should have defined one.
1767
1768                   when Program_Error =>
1769                      if Is_Limited_Type (Prefix_Type) then
1770                         Error_Msg_NE
1771                          ("stream operation not defined for type&",
1772                            N, Prefix_Type);
1773                         return;
1774
1775                      --  Otherwise, compiler should have generated default
1776
1777                      else
1778                         raise;
1779                      end if;
1780                end;
1781
1782                --  Rewrite the attribute into the name of its corresponding
1783                --  primitive dispatching subprogram. We can then proceed with
1784                --  the usual processing for subprogram renamings.
1785
1786                declare
1787                   Prim_Name : constant Node_Id :=
1788                                 Make_Identifier (Sloc (Nam),
1789                                   Chars => Chars (Stream_Prim));
1790                begin
1791                   Set_Entity (Prim_Name, Stream_Prim);
1792                   Rewrite (Nam, Prim_Name);
1793                   Analyze (Nam);
1794                end;
1795             end;
1796
1797          --  Normal processing for a renaming of an attribute
1798
1799          else
1800             Attribute_Renaming (N);
1801             return;
1802          end if;
1803       end if;
1804
1805       --  Check whether this declaration corresponds to the instantiation
1806       --  of a formal subprogram.
1807
1808       --  If this is an instantiation, the corresponding actual is frozen and
1809       --  error messages can be made more precise. If this is a default
1810       --  subprogram, the entity is already established in the generic, and is
1811       --  not retrieved by visibility. If it is a default with a box, the
1812       --  candidate interpretations, if any, have been collected when building
1813       --  the renaming declaration. If overloaded, the proper interpretation is
1814       --  determined in Find_Renamed_Entity. If the entity is an operator,
1815       --  Find_Renamed_Entity applies additional visibility checks.
1816
1817       if Is_Actual then
1818          Inst_Node := Unit_Declaration_Node (Formal_Spec);
1819
1820          if Is_Entity_Name (Nam)
1821            and then Present (Entity (Nam))
1822            and then not Comes_From_Source (Nam)
1823            and then not Is_Overloaded (Nam)
1824          then
1825             Old_S := Entity (Nam);
1826             New_S := Analyze_Subprogram_Specification (Spec);
1827
1828             --  Operator case
1829
1830             if Ekind (Entity (Nam)) = E_Operator then
1831
1832                --  Box present
1833
1834                if Box_Present (Inst_Node) then
1835                   Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
1836
1837                --  If there is an immediately visible homonym of the operator
1838                --  and the declaration has a default, this is worth a warning
1839                --  because the user probably did not intend to get the pre-
1840                --  defined operator, visible in the generic declaration. To
1841                --  find if there is an intended candidate, analyze the renaming
1842                --  again in the current context.
1843
1844                elsif Scope (Old_S) = Standard_Standard
1845                  and then Present (Default_Name (Inst_Node))
1846                then
1847                   declare
1848                      Decl   : constant Node_Id := New_Copy_Tree (N);
1849                      Hidden : Entity_Id;
1850
1851                   begin
1852                      Set_Entity (Name (Decl), Empty);
1853                      Analyze (Name (Decl));
1854                      Hidden :=
1855                        Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
1856
1857                      if Present (Hidden)
1858                        and then In_Open_Scopes (Scope (Hidden))
1859                        and then Is_Immediately_Visible (Hidden)
1860                        and then Comes_From_Source (Hidden)
1861                        and then Hidden /= Old_S
1862                      then
1863                         Error_Msg_Sloc := Sloc (Hidden);
1864                         Error_Msg_N ("?default subprogram is resolved " &
1865                                      "in the generic declaration " &
1866                                      "(RM 12.6(17))", N);
1867                         Error_Msg_NE ("\?and will not use & #", N, Hidden);
1868                      end if;
1869                   end;
1870                end if;
1871             end if;
1872
1873          else
1874             Analyze (Nam);
1875             New_S := Analyze_Subprogram_Specification (Spec);
1876          end if;
1877
1878       else
1879          --  Renamed entity must be analyzed first, to avoid being hidden by
1880          --  new name (which might be the same in a generic instance).
1881
1882          Analyze (Nam);
1883
1884          --  The renaming defines a new overloaded entity, which is analyzed
1885          --  like a subprogram declaration.
1886
1887          New_S := Analyze_Subprogram_Specification (Spec);
1888       end if;
1889
1890       if Current_Scope /= Standard_Standard then
1891          Set_Is_Pure (New_S, Is_Pure (Current_Scope));
1892       end if;
1893
1894       Rename_Spec := Find_Corresponding_Spec (N);
1895
1896       --  Case of Renaming_As_Body
1897
1898       if Present (Rename_Spec) then
1899
1900          --  Renaming declaration is the completion of the declaration of
1901          --  Rename_Spec. We build an actual body for it at the freezing point.
1902
1903          Set_Corresponding_Spec (N, Rename_Spec);
1904
1905          --  Deal with special case of stream functions of abstract types
1906          --  and interfaces.
1907
1908          if Nkind (Unit_Declaration_Node (Rename_Spec)) =
1909                                      N_Abstract_Subprogram_Declaration
1910          then
1911             --  Input stream functions are abstract if the object type is
1912             --  abstract. Similarly, all default stream functions for an
1913             --  interface type are abstract. However, these subprograms may
1914             --  receive explicit declarations in representation clauses, making
1915             --  the attribute subprograms usable as defaults in subsequent
1916             --  type extensions.
1917             --  In this case we rewrite the declaration to make the subprogram
1918             --  non-abstract. We remove the previous declaration, and insert
1919             --  the new one at the point of the renaming, to prevent premature
1920             --  access to unfrozen types. The new declaration reuses the
1921             --  specification of the previous one, and must not be analyzed.
1922
1923             pragma Assert
1924               (Is_Primitive (Entity (Nam))
1925                  and then
1926                    Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
1927             declare
1928                Old_Decl : constant Node_Id :=
1929                             Unit_Declaration_Node (Rename_Spec);
1930                New_Decl : constant Node_Id :=
1931                             Make_Subprogram_Declaration (Sloc (N),
1932                               Specification =>
1933                                 Relocate_Node (Specification (Old_Decl)));
1934             begin
1935                Remove (Old_Decl);
1936                Insert_After (N, New_Decl);
1937                Set_Is_Abstract_Subprogram (Rename_Spec, False);
1938                Set_Analyzed (New_Decl);
1939             end;
1940          end if;
1941
1942          Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
1943
1944          if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1945             Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
1946          end if;
1947
1948          Set_Convention (New_S, Convention (Rename_Spec));
1949          Check_Fully_Conformant (New_S, Rename_Spec);
1950          Set_Public_Status (New_S);
1951
1952          --  The specification does not introduce new formals, but only
1953          --  repeats the formals of the original subprogram declaration.
1954          --  For cross-reference purposes, and for refactoring tools, we
1955          --  treat the formals of the renaming declaration as body formals.
1956
1957          Reference_Body_Formals (Rename_Spec, New_S);
1958
1959          --  Indicate that the entity in the declaration functions like the
1960          --  corresponding body, and is not a new entity. The body will be
1961          --  constructed later at the freeze point, so indicate that the
1962          --  completion has not been seen yet.
1963
1964          Set_Ekind (New_S, E_Subprogram_Body);
1965          New_S := Rename_Spec;
1966          Set_Has_Completion (Rename_Spec, False);
1967
1968          --  Ada 2005: check overriding indicator
1969
1970          if Is_Overriding_Operation (Rename_Spec) then
1971             if Must_Not_Override (Specification (N)) then
1972                Error_Msg_NE
1973                  ("subprogram& overrides inherited operation",
1974                     N, Rename_Spec);
1975             elsif
1976               Style_Check and then not Must_Override (Specification (N))
1977             then
1978                Style.Missing_Overriding (N, Rename_Spec);
1979             end if;
1980
1981          elsif Must_Override (Specification (N)) then
1982             Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
1983          end if;
1984
1985       --  Normal subprogram renaming (not renaming as body)
1986
1987       else
1988          Generate_Definition (New_S);
1989          New_Overloaded_Entity (New_S);
1990
1991          if Is_Entity_Name (Nam)
1992            and then Is_Intrinsic_Subprogram (Entity (Nam))
1993          then
1994             null;
1995          else
1996             Check_Delayed_Subprogram (New_S);
1997          end if;
1998       end if;
1999
2000       --  There is no need for elaboration checks on the new entity, which may
2001       --  be called before the next freezing point where the body will appear.
2002       --  Elaboration checks refer to the real entity, not the one created by
2003       --  the renaming declaration.
2004
2005       Set_Kill_Elaboration_Checks (New_S, True);
2006
2007       if Etype (Nam) = Any_Type then
2008          Set_Has_Completion (New_S);
2009          return;
2010
2011       elsif Nkind (Nam) = N_Selected_Component then
2012
2013          --  A prefix of the form  A.B can designate an entry of task A, a
2014          --  protected operation of protected object A, or finally a primitive
2015          --  operation of object A. In the later case, A is an object of some
2016          --  tagged type, or an access type that denotes one such. To further
2017          --  distinguish these cases, note that the scope of a task entry or
2018          --  protected operation is type of the prefix.
2019
2020          --  The prefix could be an overloaded function call that returns both
2021          --  kinds of operations. This overloading pathology is left to the
2022          --  dedicated reader ???
2023
2024          declare
2025             T : constant Entity_Id := Etype (Prefix (Nam));
2026
2027          begin
2028             if Present (T)
2029               and then
2030                 (Is_Tagged_Type (T)
2031                   or else
2032                     (Is_Access_Type (T)
2033                       and then
2034                         Is_Tagged_Type (Designated_Type (T))))
2035               and then Scope (Entity (Selector_Name (Nam))) /= T
2036             then
2037                Analyze_Renamed_Primitive_Operation
2038                  (N, New_S, Present (Rename_Spec));
2039                return;
2040
2041             else
2042                --  Renamed entity is an entry or protected operation. For those
2043                --  cases an explicit body is built (at the point of freezing of
2044                --  this entity) that contains a call to the renamed entity.
2045
2046                --  This is not allowed for renaming as body if the renamed
2047                --  spec is already frozen (see RM 8.5.4(5) for details).
2048
2049                if Present (Rename_Spec)
2050                  and then Is_Frozen (Rename_Spec)
2051                then
2052                   Error_Msg_N
2053                     ("renaming-as-body cannot rename entry as subprogram", N);
2054                   Error_Msg_NE
2055                     ("\since & is already frozen (RM 8.5.4(5))",
2056                      N, Rename_Spec);
2057                else
2058                   Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
2059                end if;
2060
2061                return;
2062             end if;
2063          end;
2064
2065       elsif Nkind (Nam) = N_Explicit_Dereference then
2066
2067          --  Renamed entity is designated by access_to_subprogram expression.
2068          --  Must build body to encapsulate call, as in the entry case.
2069
2070          Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
2071          return;
2072
2073       elsif Nkind (Nam) = N_Indexed_Component then
2074          Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
2075          return;
2076
2077       elsif Nkind (Nam) = N_Character_Literal then
2078          Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
2079          return;
2080
2081       elsif (not Is_Entity_Name (Nam)
2082               and then Nkind (Nam) /= N_Operator_Symbol)
2083         or else not Is_Overloadable (Entity (Nam))
2084       then
2085          Error_Msg_N ("expect valid subprogram name in renaming", N);
2086          return;
2087       end if;
2088
2089       --  Find the renamed entity that matches the given specification. Disable
2090       --  Ada_83 because there is no requirement of full conformance between
2091       --  renamed entity and new entity, even though the same circuit is used.
2092
2093       --  This is a bit of a kludge, which introduces a really irregular use of
2094       --  Ada_Version[_Explicit]. Would be nice to find cleaner way to do this
2095       --  ???
2096
2097       Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
2098       Ada_Version_Explicit := Ada_Version;
2099
2100       if No (Old_S) then
2101          Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2102
2103          --  When the renamed subprogram is overloaded and used as an actual
2104          --  of a generic, its entity is set to the first available homonym.
2105          --  We must first disambiguate the name, then set the proper entity.
2106
2107          if Is_Actual
2108            and then Is_Overloaded (Nam)
2109          then
2110             Set_Entity (Nam, Old_S);
2111          end if;
2112       end if;
2113
2114       --  Most common case: subprogram renames subprogram. No body is generated
2115       --  in this case, so we must indicate the declaration is complete as is.
2116       --  and inherit various attributes of the renamed subprogram.
2117
2118       if No (Rename_Spec) then
2119          Set_Has_Completion   (New_S);
2120          Set_Is_Imported      (New_S, Is_Imported      (Entity (Nam)));
2121          Set_Is_Pure          (New_S, Is_Pure          (Entity (Nam)));
2122          Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
2123
2124          --  Ada 2005 (AI-423): Check the consistency of null exclusions
2125          --  between a subprogram and its correct renaming.
2126
2127          --  Note: the Any_Id check is a guard that prevents compiler crashes
2128          --  when performing a null exclusion check between a renaming and a
2129          --  renamed subprogram that has been found to be illegal.
2130
2131          if Ada_Version >= Ada_05
2132            and then Entity (Nam) /= Any_Id
2133          then
2134             Check_Null_Exclusion
2135               (Ren => New_S,
2136                Sub => Entity (Nam));
2137          end if;
2138
2139          --  Enforce the Ada 2005 rule that the renamed entity cannot require
2140          --  overriding. The flag Requires_Overriding is set very selectively
2141          --  and misses some other illegal cases. The additional conditions
2142          --  checked below are sufficient but not necessary ???
2143
2144          --  The rule does not apply to the renaming generated for an actual
2145          --  subprogram in an instance.
2146
2147          if Is_Actual then
2148             null;
2149
2150          --  Guard against previous errors, and omit renamings of predefined
2151          --  operators.
2152
2153          elsif Ekind (Old_S) /= E_Function
2154            and then Ekind (Old_S) /= E_Procedure
2155          then
2156             null;
2157
2158          elsif Requires_Overriding (Old_S)
2159            or else
2160               (Is_Abstract_Subprogram (Old_S)
2161                  and then Present (Find_Dispatching_Type (Old_S))
2162                  and then
2163                    not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
2164          then
2165             Error_Msg_N
2166               ("renamed entity cannot be "
2167                & "subprogram that requires overriding (RM 8.5.4 (5.1))", N);
2168          end if;
2169       end if;
2170
2171       if Old_S /= Any_Id then
2172          if Is_Actual
2173            and then From_Default (N)
2174          then
2175             --  This is an implicit reference to the default actual
2176
2177             Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
2178          else
2179             Generate_Reference (Old_S, Nam);
2180          end if;
2181
2182          --  For a renaming-as-body, require subtype conformance, but if the
2183          --  declaration being completed has not been frozen, then inherit the
2184          --  convention of the renamed subprogram prior to checking conformance
2185          --  (unless the renaming has an explicit convention established; the
2186          --  rule stated in the RM doesn't seem to address this ???).
2187
2188          if Present (Rename_Spec) then
2189             Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
2190             Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
2191
2192             if not Is_Frozen (Rename_Spec) then
2193                if not Has_Convention_Pragma (Rename_Spec) then
2194                   Set_Convention (New_S, Convention (Old_S));
2195                end if;
2196
2197                if Ekind (Old_S) /= E_Operator then
2198                   Check_Mode_Conformant (New_S, Old_S, Spec);
2199                end if;
2200
2201                if Original_Subprogram (Old_S) = Rename_Spec then
2202                   Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
2203                end if;
2204             else
2205                Check_Subtype_Conformant (New_S, Old_S, Spec);
2206             end if;
2207
2208             Check_Frozen_Renaming (N, Rename_Spec);
2209
2210             --  Check explicitly that renamed entity is not intrinsic, because
2211             --  in a generic the renamed body is not built. In this case,
2212             --  the renaming_as_body is a completion.
2213
2214             if Inside_A_Generic then
2215                if Is_Frozen (Rename_Spec)
2216                  and then Is_Intrinsic_Subprogram (Old_S)
2217                then
2218                   Error_Msg_N
2219                     ("subprogram in renaming_as_body cannot be intrinsic",
2220                        Name (N));
2221                end if;
2222
2223                Set_Has_Completion (Rename_Spec);
2224             end if;
2225
2226          elsif Ekind (Old_S) /= E_Operator then
2227             Check_Mode_Conformant (New_S, Old_S);
2228
2229             if Is_Actual
2230               and then Error_Posted (New_S)
2231             then
2232                Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
2233             end if;
2234          end if;
2235
2236          if No (Rename_Spec) then
2237
2238             --  The parameter profile of the new entity is that of the renamed
2239             --  entity: the subtypes given in the specification are irrelevant.
2240
2241             Inherit_Renamed_Profile (New_S, Old_S);
2242
2243             --  A call to the subprogram is transformed into a call to the
2244             --  renamed entity. This is transitive if the renamed entity is
2245             --  itself a renaming.
2246
2247             if Present (Alias (Old_S)) then
2248                Set_Alias (New_S, Alias (Old_S));
2249             else
2250                Set_Alias (New_S, Old_S);
2251             end if;
2252
2253             --  Note that we do not set Is_Intrinsic_Subprogram if we have a
2254             --  renaming as body, since the entity in this case is not an
2255             --  intrinsic (it calls an intrinsic, but we have a real body for
2256             --  this call, and it is in this body that the required intrinsic
2257             --  processing will take place).
2258
2259             --  Also, if this is a renaming of inequality, the renamed operator
2260             --  is intrinsic, but what matters is the corresponding equality
2261             --  operator, which may be user-defined.
2262
2263             Set_Is_Intrinsic_Subprogram
2264               (New_S,
2265                 Is_Intrinsic_Subprogram (Old_S)
2266                   and then
2267                     (Chars (Old_S) /= Name_Op_Ne
2268                        or else Ekind (Old_S) = E_Operator
2269                        or else
2270                          Is_Intrinsic_Subprogram
2271                             (Corresponding_Equality (Old_S))));
2272
2273             if Ekind (Alias (New_S)) = E_Operator then
2274                Set_Has_Delayed_Freeze (New_S, False);
2275             end if;
2276
2277             --  If the renaming corresponds to an association for an abstract
2278             --  formal subprogram, then various attributes must be set to
2279             --  indicate that the renaming is an abstract dispatching operation
2280             --  with a controlling type.
2281
2282             if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
2283
2284                --  Mark the renaming as abstract here, so Find_Dispatching_Type
2285                --  see it as corresponding to a generic association for a
2286                --  formal abstract subprogram
2287
2288                Set_Is_Abstract_Subprogram (New_S);
2289
2290                declare
2291                   New_S_Ctrl_Type : constant Entity_Id :=
2292                                       Find_Dispatching_Type (New_S);
2293                   Old_S_Ctrl_Type : constant Entity_Id :=
2294                                       Find_Dispatching_Type (Old_S);
2295
2296                begin
2297                   if Old_S_Ctrl_Type /= New_S_Ctrl_Type then
2298                      Error_Msg_NE
2299                        ("actual must be dispatching subprogram for type&",
2300                         Nam, New_S_Ctrl_Type);
2301
2302                   else
2303                      Set_Is_Dispatching_Operation (New_S);
2304                      Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
2305
2306                      --  If the actual in the formal subprogram is itself a
2307                      --  formal abstract subprogram association, there's no
2308                      --  dispatch table component or position to inherit.
2309
2310                      if Present (DTC_Entity (Old_S)) then
2311                         Set_DTC_Entity  (New_S, DTC_Entity (Old_S));
2312                         Set_DT_Position (New_S, DT_Position (Old_S));
2313                      end if;
2314                   end if;
2315                end;
2316             end if;
2317          end if;
2318
2319          if not Is_Actual
2320            and then (Old_S = New_S
2321                       or else (Nkind (Nam) /= N_Expanded_Name
2322                         and then  Chars (Old_S) = Chars (New_S)))
2323          then
2324             Error_Msg_N ("subprogram cannot rename itself", N);
2325          end if;
2326
2327          Set_Convention (New_S, Convention (Old_S));
2328
2329          if Is_Abstract_Subprogram (Old_S) then
2330             if Present (Rename_Spec) then
2331                Error_Msg_N
2332                  ("a renaming-as-body cannot rename an abstract subprogram",
2333                   N);
2334                Set_Has_Completion (Rename_Spec);
2335             else
2336                Set_Is_Abstract_Subprogram (New_S);
2337             end if;
2338          end if;
2339
2340          Check_Library_Unit_Renaming (N, Old_S);
2341
2342          --  Pathological case: procedure renames entry in the scope of its
2343          --  task. Entry is given by simple name, but body must be built for
2344          --  procedure. Of course if called it will deadlock.
2345
2346          if Ekind (Old_S) = E_Entry then
2347             Set_Has_Completion (New_S, False);
2348             Set_Alias (New_S, Empty);
2349          end if;
2350
2351          if Is_Actual then
2352             Freeze_Before (N, Old_S);
2353             Set_Has_Delayed_Freeze (New_S, False);
2354             Freeze_Before (N, New_S);
2355
2356             --  An abstract subprogram is only allowed as an actual in the case
2357             --  where the formal subprogram is also abstract.
2358
2359             if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
2360               and then Is_Abstract_Subprogram (Old_S)
2361               and then not Is_Abstract_Subprogram (Formal_Spec)
2362             then
2363                Error_Msg_N
2364                  ("abstract subprogram not allowed as generic actual", Nam);
2365             end if;
2366          end if;
2367
2368       else
2369          --  A common error is to assume that implicit operators for types are
2370          --  defined in Standard, or in the scope of a subtype. In those cases
2371          --  where the renamed entity is given with an expanded name, it is
2372          --  worth mentioning that operators for the type are not declared in
2373          --  the scope given by the prefix.
2374
2375          if Nkind (Nam) = N_Expanded_Name
2376            and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
2377            and then Scope (Entity (Nam)) = Standard_Standard
2378          then
2379             declare
2380                T : constant Entity_Id :=
2381                      Base_Type (Etype (First_Formal (New_S)));
2382             begin
2383                Error_Msg_Node_2 := Prefix (Nam);
2384                Error_Msg_NE
2385                  ("operator for type& is not declared in&", Prefix (Nam), T);
2386             end;
2387
2388          else
2389             Error_Msg_NE
2390               ("no visible subprogram matches the specification for&",
2391                 Spec, New_S);
2392          end if;
2393
2394          if Present (Candidate_Renaming) then
2395             declare
2396                F1 : Entity_Id;
2397                F2 : Entity_Id;
2398                T1 : Entity_Id;
2399
2400             begin
2401                F1 := First_Formal (Candidate_Renaming);
2402                F2 := First_Formal (New_S);
2403                T1 := First_Subtype (Etype (F1));
2404
2405                while Present (F1) and then Present (F2) loop
2406                   Next_Formal (F1);
2407                   Next_Formal (F2);
2408                end loop;
2409
2410                if Present (F1) and then Present (Default_Value (F1)) then
2411                   if Present (Next_Formal (F1)) then
2412                      Error_Msg_NE
2413                        ("\missing specification for &" &
2414                           " and other formals with defaults", Spec, F1);
2415                   else
2416                      Error_Msg_NE
2417                     ("\missing specification for &", Spec, F1);
2418                   end if;
2419                end if;
2420
2421                if Nkind (Nam) = N_Operator_Symbol
2422                  and then From_Default (N)
2423                then
2424                   Error_Msg_Node_2 := T1;
2425                   Error_Msg_NE
2426                     ("default & on & is not directly visible",
2427                       Nam, Nam);
2428                end if;
2429             end;
2430          end if;
2431       end if;
2432
2433       --  Ada 2005 AI 404: if the new subprogram is dispatching, verify that
2434       --  controlling access parameters are known non-null for the renamed
2435       --  subprogram. Test also applies to a subprogram instantiation that
2436       --  is dispatching. Test is skipped if some previous error was detected
2437       --  that set Old_S to Any_Id.
2438
2439       if Ada_Version >= Ada_05
2440         and then Old_S /= Any_Id
2441         and then not Is_Dispatching_Operation (Old_S)
2442         and then Is_Dispatching_Operation (New_S)
2443       then
2444          declare
2445             Old_F : Entity_Id;
2446             New_F : Entity_Id;
2447
2448          begin
2449             Old_F := First_Formal (Old_S);
2450             New_F := First_Formal (New_S);
2451             while Present (Old_F) loop
2452                if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
2453                  and then Is_Controlling_Formal (New_F)
2454                  and then not Can_Never_Be_Null (Old_F)
2455                then
2456                   Error_Msg_N ("access parameter is controlling,", New_F);
2457                   Error_Msg_NE
2458                     ("\corresponding parameter of& "
2459                      & "must be explicitly null excluding", New_F, Old_S);
2460                end if;
2461
2462                Next_Formal (Old_F);
2463                Next_Formal (New_F);
2464             end loop;
2465          end;
2466       end if;
2467
2468       --  A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
2469
2470       if Comes_From_Source (N)
2471         and then Present (Old_S)
2472         and then Nkind (Old_S) = N_Defining_Operator_Symbol
2473         and then Nkind (New_S) = N_Defining_Operator_Symbol
2474         and then Chars (Old_S) /= Chars (New_S)
2475       then
2476          Error_Msg_NE
2477            ("?& is being renamed as a different operator",
2478              New_S, Old_S);
2479       end if;
2480
2481       --  Another warning or some utility: if the new subprogram as the same
2482       --  name as the old one, the old one is not hidden by an outer homograph,
2483       --  the new one is not a public symbol, and the old one is otherwise
2484       --  directly visible, the renaming is superfluous.
2485
2486       if Chars (Old_S) = Chars (New_S)
2487         and then Comes_From_Source (N)
2488         and then Scope (Old_S) /= Standard_Standard
2489         and then Warn_On_Redundant_Constructs
2490         and then
2491           (Is_Immediately_Visible (Old_S)
2492             or else Is_Potentially_Use_Visible (Old_S))
2493         and then Is_Overloadable (Current_Scope)
2494         and then Chars (Current_Scope) /= Chars (Old_S)
2495       then
2496          Error_Msg_N
2497           ("?redundant renaming, entity is directly visible", Name (N));
2498       end if;
2499
2500       Ada_Version := Save_AV;
2501       Ada_Version_Explicit := Save_AV_Exp;
2502    end Analyze_Subprogram_Renaming;
2503
2504    -------------------------
2505    -- Analyze_Use_Package --
2506    -------------------------
2507
2508    --  Resolve the package names in the use clause, and make all the visible
2509    --  entities defined in the package potentially use-visible. If the package
2510    --  is already in use from a previous use clause, its visible entities are
2511    --  already use-visible. In that case, mark the occurrence as a redundant
2512    --  use. If the package is an open scope, i.e. if the use clause occurs
2513    --  within the package itself, ignore it.
2514
2515    procedure Analyze_Use_Package (N : Node_Id) is
2516       Pack_Name : Node_Id;
2517       Pack      : Entity_Id;
2518
2519    --  Start of processing for Analyze_Use_Package
2520
2521    begin
2522       Set_Hidden_By_Use_Clause (N, No_Elist);
2523
2524       --  Use clause is not allowed in a spec of a predefined package
2525       --  declaration except that packages whose file name starts a-n are OK
2526       --  (these are children of Ada.Numerics, and such packages are never
2527       --  loaded by Rtsfind).
2528
2529       if Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
2530         and then Name_Buffer (1 .. 3) /= "a-n"
2531         and then
2532           Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
2533       then
2534          Error_Msg_N ("use clause not allowed in predefined spec", N);
2535       end if;
2536
2537       --  Chain clause to list of use clauses in current scope
2538
2539       if Nkind (Parent (N)) /= N_Compilation_Unit then
2540          Chain_Use_Clause (N);
2541       end if;
2542
2543       --  Loop through package names to identify referenced packages
2544
2545       Pack_Name := First (Names (N));
2546       while Present (Pack_Name) loop
2547          Analyze (Pack_Name);
2548
2549          if Nkind (Parent (N)) = N_Compilation_Unit
2550            and then Nkind (Pack_Name) = N_Expanded_Name
2551          then
2552             declare
2553                Pref : Node_Id;
2554
2555             begin
2556                Pref := Prefix (Pack_Name);
2557                while Nkind (Pref) = N_Expanded_Name loop
2558                   Pref := Prefix (Pref);
2559                end loop;
2560
2561                if Entity (Pref) = Standard_Standard then
2562                   Error_Msg_N
2563                    ("predefined package Standard cannot appear"
2564                      & " in a context clause", Pref);
2565                end if;
2566             end;
2567          end if;
2568
2569          Next (Pack_Name);
2570       end loop;
2571
2572       --  Loop through package names to mark all entities as potentially
2573       --  use visible.
2574
2575       Pack_Name := First (Names (N));
2576       while Present (Pack_Name) loop
2577          if Is_Entity_Name (Pack_Name) then
2578             Pack := Entity (Pack_Name);
2579
2580             if Ekind (Pack) /= E_Package
2581               and then Etype (Pack) /= Any_Type
2582             then
2583                if Ekind (Pack) = E_Generic_Package then
2584                   Error_Msg_N
2585                    ("a generic package is not allowed in a use clause",
2586                       Pack_Name);
2587                else
2588                   Error_Msg_N ("& is not a usable package", Pack_Name);
2589                end if;
2590
2591             else
2592                if Nkind (Parent (N)) = N_Compilation_Unit then
2593                   Check_In_Previous_With_Clause (N, Pack_Name);
2594                end if;
2595
2596                if Applicable_Use (Pack_Name) then
2597                   Use_One_Package (Pack, N);
2598                end if;
2599             end if;
2600
2601          --  Report error because name denotes something other than a package
2602
2603          else
2604             Error_Msg_N ("& is not a package", Pack_Name);
2605          end if;
2606
2607          Next (Pack_Name);
2608       end loop;
2609    end Analyze_Use_Package;
2610
2611    ----------------------
2612    -- Analyze_Use_Type --
2613    ----------------------
2614
2615    procedure Analyze_Use_Type (N : Node_Id) is
2616       E  : Entity_Id;
2617       Id : Node_Id;
2618
2619    begin
2620       Set_Hidden_By_Use_Clause (N, No_Elist);
2621
2622       --  Chain clause to list of use clauses in current scope
2623
2624       if Nkind (Parent (N)) /= N_Compilation_Unit then
2625          Chain_Use_Clause (N);
2626       end if;
2627
2628       Id := First (Subtype_Marks (N));
2629       while Present (Id) loop
2630          Find_Type (Id);
2631          E := Entity (Id);
2632
2633          if E /= Any_Type then
2634             Use_One_Type (Id);
2635
2636             if Nkind (Parent (N)) = N_Compilation_Unit then
2637                if Nkind (Id) = N_Identifier then
2638                   Error_Msg_N ("type is not directly visible", Id);
2639
2640                elsif Is_Child_Unit (Scope (E))
2641                  and then Scope (E) /= System_Aux_Id
2642                then
2643                   Check_In_Previous_With_Clause (N, Prefix (Id));
2644                end if;
2645             end if;
2646
2647          else
2648             --  If the use_type_clause appears in a compilation unit context,
2649             --  check whether it comes from a unit that may appear in a
2650             --  limited_with_clause, for a better error message.
2651
2652             if Nkind (Parent (N)) = N_Compilation_Unit
2653               and then Nkind (Id) /= N_Identifier
2654             then
2655                declare
2656                   Item : Node_Id;
2657                   Pref : Node_Id;
2658
2659                   function Mentioned (Nam : Node_Id) return Boolean;
2660                   --  Check whether the prefix of expanded name for the type
2661                   --  appears in the prefix of some limited_with_clause.
2662
2663                   ---------------
2664                   -- Mentioned --
2665                   ---------------
2666
2667                   function Mentioned (Nam : Node_Id) return Boolean is
2668                   begin
2669                      return Nkind (Name (Item)) = N_Selected_Component
2670                               and then
2671                             Chars (Prefix (Name (Item))) = Chars (Nam);
2672                   end Mentioned;
2673
2674                begin
2675                   Pref := Prefix (Id);
2676                   Item := First (Context_Items (Parent (N)));
2677
2678                   while Present (Item) and then Item /= N loop
2679                      if Nkind (Item) = N_With_Clause
2680                        and then Limited_Present (Item)
2681                        and then Mentioned (Pref)
2682                      then
2683                         Change_Error_Text
2684                           (Get_Msg_Id, "premature usage of incomplete type");
2685                      end if;
2686
2687                      Next (Item);
2688                   end loop;
2689                end;
2690             end if;
2691          end if;
2692
2693          Next (Id);
2694       end loop;
2695    end Analyze_Use_Type;
2696
2697    --------------------
2698    -- Applicable_Use --
2699    --------------------
2700
2701    function Applicable_Use (Pack_Name : Node_Id) return Boolean is
2702       Pack : constant Entity_Id := Entity (Pack_Name);
2703
2704    begin
2705       if In_Open_Scopes (Pack) then
2706          if Warn_On_Redundant_Constructs
2707            and then Pack = Current_Scope
2708          then
2709             Error_Msg_NE
2710               ("& is already use-visible within itself?", Pack_Name, Pack);
2711          end if;
2712
2713          return False;
2714
2715       elsif In_Use (Pack) then
2716          Note_Redundant_Use (Pack_Name);
2717          return False;
2718
2719       elsif Present (Renamed_Object (Pack))
2720         and then In_Use (Renamed_Object (Pack))
2721       then
2722          Note_Redundant_Use (Pack_Name);
2723          return False;
2724
2725       else
2726          return True;
2727       end if;
2728    end Applicable_Use;
2729
2730    ------------------------
2731    -- Attribute_Renaming --
2732    ------------------------
2733
2734    procedure Attribute_Renaming (N : Node_Id) is
2735       Loc        : constant Source_Ptr := Sloc (N);
2736       Nam        : constant Node_Id    := Name (N);
2737       Spec       : constant Node_Id    := Specification (N);
2738       New_S      : constant Entity_Id  := Defining_Unit_Name (Spec);
2739       Aname      : constant Name_Id    := Attribute_Name (Nam);
2740
2741       Form_Num   : Nat      := 0;
2742       Expr_List  : List_Id  := No_List;
2743
2744       Attr_Node  : Node_Id;
2745       Body_Node  : Node_Id;
2746       Param_Spec : Node_Id;
2747
2748    begin
2749       Generate_Definition (New_S);
2750
2751       --  This procedure is called in the context of subprogram renaming, and
2752       --  thus the attribute must be one that is a subprogram. All of those
2753       --  have at least one formal parameter, with the singular exception of
2754       --  AST_Entry (which is a real oddity, it is odd that this can be renamed
2755       --  at all!)
2756
2757       if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
2758          if Aname /= Name_AST_Entry then
2759             Error_Msg_N
2760               ("subprogram renaming an attribute must have formals", N);
2761             return;
2762          end if;
2763
2764       else
2765          Param_Spec := First (Parameter_Specifications (Spec));
2766          while Present (Param_Spec) loop
2767             Form_Num := Form_Num + 1;
2768
2769             if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
2770                Find_Type (Parameter_Type (Param_Spec));
2771
2772                --  The profile of the new entity denotes the base type (s) of
2773                --  the types given in the specification. For access parameters
2774                --  there are no subtypes involved.
2775
2776                Rewrite (Parameter_Type (Param_Spec),
2777                 New_Reference_To
2778                   (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
2779             end if;
2780
2781             if No (Expr_List) then
2782                Expr_List := New_List;
2783             end if;
2784
2785             Append_To (Expr_List,
2786               Make_Identifier (Loc,
2787                 Chars => Chars (Defining_Identifier (Param_Spec))));
2788
2789             --  The expressions in the attribute reference are not freeze
2790             --  points. Neither is the attribute as a whole, see below.
2791
2792             Set_Must_Not_Freeze (Last (Expr_List));
2793             Next (Param_Spec);
2794          end loop;
2795       end if;
2796
2797       --  Immediate error if too many formals. Other mismatches in number or
2798       --  types of parameters are detected when we analyze the body of the
2799       --  subprogram that we construct.
2800
2801       if Form_Num > 2 then
2802          Error_Msg_N ("too many formals for attribute", N);
2803
2804       --  Error if the attribute reference has expressions that look like
2805       --  formal parameters.
2806
2807       elsif Present (Expressions (Nam)) then
2808          Error_Msg_N ("illegal expressions in attribute reference", Nam);
2809
2810       elsif
2811         Aname = Name_Compose      or else
2812         Aname = Name_Exponent     or else
2813         Aname = Name_Leading_Part or else
2814         Aname = Name_Pos          or else
2815         Aname = Name_Round        or else
2816         Aname = Name_Scaling      or else
2817         Aname = Name_Val
2818       then
2819          if Nkind (N) = N_Subprogram_Renaming_Declaration
2820            and then Present (Corresponding_Formal_Spec (N))
2821          then
2822             Error_Msg_N
2823               ("generic actual cannot be attribute involving universal type",
2824                Nam);
2825          else
2826             Error_Msg_N
2827               ("attribute involving a universal type cannot be renamed",
2828                Nam);
2829          end if;
2830       end if;
2831
2832       --  AST_Entry is an odd case. It doesn't really make much sense to allow
2833       --  it to be renamed, but that's the DEC rule, so we have to do it right.
2834       --  The point is that the AST_Entry call should be made now, and what the
2835       --  function will return is the returned value.
2836
2837       --  Note that there is no Expr_List in this case anyway
2838
2839       if Aname = Name_AST_Entry then
2840          declare
2841             Ent  : Entity_Id;
2842             Decl : Node_Id;
2843
2844          begin
2845             Ent := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
2846
2847             Decl :=
2848               Make_Object_Declaration (Loc,
2849                 Defining_Identifier => Ent,
2850                 Object_Definition =>
2851                   New_Occurrence_Of (RTE (RE_AST_Handler), Loc),
2852                 Expression => Nam,
2853                 Constant_Present => True);
2854
2855             Set_Assignment_OK (Decl, True);
2856             Insert_Action (N, Decl);
2857             Attr_Node := Make_Identifier (Loc, Chars (Ent));
2858          end;
2859
2860       --  For all other attributes, we rewrite the attribute node to have
2861       --  a list of expressions corresponding to the subprogram formals.
2862       --  A renaming declaration is not a freeze point, and the analysis of
2863       --  the attribute reference should not freeze the type of the prefix.
2864
2865       else
2866          Attr_Node :=
2867            Make_Attribute_Reference (Loc,
2868              Prefix         => Prefix (Nam),
2869              Attribute_Name => Aname,
2870              Expressions    => Expr_List);
2871
2872          Set_Must_Not_Freeze (Attr_Node);
2873          Set_Must_Not_Freeze (Prefix (Nam));
2874       end if;
2875
2876       --  Case of renaming a function
2877
2878       if Nkind (Spec) = N_Function_Specification then
2879          if Is_Procedure_Attribute_Name (Aname) then
2880             Error_Msg_N ("attribute can only be renamed as procedure", Nam);
2881             return;
2882          end if;
2883
2884          Find_Type (Result_Definition (Spec));
2885          Rewrite (Result_Definition (Spec),
2886              New_Reference_To (
2887                Base_Type (Entity (Result_Definition (Spec))), Loc));
2888
2889          Body_Node :=
2890            Make_Subprogram_Body (Loc,
2891              Specification => Spec,
2892              Declarations => New_List,
2893              Handled_Statement_Sequence =>
2894                Make_Handled_Sequence_Of_Statements (Loc,
2895                    Statements => New_List (
2896                      Make_Simple_Return_Statement (Loc,
2897                        Expression => Attr_Node))));
2898
2899       --  Case of renaming a procedure
2900
2901       else
2902          if not Is_Procedure_Attribute_Name (Aname) then
2903             Error_Msg_N ("attribute can only be renamed as function", Nam);
2904             return;
2905          end if;
2906
2907          Body_Node :=
2908            Make_Subprogram_Body (Loc,
2909              Specification => Spec,
2910              Declarations => New_List,
2911              Handled_Statement_Sequence =>
2912                Make_Handled_Sequence_Of_Statements (Loc,
2913                    Statements => New_List (Attr_Node)));
2914       end if;
2915
2916       --  In case of tagged types we add the body of the generated function to
2917       --  the freezing actions of the type (because in the general case such
2918       --  type is still not frozen). We exclude from this processing generic
2919       --  formal subprograms found in instantiations and AST_Entry renamings.
2920
2921       if not Present (Corresponding_Formal_Spec (N))
2922         and then Etype (Nam) /= RTE (RE_AST_Handler)
2923       then
2924          declare
2925             P : constant Entity_Id := Prefix (Nam);
2926
2927          begin
2928             Find_Type (P);
2929
2930             if Is_Tagged_Type (Etype (P)) then
2931                Ensure_Freeze_Node (Etype (P));
2932                Append_Freeze_Action (Etype (P), Body_Node);
2933             else
2934                Rewrite (N, Body_Node);
2935                Analyze (N);
2936                Set_Etype (New_S, Base_Type (Etype (New_S)));
2937             end if;
2938          end;
2939
2940       --  Generic formal subprograms or AST_Handler renaming
2941
2942       else
2943          Rewrite (N, Body_Node);
2944          Analyze (N);
2945          Set_Etype (New_S, Base_Type (Etype (New_S)));
2946       end if;
2947
2948       if Is_Compilation_Unit (New_S) then
2949          Error_Msg_N
2950            ("a library unit can only rename another library unit", N);
2951       end if;
2952
2953       --  We suppress elaboration warnings for the resulting entity, since
2954       --  clearly they are not needed, and more particularly, in the case
2955       --  of a generic formal subprogram, the resulting entity can appear
2956       --  after the instantiation itself, and thus look like a bogus case
2957       --  of access before elaboration.
2958
2959       Set_Suppress_Elaboration_Warnings (New_S);
2960
2961    end Attribute_Renaming;
2962
2963    ----------------------
2964    -- Chain_Use_Clause --
2965    ----------------------
2966
2967    procedure Chain_Use_Clause (N : Node_Id) is
2968       Pack : Entity_Id;
2969       Level : Int := Scope_Stack.Last;
2970
2971    begin
2972       if not Is_Compilation_Unit (Current_Scope)
2973         or else not Is_Child_Unit (Current_Scope)
2974       then
2975          null;   --  Common case
2976
2977       elsif Defining_Entity (Parent (N)) = Current_Scope then
2978          null;   --  Common case for compilation unit
2979
2980       else
2981          --  If declaration appears in some other scope, it must be in some
2982          --  parent unit when compiling a child.
2983
2984          Pack := Defining_Entity (Parent (N));
2985          if not In_Open_Scopes (Pack) then
2986             null;  --  default as well
2987
2988          else
2989             --  Find entry for parent unit in scope stack
2990
2991             while Scope_Stack.Table (Level).Entity /= Pack loop
2992                Level := Level - 1;
2993             end loop;
2994          end if;
2995       end if;
2996
2997       Set_Next_Use_Clause (N,
2998         Scope_Stack.Table (Level).First_Use_Clause);
2999       Scope_Stack.Table (Level).First_Use_Clause := N;
3000    end Chain_Use_Clause;
3001
3002    ---------------------------
3003    -- Check_Frozen_Renaming --
3004    ---------------------------
3005
3006    procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
3007       B_Node : Node_Id;
3008       Old_S  : Entity_Id;
3009
3010    begin
3011       if Is_Frozen (Subp)
3012         and then not Has_Completion (Subp)
3013       then
3014          B_Node :=
3015            Build_Renamed_Body
3016              (Parent (Declaration_Node (Subp)), Defining_Entity (N));
3017
3018          if Is_Entity_Name (Name (N)) then
3019             Old_S := Entity (Name (N));
3020
3021             if not Is_Frozen (Old_S)
3022               and then Operating_Mode /= Check_Semantics
3023             then
3024                Append_Freeze_Action (Old_S, B_Node);
3025             else
3026                Insert_After (N, B_Node);
3027                Analyze (B_Node);
3028             end if;
3029
3030             if Is_Intrinsic_Subprogram (Old_S)
3031               and then not In_Instance
3032             then
3033                Error_Msg_N
3034                  ("subprogram used in renaming_as_body cannot be intrinsic",
3035                     Name (N));
3036             end if;
3037
3038          else
3039             Insert_After (N, B_Node);
3040             Analyze (B_Node);
3041          end if;
3042       end if;
3043    end Check_Frozen_Renaming;
3044
3045    -----------------------------------
3046    -- Check_In_Previous_With_Clause --
3047    -----------------------------------
3048
3049    procedure Check_In_Previous_With_Clause
3050      (N   : Node_Id;
3051       Nam : Entity_Id)
3052    is
3053       Pack : constant Entity_Id := Entity (Original_Node (Nam));
3054       Item : Node_Id;
3055       Par  : Node_Id;
3056
3057    begin
3058       Item := First (Context_Items (Parent (N)));
3059
3060       while Present (Item)
3061         and then Item /= N
3062       loop
3063          if Nkind (Item) = N_With_Clause
3064
3065             --  Protect the frontend against previous critical errors
3066
3067            and then Nkind (Name (Item)) /= N_Selected_Component
3068            and then Entity (Name (Item)) = Pack
3069          then
3070             Par := Nam;
3071
3072             --  Find root library unit in with_clause
3073
3074             while Nkind (Par) = N_Expanded_Name loop
3075                Par := Prefix (Par);
3076             end loop;
3077
3078             if Is_Child_Unit (Entity (Original_Node (Par))) then
3079                Error_Msg_NE
3080                  ("& is not directly visible", Par, Entity (Par));
3081             else
3082                return;
3083             end if;
3084          end if;
3085
3086          Next (Item);
3087       end loop;
3088
3089       --  On exit, package is not mentioned in a previous with_clause.
3090       --  Check if its prefix is.
3091
3092       if Nkind (Nam) = N_Expanded_Name then
3093          Check_In_Previous_With_Clause (N, Prefix (Nam));
3094
3095       elsif Pack /= Any_Id then
3096          Error_Msg_NE ("& is not visible", Nam, Pack);
3097       end if;
3098    end Check_In_Previous_With_Clause;
3099
3100    ---------------------------------
3101    -- Check_Library_Unit_Renaming --
3102    ---------------------------------
3103
3104    procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
3105       New_E : Entity_Id;
3106
3107    begin
3108       if Nkind (Parent (N)) /= N_Compilation_Unit then
3109          return;
3110
3111       --  Check for library unit. Note that we used to check for the scope
3112       --  being Standard here, but that was wrong for Standard itself.
3113
3114       elsif not Is_Compilation_Unit (Old_E)
3115         and then not Is_Child_Unit (Old_E)
3116       then
3117          Error_Msg_N ("renamed unit must be a library unit", Name (N));
3118
3119       --  Entities defined in Standard (operators and boolean literals) cannot
3120       --  be renamed as library units.
3121
3122       elsif Scope (Old_E) = Standard_Standard
3123         and then Sloc (Old_E) = Standard_Location
3124       then
3125          Error_Msg_N ("renamed unit must be a library unit", Name (N));
3126
3127       elsif Present (Parent_Spec (N))
3128         and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
3129         and then not Is_Child_Unit (Old_E)
3130       then
3131          Error_Msg_N
3132            ("renamed unit must be a child unit of generic parent", Name (N));
3133
3134       elsif Nkind (N) in N_Generic_Renaming_Declaration
3135          and then  Nkind (Name (N)) = N_Expanded_Name
3136          and then Is_Generic_Instance (Entity (Prefix (Name (N))))
3137          and then Is_Generic_Unit (Old_E)
3138       then
3139          Error_Msg_N
3140            ("renamed generic unit must be a library unit", Name (N));
3141
3142       elsif Is_Package_Or_Generic_Package (Old_E) then
3143
3144          --  Inherit categorization flags
3145
3146          New_E := Defining_Entity (N);
3147          Set_Is_Pure                  (New_E, Is_Pure           (Old_E));
3148          Set_Is_Preelaborated         (New_E, Is_Preelaborated  (Old_E));
3149          Set_Is_Remote_Call_Interface (New_E,
3150                                        Is_Remote_Call_Interface (Old_E));
3151          Set_Is_Remote_Types          (New_E, Is_Remote_Types   (Old_E));
3152          Set_Is_Shared_Passive        (New_E, Is_Shared_Passive (Old_E));
3153       end if;
3154    end Check_Library_Unit_Renaming;
3155
3156    ---------------
3157    -- End_Scope --
3158    ---------------
3159
3160    procedure End_Scope is
3161       Id    : Entity_Id;
3162       Prev  : Entity_Id;
3163       Outer : Entity_Id;
3164
3165    begin
3166       Id := First_Entity (Current_Scope);
3167       while Present (Id) loop
3168          --  An entity in the current scope is not necessarily the first one
3169          --  on its homonym chain. Find its predecessor if any,
3170          --  If it is an internal entity, it will not be in the visibility
3171          --  chain altogether,  and there is nothing to unchain.
3172
3173          if Id /= Current_Entity (Id) then
3174             Prev := Current_Entity (Id);
3175             while Present (Prev)
3176               and then Present (Homonym (Prev))
3177               and then Homonym (Prev) /= Id
3178             loop
3179                Prev := Homonym (Prev);
3180             end loop;
3181
3182             --  Skip to end of loop if Id is not in the visibility chain
3183
3184             if No (Prev) or else Homonym (Prev) /= Id then
3185                goto Next_Ent;
3186             end if;
3187
3188          else
3189             Prev := Empty;
3190          end if;
3191
3192          Set_Is_Immediately_Visible (Id, False);
3193
3194          Outer := Homonym (Id);
3195          while Present (Outer) and then Scope (Outer) = Current_Scope loop
3196             Outer := Homonym (Outer);
3197          end loop;
3198
3199          --  Reset homonym link of other entities, but do not modify link
3200          --  between entities in current scope, so that the back-end can have
3201          --  a proper count of local overloadings.
3202
3203          if No (Prev) then
3204             Set_Name_Entity_Id (Chars (Id), Outer);
3205
3206          elsif Scope (Prev) /= Scope (Id) then
3207             Set_Homonym (Prev,  Outer);
3208          end if;
3209
3210          <<Next_Ent>>
3211             Next_Entity (Id);
3212       end loop;
3213
3214       --  If the scope generated freeze actions, place them before the
3215       --  current declaration and analyze them. Type declarations and
3216       --  the bodies of initialization procedures can generate such nodes.
3217       --  We follow the parent chain until we reach a list node, which is
3218       --  the enclosing list of declarations. If the list appears within
3219       --  a protected definition, move freeze nodes outside the protected
3220       --  type altogether.
3221
3222       if Present
3223          (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
3224       then
3225          declare
3226             Decl : Node_Id;
3227             L    : constant List_Id := Scope_Stack.Table
3228                     (Scope_Stack.Last).Pending_Freeze_Actions;
3229
3230          begin
3231             if Is_Itype (Current_Scope) then
3232                Decl := Associated_Node_For_Itype (Current_Scope);
3233             else
3234                Decl := Parent (Current_Scope);
3235             end if;
3236
3237             Pop_Scope;
3238
3239             while not (Is_List_Member (Decl))
3240               or else Nkind_In (Parent (Decl), N_Protected_Definition,
3241                                                N_Task_Definition)
3242             loop
3243                Decl := Parent (Decl);
3244             end loop;
3245
3246             Insert_List_Before_And_Analyze (Decl, L);
3247          end;
3248
3249       else
3250          Pop_Scope;
3251       end if;
3252
3253    end End_Scope;
3254
3255    ---------------------
3256    -- End_Use_Clauses --
3257    ---------------------
3258
3259    procedure End_Use_Clauses (Clause : Node_Id) is
3260       U   : Node_Id;
3261
3262    begin
3263       --  Remove Use_Type clauses first, because they affect the
3264       --  visibility of operators in subsequent used packages.
3265
3266       U := Clause;
3267       while Present (U) loop
3268          if Nkind (U) = N_Use_Type_Clause then
3269             End_Use_Type (U);
3270          end if;
3271
3272          Next_Use_Clause (U);
3273       end loop;
3274
3275       U := Clause;
3276       while Present (U) loop
3277          if Nkind (U) = N_Use_Package_Clause then
3278             End_Use_Package (U);
3279          end if;
3280
3281          Next_Use_Clause (U);
3282       end loop;
3283    end End_Use_Clauses;
3284
3285    ---------------------
3286    -- End_Use_Package --
3287    ---------------------
3288
3289    procedure End_Use_Package (N : Node_Id) is
3290       Pack_Name : Node_Id;
3291       Pack      : Entity_Id;
3292       Id        : Entity_Id;
3293       Elmt      : Elmt_Id;
3294
3295       function Is_Primitive_Operator
3296         (Op : Entity_Id;
3297          F  : Entity_Id) return Boolean;
3298       --  Check whether Op is a primitive operator of a use-visible type
3299
3300       ---------------------------
3301       -- Is_Primitive_Operator --
3302       ---------------------------
3303
3304       function Is_Primitive_Operator
3305         (Op : Entity_Id;
3306          F  : Entity_Id) return Boolean
3307       is
3308          T : constant Entity_Id := Etype (F);
3309       begin
3310          return In_Use (T)
3311            and then Scope (T) = Scope (Op);
3312       end Is_Primitive_Operator;
3313
3314    --  Start of processing for End_Use_Package
3315
3316    begin
3317       Pack_Name := First (Names (N));
3318       while Present (Pack_Name) loop
3319
3320          --  Test that Pack_Name actually denotes a package before processing
3321
3322          if Is_Entity_Name (Pack_Name)
3323            and then Ekind (Entity (Pack_Name)) = E_Package
3324          then
3325             Pack := Entity (Pack_Name);
3326
3327             if In_Open_Scopes (Pack) then
3328                null;
3329
3330             elsif not Redundant_Use (Pack_Name) then
3331                Set_In_Use (Pack, False);
3332                Set_Current_Use_Clause (Pack, Empty);
3333
3334                Id := First_Entity (Pack);
3335                while Present (Id) loop
3336
3337                   --  Preserve use-visibility of operators that are primitive
3338                   --  operators of a type that is use-visible through an active
3339                   --  use_type clause.
3340
3341                   if Nkind (Id) = N_Defining_Operator_Symbol
3342                        and then
3343                          (Is_Primitive_Operator (Id, First_Formal (Id))
3344                             or else
3345                           (Present (Next_Formal (First_Formal (Id)))
3346                              and then
3347                                Is_Primitive_Operator
3348                                  (Id, Next_Formal (First_Formal (Id)))))
3349                   then
3350                      null;
3351
3352                   else
3353                      Set_Is_Potentially_Use_Visible (Id, False);
3354                   end if;
3355
3356                   if Is_Private_Type (Id)
3357                     and then Present (Full_View (Id))
3358                   then
3359                      Set_Is_Potentially_Use_Visible (Full_View (Id), False);
3360                   end if;
3361
3362                   Next_Entity (Id);
3363                end loop;
3364
3365                if Present (Renamed_Object (Pack)) then
3366                   Set_In_Use (Renamed_Object (Pack), False);
3367                   Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
3368                end if;
3369
3370                if Chars (Pack) = Name_System
3371                  and then Scope (Pack) = Standard_Standard
3372                  and then Present_System_Aux
3373                then
3374                   Id := First_Entity (System_Aux_Id);
3375                   while Present (Id) loop
3376                      Set_Is_Potentially_Use_Visible (Id, False);
3377
3378                      if Is_Private_Type (Id)
3379                        and then Present (Full_View (Id))
3380                      then
3381                         Set_Is_Potentially_Use_Visible (Full_View (Id), False);
3382                      end if;
3383
3384                      Next_Entity (Id);
3385                   end loop;
3386
3387                   Set_In_Use (System_Aux_Id, False);
3388                end if;
3389
3390             else
3391                Set_Redundant_Use (Pack_Name, False);
3392             end if;
3393          end if;
3394
3395          Next (Pack_Name);
3396       end loop;
3397
3398       if Present (Hidden_By_Use_Clause (N)) then
3399          Elmt := First_Elmt (Hidden_By_Use_Clause (N));
3400          while Present (Elmt) loop
3401             declare
3402                E : constant Entity_Id := Node (Elmt);
3403
3404             begin
3405                --  Reset either Use_Visibility or Direct_Visibility, depending
3406                --  on how the entity was hidden by the use clause.
3407
3408                if In_Use (Scope (E))
3409                  and then Used_As_Generic_Actual (Scope (E))
3410                then
3411                   Set_Is_Potentially_Use_Visible (Node (Elmt));
3412                else
3413                   Set_Is_Immediately_Visible (Node (Elmt));
3414                end if;
3415
3416                Next_Elmt (Elmt);
3417             end;
3418          end loop;
3419
3420          Set_Hidden_By_Use_Clause (N, No_Elist);
3421       end if;
3422    end End_Use_Package;
3423
3424    ------------------
3425    -- End_Use_Type --
3426    ------------------
3427
3428    procedure End_Use_Type (N : Node_Id) is
3429       Id      : Entity_Id;
3430       Op_List : Elist_Id;
3431       Elmt    : Elmt_Id;
3432       T       : Entity_Id;
3433
3434    begin
3435       Id := First (Subtype_Marks (N));
3436       while Present (Id) loop
3437
3438          --  A call to rtsfind may occur while analyzing a use_type clause,
3439          --  in which case the type marks are not resolved yet, and there is
3440          --  nothing to remove.
3441
3442          if not Is_Entity_Name (Id)
3443            or else No (Entity (Id))
3444          then
3445             goto Continue;
3446          end if;
3447
3448          T := Entity (Id);
3449
3450          if T = Any_Type
3451            or else From_With_Type (T)
3452          then
3453             null;
3454
3455          --  Note that the use_Type clause may mention a subtype of the type
3456          --  whose primitive operations have been made visible. Here as
3457          --  elsewhere, it is the base type that matters for visibility.
3458
3459          elsif In_Open_Scopes (Scope (Base_Type (T))) then
3460             null;
3461
3462          elsif not Redundant_Use (Id) then
3463             Set_In_Use (T, False);
3464             Set_In_Use (Base_Type (T), False);
3465             Set_Current_Use_Clause (T, Empty);
3466             Set_Current_Use_Clause (Base_Type (T), Empty);
3467             Op_List := Collect_Primitive_Operations (T);
3468
3469             Elmt := First_Elmt (Op_List);
3470             while Present (Elmt) loop
3471                if Nkind (Node (Elmt)) = N_Defining_Operator_Symbol then
3472                   Set_Is_Potentially_Use_Visible (Node (Elmt), False);
3473                end if;
3474
3475                Next_Elmt (Elmt);
3476             end loop;
3477          end if;
3478
3479          <<Continue>>
3480          Next (Id);
3481       end loop;
3482    end End_Use_Type;
3483
3484    ----------------------
3485    -- Find_Direct_Name --
3486    ----------------------
3487
3488    procedure Find_Direct_Name (N : Node_Id) is
3489       E    : Entity_Id;
3490       E2   : Entity_Id;
3491       Msg  : Boolean;
3492
3493       Inst : Entity_Id := Empty;
3494       --  Enclosing instance, if any
3495
3496       Homonyms : Entity_Id;
3497       --  Saves start of homonym chain
3498
3499       Nvis_Entity : Boolean;
3500       --  Set True to indicate that there is at least one entity on the homonym
3501       --  chain which, while not visible, is visible enough from the user point
3502       --  of view to warrant an error message of "not visible" rather than
3503       --  undefined.
3504
3505       Nvis_Is_Private_Subprg : Boolean := False;
3506       --  Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
3507       --  effect concerning library subprograms has been detected. Used to
3508       --  generate the precise error message.
3509
3510       function From_Actual_Package (E : Entity_Id) return Boolean;
3511       --  Returns true if the entity is declared in a package that is
3512       --  an actual for a formal package of the current instance. Such an
3513       --  entity requires special handling because it may be use-visible
3514       --  but hides directly visible entities defined outside the instance.
3515
3516       function Is_Actual_Parameter return Boolean;
3517       --  This function checks if the node N is an identifier that is an actual
3518       --  parameter of a procedure call. If so it returns True, otherwise it
3519       --  return False. The reason for this check is that at this stage we do
3520       --  not know what procedure is being called if the procedure might be
3521       --  overloaded, so it is premature to go setting referenced flags or
3522       --  making calls to Generate_Reference. We will wait till Resolve_Actuals
3523       --  for that processing
3524
3525       function Known_But_Invisible (E : Entity_Id) return Boolean;
3526       --  This function determines whether the entity E (which is not
3527       --  visible) can reasonably be considered to be known to the writer
3528       --  of the reference. This is a heuristic test, used only for the
3529       --  purposes of figuring out whether we prefer to complain that an
3530       --  entity is undefined or invisible (and identify the declaration
3531       --  of the invisible entity in the latter case). The point here is
3532       --  that we don't want to complain that something is invisible and
3533       --  then point to something entirely mysterious to the writer.
3534
3535       procedure Nvis_Messages;
3536       --  Called if there are no visible entries for N, but there is at least
3537       --  one non-directly visible, or hidden declaration. This procedure
3538       --  outputs an appropriate set of error messages.
3539
3540       procedure Undefined (Nvis : Boolean);
3541       --  This function is called if the current node has no corresponding
3542       --  visible entity or entities. The value set in Msg indicates whether
3543       --  an error message was generated (multiple error messages for the
3544       --  same variable are generally suppressed, see body for details).
3545       --  Msg is True if an error message was generated, False if not. This
3546       --  value is used by the caller to determine whether or not to output
3547       --  additional messages where appropriate. The parameter is set False
3548       --  to get the message "X is undefined", and True to get the message
3549       --  "X is not visible".
3550
3551       -------------------------
3552       -- From_Actual_Package --
3553       -------------------------
3554
3555       function From_Actual_Package (E : Entity_Id) return Boolean is
3556          Scop : constant Entity_Id := Scope (E);
3557          Act  : Entity_Id;
3558
3559       begin
3560          if not In_Instance then
3561             return False;
3562          else
3563             Inst := Current_Scope;
3564             while Present (Inst)
3565               and then Ekind (Inst) /= E_Package
3566               and then not Is_Generic_Instance (Inst)
3567             loop
3568                Inst := Scope (Inst);
3569             end loop;
3570
3571             if No (Inst) then
3572                return False;
3573             end if;
3574
3575             Act := First_Entity (Inst);
3576             while Present (Act) loop
3577                if Ekind (Act) = E_Package then
3578
3579                   --  Check for end of actuals list
3580
3581                   if Renamed_Object (Act) = Inst then
3582                      return False;
3583
3584                   elsif Present (Associated_Formal_Package (Act))
3585                     and then Renamed_Object (Act) = Scop
3586                   then
3587                      --  Entity comes from (instance of) formal package
3588
3589                      return True;
3590
3591                   else
3592                      Next_Entity (Act);
3593                   end if;
3594
3595                else
3596                   Next_Entity (Act);
3597                end if;
3598             end loop;
3599
3600             return False;
3601          end if;
3602       end From_Actual_Package;
3603
3604       -------------------------
3605       -- Is_Actual_Parameter --
3606       -------------------------
3607
3608       function Is_Actual_Parameter return Boolean is
3609       begin
3610          return
3611            Nkind (N) = N_Identifier
3612              and then
3613                (Nkind (Parent (N)) = N_Procedure_Call_Statement
3614                   or else
3615                     (Nkind (Parent (N)) = N_Parameter_Association
3616                        and then N = Explicit_Actual_Parameter (Parent (N))
3617                        and then Nkind (Parent (Parent (N))) =
3618                                           N_Procedure_Call_Statement));
3619       end Is_Actual_Parameter;
3620
3621       -------------------------
3622       -- Known_But_Invisible --
3623       -------------------------
3624
3625       function Known_But_Invisible (E : Entity_Id) return Boolean is
3626          Fname : File_Name_Type;
3627
3628       begin
3629          --  Entities in Standard are always considered to be known
3630
3631          if Sloc (E) <= Standard_Location then
3632             return True;
3633
3634          --  An entity that does not come from source is always considered
3635          --  to be unknown, since it is an artifact of code expansion.
3636
3637          elsif not Comes_From_Source (E) then
3638             return False;
3639
3640          --  In gnat internal mode, we consider all entities known
3641
3642          elsif GNAT_Mode then
3643             return True;
3644          end if;
3645
3646          --  Here we have an entity that is not from package Standard, and
3647          --  which comes from Source. See if it comes from an internal file.
3648
3649          Fname := Unit_File_Name (Get_Source_Unit (E));
3650
3651          --  Case of from internal file
3652
3653          if Is_Internal_File_Name (Fname) then
3654
3655             --  Private part entities in internal files are never considered
3656             --  to be known to the writer of normal application code.
3657
3658             if Is_Hidden (E) then
3659                return False;
3660             end if;
3661
3662             --  Entities from System packages other than System and
3663             --  System.Storage_Elements are not considered to be known.
3664             --  System.Auxxxx files are also considered known to the user.
3665
3666             --  Should refine this at some point to generally distinguish
3667             --  between known and unknown internal files ???
3668
3669             Get_Name_String (Fname);
3670
3671             return
3672               Name_Len < 2
3673                 or else
3674               Name_Buffer (1 .. 2) /= "s-"
3675                 or else
3676               Name_Buffer (3 .. 8) = "stoele"
3677                 or else
3678               Name_Buffer (3 .. 5) = "aux";
3679
3680          --  If not an internal file, then entity is definitely known,
3681          --  even if it is in a private part (the message generated will
3682          --  note that it is in a private part)
3683
3684          else
3685             return True;
3686          end if;
3687       end Known_But_Invisible;
3688
3689       -------------------
3690       -- Nvis_Messages --
3691       -------------------
3692
3693       procedure Nvis_Messages is
3694          Comp_Unit : Node_Id;
3695          Ent       : Entity_Id;
3696          Hidden    : Boolean := False;
3697          Item      : Node_Id;
3698
3699       begin
3700          --  Ada 2005 (AI-262): Generate a precise error concerning the
3701          --  Beaujolais effect that was previously detected
3702
3703          if Nvis_Is_Private_Subprg then
3704
3705             pragma Assert (Nkind (E2) = N_Defining_Identifier
3706                             and then Ekind (E2) = E_Function
3707                             and then Scope (E2) = Standard_Standard
3708                             and then Has_Private_With (E2));
3709
3710             --  Find the sloc corresponding to the private with'ed unit
3711
3712             Comp_Unit := Cunit (Current_Sem_Unit);
3713             Error_Msg_Sloc := No_Location;
3714
3715             Item := First (Context_Items (Comp_Unit));
3716             while Present (Item) loop
3717                if Nkind (Item) = N_With_Clause
3718                  and then Private_Present (Item)
3719                  and then Entity (Name (Item)) = E2
3720                then
3721                   Error_Msg_Sloc := Sloc (Item);
3722                   exit;
3723                end if;
3724
3725                Next (Item);
3726             end loop;
3727
3728             pragma Assert (Error_Msg_Sloc /= No_Location);
3729
3730             Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
3731             return;
3732          end if;
3733
3734          Undefined (Nvis => True);
3735
3736          if Msg then
3737
3738             --  First loop does hidden declarations
3739
3740             Ent := Homonyms;
3741             while Present (Ent) loop
3742                if Is_Potentially_Use_Visible (Ent) then
3743                   if not Hidden then
3744                      Error_Msg_N -- CODEFIX
3745                        ("multiple use clauses cause hiding!", N);
3746                      Hidden := True;
3747                   end if;
3748
3749                   Error_Msg_Sloc := Sloc (Ent);
3750                   Error_Msg_N ("hidden declaration#!", N);
3751                end if;
3752
3753                Ent := Homonym (Ent);
3754             end loop;
3755
3756             --  If we found hidden declarations, then that's enough, don't
3757             --  bother looking for non-visible declarations as well.
3758
3759             if Hidden then
3760                return;
3761             end if;
3762
3763             --  Second loop does non-directly visible declarations
3764
3765             Ent := Homonyms;
3766             while Present (Ent) loop
3767                if not Is_Potentially_Use_Visible (Ent) then
3768
3769                   --  Do not bother the user with unknown entities
3770
3771                   if not Known_But_Invisible (Ent) then
3772                      goto Continue;
3773                   end if;
3774
3775                   Error_Msg_Sloc := Sloc (Ent);
3776
3777                   --  Output message noting that there is a non-visible
3778                   --  declaration, distinguishing the private part case.
3779
3780                   if Is_Hidden (Ent) then
3781                      Error_Msg_N ("non-visible (private) declaration#!", N);
3782                   else
3783                      Error_Msg_N ("non-visible declaration#!", N);
3784
3785                      if Is_Compilation_Unit (Ent)
3786                        and then
3787                          Nkind (Parent (Parent (N))) = N_Use_Package_Clause
3788                      then
3789                         Error_Msg_Qual_Level := 99;
3790                         Error_Msg_NE ("\\missing `WITH &;`", N, Ent);
3791                         Error_Msg_Qual_Level := 0;
3792                      end if;
3793                   end if;
3794
3795                   --  Set entity and its containing package as referenced. We
3796                   --  can't be sure of this, but this seems a better choice
3797                   --  to avoid unused entity messages.
3798
3799                   if Comes_From_Source (Ent) then
3800                      Set_Referenced (Ent);
3801                      Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
3802                   end if;
3803                end if;
3804
3805                <<Continue>>
3806                Ent := Homonym (Ent);
3807             end loop;
3808          end if;
3809       end Nvis_Messages;
3810
3811       ---------------
3812       -- Undefined --
3813       ---------------
3814
3815       procedure Undefined (Nvis : Boolean) is
3816          Emsg : Error_Msg_Id;
3817
3818       begin
3819          --  We should never find an undefined internal name. If we do, then
3820          --  see if we have previous errors. If so, ignore on the grounds that
3821          --  it is probably a cascaded message (e.g. a block label from a badly
3822          --  formed block). If no previous errors, then we have a real internal
3823          --  error of some kind so raise an exception.
3824
3825          if Is_Internal_Name (Chars (N)) then
3826             if Total_Errors_Detected /= 0 then
3827                return;
3828             else
3829                raise Program_Error;
3830             end if;
3831          end if;
3832
3833          --  A very specialized error check, if the undefined variable is
3834          --  a case tag, and the case type is an enumeration type, check
3835          --  for a possible misspelling, and if so, modify the identifier
3836
3837          --  Named aggregate should also be handled similarly ???
3838
3839          if Nkind (N) = N_Identifier
3840            and then Nkind (Parent (N)) = N_Case_Statement_Alternative
3841          then
3842             declare
3843                Case_Stm : constant Node_Id   := Parent (Parent (N));
3844                Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
3845
3846                Lit : Node_Id;
3847
3848             begin
3849                if Is_Enumeration_Type (Case_Typ)
3850                  and then not Is_Standard_Character_Type (Case_Typ)
3851                then
3852                   Lit := First_Literal (Case_Typ);
3853                   Get_Name_String (Chars (Lit));
3854
3855                   if Chars (Lit) /= Chars (N)
3856                     and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit)) then
3857                      Error_Msg_Node_2 := Lit;
3858                      Error_Msg_N
3859                        ("& is undefined, assume misspelling of &", N);
3860                      Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
3861                      return;
3862                   end if;
3863
3864                   Lit := Next_Literal (Lit);
3865                end if;
3866             end;
3867          end if;
3868
3869          --  Normal processing
3870
3871          Set_Entity (N, Any_Id);
3872          Set_Etype  (N, Any_Type);
3873
3874          --  We use the table Urefs to keep track of entities for which we
3875          --  have issued errors for undefined references. Multiple errors
3876          --  for a single name are normally suppressed, however we modify
3877          --  the error message to alert the programmer to this effect.
3878
3879          for J in Urefs.First .. Urefs.Last loop
3880             if Chars (N) = Chars (Urefs.Table (J).Node) then
3881                if Urefs.Table (J).Err /= No_Error_Msg
3882                  and then Sloc (N) /= Urefs.Table (J).Loc
3883                then
3884                   Error_Msg_Node_1 := Urefs.Table (J).Node;
3885
3886                   if Urefs.Table (J).Nvis then
3887                      Change_Error_Text (Urefs.Table (J).Err,
3888                        "& is not visible (more references follow)");
3889                   else
3890                      Change_Error_Text (Urefs.Table (J).Err,
3891                        "& is undefined (more references follow)");
3892                   end if;
3893
3894                   Urefs.Table (J).Err := No_Error_Msg;
3895                end if;
3896
3897                --  Although we will set Msg False, and thus suppress the
3898                --  message, we also set Error_Posted True, to avoid any
3899                --  cascaded messages resulting from the undefined reference.
3900
3901                Msg := False;
3902                Set_Error_Posted (N, True);
3903                return;
3904             end if;
3905          end loop;
3906
3907          --  If entry not found, this is first undefined occurrence
3908
3909          if Nvis then
3910             Error_Msg_N ("& is not visible!", N);
3911             Emsg := Get_Msg_Id;
3912
3913          else
3914             Error_Msg_N ("& is undefined!", N);
3915             Emsg := Get_Msg_Id;
3916
3917             --  A very bizarre special check, if the undefined identifier
3918             --  is put or put_line, then add a special error message (since
3919             --  this is a very common error for beginners to make).
3920
3921             if Chars (N) = Name_Put or else Chars (N) = Name_Put_Line then
3922                Error_Msg_N
3923                  ("\\possible missing `WITH Ada.Text_'I'O; " &
3924                   "USE Ada.Text_'I'O`!", N);
3925
3926             --  Another special check if N is the prefix of a selected
3927             --  component which is a known unit, add message complaining
3928             --  about missing with for this unit.
3929
3930             elsif Nkind (Parent (N)) = N_Selected_Component
3931               and then N = Prefix (Parent (N))
3932               and then Is_Known_Unit (Parent (N))
3933             then
3934                Error_Msg_Node_2 := Selector_Name (Parent (N));
3935                Error_Msg_N ("\\missing `WITH &.&;`", Prefix (Parent (N)));
3936             end if;
3937
3938             --  Now check for possible misspellings
3939
3940             declare
3941                E      : Entity_Id;
3942                Ematch : Entity_Id := Empty;
3943
3944                Last_Name_Id : constant Name_Id :=
3945                                 Name_Id (Nat (First_Name_Id) +
3946                                            Name_Entries_Count - 1);
3947
3948             begin
3949                for Nam in First_Name_Id .. Last_Name_Id loop
3950                   E := Get_Name_Entity_Id (Nam);
3951
3952                   if Present (E)
3953                      and then (Is_Immediately_Visible (E)
3954                                  or else
3955                                Is_Potentially_Use_Visible (E))
3956                   then
3957                      if Is_Bad_Spelling_Of (Chars (N), Nam) then
3958                         Ematch := E;
3959                         exit;
3960                      end if;
3961                   end if;
3962                end loop;
3963
3964                if Present (Ematch) then
3965                   Error_Msg_NE -- CODEFIX
3966                     ("\possible misspelling of&", N, Ematch);
3967                end if;
3968             end;
3969          end if;
3970
3971          --  Make entry in undefined references table unless the full errors
3972          --  switch is set, in which case by refraining from generating the
3973          --  table entry, we guarantee that we get an error message for every
3974          --  undefined reference.
3975
3976          if not All_Errors_Mode then
3977             Urefs.Append (
3978               (Node => N,
3979                Err  => Emsg,
3980                Nvis => Nvis,
3981                Loc  => Sloc (N)));
3982          end if;
3983
3984          Msg := True;
3985       end Undefined;
3986
3987    --  Start of processing for Find_Direct_Name
3988
3989    begin
3990       --  If the entity pointer is already set, this is an internal node, or
3991       --  a node that is analyzed more than once, after a tree modification.
3992       --  In such a case there is no resolution to perform, just set the type.
3993
3994       if Present (Entity (N)) then
3995          if Is_Type (Entity (N)) then
3996             Set_Etype (N, Entity (N));
3997
3998          else
3999             declare
4000                Entyp : constant Entity_Id := Etype (Entity (N));
4001
4002             begin
4003                --  One special case here. If the Etype field is already set,
4004                --  and references the packed array type corresponding to the
4005                --  etype of the referenced entity, then leave it alone. This
4006                --  happens for trees generated from Exp_Pakd, where expressions
4007                --  can be deliberately "mis-typed" to the packed array type.
4008
4009                if Is_Array_Type (Entyp)
4010                  and then Is_Packed (Entyp)
4011                  and then Present (Etype (N))
4012                  and then Etype (N) = Packed_Array_Type (Entyp)
4013                then
4014                   null;
4015
4016                --  If not that special case, then just reset the Etype
4017
4018                else
4019                   Set_Etype (N, Etype (Entity (N)));
4020                end if;
4021             end;
4022          end if;
4023
4024          return;
4025       end if;
4026
4027       --  Here if Entity pointer was not set, we need full visibility analysis
4028       --  First we generate debugging output if the debug E flag is set.
4029
4030       if Debug_Flag_E then
4031          Write_Str ("Looking for ");
4032          Write_Name (Chars (N));
4033          Write_Eol;
4034       end if;
4035
4036       Homonyms := Current_Entity (N);
4037       Nvis_Entity := False;
4038
4039       E := Homonyms;
4040       while Present (E) loop
4041
4042          --  If entity is immediately visible or potentially use visible, then
4043          --  process the entity and we are done.
4044
4045          if Is_Immediately_Visible (E) then
4046             goto Immediately_Visible_Entity;
4047
4048          elsif Is_Potentially_Use_Visible (E) then
4049             goto Potentially_Use_Visible_Entity;
4050
4051          --  Note if a known but invisible entity encountered
4052
4053          elsif Known_But_Invisible (E) then
4054             Nvis_Entity := True;
4055          end if;
4056
4057          --  Move to next entity in chain and continue search
4058
4059          E := Homonym (E);
4060       end loop;
4061
4062       --  If no entries on homonym chain that were potentially visible,
4063       --  and no entities reasonably considered as non-visible, then
4064       --  we have a plain undefined reference, with no additional
4065       --  explanation required!
4066
4067       if not Nvis_Entity then
4068          Undefined (Nvis => False);
4069
4070       --  Otherwise there is at least one entry on the homonym chain that
4071       --  is reasonably considered as being known and non-visible.
4072
4073       else
4074          Nvis_Messages;
4075       end if;
4076
4077       return;
4078
4079       --  Processing for a potentially use visible entry found. We must search
4080       --  the rest of the homonym chain for two reasons. First, if there is a
4081       --  directly visible entry, then none of the potentially use-visible
4082       --  entities are directly visible (RM 8.4(10)). Second, we need to check
4083       --  for the case of multiple potentially use-visible entries hiding one
4084       --  another and as a result being non-directly visible (RM 8.4(11)).
4085
4086       <<Potentially_Use_Visible_Entity>> declare
4087          Only_One_Visible : Boolean := True;
4088          All_Overloadable : Boolean := Is_Overloadable (E);
4089
4090       begin
4091          E2 := Homonym (E);
4092          while Present (E2) loop
4093             if Is_Immediately_Visible (E2) then
4094
4095                --  If the use-visible entity comes from the actual for a
4096                --  formal package, it hides a directly visible entity from
4097                --  outside the instance.
4098
4099                if From_Actual_Package (E)
4100                  and then Scope_Depth (E2) < Scope_Depth (Inst)
4101                then
4102                   goto Found;
4103                else
4104                   E := E2;
4105                   goto Immediately_Visible_Entity;
4106                end if;
4107
4108             elsif Is_Potentially_Use_Visible (E2) then
4109                Only_One_Visible := False;
4110                All_Overloadable := All_Overloadable and Is_Overloadable (E2);
4111
4112             --  Ada 2005 (AI-262): Protect against a form of Beaujolais effect
4113             --  that can occur in private_with clauses. Example:
4114
4115             --    with A;
4116             --    private with B;              package A is
4117             --    package C is                   function B return Integer;
4118             --      use A;                     end A;
4119             --      V1 : Integer := B;
4120             --    private                      function B return Integer;
4121             --      V2 : Integer := B;
4122             --    end C;
4123
4124             --  V1 resolves to A.B, but V2 resolves to library unit B
4125
4126             elsif Ekind (E2) = E_Function
4127               and then Scope (E2) = Standard_Standard
4128               and then Has_Private_With (E2)
4129             then
4130                Only_One_Visible       := False;
4131                All_Overloadable       := False;
4132                Nvis_Is_Private_Subprg := True;
4133                exit;
4134             end if;
4135
4136             E2 := Homonym (E2);
4137          end loop;
4138
4139          --  On falling through this loop, we have checked that there are no
4140          --  immediately visible entities. Only_One_Visible is set if exactly
4141          --  one potentially use visible entity exists. All_Overloadable is
4142          --  set if all the potentially use visible entities are overloadable.
4143          --  The condition for legality is that either there is one potentially
4144          --  use visible entity, or if there is more than one, then all of them
4145          --  are overloadable.
4146
4147          if Only_One_Visible or All_Overloadable then
4148             goto Found;
4149
4150          --  If there is more than one potentially use-visible entity and at
4151          --  least one of them non-overloadable, we have an error (RM 8.4(11).
4152          --  Note that E points to the first such entity on the homonym list.
4153          --  Special case: if one of the entities is declared in an actual
4154          --  package, it was visible in the generic, and takes precedence over
4155          --  other entities that are potentially use-visible. Same if it is
4156          --  declared in a local instantiation of the current instance.
4157
4158          else
4159             if In_Instance then
4160
4161                --  Find current instance
4162
4163                Inst := Current_Scope;
4164                while Present (Inst)
4165                  and then Inst /= Standard_Standard
4166                loop
4167                   if Is_Generic_Instance (Inst) then
4168                      exit;
4169                   end if;
4170
4171                   Inst := Scope (Inst);
4172                end loop;
4173
4174                E2 := E;
4175                while Present (E2) loop
4176                   if From_Actual_Package (E2)
4177                     or else
4178                       (Is_Generic_Instance (Scope (E2))
4179                         and then Scope_Depth (Scope (E2)) > Scope_Depth (Inst))
4180                   then
4181                      E := E2;
4182                      goto Found;
4183                   end if;
4184
4185                   E2 := Homonym (E2);
4186                end loop;
4187
4188                Nvis_Messages;
4189                return;
4190
4191             elsif
4192               Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
4193             then
4194                --  A use-clause in the body of a system file creates conflict
4195                --  with some entity in a user scope, while rtsfind is active.
4196                --  Keep only the entity coming from another predefined unit.
4197
4198                E2 := E;
4199                while Present (E2) loop
4200                   if Is_Predefined_File_Name
4201                     (Unit_File_Name (Get_Source_Unit (Sloc (E2))))
4202                   then
4203                      E := E2;
4204                      goto Found;
4205                   end if;
4206
4207                   E2 := Homonym (E2);
4208                end loop;
4209
4210                --  Entity must exist because predefined unit is correct
4211
4212                raise Program_Error;
4213
4214             else
4215                Nvis_Messages;
4216                return;
4217             end if;
4218          end if;
4219       end;
4220
4221       --  Come here with E set to the first immediately visible entity on
4222       --  the homonym chain. This is the one we want unless there is another
4223       --  immediately visible entity further on in the chain for an inner
4224       --  scope (RM 8.3(8)).
4225
4226       <<Immediately_Visible_Entity>> declare
4227          Level : Int;
4228          Scop  : Entity_Id;
4229
4230       begin
4231          --  Find scope level of initial entity. When compiling through
4232          --  Rtsfind, the previous context is not completely invisible, and
4233          --  an outer entity may appear on the chain, whose scope is below
4234          --  the entry for Standard that delimits the current scope stack.
4235          --  Indicate that the level for this spurious entry is outside of
4236          --  the current scope stack.
4237
4238          Level := Scope_Stack.Last;
4239          loop
4240             Scop := Scope_Stack.Table (Level).Entity;
4241             exit when Scop = Scope (E);
4242             Level := Level - 1;
4243             exit when Scop = Standard_Standard;
4244          end loop;
4245
4246          --  Now search remainder of homonym chain for more inner entry
4247          --  If the entity is Standard itself, it has no scope, and we
4248          --  compare it with the stack entry directly.
4249
4250          E2 := Homonym (E);
4251          while Present (E2) loop
4252             if Is_Immediately_Visible (E2) then
4253
4254                --  If a generic package contains a local declaration that
4255                --  has the same name as the generic, there may be a visibility
4256                --  conflict in an instance, where the local declaration must
4257                --  also hide the name of the corresponding package renaming.
4258                --  We check explicitly for a package declared by a renaming,
4259                --  whose renamed entity is an instance that is on the scope
4260                --  stack, and that contains a homonym in the same scope. Once
4261                --  we have found it, we know that the package renaming is not
4262                --  immediately visible, and that the identifier denotes the
4263                --  other entity (and its homonyms if overloaded).
4264
4265                if Scope (E) = Scope (E2)
4266                  and then Ekind (E) = E_Package
4267                  and then Present (Renamed_Object (E))
4268                  and then Is_Generic_Instance (Renamed_Object (E))
4269                  and then In_Open_Scopes (Renamed_Object (E))
4270                  and then Comes_From_Source (N)
4271                then
4272                   Set_Is_Immediately_Visible (E, False);
4273                   E := E2;
4274
4275                else
4276                   for J in Level + 1 .. Scope_Stack.Last loop
4277                      if Scope_Stack.Table (J).Entity = Scope (E2)
4278                        or else Scope_Stack.Table (J).Entity = E2
4279                      then
4280                         Level := J;
4281                         E := E2;
4282                         exit;
4283                      end if;
4284                   end loop;
4285                end if;
4286             end if;
4287
4288             E2 := Homonym (E2);
4289          end loop;
4290
4291          --  At the end of that loop, E is the innermost immediately
4292          --  visible entity, so we are all set.
4293       end;
4294
4295       --  Come here with entity found, and stored in E
4296
4297       <<Found>> begin
4298
4299          --  When distribution features are available (Get_PCS_Name /=
4300          --  Name_No_DSA), a remote access-to-subprogram type is converted
4301          --  into a record type holding whatever information is needed to
4302          --  perform a remote call on an RCI subprogram. In that case we
4303          --  rewrite any occurrence of the RAS type into the equivalent record
4304          --  type here. 'Access attribute references and RAS dereferences are
4305          --  then implemented using specific TSSs. However when distribution is
4306          --  not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
4307          --  generation of these TSSs, and we must keep the RAS type in its
4308          --  original access-to-subprogram form (since all calls through a
4309          --  value of such type will be local anyway in the absence of a PCS).
4310
4311          if Comes_From_Source (N)
4312            and then Is_Remote_Access_To_Subprogram_Type (E)
4313            and then Expander_Active
4314            and then Get_PCS_Name /= Name_No_DSA
4315          then
4316             Rewrite (N,
4317               New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
4318             return;
4319          end if;
4320
4321          Set_Entity (N, E);
4322          --  Why no Style_Check here???
4323
4324          if Is_Type (E) then
4325             Set_Etype (N, E);
4326          else
4327             Set_Etype (N, Get_Full_View (Etype (E)));
4328          end if;
4329
4330          if Debug_Flag_E then
4331             Write_Str (" found  ");
4332             Write_Entity_Info (E, "      ");
4333          end if;
4334
4335          --  If the Ekind of the entity is Void, it means that all homonyms
4336          --  are hidden from all visibility (RM 8.3(5,14-20)). However, this
4337          --  test is skipped if the current scope is a record and the name is
4338          --  a pragma argument expression (case of Atomic and Volatile pragmas
4339          --  and possibly other similar pragmas added later, which are allowed
4340          --  to reference components in the current record).
4341
4342          if Ekind (E) = E_Void
4343            and then
4344              (not Is_Record_Type (Current_Scope)
4345                or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
4346          then
4347             Premature_Usage (N);
4348
4349          --  If the entity is overloadable, collect all interpretations of the
4350          --  name for subsequent overload resolution. We optimize a bit here to
4351          --  do this only if we have an overloadable entity that is not on its
4352          --  own on the homonym chain.
4353
4354          elsif Is_Overloadable (E)
4355            and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
4356          then
4357             Collect_Interps (N);
4358
4359             --  If no homonyms were visible, the entity is unambiguous
4360
4361             if not Is_Overloaded (N) then
4362                if not Is_Actual_Parameter then
4363                   Generate_Reference (E, N);
4364                end if;
4365             end if;
4366
4367          --  Case of non-overloadable entity, set the entity providing that
4368          --  we do not have the case of a discriminant reference within a
4369          --  default expression. Such references are replaced with the
4370          --  corresponding discriminal, which is the formal corresponding to
4371          --  to the discriminant in the initialization procedure.
4372
4373          else
4374             --  Entity is unambiguous, indicate that it is referenced here
4375
4376             --  For a renaming of an object, always generate simple reference,
4377             --  we don't try to keep track of assignments in this case.
4378
4379             if Is_Object (E) and then Present (Renamed_Object (E)) then
4380                Generate_Reference (E, N);
4381
4382                --  If the renamed entity is a private protected component,
4383                --  reference the original component as well. This needs to be
4384                --  done because the private renamings are installed before any
4385                --  analysis has occurred. Reference to a private component will
4386                --  resolve to the renaming and the original component will be
4387                --  left unreferenced, hence the following.
4388
4389                if Is_Prival (E) then
4390                   Generate_Reference (Prival_Link (E), N);
4391                end if;
4392
4393             --  One odd case is that we do not want to set the Referenced flag
4394             --  if the entity is a label, and the identifier is the label in
4395             --  the source, since this is not a reference from the point of
4396             --  view of the user.
4397
4398             elsif Nkind (Parent (N)) = N_Label then
4399                declare
4400                   R : constant Boolean := Referenced (E);
4401
4402                begin
4403                   --  Generate reference unless this is an actual parameter
4404                   --  (see comment below)
4405
4406                   if Is_Actual_Parameter then
4407                      Generate_Reference (E, N);
4408                      Set_Referenced (E, R);
4409                   end if;
4410                end;
4411
4412             --  Normal case, not a label: generate reference
4413
4414             --  ??? It is too early to generate a reference here even if
4415             --    the entity is unambiguous, because the tree is not
4416             --    sufficiently typed at this point for Generate_Reference to
4417             --    determine whether this reference modifies the denoted object
4418             --    (because implicit dereferences cannot be identified prior to
4419             --    full type resolution).
4420             --
4421             --    The Is_Actual_Parameter routine takes care of one of these
4422             --    cases but there are others probably ???
4423
4424             else
4425                if not Is_Actual_Parameter then
4426                   Generate_Reference (E, N);
4427                end if;
4428
4429                Check_Nested_Access (E);
4430             end if;
4431
4432             --  Set Entity, with style check if need be. For a discriminant
4433             --  reference, replace by the corresponding discriminal, i.e. the
4434             --  parameter of the initialization procedure that corresponds to
4435             --  the discriminant. If this replacement is being performed, there
4436             --  is no style check to perform.
4437
4438             --  This replacement must not be done if we are currently
4439             --  processing a generic spec or body, because the discriminal
4440             --  has not been not generated in this case.
4441
4442             --  The replacement is also skipped if we are in special
4443             --  spec-expression mode. Why is this skipped in this case ???
4444
4445             if not In_Spec_Expression
4446               or else Ekind (E) /= E_Discriminant
4447               or else Inside_A_Generic
4448             then
4449                Set_Entity_With_Style_Check (N, E);
4450
4451             --  The replacement is not done either for a task discriminant that
4452             --  appears in a default expression of an entry parameter. See
4453             --  Expand_Discriminant in exp_ch2 for details on their handling.
4454
4455             elsif Is_Concurrent_Type (Scope (E)) then
4456                declare
4457                   P : Node_Id;
4458
4459                begin
4460                   P := Parent (N);
4461                   while Present (P)
4462                     and then not Nkind_In (P, N_Parameter_Specification,
4463                                               N_Component_Declaration)
4464                   loop
4465                      P := Parent (P);
4466                   end loop;
4467
4468                   if Present (P)
4469                      and then Nkind (P) = N_Parameter_Specification
4470                   then
4471                      null;
4472                   else
4473                      Set_Entity (N, Discriminal (E));
4474                   end if;
4475                end;
4476
4477             --  Otherwise, this is a discriminant in a context in which
4478             --  it is a reference to the corresponding parameter of the
4479             --  init proc for the enclosing type.
4480
4481             else
4482                Set_Entity (N, Discriminal (E));
4483             end if;
4484          end if;
4485       end;
4486    end Find_Direct_Name;
4487
4488    ------------------------
4489    -- Find_Expanded_Name --
4490    ------------------------
4491
4492    --  This routine searches the homonym chain of the entity until it finds
4493    --  an entity declared in the scope denoted by the prefix. If the entity
4494    --  is private, it may nevertheless be immediately visible, if we are in
4495    --  the scope of its declaration.
4496
4497    procedure Find_Expanded_Name (N : Node_Id) is
4498       Selector  : constant Node_Id := Selector_Name (N);
4499       Candidate : Entity_Id        := Empty;
4500       P_Name    : Entity_Id;
4501       O_Name    : Entity_Id;
4502       Id        : Entity_Id;
4503
4504    begin
4505       P_Name := Entity (Prefix (N));
4506       O_Name := P_Name;
4507
4508       --  If the prefix is a renamed package, look for the entity in the
4509       --  original package.
4510
4511       if Ekind (P_Name) = E_Package
4512         and then Present (Renamed_Object (P_Name))
4513       then
4514          P_Name := Renamed_Object (P_Name);
4515
4516          --  Rewrite node with entity field pointing to renamed object
4517
4518          Rewrite (Prefix (N), New_Copy (Prefix (N)));
4519          Set_Entity (Prefix (N), P_Name);
4520
4521       --  If the prefix is an object of a concurrent type, look for
4522       --  the entity in the associated task or protected type.
4523
4524       elsif Is_Concurrent_Type (Etype (P_Name)) then
4525          P_Name := Etype (P_Name);
4526       end if;
4527
4528       Id := Current_Entity (Selector);
4529
4530       declare
4531          Is_New_Candidate : Boolean;
4532
4533       begin
4534          while Present (Id) loop
4535             if Scope (Id) = P_Name then
4536                Candidate        := Id;
4537                Is_New_Candidate := True;
4538
4539             --  Ada 2005 (AI-217): Handle shadow entities associated with types
4540             --  declared in limited-withed nested packages. We don't need to
4541             --  handle E_Incomplete_Subtype entities because the entities in
4542             --  the limited view are always E_Incomplete_Type entities (see
4543             --  Build_Limited_Views). Regarding the expression used to evaluate
4544             --  the scope, it is important to note that the limited view also
4545             --  has shadow entities associated nested packages. For this reason
4546             --  the correct scope of the entity is the scope of the real entity
4547             --  The non-limited view may itself be incomplete, in which case
4548             --  get the full view if available.
4549
4550             elsif From_With_Type (Id)
4551               and then Is_Type (Id)
4552               and then Ekind (Id) = E_Incomplete_Type
4553               and then Present (Non_Limited_View (Id))
4554               and then Scope (Non_Limited_View (Id)) = P_Name
4555             then
4556                Candidate        := Get_Full_View (Non_Limited_View (Id));
4557                Is_New_Candidate := True;
4558
4559             else
4560                Is_New_Candidate := False;
4561             end if;
4562
4563             if Is_New_Candidate then
4564                if Is_Child_Unit (Id) then
4565                   exit when Is_Visible_Child_Unit (Id)
4566                     or else Is_Immediately_Visible (Id);
4567
4568                else
4569                   exit when not Is_Hidden (Id)
4570                     or else Is_Immediately_Visible (Id);
4571                end if;
4572             end if;
4573
4574             Id := Homonym (Id);
4575          end loop;
4576       end;
4577
4578       if No (Id)
4579         and then (Ekind (P_Name) = E_Procedure
4580                     or else
4581                   Ekind (P_Name) = E_Function)
4582         and then Is_Generic_Instance (P_Name)
4583       then
4584          --  Expanded name denotes entity in (instance of) generic subprogram.
4585          --  The entity may be in the subprogram instance, or may denote one of
4586          --  the formals, which is declared in the enclosing wrapper package.
4587
4588          P_Name := Scope (P_Name);
4589
4590          Id := Current_Entity (Selector);
4591          while Present (Id) loop
4592             exit when Scope (Id) = P_Name;
4593             Id := Homonym (Id);
4594          end loop;
4595       end if;
4596
4597       if No (Id) or else Chars (Id) /= Chars (Selector) then
4598          Set_Etype (N, Any_Type);
4599
4600          --  If we are looking for an entity defined in System, try to find it
4601          --  in the child package that may have been provided as an extension
4602          --  to System. The Extend_System pragma will have supplied the name of
4603          --  the extension, which may have to be loaded.
4604
4605          if Chars (P_Name) = Name_System
4606            and then Scope (P_Name) = Standard_Standard
4607            and then Present (System_Extend_Unit)
4608            and then Present_System_Aux (N)
4609          then
4610             Set_Entity (Prefix (N), System_Aux_Id);
4611             Find_Expanded_Name (N);
4612             return;
4613
4614          elsif Nkind (Selector) = N_Operator_Symbol
4615            and then Has_Implicit_Operator (N)
4616          then
4617             --  There is an implicit instance of the predefined operator in
4618             --  the given scope. The operator entity is defined in Standard.
4619             --  Has_Implicit_Operator makes the node into an Expanded_Name.
4620
4621             return;
4622
4623          elsif Nkind (Selector) = N_Character_Literal
4624            and then Has_Implicit_Character_Literal (N)
4625          then
4626             --  If there is no literal defined in the scope denoted by the
4627             --  prefix, the literal may belong to (a type derived from)
4628             --  Standard_Character, for which we have no explicit literals.
4629
4630             return;
4631
4632          else
4633             --  If the prefix is a single concurrent object, use its name in
4634             --  the error message, rather than that of the anonymous type.
4635
4636             if Is_Concurrent_Type (P_Name)
4637               and then Is_Internal_Name (Chars (P_Name))
4638             then
4639                Error_Msg_Node_2 := Entity (Prefix (N));
4640             else
4641                Error_Msg_Node_2 := P_Name;
4642             end if;
4643
4644             if P_Name = System_Aux_Id then
4645                P_Name := Scope (P_Name);
4646                Set_Entity (Prefix (N), P_Name);
4647             end if;
4648
4649             if Present (Candidate) then
4650
4651                --  If we know that the unit is a child unit we can give a more
4652                --  accurate error message.
4653
4654                if Is_Child_Unit (Candidate) then
4655
4656                   --  If the candidate is a private child unit and we are in
4657                   --  the visible part of a public unit, specialize the error
4658                   --  message. There might be a private with_clause for it,
4659                   --  but it is not currently active.
4660
4661                   if Is_Private_Descendant (Candidate)
4662                     and then Ekind (Current_Scope) = E_Package
4663                     and then not In_Private_Part (Current_Scope)
4664                     and then not Is_Private_Descendant (Current_Scope)
4665                   then
4666                      Error_Msg_N ("private child unit& is not visible here",
4667                                   Selector);
4668
4669                   --  Normal case where we have a missing with for a child unit
4670
4671                   else
4672                      Error_Msg_Qual_Level := 99;
4673                      Error_Msg_NE ("missing `WITH &;`", Selector, Candidate);
4674                      Error_Msg_Qual_Level := 0;
4675                   end if;
4676
4677                   --  Here we don't know that this is a child unit
4678
4679                else
4680                   Error_Msg_NE ("& is not a visible entity of&", N, Selector);
4681                end if;
4682
4683             else
4684                --  Within the instantiation of a child unit, the prefix may
4685                --  denote the parent instance, but the selector has the name
4686                --  of the original child. Find whether we are within the
4687                --  corresponding instance, and get the proper entity, which
4688                --  can only be an enclosing scope.
4689
4690                if O_Name /= P_Name
4691                  and then In_Open_Scopes (P_Name)
4692                  and then Is_Generic_Instance (P_Name)
4693                then
4694                   declare
4695                      S : Entity_Id := Current_Scope;
4696                      P : Entity_Id;
4697
4698                   begin
4699                      for J in reverse 0 .. Scope_Stack.Last loop
4700                         S := Scope_Stack.Table (J).Entity;
4701
4702                         exit when S = Standard_Standard;
4703
4704                         if Ekind (S) = E_Function
4705                           or else Ekind (S) = E_Package
4706                           or else Ekind (S) = E_Procedure
4707                         then
4708                            P := Generic_Parent (Specification
4709                                   (Unit_Declaration_Node (S)));
4710
4711                            if Present (P)
4712                              and then Chars (Scope (P)) = Chars (O_Name)
4713                              and then Chars (P) = Chars (Selector)
4714                            then
4715                               Id := S;
4716                               goto Found;
4717                            end if;
4718                         end if;
4719
4720                      end loop;
4721                   end;
4722                end if;
4723
4724                --  If this is a selection from Ada, System or Interfaces, then
4725                --  we assume a missing with for the corresponding package.
4726
4727                if Is_Known_Unit (N) then
4728                   if not Error_Posted (N) then
4729                      Error_Msg_Node_2 := Selector;
4730                      Error_Msg_N ("missing `WITH &.&;`", Prefix (N));
4731                   end if;
4732
4733                --  If this is a selection from a dummy package, then suppress
4734                --  the error message, of course the entity is missing if the
4735                --  package is missing!
4736
4737                elsif Sloc (Error_Msg_Node_2) = No_Location then
4738                   null;
4739
4740                --  Here we have the case of an undefined component
4741
4742                else
4743                   Error_Msg_NE ("& not declared in&", N, Selector);
4744
4745                   --  Check for misspelling of some entity in prefix
4746
4747                   Id := First_Entity (P_Name);
4748                   while Present (Id) loop
4749                      if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
4750                        and then not Is_Internal_Name (Chars (Id))
4751                      then
4752                         Error_Msg_NE -- CODEFIX
4753                           ("possible misspelling of&", Selector, Id);
4754                         exit;
4755                      end if;
4756
4757                      Next_Entity (Id);
4758                   end loop;
4759
4760                   --  Specialize the message if this may be an instantiation
4761                   --  of a child unit that was not mentioned in the context.
4762
4763                   if Nkind (Parent (N)) = N_Package_Instantiation
4764                     and then Is_Generic_Instance (Entity (Prefix (N)))
4765                     and then Is_Compilation_Unit
4766                                (Generic_Parent (Parent (Entity (Prefix (N)))))
4767                   then
4768                      Error_Msg_Node_2 := Selector;
4769                      Error_Msg_N ("\missing `WITH &.&;`", Prefix (N));
4770                   end if;
4771                end if;
4772             end if;
4773
4774             Id := Any_Id;
4775          end if;
4776       end if;
4777
4778       <<Found>>
4779       if Comes_From_Source (N)
4780         and then Is_Remote_Access_To_Subprogram_Type (Id)
4781         and then Present (Equivalent_Type (Id))
4782       then
4783          --  If we are not actually generating distribution code (i.e. the
4784          --  current PCS is the dummy non-distributed version), then the
4785          --  Equivalent_Type will be missing, and Id should be treated as
4786          --  a regular access-to-subprogram type.
4787
4788          Id := Equivalent_Type (Id);
4789          Set_Chars (Selector, Chars (Id));
4790       end if;
4791
4792       --  Ada 2005 (AI-50217): Check usage of entities in limited withed units
4793
4794       if Ekind (P_Name) = E_Package
4795         and then From_With_Type (P_Name)
4796       then
4797          if From_With_Type (Id)
4798            or else Is_Type (Id)
4799            or else Ekind (Id) = E_Package
4800          then
4801             null;
4802          else
4803             Error_Msg_N
4804               ("limited withed package can only be used to access "
4805                & "incomplete types",
4806                 N);
4807          end if;
4808       end if;
4809
4810       if Is_Task_Type (P_Name)
4811         and then ((Ekind (Id) = E_Entry
4812                      and then Nkind (Parent (N)) /= N_Attribute_Reference)
4813                    or else
4814                     (Ekind (Id) = E_Entry_Family
4815                       and then
4816                         Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
4817       then
4818          --  It is an entry call after all, either to the current task (which
4819          --  will deadlock) or to an enclosing task.
4820
4821          Analyze_Selected_Component (N);
4822          return;
4823       end if;
4824
4825       Change_Selected_Component_To_Expanded_Name (N);
4826
4827       --  Do style check and generate reference, but skip both steps if this
4828       --  entity has homonyms, since we may not have the right homonym set yet.
4829       --  The proper homonym will be set during the resolve phase.
4830
4831       if Has_Homonym (Id) then
4832          Set_Entity (N, Id);
4833       else
4834          Set_Entity_With_Style_Check (N, Id);
4835          Generate_Reference (Id, N);
4836       end if;
4837
4838       if Is_Type (Id) then
4839          Set_Etype (N, Id);
4840       else
4841          Set_Etype (N, Get_Full_View (Etype (Id)));
4842       end if;
4843
4844       --  If the Ekind of the entity is Void, it means that all homonyms are
4845       --  hidden from all visibility (RM 8.3(5,14-20)).
4846
4847       if Ekind (Id) = E_Void then
4848          Premature_Usage (N);
4849
4850       elsif Is_Overloadable (Id)
4851         and then Present (Homonym (Id))
4852       then
4853          declare
4854             H : Entity_Id := Homonym (Id);
4855
4856          begin
4857             while Present (H) loop
4858                if Scope (H) = Scope (Id)
4859                  and then
4860                    (not Is_Hidden (H)
4861                       or else Is_Immediately_Visible (H))
4862                then
4863                   Collect_Interps (N);
4864                   exit;
4865                end if;
4866
4867                H := Homonym (H);
4868             end loop;
4869
4870             --  If an extension of System is present, collect possible explicit
4871             --  overloadings declared in the extension.
4872
4873             if Chars (P_Name) = Name_System
4874               and then Scope (P_Name) = Standard_Standard
4875               and then Present (System_Extend_Unit)
4876               and then Present_System_Aux (N)
4877             then
4878                H := Current_Entity (Id);
4879
4880                while Present (H) loop
4881                   if Scope (H) = System_Aux_Id then
4882                      Add_One_Interp (N, H, Etype (H));
4883                   end if;
4884
4885                   H := Homonym (H);
4886                end loop;
4887             end if;
4888          end;
4889       end if;
4890
4891       if Nkind (Selector_Name (N)) = N_Operator_Symbol
4892         and then Scope (Id) /= Standard_Standard
4893       then
4894          --  In addition to user-defined operators in the given scope, there
4895          --  may be an implicit instance of the predefined operator. The
4896          --  operator (defined in Standard) is found in Has_Implicit_Operator,
4897          --  and added to the interpretations. Procedure Add_One_Interp will
4898          --  determine which hides which.
4899
4900          if Has_Implicit_Operator (N) then
4901             null;
4902          end if;
4903       end if;
4904    end Find_Expanded_Name;
4905
4906    -------------------------
4907    -- Find_Renamed_Entity --
4908    -------------------------
4909
4910    function Find_Renamed_Entity
4911      (N         : Node_Id;
4912       Nam       : Node_Id;
4913       New_S     : Entity_Id;
4914       Is_Actual : Boolean := False) return Entity_Id
4915    is
4916       Ind   : Interp_Index;
4917       I1    : Interp_Index := 0; -- Suppress junk warnings
4918       It    : Interp;
4919       It1   : Interp;
4920       Old_S : Entity_Id;
4921       Inst  : Entity_Id;
4922
4923       function Enclosing_Instance return Entity_Id;
4924       --  If the renaming determines the entity for the default of a formal
4925       --  subprogram nested within another instance, choose the innermost
4926       --  candidate. This is because if the formal has a box, and we are within
4927       --  an enclosing instance where some candidate interpretations are local
4928       --  to this enclosing instance, we know that the default was properly
4929       --  resolved when analyzing the generic, so we prefer the local
4930       --  candidates to those that are external. This is not always the case
4931       --  but is a reasonable heuristic on the use of nested generics. The
4932       --  proper solution requires a full renaming model.
4933
4934       function Is_Visible_Operation (Op : Entity_Id) return Boolean;
4935       --  If the renamed entity is an implicit operator, check whether it is
4936       --  visible because its operand type is properly visible. This check
4937       --  applies to explicit renamed entities that appear in the source in a
4938       --  renaming declaration or a formal subprogram instance, but not to
4939       --  default generic actuals with a name.
4940
4941       function Report_Overload return Entity_Id;
4942       --  List possible interpretations, and specialize message in the
4943       --  case of a generic actual.
4944
4945       function Within (Inner, Outer : Entity_Id) return Boolean;
4946       --  Determine whether a candidate subprogram is defined within the
4947       --  enclosing instance. If yes, it has precedence over outer candidates.
4948
4949       ------------------------
4950       -- Enclosing_Instance --
4951       ------------------------
4952
4953       function Enclosing_Instance return Entity_Id is
4954          S : Entity_Id;
4955
4956       begin
4957          if not Is_Generic_Instance (Current_Scope)
4958            and then not Is_Actual
4959          then
4960             return Empty;
4961          end if;
4962
4963          S := Scope (Current_Scope);
4964          while S /= Standard_Standard loop
4965             if Is_Generic_Instance (S) then
4966                return S;
4967             end if;
4968
4969             S := Scope (S);
4970          end loop;
4971
4972          return Empty;
4973       end Enclosing_Instance;
4974
4975       --------------------------
4976       -- Is_Visible_Operation --
4977       --------------------------
4978
4979       function Is_Visible_Operation (Op : Entity_Id) return Boolean is
4980          Scop : Entity_Id;
4981          Typ  : Entity_Id;
4982          Btyp : Entity_Id;
4983
4984       begin
4985          if Ekind (Op) /= E_Operator
4986            or else Scope (Op) /= Standard_Standard
4987            or else (In_Instance
4988                       and then
4989                         (not Is_Actual
4990                            or else Present (Enclosing_Instance)))
4991          then
4992             return True;
4993
4994          else
4995             --  For a fixed point type operator, check the resulting type,
4996             --  because it may be a mixed mode integer * fixed operation.
4997
4998             if Present (Next_Formal (First_Formal (New_S)))
4999               and then Is_Fixed_Point_Type (Etype (New_S))
5000             then
5001                Typ := Etype (New_S);
5002             else
5003                Typ := Etype (First_Formal (New_S));
5004             end if;
5005
5006             Btyp := Base_Type (Typ);
5007
5008             if Nkind (Nam) /= N_Expanded_Name then
5009                return (In_Open_Scopes (Scope (Btyp))
5010                         or else Is_Potentially_Use_Visible (Btyp)
5011                         or else In_Use (Btyp)
5012                         or else In_Use (Scope (Btyp)));
5013
5014             else
5015                Scop := Entity (Prefix (Nam));
5016
5017                if Ekind (Scop) = E_Package
5018                  and then Present (Renamed_Object (Scop))
5019                then
5020                   Scop := Renamed_Object (Scop);
5021                end if;
5022
5023                --  Operator is visible if prefix of expanded name denotes
5024                --  scope of type, or else type is defined in System_Aux
5025                --  and the prefix denotes System.
5026
5027                return Scope (Btyp) = Scop
5028                  or else (Scope (Btyp) = System_Aux_Id
5029                            and then Scope (Scope (Btyp)) = Scop);
5030             end if;
5031          end if;
5032       end Is_Visible_Operation;
5033
5034       ------------
5035       -- Within --
5036       ------------
5037
5038       function Within (Inner, Outer : Entity_Id) return Boolean is
5039          Sc : Entity_Id;
5040
5041       begin
5042          Sc := Scope (Inner);
5043          while Sc /= Standard_Standard loop
5044             if Sc = Outer then
5045                return True;
5046             else
5047                Sc := Scope (Sc);
5048             end if;
5049          end loop;
5050
5051          return False;
5052       end Within;
5053
5054       ---------------------
5055       -- Report_Overload --
5056       ---------------------
5057
5058       function Report_Overload return Entity_Id is
5059       begin
5060          if Is_Actual then
5061             Error_Msg_NE
5062               ("ambiguous actual subprogram&, " &
5063                  "possible interpretations:", N, Nam);
5064          else
5065             Error_Msg_N
5066               ("ambiguous subprogram, " &
5067                  "possible interpretations:", N);
5068          end if;
5069
5070          List_Interps (Nam, N);
5071          return Old_S;
5072       end Report_Overload;
5073
5074    --  Start of processing for Find_Renamed_Entry
5075
5076    begin
5077       Old_S := Any_Id;
5078       Candidate_Renaming := Empty;
5079
5080       if not Is_Overloaded (Nam) then
5081          if Entity_Matches_Spec (Entity (Nam), New_S) then
5082             Candidate_Renaming := New_S;
5083
5084             if Is_Visible_Operation (Entity (Nam)) then
5085                Old_S := Entity (Nam);
5086             end if;
5087
5088          elsif
5089            Present (First_Formal (Entity (Nam)))
5090              and then Present (First_Formal (New_S))
5091              and then (Base_Type (Etype (First_Formal (Entity (Nam))))
5092                         = Base_Type (Etype (First_Formal (New_S))))
5093          then
5094             Candidate_Renaming := Entity (Nam);
5095          end if;
5096
5097       else
5098          Get_First_Interp (Nam, Ind, It);
5099          while Present (It.Nam) loop
5100             if Entity_Matches_Spec (It.Nam, New_S)
5101                and then Is_Visible_Operation (It.Nam)
5102             then
5103                if Old_S /= Any_Id then
5104
5105                   --  Note: The call to Disambiguate only happens if a
5106                   --  previous interpretation was found, in which case I1
5107                   --  has received a value.
5108
5109                   It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
5110
5111                   if It1 = No_Interp then
5112                      Inst := Enclosing_Instance;
5113
5114                      if Present (Inst) then
5115                         if Within (It.Nam, Inst) then
5116                            return (It.Nam);
5117                         elsif Within (Old_S, Inst) then
5118                            return (Old_S);
5119                         else
5120                            return Report_Overload;
5121                         end if;
5122
5123                      else
5124                         return Report_Overload;
5125                      end if;
5126
5127                   else
5128                      Old_S := It1.Nam;
5129                      exit;
5130                   end if;
5131
5132                else
5133                   I1 := Ind;
5134                   Old_S := It.Nam;
5135                end if;
5136
5137             elsif
5138               Present (First_Formal (It.Nam))
5139                 and then Present (First_Formal (New_S))
5140                 and then  (Base_Type (Etype (First_Formal (It.Nam)))
5141                             = Base_Type (Etype (First_Formal (New_S))))
5142             then
5143                Candidate_Renaming := It.Nam;
5144             end if;
5145
5146             Get_Next_Interp (Ind, It);
5147          end loop;
5148
5149          Set_Entity (Nam, Old_S);
5150          Set_Is_Overloaded (Nam, False);
5151       end if;
5152
5153       return Old_S;
5154    end Find_Renamed_Entity;
5155
5156    -----------------------------
5157    -- Find_Selected_Component --
5158    -----------------------------
5159
5160    procedure Find_Selected_Component (N : Node_Id) is
5161       P : constant Node_Id := Prefix (N);
5162
5163       P_Name : Entity_Id;
5164       --  Entity denoted by prefix
5165
5166       P_Type : Entity_Id;
5167       --  and its type
5168
5169       Nam : Node_Id;
5170
5171    begin
5172       Analyze (P);
5173
5174       if Nkind (P) = N_Error then
5175          return;
5176
5177       --  If the selector already has an entity, the node has been constructed
5178       --  in the course of expansion, and is known to be valid. Do not verify
5179       --  that it is defined for the type (it may be a private component used
5180       --  in the expansion of record equality).
5181
5182       elsif Present (Entity (Selector_Name (N))) then
5183          if No (Etype (N))
5184            or else Etype (N) = Any_Type
5185          then
5186             declare
5187                Sel_Name : constant Node_Id   := Selector_Name (N);
5188                Selector : constant Entity_Id := Entity (Sel_Name);
5189                C_Etype  : Node_Id;
5190
5191             begin
5192                Set_Etype (Sel_Name, Etype (Selector));
5193
5194                if not Is_Entity_Name (P) then
5195                   Resolve (P);
5196                end if;
5197
5198                --  Build an actual subtype except for the first parameter
5199                --  of an init proc, where this actual subtype is by
5200                --  definition incorrect, since the object is uninitialized
5201                --  (and does not even have defined discriminants etc.)
5202
5203                if Is_Entity_Name (P)
5204                  and then Ekind (Entity (P)) = E_Function
5205                then
5206                   Nam := New_Copy (P);
5207
5208                   if Is_Overloaded (P) then
5209                      Save_Interps (P, Nam);
5210                   end if;
5211
5212                   Rewrite (P,
5213                     Make_Function_Call (Sloc (P), Name => Nam));
5214                   Analyze_Call (P);
5215                   Analyze_Selected_Component (N);
5216                   return;
5217
5218                elsif Ekind (Selector) = E_Component
5219                  and then (not Is_Entity_Name (P)
5220                             or else Chars (Entity (P)) /= Name_uInit)
5221                then
5222                   C_Etype :=
5223                     Build_Actual_Subtype_Of_Component (
5224                       Etype (Selector), N);
5225                else
5226                   C_Etype := Empty;
5227                end if;
5228
5229                if No (C_Etype) then
5230                   C_Etype := Etype (Selector);
5231                else
5232                   Insert_Action (N, C_Etype);
5233                   C_Etype := Defining_Identifier (C_Etype);
5234                end if;
5235
5236                Set_Etype (N, C_Etype);
5237             end;
5238
5239             --  If this is the name of an entry or protected operation, and
5240             --  the prefix is an access type, insert an explicit dereference,
5241             --  so that entry calls are treated uniformly.
5242
5243             if Is_Access_Type (Etype (P))
5244               and then Is_Concurrent_Type (Designated_Type (Etype (P)))
5245             then
5246                declare
5247                   New_P : constant Node_Id :=
5248                             Make_Explicit_Dereference (Sloc (P),
5249                               Prefix => Relocate_Node (P));
5250                begin
5251                   Rewrite (P, New_P);
5252                   Set_Etype (P, Designated_Type (Etype (Prefix (P))));
5253                end;
5254             end if;
5255
5256          --  If the selected component appears within a default expression
5257          --  and it has an actual subtype, the pre-analysis has not yet
5258          --  completed its analysis, because Insert_Actions is disabled in
5259          --  that context. Within the init proc of the enclosing type we
5260          --  must complete this analysis, if an actual subtype was created.
5261
5262          elsif Inside_Init_Proc then
5263             declare
5264                Typ  : constant Entity_Id := Etype (N);
5265                Decl : constant Node_Id   := Declaration_Node (Typ);
5266             begin
5267                if Nkind (Decl) = N_Subtype_Declaration
5268                  and then not Analyzed (Decl)
5269                  and then Is_List_Member (Decl)
5270                  and then No (Parent (Decl))
5271                then
5272                   Remove (Decl);
5273                   Insert_Action (N, Decl);
5274                end if;
5275             end;
5276          end if;
5277
5278          return;
5279
5280       elsif Is_Entity_Name (P) then
5281          P_Name := Entity (P);
5282
5283          --  The prefix may denote an enclosing type which is the completion
5284          --  of an incomplete type declaration.
5285
5286          if Is_Type (P_Name) then
5287             Set_Entity (P, Get_Full_View (P_Name));
5288             Set_Etype  (P, Entity (P));
5289             P_Name := Entity (P);
5290          end if;
5291
5292          P_Type := Base_Type (Etype (P));
5293
5294          if Debug_Flag_E then
5295             Write_Str ("Found prefix type to be ");
5296             Write_Entity_Info (P_Type, "      "); Write_Eol;
5297          end if;
5298
5299          --  First check for components of a record object (not the
5300          --  result of a call, which is handled below).
5301
5302          if Is_Appropriate_For_Record (P_Type)
5303            and then not Is_Overloadable (P_Name)
5304            and then not Is_Type (P_Name)
5305          then
5306             --  Selected component of record. Type checking will validate
5307             --  name of selector.
5308             --  ??? could we rewrite an implicit dereference into an explicit
5309             --  one here?
5310
5311             Analyze_Selected_Component (N);
5312
5313          elsif Is_Appropriate_For_Entry_Prefix (P_Type)
5314            and then not In_Open_Scopes (P_Name)
5315            and then (not Is_Concurrent_Type (Etype (P_Name))
5316                        or else not In_Open_Scopes (Etype (P_Name)))
5317          then
5318             --  Call to protected operation or entry. Type checking is
5319             --  needed on the prefix.
5320
5321             Analyze_Selected_Component (N);
5322
5323          elsif (In_Open_Scopes (P_Name)
5324                   and then Ekind (P_Name) /= E_Void
5325                   and then not Is_Overloadable (P_Name))
5326            or else (Is_Concurrent_Type (Etype (P_Name))
5327                       and then In_Open_Scopes (Etype (P_Name)))
5328          then
5329             --  Prefix denotes an enclosing loop, block, or task, i.e. an
5330             --  enclosing construct that is not a subprogram or accept.
5331
5332             Find_Expanded_Name (N);
5333
5334          elsif Ekind (P_Name) = E_Package then
5335             Find_Expanded_Name (N);
5336
5337          elsif Is_Overloadable (P_Name) then
5338
5339             --  The subprogram may be a renaming (of an enclosing scope) as
5340             --  in the case of the name of the generic within an instantiation.
5341
5342             if (Ekind (P_Name) = E_Procedure
5343                  or else Ekind (P_Name) = E_Function)
5344               and then Present (Alias (P_Name))
5345               and then Is_Generic_Instance (Alias (P_Name))
5346             then
5347                P_Name := Alias (P_Name);
5348             end if;
5349
5350             if Is_Overloaded (P) then
5351
5352                --  The prefix must resolve to a unique enclosing construct
5353
5354                declare
5355                   Found : Boolean := False;
5356                   Ind   : Interp_Index;
5357                   It    : Interp;
5358
5359                begin
5360                   Get_First_Interp (P, Ind, It);
5361                   while Present (It.Nam) loop
5362                      if In_Open_Scopes (It.Nam) then
5363                         if Found then
5364                            Error_Msg_N (
5365                               "prefix must be unique enclosing scope", N);
5366                            Set_Entity (N, Any_Id);
5367                            Set_Etype  (N, Any_Type);
5368                            return;
5369
5370                         else
5371                            Found := True;
5372                            P_Name := It.Nam;
5373                         end if;
5374                      end if;
5375
5376                      Get_Next_Interp (Ind, It);
5377                   end loop;
5378                end;
5379             end if;
5380
5381             if In_Open_Scopes (P_Name) then
5382                Set_Entity (P, P_Name);
5383                Set_Is_Overloaded (P, False);
5384                Find_Expanded_Name (N);
5385
5386             else
5387                --  If no interpretation as an expanded name is possible, it
5388                --  must be a selected component of a record returned by a
5389                --  function call. Reformat prefix as a function call, the rest
5390                --  is done by type resolution. If the prefix is procedure or
5391                --  entry, as is P.X; this is an error.
5392
5393                if Ekind (P_Name) /= E_Function
5394                  and then (not Is_Overloaded (P)
5395                              or else
5396                            Nkind (Parent (N)) = N_Procedure_Call_Statement)
5397                then
5398                   --  Prefix may mention a package that is hidden by a local
5399                   --  declaration: let the user know. Scan the full homonym
5400                   --  chain, the candidate package may be anywhere on it.
5401
5402                   if Present (Homonym (Current_Entity (P_Name))) then
5403
5404                      P_Name := Current_Entity (P_Name);
5405
5406                      while Present (P_Name) loop
5407                         exit when Ekind (P_Name) = E_Package;
5408                         P_Name := Homonym (P_Name);
5409                      end loop;
5410
5411                      if Present (P_Name) then
5412                         Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
5413
5414                         Error_Msg_NE
5415                           ("package& is hidden by declaration#",
5416                             N, P_Name);
5417
5418                         Set_Entity (Prefix (N), P_Name);
5419                         Find_Expanded_Name (N);
5420                         return;
5421                      else
5422                         P_Name := Entity (Prefix (N));
5423                      end if;
5424                   end if;
5425
5426                   Error_Msg_NE
5427                     ("invalid prefix in selected component&", N, P_Name);
5428                   Change_Selected_Component_To_Expanded_Name (N);
5429                   Set_Entity (N, Any_Id);
5430                   Set_Etype (N, Any_Type);
5431
5432                else
5433                   Nam := New_Copy (P);
5434                   Save_Interps (P, Nam);
5435                   Rewrite (P,
5436                     Make_Function_Call (Sloc (P), Name => Nam));
5437                   Analyze_Call (P);
5438                   Analyze_Selected_Component (N);
5439                end if;
5440             end if;
5441
5442          --  Remaining cases generate various error messages
5443
5444          else
5445             --  Format node as expanded name, to avoid cascaded errors
5446
5447             Change_Selected_Component_To_Expanded_Name (N);
5448             Set_Entity  (N, Any_Id);
5449             Set_Etype   (N, Any_Type);
5450
5451             --  Issue error message, but avoid this if error issued already.
5452             --  Use identifier of prefix if one is available.
5453
5454             if P_Name = Any_Id  then
5455                null;
5456
5457             elsif Ekind (P_Name) = E_Void then
5458                Premature_Usage (P);
5459
5460             elsif Nkind (P) /= N_Attribute_Reference then
5461                Error_Msg_N (
5462                 "invalid prefix in selected component&", P);
5463
5464                if Is_Access_Type (P_Type)
5465                  and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
5466                then
5467                   Error_Msg_N
5468                     ("\dereference must not be of an incomplete type " &
5469                        "(RM 3.10.1)", P);
5470                end if;
5471
5472             else
5473                Error_Msg_N (
5474                 "invalid prefix in selected component", P);
5475             end if;
5476          end if;
5477
5478       else
5479          --  If prefix is not the name of an entity, it must be an expression,
5480          --  whose type is appropriate for a record. This is determined by
5481          --  type resolution.
5482
5483          Analyze_Selected_Component (N);
5484       end if;
5485    end Find_Selected_Component;
5486
5487    ---------------
5488    -- Find_Type --
5489    ---------------
5490
5491    procedure Find_Type (N : Node_Id) is
5492       C      : Entity_Id;
5493       Typ    : Entity_Id;
5494       T      : Entity_Id;
5495       T_Name : Entity_Id;
5496
5497    begin
5498       if N = Error then
5499          return;
5500
5501       elsif Nkind (N) = N_Attribute_Reference then
5502
5503          --  Class attribute. This is not valid in Ada 83 mode, but we do not
5504          --  need to enforce that at this point, since the declaration of the
5505          --  tagged type in the prefix would have been flagged already.
5506
5507          if Attribute_Name (N) = Name_Class then
5508             Check_Restriction (No_Dispatch, N);
5509             Find_Type (Prefix (N));
5510
5511             --  Propagate error from bad prefix
5512
5513             if Etype (Prefix (N)) = Any_Type then
5514                Set_Entity (N, Any_Type);
5515                Set_Etype  (N, Any_Type);
5516                return;
5517             end if;
5518
5519             T := Base_Type (Entity (Prefix (N)));
5520
5521             --  Case where type is not known to be tagged. Its appearance in
5522             --  the prefix of the 'Class attribute indicates that the full view
5523             --  will be tagged.
5524
5525             if not Is_Tagged_Type (T) then
5526                if Ekind (T) = E_Incomplete_Type then
5527
5528                   --  It is legal to denote the class type of an incomplete
5529                   --  type. The full type will have to be tagged, of course.
5530                   --  In Ada 2005 this usage is declared obsolescent, so we
5531                   --  warn accordingly.
5532
5533                   --  ??? This test is temporarily disabled (always False)
5534                   --  because it causes an unwanted warning on GNAT sources
5535                   --  (built with -gnatg, which includes Warn_On_Obsolescent_
5536                   --  Feature). Once this issue is cleared in the sources, it
5537                   --  can be enabled.
5538
5539                   if not Is_Tagged_Type (T)
5540                     and then Ada_Version >= Ada_05
5541                     and then Warn_On_Obsolescent_Feature
5542                     and then False
5543                   then
5544                      Error_Msg_N
5545                        ("applying 'Class to an untagged incomplete type"
5546                          & " is an obsolescent feature  (RM J.11)", N);
5547                   end if;
5548
5549                   Set_Is_Tagged_Type (T);
5550                   Set_Primitive_Operations (T, New_Elmt_List);
5551                   Make_Class_Wide_Type (T);
5552                   Set_Entity (N, Class_Wide_Type (T));
5553                   Set_Etype  (N, Class_Wide_Type (T));
5554
5555                elsif Ekind (T) = E_Private_Type
5556                  and then not Is_Generic_Type (T)
5557                  and then In_Private_Part (Scope (T))
5558                then
5559                   --  The Class attribute can be applied to an untagged private
5560                   --  type fulfilled by a tagged type prior to the full type
5561                   --  declaration (but only within the parent package's private
5562                   --  part). Create the class-wide type now and check that the
5563                   --  full type is tagged later during its analysis. Note that
5564                   --  we do not mark the private type as tagged, unlike the
5565                   --  case of incomplete types, because the type must still
5566                   --  appear untagged to outside units.
5567
5568                   if No (Class_Wide_Type (T)) then
5569                      Make_Class_Wide_Type (T);
5570                   end if;
5571
5572                   Set_Entity (N, Class_Wide_Type (T));
5573                   Set_Etype  (N, Class_Wide_Type (T));
5574
5575                else
5576                   --  Should we introduce a type Any_Tagged and use Wrong_Type
5577                   --  here, it would be a bit more consistent???
5578
5579                   Error_Msg_NE
5580                     ("tagged type required, found}",
5581                      Prefix (N), First_Subtype (T));
5582                   Set_Entity (N, Any_Type);
5583                   return;
5584                end if;
5585
5586             --  Case of tagged type
5587
5588             else
5589                if Is_Concurrent_Type (T) then
5590                   if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
5591
5592                      --  Previous error. Use current type, which at least
5593                      --  provides some operations.
5594
5595                      C := Entity (Prefix (N));
5596
5597                   else
5598                      C := Class_Wide_Type
5599                             (Corresponding_Record_Type (Entity (Prefix (N))));
5600                   end if;
5601
5602                else
5603                   C := Class_Wide_Type (Entity (Prefix (N)));
5604                end if;
5605
5606                Set_Entity_With_Style_Check (N, C);
5607                Generate_Reference (C, N);
5608                Set_Etype (N, C);
5609             end if;
5610
5611          --  Base attribute, not allowed in Ada 83
5612
5613          elsif Attribute_Name (N) = Name_Base then
5614             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
5615                Error_Msg_N
5616                  ("(Ada 83) Base attribute not allowed in subtype mark", N);
5617
5618             else
5619                Find_Type (Prefix (N));
5620                Typ := Entity (Prefix (N));
5621
5622                if Ada_Version >= Ada_95
5623                  and then not Is_Scalar_Type (Typ)
5624                  and then not Is_Generic_Type (Typ)
5625                then
5626                   Error_Msg_N
5627                     ("prefix of Base attribute must be scalar type",
5628                       Prefix (N));
5629
5630                elsif Sloc (Typ) = Standard_Location
5631                  and then Base_Type (Typ) = Typ
5632                  and then Warn_On_Redundant_Constructs
5633                then
5634                   Error_Msg_NE
5635                     ("?redundant attribute, & is its own base type", N, Typ);
5636                end if;
5637
5638                T := Base_Type (Typ);
5639
5640                --  Rewrite attribute reference with type itself (see similar
5641                --  processing in Analyze_Attribute, case Base). Preserve
5642                --  prefix if present, for other legality checks.
5643
5644                if Nkind (Prefix (N)) = N_Expanded_Name then
5645                   Rewrite (N,
5646                      Make_Expanded_Name (Sloc (N),
5647                        Chars         => Chars (T),
5648                        Prefix        => New_Copy (Prefix (Prefix (N))),
5649                        Selector_Name => New_Reference_To (T, Sloc (N))));
5650
5651                else
5652                   Rewrite (N, New_Reference_To (T, Sloc (N)));
5653                end if;
5654
5655                Set_Entity (N, T);
5656                Set_Etype (N, T);
5657             end if;
5658
5659          elsif Attribute_Name (N) = Name_Stub_Type then
5660
5661             --  This is handled in Analyze_Attribute
5662
5663             Analyze (N);
5664
5665          --  All other attributes are invalid in a subtype mark
5666
5667          else
5668             Error_Msg_N ("invalid attribute in subtype mark", N);
5669          end if;
5670
5671       else
5672          Analyze (N);
5673
5674          if Is_Entity_Name (N) then
5675             T_Name := Entity (N);
5676          else
5677             Error_Msg_N ("subtype mark required in this context", N);
5678             Set_Etype (N, Any_Type);
5679             return;
5680          end if;
5681
5682          if T_Name  = Any_Id or else Etype (N) = Any_Type then
5683
5684             --  Undefined id. Make it into a valid type
5685
5686             Set_Entity (N, Any_Type);
5687
5688          elsif not Is_Type (T_Name)
5689            and then T_Name /= Standard_Void_Type
5690          then
5691             Error_Msg_Sloc := Sloc (T_Name);
5692             Error_Msg_N ("subtype mark required in this context", N);
5693             Error_Msg_NE ("\\found & declared#", N, T_Name);
5694             Set_Entity (N, Any_Type);
5695
5696          else
5697             --  If the type is an incomplete type created to handle
5698             --  anonymous access components of a record type, then the
5699             --  incomplete type is the visible entity and subsequent
5700             --  references will point to it. Mark the original full
5701             --  type as referenced, to prevent spurious warnings.
5702
5703             if Is_Incomplete_Type (T_Name)
5704               and then Present (Full_View (T_Name))
5705               and then not Comes_From_Source (T_Name)
5706             then
5707                Set_Referenced (Full_View (T_Name));
5708             end if;
5709
5710             T_Name := Get_Full_View (T_Name);
5711
5712             --  Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
5713             --  limited-with clauses
5714
5715             if From_With_Type (T_Name)
5716               and then Ekind (T_Name) in Incomplete_Kind
5717               and then Present (Non_Limited_View (T_Name))
5718               and then Is_Interface (Non_Limited_View (T_Name))
5719             then
5720                T_Name := Non_Limited_View (T_Name);
5721             end if;
5722
5723             if In_Open_Scopes (T_Name) then
5724                if Ekind (Base_Type (T_Name)) = E_Task_Type then
5725
5726                   --  In Ada 2005, a task name can be used in an access
5727                   --  definition within its own body. It cannot be used
5728                   --  in the discriminant part of the task declaration,
5729                   --  nor anywhere else in the declaration because entries
5730                   --  cannot have access parameters.
5731
5732                   if Ada_Version >= Ada_05
5733                     and then Nkind (Parent (N)) = N_Access_Definition
5734                   then
5735                      Set_Entity (N, T_Name);
5736                      Set_Etype  (N, T_Name);
5737
5738                      if Has_Completion (T_Name) then
5739                         return;
5740
5741                      else
5742                         Error_Msg_N
5743                           ("task type cannot be used as type mark " &
5744                            "within its own declaration", N);
5745                      end if;
5746
5747                   else
5748                      Error_Msg_N
5749                        ("task type cannot be used as type mark " &
5750                         "within its own spec or body", N);
5751                   end if;
5752
5753                elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
5754
5755                   --  In Ada 2005, a protected name can be used in an access
5756                   --  definition within its own body.
5757
5758                   if Ada_Version >= Ada_05
5759                     and then Nkind (Parent (N)) = N_Access_Definition
5760                   then
5761                      Set_Entity (N, T_Name);
5762                      Set_Etype  (N, T_Name);
5763                      return;
5764
5765                   else
5766                      Error_Msg_N
5767                        ("protected type cannot be used as type mark " &
5768                         "within its own spec or body", N);
5769                   end if;
5770
5771                else
5772                   Error_Msg_N ("type declaration cannot refer to itself", N);
5773                end if;
5774
5775                Set_Etype (N, Any_Type);
5776                Set_Entity (N, Any_Type);
5777                Set_Error_Posted (T_Name);
5778                return;
5779             end if;
5780
5781             Set_Entity (N, T_Name);
5782             Set_Etype  (N, T_Name);
5783          end if;
5784       end if;
5785
5786       if Present (Etype (N)) and then Comes_From_Source (N) then
5787          if Is_Fixed_Point_Type (Etype (N)) then
5788             Check_Restriction (No_Fixed_Point, N);
5789          elsif Is_Floating_Point_Type (Etype (N)) then
5790             Check_Restriction (No_Floating_Point, N);
5791          end if;
5792       end if;
5793    end Find_Type;
5794
5795    ------------------------------------
5796    -- Has_Implicit_Character_Literal --
5797    ------------------------------------
5798
5799    function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
5800       Id      : Entity_Id;
5801       Found   : Boolean := False;
5802       P       : constant Entity_Id := Entity (Prefix (N));
5803       Priv_Id : Entity_Id := Empty;
5804
5805    begin
5806       if Ekind (P) = E_Package
5807         and then not In_Open_Scopes (P)
5808       then
5809          Priv_Id := First_Private_Entity (P);
5810       end if;
5811
5812       if P = Standard_Standard then
5813          Change_Selected_Component_To_Expanded_Name (N);
5814          Rewrite (N, Selector_Name (N));
5815          Analyze (N);
5816          Set_Etype (Original_Node (N), Standard_Character);
5817          return True;
5818       end if;
5819
5820       Id := First_Entity (P);
5821       while Present (Id)
5822         and then Id /= Priv_Id
5823       loop
5824          if Is_Standard_Character_Type (Id)
5825            and then Id = Base_Type (Id)
5826          then
5827             --  We replace the node with the literal itself, resolve as a
5828             --  character, and set the type correctly.
5829
5830             if not Found then
5831                Change_Selected_Component_To_Expanded_Name (N);
5832                Rewrite (N, Selector_Name (N));
5833                Analyze (N);
5834                Set_Etype (N, Id);
5835                Set_Etype (Original_Node (N), Id);
5836                Found := True;
5837
5838             else
5839                --  More than one type derived from Character in given scope.
5840                --  Collect all possible interpretations.
5841
5842                Add_One_Interp (N, Id, Id);
5843             end if;
5844          end if;
5845
5846          Next_Entity (Id);
5847       end loop;
5848
5849       return Found;
5850    end Has_Implicit_Character_Literal;
5851
5852    ----------------------
5853    -- Has_Private_With --
5854    ----------------------
5855
5856    function Has_Private_With (E : Entity_Id) return Boolean is
5857       Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
5858       Item      : Node_Id;
5859
5860    begin
5861       Item := First (Context_Items (Comp_Unit));
5862       while Present (Item) loop
5863          if Nkind (Item) = N_With_Clause
5864            and then Private_Present (Item)
5865            and then Entity (Name (Item)) = E
5866          then
5867             return True;
5868          end if;
5869
5870          Next (Item);
5871       end loop;
5872
5873       return False;
5874    end Has_Private_With;
5875
5876    ---------------------------
5877    -- Has_Implicit_Operator --
5878    ---------------------------
5879
5880    function Has_Implicit_Operator (N : Node_Id) return Boolean is
5881       Op_Id   : constant Name_Id   := Chars (Selector_Name (N));
5882       P       : constant Entity_Id := Entity (Prefix (N));
5883       Id      : Entity_Id;
5884       Priv_Id : Entity_Id := Empty;
5885
5886       procedure Add_Implicit_Operator
5887         (T       : Entity_Id;
5888          Op_Type : Entity_Id := Empty);
5889       --  Add implicit interpretation to node N, using the type for which a
5890       --  predefined operator exists. If the operator yields a boolean type,
5891       --  the Operand_Type is implicitly referenced by the operator, and a
5892       --  reference to it must be generated.
5893
5894       ---------------------------
5895       -- Add_Implicit_Operator --
5896       ---------------------------
5897
5898       procedure Add_Implicit_Operator
5899         (T       : Entity_Id;
5900          Op_Type : Entity_Id := Empty)
5901       is
5902          Predef_Op : Entity_Id;
5903
5904       begin
5905          Predef_Op := Current_Entity (Selector_Name (N));
5906
5907          while Present (Predef_Op)
5908            and then Scope (Predef_Op) /= Standard_Standard
5909          loop
5910             Predef_Op := Homonym (Predef_Op);
5911          end loop;
5912
5913          if Nkind (N) = N_Selected_Component then
5914             Change_Selected_Component_To_Expanded_Name (N);
5915          end if;
5916
5917          Add_One_Interp (N, Predef_Op, T);
5918
5919          --  For operators with unary and binary interpretations, add both
5920
5921          if Present (Homonym (Predef_Op)) then
5922             Add_One_Interp (N, Homonym (Predef_Op), T);
5923          end if;
5924
5925          --  The node is a reference to a predefined operator, and
5926          --  an implicit reference to the type of its operands.
5927
5928          if Present (Op_Type) then
5929             Generate_Operator_Reference (N, Op_Type);
5930          else
5931             Generate_Operator_Reference (N, T);
5932          end if;
5933       end Add_Implicit_Operator;
5934
5935    --  Start of processing for Has_Implicit_Operator
5936
5937    begin
5938       if Ekind (P) = E_Package
5939         and then not In_Open_Scopes (P)
5940       then
5941          Priv_Id := First_Private_Entity (P);
5942       end if;
5943
5944       Id := First_Entity (P);
5945
5946       case Op_Id is
5947
5948          --  Boolean operators: an implicit declaration exists if the scope
5949          --  contains a declaration for a derived Boolean type, or for an
5950          --  array of Boolean type.
5951
5952          when Name_Op_And | Name_Op_Not | Name_Op_Or  | Name_Op_Xor =>
5953             while Id  /= Priv_Id loop
5954                if Valid_Boolean_Arg (Id)
5955                  and then Id = Base_Type (Id)
5956                then
5957                   Add_Implicit_Operator (Id);
5958                   return True;
5959                end if;
5960
5961                Next_Entity (Id);
5962             end loop;
5963
5964          --  Equality: look for any non-limited type (result is Boolean)
5965
5966          when Name_Op_Eq | Name_Op_Ne =>
5967             while Id  /= Priv_Id loop
5968                if Is_Type (Id)
5969                  and then not Is_Limited_Type (Id)
5970                  and then Id = Base_Type (Id)
5971                then
5972                   Add_Implicit_Operator (Standard_Boolean, Id);
5973                   return True;
5974                end if;
5975
5976                Next_Entity (Id);
5977             end loop;
5978
5979          --  Comparison operators: scalar type, or array of scalar
5980
5981          when Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge =>
5982             while Id  /= Priv_Id loop
5983                if (Is_Scalar_Type (Id)
5984                  or else (Is_Array_Type (Id)
5985                            and then Is_Scalar_Type (Component_Type (Id))))
5986                  and then Id = Base_Type (Id)
5987                then
5988                   Add_Implicit_Operator (Standard_Boolean, Id);
5989                   return True;
5990                end if;
5991
5992                Next_Entity (Id);
5993             end loop;
5994
5995          --  Arithmetic operators: any numeric type
5996
5997          when Name_Op_Abs      |
5998               Name_Op_Add      |
5999               Name_Op_Mod      |
6000               Name_Op_Rem      |
6001               Name_Op_Subtract |
6002               Name_Op_Multiply |
6003               Name_Op_Divide   |
6004               Name_Op_Expon    =>
6005             while Id  /= Priv_Id loop
6006                if Is_Numeric_Type (Id)
6007                  and then Id = Base_Type (Id)
6008                then
6009                   Add_Implicit_Operator (Id);
6010                   return True;
6011                end if;
6012
6013                Next_Entity (Id);
6014             end loop;
6015
6016          --  Concatenation: any one-dimensional array type
6017
6018          when Name_Op_Concat =>
6019             while Id  /= Priv_Id loop
6020                if Is_Array_Type (Id) and then Number_Dimensions (Id) = 1
6021                  and then Id = Base_Type (Id)
6022                then
6023                   Add_Implicit_Operator (Id);
6024                   return True;
6025                end if;
6026
6027                Next_Entity (Id);
6028             end loop;
6029
6030          --  What is the others condition here? Should we be using a
6031          --  subtype of Name_Id that would restrict to operators ???
6032
6033          when others => null;
6034       end case;
6035
6036       --  If we fall through, then we do not have an implicit operator
6037
6038       return False;
6039
6040    end Has_Implicit_Operator;
6041
6042    --------------------
6043    -- In_Open_Scopes --
6044    --------------------
6045
6046    function In_Open_Scopes (S : Entity_Id) return Boolean is
6047    begin
6048       --  Several scope stacks are maintained by Scope_Stack. The base of the
6049       --  currently active scope stack is denoted by the Is_Active_Stack_Base
6050       --  flag in the scope stack entry. Note that the scope stacks used to
6051       --  simply be delimited implicitly by the presence of Standard_Standard
6052       --  at their base, but there now are cases where this is not sufficient
6053       --  because Standard_Standard actually may appear in the middle of the
6054       --  active set of scopes.
6055
6056       for J in reverse 0 .. Scope_Stack.Last loop
6057          if Scope_Stack.Table (J).Entity = S then
6058             return True;
6059          end if;
6060
6061          --  Check Is_Active_Stack_Base to tell us when to stop, as there are
6062          --  cases where Standard_Standard appears in the middle of the active
6063          --  set of scopes. This affects the declaration and overriding of
6064          --  private inherited operations in instantiations of generic child
6065          --  units.
6066
6067          exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
6068       end loop;
6069
6070       return False;
6071    end In_Open_Scopes;
6072
6073    -----------------------------
6074    -- Inherit_Renamed_Profile --
6075    -----------------------------
6076
6077    procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
6078       New_F : Entity_Id;
6079       Old_F : Entity_Id;
6080       Old_T : Entity_Id;
6081       New_T : Entity_Id;
6082
6083    begin
6084       if Ekind (Old_S) = E_Operator then
6085          New_F := First_Formal (New_S);
6086
6087          while Present (New_F) loop
6088             Set_Etype (New_F, Base_Type (Etype (New_F)));
6089             Next_Formal (New_F);
6090          end loop;
6091
6092          Set_Etype (New_S, Base_Type (Etype (New_S)));
6093
6094       else
6095          New_F := First_Formal (New_S);
6096          Old_F := First_Formal (Old_S);
6097
6098          while Present (New_F) loop
6099             New_T := Etype (New_F);
6100             Old_T := Etype (Old_F);
6101
6102             --  If the new type is a renaming of the old one, as is the
6103             --  case for actuals in instances, retain its name, to simplify
6104             --  later disambiguation.
6105
6106             if Nkind (Parent (New_T)) = N_Subtype_Declaration
6107               and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
6108               and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
6109             then
6110                null;
6111             else
6112                Set_Etype (New_F, Old_T);
6113             end if;
6114
6115             Next_Formal (New_F);
6116             Next_Formal (Old_F);
6117          end loop;
6118
6119          if Ekind (Old_S) = E_Function
6120            or else Ekind (Old_S) = E_Enumeration_Literal
6121          then
6122             Set_Etype (New_S, Etype (Old_S));
6123          end if;
6124       end if;
6125    end Inherit_Renamed_Profile;
6126
6127    ----------------
6128    -- Initialize --
6129    ----------------
6130
6131    procedure Initialize is
6132    begin
6133       Urefs.Init;
6134    end Initialize;
6135
6136    -------------------------
6137    -- Install_Use_Clauses --
6138    -------------------------
6139
6140    procedure Install_Use_Clauses
6141      (Clause             : Node_Id;
6142       Force_Installation : Boolean := False)
6143    is
6144       U  : Node_Id;
6145       P  : Node_Id;
6146       Id : Entity_Id;
6147
6148    begin
6149       U := Clause;
6150       while Present (U) loop
6151
6152          --  Case of USE package
6153
6154          if Nkind (U) = N_Use_Package_Clause then
6155             P := First (Names (U));
6156             while Present (P) loop
6157                Id := Entity (P);
6158
6159                if Ekind (Id) = E_Package then
6160                   if In_Use (Id) then
6161                      Note_Redundant_Use (P);
6162
6163                   elsif Present (Renamed_Object (Id))
6164                     and then In_Use (Renamed_Object (Id))
6165                   then
6166                      Note_Redundant_Use (P);
6167
6168                   elsif Force_Installation or else Applicable_Use (P) then
6169                      Use_One_Package (Id, U);
6170
6171                   end if;
6172                end if;
6173
6174                Next (P);
6175             end loop;
6176
6177          --  Case of USE TYPE
6178
6179          else
6180             P := First (Subtype_Marks (U));
6181             while Present (P) loop
6182                if not Is_Entity_Name (P)
6183                  or else No (Entity (P))
6184                then
6185                   null;
6186
6187                elsif Entity (P) /= Any_Type then
6188                   Use_One_Type (P);
6189                end if;
6190
6191                Next (P);
6192             end loop;
6193          end if;
6194
6195          Next_Use_Clause (U);
6196       end loop;
6197    end Install_Use_Clauses;
6198
6199    -------------------------------------
6200    -- Is_Appropriate_For_Entry_Prefix --
6201    -------------------------------------
6202
6203    function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
6204       P_Type : Entity_Id := T;
6205
6206    begin
6207       if Is_Access_Type (P_Type) then
6208          P_Type := Designated_Type (P_Type);
6209       end if;
6210
6211       return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
6212    end Is_Appropriate_For_Entry_Prefix;
6213
6214    -------------------------------
6215    -- Is_Appropriate_For_Record --
6216    -------------------------------
6217
6218    function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is
6219
6220       function Has_Components (T1 : Entity_Id) return Boolean;
6221       --  Determine if given type has components (i.e. is either a record
6222       --  type or a type that has discriminants).
6223
6224       --------------------
6225       -- Has_Components --
6226       --------------------
6227
6228       function Has_Components (T1 : Entity_Id) return Boolean is
6229       begin
6230          return Is_Record_Type (T1)
6231            or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
6232            or else (Is_Task_Type (T1) and then Has_Discriminants (T1))
6233            or else (Is_Incomplete_Type (T1)
6234                      and then From_With_Type (T1)
6235                      and then Present (Non_Limited_View (T1))
6236                      and then Is_Record_Type
6237                                 (Get_Full_View (Non_Limited_View (T1))));
6238       end Has_Components;
6239
6240    --  Start of processing for Is_Appropriate_For_Record
6241
6242    begin
6243       return
6244         Present (T)
6245           and then (Has_Components (T)
6246                      or else (Is_Access_Type (T)
6247                                and then Has_Components (Designated_Type (T))));
6248    end Is_Appropriate_For_Record;
6249
6250    ------------------------
6251    -- Note_Redundant_Use --
6252    ------------------------
6253
6254    procedure Note_Redundant_Use (Clause : Node_Id) is
6255       Pack_Name : constant Entity_Id := Entity (Clause);
6256       Cur_Use   : constant Node_Id   := Current_Use_Clause (Pack_Name);
6257       Decl      : constant Node_Id   := Parent (Clause);
6258
6259       Prev_Use   : Node_Id := Empty;
6260       Redundant  : Node_Id := Empty;
6261       --  The Use_Clause which is actually redundant. In the simplest case it
6262       --  is Pack itself, but when we compile a body we install its context
6263       --  before that of its spec, in which case it is the use_clause in the
6264       --  spec that will appear to be redundant, and we want the warning to be
6265       --  placed on the body. Similar complications appear when the redundancy
6266       --  is between a child unit and one of its ancestors.
6267
6268    begin
6269       Set_Redundant_Use (Clause, True);
6270
6271       if not Comes_From_Source (Clause)
6272         or else In_Instance
6273         or else not Warn_On_Redundant_Constructs
6274       then
6275          return;
6276       end if;
6277
6278       if not Is_Compilation_Unit (Current_Scope) then
6279
6280          --  If the use_clause is in an inner scope, it is made redundant by
6281          --  some clause in the current context, with one exception: If we're
6282          --  compiling a nested package body, and the use_clause comes from the
6283          --  corresponding spec, the clause is not necessarily fully redundant,
6284          --  so we should not warn. If a warning was warranted, it would have
6285          --  been given when the spec was processed.
6286
6287          if Nkind (Parent (Decl)) = N_Package_Specification then
6288             declare
6289                Package_Spec_Entity : constant Entity_Id :=
6290                                        Defining_Unit_Name (Parent (Decl));
6291             begin
6292                if In_Package_Body (Package_Spec_Entity) then
6293                   return;
6294                end if;
6295             end;
6296          end if;
6297
6298          Redundant := Clause;
6299          Prev_Use  := Cur_Use;
6300
6301       elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
6302          declare
6303             Cur_Unit : constant Unit_Number_Type := Get_Source_Unit (Cur_Use);
6304             New_Unit : constant Unit_Number_Type := Get_Source_Unit (Clause);
6305             Scop     : Entity_Id;
6306
6307          begin
6308             if Cur_Unit = New_Unit then
6309
6310                --  Redundant clause in same body
6311
6312                Redundant := Clause;
6313                Prev_Use  := Cur_Use;
6314
6315             elsif Cur_Unit = Current_Sem_Unit then
6316
6317                --  If the new clause is not in the current unit it has been
6318                --  analyzed first, and it makes the other one redundant.
6319                --  However, if the new clause appears in a subunit, Cur_Unit
6320                --  is still the parent, and in that case the redundant one
6321                --  is the one appearing in the subunit.
6322
6323                if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
6324                   Redundant := Clause;
6325                   Prev_Use  := Cur_Use;
6326
6327                --  Most common case: redundant clause in body,
6328                --  original clause in spec. Current scope is spec entity.
6329
6330                elsif
6331                  Current_Scope =
6332                    Defining_Entity (
6333                      Unit (Library_Unit (Cunit (Current_Sem_Unit))))
6334                then
6335                   Redundant := Cur_Use;
6336                   Prev_Use  := Clause;
6337
6338                else
6339                   --  The new clause may appear in an unrelated unit, when
6340                   --  the parents of a generic are being installed prior to
6341                   --  instantiation. In this case there must be no warning.
6342                   --  We detect this case by checking whether the current top
6343                   --  of the stack is related to the current compilation.
6344
6345                   Scop := Current_Scope;
6346                   while Present (Scop)
6347                     and then Scop /= Standard_Standard
6348                   loop
6349                      if Is_Compilation_Unit (Scop)
6350                        and then not Is_Child_Unit (Scop)
6351                      then
6352                         return;
6353
6354                      elsif Scop = Cunit_Entity (Current_Sem_Unit) then
6355                         exit;
6356                      end if;
6357
6358                      Scop := Scope (Scop);
6359                   end loop;
6360
6361                   Redundant := Cur_Use;
6362                   Prev_Use  := Clause;
6363                end if;
6364
6365             elsif New_Unit = Current_Sem_Unit then
6366                Redundant := Clause;
6367                Prev_Use  := Cur_Use;
6368
6369             else
6370                --  Neither is the current unit, so they appear in parent or
6371                --  sibling units. Warning will be emitted elsewhere.
6372
6373                return;
6374             end if;
6375          end;
6376
6377       elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
6378         and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
6379       then
6380          --  Use_clause is in child unit of current unit, and the child unit
6381          --  appears in the context of the body of the parent, so it has been
6382          --  installed first, even though it is the redundant one. Depending on
6383          --  their placement in the context, the visible or the private parts
6384          --  of the two units, either might appear as redundant, but the
6385          --  message has to be on the current unit.
6386
6387          if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
6388             Redundant := Cur_Use;
6389             Prev_Use  := Clause;
6390          else
6391             Redundant := Clause;
6392             Prev_Use  := Cur_Use;
6393          end if;
6394
6395          --  If the new use clause appears in the private part of a parent unit
6396          --  it may appear to be redundant w.r.t. a use clause in a child unit,
6397          --  but the previous use clause was needed in the visible part of the
6398          --  child, and no warning should be emitted.
6399
6400          if Nkind (Parent (Decl)) = N_Package_Specification
6401            and then
6402              List_Containing (Decl) = Private_Declarations (Parent (Decl))
6403          then
6404             declare
6405                Par : constant Entity_Id := Defining_Entity (Parent (Decl));
6406                Spec : constant Node_Id  :=
6407                         Specification (Unit (Cunit (Current_Sem_Unit)));
6408
6409             begin
6410                if Is_Compilation_Unit (Par)
6411                  and then Par /= Cunit_Entity (Current_Sem_Unit)
6412                  and then Parent (Cur_Use) = Spec
6413                  and then
6414                    List_Containing (Cur_Use) = Visible_Declarations (Spec)
6415                then
6416                   return;
6417                end if;
6418             end;
6419          end if;
6420
6421       --  Finally, if the current use clause is in the context then
6422       --  the clause is redundant when it is nested within the unit.
6423
6424       elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
6425         and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
6426         and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
6427       then
6428          Redundant := Clause;
6429          Prev_Use  := Cur_Use;
6430
6431       else
6432          null;
6433       end if;
6434
6435       if Present (Redundant) then
6436          Error_Msg_Sloc := Sloc (Prev_Use);
6437          Error_Msg_NE
6438            ("& is already use-visible through previous use clause #?",
6439             Redundant, Pack_Name);
6440       end if;
6441    end Note_Redundant_Use;
6442
6443    ---------------
6444    -- Pop_Scope --
6445    ---------------
6446
6447    procedure Pop_Scope is
6448       SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
6449
6450    begin
6451       if Debug_Flag_E then
6452          Write_Info;
6453       end if;
6454
6455       Scope_Suppress           := SST.Save_Scope_Suppress;
6456       Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
6457       Check_Policy_List        := SST.Save_Check_Policy_List;
6458
6459       if Debug_Flag_W then
6460          Write_Str ("--> exiting scope: ");
6461          Write_Name (Chars (Current_Scope));
6462          Write_Str (", Depth=");
6463          Write_Int (Int (Scope_Stack.Last));
6464          Write_Eol;
6465       end if;
6466
6467       End_Use_Clauses (SST.First_Use_Clause);
6468
6469       --  If the actions to be wrapped are still there they will get lost
6470       --  causing incomplete code to be generated. It is better to abort in
6471       --  this case (and we do the abort even with assertions off since the
6472       --  penalty is incorrect code generation)
6473
6474       if SST.Actions_To_Be_Wrapped_Before /= No_List
6475            or else
6476          SST.Actions_To_Be_Wrapped_After  /= No_List
6477       then
6478          return;
6479       end if;
6480
6481       --  Free last subprogram name if allocated, and pop scope
6482
6483       Free (SST.Last_Subprogram_Name);
6484       Scope_Stack.Decrement_Last;
6485    end Pop_Scope;
6486
6487    ---------------
6488    -- Push_Scope --
6489    ---------------
6490
6491    procedure Push_Scope (S : Entity_Id) is
6492       E : Entity_Id;
6493
6494    begin
6495       if Ekind (S) = E_Void then
6496          null;
6497
6498       --  Set scope depth if not a non-concurrent type, and we have not yet set
6499       --  the scope depth. This means that we have the first occurrence of the
6500       --  scope, and this is where the depth is set.
6501
6502       elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
6503         and then not Scope_Depth_Set (S)
6504       then
6505          if S = Standard_Standard then
6506             Set_Scope_Depth_Value (S, Uint_0);
6507
6508          elsif Is_Child_Unit (S) then
6509             Set_Scope_Depth_Value (S, Uint_1);
6510
6511          elsif not Is_Record_Type (Current_Scope) then
6512             if Ekind (S) = E_Loop then
6513                Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
6514             else
6515                Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
6516             end if;
6517          end if;
6518       end if;
6519
6520       Scope_Stack.Increment_Last;
6521
6522       declare
6523          SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
6524
6525       begin
6526          SST.Entity                        := S;
6527          SST.Save_Scope_Suppress           := Scope_Suppress;
6528          SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
6529          SST.Save_Check_Policy_List        := Check_Policy_List;
6530
6531          if Scope_Stack.Last > Scope_Stack.First then
6532             SST.Component_Alignment_Default := Scope_Stack.Table
6533                                                  (Scope_Stack.Last - 1).
6534                                                    Component_Alignment_Default;
6535          end if;
6536
6537          SST.Last_Subprogram_Name           := null;
6538          SST.Is_Transient                   := False;
6539          SST.Node_To_Be_Wrapped             := Empty;
6540          SST.Pending_Freeze_Actions         := No_List;
6541          SST.Actions_To_Be_Wrapped_Before   := No_List;
6542          SST.Actions_To_Be_Wrapped_After    := No_List;
6543          SST.First_Use_Clause               := Empty;
6544          SST.Is_Active_Stack_Base           := False;
6545          SST.Previous_Visibility            := False;
6546       end;
6547
6548       if Debug_Flag_W then
6549          Write_Str ("--> new scope: ");
6550          Write_Name (Chars (Current_Scope));
6551          Write_Str (", Id=");
6552          Write_Int (Int (Current_Scope));
6553          Write_Str (", Depth=");
6554          Write_Int (Int (Scope_Stack.Last));
6555          Write_Eol;
6556       end if;
6557
6558       --  Deal with copying flags from the previous scope to this one. This is
6559       --  not necessary if either scope is standard, or if the new scope is a
6560       --  child unit.
6561
6562       if S /= Standard_Standard
6563         and then Scope (S) /= Standard_Standard
6564         and then not Is_Child_Unit (S)
6565       then
6566          E := Scope (S);
6567
6568          if Nkind (E) not in N_Entity then
6569             return;
6570          end if;
6571
6572          --  Copy categorization flags from Scope (S) to S, this is not done
6573          --  when Scope (S) is Standard_Standard since propagation is from
6574          --  library unit entity inwards. Copy other relevant attributes as
6575          --  well (Discard_Names in particular).
6576
6577          --  We only propagate inwards for library level entities,
6578          --  inner level subprograms do not inherit the categorization.
6579
6580          if Is_Library_Level_Entity (S) then
6581             Set_Is_Preelaborated  (S, Is_Preelaborated (E));
6582             Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
6583             Set_Discard_Names     (S, Discard_Names (E));
6584             Set_Suppress_Value_Tracking_On_Call
6585                                   (S, Suppress_Value_Tracking_On_Call (E));
6586             Set_Categorization_From_Scope (E => S, Scop => E);
6587          end if;
6588       end if;
6589    end Push_Scope;
6590
6591    ---------------------
6592    -- Premature_Usage --
6593    ---------------------
6594
6595    procedure Premature_Usage (N : Node_Id) is
6596       Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
6597       E    : Entity_Id := Entity (N);
6598
6599    begin
6600       --  Within an instance, the analysis of the actual for a formal object
6601       --  does not see the name of the object itself. This is significant only
6602       --  if the object is an aggregate, where its analysis does not do any
6603       --  name resolution on component associations. (see 4717-008). In such a
6604       --  case, look for the visible homonym on the chain.
6605
6606       if In_Instance
6607         and then Present (Homonym (E))
6608       then
6609          E := Homonym (E);
6610
6611          while Present (E)
6612            and then not In_Open_Scopes (Scope (E))
6613          loop
6614             E := Homonym (E);
6615          end loop;
6616
6617          if Present (E) then
6618             Set_Entity (N, E);
6619             Set_Etype (N, Etype (E));
6620             return;
6621          end if;
6622       end if;
6623
6624       if Kind  = N_Component_Declaration then
6625          Error_Msg_N
6626            ("component&! cannot be used before end of record declaration", N);
6627
6628       elsif Kind  = N_Parameter_Specification then
6629          Error_Msg_N
6630            ("formal parameter&! cannot be used before end of specification",
6631             N);
6632
6633       elsif Kind  = N_Discriminant_Specification then
6634          Error_Msg_N
6635            ("discriminant&! cannot be used before end of discriminant part",
6636             N);
6637
6638       elsif Kind  = N_Procedure_Specification
6639         or else Kind = N_Function_Specification
6640       then
6641          Error_Msg_N
6642            ("subprogram&! cannot be used before end of its declaration",
6643             N);
6644
6645       elsif Kind = N_Full_Type_Declaration then
6646          Error_Msg_N
6647            ("type& cannot be used before end of its declaration!", N);
6648
6649       else
6650          Error_Msg_N
6651            ("object& cannot be used before end of its declaration!", N);
6652       end if;
6653    end Premature_Usage;
6654
6655    ------------------------
6656    -- Present_System_Aux --
6657    ------------------------
6658
6659    function Present_System_Aux (N : Node_Id := Empty) return Boolean is
6660       Loc      : Source_Ptr;
6661       Aux_Name : Unit_Name_Type;
6662       Unum     : Unit_Number_Type;
6663       Withn    : Node_Id;
6664       With_Sys : Node_Id;
6665       The_Unit : Node_Id;
6666
6667       function Find_System (C_Unit : Node_Id) return Entity_Id;
6668       --  Scan context clause of compilation unit to find with_clause
6669       --  for System.
6670
6671       -----------------
6672       -- Find_System --
6673       -----------------
6674
6675       function Find_System (C_Unit : Node_Id) return Entity_Id is
6676          With_Clause : Node_Id;
6677
6678       begin
6679          With_Clause := First (Context_Items (C_Unit));
6680          while Present (With_Clause) loop
6681             if (Nkind (With_Clause) = N_With_Clause
6682               and then Chars (Name (With_Clause)) = Name_System)
6683               and then Comes_From_Source (With_Clause)
6684             then
6685                return With_Clause;
6686             end if;
6687
6688             Next (With_Clause);
6689          end loop;
6690
6691          return Empty;
6692       end Find_System;
6693
6694    --  Start of processing for Present_System_Aux
6695
6696    begin
6697       --  The child unit may have been loaded and analyzed already
6698
6699       if Present (System_Aux_Id) then
6700          return True;
6701
6702       --  If no previous pragma for System.Aux, nothing to load
6703
6704       elsif No (System_Extend_Unit) then
6705          return False;
6706
6707       --  Use the unit name given in the pragma to retrieve the unit.
6708       --  Verify that System itself appears in the context clause of the
6709       --  current compilation. If System is not present, an error will
6710       --  have been reported already.
6711
6712       else
6713          With_Sys := Find_System (Cunit (Current_Sem_Unit));
6714
6715          The_Unit := Unit (Cunit (Current_Sem_Unit));
6716
6717          if No (With_Sys)
6718            and then
6719              (Nkind (The_Unit) = N_Package_Body
6720                 or else (Nkind (The_Unit) = N_Subprogram_Body
6721                            and then
6722                              not Acts_As_Spec (Cunit (Current_Sem_Unit))))
6723          then
6724             With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
6725          end if;
6726
6727          if No (With_Sys)
6728            and then Present (N)
6729          then
6730             --  If we are compiling a subunit, we need to examine its
6731             --  context as well (Current_Sem_Unit is the parent unit);
6732
6733             The_Unit := Parent (N);
6734             while Nkind (The_Unit) /= N_Compilation_Unit loop
6735                The_Unit := Parent (The_Unit);
6736             end loop;
6737
6738             if Nkind (Unit (The_Unit)) = N_Subunit then
6739                With_Sys := Find_System (The_Unit);
6740             end if;
6741          end if;
6742
6743          if No (With_Sys) then
6744             return False;
6745          end if;
6746
6747          Loc := Sloc (With_Sys);
6748          Get_Name_String (Chars (Expression (System_Extend_Unit)));
6749          Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
6750          Name_Buffer (1 .. 7) := "system.";
6751          Name_Buffer (Name_Len + 8) := '%';
6752          Name_Buffer (Name_Len + 9) := 's';
6753          Name_Len := Name_Len + 9;
6754          Aux_Name := Name_Find;
6755
6756          Unum :=
6757            Load_Unit
6758              (Load_Name  => Aux_Name,
6759               Required   => False,
6760               Subunit    => False,
6761               Error_Node => With_Sys);
6762
6763          if Unum /= No_Unit then
6764             Semantics (Cunit (Unum));
6765             System_Aux_Id :=
6766               Defining_Entity (Specification (Unit (Cunit (Unum))));
6767
6768             Withn :=
6769               Make_With_Clause (Loc,
6770                 Name =>
6771                   Make_Expanded_Name (Loc,
6772                     Chars  => Chars (System_Aux_Id),
6773                     Prefix => New_Reference_To (Scope (System_Aux_Id), Loc),
6774                     Selector_Name => New_Reference_To (System_Aux_Id, Loc)));
6775
6776             Set_Entity (Name (Withn), System_Aux_Id);
6777
6778             Set_Library_Unit       (Withn, Cunit (Unum));
6779             Set_Corresponding_Spec (Withn, System_Aux_Id);
6780             Set_First_Name         (Withn, True);
6781             Set_Implicit_With      (Withn, True);
6782
6783             Insert_After (With_Sys, Withn);
6784             Mark_Rewrite_Insertion (Withn);
6785             Set_Context_Installed (Withn);
6786
6787             return True;
6788
6789          --  Here if unit load failed
6790
6791          else
6792             Error_Msg_Name_1 := Name_System;
6793             Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
6794             Error_Msg_N
6795               ("extension package `%.%` does not exist",
6796                Opt.System_Extend_Unit);
6797             return False;
6798          end if;
6799       end if;
6800    end Present_System_Aux;
6801
6802    -------------------------
6803    -- Restore_Scope_Stack --
6804    -------------------------
6805
6806    procedure Restore_Scope_Stack (Handle_Use : Boolean := True) is
6807       E         : Entity_Id;
6808       S         : Entity_Id;
6809       Comp_Unit : Node_Id;
6810       In_Child  : Boolean := False;
6811       Full_Vis  : Boolean := True;
6812       SS_Last   : constant Int := Scope_Stack.Last;
6813
6814    begin
6815       --  Restore visibility of previous scope stack, if any
6816
6817       for J in reverse 0 .. Scope_Stack.Last loop
6818          exit when  Scope_Stack.Table (J).Entity = Standard_Standard
6819             or else No (Scope_Stack.Table (J).Entity);
6820
6821          S := Scope_Stack.Table (J).Entity;
6822
6823          if not Is_Hidden_Open_Scope (S) then
6824
6825             --  If the parent scope is hidden, its entities are hidden as
6826             --  well, unless the entity is the instantiation currently
6827             --  being analyzed.
6828
6829             if not Is_Hidden_Open_Scope (Scope (S))
6830               or else not Analyzed (Parent (S))
6831               or else Scope (S) = Standard_Standard
6832             then
6833                Set_Is_Immediately_Visible (S, True);
6834             end if;
6835
6836             E := First_Entity (S);
6837             while Present (E) loop
6838                if Is_Child_Unit (E) then
6839                   if not From_With_Type (E) then
6840                      Set_Is_Immediately_Visible (E,
6841                        Is_Visible_Child_Unit (E) or else In_Open_Scopes (E));
6842
6843                   else
6844                      pragma Assert
6845                        (Nkind (Parent (E)) = N_Defining_Program_Unit_Name
6846                           and then
6847                         Nkind (Parent (Parent (E))) = N_Package_Specification);
6848                      Set_Is_Immediately_Visible (E,
6849                        Limited_View_Installed (Parent (Parent (E))));
6850                   end if;
6851                else
6852                   Set_Is_Immediately_Visible (E, True);
6853                end if;
6854
6855                Next_Entity (E);
6856
6857                if not Full_Vis
6858                  and then Is_Package_Or_Generic_Package (S)
6859                then
6860                   --  We are in the visible part of the package scope
6861
6862                   exit when E = First_Private_Entity (S);
6863                end if;
6864             end loop;
6865
6866             --  The visibility of child units (siblings of current compilation)
6867             --  must be restored in any case. Their declarations may appear
6868             --  after the private part of the parent.
6869
6870             if not Full_Vis then
6871                while Present (E) loop
6872                   if Is_Child_Unit (E) then
6873                      Set_Is_Immediately_Visible (E,
6874                        Is_Visible_Child_Unit (E) or else In_Open_Scopes (E));
6875                   end if;
6876
6877                   Next_Entity (E);
6878                end loop;
6879             end if;
6880          end if;
6881
6882          if Is_Child_Unit (S)
6883             and not In_Child     --  check only for current unit
6884          then
6885             In_Child := True;
6886
6887             --  Restore visibility of parents according to whether the child
6888             --  is private and whether we are in its visible part.
6889
6890             Comp_Unit := Parent (Unit_Declaration_Node (S));
6891
6892             if Nkind (Comp_Unit) = N_Compilation_Unit
6893               and then Private_Present (Comp_Unit)
6894             then
6895                Full_Vis := True;
6896
6897             elsif Is_Package_Or_Generic_Package (S)
6898               and then (In_Private_Part (S) or else In_Package_Body (S))
6899             then
6900                Full_Vis := True;
6901
6902             --  if S is the scope of some instance (which has already been
6903             --  seen on the stack) it does not affect the visibility of
6904             --  other scopes.
6905
6906             elsif Is_Hidden_Open_Scope (S) then
6907                null;
6908
6909             elsif (Ekind (S) = E_Procedure
6910                     or else Ekind (S) = E_Function)
6911               and then Has_Completion (S)
6912             then
6913                Full_Vis := True;
6914             else
6915                Full_Vis := False;
6916             end if;
6917          else
6918             Full_Vis := True;
6919          end if;
6920       end loop;
6921
6922       if SS_Last >= Scope_Stack.First
6923         and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
6924         and then Handle_Use
6925       then
6926          Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
6927       end if;
6928    end Restore_Scope_Stack;
6929
6930    ----------------------
6931    -- Save_Scope_Stack --
6932    ----------------------
6933
6934    procedure Save_Scope_Stack (Handle_Use : Boolean := True) is
6935       E       : Entity_Id;
6936       S       : Entity_Id;
6937       SS_Last : constant Int := Scope_Stack.Last;
6938
6939    begin
6940       if SS_Last >= Scope_Stack.First
6941         and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
6942       then
6943          if Handle_Use then
6944             End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
6945          end if;
6946
6947          --  If the call is from within a compilation unit, as when called from
6948          --  Rtsfind, make current entries in scope stack invisible while we
6949          --  analyze the new unit.
6950
6951          for J in reverse 0 .. SS_Last loop
6952             exit when  Scope_Stack.Table (J).Entity = Standard_Standard
6953                or else No (Scope_Stack.Table (J).Entity);
6954
6955             S := Scope_Stack.Table (J).Entity;
6956             Set_Is_Immediately_Visible (S, False);
6957
6958             E := First_Entity (S);
6959             while Present (E) loop
6960                Set_Is_Immediately_Visible (E, False);
6961                Next_Entity (E);
6962             end loop;
6963          end loop;
6964
6965       end if;
6966    end Save_Scope_Stack;
6967
6968    -------------
6969    -- Set_Use --
6970    -------------
6971
6972    procedure Set_Use (L : List_Id) is
6973       Decl      : Node_Id;
6974       Pack_Name : Node_Id;
6975       Pack      : Entity_Id;
6976       Id        : Entity_Id;
6977
6978    begin
6979       if Present (L) then
6980          Decl := First (L);
6981          while Present (Decl) loop
6982             if Nkind (Decl) = N_Use_Package_Clause then
6983                Chain_Use_Clause (Decl);
6984
6985                Pack_Name := First (Names (Decl));
6986                while Present (Pack_Name) loop
6987                   Pack := Entity (Pack_Name);
6988
6989                   if Ekind (Pack) = E_Package
6990                     and then Applicable_Use (Pack_Name)
6991                   then
6992                      Use_One_Package (Pack, Decl);
6993                   end if;
6994
6995                   Next (Pack_Name);
6996                end loop;
6997
6998             elsif Nkind (Decl) = N_Use_Type_Clause  then
6999                Chain_Use_Clause (Decl);
7000
7001                Id := First (Subtype_Marks (Decl));
7002                while Present (Id) loop
7003                   if Entity (Id) /= Any_Type then
7004                      Use_One_Type (Id);
7005                   end if;
7006
7007                   Next (Id);
7008                end loop;
7009             end if;
7010
7011             Next (Decl);
7012          end loop;
7013       end if;
7014    end Set_Use;
7015
7016    ---------------------
7017    -- Use_One_Package --
7018    ---------------------
7019
7020    procedure Use_One_Package (P : Entity_Id; N : Node_Id) is
7021       Id               : Entity_Id;
7022       Prev             : Entity_Id;
7023       Current_Instance : Entity_Id := Empty;
7024       Real_P           : Entity_Id;
7025       Private_With_OK  : Boolean   := False;
7026
7027    begin
7028       if Ekind (P) /= E_Package then
7029          return;
7030       end if;
7031
7032       Set_In_Use (P);
7033       Set_Current_Use_Clause (P, N);
7034
7035       --  Ada 2005 (AI-50217): Check restriction
7036
7037       if From_With_Type (P) then
7038          Error_Msg_N ("limited withed package cannot appear in use clause", N);
7039       end if;
7040
7041       --  Find enclosing instance, if any
7042
7043       if In_Instance then
7044          Current_Instance := Current_Scope;
7045          while not Is_Generic_Instance (Current_Instance) loop
7046             Current_Instance := Scope (Current_Instance);
7047          end loop;
7048
7049          if No (Hidden_By_Use_Clause (N)) then
7050             Set_Hidden_By_Use_Clause (N, New_Elmt_List);
7051          end if;
7052       end if;
7053
7054       --  If unit is a package renaming, indicate that the renamed
7055       --  package is also in use (the flags on both entities must
7056       --  remain consistent, and a subsequent use of either of them
7057       --  should be recognized as redundant).
7058
7059       if Present (Renamed_Object (P)) then
7060          Set_In_Use (Renamed_Object (P));
7061          Set_Current_Use_Clause (Renamed_Object (P), N);
7062          Real_P := Renamed_Object (P);
7063       else
7064          Real_P := P;
7065       end if;
7066
7067       --  Ada 2005 (AI-262): Check the use_clause of a private withed package
7068       --  found in the private part of a package specification
7069
7070       if In_Private_Part (Current_Scope)
7071         and then Has_Private_With (P)
7072         and then Is_Child_Unit (Current_Scope)
7073         and then Is_Child_Unit (P)
7074         and then Is_Ancestor_Package (Scope (Current_Scope), P)
7075       then
7076          Private_With_OK := True;
7077       end if;
7078
7079       --  Loop through entities in one package making them potentially
7080       --  use-visible.
7081
7082       Id := First_Entity (P);
7083       while Present (Id)
7084         and then (Id /= First_Private_Entity (P)
7085                     or else Private_With_OK) -- Ada 2005 (AI-262)
7086       loop
7087          Prev := Current_Entity (Id);
7088          while Present (Prev) loop
7089             if Is_Immediately_Visible (Prev)
7090               and then (not Is_Overloadable (Prev)
7091                          or else not Is_Overloadable (Id)
7092                          or else (Type_Conformant (Id, Prev)))
7093             then
7094                if No (Current_Instance) then
7095
7096                   --  Potentially use-visible entity remains hidden
7097
7098                   goto Next_Usable_Entity;
7099
7100                --  A use clause within an instance hides outer global entities,
7101                --  which are not used to resolve local entities in the
7102                --  instance. Note that the predefined entities in Standard
7103                --  could not have been hidden in the generic by a use clause,
7104                --  and therefore remain visible. Other compilation units whose
7105                --  entities appear in Standard must be hidden in an instance.
7106
7107                --  To determine whether an entity is external to the instance
7108                --  we compare the scope depth of its scope with that of the
7109                --  current instance. However, a generic actual of a subprogram
7110                --  instance is declared in the wrapper package but will not be
7111                --  hidden by a use-visible entity.
7112
7113                --  If Id is called Standard, the predefined package with the
7114                --  same name is in the homonym chain. It has to be ignored
7115                --  because it has no defined scope (being the only entity in
7116                --  the system with this mandated behavior).
7117
7118                elsif not Is_Hidden (Id)
7119                  and then Present (Scope (Prev))
7120                  and then not Is_Wrapper_Package (Scope (Prev))
7121                  and then Scope_Depth (Scope (Prev)) <
7122                           Scope_Depth (Current_Instance)
7123                  and then (Scope (Prev) /= Standard_Standard
7124                             or else Sloc (Prev) > Standard_Location)
7125                then
7126                   Set_Is_Potentially_Use_Visible (Id);
7127                   Set_Is_Immediately_Visible (Prev, False);
7128                   Append_Elmt (Prev, Hidden_By_Use_Clause (N));
7129                end if;
7130
7131             --  A user-defined operator is not use-visible if the predefined
7132             --  operator for the type is immediately visible, which is the case
7133             --  if the type of the operand is in an open scope. This does not
7134             --  apply to user-defined operators that have operands of different
7135             --  types, because the predefined mixed mode operations (multiply
7136             --  and divide) apply to universal types and do not hide anything.
7137
7138             elsif Ekind (Prev) = E_Operator
7139               and then Operator_Matches_Spec (Prev, Id)
7140               and then In_Open_Scopes
7141                (Scope (Base_Type (Etype (First_Formal (Id)))))
7142               and then (No (Next_Formal (First_Formal (Id)))
7143                          or else Etype (First_Formal (Id))
7144                            = Etype (Next_Formal (First_Formal (Id)))
7145                          or else Chars (Prev) = Name_Op_Expon)
7146             then
7147                goto Next_Usable_Entity;
7148
7149             --  In an instance, two homonyms may become use_visible through the
7150             --  actuals of distinct formal packages. In the generic, only the
7151             --  current one would have been visible, so make the other one
7152             --  not use_visible.
7153
7154             elsif Present (Current_Instance)
7155               and then Is_Potentially_Use_Visible (Prev)
7156               and then not Is_Overloadable (Prev)
7157               and then Scope (Id) /= Scope (Prev)
7158               and then Used_As_Generic_Actual (Scope (Prev))
7159               and then Used_As_Generic_Actual (Scope (Id))
7160               and then List_Containing (Current_Use_Clause (Scope (Prev))) /=
7161                        List_Containing (Current_Use_Clause (Scope (Id)))
7162             then
7163                Set_Is_Potentially_Use_Visible (Prev, False);
7164                Append_Elmt (Prev, Hidden_By_Use_Clause (N));
7165             end if;
7166
7167             Prev := Homonym (Prev);
7168          end loop;
7169
7170          --  On exit, we know entity is not hidden, unless it is private
7171
7172          if not Is_Hidden (Id)
7173            and then ((not Is_Child_Unit (Id))
7174                        or else Is_Visible_Child_Unit (Id))
7175          then
7176             Set_Is_Potentially_Use_Visible (Id);
7177
7178             if Is_Private_Type (Id)
7179               and then Present (Full_View (Id))
7180             then
7181                Set_Is_Potentially_Use_Visible (Full_View (Id));
7182             end if;
7183          end if;
7184
7185          <<Next_Usable_Entity>>
7186             Next_Entity (Id);
7187       end loop;
7188
7189       --  Child units are also made use-visible by a use clause, but they may
7190       --  appear after all visible declarations in the parent entity list.
7191
7192       while Present (Id) loop
7193          if Is_Child_Unit (Id)
7194            and then Is_Visible_Child_Unit (Id)
7195          then
7196             Set_Is_Potentially_Use_Visible (Id);
7197          end if;
7198
7199          Next_Entity (Id);
7200       end loop;
7201
7202       if Chars (Real_P) = Name_System
7203         and then Scope (Real_P) = Standard_Standard
7204         and then Present_System_Aux (N)
7205       then
7206          Use_One_Package (System_Aux_Id, N);
7207       end if;
7208
7209    end Use_One_Package;
7210
7211    ------------------
7212    -- Use_One_Type --
7213    ------------------
7214
7215    procedure Use_One_Type (Id : Node_Id) is
7216       Elmt          : Elmt_Id;
7217       Is_Known_Used : Boolean;
7218       Op_List       : Elist_Id;
7219       T             : Entity_Id;
7220
7221       function Spec_Reloaded_For_Body return Boolean;
7222       --  Determine whether the compilation unit is a package body and the use
7223       --  type clause is in the spec of the same package. Even though the spec
7224       --  was analyzed first, its context is reloaded when analysing the body.
7225
7226       ----------------------------
7227       -- Spec_Reloaded_For_Body --
7228       ----------------------------
7229
7230       function Spec_Reloaded_For_Body return Boolean is
7231       begin
7232          if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
7233             declare
7234                Spec : constant Node_Id :=
7235                         Parent (List_Containing (Parent (Id)));
7236             begin
7237                return
7238                  Nkind (Spec) = N_Package_Specification
7239                    and then Corresponding_Body (Parent (Spec)) =
7240                               Cunit_Entity (Current_Sem_Unit);
7241             end;
7242          end if;
7243
7244          return False;
7245       end Spec_Reloaded_For_Body;
7246
7247    --  Start of processing for Use_One_Type;
7248
7249    begin
7250       --  It is the type determined by the subtype mark (8.4(8)) whose
7251       --  operations become potentially use-visible.
7252
7253       T := Base_Type (Entity (Id));
7254
7255       --  Either the type itself is used, the package where it is declared
7256       --  is in use or the entity is declared in the current package, thus
7257       --  use-visible.
7258
7259       Is_Known_Used :=
7260         In_Use (T)
7261           or else In_Use (Scope (T))
7262           or else Scope (T) = Current_Scope;
7263
7264       Set_Redundant_Use (Id,
7265         Is_Known_Used or else Is_Potentially_Use_Visible (T));
7266
7267       if Ekind (T) = E_Incomplete_Type then
7268          Error_Msg_N ("premature usage of incomplete type", Id);
7269
7270       elsif In_Open_Scopes (Scope (T)) then
7271          null;
7272
7273       --  A limited view cannot appear in a use_type clause. However, an access
7274       --  type whose designated type is limited has the flag but is not itself
7275       --  a limited view unless we only have a limited view of its enclosing
7276       --  package.
7277
7278       elsif From_With_Type (T)
7279         and then From_With_Type (Scope (T))
7280       then
7281          Error_Msg_N
7282            ("incomplete type from limited view "
7283              & "cannot appear in use clause", Id);
7284
7285       --  If the subtype mark designates a subtype in a different package,
7286       --  we have to check that the parent type is visible, otherwise the
7287       --  use type clause is a noop. Not clear how to do that???
7288
7289       elsif not Redundant_Use (Id) then
7290          Set_In_Use (T);
7291
7292          --  If T is tagged, primitive operators on class-wide operands
7293          --  are also available.
7294
7295          if Is_Tagged_Type (T) then
7296             Set_In_Use (Class_Wide_Type (T));
7297          end if;
7298
7299          Set_Current_Use_Clause (T, Parent (Id));
7300          Op_List := Collect_Primitive_Operations (T);
7301
7302          Elmt := First_Elmt (Op_List);
7303          while Present (Elmt) loop
7304             if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
7305                  or else Chars (Node (Elmt)) in Any_Operator_Name)
7306               and then not Is_Hidden (Node (Elmt))
7307             then
7308                Set_Is_Potentially_Use_Visible (Node (Elmt));
7309             end if;
7310
7311             Next_Elmt (Elmt);
7312          end loop;
7313       end if;
7314
7315       --  If warning on redundant constructs, check for unnecessary WITH
7316
7317       if Warn_On_Redundant_Constructs
7318         and then Is_Known_Used
7319
7320          --                     with P;         with P; use P;
7321          --    package P is     package X is    package body X is
7322          --       type T ...       use P.T;
7323
7324          --  The compilation unit is the body of X. GNAT first compiles the
7325          --  spec of X, then proceeds to the body. At that point P is marked
7326          --  as use visible. The analysis then reinstalls the spec along with
7327          --  its context. The use clause P.T is now recognized as redundant,
7328          --  but in the wrong context. Do not emit a warning in such cases.
7329          --  Do not emit a warning either if we are in an instance, there is
7330          --  no redundancy between an outer use_clause and one that appears
7331          --  within the generic.
7332
7333         and then not Spec_Reloaded_For_Body
7334         and then not In_Instance
7335       then
7336          --  The type already has a use clause
7337
7338          if In_Use (T) then
7339
7340             --  Case where we know the current use clause for the type
7341
7342             if Present (Current_Use_Clause (T)) then
7343                Use_Clause_Known : declare
7344                   Clause1 : constant Node_Id := Parent (Id);
7345                   Clause2 : constant Node_Id := Current_Use_Clause (T);
7346                   Ent1    : Entity_Id;
7347                   Ent2    : Entity_Id;
7348                   Err_No  : Node_Id;
7349                   Unit1   : Node_Id;
7350                   Unit2   : Node_Id;
7351
7352                   function Entity_Of_Unit (U : Node_Id) return Entity_Id;
7353                   --  Return the appropriate entity for determining which unit
7354                   --  has a deeper scope: the defining entity for U, unless U
7355                   --  is a package instance, in which case we retrieve the
7356                   --  entity of the instance spec.
7357
7358                   --------------------
7359                   -- Entity_Of_Unit --
7360                   --------------------
7361
7362                   function Entity_Of_Unit (U : Node_Id) return Entity_Id is
7363                   begin
7364                      if Nkind (U) =  N_Package_Instantiation
7365                        and then Analyzed (U)
7366                      then
7367                         return Defining_Entity (Instance_Spec (U));
7368                      else
7369                         return Defining_Entity (U);
7370                      end if;
7371                   end Entity_Of_Unit;
7372
7373                --  Start of processing for Use_Clause_Known
7374
7375                begin
7376                   --  If both current use type clause and the use type clause
7377                   --  for the type are at the compilation unit level, one of
7378                   --  the units must be an ancestor of the other, and the
7379                   --  warning belongs on the descendant.
7380
7381                   if Nkind (Parent (Clause1)) = N_Compilation_Unit
7382                        and then
7383                      Nkind (Parent (Clause2)) = N_Compilation_Unit
7384                   then
7385
7386                      --  If the unit is a subprogram body that acts as spec,
7387                      --  the context clause is shared with the constructed
7388                      --  subprogram spec. Clearly there is no redundancy.
7389
7390                      if Clause1 = Clause2 then
7391                         return;
7392                      end if;
7393
7394                      Unit1 := Unit (Parent (Clause1));
7395                      Unit2 := Unit (Parent (Clause2));
7396
7397                      --  If both clauses are on same unit, or one is the body
7398                      --  of the other, or one of them is in a subunit, report
7399                      --  redundancy on the later one.
7400
7401                      if Unit1 = Unit2 then
7402                         Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
7403                         Error_Msg_NE
7404                           ("& is already use-visible through previous "
7405                            & "use_type_clause #?", Clause1, T);
7406                         return;
7407
7408                      elsif Nkind (Unit1) = N_Subunit then
7409                         Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
7410                         Error_Msg_NE
7411                           ("& is already use-visible through previous "
7412                            & "use_type_clause #?", Clause1, T);
7413                         return;
7414
7415                      elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body)
7416                        and then Nkind (Unit1) /= Nkind (Unit2)
7417                        and then Nkind (Unit1) /= N_Subunit
7418                      then
7419                         Error_Msg_Sloc := Sloc (Clause1);
7420                         Error_Msg_NE
7421                           ("& is already use-visible through previous "
7422                            & "use_type_clause #?", Current_Use_Clause (T), T);
7423                         return;
7424                      end if;
7425
7426                      --  There is a redundant use type clause in a child unit.
7427                      --  Determine which of the units is more deeply nested.
7428                      --  If a unit is a package instance, retrieve the entity
7429                      --  and its scope from the instance spec.
7430
7431                      Ent1 := Entity_Of_Unit (Unit1);
7432                      Ent2 := Entity_Of_Unit (Unit2);
7433
7434                      if Scope (Ent2) = Standard_Standard  then
7435                         Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
7436                         Err_No := Clause1;
7437
7438                      elsif Scope (Ent1) = Standard_Standard then
7439                         Error_Msg_Sloc := Sloc (Id);
7440                         Err_No := Clause2;
7441
7442                      --  If both units are child units, we determine which one
7443                      --  is the descendant by the scope distance to the
7444                      --  ultimate parent unit.
7445
7446                      else
7447                         declare
7448                            S1, S2 : Entity_Id;
7449
7450                         begin
7451                            S1 := Scope (Ent1);
7452                            S2 := Scope (Ent2);
7453                            while S1 /= Standard_Standard
7454                                    and then
7455                                  S2 /= Standard_Standard
7456                            loop
7457                               S1 := Scope (S1);
7458                               S2 := Scope (S2);
7459                            end loop;
7460
7461                            if S1 = Standard_Standard then
7462                               Error_Msg_Sloc := Sloc (Id);
7463                               Err_No := Clause2;
7464                            else
7465                               Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
7466                               Err_No := Clause1;
7467                            end if;
7468                         end;
7469                      end if;
7470
7471                      Error_Msg_NE
7472                        ("& is already use-visible through previous "
7473                         & "use_type_clause #?", Err_No, Id);
7474
7475                   --  Case where current use type clause and the use type
7476                   --  clause for the type are not both at the compilation unit
7477                   --  level. In this case we don't have location information.
7478
7479                   else
7480                      Error_Msg_NE
7481                        ("& is already use-visible through previous "
7482                         & "use type clause?", Id, T);
7483                   end if;
7484                end Use_Clause_Known;
7485
7486             --  Here if Current_Use_Clause is not set for T, another case
7487             --  where we do not have the location information available.
7488
7489             else
7490                Error_Msg_NE
7491                  ("& is already use-visible through previous "
7492                   & "use type clause?", Id, T);
7493             end if;
7494
7495          --  The package where T is declared is already used
7496
7497          elsif In_Use (Scope (T)) then
7498             Error_Msg_Sloc := Sloc (Current_Use_Clause (Scope (T)));
7499             Error_Msg_NE
7500               ("& is already use-visible through package use clause #?",
7501                Id, T);
7502
7503          --  The current scope is the package where T is declared
7504
7505          else
7506             Error_Msg_Node_2 := Scope (T);
7507             Error_Msg_NE
7508               ("& is already use-visible inside package &?", Id, T);
7509          end if;
7510       end if;
7511    end Use_One_Type;
7512
7513    ----------------
7514    -- Write_Info --
7515    ----------------
7516
7517    procedure Write_Info is
7518       Id : Entity_Id := First_Entity (Current_Scope);
7519
7520    begin
7521       --  No point in dumping standard entities
7522
7523       if Current_Scope = Standard_Standard then
7524          return;
7525       end if;
7526
7527       Write_Str ("========================================================");
7528       Write_Eol;
7529       Write_Str ("        Defined Entities in ");
7530       Write_Name (Chars (Current_Scope));
7531       Write_Eol;
7532       Write_Str ("========================================================");
7533       Write_Eol;
7534
7535       if No (Id) then
7536          Write_Str ("-- none --");
7537          Write_Eol;
7538
7539       else
7540          while Present (Id) loop
7541             Write_Entity_Info (Id, " ");
7542             Next_Entity (Id);
7543          end loop;
7544       end if;
7545
7546       if Scope (Current_Scope) = Standard_Standard then
7547
7548          --  Print information on the current unit itself
7549
7550          Write_Entity_Info (Current_Scope, " ");
7551       end if;
7552
7553       Write_Eol;
7554    end Write_Info;
7555
7556    -----------------
7557    -- Write_Scopes --
7558    -----------------
7559
7560    procedure Write_Scopes is
7561       S : Entity_Id;
7562    begin
7563       for J in reverse 1 .. Scope_Stack.Last loop
7564          S :=  Scope_Stack.Table (J).Entity;
7565          Write_Int (Int (S));
7566          Write_Str (" === ");
7567          Write_Name (Chars (S));
7568          Write_Eol;
7569       end loop;
7570    end Write_Scopes;
7571
7572 end Sem_Ch8;