OSDN Git Service

* sem_ch8.adb (Find_Type): If node is a reference to 'Base and the
[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       if Is_Compilation_Unit (New_S) then
1922          Error_Msg_N
1923            ("a library unit can only rename another library unit", N);
1924       end if;
1925
1926       Set_Etype (New_S, Base_Type (Etype (New_S)));
1927
1928       --  We suppress elaboration warnings for the resulting entity, since
1929       --  clearly they are not needed, and more particularly, in the case
1930       --  of a generic formal subprogram, the resulting entity can appear
1931       --  after the instantiation itself, and thus look like a bogus case
1932       --  of access before elaboration.
1933
1934       Set_Suppress_Elaboration_Warnings (New_S);
1935
1936    end Attribute_Renaming;
1937
1938    ----------------------
1939    -- Chain_Use_Clause --
1940    ----------------------
1941
1942    procedure Chain_Use_Clause (N : Node_Id) is
1943    begin
1944       Set_Next_Use_Clause (N,
1945         Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause);
1946       Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause := N;
1947    end Chain_Use_Clause;
1948
1949    ---------------------------
1950    -- Check_Frozen_Renaming --
1951    ---------------------------
1952
1953    procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
1954       B_Node : Node_Id;
1955       Old_S  : Entity_Id;
1956
1957    begin
1958       if Is_Frozen (Subp)
1959         and then not Has_Completion (Subp)
1960       then
1961          B_Node :=
1962            Build_Renamed_Body
1963              (Parent (Declaration_Node (Subp)), Defining_Entity (N));
1964
1965          if Is_Entity_Name (Name (N)) then
1966             Old_S := Entity (Name (N));
1967
1968             if not Is_Frozen (Old_S)
1969               and then Operating_Mode /= Check_Semantics
1970             then
1971                Append_Freeze_Action (Old_S, B_Node);
1972             else
1973                Insert_After (N, B_Node);
1974                Analyze (B_Node);
1975             end if;
1976
1977             if Is_Intrinsic_Subprogram (Old_S)
1978               and then not In_Instance
1979             then
1980                Error_Msg_N
1981                  ("subprogram used in renaming_as_body cannot be intrinsic",
1982                     Name (N));
1983             end if;
1984
1985          else
1986             Insert_After (N, B_Node);
1987             Analyze (B_Node);
1988          end if;
1989       end if;
1990    end Check_Frozen_Renaming;
1991
1992    -----------------------------------
1993    -- Check_In_Previous_With_Clause --
1994    -----------------------------------
1995
1996    procedure Check_In_Previous_With_Clause
1997      (N   : Node_Id;
1998       Nam : Entity_Id)
1999    is
2000       Pack : constant Entity_Id := Entity (Original_Node (Nam));
2001       Item : Node_Id;
2002       Par  : Node_Id;
2003
2004    begin
2005       Item := First (Context_Items (Parent (N)));
2006
2007       while Present (Item)
2008         and then Item /= N
2009       loop
2010          if Nkind (Item) = N_With_Clause
2011            and then Entity (Name (Item)) = Pack
2012          then
2013             Par := Nam;
2014
2015             --  Find root library unit in with_clause
2016
2017             while Nkind (Par) = N_Expanded_Name loop
2018                Par := Prefix (Par);
2019             end loop;
2020
2021             if Is_Child_Unit (Entity (Original_Node (Par))) then
2022                Error_Msg_NE
2023                  ("& is not directly visible", Par, Entity (Par));
2024             else
2025                return;
2026             end if;
2027          end if;
2028
2029          Next (Item);
2030       end loop;
2031
2032       --  On exit, package is not mentioned in a previous with_clause.
2033       --  Check if its prefix is.
2034
2035       if Nkind (Nam) = N_Expanded_Name then
2036          Check_In_Previous_With_Clause (N, Prefix (Nam));
2037
2038       elsif Pack /= Any_Id then
2039          Error_Msg_NE ("& is not visible", Nam, Pack);
2040       end if;
2041    end Check_In_Previous_With_Clause;
2042
2043    ---------------------------------
2044    -- Check_Library_Unit_Renaming --
2045    ---------------------------------
2046
2047    procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
2048       New_E : Entity_Id;
2049
2050    begin
2051       if Nkind (Parent (N)) /= N_Compilation_Unit then
2052          return;
2053
2054       elsif Scope (Old_E) /= Standard_Standard
2055         and then not Is_Child_Unit (Old_E)
2056       then
2057          Error_Msg_N ("renamed unit must be a library unit", Name (N));
2058
2059       --  Entities defined in Standard (operators and boolean literals) cannot
2060       --  be renamed as library units.
2061
2062       elsif Scope (Old_E) = Standard_Standard
2063         and then Sloc (Old_E) = Standard_Location
2064       then
2065          Error_Msg_N ("renamed unit must be a library unit", Name (N));
2066
2067       elsif Present (Parent_Spec (N))
2068         and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
2069         and then not Is_Child_Unit (Old_E)
2070       then
2071          Error_Msg_N
2072            ("renamed unit must be a child unit of generic parent", Name (N));
2073
2074       elsif Nkind (N) in N_Generic_Renaming_Declaration
2075          and then  Nkind (Name (N)) = N_Expanded_Name
2076          and then Is_Generic_Instance (Entity (Prefix (Name (N))))
2077          and then Is_Generic_Unit (Old_E)
2078       then
2079          Error_Msg_N
2080            ("renamed generic unit must be a library unit", Name (N));
2081
2082       elsif Ekind (Old_E) = E_Package
2083         or else Ekind (Old_E) = E_Generic_Package
2084       then
2085          --  Inherit categorization flags
2086
2087          New_E := Defining_Entity (N);
2088          Set_Is_Pure                  (New_E, Is_Pure           (Old_E));
2089          Set_Is_Preelaborated         (New_E, Is_Preelaborated  (Old_E));
2090          Set_Is_Remote_Call_Interface (New_E,
2091                                        Is_Remote_Call_Interface (Old_E));
2092          Set_Is_Remote_Types          (New_E, Is_Remote_Types   (Old_E));
2093          Set_Is_Shared_Passive        (New_E, Is_Shared_Passive (Old_E));
2094       end if;
2095    end Check_Library_Unit_Renaming;
2096
2097    ---------------
2098    -- End_Scope --
2099    ---------------
2100
2101    procedure End_Scope is
2102       Id    : Entity_Id;
2103       Prev  : Entity_Id;
2104       Outer : Entity_Id;
2105
2106    begin
2107       Id := First_Entity (Current_Scope);
2108
2109       while Present (Id) loop
2110          --  An entity in the current scope is not necessarily the first one
2111          --  on its homonym chain. Find its predecessor if any,
2112          --  If it is an internal entity, it will not be in the visibility
2113          --  chain altogether,  and there is nothing to unchain.
2114
2115          if Id /= Current_Entity (Id) then
2116             Prev := Current_Entity (Id);
2117             while Present (Prev)
2118               and then Present (Homonym (Prev))
2119               and then Homonym (Prev) /= Id
2120             loop
2121                Prev := Homonym (Prev);
2122             end loop;
2123
2124             --  Skip to end of loop if Id is not in the visibility chain
2125
2126             if No (Prev) or else Homonym (Prev) /= Id then
2127                goto Next_Ent;
2128             end if;
2129
2130          else
2131             Prev := Empty;
2132          end if;
2133
2134          Outer := Homonym (Id);
2135          Set_Is_Immediately_Visible (Id, False);
2136
2137          while Present (Outer) and then Scope (Outer) = Current_Scope loop
2138             Outer := Homonym (Outer);
2139          end loop;
2140
2141          --  Reset homonym link of other entities, but do not modify link
2142          --  between entities in current scope, so that the back-end can have
2143          --  a proper count of local overloadings.
2144
2145          if No (Prev) then
2146             Set_Name_Entity_Id (Chars (Id), Outer);
2147
2148          elsif Scope (Prev) /= Scope (Id) then
2149             Set_Homonym (Prev,  Outer);
2150          end if;
2151
2152          <<Next_Ent>>
2153             Next_Entity (Id);
2154       end loop;
2155
2156       --  If the scope generated freeze actions, place them before the
2157       --  current declaration and analyze them. Type declarations and
2158       --  the bodies of initialization procedures can generate such nodes.
2159       --  We follow the parent chain until we reach a list node, which is
2160       --  the enclosing list of declarations. If the list appears within
2161       --  a protected definition, move freeze nodes outside the protected
2162       --  type altogether.
2163
2164       if Present
2165          (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
2166       then
2167          declare
2168             Decl : Node_Id;
2169             L    : constant List_Id := Scope_Stack.Table
2170                     (Scope_Stack.Last).Pending_Freeze_Actions;
2171
2172          begin
2173             if Is_Itype (Current_Scope) then
2174                Decl := Associated_Node_For_Itype (Current_Scope);
2175             else
2176                Decl := Parent (Current_Scope);
2177             end if;
2178
2179             Pop_Scope;
2180
2181             while not (Is_List_Member (Decl))
2182               or else Nkind (Parent (Decl)) = N_Protected_Definition
2183               or else Nkind (Parent (Decl)) = N_Task_Definition
2184             loop
2185                Decl := Parent (Decl);
2186             end loop;
2187
2188             Insert_List_Before_And_Analyze (Decl, L);
2189          end;
2190
2191       else
2192          Pop_Scope;
2193       end if;
2194
2195    end End_Scope;
2196
2197    ---------------------
2198    -- End_Use_Clauses --
2199    ---------------------
2200
2201    procedure End_Use_Clauses (Clause : Node_Id) is
2202       U   : Node_Id;
2203
2204    begin
2205       --  Remove Use_Type clauses first, because they affect the
2206       --  visibility of operators in subsequent used packages.
2207
2208       U := Clause;
2209       while Present (U) loop
2210          if Nkind (U) = N_Use_Type_Clause then
2211             End_Use_Type (U);
2212          end if;
2213
2214          Next_Use_Clause (U);
2215       end loop;
2216
2217       U := Clause;
2218       while Present (U) loop
2219          if Nkind (U) = N_Use_Package_Clause then
2220             End_Use_Package (U);
2221          end if;
2222
2223          Next_Use_Clause (U);
2224       end loop;
2225    end End_Use_Clauses;
2226
2227    ---------------------
2228    -- End_Use_Package --
2229    ---------------------
2230
2231    procedure End_Use_Package (N : Node_Id) is
2232       Pack_Name : Node_Id;
2233       Pack      : Entity_Id;
2234       Id        : Entity_Id;
2235       Elmt      : Elmt_Id;
2236
2237       function Is_Primitive_Operator
2238         (Op : Entity_Id;
2239          F  : Entity_Id) return Boolean;
2240       --  Check whether Op is a primitive operator of a use-visible type
2241
2242       ---------------------------
2243       -- Is_Primitive_Operator --
2244       ---------------------------
2245
2246       function Is_Primitive_Operator
2247         (Op : Entity_Id;
2248          F  : Entity_Id) return Boolean
2249       is
2250          T : constant Entity_Id := Etype (F);
2251
2252       begin
2253          return In_Use (T)
2254            and then Scope (T) = Scope (Op);
2255       end Is_Primitive_Operator;
2256
2257    --  Start of processing for End_Use_Package
2258
2259    begin
2260       Pack_Name := First (Names (N));
2261
2262       while Present (Pack_Name) loop
2263          Pack := Entity (Pack_Name);
2264
2265          if Ekind (Pack) = E_Package then
2266
2267             if In_Open_Scopes (Pack) then
2268                null;
2269
2270             elsif not Redundant_Use (Pack_Name) then
2271                Set_In_Use (Pack, False);
2272                Id := First_Entity (Pack);
2273
2274                while Present (Id) loop
2275
2276                   --  Preserve use-visibility of operators that are primitive
2277                   --  operators of a type that is use_visible through an active
2278                   --  use_type clause.
2279
2280                   if Nkind (Id) = N_Defining_Operator_Symbol
2281                        and then
2282                          (Is_Primitive_Operator (Id, First_Formal (Id))
2283                             or else
2284                           (Present (Next_Formal (First_Formal (Id)))
2285                              and then
2286                                Is_Primitive_Operator
2287                                  (Id, Next_Formal (First_Formal (Id)))))
2288                   then
2289                      null;
2290
2291                   else
2292                      Set_Is_Potentially_Use_Visible (Id, False);
2293                   end if;
2294
2295                   if Is_Private_Type (Id)
2296                     and then Present (Full_View (Id))
2297                   then
2298                      Set_Is_Potentially_Use_Visible (Full_View (Id), False);
2299                   end if;
2300
2301                   Next_Entity (Id);
2302                end loop;
2303
2304                if Present (Renamed_Object (Pack)) then
2305                   Set_In_Use (Renamed_Object (Pack), False);
2306                end if;
2307
2308                if Chars (Pack) = Name_System
2309                  and then Scope (Pack) = Standard_Standard
2310                  and then Present_System_Aux
2311                then
2312                   Id := First_Entity (System_Aux_Id);
2313
2314                   while Present (Id) loop
2315                      Set_Is_Potentially_Use_Visible (Id, False);
2316
2317                      if Is_Private_Type (Id)
2318                        and then Present (Full_View (Id))
2319                      then
2320                         Set_Is_Potentially_Use_Visible (Full_View (Id), False);
2321                      end if;
2322
2323                      Next_Entity (Id);
2324                   end loop;
2325
2326                   Set_In_Use (System_Aux_Id, False);
2327                end if;
2328
2329             else
2330                Set_Redundant_Use (Pack_Name, False);
2331             end if;
2332
2333          end if;
2334
2335          Next (Pack_Name);
2336       end loop;
2337
2338       if Present (Hidden_By_Use_Clause (N)) then
2339          Elmt := First_Elmt (Hidden_By_Use_Clause (N));
2340
2341          while Present (Elmt) loop
2342             Set_Is_Immediately_Visible (Node (Elmt));
2343             Next_Elmt (Elmt);
2344          end loop;
2345
2346          Set_Hidden_By_Use_Clause (N, No_Elist);
2347       end if;
2348    end End_Use_Package;
2349
2350    ------------------
2351    -- End_Use_Type --
2352    ------------------
2353
2354    procedure End_Use_Type (N : Node_Id) is
2355       Id      : Entity_Id;
2356       Op_List : Elist_Id;
2357       Elmt    : Elmt_Id;
2358       T       : Entity_Id;
2359
2360    begin
2361       Id := First (Subtype_Marks (N));
2362
2363       while Present (Id) loop
2364
2365          --  A call to rtsfind may occur while analyzing a use_type clause,
2366          --  in which case the type marks are not resolved yet, and there is
2367          --  nothing to remove.
2368
2369          if not Is_Entity_Name (Id)
2370            or else No (Entity (Id))
2371          then
2372             goto Continue;
2373          end if;
2374
2375          T := Entity (Id);
2376
2377          if T = Any_Type then
2378             null;
2379
2380          --  Note that the use_Type clause may mention a subtype of the
2381          --  type whose primitive operations have been made visible. Here
2382          --  as elsewhere, it is the base type that matters for visibility.
2383
2384          elsif In_Open_Scopes (Scope (Base_Type (T))) then
2385             null;
2386
2387          elsif not Redundant_Use (Id) then
2388             Set_In_Use (T, False);
2389             Set_In_Use (Base_Type (T), False);
2390             Op_List := Collect_Primitive_Operations (T);
2391             Elmt := First_Elmt (Op_List);
2392
2393             while Present (Elmt) loop
2394
2395                if Nkind (Node (Elmt)) = N_Defining_Operator_Symbol then
2396                   Set_Is_Potentially_Use_Visible (Node (Elmt), False);
2397                end if;
2398
2399                Next_Elmt (Elmt);
2400             end loop;
2401          end if;
2402
2403          <<Continue>>
2404          Next (Id);
2405       end loop;
2406    end End_Use_Type;
2407
2408    ----------------------
2409    -- Find_Direct_Name --
2410    ----------------------
2411
2412    procedure Find_Direct_Name (N : Node_Id) is
2413       E    : Entity_Id;
2414       E2   : Entity_Id;
2415       Msg  : Boolean;
2416
2417       Inst : Entity_Id := Empty;
2418       --  Enclosing instance, if any
2419
2420       Homonyms : Entity_Id;
2421       --  Saves start of homonym chain
2422
2423       Nvis_Entity : Boolean;
2424       --  Set True to indicate that at there is at least one entity on the
2425       --  homonym chain which, while not visible, is visible enough from the
2426       --  user point of view to warrant an error message of "not visible"
2427       --  rather than undefined.
2428
2429       Nvis_Is_Private_Subprg : Boolean := False;
2430       --  Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
2431       --  effect concerning library subprograms has been detected. Used to
2432       --  generate the precise error message.
2433
2434       function From_Actual_Package (E : Entity_Id) return Boolean;
2435       --  Returns true if the entity is declared in a package that is
2436       --  an actual for a formal package of the current instance. Such an
2437       --  entity requires special handling because it may be use-visible
2438       --  but hides directly visible entities defined outside the instance.
2439
2440       function Known_But_Invisible (E : Entity_Id) return Boolean;
2441       --  This function determines whether the entity E (which is not
2442       --  visible) can reasonably be considered to be known to the writer
2443       --  of the reference. This is a heuristic test, used only for the
2444       --  purposes of figuring out whether we prefer to complain that an
2445       --  entity is undefined or invisible (and identify the declaration
2446       --  of the invisible entity in the latter case). The point here is
2447       --  that we don't want to complain that something is invisible and
2448       --  then point to something entirely mysterious to the writer.
2449
2450       procedure Nvis_Messages;
2451       --  Called if there are no visible entries for N, but there is at least
2452       --  one non-directly visible, or hidden declaration. This procedure
2453       --  outputs an appropriate set of error messages.
2454
2455       procedure Undefined (Nvis : Boolean);
2456       --  This function is called if the current node has no corresponding
2457       --  visible entity or entities. The value set in Msg indicates whether
2458       --  an error message was generated (multiple error messages for the
2459       --  same variable are generally suppressed, see body for details).
2460       --  Msg is True if an error message was generated, False if not. This
2461       --  value is used by the caller to determine whether or not to output
2462       --  additional messages where appropriate. The parameter is set False
2463       --  to get the message "X is undefined", and True to get the message
2464       --  "X is not visible".
2465
2466       -------------------------
2467       -- From_Actual_Package --
2468       -------------------------
2469
2470       function From_Actual_Package (E : Entity_Id) return Boolean is
2471          Scop : constant Entity_Id := Scope (E);
2472          Act  : Entity_Id;
2473
2474       begin
2475          if not In_Instance then
2476             return False;
2477          else
2478             Inst := Current_Scope;
2479
2480             while Present (Inst)
2481               and then Ekind (Inst) /= E_Package
2482               and then not Is_Generic_Instance (Inst)
2483             loop
2484                Inst := Scope (Inst);
2485             end loop;
2486
2487             if No (Inst) then
2488                return False;
2489             end if;
2490
2491             Act := First_Entity (Inst);
2492
2493             while Present (Act) loop
2494                if Ekind (Act) = E_Package then
2495
2496                   --  Check for end of actuals list
2497
2498                   if Renamed_Object (Act) = Inst then
2499                      return False;
2500
2501                   elsif Present (Associated_Formal_Package (Act))
2502                     and then Renamed_Object (Act) = Scop
2503                   then
2504                      --  Entity comes from (instance of) formal package
2505
2506                      return True;
2507
2508                   else
2509                      Next_Entity (Act);
2510                   end if;
2511
2512                else
2513                   Next_Entity (Act);
2514                end if;
2515             end loop;
2516
2517             return False;
2518          end if;
2519       end From_Actual_Package;
2520
2521       -------------------------
2522       -- Known_But_Invisible --
2523       -------------------------
2524
2525       function Known_But_Invisible (E : Entity_Id) return Boolean is
2526          Fname : File_Name_Type;
2527
2528       begin
2529          --  Entities in Standard are always considered to be known
2530
2531          if Sloc (E) <= Standard_Location then
2532             return True;
2533
2534          --  An entity that does not come from source is always considered
2535          --  to be unknown, since it is an artifact of code expansion.
2536
2537          elsif not Comes_From_Source (E) then
2538             return False;
2539
2540          --  In gnat internal mode, we consider all entities known
2541
2542          elsif GNAT_Mode then
2543             return True;
2544          end if;
2545
2546          --  Here we have an entity that is not from package Standard, and
2547          --  which comes from Source. See if it comes from an internal file.
2548
2549          Fname := Unit_File_Name (Get_Source_Unit (E));
2550
2551          --  Case of from internal file
2552
2553          if Is_Internal_File_Name (Fname) then
2554
2555             --  Private part entities in internal files are never considered
2556             --  to be known to the writer of normal application code.
2557
2558             if Is_Hidden (E) then
2559                return False;
2560             end if;
2561
2562             --  Entities from System packages other than System and
2563             --  System.Storage_Elements are not considered to be known.
2564             --  System.Auxxxx files are also considered known to the user.
2565
2566             --  Should refine this at some point to generally distinguish
2567             --  between known and unknown internal files ???
2568
2569             Get_Name_String (Fname);
2570
2571             return
2572               Name_Len < 2
2573                 or else
2574               Name_Buffer (1 .. 2) /= "s-"
2575                 or else
2576               Name_Buffer (3 .. 8) = "stoele"
2577                 or else
2578               Name_Buffer (3 .. 5) = "aux";
2579
2580          --  If not an internal file, then entity is definitely known,
2581          --  even if it is in a private part (the message generated will
2582          --  note that it is in a private part)
2583
2584          else
2585             return True;
2586          end if;
2587       end Known_But_Invisible;
2588
2589       -------------------
2590       -- Nvis_Messages --
2591       -------------------
2592
2593       procedure Nvis_Messages is
2594          Comp_Unit : Node_Id;
2595          Ent       : Entity_Id;
2596          Hidden    : Boolean := False;
2597          Item      : Node_Id;
2598
2599       begin
2600          --  Ada 2005 (AI-262): Generate a precise error concerning the
2601          --  Beaujolais effect that was previously detected
2602
2603          if Nvis_Is_Private_Subprg then
2604
2605             pragma Assert (Nkind (E2) = N_Defining_Identifier
2606                            and then Ekind (E2) = E_Function
2607                            and then Scope (E2) = Standard_Standard
2608                            and then Has_Private_With (E2));
2609
2610             --  Find the sloc corresponding to the private with'ed unit
2611
2612             Comp_Unit      := Cunit (Current_Sem_Unit);
2613             Item           := First (Context_Items (Comp_Unit));
2614             Error_Msg_Sloc := No_Location;
2615
2616             while Present (Item) loop
2617                if Nkind (Item) = N_With_Clause
2618                  and then Private_Present (Item)
2619                  and then Entity (Name (Item)) = E2
2620                then
2621                   Error_Msg_Sloc := Sloc (Item);
2622                   exit;
2623                end if;
2624
2625                Next (Item);
2626             end loop;
2627
2628             pragma Assert (Error_Msg_Sloc /= No_Location);
2629
2630             Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
2631             return;
2632          end if;
2633
2634          Undefined (Nvis => True);
2635
2636          if Msg then
2637
2638             --  First loop does hidden declarations
2639
2640             Ent := Homonyms;
2641             while Present (Ent) loop
2642                if Is_Potentially_Use_Visible (Ent) then
2643
2644                   if not Hidden then
2645                      Error_Msg_N ("multiple use clauses cause hiding!", N);
2646                      Hidden := True;
2647                   end if;
2648
2649                   Error_Msg_Sloc := Sloc (Ent);
2650                   Error_Msg_N ("hidden declaration#!", N);
2651                end if;
2652
2653                Ent := Homonym (Ent);
2654             end loop;
2655
2656             --  If we found hidden declarations, then that's enough, don't
2657             --  bother looking for non-visible declarations as well.
2658
2659             if Hidden then
2660                return;
2661             end if;
2662
2663             --  Second loop does non-directly visible declarations
2664
2665             Ent := Homonyms;
2666             while Present (Ent) loop
2667                if not Is_Potentially_Use_Visible (Ent) then
2668
2669                   --  Do not bother the user with unknown entities
2670
2671                   if not Known_But_Invisible (Ent) then
2672                      goto Continue;
2673                   end if;
2674
2675                   Error_Msg_Sloc := Sloc (Ent);
2676
2677                   --  Output message noting that there is a non-visible
2678                   --  declaration, distinguishing the private part case.
2679
2680                   if Is_Hidden (Ent) then
2681                      Error_Msg_N ("non-visible (private) declaration#!", N);
2682                   else
2683                      Error_Msg_N ("non-visible declaration#!", N);
2684
2685                      if Is_Compilation_Unit (Ent)
2686                        and then
2687                          Nkind (Parent (Parent (N))) = N_Use_Package_Clause
2688                      then
2689                         Error_Msg_NE
2690                          ("\possibly missing with_clause for&", N, Ent);
2691                      end if;
2692                   end if;
2693
2694                   --  Set entity and its containing package as referenced. We
2695                   --  can't be sure of this, but this seems a better choice
2696                   --  to avoid unused entity messages.
2697
2698                   if Comes_From_Source (Ent) then
2699                      Set_Referenced (Ent);
2700                      Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
2701                   end if;
2702                end if;
2703
2704                <<Continue>>
2705                Ent := Homonym (Ent);
2706             end loop;
2707
2708          end if;
2709       end Nvis_Messages;
2710
2711       ---------------
2712       -- Undefined --
2713       ---------------
2714
2715       procedure Undefined (Nvis : Boolean) is
2716          Emsg : Error_Msg_Id;
2717
2718       begin
2719          --  We should never find an undefined internal name. If we do, then
2720          --  see if we have previous errors. If so, ignore on the grounds that
2721          --  it is probably a cascaded message (e.g. a block label from a badly
2722          --  formed block). If no previous errors, then we have a real internal
2723          --  error of some kind so raise an exception.
2724
2725          if Is_Internal_Name (Chars (N)) then
2726             if Total_Errors_Detected /= 0 then
2727                return;
2728             else
2729                raise Program_Error;
2730             end if;
2731          end if;
2732
2733          --  A very specialized error check, if the undefined variable is
2734          --  a case tag, and the case type is an enumeration type, check
2735          --  for a possible misspelling, and if so, modify the identifier
2736
2737          --  Named aggregate should also be handled similarly ???
2738
2739          if Nkind (N) = N_Identifier
2740            and then Nkind (Parent (N)) = N_Case_Statement_Alternative
2741          then
2742             Get_Name_String (Chars (N));
2743
2744             declare
2745                Case_Str : constant String    := Name_Buffer (1 .. Name_Len);
2746                Case_Stm : constant Node_Id   := Parent (Parent (N));
2747                Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
2748
2749                Lit : Node_Id;
2750
2751             begin
2752                if Is_Enumeration_Type (Case_Typ)
2753                  and then Case_Typ /= Standard_Character
2754                  and then Case_Typ /= Standard_Wide_Character
2755                then
2756                   Lit := First_Literal (Case_Typ);
2757                   Get_Name_String (Chars (Lit));
2758
2759                   if Chars (Lit) /= Chars (N)
2760                     and then Is_Bad_Spelling_Of
2761                       (Case_Str, Name_Buffer (1 .. Name_Len))
2762                   then
2763                      Error_Msg_Node_2 := Lit;
2764                      Error_Msg_N
2765                        ("& is undefined, assume misspelling of &", N);
2766                      Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
2767                      return;
2768                   end if;
2769
2770                   Lit := Next_Literal (Lit);
2771                end if;
2772             end;
2773          end if;
2774
2775          --  Normal processing
2776
2777          Set_Entity (N, Any_Id);
2778          Set_Etype  (N, Any_Type);
2779
2780          --  We use the table Urefs to keep track of entities for which we
2781          --  have issued errors for undefined references. Multiple errors
2782          --  for a single name are normally suppressed, however we modify
2783          --  the error message to alert the programmer to this effect.
2784
2785          for J in Urefs.First .. Urefs.Last loop
2786             if Chars (N) = Chars (Urefs.Table (J).Node) then
2787                if Urefs.Table (J).Err /= No_Error_Msg
2788                  and then Sloc (N) /= Urefs.Table (J).Loc
2789                then
2790                   Error_Msg_Node_1 := Urefs.Table (J).Node;
2791
2792                   if Urefs.Table (J).Nvis then
2793                      Change_Error_Text (Urefs.Table (J).Err,
2794                        "& is not visible (more references follow)");
2795                   else
2796                      Change_Error_Text (Urefs.Table (J).Err,
2797                        "& is undefined (more references follow)");
2798                   end if;
2799
2800                   Urefs.Table (J).Err := No_Error_Msg;
2801                end if;
2802
2803                --  Although we will set Msg False, and thus suppress the
2804                --  message, we also set Error_Posted True, to avoid any
2805                --  cascaded messages resulting from the undefined reference.
2806
2807                Msg := False;
2808                Set_Error_Posted (N, True);
2809                return;
2810             end if;
2811          end loop;
2812
2813          --  If entry not found, this is first undefined occurrence
2814
2815          if Nvis then
2816             Error_Msg_N ("& is not visible!", N);
2817             Emsg := Get_Msg_Id;
2818
2819          else
2820             Error_Msg_N ("& is undefined!", N);
2821             Emsg := Get_Msg_Id;
2822
2823             --  A very bizarre special check, if the undefined identifier
2824             --  is put or put_line, then add a special error message (since
2825             --  this is a very common error for beginners to make).
2826
2827             if Chars (N) = Name_Put or else Chars (N) = Name_Put_Line then
2828                Error_Msg_N ("\possible missing with of 'Text_'I'O!", N);
2829             end if;
2830
2831             --  Now check for possible misspellings
2832
2833             Get_Name_String (Chars (N));
2834
2835             declare
2836                E      : Entity_Id;
2837                Ematch : Entity_Id := Empty;
2838
2839                Last_Name_Id : constant Name_Id :=
2840                                 Name_Id (Nat (First_Name_Id) +
2841                                            Name_Entries_Count - 1);
2842
2843                S  : constant String (1 .. Name_Len) :=
2844                       Name_Buffer (1 .. Name_Len);
2845
2846             begin
2847                for N in First_Name_Id .. Last_Name_Id loop
2848                   E := Get_Name_Entity_Id (N);
2849
2850                   if Present (E)
2851                      and then (Is_Immediately_Visible (E)
2852                                  or else
2853                                Is_Potentially_Use_Visible (E))
2854                   then
2855                      Get_Name_String (N);
2856
2857                      if Is_Bad_Spelling_Of
2858                           (Name_Buffer (1 .. Name_Len), S)
2859                      then
2860                         Ematch := E;
2861                         exit;
2862                      end if;
2863                   end if;
2864                end loop;
2865
2866                if Present (Ematch) then
2867                   Error_Msg_NE ("\possible misspelling of&", N, Ematch);
2868                end if;
2869             end;
2870          end if;
2871
2872          --  Make entry in undefined references table unless the full
2873          --  errors switch is set, in which case by refraining from
2874          --  generating the table entry, we guarantee that we get an
2875          --  error message for every undefined reference.
2876
2877          if not All_Errors_Mode then
2878             Urefs.Increment_Last;
2879             Urefs.Table (Urefs.Last).Node := N;
2880             Urefs.Table (Urefs.Last).Err  := Emsg;
2881             Urefs.Table (Urefs.Last).Nvis := Nvis;
2882             Urefs.Table (Urefs.Last).Loc  := Sloc (N);
2883          end if;
2884
2885          Msg := True;
2886       end Undefined;
2887
2888    --  Start of processing for Find_Direct_Name
2889
2890    begin
2891       --  If the entity pointer is already set, this is an internal node, or
2892       --  a node that is analyzed more than once, after a tree modification.
2893       --  In such a case there is no resolution to perform, just set the type.
2894
2895       if Present (Entity (N)) then
2896          if Is_Type (Entity (N)) then
2897             Set_Etype (N, Entity (N));
2898
2899          else
2900             declare
2901                Entyp : constant Entity_Id := Etype (Entity (N));
2902
2903             begin
2904                --  One special case here. If the Etype field is already set,
2905                --  and references the packed array type corresponding to the
2906                --  etype of the referenced entity, then leave it alone. This
2907                --  happens for trees generated from Exp_Pakd, where expressions
2908                --  can be deliberately "mis-typed" to the packed array type.
2909
2910                if Is_Array_Type (Entyp)
2911                  and then Is_Packed (Entyp)
2912                  and then Present (Etype (N))
2913                  and then Etype (N) = Packed_Array_Type (Entyp)
2914                then
2915                   null;
2916
2917                --  If not that special case, then just reset the Etype
2918
2919                else
2920                   Set_Etype (N, Etype (Entity (N)));
2921                end if;
2922             end;
2923          end if;
2924
2925          return;
2926       end if;
2927
2928       --  Here if Entity pointer was not set, we need full visibility analysis
2929       --  First we generate debugging output if the debug E flag is set.
2930
2931       if Debug_Flag_E then
2932          Write_Str ("Looking for ");
2933          Write_Name (Chars (N));
2934          Write_Eol;
2935       end if;
2936
2937       Homonyms := Current_Entity (N);
2938       Nvis_Entity := False;
2939
2940       E := Homonyms;
2941       while Present (E) loop
2942
2943          --  If entity is immediately visible or potentially use
2944          --  visible, then process the entity and we are done.
2945
2946          if Is_Immediately_Visible (E) then
2947             goto Immediately_Visible_Entity;
2948
2949          elsif Is_Potentially_Use_Visible (E) then
2950             goto Potentially_Use_Visible_Entity;
2951
2952          --  Note if a known but invisible entity encountered
2953
2954          elsif Known_But_Invisible (E) then
2955             Nvis_Entity := True;
2956          end if;
2957
2958          --  Move to next entity in chain and continue search
2959
2960          E := Homonym (E);
2961       end loop;
2962
2963       --  If no entries on homonym chain that were potentially visible,
2964       --  and no entities reasonably considered as non-visible, then
2965       --  we have a plain undefined reference, with no additional
2966       --  explanation required!
2967
2968       if not Nvis_Entity then
2969          Undefined (Nvis => False);
2970
2971       --  Otherwise there is at least one entry on the homonym chain that
2972       --  is reasonably considered as being known and non-visible.
2973
2974       else
2975          Nvis_Messages;
2976       end if;
2977
2978       return;
2979
2980       --  Processing for a potentially use visible entry found. We must search
2981       --  the rest of the homonym chain for two reasons. First, if there is a
2982       --  directly visible entry, then none of the potentially use-visible
2983       --  entities are directly visible (RM 8.4(10)). Second, we need to check
2984       --  for the case of multiple potentially use-visible entries hiding one
2985       --  another and as a result being non-directly visible (RM 8.4(11)).
2986
2987       <<Potentially_Use_Visible_Entity>> declare
2988          Only_One_Visible : Boolean := True;
2989          All_Overloadable : Boolean := Is_Overloadable (E);
2990
2991       begin
2992          E2 := Homonym (E);
2993
2994          while Present (E2) loop
2995             if Is_Immediately_Visible (E2) then
2996
2997                --  If the use-visible entity comes from the actual for a
2998                --  formal package, it hides a directly visible entity from
2999                --  outside the instance.
3000
3001                if From_Actual_Package (E)
3002                  and then Scope_Depth (E2) < Scope_Depth (Inst)
3003                then
3004                   goto Found;
3005                else
3006                   E := E2;
3007                   goto Immediately_Visible_Entity;
3008                end if;
3009
3010             elsif Is_Potentially_Use_Visible (E2) then
3011                Only_One_Visible := False;
3012                All_Overloadable := All_Overloadable and Is_Overloadable (E2);
3013
3014             --  Ada 2005 (AI-262): Protect against a form of Beujolais effect
3015             --  that can occurr in private_with clauses. Example:
3016
3017             --    with A;
3018             --    private with B;              package A is
3019             --    package C is                   function B return Integer;
3020             --      use A;                     end A;
3021             --      V1 : Integer := B;
3022             --    private                      function B return Integer;
3023             --      V2 : Integer := B;
3024             --    end C;
3025
3026             --  V1 resolves to A.B, but V2 resolves to library unit B
3027
3028             elsif Ekind (E2) = E_Function
3029               and then Scope (E2) = Standard_Standard
3030               and then Has_Private_With (E2)
3031             then
3032                Only_One_Visible       := False;
3033                All_Overloadable       := False;
3034                Nvis_Is_Private_Subprg := True;
3035                exit;
3036             end if;
3037
3038             E2 := Homonym (E2);
3039          end loop;
3040
3041          --  On falling through this loop, we have checked that there are no
3042          --  immediately visible entities. Only_One_Visible is set if exactly
3043          --  one potentially use visible entity exists. All_Overloadable is
3044          --  set if all the potentially use visible entities are overloadable.
3045          --  The condition for legality is that either there is one potentially
3046          --  use visible entity, or if there is more than one, then all of them
3047          --  are overloadable.
3048
3049          if Only_One_Visible or All_Overloadable then
3050             goto Found;
3051
3052          --  If there is more than one potentially use-visible entity and at
3053          --  least one of them non-overloadable, we have an error (RM 8.4(11).
3054          --  Note that E points to the first such entity on the homonym list.
3055          --  Special case: if one of the entities is declared in an actual
3056          --  package, it was visible in the generic, and takes precedence over
3057          --  other entities that are potentially use-visible. Same if it is
3058          --  declared in a local instantiation of the current instance.
3059
3060          else
3061             if In_Instance then
3062                Inst := Current_Scope;
3063
3064                --  Find current instance
3065
3066                while Present (Inst)
3067                  and then Inst /= Standard_Standard
3068                loop
3069                   if Is_Generic_Instance (Inst) then
3070                      exit;
3071                   end if;
3072
3073                   Inst := Scope (Inst);
3074                end loop;
3075
3076                E2 := E;
3077
3078                while Present (E2) loop
3079                   if From_Actual_Package (E2)
3080                     or else
3081                       (Is_Generic_Instance (Scope (E2))
3082                         and then Scope_Depth (Scope (E2)) > Scope_Depth (Inst))
3083                   then
3084                      E := E2;
3085                      goto Found;
3086                   end if;
3087
3088                   E2 := Homonym (E2);
3089                end loop;
3090
3091                Nvis_Messages;
3092                return;
3093
3094             else
3095                Nvis_Messages;
3096                return;
3097             end if;
3098          end if;
3099       end;
3100
3101       --  Come here with E set to the first immediately visible entity on
3102       --  the homonym chain. This is the one we want unless there is another
3103       --  immediately visible entity further on in the chain for a more
3104       --  inner scope (RM 8.3(8)).
3105
3106       <<Immediately_Visible_Entity>> declare
3107          Level : Int;
3108          Scop  : Entity_Id;
3109
3110       begin
3111          --  Find scope level of initial entity. When compiling  through
3112          --  Rtsfind, the previous context is not completely invisible, and
3113          --  an outer entity may appear on the chain, whose scope is below
3114          --  the entry for Standard that delimits the current scope stack.
3115          --  Indicate that the level for this spurious entry is outside of
3116          --  the current scope stack.
3117
3118          Level := Scope_Stack.Last;
3119          loop
3120             Scop := Scope_Stack.Table (Level).Entity;
3121             exit when Scop = Scope (E);
3122             Level := Level - 1;
3123             exit when Scop = Standard_Standard;
3124          end loop;
3125
3126          --  Now search remainder of homonym chain for more inner entry
3127          --  If the entity is Standard itself, it has no scope, and we
3128          --  compare it with the stack entry directly.
3129
3130          E2 := Homonym (E);
3131          while Present (E2) loop
3132             if Is_Immediately_Visible (E2) then
3133                for J in Level + 1 .. Scope_Stack.Last loop
3134                   if Scope_Stack.Table (J).Entity = Scope (E2)
3135                     or else Scope_Stack.Table (J).Entity = E2
3136                   then
3137                      Level := J;
3138                      E := E2;
3139                      exit;
3140                   end if;
3141                end loop;
3142             end if;
3143
3144             E2 := Homonym (E2);
3145          end loop;
3146
3147          --  At the end of that loop, E is the innermost immediately
3148          --  visible entity, so we are all set.
3149       end;
3150
3151       --  Come here with entity found, and stored in E
3152
3153       <<Found>> begin
3154
3155          if Comes_From_Source (N)
3156            and then Is_Remote_Access_To_Subprogram_Type (E)
3157            and then Expander_Active
3158          then
3159             Rewrite (N,
3160               New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
3161             return;
3162          end if;
3163
3164          Set_Entity (N, E);
3165          --  Why no Style_Check here???
3166
3167          if Is_Type (E) then
3168             Set_Etype (N, E);
3169          else
3170             Set_Etype (N, Get_Full_View (Etype (E)));
3171          end if;
3172
3173          if Debug_Flag_E then
3174             Write_Str (" found  ");
3175             Write_Entity_Info (E, "      ");
3176          end if;
3177
3178          --  If the Ekind of the entity is Void, it means that all homonyms
3179          --  are hidden from all visibility (RM 8.3(5,14-20)). However, this
3180          --  test is skipped if the current scope is a record and the name is
3181          --  a pragma argument expression (case of Atomic and Volatile pragmas
3182          --  and possibly other similar pragmas added later, which are allowed
3183          --  to reference components in the current record).
3184
3185          if Ekind (E) = E_Void
3186            and then
3187              (not Is_Record_Type (Current_Scope)
3188                or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
3189          then
3190             Premature_Usage (N);
3191
3192          --  If the entity is overloadable, collect all interpretations
3193          --  of the name for subsequent overload resolution. We optimize
3194          --  a bit here to do this only if we have an overloadable entity
3195          --  that is not on its own on the homonym chain.
3196
3197          elsif Is_Overloadable (E)
3198            and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
3199          then
3200             Collect_Interps (N);
3201
3202             --  If no homonyms were visible, the entity is unambiguous
3203
3204             if not Is_Overloaded (N) then
3205                Generate_Reference (E, N);
3206             end if;
3207
3208          --  Case of non-overloadable entity, set the entity providing that
3209          --  we do not have the case of a discriminant reference within a
3210          --  default expression. Such references are replaced with the
3211          --  corresponding discriminal, which is the formal corresponding to
3212          --  to the discriminant in the initialization procedure.
3213
3214          else
3215             --  Entity is unambiguous, indicate that it is referenced here
3216             --  One slightly odd case is that we do not want to set the
3217             --  Referenced flag if the entity is a label, and the identifier
3218             --  is the label in the source, since this is not a reference
3219             --  from the point of view of the user
3220
3221             if Nkind (Parent (N)) = N_Label then
3222                declare
3223                   R : constant Boolean := Referenced (E);
3224
3225                begin
3226                   Generate_Reference (E, N);
3227                   Set_Referenced (E, R);
3228                end;
3229
3230             --  Normal case, not a label. Generate reference
3231
3232             else
3233                Generate_Reference (E, N);
3234             end if;
3235
3236             --  Set Entity, with style check if need be. If this is a
3237             --  discriminant reference, it must be replaced by the
3238             --  corresponding discriminal, that is to say the parameter
3239             --  of the initialization procedure that corresponds to the
3240             --  discriminant. If this replacement is being performed, there
3241             --  is no style check to perform.
3242
3243             --  This replacement must not be done if we are currently
3244             --  processing a generic spec or body, because the discriminal
3245             --  has not been not generated in this case.
3246
3247             if not In_Default_Expression
3248               or else Ekind (E) /= E_Discriminant
3249               or else Inside_A_Generic
3250             then
3251                Set_Entity_With_Style_Check (N, E);
3252
3253             --  The replacement is not done either for a task discriminant that
3254             --  appears in a default expression of an entry parameter. See
3255             --  Expand_Discriminant in exp_ch2 for details on their handling.
3256
3257             elsif Is_Concurrent_Type (Scope (E)) then
3258                declare
3259                   P : Node_Id := Parent (N);
3260
3261                begin
3262                   while Present (P)
3263                     and then Nkind (P) /= N_Parameter_Specification
3264                     and then Nkind (P) /= N_Component_Declaration
3265                   loop
3266                      P := Parent (P);
3267                   end loop;
3268
3269                   if Present (P)
3270                      and then Nkind (P) = N_Parameter_Specification
3271                   then
3272                      null;
3273                   else
3274                      Set_Entity (N, Discriminal (E));
3275                   end if;
3276                end;
3277
3278             --  Otherwise, this is a discriminant in a context in which
3279             --  it is a reference to the corresponding parameter of the
3280             --  init proc for the enclosing type.
3281
3282             else
3283                Set_Entity (N, Discriminal (E));
3284             end if;
3285          end if;
3286       end;
3287    end Find_Direct_Name;
3288
3289    ------------------------
3290    -- Find_Expanded_Name --
3291    ------------------------
3292
3293    --  This routine searches the homonym chain of the entity until it finds
3294    --  an entity declared in the scope denoted by the prefix. If the entity
3295    --  is private, it may nevertheless be immediately visible, if we are in
3296    --  the scope of its declaration.
3297
3298    procedure Find_Expanded_Name (N : Node_Id) is
3299       Selector  : constant Node_Id := Selector_Name (N);
3300       Candidate : Entity_Id        := Empty;
3301       P_Name    : Entity_Id;
3302       O_Name    : Entity_Id;
3303       Id        : Entity_Id;
3304
3305    begin
3306       P_Name := Entity (Prefix (N));
3307       O_Name := P_Name;
3308
3309       --  If the prefix is a renamed package, look for the entity
3310       --  in the original package.
3311
3312       if Ekind (P_Name) = E_Package
3313         and then Present (Renamed_Object (P_Name))
3314       then
3315          P_Name := Renamed_Object (P_Name);
3316
3317          --  Rewrite node with entity field pointing to renamed object
3318
3319          Rewrite (Prefix (N), New_Copy (Prefix (N)));
3320          Set_Entity (Prefix (N), P_Name);
3321
3322       --  If the prefix is an object of a concurrent type, look for
3323       --  the entity in the associated task or protected type.
3324
3325       elsif Is_Concurrent_Type (Etype (P_Name)) then
3326          P_Name := Etype (P_Name);
3327       end if;
3328
3329       Id := Current_Entity (Selector);
3330
3331       while Present (Id) loop
3332
3333          if Scope (Id) = P_Name then
3334             Candidate := Id;
3335
3336             if Is_Child_Unit (Id) then
3337                exit when Is_Visible_Child_Unit (Id)
3338                  or else Is_Immediately_Visible (Id);
3339
3340             else
3341                exit when not Is_Hidden (Id)
3342                  or else Is_Immediately_Visible (Id);
3343             end if;
3344          end if;
3345
3346          Id := Homonym (Id);
3347       end loop;
3348
3349       if No (Id)
3350         and then (Ekind (P_Name) = E_Procedure
3351                     or else
3352                   Ekind (P_Name) = E_Function)
3353         and then Is_Generic_Instance (P_Name)
3354       then
3355          --  Expanded name denotes entity in (instance of) generic subprogram.
3356          --  The entity may be in the subprogram instance, or may denote one of
3357          --  the formals, which is declared in the enclosing wrapper package.
3358
3359          P_Name := Scope (P_Name);
3360
3361          Id := Current_Entity (Selector);
3362          while Present (Id) loop
3363             exit when Scope (Id) = P_Name;
3364             Id := Homonym (Id);
3365          end loop;
3366       end if;
3367
3368       if No (Id) or else Chars (Id) /= Chars (Selector) then
3369          Set_Etype (N, Any_Type);
3370
3371          --  If we are looking for an entity defined in System, try to
3372          --  find it in the child package that may have been provided as
3373          --  an extension to System. The Extend_System pragma will have
3374          --  supplied the name of the extension, which may have to be loaded.
3375
3376          if Chars (P_Name) = Name_System
3377            and then Scope (P_Name) = Standard_Standard
3378            and then Present (System_Extend_Unit)
3379            and then Present_System_Aux (N)
3380          then
3381             Set_Entity (Prefix (N), System_Aux_Id);
3382             Find_Expanded_Name (N);
3383             return;
3384
3385          elsif Nkind (Selector) = N_Operator_Symbol
3386            and then Has_Implicit_Operator (N)
3387          then
3388             --  There is an implicit instance of the predefined operator in
3389             --  the given scope. The operator entity is defined in Standard.
3390             --  Has_Implicit_Operator makes the node into an Expanded_Name.
3391
3392             return;
3393
3394          elsif Nkind (Selector) = N_Character_Literal
3395            and then Has_Implicit_Character_Literal (N)
3396          then
3397             --  If there is no literal defined in the scope denoted by the
3398             --  prefix, the literal may belong to (a type derived from)
3399             --  Standard_Character, for which we have no explicit literals.
3400
3401             return;
3402
3403          else
3404             --  If the prefix is a single concurrent object, use its
3405             --  name in  the error message, rather than that of the
3406             --  anonymous type.
3407
3408             if Is_Concurrent_Type (P_Name)
3409               and then Is_Internal_Name (Chars (P_Name))
3410             then
3411                Error_Msg_Node_2 := Entity (Prefix (N));
3412             else
3413                Error_Msg_Node_2 := P_Name;
3414             end if;
3415
3416             if P_Name = System_Aux_Id then
3417                P_Name := Scope (P_Name);
3418                Set_Entity (Prefix (N), P_Name);
3419             end if;
3420
3421             if Present (Candidate) then
3422
3423                if Is_Child_Unit (Candidate) then
3424                   Error_Msg_N
3425                     ("missing with_clause for child unit &", Selector);
3426                else
3427                   Error_Msg_NE ("& is not a visible entity of&", N, Selector);
3428                end if;
3429
3430             else
3431                --  Within the instantiation of a child unit, the prefix may
3432                --  denote the parent instance, but the selector has the
3433                --  name of the original child. Find whether we are within
3434                --  the corresponding instance, and get the proper entity, which
3435                --  can only be an enclosing scope.
3436
3437                if O_Name /= P_Name
3438                  and then In_Open_Scopes (P_Name)
3439                  and then Is_Generic_Instance (P_Name)
3440                then
3441                   declare
3442                      S : Entity_Id := Current_Scope;
3443                      P : Entity_Id;
3444
3445                   begin
3446                      for J in reverse 0 .. Scope_Stack.Last loop
3447                         S := Scope_Stack.Table (J).Entity;
3448
3449                         exit when S = Standard_Standard;
3450
3451                         if Ekind (S) = E_Function
3452                           or else Ekind (S) = E_Package
3453                           or else Ekind (S) = E_Procedure
3454                         then
3455                            P := Generic_Parent (Specification
3456                                   (Unit_Declaration_Node (S)));
3457
3458                            if Present (P)
3459                              and then Chars (Scope (P)) = Chars (O_Name)
3460                              and then Chars (P) = Chars (Selector)
3461                            then
3462                               Id := S;
3463                               goto found;
3464                            end if;
3465                         end if;
3466
3467                      end loop;
3468                   end;
3469                end if;
3470
3471                if Chars (P_Name) = Name_Ada
3472                  and then Scope (P_Name) = Standard_Standard
3473                then
3474                   Error_Msg_Node_2 := Selector;
3475                   Error_Msg_NE ("missing with for `&.&`", N, P_Name);
3476
3477                --  If this is a selection from a dummy package, then
3478                --  suppress the error message, of course the entity
3479                --  is missing if the package is missing!
3480
3481                elsif Sloc (Error_Msg_Node_2) = No_Location then
3482                   null;
3483
3484                --  Here we have the case of an undefined component
3485
3486                else
3487
3488                   Error_Msg_NE ("& not declared in&", N, Selector);
3489
3490                   --  Check for misspelling of some entity in prefix
3491
3492                   Id := First_Entity (P_Name);
3493                   Get_Name_String (Chars (Selector));
3494
3495                   declare
3496                      S  : constant String (1 .. Name_Len) :=
3497                             Name_Buffer (1 .. Name_Len);
3498                   begin
3499                      while Present (Id) loop
3500                         Get_Name_String (Chars (Id));
3501                         if Is_Bad_Spelling_Of
3502                           (Name_Buffer (1 .. Name_Len), S)
3503                           and then not Is_Internal_Name (Chars (Id))
3504                         then
3505                            Error_Msg_NE
3506                              ("possible misspelling of&", Selector, Id);
3507                            exit;
3508                         end if;
3509
3510                         Next_Entity (Id);
3511                      end loop;
3512                   end;
3513
3514                   --  Specialize the message if this may be an instantiation
3515                   --  of a child unit that was not mentioned in the context.
3516
3517                   if Nkind (Parent (N)) = N_Package_Instantiation
3518                     and then Is_Generic_Instance (Entity (Prefix (N)))
3519                     and then Is_Compilation_Unit
3520                      (Generic_Parent (Parent (Entity (Prefix (N)))))
3521                   then
3522                      Error_Msg_NE
3523                       ("\possible missing with clause on child unit&",
3524                         N, Selector);
3525                   end if;
3526                end if;
3527             end if;
3528
3529             Id := Any_Id;
3530          end if;
3531       end if;
3532
3533       <<found>>
3534       if Comes_From_Source (N)
3535         and then Is_Remote_Access_To_Subprogram_Type (Id)
3536       then
3537          Id := Equivalent_Type (Id);
3538          Set_Chars (Selector, Chars (Id));
3539       end if;
3540
3541       --  Ada 2005 (AI-50217): Check usage of entities in limited withed units
3542
3543       if Ekind (P_Name) = E_Package
3544         and then From_With_Type (P_Name)
3545       then
3546          if From_With_Type (Id)
3547            or else Is_Type (Id)
3548            or else Ekind (Id) = E_Package
3549          then
3550             null;
3551          else
3552             Error_Msg_N
3553               ("limited withed package can only be used to access "
3554                & " incomplete types",
3555                 N);
3556          end if;
3557       end if;
3558
3559       if Is_Task_Type (P_Name)
3560         and then ((Ekind (Id) = E_Entry
3561                     and then Nkind (Parent (N)) /= N_Attribute_Reference)
3562                     or else
3563                   (Ekind (Id) = E_Entry_Family
3564                     and then
3565                       Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
3566       then
3567          --  It is an entry call after all, either to the current task
3568          --  (which will deadlock) or to an enclosing task.
3569
3570          Analyze_Selected_Component (N);
3571          return;
3572       end if;
3573
3574       Change_Selected_Component_To_Expanded_Name (N);
3575
3576       --  Do style check and generate reference, but skip both steps if this
3577       --  entity has homonyms, since we may not have the right homonym set
3578       --  yet. The proper homonym will be set during the resolve phase.
3579
3580       if Has_Homonym (Id) then
3581          Set_Entity (N, Id);
3582       else
3583          Set_Entity_With_Style_Check (N, Id);
3584          Generate_Reference (Id, N);
3585       end if;
3586
3587       if Is_Type (Id) then
3588          Set_Etype (N, Id);
3589       else
3590          Set_Etype (N, Get_Full_View (Etype (Id)));
3591       end if;
3592
3593       --  If the Ekind of the entity is Void, it means that all homonyms
3594       --  are hidden from all visibility (RM 8.3(5,14-20)).
3595
3596       if Ekind (Id) = E_Void then
3597          Premature_Usage (N);
3598
3599       elsif Is_Overloadable (Id)
3600         and then Present (Homonym (Id))
3601       then
3602          declare
3603             H : Entity_Id := Homonym (Id);
3604
3605          begin
3606             while Present (H) loop
3607                if Scope (H) = Scope (Id)
3608                  and then
3609                    (not Is_Hidden (H)
3610                       or else Is_Immediately_Visible (H))
3611                then
3612                   Collect_Interps (N);
3613                   exit;
3614                end if;
3615
3616                H := Homonym (H);
3617             end loop;
3618
3619             --  If an extension of System is present, collect possible
3620             --  explicit overloadings declared in the extension.
3621
3622             if Chars (P_Name) = Name_System
3623               and then Scope (P_Name) = Standard_Standard
3624               and then Present (System_Extend_Unit)
3625               and then Present_System_Aux (N)
3626             then
3627                H := Current_Entity (Id);
3628
3629                while Present (H) loop
3630                   if Scope (H) = System_Aux_Id then
3631                      Add_One_Interp (N, H, Etype (H));
3632                   end if;
3633
3634                   H := Homonym (H);
3635                end loop;
3636             end if;
3637          end;
3638       end if;
3639
3640       if Nkind (Selector_Name (N)) = N_Operator_Symbol
3641         and then Scope (Id) /= Standard_Standard
3642       then
3643          --  In addition to user-defined operators in the given scope,
3644          --  there may be an implicit instance of the predefined
3645          --  operator. The operator (defined in Standard) is found
3646          --  in Has_Implicit_Operator, and added to the interpretations.
3647          --  Procedure Add_One_Interp will determine which hides which.
3648
3649          if Has_Implicit_Operator (N) then
3650             null;
3651          end if;
3652       end if;
3653    end Find_Expanded_Name;
3654
3655    -------------------------
3656    -- Find_Renamed_Entity --
3657    -------------------------
3658
3659    function Find_Renamed_Entity
3660      (N         : Node_Id;
3661       Nam       : Node_Id;
3662       New_S     : Entity_Id;
3663       Is_Actual : Boolean := False) return Entity_Id
3664    is
3665       Ind   : Interp_Index;
3666       I1    : Interp_Index := 0; -- Suppress junk warnings
3667       It    : Interp;
3668       It1   : Interp;
3669       Old_S : Entity_Id;
3670       Inst  : Entity_Id;
3671
3672       function Enclosing_Instance return Entity_Id;
3673       --  If the renaming determines the entity for the default of a formal
3674       --  subprogram nested within another instance, choose the innermost
3675       --  candidate. This is because if the formal has a box, and we are within
3676       --  an enclosing instance where some candidate interpretations are local
3677       --  to this enclosing instance, we know that the default was properly
3678       --  resolved when analyzing the generic, so we prefer the local
3679       --  candidates to those that are external. This is not always the case
3680       --  but is a reasonable heuristic on the use of nested generics.
3681       --  The proper solution requires a full renaming model.
3682
3683       function Within (Inner, Outer : Entity_Id) return Boolean;
3684       --  Determine whether a candidate subprogram is defined within
3685       --  the enclosing instance. If yes, it has precedence over outer
3686       --  candidates.
3687
3688       function Is_Visible_Operation (Op : Entity_Id) return Boolean;
3689       --  If the renamed entity is an implicit operator, check whether it is
3690       --  visible because its operand type is properly visible. This
3691       --  check applies to explicit renamed entities that appear in the
3692       --  source in a renaming declaration or a formal subprogram instance,
3693       --  but not to default generic actuals with a name.
3694
3695       ------------------------
3696       -- Enclosing_Instance --
3697       ------------------------
3698
3699       function Enclosing_Instance return Entity_Id is
3700          S : Entity_Id;
3701
3702       begin
3703          if not Is_Generic_Instance (Current_Scope)
3704            and then not Is_Actual
3705          then
3706             return Empty;
3707          end if;
3708
3709          S := Scope (Current_Scope);
3710
3711          while S /= Standard_Standard loop
3712
3713             if Is_Generic_Instance (S) then
3714                return S;
3715             end if;
3716
3717             S := Scope (S);
3718          end loop;
3719
3720          return Empty;
3721       end Enclosing_Instance;
3722
3723       --------------------------
3724       -- Is_Visible_Operation --
3725       --------------------------
3726
3727       function Is_Visible_Operation (Op : Entity_Id) return Boolean is
3728          Scop : Entity_Id;
3729          Typ  : Entity_Id;
3730          Btyp : Entity_Id;
3731
3732       begin
3733          if Ekind (Op) /= E_Operator
3734            or else Scope (Op) /= Standard_Standard
3735            or else (In_Instance
3736                       and then
3737                         (not Is_Actual
3738                            or else Present (Enclosing_Instance)))
3739          then
3740             return True;
3741
3742          else
3743             --  For a fixed point type operator, check the resulting type,
3744             --  because it may be a mixed mode integer * fixed operation.
3745
3746             if Present (Next_Formal (First_Formal (New_S)))
3747               and then Is_Fixed_Point_Type (Etype (New_S))
3748             then
3749                Typ := Etype (New_S);
3750             else
3751                Typ := Etype (First_Formal (New_S));
3752             end if;
3753
3754             Btyp := Base_Type (Typ);
3755
3756             if Nkind (Nam) /= N_Expanded_Name then
3757                return (In_Open_Scopes (Scope (Btyp))
3758                         or else Is_Potentially_Use_Visible (Btyp)
3759                         or else In_Use (Btyp)
3760                         or else In_Use (Scope (Btyp)));
3761
3762             else
3763                Scop := Entity (Prefix (Nam));
3764
3765                if Ekind (Scop) = E_Package
3766                  and then Present (Renamed_Object (Scop))
3767                then
3768                   Scop := Renamed_Object (Scop);
3769                end if;
3770
3771                --  Operator is visible if prefix of expanded name denotes
3772                --  scope of type, or else type type is defined in System_Aux
3773                --  and the prefix denotes System.
3774
3775                return Scope (Btyp) = Scop
3776                  or else (Scope (Btyp) = System_Aux_Id
3777                            and then Scope (Scope (Btyp)) = Scop);
3778             end if;
3779          end if;
3780       end Is_Visible_Operation;
3781
3782       ------------
3783       -- Within --
3784       ------------
3785
3786       function Within (Inner, Outer : Entity_Id) return Boolean is
3787          Sc : Entity_Id := Scope (Inner);
3788
3789       begin
3790          while Sc /= Standard_Standard loop
3791
3792             if Sc = Outer then
3793                return True;
3794             else
3795                Sc := Scope (Sc);
3796             end if;
3797          end loop;
3798
3799          return False;
3800       end Within;
3801
3802       function Report_Overload return Entity_Id;
3803       --  List possible interpretations, and specialize message in the
3804       --  case of a generic actual.
3805
3806       function Report_Overload return Entity_Id is
3807       begin
3808          if Is_Actual then
3809             Error_Msg_NE
3810               ("ambiguous actual subprogram&, " &
3811                  "possible interpretations: ", N, Nam);
3812          else
3813             Error_Msg_N
3814               ("ambiguous subprogram, " &
3815                  "possible interpretations: ", N);
3816          end if;
3817
3818          List_Interps (Nam, N);
3819          return Old_S;
3820       end Report_Overload;
3821
3822    --  Start of processing for Find_Renamed_Entry
3823
3824    begin
3825       Old_S := Any_Id;
3826       Candidate_Renaming := Empty;
3827
3828       if not Is_Overloaded (Nam) then
3829          if Entity_Matches_Spec (Entity (Nam), New_S)
3830            and then Is_Visible_Operation (Entity (Nam))
3831          then
3832             Old_S := Entity (Nam);
3833
3834          elsif
3835            Present (First_Formal (Entity (Nam)))
3836              and then Present (First_Formal (New_S))
3837              and then (Base_Type (Etype (First_Formal (Entity (Nam))))
3838                         = Base_Type (Etype (First_Formal (New_S))))
3839          then
3840             Candidate_Renaming := Entity (Nam);
3841          end if;
3842
3843       else
3844          Get_First_Interp (Nam, Ind, It);
3845
3846          while Present (It.Nam) loop
3847
3848             if Entity_Matches_Spec (It.Nam, New_S)
3849                and then Is_Visible_Operation (It.Nam)
3850             then
3851                if Old_S /= Any_Id then
3852
3853                   --  Note: The call to Disambiguate only happens if a
3854                   --  previous interpretation was found, in which case I1
3855                   --  has received a value.
3856
3857                   It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
3858
3859                   if It1 = No_Interp then
3860
3861                      Inst := Enclosing_Instance;
3862
3863                      if Present (Inst) then
3864
3865                         if Within (It.Nam, Inst) then
3866                            return (It.Nam);
3867
3868                         elsif Within (Old_S, Inst) then
3869                            return (Old_S);
3870
3871                         else
3872                            return Report_Overload;
3873                         end if;
3874
3875                      else
3876                         return Report_Overload;
3877                      end if;
3878
3879                   else
3880                      Old_S := It1.Nam;
3881                      exit;
3882                   end if;
3883
3884                else
3885                   I1 := Ind;
3886                   Old_S := It.Nam;
3887                end if;
3888
3889             elsif
3890               Present (First_Formal (It.Nam))
3891                 and then Present (First_Formal (New_S))
3892                 and then  (Base_Type (Etype (First_Formal (It.Nam)))
3893                             = Base_Type (Etype (First_Formal (New_S))))
3894             then
3895                Candidate_Renaming := It.Nam;
3896             end if;
3897
3898             Get_Next_Interp (Ind, It);
3899          end loop;
3900
3901          Set_Entity (Nam, Old_S);
3902          Set_Is_Overloaded (Nam, False);
3903       end if;
3904
3905       return Old_S;
3906    end Find_Renamed_Entity;
3907
3908    -----------------------------
3909    -- Find_Selected_Component --
3910    -----------------------------
3911
3912    procedure Find_Selected_Component (N : Node_Id) is
3913       P : constant Node_Id := Prefix (N);
3914
3915       P_Name : Entity_Id;
3916       --  Entity denoted by prefix
3917
3918       P_Type : Entity_Id;
3919       --  and its type
3920
3921       Nam : Node_Id;
3922
3923    begin
3924       Analyze (P);
3925
3926       if Nkind (P) = N_Error then
3927          return;
3928
3929       --  If the selector already has an entity, the node has been
3930       --  constructed in the course of expansion, and is known to be
3931       --  valid. Do not verify that it is defined for the type (it may
3932       --  be a private component used in the expansion of record equality).
3933
3934       elsif Present (Entity (Selector_Name (N))) then
3935
3936          if No (Etype (N))
3937            or else Etype (N) = Any_Type
3938          then
3939             declare
3940                Sel_Name : constant Node_Id   := Selector_Name (N);
3941                Selector : constant Entity_Id := Entity (Sel_Name);
3942                C_Etype  : Node_Id;
3943
3944             begin
3945                Set_Etype (Sel_Name, Etype (Selector));
3946
3947                if not Is_Entity_Name (P) then
3948                   Resolve (P);
3949                end if;
3950
3951                --  Build an actual subtype except for the first parameter
3952                --  of an init proc, where this actual subtype is by
3953                --  definition incorrect, since the object is uninitialized
3954                --  (and does not even have defined discriminants etc.)
3955
3956                if Is_Entity_Name (P)
3957                  and then Ekind (Entity (P)) = E_Function
3958                then
3959                   Nam := New_Copy (P);
3960
3961                   if Is_Overloaded (P) then
3962                      Save_Interps (P, Nam);
3963                   end if;
3964
3965                   Rewrite (P,
3966                     Make_Function_Call (Sloc (P), Name => Nam));
3967                   Analyze_Call (P);
3968                   Analyze_Selected_Component (N);
3969                   return;
3970
3971                elsif Ekind (Selector) = E_Component
3972                  and then (not Is_Entity_Name (P)
3973                             or else Chars (Entity (P)) /= Name_uInit)
3974                then
3975                   C_Etype :=
3976                     Build_Actual_Subtype_Of_Component (
3977                       Etype (Selector), N);
3978                else
3979                   C_Etype := Empty;
3980                end if;
3981
3982                if No (C_Etype) then
3983                   C_Etype := Etype (Selector);
3984                else
3985                   Insert_Action (N, C_Etype);
3986                   C_Etype := Defining_Identifier (C_Etype);
3987                end if;
3988
3989                Set_Etype (N, C_Etype);
3990             end;
3991
3992             --  If this is the name of an entry or protected operation, and
3993             --  the prefix is an access type, insert an explicit dereference,
3994             --  so that entry calls are treated uniformly.
3995
3996             if Is_Access_Type (Etype (P))
3997               and then Is_Concurrent_Type (Designated_Type (Etype (P)))
3998             then
3999                declare
4000                   New_P : constant Node_Id :=
4001                             Make_Explicit_Dereference (Sloc (P),
4002                               Prefix => Relocate_Node (P));
4003                begin
4004                   Rewrite (P, New_P);
4005                   Set_Etype (P, Designated_Type (Etype (Prefix (P))));
4006                end;
4007             end if;
4008
4009          --  If the selected component appears within a default expression
4010          --  and it has an actual subtype, the pre-analysis has not yet
4011          --  completed its analysis, because Insert_Actions is disabled in
4012          --  that context. Within the init proc of the enclosing type we
4013          --  must complete this analysis, if an actual subtype was created.
4014
4015          elsif Inside_Init_Proc then
4016             declare
4017                Typ  : constant Entity_Id := Etype (N);
4018                Decl : constant Node_Id   := Declaration_Node (Typ);
4019
4020             begin
4021                if Nkind (Decl) = N_Subtype_Declaration
4022                  and then not Analyzed (Decl)
4023                  and then Is_List_Member (Decl)
4024                  and then No (Parent (Decl))
4025                then
4026                   Remove (Decl);
4027                   Insert_Action (N, Decl);
4028                end if;
4029             end;
4030          end if;
4031
4032          return;
4033
4034       elsif Is_Entity_Name (P) then
4035          P_Name := Entity (P);
4036
4037          --  The prefix may denote an enclosing type which is the completion
4038          --  of an incomplete type declaration.
4039
4040          if Is_Type (P_Name) then
4041             Set_Entity (P, Get_Full_View (P_Name));
4042             Set_Etype  (P, Entity (P));
4043             P_Name := Entity (P);
4044          end if;
4045
4046          P_Type := Base_Type (Etype (P));
4047
4048          if Debug_Flag_E then
4049             Write_Str ("Found prefix type to be ");
4050             Write_Entity_Info (P_Type, "      "); Write_Eol;
4051          end if;
4052
4053          --  First check for components of a record object (not the
4054          --  result of a call, which is handled below).
4055
4056          if Is_Appropriate_For_Record (P_Type)
4057            and then not Is_Overloadable (P_Name)
4058            and then not Is_Type (P_Name)
4059          then
4060             --  Selected component of record. Type checking will validate
4061             --  name of selector.
4062
4063             Analyze_Selected_Component (N);
4064
4065          elsif Is_Appropriate_For_Entry_Prefix (P_Type)
4066            and then not In_Open_Scopes (P_Name)
4067            and then (not Is_Concurrent_Type (Etype (P_Name))
4068                        or else not In_Open_Scopes (Etype (P_Name)))
4069          then
4070             --  Call to protected operation or entry. Type checking is
4071             --  needed on the prefix.
4072
4073             Analyze_Selected_Component (N);
4074
4075          elsif (In_Open_Scopes (P_Name)
4076                   and then Ekind (P_Name) /= E_Void
4077                   and then not Is_Overloadable (P_Name))
4078            or else (Is_Concurrent_Type (Etype (P_Name))
4079                       and then In_Open_Scopes (Etype (P_Name)))
4080          then
4081             --  Prefix denotes an enclosing loop, block, or task, i.e. an
4082             --  enclosing construct that is not a subprogram or accept.
4083
4084             Find_Expanded_Name (N);
4085
4086          elsif Ekind (P_Name) = E_Package then
4087             Find_Expanded_Name (N);
4088
4089          elsif Is_Overloadable (P_Name) then
4090
4091             --  The subprogram may be a renaming (of an enclosing scope) as
4092             --  in the case of the name of the generic within an instantiation.
4093
4094             if (Ekind (P_Name) = E_Procedure
4095                  or else Ekind (P_Name) = E_Function)
4096               and then Present (Alias (P_Name))
4097               and then Is_Generic_Instance (Alias (P_Name))
4098             then
4099                P_Name := Alias (P_Name);
4100             end if;
4101
4102             if Is_Overloaded (P) then
4103
4104                --  The prefix must resolve to a unique enclosing construct
4105
4106                declare
4107                   Found : Boolean := False;
4108                   Ind   : Interp_Index;
4109                   It    : Interp;
4110
4111                begin
4112                   Get_First_Interp (P, Ind, It);
4113
4114                   while Present (It.Nam) loop
4115
4116                      if In_Open_Scopes (It.Nam) then
4117                         if Found then
4118                            Error_Msg_N (
4119                               "prefix must be unique enclosing scope", N);
4120                            Set_Entity (N, Any_Id);
4121                            Set_Etype  (N, Any_Type);
4122                            return;
4123
4124                         else
4125                            Found := True;
4126                            P_Name := It.Nam;
4127                         end if;
4128                      end if;
4129
4130                      Get_Next_Interp (Ind, It);
4131                   end loop;
4132                end;
4133             end if;
4134
4135             if In_Open_Scopes (P_Name) then
4136                Set_Entity (P, P_Name);
4137                Set_Is_Overloaded (P, False);
4138                Find_Expanded_Name (N);
4139
4140             else
4141                --  If no interpretation as an expanded name is possible, it
4142                --  must be a selected component of a record returned by a
4143                --  function call. Reformat prefix as a function call, the
4144                --  rest is done by type resolution. If the prefix is a
4145                --  procedure or entry, as is P.X;  this is an error.
4146
4147                if Ekind (P_Name) /= E_Function
4148                  and then (not Is_Overloaded (P)
4149                              or else
4150                            Nkind (Parent (N)) = N_Procedure_Call_Statement)
4151                then
4152
4153                   --  Prefix may mention a package that is hidden by a local
4154                   --  declaration: let the user know. Scan the full homonym
4155                   --  chain, the candidate package may be anywhere on it.
4156
4157                   if Present (Homonym (Current_Entity (P_Name))) then
4158
4159                      P_Name := Current_Entity (P_Name);
4160
4161                      while Present (P_Name) loop
4162                         exit when Ekind (P_Name) = E_Package;
4163                         P_Name := Homonym (P_Name);
4164                      end loop;
4165
4166                      if Present (P_Name) then
4167                         Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
4168
4169                         Error_Msg_NE
4170                           ("package& is hidden by declaration#",
4171                             N, P_Name);
4172
4173                         Set_Entity (Prefix (N), P_Name);
4174                         Find_Expanded_Name (N);
4175                         return;
4176                      else
4177                         P_Name := Entity (Prefix (N));
4178                      end if;
4179                   end if;
4180
4181                   Error_Msg_NE
4182                     ("invalid prefix in selected component&", N, P_Name);
4183                   Change_Selected_Component_To_Expanded_Name (N);
4184                   Set_Entity (N, Any_Id);
4185                   Set_Etype (N, Any_Type);
4186
4187                else
4188                   Nam := New_Copy (P);
4189                   Save_Interps (P, Nam);
4190                   Rewrite (P,
4191                     Make_Function_Call (Sloc (P), Name => Nam));
4192                   Analyze_Call (P);
4193                   Analyze_Selected_Component (N);
4194                end if;
4195             end if;
4196
4197          --  Remaining cases generate various error messages
4198
4199          else
4200             --  Format node as expanded name, to avoid cascaded errors
4201
4202             Change_Selected_Component_To_Expanded_Name (N);
4203             Set_Entity  (N, Any_Id);
4204             Set_Etype   (N, Any_Type);
4205
4206             --  Issue error message, but avoid this if error issued already.
4207             --  Use identifier of prefix if one is available.
4208
4209             if P_Name = Any_Id  then
4210                null;
4211
4212             elsif Ekind (P_Name) = E_Void then
4213                Premature_Usage (P);
4214
4215             elsif Nkind (P) /= N_Attribute_Reference then
4216                Error_Msg_N (
4217                 "invalid prefix in selected component&", P);
4218
4219                if Is_Access_Type (P_Type)
4220                  and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
4221                then
4222                   Error_Msg_N
4223                     ("\dereference must not be of an incomplete type " &
4224                        "('R'M 3.10.1)", P);
4225                end if;
4226
4227             else
4228                Error_Msg_N (
4229                 "invalid prefix in selected component", P);
4230             end if;
4231          end if;
4232
4233       else
4234          --  If prefix is not the name of an entity, it must be an expression,
4235          --  whose type is appropriate for a record. This is determined by
4236          --  type resolution.
4237
4238          Analyze_Selected_Component (N);
4239       end if;
4240    end Find_Selected_Component;
4241
4242    ---------------
4243    -- Find_Type --
4244    ---------------
4245
4246    procedure Find_Type (N : Node_Id) is
4247       C      : Entity_Id;
4248       Typ    : Entity_Id;
4249       T      : Entity_Id;
4250       T_Name : Entity_Id;
4251
4252    begin
4253       if N = Error then
4254          return;
4255
4256       elsif Nkind (N) = N_Attribute_Reference then
4257
4258          --  Class attribute. This is only valid in Ada 95 mode, but we don't
4259          --  do a check, since the tagged type referenced could only exist if
4260          --  we were in 95 mode when it was declared (or, if we were in Ada
4261          --  83 mode, then an error message would already have been issued).
4262
4263          if Attribute_Name (N) = Name_Class then
4264             Check_Restriction (No_Dispatch, N);
4265             Find_Type (Prefix (N));
4266
4267             --  Propagate error from bad prefix
4268
4269             if Etype (Prefix (N)) = Any_Type then
4270                Set_Entity (N, Any_Type);
4271                Set_Etype  (N, Any_Type);
4272                return;
4273             end if;
4274
4275             T := Base_Type (Entity (Prefix (N)));
4276
4277             --  Case of non-tagged type
4278
4279             if not Is_Tagged_Type (T) then
4280                if Ekind (T) = E_Incomplete_Type then
4281
4282                   --  It is legal to denote the class type of an incomplete
4283                   --  type. The full type will have to be tagged, of course.
4284
4285                   Set_Is_Tagged_Type (T);
4286                   Make_Class_Wide_Type (T);
4287                   Set_Entity (N, Class_Wide_Type (T));
4288                   Set_Etype  (N, Class_Wide_Type (T));
4289
4290                elsif Ekind (T) = E_Private_Type
4291                  and then not Is_Generic_Type (T)
4292                  and then In_Private_Part (Scope (T))
4293                then
4294                   --  The Class attribute can be applied to an untagged
4295                   --  private type fulfilled by a tagged type prior to
4296                   --  the full type declaration (but only within the
4297                   --  parent package's private part). Create the class-wide
4298                   --  type now and check that the full type is tagged
4299                   --  later during its analysis. Note that we do not
4300                   --  mark the private type as tagged, unlike the case
4301                   --  of incomplete types, because the type must still
4302                   --  appear untagged to outside units.
4303
4304                   if not Present (Class_Wide_Type (T)) then
4305                      Make_Class_Wide_Type (T);
4306                   end if;
4307
4308                   Set_Entity (N, Class_Wide_Type (T));
4309                   Set_Etype  (N, Class_Wide_Type (T));
4310
4311                else
4312                   --  Should we introduce a type Any_Tagged and use
4313                   --  Wrong_Type here, it would be a bit more consistent???
4314
4315                   Error_Msg_NE
4316                     ("tagged type required, found}",
4317                      Prefix (N), First_Subtype (T));
4318                   Set_Entity (N, Any_Type);
4319                   return;
4320                end if;
4321
4322             --  Case of tagged type
4323
4324             else
4325                C := Class_Wide_Type (Entity (Prefix (N)));
4326                Set_Entity_With_Style_Check (N, C);
4327                Generate_Reference (C, N);
4328                Set_Etype (N, C);
4329             end if;
4330
4331          --  Base attribute, not allowed in Ada 83
4332
4333          elsif Attribute_Name (N) = Name_Base then
4334             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4335                Error_Msg_N
4336                  ("(Ada 83) Base attribute not allowed in subtype mark", N);
4337
4338             else
4339                Find_Type (Prefix (N));
4340                Typ := Entity (Prefix (N));
4341
4342                if Ada_Version >= Ada_95
4343                  and then not Is_Scalar_Type (Typ)
4344                  and then not Is_Generic_Type (Typ)
4345                then
4346                   Error_Msg_N
4347                     ("prefix of Base attribute must be scalar type",
4348                       Prefix (N));
4349
4350                elsif Sloc (Typ) = Standard_Location
4351                  and then Base_Type (Typ) = Typ
4352                  and then Warn_On_Redundant_Constructs
4353                then
4354                   Error_Msg_NE
4355                     ("?redudant attribute, & is its own base type", N, Typ);
4356                end if;
4357
4358                T := Base_Type (Typ);
4359
4360                --  Rewrite attribute reference with type itself (see similar
4361                --  processing in Analyze_Attribute, case Base). Preserve
4362                --  prefix if present, for other legality checks.
4363
4364                if Nkind (Prefix (N)) = N_Expanded_Name then
4365                   Rewrite (N,
4366                      Make_Expanded_Name (Sloc (N),
4367                        Chars     => Chars (Entity (N)),
4368                        Prefix    => New_Copy (Prefix (Prefix (N))),
4369                        Selector_Name =>
4370                          New_Reference_To (Entity (N), Sloc (N))));
4371
4372                else
4373                   Rewrite (N,
4374                     New_Reference_To (Entity (N), Sloc (N)));
4375                end if;
4376
4377                Set_Entity (N, T);
4378                Set_Etype (N, T);
4379             end if;
4380
4381          --  All other attributes are invalid in a subtype mark
4382
4383          else
4384             Error_Msg_N ("invalid attribute in subtype mark", N);
4385          end if;
4386
4387       else
4388          Analyze (N);
4389
4390          if Is_Entity_Name (N) then
4391             T_Name := Entity (N);
4392          else
4393             Error_Msg_N ("subtype mark required in this context", N);
4394             Set_Etype (N, Any_Type);
4395             return;
4396          end if;
4397
4398          if T_Name  = Any_Id or else Etype (N) = Any_Type then
4399
4400             --  Undefined id. Make it into a valid type
4401
4402             Set_Entity (N, Any_Type);
4403
4404          elsif not Is_Type (T_Name)
4405            and then T_Name /= Standard_Void_Type
4406          then
4407             Error_Msg_Sloc := Sloc (T_Name);
4408             Error_Msg_N ("subtype mark required in this context", N);
4409             Error_Msg_NE ("\found & declared#", N, T_Name);
4410             Set_Entity (N, Any_Type);
4411
4412          else
4413             T_Name := Get_Full_View (T_Name);
4414
4415             if In_Open_Scopes (T_Name) then
4416                if Ekind (Base_Type (T_Name)) = E_Task_Type then
4417                   Error_Msg_N ("task type cannot be used as type mark " &
4418                      "within its own body", N);
4419                else
4420                   Error_Msg_N ("type declaration cannot refer to itself", N);
4421                end if;
4422
4423                Set_Etype (N, Any_Type);
4424                Set_Entity (N, Any_Type);
4425                Set_Error_Posted (T_Name);
4426                return;
4427             end if;
4428
4429             Set_Entity (N, T_Name);
4430             Set_Etype  (N, T_Name);
4431          end if;
4432       end if;
4433
4434       if Present (Etype (N)) and then Comes_From_Source (N) then
4435          if Is_Fixed_Point_Type (Etype (N)) then
4436             Check_Restriction (No_Fixed_Point, N);
4437          elsif Is_Floating_Point_Type (Etype (N)) then
4438             Check_Restriction (No_Floating_Point, N);
4439          end if;
4440       end if;
4441    end Find_Type;
4442
4443    -------------------
4444    -- Get_Full_View --
4445    -------------------
4446
4447    function Get_Full_View (T_Name : Entity_Id) return Entity_Id is
4448    begin
4449       if Ekind (T_Name) = E_Incomplete_Type
4450         and then Present (Full_View (T_Name))
4451       then
4452          return Full_View (T_Name);
4453
4454       elsif Is_Class_Wide_Type (T_Name)
4455         and then Ekind (Root_Type (T_Name)) = E_Incomplete_Type
4456         and then Present (Full_View (Root_Type (T_Name)))
4457       then
4458          return Class_Wide_Type (Full_View (Root_Type (T_Name)));
4459
4460       else
4461          return T_Name;
4462       end if;
4463    end Get_Full_View;
4464
4465    ------------------------------------
4466    -- Has_Implicit_Character_Literal --
4467    ------------------------------------
4468
4469    function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
4470       Id      : Entity_Id;
4471       Found   : Boolean := False;
4472       P       : constant Entity_Id := Entity (Prefix (N));
4473       Priv_Id : Entity_Id := Empty;
4474
4475    begin
4476       if Ekind (P) = E_Package
4477         and then not In_Open_Scopes (P)
4478       then
4479          Priv_Id := First_Private_Entity (P);
4480       end if;
4481
4482       if P = Standard_Standard then
4483          Change_Selected_Component_To_Expanded_Name (N);
4484          Rewrite (N, Selector_Name (N));
4485          Analyze (N);
4486          Set_Etype (Original_Node (N), Standard_Character);
4487          return True;
4488       end if;
4489
4490       Id := First_Entity (P);
4491
4492       while Present (Id)
4493         and then Id /= Priv_Id
4494       loop
4495          if Is_Character_Type (Id)
4496            and then (Root_Type (Id) = Standard_Character
4497                        or else Root_Type (Id) = Standard_Wide_Character)
4498            and then Id = Base_Type (Id)
4499          then
4500             --  We replace the node with the literal itself, resolve as a
4501             --  character, and set the type correctly.
4502
4503             if not Found then
4504                Change_Selected_Component_To_Expanded_Name (N);
4505                Rewrite (N, Selector_Name (N));
4506                Analyze (N);
4507                Set_Etype (N, Id);
4508                Set_Etype (Original_Node (N), Id);
4509                Found := True;
4510
4511             else
4512                --  More than one type derived from Character in given scope.
4513                --  Collect all possible interpretations.
4514
4515                Add_One_Interp (N, Id, Id);
4516             end if;
4517          end if;
4518
4519          Next_Entity (Id);
4520       end loop;
4521
4522       return Found;
4523    end Has_Implicit_Character_Literal;
4524
4525    ----------------------
4526    -- Has_Private_With --
4527    ----------------------
4528
4529    function Has_Private_With (E : Entity_Id) return Boolean is
4530       Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
4531       Item      : Node_Id;
4532
4533    begin
4534       Item := First (Context_Items (Comp_Unit));
4535       while Present (Item) loop
4536          if Nkind (Item) = N_With_Clause
4537            and then Private_Present (Item)
4538            and then Entity (Name (Item)) = E
4539          then
4540             return True;
4541          end if;
4542
4543          Next (Item);
4544       end loop;
4545
4546       return False;
4547    end Has_Private_With;
4548
4549    ---------------------------
4550    -- Has_Implicit_Operator --
4551    ---------------------------
4552
4553    function Has_Implicit_Operator (N : Node_Id) return Boolean is
4554       Op_Id   : constant Name_Id   := Chars (Selector_Name (N));
4555       P       : constant Entity_Id := Entity (Prefix (N));
4556       Id      : Entity_Id;
4557       Priv_Id : Entity_Id := Empty;
4558
4559       procedure Add_Implicit_Operator
4560         (T       : Entity_Id;
4561          Op_Type : Entity_Id := Empty);
4562       --  Add implicit interpretation to node N, using the type for which
4563       --  a predefined operator exists. If the operator yields a boolean
4564       --  type, the Operand_Type is implicitly referenced by the operator,
4565       --  and a reference to it must be generated.
4566
4567       ---------------------------
4568       -- Add_Implicit_Operator --
4569       ---------------------------
4570
4571       procedure Add_Implicit_Operator
4572         (T       : Entity_Id;
4573          Op_Type : Entity_Id := Empty)
4574       is
4575          Predef_Op : Entity_Id;
4576
4577       begin
4578          Predef_Op := Current_Entity (Selector_Name (N));
4579
4580          while Present (Predef_Op)
4581            and then Scope (Predef_Op) /= Standard_Standard
4582          loop
4583             Predef_Op := Homonym (Predef_Op);
4584          end loop;
4585
4586          if Nkind (N) = N_Selected_Component then
4587             Change_Selected_Component_To_Expanded_Name (N);
4588          end if;
4589
4590          Add_One_Interp (N, Predef_Op, T);
4591
4592          --  For operators with unary and binary interpretations, add both
4593
4594          if Present (Homonym (Predef_Op)) then
4595             Add_One_Interp (N, Homonym (Predef_Op), T);
4596          end if;
4597
4598          --  The node is a reference to a predefined operator, and
4599          --  an implicit reference to the type of its operands.
4600
4601          if Present (Op_Type) then
4602             Generate_Operator_Reference (N, Op_Type);
4603          else
4604             Generate_Operator_Reference (N, T);
4605          end if;
4606       end Add_Implicit_Operator;
4607
4608    --  Start of processing for Has_Implicit_Operator
4609
4610    begin
4611
4612       if Ekind (P) = E_Package
4613         and then not In_Open_Scopes (P)
4614       then
4615          Priv_Id := First_Private_Entity (P);
4616       end if;
4617
4618       Id := First_Entity (P);
4619
4620       case Op_Id is
4621
4622          --  Boolean operators: an implicit declaration exists if the scope
4623          --  contains a declaration for a derived Boolean type, or for an
4624          --  array of Boolean type.
4625
4626          when Name_Op_And | Name_Op_Not | Name_Op_Or  | Name_Op_Xor =>
4627
4628             while Id  /= Priv_Id loop
4629
4630                if Valid_Boolean_Arg (Id)
4631                  and then Id = Base_Type (Id)
4632                then
4633                   Add_Implicit_Operator (Id);
4634                   return True;
4635                end if;
4636
4637                Next_Entity (Id);
4638             end loop;
4639
4640          --  Equality: look for any non-limited type (result is Boolean)
4641
4642          when Name_Op_Eq | Name_Op_Ne =>
4643
4644             while Id  /= Priv_Id loop
4645
4646                if Is_Type (Id)
4647                  and then not Is_Limited_Type (Id)
4648                  and then Id = Base_Type (Id)
4649                then
4650                   Add_Implicit_Operator (Standard_Boolean, Id);
4651                   return True;
4652                end if;
4653
4654                Next_Entity (Id);
4655             end loop;
4656
4657          --  Comparison operators: scalar type, or array of scalar
4658
4659          when Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge =>
4660
4661             while Id  /= Priv_Id loop
4662                if (Is_Scalar_Type (Id)
4663                  or else (Is_Array_Type (Id)
4664                            and then Is_Scalar_Type (Component_Type (Id))))
4665                  and then Id = Base_Type (Id)
4666                then
4667                   Add_Implicit_Operator (Standard_Boolean, Id);
4668                   return True;
4669                end if;
4670
4671                Next_Entity (Id);
4672             end loop;
4673
4674          --  Arithmetic operators: any numeric type
4675
4676          when Name_Op_Abs      |
4677               Name_Op_Add      |
4678               Name_Op_Mod      |
4679               Name_Op_Rem      |
4680               Name_Op_Subtract |
4681               Name_Op_Multiply |
4682               Name_Op_Divide   |
4683               Name_Op_Expon    =>
4684
4685             while Id  /= Priv_Id loop
4686                if Is_Numeric_Type (Id)
4687                  and then Id = Base_Type (Id)
4688                then
4689                   Add_Implicit_Operator (Id);
4690                   return True;
4691                end if;
4692
4693                Next_Entity (Id);
4694             end loop;
4695
4696          --  Concatenation: any one-dimensional array type
4697
4698          when Name_Op_Concat =>
4699
4700             while Id  /= Priv_Id loop
4701                if Is_Array_Type (Id) and then Number_Dimensions (Id) = 1
4702                  and then Id = Base_Type (Id)
4703                then
4704                   Add_Implicit_Operator (Id);
4705                   return True;
4706                end if;
4707
4708                Next_Entity (Id);
4709             end loop;
4710
4711          --  What is the others condition here? Should we be using a
4712          --  subtype of Name_Id that would restrict to operators ???
4713
4714          when others => null;
4715
4716       end case;
4717
4718       --  If we fall through, then we do not have an implicit operator
4719
4720       return False;
4721
4722    end Has_Implicit_Operator;
4723
4724    --------------------
4725    -- In_Open_Scopes --
4726    --------------------
4727
4728    function In_Open_Scopes (S : Entity_Id) return Boolean is
4729    begin
4730       --  Since there are several scope stacks maintained by Scope_Stack each
4731       --  delineated by Standard (see comments by definition of Scope_Stack)
4732       --  it is necessary to end the search when Standard is reached.
4733
4734       for J in reverse 0 .. Scope_Stack.Last loop
4735          if Scope_Stack.Table (J).Entity = S then
4736             return True;
4737          end if;
4738
4739          --  We need Is_Active_Stack_Base to tell us when to stop rather
4740          --  than checking for Standard_Standard because there are cases
4741          --  where Standard_Standard appears in the middle of the active
4742          --  set of scopes. This affects the declaration and overriding
4743          --  of private inherited operations in instantiations of generic
4744          --  child units.
4745
4746          exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
4747       end loop;
4748
4749       return False;
4750    end In_Open_Scopes;
4751
4752    -----------------------------
4753    -- Inherit_Renamed_Profile --
4754    -----------------------------
4755
4756    procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
4757       New_F : Entity_Id;
4758       Old_F : Entity_Id;
4759       Old_T : Entity_Id;
4760       New_T : Entity_Id;
4761
4762    begin
4763       if Ekind (Old_S) = E_Operator then
4764
4765          New_F := First_Formal (New_S);
4766
4767          while Present (New_F) loop
4768             Set_Etype (New_F, Base_Type (Etype (New_F)));
4769             Next_Formal (New_F);
4770          end loop;
4771
4772          Set_Etype (New_S, Base_Type (Etype (New_S)));
4773
4774       else
4775          New_F := First_Formal (New_S);
4776          Old_F := First_Formal (Old_S);
4777
4778          while Present (New_F) loop
4779             New_T := Etype (New_F);
4780             Old_T := Etype (Old_F);
4781
4782             --  If the new type is a renaming of the old one, as is the
4783             --  case for actuals in instances, retain its name, to simplify
4784             --  later disambiguation.
4785
4786             if Nkind (Parent (New_T)) = N_Subtype_Declaration
4787               and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
4788               and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
4789             then
4790                null;
4791             else
4792                Set_Etype (New_F, Old_T);
4793             end if;
4794
4795             Next_Formal (New_F);
4796             Next_Formal (Old_F);
4797          end loop;
4798
4799          if Ekind (Old_S) = E_Function
4800            or else Ekind (Old_S) = E_Enumeration_Literal
4801          then
4802             Set_Etype (New_S, Etype (Old_S));
4803          end if;
4804       end if;
4805    end Inherit_Renamed_Profile;
4806
4807    ----------------
4808    -- Initialize --
4809    ----------------
4810
4811    procedure Initialize is
4812    begin
4813       Urefs.Init;
4814    end Initialize;
4815
4816    -------------------------
4817    -- Install_Use_Clauses --
4818    -------------------------
4819
4820    procedure Install_Use_Clauses
4821      (Clause             : Node_Id;
4822       Force_Installation : Boolean := False)
4823    is
4824       U  : Node_Id := Clause;
4825       P  : Node_Id;
4826       Id : Entity_Id;
4827
4828    begin
4829       while Present (U) loop
4830
4831          --  Case of USE package
4832
4833          if Nkind (U) = N_Use_Package_Clause then
4834             P := First (Names (U));
4835
4836             while Present (P) loop
4837                Id := Entity (P);
4838
4839                if Ekind (Id) = E_Package then
4840
4841                   if In_Use (Id) then
4842                      Set_Redundant_Use (P, True);
4843
4844                   elsif Present (Renamed_Object (Id))
4845                     and then In_Use (Renamed_Object (Id))
4846                   then
4847                      Set_Redundant_Use (P, True);
4848
4849                   elsif Force_Installation or else Applicable_Use (P) then
4850                      Use_One_Package (Id, U);
4851
4852                   end if;
4853                end if;
4854
4855                Next (P);
4856             end loop;
4857
4858          --  case of USE TYPE
4859
4860          else
4861             P := First (Subtype_Marks (U));
4862
4863             while Present (P) loop
4864                if not Is_Entity_Name (P)
4865                  or else No (Entity (P))
4866                then
4867                   null;
4868
4869                elsif Entity (P) /= Any_Type then
4870                   Use_One_Type (P);
4871                end if;
4872
4873                Next (P);
4874             end loop;
4875          end if;
4876
4877          Next_Use_Clause (U);
4878       end loop;
4879    end Install_Use_Clauses;
4880
4881    -------------------------------------
4882    -- Is_Appropriate_For_Entry_Prefix --
4883    -------------------------------------
4884
4885    function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
4886       P_Type : Entity_Id := T;
4887
4888    begin
4889       if Is_Access_Type (P_Type) then
4890          P_Type := Designated_Type (P_Type);
4891       end if;
4892
4893       return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
4894    end Is_Appropriate_For_Entry_Prefix;
4895
4896    -------------------------------
4897    -- Is_Appropriate_For_Record --
4898    -------------------------------
4899
4900    function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is
4901
4902       function Has_Components (T1 : Entity_Id) return Boolean;
4903       --  Determine if given type has components (i.e. is either a record
4904       --  type or a type that has discriminants).
4905
4906       function Has_Components (T1 : Entity_Id) return Boolean is
4907       begin
4908          return Is_Record_Type (T1)
4909            or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
4910            or else (Is_Task_Type (T1) and then Has_Discriminants (T1));
4911       end Has_Components;
4912
4913    --  Start of processing for Is_Appropriate_For_Record
4914
4915    begin
4916       return
4917         Present (T)
4918           and then (Has_Components (T)
4919                       or else (Is_Access_Type (T)
4920                                  and then
4921                                    Has_Components (Designated_Type (T))));
4922    end Is_Appropriate_For_Record;
4923
4924    ---------------
4925    -- New_Scope --
4926    ---------------
4927
4928    procedure New_Scope (S : Entity_Id) is
4929       E : Entity_Id;
4930
4931    begin
4932       if Ekind (S) = E_Void then
4933          null;
4934
4935       --  Set scope depth if not a non-concurrent type, and we have not
4936       --  yet set the scope depth. This means that we have the first
4937       --  occurrence of the scope, and this is where the depth is set.
4938
4939       elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
4940         and then not Scope_Depth_Set (S)
4941       then
4942          if S = Standard_Standard then
4943             Set_Scope_Depth_Value (S, Uint_0);
4944
4945          elsif Is_Child_Unit (S) then
4946             Set_Scope_Depth_Value (S, Uint_1);
4947
4948          elsif not Is_Record_Type (Current_Scope) then
4949             if Ekind (S) = E_Loop then
4950                Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
4951             else
4952                Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
4953             end if;
4954          end if;
4955       end if;
4956
4957       Scope_Stack.Increment_Last;
4958
4959       declare
4960          SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
4961
4962       begin
4963          SST.Entity                         := S;
4964          SST.Save_Scope_Suppress            := Scope_Suppress;
4965          SST.Save_Local_Entity_Suppress     := Local_Entity_Suppress.Last;
4966
4967          if Scope_Stack.Last > Scope_Stack.First then
4968             SST.Component_Alignment_Default := Scope_Stack.Table
4969                                                  (Scope_Stack.Last - 1).
4970                                                    Component_Alignment_Default;
4971          end if;
4972
4973          SST.Last_Subprogram_Name           := null;
4974          SST.Is_Transient                   := False;
4975          SST.Node_To_Be_Wrapped             := Empty;
4976          SST.Pending_Freeze_Actions         := No_List;
4977          SST.Actions_To_Be_Wrapped_Before   := No_List;
4978          SST.Actions_To_Be_Wrapped_After    := No_List;
4979          SST.First_Use_Clause               := Empty;
4980          SST.Is_Active_Stack_Base           := False;
4981       end;
4982
4983       if Debug_Flag_W then
4984          Write_Str ("--> new scope: ");
4985          Write_Name (Chars (Current_Scope));
4986          Write_Str (", Id=");
4987          Write_Int (Int (Current_Scope));
4988          Write_Str (", Depth=");
4989          Write_Int (Int (Scope_Stack.Last));
4990          Write_Eol;
4991       end if;
4992
4993       --  Copy from Scope (S) the categorization flags to S, this is not
4994       --  done in case Scope (S) is Standard_Standard since propagation
4995       --  is from library unit entity inwards.
4996
4997       if S /= Standard_Standard
4998         and then Scope (S) /= Standard_Standard
4999         and then not Is_Child_Unit (S)
5000       then
5001          E := Scope (S);
5002
5003          if Nkind (E) not in N_Entity then
5004             return;
5005          end if;
5006
5007          --  We only propagate inwards for library level entities,
5008          --  inner level subprograms do not inherit the categorization.
5009
5010          if Is_Library_Level_Entity (S) then
5011             Set_Is_Preelaborated (S, Is_Preelaborated (E));
5012             Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
5013             Set_Categorization_From_Scope (E => S, Scop => E);
5014          end if;
5015       end if;
5016    end New_Scope;
5017
5018    ---------------
5019    -- Pop_Scope --
5020    ---------------
5021
5022    procedure Pop_Scope is
5023       SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
5024
5025    begin
5026       if Debug_Flag_E then
5027          Write_Info;
5028       end if;
5029
5030       Scope_Suppress := SST.Save_Scope_Suppress;
5031       Local_Entity_Suppress.Set_Last (SST.Save_Local_Entity_Suppress);
5032
5033       if Debug_Flag_W then
5034          Write_Str ("--> exiting scope: ");
5035          Write_Name (Chars (Current_Scope));
5036          Write_Str (", Depth=");
5037          Write_Int (Int (Scope_Stack.Last));
5038          Write_Eol;
5039       end if;
5040
5041       End_Use_Clauses (SST.First_Use_Clause);
5042
5043       --  If the actions to be wrapped are still there they will get lost
5044       --  causing incomplete code to be generated. It is better to abort in
5045       --  this case (and we do the abort even with assertions off since the
5046       --  penalty is incorrect code generation)
5047
5048       if SST.Actions_To_Be_Wrapped_Before /= No_List
5049            or else
5050          SST.Actions_To_Be_Wrapped_After  /= No_List
5051       then
5052          return;
5053       end if;
5054
5055       --  Free last subprogram name if allocated, and pop scope
5056
5057       Free (SST.Last_Subprogram_Name);
5058       Scope_Stack.Decrement_Last;
5059    end Pop_Scope;
5060
5061    ---------------------
5062    -- Premature_Usage --
5063    ---------------------
5064
5065    procedure Premature_Usage (N : Node_Id) is
5066       Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
5067       E    : Entity_Id := Entity (N);
5068
5069    begin
5070       --  Within an instance, the analysis of the actual for a formal object
5071       --  does not see the name of the object itself. This is significant
5072       --  only if the object is an aggregate, where its analysis does not do
5073       --  any name resolution on component associations. (see 4717-008). In
5074       --  such a case, look for the visible homonym on the chain.
5075
5076       if In_Instance
5077         and then Present (Homonym (E))
5078       then
5079          E := Homonym (E);
5080
5081          while Present (E)
5082            and then not In_Open_Scopes (Scope (E))
5083          loop
5084             E := Homonym (E);
5085          end loop;
5086
5087          if Present (E) then
5088             Set_Entity (N, E);
5089             Set_Etype (N, Etype (E));
5090             return;
5091          end if;
5092       end if;
5093
5094       if Kind  = N_Component_Declaration then
5095          Error_Msg_N
5096            ("component&! cannot be used before end of record declaration", N);
5097
5098       elsif Kind  = N_Parameter_Specification then
5099          Error_Msg_N
5100            ("formal parameter&! cannot be used before end of specification",
5101             N);
5102
5103       elsif Kind  = N_Discriminant_Specification then
5104          Error_Msg_N
5105            ("discriminant&! cannot be used before end of discriminant part",
5106             N);
5107
5108       elsif Kind  = N_Procedure_Specification
5109         or else Kind = N_Function_Specification
5110       then
5111          Error_Msg_N
5112            ("subprogram&! cannot be used before end of its declaration",
5113             N);
5114       else
5115          Error_Msg_N
5116            ("object& cannot be used before end of its declaration!", N);
5117       end if;
5118    end Premature_Usage;
5119
5120    ------------------------
5121    -- Present_System_Aux --
5122    ------------------------
5123
5124    function Present_System_Aux (N : Node_Id := Empty) return Boolean is
5125       Loc      : Source_Ptr;
5126       Aux_Name : Name_Id;
5127       Unum     : Unit_Number_Type;
5128       Withn    : Node_Id;
5129       With_Sys : Node_Id;
5130       The_Unit : Node_Id;
5131
5132       function Find_System (C_Unit : Node_Id) return Entity_Id;
5133       --  Scan context clause of compilation unit to find a with_clause
5134       --  for System.
5135
5136       -----------------
5137       -- Find_System --
5138       -----------------
5139
5140       function Find_System (C_Unit : Node_Id) return Entity_Id is
5141          With_Clause : Node_Id;
5142
5143       begin
5144          With_Clause := First (Context_Items (C_Unit));
5145
5146          while Present (With_Clause) loop
5147             if (Nkind (With_Clause) = N_With_Clause
5148               and then Chars (Name (With_Clause)) = Name_System)
5149               and then Comes_From_Source (With_Clause)
5150             then
5151                return With_Clause;
5152             end if;
5153
5154             Next (With_Clause);
5155          end loop;
5156
5157          return Empty;
5158       end Find_System;
5159
5160    --  Start of processing for Present_System_Aux
5161
5162    begin
5163       --  The child unit may have been loaded and analyzed already
5164
5165       if Present (System_Aux_Id) then
5166          return True;
5167
5168       --  If no previous pragma for System.Aux, nothing to load
5169
5170       elsif No (System_Extend_Unit) then
5171          return False;
5172
5173       --  Use the unit name given in the pragma to retrieve the unit.
5174       --  Verify that System itself appears in the context clause of the
5175       --  current compilation. If System is not present, an error will
5176       --  have been reported already.
5177
5178       else
5179          With_Sys := Find_System (Cunit (Current_Sem_Unit));
5180
5181          The_Unit := Unit (Cunit (Current_Sem_Unit));
5182
5183          if No (With_Sys)
5184            and then (Nkind (The_Unit) = N_Package_Body
5185                       or else (Nkind (The_Unit) = N_Subprogram_Body
5186                         and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
5187          then
5188             With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
5189          end if;
5190
5191          if No (With_Sys)
5192            and then Present (N)
5193          then
5194             --  If we are compiling a subunit, we need to examine its
5195             --  context as well (Current_Sem_Unit is the parent unit);
5196
5197             The_Unit := Parent (N);
5198
5199             while Nkind (The_Unit) /= N_Compilation_Unit loop
5200                The_Unit := Parent (The_Unit);
5201             end loop;
5202
5203             if Nkind (Unit (The_Unit)) = N_Subunit then
5204                With_Sys := Find_System (The_Unit);
5205             end if;
5206          end if;
5207
5208          if No (With_Sys) then
5209             return False;
5210          end if;
5211
5212          Loc := Sloc (With_Sys);
5213          Get_Name_String (Chars (Expression (System_Extend_Unit)));
5214          Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
5215          Name_Buffer (1 .. 7) := "system.";
5216          Name_Buffer (Name_Len + 8) := '%';
5217          Name_Buffer (Name_Len + 9) := 's';
5218          Name_Len := Name_Len + 9;
5219          Aux_Name := Name_Find;
5220
5221          Unum :=
5222            Load_Unit
5223              (Load_Name  => Aux_Name,
5224               Required   => False,
5225               Subunit    => False,
5226               Error_Node => With_Sys);
5227
5228          if Unum /= No_Unit then
5229             Semantics (Cunit (Unum));
5230             System_Aux_Id :=
5231               Defining_Entity (Specification (Unit (Cunit (Unum))));
5232
5233             Withn := Make_With_Clause (Loc,
5234               Name =>
5235                 Make_Expanded_Name (Loc,
5236                   Chars  => Chars (System_Aux_Id),
5237                   Prefix =>
5238                     New_Reference_To (Scope (System_Aux_Id), Loc),
5239                   Selector_Name =>
5240                     New_Reference_To (System_Aux_Id, Loc)));
5241
5242             Set_Entity (Name (Withn), System_Aux_Id);
5243
5244             Set_Library_Unit          (Withn, Cunit (Unum));
5245             Set_Corresponding_Spec    (Withn, System_Aux_Id);
5246             Set_First_Name            (Withn, True);
5247             Set_Implicit_With         (Withn, True);
5248
5249             Insert_After (With_Sys, Withn);
5250             Mark_Rewrite_Insertion (Withn);
5251             Set_Context_Installed (Withn);
5252
5253             return True;
5254
5255          --  Here if unit load failed
5256
5257          else
5258             Error_Msg_Name_1 := Name_System;
5259             Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
5260             Error_Msg_N
5261               ("extension package `%.%` does not exist",
5262                Opt.System_Extend_Unit);
5263             return False;
5264          end if;
5265       end if;
5266    end Present_System_Aux;
5267
5268    -------------------------
5269    -- Restore_Scope_Stack --
5270    -------------------------
5271
5272    procedure Restore_Scope_Stack (Handle_Use : Boolean := True) is
5273       E         : Entity_Id;
5274       S         : Entity_Id;
5275       Comp_Unit : Node_Id;
5276       In_Child  : Boolean := False;
5277       Full_Vis  : Boolean := True;
5278       SS_Last   : constant Int := Scope_Stack.Last;
5279
5280    begin
5281       --  Restore visibility of previous scope stack, if any
5282
5283       for J in reverse 0 .. Scope_Stack.Last loop
5284          exit when  Scope_Stack.Table (J).Entity = Standard_Standard
5285             or else No (Scope_Stack.Table (J).Entity);
5286
5287          S := Scope_Stack.Table (J).Entity;
5288
5289          if not Is_Hidden_Open_Scope (S) then
5290
5291             --  If the parent scope is hidden, its entities are hidden as
5292             --  well, unless the entity is the instantiation currently
5293             --  being analyzed.
5294
5295             if not Is_Hidden_Open_Scope (Scope (S))
5296               or else not Analyzed (Parent (S))
5297               or else Scope (S) = Standard_Standard
5298             then
5299                Set_Is_Immediately_Visible (S, True);
5300             end if;
5301
5302             E := First_Entity (S);
5303
5304             while Present (E) loop
5305                if Is_Child_Unit (E) then
5306                   Set_Is_Immediately_Visible (E,
5307                     Is_Visible_Child_Unit (E) or else In_Open_Scopes (E));
5308                else
5309                   Set_Is_Immediately_Visible (E, True);
5310                end if;
5311
5312                Next_Entity (E);
5313
5314                if not Full_Vis then
5315                   exit when E = First_Private_Entity (S);
5316                end if;
5317             end loop;
5318
5319             --  The visibility of child units (siblings of current compilation)
5320             --  must be restored in any case. Their declarations may appear
5321             --  after the private part of the parent.
5322
5323             if not Full_Vis
5324               and then Present (E)
5325             then
5326                while Present (E) loop
5327                   if Is_Child_Unit (E) then
5328                      Set_Is_Immediately_Visible (E,
5329                        Is_Visible_Child_Unit (E) or else In_Open_Scopes (E));
5330                   end if;
5331
5332                   Next_Entity (E);
5333                end loop;
5334             end if;
5335          end if;
5336
5337          if Is_Child_Unit (S)
5338             and not In_Child     --  check only for current unit.
5339          then
5340             In_Child := True;
5341
5342             --  restore visibility of parents according to whether the child
5343             --  is private and whether we are in its visible part.
5344
5345             Comp_Unit := Parent (Unit_Declaration_Node (S));
5346
5347             if Nkind (Comp_Unit) = N_Compilation_Unit
5348               and then Private_Present (Comp_Unit)
5349             then
5350                Full_Vis := True;
5351
5352             elsif (Ekind (S) = E_Package
5353                     or else Ekind (S) = E_Generic_Package)
5354               and then (In_Private_Part (S)
5355                          or else In_Package_Body (S))
5356             then
5357                Full_Vis := True;
5358
5359             elsif (Ekind (S) = E_Procedure
5360                     or else Ekind (S) = E_Function)
5361               and then Has_Completion (S)
5362             then
5363                Full_Vis := True;
5364             else
5365                Full_Vis := False;
5366             end if;
5367          else
5368             Full_Vis := True;
5369          end if;
5370       end loop;
5371
5372       if SS_Last >= Scope_Stack.First
5373         and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
5374         and then Handle_Use
5375       then
5376          Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
5377       end if;
5378    end Restore_Scope_Stack;
5379
5380    ----------------------
5381    -- Save_Scope_Stack --
5382    ----------------------
5383
5384    procedure Save_Scope_Stack (Handle_Use : Boolean := True) is
5385       E       : Entity_Id;
5386       S       : Entity_Id;
5387       SS_Last : constant Int := Scope_Stack.Last;
5388
5389    begin
5390       if SS_Last >= Scope_Stack.First
5391         and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
5392       then
5393          if Handle_Use then
5394             End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
5395          end if;
5396
5397          --  If the call is from within a compilation unit, as when
5398          --  called from Rtsfind, make current entries in scope stack
5399          --  invisible while we analyze the new unit.
5400
5401          for J in reverse 0 .. SS_Last loop
5402             exit when  Scope_Stack.Table (J).Entity = Standard_Standard
5403                or else No (Scope_Stack.Table (J).Entity);
5404
5405             S := Scope_Stack.Table (J).Entity;
5406             Set_Is_Immediately_Visible (S, False);
5407             E := First_Entity (S);
5408
5409             while Present (E) loop
5410                Set_Is_Immediately_Visible (E, False);
5411                Next_Entity (E);
5412             end loop;
5413          end loop;
5414
5415       end if;
5416    end Save_Scope_Stack;
5417
5418    -------------
5419    -- Set_Use --
5420    -------------
5421
5422    procedure Set_Use (L : List_Id) is
5423       Decl      : Node_Id;
5424       Pack_Name : Node_Id;
5425       Pack      : Entity_Id;
5426       Id        : Entity_Id;
5427
5428    begin
5429       if Present (L) then
5430          Decl := First (L);
5431
5432          while Present (Decl) loop
5433             if Nkind (Decl) = N_Use_Package_Clause then
5434                Chain_Use_Clause (Decl);
5435                Pack_Name := First (Names (Decl));
5436
5437                while Present (Pack_Name) loop
5438                   Pack := Entity (Pack_Name);
5439
5440                   if Ekind (Pack) = E_Package
5441                     and then Applicable_Use (Pack_Name)
5442                   then
5443                      Use_One_Package (Pack, Decl);
5444                   end if;
5445
5446                   Next (Pack_Name);
5447                end loop;
5448
5449             elsif Nkind (Decl) = N_Use_Type_Clause  then
5450                Chain_Use_Clause (Decl);
5451                Id := First (Subtype_Marks (Decl));
5452
5453                while Present (Id) loop
5454                   if Entity (Id) /= Any_Type then
5455                      Use_One_Type (Id);
5456                   end if;
5457
5458                   Next (Id);
5459                end loop;
5460             end if;
5461
5462             Next (Decl);
5463          end loop;
5464       end if;
5465    end Set_Use;
5466
5467    ---------------------
5468    -- Use_One_Package --
5469    ---------------------
5470
5471    procedure Use_One_Package (P : Entity_Id; N : Node_Id) is
5472       Id               : Entity_Id;
5473       Prev             : Entity_Id;
5474       Current_Instance : Entity_Id := Empty;
5475       Real_P           : Entity_Id;
5476       Private_With_OK  : Boolean   := False;
5477
5478    begin
5479       if Ekind (P) /= E_Package then
5480          return;
5481       end if;
5482
5483       Set_In_Use (P);
5484
5485       --  Ada 2005 (AI-50217): Check restriction
5486
5487       if From_With_Type (P) then
5488          Error_Msg_N ("limited withed package cannot appear in use clause", N);
5489       end if;
5490
5491       --  Find enclosing instance, if any
5492
5493       if In_Instance then
5494          Current_Instance := Current_Scope;
5495
5496          while not Is_Generic_Instance (Current_Instance) loop
5497             Current_Instance := Scope (Current_Instance);
5498          end loop;
5499
5500          if No (Hidden_By_Use_Clause (N)) then
5501             Set_Hidden_By_Use_Clause (N, New_Elmt_List);
5502          end if;
5503       end if;
5504
5505       --  If unit is a package renaming, indicate that the renamed
5506       --  package is also in use (the flags on both entities must
5507       --  remain consistent, and a subsequent use of either of them
5508       --  should be recognized as redundant).
5509
5510       if Present (Renamed_Object (P)) then
5511          Set_In_Use (Renamed_Object (P));
5512          Real_P := Renamed_Object (P);
5513       else
5514          Real_P := P;
5515       end if;
5516
5517       --  Ada 2005 (AI-262): Check the use_clause of a private withed package
5518       --  found in the private part of a package specification
5519
5520       if In_Private_Part (Current_Scope)
5521         and then Has_Private_With (P)
5522         and then Is_Child_Unit (Current_Scope)
5523         and then Is_Child_Unit (P)
5524         and then Is_Ancestor_Package (Scope (Current_Scope), P)
5525       then
5526          Private_With_OK := True;
5527       end if;
5528
5529       --  Loop through entities in one package making them potentially
5530       --  use-visible.
5531
5532       Id := First_Entity (P);
5533       while Present (Id)
5534         and then (Id /= First_Private_Entity (P)
5535                     or else Private_With_OK) -- Ada 2005 (AI-262)
5536       loop
5537          Prev := Current_Entity (Id);
5538
5539          while Present (Prev) loop
5540             if Is_Immediately_Visible (Prev)
5541               and then (not Is_Overloadable (Prev)
5542                          or else not Is_Overloadable (Id)
5543                          or else (Type_Conformant (Id, Prev)))
5544             then
5545                if No (Current_Instance) then
5546
5547                   --  Potentially use-visible entity remains hidden
5548
5549                   goto Next_Usable_Entity;
5550
5551                --  A use clause within an instance hides outer global
5552                --  entities, which are not used to resolve local entities
5553                --  in the instance. Note that the predefined entities in
5554                --  Standard could not have been hidden in the generic by
5555                --  a use clause, and therefore remain visible. Other
5556                --  compilation units whose entities appear in Standard must
5557                --  be hidden in an instance.
5558
5559                --  To determine whether an entity is external to the instance
5560                --  we compare the scope depth of its scope with that of the
5561                --  current instance. However, a generic actual of a subprogram
5562                --  instance is declared in the wrapper package but will not be
5563                --  hidden by a use-visible entity.
5564
5565                elsif not Is_Hidden (Id)
5566                  and then not Is_Wrapper_Package (Scope (Prev))
5567                  and then Scope_Depth (Scope (Prev)) <
5568                           Scope_Depth (Current_Instance)
5569                  and then (Scope (Prev) /= Standard_Standard
5570                             or else Sloc (Prev) > Standard_Location)
5571                then
5572                   Set_Is_Potentially_Use_Visible (Id);
5573                   Set_Is_Immediately_Visible (Prev, False);
5574                   Append_Elmt (Prev, Hidden_By_Use_Clause (N));
5575                end if;
5576
5577             --  A user-defined operator is not use-visible if the
5578             --  predefined operator for the type is immediately visible,
5579             --  which is the case if the type of the operand is in an open
5580             --  scope. This does not apply to user-defined operators that
5581             --  have operands of different types, because the predefined
5582             --  mixed mode operations (multiplication and division) apply to
5583             --  universal types and do not hide anything.
5584
5585             elsif Ekind (Prev) = E_Operator
5586               and then Operator_Matches_Spec (Prev, Id)
5587               and then In_Open_Scopes
5588                (Scope (Base_Type (Etype (First_Formal (Id)))))
5589               and then (No (Next_Formal (First_Formal (Id)))
5590                          or else Etype (First_Formal (Id))
5591                            = Etype (Next_Formal (First_Formal (Id)))
5592                          or else Chars (Prev) = Name_Op_Expon)
5593             then
5594                goto Next_Usable_Entity;
5595             end if;
5596
5597             Prev := Homonym (Prev);
5598          end loop;
5599
5600          --  On exit, we know entity is not hidden, unless it is private
5601
5602          if not Is_Hidden (Id)
5603            and then ((not Is_Child_Unit (Id))
5604                        or else Is_Visible_Child_Unit (Id))
5605          then
5606             Set_Is_Potentially_Use_Visible (Id);
5607
5608             if Is_Private_Type (Id)
5609               and then Present (Full_View (Id))
5610             then
5611                Set_Is_Potentially_Use_Visible (Full_View (Id));
5612             end if;
5613          end if;
5614
5615          <<Next_Usable_Entity>>
5616             Next_Entity (Id);
5617       end loop;
5618
5619       --  Child units are also made use-visible by a use clause, but they
5620       --  may appear after all visible declarations in the parent entity list.
5621
5622       while Present (Id) loop
5623
5624          if Is_Child_Unit (Id)
5625            and then Is_Visible_Child_Unit (Id)
5626          then
5627             Set_Is_Potentially_Use_Visible (Id);
5628          end if;
5629
5630          Next_Entity (Id);
5631       end loop;
5632
5633       if Chars (Real_P) = Name_System
5634         and then Scope (Real_P) = Standard_Standard
5635         and then Present_System_Aux (N)
5636       then
5637          Use_One_Package (System_Aux_Id, N);
5638       end if;
5639
5640    end Use_One_Package;
5641
5642    ------------------
5643    -- Use_One_Type --
5644    ------------------
5645
5646    procedure Use_One_Type (Id : Node_Id) is
5647       T       : Entity_Id;
5648       Op_List : Elist_Id;
5649       Elmt    : Elmt_Id;
5650
5651    begin
5652       --  It is the type determined by the subtype mark (8.4(8)) whose
5653       --  operations become potentially use-visible.
5654
5655       T := Base_Type (Entity (Id));
5656
5657       Set_Redundant_Use
5658         (Id,
5659            In_Use (T)
5660              or else Is_Potentially_Use_Visible (T)
5661              or else In_Use (Scope (T)));
5662
5663       if In_Open_Scopes (Scope (T)) then
5664          null;
5665
5666       --  If the subtype mark designates a subtype in a different package,
5667       --  we have to check that the parent type is visible, otherwise the
5668       --  use type clause is a noop. Not clear how to do that???
5669
5670       elsif not Redundant_Use (Id) then
5671          Set_In_Use (T);
5672          Op_List := Collect_Primitive_Operations (T);
5673          Elmt := First_Elmt (Op_List);
5674
5675          while Present (Elmt) loop
5676
5677             if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
5678                  or else Chars (Node (Elmt)) in Any_Operator_Name)
5679               and then not Is_Hidden (Node (Elmt))
5680             then
5681                Set_Is_Potentially_Use_Visible (Node (Elmt));
5682             end if;
5683
5684             Next_Elmt (Elmt);
5685          end loop;
5686       end if;
5687    end Use_One_Type;
5688
5689    ----------------
5690    -- Write_Info --
5691    ----------------
5692
5693    procedure Write_Info is
5694       Id : Entity_Id := First_Entity (Current_Scope);
5695
5696    begin
5697       --  No point in dumping standard entities
5698
5699       if Current_Scope = Standard_Standard then
5700          return;
5701       end if;
5702
5703       Write_Str ("========================================================");
5704       Write_Eol;
5705       Write_Str ("        Defined Entities in ");
5706       Write_Name (Chars (Current_Scope));
5707       Write_Eol;
5708       Write_Str ("========================================================");
5709       Write_Eol;
5710
5711       if No (Id) then
5712          Write_Str ("-- none --");
5713          Write_Eol;
5714
5715       else
5716          while Present (Id) loop
5717             Write_Entity_Info (Id, " ");
5718             Next_Entity (Id);
5719          end loop;
5720       end if;
5721
5722       if Scope (Current_Scope) = Standard_Standard then
5723
5724          --  Print information on the current unit itself
5725
5726          Write_Entity_Info (Current_Scope, " ");
5727       end if;
5728
5729       Write_Eol;
5730    end Write_Info;
5731
5732    -----------------
5733    -- Write_Scopes --
5734    -----------------
5735
5736    procedure Write_Scopes is
5737       S : Entity_Id;
5738
5739    begin
5740       for J in reverse 1 .. Scope_Stack.Last loop
5741          S :=  Scope_Stack.Table (J).Entity;
5742          Write_Int (Int (S));
5743          Write_Str (" === ");
5744          Write_Name (Chars (S));
5745          Write_Eol;
5746       end loop;
5747    end Write_Scopes;
5748
5749 end Sem_Ch8;