* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / gcc / ada / sem_ch8.adb
blob27ccc2d3d1e772943508eeae34348a43d827ab72
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-2013, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Tss; use Exp_Tss;
32 with Exp_Util; use Exp_Util;
33 with Fname; use Fname;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Namet.Sp; use Namet.Sp;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch4; use Sem_Ch4;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch12; use Sem_Ch12;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sem_Type; use Sem_Type;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinfo.CN; use Sinfo.CN;
66 with Snames; use Snames;
67 with Style; use Style;
68 with Table;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
73 package body Sem_Ch8 is
75 ------------------------------------
76 -- Visibility and Name Resolution --
77 ------------------------------------
79 -- This package handles name resolution and the collection of possible
80 -- interpretations for overloaded names, prior to overload resolution.
82 -- Name resolution is the process that establishes a mapping between source
83 -- identifiers and the entities they denote at each point in the program.
84 -- Each entity is represented by a defining occurrence. Each identifier
85 -- that denotes an entity points to the corresponding defining occurrence.
86 -- This is the entity of the applied occurrence. Each occurrence holds
87 -- an index into the names table, where source identifiers are stored.
89 -- Each entry in the names table for an identifier or designator uses the
90 -- Info pointer to hold a link to the currently visible entity that has
91 -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
92 -- in package Sem_Util). The visibility is initialized at the beginning of
93 -- semantic processing to make entities in package Standard immediately
94 -- visible. The visibility table is used in a more subtle way when
95 -- compiling subunits (see below).
97 -- Entities that have the same name (i.e. homonyms) are chained. In the
98 -- case of overloaded entities, this chain holds all the possible meanings
99 -- of a given identifier. The process of overload resolution uses type
100 -- information to select from this chain the unique meaning of a given
101 -- identifier.
103 -- Entities are also chained in their scope, through the Next_Entity link.
104 -- As a consequence, the name space is organized as a sparse matrix, where
105 -- each row corresponds to a scope, and each column to a source identifier.
106 -- Open scopes, that is to say scopes currently being compiled, have their
107 -- corresponding rows of entities in order, innermost scope first.
109 -- The scopes of packages that are mentioned in context clauses appear in
110 -- no particular order, interspersed among open scopes. This is because
111 -- in the course of analyzing the context of a compilation, a package
112 -- declaration is first an open scope, and subsequently an element of the
113 -- context. If subunits or child units are present, a parent unit may
114 -- appear under various guises at various times in the compilation.
116 -- When the compilation of the innermost scope is complete, the entities
117 -- defined therein are no longer visible. If the scope is not a package
118 -- declaration, these entities are never visible subsequently, and can be
119 -- removed from visibility chains. If the scope is a package declaration,
120 -- its visible declarations may still be accessible. Therefore the entities
121 -- defined in such a scope are left on the visibility chains, and only
122 -- their visibility (immediately visibility or potential use-visibility)
123 -- is affected.
125 -- The ordering of homonyms on their chain does not necessarily follow
126 -- the order of their corresponding scopes on the scope stack. For
127 -- example, if package P and the enclosing scope both contain entities
128 -- named E, then when compiling the package body the chain for E will
129 -- hold the global entity first, and the local one (corresponding to
130 -- the current inner scope) next. As a result, name resolution routines
131 -- do not assume any relative ordering of the homonym chains, either
132 -- for scope nesting or to order of appearance of context clauses.
134 -- When compiling a child unit, entities in the parent scope are always
135 -- immediately visible. When compiling the body of a child unit, private
136 -- entities in the parent must also be made immediately visible. There
137 -- are separate routines to make the visible and private declarations
138 -- visible at various times (see package Sem_Ch7).
140 -- +--------+ +-----+
141 -- | In use |-------->| EU1 |-------------------------->
142 -- +--------+ +-----+
143 -- | |
144 -- +--------+ +-----+ +-----+
145 -- | Stand. |---------------->| ES1 |--------------->| ES2 |--->
146 -- +--------+ +-----+ +-----+
147 -- | |
148 -- +---------+ | +-----+
149 -- | with'ed |------------------------------>| EW2 |--->
150 -- +---------+ | +-----+
151 -- | |
152 -- +--------+ +-----+ +-----+
153 -- | Scope2 |---------------->| E12 |--------------->| E22 |--->
154 -- +--------+ +-----+ +-----+
155 -- | |
156 -- +--------+ +-----+ +-----+
157 -- | Scope1 |---------------->| E11 |--------------->| E12 |--->
158 -- +--------+ +-----+ +-----+
159 -- ^ | |
160 -- | | |
161 -- | +---------+ | |
162 -- | | with'ed |----------------------------------------->
163 -- | +---------+ | |
164 -- | | |
165 -- Scope stack | |
166 -- (innermost first) | |
167 -- +----------------------------+
168 -- Names table => | Id1 | | | | Id2 |
169 -- +----------------------------+
171 -- Name resolution must deal with several syntactic forms: simple names,
172 -- qualified names, indexed names, and various forms of calls.
174 -- Each identifier points to an entry in the names table. The resolution
175 -- of a simple name consists in traversing the homonym chain, starting
176 -- from the names table. If an entry is immediately visible, it is the one
177 -- designated by the identifier. If only potentially use-visible entities
178 -- are on the chain, we must verify that they do not hide each other. If
179 -- the entity we find is overloadable, we collect all other overloadable
180 -- entities on the chain as long as they are not hidden.
182 -- To resolve expanded names, we must find the entity at the intersection
183 -- of the entity chain for the scope (the prefix) and the homonym chain
184 -- for the selector. In general, homonym chains will be much shorter than
185 -- entity chains, so it is preferable to start from the names table as
186 -- well. If the entity found is overloadable, we must collect all other
187 -- interpretations that are defined in the scope denoted by the prefix.
189 -- For records, protected types, and tasks, their local entities are
190 -- removed from visibility chains on exit from the corresponding scope.
191 -- From the outside, these entities are always accessed by selected
192 -- notation, and the entity chain for the record type, protected type,
193 -- etc. is traversed sequentially in order to find the designated entity.
195 -- The discriminants of a type and the operations of a protected type or
196 -- task are unchained on exit from the first view of the type, (such as
197 -- a private or incomplete type declaration, or a protected type speci-
198 -- fication) and re-chained when compiling the second view.
200 -- In the case of operators, we do not make operators on derived types
201 -- explicit. As a result, the notation P."+" may denote either a user-
202 -- defined function with name "+", or else an implicit declaration of the
203 -- operator "+" in package P. The resolution of expanded names always
204 -- tries to resolve an operator name as such an implicitly defined entity,
205 -- in addition to looking for explicit declarations.
207 -- All forms of names that denote entities (simple names, expanded names,
208 -- character literals in some cases) have a Entity attribute, which
209 -- identifies the entity denoted by the name.
211 ---------------------
212 -- The Scope Stack --
213 ---------------------
215 -- The Scope stack keeps track of the scopes currently been compiled.
216 -- Every entity that contains declarations (including records) is placed
217 -- on the scope stack while it is being processed, and removed at the end.
218 -- Whenever a non-package scope is exited, the entities defined therein
219 -- are removed from the visibility table, so that entities in outer scopes
220 -- become visible (see previous description). On entry to Sem, the scope
221 -- stack only contains the package Standard. As usual, subunits complicate
222 -- this picture ever so slightly.
224 -- The Rtsfind mechanism can force a call to Semantics while another
225 -- compilation is in progress. The unit retrieved by Rtsfind must be
226 -- compiled in its own context, and has no access to the visibility of
227 -- the unit currently being compiled. The procedures Save_Scope_Stack and
228 -- Restore_Scope_Stack make entities in current open scopes invisible
229 -- before compiling the retrieved unit, and restore the compilation
230 -- environment afterwards.
232 ------------------------
233 -- Compiling subunits --
234 ------------------------
236 -- Subunits must be compiled in the environment of the corresponding stub,
237 -- that is to say with the same visibility into the parent (and its
238 -- context) that is available at the point of the stub declaration, but
239 -- with the additional visibility provided by the context clause of the
240 -- subunit itself. As a result, compilation of a subunit forces compilation
241 -- of the parent (see description in lib-). At the point of the stub
242 -- declaration, Analyze is called recursively to compile the proper body of
243 -- the subunit, but without reinitializing the names table, nor the scope
244 -- stack (i.e. standard is not pushed on the stack). In this fashion the
245 -- context of the subunit is added to the context of the parent, and the
246 -- subunit is compiled in the correct environment. Note that in the course
247 -- of processing the context of a subunit, Standard will appear twice on
248 -- the scope stack: once for the parent of the subunit, and once for the
249 -- unit in the context clause being compiled. However, the two sets of
250 -- entities are not linked by homonym chains, so that the compilation of
251 -- any context unit happens in a fresh visibility environment.
253 -------------------------------
254 -- Processing of USE Clauses --
255 -------------------------------
257 -- Every defining occurrence has a flag indicating if it is potentially use
258 -- visible. Resolution of simple names examines this flag. The processing
259 -- of use clauses consists in setting this flag on all visible entities
260 -- defined in the corresponding package. On exit from the scope of the use
261 -- clause, the corresponding flag must be reset. However, a package may
262 -- appear in several nested use clauses (pathological but legal, alas!)
263 -- which forces us to use a slightly more involved scheme:
265 -- a) The defining occurrence for a package holds a flag -In_Use- to
266 -- indicate that it is currently in the scope of a use clause. If a
267 -- redundant use clause is encountered, then the corresponding occurrence
268 -- of the package name is flagged -Redundant_Use-.
270 -- b) On exit from a scope, the use clauses in its declarative part are
271 -- scanned. The visibility flag is reset in all entities declared in
272 -- package named in a use clause, as long as the package is not flagged
273 -- as being in a redundant use clause (in which case the outer use
274 -- clause is still in effect, and the direct visibility of its entities
275 -- must be retained).
277 -- Note that entities are not removed from their homonym chains on exit
278 -- from the package specification. A subsequent use clause does not need
279 -- to rechain the visible entities, but only to establish their direct
280 -- visibility.
282 -----------------------------------
283 -- Handling private declarations --
284 -----------------------------------
286 -- The principle that each entity has a single defining occurrence clashes
287 -- with the presence of two separate definitions for private types: the
288 -- first is the private type declaration, and second is the full type
289 -- declaration. It is important that all references to the type point to
290 -- the same defining occurrence, namely the first one. To enforce the two
291 -- separate views of the entity, the corresponding information is swapped
292 -- between the two declarations. Outside of the package, the defining
293 -- occurrence only contains the private declaration information, while in
294 -- the private part and the body of the package the defining occurrence
295 -- contains the full declaration. To simplify the swap, the defining
296 -- occurrence that currently holds the private declaration points to the
297 -- full declaration. During semantic processing the defining occurrence
298 -- also points to a list of private dependents, that is to say access types
299 -- or composite types whose designated types or component types are
300 -- subtypes or derived types of the private type in question. After the
301 -- full declaration has been seen, the private dependents are updated to
302 -- indicate that they have full definitions.
304 ------------------------------------
305 -- Handling of Undefined Messages --
306 ------------------------------------
308 -- In normal mode, only the first use of an undefined identifier generates
309 -- a message. The table Urefs is used to record error messages that have
310 -- been issued so that second and subsequent ones do not generate further
311 -- messages. However, the second reference causes text to be added to the
312 -- original undefined message noting "(more references follow)". The
313 -- full error list option (-gnatf) forces messages to be generated for
314 -- every reference and disconnects the use of this table.
316 type Uref_Entry is record
317 Node : Node_Id;
318 -- Node for identifier for which original message was posted. The
319 -- Chars field of this identifier is used to detect later references
320 -- to the same identifier.
322 Err : Error_Msg_Id;
323 -- Records error message Id of original undefined message. Reset to
324 -- No_Error_Msg after the second occurrence, where it is used to add
325 -- text to the original message as described above.
327 Nvis : Boolean;
328 -- Set if the message is not visible rather than undefined
330 Loc : Source_Ptr;
331 -- Records location of error message. Used to make sure that we do
332 -- not consider a, b : undefined as two separate instances, which
333 -- would otherwise happen, since the parser converts this sequence
334 -- to a : undefined; b : undefined.
336 end record;
338 package Urefs is new Table.Table (
339 Table_Component_Type => Uref_Entry,
340 Table_Index_Type => Nat,
341 Table_Low_Bound => 1,
342 Table_Initial => 10,
343 Table_Increment => 100,
344 Table_Name => "Urefs");
346 Candidate_Renaming : Entity_Id;
347 -- Holds a candidate interpretation that appears in a subprogram renaming
348 -- declaration and does not match the given specification, but matches at
349 -- least on the first formal. Allows better error message when given
350 -- specification omits defaulted parameters, a common error.
352 -----------------------
353 -- Local Subprograms --
354 -----------------------
356 procedure Analyze_Generic_Renaming
357 (N : Node_Id;
358 K : Entity_Kind);
359 -- Common processing for all three kinds of generic renaming declarations.
360 -- Enter new name and indicate that it renames the generic unit.
362 procedure Analyze_Renamed_Character
363 (N : Node_Id;
364 New_S : Entity_Id;
365 Is_Body : Boolean);
366 -- Renamed entity is given by a character literal, which must belong
367 -- to the return type of the new entity. Is_Body indicates whether the
368 -- declaration is a renaming_as_body. If the original declaration has
369 -- already been frozen (because of an intervening body, e.g.) the body of
370 -- the function must be built now. The same applies to the following
371 -- various renaming procedures.
373 procedure Analyze_Renamed_Dereference
374 (N : Node_Id;
375 New_S : Entity_Id;
376 Is_Body : Boolean);
377 -- Renamed entity is given by an explicit dereference. Prefix must be a
378 -- conformant access_to_subprogram type.
380 procedure Analyze_Renamed_Entry
381 (N : Node_Id;
382 New_S : Entity_Id;
383 Is_Body : Boolean);
384 -- If the renamed entity in a subprogram renaming is an entry or protected
385 -- subprogram, build a body for the new entity whose only statement is a
386 -- call to the renamed entity.
388 procedure Analyze_Renamed_Family_Member
389 (N : Node_Id;
390 New_S : Entity_Id;
391 Is_Body : Boolean);
392 -- Used when the renamed entity is an indexed component. The prefix must
393 -- denote an entry family.
395 procedure Analyze_Renamed_Primitive_Operation
396 (N : Node_Id;
397 New_S : Entity_Id;
398 Is_Body : Boolean);
399 -- If the renamed entity in a subprogram renaming is a primitive operation
400 -- or a class-wide operation in prefix form, save the target object,
401 -- which must be added to the list of actuals in any subsequent call.
402 -- The renaming operation is intrinsic because the compiler must in
403 -- fact generate a wrapper for it (6.3.1 (10 1/2)).
405 function Applicable_Use (Pack_Name : Node_Id) return Boolean;
406 -- Common code to Use_One_Package and Set_Use, to determine whether use
407 -- clause must be processed. Pack_Name is an entity name that references
408 -- the package in question.
410 procedure Attribute_Renaming (N : Node_Id);
411 -- Analyze renaming of attribute as subprogram. The renaming declaration N
412 -- is rewritten as a subprogram body that returns the attribute reference
413 -- applied to the formals of the function.
415 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
416 -- Set Entity, with style check if need be. For a discriminant reference,
417 -- replace by the corresponding discriminal, i.e. the parameter of the
418 -- initialization procedure that corresponds to the discriminant.
420 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
421 -- A renaming_as_body may occur after the entity of the original decla-
422 -- ration has been frozen. In that case, the body of the new entity must
423 -- be built now, because the usual mechanism of building the renamed
424 -- body at the point of freezing will not work. Subp is the subprogram
425 -- for which N provides the Renaming_As_Body.
427 procedure Check_In_Previous_With_Clause
428 (N : Node_Id;
429 Nam : Node_Id);
430 -- N is a use_package clause and Nam the package name, or N is a use_type
431 -- clause and Nam is the prefix of the type name. In either case, verify
432 -- that the package is visible at that point in the context: either it
433 -- appears in a previous with_clause, or because it is a fully qualified
434 -- name and the root ancestor appears in a previous with_clause.
436 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
437 -- Verify that the entity in a renaming declaration that is a library unit
438 -- is itself a library unit and not a nested unit or subunit. Also check
439 -- that if the renaming is a child unit of a generic parent, then the
440 -- renamed unit must also be a child unit of that parent. Finally, verify
441 -- that a renamed generic unit is not an implicit child declared within
442 -- an instance of the parent.
444 procedure Chain_Use_Clause (N : Node_Id);
445 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
446 -- the proper scope table entry. This is usually the current scope, but it
447 -- will be an inner scope when installing the use clauses of the private
448 -- declarations of a parent unit prior to compiling the private part of a
449 -- child unit. This chain is traversed when installing/removing use clauses
450 -- when compiling a subunit or instantiating a generic body on the fly,
451 -- when it is necessary to save and restore full environments.
453 function Enclosing_Instance return Entity_Id;
454 -- In an instance nested within another one, several semantic checks are
455 -- unnecessary because the legality of the nested instance has been checked
456 -- in the enclosing generic unit. This applies in particular to legality
457 -- checks on actuals for formal subprograms of the inner instance, which
458 -- are checked as subprogram renamings, and may be complicated by confusion
459 -- in private/full views. This function returns the instance enclosing the
460 -- current one if there is such, else it returns Empty.
462 -- If the renaming determines the entity for the default of a formal
463 -- subprogram nested within another instance, choose the innermost
464 -- candidate. This is because if the formal has a box, and we are within
465 -- an enclosing instance where some candidate interpretations are local
466 -- to this enclosing instance, we know that the default was properly
467 -- resolved when analyzing the generic, so we prefer the local
468 -- candidates to those that are external. This is not always the case
469 -- but is a reasonable heuristic on the use of nested generics. The
470 -- proper solution requires a full renaming model.
472 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
473 -- Find a type derived from Character or Wide_Character in the prefix of N.
474 -- Used to resolved qualified names whose selector is a character literal.
476 function Has_Private_With (E : Entity_Id) return Boolean;
477 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
478 -- private with on E.
480 procedure Find_Expanded_Name (N : Node_Id);
481 -- The input is a selected component known to be an expanded name. Verify
482 -- legality of selector given the scope denoted by prefix, and change node
483 -- N into a expanded name with a properly set Entity field.
485 function Find_Renamed_Entity
486 (N : Node_Id;
487 Nam : Node_Id;
488 New_S : Entity_Id;
489 Is_Actual : Boolean := False) return Entity_Id;
490 -- Find the renamed entity that corresponds to the given parameter profile
491 -- in a subprogram renaming declaration. The renamed entity may be an
492 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
493 -- indicates that the renaming is the one generated for an actual subpro-
494 -- gram in an instance, for which special visibility checks apply.
496 function Has_Implicit_Operator (N : Node_Id) return Boolean;
497 -- N is an expanded name whose selector is an operator name (e.g. P."+").
498 -- declarative part contains an implicit declaration of an operator if it
499 -- has a declaration of a type to which one of the predefined operators
500 -- apply. The existence of this routine is an implementation artifact. A
501 -- more straightforward but more space-consuming choice would be to make
502 -- all inherited operators explicit in the symbol table.
504 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
505 -- A subprogram defined by a renaming declaration inherits the parameter
506 -- profile of the renamed entity. The subtypes given in the subprogram
507 -- specification are discarded and replaced with those of the renamed
508 -- subprogram, which are then used to recheck the default values.
510 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean;
511 -- Prefix is appropriate for record if it is of a record type, or an access
512 -- to such.
514 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean;
515 -- True if it is of a task type, a protected type, or else an access to one
516 -- of these types.
518 procedure Note_Redundant_Use (Clause : Node_Id);
519 -- Mark the name in a use clause as redundant if the corresponding entity
520 -- is already use-visible. Emit a warning if the use clause comes from
521 -- source and the proper warnings are enabled.
523 procedure Premature_Usage (N : Node_Id);
524 -- Diagnose usage of an entity before it is visible
526 procedure Use_One_Package (P : Entity_Id; N : Node_Id);
527 -- Make visible entities declared in package P potentially use-visible
528 -- in the current context. Also used in the analysis of subunits, when
529 -- re-installing use clauses of parent units. N is the use_clause that
530 -- names P (and possibly other packages).
532 procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False);
533 -- Id is the subtype mark from a use type clause. This procedure makes
534 -- the primitive operators of the type potentially use-visible. The
535 -- boolean flag Installed indicates that the clause is being reinstalled
536 -- after previous analysis, and primitive operations are already chained
537 -- on the Used_Operations list of the clause.
539 procedure Write_Info;
540 -- Write debugging information on entities declared in current scope
542 --------------------------------
543 -- Analyze_Exception_Renaming --
544 --------------------------------
546 -- The language only allows a single identifier, but the tree holds an
547 -- identifier list. The parser has already issued an error message if
548 -- there is more than one element in the list.
550 procedure Analyze_Exception_Renaming (N : Node_Id) is
551 Id : constant Node_Id := Defining_Identifier (N);
552 Nam : constant Node_Id := Name (N);
554 begin
555 Check_SPARK_Restriction ("exception renaming is not allowed", N);
557 Enter_Name (Id);
558 Analyze (Nam);
560 Set_Ekind (Id, E_Exception);
561 Set_Exception_Code (Id, Uint_0);
562 Set_Etype (Id, Standard_Exception_Type);
563 Set_Is_Pure (Id, Is_Pure (Current_Scope));
565 if not Is_Entity_Name (Nam) or else
566 Ekind (Entity (Nam)) /= E_Exception
567 then
568 Error_Msg_N ("invalid exception name in renaming", Nam);
569 else
570 if Present (Renamed_Object (Entity (Nam))) then
571 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
572 else
573 Set_Renamed_Object (Id, Entity (Nam));
574 end if;
575 end if;
577 -- Implementation-defined aspect specifications can appear in a renaming
578 -- declaration, but not language-defined ones. The call to procedure
579 -- Analyze_Aspect_Specifications will take care of this error check.
581 if Has_Aspects (N) then
582 Analyze_Aspect_Specifications (N, Id);
583 end if;
584 end Analyze_Exception_Renaming;
586 ---------------------------
587 -- Analyze_Expanded_Name --
588 ---------------------------
590 procedure Analyze_Expanded_Name (N : Node_Id) is
591 begin
592 -- If the entity pointer is already set, this is an internal node, or a
593 -- node that is analyzed more than once, after a tree modification. In
594 -- such a case there is no resolution to perform, just set the type. For
595 -- completeness, analyze prefix as well.
597 if Present (Entity (N)) then
598 if Is_Type (Entity (N)) then
599 Set_Etype (N, Entity (N));
600 else
601 Set_Etype (N, Etype (Entity (N)));
602 end if;
604 Analyze (Prefix (N));
605 return;
606 else
607 Find_Expanded_Name (N);
608 end if;
610 Analyze_Dimension (N);
611 end Analyze_Expanded_Name;
613 ---------------------------------------
614 -- Analyze_Generic_Function_Renaming --
615 ---------------------------------------
617 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
618 begin
619 Analyze_Generic_Renaming (N, E_Generic_Function);
620 end Analyze_Generic_Function_Renaming;
622 --------------------------------------
623 -- Analyze_Generic_Package_Renaming --
624 --------------------------------------
626 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
627 begin
628 -- Apply the Text_IO Kludge here, since we may be renaming one of the
629 -- subpackages of Text_IO, then join common routine.
631 Text_IO_Kludge (Name (N));
633 Analyze_Generic_Renaming (N, E_Generic_Package);
634 end Analyze_Generic_Package_Renaming;
636 ----------------------------------------
637 -- Analyze_Generic_Procedure_Renaming --
638 ----------------------------------------
640 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
641 begin
642 Analyze_Generic_Renaming (N, E_Generic_Procedure);
643 end Analyze_Generic_Procedure_Renaming;
645 ------------------------------
646 -- Analyze_Generic_Renaming --
647 ------------------------------
649 procedure Analyze_Generic_Renaming
650 (N : Node_Id;
651 K : Entity_Kind)
653 New_P : constant Entity_Id := Defining_Entity (N);
654 Old_P : Entity_Id;
655 Inst : Boolean := False; -- prevent junk warning
657 begin
658 if Name (N) = Error then
659 return;
660 end if;
662 Check_SPARK_Restriction ("generic renaming is not allowed", N);
664 Generate_Definition (New_P);
666 if Current_Scope /= Standard_Standard then
667 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
668 end if;
670 if Nkind (Name (N)) = N_Selected_Component then
671 Check_Generic_Child_Unit (Name (N), Inst);
672 else
673 Analyze (Name (N));
674 end if;
676 if not Is_Entity_Name (Name (N)) then
677 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
678 Old_P := Any_Id;
679 else
680 Old_P := Entity (Name (N));
681 end if;
683 Enter_Name (New_P);
684 Set_Ekind (New_P, K);
686 if Etype (Old_P) = Any_Type then
687 null;
689 elsif Ekind (Old_P) /= K then
690 Error_Msg_N ("invalid generic unit name", Name (N));
692 else
693 if Present (Renamed_Object (Old_P)) then
694 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
695 else
696 Set_Renamed_Object (New_P, Old_P);
697 end if;
699 Set_Is_Pure (New_P, Is_Pure (Old_P));
700 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
702 Set_Etype (New_P, Etype (Old_P));
703 Set_Has_Completion (New_P);
705 if In_Open_Scopes (Old_P) then
706 Error_Msg_N ("within its scope, generic denotes its instance", N);
707 end if;
709 Check_Library_Unit_Renaming (N, Old_P);
710 end if;
712 -- Implementation-defined aspect specifications can appear in a renaming
713 -- declaration, but not language-defined ones. The call to procedure
714 -- Analyze_Aspect_Specifications will take care of this error check.
716 if Has_Aspects (N) then
717 Analyze_Aspect_Specifications (N, New_P);
718 end if;
719 end Analyze_Generic_Renaming;
721 -----------------------------
722 -- Analyze_Object_Renaming --
723 -----------------------------
725 procedure Analyze_Object_Renaming (N : Node_Id) is
726 Loc : constant Source_Ptr := Sloc (N);
727 Id : constant Entity_Id := Defining_Identifier (N);
728 Dec : Node_Id;
729 Nam : constant Node_Id := Name (N);
730 T : Entity_Id;
731 T2 : Entity_Id;
733 procedure Check_Constrained_Object;
734 -- If the nominal type is unconstrained but the renamed object is
735 -- constrained, as can happen with renaming an explicit dereference or
736 -- a function return, build a constrained subtype from the object. If
737 -- the renaming is for a formal in an accept statement, the analysis
738 -- has already established its actual subtype. This is only relevant
739 -- if the renamed object is an explicit dereference.
741 function In_Generic_Scope (E : Entity_Id) return Boolean;
742 -- Determine whether entity E is inside a generic cope
744 ------------------------------
745 -- Check_Constrained_Object --
746 ------------------------------
748 procedure Check_Constrained_Object is
749 Typ : constant Entity_Id := Etype (Nam);
750 Subt : Entity_Id;
752 begin
753 if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference)
754 and then Is_Composite_Type (Etype (Nam))
755 and then not Is_Constrained (Etype (Nam))
756 and then not Has_Unknown_Discriminants (Etype (Nam))
757 and then Expander_Active
758 then
759 -- If Actual_Subtype is already set, nothing to do
761 if Ekind_In (Id, E_Variable, E_Constant)
762 and then Present (Actual_Subtype (Id))
763 then
764 null;
766 -- A renaming of an unchecked union has no actual subtype
768 elsif Is_Unchecked_Union (Typ) then
769 null;
771 -- If a record is limited its size is invariant. This is the case
772 -- in particular with record types with an access discirminant
773 -- that are used in iterators. This is an optimization, but it
774 -- also prevents typing anomalies when the prefix is further
775 -- expanded. Limited types with discriminants are included.
777 elsif Is_Limited_Record (Typ)
778 or else
779 (Ekind (Typ) = E_Limited_Private_Type
780 and then Has_Discriminants (Typ)
781 and then Is_Access_Type (Etype (First_Discriminant (Typ))))
782 then
783 null;
785 else
786 Subt := Make_Temporary (Loc, 'T');
787 Remove_Side_Effects (Nam);
788 Insert_Action (N,
789 Make_Subtype_Declaration (Loc,
790 Defining_Identifier => Subt,
791 Subtype_Indication =>
792 Make_Subtype_From_Expr (Nam, Typ)));
793 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
794 Set_Etype (Nam, Subt);
796 -- Freeze subtype at once, to prevent order of elaboration
797 -- issues in the backend. The renamed object exists, so its
798 -- type is already frozen in any case.
800 Freeze_Before (N, Subt);
801 end if;
802 end if;
803 end Check_Constrained_Object;
805 ----------------------
806 -- In_Generic_Scope --
807 ----------------------
809 function In_Generic_Scope (E : Entity_Id) return Boolean is
810 S : Entity_Id;
812 begin
813 S := Scope (E);
814 while Present (S) and then S /= Standard_Standard loop
815 if Is_Generic_Unit (S) then
816 return True;
817 end if;
819 S := Scope (S);
820 end loop;
822 return False;
823 end In_Generic_Scope;
825 -- Start of processing for Analyze_Object_Renaming
827 begin
828 if Nam = Error then
829 return;
830 end if;
832 Check_SPARK_Restriction ("object renaming is not allowed", N);
834 Set_Is_Pure (Id, Is_Pure (Current_Scope));
835 Enter_Name (Id);
837 -- The renaming of a component that depends on a discriminant requires
838 -- an actual subtype, because in subsequent use of the object Gigi will
839 -- be unable to locate the actual bounds. This explicit step is required
840 -- when the renaming is generated in removing side effects of an
841 -- already-analyzed expression.
843 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
844 T := Etype (Nam);
845 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
847 if Present (Dec) then
848 Insert_Action (N, Dec);
849 T := Defining_Identifier (Dec);
850 Set_Etype (Nam, T);
851 end if;
853 -- Complete analysis of the subtype mark in any case, for ASIS use
855 if Present (Subtype_Mark (N)) then
856 Find_Type (Subtype_Mark (N));
857 end if;
859 elsif Present (Subtype_Mark (N)) then
860 Find_Type (Subtype_Mark (N));
861 T := Entity (Subtype_Mark (N));
862 Analyze (Nam);
864 -- Reject renamings of conversions unless the type is tagged, or
865 -- the conversion is implicit (which can occur for cases of anonymous
866 -- access types in Ada 2012).
868 if Nkind (Nam) = N_Type_Conversion
869 and then Comes_From_Source (Nam)
870 and then not Is_Tagged_Type (T)
871 then
872 Error_Msg_N
873 ("renaming of conversion only allowed for tagged types", Nam);
874 end if;
876 Resolve (Nam, T);
878 -- If the renamed object is a function call of a limited type,
879 -- the expansion of the renaming is complicated by the presence
880 -- of various temporaries and subtypes that capture constraints
881 -- of the renamed object. Rewrite node as an object declaration,
882 -- whose expansion is simpler. Given that the object is limited
883 -- there is no copy involved and no performance hit.
885 if Nkind (Nam) = N_Function_Call
886 and then Is_Immutably_Limited_Type (Etype (Nam))
887 and then not Is_Constrained (Etype (Nam))
888 and then Comes_From_Source (N)
889 then
890 Set_Etype (Id, T);
891 Set_Ekind (Id, E_Constant);
892 Rewrite (N,
893 Make_Object_Declaration (Loc,
894 Defining_Identifier => Id,
895 Constant_Present => True,
896 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
897 Expression => Relocate_Node (Nam)));
898 return;
899 end if;
901 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
902 -- when renaming declaration has a named access type. The Ada 2012
903 -- coverage rules allow an anonymous access type in the context of
904 -- an expected named general access type, but the renaming rules
905 -- require the types to be the same. (An exception is when the type
906 -- of the renaming is also an anonymous access type, which can only
907 -- happen due to a renaming created by the expander.)
909 if Nkind (Nam) = N_Type_Conversion
910 and then not Comes_From_Source (Nam)
911 and then Ekind (Etype (Expression (Nam))) = E_Anonymous_Access_Type
912 and then Ekind (T) /= E_Anonymous_Access_Type
913 then
914 Wrong_Type (Expression (Nam), T); -- Should we give better error???
915 end if;
917 -- Check that a class-wide object is not being renamed as an object
918 -- of a specific type. The test for access types is needed to exclude
919 -- cases where the renamed object is a dynamically tagged access
920 -- result, such as occurs in certain expansions.
922 if Is_Tagged_Type (T) then
923 Check_Dynamically_Tagged_Expression
924 (Expr => Nam,
925 Typ => T,
926 Related_Nod => N);
927 end if;
929 -- Ada 2005 (AI-230/AI-254): Access renaming
931 else pragma Assert (Present (Access_Definition (N)));
932 T := Access_Definition
933 (Related_Nod => N,
934 N => Access_Definition (N));
936 Analyze (Nam);
938 -- Ada 2005 AI05-105: if the declaration has an anonymous access
939 -- type, the renamed object must also have an anonymous type, and
940 -- this is a name resolution rule. This was implicit in the last part
941 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
942 -- recent AI.
944 if not Is_Overloaded (Nam) then
945 if Ekind (Etype (Nam)) /= Ekind (T) then
946 Error_Msg_N
947 ("expect anonymous access type in object renaming", N);
948 end if;
950 else
951 declare
952 I : Interp_Index;
953 It : Interp;
954 Typ : Entity_Id := Empty;
955 Seen : Boolean := False;
957 begin
958 Get_First_Interp (Nam, I, It);
959 while Present (It.Typ) loop
961 -- Renaming is ambiguous if more than one candidate
962 -- interpretation is type-conformant with the context.
964 if Ekind (It.Typ) = Ekind (T) then
965 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
966 and then
967 Type_Conformant
968 (Designated_Type (T), Designated_Type (It.Typ))
969 then
970 if not Seen then
971 Seen := True;
972 else
973 Error_Msg_N
974 ("ambiguous expression in renaming", Nam);
975 end if;
977 elsif Ekind (T) = E_Anonymous_Access_Type
978 and then
979 Covers (Designated_Type (T), Designated_Type (It.Typ))
980 then
981 if not Seen then
982 Seen := True;
983 else
984 Error_Msg_N
985 ("ambiguous expression in renaming", Nam);
986 end if;
987 end if;
989 if Covers (T, It.Typ) then
990 Typ := It.Typ;
991 Set_Etype (Nam, Typ);
992 Set_Is_Overloaded (Nam, False);
993 end if;
994 end if;
996 Get_Next_Interp (I, It);
997 end loop;
998 end;
999 end if;
1001 Resolve (Nam, T);
1003 -- Ada 2005 (AI-231): "In the case where the type is defined by an
1004 -- access_definition, the renamed entity shall be of an access-to-
1005 -- constant type if and only if the access_definition defines an
1006 -- access-to-constant type" ARM 8.5.1(4)
1008 if Constant_Present (Access_Definition (N))
1009 and then not Is_Access_Constant (Etype (Nam))
1010 then
1011 Error_Msg_N ("(Ada 2005): the renamed object is not "
1012 & "access-to-constant (RM 8.5.1(6))", N);
1014 elsif not Constant_Present (Access_Definition (N))
1015 and then Is_Access_Constant (Etype (Nam))
1016 then
1017 Error_Msg_N ("(Ada 2005): the renamed object is not "
1018 & "access-to-variable (RM 8.5.1(6))", N);
1019 end if;
1021 if Is_Access_Subprogram_Type (Etype (Nam)) then
1022 Check_Subtype_Conformant
1023 (Designated_Type (T), Designated_Type (Etype (Nam)));
1025 elsif not Subtypes_Statically_Match
1026 (Designated_Type (T),
1027 Available_View (Designated_Type (Etype (Nam))))
1028 then
1029 Error_Msg_N
1030 ("subtype of renamed object does not statically match", N);
1031 end if;
1032 end if;
1034 -- Special processing for renaming function return object. Some errors
1035 -- and warnings are produced only for calls that come from source.
1037 if Nkind (Nam) = N_Function_Call then
1038 case Ada_Version is
1040 -- Usage is illegal in Ada 83
1042 when Ada_83 =>
1043 if Comes_From_Source (Nam) then
1044 Error_Msg_N
1045 ("(Ada 83) cannot rename function return object", Nam);
1046 end if;
1048 -- In Ada 95, warn for odd case of renaming parameterless function
1049 -- call if this is not a limited type (where this is useful).
1051 when others =>
1052 if Warn_On_Object_Renames_Function
1053 and then No (Parameter_Associations (Nam))
1054 and then not Is_Limited_Type (Etype (Nam))
1055 and then Comes_From_Source (Nam)
1056 then
1057 Error_Msg_N
1058 ("renaming function result object is suspicious?R?", Nam);
1059 Error_Msg_NE
1060 ("\function & will be called only once?R?", Nam,
1061 Entity (Name (Nam)));
1062 Error_Msg_N -- CODEFIX
1063 ("\suggest using an initialized constant "
1064 & "object instead?R?", Nam);
1065 end if;
1067 end case;
1068 end if;
1070 Check_Constrained_Object;
1072 -- An object renaming requires an exact match of the type. Class-wide
1073 -- matching is not allowed.
1075 if Is_Class_Wide_Type (T)
1076 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1077 then
1078 Wrong_Type (Nam, T);
1079 end if;
1081 T2 := Etype (Nam);
1083 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1085 if Nkind (Nam) = N_Explicit_Dereference
1086 and then Ekind (Etype (T2)) = E_Incomplete_Type
1087 then
1088 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1089 return;
1091 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1092 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1093 return;
1094 end if;
1096 -- Ada 2005 (AI-327)
1098 if Ada_Version >= Ada_2005
1099 and then Nkind (Nam) = N_Attribute_Reference
1100 and then Attribute_Name (Nam) = Name_Priority
1101 then
1102 null;
1104 elsif Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1105 declare
1106 Nam_Decl : Node_Id;
1107 Nam_Ent : Entity_Id;
1109 begin
1110 if Nkind (Nam) = N_Attribute_Reference then
1111 Nam_Ent := Entity (Prefix (Nam));
1112 else
1113 Nam_Ent := Entity (Nam);
1114 end if;
1116 Nam_Decl := Parent (Nam_Ent);
1118 if Has_Null_Exclusion (N)
1119 and then not Has_Null_Exclusion (Nam_Decl)
1120 then
1121 -- Ada 2005 (AI-423): If the object name denotes a generic
1122 -- formal object of a generic unit G, and the object renaming
1123 -- declaration occurs within the body of G or within the body
1124 -- of a generic unit declared within the declarative region
1125 -- of G, then the declaration of the formal object of G must
1126 -- have a null exclusion or a null-excluding subtype.
1128 if Is_Formal_Object (Nam_Ent)
1129 and then In_Generic_Scope (Id)
1130 then
1131 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1132 Error_Msg_N
1133 ("renamed formal does not exclude `NULL` "
1134 & "(RM 8.5.1(4.6/2))", N);
1136 elsif In_Package_Body (Scope (Id)) then
1137 Error_Msg_N
1138 ("formal object does not have a null exclusion"
1139 & "(RM 8.5.1(4.6/2))", N);
1140 end if;
1142 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1143 -- shall exclude null.
1145 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1146 Error_Msg_N
1147 ("renamed object does not exclude `NULL` "
1148 & "(RM 8.5.1(4.6/2))", N);
1150 -- An instance is illegal if it contains a renaming that
1151 -- excludes null, and the actual does not. The renaming
1152 -- declaration has already indicated that the declaration
1153 -- of the renamed actual in the instance will raise
1154 -- constraint_error.
1156 elsif Nkind (Nam_Decl) = N_Object_Declaration
1157 and then In_Instance
1158 and then
1159 Present (Corresponding_Generic_Association (Nam_Decl))
1160 and then Nkind (Expression (Nam_Decl)) =
1161 N_Raise_Constraint_Error
1162 then
1163 Error_Msg_N
1164 ("renamed actual does not exclude `NULL` "
1165 & "(RM 8.5.1(4.6/2))", N);
1167 -- Finally, if there is a null exclusion, the subtype mark
1168 -- must not be null-excluding.
1170 elsif No (Access_Definition (N))
1171 and then Can_Never_Be_Null (T)
1172 then
1173 Error_Msg_NE
1174 ("`NOT NULL` not allowed (& already excludes null)",
1175 N, T);
1177 end if;
1179 elsif Can_Never_Be_Null (T)
1180 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1181 then
1182 Error_Msg_N
1183 ("renamed object does not exclude `NULL` "
1184 & "(RM 8.5.1(4.6/2))", N);
1186 elsif Has_Null_Exclusion (N)
1187 and then No (Access_Definition (N))
1188 and then Can_Never_Be_Null (T)
1189 then
1190 Error_Msg_NE
1191 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1192 end if;
1193 end;
1194 end if;
1196 Set_Ekind (Id, E_Variable);
1198 -- Initialize the object size and alignment. Note that we used to call
1199 -- Init_Size_Align here, but that's wrong for objects which have only
1200 -- an Esize, not an RM_Size field!
1202 Init_Object_Size_Align (Id);
1204 if T = Any_Type or else Etype (Nam) = Any_Type then
1205 return;
1207 -- Verify that the renamed entity is an object or a function call. It
1208 -- may have been rewritten in several ways.
1210 elsif Is_Object_Reference (Nam) then
1211 if Comes_From_Source (N)
1212 and then Is_Dependent_Component_Of_Mutable_Object (Nam)
1213 then
1214 Error_Msg_N
1215 ("illegal renaming of discriminant-dependent component", Nam);
1216 end if;
1218 -- A static function call may have been folded into a literal
1220 elsif Nkind (Original_Node (Nam)) = N_Function_Call
1222 -- When expansion is disabled, attribute reference is not
1223 -- rewritten as function call. Otherwise it may be rewritten
1224 -- as a conversion, so check original node.
1226 or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference
1227 and then Is_Function_Attribute_Name
1228 (Attribute_Name (Original_Node (Nam))))
1230 -- Weird but legal, equivalent to renaming a function call.
1231 -- Illegal if the literal is the result of constant-folding an
1232 -- attribute reference that is not a function.
1234 or else (Is_Entity_Name (Nam)
1235 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1236 and then
1237 Nkind (Original_Node (Nam)) /= N_Attribute_Reference)
1239 or else (Nkind (Nam) = N_Type_Conversion
1240 and then Is_Tagged_Type (Entity (Subtype_Mark (Nam))))
1241 then
1242 null;
1244 elsif Nkind (Nam) = N_Type_Conversion then
1245 Error_Msg_N
1246 ("renaming of conversion only allowed for tagged types", Nam);
1248 -- Ada 2005 (AI-327)
1250 elsif Ada_Version >= Ada_2005
1251 and then Nkind (Nam) = N_Attribute_Reference
1252 and then Attribute_Name (Nam) = Name_Priority
1253 then
1254 null;
1256 -- Allow internally generated x'Reference expression
1258 elsif Nkind (Nam) = N_Reference then
1259 null;
1261 else
1262 Error_Msg_N ("expect object name in renaming", Nam);
1263 end if;
1265 Set_Etype (Id, T2);
1267 if not Is_Variable (Nam) then
1268 Set_Ekind (Id, E_Constant);
1269 Set_Never_Set_In_Source (Id, True);
1270 Set_Is_True_Constant (Id, True);
1271 end if;
1273 Set_Renamed_Object (Id, Nam);
1275 -- Implementation-defined aspect specifications can appear in a renaming
1276 -- declaration, but not language-defined ones. The call to procedure
1277 -- Analyze_Aspect_Specifications will take care of this error check.
1279 if Has_Aspects (N) then
1280 Analyze_Aspect_Specifications (N, Id);
1281 end if;
1283 -- Deal with dimensions
1285 Analyze_Dimension (N);
1286 end Analyze_Object_Renaming;
1288 ------------------------------
1289 -- Analyze_Package_Renaming --
1290 ------------------------------
1292 procedure Analyze_Package_Renaming (N : Node_Id) is
1293 New_P : constant Entity_Id := Defining_Entity (N);
1294 Old_P : Entity_Id;
1295 Spec : Node_Id;
1297 begin
1298 if Name (N) = Error then
1299 return;
1300 end if;
1302 -- Apply Text_IO kludge here since we may be renaming a child of Text_IO
1304 Text_IO_Kludge (Name (N));
1306 if Current_Scope /= Standard_Standard then
1307 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1308 end if;
1310 Enter_Name (New_P);
1311 Analyze (Name (N));
1313 if Is_Entity_Name (Name (N)) then
1314 Old_P := Entity (Name (N));
1315 else
1316 Old_P := Any_Id;
1317 end if;
1319 if Etype (Old_P) = Any_Type then
1320 Error_Msg_N ("expect package name in renaming", Name (N));
1322 elsif Ekind (Old_P) /= E_Package
1323 and then not (Ekind (Old_P) = E_Generic_Package
1324 and then In_Open_Scopes (Old_P))
1325 then
1326 if Ekind (Old_P) = E_Generic_Package then
1327 Error_Msg_N
1328 ("generic package cannot be renamed as a package", Name (N));
1329 else
1330 Error_Msg_Sloc := Sloc (Old_P);
1331 Error_Msg_NE
1332 ("expect package name in renaming, found& declared#",
1333 Name (N), Old_P);
1334 end if;
1336 -- Set basic attributes to minimize cascaded errors
1338 Set_Ekind (New_P, E_Package);
1339 Set_Etype (New_P, Standard_Void_Type);
1341 -- Here for OK package renaming
1343 else
1344 -- Entities in the old package are accessible through the renaming
1345 -- entity. The simplest implementation is to have both packages share
1346 -- the entity list.
1348 Set_Ekind (New_P, E_Package);
1349 Set_Etype (New_P, Standard_Void_Type);
1351 if Present (Renamed_Object (Old_P)) then
1352 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1353 else
1354 Set_Renamed_Object (New_P, Old_P);
1355 end if;
1357 Set_Has_Completion (New_P);
1359 Set_First_Entity (New_P, First_Entity (Old_P));
1360 Set_Last_Entity (New_P, Last_Entity (Old_P));
1361 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1362 Check_Library_Unit_Renaming (N, Old_P);
1363 Generate_Reference (Old_P, Name (N));
1365 -- If the renaming is in the visible part of a package, then we set
1366 -- Renamed_In_Spec for the renamed package, to prevent giving
1367 -- warnings about no entities referenced. Such a warning would be
1368 -- overenthusiastic, since clients can see entities in the renamed
1369 -- package via the visible package renaming.
1371 declare
1372 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1373 begin
1374 if Ekind (Ent) = E_Package
1375 and then not In_Private_Part (Ent)
1376 and then In_Extended_Main_Source_Unit (N)
1377 and then Ekind (Old_P) = E_Package
1378 then
1379 Set_Renamed_In_Spec (Old_P);
1380 end if;
1381 end;
1383 -- If this is the renaming declaration of a package instantiation
1384 -- within itself, it is the declaration that ends the list of actuals
1385 -- for the instantiation. At this point, the subtypes that rename
1386 -- the actuals are flagged as generic, to avoid spurious ambiguities
1387 -- if the actuals for two distinct formals happen to coincide. If
1388 -- the actual is a private type, the subtype has a private completion
1389 -- that is flagged in the same fashion.
1391 -- Resolution is identical to what is was in the original generic.
1392 -- On exit from the generic instance, these are turned into regular
1393 -- subtypes again, so they are compatible with types in their class.
1395 if not Is_Generic_Instance (Old_P) then
1396 return;
1397 else
1398 Spec := Specification (Unit_Declaration_Node (Old_P));
1399 end if;
1401 if Nkind (Spec) = N_Package_Specification
1402 and then Present (Generic_Parent (Spec))
1403 and then Old_P = Current_Scope
1404 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1405 then
1406 declare
1407 E : Entity_Id;
1409 begin
1410 E := First_Entity (Old_P);
1411 while Present (E) and then E /= New_P loop
1412 if Is_Type (E)
1413 and then Nkind (Parent (E)) = N_Subtype_Declaration
1414 then
1415 Set_Is_Generic_Actual_Type (E);
1417 if Is_Private_Type (E)
1418 and then Present (Full_View (E))
1419 then
1420 Set_Is_Generic_Actual_Type (Full_View (E));
1421 end if;
1422 end if;
1424 Next_Entity (E);
1425 end loop;
1426 end;
1427 end if;
1428 end if;
1430 -- Implementation-defined aspect specifications can appear in a renaming
1431 -- declaration, but not language-defined ones. The call to procedure
1432 -- Analyze_Aspect_Specifications will take care of this error check.
1434 if Has_Aspects (N) then
1435 Analyze_Aspect_Specifications (N, New_P);
1436 end if;
1437 end Analyze_Package_Renaming;
1439 -------------------------------
1440 -- Analyze_Renamed_Character --
1441 -------------------------------
1443 procedure Analyze_Renamed_Character
1444 (N : Node_Id;
1445 New_S : Entity_Id;
1446 Is_Body : Boolean)
1448 C : constant Node_Id := Name (N);
1450 begin
1451 if Ekind (New_S) = E_Function then
1452 Resolve (C, Etype (New_S));
1454 if Is_Body then
1455 Check_Frozen_Renaming (N, New_S);
1456 end if;
1458 else
1459 Error_Msg_N ("character literal can only be renamed as function", N);
1460 end if;
1461 end Analyze_Renamed_Character;
1463 ---------------------------------
1464 -- Analyze_Renamed_Dereference --
1465 ---------------------------------
1467 procedure Analyze_Renamed_Dereference
1468 (N : Node_Id;
1469 New_S : Entity_Id;
1470 Is_Body : Boolean)
1472 Nam : constant Node_Id := Name (N);
1473 P : constant Node_Id := Prefix (Nam);
1474 Typ : Entity_Id;
1475 Ind : Interp_Index;
1476 It : Interp;
1478 begin
1479 if not Is_Overloaded (P) then
1480 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1481 or else not Type_Conformant (Etype (Nam), New_S)
1482 then
1483 Error_Msg_N ("designated type does not match specification", P);
1484 else
1485 Resolve (P);
1486 end if;
1488 return;
1490 else
1491 Typ := Any_Type;
1492 Get_First_Interp (Nam, Ind, It);
1494 while Present (It.Nam) loop
1496 if Ekind (It.Nam) = E_Subprogram_Type
1497 and then Type_Conformant (It.Nam, New_S)
1498 then
1499 if Typ /= Any_Id then
1500 Error_Msg_N ("ambiguous renaming", P);
1501 return;
1502 else
1503 Typ := It.Nam;
1504 end if;
1505 end if;
1507 Get_Next_Interp (Ind, It);
1508 end loop;
1510 if Typ = Any_Type then
1511 Error_Msg_N ("designated type does not match specification", P);
1512 else
1513 Resolve (N, Typ);
1515 if Is_Body then
1516 Check_Frozen_Renaming (N, New_S);
1517 end if;
1518 end if;
1519 end if;
1520 end Analyze_Renamed_Dereference;
1522 ---------------------------
1523 -- Analyze_Renamed_Entry --
1524 ---------------------------
1526 procedure Analyze_Renamed_Entry
1527 (N : Node_Id;
1528 New_S : Entity_Id;
1529 Is_Body : Boolean)
1531 Nam : constant Node_Id := Name (N);
1532 Sel : constant Node_Id := Selector_Name (Nam);
1533 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1534 Old_S : Entity_Id;
1536 begin
1537 if Entity (Sel) = Any_Id then
1539 -- Selector is undefined on prefix. Error emitted already
1541 Set_Has_Completion (New_S);
1542 return;
1543 end if;
1545 -- Otherwise find renamed entity and build body of New_S as a call to it
1547 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1549 if Old_S = Any_Id then
1550 Error_Msg_N (" no subprogram or entry matches specification", N);
1551 else
1552 if Is_Body then
1553 Check_Subtype_Conformant (New_S, Old_S, N);
1554 Generate_Reference (New_S, Defining_Entity (N), 'b');
1555 Style.Check_Identifier (Defining_Entity (N), New_S);
1557 else
1558 -- Only mode conformance required for a renaming_as_declaration
1560 Check_Mode_Conformant (New_S, Old_S, N);
1561 end if;
1563 Inherit_Renamed_Profile (New_S, Old_S);
1565 -- The prefix can be an arbitrary expression that yields a task or
1566 -- protected object, so it must be resolved.
1568 Resolve (Prefix (Nam), Scope (Old_S));
1569 end if;
1571 Set_Convention (New_S, Convention (Old_S));
1572 Set_Has_Completion (New_S, Inside_A_Generic);
1574 -- AI05-0225: If the renamed entity is a procedure or entry of a
1575 -- protected object, the target object must be a variable.
1577 if Ekind (Scope (Old_S)) in Protected_Kind
1578 and then Ekind (New_S) = E_Procedure
1579 and then not Is_Variable (Prefix (Nam))
1580 then
1581 if Is_Actual then
1582 Error_Msg_N
1583 ("target object of protected operation used as actual for "
1584 & "formal procedure must be a variable", Nam);
1585 else
1586 Error_Msg_N
1587 ("target object of protected operation renamed as procedure, "
1588 & "must be a variable", Nam);
1589 end if;
1590 end if;
1592 if Is_Body then
1593 Check_Frozen_Renaming (N, New_S);
1594 end if;
1595 end Analyze_Renamed_Entry;
1597 -----------------------------------
1598 -- Analyze_Renamed_Family_Member --
1599 -----------------------------------
1601 procedure Analyze_Renamed_Family_Member
1602 (N : Node_Id;
1603 New_S : Entity_Id;
1604 Is_Body : Boolean)
1606 Nam : constant Node_Id := Name (N);
1607 P : constant Node_Id := Prefix (Nam);
1608 Old_S : Entity_Id;
1610 begin
1611 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1612 or else (Nkind (P) = N_Selected_Component
1613 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1614 then
1615 if Is_Entity_Name (P) then
1616 Old_S := Entity (P);
1617 else
1618 Old_S := Entity (Selector_Name (P));
1619 end if;
1621 if not Entity_Matches_Spec (Old_S, New_S) then
1622 Error_Msg_N ("entry family does not match specification", N);
1624 elsif Is_Body then
1625 Check_Subtype_Conformant (New_S, Old_S, N);
1626 Generate_Reference (New_S, Defining_Entity (N), 'b');
1627 Style.Check_Identifier (Defining_Entity (N), New_S);
1628 end if;
1630 else
1631 Error_Msg_N ("no entry family matches specification", N);
1632 end if;
1634 Set_Has_Completion (New_S, Inside_A_Generic);
1636 if Is_Body then
1637 Check_Frozen_Renaming (N, New_S);
1638 end if;
1639 end Analyze_Renamed_Family_Member;
1641 -----------------------------------------
1642 -- Analyze_Renamed_Primitive_Operation --
1643 -----------------------------------------
1645 procedure Analyze_Renamed_Primitive_Operation
1646 (N : Node_Id;
1647 New_S : Entity_Id;
1648 Is_Body : Boolean)
1650 Old_S : Entity_Id;
1652 function Conforms
1653 (Subp : Entity_Id;
1654 Ctyp : Conformance_Type) return Boolean;
1655 -- Verify that the signatures of the renamed entity and the new entity
1656 -- match. The first formal of the renamed entity is skipped because it
1657 -- is the target object in any subsequent call.
1659 --------------
1660 -- Conforms --
1661 --------------
1663 function Conforms
1664 (Subp : Entity_Id;
1665 Ctyp : Conformance_Type) return Boolean
1667 Old_F : Entity_Id;
1668 New_F : Entity_Id;
1670 begin
1671 if Ekind (Subp) /= Ekind (New_S) then
1672 return False;
1673 end if;
1675 Old_F := Next_Formal (First_Formal (Subp));
1676 New_F := First_Formal (New_S);
1677 while Present (Old_F) and then Present (New_F) loop
1678 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
1679 return False;
1680 end if;
1682 if Ctyp >= Mode_Conformant
1683 and then Ekind (Old_F) /= Ekind (New_F)
1684 then
1685 return False;
1686 end if;
1688 Next_Formal (New_F);
1689 Next_Formal (Old_F);
1690 end loop;
1692 return True;
1693 end Conforms;
1695 -- Start of processing for Analyze_Renamed_Primitive_Operation
1697 begin
1698 if not Is_Overloaded (Selector_Name (Name (N))) then
1699 Old_S := Entity (Selector_Name (Name (N)));
1701 if not Conforms (Old_S, Type_Conformant) then
1702 Old_S := Any_Id;
1703 end if;
1705 else
1706 -- Find the operation that matches the given signature
1708 declare
1709 It : Interp;
1710 Ind : Interp_Index;
1712 begin
1713 Old_S := Any_Id;
1714 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
1716 while Present (It.Nam) loop
1717 if Conforms (It.Nam, Type_Conformant) then
1718 Old_S := It.Nam;
1719 end if;
1721 Get_Next_Interp (Ind, It);
1722 end loop;
1723 end;
1724 end if;
1726 if Old_S = Any_Id then
1727 Error_Msg_N (" no subprogram or entry matches specification", N);
1729 else
1730 if Is_Body then
1731 if not Conforms (Old_S, Subtype_Conformant) then
1732 Error_Msg_N ("subtype conformance error in renaming", N);
1733 end if;
1735 Generate_Reference (New_S, Defining_Entity (N), 'b');
1736 Style.Check_Identifier (Defining_Entity (N), New_S);
1738 else
1739 -- Only mode conformance required for a renaming_as_declaration
1741 if not Conforms (Old_S, Mode_Conformant) then
1742 Error_Msg_N ("mode conformance error in renaming", N);
1743 end if;
1745 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
1746 -- view of a subprogram is intrinsic, because the compiler has
1747 -- to generate a wrapper for any call to it. If the name in a
1748 -- subprogram renaming is a prefixed view, the entity is thus
1749 -- intrinsic, and 'Access cannot be applied to it.
1751 Set_Convention (New_S, Convention_Intrinsic);
1752 end if;
1754 -- Inherit_Renamed_Profile (New_S, Old_S);
1756 -- The prefix can be an arbitrary expression that yields an
1757 -- object, so it must be resolved.
1759 Resolve (Prefix (Name (N)));
1760 end if;
1761 end Analyze_Renamed_Primitive_Operation;
1763 ---------------------------------
1764 -- Analyze_Subprogram_Renaming --
1765 ---------------------------------
1767 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1768 Formal_Spec : constant Node_Id := Corresponding_Formal_Spec (N);
1769 Is_Actual : constant Boolean := Present (Formal_Spec);
1770 Inst_Node : Node_Id := Empty;
1771 Nam : constant Node_Id := Name (N);
1772 New_S : Entity_Id;
1773 Old_S : Entity_Id := Empty;
1774 Rename_Spec : Entity_Id;
1775 Save_AV : constant Ada_Version_Type := Ada_Version;
1776 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
1777 Spec : constant Node_Id := Specification (N);
1779 procedure Check_Null_Exclusion
1780 (Ren : Entity_Id;
1781 Sub : Entity_Id);
1782 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
1783 -- following AI rules:
1785 -- If Ren is a renaming of a formal subprogram and one of its
1786 -- parameters has a null exclusion, then the corresponding formal
1787 -- in Sub must also have one. Otherwise the subtype of the Sub's
1788 -- formal parameter must exclude null.
1790 -- If Ren is a renaming of a formal function and its return
1791 -- profile has a null exclusion, then Sub's return profile must
1792 -- have one. Otherwise the subtype of Sub's return profile must
1793 -- exclude null.
1795 procedure Freeze_Actual_Profile;
1796 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
1797 -- types: a callable entity freezes its profile, unless it has an
1798 -- incomplete untagged formal (RM 13.14(10.2/3)).
1800 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
1801 -- Find renamed entity when the declaration is a renaming_as_body and
1802 -- the renamed entity may itself be a renaming_as_body. Used to enforce
1803 -- rule that a renaming_as_body is illegal if the declaration occurs
1804 -- before the subprogram it completes is frozen, and renaming indirectly
1805 -- renames the subprogram itself.(Defect Report 8652/0027).
1807 function Check_Class_Wide_Actual return Entity_Id;
1808 -- AI05-0071: In an instance, if the actual for a formal type FT with
1809 -- unknown discriminants is a class-wide type CT, and the generic has
1810 -- a formal subprogram with a box for a primitive operation of FT,
1811 -- then the corresponding actual subprogram denoted by the default is a
1812 -- class-wide operation whose body is a dispatching call. We replace the
1813 -- generated renaming declaration:
1815 -- procedure P (X : CT) renames P;
1817 -- by a different renaming and a class-wide operation:
1819 -- procedure Pr (X : T) renames P; -- renames primitive operation
1820 -- procedure P (X : CT); -- class-wide operation
1821 -- ...
1822 -- procedure P (X : CT) is begin Pr (X); end; -- dispatching call
1824 -- This rule only applies if there is no explicit visible class-wide
1825 -- operation at the point of the instantiation.
1827 function Has_Class_Wide_Actual return Boolean;
1828 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
1829 -- defaulted formal subprogram when the actual for the controlling
1830 -- formal type is class-wide.
1832 -----------------------------
1833 -- Check_Class_Wide_Actual --
1834 -----------------------------
1836 function Check_Class_Wide_Actual return Entity_Id is
1837 Loc : constant Source_Ptr := Sloc (N);
1839 F : Entity_Id;
1840 Formal_Type : Entity_Id;
1841 Actual_Type : Entity_Id;
1842 New_Body : Node_Id;
1843 New_Decl : Node_Id;
1844 Result : Entity_Id;
1846 function Make_Call (Prim_Op : Entity_Id) return Node_Id;
1847 -- Build dispatching call for body of class-wide operation
1849 function Make_Spec return Node_Id;
1850 -- Create subprogram specification for declaration and body of
1851 -- class-wide operation, using signature of renaming declaration.
1853 ---------------
1854 -- Make_Call --
1855 ---------------
1857 function Make_Call (Prim_Op : Entity_Id) return Node_Id is
1858 Actuals : List_Id;
1859 F : Node_Id;
1861 begin
1862 Actuals := New_List;
1863 F := First (Parameter_Specifications (Specification (New_Decl)));
1864 while Present (F) loop
1865 Append_To (Actuals,
1866 Make_Identifier (Loc, Chars (Defining_Identifier (F))));
1867 Next (F);
1868 end loop;
1870 if Ekind_In (Prim_Op, E_Function, E_Operator) then
1871 return Make_Simple_Return_Statement (Loc,
1872 Expression =>
1873 Make_Function_Call (Loc,
1874 Name => New_Occurrence_Of (Prim_Op, Loc),
1875 Parameter_Associations => Actuals));
1876 else
1877 return
1878 Make_Procedure_Call_Statement (Loc,
1879 Name => New_Occurrence_Of (Prim_Op, Loc),
1880 Parameter_Associations => Actuals);
1881 end if;
1882 end Make_Call;
1884 ---------------
1885 -- Make_Spec --
1886 ---------------
1888 function Make_Spec return Node_Id is
1889 Param_Specs : constant List_Id := Copy_Parameter_List (New_S);
1891 begin
1892 if Ekind (New_S) = E_Procedure then
1893 return
1894 Make_Procedure_Specification (Loc,
1895 Defining_Unit_Name =>
1896 Make_Defining_Identifier (Loc,
1897 Chars (Defining_Unit_Name (Spec))),
1898 Parameter_Specifications => Param_Specs);
1899 else
1900 return
1901 Make_Function_Specification (Loc,
1902 Defining_Unit_Name =>
1903 Make_Defining_Identifier (Loc,
1904 Chars (Defining_Unit_Name (Spec))),
1905 Parameter_Specifications => Param_Specs,
1906 Result_Definition =>
1907 New_Copy_Tree (Result_Definition (Spec)));
1908 end if;
1909 end Make_Spec;
1911 -- Start of processing for Check_Class_Wide_Actual
1913 begin
1914 Result := Any_Id;
1915 Formal_Type := Empty;
1916 Actual_Type := Empty;
1918 F := First_Formal (Formal_Spec);
1919 while Present (F) loop
1920 if Has_Unknown_Discriminants (Etype (F))
1921 and then not Is_Class_Wide_Type (Etype (F))
1922 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (F)))
1923 then
1924 Formal_Type := Etype (F);
1925 Actual_Type := Etype (Get_Instance_Of (Formal_Type));
1926 exit;
1927 end if;
1929 Next_Formal (F);
1930 end loop;
1932 if Present (Formal_Type) then
1934 -- Create declaration and body for class-wide operation
1936 New_Decl :=
1937 Make_Subprogram_Declaration (Loc, Specification => Make_Spec);
1939 New_Body :=
1940 Make_Subprogram_Body (Loc,
1941 Specification => Make_Spec,
1942 Declarations => No_List,
1943 Handled_Statement_Sequence =>
1944 Make_Handled_Sequence_Of_Statements (Loc, New_List));
1946 -- Modify Spec and create internal name for renaming of primitive
1947 -- operation.
1949 Set_Defining_Unit_Name (Spec, Make_Temporary (Loc, 'R'));
1950 F := First (Parameter_Specifications (Spec));
1951 while Present (F) loop
1952 if Nkind (Parameter_Type (F)) = N_Identifier
1953 and then Is_Class_Wide_Type (Entity (Parameter_Type (F)))
1954 then
1955 Set_Parameter_Type (F, New_Occurrence_Of (Actual_Type, Loc));
1956 end if;
1957 Next (F);
1958 end loop;
1960 New_S := Analyze_Subprogram_Specification (Spec);
1961 Result := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
1962 end if;
1964 if Result /= Any_Id then
1965 Insert_Before (N, New_Decl);
1966 Analyze (New_Decl);
1968 -- Add dispatching call to body of class-wide operation
1970 Append (Make_Call (Result),
1971 Statements (Handled_Statement_Sequence (New_Body)));
1973 -- The generated body does not freeze. It is analyzed when the
1974 -- generated operation is frozen. This body is only needed if
1975 -- expansion is enabled.
1977 if Expander_Active then
1978 Append_Freeze_Action (Defining_Entity (New_Decl), New_Body);
1979 end if;
1981 Result := Defining_Entity (New_Decl);
1982 end if;
1984 -- Return the class-wide operation if one was created
1986 return Result;
1987 end Check_Class_Wide_Actual;
1989 --------------------------
1990 -- Check_Null_Exclusion --
1991 --------------------------
1993 procedure Check_Null_Exclusion
1994 (Ren : Entity_Id;
1995 Sub : Entity_Id)
1997 Ren_Formal : Entity_Id;
1998 Sub_Formal : Entity_Id;
2000 begin
2001 -- Parameter check
2003 Ren_Formal := First_Formal (Ren);
2004 Sub_Formal := First_Formal (Sub);
2005 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2006 if Has_Null_Exclusion (Parent (Ren_Formal))
2007 and then
2008 not (Has_Null_Exclusion (Parent (Sub_Formal))
2009 or else Can_Never_Be_Null (Etype (Sub_Formal)))
2010 then
2011 Error_Msg_NE
2012 ("`NOT NULL` required for parameter &",
2013 Parent (Sub_Formal), Sub_Formal);
2014 end if;
2016 Next_Formal (Ren_Formal);
2017 Next_Formal (Sub_Formal);
2018 end loop;
2020 -- Return profile check
2022 if Nkind (Parent (Ren)) = N_Function_Specification
2023 and then Nkind (Parent (Sub)) = N_Function_Specification
2024 and then Has_Null_Exclusion (Parent (Ren))
2025 and then not (Has_Null_Exclusion (Parent (Sub))
2026 or else Can_Never_Be_Null (Etype (Sub)))
2027 then
2028 Error_Msg_N
2029 ("return must specify `NOT NULL`",
2030 Result_Definition (Parent (Sub)));
2031 end if;
2032 end Check_Null_Exclusion;
2034 ---------------------------
2035 -- Freeze_Actual_Profile --
2036 ---------------------------
2038 procedure Freeze_Actual_Profile is
2039 F : Entity_Id;
2040 Has_Untagged_Inc : Boolean;
2041 Instantiation_Node : constant Node_Id := Parent (N);
2043 begin
2044 if Ada_Version >= Ada_2012 then
2045 F := First_Formal (Formal_Spec);
2046 Has_Untagged_Inc := False;
2047 while Present (F) loop
2048 if Ekind (Etype (F)) = E_Incomplete_Type
2049 and then not Is_Tagged_Type (Etype (F))
2050 then
2051 Has_Untagged_Inc := True;
2052 exit;
2053 end if;
2055 F := Next_Formal (F);
2056 end loop;
2058 if Ekind (Formal_Spec) = E_Function
2059 and then Ekind (Etype (Formal_Spec)) = E_Incomplete_Type
2060 and then not Is_Tagged_Type (Etype (F))
2061 then
2062 Has_Untagged_Inc := True;
2063 end if;
2065 if not Has_Untagged_Inc then
2066 F := First_Formal (Old_S);
2067 while Present (F) loop
2068 Freeze_Before (Instantiation_Node, Etype (F));
2070 if Is_Incomplete_Or_Private_Type (Etype (F))
2071 and then No (Underlying_Type (Etype (F)))
2072 then
2074 -- Exclude generic types, or types derived from them.
2075 -- They will be frozen in the enclosing instance.
2077 if Is_Generic_Type (Etype (F))
2078 or else Is_Generic_Type (Root_Type (Etype (F)))
2079 then
2080 null;
2081 else
2082 Error_Msg_NE
2083 ("type& must be frozen before this point",
2084 Instantiation_Node, Etype (F));
2085 end if;
2086 end if;
2088 F := Next_Formal (F);
2089 end loop;
2090 end if;
2091 end if;
2092 end Freeze_Actual_Profile;
2094 ---------------------------
2095 -- Has_Class_Wide_Actual --
2096 ---------------------------
2098 function Has_Class_Wide_Actual return Boolean is
2099 F_Nam : Entity_Id;
2100 F_Spec : Entity_Id;
2102 begin
2103 if Is_Actual
2104 and then Nkind (Nam) in N_Has_Entity
2105 and then Present (Entity (Nam))
2106 and then Is_Dispatching_Operation (Entity (Nam))
2107 then
2108 F_Nam := First_Entity (Entity (Nam));
2109 F_Spec := First_Formal (Formal_Spec);
2110 while Present (F_Nam) and then Present (F_Spec) loop
2111 if Is_Controlling_Formal (F_Nam)
2112 and then Has_Unknown_Discriminants (Etype (F_Spec))
2113 and then not Is_Class_Wide_Type (Etype (F_Spec))
2114 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (F_Spec)))
2115 then
2116 return True;
2117 end if;
2119 Next_Entity (F_Nam);
2120 Next_Formal (F_Spec);
2121 end loop;
2122 end if;
2124 return False;
2125 end Has_Class_Wide_Actual;
2127 -------------------------
2128 -- Original_Subprogram --
2129 -------------------------
2131 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2132 Orig_Decl : Node_Id;
2133 Orig_Subp : Entity_Id;
2135 begin
2136 -- First case: renamed entity is itself a renaming
2138 if Present (Alias (Subp)) then
2139 return Alias (Subp);
2141 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2142 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2143 then
2144 -- Check if renamed entity is a renaming_as_body
2146 Orig_Decl :=
2147 Unit_Declaration_Node
2148 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2150 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2151 Orig_Subp := Entity (Name (Orig_Decl));
2153 if Orig_Subp = Rename_Spec then
2155 -- Circularity detected
2157 return Orig_Subp;
2159 else
2160 return (Original_Subprogram (Orig_Subp));
2161 end if;
2162 else
2163 return Subp;
2164 end if;
2165 else
2166 return Subp;
2167 end if;
2168 end Original_Subprogram;
2170 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2171 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2172 -- defaulted formal subprogram when the actual for a related formal
2173 -- type is class-wide.
2175 -- Start of processing for Analyze_Subprogram_Renaming
2177 begin
2178 -- We must test for the attribute renaming case before the Analyze
2179 -- call because otherwise Sem_Attr will complain that the attribute
2180 -- is missing an argument when it is analyzed.
2182 if Nkind (Nam) = N_Attribute_Reference then
2184 -- In the case of an abstract formal subprogram association, rewrite
2185 -- an actual given by a stream attribute as the name of the
2186 -- corresponding stream primitive of the type.
2188 -- In a generic context the stream operations are not generated, and
2189 -- this must be treated as a normal attribute reference, to be
2190 -- expanded in subsequent instantiations.
2192 if Is_Actual
2193 and then Is_Abstract_Subprogram (Formal_Spec)
2194 and then Full_Expander_Active
2195 then
2196 declare
2197 Stream_Prim : Entity_Id;
2198 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
2200 begin
2201 -- The class-wide forms of the stream attributes are not
2202 -- primitive dispatching operations (even though they
2203 -- internally dispatch to a stream attribute).
2205 if Is_Class_Wide_Type (Prefix_Type) then
2206 Error_Msg_N
2207 ("attribute must be a primitive dispatching operation",
2208 Nam);
2209 return;
2210 end if;
2212 -- Retrieve the primitive subprogram associated with the
2213 -- attribute. This can only be a stream attribute, since those
2214 -- are the only ones that are dispatching (and the actual for
2215 -- an abstract formal subprogram must be dispatching
2216 -- operation).
2218 begin
2219 case Attribute_Name (Nam) is
2220 when Name_Input =>
2221 Stream_Prim :=
2222 Find_Prim_Op (Prefix_Type, TSS_Stream_Input);
2223 when Name_Output =>
2224 Stream_Prim :=
2225 Find_Prim_Op (Prefix_Type, TSS_Stream_Output);
2226 when Name_Read =>
2227 Stream_Prim :=
2228 Find_Prim_Op (Prefix_Type, TSS_Stream_Read);
2229 when Name_Write =>
2230 Stream_Prim :=
2231 Find_Prim_Op (Prefix_Type, TSS_Stream_Write);
2232 when others =>
2233 Error_Msg_N
2234 ("attribute must be a primitive"
2235 & " dispatching operation", Nam);
2236 return;
2237 end case;
2239 exception
2241 -- If no operation was found, and the type is limited,
2242 -- the user should have defined one.
2244 when Program_Error =>
2245 if Is_Limited_Type (Prefix_Type) then
2246 Error_Msg_NE
2247 ("stream operation not defined for type&",
2248 N, Prefix_Type);
2249 return;
2251 -- Otherwise, compiler should have generated default
2253 else
2254 raise;
2255 end if;
2256 end;
2258 -- Rewrite the attribute into the name of its corresponding
2259 -- primitive dispatching subprogram. We can then proceed with
2260 -- the usual processing for subprogram renamings.
2262 declare
2263 Prim_Name : constant Node_Id :=
2264 Make_Identifier (Sloc (Nam),
2265 Chars => Chars (Stream_Prim));
2266 begin
2267 Set_Entity (Prim_Name, Stream_Prim);
2268 Rewrite (Nam, Prim_Name);
2269 Analyze (Nam);
2270 end;
2271 end;
2273 -- Normal processing for a renaming of an attribute
2275 else
2276 Attribute_Renaming (N);
2277 return;
2278 end if;
2279 end if;
2281 -- Check whether this declaration corresponds to the instantiation
2282 -- of a formal subprogram.
2284 -- If this is an instantiation, the corresponding actual is frozen and
2285 -- error messages can be made more precise. If this is a default
2286 -- subprogram, the entity is already established in the generic, and is
2287 -- not retrieved by visibility. If it is a default with a box, the
2288 -- candidate interpretations, if any, have been collected when building
2289 -- the renaming declaration. If overloaded, the proper interpretation is
2290 -- determined in Find_Renamed_Entity. If the entity is an operator,
2291 -- Find_Renamed_Entity applies additional visibility checks.
2293 if Is_Actual then
2294 Inst_Node := Unit_Declaration_Node (Formal_Spec);
2296 -- Check whether the renaming is for a defaulted actual subprogram
2297 -- with a class-wide actual.
2299 if CW_Actual then
2300 New_S := Analyze_Subprogram_Specification (Spec);
2301 Old_S := Check_Class_Wide_Actual;
2303 elsif Is_Entity_Name (Nam)
2304 and then Present (Entity (Nam))
2305 and then not Comes_From_Source (Nam)
2306 and then not Is_Overloaded (Nam)
2307 then
2308 Old_S := Entity (Nam);
2309 New_S := Analyze_Subprogram_Specification (Spec);
2311 -- Operator case
2313 if Ekind (Entity (Nam)) = E_Operator then
2315 -- Box present
2317 if Box_Present (Inst_Node) then
2318 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2320 -- If there is an immediately visible homonym of the operator
2321 -- and the declaration has a default, this is worth a warning
2322 -- because the user probably did not intend to get the pre-
2323 -- defined operator, visible in the generic declaration. To
2324 -- find if there is an intended candidate, analyze the renaming
2325 -- again in the current context.
2327 elsif Scope (Old_S) = Standard_Standard
2328 and then Present (Default_Name (Inst_Node))
2329 then
2330 declare
2331 Decl : constant Node_Id := New_Copy_Tree (N);
2332 Hidden : Entity_Id;
2334 begin
2335 Set_Entity (Name (Decl), Empty);
2336 Analyze (Name (Decl));
2337 Hidden :=
2338 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
2340 if Present (Hidden)
2341 and then In_Open_Scopes (Scope (Hidden))
2342 and then Is_Immediately_Visible (Hidden)
2343 and then Comes_From_Source (Hidden)
2344 and then Hidden /= Old_S
2345 then
2346 Error_Msg_Sloc := Sloc (Hidden);
2347 Error_Msg_N ("default subprogram is resolved " &
2348 "in the generic declaration " &
2349 "(RM 12.6(17))??", N);
2350 Error_Msg_NE ("\and will not use & #??", N, Hidden);
2351 end if;
2352 end;
2353 end if;
2354 end if;
2356 else
2357 Analyze (Nam);
2358 New_S := Analyze_Subprogram_Specification (Spec);
2359 end if;
2361 else
2362 -- Renamed entity must be analyzed first, to avoid being hidden by
2363 -- new name (which might be the same in a generic instance).
2365 Analyze (Nam);
2367 -- The renaming defines a new overloaded entity, which is analyzed
2368 -- like a subprogram declaration.
2370 New_S := Analyze_Subprogram_Specification (Spec);
2371 end if;
2373 if Current_Scope /= Standard_Standard then
2374 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
2375 end if;
2377 Rename_Spec := Find_Corresponding_Spec (N);
2379 -- Case of Renaming_As_Body
2381 if Present (Rename_Spec) then
2383 -- Renaming declaration is the completion of the declaration of
2384 -- Rename_Spec. We build an actual body for it at the freezing point.
2386 Set_Corresponding_Spec (N, Rename_Spec);
2388 -- Deal with special case of stream functions of abstract types
2389 -- and interfaces.
2391 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
2392 N_Abstract_Subprogram_Declaration
2393 then
2394 -- Input stream functions are abstract if the object type is
2395 -- abstract. Similarly, all default stream functions for an
2396 -- interface type are abstract. However, these subprograms may
2397 -- receive explicit declarations in representation clauses, making
2398 -- the attribute subprograms usable as defaults in subsequent
2399 -- type extensions.
2400 -- In this case we rewrite the declaration to make the subprogram
2401 -- non-abstract. We remove the previous declaration, and insert
2402 -- the new one at the point of the renaming, to prevent premature
2403 -- access to unfrozen types. The new declaration reuses the
2404 -- specification of the previous one, and must not be analyzed.
2406 pragma Assert
2407 (Is_Primitive (Entity (Nam))
2408 and then
2409 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
2410 declare
2411 Old_Decl : constant Node_Id :=
2412 Unit_Declaration_Node (Rename_Spec);
2413 New_Decl : constant Node_Id :=
2414 Make_Subprogram_Declaration (Sloc (N),
2415 Specification =>
2416 Relocate_Node (Specification (Old_Decl)));
2417 begin
2418 Remove (Old_Decl);
2419 Insert_After (N, New_Decl);
2420 Set_Is_Abstract_Subprogram (Rename_Spec, False);
2421 Set_Analyzed (New_Decl);
2422 end;
2423 end if;
2425 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
2427 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2428 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
2429 end if;
2431 Set_Convention (New_S, Convention (Rename_Spec));
2432 Check_Fully_Conformant (New_S, Rename_Spec);
2433 Set_Public_Status (New_S);
2435 -- The specification does not introduce new formals, but only
2436 -- repeats the formals of the original subprogram declaration.
2437 -- For cross-reference purposes, and for refactoring tools, we
2438 -- treat the formals of the renaming declaration as body formals.
2440 Reference_Body_Formals (Rename_Spec, New_S);
2442 -- Indicate that the entity in the declaration functions like the
2443 -- corresponding body, and is not a new entity. The body will be
2444 -- constructed later at the freeze point, so indicate that the
2445 -- completion has not been seen yet.
2447 Set_Contract (New_S, Empty);
2448 Set_Ekind (New_S, E_Subprogram_Body);
2449 New_S := Rename_Spec;
2450 Set_Has_Completion (Rename_Spec, False);
2452 -- Ada 2005: check overriding indicator
2454 if Present (Overridden_Operation (Rename_Spec)) then
2455 if Must_Not_Override (Specification (N)) then
2456 Error_Msg_NE
2457 ("subprogram& overrides inherited operation",
2458 N, Rename_Spec);
2459 elsif
2460 Style_Check and then not Must_Override (Specification (N))
2461 then
2462 Style.Missing_Overriding (N, Rename_Spec);
2463 end if;
2465 elsif Must_Override (Specification (N)) then
2466 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
2467 end if;
2469 -- Normal subprogram renaming (not renaming as body)
2471 else
2472 Generate_Definition (New_S);
2473 New_Overloaded_Entity (New_S);
2475 if Is_Entity_Name (Nam)
2476 and then Is_Intrinsic_Subprogram (Entity (Nam))
2477 then
2478 null;
2479 else
2480 Check_Delayed_Subprogram (New_S);
2481 end if;
2482 end if;
2484 -- There is no need for elaboration checks on the new entity, which may
2485 -- be called before the next freezing point where the body will appear.
2486 -- Elaboration checks refer to the real entity, not the one created by
2487 -- the renaming declaration.
2489 Set_Kill_Elaboration_Checks (New_S, True);
2491 if Etype (Nam) = Any_Type then
2492 Set_Has_Completion (New_S);
2493 return;
2495 elsif Nkind (Nam) = N_Selected_Component then
2497 -- A prefix of the form A.B can designate an entry of task A, a
2498 -- protected operation of protected object A, or finally a primitive
2499 -- operation of object A. In the later case, A is an object of some
2500 -- tagged type, or an access type that denotes one such. To further
2501 -- distinguish these cases, note that the scope of a task entry or
2502 -- protected operation is type of the prefix.
2504 -- The prefix could be an overloaded function call that returns both
2505 -- kinds of operations. This overloading pathology is left to the
2506 -- dedicated reader ???
2508 declare
2509 T : constant Entity_Id := Etype (Prefix (Nam));
2511 begin
2512 if Present (T)
2513 and then
2514 (Is_Tagged_Type (T)
2515 or else
2516 (Is_Access_Type (T)
2517 and then Is_Tagged_Type (Designated_Type (T))))
2518 and then Scope (Entity (Selector_Name (Nam))) /= T
2519 then
2520 Analyze_Renamed_Primitive_Operation
2521 (N, New_S, Present (Rename_Spec));
2522 return;
2524 else
2525 -- Renamed entity is an entry or protected operation. For those
2526 -- cases an explicit body is built (at the point of freezing of
2527 -- this entity) that contains a call to the renamed entity.
2529 -- This is not allowed for renaming as body if the renamed
2530 -- spec is already frozen (see RM 8.5.4(5) for details).
2532 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
2533 Error_Msg_N
2534 ("renaming-as-body cannot rename entry as subprogram", N);
2535 Error_Msg_NE
2536 ("\since & is already frozen (RM 8.5.4(5))",
2537 N, Rename_Spec);
2538 else
2539 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
2540 end if;
2542 return;
2543 end if;
2544 end;
2546 elsif Nkind (Nam) = N_Explicit_Dereference then
2548 -- Renamed entity is designated by access_to_subprogram expression.
2549 -- Must build body to encapsulate call, as in the entry case.
2551 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
2552 return;
2554 elsif Nkind (Nam) = N_Indexed_Component then
2555 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
2556 return;
2558 elsif Nkind (Nam) = N_Character_Literal then
2559 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
2560 return;
2562 elsif not Is_Entity_Name (Nam)
2563 or else not Is_Overloadable (Entity (Nam))
2564 then
2565 -- Do not mention the renaming if it comes from an instance
2567 if not Is_Actual then
2568 Error_Msg_N ("expect valid subprogram name in renaming", N);
2569 else
2570 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
2571 end if;
2573 return;
2574 end if;
2576 -- Find the renamed entity that matches the given specification. Disable
2577 -- Ada_83 because there is no requirement of full conformance between
2578 -- renamed entity and new entity, even though the same circuit is used.
2580 -- This is a bit of a kludge, which introduces a really irregular use of
2581 -- Ada_Version[_Explicit]. Would be nice to find cleaner way to do this
2582 -- ???
2584 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
2585 Ada_Version_Explicit := Ada_Version;
2587 if No (Old_S) then
2588 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2590 -- The visible operation may be an inherited abstract operation that
2591 -- was overridden in the private part, in which case a call will
2592 -- dispatch to the overriding operation. Use the overriding one in
2593 -- the renaming declaration, to prevent spurious errors below.
2595 if Is_Overloadable (Old_S)
2596 and then Is_Abstract_Subprogram (Old_S)
2597 and then No (DTC_Entity (Old_S))
2598 and then Present (Alias (Old_S))
2599 and then not Is_Abstract_Subprogram (Alias (Old_S))
2600 and then Present (Overridden_Operation (Alias (Old_S)))
2601 then
2602 Old_S := Alias (Old_S);
2603 end if;
2605 -- When the renamed subprogram is overloaded and used as an actual
2606 -- of a generic, its entity is set to the first available homonym.
2607 -- We must first disambiguate the name, then set the proper entity.
2609 if Is_Actual and then Is_Overloaded (Nam) then
2610 Set_Entity (Nam, Old_S);
2611 end if;
2612 end if;
2614 -- Most common case: subprogram renames subprogram. No body is generated
2615 -- in this case, so we must indicate the declaration is complete as is.
2616 -- and inherit various attributes of the renamed subprogram.
2618 if No (Rename_Spec) then
2619 Set_Has_Completion (New_S);
2620 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
2621 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
2622 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
2624 -- Ada 2005 (AI-423): Check the consistency of null exclusions
2625 -- between a subprogram and its correct renaming.
2627 -- Note: the Any_Id check is a guard that prevents compiler crashes
2628 -- when performing a null exclusion check between a renaming and a
2629 -- renamed subprogram that has been found to be illegal.
2631 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
2632 Check_Null_Exclusion
2633 (Ren => New_S,
2634 Sub => Entity (Nam));
2635 end if;
2637 -- Enforce the Ada 2005 rule that the renamed entity cannot require
2638 -- overriding. The flag Requires_Overriding is set very selectively
2639 -- and misses some other illegal cases. The additional conditions
2640 -- checked below are sufficient but not necessary ???
2642 -- The rule does not apply to the renaming generated for an actual
2643 -- subprogram in an instance.
2645 if Is_Actual then
2646 null;
2648 -- Guard against previous errors, and omit renamings of predefined
2649 -- operators.
2651 elsif not Ekind_In (Old_S, E_Function, E_Procedure) then
2652 null;
2654 elsif Requires_Overriding (Old_S)
2655 or else
2656 (Is_Abstract_Subprogram (Old_S)
2657 and then Present (Find_Dispatching_Type (Old_S))
2658 and then
2659 not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
2660 then
2661 Error_Msg_N
2662 ("renamed entity cannot be "
2663 & "subprogram that requires overriding (RM 8.5.4 (5.1))", N);
2664 end if;
2665 end if;
2667 if Old_S /= Any_Id then
2668 if Is_Actual and then From_Default (N) then
2670 -- This is an implicit reference to the default actual
2672 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
2674 else
2675 Generate_Reference (Old_S, Nam);
2676 end if;
2678 Check_Internal_Protected_Use (N, Old_S);
2680 -- For a renaming-as-body, require subtype conformance, but if the
2681 -- declaration being completed has not been frozen, then inherit the
2682 -- convention of the renamed subprogram prior to checking conformance
2683 -- (unless the renaming has an explicit convention established; the
2684 -- rule stated in the RM doesn't seem to address this ???).
2686 if Present (Rename_Spec) then
2687 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
2688 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
2690 if not Is_Frozen (Rename_Spec) then
2691 if not Has_Convention_Pragma (Rename_Spec) then
2692 Set_Convention (New_S, Convention (Old_S));
2693 end if;
2695 if Ekind (Old_S) /= E_Operator then
2696 Check_Mode_Conformant (New_S, Old_S, Spec);
2697 end if;
2699 if Original_Subprogram (Old_S) = Rename_Spec then
2700 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
2701 end if;
2702 else
2703 Check_Subtype_Conformant (New_S, Old_S, Spec);
2704 end if;
2706 Check_Frozen_Renaming (N, Rename_Spec);
2708 -- Check explicitly that renamed entity is not intrinsic, because
2709 -- in a generic the renamed body is not built. In this case,
2710 -- the renaming_as_body is a completion.
2712 if Inside_A_Generic then
2713 if Is_Frozen (Rename_Spec)
2714 and then Is_Intrinsic_Subprogram (Old_S)
2715 then
2716 Error_Msg_N
2717 ("subprogram in renaming_as_body cannot be intrinsic",
2718 Name (N));
2719 end if;
2721 Set_Has_Completion (Rename_Spec);
2722 end if;
2724 elsif Ekind (Old_S) /= E_Operator then
2726 -- If this a defaulted subprogram for a class-wide actual there is
2727 -- no check for mode conformance, given that the signatures don't
2728 -- match (the source mentions T but the actual mentions T'Class).
2730 if CW_Actual then
2731 null;
2732 elsif not Is_Actual or else No (Enclosing_Instance) then
2733 Check_Mode_Conformant (New_S, Old_S);
2734 end if;
2736 if Is_Actual and then Error_Posted (New_S) then
2737 Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
2738 end if;
2739 end if;
2741 if No (Rename_Spec) then
2743 -- The parameter profile of the new entity is that of the renamed
2744 -- entity: the subtypes given in the specification are irrelevant.
2746 Inherit_Renamed_Profile (New_S, Old_S);
2748 -- A call to the subprogram is transformed into a call to the
2749 -- renamed entity. This is transitive if the renamed entity is
2750 -- itself a renaming.
2752 if Present (Alias (Old_S)) then
2753 Set_Alias (New_S, Alias (Old_S));
2754 else
2755 Set_Alias (New_S, Old_S);
2756 end if;
2758 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
2759 -- renaming as body, since the entity in this case is not an
2760 -- intrinsic (it calls an intrinsic, but we have a real body for
2761 -- this call, and it is in this body that the required intrinsic
2762 -- processing will take place).
2764 -- Also, if this is a renaming of inequality, the renamed operator
2765 -- is intrinsic, but what matters is the corresponding equality
2766 -- operator, which may be user-defined.
2768 Set_Is_Intrinsic_Subprogram
2769 (New_S,
2770 Is_Intrinsic_Subprogram (Old_S)
2771 and then
2772 (Chars (Old_S) /= Name_Op_Ne
2773 or else Ekind (Old_S) = E_Operator
2774 or else Is_Intrinsic_Subprogram
2775 (Corresponding_Equality (Old_S))));
2777 if Ekind (Alias (New_S)) = E_Operator then
2778 Set_Has_Delayed_Freeze (New_S, False);
2779 end if;
2781 -- If the renaming corresponds to an association for an abstract
2782 -- formal subprogram, then various attributes must be set to
2783 -- indicate that the renaming is an abstract dispatching operation
2784 -- with a controlling type.
2786 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
2788 -- Mark the renaming as abstract here, so Find_Dispatching_Type
2789 -- see it as corresponding to a generic association for a
2790 -- formal abstract subprogram
2792 Set_Is_Abstract_Subprogram (New_S);
2794 declare
2795 New_S_Ctrl_Type : constant Entity_Id :=
2796 Find_Dispatching_Type (New_S);
2797 Old_S_Ctrl_Type : constant Entity_Id :=
2798 Find_Dispatching_Type (Old_S);
2800 begin
2801 if Old_S_Ctrl_Type /= New_S_Ctrl_Type then
2802 Error_Msg_NE
2803 ("actual must be dispatching subprogram for type&",
2804 Nam, New_S_Ctrl_Type);
2806 else
2807 Set_Is_Dispatching_Operation (New_S);
2808 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
2810 -- If the actual in the formal subprogram is itself a
2811 -- formal abstract subprogram association, there's no
2812 -- dispatch table component or position to inherit.
2814 if Present (DTC_Entity (Old_S)) then
2815 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
2816 Set_DT_Position (New_S, DT_Position (Old_S));
2817 end if;
2818 end if;
2819 end;
2820 end if;
2821 end if;
2823 if Is_Actual then
2824 null;
2826 -- The following is illegal, because F hides whatever other F may
2827 -- be around:
2828 -- function F (...) renames F;
2830 elsif Old_S = New_S
2831 or else (Nkind (Nam) /= N_Expanded_Name
2832 and then Chars (Old_S) = Chars (New_S))
2833 then
2834 Error_Msg_N ("subprogram cannot rename itself", N);
2836 -- This is illegal even if we use a selector:
2837 -- function F (...) renames Pkg.F;
2838 -- because F is still hidden.
2840 elsif Nkind (Nam) = N_Expanded_Name
2841 and then Entity (Prefix (Nam)) = Current_Scope
2842 and then Chars (Selector_Name (Nam)) = Chars (New_S)
2843 then
2844 -- This is an error, but we overlook the error and accept the
2845 -- renaming if the special Overriding_Renamings mode is in effect.
2847 if not Overriding_Renamings then
2848 Error_Msg_NE
2849 ("implicit operation& is not visible (RM 8.3 (15))",
2850 Nam, Old_S);
2851 end if;
2852 end if;
2854 Set_Convention (New_S, Convention (Old_S));
2856 if Is_Abstract_Subprogram (Old_S) then
2857 if Present (Rename_Spec) then
2858 Error_Msg_N
2859 ("a renaming-as-body cannot rename an abstract subprogram",
2861 Set_Has_Completion (Rename_Spec);
2862 else
2863 Set_Is_Abstract_Subprogram (New_S);
2864 end if;
2865 end if;
2867 Check_Library_Unit_Renaming (N, Old_S);
2869 -- Pathological case: procedure renames entry in the scope of its
2870 -- task. Entry is given by simple name, but body must be built for
2871 -- procedure. Of course if called it will deadlock.
2873 if Ekind (Old_S) = E_Entry then
2874 Set_Has_Completion (New_S, False);
2875 Set_Alias (New_S, Empty);
2876 end if;
2878 if Is_Actual then
2879 Freeze_Before (N, Old_S);
2880 Freeze_Actual_Profile;
2881 Set_Has_Delayed_Freeze (New_S, False);
2882 Freeze_Before (N, New_S);
2884 -- An abstract subprogram is only allowed as an actual in the case
2885 -- where the formal subprogram is also abstract.
2887 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
2888 and then Is_Abstract_Subprogram (Old_S)
2889 and then not Is_Abstract_Subprogram (Formal_Spec)
2890 then
2891 Error_Msg_N
2892 ("abstract subprogram not allowed as generic actual", Nam);
2893 end if;
2894 end if;
2896 else
2897 -- A common error is to assume that implicit operators for types are
2898 -- defined in Standard, or in the scope of a subtype. In those cases
2899 -- where the renamed entity is given with an expanded name, it is
2900 -- worth mentioning that operators for the type are not declared in
2901 -- the scope given by the prefix.
2903 if Nkind (Nam) = N_Expanded_Name
2904 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
2905 and then Scope (Entity (Nam)) = Standard_Standard
2906 then
2907 declare
2908 T : constant Entity_Id :=
2909 Base_Type (Etype (First_Formal (New_S)));
2910 begin
2911 Error_Msg_Node_2 := Prefix (Nam);
2912 Error_Msg_NE
2913 ("operator for type& is not declared in&", Prefix (Nam), T);
2914 end;
2916 else
2917 Error_Msg_NE
2918 ("no visible subprogram matches the specification for&",
2919 Spec, New_S);
2920 end if;
2922 if Present (Candidate_Renaming) then
2923 declare
2924 F1 : Entity_Id;
2925 F2 : Entity_Id;
2926 T1 : Entity_Id;
2928 begin
2929 F1 := First_Formal (Candidate_Renaming);
2930 F2 := First_Formal (New_S);
2931 T1 := First_Subtype (Etype (F1));
2932 while Present (F1) and then Present (F2) loop
2933 Next_Formal (F1);
2934 Next_Formal (F2);
2935 end loop;
2937 if Present (F1) and then Present (Default_Value (F1)) then
2938 if Present (Next_Formal (F1)) then
2939 Error_Msg_NE
2940 ("\missing specification for &" &
2941 " and other formals with defaults", Spec, F1);
2942 else
2943 Error_Msg_NE
2944 ("\missing specification for &", Spec, F1);
2945 end if;
2946 end if;
2948 if Nkind (Nam) = N_Operator_Symbol
2949 and then From_Default (N)
2950 then
2951 Error_Msg_Node_2 := T1;
2952 Error_Msg_NE
2953 ("default & on & is not directly visible",
2954 Nam, Nam);
2955 end if;
2956 end;
2957 end if;
2958 end if;
2960 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
2961 -- controlling access parameters are known non-null for the renamed
2962 -- subprogram. Test also applies to a subprogram instantiation that
2963 -- is dispatching. Test is skipped if some previous error was detected
2964 -- that set Old_S to Any_Id.
2966 if Ada_Version >= Ada_2005
2967 and then Old_S /= Any_Id
2968 and then not Is_Dispatching_Operation (Old_S)
2969 and then Is_Dispatching_Operation (New_S)
2970 then
2971 declare
2972 Old_F : Entity_Id;
2973 New_F : Entity_Id;
2975 begin
2976 Old_F := First_Formal (Old_S);
2977 New_F := First_Formal (New_S);
2978 while Present (Old_F) loop
2979 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
2980 and then Is_Controlling_Formal (New_F)
2981 and then not Can_Never_Be_Null (Old_F)
2982 then
2983 Error_Msg_N ("access parameter is controlling,", New_F);
2984 Error_Msg_NE
2985 ("\corresponding parameter of& "
2986 & "must be explicitly null excluding", New_F, Old_S);
2987 end if;
2989 Next_Formal (Old_F);
2990 Next_Formal (New_F);
2991 end loop;
2992 end;
2993 end if;
2995 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
2996 -- is to warn if an operator is being renamed as a different operator.
2997 -- If the operator is predefined, examine the kind of the entity, not
2998 -- the abbreviated declaration in Standard.
3000 if Comes_From_Source (N)
3001 and then Present (Old_S)
3002 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3003 or else Ekind (Old_S) = E_Operator)
3004 and then Nkind (New_S) = N_Defining_Operator_Symbol
3005 and then Chars (Old_S) /= Chars (New_S)
3006 then
3007 Error_Msg_NE
3008 ("& is being renamed as a different operator??", N, Old_S);
3009 end if;
3011 -- Check for renaming of obsolescent subprogram
3013 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3015 -- Another warning or some utility: if the new subprogram as the same
3016 -- name as the old one, the old one is not hidden by an outer homograph,
3017 -- the new one is not a public symbol, and the old one is otherwise
3018 -- directly visible, the renaming is superfluous.
3020 if Chars (Old_S) = Chars (New_S)
3021 and then Comes_From_Source (N)
3022 and then Scope (Old_S) /= Standard_Standard
3023 and then Warn_On_Redundant_Constructs
3024 and then (Is_Immediately_Visible (Old_S)
3025 or else Is_Potentially_Use_Visible (Old_S))
3026 and then Is_Overloadable (Current_Scope)
3027 and then Chars (Current_Scope) /= Chars (Old_S)
3028 then
3029 Error_Msg_N
3030 ("redundant renaming, entity is directly visible?r?", Name (N));
3031 end if;
3033 -- Implementation-defined aspect specifications can appear in a renaming
3034 -- declaration, but not language-defined ones. The call to procedure
3035 -- Analyze_Aspect_Specifications will take care of this error check.
3037 if Has_Aspects (N) then
3038 Analyze_Aspect_Specifications (N, New_S);
3039 end if;
3041 Ada_Version := Save_AV;
3042 Ada_Version_Explicit := Save_AV_Exp;
3043 end Analyze_Subprogram_Renaming;
3045 -------------------------
3046 -- Analyze_Use_Package --
3047 -------------------------
3049 -- Resolve the package names in the use clause, and make all the visible
3050 -- entities defined in the package potentially use-visible. If the package
3051 -- is already in use from a previous use clause, its visible entities are
3052 -- already use-visible. In that case, mark the occurrence as a redundant
3053 -- use. If the package is an open scope, i.e. if the use clause occurs
3054 -- within the package itself, ignore it.
3056 procedure Analyze_Use_Package (N : Node_Id) is
3057 Pack_Name : Node_Id;
3058 Pack : Entity_Id;
3060 -- Start of processing for Analyze_Use_Package
3062 begin
3063 Check_SPARK_Restriction ("use clause is not allowed", N);
3065 Set_Hidden_By_Use_Clause (N, No_Elist);
3067 -- Use clause not allowed in a spec of a predefined package declaration
3068 -- except that packages whose file name starts a-n are OK (these are
3069 -- children of Ada.Numerics, which are never loaded by Rtsfind).
3071 if Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
3072 and then Name_Buffer (1 .. 3) /= "a-n"
3073 and then
3074 Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3075 then
3076 Error_Msg_N ("use clause not allowed in predefined spec", N);
3077 end if;
3079 -- Chain clause to list of use clauses in current scope
3081 if Nkind (Parent (N)) /= N_Compilation_Unit then
3082 Chain_Use_Clause (N);
3083 end if;
3085 -- Loop through package names to identify referenced packages
3087 Pack_Name := First (Names (N));
3088 while Present (Pack_Name) loop
3089 Analyze (Pack_Name);
3091 if Nkind (Parent (N)) = N_Compilation_Unit
3092 and then Nkind (Pack_Name) = N_Expanded_Name
3093 then
3094 declare
3095 Pref : Node_Id;
3097 begin
3098 Pref := Prefix (Pack_Name);
3099 while Nkind (Pref) = N_Expanded_Name loop
3100 Pref := Prefix (Pref);
3101 end loop;
3103 if Entity (Pref) = Standard_Standard then
3104 Error_Msg_N
3105 ("predefined package Standard cannot appear"
3106 & " in a context clause", Pref);
3107 end if;
3108 end;
3109 end if;
3111 Next (Pack_Name);
3112 end loop;
3114 -- Loop through package names to mark all entities as potentially
3115 -- use visible.
3117 Pack_Name := First (Names (N));
3118 while Present (Pack_Name) loop
3119 if Is_Entity_Name (Pack_Name) then
3120 Pack := Entity (Pack_Name);
3122 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
3123 if Ekind (Pack) = E_Generic_Package then
3124 Error_Msg_N -- CODEFIX
3125 ("a generic package is not allowed in a use clause",
3126 Pack_Name);
3127 else
3128 Error_Msg_N ("& is not a usable package", Pack_Name);
3129 end if;
3131 else
3132 if Nkind (Parent (N)) = N_Compilation_Unit then
3133 Check_In_Previous_With_Clause (N, Pack_Name);
3134 end if;
3136 if Applicable_Use (Pack_Name) then
3137 Use_One_Package (Pack, N);
3138 end if;
3139 end if;
3141 -- Report error because name denotes something other than a package
3143 else
3144 Error_Msg_N ("& is not a package", Pack_Name);
3145 end if;
3147 Next (Pack_Name);
3148 end loop;
3149 end Analyze_Use_Package;
3151 ----------------------
3152 -- Analyze_Use_Type --
3153 ----------------------
3155 procedure Analyze_Use_Type (N : Node_Id) is
3156 E : Entity_Id;
3157 Id : Node_Id;
3159 begin
3160 Set_Hidden_By_Use_Clause (N, No_Elist);
3162 -- Chain clause to list of use clauses in current scope
3164 if Nkind (Parent (N)) /= N_Compilation_Unit then
3165 Chain_Use_Clause (N);
3166 end if;
3168 -- If the Used_Operations list is already initialized, the clause has
3169 -- been analyzed previously, and it is begin reinstalled, for example
3170 -- when the clause appears in a package spec and we are compiling the
3171 -- corresponding package body. In that case, make the entities on the
3172 -- existing list use_visible, and mark the corresponding types In_Use.
3174 if Present (Used_Operations (N)) then
3175 declare
3176 Mark : Node_Id;
3177 Elmt : Elmt_Id;
3179 begin
3180 Mark := First (Subtype_Marks (N));
3181 while Present (Mark) loop
3182 Use_One_Type (Mark, Installed => True);
3183 Next (Mark);
3184 end loop;
3186 Elmt := First_Elmt (Used_Operations (N));
3187 while Present (Elmt) loop
3188 Set_Is_Potentially_Use_Visible (Node (Elmt));
3189 Next_Elmt (Elmt);
3190 end loop;
3191 end;
3193 return;
3194 end if;
3196 -- Otherwise, create new list and attach to it the operations that
3197 -- are made use-visible by the clause.
3199 Set_Used_Operations (N, New_Elmt_List);
3200 Id := First (Subtype_Marks (N));
3201 while Present (Id) loop
3202 Find_Type (Id);
3203 E := Entity (Id);
3205 if E /= Any_Type then
3206 Use_One_Type (Id);
3208 if Nkind (Parent (N)) = N_Compilation_Unit then
3209 if Nkind (Id) = N_Identifier then
3210 Error_Msg_N ("type is not directly visible", Id);
3212 elsif Is_Child_Unit (Scope (E))
3213 and then Scope (E) /= System_Aux_Id
3214 then
3215 Check_In_Previous_With_Clause (N, Prefix (Id));
3216 end if;
3217 end if;
3219 else
3220 -- If the use_type_clause appears in a compilation unit context,
3221 -- check whether it comes from a unit that may appear in a
3222 -- limited_with_clause, for a better error message.
3224 if Nkind (Parent (N)) = N_Compilation_Unit
3225 and then Nkind (Id) /= N_Identifier
3226 then
3227 declare
3228 Item : Node_Id;
3229 Pref : Node_Id;
3231 function Mentioned (Nam : Node_Id) return Boolean;
3232 -- Check whether the prefix of expanded name for the type
3233 -- appears in the prefix of some limited_with_clause.
3235 ---------------
3236 -- Mentioned --
3237 ---------------
3239 function Mentioned (Nam : Node_Id) return Boolean is
3240 begin
3241 return Nkind (Name (Item)) = N_Selected_Component
3242 and then Chars (Prefix (Name (Item))) = Chars (Nam);
3243 end Mentioned;
3245 begin
3246 Pref := Prefix (Id);
3247 Item := First (Context_Items (Parent (N)));
3248 while Present (Item) and then Item /= N loop
3249 if Nkind (Item) = N_With_Clause
3250 and then Limited_Present (Item)
3251 and then Mentioned (Pref)
3252 then
3253 Change_Error_Text
3254 (Get_Msg_Id, "premature usage of incomplete type");
3255 end if;
3257 Next (Item);
3258 end loop;
3259 end;
3260 end if;
3261 end if;
3263 Next (Id);
3264 end loop;
3265 end Analyze_Use_Type;
3267 --------------------
3268 -- Applicable_Use --
3269 --------------------
3271 function Applicable_Use (Pack_Name : Node_Id) return Boolean is
3272 Pack : constant Entity_Id := Entity (Pack_Name);
3274 begin
3275 if In_Open_Scopes (Pack) then
3276 if Warn_On_Redundant_Constructs and then Pack = Current_Scope then
3277 Error_Msg_NE -- CODEFIX
3278 ("& is already use-visible within itself?r?", Pack_Name, Pack);
3279 end if;
3281 return False;
3283 elsif In_Use (Pack) then
3284 Note_Redundant_Use (Pack_Name);
3285 return False;
3287 elsif Present (Renamed_Object (Pack))
3288 and then In_Use (Renamed_Object (Pack))
3289 then
3290 Note_Redundant_Use (Pack_Name);
3291 return False;
3293 else
3294 return True;
3295 end if;
3296 end Applicable_Use;
3298 ------------------------
3299 -- Attribute_Renaming --
3300 ------------------------
3302 procedure Attribute_Renaming (N : Node_Id) is
3303 Loc : constant Source_Ptr := Sloc (N);
3304 Nam : constant Node_Id := Name (N);
3305 Spec : constant Node_Id := Specification (N);
3306 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
3307 Aname : constant Name_Id := Attribute_Name (Nam);
3309 Form_Num : Nat := 0;
3310 Expr_List : List_Id := No_List;
3312 Attr_Node : Node_Id;
3313 Body_Node : Node_Id;
3314 Param_Spec : Node_Id;
3316 begin
3317 Generate_Definition (New_S);
3319 -- This procedure is called in the context of subprogram renaming, and
3320 -- thus the attribute must be one that is a subprogram. All of those
3321 -- have at least one formal parameter, with the exceptions of AST_Entry
3322 -- (which is a real oddity, it is odd that this can be renamed at all!)
3323 -- and the GNAT attribute 'Img, which GNAT treats as renameable.
3325 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
3326 if Aname /= Name_AST_Entry and then Aname /= Name_Img then
3327 Error_Msg_N
3328 ("subprogram renaming an attribute must have formals", N);
3329 return;
3330 end if;
3332 else
3333 Param_Spec := First (Parameter_Specifications (Spec));
3334 while Present (Param_Spec) loop
3335 Form_Num := Form_Num + 1;
3337 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
3338 Find_Type (Parameter_Type (Param_Spec));
3340 -- The profile of the new entity denotes the base type (s) of
3341 -- the types given in the specification. For access parameters
3342 -- there are no subtypes involved.
3344 Rewrite (Parameter_Type (Param_Spec),
3345 New_Reference_To
3346 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
3347 end if;
3349 if No (Expr_List) then
3350 Expr_List := New_List;
3351 end if;
3353 Append_To (Expr_List,
3354 Make_Identifier (Loc,
3355 Chars => Chars (Defining_Identifier (Param_Spec))));
3357 -- The expressions in the attribute reference are not freeze
3358 -- points. Neither is the attribute as a whole, see below.
3360 Set_Must_Not_Freeze (Last (Expr_List));
3361 Next (Param_Spec);
3362 end loop;
3363 end if;
3365 -- Immediate error if too many formals. Other mismatches in number or
3366 -- types of parameters are detected when we analyze the body of the
3367 -- subprogram that we construct.
3369 if Form_Num > 2 then
3370 Error_Msg_N ("too many formals for attribute", N);
3372 -- Error if the attribute reference has expressions that look like
3373 -- formal parameters.
3375 elsif Present (Expressions (Nam)) then
3376 Error_Msg_N ("illegal expressions in attribute reference", Nam);
3378 elsif
3379 Nam_In (Aname, Name_Compose, Name_Exponent, Name_Leading_Part,
3380 Name_Pos, Name_Round, Name_Scaling,
3381 Name_Val)
3382 then
3383 if Nkind (N) = N_Subprogram_Renaming_Declaration
3384 and then Present (Corresponding_Formal_Spec (N))
3385 then
3386 Error_Msg_N
3387 ("generic actual cannot be attribute involving universal type",
3388 Nam);
3389 else
3390 Error_Msg_N
3391 ("attribute involving a universal type cannot be renamed",
3392 Nam);
3393 end if;
3394 end if;
3396 -- AST_Entry is an odd case. It doesn't really make much sense to allow
3397 -- it to be renamed, but that's the DEC rule, so we have to do it right.
3398 -- The point is that the AST_Entry call should be made now, and what the
3399 -- function will return is the returned value.
3401 -- Note that there is no Expr_List in this case anyway
3403 if Aname = Name_AST_Entry then
3404 declare
3405 Ent : constant Entity_Id := Make_Temporary (Loc, 'R', Nam);
3406 Decl : Node_Id;
3408 begin
3409 Decl :=
3410 Make_Object_Declaration (Loc,
3411 Defining_Identifier => Ent,
3412 Object_Definition =>
3413 New_Occurrence_Of (RTE (RE_AST_Handler), Loc),
3414 Expression => Nam,
3415 Constant_Present => True);
3417 Set_Assignment_OK (Decl, True);
3418 Insert_Action (N, Decl);
3419 Attr_Node := Make_Identifier (Loc, Chars (Ent));
3420 end;
3422 -- For all other attributes, we rewrite the attribute node to have
3423 -- a list of expressions corresponding to the subprogram formals.
3424 -- A renaming declaration is not a freeze point, and the analysis of
3425 -- the attribute reference should not freeze the type of the prefix.
3427 else
3428 Attr_Node :=
3429 Make_Attribute_Reference (Loc,
3430 Prefix => Prefix (Nam),
3431 Attribute_Name => Aname,
3432 Expressions => Expr_List);
3434 Set_Must_Not_Freeze (Attr_Node);
3435 Set_Must_Not_Freeze (Prefix (Nam));
3436 end if;
3438 -- Case of renaming a function
3440 if Nkind (Spec) = N_Function_Specification then
3441 if Is_Procedure_Attribute_Name (Aname) then
3442 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
3443 return;
3444 end if;
3446 Find_Type (Result_Definition (Spec));
3447 Rewrite (Result_Definition (Spec),
3448 New_Reference_To (
3449 Base_Type (Entity (Result_Definition (Spec))), Loc));
3451 Body_Node :=
3452 Make_Subprogram_Body (Loc,
3453 Specification => Spec,
3454 Declarations => New_List,
3455 Handled_Statement_Sequence =>
3456 Make_Handled_Sequence_Of_Statements (Loc,
3457 Statements => New_List (
3458 Make_Simple_Return_Statement (Loc,
3459 Expression => Attr_Node))));
3461 -- Case of renaming a procedure
3463 else
3464 if not Is_Procedure_Attribute_Name (Aname) then
3465 Error_Msg_N ("attribute can only be renamed as function", Nam);
3466 return;
3467 end if;
3469 Body_Node :=
3470 Make_Subprogram_Body (Loc,
3471 Specification => Spec,
3472 Declarations => New_List,
3473 Handled_Statement_Sequence =>
3474 Make_Handled_Sequence_Of_Statements (Loc,
3475 Statements => New_List (Attr_Node)));
3476 end if;
3478 -- In case of tagged types we add the body of the generated function to
3479 -- the freezing actions of the type (because in the general case such
3480 -- type is still not frozen). We exclude from this processing generic
3481 -- formal subprograms found in instantiations and AST_Entry renamings.
3483 -- We must exclude VM targets and restricted run-time libraries because
3484 -- entity AST_Handler is defined in package System.Aux_Dec which is not
3485 -- available in those platforms. Note that we cannot use the function
3486 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
3487 -- the ZFP run-time library is not defined as a profile, and we do not
3488 -- want to deal with AST_Handler in ZFP mode.
3490 if VM_Target = No_VM
3491 and then not Configurable_Run_Time_Mode
3492 and then not Present (Corresponding_Formal_Spec (N))
3493 and then Etype (Nam) /= RTE (RE_AST_Handler)
3494 then
3495 declare
3496 P : constant Node_Id := Prefix (Nam);
3498 begin
3499 -- The prefix of 'Img is an object that is evaluated for each call
3500 -- of the function that renames it.
3502 if Aname = Name_Img then
3503 Preanalyze_And_Resolve (P);
3505 -- For all other attribute renamings, the prefix is a subtype
3507 else
3508 Find_Type (P);
3509 end if;
3511 if Is_Tagged_Type (Etype (P)) then
3512 Ensure_Freeze_Node (Etype (P));
3513 Append_Freeze_Action (Etype (P), Body_Node);
3514 else
3515 Rewrite (N, Body_Node);
3516 Analyze (N);
3517 Set_Etype (New_S, Base_Type (Etype (New_S)));
3518 end if;
3519 end;
3521 -- Generic formal subprograms or AST_Handler renaming
3523 else
3524 Rewrite (N, Body_Node);
3525 Analyze (N);
3526 Set_Etype (New_S, Base_Type (Etype (New_S)));
3527 end if;
3529 if Is_Compilation_Unit (New_S) then
3530 Error_Msg_N
3531 ("a library unit can only rename another library unit", N);
3532 end if;
3534 -- We suppress elaboration warnings for the resulting entity, since
3535 -- clearly they are not needed, and more particularly, in the case
3536 -- of a generic formal subprogram, the resulting entity can appear
3537 -- after the instantiation itself, and thus look like a bogus case
3538 -- of access before elaboration.
3540 Set_Suppress_Elaboration_Warnings (New_S);
3542 end Attribute_Renaming;
3544 ----------------------
3545 -- Chain_Use_Clause --
3546 ----------------------
3548 procedure Chain_Use_Clause (N : Node_Id) is
3549 Pack : Entity_Id;
3550 Level : Int := Scope_Stack.Last;
3552 begin
3553 if not Is_Compilation_Unit (Current_Scope)
3554 or else not Is_Child_Unit (Current_Scope)
3555 then
3556 null; -- Common case
3558 elsif Defining_Entity (Parent (N)) = Current_Scope then
3559 null; -- Common case for compilation unit
3561 else
3562 -- If declaration appears in some other scope, it must be in some
3563 -- parent unit when compiling a child.
3565 Pack := Defining_Entity (Parent (N));
3566 if not In_Open_Scopes (Pack) then
3567 null; -- default as well
3569 else
3570 -- Find entry for parent unit in scope stack
3572 while Scope_Stack.Table (Level).Entity /= Pack loop
3573 Level := Level - 1;
3574 end loop;
3575 end if;
3576 end if;
3578 Set_Next_Use_Clause (N,
3579 Scope_Stack.Table (Level).First_Use_Clause);
3580 Scope_Stack.Table (Level).First_Use_Clause := N;
3581 end Chain_Use_Clause;
3583 ---------------------------
3584 -- Check_Frozen_Renaming --
3585 ---------------------------
3587 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
3588 B_Node : Node_Id;
3589 Old_S : Entity_Id;
3591 begin
3592 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
3593 B_Node :=
3594 Build_Renamed_Body
3595 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
3597 if Is_Entity_Name (Name (N)) then
3598 Old_S := Entity (Name (N));
3600 if not Is_Frozen (Old_S)
3601 and then Operating_Mode /= Check_Semantics
3602 then
3603 Append_Freeze_Action (Old_S, B_Node);
3604 else
3605 Insert_After (N, B_Node);
3606 Analyze (B_Node);
3607 end if;
3609 if Is_Intrinsic_Subprogram (Old_S) and then not In_Instance then
3610 Error_Msg_N
3611 ("subprogram used in renaming_as_body cannot be intrinsic",
3612 Name (N));
3613 end if;
3615 else
3616 Insert_After (N, B_Node);
3617 Analyze (B_Node);
3618 end if;
3619 end if;
3620 end Check_Frozen_Renaming;
3622 -------------------------------
3623 -- Set_Entity_Or_Discriminal --
3624 -------------------------------
3626 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
3627 P : Node_Id;
3629 begin
3630 -- If the entity is not a discriminant, or else expansion is disabled,
3631 -- simply set the entity.
3633 if not In_Spec_Expression
3634 or else Ekind (E) /= E_Discriminant
3635 or else Inside_A_Generic
3636 then
3637 Set_Entity_With_Style_Check (N, E);
3639 -- The replacement of a discriminant by the corresponding discriminal
3640 -- is not done for a task discriminant that appears in a default
3641 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
3642 -- for details on their handling.
3644 elsif Is_Concurrent_Type (Scope (E)) then
3645 P := Parent (N);
3646 while Present (P)
3647 and then not Nkind_In (P, N_Parameter_Specification,
3648 N_Component_Declaration)
3649 loop
3650 P := Parent (P);
3651 end loop;
3653 if Present (P)
3654 and then Nkind (P) = N_Parameter_Specification
3655 then
3656 null;
3658 else
3659 Set_Entity (N, Discriminal (E));
3660 end if;
3662 -- Otherwise, this is a discriminant in a context in which
3663 -- it is a reference to the corresponding parameter of the
3664 -- init proc for the enclosing type.
3666 else
3667 Set_Entity (N, Discriminal (E));
3668 end if;
3669 end Set_Entity_Or_Discriminal;
3671 -----------------------------------
3672 -- Check_In_Previous_With_Clause --
3673 -----------------------------------
3675 procedure Check_In_Previous_With_Clause
3676 (N : Node_Id;
3677 Nam : Entity_Id)
3679 Pack : constant Entity_Id := Entity (Original_Node (Nam));
3680 Item : Node_Id;
3681 Par : Node_Id;
3683 begin
3684 Item := First (Context_Items (Parent (N)));
3685 while Present (Item) and then Item /= N loop
3686 if Nkind (Item) = N_With_Clause
3688 -- Protect the frontend against previous critical errors
3690 and then Nkind (Name (Item)) /= N_Selected_Component
3691 and then Entity (Name (Item)) = Pack
3692 then
3693 Par := Nam;
3695 -- Find root library unit in with_clause
3697 while Nkind (Par) = N_Expanded_Name loop
3698 Par := Prefix (Par);
3699 end loop;
3701 if Is_Child_Unit (Entity (Original_Node (Par))) then
3702 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
3703 else
3704 return;
3705 end if;
3706 end if;
3708 Next (Item);
3709 end loop;
3711 -- On exit, package is not mentioned in a previous with_clause.
3712 -- Check if its prefix is.
3714 if Nkind (Nam) = N_Expanded_Name then
3715 Check_In_Previous_With_Clause (N, Prefix (Nam));
3717 elsif Pack /= Any_Id then
3718 Error_Msg_NE ("& is not visible", Nam, Pack);
3719 end if;
3720 end Check_In_Previous_With_Clause;
3722 ---------------------------------
3723 -- Check_Library_Unit_Renaming --
3724 ---------------------------------
3726 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
3727 New_E : Entity_Id;
3729 begin
3730 if Nkind (Parent (N)) /= N_Compilation_Unit then
3731 return;
3733 -- Check for library unit. Note that we used to check for the scope
3734 -- being Standard here, but that was wrong for Standard itself.
3736 elsif not Is_Compilation_Unit (Old_E)
3737 and then not Is_Child_Unit (Old_E)
3738 then
3739 Error_Msg_N ("renamed unit must be a library unit", Name (N));
3741 -- Entities defined in Standard (operators and boolean literals) cannot
3742 -- be renamed as library units.
3744 elsif Scope (Old_E) = Standard_Standard
3745 and then Sloc (Old_E) = Standard_Location
3746 then
3747 Error_Msg_N ("renamed unit must be a library unit", Name (N));
3749 elsif Present (Parent_Spec (N))
3750 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
3751 and then not Is_Child_Unit (Old_E)
3752 then
3753 Error_Msg_N
3754 ("renamed unit must be a child unit of generic parent", Name (N));
3756 elsif Nkind (N) in N_Generic_Renaming_Declaration
3757 and then Nkind (Name (N)) = N_Expanded_Name
3758 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
3759 and then Is_Generic_Unit (Old_E)
3760 then
3761 Error_Msg_N
3762 ("renamed generic unit must be a library unit", Name (N));
3764 elsif Is_Package_Or_Generic_Package (Old_E) then
3766 -- Inherit categorization flags
3768 New_E := Defining_Entity (N);
3769 Set_Is_Pure (New_E, Is_Pure (Old_E));
3770 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
3771 Set_Is_Remote_Call_Interface (New_E,
3772 Is_Remote_Call_Interface (Old_E));
3773 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
3774 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
3775 end if;
3776 end Check_Library_Unit_Renaming;
3778 ------------------------
3779 -- Enclosing_Instance --
3780 ------------------------
3782 function Enclosing_Instance return Entity_Id is
3783 S : Entity_Id;
3785 begin
3786 if not Is_Generic_Instance (Current_Scope) then
3787 return Empty;
3788 end if;
3790 S := Scope (Current_Scope);
3791 while S /= Standard_Standard loop
3792 if Is_Generic_Instance (S) then
3793 return S;
3794 end if;
3796 S := Scope (S);
3797 end loop;
3799 return Empty;
3800 end Enclosing_Instance;
3802 ---------------
3803 -- End_Scope --
3804 ---------------
3806 procedure End_Scope is
3807 Id : Entity_Id;
3808 Prev : Entity_Id;
3809 Outer : Entity_Id;
3811 begin
3812 Id := First_Entity (Current_Scope);
3813 while Present (Id) loop
3814 -- An entity in the current scope is not necessarily the first one
3815 -- on its homonym chain. Find its predecessor if any,
3816 -- If it is an internal entity, it will not be in the visibility
3817 -- chain altogether, and there is nothing to unchain.
3819 if Id /= Current_Entity (Id) then
3820 Prev := Current_Entity (Id);
3821 while Present (Prev)
3822 and then Present (Homonym (Prev))
3823 and then Homonym (Prev) /= Id
3824 loop
3825 Prev := Homonym (Prev);
3826 end loop;
3828 -- Skip to end of loop if Id is not in the visibility chain
3830 if No (Prev) or else Homonym (Prev) /= Id then
3831 goto Next_Ent;
3832 end if;
3834 else
3835 Prev := Empty;
3836 end if;
3838 Set_Is_Immediately_Visible (Id, False);
3840 Outer := Homonym (Id);
3841 while Present (Outer) and then Scope (Outer) = Current_Scope loop
3842 Outer := Homonym (Outer);
3843 end loop;
3845 -- Reset homonym link of other entities, but do not modify link
3846 -- between entities in current scope, so that the back-end can have
3847 -- a proper count of local overloadings.
3849 if No (Prev) then
3850 Set_Name_Entity_Id (Chars (Id), Outer);
3852 elsif Scope (Prev) /= Scope (Id) then
3853 Set_Homonym (Prev, Outer);
3854 end if;
3856 <<Next_Ent>>
3857 Next_Entity (Id);
3858 end loop;
3860 -- If the scope generated freeze actions, place them before the
3861 -- current declaration and analyze them. Type declarations and
3862 -- the bodies of initialization procedures can generate such nodes.
3863 -- We follow the parent chain until we reach a list node, which is
3864 -- the enclosing list of declarations. If the list appears within
3865 -- a protected definition, move freeze nodes outside the protected
3866 -- type altogether.
3868 if Present
3869 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
3870 then
3871 declare
3872 Decl : Node_Id;
3873 L : constant List_Id := Scope_Stack.Table
3874 (Scope_Stack.Last).Pending_Freeze_Actions;
3876 begin
3877 if Is_Itype (Current_Scope) then
3878 Decl := Associated_Node_For_Itype (Current_Scope);
3879 else
3880 Decl := Parent (Current_Scope);
3881 end if;
3883 Pop_Scope;
3885 while not (Is_List_Member (Decl))
3886 or else Nkind_In (Parent (Decl), N_Protected_Definition,
3887 N_Task_Definition)
3888 loop
3889 Decl := Parent (Decl);
3890 end loop;
3892 Insert_List_Before_And_Analyze (Decl, L);
3893 end;
3895 else
3896 Pop_Scope;
3897 end if;
3899 end End_Scope;
3901 ---------------------
3902 -- End_Use_Clauses --
3903 ---------------------
3905 procedure End_Use_Clauses (Clause : Node_Id) is
3906 U : Node_Id;
3908 begin
3909 -- Remove Use_Type clauses first, because they affect the
3910 -- visibility of operators in subsequent used packages.
3912 U := Clause;
3913 while Present (U) loop
3914 if Nkind (U) = N_Use_Type_Clause then
3915 End_Use_Type (U);
3916 end if;
3918 Next_Use_Clause (U);
3919 end loop;
3921 U := Clause;
3922 while Present (U) loop
3923 if Nkind (U) = N_Use_Package_Clause then
3924 End_Use_Package (U);
3925 end if;
3927 Next_Use_Clause (U);
3928 end loop;
3929 end End_Use_Clauses;
3931 ---------------------
3932 -- End_Use_Package --
3933 ---------------------
3935 procedure End_Use_Package (N : Node_Id) is
3936 Pack_Name : Node_Id;
3937 Pack : Entity_Id;
3938 Id : Entity_Id;
3939 Elmt : Elmt_Id;
3941 function Is_Primitive_Operator_In_Use
3942 (Op : Entity_Id;
3943 F : Entity_Id) return Boolean;
3944 -- Check whether Op is a primitive operator of a use-visible type
3946 ----------------------------------
3947 -- Is_Primitive_Operator_In_Use --
3948 ----------------------------------
3950 function Is_Primitive_Operator_In_Use
3951 (Op : Entity_Id;
3952 F : Entity_Id) return Boolean
3954 T : constant Entity_Id := Base_Type (Etype (F));
3955 begin
3956 return In_Use (T) and then Scope (T) = Scope (Op);
3957 end Is_Primitive_Operator_In_Use;
3959 -- Start of processing for End_Use_Package
3961 begin
3962 Pack_Name := First (Names (N));
3963 while Present (Pack_Name) loop
3965 -- Test that Pack_Name actually denotes a package before processing
3967 if Is_Entity_Name (Pack_Name)
3968 and then Ekind (Entity (Pack_Name)) = E_Package
3969 then
3970 Pack := Entity (Pack_Name);
3972 if In_Open_Scopes (Pack) then
3973 null;
3975 elsif not Redundant_Use (Pack_Name) then
3976 Set_In_Use (Pack, False);
3977 Set_Current_Use_Clause (Pack, Empty);
3979 Id := First_Entity (Pack);
3980 while Present (Id) loop
3982 -- Preserve use-visibility of operators that are primitive
3983 -- operators of a type that is use-visible through an active
3984 -- use_type clause.
3986 if Nkind (Id) = N_Defining_Operator_Symbol
3987 and then
3988 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
3989 or else
3990 (Present (Next_Formal (First_Formal (Id)))
3991 and then
3992 Is_Primitive_Operator_In_Use
3993 (Id, Next_Formal (First_Formal (Id)))))
3994 then
3995 null;
3996 else
3997 Set_Is_Potentially_Use_Visible (Id, False);
3998 end if;
4000 if Is_Private_Type (Id)
4001 and then Present (Full_View (Id))
4002 then
4003 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4004 end if;
4006 Next_Entity (Id);
4007 end loop;
4009 if Present (Renamed_Object (Pack)) then
4010 Set_In_Use (Renamed_Object (Pack), False);
4011 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
4012 end if;
4014 if Chars (Pack) = Name_System
4015 and then Scope (Pack) = Standard_Standard
4016 and then Present_System_Aux
4017 then
4018 Id := First_Entity (System_Aux_Id);
4019 while Present (Id) loop
4020 Set_Is_Potentially_Use_Visible (Id, False);
4022 if Is_Private_Type (Id)
4023 and then Present (Full_View (Id))
4024 then
4025 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4026 end if;
4028 Next_Entity (Id);
4029 end loop;
4031 Set_In_Use (System_Aux_Id, False);
4032 end if;
4034 else
4035 Set_Redundant_Use (Pack_Name, False);
4036 end if;
4037 end if;
4039 Next (Pack_Name);
4040 end loop;
4042 if Present (Hidden_By_Use_Clause (N)) then
4043 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
4044 while Present (Elmt) loop
4045 declare
4046 E : constant Entity_Id := Node (Elmt);
4048 begin
4049 -- Reset either Use_Visibility or Direct_Visibility, depending
4050 -- on how the entity was hidden by the use clause.
4052 if In_Use (Scope (E))
4053 and then Used_As_Generic_Actual (Scope (E))
4054 then
4055 Set_Is_Potentially_Use_Visible (Node (Elmt));
4056 else
4057 Set_Is_Immediately_Visible (Node (Elmt));
4058 end if;
4060 Next_Elmt (Elmt);
4061 end;
4062 end loop;
4064 Set_Hidden_By_Use_Clause (N, No_Elist);
4065 end if;
4066 end End_Use_Package;
4068 ------------------
4069 -- End_Use_Type --
4070 ------------------
4072 procedure End_Use_Type (N : Node_Id) is
4073 Elmt : Elmt_Id;
4074 Id : Entity_Id;
4075 T : Entity_Id;
4077 -- Start of processing for End_Use_Type
4079 begin
4080 Id := First (Subtype_Marks (N));
4081 while Present (Id) loop
4083 -- A call to Rtsfind may occur while analyzing a use_type clause,
4084 -- in which case the type marks are not resolved yet, and there is
4085 -- nothing to remove.
4087 if not Is_Entity_Name (Id) or else No (Entity (Id)) then
4088 goto Continue;
4089 end if;
4091 T := Entity (Id);
4093 if T = Any_Type or else From_With_Type (T) then
4094 null;
4096 -- Note that the use_type clause may mention a subtype of the type
4097 -- whose primitive operations have been made visible. Here as
4098 -- elsewhere, it is the base type that matters for visibility.
4100 elsif In_Open_Scopes (Scope (Base_Type (T))) then
4101 null;
4103 elsif not Redundant_Use (Id) then
4104 Set_In_Use (T, False);
4105 Set_In_Use (Base_Type (T), False);
4106 Set_Current_Use_Clause (T, Empty);
4107 Set_Current_Use_Clause (Base_Type (T), Empty);
4108 end if;
4110 <<Continue>>
4111 Next (Id);
4112 end loop;
4114 if Is_Empty_Elmt_List (Used_Operations (N)) then
4115 return;
4117 else
4118 Elmt := First_Elmt (Used_Operations (N));
4119 while Present (Elmt) loop
4120 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
4121 Next_Elmt (Elmt);
4122 end loop;
4123 end if;
4124 end End_Use_Type;
4126 ----------------------
4127 -- Find_Direct_Name --
4128 ----------------------
4130 procedure Find_Direct_Name (N : Node_Id) is
4131 E : Entity_Id;
4132 E2 : Entity_Id;
4133 Msg : Boolean;
4135 Inst : Entity_Id := Empty;
4136 -- Enclosing instance, if any
4138 Homonyms : Entity_Id;
4139 -- Saves start of homonym chain
4141 Nvis_Entity : Boolean;
4142 -- Set True to indicate that there is at least one entity on the homonym
4143 -- chain which, while not visible, is visible enough from the user point
4144 -- of view to warrant an error message of "not visible" rather than
4145 -- undefined.
4147 Nvis_Is_Private_Subprg : Boolean := False;
4148 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
4149 -- effect concerning library subprograms has been detected. Used to
4150 -- generate the precise error message.
4152 function From_Actual_Package (E : Entity_Id) return Boolean;
4153 -- Returns true if the entity is declared in a package that is
4154 -- an actual for a formal package of the current instance. Such an
4155 -- entity requires special handling because it may be use-visible
4156 -- but hides directly visible entities defined outside the instance.
4158 function Is_Actual_Parameter return Boolean;
4159 -- This function checks if the node N is an identifier that is an actual
4160 -- parameter of a procedure call. If so it returns True, otherwise it
4161 -- return False. The reason for this check is that at this stage we do
4162 -- not know what procedure is being called if the procedure might be
4163 -- overloaded, so it is premature to go setting referenced flags or
4164 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
4165 -- for that processing
4167 function Known_But_Invisible (E : Entity_Id) return Boolean;
4168 -- This function determines whether the entity E (which is not
4169 -- visible) can reasonably be considered to be known to the writer
4170 -- of the reference. This is a heuristic test, used only for the
4171 -- purposes of figuring out whether we prefer to complain that an
4172 -- entity is undefined or invisible (and identify the declaration
4173 -- of the invisible entity in the latter case). The point here is
4174 -- that we don't want to complain that something is invisible and
4175 -- then point to something entirely mysterious to the writer.
4177 procedure Nvis_Messages;
4178 -- Called if there are no visible entries for N, but there is at least
4179 -- one non-directly visible, or hidden declaration. This procedure
4180 -- outputs an appropriate set of error messages.
4182 procedure Undefined (Nvis : Boolean);
4183 -- This function is called if the current node has no corresponding
4184 -- visible entity or entities. The value set in Msg indicates whether
4185 -- an error message was generated (multiple error messages for the
4186 -- same variable are generally suppressed, see body for details).
4187 -- Msg is True if an error message was generated, False if not. This
4188 -- value is used by the caller to determine whether or not to output
4189 -- additional messages where appropriate. The parameter is set False
4190 -- to get the message "X is undefined", and True to get the message
4191 -- "X is not visible".
4193 -------------------------
4194 -- From_Actual_Package --
4195 -------------------------
4197 function From_Actual_Package (E : Entity_Id) return Boolean is
4198 Scop : constant Entity_Id := Scope (E);
4199 Act : Entity_Id;
4201 begin
4202 if not In_Instance then
4203 return False;
4204 else
4205 Inst := Current_Scope;
4206 while Present (Inst)
4207 and then Ekind (Inst) /= E_Package
4208 and then not Is_Generic_Instance (Inst)
4209 loop
4210 Inst := Scope (Inst);
4211 end loop;
4213 if No (Inst) then
4214 return False;
4215 end if;
4217 Act := First_Entity (Inst);
4218 while Present (Act) loop
4219 if Ekind (Act) = E_Package then
4221 -- Check for end of actuals list
4223 if Renamed_Object (Act) = Inst then
4224 return False;
4226 elsif Present (Associated_Formal_Package (Act))
4227 and then Renamed_Object (Act) = Scop
4228 then
4229 -- Entity comes from (instance of) formal package
4231 return True;
4233 else
4234 Next_Entity (Act);
4235 end if;
4237 else
4238 Next_Entity (Act);
4239 end if;
4240 end loop;
4242 return False;
4243 end if;
4244 end From_Actual_Package;
4246 -------------------------
4247 -- Is_Actual_Parameter --
4248 -------------------------
4250 function Is_Actual_Parameter return Boolean is
4251 begin
4252 return
4253 Nkind (N) = N_Identifier
4254 and then
4255 (Nkind (Parent (N)) = N_Procedure_Call_Statement
4256 or else
4257 (Nkind (Parent (N)) = N_Parameter_Association
4258 and then N = Explicit_Actual_Parameter (Parent (N))
4259 and then Nkind (Parent (Parent (N))) =
4260 N_Procedure_Call_Statement));
4261 end Is_Actual_Parameter;
4263 -------------------------
4264 -- Known_But_Invisible --
4265 -------------------------
4267 function Known_But_Invisible (E : Entity_Id) return Boolean is
4268 Fname : File_Name_Type;
4270 begin
4271 -- Entities in Standard are always considered to be known
4273 if Sloc (E) <= Standard_Location then
4274 return True;
4276 -- An entity that does not come from source is always considered
4277 -- to be unknown, since it is an artifact of code expansion.
4279 elsif not Comes_From_Source (E) then
4280 return False;
4282 -- In gnat internal mode, we consider all entities known
4284 elsif GNAT_Mode then
4285 return True;
4286 end if;
4288 -- Here we have an entity that is not from package Standard, and
4289 -- which comes from Source. See if it comes from an internal file.
4291 Fname := Unit_File_Name (Get_Source_Unit (E));
4293 -- Case of from internal file
4295 if Is_Internal_File_Name (Fname) then
4297 -- Private part entities in internal files are never considered
4298 -- to be known to the writer of normal application code.
4300 if Is_Hidden (E) then
4301 return False;
4302 end if;
4304 -- Entities from System packages other than System and
4305 -- System.Storage_Elements are not considered to be known.
4306 -- System.Auxxxx files are also considered known to the user.
4308 -- Should refine this at some point to generally distinguish
4309 -- between known and unknown internal files ???
4311 Get_Name_String (Fname);
4313 return
4314 Name_Len < 2
4315 or else
4316 Name_Buffer (1 .. 2) /= "s-"
4317 or else
4318 Name_Buffer (3 .. 8) = "stoele"
4319 or else
4320 Name_Buffer (3 .. 5) = "aux";
4322 -- If not an internal file, then entity is definitely known,
4323 -- even if it is in a private part (the message generated will
4324 -- note that it is in a private part)
4326 else
4327 return True;
4328 end if;
4329 end Known_But_Invisible;
4331 -------------------
4332 -- Nvis_Messages --
4333 -------------------
4335 procedure Nvis_Messages is
4336 Comp_Unit : Node_Id;
4337 Ent : Entity_Id;
4338 Found : Boolean := False;
4339 Hidden : Boolean := False;
4340 Item : Node_Id;
4342 begin
4343 -- Ada 2005 (AI-262): Generate a precise error concerning the
4344 -- Beaujolais effect that was previously detected
4346 if Nvis_Is_Private_Subprg then
4348 pragma Assert (Nkind (E2) = N_Defining_Identifier
4349 and then Ekind (E2) = E_Function
4350 and then Scope (E2) = Standard_Standard
4351 and then Has_Private_With (E2));
4353 -- Find the sloc corresponding to the private with'ed unit
4355 Comp_Unit := Cunit (Current_Sem_Unit);
4356 Error_Msg_Sloc := No_Location;
4358 Item := First (Context_Items (Comp_Unit));
4359 while Present (Item) loop
4360 if Nkind (Item) = N_With_Clause
4361 and then Private_Present (Item)
4362 and then Entity (Name (Item)) = E2
4363 then
4364 Error_Msg_Sloc := Sloc (Item);
4365 exit;
4366 end if;
4368 Next (Item);
4369 end loop;
4371 pragma Assert (Error_Msg_Sloc /= No_Location);
4373 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
4374 return;
4375 end if;
4377 Undefined (Nvis => True);
4379 if Msg then
4381 -- First loop does hidden declarations
4383 Ent := Homonyms;
4384 while Present (Ent) loop
4385 if Is_Potentially_Use_Visible (Ent) then
4386 if not Hidden then
4387 Error_Msg_N -- CODEFIX
4388 ("multiple use clauses cause hiding!", N);
4389 Hidden := True;
4390 end if;
4392 Error_Msg_Sloc := Sloc (Ent);
4393 Error_Msg_N -- CODEFIX
4394 ("hidden declaration#!", N);
4395 end if;
4397 Ent := Homonym (Ent);
4398 end loop;
4400 -- If we found hidden declarations, then that's enough, don't
4401 -- bother looking for non-visible declarations as well.
4403 if Hidden then
4404 return;
4405 end if;
4407 -- Second loop does non-directly visible declarations
4409 Ent := Homonyms;
4410 while Present (Ent) loop
4411 if not Is_Potentially_Use_Visible (Ent) then
4413 -- Do not bother the user with unknown entities
4415 if not Known_But_Invisible (Ent) then
4416 goto Continue;
4417 end if;
4419 Error_Msg_Sloc := Sloc (Ent);
4421 -- Output message noting that there is a non-visible
4422 -- declaration, distinguishing the private part case.
4424 if Is_Hidden (Ent) then
4425 Error_Msg_N ("non-visible (private) declaration#!", N);
4427 -- If the entity is declared in a generic package, it
4428 -- cannot be visible, so there is no point in adding it
4429 -- to the list of candidates if another homograph from a
4430 -- non-generic package has been seen.
4432 elsif Ekind (Scope (Ent)) = E_Generic_Package
4433 and then Found
4434 then
4435 null;
4437 else
4438 Error_Msg_N -- CODEFIX
4439 ("non-visible declaration#!", N);
4441 if Ekind (Scope (Ent)) /= E_Generic_Package then
4442 Found := True;
4443 end if;
4445 if Is_Compilation_Unit (Ent)
4446 and then
4447 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
4448 then
4449 Error_Msg_Qual_Level := 99;
4450 Error_Msg_NE -- CODEFIX
4451 ("\\missing `WITH &;`", N, Ent);
4452 Error_Msg_Qual_Level := 0;
4453 end if;
4455 if Ekind (Ent) = E_Discriminant
4456 and then Present (Corresponding_Discriminant (Ent))
4457 and then Scope (Corresponding_Discriminant (Ent)) =
4458 Etype (Scope (Ent))
4459 then
4460 Error_Msg_N
4461 ("inherited discriminant not allowed here" &
4462 " (RM 3.8 (12), 3.8.1 (6))!", N);
4463 end if;
4464 end if;
4466 -- Set entity and its containing package as referenced. We
4467 -- can't be sure of this, but this seems a better choice
4468 -- to avoid unused entity messages.
4470 if Comes_From_Source (Ent) then
4471 Set_Referenced (Ent);
4472 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
4473 end if;
4474 end if;
4476 <<Continue>>
4477 Ent := Homonym (Ent);
4478 end loop;
4479 end if;
4480 end Nvis_Messages;
4482 ---------------
4483 -- Undefined --
4484 ---------------
4486 procedure Undefined (Nvis : Boolean) is
4487 Emsg : Error_Msg_Id;
4489 begin
4490 -- We should never find an undefined internal name. If we do, then
4491 -- see if we have previous errors. If so, ignore on the grounds that
4492 -- it is probably a cascaded message (e.g. a block label from a badly
4493 -- formed block). If no previous errors, then we have a real internal
4494 -- error of some kind so raise an exception.
4496 if Is_Internal_Name (Chars (N)) then
4497 if Total_Errors_Detected /= 0 then
4498 return;
4499 else
4500 raise Program_Error;
4501 end if;
4502 end if;
4504 -- A very specialized error check, if the undefined variable is
4505 -- a case tag, and the case type is an enumeration type, check
4506 -- for a possible misspelling, and if so, modify the identifier
4508 -- Named aggregate should also be handled similarly ???
4510 if Nkind (N) = N_Identifier
4511 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
4512 then
4513 declare
4514 Case_Stm : constant Node_Id := Parent (Parent (N));
4515 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
4517 Lit : Node_Id;
4519 begin
4520 if Is_Enumeration_Type (Case_Typ)
4521 and then not Is_Standard_Character_Type (Case_Typ)
4522 then
4523 Lit := First_Literal (Case_Typ);
4524 Get_Name_String (Chars (Lit));
4526 if Chars (Lit) /= Chars (N)
4527 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit)) then
4528 Error_Msg_Node_2 := Lit;
4529 Error_Msg_N -- CODEFIX
4530 ("& is undefined, assume misspelling of &", N);
4531 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
4532 return;
4533 end if;
4535 Lit := Next_Literal (Lit);
4536 end if;
4537 end;
4538 end if;
4540 -- Normal processing
4542 Set_Entity (N, Any_Id);
4543 Set_Etype (N, Any_Type);
4545 -- We use the table Urefs to keep track of entities for which we
4546 -- have issued errors for undefined references. Multiple errors
4547 -- for a single name are normally suppressed, however we modify
4548 -- the error message to alert the programmer to this effect.
4550 for J in Urefs.First .. Urefs.Last loop
4551 if Chars (N) = Chars (Urefs.Table (J).Node) then
4552 if Urefs.Table (J).Err /= No_Error_Msg
4553 and then Sloc (N) /= Urefs.Table (J).Loc
4554 then
4555 Error_Msg_Node_1 := Urefs.Table (J).Node;
4557 if Urefs.Table (J).Nvis then
4558 Change_Error_Text (Urefs.Table (J).Err,
4559 "& is not visible (more references follow)");
4560 else
4561 Change_Error_Text (Urefs.Table (J).Err,
4562 "& is undefined (more references follow)");
4563 end if;
4565 Urefs.Table (J).Err := No_Error_Msg;
4566 end if;
4568 -- Although we will set Msg False, and thus suppress the
4569 -- message, we also set Error_Posted True, to avoid any
4570 -- cascaded messages resulting from the undefined reference.
4572 Msg := False;
4573 Set_Error_Posted (N, True);
4574 return;
4575 end if;
4576 end loop;
4578 -- If entry not found, this is first undefined occurrence
4580 if Nvis then
4581 Error_Msg_N ("& is not visible!", N);
4582 Emsg := Get_Msg_Id;
4584 else
4585 Error_Msg_N ("& is undefined!", N);
4586 Emsg := Get_Msg_Id;
4588 -- A very bizarre special check, if the undefined identifier
4589 -- is put or put_line, then add a special error message (since
4590 -- this is a very common error for beginners to make).
4592 if Nam_In (Chars (N), Name_Put, Name_Put_Line) then
4593 Error_Msg_N -- CODEFIX
4594 ("\\possible missing `WITH Ada.Text_'I'O; " &
4595 "USE Ada.Text_'I'O`!", N);
4597 -- Another special check if N is the prefix of a selected
4598 -- component which is a known unit, add message complaining
4599 -- about missing with for this unit.
4601 elsif Nkind (Parent (N)) = N_Selected_Component
4602 and then N = Prefix (Parent (N))
4603 and then Is_Known_Unit (Parent (N))
4604 then
4605 Error_Msg_Node_2 := Selector_Name (Parent (N));
4606 Error_Msg_N -- CODEFIX
4607 ("\\missing `WITH &.&;`", Prefix (Parent (N)));
4608 end if;
4610 -- Now check for possible misspellings
4612 declare
4613 E : Entity_Id;
4614 Ematch : Entity_Id := Empty;
4616 Last_Name_Id : constant Name_Id :=
4617 Name_Id (Nat (First_Name_Id) +
4618 Name_Entries_Count - 1);
4620 begin
4621 for Nam in First_Name_Id .. Last_Name_Id loop
4622 E := Get_Name_Entity_Id (Nam);
4624 if Present (E)
4625 and then (Is_Immediately_Visible (E)
4626 or else
4627 Is_Potentially_Use_Visible (E))
4628 then
4629 if Is_Bad_Spelling_Of (Chars (N), Nam) then
4630 Ematch := E;
4631 exit;
4632 end if;
4633 end if;
4634 end loop;
4636 if Present (Ematch) then
4637 Error_Msg_NE -- CODEFIX
4638 ("\possible misspelling of&", N, Ematch);
4639 end if;
4640 end;
4641 end if;
4643 -- Make entry in undefined references table unless the full errors
4644 -- switch is set, in which case by refraining from generating the
4645 -- table entry, we guarantee that we get an error message for every
4646 -- undefined reference.
4648 if not All_Errors_Mode then
4649 Urefs.Append (
4650 (Node => N,
4651 Err => Emsg,
4652 Nvis => Nvis,
4653 Loc => Sloc (N)));
4654 end if;
4656 Msg := True;
4657 end Undefined;
4659 -- Start of processing for Find_Direct_Name
4661 begin
4662 -- If the entity pointer is already set, this is an internal node, or
4663 -- a node that is analyzed more than once, after a tree modification.
4664 -- In such a case there is no resolution to perform, just set the type.
4666 if Present (Entity (N)) then
4667 if Is_Type (Entity (N)) then
4668 Set_Etype (N, Entity (N));
4670 else
4671 declare
4672 Entyp : constant Entity_Id := Etype (Entity (N));
4674 begin
4675 -- One special case here. If the Etype field is already set,
4676 -- and references the packed array type corresponding to the
4677 -- etype of the referenced entity, then leave it alone. This
4678 -- happens for trees generated from Exp_Pakd, where expressions
4679 -- can be deliberately "mis-typed" to the packed array type.
4681 if Is_Array_Type (Entyp)
4682 and then Is_Packed (Entyp)
4683 and then Present (Etype (N))
4684 and then Etype (N) = Packed_Array_Type (Entyp)
4685 then
4686 null;
4688 -- If not that special case, then just reset the Etype
4690 else
4691 Set_Etype (N, Etype (Entity (N)));
4692 end if;
4693 end;
4694 end if;
4696 return;
4697 end if;
4699 -- Here if Entity pointer was not set, we need full visibility analysis
4700 -- First we generate debugging output if the debug E flag is set.
4702 if Debug_Flag_E then
4703 Write_Str ("Looking for ");
4704 Write_Name (Chars (N));
4705 Write_Eol;
4706 end if;
4708 Homonyms := Current_Entity (N);
4709 Nvis_Entity := False;
4711 E := Homonyms;
4712 while Present (E) loop
4714 -- If entity is immediately visible or potentially use visible, then
4715 -- process the entity and we are done.
4717 if Is_Immediately_Visible (E) then
4718 goto Immediately_Visible_Entity;
4720 elsif Is_Potentially_Use_Visible (E) then
4721 goto Potentially_Use_Visible_Entity;
4723 -- Note if a known but invisible entity encountered
4725 elsif Known_But_Invisible (E) then
4726 Nvis_Entity := True;
4727 end if;
4729 -- Move to next entity in chain and continue search
4731 E := Homonym (E);
4732 end loop;
4734 -- If no entries on homonym chain that were potentially visible,
4735 -- and no entities reasonably considered as non-visible, then
4736 -- we have a plain undefined reference, with no additional
4737 -- explanation required!
4739 if not Nvis_Entity then
4740 Undefined (Nvis => False);
4742 -- Otherwise there is at least one entry on the homonym chain that
4743 -- is reasonably considered as being known and non-visible.
4745 else
4746 Nvis_Messages;
4747 end if;
4749 return;
4751 -- Processing for a potentially use visible entry found. We must search
4752 -- the rest of the homonym chain for two reasons. First, if there is a
4753 -- directly visible entry, then none of the potentially use-visible
4754 -- entities are directly visible (RM 8.4(10)). Second, we need to check
4755 -- for the case of multiple potentially use-visible entries hiding one
4756 -- another and as a result being non-directly visible (RM 8.4(11)).
4758 <<Potentially_Use_Visible_Entity>> declare
4759 Only_One_Visible : Boolean := True;
4760 All_Overloadable : Boolean := Is_Overloadable (E);
4762 begin
4763 E2 := Homonym (E);
4764 while Present (E2) loop
4765 if Is_Immediately_Visible (E2) then
4767 -- If the use-visible entity comes from the actual for a
4768 -- formal package, it hides a directly visible entity from
4769 -- outside the instance.
4771 if From_Actual_Package (E)
4772 and then Scope_Depth (E2) < Scope_Depth (Inst)
4773 then
4774 goto Found;
4775 else
4776 E := E2;
4777 goto Immediately_Visible_Entity;
4778 end if;
4780 elsif Is_Potentially_Use_Visible (E2) then
4781 Only_One_Visible := False;
4782 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
4784 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
4785 -- that can occur in private_with clauses. Example:
4787 -- with A;
4788 -- private with B; package A is
4789 -- package C is function B return Integer;
4790 -- use A; end A;
4791 -- V1 : Integer := B;
4792 -- private function B return Integer;
4793 -- V2 : Integer := B;
4794 -- end C;
4796 -- V1 resolves to A.B, but V2 resolves to library unit B
4798 elsif Ekind (E2) = E_Function
4799 and then Scope (E2) = Standard_Standard
4800 and then Has_Private_With (E2)
4801 then
4802 Only_One_Visible := False;
4803 All_Overloadable := False;
4804 Nvis_Is_Private_Subprg := True;
4805 exit;
4806 end if;
4808 E2 := Homonym (E2);
4809 end loop;
4811 -- On falling through this loop, we have checked that there are no
4812 -- immediately visible entities. Only_One_Visible is set if exactly
4813 -- one potentially use visible entity exists. All_Overloadable is
4814 -- set if all the potentially use visible entities are overloadable.
4815 -- The condition for legality is that either there is one potentially
4816 -- use visible entity, or if there is more than one, then all of them
4817 -- are overloadable.
4819 if Only_One_Visible or All_Overloadable then
4820 goto Found;
4822 -- If there is more than one potentially use-visible entity and at
4823 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
4824 -- Note that E points to the first such entity on the homonym list.
4825 -- Special case: if one of the entities is declared in an actual
4826 -- package, it was visible in the generic, and takes precedence over
4827 -- other entities that are potentially use-visible. Same if it is
4828 -- declared in a local instantiation of the current instance.
4830 else
4831 if In_Instance then
4833 -- Find current instance
4835 Inst := Current_Scope;
4836 while Present (Inst) and then Inst /= Standard_Standard loop
4837 if Is_Generic_Instance (Inst) then
4838 exit;
4839 end if;
4841 Inst := Scope (Inst);
4842 end loop;
4844 E2 := E;
4845 while Present (E2) loop
4846 if From_Actual_Package (E2)
4847 or else
4848 (Is_Generic_Instance (Scope (E2))
4849 and then Scope_Depth (Scope (E2)) > Scope_Depth (Inst))
4850 then
4851 E := E2;
4852 goto Found;
4853 end if;
4855 E2 := Homonym (E2);
4856 end loop;
4858 Nvis_Messages;
4859 return;
4861 elsif
4862 Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
4863 then
4864 -- A use-clause in the body of a system file creates conflict
4865 -- with some entity in a user scope, while rtsfind is active.
4866 -- Keep only the entity coming from another predefined unit.
4868 E2 := E;
4869 while Present (E2) loop
4870 if Is_Predefined_File_Name
4871 (Unit_File_Name (Get_Source_Unit (Sloc (E2))))
4872 then
4873 E := E2;
4874 goto Found;
4875 end if;
4877 E2 := Homonym (E2);
4878 end loop;
4880 -- Entity must exist because predefined unit is correct
4882 raise Program_Error;
4884 else
4885 Nvis_Messages;
4886 return;
4887 end if;
4888 end if;
4889 end;
4891 -- Come here with E set to the first immediately visible entity on
4892 -- the homonym chain. This is the one we want unless there is another
4893 -- immediately visible entity further on in the chain for an inner
4894 -- scope (RM 8.3(8)).
4896 <<Immediately_Visible_Entity>> declare
4897 Level : Int;
4898 Scop : Entity_Id;
4900 begin
4901 -- Find scope level of initial entity. When compiling through
4902 -- Rtsfind, the previous context is not completely invisible, and
4903 -- an outer entity may appear on the chain, whose scope is below
4904 -- the entry for Standard that delimits the current scope stack.
4905 -- Indicate that the level for this spurious entry is outside of
4906 -- the current scope stack.
4908 Level := Scope_Stack.Last;
4909 loop
4910 Scop := Scope_Stack.Table (Level).Entity;
4911 exit when Scop = Scope (E);
4912 Level := Level - 1;
4913 exit when Scop = Standard_Standard;
4914 end loop;
4916 -- Now search remainder of homonym chain for more inner entry
4917 -- If the entity is Standard itself, it has no scope, and we
4918 -- compare it with the stack entry directly.
4920 E2 := Homonym (E);
4921 while Present (E2) loop
4922 if Is_Immediately_Visible (E2) then
4924 -- If a generic package contains a local declaration that
4925 -- has the same name as the generic, there may be a visibility
4926 -- conflict in an instance, where the local declaration must
4927 -- also hide the name of the corresponding package renaming.
4928 -- We check explicitly for a package declared by a renaming,
4929 -- whose renamed entity is an instance that is on the scope
4930 -- stack, and that contains a homonym in the same scope. Once
4931 -- we have found it, we know that the package renaming is not
4932 -- immediately visible, and that the identifier denotes the
4933 -- other entity (and its homonyms if overloaded).
4935 if Scope (E) = Scope (E2)
4936 and then Ekind (E) = E_Package
4937 and then Present (Renamed_Object (E))
4938 and then Is_Generic_Instance (Renamed_Object (E))
4939 and then In_Open_Scopes (Renamed_Object (E))
4940 and then Comes_From_Source (N)
4941 then
4942 Set_Is_Immediately_Visible (E, False);
4943 E := E2;
4945 else
4946 for J in Level + 1 .. Scope_Stack.Last loop
4947 if Scope_Stack.Table (J).Entity = Scope (E2)
4948 or else Scope_Stack.Table (J).Entity = E2
4949 then
4950 Level := J;
4951 E := E2;
4952 exit;
4953 end if;
4954 end loop;
4955 end if;
4956 end if;
4958 E2 := Homonym (E2);
4959 end loop;
4961 -- At the end of that loop, E is the innermost immediately
4962 -- visible entity, so we are all set.
4963 end;
4965 -- Come here with entity found, and stored in E
4967 <<Found>> begin
4969 -- Check violation of No_Wide_Characters restriction
4971 Check_Wide_Character_Restriction (E, N);
4973 -- When distribution features are available (Get_PCS_Name /=
4974 -- Name_No_DSA), a remote access-to-subprogram type is converted
4975 -- into a record type holding whatever information is needed to
4976 -- perform a remote call on an RCI subprogram. In that case we
4977 -- rewrite any occurrence of the RAS type into the equivalent record
4978 -- type here. 'Access attribute references and RAS dereferences are
4979 -- then implemented using specific TSSs. However when distribution is
4980 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
4981 -- generation of these TSSs, and we must keep the RAS type in its
4982 -- original access-to-subprogram form (since all calls through a
4983 -- value of such type will be local anyway in the absence of a PCS).
4985 if Comes_From_Source (N)
4986 and then Is_Remote_Access_To_Subprogram_Type (E)
4987 and then Expander_Active
4988 and then Get_PCS_Name /= Name_No_DSA
4989 then
4990 Rewrite (N,
4991 New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
4992 return;
4993 end if;
4995 -- Set the entity. Note that the reason we call Set_Entity for the
4996 -- overloadable case, as opposed to Set_Entity_With_Style_Check is
4997 -- that in the overloaded case, the initial call can set the wrong
4998 -- homonym. The call that sets the right homonym is in Sem_Res and
4999 -- that call does use Set_Entity_With_Style_Check, so we don't miss
5000 -- a style check.
5002 if Is_Overloadable (E) then
5003 Set_Entity (N, E);
5004 else
5005 Set_Entity_With_Style_Check (N, E);
5006 end if;
5008 if Is_Type (E) then
5009 Set_Etype (N, E);
5010 else
5011 Set_Etype (N, Get_Full_View (Etype (E)));
5012 end if;
5014 if Debug_Flag_E then
5015 Write_Str (" found ");
5016 Write_Entity_Info (E, " ");
5017 end if;
5019 -- If the Ekind of the entity is Void, it means that all homonyms
5020 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
5021 -- test is skipped if the current scope is a record and the name is
5022 -- a pragma argument expression (case of Atomic and Volatile pragmas
5023 -- and possibly other similar pragmas added later, which are allowed
5024 -- to reference components in the current record).
5026 if Ekind (E) = E_Void
5027 and then
5028 (not Is_Record_Type (Current_Scope)
5029 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
5030 then
5031 Premature_Usage (N);
5033 -- If the entity is overloadable, collect all interpretations of the
5034 -- name for subsequent overload resolution. We optimize a bit here to
5035 -- do this only if we have an overloadable entity that is not on its
5036 -- own on the homonym chain.
5038 elsif Is_Overloadable (E)
5039 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
5040 then
5041 Collect_Interps (N);
5043 -- If no homonyms were visible, the entity is unambiguous
5045 if not Is_Overloaded (N) then
5046 if not Is_Actual_Parameter then
5047 Generate_Reference (E, N);
5048 end if;
5049 end if;
5051 -- Case of non-overloadable entity, set the entity providing that
5052 -- we do not have the case of a discriminant reference within a
5053 -- default expression. Such references are replaced with the
5054 -- corresponding discriminal, which is the formal corresponding to
5055 -- to the discriminant in the initialization procedure.
5057 else
5058 -- Entity is unambiguous, indicate that it is referenced here
5060 -- For a renaming of an object, always generate simple reference,
5061 -- we don't try to keep track of assignments in this case.
5063 if Is_Object (E) and then Present (Renamed_Object (E)) then
5064 Generate_Reference (E, N);
5066 -- If the renamed entity is a private protected component,
5067 -- reference the original component as well. This needs to be
5068 -- done because the private renamings are installed before any
5069 -- analysis has occurred. Reference to a private component will
5070 -- resolve to the renaming and the original component will be
5071 -- left unreferenced, hence the following.
5073 if Is_Prival (E) then
5074 Generate_Reference (Prival_Link (E), N);
5075 end if;
5077 -- One odd case is that we do not want to set the Referenced flag
5078 -- if the entity is a label, and the identifier is the label in
5079 -- the source, since this is not a reference from the point of
5080 -- view of the user.
5082 elsif Nkind (Parent (N)) = N_Label then
5083 declare
5084 R : constant Boolean := Referenced (E);
5086 begin
5087 -- Generate reference unless this is an actual parameter
5088 -- (see comment below)
5090 if Is_Actual_Parameter then
5091 Generate_Reference (E, N);
5092 Set_Referenced (E, R);
5093 end if;
5094 end;
5096 -- Normal case, not a label: generate reference
5098 -- ??? It is too early to generate a reference here even if the
5099 -- entity is unambiguous, because the tree is not sufficiently
5100 -- typed at this point for Generate_Reference to determine
5101 -- whether this reference modifies the denoted object (because
5102 -- implicit dereferences cannot be identified prior to full type
5103 -- resolution).
5105 -- The Is_Actual_Parameter routine takes care of one of these
5106 -- cases but there are others probably ???
5108 -- If the entity is the LHS of an assignment, and is a variable
5109 -- (rather than a package prefix), we can mark it as a
5110 -- modification right away, to avoid duplicate references.
5112 else
5113 if not Is_Actual_Parameter then
5114 if Is_LHS (N)
5115 and then Ekind (E) /= E_Package
5116 and then Ekind (E) /= E_Generic_Package
5117 then
5118 Generate_Reference (E, N, 'm');
5119 else
5120 Generate_Reference (E, N);
5121 end if;
5122 end if;
5124 Check_Nested_Access (E);
5125 end if;
5127 Set_Entity_Or_Discriminal (N, E);
5129 -- The name may designate a generalized reference, in which case
5130 -- the dereference interpretation will be included.
5132 if Ada_Version >= Ada_2012
5133 and then
5134 (Nkind (Parent (N)) in N_Subexpr
5135 or else Nkind_In (Parent (N), N_Object_Declaration,
5136 N_Assignment_Statement))
5137 then
5138 Check_Implicit_Dereference (N, Etype (E));
5139 end if;
5140 end if;
5141 end;
5142 end Find_Direct_Name;
5144 ------------------------
5145 -- Find_Expanded_Name --
5146 ------------------------
5148 -- This routine searches the homonym chain of the entity until it finds
5149 -- an entity declared in the scope denoted by the prefix. If the entity
5150 -- is private, it may nevertheless be immediately visible, if we are in
5151 -- the scope of its declaration.
5153 procedure Find_Expanded_Name (N : Node_Id) is
5154 Selector : constant Node_Id := Selector_Name (N);
5155 Candidate : Entity_Id := Empty;
5156 P_Name : Entity_Id;
5157 O_Name : Entity_Id;
5158 Id : Entity_Id;
5160 begin
5161 P_Name := Entity (Prefix (N));
5162 O_Name := P_Name;
5164 -- If the prefix is a renamed package, look for the entity in the
5165 -- original package.
5167 if Ekind (P_Name) = E_Package
5168 and then Present (Renamed_Object (P_Name))
5169 then
5170 P_Name := Renamed_Object (P_Name);
5172 -- Rewrite node with entity field pointing to renamed object
5174 Rewrite (Prefix (N), New_Copy (Prefix (N)));
5175 Set_Entity (Prefix (N), P_Name);
5177 -- If the prefix is an object of a concurrent type, look for
5178 -- the entity in the associated task or protected type.
5180 elsif Is_Concurrent_Type (Etype (P_Name)) then
5181 P_Name := Etype (P_Name);
5182 end if;
5184 Id := Current_Entity (Selector);
5186 declare
5187 Is_New_Candidate : Boolean;
5189 begin
5190 while Present (Id) loop
5191 if Scope (Id) = P_Name then
5192 Candidate := Id;
5193 Is_New_Candidate := True;
5195 -- Ada 2005 (AI-217): Handle shadow entities associated with types
5196 -- declared in limited-withed nested packages. We don't need to
5197 -- handle E_Incomplete_Subtype entities because the entities in
5198 -- the limited view are always E_Incomplete_Type entities (see
5199 -- Build_Limited_Views). Regarding the expression used to evaluate
5200 -- the scope, it is important to note that the limited view also
5201 -- has shadow entities associated nested packages. For this reason
5202 -- the correct scope of the entity is the scope of the real entity
5203 -- The non-limited view may itself be incomplete, in which case
5204 -- get the full view if available.
5206 elsif From_With_Type (Id)
5207 and then Is_Type (Id)
5208 and then Ekind (Id) = E_Incomplete_Type
5209 and then Present (Non_Limited_View (Id))
5210 and then Scope (Non_Limited_View (Id)) = P_Name
5211 then
5212 Candidate := Get_Full_View (Non_Limited_View (Id));
5213 Is_New_Candidate := True;
5215 else
5216 Is_New_Candidate := False;
5217 end if;
5219 if Is_New_Candidate then
5220 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
5221 exit when Is_Visible_Lib_Unit (Id);
5222 else
5223 exit when not Is_Hidden (Id);
5224 end if;
5226 exit when Is_Immediately_Visible (Id);
5227 end if;
5229 Id := Homonym (Id);
5230 end loop;
5231 end;
5233 if No (Id)
5234 and then Ekind_In (P_Name, E_Procedure, E_Function)
5235 and then Is_Generic_Instance (P_Name)
5236 then
5237 -- Expanded name denotes entity in (instance of) generic subprogram.
5238 -- The entity may be in the subprogram instance, or may denote one of
5239 -- the formals, which is declared in the enclosing wrapper package.
5241 P_Name := Scope (P_Name);
5243 Id := Current_Entity (Selector);
5244 while Present (Id) loop
5245 exit when Scope (Id) = P_Name;
5246 Id := Homonym (Id);
5247 end loop;
5248 end if;
5250 if No (Id) or else Chars (Id) /= Chars (Selector) then
5251 Set_Etype (N, Any_Type);
5253 -- If we are looking for an entity defined in System, try to find it
5254 -- in the child package that may have been provided as an extension
5255 -- to System. The Extend_System pragma will have supplied the name of
5256 -- the extension, which may have to be loaded.
5258 if Chars (P_Name) = Name_System
5259 and then Scope (P_Name) = Standard_Standard
5260 and then Present (System_Extend_Unit)
5261 and then Present_System_Aux (N)
5262 then
5263 Set_Entity (Prefix (N), System_Aux_Id);
5264 Find_Expanded_Name (N);
5265 return;
5267 elsif Nkind (Selector) = N_Operator_Symbol
5268 and then Has_Implicit_Operator (N)
5269 then
5270 -- There is an implicit instance of the predefined operator in
5271 -- the given scope. The operator entity is defined in Standard.
5272 -- Has_Implicit_Operator makes the node into an Expanded_Name.
5274 return;
5276 elsif Nkind (Selector) = N_Character_Literal
5277 and then Has_Implicit_Character_Literal (N)
5278 then
5279 -- If there is no literal defined in the scope denoted by the
5280 -- prefix, the literal may belong to (a type derived from)
5281 -- Standard_Character, for which we have no explicit literals.
5283 return;
5285 else
5286 -- If the prefix is a single concurrent object, use its name in
5287 -- the error message, rather than that of the anonymous type.
5289 if Is_Concurrent_Type (P_Name)
5290 and then Is_Internal_Name (Chars (P_Name))
5291 then
5292 Error_Msg_Node_2 := Entity (Prefix (N));
5293 else
5294 Error_Msg_Node_2 := P_Name;
5295 end if;
5297 if P_Name = System_Aux_Id then
5298 P_Name := Scope (P_Name);
5299 Set_Entity (Prefix (N), P_Name);
5300 end if;
5302 if Present (Candidate) then
5304 -- If we know that the unit is a child unit we can give a more
5305 -- accurate error message.
5307 if Is_Child_Unit (Candidate) then
5309 -- If the candidate is a private child unit and we are in
5310 -- the visible part of a public unit, specialize the error
5311 -- message. There might be a private with_clause for it,
5312 -- but it is not currently active.
5314 if Is_Private_Descendant (Candidate)
5315 and then Ekind (Current_Scope) = E_Package
5316 and then not In_Private_Part (Current_Scope)
5317 and then not Is_Private_Descendant (Current_Scope)
5318 then
5319 Error_Msg_N ("private child unit& is not visible here",
5320 Selector);
5322 -- Normal case where we have a missing with for a child unit
5324 else
5325 Error_Msg_Qual_Level := 99;
5326 Error_Msg_NE -- CODEFIX
5327 ("missing `WITH &;`", Selector, Candidate);
5328 Error_Msg_Qual_Level := 0;
5329 end if;
5331 -- Here we don't know that this is a child unit
5333 else
5334 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
5335 end if;
5337 else
5338 -- Within the instantiation of a child unit, the prefix may
5339 -- denote the parent instance, but the selector has the name
5340 -- of the original child. Find whether we are within the
5341 -- corresponding instance, and get the proper entity, which
5342 -- can only be an enclosing scope.
5344 if O_Name /= P_Name
5345 and then In_Open_Scopes (P_Name)
5346 and then Is_Generic_Instance (P_Name)
5347 then
5348 declare
5349 S : Entity_Id := Current_Scope;
5350 P : Entity_Id;
5352 begin
5353 for J in reverse 0 .. Scope_Stack.Last loop
5354 S := Scope_Stack.Table (J).Entity;
5356 exit when S = Standard_Standard;
5358 if Ekind_In (S, E_Function,
5359 E_Package,
5360 E_Procedure)
5361 then
5362 P := Generic_Parent (Specification
5363 (Unit_Declaration_Node (S)));
5365 if Present (P)
5366 and then Chars (Scope (P)) = Chars (O_Name)
5367 and then Chars (P) = Chars (Selector)
5368 then
5369 Id := S;
5370 goto Found;
5371 end if;
5372 end if;
5374 end loop;
5375 end;
5376 end if;
5378 -- If this is a selection from Ada, System or Interfaces, then
5379 -- we assume a missing with for the corresponding package.
5381 if Is_Known_Unit (N) then
5382 if not Error_Posted (N) then
5383 Error_Msg_Node_2 := Selector;
5384 Error_Msg_N -- CODEFIX
5385 ("missing `WITH &.&;`", Prefix (N));
5386 end if;
5388 -- If this is a selection from a dummy package, then suppress
5389 -- the error message, of course the entity is missing if the
5390 -- package is missing!
5392 elsif Sloc (Error_Msg_Node_2) = No_Location then
5393 null;
5395 -- Here we have the case of an undefined component
5397 else
5399 -- The prefix may hide a homonym in the context that
5400 -- declares the desired entity. This error can use a
5401 -- specialized message.
5403 if In_Open_Scopes (P_Name) then
5404 declare
5405 H : constant Entity_Id := Homonym (P_Name);
5407 begin
5408 if Present (H)
5409 and then Is_Compilation_Unit (H)
5410 and then
5411 (Is_Immediately_Visible (H)
5412 or else Is_Visible_Lib_Unit (H))
5413 then
5414 Id := First_Entity (H);
5415 while Present (Id) loop
5416 if Chars (Id) = Chars (Selector) then
5417 Error_Msg_Qual_Level := 99;
5418 Error_Msg_Name_1 := Chars (Selector);
5419 Error_Msg_NE
5420 ("% not declared in&", N, P_Name);
5421 Error_Msg_NE
5422 ("\use fully qualified name starting with "
5423 & "Standard to make& visible", N, H);
5424 Error_Msg_Qual_Level := 0;
5425 goto Done;
5426 end if;
5428 Next_Entity (Id);
5429 end loop;
5430 end if;
5432 -- If not found, standard error message
5434 Error_Msg_NE ("& not declared in&", N, Selector);
5436 <<Done>> null;
5437 end;
5439 else
5440 Error_Msg_NE ("& not declared in&", N, Selector);
5441 end if;
5443 -- Check for misspelling of some entity in prefix
5445 Id := First_Entity (P_Name);
5446 while Present (Id) loop
5447 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
5448 and then not Is_Internal_Name (Chars (Id))
5449 then
5450 Error_Msg_NE -- CODEFIX
5451 ("possible misspelling of&", Selector, Id);
5452 exit;
5453 end if;
5455 Next_Entity (Id);
5456 end loop;
5458 -- Specialize the message if this may be an instantiation
5459 -- of a child unit that was not mentioned in the context.
5461 if Nkind (Parent (N)) = N_Package_Instantiation
5462 and then Is_Generic_Instance (Entity (Prefix (N)))
5463 and then Is_Compilation_Unit
5464 (Generic_Parent (Parent (Entity (Prefix (N)))))
5465 then
5466 Error_Msg_Node_2 := Selector;
5467 Error_Msg_N -- CODEFIX
5468 ("\missing `WITH &.&;`", Prefix (N));
5469 end if;
5470 end if;
5471 end if;
5473 Id := Any_Id;
5474 end if;
5475 end if;
5477 <<Found>>
5478 if Comes_From_Source (N)
5479 and then Is_Remote_Access_To_Subprogram_Type (Id)
5480 and then Present (Equivalent_Type (Id))
5481 then
5482 -- If we are not actually generating distribution code (i.e. the
5483 -- current PCS is the dummy non-distributed version), then the
5484 -- Equivalent_Type will be missing, and Id should be treated as
5485 -- a regular access-to-subprogram type.
5487 Id := Equivalent_Type (Id);
5488 Set_Chars (Selector, Chars (Id));
5489 end if;
5491 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
5493 if Ekind (P_Name) = E_Package and then From_With_Type (P_Name) then
5494 if From_With_Type (Id)
5495 or else Is_Type (Id)
5496 or else Ekind (Id) = E_Package
5497 then
5498 null;
5499 else
5500 Error_Msg_N
5501 ("limited withed package can only be used to access "
5502 & "incomplete types",
5504 end if;
5505 end if;
5507 if Is_Task_Type (P_Name)
5508 and then ((Ekind (Id) = E_Entry
5509 and then Nkind (Parent (N)) /= N_Attribute_Reference)
5510 or else
5511 (Ekind (Id) = E_Entry_Family
5512 and then
5513 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
5514 then
5515 -- If both the task type and the entry are in scope, this may still
5516 -- be the expanded name of an entry formal.
5518 if In_Open_Scopes (Id)
5519 and then Nkind (Parent (N)) = N_Selected_Component
5520 then
5521 null;
5523 else
5524 -- It is an entry call after all, either to the current task
5525 -- (which will deadlock) or to an enclosing task.
5527 Analyze_Selected_Component (N);
5528 return;
5529 end if;
5530 end if;
5532 Change_Selected_Component_To_Expanded_Name (N);
5534 -- Do style check and generate reference, but skip both steps if this
5535 -- entity has homonyms, since we may not have the right homonym set yet.
5536 -- The proper homonym will be set during the resolve phase.
5538 if Has_Homonym (Id) then
5539 Set_Entity (N, Id);
5540 else
5541 Set_Entity_Or_Discriminal (N, Id);
5543 if Is_LHS (N) then
5544 Generate_Reference (Id, N, 'm');
5545 else
5546 Generate_Reference (Id, N);
5547 end if;
5548 end if;
5550 if Is_Type (Id) then
5551 Set_Etype (N, Id);
5552 else
5553 Set_Etype (N, Get_Full_View (Etype (Id)));
5554 end if;
5556 -- Check for violation of No_Wide_Characters
5558 Check_Wide_Character_Restriction (Id, N);
5560 -- If the Ekind of the entity is Void, it means that all homonyms are
5561 -- hidden from all visibility (RM 8.3(5,14-20)).
5563 if Ekind (Id) = E_Void then
5564 Premature_Usage (N);
5566 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
5567 declare
5568 H : Entity_Id := Homonym (Id);
5570 begin
5571 while Present (H) loop
5572 if Scope (H) = Scope (Id)
5573 and then (not Is_Hidden (H)
5574 or else Is_Immediately_Visible (H))
5575 then
5576 Collect_Interps (N);
5577 exit;
5578 end if;
5580 H := Homonym (H);
5581 end loop;
5583 -- If an extension of System is present, collect possible explicit
5584 -- overloadings declared in the extension.
5586 if Chars (P_Name) = Name_System
5587 and then Scope (P_Name) = Standard_Standard
5588 and then Present (System_Extend_Unit)
5589 and then Present_System_Aux (N)
5590 then
5591 H := Current_Entity (Id);
5593 while Present (H) loop
5594 if Scope (H) = System_Aux_Id then
5595 Add_One_Interp (N, H, Etype (H));
5596 end if;
5598 H := Homonym (H);
5599 end loop;
5600 end if;
5601 end;
5602 end if;
5604 if Nkind (Selector_Name (N)) = N_Operator_Symbol
5605 and then Scope (Id) /= Standard_Standard
5606 then
5607 -- In addition to user-defined operators in the given scope, there
5608 -- may be an implicit instance of the predefined operator. The
5609 -- operator (defined in Standard) is found in Has_Implicit_Operator,
5610 -- and added to the interpretations. Procedure Add_One_Interp will
5611 -- determine which hides which.
5613 if Has_Implicit_Operator (N) then
5614 null;
5615 end if;
5616 end if;
5618 -- If there is a single interpretation for N we can generate a
5619 -- reference to the unique entity found.
5621 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
5622 Generate_Reference (Id, N);
5623 end if;
5624 end Find_Expanded_Name;
5626 -------------------------
5627 -- Find_Renamed_Entity --
5628 -------------------------
5630 function Find_Renamed_Entity
5631 (N : Node_Id;
5632 Nam : Node_Id;
5633 New_S : Entity_Id;
5634 Is_Actual : Boolean := False) return Entity_Id
5636 Ind : Interp_Index;
5637 I1 : Interp_Index := 0; -- Suppress junk warnings
5638 It : Interp;
5639 It1 : Interp;
5640 Old_S : Entity_Id;
5641 Inst : Entity_Id;
5643 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
5644 -- If the renamed entity is an implicit operator, check whether it is
5645 -- visible because its operand type is properly visible. This check
5646 -- applies to explicit renamed entities that appear in the source in a
5647 -- renaming declaration or a formal subprogram instance, but not to
5648 -- default generic actuals with a name.
5650 function Report_Overload return Entity_Id;
5651 -- List possible interpretations, and specialize message in the
5652 -- case of a generic actual.
5654 function Within (Inner, Outer : Entity_Id) return Boolean;
5655 -- Determine whether a candidate subprogram is defined within the
5656 -- enclosing instance. If yes, it has precedence over outer candidates.
5658 --------------------------
5659 -- Is_Visible_Operation --
5660 --------------------------
5662 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
5663 Scop : Entity_Id;
5664 Typ : Entity_Id;
5665 Btyp : Entity_Id;
5667 begin
5668 if Ekind (Op) /= E_Operator
5669 or else Scope (Op) /= Standard_Standard
5670 or else (In_Instance
5671 and then (not Is_Actual
5672 or else Present (Enclosing_Instance)))
5673 then
5674 return True;
5676 else
5677 -- For a fixed point type operator, check the resulting type,
5678 -- because it may be a mixed mode integer * fixed operation.
5680 if Present (Next_Formal (First_Formal (New_S)))
5681 and then Is_Fixed_Point_Type (Etype (New_S))
5682 then
5683 Typ := Etype (New_S);
5684 else
5685 Typ := Etype (First_Formal (New_S));
5686 end if;
5688 Btyp := Base_Type (Typ);
5690 if Nkind (Nam) /= N_Expanded_Name then
5691 return (In_Open_Scopes (Scope (Btyp))
5692 or else Is_Potentially_Use_Visible (Btyp)
5693 or else In_Use (Btyp)
5694 or else In_Use (Scope (Btyp)));
5696 else
5697 Scop := Entity (Prefix (Nam));
5699 if Ekind (Scop) = E_Package
5700 and then Present (Renamed_Object (Scop))
5701 then
5702 Scop := Renamed_Object (Scop);
5703 end if;
5705 -- Operator is visible if prefix of expanded name denotes
5706 -- scope of type, or else type is defined in System_Aux
5707 -- and the prefix denotes System.
5709 return Scope (Btyp) = Scop
5710 or else (Scope (Btyp) = System_Aux_Id
5711 and then Scope (Scope (Btyp)) = Scop);
5712 end if;
5713 end if;
5714 end Is_Visible_Operation;
5716 ------------
5717 -- Within --
5718 ------------
5720 function Within (Inner, Outer : Entity_Id) return Boolean is
5721 Sc : Entity_Id;
5723 begin
5724 Sc := Scope (Inner);
5725 while Sc /= Standard_Standard loop
5726 if Sc = Outer then
5727 return True;
5728 else
5729 Sc := Scope (Sc);
5730 end if;
5731 end loop;
5733 return False;
5734 end Within;
5736 ---------------------
5737 -- Report_Overload --
5738 ---------------------
5740 function Report_Overload return Entity_Id is
5741 begin
5742 if Is_Actual then
5743 Error_Msg_NE -- CODEFIX
5744 ("ambiguous actual subprogram&, " &
5745 "possible interpretations:", N, Nam);
5746 else
5747 Error_Msg_N -- CODEFIX
5748 ("ambiguous subprogram, " &
5749 "possible interpretations:", N);
5750 end if;
5752 List_Interps (Nam, N);
5753 return Old_S;
5754 end Report_Overload;
5756 -- Start of processing for Find_Renamed_Entity
5758 begin
5759 Old_S := Any_Id;
5760 Candidate_Renaming := Empty;
5762 if not Is_Overloaded (Nam) then
5763 if Is_Actual and then Present (Enclosing_Instance) then
5764 Old_S := Entity (Nam);
5766 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
5767 Candidate_Renaming := New_S;
5769 if Is_Visible_Operation (Entity (Nam)) then
5770 Old_S := Entity (Nam);
5771 end if;
5773 elsif
5774 Present (First_Formal (Entity (Nam)))
5775 and then Present (First_Formal (New_S))
5776 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
5777 Base_Type (Etype (First_Formal (New_S))))
5778 then
5779 Candidate_Renaming := Entity (Nam);
5780 end if;
5782 else
5783 Get_First_Interp (Nam, Ind, It);
5784 while Present (It.Nam) loop
5785 if Entity_Matches_Spec (It.Nam, New_S)
5786 and then Is_Visible_Operation (It.Nam)
5787 then
5788 if Old_S /= Any_Id then
5790 -- Note: The call to Disambiguate only happens if a
5791 -- previous interpretation was found, in which case I1
5792 -- has received a value.
5794 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
5796 if It1 = No_Interp then
5797 Inst := Enclosing_Instance;
5799 if Present (Inst) then
5800 if Within (It.Nam, Inst) then
5801 if Within (Old_S, Inst) then
5803 -- Choose the innermost subprogram, which would
5804 -- have hidden the outer one in the generic.
5806 if Scope_Depth (It.Nam) <
5807 Scope_Depth (Old_S)
5808 then
5809 return Old_S;
5810 else
5811 return It.Nam;
5812 end if;
5813 end if;
5815 elsif Within (Old_S, Inst) then
5816 return (Old_S);
5818 else
5819 return Report_Overload;
5820 end if;
5822 -- If not within an instance, ambiguity is real
5824 else
5825 return Report_Overload;
5826 end if;
5828 else
5829 Old_S := It1.Nam;
5830 exit;
5831 end if;
5833 else
5834 I1 := Ind;
5835 Old_S := It.Nam;
5836 end if;
5838 elsif
5839 Present (First_Formal (It.Nam))
5840 and then Present (First_Formal (New_S))
5841 and then (Base_Type (Etype (First_Formal (It.Nam))) =
5842 Base_Type (Etype (First_Formal (New_S))))
5843 then
5844 Candidate_Renaming := It.Nam;
5845 end if;
5847 Get_Next_Interp (Ind, It);
5848 end loop;
5850 Set_Entity (Nam, Old_S);
5852 if Old_S /= Any_Id then
5853 Set_Is_Overloaded (Nam, False);
5854 end if;
5855 end if;
5857 return Old_S;
5858 end Find_Renamed_Entity;
5860 -----------------------------
5861 -- Find_Selected_Component --
5862 -----------------------------
5864 procedure Find_Selected_Component (N : Node_Id) is
5865 P : constant Node_Id := Prefix (N);
5867 P_Name : Entity_Id;
5868 -- Entity denoted by prefix
5870 P_Type : Entity_Id;
5871 -- and its type
5873 Nam : Node_Id;
5875 begin
5876 Analyze (P);
5878 if Nkind (P) = N_Error then
5879 return;
5880 end if;
5882 -- Selector name cannot be a character literal or an operator symbol in
5883 -- SPARK, except for the operator symbol in a renaming.
5885 if Restriction_Check_Required (SPARK_05) then
5886 if Nkind (Selector_Name (N)) = N_Character_Literal then
5887 Check_SPARK_Restriction
5888 ("character literal cannot be prefixed", N);
5889 elsif Nkind (Selector_Name (N)) = N_Operator_Symbol
5890 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
5891 then
5892 Check_SPARK_Restriction ("operator symbol cannot be prefixed", N);
5893 end if;
5894 end if;
5896 -- If the selector already has an entity, the node has been constructed
5897 -- in the course of expansion, and is known to be valid. Do not verify
5898 -- that it is defined for the type (it may be a private component used
5899 -- in the expansion of record equality).
5901 if Present (Entity (Selector_Name (N))) then
5902 if No (Etype (N))
5903 or else Etype (N) = Any_Type
5904 then
5905 declare
5906 Sel_Name : constant Node_Id := Selector_Name (N);
5907 Selector : constant Entity_Id := Entity (Sel_Name);
5908 C_Etype : Node_Id;
5910 begin
5911 Set_Etype (Sel_Name, Etype (Selector));
5913 if not Is_Entity_Name (P) then
5914 Resolve (P);
5915 end if;
5917 -- Build an actual subtype except for the first parameter
5918 -- of an init proc, where this actual subtype is by
5919 -- definition incorrect, since the object is uninitialized
5920 -- (and does not even have defined discriminants etc.)
5922 if Is_Entity_Name (P)
5923 and then Ekind (Entity (P)) = E_Function
5924 then
5925 Nam := New_Copy (P);
5927 if Is_Overloaded (P) then
5928 Save_Interps (P, Nam);
5929 end if;
5931 Rewrite (P,
5932 Make_Function_Call (Sloc (P), Name => Nam));
5933 Analyze_Call (P);
5934 Analyze_Selected_Component (N);
5935 return;
5937 elsif Ekind (Selector) = E_Component
5938 and then (not Is_Entity_Name (P)
5939 or else Chars (Entity (P)) /= Name_uInit)
5940 then
5941 -- Do not build the subtype when referencing components of
5942 -- dispatch table wrappers. Required to avoid generating
5943 -- elaboration code with HI runtimes. JVM and .NET use a
5944 -- modified version of Ada.Tags which does not contain RE_
5945 -- Dispatch_Table_Wrapper and RE_No_Dispatch_Table_Wrapper.
5946 -- Avoid raising RE_Not_Available exception in those cases.
5948 if VM_Target = No_VM
5949 and then RTU_Loaded (Ada_Tags)
5950 and then
5951 ((RTE_Available (RE_Dispatch_Table_Wrapper)
5952 and then Scope (Selector) =
5953 RTE (RE_Dispatch_Table_Wrapper))
5954 or else
5955 (RTE_Available (RE_No_Dispatch_Table_Wrapper)
5956 and then Scope (Selector) =
5957 RTE (RE_No_Dispatch_Table_Wrapper)))
5958 then
5959 C_Etype := Empty;
5961 else
5962 C_Etype :=
5963 Build_Actual_Subtype_Of_Component
5964 (Etype (Selector), N);
5965 end if;
5967 else
5968 C_Etype := Empty;
5969 end if;
5971 if No (C_Etype) then
5972 C_Etype := Etype (Selector);
5973 else
5974 Insert_Action (N, C_Etype);
5975 C_Etype := Defining_Identifier (C_Etype);
5976 end if;
5978 Set_Etype (N, C_Etype);
5979 end;
5981 -- If this is the name of an entry or protected operation, and
5982 -- the prefix is an access type, insert an explicit dereference,
5983 -- so that entry calls are treated uniformly.
5985 if Is_Access_Type (Etype (P))
5986 and then Is_Concurrent_Type (Designated_Type (Etype (P)))
5987 then
5988 declare
5989 New_P : constant Node_Id :=
5990 Make_Explicit_Dereference (Sloc (P),
5991 Prefix => Relocate_Node (P));
5992 begin
5993 Rewrite (P, New_P);
5994 Set_Etype (P, Designated_Type (Etype (Prefix (P))));
5995 end;
5996 end if;
5998 -- If the selected component appears within a default expression
5999 -- and it has an actual subtype, the pre-analysis has not yet
6000 -- completed its analysis, because Insert_Actions is disabled in
6001 -- that context. Within the init proc of the enclosing type we
6002 -- must complete this analysis, if an actual subtype was created.
6004 elsif Inside_Init_Proc then
6005 declare
6006 Typ : constant Entity_Id := Etype (N);
6007 Decl : constant Node_Id := Declaration_Node (Typ);
6008 begin
6009 if Nkind (Decl) = N_Subtype_Declaration
6010 and then not Analyzed (Decl)
6011 and then Is_List_Member (Decl)
6012 and then No (Parent (Decl))
6013 then
6014 Remove (Decl);
6015 Insert_Action (N, Decl);
6016 end if;
6017 end;
6018 end if;
6020 return;
6022 elsif Is_Entity_Name (P) then
6023 P_Name := Entity (P);
6025 -- The prefix may denote an enclosing type which is the completion
6026 -- of an incomplete type declaration.
6028 if Is_Type (P_Name) then
6029 Set_Entity (P, Get_Full_View (P_Name));
6030 Set_Etype (P, Entity (P));
6031 P_Name := Entity (P);
6032 end if;
6034 P_Type := Base_Type (Etype (P));
6036 if Debug_Flag_E then
6037 Write_Str ("Found prefix type to be ");
6038 Write_Entity_Info (P_Type, " "); Write_Eol;
6039 end if;
6041 -- First check for components of a record object (not the
6042 -- result of a call, which is handled below).
6044 if Is_Appropriate_For_Record (P_Type)
6045 and then not Is_Overloadable (P_Name)
6046 and then not Is_Type (P_Name)
6047 then
6048 -- Selected component of record. Type checking will validate
6049 -- name of selector.
6051 -- ??? Could we rewrite an implicit dereference into an explicit
6052 -- one here?
6054 Analyze_Selected_Component (N);
6056 -- Reference to type name in predicate/invariant expression
6058 elsif Is_Appropriate_For_Entry_Prefix (P_Type)
6059 and then not In_Open_Scopes (P_Name)
6060 and then (not Is_Concurrent_Type (Etype (P_Name))
6061 or else not In_Open_Scopes (Etype (P_Name)))
6062 then
6063 -- Call to protected operation or entry. Type checking is
6064 -- needed on the prefix.
6066 Analyze_Selected_Component (N);
6068 elsif (In_Open_Scopes (P_Name)
6069 and then Ekind (P_Name) /= E_Void
6070 and then not Is_Overloadable (P_Name))
6071 or else (Is_Concurrent_Type (Etype (P_Name))
6072 and then In_Open_Scopes (Etype (P_Name)))
6073 then
6074 -- Prefix denotes an enclosing loop, block, or task, i.e. an
6075 -- enclosing construct that is not a subprogram or accept.
6077 Find_Expanded_Name (N);
6079 elsif Ekind (P_Name) = E_Package then
6080 Find_Expanded_Name (N);
6082 elsif Is_Overloadable (P_Name) then
6084 -- The subprogram may be a renaming (of an enclosing scope) as
6085 -- in the case of the name of the generic within an instantiation.
6087 if Ekind_In (P_Name, E_Procedure, E_Function)
6088 and then Present (Alias (P_Name))
6089 and then Is_Generic_Instance (Alias (P_Name))
6090 then
6091 P_Name := Alias (P_Name);
6092 end if;
6094 if Is_Overloaded (P) then
6096 -- The prefix must resolve to a unique enclosing construct
6098 declare
6099 Found : Boolean := False;
6100 Ind : Interp_Index;
6101 It : Interp;
6103 begin
6104 Get_First_Interp (P, Ind, It);
6105 while Present (It.Nam) loop
6106 if In_Open_Scopes (It.Nam) then
6107 if Found then
6108 Error_Msg_N (
6109 "prefix must be unique enclosing scope", N);
6110 Set_Entity (N, Any_Id);
6111 Set_Etype (N, Any_Type);
6112 return;
6114 else
6115 Found := True;
6116 P_Name := It.Nam;
6117 end if;
6118 end if;
6120 Get_Next_Interp (Ind, It);
6121 end loop;
6122 end;
6123 end if;
6125 if In_Open_Scopes (P_Name) then
6126 Set_Entity (P, P_Name);
6127 Set_Is_Overloaded (P, False);
6128 Find_Expanded_Name (N);
6130 else
6131 -- If no interpretation as an expanded name is possible, it
6132 -- must be a selected component of a record returned by a
6133 -- function call. Reformat prefix as a function call, the rest
6134 -- is done by type resolution. If the prefix is procedure or
6135 -- entry, as is P.X; this is an error.
6137 if Ekind (P_Name) /= E_Function
6138 and then
6139 (not Is_Overloaded (P)
6140 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
6141 then
6142 -- Prefix may mention a package that is hidden by a local
6143 -- declaration: let the user know. Scan the full homonym
6144 -- chain, the candidate package may be anywhere on it.
6146 if Present (Homonym (Current_Entity (P_Name))) then
6148 P_Name := Current_Entity (P_Name);
6150 while Present (P_Name) loop
6151 exit when Ekind (P_Name) = E_Package;
6152 P_Name := Homonym (P_Name);
6153 end loop;
6155 if Present (P_Name) then
6156 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
6158 Error_Msg_NE
6159 ("package& is hidden by declaration#",
6160 N, P_Name);
6162 Set_Entity (Prefix (N), P_Name);
6163 Find_Expanded_Name (N);
6164 return;
6165 else
6166 P_Name := Entity (Prefix (N));
6167 end if;
6168 end if;
6170 Error_Msg_NE
6171 ("invalid prefix in selected component&", N, P_Name);
6172 Change_Selected_Component_To_Expanded_Name (N);
6173 Set_Entity (N, Any_Id);
6174 Set_Etype (N, Any_Type);
6176 else
6177 Nam := New_Copy (P);
6178 Save_Interps (P, Nam);
6179 Rewrite (P,
6180 Make_Function_Call (Sloc (P), Name => Nam));
6181 Analyze_Call (P);
6182 Analyze_Selected_Component (N);
6183 end if;
6184 end if;
6186 -- Remaining cases generate various error messages
6188 else
6189 -- Format node as expanded name, to avoid cascaded errors
6191 Change_Selected_Component_To_Expanded_Name (N);
6192 Set_Entity (N, Any_Id);
6193 Set_Etype (N, Any_Type);
6195 -- Issue error message, but avoid this if error issued already.
6196 -- Use identifier of prefix if one is available.
6198 if P_Name = Any_Id then
6199 null;
6201 elsif Ekind (P_Name) = E_Void then
6202 Premature_Usage (P);
6204 elsif Nkind (P) /= N_Attribute_Reference then
6205 Error_Msg_N (
6206 "invalid prefix in selected component&", P);
6208 if Is_Access_Type (P_Type)
6209 and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
6210 then
6211 Error_Msg_N
6212 ("\dereference must not be of an incomplete type " &
6213 "(RM 3.10.1)", P);
6214 end if;
6216 else
6217 Error_Msg_N (
6218 "invalid prefix in selected component", P);
6219 end if;
6220 end if;
6222 -- Selector name is restricted in SPARK
6224 if Nkind (N) = N_Expanded_Name
6225 and then Restriction_Check_Required (SPARK_05)
6226 then
6227 if Is_Subprogram (P_Name) then
6228 Check_SPARK_Restriction
6229 ("prefix of expanded name cannot be a subprogram", P);
6230 elsif Ekind (P_Name) = E_Loop then
6231 Check_SPARK_Restriction
6232 ("prefix of expanded name cannot be a loop statement", P);
6233 end if;
6234 end if;
6236 else
6237 -- If prefix is not the name of an entity, it must be an expression,
6238 -- whose type is appropriate for a record. This is determined by
6239 -- type resolution.
6241 Analyze_Selected_Component (N);
6242 end if;
6244 Analyze_Dimension (N);
6245 end Find_Selected_Component;
6247 ---------------
6248 -- Find_Type --
6249 ---------------
6251 procedure Find_Type (N : Node_Id) is
6252 C : Entity_Id;
6253 Typ : Entity_Id;
6254 T : Entity_Id;
6255 T_Name : Entity_Id;
6257 begin
6258 if N = Error then
6259 return;
6261 elsif Nkind (N) = N_Attribute_Reference then
6263 -- Class attribute. This is not valid in Ada 83 mode, but we do not
6264 -- need to enforce that at this point, since the declaration of the
6265 -- tagged type in the prefix would have been flagged already.
6267 if Attribute_Name (N) = Name_Class then
6268 Check_Restriction (No_Dispatch, N);
6269 Find_Type (Prefix (N));
6271 -- Propagate error from bad prefix
6273 if Etype (Prefix (N)) = Any_Type then
6274 Set_Entity (N, Any_Type);
6275 Set_Etype (N, Any_Type);
6276 return;
6277 end if;
6279 T := Base_Type (Entity (Prefix (N)));
6281 -- Case where type is not known to be tagged. Its appearance in
6282 -- the prefix of the 'Class attribute indicates that the full view
6283 -- will be tagged.
6285 if not Is_Tagged_Type (T) then
6286 if Ekind (T) = E_Incomplete_Type then
6288 -- It is legal to denote the class type of an incomplete
6289 -- type. The full type will have to be tagged, of course.
6290 -- In Ada 2005 this usage is declared obsolescent, so we
6291 -- warn accordingly. This usage is only legal if the type
6292 -- is completed in the current scope, and not for a limited
6293 -- view of a type.
6295 if Ada_Version >= Ada_2005 then
6297 -- Test whether the Available_View of a limited type view
6298 -- is tagged, since the limited view may not be marked as
6299 -- tagged if the type itself has an untagged incomplete
6300 -- type view in its package.
6302 if From_With_Type (T)
6303 and then not Is_Tagged_Type (Available_View (T))
6304 then
6305 Error_Msg_N
6306 ("prefix of Class attribute must be tagged", N);
6307 Set_Etype (N, Any_Type);
6308 Set_Entity (N, Any_Type);
6309 return;
6311 -- ??? This test is temporarily disabled (always
6312 -- False) because it causes an unwanted warning on
6313 -- GNAT sources (built with -gnatg, which includes
6314 -- Warn_On_Obsolescent_ Feature). Once this issue
6315 -- is cleared in the sources, it can be enabled.
6317 elsif Warn_On_Obsolescent_Feature and then False then
6318 Error_Msg_N
6319 ("applying 'Class to an untagged incomplete type"
6320 & " is an obsolescent feature (RM J.11)?r?", N);
6321 end if;
6322 end if;
6324 Set_Is_Tagged_Type (T);
6325 Set_Direct_Primitive_Operations (T, New_Elmt_List);
6326 Make_Class_Wide_Type (T);
6327 Set_Entity (N, Class_Wide_Type (T));
6328 Set_Etype (N, Class_Wide_Type (T));
6330 elsif Ekind (T) = E_Private_Type
6331 and then not Is_Generic_Type (T)
6332 and then In_Private_Part (Scope (T))
6333 then
6334 -- The Class attribute can be applied to an untagged private
6335 -- type fulfilled by a tagged type prior to the full type
6336 -- declaration (but only within the parent package's private
6337 -- part). Create the class-wide type now and check that the
6338 -- full type is tagged later during its analysis. Note that
6339 -- we do not mark the private type as tagged, unlike the
6340 -- case of incomplete types, because the type must still
6341 -- appear untagged to outside units.
6343 if No (Class_Wide_Type (T)) then
6344 Make_Class_Wide_Type (T);
6345 end if;
6347 Set_Entity (N, Class_Wide_Type (T));
6348 Set_Etype (N, Class_Wide_Type (T));
6350 else
6351 -- Should we introduce a type Any_Tagged and use Wrong_Type
6352 -- here, it would be a bit more consistent???
6354 Error_Msg_NE
6355 ("tagged type required, found}",
6356 Prefix (N), First_Subtype (T));
6357 Set_Entity (N, Any_Type);
6358 return;
6359 end if;
6361 -- Case of tagged type
6363 else
6364 if Is_Concurrent_Type (T) then
6365 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
6367 -- Previous error. Use current type, which at least
6368 -- provides some operations.
6370 C := Entity (Prefix (N));
6372 else
6373 C := Class_Wide_Type
6374 (Corresponding_Record_Type (Entity (Prefix (N))));
6375 end if;
6377 else
6378 C := Class_Wide_Type (Entity (Prefix (N)));
6379 end if;
6381 Set_Entity_With_Style_Check (N, C);
6382 Generate_Reference (C, N);
6383 Set_Etype (N, C);
6384 end if;
6386 -- Base attribute, not allowed in Ada 83
6388 elsif Attribute_Name (N) = Name_Base then
6389 Error_Msg_Name_1 := Name_Base;
6390 Check_SPARK_Restriction
6391 ("attribute% is only allowed as prefix of another attribute", N);
6393 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
6394 Error_Msg_N
6395 ("(Ada 83) Base attribute not allowed in subtype mark", N);
6397 else
6398 Find_Type (Prefix (N));
6399 Typ := Entity (Prefix (N));
6401 if Ada_Version >= Ada_95
6402 and then not Is_Scalar_Type (Typ)
6403 and then not Is_Generic_Type (Typ)
6404 then
6405 Error_Msg_N
6406 ("prefix of Base attribute must be scalar type",
6407 Prefix (N));
6409 elsif Warn_On_Redundant_Constructs
6410 and then Base_Type (Typ) = Typ
6411 then
6412 Error_Msg_NE -- CODEFIX
6413 ("redundant attribute, & is its own base type?r?", N, Typ);
6414 end if;
6416 T := Base_Type (Typ);
6418 -- Rewrite attribute reference with type itself (see similar
6419 -- processing in Analyze_Attribute, case Base). Preserve prefix
6420 -- if present, for other legality checks.
6422 if Nkind (Prefix (N)) = N_Expanded_Name then
6423 Rewrite (N,
6424 Make_Expanded_Name (Sloc (N),
6425 Chars => Chars (T),
6426 Prefix => New_Copy (Prefix (Prefix (N))),
6427 Selector_Name => New_Reference_To (T, Sloc (N))));
6429 else
6430 Rewrite (N, New_Reference_To (T, Sloc (N)));
6431 end if;
6433 Set_Entity (N, T);
6434 Set_Etype (N, T);
6435 end if;
6437 elsif Attribute_Name (N) = Name_Stub_Type then
6439 -- This is handled in Analyze_Attribute
6441 Analyze (N);
6443 -- All other attributes are invalid in a subtype mark
6445 else
6446 Error_Msg_N ("invalid attribute in subtype mark", N);
6447 end if;
6449 else
6450 Analyze (N);
6452 if Is_Entity_Name (N) then
6453 T_Name := Entity (N);
6454 else
6455 Error_Msg_N ("subtype mark required in this context", N);
6456 Set_Etype (N, Any_Type);
6457 return;
6458 end if;
6460 if T_Name = Any_Id or else Etype (N) = Any_Type then
6462 -- Undefined id. Make it into a valid type
6464 Set_Entity (N, Any_Type);
6466 elsif not Is_Type (T_Name)
6467 and then T_Name /= Standard_Void_Type
6468 then
6469 Error_Msg_Sloc := Sloc (T_Name);
6470 Error_Msg_N ("subtype mark required in this context", N);
6471 Error_Msg_NE ("\\found & declared#", N, T_Name);
6472 Set_Entity (N, Any_Type);
6474 else
6475 -- If the type is an incomplete type created to handle
6476 -- anonymous access components of a record type, then the
6477 -- incomplete type is the visible entity and subsequent
6478 -- references will point to it. Mark the original full
6479 -- type as referenced, to prevent spurious warnings.
6481 if Is_Incomplete_Type (T_Name)
6482 and then Present (Full_View (T_Name))
6483 and then not Comes_From_Source (T_Name)
6484 then
6485 Set_Referenced (Full_View (T_Name));
6486 end if;
6488 T_Name := Get_Full_View (T_Name);
6490 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
6491 -- limited-with clauses
6493 if From_With_Type (T_Name)
6494 and then Ekind (T_Name) in Incomplete_Kind
6495 and then Present (Non_Limited_View (T_Name))
6496 and then Is_Interface (Non_Limited_View (T_Name))
6497 then
6498 T_Name := Non_Limited_View (T_Name);
6499 end if;
6501 if In_Open_Scopes (T_Name) then
6502 if Ekind (Base_Type (T_Name)) = E_Task_Type then
6504 -- In Ada 2005, a task name can be used in an access
6505 -- definition within its own body. It cannot be used
6506 -- in the discriminant part of the task declaration,
6507 -- nor anywhere else in the declaration because entries
6508 -- cannot have access parameters.
6510 if Ada_Version >= Ada_2005
6511 and then Nkind (Parent (N)) = N_Access_Definition
6512 then
6513 Set_Entity (N, T_Name);
6514 Set_Etype (N, T_Name);
6516 if Has_Completion (T_Name) then
6517 return;
6519 else
6520 Error_Msg_N
6521 ("task type cannot be used as type mark " &
6522 "within its own declaration", N);
6523 end if;
6525 else
6526 Error_Msg_N
6527 ("task type cannot be used as type mark " &
6528 "within its own spec or body", N);
6529 end if;
6531 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
6533 -- In Ada 2005, a protected name can be used in an access
6534 -- definition within its own body.
6536 if Ada_Version >= Ada_2005
6537 and then Nkind (Parent (N)) = N_Access_Definition
6538 then
6539 Set_Entity (N, T_Name);
6540 Set_Etype (N, T_Name);
6541 return;
6543 else
6544 Error_Msg_N
6545 ("protected type cannot be used as type mark " &
6546 "within its own spec or body", N);
6547 end if;
6549 else
6550 Error_Msg_N ("type declaration cannot refer to itself", N);
6551 end if;
6553 Set_Etype (N, Any_Type);
6554 Set_Entity (N, Any_Type);
6555 Set_Error_Posted (T_Name);
6556 return;
6557 end if;
6559 Set_Entity (N, T_Name);
6560 Set_Etype (N, T_Name);
6561 end if;
6562 end if;
6564 if Present (Etype (N)) and then Comes_From_Source (N) then
6565 if Is_Fixed_Point_Type (Etype (N)) then
6566 Check_Restriction (No_Fixed_Point, N);
6567 elsif Is_Floating_Point_Type (Etype (N)) then
6568 Check_Restriction (No_Floating_Point, N);
6569 end if;
6570 end if;
6571 end Find_Type;
6573 ------------------------------------
6574 -- Has_Implicit_Character_Literal --
6575 ------------------------------------
6577 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
6578 Id : Entity_Id;
6579 Found : Boolean := False;
6580 P : constant Entity_Id := Entity (Prefix (N));
6581 Priv_Id : Entity_Id := Empty;
6583 begin
6584 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
6585 Priv_Id := First_Private_Entity (P);
6586 end if;
6588 if P = Standard_Standard then
6589 Change_Selected_Component_To_Expanded_Name (N);
6590 Rewrite (N, Selector_Name (N));
6591 Analyze (N);
6592 Set_Etype (Original_Node (N), Standard_Character);
6593 return True;
6594 end if;
6596 Id := First_Entity (P);
6597 while Present (Id) and then Id /= Priv_Id loop
6598 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
6600 -- We replace the node with the literal itself, resolve as a
6601 -- character, and set the type correctly.
6603 if not Found then
6604 Change_Selected_Component_To_Expanded_Name (N);
6605 Rewrite (N, Selector_Name (N));
6606 Analyze (N);
6607 Set_Etype (N, Id);
6608 Set_Etype (Original_Node (N), Id);
6609 Found := True;
6611 else
6612 -- More than one type derived from Character in given scope.
6613 -- Collect all possible interpretations.
6615 Add_One_Interp (N, Id, Id);
6616 end if;
6617 end if;
6619 Next_Entity (Id);
6620 end loop;
6622 return Found;
6623 end Has_Implicit_Character_Literal;
6625 ----------------------
6626 -- Has_Private_With --
6627 ----------------------
6629 function Has_Private_With (E : Entity_Id) return Boolean is
6630 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
6631 Item : Node_Id;
6633 begin
6634 Item := First (Context_Items (Comp_Unit));
6635 while Present (Item) loop
6636 if Nkind (Item) = N_With_Clause
6637 and then Private_Present (Item)
6638 and then Entity (Name (Item)) = E
6639 then
6640 return True;
6641 end if;
6643 Next (Item);
6644 end loop;
6646 return False;
6647 end Has_Private_With;
6649 ---------------------------
6650 -- Has_Implicit_Operator --
6651 ---------------------------
6653 function Has_Implicit_Operator (N : Node_Id) return Boolean is
6654 Op_Id : constant Name_Id := Chars (Selector_Name (N));
6655 P : constant Entity_Id := Entity (Prefix (N));
6656 Id : Entity_Id;
6657 Priv_Id : Entity_Id := Empty;
6659 procedure Add_Implicit_Operator
6660 (T : Entity_Id;
6661 Op_Type : Entity_Id := Empty);
6662 -- Add implicit interpretation to node N, using the type for which a
6663 -- predefined operator exists. If the operator yields a boolean type,
6664 -- the Operand_Type is implicitly referenced by the operator, and a
6665 -- reference to it must be generated.
6667 ---------------------------
6668 -- Add_Implicit_Operator --
6669 ---------------------------
6671 procedure Add_Implicit_Operator
6672 (T : Entity_Id;
6673 Op_Type : Entity_Id := Empty)
6675 Predef_Op : Entity_Id;
6677 begin
6678 Predef_Op := Current_Entity (Selector_Name (N));
6679 while Present (Predef_Op)
6680 and then Scope (Predef_Op) /= Standard_Standard
6681 loop
6682 Predef_Op := Homonym (Predef_Op);
6683 end loop;
6685 if Nkind (N) = N_Selected_Component then
6686 Change_Selected_Component_To_Expanded_Name (N);
6687 end if;
6689 -- If the context is an unanalyzed function call, determine whether
6690 -- a binary or unary interpretation is required.
6692 if Nkind (Parent (N)) = N_Indexed_Component then
6693 declare
6694 Is_Binary_Call : constant Boolean :=
6695 Present
6696 (Next (First (Expressions (Parent (N)))));
6697 Is_Binary_Op : constant Boolean :=
6698 First_Entity
6699 (Predef_Op) /= Last_Entity (Predef_Op);
6700 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
6702 begin
6703 if Is_Binary_Call then
6704 if Is_Binary_Op then
6705 Add_One_Interp (N, Predef_Op, T);
6706 else
6707 Add_One_Interp (N, Predef_Op2, T);
6708 end if;
6710 else
6711 if not Is_Binary_Op then
6712 Add_One_Interp (N, Predef_Op, T);
6713 else
6714 Add_One_Interp (N, Predef_Op2, T);
6715 end if;
6716 end if;
6717 end;
6719 else
6720 Add_One_Interp (N, Predef_Op, T);
6722 -- For operators with unary and binary interpretations, if
6723 -- context is not a call, add both
6725 if Present (Homonym (Predef_Op)) then
6726 Add_One_Interp (N, Homonym (Predef_Op), T);
6727 end if;
6728 end if;
6730 -- The node is a reference to a predefined operator, and
6731 -- an implicit reference to the type of its operands.
6733 if Present (Op_Type) then
6734 Generate_Operator_Reference (N, Op_Type);
6735 else
6736 Generate_Operator_Reference (N, T);
6737 end if;
6738 end Add_Implicit_Operator;
6740 -- Start of processing for Has_Implicit_Operator
6742 begin
6743 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
6744 Priv_Id := First_Private_Entity (P);
6745 end if;
6747 Id := First_Entity (P);
6749 case Op_Id is
6751 -- Boolean operators: an implicit declaration exists if the scope
6752 -- contains a declaration for a derived Boolean type, or for an
6753 -- array of Boolean type.
6755 when Name_Op_And | Name_Op_Not | Name_Op_Or | Name_Op_Xor =>
6756 while Id /= Priv_Id loop
6757 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
6758 Add_Implicit_Operator (Id);
6759 return True;
6760 end if;
6762 Next_Entity (Id);
6763 end loop;
6765 -- Equality: look for any non-limited type (result is Boolean)
6767 when Name_Op_Eq | Name_Op_Ne =>
6768 while Id /= Priv_Id loop
6769 if Is_Type (Id)
6770 and then not Is_Limited_Type (Id)
6771 and then Is_Base_Type (Id)
6772 then
6773 Add_Implicit_Operator (Standard_Boolean, Id);
6774 return True;
6775 end if;
6777 Next_Entity (Id);
6778 end loop;
6780 -- Comparison operators: scalar type, or array of scalar
6782 when Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge =>
6783 while Id /= Priv_Id loop
6784 if (Is_Scalar_Type (Id)
6785 or else (Is_Array_Type (Id)
6786 and then Is_Scalar_Type (Component_Type (Id))))
6787 and then Is_Base_Type (Id)
6788 then
6789 Add_Implicit_Operator (Standard_Boolean, Id);
6790 return True;
6791 end if;
6793 Next_Entity (Id);
6794 end loop;
6796 -- Arithmetic operators: any numeric type
6798 when Name_Op_Abs |
6799 Name_Op_Add |
6800 Name_Op_Mod |
6801 Name_Op_Rem |
6802 Name_Op_Subtract |
6803 Name_Op_Multiply |
6804 Name_Op_Divide |
6805 Name_Op_Expon =>
6806 while Id /= Priv_Id loop
6807 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
6808 Add_Implicit_Operator (Id);
6809 return True;
6810 end if;
6812 Next_Entity (Id);
6813 end loop;
6815 -- Concatenation: any one-dimensional array type
6817 when Name_Op_Concat =>
6818 while Id /= Priv_Id loop
6819 if Is_Array_Type (Id)
6820 and then Number_Dimensions (Id) = 1
6821 and then Is_Base_Type (Id)
6822 then
6823 Add_Implicit_Operator (Id);
6824 return True;
6825 end if;
6827 Next_Entity (Id);
6828 end loop;
6830 -- What is the others condition here? Should we be using a
6831 -- subtype of Name_Id that would restrict to operators ???
6833 when others => null;
6834 end case;
6836 -- If we fall through, then we do not have an implicit operator
6838 return False;
6840 end Has_Implicit_Operator;
6842 -----------------------------------
6843 -- Has_Loop_In_Inner_Open_Scopes --
6844 -----------------------------------
6846 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
6847 begin
6848 -- Several scope stacks are maintained by Scope_Stack. The base of the
6849 -- currently active scope stack is denoted by the Is_Active_Stack_Base
6850 -- flag in the scope stack entry. Note that the scope stacks used to
6851 -- simply be delimited implicitly by the presence of Standard_Standard
6852 -- at their base, but there now are cases where this is not sufficient
6853 -- because Standard_Standard actually may appear in the middle of the
6854 -- active set of scopes.
6856 for J in reverse 0 .. Scope_Stack.Last loop
6858 -- S was reached without seing a loop scope first
6860 if Scope_Stack.Table (J).Entity = S then
6861 return False;
6863 -- S was not yet reached, so it contains at least one inner loop
6865 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
6866 return True;
6867 end if;
6869 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
6870 -- cases where Standard_Standard appears in the middle of the active
6871 -- set of scopes. This affects the declaration and overriding of
6872 -- private inherited operations in instantiations of generic child
6873 -- units.
6875 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
6876 end loop;
6878 raise Program_Error; -- unreachable
6879 end Has_Loop_In_Inner_Open_Scopes;
6881 --------------------
6882 -- In_Open_Scopes --
6883 --------------------
6885 function In_Open_Scopes (S : Entity_Id) return Boolean is
6886 begin
6887 -- Several scope stacks are maintained by Scope_Stack. The base of the
6888 -- currently active scope stack is denoted by the Is_Active_Stack_Base
6889 -- flag in the scope stack entry. Note that the scope stacks used to
6890 -- simply be delimited implicitly by the presence of Standard_Standard
6891 -- at their base, but there now are cases where this is not sufficient
6892 -- because Standard_Standard actually may appear in the middle of the
6893 -- active set of scopes.
6895 for J in reverse 0 .. Scope_Stack.Last loop
6896 if Scope_Stack.Table (J).Entity = S then
6897 return True;
6898 end if;
6900 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
6901 -- cases where Standard_Standard appears in the middle of the active
6902 -- set of scopes. This affects the declaration and overriding of
6903 -- private inherited operations in instantiations of generic child
6904 -- units.
6906 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
6907 end loop;
6909 return False;
6910 end In_Open_Scopes;
6912 -----------------------------
6913 -- Inherit_Renamed_Profile --
6914 -----------------------------
6916 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
6917 New_F : Entity_Id;
6918 Old_F : Entity_Id;
6919 Old_T : Entity_Id;
6920 New_T : Entity_Id;
6922 begin
6923 if Ekind (Old_S) = E_Operator then
6924 New_F := First_Formal (New_S);
6926 while Present (New_F) loop
6927 Set_Etype (New_F, Base_Type (Etype (New_F)));
6928 Next_Formal (New_F);
6929 end loop;
6931 Set_Etype (New_S, Base_Type (Etype (New_S)));
6933 else
6934 New_F := First_Formal (New_S);
6935 Old_F := First_Formal (Old_S);
6937 while Present (New_F) loop
6938 New_T := Etype (New_F);
6939 Old_T := Etype (Old_F);
6941 -- If the new type is a renaming of the old one, as is the
6942 -- case for actuals in instances, retain its name, to simplify
6943 -- later disambiguation.
6945 if Nkind (Parent (New_T)) = N_Subtype_Declaration
6946 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
6947 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
6948 then
6949 null;
6950 else
6951 Set_Etype (New_F, Old_T);
6952 end if;
6954 Next_Formal (New_F);
6955 Next_Formal (Old_F);
6956 end loop;
6958 if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then
6959 Set_Etype (New_S, Etype (Old_S));
6960 end if;
6961 end if;
6962 end Inherit_Renamed_Profile;
6964 ----------------
6965 -- Initialize --
6966 ----------------
6968 procedure Initialize is
6969 begin
6970 Urefs.Init;
6971 end Initialize;
6973 -------------------------
6974 -- Install_Use_Clauses --
6975 -------------------------
6977 procedure Install_Use_Clauses
6978 (Clause : Node_Id;
6979 Force_Installation : Boolean := False)
6981 U : Node_Id;
6982 P : Node_Id;
6983 Id : Entity_Id;
6985 begin
6986 U := Clause;
6987 while Present (U) loop
6989 -- Case of USE package
6991 if Nkind (U) = N_Use_Package_Clause then
6992 P := First (Names (U));
6993 while Present (P) loop
6994 Id := Entity (P);
6996 if Ekind (Id) = E_Package then
6997 if In_Use (Id) then
6998 Note_Redundant_Use (P);
7000 elsif Present (Renamed_Object (Id))
7001 and then In_Use (Renamed_Object (Id))
7002 then
7003 Note_Redundant_Use (P);
7005 elsif Force_Installation or else Applicable_Use (P) then
7006 Use_One_Package (Id, U);
7008 end if;
7009 end if;
7011 Next (P);
7012 end loop;
7014 -- Case of USE TYPE
7016 else
7017 P := First (Subtype_Marks (U));
7018 while Present (P) loop
7019 if not Is_Entity_Name (P)
7020 or else No (Entity (P))
7021 then
7022 null;
7024 elsif Entity (P) /= Any_Type then
7025 Use_One_Type (P);
7026 end if;
7028 Next (P);
7029 end loop;
7030 end if;
7032 Next_Use_Clause (U);
7033 end loop;
7034 end Install_Use_Clauses;
7036 -------------------------------------
7037 -- Is_Appropriate_For_Entry_Prefix --
7038 -------------------------------------
7040 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
7041 P_Type : Entity_Id := T;
7043 begin
7044 if Is_Access_Type (P_Type) then
7045 P_Type := Designated_Type (P_Type);
7046 end if;
7048 return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
7049 end Is_Appropriate_For_Entry_Prefix;
7051 -------------------------------
7052 -- Is_Appropriate_For_Record --
7053 -------------------------------
7055 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is
7057 function Has_Components (T1 : Entity_Id) return Boolean;
7058 -- Determine if given type has components (i.e. is either a record
7059 -- type or a type that has discriminants).
7061 --------------------
7062 -- Has_Components --
7063 --------------------
7065 function Has_Components (T1 : Entity_Id) return Boolean is
7066 begin
7067 return Is_Record_Type (T1)
7068 or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
7069 or else (Is_Task_Type (T1) and then Has_Discriminants (T1))
7070 or else (Is_Incomplete_Type (T1)
7071 and then From_With_Type (T1)
7072 and then Present (Non_Limited_View (T1))
7073 and then Is_Record_Type
7074 (Get_Full_View (Non_Limited_View (T1))));
7075 end Has_Components;
7077 -- Start of processing for Is_Appropriate_For_Record
7079 begin
7080 return
7081 Present (T)
7082 and then (Has_Components (T)
7083 or else (Is_Access_Type (T)
7084 and then Has_Components (Designated_Type (T))));
7085 end Is_Appropriate_For_Record;
7087 ------------------------
7088 -- Note_Redundant_Use --
7089 ------------------------
7091 procedure Note_Redundant_Use (Clause : Node_Id) is
7092 Pack_Name : constant Entity_Id := Entity (Clause);
7093 Cur_Use : constant Node_Id := Current_Use_Clause (Pack_Name);
7094 Decl : constant Node_Id := Parent (Clause);
7096 Prev_Use : Node_Id := Empty;
7097 Redundant : Node_Id := Empty;
7098 -- The Use_Clause which is actually redundant. In the simplest case it
7099 -- is Pack itself, but when we compile a body we install its context
7100 -- before that of its spec, in which case it is the use_clause in the
7101 -- spec that will appear to be redundant, and we want the warning to be
7102 -- placed on the body. Similar complications appear when the redundancy
7103 -- is between a child unit and one of its ancestors.
7105 begin
7106 Set_Redundant_Use (Clause, True);
7108 if not Comes_From_Source (Clause)
7109 or else In_Instance
7110 or else not Warn_On_Redundant_Constructs
7111 then
7112 return;
7113 end if;
7115 if not Is_Compilation_Unit (Current_Scope) then
7117 -- If the use_clause is in an inner scope, it is made redundant by
7118 -- some clause in the current context, with one exception: If we're
7119 -- compiling a nested package body, and the use_clause comes from the
7120 -- corresponding spec, the clause is not necessarily fully redundant,
7121 -- so we should not warn. If a warning was warranted, it would have
7122 -- been given when the spec was processed.
7124 if Nkind (Parent (Decl)) = N_Package_Specification then
7125 declare
7126 Package_Spec_Entity : constant Entity_Id :=
7127 Defining_Unit_Name (Parent (Decl));
7128 begin
7129 if In_Package_Body (Package_Spec_Entity) then
7130 return;
7131 end if;
7132 end;
7133 end if;
7135 Redundant := Clause;
7136 Prev_Use := Cur_Use;
7138 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
7139 declare
7140 Cur_Unit : constant Unit_Number_Type := Get_Source_Unit (Cur_Use);
7141 New_Unit : constant Unit_Number_Type := Get_Source_Unit (Clause);
7142 Scop : Entity_Id;
7144 begin
7145 if Cur_Unit = New_Unit then
7147 -- Redundant clause in same body
7149 Redundant := Clause;
7150 Prev_Use := Cur_Use;
7152 elsif Cur_Unit = Current_Sem_Unit then
7154 -- If the new clause is not in the current unit it has been
7155 -- analyzed first, and it makes the other one redundant.
7156 -- However, if the new clause appears in a subunit, Cur_Unit
7157 -- is still the parent, and in that case the redundant one
7158 -- is the one appearing in the subunit.
7160 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
7161 Redundant := Clause;
7162 Prev_Use := Cur_Use;
7164 -- Most common case: redundant clause in body,
7165 -- original clause in spec. Current scope is spec entity.
7167 elsif
7168 Current_Scope =
7169 Defining_Entity (
7170 Unit (Library_Unit (Cunit (Current_Sem_Unit))))
7171 then
7172 Redundant := Cur_Use;
7173 Prev_Use := Clause;
7175 else
7176 -- The new clause may appear in an unrelated unit, when
7177 -- the parents of a generic are being installed prior to
7178 -- instantiation. In this case there must be no warning.
7179 -- We detect this case by checking whether the current top
7180 -- of the stack is related to the current compilation.
7182 Scop := Current_Scope;
7183 while Present (Scop) and then Scop /= Standard_Standard loop
7184 if Is_Compilation_Unit (Scop)
7185 and then not Is_Child_Unit (Scop)
7186 then
7187 return;
7189 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
7190 exit;
7191 end if;
7193 Scop := Scope (Scop);
7194 end loop;
7196 Redundant := Cur_Use;
7197 Prev_Use := Clause;
7198 end if;
7200 elsif New_Unit = Current_Sem_Unit then
7201 Redundant := Clause;
7202 Prev_Use := Cur_Use;
7204 else
7205 -- Neither is the current unit, so they appear in parent or
7206 -- sibling units. Warning will be emitted elsewhere.
7208 return;
7209 end if;
7210 end;
7212 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
7213 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
7214 then
7215 -- Use_clause is in child unit of current unit, and the child unit
7216 -- appears in the context of the body of the parent, so it has been
7217 -- installed first, even though it is the redundant one. Depending on
7218 -- their placement in the context, the visible or the private parts
7219 -- of the two units, either might appear as redundant, but the
7220 -- message has to be on the current unit.
7222 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
7223 Redundant := Cur_Use;
7224 Prev_Use := Clause;
7225 else
7226 Redundant := Clause;
7227 Prev_Use := Cur_Use;
7228 end if;
7230 -- If the new use clause appears in the private part of a parent unit
7231 -- it may appear to be redundant w.r.t. a use clause in a child unit,
7232 -- but the previous use clause was needed in the visible part of the
7233 -- child, and no warning should be emitted.
7235 if Nkind (Parent (Decl)) = N_Package_Specification
7236 and then
7237 List_Containing (Decl) = Private_Declarations (Parent (Decl))
7238 then
7239 declare
7240 Par : constant Entity_Id := Defining_Entity (Parent (Decl));
7241 Spec : constant Node_Id :=
7242 Specification (Unit (Cunit (Current_Sem_Unit)));
7244 begin
7245 if Is_Compilation_Unit (Par)
7246 and then Par /= Cunit_Entity (Current_Sem_Unit)
7247 and then Parent (Cur_Use) = Spec
7248 and then
7249 List_Containing (Cur_Use) = Visible_Declarations (Spec)
7250 then
7251 return;
7252 end if;
7253 end;
7254 end if;
7256 -- Finally, if the current use clause is in the context then
7257 -- the clause is redundant when it is nested within the unit.
7259 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
7260 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
7261 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
7262 then
7263 Redundant := Clause;
7264 Prev_Use := Cur_Use;
7266 else
7267 null;
7268 end if;
7270 if Present (Redundant) then
7271 Error_Msg_Sloc := Sloc (Prev_Use);
7272 Error_Msg_NE -- CODEFIX
7273 ("& is already use-visible through previous use clause #??",
7274 Redundant, Pack_Name);
7275 end if;
7276 end Note_Redundant_Use;
7278 ---------------
7279 -- Pop_Scope --
7280 ---------------
7282 procedure Pop_Scope is
7283 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
7284 S : constant Entity_Id := SST.Entity;
7286 begin
7287 if Debug_Flag_E then
7288 Write_Info;
7289 end if;
7291 -- Set Default_Storage_Pool field of the library unit if necessary
7293 if Ekind_In (S, E_Package, E_Generic_Package)
7294 and then
7295 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
7296 then
7297 declare
7298 Aux : constant Node_Id :=
7299 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
7300 begin
7301 if No (Default_Storage_Pool (Aux)) then
7302 Set_Default_Storage_Pool (Aux, Default_Pool);
7303 end if;
7304 end;
7305 end if;
7307 Scope_Suppress := SST.Save_Scope_Suppress;
7308 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
7309 Check_Policy_List := SST.Save_Check_Policy_List;
7310 Default_Pool := SST.Save_Default_Storage_Pool;
7312 if Debug_Flag_W then
7313 Write_Str ("<-- exiting scope: ");
7314 Write_Name (Chars (Current_Scope));
7315 Write_Str (", Depth=");
7316 Write_Int (Int (Scope_Stack.Last));
7317 Write_Eol;
7318 end if;
7320 End_Use_Clauses (SST.First_Use_Clause);
7322 -- If the actions to be wrapped are still there they will get lost
7323 -- causing incomplete code to be generated. It is better to abort in
7324 -- this case (and we do the abort even with assertions off since the
7325 -- penalty is incorrect code generation).
7327 if SST.Actions_To_Be_Wrapped_Before /= No_List
7328 or else
7329 SST.Actions_To_Be_Wrapped_After /= No_List
7330 then
7331 raise Program_Error;
7332 end if;
7334 -- Free last subprogram name if allocated, and pop scope
7336 Free (SST.Last_Subprogram_Name);
7337 Scope_Stack.Decrement_Last;
7338 end Pop_Scope;
7340 ---------------
7341 -- Push_Scope --
7342 ---------------
7344 procedure Push_Scope (S : Entity_Id) is
7345 E : constant Entity_Id := Scope (S);
7347 begin
7348 if Ekind (S) = E_Void then
7349 null;
7351 -- Set scope depth if not a non-concurrent type, and we have not yet set
7352 -- the scope depth. This means that we have the first occurrence of the
7353 -- scope, and this is where the depth is set.
7355 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
7356 and then not Scope_Depth_Set (S)
7357 then
7358 if S = Standard_Standard then
7359 Set_Scope_Depth_Value (S, Uint_0);
7361 elsif Is_Child_Unit (S) then
7362 Set_Scope_Depth_Value (S, Uint_1);
7364 elsif not Is_Record_Type (Current_Scope) then
7365 if Ekind (S) = E_Loop then
7366 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
7367 else
7368 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
7369 end if;
7370 end if;
7371 end if;
7373 Scope_Stack.Increment_Last;
7375 declare
7376 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
7378 begin
7379 SST.Entity := S;
7380 SST.Save_Scope_Suppress := Scope_Suppress;
7381 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
7382 SST.Save_Check_Policy_List := Check_Policy_List;
7383 SST.Save_Default_Storage_Pool := Default_Pool;
7385 if Scope_Stack.Last > Scope_Stack.First then
7386 SST.Component_Alignment_Default := Scope_Stack.Table
7387 (Scope_Stack.Last - 1).
7388 Component_Alignment_Default;
7389 end if;
7391 SST.Last_Subprogram_Name := null;
7392 SST.Is_Transient := False;
7393 SST.Node_To_Be_Wrapped := Empty;
7394 SST.Pending_Freeze_Actions := No_List;
7395 SST.Actions_To_Be_Wrapped_Before := No_List;
7396 SST.Actions_To_Be_Wrapped_After := No_List;
7397 SST.First_Use_Clause := Empty;
7398 SST.Is_Active_Stack_Base := False;
7399 SST.Previous_Visibility := False;
7400 end;
7402 if Debug_Flag_W then
7403 Write_Str ("--> new scope: ");
7404 Write_Name (Chars (Current_Scope));
7405 Write_Str (", Id=");
7406 Write_Int (Int (Current_Scope));
7407 Write_Str (", Depth=");
7408 Write_Int (Int (Scope_Stack.Last));
7409 Write_Eol;
7410 end if;
7412 -- Deal with copying flags from the previous scope to this one. This is
7413 -- not necessary if either scope is standard, or if the new scope is a
7414 -- child unit.
7416 if S /= Standard_Standard
7417 and then Scope (S) /= Standard_Standard
7418 and then not Is_Child_Unit (S)
7419 then
7420 if Nkind (E) not in N_Entity then
7421 return;
7422 end if;
7424 -- Copy categorization flags from Scope (S) to S, this is not done
7425 -- when Scope (S) is Standard_Standard since propagation is from
7426 -- library unit entity inwards. Copy other relevant attributes as
7427 -- well (Discard_Names in particular).
7429 -- We only propagate inwards for library level entities,
7430 -- inner level subprograms do not inherit the categorization.
7432 if Is_Library_Level_Entity (S) then
7433 Set_Is_Preelaborated (S, Is_Preelaborated (E));
7434 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
7435 Set_Discard_Names (S, Discard_Names (E));
7436 Set_Suppress_Value_Tracking_On_Call
7437 (S, Suppress_Value_Tracking_On_Call (E));
7438 Set_Categorization_From_Scope (E => S, Scop => E);
7439 end if;
7440 end if;
7442 if Is_Child_Unit (S)
7443 and then Present (E)
7444 and then Ekind_In (E, E_Package, E_Generic_Package)
7445 and then
7446 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
7447 then
7448 declare
7449 Aux : constant Node_Id :=
7450 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
7451 begin
7452 if Present (Default_Storage_Pool (Aux)) then
7453 Default_Pool := Default_Storage_Pool (Aux);
7454 end if;
7455 end;
7456 end if;
7457 end Push_Scope;
7459 ---------------------
7460 -- Premature_Usage --
7461 ---------------------
7463 procedure Premature_Usage (N : Node_Id) is
7464 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
7465 E : Entity_Id := Entity (N);
7467 begin
7468 -- Within an instance, the analysis of the actual for a formal object
7469 -- does not see the name of the object itself. This is significant only
7470 -- if the object is an aggregate, where its analysis does not do any
7471 -- name resolution on component associations. (see 4717-008). In such a
7472 -- case, look for the visible homonym on the chain.
7474 if In_Instance and then Present (Homonym (E)) then
7475 E := Homonym (E);
7476 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
7477 E := Homonym (E);
7478 end loop;
7480 if Present (E) then
7481 Set_Entity (N, E);
7482 Set_Etype (N, Etype (E));
7483 return;
7484 end if;
7485 end if;
7487 if Kind = N_Component_Declaration then
7488 Error_Msg_N
7489 ("component&! cannot be used before end of record declaration", N);
7491 elsif Kind = N_Parameter_Specification then
7492 Error_Msg_N
7493 ("formal parameter&! cannot be used before end of specification",
7496 elsif Kind = N_Discriminant_Specification then
7497 Error_Msg_N
7498 ("discriminant&! cannot be used before end of discriminant part",
7501 elsif Kind = N_Procedure_Specification
7502 or else Kind = N_Function_Specification
7503 then
7504 Error_Msg_N
7505 ("subprogram&! cannot be used before end of its declaration",
7508 elsif Kind = N_Full_Type_Declaration then
7509 Error_Msg_N
7510 ("type& cannot be used before end of its declaration!", N);
7512 else
7513 Error_Msg_N
7514 ("object& cannot be used before end of its declaration!", N);
7515 end if;
7516 end Premature_Usage;
7518 ------------------------
7519 -- Present_System_Aux --
7520 ------------------------
7522 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
7523 Loc : Source_Ptr;
7524 Aux_Name : Unit_Name_Type;
7525 Unum : Unit_Number_Type;
7526 Withn : Node_Id;
7527 With_Sys : Node_Id;
7528 The_Unit : Node_Id;
7530 function Find_System (C_Unit : Node_Id) return Entity_Id;
7531 -- Scan context clause of compilation unit to find with_clause
7532 -- for System.
7534 -----------------
7535 -- Find_System --
7536 -----------------
7538 function Find_System (C_Unit : Node_Id) return Entity_Id is
7539 With_Clause : Node_Id;
7541 begin
7542 With_Clause := First (Context_Items (C_Unit));
7543 while Present (With_Clause) loop
7544 if (Nkind (With_Clause) = N_With_Clause
7545 and then Chars (Name (With_Clause)) = Name_System)
7546 and then Comes_From_Source (With_Clause)
7547 then
7548 return With_Clause;
7549 end if;
7551 Next (With_Clause);
7552 end loop;
7554 return Empty;
7555 end Find_System;
7557 -- Start of processing for Present_System_Aux
7559 begin
7560 -- The child unit may have been loaded and analyzed already
7562 if Present (System_Aux_Id) then
7563 return True;
7565 -- If no previous pragma for System.Aux, nothing to load
7567 elsif No (System_Extend_Unit) then
7568 return False;
7570 -- Use the unit name given in the pragma to retrieve the unit.
7571 -- Verify that System itself appears in the context clause of the
7572 -- current compilation. If System is not present, an error will
7573 -- have been reported already.
7575 else
7576 With_Sys := Find_System (Cunit (Current_Sem_Unit));
7578 The_Unit := Unit (Cunit (Current_Sem_Unit));
7580 if No (With_Sys)
7581 and then
7582 (Nkind (The_Unit) = N_Package_Body
7583 or else (Nkind (The_Unit) = N_Subprogram_Body
7584 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
7585 then
7586 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
7587 end if;
7589 if No (With_Sys) and then Present (N) then
7591 -- If we are compiling a subunit, we need to examine its
7592 -- context as well (Current_Sem_Unit is the parent unit);
7594 The_Unit := Parent (N);
7595 while Nkind (The_Unit) /= N_Compilation_Unit loop
7596 The_Unit := Parent (The_Unit);
7597 end loop;
7599 if Nkind (Unit (The_Unit)) = N_Subunit then
7600 With_Sys := Find_System (The_Unit);
7601 end if;
7602 end if;
7604 if No (With_Sys) then
7605 return False;
7606 end if;
7608 Loc := Sloc (With_Sys);
7609 Get_Name_String (Chars (Expression (System_Extend_Unit)));
7610 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
7611 Name_Buffer (1 .. 7) := "system.";
7612 Name_Buffer (Name_Len + 8) := '%';
7613 Name_Buffer (Name_Len + 9) := 's';
7614 Name_Len := Name_Len + 9;
7615 Aux_Name := Name_Find;
7617 Unum :=
7618 Load_Unit
7619 (Load_Name => Aux_Name,
7620 Required => False,
7621 Subunit => False,
7622 Error_Node => With_Sys);
7624 if Unum /= No_Unit then
7625 Semantics (Cunit (Unum));
7626 System_Aux_Id :=
7627 Defining_Entity (Specification (Unit (Cunit (Unum))));
7629 Withn :=
7630 Make_With_Clause (Loc,
7631 Name =>
7632 Make_Expanded_Name (Loc,
7633 Chars => Chars (System_Aux_Id),
7634 Prefix => New_Reference_To (Scope (System_Aux_Id), Loc),
7635 Selector_Name => New_Reference_To (System_Aux_Id, Loc)));
7637 Set_Entity (Name (Withn), System_Aux_Id);
7639 Set_Library_Unit (Withn, Cunit (Unum));
7640 Set_Corresponding_Spec (Withn, System_Aux_Id);
7641 Set_First_Name (Withn, True);
7642 Set_Implicit_With (Withn, True);
7644 Insert_After (With_Sys, Withn);
7645 Mark_Rewrite_Insertion (Withn);
7646 Set_Context_Installed (Withn);
7648 return True;
7650 -- Here if unit load failed
7652 else
7653 Error_Msg_Name_1 := Name_System;
7654 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
7655 Error_Msg_N
7656 ("extension package `%.%` does not exist",
7657 Opt.System_Extend_Unit);
7658 return False;
7659 end if;
7660 end if;
7661 end Present_System_Aux;
7663 -------------------------
7664 -- Restore_Scope_Stack --
7665 -------------------------
7667 procedure Restore_Scope_Stack
7668 (List : Elist_Id;
7669 Handle_Use : Boolean := True)
7671 SS_Last : constant Int := Scope_Stack.Last;
7672 Elmt : Elmt_Id;
7674 begin
7675 -- Restore visibility of previous scope stack, if any, using the list
7676 -- we saved (we use Remove, since this list will not be used again).
7678 loop
7679 Elmt := Last_Elmt (List);
7680 exit when Elmt = No_Elmt;
7681 Set_Is_Immediately_Visible (Node (Elmt));
7682 Remove_Last_Elmt (List);
7683 end loop;
7685 -- Restore use clauses
7687 if SS_Last >= Scope_Stack.First
7688 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
7689 and then Handle_Use
7690 then
7691 Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
7692 end if;
7693 end Restore_Scope_Stack;
7695 ----------------------
7696 -- Save_Scope_Stack --
7697 ----------------------
7699 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
7700 -- consuming any memory. That is, Save_Scope_Stack took care of removing
7701 -- from immediate visibility entities and Restore_Scope_Stack took care
7702 -- of restoring their visibility analyzing the context of each entity. The
7703 -- problem of such approach is that it was fragile and caused unexpected
7704 -- visibility problems, and indeed one test was found where there was a
7705 -- real problem.
7707 -- Furthermore, the following experiment was carried out:
7709 -- - Save_Scope_Stack was modified to store in an Elist1 all those
7710 -- entities whose attribute Is_Immediately_Visible is modified
7711 -- from True to False.
7713 -- - Restore_Scope_Stack was modified to store in another Elist2
7714 -- all the entities whose attribute Is_Immediately_Visible is
7715 -- modified from False to True.
7717 -- - Extra code was added to verify that all the elements of Elist1
7718 -- are found in Elist2
7720 -- This test shows that there may be more occurrences of this problem which
7721 -- have not yet been detected. As a result, we replaced that approach by
7722 -- the current one in which Save_Scope_Stack returns the list of entities
7723 -- whose visibility is changed, and that list is passed to Restore_Scope_
7724 -- Stack to undo that change. This approach is simpler and safer, although
7725 -- it consumes more memory.
7727 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
7728 Result : constant Elist_Id := New_Elmt_List;
7729 E : Entity_Id;
7730 S : Entity_Id;
7731 SS_Last : constant Int := Scope_Stack.Last;
7733 procedure Remove_From_Visibility (E : Entity_Id);
7734 -- If E is immediately visible then append it to the result and remove
7735 -- it temporarily from visibility.
7737 ----------------------------
7738 -- Remove_From_Visibility --
7739 ----------------------------
7741 procedure Remove_From_Visibility (E : Entity_Id) is
7742 begin
7743 if Is_Immediately_Visible (E) then
7744 Append_Elmt (E, Result);
7745 Set_Is_Immediately_Visible (E, False);
7746 end if;
7747 end Remove_From_Visibility;
7749 -- Start of processing for Save_Scope_Stack
7751 begin
7752 if SS_Last >= Scope_Stack.First
7753 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
7754 then
7755 if Handle_Use then
7756 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
7757 end if;
7759 -- If the call is from within a compilation unit, as when called from
7760 -- Rtsfind, make current entries in scope stack invisible while we
7761 -- analyze the new unit.
7763 for J in reverse 0 .. SS_Last loop
7764 exit when Scope_Stack.Table (J).Entity = Standard_Standard
7765 or else No (Scope_Stack.Table (J).Entity);
7767 S := Scope_Stack.Table (J).Entity;
7769 Remove_From_Visibility (S);
7771 E := First_Entity (S);
7772 while Present (E) loop
7773 Remove_From_Visibility (E);
7774 Next_Entity (E);
7775 end loop;
7776 end loop;
7778 end if;
7780 return Result;
7781 end Save_Scope_Stack;
7783 -------------
7784 -- Set_Use --
7785 -------------
7787 procedure Set_Use (L : List_Id) is
7788 Decl : Node_Id;
7789 Pack_Name : Node_Id;
7790 Pack : Entity_Id;
7791 Id : Entity_Id;
7793 begin
7794 if Present (L) then
7795 Decl := First (L);
7796 while Present (Decl) loop
7797 if Nkind (Decl) = N_Use_Package_Clause then
7798 Chain_Use_Clause (Decl);
7800 Pack_Name := First (Names (Decl));
7801 while Present (Pack_Name) loop
7802 Pack := Entity (Pack_Name);
7804 if Ekind (Pack) = E_Package
7805 and then Applicable_Use (Pack_Name)
7806 then
7807 Use_One_Package (Pack, Decl);
7808 end if;
7810 Next (Pack_Name);
7811 end loop;
7813 elsif Nkind (Decl) = N_Use_Type_Clause then
7814 Chain_Use_Clause (Decl);
7816 Id := First (Subtype_Marks (Decl));
7817 while Present (Id) loop
7818 if Entity (Id) /= Any_Type then
7819 Use_One_Type (Id);
7820 end if;
7822 Next (Id);
7823 end loop;
7824 end if;
7826 Next (Decl);
7827 end loop;
7828 end if;
7829 end Set_Use;
7831 ---------------------
7832 -- Use_One_Package --
7833 ---------------------
7835 procedure Use_One_Package (P : Entity_Id; N : Node_Id) is
7836 Id : Entity_Id;
7837 Prev : Entity_Id;
7838 Current_Instance : Entity_Id := Empty;
7839 Real_P : Entity_Id;
7840 Private_With_OK : Boolean := False;
7842 begin
7843 if Ekind (P) /= E_Package then
7844 return;
7845 end if;
7847 Set_In_Use (P);
7848 Set_Current_Use_Clause (P, N);
7850 -- Ada 2005 (AI-50217): Check restriction
7852 if From_With_Type (P) then
7853 Error_Msg_N ("limited withed package cannot appear in use clause", N);
7854 end if;
7856 -- Find enclosing instance, if any
7858 if In_Instance then
7859 Current_Instance := Current_Scope;
7860 while not Is_Generic_Instance (Current_Instance) loop
7861 Current_Instance := Scope (Current_Instance);
7862 end loop;
7864 if No (Hidden_By_Use_Clause (N)) then
7865 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
7866 end if;
7867 end if;
7869 -- If unit is a package renaming, indicate that the renamed
7870 -- package is also in use (the flags on both entities must
7871 -- remain consistent, and a subsequent use of either of them
7872 -- should be recognized as redundant).
7874 if Present (Renamed_Object (P)) then
7875 Set_In_Use (Renamed_Object (P));
7876 Set_Current_Use_Clause (Renamed_Object (P), N);
7877 Real_P := Renamed_Object (P);
7878 else
7879 Real_P := P;
7880 end if;
7882 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
7883 -- found in the private part of a package specification
7885 if In_Private_Part (Current_Scope)
7886 and then Has_Private_With (P)
7887 and then Is_Child_Unit (Current_Scope)
7888 and then Is_Child_Unit (P)
7889 and then Is_Ancestor_Package (Scope (Current_Scope), P)
7890 then
7891 Private_With_OK := True;
7892 end if;
7894 -- Loop through entities in one package making them potentially
7895 -- use-visible.
7897 Id := First_Entity (P);
7898 while Present (Id)
7899 and then (Id /= First_Private_Entity (P)
7900 or else Private_With_OK) -- Ada 2005 (AI-262)
7901 loop
7902 Prev := Current_Entity (Id);
7903 while Present (Prev) loop
7904 if Is_Immediately_Visible (Prev)
7905 and then (not Is_Overloadable (Prev)
7906 or else not Is_Overloadable (Id)
7907 or else (Type_Conformant (Id, Prev)))
7908 then
7909 if No (Current_Instance) then
7911 -- Potentially use-visible entity remains hidden
7913 goto Next_Usable_Entity;
7915 -- A use clause within an instance hides outer global entities,
7916 -- which are not used to resolve local entities in the
7917 -- instance. Note that the predefined entities in Standard
7918 -- could not have been hidden in the generic by a use clause,
7919 -- and therefore remain visible. Other compilation units whose
7920 -- entities appear in Standard must be hidden in an instance.
7922 -- To determine whether an entity is external to the instance
7923 -- we compare the scope depth of its scope with that of the
7924 -- current instance. However, a generic actual of a subprogram
7925 -- instance is declared in the wrapper package but will not be
7926 -- hidden by a use-visible entity. similarly, an entity that is
7927 -- declared in an enclosing instance will not be hidden by an
7928 -- an entity declared in a generic actual, which can only have
7929 -- been use-visible in the generic and will not have hidden the
7930 -- entity in the generic parent.
7932 -- If Id is called Standard, the predefined package with the
7933 -- same name is in the homonym chain. It has to be ignored
7934 -- because it has no defined scope (being the only entity in
7935 -- the system with this mandated behavior).
7937 elsif not Is_Hidden (Id)
7938 and then Present (Scope (Prev))
7939 and then not Is_Wrapper_Package (Scope (Prev))
7940 and then Scope_Depth (Scope (Prev)) <
7941 Scope_Depth (Current_Instance)
7942 and then (Scope (Prev) /= Standard_Standard
7943 or else Sloc (Prev) > Standard_Location)
7944 then
7945 if In_Open_Scopes (Scope (Prev))
7946 and then Is_Generic_Instance (Scope (Prev))
7947 and then Present (Associated_Formal_Package (P))
7948 then
7949 null;
7951 else
7952 Set_Is_Potentially_Use_Visible (Id);
7953 Set_Is_Immediately_Visible (Prev, False);
7954 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
7955 end if;
7956 end if;
7958 -- A user-defined operator is not use-visible if the predefined
7959 -- operator for the type is immediately visible, which is the case
7960 -- if the type of the operand is in an open scope. This does not
7961 -- apply to user-defined operators that have operands of different
7962 -- types, because the predefined mixed mode operations (multiply
7963 -- and divide) apply to universal types and do not hide anything.
7965 elsif Ekind (Prev) = E_Operator
7966 and then Operator_Matches_Spec (Prev, Id)
7967 and then In_Open_Scopes
7968 (Scope (Base_Type (Etype (First_Formal (Id)))))
7969 and then (No (Next_Formal (First_Formal (Id)))
7970 or else Etype (First_Formal (Id)) =
7971 Etype (Next_Formal (First_Formal (Id)))
7972 or else Chars (Prev) = Name_Op_Expon)
7973 then
7974 goto Next_Usable_Entity;
7976 -- In an instance, two homonyms may become use_visible through the
7977 -- actuals of distinct formal packages. In the generic, only the
7978 -- current one would have been visible, so make the other one
7979 -- not use_visible.
7981 elsif Present (Current_Instance)
7982 and then Is_Potentially_Use_Visible (Prev)
7983 and then not Is_Overloadable (Prev)
7984 and then Scope (Id) /= Scope (Prev)
7985 and then Used_As_Generic_Actual (Scope (Prev))
7986 and then Used_As_Generic_Actual (Scope (Id))
7987 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
7988 Current_Use_Clause (Scope (Id)))
7989 then
7990 Set_Is_Potentially_Use_Visible (Prev, False);
7991 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
7992 end if;
7994 Prev := Homonym (Prev);
7995 end loop;
7997 -- On exit, we know entity is not hidden, unless it is private
7999 if not Is_Hidden (Id)
8000 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
8001 then
8002 Set_Is_Potentially_Use_Visible (Id);
8004 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
8005 Set_Is_Potentially_Use_Visible (Full_View (Id));
8006 end if;
8007 end if;
8009 <<Next_Usable_Entity>>
8010 Next_Entity (Id);
8011 end loop;
8013 -- Child units are also made use-visible by a use clause, but they may
8014 -- appear after all visible declarations in the parent entity list.
8016 while Present (Id) loop
8017 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
8018 Set_Is_Potentially_Use_Visible (Id);
8019 end if;
8021 Next_Entity (Id);
8022 end loop;
8024 if Chars (Real_P) = Name_System
8025 and then Scope (Real_P) = Standard_Standard
8026 and then Present_System_Aux (N)
8027 then
8028 Use_One_Package (System_Aux_Id, N);
8029 end if;
8031 end Use_One_Package;
8033 ------------------
8034 -- Use_One_Type --
8035 ------------------
8037 procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False) is
8038 Elmt : Elmt_Id;
8039 Is_Known_Used : Boolean;
8040 Op_List : Elist_Id;
8041 T : Entity_Id;
8043 function Spec_Reloaded_For_Body return Boolean;
8044 -- Determine whether the compilation unit is a package body and the use
8045 -- type clause is in the spec of the same package. Even though the spec
8046 -- was analyzed first, its context is reloaded when analysing the body.
8048 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
8049 -- AI05-150: if the use_type_clause carries the "all" qualifier,
8050 -- class-wide operations of ancestor types are use-visible if the
8051 -- ancestor type is visible.
8053 ----------------------------
8054 -- Spec_Reloaded_For_Body --
8055 ----------------------------
8057 function Spec_Reloaded_For_Body return Boolean is
8058 begin
8059 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
8060 declare
8061 Spec : constant Node_Id :=
8062 Parent (List_Containing (Parent (Id)));
8064 begin
8065 -- Check whether type is declared in a package specification,
8066 -- and current unit is the corresponding package body. The
8067 -- use clauses themselves may be within a nested package.
8069 return
8070 Nkind (Spec) = N_Package_Specification
8071 and then
8072 In_Same_Source_Unit (Corresponding_Body (Parent (Spec)),
8073 Cunit_Entity (Current_Sem_Unit));
8074 end;
8075 end if;
8077 return False;
8078 end Spec_Reloaded_For_Body;
8080 -------------------------------
8081 -- Use_Class_Wide_Operations --
8082 -------------------------------
8084 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
8085 Scop : Entity_Id;
8086 Ent : Entity_Id;
8088 function Is_Class_Wide_Operation_Of
8089 (Op : Entity_Id;
8090 T : Entity_Id) return Boolean;
8091 -- Determine whether a subprogram has a class-wide parameter or
8092 -- result that is T'Class.
8094 ---------------------------------
8095 -- Is_Class_Wide_Operation_Of --
8096 ---------------------------------
8098 function Is_Class_Wide_Operation_Of
8099 (Op : Entity_Id;
8100 T : Entity_Id) return Boolean
8102 Formal : Entity_Id;
8104 begin
8105 Formal := First_Formal (Op);
8106 while Present (Formal) loop
8107 if Etype (Formal) = Class_Wide_Type (T) then
8108 return True;
8109 end if;
8110 Next_Formal (Formal);
8111 end loop;
8113 if Etype (Op) = Class_Wide_Type (T) then
8114 return True;
8115 end if;
8117 return False;
8118 end Is_Class_Wide_Operation_Of;
8120 -- Start of processing for Use_Class_Wide_Operations
8122 begin
8123 Scop := Scope (Typ);
8124 if not Is_Hidden (Scop) then
8125 Ent := First_Entity (Scop);
8126 while Present (Ent) loop
8127 if Is_Overloadable (Ent)
8128 and then Is_Class_Wide_Operation_Of (Ent, Typ)
8129 and then not Is_Potentially_Use_Visible (Ent)
8130 then
8131 Set_Is_Potentially_Use_Visible (Ent);
8132 Append_Elmt (Ent, Used_Operations (Parent (Id)));
8133 end if;
8135 Next_Entity (Ent);
8136 end loop;
8137 end if;
8139 if Is_Derived_Type (Typ) then
8140 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
8141 end if;
8142 end Use_Class_Wide_Operations;
8144 -- Start of processing for Use_One_Type
8146 begin
8147 -- It is the type determined by the subtype mark (8.4(8)) whose
8148 -- operations become potentially use-visible.
8150 T := Base_Type (Entity (Id));
8152 -- Either the type itself is used, the package where it is declared
8153 -- is in use or the entity is declared in the current package, thus
8154 -- use-visible.
8156 Is_Known_Used :=
8157 In_Use (T)
8158 or else In_Use (Scope (T))
8159 or else Scope (T) = Current_Scope;
8161 Set_Redundant_Use (Id,
8162 Is_Known_Used or else Is_Potentially_Use_Visible (T));
8164 if Ekind (T) = E_Incomplete_Type then
8165 Error_Msg_N ("premature usage of incomplete type", Id);
8167 elsif In_Open_Scopes (Scope (T)) then
8168 null;
8170 -- A limited view cannot appear in a use_type clause. However, an access
8171 -- type whose designated type is limited has the flag but is not itself
8172 -- a limited view unless we only have a limited view of its enclosing
8173 -- package.
8175 elsif From_With_Type (T) and then From_With_Type (Scope (T)) then
8176 Error_Msg_N
8177 ("incomplete type from limited view "
8178 & "cannot appear in use clause", Id);
8180 -- If the subtype mark designates a subtype in a different package,
8181 -- we have to check that the parent type is visible, otherwise the
8182 -- use type clause is a noop. Not clear how to do that???
8184 elsif not Redundant_Use (Id) then
8185 Set_In_Use (T);
8187 -- If T is tagged, primitive operators on class-wide operands
8188 -- are also available.
8190 if Is_Tagged_Type (T) then
8191 Set_In_Use (Class_Wide_Type (T));
8192 end if;
8194 Set_Current_Use_Clause (T, Parent (Id));
8196 -- Iterate over primitive operations of the type. If an operation is
8197 -- already use_visible, it is the result of a previous use_clause,
8198 -- and already appears on the corresponding entity chain. If the
8199 -- clause is being reinstalled, operations are already use-visible.
8201 if Installed then
8202 null;
8204 else
8205 Op_List := Collect_Primitive_Operations (T);
8206 Elmt := First_Elmt (Op_List);
8207 while Present (Elmt) loop
8208 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
8209 or else Chars (Node (Elmt)) in Any_Operator_Name)
8210 and then not Is_Hidden (Node (Elmt))
8211 and then not Is_Potentially_Use_Visible (Node (Elmt))
8212 then
8213 Set_Is_Potentially_Use_Visible (Node (Elmt));
8214 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
8216 elsif Ada_Version >= Ada_2012
8217 and then All_Present (Parent (Id))
8218 and then not Is_Hidden (Node (Elmt))
8219 and then not Is_Potentially_Use_Visible (Node (Elmt))
8220 then
8221 Set_Is_Potentially_Use_Visible (Node (Elmt));
8222 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
8223 end if;
8225 Next_Elmt (Elmt);
8226 end loop;
8227 end if;
8229 if Ada_Version >= Ada_2012
8230 and then All_Present (Parent (Id))
8231 and then Is_Tagged_Type (T)
8232 then
8233 Use_Class_Wide_Operations (T);
8234 end if;
8235 end if;
8237 -- If warning on redundant constructs, check for unnecessary WITH
8239 if Warn_On_Redundant_Constructs
8240 and then Is_Known_Used
8242 -- with P; with P; use P;
8243 -- package P is package X is package body X is
8244 -- type T ... use P.T;
8246 -- The compilation unit is the body of X. GNAT first compiles the
8247 -- spec of X, then proceeds to the body. At that point P is marked
8248 -- as use visible. The analysis then reinstalls the spec along with
8249 -- its context. The use clause P.T is now recognized as redundant,
8250 -- but in the wrong context. Do not emit a warning in such cases.
8251 -- Do not emit a warning either if we are in an instance, there is
8252 -- no redundancy between an outer use_clause and one that appears
8253 -- within the generic.
8255 and then not Spec_Reloaded_For_Body
8256 and then not In_Instance
8257 then
8258 -- The type already has a use clause
8260 if In_Use (T) then
8262 -- Case where we know the current use clause for the type
8264 if Present (Current_Use_Clause (T)) then
8265 Use_Clause_Known : declare
8266 Clause1 : constant Node_Id := Parent (Id);
8267 Clause2 : constant Node_Id := Current_Use_Clause (T);
8268 Ent1 : Entity_Id;
8269 Ent2 : Entity_Id;
8270 Err_No : Node_Id;
8271 Unit1 : Node_Id;
8272 Unit2 : Node_Id;
8274 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
8275 -- Return the appropriate entity for determining which unit
8276 -- has a deeper scope: the defining entity for U, unless U
8277 -- is a package instance, in which case we retrieve the
8278 -- entity of the instance spec.
8280 --------------------
8281 -- Entity_Of_Unit --
8282 --------------------
8284 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
8285 begin
8286 if Nkind (U) = N_Package_Instantiation
8287 and then Analyzed (U)
8288 then
8289 return Defining_Entity (Instance_Spec (U));
8290 else
8291 return Defining_Entity (U);
8292 end if;
8293 end Entity_Of_Unit;
8295 -- Start of processing for Use_Clause_Known
8297 begin
8298 -- If both current use type clause and the use type clause
8299 -- for the type are at the compilation unit level, one of
8300 -- the units must be an ancestor of the other, and the
8301 -- warning belongs on the descendant.
8303 if Nkind (Parent (Clause1)) = N_Compilation_Unit
8304 and then
8305 Nkind (Parent (Clause2)) = N_Compilation_Unit
8306 then
8307 -- If the unit is a subprogram body that acts as spec,
8308 -- the context clause is shared with the constructed
8309 -- subprogram spec. Clearly there is no redundancy.
8311 if Clause1 = Clause2 then
8312 return;
8313 end if;
8315 Unit1 := Unit (Parent (Clause1));
8316 Unit2 := Unit (Parent (Clause2));
8318 -- If both clauses are on same unit, or one is the body
8319 -- of the other, or one of them is in a subunit, report
8320 -- redundancy on the later one.
8322 if Unit1 = Unit2 then
8323 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8324 Error_Msg_NE -- CODEFIX
8325 ("& is already use-visible through previous "
8326 & "use_type_clause #??", Clause1, T);
8327 return;
8329 elsif Nkind (Unit1) = N_Subunit then
8330 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8331 Error_Msg_NE -- CODEFIX
8332 ("& is already use-visible through previous "
8333 & "use_type_clause #??", Clause1, T);
8334 return;
8336 elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body)
8337 and then Nkind (Unit1) /= Nkind (Unit2)
8338 and then Nkind (Unit1) /= N_Subunit
8339 then
8340 Error_Msg_Sloc := Sloc (Clause1);
8341 Error_Msg_NE -- CODEFIX
8342 ("& is already use-visible through previous "
8343 & "use_type_clause #??", Current_Use_Clause (T), T);
8344 return;
8345 end if;
8347 -- There is a redundant use type clause in a child unit.
8348 -- Determine which of the units is more deeply nested.
8349 -- If a unit is a package instance, retrieve the entity
8350 -- and its scope from the instance spec.
8352 Ent1 := Entity_Of_Unit (Unit1);
8353 Ent2 := Entity_Of_Unit (Unit2);
8355 if Scope (Ent2) = Standard_Standard then
8356 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8357 Err_No := Clause1;
8359 elsif Scope (Ent1) = Standard_Standard then
8360 Error_Msg_Sloc := Sloc (Id);
8361 Err_No := Clause2;
8363 -- If both units are child units, we determine which one
8364 -- is the descendant by the scope distance to the
8365 -- ultimate parent unit.
8367 else
8368 declare
8369 S1, S2 : Entity_Id;
8371 begin
8372 S1 := Scope (Ent1);
8373 S2 := Scope (Ent2);
8374 while Present (S1)
8375 and then Present (S2)
8376 and then S1 /= Standard_Standard
8377 and then S2 /= Standard_Standard
8378 loop
8379 S1 := Scope (S1);
8380 S2 := Scope (S2);
8381 end loop;
8383 if S1 = Standard_Standard then
8384 Error_Msg_Sloc := Sloc (Id);
8385 Err_No := Clause2;
8386 else
8387 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8388 Err_No := Clause1;
8389 end if;
8390 end;
8391 end if;
8393 Error_Msg_NE -- CODEFIX
8394 ("& is already use-visible through previous "
8395 & "use_type_clause #??", Err_No, Id);
8397 -- Case where current use type clause and the use type
8398 -- clause for the type are not both at the compilation unit
8399 -- level. In this case we don't have location information.
8401 else
8402 Error_Msg_NE -- CODEFIX
8403 ("& is already use-visible through previous "
8404 & "use type clause??", Id, T);
8405 end if;
8406 end Use_Clause_Known;
8408 -- Here if Current_Use_Clause is not set for T, another case
8409 -- where we do not have the location information available.
8411 else
8412 Error_Msg_NE -- CODEFIX
8413 ("& is already use-visible through previous "
8414 & "use type clause??", Id, T);
8415 end if;
8417 -- The package where T is declared is already used
8419 elsif In_Use (Scope (T)) then
8420 Error_Msg_Sloc := Sloc (Current_Use_Clause (Scope (T)));
8421 Error_Msg_NE -- CODEFIX
8422 ("& is already use-visible through package use clause #??",
8423 Id, T);
8425 -- The current scope is the package where T is declared
8427 else
8428 Error_Msg_Node_2 := Scope (T);
8429 Error_Msg_NE -- CODEFIX
8430 ("& is already use-visible inside package &??", Id, T);
8431 end if;
8432 end if;
8433 end Use_One_Type;
8435 ----------------
8436 -- Write_Info --
8437 ----------------
8439 procedure Write_Info is
8440 Id : Entity_Id := First_Entity (Current_Scope);
8442 begin
8443 -- No point in dumping standard entities
8445 if Current_Scope = Standard_Standard then
8446 return;
8447 end if;
8449 Write_Str ("========================================================");
8450 Write_Eol;
8451 Write_Str (" Defined Entities in ");
8452 Write_Name (Chars (Current_Scope));
8453 Write_Eol;
8454 Write_Str ("========================================================");
8455 Write_Eol;
8457 if No (Id) then
8458 Write_Str ("-- none --");
8459 Write_Eol;
8461 else
8462 while Present (Id) loop
8463 Write_Entity_Info (Id, " ");
8464 Next_Entity (Id);
8465 end loop;
8466 end if;
8468 if Scope (Current_Scope) = Standard_Standard then
8470 -- Print information on the current unit itself
8472 Write_Entity_Info (Current_Scope, " ");
8473 end if;
8475 Write_Eol;
8476 end Write_Info;
8478 --------
8479 -- ws --
8480 --------
8482 procedure ws is
8483 S : Entity_Id;
8484 begin
8485 for J in reverse 1 .. Scope_Stack.Last loop
8486 S := Scope_Stack.Table (J).Entity;
8487 Write_Int (Int (S));
8488 Write_Str (" === ");
8489 Write_Name (Chars (S));
8490 Write_Eol;
8491 end loop;
8492 end ws;
8494 --------
8495 -- we --
8496 --------
8498 procedure we (S : Entity_Id) is
8499 E : Entity_Id;
8500 begin
8501 E := First_Entity (S);
8502 while Present (E) loop
8503 Write_Int (Int (E));
8504 Write_Str (" === ");
8505 Write_Name (Chars (E));
8506 Write_Eol;
8507 Next_Entity (E);
8508 end loop;
8509 end we;
8510 end Sem_Ch8;