OSDN Git Service

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