OSDN Git Service

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