OSDN Git Service

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