OSDN Git Service

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