OSDN Git Service

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