OSDN Git Service

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