OSDN Git Service

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