OSDN Git Service

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