Rebase.
[official-gcc.git] / gcc / ada / sem_ch8.adb
blob655f38bf6f4002004f70afdfcd0622499c06e5f5
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-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Tss; use Exp_Tss;
32 with Exp_Util; use Exp_Util;
33 with Fname; use Fname;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Namet.Sp; use Namet.Sp;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch4; use Sem_Ch4;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch12; use Sem_Ch12;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sem_Type; use Sem_Type;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinfo.CN; use Sinfo.CN;
66 with Snames; use Snames;
67 with Style; use Style;
68 with Table;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
73 package body Sem_Ch8 is
75 ------------------------------------
76 -- Visibility and Name Resolution --
77 ------------------------------------
79 -- This package handles name resolution and the collection of possible
80 -- interpretations for overloaded names, prior to overload resolution.
82 -- Name resolution is the process that establishes a mapping between source
83 -- identifiers and the entities they denote at each point in the program.
84 -- Each entity is represented by a defining occurrence. Each identifier
85 -- that denotes an entity points to the corresponding defining occurrence.
86 -- This is the entity of the applied occurrence. Each occurrence holds
87 -- an index into the names table, where source identifiers are stored.
89 -- Each entry in the names table for an identifier or designator uses the
90 -- Info pointer to hold a link to the currently visible entity that has
91 -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
92 -- in package Sem_Util). The visibility is initialized at the beginning of
93 -- semantic processing to make entities in package Standard immediately
94 -- visible. The visibility table is used in a more subtle way when
95 -- compiling subunits (see below).
97 -- Entities that have the same name (i.e. homonyms) are chained. In the
98 -- case of overloaded entities, this chain holds all the possible meanings
99 -- of a given identifier. The process of overload resolution uses type
100 -- information to select from this chain the unique meaning of a given
101 -- identifier.
103 -- Entities are also chained in their scope, through the Next_Entity link.
104 -- As a consequence, the name space is organized as a sparse matrix, where
105 -- each row corresponds to a scope, and each column to a source identifier.
106 -- Open scopes, that is to say scopes currently being compiled, have their
107 -- corresponding rows of entities in order, innermost scope first.
109 -- The scopes of packages that are mentioned in context clauses appear in
110 -- no particular order, interspersed among open scopes. This is because
111 -- in the course of analyzing the context of a compilation, a package
112 -- declaration is first an open scope, and subsequently an element of the
113 -- context. If subunits or child units are present, a parent unit may
114 -- appear under various guises at various times in the compilation.
116 -- When the compilation of the innermost scope is complete, the entities
117 -- defined therein are no longer visible. If the scope is not a package
118 -- declaration, these entities are never visible subsequently, and can be
119 -- removed from visibility chains. If the scope is a package declaration,
120 -- its visible declarations may still be accessible. Therefore the entities
121 -- defined in such a scope are left on the visibility chains, and only
122 -- their visibility (immediately visibility or potential use-visibility)
123 -- is affected.
125 -- The ordering of homonyms on their chain does not necessarily follow
126 -- the order of their corresponding scopes on the scope stack. For
127 -- example, if package P and the enclosing scope both contain entities
128 -- named E, then when compiling the package body the chain for E will
129 -- hold the global entity first, and the local one (corresponding to
130 -- the current inner scope) next. As a result, name resolution routines
131 -- do not assume any relative ordering of the homonym chains, either
132 -- for scope nesting or to order of appearance of context clauses.
134 -- When compiling a child unit, entities in the parent scope are always
135 -- immediately visible. When compiling the body of a child unit, private
136 -- entities in the parent must also be made immediately visible. There
137 -- are separate routines to make the visible and private declarations
138 -- visible at various times (see package Sem_Ch7).
140 -- +--------+ +-----+
141 -- | In use |-------->| EU1 |-------------------------->
142 -- +--------+ +-----+
143 -- | |
144 -- +--------+ +-----+ +-----+
145 -- | Stand. |---------------->| ES1 |--------------->| ES2 |--->
146 -- +--------+ +-----+ +-----+
147 -- | |
148 -- +---------+ | +-----+
149 -- | with'ed |------------------------------>| EW2 |--->
150 -- +---------+ | +-----+
151 -- | |
152 -- +--------+ +-----+ +-----+
153 -- | Scope2 |---------------->| E12 |--------------->| E22 |--->
154 -- +--------+ +-----+ +-----+
155 -- | |
156 -- +--------+ +-----+ +-----+
157 -- | Scope1 |---------------->| E11 |--------------->| E12 |--->
158 -- +--------+ +-----+ +-----+
159 -- ^ | |
160 -- | | |
161 -- | +---------+ | |
162 -- | | with'ed |----------------------------------------->
163 -- | +---------+ | |
164 -- | | |
165 -- Scope stack | |
166 -- (innermost first) | |
167 -- +----------------------------+
168 -- Names table => | Id1 | | | | Id2 |
169 -- +----------------------------+
171 -- Name resolution must deal with several syntactic forms: simple names,
172 -- qualified names, indexed names, and various forms of calls.
174 -- Each identifier points to an entry in the names table. The resolution
175 -- of a simple name consists in traversing the homonym chain, starting
176 -- from the names table. If an entry is immediately visible, it is the one
177 -- designated by the identifier. If only potentially use-visible entities
178 -- are on the chain, we must verify that they do not hide each other. If
179 -- the entity we find is overloadable, we collect all other overloadable
180 -- entities on the chain as long as they are not hidden.
182 -- To resolve expanded names, we must find the entity at the intersection
183 -- of the entity chain for the scope (the prefix) and the homonym chain
184 -- for the selector. In general, homonym chains will be much shorter than
185 -- entity chains, so it is preferable to start from the names table as
186 -- well. If the entity found is overloadable, we must collect all other
187 -- interpretations that are defined in the scope denoted by the prefix.
189 -- For records, protected types, and tasks, their local entities are
190 -- removed from visibility chains on exit from the corresponding scope.
191 -- From the outside, these entities are always accessed by selected
192 -- notation, and the entity chain for the record type, protected type,
193 -- etc. is traversed sequentially in order to find the designated entity.
195 -- The discriminants of a type and the operations of a protected type or
196 -- task are unchained on exit from the first view of the type, (such as
197 -- a private or incomplete type declaration, or a protected type speci-
198 -- fication) and re-chained when compiling the second view.
200 -- In the case of operators, we do not make operators on derived types
201 -- explicit. As a result, the notation P."+" may denote either a user-
202 -- defined function with name "+", or else an implicit declaration of the
203 -- operator "+" in package P. The resolution of expanded names always
204 -- tries to resolve an operator name as such an implicitly defined entity,
205 -- in addition to looking for explicit declarations.
207 -- All forms of names that denote entities (simple names, expanded names,
208 -- character literals in some cases) have a Entity attribute, which
209 -- identifies the entity denoted by the name.
211 ---------------------
212 -- The Scope Stack --
213 ---------------------
215 -- The Scope stack keeps track of the scopes currently been compiled.
216 -- Every entity that contains declarations (including records) is placed
217 -- on the scope stack while it is being processed, and removed at the end.
218 -- Whenever a non-package scope is exited, the entities defined therein
219 -- are removed from the visibility table, so that entities in outer scopes
220 -- become visible (see previous description). On entry to Sem, the scope
221 -- stack only contains the package Standard. As usual, subunits complicate
222 -- this picture ever so slightly.
224 -- The Rtsfind mechanism can force a call to Semantics while another
225 -- compilation is in progress. The unit retrieved by Rtsfind must be
226 -- compiled in its own context, and has no access to the visibility of
227 -- the unit currently being compiled. The procedures Save_Scope_Stack and
228 -- Restore_Scope_Stack make entities in current open scopes invisible
229 -- before compiling the retrieved unit, and restore the compilation
230 -- environment afterwards.
232 ------------------------
233 -- Compiling subunits --
234 ------------------------
236 -- Subunits must be compiled in the environment of the corresponding stub,
237 -- that is to say with the same visibility into the parent (and its
238 -- context) that is available at the point of the stub declaration, but
239 -- with the additional visibility provided by the context clause of the
240 -- subunit itself. As a result, compilation of a subunit forces compilation
241 -- of the parent (see description in lib-). At the point of the stub
242 -- declaration, Analyze is called recursively to compile the proper body of
243 -- the subunit, but without reinitializing the names table, nor the scope
244 -- stack (i.e. standard is not pushed on the stack). In this fashion the
245 -- context of the subunit is added to the context of the parent, and the
246 -- subunit is compiled in the correct environment. Note that in the course
247 -- of processing the context of a subunit, Standard will appear twice on
248 -- the scope stack: once for the parent of the subunit, and once for the
249 -- unit in the context clause being compiled. However, the two sets of
250 -- entities are not linked by homonym chains, so that the compilation of
251 -- any context unit happens in a fresh visibility environment.
253 -------------------------------
254 -- Processing of USE Clauses --
255 -------------------------------
257 -- Every defining occurrence has a flag indicating if it is potentially use
258 -- visible. Resolution of simple names examines this flag. The processing
259 -- of use clauses consists in setting this flag on all visible entities
260 -- defined in the corresponding package. On exit from the scope of the use
261 -- clause, the corresponding flag must be reset. However, a package may
262 -- appear in several nested use clauses (pathological but legal, alas)
263 -- which forces us to use a slightly more involved scheme:
265 -- a) The defining occurrence for a package holds a flag -In_Use- to
266 -- indicate that it is currently in the scope of a use clause. If a
267 -- redundant use clause is encountered, then the corresponding occurrence
268 -- of the package name is flagged -Redundant_Use-.
270 -- b) On exit from a scope, the use clauses in its declarative part are
271 -- scanned. The visibility flag is reset in all entities declared in
272 -- package named in a use clause, as long as the package is not flagged
273 -- as being in a redundant use clause (in which case the outer use
274 -- clause is still in effect, and the direct visibility of its entities
275 -- must be retained).
277 -- Note that entities are not removed from their homonym chains on exit
278 -- from the package specification. A subsequent use clause does not need
279 -- to rechain the visible entities, but only to establish their direct
280 -- visibility.
282 -----------------------------------
283 -- Handling private declarations --
284 -----------------------------------
286 -- The principle that each entity has a single defining occurrence clashes
287 -- with the presence of two separate definitions for private types: the
288 -- first is the private type declaration, and second is the full type
289 -- declaration. It is important that all references to the type point to
290 -- the same defining occurrence, namely the first one. To enforce the two
291 -- separate views of the entity, the corresponding information is swapped
292 -- between the two declarations. Outside of the package, the defining
293 -- occurrence only contains the private declaration information, while in
294 -- the private part and the body of the package the defining occurrence
295 -- contains the full declaration. To simplify the swap, the defining
296 -- occurrence that currently holds the private declaration points to the
297 -- full declaration. During semantic processing the defining occurrence
298 -- also points to a list of private dependents, that is to say access types
299 -- or composite types whose designated types or component types are
300 -- subtypes or derived types of the private type in question. After the
301 -- full declaration has been seen, the private dependents are updated to
302 -- indicate that they have full definitions.
304 ------------------------------------
305 -- Handling of Undefined Messages --
306 ------------------------------------
308 -- In normal mode, only the first use of an undefined identifier generates
309 -- a message. The table Urefs is used to record error messages that have
310 -- been issued so that second and subsequent ones do not generate further
311 -- messages. However, the second reference causes text to be added to the
312 -- original undefined message noting "(more references follow)". The
313 -- full error list option (-gnatf) forces messages to be generated for
314 -- every reference and disconnects the use of this table.
316 type Uref_Entry is record
317 Node : Node_Id;
318 -- Node for identifier for which original message was posted. The
319 -- Chars field of this identifier is used to detect later references
320 -- to the same identifier.
322 Err : Error_Msg_Id;
323 -- Records error message Id of original undefined message. Reset to
324 -- No_Error_Msg after the second occurrence, where it is used to add
325 -- text to the original message as described above.
327 Nvis : Boolean;
328 -- Set if the message is not visible rather than undefined
330 Loc : Source_Ptr;
331 -- Records location of error message. Used to make sure that we do
332 -- not consider a, b : undefined as two separate instances, which
333 -- would otherwise happen, since the parser converts this sequence
334 -- to a : undefined; b : undefined.
336 end record;
338 package Urefs is new Table.Table (
339 Table_Component_Type => Uref_Entry,
340 Table_Index_Type => Nat,
341 Table_Low_Bound => 1,
342 Table_Initial => 10,
343 Table_Increment => 100,
344 Table_Name => "Urefs");
346 Candidate_Renaming : Entity_Id;
347 -- Holds a candidate interpretation that appears in a subprogram renaming
348 -- declaration and does not match the given specification, but matches at
349 -- least on the first formal. Allows better error message when given
350 -- specification omits defaulted parameters, a common error.
352 -----------------------
353 -- Local Subprograms --
354 -----------------------
356 procedure Analyze_Generic_Renaming
357 (N : Node_Id;
358 K : Entity_Kind);
359 -- Common processing for all three kinds of generic renaming declarations.
360 -- Enter new name and indicate that it renames the generic unit.
362 procedure Analyze_Renamed_Character
363 (N : Node_Id;
364 New_S : Entity_Id;
365 Is_Body : Boolean);
366 -- Renamed entity is given by a character literal, which must belong
367 -- to the return type of the new entity. Is_Body indicates whether the
368 -- declaration is a renaming_as_body. If the original declaration has
369 -- already been frozen (because of an intervening body, e.g.) the body of
370 -- the function must be built now. The same applies to the following
371 -- various renaming procedures.
373 procedure Analyze_Renamed_Dereference
374 (N : Node_Id;
375 New_S : Entity_Id;
376 Is_Body : Boolean);
377 -- Renamed entity is given by an explicit dereference. Prefix must be a
378 -- conformant access_to_subprogram type.
380 procedure Analyze_Renamed_Entry
381 (N : Node_Id;
382 New_S : Entity_Id;
383 Is_Body : Boolean);
384 -- If the renamed entity in a subprogram renaming is an entry or protected
385 -- subprogram, build a body for the new entity whose only statement is a
386 -- call to the renamed entity.
388 procedure Analyze_Renamed_Family_Member
389 (N : Node_Id;
390 New_S : Entity_Id;
391 Is_Body : Boolean);
392 -- Used when the renamed entity is an indexed component. The prefix must
393 -- denote an entry family.
395 procedure Analyze_Renamed_Primitive_Operation
396 (N : Node_Id;
397 New_S : Entity_Id;
398 Is_Body : Boolean);
399 -- If the renamed entity in a subprogram renaming is a primitive operation
400 -- or a class-wide operation in prefix form, save the target object,
401 -- which must be added to the list of actuals in any subsequent call.
402 -- The renaming operation is intrinsic because the compiler must in
403 -- fact generate a wrapper for it (6.3.1 (10 1/2)).
405 function Applicable_Use (Pack_Name : Node_Id) return Boolean;
406 -- Common code to Use_One_Package and Set_Use, to determine whether use
407 -- clause must be processed. Pack_Name is an entity name that references
408 -- the package in question.
410 procedure Attribute_Renaming (N : Node_Id);
411 -- Analyze renaming of attribute as subprogram. The renaming declaration N
412 -- is rewritten as a subprogram body that returns the attribute reference
413 -- applied to the formals of the function.
415 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
416 -- Set Entity, with style check if need be. For a discriminant reference,
417 -- replace by the corresponding discriminal, i.e. the parameter of the
418 -- initialization procedure that corresponds to the discriminant.
420 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
421 -- A renaming_as_body may occur after the entity of the original decla-
422 -- ration has been frozen. In that case, the body of the new entity must
423 -- be built now, because the usual mechanism of building the renamed
424 -- body at the point of freezing will not work. Subp is the subprogram
425 -- for which N provides the Renaming_As_Body.
427 procedure Check_In_Previous_With_Clause
428 (N : Node_Id;
429 Nam : Node_Id);
430 -- N is a use_package clause and Nam the package name, or N is a use_type
431 -- clause and Nam is the prefix of the type name. In either case, verify
432 -- that the package is visible at that point in the context: either it
433 -- appears in a previous with_clause, or because it is a fully qualified
434 -- name and the root ancestor appears in a previous with_clause.
436 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
437 -- Verify that the entity in a renaming declaration that is a library unit
438 -- is itself a library unit and not a nested unit or subunit. Also check
439 -- that if the renaming is a child unit of a generic parent, then the
440 -- renamed unit must also be a child unit of that parent. Finally, verify
441 -- that a renamed generic unit is not an implicit child declared within
442 -- an instance of the parent.
444 procedure Chain_Use_Clause (N : Node_Id);
445 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
446 -- the proper scope table entry. This is usually the current scope, but it
447 -- will be an inner scope when installing the use clauses of the private
448 -- declarations of a parent unit prior to compiling the private part of a
449 -- child unit. This chain is traversed when installing/removing use clauses
450 -- when compiling a subunit or instantiating a generic body on the fly,
451 -- when it is necessary to save and restore full environments.
453 function Enclosing_Instance return Entity_Id;
454 -- In an instance nested within another one, several semantic checks are
455 -- unnecessary because the legality of the nested instance has been checked
456 -- in the enclosing generic unit. This applies in particular to legality
457 -- checks on actuals for formal subprograms of the inner instance, which
458 -- are checked as subprogram renamings, and may be complicated by confusion
459 -- in private/full views. This function returns the instance enclosing the
460 -- current one if there is such, else it returns Empty.
462 -- If the renaming determines the entity for the default of a formal
463 -- subprogram nested within another instance, choose the innermost
464 -- candidate. This is because if the formal has a box, and we are within
465 -- an enclosing instance where some candidate interpretations are local
466 -- to this enclosing instance, we know that the default was properly
467 -- resolved when analyzing the generic, so we prefer the local
468 -- candidates to those that are external. This is not always the case
469 -- but is a reasonable heuristic on the use of nested generics. The
470 -- proper solution requires a full renaming model.
472 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
473 -- Find a type derived from Character or Wide_Character in the prefix of N.
474 -- Used to resolved qualified names whose selector is a character literal.
476 function Has_Private_With (E : Entity_Id) return Boolean;
477 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
478 -- private with on E.
480 procedure Find_Expanded_Name (N : Node_Id);
481 -- The input is a selected component known to be an expanded name. Verify
482 -- legality of selector given the scope denoted by prefix, and change node
483 -- N into a expanded name with a properly set Entity field.
485 function Find_Renamed_Entity
486 (N : Node_Id;
487 Nam : Node_Id;
488 New_S : Entity_Id;
489 Is_Actual : Boolean := False) return Entity_Id;
490 -- Find the renamed entity that corresponds to the given parameter profile
491 -- in a subprogram renaming declaration. The renamed entity may be an
492 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
493 -- indicates that the renaming is the one generated for an actual subpro-
494 -- gram in an instance, for which special visibility checks apply.
496 function Has_Implicit_Operator (N : Node_Id) return Boolean;
497 -- N is an expanded name whose selector is an operator name (e.g. P."+").
498 -- declarative part contains an implicit declaration of an operator if it
499 -- has a declaration of a type to which one of the predefined operators
500 -- apply. The existence of this routine is an implementation artifact. A
501 -- more straightforward but more space-consuming choice would be to make
502 -- all inherited operators explicit in the symbol table.
504 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
505 -- A subprogram defined by a renaming declaration inherits the parameter
506 -- profile of the renamed entity. The subtypes given in the subprogram
507 -- specification are discarded and replaced with those of the renamed
508 -- subprogram, which are then used to recheck the default values.
510 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean;
511 -- Prefix is appropriate for record if it is of a record type, or an access
512 -- to such.
514 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean;
515 -- True if it is of a task type, a protected type, or else an access to one
516 -- of these types.
518 procedure Note_Redundant_Use (Clause : Node_Id);
519 -- Mark the name in a use clause as redundant if the corresponding entity
520 -- is already use-visible. Emit a warning if the use clause comes from
521 -- source and the proper warnings are enabled.
523 procedure Premature_Usage (N : Node_Id);
524 -- Diagnose usage of an entity before it is visible
526 procedure Use_One_Package (P : Entity_Id; N : Node_Id);
527 -- Make visible entities declared in package P potentially use-visible
528 -- in the current context. Also used in the analysis of subunits, when
529 -- re-installing use clauses of parent units. N is the use_clause that
530 -- names P (and possibly other packages).
532 procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False);
533 -- Id is the subtype mark from a use type clause. This procedure makes
534 -- the primitive operators of the type potentially use-visible. The
535 -- boolean flag Installed indicates that the clause is being reinstalled
536 -- after previous analysis, and primitive operations are already chained
537 -- on the Used_Operations list of the clause.
539 procedure Write_Info;
540 -- Write debugging information on entities declared in current scope
542 --------------------------------
543 -- Analyze_Exception_Renaming --
544 --------------------------------
546 -- The language only allows a single identifier, but the tree holds an
547 -- identifier list. The parser has already issued an error message if
548 -- there is more than one element in the list.
550 procedure Analyze_Exception_Renaming (N : Node_Id) is
551 Id : constant Node_Id := Defining_Identifier (N);
552 Nam : constant Node_Id := Name (N);
554 begin
555 Check_SPARK_05_Restriction ("exception renaming is not allowed", N);
557 Enter_Name (Id);
558 Analyze (Nam);
560 Set_Ekind (Id, E_Exception);
561 Set_Etype (Id, Standard_Exception_Type);
562 Set_Is_Pure (Id, Is_Pure (Current_Scope));
564 if not Is_Entity_Name (Nam) or else
565 Ekind (Entity (Nam)) /= E_Exception
566 then
567 Error_Msg_N ("invalid exception name in renaming", Nam);
568 else
569 if Present (Renamed_Object (Entity (Nam))) then
570 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
571 else
572 Set_Renamed_Object (Id, Entity (Nam));
573 end if;
574 end if;
576 -- Implementation-defined aspect specifications can appear in a renaming
577 -- declaration, but not language-defined ones. The call to procedure
578 -- Analyze_Aspect_Specifications will take care of this error check.
580 if Has_Aspects (N) then
581 Analyze_Aspect_Specifications (N, Id);
582 end if;
583 end Analyze_Exception_Renaming;
585 ---------------------------
586 -- Analyze_Expanded_Name --
587 ---------------------------
589 procedure Analyze_Expanded_Name (N : Node_Id) is
590 begin
591 -- If the entity pointer is already set, this is an internal node, or a
592 -- node that is analyzed more than once, after a tree modification. In
593 -- such a case there is no resolution to perform, just set the type. For
594 -- completeness, analyze prefix as well.
596 if Present (Entity (N)) then
597 if Is_Type (Entity (N)) then
598 Set_Etype (N, Entity (N));
599 else
600 Set_Etype (N, Etype (Entity (N)));
601 end if;
603 Analyze (Prefix (N));
604 return;
605 else
606 Find_Expanded_Name (N);
607 end if;
609 Analyze_Dimension (N);
610 end Analyze_Expanded_Name;
612 ---------------------------------------
613 -- Analyze_Generic_Function_Renaming --
614 ---------------------------------------
616 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
617 begin
618 Analyze_Generic_Renaming (N, E_Generic_Function);
619 end Analyze_Generic_Function_Renaming;
621 --------------------------------------
622 -- Analyze_Generic_Package_Renaming --
623 --------------------------------------
625 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
626 begin
627 -- Test for the Text_IO special unit case here, since we may be renaming
628 -- one of the subpackages of Text_IO, then join common routine.
630 Check_Text_IO_Special_Unit (Name (N));
632 Analyze_Generic_Renaming (N, E_Generic_Package);
633 end Analyze_Generic_Package_Renaming;
635 ----------------------------------------
636 -- Analyze_Generic_Procedure_Renaming --
637 ----------------------------------------
639 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
640 begin
641 Analyze_Generic_Renaming (N, E_Generic_Procedure);
642 end Analyze_Generic_Procedure_Renaming;
644 ------------------------------
645 -- Analyze_Generic_Renaming --
646 ------------------------------
648 procedure Analyze_Generic_Renaming
649 (N : Node_Id;
650 K : Entity_Kind)
652 New_P : constant Entity_Id := Defining_Entity (N);
653 Old_P : Entity_Id;
654 Inst : Boolean := False; -- prevent junk warning
656 begin
657 if Name (N) = Error then
658 return;
659 end if;
661 Check_SPARK_05_Restriction ("generic renaming is not allowed", N);
663 Generate_Definition (New_P);
665 if Current_Scope /= Standard_Standard then
666 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
667 end if;
669 if Nkind (Name (N)) = N_Selected_Component then
670 Check_Generic_Child_Unit (Name (N), Inst);
671 else
672 Analyze (Name (N));
673 end if;
675 if not Is_Entity_Name (Name (N)) then
676 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
677 Old_P := Any_Id;
678 else
679 Old_P := Entity (Name (N));
680 end if;
682 Enter_Name (New_P);
683 Set_Ekind (New_P, K);
685 if Etype (Old_P) = Any_Type then
686 null;
688 elsif Ekind (Old_P) /= K then
689 Error_Msg_N ("invalid generic unit name", Name (N));
691 else
692 if Present (Renamed_Object (Old_P)) then
693 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
694 else
695 Set_Renamed_Object (New_P, Old_P);
696 end if;
698 Set_Is_Pure (New_P, Is_Pure (Old_P));
699 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
701 Set_Etype (New_P, Etype (Old_P));
702 Set_Has_Completion (New_P);
704 if In_Open_Scopes (Old_P) then
705 Error_Msg_N ("within its scope, generic denotes its instance", N);
706 end if;
708 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
709 -- renamings and subsequent instantiations of Unchecked_Conversion.
711 if Ekind_In (Old_P, E_Generic_Function, E_Generic_Procedure) then
712 Set_Is_Intrinsic_Subprogram
713 (New_P, Is_Intrinsic_Subprogram (Old_P));
714 end if;
716 Check_Library_Unit_Renaming (N, Old_P);
717 end if;
719 -- Implementation-defined aspect specifications can appear in a renaming
720 -- declaration, but not language-defined ones. The call to procedure
721 -- Analyze_Aspect_Specifications will take care of this error check.
723 if Has_Aspects (N) then
724 Analyze_Aspect_Specifications (N, New_P);
725 end if;
726 end Analyze_Generic_Renaming;
728 -----------------------------
729 -- Analyze_Object_Renaming --
730 -----------------------------
732 procedure Analyze_Object_Renaming (N : Node_Id) is
733 Loc : constant Source_Ptr := Sloc (N);
734 Id : constant Entity_Id := Defining_Identifier (N);
735 Dec : Node_Id;
736 Nam : constant Node_Id := Name (N);
737 T : Entity_Id;
738 T2 : Entity_Id;
740 procedure Check_Constrained_Object;
741 -- If the nominal type is unconstrained but the renamed object is
742 -- constrained, as can happen with renaming an explicit dereference or
743 -- a function return, build a constrained subtype from the object. If
744 -- the renaming is for a formal in an accept statement, the analysis
745 -- has already established its actual subtype. This is only relevant
746 -- if the renamed object is an explicit dereference.
748 function In_Generic_Scope (E : Entity_Id) return Boolean;
749 -- Determine whether entity E is inside a generic cope
751 ------------------------------
752 -- Check_Constrained_Object --
753 ------------------------------
755 procedure Check_Constrained_Object is
756 Typ : constant Entity_Id := Etype (Nam);
757 Subt : Entity_Id;
759 begin
760 if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference)
761 and then Is_Composite_Type (Etype (Nam))
762 and then not Is_Constrained (Etype (Nam))
763 and then not Has_Unknown_Discriminants (Etype (Nam))
764 and then Expander_Active
765 then
766 -- If Actual_Subtype is already set, nothing to do
768 if Ekind_In (Id, E_Variable, E_Constant)
769 and then Present (Actual_Subtype (Id))
770 then
771 null;
773 -- A renaming of an unchecked union has no actual subtype
775 elsif Is_Unchecked_Union (Typ) then
776 null;
778 -- If a record is limited its size is invariant. This is the case
779 -- in particular with record types with an access discirminant
780 -- that are used in iterators. This is an optimization, but it
781 -- also prevents typing anomalies when the prefix is further
782 -- expanded. Limited types with discriminants are included.
784 elsif Is_Limited_Record (Typ)
785 or else
786 (Ekind (Typ) = E_Limited_Private_Type
787 and then Has_Discriminants (Typ)
788 and then Is_Access_Type (Etype (First_Discriminant (Typ))))
789 then
790 null;
792 else
793 Subt := Make_Temporary (Loc, 'T');
794 Remove_Side_Effects (Nam);
795 Insert_Action (N,
796 Make_Subtype_Declaration (Loc,
797 Defining_Identifier => Subt,
798 Subtype_Indication =>
799 Make_Subtype_From_Expr (Nam, Typ)));
800 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
801 Set_Etype (Nam, Subt);
803 -- Freeze subtype at once, to prevent order of elaboration
804 -- issues in the backend. The renamed object exists, so its
805 -- type is already frozen in any case.
807 Freeze_Before (N, Subt);
808 end if;
809 end if;
810 end Check_Constrained_Object;
812 ----------------------
813 -- In_Generic_Scope --
814 ----------------------
816 function In_Generic_Scope (E : Entity_Id) return Boolean is
817 S : Entity_Id;
819 begin
820 S := Scope (E);
821 while Present (S) and then S /= Standard_Standard loop
822 if Is_Generic_Unit (S) then
823 return True;
824 end if;
826 S := Scope (S);
827 end loop;
829 return False;
830 end In_Generic_Scope;
832 -- Start of processing for Analyze_Object_Renaming
834 begin
835 if Nam = Error then
836 return;
837 end if;
839 Check_SPARK_05_Restriction ("object renaming is not allowed", N);
841 Set_Is_Pure (Id, Is_Pure (Current_Scope));
842 Enter_Name (Id);
844 -- The renaming of a component that depends on a discriminant requires
845 -- an actual subtype, because in subsequent use of the object Gigi will
846 -- be unable to locate the actual bounds. This explicit step is required
847 -- when the renaming is generated in removing side effects of an
848 -- already-analyzed expression.
850 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
851 T := Etype (Nam);
852 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
854 if Present (Dec) then
855 Insert_Action (N, Dec);
856 T := Defining_Identifier (Dec);
857 Set_Etype (Nam, T);
858 end if;
860 -- Complete analysis of the subtype mark in any case, for ASIS use
862 if Present (Subtype_Mark (N)) then
863 Find_Type (Subtype_Mark (N));
864 end if;
866 elsif Present (Subtype_Mark (N)) then
867 Find_Type (Subtype_Mark (N));
868 T := Entity (Subtype_Mark (N));
869 Analyze (Nam);
871 -- Reject renamings of conversions unless the type is tagged, or
872 -- the conversion is implicit (which can occur for cases of anonymous
873 -- access types in Ada 2012).
875 if Nkind (Nam) = N_Type_Conversion
876 and then Comes_From_Source (Nam)
877 and then not Is_Tagged_Type (T)
878 then
879 Error_Msg_N
880 ("renaming of conversion only allowed for tagged types", Nam);
881 end if;
883 Resolve (Nam, T);
885 -- If the renamed object is a function call of a limited type,
886 -- the expansion of the renaming is complicated by the presence
887 -- of various temporaries and subtypes that capture constraints
888 -- of the renamed object. Rewrite node as an object declaration,
889 -- whose expansion is simpler. Given that the object is limited
890 -- there is no copy involved and no performance hit.
892 if Nkind (Nam) = N_Function_Call
893 and then Is_Limited_View (Etype (Nam))
894 and then not Is_Constrained (Etype (Nam))
895 and then Comes_From_Source (N)
896 then
897 Set_Etype (Id, T);
898 Set_Ekind (Id, E_Constant);
899 Rewrite (N,
900 Make_Object_Declaration (Loc,
901 Defining_Identifier => Id,
902 Constant_Present => True,
903 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
904 Expression => Relocate_Node (Nam)));
905 return;
906 end if;
908 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
909 -- when renaming declaration has a named access type. The Ada 2012
910 -- coverage rules allow an anonymous access type in the context of
911 -- an expected named general access type, but the renaming rules
912 -- require the types to be the same. (An exception is when the type
913 -- of the renaming is also an anonymous access type, which can only
914 -- happen due to a renaming created by the expander.)
916 if Nkind (Nam) = N_Type_Conversion
917 and then not Comes_From_Source (Nam)
918 and then Ekind (Etype (Expression (Nam))) = E_Anonymous_Access_Type
919 and then Ekind (T) /= E_Anonymous_Access_Type
920 then
921 Wrong_Type (Expression (Nam), T); -- Should we give better error???
922 end if;
924 -- Check that a class-wide object is not being renamed as an object
925 -- of a specific type. The test for access types is needed to exclude
926 -- cases where the renamed object is a dynamically tagged access
927 -- result, such as occurs in certain expansions.
929 if Is_Tagged_Type (T) then
930 Check_Dynamically_Tagged_Expression
931 (Expr => Nam,
932 Typ => T,
933 Related_Nod => N);
934 end if;
936 -- Ada 2005 (AI-230/AI-254): Access renaming
938 else pragma Assert (Present (Access_Definition (N)));
939 T := Access_Definition
940 (Related_Nod => N,
941 N => Access_Definition (N));
943 Analyze (Nam);
945 -- Ada 2005 AI05-105: if the declaration has an anonymous access
946 -- type, the renamed object must also have an anonymous type, and
947 -- this is a name resolution rule. This was implicit in the last part
948 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
949 -- recent AI.
951 if not Is_Overloaded (Nam) then
952 if Ekind (Etype (Nam)) /= Ekind (T) then
953 Error_Msg_N
954 ("expect anonymous access type in object renaming", N);
955 end if;
957 else
958 declare
959 I : Interp_Index;
960 It : Interp;
961 Typ : Entity_Id := Empty;
962 Seen : Boolean := False;
964 begin
965 Get_First_Interp (Nam, I, It);
966 while Present (It.Typ) loop
968 -- Renaming is ambiguous if more than one candidate
969 -- interpretation is type-conformant with the context.
971 if Ekind (It.Typ) = Ekind (T) then
972 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
973 and then
974 Type_Conformant
975 (Designated_Type (T), Designated_Type (It.Typ))
976 then
977 if not Seen then
978 Seen := True;
979 else
980 Error_Msg_N
981 ("ambiguous expression in renaming", Nam);
982 end if;
984 elsif Ekind (T) = E_Anonymous_Access_Type
985 and then
986 Covers (Designated_Type (T), Designated_Type (It.Typ))
987 then
988 if not Seen then
989 Seen := True;
990 else
991 Error_Msg_N
992 ("ambiguous expression in renaming", Nam);
993 end if;
994 end if;
996 if Covers (T, It.Typ) then
997 Typ := It.Typ;
998 Set_Etype (Nam, Typ);
999 Set_Is_Overloaded (Nam, False);
1000 end if;
1001 end if;
1003 Get_Next_Interp (I, It);
1004 end loop;
1005 end;
1006 end if;
1008 Resolve (Nam, T);
1010 -- Ada 2005 (AI-231): In the case where the type is defined by an
1011 -- access_definition, the renamed entity shall be of an access-to-
1012 -- constant type if and only if the access_definition defines an
1013 -- access-to-constant type. ARM 8.5.1(4)
1015 if Constant_Present (Access_Definition (N))
1016 and then not Is_Access_Constant (Etype (Nam))
1017 then
1018 Error_Msg_N ("(Ada 2005): the renamed object is not "
1019 & "access-to-constant (RM 8.5.1(6))", N);
1021 elsif not Constant_Present (Access_Definition (N))
1022 and then Is_Access_Constant (Etype (Nam))
1023 then
1024 Error_Msg_N ("(Ada 2005): the renamed object is not "
1025 & "access-to-variable (RM 8.5.1(6))", N);
1026 end if;
1028 if Is_Access_Subprogram_Type (Etype (Nam)) then
1029 Check_Subtype_Conformant
1030 (Designated_Type (T), Designated_Type (Etype (Nam)));
1032 elsif not Subtypes_Statically_Match
1033 (Designated_Type (T),
1034 Available_View (Designated_Type (Etype (Nam))))
1035 then
1036 Error_Msg_N
1037 ("subtype of renamed object does not statically match", N);
1038 end if;
1039 end if;
1041 -- Special processing for renaming function return object. Some errors
1042 -- and warnings are produced only for calls that come from source.
1044 if Nkind (Nam) = N_Function_Call then
1045 case Ada_Version is
1047 -- Usage is illegal in Ada 83
1049 when Ada_83 =>
1050 if Comes_From_Source (Nam) then
1051 Error_Msg_N
1052 ("(Ada 83) cannot rename function return object", Nam);
1053 end if;
1055 -- In Ada 95, warn for odd case of renaming parameterless function
1056 -- call if this is not a limited type (where this is useful).
1058 when others =>
1059 if Warn_On_Object_Renames_Function
1060 and then No (Parameter_Associations (Nam))
1061 and then not Is_Limited_Type (Etype (Nam))
1062 and then Comes_From_Source (Nam)
1063 then
1064 Error_Msg_N
1065 ("renaming function result object is suspicious?R?", Nam);
1066 Error_Msg_NE
1067 ("\function & will be called only once?R?", Nam,
1068 Entity (Name (Nam)));
1069 Error_Msg_N -- CODEFIX
1070 ("\suggest using an initialized constant "
1071 & "object instead?R?", Nam);
1072 end if;
1074 end case;
1075 end if;
1077 Check_Constrained_Object;
1079 -- An object renaming requires an exact match of the type. Class-wide
1080 -- matching is not allowed.
1082 if Is_Class_Wide_Type (T)
1083 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1084 then
1085 Wrong_Type (Nam, T);
1086 end if;
1088 T2 := Etype (Nam);
1090 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1092 if Nkind (Nam) = N_Explicit_Dereference
1093 and then Ekind (Etype (T2)) = E_Incomplete_Type
1094 then
1095 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1096 return;
1098 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1099 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1100 return;
1101 end if;
1103 -- Ada 2005 (AI-327)
1105 if Ada_Version >= Ada_2005
1106 and then Nkind (Nam) = N_Attribute_Reference
1107 and then Attribute_Name (Nam) = Name_Priority
1108 then
1109 null;
1111 elsif Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1112 declare
1113 Nam_Decl : Node_Id;
1114 Nam_Ent : Entity_Id;
1116 begin
1117 if Nkind (Nam) = N_Attribute_Reference then
1118 Nam_Ent := Entity (Prefix (Nam));
1119 else
1120 Nam_Ent := Entity (Nam);
1121 end if;
1123 Nam_Decl := Parent (Nam_Ent);
1125 if Has_Null_Exclusion (N)
1126 and then not Has_Null_Exclusion (Nam_Decl)
1127 then
1128 -- Ada 2005 (AI-423): If the object name denotes a generic
1129 -- formal object of a generic unit G, and the object renaming
1130 -- declaration occurs within the body of G or within the body
1131 -- of a generic unit declared within the declarative region
1132 -- of G, then the declaration of the formal object of G must
1133 -- have a null exclusion or a null-excluding subtype.
1135 if Is_Formal_Object (Nam_Ent)
1136 and then In_Generic_Scope (Id)
1137 then
1138 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1139 Error_Msg_N
1140 ("renamed formal does not exclude `NULL` "
1141 & "(RM 8.5.1(4.6/2))", N);
1143 elsif In_Package_Body (Scope (Id)) then
1144 Error_Msg_N
1145 ("formal object does not have a null exclusion"
1146 & "(RM 8.5.1(4.6/2))", N);
1147 end if;
1149 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1150 -- shall exclude null.
1152 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1153 Error_Msg_N
1154 ("renamed object does not exclude `NULL` "
1155 & "(RM 8.5.1(4.6/2))", N);
1157 -- An instance is illegal if it contains a renaming that
1158 -- excludes null, and the actual does not. The renaming
1159 -- declaration has already indicated that the declaration
1160 -- of the renamed actual in the instance will raise
1161 -- constraint_error.
1163 elsif Nkind (Nam_Decl) = N_Object_Declaration
1164 and then In_Instance
1165 and then
1166 Present (Corresponding_Generic_Association (Nam_Decl))
1167 and then Nkind (Expression (Nam_Decl)) =
1168 N_Raise_Constraint_Error
1169 then
1170 Error_Msg_N
1171 ("renamed actual does not exclude `NULL` "
1172 & "(RM 8.5.1(4.6/2))", N);
1174 -- Finally, if there is a null exclusion, the subtype mark
1175 -- must not be null-excluding.
1177 elsif No (Access_Definition (N))
1178 and then Can_Never_Be_Null (T)
1179 then
1180 Error_Msg_NE
1181 ("`NOT NULL` not allowed (& already excludes null)",
1182 N, T);
1184 end if;
1186 elsif Can_Never_Be_Null (T)
1187 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1188 then
1189 Error_Msg_N
1190 ("renamed object does not exclude `NULL` "
1191 & "(RM 8.5.1(4.6/2))", N);
1193 elsif Has_Null_Exclusion (N)
1194 and then No (Access_Definition (N))
1195 and then Can_Never_Be_Null (T)
1196 then
1197 Error_Msg_NE
1198 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1199 end if;
1200 end;
1201 end if;
1203 -- Set the Ekind of the entity, unless it has been set already, as is
1204 -- the case for the iteration object over a container with no variable
1205 -- indexing. In that case it's been marked as a constant, and we do not
1206 -- want to change it to a variable.
1208 if Ekind (Id) /= E_Constant then
1209 Set_Ekind (Id, E_Variable);
1210 end if;
1212 -- Initialize the object size and alignment. Note that we used to call
1213 -- Init_Size_Align here, but that's wrong for objects which have only
1214 -- an Esize, not an RM_Size field.
1216 Init_Object_Size_Align (Id);
1218 if T = Any_Type or else Etype (Nam) = Any_Type then
1219 return;
1221 -- Verify that the renamed entity is an object or a function call. It
1222 -- may have been rewritten in several ways.
1224 elsif Is_Object_Reference (Nam) then
1225 if Comes_From_Source (N) then
1226 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1227 Error_Msg_N
1228 ("illegal renaming of discriminant-dependent component", Nam);
1229 end if;
1231 -- If the renaming comes from source and the renamed object is a
1232 -- dereference, then mark the prefix as needing debug information,
1233 -- since it might have been rewritten hence internally generated
1234 -- and Debug_Renaming_Declaration will link the renaming to it.
1236 if Nkind (Nam) = N_Explicit_Dereference
1237 and then Is_Entity_Name (Prefix (Nam))
1238 then
1239 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1240 end if;
1241 end if;
1243 -- A static function call may have been folded into a literal
1245 elsif Nkind (Original_Node (Nam)) = N_Function_Call
1247 -- When expansion is disabled, attribute reference is not rewritten
1248 -- as function call. Otherwise it may be rewritten as a conversion,
1249 -- so check original node.
1251 or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference
1252 and then Is_Function_Attribute_Name
1253 (Attribute_Name (Original_Node (Nam))))
1255 -- Weird but legal, equivalent to renaming a function call. Illegal
1256 -- if the literal is the result of constant-folding an attribute
1257 -- reference that is not a function.
1259 or else (Is_Entity_Name (Nam)
1260 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1261 and then
1262 Nkind (Original_Node (Nam)) /= N_Attribute_Reference)
1264 or else (Nkind (Nam) = N_Type_Conversion
1265 and then Is_Tagged_Type (Entity (Subtype_Mark (Nam))))
1266 then
1267 null;
1269 elsif Nkind (Nam) = N_Type_Conversion then
1270 Error_Msg_N
1271 ("renaming of conversion only allowed for tagged types", Nam);
1273 -- Ada 2005 (AI-327)
1275 elsif Ada_Version >= Ada_2005
1276 and then Nkind (Nam) = N_Attribute_Reference
1277 and then Attribute_Name (Nam) = Name_Priority
1278 then
1279 null;
1281 -- Allow internally generated x'Reference expression
1283 elsif Nkind (Nam) = N_Reference then
1284 null;
1286 else
1287 Error_Msg_N ("expect object name in renaming", Nam);
1288 end if;
1290 Set_Etype (Id, T2);
1292 if not Is_Variable (Nam) then
1293 Set_Ekind (Id, E_Constant);
1294 Set_Never_Set_In_Source (Id, True);
1295 Set_Is_True_Constant (Id, True);
1296 end if;
1298 -- The entity of the renaming declaration needs to reflect whether the
1299 -- renamed object is volatile. Is_Volatile is set if the renamed object
1300 -- is volatile in the RM legality sense.
1302 Set_Is_Volatile (Id, Is_Volatile_Object (Nam));
1304 -- Treat as volatile if we just set the Volatile flag
1306 if Is_Volatile (Id)
1308 -- Or if we are renaming an entity which was marked this way
1310 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1312 or else (Is_Entity_Name (Nam)
1313 and then Treat_As_Volatile (Entity (Nam)))
1314 then
1315 Set_Treat_As_Volatile (Id, True);
1316 end if;
1318 -- Now make the link to the renamed object
1320 Set_Renamed_Object (Id, Nam);
1322 -- Implementation-defined aspect specifications can appear in a renaming
1323 -- declaration, but not language-defined ones. The call to procedure
1324 -- Analyze_Aspect_Specifications will take care of this error check.
1326 if Has_Aspects (N) then
1327 Analyze_Aspect_Specifications (N, Id);
1328 end if;
1330 -- Deal with dimensions
1332 Analyze_Dimension (N);
1333 end Analyze_Object_Renaming;
1335 ------------------------------
1336 -- Analyze_Package_Renaming --
1337 ------------------------------
1339 procedure Analyze_Package_Renaming (N : Node_Id) is
1340 New_P : constant Entity_Id := Defining_Entity (N);
1341 Old_P : Entity_Id;
1342 Spec : Node_Id;
1344 begin
1345 if Name (N) = Error then
1346 return;
1347 end if;
1349 -- Check for Text_IO special unit (we may be renaming a Text_IO child)
1351 Check_Text_IO_Special_Unit (Name (N));
1353 if Current_Scope /= Standard_Standard then
1354 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1355 end if;
1357 Enter_Name (New_P);
1358 Analyze (Name (N));
1360 if Is_Entity_Name (Name (N)) then
1361 Old_P := Entity (Name (N));
1362 else
1363 Old_P := Any_Id;
1364 end if;
1366 if Etype (Old_P) = Any_Type then
1367 Error_Msg_N ("expect package name in renaming", Name (N));
1369 elsif Ekind (Old_P) /= E_Package
1370 and then not (Ekind (Old_P) = E_Generic_Package
1371 and then In_Open_Scopes (Old_P))
1372 then
1373 if Ekind (Old_P) = E_Generic_Package then
1374 Error_Msg_N
1375 ("generic package cannot be renamed as a package", Name (N));
1376 else
1377 Error_Msg_Sloc := Sloc (Old_P);
1378 Error_Msg_NE
1379 ("expect package name in renaming, found& declared#",
1380 Name (N), Old_P);
1381 end if;
1383 -- Set basic attributes to minimize cascaded errors
1385 Set_Ekind (New_P, E_Package);
1386 Set_Etype (New_P, Standard_Void_Type);
1388 -- Here for OK package renaming
1390 else
1391 -- Entities in the old package are accessible through the renaming
1392 -- entity. The simplest implementation is to have both packages share
1393 -- the entity list.
1395 Set_Ekind (New_P, E_Package);
1396 Set_Etype (New_P, Standard_Void_Type);
1398 if Present (Renamed_Object (Old_P)) then
1399 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1400 else
1401 Set_Renamed_Object (New_P, Old_P);
1402 end if;
1404 Set_Has_Completion (New_P);
1406 Set_First_Entity (New_P, First_Entity (Old_P));
1407 Set_Last_Entity (New_P, Last_Entity (Old_P));
1408 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1409 Check_Library_Unit_Renaming (N, Old_P);
1410 Generate_Reference (Old_P, Name (N));
1412 -- If the renaming is in the visible part of a package, then we set
1413 -- Renamed_In_Spec for the renamed package, to prevent giving
1414 -- warnings about no entities referenced. Such a warning would be
1415 -- overenthusiastic, since clients can see entities in the renamed
1416 -- package via the visible package renaming.
1418 declare
1419 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1420 begin
1421 if Ekind (Ent) = E_Package
1422 and then not In_Private_Part (Ent)
1423 and then In_Extended_Main_Source_Unit (N)
1424 and then Ekind (Old_P) = E_Package
1425 then
1426 Set_Renamed_In_Spec (Old_P);
1427 end if;
1428 end;
1430 -- If this is the renaming declaration of a package instantiation
1431 -- within itself, it is the declaration that ends the list of actuals
1432 -- for the instantiation. At this point, the subtypes that rename
1433 -- the actuals are flagged as generic, to avoid spurious ambiguities
1434 -- if the actuals for two distinct formals happen to coincide. If
1435 -- the actual is a private type, the subtype has a private completion
1436 -- that is flagged in the same fashion.
1438 -- Resolution is identical to what is was in the original generic.
1439 -- On exit from the generic instance, these are turned into regular
1440 -- subtypes again, so they are compatible with types in their class.
1442 if not Is_Generic_Instance (Old_P) then
1443 return;
1444 else
1445 Spec := Specification (Unit_Declaration_Node (Old_P));
1446 end if;
1448 if Nkind (Spec) = N_Package_Specification
1449 and then Present (Generic_Parent (Spec))
1450 and then Old_P = Current_Scope
1451 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1452 then
1453 declare
1454 E : Entity_Id;
1456 begin
1457 E := First_Entity (Old_P);
1458 while Present (E) and then E /= New_P loop
1459 if Is_Type (E)
1460 and then Nkind (Parent (E)) = N_Subtype_Declaration
1461 then
1462 Set_Is_Generic_Actual_Type (E);
1464 if Is_Private_Type (E)
1465 and then Present (Full_View (E))
1466 then
1467 Set_Is_Generic_Actual_Type (Full_View (E));
1468 end if;
1469 end if;
1471 Next_Entity (E);
1472 end loop;
1473 end;
1474 end if;
1475 end if;
1477 -- Implementation-defined aspect specifications can appear in a renaming
1478 -- declaration, but not language-defined ones. The call to procedure
1479 -- Analyze_Aspect_Specifications will take care of this error check.
1481 if Has_Aspects (N) then
1482 Analyze_Aspect_Specifications (N, New_P);
1483 end if;
1484 end Analyze_Package_Renaming;
1486 -------------------------------
1487 -- Analyze_Renamed_Character --
1488 -------------------------------
1490 procedure Analyze_Renamed_Character
1491 (N : Node_Id;
1492 New_S : Entity_Id;
1493 Is_Body : Boolean)
1495 C : constant Node_Id := Name (N);
1497 begin
1498 if Ekind (New_S) = E_Function then
1499 Resolve (C, Etype (New_S));
1501 if Is_Body then
1502 Check_Frozen_Renaming (N, New_S);
1503 end if;
1505 else
1506 Error_Msg_N ("character literal can only be renamed as function", N);
1507 end if;
1508 end Analyze_Renamed_Character;
1510 ---------------------------------
1511 -- Analyze_Renamed_Dereference --
1512 ---------------------------------
1514 procedure Analyze_Renamed_Dereference
1515 (N : Node_Id;
1516 New_S : Entity_Id;
1517 Is_Body : Boolean)
1519 Nam : constant Node_Id := Name (N);
1520 P : constant Node_Id := Prefix (Nam);
1521 Typ : Entity_Id;
1522 Ind : Interp_Index;
1523 It : Interp;
1525 begin
1526 if not Is_Overloaded (P) then
1527 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1528 or else not Type_Conformant (Etype (Nam), New_S)
1529 then
1530 Error_Msg_N ("designated type does not match specification", P);
1531 else
1532 Resolve (P);
1533 end if;
1535 return;
1537 else
1538 Typ := Any_Type;
1539 Get_First_Interp (Nam, Ind, It);
1541 while Present (It.Nam) loop
1543 if Ekind (It.Nam) = E_Subprogram_Type
1544 and then Type_Conformant (It.Nam, New_S)
1545 then
1546 if Typ /= Any_Id then
1547 Error_Msg_N ("ambiguous renaming", P);
1548 return;
1549 else
1550 Typ := It.Nam;
1551 end if;
1552 end if;
1554 Get_Next_Interp (Ind, It);
1555 end loop;
1557 if Typ = Any_Type then
1558 Error_Msg_N ("designated type does not match specification", P);
1559 else
1560 Resolve (N, Typ);
1562 if Is_Body then
1563 Check_Frozen_Renaming (N, New_S);
1564 end if;
1565 end if;
1566 end if;
1567 end Analyze_Renamed_Dereference;
1569 ---------------------------
1570 -- Analyze_Renamed_Entry --
1571 ---------------------------
1573 procedure Analyze_Renamed_Entry
1574 (N : Node_Id;
1575 New_S : Entity_Id;
1576 Is_Body : Boolean)
1578 Nam : constant Node_Id := Name (N);
1579 Sel : constant Node_Id := Selector_Name (Nam);
1580 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1581 Old_S : Entity_Id;
1583 begin
1584 if Entity (Sel) = Any_Id then
1586 -- Selector is undefined on prefix. Error emitted already
1588 Set_Has_Completion (New_S);
1589 return;
1590 end if;
1592 -- Otherwise find renamed entity and build body of New_S as a call to it
1594 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1596 if Old_S = Any_Id then
1597 Error_Msg_N (" no subprogram or entry matches specification", N);
1598 else
1599 if Is_Body then
1600 Check_Subtype_Conformant (New_S, Old_S, N);
1601 Generate_Reference (New_S, Defining_Entity (N), 'b');
1602 Style.Check_Identifier (Defining_Entity (N), New_S);
1604 else
1605 -- Only mode conformance required for a renaming_as_declaration
1607 Check_Mode_Conformant (New_S, Old_S, N);
1608 end if;
1610 Inherit_Renamed_Profile (New_S, Old_S);
1612 -- The prefix can be an arbitrary expression that yields a task or
1613 -- protected object, so it must be resolved.
1615 Resolve (Prefix (Nam), Scope (Old_S));
1616 end if;
1618 Set_Convention (New_S, Convention (Old_S));
1619 Set_Has_Completion (New_S, Inside_A_Generic);
1621 -- AI05-0225: If the renamed entity is a procedure or entry of a
1622 -- protected object, the target object must be a variable.
1624 if Ekind (Scope (Old_S)) in Protected_Kind
1625 and then Ekind (New_S) = E_Procedure
1626 and then not Is_Variable (Prefix (Nam))
1627 then
1628 if Is_Actual then
1629 Error_Msg_N
1630 ("target object of protected operation used as actual for "
1631 & "formal procedure must be a variable", Nam);
1632 else
1633 Error_Msg_N
1634 ("target object of protected operation renamed as procedure, "
1635 & "must be a variable", Nam);
1636 end if;
1637 end if;
1639 if Is_Body then
1640 Check_Frozen_Renaming (N, New_S);
1641 end if;
1642 end Analyze_Renamed_Entry;
1644 -----------------------------------
1645 -- Analyze_Renamed_Family_Member --
1646 -----------------------------------
1648 procedure Analyze_Renamed_Family_Member
1649 (N : Node_Id;
1650 New_S : Entity_Id;
1651 Is_Body : Boolean)
1653 Nam : constant Node_Id := Name (N);
1654 P : constant Node_Id := Prefix (Nam);
1655 Old_S : Entity_Id;
1657 begin
1658 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1659 or else (Nkind (P) = N_Selected_Component
1660 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1661 then
1662 if Is_Entity_Name (P) then
1663 Old_S := Entity (P);
1664 else
1665 Old_S := Entity (Selector_Name (P));
1666 end if;
1668 if not Entity_Matches_Spec (Old_S, New_S) then
1669 Error_Msg_N ("entry family does not match specification", N);
1671 elsif Is_Body then
1672 Check_Subtype_Conformant (New_S, Old_S, N);
1673 Generate_Reference (New_S, Defining_Entity (N), 'b');
1674 Style.Check_Identifier (Defining_Entity (N), New_S);
1675 end if;
1677 else
1678 Error_Msg_N ("no entry family matches specification", N);
1679 end if;
1681 Set_Has_Completion (New_S, Inside_A_Generic);
1683 if Is_Body then
1684 Check_Frozen_Renaming (N, New_S);
1685 end if;
1686 end Analyze_Renamed_Family_Member;
1688 -----------------------------------------
1689 -- Analyze_Renamed_Primitive_Operation --
1690 -----------------------------------------
1692 procedure Analyze_Renamed_Primitive_Operation
1693 (N : Node_Id;
1694 New_S : Entity_Id;
1695 Is_Body : Boolean)
1697 Old_S : Entity_Id;
1699 function Conforms
1700 (Subp : Entity_Id;
1701 Ctyp : Conformance_Type) return Boolean;
1702 -- Verify that the signatures of the renamed entity and the new entity
1703 -- match. The first formal of the renamed entity is skipped because it
1704 -- is the target object in any subsequent call.
1706 --------------
1707 -- Conforms --
1708 --------------
1710 function Conforms
1711 (Subp : Entity_Id;
1712 Ctyp : Conformance_Type) return Boolean
1714 Old_F : Entity_Id;
1715 New_F : Entity_Id;
1717 begin
1718 if Ekind (Subp) /= Ekind (New_S) then
1719 return False;
1720 end if;
1722 Old_F := Next_Formal (First_Formal (Subp));
1723 New_F := First_Formal (New_S);
1724 while Present (Old_F) and then Present (New_F) loop
1725 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
1726 return False;
1727 end if;
1729 if Ctyp >= Mode_Conformant
1730 and then Ekind (Old_F) /= Ekind (New_F)
1731 then
1732 return False;
1733 end if;
1735 Next_Formal (New_F);
1736 Next_Formal (Old_F);
1737 end loop;
1739 return True;
1740 end Conforms;
1742 -- Start of processing for Analyze_Renamed_Primitive_Operation
1744 begin
1745 if not Is_Overloaded (Selector_Name (Name (N))) then
1746 Old_S := Entity (Selector_Name (Name (N)));
1748 if not Conforms (Old_S, Type_Conformant) then
1749 Old_S := Any_Id;
1750 end if;
1752 else
1753 -- Find the operation that matches the given signature
1755 declare
1756 It : Interp;
1757 Ind : Interp_Index;
1759 begin
1760 Old_S := Any_Id;
1761 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
1763 while Present (It.Nam) loop
1764 if Conforms (It.Nam, Type_Conformant) then
1765 Old_S := It.Nam;
1766 end if;
1768 Get_Next_Interp (Ind, It);
1769 end loop;
1770 end;
1771 end if;
1773 if Old_S = Any_Id then
1774 Error_Msg_N (" no subprogram or entry matches specification", N);
1776 else
1777 if Is_Body then
1778 if not Conforms (Old_S, Subtype_Conformant) then
1779 Error_Msg_N ("subtype conformance error in renaming", N);
1780 end if;
1782 Generate_Reference (New_S, Defining_Entity (N), 'b');
1783 Style.Check_Identifier (Defining_Entity (N), New_S);
1785 else
1786 -- Only mode conformance required for a renaming_as_declaration
1788 if not Conforms (Old_S, Mode_Conformant) then
1789 Error_Msg_N ("mode conformance error in renaming", N);
1790 end if;
1792 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
1793 -- view of a subprogram is intrinsic, because the compiler has
1794 -- to generate a wrapper for any call to it. If the name in a
1795 -- subprogram renaming is a prefixed view, the entity is thus
1796 -- intrinsic, and 'Access cannot be applied to it.
1798 Set_Convention (New_S, Convention_Intrinsic);
1799 end if;
1801 -- Inherit_Renamed_Profile (New_S, Old_S);
1803 -- The prefix can be an arbitrary expression that yields an
1804 -- object, so it must be resolved.
1806 Resolve (Prefix (Name (N)));
1807 end if;
1808 end Analyze_Renamed_Primitive_Operation;
1810 ---------------------------------
1811 -- Analyze_Subprogram_Renaming --
1812 ---------------------------------
1814 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1815 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
1816 Is_Actual : constant Boolean := Present (Formal_Spec);
1817 Nam : constant Node_Id := Name (N);
1818 Save_AV : constant Ada_Version_Type := Ada_Version;
1819 Save_AVP : constant Node_Id := Ada_Version_Pragma;
1820 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
1821 Spec : constant Node_Id := Specification (N);
1823 Old_S : Entity_Id := Empty;
1824 Rename_Spec : Entity_Id;
1826 procedure Build_Class_Wide_Wrapper
1827 (Ren_Id : out Entity_Id;
1828 Wrap_Id : out Entity_Id);
1829 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal
1830 -- type with unknown discriminants and a generic primitive operation of
1831 -- the said type with a box require special processing when the actual
1832 -- is a class-wide type:
1834 -- generic
1835 -- type Formal_Typ (<>) is private;
1836 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
1837 -- package Gen is ...
1839 -- package Inst is new Gen (Actual_Typ'Class);
1841 -- In this case the general renaming mechanism used in the prologue of
1842 -- an instance no longer applies:
1844 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
1846 -- The above is replaced the following wrapper/renaming combination:
1848 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
1849 -- begin
1850 -- Prim_Op (Param); -- primitive
1851 -- end Wrapper;
1853 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
1855 -- This transformation applies only if there is no explicit visible
1856 -- class-wide operation at the point of the instantiation. Ren_Id is
1857 -- the entity of the renaming declaration. Wrap_Id is the entity of
1858 -- the generated class-wide wrapper (or Any_Id).
1860 procedure Check_Null_Exclusion
1861 (Ren : Entity_Id;
1862 Sub : Entity_Id);
1863 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
1864 -- following AI rules:
1866 -- If Ren is a renaming of a formal subprogram and one of its
1867 -- parameters has a null exclusion, then the corresponding formal
1868 -- in Sub must also have one. Otherwise the subtype of the Sub's
1869 -- formal parameter must exclude null.
1871 -- If Ren is a renaming of a formal function and its return
1872 -- profile has a null exclusion, then Sub's return profile must
1873 -- have one. Otherwise the subtype of Sub's return profile must
1874 -- exclude null.
1876 procedure Freeze_Actual_Profile;
1877 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
1878 -- types: a callable entity freezes its profile, unless it has an
1879 -- incomplete untagged formal (RM 13.14(10.2/3)).
1881 function Has_Class_Wide_Actual return Boolean;
1882 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
1883 -- defaulted formal subprogram where the actual for the controlling
1884 -- formal type is class-wide.
1886 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
1887 -- Find renamed entity when the declaration is a renaming_as_body and
1888 -- the renamed entity may itself be a renaming_as_body. Used to enforce
1889 -- rule that a renaming_as_body is illegal if the declaration occurs
1890 -- before the subprogram it completes is frozen, and renaming indirectly
1891 -- renames the subprogram itself.(Defect Report 8652/0027).
1893 ------------------------------
1894 -- Build_Class_Wide_Wrapper --
1895 ------------------------------
1897 procedure Build_Class_Wide_Wrapper
1898 (Ren_Id : out Entity_Id;
1899 Wrap_Id : out Entity_Id)
1901 Loc : constant Source_Ptr := Sloc (N);
1903 function Build_Call
1904 (Subp_Id : Entity_Id;
1905 Params : List_Id) return Node_Id;
1906 -- Create a dispatching call to invoke routine Subp_Id with actuals
1907 -- built from the parameter specifications of list Params.
1909 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
1910 -- Create a subprogram specification based on the subprogram profile
1911 -- of Subp_Id.
1913 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
1914 -- Find a primitive subprogram of type Typ which matches the profile
1915 -- of the renaming declaration.
1917 procedure Interpretation_Error (Subp_Id : Entity_Id);
1918 -- Emit a continuation error message suggesting subprogram Subp_Id as
1919 -- a possible interpretation.
1921 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean;
1922 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
1923 -- operator.
1925 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean;
1926 -- Determine whether subprogram Subp_Id is a suitable candidate for
1927 -- the role of a wrapped subprogram.
1929 ----------------
1930 -- Build_Call --
1931 ----------------
1933 function Build_Call
1934 (Subp_Id : Entity_Id;
1935 Params : List_Id) return Node_Id
1937 Actuals : constant List_Id := New_List;
1938 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
1939 Formal : Node_Id;
1941 begin
1942 -- Build the actual parameters of the call
1944 Formal := First (Params);
1945 while Present (Formal) loop
1946 Append_To (Actuals,
1947 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
1948 Next (Formal);
1949 end loop;
1951 -- Generate:
1952 -- return Subp_Id (Actuals);
1954 if Ekind_In (Subp_Id, E_Function, E_Operator) then
1955 return
1956 Make_Simple_Return_Statement (Loc,
1957 Expression =>
1958 Make_Function_Call (Loc,
1959 Name => Call_Ref,
1960 Parameter_Associations => Actuals));
1962 -- Generate:
1963 -- Subp_Id (Actuals);
1965 else
1966 return
1967 Make_Procedure_Call_Statement (Loc,
1968 Name => Call_Ref,
1969 Parameter_Associations => Actuals);
1970 end if;
1971 end Build_Call;
1973 ----------------
1974 -- Build_Spec --
1975 ----------------
1977 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
1978 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
1979 Spec_Id : constant Entity_Id :=
1980 Make_Defining_Identifier (Loc,
1981 Chars => New_External_Name (Chars (Subp_Id), 'R'));
1983 begin
1984 if Ekind (Formal_Spec) = E_Procedure then
1985 return
1986 Make_Procedure_Specification (Loc,
1987 Defining_Unit_Name => Spec_Id,
1988 Parameter_Specifications => Params);
1989 else
1990 return
1991 Make_Function_Specification (Loc,
1992 Defining_Unit_Name => Spec_Id,
1993 Parameter_Specifications => Params,
1994 Result_Definition =>
1995 New_Copy_Tree (Result_Definition (Spec)));
1996 end if;
1997 end Build_Spec;
1999 --------------------
2000 -- Find_Primitive --
2001 --------------------
2003 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2004 procedure Replace_Parameter_Types (Spec : Node_Id);
2005 -- Given a specification Spec, replace all class-wide parameter
2006 -- types with reference to type Typ.
2008 -----------------------------
2009 -- Replace_Parameter_Types --
2010 -----------------------------
2012 procedure Replace_Parameter_Types (Spec : Node_Id) is
2013 Formal : Node_Id;
2014 Formal_Id : Entity_Id;
2015 Formal_Typ : Node_Id;
2017 begin
2018 Formal := First (Parameter_Specifications (Spec));
2019 while Present (Formal) loop
2020 Formal_Id := Defining_Identifier (Formal);
2021 Formal_Typ := Parameter_Type (Formal);
2023 -- Create a new entity for each class-wide formal to prevent
2024 -- aliasing with the original renaming. Replace the type of
2025 -- such a parameter with the candidate type.
2027 if Nkind (Formal_Typ) = N_Identifier
2028 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2029 then
2030 Set_Defining_Identifier (Formal,
2031 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2033 Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc));
2034 end if;
2036 Next (Formal);
2037 end loop;
2038 end Replace_Parameter_Types;
2040 -- Local variables
2042 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2043 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2044 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2045 Subp_Id : Entity_Id;
2047 -- Start of processing for Find_Primitive
2049 begin
2050 -- Each attempt to find a suitable primitive of a particular type
2051 -- operates on its own copy of the original renaming. As a result
2052 -- the original renaming is kept decoration and side-effect free.
2054 -- Inherit the overloaded status of the renamed subprogram name
2056 if Is_Overloaded (Nam) then
2057 Set_Is_Overloaded (Alt_Nam);
2058 Save_Interps (Nam, Alt_Nam);
2059 end if;
2061 -- The copied renaming is hidden from visibility to prevent the
2062 -- pollution of the enclosing context.
2064 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2066 -- The types of all class-wide parameters must be changed to the
2067 -- candidate type.
2069 Replace_Parameter_Types (Alt_Spec);
2071 -- Try to find a suitable primitive which matches the altered
2072 -- profile of the renaming specification.
2074 Subp_Id :=
2075 Find_Renamed_Entity
2076 (N => Alt_Ren,
2077 Nam => Name (Alt_Ren),
2078 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2079 Is_Actual => Is_Actual);
2081 -- Do not return Any_Id if the resolion of the altered profile
2082 -- failed as this complicates further checks on the caller side,
2083 -- return Empty instead.
2085 if Subp_Id = Any_Id then
2086 return Empty;
2087 else
2088 return Subp_Id;
2089 end if;
2090 end Find_Primitive;
2092 --------------------------
2093 -- Interpretation_Error --
2094 --------------------------
2096 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2097 begin
2098 Error_Msg_Sloc := Sloc (Subp_Id);
2100 if Is_Internal (Subp_Id) then
2101 Error_Msg_NE
2102 ("\\possible interpretation: predefined & #",
2103 Spec, Formal_Spec);
2104 else
2105 Error_Msg_NE
2106 ("\\possible interpretation: & defined #", Spec, Formal_Spec);
2107 end if;
2108 end Interpretation_Error;
2110 ---------------------------
2111 -- Is_Intrinsic_Equality --
2112 ---------------------------
2114 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is
2115 begin
2116 return
2117 Ekind (Subp_Id) = E_Operator
2118 and then Chars (Subp_Id) = Name_Op_Eq
2119 and then Is_Intrinsic_Subprogram (Subp_Id);
2120 end Is_Intrinsic_Equality;
2122 ---------------------------
2123 -- Is_Suitable_Candidate --
2124 ---------------------------
2126 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is
2127 begin
2128 if No (Subp_Id) then
2129 return False;
2131 -- An intrinsic subprogram is never a good candidate. This is an
2132 -- indication of a missing primitive, either defined directly or
2133 -- inherited from a parent tagged type.
2135 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2136 return False;
2138 else
2139 return True;
2140 end if;
2141 end Is_Suitable_Candidate;
2143 -- Local variables
2145 Actual_Typ : Entity_Id := Empty;
2146 -- The actual class-wide type for Formal_Typ
2148 CW_Prim_OK : Boolean;
2149 CW_Prim_Op : Entity_Id;
2150 -- The class-wide subprogram (if available) which corresponds to the
2151 -- renamed generic formal subprogram.
2153 Formal_Typ : Entity_Id := Empty;
2154 -- The generic formal type with unknown discriminants
2156 Root_Prim_OK : Boolean;
2157 Root_Prim_Op : Entity_Id;
2158 -- The root type primitive (if available) which corresponds to the
2159 -- renamed generic formal subprogram.
2161 Root_Typ : Entity_Id := Empty;
2162 -- The root type of Actual_Typ
2164 Body_Decl : Node_Id;
2165 Formal : Node_Id;
2166 Prim_Op : Entity_Id;
2167 Spec_Decl : Node_Id;
2169 -- Start of processing for Build_Class_Wide_Wrapper
2171 begin
2172 -- Analyze the specification of the renaming in case the generation
2173 -- of the class-wide wrapper fails.
2175 Ren_Id := Analyze_Subprogram_Specification (Spec);
2176 Wrap_Id := Any_Id;
2178 -- Do not attempt to build a wrapper if the renaming is in error
2180 if Error_Posted (Nam) then
2181 return;
2182 end if;
2184 -- Analyze the renamed name, but do not resolve it. The resolution is
2185 -- completed once a suitable subprogram is found.
2187 Analyze (Nam);
2189 -- When the renamed name denotes the intrinsic operator equals, the
2190 -- name must be treated as overloaded. This allows for a potential
2191 -- match against the root type's predefined equality function.
2193 if Is_Intrinsic_Equality (Entity (Nam)) then
2194 Set_Is_Overloaded (Nam);
2195 Collect_Interps (Nam);
2196 end if;
2198 -- Step 1: Find the generic formal type with unknown discriminants
2199 -- and its corresponding class-wide actual type from the renamed
2200 -- generic formal subprogram.
2202 Formal := First_Formal (Formal_Spec);
2203 while Present (Formal) loop
2204 if Has_Unknown_Discriminants (Etype (Formal))
2205 and then not Is_Class_Wide_Type (Etype (Formal))
2206 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2207 then
2208 Formal_Typ := Etype (Formal);
2209 Actual_Typ := Get_Instance_Of (Formal_Typ);
2210 Root_Typ := Etype (Actual_Typ);
2211 exit;
2212 end if;
2214 Next_Formal (Formal);
2215 end loop;
2217 -- The specification of the generic formal subprogram should always
2218 -- contain a formal type with unknown discriminants whose actual is
2219 -- a class-wide type, otherwise this indicates a failure in routine
2220 -- Has_Class_Wide_Actual.
2222 pragma Assert (Present (Formal_Typ));
2224 -- Step 2: Find the proper class-wide subprogram or primitive which
2225 -- corresponds to the renamed generic formal subprogram.
2227 CW_Prim_Op := Find_Primitive (Actual_Typ);
2228 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2229 Root_Prim_Op := Find_Primitive (Root_Typ);
2230 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2232 -- The class-wide actual type has two subprograms which correspond to
2233 -- the renamed generic formal subprogram:
2235 -- with procedure Prim_Op (Param : Formal_Typ);
2237 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2238 -- procedure Prim_Op (Param : Actual_Typ'Class);
2240 -- Even though the declaration of the two subprograms is legal, a
2241 -- call to either one is ambiguous and therefore illegal.
2243 if CW_Prim_OK and Root_Prim_OK then
2245 -- A user-defined primitive has precedence over a predefined one
2247 if Is_Internal (CW_Prim_Op)
2248 and then not Is_Internal (Root_Prim_Op)
2249 then
2250 Prim_Op := Root_Prim_Op;
2252 elsif Is_Internal (Root_Prim_Op)
2253 and then not Is_Internal (CW_Prim_Op)
2254 then
2255 Prim_Op := CW_Prim_Op;
2257 elsif CW_Prim_Op = Root_Prim_Op then
2258 Prim_Op := Root_Prim_Op;
2260 -- Otherwise both candidate subprograms are user-defined and
2261 -- ambiguous.
2263 else
2264 Error_Msg_NE
2265 ("ambiguous actual for generic subprogram &",
2266 Spec, Formal_Spec);
2267 Interpretation_Error (Root_Prim_Op);
2268 Interpretation_Error (CW_Prim_Op);
2269 return;
2270 end if;
2272 elsif CW_Prim_OK and not Root_Prim_OK then
2273 Prim_Op := CW_Prim_Op;
2275 elsif not CW_Prim_OK and Root_Prim_OK then
2276 Prim_Op := Root_Prim_Op;
2278 -- An intrinsic equality may act as a suitable candidate in the case
2279 -- of a null type extension where the parent's equality is hidden. A
2280 -- call to an intrinsic equality is expanded as dispatching.
2282 elsif Present (Root_Prim_Op)
2283 and then Is_Intrinsic_Equality (Root_Prim_Op)
2284 then
2285 Prim_Op := Root_Prim_Op;
2287 -- Otherwise there are no candidate subprograms. Let the caller
2288 -- diagnose the error.
2290 else
2291 return;
2292 end if;
2294 -- At this point resolution has taken place and the name is no longer
2295 -- overloaded. Mark the primitive as referenced.
2297 Set_Is_Overloaded (Name (N), False);
2298 Set_Referenced (Prim_Op);
2300 -- Step 3: Create the declaration and the body of the wrapper, insert
2301 -- all the pieces into the tree.
2303 Spec_Decl :=
2304 Make_Subprogram_Declaration (Loc,
2305 Specification => Build_Spec (Ren_Id));
2306 Insert_Before_And_Analyze (N, Spec_Decl);
2308 -- If the operator carries an Eliminated pragma, indicate that the
2309 -- wrapper is also to be eliminated, to prevent spurious error when
2310 -- using gnatelim on programs that include box-initialization of
2311 -- equality operators.
2313 Wrap_Id := Defining_Entity (Spec_Decl);
2314 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2316 Body_Decl :=
2317 Make_Subprogram_Body (Loc,
2318 Specification => Build_Spec (Ren_Id),
2319 Declarations => New_List,
2320 Handled_Statement_Sequence =>
2321 Make_Handled_Sequence_Of_Statements (Loc,
2322 Statements => New_List (
2323 Build_Call
2324 (Subp_Id => Prim_Op,
2325 Params =>
2326 Parameter_Specifications
2327 (Specification (Spec_Decl))))));
2329 -- The generated body does not freeze and must be analyzed when the
2330 -- class-wide wrapper is frozen. The body is only needed if expansion
2331 -- is enabled.
2333 if Expander_Active then
2334 Append_Freeze_Action (Wrap_Id, Body_Decl);
2335 end if;
2337 -- Step 4: The subprogram renaming aliases the wrapper
2339 Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc));
2340 end Build_Class_Wide_Wrapper;
2342 --------------------------
2343 -- Check_Null_Exclusion --
2344 --------------------------
2346 procedure Check_Null_Exclusion
2347 (Ren : Entity_Id;
2348 Sub : Entity_Id)
2350 Ren_Formal : Entity_Id;
2351 Sub_Formal : Entity_Id;
2353 begin
2354 -- Parameter check
2356 Ren_Formal := First_Formal (Ren);
2357 Sub_Formal := First_Formal (Sub);
2358 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2359 if Has_Null_Exclusion (Parent (Ren_Formal))
2360 and then
2361 not (Has_Null_Exclusion (Parent (Sub_Formal))
2362 or else Can_Never_Be_Null (Etype (Sub_Formal)))
2363 then
2364 Error_Msg_NE
2365 ("`NOT NULL` required for parameter &",
2366 Parent (Sub_Formal), Sub_Formal);
2367 end if;
2369 Next_Formal (Ren_Formal);
2370 Next_Formal (Sub_Formal);
2371 end loop;
2373 -- Return profile check
2375 if Nkind (Parent (Ren)) = N_Function_Specification
2376 and then Nkind (Parent (Sub)) = N_Function_Specification
2377 and then Has_Null_Exclusion (Parent (Ren))
2378 and then not (Has_Null_Exclusion (Parent (Sub))
2379 or else Can_Never_Be_Null (Etype (Sub)))
2380 then
2381 Error_Msg_N
2382 ("return must specify `NOT NULL`",
2383 Result_Definition (Parent (Sub)));
2384 end if;
2385 end Check_Null_Exclusion;
2387 ---------------------------
2388 -- Freeze_Actual_Profile --
2389 ---------------------------
2391 procedure Freeze_Actual_Profile is
2392 F : Entity_Id;
2393 Has_Untagged_Inc : Boolean;
2394 Instantiation_Node : constant Node_Id := Parent (N);
2396 begin
2397 if Ada_Version >= Ada_2012 then
2398 F := First_Formal (Formal_Spec);
2399 Has_Untagged_Inc := False;
2400 while Present (F) loop
2401 if Ekind (Etype (F)) = E_Incomplete_Type
2402 and then not Is_Tagged_Type (Etype (F))
2403 then
2404 Has_Untagged_Inc := True;
2405 exit;
2406 end if;
2408 F := Next_Formal (F);
2409 end loop;
2411 if Ekind (Formal_Spec) = E_Function
2412 and then Ekind (Etype (Formal_Spec)) = E_Incomplete_Type
2413 and then not Is_Tagged_Type (Etype (F))
2414 then
2415 Has_Untagged_Inc := True;
2416 end if;
2418 if not Has_Untagged_Inc then
2419 F := First_Formal (Old_S);
2420 while Present (F) loop
2421 Freeze_Before (Instantiation_Node, Etype (F));
2423 if Is_Incomplete_Or_Private_Type (Etype (F))
2424 and then No (Underlying_Type (Etype (F)))
2425 then
2426 -- Exclude generic types, or types derived from them.
2427 -- They will be frozen in the enclosing instance.
2429 if Is_Generic_Type (Etype (F))
2430 or else Is_Generic_Type (Root_Type (Etype (F)))
2431 then
2432 null;
2433 else
2434 Error_Msg_NE
2435 ("type& must be frozen before this point",
2436 Instantiation_Node, Etype (F));
2437 end if;
2438 end if;
2440 F := Next_Formal (F);
2441 end loop;
2442 end if;
2443 end if;
2444 end Freeze_Actual_Profile;
2446 ---------------------------
2447 -- Has_Class_Wide_Actual --
2448 ---------------------------
2450 function Has_Class_Wide_Actual return Boolean is
2451 Formal : Entity_Id;
2452 Formal_Typ : Entity_Id;
2454 begin
2455 if Is_Actual then
2456 Formal := First_Formal (Formal_Spec);
2457 while Present (Formal) loop
2458 Formal_Typ := Etype (Formal);
2460 if Has_Unknown_Discriminants (Formal_Typ)
2461 and then not Is_Class_Wide_Type (Formal_Typ)
2462 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2463 then
2464 return True;
2465 end if;
2467 Next_Formal (Formal);
2468 end loop;
2469 end if;
2471 return False;
2472 end Has_Class_Wide_Actual;
2474 -------------------------
2475 -- Original_Subprogram --
2476 -------------------------
2478 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2479 Orig_Decl : Node_Id;
2480 Orig_Subp : Entity_Id;
2482 begin
2483 -- First case: renamed entity is itself a renaming
2485 if Present (Alias (Subp)) then
2486 return Alias (Subp);
2488 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2489 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2490 then
2491 -- Check if renamed entity is a renaming_as_body
2493 Orig_Decl :=
2494 Unit_Declaration_Node
2495 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2497 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2498 Orig_Subp := Entity (Name (Orig_Decl));
2500 if Orig_Subp = Rename_Spec then
2502 -- Circularity detected
2504 return Orig_Subp;
2506 else
2507 return (Original_Subprogram (Orig_Subp));
2508 end if;
2509 else
2510 return Subp;
2511 end if;
2512 else
2513 return Subp;
2514 end if;
2515 end Original_Subprogram;
2517 -- Local variables
2519 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2520 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2521 -- defaulted formal subprogram when the actual for a related formal
2522 -- type is class-wide.
2524 Inst_Node : Node_Id := Empty;
2525 New_S : Entity_Id;
2527 -- Start of processing for Analyze_Subprogram_Renaming
2529 begin
2530 -- We must test for the attribute renaming case before the Analyze
2531 -- call because otherwise Sem_Attr will complain that the attribute
2532 -- is missing an argument when it is analyzed.
2534 if Nkind (Nam) = N_Attribute_Reference then
2536 -- In the case of an abstract formal subprogram association, rewrite
2537 -- an actual given by a stream attribute as the name of the
2538 -- corresponding stream primitive of the type.
2540 -- In a generic context the stream operations are not generated, and
2541 -- this must be treated as a normal attribute reference, to be
2542 -- expanded in subsequent instantiations.
2544 if Is_Actual
2545 and then Is_Abstract_Subprogram (Formal_Spec)
2546 and then Expander_Active
2547 then
2548 declare
2549 Stream_Prim : Entity_Id;
2550 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
2552 begin
2553 -- The class-wide forms of the stream attributes are not
2554 -- primitive dispatching operations (even though they
2555 -- internally dispatch to a stream attribute).
2557 if Is_Class_Wide_Type (Prefix_Type) then
2558 Error_Msg_N
2559 ("attribute must be a primitive dispatching operation",
2560 Nam);
2561 return;
2562 end if;
2564 -- Retrieve the primitive subprogram associated with the
2565 -- attribute. This can only be a stream attribute, since those
2566 -- are the only ones that are dispatching (and the actual for
2567 -- an abstract formal subprogram must be dispatching
2568 -- operation).
2570 begin
2571 case Attribute_Name (Nam) is
2572 when Name_Input =>
2573 Stream_Prim :=
2574 Find_Prim_Op (Prefix_Type, TSS_Stream_Input);
2575 when Name_Output =>
2576 Stream_Prim :=
2577 Find_Prim_Op (Prefix_Type, TSS_Stream_Output);
2578 when Name_Read =>
2579 Stream_Prim :=
2580 Find_Prim_Op (Prefix_Type, TSS_Stream_Read);
2581 when Name_Write =>
2582 Stream_Prim :=
2583 Find_Prim_Op (Prefix_Type, TSS_Stream_Write);
2584 when others =>
2585 Error_Msg_N
2586 ("attribute must be a primitive"
2587 & " dispatching operation", Nam);
2588 return;
2589 end case;
2591 exception
2593 -- If no operation was found, and the type is limited,
2594 -- the user should have defined one.
2596 when Program_Error =>
2597 if Is_Limited_Type (Prefix_Type) then
2598 Error_Msg_NE
2599 ("stream operation not defined for type&",
2600 N, Prefix_Type);
2601 return;
2603 -- Otherwise, compiler should have generated default
2605 else
2606 raise;
2607 end if;
2608 end;
2610 -- Rewrite the attribute into the name of its corresponding
2611 -- primitive dispatching subprogram. We can then proceed with
2612 -- the usual processing for subprogram renamings.
2614 declare
2615 Prim_Name : constant Node_Id :=
2616 Make_Identifier (Sloc (Nam),
2617 Chars => Chars (Stream_Prim));
2618 begin
2619 Set_Entity (Prim_Name, Stream_Prim);
2620 Rewrite (Nam, Prim_Name);
2621 Analyze (Nam);
2622 end;
2623 end;
2625 -- Normal processing for a renaming of an attribute
2627 else
2628 Attribute_Renaming (N);
2629 return;
2630 end if;
2631 end if;
2633 -- Check whether this declaration corresponds to the instantiation
2634 -- of a formal subprogram.
2636 -- If this is an instantiation, the corresponding actual is frozen and
2637 -- error messages can be made more precise. If this is a default
2638 -- subprogram, the entity is already established in the generic, and is
2639 -- not retrieved by visibility. If it is a default with a box, the
2640 -- candidate interpretations, if any, have been collected when building
2641 -- the renaming declaration. If overloaded, the proper interpretation is
2642 -- determined in Find_Renamed_Entity. If the entity is an operator,
2643 -- Find_Renamed_Entity applies additional visibility checks.
2645 if Is_Actual then
2646 Inst_Node := Unit_Declaration_Node (Formal_Spec);
2648 -- Check whether the renaming is for a defaulted actual subprogram
2649 -- with a class-wide actual.
2651 if CW_Actual and then Box_Present (Inst_Node) then
2652 Build_Class_Wide_Wrapper (New_S, Old_S);
2654 elsif Is_Entity_Name (Nam)
2655 and then Present (Entity (Nam))
2656 and then not Comes_From_Source (Nam)
2657 and then not Is_Overloaded (Nam)
2658 then
2659 Old_S := Entity (Nam);
2660 New_S := Analyze_Subprogram_Specification (Spec);
2662 -- Operator case
2664 if Ekind (Entity (Nam)) = E_Operator then
2666 -- Box present
2668 if Box_Present (Inst_Node) then
2669 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2671 -- If there is an immediately visible homonym of the operator
2672 -- and the declaration has a default, this is worth a warning
2673 -- because the user probably did not intend to get the pre-
2674 -- defined operator, visible in the generic declaration. To
2675 -- find if there is an intended candidate, analyze the renaming
2676 -- again in the current context.
2678 elsif Scope (Old_S) = Standard_Standard
2679 and then Present (Default_Name (Inst_Node))
2680 then
2681 declare
2682 Decl : constant Node_Id := New_Copy_Tree (N);
2683 Hidden : Entity_Id;
2685 begin
2686 Set_Entity (Name (Decl), Empty);
2687 Analyze (Name (Decl));
2688 Hidden :=
2689 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
2691 if Present (Hidden)
2692 and then In_Open_Scopes (Scope (Hidden))
2693 and then Is_Immediately_Visible (Hidden)
2694 and then Comes_From_Source (Hidden)
2695 and then Hidden /= Old_S
2696 then
2697 Error_Msg_Sloc := Sloc (Hidden);
2698 Error_Msg_N ("default subprogram is resolved " &
2699 "in the generic declaration " &
2700 "(RM 12.6(17))??", N);
2701 Error_Msg_NE ("\and will not use & #??", N, Hidden);
2702 end if;
2703 end;
2704 end if;
2705 end if;
2707 else
2708 Analyze (Nam);
2709 New_S := Analyze_Subprogram_Specification (Spec);
2710 end if;
2712 else
2713 -- Renamed entity must be analyzed first, to avoid being hidden by
2714 -- new name (which might be the same in a generic instance).
2716 Analyze (Nam);
2718 -- The renaming defines a new overloaded entity, which is analyzed
2719 -- like a subprogram declaration.
2721 New_S := Analyze_Subprogram_Specification (Spec);
2722 end if;
2724 if Current_Scope /= Standard_Standard then
2725 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
2726 end if;
2728 -- Set SPARK mode from current context
2730 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
2731 Set_SPARK_Pragma_Inherited (New_S, True);
2733 Rename_Spec := Find_Corresponding_Spec (N);
2735 -- Case of Renaming_As_Body
2737 if Present (Rename_Spec) then
2739 -- Renaming declaration is the completion of the declaration of
2740 -- Rename_Spec. We build an actual body for it at the freezing point.
2742 Set_Corresponding_Spec (N, Rename_Spec);
2744 -- Deal with special case of stream functions of abstract types
2745 -- and interfaces.
2747 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
2748 N_Abstract_Subprogram_Declaration
2749 then
2750 -- Input stream functions are abstract if the object type is
2751 -- abstract. Similarly, all default stream functions for an
2752 -- interface type are abstract. However, these subprograms may
2753 -- receive explicit declarations in representation clauses, making
2754 -- the attribute subprograms usable as defaults in subsequent
2755 -- type extensions.
2756 -- In this case we rewrite the declaration to make the subprogram
2757 -- non-abstract. We remove the previous declaration, and insert
2758 -- the new one at the point of the renaming, to prevent premature
2759 -- access to unfrozen types. The new declaration reuses the
2760 -- specification of the previous one, and must not be analyzed.
2762 pragma Assert
2763 (Is_Primitive (Entity (Nam))
2764 and then
2765 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
2766 declare
2767 Old_Decl : constant Node_Id :=
2768 Unit_Declaration_Node (Rename_Spec);
2769 New_Decl : constant Node_Id :=
2770 Make_Subprogram_Declaration (Sloc (N),
2771 Specification =>
2772 Relocate_Node (Specification (Old_Decl)));
2773 begin
2774 Remove (Old_Decl);
2775 Insert_After (N, New_Decl);
2776 Set_Is_Abstract_Subprogram (Rename_Spec, False);
2777 Set_Analyzed (New_Decl);
2778 end;
2779 end if;
2781 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
2783 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2784 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
2785 end if;
2787 Set_Convention (New_S, Convention (Rename_Spec));
2788 Check_Fully_Conformant (New_S, Rename_Spec);
2789 Set_Public_Status (New_S);
2791 -- The specification does not introduce new formals, but only
2792 -- repeats the formals of the original subprogram declaration.
2793 -- For cross-reference purposes, and for refactoring tools, we
2794 -- treat the formals of the renaming declaration as body formals.
2796 Reference_Body_Formals (Rename_Spec, New_S);
2798 -- Indicate that the entity in the declaration functions like the
2799 -- corresponding body, and is not a new entity. The body will be
2800 -- constructed later at the freeze point, so indicate that the
2801 -- completion has not been seen yet.
2803 Set_Contract (New_S, Empty);
2804 Set_Ekind (New_S, E_Subprogram_Body);
2805 New_S := Rename_Spec;
2806 Set_Has_Completion (Rename_Spec, False);
2808 -- Ada 2005: check overriding indicator
2810 if Present (Overridden_Operation (Rename_Spec)) then
2811 if Must_Not_Override (Specification (N)) then
2812 Error_Msg_NE
2813 ("subprogram& overrides inherited operation",
2814 N, Rename_Spec);
2815 elsif
2816 Style_Check and then not Must_Override (Specification (N))
2817 then
2818 Style.Missing_Overriding (N, Rename_Spec);
2819 end if;
2821 elsif Must_Override (Specification (N)) then
2822 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
2823 end if;
2825 -- Normal subprogram renaming (not renaming as body)
2827 else
2828 Generate_Definition (New_S);
2829 New_Overloaded_Entity (New_S);
2831 if Is_Entity_Name (Nam)
2832 and then Is_Intrinsic_Subprogram (Entity (Nam))
2833 then
2834 null;
2835 else
2836 Check_Delayed_Subprogram (New_S);
2837 end if;
2838 end if;
2840 -- There is no need for elaboration checks on the new entity, which may
2841 -- be called before the next freezing point where the body will appear.
2842 -- Elaboration checks refer to the real entity, not the one created by
2843 -- the renaming declaration.
2845 Set_Kill_Elaboration_Checks (New_S, True);
2847 -- If we had a previous error, indicate a completely is present to stop
2848 -- junk cascaded messages, but don't take any further action.
2850 if Etype (Nam) = Any_Type then
2851 Set_Has_Completion (New_S);
2852 return;
2854 -- Case where name has the form of a selected component
2856 elsif Nkind (Nam) = N_Selected_Component then
2858 -- A name which has the form A.B can designate an entry of task A, a
2859 -- protected operation of protected object A, or finally a primitive
2860 -- operation of object A. In the later case, A is an object of some
2861 -- tagged type, or an access type that denotes one such. To further
2862 -- distinguish these cases, note that the scope of a task entry or
2863 -- protected operation is type of the prefix.
2865 -- The prefix could be an overloaded function call that returns both
2866 -- kinds of operations. This overloading pathology is left to the
2867 -- dedicated reader ???
2869 declare
2870 T : constant Entity_Id := Etype (Prefix (Nam));
2872 begin
2873 if Present (T)
2874 and then
2875 (Is_Tagged_Type (T)
2876 or else
2877 (Is_Access_Type (T)
2878 and then Is_Tagged_Type (Designated_Type (T))))
2879 and then Scope (Entity (Selector_Name (Nam))) /= T
2880 then
2881 Analyze_Renamed_Primitive_Operation
2882 (N, New_S, Present (Rename_Spec));
2883 return;
2885 else
2886 -- Renamed entity is an entry or protected operation. For those
2887 -- cases an explicit body is built (at the point of freezing of
2888 -- this entity) that contains a call to the renamed entity.
2890 -- This is not allowed for renaming as body if the renamed
2891 -- spec is already frozen (see RM 8.5.4(5) for details).
2893 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
2894 Error_Msg_N
2895 ("renaming-as-body cannot rename entry as subprogram", N);
2896 Error_Msg_NE
2897 ("\since & is already frozen (RM 8.5.4(5))",
2898 N, Rename_Spec);
2899 else
2900 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
2901 end if;
2903 return;
2904 end if;
2905 end;
2907 -- Case where name is an explicit dereference X.all
2909 elsif Nkind (Nam) = N_Explicit_Dereference then
2911 -- Renamed entity is designated by access_to_subprogram expression.
2912 -- Must build body to encapsulate call, as in the entry case.
2914 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
2915 return;
2917 -- Indexed component
2919 elsif Nkind (Nam) = N_Indexed_Component then
2920 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
2921 return;
2923 -- Character literal
2925 elsif Nkind (Nam) = N_Character_Literal then
2926 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
2927 return;
2929 -- Only remaining case is where we have a non-entity name, or a renaming
2930 -- of some other non-overloadable entity.
2932 elsif not Is_Entity_Name (Nam)
2933 or else not Is_Overloadable (Entity (Nam))
2934 then
2935 -- Do not mention the renaming if it comes from an instance
2937 if not Is_Actual then
2938 Error_Msg_N ("expect valid subprogram name in renaming", N);
2939 else
2940 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
2941 end if;
2943 return;
2944 end if;
2946 -- Find the renamed entity that matches the given specification. Disable
2947 -- Ada_83 because there is no requirement of full conformance between
2948 -- renamed entity and new entity, even though the same circuit is used.
2950 -- This is a bit of an odd case, which introduces a really irregular use
2951 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
2952 -- this. ???
2954 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
2955 Ada_Version_Pragma := Empty;
2956 Ada_Version_Explicit := Ada_Version;
2958 if No (Old_S) then
2959 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2961 -- The visible operation may be an inherited abstract operation that
2962 -- was overridden in the private part, in which case a call will
2963 -- dispatch to the overriding operation. Use the overriding one in
2964 -- the renaming declaration, to prevent spurious errors below.
2966 if Is_Overloadable (Old_S)
2967 and then Is_Abstract_Subprogram (Old_S)
2968 and then No (DTC_Entity (Old_S))
2969 and then Present (Alias (Old_S))
2970 and then not Is_Abstract_Subprogram (Alias (Old_S))
2971 and then Present (Overridden_Operation (Alias (Old_S)))
2972 then
2973 Old_S := Alias (Old_S);
2974 end if;
2976 -- When the renamed subprogram is overloaded and used as an actual
2977 -- of a generic, its entity is set to the first available homonym.
2978 -- We must first disambiguate the name, then set the proper entity.
2980 if Is_Actual and then Is_Overloaded (Nam) then
2981 Set_Entity (Nam, Old_S);
2982 end if;
2983 end if;
2985 -- Most common case: subprogram renames subprogram. No body is generated
2986 -- in this case, so we must indicate the declaration is complete as is.
2987 -- and inherit various attributes of the renamed subprogram.
2989 if No (Rename_Spec) then
2990 Set_Has_Completion (New_S);
2991 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
2992 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
2993 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
2995 -- Ada 2005 (AI-423): Check the consistency of null exclusions
2996 -- between a subprogram and its correct renaming.
2998 -- Note: the Any_Id check is a guard that prevents compiler crashes
2999 -- when performing a null exclusion check between a renaming and a
3000 -- renamed subprogram that has been found to be illegal.
3002 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3003 Check_Null_Exclusion
3004 (Ren => New_S,
3005 Sub => Entity (Nam));
3006 end if;
3008 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3009 -- overriding. The flag Requires_Overriding is set very selectively
3010 -- and misses some other illegal cases. The additional conditions
3011 -- checked below are sufficient but not necessary ???
3013 -- The rule does not apply to the renaming generated for an actual
3014 -- subprogram in an instance.
3016 if Is_Actual then
3017 null;
3019 -- Guard against previous errors, and omit renamings of predefined
3020 -- operators.
3022 elsif not Ekind_In (Old_S, E_Function, E_Procedure) then
3023 null;
3025 elsif Requires_Overriding (Old_S)
3026 or else
3027 (Is_Abstract_Subprogram (Old_S)
3028 and then Present (Find_Dispatching_Type (Old_S))
3029 and then
3030 not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3031 then
3032 Error_Msg_N
3033 ("renamed entity cannot be "
3034 & "subprogram that requires overriding (RM 8.5.4 (5.1))", N);
3035 end if;
3036 end if;
3038 if Old_S /= Any_Id then
3039 if Is_Actual and then From_Default (N) then
3041 -- This is an implicit reference to the default actual
3043 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3045 else
3046 Generate_Reference (Old_S, Nam);
3047 end if;
3049 Check_Internal_Protected_Use (N, Old_S);
3051 -- For a renaming-as-body, require subtype conformance, but if the
3052 -- declaration being completed has not been frozen, then inherit the
3053 -- convention of the renamed subprogram prior to checking conformance
3054 -- (unless the renaming has an explicit convention established; the
3055 -- rule stated in the RM doesn't seem to address this ???).
3057 if Present (Rename_Spec) then
3058 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3059 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3061 if not Is_Frozen (Rename_Spec) then
3062 if not Has_Convention_Pragma (Rename_Spec) then
3063 Set_Convention (New_S, Convention (Old_S));
3064 end if;
3066 if Ekind (Old_S) /= E_Operator then
3067 Check_Mode_Conformant (New_S, Old_S, Spec);
3068 end if;
3070 if Original_Subprogram (Old_S) = Rename_Spec then
3071 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
3072 end if;
3073 else
3074 Check_Subtype_Conformant (New_S, Old_S, Spec);
3075 end if;
3077 Check_Frozen_Renaming (N, Rename_Spec);
3079 -- Check explicitly that renamed entity is not intrinsic, because
3080 -- in a generic the renamed body is not built. In this case,
3081 -- the renaming_as_body is a completion.
3083 if Inside_A_Generic then
3084 if Is_Frozen (Rename_Spec)
3085 and then Is_Intrinsic_Subprogram (Old_S)
3086 then
3087 Error_Msg_N
3088 ("subprogram in renaming_as_body cannot be intrinsic",
3089 Name (N));
3090 end if;
3092 Set_Has_Completion (Rename_Spec);
3093 end if;
3095 elsif Ekind (Old_S) /= E_Operator then
3097 -- If this a defaulted subprogram for a class-wide actual there is
3098 -- no check for mode conformance, given that the signatures don't
3099 -- match (the source mentions T but the actual mentions T'Class).
3101 if CW_Actual then
3102 null;
3103 elsif not Is_Actual or else No (Enclosing_Instance) then
3104 Check_Mode_Conformant (New_S, Old_S);
3105 end if;
3107 if Is_Actual and then Error_Posted (New_S) then
3108 Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
3109 end if;
3110 end if;
3112 if No (Rename_Spec) then
3114 -- The parameter profile of the new entity is that of the renamed
3115 -- entity: the subtypes given in the specification are irrelevant.
3117 Inherit_Renamed_Profile (New_S, Old_S);
3119 -- A call to the subprogram is transformed into a call to the
3120 -- renamed entity. This is transitive if the renamed entity is
3121 -- itself a renaming.
3123 if Present (Alias (Old_S)) then
3124 Set_Alias (New_S, Alias (Old_S));
3125 else
3126 Set_Alias (New_S, Old_S);
3127 end if;
3129 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3130 -- renaming as body, since the entity in this case is not an
3131 -- intrinsic (it calls an intrinsic, but we have a real body for
3132 -- this call, and it is in this body that the required intrinsic
3133 -- processing will take place).
3135 -- Also, if this is a renaming of inequality, the renamed operator
3136 -- is intrinsic, but what matters is the corresponding equality
3137 -- operator, which may be user-defined.
3139 Set_Is_Intrinsic_Subprogram
3140 (New_S,
3141 Is_Intrinsic_Subprogram (Old_S)
3142 and then
3143 (Chars (Old_S) /= Name_Op_Ne
3144 or else Ekind (Old_S) = E_Operator
3145 or else Is_Intrinsic_Subprogram
3146 (Corresponding_Equality (Old_S))));
3148 if Ekind (Alias (New_S)) = E_Operator then
3149 Set_Has_Delayed_Freeze (New_S, False);
3150 end if;
3152 -- If the renaming corresponds to an association for an abstract
3153 -- formal subprogram, then various attributes must be set to
3154 -- indicate that the renaming is an abstract dispatching operation
3155 -- with a controlling type.
3157 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
3159 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3160 -- see it as corresponding to a generic association for a
3161 -- formal abstract subprogram
3163 Set_Is_Abstract_Subprogram (New_S);
3165 declare
3166 New_S_Ctrl_Type : constant Entity_Id :=
3167 Find_Dispatching_Type (New_S);
3168 Old_S_Ctrl_Type : constant Entity_Id :=
3169 Find_Dispatching_Type (Old_S);
3171 begin
3172 if Old_S_Ctrl_Type /= New_S_Ctrl_Type then
3173 Error_Msg_NE
3174 ("actual must be dispatching subprogram for type&",
3175 Nam, New_S_Ctrl_Type);
3177 else
3178 Set_Is_Dispatching_Operation (New_S);
3179 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3181 -- If the actual in the formal subprogram is itself a
3182 -- formal abstract subprogram association, there's no
3183 -- dispatch table component or position to inherit.
3185 if Present (DTC_Entity (Old_S)) then
3186 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3187 Set_DT_Position (New_S, DT_Position (Old_S));
3188 end if;
3189 end if;
3190 end;
3191 end if;
3192 end if;
3194 if Is_Actual then
3195 null;
3197 -- The following is illegal, because F hides whatever other F may
3198 -- be around:
3199 -- function F (...) renames F;
3201 elsif Old_S = New_S
3202 or else (Nkind (Nam) /= N_Expanded_Name
3203 and then Chars (Old_S) = Chars (New_S))
3204 then
3205 Error_Msg_N ("subprogram cannot rename itself", N);
3207 -- This is illegal even if we use a selector:
3208 -- function F (...) renames Pkg.F;
3209 -- because F is still hidden.
3211 elsif Nkind (Nam) = N_Expanded_Name
3212 and then Entity (Prefix (Nam)) = Current_Scope
3213 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3214 then
3215 -- This is an error, but we overlook the error and accept the
3216 -- renaming if the special Overriding_Renamings mode is in effect.
3218 if not Overriding_Renamings then
3219 Error_Msg_NE
3220 ("implicit operation& is not visible (RM 8.3 (15))",
3221 Nam, Old_S);
3222 end if;
3223 end if;
3225 Set_Convention (New_S, Convention (Old_S));
3227 if Is_Abstract_Subprogram (Old_S) then
3228 if Present (Rename_Spec) then
3229 Error_Msg_N
3230 ("a renaming-as-body cannot rename an abstract subprogram",
3232 Set_Has_Completion (Rename_Spec);
3233 else
3234 Set_Is_Abstract_Subprogram (New_S);
3235 end if;
3236 end if;
3238 Check_Library_Unit_Renaming (N, Old_S);
3240 -- Pathological case: procedure renames entry in the scope of its
3241 -- task. Entry is given by simple name, but body must be built for
3242 -- procedure. Of course if called it will deadlock.
3244 if Ekind (Old_S) = E_Entry then
3245 Set_Has_Completion (New_S, False);
3246 Set_Alias (New_S, Empty);
3247 end if;
3249 if Is_Actual then
3250 Freeze_Before (N, Old_S);
3251 Freeze_Actual_Profile;
3252 Set_Has_Delayed_Freeze (New_S, False);
3253 Freeze_Before (N, New_S);
3255 -- An abstract subprogram is only allowed as an actual in the case
3256 -- where the formal subprogram is also abstract.
3258 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
3259 and then Is_Abstract_Subprogram (Old_S)
3260 and then not Is_Abstract_Subprogram (Formal_Spec)
3261 then
3262 Error_Msg_N
3263 ("abstract subprogram not allowed as generic actual", Nam);
3264 end if;
3265 end if;
3267 else
3268 -- A common error is to assume that implicit operators for types are
3269 -- defined in Standard, or in the scope of a subtype. In those cases
3270 -- where the renamed entity is given with an expanded name, it is
3271 -- worth mentioning that operators for the type are not declared in
3272 -- the scope given by the prefix.
3274 if Nkind (Nam) = N_Expanded_Name
3275 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
3276 and then Scope (Entity (Nam)) = Standard_Standard
3277 then
3278 declare
3279 T : constant Entity_Id :=
3280 Base_Type (Etype (First_Formal (New_S)));
3281 begin
3282 Error_Msg_Node_2 := Prefix (Nam);
3283 Error_Msg_NE
3284 ("operator for type& is not declared in&", Prefix (Nam), T);
3285 end;
3287 else
3288 Error_Msg_NE
3289 ("no visible subprogram matches the specification for&",
3290 Spec, New_S);
3291 end if;
3293 if Present (Candidate_Renaming) then
3294 declare
3295 F1 : Entity_Id;
3296 F2 : Entity_Id;
3297 T1 : Entity_Id;
3299 begin
3300 F1 := First_Formal (Candidate_Renaming);
3301 F2 := First_Formal (New_S);
3302 T1 := First_Subtype (Etype (F1));
3303 while Present (F1) and then Present (F2) loop
3304 Next_Formal (F1);
3305 Next_Formal (F2);
3306 end loop;
3308 if Present (F1) and then Present (Default_Value (F1)) then
3309 if Present (Next_Formal (F1)) then
3310 Error_Msg_NE
3311 ("\missing specification for &" &
3312 " and other formals with defaults", Spec, F1);
3313 else
3314 Error_Msg_NE
3315 ("\missing specification for &", Spec, F1);
3316 end if;
3317 end if;
3319 if Nkind (Nam) = N_Operator_Symbol
3320 and then From_Default (N)
3321 then
3322 Error_Msg_Node_2 := T1;
3323 Error_Msg_NE
3324 ("default & on & is not directly visible",
3325 Nam, Nam);
3326 end if;
3327 end;
3328 end if;
3329 end if;
3331 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
3332 -- controlling access parameters are known non-null for the renamed
3333 -- subprogram. Test also applies to a subprogram instantiation that
3334 -- is dispatching. Test is skipped if some previous error was detected
3335 -- that set Old_S to Any_Id.
3337 if Ada_Version >= Ada_2005
3338 and then Old_S /= Any_Id
3339 and then not Is_Dispatching_Operation (Old_S)
3340 and then Is_Dispatching_Operation (New_S)
3341 then
3342 declare
3343 Old_F : Entity_Id;
3344 New_F : Entity_Id;
3346 begin
3347 Old_F := First_Formal (Old_S);
3348 New_F := First_Formal (New_S);
3349 while Present (Old_F) loop
3350 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
3351 and then Is_Controlling_Formal (New_F)
3352 and then not Can_Never_Be_Null (Old_F)
3353 then
3354 Error_Msg_N ("access parameter is controlling,", New_F);
3355 Error_Msg_NE
3356 ("\corresponding parameter of& "
3357 & "must be explicitly null excluding", New_F, Old_S);
3358 end if;
3360 Next_Formal (Old_F);
3361 Next_Formal (New_F);
3362 end loop;
3363 end;
3364 end if;
3366 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
3367 -- is to warn if an operator is being renamed as a different operator.
3368 -- If the operator is predefined, examine the kind of the entity, not
3369 -- the abbreviated declaration in Standard.
3371 if Comes_From_Source (N)
3372 and then Present (Old_S)
3373 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3374 or else Ekind (Old_S) = E_Operator)
3375 and then Nkind (New_S) = N_Defining_Operator_Symbol
3376 and then Chars (Old_S) /= Chars (New_S)
3377 then
3378 Error_Msg_NE
3379 ("& is being renamed as a different operator??", N, Old_S);
3380 end if;
3382 -- Check for renaming of obsolescent subprogram
3384 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3386 -- Another warning or some utility: if the new subprogram as the same
3387 -- name as the old one, the old one is not hidden by an outer homograph,
3388 -- the new one is not a public symbol, and the old one is otherwise
3389 -- directly visible, the renaming is superfluous.
3391 if Chars (Old_S) = Chars (New_S)
3392 and then Comes_From_Source (N)
3393 and then Scope (Old_S) /= Standard_Standard
3394 and then Warn_On_Redundant_Constructs
3395 and then (Is_Immediately_Visible (Old_S)
3396 or else Is_Potentially_Use_Visible (Old_S))
3397 and then Is_Overloadable (Current_Scope)
3398 and then Chars (Current_Scope) /= Chars (Old_S)
3399 then
3400 Error_Msg_N
3401 ("redundant renaming, entity is directly visible?r?", Name (N));
3402 end if;
3404 -- Implementation-defined aspect specifications can appear in a renaming
3405 -- declaration, but not language-defined ones. The call to procedure
3406 -- Analyze_Aspect_Specifications will take care of this error check.
3408 if Has_Aspects (N) then
3409 Analyze_Aspect_Specifications (N, New_S);
3410 end if;
3412 Ada_Version := Save_AV;
3413 Ada_Version_Pragma := Save_AVP;
3414 Ada_Version_Explicit := Save_AV_Exp;
3415 end Analyze_Subprogram_Renaming;
3417 -------------------------
3418 -- Analyze_Use_Package --
3419 -------------------------
3421 -- Resolve the package names in the use clause, and make all the visible
3422 -- entities defined in the package potentially use-visible. If the package
3423 -- is already in use from a previous use clause, its visible entities are
3424 -- already use-visible. In that case, mark the occurrence as a redundant
3425 -- use. If the package is an open scope, i.e. if the use clause occurs
3426 -- within the package itself, ignore it.
3428 procedure Analyze_Use_Package (N : Node_Id) is
3429 Pack_Name : Node_Id;
3430 Pack : Entity_Id;
3432 -- Start of processing for Analyze_Use_Package
3434 begin
3435 Check_SPARK_05_Restriction ("use clause is not allowed", N);
3437 Set_Hidden_By_Use_Clause (N, No_Elist);
3439 -- Use clause not allowed in a spec of a predefined package declaration
3440 -- except that packages whose file name starts a-n are OK (these are
3441 -- children of Ada.Numerics, which are never loaded by Rtsfind).
3443 if Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
3444 and then Name_Buffer (1 .. 3) /= "a-n"
3445 and then
3446 Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3447 then
3448 Error_Msg_N ("use clause not allowed in predefined spec", N);
3449 end if;
3451 -- Chain clause to list of use clauses in current scope
3453 if Nkind (Parent (N)) /= N_Compilation_Unit then
3454 Chain_Use_Clause (N);
3455 end if;
3457 -- Loop through package names to identify referenced packages
3459 Pack_Name := First (Names (N));
3460 while Present (Pack_Name) loop
3461 Analyze (Pack_Name);
3463 if Nkind (Parent (N)) = N_Compilation_Unit
3464 and then Nkind (Pack_Name) = N_Expanded_Name
3465 then
3466 declare
3467 Pref : Node_Id;
3469 begin
3470 Pref := Prefix (Pack_Name);
3471 while Nkind (Pref) = N_Expanded_Name loop
3472 Pref := Prefix (Pref);
3473 end loop;
3475 if Entity (Pref) = Standard_Standard then
3476 Error_Msg_N
3477 ("predefined package Standard cannot appear"
3478 & " in a context clause", Pref);
3479 end if;
3480 end;
3481 end if;
3483 Next (Pack_Name);
3484 end loop;
3486 -- Loop through package names to mark all entities as potentially
3487 -- use visible.
3489 Pack_Name := First (Names (N));
3490 while Present (Pack_Name) loop
3491 if Is_Entity_Name (Pack_Name) then
3492 Pack := Entity (Pack_Name);
3494 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
3495 if Ekind (Pack) = E_Generic_Package then
3496 Error_Msg_N -- CODEFIX
3497 ("a generic package is not allowed in a use clause",
3498 Pack_Name);
3499 else
3500 Error_Msg_N ("& is not a usable package", Pack_Name);
3501 end if;
3503 else
3504 if Nkind (Parent (N)) = N_Compilation_Unit then
3505 Check_In_Previous_With_Clause (N, Pack_Name);
3506 end if;
3508 if Applicable_Use (Pack_Name) then
3509 Use_One_Package (Pack, N);
3510 end if;
3511 end if;
3513 -- Report error because name denotes something other than a package
3515 else
3516 Error_Msg_N ("& is not a package", Pack_Name);
3517 end if;
3519 Next (Pack_Name);
3520 end loop;
3521 end Analyze_Use_Package;
3523 ----------------------
3524 -- Analyze_Use_Type --
3525 ----------------------
3527 procedure Analyze_Use_Type (N : Node_Id) is
3528 E : Entity_Id;
3529 Id : Node_Id;
3531 begin
3532 Set_Hidden_By_Use_Clause (N, No_Elist);
3534 -- Chain clause to list of use clauses in current scope
3536 if Nkind (Parent (N)) /= N_Compilation_Unit then
3537 Chain_Use_Clause (N);
3538 end if;
3540 -- If the Used_Operations list is already initialized, the clause has
3541 -- been analyzed previously, and it is begin reinstalled, for example
3542 -- when the clause appears in a package spec and we are compiling the
3543 -- corresponding package body. In that case, make the entities on the
3544 -- existing list use_visible, and mark the corresponding types In_Use.
3546 if Present (Used_Operations (N)) then
3547 declare
3548 Mark : Node_Id;
3549 Elmt : Elmt_Id;
3551 begin
3552 Mark := First (Subtype_Marks (N));
3553 while Present (Mark) loop
3554 Use_One_Type (Mark, Installed => True);
3555 Next (Mark);
3556 end loop;
3558 Elmt := First_Elmt (Used_Operations (N));
3559 while Present (Elmt) loop
3560 Set_Is_Potentially_Use_Visible (Node (Elmt));
3561 Next_Elmt (Elmt);
3562 end loop;
3563 end;
3565 return;
3566 end if;
3568 -- Otherwise, create new list and attach to it the operations that
3569 -- are made use-visible by the clause.
3571 Set_Used_Operations (N, New_Elmt_List);
3572 Id := First (Subtype_Marks (N));
3573 while Present (Id) loop
3574 Find_Type (Id);
3575 E := Entity (Id);
3577 if E /= Any_Type then
3578 Use_One_Type (Id);
3580 if Nkind (Parent (N)) = N_Compilation_Unit then
3581 if Nkind (Id) = N_Identifier then
3582 Error_Msg_N ("type is not directly visible", Id);
3584 elsif Is_Child_Unit (Scope (E))
3585 and then Scope (E) /= System_Aux_Id
3586 then
3587 Check_In_Previous_With_Clause (N, Prefix (Id));
3588 end if;
3589 end if;
3591 else
3592 -- If the use_type_clause appears in a compilation unit context,
3593 -- check whether it comes from a unit that may appear in a
3594 -- limited_with_clause, for a better error message.
3596 if Nkind (Parent (N)) = N_Compilation_Unit
3597 and then Nkind (Id) /= N_Identifier
3598 then
3599 declare
3600 Item : Node_Id;
3601 Pref : Node_Id;
3603 function Mentioned (Nam : Node_Id) return Boolean;
3604 -- Check whether the prefix of expanded name for the type
3605 -- appears in the prefix of some limited_with_clause.
3607 ---------------
3608 -- Mentioned --
3609 ---------------
3611 function Mentioned (Nam : Node_Id) return Boolean is
3612 begin
3613 return Nkind (Name (Item)) = N_Selected_Component
3614 and then Chars (Prefix (Name (Item))) = Chars (Nam);
3615 end Mentioned;
3617 begin
3618 Pref := Prefix (Id);
3619 Item := First (Context_Items (Parent (N)));
3620 while Present (Item) and then Item /= N loop
3621 if Nkind (Item) = N_With_Clause
3622 and then Limited_Present (Item)
3623 and then Mentioned (Pref)
3624 then
3625 Change_Error_Text
3626 (Get_Msg_Id, "premature usage of incomplete type");
3627 end if;
3629 Next (Item);
3630 end loop;
3631 end;
3632 end if;
3633 end if;
3635 Next (Id);
3636 end loop;
3637 end Analyze_Use_Type;
3639 --------------------
3640 -- Applicable_Use --
3641 --------------------
3643 function Applicable_Use (Pack_Name : Node_Id) return Boolean is
3644 Pack : constant Entity_Id := Entity (Pack_Name);
3646 begin
3647 if In_Open_Scopes (Pack) then
3648 if Warn_On_Redundant_Constructs and then Pack = Current_Scope then
3649 Error_Msg_NE -- CODEFIX
3650 ("& is already use-visible within itself?r?", Pack_Name, Pack);
3651 end if;
3653 return False;
3655 elsif In_Use (Pack) then
3656 Note_Redundant_Use (Pack_Name);
3657 return False;
3659 elsif Present (Renamed_Object (Pack))
3660 and then In_Use (Renamed_Object (Pack))
3661 then
3662 Note_Redundant_Use (Pack_Name);
3663 return False;
3665 else
3666 return True;
3667 end if;
3668 end Applicable_Use;
3670 ------------------------
3671 -- Attribute_Renaming --
3672 ------------------------
3674 procedure Attribute_Renaming (N : Node_Id) is
3675 Loc : constant Source_Ptr := Sloc (N);
3676 Nam : constant Node_Id := Name (N);
3677 Spec : constant Node_Id := Specification (N);
3678 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
3679 Aname : constant Name_Id := Attribute_Name (Nam);
3681 Form_Num : Nat := 0;
3682 Expr_List : List_Id := No_List;
3684 Attr_Node : Node_Id;
3685 Body_Node : Node_Id;
3686 Param_Spec : Node_Id;
3688 begin
3689 Generate_Definition (New_S);
3691 -- This procedure is called in the context of subprogram renaming, and
3692 -- thus the attribute must be one that is a subprogram. All of those
3693 -- have at least one formal parameter, with the exceptions of the GNAT
3694 -- attribute 'Img, which GNAT treats as renameable.
3696 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
3697 if Aname /= Name_Img then
3698 Error_Msg_N
3699 ("subprogram renaming an attribute must have formals", N);
3700 return;
3701 end if;
3703 else
3704 Param_Spec := First (Parameter_Specifications (Spec));
3705 while Present (Param_Spec) loop
3706 Form_Num := Form_Num + 1;
3708 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
3709 Find_Type (Parameter_Type (Param_Spec));
3711 -- The profile of the new entity denotes the base type (s) of
3712 -- the types given in the specification. For access parameters
3713 -- there are no subtypes involved.
3715 Rewrite (Parameter_Type (Param_Spec),
3716 New_Occurrence_Of
3717 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
3718 end if;
3720 if No (Expr_List) then
3721 Expr_List := New_List;
3722 end if;
3724 Append_To (Expr_List,
3725 Make_Identifier (Loc,
3726 Chars => Chars (Defining_Identifier (Param_Spec))));
3728 -- The expressions in the attribute reference are not freeze
3729 -- points. Neither is the attribute as a whole, see below.
3731 Set_Must_Not_Freeze (Last (Expr_List));
3732 Next (Param_Spec);
3733 end loop;
3734 end if;
3736 -- Immediate error if too many formals. Other mismatches in number or
3737 -- types of parameters are detected when we analyze the body of the
3738 -- subprogram that we construct.
3740 if Form_Num > 2 then
3741 Error_Msg_N ("too many formals for attribute", N);
3743 -- Error if the attribute reference has expressions that look like
3744 -- formal parameters.
3746 elsif Present (Expressions (Nam)) then
3747 Error_Msg_N ("illegal expressions in attribute reference", Nam);
3749 elsif
3750 Nam_In (Aname, Name_Compose, Name_Exponent, Name_Leading_Part,
3751 Name_Pos, Name_Round, Name_Scaling,
3752 Name_Val)
3753 then
3754 if Nkind (N) = N_Subprogram_Renaming_Declaration
3755 and then Present (Corresponding_Formal_Spec (N))
3756 then
3757 Error_Msg_N
3758 ("generic actual cannot be attribute involving universal type",
3759 Nam);
3760 else
3761 Error_Msg_N
3762 ("attribute involving a universal type cannot be renamed",
3763 Nam);
3764 end if;
3765 end if;
3767 -- Rewrite attribute node to have a list of expressions corresponding to
3768 -- the subprogram formals. A renaming declaration is not a freeze point,
3769 -- and the analysis of the attribute reference should not freeze the
3770 -- type of the prefix. We use the original node in the renaming so that
3771 -- its source location is preserved, and checks on stream attributes are
3772 -- properly applied.
3774 Attr_Node := Relocate_Node (Nam);
3775 Set_Expressions (Attr_Node, Expr_List);
3777 Set_Must_Not_Freeze (Attr_Node);
3778 Set_Must_Not_Freeze (Prefix (Nam));
3780 -- Case of renaming a function
3782 if Nkind (Spec) = N_Function_Specification then
3783 if Is_Procedure_Attribute_Name (Aname) then
3784 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
3785 return;
3786 end if;
3788 Find_Type (Result_Definition (Spec));
3789 Rewrite (Result_Definition (Spec),
3790 New_Occurrence_Of
3791 (Base_Type (Entity (Result_Definition (Spec))), Loc));
3793 Body_Node :=
3794 Make_Subprogram_Body (Loc,
3795 Specification => Spec,
3796 Declarations => New_List,
3797 Handled_Statement_Sequence =>
3798 Make_Handled_Sequence_Of_Statements (Loc,
3799 Statements => New_List (
3800 Make_Simple_Return_Statement (Loc,
3801 Expression => Attr_Node))));
3803 -- Case of renaming a procedure
3805 else
3806 if not Is_Procedure_Attribute_Name (Aname) then
3807 Error_Msg_N ("attribute can only be renamed as function", Nam);
3808 return;
3809 end if;
3811 Body_Node :=
3812 Make_Subprogram_Body (Loc,
3813 Specification => Spec,
3814 Declarations => New_List,
3815 Handled_Statement_Sequence =>
3816 Make_Handled_Sequence_Of_Statements (Loc,
3817 Statements => New_List (Attr_Node)));
3818 end if;
3820 -- In case of tagged types we add the body of the generated function to
3821 -- the freezing actions of the type (because in the general case such
3822 -- type is still not frozen). We exclude from this processing generic
3823 -- formal subprograms found in instantiations.
3825 -- We must exclude VM targets and restricted run-time libraries because
3826 -- entity AST_Handler is defined in package System.Aux_Dec which is not
3827 -- available in those platforms. Note that we cannot use the function
3828 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
3829 -- the ZFP run-time library is not defined as a profile, and we do not
3830 -- want to deal with AST_Handler in ZFP mode.
3832 if VM_Target = No_VM
3833 and then not Configurable_Run_Time_Mode
3834 and then not Present (Corresponding_Formal_Spec (N))
3835 and then Etype (Nam) /= RTE (RE_AST_Handler)
3836 then
3837 declare
3838 P : constant Node_Id := Prefix (Nam);
3840 begin
3841 -- The prefix of 'Img is an object that is evaluated for each call
3842 -- of the function that renames it.
3844 if Aname = Name_Img then
3845 Preanalyze_And_Resolve (P);
3847 -- For all other attribute renamings, the prefix is a subtype
3849 else
3850 Find_Type (P);
3851 end if;
3853 -- If the target type is not yet frozen, add the body to the
3854 -- actions to be elaborated at freeze time.
3856 if Is_Tagged_Type (Etype (P))
3857 and then In_Open_Scopes (Scope (Etype (P)))
3858 then
3859 Ensure_Freeze_Node (Etype (P));
3860 Append_Freeze_Action (Etype (P), Body_Node);
3861 else
3862 Rewrite (N, Body_Node);
3863 Analyze (N);
3864 Set_Etype (New_S, Base_Type (Etype (New_S)));
3865 end if;
3866 end;
3868 -- Generic formal subprograms or AST_Handler renaming
3870 else
3871 Rewrite (N, Body_Node);
3872 Analyze (N);
3873 Set_Etype (New_S, Base_Type (Etype (New_S)));
3874 end if;
3876 if Is_Compilation_Unit (New_S) then
3877 Error_Msg_N
3878 ("a library unit can only rename another library unit", N);
3879 end if;
3881 -- We suppress elaboration warnings for the resulting entity, since
3882 -- clearly they are not needed, and more particularly, in the case
3883 -- of a generic formal subprogram, the resulting entity can appear
3884 -- after the instantiation itself, and thus look like a bogus case
3885 -- of access before elaboration.
3887 Set_Suppress_Elaboration_Warnings (New_S);
3889 end Attribute_Renaming;
3891 ----------------------
3892 -- Chain_Use_Clause --
3893 ----------------------
3895 procedure Chain_Use_Clause (N : Node_Id) is
3896 Pack : Entity_Id;
3897 Level : Int := Scope_Stack.Last;
3899 begin
3900 if not Is_Compilation_Unit (Current_Scope)
3901 or else not Is_Child_Unit (Current_Scope)
3902 then
3903 null; -- Common case
3905 elsif Defining_Entity (Parent (N)) = Current_Scope then
3906 null; -- Common case for compilation unit
3908 else
3909 -- If declaration appears in some other scope, it must be in some
3910 -- parent unit when compiling a child.
3912 Pack := Defining_Entity (Parent (N));
3913 if not In_Open_Scopes (Pack) then
3914 null; -- default as well
3916 else
3917 -- Find entry for parent unit in scope stack
3919 while Scope_Stack.Table (Level).Entity /= Pack loop
3920 Level := Level - 1;
3921 end loop;
3922 end if;
3923 end if;
3925 Set_Next_Use_Clause (N,
3926 Scope_Stack.Table (Level).First_Use_Clause);
3927 Scope_Stack.Table (Level).First_Use_Clause := N;
3928 end Chain_Use_Clause;
3930 ---------------------------
3931 -- Check_Frozen_Renaming --
3932 ---------------------------
3934 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
3935 B_Node : Node_Id;
3936 Old_S : Entity_Id;
3938 begin
3939 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
3940 B_Node :=
3941 Build_Renamed_Body
3942 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
3944 if Is_Entity_Name (Name (N)) then
3945 Old_S := Entity (Name (N));
3947 if not Is_Frozen (Old_S)
3948 and then Operating_Mode /= Check_Semantics
3949 then
3950 Append_Freeze_Action (Old_S, B_Node);
3951 else
3952 Insert_After (N, B_Node);
3953 Analyze (B_Node);
3954 end if;
3956 if Is_Intrinsic_Subprogram (Old_S) and then not In_Instance then
3957 Error_Msg_N
3958 ("subprogram used in renaming_as_body cannot be intrinsic",
3959 Name (N));
3960 end if;
3962 else
3963 Insert_After (N, B_Node);
3964 Analyze (B_Node);
3965 end if;
3966 end if;
3967 end Check_Frozen_Renaming;
3969 -------------------------------
3970 -- Set_Entity_Or_Discriminal --
3971 -------------------------------
3973 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
3974 P : Node_Id;
3976 begin
3977 -- If the entity is not a discriminant, or else expansion is disabled,
3978 -- simply set the entity.
3980 if not In_Spec_Expression
3981 or else Ekind (E) /= E_Discriminant
3982 or else Inside_A_Generic
3983 then
3984 Set_Entity_With_Checks (N, E);
3986 -- The replacement of a discriminant by the corresponding discriminal
3987 -- is not done for a task discriminant that appears in a default
3988 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
3989 -- for details on their handling.
3991 elsif Is_Concurrent_Type (Scope (E)) then
3992 P := Parent (N);
3993 while Present (P)
3994 and then not Nkind_In (P, N_Parameter_Specification,
3995 N_Component_Declaration)
3996 loop
3997 P := Parent (P);
3998 end loop;
4000 if Present (P)
4001 and then Nkind (P) = N_Parameter_Specification
4002 then
4003 null;
4005 else
4006 Set_Entity (N, Discriminal (E));
4007 end if;
4009 -- Otherwise, this is a discriminant in a context in which
4010 -- it is a reference to the corresponding parameter of the
4011 -- init proc for the enclosing type.
4013 else
4014 Set_Entity (N, Discriminal (E));
4015 end if;
4016 end Set_Entity_Or_Discriminal;
4018 -----------------------------------
4019 -- Check_In_Previous_With_Clause --
4020 -----------------------------------
4022 procedure Check_In_Previous_With_Clause
4023 (N : Node_Id;
4024 Nam : Entity_Id)
4026 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4027 Item : Node_Id;
4028 Par : Node_Id;
4030 begin
4031 Item := First (Context_Items (Parent (N)));
4032 while Present (Item) and then Item /= N loop
4033 if Nkind (Item) = N_With_Clause
4035 -- Protect the frontend against previous critical errors
4037 and then Nkind (Name (Item)) /= N_Selected_Component
4038 and then Entity (Name (Item)) = Pack
4039 then
4040 Par := Nam;
4042 -- Find root library unit in with_clause
4044 while Nkind (Par) = N_Expanded_Name loop
4045 Par := Prefix (Par);
4046 end loop;
4048 if Is_Child_Unit (Entity (Original_Node (Par))) then
4049 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4050 else
4051 return;
4052 end if;
4053 end if;
4055 Next (Item);
4056 end loop;
4058 -- On exit, package is not mentioned in a previous with_clause.
4059 -- Check if its prefix is.
4061 if Nkind (Nam) = N_Expanded_Name then
4062 Check_In_Previous_With_Clause (N, Prefix (Nam));
4064 elsif Pack /= Any_Id then
4065 Error_Msg_NE ("& is not visible", Nam, Pack);
4066 end if;
4067 end Check_In_Previous_With_Clause;
4069 ---------------------------------
4070 -- Check_Library_Unit_Renaming --
4071 ---------------------------------
4073 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4074 New_E : Entity_Id;
4076 begin
4077 if Nkind (Parent (N)) /= N_Compilation_Unit then
4078 return;
4080 -- Check for library unit. Note that we used to check for the scope
4081 -- being Standard here, but that was wrong for Standard itself.
4083 elsif not Is_Compilation_Unit (Old_E)
4084 and then not Is_Child_Unit (Old_E)
4085 then
4086 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4088 -- Entities defined in Standard (operators and boolean literals) cannot
4089 -- be renamed as library units.
4091 elsif Scope (Old_E) = Standard_Standard
4092 and then Sloc (Old_E) = Standard_Location
4093 then
4094 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4096 elsif Present (Parent_Spec (N))
4097 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
4098 and then not Is_Child_Unit (Old_E)
4099 then
4100 Error_Msg_N
4101 ("renamed unit must be a child unit of generic parent", Name (N));
4103 elsif Nkind (N) in N_Generic_Renaming_Declaration
4104 and then Nkind (Name (N)) = N_Expanded_Name
4105 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
4106 and then Is_Generic_Unit (Old_E)
4107 then
4108 Error_Msg_N
4109 ("renamed generic unit must be a library unit", Name (N));
4111 elsif Is_Package_Or_Generic_Package (Old_E) then
4113 -- Inherit categorization flags
4115 New_E := Defining_Entity (N);
4116 Set_Is_Pure (New_E, Is_Pure (Old_E));
4117 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
4118 Set_Is_Remote_Call_Interface (New_E,
4119 Is_Remote_Call_Interface (Old_E));
4120 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
4121 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
4122 end if;
4123 end Check_Library_Unit_Renaming;
4125 ------------------------
4126 -- Enclosing_Instance --
4127 ------------------------
4129 function Enclosing_Instance return Entity_Id is
4130 S : Entity_Id;
4132 begin
4133 if not Is_Generic_Instance (Current_Scope) then
4134 return Empty;
4135 end if;
4137 S := Scope (Current_Scope);
4138 while S /= Standard_Standard loop
4139 if Is_Generic_Instance (S) then
4140 return S;
4141 end if;
4143 S := Scope (S);
4144 end loop;
4146 return Empty;
4147 end Enclosing_Instance;
4149 ---------------
4150 -- End_Scope --
4151 ---------------
4153 procedure End_Scope is
4154 Id : Entity_Id;
4155 Prev : Entity_Id;
4156 Outer : Entity_Id;
4158 begin
4159 Id := First_Entity (Current_Scope);
4160 while Present (Id) loop
4161 -- An entity in the current scope is not necessarily the first one
4162 -- on its homonym chain. Find its predecessor if any,
4163 -- If it is an internal entity, it will not be in the visibility
4164 -- chain altogether, and there is nothing to unchain.
4166 if Id /= Current_Entity (Id) then
4167 Prev := Current_Entity (Id);
4168 while Present (Prev)
4169 and then Present (Homonym (Prev))
4170 and then Homonym (Prev) /= Id
4171 loop
4172 Prev := Homonym (Prev);
4173 end loop;
4175 -- Skip to end of loop if Id is not in the visibility chain
4177 if No (Prev) or else Homonym (Prev) /= Id then
4178 goto Next_Ent;
4179 end if;
4181 else
4182 Prev := Empty;
4183 end if;
4185 Set_Is_Immediately_Visible (Id, False);
4187 Outer := Homonym (Id);
4188 while Present (Outer) and then Scope (Outer) = Current_Scope loop
4189 Outer := Homonym (Outer);
4190 end loop;
4192 -- Reset homonym link of other entities, but do not modify link
4193 -- between entities in current scope, so that the back-end can have
4194 -- a proper count of local overloadings.
4196 if No (Prev) then
4197 Set_Name_Entity_Id (Chars (Id), Outer);
4199 elsif Scope (Prev) /= Scope (Id) then
4200 Set_Homonym (Prev, Outer);
4201 end if;
4203 <<Next_Ent>>
4204 Next_Entity (Id);
4205 end loop;
4207 -- If the scope generated freeze actions, place them before the
4208 -- current declaration and analyze them. Type declarations and
4209 -- the bodies of initialization procedures can generate such nodes.
4210 -- We follow the parent chain until we reach a list node, which is
4211 -- the enclosing list of declarations. If the list appears within
4212 -- a protected definition, move freeze nodes outside the protected
4213 -- type altogether.
4215 if Present
4216 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
4217 then
4218 declare
4219 Decl : Node_Id;
4220 L : constant List_Id := Scope_Stack.Table
4221 (Scope_Stack.Last).Pending_Freeze_Actions;
4223 begin
4224 if Is_Itype (Current_Scope) then
4225 Decl := Associated_Node_For_Itype (Current_Scope);
4226 else
4227 Decl := Parent (Current_Scope);
4228 end if;
4230 Pop_Scope;
4232 while not (Is_List_Member (Decl))
4233 or else Nkind_In (Parent (Decl), N_Protected_Definition,
4234 N_Task_Definition)
4235 loop
4236 Decl := Parent (Decl);
4237 end loop;
4239 Insert_List_Before_And_Analyze (Decl, L);
4240 end;
4242 else
4243 Pop_Scope;
4244 end if;
4245 end End_Scope;
4247 ---------------------
4248 -- End_Use_Clauses --
4249 ---------------------
4251 procedure End_Use_Clauses (Clause : Node_Id) is
4252 U : Node_Id;
4254 begin
4255 -- Remove Use_Type clauses first, because they affect the
4256 -- visibility of operators in subsequent used packages.
4258 U := Clause;
4259 while Present (U) loop
4260 if Nkind (U) = N_Use_Type_Clause then
4261 End_Use_Type (U);
4262 end if;
4264 Next_Use_Clause (U);
4265 end loop;
4267 U := Clause;
4268 while Present (U) loop
4269 if Nkind (U) = N_Use_Package_Clause then
4270 End_Use_Package (U);
4271 end if;
4273 Next_Use_Clause (U);
4274 end loop;
4275 end End_Use_Clauses;
4277 ---------------------
4278 -- End_Use_Package --
4279 ---------------------
4281 procedure End_Use_Package (N : Node_Id) is
4282 Pack_Name : Node_Id;
4283 Pack : Entity_Id;
4284 Id : Entity_Id;
4285 Elmt : Elmt_Id;
4287 function Is_Primitive_Operator_In_Use
4288 (Op : Entity_Id;
4289 F : Entity_Id) return Boolean;
4290 -- Check whether Op is a primitive operator of a use-visible type
4292 ----------------------------------
4293 -- Is_Primitive_Operator_In_Use --
4294 ----------------------------------
4296 function Is_Primitive_Operator_In_Use
4297 (Op : Entity_Id;
4298 F : Entity_Id) return Boolean
4300 T : constant Entity_Id := Base_Type (Etype (F));
4301 begin
4302 return In_Use (T) and then Scope (T) = Scope (Op);
4303 end Is_Primitive_Operator_In_Use;
4305 -- Start of processing for End_Use_Package
4307 begin
4308 Pack_Name := First (Names (N));
4309 while Present (Pack_Name) loop
4311 -- Test that Pack_Name actually denotes a package before processing
4313 if Is_Entity_Name (Pack_Name)
4314 and then Ekind (Entity (Pack_Name)) = E_Package
4315 then
4316 Pack := Entity (Pack_Name);
4318 if In_Open_Scopes (Pack) then
4319 null;
4321 elsif not Redundant_Use (Pack_Name) then
4322 Set_In_Use (Pack, False);
4323 Set_Current_Use_Clause (Pack, Empty);
4325 Id := First_Entity (Pack);
4326 while Present (Id) loop
4328 -- Preserve use-visibility of operators that are primitive
4329 -- operators of a type that is use-visible through an active
4330 -- use_type clause.
4332 if Nkind (Id) = N_Defining_Operator_Symbol
4333 and then
4334 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
4335 or else
4336 (Present (Next_Formal (First_Formal (Id)))
4337 and then
4338 Is_Primitive_Operator_In_Use
4339 (Id, Next_Formal (First_Formal (Id)))))
4340 then
4341 null;
4342 else
4343 Set_Is_Potentially_Use_Visible (Id, False);
4344 end if;
4346 if Is_Private_Type (Id)
4347 and then Present (Full_View (Id))
4348 then
4349 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4350 end if;
4352 Next_Entity (Id);
4353 end loop;
4355 if Present (Renamed_Object (Pack)) then
4356 Set_In_Use (Renamed_Object (Pack), False);
4357 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
4358 end if;
4360 if Chars (Pack) = Name_System
4361 and then Scope (Pack) = Standard_Standard
4362 and then Present_System_Aux
4363 then
4364 Id := First_Entity (System_Aux_Id);
4365 while Present (Id) loop
4366 Set_Is_Potentially_Use_Visible (Id, False);
4368 if Is_Private_Type (Id)
4369 and then Present (Full_View (Id))
4370 then
4371 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4372 end if;
4374 Next_Entity (Id);
4375 end loop;
4377 Set_In_Use (System_Aux_Id, False);
4378 end if;
4380 else
4381 Set_Redundant_Use (Pack_Name, False);
4382 end if;
4383 end if;
4385 Next (Pack_Name);
4386 end loop;
4388 if Present (Hidden_By_Use_Clause (N)) then
4389 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
4390 while Present (Elmt) loop
4391 declare
4392 E : constant Entity_Id := Node (Elmt);
4394 begin
4395 -- Reset either Use_Visibility or Direct_Visibility, depending
4396 -- on how the entity was hidden by the use clause.
4398 if In_Use (Scope (E))
4399 and then Used_As_Generic_Actual (Scope (E))
4400 then
4401 Set_Is_Potentially_Use_Visible (Node (Elmt));
4402 else
4403 Set_Is_Immediately_Visible (Node (Elmt));
4404 end if;
4406 Next_Elmt (Elmt);
4407 end;
4408 end loop;
4410 Set_Hidden_By_Use_Clause (N, No_Elist);
4411 end if;
4412 end End_Use_Package;
4414 ------------------
4415 -- End_Use_Type --
4416 ------------------
4418 procedure End_Use_Type (N : Node_Id) is
4419 Elmt : Elmt_Id;
4420 Id : Entity_Id;
4421 T : Entity_Id;
4423 -- Start of processing for End_Use_Type
4425 begin
4426 Id := First (Subtype_Marks (N));
4427 while Present (Id) loop
4429 -- A call to Rtsfind may occur while analyzing a use_type clause,
4430 -- in which case the type marks are not resolved yet, and there is
4431 -- nothing to remove.
4433 if not Is_Entity_Name (Id) or else No (Entity (Id)) then
4434 goto Continue;
4435 end if;
4437 T := Entity (Id);
4439 if T = Any_Type or else From_Limited_With (T) then
4440 null;
4442 -- Note that the use_type clause may mention a subtype of the type
4443 -- whose primitive operations have been made visible. Here as
4444 -- elsewhere, it is the base type that matters for visibility.
4446 elsif In_Open_Scopes (Scope (Base_Type (T))) then
4447 null;
4449 elsif not Redundant_Use (Id) then
4450 Set_In_Use (T, False);
4451 Set_In_Use (Base_Type (T), False);
4452 Set_Current_Use_Clause (T, Empty);
4453 Set_Current_Use_Clause (Base_Type (T), Empty);
4454 end if;
4456 <<Continue>>
4457 Next (Id);
4458 end loop;
4460 if Is_Empty_Elmt_List (Used_Operations (N)) then
4461 return;
4463 else
4464 Elmt := First_Elmt (Used_Operations (N));
4465 while Present (Elmt) loop
4466 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
4467 Next_Elmt (Elmt);
4468 end loop;
4469 end if;
4470 end End_Use_Type;
4472 ----------------------
4473 -- Find_Direct_Name --
4474 ----------------------
4476 procedure Find_Direct_Name (N : Node_Id) is
4477 E : Entity_Id;
4478 E2 : Entity_Id;
4479 Msg : Boolean;
4481 Inst : Entity_Id := Empty;
4482 -- Enclosing instance, if any
4484 Homonyms : Entity_Id;
4485 -- Saves start of homonym chain
4487 Nvis_Entity : Boolean;
4488 -- Set True to indicate that there is at least one entity on the homonym
4489 -- chain which, while not visible, is visible enough from the user point
4490 -- of view to warrant an error message of "not visible" rather than
4491 -- undefined.
4493 Nvis_Is_Private_Subprg : Boolean := False;
4494 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
4495 -- effect concerning library subprograms has been detected. Used to
4496 -- generate the precise error message.
4498 function From_Actual_Package (E : Entity_Id) return Boolean;
4499 -- Returns true if the entity is an actual for a package that is itself
4500 -- an actual for a formal package of the current instance. Such an
4501 -- entity requires special handling because it may be use-visible but
4502 -- hides directly visible entities defined outside the instance, because
4503 -- the corresponding formal did so in the generic.
4505 function Is_Actual_Parameter return Boolean;
4506 -- This function checks if the node N is an identifier that is an actual
4507 -- parameter of a procedure call. If so it returns True, otherwise it
4508 -- return False. The reason for this check is that at this stage we do
4509 -- not know what procedure is being called if the procedure might be
4510 -- overloaded, so it is premature to go setting referenced flags or
4511 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
4512 -- for that processing
4514 function Known_But_Invisible (E : Entity_Id) return Boolean;
4515 -- This function determines whether a reference to the entity E, which
4516 -- is not visible, can reasonably be considered to be known to the
4517 -- writer of the reference. This is a heuristic test, used only for
4518 -- the purposes of figuring out whether we prefer to complain that an
4519 -- entity is undefined or invisible (and identify the declaration of
4520 -- the invisible entity in the latter case). The point here is that we
4521 -- don't want to complain that something is invisible and then point to
4522 -- something entirely mysterious to the writer.
4524 procedure Nvis_Messages;
4525 -- Called if there are no visible entries for N, but there is at least
4526 -- one non-directly visible, or hidden declaration. This procedure
4527 -- outputs an appropriate set of error messages.
4529 procedure Undefined (Nvis : Boolean);
4530 -- This function is called if the current node has no corresponding
4531 -- visible entity or entities. The value set in Msg indicates whether
4532 -- an error message was generated (multiple error messages for the
4533 -- same variable are generally suppressed, see body for details).
4534 -- Msg is True if an error message was generated, False if not. This
4535 -- value is used by the caller to determine whether or not to output
4536 -- additional messages where appropriate. The parameter is set False
4537 -- to get the message "X is undefined", and True to get the message
4538 -- "X is not visible".
4540 -------------------------
4541 -- From_Actual_Package --
4542 -------------------------
4544 function From_Actual_Package (E : Entity_Id) return Boolean is
4545 Scop : constant Entity_Id := Scope (E);
4546 -- Declared scope of candidate entity
4548 Act : Entity_Id;
4550 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
4551 -- Recursive function that does the work and examines actuals of
4552 -- actual packages of current instance.
4554 ------------------------
4555 -- Declared_In_Actual --
4556 ------------------------
4558 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
4559 Act : Entity_Id;
4561 begin
4562 if No (Associated_Formal_Package (Pack)) then
4563 return False;
4565 else
4566 Act := First_Entity (Pack);
4567 while Present (Act) loop
4568 if Renamed_Object (Pack) = Scop then
4569 return True;
4571 -- Check for end of list of actuals.
4573 elsif Ekind (Act) = E_Package
4574 and then Renamed_Object (Act) = Pack
4575 then
4576 return False;
4578 elsif Ekind (Act) = E_Package
4579 and then Declared_In_Actual (Act)
4580 then
4581 return True;
4582 end if;
4584 Next_Entity (Act);
4585 end loop;
4587 return False;
4588 end if;
4589 end Declared_In_Actual;
4591 -- Start of processing for From_Actual_Package
4593 begin
4594 if not In_Instance then
4595 return False;
4597 else
4598 Inst := Current_Scope;
4599 while Present (Inst)
4600 and then Ekind (Inst) /= E_Package
4601 and then not Is_Generic_Instance (Inst)
4602 loop
4603 Inst := Scope (Inst);
4604 end loop;
4606 if No (Inst) then
4607 return False;
4608 end if;
4610 Act := First_Entity (Inst);
4611 while Present (Act) loop
4612 if Ekind (Act) = E_Package
4613 and then Declared_In_Actual (Act)
4614 then
4615 return True;
4616 end if;
4618 Next_Entity (Act);
4619 end loop;
4621 return False;
4622 end if;
4623 end From_Actual_Package;
4625 -------------------------
4626 -- Is_Actual_Parameter --
4627 -------------------------
4629 function Is_Actual_Parameter return Boolean is
4630 begin
4631 return
4632 Nkind (N) = N_Identifier
4633 and then
4634 (Nkind (Parent (N)) = N_Procedure_Call_Statement
4635 or else
4636 (Nkind (Parent (N)) = N_Parameter_Association
4637 and then N = Explicit_Actual_Parameter (Parent (N))
4638 and then Nkind (Parent (Parent (N))) =
4639 N_Procedure_Call_Statement));
4640 end Is_Actual_Parameter;
4642 -------------------------
4643 -- Known_But_Invisible --
4644 -------------------------
4646 function Known_But_Invisible (E : Entity_Id) return Boolean is
4647 Fname : File_Name_Type;
4649 begin
4650 -- Entities in Standard are always considered to be known
4652 if Sloc (E) <= Standard_Location then
4653 return True;
4655 -- An entity that does not come from source is always considered
4656 -- to be unknown, since it is an artifact of code expansion.
4658 elsif not Comes_From_Source (E) then
4659 return False;
4661 -- In gnat internal mode, we consider all entities known. The
4662 -- historical reason behind this discrepancy is not known??? But the
4663 -- only effect is to modify the error message given, so it is not
4664 -- critical. Since it only affects the exact wording of error
4665 -- messages in illegal programs, we do not mention this as an
4666 -- effect of -gnatg, since it is not a language modification.
4668 elsif GNAT_Mode then
4669 return True;
4670 end if;
4672 -- Here we have an entity that is not from package Standard, and
4673 -- which comes from Source. See if it comes from an internal file.
4675 Fname := Unit_File_Name (Get_Source_Unit (E));
4677 -- Case of from internal file
4679 if Is_Internal_File_Name (Fname) then
4681 -- Private part entities in internal files are never considered
4682 -- to be known to the writer of normal application code.
4684 if Is_Hidden (E) then
4685 return False;
4686 end if;
4688 -- Entities from System packages other than System and
4689 -- System.Storage_Elements are not considered to be known.
4690 -- System.Auxxxx files are also considered known to the user.
4692 -- Should refine this at some point to generally distinguish
4693 -- between known and unknown internal files ???
4695 Get_Name_String (Fname);
4697 return
4698 Name_Len < 2
4699 or else
4700 Name_Buffer (1 .. 2) /= "s-"
4701 or else
4702 Name_Buffer (3 .. 8) = "stoele"
4703 or else
4704 Name_Buffer (3 .. 5) = "aux";
4706 -- If not an internal file, then entity is definitely known,
4707 -- even if it is in a private part (the message generated will
4708 -- note that it is in a private part)
4710 else
4711 return True;
4712 end if;
4713 end Known_But_Invisible;
4715 -------------------
4716 -- Nvis_Messages --
4717 -------------------
4719 procedure Nvis_Messages is
4720 Comp_Unit : Node_Id;
4721 Ent : Entity_Id;
4722 Found : Boolean := False;
4723 Hidden : Boolean := False;
4724 Item : Node_Id;
4726 begin
4727 -- Ada 2005 (AI-262): Generate a precise error concerning the
4728 -- Beaujolais effect that was previously detected
4730 if Nvis_Is_Private_Subprg then
4732 pragma Assert (Nkind (E2) = N_Defining_Identifier
4733 and then Ekind (E2) = E_Function
4734 and then Scope (E2) = Standard_Standard
4735 and then Has_Private_With (E2));
4737 -- Find the sloc corresponding to the private with'ed unit
4739 Comp_Unit := Cunit (Current_Sem_Unit);
4740 Error_Msg_Sloc := No_Location;
4742 Item := First (Context_Items (Comp_Unit));
4743 while Present (Item) loop
4744 if Nkind (Item) = N_With_Clause
4745 and then Private_Present (Item)
4746 and then Entity (Name (Item)) = E2
4747 then
4748 Error_Msg_Sloc := Sloc (Item);
4749 exit;
4750 end if;
4752 Next (Item);
4753 end loop;
4755 pragma Assert (Error_Msg_Sloc /= No_Location);
4757 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
4758 return;
4759 end if;
4761 Undefined (Nvis => True);
4763 if Msg then
4765 -- First loop does hidden declarations
4767 Ent := Homonyms;
4768 while Present (Ent) loop
4769 if Is_Potentially_Use_Visible (Ent) then
4770 if not Hidden then
4771 Error_Msg_N -- CODEFIX
4772 ("multiple use clauses cause hiding!", N);
4773 Hidden := True;
4774 end if;
4776 Error_Msg_Sloc := Sloc (Ent);
4777 Error_Msg_N -- CODEFIX
4778 ("hidden declaration#!", N);
4779 end if;
4781 Ent := Homonym (Ent);
4782 end loop;
4784 -- If we found hidden declarations, then that's enough, don't
4785 -- bother looking for non-visible declarations as well.
4787 if Hidden then
4788 return;
4789 end if;
4791 -- Second loop does non-directly visible declarations
4793 Ent := Homonyms;
4794 while Present (Ent) loop
4795 if not Is_Potentially_Use_Visible (Ent) then
4797 -- Do not bother the user with unknown entities
4799 if not Known_But_Invisible (Ent) then
4800 goto Continue;
4801 end if;
4803 Error_Msg_Sloc := Sloc (Ent);
4805 -- Output message noting that there is a non-visible
4806 -- declaration, distinguishing the private part case.
4808 if Is_Hidden (Ent) then
4809 Error_Msg_N ("non-visible (private) declaration#!", N);
4811 -- If the entity is declared in a generic package, it
4812 -- cannot be visible, so there is no point in adding it
4813 -- to the list of candidates if another homograph from a
4814 -- non-generic package has been seen.
4816 elsif Ekind (Scope (Ent)) = E_Generic_Package
4817 and then Found
4818 then
4819 null;
4821 else
4822 Error_Msg_N -- CODEFIX
4823 ("non-visible declaration#!", N);
4825 if Ekind (Scope (Ent)) /= E_Generic_Package then
4826 Found := True;
4827 end if;
4829 if Is_Compilation_Unit (Ent)
4830 and then
4831 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
4832 then
4833 Error_Msg_Qual_Level := 99;
4834 Error_Msg_NE -- CODEFIX
4835 ("\\missing `WITH &;`", N, Ent);
4836 Error_Msg_Qual_Level := 0;
4837 end if;
4839 if Ekind (Ent) = E_Discriminant
4840 and then Present (Corresponding_Discriminant (Ent))
4841 and then Scope (Corresponding_Discriminant (Ent)) =
4842 Etype (Scope (Ent))
4843 then
4844 Error_Msg_N
4845 ("inherited discriminant not allowed here" &
4846 " (RM 3.8 (12), 3.8.1 (6))!", N);
4847 end if;
4848 end if;
4850 -- Set entity and its containing package as referenced. We
4851 -- can't be sure of this, but this seems a better choice
4852 -- to avoid unused entity messages.
4854 if Comes_From_Source (Ent) then
4855 Set_Referenced (Ent);
4856 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
4857 end if;
4858 end if;
4860 <<Continue>>
4861 Ent := Homonym (Ent);
4862 end loop;
4863 end if;
4864 end Nvis_Messages;
4866 ---------------
4867 -- Undefined --
4868 ---------------
4870 procedure Undefined (Nvis : Boolean) is
4871 Emsg : Error_Msg_Id;
4873 begin
4874 -- We should never find an undefined internal name. If we do, then
4875 -- see if we have previous errors. If so, ignore on the grounds that
4876 -- it is probably a cascaded message (e.g. a block label from a badly
4877 -- formed block). If no previous errors, then we have a real internal
4878 -- error of some kind so raise an exception.
4880 if Is_Internal_Name (Chars (N)) then
4881 if Total_Errors_Detected /= 0 then
4882 return;
4883 else
4884 raise Program_Error;
4885 end if;
4886 end if;
4888 -- A very specialized error check, if the undefined variable is
4889 -- a case tag, and the case type is an enumeration type, check
4890 -- for a possible misspelling, and if so, modify the identifier
4892 -- Named aggregate should also be handled similarly ???
4894 if Nkind (N) = N_Identifier
4895 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
4896 then
4897 declare
4898 Case_Stm : constant Node_Id := Parent (Parent (N));
4899 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
4901 Lit : Node_Id;
4903 begin
4904 if Is_Enumeration_Type (Case_Typ)
4905 and then not Is_Standard_Character_Type (Case_Typ)
4906 then
4907 Lit := First_Literal (Case_Typ);
4908 Get_Name_String (Chars (Lit));
4910 if Chars (Lit) /= Chars (N)
4911 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
4912 then
4913 Error_Msg_Node_2 := Lit;
4914 Error_Msg_N -- CODEFIX
4915 ("& is undefined, assume misspelling of &", N);
4916 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
4917 return;
4918 end if;
4920 Lit := Next_Literal (Lit);
4921 end if;
4922 end;
4923 end if;
4925 -- Normal processing
4927 Set_Entity (N, Any_Id);
4928 Set_Etype (N, Any_Type);
4930 -- We use the table Urefs to keep track of entities for which we
4931 -- have issued errors for undefined references. Multiple errors
4932 -- for a single name are normally suppressed, however we modify
4933 -- the error message to alert the programmer to this effect.
4935 for J in Urefs.First .. Urefs.Last loop
4936 if Chars (N) = Chars (Urefs.Table (J).Node) then
4937 if Urefs.Table (J).Err /= No_Error_Msg
4938 and then Sloc (N) /= Urefs.Table (J).Loc
4939 then
4940 Error_Msg_Node_1 := Urefs.Table (J).Node;
4942 if Urefs.Table (J).Nvis then
4943 Change_Error_Text (Urefs.Table (J).Err,
4944 "& is not visible (more references follow)");
4945 else
4946 Change_Error_Text (Urefs.Table (J).Err,
4947 "& is undefined (more references follow)");
4948 end if;
4950 Urefs.Table (J).Err := No_Error_Msg;
4951 end if;
4953 -- Although we will set Msg False, and thus suppress the
4954 -- message, we also set Error_Posted True, to avoid any
4955 -- cascaded messages resulting from the undefined reference.
4957 Msg := False;
4958 Set_Error_Posted (N, True);
4959 return;
4960 end if;
4961 end loop;
4963 -- If entry not found, this is first undefined occurrence
4965 if Nvis then
4966 Error_Msg_N ("& is not visible!", N);
4967 Emsg := Get_Msg_Id;
4969 else
4970 Error_Msg_N ("& is undefined!", N);
4971 Emsg := Get_Msg_Id;
4973 -- A very bizarre special check, if the undefined identifier
4974 -- is put or put_line, then add a special error message (since
4975 -- this is a very common error for beginners to make).
4977 if Nam_In (Chars (N), Name_Put, Name_Put_Line) then
4978 Error_Msg_N -- CODEFIX
4979 ("\\possible missing `WITH Ada.Text_'I'O; " &
4980 "USE Ada.Text_'I'O`!", N);
4982 -- Another special check if N is the prefix of a selected
4983 -- component which is a known unit, add message complaining
4984 -- about missing with for this unit.
4986 elsif Nkind (Parent (N)) = N_Selected_Component
4987 and then N = Prefix (Parent (N))
4988 and then Is_Known_Unit (Parent (N))
4989 then
4990 Error_Msg_Node_2 := Selector_Name (Parent (N));
4991 Error_Msg_N -- CODEFIX
4992 ("\\missing `WITH &.&;`", Prefix (Parent (N)));
4993 end if;
4995 -- Now check for possible misspellings
4997 declare
4998 E : Entity_Id;
4999 Ematch : Entity_Id := Empty;
5001 Last_Name_Id : constant Name_Id :=
5002 Name_Id (Nat (First_Name_Id) +
5003 Name_Entries_Count - 1);
5005 begin
5006 for Nam in First_Name_Id .. Last_Name_Id loop
5007 E := Get_Name_Entity_Id (Nam);
5009 if Present (E)
5010 and then (Is_Immediately_Visible (E)
5011 or else
5012 Is_Potentially_Use_Visible (E))
5013 then
5014 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5015 Ematch := E;
5016 exit;
5017 end if;
5018 end if;
5019 end loop;
5021 if Present (Ematch) then
5022 Error_Msg_NE -- CODEFIX
5023 ("\possible misspelling of&", N, Ematch);
5024 end if;
5025 end;
5026 end if;
5028 -- Make entry in undefined references table unless the full errors
5029 -- switch is set, in which case by refraining from generating the
5030 -- table entry, we guarantee that we get an error message for every
5031 -- undefined reference.
5033 if not All_Errors_Mode then
5034 Urefs.Append (
5035 (Node => N,
5036 Err => Emsg,
5037 Nvis => Nvis,
5038 Loc => Sloc (N)));
5039 end if;
5041 Msg := True;
5042 end Undefined;
5044 -- Start of processing for Find_Direct_Name
5046 begin
5047 -- If the entity pointer is already set, this is an internal node, or
5048 -- a node that is analyzed more than once, after a tree modification.
5049 -- In such a case there is no resolution to perform, just set the type.
5051 if Present (Entity (N)) then
5052 if Is_Type (Entity (N)) then
5053 Set_Etype (N, Entity (N));
5055 else
5056 declare
5057 Entyp : constant Entity_Id := Etype (Entity (N));
5059 begin
5060 -- One special case here. If the Etype field is already set,
5061 -- and references the packed array type corresponding to the
5062 -- etype of the referenced entity, then leave it alone. This
5063 -- happens for trees generated from Exp_Pakd, where expressions
5064 -- can be deliberately "mis-typed" to the packed array type.
5066 if Is_Array_Type (Entyp)
5067 and then Is_Packed (Entyp)
5068 and then Present (Etype (N))
5069 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5070 then
5071 null;
5073 -- If not that special case, then just reset the Etype
5075 else
5076 Set_Etype (N, Etype (Entity (N)));
5077 end if;
5078 end;
5079 end if;
5081 return;
5082 end if;
5084 -- Here if Entity pointer was not set, we need full visibility analysis
5085 -- First we generate debugging output if the debug E flag is set.
5087 if Debug_Flag_E then
5088 Write_Str ("Looking for ");
5089 Write_Name (Chars (N));
5090 Write_Eol;
5091 end if;
5093 Homonyms := Current_Entity (N);
5094 Nvis_Entity := False;
5096 E := Homonyms;
5097 while Present (E) loop
5099 -- If entity is immediately visible or potentially use visible, then
5100 -- process the entity and we are done.
5102 if Is_Immediately_Visible (E) then
5103 goto Immediately_Visible_Entity;
5105 elsif Is_Potentially_Use_Visible (E) then
5106 goto Potentially_Use_Visible_Entity;
5108 -- Note if a known but invisible entity encountered
5110 elsif Known_But_Invisible (E) then
5111 Nvis_Entity := True;
5112 end if;
5114 -- Move to next entity in chain and continue search
5116 E := Homonym (E);
5117 end loop;
5119 -- If no entries on homonym chain that were potentially visible,
5120 -- and no entities reasonably considered as non-visible, then
5121 -- we have a plain undefined reference, with no additional
5122 -- explanation required.
5124 if not Nvis_Entity then
5125 Undefined (Nvis => False);
5127 -- Otherwise there is at least one entry on the homonym chain that
5128 -- is reasonably considered as being known and non-visible.
5130 else
5131 Nvis_Messages;
5132 end if;
5134 return;
5136 -- Processing for a potentially use visible entry found. We must search
5137 -- the rest of the homonym chain for two reasons. First, if there is a
5138 -- directly visible entry, then none of the potentially use-visible
5139 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5140 -- for the case of multiple potentially use-visible entries hiding one
5141 -- another and as a result being non-directly visible (RM 8.4(11)).
5143 <<Potentially_Use_Visible_Entity>> declare
5144 Only_One_Visible : Boolean := True;
5145 All_Overloadable : Boolean := Is_Overloadable (E);
5147 begin
5148 E2 := Homonym (E);
5149 while Present (E2) loop
5150 if Is_Immediately_Visible (E2) then
5152 -- If the use-visible entity comes from the actual for a
5153 -- formal package, it hides a directly visible entity from
5154 -- outside the instance.
5156 if From_Actual_Package (E)
5157 and then Scope_Depth (E2) < Scope_Depth (Inst)
5158 then
5159 goto Found;
5160 else
5161 E := E2;
5162 goto Immediately_Visible_Entity;
5163 end if;
5165 elsif Is_Potentially_Use_Visible (E2) then
5166 Only_One_Visible := False;
5167 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5169 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5170 -- that can occur in private_with clauses. Example:
5172 -- with A;
5173 -- private with B; package A is
5174 -- package C is function B return Integer;
5175 -- use A; end A;
5176 -- V1 : Integer := B;
5177 -- private function B return Integer;
5178 -- V2 : Integer := B;
5179 -- end C;
5181 -- V1 resolves to A.B, but V2 resolves to library unit B
5183 elsif Ekind (E2) = E_Function
5184 and then Scope (E2) = Standard_Standard
5185 and then Has_Private_With (E2)
5186 then
5187 Only_One_Visible := False;
5188 All_Overloadable := False;
5189 Nvis_Is_Private_Subprg := True;
5190 exit;
5191 end if;
5193 E2 := Homonym (E2);
5194 end loop;
5196 -- On falling through this loop, we have checked that there are no
5197 -- immediately visible entities. Only_One_Visible is set if exactly
5198 -- one potentially use visible entity exists. All_Overloadable is
5199 -- set if all the potentially use visible entities are overloadable.
5200 -- The condition for legality is that either there is one potentially
5201 -- use visible entity, or if there is more than one, then all of them
5202 -- are overloadable.
5204 if Only_One_Visible or All_Overloadable then
5205 goto Found;
5207 -- If there is more than one potentially use-visible entity and at
5208 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5209 -- Note that E points to the first such entity on the homonym list.
5210 -- Special case: if one of the entities is declared in an actual
5211 -- package, it was visible in the generic, and takes precedence over
5212 -- other entities that are potentially use-visible. Same if it is
5213 -- declared in a local instantiation of the current instance.
5215 else
5216 if In_Instance then
5218 -- Find current instance
5220 Inst := Current_Scope;
5221 while Present (Inst) and then Inst /= Standard_Standard loop
5222 if Is_Generic_Instance (Inst) then
5223 exit;
5224 end if;
5226 Inst := Scope (Inst);
5227 end loop;
5229 E2 := E;
5230 while Present (E2) loop
5231 if From_Actual_Package (E2)
5232 or else
5233 (Is_Generic_Instance (Scope (E2))
5234 and then Scope_Depth (Scope (E2)) > Scope_Depth (Inst))
5235 then
5236 E := E2;
5237 goto Found;
5238 end if;
5240 E2 := Homonym (E2);
5241 end loop;
5243 Nvis_Messages;
5244 return;
5246 elsif
5247 Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
5248 then
5249 -- A use-clause in the body of a system file creates conflict
5250 -- with some entity in a user scope, while rtsfind is active.
5251 -- Keep only the entity coming from another predefined unit.
5253 E2 := E;
5254 while Present (E2) loop
5255 if Is_Predefined_File_Name
5256 (Unit_File_Name (Get_Source_Unit (Sloc (E2))))
5257 then
5258 E := E2;
5259 goto Found;
5260 end if;
5262 E2 := Homonym (E2);
5263 end loop;
5265 -- Entity must exist because predefined unit is correct
5267 raise Program_Error;
5269 else
5270 Nvis_Messages;
5271 return;
5272 end if;
5273 end if;
5274 end;
5276 -- Come here with E set to the first immediately visible entity on
5277 -- the homonym chain. This is the one we want unless there is another
5278 -- immediately visible entity further on in the chain for an inner
5279 -- scope (RM 8.3(8)).
5281 <<Immediately_Visible_Entity>> declare
5282 Level : Int;
5283 Scop : Entity_Id;
5285 begin
5286 -- Find scope level of initial entity. When compiling through
5287 -- Rtsfind, the previous context is not completely invisible, and
5288 -- an outer entity may appear on the chain, whose scope is below
5289 -- the entry for Standard that delimits the current scope stack.
5290 -- Indicate that the level for this spurious entry is outside of
5291 -- the current scope stack.
5293 Level := Scope_Stack.Last;
5294 loop
5295 Scop := Scope_Stack.Table (Level).Entity;
5296 exit when Scop = Scope (E);
5297 Level := Level - 1;
5298 exit when Scop = Standard_Standard;
5299 end loop;
5301 -- Now search remainder of homonym chain for more inner entry
5302 -- If the entity is Standard itself, it has no scope, and we
5303 -- compare it with the stack entry directly.
5305 E2 := Homonym (E);
5306 while Present (E2) loop
5307 if Is_Immediately_Visible (E2) then
5309 -- If a generic package contains a local declaration that
5310 -- has the same name as the generic, there may be a visibility
5311 -- conflict in an instance, where the local declaration must
5312 -- also hide the name of the corresponding package renaming.
5313 -- We check explicitly for a package declared by a renaming,
5314 -- whose renamed entity is an instance that is on the scope
5315 -- stack, and that contains a homonym in the same scope. Once
5316 -- we have found it, we know that the package renaming is not
5317 -- immediately visible, and that the identifier denotes the
5318 -- other entity (and its homonyms if overloaded).
5320 if Scope (E) = Scope (E2)
5321 and then Ekind (E) = E_Package
5322 and then Present (Renamed_Object (E))
5323 and then Is_Generic_Instance (Renamed_Object (E))
5324 and then In_Open_Scopes (Renamed_Object (E))
5325 and then Comes_From_Source (N)
5326 then
5327 Set_Is_Immediately_Visible (E, False);
5328 E := E2;
5330 else
5331 for J in Level + 1 .. Scope_Stack.Last loop
5332 if Scope_Stack.Table (J).Entity = Scope (E2)
5333 or else Scope_Stack.Table (J).Entity = E2
5334 then
5335 Level := J;
5336 E := E2;
5337 exit;
5338 end if;
5339 end loop;
5340 end if;
5341 end if;
5343 E2 := Homonym (E2);
5344 end loop;
5346 -- At the end of that loop, E is the innermost immediately
5347 -- visible entity, so we are all set.
5348 end;
5350 -- Come here with entity found, and stored in E
5352 <<Found>> begin
5354 -- Check violation of No_Wide_Characters restriction
5356 Check_Wide_Character_Restriction (E, N);
5358 -- When distribution features are available (Get_PCS_Name /=
5359 -- Name_No_DSA), a remote access-to-subprogram type is converted
5360 -- into a record type holding whatever information is needed to
5361 -- perform a remote call on an RCI subprogram. In that case we
5362 -- rewrite any occurrence of the RAS type into the equivalent record
5363 -- type here. 'Access attribute references and RAS dereferences are
5364 -- then implemented using specific TSSs. However when distribution is
5365 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
5366 -- generation of these TSSs, and we must keep the RAS type in its
5367 -- original access-to-subprogram form (since all calls through a
5368 -- value of such type will be local anyway in the absence of a PCS).
5370 if Comes_From_Source (N)
5371 and then Is_Remote_Access_To_Subprogram_Type (E)
5372 and then Ekind (E) = E_Access_Subprogram_Type
5373 and then Expander_Active
5374 and then Get_PCS_Name /= Name_No_DSA
5375 then
5376 Rewrite (N,
5377 New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
5378 return;
5379 end if;
5381 -- Set the entity. Note that the reason we call Set_Entity for the
5382 -- overloadable case, as opposed to Set_Entity_With_Checks is
5383 -- that in the overloaded case, the initial call can set the wrong
5384 -- homonym. The call that sets the right homonym is in Sem_Res and
5385 -- that call does use Set_Entity_With_Checks, so we don't miss
5386 -- a style check.
5388 if Is_Overloadable (E) then
5389 Set_Entity (N, E);
5390 else
5391 Set_Entity_With_Checks (N, E);
5392 end if;
5394 if Is_Type (E) then
5395 Set_Etype (N, E);
5396 else
5397 Set_Etype (N, Get_Full_View (Etype (E)));
5398 end if;
5400 if Debug_Flag_E then
5401 Write_Str (" found ");
5402 Write_Entity_Info (E, " ");
5403 end if;
5405 -- If the Ekind of the entity is Void, it means that all homonyms
5406 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
5407 -- test is skipped if the current scope is a record and the name is
5408 -- a pragma argument expression (case of Atomic and Volatile pragmas
5409 -- and possibly other similar pragmas added later, which are allowed
5410 -- to reference components in the current record).
5412 if Ekind (E) = E_Void
5413 and then
5414 (not Is_Record_Type (Current_Scope)
5415 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
5416 then
5417 Premature_Usage (N);
5419 -- If the entity is overloadable, collect all interpretations of the
5420 -- name for subsequent overload resolution. We optimize a bit here to
5421 -- do this only if we have an overloadable entity that is not on its
5422 -- own on the homonym chain.
5424 elsif Is_Overloadable (E)
5425 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
5426 then
5427 Collect_Interps (N);
5429 -- If no homonyms were visible, the entity is unambiguous
5431 if not Is_Overloaded (N) then
5432 if not Is_Actual_Parameter then
5433 Generate_Reference (E, N);
5434 end if;
5435 end if;
5437 -- Case of non-overloadable entity, set the entity providing that
5438 -- we do not have the case of a discriminant reference within a
5439 -- default expression. Such references are replaced with the
5440 -- corresponding discriminal, which is the formal corresponding to
5441 -- to the discriminant in the initialization procedure.
5443 else
5444 -- Entity is unambiguous, indicate that it is referenced here
5446 -- For a renaming of an object, always generate simple reference,
5447 -- we don't try to keep track of assignments in this case, except
5448 -- in SPARK mode where renamings are traversed for generating
5449 -- local effects of subprograms.
5451 if Is_Object (E)
5452 and then Present (Renamed_Object (E))
5453 and then not GNATprove_Mode
5454 then
5455 Generate_Reference (E, N);
5457 -- If the renamed entity is a private protected component,
5458 -- reference the original component as well. This needs to be
5459 -- done because the private renamings are installed before any
5460 -- analysis has occurred. Reference to a private component will
5461 -- resolve to the renaming and the original component will be
5462 -- left unreferenced, hence the following.
5464 if Is_Prival (E) then
5465 Generate_Reference (Prival_Link (E), N);
5466 end if;
5468 -- One odd case is that we do not want to set the Referenced flag
5469 -- if the entity is a label, and the identifier is the label in
5470 -- the source, since this is not a reference from the point of
5471 -- view of the user.
5473 elsif Nkind (Parent (N)) = N_Label then
5474 declare
5475 R : constant Boolean := Referenced (E);
5477 begin
5478 -- Generate reference unless this is an actual parameter
5479 -- (see comment below)
5481 if Is_Actual_Parameter then
5482 Generate_Reference (E, N);
5483 Set_Referenced (E, R);
5484 end if;
5485 end;
5487 -- Normal case, not a label: generate reference
5489 else
5490 if not Is_Actual_Parameter then
5492 -- Package or generic package is always a simple reference
5494 if Ekind_In (E, E_Package, E_Generic_Package) then
5495 Generate_Reference (E, N, 'r');
5497 -- Else see if we have a left hand side
5499 else
5500 case Is_LHS (N) is
5501 when Yes =>
5502 Generate_Reference (E, N, 'm');
5504 when No =>
5505 Generate_Reference (E, N, 'r');
5507 -- If we don't know now, generate reference later
5509 when Unknown =>
5510 Deferred_References.Append ((E, N));
5511 end case;
5512 end if;
5513 end if;
5515 Check_Nested_Access (E);
5516 end if;
5518 Set_Entity_Or_Discriminal (N, E);
5520 -- The name may designate a generalized reference, in which case
5521 -- the dereference interpretation will be included.
5523 if Ada_Version >= Ada_2012
5524 and then
5525 (Nkind (Parent (N)) in N_Subexpr
5526 or else Nkind_In (Parent (N), N_Object_Declaration,
5527 N_Assignment_Statement))
5528 then
5529 Check_Implicit_Dereference (N, Etype (E));
5530 end if;
5531 end if;
5532 end;
5533 end Find_Direct_Name;
5535 ------------------------
5536 -- Find_Expanded_Name --
5537 ------------------------
5539 -- This routine searches the homonym chain of the entity until it finds
5540 -- an entity declared in the scope denoted by the prefix. If the entity
5541 -- is private, it may nevertheless be immediately visible, if we are in
5542 -- the scope of its declaration.
5544 procedure Find_Expanded_Name (N : Node_Id) is
5545 function In_Pragmas_Depends_Or_Global (N : Node_Id) return Boolean;
5546 -- Determine whether an arbitrary node N appears in pragmas [Refined_]
5547 -- Depends or [Refined_]Global.
5549 ----------------------------------
5550 -- In_Pragmas_Depends_Or_Global --
5551 ----------------------------------
5553 function In_Pragmas_Depends_Or_Global (N : Node_Id) return Boolean is
5554 Par : Node_Id;
5556 begin
5557 -- Climb the parent chain looking for a pragma
5559 Par := N;
5560 while Present (Par) loop
5561 if Nkind (Par) = N_Pragma
5562 and then Nam_In (Pragma_Name (Par), Name_Depends,
5563 Name_Global,
5564 Name_Refined_Depends,
5565 Name_Refined_Global)
5566 then
5567 return True;
5569 -- Prevent the search from going too far
5571 elsif Is_Body_Or_Package_Declaration (Par) then
5572 return False;
5573 end if;
5575 Par := Parent (Par);
5576 end loop;
5578 return False;
5579 end In_Pragmas_Depends_Or_Global;
5581 -- Local variables
5583 Selector : constant Node_Id := Selector_Name (N);
5584 Candidate : Entity_Id := Empty;
5585 P_Name : Entity_Id;
5586 Id : Entity_Id;
5588 -- Start of processing for Find_Expanded_Name
5590 begin
5591 P_Name := Entity (Prefix (N));
5593 -- If the prefix is a renamed package, look for the entity in the
5594 -- original package.
5596 if Ekind (P_Name) = E_Package
5597 and then Present (Renamed_Object (P_Name))
5598 then
5599 P_Name := Renamed_Object (P_Name);
5601 -- Rewrite node with entity field pointing to renamed object
5603 Rewrite (Prefix (N), New_Copy (Prefix (N)));
5604 Set_Entity (Prefix (N), P_Name);
5606 -- If the prefix is an object of a concurrent type, look for
5607 -- the entity in the associated task or protected type.
5609 elsif Is_Concurrent_Type (Etype (P_Name)) then
5610 P_Name := Etype (P_Name);
5611 end if;
5613 Id := Current_Entity (Selector);
5615 declare
5616 Is_New_Candidate : Boolean;
5618 begin
5619 while Present (Id) loop
5620 if Scope (Id) = P_Name then
5621 Candidate := Id;
5622 Is_New_Candidate := True;
5624 -- Handle abstract views of states and variables. These are
5625 -- acceptable only when the reference to the view appears in
5626 -- pragmas [Refined_]Depends and [Refined_]Global.
5628 if Ekind (Id) = E_Abstract_State
5629 and then From_Limited_With (Id)
5630 and then Present (Non_Limited_View (Id))
5631 then
5632 if In_Pragmas_Depends_Or_Global (N) then
5633 Candidate := Non_Limited_View (Id);
5634 Is_New_Candidate := True;
5636 -- Hide candidate because it is not used in a proper context
5638 else
5639 Candidate := Empty;
5640 Is_New_Candidate := False;
5641 end if;
5642 end if;
5644 -- Ada 2005 (AI-217): Handle shadow entities associated with types
5645 -- declared in limited-withed nested packages. We don't need to
5646 -- handle E_Incomplete_Subtype entities because the entities in
5647 -- the limited view are always E_Incomplete_Type entities (see
5648 -- Build_Limited_Views). Regarding the expression used to evaluate
5649 -- the scope, it is important to note that the limited view also
5650 -- has shadow entities associated nested packages. For this reason
5651 -- the correct scope of the entity is the scope of the real entity
5652 -- The non-limited view may itself be incomplete, in which case
5653 -- get the full view if available.
5655 elsif Ekind (Id) = E_Incomplete_Type
5656 and then From_Limited_With (Id)
5657 and then Present (Non_Limited_View (Id))
5658 and then Scope (Non_Limited_View (Id)) = P_Name
5659 then
5660 Candidate := Get_Full_View (Non_Limited_View (Id));
5661 Is_New_Candidate := True;
5663 else
5664 Is_New_Candidate := False;
5665 end if;
5667 if Is_New_Candidate then
5668 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
5669 exit when Is_Visible_Lib_Unit (Id);
5670 else
5671 exit when not Is_Hidden (Id);
5672 end if;
5674 exit when Is_Immediately_Visible (Id);
5675 end if;
5677 Id := Homonym (Id);
5678 end loop;
5679 end;
5681 if No (Id)
5682 and then Ekind_In (P_Name, E_Procedure, E_Function)
5683 and then Is_Generic_Instance (P_Name)
5684 then
5685 -- Expanded name denotes entity in (instance of) generic subprogram.
5686 -- The entity may be in the subprogram instance, or may denote one of
5687 -- the formals, which is declared in the enclosing wrapper package.
5689 P_Name := Scope (P_Name);
5691 Id := Current_Entity (Selector);
5692 while Present (Id) loop
5693 exit when Scope (Id) = P_Name;
5694 Id := Homonym (Id);
5695 end loop;
5696 end if;
5698 if No (Id) or else Chars (Id) /= Chars (Selector) then
5699 Set_Etype (N, Any_Type);
5701 -- If we are looking for an entity defined in System, try to find it
5702 -- in the child package that may have been provided as an extension
5703 -- to System. The Extend_System pragma will have supplied the name of
5704 -- the extension, which may have to be loaded.
5706 if Chars (P_Name) = Name_System
5707 and then Scope (P_Name) = Standard_Standard
5708 and then Present (System_Extend_Unit)
5709 and then Present_System_Aux (N)
5710 then
5711 Set_Entity (Prefix (N), System_Aux_Id);
5712 Find_Expanded_Name (N);
5713 return;
5715 elsif Nkind (Selector) = N_Operator_Symbol
5716 and then Has_Implicit_Operator (N)
5717 then
5718 -- There is an implicit instance of the predefined operator in
5719 -- the given scope. The operator entity is defined in Standard.
5720 -- Has_Implicit_Operator makes the node into an Expanded_Name.
5722 return;
5724 elsif Nkind (Selector) = N_Character_Literal
5725 and then Has_Implicit_Character_Literal (N)
5726 then
5727 -- If there is no literal defined in the scope denoted by the
5728 -- prefix, the literal may belong to (a type derived from)
5729 -- Standard_Character, for which we have no explicit literals.
5731 return;
5733 else
5734 -- If the prefix is a single concurrent object, use its name in
5735 -- the error message, rather than that of the anonymous type.
5737 if Is_Concurrent_Type (P_Name)
5738 and then Is_Internal_Name (Chars (P_Name))
5739 then
5740 Error_Msg_Node_2 := Entity (Prefix (N));
5741 else
5742 Error_Msg_Node_2 := P_Name;
5743 end if;
5745 if P_Name = System_Aux_Id then
5746 P_Name := Scope (P_Name);
5747 Set_Entity (Prefix (N), P_Name);
5748 end if;
5750 if Present (Candidate) then
5752 -- If we know that the unit is a child unit we can give a more
5753 -- accurate error message.
5755 if Is_Child_Unit (Candidate) then
5757 -- If the candidate is a private child unit and we are in
5758 -- the visible part of a public unit, specialize the error
5759 -- message. There might be a private with_clause for it,
5760 -- but it is not currently active.
5762 if Is_Private_Descendant (Candidate)
5763 and then Ekind (Current_Scope) = E_Package
5764 and then not In_Private_Part (Current_Scope)
5765 and then not Is_Private_Descendant (Current_Scope)
5766 then
5767 Error_Msg_N ("private child unit& is not visible here",
5768 Selector);
5770 -- Normal case where we have a missing with for a child unit
5772 else
5773 Error_Msg_Qual_Level := 99;
5774 Error_Msg_NE -- CODEFIX
5775 ("missing `WITH &;`", Selector, Candidate);
5776 Error_Msg_Qual_Level := 0;
5777 end if;
5779 -- Here we don't know that this is a child unit
5781 else
5782 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
5783 end if;
5785 else
5786 -- Within the instantiation of a child unit, the prefix may
5787 -- denote the parent instance, but the selector has the name
5788 -- of the original child. That is to say, when A.B appears
5789 -- within an instantiation of generic child unit B, the scope
5790 -- stack includes an instance of A (P_Name) and an instance
5791 -- of B under some other name. We scan the scope to find this
5792 -- child instance, which is the desired entity.
5793 -- Note that the parent may itself be a child instance, if
5794 -- the reference is of the form A.B.C, in which case A.B has
5795 -- already been rewritten with the proper entity.
5797 if In_Open_Scopes (P_Name)
5798 and then Is_Generic_Instance (P_Name)
5799 then
5800 declare
5801 Gen_Par : constant Entity_Id :=
5802 Generic_Parent (Specification
5803 (Unit_Declaration_Node (P_Name)));
5804 S : Entity_Id := Current_Scope;
5805 P : Entity_Id;
5807 begin
5808 for J in reverse 0 .. Scope_Stack.Last loop
5809 S := Scope_Stack.Table (J).Entity;
5811 exit when S = Standard_Standard;
5813 if Ekind_In (S, E_Function,
5814 E_Package,
5815 E_Procedure)
5816 then
5817 P := Generic_Parent (Specification
5818 (Unit_Declaration_Node (S)));
5820 -- Check that P is a generic child of the generic
5821 -- parent of the prefix.
5823 if Present (P)
5824 and then Chars (P) = Chars (Selector)
5825 and then Scope (P) = Gen_Par
5826 then
5827 Id := S;
5828 goto Found;
5829 end if;
5830 end if;
5832 end loop;
5833 end;
5834 end if;
5836 -- If this is a selection from Ada, System or Interfaces, then
5837 -- we assume a missing with for the corresponding package.
5839 if Is_Known_Unit (N) then
5840 if not Error_Posted (N) then
5841 Error_Msg_Node_2 := Selector;
5842 Error_Msg_N -- CODEFIX
5843 ("missing `WITH &.&;`", Prefix (N));
5844 end if;
5846 -- If this is a selection from a dummy package, then suppress
5847 -- the error message, of course the entity is missing if the
5848 -- package is missing.
5850 elsif Sloc (Error_Msg_Node_2) = No_Location then
5851 null;
5853 -- Here we have the case of an undefined component
5855 else
5857 -- The prefix may hide a homonym in the context that
5858 -- declares the desired entity. This error can use a
5859 -- specialized message.
5861 if In_Open_Scopes (P_Name) then
5862 declare
5863 H : constant Entity_Id := Homonym (P_Name);
5865 begin
5866 if Present (H)
5867 and then Is_Compilation_Unit (H)
5868 and then
5869 (Is_Immediately_Visible (H)
5870 or else Is_Visible_Lib_Unit (H))
5871 then
5872 Id := First_Entity (H);
5873 while Present (Id) loop
5874 if Chars (Id) = Chars (Selector) then
5875 Error_Msg_Qual_Level := 99;
5876 Error_Msg_Name_1 := Chars (Selector);
5877 Error_Msg_NE
5878 ("% not declared in&", N, P_Name);
5879 Error_Msg_NE
5880 ("\use fully qualified name starting with "
5881 & "Standard to make& visible", N, H);
5882 Error_Msg_Qual_Level := 0;
5883 goto Done;
5884 end if;
5886 Next_Entity (Id);
5887 end loop;
5888 end if;
5890 -- If not found, standard error message
5892 Error_Msg_NE ("& not declared in&", N, Selector);
5894 <<Done>> null;
5895 end;
5897 else
5898 Error_Msg_NE ("& not declared in&", N, Selector);
5899 end if;
5901 -- Check for misspelling of some entity in prefix
5903 Id := First_Entity (P_Name);
5904 while Present (Id) loop
5905 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
5906 and then not Is_Internal_Name (Chars (Id))
5907 then
5908 Error_Msg_NE -- CODEFIX
5909 ("possible misspelling of&", Selector, Id);
5910 exit;
5911 end if;
5913 Next_Entity (Id);
5914 end loop;
5916 -- Specialize the message if this may be an instantiation
5917 -- of a child unit that was not mentioned in the context.
5919 if Nkind (Parent (N)) = N_Package_Instantiation
5920 and then Is_Generic_Instance (Entity (Prefix (N)))
5921 and then Is_Compilation_Unit
5922 (Generic_Parent (Parent (Entity (Prefix (N)))))
5923 then
5924 Error_Msg_Node_2 := Selector;
5925 Error_Msg_N -- CODEFIX
5926 ("\missing `WITH &.&;`", Prefix (N));
5927 end if;
5928 end if;
5929 end if;
5931 Id := Any_Id;
5932 end if;
5933 end if;
5935 <<Found>>
5936 if Comes_From_Source (N)
5937 and then Is_Remote_Access_To_Subprogram_Type (Id)
5938 and then Ekind (Id) = E_Access_Subprogram_Type
5939 and then Present (Equivalent_Type (Id))
5940 then
5941 -- If we are not actually generating distribution code (i.e. the
5942 -- current PCS is the dummy non-distributed version), then the
5943 -- Equivalent_Type will be missing, and Id should be treated as
5944 -- a regular access-to-subprogram type.
5946 Id := Equivalent_Type (Id);
5947 Set_Chars (Selector, Chars (Id));
5948 end if;
5950 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
5952 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
5953 if From_Limited_With (Id)
5954 or else Is_Type (Id)
5955 or else Ekind (Id) = E_Package
5956 then
5957 null;
5958 else
5959 Error_Msg_N
5960 ("limited withed package can only be used to access "
5961 & "incomplete types", N);
5962 end if;
5963 end if;
5965 if Is_Task_Type (P_Name)
5966 and then ((Ekind (Id) = E_Entry
5967 and then Nkind (Parent (N)) /= N_Attribute_Reference)
5968 or else
5969 (Ekind (Id) = E_Entry_Family
5970 and then
5971 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
5972 then
5973 -- If both the task type and the entry are in scope, this may still
5974 -- be the expanded name of an entry formal.
5976 if In_Open_Scopes (Id)
5977 and then Nkind (Parent (N)) = N_Selected_Component
5978 then
5979 null;
5981 else
5982 -- It is an entry call after all, either to the current task
5983 -- (which will deadlock) or to an enclosing task.
5985 Analyze_Selected_Component (N);
5986 return;
5987 end if;
5988 end if;
5990 Change_Selected_Component_To_Expanded_Name (N);
5992 -- Set appropriate type
5994 if Is_Type (Id) then
5995 Set_Etype (N, Id);
5996 else
5997 Set_Etype (N, Get_Full_View (Etype (Id)));
5998 end if;
6000 -- Do style check and generate reference, but skip both steps if this
6001 -- entity has homonyms, since we may not have the right homonym set yet.
6002 -- The proper homonym will be set during the resolve phase.
6004 if Has_Homonym (Id) then
6005 Set_Entity (N, Id);
6007 else
6008 Set_Entity_Or_Discriminal (N, Id);
6010 case Is_LHS (N) is
6011 when Yes =>
6012 Generate_Reference (Id, N, 'm');
6013 when No =>
6014 Generate_Reference (Id, N, 'r');
6015 when Unknown =>
6016 Deferred_References.Append ((Id, N));
6017 end case;
6018 end if;
6020 -- Check for violation of No_Wide_Characters
6022 Check_Wide_Character_Restriction (Id, N);
6024 -- If the Ekind of the entity is Void, it means that all homonyms are
6025 -- hidden from all visibility (RM 8.3(5,14-20)).
6027 if Ekind (Id) = E_Void then
6028 Premature_Usage (N);
6030 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6031 declare
6032 H : Entity_Id := Homonym (Id);
6034 begin
6035 while Present (H) loop
6036 if Scope (H) = Scope (Id)
6037 and then (not Is_Hidden (H)
6038 or else Is_Immediately_Visible (H))
6039 then
6040 Collect_Interps (N);
6041 exit;
6042 end if;
6044 H := Homonym (H);
6045 end loop;
6047 -- If an extension of System is present, collect possible explicit
6048 -- overloadings declared in the extension.
6050 if Chars (P_Name) = Name_System
6051 and then Scope (P_Name) = Standard_Standard
6052 and then Present (System_Extend_Unit)
6053 and then Present_System_Aux (N)
6054 then
6055 H := Current_Entity (Id);
6057 while Present (H) loop
6058 if Scope (H) = System_Aux_Id then
6059 Add_One_Interp (N, H, Etype (H));
6060 end if;
6062 H := Homonym (H);
6063 end loop;
6064 end if;
6065 end;
6066 end if;
6068 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6069 and then Scope (Id) /= Standard_Standard
6070 then
6071 -- In addition to user-defined operators in the given scope, there
6072 -- may be an implicit instance of the predefined operator. The
6073 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6074 -- and added to the interpretations. Procedure Add_One_Interp will
6075 -- determine which hides which.
6077 if Has_Implicit_Operator (N) then
6078 null;
6079 end if;
6080 end if;
6082 -- If there is a single interpretation for N we can generate a
6083 -- reference to the unique entity found.
6085 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6086 Generate_Reference (Id, N);
6087 end if;
6088 end Find_Expanded_Name;
6090 -------------------------
6091 -- Find_Renamed_Entity --
6092 -------------------------
6094 function Find_Renamed_Entity
6095 (N : Node_Id;
6096 Nam : Node_Id;
6097 New_S : Entity_Id;
6098 Is_Actual : Boolean := False) return Entity_Id
6100 Ind : Interp_Index;
6101 I1 : Interp_Index := 0; -- Suppress junk warnings
6102 It : Interp;
6103 It1 : Interp;
6104 Old_S : Entity_Id;
6105 Inst : Entity_Id;
6107 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
6108 -- If the renamed entity is an implicit operator, check whether it is
6109 -- visible because its operand type is properly visible. This check
6110 -- applies to explicit renamed entities that appear in the source in a
6111 -- renaming declaration or a formal subprogram instance, but not to
6112 -- default generic actuals with a name.
6114 function Report_Overload return Entity_Id;
6115 -- List possible interpretations, and specialize message in the
6116 -- case of a generic actual.
6118 function Within (Inner, Outer : Entity_Id) return Boolean;
6119 -- Determine whether a candidate subprogram is defined within the
6120 -- enclosing instance. If yes, it has precedence over outer candidates.
6122 --------------------------
6123 -- Is_Visible_Operation --
6124 --------------------------
6126 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
6127 Scop : Entity_Id;
6128 Typ : Entity_Id;
6129 Btyp : Entity_Id;
6131 begin
6132 if Ekind (Op) /= E_Operator
6133 or else Scope (Op) /= Standard_Standard
6134 or else (In_Instance
6135 and then (not Is_Actual
6136 or else Present (Enclosing_Instance)))
6137 then
6138 return True;
6140 else
6141 -- For a fixed point type operator, check the resulting type,
6142 -- because it may be a mixed mode integer * fixed operation.
6144 if Present (Next_Formal (First_Formal (New_S)))
6145 and then Is_Fixed_Point_Type (Etype (New_S))
6146 then
6147 Typ := Etype (New_S);
6148 else
6149 Typ := Etype (First_Formal (New_S));
6150 end if;
6152 Btyp := Base_Type (Typ);
6154 if Nkind (Nam) /= N_Expanded_Name then
6155 return (In_Open_Scopes (Scope (Btyp))
6156 or else Is_Potentially_Use_Visible (Btyp)
6157 or else In_Use (Btyp)
6158 or else In_Use (Scope (Btyp)));
6160 else
6161 Scop := Entity (Prefix (Nam));
6163 if Ekind (Scop) = E_Package
6164 and then Present (Renamed_Object (Scop))
6165 then
6166 Scop := Renamed_Object (Scop);
6167 end if;
6169 -- Operator is visible if prefix of expanded name denotes
6170 -- scope of type, or else type is defined in System_Aux
6171 -- and the prefix denotes System.
6173 return Scope (Btyp) = Scop
6174 or else (Scope (Btyp) = System_Aux_Id
6175 and then Scope (Scope (Btyp)) = Scop);
6176 end if;
6177 end if;
6178 end Is_Visible_Operation;
6180 ------------
6181 -- Within --
6182 ------------
6184 function Within (Inner, Outer : Entity_Id) return Boolean is
6185 Sc : Entity_Id;
6187 begin
6188 Sc := Scope (Inner);
6189 while Sc /= Standard_Standard loop
6190 if Sc = Outer then
6191 return True;
6192 else
6193 Sc := Scope (Sc);
6194 end if;
6195 end loop;
6197 return False;
6198 end Within;
6200 ---------------------
6201 -- Report_Overload --
6202 ---------------------
6204 function Report_Overload return Entity_Id is
6205 begin
6206 if Is_Actual then
6207 Error_Msg_NE -- CODEFIX
6208 ("ambiguous actual subprogram&, " &
6209 "possible interpretations:", N, Nam);
6210 else
6211 Error_Msg_N -- CODEFIX
6212 ("ambiguous subprogram, " &
6213 "possible interpretations:", N);
6214 end if;
6216 List_Interps (Nam, N);
6217 return Old_S;
6218 end Report_Overload;
6220 -- Start of processing for Find_Renamed_Entity
6222 begin
6223 Old_S := Any_Id;
6224 Candidate_Renaming := Empty;
6226 if Is_Overloaded (Nam) then
6227 Get_First_Interp (Nam, Ind, It);
6228 while Present (It.Nam) loop
6229 if Entity_Matches_Spec (It.Nam, New_S)
6230 and then Is_Visible_Operation (It.Nam)
6231 then
6232 if Old_S /= Any_Id then
6234 -- Note: The call to Disambiguate only happens if a
6235 -- previous interpretation was found, in which case I1
6236 -- has received a value.
6238 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
6240 if It1 = No_Interp then
6241 Inst := Enclosing_Instance;
6243 if Present (Inst) then
6244 if Within (It.Nam, Inst) then
6245 if Within (Old_S, Inst) then
6247 -- Choose the innermost subprogram, which would
6248 -- have hidden the outer one in the generic.
6250 if Scope_Depth (It.Nam) <
6251 Scope_Depth (Old_S)
6252 then
6253 return Old_S;
6254 else
6255 return It.Nam;
6256 end if;
6257 end if;
6259 elsif Within (Old_S, Inst) then
6260 return (Old_S);
6262 else
6263 return Report_Overload;
6264 end if;
6266 -- If not within an instance, ambiguity is real
6268 else
6269 return Report_Overload;
6270 end if;
6272 else
6273 Old_S := It1.Nam;
6274 exit;
6275 end if;
6277 else
6278 I1 := Ind;
6279 Old_S := It.Nam;
6280 end if;
6282 elsif
6283 Present (First_Formal (It.Nam))
6284 and then Present (First_Formal (New_S))
6285 and then (Base_Type (Etype (First_Formal (It.Nam))) =
6286 Base_Type (Etype (First_Formal (New_S))))
6287 then
6288 Candidate_Renaming := It.Nam;
6289 end if;
6291 Get_Next_Interp (Ind, It);
6292 end loop;
6294 Set_Entity (Nam, Old_S);
6296 if Old_S /= Any_Id then
6297 Set_Is_Overloaded (Nam, False);
6298 end if;
6300 -- Non-overloaded case
6302 else
6303 if Is_Actual and then Present (Enclosing_Instance) then
6304 Old_S := Entity (Nam);
6306 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
6307 Candidate_Renaming := New_S;
6309 if Is_Visible_Operation (Entity (Nam)) then
6310 Old_S := Entity (Nam);
6311 end if;
6313 elsif Present (First_Formal (Entity (Nam)))
6314 and then Present (First_Formal (New_S))
6315 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
6316 Base_Type (Etype (First_Formal (New_S))))
6317 then
6318 Candidate_Renaming := Entity (Nam);
6319 end if;
6320 end if;
6322 return Old_S;
6323 end Find_Renamed_Entity;
6325 -----------------------------
6326 -- Find_Selected_Component --
6327 -----------------------------
6329 procedure Find_Selected_Component (N : Node_Id) is
6330 P : constant Node_Id := Prefix (N);
6332 P_Name : Entity_Id;
6333 -- Entity denoted by prefix
6335 P_Type : Entity_Id;
6336 -- and its type
6338 Nam : Node_Id;
6340 function Is_Reference_In_Subunit return Boolean;
6341 -- In a subunit, the scope depth is not a proper measure of hiding,
6342 -- because the context of the proper body may itself hide entities in
6343 -- parent units. This rare case requires inspecting the tree directly
6344 -- because the proper body is inserted in the main unit and its context
6345 -- is simply added to that of the parent.
6347 -----------------------------
6348 -- Is_Reference_In_Subunit --
6349 -----------------------------
6351 function Is_Reference_In_Subunit return Boolean is
6352 Clause : Node_Id;
6353 Comp_Unit : Node_Id;
6355 begin
6356 Comp_Unit := N;
6357 while Present (Comp_Unit)
6358 and then Nkind (Comp_Unit) /= N_Compilation_Unit
6359 loop
6360 Comp_Unit := Parent (Comp_Unit);
6361 end loop;
6363 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
6364 return False;
6365 end if;
6367 -- Now check whether the package is in the context of the subunit
6369 Clause := First (Context_Items (Comp_Unit));
6370 while Present (Clause) loop
6371 if Nkind (Clause) = N_With_Clause
6372 and then Entity (Name (Clause)) = P_Name
6373 then
6374 return True;
6375 end if;
6377 Clause := Next (Clause);
6378 end loop;
6380 return False;
6381 end Is_Reference_In_Subunit;
6383 -- Start of processing for Find_Selected_Component
6385 begin
6386 Analyze (P);
6388 if Nkind (P) = N_Error then
6389 return;
6390 end if;
6392 -- Selector name cannot be a character literal or an operator symbol in
6393 -- SPARK, except for the operator symbol in a renaming.
6395 if Restriction_Check_Required (SPARK_05) then
6396 if Nkind (Selector_Name (N)) = N_Character_Literal then
6397 Check_SPARK_05_Restriction
6398 ("character literal cannot be prefixed", N);
6399 elsif Nkind (Selector_Name (N)) = N_Operator_Symbol
6400 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
6401 then
6402 Check_SPARK_05_Restriction
6403 ("operator symbol cannot be prefixed", N);
6404 end if;
6405 end if;
6407 -- If the selector already has an entity, the node has been constructed
6408 -- in the course of expansion, and is known to be valid. Do not verify
6409 -- that it is defined for the type (it may be a private component used
6410 -- in the expansion of record equality).
6412 if Present (Entity (Selector_Name (N))) then
6413 if No (Etype (N)) or else Etype (N) = Any_Type then
6414 declare
6415 Sel_Name : constant Node_Id := Selector_Name (N);
6416 Selector : constant Entity_Id := Entity (Sel_Name);
6417 C_Etype : Node_Id;
6419 begin
6420 Set_Etype (Sel_Name, Etype (Selector));
6422 if not Is_Entity_Name (P) then
6423 Resolve (P);
6424 end if;
6426 -- Build an actual subtype except for the first parameter
6427 -- of an init proc, where this actual subtype is by
6428 -- definition incorrect, since the object is uninitialized
6429 -- (and does not even have defined discriminants etc.)
6431 if Is_Entity_Name (P)
6432 and then Ekind (Entity (P)) = E_Function
6433 then
6434 Nam := New_Copy (P);
6436 if Is_Overloaded (P) then
6437 Save_Interps (P, Nam);
6438 end if;
6440 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
6441 Analyze_Call (P);
6442 Analyze_Selected_Component (N);
6443 return;
6445 elsif Ekind (Selector) = E_Component
6446 and then (not Is_Entity_Name (P)
6447 or else Chars (Entity (P)) /= Name_uInit)
6448 then
6449 -- Do not build the subtype when referencing components of
6450 -- dispatch table wrappers. Required to avoid generating
6451 -- elaboration code with HI runtimes. JVM and .NET use a
6452 -- modified version of Ada.Tags which does not contain RE_
6453 -- Dispatch_Table_Wrapper and RE_No_Dispatch_Table_Wrapper.
6454 -- Avoid raising RE_Not_Available exception in those cases.
6456 if VM_Target = No_VM
6457 and then RTU_Loaded (Ada_Tags)
6458 and then
6459 ((RTE_Available (RE_Dispatch_Table_Wrapper)
6460 and then Scope (Selector) =
6461 RTE (RE_Dispatch_Table_Wrapper))
6462 or else
6463 (RTE_Available (RE_No_Dispatch_Table_Wrapper)
6464 and then Scope (Selector) =
6465 RTE (RE_No_Dispatch_Table_Wrapper)))
6466 then
6467 C_Etype := Empty;
6468 else
6469 C_Etype :=
6470 Build_Actual_Subtype_Of_Component
6471 (Etype (Selector), N);
6472 end if;
6474 else
6475 C_Etype := Empty;
6476 end if;
6478 if No (C_Etype) then
6479 C_Etype := Etype (Selector);
6480 else
6481 Insert_Action (N, C_Etype);
6482 C_Etype := Defining_Identifier (C_Etype);
6483 end if;
6485 Set_Etype (N, C_Etype);
6486 end;
6488 -- If this is the name of an entry or protected operation, and
6489 -- the prefix is an access type, insert an explicit dereference,
6490 -- so that entry calls are treated uniformly.
6492 if Is_Access_Type (Etype (P))
6493 and then Is_Concurrent_Type (Designated_Type (Etype (P)))
6494 then
6495 declare
6496 New_P : constant Node_Id :=
6497 Make_Explicit_Dereference (Sloc (P),
6498 Prefix => Relocate_Node (P));
6499 begin
6500 Rewrite (P, New_P);
6501 Set_Etype (P, Designated_Type (Etype (Prefix (P))));
6502 end;
6503 end if;
6505 -- If the selected component appears within a default expression
6506 -- and it has an actual subtype, the pre-analysis has not yet
6507 -- completed its analysis, because Insert_Actions is disabled in
6508 -- that context. Within the init proc of the enclosing type we
6509 -- must complete this analysis, if an actual subtype was created.
6511 elsif Inside_Init_Proc then
6512 declare
6513 Typ : constant Entity_Id := Etype (N);
6514 Decl : constant Node_Id := Declaration_Node (Typ);
6515 begin
6516 if Nkind (Decl) = N_Subtype_Declaration
6517 and then not Analyzed (Decl)
6518 and then Is_List_Member (Decl)
6519 and then No (Parent (Decl))
6520 then
6521 Remove (Decl);
6522 Insert_Action (N, Decl);
6523 end if;
6524 end;
6525 end if;
6527 return;
6529 elsif Is_Entity_Name (P) then
6530 P_Name := Entity (P);
6532 -- The prefix may denote an enclosing type which is the completion
6533 -- of an incomplete type declaration.
6535 if Is_Type (P_Name) then
6536 Set_Entity (P, Get_Full_View (P_Name));
6537 Set_Etype (P, Entity (P));
6538 P_Name := Entity (P);
6539 end if;
6541 P_Type := Base_Type (Etype (P));
6543 if Debug_Flag_E then
6544 Write_Str ("Found prefix type to be ");
6545 Write_Entity_Info (P_Type, " "); Write_Eol;
6546 end if;
6548 -- The designated type may be a limited view with no components.
6549 -- Check whether the non-limited view is available, because in some
6550 -- cases this will not be set when instlling the context.
6552 if Is_Access_Type (P_Type) then
6553 declare
6554 D : constant Entity_Id := Directly_Designated_Type (P_Type);
6555 begin
6556 if Is_Incomplete_Type (D)
6557 and then not Is_Class_Wide_Type (D)
6558 and then From_Limited_With (D)
6559 and then Present (Non_Limited_View (D))
6560 and then not Is_Class_Wide_Type (Non_Limited_View (D))
6561 then
6562 Set_Directly_Designated_Type (P_Type, Non_Limited_View (D));
6563 end if;
6564 end;
6565 end if;
6567 -- First check for components of a record object (not the
6568 -- result of a call, which is handled below).
6570 if Is_Appropriate_For_Record (P_Type)
6571 and then not Is_Overloadable (P_Name)
6572 and then not Is_Type (P_Name)
6573 then
6574 -- Selected component of record. Type checking will validate
6575 -- name of selector.
6577 -- ??? Could we rewrite an implicit dereference into an explicit
6578 -- one here?
6580 Analyze_Selected_Component (N);
6582 -- Reference to type name in predicate/invariant expression
6584 elsif Is_Appropriate_For_Entry_Prefix (P_Type)
6585 and then not In_Open_Scopes (P_Name)
6586 and then (not Is_Concurrent_Type (Etype (P_Name))
6587 or else not In_Open_Scopes (Etype (P_Name)))
6588 then
6589 -- Call to protected operation or entry. Type checking is
6590 -- needed on the prefix.
6592 Analyze_Selected_Component (N);
6594 elsif (In_Open_Scopes (P_Name)
6595 and then Ekind (P_Name) /= E_Void
6596 and then not Is_Overloadable (P_Name))
6597 or else (Is_Concurrent_Type (Etype (P_Name))
6598 and then In_Open_Scopes (Etype (P_Name)))
6599 then
6600 -- Prefix denotes an enclosing loop, block, or task, i.e. an
6601 -- enclosing construct that is not a subprogram or accept.
6603 Find_Expanded_Name (N);
6605 elsif Ekind (P_Name) = E_Package then
6606 Find_Expanded_Name (N);
6608 elsif Is_Overloadable (P_Name) then
6610 -- The subprogram may be a renaming (of an enclosing scope) as
6611 -- in the case of the name of the generic within an instantiation.
6613 if Ekind_In (P_Name, E_Procedure, E_Function)
6614 and then Present (Alias (P_Name))
6615 and then Is_Generic_Instance (Alias (P_Name))
6616 then
6617 P_Name := Alias (P_Name);
6618 end if;
6620 if Is_Overloaded (P) then
6622 -- The prefix must resolve to a unique enclosing construct
6624 declare
6625 Found : Boolean := False;
6626 Ind : Interp_Index;
6627 It : Interp;
6629 begin
6630 Get_First_Interp (P, Ind, It);
6631 while Present (It.Nam) loop
6632 if In_Open_Scopes (It.Nam) then
6633 if Found then
6634 Error_Msg_N (
6635 "prefix must be unique enclosing scope", N);
6636 Set_Entity (N, Any_Id);
6637 Set_Etype (N, Any_Type);
6638 return;
6640 else
6641 Found := True;
6642 P_Name := It.Nam;
6643 end if;
6644 end if;
6646 Get_Next_Interp (Ind, It);
6647 end loop;
6648 end;
6649 end if;
6651 if In_Open_Scopes (P_Name) then
6652 Set_Entity (P, P_Name);
6653 Set_Is_Overloaded (P, False);
6654 Find_Expanded_Name (N);
6656 else
6657 -- If no interpretation as an expanded name is possible, it
6658 -- must be a selected component of a record returned by a
6659 -- function call. Reformat prefix as a function call, the rest
6660 -- is done by type resolution.
6662 -- Error if the prefix is procedure or entry, as is P.X
6664 if Ekind (P_Name) /= E_Function
6665 and then
6666 (not Is_Overloaded (P)
6667 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
6668 then
6669 -- Prefix may mention a package that is hidden by a local
6670 -- declaration: let the user know. Scan the full homonym
6671 -- chain, the candidate package may be anywhere on it.
6673 if Present (Homonym (Current_Entity (P_Name))) then
6674 P_Name := Current_Entity (P_Name);
6676 while Present (P_Name) loop
6677 exit when Ekind (P_Name) = E_Package;
6678 P_Name := Homonym (P_Name);
6679 end loop;
6681 if Present (P_Name) then
6682 if not Is_Reference_In_Subunit then
6683 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
6684 Error_Msg_NE
6685 ("package& is hidden by declaration#", N, P_Name);
6686 end if;
6688 Set_Entity (Prefix (N), P_Name);
6689 Find_Expanded_Name (N);
6690 return;
6692 else
6693 P_Name := Entity (Prefix (N));
6694 end if;
6695 end if;
6697 Error_Msg_NE
6698 ("invalid prefix in selected component&", N, P_Name);
6699 Change_Selected_Component_To_Expanded_Name (N);
6700 Set_Entity (N, Any_Id);
6701 Set_Etype (N, Any_Type);
6703 -- Here we have a function call, so do the reformatting
6705 else
6706 Nam := New_Copy (P);
6707 Save_Interps (P, Nam);
6709 -- We use Replace here because this is one of those cases
6710 -- where the parser has missclassified the node, and we
6711 -- fix things up and then do the semantic analysis on the
6712 -- fixed up node. Normally we do this using one of the
6713 -- Sinfo.CN routines, but this is too tricky for that.
6715 -- Note that using Rewrite would be wrong, because we
6716 -- would have a tree where the original node is unanalyzed,
6717 -- and this violates the required interface for ASIS.
6719 Replace (P,
6720 Make_Function_Call (Sloc (P), Name => Nam));
6722 -- Now analyze the reformatted node
6724 Analyze_Call (P);
6725 Analyze_Selected_Component (N);
6726 end if;
6727 end if;
6729 -- Remaining cases generate various error messages
6731 else
6732 -- Format node as expanded name, to avoid cascaded errors
6734 Change_Selected_Component_To_Expanded_Name (N);
6735 Set_Entity (N, Any_Id);
6736 Set_Etype (N, Any_Type);
6738 -- Issue error message, but avoid this if error issued already.
6739 -- Use identifier of prefix if one is available.
6741 if P_Name = Any_Id then
6742 null;
6744 elsif Ekind (P_Name) = E_Void then
6745 Premature_Usage (P);
6747 elsif Nkind (P) /= N_Attribute_Reference then
6748 Error_Msg_N (
6749 "invalid prefix in selected component&", P);
6751 if Is_Access_Type (P_Type)
6752 and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
6753 then
6754 Error_Msg_N
6755 ("\dereference must not be of an incomplete type " &
6756 "(RM 3.10.1)", P);
6757 end if;
6759 else
6760 Error_Msg_N (
6761 "invalid prefix in selected component", P);
6762 end if;
6763 end if;
6765 -- Selector name is restricted in SPARK
6767 if Nkind (N) = N_Expanded_Name
6768 and then Restriction_Check_Required (SPARK_05)
6769 then
6770 if Is_Subprogram (P_Name) then
6771 Check_SPARK_05_Restriction
6772 ("prefix of expanded name cannot be a subprogram", P);
6773 elsif Ekind (P_Name) = E_Loop then
6774 Check_SPARK_05_Restriction
6775 ("prefix of expanded name cannot be a loop statement", P);
6776 end if;
6777 end if;
6779 else
6780 -- If prefix is not the name of an entity, it must be an expression,
6781 -- whose type is appropriate for a record. This is determined by
6782 -- type resolution.
6784 Analyze_Selected_Component (N);
6785 end if;
6787 Analyze_Dimension (N);
6788 end Find_Selected_Component;
6790 ---------------
6791 -- Find_Type --
6792 ---------------
6794 procedure Find_Type (N : Node_Id) is
6795 C : Entity_Id;
6796 Typ : Entity_Id;
6797 T : Entity_Id;
6798 T_Name : Entity_Id;
6800 begin
6801 if N = Error then
6802 return;
6804 elsif Nkind (N) = N_Attribute_Reference then
6806 -- Class attribute. This is not valid in Ada 83 mode, but we do not
6807 -- need to enforce that at this point, since the declaration of the
6808 -- tagged type in the prefix would have been flagged already.
6810 if Attribute_Name (N) = Name_Class then
6811 Check_Restriction (No_Dispatch, N);
6812 Find_Type (Prefix (N));
6814 -- Propagate error from bad prefix
6816 if Etype (Prefix (N)) = Any_Type then
6817 Set_Entity (N, Any_Type);
6818 Set_Etype (N, Any_Type);
6819 return;
6820 end if;
6822 T := Base_Type (Entity (Prefix (N)));
6824 -- Case where type is not known to be tagged. Its appearance in
6825 -- the prefix of the 'Class attribute indicates that the full view
6826 -- will be tagged.
6828 if not Is_Tagged_Type (T) then
6829 if Ekind (T) = E_Incomplete_Type then
6831 -- It is legal to denote the class type of an incomplete
6832 -- type. The full type will have to be tagged, of course.
6833 -- In Ada 2005 this usage is declared obsolescent, so we
6834 -- warn accordingly. This usage is only legal if the type
6835 -- is completed in the current scope, and not for a limited
6836 -- view of a type.
6838 if Ada_Version >= Ada_2005 then
6840 -- Test whether the Available_View of a limited type view
6841 -- is tagged, since the limited view may not be marked as
6842 -- tagged if the type itself has an untagged incomplete
6843 -- type view in its package.
6845 if From_Limited_With (T)
6846 and then not Is_Tagged_Type (Available_View (T))
6847 then
6848 Error_Msg_N
6849 ("prefix of Class attribute must be tagged", N);
6850 Set_Etype (N, Any_Type);
6851 Set_Entity (N, Any_Type);
6852 return;
6854 -- ??? This test is temporarily disabled (always
6855 -- False) because it causes an unwanted warning on
6856 -- GNAT sources (built with -gnatg, which includes
6857 -- Warn_On_Obsolescent_ Feature). Once this issue
6858 -- is cleared in the sources, it can be enabled.
6860 elsif Warn_On_Obsolescent_Feature and then False then
6861 Error_Msg_N
6862 ("applying 'Class to an untagged incomplete type"
6863 & " is an obsolescent feature (RM J.11)?r?", N);
6864 end if;
6865 end if;
6867 Set_Is_Tagged_Type (T);
6868 Set_Direct_Primitive_Operations (T, New_Elmt_List);
6869 Make_Class_Wide_Type (T);
6870 Set_Entity (N, Class_Wide_Type (T));
6871 Set_Etype (N, Class_Wide_Type (T));
6873 elsif Ekind (T) = E_Private_Type
6874 and then not Is_Generic_Type (T)
6875 and then In_Private_Part (Scope (T))
6876 then
6877 -- The Class attribute can be applied to an untagged private
6878 -- type fulfilled by a tagged type prior to the full type
6879 -- declaration (but only within the parent package's private
6880 -- part). Create the class-wide type now and check that the
6881 -- full type is tagged later during its analysis. Note that
6882 -- we do not mark the private type as tagged, unlike the
6883 -- case of incomplete types, because the type must still
6884 -- appear untagged to outside units.
6886 if No (Class_Wide_Type (T)) then
6887 Make_Class_Wide_Type (T);
6888 end if;
6890 Set_Entity (N, Class_Wide_Type (T));
6891 Set_Etype (N, Class_Wide_Type (T));
6893 else
6894 -- Should we introduce a type Any_Tagged and use Wrong_Type
6895 -- here, it would be a bit more consistent???
6897 Error_Msg_NE
6898 ("tagged type required, found}",
6899 Prefix (N), First_Subtype (T));
6900 Set_Entity (N, Any_Type);
6901 return;
6902 end if;
6904 -- Case of tagged type
6906 else
6907 if Is_Concurrent_Type (T) then
6908 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
6910 -- Previous error. Use current type, which at least
6911 -- provides some operations.
6913 C := Entity (Prefix (N));
6915 else
6916 C := Class_Wide_Type
6917 (Corresponding_Record_Type (Entity (Prefix (N))));
6918 end if;
6920 else
6921 C := Class_Wide_Type (Entity (Prefix (N)));
6922 end if;
6924 Set_Entity_With_Checks (N, C);
6925 Generate_Reference (C, N);
6926 Set_Etype (N, C);
6927 end if;
6929 -- Base attribute, not allowed in Ada 83
6931 elsif Attribute_Name (N) = Name_Base then
6932 Error_Msg_Name_1 := Name_Base;
6933 Check_SPARK_05_Restriction
6934 ("attribute% is only allowed as prefix of another attribute", N);
6936 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
6937 Error_Msg_N
6938 ("(Ada 83) Base attribute not allowed in subtype mark", N);
6940 else
6941 Find_Type (Prefix (N));
6942 Typ := Entity (Prefix (N));
6944 if Ada_Version >= Ada_95
6945 and then not Is_Scalar_Type (Typ)
6946 and then not Is_Generic_Type (Typ)
6947 then
6948 Error_Msg_N
6949 ("prefix of Base attribute must be scalar type",
6950 Prefix (N));
6952 elsif Warn_On_Redundant_Constructs
6953 and then Base_Type (Typ) = Typ
6954 then
6955 Error_Msg_NE -- CODEFIX
6956 ("redundant attribute, & is its own base type?r?", N, Typ);
6957 end if;
6959 T := Base_Type (Typ);
6961 -- Rewrite attribute reference with type itself (see similar
6962 -- processing in Analyze_Attribute, case Base). Preserve prefix
6963 -- if present, for other legality checks.
6965 if Nkind (Prefix (N)) = N_Expanded_Name then
6966 Rewrite (N,
6967 Make_Expanded_Name (Sloc (N),
6968 Chars => Chars (T),
6969 Prefix => New_Copy (Prefix (Prefix (N))),
6970 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
6972 else
6973 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
6974 end if;
6976 Set_Entity (N, T);
6977 Set_Etype (N, T);
6978 end if;
6980 elsif Attribute_Name (N) = Name_Stub_Type then
6982 -- This is handled in Analyze_Attribute
6984 Analyze (N);
6986 -- All other attributes are invalid in a subtype mark
6988 else
6989 Error_Msg_N ("invalid attribute in subtype mark", N);
6990 end if;
6992 else
6993 Analyze (N);
6995 if Is_Entity_Name (N) then
6996 T_Name := Entity (N);
6997 else
6998 Error_Msg_N ("subtype mark required in this context", N);
6999 Set_Etype (N, Any_Type);
7000 return;
7001 end if;
7003 if T_Name = Any_Id or else Etype (N) = Any_Type then
7005 -- Undefined id. Make it into a valid type
7007 Set_Entity (N, Any_Type);
7009 elsif not Is_Type (T_Name)
7010 and then T_Name /= Standard_Void_Type
7011 then
7012 Error_Msg_Sloc := Sloc (T_Name);
7013 Error_Msg_N ("subtype mark required in this context", N);
7014 Error_Msg_NE ("\\found & declared#", N, T_Name);
7015 Set_Entity (N, Any_Type);
7017 else
7018 -- If the type is an incomplete type created to handle
7019 -- anonymous access components of a record type, then the
7020 -- incomplete type is the visible entity and subsequent
7021 -- references will point to it. Mark the original full
7022 -- type as referenced, to prevent spurious warnings.
7024 if Is_Incomplete_Type (T_Name)
7025 and then Present (Full_View (T_Name))
7026 and then not Comes_From_Source (T_Name)
7027 then
7028 Set_Referenced (Full_View (T_Name));
7029 end if;
7031 T_Name := Get_Full_View (T_Name);
7033 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
7034 -- limited-with clauses
7036 if From_Limited_With (T_Name)
7037 and then Ekind (T_Name) in Incomplete_Kind
7038 and then Present (Non_Limited_View (T_Name))
7039 and then Is_Interface (Non_Limited_View (T_Name))
7040 then
7041 T_Name := Non_Limited_View (T_Name);
7042 end if;
7044 if In_Open_Scopes (T_Name) then
7045 if Ekind (Base_Type (T_Name)) = E_Task_Type then
7047 -- In Ada 2005, a task name can be used in an access
7048 -- definition within its own body. It cannot be used
7049 -- in the discriminant part of the task declaration,
7050 -- nor anywhere else in the declaration because entries
7051 -- cannot have access parameters.
7053 if Ada_Version >= Ada_2005
7054 and then Nkind (Parent (N)) = N_Access_Definition
7055 then
7056 Set_Entity (N, T_Name);
7057 Set_Etype (N, T_Name);
7059 if Has_Completion (T_Name) then
7060 return;
7062 else
7063 Error_Msg_N
7064 ("task type cannot be used as type mark " &
7065 "within its own declaration", N);
7066 end if;
7068 else
7069 Error_Msg_N
7070 ("task type cannot be used as type mark " &
7071 "within its own spec or body", N);
7072 end if;
7074 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
7076 -- In Ada 2005, a protected name can be used in an access
7077 -- definition within its own body.
7079 if Ada_Version >= Ada_2005
7080 and then Nkind (Parent (N)) = N_Access_Definition
7081 then
7082 Set_Entity (N, T_Name);
7083 Set_Etype (N, T_Name);
7084 return;
7086 else
7087 Error_Msg_N
7088 ("protected type cannot be used as type mark " &
7089 "within its own spec or body", N);
7090 end if;
7092 else
7093 Error_Msg_N ("type declaration cannot refer to itself", N);
7094 end if;
7096 Set_Etype (N, Any_Type);
7097 Set_Entity (N, Any_Type);
7098 Set_Error_Posted (T_Name);
7099 return;
7100 end if;
7102 Set_Entity (N, T_Name);
7103 Set_Etype (N, T_Name);
7104 end if;
7105 end if;
7107 if Present (Etype (N)) and then Comes_From_Source (N) then
7108 if Is_Fixed_Point_Type (Etype (N)) then
7109 Check_Restriction (No_Fixed_Point, N);
7110 elsif Is_Floating_Point_Type (Etype (N)) then
7111 Check_Restriction (No_Floating_Point, N);
7112 end if;
7113 end if;
7114 end Find_Type;
7116 ------------------------------------
7117 -- Has_Implicit_Character_Literal --
7118 ------------------------------------
7120 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
7121 Id : Entity_Id;
7122 Found : Boolean := False;
7123 P : constant Entity_Id := Entity (Prefix (N));
7124 Priv_Id : Entity_Id := Empty;
7126 begin
7127 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
7128 Priv_Id := First_Private_Entity (P);
7129 end if;
7131 if P = Standard_Standard then
7132 Change_Selected_Component_To_Expanded_Name (N);
7133 Rewrite (N, Selector_Name (N));
7134 Analyze (N);
7135 Set_Etype (Original_Node (N), Standard_Character);
7136 return True;
7137 end if;
7139 Id := First_Entity (P);
7140 while Present (Id) and then Id /= Priv_Id loop
7141 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
7143 -- We replace the node with the literal itself, resolve as a
7144 -- character, and set the type correctly.
7146 if not Found then
7147 Change_Selected_Component_To_Expanded_Name (N);
7148 Rewrite (N, Selector_Name (N));
7149 Analyze (N);
7150 Set_Etype (N, Id);
7151 Set_Etype (Original_Node (N), Id);
7152 Found := True;
7154 else
7155 -- More than one type derived from Character in given scope.
7156 -- Collect all possible interpretations.
7158 Add_One_Interp (N, Id, Id);
7159 end if;
7160 end if;
7162 Next_Entity (Id);
7163 end loop;
7165 return Found;
7166 end Has_Implicit_Character_Literal;
7168 ----------------------
7169 -- Has_Private_With --
7170 ----------------------
7172 function Has_Private_With (E : Entity_Id) return Boolean is
7173 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
7174 Item : Node_Id;
7176 begin
7177 Item := First (Context_Items (Comp_Unit));
7178 while Present (Item) loop
7179 if Nkind (Item) = N_With_Clause
7180 and then Private_Present (Item)
7181 and then Entity (Name (Item)) = E
7182 then
7183 return True;
7184 end if;
7186 Next (Item);
7187 end loop;
7189 return False;
7190 end Has_Private_With;
7192 ---------------------------
7193 -- Has_Implicit_Operator --
7194 ---------------------------
7196 function Has_Implicit_Operator (N : Node_Id) return Boolean is
7197 Op_Id : constant Name_Id := Chars (Selector_Name (N));
7198 P : constant Entity_Id := Entity (Prefix (N));
7199 Id : Entity_Id;
7200 Priv_Id : Entity_Id := Empty;
7202 procedure Add_Implicit_Operator
7203 (T : Entity_Id;
7204 Op_Type : Entity_Id := Empty);
7205 -- Add implicit interpretation to node N, using the type for which a
7206 -- predefined operator exists. If the operator yields a boolean type,
7207 -- the Operand_Type is implicitly referenced by the operator, and a
7208 -- reference to it must be generated.
7210 ---------------------------
7211 -- Add_Implicit_Operator --
7212 ---------------------------
7214 procedure Add_Implicit_Operator
7215 (T : Entity_Id;
7216 Op_Type : Entity_Id := Empty)
7218 Predef_Op : Entity_Id;
7220 begin
7221 Predef_Op := Current_Entity (Selector_Name (N));
7222 while Present (Predef_Op)
7223 and then Scope (Predef_Op) /= Standard_Standard
7224 loop
7225 Predef_Op := Homonym (Predef_Op);
7226 end loop;
7228 if Nkind (N) = N_Selected_Component then
7229 Change_Selected_Component_To_Expanded_Name (N);
7230 end if;
7232 -- If the context is an unanalyzed function call, determine whether
7233 -- a binary or unary interpretation is required.
7235 if Nkind (Parent (N)) = N_Indexed_Component then
7236 declare
7237 Is_Binary_Call : constant Boolean :=
7238 Present
7239 (Next (First (Expressions (Parent (N)))));
7240 Is_Binary_Op : constant Boolean :=
7241 First_Entity
7242 (Predef_Op) /= Last_Entity (Predef_Op);
7243 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
7245 begin
7246 if Is_Binary_Call then
7247 if Is_Binary_Op then
7248 Add_One_Interp (N, Predef_Op, T);
7249 else
7250 Add_One_Interp (N, Predef_Op2, T);
7251 end if;
7253 else
7254 if not Is_Binary_Op then
7255 Add_One_Interp (N, Predef_Op, T);
7256 else
7257 Add_One_Interp (N, Predef_Op2, T);
7258 end if;
7259 end if;
7260 end;
7262 else
7263 Add_One_Interp (N, Predef_Op, T);
7265 -- For operators with unary and binary interpretations, if
7266 -- context is not a call, add both
7268 if Present (Homonym (Predef_Op)) then
7269 Add_One_Interp (N, Homonym (Predef_Op), T);
7270 end if;
7271 end if;
7273 -- The node is a reference to a predefined operator, and
7274 -- an implicit reference to the type of its operands.
7276 if Present (Op_Type) then
7277 Generate_Operator_Reference (N, Op_Type);
7278 else
7279 Generate_Operator_Reference (N, T);
7280 end if;
7281 end Add_Implicit_Operator;
7283 -- Start of processing for Has_Implicit_Operator
7285 begin
7286 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
7287 Priv_Id := First_Private_Entity (P);
7288 end if;
7290 Id := First_Entity (P);
7292 case Op_Id is
7294 -- Boolean operators: an implicit declaration exists if the scope
7295 -- contains a declaration for a derived Boolean type, or for an
7296 -- array of Boolean type.
7298 when Name_Op_And | Name_Op_Not | Name_Op_Or | Name_Op_Xor =>
7299 while Id /= Priv_Id loop
7300 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
7301 Add_Implicit_Operator (Id);
7302 return True;
7303 end if;
7305 Next_Entity (Id);
7306 end loop;
7308 -- Equality: look for any non-limited type (result is Boolean)
7310 when Name_Op_Eq | Name_Op_Ne =>
7311 while Id /= Priv_Id loop
7312 if Is_Type (Id)
7313 and then not Is_Limited_Type (Id)
7314 and then Is_Base_Type (Id)
7315 then
7316 Add_Implicit_Operator (Standard_Boolean, Id);
7317 return True;
7318 end if;
7320 Next_Entity (Id);
7321 end loop;
7323 -- Comparison operators: scalar type, or array of scalar
7325 when Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge =>
7326 while Id /= Priv_Id loop
7327 if (Is_Scalar_Type (Id)
7328 or else (Is_Array_Type (Id)
7329 and then Is_Scalar_Type (Component_Type (Id))))
7330 and then Is_Base_Type (Id)
7331 then
7332 Add_Implicit_Operator (Standard_Boolean, Id);
7333 return True;
7334 end if;
7336 Next_Entity (Id);
7337 end loop;
7339 -- Arithmetic operators: any numeric type
7341 when Name_Op_Abs |
7342 Name_Op_Add |
7343 Name_Op_Mod |
7344 Name_Op_Rem |
7345 Name_Op_Subtract |
7346 Name_Op_Multiply |
7347 Name_Op_Divide |
7348 Name_Op_Expon =>
7349 while Id /= Priv_Id loop
7350 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
7351 Add_Implicit_Operator (Id);
7352 return True;
7353 end if;
7355 Next_Entity (Id);
7356 end loop;
7358 -- Concatenation: any one-dimensional array type
7360 when Name_Op_Concat =>
7361 while Id /= Priv_Id loop
7362 if Is_Array_Type (Id)
7363 and then Number_Dimensions (Id) = 1
7364 and then Is_Base_Type (Id)
7365 then
7366 Add_Implicit_Operator (Id);
7367 return True;
7368 end if;
7370 Next_Entity (Id);
7371 end loop;
7373 -- What is the others condition here? Should we be using a
7374 -- subtype of Name_Id that would restrict to operators ???
7376 when others => null;
7377 end case;
7379 -- If we fall through, then we do not have an implicit operator
7381 return False;
7383 end Has_Implicit_Operator;
7385 -----------------------------------
7386 -- Has_Loop_In_Inner_Open_Scopes --
7387 -----------------------------------
7389 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
7390 begin
7391 -- Several scope stacks are maintained by Scope_Stack. The base of the
7392 -- currently active scope stack is denoted by the Is_Active_Stack_Base
7393 -- flag in the scope stack entry. Note that the scope stacks used to
7394 -- simply be delimited implicitly by the presence of Standard_Standard
7395 -- at their base, but there now are cases where this is not sufficient
7396 -- because Standard_Standard actually may appear in the middle of the
7397 -- active set of scopes.
7399 for J in reverse 0 .. Scope_Stack.Last loop
7401 -- S was reached without seing a loop scope first
7403 if Scope_Stack.Table (J).Entity = S then
7404 return False;
7406 -- S was not yet reached, so it contains at least one inner loop
7408 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
7409 return True;
7410 end if;
7412 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
7413 -- cases where Standard_Standard appears in the middle of the active
7414 -- set of scopes. This affects the declaration and overriding of
7415 -- private inherited operations in instantiations of generic child
7416 -- units.
7418 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
7419 end loop;
7421 raise Program_Error; -- unreachable
7422 end Has_Loop_In_Inner_Open_Scopes;
7424 --------------------
7425 -- In_Open_Scopes --
7426 --------------------
7428 function In_Open_Scopes (S : Entity_Id) return Boolean is
7429 begin
7430 -- Several scope stacks are maintained by Scope_Stack. The base of the
7431 -- currently active scope stack is denoted by the Is_Active_Stack_Base
7432 -- flag in the scope stack entry. Note that the scope stacks used to
7433 -- simply be delimited implicitly by the presence of Standard_Standard
7434 -- at their base, but there now are cases where this is not sufficient
7435 -- because Standard_Standard actually may appear in the middle of the
7436 -- active set of scopes.
7438 for J in reverse 0 .. Scope_Stack.Last loop
7439 if Scope_Stack.Table (J).Entity = S then
7440 return True;
7441 end if;
7443 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
7444 -- cases where Standard_Standard appears in the middle of the active
7445 -- set of scopes. This affects the declaration and overriding of
7446 -- private inherited operations in instantiations of generic child
7447 -- units.
7449 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
7450 end loop;
7452 return False;
7453 end In_Open_Scopes;
7455 -----------------------------
7456 -- Inherit_Renamed_Profile --
7457 -----------------------------
7459 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
7460 New_F : Entity_Id;
7461 Old_F : Entity_Id;
7462 Old_T : Entity_Id;
7463 New_T : Entity_Id;
7465 begin
7466 if Ekind (Old_S) = E_Operator then
7467 New_F := First_Formal (New_S);
7469 while Present (New_F) loop
7470 Set_Etype (New_F, Base_Type (Etype (New_F)));
7471 Next_Formal (New_F);
7472 end loop;
7474 Set_Etype (New_S, Base_Type (Etype (New_S)));
7476 else
7477 New_F := First_Formal (New_S);
7478 Old_F := First_Formal (Old_S);
7480 while Present (New_F) loop
7481 New_T := Etype (New_F);
7482 Old_T := Etype (Old_F);
7484 -- If the new type is a renaming of the old one, as is the
7485 -- case for actuals in instances, retain its name, to simplify
7486 -- later disambiguation.
7488 if Nkind (Parent (New_T)) = N_Subtype_Declaration
7489 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
7490 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
7491 then
7492 null;
7493 else
7494 Set_Etype (New_F, Old_T);
7495 end if;
7497 Next_Formal (New_F);
7498 Next_Formal (Old_F);
7499 end loop;
7501 if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then
7502 Set_Etype (New_S, Etype (Old_S));
7503 end if;
7504 end if;
7505 end Inherit_Renamed_Profile;
7507 ----------------
7508 -- Initialize --
7509 ----------------
7511 procedure Initialize is
7512 begin
7513 Urefs.Init;
7514 end Initialize;
7516 -------------------------
7517 -- Install_Use_Clauses --
7518 -------------------------
7520 procedure Install_Use_Clauses
7521 (Clause : Node_Id;
7522 Force_Installation : Boolean := False)
7524 U : Node_Id;
7525 P : Node_Id;
7526 Id : Entity_Id;
7528 begin
7529 U := Clause;
7530 while Present (U) loop
7532 -- Case of USE package
7534 if Nkind (U) = N_Use_Package_Clause then
7535 P := First (Names (U));
7536 while Present (P) loop
7537 Id := Entity (P);
7539 if Ekind (Id) = E_Package then
7540 if In_Use (Id) then
7541 Note_Redundant_Use (P);
7543 elsif Present (Renamed_Object (Id))
7544 and then In_Use (Renamed_Object (Id))
7545 then
7546 Note_Redundant_Use (P);
7548 elsif Force_Installation or else Applicable_Use (P) then
7549 Use_One_Package (Id, U);
7551 end if;
7552 end if;
7554 Next (P);
7555 end loop;
7557 -- Case of USE TYPE
7559 else
7560 P := First (Subtype_Marks (U));
7561 while Present (P) loop
7562 if not Is_Entity_Name (P)
7563 or else No (Entity (P))
7564 then
7565 null;
7567 elsif Entity (P) /= Any_Type then
7568 Use_One_Type (P);
7569 end if;
7571 Next (P);
7572 end loop;
7573 end if;
7575 Next_Use_Clause (U);
7576 end loop;
7577 end Install_Use_Clauses;
7579 -------------------------------------
7580 -- Is_Appropriate_For_Entry_Prefix --
7581 -------------------------------------
7583 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
7584 P_Type : Entity_Id := T;
7586 begin
7587 if Is_Access_Type (P_Type) then
7588 P_Type := Designated_Type (P_Type);
7589 end if;
7591 return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
7592 end Is_Appropriate_For_Entry_Prefix;
7594 -------------------------------
7595 -- Is_Appropriate_For_Record --
7596 -------------------------------
7598 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is
7600 function Has_Components (T1 : Entity_Id) return Boolean;
7601 -- Determine if given type has components (i.e. is either a record
7602 -- type or a type that has discriminants).
7604 --------------------
7605 -- Has_Components --
7606 --------------------
7608 function Has_Components (T1 : Entity_Id) return Boolean is
7609 begin
7610 return Is_Record_Type (T1)
7611 or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
7612 or else (Is_Task_Type (T1) and then Has_Discriminants (T1))
7613 or else (Is_Incomplete_Type (T1)
7614 and then From_Limited_With (T1)
7615 and then Present (Non_Limited_View (T1))
7616 and then Is_Record_Type
7617 (Get_Full_View (Non_Limited_View (T1))));
7618 end Has_Components;
7620 -- Start of processing for Is_Appropriate_For_Record
7622 begin
7623 return
7624 Present (T)
7625 and then (Has_Components (T)
7626 or else (Is_Access_Type (T)
7627 and then Has_Components (Designated_Type (T))));
7628 end Is_Appropriate_For_Record;
7630 ------------------------
7631 -- Note_Redundant_Use --
7632 ------------------------
7634 procedure Note_Redundant_Use (Clause : Node_Id) is
7635 Pack_Name : constant Entity_Id := Entity (Clause);
7636 Cur_Use : constant Node_Id := Current_Use_Clause (Pack_Name);
7637 Decl : constant Node_Id := Parent (Clause);
7639 Prev_Use : Node_Id := Empty;
7640 Redundant : Node_Id := Empty;
7641 -- The Use_Clause which is actually redundant. In the simplest case it
7642 -- is Pack itself, but when we compile a body we install its context
7643 -- before that of its spec, in which case it is the use_clause in the
7644 -- spec that will appear to be redundant, and we want the warning to be
7645 -- placed on the body. Similar complications appear when the redundancy
7646 -- is between a child unit and one of its ancestors.
7648 begin
7649 Set_Redundant_Use (Clause, True);
7651 if not Comes_From_Source (Clause)
7652 or else In_Instance
7653 or else not Warn_On_Redundant_Constructs
7654 then
7655 return;
7656 end if;
7658 if not Is_Compilation_Unit (Current_Scope) then
7660 -- If the use_clause is in an inner scope, it is made redundant by
7661 -- some clause in the current context, with one exception: If we're
7662 -- compiling a nested package body, and the use_clause comes from the
7663 -- corresponding spec, the clause is not necessarily fully redundant,
7664 -- so we should not warn. If a warning was warranted, it would have
7665 -- been given when the spec was processed.
7667 if Nkind (Parent (Decl)) = N_Package_Specification then
7668 declare
7669 Package_Spec_Entity : constant Entity_Id :=
7670 Defining_Unit_Name (Parent (Decl));
7671 begin
7672 if In_Package_Body (Package_Spec_Entity) then
7673 return;
7674 end if;
7675 end;
7676 end if;
7678 Redundant := Clause;
7679 Prev_Use := Cur_Use;
7681 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
7682 declare
7683 Cur_Unit : constant Unit_Number_Type := Get_Source_Unit (Cur_Use);
7684 New_Unit : constant Unit_Number_Type := Get_Source_Unit (Clause);
7685 Scop : Entity_Id;
7687 begin
7688 if Cur_Unit = New_Unit then
7690 -- Redundant clause in same body
7692 Redundant := Clause;
7693 Prev_Use := Cur_Use;
7695 elsif Cur_Unit = Current_Sem_Unit then
7697 -- If the new clause is not in the current unit it has been
7698 -- analyzed first, and it makes the other one redundant.
7699 -- However, if the new clause appears in a subunit, Cur_Unit
7700 -- is still the parent, and in that case the redundant one
7701 -- is the one appearing in the subunit.
7703 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
7704 Redundant := Clause;
7705 Prev_Use := Cur_Use;
7707 -- Most common case: redundant clause in body,
7708 -- original clause in spec. Current scope is spec entity.
7710 elsif
7711 Current_Scope =
7712 Defining_Entity (
7713 Unit (Library_Unit (Cunit (Current_Sem_Unit))))
7714 then
7715 Redundant := Cur_Use;
7716 Prev_Use := Clause;
7718 else
7719 -- The new clause may appear in an unrelated unit, when
7720 -- the parents of a generic are being installed prior to
7721 -- instantiation. In this case there must be no warning.
7722 -- We detect this case by checking whether the current top
7723 -- of the stack is related to the current compilation.
7725 Scop := Current_Scope;
7726 while Present (Scop) and then Scop /= Standard_Standard loop
7727 if Is_Compilation_Unit (Scop)
7728 and then not Is_Child_Unit (Scop)
7729 then
7730 return;
7732 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
7733 exit;
7734 end if;
7736 Scop := Scope (Scop);
7737 end loop;
7739 Redundant := Cur_Use;
7740 Prev_Use := Clause;
7741 end if;
7743 elsif New_Unit = Current_Sem_Unit then
7744 Redundant := Clause;
7745 Prev_Use := Cur_Use;
7747 else
7748 -- Neither is the current unit, so they appear in parent or
7749 -- sibling units. Warning will be emitted elsewhere.
7751 return;
7752 end if;
7753 end;
7755 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
7756 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
7757 then
7758 -- Use_clause is in child unit of current unit, and the child unit
7759 -- appears in the context of the body of the parent, so it has been
7760 -- installed first, even though it is the redundant one. Depending on
7761 -- their placement in the context, the visible or the private parts
7762 -- of the two units, either might appear as redundant, but the
7763 -- message has to be on the current unit.
7765 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
7766 Redundant := Cur_Use;
7767 Prev_Use := Clause;
7768 else
7769 Redundant := Clause;
7770 Prev_Use := Cur_Use;
7771 end if;
7773 -- If the new use clause appears in the private part of a parent unit
7774 -- it may appear to be redundant w.r.t. a use clause in a child unit,
7775 -- but the previous use clause was needed in the visible part of the
7776 -- child, and no warning should be emitted.
7778 if Nkind (Parent (Decl)) = N_Package_Specification
7779 and then
7780 List_Containing (Decl) = Private_Declarations (Parent (Decl))
7781 then
7782 declare
7783 Par : constant Entity_Id := Defining_Entity (Parent (Decl));
7784 Spec : constant Node_Id :=
7785 Specification (Unit (Cunit (Current_Sem_Unit)));
7787 begin
7788 if Is_Compilation_Unit (Par)
7789 and then Par /= Cunit_Entity (Current_Sem_Unit)
7790 and then Parent (Cur_Use) = Spec
7791 and then
7792 List_Containing (Cur_Use) = Visible_Declarations (Spec)
7793 then
7794 return;
7795 end if;
7796 end;
7797 end if;
7799 -- Finally, if the current use clause is in the context then
7800 -- the clause is redundant when it is nested within the unit.
7802 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
7803 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
7804 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
7805 then
7806 Redundant := Clause;
7807 Prev_Use := Cur_Use;
7809 else
7810 null;
7811 end if;
7813 if Present (Redundant) then
7814 Error_Msg_Sloc := Sloc (Prev_Use);
7815 Error_Msg_NE -- CODEFIX
7816 ("& is already use-visible through previous use clause #??",
7817 Redundant, Pack_Name);
7818 end if;
7819 end Note_Redundant_Use;
7821 ---------------
7822 -- Pop_Scope --
7823 ---------------
7825 procedure Pop_Scope is
7826 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
7827 S : constant Entity_Id := SST.Entity;
7829 begin
7830 if Debug_Flag_E then
7831 Write_Info;
7832 end if;
7834 -- Set Default_Storage_Pool field of the library unit if necessary
7836 if Ekind_In (S, E_Package, E_Generic_Package)
7837 and then
7838 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
7839 then
7840 declare
7841 Aux : constant Node_Id :=
7842 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
7843 begin
7844 if No (Default_Storage_Pool (Aux)) then
7845 Set_Default_Storage_Pool (Aux, Default_Pool);
7846 end if;
7847 end;
7848 end if;
7850 Scope_Suppress := SST.Save_Scope_Suppress;
7851 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
7852 Check_Policy_List := SST.Save_Check_Policy_List;
7853 Default_Pool := SST.Save_Default_Storage_Pool;
7854 SPARK_Mode := SST.Save_SPARK_Mode;
7855 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
7856 Default_SSO := SST.Save_Default_SSO;
7857 Uneval_Old := SST.Save_Uneval_Old;
7859 if Debug_Flag_W then
7860 Write_Str ("<-- exiting scope: ");
7861 Write_Name (Chars (Current_Scope));
7862 Write_Str (", Depth=");
7863 Write_Int (Int (Scope_Stack.Last));
7864 Write_Eol;
7865 end if;
7867 End_Use_Clauses (SST.First_Use_Clause);
7869 -- If the actions to be wrapped are still there they will get lost
7870 -- causing incomplete code to be generated. It is better to abort in
7871 -- this case (and we do the abort even with assertions off since the
7872 -- penalty is incorrect code generation).
7874 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
7875 raise Program_Error;
7876 end if;
7878 -- Free last subprogram name if allocated, and pop scope
7880 Free (SST.Last_Subprogram_Name);
7881 Scope_Stack.Decrement_Last;
7882 end Pop_Scope;
7884 ---------------
7885 -- Push_Scope --
7886 ---------------
7888 procedure Push_Scope (S : Entity_Id) is
7889 E : constant Entity_Id := Scope (S);
7891 begin
7892 if Ekind (S) = E_Void then
7893 null;
7895 -- Set scope depth if not a non-concurrent type, and we have not yet set
7896 -- the scope depth. This means that we have the first occurrence of the
7897 -- scope, and this is where the depth is set.
7899 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
7900 and then not Scope_Depth_Set (S)
7901 then
7902 if S = Standard_Standard then
7903 Set_Scope_Depth_Value (S, Uint_0);
7905 elsif Is_Child_Unit (S) then
7906 Set_Scope_Depth_Value (S, Uint_1);
7908 elsif not Is_Record_Type (Current_Scope) then
7909 if Ekind (S) = E_Loop then
7910 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
7911 else
7912 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
7913 end if;
7914 end if;
7915 end if;
7917 Scope_Stack.Increment_Last;
7919 declare
7920 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
7922 begin
7923 SST.Entity := S;
7924 SST.Save_Scope_Suppress := Scope_Suppress;
7925 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
7926 SST.Save_Check_Policy_List := Check_Policy_List;
7927 SST.Save_Default_Storage_Pool := Default_Pool;
7928 SST.Save_SPARK_Mode := SPARK_Mode;
7929 SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma;
7930 SST.Save_Default_SSO := Default_SSO;
7931 SST.Save_Uneval_Old := Uneval_Old;
7933 if Scope_Stack.Last > Scope_Stack.First then
7934 SST.Component_Alignment_Default := Scope_Stack.Table
7935 (Scope_Stack.Last - 1).
7936 Component_Alignment_Default;
7937 end if;
7939 SST.Last_Subprogram_Name := null;
7940 SST.Is_Transient := False;
7941 SST.Node_To_Be_Wrapped := Empty;
7942 SST.Pending_Freeze_Actions := No_List;
7943 SST.Actions_To_Be_Wrapped := (others => No_List);
7944 SST.First_Use_Clause := Empty;
7945 SST.Is_Active_Stack_Base := False;
7946 SST.Previous_Visibility := False;
7947 SST.Locked_Shared_Objects := No_Elist;
7948 end;
7950 if Debug_Flag_W then
7951 Write_Str ("--> new scope: ");
7952 Write_Name (Chars (Current_Scope));
7953 Write_Str (", Id=");
7954 Write_Int (Int (Current_Scope));
7955 Write_Str (", Depth=");
7956 Write_Int (Int (Scope_Stack.Last));
7957 Write_Eol;
7958 end if;
7960 -- Deal with copying flags from the previous scope to this one. This is
7961 -- not necessary if either scope is standard, or if the new scope is a
7962 -- child unit.
7964 if S /= Standard_Standard
7965 and then Scope (S) /= Standard_Standard
7966 and then not Is_Child_Unit (S)
7967 then
7968 if Nkind (E) not in N_Entity then
7969 return;
7970 end if;
7972 -- Copy categorization flags from Scope (S) to S, this is not done
7973 -- when Scope (S) is Standard_Standard since propagation is from
7974 -- library unit entity inwards. Copy other relevant attributes as
7975 -- well (Discard_Names in particular).
7977 -- We only propagate inwards for library level entities,
7978 -- inner level subprograms do not inherit the categorization.
7980 if Is_Library_Level_Entity (S) then
7981 Set_Is_Preelaborated (S, Is_Preelaborated (E));
7982 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
7983 Set_Discard_Names (S, Discard_Names (E));
7984 Set_Suppress_Value_Tracking_On_Call
7985 (S, Suppress_Value_Tracking_On_Call (E));
7986 Set_Categorization_From_Scope (E => S, Scop => E);
7987 end if;
7988 end if;
7990 if Is_Child_Unit (S)
7991 and then Present (E)
7992 and then Ekind_In (E, E_Package, E_Generic_Package)
7993 and then
7994 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
7995 then
7996 declare
7997 Aux : constant Node_Id :=
7998 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
7999 begin
8000 if Present (Default_Storage_Pool (Aux)) then
8001 Default_Pool := Default_Storage_Pool (Aux);
8002 end if;
8003 end;
8004 end if;
8005 end Push_Scope;
8007 ---------------------
8008 -- Premature_Usage --
8009 ---------------------
8011 procedure Premature_Usage (N : Node_Id) is
8012 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
8013 E : Entity_Id := Entity (N);
8015 begin
8016 -- Within an instance, the analysis of the actual for a formal object
8017 -- does not see the name of the object itself. This is significant only
8018 -- if the object is an aggregate, where its analysis does not do any
8019 -- name resolution on component associations. (see 4717-008). In such a
8020 -- case, look for the visible homonym on the chain.
8022 if In_Instance and then Present (Homonym (E)) then
8023 E := Homonym (E);
8024 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
8025 E := Homonym (E);
8026 end loop;
8028 if Present (E) then
8029 Set_Entity (N, E);
8030 Set_Etype (N, Etype (E));
8031 return;
8032 end if;
8033 end if;
8035 if Kind = N_Component_Declaration then
8036 Error_Msg_N
8037 ("component&! cannot be used before end of record declaration", N);
8039 elsif Kind = N_Parameter_Specification then
8040 Error_Msg_N
8041 ("formal parameter&! cannot be used before end of specification",
8044 elsif Kind = N_Discriminant_Specification then
8045 Error_Msg_N
8046 ("discriminant&! cannot be used before end of discriminant part",
8049 elsif Kind = N_Procedure_Specification
8050 or else Kind = N_Function_Specification
8051 then
8052 Error_Msg_N
8053 ("subprogram&! cannot be used before end of its declaration",
8056 elsif Kind = N_Full_Type_Declaration then
8057 Error_Msg_N
8058 ("type& cannot be used before end of its declaration!", N);
8060 else
8061 Error_Msg_N
8062 ("object& cannot be used before end of its declaration!", N);
8063 end if;
8064 end Premature_Usage;
8066 ------------------------
8067 -- Present_System_Aux --
8068 ------------------------
8070 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
8071 Loc : Source_Ptr;
8072 Aux_Name : Unit_Name_Type;
8073 Unum : Unit_Number_Type;
8074 Withn : Node_Id;
8075 With_Sys : Node_Id;
8076 The_Unit : Node_Id;
8078 function Find_System (C_Unit : Node_Id) return Entity_Id;
8079 -- Scan context clause of compilation unit to find with_clause
8080 -- for System.
8082 -----------------
8083 -- Find_System --
8084 -----------------
8086 function Find_System (C_Unit : Node_Id) return Entity_Id is
8087 With_Clause : Node_Id;
8089 begin
8090 With_Clause := First (Context_Items (C_Unit));
8091 while Present (With_Clause) loop
8092 if (Nkind (With_Clause) = N_With_Clause
8093 and then Chars (Name (With_Clause)) = Name_System)
8094 and then Comes_From_Source (With_Clause)
8095 then
8096 return With_Clause;
8097 end if;
8099 Next (With_Clause);
8100 end loop;
8102 return Empty;
8103 end Find_System;
8105 -- Start of processing for Present_System_Aux
8107 begin
8108 -- The child unit may have been loaded and analyzed already
8110 if Present (System_Aux_Id) then
8111 return True;
8113 -- If no previous pragma for System.Aux, nothing to load
8115 elsif No (System_Extend_Unit) then
8116 return False;
8118 -- Use the unit name given in the pragma to retrieve the unit.
8119 -- Verify that System itself appears in the context clause of the
8120 -- current compilation. If System is not present, an error will
8121 -- have been reported already.
8123 else
8124 With_Sys := Find_System (Cunit (Current_Sem_Unit));
8126 The_Unit := Unit (Cunit (Current_Sem_Unit));
8128 if No (With_Sys)
8129 and then
8130 (Nkind (The_Unit) = N_Package_Body
8131 or else (Nkind (The_Unit) = N_Subprogram_Body
8132 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
8133 then
8134 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
8135 end if;
8137 if No (With_Sys) and then Present (N) then
8139 -- If we are compiling a subunit, we need to examine its
8140 -- context as well (Current_Sem_Unit is the parent unit);
8142 The_Unit := Parent (N);
8143 while Nkind (The_Unit) /= N_Compilation_Unit loop
8144 The_Unit := Parent (The_Unit);
8145 end loop;
8147 if Nkind (Unit (The_Unit)) = N_Subunit then
8148 With_Sys := Find_System (The_Unit);
8149 end if;
8150 end if;
8152 if No (With_Sys) then
8153 return False;
8154 end if;
8156 Loc := Sloc (With_Sys);
8157 Get_Name_String (Chars (Expression (System_Extend_Unit)));
8158 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
8159 Name_Buffer (1 .. 7) := "system.";
8160 Name_Buffer (Name_Len + 8) := '%';
8161 Name_Buffer (Name_Len + 9) := 's';
8162 Name_Len := Name_Len + 9;
8163 Aux_Name := Name_Find;
8165 Unum :=
8166 Load_Unit
8167 (Load_Name => Aux_Name,
8168 Required => False,
8169 Subunit => False,
8170 Error_Node => With_Sys);
8172 if Unum /= No_Unit then
8173 Semantics (Cunit (Unum));
8174 System_Aux_Id :=
8175 Defining_Entity (Specification (Unit (Cunit (Unum))));
8177 Withn :=
8178 Make_With_Clause (Loc,
8179 Name =>
8180 Make_Expanded_Name (Loc,
8181 Chars => Chars (System_Aux_Id),
8182 Prefix => New_Occurrence_Of (Scope (System_Aux_Id), Loc),
8183 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
8185 Set_Entity (Name (Withn), System_Aux_Id);
8187 Set_Library_Unit (Withn, Cunit (Unum));
8188 Set_Corresponding_Spec (Withn, System_Aux_Id);
8189 Set_First_Name (Withn, True);
8190 Set_Implicit_With (Withn, True);
8192 Insert_After (With_Sys, Withn);
8193 Mark_Rewrite_Insertion (Withn);
8194 Set_Context_Installed (Withn);
8196 return True;
8198 -- Here if unit load failed
8200 else
8201 Error_Msg_Name_1 := Name_System;
8202 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
8203 Error_Msg_N
8204 ("extension package `%.%` does not exist",
8205 Opt.System_Extend_Unit);
8206 return False;
8207 end if;
8208 end if;
8209 end Present_System_Aux;
8211 -------------------------
8212 -- Restore_Scope_Stack --
8213 -------------------------
8215 procedure Restore_Scope_Stack
8216 (List : Elist_Id;
8217 Handle_Use : Boolean := True)
8219 SS_Last : constant Int := Scope_Stack.Last;
8220 Elmt : Elmt_Id;
8222 begin
8223 -- Restore visibility of previous scope stack, if any, using the list
8224 -- we saved (we use Remove, since this list will not be used again).
8226 loop
8227 Elmt := Last_Elmt (List);
8228 exit when Elmt = No_Elmt;
8229 Set_Is_Immediately_Visible (Node (Elmt));
8230 Remove_Last_Elmt (List);
8231 end loop;
8233 -- Restore use clauses
8235 if SS_Last >= Scope_Stack.First
8236 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
8237 and then Handle_Use
8238 then
8239 Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
8240 end if;
8241 end Restore_Scope_Stack;
8243 ----------------------
8244 -- Save_Scope_Stack --
8245 ----------------------
8247 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
8248 -- consuming any memory. That is, Save_Scope_Stack took care of removing
8249 -- from immediate visibility entities and Restore_Scope_Stack took care
8250 -- of restoring their visibility analyzing the context of each entity. The
8251 -- problem of such approach is that it was fragile and caused unexpected
8252 -- visibility problems, and indeed one test was found where there was a
8253 -- real problem.
8255 -- Furthermore, the following experiment was carried out:
8257 -- - Save_Scope_Stack was modified to store in an Elist1 all those
8258 -- entities whose attribute Is_Immediately_Visible is modified
8259 -- from True to False.
8261 -- - Restore_Scope_Stack was modified to store in another Elist2
8262 -- all the entities whose attribute Is_Immediately_Visible is
8263 -- modified from False to True.
8265 -- - Extra code was added to verify that all the elements of Elist1
8266 -- are found in Elist2
8268 -- This test shows that there may be more occurrences of this problem which
8269 -- have not yet been detected. As a result, we replaced that approach by
8270 -- the current one in which Save_Scope_Stack returns the list of entities
8271 -- whose visibility is changed, and that list is passed to Restore_Scope_
8272 -- Stack to undo that change. This approach is simpler and safer, although
8273 -- it consumes more memory.
8275 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
8276 Result : constant Elist_Id := New_Elmt_List;
8277 E : Entity_Id;
8278 S : Entity_Id;
8279 SS_Last : constant Int := Scope_Stack.Last;
8281 procedure Remove_From_Visibility (E : Entity_Id);
8282 -- If E is immediately visible then append it to the result and remove
8283 -- it temporarily from visibility.
8285 ----------------------------
8286 -- Remove_From_Visibility --
8287 ----------------------------
8289 procedure Remove_From_Visibility (E : Entity_Id) is
8290 begin
8291 if Is_Immediately_Visible (E) then
8292 Append_Elmt (E, Result);
8293 Set_Is_Immediately_Visible (E, False);
8294 end if;
8295 end Remove_From_Visibility;
8297 -- Start of processing for Save_Scope_Stack
8299 begin
8300 if SS_Last >= Scope_Stack.First
8301 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
8302 then
8303 if Handle_Use then
8304 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
8305 end if;
8307 -- If the call is from within a compilation unit, as when called from
8308 -- Rtsfind, make current entries in scope stack invisible while we
8309 -- analyze the new unit.
8311 for J in reverse 0 .. SS_Last loop
8312 exit when Scope_Stack.Table (J).Entity = Standard_Standard
8313 or else No (Scope_Stack.Table (J).Entity);
8315 S := Scope_Stack.Table (J).Entity;
8317 Remove_From_Visibility (S);
8319 E := First_Entity (S);
8320 while Present (E) loop
8321 Remove_From_Visibility (E);
8322 Next_Entity (E);
8323 end loop;
8324 end loop;
8326 end if;
8328 return Result;
8329 end Save_Scope_Stack;
8331 -------------
8332 -- Set_Use --
8333 -------------
8335 procedure Set_Use (L : List_Id) is
8336 Decl : Node_Id;
8337 Pack_Name : Node_Id;
8338 Pack : Entity_Id;
8339 Id : Entity_Id;
8341 begin
8342 if Present (L) then
8343 Decl := First (L);
8344 while Present (Decl) loop
8345 if Nkind (Decl) = N_Use_Package_Clause then
8346 Chain_Use_Clause (Decl);
8348 Pack_Name := First (Names (Decl));
8349 while Present (Pack_Name) loop
8350 Pack := Entity (Pack_Name);
8352 if Ekind (Pack) = E_Package
8353 and then Applicable_Use (Pack_Name)
8354 then
8355 Use_One_Package (Pack, Decl);
8356 end if;
8358 Next (Pack_Name);
8359 end loop;
8361 elsif Nkind (Decl) = N_Use_Type_Clause then
8362 Chain_Use_Clause (Decl);
8364 Id := First (Subtype_Marks (Decl));
8365 while Present (Id) loop
8366 if Entity (Id) /= Any_Type then
8367 Use_One_Type (Id);
8368 end if;
8370 Next (Id);
8371 end loop;
8372 end if;
8374 Next (Decl);
8375 end loop;
8376 end if;
8377 end Set_Use;
8379 ---------------------
8380 -- Use_One_Package --
8381 ---------------------
8383 procedure Use_One_Package (P : Entity_Id; N : Node_Id) is
8384 Id : Entity_Id;
8385 Prev : Entity_Id;
8386 Current_Instance : Entity_Id := Empty;
8387 Real_P : Entity_Id;
8388 Private_With_OK : Boolean := False;
8390 begin
8391 if Ekind (P) /= E_Package then
8392 return;
8393 end if;
8395 Set_In_Use (P);
8396 Set_Current_Use_Clause (P, N);
8398 -- Ada 2005 (AI-50217): Check restriction
8400 if From_Limited_With (P) then
8401 Error_Msg_N ("limited withed package cannot appear in use clause", N);
8402 end if;
8404 -- Find enclosing instance, if any
8406 if In_Instance then
8407 Current_Instance := Current_Scope;
8408 while not Is_Generic_Instance (Current_Instance) loop
8409 Current_Instance := Scope (Current_Instance);
8410 end loop;
8412 if No (Hidden_By_Use_Clause (N)) then
8413 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
8414 end if;
8415 end if;
8417 -- If unit is a package renaming, indicate that the renamed
8418 -- package is also in use (the flags on both entities must
8419 -- remain consistent, and a subsequent use of either of them
8420 -- should be recognized as redundant).
8422 if Present (Renamed_Object (P)) then
8423 Set_In_Use (Renamed_Object (P));
8424 Set_Current_Use_Clause (Renamed_Object (P), N);
8425 Real_P := Renamed_Object (P);
8426 else
8427 Real_P := P;
8428 end if;
8430 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
8431 -- found in the private part of a package specification
8433 if In_Private_Part (Current_Scope)
8434 and then Has_Private_With (P)
8435 and then Is_Child_Unit (Current_Scope)
8436 and then Is_Child_Unit (P)
8437 and then Is_Ancestor_Package (Scope (Current_Scope), P)
8438 then
8439 Private_With_OK := True;
8440 end if;
8442 -- Loop through entities in one package making them potentially
8443 -- use-visible.
8445 Id := First_Entity (P);
8446 while Present (Id)
8447 and then (Id /= First_Private_Entity (P)
8448 or else Private_With_OK) -- Ada 2005 (AI-262)
8449 loop
8450 Prev := Current_Entity (Id);
8451 while Present (Prev) loop
8452 if Is_Immediately_Visible (Prev)
8453 and then (not Is_Overloadable (Prev)
8454 or else not Is_Overloadable (Id)
8455 or else (Type_Conformant (Id, Prev)))
8456 then
8457 if No (Current_Instance) then
8459 -- Potentially use-visible entity remains hidden
8461 goto Next_Usable_Entity;
8463 -- A use clause within an instance hides outer global entities,
8464 -- which are not used to resolve local entities in the
8465 -- instance. Note that the predefined entities in Standard
8466 -- could not have been hidden in the generic by a use clause,
8467 -- and therefore remain visible. Other compilation units whose
8468 -- entities appear in Standard must be hidden in an instance.
8470 -- To determine whether an entity is external to the instance
8471 -- we compare the scope depth of its scope with that of the
8472 -- current instance. However, a generic actual of a subprogram
8473 -- instance is declared in the wrapper package but will not be
8474 -- hidden by a use-visible entity. similarly, an entity that is
8475 -- declared in an enclosing instance will not be hidden by an
8476 -- an entity declared in a generic actual, which can only have
8477 -- been use-visible in the generic and will not have hidden the
8478 -- entity in the generic parent.
8480 -- If Id is called Standard, the predefined package with the
8481 -- same name is in the homonym chain. It has to be ignored
8482 -- because it has no defined scope (being the only entity in
8483 -- the system with this mandated behavior).
8485 elsif not Is_Hidden (Id)
8486 and then Present (Scope (Prev))
8487 and then not Is_Wrapper_Package (Scope (Prev))
8488 and then Scope_Depth (Scope (Prev)) <
8489 Scope_Depth (Current_Instance)
8490 and then (Scope (Prev) /= Standard_Standard
8491 or else Sloc (Prev) > Standard_Location)
8492 then
8493 if In_Open_Scopes (Scope (Prev))
8494 and then Is_Generic_Instance (Scope (Prev))
8495 and then Present (Associated_Formal_Package (P))
8496 then
8497 null;
8499 else
8500 Set_Is_Potentially_Use_Visible (Id);
8501 Set_Is_Immediately_Visible (Prev, False);
8502 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
8503 end if;
8504 end if;
8506 -- A user-defined operator is not use-visible if the predefined
8507 -- operator for the type is immediately visible, which is the case
8508 -- if the type of the operand is in an open scope. This does not
8509 -- apply to user-defined operators that have operands of different
8510 -- types, because the predefined mixed mode operations (multiply
8511 -- and divide) apply to universal types and do not hide anything.
8513 elsif Ekind (Prev) = E_Operator
8514 and then Operator_Matches_Spec (Prev, Id)
8515 and then In_Open_Scopes
8516 (Scope (Base_Type (Etype (First_Formal (Id)))))
8517 and then (No (Next_Formal (First_Formal (Id)))
8518 or else Etype (First_Formal (Id)) =
8519 Etype (Next_Formal (First_Formal (Id)))
8520 or else Chars (Prev) = Name_Op_Expon)
8521 then
8522 goto Next_Usable_Entity;
8524 -- In an instance, two homonyms may become use_visible through the
8525 -- actuals of distinct formal packages. In the generic, only the
8526 -- current one would have been visible, so make the other one
8527 -- not use_visible.
8529 elsif Present (Current_Instance)
8530 and then Is_Potentially_Use_Visible (Prev)
8531 and then not Is_Overloadable (Prev)
8532 and then Scope (Id) /= Scope (Prev)
8533 and then Used_As_Generic_Actual (Scope (Prev))
8534 and then Used_As_Generic_Actual (Scope (Id))
8535 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
8536 Current_Use_Clause (Scope (Id)))
8537 then
8538 Set_Is_Potentially_Use_Visible (Prev, False);
8539 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
8540 end if;
8542 Prev := Homonym (Prev);
8543 end loop;
8545 -- On exit, we know entity is not hidden, unless it is private
8547 if not Is_Hidden (Id)
8548 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
8549 then
8550 Set_Is_Potentially_Use_Visible (Id);
8552 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
8553 Set_Is_Potentially_Use_Visible (Full_View (Id));
8554 end if;
8555 end if;
8557 <<Next_Usable_Entity>>
8558 Next_Entity (Id);
8559 end loop;
8561 -- Child units are also made use-visible by a use clause, but they may
8562 -- appear after all visible declarations in the parent entity list.
8564 while Present (Id) loop
8565 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
8566 Set_Is_Potentially_Use_Visible (Id);
8567 end if;
8569 Next_Entity (Id);
8570 end loop;
8572 if Chars (Real_P) = Name_System
8573 and then Scope (Real_P) = Standard_Standard
8574 and then Present_System_Aux (N)
8575 then
8576 Use_One_Package (System_Aux_Id, N);
8577 end if;
8579 end Use_One_Package;
8581 ------------------
8582 -- Use_One_Type --
8583 ------------------
8585 procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False) is
8586 Elmt : Elmt_Id;
8587 Is_Known_Used : Boolean;
8588 Op_List : Elist_Id;
8589 T : Entity_Id;
8591 function Spec_Reloaded_For_Body return Boolean;
8592 -- Determine whether the compilation unit is a package body and the use
8593 -- type clause is in the spec of the same package. Even though the spec
8594 -- was analyzed first, its context is reloaded when analysing the body.
8596 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
8597 -- AI05-150: if the use_type_clause carries the "all" qualifier,
8598 -- class-wide operations of ancestor types are use-visible if the
8599 -- ancestor type is visible.
8601 ----------------------------
8602 -- Spec_Reloaded_For_Body --
8603 ----------------------------
8605 function Spec_Reloaded_For_Body return Boolean is
8606 begin
8607 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
8608 declare
8609 Spec : constant Node_Id :=
8610 Parent (List_Containing (Parent (Id)));
8612 begin
8613 -- Check whether type is declared in a package specification,
8614 -- and current unit is the corresponding package body. The
8615 -- use clauses themselves may be within a nested package.
8617 return
8618 Nkind (Spec) = N_Package_Specification
8619 and then
8620 In_Same_Source_Unit (Corresponding_Body (Parent (Spec)),
8621 Cunit_Entity (Current_Sem_Unit));
8622 end;
8623 end if;
8625 return False;
8626 end Spec_Reloaded_For_Body;
8628 -------------------------------
8629 -- Use_Class_Wide_Operations --
8630 -------------------------------
8632 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
8633 Scop : Entity_Id;
8634 Ent : Entity_Id;
8636 function Is_Class_Wide_Operation_Of
8637 (Op : Entity_Id;
8638 T : Entity_Id) return Boolean;
8639 -- Determine whether a subprogram has a class-wide parameter or
8640 -- result that is T'Class.
8642 ---------------------------------
8643 -- Is_Class_Wide_Operation_Of --
8644 ---------------------------------
8646 function Is_Class_Wide_Operation_Of
8647 (Op : Entity_Id;
8648 T : Entity_Id) return Boolean
8650 Formal : Entity_Id;
8652 begin
8653 Formal := First_Formal (Op);
8654 while Present (Formal) loop
8655 if Etype (Formal) = Class_Wide_Type (T) then
8656 return True;
8657 end if;
8658 Next_Formal (Formal);
8659 end loop;
8661 if Etype (Op) = Class_Wide_Type (T) then
8662 return True;
8663 end if;
8665 return False;
8666 end Is_Class_Wide_Operation_Of;
8668 -- Start of processing for Use_Class_Wide_Operations
8670 begin
8671 Scop := Scope (Typ);
8672 if not Is_Hidden (Scop) then
8673 Ent := First_Entity (Scop);
8674 while Present (Ent) loop
8675 if Is_Overloadable (Ent)
8676 and then Is_Class_Wide_Operation_Of (Ent, Typ)
8677 and then not Is_Potentially_Use_Visible (Ent)
8678 then
8679 Set_Is_Potentially_Use_Visible (Ent);
8680 Append_Elmt (Ent, Used_Operations (Parent (Id)));
8681 end if;
8683 Next_Entity (Ent);
8684 end loop;
8685 end if;
8687 if Is_Derived_Type (Typ) then
8688 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
8689 end if;
8690 end Use_Class_Wide_Operations;
8692 -- Start of processing for Use_One_Type
8694 begin
8695 -- It is the type determined by the subtype mark (8.4(8)) whose
8696 -- operations become potentially use-visible.
8698 T := Base_Type (Entity (Id));
8700 -- Either the type itself is used, the package where it is declared
8701 -- is in use or the entity is declared in the current package, thus
8702 -- use-visible.
8704 Is_Known_Used :=
8705 In_Use (T)
8706 or else In_Use (Scope (T))
8707 or else Scope (T) = Current_Scope;
8709 Set_Redundant_Use (Id,
8710 Is_Known_Used or else Is_Potentially_Use_Visible (T));
8712 if Ekind (T) = E_Incomplete_Type then
8713 Error_Msg_N ("premature usage of incomplete type", Id);
8715 elsif In_Open_Scopes (Scope (T)) then
8716 null;
8718 -- A limited view cannot appear in a use_type clause. However, an access
8719 -- type whose designated type is limited has the flag but is not itself
8720 -- a limited view unless we only have a limited view of its enclosing
8721 -- package.
8723 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
8724 Error_Msg_N
8725 ("incomplete type from limited view "
8726 & "cannot appear in use clause", Id);
8728 -- If the subtype mark designates a subtype in a different package,
8729 -- we have to check that the parent type is visible, otherwise the
8730 -- use type clause is a noop. Not clear how to do that???
8732 elsif not Redundant_Use (Id) then
8733 Set_In_Use (T);
8735 -- If T is tagged, primitive operators on class-wide operands
8736 -- are also available.
8738 if Is_Tagged_Type (T) then
8739 Set_In_Use (Class_Wide_Type (T));
8740 end if;
8742 Set_Current_Use_Clause (T, Parent (Id));
8744 -- Iterate over primitive operations of the type. If an operation is
8745 -- already use_visible, it is the result of a previous use_clause,
8746 -- and already appears on the corresponding entity chain. If the
8747 -- clause is being reinstalled, operations are already use-visible.
8749 if Installed then
8750 null;
8752 else
8753 Op_List := Collect_Primitive_Operations (T);
8754 Elmt := First_Elmt (Op_List);
8755 while Present (Elmt) loop
8756 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
8757 or else Chars (Node (Elmt)) in Any_Operator_Name)
8758 and then not Is_Hidden (Node (Elmt))
8759 and then not Is_Potentially_Use_Visible (Node (Elmt))
8760 then
8761 Set_Is_Potentially_Use_Visible (Node (Elmt));
8762 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
8764 elsif Ada_Version >= Ada_2012
8765 and then All_Present (Parent (Id))
8766 and then not Is_Hidden (Node (Elmt))
8767 and then not Is_Potentially_Use_Visible (Node (Elmt))
8768 then
8769 Set_Is_Potentially_Use_Visible (Node (Elmt));
8770 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
8771 end if;
8773 Next_Elmt (Elmt);
8774 end loop;
8775 end if;
8777 if Ada_Version >= Ada_2012
8778 and then All_Present (Parent (Id))
8779 and then Is_Tagged_Type (T)
8780 then
8781 Use_Class_Wide_Operations (T);
8782 end if;
8783 end if;
8785 -- If warning on redundant constructs, check for unnecessary WITH
8787 if Warn_On_Redundant_Constructs
8788 and then Is_Known_Used
8790 -- with P; with P; use P;
8791 -- package P is package X is package body X is
8792 -- type T ... use P.T;
8794 -- The compilation unit is the body of X. GNAT first compiles the
8795 -- spec of X, then proceeds to the body. At that point P is marked
8796 -- as use visible. The analysis then reinstalls the spec along with
8797 -- its context. The use clause P.T is now recognized as redundant,
8798 -- but in the wrong context. Do not emit a warning in such cases.
8799 -- Do not emit a warning either if we are in an instance, there is
8800 -- no redundancy between an outer use_clause and one that appears
8801 -- within the generic.
8803 and then not Spec_Reloaded_For_Body
8804 and then not In_Instance
8805 then
8806 -- The type already has a use clause
8808 if In_Use (T) then
8810 -- Case where we know the current use clause for the type
8812 if Present (Current_Use_Clause (T)) then
8813 Use_Clause_Known : declare
8814 Clause1 : constant Node_Id := Parent (Id);
8815 Clause2 : constant Node_Id := Current_Use_Clause (T);
8816 Ent1 : Entity_Id;
8817 Ent2 : Entity_Id;
8818 Err_No : Node_Id;
8819 Unit1 : Node_Id;
8820 Unit2 : Node_Id;
8822 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
8823 -- Return the appropriate entity for determining which unit
8824 -- has a deeper scope: the defining entity for U, unless U
8825 -- is a package instance, in which case we retrieve the
8826 -- entity of the instance spec.
8828 --------------------
8829 -- Entity_Of_Unit --
8830 --------------------
8832 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
8833 begin
8834 if Nkind (U) = N_Package_Instantiation
8835 and then Analyzed (U)
8836 then
8837 return Defining_Entity (Instance_Spec (U));
8838 else
8839 return Defining_Entity (U);
8840 end if;
8841 end Entity_Of_Unit;
8843 -- Start of processing for Use_Clause_Known
8845 begin
8846 -- If both current use type clause and the use type clause
8847 -- for the type are at the compilation unit level, one of
8848 -- the units must be an ancestor of the other, and the
8849 -- warning belongs on the descendant.
8851 if Nkind (Parent (Clause1)) = N_Compilation_Unit
8852 and then
8853 Nkind (Parent (Clause2)) = N_Compilation_Unit
8854 then
8855 -- If the unit is a subprogram body that acts as spec,
8856 -- the context clause is shared with the constructed
8857 -- subprogram spec. Clearly there is no redundancy.
8859 if Clause1 = Clause2 then
8860 return;
8861 end if;
8863 Unit1 := Unit (Parent (Clause1));
8864 Unit2 := Unit (Parent (Clause2));
8866 -- If both clauses are on same unit, or one is the body
8867 -- of the other, or one of them is in a subunit, report
8868 -- redundancy on the later one.
8870 if Unit1 = Unit2 then
8871 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8872 Error_Msg_NE -- CODEFIX
8873 ("& is already use-visible through previous "
8874 & "use_type_clause #??", Clause1, T);
8875 return;
8877 elsif Nkind (Unit1) = N_Subunit then
8878 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8879 Error_Msg_NE -- CODEFIX
8880 ("& is already use-visible through previous "
8881 & "use_type_clause #??", Clause1, T);
8882 return;
8884 elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body)
8885 and then Nkind (Unit1) /= Nkind (Unit2)
8886 and then Nkind (Unit1) /= N_Subunit
8887 then
8888 Error_Msg_Sloc := Sloc (Clause1);
8889 Error_Msg_NE -- CODEFIX
8890 ("& is already use-visible through previous "
8891 & "use_type_clause #??", Current_Use_Clause (T), T);
8892 return;
8893 end if;
8895 -- There is a redundant use type clause in a child unit.
8896 -- Determine which of the units is more deeply nested.
8897 -- If a unit is a package instance, retrieve the entity
8898 -- and its scope from the instance spec.
8900 Ent1 := Entity_Of_Unit (Unit1);
8901 Ent2 := Entity_Of_Unit (Unit2);
8903 if Scope (Ent2) = Standard_Standard then
8904 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8905 Err_No := Clause1;
8907 elsif Scope (Ent1) = Standard_Standard then
8908 Error_Msg_Sloc := Sloc (Id);
8909 Err_No := Clause2;
8911 -- If both units are child units, we determine which one
8912 -- is the descendant by the scope distance to the
8913 -- ultimate parent unit.
8915 else
8916 declare
8917 S1, S2 : Entity_Id;
8919 begin
8920 S1 := Scope (Ent1);
8921 S2 := Scope (Ent2);
8922 while Present (S1)
8923 and then Present (S2)
8924 and then S1 /= Standard_Standard
8925 and then S2 /= Standard_Standard
8926 loop
8927 S1 := Scope (S1);
8928 S2 := Scope (S2);
8929 end loop;
8931 if S1 = Standard_Standard then
8932 Error_Msg_Sloc := Sloc (Id);
8933 Err_No := Clause2;
8934 else
8935 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8936 Err_No := Clause1;
8937 end if;
8938 end;
8939 end if;
8941 Error_Msg_NE -- CODEFIX
8942 ("& is already use-visible through previous "
8943 & "use_type_clause #??", Err_No, Id);
8945 -- Case where current use type clause and the use type
8946 -- clause for the type are not both at the compilation unit
8947 -- level. In this case we don't have location information.
8949 else
8950 Error_Msg_NE -- CODEFIX
8951 ("& is already use-visible through previous "
8952 & "use type clause??", Id, T);
8953 end if;
8954 end Use_Clause_Known;
8956 -- Here if Current_Use_Clause is not set for T, another case
8957 -- where we do not have the location information available.
8959 else
8960 Error_Msg_NE -- CODEFIX
8961 ("& is already use-visible through previous "
8962 & "use type clause??", Id, T);
8963 end if;
8965 -- The package where T is declared is already used
8967 elsif In_Use (Scope (T)) then
8968 Error_Msg_Sloc := Sloc (Current_Use_Clause (Scope (T)));
8969 Error_Msg_NE -- CODEFIX
8970 ("& is already use-visible through package use clause #??",
8971 Id, T);
8973 -- The current scope is the package where T is declared
8975 else
8976 Error_Msg_Node_2 := Scope (T);
8977 Error_Msg_NE -- CODEFIX
8978 ("& is already use-visible inside package &??", Id, T);
8979 end if;
8980 end if;
8981 end Use_One_Type;
8983 ----------------
8984 -- Write_Info --
8985 ----------------
8987 procedure Write_Info is
8988 Id : Entity_Id := First_Entity (Current_Scope);
8990 begin
8991 -- No point in dumping standard entities
8993 if Current_Scope = Standard_Standard then
8994 return;
8995 end if;
8997 Write_Str ("========================================================");
8998 Write_Eol;
8999 Write_Str (" Defined Entities in ");
9000 Write_Name (Chars (Current_Scope));
9001 Write_Eol;
9002 Write_Str ("========================================================");
9003 Write_Eol;
9005 if No (Id) then
9006 Write_Str ("-- none --");
9007 Write_Eol;
9009 else
9010 while Present (Id) loop
9011 Write_Entity_Info (Id, " ");
9012 Next_Entity (Id);
9013 end loop;
9014 end if;
9016 if Scope (Current_Scope) = Standard_Standard then
9018 -- Print information on the current unit itself
9020 Write_Entity_Info (Current_Scope, " ");
9021 end if;
9023 Write_Eol;
9024 end Write_Info;
9026 --------
9027 -- ws --
9028 --------
9030 procedure ws is
9031 S : Entity_Id;
9032 begin
9033 for J in reverse 1 .. Scope_Stack.Last loop
9034 S := Scope_Stack.Table (J).Entity;
9035 Write_Int (Int (S));
9036 Write_Str (" === ");
9037 Write_Name (Chars (S));
9038 Write_Eol;
9039 end loop;
9040 end ws;
9042 --------
9043 -- we --
9044 --------
9046 procedure we (S : Entity_Id) is
9047 E : Entity_Id;
9048 begin
9049 E := First_Entity (S);
9050 while Present (E) loop
9051 Write_Int (Int (E));
9052 Write_Str (" === ");
9053 Write_Name (Chars (E));
9054 Write_Eol;
9055 Next_Entity (E);
9056 end loop;
9057 end we;
9058 end Sem_Ch8;