2003-11-27 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / sem_ch8.adb
blob6c65a7b5ecd8714cab8fa2253103c98946344ae4
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 ------------------------------------------------------------------------------
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;
63 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
65 package body Sem_Ch8 is
67 ------------------------------------
68 -- Visibility and Name Resolution --
69 ------------------------------------
71 -- This package handles name resolution and the collection of
72 -- interpretations for overloaded names, prior to overload resolution.
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.
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).
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.
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.
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.
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.
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.
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).
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 -- +----------------------------+
163 -- Name resolution must deal with several syntactic forms: simple names,
164 -- qualified names, indexed names, and various forms of calls.
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.
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.
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.
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.
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.
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.
203 ---------------------
204 -- The Scope Stack --
205 ---------------------
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.
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.
224 ------------------------
225 -- Compiling subunits --
226 ------------------------
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.
246 -------------------------------
247 -- Processing of USE Clauses --
248 -------------------------------
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:
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-.
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).
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.
275 -----------------------------------
276 -- Handling private declarations --
277 -----------------------------------
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.
297 ------------------------------------
298 -- Handling of Undefined Messages --
299 ------------------------------------
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.
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.
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.
320 Nvis : Boolean;
321 -- Set if the message is not visible rather than undefined
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.
329 end record;
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");
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.
345 -----------------------
346 -- Local Subprograms --
347 -----------------------
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
468 procedure Premature_Usage (N : Node_Id);
469 -- Diagnose usage of an entity before it is visible.
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).
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.
481 procedure Write_Info;
482 -- Write debugging information on entities declared in current scope
484 procedure Write_Scopes;
485 pragma Warnings (Off, Write_Scopes);
486 -- Debugging information: dump all entities on scope stack
488 --------------------------------
489 -- Analyze_Exception_Renaming --
490 --------------------------------
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.
496 procedure Analyze_Exception_Renaming (N : Node_Id) is
497 Id : constant Node_Id := Defining_Identifier (N);
498 Nam : constant Node_Id := Name (N);
500 begin
501 Enter_Name (Id);
502 Analyze (Nam);
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));
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;
522 ---------------------------
523 -- Analyze_Expanded_Name --
524 ---------------------------
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.
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;
540 Analyze (Prefix (N));
541 return;
542 else
543 Find_Expanded_Name (N);
544 end if;
545 end Analyze_Expanded_Name;
547 ----------------------------------------
548 -- Analyze_Generic_Function_Renaming --
549 ----------------------------------------
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;
556 ---------------------------------------
557 -- Analyze_Generic_Package_Renaming --
558 ---------------------------------------
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.
565 Text_IO_Kludge (Name (N));
567 Analyze_Generic_Renaming (N, E_Generic_Package);
568 end Analyze_Generic_Package_Renaming;
570 -----------------------------------------
571 -- Analyze_Generic_Procedure_Renaming --
572 -----------------------------------------
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;
579 ------------------------------
580 -- Analyze_Generic_Renaming --
581 ------------------------------
583 procedure Analyze_Generic_Renaming
584 (N : Node_Id;
585 K : Entity_Kind)
587 New_P : constant Entity_Id := Defining_Entity (N);
588 Old_P : Entity_Id;
589 Inst : Boolean := False; -- prevent junk warning
591 begin
592 if Name (N) = Error then
593 return;
594 end if;
596 Generate_Definition (New_P);
598 if Current_Scope /= Standard_Standard then
599 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
600 end if;
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;
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;
615 Enter_Name (New_P);
616 Set_Ekind (New_P, K);
618 if Etype (Old_P) = Any_Type then
619 null;
621 elsif Ekind (Old_P) /= K then
622 Error_Msg_N ("invalid generic unit name", Name (N));
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;
631 Set_Etype (New_P, Etype (Old_P));
632 Set_Has_Completion (New_P);
634 if In_Open_Scopes (Old_P) then
635 Error_Msg_N ("within its scope, generic denotes its instance", N);
636 end if;
638 Check_Library_Unit_Renaming (N, Old_P);
639 end if;
641 end Analyze_Generic_Renaming;
643 -----------------------------
644 -- Analyze_Object_Renaming --
645 -----------------------------
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;
655 begin
656 if Nam = Error then
657 return;
658 end if;
660 Set_Is_Pure (Id, Is_Pure (Current_Scope));
661 Enter_Name (Id);
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.
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);
675 if Present (Dec) then
676 Insert_Action (N, Dec);
677 T := Defining_Identifier (Dec);
678 Set_Etype (Nam, T);
679 end if;
681 else
682 Find_Type (S);
683 T := Entity (S);
684 Analyze_And_Resolve (Nam, T);
685 end if;
687 -- An object renaming requires an exact match of the type;
688 -- class-wide matching is not allowed.
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;
696 T2 := Etype (Nam);
697 Set_Ekind (Id, E_Variable);
698 Init_Size_Align (Id);
700 if T = Any_Type or else Etype (Nam) = Any_Type then
701 return;
703 -- Verify that the renamed entity is an object or a function call.
704 -- It may have been rewritten in several ways.
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;
716 -- A static function call may have been folded into a literal
718 elsif Nkind (Original_Node (Nam)) = N_Function_Call
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.
724 or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference
725 and then Is_Function_Attribute_Name
726 (Attribute_Name (Original_Node (Nam))))
728 -- Weird but legal, equivalent to renaming a function call.
730 or else (Is_Entity_Name (Nam)
731 and then Ekind (Entity (Nam)) = E_Enumeration_Literal)
733 or else (Nkind (Nam) = N_Type_Conversion
734 and then Is_Tagged_Type (Entity (Subtype_Mark (Nam))))
735 then
736 null;
738 else
739 if Nkind (Nam) = N_Type_Conversion then
740 Error_Msg_N
741 ("renaming of conversion only allowed for tagged types", Nam);
743 else
744 Error_Msg_N ("expect object name in renaming", Nam);
745 end if;
747 end if;
749 Set_Etype (Id, T2);
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;
757 Set_Renamed_Object (Id, Nam);
758 end Analyze_Object_Renaming;
760 ------------------------------
761 -- Analyze_Package_Renaming --
762 ------------------------------
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;
769 begin
770 if Name (N) = Error then
771 return;
772 end if;
774 -- Apply Text_IO kludge here, since we may be renaming one of
775 -- the children of Text_IO
777 Text_IO_Kludge (Name (N));
779 if Current_Scope /= Standard_Standard then
780 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
781 end if;
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;
791 if Etype (Old_P) = Any_Type then
792 Error_Msg_N
793 ("expect package name in renaming", Name (N));
795 -- Ada0Y (AI-50217): Limited withed packages can not be renamed
797 elsif Ekind (Old_P) = E_Package
798 and then From_With_Type (Old_P)
799 then
800 Error_Msg_N
801 ("limited withed package cannot be renamed", Name (N));
803 elsif Ekind (Old_P) /= E_Package
804 and then not (Ekind (Old_P) = E_Generic_Package
805 and then In_Open_Scopes (Old_P))
806 then
807 if Ekind (Old_P) = E_Generic_Package then
808 Error_Msg_N
809 ("generic package cannot be renamed as a package", Name (N));
810 else
811 Error_Msg_Sloc := Sloc (Old_P);
812 Error_Msg_NE
813 ("expect package name in renaming, found& declared#",
814 Name (N), Old_P);
815 end if;
817 -- Set basic attributes to minimize cascaded errors.
819 Set_Ekind (New_P, E_Package);
820 Set_Etype (New_P, Standard_Void_Type);
822 else
823 -- Entities in the old package are accessible through the
824 -- renaming entity. The simplest implementation is to have
825 -- both packages share the entity list.
827 Set_Ekind (New_P, E_Package);
828 Set_Etype (New_P, Standard_Void_Type);
830 if Present (Renamed_Object (Old_P)) then
831 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
832 else
833 Set_Renamed_Object (New_P, Old_P);
834 end if;
836 Set_Has_Completion (New_P);
838 Set_First_Entity (New_P, First_Entity (Old_P));
839 Set_Last_Entity (New_P, Last_Entity (Old_P));
840 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
841 Check_Library_Unit_Renaming (N, Old_P);
842 Generate_Reference (Old_P, Name (N));
844 -- If this is the renaming declaration of a package instantiation
845 -- within itself, it is the declaration that ends the list of actuals
846 -- for the instantiation. At this point, the subtypes that rename
847 -- the actuals are flagged as generic, to avoid spurious ambiguities
848 -- if the actuals for two distinct formals happen to coincide. If
849 -- the actual is a private type, the subtype has a private completion
850 -- that is flagged in the same fashion.
852 -- Resolution is identical to what is was in the original generic.
853 -- On exit from the generic instance, these are turned into regular
854 -- subtypes again, so they are compatible with types in their class.
856 if not Is_Generic_Instance (Old_P) then
857 return;
858 else
859 Spec := Specification (Unit_Declaration_Node (Old_P));
860 end if;
862 if Nkind (Spec) = N_Package_Specification
863 and then Present (Generic_Parent (Spec))
864 and then Old_P = Current_Scope
865 and then Chars (New_P) = Chars (Generic_Parent (Spec))
866 then
867 declare
868 E : Entity_Id := First_Entity (Old_P);
869 begin
870 while Present (E)
871 and then E /= New_P
872 loop
873 if Is_Type (E)
874 and then Nkind (Parent (E)) = N_Subtype_Declaration
875 then
876 Set_Is_Generic_Actual_Type (E);
878 if Is_Private_Type (E)
879 and then Present (Full_View (E))
880 then
881 Set_Is_Generic_Actual_Type (Full_View (E));
882 end if;
883 end if;
885 Next_Entity (E);
886 end loop;
887 end;
888 end if;
889 end if;
891 end Analyze_Package_Renaming;
893 -------------------------------
894 -- Analyze_Renamed_Character --
895 -------------------------------
897 procedure Analyze_Renamed_Character
898 (N : Node_Id;
899 New_S : Entity_Id;
900 Is_Body : Boolean)
902 C : constant Node_Id := Name (N);
904 begin
905 if Ekind (New_S) = E_Function then
906 Resolve (C, Etype (New_S));
908 if Is_Body then
909 Check_Frozen_Renaming (N, New_S);
910 end if;
912 else
913 Error_Msg_N ("character literal can only be renamed as function", N);
914 end if;
915 end Analyze_Renamed_Character;
917 ---------------------------------
918 -- Analyze_Renamed_Dereference --
919 ---------------------------------
921 procedure Analyze_Renamed_Dereference
922 (N : Node_Id;
923 New_S : Entity_Id;
924 Is_Body : Boolean)
926 Nam : constant Node_Id := Name (N);
927 P : constant Node_Id := Prefix (Nam);
928 Typ : Entity_Id;
929 Ind : Interp_Index;
930 It : Interp;
932 begin
933 if not Is_Overloaded (P) then
934 if Ekind (Etype (Nam)) /= E_Subprogram_Type
935 or else not Type_Conformant (Etype (Nam), New_S) then
936 Error_Msg_N ("designated type does not match specification", P);
937 else
938 Resolve (P);
939 end if;
941 return;
943 else
944 Typ := Any_Type;
945 Get_First_Interp (Nam, Ind, It);
947 while Present (It.Nam) loop
949 if Ekind (It.Nam) = E_Subprogram_Type
950 and then Type_Conformant (It.Nam, New_S) then
952 if Typ /= Any_Id then
953 Error_Msg_N ("ambiguous renaming", P);
954 return;
955 else
956 Typ := It.Nam;
957 end if;
958 end if;
960 Get_Next_Interp (Ind, It);
961 end loop;
963 if Typ = Any_Type then
964 Error_Msg_N ("designated type does not match specification", P);
965 else
966 Resolve (N, Typ);
968 if Is_Body then
969 Check_Frozen_Renaming (N, New_S);
970 end if;
971 end if;
972 end if;
973 end Analyze_Renamed_Dereference;
975 ---------------------------
976 -- Analyze_Renamed_Entry --
977 ---------------------------
979 procedure Analyze_Renamed_Entry
980 (N : Node_Id;
981 New_S : Entity_Id;
982 Is_Body : Boolean)
984 Nam : constant Node_Id := Name (N);
985 Sel : constant Node_Id := Selector_Name (Nam);
986 Old_S : Entity_Id;
988 begin
989 if Entity (Sel) = Any_Id then
991 -- Selector is undefined on prefix. Error emitted already.
993 Set_Has_Completion (New_S);
994 return;
995 end if;
997 -- Otherwise, find renamed entity, and build body of New_S as a call
998 -- to it.
1000 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1002 if Old_S = Any_Id then
1003 Error_Msg_N (" no subprogram or entry matches specification", N);
1004 else
1005 if Is_Body then
1006 Check_Subtype_Conformant (New_S, Old_S, N);
1007 Generate_Reference (New_S, Defining_Entity (N), 'b');
1008 Style.Check_Identifier (Defining_Entity (N), New_S);
1009 end if;
1011 Inherit_Renamed_Profile (New_S, Old_S);
1012 end if;
1014 Set_Convention (New_S, Convention (Old_S));
1015 Set_Has_Completion (New_S, Inside_A_Generic);
1017 if Is_Body then
1018 Check_Frozen_Renaming (N, New_S);
1019 end if;
1020 end Analyze_Renamed_Entry;
1022 -----------------------------------
1023 -- Analyze_Renamed_Family_Member --
1024 -----------------------------------
1026 procedure Analyze_Renamed_Family_Member
1027 (N : Node_Id;
1028 New_S : Entity_Id;
1029 Is_Body : Boolean)
1031 Nam : constant Node_Id := Name (N);
1032 P : constant Node_Id := Prefix (Nam);
1033 Old_S : Entity_Id;
1035 begin
1036 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1037 or else (Nkind (P) = N_Selected_Component
1038 and then
1039 Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1040 then
1041 if Is_Entity_Name (P) then
1042 Old_S := Entity (P);
1043 else
1044 Old_S := Entity (Selector_Name (P));
1045 end if;
1047 if not Entity_Matches_Spec (Old_S, New_S) then
1048 Error_Msg_N ("entry family does not match specification", N);
1050 elsif Is_Body then
1051 Check_Subtype_Conformant (New_S, Old_S, N);
1052 Generate_Reference (New_S, Defining_Entity (N), 'b');
1053 Style.Check_Identifier (Defining_Entity (N), New_S);
1054 end if;
1055 else
1056 Error_Msg_N ("no entry family matches specification", N);
1057 end if;
1059 Set_Has_Completion (New_S, Inside_A_Generic);
1061 if Is_Body then
1062 Check_Frozen_Renaming (N, New_S);
1063 end if;
1064 end Analyze_Renamed_Family_Member;
1066 ---------------------------------
1067 -- Analyze_Subprogram_Renaming --
1068 ---------------------------------
1070 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1071 Spec : constant Node_Id := Specification (N);
1072 Save_83 : constant Boolean := Ada_83;
1073 Nam : constant Node_Id := Name (N);
1074 New_S : Entity_Id;
1075 Old_S : Entity_Id := Empty;
1076 Rename_Spec : Entity_Id;
1077 Is_Actual : Boolean := False;
1078 Inst_Node : Node_Id := Empty;
1080 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
1081 -- Find renamed entity when the declaration is a renaming_as_body
1082 -- and the renamed entity may itself be a renaming_as_body. Used to
1083 -- enforce rule that a renaming_as_body is illegal if the declaration
1084 -- occurs before the subprogram it completes is frozen, and renaming
1085 -- indirectly renames the subprogram itself.(Defect Report 8652/0027).
1087 -------------------------
1088 -- Original_Subprogram --
1089 -------------------------
1091 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
1092 Orig_Decl : Node_Id;
1093 Orig_Subp : Entity_Id;
1095 begin
1096 -- First case: renamed entity is itself a renaming
1098 if Present (Alias (Subp)) then
1099 return Alias (Subp);
1101 elsif
1102 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1103 and then Present
1104 (Corresponding_Body (Unit_Declaration_Node (Subp)))
1105 then
1106 -- Check if renamed entity is a renaming_as_body
1108 Orig_Decl :=
1109 Unit_Declaration_Node
1110 (Corresponding_Body (Unit_Declaration_Node (Subp)));
1112 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
1113 Orig_Subp := Entity (Name (Orig_Decl));
1115 if Orig_Subp = Rename_Spec then
1117 -- Circularity detected.
1119 return Orig_Subp;
1121 else
1122 return (Original_Subprogram (Orig_Subp));
1123 end if;
1124 else
1125 return Subp;
1126 end if;
1127 else
1128 return Subp;
1129 end if;
1130 end Original_Subprogram;
1132 -- Start of processing for Analyze_Subprogram_Renaming
1134 begin
1135 -- We must test for the attribute renaming case before the Analyze
1136 -- call because otherwise Sem_Attr will complain that the attribute
1137 -- is missing an argument when it is analyzed.
1139 if Nkind (Nam) = N_Attribute_Reference then
1140 Attribute_Renaming (N);
1141 return;
1142 end if;
1144 -- Check whether this declaration corresponds to the instantiation
1145 -- of a formal subprogram. This is indicated by the presence of a
1146 -- Corresponding_Spec that is the instantiation declaration.
1148 -- If this is an instantiation, the corresponding actual is frozen
1149 -- and error messages can be made more precise. If this is a default
1150 -- subprogram, the entity is already established in the generic, and
1151 -- is not retrieved by visibility. If it is a default with a box, the
1152 -- candidate interpretations, if any, have been collected when building
1153 -- the renaming declaration. If overloaded, the proper interpretation
1154 -- is determined in Find_Renamed_Entity. If the entity is an operator,
1155 -- Find_Renamed_Entity applies additional visibility checks.
1157 if Present (Corresponding_Spec (N)) then
1158 Is_Actual := True;
1159 Inst_Node := Unit_Declaration_Node (Corresponding_Spec (N));
1161 if Is_Entity_Name (Nam)
1162 and then Present (Entity (Nam))
1163 and then not Comes_From_Source (Nam)
1164 and then not Is_Overloaded (Nam)
1165 then
1166 Old_S := Entity (Nam);
1167 New_S := Analyze_Subprogram_Specification (Spec);
1169 if Ekind (Entity (Nam)) = E_Operator
1170 and then Box_Present (Inst_Node)
1171 then
1172 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
1173 end if;
1175 else
1176 Analyze (Nam);
1177 New_S := Analyze_Subprogram_Specification (Spec);
1178 end if;
1180 Set_Corresponding_Spec (N, Empty);
1182 else
1183 -- Renamed entity must be analyzed first, to avoid being hidden by
1184 -- new name (which might be the same in a generic instance).
1186 Analyze (Nam);
1188 -- The renaming defines a new overloaded entity, which is analyzed
1189 -- like a subprogram declaration.
1191 New_S := Analyze_Subprogram_Specification (Spec);
1192 end if;
1194 if Current_Scope /= Standard_Standard then
1195 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
1196 end if;
1198 Rename_Spec := Find_Corresponding_Spec (N);
1200 if Present (Rename_Spec) then
1202 -- Renaming_As_Body. Renaming declaration is the completion of
1203 -- the declaration of Rename_Spec. We will build an actual body
1204 -- for it at the freezing point.
1206 Set_Corresponding_Spec (N, Rename_Spec);
1207 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
1209 -- The body is created when the entity is frozen. If the context
1210 -- is generic, freeze_all is not invoked, so we need to indicate
1211 -- that the entity has a completion.
1213 Set_Has_Completion (Rename_Spec, Inside_A_Generic);
1215 if Ada_83 and then Comes_From_Source (N) then
1216 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
1217 end if;
1219 Set_Convention (New_S, Convention (Rename_Spec));
1220 Check_Fully_Conformant (New_S, Rename_Spec);
1221 Set_Public_Status (New_S);
1223 -- Indicate that the entity in the declaration functions like
1224 -- the corresponding body, and is not a new entity.
1226 Set_Ekind (New_S, E_Subprogram_Body);
1227 New_S := Rename_Spec;
1229 else
1230 Generate_Definition (New_S);
1231 New_Overloaded_Entity (New_S);
1232 if Is_Entity_Name (Nam)
1233 and then Is_Intrinsic_Subprogram (Entity (Nam))
1234 then
1235 null;
1236 else
1237 Check_Delayed_Subprogram (New_S);
1238 end if;
1239 end if;
1241 -- There is no need for elaboration checks on the new entity, which
1242 -- may be called before the next freezing point where the body will
1243 -- appear.
1245 Set_Kill_Elaboration_Checks (New_S, True);
1247 if Etype (Nam) = Any_Type then
1248 Set_Has_Completion (New_S);
1249 return;
1251 elsif Nkind (Nam) = N_Selected_Component then
1253 -- Renamed entity is an entry or protected subprogram. For those
1254 -- cases an explicit body is built (at the point of freezing of
1255 -- this entity) that contains a call to the renamed entity.
1257 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
1258 return;
1260 elsif Nkind (Nam) = N_Explicit_Dereference then
1262 -- Renamed entity is designated by access_to_subprogram expression.
1263 -- Must build body to encapsulate call, as in the entry case.
1265 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
1266 return;
1268 elsif Nkind (Nam) = N_Indexed_Component then
1269 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
1270 return;
1272 elsif Nkind (Nam) = N_Character_Literal then
1273 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
1274 return;
1276 elsif (not Is_Entity_Name (Nam)
1277 and then Nkind (Nam) /= N_Operator_Symbol)
1278 or else not Is_Overloadable (Entity (Nam))
1279 then
1280 Error_Msg_N ("expect valid subprogram name in renaming", N);
1281 return;
1283 end if;
1285 -- Most common case: subprogram renames subprogram. No body is
1286 -- generated in this case, so we must indicate that the declaration
1287 -- is complete as is.
1289 if No (Rename_Spec) then
1290 Set_Has_Completion (New_S);
1291 end if;
1293 -- Find the renamed entity that matches the given specification.
1294 -- Disable Ada_83 because there is no requirement of full conformance
1295 -- between renamed entity and new entity, even though the same circuit
1296 -- is used.
1298 Ada_83 := False;
1300 if No (Old_S) then
1301 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
1302 end if;
1304 if Old_S /= Any_Id then
1306 if Is_Actual
1307 and then Box_Present (Inst_Node)
1308 then
1309 -- This is an implicit reference to the default actual
1311 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
1312 else
1313 Generate_Reference (Old_S, Nam);
1314 end if;
1316 -- For a renaming-as-body, require subtype conformance,
1317 -- but if the declaration being completed has not been
1318 -- frozen, then inherit the convention of the renamed
1319 -- subprogram prior to checking conformance (unless the
1320 -- renaming has an explicit convention established; the
1321 -- rule stated in the RM doesn't seem to address this ???).
1323 if Present (Rename_Spec) then
1324 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
1325 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
1327 if not Is_Frozen (Rename_Spec) then
1328 if not Has_Convention_Pragma (Rename_Spec) then
1329 Set_Convention (New_S, Convention (Old_S));
1330 end if;
1332 if Ekind (Old_S) /= E_Operator then
1333 Check_Mode_Conformant (New_S, Old_S, Spec);
1334 end if;
1336 if Original_Subprogram (Old_S) = Rename_Spec then
1337 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
1338 end if;
1339 else
1340 Check_Subtype_Conformant (New_S, Old_S, Spec);
1341 end if;
1343 Check_Frozen_Renaming (N, Rename_Spec);
1345 elsif Ekind (Old_S) /= E_Operator then
1346 Check_Mode_Conformant (New_S, Old_S);
1348 if Is_Actual
1349 and then Error_Posted (New_S)
1350 then
1351 Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
1352 end if;
1353 end if;
1355 if No (Rename_Spec) then
1357 -- The parameter profile of the new entity is that of the renamed
1358 -- entity: the subtypes given in the specification are irrelevant.
1360 Inherit_Renamed_Profile (New_S, Old_S);
1362 -- A call to the subprogram is transformed into a call to the
1363 -- renamed entity. This is transitive if the renamed entity is
1364 -- itself a renaming.
1366 if Present (Alias (Old_S)) then
1367 Set_Alias (New_S, Alias (Old_S));
1368 else
1369 Set_Alias (New_S, Old_S);
1370 end if;
1372 -- Note that we do not set Is_Instrinsic_Subprogram if we have
1373 -- a renaming as body, since the entity in this case is not an
1374 -- intrinsic (it calls an intrinsic, but we have a real body
1375 -- for this call, and it is in this body that the required
1376 -- intrinsic processing will take place).
1378 -- Also, if this is a renaming of inequality, the renamed
1379 -- operator is intrinsic, but what matters is the corresponding
1380 -- equality operator, which may be user-defined.
1382 Set_Is_Intrinsic_Subprogram
1383 (New_S,
1384 Is_Intrinsic_Subprogram (Old_S)
1385 and then
1386 (Chars (Old_S) /= Name_Op_Ne
1387 or else Ekind (Old_S) = E_Operator
1388 or else
1389 Is_Intrinsic_Subprogram
1390 (Corresponding_Equality (Old_S))));
1392 if Ekind (Alias (New_S)) = E_Operator then
1393 Set_Has_Delayed_Freeze (New_S, False);
1394 end if;
1396 end if;
1398 if not Is_Actual
1399 and then (Old_S = New_S
1400 or else (Nkind (Nam) /= N_Expanded_Name
1401 and then Chars (Old_S) = Chars (New_S)))
1402 then
1403 Error_Msg_N ("subprogram cannot rename itself", N);
1404 end if;
1406 Set_Convention (New_S, Convention (Old_S));
1407 Set_Is_Abstract (New_S, Is_Abstract (Old_S));
1408 Check_Library_Unit_Renaming (N, Old_S);
1410 -- Pathological case: procedure renames entry in the scope of
1411 -- its task. Entry is given by simple name, but body must be built
1412 -- for procedure. Of course if called it will deadlock.
1414 if Ekind (Old_S) = E_Entry then
1415 Set_Has_Completion (New_S, False);
1416 Set_Alias (New_S, Empty);
1417 end if;
1419 if Is_Actual then
1420 Freeze_Before (N, Old_S);
1421 Set_Has_Delayed_Freeze (New_S, False);
1422 Freeze_Before (N, New_S);
1424 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
1425 and then Is_Abstract (Old_S)
1426 then
1427 Error_Msg_N
1428 ("abstract subprogram not allowed as generic actual", Nam);
1429 end if;
1430 end if;
1432 else
1433 -- A common error is to assume that implicit operators for types
1434 -- are defined in Standard, or in the scope of a subtype. In those
1435 -- cases where the renamed entity is given with an expanded name,
1436 -- it is worth mentioning that operators for the type are not
1437 -- declared in the scope given by the prefix.
1439 if Nkind (Nam) = N_Expanded_Name
1440 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
1441 and then Scope (Entity (Nam)) = Standard_Standard
1442 then
1443 declare
1444 T : constant Entity_Id :=
1445 Base_Type (Etype (First_Formal (New_S)));
1447 begin
1448 Error_Msg_Node_2 := Prefix (Nam);
1449 Error_Msg_NE
1450 ("operator for type& is not declared in&", Prefix (Nam), T);
1451 end;
1453 else
1454 Error_Msg_NE
1455 ("no visible subprogram matches the specification for&",
1456 Spec, New_S);
1457 end if;
1459 if Present (Candidate_Renaming) then
1460 declare
1461 F1 : Entity_Id;
1462 F2 : Entity_Id;
1464 begin
1465 F1 := First_Formal (Candidate_Renaming);
1466 F2 := First_Formal (New_S);
1468 while Present (F1) and then Present (F2) loop
1469 Next_Formal (F1);
1470 Next_Formal (F2);
1471 end loop;
1473 if Present (F1) and then Present (Default_Value (F1)) then
1474 if Present (Next_Formal (F1)) then
1475 Error_Msg_NE
1476 ("\missing specification for &" &
1477 " and other formals with defaults", Spec, F1);
1478 else
1479 Error_Msg_NE
1480 ("\missing specification for &", Spec, F1);
1481 end if;
1482 end if;
1483 end;
1484 end if;
1485 end if;
1487 Ada_83 := Save_83;
1488 end Analyze_Subprogram_Renaming;
1490 -------------------------
1491 -- Analyze_Use_Package --
1492 -------------------------
1494 -- Resolve the package names in the use clause, and make all the visible
1495 -- entities defined in the package potentially use-visible. If the package
1496 -- is already in use from a previous use clause, its visible entities are
1497 -- already use-visible. In that case, mark the occurrence as a redundant
1498 -- use. If the package is an open scope, i.e. if the use clause occurs
1499 -- within the package itself, ignore it.
1501 procedure Analyze_Use_Package (N : Node_Id) is
1502 Pack_Name : Node_Id;
1503 Pack : Entity_Id;
1505 -- Start of processing for Analyze_Use_Package
1507 begin
1508 Set_Hidden_By_Use_Clause (N, No_Elist);
1510 -- Use clause is not allowed in a spec of a predefined package
1511 -- declaration except that packages whose file name starts a-n
1512 -- are OK (these are children of Ada.Numerics, and such packages
1513 -- are never loaded by Rtsfind).
1515 if Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
1516 and then Name_Buffer (1 .. 3) /= "a-n"
1517 and then
1518 Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
1519 then
1520 Error_Msg_N ("use clause not allowed in predefined spec", N);
1521 end if;
1523 -- Chain clause to list of use clauses in current scope.
1525 if Nkind (Parent (N)) /= N_Compilation_Unit then
1526 Chain_Use_Clause (N);
1527 end if;
1529 -- Loop through package names to identify referenced packages
1531 Pack_Name := First (Names (N));
1533 while Present (Pack_Name) loop
1534 Analyze (Pack_Name);
1536 if Nkind (Parent (N)) = N_Compilation_Unit
1537 and then Nkind (Pack_Name) = N_Expanded_Name
1538 then
1539 declare
1540 Pref : Node_Id := Prefix (Pack_Name);
1542 begin
1543 while Nkind (Pref) = N_Expanded_Name loop
1544 Pref := Prefix (Pref);
1545 end loop;
1547 if Entity (Pref) = Standard_Standard then
1548 Error_Msg_N
1549 ("predefined package Standard cannot appear"
1550 & " in a context clause", Pref);
1551 end if;
1552 end;
1553 end if;
1555 Next (Pack_Name);
1556 end loop;
1558 -- Loop through package names to mark all entities as potentially
1559 -- use visible.
1561 Pack_Name := First (Names (N));
1563 while Present (Pack_Name) loop
1565 if Is_Entity_Name (Pack_Name) then
1566 Pack := Entity (Pack_Name);
1568 if Ekind (Pack) /= E_Package
1569 and then Etype (Pack) /= Any_Type
1570 then
1571 if Ekind (Pack) = E_Generic_Package then
1572 Error_Msg_N
1573 ("a generic package is not allowed in a use clause",
1574 Pack_Name);
1575 else
1576 Error_Msg_N ("& is not a usable package", Pack_Name);
1577 end if;
1579 else
1580 if Nkind (Parent (N)) = N_Compilation_Unit then
1581 Check_In_Previous_With_Clause (N, Pack_Name);
1582 end if;
1584 if Applicable_Use (Pack_Name) then
1585 Use_One_Package (Pack, N);
1586 end if;
1587 end if;
1588 end if;
1590 Next (Pack_Name);
1591 end loop;
1593 end Analyze_Use_Package;
1595 ----------------------
1596 -- Analyze_Use_Type --
1597 ----------------------
1599 procedure Analyze_Use_Type (N : Node_Id) is
1600 Id : Entity_Id;
1602 begin
1603 Set_Hidden_By_Use_Clause (N, No_Elist);
1605 -- Chain clause to list of use clauses in current scope.
1607 if Nkind (Parent (N)) /= N_Compilation_Unit then
1608 Chain_Use_Clause (N);
1609 end if;
1611 Id := First (Subtype_Marks (N));
1613 while Present (Id) loop
1614 Find_Type (Id);
1616 if Entity (Id) /= Any_Type then
1617 Use_One_Type (Id);
1619 if Nkind (Parent (N)) = N_Compilation_Unit then
1620 if Nkind (Id) = N_Identifier then
1621 Error_Msg_N ("Type is not directly visible", Id);
1623 elsif Is_Child_Unit (Scope (Entity (Id)))
1624 and then Scope (Entity (Id)) /= System_Aux_Id
1625 then
1626 Check_In_Previous_With_Clause (N, Prefix (Id));
1627 end if;
1628 end if;
1629 end if;
1631 Next (Id);
1632 end loop;
1633 end Analyze_Use_Type;
1635 --------------------
1636 -- Applicable_Use --
1637 --------------------
1639 function Applicable_Use (Pack_Name : Node_Id) return Boolean is
1640 Pack : constant Entity_Id := Entity (Pack_Name);
1642 begin
1643 if In_Open_Scopes (Pack) then
1644 return False;
1646 elsif In_Use (Pack) then
1647 Set_Redundant_Use (Pack_Name, True);
1648 return False;
1650 elsif Present (Renamed_Object (Pack))
1651 and then In_Use (Renamed_Object (Pack))
1652 then
1653 Set_Redundant_Use (Pack_Name, True);
1654 return False;
1656 else
1657 return True;
1658 end if;
1659 end Applicable_Use;
1661 ------------------------
1662 -- Attribute_Renaming --
1663 ------------------------
1665 procedure Attribute_Renaming (N : Node_Id) is
1666 Loc : constant Source_Ptr := Sloc (N);
1667 Nam : constant Node_Id := Name (N);
1668 Spec : constant Node_Id := Specification (N);
1669 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
1670 Aname : constant Name_Id := Attribute_Name (Nam);
1672 Form_Num : Nat := 0;
1673 Expr_List : List_Id := No_List;
1675 Attr_Node : Node_Id;
1676 Body_Node : Node_Id;
1677 Param_Spec : Node_Id;
1679 begin
1680 Generate_Definition (New_S);
1682 -- This procedure is called in the context of subprogram renaming,
1683 -- and thus the attribute must be one that is a subprogram. All of
1684 -- those have at least one formal parameter, with the singular
1685 -- exception of AST_Entry (which is a real oddity, it is odd that
1686 -- this can be renamed at all!)
1688 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
1689 if Aname /= Name_AST_Entry then
1690 Error_Msg_N
1691 ("subprogram renaming an attribute must have formals", N);
1692 return;
1693 end if;
1695 else
1696 Param_Spec := First (Parameter_Specifications (Spec));
1698 while Present (Param_Spec) loop
1699 Form_Num := Form_Num + 1;
1701 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
1702 Find_Type (Parameter_Type (Param_Spec));
1704 -- The profile of the new entity denotes the base type (s) of
1705 -- the types given in the specification. For access parameters
1706 -- there are no subtypes involved.
1708 Rewrite (Parameter_Type (Param_Spec),
1709 New_Reference_To
1710 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
1711 end if;
1713 if No (Expr_List) then
1714 Expr_List := New_List;
1715 end if;
1717 Append_To (Expr_List,
1718 Make_Identifier (Loc,
1719 Chars => Chars (Defining_Identifier (Param_Spec))));
1721 -- The expressions in the attribute reference are not freeze
1722 -- points. Neither is the attribute as a whole, see below.
1724 Set_Must_Not_Freeze (Last (Expr_List));
1725 Next (Param_Spec);
1726 end loop;
1727 end if;
1729 -- Immediate error if too many formals. Other mismatches in numbers
1730 -- of number of types of parameters are detected when we analyze the
1731 -- body of the subprogram that we construct.
1733 if Form_Num > 2 then
1734 Error_Msg_N ("too many formals for attribute", N);
1736 elsif
1737 Aname = Name_Compose or else
1738 Aname = Name_Exponent or else
1739 Aname = Name_Leading_Part or else
1740 Aname = Name_Pos or else
1741 Aname = Name_Round or else
1742 Aname = Name_Scaling or else
1743 Aname = Name_Val
1744 then
1745 if Nkind (N) = N_Subprogram_Renaming_Declaration
1746 and then Present (Corresponding_Spec (N))
1747 and then Nkind (Unit_Declaration_Node (Corresponding_Spec (N))) =
1748 N_Formal_Subprogram_Declaration
1749 then
1750 Error_Msg_N
1751 ("generic actual cannot be attribute involving universal type",
1752 Nam);
1753 else
1754 Error_Msg_N
1755 ("attribute involving a universal type cannot be renamed",
1756 Nam);
1757 end if;
1758 end if;
1760 -- AST_Entry is an odd case. It doesn't really make much sense to
1761 -- allow it to be renamed, but that's the DEC rule, so we have to
1762 -- do it right. The point is that the AST_Entry call should be made
1763 -- now, and what the function will return is the returned value.
1765 -- Note that there is no Expr_List in this case anyway
1767 if Aname = Name_AST_Entry then
1769 declare
1770 Ent : Entity_Id;
1771 Decl : Node_Id;
1773 begin
1774 Ent := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
1776 Decl :=
1777 Make_Object_Declaration (Loc,
1778 Defining_Identifier => Ent,
1779 Object_Definition =>
1780 New_Occurrence_Of (RTE (RE_AST_Handler), Loc),
1781 Expression => Nam,
1782 Constant_Present => True);
1784 Set_Assignment_OK (Decl, True);
1785 Insert_Action (N, Decl);
1786 Attr_Node := Make_Identifier (Loc, Chars (Ent));
1787 end;
1789 -- For all other attributes, we rewrite the attribute node to have
1790 -- a list of expressions corresponding to the subprogram formals.
1791 -- A renaming declaration is not a freeze point, and the analysis of
1792 -- the attribute reference should not freeze the type of the prefix.
1794 else
1795 Attr_Node :=
1796 Make_Attribute_Reference (Loc,
1797 Prefix => Prefix (Nam),
1798 Attribute_Name => Aname,
1799 Expressions => Expr_List);
1801 Set_Must_Not_Freeze (Attr_Node);
1802 Set_Must_Not_Freeze (Prefix (Nam));
1803 end if;
1805 -- Case of renaming a function
1807 if Nkind (Spec) = N_Function_Specification then
1809 if Is_Procedure_Attribute_Name (Aname) then
1810 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
1811 return;
1812 end if;
1814 Find_Type (Subtype_Mark (Spec));
1815 Rewrite (Subtype_Mark (Spec),
1816 New_Reference_To (Base_Type (Entity (Subtype_Mark (Spec))), Loc));
1818 Body_Node :=
1819 Make_Subprogram_Body (Loc,
1820 Specification => Spec,
1821 Declarations => New_List,
1822 Handled_Statement_Sequence =>
1823 Make_Handled_Sequence_Of_Statements (Loc,
1824 Statements => New_List (
1825 Make_Return_Statement (Loc,
1826 Expression => Attr_Node))));
1828 -- Case of renaming a procedure
1830 else
1831 if not Is_Procedure_Attribute_Name (Aname) then
1832 Error_Msg_N ("attribute can only be renamed as function", Nam);
1833 return;
1834 end if;
1836 Body_Node :=
1837 Make_Subprogram_Body (Loc,
1838 Specification => Spec,
1839 Declarations => New_List,
1840 Handled_Statement_Sequence =>
1841 Make_Handled_Sequence_Of_Statements (Loc,
1842 Statements => New_List (Attr_Node)));
1843 end if;
1845 Rewrite (N, Body_Node);
1846 Analyze (N);
1848 Set_Etype (New_S, Base_Type (Etype (New_S)));
1850 -- We suppress elaboration warnings for the resulting entity, since
1851 -- clearly they are not needed, and more particularly, in the case
1852 -- of a generic formal subprogram, the resulting entity can appear
1853 -- after the instantiation itself, and thus look like a bogus case
1854 -- of access before elaboration.
1856 Set_Suppress_Elaboration_Warnings (New_S);
1858 end Attribute_Renaming;
1860 ----------------------
1861 -- Chain_Use_Clause --
1862 ----------------------
1864 procedure Chain_Use_Clause (N : Node_Id) is
1865 begin
1866 Set_Next_Use_Clause (N,
1867 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause);
1868 Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause := N;
1869 end Chain_Use_Clause;
1871 ----------------------------
1872 -- Check_Frozen_Renaming --
1873 ----------------------------
1875 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
1876 B_Node : Node_Id;
1877 Old_S : Entity_Id;
1879 begin
1880 if Is_Frozen (Subp)
1881 and then not Has_Completion (Subp)
1882 then
1883 B_Node :=
1884 Build_Renamed_Body
1885 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
1887 if Is_Entity_Name (Name (N)) then
1888 Old_S := Entity (Name (N));
1890 if not Is_Frozen (Old_S)
1891 and then Operating_Mode /= Check_Semantics
1892 then
1893 Append_Freeze_Action (Old_S, B_Node);
1894 else
1895 Insert_After (N, B_Node);
1896 Analyze (B_Node);
1897 end if;
1899 if Is_Intrinsic_Subprogram (Old_S)
1900 and then not In_Instance
1901 then
1902 Error_Msg_N
1903 ("subprogram used in renaming_as_body cannot be intrinsic",
1904 Name (N));
1905 end if;
1907 else
1908 Insert_After (N, B_Node);
1909 Analyze (B_Node);
1910 end if;
1911 end if;
1912 end Check_Frozen_Renaming;
1914 -----------------------------------
1915 -- Check_In_Previous_With_Clause --
1916 -----------------------------------
1918 procedure Check_In_Previous_With_Clause
1919 (N : Node_Id;
1920 Nam : Entity_Id)
1922 Pack : constant Entity_Id := Entity (Original_Node (Nam));
1923 Item : Node_Id;
1924 Par : Node_Id;
1926 begin
1927 Item := First (Context_Items (Parent (N)));
1929 while Present (Item)
1930 and then Item /= N
1931 loop
1932 if Nkind (Item) = N_With_Clause
1933 and then Entity (Name (Item)) = Pack
1934 then
1935 Par := Nam;
1937 -- Find root library unit in with_clause.
1939 while Nkind (Par) = N_Expanded_Name loop
1940 Par := Prefix (Par);
1941 end loop;
1943 if Is_Child_Unit (Entity (Original_Node (Par))) then
1944 Error_Msg_NE
1945 ("& is not directly visible", Par, Entity (Par));
1946 else
1947 return;
1948 end if;
1949 end if;
1951 Next (Item);
1952 end loop;
1954 -- On exit, package is not mentioned in a previous with_clause.
1955 -- Check if its prefix is.
1957 if Nkind (Nam) = N_Expanded_Name then
1958 Check_In_Previous_With_Clause (N, Prefix (Nam));
1960 elsif Pack /= Any_Id then
1961 Error_Msg_NE ("& is not visible", Nam, Pack);
1962 end if;
1963 end Check_In_Previous_With_Clause;
1965 ---------------------------------
1966 -- Check_Library_Unit_Renaming --
1967 ---------------------------------
1969 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
1970 New_E : Entity_Id;
1972 begin
1973 if Nkind (Parent (N)) /= N_Compilation_Unit then
1974 return;
1976 elsif Scope (Old_E) /= Standard_Standard
1977 and then not Is_Child_Unit (Old_E)
1978 then
1979 Error_Msg_N ("renamed unit must be a library unit", Name (N));
1981 elsif Present (Parent_Spec (N))
1982 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
1983 and then not Is_Child_Unit (Old_E)
1984 then
1985 Error_Msg_N
1986 ("renamed unit must be a child unit of generic parent", Name (N));
1988 elsif Nkind (N) in N_Generic_Renaming_Declaration
1989 and then Nkind (Name (N)) = N_Expanded_Name
1990 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
1991 and then Is_Generic_Unit (Old_E)
1992 then
1993 Error_Msg_N
1994 ("renamed generic unit must be a library unit", Name (N));
1996 elsif Ekind (Old_E) = E_Package
1997 or else Ekind (Old_E) = E_Generic_Package
1998 then
1999 -- Inherit categorization flags
2001 New_E := Defining_Entity (N);
2002 Set_Is_Pure (New_E, Is_Pure (Old_E));
2003 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
2004 Set_Is_Remote_Call_Interface (New_E,
2005 Is_Remote_Call_Interface (Old_E));
2006 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
2007 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
2008 end if;
2009 end Check_Library_Unit_Renaming;
2011 ---------------
2012 -- End_Scope --
2013 ---------------
2015 procedure End_Scope is
2016 Id : Entity_Id;
2017 Prev : Entity_Id;
2018 Outer : Entity_Id;
2020 begin
2021 Id := First_Entity (Current_Scope);
2023 while Present (Id) loop
2024 -- An entity in the current scope is not necessarily the first one
2025 -- on its homonym chain. Find its predecessor if any,
2026 -- If it is an internal entity, it will not be in the visibility
2027 -- chain altogether, and there is nothing to unchain.
2029 if Id /= Current_Entity (Id) then
2030 Prev := Current_Entity (Id);
2031 while Present (Prev)
2032 and then Present (Homonym (Prev))
2033 and then Homonym (Prev) /= Id
2034 loop
2035 Prev := Homonym (Prev);
2036 end loop;
2038 -- Skip to end of loop if Id is not in the visibility chain
2040 if No (Prev) or else Homonym (Prev) /= Id then
2041 goto Next_Ent;
2042 end if;
2044 else
2045 Prev := Empty;
2046 end if;
2048 Outer := Homonym (Id);
2049 Set_Is_Immediately_Visible (Id, False);
2051 while Present (Outer) and then Scope (Outer) = Current_Scope loop
2052 Outer := Homonym (Outer);
2053 end loop;
2055 -- Reset homonym link of other entities, but do not modify link
2056 -- between entities in current scope, so that the back-end can have
2057 -- a proper count of local overloadings.
2059 if No (Prev) then
2060 Set_Name_Entity_Id (Chars (Id), Outer);
2062 elsif Scope (Prev) /= Scope (Id) then
2063 Set_Homonym (Prev, Outer);
2064 end if;
2066 <<Next_Ent>>
2067 Next_Entity (Id);
2068 end loop;
2070 -- If the scope generated freeze actions, place them before the
2071 -- current declaration and analyze them. Type declarations and
2072 -- the bodies of initialization procedures can generate such nodes.
2073 -- We follow the parent chain until we reach a list node, which is
2074 -- the enclosing list of declarations. If the list appears within
2075 -- a protected definition, move freeze nodes outside the protected
2076 -- type altogether.
2078 if Present
2079 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
2080 then
2081 declare
2082 Decl : Node_Id;
2083 L : constant List_Id := Scope_Stack.Table
2084 (Scope_Stack.Last).Pending_Freeze_Actions;
2086 begin
2087 if Is_Itype (Current_Scope) then
2088 Decl := Associated_Node_For_Itype (Current_Scope);
2089 else
2090 Decl := Parent (Current_Scope);
2091 end if;
2093 Pop_Scope;
2095 while not (Is_List_Member (Decl))
2096 or else Nkind (Parent (Decl)) = N_Protected_Definition
2097 or else Nkind (Parent (Decl)) = N_Task_Definition
2098 loop
2099 Decl := Parent (Decl);
2100 end loop;
2102 Insert_List_Before_And_Analyze (Decl, L);
2103 end;
2105 else
2106 Pop_Scope;
2107 end if;
2109 end End_Scope;
2111 ---------------------
2112 -- End_Use_Clauses --
2113 ---------------------
2115 procedure End_Use_Clauses (Clause : Node_Id) is
2116 U : Node_Id;
2118 begin
2119 -- Remove Use_Type clauses first, because they affect the
2120 -- visibility of operators in subsequent used packages.
2122 U := Clause;
2123 while Present (U) loop
2124 if Nkind (U) = N_Use_Type_Clause then
2125 End_Use_Type (U);
2126 end if;
2128 Next_Use_Clause (U);
2129 end loop;
2131 U := Clause;
2132 while Present (U) loop
2133 if Nkind (U) = N_Use_Package_Clause then
2134 End_Use_Package (U);
2135 end if;
2137 Next_Use_Clause (U);
2138 end loop;
2139 end End_Use_Clauses;
2141 ---------------------
2142 -- End_Use_Package --
2143 ---------------------
2145 procedure End_Use_Package (N : Node_Id) is
2146 Pack_Name : Node_Id;
2147 Pack : Entity_Id;
2148 Id : Entity_Id;
2149 Elmt : Elmt_Id;
2151 function Is_Primitive_Operator
2152 (Op : Entity_Id;
2153 F : Entity_Id)
2154 return Boolean;
2155 -- Check whether Op is a primitive operator of a use-visible type
2157 ---------------------------
2158 -- Is_Primitive_Operator --
2159 ---------------------------
2161 function Is_Primitive_Operator
2162 (Op : Entity_Id;
2163 F : Entity_Id)
2164 return Boolean
2166 T : constant Entity_Id := Etype (F);
2168 begin
2169 return In_Use (T)
2170 and then Scope (T) = Scope (Op);
2171 end Is_Primitive_Operator;
2173 -- Start of processing for End_Use_Package
2175 begin
2176 Pack_Name := First (Names (N));
2178 while Present (Pack_Name) loop
2179 Pack := Entity (Pack_Name);
2181 if Ekind (Pack) = E_Package then
2183 if In_Open_Scopes (Pack) then
2184 null;
2186 elsif not Redundant_Use (Pack_Name) then
2187 Set_In_Use (Pack, False);
2188 Id := First_Entity (Pack);
2190 while Present (Id) loop
2192 -- Preserve use-visibility of operators that are primitive
2193 -- operators of a type that is use_visible through an active
2194 -- use_type clause.
2196 if Nkind (Id) = N_Defining_Operator_Symbol
2197 and then
2198 (Is_Primitive_Operator (Id, First_Formal (Id))
2199 or else
2200 (Present (Next_Formal (First_Formal (Id)))
2201 and then
2202 Is_Primitive_Operator
2203 (Id, Next_Formal (First_Formal (Id)))))
2204 then
2205 null;
2207 else
2208 Set_Is_Potentially_Use_Visible (Id, False);
2209 end if;
2211 if Is_Private_Type (Id)
2212 and then Present (Full_View (Id))
2213 then
2214 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
2215 end if;
2217 Next_Entity (Id);
2218 end loop;
2220 if Present (Renamed_Object (Pack)) then
2221 Set_In_Use (Renamed_Object (Pack), False);
2222 end if;
2224 if Chars (Pack) = Name_System
2225 and then Scope (Pack) = Standard_Standard
2226 and then Present_System_Aux
2227 then
2228 Id := First_Entity (System_Aux_Id);
2230 while Present (Id) loop
2231 Set_Is_Potentially_Use_Visible (Id, False);
2233 if Is_Private_Type (Id)
2234 and then Present (Full_View (Id))
2235 then
2236 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
2237 end if;
2239 Next_Entity (Id);
2240 end loop;
2242 Set_In_Use (System_Aux_Id, False);
2243 end if;
2245 else
2246 Set_Redundant_Use (Pack_Name, False);
2247 end if;
2249 end if;
2251 Next (Pack_Name);
2252 end loop;
2254 if Present (Hidden_By_Use_Clause (N)) then
2255 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
2257 while Present (Elmt) loop
2258 Set_Is_Immediately_Visible (Node (Elmt));
2259 Next_Elmt (Elmt);
2260 end loop;
2262 Set_Hidden_By_Use_Clause (N, No_Elist);
2263 end if;
2264 end End_Use_Package;
2266 ------------------
2267 -- End_Use_Type --
2268 ------------------
2270 procedure End_Use_Type (N : Node_Id) is
2271 Id : Entity_Id;
2272 Op_List : Elist_Id;
2273 Elmt : Elmt_Id;
2274 T : Entity_Id;
2276 begin
2277 Id := First (Subtype_Marks (N));
2279 while Present (Id) loop
2281 -- A call to rtsfind may occur while analyzing a use_type clause,
2282 -- in which case the type marks are not resolved yet, and there is
2283 -- nothing to remove.
2285 if not Is_Entity_Name (Id)
2286 or else No (Entity (Id))
2287 then
2288 goto Continue;
2289 end if;
2291 T := Entity (Id);
2293 if T = Any_Type then
2294 null;
2296 -- Note that the use_Type clause may mention a subtype of the
2297 -- type whose primitive operations have been made visible. Here
2298 -- as elsewhere, it is the base type that matters for visibility.
2300 elsif In_Open_Scopes (Scope (Base_Type (T))) then
2301 null;
2303 elsif not Redundant_Use (Id) then
2304 Set_In_Use (T, False);
2305 Set_In_Use (Base_Type (T), False);
2306 Op_List := Collect_Primitive_Operations (T);
2307 Elmt := First_Elmt (Op_List);
2309 while Present (Elmt) loop
2311 if Nkind (Node (Elmt)) = N_Defining_Operator_Symbol then
2312 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
2313 end if;
2315 Next_Elmt (Elmt);
2316 end loop;
2317 end if;
2319 <<Continue>>
2320 Next (Id);
2321 end loop;
2322 end End_Use_Type;
2324 ----------------------
2325 -- Find_Direct_Name --
2326 ----------------------
2328 procedure Find_Direct_Name (N : Node_Id) is
2329 E : Entity_Id;
2330 E2 : Entity_Id;
2331 Msg : Boolean;
2333 Inst : Entity_Id := Empty;
2334 -- Enclosing instance, if any.
2336 Homonyms : Entity_Id;
2337 -- Saves start of homonym chain
2339 Nvis_Entity : Boolean;
2340 -- Set True to indicate that at there is at least one entity on the
2341 -- homonym chain which, while not visible, is visible enough from the
2342 -- user point of view to warrant an error message of "not visible"
2343 -- rather than undefined.
2345 function From_Actual_Package (E : Entity_Id) return Boolean;
2346 -- Returns true if the entity is declared in a package that is
2347 -- an actual for a formal package of the current instance. Such an
2348 -- entity requires special handling because it may be use-visible
2349 -- but hides directly visible entities defined outside the instance.
2351 function Known_But_Invisible (E : Entity_Id) return Boolean;
2352 -- This function determines whether the entity E (which is not
2353 -- visible) can reasonably be considered to be known to the writer
2354 -- of the reference. This is a heuristic test, used only for the
2355 -- purposes of figuring out whether we prefer to complain that an
2356 -- entity is undefined or invisible (and identify the declaration
2357 -- of the invisible entity in the latter case). The point here is
2358 -- that we don't want to complain that something is invisible and
2359 -- then point to something entirely mysterious to the writer.
2361 procedure Nvis_Messages;
2362 -- Called if there are no visible entries for N, but there is at least
2363 -- one non-directly visible, or hidden declaration. This procedure
2364 -- outputs an appropriate set of error messages.
2366 procedure Undefined (Nvis : Boolean);
2367 -- This function is called if the current node has no corresponding
2368 -- visible entity or entities. The value set in Msg indicates whether
2369 -- an error message was generated (multiple error messages for the
2370 -- same variable are generally suppressed, see body for details).
2371 -- Msg is True if an error message was generated, False if not. This
2372 -- value is used by the caller to determine whether or not to output
2373 -- additional messages where appropriate. The parameter is set False
2374 -- to get the message "X is undefined", and True to get the message
2375 -- "X is not visible".
2377 -------------------------
2378 -- From_Actual_Package --
2379 -------------------------
2381 function From_Actual_Package (E : Entity_Id) return Boolean is
2382 Scop : constant Entity_Id := Scope (E);
2383 Act : Entity_Id;
2385 begin
2386 if not In_Instance then
2387 return False;
2388 else
2389 Inst := Current_Scope;
2391 while Present (Inst)
2392 and then Ekind (Inst) /= E_Package
2393 and then not Is_Generic_Instance (Inst)
2394 loop
2395 Inst := Scope (Inst);
2396 end loop;
2398 if No (Inst) then
2399 return False;
2400 end if;
2402 Act := First_Entity (Inst);
2404 while Present (Act) loop
2405 if Ekind (Act) = E_Package then
2407 -- Check for end of actuals list
2409 if Renamed_Object (Act) = Inst then
2410 return False;
2412 elsif Present (Associated_Formal_Package (Act))
2413 and then Renamed_Object (Act) = Scop
2414 then
2415 -- Entity comes from (instance of) formal package
2417 return True;
2419 else
2420 Next_Entity (Act);
2421 end if;
2423 else
2424 Next_Entity (Act);
2425 end if;
2426 end loop;
2428 return False;
2429 end if;
2430 end From_Actual_Package;
2432 -------------------------
2433 -- Known_But_Invisible --
2434 -------------------------
2436 function Known_But_Invisible (E : Entity_Id) return Boolean is
2437 Fname : File_Name_Type;
2439 begin
2440 -- Entities in Standard are always considered to be known
2442 if Sloc (E) <= Standard_Location then
2443 return True;
2445 -- An entity that does not come from source is always considered
2446 -- to be unknown, since it is an artifact of code expansion.
2448 elsif not Comes_From_Source (E) then
2449 return False;
2451 -- In gnat internal mode, we consider all entities known
2453 elsif GNAT_Mode then
2454 return True;
2455 end if;
2457 -- Here we have an entity that is not from package Standard, and
2458 -- which comes from Source. See if it comes from an internal file.
2460 Fname := Unit_File_Name (Get_Source_Unit (E));
2462 -- Case of from internal file
2464 if Is_Internal_File_Name (Fname) then
2466 -- Private part entities in internal files are never considered
2467 -- to be known to the writer of normal application code.
2469 if Is_Hidden (E) then
2470 return False;
2471 end if;
2473 -- Entities from System packages other than System and
2474 -- System.Storage_Elements are not considered to be known.
2475 -- System.Auxxxx files are also considered known to the user.
2477 -- Should refine this at some point to generally distinguish
2478 -- between known and unknown internal files ???
2480 Get_Name_String (Fname);
2482 return
2483 Name_Len < 2
2484 or else
2485 Name_Buffer (1 .. 2) /= "s-"
2486 or else
2487 Name_Buffer (3 .. 8) = "stoele"
2488 or else
2489 Name_Buffer (3 .. 5) = "aux";
2491 -- If not an internal file, then entity is definitely known,
2492 -- even if it is in a private part (the message generated will
2493 -- note that it is in a private part)
2495 else
2496 return True;
2497 end if;
2498 end Known_But_Invisible;
2500 -------------------
2501 -- Nvis_Messages --
2502 -------------------
2504 procedure Nvis_Messages is
2505 Ent : Entity_Id;
2506 Hidden : Boolean := False;
2508 begin
2509 Undefined (Nvis => True);
2511 if Msg then
2513 -- First loop does hidden declarations
2515 Ent := Homonyms;
2516 while Present (Ent) loop
2517 if Is_Potentially_Use_Visible (Ent) then
2519 if not Hidden then
2520 Error_Msg_N ("multiple use clauses cause hiding!", N);
2521 Hidden := True;
2522 end if;
2524 Error_Msg_Sloc := Sloc (Ent);
2525 Error_Msg_N ("hidden declaration#!", N);
2526 end if;
2528 Ent := Homonym (Ent);
2529 end loop;
2531 -- If we found hidden declarations, then that's enough, don't
2532 -- bother looking for non-visible declarations as well.
2534 if Hidden then
2535 return;
2536 end if;
2538 -- Second loop does non-directly visible declarations
2540 Ent := Homonyms;
2541 while Present (Ent) loop
2542 if not Is_Potentially_Use_Visible (Ent) then
2544 -- Do not bother the user with unknown entities
2546 if not Known_But_Invisible (Ent) then
2547 goto Continue;
2548 end if;
2550 Error_Msg_Sloc := Sloc (Ent);
2552 -- Output message noting that there is a non-visible
2553 -- declaration, distinguishing the private part case.
2555 if Is_Hidden (Ent) then
2556 Error_Msg_N ("non-visible (private) declaration#!", N);
2557 else
2558 Error_Msg_N ("non-visible declaration#!", N);
2560 if Is_Compilation_Unit (Ent)
2561 and then
2562 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
2563 then
2564 Error_Msg_NE
2565 ("\possibly missing with_clause for&", N, Ent);
2566 end if;
2567 end if;
2569 -- Set entity and its containing package as referenced. We
2570 -- can't be sure of this, but this seems a better choice
2571 -- to avoid unused entity messages.
2573 if Comes_From_Source (Ent) then
2574 Set_Referenced (Ent);
2575 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
2576 end if;
2577 end if;
2579 <<Continue>>
2580 Ent := Homonym (Ent);
2581 end loop;
2583 end if;
2584 end Nvis_Messages;
2586 ---------------
2587 -- Undefined --
2588 ---------------
2590 procedure Undefined (Nvis : Boolean) is
2591 Emsg : Error_Msg_Id;
2593 begin
2594 -- We should never find an undefined internal name. If we do, then
2595 -- see if we have previous errors. If so, ignore on the grounds that
2596 -- it is probably a cascaded message (e.g. a block label from a badly
2597 -- formed block). If no previous errors, then we have a real internal
2598 -- error of some kind so raise an exception.
2600 if Is_Internal_Name (Chars (N)) then
2601 if Total_Errors_Detected /= 0 then
2602 return;
2603 else
2604 raise Program_Error;
2605 end if;
2606 end if;
2608 -- A very specialized error check, if the undefined variable is
2609 -- a case tag, and the case type is an enumeration type, check
2610 -- for a possible misspelling, and if so, modify the identifier
2612 -- Named aggregate should also be handled similarly ???
2614 if Nkind (N) = N_Identifier
2615 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
2616 then
2617 Get_Name_String (Chars (N));
2619 declare
2620 Case_Str : constant String := Name_Buffer (1 .. Name_Len);
2621 Case_Stm : constant Node_Id := Parent (Parent (N));
2622 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
2624 Lit : Node_Id;
2626 begin
2627 if Is_Enumeration_Type (Case_Typ)
2628 and then Case_Typ /= Standard_Character
2629 and then Case_Typ /= Standard_Wide_Character
2630 then
2631 Lit := First_Literal (Case_Typ);
2632 Get_Name_String (Chars (Lit));
2634 if Chars (Lit) /= Chars (N)
2635 and then Is_Bad_Spelling_Of
2636 (Case_Str, Name_Buffer (1 .. Name_Len))
2637 then
2638 Error_Msg_Node_2 := Lit;
2639 Error_Msg_N
2640 ("& is undefined, assume misspelling of &", N);
2641 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
2642 return;
2643 end if;
2645 Lit := Next_Literal (Lit);
2646 end if;
2647 end;
2648 end if;
2650 -- Normal processing
2652 Set_Entity (N, Any_Id);
2653 Set_Etype (N, Any_Type);
2655 -- We use the table Urefs to keep track of entities for which we
2656 -- have issued errors for undefined references. Multiple errors
2657 -- for a single name are normally suppressed, however we modify
2658 -- the error message to alert the programmer to this effect.
2660 for J in Urefs.First .. Urefs.Last loop
2661 if Chars (N) = Chars (Urefs.Table (J).Node) then
2662 if Urefs.Table (J).Err /= No_Error_Msg
2663 and then Sloc (N) /= Urefs.Table (J).Loc
2664 then
2665 Error_Msg_Node_1 := Urefs.Table (J).Node;
2667 if Urefs.Table (J).Nvis then
2668 Change_Error_Text (Urefs.Table (J).Err,
2669 "& is not visible (more references follow)");
2670 else
2671 Change_Error_Text (Urefs.Table (J).Err,
2672 "& is undefined (more references follow)");
2673 end if;
2675 Urefs.Table (J).Err := No_Error_Msg;
2676 end if;
2678 -- Although we will set Msg False, and thus suppress the
2679 -- message, we also set Error_Posted True, to avoid any
2680 -- cascaded messages resulting from the undefined reference.
2682 Msg := False;
2683 Set_Error_Posted (N, True);
2684 return;
2685 end if;
2686 end loop;
2688 -- If entry not found, this is first undefined occurrence
2690 if Nvis then
2691 Error_Msg_N ("& is not visible!", N);
2692 Emsg := Get_Msg_Id;
2694 else
2695 Error_Msg_N ("& is undefined!", N);
2696 Emsg := Get_Msg_Id;
2698 -- A very bizarre special check, if the undefined identifier
2699 -- is put or put_line, then add a special error message (since
2700 -- this is a very common error for beginners to make).
2702 if Chars (N) = Name_Put or else Chars (N) = Name_Put_Line then
2703 Error_Msg_N ("\possible missing with of 'Text_'I'O!", N);
2704 end if;
2706 -- Now check for possible misspellings
2708 Get_Name_String (Chars (N));
2710 declare
2711 E : Entity_Id;
2712 Ematch : Entity_Id := Empty;
2714 Last_Name_Id : constant Name_Id :=
2715 Name_Id (Nat (First_Name_Id) +
2716 Name_Entries_Count - 1);
2718 S : constant String (1 .. Name_Len) :=
2719 Name_Buffer (1 .. Name_Len);
2721 begin
2722 for N in First_Name_Id .. Last_Name_Id loop
2723 E := Get_Name_Entity_Id (N);
2725 if Present (E)
2726 and then (Is_Immediately_Visible (E)
2727 or else
2728 Is_Potentially_Use_Visible (E))
2729 then
2730 Get_Name_String (N);
2732 if Is_Bad_Spelling_Of
2733 (Name_Buffer (1 .. Name_Len), S)
2734 then
2735 Ematch := E;
2736 exit;
2737 end if;
2738 end if;
2739 end loop;
2741 if Present (Ematch) then
2742 Error_Msg_NE ("\possible misspelling of&", N, Ematch);
2743 end if;
2744 end;
2745 end if;
2747 -- Make entry in undefined references table unless the full
2748 -- errors switch is set, in which case by refraining from
2749 -- generating the table entry, we guarantee that we get an
2750 -- error message for every undefined reference.
2752 if not All_Errors_Mode then
2753 Urefs.Increment_Last;
2754 Urefs.Table (Urefs.Last).Node := N;
2755 Urefs.Table (Urefs.Last).Err := Emsg;
2756 Urefs.Table (Urefs.Last).Nvis := Nvis;
2757 Urefs.Table (Urefs.Last).Loc := Sloc (N);
2758 end if;
2760 Msg := True;
2761 end Undefined;
2763 -- Start of processing for Find_Direct_Name
2765 begin
2766 -- If the entity pointer is already set, this is an internal node, or
2767 -- a node that is analyzed more than once, after a tree modification.
2768 -- In such a case there is no resolution to perform, just set the type.
2770 if Present (Entity (N)) then
2771 if Is_Type (Entity (N)) then
2772 Set_Etype (N, Entity (N));
2774 else
2775 declare
2776 Entyp : constant Entity_Id := Etype (Entity (N));
2778 begin
2779 -- One special case here. If the Etype field is already set,
2780 -- and references the packed array type corresponding to the
2781 -- etype of the referenced entity, then leave it alone. This
2782 -- happens for trees generated from Exp_Pakd, where expressions
2783 -- can be deliberately "mis-typed" to the packed array type.
2785 if Is_Array_Type (Entyp)
2786 and then Is_Packed (Entyp)
2787 and then Present (Etype (N))
2788 and then Etype (N) = Packed_Array_Type (Entyp)
2789 then
2790 null;
2792 -- If not that special case, then just reset the Etype
2794 else
2795 Set_Etype (N, Etype (Entity (N)));
2796 end if;
2797 end;
2798 end if;
2800 return;
2801 end if;
2803 -- Here if Entity pointer was not set, we need full visibility analysis
2804 -- First we generate debugging output if the debug E flag is set.
2806 if Debug_Flag_E then
2807 Write_Str ("Looking for ");
2808 Write_Name (Chars (N));
2809 Write_Eol;
2810 end if;
2812 Homonyms := Current_Entity (N);
2813 Nvis_Entity := False;
2815 E := Homonyms;
2816 while Present (E) loop
2818 -- If entity is immediately visible or potentially use
2819 -- visible, then process the entity and we are done.
2821 if Is_Immediately_Visible (E) then
2822 goto Immediately_Visible_Entity;
2824 elsif Is_Potentially_Use_Visible (E) then
2825 goto Potentially_Use_Visible_Entity;
2827 -- Note if a known but invisible entity encountered
2829 elsif Known_But_Invisible (E) then
2830 Nvis_Entity := True;
2831 end if;
2833 -- Move to next entity in chain and continue search
2835 E := Homonym (E);
2836 end loop;
2838 -- If no entries on homonym chain that were potentially visible,
2839 -- and no entities reasonably considered as non-visible, then
2840 -- we have a plain undefined reference, with no additional
2841 -- explanation required!
2843 if not Nvis_Entity then
2844 Undefined (Nvis => False);
2846 -- Otherwise there is at least one entry on the homonym chain that
2847 -- is reasonably considered as being known and non-visible.
2849 else
2850 Nvis_Messages;
2851 end if;
2853 return;
2855 -- Processing for a potentially use visible entry found. We must search
2856 -- the rest of the homonym chain for two reasons. First, if there is a
2857 -- directly visible entry, then none of the potentially use-visible
2858 -- entities are directly visible (RM 8.4(10)). Second, we need to check
2859 -- for the case of multiple potentially use-visible entries hiding one
2860 -- another and as a result being non-directly visible (RM 8.4(11)).
2862 <<Potentially_Use_Visible_Entity>> declare
2863 Only_One_Visible : Boolean := True;
2864 All_Overloadable : Boolean := Is_Overloadable (E);
2866 begin
2867 E2 := Homonym (E);
2869 while Present (E2) loop
2870 if Is_Immediately_Visible (E2) then
2872 -- If the use-visible entity comes from the actual for a
2873 -- formal package, it hides a directly visible entity from
2874 -- outside the instance.
2876 if From_Actual_Package (E)
2877 and then Scope_Depth (E2) < Scope_Depth (Inst)
2878 then
2879 goto Found;
2880 else
2881 E := E2;
2882 goto Immediately_Visible_Entity;
2883 end if;
2885 elsif Is_Potentially_Use_Visible (E2) then
2886 Only_One_Visible := False;
2887 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
2888 end if;
2890 E2 := Homonym (E2);
2891 end loop;
2893 -- On falling through this loop, we have checked that there are no
2894 -- immediately visible entities. Only_One_Visible is set if exactly
2895 -- one potentially use visible entity exists. All_Overloadable is
2896 -- set if all the potentially use visible entities are overloadable.
2897 -- The condition for legality is that either there is one potentially
2898 -- use visible entity, or if there is more than one, then all of them
2899 -- are overloadable.
2901 if Only_One_Visible or All_Overloadable then
2902 goto Found;
2904 -- If there is more than one potentially use-visible entity and at
2905 -- least one of them non-overloadable, we have an error (RM 8.4(11).
2906 -- Note that E points to the first such entity on the homonym list.
2907 -- Special case: if one of the entities is declared in an actual
2908 -- package, it was visible in the generic, and takes precedence over
2909 -- other entities that are potentially use-visible. Same if it is
2910 -- declared in a local instantiation of the current instance.
2912 else
2913 if In_Instance then
2914 Inst := Current_Scope;
2916 -- Find current instance.
2918 while Present (Inst)
2919 and then Inst /= Standard_Standard
2920 loop
2921 if Is_Generic_Instance (Inst) then
2922 exit;
2923 end if;
2925 Inst := Scope (Inst);
2926 end loop;
2928 E2 := E;
2930 while Present (E2) loop
2931 if From_Actual_Package (E2)
2932 or else
2933 (Is_Generic_Instance (Scope (E2))
2934 and then Scope_Depth (Scope (E2)) > Scope_Depth (Inst))
2935 then
2936 E := E2;
2937 goto Found;
2938 end if;
2940 E2 := Homonym (E2);
2941 end loop;
2943 Nvis_Messages;
2944 return;
2946 else
2947 Nvis_Messages;
2948 return;
2949 end if;
2950 end if;
2951 end;
2953 -- Come here with E set to the first immediately visible entity on
2954 -- the homonym chain. This is the one we want unless there is another
2955 -- immediately visible entity further on in the chain for a more
2956 -- inner scope (RM 8.3(8)).
2958 <<Immediately_Visible_Entity>> declare
2959 Level : Int;
2960 Scop : Entity_Id;
2962 begin
2963 -- Find scope level of initial entity. When compiling through
2964 -- Rtsfind, the previous context is not completely invisible, and
2965 -- an outer entity may appear on the chain, whose scope is below
2966 -- the entry for Standard that delimits the current scope stack.
2967 -- Indicate that the level for this spurious entry is outside of
2968 -- the current scope stack.
2970 Level := Scope_Stack.Last;
2971 loop
2972 Scop := Scope_Stack.Table (Level).Entity;
2973 exit when Scop = Scope (E);
2974 Level := Level - 1;
2975 exit when Scop = Standard_Standard;
2976 end loop;
2978 -- Now search remainder of homonym chain for more inner entry
2979 -- If the entity is Standard itself, it has no scope, and we
2980 -- compare it with the stack entry directly.
2982 E2 := Homonym (E);
2983 while Present (E2) loop
2984 if Is_Immediately_Visible (E2) then
2985 for J in Level + 1 .. Scope_Stack.Last loop
2986 if Scope_Stack.Table (J).Entity = Scope (E2)
2987 or else Scope_Stack.Table (J).Entity = E2
2988 then
2989 Level := J;
2990 E := E2;
2991 exit;
2992 end if;
2993 end loop;
2994 end if;
2996 E2 := Homonym (E2);
2997 end loop;
2999 -- At the end of that loop, E is the innermost immediately
3000 -- visible entity, so we are all set.
3001 end;
3003 -- Come here with entity found, and stored in E
3005 <<Found>> begin
3007 if Comes_From_Source (N)
3008 and then Is_Remote_Access_To_Subprogram_Type (E)
3009 and then Expander_Active
3010 then
3011 Rewrite (N,
3012 New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
3013 return;
3014 end if;
3016 Set_Entity (N, E);
3017 -- Why no Style_Check here???
3019 if Is_Type (E) then
3020 Set_Etype (N, E);
3021 else
3022 Set_Etype (N, Get_Full_View (Etype (E)));
3023 end if;
3025 if Debug_Flag_E then
3026 Write_Str (" found ");
3027 Write_Entity_Info (E, " ");
3028 end if;
3030 -- If the Ekind of the entity is Void, it means that all homonyms
3031 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
3032 -- test is skipped if the current scope is a record and the name is
3033 -- a pragma argument expression (case of Atomic and Volatile pragmas
3034 -- and possibly other similar pragmas added later, which are allowed
3035 -- to reference components in the current record).
3037 if Ekind (E) = E_Void
3038 and then
3039 (not Is_Record_Type (Current_Scope)
3040 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
3041 then
3042 Premature_Usage (N);
3044 -- If the entity is overloadable, collect all interpretations
3045 -- of the name for subsequent overload resolution. We optimize
3046 -- a bit here to do this only if we have an overloadable entity
3047 -- that is not on its own on the homonym chain.
3049 elsif Is_Overloadable (E)
3050 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
3051 then
3052 Collect_Interps (N);
3054 -- If no homonyms were visible, the entity is unambiguous.
3056 if not Is_Overloaded (N) then
3057 Generate_Reference (E, N);
3058 end if;
3060 -- Case of non-overloadable entity, set the entity providing that
3061 -- we do not have the case of a discriminant reference within a
3062 -- default expression. Such references are replaced with the
3063 -- corresponding discriminal, which is the formal corresponding to
3064 -- to the discriminant in the initialization procedure.
3066 else
3067 -- Entity is unambiguous, indicate that it is referenced here
3068 -- One slightly odd case is that we do not want to set the
3069 -- Referenced flag if the entity is a label, and the identifier
3070 -- is the label in the source, since this is not a reference
3071 -- from the point of view of the user
3073 if Nkind (Parent (N)) = N_Label then
3074 declare
3075 R : constant Boolean := Referenced (E);
3077 begin
3078 Generate_Reference (E, N);
3079 Set_Referenced (E, R);
3080 end;
3082 -- Normal case, not a label. Generate reference.
3084 else
3085 Generate_Reference (E, N);
3086 end if;
3088 -- Set Entity, with style check if need be. If this is a
3089 -- discriminant reference, it must be replaced by the
3090 -- corresponding discriminal, that is to say the parameter
3091 -- of the initialization procedure that corresponds to the
3092 -- discriminant. If this replacement is being performed, there
3093 -- is no style check to perform.
3095 -- This replacement must not be done if we are currently
3096 -- processing a generic spec or body, because the discriminal
3097 -- has not been not generated in this case.
3099 if not In_Default_Expression
3100 or else Ekind (E) /= E_Discriminant
3101 or else Inside_A_Generic
3102 then
3103 Set_Entity_With_Style_Check (N, E);
3105 -- The replacement is not done either for a task discriminant that
3106 -- appears in a default expression of an entry parameter. See
3107 -- Expand_Discriminant in exp_ch2 for details on their handling.
3109 elsif Is_Concurrent_Type (Scope (E)) then
3110 declare
3111 P : Node_Id := Parent (N);
3113 begin
3114 while Present (P)
3115 and then Nkind (P) /= N_Parameter_Specification
3116 and then Nkind (P) /= N_Component_Declaration
3117 loop
3118 P := Parent (P);
3119 end loop;
3121 if Present (P)
3122 and then Nkind (P) = N_Parameter_Specification
3123 then
3124 null;
3125 else
3126 Set_Entity (N, Discriminal (E));
3127 end if;
3128 end;
3130 -- Otherwise, this is a discriminant in a context in which
3131 -- it is a reference to the corresponding parameter of the
3132 -- init proc for the enclosing type.
3134 else
3135 Set_Entity (N, Discriminal (E));
3136 end if;
3137 end if;
3138 end;
3139 end Find_Direct_Name;
3141 ------------------------
3142 -- Find_Expanded_Name --
3143 ------------------------
3145 -- This routine searches the homonym chain of the entity until it finds
3146 -- an entity declared in the scope denoted by the prefix. If the entity
3147 -- is private, it may nevertheless be immediately visible, if we are in
3148 -- the scope of its declaration.
3150 procedure Find_Expanded_Name (N : Node_Id) is
3151 Selector : constant Node_Id := Selector_Name (N);
3152 Candidate : Entity_Id := Empty;
3153 P_Name : Entity_Id;
3154 O_Name : Entity_Id;
3155 Id : Entity_Id;
3157 begin
3158 P_Name := Entity (Prefix (N));
3159 O_Name := P_Name;
3161 -- If the prefix is a renamed package, look for the entity
3162 -- in the original package.
3164 if Ekind (P_Name) = E_Package
3165 and then Present (Renamed_Object (P_Name))
3166 then
3167 P_Name := Renamed_Object (P_Name);
3169 -- Rewrite node with entity field pointing to renamed object
3171 Rewrite (Prefix (N), New_Copy (Prefix (N)));
3172 Set_Entity (Prefix (N), P_Name);
3174 -- If the prefix is an object of a concurrent type, look for
3175 -- the entity in the associated task or protected type.
3177 elsif Is_Concurrent_Type (Etype (P_Name)) then
3178 P_Name := Etype (P_Name);
3179 end if;
3181 Id := Current_Entity (Selector);
3183 while Present (Id) loop
3185 if Scope (Id) = P_Name then
3186 Candidate := Id;
3188 if Is_Child_Unit (Id) then
3189 exit when Is_Visible_Child_Unit (Id)
3190 or else Is_Immediately_Visible (Id);
3192 else
3193 exit when not Is_Hidden (Id)
3194 or else Is_Immediately_Visible (Id);
3195 end if;
3196 end if;
3198 Id := Homonym (Id);
3199 end loop;
3201 if No (Id)
3202 and then (Ekind (P_Name) = E_Procedure
3203 or else
3204 Ekind (P_Name) = E_Function)
3205 and then Is_Generic_Instance (P_Name)
3206 then
3207 -- Expanded name denotes entity in (instance of) generic subprogram.
3208 -- The entity may be in the subprogram instance, or may denote one of
3209 -- the formals, which is declared in the enclosing wrapper package.
3211 P_Name := Scope (P_Name);
3212 Id := Current_Entity (Selector);
3214 while Present (Id) loop
3215 exit when Scope (Id) = P_Name;
3216 Id := Homonym (Id);
3217 end loop;
3218 end if;
3220 if No (Id) or else Chars (Id) /= Chars (Selector) then
3222 Set_Etype (N, Any_Type);
3224 -- If we are looking for an entity defined in System, try to
3225 -- find it in the child package that may have been provided as
3226 -- an extension to System. The Extend_System pragma will have
3227 -- supplied the name of the extension, which may have to be loaded.
3229 if Chars (P_Name) = Name_System
3230 and then Scope (P_Name) = Standard_Standard
3231 and then Present (System_Extend_Unit)
3232 and then Present_System_Aux (N)
3233 then
3234 Set_Entity (Prefix (N), System_Aux_Id);
3235 Find_Expanded_Name (N);
3236 return;
3238 elsif Nkind (Selector) = N_Operator_Symbol
3239 and then Has_Implicit_Operator (N)
3240 then
3241 -- There is an implicit instance of the predefined operator in
3242 -- the given scope. The operator entity is defined in Standard.
3243 -- Has_Implicit_Operator makes the node into an Expanded_Name.
3245 return;
3247 elsif Nkind (Selector) = N_Character_Literal
3248 and then Has_Implicit_Character_Literal (N)
3249 then
3250 -- If there is no literal defined in the scope denoted by the
3251 -- prefix, the literal may belong to (a type derived from)
3252 -- Standard_Character, for which we have no explicit literals.
3254 return;
3256 else
3257 -- If the prefix is a single concurrent object, use its
3258 -- name in the error message, rather than that of the
3259 -- anonymous type.
3261 if Is_Concurrent_Type (P_Name)
3262 and then Is_Internal_Name (Chars (P_Name))
3263 then
3264 Error_Msg_Node_2 := Entity (Prefix (N));
3265 else
3266 Error_Msg_Node_2 := P_Name;
3267 end if;
3269 if P_Name = System_Aux_Id then
3270 P_Name := Scope (P_Name);
3271 Set_Entity (Prefix (N), P_Name);
3272 end if;
3274 if Present (Candidate) then
3276 if Is_Child_Unit (Candidate) then
3277 Error_Msg_N
3278 ("missing with_clause for child unit &", Selector);
3279 else
3280 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
3281 end if;
3283 else
3284 -- Within the instantiation of a child unit, the prefix may
3285 -- denote the parent instance, but the selector has the
3286 -- name of the original child. Find whether we are within
3287 -- the corresponding instance, and get the proper entity, which
3288 -- can only be an enclosing scope.
3290 if O_Name /= P_Name
3291 and then In_Open_Scopes (P_Name)
3292 and then Is_Generic_Instance (P_Name)
3293 then
3294 declare
3295 S : Entity_Id := Current_Scope;
3296 P : Entity_Id;
3298 begin
3299 for J in reverse 0 .. Scope_Stack.Last loop
3300 S := Scope_Stack.Table (J).Entity;
3302 exit when S = Standard_Standard;
3304 if Ekind (S) = E_Function
3305 or else Ekind (S) = E_Package
3306 or else Ekind (S) = E_Procedure
3307 then
3308 P := Generic_Parent (Specification
3309 (Unit_Declaration_Node (S)));
3311 if Present (P)
3312 and then Chars (Scope (P)) = Chars (O_Name)
3313 and then Chars (P) = Chars (Selector)
3314 then
3315 Id := S;
3316 goto found;
3317 end if;
3318 end if;
3320 end loop;
3321 end;
3322 end if;
3324 if Chars (P_Name) = Name_Ada
3325 and then Scope (P_Name) = Standard_Standard
3326 then
3327 Error_Msg_Node_2 := Selector;
3328 Error_Msg_NE ("missing with for `&.&`", N, P_Name);
3330 -- If this is a selection from a dummy package, then
3331 -- suppress the error message, of course the entity
3332 -- is missing if the package is missing!
3334 elsif Sloc (Error_Msg_Node_2) = No_Location then
3335 null;
3337 -- Here we have the case of an undefined component
3339 else
3341 Error_Msg_NE ("& not declared in&", N, Selector);
3343 -- Check for misspelling of some entity in prefix.
3345 Id := First_Entity (P_Name);
3346 Get_Name_String (Chars (Selector));
3348 declare
3349 S : constant String (1 .. Name_Len) :=
3350 Name_Buffer (1 .. Name_Len);
3351 begin
3352 while Present (Id) loop
3353 Get_Name_String (Chars (Id));
3354 if Is_Bad_Spelling_Of
3355 (Name_Buffer (1 .. Name_Len), S)
3356 and then not Is_Internal_Name (Chars (Id))
3357 then
3358 Error_Msg_NE
3359 ("possible misspelling of&", Selector, Id);
3360 exit;
3361 end if;
3363 Next_Entity (Id);
3364 end loop;
3365 end;
3367 -- Specialize the message if this may be an instantiation
3368 -- of a child unit that was not mentioned in the context.
3370 if Nkind (Parent (N)) = N_Package_Instantiation
3371 and then Is_Generic_Instance (Entity (Prefix (N)))
3372 and then Is_Compilation_Unit
3373 (Generic_Parent (Parent (Entity (Prefix (N)))))
3374 then
3375 Error_Msg_NE
3376 ("\possible missing with clause on child unit&",
3377 N, Selector);
3378 end if;
3379 end if;
3380 end if;
3382 Id := Any_Id;
3383 end if;
3384 end if;
3386 <<found>>
3387 if Comes_From_Source (N)
3388 and then Is_Remote_Access_To_Subprogram_Type (Id)
3389 then
3390 Id := Equivalent_Type (Id);
3391 Set_Chars (Selector, Chars (Id));
3392 end if;
3394 -- Ada0Y (AI-50217): Check usage of entities in limited withed units
3396 if Ekind (P_Name) = E_Package
3397 and then From_With_Type (P_Name)
3398 then
3399 if From_With_Type (Id)
3400 or else (Ekind (Id) = E_Package and then From_With_Type (Id))
3401 then
3402 null;
3403 else
3404 Error_Msg_N
3405 ("limited withed package can only be used to access "
3406 & " incomplete types",
3408 end if;
3409 end if;
3411 if Is_Task_Type (P_Name)
3412 and then ((Ekind (Id) = E_Entry
3413 and then Nkind (Parent (N)) /= N_Attribute_Reference)
3414 or else
3415 (Ekind (Id) = E_Entry_Family
3416 and then
3417 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
3418 then
3419 -- It is an entry call after all, either to the current task
3420 -- (which will deadlock) or to an enclosing task.
3422 Analyze_Selected_Component (N);
3423 return;
3424 end if;
3426 Change_Selected_Component_To_Expanded_Name (N);
3428 -- Do style check and generate reference, but skip both steps if this
3429 -- entity has homonyms, since we may not have the right homonym set
3430 -- yet. The proper homonym will be set during the resolve phase.
3432 if Has_Homonym (Id) then
3433 Set_Entity (N, Id);
3434 else
3435 Set_Entity_With_Style_Check (N, Id);
3436 Generate_Reference (Id, N);
3437 end if;
3439 if Is_Type (Id) then
3440 Set_Etype (N, Id);
3441 else
3442 Set_Etype (N, Get_Full_View (Etype (Id)));
3443 end if;
3445 -- If the Ekind of the entity is Void, it means that all homonyms
3446 -- are hidden from all visibility (RM 8.3(5,14-20)).
3448 if Ekind (Id) = E_Void then
3449 Premature_Usage (N);
3451 elsif Is_Overloadable (Id)
3452 and then Present (Homonym (Id))
3453 then
3454 declare
3455 H : Entity_Id := Homonym (Id);
3457 begin
3458 while Present (H) loop
3459 if Scope (H) = Scope (Id) then
3460 Collect_Interps (N);
3461 exit;
3462 end if;
3464 H := Homonym (H);
3465 end loop;
3467 -- If an extension of System is present, collect possible
3468 -- explicit overloadings declared in the extension.
3470 if Chars (P_Name) = Name_System
3471 and then Scope (P_Name) = Standard_Standard
3472 and then Present (System_Extend_Unit)
3473 and then Present_System_Aux (N)
3474 then
3475 H := Current_Entity (Id);
3477 while Present (H) loop
3478 if Scope (H) = System_Aux_Id then
3479 Add_One_Interp (N, H, Etype (H));
3480 end if;
3482 H := Homonym (H);
3483 end loop;
3484 end if;
3485 end;
3486 end if;
3488 if Nkind (Selector_Name (N)) = N_Operator_Symbol
3489 and then Scope (Id) /= Standard_Standard
3490 then
3491 -- In addition to user-defined operators in the given scope,
3492 -- there may be an implicit instance of the predefined
3493 -- operator. The operator (defined in Standard) is found
3494 -- in Has_Implicit_Operator, and added to the interpretations.
3495 -- Procedure Add_One_Interp will determine which hides which.
3497 if Has_Implicit_Operator (N) then
3498 null;
3499 end if;
3500 end if;
3501 end Find_Expanded_Name;
3503 -------------------------
3504 -- Find_Renamed_Entity --
3505 -------------------------
3507 function Find_Renamed_Entity
3508 (N : Node_Id;
3509 Nam : Node_Id;
3510 New_S : Entity_Id;
3511 Is_Actual : Boolean := False) return Entity_Id
3513 Ind : Interp_Index;
3514 I1 : Interp_Index := 0; -- Suppress junk warnings
3515 It : Interp;
3516 It1 : Interp;
3517 Old_S : Entity_Id;
3518 Inst : Entity_Id;
3520 function Enclosing_Instance return Entity_Id;
3521 -- If the renaming determines the entity for the default of a formal
3522 -- subprogram nested within another instance, choose the innermost
3523 -- candidate. This is because if the formal has a box, and we are within
3524 -- an enclosing instance where some candidate interpretations are local
3525 -- to this enclosing instance, we know that the default was properly
3526 -- resolved when analyzing the generic, so we prefer the local
3527 -- candidates to those that are external. This is not always the case
3528 -- but is a reasonable heuristic on the use of nested generics.
3529 -- The proper solution requires a full renaming model.
3531 function Within (Inner, Outer : Entity_Id) return Boolean;
3532 -- Determine whether a candidate subprogram is defined within
3533 -- the enclosing instance. If yes, it has precedence over outer
3534 -- candidates.
3536 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
3537 -- If the renamed entity is an implicit operator, check whether it is
3538 -- visible because its operand type is properly visible. This
3539 -- check applies to explicit renamed entities that appear in the
3540 -- source in a renaming declaration or a formal subprogram instance,
3541 -- but not to default generic actuals with a name.
3543 ------------------------
3544 -- Enclosing_Instance --
3545 ------------------------
3547 function Enclosing_Instance return Entity_Id is
3548 S : Entity_Id;
3550 begin
3551 if not Is_Generic_Instance (Current_Scope)
3552 and then not Is_Actual
3553 then
3554 return Empty;
3555 end if;
3557 S := Scope (Current_Scope);
3559 while S /= Standard_Standard loop
3561 if Is_Generic_Instance (S) then
3562 return S;
3563 end if;
3565 S := Scope (S);
3566 end loop;
3568 return Empty;
3569 end Enclosing_Instance;
3571 --------------------------
3572 -- Is_Visible_Operation --
3573 --------------------------
3575 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
3576 Scop : Entity_Id;
3577 Typ : Entity_Id;
3578 Btyp : Entity_Id;
3580 begin
3581 if Ekind (Op) /= E_Operator
3582 or else Scope (Op) /= Standard_Standard
3583 or else (In_Instance
3584 and then
3585 (not Is_Actual
3586 or else Present (Enclosing_Instance)))
3587 then
3588 return True;
3590 else
3591 -- For a fixed point type operator, check the resulting type,
3592 -- because it may be a mixed mode integer * fixed operation.
3594 if Present (Next_Formal (First_Formal (New_S)))
3595 and then Is_Fixed_Point_Type (Etype (New_S))
3596 then
3597 Typ := Etype (New_S);
3598 else
3599 Typ := Etype (First_Formal (New_S));
3600 end if;
3602 Btyp := Base_Type (Typ);
3604 if Nkind (Nam) /= N_Expanded_Name then
3605 return (In_Open_Scopes (Scope (Btyp))
3606 or else Is_Potentially_Use_Visible (Btyp)
3607 or else In_Use (Btyp)
3608 or else In_Use (Scope (Btyp)));
3610 else
3611 Scop := Entity (Prefix (Nam));
3613 if Ekind (Scop) = E_Package
3614 and then Present (Renamed_Object (Scop))
3615 then
3616 Scop := Renamed_Object (Scop);
3617 end if;
3619 -- Operator is visible if prefix of expanded name denotes
3620 -- scope of type, or else type type is defined in System_Aux
3621 -- and the prefix denotes System.
3623 return Scope (Btyp) = Scop
3624 or else (Scope (Btyp) = System_Aux_Id
3625 and then Scope (Scope (Btyp)) = Scop);
3626 end if;
3627 end if;
3628 end Is_Visible_Operation;
3630 ------------
3631 -- Within --
3632 ------------
3634 function Within (Inner, Outer : Entity_Id) return Boolean is
3635 Sc : Entity_Id := Scope (Inner);
3637 begin
3638 while Sc /= Standard_Standard loop
3640 if Sc = Outer then
3641 return True;
3642 else
3643 Sc := Scope (Sc);
3644 end if;
3645 end loop;
3647 return False;
3648 end Within;
3650 function Report_Overload return Entity_Id;
3651 -- List possible interpretations, and specialize message in the
3652 -- case of a generic actual.
3654 function Report_Overload return Entity_Id is
3655 begin
3656 if Is_Actual then
3657 Error_Msg_NE
3658 ("ambiguous actual subprogram&, " &
3659 "possible interpretations: ", N, Nam);
3660 else
3661 Error_Msg_N
3662 ("ambiguous subprogram, " &
3663 "possible interpretations: ", N);
3664 end if;
3666 List_Interps (Nam, N);
3667 return Old_S;
3668 end Report_Overload;
3670 -- Start of processing for Find_Renamed_Entry
3672 begin
3673 Old_S := Any_Id;
3674 Candidate_Renaming := Empty;
3676 if not Is_Overloaded (Nam) then
3677 if Entity_Matches_Spec (Entity (Nam), New_S)
3678 and then Is_Visible_Operation (Entity (Nam))
3679 then
3680 Old_S := Entity (Nam);
3682 elsif
3683 Present (First_Formal (Entity (Nam)))
3684 and then Present (First_Formal (New_S))
3685 and then (Base_Type (Etype (First_Formal (Entity (Nam))))
3686 = Base_Type (Etype (First_Formal (New_S))))
3687 then
3688 Candidate_Renaming := Entity (Nam);
3689 end if;
3691 else
3692 Get_First_Interp (Nam, Ind, It);
3694 while Present (It.Nam) loop
3696 if Entity_Matches_Spec (It.Nam, New_S)
3697 and then Is_Visible_Operation (It.Nam)
3698 then
3699 if Old_S /= Any_Id then
3701 -- Note: The call to Disambiguate only happens if a
3702 -- previous interpretation was found, in which case I1
3703 -- has received a value.
3705 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
3707 if It1 = No_Interp then
3709 Inst := Enclosing_Instance;
3711 if Present (Inst) then
3713 if Within (It.Nam, Inst) then
3714 return (It.Nam);
3716 elsif Within (Old_S, Inst) then
3717 return (Old_S);
3719 else
3720 return Report_Overload;
3721 end if;
3723 else
3724 return Report_Overload;
3725 end if;
3727 else
3728 Old_S := It1.Nam;
3729 exit;
3730 end if;
3732 else
3733 I1 := Ind;
3734 Old_S := It.Nam;
3735 end if;
3737 elsif
3738 Present (First_Formal (It.Nam))
3739 and then Present (First_Formal (New_S))
3740 and then (Base_Type (Etype (First_Formal (It.Nam)))
3741 = Base_Type (Etype (First_Formal (New_S))))
3742 then
3743 Candidate_Renaming := It.Nam;
3744 end if;
3746 Get_Next_Interp (Ind, It);
3747 end loop;
3749 Set_Entity (Nam, Old_S);
3750 Set_Is_Overloaded (Nam, False);
3751 end if;
3753 return Old_S;
3754 end Find_Renamed_Entity;
3756 -----------------------------
3757 -- Find_Selected_Component --
3758 -----------------------------
3760 procedure Find_Selected_Component (N : Node_Id) is
3761 P : constant Node_Id := Prefix (N);
3763 P_Name : Entity_Id;
3764 -- Entity denoted by prefix
3766 P_Type : Entity_Id;
3767 -- and its type
3769 Nam : Node_Id;
3771 begin
3772 Analyze (P);
3774 if Nkind (P) = N_Error then
3775 return;
3777 -- If the selector already has an entity, the node has been
3778 -- constructed in the course of expansion, and is known to be
3779 -- valid. Do not verify that it is defined for the type (it may
3780 -- be a private component used in the expansion of record equality).
3782 elsif Present (Entity (Selector_Name (N))) then
3784 if No (Etype (N))
3785 or else Etype (N) = Any_Type
3786 then
3787 declare
3788 Sel_Name : constant Node_Id := Selector_Name (N);
3789 Selector : constant Entity_Id := Entity (Sel_Name);
3790 C_Etype : Node_Id;
3792 begin
3793 Set_Etype (Sel_Name, Etype (Selector));
3795 if not Is_Entity_Name (P) then
3796 Resolve (P);
3797 end if;
3799 -- Build an actual subtype except for the first parameter
3800 -- of an init proc, where this actual subtype is by
3801 -- definition incorrect, since the object is uninitialized
3802 -- (and does not even have defined discriminants etc.)
3804 if Is_Entity_Name (P)
3805 and then Ekind (Entity (P)) = E_Function
3806 then
3807 Nam := New_Copy (P);
3809 if Is_Overloaded (P) then
3810 Save_Interps (P, Nam);
3811 end if;
3813 Rewrite (P,
3814 Make_Function_Call (Sloc (P), Name => Nam));
3815 Analyze_Call (P);
3816 Analyze_Selected_Component (N);
3817 return;
3819 elsif Ekind (Selector) = E_Component
3820 and then (not Is_Entity_Name (P)
3821 or else Chars (Entity (P)) /= Name_uInit)
3822 then
3823 C_Etype :=
3824 Build_Actual_Subtype_Of_Component (
3825 Etype (Selector), N);
3826 else
3827 C_Etype := Empty;
3828 end if;
3830 if No (C_Etype) then
3831 C_Etype := Etype (Selector);
3832 else
3833 Insert_Action (N, C_Etype);
3834 C_Etype := Defining_Identifier (C_Etype);
3835 end if;
3837 Set_Etype (N, C_Etype);
3838 end;
3840 -- If this is the name of an entry or protected operation, and
3841 -- the prefix is an access type, insert an explicit dereference,
3842 -- so that entry calls are treated uniformly.
3844 if Is_Access_Type (Etype (P))
3845 and then Is_Concurrent_Type (Designated_Type (Etype (P)))
3846 then
3847 declare
3848 New_P : constant Node_Id :=
3849 Make_Explicit_Dereference (Sloc (P),
3850 Prefix => Relocate_Node (P));
3851 begin
3852 Rewrite (P, New_P);
3853 Set_Etype (P, Designated_Type (Etype (Prefix (P))));
3854 end;
3855 end if;
3857 -- If the selected component appears within a default expression
3858 -- and it has an actual subtype, the pre-analysis has not yet
3859 -- completed its analysis, because Insert_Actions is disabled in
3860 -- that context. Within the init proc of the enclosing type we
3861 -- must complete this analysis, if an actual subtype was created.
3863 elsif Inside_Init_Proc then
3864 declare
3865 Typ : constant Entity_Id := Etype (N);
3866 Decl : constant Node_Id := Declaration_Node (Typ);
3868 begin
3869 if Nkind (Decl) = N_Subtype_Declaration
3870 and then not Analyzed (Decl)
3871 and then Is_List_Member (Decl)
3872 and then No (Parent (Decl))
3873 then
3874 Remove (Decl);
3875 Insert_Action (N, Decl);
3876 end if;
3877 end;
3878 end if;
3880 return;
3882 elsif Is_Entity_Name (P) then
3883 P_Name := Entity (P);
3885 -- The prefix may denote an enclosing type which is the completion
3886 -- of an incomplete type declaration.
3888 if Is_Type (P_Name) then
3889 Set_Entity (P, Get_Full_View (P_Name));
3890 Set_Etype (P, Entity (P));
3891 P_Name := Entity (P);
3892 end if;
3894 P_Type := Base_Type (Etype (P));
3896 if Debug_Flag_E then
3897 Write_Str ("Found prefix type to be ");
3898 Write_Entity_Info (P_Type, " "); Write_Eol;
3899 end if;
3901 -- First check for components of a record object (not the
3902 -- result of a call, which is handled below).
3904 if Is_Appropriate_For_Record (P_Type)
3905 and then not Is_Overloadable (P_Name)
3906 and then not Is_Type (P_Name)
3907 then
3908 -- Selected component of record. Type checking will validate
3909 -- name of selector.
3911 Analyze_Selected_Component (N);
3913 elsif Is_Appropriate_For_Entry_Prefix (P_Type)
3914 and then not In_Open_Scopes (P_Name)
3915 and then (not Is_Concurrent_Type (Etype (P_Name))
3916 or else not In_Open_Scopes (Etype (P_Name)))
3917 then
3918 -- Call to protected operation or entry. Type checking is
3919 -- needed on the prefix.
3921 Analyze_Selected_Component (N);
3923 elsif (In_Open_Scopes (P_Name)
3924 and then Ekind (P_Name) /= E_Void
3925 and then not Is_Overloadable (P_Name))
3926 or else (Is_Concurrent_Type (Etype (P_Name))
3927 and then In_Open_Scopes (Etype (P_Name)))
3928 then
3929 -- Prefix denotes an enclosing loop, block, or task, i.e. an
3930 -- enclosing construct that is not a subprogram or accept.
3932 Find_Expanded_Name (N);
3934 elsif Ekind (P_Name) = E_Package then
3935 Find_Expanded_Name (N);
3937 elsif Is_Overloadable (P_Name) then
3939 -- The subprogram may be a renaming (of an enclosing scope) as
3940 -- in the case of the name of the generic within an instantiation.
3942 if (Ekind (P_Name) = E_Procedure
3943 or else Ekind (P_Name) = E_Function)
3944 and then Present (Alias (P_Name))
3945 and then Is_Generic_Instance (Alias (P_Name))
3946 then
3947 P_Name := Alias (P_Name);
3948 end if;
3950 if Is_Overloaded (P) then
3952 -- The prefix must resolve to a unique enclosing construct.
3954 declare
3955 Found : Boolean := False;
3956 Ind : Interp_Index;
3957 It : Interp;
3959 begin
3960 Get_First_Interp (P, Ind, It);
3962 while Present (It.Nam) loop
3964 if In_Open_Scopes (It.Nam) then
3965 if Found then
3966 Error_Msg_N (
3967 "prefix must be unique enclosing scope", N);
3968 Set_Entity (N, Any_Id);
3969 Set_Etype (N, Any_Type);
3970 return;
3972 else
3973 Found := True;
3974 P_Name := It.Nam;
3975 end if;
3976 end if;
3978 Get_Next_Interp (Ind, It);
3979 end loop;
3980 end;
3981 end if;
3983 if In_Open_Scopes (P_Name) then
3984 Set_Entity (P, P_Name);
3985 Set_Is_Overloaded (P, False);
3986 Find_Expanded_Name (N);
3988 else
3989 -- If no interpretation as an expanded name is possible, it
3990 -- must be a selected component of a record returned by a
3991 -- function call. Reformat prefix as a function call, the
3992 -- rest is done by type resolution. If the prefix is a
3993 -- procedure or entry, as is P.X; this is an error.
3995 if Ekind (P_Name) /= E_Function
3996 and then (not Is_Overloaded (P)
3997 or else
3998 Nkind (Parent (N)) = N_Procedure_Call_Statement)
3999 then
4001 -- Prefix may mention a package that is hidden by a local
4002 -- declaration: let the user know. Scan the full homonym
4003 -- chain, the candidate package may be anywhere on it.
4005 if Present (Homonym (Current_Entity (P_Name))) then
4007 P_Name := Current_Entity (P_Name);
4009 while Present (P_Name) loop
4010 exit when Ekind (P_Name) = E_Package;
4011 P_Name := Homonym (P_Name);
4012 end loop;
4014 if Present (P_Name) then
4015 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
4017 Error_Msg_NE
4018 ("package& is hidden by declaration#",
4019 N, P_Name);
4021 Set_Entity (Prefix (N), P_Name);
4022 Find_Expanded_Name (N);
4023 return;
4024 else
4025 P_Name := Entity (Prefix (N));
4026 end if;
4027 end if;
4029 Error_Msg_NE
4030 ("invalid prefix in selected component&", N, P_Name);
4031 Change_Selected_Component_To_Expanded_Name (N);
4032 Set_Entity (N, Any_Id);
4033 Set_Etype (N, Any_Type);
4035 else
4036 Nam := New_Copy (P);
4037 Save_Interps (P, Nam);
4038 Rewrite (P,
4039 Make_Function_Call (Sloc (P), Name => Nam));
4040 Analyze_Call (P);
4041 Analyze_Selected_Component (N);
4042 end if;
4043 end if;
4045 -- Remaining cases generate various error messages
4047 else
4048 -- Format node as expanded name, to avoid cascaded errors
4050 Change_Selected_Component_To_Expanded_Name (N);
4051 Set_Entity (N, Any_Id);
4052 Set_Etype (N, Any_Type);
4054 -- Issue error message, but avoid this if error issued already.
4055 -- Use identifier of prefix if one is available.
4057 if P_Name = Any_Id then
4058 null;
4060 elsif Ekind (P_Name) = E_Void then
4061 Premature_Usage (P);
4063 elsif Nkind (P) /= N_Attribute_Reference then
4064 Error_Msg_N (
4065 "invalid prefix in selected component&", P);
4067 if Is_Access_Type (P_Type)
4068 and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
4069 then
4070 Error_Msg_N
4071 ("\dereference must not be of an incomplete type " &
4072 "('R'M 3.10.1)", P);
4073 end if;
4075 else
4076 Error_Msg_N (
4077 "invalid prefix in selected component", P);
4078 end if;
4079 end if;
4081 else
4082 -- If prefix is not the name of an entity, it must be an expression,
4083 -- whose type is appropriate for a record. This is determined by
4084 -- type resolution.
4086 Analyze_Selected_Component (N);
4087 end if;
4088 end Find_Selected_Component;
4090 ---------------
4091 -- Find_Type --
4092 ---------------
4094 procedure Find_Type (N : Node_Id) is
4095 C : Entity_Id;
4096 Typ : Entity_Id;
4097 T : Entity_Id;
4098 T_Name : Entity_Id;
4100 begin
4101 if N = Error then
4102 return;
4104 elsif Nkind (N) = N_Attribute_Reference then
4106 -- Class attribute. This is only valid in Ada 95 mode, but we don't
4107 -- do a check, since the tagged type referenced could only exist if
4108 -- we were in 95 mode when it was declared (or, if we were in Ada
4109 -- 83 mode, then an error message would already have been issued).
4111 if Attribute_Name (N) = Name_Class then
4112 Check_Restriction (No_Dispatch, N);
4113 Find_Type (Prefix (N));
4115 -- Propagate error from bad prefix
4117 if Etype (Prefix (N)) = Any_Type then
4118 Set_Entity (N, Any_Type);
4119 Set_Etype (N, Any_Type);
4120 return;
4121 end if;
4123 T := Base_Type (Entity (Prefix (N)));
4125 -- Case of non-tagged type
4127 if not Is_Tagged_Type (T) then
4128 if Ekind (T) = E_Incomplete_Type then
4130 -- It is legal to denote the class type of an incomplete
4131 -- type. The full type will have to be tagged, of course.
4133 Set_Is_Tagged_Type (T);
4134 Make_Class_Wide_Type (T);
4135 Set_Entity (N, Class_Wide_Type (T));
4136 Set_Etype (N, Class_Wide_Type (T));
4138 elsif Ekind (T) = E_Private_Type
4139 and then not Is_Generic_Type (T)
4140 and then In_Private_Part (Scope (T))
4141 then
4142 -- The Class attribute can be applied to an untagged
4143 -- private type fulfilled by a tagged type prior to
4144 -- the full type declaration (but only within the
4145 -- parent package's private part). Create the class-wide
4146 -- type now and check that the full type is tagged
4147 -- later during its analysis. Note that we do not
4148 -- mark the private type as tagged, unlike the case
4149 -- of incomplete types, because the type must still
4150 -- appear untagged to outside units.
4152 if not Present (Class_Wide_Type (T)) then
4153 Make_Class_Wide_Type (T);
4154 end if;
4156 Set_Entity (N, Class_Wide_Type (T));
4157 Set_Etype (N, Class_Wide_Type (T));
4159 else
4160 -- Should we introduce a type Any_Tagged and use
4161 -- Wrong_Type here, it would be a bit more consistent???
4163 Error_Msg_NE
4164 ("tagged type required, found}",
4165 Prefix (N), First_Subtype (T));
4166 Set_Entity (N, Any_Type);
4167 return;
4168 end if;
4170 -- Case of tagged type
4172 else
4173 C := Class_Wide_Type (Entity (Prefix (N)));
4174 Set_Entity_With_Style_Check (N, C);
4175 Generate_Reference (C, N);
4176 Set_Etype (N, C);
4177 end if;
4179 -- Base attribute, allowed in Ada 95 mode only
4181 elsif Attribute_Name (N) = Name_Base then
4182 if Ada_83 and then Comes_From_Source (N) then
4183 Error_Msg_N
4184 ("(Ada 83) Base attribute not allowed in subtype mark", N);
4186 else
4187 Find_Type (Prefix (N));
4188 Typ := Entity (Prefix (N));
4190 if Ada_95
4191 and then not Is_Scalar_Type (Typ)
4192 and then not Is_Generic_Type (Typ)
4193 then
4194 Error_Msg_N
4195 ("prefix of Base attribute must be scalar type", Typ);
4197 elsif Sloc (Typ) = Standard_Location
4198 and then Base_Type (Typ) = Typ
4199 and then Warn_On_Redundant_Constructs
4200 then
4201 Error_Msg_NE
4202 ("?redudant attribute, & is its own base type", N, Typ);
4203 end if;
4205 T := Base_Type (Typ);
4207 -- Rewrite attribute reference with type itself (see similar
4208 -- processing in Analyze_Attribute, case Base). Preserve
4209 -- prefix if present, for other legality checks.
4211 if Nkind (Prefix (N)) = N_Expanded_Name then
4212 Rewrite (N,
4213 Make_Expanded_Name (Sloc (N),
4214 Chars => Chars (Entity (N)),
4215 Prefix => New_Copy (Prefix (Prefix (N))),
4216 Selector_Name =>
4217 New_Reference_To (Entity (N), Sloc (N))));
4219 else
4220 Rewrite (N,
4221 New_Reference_To (Entity (N), Sloc (N)));
4222 end if;
4224 Set_Entity (N, T);
4225 Set_Etype (N, T);
4226 end if;
4228 -- All other attributes are invalid in a subtype mark
4230 else
4231 Error_Msg_N ("invalid attribute in subtype mark", N);
4232 end if;
4234 else
4235 Analyze (N);
4237 if Is_Entity_Name (N) then
4238 T_Name := Entity (N);
4239 else
4240 Error_Msg_N ("subtype mark required in this context", N);
4241 Set_Etype (N, Any_Type);
4242 return;
4243 end if;
4245 if T_Name = Any_Id or else Etype (N) = Any_Type then
4247 -- Undefined id. Make it into a valid type
4249 Set_Entity (N, Any_Type);
4251 elsif not Is_Type (T_Name)
4252 and then T_Name /= Standard_Void_Type
4253 then
4254 Error_Msg_Sloc := Sloc (T_Name);
4255 Error_Msg_N ("subtype mark required in this context", N);
4256 Error_Msg_NE ("\found & declared#", N, T_Name);
4257 Set_Entity (N, Any_Type);
4259 else
4260 T_Name := Get_Full_View (T_Name);
4262 if In_Open_Scopes (T_Name) then
4263 if Ekind (Base_Type (T_Name)) = E_Task_Type then
4264 Error_Msg_N ("task type cannot be used as type mark " &
4265 "within its own body", N);
4266 else
4267 Error_Msg_N ("type declaration cannot refer to itself", N);
4268 end if;
4270 Set_Etype (N, Any_Type);
4271 Set_Entity (N, Any_Type);
4272 Set_Error_Posted (T_Name);
4273 return;
4274 end if;
4276 Set_Entity (N, T_Name);
4277 Set_Etype (N, T_Name);
4278 end if;
4279 end if;
4281 if Present (Etype (N)) and then Comes_From_Source (N) then
4282 if Is_Fixed_Point_Type (Etype (N)) then
4283 Check_Restriction (No_Fixed_Point, N);
4284 elsif Is_Floating_Point_Type (Etype (N)) then
4285 Check_Restriction (No_Floating_Point, N);
4286 end if;
4287 end if;
4288 end Find_Type;
4290 -------------------
4291 -- Get_Full_View --
4292 -------------------
4294 function Get_Full_View (T_Name : Entity_Id) return Entity_Id is
4295 begin
4296 if Ekind (T_Name) = E_Incomplete_Type
4297 and then Present (Full_View (T_Name))
4298 then
4299 return Full_View (T_Name);
4301 elsif Is_Class_Wide_Type (T_Name)
4302 and then Ekind (Root_Type (T_Name)) = E_Incomplete_Type
4303 and then Present (Full_View (Root_Type (T_Name)))
4304 then
4305 return Class_Wide_Type (Full_View (Root_Type (T_Name)));
4307 else
4308 return T_Name;
4309 end if;
4310 end Get_Full_View;
4312 ------------------------------------
4313 -- Has_Implicit_Character_Literal --
4314 ------------------------------------
4316 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
4317 Id : Entity_Id;
4318 Found : Boolean := False;
4319 P : constant Entity_Id := Entity (Prefix (N));
4320 Priv_Id : Entity_Id := Empty;
4322 begin
4323 if Ekind (P) = E_Package
4324 and then not In_Open_Scopes (P)
4325 then
4326 Priv_Id := First_Private_Entity (P);
4327 end if;
4329 if P = Standard_Standard then
4330 Change_Selected_Component_To_Expanded_Name (N);
4331 Rewrite (N, Selector_Name (N));
4332 Analyze (N);
4333 Set_Etype (Original_Node (N), Standard_Character);
4334 return True;
4335 end if;
4337 Id := First_Entity (P);
4339 while Present (Id)
4340 and then Id /= Priv_Id
4341 loop
4342 if Is_Character_Type (Id)
4343 and then (Root_Type (Id) = Standard_Character
4344 or else Root_Type (Id) = Standard_Wide_Character)
4345 and then Id = Base_Type (Id)
4346 then
4347 -- We replace the node with the literal itself, resolve as a
4348 -- character, and set the type correctly.
4350 if not Found then
4351 Change_Selected_Component_To_Expanded_Name (N);
4352 Rewrite (N, Selector_Name (N));
4353 Analyze (N);
4354 Set_Etype (N, Id);
4355 Set_Etype (Original_Node (N), Id);
4356 Found := True;
4358 else
4359 -- More than one type derived from Character in given scope.
4360 -- Collect all possible interpretations.
4362 Add_One_Interp (N, Id, Id);
4363 end if;
4364 end if;
4366 Next_Entity (Id);
4367 end loop;
4369 return Found;
4370 end Has_Implicit_Character_Literal;
4372 ---------------------------
4373 -- Has_Implicit_Operator --
4374 ---------------------------
4376 function Has_Implicit_Operator (N : Node_Id) return Boolean is
4377 Op_Id : constant Name_Id := Chars (Selector_Name (N));
4378 P : constant Entity_Id := Entity (Prefix (N));
4379 Id : Entity_Id;
4380 Priv_Id : Entity_Id := Empty;
4382 procedure Add_Implicit_Operator
4383 (T : Entity_Id;
4384 Op_Type : Entity_Id := Empty);
4385 -- Add implicit interpretation to node N, using the type for which
4386 -- a predefined operator exists. If the operator yields a boolean
4387 -- type, the Operand_Type is implicitly referenced by the operator,
4388 -- and a reference to it must be generated.
4390 ---------------------------
4391 -- Add_Implicit_Operator --
4392 ---------------------------
4394 procedure Add_Implicit_Operator
4395 (T : Entity_Id;
4396 Op_Type : Entity_Id := Empty)
4398 Predef_Op : Entity_Id;
4400 begin
4401 Predef_Op := Current_Entity (Selector_Name (N));
4403 while Present (Predef_Op)
4404 and then Scope (Predef_Op) /= Standard_Standard
4405 loop
4406 Predef_Op := Homonym (Predef_Op);
4407 end loop;
4409 if Nkind (N) = N_Selected_Component then
4410 Change_Selected_Component_To_Expanded_Name (N);
4411 end if;
4413 Add_One_Interp (N, Predef_Op, T);
4415 -- For operators with unary and binary interpretations, add both
4417 if Present (Homonym (Predef_Op)) then
4418 Add_One_Interp (N, Homonym (Predef_Op), T);
4419 end if;
4421 -- The node is a reference to a predefined operator, and
4422 -- an implicit reference to the type of its operands.
4424 if Present (Op_Type) then
4425 Generate_Operator_Reference (N, Op_Type);
4426 else
4427 Generate_Operator_Reference (N, T);
4428 end if;
4429 end Add_Implicit_Operator;
4431 -- Start of processing for Has_Implicit_Operator
4433 begin
4435 if Ekind (P) = E_Package
4436 and then not In_Open_Scopes (P)
4437 then
4438 Priv_Id := First_Private_Entity (P);
4439 end if;
4441 Id := First_Entity (P);
4443 case Op_Id is
4445 -- Boolean operators: an implicit declaration exists if the scope
4446 -- contains a declaration for a derived Boolean type, or for an
4447 -- array of Boolean type.
4449 when Name_Op_And | Name_Op_Not | Name_Op_Or | Name_Op_Xor =>
4451 while Id /= Priv_Id loop
4453 if Valid_Boolean_Arg (Id)
4454 and then Id = Base_Type (Id)
4455 then
4456 Add_Implicit_Operator (Id);
4457 return True;
4458 end if;
4460 Next_Entity (Id);
4461 end loop;
4463 -- Equality: look for any non-limited type. Result is Boolean.
4465 when Name_Op_Eq | Name_Op_Ne =>
4467 while Id /= Priv_Id loop
4469 if Is_Type (Id)
4470 and then not Is_Limited_Type (Id)
4471 and then Id = Base_Type (Id)
4472 then
4473 Add_Implicit_Operator (Standard_Boolean, Id);
4474 return True;
4475 end if;
4477 Next_Entity (Id);
4478 end loop;
4480 -- Comparison operators: scalar type, or array of scalar.
4482 when Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge =>
4484 while Id /= Priv_Id loop
4485 if (Is_Scalar_Type (Id)
4486 or else (Is_Array_Type (Id)
4487 and then Is_Scalar_Type (Component_Type (Id))))
4488 and then Id = Base_Type (Id)
4489 then
4490 Add_Implicit_Operator (Standard_Boolean, Id);
4491 return True;
4492 end if;
4494 Next_Entity (Id);
4495 end loop;
4497 -- Arithmetic operators: any numeric type
4499 when Name_Op_Abs |
4500 Name_Op_Add |
4501 Name_Op_Mod |
4502 Name_Op_Rem |
4503 Name_Op_Subtract |
4504 Name_Op_Multiply |
4505 Name_Op_Divide |
4506 Name_Op_Expon =>
4508 while Id /= Priv_Id loop
4509 if Is_Numeric_Type (Id)
4510 and then Id = Base_Type (Id)
4511 then
4512 Add_Implicit_Operator (Id);
4513 return True;
4514 end if;
4516 Next_Entity (Id);
4517 end loop;
4519 -- Concatenation: any one-dimensional array type
4521 when Name_Op_Concat =>
4523 while Id /= Priv_Id loop
4524 if Is_Array_Type (Id) and then Number_Dimensions (Id) = 1
4525 and then Id = Base_Type (Id)
4526 then
4527 Add_Implicit_Operator (Id);
4528 return True;
4529 end if;
4531 Next_Entity (Id);
4532 end loop;
4534 -- What is the others condition here? Should we be using a
4535 -- subtype of Name_Id that would restrict to operators ???
4537 when others => null;
4539 end case;
4541 -- If we fall through, then we do not have an implicit operator
4543 return False;
4545 end Has_Implicit_Operator;
4547 --------------------
4548 -- In_Open_Scopes --
4549 --------------------
4551 function In_Open_Scopes (S : Entity_Id) return Boolean is
4552 begin
4553 -- Since there are several scope stacks maintained by Scope_Stack each
4554 -- delineated by Standard (see comments by definition of Scope_Stack)
4555 -- it is necessary to end the search when Standard is reached.
4557 for J in reverse 0 .. Scope_Stack.Last loop
4558 if Scope_Stack.Table (J).Entity = S then
4559 return True;
4560 end if;
4562 -- We need Is_Active_Stack_Base to tell us when to stop rather
4563 -- than checking for Standard_Standard because there are cases
4564 -- where Standard_Standard appears in the middle of the active
4565 -- set of scopes. This affects the declaration and overriding
4566 -- of private inherited operations in instantiations of generic
4567 -- child units.
4569 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
4570 end loop;
4572 return False;
4573 end In_Open_Scopes;
4575 -----------------------------
4576 -- Inherit_Renamed_Profile --
4577 -----------------------------
4579 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
4580 New_F : Entity_Id;
4581 Old_F : Entity_Id;
4582 Old_T : Entity_Id;
4583 New_T : Entity_Id;
4585 begin
4586 if Ekind (Old_S) = E_Operator then
4588 New_F := First_Formal (New_S);
4590 while Present (New_F) loop
4591 Set_Etype (New_F, Base_Type (Etype (New_F)));
4592 Next_Formal (New_F);
4593 end loop;
4595 Set_Etype (New_S, Base_Type (Etype (New_S)));
4597 else
4598 New_F := First_Formal (New_S);
4599 Old_F := First_Formal (Old_S);
4601 while Present (New_F) loop
4602 New_T := Etype (New_F);
4603 Old_T := Etype (Old_F);
4605 -- If the new type is a renaming of the old one, as is the
4606 -- case for actuals in instances, retain its name, to simplify
4607 -- later disambiguation.
4609 if Nkind (Parent (New_T)) = N_Subtype_Declaration
4610 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
4611 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
4612 then
4613 null;
4614 else
4615 Set_Etype (New_F, Old_T);
4616 end if;
4618 Next_Formal (New_F);
4619 Next_Formal (Old_F);
4620 end loop;
4622 if Ekind (Old_S) = E_Function
4623 or else Ekind (Old_S) = E_Enumeration_Literal
4624 then
4625 Set_Etype (New_S, Etype (Old_S));
4626 end if;
4627 end if;
4628 end Inherit_Renamed_Profile;
4630 ----------------
4631 -- Initialize --
4632 ----------------
4634 procedure Initialize is
4635 begin
4636 Urefs.Init;
4637 end Initialize;
4639 -------------------------
4640 -- Install_Use_Clauses --
4641 -------------------------
4643 procedure Install_Use_Clauses (Clause : Node_Id) is
4644 U : Node_Id := Clause;
4645 P : Node_Id;
4646 Id : Entity_Id;
4648 begin
4649 while Present (U) loop
4651 -- Case of USE package
4653 if Nkind (U) = N_Use_Package_Clause then
4654 P := First (Names (U));
4656 while Present (P) loop
4657 Id := Entity (P);
4659 if Ekind (Id) = E_Package then
4661 if In_Use (Id) then
4662 Set_Redundant_Use (P, True);
4664 elsif Present (Renamed_Object (Id))
4665 and then In_Use (Renamed_Object (Id))
4666 then
4667 Set_Redundant_Use (P, True);
4669 else
4670 Use_One_Package (Id, U);
4671 end if;
4672 end if;
4674 Next (P);
4675 end loop;
4677 -- case of USE TYPE
4679 else
4680 P := First (Subtype_Marks (U));
4682 while Present (P) loop
4683 if not Is_Entity_Name (P)
4684 or else No (Entity (P))
4685 then
4686 null;
4688 elsif Entity (P) /= Any_Type then
4689 Use_One_Type (P);
4690 end if;
4692 Next (P);
4693 end loop;
4694 end if;
4696 Next_Use_Clause (U);
4697 end loop;
4698 end Install_Use_Clauses;
4700 -------------------------------------
4701 -- Is_Appropriate_For_Entry_Prefix --
4702 -------------------------------------
4704 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
4705 P_Type : Entity_Id := T;
4707 begin
4708 if Is_Access_Type (P_Type) then
4709 P_Type := Designated_Type (P_Type);
4710 end if;
4712 return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
4713 end Is_Appropriate_For_Entry_Prefix;
4715 -------------------------------
4716 -- Is_Appropriate_For_Record --
4717 -------------------------------
4719 function Is_Appropriate_For_Record
4720 (T : Entity_Id)
4721 return Boolean
4723 function Has_Components (T1 : Entity_Id) return Boolean;
4724 -- Determine if given type has components (i.e. is either a record
4725 -- type or a type that has discriminants).
4727 function Has_Components (T1 : Entity_Id) return Boolean is
4728 begin
4729 return Is_Record_Type (T1)
4730 or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
4731 or else (Is_Task_Type (T1) and then Has_Discriminants (T1));
4732 end Has_Components;
4734 -- Start of processing for Is_Appropriate_For_Record
4736 begin
4737 return
4738 Present (T)
4739 and then (Has_Components (T)
4740 or else (Is_Access_Type (T)
4741 and then
4742 Has_Components (Designated_Type (T))));
4743 end Is_Appropriate_For_Record;
4745 ---------------
4746 -- New_Scope --
4747 ---------------
4749 procedure New_Scope (S : Entity_Id) is
4750 E : Entity_Id;
4752 begin
4753 if Ekind (S) = E_Void then
4754 null;
4756 -- Set scope depth if not a non-concurrent type, and we have not
4757 -- yet set the scope depth. This means that we have the first
4758 -- occurrence of the scope, and this is where the depth is set.
4760 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
4761 and then not Scope_Depth_Set (S)
4762 then
4763 if S = Standard_Standard then
4764 Set_Scope_Depth_Value (S, Uint_0);
4766 elsif Is_Child_Unit (S) then
4767 Set_Scope_Depth_Value (S, Uint_1);
4769 elsif not Is_Record_Type (Current_Scope) then
4770 if Ekind (S) = E_Loop then
4771 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
4772 else
4773 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
4774 end if;
4775 end if;
4776 end if;
4778 Scope_Stack.Increment_Last;
4780 declare
4781 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
4783 begin
4784 SST.Entity := S;
4785 SST.Save_Scope_Suppress := Scope_Suppress;
4786 SST.Save_Local_Entity_Suppress := Local_Entity_Suppress.Last;
4788 if Scope_Stack.Last > Scope_Stack.First then
4789 SST.Component_Alignment_Default := Scope_Stack.Table
4790 (Scope_Stack.Last - 1).
4791 Component_Alignment_Default;
4792 end if;
4794 SST.Last_Subprogram_Name := null;
4795 SST.Is_Transient := False;
4796 SST.Node_To_Be_Wrapped := Empty;
4797 SST.Pending_Freeze_Actions := No_List;
4798 SST.Actions_To_Be_Wrapped_Before := No_List;
4799 SST.Actions_To_Be_Wrapped_After := No_List;
4800 SST.First_Use_Clause := Empty;
4801 SST.Is_Active_Stack_Base := False;
4802 end;
4804 if Debug_Flag_W then
4805 Write_Str ("--> new scope: ");
4806 Write_Name (Chars (Current_Scope));
4807 Write_Str (", Id=");
4808 Write_Int (Int (Current_Scope));
4809 Write_Str (", Depth=");
4810 Write_Int (Int (Scope_Stack.Last));
4811 Write_Eol;
4812 end if;
4814 -- Copy from Scope (S) the categorization flags to S, this is not
4815 -- done in case Scope (S) is Standard_Standard since propagation
4816 -- is from library unit entity inwards.
4818 if S /= Standard_Standard
4819 and then Scope (S) /= Standard_Standard
4820 and then not Is_Child_Unit (S)
4821 then
4822 E := Scope (S);
4824 if Nkind (E) not in N_Entity then
4825 return;
4826 end if;
4828 -- We only propagate inwards for library level entities,
4829 -- inner level subprograms do not inherit the categorization.
4831 if Is_Library_Level_Entity (S) then
4832 Set_Is_Preelaborated (S, Is_Preelaborated (E));
4833 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
4834 Set_Categorization_From_Scope (E => S, Scop => E);
4835 end if;
4836 end if;
4837 end New_Scope;
4839 ---------------
4840 -- Pop_Scope --
4841 ---------------
4843 procedure Pop_Scope is
4844 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
4846 begin
4847 if Debug_Flag_E then
4848 Write_Info;
4849 end if;
4851 Scope_Suppress := SST.Save_Scope_Suppress;
4852 Local_Entity_Suppress.Set_Last (SST.Save_Local_Entity_Suppress);
4854 if Debug_Flag_W then
4855 Write_Str ("--> exiting scope: ");
4856 Write_Name (Chars (Current_Scope));
4857 Write_Str (", Depth=");
4858 Write_Int (Int (Scope_Stack.Last));
4859 Write_Eol;
4860 end if;
4862 End_Use_Clauses (SST.First_Use_Clause);
4864 -- If the actions to be wrapped are still there they will get lost
4865 -- causing incomplete code to be generated. It is better to abort in
4866 -- this case (and we do the abort even with assertions off since the
4867 -- penalty is incorrect code generation)
4869 if SST.Actions_To_Be_Wrapped_Before /= No_List
4870 or else
4871 SST.Actions_To_Be_Wrapped_After /= No_List
4872 then
4873 return;
4874 end if;
4876 -- Free last subprogram name if allocated, and pop scope
4878 Free (SST.Last_Subprogram_Name);
4879 Scope_Stack.Decrement_Last;
4880 end Pop_Scope;
4882 ---------------------
4883 -- Premature_Usage --
4884 ---------------------
4886 procedure Premature_Usage (N : Node_Id) is
4887 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
4888 E : Entity_Id := Entity (N);
4890 begin
4891 -- Within an instance, the analysis of the actual for a formal object
4892 -- does not see the name of the object itself. This is significant
4893 -- only if the object is an aggregate, where its analysis does not do
4894 -- any name resolution on component associations. (see 4717-008). In
4895 -- such a case, look for the visible homonym on the chain.
4897 if In_Instance
4898 and then Present (Homonym (E))
4899 then
4900 E := Homonym (E);
4902 while Present (E)
4903 and then not In_Open_Scopes (Scope (E))
4904 loop
4905 E := Homonym (E);
4906 end loop;
4908 if Present (E) then
4909 Set_Entity (N, E);
4910 Set_Etype (N, Etype (E));
4911 return;
4912 end if;
4913 end if;
4915 if Kind = N_Component_Declaration then
4916 Error_Msg_N
4917 ("component&! cannot be used before end of record declaration", N);
4919 elsif Kind = N_Parameter_Specification then
4920 Error_Msg_N
4921 ("formal parameter&! cannot be used before end of specification",
4924 elsif Kind = N_Discriminant_Specification then
4925 Error_Msg_N
4926 ("discriminant&! cannot be used before end of discriminant part",
4929 elsif Kind = N_Procedure_Specification
4930 or else Kind = N_Function_Specification
4931 then
4932 Error_Msg_N
4933 ("subprogram&! cannot be used before end of its declaration",
4935 else
4936 Error_Msg_N
4937 ("object& cannot be used before end of its declaration!", N);
4938 end if;
4939 end Premature_Usage;
4941 ------------------------
4942 -- Present_System_Aux --
4943 ------------------------
4945 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
4946 Loc : Source_Ptr;
4947 Aux_Name : Name_Id;
4948 Unum : Unit_Number_Type;
4949 Withn : Node_Id;
4950 With_Sys : Node_Id;
4951 The_Unit : Node_Id;
4953 function Find_System (C_Unit : Node_Id) return Entity_Id;
4954 -- Scan context clause of compilation unit to find a with_clause
4955 -- for System.
4957 function Find_System (C_Unit : Node_Id) return Entity_Id is
4958 With_Clause : Node_Id;
4960 begin
4961 With_Clause := First (Context_Items (C_Unit));
4963 while Present (With_Clause) loop
4964 if (Nkind (With_Clause) = N_With_Clause
4965 and then Chars (Name (With_Clause)) = Name_System)
4966 and then Comes_From_Source (With_Clause)
4967 then
4968 return With_Clause;
4969 end if;
4971 Next (With_Clause);
4972 end loop;
4974 return Empty;
4975 end Find_System;
4977 -- Start of processing for Present_System_Aux
4979 begin
4980 -- The child unit may have been loaded and analyzed already.
4982 if Present (System_Aux_Id) then
4983 return True;
4985 -- If no previous pragma for System.Aux, nothing to load
4987 elsif No (System_Extend_Unit) then
4988 return False;
4990 -- Use the unit name given in the pragma to retrieve the unit.
4991 -- Verify that System itself appears in the context clause of the
4992 -- current compilation. If System is not present, an error will
4993 -- have been reported already.
4995 else
4996 With_Sys := Find_System (Cunit (Current_Sem_Unit));
4998 The_Unit := Unit (Cunit (Current_Sem_Unit));
5000 if No (With_Sys)
5001 and then (Nkind (The_Unit) = N_Package_Body
5002 or else (Nkind (The_Unit) = N_Subprogram_Body
5003 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
5004 then
5005 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
5006 end if;
5008 if No (With_Sys)
5009 and then Present (N)
5010 then
5011 -- If we are compiling a subunit, we need to examine its
5012 -- context as well (Current_Sem_Unit is the parent unit);
5014 The_Unit := Parent (N);
5016 while Nkind (The_Unit) /= N_Compilation_Unit loop
5017 The_Unit := Parent (The_Unit);
5018 end loop;
5020 if Nkind (Unit (The_Unit)) = N_Subunit then
5021 With_Sys := Find_System (The_Unit);
5022 end if;
5023 end if;
5025 if No (With_Sys) then
5026 return False;
5027 end if;
5029 Loc := Sloc (With_Sys);
5030 Get_Name_String (Chars (Expression (System_Extend_Unit)));
5031 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
5032 Name_Buffer (1 .. 7) := "system.";
5033 Name_Buffer (Name_Len + 8) := '%';
5034 Name_Buffer (Name_Len + 9) := 's';
5035 Name_Len := Name_Len + 9;
5036 Aux_Name := Name_Find;
5038 Unum :=
5039 Load_Unit
5040 (Load_Name => Aux_Name,
5041 Required => False,
5042 Subunit => False,
5043 Error_Node => With_Sys);
5045 if Unum /= No_Unit then
5046 Semantics (Cunit (Unum));
5047 System_Aux_Id :=
5048 Defining_Entity (Specification (Unit (Cunit (Unum))));
5050 Withn := Make_With_Clause (Loc,
5051 Name =>
5052 Make_Expanded_Name (Loc,
5053 Chars => Chars (System_Aux_Id),
5054 Prefix =>
5055 New_Reference_To (Scope (System_Aux_Id), Loc),
5056 Selector_Name =>
5057 New_Reference_To (System_Aux_Id, Loc)));
5059 Set_Entity (Name (Withn), System_Aux_Id);
5061 Set_Library_Unit (Withn, Cunit (Unum));
5062 Set_Corresponding_Spec (Withn, System_Aux_Id);
5063 Set_First_Name (Withn, True);
5064 Set_Implicit_With (Withn, True);
5066 Insert_After (With_Sys, Withn);
5067 Mark_Rewrite_Insertion (Withn);
5068 Set_Context_Installed (Withn);
5070 return True;
5072 -- Here if unit load failed
5074 else
5075 Error_Msg_Name_1 := Name_System;
5076 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
5077 Error_Msg_N
5078 ("extension package `%.%` does not exist",
5079 Opt.System_Extend_Unit);
5080 return False;
5081 end if;
5082 end if;
5083 end Present_System_Aux;
5085 -------------------------
5086 -- Restore_Scope_Stack --
5087 -------------------------
5089 procedure Restore_Scope_Stack (Handle_Use : Boolean := True) is
5090 E : Entity_Id;
5091 S : Entity_Id;
5092 Comp_Unit : Node_Id;
5093 In_Child : Boolean := False;
5094 Full_Vis : Boolean := True;
5095 SS_Last : constant Int := Scope_Stack.Last;
5097 begin
5098 -- Restore visibility of previous scope stack, if any.
5100 for J in reverse 0 .. Scope_Stack.Last loop
5101 exit when Scope_Stack.Table (J).Entity = Standard_Standard
5102 or else No (Scope_Stack.Table (J).Entity);
5104 S := Scope_Stack.Table (J).Entity;
5106 if not Is_Hidden_Open_Scope (S) then
5108 -- If the parent scope is hidden, its entities are hidden as
5109 -- well, unless the entity is the instantiation currently
5110 -- being analyzed.
5112 if not Is_Hidden_Open_Scope (Scope (S))
5113 or else not Analyzed (Parent (S))
5114 or else Scope (S) = Standard_Standard
5115 then
5116 Set_Is_Immediately_Visible (S, True);
5117 end if;
5119 E := First_Entity (S);
5121 while Present (E) loop
5122 if Is_Child_Unit (E) then
5123 Set_Is_Immediately_Visible (E,
5124 Is_Visible_Child_Unit (E) or else In_Open_Scopes (E));
5125 else
5126 Set_Is_Immediately_Visible (E, True);
5127 end if;
5129 Next_Entity (E);
5131 if not Full_Vis then
5132 exit when E = First_Private_Entity (S);
5133 end if;
5134 end loop;
5136 -- The visibility of child units (siblings of current compilation)
5137 -- must be restored in any case. Their declarations may appear
5138 -- after the private part of the parent.
5140 if not Full_Vis
5141 and then Present (E)
5142 then
5143 while Present (E) loop
5144 if Is_Child_Unit (E) then
5145 Set_Is_Immediately_Visible (E,
5146 Is_Visible_Child_Unit (E) or else In_Open_Scopes (E));
5147 end if;
5149 Next_Entity (E);
5150 end loop;
5151 end if;
5152 end if;
5154 if Is_Child_Unit (S)
5155 and not In_Child -- check only for current unit.
5156 then
5157 In_Child := True;
5159 -- restore visibility of parents according to whether the child
5160 -- is private and whether we are in its visible part.
5162 Comp_Unit := Parent (Unit_Declaration_Node (S));
5164 if Nkind (Comp_Unit) = N_Compilation_Unit
5165 and then Private_Present (Comp_Unit)
5166 then
5167 Full_Vis := True;
5169 elsif (Ekind (S) = E_Package
5170 or else Ekind (S) = E_Generic_Package)
5171 and then (In_Private_Part (S)
5172 or else In_Package_Body (S))
5173 then
5174 Full_Vis := True;
5176 elsif (Ekind (S) = E_Procedure
5177 or else Ekind (S) = E_Function)
5178 and then Has_Completion (S)
5179 then
5180 Full_Vis := True;
5181 else
5182 Full_Vis := False;
5183 end if;
5184 else
5185 Full_Vis := True;
5186 end if;
5187 end loop;
5189 if SS_Last >= Scope_Stack.First
5190 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
5191 and then Handle_Use
5192 then
5193 Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
5194 end if;
5195 end Restore_Scope_Stack;
5197 ----------------------
5198 -- Save_Scope_Stack --
5199 ----------------------
5201 procedure Save_Scope_Stack (Handle_Use : Boolean := True) is
5202 E : Entity_Id;
5203 S : Entity_Id;
5204 SS_Last : constant Int := Scope_Stack.Last;
5206 begin
5207 if SS_Last >= Scope_Stack.First
5208 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
5209 then
5210 if Handle_Use then
5211 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
5212 end if;
5214 -- If the call is from within a compilation unit, as when
5215 -- called from Rtsfind, make current entries in scope stack
5216 -- invisible while we analyze the new unit.
5218 for J in reverse 0 .. SS_Last loop
5219 exit when Scope_Stack.Table (J).Entity = Standard_Standard
5220 or else No (Scope_Stack.Table (J).Entity);
5222 S := Scope_Stack.Table (J).Entity;
5223 Set_Is_Immediately_Visible (S, False);
5224 E := First_Entity (S);
5226 while Present (E) loop
5227 Set_Is_Immediately_Visible (E, False);
5228 Next_Entity (E);
5229 end loop;
5230 end loop;
5232 end if;
5233 end Save_Scope_Stack;
5235 -------------
5236 -- Set_Use --
5237 -------------
5239 procedure Set_Use (L : List_Id) is
5240 Decl : Node_Id;
5241 Pack_Name : Node_Id;
5242 Pack : Entity_Id;
5243 Id : Entity_Id;
5245 begin
5246 if Present (L) then
5247 Decl := First (L);
5249 while Present (Decl) loop
5250 if Nkind (Decl) = N_Use_Package_Clause then
5251 Chain_Use_Clause (Decl);
5252 Pack_Name := First (Names (Decl));
5254 while Present (Pack_Name) loop
5255 Pack := Entity (Pack_Name);
5257 if Ekind (Pack) = E_Package
5258 and then Applicable_Use (Pack_Name)
5259 then
5260 Use_One_Package (Pack, Decl);
5261 end if;
5263 Next (Pack_Name);
5264 end loop;
5266 elsif Nkind (Decl) = N_Use_Type_Clause then
5267 Chain_Use_Clause (Decl);
5268 Id := First (Subtype_Marks (Decl));
5270 while Present (Id) loop
5271 if Entity (Id) /= Any_Type then
5272 Use_One_Type (Id);
5273 end if;
5275 Next (Id);
5276 end loop;
5277 end if;
5279 Next (Decl);
5280 end loop;
5281 end if;
5282 end Set_Use;
5284 ---------------------
5285 -- Use_One_Package --
5286 ---------------------
5288 procedure Use_One_Package (P : Entity_Id; N : Node_Id) is
5289 Id : Entity_Id;
5290 Prev : Entity_Id;
5291 Current_Instance : Entity_Id := Empty;
5292 Real_P : Entity_Id;
5294 begin
5295 if Ekind (P) /= E_Package then
5296 return;
5297 end if;
5299 Set_In_Use (P);
5301 -- Ada0Y (AI-50217): Check restriction.
5303 if From_With_Type (P) then
5304 Error_Msg_N ("limited withed package cannot appear in use clause", N);
5305 end if;
5307 -- Find enclosing instance, if any.
5309 if In_Instance then
5310 Current_Instance := Current_Scope;
5312 while not Is_Generic_Instance (Current_Instance) loop
5313 Current_Instance := Scope (Current_Instance);
5314 end loop;
5316 if No (Hidden_By_Use_Clause (N)) then
5317 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
5318 end if;
5319 end if;
5321 -- If unit is a package renaming, indicate that the renamed
5322 -- package is also in use (the flags on both entities must
5323 -- remain consistent, and a subsequent use of either of them
5324 -- should be recognized as redundant).
5326 if Present (Renamed_Object (P)) then
5327 Set_In_Use (Renamed_Object (P));
5328 Real_P := Renamed_Object (P);
5329 else
5330 Real_P := P;
5331 end if;
5333 -- Loop through entities in one package making them potentially
5334 -- use-visible.
5336 Id := First_Entity (P);
5337 while Present (Id)
5338 and then Id /= First_Private_Entity (P)
5339 loop
5340 Prev := Current_Entity (Id);
5342 while Present (Prev) loop
5343 if Is_Immediately_Visible (Prev)
5344 and then (not Is_Overloadable (Prev)
5345 or else not Is_Overloadable (Id)
5346 or else (Type_Conformant (Id, Prev)))
5347 then
5348 if No (Current_Instance) then
5350 -- Potentially use-visible entity remains hidden
5352 goto Next_Usable_Entity;
5354 -- A use clause within an instance hides outer global
5355 -- entities, which are not used to resolve local entities
5356 -- in the instance. Note that the predefined entities in
5357 -- Standard could not have been hidden in the generic by
5358 -- a use clause, and therefore remain visible. Other
5359 -- compilation units whose entities appear in Standard must
5360 -- be hidden in an instance.
5362 -- To determine whether an entity is external to the instance
5363 -- we compare the scope depth of its scope with that of the
5364 -- current instance. However, a generic actual of a subprogram
5365 -- instance is declared in the wrapper package but will not be
5366 -- hidden by a use-visible entity.
5368 elsif not Is_Hidden (Id)
5369 and then not Is_Wrapper_Package (Scope (Prev))
5370 and then Scope_Depth (Scope (Prev)) <
5371 Scope_Depth (Current_Instance)
5372 and then (Scope (Prev) /= Standard_Standard
5373 or else Sloc (Prev) > Standard_Location)
5374 then
5375 Set_Is_Potentially_Use_Visible (Id);
5376 Set_Is_Immediately_Visible (Prev, False);
5377 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
5378 end if;
5380 -- A user-defined operator is not use-visible if the
5381 -- predefined operator for the type is immediately visible,
5382 -- which is the case if the type of the operand is in an open
5383 -- scope. This does not apply to user-defined operators that
5384 -- have operands of different types, because the predefined
5385 -- mixed mode operations (multiplication and division) apply to
5386 -- universal types and do not hide anything.
5388 elsif Ekind (Prev) = E_Operator
5389 and then Operator_Matches_Spec (Prev, Id)
5390 and then In_Open_Scopes
5391 (Scope (Base_Type (Etype (First_Formal (Id)))))
5392 and then (No (Next_Formal (First_Formal (Id)))
5393 or else Etype (First_Formal (Id))
5394 = Etype (Next_Formal (First_Formal (Id)))
5395 or else Chars (Prev) = Name_Op_Expon)
5396 then
5397 goto Next_Usable_Entity;
5398 end if;
5400 Prev := Homonym (Prev);
5401 end loop;
5403 -- On exit, we know entity is not hidden, unless it is private.
5405 if not Is_Hidden (Id)
5406 and then ((not Is_Child_Unit (Id))
5407 or else Is_Visible_Child_Unit (Id))
5408 then
5409 Set_Is_Potentially_Use_Visible (Id);
5411 if Is_Private_Type (Id)
5412 and then Present (Full_View (Id))
5413 then
5414 Set_Is_Potentially_Use_Visible (Full_View (Id));
5415 end if;
5416 end if;
5418 <<Next_Usable_Entity>>
5419 Next_Entity (Id);
5420 end loop;
5422 -- Child units are also made use-visible by a use clause, but they
5423 -- may appear after all visible declarations in the parent entity list.
5425 while Present (Id) loop
5427 if Is_Child_Unit (Id)
5428 and then Is_Visible_Child_Unit (Id)
5429 then
5430 Set_Is_Potentially_Use_Visible (Id);
5431 end if;
5433 Next_Entity (Id);
5434 end loop;
5436 if Chars (Real_P) = Name_System
5437 and then Scope (Real_P) = Standard_Standard
5438 and then Present_System_Aux (N)
5439 then
5440 Use_One_Package (System_Aux_Id, N);
5441 end if;
5443 end Use_One_Package;
5445 ------------------
5446 -- Use_One_Type --
5447 ------------------
5449 procedure Use_One_Type (Id : Node_Id) is
5450 T : Entity_Id;
5451 Op_List : Elist_Id;
5452 Elmt : Elmt_Id;
5454 begin
5455 -- It is the type determined by the subtype mark (8.4(8)) whose
5456 -- operations become potentially use-visible.
5458 T := Base_Type (Entity (Id));
5460 Set_Redundant_Use
5461 (Id,
5462 In_Use (T)
5463 or else Is_Potentially_Use_Visible (T)
5464 or else In_Use (Scope (T)));
5466 if In_Open_Scopes (Scope (T)) then
5467 null;
5469 -- If the subtype mark designates a subtype in a different package,
5470 -- we have to check that the parent type is visible, otherwise the
5471 -- use type clause is a noop. Not clear how to do that???
5473 elsif not Redundant_Use (Id) then
5474 Set_In_Use (T);
5475 Op_List := Collect_Primitive_Operations (T);
5476 Elmt := First_Elmt (Op_List);
5478 while Present (Elmt) loop
5480 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
5481 or else Chars (Node (Elmt)) in Any_Operator_Name)
5482 and then not Is_Hidden (Node (Elmt))
5483 then
5484 Set_Is_Potentially_Use_Visible (Node (Elmt));
5485 end if;
5487 Next_Elmt (Elmt);
5488 end loop;
5489 end if;
5490 end Use_One_Type;
5492 ----------------
5493 -- Write_Info --
5494 ----------------
5496 procedure Write_Info is
5497 Id : Entity_Id := First_Entity (Current_Scope);
5499 begin
5500 -- No point in dumping standard entities
5502 if Current_Scope = Standard_Standard then
5503 return;
5504 end if;
5506 Write_Str ("========================================================");
5507 Write_Eol;
5508 Write_Str (" Defined Entities in ");
5509 Write_Name (Chars (Current_Scope));
5510 Write_Eol;
5511 Write_Str ("========================================================");
5512 Write_Eol;
5514 if No (Id) then
5515 Write_Str ("-- none --");
5516 Write_Eol;
5518 else
5519 while Present (Id) loop
5520 Write_Entity_Info (Id, " ");
5521 Next_Entity (Id);
5522 end loop;
5523 end if;
5525 if Scope (Current_Scope) = Standard_Standard then
5527 -- Print information on the current unit itself
5529 Write_Entity_Info (Current_Scope, " ");
5530 end if;
5532 Write_Eol;
5533 end Write_Info;
5535 -----------------
5536 -- Write_Scopes --
5537 -----------------
5539 procedure Write_Scopes is
5540 S : Entity_Id;
5542 begin
5543 for J in reverse 1 .. Scope_Stack.Last loop
5544 S := Scope_Stack.Table (J).Entity;
5545 Write_Int (Int (S));
5546 Write_Str (" === ");
5547 Write_Name (Chars (S));
5548 Write_Eol;
5549 end loop;
5550 end Write_Scopes;
5552 end Sem_Ch8;