OSDN Git Service

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