OSDN Git Service

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