OSDN Git Service

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