OSDN Git Service

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