OSDN Git Service

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