OSDN Git Service

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