OSDN Git Service

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