OSDN Git Service

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