2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / sem_ch8.adb
blobee76eda0fced58a065fa4eed31f3df44f5382123
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-2015, 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_Disp; use Exp_Disp;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Fname; use Fname;
35 with Freeze; use Freeze;
36 with Ghost; use Ghost;
37 with Impunit; use Impunit;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Namet; use Namet;
42 with Namet.Sp; use Namet.Sp;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Output; use Output;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch4; use Sem_Ch4;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch12; use Sem_Ch12;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Dim; use Sem_Dim;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Res; use Sem_Res;
63 with Sem_Util; use Sem_Util;
64 with Sem_Type; use Sem_Type;
65 with Stand; use Stand;
66 with Sinfo; use Sinfo;
67 with Sinfo.CN; use Sinfo.CN;
68 with Snames; use Snames;
69 with Style; use Style;
70 with Table;
71 with Targparm; use Targparm;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
75 package body Sem_Ch8 is
77 ------------------------------------
78 -- Visibility and Name Resolution --
79 ------------------------------------
81 -- This package handles name resolution and the collection of possible
82 -- interpretations for overloaded names, prior to overload resolution.
84 -- Name resolution is the process that establishes a mapping between source
85 -- identifiers and the entities they denote at each point in the program.
86 -- Each entity is represented by a defining occurrence. Each identifier
87 -- that denotes an entity points to the corresponding defining occurrence.
88 -- This is the entity of the applied occurrence. Each occurrence holds
89 -- an index into the names table, where source identifiers are stored.
91 -- Each entry in the names table for an identifier or designator uses the
92 -- Info pointer to hold a link to the currently visible entity that has
93 -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
94 -- in package Sem_Util). The visibility is initialized at the beginning of
95 -- semantic processing to make entities in package Standard immediately
96 -- visible. The visibility table is used in a more subtle way when
97 -- compiling subunits (see below).
99 -- Entities that have the same name (i.e. homonyms) are chained. In the
100 -- case of overloaded entities, this chain holds all the possible meanings
101 -- of a given identifier. The process of overload resolution uses type
102 -- information to select from this chain the unique meaning of a given
103 -- identifier.
105 -- Entities are also chained in their scope, through the Next_Entity link.
106 -- As a consequence, the name space is organized as a sparse matrix, where
107 -- each row corresponds to a scope, and each column to a source identifier.
108 -- Open scopes, that is to say scopes currently being compiled, have their
109 -- corresponding rows of entities in order, innermost scope first.
111 -- The scopes of packages that are mentioned in context clauses appear in
112 -- no particular order, interspersed among open scopes. This is because
113 -- in the course of analyzing the context of a compilation, a package
114 -- declaration is first an open scope, and subsequently an element of the
115 -- context. If subunits or child units are present, a parent unit may
116 -- appear under various guises at various times in the compilation.
118 -- When the compilation of the innermost scope is complete, the entities
119 -- defined therein are no longer visible. If the scope is not a package
120 -- declaration, these entities are never visible subsequently, and can be
121 -- removed from visibility chains. If the scope is a package declaration,
122 -- its visible declarations may still be accessible. Therefore the entities
123 -- defined in such a scope are left on the visibility chains, and only
124 -- their visibility (immediately visibility or potential use-visibility)
125 -- is affected.
127 -- The ordering of homonyms on their chain does not necessarily follow
128 -- the order of their corresponding scopes on the scope stack. For
129 -- example, if package P and the enclosing scope both contain entities
130 -- named E, then when compiling the package body the chain for E will
131 -- hold the global entity first, and the local one (corresponding to
132 -- the current inner scope) next. As a result, name resolution routines
133 -- do not assume any relative ordering of the homonym chains, either
134 -- for scope nesting or to order of appearance of context clauses.
136 -- When compiling a child unit, entities in the parent scope are always
137 -- immediately visible. When compiling the body of a child unit, private
138 -- entities in the parent must also be made immediately visible. There
139 -- are separate routines to make the visible and private declarations
140 -- visible at various times (see package Sem_Ch7).
142 -- +--------+ +-----+
143 -- | In use |-------->| EU1 |-------------------------->
144 -- +--------+ +-----+
145 -- | |
146 -- +--------+ +-----+ +-----+
147 -- | Stand. |---------------->| ES1 |--------------->| ES2 |--->
148 -- +--------+ +-----+ +-----+
149 -- | |
150 -- +---------+ | +-----+
151 -- | with'ed |------------------------------>| EW2 |--->
152 -- +---------+ | +-----+
153 -- | |
154 -- +--------+ +-----+ +-----+
155 -- | Scope2 |---------------->| E12 |--------------->| E22 |--->
156 -- +--------+ +-----+ +-----+
157 -- | |
158 -- +--------+ +-----+ +-----+
159 -- | Scope1 |---------------->| E11 |--------------->| E12 |--->
160 -- +--------+ +-----+ +-----+
161 -- ^ | |
162 -- | | |
163 -- | +---------+ | |
164 -- | | with'ed |----------------------------------------->
165 -- | +---------+ | |
166 -- | | |
167 -- Scope stack | |
168 -- (innermost first) | |
169 -- +----------------------------+
170 -- Names table => | Id1 | | | | Id2 |
171 -- +----------------------------+
173 -- Name resolution must deal with several syntactic forms: simple names,
174 -- qualified names, indexed names, and various forms of calls.
176 -- Each identifier points to an entry in the names table. The resolution
177 -- of a simple name consists in traversing the homonym chain, starting
178 -- from the names table. If an entry is immediately visible, it is the one
179 -- designated by the identifier. If only potentially use-visible entities
180 -- are on the chain, we must verify that they do not hide each other. If
181 -- the entity we find is overloadable, we collect all other overloadable
182 -- entities on the chain as long as they are not hidden.
184 -- To resolve expanded names, we must find the entity at the intersection
185 -- of the entity chain for the scope (the prefix) and the homonym chain
186 -- for the selector. In general, homonym chains will be much shorter than
187 -- entity chains, so it is preferable to start from the names table as
188 -- well. If the entity found is overloadable, we must collect all other
189 -- interpretations that are defined in the scope denoted by the prefix.
191 -- For records, protected types, and tasks, their local entities are
192 -- removed from visibility chains on exit from the corresponding scope.
193 -- From the outside, these entities are always accessed by selected
194 -- notation, and the entity chain for the record type, protected type,
195 -- etc. is traversed sequentially in order to find the designated entity.
197 -- The discriminants of a type and the operations of a protected type or
198 -- task are unchained on exit from the first view of the type, (such as
199 -- a private or incomplete type declaration, or a protected type speci-
200 -- fication) and re-chained when compiling the second view.
202 -- In the case of operators, we do not make operators on derived types
203 -- explicit. As a result, the notation P."+" may denote either a user-
204 -- defined function with name "+", or else an implicit declaration of the
205 -- operator "+" in package P. The resolution of expanded names always
206 -- tries to resolve an operator name as such an implicitly defined entity,
207 -- in addition to looking for explicit declarations.
209 -- All forms of names that denote entities (simple names, expanded names,
210 -- character literals in some cases) have a Entity attribute, which
211 -- identifies the entity denoted by the name.
213 ---------------------
214 -- The Scope Stack --
215 ---------------------
217 -- The Scope stack keeps track of the scopes currently been compiled.
218 -- Every entity that contains declarations (including records) is placed
219 -- on the scope stack while it is being processed, and removed at the end.
220 -- Whenever a non-package scope is exited, the entities defined therein
221 -- are removed from the visibility table, so that entities in outer scopes
222 -- become visible (see previous description). On entry to Sem, the scope
223 -- stack only contains the package Standard. As usual, subunits complicate
224 -- this picture ever so slightly.
226 -- The Rtsfind mechanism can force a call to Semantics while another
227 -- compilation is in progress. The unit retrieved by Rtsfind must be
228 -- compiled in its own context, and has no access to the visibility of
229 -- the unit currently being compiled. The procedures Save_Scope_Stack and
230 -- Restore_Scope_Stack make entities in current open scopes invisible
231 -- before compiling the retrieved unit, and restore the compilation
232 -- environment afterwards.
234 ------------------------
235 -- Compiling subunits --
236 ------------------------
238 -- Subunits must be compiled in the environment of the corresponding stub,
239 -- that is to say with the same visibility into the parent (and its
240 -- context) that is available at the point of the stub declaration, but
241 -- with the additional visibility provided by the context clause of the
242 -- subunit itself. As a result, compilation of a subunit forces compilation
243 -- of the parent (see description in lib-). At the point of the stub
244 -- declaration, Analyze is called recursively to compile the proper body of
245 -- the subunit, but without reinitializing the names table, nor the scope
246 -- stack (i.e. standard is not pushed on the stack). In this fashion the
247 -- context of the subunit is added to the context of the parent, and the
248 -- subunit is compiled in the correct environment. Note that in the course
249 -- of processing the context of a subunit, Standard will appear twice on
250 -- the scope stack: once for the parent of the subunit, and once for the
251 -- unit in the context clause being compiled. However, the two sets of
252 -- entities are not linked by homonym chains, so that the compilation of
253 -- any context unit happens in a fresh visibility environment.
255 -------------------------------
256 -- Processing of USE Clauses --
257 -------------------------------
259 -- Every defining occurrence has a flag indicating if it is potentially use
260 -- visible. Resolution of simple names examines this flag. The processing
261 -- of use clauses consists in setting this flag on all visible entities
262 -- defined in the corresponding package. On exit from the scope of the use
263 -- clause, the corresponding flag must be reset. However, a package may
264 -- appear in several nested use clauses (pathological but legal, alas)
265 -- which forces us to use a slightly more involved scheme:
267 -- a) The defining occurrence for a package holds a flag -In_Use- to
268 -- indicate that it is currently in the scope of a use clause. If a
269 -- redundant use clause is encountered, then the corresponding occurrence
270 -- of the package name is flagged -Redundant_Use-.
272 -- b) On exit from a scope, the use clauses in its declarative part are
273 -- scanned. The visibility flag is reset in all entities declared in
274 -- package named in a use clause, as long as the package is not flagged
275 -- as being in a redundant use clause (in which case the outer use
276 -- clause is still in effect, and the direct visibility of its entities
277 -- must be retained).
279 -- Note that entities are not removed from their homonym chains on exit
280 -- from the package specification. A subsequent use clause does not need
281 -- to rechain the visible entities, but only to establish their direct
282 -- visibility.
284 -----------------------------------
285 -- Handling private declarations --
286 -----------------------------------
288 -- The principle that each entity has a single defining occurrence clashes
289 -- with the presence of two separate definitions for private types: the
290 -- first is the private type declaration, and second is the full type
291 -- declaration. It is important that all references to the type point to
292 -- the same defining occurrence, namely the first one. To enforce the two
293 -- separate views of the entity, the corresponding information is swapped
294 -- between the two declarations. Outside of the package, the defining
295 -- occurrence only contains the private declaration information, while in
296 -- the private part and the body of the package the defining occurrence
297 -- contains the full declaration. To simplify the swap, the defining
298 -- occurrence that currently holds the private declaration points to the
299 -- full declaration. During semantic processing the defining occurrence
300 -- also points to a list of private dependents, that is to say access types
301 -- or composite types whose designated types or component types are
302 -- subtypes or derived types of the private type in question. After the
303 -- full declaration has been seen, the private dependents are updated to
304 -- indicate that they have full definitions.
306 ------------------------------------
307 -- Handling of Undefined Messages --
308 ------------------------------------
310 -- In normal mode, only the first use of an undefined identifier generates
311 -- a message. The table Urefs is used to record error messages that have
312 -- been issued so that second and subsequent ones do not generate further
313 -- messages. However, the second reference causes text to be added to the
314 -- original undefined message noting "(more references follow)". The
315 -- full error list option (-gnatf) forces messages to be generated for
316 -- every reference and disconnects the use of this table.
318 type Uref_Entry is record
319 Node : Node_Id;
320 -- Node for identifier for which original message was posted. The
321 -- Chars field of this identifier is used to detect later references
322 -- to the same identifier.
324 Err : Error_Msg_Id;
325 -- Records error message Id of original undefined message. Reset to
326 -- No_Error_Msg after the second occurrence, where it is used to add
327 -- text to the original message as described above.
329 Nvis : Boolean;
330 -- Set if the message is not visible rather than undefined
332 Loc : Source_Ptr;
333 -- Records location of error message. Used to make sure that we do
334 -- not consider a, b : undefined as two separate instances, which
335 -- would otherwise happen, since the parser converts this sequence
336 -- to a : undefined; b : undefined.
338 end record;
340 package Urefs is new Table.Table (
341 Table_Component_Type => Uref_Entry,
342 Table_Index_Type => Nat,
343 Table_Low_Bound => 1,
344 Table_Initial => 10,
345 Table_Increment => 100,
346 Table_Name => "Urefs");
348 Candidate_Renaming : Entity_Id;
349 -- Holds a candidate interpretation that appears in a subprogram renaming
350 -- declaration and does not match the given specification, but matches at
351 -- least on the first formal. Allows better error message when given
352 -- specification omits defaulted parameters, a common error.
354 -----------------------
355 -- Local Subprograms --
356 -----------------------
358 procedure Analyze_Generic_Renaming
359 (N : Node_Id;
360 K : Entity_Kind);
361 -- Common processing for all three kinds of generic renaming declarations.
362 -- Enter new name and indicate that it renames the generic unit.
364 procedure Analyze_Renamed_Character
365 (N : Node_Id;
366 New_S : Entity_Id;
367 Is_Body : Boolean);
368 -- Renamed entity is given by a character literal, which must belong
369 -- to the return type of the new entity. Is_Body indicates whether the
370 -- declaration is a renaming_as_body. If the original declaration has
371 -- already been frozen (because of an intervening body, e.g.) the body of
372 -- the function must be built now. The same applies to the following
373 -- various renaming procedures.
375 procedure Analyze_Renamed_Dereference
376 (N : Node_Id;
377 New_S : Entity_Id;
378 Is_Body : Boolean);
379 -- Renamed entity is given by an explicit dereference. Prefix must be a
380 -- conformant access_to_subprogram type.
382 procedure Analyze_Renamed_Entry
383 (N : Node_Id;
384 New_S : Entity_Id;
385 Is_Body : Boolean);
386 -- If the renamed entity in a subprogram renaming is an entry or protected
387 -- subprogram, build a body for the new entity whose only statement is a
388 -- call to the renamed entity.
390 procedure Analyze_Renamed_Family_Member
391 (N : Node_Id;
392 New_S : Entity_Id;
393 Is_Body : Boolean);
394 -- Used when the renamed entity is an indexed component. The prefix must
395 -- denote an entry family.
397 procedure Analyze_Renamed_Primitive_Operation
398 (N : Node_Id;
399 New_S : Entity_Id;
400 Is_Body : Boolean);
401 -- If the renamed entity in a subprogram renaming is a primitive operation
402 -- or a class-wide operation in prefix form, save the target object,
403 -- which must be added to the list of actuals in any subsequent call.
404 -- The renaming operation is intrinsic because the compiler must in
405 -- fact generate a wrapper for it (6.3.1 (10 1/2)).
407 function Applicable_Use (Pack_Name : Node_Id) return Boolean;
408 -- Common code to Use_One_Package and Set_Use, to determine whether use
409 -- clause must be processed. Pack_Name is an entity name that references
410 -- the package in question.
412 procedure Attribute_Renaming (N : Node_Id);
413 -- Analyze renaming of attribute as subprogram. The renaming declaration N
414 -- is rewritten as a subprogram body that returns the attribute reference
415 -- applied to the formals of the function.
417 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
418 -- Set Entity, with style check if need be. For a discriminant reference,
419 -- replace by the corresponding discriminal, i.e. the parameter of the
420 -- initialization procedure that corresponds to the discriminant.
422 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
423 -- A renaming_as_body may occur after the entity of the original decla-
424 -- ration has been frozen. In that case, the body of the new entity must
425 -- be built now, because the usual mechanism of building the renamed
426 -- body at the point of freezing will not work. Subp is the subprogram
427 -- for which N provides the Renaming_As_Body.
429 procedure Check_In_Previous_With_Clause
430 (N : Node_Id;
431 Nam : Node_Id);
432 -- N is a use_package clause and Nam the package name, or N is a use_type
433 -- clause and Nam is the prefix of the type name. In either case, verify
434 -- that the package is visible at that point in the context: either it
435 -- appears in a previous with_clause, or because it is a fully qualified
436 -- name and the root ancestor appears in a previous with_clause.
438 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
439 -- Verify that the entity in a renaming declaration that is a library unit
440 -- is itself a library unit and not a nested unit or subunit. Also check
441 -- that if the renaming is a child unit of a generic parent, then the
442 -- renamed unit must also be a child unit of that parent. Finally, verify
443 -- that a renamed generic unit is not an implicit child declared within
444 -- an instance of the parent.
446 procedure Chain_Use_Clause (N : Node_Id);
447 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
448 -- the proper scope table entry. This is usually the current scope, but it
449 -- will be an inner scope when installing the use clauses of the private
450 -- declarations of a parent unit prior to compiling the private part of a
451 -- child unit. This chain is traversed when installing/removing use clauses
452 -- when compiling a subunit or instantiating a generic body on the fly,
453 -- when it is necessary to save and restore full environments.
455 function Enclosing_Instance return Entity_Id;
456 -- In an instance nested within another one, several semantic checks are
457 -- unnecessary because the legality of the nested instance has been checked
458 -- in the enclosing generic unit. This applies in particular to legality
459 -- checks on actuals for formal subprograms of the inner instance, which
460 -- are checked as subprogram renamings, and may be complicated by confusion
461 -- in private/full views. This function returns the instance enclosing the
462 -- current one if there is such, else it returns Empty.
464 -- If the renaming determines the entity for the default of a formal
465 -- subprogram nested within another instance, choose the innermost
466 -- candidate. This is because if the formal has a box, and we are within
467 -- an enclosing instance where some candidate interpretations are local
468 -- to this enclosing instance, we know that the default was properly
469 -- resolved when analyzing the generic, so we prefer the local
470 -- candidates to those that are external. This is not always the case
471 -- but is a reasonable heuristic on the use of nested generics. The
472 -- proper solution requires a full renaming model.
474 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
475 -- Find a type derived from Character or Wide_Character in the prefix of N.
476 -- Used to resolved qualified names whose selector is a character literal.
478 function Has_Private_With (E : Entity_Id) return Boolean;
479 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
480 -- private with on E.
482 procedure Find_Expanded_Name (N : Node_Id);
483 -- The input is a selected component known to be an expanded name. Verify
484 -- legality of selector given the scope denoted by prefix, and change node
485 -- N into a expanded name with a properly set Entity field.
487 function Find_Renamed_Entity
488 (N : Node_Id;
489 Nam : Node_Id;
490 New_S : Entity_Id;
491 Is_Actual : Boolean := False) return Entity_Id;
492 -- Find the renamed entity that corresponds to the given parameter profile
493 -- in a subprogram renaming declaration. The renamed entity may be an
494 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
495 -- indicates that the renaming is the one generated for an actual subpro-
496 -- gram in an instance, for which special visibility checks apply.
498 function Has_Implicit_Operator (N : Node_Id) return Boolean;
499 -- N is an expanded name whose selector is an operator name (e.g. P."+").
500 -- declarative part contains an implicit declaration of an operator if it
501 -- has a declaration of a type to which one of the predefined operators
502 -- apply. The existence of this routine is an implementation artifact. A
503 -- more straightforward but more space-consuming choice would be to make
504 -- all inherited operators explicit in the symbol table.
506 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
507 -- A subprogram defined by a renaming declaration inherits the parameter
508 -- profile of the renamed entity. The subtypes given in the subprogram
509 -- specification are discarded and replaced with those of the renamed
510 -- subprogram, which are then used to recheck the default values.
512 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean;
513 -- Prefix is appropriate for record if it is of a record type, or an access
514 -- to such.
516 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean;
517 -- True if it is of a task type, a protected type, or else an access to one
518 -- of these types.
520 procedure Note_Redundant_Use (Clause : Node_Id);
521 -- Mark the name in a use clause as redundant if the corresponding entity
522 -- is already use-visible. Emit a warning if the use clause comes from
523 -- source and the proper warnings are enabled.
525 procedure Premature_Usage (N : Node_Id);
526 -- Diagnose usage of an entity before it is visible
528 procedure Use_One_Package (P : Entity_Id; N : Node_Id);
529 -- Make visible entities declared in package P potentially use-visible
530 -- in the current context. Also used in the analysis of subunits, when
531 -- re-installing use clauses of parent units. N is the use_clause that
532 -- names P (and possibly other packages).
534 procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False);
535 -- Id is the subtype mark from a use type clause. This procedure makes
536 -- the primitive operators of the type potentially use-visible. The
537 -- boolean flag Installed indicates that the clause is being reinstalled
538 -- after previous analysis, and primitive operations are already chained
539 -- on the Used_Operations list of the clause.
541 procedure Write_Info;
542 -- Write debugging information on entities declared in current scope
544 --------------------------------
545 -- Analyze_Exception_Renaming --
546 --------------------------------
548 -- The language only allows a single identifier, but the tree holds an
549 -- identifier list. The parser has already issued an error message if
550 -- there is more than one element in the list.
552 procedure Analyze_Exception_Renaming (N : Node_Id) is
553 GM : constant Ghost_Mode_Type := Ghost_Mode;
554 Id : constant Entity_Id := Defining_Entity (N);
555 Nam : constant Node_Id := Name (N);
557 begin
558 -- The exception renaming declaration may be subject to pragma Ghost
559 -- with policy Ignore. Set the mode now to ensure that any nodes
560 -- generated during analysis and expansion are properly flagged as
561 -- ignored Ghost.
563 Set_Ghost_Mode (N);
564 Check_SPARK_05_Restriction ("exception renaming is not allowed", N);
566 Enter_Name (Id);
567 Analyze (Nam);
569 Set_Ekind (Id, E_Exception);
570 Set_Etype (Id, Standard_Exception_Type);
571 Set_Is_Pure (Id, Is_Pure (Current_Scope));
573 if Is_Entity_Name (Nam)
574 and then Present (Entity (Nam))
575 and then Ekind (Entity (Nam)) = E_Exception
576 then
577 if Present (Renamed_Object (Entity (Nam))) then
578 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
579 else
580 Set_Renamed_Object (Id, Entity (Nam));
581 end if;
583 -- The exception renaming declaration may become Ghost if it renames
584 -- a Ghost entity.
586 Mark_Renaming_As_Ghost (N, Entity (Nam));
587 else
588 Error_Msg_N ("invalid exception name in renaming", Nam);
589 end if;
591 -- Implementation-defined aspect specifications can appear in a renaming
592 -- declaration, but not language-defined ones. The call to procedure
593 -- Analyze_Aspect_Specifications will take care of this error check.
595 if Has_Aspects (N) then
596 Analyze_Aspect_Specifications (N, Id);
597 end if;
599 -- Restore the original Ghost mode once analysis and expansion have
600 -- taken place.
602 Ghost_Mode := GM;
603 end Analyze_Exception_Renaming;
605 ---------------------------
606 -- Analyze_Expanded_Name --
607 ---------------------------
609 procedure Analyze_Expanded_Name (N : Node_Id) is
610 begin
611 -- If the entity pointer is already set, this is an internal node, or a
612 -- node that is analyzed more than once, after a tree modification. In
613 -- such a case there is no resolution to perform, just set the type. For
614 -- completeness, analyze prefix as well.
616 if Present (Entity (N)) then
617 if Is_Type (Entity (N)) then
618 Set_Etype (N, Entity (N));
619 else
620 Set_Etype (N, Etype (Entity (N)));
621 end if;
623 Analyze (Prefix (N));
624 return;
625 else
626 Find_Expanded_Name (N);
627 end if;
629 Analyze_Dimension (N);
630 end Analyze_Expanded_Name;
632 ---------------------------------------
633 -- Analyze_Generic_Function_Renaming --
634 ---------------------------------------
636 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
637 begin
638 Analyze_Generic_Renaming (N, E_Generic_Function);
639 end Analyze_Generic_Function_Renaming;
641 --------------------------------------
642 -- Analyze_Generic_Package_Renaming --
643 --------------------------------------
645 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
646 begin
647 -- Test for the Text_IO special unit case here, since we may be renaming
648 -- one of the subpackages of Text_IO, then join common routine.
650 Check_Text_IO_Special_Unit (Name (N));
652 Analyze_Generic_Renaming (N, E_Generic_Package);
653 end Analyze_Generic_Package_Renaming;
655 ----------------------------------------
656 -- Analyze_Generic_Procedure_Renaming --
657 ----------------------------------------
659 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
660 begin
661 Analyze_Generic_Renaming (N, E_Generic_Procedure);
662 end Analyze_Generic_Procedure_Renaming;
664 ------------------------------
665 -- Analyze_Generic_Renaming --
666 ------------------------------
668 procedure Analyze_Generic_Renaming
669 (N : Node_Id;
670 K : Entity_Kind)
672 GM : constant Ghost_Mode_Type := Ghost_Mode;
673 New_P : constant Entity_Id := Defining_Entity (N);
674 Old_P : Entity_Id;
676 Inst : Boolean := False;
677 -- Prevent junk warning
679 begin
680 if Name (N) = Error then
681 return;
682 end if;
684 -- The generic renaming declaration may be subject to pragma Ghost with
685 -- policy Ignore. Set the mode now to ensure that any nodes generated
686 -- during analysis and expansion are properly flagged as ignored Ghost.
688 Set_Ghost_Mode (N);
689 Check_SPARK_05_Restriction ("generic renaming is not allowed", N);
691 Generate_Definition (New_P);
693 if Current_Scope /= Standard_Standard then
694 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
695 end if;
697 if Nkind (Name (N)) = N_Selected_Component then
698 Check_Generic_Child_Unit (Name (N), Inst);
699 else
700 Analyze (Name (N));
701 end if;
703 if not Is_Entity_Name (Name (N)) then
704 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
705 Old_P := Any_Id;
706 else
707 Old_P := Entity (Name (N));
708 end if;
710 Enter_Name (New_P);
711 Set_Ekind (New_P, K);
713 if Etype (Old_P) = Any_Type then
714 null;
716 elsif Ekind (Old_P) /= K then
717 Error_Msg_N ("invalid generic unit name", Name (N));
719 else
720 if Present (Renamed_Object (Old_P)) then
721 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
722 else
723 Set_Renamed_Object (New_P, Old_P);
724 end if;
726 Set_Is_Pure (New_P, Is_Pure (Old_P));
727 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
729 Set_Etype (New_P, Etype (Old_P));
730 Set_Has_Completion (New_P);
732 -- The generic renaming declaration may become Ghost if it renames a
733 -- Ghost entity.
735 Mark_Renaming_As_Ghost (N, Old_P);
737 if In_Open_Scopes (Old_P) then
738 Error_Msg_N ("within its scope, generic denotes its instance", N);
739 end if;
741 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
742 -- renamings and subsequent instantiations of Unchecked_Conversion.
744 if Ekind_In (Old_P, E_Generic_Function, E_Generic_Procedure) then
745 Set_Is_Intrinsic_Subprogram
746 (New_P, Is_Intrinsic_Subprogram (Old_P));
747 end if;
749 Check_Library_Unit_Renaming (N, Old_P);
750 end if;
752 -- Implementation-defined aspect specifications can appear in a renaming
753 -- declaration, but not language-defined ones. The call to procedure
754 -- Analyze_Aspect_Specifications will take care of this error check.
756 if Has_Aspects (N) then
757 Analyze_Aspect_Specifications (N, New_P);
758 end if;
760 -- Restore the original Ghost mode once analysis and expansion have
761 -- taken place.
763 Ghost_Mode := GM;
764 end Analyze_Generic_Renaming;
766 -----------------------------
767 -- Analyze_Object_Renaming --
768 -----------------------------
770 procedure Analyze_Object_Renaming (N : Node_Id) is
771 Id : constant Entity_Id := Defining_Identifier (N);
772 Loc : constant Source_Ptr := Sloc (N);
773 Nam : constant Node_Id := Name (N);
774 Dec : Node_Id;
775 T : Entity_Id;
776 T2 : Entity_Id;
778 procedure Check_Constrained_Object;
779 -- If the nominal type is unconstrained but the renamed object is
780 -- constrained, as can happen with renaming an explicit dereference or
781 -- a function return, build a constrained subtype from the object. If
782 -- the renaming is for a formal in an accept statement, the analysis
783 -- has already established its actual subtype. This is only relevant
784 -- if the renamed object is an explicit dereference.
786 function In_Generic_Scope (E : Entity_Id) return Boolean;
787 -- Determine whether entity E is inside a generic cope
789 ------------------------------
790 -- Check_Constrained_Object --
791 ------------------------------
793 procedure Check_Constrained_Object is
794 Typ : constant Entity_Id := Etype (Nam);
795 Subt : Entity_Id;
797 begin
798 if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference)
799 and then Is_Composite_Type (Etype (Nam))
800 and then not Is_Constrained (Etype (Nam))
801 and then not Has_Unknown_Discriminants (Etype (Nam))
802 and then Expander_Active
803 then
804 -- If Actual_Subtype is already set, nothing to do
806 if Ekind_In (Id, E_Variable, E_Constant)
807 and then Present (Actual_Subtype (Id))
808 then
809 null;
811 -- A renaming of an unchecked union has no actual subtype
813 elsif Is_Unchecked_Union (Typ) then
814 null;
816 -- If a record is limited its size is invariant. This is the case
817 -- in particular with record types with an access discirminant
818 -- that are used in iterators. This is an optimization, but it
819 -- also prevents typing anomalies when the prefix is further
820 -- expanded. Limited types with discriminants are included.
822 elsif Is_Limited_Record (Typ)
823 or else
824 (Ekind (Typ) = E_Limited_Private_Type
825 and then Has_Discriminants (Typ)
826 and then Is_Access_Type (Etype (First_Discriminant (Typ))))
827 then
828 null;
830 else
831 Subt := Make_Temporary (Loc, 'T');
832 Remove_Side_Effects (Nam);
833 Insert_Action (N,
834 Make_Subtype_Declaration (Loc,
835 Defining_Identifier => Subt,
836 Subtype_Indication =>
837 Make_Subtype_From_Expr (Nam, Typ)));
838 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
839 Set_Etype (Nam, Subt);
841 -- Freeze subtype at once, to prevent order of elaboration
842 -- issues in the backend. The renamed object exists, so its
843 -- type is already frozen in any case.
845 Freeze_Before (N, Subt);
846 end if;
847 end if;
848 end Check_Constrained_Object;
850 ----------------------
851 -- In_Generic_Scope --
852 ----------------------
854 function In_Generic_Scope (E : Entity_Id) return Boolean is
855 S : Entity_Id;
857 begin
858 S := Scope (E);
859 while Present (S) and then S /= Standard_Standard loop
860 if Is_Generic_Unit (S) then
861 return True;
862 end if;
864 S := Scope (S);
865 end loop;
867 return False;
868 end In_Generic_Scope;
870 -- Local variables
872 GM : constant Ghost_Mode_Type := Ghost_Mode;
874 -- Start of processing for Analyze_Object_Renaming
876 begin
877 if Nam = Error then
878 return;
879 end if;
881 -- The object renaming declaration may be subject to pragma Ghost with
882 -- policy Ignore. Set the mode now to ensure that any nodes generated
883 -- during analysis and expansion are properly flagged as ignored Ghost.
885 Set_Ghost_Mode (N);
886 Check_SPARK_05_Restriction ("object renaming is not allowed", N);
888 Set_Is_Pure (Id, Is_Pure (Current_Scope));
889 Enter_Name (Id);
891 -- The renaming of a component that depends on a discriminant requires
892 -- an actual subtype, because in subsequent use of the object Gigi will
893 -- be unable to locate the actual bounds. This explicit step is required
894 -- when the renaming is generated in removing side effects of an
895 -- already-analyzed expression.
897 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
898 T := Etype (Nam);
899 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
901 if Present (Dec) then
902 Insert_Action (N, Dec);
903 T := Defining_Identifier (Dec);
904 Set_Etype (Nam, T);
905 end if;
907 -- Complete analysis of the subtype mark in any case, for ASIS use
909 if Present (Subtype_Mark (N)) then
910 Find_Type (Subtype_Mark (N));
911 end if;
913 elsif Present (Subtype_Mark (N)) then
914 Find_Type (Subtype_Mark (N));
915 T := Entity (Subtype_Mark (N));
916 Analyze (Nam);
918 -- Reject renamings of conversions unless the type is tagged, or
919 -- the conversion is implicit (which can occur for cases of anonymous
920 -- access types in Ada 2012).
922 if Nkind (Nam) = N_Type_Conversion
923 and then Comes_From_Source (Nam)
924 and then not Is_Tagged_Type (T)
925 then
926 Error_Msg_N
927 ("renaming of conversion only allowed for tagged types", Nam);
928 end if;
930 Resolve (Nam, T);
932 -- If the renamed object is a function call of a limited type,
933 -- the expansion of the renaming is complicated by the presence
934 -- of various temporaries and subtypes that capture constraints
935 -- of the renamed object. Rewrite node as an object declaration,
936 -- whose expansion is simpler. Given that the object is limited
937 -- there is no copy involved and no performance hit.
939 if Nkind (Nam) = N_Function_Call
940 and then Is_Limited_View (Etype (Nam))
941 and then not Is_Constrained (Etype (Nam))
942 and then Comes_From_Source (N)
943 then
944 Set_Etype (Id, T);
945 Set_Ekind (Id, E_Constant);
946 Rewrite (N,
947 Make_Object_Declaration (Loc,
948 Defining_Identifier => Id,
949 Constant_Present => True,
950 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
951 Expression => Relocate_Node (Nam)));
952 return;
953 end if;
955 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
956 -- when renaming declaration has a named access type. The Ada 2012
957 -- coverage rules allow an anonymous access type in the context of
958 -- an expected named general access type, but the renaming rules
959 -- require the types to be the same. (An exception is when the type
960 -- of the renaming is also an anonymous access type, which can only
961 -- happen due to a renaming created by the expander.)
963 if Nkind (Nam) = N_Type_Conversion
964 and then not Comes_From_Source (Nam)
965 and then Ekind (Etype (Expression (Nam))) = E_Anonymous_Access_Type
966 and then Ekind (T) /= E_Anonymous_Access_Type
967 then
968 Wrong_Type (Expression (Nam), T); -- Should we give better error???
969 end if;
971 -- Check that a class-wide object is not being renamed as an object
972 -- of a specific type. The test for access types is needed to exclude
973 -- cases where the renamed object is a dynamically tagged access
974 -- result, such as occurs in certain expansions.
976 if Is_Tagged_Type (T) then
977 Check_Dynamically_Tagged_Expression
978 (Expr => Nam,
979 Typ => T,
980 Related_Nod => N);
981 end if;
983 -- Ada 2005 (AI-230/AI-254): Access renaming
985 else pragma Assert (Present (Access_Definition (N)));
986 T := Access_Definition
987 (Related_Nod => N,
988 N => Access_Definition (N));
990 Analyze (Nam);
992 -- Ada 2005 AI05-105: if the declaration has an anonymous access
993 -- type, the renamed object must also have an anonymous type, and
994 -- this is a name resolution rule. This was implicit in the last part
995 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
996 -- recent AI.
998 if not Is_Overloaded (Nam) then
999 if Ekind (Etype (Nam)) /= Ekind (T) then
1000 Error_Msg_N
1001 ("expect anonymous access type in object renaming", N);
1002 end if;
1004 else
1005 declare
1006 I : Interp_Index;
1007 It : Interp;
1008 Typ : Entity_Id := Empty;
1009 Seen : Boolean := False;
1011 begin
1012 Get_First_Interp (Nam, I, It);
1013 while Present (It.Typ) loop
1015 -- Renaming is ambiguous if more than one candidate
1016 -- interpretation is type-conformant with the context.
1018 if Ekind (It.Typ) = Ekind (T) then
1019 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
1020 and then
1021 Type_Conformant
1022 (Designated_Type (T), Designated_Type (It.Typ))
1023 then
1024 if not Seen then
1025 Seen := True;
1026 else
1027 Error_Msg_N
1028 ("ambiguous expression in renaming", Nam);
1029 end if;
1031 elsif Ekind (T) = E_Anonymous_Access_Type
1032 and then
1033 Covers (Designated_Type (T), Designated_Type (It.Typ))
1034 then
1035 if not Seen then
1036 Seen := True;
1037 else
1038 Error_Msg_N
1039 ("ambiguous expression in renaming", Nam);
1040 end if;
1041 end if;
1043 if Covers (T, It.Typ) then
1044 Typ := It.Typ;
1045 Set_Etype (Nam, Typ);
1046 Set_Is_Overloaded (Nam, False);
1047 end if;
1048 end if;
1050 Get_Next_Interp (I, It);
1051 end loop;
1052 end;
1053 end if;
1055 Resolve (Nam, T);
1057 -- Ada 2005 (AI-231): In the case where the type is defined by an
1058 -- access_definition, the renamed entity shall be of an access-to-
1059 -- constant type if and only if the access_definition defines an
1060 -- access-to-constant type. ARM 8.5.1(4)
1062 if Constant_Present (Access_Definition (N))
1063 and then not Is_Access_Constant (Etype (Nam))
1064 then
1065 Error_Msg_N ("(Ada 2005): the renamed object is not "
1066 & "access-to-constant (RM 8.5.1(6))", N);
1068 elsif not Constant_Present (Access_Definition (N))
1069 and then Is_Access_Constant (Etype (Nam))
1070 then
1071 Error_Msg_N ("(Ada 2005): the renamed object is not "
1072 & "access-to-variable (RM 8.5.1(6))", N);
1073 end if;
1075 if Is_Access_Subprogram_Type (Etype (Nam)) then
1076 Check_Subtype_Conformant
1077 (Designated_Type (T), Designated_Type (Etype (Nam)));
1079 elsif not Subtypes_Statically_Match
1080 (Designated_Type (T),
1081 Available_View (Designated_Type (Etype (Nam))))
1082 then
1083 Error_Msg_N
1084 ("subtype of renamed object does not statically match", N);
1085 end if;
1086 end if;
1088 -- Special processing for renaming function return object. Some errors
1089 -- and warnings are produced only for calls that come from source.
1091 if Nkind (Nam) = N_Function_Call then
1092 case Ada_Version is
1094 -- Usage is illegal in Ada 83, but renamings are also introduced
1095 -- during expansion, and error does not apply to those.
1097 when Ada_83 =>
1098 if Comes_From_Source (N) then
1099 Error_Msg_N
1100 ("(Ada 83) cannot rename function return object", Nam);
1101 end if;
1103 -- In Ada 95, warn for odd case of renaming parameterless function
1104 -- call if this is not a limited type (where this is useful).
1106 when others =>
1107 if Warn_On_Object_Renames_Function
1108 and then No (Parameter_Associations (Nam))
1109 and then not Is_Limited_Type (Etype (Nam))
1110 and then Comes_From_Source (Nam)
1111 then
1112 Error_Msg_N
1113 ("renaming function result object is suspicious?R?", Nam);
1114 Error_Msg_NE
1115 ("\function & will be called only once?R?", Nam,
1116 Entity (Name (Nam)));
1117 Error_Msg_N -- CODEFIX
1118 ("\suggest using an initialized constant "
1119 & "object instead?R?", Nam);
1120 end if;
1122 end case;
1123 end if;
1125 Check_Constrained_Object;
1127 -- An object renaming requires an exact match of the type. Class-wide
1128 -- matching is not allowed.
1130 if Is_Class_Wide_Type (T)
1131 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1132 then
1133 Wrong_Type (Nam, T);
1134 end if;
1136 T2 := Etype (Nam);
1138 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1140 if Nkind (Nam) = N_Explicit_Dereference
1141 and then Ekind (Etype (T2)) = E_Incomplete_Type
1142 then
1143 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1144 return;
1146 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1147 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1148 return;
1149 end if;
1151 -- Ada 2005 (AI-327)
1153 if Ada_Version >= Ada_2005
1154 and then Nkind (Nam) = N_Attribute_Reference
1155 and then Attribute_Name (Nam) = Name_Priority
1156 then
1157 null;
1159 elsif Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1160 declare
1161 Nam_Decl : Node_Id;
1162 Nam_Ent : Entity_Id;
1164 begin
1165 if Nkind (Nam) = N_Attribute_Reference then
1166 Nam_Ent := Entity (Prefix (Nam));
1167 else
1168 Nam_Ent := Entity (Nam);
1169 end if;
1171 Nam_Decl := Parent (Nam_Ent);
1173 if Has_Null_Exclusion (N)
1174 and then not Has_Null_Exclusion (Nam_Decl)
1175 then
1176 -- Ada 2005 (AI-423): If the object name denotes a generic
1177 -- formal object of a generic unit G, and the object renaming
1178 -- declaration occurs within the body of G or within the body
1179 -- of a generic unit declared within the declarative region
1180 -- of G, then the declaration of the formal object of G must
1181 -- have a null exclusion or a null-excluding subtype.
1183 if Is_Formal_Object (Nam_Ent)
1184 and then In_Generic_Scope (Id)
1185 then
1186 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1187 Error_Msg_N
1188 ("renamed formal does not exclude `NULL` "
1189 & "(RM 8.5.1(4.6/2))", N);
1191 elsif In_Package_Body (Scope (Id)) then
1192 Error_Msg_N
1193 ("formal object does not have a null exclusion"
1194 & "(RM 8.5.1(4.6/2))", N);
1195 end if;
1197 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1198 -- shall exclude null.
1200 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1201 Error_Msg_N
1202 ("renamed object does not exclude `NULL` "
1203 & "(RM 8.5.1(4.6/2))", N);
1205 -- An instance is illegal if it contains a renaming that
1206 -- excludes null, and the actual does not. The renaming
1207 -- declaration has already indicated that the declaration
1208 -- of the renamed actual in the instance will raise
1209 -- constraint_error.
1211 elsif Nkind (Nam_Decl) = N_Object_Declaration
1212 and then In_Instance
1213 and then
1214 Present (Corresponding_Generic_Association (Nam_Decl))
1215 and then Nkind (Expression (Nam_Decl)) =
1216 N_Raise_Constraint_Error
1217 then
1218 Error_Msg_N
1219 ("renamed actual does not exclude `NULL` "
1220 & "(RM 8.5.1(4.6/2))", N);
1222 -- Finally, if there is a null exclusion, the subtype mark
1223 -- must not be null-excluding.
1225 elsif No (Access_Definition (N))
1226 and then Can_Never_Be_Null (T)
1227 then
1228 Error_Msg_NE
1229 ("`NOT NULL` not allowed (& already excludes null)",
1230 N, T);
1232 end if;
1234 elsif Can_Never_Be_Null (T)
1235 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1236 then
1237 Error_Msg_N
1238 ("renamed object does not exclude `NULL` "
1239 & "(RM 8.5.1(4.6/2))", N);
1241 elsif Has_Null_Exclusion (N)
1242 and then No (Access_Definition (N))
1243 and then Can_Never_Be_Null (T)
1244 then
1245 Error_Msg_NE
1246 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1247 end if;
1248 end;
1249 end if;
1251 -- Set the Ekind of the entity, unless it has been set already, as is
1252 -- the case for the iteration object over a container with no variable
1253 -- indexing. In that case it's been marked as a constant, and we do not
1254 -- want to change it to a variable.
1256 if Ekind (Id) /= E_Constant then
1257 Set_Ekind (Id, E_Variable);
1258 end if;
1260 -- Initialize the object size and alignment. Note that we used to call
1261 -- Init_Size_Align here, but that's wrong for objects which have only
1262 -- an Esize, not an RM_Size field.
1264 Init_Object_Size_Align (Id);
1266 if T = Any_Type or else Etype (Nam) = Any_Type then
1267 return;
1269 -- Verify that the renamed entity is an object or a function call. It
1270 -- may have been rewritten in several ways.
1272 elsif Is_Object_Reference (Nam) then
1273 if Comes_From_Source (N) then
1274 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1275 Error_Msg_N
1276 ("illegal renaming of discriminant-dependent component", Nam);
1277 end if;
1279 -- If the renaming comes from source and the renamed object is a
1280 -- dereference, then mark the prefix as needing debug information,
1281 -- since it might have been rewritten hence internally generated
1282 -- and Debug_Renaming_Declaration will link the renaming to it.
1284 if Nkind (Nam) = N_Explicit_Dereference
1285 and then Is_Entity_Name (Prefix (Nam))
1286 then
1287 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1288 end if;
1289 end if;
1291 -- A static function call may have been folded into a literal
1293 elsif Nkind (Original_Node (Nam)) = N_Function_Call
1295 -- When expansion is disabled, attribute reference is not rewritten
1296 -- as function call. Otherwise it may be rewritten as a conversion,
1297 -- so check original node.
1299 or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference
1300 and then Is_Function_Attribute_Name
1301 (Attribute_Name (Original_Node (Nam))))
1303 -- Weird but legal, equivalent to renaming a function call. Illegal
1304 -- if the literal is the result of constant-folding an attribute
1305 -- reference that is not a function.
1307 or else (Is_Entity_Name (Nam)
1308 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1309 and then
1310 Nkind (Original_Node (Nam)) /= N_Attribute_Reference)
1312 or else (Nkind (Nam) = N_Type_Conversion
1313 and then Is_Tagged_Type (Entity (Subtype_Mark (Nam))))
1314 then
1315 null;
1317 elsif Nkind (Nam) = N_Type_Conversion then
1318 Error_Msg_N
1319 ("renaming of conversion only allowed for tagged types", Nam);
1321 -- Ada 2005 (AI-327)
1323 elsif Ada_Version >= Ada_2005
1324 and then Nkind (Nam) = N_Attribute_Reference
1325 and then Attribute_Name (Nam) = Name_Priority
1326 then
1327 null;
1329 -- Allow internally generated x'Ref resulting in N_Reference node
1331 elsif Nkind (Nam) = N_Reference then
1332 null;
1334 else
1335 Error_Msg_N ("expect object name in renaming", Nam);
1336 end if;
1338 Set_Etype (Id, T2);
1340 if not Is_Variable (Nam) then
1341 Set_Ekind (Id, E_Constant);
1342 Set_Never_Set_In_Source (Id, True);
1343 Set_Is_True_Constant (Id, True);
1344 end if;
1346 -- The object renaming declaration may become Ghost if it renames a
1347 -- Ghost entity.
1349 if Is_Entity_Name (Nam) then
1350 Mark_Renaming_As_Ghost (N, Entity (Nam));
1351 end if;
1353 -- The entity of the renaming declaration needs to reflect whether the
1354 -- renamed object is volatile. Is_Volatile is set if the renamed object
1355 -- is volatile in the RM legality sense.
1357 Set_Is_Volatile (Id, Is_Volatile_Object (Nam));
1359 -- Also copy settings of Atomic/Independent/Volatile_Full_Access
1361 if Is_Entity_Name (Nam) then
1362 Set_Is_Atomic (Id, Is_Atomic (Entity (Nam)));
1363 Set_Is_Independent (Id, Is_Independent (Entity (Nam)));
1364 Set_Is_Volatile_Full_Access (Id,
1365 Is_Volatile_Full_Access (Entity (Nam)));
1366 end if;
1368 -- Treat as volatile if we just set the Volatile flag
1370 if Is_Volatile (Id)
1372 -- Or if we are renaming an entity which was marked this way
1374 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1376 or else (Is_Entity_Name (Nam)
1377 and then Treat_As_Volatile (Entity (Nam)))
1378 then
1379 Set_Treat_As_Volatile (Id, True);
1380 end if;
1382 -- Now make the link to the renamed object
1384 Set_Renamed_Object (Id, Nam);
1386 -- Implementation-defined aspect specifications can appear in a renaming
1387 -- declaration, but not language-defined ones. The call to procedure
1388 -- Analyze_Aspect_Specifications will take care of this error check.
1390 if Has_Aspects (N) then
1391 Analyze_Aspect_Specifications (N, Id);
1392 end if;
1394 -- Deal with dimensions
1396 Analyze_Dimension (N);
1398 -- Restore the original Ghost mode once analysis and expansion have
1399 -- taken place.
1401 Ghost_Mode := GM;
1402 end Analyze_Object_Renaming;
1404 ------------------------------
1405 -- Analyze_Package_Renaming --
1406 ------------------------------
1408 procedure Analyze_Package_Renaming (N : Node_Id) is
1409 GM : constant Ghost_Mode_Type := Ghost_Mode;
1411 procedure Restore_Globals;
1412 -- Restore the values of all saved global variables
1414 ---------------------
1415 -- Restore_Globals --
1416 ---------------------
1418 procedure Restore_Globals is
1419 begin
1420 Ghost_Mode := GM;
1421 end Restore_Globals;
1423 -- Local variables
1425 New_P : constant Entity_Id := Defining_Entity (N);
1426 Old_P : Entity_Id;
1427 Spec : Node_Id;
1429 -- Start of processing for Analyze_Package_Renaming
1431 begin
1432 if Name (N) = Error then
1433 return;
1434 end if;
1436 -- The package renaming declaration may be subject to pragma Ghost with
1437 -- policy Ignore. Set the mode now to ensure that any nodes generated
1438 -- during analysis and expansion are properly flagged as ignored Ghost.
1440 Set_Ghost_Mode (N);
1442 -- Check for Text_IO special unit (we may be renaming a Text_IO child)
1444 Check_Text_IO_Special_Unit (Name (N));
1446 if Current_Scope /= Standard_Standard then
1447 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1448 end if;
1450 Enter_Name (New_P);
1451 Analyze (Name (N));
1453 if Is_Entity_Name (Name (N)) then
1454 Old_P := Entity (Name (N));
1455 else
1456 Old_P := Any_Id;
1457 end if;
1459 if Etype (Old_P) = Any_Type then
1460 Error_Msg_N ("expect package name in renaming", Name (N));
1462 elsif Ekind (Old_P) /= E_Package
1463 and then not (Ekind (Old_P) = E_Generic_Package
1464 and then In_Open_Scopes (Old_P))
1465 then
1466 if Ekind (Old_P) = E_Generic_Package then
1467 Error_Msg_N
1468 ("generic package cannot be renamed as a package", Name (N));
1469 else
1470 Error_Msg_Sloc := Sloc (Old_P);
1471 Error_Msg_NE
1472 ("expect package name in renaming, found& declared#",
1473 Name (N), Old_P);
1474 end if;
1476 -- Set basic attributes to minimize cascaded errors
1478 Set_Ekind (New_P, E_Package);
1479 Set_Etype (New_P, Standard_Void_Type);
1481 -- Here for OK package renaming
1483 else
1484 -- Entities in the old package are accessible through the renaming
1485 -- entity. The simplest implementation is to have both packages share
1486 -- the entity list.
1488 Set_Ekind (New_P, E_Package);
1489 Set_Etype (New_P, Standard_Void_Type);
1491 if Present (Renamed_Object (Old_P)) then
1492 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1493 else
1494 Set_Renamed_Object (New_P, Old_P);
1495 end if;
1497 Set_Has_Completion (New_P);
1499 Set_First_Entity (New_P, First_Entity (Old_P));
1500 Set_Last_Entity (New_P, Last_Entity (Old_P));
1501 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1502 Check_Library_Unit_Renaming (N, Old_P);
1503 Generate_Reference (Old_P, Name (N));
1505 -- The package renaming declaration may become Ghost if it renames a
1506 -- Ghost entity.
1508 Mark_Renaming_As_Ghost (N, Old_P);
1510 -- If the renaming is in the visible part of a package, then we set
1511 -- Renamed_In_Spec for the renamed package, to prevent giving
1512 -- warnings about no entities referenced. Such a warning would be
1513 -- overenthusiastic, since clients can see entities in the renamed
1514 -- package via the visible package renaming.
1516 declare
1517 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1518 begin
1519 if Ekind (Ent) = E_Package
1520 and then not In_Private_Part (Ent)
1521 and then In_Extended_Main_Source_Unit (N)
1522 and then Ekind (Old_P) = E_Package
1523 then
1524 Set_Renamed_In_Spec (Old_P);
1525 end if;
1526 end;
1528 -- If this is the renaming declaration of a package instantiation
1529 -- within itself, it is the declaration that ends the list of actuals
1530 -- for the instantiation. At this point, the subtypes that rename
1531 -- the actuals are flagged as generic, to avoid spurious ambiguities
1532 -- if the actuals for two distinct formals happen to coincide. If
1533 -- the actual is a private type, the subtype has a private completion
1534 -- that is flagged in the same fashion.
1536 -- Resolution is identical to what is was in the original generic.
1537 -- On exit from the generic instance, these are turned into regular
1538 -- subtypes again, so they are compatible with types in their class.
1540 if not Is_Generic_Instance (Old_P) then
1541 Restore_Globals;
1542 return;
1543 else
1544 Spec := Specification (Unit_Declaration_Node (Old_P));
1545 end if;
1547 if Nkind (Spec) = N_Package_Specification
1548 and then Present (Generic_Parent (Spec))
1549 and then Old_P = Current_Scope
1550 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1551 then
1552 declare
1553 E : Entity_Id;
1555 begin
1556 E := First_Entity (Old_P);
1557 while Present (E) and then E /= New_P loop
1558 if Is_Type (E)
1559 and then Nkind (Parent (E)) = N_Subtype_Declaration
1560 then
1561 Set_Is_Generic_Actual_Type (E);
1563 if Is_Private_Type (E)
1564 and then Present (Full_View (E))
1565 then
1566 Set_Is_Generic_Actual_Type (Full_View (E));
1567 end if;
1568 end if;
1570 Next_Entity (E);
1571 end loop;
1572 end;
1573 end if;
1574 end if;
1576 -- Implementation-defined aspect specifications can appear in a renaming
1577 -- declaration, but not language-defined ones. The call to procedure
1578 -- Analyze_Aspect_Specifications will take care of this error check.
1580 if Has_Aspects (N) then
1581 Analyze_Aspect_Specifications (N, New_P);
1582 end if;
1584 Restore_Globals;
1585 end Analyze_Package_Renaming;
1587 -------------------------------
1588 -- Analyze_Renamed_Character --
1589 -------------------------------
1591 procedure Analyze_Renamed_Character
1592 (N : Node_Id;
1593 New_S : Entity_Id;
1594 Is_Body : Boolean)
1596 C : constant Node_Id := Name (N);
1598 begin
1599 if Ekind (New_S) = E_Function then
1600 Resolve (C, Etype (New_S));
1602 if Is_Body then
1603 Check_Frozen_Renaming (N, New_S);
1604 end if;
1606 else
1607 Error_Msg_N ("character literal can only be renamed as function", N);
1608 end if;
1609 end Analyze_Renamed_Character;
1611 ---------------------------------
1612 -- Analyze_Renamed_Dereference --
1613 ---------------------------------
1615 procedure Analyze_Renamed_Dereference
1616 (N : Node_Id;
1617 New_S : Entity_Id;
1618 Is_Body : Boolean)
1620 Nam : constant Node_Id := Name (N);
1621 P : constant Node_Id := Prefix (Nam);
1622 Typ : Entity_Id;
1623 Ind : Interp_Index;
1624 It : Interp;
1626 begin
1627 if not Is_Overloaded (P) then
1628 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1629 or else not Type_Conformant (Etype (Nam), New_S)
1630 then
1631 Error_Msg_N ("designated type does not match specification", P);
1632 else
1633 Resolve (P);
1634 end if;
1636 return;
1638 else
1639 Typ := Any_Type;
1640 Get_First_Interp (Nam, Ind, It);
1642 while Present (It.Nam) loop
1644 if Ekind (It.Nam) = E_Subprogram_Type
1645 and then Type_Conformant (It.Nam, New_S)
1646 then
1647 if Typ /= Any_Id then
1648 Error_Msg_N ("ambiguous renaming", P);
1649 return;
1650 else
1651 Typ := It.Nam;
1652 end if;
1653 end if;
1655 Get_Next_Interp (Ind, It);
1656 end loop;
1658 if Typ = Any_Type then
1659 Error_Msg_N ("designated type does not match specification", P);
1660 else
1661 Resolve (N, Typ);
1663 if Is_Body then
1664 Check_Frozen_Renaming (N, New_S);
1665 end if;
1666 end if;
1667 end if;
1668 end Analyze_Renamed_Dereference;
1670 ---------------------------
1671 -- Analyze_Renamed_Entry --
1672 ---------------------------
1674 procedure Analyze_Renamed_Entry
1675 (N : Node_Id;
1676 New_S : Entity_Id;
1677 Is_Body : Boolean)
1679 Nam : constant Node_Id := Name (N);
1680 Sel : constant Node_Id := Selector_Name (Nam);
1681 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1682 Old_S : Entity_Id;
1684 begin
1685 if Entity (Sel) = Any_Id then
1687 -- Selector is undefined on prefix. Error emitted already
1689 Set_Has_Completion (New_S);
1690 return;
1691 end if;
1693 -- Otherwise find renamed entity and build body of New_S as a call to it
1695 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1697 if Old_S = Any_Id then
1698 Error_Msg_N (" no subprogram or entry matches specification", N);
1699 else
1700 if Is_Body then
1701 Check_Subtype_Conformant (New_S, Old_S, N);
1702 Generate_Reference (New_S, Defining_Entity (N), 'b');
1703 Style.Check_Identifier (Defining_Entity (N), New_S);
1705 else
1706 -- Only mode conformance required for a renaming_as_declaration
1708 Check_Mode_Conformant (New_S, Old_S, N);
1709 end if;
1711 Inherit_Renamed_Profile (New_S, Old_S);
1713 -- The prefix can be an arbitrary expression that yields a task or
1714 -- protected object, so it must be resolved.
1716 Resolve (Prefix (Nam), Scope (Old_S));
1717 end if;
1719 Set_Convention (New_S, Convention (Old_S));
1720 Set_Has_Completion (New_S, Inside_A_Generic);
1722 -- AI05-0225: If the renamed entity is a procedure or entry of a
1723 -- protected object, the target object must be a variable.
1725 if Ekind (Scope (Old_S)) in Protected_Kind
1726 and then Ekind (New_S) = E_Procedure
1727 and then not Is_Variable (Prefix (Nam))
1728 then
1729 if Is_Actual then
1730 Error_Msg_N
1731 ("target object of protected operation used as actual for "
1732 & "formal procedure must be a variable", Nam);
1733 else
1734 Error_Msg_N
1735 ("target object of protected operation renamed as procedure, "
1736 & "must be a variable", Nam);
1737 end if;
1738 end if;
1740 if Is_Body then
1741 Check_Frozen_Renaming (N, New_S);
1742 end if;
1743 end Analyze_Renamed_Entry;
1745 -----------------------------------
1746 -- Analyze_Renamed_Family_Member --
1747 -----------------------------------
1749 procedure Analyze_Renamed_Family_Member
1750 (N : Node_Id;
1751 New_S : Entity_Id;
1752 Is_Body : Boolean)
1754 Nam : constant Node_Id := Name (N);
1755 P : constant Node_Id := Prefix (Nam);
1756 Old_S : Entity_Id;
1758 begin
1759 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1760 or else (Nkind (P) = N_Selected_Component
1761 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1762 then
1763 if Is_Entity_Name (P) then
1764 Old_S := Entity (P);
1765 else
1766 Old_S := Entity (Selector_Name (P));
1767 end if;
1769 if not Entity_Matches_Spec (Old_S, New_S) then
1770 Error_Msg_N ("entry family does not match specification", N);
1772 elsif Is_Body then
1773 Check_Subtype_Conformant (New_S, Old_S, N);
1774 Generate_Reference (New_S, Defining_Entity (N), 'b');
1775 Style.Check_Identifier (Defining_Entity (N), New_S);
1776 end if;
1778 else
1779 Error_Msg_N ("no entry family matches specification", N);
1780 end if;
1782 Set_Has_Completion (New_S, Inside_A_Generic);
1784 if Is_Body then
1785 Check_Frozen_Renaming (N, New_S);
1786 end if;
1787 end Analyze_Renamed_Family_Member;
1789 -----------------------------------------
1790 -- Analyze_Renamed_Primitive_Operation --
1791 -----------------------------------------
1793 procedure Analyze_Renamed_Primitive_Operation
1794 (N : Node_Id;
1795 New_S : Entity_Id;
1796 Is_Body : Boolean)
1798 Old_S : Entity_Id;
1800 function Conforms
1801 (Subp : Entity_Id;
1802 Ctyp : Conformance_Type) return Boolean;
1803 -- Verify that the signatures of the renamed entity and the new entity
1804 -- match. The first formal of the renamed entity is skipped because it
1805 -- is the target object in any subsequent call.
1807 --------------
1808 -- Conforms --
1809 --------------
1811 function Conforms
1812 (Subp : Entity_Id;
1813 Ctyp : Conformance_Type) return Boolean
1815 Old_F : Entity_Id;
1816 New_F : Entity_Id;
1818 begin
1819 if Ekind (Subp) /= Ekind (New_S) then
1820 return False;
1821 end if;
1823 Old_F := Next_Formal (First_Formal (Subp));
1824 New_F := First_Formal (New_S);
1825 while Present (Old_F) and then Present (New_F) loop
1826 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
1827 return False;
1828 end if;
1830 if Ctyp >= Mode_Conformant
1831 and then Ekind (Old_F) /= Ekind (New_F)
1832 then
1833 return False;
1834 end if;
1836 Next_Formal (New_F);
1837 Next_Formal (Old_F);
1838 end loop;
1840 return True;
1841 end Conforms;
1843 -- Start of processing for Analyze_Renamed_Primitive_Operation
1845 begin
1846 if not Is_Overloaded (Selector_Name (Name (N))) then
1847 Old_S := Entity (Selector_Name (Name (N)));
1849 if not Conforms (Old_S, Type_Conformant) then
1850 Old_S := Any_Id;
1851 end if;
1853 else
1854 -- Find the operation that matches the given signature
1856 declare
1857 It : Interp;
1858 Ind : Interp_Index;
1860 begin
1861 Old_S := Any_Id;
1862 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
1864 while Present (It.Nam) loop
1865 if Conforms (It.Nam, Type_Conformant) then
1866 Old_S := It.Nam;
1867 end if;
1869 Get_Next_Interp (Ind, It);
1870 end loop;
1871 end;
1872 end if;
1874 if Old_S = Any_Id then
1875 Error_Msg_N (" no subprogram or entry matches specification", N);
1877 else
1878 if Is_Body then
1879 if not Conforms (Old_S, Subtype_Conformant) then
1880 Error_Msg_N ("subtype conformance error in renaming", N);
1881 end if;
1883 Generate_Reference (New_S, Defining_Entity (N), 'b');
1884 Style.Check_Identifier (Defining_Entity (N), New_S);
1886 else
1887 -- Only mode conformance required for a renaming_as_declaration
1889 if not Conforms (Old_S, Mode_Conformant) then
1890 Error_Msg_N ("mode conformance error in renaming", N);
1891 end if;
1893 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
1894 -- view of a subprogram is intrinsic, because the compiler has
1895 -- to generate a wrapper for any call to it. If the name in a
1896 -- subprogram renaming is a prefixed view, the entity is thus
1897 -- intrinsic, and 'Access cannot be applied to it.
1899 Set_Convention (New_S, Convention_Intrinsic);
1900 end if;
1902 -- Inherit_Renamed_Profile (New_S, Old_S);
1904 -- The prefix can be an arbitrary expression that yields an
1905 -- object, so it must be resolved.
1907 Resolve (Prefix (Name (N)));
1908 end if;
1909 end Analyze_Renamed_Primitive_Operation;
1911 ---------------------------------
1912 -- Analyze_Subprogram_Renaming --
1913 ---------------------------------
1915 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1916 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
1917 Is_Actual : constant Boolean := Present (Formal_Spec);
1918 Nam : constant Node_Id := Name (N);
1919 Save_AV : constant Ada_Version_Type := Ada_Version;
1920 Save_AVP : constant Node_Id := Ada_Version_Pragma;
1921 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
1922 Spec : constant Node_Id := Specification (N);
1924 Old_S : Entity_Id := Empty;
1925 Rename_Spec : Entity_Id;
1927 procedure Build_Class_Wide_Wrapper
1928 (Ren_Id : out Entity_Id;
1929 Wrap_Id : out Entity_Id);
1930 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal
1931 -- type with unknown discriminants and a generic primitive operation of
1932 -- the said type with a box require special processing when the actual
1933 -- is a class-wide type:
1935 -- generic
1936 -- type Formal_Typ (<>) is private;
1937 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
1938 -- package Gen is ...
1940 -- package Inst is new Gen (Actual_Typ'Class);
1942 -- In this case the general renaming mechanism used in the prologue of
1943 -- an instance no longer applies:
1945 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
1947 -- The above is replaced the following wrapper/renaming combination:
1949 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
1950 -- begin
1951 -- Prim_Op (Param); -- primitive
1952 -- end Wrapper;
1954 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
1956 -- This transformation applies only if there is no explicit visible
1957 -- class-wide operation at the point of the instantiation. Ren_Id is
1958 -- the entity of the renaming declaration. Wrap_Id is the entity of
1959 -- the generated class-wide wrapper (or Any_Id).
1961 procedure Check_Null_Exclusion
1962 (Ren : Entity_Id;
1963 Sub : Entity_Id);
1964 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
1965 -- following AI rules:
1967 -- If Ren is a renaming of a formal subprogram and one of its
1968 -- parameters has a null exclusion, then the corresponding formal
1969 -- in Sub must also have one. Otherwise the subtype of the Sub's
1970 -- formal parameter must exclude null.
1972 -- If Ren is a renaming of a formal function and its return
1973 -- profile has a null exclusion, then Sub's return profile must
1974 -- have one. Otherwise the subtype of Sub's return profile must
1975 -- exclude null.
1977 procedure Freeze_Actual_Profile;
1978 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
1979 -- types: a callable entity freezes its profile, unless it has an
1980 -- incomplete untagged formal (RM 13.14(10.2/3)).
1982 function Has_Class_Wide_Actual return Boolean;
1983 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
1984 -- defaulted formal subprogram where the actual for the controlling
1985 -- formal type is class-wide.
1987 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
1988 -- Find renamed entity when the declaration is a renaming_as_body and
1989 -- the renamed entity may itself be a renaming_as_body. Used to enforce
1990 -- rule that a renaming_as_body is illegal if the declaration occurs
1991 -- before the subprogram it completes is frozen, and renaming indirectly
1992 -- renames the subprogram itself.(Defect Report 8652/0027).
1994 ------------------------------
1995 -- Build_Class_Wide_Wrapper --
1996 ------------------------------
1998 procedure Build_Class_Wide_Wrapper
1999 (Ren_Id : out Entity_Id;
2000 Wrap_Id : out Entity_Id)
2002 Loc : constant Source_Ptr := Sloc (N);
2004 function Build_Call
2005 (Subp_Id : Entity_Id;
2006 Params : List_Id) return Node_Id;
2007 -- Create a dispatching call to invoke routine Subp_Id with actuals
2008 -- built from the parameter specifications of list Params.
2010 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
2011 -- Create a subprogram specification based on the subprogram profile
2012 -- of Subp_Id.
2014 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
2015 -- Find a primitive subprogram of type Typ which matches the profile
2016 -- of the renaming declaration.
2018 procedure Interpretation_Error (Subp_Id : Entity_Id);
2019 -- Emit a continuation error message suggesting subprogram Subp_Id as
2020 -- a possible interpretation.
2022 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean;
2023 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
2024 -- operator.
2026 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean;
2027 -- Determine whether subprogram Subp_Id is a suitable candidate for
2028 -- the role of a wrapped subprogram.
2030 ----------------
2031 -- Build_Call --
2032 ----------------
2034 function Build_Call
2035 (Subp_Id : Entity_Id;
2036 Params : List_Id) return Node_Id
2038 Actuals : constant List_Id := New_List;
2039 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2040 Formal : Node_Id;
2042 begin
2043 -- Build the actual parameters of the call
2045 Formal := First (Params);
2046 while Present (Formal) loop
2047 Append_To (Actuals,
2048 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2049 Next (Formal);
2050 end loop;
2052 -- Generate:
2053 -- return Subp_Id (Actuals);
2055 if Ekind_In (Subp_Id, E_Function, E_Operator) then
2056 return
2057 Make_Simple_Return_Statement (Loc,
2058 Expression =>
2059 Make_Function_Call (Loc,
2060 Name => Call_Ref,
2061 Parameter_Associations => Actuals));
2063 -- Generate:
2064 -- Subp_Id (Actuals);
2066 else
2067 return
2068 Make_Procedure_Call_Statement (Loc,
2069 Name => Call_Ref,
2070 Parameter_Associations => Actuals);
2071 end if;
2072 end Build_Call;
2074 ----------------
2075 -- Build_Spec --
2076 ----------------
2078 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2079 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2080 Spec_Id : constant Entity_Id :=
2081 Make_Defining_Identifier (Loc,
2082 Chars => New_External_Name (Chars (Subp_Id), 'R'));
2084 begin
2085 if Ekind (Formal_Spec) = E_Procedure then
2086 return
2087 Make_Procedure_Specification (Loc,
2088 Defining_Unit_Name => Spec_Id,
2089 Parameter_Specifications => Params);
2090 else
2091 return
2092 Make_Function_Specification (Loc,
2093 Defining_Unit_Name => Spec_Id,
2094 Parameter_Specifications => Params,
2095 Result_Definition =>
2096 New_Copy_Tree (Result_Definition (Spec)));
2097 end if;
2098 end Build_Spec;
2100 --------------------
2101 -- Find_Primitive --
2102 --------------------
2104 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2105 procedure Replace_Parameter_Types (Spec : Node_Id);
2106 -- Given a specification Spec, replace all class-wide parameter
2107 -- types with reference to type Typ.
2109 -----------------------------
2110 -- Replace_Parameter_Types --
2111 -----------------------------
2113 procedure Replace_Parameter_Types (Spec : Node_Id) is
2114 Formal : Node_Id;
2115 Formal_Id : Entity_Id;
2116 Formal_Typ : Node_Id;
2118 begin
2119 Formal := First (Parameter_Specifications (Spec));
2120 while Present (Formal) loop
2121 Formal_Id := Defining_Identifier (Formal);
2122 Formal_Typ := Parameter_Type (Formal);
2124 -- Create a new entity for each class-wide formal to prevent
2125 -- aliasing with the original renaming. Replace the type of
2126 -- such a parameter with the candidate type.
2128 if Nkind (Formal_Typ) = N_Identifier
2129 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2130 then
2131 Set_Defining_Identifier (Formal,
2132 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2134 Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc));
2135 end if;
2137 Next (Formal);
2138 end loop;
2139 end Replace_Parameter_Types;
2141 -- Local variables
2143 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2144 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2145 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2146 Subp_Id : Entity_Id;
2148 -- Start of processing for Find_Primitive
2150 begin
2151 -- Each attempt to find a suitable primitive of a particular type
2152 -- operates on its own copy of the original renaming. As a result
2153 -- the original renaming is kept decoration and side-effect free.
2155 -- Inherit the overloaded status of the renamed subprogram name
2157 if Is_Overloaded (Nam) then
2158 Set_Is_Overloaded (Alt_Nam);
2159 Save_Interps (Nam, Alt_Nam);
2160 end if;
2162 -- The copied renaming is hidden from visibility to prevent the
2163 -- pollution of the enclosing context.
2165 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2167 -- The types of all class-wide parameters must be changed to the
2168 -- candidate type.
2170 Replace_Parameter_Types (Alt_Spec);
2172 -- Try to find a suitable primitive which matches the altered
2173 -- profile of the renaming specification.
2175 Subp_Id :=
2176 Find_Renamed_Entity
2177 (N => Alt_Ren,
2178 Nam => Name (Alt_Ren),
2179 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2180 Is_Actual => Is_Actual);
2182 -- Do not return Any_Id if the resolion of the altered profile
2183 -- failed as this complicates further checks on the caller side,
2184 -- return Empty instead.
2186 if Subp_Id = Any_Id then
2187 return Empty;
2188 else
2189 return Subp_Id;
2190 end if;
2191 end Find_Primitive;
2193 --------------------------
2194 -- Interpretation_Error --
2195 --------------------------
2197 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2198 begin
2199 Error_Msg_Sloc := Sloc (Subp_Id);
2201 if Is_Internal (Subp_Id) then
2202 Error_Msg_NE
2203 ("\\possible interpretation: predefined & #",
2204 Spec, Formal_Spec);
2205 else
2206 Error_Msg_NE
2207 ("\\possible interpretation: & defined #", Spec, Formal_Spec);
2208 end if;
2209 end Interpretation_Error;
2211 ---------------------------
2212 -- Is_Intrinsic_Equality --
2213 ---------------------------
2215 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is
2216 begin
2217 return
2218 Ekind (Subp_Id) = E_Operator
2219 and then Chars (Subp_Id) = Name_Op_Eq
2220 and then Is_Intrinsic_Subprogram (Subp_Id);
2221 end Is_Intrinsic_Equality;
2223 ---------------------------
2224 -- Is_Suitable_Candidate --
2225 ---------------------------
2227 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is
2228 begin
2229 if No (Subp_Id) then
2230 return False;
2232 -- An intrinsic subprogram is never a good candidate. This is an
2233 -- indication of a missing primitive, either defined directly or
2234 -- inherited from a parent tagged type.
2236 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2237 return False;
2239 else
2240 return True;
2241 end if;
2242 end Is_Suitable_Candidate;
2244 -- Local variables
2246 Actual_Typ : Entity_Id := Empty;
2247 -- The actual class-wide type for Formal_Typ
2249 CW_Prim_OK : Boolean;
2250 CW_Prim_Op : Entity_Id;
2251 -- The class-wide subprogram (if available) which corresponds to the
2252 -- renamed generic formal subprogram.
2254 Formal_Typ : Entity_Id := Empty;
2255 -- The generic formal type with unknown discriminants
2257 Root_Prim_OK : Boolean;
2258 Root_Prim_Op : Entity_Id;
2259 -- The root type primitive (if available) which corresponds to the
2260 -- renamed generic formal subprogram.
2262 Root_Typ : Entity_Id := Empty;
2263 -- The root type of Actual_Typ
2265 Body_Decl : Node_Id;
2266 Formal : Node_Id;
2267 Prim_Op : Entity_Id;
2268 Spec_Decl : Node_Id;
2270 -- Start of processing for Build_Class_Wide_Wrapper
2272 begin
2273 -- Analyze the specification of the renaming in case the generation
2274 -- of the class-wide wrapper fails.
2276 Ren_Id := Analyze_Subprogram_Specification (Spec);
2277 Wrap_Id := Any_Id;
2279 -- Do not attempt to build a wrapper if the renaming is in error
2281 if Error_Posted (Nam) then
2282 return;
2283 end if;
2285 -- Analyze the renamed name, but do not resolve it. The resolution is
2286 -- completed once a suitable subprogram is found.
2288 Analyze (Nam);
2290 -- When the renamed name denotes the intrinsic operator equals, the
2291 -- name must be treated as overloaded. This allows for a potential
2292 -- match against the root type's predefined equality function.
2294 if Is_Intrinsic_Equality (Entity (Nam)) then
2295 Set_Is_Overloaded (Nam);
2296 Collect_Interps (Nam);
2297 end if;
2299 -- Step 1: Find the generic formal type with unknown discriminants
2300 -- and its corresponding class-wide actual type from the renamed
2301 -- generic formal subprogram.
2303 Formal := First_Formal (Formal_Spec);
2304 while Present (Formal) loop
2305 if Has_Unknown_Discriminants (Etype (Formal))
2306 and then not Is_Class_Wide_Type (Etype (Formal))
2307 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2308 then
2309 Formal_Typ := Etype (Formal);
2310 Actual_Typ := Get_Instance_Of (Formal_Typ);
2311 Root_Typ := Etype (Actual_Typ);
2312 exit;
2313 end if;
2315 Next_Formal (Formal);
2316 end loop;
2318 -- The specification of the generic formal subprogram should always
2319 -- contain a formal type with unknown discriminants whose actual is
2320 -- a class-wide type, otherwise this indicates a failure in routine
2321 -- Has_Class_Wide_Actual.
2323 pragma Assert (Present (Formal_Typ));
2325 -- Step 2: Find the proper class-wide subprogram or primitive which
2326 -- corresponds to the renamed generic formal subprogram.
2328 CW_Prim_Op := Find_Primitive (Actual_Typ);
2329 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2330 Root_Prim_Op := Find_Primitive (Root_Typ);
2331 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2333 -- The class-wide actual type has two subprograms which correspond to
2334 -- the renamed generic formal subprogram:
2336 -- with procedure Prim_Op (Param : Formal_Typ);
2338 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2339 -- procedure Prim_Op (Param : Actual_Typ'Class);
2341 -- Even though the declaration of the two subprograms is legal, a
2342 -- call to either one is ambiguous and therefore illegal.
2344 if CW_Prim_OK and Root_Prim_OK then
2346 -- A user-defined primitive has precedence over a predefined one
2348 if Is_Internal (CW_Prim_Op)
2349 and then not Is_Internal (Root_Prim_Op)
2350 then
2351 Prim_Op := Root_Prim_Op;
2353 elsif Is_Internal (Root_Prim_Op)
2354 and then not Is_Internal (CW_Prim_Op)
2355 then
2356 Prim_Op := CW_Prim_Op;
2358 elsif CW_Prim_Op = Root_Prim_Op then
2359 Prim_Op := Root_Prim_Op;
2361 -- Otherwise both candidate subprograms are user-defined and
2362 -- ambiguous.
2364 else
2365 Error_Msg_NE
2366 ("ambiguous actual for generic subprogram &",
2367 Spec, Formal_Spec);
2368 Interpretation_Error (Root_Prim_Op);
2369 Interpretation_Error (CW_Prim_Op);
2370 return;
2371 end if;
2373 elsif CW_Prim_OK and not Root_Prim_OK then
2374 Prim_Op := CW_Prim_Op;
2376 elsif not CW_Prim_OK and Root_Prim_OK then
2377 Prim_Op := Root_Prim_Op;
2379 -- An intrinsic equality may act as a suitable candidate in the case
2380 -- of a null type extension where the parent's equality is hidden. A
2381 -- call to an intrinsic equality is expanded as dispatching.
2383 elsif Present (Root_Prim_Op)
2384 and then Is_Intrinsic_Equality (Root_Prim_Op)
2385 then
2386 Prim_Op := Root_Prim_Op;
2388 -- Otherwise there are no candidate subprograms. Let the caller
2389 -- diagnose the error.
2391 else
2392 return;
2393 end if;
2395 -- At this point resolution has taken place and the name is no longer
2396 -- overloaded. Mark the primitive as referenced.
2398 Set_Is_Overloaded (Name (N), False);
2399 Set_Referenced (Prim_Op);
2401 -- Step 3: Create the declaration and the body of the wrapper, insert
2402 -- all the pieces into the tree.
2404 Spec_Decl :=
2405 Make_Subprogram_Declaration (Loc,
2406 Specification => Build_Spec (Ren_Id));
2407 Insert_Before_And_Analyze (N, Spec_Decl);
2409 -- If the operator carries an Eliminated pragma, indicate that the
2410 -- wrapper is also to be eliminated, to prevent spurious error when
2411 -- using gnatelim on programs that include box-initialization of
2412 -- equality operators.
2414 Wrap_Id := Defining_Entity (Spec_Decl);
2415 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2417 Body_Decl :=
2418 Make_Subprogram_Body (Loc,
2419 Specification => Build_Spec (Ren_Id),
2420 Declarations => New_List,
2421 Handled_Statement_Sequence =>
2422 Make_Handled_Sequence_Of_Statements (Loc,
2423 Statements => New_List (
2424 Build_Call
2425 (Subp_Id => Prim_Op,
2426 Params =>
2427 Parameter_Specifications
2428 (Specification (Spec_Decl))))));
2430 -- The generated body does not freeze and must be analyzed when the
2431 -- class-wide wrapper is frozen. The body is only needed if expansion
2432 -- is enabled.
2434 if Expander_Active then
2435 Append_Freeze_Action (Wrap_Id, Body_Decl);
2436 end if;
2438 -- Step 4: The subprogram renaming aliases the wrapper
2440 Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc));
2441 end Build_Class_Wide_Wrapper;
2443 --------------------------
2444 -- Check_Null_Exclusion --
2445 --------------------------
2447 procedure Check_Null_Exclusion
2448 (Ren : Entity_Id;
2449 Sub : Entity_Id)
2451 Ren_Formal : Entity_Id;
2452 Sub_Formal : Entity_Id;
2454 begin
2455 -- Parameter check
2457 Ren_Formal := First_Formal (Ren);
2458 Sub_Formal := First_Formal (Sub);
2459 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2460 if Has_Null_Exclusion (Parent (Ren_Formal))
2461 and then
2462 not (Has_Null_Exclusion (Parent (Sub_Formal))
2463 or else Can_Never_Be_Null (Etype (Sub_Formal)))
2464 then
2465 Error_Msg_NE
2466 ("`NOT NULL` required for parameter &",
2467 Parent (Sub_Formal), Sub_Formal);
2468 end if;
2470 Next_Formal (Ren_Formal);
2471 Next_Formal (Sub_Formal);
2472 end loop;
2474 -- Return profile check
2476 if Nkind (Parent (Ren)) = N_Function_Specification
2477 and then Nkind (Parent (Sub)) = N_Function_Specification
2478 and then Has_Null_Exclusion (Parent (Ren))
2479 and then not (Has_Null_Exclusion (Parent (Sub))
2480 or else Can_Never_Be_Null (Etype (Sub)))
2481 then
2482 Error_Msg_N
2483 ("return must specify `NOT NULL`",
2484 Result_Definition (Parent (Sub)));
2485 end if;
2486 end Check_Null_Exclusion;
2488 ---------------------------
2489 -- Freeze_Actual_Profile --
2490 ---------------------------
2492 procedure Freeze_Actual_Profile is
2493 F : Entity_Id;
2494 Has_Untagged_Inc : Boolean;
2495 Instantiation_Node : constant Node_Id := Parent (N);
2497 begin
2498 if Ada_Version >= Ada_2012 then
2499 F := First_Formal (Formal_Spec);
2500 Has_Untagged_Inc := False;
2501 while Present (F) loop
2502 if Ekind (Etype (F)) = E_Incomplete_Type
2503 and then not Is_Tagged_Type (Etype (F))
2504 then
2505 Has_Untagged_Inc := True;
2506 exit;
2507 end if;
2509 F := Next_Formal (F);
2510 end loop;
2512 if Ekind (Formal_Spec) = E_Function
2513 and then not Is_Tagged_Type (Etype (Formal_Spec))
2514 then
2515 Has_Untagged_Inc := True;
2516 end if;
2518 if not Has_Untagged_Inc then
2519 F := First_Formal (Old_S);
2520 while Present (F) loop
2521 Freeze_Before (Instantiation_Node, Etype (F));
2523 if Is_Incomplete_Or_Private_Type (Etype (F))
2524 and then No (Underlying_Type (Etype (F)))
2525 then
2526 -- Exclude generic types, or types derived from them.
2527 -- They will be frozen in the enclosing instance.
2529 if Is_Generic_Type (Etype (F))
2530 or else Is_Generic_Type (Root_Type (Etype (F)))
2531 then
2532 null;
2534 -- A limited view of a type declared elsewhere needs no
2535 -- freezing actions.
2537 elsif From_Limited_With (Etype (F)) then
2538 null;
2540 else
2541 Error_Msg_NE
2542 ("type& must be frozen before this point",
2543 Instantiation_Node, Etype (F));
2544 end if;
2545 end if;
2547 F := Next_Formal (F);
2548 end loop;
2549 end if;
2550 end if;
2551 end Freeze_Actual_Profile;
2553 ---------------------------
2554 -- Has_Class_Wide_Actual --
2555 ---------------------------
2557 function Has_Class_Wide_Actual return Boolean is
2558 Formal : Entity_Id;
2559 Formal_Typ : Entity_Id;
2561 begin
2562 if Is_Actual then
2563 Formal := First_Formal (Formal_Spec);
2564 while Present (Formal) loop
2565 Formal_Typ := Etype (Formal);
2567 if Has_Unknown_Discriminants (Formal_Typ)
2568 and then not Is_Class_Wide_Type (Formal_Typ)
2569 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2570 then
2571 return True;
2572 end if;
2574 Next_Formal (Formal);
2575 end loop;
2576 end if;
2578 return False;
2579 end Has_Class_Wide_Actual;
2581 -------------------------
2582 -- Original_Subprogram --
2583 -------------------------
2585 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2586 Orig_Decl : Node_Id;
2587 Orig_Subp : Entity_Id;
2589 begin
2590 -- First case: renamed entity is itself a renaming
2592 if Present (Alias (Subp)) then
2593 return Alias (Subp);
2595 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2596 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2597 then
2598 -- Check if renamed entity is a renaming_as_body
2600 Orig_Decl :=
2601 Unit_Declaration_Node
2602 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2604 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2605 Orig_Subp := Entity (Name (Orig_Decl));
2607 if Orig_Subp = Rename_Spec then
2609 -- Circularity detected
2611 return Orig_Subp;
2613 else
2614 return (Original_Subprogram (Orig_Subp));
2615 end if;
2616 else
2617 return Subp;
2618 end if;
2619 else
2620 return Subp;
2621 end if;
2622 end Original_Subprogram;
2624 -- Local variables
2626 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2627 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2628 -- defaulted formal subprogram when the actual for a related formal
2629 -- type is class-wide.
2631 GM : constant Ghost_Mode_Type := Ghost_Mode;
2632 Inst_Node : Node_Id := Empty;
2633 New_S : Entity_Id;
2635 -- Start of processing for Analyze_Subprogram_Renaming
2637 begin
2638 -- The subprogram renaming declaration may be subject to pragma Ghost
2639 -- with policy Ignore. Set the mode now to ensure that any nodes
2640 -- generated during analysis and expansion are properly flagged as
2641 -- ignored Ghost.
2643 Set_Ghost_Mode (N);
2645 -- We must test for the attribute renaming case before the Analyze
2646 -- call because otherwise Sem_Attr will complain that the attribute
2647 -- is missing an argument when it is analyzed.
2649 if Nkind (Nam) = N_Attribute_Reference then
2651 -- In the case of an abstract formal subprogram association, rewrite
2652 -- an actual given by a stream attribute as the name of the
2653 -- corresponding stream primitive of the type.
2655 -- In a generic context the stream operations are not generated, and
2656 -- this must be treated as a normal attribute reference, to be
2657 -- expanded in subsequent instantiations.
2659 if Is_Actual
2660 and then Is_Abstract_Subprogram (Formal_Spec)
2661 and then Expander_Active
2662 then
2663 declare
2664 Stream_Prim : Entity_Id;
2665 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
2667 begin
2668 -- The class-wide forms of the stream attributes are not
2669 -- primitive dispatching operations (even though they
2670 -- internally dispatch to a stream attribute).
2672 if Is_Class_Wide_Type (Prefix_Type) then
2673 Error_Msg_N
2674 ("attribute must be a primitive dispatching operation",
2675 Nam);
2676 return;
2677 end if;
2679 -- Retrieve the primitive subprogram associated with the
2680 -- attribute. This can only be a stream attribute, since those
2681 -- are the only ones that are dispatching (and the actual for
2682 -- an abstract formal subprogram must be dispatching
2683 -- operation).
2685 case Attribute_Name (Nam) is
2686 when Name_Input =>
2687 Stream_Prim :=
2688 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input);
2689 when Name_Output =>
2690 Stream_Prim :=
2691 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output);
2692 when Name_Read =>
2693 Stream_Prim :=
2694 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read);
2695 when Name_Write =>
2696 Stream_Prim :=
2697 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write);
2698 when others =>
2699 Error_Msg_N
2700 ("attribute must be a primitive"
2701 & " dispatching operation", Nam);
2702 return;
2703 end case;
2705 -- If no operation was found, and the type is limited,
2706 -- the user should have defined one.
2708 if No (Stream_Prim) then
2709 if Is_Limited_Type (Prefix_Type) then
2710 Error_Msg_NE
2711 ("stream operation not defined for type&",
2712 N, Prefix_Type);
2713 return;
2715 -- Otherwise, compiler should have generated default
2717 else
2718 raise Program_Error;
2719 end if;
2720 end if;
2722 -- Rewrite the attribute into the name of its corresponding
2723 -- primitive dispatching subprogram. We can then proceed with
2724 -- the usual processing for subprogram renamings.
2726 declare
2727 Prim_Name : constant Node_Id :=
2728 Make_Identifier (Sloc (Nam),
2729 Chars => Chars (Stream_Prim));
2730 begin
2731 Set_Entity (Prim_Name, Stream_Prim);
2732 Rewrite (Nam, Prim_Name);
2733 Analyze (Nam);
2734 end;
2735 end;
2737 -- Normal processing for a renaming of an attribute
2739 else
2740 Attribute_Renaming (N);
2741 return;
2742 end if;
2743 end if;
2745 -- Check whether this declaration corresponds to the instantiation
2746 -- of a formal subprogram.
2748 -- If this is an instantiation, the corresponding actual is frozen and
2749 -- error messages can be made more precise. If this is a default
2750 -- subprogram, the entity is already established in the generic, and is
2751 -- not retrieved by visibility. If it is a default with a box, the
2752 -- candidate interpretations, if any, have been collected when building
2753 -- the renaming declaration. If overloaded, the proper interpretation is
2754 -- determined in Find_Renamed_Entity. If the entity is an operator,
2755 -- Find_Renamed_Entity applies additional visibility checks.
2757 if Is_Actual then
2758 Inst_Node := Unit_Declaration_Node (Formal_Spec);
2760 -- Check whether the renaming is for a defaulted actual subprogram
2761 -- with a class-wide actual.
2763 -- The class-wide wrapper is not needed in GNATprove_Mode and there
2764 -- is an external axiomatization on the package.
2766 if CW_Actual
2767 and then Box_Present (Inst_Node)
2768 and then not
2769 (GNATprove_Mode
2770 and then
2771 Present (Containing_Package_With_Ext_Axioms (Formal_Spec)))
2772 then
2773 Build_Class_Wide_Wrapper (New_S, Old_S);
2775 elsif Is_Entity_Name (Nam)
2776 and then Present (Entity (Nam))
2777 and then not Comes_From_Source (Nam)
2778 and then not Is_Overloaded (Nam)
2779 then
2780 Old_S := Entity (Nam);
2781 New_S := Analyze_Subprogram_Specification (Spec);
2783 -- Operator case
2785 if Ekind (Entity (Nam)) = E_Operator then
2787 -- Box present
2789 if Box_Present (Inst_Node) then
2790 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2792 -- If there is an immediately visible homonym of the operator
2793 -- and the declaration has a default, this is worth a warning
2794 -- because the user probably did not intend to get the pre-
2795 -- defined operator, visible in the generic declaration. To
2796 -- find if there is an intended candidate, analyze the renaming
2797 -- again in the current context.
2799 elsif Scope (Old_S) = Standard_Standard
2800 and then Present (Default_Name (Inst_Node))
2801 then
2802 declare
2803 Decl : constant Node_Id := New_Copy_Tree (N);
2804 Hidden : Entity_Id;
2806 begin
2807 Set_Entity (Name (Decl), Empty);
2808 Analyze (Name (Decl));
2809 Hidden :=
2810 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
2812 if Present (Hidden)
2813 and then In_Open_Scopes (Scope (Hidden))
2814 and then Is_Immediately_Visible (Hidden)
2815 and then Comes_From_Source (Hidden)
2816 and then Hidden /= Old_S
2817 then
2818 Error_Msg_Sloc := Sloc (Hidden);
2819 Error_Msg_N ("default subprogram is resolved " &
2820 "in the generic declaration " &
2821 "(RM 12.6(17))??", N);
2822 Error_Msg_NE ("\and will not use & #??", N, Hidden);
2823 end if;
2824 end;
2825 end if;
2826 end if;
2828 else
2829 Analyze (Nam);
2830 New_S := Analyze_Subprogram_Specification (Spec);
2831 end if;
2833 else
2834 -- Renamed entity must be analyzed first, to avoid being hidden by
2835 -- new name (which might be the same in a generic instance).
2837 Analyze (Nam);
2839 -- The renaming defines a new overloaded entity, which is analyzed
2840 -- like a subprogram declaration.
2842 New_S := Analyze_Subprogram_Specification (Spec);
2843 end if;
2845 if Current_Scope /= Standard_Standard then
2846 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
2847 end if;
2849 -- Set SPARK mode from current context
2851 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
2852 Set_SPARK_Pragma_Inherited (New_S, True);
2854 Rename_Spec := Find_Corresponding_Spec (N);
2856 -- Case of Renaming_As_Body
2858 if Present (Rename_Spec) then
2860 -- Renaming declaration is the completion of the declaration of
2861 -- Rename_Spec. We build an actual body for it at the freezing point.
2863 Set_Corresponding_Spec (N, Rename_Spec);
2865 -- Deal with special case of stream functions of abstract types
2866 -- and interfaces.
2868 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
2869 N_Abstract_Subprogram_Declaration
2870 then
2871 -- Input stream functions are abstract if the object type is
2872 -- abstract. Similarly, all default stream functions for an
2873 -- interface type are abstract. However, these subprograms may
2874 -- receive explicit declarations in representation clauses, making
2875 -- the attribute subprograms usable as defaults in subsequent
2876 -- type extensions.
2877 -- In this case we rewrite the declaration to make the subprogram
2878 -- non-abstract. We remove the previous declaration, and insert
2879 -- the new one at the point of the renaming, to prevent premature
2880 -- access to unfrozen types. The new declaration reuses the
2881 -- specification of the previous one, and must not be analyzed.
2883 pragma Assert
2884 (Is_Primitive (Entity (Nam))
2885 and then
2886 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
2887 declare
2888 Old_Decl : constant Node_Id :=
2889 Unit_Declaration_Node (Rename_Spec);
2890 New_Decl : constant Node_Id :=
2891 Make_Subprogram_Declaration (Sloc (N),
2892 Specification =>
2893 Relocate_Node (Specification (Old_Decl)));
2894 begin
2895 Remove (Old_Decl);
2896 Insert_After (N, New_Decl);
2897 Set_Is_Abstract_Subprogram (Rename_Spec, False);
2898 Set_Analyzed (New_Decl);
2899 end;
2900 end if;
2902 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
2904 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2905 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
2906 end if;
2908 Set_Convention (New_S, Convention (Rename_Spec));
2909 Check_Fully_Conformant (New_S, Rename_Spec);
2910 Set_Public_Status (New_S);
2912 -- The specification does not introduce new formals, but only
2913 -- repeats the formals of the original subprogram declaration.
2914 -- For cross-reference purposes, and for refactoring tools, we
2915 -- treat the formals of the renaming declaration as body formals.
2917 Reference_Body_Formals (Rename_Spec, New_S);
2919 -- Indicate that the entity in the declaration functions like the
2920 -- corresponding body, and is not a new entity. The body will be
2921 -- constructed later at the freeze point, so indicate that the
2922 -- completion has not been seen yet.
2924 Set_Ekind (New_S, E_Subprogram_Body);
2925 New_S := Rename_Spec;
2926 Set_Has_Completion (Rename_Spec, False);
2928 -- Ada 2005: check overriding indicator
2930 if Present (Overridden_Operation (Rename_Spec)) then
2931 if Must_Not_Override (Specification (N)) then
2932 Error_Msg_NE
2933 ("subprogram& overrides inherited operation",
2934 N, Rename_Spec);
2935 elsif
2936 Style_Check and then not Must_Override (Specification (N))
2937 then
2938 Style.Missing_Overriding (N, Rename_Spec);
2939 end if;
2941 elsif Must_Override (Specification (N)) then
2942 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
2943 end if;
2945 -- Normal subprogram renaming (not renaming as body)
2947 else
2948 Generate_Definition (New_S);
2949 New_Overloaded_Entity (New_S);
2951 if Is_Entity_Name (Nam)
2952 and then Is_Intrinsic_Subprogram (Entity (Nam))
2953 then
2954 null;
2955 else
2956 Check_Delayed_Subprogram (New_S);
2957 end if;
2958 end if;
2960 -- There is no need for elaboration checks on the new entity, which may
2961 -- be called before the next freezing point where the body will appear.
2962 -- Elaboration checks refer to the real entity, not the one created by
2963 -- the renaming declaration.
2965 Set_Kill_Elaboration_Checks (New_S, True);
2967 -- If we had a previous error, indicate a completely is present to stop
2968 -- junk cascaded messages, but don't take any further action.
2970 if Etype (Nam) = Any_Type then
2971 Set_Has_Completion (New_S);
2972 return;
2974 -- Case where name has the form of a selected component
2976 elsif Nkind (Nam) = N_Selected_Component then
2978 -- A name which has the form A.B can designate an entry of task A, a
2979 -- protected operation of protected object A, or finally a primitive
2980 -- operation of object A. In the later case, A is an object of some
2981 -- tagged type, or an access type that denotes one such. To further
2982 -- distinguish these cases, note that the scope of a task entry or
2983 -- protected operation is type of the prefix.
2985 -- The prefix could be an overloaded function call that returns both
2986 -- kinds of operations. This overloading pathology is left to the
2987 -- dedicated reader ???
2989 declare
2990 T : constant Entity_Id := Etype (Prefix (Nam));
2992 begin
2993 if Present (T)
2994 and then
2995 (Is_Tagged_Type (T)
2996 or else
2997 (Is_Access_Type (T)
2998 and then Is_Tagged_Type (Designated_Type (T))))
2999 and then Scope (Entity (Selector_Name (Nam))) /= T
3000 then
3001 Analyze_Renamed_Primitive_Operation
3002 (N, New_S, Present (Rename_Spec));
3003 return;
3005 else
3006 -- Renamed entity is an entry or protected operation. For those
3007 -- cases an explicit body is built (at the point of freezing of
3008 -- this entity) that contains a call to the renamed entity.
3010 -- This is not allowed for renaming as body if the renamed
3011 -- spec is already frozen (see RM 8.5.4(5) for details).
3013 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
3014 Error_Msg_N
3015 ("renaming-as-body cannot rename entry as subprogram", N);
3016 Error_Msg_NE
3017 ("\since & is already frozen (RM 8.5.4(5))",
3018 N, Rename_Spec);
3019 else
3020 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
3021 end if;
3023 return;
3024 end if;
3025 end;
3027 -- Case where name is an explicit dereference X.all
3029 elsif Nkind (Nam) = N_Explicit_Dereference then
3031 -- Renamed entity is designated by access_to_subprogram expression.
3032 -- Must build body to encapsulate call, as in the entry case.
3034 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
3035 return;
3037 -- Indexed component
3039 elsif Nkind (Nam) = N_Indexed_Component then
3040 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
3041 return;
3043 -- Character literal
3045 elsif Nkind (Nam) = N_Character_Literal then
3046 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
3047 return;
3049 -- Only remaining case is where we have a non-entity name, or a renaming
3050 -- of some other non-overloadable entity.
3052 elsif not Is_Entity_Name (Nam)
3053 or else not Is_Overloadable (Entity (Nam))
3054 then
3055 -- Do not mention the renaming if it comes from an instance
3057 if not Is_Actual then
3058 Error_Msg_N ("expect valid subprogram name in renaming", N);
3059 else
3060 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
3061 end if;
3063 return;
3064 end if;
3066 -- Find the renamed entity that matches the given specification. Disable
3067 -- Ada_83 because there is no requirement of full conformance between
3068 -- renamed entity and new entity, even though the same circuit is used.
3070 -- This is a bit of an odd case, which introduces a really irregular use
3071 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
3072 -- this. ???
3074 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
3075 Ada_Version_Pragma := Empty;
3076 Ada_Version_Explicit := Ada_Version;
3078 if No (Old_S) then
3079 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3081 -- The visible operation may be an inherited abstract operation that
3082 -- was overridden in the private part, in which case a call will
3083 -- dispatch to the overriding operation. Use the overriding one in
3084 -- the renaming declaration, to prevent spurious errors below.
3086 if Is_Overloadable (Old_S)
3087 and then Is_Abstract_Subprogram (Old_S)
3088 and then No (DTC_Entity (Old_S))
3089 and then Present (Alias (Old_S))
3090 and then not Is_Abstract_Subprogram (Alias (Old_S))
3091 and then Present (Overridden_Operation (Alias (Old_S)))
3092 then
3093 Old_S := Alias (Old_S);
3094 end if;
3096 -- When the renamed subprogram is overloaded and used as an actual
3097 -- of a generic, its entity is set to the first available homonym.
3098 -- We must first disambiguate the name, then set the proper entity.
3100 if Is_Actual and then Is_Overloaded (Nam) then
3101 Set_Entity (Nam, Old_S);
3102 end if;
3103 end if;
3105 -- Most common case: subprogram renames subprogram. No body is generated
3106 -- in this case, so we must indicate the declaration is complete as is.
3107 -- and inherit various attributes of the renamed subprogram.
3109 if No (Rename_Spec) then
3110 Set_Has_Completion (New_S);
3111 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3112 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3113 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3115 -- The subprogram renaming declaration may become Ghost if it renames
3116 -- a Ghost entity.
3118 Mark_Renaming_As_Ghost (N, Entity (Nam));
3120 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3121 -- between a subprogram and its correct renaming.
3123 -- Note: the Any_Id check is a guard that prevents compiler crashes
3124 -- when performing a null exclusion check between a renaming and a
3125 -- renamed subprogram that has been found to be illegal.
3127 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3128 Check_Null_Exclusion
3129 (Ren => New_S,
3130 Sub => Entity (Nam));
3131 end if;
3133 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3134 -- overriding. The flag Requires_Overriding is set very selectively
3135 -- and misses some other illegal cases. The additional conditions
3136 -- checked below are sufficient but not necessary ???
3138 -- The rule does not apply to the renaming generated for an actual
3139 -- subprogram in an instance.
3141 if Is_Actual then
3142 null;
3144 -- Guard against previous errors, and omit renamings of predefined
3145 -- operators.
3147 elsif not Ekind_In (Old_S, E_Function, E_Procedure) then
3148 null;
3150 elsif Requires_Overriding (Old_S)
3151 or else
3152 (Is_Abstract_Subprogram (Old_S)
3153 and then Present (Find_Dispatching_Type (Old_S))
3154 and then
3155 not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3156 then
3157 Error_Msg_N
3158 ("renamed entity cannot be "
3159 & "subprogram that requires overriding (RM 8.5.4 (5.1))", N);
3160 end if;
3161 end if;
3163 if Old_S /= Any_Id then
3164 if Is_Actual and then From_Default (N) then
3166 -- This is an implicit reference to the default actual
3168 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3170 else
3171 Generate_Reference (Old_S, Nam);
3172 end if;
3174 Check_Internal_Protected_Use (N, Old_S);
3176 -- For a renaming-as-body, require subtype conformance, but if the
3177 -- declaration being completed has not been frozen, then inherit the
3178 -- convention of the renamed subprogram prior to checking conformance
3179 -- (unless the renaming has an explicit convention established; the
3180 -- rule stated in the RM doesn't seem to address this ???).
3182 if Present (Rename_Spec) then
3183 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3184 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3186 if not Is_Frozen (Rename_Spec) then
3187 if not Has_Convention_Pragma (Rename_Spec) then
3188 Set_Convention (New_S, Convention (Old_S));
3189 end if;
3191 if Ekind (Old_S) /= E_Operator then
3192 Check_Mode_Conformant (New_S, Old_S, Spec);
3193 end if;
3195 if Original_Subprogram (Old_S) = Rename_Spec then
3196 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
3197 end if;
3198 else
3199 Check_Subtype_Conformant (New_S, Old_S, Spec);
3200 end if;
3202 Check_Frozen_Renaming (N, Rename_Spec);
3204 -- Check explicitly that renamed entity is not intrinsic, because
3205 -- in a generic the renamed body is not built. In this case,
3206 -- the renaming_as_body is a completion.
3208 if Inside_A_Generic then
3209 if Is_Frozen (Rename_Spec)
3210 and then Is_Intrinsic_Subprogram (Old_S)
3211 then
3212 Error_Msg_N
3213 ("subprogram in renaming_as_body cannot be intrinsic",
3214 Name (N));
3215 end if;
3217 Set_Has_Completion (Rename_Spec);
3218 end if;
3220 elsif Ekind (Old_S) /= E_Operator then
3222 -- If this a defaulted subprogram for a class-wide actual there is
3223 -- no check for mode conformance, given that the signatures don't
3224 -- match (the source mentions T but the actual mentions T'Class).
3226 if CW_Actual then
3227 null;
3228 elsif not Is_Actual or else No (Enclosing_Instance) then
3229 Check_Mode_Conformant (New_S, Old_S);
3230 end if;
3232 if Is_Actual and then Error_Posted (New_S) then
3233 Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
3234 end if;
3235 end if;
3237 if No (Rename_Spec) then
3239 -- The parameter profile of the new entity is that of the renamed
3240 -- entity: the subtypes given in the specification are irrelevant.
3242 Inherit_Renamed_Profile (New_S, Old_S);
3244 -- A call to the subprogram is transformed into a call to the
3245 -- renamed entity. This is transitive if the renamed entity is
3246 -- itself a renaming.
3248 if Present (Alias (Old_S)) then
3249 Set_Alias (New_S, Alias (Old_S));
3250 else
3251 Set_Alias (New_S, Old_S);
3252 end if;
3254 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3255 -- renaming as body, since the entity in this case is not an
3256 -- intrinsic (it calls an intrinsic, but we have a real body for
3257 -- this call, and it is in this body that the required intrinsic
3258 -- processing will take place).
3260 -- Also, if this is a renaming of inequality, the renamed operator
3261 -- is intrinsic, but what matters is the corresponding equality
3262 -- operator, which may be user-defined.
3264 Set_Is_Intrinsic_Subprogram
3265 (New_S,
3266 Is_Intrinsic_Subprogram (Old_S)
3267 and then
3268 (Chars (Old_S) /= Name_Op_Ne
3269 or else Ekind (Old_S) = E_Operator
3270 or else Is_Intrinsic_Subprogram
3271 (Corresponding_Equality (Old_S))));
3273 if Ekind (Alias (New_S)) = E_Operator then
3274 Set_Has_Delayed_Freeze (New_S, False);
3275 end if;
3277 -- If the renaming corresponds to an association for an abstract
3278 -- formal subprogram, then various attributes must be set to
3279 -- indicate that the renaming is an abstract dispatching operation
3280 -- with a controlling type.
3282 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
3284 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3285 -- see it as corresponding to a generic association for a
3286 -- formal abstract subprogram
3288 Set_Is_Abstract_Subprogram (New_S);
3290 declare
3291 New_S_Ctrl_Type : constant Entity_Id :=
3292 Find_Dispatching_Type (New_S);
3293 Old_S_Ctrl_Type : constant Entity_Id :=
3294 Find_Dispatching_Type (Old_S);
3296 begin
3297 if Old_S_Ctrl_Type /= New_S_Ctrl_Type then
3298 Error_Msg_NE
3299 ("actual must be dispatching subprogram for type&",
3300 Nam, New_S_Ctrl_Type);
3302 else
3303 Set_Is_Dispatching_Operation (New_S);
3304 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3306 -- If the actual in the formal subprogram is itself a
3307 -- formal abstract subprogram association, there's no
3308 -- dispatch table component or position to inherit.
3310 if Present (DTC_Entity (Old_S)) then
3311 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3312 Set_DT_Position_Value (New_S, DT_Position (Old_S));
3313 end if;
3314 end if;
3315 end;
3316 end if;
3317 end if;
3319 if Is_Actual then
3320 null;
3322 -- The following is illegal, because F hides whatever other F may
3323 -- be around:
3324 -- function F (...) renames F;
3326 elsif Old_S = New_S
3327 or else (Nkind (Nam) /= N_Expanded_Name
3328 and then Chars (Old_S) = Chars (New_S))
3329 then
3330 Error_Msg_N ("subprogram cannot rename itself", N);
3332 -- This is illegal even if we use a selector:
3333 -- function F (...) renames Pkg.F;
3334 -- because F is still hidden.
3336 elsif Nkind (Nam) = N_Expanded_Name
3337 and then Entity (Prefix (Nam)) = Current_Scope
3338 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3339 then
3340 -- This is an error, but we overlook the error and accept the
3341 -- renaming if the special Overriding_Renamings mode is in effect.
3343 if not Overriding_Renamings then
3344 Error_Msg_NE
3345 ("implicit operation& is not visible (RM 8.3 (15))",
3346 Nam, Old_S);
3347 end if;
3348 end if;
3350 Set_Convention (New_S, Convention (Old_S));
3352 if Is_Abstract_Subprogram (Old_S) then
3353 if Present (Rename_Spec) then
3354 Error_Msg_N
3355 ("a renaming-as-body cannot rename an abstract subprogram",
3357 Set_Has_Completion (Rename_Spec);
3358 else
3359 Set_Is_Abstract_Subprogram (New_S);
3360 end if;
3361 end if;
3363 Check_Library_Unit_Renaming (N, Old_S);
3365 -- Pathological case: procedure renames entry in the scope of its
3366 -- task. Entry is given by simple name, but body must be built for
3367 -- procedure. Of course if called it will deadlock.
3369 if Ekind (Old_S) = E_Entry then
3370 Set_Has_Completion (New_S, False);
3371 Set_Alias (New_S, Empty);
3372 end if;
3374 if Is_Actual then
3375 Freeze_Before (N, Old_S);
3376 Freeze_Actual_Profile;
3377 Set_Has_Delayed_Freeze (New_S, False);
3378 Freeze_Before (N, New_S);
3380 -- An abstract subprogram is only allowed as an actual in the case
3381 -- where the formal subprogram is also abstract.
3383 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
3384 and then Is_Abstract_Subprogram (Old_S)
3385 and then not Is_Abstract_Subprogram (Formal_Spec)
3386 then
3387 Error_Msg_N
3388 ("abstract subprogram not allowed as generic actual", Nam);
3389 end if;
3390 end if;
3392 else
3393 -- A common error is to assume that implicit operators for types are
3394 -- defined in Standard, or in the scope of a subtype. In those cases
3395 -- where the renamed entity is given with an expanded name, it is
3396 -- worth mentioning that operators for the type are not declared in
3397 -- the scope given by the prefix.
3399 if Nkind (Nam) = N_Expanded_Name
3400 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
3401 and then Scope (Entity (Nam)) = Standard_Standard
3402 then
3403 declare
3404 T : constant Entity_Id :=
3405 Base_Type (Etype (First_Formal (New_S)));
3406 begin
3407 Error_Msg_Node_2 := Prefix (Nam);
3408 Error_Msg_NE
3409 ("operator for type& is not declared in&", Prefix (Nam), T);
3410 end;
3412 else
3413 Error_Msg_NE
3414 ("no visible subprogram matches the specification for&",
3415 Spec, New_S);
3416 end if;
3418 if Present (Candidate_Renaming) then
3419 declare
3420 F1 : Entity_Id;
3421 F2 : Entity_Id;
3422 T1 : Entity_Id;
3424 begin
3425 F1 := First_Formal (Candidate_Renaming);
3426 F2 := First_Formal (New_S);
3427 T1 := First_Subtype (Etype (F1));
3428 while Present (F1) and then Present (F2) loop
3429 Next_Formal (F1);
3430 Next_Formal (F2);
3431 end loop;
3433 if Present (F1) and then Present (Default_Value (F1)) then
3434 if Present (Next_Formal (F1)) then
3435 Error_Msg_NE
3436 ("\missing specification for & and other formals with "
3437 & "defaults", Spec, F1);
3438 else
3439 Error_Msg_NE ("\missing specification for &", Spec, F1);
3440 end if;
3441 end if;
3443 if Nkind (Nam) = N_Operator_Symbol
3444 and then From_Default (N)
3445 then
3446 Error_Msg_Node_2 := T1;
3447 Error_Msg_NE
3448 ("default & on & is not directly visible",
3449 Nam, Nam);
3450 end if;
3451 end;
3452 end if;
3453 end if;
3455 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
3456 -- controlling access parameters are known non-null for the renamed
3457 -- subprogram. Test also applies to a subprogram instantiation that
3458 -- is dispatching. Test is skipped if some previous error was detected
3459 -- that set Old_S to Any_Id.
3461 if Ada_Version >= Ada_2005
3462 and then Old_S /= Any_Id
3463 and then not Is_Dispatching_Operation (Old_S)
3464 and then Is_Dispatching_Operation (New_S)
3465 then
3466 declare
3467 Old_F : Entity_Id;
3468 New_F : Entity_Id;
3470 begin
3471 Old_F := First_Formal (Old_S);
3472 New_F := First_Formal (New_S);
3473 while Present (Old_F) loop
3474 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
3475 and then Is_Controlling_Formal (New_F)
3476 and then not Can_Never_Be_Null (Old_F)
3477 then
3478 Error_Msg_N ("access parameter is controlling,", New_F);
3479 Error_Msg_NE
3480 ("\corresponding parameter of& "
3481 & "must be explicitly null excluding", New_F, Old_S);
3482 end if;
3484 Next_Formal (Old_F);
3485 Next_Formal (New_F);
3486 end loop;
3487 end;
3488 end if;
3490 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
3491 -- is to warn if an operator is being renamed as a different operator.
3492 -- If the operator is predefined, examine the kind of the entity, not
3493 -- the abbreviated declaration in Standard.
3495 if Comes_From_Source (N)
3496 and then Present (Old_S)
3497 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3498 or else Ekind (Old_S) = E_Operator)
3499 and then Nkind (New_S) = N_Defining_Operator_Symbol
3500 and then Chars (Old_S) /= Chars (New_S)
3501 then
3502 Error_Msg_NE
3503 ("& is being renamed as a different operator??", N, Old_S);
3504 end if;
3506 -- Check for renaming of obsolescent subprogram
3508 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3510 -- Another warning or some utility: if the new subprogram as the same
3511 -- name as the old one, the old one is not hidden by an outer homograph,
3512 -- the new one is not a public symbol, and the old one is otherwise
3513 -- directly visible, the renaming is superfluous.
3515 if Chars (Old_S) = Chars (New_S)
3516 and then Comes_From_Source (N)
3517 and then Scope (Old_S) /= Standard_Standard
3518 and then Warn_On_Redundant_Constructs
3519 and then (Is_Immediately_Visible (Old_S)
3520 or else Is_Potentially_Use_Visible (Old_S))
3521 and then Is_Overloadable (Current_Scope)
3522 and then Chars (Current_Scope) /= Chars (Old_S)
3523 then
3524 Error_Msg_N
3525 ("redundant renaming, entity is directly visible?r?", Name (N));
3526 end if;
3528 -- Implementation-defined aspect specifications can appear in a renaming
3529 -- declaration, but not language-defined ones. The call to procedure
3530 -- Analyze_Aspect_Specifications will take care of this error check.
3532 if Has_Aspects (N) then
3533 Analyze_Aspect_Specifications (N, New_S);
3534 end if;
3536 Ada_Version := Save_AV;
3537 Ada_Version_Pragma := Save_AVP;
3538 Ada_Version_Explicit := Save_AV_Exp;
3540 -- In GNATprove mode, the renamings of actual subprograms are replaced
3541 -- with wrapper functions that make it easier to propagate axioms to the
3542 -- points of call within an instance. Wrappers are generated if formal
3543 -- subprogram is subject to axiomatization.
3545 -- The types in the wrapper profiles are obtained from (instances of)
3546 -- the types of the formal subprogram.
3548 if Is_Actual
3549 and then GNATprove_Mode
3550 and then Present (Containing_Package_With_Ext_Axioms (Formal_Spec))
3551 and then not Inside_A_Generic
3552 then
3553 if Ekind (Old_S) = E_Function then
3554 Rewrite (N, Build_Function_Wrapper (Formal_Spec, Old_S));
3555 Analyze (N);
3557 elsif Ekind (Old_S) = E_Operator then
3558 Rewrite (N, Build_Operator_Wrapper (Formal_Spec, Old_S));
3559 Analyze (N);
3560 end if;
3561 end if;
3563 -- Restore the original Ghost mode once analysis and expansion have
3564 -- taken place.
3566 Ghost_Mode := GM;
3567 end Analyze_Subprogram_Renaming;
3569 -------------------------
3570 -- Analyze_Use_Package --
3571 -------------------------
3573 -- Resolve the package names in the use clause, and make all the visible
3574 -- entities defined in the package potentially use-visible. If the package
3575 -- is already in use from a previous use clause, its visible entities are
3576 -- already use-visible. In that case, mark the occurrence as a redundant
3577 -- use. If the package is an open scope, i.e. if the use clause occurs
3578 -- within the package itself, ignore it.
3580 procedure Analyze_Use_Package (N : Node_Id) is
3581 Pack_Name : Node_Id;
3582 Pack : Entity_Id;
3584 -- Start of processing for Analyze_Use_Package
3586 begin
3587 Check_SPARK_05_Restriction ("use clause is not allowed", N);
3589 Set_Hidden_By_Use_Clause (N, No_Elist);
3591 -- Use clause not allowed in a spec of a predefined package declaration
3592 -- except that packages whose file name starts a-n are OK (these are
3593 -- children of Ada.Numerics, which are never loaded by Rtsfind).
3595 if Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
3596 and then Name_Buffer (1 .. 3) /= "a-n"
3597 and then
3598 Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3599 then
3600 Error_Msg_N ("use clause not allowed in predefined spec", N);
3601 end if;
3603 -- Chain clause to list of use clauses in current scope
3605 if Nkind (Parent (N)) /= N_Compilation_Unit then
3606 Chain_Use_Clause (N);
3607 end if;
3609 -- Loop through package names to identify referenced packages
3611 Pack_Name := First (Names (N));
3612 while Present (Pack_Name) loop
3613 Analyze (Pack_Name);
3615 if Nkind (Parent (N)) = N_Compilation_Unit
3616 and then Nkind (Pack_Name) = N_Expanded_Name
3617 then
3618 declare
3619 Pref : Node_Id;
3621 begin
3622 Pref := Prefix (Pack_Name);
3623 while Nkind (Pref) = N_Expanded_Name loop
3624 Pref := Prefix (Pref);
3625 end loop;
3627 if Entity (Pref) = Standard_Standard then
3628 Error_Msg_N
3629 ("predefined package Standard cannot appear"
3630 & " in a context clause", Pref);
3631 end if;
3632 end;
3633 end if;
3635 Next (Pack_Name);
3636 end loop;
3638 -- Loop through package names to mark all entities as potentially
3639 -- use visible.
3641 Pack_Name := First (Names (N));
3642 while Present (Pack_Name) loop
3643 if Is_Entity_Name (Pack_Name) then
3644 Pack := Entity (Pack_Name);
3646 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
3647 if Ekind (Pack) = E_Generic_Package then
3648 Error_Msg_N -- CODEFIX
3649 ("a generic package is not allowed in a use clause",
3650 Pack_Name);
3652 elsif Ekind_In (Pack, E_Generic_Function, E_Generic_Package)
3653 then
3654 Error_Msg_N -- CODEFIX
3655 ("a generic subprogram is not allowed in a use clause",
3656 Pack_Name);
3658 elsif Ekind_In (Pack, E_Function, E_Procedure, E_Operator) then
3659 Error_Msg_N -- CODEFIX
3660 ("a subprogram is not allowed in a use clause",
3661 Pack_Name);
3663 else
3664 Error_Msg_N ("& is not allowed in a use clause", Pack_Name);
3665 end if;
3667 else
3668 if Nkind (Parent (N)) = N_Compilation_Unit then
3669 Check_In_Previous_With_Clause (N, Pack_Name);
3670 end if;
3672 if Applicable_Use (Pack_Name) then
3673 Use_One_Package (Pack, N);
3674 end if;
3675 end if;
3677 -- Report error because name denotes something other than a package
3679 else
3680 Error_Msg_N ("& is not a package", Pack_Name);
3681 end if;
3683 Next (Pack_Name);
3684 end loop;
3685 end Analyze_Use_Package;
3687 ----------------------
3688 -- Analyze_Use_Type --
3689 ----------------------
3691 procedure Analyze_Use_Type (N : Node_Id) is
3692 E : Entity_Id;
3693 Id : Node_Id;
3695 begin
3696 Set_Hidden_By_Use_Clause (N, No_Elist);
3698 -- Chain clause to list of use clauses in current scope
3700 if Nkind (Parent (N)) /= N_Compilation_Unit then
3701 Chain_Use_Clause (N);
3702 end if;
3704 -- If the Used_Operations list is already initialized, the clause has
3705 -- been analyzed previously, and it is begin reinstalled, for example
3706 -- when the clause appears in a package spec and we are compiling the
3707 -- corresponding package body. In that case, make the entities on the
3708 -- existing list use_visible, and mark the corresponding types In_Use.
3710 if Present (Used_Operations (N)) then
3711 declare
3712 Mark : Node_Id;
3713 Elmt : Elmt_Id;
3715 begin
3716 Mark := First (Subtype_Marks (N));
3717 while Present (Mark) loop
3718 Use_One_Type (Mark, Installed => True);
3719 Next (Mark);
3720 end loop;
3722 Elmt := First_Elmt (Used_Operations (N));
3723 while Present (Elmt) loop
3724 Set_Is_Potentially_Use_Visible (Node (Elmt));
3725 Next_Elmt (Elmt);
3726 end loop;
3727 end;
3729 return;
3730 end if;
3732 -- Otherwise, create new list and attach to it the operations that
3733 -- are made use-visible by the clause.
3735 Set_Used_Operations (N, New_Elmt_List);
3736 Id := First (Subtype_Marks (N));
3737 while Present (Id) loop
3738 Find_Type (Id);
3739 E := Entity (Id);
3741 if E /= Any_Type then
3742 Use_One_Type (Id);
3744 if Nkind (Parent (N)) = N_Compilation_Unit then
3745 if Nkind (Id) = N_Identifier then
3746 Error_Msg_N ("type is not directly visible", Id);
3748 elsif Is_Child_Unit (Scope (E))
3749 and then Scope (E) /= System_Aux_Id
3750 then
3751 Check_In_Previous_With_Clause (N, Prefix (Id));
3752 end if;
3753 end if;
3755 else
3756 -- If the use_type_clause appears in a compilation unit context,
3757 -- check whether it comes from a unit that may appear in a
3758 -- limited_with_clause, for a better error message.
3760 if Nkind (Parent (N)) = N_Compilation_Unit
3761 and then Nkind (Id) /= N_Identifier
3762 then
3763 declare
3764 Item : Node_Id;
3765 Pref : Node_Id;
3767 function Mentioned (Nam : Node_Id) return Boolean;
3768 -- Check whether the prefix of expanded name for the type
3769 -- appears in the prefix of some limited_with_clause.
3771 ---------------
3772 -- Mentioned --
3773 ---------------
3775 function Mentioned (Nam : Node_Id) return Boolean is
3776 begin
3777 return Nkind (Name (Item)) = N_Selected_Component
3778 and then Chars (Prefix (Name (Item))) = Chars (Nam);
3779 end Mentioned;
3781 begin
3782 Pref := Prefix (Id);
3783 Item := First (Context_Items (Parent (N)));
3784 while Present (Item) and then Item /= N loop
3785 if Nkind (Item) = N_With_Clause
3786 and then Limited_Present (Item)
3787 and then Mentioned (Pref)
3788 then
3789 Change_Error_Text
3790 (Get_Msg_Id, "premature usage of incomplete type");
3791 end if;
3793 Next (Item);
3794 end loop;
3795 end;
3796 end if;
3797 end if;
3799 Next (Id);
3800 end loop;
3801 end Analyze_Use_Type;
3803 --------------------
3804 -- Applicable_Use --
3805 --------------------
3807 function Applicable_Use (Pack_Name : Node_Id) return Boolean is
3808 Pack : constant Entity_Id := Entity (Pack_Name);
3810 begin
3811 if In_Open_Scopes (Pack) then
3812 if Warn_On_Redundant_Constructs and then Pack = Current_Scope then
3813 Error_Msg_NE -- CODEFIX
3814 ("& is already use-visible within itself?r?", Pack_Name, Pack);
3815 end if;
3817 return False;
3819 elsif In_Use (Pack) then
3820 Note_Redundant_Use (Pack_Name);
3821 return False;
3823 elsif Present (Renamed_Object (Pack))
3824 and then In_Use (Renamed_Object (Pack))
3825 then
3826 Note_Redundant_Use (Pack_Name);
3827 return False;
3829 else
3830 return True;
3831 end if;
3832 end Applicable_Use;
3834 ------------------------
3835 -- Attribute_Renaming --
3836 ------------------------
3838 procedure Attribute_Renaming (N : Node_Id) is
3839 Loc : constant Source_Ptr := Sloc (N);
3840 Nam : constant Node_Id := Name (N);
3841 Spec : constant Node_Id := Specification (N);
3842 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
3843 Aname : constant Name_Id := Attribute_Name (Nam);
3845 Form_Num : Nat := 0;
3846 Expr_List : List_Id := No_List;
3848 Attr_Node : Node_Id;
3849 Body_Node : Node_Id;
3850 Param_Spec : Node_Id;
3852 begin
3853 Generate_Definition (New_S);
3855 -- This procedure is called in the context of subprogram renaming, and
3856 -- thus the attribute must be one that is a subprogram. All of those
3857 -- have at least one formal parameter, with the exceptions of the GNAT
3858 -- attribute 'Img, which GNAT treats as renameable.
3860 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
3861 if Aname /= Name_Img then
3862 Error_Msg_N
3863 ("subprogram renaming an attribute must have formals", N);
3864 return;
3865 end if;
3867 else
3868 Param_Spec := First (Parameter_Specifications (Spec));
3869 while Present (Param_Spec) loop
3870 Form_Num := Form_Num + 1;
3872 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
3873 Find_Type (Parameter_Type (Param_Spec));
3875 -- The profile of the new entity denotes the base type (s) of
3876 -- the types given in the specification. For access parameters
3877 -- there are no subtypes involved.
3879 Rewrite (Parameter_Type (Param_Spec),
3880 New_Occurrence_Of
3881 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
3882 end if;
3884 if No (Expr_List) then
3885 Expr_List := New_List;
3886 end if;
3888 Append_To (Expr_List,
3889 Make_Identifier (Loc,
3890 Chars => Chars (Defining_Identifier (Param_Spec))));
3892 -- The expressions in the attribute reference are not freeze
3893 -- points. Neither is the attribute as a whole, see below.
3895 Set_Must_Not_Freeze (Last (Expr_List));
3896 Next (Param_Spec);
3897 end loop;
3898 end if;
3900 -- Immediate error if too many formals. Other mismatches in number or
3901 -- types of parameters are detected when we analyze the body of the
3902 -- subprogram that we construct.
3904 if Form_Num > 2 then
3905 Error_Msg_N ("too many formals for attribute", N);
3907 -- Error if the attribute reference has expressions that look like
3908 -- formal parameters.
3910 elsif Present (Expressions (Nam)) then
3911 Error_Msg_N ("illegal expressions in attribute reference", Nam);
3913 elsif
3914 Nam_In (Aname, Name_Compose, Name_Exponent, Name_Leading_Part,
3915 Name_Pos, Name_Round, Name_Scaling,
3916 Name_Val)
3917 then
3918 if Nkind (N) = N_Subprogram_Renaming_Declaration
3919 and then Present (Corresponding_Formal_Spec (N))
3920 then
3921 Error_Msg_N
3922 ("generic actual cannot be attribute involving universal type",
3923 Nam);
3924 else
3925 Error_Msg_N
3926 ("attribute involving a universal type cannot be renamed",
3927 Nam);
3928 end if;
3929 end if;
3931 -- Rewrite attribute node to have a list of expressions corresponding to
3932 -- the subprogram formals. A renaming declaration is not a freeze point,
3933 -- and the analysis of the attribute reference should not freeze the
3934 -- type of the prefix. We use the original node in the renaming so that
3935 -- its source location is preserved, and checks on stream attributes are
3936 -- properly applied.
3938 Attr_Node := Relocate_Node (Nam);
3939 Set_Expressions (Attr_Node, Expr_List);
3941 Set_Must_Not_Freeze (Attr_Node);
3942 Set_Must_Not_Freeze (Prefix (Nam));
3944 -- Case of renaming a function
3946 if Nkind (Spec) = N_Function_Specification then
3947 if Is_Procedure_Attribute_Name (Aname) then
3948 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
3949 return;
3950 end if;
3952 Find_Type (Result_Definition (Spec));
3953 Rewrite (Result_Definition (Spec),
3954 New_Occurrence_Of
3955 (Base_Type (Entity (Result_Definition (Spec))), Loc));
3957 Body_Node :=
3958 Make_Subprogram_Body (Loc,
3959 Specification => Spec,
3960 Declarations => New_List,
3961 Handled_Statement_Sequence =>
3962 Make_Handled_Sequence_Of_Statements (Loc,
3963 Statements => New_List (
3964 Make_Simple_Return_Statement (Loc,
3965 Expression => Attr_Node))));
3967 -- Case of renaming a procedure
3969 else
3970 if not Is_Procedure_Attribute_Name (Aname) then
3971 Error_Msg_N ("attribute can only be renamed as function", Nam);
3972 return;
3973 end if;
3975 Body_Node :=
3976 Make_Subprogram_Body (Loc,
3977 Specification => Spec,
3978 Declarations => New_List,
3979 Handled_Statement_Sequence =>
3980 Make_Handled_Sequence_Of_Statements (Loc,
3981 Statements => New_List (Attr_Node)));
3982 end if;
3984 -- In case of tagged types we add the body of the generated function to
3985 -- the freezing actions of the type (because in the general case such
3986 -- type is still not frozen). We exclude from this processing generic
3987 -- formal subprograms found in instantiations.
3989 -- We must exclude VM targets and restricted run-time libraries because
3990 -- entity AST_Handler is defined in package System.Aux_Dec which is not
3991 -- available in those platforms. Note that we cannot use the function
3992 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
3993 -- the ZFP run-time library is not defined as a profile, and we do not
3994 -- want to deal with AST_Handler in ZFP mode.
3996 if VM_Target = No_VM
3997 and then not Configurable_Run_Time_Mode
3998 and then not Present (Corresponding_Formal_Spec (N))
3999 and then Etype (Nam) /= RTE (RE_AST_Handler)
4000 then
4001 declare
4002 P : constant Node_Id := Prefix (Nam);
4004 begin
4005 -- The prefix of 'Img is an object that is evaluated for each call
4006 -- of the function that renames it.
4008 if Aname = Name_Img then
4009 Preanalyze_And_Resolve (P);
4011 -- For all other attribute renamings, the prefix is a subtype
4013 else
4014 Find_Type (P);
4015 end if;
4017 -- If the target type is not yet frozen, add the body to the
4018 -- actions to be elaborated at freeze time.
4020 if Is_Tagged_Type (Etype (P))
4021 and then In_Open_Scopes (Scope (Etype (P)))
4022 then
4023 Ensure_Freeze_Node (Etype (P));
4024 Append_Freeze_Action (Etype (P), Body_Node);
4025 else
4026 Rewrite (N, Body_Node);
4027 Analyze (N);
4028 Set_Etype (New_S, Base_Type (Etype (New_S)));
4029 end if;
4030 end;
4032 -- Generic formal subprograms or AST_Handler renaming
4034 else
4035 Rewrite (N, Body_Node);
4036 Analyze (N);
4037 Set_Etype (New_S, Base_Type (Etype (New_S)));
4038 end if;
4040 if Is_Compilation_Unit (New_S) then
4041 Error_Msg_N
4042 ("a library unit can only rename another library unit", N);
4043 end if;
4045 -- We suppress elaboration warnings for the resulting entity, since
4046 -- clearly they are not needed, and more particularly, in the case
4047 -- of a generic formal subprogram, the resulting entity can appear
4048 -- after the instantiation itself, and thus look like a bogus case
4049 -- of access before elaboration.
4051 Set_Suppress_Elaboration_Warnings (New_S);
4053 end Attribute_Renaming;
4055 ----------------------
4056 -- Chain_Use_Clause --
4057 ----------------------
4059 procedure Chain_Use_Clause (N : Node_Id) is
4060 Pack : Entity_Id;
4061 Level : Int := Scope_Stack.Last;
4063 begin
4064 if not Is_Compilation_Unit (Current_Scope)
4065 or else not Is_Child_Unit (Current_Scope)
4066 then
4067 null; -- Common case
4069 elsif Defining_Entity (Parent (N)) = Current_Scope then
4070 null; -- Common case for compilation unit
4072 else
4073 -- If declaration appears in some other scope, it must be in some
4074 -- parent unit when compiling a child.
4076 Pack := Defining_Entity (Parent (N));
4077 if not In_Open_Scopes (Pack) then
4078 null; -- default as well
4080 -- If the use clause appears in an ancestor and we are in the
4081 -- private part of the immediate parent, the use clauses are
4082 -- already installed.
4084 elsif Pack /= Scope (Current_Scope)
4085 and then In_Private_Part (Scope (Current_Scope))
4086 then
4087 null;
4089 else
4090 -- Find entry for parent unit in scope stack
4092 while Scope_Stack.Table (Level).Entity /= Pack loop
4093 Level := Level - 1;
4094 end loop;
4095 end if;
4096 end if;
4098 Set_Next_Use_Clause (N,
4099 Scope_Stack.Table (Level).First_Use_Clause);
4100 Scope_Stack.Table (Level).First_Use_Clause := N;
4101 end Chain_Use_Clause;
4103 ---------------------------
4104 -- Check_Frozen_Renaming --
4105 ---------------------------
4107 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
4108 B_Node : Node_Id;
4109 Old_S : Entity_Id;
4111 begin
4112 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
4113 B_Node :=
4114 Build_Renamed_Body
4115 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
4117 if Is_Entity_Name (Name (N)) then
4118 Old_S := Entity (Name (N));
4120 if not Is_Frozen (Old_S)
4121 and then Operating_Mode /= Check_Semantics
4122 then
4123 Append_Freeze_Action (Old_S, B_Node);
4124 else
4125 Insert_After (N, B_Node);
4126 Analyze (B_Node);
4127 end if;
4129 if Is_Intrinsic_Subprogram (Old_S) and then not In_Instance then
4130 Error_Msg_N
4131 ("subprogram used in renaming_as_body cannot be intrinsic",
4132 Name (N));
4133 end if;
4135 else
4136 Insert_After (N, B_Node);
4137 Analyze (B_Node);
4138 end if;
4139 end if;
4140 end Check_Frozen_Renaming;
4142 -------------------------------
4143 -- Set_Entity_Or_Discriminal --
4144 -------------------------------
4146 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4147 P : Node_Id;
4149 begin
4150 -- If the entity is not a discriminant, or else expansion is disabled,
4151 -- simply set the entity.
4153 if not In_Spec_Expression
4154 or else Ekind (E) /= E_Discriminant
4155 or else Inside_A_Generic
4156 then
4157 Set_Entity_With_Checks (N, E);
4159 -- The replacement of a discriminant by the corresponding discriminal
4160 -- is not done for a task discriminant that appears in a default
4161 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4162 -- for details on their handling.
4164 elsif Is_Concurrent_Type (Scope (E)) then
4165 P := Parent (N);
4166 while Present (P)
4167 and then not Nkind_In (P, N_Parameter_Specification,
4168 N_Component_Declaration)
4169 loop
4170 P := Parent (P);
4171 end loop;
4173 if Present (P)
4174 and then Nkind (P) = N_Parameter_Specification
4175 then
4176 null;
4178 else
4179 Set_Entity (N, Discriminal (E));
4180 end if;
4182 -- Otherwise, this is a discriminant in a context in which
4183 -- it is a reference to the corresponding parameter of the
4184 -- init proc for the enclosing type.
4186 else
4187 Set_Entity (N, Discriminal (E));
4188 end if;
4189 end Set_Entity_Or_Discriminal;
4191 -----------------------------------
4192 -- Check_In_Previous_With_Clause --
4193 -----------------------------------
4195 procedure Check_In_Previous_With_Clause
4196 (N : Node_Id;
4197 Nam : Entity_Id)
4199 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4200 Item : Node_Id;
4201 Par : Node_Id;
4203 begin
4204 Item := First (Context_Items (Parent (N)));
4205 while Present (Item) and then Item /= N loop
4206 if Nkind (Item) = N_With_Clause
4208 -- Protect the frontend against previous critical errors
4210 and then Nkind (Name (Item)) /= N_Selected_Component
4211 and then Entity (Name (Item)) = Pack
4212 then
4213 Par := Nam;
4215 -- Find root library unit in with_clause
4217 while Nkind (Par) = N_Expanded_Name loop
4218 Par := Prefix (Par);
4219 end loop;
4221 if Is_Child_Unit (Entity (Original_Node (Par))) then
4222 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4223 else
4224 return;
4225 end if;
4226 end if;
4228 Next (Item);
4229 end loop;
4231 -- On exit, package is not mentioned in a previous with_clause.
4232 -- Check if its prefix is.
4234 if Nkind (Nam) = N_Expanded_Name then
4235 Check_In_Previous_With_Clause (N, Prefix (Nam));
4237 elsif Pack /= Any_Id then
4238 Error_Msg_NE ("& is not visible", Nam, Pack);
4239 end if;
4240 end Check_In_Previous_With_Clause;
4242 ---------------------------------
4243 -- Check_Library_Unit_Renaming --
4244 ---------------------------------
4246 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4247 New_E : Entity_Id;
4249 begin
4250 if Nkind (Parent (N)) /= N_Compilation_Unit then
4251 return;
4253 -- Check for library unit. Note that we used to check for the scope
4254 -- being Standard here, but that was wrong for Standard itself.
4256 elsif not Is_Compilation_Unit (Old_E)
4257 and then not Is_Child_Unit (Old_E)
4258 then
4259 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4261 -- Entities defined in Standard (operators and boolean literals) cannot
4262 -- be renamed as library units.
4264 elsif Scope (Old_E) = Standard_Standard
4265 and then Sloc (Old_E) = Standard_Location
4266 then
4267 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4269 elsif Present (Parent_Spec (N))
4270 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
4271 and then not Is_Child_Unit (Old_E)
4272 then
4273 Error_Msg_N
4274 ("renamed unit must be a child unit of generic parent", Name (N));
4276 elsif Nkind (N) in N_Generic_Renaming_Declaration
4277 and then Nkind (Name (N)) = N_Expanded_Name
4278 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
4279 and then Is_Generic_Unit (Old_E)
4280 then
4281 Error_Msg_N
4282 ("renamed generic unit must be a library unit", Name (N));
4284 elsif Is_Package_Or_Generic_Package (Old_E) then
4286 -- Inherit categorization flags
4288 New_E := Defining_Entity (N);
4289 Set_Is_Pure (New_E, Is_Pure (Old_E));
4290 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
4291 Set_Is_Remote_Call_Interface (New_E,
4292 Is_Remote_Call_Interface (Old_E));
4293 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
4294 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
4295 end if;
4296 end Check_Library_Unit_Renaming;
4298 ------------------------
4299 -- Enclosing_Instance --
4300 ------------------------
4302 function Enclosing_Instance return Entity_Id is
4303 S : Entity_Id;
4305 begin
4306 if not Is_Generic_Instance (Current_Scope) then
4307 return Empty;
4308 end if;
4310 S := Scope (Current_Scope);
4311 while S /= Standard_Standard loop
4312 if Is_Generic_Instance (S) then
4313 return S;
4314 end if;
4316 S := Scope (S);
4317 end loop;
4319 return Empty;
4320 end Enclosing_Instance;
4322 ---------------
4323 -- End_Scope --
4324 ---------------
4326 procedure End_Scope is
4327 Id : Entity_Id;
4328 Prev : Entity_Id;
4329 Outer : Entity_Id;
4331 begin
4332 Id := First_Entity (Current_Scope);
4333 while Present (Id) loop
4334 -- An entity in the current scope is not necessarily the first one
4335 -- on its homonym chain. Find its predecessor if any,
4336 -- If it is an internal entity, it will not be in the visibility
4337 -- chain altogether, and there is nothing to unchain.
4339 if Id /= Current_Entity (Id) then
4340 Prev := Current_Entity (Id);
4341 while Present (Prev)
4342 and then Present (Homonym (Prev))
4343 and then Homonym (Prev) /= Id
4344 loop
4345 Prev := Homonym (Prev);
4346 end loop;
4348 -- Skip to end of loop if Id is not in the visibility chain
4350 if No (Prev) or else Homonym (Prev) /= Id then
4351 goto Next_Ent;
4352 end if;
4354 else
4355 Prev := Empty;
4356 end if;
4358 Set_Is_Immediately_Visible (Id, False);
4360 Outer := Homonym (Id);
4361 while Present (Outer) and then Scope (Outer) = Current_Scope loop
4362 Outer := Homonym (Outer);
4363 end loop;
4365 -- Reset homonym link of other entities, but do not modify link
4366 -- between entities in current scope, so that the back-end can have
4367 -- a proper count of local overloadings.
4369 if No (Prev) then
4370 Set_Name_Entity_Id (Chars (Id), Outer);
4372 elsif Scope (Prev) /= Scope (Id) then
4373 Set_Homonym (Prev, Outer);
4374 end if;
4376 <<Next_Ent>>
4377 Next_Entity (Id);
4378 end loop;
4380 -- If the scope generated freeze actions, place them before the
4381 -- current declaration and analyze them. Type declarations and
4382 -- the bodies of initialization procedures can generate such nodes.
4383 -- We follow the parent chain until we reach a list node, which is
4384 -- the enclosing list of declarations. If the list appears within
4385 -- a protected definition, move freeze nodes outside the protected
4386 -- type altogether.
4388 if Present
4389 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
4390 then
4391 declare
4392 Decl : Node_Id;
4393 L : constant List_Id := Scope_Stack.Table
4394 (Scope_Stack.Last).Pending_Freeze_Actions;
4396 begin
4397 if Is_Itype (Current_Scope) then
4398 Decl := Associated_Node_For_Itype (Current_Scope);
4399 else
4400 Decl := Parent (Current_Scope);
4401 end if;
4403 Pop_Scope;
4405 while not (Is_List_Member (Decl))
4406 or else Nkind_In (Parent (Decl), N_Protected_Definition,
4407 N_Task_Definition)
4408 loop
4409 Decl := Parent (Decl);
4410 end loop;
4412 Insert_List_Before_And_Analyze (Decl, L);
4413 end;
4415 else
4416 Pop_Scope;
4417 end if;
4418 end End_Scope;
4420 ---------------------
4421 -- End_Use_Clauses --
4422 ---------------------
4424 procedure End_Use_Clauses (Clause : Node_Id) is
4425 U : Node_Id;
4427 begin
4428 -- Remove Use_Type clauses first, because they affect the
4429 -- visibility of operators in subsequent used packages.
4431 U := Clause;
4432 while Present (U) loop
4433 if Nkind (U) = N_Use_Type_Clause then
4434 End_Use_Type (U);
4435 end if;
4437 Next_Use_Clause (U);
4438 end loop;
4440 U := Clause;
4441 while Present (U) loop
4442 if Nkind (U) = N_Use_Package_Clause then
4443 End_Use_Package (U);
4444 end if;
4446 Next_Use_Clause (U);
4447 end loop;
4448 end End_Use_Clauses;
4450 ---------------------
4451 -- End_Use_Package --
4452 ---------------------
4454 procedure End_Use_Package (N : Node_Id) is
4455 Pack_Name : Node_Id;
4456 Pack : Entity_Id;
4457 Id : Entity_Id;
4458 Elmt : Elmt_Id;
4460 function Is_Primitive_Operator_In_Use
4461 (Op : Entity_Id;
4462 F : Entity_Id) return Boolean;
4463 -- Check whether Op is a primitive operator of a use-visible type
4465 ----------------------------------
4466 -- Is_Primitive_Operator_In_Use --
4467 ----------------------------------
4469 function Is_Primitive_Operator_In_Use
4470 (Op : Entity_Id;
4471 F : Entity_Id) return Boolean
4473 T : constant Entity_Id := Base_Type (Etype (F));
4474 begin
4475 return In_Use (T) and then Scope (T) = Scope (Op);
4476 end Is_Primitive_Operator_In_Use;
4478 -- Start of processing for End_Use_Package
4480 begin
4481 Pack_Name := First (Names (N));
4482 while Present (Pack_Name) loop
4484 -- Test that Pack_Name actually denotes a package before processing
4486 if Is_Entity_Name (Pack_Name)
4487 and then Ekind (Entity (Pack_Name)) = E_Package
4488 then
4489 Pack := Entity (Pack_Name);
4491 if In_Open_Scopes (Pack) then
4492 null;
4494 elsif not Redundant_Use (Pack_Name) then
4495 Set_In_Use (Pack, False);
4496 Set_Current_Use_Clause (Pack, Empty);
4498 Id := First_Entity (Pack);
4499 while Present (Id) loop
4501 -- Preserve use-visibility of operators that are primitive
4502 -- operators of a type that is use-visible through an active
4503 -- use_type clause.
4505 if Nkind (Id) = N_Defining_Operator_Symbol
4506 and then
4507 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
4508 or else
4509 (Present (Next_Formal (First_Formal (Id)))
4510 and then
4511 Is_Primitive_Operator_In_Use
4512 (Id, Next_Formal (First_Formal (Id)))))
4513 then
4514 null;
4515 else
4516 Set_Is_Potentially_Use_Visible (Id, False);
4517 end if;
4519 if Is_Private_Type (Id)
4520 and then Present (Full_View (Id))
4521 then
4522 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4523 end if;
4525 Next_Entity (Id);
4526 end loop;
4528 if Present (Renamed_Object (Pack)) then
4529 Set_In_Use (Renamed_Object (Pack), False);
4530 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
4531 end if;
4533 if Chars (Pack) = Name_System
4534 and then Scope (Pack) = Standard_Standard
4535 and then Present_System_Aux
4536 then
4537 Id := First_Entity (System_Aux_Id);
4538 while Present (Id) loop
4539 Set_Is_Potentially_Use_Visible (Id, False);
4541 if Is_Private_Type (Id)
4542 and then Present (Full_View (Id))
4543 then
4544 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4545 end if;
4547 Next_Entity (Id);
4548 end loop;
4550 Set_In_Use (System_Aux_Id, False);
4551 end if;
4553 else
4554 Set_Redundant_Use (Pack_Name, False);
4555 end if;
4556 end if;
4558 Next (Pack_Name);
4559 end loop;
4561 if Present (Hidden_By_Use_Clause (N)) then
4562 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
4563 while Present (Elmt) loop
4564 declare
4565 E : constant Entity_Id := Node (Elmt);
4567 begin
4568 -- Reset either Use_Visibility or Direct_Visibility, depending
4569 -- on how the entity was hidden by the use clause.
4571 if In_Use (Scope (E))
4572 and then Used_As_Generic_Actual (Scope (E))
4573 then
4574 Set_Is_Potentially_Use_Visible (Node (Elmt));
4575 else
4576 Set_Is_Immediately_Visible (Node (Elmt));
4577 end if;
4579 Next_Elmt (Elmt);
4580 end;
4581 end loop;
4583 Set_Hidden_By_Use_Clause (N, No_Elist);
4584 end if;
4585 end End_Use_Package;
4587 ------------------
4588 -- End_Use_Type --
4589 ------------------
4591 procedure End_Use_Type (N : Node_Id) is
4592 Elmt : Elmt_Id;
4593 Id : Entity_Id;
4594 T : Entity_Id;
4596 -- Start of processing for End_Use_Type
4598 begin
4599 Id := First (Subtype_Marks (N));
4600 while Present (Id) loop
4602 -- A call to Rtsfind may occur while analyzing a use_type clause,
4603 -- in which case the type marks are not resolved yet, and there is
4604 -- nothing to remove.
4606 if not Is_Entity_Name (Id) or else No (Entity (Id)) then
4607 goto Continue;
4608 end if;
4610 T := Entity (Id);
4612 if T = Any_Type or else From_Limited_With (T) then
4613 null;
4615 -- Note that the use_type clause may mention a subtype of the type
4616 -- whose primitive operations have been made visible. Here as
4617 -- elsewhere, it is the base type that matters for visibility.
4619 elsif In_Open_Scopes (Scope (Base_Type (T))) then
4620 null;
4622 elsif not Redundant_Use (Id) then
4623 Set_In_Use (T, False);
4624 Set_In_Use (Base_Type (T), False);
4625 Set_Current_Use_Clause (T, Empty);
4626 Set_Current_Use_Clause (Base_Type (T), Empty);
4627 end if;
4629 <<Continue>>
4630 Next (Id);
4631 end loop;
4633 if Is_Empty_Elmt_List (Used_Operations (N)) then
4634 return;
4636 else
4637 Elmt := First_Elmt (Used_Operations (N));
4638 while Present (Elmt) loop
4639 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
4640 Next_Elmt (Elmt);
4641 end loop;
4642 end if;
4643 end End_Use_Type;
4645 ----------------------
4646 -- Find_Direct_Name --
4647 ----------------------
4649 procedure Find_Direct_Name (N : Node_Id) is
4650 E : Entity_Id;
4651 E2 : Entity_Id;
4652 Msg : Boolean;
4654 Inst : Entity_Id := Empty;
4655 -- Enclosing instance, if any
4657 Homonyms : Entity_Id;
4658 -- Saves start of homonym chain
4660 Nvis_Entity : Boolean;
4661 -- Set True to indicate that there is at least one entity on the homonym
4662 -- chain which, while not visible, is visible enough from the user point
4663 -- of view to warrant an error message of "not visible" rather than
4664 -- undefined.
4666 Nvis_Is_Private_Subprg : Boolean := False;
4667 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
4668 -- effect concerning library subprograms has been detected. Used to
4669 -- generate the precise error message.
4671 function From_Actual_Package (E : Entity_Id) return Boolean;
4672 -- Returns true if the entity is an actual for a package that is itself
4673 -- an actual for a formal package of the current instance. Such an
4674 -- entity requires special handling because it may be use-visible but
4675 -- hides directly visible entities defined outside the instance, because
4676 -- the corresponding formal did so in the generic.
4678 function Is_Actual_Parameter return Boolean;
4679 -- This function checks if the node N is an identifier that is an actual
4680 -- parameter of a procedure call. If so it returns True, otherwise it
4681 -- return False. The reason for this check is that at this stage we do
4682 -- not know what procedure is being called if the procedure might be
4683 -- overloaded, so it is premature to go setting referenced flags or
4684 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
4685 -- for that processing
4687 function Known_But_Invisible (E : Entity_Id) return Boolean;
4688 -- This function determines whether a reference to the entity E, which
4689 -- is not visible, can reasonably be considered to be known to the
4690 -- writer of the reference. This is a heuristic test, used only for
4691 -- the purposes of figuring out whether we prefer to complain that an
4692 -- entity is undefined or invisible (and identify the declaration of
4693 -- the invisible entity in the latter case). The point here is that we
4694 -- don't want to complain that something is invisible and then point to
4695 -- something entirely mysterious to the writer.
4697 procedure Nvis_Messages;
4698 -- Called if there are no visible entries for N, but there is at least
4699 -- one non-directly visible, or hidden declaration. This procedure
4700 -- outputs an appropriate set of error messages.
4702 procedure Undefined (Nvis : Boolean);
4703 -- This function is called if the current node has no corresponding
4704 -- visible entity or entities. The value set in Msg indicates whether
4705 -- an error message was generated (multiple error messages for the
4706 -- same variable are generally suppressed, see body for details).
4707 -- Msg is True if an error message was generated, False if not. This
4708 -- value is used by the caller to determine whether or not to output
4709 -- additional messages where appropriate. The parameter is set False
4710 -- to get the message "X is undefined", and True to get the message
4711 -- "X is not visible".
4713 -------------------------
4714 -- From_Actual_Package --
4715 -------------------------
4717 function From_Actual_Package (E : Entity_Id) return Boolean is
4718 Scop : constant Entity_Id := Scope (E);
4719 -- Declared scope of candidate entity
4721 Act : Entity_Id;
4723 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
4724 -- Recursive function that does the work and examines actuals of
4725 -- actual packages of current instance.
4727 ------------------------
4728 -- Declared_In_Actual --
4729 ------------------------
4731 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
4732 Act : Entity_Id;
4734 begin
4735 if No (Associated_Formal_Package (Pack)) then
4736 return False;
4738 else
4739 Act := First_Entity (Pack);
4740 while Present (Act) loop
4741 if Renamed_Object (Pack) = Scop then
4742 return True;
4744 -- Check for end of list of actuals.
4746 elsif Ekind (Act) = E_Package
4747 and then Renamed_Object (Act) = Pack
4748 then
4749 return False;
4751 elsif Ekind (Act) = E_Package
4752 and then Declared_In_Actual (Act)
4753 then
4754 return True;
4755 end if;
4757 Next_Entity (Act);
4758 end loop;
4760 return False;
4761 end if;
4762 end Declared_In_Actual;
4764 -- Start of processing for From_Actual_Package
4766 begin
4767 if not In_Instance then
4768 return False;
4770 else
4771 Inst := Current_Scope;
4772 while Present (Inst)
4773 and then Ekind (Inst) /= E_Package
4774 and then not Is_Generic_Instance (Inst)
4775 loop
4776 Inst := Scope (Inst);
4777 end loop;
4779 if No (Inst) then
4780 return False;
4781 end if;
4783 Act := First_Entity (Inst);
4784 while Present (Act) loop
4785 if Ekind (Act) = E_Package
4786 and then Declared_In_Actual (Act)
4787 then
4788 return True;
4789 end if;
4791 Next_Entity (Act);
4792 end loop;
4794 return False;
4795 end if;
4796 end From_Actual_Package;
4798 -------------------------
4799 -- Is_Actual_Parameter --
4800 -------------------------
4802 function Is_Actual_Parameter return Boolean is
4803 begin
4804 return
4805 Nkind (N) = N_Identifier
4806 and then
4807 (Nkind (Parent (N)) = N_Procedure_Call_Statement
4808 or else
4809 (Nkind (Parent (N)) = N_Parameter_Association
4810 and then N = Explicit_Actual_Parameter (Parent (N))
4811 and then Nkind (Parent (Parent (N))) =
4812 N_Procedure_Call_Statement));
4813 end Is_Actual_Parameter;
4815 -------------------------
4816 -- Known_But_Invisible --
4817 -------------------------
4819 function Known_But_Invisible (E : Entity_Id) return Boolean is
4820 Fname : File_Name_Type;
4822 begin
4823 -- Entities in Standard are always considered to be known
4825 if Sloc (E) <= Standard_Location then
4826 return True;
4828 -- An entity that does not come from source is always considered
4829 -- to be unknown, since it is an artifact of code expansion.
4831 elsif not Comes_From_Source (E) then
4832 return False;
4834 -- In gnat internal mode, we consider all entities known. The
4835 -- historical reason behind this discrepancy is not known??? But the
4836 -- only effect is to modify the error message given, so it is not
4837 -- critical. Since it only affects the exact wording of error
4838 -- messages in illegal programs, we do not mention this as an
4839 -- effect of -gnatg, since it is not a language modification.
4841 elsif GNAT_Mode then
4842 return True;
4843 end if;
4845 -- Here we have an entity that is not from package Standard, and
4846 -- which comes from Source. See if it comes from an internal file.
4848 Fname := Unit_File_Name (Get_Source_Unit (E));
4850 -- Case of from internal file
4852 if Is_Internal_File_Name (Fname) then
4854 -- Private part entities in internal files are never considered
4855 -- to be known to the writer of normal application code.
4857 if Is_Hidden (E) then
4858 return False;
4859 end if;
4861 -- Entities from System packages other than System and
4862 -- System.Storage_Elements are not considered to be known.
4863 -- System.Auxxxx files are also considered known to the user.
4865 -- Should refine this at some point to generally distinguish
4866 -- between known and unknown internal files ???
4868 Get_Name_String (Fname);
4870 return
4871 Name_Len < 2
4872 or else
4873 Name_Buffer (1 .. 2) /= "s-"
4874 or else
4875 Name_Buffer (3 .. 8) = "stoele"
4876 or else
4877 Name_Buffer (3 .. 5) = "aux";
4879 -- If not an internal file, then entity is definitely known,
4880 -- even if it is in a private part (the message generated will
4881 -- note that it is in a private part)
4883 else
4884 return True;
4885 end if;
4886 end Known_But_Invisible;
4888 -------------------
4889 -- Nvis_Messages --
4890 -------------------
4892 procedure Nvis_Messages is
4893 Comp_Unit : Node_Id;
4894 Ent : Entity_Id;
4895 Found : Boolean := False;
4896 Hidden : Boolean := False;
4897 Item : Node_Id;
4899 begin
4900 -- Ada 2005 (AI-262): Generate a precise error concerning the
4901 -- Beaujolais effect that was previously detected
4903 if Nvis_Is_Private_Subprg then
4905 pragma Assert (Nkind (E2) = N_Defining_Identifier
4906 and then Ekind (E2) = E_Function
4907 and then Scope (E2) = Standard_Standard
4908 and then Has_Private_With (E2));
4910 -- Find the sloc corresponding to the private with'ed unit
4912 Comp_Unit := Cunit (Current_Sem_Unit);
4913 Error_Msg_Sloc := No_Location;
4915 Item := First (Context_Items (Comp_Unit));
4916 while Present (Item) loop
4917 if Nkind (Item) = N_With_Clause
4918 and then Private_Present (Item)
4919 and then Entity (Name (Item)) = E2
4920 then
4921 Error_Msg_Sloc := Sloc (Item);
4922 exit;
4923 end if;
4925 Next (Item);
4926 end loop;
4928 pragma Assert (Error_Msg_Sloc /= No_Location);
4930 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
4931 return;
4932 end if;
4934 Undefined (Nvis => True);
4936 if Msg then
4938 -- First loop does hidden declarations
4940 Ent := Homonyms;
4941 while Present (Ent) loop
4942 if Is_Potentially_Use_Visible (Ent) then
4943 if not Hidden then
4944 Error_Msg_N -- CODEFIX
4945 ("multiple use clauses cause hiding!", N);
4946 Hidden := True;
4947 end if;
4949 Error_Msg_Sloc := Sloc (Ent);
4950 Error_Msg_N -- CODEFIX
4951 ("hidden declaration#!", N);
4952 end if;
4954 Ent := Homonym (Ent);
4955 end loop;
4957 -- If we found hidden declarations, then that's enough, don't
4958 -- bother looking for non-visible declarations as well.
4960 if Hidden then
4961 return;
4962 end if;
4964 -- Second loop does non-directly visible declarations
4966 Ent := Homonyms;
4967 while Present (Ent) loop
4968 if not Is_Potentially_Use_Visible (Ent) then
4970 -- Do not bother the user with unknown entities
4972 if not Known_But_Invisible (Ent) then
4973 goto Continue;
4974 end if;
4976 Error_Msg_Sloc := Sloc (Ent);
4978 -- Output message noting that there is a non-visible
4979 -- declaration, distinguishing the private part case.
4981 if Is_Hidden (Ent) then
4982 Error_Msg_N ("non-visible (private) declaration#!", N);
4984 -- If the entity is declared in a generic package, it
4985 -- cannot be visible, so there is no point in adding it
4986 -- to the list of candidates if another homograph from a
4987 -- non-generic package has been seen.
4989 elsif Ekind (Scope (Ent)) = E_Generic_Package
4990 and then Found
4991 then
4992 null;
4994 else
4995 Error_Msg_N -- CODEFIX
4996 ("non-visible declaration#!", N);
4998 if Ekind (Scope (Ent)) /= E_Generic_Package then
4999 Found := True;
5000 end if;
5002 if Is_Compilation_Unit (Ent)
5003 and then
5004 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5005 then
5006 Error_Msg_Qual_Level := 99;
5007 Error_Msg_NE -- CODEFIX
5008 ("\\missing `WITH &;`", N, Ent);
5009 Error_Msg_Qual_Level := 0;
5010 end if;
5012 if Ekind (Ent) = E_Discriminant
5013 and then Present (Corresponding_Discriminant (Ent))
5014 and then Scope (Corresponding_Discriminant (Ent)) =
5015 Etype (Scope (Ent))
5016 then
5017 Error_Msg_N
5018 ("inherited discriminant not allowed here" &
5019 " (RM 3.8 (12), 3.8.1 (6))!", N);
5020 end if;
5021 end if;
5023 -- Set entity and its containing package as referenced. We
5024 -- can't be sure of this, but this seems a better choice
5025 -- to avoid unused entity messages.
5027 if Comes_From_Source (Ent) then
5028 Set_Referenced (Ent);
5029 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5030 end if;
5031 end if;
5033 <<Continue>>
5034 Ent := Homonym (Ent);
5035 end loop;
5036 end if;
5037 end Nvis_Messages;
5039 ---------------
5040 -- Undefined --
5041 ---------------
5043 procedure Undefined (Nvis : Boolean) is
5044 Emsg : Error_Msg_Id;
5046 begin
5047 -- We should never find an undefined internal name. If we do, then
5048 -- see if we have previous errors. If so, ignore on the grounds that
5049 -- it is probably a cascaded message (e.g. a block label from a badly
5050 -- formed block). If no previous errors, then we have a real internal
5051 -- error of some kind so raise an exception.
5053 if Is_Internal_Name (Chars (N)) then
5054 if Total_Errors_Detected /= 0 then
5055 return;
5056 else
5057 raise Program_Error;
5058 end if;
5059 end if;
5061 -- A very specialized error check, if the undefined variable is
5062 -- a case tag, and the case type is an enumeration type, check
5063 -- for a possible misspelling, and if so, modify the identifier
5065 -- Named aggregate should also be handled similarly ???
5067 if Nkind (N) = N_Identifier
5068 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5069 then
5070 declare
5071 Case_Stm : constant Node_Id := Parent (Parent (N));
5072 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5074 Lit : Node_Id;
5076 begin
5077 if Is_Enumeration_Type (Case_Typ)
5078 and then not Is_Standard_Character_Type (Case_Typ)
5079 then
5080 Lit := First_Literal (Case_Typ);
5081 Get_Name_String (Chars (Lit));
5083 if Chars (Lit) /= Chars (N)
5084 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5085 then
5086 Error_Msg_Node_2 := Lit;
5087 Error_Msg_N -- CODEFIX
5088 ("& is undefined, assume misspelling of &", N);
5089 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5090 return;
5091 end if;
5093 Lit := Next_Literal (Lit);
5094 end if;
5095 end;
5096 end if;
5098 -- Normal processing
5100 Set_Entity (N, Any_Id);
5101 Set_Etype (N, Any_Type);
5103 -- We use the table Urefs to keep track of entities for which we
5104 -- have issued errors for undefined references. Multiple errors
5105 -- for a single name are normally suppressed, however we modify
5106 -- the error message to alert the programmer to this effect.
5108 for J in Urefs.First .. Urefs.Last loop
5109 if Chars (N) = Chars (Urefs.Table (J).Node) then
5110 if Urefs.Table (J).Err /= No_Error_Msg
5111 and then Sloc (N) /= Urefs.Table (J).Loc
5112 then
5113 Error_Msg_Node_1 := Urefs.Table (J).Node;
5115 if Urefs.Table (J).Nvis then
5116 Change_Error_Text (Urefs.Table (J).Err,
5117 "& is not visible (more references follow)");
5118 else
5119 Change_Error_Text (Urefs.Table (J).Err,
5120 "& is undefined (more references follow)");
5121 end if;
5123 Urefs.Table (J).Err := No_Error_Msg;
5124 end if;
5126 -- Although we will set Msg False, and thus suppress the
5127 -- message, we also set Error_Posted True, to avoid any
5128 -- cascaded messages resulting from the undefined reference.
5130 Msg := False;
5131 Set_Error_Posted (N, True);
5132 return;
5133 end if;
5134 end loop;
5136 -- If entry not found, this is first undefined occurrence
5138 if Nvis then
5139 Error_Msg_N ("& is not visible!", N);
5140 Emsg := Get_Msg_Id;
5142 else
5143 Error_Msg_N ("& is undefined!", N);
5144 Emsg := Get_Msg_Id;
5146 -- A very bizarre special check, if the undefined identifier
5147 -- is put or put_line, then add a special error message (since
5148 -- this is a very common error for beginners to make).
5150 if Nam_In (Chars (N), Name_Put, Name_Put_Line) then
5151 Error_Msg_N -- CODEFIX
5152 ("\\possible missing `WITH Ada.Text_'I'O; " &
5153 "USE Ada.Text_'I'O`!", N);
5155 -- Another special check if N is the prefix of a selected
5156 -- component which is a known unit, add message complaining
5157 -- about missing with for this unit.
5159 elsif Nkind (Parent (N)) = N_Selected_Component
5160 and then N = Prefix (Parent (N))
5161 and then Is_Known_Unit (Parent (N))
5162 then
5163 Error_Msg_Node_2 := Selector_Name (Parent (N));
5164 Error_Msg_N -- CODEFIX
5165 ("\\missing `WITH &.&;`", Prefix (Parent (N)));
5166 end if;
5168 -- Now check for possible misspellings
5170 declare
5171 E : Entity_Id;
5172 Ematch : Entity_Id := Empty;
5174 Last_Name_Id : constant Name_Id :=
5175 Name_Id (Nat (First_Name_Id) +
5176 Name_Entries_Count - 1);
5178 begin
5179 for Nam in First_Name_Id .. Last_Name_Id loop
5180 E := Get_Name_Entity_Id (Nam);
5182 if Present (E)
5183 and then (Is_Immediately_Visible (E)
5184 or else
5185 Is_Potentially_Use_Visible (E))
5186 then
5187 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5188 Ematch := E;
5189 exit;
5190 end if;
5191 end if;
5192 end loop;
5194 if Present (Ematch) then
5195 Error_Msg_NE -- CODEFIX
5196 ("\possible misspelling of&", N, Ematch);
5197 end if;
5198 end;
5199 end if;
5201 -- Make entry in undefined references table unless the full errors
5202 -- switch is set, in which case by refraining from generating the
5203 -- table entry, we guarantee that we get an error message for every
5204 -- undefined reference.
5206 if not All_Errors_Mode then
5207 Urefs.Append (
5208 (Node => N,
5209 Err => Emsg,
5210 Nvis => Nvis,
5211 Loc => Sloc (N)));
5212 end if;
5214 Msg := True;
5215 end Undefined;
5217 -- Start of processing for Find_Direct_Name
5219 begin
5220 -- If the entity pointer is already set, this is an internal node, or
5221 -- a node that is analyzed more than once, after a tree modification.
5222 -- In such a case there is no resolution to perform, just set the type.
5224 if Present (Entity (N)) then
5225 if Is_Type (Entity (N)) then
5226 Set_Etype (N, Entity (N));
5228 else
5229 declare
5230 Entyp : constant Entity_Id := Etype (Entity (N));
5232 begin
5233 -- One special case here. If the Etype field is already set,
5234 -- and references the packed array type corresponding to the
5235 -- etype of the referenced entity, then leave it alone. This
5236 -- happens for trees generated from Exp_Pakd, where expressions
5237 -- can be deliberately "mis-typed" to the packed array type.
5239 if Is_Array_Type (Entyp)
5240 and then Is_Packed (Entyp)
5241 and then Present (Etype (N))
5242 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5243 then
5244 null;
5246 -- If not that special case, then just reset the Etype
5248 else
5249 Set_Etype (N, Etype (Entity (N)));
5250 end if;
5251 end;
5252 end if;
5254 return;
5255 end if;
5257 -- Here if Entity pointer was not set, we need full visibility analysis
5258 -- First we generate debugging output if the debug E flag is set.
5260 if Debug_Flag_E then
5261 Write_Str ("Looking for ");
5262 Write_Name (Chars (N));
5263 Write_Eol;
5264 end if;
5266 Homonyms := Current_Entity (N);
5267 Nvis_Entity := False;
5269 E := Homonyms;
5270 while Present (E) loop
5272 -- If entity is immediately visible or potentially use visible, then
5273 -- process the entity and we are done.
5275 if Is_Immediately_Visible (E) then
5276 goto Immediately_Visible_Entity;
5278 elsif Is_Potentially_Use_Visible (E) then
5279 goto Potentially_Use_Visible_Entity;
5281 -- Note if a known but invisible entity encountered
5283 elsif Known_But_Invisible (E) then
5284 Nvis_Entity := True;
5285 end if;
5287 -- Move to next entity in chain and continue search
5289 E := Homonym (E);
5290 end loop;
5292 -- If no entries on homonym chain that were potentially visible,
5293 -- and no entities reasonably considered as non-visible, then
5294 -- we have a plain undefined reference, with no additional
5295 -- explanation required.
5297 if not Nvis_Entity then
5298 Undefined (Nvis => False);
5300 -- Otherwise there is at least one entry on the homonym chain that
5301 -- is reasonably considered as being known and non-visible.
5303 else
5304 Nvis_Messages;
5305 end if;
5307 goto Done;
5309 -- Processing for a potentially use visible entry found. We must search
5310 -- the rest of the homonym chain for two reasons. First, if there is a
5311 -- directly visible entry, then none of the potentially use-visible
5312 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5313 -- for the case of multiple potentially use-visible entries hiding one
5314 -- another and as a result being non-directly visible (RM 8.4(11)).
5316 <<Potentially_Use_Visible_Entity>> declare
5317 Only_One_Visible : Boolean := True;
5318 All_Overloadable : Boolean := Is_Overloadable (E);
5320 begin
5321 E2 := Homonym (E);
5322 while Present (E2) loop
5323 if Is_Immediately_Visible (E2) then
5325 -- If the use-visible entity comes from the actual for a
5326 -- formal package, it hides a directly visible entity from
5327 -- outside the instance.
5329 if From_Actual_Package (E)
5330 and then Scope_Depth (E2) < Scope_Depth (Inst)
5331 then
5332 goto Found;
5333 else
5334 E := E2;
5335 goto Immediately_Visible_Entity;
5336 end if;
5338 elsif Is_Potentially_Use_Visible (E2) then
5339 Only_One_Visible := False;
5340 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5342 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5343 -- that can occur in private_with clauses. Example:
5345 -- with A;
5346 -- private with B; package A is
5347 -- package C is function B return Integer;
5348 -- use A; end A;
5349 -- V1 : Integer := B;
5350 -- private function B return Integer;
5351 -- V2 : Integer := B;
5352 -- end C;
5354 -- V1 resolves to A.B, but V2 resolves to library unit B
5356 elsif Ekind (E2) = E_Function
5357 and then Scope (E2) = Standard_Standard
5358 and then Has_Private_With (E2)
5359 then
5360 Only_One_Visible := False;
5361 All_Overloadable := False;
5362 Nvis_Is_Private_Subprg := True;
5363 exit;
5364 end if;
5366 E2 := Homonym (E2);
5367 end loop;
5369 -- On falling through this loop, we have checked that there are no
5370 -- immediately visible entities. Only_One_Visible is set if exactly
5371 -- one potentially use visible entity exists. All_Overloadable is
5372 -- set if all the potentially use visible entities are overloadable.
5373 -- The condition for legality is that either there is one potentially
5374 -- use visible entity, or if there is more than one, then all of them
5375 -- are overloadable.
5377 if Only_One_Visible or All_Overloadable then
5378 goto Found;
5380 -- If there is more than one potentially use-visible entity and at
5381 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5382 -- Note that E points to the first such entity on the homonym list.
5383 -- Special case: if one of the entities is declared in an actual
5384 -- package, it was visible in the generic, and takes precedence over
5385 -- other entities that are potentially use-visible. Same if it is
5386 -- declared in a local instantiation of the current instance.
5388 else
5389 if In_Instance then
5391 -- Find current instance
5393 Inst := Current_Scope;
5394 while Present (Inst) and then Inst /= Standard_Standard loop
5395 if Is_Generic_Instance (Inst) then
5396 exit;
5397 end if;
5399 Inst := Scope (Inst);
5400 end loop;
5402 E2 := E;
5403 while Present (E2) loop
5404 if From_Actual_Package (E2)
5405 or else
5406 (Is_Generic_Instance (Scope (E2))
5407 and then Scope_Depth (Scope (E2)) > Scope_Depth (Inst))
5408 then
5409 E := E2;
5410 goto Found;
5411 end if;
5413 E2 := Homonym (E2);
5414 end loop;
5416 Nvis_Messages;
5417 goto Done;
5419 elsif
5420 Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
5421 then
5422 -- A use-clause in the body of a system file creates conflict
5423 -- with some entity in a user scope, while rtsfind is active.
5424 -- Keep only the entity coming from another predefined unit.
5426 E2 := E;
5427 while Present (E2) loop
5428 if Is_Predefined_File_Name
5429 (Unit_File_Name (Get_Source_Unit (Sloc (E2))))
5430 then
5431 E := E2;
5432 goto Found;
5433 end if;
5435 E2 := Homonym (E2);
5436 end loop;
5438 -- Entity must exist because predefined unit is correct
5440 raise Program_Error;
5442 else
5443 Nvis_Messages;
5444 goto Done;
5445 end if;
5446 end if;
5447 end;
5449 -- Come here with E set to the first immediately visible entity on
5450 -- the homonym chain. This is the one we want unless there is another
5451 -- immediately visible entity further on in the chain for an inner
5452 -- scope (RM 8.3(8)).
5454 <<Immediately_Visible_Entity>> declare
5455 Level : Int;
5456 Scop : Entity_Id;
5458 begin
5459 -- Find scope level of initial entity. When compiling through
5460 -- Rtsfind, the previous context is not completely invisible, and
5461 -- an outer entity may appear on the chain, whose scope is below
5462 -- the entry for Standard that delimits the current scope stack.
5463 -- Indicate that the level for this spurious entry is outside of
5464 -- the current scope stack.
5466 Level := Scope_Stack.Last;
5467 loop
5468 Scop := Scope_Stack.Table (Level).Entity;
5469 exit when Scop = Scope (E);
5470 Level := Level - 1;
5471 exit when Scop = Standard_Standard;
5472 end loop;
5474 -- Now search remainder of homonym chain for more inner entry
5475 -- If the entity is Standard itself, it has no scope, and we
5476 -- compare it with the stack entry directly.
5478 E2 := Homonym (E);
5479 while Present (E2) loop
5480 if Is_Immediately_Visible (E2) then
5482 -- If a generic package contains a local declaration that
5483 -- has the same name as the generic, there may be a visibility
5484 -- conflict in an instance, where the local declaration must
5485 -- also hide the name of the corresponding package renaming.
5486 -- We check explicitly for a package declared by a renaming,
5487 -- whose renamed entity is an instance that is on the scope
5488 -- stack, and that contains a homonym in the same scope. Once
5489 -- we have found it, we know that the package renaming is not
5490 -- immediately visible, and that the identifier denotes the
5491 -- other entity (and its homonyms if overloaded).
5493 if Scope (E) = Scope (E2)
5494 and then Ekind (E) = E_Package
5495 and then Present (Renamed_Object (E))
5496 and then Is_Generic_Instance (Renamed_Object (E))
5497 and then In_Open_Scopes (Renamed_Object (E))
5498 and then Comes_From_Source (N)
5499 then
5500 Set_Is_Immediately_Visible (E, False);
5501 E := E2;
5503 else
5504 for J in Level + 1 .. Scope_Stack.Last loop
5505 if Scope_Stack.Table (J).Entity = Scope (E2)
5506 or else Scope_Stack.Table (J).Entity = E2
5507 then
5508 Level := J;
5509 E := E2;
5510 exit;
5511 end if;
5512 end loop;
5513 end if;
5514 end if;
5516 E2 := Homonym (E2);
5517 end loop;
5519 -- At the end of that loop, E is the innermost immediately
5520 -- visible entity, so we are all set.
5521 end;
5523 -- Come here with entity found, and stored in E
5525 <<Found>> begin
5527 -- Check violation of No_Wide_Characters restriction
5529 Check_Wide_Character_Restriction (E, N);
5531 -- When distribution features are available (Get_PCS_Name /=
5532 -- Name_No_DSA), a remote access-to-subprogram type is converted
5533 -- into a record type holding whatever information is needed to
5534 -- perform a remote call on an RCI subprogram. In that case we
5535 -- rewrite any occurrence of the RAS type into the equivalent record
5536 -- type here. 'Access attribute references and RAS dereferences are
5537 -- then implemented using specific TSSs. However when distribution is
5538 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
5539 -- generation of these TSSs, and we must keep the RAS type in its
5540 -- original access-to-subprogram form (since all calls through a
5541 -- value of such type will be local anyway in the absence of a PCS).
5543 if Comes_From_Source (N)
5544 and then Is_Remote_Access_To_Subprogram_Type (E)
5545 and then Ekind (E) = E_Access_Subprogram_Type
5546 and then Expander_Active
5547 and then Get_PCS_Name /= Name_No_DSA
5548 then
5549 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
5550 goto Done;
5551 end if;
5553 -- Set the entity. Note that the reason we call Set_Entity for the
5554 -- overloadable case, as opposed to Set_Entity_With_Checks is
5555 -- that in the overloaded case, the initial call can set the wrong
5556 -- homonym. The call that sets the right homonym is in Sem_Res and
5557 -- that call does use Set_Entity_With_Checks, so we don't miss
5558 -- a style check.
5560 if Is_Overloadable (E) then
5561 Set_Entity (N, E);
5562 else
5563 Set_Entity_With_Checks (N, E);
5564 end if;
5566 if Is_Type (E) then
5567 Set_Etype (N, E);
5568 else
5569 Set_Etype (N, Get_Full_View (Etype (E)));
5570 end if;
5572 if Debug_Flag_E then
5573 Write_Str (" found ");
5574 Write_Entity_Info (E, " ");
5575 end if;
5577 -- If the Ekind of the entity is Void, it means that all homonyms
5578 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
5579 -- test is skipped if the current scope is a record and the name is
5580 -- a pragma argument expression (case of Atomic and Volatile pragmas
5581 -- and possibly other similar pragmas added later, which are allowed
5582 -- to reference components in the current record).
5584 if Ekind (E) = E_Void
5585 and then
5586 (not Is_Record_Type (Current_Scope)
5587 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
5588 then
5589 Premature_Usage (N);
5591 -- If the entity is overloadable, collect all interpretations of the
5592 -- name for subsequent overload resolution. We optimize a bit here to
5593 -- do this only if we have an overloadable entity that is not on its
5594 -- own on the homonym chain.
5596 elsif Is_Overloadable (E)
5597 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
5598 then
5599 Collect_Interps (N);
5601 -- If no homonyms were visible, the entity is unambiguous
5603 if not Is_Overloaded (N) then
5604 if not Is_Actual_Parameter then
5605 Generate_Reference (E, N);
5606 end if;
5607 end if;
5609 -- Case of non-overloadable entity, set the entity providing that
5610 -- we do not have the case of a discriminant reference within a
5611 -- default expression. Such references are replaced with the
5612 -- corresponding discriminal, which is the formal corresponding to
5613 -- to the discriminant in the initialization procedure.
5615 else
5616 -- Entity is unambiguous, indicate that it is referenced here
5618 -- For a renaming of an object, always generate simple reference,
5619 -- we don't try to keep track of assignments in this case, except
5620 -- in SPARK mode where renamings are traversed for generating
5621 -- local effects of subprograms.
5623 if Is_Object (E)
5624 and then Present (Renamed_Object (E))
5625 and then not GNATprove_Mode
5626 then
5627 Generate_Reference (E, N);
5629 -- If the renamed entity is a private protected component,
5630 -- reference the original component as well. This needs to be
5631 -- done because the private renamings are installed before any
5632 -- analysis has occurred. Reference to a private component will
5633 -- resolve to the renaming and the original component will be
5634 -- left unreferenced, hence the following.
5636 if Is_Prival (E) then
5637 Generate_Reference (Prival_Link (E), N);
5638 end if;
5640 -- One odd case is that we do not want to set the Referenced flag
5641 -- if the entity is a label, and the identifier is the label in
5642 -- the source, since this is not a reference from the point of
5643 -- view of the user.
5645 elsif Nkind (Parent (N)) = N_Label then
5646 declare
5647 R : constant Boolean := Referenced (E);
5649 begin
5650 -- Generate reference unless this is an actual parameter
5651 -- (see comment below)
5653 if Is_Actual_Parameter then
5654 Generate_Reference (E, N);
5655 Set_Referenced (E, R);
5656 end if;
5657 end;
5659 -- Normal case, not a label: generate reference
5661 else
5662 if not Is_Actual_Parameter then
5664 -- Package or generic package is always a simple reference
5666 if Ekind_In (E, E_Package, E_Generic_Package) then
5667 Generate_Reference (E, N, 'r');
5669 -- Else see if we have a left hand side
5671 else
5672 case Is_LHS (N) is
5673 when Yes =>
5674 Generate_Reference (E, N, 'm');
5676 when No =>
5677 Generate_Reference (E, N, 'r');
5679 -- If we don't know now, generate reference later
5681 when Unknown =>
5682 Deferred_References.Append ((E, N));
5683 end case;
5684 end if;
5685 end if;
5687 Check_Nested_Access (E);
5688 end if;
5690 Set_Entity_Or_Discriminal (N, E);
5692 -- The name may designate a generalized reference, in which case
5693 -- the dereference interpretation will be included.
5695 if Ada_Version >= Ada_2012
5696 and then
5697 (Nkind (Parent (N)) in N_Subexpr
5698 or else Nkind_In (Parent (N), N_Object_Declaration,
5699 N_Assignment_Statement))
5700 then
5701 Check_Implicit_Dereference (N, Etype (E));
5702 end if;
5703 end if;
5704 end;
5706 -- Come here with entity set
5708 <<Done>>
5709 Check_Restriction_No_Use_Of_Entity (N);
5710 end Find_Direct_Name;
5712 ------------------------
5713 -- Find_Expanded_Name --
5714 ------------------------
5716 -- This routine searches the homonym chain of the entity until it finds
5717 -- an entity declared in the scope denoted by the prefix. If the entity
5718 -- is private, it may nevertheless be immediately visible, if we are in
5719 -- the scope of its declaration.
5721 procedure Find_Expanded_Name (N : Node_Id) is
5722 function In_Pragmas_Depends_Or_Global (N : Node_Id) return Boolean;
5723 -- Determine whether an arbitrary node N appears in pragmas [Refined_]
5724 -- Depends or [Refined_]Global.
5726 ----------------------------------
5727 -- In_Pragmas_Depends_Or_Global --
5728 ----------------------------------
5730 function In_Pragmas_Depends_Or_Global (N : Node_Id) return Boolean is
5731 Par : Node_Id;
5733 begin
5734 -- Climb the parent chain looking for a pragma
5736 Par := N;
5737 while Present (Par) loop
5738 if Nkind (Par) = N_Pragma
5739 and then Nam_In (Pragma_Name (Par), Name_Depends,
5740 Name_Global,
5741 Name_Refined_Depends,
5742 Name_Refined_Global)
5743 then
5744 return True;
5746 -- Prevent the search from going too far
5748 elsif Is_Body_Or_Package_Declaration (Par) then
5749 return False;
5750 end if;
5752 Par := Parent (Par);
5753 end loop;
5755 return False;
5756 end In_Pragmas_Depends_Or_Global;
5758 -- Local variables
5760 Selector : constant Node_Id := Selector_Name (N);
5761 Candidate : Entity_Id := Empty;
5762 P_Name : Entity_Id;
5763 Id : Entity_Id;
5765 -- Start of processing for Find_Expanded_Name
5767 begin
5768 P_Name := Entity (Prefix (N));
5770 -- If the prefix is a renamed package, look for the entity in the
5771 -- original package.
5773 if Ekind (P_Name) = E_Package
5774 and then Present (Renamed_Object (P_Name))
5775 then
5776 P_Name := Renamed_Object (P_Name);
5778 -- Rewrite node with entity field pointing to renamed object
5780 Rewrite (Prefix (N), New_Copy (Prefix (N)));
5781 Set_Entity (Prefix (N), P_Name);
5783 -- If the prefix is an object of a concurrent type, look for
5784 -- the entity in the associated task or protected type.
5786 elsif Is_Concurrent_Type (Etype (P_Name)) then
5787 P_Name := Etype (P_Name);
5788 end if;
5790 Id := Current_Entity (Selector);
5792 declare
5793 Is_New_Candidate : Boolean;
5795 begin
5796 while Present (Id) loop
5797 if Scope (Id) = P_Name then
5798 Candidate := Id;
5799 Is_New_Candidate := True;
5801 -- Handle abstract views of states and variables. These are
5802 -- acceptable only when the reference to the view appears in
5803 -- pragmas [Refined_]Depends and [Refined_]Global.
5805 if Ekind (Id) = E_Abstract_State
5806 and then From_Limited_With (Id)
5807 and then Present (Non_Limited_View (Id))
5808 then
5809 if In_Pragmas_Depends_Or_Global (N) then
5810 Candidate := Non_Limited_View (Id);
5811 Is_New_Candidate := True;
5813 -- Hide candidate because it is not used in a proper context
5815 else
5816 Candidate := Empty;
5817 Is_New_Candidate := False;
5818 end if;
5819 end if;
5821 -- Ada 2005 (AI-217): Handle shadow entities associated with
5822 -- types declared in limited-withed nested packages. We don't need
5823 -- to handle E_Incomplete_Subtype entities because the entities
5824 -- in the limited view are always E_Incomplete_Type and
5825 -- E_Class_Wide_Type entities (see Build_Limited_Views).
5827 -- Regarding the expression used to evaluate the scope, it
5828 -- is important to note that the limited view also has shadow
5829 -- entities associated nested packages. For this reason the
5830 -- correct scope of the entity is the scope of the real entity.
5831 -- The non-limited view may itself be incomplete, in which case
5832 -- get the full view if available.
5834 elsif Ekind_In (Id, E_Incomplete_Type, E_Class_Wide_Type)
5835 and then From_Limited_With (Id)
5836 and then Present (Non_Limited_View (Id))
5837 and then Scope (Non_Limited_View (Id)) = P_Name
5838 then
5839 Candidate := Get_Full_View (Non_Limited_View (Id));
5840 Is_New_Candidate := True;
5842 else
5843 Is_New_Candidate := False;
5844 end if;
5846 if Is_New_Candidate then
5848 -- If entity is a child unit, either it is a visible child of
5849 -- the prefix, or we are in the body of a generic prefix, as
5850 -- will happen when a child unit is instantiated in the body
5851 -- of a generic parent. This is because the instance body does
5852 -- not restore the full compilation context, given that all
5853 -- non-local references have been captured.
5855 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
5856 exit when Is_Visible_Lib_Unit (Id)
5857 or else (Is_Child_Unit (Id)
5858 and then In_Open_Scopes (Scope (Id))
5859 and then In_Instance_Body);
5860 else
5861 exit when not Is_Hidden (Id);
5862 end if;
5864 exit when Is_Immediately_Visible (Id);
5865 end if;
5867 Id := Homonym (Id);
5868 end loop;
5869 end;
5871 if No (Id)
5872 and then Ekind_In (P_Name, E_Procedure, E_Function)
5873 and then Is_Generic_Instance (P_Name)
5874 then
5875 -- Expanded name denotes entity in (instance of) generic subprogram.
5876 -- The entity may be in the subprogram instance, or may denote one of
5877 -- the formals, which is declared in the enclosing wrapper package.
5879 P_Name := Scope (P_Name);
5881 Id := Current_Entity (Selector);
5882 while Present (Id) loop
5883 exit when Scope (Id) = P_Name;
5884 Id := Homonym (Id);
5885 end loop;
5886 end if;
5888 if No (Id) or else Chars (Id) /= Chars (Selector) then
5889 Set_Etype (N, Any_Type);
5891 -- If we are looking for an entity defined in System, try to find it
5892 -- in the child package that may have been provided as an extension
5893 -- to System. The Extend_System pragma will have supplied the name of
5894 -- the extension, which may have to be loaded.
5896 if Chars (P_Name) = Name_System
5897 and then Scope (P_Name) = Standard_Standard
5898 and then Present (System_Extend_Unit)
5899 and then Present_System_Aux (N)
5900 then
5901 Set_Entity (Prefix (N), System_Aux_Id);
5902 Find_Expanded_Name (N);
5903 return;
5905 elsif Nkind (Selector) = N_Operator_Symbol
5906 and then Has_Implicit_Operator (N)
5907 then
5908 -- There is an implicit instance of the predefined operator in
5909 -- the given scope. The operator entity is defined in Standard.
5910 -- Has_Implicit_Operator makes the node into an Expanded_Name.
5912 return;
5914 elsif Nkind (Selector) = N_Character_Literal
5915 and then Has_Implicit_Character_Literal (N)
5916 then
5917 -- If there is no literal defined in the scope denoted by the
5918 -- prefix, the literal may belong to (a type derived from)
5919 -- Standard_Character, for which we have no explicit literals.
5921 return;
5923 else
5924 -- If the prefix is a single concurrent object, use its name in
5925 -- the error message, rather than that of the anonymous type.
5927 if Is_Concurrent_Type (P_Name)
5928 and then Is_Internal_Name (Chars (P_Name))
5929 then
5930 Error_Msg_Node_2 := Entity (Prefix (N));
5931 else
5932 Error_Msg_Node_2 := P_Name;
5933 end if;
5935 if P_Name = System_Aux_Id then
5936 P_Name := Scope (P_Name);
5937 Set_Entity (Prefix (N), P_Name);
5938 end if;
5940 if Present (Candidate) then
5942 -- If we know that the unit is a child unit we can give a more
5943 -- accurate error message.
5945 if Is_Child_Unit (Candidate) then
5947 -- If the candidate is a private child unit and we are in
5948 -- the visible part of a public unit, specialize the error
5949 -- message. There might be a private with_clause for it,
5950 -- but it is not currently active.
5952 if Is_Private_Descendant (Candidate)
5953 and then Ekind (Current_Scope) = E_Package
5954 and then not In_Private_Part (Current_Scope)
5955 and then not Is_Private_Descendant (Current_Scope)
5956 then
5957 Error_Msg_N ("private child unit& is not visible here",
5958 Selector);
5960 -- Normal case where we have a missing with for a child unit
5962 else
5963 Error_Msg_Qual_Level := 99;
5964 Error_Msg_NE -- CODEFIX
5965 ("missing `WITH &;`", Selector, Candidate);
5966 Error_Msg_Qual_Level := 0;
5967 end if;
5969 -- Here we don't know that this is a child unit
5971 else
5972 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
5973 end if;
5975 else
5976 -- Within the instantiation of a child unit, the prefix may
5977 -- denote the parent instance, but the selector has the name
5978 -- of the original child. That is to say, when A.B appears
5979 -- within an instantiation of generic child unit B, the scope
5980 -- stack includes an instance of A (P_Name) and an instance
5981 -- of B under some other name. We scan the scope to find this
5982 -- child instance, which is the desired entity.
5983 -- Note that the parent may itself be a child instance, if
5984 -- the reference is of the form A.B.C, in which case A.B has
5985 -- already been rewritten with the proper entity.
5987 if In_Open_Scopes (P_Name)
5988 and then Is_Generic_Instance (P_Name)
5989 then
5990 declare
5991 Gen_Par : constant Entity_Id :=
5992 Generic_Parent (Specification
5993 (Unit_Declaration_Node (P_Name)));
5994 S : Entity_Id := Current_Scope;
5995 P : Entity_Id;
5997 begin
5998 for J in reverse 0 .. Scope_Stack.Last loop
5999 S := Scope_Stack.Table (J).Entity;
6001 exit when S = Standard_Standard;
6003 if Ekind_In (S, E_Function,
6004 E_Package,
6005 E_Procedure)
6006 then
6007 P := Generic_Parent (Specification
6008 (Unit_Declaration_Node (S)));
6010 -- Check that P is a generic child of the generic
6011 -- parent of the prefix.
6013 if Present (P)
6014 and then Chars (P) = Chars (Selector)
6015 and then Scope (P) = Gen_Par
6016 then
6017 Id := S;
6018 goto Found;
6019 end if;
6020 end if;
6022 end loop;
6023 end;
6024 end if;
6026 -- If this is a selection from Ada, System or Interfaces, then
6027 -- we assume a missing with for the corresponding package.
6029 if Is_Known_Unit (N) then
6030 if not Error_Posted (N) then
6031 Error_Msg_Node_2 := Selector;
6032 Error_Msg_N -- CODEFIX
6033 ("missing `WITH &.&;`", Prefix (N));
6034 end if;
6036 -- If this is a selection from a dummy package, then suppress
6037 -- the error message, of course the entity is missing if the
6038 -- package is missing.
6040 elsif Sloc (Error_Msg_Node_2) = No_Location then
6041 null;
6043 -- Here we have the case of an undefined component
6045 else
6047 -- The prefix may hide a homonym in the context that
6048 -- declares the desired entity. This error can use a
6049 -- specialized message.
6051 if In_Open_Scopes (P_Name) then
6052 declare
6053 H : constant Entity_Id := Homonym (P_Name);
6055 begin
6056 if Present (H)
6057 and then Is_Compilation_Unit (H)
6058 and then
6059 (Is_Immediately_Visible (H)
6060 or else Is_Visible_Lib_Unit (H))
6061 then
6062 Id := First_Entity (H);
6063 while Present (Id) loop
6064 if Chars (Id) = Chars (Selector) then
6065 Error_Msg_Qual_Level := 99;
6066 Error_Msg_Name_1 := Chars (Selector);
6067 Error_Msg_NE
6068 ("% not declared in&", N, P_Name);
6069 Error_Msg_NE
6070 ("\use fully qualified name starting with "
6071 & "Standard to make& visible", N, H);
6072 Error_Msg_Qual_Level := 0;
6073 goto Done;
6074 end if;
6076 Next_Entity (Id);
6077 end loop;
6078 end if;
6080 -- If not found, standard error message
6082 Error_Msg_NE ("& not declared in&", N, Selector);
6084 <<Done>> null;
6085 end;
6087 else
6088 Error_Msg_NE ("& not declared in&", N, Selector);
6089 end if;
6091 -- Check for misspelling of some entity in prefix
6093 Id := First_Entity (P_Name);
6094 while Present (Id) loop
6095 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
6096 and then not Is_Internal_Name (Chars (Id))
6097 then
6098 Error_Msg_NE -- CODEFIX
6099 ("possible misspelling of&", Selector, Id);
6100 exit;
6101 end if;
6103 Next_Entity (Id);
6104 end loop;
6106 -- Specialize the message if this may be an instantiation
6107 -- of a child unit that was not mentioned in the context.
6109 if Nkind (Parent (N)) = N_Package_Instantiation
6110 and then Is_Generic_Instance (Entity (Prefix (N)))
6111 and then Is_Compilation_Unit
6112 (Generic_Parent (Parent (Entity (Prefix (N)))))
6113 then
6114 Error_Msg_Node_2 := Selector;
6115 Error_Msg_N -- CODEFIX
6116 ("\missing `WITH &.&;`", Prefix (N));
6117 end if;
6118 end if;
6119 end if;
6121 Id := Any_Id;
6122 end if;
6123 end if;
6125 <<Found>>
6126 if Comes_From_Source (N)
6127 and then Is_Remote_Access_To_Subprogram_Type (Id)
6128 and then Ekind (Id) = E_Access_Subprogram_Type
6129 and then Present (Equivalent_Type (Id))
6130 then
6131 -- If we are not actually generating distribution code (i.e. the
6132 -- current PCS is the dummy non-distributed version), then the
6133 -- Equivalent_Type will be missing, and Id should be treated as
6134 -- a regular access-to-subprogram type.
6136 Id := Equivalent_Type (Id);
6137 Set_Chars (Selector, Chars (Id));
6138 end if;
6140 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
6142 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
6143 if From_Limited_With (Id)
6144 or else Is_Type (Id)
6145 or else Ekind (Id) = E_Package
6146 then
6147 null;
6148 else
6149 Error_Msg_N
6150 ("limited withed package can only be used to access "
6151 & "incomplete types", N);
6152 end if;
6153 end if;
6155 if Is_Task_Type (P_Name)
6156 and then ((Ekind (Id) = E_Entry
6157 and then Nkind (Parent (N)) /= N_Attribute_Reference)
6158 or else
6159 (Ekind (Id) = E_Entry_Family
6160 and then
6161 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
6162 then
6163 -- If both the task type and the entry are in scope, this may still
6164 -- be the expanded name of an entry formal.
6166 if In_Open_Scopes (Id)
6167 and then Nkind (Parent (N)) = N_Selected_Component
6168 then
6169 null;
6171 else
6172 -- It is an entry call after all, either to the current task
6173 -- (which will deadlock) or to an enclosing task.
6175 Analyze_Selected_Component (N);
6176 return;
6177 end if;
6178 end if;
6180 Change_Selected_Component_To_Expanded_Name (N);
6182 -- Set appropriate type
6184 if Is_Type (Id) then
6185 Set_Etype (N, Id);
6186 else
6187 Set_Etype (N, Get_Full_View (Etype (Id)));
6188 end if;
6190 -- Do style check and generate reference, but skip both steps if this
6191 -- entity has homonyms, since we may not have the right homonym set yet.
6192 -- The proper homonym will be set during the resolve phase.
6194 if Has_Homonym (Id) then
6195 Set_Entity (N, Id);
6197 else
6198 Set_Entity_Or_Discriminal (N, Id);
6200 case Is_LHS (N) is
6201 when Yes =>
6202 Generate_Reference (Id, N, 'm');
6203 when No =>
6204 Generate_Reference (Id, N, 'r');
6205 when Unknown =>
6206 Deferred_References.Append ((Id, N));
6207 end case;
6208 end if;
6210 -- Check for violation of No_Wide_Characters
6212 Check_Wide_Character_Restriction (Id, N);
6214 -- If the Ekind of the entity is Void, it means that all homonyms are
6215 -- hidden from all visibility (RM 8.3(5,14-20)).
6217 if Ekind (Id) = E_Void then
6218 Premature_Usage (N);
6220 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6221 declare
6222 H : Entity_Id := Homonym (Id);
6224 begin
6225 while Present (H) loop
6226 if Scope (H) = Scope (Id)
6227 and then (not Is_Hidden (H)
6228 or else Is_Immediately_Visible (H))
6229 then
6230 Collect_Interps (N);
6231 exit;
6232 end if;
6234 H := Homonym (H);
6235 end loop;
6237 -- If an extension of System is present, collect possible explicit
6238 -- overloadings declared in the extension.
6240 if Chars (P_Name) = Name_System
6241 and then Scope (P_Name) = Standard_Standard
6242 and then Present (System_Extend_Unit)
6243 and then Present_System_Aux (N)
6244 then
6245 H := Current_Entity (Id);
6247 while Present (H) loop
6248 if Scope (H) = System_Aux_Id then
6249 Add_One_Interp (N, H, Etype (H));
6250 end if;
6252 H := Homonym (H);
6253 end loop;
6254 end if;
6255 end;
6256 end if;
6258 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6259 and then Scope (Id) /= Standard_Standard
6260 then
6261 -- In addition to user-defined operators in the given scope, there
6262 -- may be an implicit instance of the predefined operator. The
6263 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6264 -- and added to the interpretations. Procedure Add_One_Interp will
6265 -- determine which hides which.
6267 if Has_Implicit_Operator (N) then
6268 null;
6269 end if;
6270 end if;
6272 -- If there is a single interpretation for N we can generate a
6273 -- reference to the unique entity found.
6275 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6276 Generate_Reference (Id, N);
6277 end if;
6278 end Find_Expanded_Name;
6280 -------------------------
6281 -- Find_Renamed_Entity --
6282 -------------------------
6284 function Find_Renamed_Entity
6285 (N : Node_Id;
6286 Nam : Node_Id;
6287 New_S : Entity_Id;
6288 Is_Actual : Boolean := False) return Entity_Id
6290 Ind : Interp_Index;
6291 I1 : Interp_Index := 0; -- Suppress junk warnings
6292 It : Interp;
6293 It1 : Interp;
6294 Old_S : Entity_Id;
6295 Inst : Entity_Id;
6297 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
6298 -- If the renamed entity is an implicit operator, check whether it is
6299 -- visible because its operand type is properly visible. This check
6300 -- applies to explicit renamed entities that appear in the source in a
6301 -- renaming declaration or a formal subprogram instance, but not to
6302 -- default generic actuals with a name.
6304 function Report_Overload return Entity_Id;
6305 -- List possible interpretations, and specialize message in the
6306 -- case of a generic actual.
6308 function Within (Inner, Outer : Entity_Id) return Boolean;
6309 -- Determine whether a candidate subprogram is defined within the
6310 -- enclosing instance. If yes, it has precedence over outer candidates.
6312 --------------------------
6313 -- Is_Visible_Operation --
6314 --------------------------
6316 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
6317 Scop : Entity_Id;
6318 Typ : Entity_Id;
6319 Btyp : Entity_Id;
6321 begin
6322 if Ekind (Op) /= E_Operator
6323 or else Scope (Op) /= Standard_Standard
6324 or else (In_Instance
6325 and then (not Is_Actual
6326 or else Present (Enclosing_Instance)))
6327 then
6328 return True;
6330 else
6331 -- For a fixed point type operator, check the resulting type,
6332 -- because it may be a mixed mode integer * fixed operation.
6334 if Present (Next_Formal (First_Formal (New_S)))
6335 and then Is_Fixed_Point_Type (Etype (New_S))
6336 then
6337 Typ := Etype (New_S);
6338 else
6339 Typ := Etype (First_Formal (New_S));
6340 end if;
6342 Btyp := Base_Type (Typ);
6344 if Nkind (Nam) /= N_Expanded_Name then
6345 return (In_Open_Scopes (Scope (Btyp))
6346 or else Is_Potentially_Use_Visible (Btyp)
6347 or else In_Use (Btyp)
6348 or else In_Use (Scope (Btyp)));
6350 else
6351 Scop := Entity (Prefix (Nam));
6353 if Ekind (Scop) = E_Package
6354 and then Present (Renamed_Object (Scop))
6355 then
6356 Scop := Renamed_Object (Scop);
6357 end if;
6359 -- Operator is visible if prefix of expanded name denotes
6360 -- scope of type, or else type is defined in System_Aux
6361 -- and the prefix denotes System.
6363 return Scope (Btyp) = Scop
6364 or else (Scope (Btyp) = System_Aux_Id
6365 and then Scope (Scope (Btyp)) = Scop);
6366 end if;
6367 end if;
6368 end Is_Visible_Operation;
6370 ------------
6371 -- Within --
6372 ------------
6374 function Within (Inner, Outer : Entity_Id) return Boolean is
6375 Sc : Entity_Id;
6377 begin
6378 Sc := Scope (Inner);
6379 while Sc /= Standard_Standard loop
6380 if Sc = Outer then
6381 return True;
6382 else
6383 Sc := Scope (Sc);
6384 end if;
6385 end loop;
6387 return False;
6388 end Within;
6390 ---------------------
6391 -- Report_Overload --
6392 ---------------------
6394 function Report_Overload return Entity_Id is
6395 begin
6396 if Is_Actual then
6397 Error_Msg_NE -- CODEFIX
6398 ("ambiguous actual subprogram&, " &
6399 "possible interpretations:", N, Nam);
6400 else
6401 Error_Msg_N -- CODEFIX
6402 ("ambiguous subprogram, " &
6403 "possible interpretations:", N);
6404 end if;
6406 List_Interps (Nam, N);
6407 return Old_S;
6408 end Report_Overload;
6410 -- Start of processing for Find_Renamed_Entity
6412 begin
6413 Old_S := Any_Id;
6414 Candidate_Renaming := Empty;
6416 if Is_Overloaded (Nam) then
6417 Get_First_Interp (Nam, Ind, It);
6418 while Present (It.Nam) loop
6419 if Entity_Matches_Spec (It.Nam, New_S)
6420 and then Is_Visible_Operation (It.Nam)
6421 then
6422 if Old_S /= Any_Id then
6424 -- Note: The call to Disambiguate only happens if a
6425 -- previous interpretation was found, in which case I1
6426 -- has received a value.
6428 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
6430 if It1 = No_Interp then
6431 Inst := Enclosing_Instance;
6433 if Present (Inst) then
6434 if Within (It.Nam, Inst) then
6435 if Within (Old_S, Inst) then
6437 -- Choose the innermost subprogram, which would
6438 -- have hidden the outer one in the generic.
6440 if Scope_Depth (It.Nam) <
6441 Scope_Depth (Old_S)
6442 then
6443 return Old_S;
6444 else
6445 return It.Nam;
6446 end if;
6447 end if;
6449 elsif Within (Old_S, Inst) then
6450 return (Old_S);
6452 else
6453 return Report_Overload;
6454 end if;
6456 -- If not within an instance, ambiguity is real
6458 else
6459 return Report_Overload;
6460 end if;
6462 else
6463 Old_S := It1.Nam;
6464 exit;
6465 end if;
6467 else
6468 I1 := Ind;
6469 Old_S := It.Nam;
6470 end if;
6472 elsif
6473 Present (First_Formal (It.Nam))
6474 and then Present (First_Formal (New_S))
6475 and then (Base_Type (Etype (First_Formal (It.Nam))) =
6476 Base_Type (Etype (First_Formal (New_S))))
6477 then
6478 Candidate_Renaming := It.Nam;
6479 end if;
6481 Get_Next_Interp (Ind, It);
6482 end loop;
6484 Set_Entity (Nam, Old_S);
6486 if Old_S /= Any_Id then
6487 Set_Is_Overloaded (Nam, False);
6488 end if;
6490 -- Non-overloaded case
6492 else
6493 if Is_Actual and then Present (Enclosing_Instance) then
6494 Old_S := Entity (Nam);
6496 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
6497 Candidate_Renaming := New_S;
6499 if Is_Visible_Operation (Entity (Nam)) then
6500 Old_S := Entity (Nam);
6501 end if;
6503 elsif Present (First_Formal (Entity (Nam)))
6504 and then Present (First_Formal (New_S))
6505 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
6506 Base_Type (Etype (First_Formal (New_S))))
6507 then
6508 Candidate_Renaming := Entity (Nam);
6509 end if;
6510 end if;
6512 return Old_S;
6513 end Find_Renamed_Entity;
6515 -----------------------------
6516 -- Find_Selected_Component --
6517 -----------------------------
6519 procedure Find_Selected_Component (N : Node_Id) is
6520 P : constant Node_Id := Prefix (N);
6522 P_Name : Entity_Id;
6523 -- Entity denoted by prefix
6525 P_Type : Entity_Id;
6526 -- and its type
6528 Nam : Node_Id;
6530 function Available_Subtype return Boolean;
6531 -- A small optimization: if the prefix is constrained and the component
6532 -- is an array type we may already have a usable subtype for it, so we
6533 -- can use it rather than generating a new one, because the bounds
6534 -- will be the values of the discriminants and not discriminant refs.
6535 -- This simplifies value tracing in GNATProve. For consistency, both
6536 -- the entity name and the subtype come from the constrained component.
6538 function Is_Reference_In_Subunit return Boolean;
6539 -- In a subunit, the scope depth is not a proper measure of hiding,
6540 -- because the context of the proper body may itself hide entities in
6541 -- parent units. This rare case requires inspecting the tree directly
6542 -- because the proper body is inserted in the main unit and its context
6543 -- is simply added to that of the parent.
6545 -----------------------
6546 -- Available_Subtype --
6547 -----------------------
6549 function Available_Subtype return Boolean is
6550 Comp : Entity_Id;
6552 begin
6553 Comp := First_Entity (Etype (P));
6554 while Present (Comp) loop
6555 if Chars (Comp) = Chars (Selector_Name (N)) then
6556 Set_Etype (N, Etype (Comp));
6557 Set_Entity (Selector_Name (N), Comp);
6558 Set_Etype (Selector_Name (N), Etype (Comp));
6559 return True;
6560 end if;
6562 Next_Component (Comp);
6563 end loop;
6565 return False;
6566 end Available_Subtype;
6568 -----------------------------
6569 -- Is_Reference_In_Subunit --
6570 -----------------------------
6572 function Is_Reference_In_Subunit return Boolean is
6573 Clause : Node_Id;
6574 Comp_Unit : Node_Id;
6576 begin
6577 Comp_Unit := N;
6578 while Present (Comp_Unit)
6579 and then Nkind (Comp_Unit) /= N_Compilation_Unit
6580 loop
6581 Comp_Unit := Parent (Comp_Unit);
6582 end loop;
6584 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
6585 return False;
6586 end if;
6588 -- Now check whether the package is in the context of the subunit
6590 Clause := First (Context_Items (Comp_Unit));
6591 while Present (Clause) loop
6592 if Nkind (Clause) = N_With_Clause
6593 and then Entity (Name (Clause)) = P_Name
6594 then
6595 return True;
6596 end if;
6598 Clause := Next (Clause);
6599 end loop;
6601 return False;
6602 end Is_Reference_In_Subunit;
6604 -- Start of processing for Find_Selected_Component
6606 begin
6607 Analyze (P);
6609 if Nkind (P) = N_Error then
6610 return;
6611 end if;
6613 -- Selector name cannot be a character literal or an operator symbol in
6614 -- SPARK, except for the operator symbol in a renaming.
6616 if Restriction_Check_Required (SPARK_05) then
6617 if Nkind (Selector_Name (N)) = N_Character_Literal then
6618 Check_SPARK_05_Restriction
6619 ("character literal cannot be prefixed", N);
6620 elsif Nkind (Selector_Name (N)) = N_Operator_Symbol
6621 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
6622 then
6623 Check_SPARK_05_Restriction
6624 ("operator symbol cannot be prefixed", N);
6625 end if;
6626 end if;
6628 -- If the selector already has an entity, the node has been constructed
6629 -- in the course of expansion, and is known to be valid. Do not verify
6630 -- that it is defined for the type (it may be a private component used
6631 -- in the expansion of record equality).
6633 if Present (Entity (Selector_Name (N))) then
6634 if No (Etype (N)) or else Etype (N) = Any_Type then
6635 declare
6636 Sel_Name : constant Node_Id := Selector_Name (N);
6637 Selector : constant Entity_Id := Entity (Sel_Name);
6638 C_Etype : Node_Id;
6640 begin
6641 Set_Etype (Sel_Name, Etype (Selector));
6643 if not Is_Entity_Name (P) then
6644 Resolve (P);
6645 end if;
6647 -- Build an actual subtype except for the first parameter
6648 -- of an init proc, where this actual subtype is by
6649 -- definition incorrect, since the object is uninitialized
6650 -- (and does not even have defined discriminants etc.)
6652 if Is_Entity_Name (P)
6653 and then Ekind (Entity (P)) = E_Function
6654 then
6655 Nam := New_Copy (P);
6657 if Is_Overloaded (P) then
6658 Save_Interps (P, Nam);
6659 end if;
6661 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
6662 Analyze_Call (P);
6663 Analyze_Selected_Component (N);
6664 return;
6666 elsif Ekind (Selector) = E_Component
6667 and then (not Is_Entity_Name (P)
6668 or else Chars (Entity (P)) /= Name_uInit)
6669 then
6670 -- Check if we already have an available subtype we can use
6672 if Ekind (Etype (P)) = E_Record_Subtype
6673 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
6674 and then Is_Array_Type (Etype (Selector))
6675 and then not Is_Packed (Etype (Selector))
6676 and then Available_Subtype
6677 then
6678 return;
6680 -- Do not build the subtype when referencing components of
6681 -- dispatch table wrappers. Required to avoid generating
6682 -- elaboration code with HI runtimes. JVM and .NET use a
6683 -- modified version of Ada.Tags which does not contain RE_
6684 -- Dispatch_Table_Wrapper and RE_No_Dispatch_Table_Wrapper.
6685 -- Avoid raising RE_Not_Available exception in those cases.
6687 elsif VM_Target = No_VM
6688 and then RTU_Loaded (Ada_Tags)
6689 and then
6690 ((RTE_Available (RE_Dispatch_Table_Wrapper)
6691 and then Scope (Selector) =
6692 RTE (RE_Dispatch_Table_Wrapper))
6693 or else
6694 (RTE_Available (RE_No_Dispatch_Table_Wrapper)
6695 and then Scope (Selector) =
6696 RTE (RE_No_Dispatch_Table_Wrapper)))
6697 then
6698 C_Etype := Empty;
6699 else
6700 C_Etype :=
6701 Build_Actual_Subtype_Of_Component
6702 (Etype (Selector), N);
6703 end if;
6705 else
6706 C_Etype := Empty;
6707 end if;
6709 if No (C_Etype) then
6710 C_Etype := Etype (Selector);
6711 else
6712 Insert_Action (N, C_Etype);
6713 C_Etype := Defining_Identifier (C_Etype);
6714 end if;
6716 Set_Etype (N, C_Etype);
6717 end;
6719 -- If this is the name of an entry or protected operation, and
6720 -- the prefix is an access type, insert an explicit dereference,
6721 -- so that entry calls are treated uniformly.
6723 if Is_Access_Type (Etype (P))
6724 and then Is_Concurrent_Type (Designated_Type (Etype (P)))
6725 then
6726 declare
6727 New_P : constant Node_Id :=
6728 Make_Explicit_Dereference (Sloc (P),
6729 Prefix => Relocate_Node (P));
6730 begin
6731 Rewrite (P, New_P);
6732 Set_Etype (P, Designated_Type (Etype (Prefix (P))));
6733 end;
6734 end if;
6736 -- If the selected component appears within a default expression
6737 -- and it has an actual subtype, the pre-analysis has not yet
6738 -- completed its analysis, because Insert_Actions is disabled in
6739 -- that context. Within the init proc of the enclosing type we
6740 -- must complete this analysis, if an actual subtype was created.
6742 elsif Inside_Init_Proc then
6743 declare
6744 Typ : constant Entity_Id := Etype (N);
6745 Decl : constant Node_Id := Declaration_Node (Typ);
6746 begin
6747 if Nkind (Decl) = N_Subtype_Declaration
6748 and then not Analyzed (Decl)
6749 and then Is_List_Member (Decl)
6750 and then No (Parent (Decl))
6751 then
6752 Remove (Decl);
6753 Insert_Action (N, Decl);
6754 end if;
6755 end;
6756 end if;
6758 return;
6760 elsif Is_Entity_Name (P) then
6761 P_Name := Entity (P);
6763 -- The prefix may denote an enclosing type which is the completion
6764 -- of an incomplete type declaration.
6766 if Is_Type (P_Name) then
6767 Set_Entity (P, Get_Full_View (P_Name));
6768 Set_Etype (P, Entity (P));
6769 P_Name := Entity (P);
6770 end if;
6772 P_Type := Base_Type (Etype (P));
6774 if Debug_Flag_E then
6775 Write_Str ("Found prefix type to be ");
6776 Write_Entity_Info (P_Type, " "); Write_Eol;
6777 end if;
6779 -- The designated type may be a limited view with no components.
6780 -- Check whether the non-limited view is available, because in some
6781 -- cases this will not be set when installing the context.
6783 if Is_Access_Type (P_Type) then
6784 declare
6785 D : constant Entity_Id := Directly_Designated_Type (P_Type);
6786 begin
6787 if Is_Incomplete_Type (D)
6788 and then From_Limited_With (D)
6789 and then Present (Non_Limited_View (D))
6790 then
6791 Set_Directly_Designated_Type (P_Type, Non_Limited_View (D));
6792 end if;
6793 end;
6794 end if;
6796 -- First check for components of a record object (not the
6797 -- result of a call, which is handled below).
6799 if Is_Appropriate_For_Record (P_Type)
6800 and then not Is_Overloadable (P_Name)
6801 and then not Is_Type (P_Name)
6802 then
6803 -- Selected component of record. Type checking will validate
6804 -- name of selector.
6806 -- ??? Could we rewrite an implicit dereference into an explicit
6807 -- one here?
6809 Analyze_Selected_Component (N);
6811 -- Reference to type name in predicate/invariant expression
6813 elsif Is_Appropriate_For_Entry_Prefix (P_Type)
6814 and then not In_Open_Scopes (P_Name)
6815 and then (not Is_Concurrent_Type (Etype (P_Name))
6816 or else not In_Open_Scopes (Etype (P_Name)))
6817 then
6818 -- Call to protected operation or entry. Type checking is
6819 -- needed on the prefix.
6821 Analyze_Selected_Component (N);
6823 elsif (In_Open_Scopes (P_Name)
6824 and then Ekind (P_Name) /= E_Void
6825 and then not Is_Overloadable (P_Name))
6826 or else (Is_Concurrent_Type (Etype (P_Name))
6827 and then In_Open_Scopes (Etype (P_Name)))
6828 then
6829 -- Prefix denotes an enclosing loop, block, or task, i.e. an
6830 -- enclosing construct that is not a subprogram or accept.
6832 Find_Expanded_Name (N);
6834 elsif Ekind (P_Name) = E_Package then
6835 Find_Expanded_Name (N);
6837 elsif Is_Overloadable (P_Name) then
6839 -- The subprogram may be a renaming (of an enclosing scope) as
6840 -- in the case of the name of the generic within an instantiation.
6842 if Ekind_In (P_Name, E_Procedure, E_Function)
6843 and then Present (Alias (P_Name))
6844 and then Is_Generic_Instance (Alias (P_Name))
6845 then
6846 P_Name := Alias (P_Name);
6847 end if;
6849 if Is_Overloaded (P) then
6851 -- The prefix must resolve to a unique enclosing construct
6853 declare
6854 Found : Boolean := False;
6855 Ind : Interp_Index;
6856 It : Interp;
6858 begin
6859 Get_First_Interp (P, Ind, It);
6860 while Present (It.Nam) loop
6861 if In_Open_Scopes (It.Nam) then
6862 if Found then
6863 Error_Msg_N (
6864 "prefix must be unique enclosing scope", N);
6865 Set_Entity (N, Any_Id);
6866 Set_Etype (N, Any_Type);
6867 return;
6869 else
6870 Found := True;
6871 P_Name := It.Nam;
6872 end if;
6873 end if;
6875 Get_Next_Interp (Ind, It);
6876 end loop;
6877 end;
6878 end if;
6880 if In_Open_Scopes (P_Name) then
6881 Set_Entity (P, P_Name);
6882 Set_Is_Overloaded (P, False);
6883 Find_Expanded_Name (N);
6885 else
6886 -- If no interpretation as an expanded name is possible, it
6887 -- must be a selected component of a record returned by a
6888 -- function call. Reformat prefix as a function call, the rest
6889 -- is done by type resolution.
6891 -- Error if the prefix is procedure or entry, as is P.X
6893 if Ekind (P_Name) /= E_Function
6894 and then
6895 (not Is_Overloaded (P)
6896 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
6897 then
6898 -- Prefix may mention a package that is hidden by a local
6899 -- declaration: let the user know. Scan the full homonym
6900 -- chain, the candidate package may be anywhere on it.
6902 if Present (Homonym (Current_Entity (P_Name))) then
6903 P_Name := Current_Entity (P_Name);
6905 while Present (P_Name) loop
6906 exit when Ekind (P_Name) = E_Package;
6907 P_Name := Homonym (P_Name);
6908 end loop;
6910 if Present (P_Name) then
6911 if not Is_Reference_In_Subunit then
6912 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
6913 Error_Msg_NE
6914 ("package& is hidden by declaration#", N, P_Name);
6915 end if;
6917 Set_Entity (Prefix (N), P_Name);
6918 Find_Expanded_Name (N);
6919 return;
6921 else
6922 P_Name := Entity (Prefix (N));
6923 end if;
6924 end if;
6926 Error_Msg_NE
6927 ("invalid prefix in selected component&", N, P_Name);
6928 Change_Selected_Component_To_Expanded_Name (N);
6929 Set_Entity (N, Any_Id);
6930 Set_Etype (N, Any_Type);
6932 -- Here we have a function call, so do the reformatting
6934 else
6935 Nam := New_Copy (P);
6936 Save_Interps (P, Nam);
6938 -- We use Replace here because this is one of those cases
6939 -- where the parser has missclassified the node, and we
6940 -- fix things up and then do the semantic analysis on the
6941 -- fixed up node. Normally we do this using one of the
6942 -- Sinfo.CN routines, but this is too tricky for that.
6944 -- Note that using Rewrite would be wrong, because we
6945 -- would have a tree where the original node is unanalyzed,
6946 -- and this violates the required interface for ASIS.
6948 Replace (P,
6949 Make_Function_Call (Sloc (P), Name => Nam));
6951 -- Now analyze the reformatted node
6953 Analyze_Call (P);
6954 Analyze_Selected_Component (N);
6955 end if;
6956 end if;
6958 -- Remaining cases generate various error messages
6960 else
6961 -- Format node as expanded name, to avoid cascaded errors
6963 Change_Selected_Component_To_Expanded_Name (N);
6964 Set_Entity (N, Any_Id);
6965 Set_Etype (N, Any_Type);
6967 -- Issue error message, but avoid this if error issued already.
6968 -- Use identifier of prefix if one is available.
6970 if P_Name = Any_Id then
6971 null;
6973 -- It is not an error if the prefix is the current instance of
6974 -- type name, e.g. the expression of a type aspect, when it is
6975 -- analyzed for ASIS use.
6977 elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then
6978 null;
6980 elsif Ekind (P_Name) = E_Void then
6981 Premature_Usage (P);
6983 elsif Nkind (P) /= N_Attribute_Reference then
6985 -- This may have been meant as a prefixed call to a primitive
6986 -- of an untagged type.
6988 declare
6989 F : constant Entity_Id :=
6990 Current_Entity (Selector_Name (N));
6991 begin
6992 if Present (F)
6993 and then Is_Overloadable (F)
6994 and then Present (First_Entity (F))
6995 and then Etype (First_Entity (F)) = Etype (P)
6996 and then not Is_Tagged_Type (Etype (P))
6997 then
6998 Error_Msg_N
6999 ("prefixed call is only allowed for objects "
7000 & "of a tagged type", N);
7001 end if;
7002 end;
7004 Error_Msg_N ("invalid prefix in selected component&", P);
7006 if Is_Access_Type (P_Type)
7007 and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
7008 then
7009 Error_Msg_N
7010 ("\dereference must not be of an incomplete type "
7011 & "(RM 3.10.1)", P);
7012 end if;
7014 else
7015 Error_Msg_N ("invalid prefix in selected component", P);
7016 end if;
7017 end if;
7019 -- Selector name is restricted in SPARK
7021 if Nkind (N) = N_Expanded_Name
7022 and then Restriction_Check_Required (SPARK_05)
7023 then
7024 if Is_Subprogram (P_Name) then
7025 Check_SPARK_05_Restriction
7026 ("prefix of expanded name cannot be a subprogram", P);
7027 elsif Ekind (P_Name) = E_Loop then
7028 Check_SPARK_05_Restriction
7029 ("prefix of expanded name cannot be a loop statement", P);
7030 end if;
7031 end if;
7033 else
7034 -- If prefix is not the name of an entity, it must be an expression,
7035 -- whose type is appropriate for a record. This is determined by
7036 -- type resolution.
7038 Analyze_Selected_Component (N);
7039 end if;
7041 Analyze_Dimension (N);
7042 end Find_Selected_Component;
7044 ---------------
7045 -- Find_Type --
7046 ---------------
7048 procedure Find_Type (N : Node_Id) is
7049 C : Entity_Id;
7050 Typ : Entity_Id;
7051 T : Entity_Id;
7052 T_Name : Entity_Id;
7054 begin
7055 if N = Error then
7056 return;
7058 elsif Nkind (N) = N_Attribute_Reference then
7060 -- Class attribute. This is not valid in Ada 83 mode, but we do not
7061 -- need to enforce that at this point, since the declaration of the
7062 -- tagged type in the prefix would have been flagged already.
7064 if Attribute_Name (N) = Name_Class then
7065 Check_Restriction (No_Dispatch, N);
7066 Find_Type (Prefix (N));
7068 -- Propagate error from bad prefix
7070 if Etype (Prefix (N)) = Any_Type then
7071 Set_Entity (N, Any_Type);
7072 Set_Etype (N, Any_Type);
7073 return;
7074 end if;
7076 T := Base_Type (Entity (Prefix (N)));
7078 -- Case where type is not known to be tagged. Its appearance in
7079 -- the prefix of the 'Class attribute indicates that the full view
7080 -- will be tagged.
7082 if not Is_Tagged_Type (T) then
7083 if Ekind (T) = E_Incomplete_Type then
7085 -- It is legal to denote the class type of an incomplete
7086 -- type. The full type will have to be tagged, of course.
7087 -- In Ada 2005 this usage is declared obsolescent, so we
7088 -- warn accordingly. This usage is only legal if the type
7089 -- is completed in the current scope, and not for a limited
7090 -- view of a type.
7092 if Ada_Version >= Ada_2005 then
7094 -- Test whether the Available_View of a limited type view
7095 -- is tagged, since the limited view may not be marked as
7096 -- tagged if the type itself has an untagged incomplete
7097 -- type view in its package.
7099 if From_Limited_With (T)
7100 and then not Is_Tagged_Type (Available_View (T))
7101 then
7102 Error_Msg_N
7103 ("prefix of Class attribute must be tagged", N);
7104 Set_Etype (N, Any_Type);
7105 Set_Entity (N, Any_Type);
7106 return;
7108 -- ??? This test is temporarily disabled (always
7109 -- False) because it causes an unwanted warning on
7110 -- GNAT sources (built with -gnatg, which includes
7111 -- Warn_On_Obsolescent_ Feature). Once this issue
7112 -- is cleared in the sources, it can be enabled.
7114 elsif Warn_On_Obsolescent_Feature and then False then
7115 Error_Msg_N
7116 ("applying 'Class to an untagged incomplete type"
7117 & " is an obsolescent feature (RM J.11)?r?", N);
7118 end if;
7119 end if;
7121 Set_Is_Tagged_Type (T);
7122 Set_Direct_Primitive_Operations (T, New_Elmt_List);
7123 Make_Class_Wide_Type (T);
7124 Set_Entity (N, Class_Wide_Type (T));
7125 Set_Etype (N, Class_Wide_Type (T));
7127 elsif Ekind (T) = E_Private_Type
7128 and then not Is_Generic_Type (T)
7129 and then In_Private_Part (Scope (T))
7130 then
7131 -- The Class attribute can be applied to an untagged private
7132 -- type fulfilled by a tagged type prior to the full type
7133 -- declaration (but only within the parent package's private
7134 -- part). Create the class-wide type now and check that the
7135 -- full type is tagged later during its analysis. Note that
7136 -- we do not mark the private type as tagged, unlike the
7137 -- case of incomplete types, because the type must still
7138 -- appear untagged to outside units.
7140 if No (Class_Wide_Type (T)) then
7141 Make_Class_Wide_Type (T);
7142 end if;
7144 Set_Entity (N, Class_Wide_Type (T));
7145 Set_Etype (N, Class_Wide_Type (T));
7147 else
7148 -- Should we introduce a type Any_Tagged and use Wrong_Type
7149 -- here, it would be a bit more consistent???
7151 Error_Msg_NE
7152 ("tagged type required, found}",
7153 Prefix (N), First_Subtype (T));
7154 Set_Entity (N, Any_Type);
7155 return;
7156 end if;
7158 -- Case of tagged type
7160 else
7161 if Is_Concurrent_Type (T) then
7162 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
7164 -- Previous error. Use current type, which at least
7165 -- provides some operations.
7167 C := Entity (Prefix (N));
7169 else
7170 C := Class_Wide_Type
7171 (Corresponding_Record_Type (Entity (Prefix (N))));
7172 end if;
7174 else
7175 C := Class_Wide_Type (Entity (Prefix (N)));
7176 end if;
7178 Set_Entity_With_Checks (N, C);
7179 Generate_Reference (C, N);
7180 Set_Etype (N, C);
7181 end if;
7183 -- Base attribute, not allowed in Ada 83
7185 elsif Attribute_Name (N) = Name_Base then
7186 Error_Msg_Name_1 := Name_Base;
7187 Check_SPARK_05_Restriction
7188 ("attribute% is only allowed as prefix of another attribute", N);
7190 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
7191 Error_Msg_N
7192 ("(Ada 83) Base attribute not allowed in subtype mark", N);
7194 else
7195 Find_Type (Prefix (N));
7196 Typ := Entity (Prefix (N));
7198 if Ada_Version >= Ada_95
7199 and then not Is_Scalar_Type (Typ)
7200 and then not Is_Generic_Type (Typ)
7201 then
7202 Error_Msg_N
7203 ("prefix of Base attribute must be scalar type",
7204 Prefix (N));
7206 elsif Warn_On_Redundant_Constructs
7207 and then Base_Type (Typ) = Typ
7208 then
7209 Error_Msg_NE -- CODEFIX
7210 ("redundant attribute, & is its own base type?r?", N, Typ);
7211 end if;
7213 T := Base_Type (Typ);
7215 -- Rewrite attribute reference with type itself (see similar
7216 -- processing in Analyze_Attribute, case Base). Preserve prefix
7217 -- if present, for other legality checks.
7219 if Nkind (Prefix (N)) = N_Expanded_Name then
7220 Rewrite (N,
7221 Make_Expanded_Name (Sloc (N),
7222 Chars => Chars (T),
7223 Prefix => New_Copy (Prefix (Prefix (N))),
7224 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
7226 else
7227 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
7228 end if;
7230 Set_Entity (N, T);
7231 Set_Etype (N, T);
7232 end if;
7234 elsif Attribute_Name (N) = Name_Stub_Type then
7236 -- This is handled in Analyze_Attribute
7238 Analyze (N);
7240 -- All other attributes are invalid in a subtype mark
7242 else
7243 Error_Msg_N ("invalid attribute in subtype mark", N);
7244 end if;
7246 else
7247 Analyze (N);
7249 if Is_Entity_Name (N) then
7250 T_Name := Entity (N);
7251 else
7252 Error_Msg_N ("subtype mark required in this context", N);
7253 Set_Etype (N, Any_Type);
7254 return;
7255 end if;
7257 if T_Name = Any_Id or else Etype (N) = Any_Type then
7259 -- Undefined id. Make it into a valid type
7261 Set_Entity (N, Any_Type);
7263 elsif not Is_Type (T_Name)
7264 and then T_Name /= Standard_Void_Type
7265 then
7266 Error_Msg_Sloc := Sloc (T_Name);
7267 Error_Msg_N ("subtype mark required in this context", N);
7268 Error_Msg_NE ("\\found & declared#", N, T_Name);
7269 Set_Entity (N, Any_Type);
7271 else
7272 -- If the type is an incomplete type created to handle
7273 -- anonymous access components of a record type, then the
7274 -- incomplete type is the visible entity and subsequent
7275 -- references will point to it. Mark the original full
7276 -- type as referenced, to prevent spurious warnings.
7278 if Is_Incomplete_Type (T_Name)
7279 and then Present (Full_View (T_Name))
7280 and then not Comes_From_Source (T_Name)
7281 then
7282 Set_Referenced (Full_View (T_Name));
7283 end if;
7285 T_Name := Get_Full_View (T_Name);
7287 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
7288 -- limited-with clauses
7290 if From_Limited_With (T_Name)
7291 and then Ekind (T_Name) in Incomplete_Kind
7292 and then Present (Non_Limited_View (T_Name))
7293 and then Is_Interface (Non_Limited_View (T_Name))
7294 then
7295 T_Name := Non_Limited_View (T_Name);
7296 end if;
7298 if In_Open_Scopes (T_Name) then
7299 if Ekind (Base_Type (T_Name)) = E_Task_Type then
7301 -- In Ada 2005, a task name can be used in an access
7302 -- definition within its own body. It cannot be used
7303 -- in the discriminant part of the task declaration,
7304 -- nor anywhere else in the declaration because entries
7305 -- cannot have access parameters.
7307 if Ada_Version >= Ada_2005
7308 and then Nkind (Parent (N)) = N_Access_Definition
7309 then
7310 Set_Entity (N, T_Name);
7311 Set_Etype (N, T_Name);
7313 if Has_Completion (T_Name) then
7314 return;
7316 else
7317 Error_Msg_N
7318 ("task type cannot be used as type mark " &
7319 "within its own declaration", N);
7320 end if;
7322 else
7323 Error_Msg_N
7324 ("task type cannot be used as type mark " &
7325 "within its own spec or body", N);
7326 end if;
7328 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
7330 -- In Ada 2005, a protected name can be used in an access
7331 -- definition within its own body.
7333 if Ada_Version >= Ada_2005
7334 and then Nkind (Parent (N)) = N_Access_Definition
7335 then
7336 Set_Entity (N, T_Name);
7337 Set_Etype (N, T_Name);
7338 return;
7340 else
7341 Error_Msg_N
7342 ("protected type cannot be used as type mark " &
7343 "within its own spec or body", N);
7344 end if;
7346 else
7347 Error_Msg_N ("type declaration cannot refer to itself", N);
7348 end if;
7350 Set_Etype (N, Any_Type);
7351 Set_Entity (N, Any_Type);
7352 Set_Error_Posted (T_Name);
7353 return;
7354 end if;
7356 Set_Entity (N, T_Name);
7357 Set_Etype (N, T_Name);
7358 end if;
7359 end if;
7361 if Present (Etype (N)) and then Comes_From_Source (N) then
7362 if Is_Fixed_Point_Type (Etype (N)) then
7363 Check_Restriction (No_Fixed_Point, N);
7364 elsif Is_Floating_Point_Type (Etype (N)) then
7365 Check_Restriction (No_Floating_Point, N);
7366 end if;
7368 -- A Ghost type must appear in a specific context
7370 if Is_Ghost_Entity (Etype (N)) then
7371 Check_Ghost_Context (Etype (N), N);
7372 end if;
7373 end if;
7374 end Find_Type;
7376 ------------------------------------
7377 -- Has_Implicit_Character_Literal --
7378 ------------------------------------
7380 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
7381 Id : Entity_Id;
7382 Found : Boolean := False;
7383 P : constant Entity_Id := Entity (Prefix (N));
7384 Priv_Id : Entity_Id := Empty;
7386 begin
7387 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
7388 Priv_Id := First_Private_Entity (P);
7389 end if;
7391 if P = Standard_Standard then
7392 Change_Selected_Component_To_Expanded_Name (N);
7393 Rewrite (N, Selector_Name (N));
7394 Analyze (N);
7395 Set_Etype (Original_Node (N), Standard_Character);
7396 return True;
7397 end if;
7399 Id := First_Entity (P);
7400 while Present (Id) and then Id /= Priv_Id loop
7401 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
7403 -- We replace the node with the literal itself, resolve as a
7404 -- character, and set the type correctly.
7406 if not Found then
7407 Change_Selected_Component_To_Expanded_Name (N);
7408 Rewrite (N, Selector_Name (N));
7409 Analyze (N);
7410 Set_Etype (N, Id);
7411 Set_Etype (Original_Node (N), Id);
7412 Found := True;
7414 else
7415 -- More than one type derived from Character in given scope.
7416 -- Collect all possible interpretations.
7418 Add_One_Interp (N, Id, Id);
7419 end if;
7420 end if;
7422 Next_Entity (Id);
7423 end loop;
7425 return Found;
7426 end Has_Implicit_Character_Literal;
7428 ----------------------
7429 -- Has_Private_With --
7430 ----------------------
7432 function Has_Private_With (E : Entity_Id) return Boolean is
7433 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
7434 Item : Node_Id;
7436 begin
7437 Item := First (Context_Items (Comp_Unit));
7438 while Present (Item) loop
7439 if Nkind (Item) = N_With_Clause
7440 and then Private_Present (Item)
7441 and then Entity (Name (Item)) = E
7442 then
7443 return True;
7444 end if;
7446 Next (Item);
7447 end loop;
7449 return False;
7450 end Has_Private_With;
7452 ---------------------------
7453 -- Has_Implicit_Operator --
7454 ---------------------------
7456 function Has_Implicit_Operator (N : Node_Id) return Boolean is
7457 Op_Id : constant Name_Id := Chars (Selector_Name (N));
7458 P : constant Entity_Id := Entity (Prefix (N));
7459 Id : Entity_Id;
7460 Priv_Id : Entity_Id := Empty;
7462 procedure Add_Implicit_Operator
7463 (T : Entity_Id;
7464 Op_Type : Entity_Id := Empty);
7465 -- Add implicit interpretation to node N, using the type for which a
7466 -- predefined operator exists. If the operator yields a boolean type,
7467 -- the Operand_Type is implicitly referenced by the operator, and a
7468 -- reference to it must be generated.
7470 ---------------------------
7471 -- Add_Implicit_Operator --
7472 ---------------------------
7474 procedure Add_Implicit_Operator
7475 (T : Entity_Id;
7476 Op_Type : Entity_Id := Empty)
7478 Predef_Op : Entity_Id;
7480 begin
7481 Predef_Op := Current_Entity (Selector_Name (N));
7482 while Present (Predef_Op)
7483 and then Scope (Predef_Op) /= Standard_Standard
7484 loop
7485 Predef_Op := Homonym (Predef_Op);
7486 end loop;
7488 if Nkind (N) = N_Selected_Component then
7489 Change_Selected_Component_To_Expanded_Name (N);
7490 end if;
7492 -- If the context is an unanalyzed function call, determine whether
7493 -- a binary or unary interpretation is required.
7495 if Nkind (Parent (N)) = N_Indexed_Component then
7496 declare
7497 Is_Binary_Call : constant Boolean :=
7498 Present
7499 (Next (First (Expressions (Parent (N)))));
7500 Is_Binary_Op : constant Boolean :=
7501 First_Entity
7502 (Predef_Op) /= Last_Entity (Predef_Op);
7503 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
7505 begin
7506 if Is_Binary_Call then
7507 if Is_Binary_Op then
7508 Add_One_Interp (N, Predef_Op, T);
7509 else
7510 Add_One_Interp (N, Predef_Op2, T);
7511 end if;
7513 else
7514 if not Is_Binary_Op then
7515 Add_One_Interp (N, Predef_Op, T);
7516 else
7517 Add_One_Interp (N, Predef_Op2, T);
7518 end if;
7519 end if;
7520 end;
7522 else
7523 Add_One_Interp (N, Predef_Op, T);
7525 -- For operators with unary and binary interpretations, if
7526 -- context is not a call, add both
7528 if Present (Homonym (Predef_Op)) then
7529 Add_One_Interp (N, Homonym (Predef_Op), T);
7530 end if;
7531 end if;
7533 -- The node is a reference to a predefined operator, and
7534 -- an implicit reference to the type of its operands.
7536 if Present (Op_Type) then
7537 Generate_Operator_Reference (N, Op_Type);
7538 else
7539 Generate_Operator_Reference (N, T);
7540 end if;
7541 end Add_Implicit_Operator;
7543 -- Start of processing for Has_Implicit_Operator
7545 begin
7546 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
7547 Priv_Id := First_Private_Entity (P);
7548 end if;
7550 Id := First_Entity (P);
7552 case Op_Id is
7554 -- Boolean operators: an implicit declaration exists if the scope
7555 -- contains a declaration for a derived Boolean type, or for an
7556 -- array of Boolean type.
7558 when Name_Op_And | Name_Op_Not | Name_Op_Or | Name_Op_Xor =>
7559 while Id /= Priv_Id loop
7560 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
7561 Add_Implicit_Operator (Id);
7562 return True;
7563 end if;
7565 Next_Entity (Id);
7566 end loop;
7568 -- Equality: look for any non-limited type (result is Boolean)
7570 when Name_Op_Eq | Name_Op_Ne =>
7571 while Id /= Priv_Id loop
7572 if Is_Type (Id)
7573 and then not Is_Limited_Type (Id)
7574 and then Is_Base_Type (Id)
7575 then
7576 Add_Implicit_Operator (Standard_Boolean, Id);
7577 return True;
7578 end if;
7580 Next_Entity (Id);
7581 end loop;
7583 -- Comparison operators: scalar type, or array of scalar
7585 when Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge =>
7586 while Id /= Priv_Id loop
7587 if (Is_Scalar_Type (Id)
7588 or else (Is_Array_Type (Id)
7589 and then Is_Scalar_Type (Component_Type (Id))))
7590 and then Is_Base_Type (Id)
7591 then
7592 Add_Implicit_Operator (Standard_Boolean, Id);
7593 return True;
7594 end if;
7596 Next_Entity (Id);
7597 end loop;
7599 -- Arithmetic operators: any numeric type
7601 when Name_Op_Abs |
7602 Name_Op_Add |
7603 Name_Op_Mod |
7604 Name_Op_Rem |
7605 Name_Op_Subtract |
7606 Name_Op_Multiply |
7607 Name_Op_Divide |
7608 Name_Op_Expon =>
7609 while Id /= Priv_Id loop
7610 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
7611 Add_Implicit_Operator (Id);
7612 return True;
7613 end if;
7615 Next_Entity (Id);
7616 end loop;
7618 -- Concatenation: any one-dimensional array type
7620 when Name_Op_Concat =>
7621 while Id /= Priv_Id loop
7622 if Is_Array_Type (Id)
7623 and then Number_Dimensions (Id) = 1
7624 and then Is_Base_Type (Id)
7625 then
7626 Add_Implicit_Operator (Id);
7627 return True;
7628 end if;
7630 Next_Entity (Id);
7631 end loop;
7633 -- What is the others condition here? Should we be using a
7634 -- subtype of Name_Id that would restrict to operators ???
7636 when others => null;
7637 end case;
7639 -- If we fall through, then we do not have an implicit operator
7641 return False;
7643 end Has_Implicit_Operator;
7645 -----------------------------------
7646 -- Has_Loop_In_Inner_Open_Scopes --
7647 -----------------------------------
7649 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
7650 begin
7651 -- Several scope stacks are maintained by Scope_Stack. The base of the
7652 -- currently active scope stack is denoted by the Is_Active_Stack_Base
7653 -- flag in the scope stack entry. Note that the scope stacks used to
7654 -- simply be delimited implicitly by the presence of Standard_Standard
7655 -- at their base, but there now are cases where this is not sufficient
7656 -- because Standard_Standard actually may appear in the middle of the
7657 -- active set of scopes.
7659 for J in reverse 0 .. Scope_Stack.Last loop
7661 -- S was reached without seing a loop scope first
7663 if Scope_Stack.Table (J).Entity = S then
7664 return False;
7666 -- S was not yet reached, so it contains at least one inner loop
7668 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
7669 return True;
7670 end if;
7672 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
7673 -- cases where Standard_Standard appears in the middle of the active
7674 -- set of scopes. This affects the declaration and overriding of
7675 -- private inherited operations in instantiations of generic child
7676 -- units.
7678 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
7679 end loop;
7681 raise Program_Error; -- unreachable
7682 end Has_Loop_In_Inner_Open_Scopes;
7684 --------------------
7685 -- In_Open_Scopes --
7686 --------------------
7688 function In_Open_Scopes (S : Entity_Id) return Boolean is
7689 begin
7690 -- Several scope stacks are maintained by Scope_Stack. The base of the
7691 -- currently active scope stack is denoted by the Is_Active_Stack_Base
7692 -- flag in the scope stack entry. Note that the scope stacks used to
7693 -- simply be delimited implicitly by the presence of Standard_Standard
7694 -- at their base, but there now are cases where this is not sufficient
7695 -- because Standard_Standard actually may appear in the middle of the
7696 -- active set of scopes.
7698 for J in reverse 0 .. Scope_Stack.Last loop
7699 if Scope_Stack.Table (J).Entity = S then
7700 return True;
7701 end if;
7703 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
7704 -- cases where Standard_Standard appears in the middle of the active
7705 -- set of scopes. This affects the declaration and overriding of
7706 -- private inherited operations in instantiations of generic child
7707 -- units.
7709 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
7710 end loop;
7712 return False;
7713 end In_Open_Scopes;
7715 -----------------------------
7716 -- Inherit_Renamed_Profile --
7717 -----------------------------
7719 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
7720 New_F : Entity_Id;
7721 Old_F : Entity_Id;
7722 Old_T : Entity_Id;
7723 New_T : Entity_Id;
7725 begin
7726 if Ekind (Old_S) = E_Operator then
7727 New_F := First_Formal (New_S);
7729 while Present (New_F) loop
7730 Set_Etype (New_F, Base_Type (Etype (New_F)));
7731 Next_Formal (New_F);
7732 end loop;
7734 Set_Etype (New_S, Base_Type (Etype (New_S)));
7736 else
7737 New_F := First_Formal (New_S);
7738 Old_F := First_Formal (Old_S);
7740 while Present (New_F) loop
7741 New_T := Etype (New_F);
7742 Old_T := Etype (Old_F);
7744 -- If the new type is a renaming of the old one, as is the
7745 -- case for actuals in instances, retain its name, to simplify
7746 -- later disambiguation.
7748 if Nkind (Parent (New_T)) = N_Subtype_Declaration
7749 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
7750 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
7751 then
7752 null;
7753 else
7754 Set_Etype (New_F, Old_T);
7755 end if;
7757 Next_Formal (New_F);
7758 Next_Formal (Old_F);
7759 end loop;
7761 if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then
7762 Set_Etype (New_S, Etype (Old_S));
7763 end if;
7764 end if;
7765 end Inherit_Renamed_Profile;
7767 ----------------
7768 -- Initialize --
7769 ----------------
7771 procedure Initialize is
7772 begin
7773 Urefs.Init;
7774 end Initialize;
7776 -------------------------
7777 -- Install_Use_Clauses --
7778 -------------------------
7780 procedure Install_Use_Clauses
7781 (Clause : Node_Id;
7782 Force_Installation : Boolean := False)
7784 U : Node_Id;
7785 P : Node_Id;
7786 Id : Entity_Id;
7788 begin
7789 U := Clause;
7790 while Present (U) loop
7792 -- Case of USE package
7794 if Nkind (U) = N_Use_Package_Clause then
7795 P := First (Names (U));
7796 while Present (P) loop
7797 Id := Entity (P);
7799 if Ekind (Id) = E_Package then
7800 if In_Use (Id) then
7801 Note_Redundant_Use (P);
7803 elsif Present (Renamed_Object (Id))
7804 and then In_Use (Renamed_Object (Id))
7805 then
7806 Note_Redundant_Use (P);
7808 elsif Force_Installation or else Applicable_Use (P) then
7809 Use_One_Package (Id, U);
7811 end if;
7812 end if;
7814 Next (P);
7815 end loop;
7817 -- Case of USE TYPE
7819 else
7820 P := First (Subtype_Marks (U));
7821 while Present (P) loop
7822 if not Is_Entity_Name (P)
7823 or else No (Entity (P))
7824 then
7825 null;
7827 elsif Entity (P) /= Any_Type then
7828 Use_One_Type (P);
7829 end if;
7831 Next (P);
7832 end loop;
7833 end if;
7835 Next_Use_Clause (U);
7836 end loop;
7837 end Install_Use_Clauses;
7839 -------------------------------------
7840 -- Is_Appropriate_For_Entry_Prefix --
7841 -------------------------------------
7843 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
7844 P_Type : Entity_Id := T;
7846 begin
7847 if Is_Access_Type (P_Type) then
7848 P_Type := Designated_Type (P_Type);
7849 end if;
7851 return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
7852 end Is_Appropriate_For_Entry_Prefix;
7854 -------------------------------
7855 -- Is_Appropriate_For_Record --
7856 -------------------------------
7858 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is
7860 function Has_Components (T1 : Entity_Id) return Boolean;
7861 -- Determine if given type has components (i.e. is either a record
7862 -- type or a type that has discriminants).
7864 --------------------
7865 -- Has_Components --
7866 --------------------
7868 function Has_Components (T1 : Entity_Id) return Boolean is
7869 begin
7870 return Is_Record_Type (T1)
7871 or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
7872 or else (Is_Task_Type (T1) and then Has_Discriminants (T1))
7873 or else (Is_Incomplete_Type (T1)
7874 and then From_Limited_With (T1)
7875 and then Present (Non_Limited_View (T1))
7876 and then Is_Record_Type
7877 (Get_Full_View (Non_Limited_View (T1))));
7878 end Has_Components;
7880 -- Start of processing for Is_Appropriate_For_Record
7882 begin
7883 return
7884 Present (T)
7885 and then (Has_Components (T)
7886 or else (Is_Access_Type (T)
7887 and then Has_Components (Designated_Type (T))));
7888 end Is_Appropriate_For_Record;
7890 ------------------------
7891 -- Note_Redundant_Use --
7892 ------------------------
7894 procedure Note_Redundant_Use (Clause : Node_Id) is
7895 Pack_Name : constant Entity_Id := Entity (Clause);
7896 Cur_Use : constant Node_Id := Current_Use_Clause (Pack_Name);
7897 Decl : constant Node_Id := Parent (Clause);
7899 Prev_Use : Node_Id := Empty;
7900 Redundant : Node_Id := Empty;
7901 -- The Use_Clause which is actually redundant. In the simplest case it
7902 -- is Pack itself, but when we compile a body we install its context
7903 -- before that of its spec, in which case it is the use_clause in the
7904 -- spec that will appear to be redundant, and we want the warning to be
7905 -- placed on the body. Similar complications appear when the redundancy
7906 -- is between a child unit and one of its ancestors.
7908 begin
7909 Set_Redundant_Use (Clause, True);
7911 if not Comes_From_Source (Clause)
7912 or else In_Instance
7913 or else not Warn_On_Redundant_Constructs
7914 then
7915 return;
7916 end if;
7918 if not Is_Compilation_Unit (Current_Scope) then
7920 -- If the use_clause is in an inner scope, it is made redundant by
7921 -- some clause in the current context, with one exception: If we're
7922 -- compiling a nested package body, and the use_clause comes from the
7923 -- corresponding spec, the clause is not necessarily fully redundant,
7924 -- so we should not warn. If a warning was warranted, it would have
7925 -- been given when the spec was processed.
7927 if Nkind (Parent (Decl)) = N_Package_Specification then
7928 declare
7929 Package_Spec_Entity : constant Entity_Id :=
7930 Defining_Unit_Name (Parent (Decl));
7931 begin
7932 if In_Package_Body (Package_Spec_Entity) then
7933 return;
7934 end if;
7935 end;
7936 end if;
7938 Redundant := Clause;
7939 Prev_Use := Cur_Use;
7941 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
7942 declare
7943 Cur_Unit : constant Unit_Number_Type := Get_Source_Unit (Cur_Use);
7944 New_Unit : constant Unit_Number_Type := Get_Source_Unit (Clause);
7945 Scop : Entity_Id;
7947 begin
7948 if Cur_Unit = New_Unit then
7950 -- Redundant clause in same body
7952 Redundant := Clause;
7953 Prev_Use := Cur_Use;
7955 elsif Cur_Unit = Current_Sem_Unit then
7957 -- If the new clause is not in the current unit it has been
7958 -- analyzed first, and it makes the other one redundant.
7959 -- However, if the new clause appears in a subunit, Cur_Unit
7960 -- is still the parent, and in that case the redundant one
7961 -- is the one appearing in the subunit.
7963 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
7964 Redundant := Clause;
7965 Prev_Use := Cur_Use;
7967 -- Most common case: redundant clause in body,
7968 -- original clause in spec. Current scope is spec entity.
7970 elsif
7971 Current_Scope =
7972 Defining_Entity (
7973 Unit (Library_Unit (Cunit (Current_Sem_Unit))))
7974 then
7975 Redundant := Cur_Use;
7976 Prev_Use := Clause;
7978 else
7979 -- The new clause may appear in an unrelated unit, when
7980 -- the parents of a generic are being installed prior to
7981 -- instantiation. In this case there must be no warning.
7982 -- We detect this case by checking whether the current top
7983 -- of the stack is related to the current compilation.
7985 Scop := Current_Scope;
7986 while Present (Scop) and then Scop /= Standard_Standard loop
7987 if Is_Compilation_Unit (Scop)
7988 and then not Is_Child_Unit (Scop)
7989 then
7990 return;
7992 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
7993 exit;
7994 end if;
7996 Scop := Scope (Scop);
7997 end loop;
7999 Redundant := Cur_Use;
8000 Prev_Use := Clause;
8001 end if;
8003 elsif New_Unit = Current_Sem_Unit then
8004 Redundant := Clause;
8005 Prev_Use := Cur_Use;
8007 else
8008 -- Neither is the current unit, so they appear in parent or
8009 -- sibling units. Warning will be emitted elsewhere.
8011 return;
8012 end if;
8013 end;
8015 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
8016 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
8017 then
8018 -- Use_clause is in child unit of current unit, and the child unit
8019 -- appears in the context of the body of the parent, so it has been
8020 -- installed first, even though it is the redundant one. Depending on
8021 -- their placement in the context, the visible or the private parts
8022 -- of the two units, either might appear as redundant, but the
8023 -- message has to be on the current unit.
8025 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
8026 Redundant := Cur_Use;
8027 Prev_Use := Clause;
8028 else
8029 Redundant := Clause;
8030 Prev_Use := Cur_Use;
8031 end if;
8033 -- If the new use clause appears in the private part of a parent unit
8034 -- it may appear to be redundant w.r.t. a use clause in a child unit,
8035 -- but the previous use clause was needed in the visible part of the
8036 -- child, and no warning should be emitted.
8038 if Nkind (Parent (Decl)) = N_Package_Specification
8039 and then
8040 List_Containing (Decl) = Private_Declarations (Parent (Decl))
8041 then
8042 declare
8043 Par : constant Entity_Id := Defining_Entity (Parent (Decl));
8044 Spec : constant Node_Id :=
8045 Specification (Unit (Cunit (Current_Sem_Unit)));
8047 begin
8048 if Is_Compilation_Unit (Par)
8049 and then Par /= Cunit_Entity (Current_Sem_Unit)
8050 and then Parent (Cur_Use) = Spec
8051 and then
8052 List_Containing (Cur_Use) = Visible_Declarations (Spec)
8053 then
8054 return;
8055 end if;
8056 end;
8057 end if;
8059 -- Finally, if the current use clause is in the context then
8060 -- the clause is redundant when it is nested within the unit.
8062 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
8063 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
8064 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
8065 then
8066 Redundant := Clause;
8067 Prev_Use := Cur_Use;
8069 else
8070 null;
8071 end if;
8073 if Present (Redundant) then
8074 Error_Msg_Sloc := Sloc (Prev_Use);
8075 Error_Msg_NE -- CODEFIX
8076 ("& is already use-visible through previous use clause #??",
8077 Redundant, Pack_Name);
8078 end if;
8079 end Note_Redundant_Use;
8081 ---------------
8082 -- Pop_Scope --
8083 ---------------
8085 procedure Pop_Scope is
8086 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8087 S : constant Entity_Id := SST.Entity;
8089 begin
8090 if Debug_Flag_E then
8091 Write_Info;
8092 end if;
8094 -- Set Default_Storage_Pool field of the library unit if necessary
8096 if Ekind_In (S, E_Package, E_Generic_Package)
8097 and then
8098 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
8099 then
8100 declare
8101 Aux : constant Node_Id :=
8102 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
8103 begin
8104 if No (Default_Storage_Pool (Aux)) then
8105 Set_Default_Storage_Pool (Aux, Default_Pool);
8106 end if;
8107 end;
8108 end if;
8110 Scope_Suppress := SST.Save_Scope_Suppress;
8111 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
8112 Check_Policy_List := SST.Save_Check_Policy_List;
8113 Default_Pool := SST.Save_Default_Storage_Pool;
8114 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
8115 SPARK_Mode := SST.Save_SPARK_Mode;
8116 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
8117 Default_SSO := SST.Save_Default_SSO;
8118 Uneval_Old := SST.Save_Uneval_Old;
8120 if Debug_Flag_W then
8121 Write_Str ("<-- exiting scope: ");
8122 Write_Name (Chars (Current_Scope));
8123 Write_Str (", Depth=");
8124 Write_Int (Int (Scope_Stack.Last));
8125 Write_Eol;
8126 end if;
8128 End_Use_Clauses (SST.First_Use_Clause);
8130 -- If the actions to be wrapped are still there they will get lost
8131 -- causing incomplete code to be generated. It is better to abort in
8132 -- this case (and we do the abort even with assertions off since the
8133 -- penalty is incorrect code generation).
8135 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
8136 raise Program_Error;
8137 end if;
8139 -- Free last subprogram name if allocated, and pop scope
8141 Free (SST.Last_Subprogram_Name);
8142 Scope_Stack.Decrement_Last;
8143 end Pop_Scope;
8145 ---------------
8146 -- Push_Scope --
8147 ---------------
8149 procedure Push_Scope (S : Entity_Id) is
8150 E : constant Entity_Id := Scope (S);
8152 begin
8153 if Ekind (S) = E_Void then
8154 null;
8156 -- Set scope depth if not a non-concurrent type, and we have not yet set
8157 -- the scope depth. This means that we have the first occurrence of the
8158 -- scope, and this is where the depth is set.
8160 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
8161 and then not Scope_Depth_Set (S)
8162 then
8163 if S = Standard_Standard then
8164 Set_Scope_Depth_Value (S, Uint_0);
8166 elsif Is_Child_Unit (S) then
8167 Set_Scope_Depth_Value (S, Uint_1);
8169 elsif not Is_Record_Type (Current_Scope) then
8170 if Ekind (S) = E_Loop then
8171 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
8172 else
8173 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
8174 end if;
8175 end if;
8176 end if;
8178 Scope_Stack.Increment_Last;
8180 declare
8181 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8183 begin
8184 SST.Entity := S;
8185 SST.Save_Scope_Suppress := Scope_Suppress;
8186 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
8187 SST.Save_Check_Policy_List := Check_Policy_List;
8188 SST.Save_Default_Storage_Pool := Default_Pool;
8189 SST.Save_No_Tagged_Streams := No_Tagged_Streams;
8190 SST.Save_SPARK_Mode := SPARK_Mode;
8191 SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma;
8192 SST.Save_Default_SSO := Default_SSO;
8193 SST.Save_Uneval_Old := Uneval_Old;
8195 if Scope_Stack.Last > Scope_Stack.First then
8196 SST.Component_Alignment_Default := Scope_Stack.Table
8197 (Scope_Stack.Last - 1).
8198 Component_Alignment_Default;
8199 end if;
8201 SST.Last_Subprogram_Name := null;
8202 SST.Is_Transient := False;
8203 SST.Node_To_Be_Wrapped := Empty;
8204 SST.Pending_Freeze_Actions := No_List;
8205 SST.Actions_To_Be_Wrapped := (others => No_List);
8206 SST.First_Use_Clause := Empty;
8207 SST.Is_Active_Stack_Base := False;
8208 SST.Previous_Visibility := False;
8209 SST.Locked_Shared_Objects := No_Elist;
8210 end;
8212 if Debug_Flag_W then
8213 Write_Str ("--> new scope: ");
8214 Write_Name (Chars (Current_Scope));
8215 Write_Str (", Id=");
8216 Write_Int (Int (Current_Scope));
8217 Write_Str (", Depth=");
8218 Write_Int (Int (Scope_Stack.Last));
8219 Write_Eol;
8220 end if;
8222 -- Deal with copying flags from the previous scope to this one. This is
8223 -- not necessary if either scope is standard, or if the new scope is a
8224 -- child unit.
8226 if S /= Standard_Standard
8227 and then Scope (S) /= Standard_Standard
8228 and then not Is_Child_Unit (S)
8229 then
8230 if Nkind (E) not in N_Entity then
8231 return;
8232 end if;
8234 -- Copy categorization flags from Scope (S) to S, this is not done
8235 -- when Scope (S) is Standard_Standard since propagation is from
8236 -- library unit entity inwards. Copy other relevant attributes as
8237 -- well (Discard_Names in particular).
8239 -- We only propagate inwards for library level entities,
8240 -- inner level subprograms do not inherit the categorization.
8242 if Is_Library_Level_Entity (S) then
8243 Set_Is_Preelaborated (S, Is_Preelaborated (E));
8244 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
8245 Set_Discard_Names (S, Discard_Names (E));
8246 Set_Suppress_Value_Tracking_On_Call
8247 (S, Suppress_Value_Tracking_On_Call (E));
8248 Set_Categorization_From_Scope (E => S, Scop => E);
8249 end if;
8250 end if;
8252 if Is_Child_Unit (S)
8253 and then Present (E)
8254 and then Ekind_In (E, E_Package, E_Generic_Package)
8255 and then
8256 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
8257 then
8258 declare
8259 Aux : constant Node_Id :=
8260 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
8261 begin
8262 if Present (Default_Storage_Pool (Aux)) then
8263 Default_Pool := Default_Storage_Pool (Aux);
8264 end if;
8265 end;
8266 end if;
8267 end Push_Scope;
8269 ---------------------
8270 -- Premature_Usage --
8271 ---------------------
8273 procedure Premature_Usage (N : Node_Id) is
8274 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
8275 E : Entity_Id := Entity (N);
8277 begin
8278 -- Within an instance, the analysis of the actual for a formal object
8279 -- does not see the name of the object itself. This is significant only
8280 -- if the object is an aggregate, where its analysis does not do any
8281 -- name resolution on component associations. (see 4717-008). In such a
8282 -- case, look for the visible homonym on the chain.
8284 if In_Instance and then Present (Homonym (E)) then
8285 E := Homonym (E);
8286 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
8287 E := Homonym (E);
8288 end loop;
8290 if Present (E) then
8291 Set_Entity (N, E);
8292 Set_Etype (N, Etype (E));
8293 return;
8294 end if;
8295 end if;
8297 if Kind = N_Component_Declaration then
8298 Error_Msg_N
8299 ("component&! cannot be used before end of record declaration", N);
8301 elsif Kind = N_Parameter_Specification then
8302 Error_Msg_N
8303 ("formal parameter&! cannot be used before end of specification",
8306 elsif Kind = N_Discriminant_Specification then
8307 Error_Msg_N
8308 ("discriminant&! cannot be used before end of discriminant part",
8311 elsif Kind = N_Procedure_Specification
8312 or else Kind = N_Function_Specification
8313 then
8314 Error_Msg_N
8315 ("subprogram&! cannot be used before end of its declaration",
8318 elsif Kind = N_Full_Type_Declaration then
8319 Error_Msg_N
8320 ("type& cannot be used before end of its declaration!", N);
8322 else
8323 Error_Msg_N
8324 ("object& cannot be used before end of its declaration!", N);
8325 end if;
8326 end Premature_Usage;
8328 ------------------------
8329 -- Present_System_Aux --
8330 ------------------------
8332 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
8333 Loc : Source_Ptr;
8334 Aux_Name : Unit_Name_Type;
8335 Unum : Unit_Number_Type;
8336 Withn : Node_Id;
8337 With_Sys : Node_Id;
8338 The_Unit : Node_Id;
8340 function Find_System (C_Unit : Node_Id) return Entity_Id;
8341 -- Scan context clause of compilation unit to find with_clause
8342 -- for System.
8344 -----------------
8345 -- Find_System --
8346 -----------------
8348 function Find_System (C_Unit : Node_Id) return Entity_Id is
8349 With_Clause : Node_Id;
8351 begin
8352 With_Clause := First (Context_Items (C_Unit));
8353 while Present (With_Clause) loop
8354 if (Nkind (With_Clause) = N_With_Clause
8355 and then Chars (Name (With_Clause)) = Name_System)
8356 and then Comes_From_Source (With_Clause)
8357 then
8358 return With_Clause;
8359 end if;
8361 Next (With_Clause);
8362 end loop;
8364 return Empty;
8365 end Find_System;
8367 -- Start of processing for Present_System_Aux
8369 begin
8370 -- The child unit may have been loaded and analyzed already
8372 if Present (System_Aux_Id) then
8373 return True;
8375 -- If no previous pragma for System.Aux, nothing to load
8377 elsif No (System_Extend_Unit) then
8378 return False;
8380 -- Use the unit name given in the pragma to retrieve the unit.
8381 -- Verify that System itself appears in the context clause of the
8382 -- current compilation. If System is not present, an error will
8383 -- have been reported already.
8385 else
8386 With_Sys := Find_System (Cunit (Current_Sem_Unit));
8388 The_Unit := Unit (Cunit (Current_Sem_Unit));
8390 if No (With_Sys)
8391 and then
8392 (Nkind (The_Unit) = N_Package_Body
8393 or else (Nkind (The_Unit) = N_Subprogram_Body
8394 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
8395 then
8396 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
8397 end if;
8399 if No (With_Sys) and then Present (N) then
8401 -- If we are compiling a subunit, we need to examine its
8402 -- context as well (Current_Sem_Unit is the parent unit);
8404 The_Unit := Parent (N);
8405 while Nkind (The_Unit) /= N_Compilation_Unit loop
8406 The_Unit := Parent (The_Unit);
8407 end loop;
8409 if Nkind (Unit (The_Unit)) = N_Subunit then
8410 With_Sys := Find_System (The_Unit);
8411 end if;
8412 end if;
8414 if No (With_Sys) then
8415 return False;
8416 end if;
8418 Loc := Sloc (With_Sys);
8419 Get_Name_String (Chars (Expression (System_Extend_Unit)));
8420 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
8421 Name_Buffer (1 .. 7) := "system.";
8422 Name_Buffer (Name_Len + 8) := '%';
8423 Name_Buffer (Name_Len + 9) := 's';
8424 Name_Len := Name_Len + 9;
8425 Aux_Name := Name_Find;
8427 Unum :=
8428 Load_Unit
8429 (Load_Name => Aux_Name,
8430 Required => False,
8431 Subunit => False,
8432 Error_Node => With_Sys);
8434 if Unum /= No_Unit then
8435 Semantics (Cunit (Unum));
8436 System_Aux_Id :=
8437 Defining_Entity (Specification (Unit (Cunit (Unum))));
8439 Withn :=
8440 Make_With_Clause (Loc,
8441 Name =>
8442 Make_Expanded_Name (Loc,
8443 Chars => Chars (System_Aux_Id),
8444 Prefix => New_Occurrence_Of (Scope (System_Aux_Id), Loc),
8445 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
8447 Set_Entity (Name (Withn), System_Aux_Id);
8449 Set_Library_Unit (Withn, Cunit (Unum));
8450 Set_Corresponding_Spec (Withn, System_Aux_Id);
8451 Set_First_Name (Withn, True);
8452 Set_Implicit_With (Withn, True);
8454 Insert_After (With_Sys, Withn);
8455 Mark_Rewrite_Insertion (Withn);
8456 Set_Context_Installed (Withn);
8458 return True;
8460 -- Here if unit load failed
8462 else
8463 Error_Msg_Name_1 := Name_System;
8464 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
8465 Error_Msg_N
8466 ("extension package `%.%` does not exist",
8467 Opt.System_Extend_Unit);
8468 return False;
8469 end if;
8470 end if;
8471 end Present_System_Aux;
8473 -------------------------
8474 -- Restore_Scope_Stack --
8475 -------------------------
8477 procedure Restore_Scope_Stack
8478 (List : Elist_Id;
8479 Handle_Use : Boolean := True)
8481 SS_Last : constant Int := Scope_Stack.Last;
8482 Elmt : Elmt_Id;
8484 begin
8485 -- Restore visibility of previous scope stack, if any, using the list
8486 -- we saved (we use Remove, since this list will not be used again).
8488 loop
8489 Elmt := Last_Elmt (List);
8490 exit when Elmt = No_Elmt;
8491 Set_Is_Immediately_Visible (Node (Elmt));
8492 Remove_Last_Elmt (List);
8493 end loop;
8495 -- Restore use clauses
8497 if SS_Last >= Scope_Stack.First
8498 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
8499 and then Handle_Use
8500 then
8501 Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
8502 end if;
8503 end Restore_Scope_Stack;
8505 ----------------------
8506 -- Save_Scope_Stack --
8507 ----------------------
8509 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
8510 -- consuming any memory. That is, Save_Scope_Stack took care of removing
8511 -- from immediate visibility entities and Restore_Scope_Stack took care
8512 -- of restoring their visibility analyzing the context of each entity. The
8513 -- problem of such approach is that it was fragile and caused unexpected
8514 -- visibility problems, and indeed one test was found where there was a
8515 -- real problem.
8517 -- Furthermore, the following experiment was carried out:
8519 -- - Save_Scope_Stack was modified to store in an Elist1 all those
8520 -- entities whose attribute Is_Immediately_Visible is modified
8521 -- from True to False.
8523 -- - Restore_Scope_Stack was modified to store in another Elist2
8524 -- all the entities whose attribute Is_Immediately_Visible is
8525 -- modified from False to True.
8527 -- - Extra code was added to verify that all the elements of Elist1
8528 -- are found in Elist2
8530 -- This test shows that there may be more occurrences of this problem which
8531 -- have not yet been detected. As a result, we replaced that approach by
8532 -- the current one in which Save_Scope_Stack returns the list of entities
8533 -- whose visibility is changed, and that list is passed to Restore_Scope_
8534 -- Stack to undo that change. This approach is simpler and safer, although
8535 -- it consumes more memory.
8537 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
8538 Result : constant Elist_Id := New_Elmt_List;
8539 E : Entity_Id;
8540 S : Entity_Id;
8541 SS_Last : constant Int := Scope_Stack.Last;
8543 procedure Remove_From_Visibility (E : Entity_Id);
8544 -- If E is immediately visible then append it to the result and remove
8545 -- it temporarily from visibility.
8547 ----------------------------
8548 -- Remove_From_Visibility --
8549 ----------------------------
8551 procedure Remove_From_Visibility (E : Entity_Id) is
8552 begin
8553 if Is_Immediately_Visible (E) then
8554 Append_Elmt (E, Result);
8555 Set_Is_Immediately_Visible (E, False);
8556 end if;
8557 end Remove_From_Visibility;
8559 -- Start of processing for Save_Scope_Stack
8561 begin
8562 if SS_Last >= Scope_Stack.First
8563 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
8564 then
8565 if Handle_Use then
8566 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
8567 end if;
8569 -- If the call is from within a compilation unit, as when called from
8570 -- Rtsfind, make current entries in scope stack invisible while we
8571 -- analyze the new unit.
8573 for J in reverse 0 .. SS_Last loop
8574 exit when Scope_Stack.Table (J).Entity = Standard_Standard
8575 or else No (Scope_Stack.Table (J).Entity);
8577 S := Scope_Stack.Table (J).Entity;
8579 Remove_From_Visibility (S);
8581 E := First_Entity (S);
8582 while Present (E) loop
8583 Remove_From_Visibility (E);
8584 Next_Entity (E);
8585 end loop;
8586 end loop;
8588 end if;
8590 return Result;
8591 end Save_Scope_Stack;
8593 -------------
8594 -- Set_Use --
8595 -------------
8597 procedure Set_Use (L : List_Id) is
8598 Decl : Node_Id;
8599 Pack_Name : Node_Id;
8600 Pack : Entity_Id;
8601 Id : Entity_Id;
8603 begin
8604 if Present (L) then
8605 Decl := First (L);
8606 while Present (Decl) loop
8607 if Nkind (Decl) = N_Use_Package_Clause then
8608 Chain_Use_Clause (Decl);
8610 Pack_Name := First (Names (Decl));
8611 while Present (Pack_Name) loop
8612 Pack := Entity (Pack_Name);
8614 if Ekind (Pack) = E_Package
8615 and then Applicable_Use (Pack_Name)
8616 then
8617 Use_One_Package (Pack, Decl);
8618 end if;
8620 Next (Pack_Name);
8621 end loop;
8623 elsif Nkind (Decl) = N_Use_Type_Clause then
8624 Chain_Use_Clause (Decl);
8626 Id := First (Subtype_Marks (Decl));
8627 while Present (Id) loop
8628 if Entity (Id) /= Any_Type then
8629 Use_One_Type (Id);
8630 end if;
8632 Next (Id);
8633 end loop;
8634 end if;
8636 Next (Decl);
8637 end loop;
8638 end if;
8639 end Set_Use;
8641 ---------------------
8642 -- Use_One_Package --
8643 ---------------------
8645 procedure Use_One_Package (P : Entity_Id; N : Node_Id) is
8646 Id : Entity_Id;
8647 Prev : Entity_Id;
8648 Current_Instance : Entity_Id := Empty;
8649 Real_P : Entity_Id;
8650 Private_With_OK : Boolean := False;
8652 begin
8653 if Ekind (P) /= E_Package then
8654 return;
8655 end if;
8657 Set_In_Use (P);
8658 Set_Current_Use_Clause (P, N);
8660 -- Ada 2005 (AI-50217): Check restriction
8662 if From_Limited_With (P) then
8663 Error_Msg_N ("limited withed package cannot appear in use clause", N);
8664 end if;
8666 -- Find enclosing instance, if any
8668 if In_Instance then
8669 Current_Instance := Current_Scope;
8670 while not Is_Generic_Instance (Current_Instance) loop
8671 Current_Instance := Scope (Current_Instance);
8672 end loop;
8674 if No (Hidden_By_Use_Clause (N)) then
8675 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
8676 end if;
8677 end if;
8679 -- If unit is a package renaming, indicate that the renamed
8680 -- package is also in use (the flags on both entities must
8681 -- remain consistent, and a subsequent use of either of them
8682 -- should be recognized as redundant).
8684 if Present (Renamed_Object (P)) then
8685 Set_In_Use (Renamed_Object (P));
8686 Set_Current_Use_Clause (Renamed_Object (P), N);
8687 Real_P := Renamed_Object (P);
8688 else
8689 Real_P := P;
8690 end if;
8692 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
8693 -- found in the private part of a package specification
8695 if In_Private_Part (Current_Scope)
8696 and then Has_Private_With (P)
8697 and then Is_Child_Unit (Current_Scope)
8698 and then Is_Child_Unit (P)
8699 and then Is_Ancestor_Package (Scope (Current_Scope), P)
8700 then
8701 Private_With_OK := True;
8702 end if;
8704 -- Loop through entities in one package making them potentially
8705 -- use-visible.
8707 Id := First_Entity (P);
8708 while Present (Id)
8709 and then (Id /= First_Private_Entity (P)
8710 or else Private_With_OK) -- Ada 2005 (AI-262)
8711 loop
8712 Prev := Current_Entity (Id);
8713 while Present (Prev) loop
8714 if Is_Immediately_Visible (Prev)
8715 and then (not Is_Overloadable (Prev)
8716 or else not Is_Overloadable (Id)
8717 or else (Type_Conformant (Id, Prev)))
8718 then
8719 if No (Current_Instance) then
8721 -- Potentially use-visible entity remains hidden
8723 goto Next_Usable_Entity;
8725 -- A use clause within an instance hides outer global entities,
8726 -- which are not used to resolve local entities in the
8727 -- instance. Note that the predefined entities in Standard
8728 -- could not have been hidden in the generic by a use clause,
8729 -- and therefore remain visible. Other compilation units whose
8730 -- entities appear in Standard must be hidden in an instance.
8732 -- To determine whether an entity is external to the instance
8733 -- we compare the scope depth of its scope with that of the
8734 -- current instance. However, a generic actual of a subprogram
8735 -- instance is declared in the wrapper package but will not be
8736 -- hidden by a use-visible entity. similarly, an entity that is
8737 -- declared in an enclosing instance will not be hidden by an
8738 -- an entity declared in a generic actual, which can only have
8739 -- been use-visible in the generic and will not have hidden the
8740 -- entity in the generic parent.
8742 -- If Id is called Standard, the predefined package with the
8743 -- same name is in the homonym chain. It has to be ignored
8744 -- because it has no defined scope (being the only entity in
8745 -- the system with this mandated behavior).
8747 elsif not Is_Hidden (Id)
8748 and then Present (Scope (Prev))
8749 and then not Is_Wrapper_Package (Scope (Prev))
8750 and then Scope_Depth (Scope (Prev)) <
8751 Scope_Depth (Current_Instance)
8752 and then (Scope (Prev) /= Standard_Standard
8753 or else Sloc (Prev) > Standard_Location)
8754 then
8755 if In_Open_Scopes (Scope (Prev))
8756 and then Is_Generic_Instance (Scope (Prev))
8757 and then Present (Associated_Formal_Package (P))
8758 then
8759 null;
8761 else
8762 Set_Is_Potentially_Use_Visible (Id);
8763 Set_Is_Immediately_Visible (Prev, False);
8764 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
8765 end if;
8766 end if;
8768 -- A user-defined operator is not use-visible if the predefined
8769 -- operator for the type is immediately visible, which is the case
8770 -- if the type of the operand is in an open scope. This does not
8771 -- apply to user-defined operators that have operands of different
8772 -- types, because the predefined mixed mode operations (multiply
8773 -- and divide) apply to universal types and do not hide anything.
8775 elsif Ekind (Prev) = E_Operator
8776 and then Operator_Matches_Spec (Prev, Id)
8777 and then In_Open_Scopes
8778 (Scope (Base_Type (Etype (First_Formal (Id)))))
8779 and then (No (Next_Formal (First_Formal (Id)))
8780 or else Etype (First_Formal (Id)) =
8781 Etype (Next_Formal (First_Formal (Id)))
8782 or else Chars (Prev) = Name_Op_Expon)
8783 then
8784 goto Next_Usable_Entity;
8786 -- In an instance, two homonyms may become use_visible through the
8787 -- actuals of distinct formal packages. In the generic, only the
8788 -- current one would have been visible, so make the other one
8789 -- not use_visible.
8791 elsif Present (Current_Instance)
8792 and then Is_Potentially_Use_Visible (Prev)
8793 and then not Is_Overloadable (Prev)
8794 and then Scope (Id) /= Scope (Prev)
8795 and then Used_As_Generic_Actual (Scope (Prev))
8796 and then Used_As_Generic_Actual (Scope (Id))
8797 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
8798 Current_Use_Clause (Scope (Id)))
8799 then
8800 Set_Is_Potentially_Use_Visible (Prev, False);
8801 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
8802 end if;
8804 Prev := Homonym (Prev);
8805 end loop;
8807 -- On exit, we know entity is not hidden, unless it is private
8809 if not Is_Hidden (Id)
8810 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
8811 then
8812 Set_Is_Potentially_Use_Visible (Id);
8814 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
8815 Set_Is_Potentially_Use_Visible (Full_View (Id));
8816 end if;
8817 end if;
8819 <<Next_Usable_Entity>>
8820 Next_Entity (Id);
8821 end loop;
8823 -- Child units are also made use-visible by a use clause, but they may
8824 -- appear after all visible declarations in the parent entity list.
8826 while Present (Id) loop
8827 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
8828 Set_Is_Potentially_Use_Visible (Id);
8829 end if;
8831 Next_Entity (Id);
8832 end loop;
8834 if Chars (Real_P) = Name_System
8835 and then Scope (Real_P) = Standard_Standard
8836 and then Present_System_Aux (N)
8837 then
8838 Use_One_Package (System_Aux_Id, N);
8839 end if;
8841 end Use_One_Package;
8843 ------------------
8844 -- Use_One_Type --
8845 ------------------
8847 procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False) is
8848 Elmt : Elmt_Id;
8849 Is_Known_Used : Boolean;
8850 Op_List : Elist_Id;
8851 T : Entity_Id;
8853 function Spec_Reloaded_For_Body return Boolean;
8854 -- Determine whether the compilation unit is a package body and the use
8855 -- type clause is in the spec of the same package. Even though the spec
8856 -- was analyzed first, its context is reloaded when analysing the body.
8858 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
8859 -- AI05-150: if the use_type_clause carries the "all" qualifier,
8860 -- class-wide operations of ancestor types are use-visible if the
8861 -- ancestor type is visible.
8863 ----------------------------
8864 -- Spec_Reloaded_For_Body --
8865 ----------------------------
8867 function Spec_Reloaded_For_Body return Boolean is
8868 begin
8869 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
8870 declare
8871 Spec : constant Node_Id :=
8872 Parent (List_Containing (Parent (Id)));
8874 begin
8875 -- Check whether type is declared in a package specification,
8876 -- and current unit is the corresponding package body. The
8877 -- use clauses themselves may be within a nested package.
8879 return
8880 Nkind (Spec) = N_Package_Specification
8881 and then
8882 In_Same_Source_Unit (Corresponding_Body (Parent (Spec)),
8883 Cunit_Entity (Current_Sem_Unit));
8884 end;
8885 end if;
8887 return False;
8888 end Spec_Reloaded_For_Body;
8890 -------------------------------
8891 -- Use_Class_Wide_Operations --
8892 -------------------------------
8894 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
8895 Scop : Entity_Id;
8896 Ent : Entity_Id;
8898 function Is_Class_Wide_Operation_Of
8899 (Op : Entity_Id;
8900 T : Entity_Id) return Boolean;
8901 -- Determine whether a subprogram has a class-wide parameter or
8902 -- result that is T'Class.
8904 ---------------------------------
8905 -- Is_Class_Wide_Operation_Of --
8906 ---------------------------------
8908 function Is_Class_Wide_Operation_Of
8909 (Op : Entity_Id;
8910 T : Entity_Id) return Boolean
8912 Formal : Entity_Id;
8914 begin
8915 Formal := First_Formal (Op);
8916 while Present (Formal) loop
8917 if Etype (Formal) = Class_Wide_Type (T) then
8918 return True;
8919 end if;
8920 Next_Formal (Formal);
8921 end loop;
8923 if Etype (Op) = Class_Wide_Type (T) then
8924 return True;
8925 end if;
8927 return False;
8928 end Is_Class_Wide_Operation_Of;
8930 -- Start of processing for Use_Class_Wide_Operations
8932 begin
8933 Scop := Scope (Typ);
8934 if not Is_Hidden (Scop) then
8935 Ent := First_Entity (Scop);
8936 while Present (Ent) loop
8937 if Is_Overloadable (Ent)
8938 and then Is_Class_Wide_Operation_Of (Ent, Typ)
8939 and then not Is_Potentially_Use_Visible (Ent)
8940 then
8941 Set_Is_Potentially_Use_Visible (Ent);
8942 Append_Elmt (Ent, Used_Operations (Parent (Id)));
8943 end if;
8945 Next_Entity (Ent);
8946 end loop;
8947 end if;
8949 if Is_Derived_Type (Typ) then
8950 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
8951 end if;
8952 end Use_Class_Wide_Operations;
8954 -- Start of processing for Use_One_Type
8956 begin
8957 -- It is the type determined by the subtype mark (8.4(8)) whose
8958 -- operations become potentially use-visible.
8960 T := Base_Type (Entity (Id));
8962 -- Either the type itself is used, the package where it is declared
8963 -- is in use or the entity is declared in the current package, thus
8964 -- use-visible.
8966 Is_Known_Used :=
8967 In_Use (T)
8968 or else In_Use (Scope (T))
8969 or else Scope (T) = Current_Scope;
8971 Set_Redundant_Use (Id,
8972 Is_Known_Used or else Is_Potentially_Use_Visible (T));
8974 if Ekind (T) = E_Incomplete_Type then
8975 Error_Msg_N ("premature usage of incomplete type", Id);
8977 elsif In_Open_Scopes (Scope (T)) then
8978 null;
8980 -- A limited view cannot appear in a use_type clause. However, an access
8981 -- type whose designated type is limited has the flag but is not itself
8982 -- a limited view unless we only have a limited view of its enclosing
8983 -- package.
8985 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
8986 Error_Msg_N
8987 ("incomplete type from limited view "
8988 & "cannot appear in use clause", Id);
8990 -- If the subtype mark designates a subtype in a different package,
8991 -- we have to check that the parent type is visible, otherwise the
8992 -- use type clause is a noop. Not clear how to do that???
8994 elsif not Redundant_Use (Id) then
8995 Set_In_Use (T);
8997 -- If T is tagged, primitive operators on class-wide operands
8998 -- are also available.
9000 if Is_Tagged_Type (T) then
9001 Set_In_Use (Class_Wide_Type (T));
9002 end if;
9004 Set_Current_Use_Clause (T, Parent (Id));
9006 -- Iterate over primitive operations of the type. If an operation is
9007 -- already use_visible, it is the result of a previous use_clause,
9008 -- and already appears on the corresponding entity chain. If the
9009 -- clause is being reinstalled, operations are already use-visible.
9011 if Installed then
9012 null;
9014 else
9015 Op_List := Collect_Primitive_Operations (T);
9016 Elmt := First_Elmt (Op_List);
9017 while Present (Elmt) loop
9018 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
9019 or else Chars (Node (Elmt)) in Any_Operator_Name)
9020 and then not Is_Hidden (Node (Elmt))
9021 and then not Is_Potentially_Use_Visible (Node (Elmt))
9022 then
9023 Set_Is_Potentially_Use_Visible (Node (Elmt));
9024 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
9026 elsif Ada_Version >= Ada_2012
9027 and then All_Present (Parent (Id))
9028 and then not Is_Hidden (Node (Elmt))
9029 and then not Is_Potentially_Use_Visible (Node (Elmt))
9030 then
9031 Set_Is_Potentially_Use_Visible (Node (Elmt));
9032 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
9033 end if;
9035 Next_Elmt (Elmt);
9036 end loop;
9037 end if;
9039 if Ada_Version >= Ada_2012
9040 and then All_Present (Parent (Id))
9041 and then Is_Tagged_Type (T)
9042 then
9043 Use_Class_Wide_Operations (T);
9044 end if;
9045 end if;
9047 -- If warning on redundant constructs, check for unnecessary WITH
9049 if Warn_On_Redundant_Constructs
9050 and then Is_Known_Used
9052 -- with P; with P; use P;
9053 -- package P is package X is package body X is
9054 -- type T ... use P.T;
9056 -- The compilation unit is the body of X. GNAT first compiles the
9057 -- spec of X, then proceeds to the body. At that point P is marked
9058 -- as use visible. The analysis then reinstalls the spec along with
9059 -- its context. The use clause P.T is now recognized as redundant,
9060 -- but in the wrong context. Do not emit a warning in such cases.
9061 -- Do not emit a warning either if we are in an instance, there is
9062 -- no redundancy between an outer use_clause and one that appears
9063 -- within the generic.
9065 and then not Spec_Reloaded_For_Body
9066 and then not In_Instance
9067 then
9068 -- The type already has a use clause
9070 if In_Use (T) then
9072 -- Case where we know the current use clause for the type
9074 if Present (Current_Use_Clause (T)) then
9075 Use_Clause_Known : declare
9076 Clause1 : constant Node_Id := Parent (Id);
9077 Clause2 : constant Node_Id := Current_Use_Clause (T);
9078 Ent1 : Entity_Id;
9079 Ent2 : Entity_Id;
9080 Err_No : Node_Id;
9081 Unit1 : Node_Id;
9082 Unit2 : Node_Id;
9084 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
9085 -- Return the appropriate entity for determining which unit
9086 -- has a deeper scope: the defining entity for U, unless U
9087 -- is a package instance, in which case we retrieve the
9088 -- entity of the instance spec.
9090 --------------------
9091 -- Entity_Of_Unit --
9092 --------------------
9094 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
9095 begin
9096 if Nkind (U) = N_Package_Instantiation
9097 and then Analyzed (U)
9098 then
9099 return Defining_Entity (Instance_Spec (U));
9100 else
9101 return Defining_Entity (U);
9102 end if;
9103 end Entity_Of_Unit;
9105 -- Start of processing for Use_Clause_Known
9107 begin
9108 -- If both current use type clause and the use type clause
9109 -- for the type are at the compilation unit level, one of
9110 -- the units must be an ancestor of the other, and the
9111 -- warning belongs on the descendant.
9113 if Nkind (Parent (Clause1)) = N_Compilation_Unit
9114 and then
9115 Nkind (Parent (Clause2)) = N_Compilation_Unit
9116 then
9117 -- If the unit is a subprogram body that acts as spec,
9118 -- the context clause is shared with the constructed
9119 -- subprogram spec. Clearly there is no redundancy.
9121 if Clause1 = Clause2 then
9122 return;
9123 end if;
9125 Unit1 := Unit (Parent (Clause1));
9126 Unit2 := Unit (Parent (Clause2));
9128 -- If both clauses are on same unit, or one is the body
9129 -- of the other, or one of them is in a subunit, report
9130 -- redundancy on the later one.
9132 if Unit1 = Unit2 then
9133 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
9134 Error_Msg_NE -- CODEFIX
9135 ("& is already use-visible through previous "
9136 & "use_type_clause #??", Clause1, T);
9137 return;
9139 elsif Nkind (Unit1) = N_Subunit then
9140 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
9141 Error_Msg_NE -- CODEFIX
9142 ("& is already use-visible through previous "
9143 & "use_type_clause #??", Clause1, T);
9144 return;
9146 elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body)
9147 and then Nkind (Unit1) /= Nkind (Unit2)
9148 and then Nkind (Unit1) /= N_Subunit
9149 then
9150 Error_Msg_Sloc := Sloc (Clause1);
9151 Error_Msg_NE -- CODEFIX
9152 ("& is already use-visible through previous "
9153 & "use_type_clause #??", Current_Use_Clause (T), T);
9154 return;
9155 end if;
9157 -- There is a redundant use type clause in a child unit.
9158 -- Determine which of the units is more deeply nested.
9159 -- If a unit is a package instance, retrieve the entity
9160 -- and its scope from the instance spec.
9162 Ent1 := Entity_Of_Unit (Unit1);
9163 Ent2 := Entity_Of_Unit (Unit2);
9165 if Scope (Ent2) = Standard_Standard then
9166 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
9167 Err_No := Clause1;
9169 elsif Scope (Ent1) = Standard_Standard then
9170 Error_Msg_Sloc := Sloc (Id);
9171 Err_No := Clause2;
9173 -- If both units are child units, we determine which one
9174 -- is the descendant by the scope distance to the
9175 -- ultimate parent unit.
9177 else
9178 declare
9179 S1, S2 : Entity_Id;
9181 begin
9182 S1 := Scope (Ent1);
9183 S2 := Scope (Ent2);
9184 while Present (S1)
9185 and then Present (S2)
9186 and then S1 /= Standard_Standard
9187 and then S2 /= Standard_Standard
9188 loop
9189 S1 := Scope (S1);
9190 S2 := Scope (S2);
9191 end loop;
9193 if S1 = Standard_Standard then
9194 Error_Msg_Sloc := Sloc (Id);
9195 Err_No := Clause2;
9196 else
9197 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
9198 Err_No := Clause1;
9199 end if;
9200 end;
9201 end if;
9203 Error_Msg_NE -- CODEFIX
9204 ("& is already use-visible through previous "
9205 & "use_type_clause #??", Err_No, Id);
9207 -- Case where current use type clause and the use type
9208 -- clause for the type are not both at the compilation unit
9209 -- level. In this case we don't have location information.
9211 else
9212 Error_Msg_NE -- CODEFIX
9213 ("& is already use-visible through previous "
9214 & "use type clause??", Id, T);
9215 end if;
9216 end Use_Clause_Known;
9218 -- Here if Current_Use_Clause is not set for T, another case
9219 -- where we do not have the location information available.
9221 else
9222 Error_Msg_NE -- CODEFIX
9223 ("& is already use-visible through previous "
9224 & "use type clause??", Id, T);
9225 end if;
9227 -- The package where T is declared is already used
9229 elsif In_Use (Scope (T)) then
9230 Error_Msg_Sloc := Sloc (Current_Use_Clause (Scope (T)));
9231 Error_Msg_NE -- CODEFIX
9232 ("& is already use-visible through package use clause #??",
9233 Id, T);
9235 -- The current scope is the package where T is declared
9237 else
9238 Error_Msg_Node_2 := Scope (T);
9239 Error_Msg_NE -- CODEFIX
9240 ("& is already use-visible inside package &??", Id, T);
9241 end if;
9242 end if;
9243 end Use_One_Type;
9245 ----------------
9246 -- Write_Info --
9247 ----------------
9249 procedure Write_Info is
9250 Id : Entity_Id := First_Entity (Current_Scope);
9252 begin
9253 -- No point in dumping standard entities
9255 if Current_Scope = Standard_Standard then
9256 return;
9257 end if;
9259 Write_Str ("========================================================");
9260 Write_Eol;
9261 Write_Str (" Defined Entities in ");
9262 Write_Name (Chars (Current_Scope));
9263 Write_Eol;
9264 Write_Str ("========================================================");
9265 Write_Eol;
9267 if No (Id) then
9268 Write_Str ("-- none --");
9269 Write_Eol;
9271 else
9272 while Present (Id) loop
9273 Write_Entity_Info (Id, " ");
9274 Next_Entity (Id);
9275 end loop;
9276 end if;
9278 if Scope (Current_Scope) = Standard_Standard then
9280 -- Print information on the current unit itself
9282 Write_Entity_Info (Current_Scope, " ");
9283 end if;
9285 Write_Eol;
9286 end Write_Info;
9288 --------
9289 -- ws --
9290 --------
9292 procedure ws is
9293 S : Entity_Id;
9294 begin
9295 for J in reverse 1 .. Scope_Stack.Last loop
9296 S := Scope_Stack.Table (J).Entity;
9297 Write_Int (Int (S));
9298 Write_Str (" === ");
9299 Write_Name (Chars (S));
9300 Write_Eol;
9301 end loop;
9302 end ws;
9304 --------
9305 -- we --
9306 --------
9308 procedure we (S : Entity_Id) is
9309 E : Entity_Id;
9310 begin
9311 E := First_Entity (S);
9312 while Present (E) loop
9313 Write_Int (Int (E));
9314 Write_Str (" === ");
9315 Write_Name (Chars (E));
9316 Write_Eol;
9317 Next_Entity (E);
9318 end loop;
9319 end we;
9320 end Sem_Ch8;