OSDN Git Service

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