* MAINTAINERS: Add a note that maintainership also includes web
[official-gcc.git] / gcc / ada / sem_ch8.adb
bloba51cc636298f429fbcc74e82c0eb16ecba30fbde
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-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Disp; use Exp_Disp;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Ghost; use Ghost;
36 with Impunit; use Impunit;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Namet.Sp; use Namet.Sp;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch4; use Sem_Ch4;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch12; use Sem_Ch12;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Dist; use Sem_Dist;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Res; use Sem_Res;
62 with Sem_Util; use Sem_Util;
63 with Sem_Type; use Sem_Type;
64 with Stand; use Stand;
65 with Sinfo; use Sinfo;
66 with Sinfo.CN; use Sinfo.CN;
67 with Snames; use Snames;
68 with Style;
69 with Table;
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 procedure Attribute_Renaming (N : Node_Id);
406 -- Analyze renaming of attribute as subprogram. The renaming declaration N
407 -- is rewritten as a subprogram body that returns the attribute reference
408 -- applied to the formals of the function.
410 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
411 -- Set Entity, with style check if need be. For a discriminant reference,
412 -- replace by the corresponding discriminal, i.e. the parameter of the
413 -- initialization procedure that corresponds to the discriminant.
415 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
416 -- A renaming_as_body may occur after the entity of the original decla-
417 -- ration has been frozen. In that case, the body of the new entity must
418 -- be built now, because the usual mechanism of building the renamed
419 -- body at the point of freezing will not work. Subp is the subprogram
420 -- for which N provides the Renaming_As_Body.
422 procedure Check_In_Previous_With_Clause
423 (N : Node_Id;
424 Nam : Node_Id);
425 -- N is a use_package clause and Nam the package name, or N is a use_type
426 -- clause and Nam is the prefix of the type name. In either case, verify
427 -- that the package is visible at that point in the context: either it
428 -- appears in a previous with_clause, or because it is a fully qualified
429 -- name and the root ancestor appears in a previous with_clause.
431 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
432 -- Verify that the entity in a renaming declaration that is a library unit
433 -- is itself a library unit and not a nested unit or subunit. Also check
434 -- that if the renaming is a child unit of a generic parent, then the
435 -- renamed unit must also be a child unit of that parent. Finally, verify
436 -- that a renamed generic unit is not an implicit child declared within
437 -- an instance of the parent.
439 procedure Chain_Use_Clause (N : Node_Id);
440 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
441 -- the proper scope table entry. This is usually the current scope, but it
442 -- will be an inner scope when installing the use clauses of the private
443 -- declarations of a parent unit prior to compiling the private part of a
444 -- child unit. This chain is traversed when installing/removing use clauses
445 -- when compiling a subunit or instantiating a generic body on the fly,
446 -- when it is necessary to save and restore full environments.
448 function Enclosing_Instance return Entity_Id;
449 -- In an instance nested within another one, several semantic checks are
450 -- unnecessary because the legality of the nested instance has been checked
451 -- in the enclosing generic unit. This applies in particular to legality
452 -- checks on actuals for formal subprograms of the inner instance, which
453 -- are checked as subprogram renamings, and may be complicated by confusion
454 -- in private/full views. This function returns the instance enclosing the
455 -- current one if there is such, else it returns Empty.
457 -- If the renaming determines the entity for the default of a formal
458 -- subprogram nested within another instance, choose the innermost
459 -- candidate. This is because if the formal has a box, and we are within
460 -- an enclosing instance where some candidate interpretations are local
461 -- to this enclosing instance, we know that the default was properly
462 -- resolved when analyzing the generic, so we prefer the local
463 -- candidates to those that are external. This is not always the case
464 -- but is a reasonable heuristic on the use of nested generics. The
465 -- proper solution requires a full renaming model.
467 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
468 -- Return the appropriate entity for determining which unit has a deeper
469 -- scope: the defining entity for U, unless U is a package instance, in
470 -- which case we retrieve the entity of the instance spec.
472 procedure Find_Expanded_Name (N : Node_Id);
473 -- The input is a selected component known to be an expanded name. Verify
474 -- legality of selector given the scope denoted by prefix, and change node
475 -- N into a expanded name with a properly set Entity field.
477 function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id;
478 -- Find the most previous use clause (that is, the first one to appear in
479 -- the source) by traversing the previous clause chain that exists in both
480 -- N_Use_Package_Clause nodes and N_Use_Type_Clause nodes.
482 function Find_Renamed_Entity
483 (N : Node_Id;
484 Nam : Node_Id;
485 New_S : Entity_Id;
486 Is_Actual : Boolean := False) return Entity_Id;
487 -- Find the renamed entity that corresponds to the given parameter profile
488 -- in a subprogram renaming declaration. The renamed entity may be an
489 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
490 -- indicates that the renaming is the one generated for an actual subpro-
491 -- gram in an instance, for which special visibility checks apply.
493 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
494 -- Find a type derived from Character or Wide_Character in the prefix of N.
495 -- Used to resolved qualified names whose selector is a character literal.
497 function Has_Private_With (E : Entity_Id) return Boolean;
498 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
499 -- private with on E.
501 function Has_Implicit_Operator (N : Node_Id) return Boolean;
502 -- N is an expanded name whose selector is an operator name (e.g. P."+").
503 -- declarative part contains an implicit declaration of an operator if it
504 -- has a declaration of a type to which one of the predefined operators
505 -- apply. The existence of this routine is an implementation artifact. A
506 -- more straightforward but more space-consuming choice would be to make
507 -- all inherited operators explicit in the symbol table.
509 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
510 -- A subprogram defined by a renaming declaration inherits the parameter
511 -- profile of the renamed entity. The subtypes given in the subprogram
512 -- specification are discarded and replaced with those of the renamed
513 -- subprogram, which are then used to recheck the default values.
515 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean;
516 -- True if it is of a task type, a protected type, or else an access to one
517 -- of these types.
519 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean;
520 -- Prefix is appropriate for record if it is of a record type, or an access
521 -- to such.
523 function Most_Descendant_Use_Clause
524 (Clause1 : Entity_Id;
525 Clause2 : Entity_Id) return Entity_Id;
526 -- Determine which use clause parameter is the most descendant in terms of
527 -- scope.
529 procedure Premature_Usage (N : Node_Id);
530 -- Diagnose usage of an entity before it is visible
532 procedure Use_One_Package
533 (N : Node_Id; Pack_Name : Entity_Id := Empty; Force : Boolean := False);
534 -- Make visible entities declared in package P potentially use-visible
535 -- in the current context. Also used in the analysis of subunits, when
536 -- re-installing use clauses of parent units. N is the use_clause that
537 -- names P (and possibly other packages).
539 procedure Use_One_Type
540 (Id : Node_Id; Installed : Boolean := False; Force : Boolean := False);
541 -- Id is the subtype mark from a use_type_clause. This procedure makes
542 -- the primitive operators of the type potentially use-visible. The
543 -- boolean flag Installed indicates that the clause is being reinstalled
544 -- after previous analysis, and primitive operations are already chained
545 -- on the Used_Operations list of the clause.
547 procedure Write_Info;
548 -- Write debugging information on entities declared in current scope
550 --------------------------------
551 -- Analyze_Exception_Renaming --
552 --------------------------------
554 -- The language only allows a single identifier, but the tree holds an
555 -- identifier list. The parser has already issued an error message if
556 -- there is more than one element in the list.
558 procedure Analyze_Exception_Renaming (N : Node_Id) is
559 Id : constant Entity_Id := Defining_Entity (N);
560 Nam : constant Node_Id := Name (N);
562 begin
563 Check_SPARK_05_Restriction ("exception renaming is not allowed", N);
565 Enter_Name (Id);
566 Analyze (Nam);
568 Set_Ekind (Id, E_Exception);
569 Set_Etype (Id, Standard_Exception_Type);
570 Set_Is_Pure (Id, Is_Pure (Current_Scope));
572 if Is_Entity_Name (Nam)
573 and then Present (Entity (Nam))
574 and then Ekind (Entity (Nam)) = E_Exception
575 then
576 if Present (Renamed_Object (Entity (Nam))) then
577 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
578 else
579 Set_Renamed_Object (Id, Entity (Nam));
580 end if;
582 -- The exception renaming declaration may become Ghost if it renames
583 -- a Ghost entity.
585 Mark_Ghost_Renaming (N, Entity (Nam));
586 else
587 Error_Msg_N ("invalid exception name in renaming", Nam);
588 end if;
590 -- Implementation-defined aspect specifications can appear in a renaming
591 -- declaration, but not language-defined ones. The call to procedure
592 -- Analyze_Aspect_Specifications will take care of this error check.
594 if Has_Aspects (N) then
595 Analyze_Aspect_Specifications (N, Id);
596 end if;
597 end Analyze_Exception_Renaming;
599 ---------------------------
600 -- Analyze_Expanded_Name --
601 ---------------------------
603 procedure Analyze_Expanded_Name (N : Node_Id) is
604 begin
605 -- If the entity pointer is already set, this is an internal node, or a
606 -- node that is analyzed more than once, after a tree modification. In
607 -- such a case there is no resolution to perform, just set the type. In
608 -- either case, start by analyzing the prefix.
610 Analyze (Prefix (N));
612 if Present (Entity (N)) then
613 if Is_Type (Entity (N)) then
614 Set_Etype (N, Entity (N));
615 else
616 Set_Etype (N, Etype (Entity (N)));
617 end if;
619 else
620 Find_Expanded_Name (N);
621 end if;
623 -- In either case, propagate dimension of entity to expanded name
625 Analyze_Dimension (N);
626 end Analyze_Expanded_Name;
628 ---------------------------------------
629 -- Analyze_Generic_Function_Renaming --
630 ---------------------------------------
632 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
633 begin
634 Analyze_Generic_Renaming (N, E_Generic_Function);
635 end Analyze_Generic_Function_Renaming;
637 --------------------------------------
638 -- Analyze_Generic_Package_Renaming --
639 --------------------------------------
641 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
642 begin
643 -- Test for the Text_IO special unit case here, since we may be renaming
644 -- one of the subpackages of Text_IO, then join common routine.
646 Check_Text_IO_Special_Unit (Name (N));
648 Analyze_Generic_Renaming (N, E_Generic_Package);
649 end Analyze_Generic_Package_Renaming;
651 ----------------------------------------
652 -- Analyze_Generic_Procedure_Renaming --
653 ----------------------------------------
655 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
656 begin
657 Analyze_Generic_Renaming (N, E_Generic_Procedure);
658 end Analyze_Generic_Procedure_Renaming;
660 ------------------------------
661 -- Analyze_Generic_Renaming --
662 ------------------------------
664 procedure Analyze_Generic_Renaming
665 (N : Node_Id;
666 K : Entity_Kind)
668 New_P : constant Entity_Id := Defining_Entity (N);
669 Inst : Boolean := False;
670 Old_P : Entity_Id;
672 begin
673 if Name (N) = Error then
674 return;
675 end if;
677 Check_SPARK_05_Restriction ("generic renaming is not allowed", N);
679 Generate_Definition (New_P);
681 if Current_Scope /= Standard_Standard then
682 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
683 end if;
685 if Nkind (Name (N)) = N_Selected_Component then
686 Check_Generic_Child_Unit (Name (N), Inst);
687 else
688 Analyze (Name (N));
689 end if;
691 if not Is_Entity_Name (Name (N)) then
692 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
693 Old_P := Any_Id;
694 else
695 Old_P := Entity (Name (N));
696 end if;
698 Enter_Name (New_P);
699 Set_Ekind (New_P, K);
701 if Etype (Old_P) = Any_Type then
702 null;
704 elsif Ekind (Old_P) /= K then
705 Error_Msg_N ("invalid generic unit name", Name (N));
707 else
708 if Present (Renamed_Object (Old_P)) then
709 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
710 else
711 Set_Renamed_Object (New_P, Old_P);
712 end if;
714 -- The generic renaming declaration may become Ghost if it renames a
715 -- Ghost entity.
717 Mark_Ghost_Renaming (N, Old_P);
719 Set_Is_Pure (New_P, Is_Pure (Old_P));
720 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
722 Set_Etype (New_P, Etype (Old_P));
723 Set_Has_Completion (New_P);
725 if In_Open_Scopes (Old_P) then
726 Error_Msg_N ("within its scope, generic denotes its instance", N);
727 end if;
729 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
730 -- renamings and subsequent instantiations of Unchecked_Conversion.
732 if Ekind_In (Old_P, E_Generic_Function, E_Generic_Procedure) then
733 Set_Is_Intrinsic_Subprogram
734 (New_P, Is_Intrinsic_Subprogram (Old_P));
735 end if;
737 Check_Library_Unit_Renaming (N, Old_P);
738 end if;
740 -- Implementation-defined aspect specifications can appear in a renaming
741 -- declaration, but not language-defined ones. The call to procedure
742 -- Analyze_Aspect_Specifications will take care of this error check.
744 if Has_Aspects (N) then
745 Analyze_Aspect_Specifications (N, New_P);
746 end if;
747 end Analyze_Generic_Renaming;
749 -----------------------------
750 -- Analyze_Object_Renaming --
751 -----------------------------
753 procedure Analyze_Object_Renaming (N : Node_Id) is
754 Id : constant Entity_Id := Defining_Identifier (N);
755 Loc : constant Source_Ptr := Sloc (N);
756 Nam : constant Node_Id := Name (N);
757 Dec : Node_Id;
758 T : Entity_Id;
759 T2 : Entity_Id;
761 procedure Check_Constrained_Object;
762 -- If the nominal type is unconstrained but the renamed object is
763 -- constrained, as can happen with renaming an explicit dereference or
764 -- a function return, build a constrained subtype from the object. If
765 -- the renaming is for a formal in an accept statement, the analysis
766 -- has already established its actual subtype. This is only relevant
767 -- if the renamed object is an explicit dereference.
769 ------------------------------
770 -- Check_Constrained_Object --
771 ------------------------------
773 procedure Check_Constrained_Object is
774 Typ : constant Entity_Id := Etype (Nam);
775 Subt : Entity_Id;
777 begin
778 if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference)
779 and then Is_Composite_Type (Etype (Nam))
780 and then not Is_Constrained (Etype (Nam))
781 and then not Has_Unknown_Discriminants (Etype (Nam))
782 and then Expander_Active
783 then
784 -- If Actual_Subtype is already set, nothing to do
786 if Ekind_In (Id, E_Variable, E_Constant)
787 and then Present (Actual_Subtype (Id))
788 then
789 null;
791 -- A renaming of an unchecked union has no actual subtype
793 elsif Is_Unchecked_Union (Typ) then
794 null;
796 -- If a record is limited its size is invariant. This is the case
797 -- in particular with record types with an access discirminant
798 -- that are used in iterators. This is an optimization, but it
799 -- also prevents typing anomalies when the prefix is further
800 -- expanded. Limited types with discriminants are included.
802 elsif Is_Limited_Record (Typ)
803 or else
804 (Ekind (Typ) = E_Limited_Private_Type
805 and then Has_Discriminants (Typ)
806 and then Is_Access_Type (Etype (First_Discriminant (Typ))))
807 then
808 null;
810 else
811 Subt := Make_Temporary (Loc, 'T');
812 Remove_Side_Effects (Nam);
813 Insert_Action (N,
814 Make_Subtype_Declaration (Loc,
815 Defining_Identifier => Subt,
816 Subtype_Indication =>
817 Make_Subtype_From_Expr (Nam, Typ)));
818 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
819 Set_Etype (Nam, Subt);
821 -- Freeze subtype at once, to prevent order of elaboration
822 -- issues in the backend. The renamed object exists, so its
823 -- type is already frozen in any case.
825 Freeze_Before (N, Subt);
826 end if;
827 end if;
828 end Check_Constrained_Object;
830 -- Start of processing for Analyze_Object_Renaming
832 begin
833 if Nam = Error then
834 return;
835 end if;
837 Check_SPARK_05_Restriction ("object renaming is not allowed", N);
839 Set_Is_Pure (Id, Is_Pure (Current_Scope));
840 Enter_Name (Id);
842 -- The renaming of a component that depends on a discriminant requires
843 -- an actual subtype, because in subsequent use of the object Gigi will
844 -- be unable to locate the actual bounds. This explicit step is required
845 -- when the renaming is generated in removing side effects of an
846 -- already-analyzed expression.
848 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
850 -- The object renaming declaration may become Ghost if it renames a
851 -- Ghost entity.
853 if Is_Entity_Name (Nam) then
854 Mark_Ghost_Renaming (N, Entity (Nam));
855 end if;
857 T := Etype (Nam);
858 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
860 if Present (Dec) then
861 Insert_Action (N, Dec);
862 T := Defining_Identifier (Dec);
863 Set_Etype (Nam, T);
864 end if;
866 -- Complete analysis of the subtype mark in any case, for ASIS use
868 if Present (Subtype_Mark (N)) then
869 Find_Type (Subtype_Mark (N));
870 end if;
872 elsif Present (Subtype_Mark (N)) then
873 Find_Type (Subtype_Mark (N));
874 T := Entity (Subtype_Mark (N));
875 Analyze (Nam);
877 -- The object renaming declaration may become Ghost if it renames a
878 -- Ghost entity.
880 if Is_Entity_Name (Nam) then
881 Mark_Ghost_Renaming (N, Entity (Nam));
882 end if;
884 -- Reject renamings of conversions unless the type is tagged, or
885 -- the conversion is implicit (which can occur for cases of anonymous
886 -- access types in Ada 2012).
888 if Nkind (Nam) = N_Type_Conversion
889 and then Comes_From_Source (Nam)
890 and then not Is_Tagged_Type (T)
891 then
892 Error_Msg_N
893 ("renaming of conversion only allowed for tagged types", Nam);
894 end if;
896 Resolve (Nam, T);
898 -- If the renamed object is a function call of a limited type,
899 -- the expansion of the renaming is complicated by the presence
900 -- of various temporaries and subtypes that capture constraints
901 -- of the renamed object. Rewrite node as an object declaration,
902 -- whose expansion is simpler. Given that the object is limited
903 -- there is no copy involved and no performance hit.
905 if Nkind (Nam) = N_Function_Call
906 and then Is_Limited_View (Etype (Nam))
907 and then not Is_Constrained (Etype (Nam))
908 and then Comes_From_Source (N)
909 then
910 Set_Etype (Id, T);
911 Set_Ekind (Id, E_Constant);
912 Rewrite (N,
913 Make_Object_Declaration (Loc,
914 Defining_Identifier => Id,
915 Constant_Present => True,
916 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
917 Expression => Relocate_Node (Nam)));
918 return;
919 end if;
921 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
922 -- when renaming declaration has a named access type. The Ada 2012
923 -- coverage rules allow an anonymous access type in the context of
924 -- an expected named general access type, but the renaming rules
925 -- require the types to be the same. (An exception is when the type
926 -- of the renaming is also an anonymous access type, which can only
927 -- happen due to a renaming created by the expander.)
929 if Nkind (Nam) = N_Type_Conversion
930 and then not Comes_From_Source (Nam)
931 and then Ekind (Etype (Expression (Nam))) = E_Anonymous_Access_Type
932 and then Ekind (T) /= E_Anonymous_Access_Type
933 then
934 Wrong_Type (Expression (Nam), T); -- Should we give better error???
935 end if;
937 -- Check that a class-wide object is not being renamed as an object
938 -- of a specific type. The test for access types is needed to exclude
939 -- cases where the renamed object is a dynamically tagged access
940 -- result, such as occurs in certain expansions.
942 if Is_Tagged_Type (T) then
943 Check_Dynamically_Tagged_Expression
944 (Expr => Nam,
945 Typ => T,
946 Related_Nod => N);
947 end if;
949 -- Ada 2005 (AI-230/AI-254): Access renaming
951 else pragma Assert (Present (Access_Definition (N)));
952 T :=
953 Access_Definition
954 (Related_Nod => N,
955 N => Access_Definition (N));
957 Analyze (Nam);
959 -- The object renaming declaration may become Ghost if it renames a
960 -- Ghost entity.
962 if Is_Entity_Name (Nam) then
963 Mark_Ghost_Renaming (N, Entity (Nam));
964 end if;
966 -- Ada 2005 AI05-105: if the declaration has an anonymous access
967 -- type, the renamed object must also have an anonymous type, and
968 -- this is a name resolution rule. This was implicit in the last part
969 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
970 -- recent AI.
972 if not Is_Overloaded (Nam) then
973 if Ekind (Etype (Nam)) /= Ekind (T) then
974 Error_Msg_N
975 ("expect anonymous access type in object renaming", N);
976 end if;
978 else
979 declare
980 I : Interp_Index;
981 It : Interp;
982 Typ : Entity_Id := Empty;
983 Seen : Boolean := False;
985 begin
986 Get_First_Interp (Nam, I, It);
987 while Present (It.Typ) loop
989 -- Renaming is ambiguous if more than one candidate
990 -- interpretation is type-conformant with the context.
992 if Ekind (It.Typ) = Ekind (T) then
993 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
994 and then
995 Type_Conformant
996 (Designated_Type (T), Designated_Type (It.Typ))
997 then
998 if not Seen then
999 Seen := True;
1000 else
1001 Error_Msg_N
1002 ("ambiguous expression in renaming", Nam);
1003 end if;
1005 elsif Ekind (T) = E_Anonymous_Access_Type
1006 and then
1007 Covers (Designated_Type (T), Designated_Type (It.Typ))
1008 then
1009 if not Seen then
1010 Seen := True;
1011 else
1012 Error_Msg_N
1013 ("ambiguous expression in renaming", Nam);
1014 end if;
1015 end if;
1017 if Covers (T, It.Typ) then
1018 Typ := It.Typ;
1019 Set_Etype (Nam, Typ);
1020 Set_Is_Overloaded (Nam, False);
1021 end if;
1022 end if;
1024 Get_Next_Interp (I, It);
1025 end loop;
1026 end;
1027 end if;
1029 Resolve (Nam, T);
1031 -- Do not perform the legality checks below when the resolution of
1032 -- the renaming name failed because the associated type is Any_Type.
1034 if Etype (Nam) = Any_Type then
1035 null;
1037 -- Ada 2005 (AI-231): In the case where the type is defined by an
1038 -- access_definition, the renamed entity shall be of an access-to-
1039 -- constant type if and only if the access_definition defines an
1040 -- access-to-constant type. ARM 8.5.1(4)
1042 elsif Constant_Present (Access_Definition (N))
1043 and then not Is_Access_Constant (Etype (Nam))
1044 then
1045 Error_Msg_N
1046 ("(Ada 2005): the renamed object is not access-to-constant "
1047 & "(RM 8.5.1(6))", N);
1049 elsif not Constant_Present (Access_Definition (N))
1050 and then Is_Access_Constant (Etype (Nam))
1051 then
1052 Error_Msg_N
1053 ("(Ada 2005): the renamed object is not access-to-variable "
1054 & "(RM 8.5.1(6))", N);
1055 end if;
1057 if Is_Access_Subprogram_Type (Etype (Nam)) then
1058 Check_Subtype_Conformant
1059 (Designated_Type (T), Designated_Type (Etype (Nam)));
1061 elsif not Subtypes_Statically_Match
1062 (Designated_Type (T),
1063 Available_View (Designated_Type (Etype (Nam))))
1064 then
1065 Error_Msg_N
1066 ("subtype of renamed object does not statically match", N);
1067 end if;
1068 end if;
1070 -- Special processing for renaming function return object. Some errors
1071 -- and warnings are produced only for calls that come from source.
1073 if Nkind (Nam) = N_Function_Call then
1074 case Ada_Version is
1076 -- Usage is illegal in Ada 83, but renamings are also introduced
1077 -- during expansion, and error does not apply to those.
1079 when Ada_83 =>
1080 if Comes_From_Source (N) then
1081 Error_Msg_N
1082 ("(Ada 83) cannot rename function return object", Nam);
1083 end if;
1085 -- In Ada 95, warn for odd case of renaming parameterless function
1086 -- call if this is not a limited type (where this is useful).
1088 when others =>
1089 if Warn_On_Object_Renames_Function
1090 and then No (Parameter_Associations (Nam))
1091 and then not Is_Limited_Type (Etype (Nam))
1092 and then Comes_From_Source (Nam)
1093 then
1094 Error_Msg_N
1095 ("renaming function result object is suspicious?R?", Nam);
1096 Error_Msg_NE
1097 ("\function & will be called only once?R?", Nam,
1098 Entity (Name (Nam)));
1099 Error_Msg_N -- CODEFIX
1100 ("\suggest using an initialized constant object "
1101 & "instead?R?", Nam);
1102 end if;
1103 end case;
1104 end if;
1106 Check_Constrained_Object;
1108 -- An object renaming requires an exact match of the type. Class-wide
1109 -- matching is not allowed.
1111 if Is_Class_Wide_Type (T)
1112 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1113 then
1114 Wrong_Type (Nam, T);
1115 end if;
1117 T2 := Etype (Nam);
1119 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1121 if Nkind (Nam) = N_Explicit_Dereference
1122 and then Ekind (Etype (T2)) = E_Incomplete_Type
1123 then
1124 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1125 return;
1127 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1128 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1129 return;
1130 end if;
1132 -- Ada 2005 (AI-327)
1134 if Ada_Version >= Ada_2005
1135 and then Nkind (Nam) = N_Attribute_Reference
1136 and then Attribute_Name (Nam) = Name_Priority
1137 then
1138 null;
1140 elsif Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1141 declare
1142 Nam_Decl : Node_Id;
1143 Nam_Ent : Entity_Id;
1145 begin
1146 if Nkind (Nam) = N_Attribute_Reference then
1147 Nam_Ent := Entity (Prefix (Nam));
1148 else
1149 Nam_Ent := Entity (Nam);
1150 end if;
1152 Nam_Decl := Parent (Nam_Ent);
1154 if Has_Null_Exclusion (N)
1155 and then not Has_Null_Exclusion (Nam_Decl)
1156 then
1157 -- Ada 2005 (AI-423): If the object name denotes a generic
1158 -- formal object of a generic unit G, and the object renaming
1159 -- declaration occurs within the body of G or within the body
1160 -- of a generic unit declared within the declarative region
1161 -- of G, then the declaration of the formal object of G must
1162 -- have a null exclusion or a null-excluding subtype.
1164 if Is_Formal_Object (Nam_Ent)
1165 and then In_Generic_Scope (Id)
1166 then
1167 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1168 Error_Msg_N
1169 ("renamed formal does not exclude `NULL` "
1170 & "(RM 8.5.1(4.6/2))", N);
1172 elsif In_Package_Body (Scope (Id)) then
1173 Error_Msg_N
1174 ("formal object does not have a null exclusion"
1175 & "(RM 8.5.1(4.6/2))", N);
1176 end if;
1178 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1179 -- shall exclude null.
1181 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1182 Error_Msg_N
1183 ("renamed object does not exclude `NULL` "
1184 & "(RM 8.5.1(4.6/2))", N);
1186 -- An instance is illegal if it contains a renaming that
1187 -- excludes null, and the actual does not. The renaming
1188 -- declaration has already indicated that the declaration
1189 -- of the renamed actual in the instance will raise
1190 -- constraint_error.
1192 elsif Nkind (Nam_Decl) = N_Object_Declaration
1193 and then In_Instance
1194 and then
1195 Present (Corresponding_Generic_Association (Nam_Decl))
1196 and then Nkind (Expression (Nam_Decl)) =
1197 N_Raise_Constraint_Error
1198 then
1199 Error_Msg_N
1200 ("renamed actual does not exclude `NULL` "
1201 & "(RM 8.5.1(4.6/2))", N);
1203 -- Finally, if there is a null exclusion, the subtype mark
1204 -- must not be null-excluding.
1206 elsif No (Access_Definition (N))
1207 and then Can_Never_Be_Null (T)
1208 then
1209 Error_Msg_NE
1210 ("`NOT NULL` not allowed (& already excludes null)",
1211 N, T);
1213 end if;
1215 elsif Can_Never_Be_Null (T)
1216 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1217 then
1218 Error_Msg_N
1219 ("renamed object does not exclude `NULL` "
1220 & "(RM 8.5.1(4.6/2))", N);
1222 elsif Has_Null_Exclusion (N)
1223 and then No (Access_Definition (N))
1224 and then Can_Never_Be_Null (T)
1225 then
1226 Error_Msg_NE
1227 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1228 end if;
1229 end;
1230 end if;
1232 -- Set the Ekind of the entity, unless it has been set already, as is
1233 -- the case for the iteration object over a container with no variable
1234 -- indexing. In that case it's been marked as a constant, and we do not
1235 -- want to change it to a variable.
1237 if Ekind (Id) /= E_Constant then
1238 Set_Ekind (Id, E_Variable);
1239 end if;
1241 -- Initialize the object size and alignment. Note that we used to call
1242 -- Init_Size_Align here, but that's wrong for objects which have only
1243 -- an Esize, not an RM_Size field.
1245 Init_Object_Size_Align (Id);
1247 if T = Any_Type or else Etype (Nam) = Any_Type then
1248 return;
1250 -- Verify that the renamed entity is an object or a function call. It
1251 -- may have been rewritten in several ways.
1253 elsif Is_Object_Reference (Nam) then
1254 if Comes_From_Source (N) then
1255 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1256 Error_Msg_N
1257 ("illegal renaming of discriminant-dependent component", Nam);
1258 end if;
1260 -- If the renaming comes from source and the renamed object is a
1261 -- dereference, then mark the prefix as needing debug information,
1262 -- since it might have been rewritten hence internally generated
1263 -- and Debug_Renaming_Declaration will link the renaming to it.
1265 if Nkind (Nam) = N_Explicit_Dereference
1266 and then Is_Entity_Name (Prefix (Nam))
1267 then
1268 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1269 end if;
1270 end if;
1272 -- A static function call may have been folded into a literal
1274 elsif Nkind (Original_Node (Nam)) = N_Function_Call
1276 -- When expansion is disabled, attribute reference is not rewritten
1277 -- as function call. Otherwise it may be rewritten as a conversion,
1278 -- so check original node.
1280 or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference
1281 and then Is_Function_Attribute_Name
1282 (Attribute_Name (Original_Node (Nam))))
1284 -- Weird but legal, equivalent to renaming a function call. Illegal
1285 -- if the literal is the result of constant-folding an attribute
1286 -- reference that is not a function.
1288 or else (Is_Entity_Name (Nam)
1289 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1290 and then
1291 Nkind (Original_Node (Nam)) /= N_Attribute_Reference)
1293 or else (Nkind (Nam) = N_Type_Conversion
1294 and then Is_Tagged_Type (Entity (Subtype_Mark (Nam))))
1295 then
1296 null;
1298 elsif Nkind (Nam) = N_Type_Conversion then
1299 Error_Msg_N
1300 ("renaming of conversion only allowed for tagged types", Nam);
1302 -- Ada 2005 (AI-327)
1304 elsif Ada_Version >= Ada_2005
1305 and then Nkind (Nam) = N_Attribute_Reference
1306 and then Attribute_Name (Nam) = Name_Priority
1307 then
1308 null;
1310 -- Allow internally generated x'Ref resulting in N_Reference node
1312 elsif Nkind (Nam) = N_Reference then
1313 null;
1315 else
1316 Error_Msg_N ("expect object name in renaming", Nam);
1317 end if;
1319 Set_Etype (Id, T2);
1321 if not Is_Variable (Nam) then
1322 Set_Ekind (Id, E_Constant);
1323 Set_Never_Set_In_Source (Id, True);
1324 Set_Is_True_Constant (Id, True);
1325 end if;
1327 -- The entity of the renaming declaration needs to reflect whether the
1328 -- renamed object is volatile. Is_Volatile is set if the renamed object
1329 -- is volatile in the RM legality sense.
1331 Set_Is_Volatile (Id, Is_Volatile_Object (Nam));
1333 -- Also copy settings of Atomic/Independent/Volatile_Full_Access
1335 if Is_Entity_Name (Nam) then
1336 Set_Is_Atomic (Id, Is_Atomic (Entity (Nam)));
1337 Set_Is_Independent (Id, Is_Independent (Entity (Nam)));
1338 Set_Is_Volatile_Full_Access (Id,
1339 Is_Volatile_Full_Access (Entity (Nam)));
1340 end if;
1342 -- Treat as volatile if we just set the Volatile flag
1344 if Is_Volatile (Id)
1346 -- Or if we are renaming an entity which was marked this way
1348 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1350 or else (Is_Entity_Name (Nam)
1351 and then Treat_As_Volatile (Entity (Nam)))
1352 then
1353 Set_Treat_As_Volatile (Id, True);
1354 end if;
1356 -- Now make the link to the renamed object
1358 Set_Renamed_Object (Id, Nam);
1360 -- Implementation-defined aspect specifications can appear in a renaming
1361 -- declaration, but not language-defined ones. The call to procedure
1362 -- Analyze_Aspect_Specifications will take care of this error check.
1364 if Has_Aspects (N) then
1365 Analyze_Aspect_Specifications (N, Id);
1366 end if;
1368 -- Deal with dimensions
1370 Analyze_Dimension (N);
1371 end Analyze_Object_Renaming;
1373 ------------------------------
1374 -- Analyze_Package_Renaming --
1375 ------------------------------
1377 procedure Analyze_Package_Renaming (N : Node_Id) is
1378 New_P : constant Entity_Id := Defining_Entity (N);
1379 Old_P : Entity_Id;
1380 Spec : Node_Id;
1382 begin
1383 if Name (N) = Error then
1384 return;
1385 end if;
1387 -- Check for Text_IO special unit (we may be renaming a Text_IO child)
1389 Check_Text_IO_Special_Unit (Name (N));
1391 if Current_Scope /= Standard_Standard then
1392 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1393 end if;
1395 Enter_Name (New_P);
1396 Analyze (Name (N));
1398 if Is_Entity_Name (Name (N)) then
1399 Old_P := Entity (Name (N));
1400 else
1401 Old_P := Any_Id;
1402 end if;
1404 if Etype (Old_P) = Any_Type then
1405 Error_Msg_N ("expect package name in renaming", Name (N));
1407 elsif Ekind (Old_P) /= E_Package
1408 and then not (Ekind (Old_P) = E_Generic_Package
1409 and then In_Open_Scopes (Old_P))
1410 then
1411 if Ekind (Old_P) = E_Generic_Package then
1412 Error_Msg_N
1413 ("generic package cannot be renamed as a package", Name (N));
1414 else
1415 Error_Msg_Sloc := Sloc (Old_P);
1416 Error_Msg_NE
1417 ("expect package name in renaming, found& declared#",
1418 Name (N), Old_P);
1419 end if;
1421 -- Set basic attributes to minimize cascaded errors
1423 Set_Ekind (New_P, E_Package);
1424 Set_Etype (New_P, Standard_Void_Type);
1426 -- Here for OK package renaming
1428 else
1429 -- Entities in the old package are accessible through the renaming
1430 -- entity. The simplest implementation is to have both packages share
1431 -- the entity list.
1433 Set_Ekind (New_P, E_Package);
1434 Set_Etype (New_P, Standard_Void_Type);
1436 if Present (Renamed_Object (Old_P)) then
1437 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1438 else
1439 Set_Renamed_Object (New_P, Old_P);
1440 end if;
1442 -- The package renaming declaration may become Ghost if it renames a
1443 -- Ghost entity.
1445 Mark_Ghost_Renaming (N, Old_P);
1447 Set_Has_Completion (New_P);
1448 Set_First_Entity (New_P, First_Entity (Old_P));
1449 Set_Last_Entity (New_P, Last_Entity (Old_P));
1450 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1451 Check_Library_Unit_Renaming (N, Old_P);
1452 Generate_Reference (Old_P, Name (N));
1454 -- If the renaming is in the visible part of a package, then we set
1455 -- Renamed_In_Spec for the renamed package, to prevent giving
1456 -- warnings about no entities referenced. Such a warning would be
1457 -- overenthusiastic, since clients can see entities in the renamed
1458 -- package via the visible package renaming.
1460 declare
1461 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1462 begin
1463 if Ekind (Ent) = E_Package
1464 and then not In_Private_Part (Ent)
1465 and then In_Extended_Main_Source_Unit (N)
1466 and then Ekind (Old_P) = E_Package
1467 then
1468 Set_Renamed_In_Spec (Old_P);
1469 end if;
1470 end;
1472 -- If this is the renaming declaration of a package instantiation
1473 -- within itself, it is the declaration that ends the list of actuals
1474 -- for the instantiation. At this point, the subtypes that rename
1475 -- the actuals are flagged as generic, to avoid spurious ambiguities
1476 -- if the actuals for two distinct formals happen to coincide. If
1477 -- the actual is a private type, the subtype has a private completion
1478 -- that is flagged in the same fashion.
1480 -- Resolution is identical to what is was in the original generic.
1481 -- On exit from the generic instance, these are turned into regular
1482 -- subtypes again, so they are compatible with types in their class.
1484 if not Is_Generic_Instance (Old_P) then
1485 return;
1486 else
1487 Spec := Specification (Unit_Declaration_Node (Old_P));
1488 end if;
1490 if Nkind (Spec) = N_Package_Specification
1491 and then Present (Generic_Parent (Spec))
1492 and then Old_P = Current_Scope
1493 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1494 then
1495 declare
1496 E : Entity_Id;
1498 begin
1499 E := First_Entity (Old_P);
1500 while Present (E) and then E /= New_P loop
1501 if Is_Type (E)
1502 and then Nkind (Parent (E)) = N_Subtype_Declaration
1503 then
1504 Set_Is_Generic_Actual_Type (E);
1506 if Is_Private_Type (E)
1507 and then Present (Full_View (E))
1508 then
1509 Set_Is_Generic_Actual_Type (Full_View (E));
1510 end if;
1511 end if;
1513 Next_Entity (E);
1514 end loop;
1515 end;
1516 end if;
1517 end if;
1519 -- Implementation-defined aspect specifications can appear in a renaming
1520 -- declaration, but not language-defined ones. The call to procedure
1521 -- Analyze_Aspect_Specifications will take care of this error check.
1523 if Has_Aspects (N) then
1524 Analyze_Aspect_Specifications (N, New_P);
1525 end if;
1526 end Analyze_Package_Renaming;
1528 -------------------------------
1529 -- Analyze_Renamed_Character --
1530 -------------------------------
1532 procedure Analyze_Renamed_Character
1533 (N : Node_Id;
1534 New_S : Entity_Id;
1535 Is_Body : Boolean)
1537 C : constant Node_Id := Name (N);
1539 begin
1540 if Ekind (New_S) = E_Function then
1541 Resolve (C, Etype (New_S));
1543 if Is_Body then
1544 Check_Frozen_Renaming (N, New_S);
1545 end if;
1547 else
1548 Error_Msg_N ("character literal can only be renamed as function", N);
1549 end if;
1550 end Analyze_Renamed_Character;
1552 ---------------------------------
1553 -- Analyze_Renamed_Dereference --
1554 ---------------------------------
1556 procedure Analyze_Renamed_Dereference
1557 (N : Node_Id;
1558 New_S : Entity_Id;
1559 Is_Body : Boolean)
1561 Nam : constant Node_Id := Name (N);
1562 P : constant Node_Id := Prefix (Nam);
1563 Typ : Entity_Id;
1564 Ind : Interp_Index;
1565 It : Interp;
1567 begin
1568 if not Is_Overloaded (P) then
1569 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1570 or else not Type_Conformant (Etype (Nam), New_S)
1571 then
1572 Error_Msg_N ("designated type does not match specification", P);
1573 else
1574 Resolve (P);
1575 end if;
1577 return;
1579 else
1580 Typ := Any_Type;
1581 Get_First_Interp (Nam, Ind, It);
1583 while Present (It.Nam) loop
1585 if Ekind (It.Nam) = E_Subprogram_Type
1586 and then Type_Conformant (It.Nam, New_S)
1587 then
1588 if Typ /= Any_Id then
1589 Error_Msg_N ("ambiguous renaming", P);
1590 return;
1591 else
1592 Typ := It.Nam;
1593 end if;
1594 end if;
1596 Get_Next_Interp (Ind, It);
1597 end loop;
1599 if Typ = Any_Type then
1600 Error_Msg_N ("designated type does not match specification", P);
1601 else
1602 Resolve (N, Typ);
1604 if Is_Body then
1605 Check_Frozen_Renaming (N, New_S);
1606 end if;
1607 end if;
1608 end if;
1609 end Analyze_Renamed_Dereference;
1611 ---------------------------
1612 -- Analyze_Renamed_Entry --
1613 ---------------------------
1615 procedure Analyze_Renamed_Entry
1616 (N : Node_Id;
1617 New_S : Entity_Id;
1618 Is_Body : Boolean)
1620 Nam : constant Node_Id := Name (N);
1621 Sel : constant Node_Id := Selector_Name (Nam);
1622 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1623 Old_S : Entity_Id;
1625 begin
1626 if Entity (Sel) = Any_Id then
1628 -- Selector is undefined on prefix. Error emitted already
1630 Set_Has_Completion (New_S);
1631 return;
1632 end if;
1634 -- Otherwise find renamed entity and build body of New_S as a call to it
1636 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1638 if Old_S = Any_Id then
1639 Error_Msg_N (" no subprogram or entry matches specification", N);
1640 else
1641 if Is_Body then
1642 Check_Subtype_Conformant (New_S, Old_S, N);
1643 Generate_Reference (New_S, Defining_Entity (N), 'b');
1644 Style.Check_Identifier (Defining_Entity (N), New_S);
1646 else
1647 -- Only mode conformance required for a renaming_as_declaration
1649 Check_Mode_Conformant (New_S, Old_S, N);
1650 end if;
1652 Inherit_Renamed_Profile (New_S, Old_S);
1654 -- The prefix can be an arbitrary expression that yields a task or
1655 -- protected object, so it must be resolved.
1657 Resolve (Prefix (Nam), Scope (Old_S));
1658 end if;
1660 Set_Convention (New_S, Convention (Old_S));
1661 Set_Has_Completion (New_S, Inside_A_Generic);
1663 -- AI05-0225: If the renamed entity is a procedure or entry of a
1664 -- protected object, the target object must be a variable.
1666 if Ekind (Scope (Old_S)) in Protected_Kind
1667 and then Ekind (New_S) = E_Procedure
1668 and then not Is_Variable (Prefix (Nam))
1669 then
1670 if Is_Actual then
1671 Error_Msg_N
1672 ("target object of protected operation used as actual for "
1673 & "formal procedure must be a variable", Nam);
1674 else
1675 Error_Msg_N
1676 ("target object of protected operation renamed as procedure, "
1677 & "must be a variable", Nam);
1678 end if;
1679 end if;
1681 if Is_Body then
1682 Check_Frozen_Renaming (N, New_S);
1683 end if;
1684 end Analyze_Renamed_Entry;
1686 -----------------------------------
1687 -- Analyze_Renamed_Family_Member --
1688 -----------------------------------
1690 procedure Analyze_Renamed_Family_Member
1691 (N : Node_Id;
1692 New_S : Entity_Id;
1693 Is_Body : Boolean)
1695 Nam : constant Node_Id := Name (N);
1696 P : constant Node_Id := Prefix (Nam);
1697 Old_S : Entity_Id;
1699 begin
1700 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1701 or else (Nkind (P) = N_Selected_Component
1702 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1703 then
1704 if Is_Entity_Name (P) then
1705 Old_S := Entity (P);
1706 else
1707 Old_S := Entity (Selector_Name (P));
1708 end if;
1710 if not Entity_Matches_Spec (Old_S, New_S) then
1711 Error_Msg_N ("entry family does not match specification", N);
1713 elsif Is_Body then
1714 Check_Subtype_Conformant (New_S, Old_S, N);
1715 Generate_Reference (New_S, Defining_Entity (N), 'b');
1716 Style.Check_Identifier (Defining_Entity (N), New_S);
1717 end if;
1719 else
1720 Error_Msg_N ("no entry family matches specification", N);
1721 end if;
1723 Set_Has_Completion (New_S, Inside_A_Generic);
1725 if Is_Body then
1726 Check_Frozen_Renaming (N, New_S);
1727 end if;
1728 end Analyze_Renamed_Family_Member;
1730 -----------------------------------------
1731 -- Analyze_Renamed_Primitive_Operation --
1732 -----------------------------------------
1734 procedure Analyze_Renamed_Primitive_Operation
1735 (N : Node_Id;
1736 New_S : Entity_Id;
1737 Is_Body : Boolean)
1739 Old_S : Entity_Id;
1741 function Conforms
1742 (Subp : Entity_Id;
1743 Ctyp : Conformance_Type) return Boolean;
1744 -- Verify that the signatures of the renamed entity and the new entity
1745 -- match. The first formal of the renamed entity is skipped because it
1746 -- is the target object in any subsequent call.
1748 --------------
1749 -- Conforms --
1750 --------------
1752 function Conforms
1753 (Subp : Entity_Id;
1754 Ctyp : Conformance_Type) return Boolean
1756 Old_F : Entity_Id;
1757 New_F : Entity_Id;
1759 begin
1760 if Ekind (Subp) /= Ekind (New_S) then
1761 return False;
1762 end if;
1764 Old_F := Next_Formal (First_Formal (Subp));
1765 New_F := First_Formal (New_S);
1766 while Present (Old_F) and then Present (New_F) loop
1767 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
1768 return False;
1769 end if;
1771 if Ctyp >= Mode_Conformant
1772 and then Ekind (Old_F) /= Ekind (New_F)
1773 then
1774 return False;
1775 end if;
1777 Next_Formal (New_F);
1778 Next_Formal (Old_F);
1779 end loop;
1781 return True;
1782 end Conforms;
1784 -- Start of processing for Analyze_Renamed_Primitive_Operation
1786 begin
1787 if not Is_Overloaded (Selector_Name (Name (N))) then
1788 Old_S := Entity (Selector_Name (Name (N)));
1790 if not Conforms (Old_S, Type_Conformant) then
1791 Old_S := Any_Id;
1792 end if;
1794 else
1795 -- Find the operation that matches the given signature
1797 declare
1798 It : Interp;
1799 Ind : Interp_Index;
1801 begin
1802 Old_S := Any_Id;
1803 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
1805 while Present (It.Nam) loop
1806 if Conforms (It.Nam, Type_Conformant) then
1807 Old_S := It.Nam;
1808 end if;
1810 Get_Next_Interp (Ind, It);
1811 end loop;
1812 end;
1813 end if;
1815 if Old_S = Any_Id then
1816 Error_Msg_N (" no subprogram or entry matches specification", N);
1818 else
1819 if Is_Body then
1820 if not Conforms (Old_S, Subtype_Conformant) then
1821 Error_Msg_N ("subtype conformance error in renaming", N);
1822 end if;
1824 Generate_Reference (New_S, Defining_Entity (N), 'b');
1825 Style.Check_Identifier (Defining_Entity (N), New_S);
1827 else
1828 -- Only mode conformance required for a renaming_as_declaration
1830 if not Conforms (Old_S, Mode_Conformant) then
1831 Error_Msg_N ("mode conformance error in renaming", N);
1832 end if;
1834 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
1835 -- view of a subprogram is intrinsic, because the compiler has
1836 -- to generate a wrapper for any call to it. If the name in a
1837 -- subprogram renaming is a prefixed view, the entity is thus
1838 -- intrinsic, and 'Access cannot be applied to it.
1840 Set_Convention (New_S, Convention_Intrinsic);
1841 end if;
1843 -- Inherit_Renamed_Profile (New_S, Old_S);
1845 -- The prefix can be an arbitrary expression that yields an
1846 -- object, so it must be resolved.
1848 Resolve (Prefix (Name (N)));
1849 end if;
1850 end Analyze_Renamed_Primitive_Operation;
1852 ---------------------------------
1853 -- Analyze_Subprogram_Renaming --
1854 ---------------------------------
1856 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1857 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
1858 Is_Actual : constant Boolean := Present (Formal_Spec);
1859 Nam : constant Node_Id := Name (N);
1860 Save_AV : constant Ada_Version_Type := Ada_Version;
1861 Save_AVP : constant Node_Id := Ada_Version_Pragma;
1862 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
1863 Spec : constant Node_Id := Specification (N);
1865 Old_S : Entity_Id := Empty;
1866 Rename_Spec : Entity_Id;
1868 procedure Build_Class_Wide_Wrapper
1869 (Ren_Id : out Entity_Id;
1870 Wrap_Id : out Entity_Id);
1871 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal
1872 -- type with unknown discriminants and a generic primitive operation of
1873 -- the said type with a box require special processing when the actual
1874 -- is a class-wide type:
1876 -- generic
1877 -- type Formal_Typ (<>) is private;
1878 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
1879 -- package Gen is ...
1881 -- package Inst is new Gen (Actual_Typ'Class);
1883 -- In this case the general renaming mechanism used in the prologue of
1884 -- an instance no longer applies:
1886 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
1888 -- The above is replaced the following wrapper/renaming combination:
1890 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
1891 -- begin
1892 -- Prim_Op (Param); -- primitive
1893 -- end Wrapper;
1895 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
1897 -- This transformation applies only if there is no explicit visible
1898 -- class-wide operation at the point of the instantiation. Ren_Id is
1899 -- the entity of the renaming declaration. When the transformation
1900 -- applies, Wrap_Id is the entity of the generated class-wide wrapper
1901 -- (or Any_Id). Otherwise, Wrap_Id is the entity of the class-wide
1902 -- operation.
1904 procedure Check_Null_Exclusion
1905 (Ren : Entity_Id;
1906 Sub : Entity_Id);
1907 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
1908 -- following AI rules:
1910 -- If Ren is a renaming of a formal subprogram and one of its
1911 -- parameters has a null exclusion, then the corresponding formal
1912 -- in Sub must also have one. Otherwise the subtype of the Sub's
1913 -- formal parameter must exclude null.
1915 -- If Ren is a renaming of a formal function and its return
1916 -- profile has a null exclusion, then Sub's return profile must
1917 -- have one. Otherwise the subtype of Sub's return profile must
1918 -- exclude null.
1920 procedure Freeze_Actual_Profile;
1921 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
1922 -- types: a callable entity freezes its profile, unless it has an
1923 -- incomplete untagged formal (RM 13.14(10.2/3)).
1925 function Has_Class_Wide_Actual return Boolean;
1926 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
1927 -- defaulted formal subprogram where the actual for the controlling
1928 -- formal type is class-wide.
1930 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
1931 -- Find renamed entity when the declaration is a renaming_as_body and
1932 -- the renamed entity may itself be a renaming_as_body. Used to enforce
1933 -- rule that a renaming_as_body is illegal if the declaration occurs
1934 -- before the subprogram it completes is frozen, and renaming indirectly
1935 -- renames the subprogram itself.(Defect Report 8652/0027).
1937 ------------------------------
1938 -- Build_Class_Wide_Wrapper --
1939 ------------------------------
1941 procedure Build_Class_Wide_Wrapper
1942 (Ren_Id : out Entity_Id;
1943 Wrap_Id : out Entity_Id)
1945 Loc : constant Source_Ptr := Sloc (N);
1947 function Build_Call
1948 (Subp_Id : Entity_Id;
1949 Params : List_Id) return Node_Id;
1950 -- Create a dispatching call to invoke routine Subp_Id with actuals
1951 -- built from the parameter specifications of list Params.
1953 function Build_Expr_Fun_Call
1954 (Subp_Id : Entity_Id;
1955 Params : List_Id) return Node_Id;
1956 -- Create a dispatching call to invoke function Subp_Id with actuals
1957 -- built from the parameter specifications of list Params. Return
1958 -- directly the call, so that it can be used inside an expression
1959 -- function. This is a specificity of the GNATprove mode.
1961 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
1962 -- Create a subprogram specification based on the subprogram profile
1963 -- of Subp_Id.
1965 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
1966 -- Find a primitive subprogram of type Typ which matches the profile
1967 -- of the renaming declaration.
1969 procedure Interpretation_Error (Subp_Id : Entity_Id);
1970 -- Emit a continuation error message suggesting subprogram Subp_Id as
1971 -- a possible interpretation.
1973 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean;
1974 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
1975 -- operator.
1977 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean;
1978 -- Determine whether subprogram Subp_Id is a suitable candidate for
1979 -- the role of a wrapped subprogram.
1981 ----------------
1982 -- Build_Call --
1983 ----------------
1985 function Build_Call
1986 (Subp_Id : Entity_Id;
1987 Params : List_Id) return Node_Id
1989 Actuals : constant List_Id := New_List;
1990 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
1991 Formal : Node_Id;
1993 begin
1994 -- Build the actual parameters of the call
1996 Formal := First (Params);
1997 while Present (Formal) loop
1998 Append_To (Actuals,
1999 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2000 Next (Formal);
2001 end loop;
2003 -- Generate:
2004 -- return Subp_Id (Actuals);
2006 if Ekind_In (Subp_Id, E_Function, E_Operator) then
2007 return
2008 Make_Simple_Return_Statement (Loc,
2009 Expression =>
2010 Make_Function_Call (Loc,
2011 Name => Call_Ref,
2012 Parameter_Associations => Actuals));
2014 -- Generate:
2015 -- Subp_Id (Actuals);
2017 else
2018 return
2019 Make_Procedure_Call_Statement (Loc,
2020 Name => Call_Ref,
2021 Parameter_Associations => Actuals);
2022 end if;
2023 end Build_Call;
2025 -------------------------
2026 -- Build_Expr_Fun_Call --
2027 -------------------------
2029 function Build_Expr_Fun_Call
2030 (Subp_Id : Entity_Id;
2031 Params : List_Id) return Node_Id
2033 Actuals : constant List_Id := New_List;
2034 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2035 Formal : Node_Id;
2037 begin
2038 pragma Assert (Ekind_In (Subp_Id, E_Function, E_Operator));
2040 -- Build the actual parameters of the call
2042 Formal := First (Params);
2043 while Present (Formal) loop
2044 Append_To (Actuals,
2045 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2046 Next (Formal);
2047 end loop;
2049 -- Generate:
2050 -- Subp_Id (Actuals);
2052 return
2053 Make_Function_Call (Loc,
2054 Name => Call_Ref,
2055 Parameter_Associations => Actuals);
2056 end Build_Expr_Fun_Call;
2058 ----------------
2059 -- Build_Spec --
2060 ----------------
2062 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2063 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2064 Spec_Id : constant Entity_Id :=
2065 Make_Defining_Identifier (Loc,
2066 Chars => New_External_Name (Chars (Subp_Id), 'R'));
2068 begin
2069 if Ekind (Formal_Spec) = E_Procedure then
2070 return
2071 Make_Procedure_Specification (Loc,
2072 Defining_Unit_Name => Spec_Id,
2073 Parameter_Specifications => Params);
2074 else
2075 return
2076 Make_Function_Specification (Loc,
2077 Defining_Unit_Name => Spec_Id,
2078 Parameter_Specifications => Params,
2079 Result_Definition =>
2080 New_Copy_Tree (Result_Definition (Spec)));
2081 end if;
2082 end Build_Spec;
2084 --------------------
2085 -- Find_Primitive --
2086 --------------------
2088 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2089 procedure Replace_Parameter_Types (Spec : Node_Id);
2090 -- Given a specification Spec, replace all class-wide parameter
2091 -- types with reference to type Typ.
2093 -----------------------------
2094 -- Replace_Parameter_Types --
2095 -----------------------------
2097 procedure Replace_Parameter_Types (Spec : Node_Id) is
2098 Formal : Node_Id;
2099 Formal_Id : Entity_Id;
2100 Formal_Typ : Node_Id;
2102 begin
2103 Formal := First (Parameter_Specifications (Spec));
2104 while Present (Formal) loop
2105 Formal_Id := Defining_Identifier (Formal);
2106 Formal_Typ := Parameter_Type (Formal);
2108 -- Create a new entity for each class-wide formal to prevent
2109 -- aliasing with the original renaming. Replace the type of
2110 -- such a parameter with the candidate type.
2112 if Nkind (Formal_Typ) = N_Identifier
2113 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2114 then
2115 Set_Defining_Identifier (Formal,
2116 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2118 Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc));
2119 end if;
2121 Next (Formal);
2122 end loop;
2123 end Replace_Parameter_Types;
2125 -- Local variables
2127 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2128 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2129 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2130 Subp_Id : Entity_Id;
2132 -- Start of processing for Find_Primitive
2134 begin
2135 -- Each attempt to find a suitable primitive of a particular type
2136 -- operates on its own copy of the original renaming. As a result
2137 -- the original renaming is kept decoration and side-effect free.
2139 -- Inherit the overloaded status of the renamed subprogram name
2141 if Is_Overloaded (Nam) then
2142 Set_Is_Overloaded (Alt_Nam);
2143 Save_Interps (Nam, Alt_Nam);
2144 end if;
2146 -- The copied renaming is hidden from visibility to prevent the
2147 -- pollution of the enclosing context.
2149 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2151 -- The types of all class-wide parameters must be changed to the
2152 -- candidate type.
2154 Replace_Parameter_Types (Alt_Spec);
2156 -- Try to find a suitable primitive which matches the altered
2157 -- profile of the renaming specification.
2159 Subp_Id :=
2160 Find_Renamed_Entity
2161 (N => Alt_Ren,
2162 Nam => Name (Alt_Ren),
2163 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2164 Is_Actual => Is_Actual);
2166 -- Do not return Any_Id if the resolion of the altered profile
2167 -- failed as this complicates further checks on the caller side,
2168 -- return Empty instead.
2170 if Subp_Id = Any_Id then
2171 return Empty;
2172 else
2173 return Subp_Id;
2174 end if;
2175 end Find_Primitive;
2177 --------------------------
2178 -- Interpretation_Error --
2179 --------------------------
2181 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2182 begin
2183 Error_Msg_Sloc := Sloc (Subp_Id);
2185 if Is_Internal (Subp_Id) then
2186 Error_Msg_NE
2187 ("\\possible interpretation: predefined & #",
2188 Spec, Formal_Spec);
2189 else
2190 Error_Msg_NE
2191 ("\\possible interpretation: & defined #", Spec, Formal_Spec);
2192 end if;
2193 end Interpretation_Error;
2195 ---------------------------
2196 -- Is_Intrinsic_Equality --
2197 ---------------------------
2199 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is
2200 begin
2201 return
2202 Ekind (Subp_Id) = E_Operator
2203 and then Chars (Subp_Id) = Name_Op_Eq
2204 and then Is_Intrinsic_Subprogram (Subp_Id);
2205 end Is_Intrinsic_Equality;
2207 ---------------------------
2208 -- Is_Suitable_Candidate --
2209 ---------------------------
2211 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is
2212 begin
2213 if No (Subp_Id) then
2214 return False;
2216 -- An intrinsic subprogram is never a good candidate. This is an
2217 -- indication of a missing primitive, either defined directly or
2218 -- inherited from a parent tagged type.
2220 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2221 return False;
2223 else
2224 return True;
2225 end if;
2226 end Is_Suitable_Candidate;
2228 -- Local variables
2230 Actual_Typ : Entity_Id := Empty;
2231 -- The actual class-wide type for Formal_Typ
2233 CW_Prim_OK : Boolean;
2234 CW_Prim_Op : Entity_Id;
2235 -- The class-wide subprogram (if available) which corresponds to the
2236 -- renamed generic formal subprogram.
2238 Formal_Typ : Entity_Id := Empty;
2239 -- The generic formal type with unknown discriminants
2241 Root_Prim_OK : Boolean;
2242 Root_Prim_Op : Entity_Id;
2243 -- The root type primitive (if available) which corresponds to the
2244 -- renamed generic formal subprogram.
2246 Root_Typ : Entity_Id := Empty;
2247 -- The root type of Actual_Typ
2249 Body_Decl : Node_Id;
2250 Formal : Node_Id;
2251 Prim_Op : Entity_Id;
2252 Spec_Decl : Node_Id;
2253 New_Spec : Node_Id;
2255 -- Start of processing for Build_Class_Wide_Wrapper
2257 begin
2258 -- Analyze the specification of the renaming in case the generation
2259 -- of the class-wide wrapper fails.
2261 Ren_Id := Analyze_Subprogram_Specification (Spec);
2262 Wrap_Id := Any_Id;
2264 -- Do not attempt to build a wrapper if the renaming is in error
2266 if Error_Posted (Nam) then
2267 return;
2268 end if;
2270 -- Analyze the renamed name, but do not resolve it. The resolution is
2271 -- completed once a suitable subprogram is found.
2273 Analyze (Nam);
2275 -- When the renamed name denotes the intrinsic operator equals, the
2276 -- name must be treated as overloaded. This allows for a potential
2277 -- match against the root type's predefined equality function.
2279 if Is_Intrinsic_Equality (Entity (Nam)) then
2280 Set_Is_Overloaded (Nam);
2281 Collect_Interps (Nam);
2282 end if;
2284 -- Step 1: Find the generic formal type with unknown discriminants
2285 -- and its corresponding class-wide actual type from the renamed
2286 -- generic formal subprogram.
2288 Formal := First_Formal (Formal_Spec);
2289 while Present (Formal) loop
2290 if Has_Unknown_Discriminants (Etype (Formal))
2291 and then not Is_Class_Wide_Type (Etype (Formal))
2292 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2293 then
2294 Formal_Typ := Etype (Formal);
2295 Actual_Typ := Get_Instance_Of (Formal_Typ);
2296 Root_Typ := Etype (Actual_Typ);
2297 exit;
2298 end if;
2300 Next_Formal (Formal);
2301 end loop;
2303 -- The specification of the generic formal subprogram should always
2304 -- contain a formal type with unknown discriminants whose actual is
2305 -- a class-wide type, otherwise this indicates a failure in routine
2306 -- Has_Class_Wide_Actual.
2308 pragma Assert (Present (Formal_Typ));
2310 -- Step 2: Find the proper class-wide subprogram or primitive which
2311 -- corresponds to the renamed generic formal subprogram.
2313 CW_Prim_Op := Find_Primitive (Actual_Typ);
2314 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2315 Root_Prim_Op := Find_Primitive (Root_Typ);
2316 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2318 -- The class-wide actual type has two subprograms which correspond to
2319 -- the renamed generic formal subprogram:
2321 -- with procedure Prim_Op (Param : Formal_Typ);
2323 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2324 -- procedure Prim_Op (Param : Actual_Typ'Class);
2326 -- Even though the declaration of the two subprograms is legal, a
2327 -- call to either one is ambiguous and therefore illegal.
2329 if CW_Prim_OK and Root_Prim_OK then
2331 -- A user-defined primitive has precedence over a predefined one
2333 if Is_Internal (CW_Prim_Op)
2334 and then not Is_Internal (Root_Prim_Op)
2335 then
2336 Prim_Op := Root_Prim_Op;
2338 elsif Is_Internal (Root_Prim_Op)
2339 and then not Is_Internal (CW_Prim_Op)
2340 then
2341 Prim_Op := CW_Prim_Op;
2343 elsif CW_Prim_Op = Root_Prim_Op then
2344 Prim_Op := Root_Prim_Op;
2346 -- Otherwise both candidate subprograms are user-defined and
2347 -- ambiguous.
2349 else
2350 Error_Msg_NE
2351 ("ambiguous actual for generic subprogram &",
2352 Spec, Formal_Spec);
2353 Interpretation_Error (Root_Prim_Op);
2354 Interpretation_Error (CW_Prim_Op);
2355 return;
2356 end if;
2358 elsif CW_Prim_OK and not Root_Prim_OK then
2359 Prim_Op := CW_Prim_Op;
2361 elsif not CW_Prim_OK and Root_Prim_OK then
2362 Prim_Op := Root_Prim_Op;
2364 -- An intrinsic equality may act as a suitable candidate in the case
2365 -- of a null type extension where the parent's equality is hidden. A
2366 -- call to an intrinsic equality is expanded as dispatching.
2368 elsif Present (Root_Prim_Op)
2369 and then Is_Intrinsic_Equality (Root_Prim_Op)
2370 then
2371 Prim_Op := Root_Prim_Op;
2373 -- Otherwise there are no candidate subprograms. Let the caller
2374 -- diagnose the error.
2376 else
2377 return;
2378 end if;
2380 -- At this point resolution has taken place and the name is no longer
2381 -- overloaded. Mark the primitive as referenced.
2383 Set_Is_Overloaded (Name (N), False);
2384 Set_Referenced (Prim_Op);
2386 -- Do not generate a wrapper when the only candidate is a class-wide
2387 -- subprogram. Instead modify the renaming to directly map the actual
2388 -- to the generic formal.
2390 if CW_Prim_OK and then Prim_Op = CW_Prim_Op then
2391 Wrap_Id := Prim_Op;
2392 Rewrite (Nam, New_Occurrence_Of (Prim_Op, Loc));
2393 return;
2394 end if;
2396 -- Step 3: Create the declaration and the body of the wrapper, insert
2397 -- all the pieces into the tree.
2399 -- In GNATprove mode, create a function wrapper in the form of an
2400 -- expression function, so that an implicit postcondition relating
2401 -- the result of calling the wrapper function and the result of the
2402 -- dispatching call to the wrapped function is known during proof.
2404 if GNATprove_Mode
2405 and then Ekind_In (Ren_Id, E_Function, E_Operator)
2406 then
2407 New_Spec := Build_Spec (Ren_Id);
2408 Body_Decl :=
2409 Make_Expression_Function (Loc,
2410 Specification => New_Spec,
2411 Expression =>
2412 Build_Expr_Fun_Call
2413 (Subp_Id => Prim_Op,
2414 Params => Parameter_Specifications (New_Spec)));
2416 Wrap_Id := Defining_Entity (Body_Decl);
2418 -- Otherwise, create separate spec and body for the subprogram
2420 else
2421 Spec_Decl :=
2422 Make_Subprogram_Declaration (Loc,
2423 Specification => Build_Spec (Ren_Id));
2424 Insert_Before_And_Analyze (N, Spec_Decl);
2426 Wrap_Id := Defining_Entity (Spec_Decl);
2428 Body_Decl :=
2429 Make_Subprogram_Body (Loc,
2430 Specification => Build_Spec (Ren_Id),
2431 Declarations => New_List,
2432 Handled_Statement_Sequence =>
2433 Make_Handled_Sequence_Of_Statements (Loc,
2434 Statements => New_List (
2435 Build_Call
2436 (Subp_Id => Prim_Op,
2437 Params =>
2438 Parameter_Specifications
2439 (Specification (Spec_Decl))))));
2441 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
2442 end if;
2444 -- If the operator carries an Eliminated pragma, indicate that the
2445 -- wrapper is also to be eliminated, to prevent spurious error when
2446 -- using gnatelim on programs that include box-initialization of
2447 -- equality operators.
2449 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2451 -- In GNATprove mode, insert the body in the tree for analysis
2453 if GNATprove_Mode then
2454 Insert_Before_And_Analyze (N, Body_Decl);
2455 end if;
2457 -- The generated body does not freeze and must be analyzed when the
2458 -- class-wide wrapper is frozen. The body is only needed if expansion
2459 -- is enabled.
2461 if Expander_Active then
2462 Append_Freeze_Action (Wrap_Id, Body_Decl);
2463 end if;
2465 -- Step 4: The subprogram renaming aliases the wrapper
2467 Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc));
2468 end Build_Class_Wide_Wrapper;
2470 --------------------------
2471 -- Check_Null_Exclusion --
2472 --------------------------
2474 procedure Check_Null_Exclusion
2475 (Ren : Entity_Id;
2476 Sub : Entity_Id)
2478 Ren_Formal : Entity_Id;
2479 Sub_Formal : Entity_Id;
2481 begin
2482 -- Parameter check
2484 Ren_Formal := First_Formal (Ren);
2485 Sub_Formal := First_Formal (Sub);
2486 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2487 if Has_Null_Exclusion (Parent (Ren_Formal))
2488 and then
2489 not (Has_Null_Exclusion (Parent (Sub_Formal))
2490 or else Can_Never_Be_Null (Etype (Sub_Formal)))
2491 then
2492 Error_Msg_NE
2493 ("`NOT NULL` required for parameter &",
2494 Parent (Sub_Formal), Sub_Formal);
2495 end if;
2497 Next_Formal (Ren_Formal);
2498 Next_Formal (Sub_Formal);
2499 end loop;
2501 -- Return profile check
2503 if Nkind (Parent (Ren)) = N_Function_Specification
2504 and then Nkind (Parent (Sub)) = N_Function_Specification
2505 and then Has_Null_Exclusion (Parent (Ren))
2506 and then not (Has_Null_Exclusion (Parent (Sub))
2507 or else Can_Never_Be_Null (Etype (Sub)))
2508 then
2509 Error_Msg_N
2510 ("return must specify `NOT NULL`",
2511 Result_Definition (Parent (Sub)));
2512 end if;
2513 end Check_Null_Exclusion;
2515 ---------------------------
2516 -- Freeze_Actual_Profile --
2517 ---------------------------
2519 procedure Freeze_Actual_Profile is
2520 F : Entity_Id;
2521 Has_Untagged_Inc : Boolean;
2522 Instantiation_Node : constant Node_Id := Parent (N);
2524 begin
2525 if Ada_Version >= Ada_2012 then
2526 F := First_Formal (Formal_Spec);
2527 Has_Untagged_Inc := False;
2528 while Present (F) loop
2529 if Ekind (Etype (F)) = E_Incomplete_Type
2530 and then not Is_Tagged_Type (Etype (F))
2531 then
2532 Has_Untagged_Inc := True;
2533 exit;
2534 end if;
2536 F := Next_Formal (F);
2537 end loop;
2539 if Ekind (Formal_Spec) = E_Function
2540 and then not Is_Tagged_Type (Etype (Formal_Spec))
2541 then
2542 Has_Untagged_Inc := True;
2543 end if;
2545 if not Has_Untagged_Inc then
2546 F := First_Formal (Old_S);
2547 while Present (F) loop
2548 Freeze_Before (Instantiation_Node, Etype (F));
2550 if Is_Incomplete_Or_Private_Type (Etype (F))
2551 and then No (Underlying_Type (Etype (F)))
2552 then
2553 -- Exclude generic types, or types derived from them.
2554 -- They will be frozen in the enclosing instance.
2556 if Is_Generic_Type (Etype (F))
2557 or else Is_Generic_Type (Root_Type (Etype (F)))
2558 then
2559 null;
2561 -- A limited view of a type declared elsewhere needs no
2562 -- freezing actions.
2564 elsif From_Limited_With (Etype (F)) then
2565 null;
2567 else
2568 Error_Msg_NE
2569 ("type& must be frozen before this point",
2570 Instantiation_Node, Etype (F));
2571 end if;
2572 end if;
2574 F := Next_Formal (F);
2575 end loop;
2576 end if;
2577 end if;
2578 end Freeze_Actual_Profile;
2580 ---------------------------
2581 -- Has_Class_Wide_Actual --
2582 ---------------------------
2584 function Has_Class_Wide_Actual return Boolean is
2585 Formal : Entity_Id;
2586 Formal_Typ : Entity_Id;
2588 begin
2589 if Is_Actual then
2590 Formal := First_Formal (Formal_Spec);
2591 while Present (Formal) loop
2592 Formal_Typ := Etype (Formal);
2594 if Has_Unknown_Discriminants (Formal_Typ)
2595 and then not Is_Class_Wide_Type (Formal_Typ)
2596 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2597 then
2598 return True;
2599 end if;
2601 Next_Formal (Formal);
2602 end loop;
2603 end if;
2605 return False;
2606 end Has_Class_Wide_Actual;
2608 -------------------------
2609 -- Original_Subprogram --
2610 -------------------------
2612 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2613 Orig_Decl : Node_Id;
2614 Orig_Subp : Entity_Id;
2616 begin
2617 -- First case: renamed entity is itself a renaming
2619 if Present (Alias (Subp)) then
2620 return Alias (Subp);
2622 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2623 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2624 then
2625 -- Check if renamed entity is a renaming_as_body
2627 Orig_Decl :=
2628 Unit_Declaration_Node
2629 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2631 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2632 Orig_Subp := Entity (Name (Orig_Decl));
2634 if Orig_Subp = Rename_Spec then
2636 -- Circularity detected
2638 return Orig_Subp;
2640 else
2641 return (Original_Subprogram (Orig_Subp));
2642 end if;
2643 else
2644 return Subp;
2645 end if;
2646 else
2647 return Subp;
2648 end if;
2649 end Original_Subprogram;
2651 -- Local variables
2653 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2654 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2655 -- defaulted formal subprogram when the actual for a related formal
2656 -- type is class-wide.
2658 Inst_Node : Node_Id := Empty;
2659 New_S : Entity_Id;
2661 -- Start of processing for Analyze_Subprogram_Renaming
2663 begin
2664 -- We must test for the attribute renaming case before the Analyze
2665 -- call because otherwise Sem_Attr will complain that the attribute
2666 -- is missing an argument when it is analyzed.
2668 if Nkind (Nam) = N_Attribute_Reference then
2670 -- In the case of an abstract formal subprogram association, rewrite
2671 -- an actual given by a stream attribute as the name of the
2672 -- corresponding stream primitive of the type.
2674 -- In a generic context the stream operations are not generated, and
2675 -- this must be treated as a normal attribute reference, to be
2676 -- expanded in subsequent instantiations.
2678 if Is_Actual
2679 and then Is_Abstract_Subprogram (Formal_Spec)
2680 and then Expander_Active
2681 then
2682 declare
2683 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
2684 Stream_Prim : Entity_Id;
2686 begin
2687 -- The class-wide forms of the stream attributes are not
2688 -- primitive dispatching operations (even though they
2689 -- internally dispatch to a stream attribute).
2691 if Is_Class_Wide_Type (Prefix_Type) then
2692 Error_Msg_N
2693 ("attribute must be a primitive dispatching operation",
2694 Nam);
2695 return;
2696 end if;
2698 -- Retrieve the primitive subprogram associated with the
2699 -- attribute. This can only be a stream attribute, since those
2700 -- are the only ones that are dispatching (and the actual for
2701 -- an abstract formal subprogram must be dispatching
2702 -- operation).
2704 case Attribute_Name (Nam) is
2705 when Name_Input =>
2706 Stream_Prim :=
2707 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input);
2709 when Name_Output =>
2710 Stream_Prim :=
2711 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output);
2713 when Name_Read =>
2714 Stream_Prim :=
2715 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read);
2717 when Name_Write =>
2718 Stream_Prim :=
2719 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write);
2721 when others =>
2722 Error_Msg_N
2723 ("attribute must be a primitive dispatching operation",
2724 Nam);
2725 return;
2726 end case;
2728 -- If no operation was found, and the type is limited, the user
2729 -- should have defined one.
2731 if No (Stream_Prim) then
2732 if Is_Limited_Type (Prefix_Type) then
2733 Error_Msg_NE
2734 ("stream operation not defined for type&",
2735 N, Prefix_Type);
2736 return;
2738 -- Otherwise, compiler should have generated default
2740 else
2741 raise Program_Error;
2742 end if;
2743 end if;
2745 -- Rewrite the attribute into the name of its corresponding
2746 -- primitive dispatching subprogram. We can then proceed with
2747 -- the usual processing for subprogram renamings.
2749 declare
2750 Prim_Name : constant Node_Id :=
2751 Make_Identifier (Sloc (Nam),
2752 Chars => Chars (Stream_Prim));
2753 begin
2754 Set_Entity (Prim_Name, Stream_Prim);
2755 Rewrite (Nam, Prim_Name);
2756 Analyze (Nam);
2757 end;
2758 end;
2760 -- Normal processing for a renaming of an attribute
2762 else
2763 Attribute_Renaming (N);
2764 return;
2765 end if;
2766 end if;
2768 -- Check whether this declaration corresponds to the instantiation of a
2769 -- formal subprogram.
2771 -- If this is an instantiation, the corresponding actual is frozen and
2772 -- error messages can be made more precise. If this is a default
2773 -- subprogram, the entity is already established in the generic, and is
2774 -- not retrieved by visibility. If it is a default with a box, the
2775 -- candidate interpretations, if any, have been collected when building
2776 -- the renaming declaration. If overloaded, the proper interpretation is
2777 -- determined in Find_Renamed_Entity. If the entity is an operator,
2778 -- Find_Renamed_Entity applies additional visibility checks.
2780 if Is_Actual then
2781 Inst_Node := Unit_Declaration_Node (Formal_Spec);
2783 -- Check whether the renaming is for a defaulted actual subprogram
2784 -- with a class-wide actual.
2786 -- The class-wide wrapper is not needed in GNATprove_Mode and there
2787 -- is an external axiomatization on the package.
2789 if CW_Actual
2790 and then Box_Present (Inst_Node)
2791 and then not
2792 (GNATprove_Mode
2793 and then
2794 Present (Containing_Package_With_Ext_Axioms (Formal_Spec)))
2795 then
2796 Build_Class_Wide_Wrapper (New_S, Old_S);
2798 elsif Is_Entity_Name (Nam)
2799 and then Present (Entity (Nam))
2800 and then not Comes_From_Source (Nam)
2801 and then not Is_Overloaded (Nam)
2802 then
2803 Old_S := Entity (Nam);
2805 -- The subprogram renaming declaration may become Ghost if it
2806 -- renames a Ghost entity.
2808 Mark_Ghost_Renaming (N, Old_S);
2810 New_S := Analyze_Subprogram_Specification (Spec);
2812 -- Operator case
2814 if Ekind (Old_S) = E_Operator then
2816 -- Box present
2818 if Box_Present (Inst_Node) then
2819 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2821 -- If there is an immediately visible homonym of the operator
2822 -- and the declaration has a default, this is worth a warning
2823 -- because the user probably did not intend to get the pre-
2824 -- defined operator, visible in the generic declaration. To
2825 -- find if there is an intended candidate, analyze the renaming
2826 -- again in the current context.
2828 elsif Scope (Old_S) = Standard_Standard
2829 and then Present (Default_Name (Inst_Node))
2830 then
2831 declare
2832 Decl : constant Node_Id := New_Copy_Tree (N);
2833 Hidden : Entity_Id;
2835 begin
2836 Set_Entity (Name (Decl), Empty);
2837 Analyze (Name (Decl));
2838 Hidden :=
2839 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
2841 if Present (Hidden)
2842 and then In_Open_Scopes (Scope (Hidden))
2843 and then Is_Immediately_Visible (Hidden)
2844 and then Comes_From_Source (Hidden)
2845 and then Hidden /= Old_S
2846 then
2847 Error_Msg_Sloc := Sloc (Hidden);
2848 Error_Msg_N
2849 ("default subprogram is resolved in the generic "
2850 & "declaration (RM 12.6(17))??", N);
2851 Error_Msg_NE ("\and will not use & #??", N, Hidden);
2852 end if;
2853 end;
2854 end if;
2855 end if;
2857 else
2858 Analyze (Nam);
2860 -- The subprogram renaming declaration may become Ghost if it
2861 -- renames a Ghost entity.
2863 if Is_Entity_Name (Nam) then
2864 Mark_Ghost_Renaming (N, Entity (Nam));
2865 end if;
2867 New_S := Analyze_Subprogram_Specification (Spec);
2868 end if;
2870 else
2871 -- Renamed entity must be analyzed first, to avoid being hidden by
2872 -- new name (which might be the same in a generic instance).
2874 Analyze (Nam);
2876 -- The subprogram renaming declaration may become Ghost if it renames
2877 -- a Ghost entity.
2879 if Is_Entity_Name (Nam) then
2880 Mark_Ghost_Renaming (N, Entity (Nam));
2881 end if;
2883 -- The renaming defines a new overloaded entity, which is analyzed
2884 -- like a subprogram declaration.
2886 New_S := Analyze_Subprogram_Specification (Spec);
2887 end if;
2889 if Current_Scope /= Standard_Standard then
2890 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
2891 end if;
2893 -- Set SPARK mode from current context
2895 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
2896 Set_SPARK_Pragma_Inherited (New_S);
2898 Rename_Spec := Find_Corresponding_Spec (N);
2900 -- Case of Renaming_As_Body
2902 if Present (Rename_Spec) then
2903 Check_Previous_Null_Procedure (N, Rename_Spec);
2905 -- Renaming declaration is the completion of the declaration of
2906 -- Rename_Spec. We build an actual body for it at the freezing point.
2908 Set_Corresponding_Spec (N, Rename_Spec);
2910 -- Deal with special case of stream functions of abstract types
2911 -- and interfaces.
2913 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
2914 N_Abstract_Subprogram_Declaration
2915 then
2916 -- Input stream functions are abstract if the object type is
2917 -- abstract. Similarly, all default stream functions for an
2918 -- interface type are abstract. However, these subprograms may
2919 -- receive explicit declarations in representation clauses, making
2920 -- the attribute subprograms usable as defaults in subsequent
2921 -- type extensions.
2922 -- In this case we rewrite the declaration to make the subprogram
2923 -- non-abstract. We remove the previous declaration, and insert
2924 -- the new one at the point of the renaming, to prevent premature
2925 -- access to unfrozen types. The new declaration reuses the
2926 -- specification of the previous one, and must not be analyzed.
2928 pragma Assert
2929 (Is_Primitive (Entity (Nam))
2930 and then
2931 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
2932 declare
2933 Old_Decl : constant Node_Id :=
2934 Unit_Declaration_Node (Rename_Spec);
2935 New_Decl : constant Node_Id :=
2936 Make_Subprogram_Declaration (Sloc (N),
2937 Specification =>
2938 Relocate_Node (Specification (Old_Decl)));
2939 begin
2940 Remove (Old_Decl);
2941 Insert_After (N, New_Decl);
2942 Set_Is_Abstract_Subprogram (Rename_Spec, False);
2943 Set_Analyzed (New_Decl);
2944 end;
2945 end if;
2947 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
2949 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2950 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
2951 end if;
2953 Set_Convention (New_S, Convention (Rename_Spec));
2954 Check_Fully_Conformant (New_S, Rename_Spec);
2955 Set_Public_Status (New_S);
2957 if No_Return (Rename_Spec)
2958 and then not No_Return (Entity (Nam))
2959 then
2960 Error_Msg_N ("renaming completes a No_Return procedure", N);
2961 Error_Msg_N
2962 ("\renamed procedure must be nonreturning (RM 6.5.1 (7/2))", N);
2963 end if;
2965 -- The specification does not introduce new formals, but only
2966 -- repeats the formals of the original subprogram declaration.
2967 -- For cross-reference purposes, and for refactoring tools, we
2968 -- treat the formals of the renaming declaration as body formals.
2970 Reference_Body_Formals (Rename_Spec, New_S);
2972 -- Indicate that the entity in the declaration functions like the
2973 -- corresponding body, and is not a new entity. The body will be
2974 -- constructed later at the freeze point, so indicate that the
2975 -- completion has not been seen yet.
2977 Set_Ekind (New_S, E_Subprogram_Body);
2978 New_S := Rename_Spec;
2979 Set_Has_Completion (Rename_Spec, False);
2981 -- Ada 2005: check overriding indicator
2983 if Present (Overridden_Operation (Rename_Spec)) then
2984 if Must_Not_Override (Specification (N)) then
2985 Error_Msg_NE
2986 ("subprogram& overrides inherited operation",
2987 N, Rename_Spec);
2989 elsif Style_Check
2990 and then not Must_Override (Specification (N))
2991 then
2992 Style.Missing_Overriding (N, Rename_Spec);
2993 end if;
2995 elsif Must_Override (Specification (N)) then
2996 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
2997 end if;
2999 -- Normal subprogram renaming (not renaming as body)
3001 else
3002 Generate_Definition (New_S);
3003 New_Overloaded_Entity (New_S);
3005 if Is_Entity_Name (Nam)
3006 and then Is_Intrinsic_Subprogram (Entity (Nam))
3007 then
3008 null;
3009 else
3010 Check_Delayed_Subprogram (New_S);
3011 end if;
3012 end if;
3014 -- There is no need for elaboration checks on the new entity, which may
3015 -- be called before the next freezing point where the body will appear.
3016 -- Elaboration checks refer to the real entity, not the one created by
3017 -- the renaming declaration.
3019 Set_Kill_Elaboration_Checks (New_S, True);
3021 -- If we had a previous error, indicate a completely is present to stop
3022 -- junk cascaded messages, but don't take any further action.
3024 if Etype (Nam) = Any_Type then
3025 Set_Has_Completion (New_S);
3026 return;
3028 -- Case where name has the form of a selected component
3030 elsif Nkind (Nam) = N_Selected_Component then
3032 -- A name which has the form A.B can designate an entry of task A, a
3033 -- protected operation of protected object A, or finally a primitive
3034 -- operation of object A. In the later case, A is an object of some
3035 -- tagged type, or an access type that denotes one such. To further
3036 -- distinguish these cases, note that the scope of a task entry or
3037 -- protected operation is type of the prefix.
3039 -- The prefix could be an overloaded function call that returns both
3040 -- kinds of operations. This overloading pathology is left to the
3041 -- dedicated reader ???
3043 declare
3044 T : constant Entity_Id := Etype (Prefix (Nam));
3046 begin
3047 if Present (T)
3048 and then
3049 (Is_Tagged_Type (T)
3050 or else
3051 (Is_Access_Type (T)
3052 and then Is_Tagged_Type (Designated_Type (T))))
3053 and then Scope (Entity (Selector_Name (Nam))) /= T
3054 then
3055 Analyze_Renamed_Primitive_Operation
3056 (N, New_S, Present (Rename_Spec));
3057 return;
3059 else
3060 -- Renamed entity is an entry or protected operation. For those
3061 -- cases an explicit body is built (at the point of freezing of
3062 -- this entity) that contains a call to the renamed entity.
3064 -- This is not allowed for renaming as body if the renamed
3065 -- spec is already frozen (see RM 8.5.4(5) for details).
3067 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
3068 Error_Msg_N
3069 ("renaming-as-body cannot rename entry as subprogram", N);
3070 Error_Msg_NE
3071 ("\since & is already frozen (RM 8.5.4(5))",
3072 N, Rename_Spec);
3073 else
3074 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
3075 end if;
3077 return;
3078 end if;
3079 end;
3081 -- Case where name is an explicit dereference X.all
3083 elsif Nkind (Nam) = N_Explicit_Dereference then
3085 -- Renamed entity is designated by access_to_subprogram expression.
3086 -- Must build body to encapsulate call, as in the entry case.
3088 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
3089 return;
3091 -- Indexed component
3093 elsif Nkind (Nam) = N_Indexed_Component then
3094 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
3095 return;
3097 -- Character literal
3099 elsif Nkind (Nam) = N_Character_Literal then
3100 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
3101 return;
3103 -- Only remaining case is where we have a non-entity name, or a renaming
3104 -- of some other non-overloadable entity.
3106 elsif not Is_Entity_Name (Nam)
3107 or else not Is_Overloadable (Entity (Nam))
3108 then
3109 -- Do not mention the renaming if it comes from an instance
3111 if not Is_Actual then
3112 Error_Msg_N ("expect valid subprogram name in renaming", N);
3113 else
3114 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
3115 end if;
3117 return;
3118 end if;
3120 -- Find the renamed entity that matches the given specification. Disable
3121 -- Ada_83 because there is no requirement of full conformance between
3122 -- renamed entity and new entity, even though the same circuit is used.
3124 -- This is a bit of an odd case, which introduces a really irregular use
3125 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
3126 -- this. ???
3128 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
3129 Ada_Version_Pragma := Empty;
3130 Ada_Version_Explicit := Ada_Version;
3132 if No (Old_S) then
3133 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3135 -- The visible operation may be an inherited abstract operation that
3136 -- was overridden in the private part, in which case a call will
3137 -- dispatch to the overriding operation. Use the overriding one in
3138 -- the renaming declaration, to prevent spurious errors below.
3140 if Is_Overloadable (Old_S)
3141 and then Is_Abstract_Subprogram (Old_S)
3142 and then No (DTC_Entity (Old_S))
3143 and then Present (Alias (Old_S))
3144 and then not Is_Abstract_Subprogram (Alias (Old_S))
3145 and then Present (Overridden_Operation (Alias (Old_S)))
3146 then
3147 Old_S := Alias (Old_S);
3148 end if;
3150 -- When the renamed subprogram is overloaded and used as an actual
3151 -- of a generic, its entity is set to the first available homonym.
3152 -- We must first disambiguate the name, then set the proper entity.
3154 if Is_Actual and then Is_Overloaded (Nam) then
3155 Set_Entity (Nam, Old_S);
3156 end if;
3157 end if;
3159 -- Most common case: subprogram renames subprogram. No body is generated
3160 -- in this case, so we must indicate the declaration is complete as is.
3161 -- and inherit various attributes of the renamed subprogram.
3163 if No (Rename_Spec) then
3164 Set_Has_Completion (New_S);
3165 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3166 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3167 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3169 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3170 -- between a subprogram and its correct renaming.
3172 -- Note: the Any_Id check is a guard that prevents compiler crashes
3173 -- when performing a null exclusion check between a renaming and a
3174 -- renamed subprogram that has been found to be illegal.
3176 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3177 Check_Null_Exclusion
3178 (Ren => New_S,
3179 Sub => Entity (Nam));
3180 end if;
3182 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3183 -- overriding. The flag Requires_Overriding is set very selectively
3184 -- and misses some other illegal cases. The additional conditions
3185 -- checked below are sufficient but not necessary ???
3187 -- The rule does not apply to the renaming generated for an actual
3188 -- subprogram in an instance.
3190 if Is_Actual then
3191 null;
3193 -- Guard against previous errors, and omit renamings of predefined
3194 -- operators.
3196 elsif not Ekind_In (Old_S, E_Function, E_Procedure) then
3197 null;
3199 elsif Requires_Overriding (Old_S)
3200 or else
3201 (Is_Abstract_Subprogram (Old_S)
3202 and then Present (Find_Dispatching_Type (Old_S))
3203 and then
3204 not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3205 then
3206 Error_Msg_N
3207 ("renamed entity cannot be subprogram that requires overriding "
3208 & "(RM 8.5.4 (5.1))", N);
3209 end if;
3211 declare
3212 Prev : constant Entity_Id := Overridden_Operation (New_S);
3213 begin
3214 if Present (Prev)
3215 and then
3216 (Has_Non_Trivial_Precondition (Prev)
3217 or else Has_Non_Trivial_Precondition (Old_S))
3218 then
3219 Error_Msg_NE
3220 ("conflicting inherited classwide preconditions in renaming "
3221 & "of& (RM 6.1.1 (17)", N, Old_S);
3222 end if;
3223 end;
3224 end if;
3226 if Old_S /= Any_Id then
3227 if Is_Actual and then From_Default (N) then
3229 -- This is an implicit reference to the default actual
3231 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3233 else
3234 Generate_Reference (Old_S, Nam);
3235 end if;
3237 Check_Internal_Protected_Use (N, Old_S);
3239 -- For a renaming-as-body, require subtype conformance, but if the
3240 -- declaration being completed has not been frozen, then inherit the
3241 -- convention of the renamed subprogram prior to checking conformance
3242 -- (unless the renaming has an explicit convention established; the
3243 -- rule stated in the RM doesn't seem to address this ???).
3245 if Present (Rename_Spec) then
3246 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3247 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3249 if not Is_Frozen (Rename_Spec) then
3250 if not Has_Convention_Pragma (Rename_Spec) then
3251 Set_Convention (New_S, Convention (Old_S));
3252 end if;
3254 if Ekind (Old_S) /= E_Operator then
3255 Check_Mode_Conformant (New_S, Old_S, Spec);
3256 end if;
3258 if Original_Subprogram (Old_S) = Rename_Spec then
3259 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
3260 end if;
3261 else
3262 Check_Subtype_Conformant (New_S, Old_S, Spec);
3263 end if;
3265 Check_Frozen_Renaming (N, Rename_Spec);
3267 -- Check explicitly that renamed entity is not intrinsic, because
3268 -- in a generic the renamed body is not built. In this case,
3269 -- the renaming_as_body is a completion.
3271 if Inside_A_Generic then
3272 if Is_Frozen (Rename_Spec)
3273 and then Is_Intrinsic_Subprogram (Old_S)
3274 then
3275 Error_Msg_N
3276 ("subprogram in renaming_as_body cannot be intrinsic",
3277 Name (N));
3278 end if;
3280 Set_Has_Completion (Rename_Spec);
3281 end if;
3283 elsif Ekind (Old_S) /= E_Operator then
3285 -- If this a defaulted subprogram for a class-wide actual there is
3286 -- no check for mode conformance, given that the signatures don't
3287 -- match (the source mentions T but the actual mentions T'Class).
3289 if CW_Actual then
3290 null;
3291 elsif not Is_Actual or else No (Enclosing_Instance) then
3292 Check_Mode_Conformant (New_S, Old_S);
3293 end if;
3295 if Is_Actual and then Error_Posted (New_S) then
3296 Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
3297 end if;
3298 end if;
3300 if No (Rename_Spec) then
3302 -- The parameter profile of the new entity is that of the renamed
3303 -- entity: the subtypes given in the specification are irrelevant.
3305 Inherit_Renamed_Profile (New_S, Old_S);
3307 -- A call to the subprogram is transformed into a call to the
3308 -- renamed entity. This is transitive if the renamed entity is
3309 -- itself a renaming.
3311 if Present (Alias (Old_S)) then
3312 Set_Alias (New_S, Alias (Old_S));
3313 else
3314 Set_Alias (New_S, Old_S);
3315 end if;
3317 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3318 -- renaming as body, since the entity in this case is not an
3319 -- intrinsic (it calls an intrinsic, but we have a real body for
3320 -- this call, and it is in this body that the required intrinsic
3321 -- processing will take place).
3323 -- Also, if this is a renaming of inequality, the renamed operator
3324 -- is intrinsic, but what matters is the corresponding equality
3325 -- operator, which may be user-defined.
3327 Set_Is_Intrinsic_Subprogram
3328 (New_S,
3329 Is_Intrinsic_Subprogram (Old_S)
3330 and then
3331 (Chars (Old_S) /= Name_Op_Ne
3332 or else Ekind (Old_S) = E_Operator
3333 or else Is_Intrinsic_Subprogram
3334 (Corresponding_Equality (Old_S))));
3336 if Ekind (Alias (New_S)) = E_Operator then
3337 Set_Has_Delayed_Freeze (New_S, False);
3338 end if;
3340 -- If the renaming corresponds to an association for an abstract
3341 -- formal subprogram, then various attributes must be set to
3342 -- indicate that the renaming is an abstract dispatching operation
3343 -- with a controlling type.
3345 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
3347 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3348 -- see it as corresponding to a generic association for a
3349 -- formal abstract subprogram
3351 Set_Is_Abstract_Subprogram (New_S);
3353 declare
3354 New_S_Ctrl_Type : constant Entity_Id :=
3355 Find_Dispatching_Type (New_S);
3356 Old_S_Ctrl_Type : constant Entity_Id :=
3357 Find_Dispatching_Type (Old_S);
3359 begin
3361 -- The actual must match the (instance of the) formal,
3362 -- and must be a controlling type.
3364 if Old_S_Ctrl_Type /= New_S_Ctrl_Type
3365 or else No (New_S_Ctrl_Type)
3366 then
3367 Error_Msg_NE
3368 ("actual must be dispatching subprogram for type&",
3369 Nam, New_S_Ctrl_Type);
3371 else
3372 Set_Is_Dispatching_Operation (New_S);
3373 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3375 -- If the actual in the formal subprogram is itself a
3376 -- formal abstract subprogram association, there's no
3377 -- dispatch table component or position to inherit.
3379 if Present (DTC_Entity (Old_S)) then
3380 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3381 Set_DT_Position_Value (New_S, DT_Position (Old_S));
3382 end if;
3383 end if;
3384 end;
3385 end if;
3386 end if;
3388 if Is_Actual then
3389 null;
3391 -- The following is illegal, because F hides whatever other F may
3392 -- be around:
3393 -- function F (...) renames F;
3395 elsif Old_S = New_S
3396 or else (Nkind (Nam) /= N_Expanded_Name
3397 and then Chars (Old_S) = Chars (New_S))
3398 then
3399 Error_Msg_N ("subprogram cannot rename itself", N);
3401 -- This is illegal even if we use a selector:
3402 -- function F (...) renames Pkg.F;
3403 -- because F is still hidden.
3405 elsif Nkind (Nam) = N_Expanded_Name
3406 and then Entity (Prefix (Nam)) = Current_Scope
3407 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3408 then
3409 -- This is an error, but we overlook the error and accept the
3410 -- renaming if the special Overriding_Renamings mode is in effect.
3412 if not Overriding_Renamings then
3413 Error_Msg_NE
3414 ("implicit operation& is not visible (RM 8.3 (15))",
3415 Nam, Old_S);
3416 end if;
3417 end if;
3419 Set_Convention (New_S, Convention (Old_S));
3421 if Is_Abstract_Subprogram (Old_S) then
3422 if Present (Rename_Spec) then
3423 Error_Msg_N
3424 ("a renaming-as-body cannot rename an abstract subprogram",
3426 Set_Has_Completion (Rename_Spec);
3427 else
3428 Set_Is_Abstract_Subprogram (New_S);
3429 end if;
3430 end if;
3432 Check_Library_Unit_Renaming (N, Old_S);
3434 -- Pathological case: procedure renames entry in the scope of its
3435 -- task. Entry is given by simple name, but body must be built for
3436 -- procedure. Of course if called it will deadlock.
3438 if Ekind (Old_S) = E_Entry then
3439 Set_Has_Completion (New_S, False);
3440 Set_Alias (New_S, Empty);
3441 end if;
3443 -- Do not freeze the renaming nor the renamed entity when the context
3444 -- is an enclosing generic. Freezing is an expansion activity, and in
3445 -- addition the renamed entity may depend on the generic formals of
3446 -- the enclosing generic.
3448 if Is_Actual and not Inside_A_Generic then
3449 Freeze_Before (N, Old_S);
3450 Freeze_Actual_Profile;
3451 Set_Has_Delayed_Freeze (New_S, False);
3452 Freeze_Before (N, New_S);
3454 -- An abstract subprogram is only allowed as an actual in the case
3455 -- where the formal subprogram is also abstract.
3457 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
3458 and then Is_Abstract_Subprogram (Old_S)
3459 and then not Is_Abstract_Subprogram (Formal_Spec)
3460 then
3461 Error_Msg_N
3462 ("abstract subprogram not allowed as generic actual", Nam);
3463 end if;
3464 end if;
3466 else
3467 -- A common error is to assume that implicit operators for types are
3468 -- defined in Standard, or in the scope of a subtype. In those cases
3469 -- where the renamed entity is given with an expanded name, it is
3470 -- worth mentioning that operators for the type are not declared in
3471 -- the scope given by the prefix.
3473 if Nkind (Nam) = N_Expanded_Name
3474 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
3475 and then Scope (Entity (Nam)) = Standard_Standard
3476 then
3477 declare
3478 T : constant Entity_Id :=
3479 Base_Type (Etype (First_Formal (New_S)));
3480 begin
3481 Error_Msg_Node_2 := Prefix (Nam);
3482 Error_Msg_NE
3483 ("operator for type& is not declared in&", Prefix (Nam), T);
3484 end;
3486 else
3487 Error_Msg_NE
3488 ("no visible subprogram matches the specification for&",
3489 Spec, New_S);
3490 end if;
3492 if Present (Candidate_Renaming) then
3493 declare
3494 F1 : Entity_Id;
3495 F2 : Entity_Id;
3496 T1 : Entity_Id;
3498 begin
3499 F1 := First_Formal (Candidate_Renaming);
3500 F2 := First_Formal (New_S);
3501 T1 := First_Subtype (Etype (F1));
3502 while Present (F1) and then Present (F2) loop
3503 Next_Formal (F1);
3504 Next_Formal (F2);
3505 end loop;
3507 if Present (F1) and then Present (Default_Value (F1)) then
3508 if Present (Next_Formal (F1)) then
3509 Error_Msg_NE
3510 ("\missing specification for & and other formals with "
3511 & "defaults", Spec, F1);
3512 else
3513 Error_Msg_NE ("\missing specification for &", Spec, F1);
3514 end if;
3515 end if;
3517 if Nkind (Nam) = N_Operator_Symbol
3518 and then From_Default (N)
3519 then
3520 Error_Msg_Node_2 := T1;
3521 Error_Msg_NE
3522 ("default & on & is not directly visible", Nam, Nam);
3523 end if;
3524 end;
3525 end if;
3526 end if;
3528 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
3529 -- controlling access parameters are known non-null for the renamed
3530 -- subprogram. Test also applies to a subprogram instantiation that
3531 -- is dispatching. Test is skipped if some previous error was detected
3532 -- that set Old_S to Any_Id.
3534 if Ada_Version >= Ada_2005
3535 and then Old_S /= Any_Id
3536 and then not Is_Dispatching_Operation (Old_S)
3537 and then Is_Dispatching_Operation (New_S)
3538 then
3539 declare
3540 Old_F : Entity_Id;
3541 New_F : Entity_Id;
3543 begin
3544 Old_F := First_Formal (Old_S);
3545 New_F := First_Formal (New_S);
3546 while Present (Old_F) loop
3547 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
3548 and then Is_Controlling_Formal (New_F)
3549 and then not Can_Never_Be_Null (Old_F)
3550 then
3551 Error_Msg_N ("access parameter is controlling,", New_F);
3552 Error_Msg_NE
3553 ("\corresponding parameter of& must be explicitly null "
3554 & "excluding", New_F, Old_S);
3555 end if;
3557 Next_Formal (Old_F);
3558 Next_Formal (New_F);
3559 end loop;
3560 end;
3561 end if;
3563 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
3564 -- is to warn if an operator is being renamed as a different operator.
3565 -- If the operator is predefined, examine the kind of the entity, not
3566 -- the abbreviated declaration in Standard.
3568 if Comes_From_Source (N)
3569 and then Present (Old_S)
3570 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3571 or else Ekind (Old_S) = E_Operator)
3572 and then Nkind (New_S) = N_Defining_Operator_Symbol
3573 and then Chars (Old_S) /= Chars (New_S)
3574 then
3575 Error_Msg_NE
3576 ("& is being renamed as a different operator??", N, Old_S);
3577 end if;
3579 -- Check for renaming of obsolescent subprogram
3581 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3583 -- Another warning or some utility: if the new subprogram as the same
3584 -- name as the old one, the old one is not hidden by an outer homograph,
3585 -- the new one is not a public symbol, and the old one is otherwise
3586 -- directly visible, the renaming is superfluous.
3588 if Chars (Old_S) = Chars (New_S)
3589 and then Comes_From_Source (N)
3590 and then Scope (Old_S) /= Standard_Standard
3591 and then Warn_On_Redundant_Constructs
3592 and then (Is_Immediately_Visible (Old_S)
3593 or else Is_Potentially_Use_Visible (Old_S))
3594 and then Is_Overloadable (Current_Scope)
3595 and then Chars (Current_Scope) /= Chars (Old_S)
3596 then
3597 Error_Msg_N
3598 ("redundant renaming, entity is directly visible?r?", Name (N));
3599 end if;
3601 -- Implementation-defined aspect specifications can appear in a renaming
3602 -- declaration, but not language-defined ones. The call to procedure
3603 -- Analyze_Aspect_Specifications will take care of this error check.
3605 if Has_Aspects (N) then
3606 Analyze_Aspect_Specifications (N, New_S);
3607 end if;
3609 Ada_Version := Save_AV;
3610 Ada_Version_Pragma := Save_AVP;
3611 Ada_Version_Explicit := Save_AV_Exp;
3613 -- In GNATprove mode, the renamings of actual subprograms are replaced
3614 -- with wrapper functions that make it easier to propagate axioms to the
3615 -- points of call within an instance. Wrappers are generated if formal
3616 -- subprogram is subject to axiomatization.
3618 -- The types in the wrapper profiles are obtained from (instances of)
3619 -- the types of the formal subprogram.
3621 if Is_Actual
3622 and then GNATprove_Mode
3623 and then Present (Containing_Package_With_Ext_Axioms (Formal_Spec))
3624 and then not Inside_A_Generic
3625 then
3626 if Ekind (Old_S) = E_Function then
3627 Rewrite (N, Build_Function_Wrapper (Formal_Spec, Old_S));
3628 Analyze (N);
3630 elsif Ekind (Old_S) = E_Operator then
3631 Rewrite (N, Build_Operator_Wrapper (Formal_Spec, Old_S));
3632 Analyze (N);
3633 end if;
3634 end if;
3636 -- Check if we are looking at an Ada 2012 defaulted formal subprogram
3637 -- and mark any use_package_clauses that affect the visibility of the
3638 -- implicit generic actual.
3640 if From_Default (N)
3641 and then Is_Generic_Actual_Subprogram (New_S)
3642 and then Present (Alias (New_S))
3643 then
3644 Mark_Use_Clauses (Alias (New_S));
3646 -- Check intrinsic operators used as generic actuals since they may
3647 -- make a use_type_clause effective.
3649 elsif Is_Generic_Actual_Subprogram (New_S)
3650 and then Is_Intrinsic_Subprogram (New_S)
3651 then
3652 Mark_Use_Clauses (New_S);
3653 end if;
3654 end Analyze_Subprogram_Renaming;
3656 -------------------------
3657 -- Analyze_Use_Package --
3658 -------------------------
3660 -- Resolve the package names in the use clause, and make all the visible
3661 -- entities defined in the package potentially use-visible. If the package
3662 -- is already in use from a previous use clause, its visible entities are
3663 -- already use-visible. In that case, mark the occurrence as a redundant
3664 -- use. If the package is an open scope, i.e. if the use clause occurs
3665 -- within the package itself, ignore it.
3667 procedure Analyze_Use_Package (N : Node_Id; Chain : Boolean := True) is
3669 procedure Analyze_Package_Name (Clause : Node_Id);
3670 -- Perform analysis on a package name from a use_package_clause
3672 procedure Analyze_Package_Name_List (Head_Clause : Node_Id);
3673 -- Similar to Analyze_Package_Name but iterates over all the names
3674 -- in a use clause.
3676 --------------------------
3677 -- Analyze_Package_Name --
3678 --------------------------
3680 procedure Analyze_Package_Name (Clause : Node_Id) is
3681 Pack : constant Node_Id := Name (Clause);
3682 Pref : Node_Id;
3684 begin
3685 pragma Assert (Nkind (Clause) = N_Use_Package_Clause);
3686 Analyze (Pack);
3688 -- Verify that the package standard is not directly named in a
3689 -- use_package_clause.
3691 if Nkind (Parent (Clause)) = N_Compilation_Unit
3692 and then Nkind (Pack) = N_Expanded_Name
3693 then
3694 Pref := Prefix (Pack);
3696 while Nkind (Pref) = N_Expanded_Name loop
3697 Pref := Prefix (Pref);
3698 end loop;
3700 if Entity (Pref) = Standard_Standard then
3701 Error_Msg_N
3702 ("predefined package Standard cannot appear in a "
3703 & "context clause", Pref);
3704 end if;
3705 end if;
3706 end Analyze_Package_Name;
3708 -------------------------------
3709 -- Analyze_Package_Name_List --
3710 -------------------------------
3712 procedure Analyze_Package_Name_List (Head_Clause : Node_Id) is
3713 Curr : Node_Id;
3715 begin
3716 -- Due to the way source use clauses are split during parsing we are
3717 -- forced to simply iterate through all entities in scope until the
3718 -- clause representing the last name in the list is found.
3720 Curr := Head_Clause;
3721 while Present (Curr) loop
3722 Analyze_Package_Name (Curr);
3724 -- Stop iterating over the names in the use clause when we are at
3725 -- the last one.
3727 exit when not More_Ids (Curr) and then Prev_Ids (Curr);
3728 Next (Curr);
3729 end loop;
3730 end Analyze_Package_Name_List;
3732 -- Local variables
3734 Ghost_Id : Entity_Id := Empty;
3735 Living_Id : Entity_Id := Empty;
3736 Pack : Entity_Id;
3738 -- Start of processing for Analyze_Use_Package
3740 begin
3741 Check_SPARK_05_Restriction ("use clause is not allowed", N);
3743 Set_Hidden_By_Use_Clause (N, No_Elist);
3745 -- Use clause not allowed in a spec of a predefined package declaration
3746 -- except that packages whose file name starts a-n are OK (these are
3747 -- children of Ada.Numerics, which are never loaded by Rtsfind).
3749 if Is_Predefined_Unit (Current_Sem_Unit)
3750 and then Get_Name_String
3751 (Unit_File_Name (Current_Sem_Unit)) (1 .. 3) /= "a-n"
3752 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
3753 N_Package_Declaration
3754 then
3755 Error_Msg_N ("use clause not allowed in predefined spec", N);
3756 end if;
3758 -- Loop through all package names from the original use clause in
3759 -- order to analyze referenced packages. A use_package_clause with only
3760 -- one name does not have More_Ids or Prev_Ids set, while a clause with
3761 -- More_Ids only starts the chain produced by the parser.
3763 if not More_Ids (N) and then not Prev_Ids (N) then
3764 Analyze_Package_Name (N);
3765 elsif More_Ids (N) and then not Prev_Ids (N) then
3766 Analyze_Package_Name_List (N);
3767 end if;
3769 if not Is_Entity_Name (Name (N)) then
3770 Error_Msg_N ("& is not a package", Name (N));
3772 return;
3773 end if;
3774 Pack := Entity (Name (N));
3776 if Chain then
3777 Chain_Use_Clause (N);
3778 end if;
3780 -- There are many cases where scopes are manipulated during analysis, so
3781 -- check that Pack's current use clause has not already been chained
3782 -- before setting its previous use clause.
3784 if Ekind (Pack) = E_Package
3785 and then Present (Current_Use_Clause (Pack))
3786 and then Current_Use_Clause (Pack) /= N
3787 and then No (Prev_Use_Clause (N))
3788 and then Prev_Use_Clause (Current_Use_Clause (Pack)) /= N
3789 then
3790 Set_Prev_Use_Clause (N, Current_Use_Clause (Pack));
3791 end if;
3793 -- Mark all entities as potentially use visible.
3795 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
3796 if Ekind (Pack) = E_Generic_Package then
3797 Error_Msg_N -- CODEFIX
3798 ("a generic package is not allowed in a use clause",
3799 Name (N));
3801 elsif Ekind_In (Pack, E_Generic_Function, E_Generic_Package)
3802 then
3803 Error_Msg_N -- CODEFIX
3804 ("a generic subprogram is not allowed in a use clause",
3805 Name (N));
3807 elsif Ekind_In (Pack, E_Function, E_Procedure, E_Operator) then
3808 Error_Msg_N -- CODEFIX
3809 ("a subprogram is not allowed in a use clause",
3810 Name (N));
3812 else
3813 Error_Msg_N ("& is not allowed in a use clause", Name (N));
3814 end if;
3816 else
3817 if Nkind (Parent (N)) = N_Compilation_Unit then
3818 Check_In_Previous_With_Clause (N, Name (N));
3819 end if;
3821 Use_One_Package (N, Name (N));
3823 -- Capture the first Ghost package and the first living package
3825 if Is_Entity_Name (Name (N)) then
3826 Pack := Entity (Name (N));
3828 if Is_Ghost_Entity (Pack) then
3829 if No (Ghost_Id) then
3830 Ghost_Id := Pack;
3831 end if;
3833 elsif No (Living_Id) then
3834 Living_Id := Pack;
3835 end if;
3836 end if;
3837 end if;
3839 -- Detect a mixture of Ghost packages and living packages within the
3840 -- same use_package_clause. Ideally one would split a use_package_clause
3841 -- with multiple names into multiple use_package_clauses with a single
3842 -- name, however clients of the front end would have to adapt to this
3843 -- change.
3845 if Present (Ghost_Id) and then Present (Living_Id) then
3846 Error_Msg_N
3847 ("use clause cannot mention ghost and non-ghost ghost units", N);
3849 Error_Msg_Sloc := Sloc (Ghost_Id);
3850 Error_Msg_NE ("\& # declared as ghost", N, Ghost_Id);
3852 Error_Msg_Sloc := Sloc (Living_Id);
3853 Error_Msg_NE ("\& # declared as non-ghost", N, Living_Id);
3854 end if;
3856 Mark_Ghost_Clause (N);
3857 end Analyze_Use_Package;
3859 ----------------------
3860 -- Analyze_Use_Type --
3861 ----------------------
3863 procedure Analyze_Use_Type (N : Node_Id; Chain : Boolean := True) is
3864 E : Entity_Id;
3865 Id : Node_Id;
3867 begin
3868 Set_Hidden_By_Use_Clause (N, No_Elist);
3870 -- Chain clause to list of use clauses in current scope when flagged
3872 if Chain then
3873 Chain_Use_Clause (N);
3874 end if;
3876 -- Obtain the base type of the type denoted within the use_type_clause's
3877 -- subtype mark.
3879 Id := Subtype_Mark (N);
3880 Find_Type (Id);
3881 E := Base_Type (Entity (Id));
3883 -- There are many cases where a use_type_clause may be reanalyzed due to
3884 -- manipulation of the scope stack so we much guard against those cases
3885 -- here, otherwise, we must add the new use_type_clause to the previous
3886 -- use_type_clause chain in order to mark redundant use_type_clauses as
3887 -- used.
3889 if Present (Current_Use_Clause (E))
3890 and then Current_Use_Clause (E) /= N
3891 and then No (Prev_Use_Clause (N))
3892 then
3893 Set_Prev_Use_Clause (N, Current_Use_Clause (E));
3894 end if;
3896 -- If the Used_Operations list is already initialized, the clause has
3897 -- been analyzed previously, and it is being reinstalled, for example
3898 -- when the clause appears in a package spec and we are compiling the
3899 -- corresponding package body. In that case, make the entities on the
3900 -- existing list use_visible, and mark the corresponding types In_Use.
3902 if Present (Used_Operations (N)) then
3903 declare
3904 Elmt : Elmt_Id;
3906 begin
3907 Use_One_Type (Subtype_Mark (N), Installed => True);
3909 Elmt := First_Elmt (Used_Operations (N));
3910 while Present (Elmt) loop
3911 Set_Is_Potentially_Use_Visible (Node (Elmt));
3912 Next_Elmt (Elmt);
3913 end loop;
3914 end;
3916 return;
3917 end if;
3919 -- Otherwise, create new list and attach to it the operations that are
3920 -- made use-visible by the clause.
3922 Set_Used_Operations (N, New_Elmt_List);
3923 E := Entity (Id);
3925 if E /= Any_Type then
3926 Use_One_Type (Id);
3928 if Nkind (Parent (N)) = N_Compilation_Unit then
3929 if Nkind (Id) = N_Identifier then
3930 Error_Msg_N ("type is not directly visible", Id);
3932 elsif Is_Child_Unit (Scope (E))
3933 and then Scope (E) /= System_Aux_Id
3934 then
3935 Check_In_Previous_With_Clause (N, Prefix (Id));
3936 end if;
3937 end if;
3939 else
3940 -- If the use_type_clause appears in a compilation unit context,
3941 -- check whether it comes from a unit that may appear in a
3942 -- limited_with_clause, for a better error message.
3944 if Nkind (Parent (N)) = N_Compilation_Unit
3945 and then Nkind (Id) /= N_Identifier
3946 then
3947 declare
3948 Item : Node_Id;
3949 Pref : Node_Id;
3951 function Mentioned (Nam : Node_Id) return Boolean;
3952 -- Check whether the prefix of expanded name for the type
3953 -- appears in the prefix of some limited_with_clause.
3955 ---------------
3956 -- Mentioned --
3957 ---------------
3959 function Mentioned (Nam : Node_Id) return Boolean is
3960 begin
3961 return Nkind (Name (Item)) = N_Selected_Component
3962 and then Chars (Prefix (Name (Item))) = Chars (Nam);
3963 end Mentioned;
3965 begin
3966 Pref := Prefix (Id);
3967 Item := First (Context_Items (Parent (N)));
3968 while Present (Item) and then Item /= N loop
3969 if Nkind (Item) = N_With_Clause
3970 and then Limited_Present (Item)
3971 and then Mentioned (Pref)
3972 then
3973 Change_Error_Text
3974 (Get_Msg_Id, "premature usage of incomplete type");
3975 end if;
3977 Next (Item);
3978 end loop;
3979 end;
3980 end if;
3981 end if;
3983 Mark_Ghost_Clause (N);
3984 end Analyze_Use_Type;
3986 ------------------------
3987 -- Attribute_Renaming --
3988 ------------------------
3990 procedure Attribute_Renaming (N : Node_Id) is
3991 Loc : constant Source_Ptr := Sloc (N);
3992 Nam : constant Node_Id := Name (N);
3993 Spec : constant Node_Id := Specification (N);
3994 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
3995 Aname : constant Name_Id := Attribute_Name (Nam);
3997 Form_Num : Nat := 0;
3998 Expr_List : List_Id := No_List;
4000 Attr_Node : Node_Id;
4001 Body_Node : Node_Id;
4002 Param_Spec : Node_Id;
4004 begin
4005 Generate_Definition (New_S);
4007 -- This procedure is called in the context of subprogram renaming, and
4008 -- thus the attribute must be one that is a subprogram. All of those
4009 -- have at least one formal parameter, with the exceptions of the GNAT
4010 -- attribute 'Img, which GNAT treats as renameable.
4012 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
4013 if Aname /= Name_Img then
4014 Error_Msg_N
4015 ("subprogram renaming an attribute must have formals", N);
4016 return;
4017 end if;
4019 else
4020 Param_Spec := First (Parameter_Specifications (Spec));
4021 while Present (Param_Spec) loop
4022 Form_Num := Form_Num + 1;
4024 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4025 Find_Type (Parameter_Type (Param_Spec));
4027 -- The profile of the new entity denotes the base type (s) of
4028 -- the types given in the specification. For access parameters
4029 -- there are no subtypes involved.
4031 Rewrite (Parameter_Type (Param_Spec),
4032 New_Occurrence_Of
4033 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
4034 end if;
4036 if No (Expr_List) then
4037 Expr_List := New_List;
4038 end if;
4040 Append_To (Expr_List,
4041 Make_Identifier (Loc,
4042 Chars => Chars (Defining_Identifier (Param_Spec))));
4044 -- The expressions in the attribute reference are not freeze
4045 -- points. Neither is the attribute as a whole, see below.
4047 Set_Must_Not_Freeze (Last (Expr_List));
4048 Next (Param_Spec);
4049 end loop;
4050 end if;
4052 -- Immediate error if too many formals. Other mismatches in number or
4053 -- types of parameters are detected when we analyze the body of the
4054 -- subprogram that we construct.
4056 if Form_Num > 2 then
4057 Error_Msg_N ("too many formals for attribute", N);
4059 -- Error if the attribute reference has expressions that look like
4060 -- formal parameters.
4062 elsif Present (Expressions (Nam)) then
4063 Error_Msg_N ("illegal expressions in attribute reference", Nam);
4065 elsif
4066 Nam_In (Aname, Name_Compose, Name_Exponent, Name_Leading_Part,
4067 Name_Pos, Name_Round, Name_Scaling,
4068 Name_Val)
4069 then
4070 if Nkind (N) = N_Subprogram_Renaming_Declaration
4071 and then Present (Corresponding_Formal_Spec (N))
4072 then
4073 Error_Msg_N
4074 ("generic actual cannot be attribute involving universal type",
4075 Nam);
4076 else
4077 Error_Msg_N
4078 ("attribute involving a universal type cannot be renamed",
4079 Nam);
4080 end if;
4081 end if;
4083 -- Rewrite attribute node to have a list of expressions corresponding to
4084 -- the subprogram formals. A renaming declaration is not a freeze point,
4085 -- and the analysis of the attribute reference should not freeze the
4086 -- type of the prefix. We use the original node in the renaming so that
4087 -- its source location is preserved, and checks on stream attributes are
4088 -- properly applied.
4090 Attr_Node := Relocate_Node (Nam);
4091 Set_Expressions (Attr_Node, Expr_List);
4093 Set_Must_Not_Freeze (Attr_Node);
4094 Set_Must_Not_Freeze (Prefix (Nam));
4096 -- Case of renaming a function
4098 if Nkind (Spec) = N_Function_Specification then
4099 if Is_Procedure_Attribute_Name (Aname) then
4100 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
4101 return;
4102 end if;
4104 Find_Type (Result_Definition (Spec));
4105 Rewrite (Result_Definition (Spec),
4106 New_Occurrence_Of
4107 (Base_Type (Entity (Result_Definition (Spec))), Loc));
4109 Body_Node :=
4110 Make_Subprogram_Body (Loc,
4111 Specification => Spec,
4112 Declarations => New_List,
4113 Handled_Statement_Sequence =>
4114 Make_Handled_Sequence_Of_Statements (Loc,
4115 Statements => New_List (
4116 Make_Simple_Return_Statement (Loc,
4117 Expression => Attr_Node))));
4119 -- Case of renaming a procedure
4121 else
4122 if not Is_Procedure_Attribute_Name (Aname) then
4123 Error_Msg_N ("attribute can only be renamed as function", Nam);
4124 return;
4125 end if;
4127 Body_Node :=
4128 Make_Subprogram_Body (Loc,
4129 Specification => Spec,
4130 Declarations => New_List,
4131 Handled_Statement_Sequence =>
4132 Make_Handled_Sequence_Of_Statements (Loc,
4133 Statements => New_List (Attr_Node)));
4134 end if;
4136 -- In case of tagged types we add the body of the generated function to
4137 -- the freezing actions of the type (because in the general case such
4138 -- type is still not frozen). We exclude from this processing generic
4139 -- formal subprograms found in instantiations.
4141 -- We must exclude restricted run-time libraries because
4142 -- entity AST_Handler is defined in package System.Aux_Dec which is not
4143 -- available in those platforms. Note that we cannot use the function
4144 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
4145 -- the ZFP run-time library is not defined as a profile, and we do not
4146 -- want to deal with AST_Handler in ZFP mode.
4148 if not Configurable_Run_Time_Mode
4149 and then not Present (Corresponding_Formal_Spec (N))
4150 and then Etype (Nam) /= RTE (RE_AST_Handler)
4151 then
4152 declare
4153 P : constant Node_Id := Prefix (Nam);
4155 begin
4156 -- The prefix of 'Img is an object that is evaluated for each call
4157 -- of the function that renames it.
4159 if Aname = Name_Img then
4160 Preanalyze_And_Resolve (P);
4162 -- For all other attribute renamings, the prefix is a subtype
4164 else
4165 Find_Type (P);
4166 end if;
4168 -- If the target type is not yet frozen, add the body to the
4169 -- actions to be elaborated at freeze time.
4171 if Is_Tagged_Type (Etype (P))
4172 and then In_Open_Scopes (Scope (Etype (P)))
4173 then
4174 Ensure_Freeze_Node (Etype (P));
4175 Append_Freeze_Action (Etype (P), Body_Node);
4176 else
4177 Rewrite (N, Body_Node);
4178 Analyze (N);
4179 Set_Etype (New_S, Base_Type (Etype (New_S)));
4180 end if;
4181 end;
4183 -- Generic formal subprograms or AST_Handler renaming
4185 else
4186 Rewrite (N, Body_Node);
4187 Analyze (N);
4188 Set_Etype (New_S, Base_Type (Etype (New_S)));
4189 end if;
4191 if Is_Compilation_Unit (New_S) then
4192 Error_Msg_N
4193 ("a library unit can only rename another library unit", N);
4194 end if;
4196 -- We suppress elaboration warnings for the resulting entity, since
4197 -- clearly they are not needed, and more particularly, in the case
4198 -- of a generic formal subprogram, the resulting entity can appear
4199 -- after the instantiation itself, and thus look like a bogus case
4200 -- of access before elaboration.
4202 Set_Suppress_Elaboration_Warnings (New_S);
4204 end Attribute_Renaming;
4206 ----------------------
4207 -- Chain_Use_Clause --
4208 ----------------------
4210 procedure Chain_Use_Clause (N : Node_Id) is
4211 Pack : Entity_Id;
4212 Level : Int := Scope_Stack.Last;
4214 begin
4215 -- Common case
4217 if not Is_Compilation_Unit (Current_Scope)
4218 or else not Is_Child_Unit (Current_Scope)
4219 then
4220 null;
4222 -- Common case for compilation unit
4224 elsif Defining_Entity (N => Parent (N),
4225 Empty_On_Errors => True) = Current_Scope
4226 then
4227 null;
4229 else
4230 -- If declaration appears in some other scope, it must be in some
4231 -- parent unit when compiling a child.
4233 Pack := Defining_Entity (Parent (N), Empty_On_Errors => True);
4234 if not In_Open_Scopes (Pack) then
4235 null;
4237 -- If the use clause appears in an ancestor and we are in the
4238 -- private part of the immediate parent, the use clauses are
4239 -- already installed.
4241 elsif Pack /= Scope (Current_Scope)
4242 and then In_Private_Part (Scope (Current_Scope))
4243 then
4244 null;
4246 else
4247 -- Find entry for parent unit in scope stack
4249 while Scope_Stack.Table (Level).Entity /= Pack loop
4250 Level := Level - 1;
4251 end loop;
4252 end if;
4253 end if;
4255 Set_Next_Use_Clause (N,
4256 Scope_Stack.Table (Level).First_Use_Clause);
4257 Scope_Stack.Table (Level).First_Use_Clause := N;
4258 end Chain_Use_Clause;
4260 ---------------------------
4261 -- Check_Frozen_Renaming --
4262 ---------------------------
4264 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
4265 B_Node : Node_Id;
4266 Old_S : Entity_Id;
4268 begin
4269 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
4270 B_Node :=
4271 Build_Renamed_Body
4272 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
4274 if Is_Entity_Name (Name (N)) then
4275 Old_S := Entity (Name (N));
4277 if not Is_Frozen (Old_S)
4278 and then Operating_Mode /= Check_Semantics
4279 then
4280 Append_Freeze_Action (Old_S, B_Node);
4281 else
4282 Insert_After (N, B_Node);
4283 Analyze (B_Node);
4284 end if;
4286 if Is_Intrinsic_Subprogram (Old_S) and then not In_Instance then
4287 Error_Msg_N
4288 ("subprogram used in renaming_as_body cannot be intrinsic",
4289 Name (N));
4290 end if;
4292 else
4293 Insert_After (N, B_Node);
4294 Analyze (B_Node);
4295 end if;
4296 end if;
4297 end Check_Frozen_Renaming;
4299 -------------------------------
4300 -- Set_Entity_Or_Discriminal --
4301 -------------------------------
4303 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4304 P : Node_Id;
4306 begin
4307 -- If the entity is not a discriminant, or else expansion is disabled,
4308 -- simply set the entity.
4310 if not In_Spec_Expression
4311 or else Ekind (E) /= E_Discriminant
4312 or else Inside_A_Generic
4313 then
4314 Set_Entity_With_Checks (N, E);
4316 -- The replacement of a discriminant by the corresponding discriminal
4317 -- is not done for a task discriminant that appears in a default
4318 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4319 -- for details on their handling.
4321 elsif Is_Concurrent_Type (Scope (E)) then
4322 P := Parent (N);
4323 while Present (P)
4324 and then not Nkind_In (P, N_Parameter_Specification,
4325 N_Component_Declaration)
4326 loop
4327 P := Parent (P);
4328 end loop;
4330 if Present (P)
4331 and then Nkind (P) = N_Parameter_Specification
4332 then
4333 null;
4335 else
4336 Set_Entity (N, Discriminal (E));
4337 end if;
4339 -- Otherwise, this is a discriminant in a context in which
4340 -- it is a reference to the corresponding parameter of the
4341 -- init proc for the enclosing type.
4343 else
4344 Set_Entity (N, Discriminal (E));
4345 end if;
4346 end Set_Entity_Or_Discriminal;
4348 -----------------------------------
4349 -- Check_In_Previous_With_Clause --
4350 -----------------------------------
4352 procedure Check_In_Previous_With_Clause
4353 (N : Node_Id;
4354 Nam : Entity_Id)
4356 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4357 Item : Node_Id;
4358 Par : Node_Id;
4360 begin
4361 Item := First (Context_Items (Parent (N)));
4362 while Present (Item) and then Item /= N loop
4363 if Nkind (Item) = N_With_Clause
4365 -- Protect the frontend against previous critical errors
4367 and then Nkind (Name (Item)) /= N_Selected_Component
4368 and then Entity (Name (Item)) = Pack
4369 then
4370 Par := Nam;
4372 -- Find root library unit in with_clause
4374 while Nkind (Par) = N_Expanded_Name loop
4375 Par := Prefix (Par);
4376 end loop;
4378 if Is_Child_Unit (Entity (Original_Node (Par))) then
4379 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4380 else
4381 return;
4382 end if;
4383 end if;
4385 Next (Item);
4386 end loop;
4388 -- On exit, package is not mentioned in a previous with_clause.
4389 -- Check if its prefix is.
4391 if Nkind (Nam) = N_Expanded_Name then
4392 Check_In_Previous_With_Clause (N, Prefix (Nam));
4394 elsif Pack /= Any_Id then
4395 Error_Msg_NE ("& is not visible", Nam, Pack);
4396 end if;
4397 end Check_In_Previous_With_Clause;
4399 ---------------------------------
4400 -- Check_Library_Unit_Renaming --
4401 ---------------------------------
4403 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4404 New_E : Entity_Id;
4406 begin
4407 if Nkind (Parent (N)) /= N_Compilation_Unit then
4408 return;
4410 -- Check for library unit. Note that we used to check for the scope
4411 -- being Standard here, but that was wrong for Standard itself.
4413 elsif not Is_Compilation_Unit (Old_E)
4414 and then not Is_Child_Unit (Old_E)
4415 then
4416 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4418 -- Entities defined in Standard (operators and boolean literals) cannot
4419 -- be renamed as library units.
4421 elsif Scope (Old_E) = Standard_Standard
4422 and then Sloc (Old_E) = Standard_Location
4423 then
4424 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4426 elsif Present (Parent_Spec (N))
4427 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
4428 and then not Is_Child_Unit (Old_E)
4429 then
4430 Error_Msg_N
4431 ("renamed unit must be a child unit of generic parent", Name (N));
4433 elsif Nkind (N) in N_Generic_Renaming_Declaration
4434 and then Nkind (Name (N)) = N_Expanded_Name
4435 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
4436 and then Is_Generic_Unit (Old_E)
4437 then
4438 Error_Msg_N
4439 ("renamed generic unit must be a library unit", Name (N));
4441 elsif Is_Package_Or_Generic_Package (Old_E) then
4443 -- Inherit categorization flags
4445 New_E := Defining_Entity (N);
4446 Set_Is_Pure (New_E, Is_Pure (Old_E));
4447 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
4448 Set_Is_Remote_Call_Interface (New_E,
4449 Is_Remote_Call_Interface (Old_E));
4450 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
4451 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
4452 end if;
4453 end Check_Library_Unit_Renaming;
4455 ------------------------
4456 -- Enclosing_Instance --
4457 ------------------------
4459 function Enclosing_Instance return Entity_Id is
4460 S : Entity_Id;
4462 begin
4463 if not Is_Generic_Instance (Current_Scope) then
4464 return Empty;
4465 end if;
4467 S := Scope (Current_Scope);
4468 while S /= Standard_Standard loop
4469 if Is_Generic_Instance (S) then
4470 return S;
4471 end if;
4473 S := Scope (S);
4474 end loop;
4476 return Empty;
4477 end Enclosing_Instance;
4479 ---------------
4480 -- End_Scope --
4481 ---------------
4483 procedure End_Scope is
4484 Id : Entity_Id;
4485 Prev : Entity_Id;
4486 Outer : Entity_Id;
4488 begin
4489 Id := First_Entity (Current_Scope);
4490 while Present (Id) loop
4491 -- An entity in the current scope is not necessarily the first one
4492 -- on its homonym chain. Find its predecessor if any,
4493 -- If it is an internal entity, it will not be in the visibility
4494 -- chain altogether, and there is nothing to unchain.
4496 if Id /= Current_Entity (Id) then
4497 Prev := Current_Entity (Id);
4498 while Present (Prev)
4499 and then Present (Homonym (Prev))
4500 and then Homonym (Prev) /= Id
4501 loop
4502 Prev := Homonym (Prev);
4503 end loop;
4505 -- Skip to end of loop if Id is not in the visibility chain
4507 if No (Prev) or else Homonym (Prev) /= Id then
4508 goto Next_Ent;
4509 end if;
4511 else
4512 Prev := Empty;
4513 end if;
4515 Set_Is_Immediately_Visible (Id, False);
4517 Outer := Homonym (Id);
4518 while Present (Outer) and then Scope (Outer) = Current_Scope loop
4519 Outer := Homonym (Outer);
4520 end loop;
4522 -- Reset homonym link of other entities, but do not modify link
4523 -- between entities in current scope, so that the back-end can have
4524 -- a proper count of local overloadings.
4526 if No (Prev) then
4527 Set_Name_Entity_Id (Chars (Id), Outer);
4529 elsif Scope (Prev) /= Scope (Id) then
4530 Set_Homonym (Prev, Outer);
4531 end if;
4533 <<Next_Ent>>
4534 Next_Entity (Id);
4535 end loop;
4537 -- If the scope generated freeze actions, place them before the
4538 -- current declaration and analyze them. Type declarations and
4539 -- the bodies of initialization procedures can generate such nodes.
4540 -- We follow the parent chain until we reach a list node, which is
4541 -- the enclosing list of declarations. If the list appears within
4542 -- a protected definition, move freeze nodes outside the protected
4543 -- type altogether.
4545 if Present
4546 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
4547 then
4548 declare
4549 Decl : Node_Id;
4550 L : constant List_Id := Scope_Stack.Table
4551 (Scope_Stack.Last).Pending_Freeze_Actions;
4553 begin
4554 if Is_Itype (Current_Scope) then
4555 Decl := Associated_Node_For_Itype (Current_Scope);
4556 else
4557 Decl := Parent (Current_Scope);
4558 end if;
4560 Pop_Scope;
4562 while not (Is_List_Member (Decl))
4563 or else Nkind_In (Parent (Decl), N_Protected_Definition,
4564 N_Task_Definition)
4565 loop
4566 Decl := Parent (Decl);
4567 end loop;
4569 Insert_List_Before_And_Analyze (Decl, L);
4570 end;
4572 else
4573 Pop_Scope;
4574 end if;
4575 end End_Scope;
4577 ---------------------
4578 -- End_Use_Clauses --
4579 ---------------------
4581 procedure End_Use_Clauses (Clause : Node_Id) is
4582 U : Node_Id;
4584 begin
4585 -- Remove use_type_clauses first, because they affect the visibility of
4586 -- operators in subsequent used packages.
4588 U := Clause;
4589 while Present (U) loop
4590 if Nkind (U) = N_Use_Type_Clause then
4591 End_Use_Type (U);
4592 end if;
4594 Next_Use_Clause (U);
4595 end loop;
4597 U := Clause;
4598 while Present (U) loop
4599 if Nkind (U) = N_Use_Package_Clause then
4600 End_Use_Package (U);
4601 end if;
4603 Next_Use_Clause (U);
4604 end loop;
4605 end End_Use_Clauses;
4607 ---------------------
4608 -- End_Use_Package --
4609 ---------------------
4611 procedure End_Use_Package (N : Node_Id) is
4612 Pack : Entity_Id;
4613 Pack_Name : Node_Id;
4614 Id : Entity_Id;
4615 Elmt : Elmt_Id;
4617 function Is_Primitive_Operator_In_Use
4618 (Op : Entity_Id;
4619 F : Entity_Id) return Boolean;
4620 -- Check whether Op is a primitive operator of a use-visible type
4622 ----------------------------------
4623 -- Is_Primitive_Operator_In_Use --
4624 ----------------------------------
4626 function Is_Primitive_Operator_In_Use
4627 (Op : Entity_Id;
4628 F : Entity_Id) return Boolean
4630 T : constant Entity_Id := Base_Type (Etype (F));
4631 begin
4632 return In_Use (T) and then Scope (T) = Scope (Op);
4633 end Is_Primitive_Operator_In_Use;
4635 -- Start of processing for End_Use_Package
4637 begin
4638 Pack_Name := Name (N);
4640 -- Test that Pack_Name actually denotes a package before processing
4642 if Is_Entity_Name (Pack_Name)
4643 and then Ekind (Entity (Pack_Name)) = E_Package
4644 then
4645 Pack := Entity (Pack_Name);
4647 if In_Open_Scopes (Pack) then
4648 null;
4650 elsif not Redundant_Use (Pack_Name) then
4651 Set_In_Use (Pack, False);
4652 Set_Current_Use_Clause (Pack, Empty);
4654 Id := First_Entity (Pack);
4655 while Present (Id) loop
4657 -- Preserve use-visibility of operators that are primitive
4658 -- operators of a type that is use-visible through an active
4659 -- use_type_clause.
4661 if Nkind (Id) = N_Defining_Operator_Symbol
4662 and then
4663 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
4664 or else
4665 (Present (Next_Formal (First_Formal (Id)))
4666 and then
4667 Is_Primitive_Operator_In_Use
4668 (Id, Next_Formal (First_Formal (Id)))))
4669 then
4670 null;
4671 else
4672 Set_Is_Potentially_Use_Visible (Id, False);
4673 end if;
4675 if Is_Private_Type (Id)
4676 and then Present (Full_View (Id))
4677 then
4678 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4679 end if;
4681 Next_Entity (Id);
4682 end loop;
4684 if Present (Renamed_Object (Pack)) then
4685 Set_In_Use (Renamed_Object (Pack), False);
4686 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
4687 end if;
4689 if Chars (Pack) = Name_System
4690 and then Scope (Pack) = Standard_Standard
4691 and then Present_System_Aux
4692 then
4693 Id := First_Entity (System_Aux_Id);
4694 while Present (Id) loop
4695 Set_Is_Potentially_Use_Visible (Id, False);
4697 if Is_Private_Type (Id)
4698 and then Present (Full_View (Id))
4699 then
4700 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4701 end if;
4703 Next_Entity (Id);
4704 end loop;
4706 Set_In_Use (System_Aux_Id, False);
4707 end if;
4708 else
4709 Set_Redundant_Use (Pack_Name, False);
4710 end if;
4711 end if;
4713 if Present (Hidden_By_Use_Clause (N)) then
4714 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
4715 while Present (Elmt) loop
4716 declare
4717 E : constant Entity_Id := Node (Elmt);
4719 begin
4720 -- Reset either Use_Visibility or Direct_Visibility, depending
4721 -- on how the entity was hidden by the use clause.
4723 if In_Use (Scope (E))
4724 and then Used_As_Generic_Actual (Scope (E))
4725 then
4726 Set_Is_Potentially_Use_Visible (Node (Elmt));
4727 else
4728 Set_Is_Immediately_Visible (Node (Elmt));
4729 end if;
4731 Next_Elmt (Elmt);
4732 end;
4733 end loop;
4735 Set_Hidden_By_Use_Clause (N, No_Elist);
4736 end if;
4737 end End_Use_Package;
4739 ------------------
4740 -- End_Use_Type --
4741 ------------------
4743 procedure End_Use_Type (N : Node_Id) is
4744 Elmt : Elmt_Id;
4745 Id : Entity_Id;
4746 T : Entity_Id;
4748 -- Start of processing for End_Use_Type
4750 begin
4751 Id := Subtype_Mark (N);
4753 -- A call to Rtsfind may occur while analyzing a use_type_clause, in
4754 -- which case the type marks are not resolved yet, so guard against that
4755 -- here.
4757 if Is_Entity_Name (Id) and then Present (Entity (Id)) then
4758 T := Entity (Id);
4760 if T = Any_Type or else From_Limited_With (T) then
4761 null;
4763 -- Note that the use_type_clause may mention a subtype of the type
4764 -- whose primitive operations have been made visible. Here as
4765 -- elsewhere, it is the base type that matters for visibility.
4767 elsif In_Open_Scopes (Scope (Base_Type (T))) then
4768 null;
4770 elsif not Redundant_Use (Id) then
4771 Set_In_Use (T, False);
4772 Set_In_Use (Base_Type (T), False);
4773 Set_Current_Use_Clause (T, Empty);
4774 Set_Current_Use_Clause (Base_Type (T), Empty);
4775 end if;
4776 end if;
4778 if Is_Empty_Elmt_List (Used_Operations (N)) then
4779 return;
4781 else
4782 Elmt := First_Elmt (Used_Operations (N));
4783 while Present (Elmt) loop
4784 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
4785 Next_Elmt (Elmt);
4786 end loop;
4787 end if;
4788 end End_Use_Type;
4790 --------------------
4791 -- Entity_Of_Unit --
4792 --------------------
4794 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
4795 begin
4796 if Nkind (U) = N_Package_Instantiation
4797 and then Analyzed (U)
4798 then
4799 return Defining_Entity (Instance_Spec (U));
4800 else
4801 return Defining_Entity (U);
4802 end if;
4803 end Entity_Of_Unit;
4805 ----------------------
4806 -- Find_Direct_Name --
4807 ----------------------
4809 procedure Find_Direct_Name (N : Node_Id) is
4810 E : Entity_Id;
4811 E2 : Entity_Id;
4812 Msg : Boolean;
4814 Homonyms : Entity_Id;
4815 -- Saves start of homonym chain
4817 Inst : Entity_Id := Empty;
4818 -- Enclosing instance, if any
4820 Nvis_Entity : Boolean;
4821 -- Set True to indicate that there is at least one entity on the homonym
4822 -- chain which, while not visible, is visible enough from the user point
4823 -- of view to warrant an error message of "not visible" rather than
4824 -- undefined.
4826 Nvis_Is_Private_Subprg : Boolean := False;
4827 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
4828 -- effect concerning library subprograms has been detected. Used to
4829 -- generate the precise error message.
4831 function From_Actual_Package (E : Entity_Id) return Boolean;
4832 -- Returns true if the entity is an actual for a package that is itself
4833 -- an actual for a formal package of the current instance. Such an
4834 -- entity requires special handling because it may be use-visible but
4835 -- hides directly visible entities defined outside the instance, because
4836 -- the corresponding formal did so in the generic.
4838 function Is_Actual_Parameter return Boolean;
4839 -- This function checks if the node N is an identifier that is an actual
4840 -- parameter of a procedure call. If so it returns True, otherwise it
4841 -- return False. The reason for this check is that at this stage we do
4842 -- not know what procedure is being called if the procedure might be
4843 -- overloaded, so it is premature to go setting referenced flags or
4844 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
4845 -- for that processing
4847 function Known_But_Invisible (E : Entity_Id) return Boolean;
4848 -- This function determines whether a reference to the entity E, which
4849 -- is not visible, can reasonably be considered to be known to the
4850 -- writer of the reference. This is a heuristic test, used only for
4851 -- the purposes of figuring out whether we prefer to complain that an
4852 -- entity is undefined or invisible (and identify the declaration of
4853 -- the invisible entity in the latter case). The point here is that we
4854 -- don't want to complain that something is invisible and then point to
4855 -- something entirely mysterious to the writer.
4857 procedure Nvis_Messages;
4858 -- Called if there are no visible entries for N, but there is at least
4859 -- one non-directly visible, or hidden declaration. This procedure
4860 -- outputs an appropriate set of error messages.
4862 procedure Undefined (Nvis : Boolean);
4863 -- This function is called if the current node has no corresponding
4864 -- visible entity or entities. The value set in Msg indicates whether
4865 -- an error message was generated (multiple error messages for the
4866 -- same variable are generally suppressed, see body for details).
4867 -- Msg is True if an error message was generated, False if not. This
4868 -- value is used by the caller to determine whether or not to output
4869 -- additional messages where appropriate. The parameter is set False
4870 -- to get the message "X is undefined", and True to get the message
4871 -- "X is not visible".
4873 -------------------------
4874 -- From_Actual_Package --
4875 -------------------------
4877 function From_Actual_Package (E : Entity_Id) return Boolean is
4878 Scop : constant Entity_Id := Scope (E);
4879 -- Declared scope of candidate entity
4881 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
4882 -- Recursive function that does the work and examines actuals of
4883 -- actual packages of current instance.
4885 ------------------------
4886 -- Declared_In_Actual --
4887 ------------------------
4889 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
4890 Act : Entity_Id;
4892 begin
4893 if No (Associated_Formal_Package (Pack)) then
4894 return False;
4896 else
4897 Act := First_Entity (Pack);
4898 while Present (Act) loop
4899 if Renamed_Object (Pack) = Scop then
4900 return True;
4902 -- Check for end of list of actuals
4904 elsif Ekind (Act) = E_Package
4905 and then Renamed_Object (Act) = Pack
4906 then
4907 return False;
4909 elsif Ekind (Act) = E_Package
4910 and then Declared_In_Actual (Act)
4911 then
4912 return True;
4913 end if;
4915 Next_Entity (Act);
4916 end loop;
4918 return False;
4919 end if;
4920 end Declared_In_Actual;
4922 -- Local variables
4924 Act : Entity_Id;
4926 -- Start of processing for From_Actual_Package
4928 begin
4929 if not In_Instance then
4930 return False;
4932 else
4933 Inst := Current_Scope;
4934 while Present (Inst)
4935 and then Ekind (Inst) /= E_Package
4936 and then not Is_Generic_Instance (Inst)
4937 loop
4938 Inst := Scope (Inst);
4939 end loop;
4941 if No (Inst) then
4942 return False;
4943 end if;
4945 Act := First_Entity (Inst);
4946 while Present (Act) loop
4947 if Ekind (Act) = E_Package
4948 and then Declared_In_Actual (Act)
4949 then
4950 return True;
4951 end if;
4953 Next_Entity (Act);
4954 end loop;
4956 return False;
4957 end if;
4958 end From_Actual_Package;
4960 -------------------------
4961 -- Is_Actual_Parameter --
4962 -------------------------
4964 function Is_Actual_Parameter return Boolean is
4965 begin
4966 return
4967 Nkind (N) = N_Identifier
4968 and then
4969 (Nkind (Parent (N)) = N_Procedure_Call_Statement
4970 or else
4971 (Nkind (Parent (N)) = N_Parameter_Association
4972 and then N = Explicit_Actual_Parameter (Parent (N))
4973 and then Nkind (Parent (Parent (N))) =
4974 N_Procedure_Call_Statement));
4975 end Is_Actual_Parameter;
4977 -------------------------
4978 -- Known_But_Invisible --
4979 -------------------------
4981 function Known_But_Invisible (E : Entity_Id) return Boolean is
4982 Fname : File_Name_Type;
4984 begin
4985 -- Entities in Standard are always considered to be known
4987 if Sloc (E) <= Standard_Location then
4988 return True;
4990 -- An entity that does not come from source is always considered
4991 -- to be unknown, since it is an artifact of code expansion.
4993 elsif not Comes_From_Source (E) then
4994 return False;
4996 -- In gnat internal mode, we consider all entities known. The
4997 -- historical reason behind this discrepancy is not known??? But the
4998 -- only effect is to modify the error message given, so it is not
4999 -- critical. Since it only affects the exact wording of error
5000 -- messages in illegal programs, we do not mention this as an
5001 -- effect of -gnatg, since it is not a language modification.
5003 elsif GNAT_Mode then
5004 return True;
5005 end if;
5007 -- Here we have an entity that is not from package Standard, and
5008 -- which comes from Source. See if it comes from an internal file.
5010 Fname := Unit_File_Name (Get_Source_Unit (E));
5012 -- Case of from internal file
5014 if In_Internal_Unit (E) then
5016 -- Private part entities in internal files are never considered
5017 -- to be known to the writer of normal application code.
5019 if Is_Hidden (E) then
5020 return False;
5021 end if;
5023 -- Entities from System packages other than System and
5024 -- System.Storage_Elements are not considered to be known.
5025 -- System.Auxxxx files are also considered known to the user.
5027 -- Should refine this at some point to generally distinguish
5028 -- between known and unknown internal files ???
5030 Get_Name_String (Fname);
5032 return
5033 Name_Len < 2
5034 or else
5035 Name_Buffer (1 .. 2) /= "s-"
5036 or else
5037 Name_Buffer (3 .. 8) = "stoele"
5038 or else
5039 Name_Buffer (3 .. 5) = "aux";
5041 -- If not an internal file, then entity is definitely known, even if
5042 -- it is in a private part (the message generated will note that it
5043 -- is in a private part).
5045 else
5046 return True;
5047 end if;
5048 end Known_But_Invisible;
5050 -------------------
5051 -- Nvis_Messages --
5052 -------------------
5054 procedure Nvis_Messages is
5055 Comp_Unit : Node_Id;
5056 Ent : Entity_Id;
5057 Found : Boolean := False;
5058 Hidden : Boolean := False;
5059 Item : Node_Id;
5061 begin
5062 -- Ada 2005 (AI-262): Generate a precise error concerning the
5063 -- Beaujolais effect that was previously detected
5065 if Nvis_Is_Private_Subprg then
5067 pragma Assert (Nkind (E2) = N_Defining_Identifier
5068 and then Ekind (E2) = E_Function
5069 and then Scope (E2) = Standard_Standard
5070 and then Has_Private_With (E2));
5072 -- Find the sloc corresponding to the private with'ed unit
5074 Comp_Unit := Cunit (Current_Sem_Unit);
5075 Error_Msg_Sloc := No_Location;
5077 Item := First (Context_Items (Comp_Unit));
5078 while Present (Item) loop
5079 if Nkind (Item) = N_With_Clause
5080 and then Private_Present (Item)
5081 and then Entity (Name (Item)) = E2
5082 then
5083 Error_Msg_Sloc := Sloc (Item);
5084 exit;
5085 end if;
5087 Next (Item);
5088 end loop;
5090 pragma Assert (Error_Msg_Sloc /= No_Location);
5092 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
5093 return;
5094 end if;
5096 Undefined (Nvis => True);
5098 if Msg then
5100 -- First loop does hidden declarations
5102 Ent := Homonyms;
5103 while Present (Ent) loop
5104 if Is_Potentially_Use_Visible (Ent) then
5105 if not Hidden then
5106 Error_Msg_N -- CODEFIX
5107 ("multiple use clauses cause hiding!", N);
5108 Hidden := True;
5109 end if;
5111 Error_Msg_Sloc := Sloc (Ent);
5112 Error_Msg_N -- CODEFIX
5113 ("hidden declaration#!", N);
5114 end if;
5116 Ent := Homonym (Ent);
5117 end loop;
5119 -- If we found hidden declarations, then that's enough, don't
5120 -- bother looking for non-visible declarations as well.
5122 if Hidden then
5123 return;
5124 end if;
5126 -- Second loop does non-directly visible declarations
5128 Ent := Homonyms;
5129 while Present (Ent) loop
5130 if not Is_Potentially_Use_Visible (Ent) then
5132 -- Do not bother the user with unknown entities
5134 if not Known_But_Invisible (Ent) then
5135 goto Continue;
5136 end if;
5138 Error_Msg_Sloc := Sloc (Ent);
5140 -- Output message noting that there is a non-visible
5141 -- declaration, distinguishing the private part case.
5143 if Is_Hidden (Ent) then
5144 Error_Msg_N ("non-visible (private) declaration#!", N);
5146 -- If the entity is declared in a generic package, it
5147 -- cannot be visible, so there is no point in adding it
5148 -- to the list of candidates if another homograph from a
5149 -- non-generic package has been seen.
5151 elsif Ekind (Scope (Ent)) = E_Generic_Package
5152 and then Found
5153 then
5154 null;
5156 else
5157 Error_Msg_N -- CODEFIX
5158 ("non-visible declaration#!", N);
5160 if Ekind (Scope (Ent)) /= E_Generic_Package then
5161 Found := True;
5162 end if;
5164 if Is_Compilation_Unit (Ent)
5165 and then
5166 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5167 then
5168 Error_Msg_Qual_Level := 99;
5169 Error_Msg_NE -- CODEFIX
5170 ("\\missing `WITH &;`", N, Ent);
5171 Error_Msg_Qual_Level := 0;
5172 end if;
5174 if Ekind (Ent) = E_Discriminant
5175 and then Present (Corresponding_Discriminant (Ent))
5176 and then Scope (Corresponding_Discriminant (Ent)) =
5177 Etype (Scope (Ent))
5178 then
5179 Error_Msg_N
5180 ("inherited discriminant not allowed here" &
5181 " (RM 3.8 (12), 3.8.1 (6))!", N);
5182 end if;
5183 end if;
5185 -- Set entity and its containing package as referenced. We
5186 -- can't be sure of this, but this seems a better choice
5187 -- to avoid unused entity messages.
5189 if Comes_From_Source (Ent) then
5190 Set_Referenced (Ent);
5191 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5192 end if;
5193 end if;
5195 <<Continue>>
5196 Ent := Homonym (Ent);
5197 end loop;
5198 end if;
5199 end Nvis_Messages;
5201 ---------------
5202 -- Undefined --
5203 ---------------
5205 procedure Undefined (Nvis : Boolean) is
5206 Emsg : Error_Msg_Id;
5208 begin
5209 -- We should never find an undefined internal name. If we do, then
5210 -- see if we have previous errors. If so, ignore on the grounds that
5211 -- it is probably a cascaded message (e.g. a block label from a badly
5212 -- formed block). If no previous errors, then we have a real internal
5213 -- error of some kind so raise an exception.
5215 if Is_Internal_Name (Chars (N)) then
5216 if Total_Errors_Detected /= 0 then
5217 return;
5218 else
5219 raise Program_Error;
5220 end if;
5221 end if;
5223 -- A very specialized error check, if the undefined variable is
5224 -- a case tag, and the case type is an enumeration type, check
5225 -- for a possible misspelling, and if so, modify the identifier
5227 -- Named aggregate should also be handled similarly ???
5229 if Nkind (N) = N_Identifier
5230 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5231 then
5232 declare
5233 Case_Stm : constant Node_Id := Parent (Parent (N));
5234 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5236 Lit : Node_Id;
5238 begin
5239 if Is_Enumeration_Type (Case_Typ)
5240 and then not Is_Standard_Character_Type (Case_Typ)
5241 then
5242 Lit := First_Literal (Case_Typ);
5243 Get_Name_String (Chars (Lit));
5245 if Chars (Lit) /= Chars (N)
5246 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5247 then
5248 Error_Msg_Node_2 := Lit;
5249 Error_Msg_N -- CODEFIX
5250 ("& is undefined, assume misspelling of &", N);
5251 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5252 return;
5253 end if;
5255 Lit := Next_Literal (Lit);
5256 end if;
5257 end;
5258 end if;
5260 -- Normal processing
5262 Set_Entity (N, Any_Id);
5263 Set_Etype (N, Any_Type);
5265 -- We use the table Urefs to keep track of entities for which we
5266 -- have issued errors for undefined references. Multiple errors
5267 -- for a single name are normally suppressed, however we modify
5268 -- the error message to alert the programmer to this effect.
5270 for J in Urefs.First .. Urefs.Last loop
5271 if Chars (N) = Chars (Urefs.Table (J).Node) then
5272 if Urefs.Table (J).Err /= No_Error_Msg
5273 and then Sloc (N) /= Urefs.Table (J).Loc
5274 then
5275 Error_Msg_Node_1 := Urefs.Table (J).Node;
5277 if Urefs.Table (J).Nvis then
5278 Change_Error_Text (Urefs.Table (J).Err,
5279 "& is not visible (more references follow)");
5280 else
5281 Change_Error_Text (Urefs.Table (J).Err,
5282 "& is undefined (more references follow)");
5283 end if;
5285 Urefs.Table (J).Err := No_Error_Msg;
5286 end if;
5288 -- Although we will set Msg False, and thus suppress the
5289 -- message, we also set Error_Posted True, to avoid any
5290 -- cascaded messages resulting from the undefined reference.
5292 Msg := False;
5293 Set_Error_Posted (N, True);
5294 return;
5295 end if;
5296 end loop;
5298 -- If entry not found, this is first undefined occurrence
5300 if Nvis then
5301 Error_Msg_N ("& is not visible!", N);
5302 Emsg := Get_Msg_Id;
5304 else
5305 Error_Msg_N ("& is undefined!", N);
5306 Emsg := Get_Msg_Id;
5308 -- A very bizarre special check, if the undefined identifier
5309 -- is put or put_line, then add a special error message (since
5310 -- this is a very common error for beginners to make).
5312 if Nam_In (Chars (N), Name_Put, Name_Put_Line) then
5313 Error_Msg_N -- CODEFIX
5314 ("\\possible missing `WITH Ada.Text_'I'O; " &
5315 "USE Ada.Text_'I'O`!", N);
5317 -- Another special check if N is the prefix of a selected
5318 -- component which is a known unit, add message complaining
5319 -- about missing with for this unit.
5321 elsif Nkind (Parent (N)) = N_Selected_Component
5322 and then N = Prefix (Parent (N))
5323 and then Is_Known_Unit (Parent (N))
5324 then
5325 Error_Msg_Node_2 := Selector_Name (Parent (N));
5326 Error_Msg_N -- CODEFIX
5327 ("\\missing `WITH &.&;`", Prefix (Parent (N)));
5328 end if;
5330 -- Now check for possible misspellings
5332 declare
5333 E : Entity_Id;
5334 Ematch : Entity_Id := Empty;
5336 Last_Name_Id : constant Name_Id :=
5337 Name_Id (Nat (First_Name_Id) +
5338 Name_Entries_Count - 1);
5340 begin
5341 for Nam in First_Name_Id .. Last_Name_Id loop
5342 E := Get_Name_Entity_Id (Nam);
5344 if Present (E)
5345 and then (Is_Immediately_Visible (E)
5346 or else
5347 Is_Potentially_Use_Visible (E))
5348 then
5349 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5350 Ematch := E;
5351 exit;
5352 end if;
5353 end if;
5354 end loop;
5356 if Present (Ematch) then
5357 Error_Msg_NE -- CODEFIX
5358 ("\possible misspelling of&", N, Ematch);
5359 end if;
5360 end;
5361 end if;
5363 -- Make entry in undefined references table unless the full errors
5364 -- switch is set, in which case by refraining from generating the
5365 -- table entry, we guarantee that we get an error message for every
5366 -- undefined reference. The entry is not added if we are ignoring
5367 -- errors.
5369 if not All_Errors_Mode and then Ignore_Errors_Enable = 0 then
5370 Urefs.Append (
5371 (Node => N,
5372 Err => Emsg,
5373 Nvis => Nvis,
5374 Loc => Sloc (N)));
5375 end if;
5377 Msg := True;
5378 end Undefined;
5380 -- Local variables
5382 Nested_Inst : Entity_Id := Empty;
5383 -- The entity of a nested instance which appears within Inst (if any)
5385 -- Start of processing for Find_Direct_Name
5387 begin
5388 -- If the entity pointer is already set, this is an internal node, or
5389 -- a node that is analyzed more than once, after a tree modification.
5390 -- In such a case there is no resolution to perform, just set the type.
5392 if Present (Entity (N)) then
5393 if Is_Type (Entity (N)) then
5394 Set_Etype (N, Entity (N));
5396 else
5397 declare
5398 Entyp : constant Entity_Id := Etype (Entity (N));
5400 begin
5401 -- One special case here. If the Etype field is already set,
5402 -- and references the packed array type corresponding to the
5403 -- etype of the referenced entity, then leave it alone. This
5404 -- happens for trees generated from Exp_Pakd, where expressions
5405 -- can be deliberately "mis-typed" to the packed array type.
5407 if Is_Array_Type (Entyp)
5408 and then Is_Packed (Entyp)
5409 and then Present (Etype (N))
5410 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5411 then
5412 null;
5414 -- If not that special case, then just reset the Etype
5416 else
5417 Set_Etype (N, Etype (Entity (N)));
5418 end if;
5419 end;
5420 end if;
5422 -- Although the marking of use clauses happens at the end of
5423 -- Find_Direct_Name, a certain case where a generic actual satisfies
5424 -- a use clause must be checked here due to how the generic machinery
5425 -- handles the analysis of said actuals.
5427 if In_Instance
5428 and then Nkind (Parent (N)) = N_Generic_Association
5429 then
5430 Mark_Use_Clauses (Entity (N));
5431 end if;
5433 return;
5434 end if;
5436 -- Here if Entity pointer was not set, we need full visibility analysis
5437 -- First we generate debugging output if the debug E flag is set.
5439 if Debug_Flag_E then
5440 Write_Str ("Looking for ");
5441 Write_Name (Chars (N));
5442 Write_Eol;
5443 end if;
5445 Homonyms := Current_Entity (N);
5446 Nvis_Entity := False;
5448 E := Homonyms;
5449 while Present (E) loop
5451 -- If entity is immediately visible or potentially use visible, then
5452 -- process the entity and we are done.
5454 if Is_Immediately_Visible (E) then
5455 goto Immediately_Visible_Entity;
5457 elsif Is_Potentially_Use_Visible (E) then
5458 goto Potentially_Use_Visible_Entity;
5460 -- Note if a known but invisible entity encountered
5462 elsif Known_But_Invisible (E) then
5463 Nvis_Entity := True;
5464 end if;
5466 -- Move to next entity in chain and continue search
5468 E := Homonym (E);
5469 end loop;
5471 -- If no entries on homonym chain that were potentially visible,
5472 -- and no entities reasonably considered as non-visible, then
5473 -- we have a plain undefined reference, with no additional
5474 -- explanation required.
5476 if not Nvis_Entity then
5477 Undefined (Nvis => False);
5479 -- Otherwise there is at least one entry on the homonym chain that
5480 -- is reasonably considered as being known and non-visible.
5482 else
5483 Nvis_Messages;
5484 end if;
5486 goto Done;
5488 -- Processing for a potentially use visible entry found. We must search
5489 -- the rest of the homonym chain for two reasons. First, if there is a
5490 -- directly visible entry, then none of the potentially use-visible
5491 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5492 -- for the case of multiple potentially use-visible entries hiding one
5493 -- another and as a result being non-directly visible (RM 8.4(11)).
5495 <<Potentially_Use_Visible_Entity>> declare
5496 Only_One_Visible : Boolean := True;
5497 All_Overloadable : Boolean := Is_Overloadable (E);
5499 begin
5500 E2 := Homonym (E);
5501 while Present (E2) loop
5502 if Is_Immediately_Visible (E2) then
5504 -- If the use-visible entity comes from the actual for a
5505 -- formal package, it hides a directly visible entity from
5506 -- outside the instance.
5508 if From_Actual_Package (E)
5509 and then Scope_Depth (E2) < Scope_Depth (Inst)
5510 then
5511 goto Found;
5512 else
5513 E := E2;
5514 goto Immediately_Visible_Entity;
5515 end if;
5517 elsif Is_Potentially_Use_Visible (E2) then
5518 Only_One_Visible := False;
5519 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5521 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5522 -- that can occur in private_with clauses. Example:
5524 -- with A;
5525 -- private with B; package A is
5526 -- package C is function B return Integer;
5527 -- use A; end A;
5528 -- V1 : Integer := B;
5529 -- private function B return Integer;
5530 -- V2 : Integer := B;
5531 -- end C;
5533 -- V1 resolves to A.B, but V2 resolves to library unit B
5535 elsif Ekind (E2) = E_Function
5536 and then Scope (E2) = Standard_Standard
5537 and then Has_Private_With (E2)
5538 then
5539 Only_One_Visible := False;
5540 All_Overloadable := False;
5541 Nvis_Is_Private_Subprg := True;
5542 exit;
5543 end if;
5545 E2 := Homonym (E2);
5546 end loop;
5548 -- On falling through this loop, we have checked that there are no
5549 -- immediately visible entities. Only_One_Visible is set if exactly
5550 -- one potentially use visible entity exists. All_Overloadable is
5551 -- set if all the potentially use visible entities are overloadable.
5552 -- The condition for legality is that either there is one potentially
5553 -- use visible entity, or if there is more than one, then all of them
5554 -- are overloadable.
5556 if Only_One_Visible or All_Overloadable then
5557 goto Found;
5559 -- If there is more than one potentially use-visible entity and at
5560 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5561 -- Note that E points to the first such entity on the homonym list.
5563 else
5564 -- If one of the entities is declared in an actual package, it
5565 -- was visible in the generic, and takes precedence over other
5566 -- entities that are potentially use-visible. The same applies
5567 -- if the entity is declared in a local instantiation of the
5568 -- current instance.
5570 if In_Instance then
5572 -- Find the current instance
5574 Inst := Current_Scope;
5575 while Present (Inst) and then Inst /= Standard_Standard loop
5576 if Is_Generic_Instance (Inst) then
5577 exit;
5578 end if;
5580 Inst := Scope (Inst);
5581 end loop;
5583 -- Reexamine the candidate entities, giving priority to those
5584 -- that were visible within the generic.
5586 E2 := E;
5587 while Present (E2) loop
5588 Nested_Inst := Nearest_Enclosing_Instance (E2);
5590 -- The entity is declared within an actual package, or in a
5591 -- nested instance. The ">=" accounts for the case where the
5592 -- current instance and the nested instance are the same.
5594 if From_Actual_Package (E2)
5595 or else (Present (Nested_Inst)
5596 and then Scope_Depth (Nested_Inst) >=
5597 Scope_Depth (Inst))
5598 then
5599 E := E2;
5600 goto Found;
5601 end if;
5603 E2 := Homonym (E2);
5604 end loop;
5606 Nvis_Messages;
5607 goto Done;
5609 elsif Is_Predefined_Unit (Current_Sem_Unit) then
5610 -- A use clause in the body of a system file creates conflict
5611 -- with some entity in a user scope, while rtsfind is active.
5612 -- Keep only the entity coming from another predefined unit.
5614 E2 := E;
5615 while Present (E2) loop
5616 if In_Predefined_Unit (E2) then
5617 E := E2;
5618 goto Found;
5619 end if;
5621 E2 := Homonym (E2);
5622 end loop;
5624 -- Entity must exist because predefined unit is correct
5626 raise Program_Error;
5628 else
5629 Nvis_Messages;
5630 goto Done;
5631 end if;
5632 end if;
5633 end;
5635 -- Come here with E set to the first immediately visible entity on
5636 -- the homonym chain. This is the one we want unless there is another
5637 -- immediately visible entity further on in the chain for an inner
5638 -- scope (RM 8.3(8)).
5640 <<Immediately_Visible_Entity>> declare
5641 Level : Int;
5642 Scop : Entity_Id;
5644 begin
5645 -- Find scope level of initial entity. When compiling through
5646 -- Rtsfind, the previous context is not completely invisible, and
5647 -- an outer entity may appear on the chain, whose scope is below
5648 -- the entry for Standard that delimits the current scope stack.
5649 -- Indicate that the level for this spurious entry is outside of
5650 -- the current scope stack.
5652 Level := Scope_Stack.Last;
5653 loop
5654 Scop := Scope_Stack.Table (Level).Entity;
5655 exit when Scop = Scope (E);
5656 Level := Level - 1;
5657 exit when Scop = Standard_Standard;
5658 end loop;
5660 -- Now search remainder of homonym chain for more inner entry
5661 -- If the entity is Standard itself, it has no scope, and we
5662 -- compare it with the stack entry directly.
5664 E2 := Homonym (E);
5665 while Present (E2) loop
5666 if Is_Immediately_Visible (E2) then
5668 -- If a generic package contains a local declaration that
5669 -- has the same name as the generic, there may be a visibility
5670 -- conflict in an instance, where the local declaration must
5671 -- also hide the name of the corresponding package renaming.
5672 -- We check explicitly for a package declared by a renaming,
5673 -- whose renamed entity is an instance that is on the scope
5674 -- stack, and that contains a homonym in the same scope. Once
5675 -- we have found it, we know that the package renaming is not
5676 -- immediately visible, and that the identifier denotes the
5677 -- other entity (and its homonyms if overloaded).
5679 if Scope (E) = Scope (E2)
5680 and then Ekind (E) = E_Package
5681 and then Present (Renamed_Object (E))
5682 and then Is_Generic_Instance (Renamed_Object (E))
5683 and then In_Open_Scopes (Renamed_Object (E))
5684 and then Comes_From_Source (N)
5685 then
5686 Set_Is_Immediately_Visible (E, False);
5687 E := E2;
5689 else
5690 for J in Level + 1 .. Scope_Stack.Last loop
5691 if Scope_Stack.Table (J).Entity = Scope (E2)
5692 or else Scope_Stack.Table (J).Entity = E2
5693 then
5694 Level := J;
5695 E := E2;
5696 exit;
5697 end if;
5698 end loop;
5699 end if;
5700 end if;
5702 E2 := Homonym (E2);
5703 end loop;
5705 -- At the end of that loop, E is the innermost immediately
5706 -- visible entity, so we are all set.
5707 end;
5709 -- Come here with entity found, and stored in E
5711 <<Found>> begin
5713 -- Check violation of No_Wide_Characters restriction
5715 Check_Wide_Character_Restriction (E, N);
5717 -- When distribution features are available (Get_PCS_Name /=
5718 -- Name_No_DSA), a remote access-to-subprogram type is converted
5719 -- into a record type holding whatever information is needed to
5720 -- perform a remote call on an RCI subprogram. In that case we
5721 -- rewrite any occurrence of the RAS type into the equivalent record
5722 -- type here. 'Access attribute references and RAS dereferences are
5723 -- then implemented using specific TSSs. However when distribution is
5724 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
5725 -- generation of these TSSs, and we must keep the RAS type in its
5726 -- original access-to-subprogram form (since all calls through a
5727 -- value of such type will be local anyway in the absence of a PCS).
5729 if Comes_From_Source (N)
5730 and then Is_Remote_Access_To_Subprogram_Type (E)
5731 and then Ekind (E) = E_Access_Subprogram_Type
5732 and then Expander_Active
5733 and then Get_PCS_Name /= Name_No_DSA
5734 then
5735 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
5736 goto Done;
5737 end if;
5739 -- Set the entity. Note that the reason we call Set_Entity for the
5740 -- overloadable case, as opposed to Set_Entity_With_Checks is
5741 -- that in the overloaded case, the initial call can set the wrong
5742 -- homonym. The call that sets the right homonym is in Sem_Res and
5743 -- that call does use Set_Entity_With_Checks, so we don't miss
5744 -- a style check.
5746 if Is_Overloadable (E) then
5747 Set_Entity (N, E);
5748 else
5749 Set_Entity_With_Checks (N, E);
5750 end if;
5752 if Is_Type (E) then
5753 Set_Etype (N, E);
5754 else
5755 Set_Etype (N, Get_Full_View (Etype (E)));
5756 end if;
5758 if Debug_Flag_E then
5759 Write_Str (" found ");
5760 Write_Entity_Info (E, " ");
5761 end if;
5763 -- If the Ekind of the entity is Void, it means that all homonyms
5764 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
5765 -- test is skipped if the current scope is a record and the name is
5766 -- a pragma argument expression (case of Atomic and Volatile pragmas
5767 -- and possibly other similar pragmas added later, which are allowed
5768 -- to reference components in the current record).
5770 if Ekind (E) = E_Void
5771 and then
5772 (not Is_Record_Type (Current_Scope)
5773 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
5774 then
5775 Premature_Usage (N);
5777 -- If the entity is overloadable, collect all interpretations of the
5778 -- name for subsequent overload resolution. We optimize a bit here to
5779 -- do this only if we have an overloadable entity that is not on its
5780 -- own on the homonym chain.
5782 elsif Is_Overloadable (E)
5783 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
5784 then
5785 Collect_Interps (N);
5787 -- If no homonyms were visible, the entity is unambiguous
5789 if not Is_Overloaded (N) then
5790 if not Is_Actual_Parameter then
5791 Generate_Reference (E, N);
5792 end if;
5793 end if;
5795 -- Case of non-overloadable entity, set the entity providing that
5796 -- we do not have the case of a discriminant reference within a
5797 -- default expression. Such references are replaced with the
5798 -- corresponding discriminal, which is the formal corresponding to
5799 -- to the discriminant in the initialization procedure.
5801 else
5802 -- Entity is unambiguous, indicate that it is referenced here
5804 -- For a renaming of an object, always generate simple reference,
5805 -- we don't try to keep track of assignments in this case, except
5806 -- in SPARK mode where renamings are traversed for generating
5807 -- local effects of subprograms.
5809 if Is_Object (E)
5810 and then Present (Renamed_Object (E))
5811 and then not GNATprove_Mode
5812 then
5813 Generate_Reference (E, N);
5815 -- If the renamed entity is a private protected component,
5816 -- reference the original component as well. This needs to be
5817 -- done because the private renamings are installed before any
5818 -- analysis has occurred. Reference to a private component will
5819 -- resolve to the renaming and the original component will be
5820 -- left unreferenced, hence the following.
5822 if Is_Prival (E) then
5823 Generate_Reference (Prival_Link (E), N);
5824 end if;
5826 -- One odd case is that we do not want to set the Referenced flag
5827 -- if the entity is a label, and the identifier is the label in
5828 -- the source, since this is not a reference from the point of
5829 -- view of the user.
5831 elsif Nkind (Parent (N)) = N_Label then
5832 declare
5833 R : constant Boolean := Referenced (E);
5835 begin
5836 -- Generate reference unless this is an actual parameter
5837 -- (see comment below)
5839 if Is_Actual_Parameter then
5840 Generate_Reference (E, N);
5841 Set_Referenced (E, R);
5842 end if;
5843 end;
5845 -- Normal case, not a label: generate reference
5847 else
5848 if not Is_Actual_Parameter then
5850 -- Package or generic package is always a simple reference
5852 if Ekind_In (E, E_Package, E_Generic_Package) then
5853 Generate_Reference (E, N, 'r');
5855 -- Else see if we have a left hand side
5857 else
5858 case Is_LHS (N) is
5859 when Yes =>
5860 Generate_Reference (E, N, 'm');
5862 when No =>
5863 Generate_Reference (E, N, 'r');
5865 -- If we don't know now, generate reference later
5867 when Unknown =>
5868 Deferred_References.Append ((E, N));
5869 end case;
5870 end if;
5871 end if;
5872 end if;
5874 Set_Entity_Or_Discriminal (N, E);
5876 -- The name may designate a generalized reference, in which case
5877 -- the dereference interpretation will be included. Context is
5878 -- one in which a name is legal.
5880 if Ada_Version >= Ada_2012
5881 and then
5882 (Nkind (Parent (N)) in N_Subexpr
5883 or else Nkind_In (Parent (N), N_Assignment_Statement,
5884 N_Object_Declaration,
5885 N_Parameter_Association))
5886 then
5887 Check_Implicit_Dereference (N, Etype (E));
5888 end if;
5889 end if;
5890 end;
5892 -- Mark relevant use-type and use-package clauses as effective if the
5893 -- node in question is not overloaded and therefore does not require
5894 -- resolution.
5896 -- Note: Generic actual subprograms do not follow the normal resolution
5897 -- path, so ignore the fact that they are overloaded and mark them
5898 -- anyway.
5900 if Nkind (N) not in N_Subexpr
5901 or else not Is_Overloaded (N)
5902 then
5903 Mark_Use_Clauses (N);
5904 end if;
5906 -- Come here with entity set
5908 <<Done>>
5909 Check_Restriction_No_Use_Of_Entity (N);
5910 end Find_Direct_Name;
5912 ------------------------
5913 -- Find_Expanded_Name --
5914 ------------------------
5916 -- This routine searches the homonym chain of the entity until it finds
5917 -- an entity declared in the scope denoted by the prefix. If the entity
5918 -- is private, it may nevertheless be immediately visible, if we are in
5919 -- the scope of its declaration.
5921 procedure Find_Expanded_Name (N : Node_Id) is
5922 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean;
5923 -- Determine whether expanded name Nod appears within a pragma which is
5924 -- a suitable context for an abstract view of a state or variable. The
5925 -- following pragmas fall in this category:
5926 -- Depends
5927 -- Global
5928 -- Initializes
5929 -- Refined_Depends
5930 -- Refined_Global
5932 -- In addition, pragma Abstract_State is also considered suitable even
5933 -- though it is an illegal context for an abstract view as this allows
5934 -- for proper resolution of abstract views of variables. This illegal
5935 -- context is later flagged in the analysis of indicator Part_Of.
5937 -----------------------------
5938 -- In_Abstract_View_Pragma --
5939 -----------------------------
5941 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is
5942 Par : Node_Id;
5944 begin
5945 -- Climb the parent chain looking for a pragma
5947 Par := Nod;
5948 while Present (Par) loop
5949 if Nkind (Par) = N_Pragma then
5950 if Nam_In (Pragma_Name_Unmapped (Par),
5951 Name_Abstract_State,
5952 Name_Depends,
5953 Name_Global,
5954 Name_Initializes,
5955 Name_Refined_Depends,
5956 Name_Refined_Global)
5957 then
5958 return True;
5960 -- Otherwise the pragma is not a legal context for an abstract
5961 -- view.
5963 else
5964 exit;
5965 end if;
5967 -- Prevent the search from going too far
5969 elsif Is_Body_Or_Package_Declaration (Par) then
5970 exit;
5971 end if;
5973 Par := Parent (Par);
5974 end loop;
5976 return False;
5977 end In_Abstract_View_Pragma;
5979 -- Local variables
5981 Selector : constant Node_Id := Selector_Name (N);
5982 Candidate : Entity_Id := Empty;
5983 P_Name : Entity_Id;
5984 Id : Entity_Id;
5986 -- Start of processing for Find_Expanded_Name
5988 begin
5989 P_Name := Entity (Prefix (N));
5991 -- If the prefix is a renamed package, look for the entity in the
5992 -- original package.
5994 if Ekind (P_Name) = E_Package
5995 and then Present (Renamed_Object (P_Name))
5996 then
5997 P_Name := Renamed_Object (P_Name);
5999 -- Rewrite node with entity field pointing to renamed object
6001 Rewrite (Prefix (N), New_Copy (Prefix (N)));
6002 Set_Entity (Prefix (N), P_Name);
6004 -- If the prefix is an object of a concurrent type, look for
6005 -- the entity in the associated task or protected type.
6007 elsif Is_Concurrent_Type (Etype (P_Name)) then
6008 P_Name := Etype (P_Name);
6009 end if;
6011 Id := Current_Entity (Selector);
6013 declare
6014 Is_New_Candidate : Boolean;
6016 begin
6017 while Present (Id) loop
6018 if Scope (Id) = P_Name then
6019 Candidate := Id;
6020 Is_New_Candidate := True;
6022 -- Handle abstract views of states and variables. These are
6023 -- acceptable candidates only when the reference to the view
6024 -- appears in certain pragmas.
6026 if Ekind (Id) = E_Abstract_State
6027 and then From_Limited_With (Id)
6028 and then Present (Non_Limited_View (Id))
6029 then
6030 if In_Abstract_View_Pragma (N) then
6031 Candidate := Non_Limited_View (Id);
6032 Is_New_Candidate := True;
6034 -- Hide the candidate because it is not used in a proper
6035 -- context.
6037 else
6038 Candidate := Empty;
6039 Is_New_Candidate := False;
6040 end if;
6041 end if;
6043 -- Ada 2005 (AI-217): Handle shadow entities associated with
6044 -- types declared in limited-withed nested packages. We don't need
6045 -- to handle E_Incomplete_Subtype entities because the entities
6046 -- in the limited view are always E_Incomplete_Type and
6047 -- E_Class_Wide_Type entities (see Build_Limited_Views).
6049 -- Regarding the expression used to evaluate the scope, it
6050 -- is important to note that the limited view also has shadow
6051 -- entities associated nested packages. For this reason the
6052 -- correct scope of the entity is the scope of the real entity.
6053 -- The non-limited view may itself be incomplete, in which case
6054 -- get the full view if available.
6056 elsif Ekind_In (Id, E_Incomplete_Type, E_Class_Wide_Type)
6057 and then From_Limited_With (Id)
6058 and then Present (Non_Limited_View (Id))
6059 and then Scope (Non_Limited_View (Id)) = P_Name
6060 then
6061 Candidate := Get_Full_View (Non_Limited_View (Id));
6062 Is_New_Candidate := True;
6064 -- An unusual case arises with a fully qualified name for an
6065 -- entity local to a generic child unit package, within an
6066 -- instantiation of that package. The name of the unit now
6067 -- denotes the renaming created within the instance. This is
6068 -- only relevant in an instance body, see below.
6070 elsif Is_Generic_Instance (Scope (Id))
6071 and then In_Open_Scopes (Scope (Id))
6072 and then In_Instance_Body
6073 and then Ekind (Scope (Id)) = E_Package
6074 and then Ekind (Id) = E_Package
6075 and then Renamed_Entity (Id) = Scope (Id)
6076 and then Is_Immediately_Visible (P_Name)
6077 then
6078 Is_New_Candidate := True;
6080 else
6081 Is_New_Candidate := False;
6082 end if;
6084 if Is_New_Candidate then
6086 -- If entity is a child unit, either it is a visible child of
6087 -- the prefix, or we are in the body of a generic prefix, as
6088 -- will happen when a child unit is instantiated in the body
6089 -- of a generic parent. This is because the instance body does
6090 -- not restore the full compilation context, given that all
6091 -- non-local references have been captured.
6093 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
6094 exit when Is_Visible_Lib_Unit (Id)
6095 or else (Is_Child_Unit (Id)
6096 and then In_Open_Scopes (Scope (Id))
6097 and then In_Instance_Body);
6098 else
6099 exit when not Is_Hidden (Id);
6100 end if;
6102 exit when Is_Immediately_Visible (Id);
6103 end if;
6105 Id := Homonym (Id);
6106 end loop;
6107 end;
6109 if No (Id)
6110 and then Ekind_In (P_Name, E_Procedure, E_Function)
6111 and then Is_Generic_Instance (P_Name)
6112 then
6113 -- Expanded name denotes entity in (instance of) generic subprogram.
6114 -- The entity may be in the subprogram instance, or may denote one of
6115 -- the formals, which is declared in the enclosing wrapper package.
6117 P_Name := Scope (P_Name);
6119 Id := Current_Entity (Selector);
6120 while Present (Id) loop
6121 exit when Scope (Id) = P_Name;
6122 Id := Homonym (Id);
6123 end loop;
6124 end if;
6126 if No (Id) or else Chars (Id) /= Chars (Selector) then
6127 Set_Etype (N, Any_Type);
6129 -- If we are looking for an entity defined in System, try to find it
6130 -- in the child package that may have been provided as an extension
6131 -- to System. The Extend_System pragma will have supplied the name of
6132 -- the extension, which may have to be loaded.
6134 if Chars (P_Name) = Name_System
6135 and then Scope (P_Name) = Standard_Standard
6136 and then Present (System_Extend_Unit)
6137 and then Present_System_Aux (N)
6138 then
6139 Set_Entity (Prefix (N), System_Aux_Id);
6140 Find_Expanded_Name (N);
6141 return;
6143 -- There is an implicit instance of the predefined operator in
6144 -- the given scope. The operator entity is defined in Standard.
6145 -- Has_Implicit_Operator makes the node into an Expanded_Name.
6147 elsif Nkind (Selector) = N_Operator_Symbol
6148 and then Has_Implicit_Operator (N)
6149 then
6150 return;
6152 -- If there is no literal defined in the scope denoted by the
6153 -- prefix, the literal may belong to (a type derived from)
6154 -- Standard_Character, for which we have no explicit literals.
6156 elsif Nkind (Selector) = N_Character_Literal
6157 and then Has_Implicit_Character_Literal (N)
6158 then
6159 return;
6161 else
6162 -- If the prefix is a single concurrent object, use its name in
6163 -- the error message, rather than that of the anonymous type.
6165 if Is_Concurrent_Type (P_Name)
6166 and then Is_Internal_Name (Chars (P_Name))
6167 then
6168 Error_Msg_Node_2 := Entity (Prefix (N));
6169 else
6170 Error_Msg_Node_2 := P_Name;
6171 end if;
6173 if P_Name = System_Aux_Id then
6174 P_Name := Scope (P_Name);
6175 Set_Entity (Prefix (N), P_Name);
6176 end if;
6178 if Present (Candidate) then
6180 -- If we know that the unit is a child unit we can give a more
6181 -- accurate error message.
6183 if Is_Child_Unit (Candidate) then
6185 -- If the candidate is a private child unit and we are in
6186 -- the visible part of a public unit, specialize the error
6187 -- message. There might be a private with_clause for it,
6188 -- but it is not currently active.
6190 if Is_Private_Descendant (Candidate)
6191 and then Ekind (Current_Scope) = E_Package
6192 and then not In_Private_Part (Current_Scope)
6193 and then not Is_Private_Descendant (Current_Scope)
6194 then
6195 Error_Msg_N
6196 ("private child unit& is not visible here", Selector);
6198 -- Normal case where we have a missing with for a child unit
6200 else
6201 Error_Msg_Qual_Level := 99;
6202 Error_Msg_NE -- CODEFIX
6203 ("missing `WITH &;`", Selector, Candidate);
6204 Error_Msg_Qual_Level := 0;
6205 end if;
6207 -- Here we don't know that this is a child unit
6209 else
6210 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
6211 end if;
6213 else
6214 -- Within the instantiation of a child unit, the prefix may
6215 -- denote the parent instance, but the selector has the name
6216 -- of the original child. That is to say, when A.B appears
6217 -- within an instantiation of generic child unit B, the scope
6218 -- stack includes an instance of A (P_Name) and an instance
6219 -- of B under some other name. We scan the scope to find this
6220 -- child instance, which is the desired entity.
6221 -- Note that the parent may itself be a child instance, if
6222 -- the reference is of the form A.B.C, in which case A.B has
6223 -- already been rewritten with the proper entity.
6225 if In_Open_Scopes (P_Name)
6226 and then Is_Generic_Instance (P_Name)
6227 then
6228 declare
6229 Gen_Par : constant Entity_Id :=
6230 Generic_Parent (Specification
6231 (Unit_Declaration_Node (P_Name)));
6232 S : Entity_Id := Current_Scope;
6233 P : Entity_Id;
6235 begin
6236 for J in reverse 0 .. Scope_Stack.Last loop
6237 S := Scope_Stack.Table (J).Entity;
6239 exit when S = Standard_Standard;
6241 if Ekind_In (S, E_Function,
6242 E_Package,
6243 E_Procedure)
6244 then
6245 P :=
6246 Generic_Parent (Specification
6247 (Unit_Declaration_Node (S)));
6249 -- Check that P is a generic child of the generic
6250 -- parent of the prefix.
6252 if Present (P)
6253 and then Chars (P) = Chars (Selector)
6254 and then Scope (P) = Gen_Par
6255 then
6256 Id := S;
6257 goto Found;
6258 end if;
6259 end if;
6261 end loop;
6262 end;
6263 end if;
6265 -- If this is a selection from Ada, System or Interfaces, then
6266 -- we assume a missing with for the corresponding package.
6268 if Is_Known_Unit (N) then
6269 if not Error_Posted (N) then
6270 Error_Msg_Node_2 := Selector;
6271 Error_Msg_N -- CODEFIX
6272 ("missing `WITH &.&;`", Prefix (N));
6273 end if;
6275 -- If this is a selection from a dummy package, then suppress
6276 -- the error message, of course the entity is missing if the
6277 -- package is missing.
6279 elsif Sloc (Error_Msg_Node_2) = No_Location then
6280 null;
6282 -- Here we have the case of an undefined component
6284 else
6285 -- The prefix may hide a homonym in the context that
6286 -- declares the desired entity. This error can use a
6287 -- specialized message.
6289 if In_Open_Scopes (P_Name) then
6290 declare
6291 H : constant Entity_Id := Homonym (P_Name);
6293 begin
6294 if Present (H)
6295 and then Is_Compilation_Unit (H)
6296 and then
6297 (Is_Immediately_Visible (H)
6298 or else Is_Visible_Lib_Unit (H))
6299 then
6300 Id := First_Entity (H);
6301 while Present (Id) loop
6302 if Chars (Id) = Chars (Selector) then
6303 Error_Msg_Qual_Level := 99;
6304 Error_Msg_Name_1 := Chars (Selector);
6305 Error_Msg_NE
6306 ("% not declared in&", N, P_Name);
6307 Error_Msg_NE
6308 ("\use fully qualified name starting with "
6309 & "Standard to make& visible", N, H);
6310 Error_Msg_Qual_Level := 0;
6311 goto Done;
6312 end if;
6314 Next_Entity (Id);
6315 end loop;
6316 end if;
6318 -- If not found, standard error message
6320 Error_Msg_NE ("& not declared in&", N, Selector);
6322 <<Done>> null;
6323 end;
6325 else
6326 -- Might be worth specializing the case when the prefix
6327 -- is a limited view.
6328 -- ... not declared in limited view of...
6330 Error_Msg_NE ("& not declared in&", N, Selector);
6331 end if;
6333 -- Check for misspelling of some entity in prefix
6335 Id := First_Entity (P_Name);
6336 while Present (Id) loop
6337 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
6338 and then not Is_Internal_Name (Chars (Id))
6339 then
6340 Error_Msg_NE -- CODEFIX
6341 ("possible misspelling of&", Selector, Id);
6342 exit;
6343 end if;
6345 Next_Entity (Id);
6346 end loop;
6348 -- Specialize the message if this may be an instantiation
6349 -- of a child unit that was not mentioned in the context.
6351 if Nkind (Parent (N)) = N_Package_Instantiation
6352 and then Is_Generic_Instance (Entity (Prefix (N)))
6353 and then Is_Compilation_Unit
6354 (Generic_Parent (Parent (Entity (Prefix (N)))))
6355 then
6356 Error_Msg_Node_2 := Selector;
6357 Error_Msg_N -- CODEFIX
6358 ("\missing `WITH &.&;`", Prefix (N));
6359 end if;
6360 end if;
6361 end if;
6363 Id := Any_Id;
6364 end if;
6365 end if;
6367 <<Found>>
6368 if Comes_From_Source (N)
6369 and then Is_Remote_Access_To_Subprogram_Type (Id)
6370 and then Ekind (Id) = E_Access_Subprogram_Type
6371 and then Present (Equivalent_Type (Id))
6372 then
6373 -- If we are not actually generating distribution code (i.e. the
6374 -- current PCS is the dummy non-distributed version), then the
6375 -- Equivalent_Type will be missing, and Id should be treated as
6376 -- a regular access-to-subprogram type.
6378 Id := Equivalent_Type (Id);
6379 Set_Chars (Selector, Chars (Id));
6380 end if;
6382 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
6384 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
6385 if From_Limited_With (Id)
6386 or else Is_Type (Id)
6387 or else Ekind (Id) = E_Package
6388 then
6389 null;
6390 else
6391 Error_Msg_N
6392 ("limited withed package can only be used to access incomplete "
6393 & "types", N);
6394 end if;
6395 end if;
6397 if Is_Task_Type (P_Name)
6398 and then ((Ekind (Id) = E_Entry
6399 and then Nkind (Parent (N)) /= N_Attribute_Reference)
6400 or else
6401 (Ekind (Id) = E_Entry_Family
6402 and then
6403 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
6404 then
6405 -- If both the task type and the entry are in scope, this may still
6406 -- be the expanded name of an entry formal.
6408 if In_Open_Scopes (Id)
6409 and then Nkind (Parent (N)) = N_Selected_Component
6410 then
6411 null;
6413 else
6414 -- It is an entry call after all, either to the current task
6415 -- (which will deadlock) or to an enclosing task.
6417 Analyze_Selected_Component (N);
6418 return;
6419 end if;
6420 end if;
6422 Change_Selected_Component_To_Expanded_Name (N);
6424 -- Set appropriate type
6426 if Is_Type (Id) then
6427 Set_Etype (N, Id);
6428 else
6429 Set_Etype (N, Get_Full_View (Etype (Id)));
6430 end if;
6432 -- Do style check and generate reference, but skip both steps if this
6433 -- entity has homonyms, since we may not have the right homonym set yet.
6434 -- The proper homonym will be set during the resolve phase.
6436 if Has_Homonym (Id) then
6437 Set_Entity (N, Id);
6439 else
6440 Set_Entity_Or_Discriminal (N, Id);
6442 case Is_LHS (N) is
6443 when Yes =>
6444 Generate_Reference (Id, N, 'm');
6446 when No =>
6447 Generate_Reference (Id, N, 'r');
6449 when Unknown =>
6450 Deferred_References.Append ((Id, N));
6451 end case;
6452 end if;
6454 -- Check for violation of No_Wide_Characters
6456 Check_Wide_Character_Restriction (Id, N);
6458 -- If the Ekind of the entity is Void, it means that all homonyms are
6459 -- hidden from all visibility (RM 8.3(5,14-20)).
6461 if Ekind (Id) = E_Void then
6462 Premature_Usage (N);
6464 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6465 declare
6466 H : Entity_Id := Homonym (Id);
6468 begin
6469 while Present (H) loop
6470 if Scope (H) = Scope (Id)
6471 and then (not Is_Hidden (H)
6472 or else Is_Immediately_Visible (H))
6473 then
6474 Collect_Interps (N);
6475 exit;
6476 end if;
6478 H := Homonym (H);
6479 end loop;
6481 -- If an extension of System is present, collect possible explicit
6482 -- overloadings declared in the extension.
6484 if Chars (P_Name) = Name_System
6485 and then Scope (P_Name) = Standard_Standard
6486 and then Present (System_Extend_Unit)
6487 and then Present_System_Aux (N)
6488 then
6489 H := Current_Entity (Id);
6491 while Present (H) loop
6492 if Scope (H) = System_Aux_Id then
6493 Add_One_Interp (N, H, Etype (H));
6494 end if;
6496 H := Homonym (H);
6497 end loop;
6498 end if;
6499 end;
6500 end if;
6502 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6503 and then Scope (Id) /= Standard_Standard
6504 then
6505 -- In addition to user-defined operators in the given scope, there
6506 -- may be an implicit instance of the predefined operator. The
6507 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6508 -- and added to the interpretations. Procedure Add_One_Interp will
6509 -- determine which hides which.
6511 if Has_Implicit_Operator (N) then
6512 null;
6513 end if;
6514 end if;
6516 -- If there is a single interpretation for N we can generate a
6517 -- reference to the unique entity found.
6519 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6520 Generate_Reference (Id, N);
6521 end if;
6523 -- Mark relevant use-type and use-package clauses as effective if the
6524 -- node in question is not overloaded and therefore does not require
6525 -- resolution.
6527 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6528 Mark_Use_Clauses (N);
6529 end if;
6531 Check_Restriction_No_Use_Of_Entity (N);
6532 end Find_Expanded_Name;
6534 --------------------
6535 -- Find_Most_Prev --
6536 --------------------
6538 function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id is
6539 Curr : Node_Id;
6540 begin
6541 -- Loop through the Prev_Use_Clause chain
6543 Curr := Use_Clause;
6544 while Present (Prev_Use_Clause (Curr)) loop
6545 Curr := Prev_Use_Clause (Curr);
6546 end loop;
6548 return Curr;
6549 end Find_Most_Prev;
6551 -------------------------
6552 -- Find_Renamed_Entity --
6553 -------------------------
6555 function Find_Renamed_Entity
6556 (N : Node_Id;
6557 Nam : Node_Id;
6558 New_S : Entity_Id;
6559 Is_Actual : Boolean := False) return Entity_Id
6561 Ind : Interp_Index;
6562 I1 : Interp_Index := 0; -- Suppress junk warnings
6563 It : Interp;
6564 It1 : Interp;
6565 Old_S : Entity_Id;
6566 Inst : Entity_Id;
6568 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
6569 -- If the renamed entity is an implicit operator, check whether it is
6570 -- visible because its operand type is properly visible. This check
6571 -- applies to explicit renamed entities that appear in the source in a
6572 -- renaming declaration or a formal subprogram instance, but not to
6573 -- default generic actuals with a name.
6575 function Report_Overload return Entity_Id;
6576 -- List possible interpretations, and specialize message in the
6577 -- case of a generic actual.
6579 function Within (Inner, Outer : Entity_Id) return Boolean;
6580 -- Determine whether a candidate subprogram is defined within the
6581 -- enclosing instance. If yes, it has precedence over outer candidates.
6583 --------------------------
6584 -- Is_Visible_Operation --
6585 --------------------------
6587 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
6588 Scop : Entity_Id;
6589 Typ : Entity_Id;
6590 Btyp : Entity_Id;
6592 begin
6593 if Ekind (Op) /= E_Operator
6594 or else Scope (Op) /= Standard_Standard
6595 or else (In_Instance
6596 and then (not Is_Actual
6597 or else Present (Enclosing_Instance)))
6598 then
6599 return True;
6601 else
6602 -- For a fixed point type operator, check the resulting type,
6603 -- because it may be a mixed mode integer * fixed operation.
6605 if Present (Next_Formal (First_Formal (New_S)))
6606 and then Is_Fixed_Point_Type (Etype (New_S))
6607 then
6608 Typ := Etype (New_S);
6609 else
6610 Typ := Etype (First_Formal (New_S));
6611 end if;
6613 Btyp := Base_Type (Typ);
6615 if Nkind (Nam) /= N_Expanded_Name then
6616 return (In_Open_Scopes (Scope (Btyp))
6617 or else Is_Potentially_Use_Visible (Btyp)
6618 or else In_Use (Btyp)
6619 or else In_Use (Scope (Btyp)));
6621 else
6622 Scop := Entity (Prefix (Nam));
6624 if Ekind (Scop) = E_Package
6625 and then Present (Renamed_Object (Scop))
6626 then
6627 Scop := Renamed_Object (Scop);
6628 end if;
6630 -- Operator is visible if prefix of expanded name denotes
6631 -- scope of type, or else type is defined in System_Aux
6632 -- and the prefix denotes System.
6634 return Scope (Btyp) = Scop
6635 or else (Scope (Btyp) = System_Aux_Id
6636 and then Scope (Scope (Btyp)) = Scop);
6637 end if;
6638 end if;
6639 end Is_Visible_Operation;
6641 ------------
6642 -- Within --
6643 ------------
6645 function Within (Inner, Outer : Entity_Id) return Boolean is
6646 Sc : Entity_Id;
6648 begin
6649 Sc := Scope (Inner);
6650 while Sc /= Standard_Standard loop
6651 if Sc = Outer then
6652 return True;
6653 else
6654 Sc := Scope (Sc);
6655 end if;
6656 end loop;
6658 return False;
6659 end Within;
6661 ---------------------
6662 -- Report_Overload --
6663 ---------------------
6665 function Report_Overload return Entity_Id is
6666 begin
6667 if Is_Actual then
6668 Error_Msg_NE -- CODEFIX
6669 ("ambiguous actual subprogram&, " &
6670 "possible interpretations:", N, Nam);
6671 else
6672 Error_Msg_N -- CODEFIX
6673 ("ambiguous subprogram, " &
6674 "possible interpretations:", N);
6675 end if;
6677 List_Interps (Nam, N);
6678 return Old_S;
6679 end Report_Overload;
6681 -- Start of processing for Find_Renamed_Entity
6683 begin
6684 Old_S := Any_Id;
6685 Candidate_Renaming := Empty;
6687 if Is_Overloaded (Nam) then
6688 Get_First_Interp (Nam, Ind, It);
6689 while Present (It.Nam) loop
6690 if Entity_Matches_Spec (It.Nam, New_S)
6691 and then Is_Visible_Operation (It.Nam)
6692 then
6693 if Old_S /= Any_Id then
6695 -- Note: The call to Disambiguate only happens if a
6696 -- previous interpretation was found, in which case I1
6697 -- has received a value.
6699 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
6701 if It1 = No_Interp then
6702 Inst := Enclosing_Instance;
6704 if Present (Inst) then
6705 if Within (It.Nam, Inst) then
6706 if Within (Old_S, Inst) then
6708 -- Choose the innermost subprogram, which would
6709 -- have hidden the outer one in the generic.
6711 if Scope_Depth (It.Nam) <
6712 Scope_Depth (Old_S)
6713 then
6714 return Old_S;
6715 else
6716 return It.Nam;
6717 end if;
6718 end if;
6720 elsif Within (Old_S, Inst) then
6721 return (Old_S);
6723 else
6724 return Report_Overload;
6725 end if;
6727 -- If not within an instance, ambiguity is real
6729 else
6730 return Report_Overload;
6731 end if;
6733 else
6734 Old_S := It1.Nam;
6735 exit;
6736 end if;
6738 else
6739 I1 := Ind;
6740 Old_S := It.Nam;
6741 end if;
6743 elsif
6744 Present (First_Formal (It.Nam))
6745 and then Present (First_Formal (New_S))
6746 and then (Base_Type (Etype (First_Formal (It.Nam))) =
6747 Base_Type (Etype (First_Formal (New_S))))
6748 then
6749 Candidate_Renaming := It.Nam;
6750 end if;
6752 Get_Next_Interp (Ind, It);
6753 end loop;
6755 Set_Entity (Nam, Old_S);
6757 if Old_S /= Any_Id then
6758 Set_Is_Overloaded (Nam, False);
6759 end if;
6761 -- Non-overloaded case
6763 else
6764 if Is_Actual
6765 and then Present (Enclosing_Instance)
6766 and then Entity_Matches_Spec (Entity (Nam), New_S)
6767 then
6768 Old_S := Entity (Nam);
6770 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
6771 Candidate_Renaming := New_S;
6773 if Is_Visible_Operation (Entity (Nam)) then
6774 Old_S := Entity (Nam);
6775 end if;
6777 elsif Present (First_Formal (Entity (Nam)))
6778 and then Present (First_Formal (New_S))
6779 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
6780 Base_Type (Etype (First_Formal (New_S))))
6781 then
6782 Candidate_Renaming := Entity (Nam);
6783 end if;
6784 end if;
6786 return Old_S;
6787 end Find_Renamed_Entity;
6789 -----------------------------
6790 -- Find_Selected_Component --
6791 -----------------------------
6793 procedure Find_Selected_Component (N : Node_Id) is
6794 P : constant Node_Id := Prefix (N);
6796 P_Name : Entity_Id;
6797 -- Entity denoted by prefix
6799 P_Type : Entity_Id;
6800 -- and its type
6802 Nam : Node_Id;
6804 function Available_Subtype return Boolean;
6805 -- A small optimization: if the prefix is constrained and the component
6806 -- is an array type we may already have a usable subtype for it, so we
6807 -- can use it rather than generating a new one, because the bounds
6808 -- will be the values of the discriminants and not discriminant refs.
6809 -- This simplifies value tracing in GNATProve. For consistency, both
6810 -- the entity name and the subtype come from the constrained component.
6812 -- This is only used in GNATProve mode: when generating code it may be
6813 -- necessary to create an itype in the scope of use of the selected
6814 -- component, e.g. in the context of a expanded record equality.
6816 function Is_Reference_In_Subunit return Boolean;
6817 -- In a subunit, the scope depth is not a proper measure of hiding,
6818 -- because the context of the proper body may itself hide entities in
6819 -- parent units. This rare case requires inspecting the tree directly
6820 -- because the proper body is inserted in the main unit and its context
6821 -- is simply added to that of the parent.
6823 -----------------------
6824 -- Available_Subtype --
6825 -----------------------
6827 function Available_Subtype return Boolean is
6828 Comp : Entity_Id;
6830 begin
6831 if GNATprove_Mode then
6832 Comp := First_Entity (Etype (P));
6833 while Present (Comp) loop
6834 if Chars (Comp) = Chars (Selector_Name (N)) then
6835 Set_Etype (N, Etype (Comp));
6836 Set_Entity (Selector_Name (N), Comp);
6837 Set_Etype (Selector_Name (N), Etype (Comp));
6838 return True;
6839 end if;
6841 Next_Component (Comp);
6842 end loop;
6843 end if;
6845 return False;
6846 end Available_Subtype;
6848 -----------------------------
6849 -- Is_Reference_In_Subunit --
6850 -----------------------------
6852 function Is_Reference_In_Subunit return Boolean is
6853 Clause : Node_Id;
6854 Comp_Unit : Node_Id;
6856 begin
6857 Comp_Unit := N;
6858 while Present (Comp_Unit)
6859 and then Nkind (Comp_Unit) /= N_Compilation_Unit
6860 loop
6861 Comp_Unit := Parent (Comp_Unit);
6862 end loop;
6864 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
6865 return False;
6866 end if;
6868 -- Now check whether the package is in the context of the subunit
6870 Clause := First (Context_Items (Comp_Unit));
6871 while Present (Clause) loop
6872 if Nkind (Clause) = N_With_Clause
6873 and then Entity (Name (Clause)) = P_Name
6874 then
6875 return True;
6876 end if;
6878 Clause := Next (Clause);
6879 end loop;
6881 return False;
6882 end Is_Reference_In_Subunit;
6884 -- Start of processing for Find_Selected_Component
6886 begin
6887 Analyze (P);
6889 if Nkind (P) = N_Error then
6890 return;
6891 end if;
6893 -- Selector name cannot be a character literal or an operator symbol in
6894 -- SPARK, except for the operator symbol in a renaming.
6896 if Restriction_Check_Required (SPARK_05) then
6897 if Nkind (Selector_Name (N)) = N_Character_Literal then
6898 Check_SPARK_05_Restriction
6899 ("character literal cannot be prefixed", N);
6900 elsif Nkind (Selector_Name (N)) = N_Operator_Symbol
6901 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
6902 then
6903 Check_SPARK_05_Restriction
6904 ("operator symbol cannot be prefixed", N);
6905 end if;
6906 end if;
6908 -- If the selector already has an entity, the node has been constructed
6909 -- in the course of expansion, and is known to be valid. Do not verify
6910 -- that it is defined for the type (it may be a private component used
6911 -- in the expansion of record equality).
6913 if Present (Entity (Selector_Name (N))) then
6914 if No (Etype (N)) or else Etype (N) = Any_Type then
6915 declare
6916 Sel_Name : constant Node_Id := Selector_Name (N);
6917 Selector : constant Entity_Id := Entity (Sel_Name);
6918 C_Etype : Node_Id;
6920 begin
6921 Set_Etype (Sel_Name, Etype (Selector));
6923 if not Is_Entity_Name (P) then
6924 Resolve (P);
6925 end if;
6927 -- Build an actual subtype except for the first parameter
6928 -- of an init proc, where this actual subtype is by
6929 -- definition incorrect, since the object is uninitialized
6930 -- (and does not even have defined discriminants etc.)
6932 if Is_Entity_Name (P)
6933 and then Ekind (Entity (P)) = E_Function
6934 then
6935 Nam := New_Copy (P);
6937 if Is_Overloaded (P) then
6938 Save_Interps (P, Nam);
6939 end if;
6941 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
6942 Analyze_Call (P);
6943 Analyze_Selected_Component (N);
6944 return;
6946 elsif Ekind (Selector) = E_Component
6947 and then (not Is_Entity_Name (P)
6948 or else Chars (Entity (P)) /= Name_uInit)
6949 then
6950 -- Check if we already have an available subtype we can use
6952 if Ekind (Etype (P)) = E_Record_Subtype
6953 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
6954 and then Is_Array_Type (Etype (Selector))
6955 and then not Is_Packed (Etype (Selector))
6956 and then Available_Subtype
6957 then
6958 return;
6960 -- Do not build the subtype when referencing components of
6961 -- dispatch table wrappers. Required to avoid generating
6962 -- elaboration code with HI runtimes.
6964 elsif RTU_Loaded (Ada_Tags)
6965 and then
6966 ((RTE_Available (RE_Dispatch_Table_Wrapper)
6967 and then Scope (Selector) =
6968 RTE (RE_Dispatch_Table_Wrapper))
6969 or else
6970 (RTE_Available (RE_No_Dispatch_Table_Wrapper)
6971 and then Scope (Selector) =
6972 RTE (RE_No_Dispatch_Table_Wrapper)))
6973 then
6974 C_Etype := Empty;
6975 else
6976 C_Etype :=
6977 Build_Actual_Subtype_Of_Component
6978 (Etype (Selector), N);
6979 end if;
6981 else
6982 C_Etype := Empty;
6983 end if;
6985 if No (C_Etype) then
6986 C_Etype := Etype (Selector);
6987 else
6988 Insert_Action (N, C_Etype);
6989 C_Etype := Defining_Identifier (C_Etype);
6990 end if;
6992 Set_Etype (N, C_Etype);
6993 end;
6995 -- If this is the name of an entry or protected operation, and
6996 -- the prefix is an access type, insert an explicit dereference,
6997 -- so that entry calls are treated uniformly.
6999 if Is_Access_Type (Etype (P))
7000 and then Is_Concurrent_Type (Designated_Type (Etype (P)))
7001 then
7002 declare
7003 New_P : constant Node_Id :=
7004 Make_Explicit_Dereference (Sloc (P),
7005 Prefix => Relocate_Node (P));
7006 begin
7007 Rewrite (P, New_P);
7008 Set_Etype (P, Designated_Type (Etype (Prefix (P))));
7009 end;
7010 end if;
7012 -- If the selected component appears within a default expression
7013 -- and it has an actual subtype, the pre-analysis has not yet
7014 -- completed its analysis, because Insert_Actions is disabled in
7015 -- that context. Within the init proc of the enclosing type we
7016 -- must complete this analysis, if an actual subtype was created.
7018 elsif Inside_Init_Proc then
7019 declare
7020 Typ : constant Entity_Id := Etype (N);
7021 Decl : constant Node_Id := Declaration_Node (Typ);
7022 begin
7023 if Nkind (Decl) = N_Subtype_Declaration
7024 and then not Analyzed (Decl)
7025 and then Is_List_Member (Decl)
7026 and then No (Parent (Decl))
7027 then
7028 Remove (Decl);
7029 Insert_Action (N, Decl);
7030 end if;
7031 end;
7032 end if;
7034 return;
7036 elsif Is_Entity_Name (P) then
7037 P_Name := Entity (P);
7039 -- The prefix may denote an enclosing type which is the completion
7040 -- of an incomplete type declaration.
7042 if Is_Type (P_Name) then
7043 Set_Entity (P, Get_Full_View (P_Name));
7044 Set_Etype (P, Entity (P));
7045 P_Name := Entity (P);
7046 end if;
7048 P_Type := Base_Type (Etype (P));
7050 if Debug_Flag_E then
7051 Write_Str ("Found prefix type to be ");
7052 Write_Entity_Info (P_Type, " "); Write_Eol;
7053 end if;
7055 -- The designated type may be a limited view with no components.
7056 -- Check whether the non-limited view is available, because in some
7057 -- cases this will not be set when installing the context. Rewrite
7058 -- the node by introducing an explicit dereference at once, and
7059 -- setting the type of the rewritten prefix to the non-limited view
7060 -- of the original designated type.
7062 if Is_Access_Type (P_Type) then
7063 declare
7064 Desig_Typ : constant Entity_Id :=
7065 Directly_Designated_Type (P_Type);
7067 begin
7068 if Is_Incomplete_Type (Desig_Typ)
7069 and then From_Limited_With (Desig_Typ)
7070 and then Present (Non_Limited_View (Desig_Typ))
7071 then
7072 Rewrite (P,
7073 Make_Explicit_Dereference (Sloc (P),
7074 Prefix => Relocate_Node (P)));
7076 Set_Etype (P, Get_Full_View (Non_Limited_View (Desig_Typ)));
7077 P_Type := Etype (P);
7078 end if;
7079 end;
7080 end if;
7082 -- First check for components of a record object (not the
7083 -- result of a call, which is handled below).
7085 if Is_Appropriate_For_Record (P_Type)
7086 and then not Is_Overloadable (P_Name)
7087 and then not Is_Type (P_Name)
7088 then
7089 -- Selected component of record. Type checking will validate
7090 -- name of selector.
7092 -- ??? Could we rewrite an implicit dereference into an explicit
7093 -- one here?
7095 Analyze_Selected_Component (N);
7097 -- Reference to type name in predicate/invariant expression
7099 elsif Is_Appropriate_For_Entry_Prefix (P_Type)
7100 and then not In_Open_Scopes (P_Name)
7101 and then (not Is_Concurrent_Type (Etype (P_Name))
7102 or else not In_Open_Scopes (Etype (P_Name)))
7103 then
7104 -- Call to protected operation or entry. Type checking is
7105 -- needed on the prefix.
7107 Analyze_Selected_Component (N);
7109 elsif (In_Open_Scopes (P_Name)
7110 and then Ekind (P_Name) /= E_Void
7111 and then not Is_Overloadable (P_Name))
7112 or else (Is_Concurrent_Type (Etype (P_Name))
7113 and then In_Open_Scopes (Etype (P_Name)))
7114 then
7115 -- Prefix denotes an enclosing loop, block, or task, i.e. an
7116 -- enclosing construct that is not a subprogram or accept.
7118 -- A special case: a protected body may call an operation
7119 -- on an external object of the same type, in which case it
7120 -- is not an expanded name. If the prefix is the type itself,
7121 -- or the context is a single synchronized object it can only
7122 -- be interpreted as an expanded name.
7124 if Is_Concurrent_Type (Etype (P_Name)) then
7125 if Is_Type (P_Name)
7126 or else Present (Anonymous_Object (Etype (P_Name)))
7127 then
7128 Find_Expanded_Name (N);
7130 else
7131 Analyze_Selected_Component (N);
7132 return;
7133 end if;
7135 else
7136 Find_Expanded_Name (N);
7137 end if;
7139 elsif Ekind (P_Name) = E_Package then
7140 Find_Expanded_Name (N);
7142 elsif Is_Overloadable (P_Name) then
7144 -- The subprogram may be a renaming (of an enclosing scope) as
7145 -- in the case of the name of the generic within an instantiation.
7147 if Ekind_In (P_Name, E_Procedure, E_Function)
7148 and then Present (Alias (P_Name))
7149 and then Is_Generic_Instance (Alias (P_Name))
7150 then
7151 P_Name := Alias (P_Name);
7152 end if;
7154 if Is_Overloaded (P) then
7156 -- The prefix must resolve to a unique enclosing construct
7158 declare
7159 Found : Boolean := False;
7160 Ind : Interp_Index;
7161 It : Interp;
7163 begin
7164 Get_First_Interp (P, Ind, It);
7165 while Present (It.Nam) loop
7166 if In_Open_Scopes (It.Nam) then
7167 if Found then
7168 Error_Msg_N (
7169 "prefix must be unique enclosing scope", N);
7170 Set_Entity (N, Any_Id);
7171 Set_Etype (N, Any_Type);
7172 return;
7174 else
7175 Found := True;
7176 P_Name := It.Nam;
7177 end if;
7178 end if;
7180 Get_Next_Interp (Ind, It);
7181 end loop;
7182 end;
7183 end if;
7185 if In_Open_Scopes (P_Name) then
7186 Set_Entity (P, P_Name);
7187 Set_Is_Overloaded (P, False);
7188 Find_Expanded_Name (N);
7190 else
7191 -- If no interpretation as an expanded name is possible, it
7192 -- must be a selected component of a record returned by a
7193 -- function call. Reformat prefix as a function call, the rest
7194 -- is done by type resolution.
7196 -- Error if the prefix is procedure or entry, as is P.X
7198 if Ekind (P_Name) /= E_Function
7199 and then
7200 (not Is_Overloaded (P)
7201 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
7202 then
7203 -- Prefix may mention a package that is hidden by a local
7204 -- declaration: let the user know. Scan the full homonym
7205 -- chain, the candidate package may be anywhere on it.
7207 if Present (Homonym (Current_Entity (P_Name))) then
7208 P_Name := Current_Entity (P_Name);
7210 while Present (P_Name) loop
7211 exit when Ekind (P_Name) = E_Package;
7212 P_Name := Homonym (P_Name);
7213 end loop;
7215 if Present (P_Name) then
7216 if not Is_Reference_In_Subunit then
7217 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
7218 Error_Msg_NE
7219 ("package& is hidden by declaration#", N, P_Name);
7220 end if;
7222 Set_Entity (Prefix (N), P_Name);
7223 Find_Expanded_Name (N);
7224 return;
7226 else
7227 P_Name := Entity (Prefix (N));
7228 end if;
7229 end if;
7231 Error_Msg_NE
7232 ("invalid prefix in selected component&", N, P_Name);
7233 Change_Selected_Component_To_Expanded_Name (N);
7234 Set_Entity (N, Any_Id);
7235 Set_Etype (N, Any_Type);
7237 -- Here we have a function call, so do the reformatting
7239 else
7240 Nam := New_Copy (P);
7241 Save_Interps (P, Nam);
7243 -- We use Replace here because this is one of those cases
7244 -- where the parser has missclassified the node, and we fix
7245 -- things up and then do the semantic analysis on the fixed
7246 -- up node. Normally we do this using one of the Sinfo.CN
7247 -- routines, but this is too tricky for that.
7249 -- Note that using Rewrite would be wrong, because we would
7250 -- have a tree where the original node is unanalyzed, and
7251 -- this violates the required interface for ASIS.
7253 Replace (P,
7254 Make_Function_Call (Sloc (P), Name => Nam));
7256 -- Now analyze the reformatted node
7258 Analyze_Call (P);
7260 -- If the prefix is illegal after this transformation, there
7261 -- may be visibility errors on the prefix. The safest is to
7262 -- treat the selected component as an error.
7264 if Error_Posted (P) then
7265 Set_Etype (N, Any_Type);
7266 return;
7268 else
7269 Analyze_Selected_Component (N);
7270 end if;
7271 end if;
7272 end if;
7274 -- Remaining cases generate various error messages
7276 else
7277 -- Format node as expanded name, to avoid cascaded errors
7279 -- If the limited_with transformation was applied earlier, restore
7280 -- source for proper error reporting.
7282 if not Comes_From_Source (P)
7283 and then Nkind (P) = N_Explicit_Dereference
7284 then
7285 Rewrite (P, Prefix (P));
7286 P_Type := Etype (P);
7287 end if;
7289 Change_Selected_Component_To_Expanded_Name (N);
7290 Set_Entity (N, Any_Id);
7291 Set_Etype (N, Any_Type);
7293 -- Issue error message, but avoid this if error issued already.
7294 -- Use identifier of prefix if one is available.
7296 if P_Name = Any_Id then
7297 null;
7299 -- It is not an error if the prefix is the current instance of
7300 -- type name, e.g. the expression of a type aspect, when it is
7301 -- analyzed for ASIS use.
7303 elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then
7304 null;
7306 elsif Ekind (P_Name) = E_Void then
7307 Premature_Usage (P);
7309 elsif Nkind (P) /= N_Attribute_Reference then
7311 -- This may have been meant as a prefixed call to a primitive
7312 -- of an untagged type. If it is a function call check type of
7313 -- its first formal and add explanation.
7315 declare
7316 F : constant Entity_Id :=
7317 Current_Entity (Selector_Name (N));
7318 begin
7319 if Present (F)
7320 and then Is_Overloadable (F)
7321 and then Present (First_Entity (F))
7322 and then not Is_Tagged_Type (Etype (First_Entity (F)))
7323 then
7324 Error_Msg_N
7325 ("prefixed call is only allowed for objects of a "
7326 & "tagged type", N);
7327 end if;
7328 end;
7330 Error_Msg_N ("invalid prefix in selected component&", P);
7332 if Is_Access_Type (P_Type)
7333 and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
7334 then
7335 Error_Msg_N
7336 ("\dereference must not be of an incomplete type "
7337 & "(RM 3.10.1)", P);
7338 end if;
7340 else
7341 Error_Msg_N ("invalid prefix in selected component", P);
7342 end if;
7343 end if;
7345 -- Selector name is restricted in SPARK
7347 if Nkind (N) = N_Expanded_Name
7348 and then Restriction_Check_Required (SPARK_05)
7349 then
7350 if Is_Subprogram (P_Name) then
7351 Check_SPARK_05_Restriction
7352 ("prefix of expanded name cannot be a subprogram", P);
7353 elsif Ekind (P_Name) = E_Loop then
7354 Check_SPARK_05_Restriction
7355 ("prefix of expanded name cannot be a loop statement", P);
7356 end if;
7357 end if;
7359 else
7360 -- If prefix is not the name of an entity, it must be an expression,
7361 -- whose type is appropriate for a record. This is determined by
7362 -- type resolution.
7364 Analyze_Selected_Component (N);
7365 end if;
7367 Analyze_Dimension (N);
7368 end Find_Selected_Component;
7370 ---------------
7371 -- Find_Type --
7372 ---------------
7374 procedure Find_Type (N : Node_Id) is
7375 C : Entity_Id;
7376 Typ : Entity_Id;
7377 T : Entity_Id;
7378 T_Name : Entity_Id;
7380 begin
7381 if N = Error then
7382 return;
7384 elsif Nkind (N) = N_Attribute_Reference then
7386 -- Class attribute. This is not valid in Ada 83 mode, but we do not
7387 -- need to enforce that at this point, since the declaration of the
7388 -- tagged type in the prefix would have been flagged already.
7390 if Attribute_Name (N) = Name_Class then
7391 Check_Restriction (No_Dispatch, N);
7392 Find_Type (Prefix (N));
7394 -- Propagate error from bad prefix
7396 if Etype (Prefix (N)) = Any_Type then
7397 Set_Entity (N, Any_Type);
7398 Set_Etype (N, Any_Type);
7399 return;
7400 end if;
7402 T := Base_Type (Entity (Prefix (N)));
7404 -- Case where type is not known to be tagged. Its appearance in
7405 -- the prefix of the 'Class attribute indicates that the full view
7406 -- will be tagged.
7408 if not Is_Tagged_Type (T) then
7409 if Ekind (T) = E_Incomplete_Type then
7411 -- It is legal to denote the class type of an incomplete
7412 -- type. The full type will have to be tagged, of course.
7413 -- In Ada 2005 this usage is declared obsolescent, so we
7414 -- warn accordingly. This usage is only legal if the type
7415 -- is completed in the current scope, and not for a limited
7416 -- view of a type.
7418 if Ada_Version >= Ada_2005 then
7420 -- Test whether the Available_View of a limited type view
7421 -- is tagged, since the limited view may not be marked as
7422 -- tagged if the type itself has an untagged incomplete
7423 -- type view in its package.
7425 if From_Limited_With (T)
7426 and then not Is_Tagged_Type (Available_View (T))
7427 then
7428 Error_Msg_N
7429 ("prefix of Class attribute must be tagged", N);
7430 Set_Etype (N, Any_Type);
7431 Set_Entity (N, Any_Type);
7432 return;
7434 -- ??? This test is temporarily disabled (always
7435 -- False) because it causes an unwanted warning on
7436 -- GNAT sources (built with -gnatg, which includes
7437 -- Warn_On_Obsolescent_ Feature). Once this issue
7438 -- is cleared in the sources, it can be enabled.
7440 elsif Warn_On_Obsolescent_Feature and then False then
7441 Error_Msg_N
7442 ("applying 'Class to an untagged incomplete type"
7443 & " is an obsolescent feature (RM J.11)?r?", N);
7444 end if;
7445 end if;
7447 Set_Is_Tagged_Type (T);
7448 Set_Direct_Primitive_Operations (T, New_Elmt_List);
7449 Make_Class_Wide_Type (T);
7450 Set_Entity (N, Class_Wide_Type (T));
7451 Set_Etype (N, Class_Wide_Type (T));
7453 elsif Ekind (T) = E_Private_Type
7454 and then not Is_Generic_Type (T)
7455 and then In_Private_Part (Scope (T))
7456 then
7457 -- The Class attribute can be applied to an untagged private
7458 -- type fulfilled by a tagged type prior to the full type
7459 -- declaration (but only within the parent package's private
7460 -- part). Create the class-wide type now and check that the
7461 -- full type is tagged later during its analysis. Note that
7462 -- we do not mark the private type as tagged, unlike the
7463 -- case of incomplete types, because the type must still
7464 -- appear untagged to outside units.
7466 if No (Class_Wide_Type (T)) then
7467 Make_Class_Wide_Type (T);
7468 end if;
7470 Set_Entity (N, Class_Wide_Type (T));
7471 Set_Etype (N, Class_Wide_Type (T));
7473 else
7474 -- Should we introduce a type Any_Tagged and use Wrong_Type
7475 -- here, it would be a bit more consistent???
7477 Error_Msg_NE
7478 ("tagged type required, found}",
7479 Prefix (N), First_Subtype (T));
7480 Set_Entity (N, Any_Type);
7481 return;
7482 end if;
7484 -- Case of tagged type
7486 else
7487 if Is_Concurrent_Type (T) then
7488 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
7490 -- Previous error. Create a class-wide type for the
7491 -- synchronized type itself, with minimal semantic
7492 -- attributes, to catch other errors in some ACATS tests.
7494 pragma Assert (Serious_Errors_Detected /= 0);
7495 Make_Class_Wide_Type (T);
7496 C := Class_Wide_Type (T);
7497 Set_First_Entity (C, First_Entity (T));
7499 else
7500 C := Class_Wide_Type
7501 (Corresponding_Record_Type (Entity (Prefix (N))));
7502 end if;
7504 else
7505 C := Class_Wide_Type (Entity (Prefix (N)));
7506 end if;
7508 Set_Entity_With_Checks (N, C);
7509 Generate_Reference (C, N);
7510 Set_Etype (N, C);
7511 end if;
7513 -- Base attribute, not allowed in Ada 83
7515 elsif Attribute_Name (N) = Name_Base then
7516 Error_Msg_Name_1 := Name_Base;
7517 Check_SPARK_05_Restriction
7518 ("attribute% is only allowed as prefix of another attribute", N);
7520 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
7521 Error_Msg_N
7522 ("(Ada 83) Base attribute not allowed in subtype mark", N);
7524 else
7525 Find_Type (Prefix (N));
7526 Typ := Entity (Prefix (N));
7528 if Ada_Version >= Ada_95
7529 and then not Is_Scalar_Type (Typ)
7530 and then not Is_Generic_Type (Typ)
7531 then
7532 Error_Msg_N
7533 ("prefix of Base attribute must be scalar type",
7534 Prefix (N));
7536 elsif Warn_On_Redundant_Constructs
7537 and then Base_Type (Typ) = Typ
7538 then
7539 Error_Msg_NE -- CODEFIX
7540 ("redundant attribute, & is its own base type?r?", N, Typ);
7541 end if;
7543 T := Base_Type (Typ);
7545 -- Rewrite attribute reference with type itself (see similar
7546 -- processing in Analyze_Attribute, case Base). Preserve prefix
7547 -- if present, for other legality checks.
7549 if Nkind (Prefix (N)) = N_Expanded_Name then
7550 Rewrite (N,
7551 Make_Expanded_Name (Sloc (N),
7552 Chars => Chars (T),
7553 Prefix => New_Copy (Prefix (Prefix (N))),
7554 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
7556 else
7557 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
7558 end if;
7560 Set_Entity (N, T);
7561 Set_Etype (N, T);
7562 end if;
7564 elsif Attribute_Name (N) = Name_Stub_Type then
7566 -- This is handled in Analyze_Attribute
7568 Analyze (N);
7570 -- All other attributes are invalid in a subtype mark
7572 else
7573 Error_Msg_N ("invalid attribute in subtype mark", N);
7574 end if;
7576 else
7577 Analyze (N);
7579 if Is_Entity_Name (N) then
7580 T_Name := Entity (N);
7581 else
7582 Error_Msg_N ("subtype mark required in this context", N);
7583 Set_Etype (N, Any_Type);
7584 return;
7585 end if;
7587 if T_Name = Any_Id or else Etype (N) = Any_Type then
7589 -- Undefined id. Make it into a valid type
7591 Set_Entity (N, Any_Type);
7593 elsif not Is_Type (T_Name)
7594 and then T_Name /= Standard_Void_Type
7595 then
7596 Error_Msg_Sloc := Sloc (T_Name);
7597 Error_Msg_N ("subtype mark required in this context", N);
7598 Error_Msg_NE ("\\found & declared#", N, T_Name);
7599 Set_Entity (N, Any_Type);
7601 else
7602 -- If the type is an incomplete type created to handle
7603 -- anonymous access components of a record type, then the
7604 -- incomplete type is the visible entity and subsequent
7605 -- references will point to it. Mark the original full
7606 -- type as referenced, to prevent spurious warnings.
7608 if Is_Incomplete_Type (T_Name)
7609 and then Present (Full_View (T_Name))
7610 and then not Comes_From_Source (T_Name)
7611 then
7612 Set_Referenced (Full_View (T_Name));
7613 end if;
7615 T_Name := Get_Full_View (T_Name);
7617 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
7618 -- limited-with clauses
7620 if From_Limited_With (T_Name)
7621 and then Ekind (T_Name) in Incomplete_Kind
7622 and then Present (Non_Limited_View (T_Name))
7623 and then Is_Interface (Non_Limited_View (T_Name))
7624 then
7625 T_Name := Non_Limited_View (T_Name);
7626 end if;
7628 if In_Open_Scopes (T_Name) then
7629 if Ekind (Base_Type (T_Name)) = E_Task_Type then
7631 -- In Ada 2005, a task name can be used in an access
7632 -- definition within its own body. It cannot be used
7633 -- in the discriminant part of the task declaration,
7634 -- nor anywhere else in the declaration because entries
7635 -- cannot have access parameters.
7637 if Ada_Version >= Ada_2005
7638 and then Nkind (Parent (N)) = N_Access_Definition
7639 then
7640 Set_Entity (N, T_Name);
7641 Set_Etype (N, T_Name);
7643 if Has_Completion (T_Name) then
7644 return;
7646 else
7647 Error_Msg_N
7648 ("task type cannot be used as type mark " &
7649 "within its own declaration", N);
7650 end if;
7652 else
7653 Error_Msg_N
7654 ("task type cannot be used as type mark " &
7655 "within its own spec or body", N);
7656 end if;
7658 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
7660 -- In Ada 2005, a protected name can be used in an access
7661 -- definition within its own body.
7663 if Ada_Version >= Ada_2005
7664 and then Nkind (Parent (N)) = N_Access_Definition
7665 then
7666 Set_Entity (N, T_Name);
7667 Set_Etype (N, T_Name);
7668 return;
7670 else
7671 Error_Msg_N
7672 ("protected type cannot be used as type mark " &
7673 "within its own spec or body", N);
7674 end if;
7676 else
7677 Error_Msg_N ("type declaration cannot refer to itself", N);
7678 end if;
7680 Set_Etype (N, Any_Type);
7681 Set_Entity (N, Any_Type);
7682 Set_Error_Posted (T_Name);
7683 return;
7684 end if;
7686 Set_Entity (N, T_Name);
7687 Set_Etype (N, T_Name);
7688 end if;
7689 end if;
7691 if Present (Etype (N)) and then Comes_From_Source (N) then
7692 if Is_Fixed_Point_Type (Etype (N)) then
7693 Check_Restriction (No_Fixed_Point, N);
7694 elsif Is_Floating_Point_Type (Etype (N)) then
7695 Check_Restriction (No_Floating_Point, N);
7696 end if;
7698 -- A Ghost type must appear in a specific context
7700 if Is_Ghost_Entity (Etype (N)) then
7701 Check_Ghost_Context (Etype (N), N);
7702 end if;
7703 end if;
7704 end Find_Type;
7706 ------------------------------------
7707 -- Has_Implicit_Character_Literal --
7708 ------------------------------------
7710 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
7711 Id : Entity_Id;
7712 Found : Boolean := False;
7713 P : constant Entity_Id := Entity (Prefix (N));
7714 Priv_Id : Entity_Id := Empty;
7716 begin
7717 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
7718 Priv_Id := First_Private_Entity (P);
7719 end if;
7721 if P = Standard_Standard then
7722 Change_Selected_Component_To_Expanded_Name (N);
7723 Rewrite (N, Selector_Name (N));
7724 Analyze (N);
7725 Set_Etype (Original_Node (N), Standard_Character);
7726 return True;
7727 end if;
7729 Id := First_Entity (P);
7730 while Present (Id) and then Id /= Priv_Id loop
7731 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
7733 -- We replace the node with the literal itself, resolve as a
7734 -- character, and set the type correctly.
7736 if not Found then
7737 Change_Selected_Component_To_Expanded_Name (N);
7738 Rewrite (N, Selector_Name (N));
7739 Analyze (N);
7740 Set_Etype (N, Id);
7741 Set_Etype (Original_Node (N), Id);
7742 Found := True;
7744 else
7745 -- More than one type derived from Character in given scope.
7746 -- Collect all possible interpretations.
7748 Add_One_Interp (N, Id, Id);
7749 end if;
7750 end if;
7752 Next_Entity (Id);
7753 end loop;
7755 return Found;
7756 end Has_Implicit_Character_Literal;
7758 ----------------------
7759 -- Has_Private_With --
7760 ----------------------
7762 function Has_Private_With (E : Entity_Id) return Boolean is
7763 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
7764 Item : Node_Id;
7766 begin
7767 Item := First (Context_Items (Comp_Unit));
7768 while Present (Item) loop
7769 if Nkind (Item) = N_With_Clause
7770 and then Private_Present (Item)
7771 and then Entity (Name (Item)) = E
7772 then
7773 return True;
7774 end if;
7776 Next (Item);
7777 end loop;
7779 return False;
7780 end Has_Private_With;
7782 ---------------------------
7783 -- Has_Implicit_Operator --
7784 ---------------------------
7786 function Has_Implicit_Operator (N : Node_Id) return Boolean is
7787 Op_Id : constant Name_Id := Chars (Selector_Name (N));
7788 P : constant Entity_Id := Entity (Prefix (N));
7789 Id : Entity_Id;
7790 Priv_Id : Entity_Id := Empty;
7792 procedure Add_Implicit_Operator
7793 (T : Entity_Id;
7794 Op_Type : Entity_Id := Empty);
7795 -- Add implicit interpretation to node N, using the type for which a
7796 -- predefined operator exists. If the operator yields a boolean type,
7797 -- the Operand_Type is implicitly referenced by the operator, and a
7798 -- reference to it must be generated.
7800 ---------------------------
7801 -- Add_Implicit_Operator --
7802 ---------------------------
7804 procedure Add_Implicit_Operator
7805 (T : Entity_Id;
7806 Op_Type : Entity_Id := Empty)
7808 Predef_Op : Entity_Id;
7810 begin
7811 Predef_Op := Current_Entity (Selector_Name (N));
7812 while Present (Predef_Op)
7813 and then Scope (Predef_Op) /= Standard_Standard
7814 loop
7815 Predef_Op := Homonym (Predef_Op);
7816 end loop;
7818 if Nkind (N) = N_Selected_Component then
7819 Change_Selected_Component_To_Expanded_Name (N);
7820 end if;
7822 -- If the context is an unanalyzed function call, determine whether
7823 -- a binary or unary interpretation is required.
7825 if Nkind (Parent (N)) = N_Indexed_Component then
7826 declare
7827 Is_Binary_Call : constant Boolean :=
7828 Present
7829 (Next (First (Expressions (Parent (N)))));
7830 Is_Binary_Op : constant Boolean :=
7831 First_Entity
7832 (Predef_Op) /= Last_Entity (Predef_Op);
7833 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
7835 begin
7836 if Is_Binary_Call then
7837 if Is_Binary_Op then
7838 Add_One_Interp (N, Predef_Op, T);
7839 else
7840 Add_One_Interp (N, Predef_Op2, T);
7841 end if;
7843 else
7844 if not Is_Binary_Op then
7845 Add_One_Interp (N, Predef_Op, T);
7846 else
7847 Add_One_Interp (N, Predef_Op2, T);
7848 end if;
7849 end if;
7850 end;
7852 else
7853 Add_One_Interp (N, Predef_Op, T);
7855 -- For operators with unary and binary interpretations, if
7856 -- context is not a call, add both
7858 if Present (Homonym (Predef_Op)) then
7859 Add_One_Interp (N, Homonym (Predef_Op), T);
7860 end if;
7861 end if;
7863 -- The node is a reference to a predefined operator, and
7864 -- an implicit reference to the type of its operands.
7866 if Present (Op_Type) then
7867 Generate_Operator_Reference (N, Op_Type);
7868 else
7869 Generate_Operator_Reference (N, T);
7870 end if;
7871 end Add_Implicit_Operator;
7873 -- Start of processing for Has_Implicit_Operator
7875 begin
7876 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
7877 Priv_Id := First_Private_Entity (P);
7878 end if;
7880 Id := First_Entity (P);
7882 case Op_Id is
7884 -- Boolean operators: an implicit declaration exists if the scope
7885 -- contains a declaration for a derived Boolean type, or for an
7886 -- array of Boolean type.
7888 when Name_Op_And
7889 | Name_Op_Not
7890 | Name_Op_Or
7891 | Name_Op_Xor
7893 while Id /= Priv_Id loop
7894 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
7895 Add_Implicit_Operator (Id);
7896 return True;
7897 end if;
7899 Next_Entity (Id);
7900 end loop;
7902 -- Equality: look for any non-limited type (result is Boolean)
7904 when Name_Op_Eq
7905 | Name_Op_Ne
7907 while Id /= Priv_Id loop
7908 if Is_Type (Id)
7909 and then not Is_Limited_Type (Id)
7910 and then Is_Base_Type (Id)
7911 then
7912 Add_Implicit_Operator (Standard_Boolean, Id);
7913 return True;
7914 end if;
7916 Next_Entity (Id);
7917 end loop;
7919 -- Comparison operators: scalar type, or array of scalar
7921 when Name_Op_Ge
7922 | Name_Op_Gt
7923 | Name_Op_Le
7924 | Name_Op_Lt
7926 while Id /= Priv_Id loop
7927 if (Is_Scalar_Type (Id)
7928 or else (Is_Array_Type (Id)
7929 and then Is_Scalar_Type (Component_Type (Id))))
7930 and then Is_Base_Type (Id)
7931 then
7932 Add_Implicit_Operator (Standard_Boolean, Id);
7933 return True;
7934 end if;
7936 Next_Entity (Id);
7937 end loop;
7939 -- Arithmetic operators: any numeric type
7941 when Name_Op_Abs
7942 | Name_Op_Add
7943 | Name_Op_Divide
7944 | Name_Op_Expon
7945 | Name_Op_Mod
7946 | Name_Op_Multiply
7947 | Name_Op_Rem
7948 | Name_Op_Subtract
7950 while Id /= Priv_Id loop
7951 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
7952 Add_Implicit_Operator (Id);
7953 return True;
7954 end if;
7956 Next_Entity (Id);
7957 end loop;
7959 -- Concatenation: any one-dimensional array type
7961 when Name_Op_Concat =>
7962 while Id /= Priv_Id loop
7963 if Is_Array_Type (Id)
7964 and then Number_Dimensions (Id) = 1
7965 and then Is_Base_Type (Id)
7966 then
7967 Add_Implicit_Operator (Id);
7968 return True;
7969 end if;
7971 Next_Entity (Id);
7972 end loop;
7974 -- What is the others condition here? Should we be using a
7975 -- subtype of Name_Id that would restrict to operators ???
7977 when others =>
7978 null;
7979 end case;
7981 -- If we fall through, then we do not have an implicit operator
7983 return False;
7984 end Has_Implicit_Operator;
7986 -----------------------------------
7987 -- Has_Loop_In_Inner_Open_Scopes --
7988 -----------------------------------
7990 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
7991 begin
7992 -- Several scope stacks are maintained by Scope_Stack. The base of the
7993 -- currently active scope stack is denoted by the Is_Active_Stack_Base
7994 -- flag in the scope stack entry. Note that the scope stacks used to
7995 -- simply be delimited implicitly by the presence of Standard_Standard
7996 -- at their base, but there now are cases where this is not sufficient
7997 -- because Standard_Standard actually may appear in the middle of the
7998 -- active set of scopes.
8000 for J in reverse 0 .. Scope_Stack.Last loop
8002 -- S was reached without seing a loop scope first
8004 if Scope_Stack.Table (J).Entity = S then
8005 return False;
8007 -- S was not yet reached, so it contains at least one inner loop
8009 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
8010 return True;
8011 end if;
8013 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8014 -- cases where Standard_Standard appears in the middle of the active
8015 -- set of scopes. This affects the declaration and overriding of
8016 -- private inherited operations in instantiations of generic child
8017 -- units.
8019 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
8020 end loop;
8022 raise Program_Error; -- unreachable
8023 end Has_Loop_In_Inner_Open_Scopes;
8025 --------------------
8026 -- In_Open_Scopes --
8027 --------------------
8029 function In_Open_Scopes (S : Entity_Id) return Boolean is
8030 begin
8031 -- Several scope stacks are maintained by Scope_Stack. The base of the
8032 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8033 -- flag in the scope stack entry. Note that the scope stacks used to
8034 -- simply be delimited implicitly by the presence of Standard_Standard
8035 -- at their base, but there now are cases where this is not sufficient
8036 -- because Standard_Standard actually may appear in the middle of the
8037 -- active set of scopes.
8039 for J in reverse 0 .. Scope_Stack.Last loop
8040 if Scope_Stack.Table (J).Entity = S then
8041 return True;
8042 end if;
8044 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8045 -- cases where Standard_Standard appears in the middle of the active
8046 -- set of scopes. This affects the declaration and overriding of
8047 -- private inherited operations in instantiations of generic child
8048 -- units.
8050 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
8051 end loop;
8053 return False;
8054 end In_Open_Scopes;
8056 -----------------------------
8057 -- Inherit_Renamed_Profile --
8058 -----------------------------
8060 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
8061 New_F : Entity_Id;
8062 Old_F : Entity_Id;
8063 Old_T : Entity_Id;
8064 New_T : Entity_Id;
8066 begin
8067 if Ekind (Old_S) = E_Operator then
8068 New_F := First_Formal (New_S);
8070 while Present (New_F) loop
8071 Set_Etype (New_F, Base_Type (Etype (New_F)));
8072 Next_Formal (New_F);
8073 end loop;
8075 Set_Etype (New_S, Base_Type (Etype (New_S)));
8077 else
8078 New_F := First_Formal (New_S);
8079 Old_F := First_Formal (Old_S);
8081 while Present (New_F) loop
8082 New_T := Etype (New_F);
8083 Old_T := Etype (Old_F);
8085 -- If the new type is a renaming of the old one, as is the case
8086 -- for actuals in instances, retain its name, to simplify later
8087 -- disambiguation.
8089 if Nkind (Parent (New_T)) = N_Subtype_Declaration
8090 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
8091 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
8092 then
8093 null;
8094 else
8095 Set_Etype (New_F, Old_T);
8096 end if;
8098 Next_Formal (New_F);
8099 Next_Formal (Old_F);
8100 end loop;
8102 pragma Assert (No (Old_F));
8104 if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then
8105 Set_Etype (New_S, Etype (Old_S));
8106 end if;
8107 end if;
8108 end Inherit_Renamed_Profile;
8110 ----------------
8111 -- Initialize --
8112 ----------------
8114 procedure Initialize is
8115 begin
8116 Urefs.Init;
8117 end Initialize;
8119 -------------------------
8120 -- Install_Use_Clauses --
8121 -------------------------
8123 procedure Install_Use_Clauses
8124 (Clause : Node_Id;
8125 Force_Installation : Boolean := False)
8127 U : Node_Id;
8129 begin
8130 U := Clause;
8131 while Present (U) loop
8133 -- Case of USE package
8135 if Nkind (U) = N_Use_Package_Clause then
8136 Use_One_Package (U, Name (U), True);
8138 -- Case of USE TYPE
8140 else
8141 Use_One_Type (Subtype_Mark (U), Force => Force_Installation);
8143 end if;
8145 Next_Use_Clause (U);
8146 end loop;
8147 end Install_Use_Clauses;
8149 -------------------------------------
8150 -- Is_Appropriate_For_Entry_Prefix --
8151 -------------------------------------
8153 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
8154 P_Type : Entity_Id := T;
8156 begin
8157 if Is_Access_Type (P_Type) then
8158 P_Type := Designated_Type (P_Type);
8159 end if;
8161 return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
8162 end Is_Appropriate_For_Entry_Prefix;
8164 -------------------------------
8165 -- Is_Appropriate_For_Record --
8166 -------------------------------
8168 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is
8170 function Has_Components (T1 : Entity_Id) return Boolean;
8171 -- Determine if given type has components (i.e. is either a record
8172 -- type or a type that has discriminants).
8174 --------------------
8175 -- Has_Components --
8176 --------------------
8178 function Has_Components (T1 : Entity_Id) return Boolean is
8179 begin
8180 return Is_Record_Type (T1)
8181 or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
8182 or else (Is_Task_Type (T1) and then Has_Discriminants (T1))
8183 or else (Is_Incomplete_Type (T1)
8184 and then From_Limited_With (T1)
8185 and then Present (Non_Limited_View (T1))
8186 and then Is_Record_Type
8187 (Get_Full_View (Non_Limited_View (T1))));
8188 end Has_Components;
8190 -- Start of processing for Is_Appropriate_For_Record
8192 begin
8193 return
8194 Present (T)
8195 and then (Has_Components (T)
8196 or else (Is_Access_Type (T)
8197 and then Has_Components (Designated_Type (T))));
8198 end Is_Appropriate_For_Record;
8200 ----------------------
8201 -- Mark_Use_Clauses --
8202 ----------------------
8204 procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is
8206 procedure Mark_Parameters (Call : Entity_Id);
8207 -- Perform use_type_clause marking for all parameters in a subprogram
8208 -- or operator call.
8210 procedure Mark_Use_Package (Pak : Entity_Id);
8211 -- Move up the Prev_Use_Clause chain for packages denoted by Pak -
8212 -- marking each clause in the chain as effective in the process.
8214 procedure Mark_Use_Type (E : Entity_Id);
8215 -- Similar to Do_Use_Package_Marking except we move up the
8216 -- Prev_Use_Clause chain for the type denoted by E.
8218 ---------------------
8219 -- Mark_Parameters --
8220 ---------------------
8222 procedure Mark_Parameters (Call : Entity_Id) is
8223 Curr : Node_Id;
8225 begin
8226 -- Move through all of the formals
8228 Curr := First_Formal (Call);
8229 while Present (Curr) loop
8230 Mark_Use_Type (Curr);
8232 Curr := Next_Formal (Curr);
8233 end loop;
8235 -- Handle the return type
8237 Mark_Use_Type (Call);
8238 end Mark_Parameters;
8240 ----------------------
8241 -- Mark_Use_Package --
8242 ----------------------
8244 procedure Mark_Use_Package (Pak : Entity_Id) is
8245 Curr : Node_Id;
8247 begin
8248 -- Ignore cases where the scope of the type is not a package
8249 -- (e.g. Standard_Standard).
8251 if Ekind (Pak) /= E_Package then
8252 return;
8253 end if;
8255 Curr := Current_Use_Clause (Pak);
8256 while Present (Curr)
8257 and then not Is_Effective_Use_Clause (Curr)
8258 loop
8259 -- We need to mark the previous use clauses as effective, but each
8260 -- use clause may in turn render other use_package_clauses
8261 -- effective. Additionally, it is possible to have a parent
8262 -- package renamed as a child of itself so we must check the
8263 -- prefix entity is not the same as the package we are marking.
8265 if Nkind (Name (Curr)) /= N_Identifier
8266 and then Present (Prefix (Name (Curr)))
8267 and then Entity (Prefix (Name (Curr))) /= Pak
8268 then
8269 Mark_Use_Package (Entity (Prefix (Name (Curr))));
8271 -- It is also possible to have a child package without a prefix
8272 -- that relies on a previous use_package_clause.
8274 elsif Nkind (Name (Curr)) = N_Identifier
8275 and then Is_Child_Unit (Entity (Name (Curr)))
8276 then
8277 Mark_Use_Package (Scope (Entity (Name (Curr))));
8278 end if;
8280 -- Mark the use_package_clause as effective and move up the chain
8282 Set_Is_Effective_Use_Clause (Curr);
8284 Curr := Prev_Use_Clause (Curr);
8285 end loop;
8286 end Mark_Use_Package;
8288 -------------------
8289 -- Mark_Use_Type --
8290 -------------------
8292 procedure Mark_Use_Type (E : Entity_Id) is
8293 Curr : Node_Id;
8295 begin
8296 -- Ignore void types and unresolved string literals and primitives
8298 if Nkind (E) = N_String_Literal
8299 or else Nkind (Etype (E)) not in N_Entity
8300 or else not Is_Type (Etype (E))
8301 then
8302 return;
8303 end if;
8305 -- The package containing the type or operator function being used
8306 -- may be in use as well, so mark any use_package_clauses for it as
8307 -- effective. There are also additional sanity checks performed here
8308 -- for ignoring previous errors.
8310 Mark_Use_Package (Scope (Base_Type (Etype (E))));
8311 if Nkind (E) in N_Op
8312 and then Present (Entity (E))
8313 and then Present (Scope (Entity (E)))
8314 then
8315 Mark_Use_Package (Scope (Entity (E)));
8316 end if;
8318 Curr := Current_Use_Clause (Base_Type (Etype (E)));
8319 while Present (Curr)
8320 and then not Is_Effective_Use_Clause (Curr)
8321 loop
8322 -- Current use_type_clause may render other use_package_clauses
8323 -- effective.
8325 if Nkind (Subtype_Mark (Curr)) /= N_Identifier
8326 and then Present (Prefix (Subtype_Mark (Curr)))
8327 then
8328 Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr))));
8329 end if;
8331 -- Mark the use_type_clause as effective and move up the chain
8333 Set_Is_Effective_Use_Clause (Curr);
8335 Curr := Prev_Use_Clause (Curr);
8336 end loop;
8337 end Mark_Use_Type;
8339 -- Start of processing for Mark_Use_Clauses
8341 begin
8342 -- Use clauses in and of themselves do not count as a "use" of a
8343 -- package.
8345 if Nkind_In (Parent (Id), N_Use_Type_Clause, N_Use_Package_Clause) then
8346 return;
8347 end if;
8349 -- Handle entities
8351 if Nkind (Id) in N_Entity then
8353 -- Mark the entity's package
8355 if Is_Potentially_Use_Visible (Id) then
8356 Mark_Use_Package (Scope (Id));
8357 end if;
8359 -- Mark enumeration literals
8361 if Ekind (Id) = E_Enumeration_Literal then
8362 Mark_Use_Type (Id);
8364 -- Mark primitives
8366 elsif (Ekind (Id) in Overloadable_Kind
8367 or else Ekind_In
8368 (Ekind (Id), E_Generic_Function, E_Generic_Procedure))
8369 and then (Is_Potentially_Use_Visible (Id)
8370 or else Is_Intrinsic_Subprogram (Id))
8371 then
8372 Mark_Parameters (Id);
8373 end if;
8375 -- Handle nodes
8377 else
8378 -- Mark operators
8380 if Nkind (Id) in N_Op then
8382 -- At this point the left operand may not be resolved if we are
8383 -- encountering multiple operators next to eachother in an
8384 -- expression.
8386 if Nkind (Id) in N_Binary_Op
8387 and then not (Nkind (Left_Opnd (Id)) in N_Op)
8388 then
8389 Mark_Use_Type (Left_Opnd (Id));
8390 end if;
8392 Mark_Use_Type (Right_Opnd (Id));
8393 Mark_Use_Type (Id);
8395 -- Mark entity identifiers
8397 elsif Nkind (Id) in N_Has_Entity
8398 and then (Is_Potentially_Use_Visible (Entity (Id))
8399 or else (Is_Generic_Instance (Entity (Id))
8400 and then Is_Immediately_Visible (Entity (Id))))
8401 then
8402 -- Ignore fully qualified names as they do not count as a "use" of
8403 -- a package.
8405 if Nkind_In (Id, N_Identifier, N_Operator_Symbol)
8406 or else (Present (Prefix (Id))
8407 and then Scope (Entity (Id)) /= Entity (Prefix (Id)))
8408 then
8409 Mark_Use_Clauses (Entity (Id));
8410 end if;
8411 end if;
8412 end if;
8413 end Mark_Use_Clauses;
8415 --------------------------------
8416 -- Most_Descendant_Use_Clause --
8417 --------------------------------
8419 function Most_Descendant_Use_Clause
8420 (Clause1 : Entity_Id;
8421 Clause2 : Entity_Id) return Entity_Id
8423 Scope1, Scope2 : Entity_Id;
8425 begin
8426 if Clause1 = Clause2 then
8427 return Clause1;
8428 end if;
8430 -- We determine which one is the most descendant by the scope distance
8431 -- to the ultimate parent unit.
8433 Scope1 := Entity_Of_Unit (Unit (Parent (Clause1)));
8434 Scope2 := Entity_Of_Unit (Unit (Parent (Clause2)));
8435 while Scope1 /= Standard_Standard
8436 and then Scope2 /= Standard_Standard
8437 loop
8438 Scope1 := Scope (Scope1);
8439 Scope2 := Scope (Scope2);
8441 if not Present (Scope1) then
8442 return Clause1;
8443 elsif not Present (Scope2) then
8444 return Clause2;
8445 end if;
8446 end loop;
8448 if Scope1 = Standard_Standard then
8449 return Clause1;
8450 end if;
8452 return Clause2;
8453 end Most_Descendant_Use_Clause;
8455 ---------------
8456 -- Pop_Scope --
8457 ---------------
8459 procedure Pop_Scope is
8460 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8461 S : constant Entity_Id := SST.Entity;
8463 begin
8464 if Debug_Flag_E then
8465 Write_Info;
8466 end if;
8468 -- Set Default_Storage_Pool field of the library unit if necessary
8470 if Ekind_In (S, E_Package, E_Generic_Package)
8471 and then
8472 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
8473 then
8474 declare
8475 Aux : constant Node_Id :=
8476 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
8477 begin
8478 if No (Default_Storage_Pool (Aux)) then
8479 Set_Default_Storage_Pool (Aux, Default_Pool);
8480 end if;
8481 end;
8482 end if;
8484 Scope_Suppress := SST.Save_Scope_Suppress;
8485 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
8486 Check_Policy_List := SST.Save_Check_Policy_List;
8487 Default_Pool := SST.Save_Default_Storage_Pool;
8488 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
8489 SPARK_Mode := SST.Save_SPARK_Mode;
8490 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
8491 Default_SSO := SST.Save_Default_SSO;
8492 Uneval_Old := SST.Save_Uneval_Old;
8494 if Debug_Flag_W then
8495 Write_Str ("<-- exiting scope: ");
8496 Write_Name (Chars (Current_Scope));
8497 Write_Str (", Depth=");
8498 Write_Int (Int (Scope_Stack.Last));
8499 Write_Eol;
8500 end if;
8502 End_Use_Clauses (SST.First_Use_Clause);
8504 -- If the actions to be wrapped are still there they will get lost
8505 -- causing incomplete code to be generated. It is better to abort in
8506 -- this case (and we do the abort even with assertions off since the
8507 -- penalty is incorrect code generation).
8509 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
8510 raise Program_Error;
8511 end if;
8513 -- Free last subprogram name if allocated, and pop scope
8515 Free (SST.Last_Subprogram_Name);
8516 Scope_Stack.Decrement_Last;
8517 end Pop_Scope;
8519 ----------------
8520 -- Push_Scope --
8521 ----------------
8523 procedure Push_Scope (S : Entity_Id) is
8524 E : constant Entity_Id := Scope (S);
8526 begin
8527 if Ekind (S) = E_Void then
8528 null;
8530 -- Set scope depth if not a non-concurrent type, and we have not yet set
8531 -- the scope depth. This means that we have the first occurrence of the
8532 -- scope, and this is where the depth is set.
8534 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
8535 and then not Scope_Depth_Set (S)
8536 then
8537 if S = Standard_Standard then
8538 Set_Scope_Depth_Value (S, Uint_0);
8540 elsif Is_Child_Unit (S) then
8541 Set_Scope_Depth_Value (S, Uint_1);
8543 elsif not Is_Record_Type (Current_Scope) then
8544 if Ekind (S) = E_Loop then
8545 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
8546 else
8547 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
8548 end if;
8549 end if;
8550 end if;
8552 Scope_Stack.Increment_Last;
8554 declare
8555 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8557 begin
8558 SST.Entity := S;
8559 SST.Save_Scope_Suppress := Scope_Suppress;
8560 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
8561 SST.Save_Check_Policy_List := Check_Policy_List;
8562 SST.Save_Default_Storage_Pool := Default_Pool;
8563 SST.Save_No_Tagged_Streams := No_Tagged_Streams;
8564 SST.Save_SPARK_Mode := SPARK_Mode;
8565 SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma;
8566 SST.Save_Default_SSO := Default_SSO;
8567 SST.Save_Uneval_Old := Uneval_Old;
8569 -- Each new scope pushed onto the scope stack inherits the component
8570 -- alignment of the previous scope. This emulates the "visibility"
8571 -- semantics of pragma Component_Alignment.
8573 if Scope_Stack.Last > Scope_Stack.First then
8574 SST.Component_Alignment_Default :=
8575 Scope_Stack.Table
8576 (Scope_Stack.Last - 1). Component_Alignment_Default;
8578 -- Otherwise, this is the first scope being pushed on the scope
8579 -- stack. Inherit the component alignment from the configuration
8580 -- form of pragma Component_Alignment (if any).
8582 else
8583 SST.Component_Alignment_Default :=
8584 Configuration_Component_Alignment;
8585 end if;
8587 SST.Last_Subprogram_Name := null;
8588 SST.Is_Transient := False;
8589 SST.Node_To_Be_Wrapped := Empty;
8590 SST.Pending_Freeze_Actions := No_List;
8591 SST.Actions_To_Be_Wrapped := (others => No_List);
8592 SST.First_Use_Clause := Empty;
8593 SST.Is_Active_Stack_Base := False;
8594 SST.Previous_Visibility := False;
8595 SST.Locked_Shared_Objects := No_Elist;
8596 end;
8598 if Debug_Flag_W then
8599 Write_Str ("--> new scope: ");
8600 Write_Name (Chars (Current_Scope));
8601 Write_Str (", Id=");
8602 Write_Int (Int (Current_Scope));
8603 Write_Str (", Depth=");
8604 Write_Int (Int (Scope_Stack.Last));
8605 Write_Eol;
8606 end if;
8608 -- Deal with copying flags from the previous scope to this one. This is
8609 -- not necessary if either scope is standard, or if the new scope is a
8610 -- child unit.
8612 if S /= Standard_Standard
8613 and then Scope (S) /= Standard_Standard
8614 and then not Is_Child_Unit (S)
8615 then
8616 if Nkind (E) not in N_Entity then
8617 return;
8618 end if;
8620 -- Copy categorization flags from Scope (S) to S, this is not done
8621 -- when Scope (S) is Standard_Standard since propagation is from
8622 -- library unit entity inwards. Copy other relevant attributes as
8623 -- well (Discard_Names in particular).
8625 -- We only propagate inwards for library level entities,
8626 -- inner level subprograms do not inherit the categorization.
8628 if Is_Library_Level_Entity (S) then
8629 Set_Is_Preelaborated (S, Is_Preelaborated (E));
8630 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
8631 Set_Discard_Names (S, Discard_Names (E));
8632 Set_Suppress_Value_Tracking_On_Call
8633 (S, Suppress_Value_Tracking_On_Call (E));
8634 Set_Categorization_From_Scope (E => S, Scop => E);
8635 end if;
8636 end if;
8638 if Is_Child_Unit (S)
8639 and then Present (E)
8640 and then Ekind_In (E, E_Package, E_Generic_Package)
8641 and then
8642 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
8643 then
8644 declare
8645 Aux : constant Node_Id :=
8646 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
8647 begin
8648 if Present (Default_Storage_Pool (Aux)) then
8649 Default_Pool := Default_Storage_Pool (Aux);
8650 end if;
8651 end;
8652 end if;
8653 end Push_Scope;
8655 ---------------------
8656 -- Premature_Usage --
8657 ---------------------
8659 procedure Premature_Usage (N : Node_Id) is
8660 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
8661 E : Entity_Id := Entity (N);
8663 begin
8664 -- Within an instance, the analysis of the actual for a formal object
8665 -- does not see the name of the object itself. This is significant only
8666 -- if the object is an aggregate, where its analysis does not do any
8667 -- name resolution on component associations. (see 4717-008). In such a
8668 -- case, look for the visible homonym on the chain.
8670 if In_Instance and then Present (Homonym (E)) then
8671 E := Homonym (E);
8672 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
8673 E := Homonym (E);
8674 end loop;
8676 if Present (E) then
8677 Set_Entity (N, E);
8678 Set_Etype (N, Etype (E));
8679 return;
8680 end if;
8681 end if;
8683 if Kind = N_Component_Declaration then
8684 Error_Msg_N
8685 ("component&! cannot be used before end of record declaration", N);
8687 elsif Kind = N_Parameter_Specification then
8688 Error_Msg_N
8689 ("formal parameter&! cannot be used before end of specification",
8692 elsif Kind = N_Discriminant_Specification then
8693 Error_Msg_N
8694 ("discriminant&! cannot be used before end of discriminant part",
8697 elsif Kind = N_Procedure_Specification
8698 or else Kind = N_Function_Specification
8699 then
8700 Error_Msg_N
8701 ("subprogram&! cannot be used before end of its declaration",
8704 elsif Kind = N_Full_Type_Declaration then
8705 Error_Msg_N
8706 ("type& cannot be used before end of its declaration!", N);
8708 else
8709 Error_Msg_N
8710 ("object& cannot be used before end of its declaration!", N);
8712 -- If the premature reference appears as the expression in its own
8713 -- declaration, rewrite it to prevent compiler loops in subsequent
8714 -- uses of this mangled declaration in address clauses.
8716 if Nkind (Parent (N)) = N_Object_Declaration then
8717 Set_Entity (N, Any_Id);
8718 end if;
8719 end if;
8720 end Premature_Usage;
8722 ------------------------
8723 -- Present_System_Aux --
8724 ------------------------
8726 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
8727 Loc : Source_Ptr;
8728 Aux_Name : Unit_Name_Type;
8729 Unum : Unit_Number_Type;
8730 Withn : Node_Id;
8731 With_Sys : Node_Id;
8732 The_Unit : Node_Id;
8734 function Find_System (C_Unit : Node_Id) return Entity_Id;
8735 -- Scan context clause of compilation unit to find with_clause
8736 -- for System.
8738 -----------------
8739 -- Find_System --
8740 -----------------
8742 function Find_System (C_Unit : Node_Id) return Entity_Id is
8743 With_Clause : Node_Id;
8745 begin
8746 With_Clause := First (Context_Items (C_Unit));
8747 while Present (With_Clause) loop
8748 if (Nkind (With_Clause) = N_With_Clause
8749 and then Chars (Name (With_Clause)) = Name_System)
8750 and then Comes_From_Source (With_Clause)
8751 then
8752 return With_Clause;
8753 end if;
8755 Next (With_Clause);
8756 end loop;
8758 return Empty;
8759 end Find_System;
8761 -- Start of processing for Present_System_Aux
8763 begin
8764 -- The child unit may have been loaded and analyzed already
8766 if Present (System_Aux_Id) then
8767 return True;
8769 -- If no previous pragma for System.Aux, nothing to load
8771 elsif No (System_Extend_Unit) then
8772 return False;
8774 -- Use the unit name given in the pragma to retrieve the unit.
8775 -- Verify that System itself appears in the context clause of the
8776 -- current compilation. If System is not present, an error will
8777 -- have been reported already.
8779 else
8780 With_Sys := Find_System (Cunit (Current_Sem_Unit));
8782 The_Unit := Unit (Cunit (Current_Sem_Unit));
8784 if No (With_Sys)
8785 and then
8786 (Nkind (The_Unit) = N_Package_Body
8787 or else (Nkind (The_Unit) = N_Subprogram_Body
8788 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
8789 then
8790 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
8791 end if;
8793 if No (With_Sys) and then Present (N) then
8795 -- If we are compiling a subunit, we need to examine its
8796 -- context as well (Current_Sem_Unit is the parent unit);
8798 The_Unit := Parent (N);
8799 while Nkind (The_Unit) /= N_Compilation_Unit loop
8800 The_Unit := Parent (The_Unit);
8801 end loop;
8803 if Nkind (Unit (The_Unit)) = N_Subunit then
8804 With_Sys := Find_System (The_Unit);
8805 end if;
8806 end if;
8808 if No (With_Sys) then
8809 return False;
8810 end if;
8812 Loc := Sloc (With_Sys);
8813 Get_Name_String (Chars (Expression (System_Extend_Unit)));
8814 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
8815 Name_Buffer (1 .. 7) := "system.";
8816 Name_Buffer (Name_Len + 8) := '%';
8817 Name_Buffer (Name_Len + 9) := 's';
8818 Name_Len := Name_Len + 9;
8819 Aux_Name := Name_Find;
8821 Unum :=
8822 Load_Unit
8823 (Load_Name => Aux_Name,
8824 Required => False,
8825 Subunit => False,
8826 Error_Node => With_Sys);
8828 if Unum /= No_Unit then
8829 Semantics (Cunit (Unum));
8830 System_Aux_Id :=
8831 Defining_Entity (Specification (Unit (Cunit (Unum))));
8833 Withn :=
8834 Make_With_Clause (Loc,
8835 Name =>
8836 Make_Expanded_Name (Loc,
8837 Chars => Chars (System_Aux_Id),
8838 Prefix => New_Occurrence_Of (Scope (System_Aux_Id), Loc),
8839 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
8841 Set_Entity (Name (Withn), System_Aux_Id);
8843 Set_Library_Unit (Withn, Cunit (Unum));
8844 Set_Corresponding_Spec (Withn, System_Aux_Id);
8845 Set_First_Name (Withn, True);
8846 Set_Implicit_With (Withn, True);
8848 Insert_After (With_Sys, Withn);
8849 Mark_Rewrite_Insertion (Withn);
8850 Set_Context_Installed (Withn);
8852 return True;
8854 -- Here if unit load failed
8856 else
8857 Error_Msg_Name_1 := Name_System;
8858 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
8859 Error_Msg_N
8860 ("extension package `%.%` does not exist",
8861 Opt.System_Extend_Unit);
8862 return False;
8863 end if;
8864 end if;
8865 end Present_System_Aux;
8867 -------------------------
8868 -- Restore_Scope_Stack --
8869 -------------------------
8871 procedure Restore_Scope_Stack
8872 (List : Elist_Id;
8873 Handle_Use : Boolean := True)
8875 SS_Last : constant Int := Scope_Stack.Last;
8876 Elmt : Elmt_Id;
8878 begin
8879 -- Restore visibility of previous scope stack, if any, using the list
8880 -- we saved (we use Remove, since this list will not be used again).
8882 loop
8883 Elmt := Last_Elmt (List);
8884 exit when Elmt = No_Elmt;
8885 Set_Is_Immediately_Visible (Node (Elmt));
8886 Remove_Last_Elmt (List);
8887 end loop;
8889 -- Restore use clauses
8891 if SS_Last >= Scope_Stack.First
8892 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
8893 and then Handle_Use
8894 then
8895 Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause,
8896 Force_Installation => True);
8897 end if;
8898 end Restore_Scope_Stack;
8900 ----------------------
8901 -- Save_Scope_Stack --
8902 ----------------------
8904 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
8905 -- consuming any memory. That is, Save_Scope_Stack took care of removing
8906 -- from immediate visibility entities and Restore_Scope_Stack took care
8907 -- of restoring their visibility analyzing the context of each entity. The
8908 -- problem of such approach is that it was fragile and caused unexpected
8909 -- visibility problems, and indeed one test was found where there was a
8910 -- real problem.
8912 -- Furthermore, the following experiment was carried out:
8914 -- - Save_Scope_Stack was modified to store in an Elist1 all those
8915 -- entities whose attribute Is_Immediately_Visible is modified
8916 -- from True to False.
8918 -- - Restore_Scope_Stack was modified to store in another Elist2
8919 -- all the entities whose attribute Is_Immediately_Visible is
8920 -- modified from False to True.
8922 -- - Extra code was added to verify that all the elements of Elist1
8923 -- are found in Elist2
8925 -- This test shows that there may be more occurrences of this problem which
8926 -- have not yet been detected. As a result, we replaced that approach by
8927 -- the current one in which Save_Scope_Stack returns the list of entities
8928 -- whose visibility is changed, and that list is passed to Restore_Scope_
8929 -- Stack to undo that change. This approach is simpler and safer, although
8930 -- it consumes more memory.
8932 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
8933 Result : constant Elist_Id := New_Elmt_List;
8934 E : Entity_Id;
8935 S : Entity_Id;
8936 SS_Last : constant Int := Scope_Stack.Last;
8938 procedure Remove_From_Visibility (E : Entity_Id);
8939 -- If E is immediately visible then append it to the result and remove
8940 -- it temporarily from visibility.
8942 ----------------------------
8943 -- Remove_From_Visibility --
8944 ----------------------------
8946 procedure Remove_From_Visibility (E : Entity_Id) is
8947 begin
8948 if Is_Immediately_Visible (E) then
8949 Append_Elmt (E, Result);
8950 Set_Is_Immediately_Visible (E, False);
8951 end if;
8952 end Remove_From_Visibility;
8954 -- Start of processing for Save_Scope_Stack
8956 begin
8957 if SS_Last >= Scope_Stack.First
8958 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
8959 then
8960 if Handle_Use then
8961 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
8962 end if;
8964 -- If the call is from within a compilation unit, as when called from
8965 -- Rtsfind, make current entries in scope stack invisible while we
8966 -- analyze the new unit.
8968 for J in reverse 0 .. SS_Last loop
8969 exit when Scope_Stack.Table (J).Entity = Standard_Standard
8970 or else No (Scope_Stack.Table (J).Entity);
8972 S := Scope_Stack.Table (J).Entity;
8974 Remove_From_Visibility (S);
8976 E := First_Entity (S);
8977 while Present (E) loop
8978 Remove_From_Visibility (E);
8979 Next_Entity (E);
8980 end loop;
8981 end loop;
8983 end if;
8985 return Result;
8986 end Save_Scope_Stack;
8988 -------------
8989 -- Set_Use --
8990 -------------
8992 procedure Set_Use (L : List_Id) is
8993 Decl : Node_Id;
8995 begin
8996 if Present (L) then
8997 Decl := First (L);
8998 while Present (Decl) loop
8999 if Nkind (Decl) = N_Use_Package_Clause then
9000 Chain_Use_Clause (Decl);
9001 Use_One_Package (Decl, Name (Decl));
9003 elsif Nkind (Decl) = N_Use_Type_Clause then
9004 Chain_Use_Clause (Decl);
9005 Use_One_Type (Subtype_Mark (Decl));
9007 end if;
9009 Next (Decl);
9010 end loop;
9011 end if;
9012 end Set_Use;
9014 -----------------------------
9015 -- Update_Use_Clause_Chain --
9016 -----------------------------
9018 procedure Update_Use_Clause_Chain is
9020 procedure Update_Chain_In_Scope (Level : Int);
9021 -- Iterate through one level in the scope stack verifying each use-type
9022 -- clause within said level is used then reset the Current_Use_Clause
9023 -- to a redundant use clause outside of the current ending scope if such
9024 -- a clause exists.
9026 ---------------------------
9027 -- Update_Chain_In_Scope --
9028 ---------------------------
9030 procedure Update_Chain_In_Scope (Level : Int) is
9031 Curr : Node_Id;
9032 N : Node_Id;
9034 begin
9035 -- Loop through all use clauses within the scope dictated by Level
9037 Curr := Scope_Stack.Table (Level).First_Use_Clause;
9038 while Present (Curr) loop
9040 -- Retrieve the subtype mark or name within the current current
9041 -- use clause.
9043 if Nkind (Curr) = N_Use_Type_Clause then
9044 N := Subtype_Mark (Curr);
9045 else
9046 N := Name (Curr);
9047 end if;
9049 -- If warnings for unreferenced entities are enabled and the
9050 -- current use clause has not been marked effective.
9052 if Check_Unreferenced
9053 and then Comes_From_Source (Curr)
9054 and then not Is_Effective_Use_Clause (Curr)
9055 and then not In_Instance
9056 then
9058 -- We are dealing with a potentially unused use_package_clause
9060 if Nkind (Curr) = N_Use_Package_Clause then
9062 -- Renamings and formal subprograms may cause the associated
9063 -- to be marked as effective instead of the original.
9065 if not (Present (Associated_Node (N))
9066 and then Present
9067 (Current_Use_Clause (Associated_Node (N)))
9068 and then Is_Effective_Use_Clause
9069 (Current_Use_Clause (Associated_Node (N))))
9070 then
9071 Error_Msg_Node_1 := Entity (N);
9072 Error_Msg_NE ("ineffective use clause for package &?",
9073 Curr, Entity (N));
9074 end if;
9076 -- We are dealing with an unused use_type_clause
9078 else
9079 Error_Msg_Node_1 := Etype (N);
9080 Error_Msg_NE ("ineffective use clause for }?",
9081 Curr, Etype (N));
9082 end if;
9083 end if;
9085 -- Verify that we haven't already processed a redundant
9086 -- use_type_clause within the same scope before we move the
9087 -- current use clause up to a previous one for type T.
9089 if Present (Prev_Use_Clause (Curr)) then
9090 Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr));
9091 end if;
9093 Curr := Next_Use_Clause (Curr);
9094 end loop;
9095 end Update_Chain_In_Scope;
9097 -- Start of processing for Update_Use_Clause_Chain
9099 begin
9100 Update_Chain_In_Scope (Scope_Stack.Last);
9102 -- Deal with use clauses within the context area if the current
9103 -- scope is a compilation unit.
9105 if Is_Compilation_Unit (Current_Scope) then
9107 pragma Assert (Scope_Stack.Last /= Scope_Stack.First);
9109 Update_Chain_In_Scope (Scope_Stack.Last - 1);
9110 end if;
9111 end Update_Use_Clause_Chain;
9113 ---------------------
9114 -- Use_One_Package --
9115 ---------------------
9117 procedure Use_One_Package
9118 (N : Node_Id;
9119 Pack_Name : Entity_Id := Empty;
9120 Force : Boolean := False)
9123 procedure Note_Redundant_Use (Clause : Node_Id);
9124 -- Mark the name in a use clause as redundant if the corresponding
9125 -- entity is already use-visible. Emit a warning if the use clause comes
9126 -- from source and the proper warnings are enabled.
9128 ------------------------
9129 -- Note_Redundant_Use --
9130 ------------------------
9132 procedure Note_Redundant_Use (Clause : Node_Id) is
9133 Pack_Name : constant Entity_Id := Entity (Clause);
9134 Decl : constant Node_Id := Parent (Clause);
9136 Cur_Use : Node_Id := Current_Use_Clause (Pack_Name);
9137 Prev_Use : Node_Id := Empty;
9138 Redundant : Node_Id := Empty;
9139 -- The Use_Clause which is actually redundant. In the simplest case
9140 -- it is Pack itself, but when we compile a body we install its
9141 -- context before that of its spec, in which case it is the
9142 -- use_clause in the spec that will appear to be redundant, and we
9143 -- want the warning to be placed on the body. Similar complications
9144 -- appear when the redundancy is between a child unit and one of its
9145 -- ancestors.
9147 begin
9148 -- Could be renamed...
9150 if No (Cur_Use) then
9151 Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name));
9152 end if;
9154 Set_Redundant_Use (Clause, True);
9156 if not Comes_From_Source (Clause)
9157 or else In_Instance
9158 or else not Warn_On_Redundant_Constructs
9159 then
9160 return;
9161 end if;
9163 if not Is_Compilation_Unit (Current_Scope) then
9165 -- If the use_clause is in an inner scope, it is made redundant by
9166 -- some clause in the current context, with one exception: If we
9167 -- are compiling a nested package body, and the use_clause comes
9168 -- from then corresponding spec, the clause is not necessarily
9169 -- fully redundant, so we should not warn. If a warning was
9170 -- warranted, it would have been given when the spec was
9171 -- processed.
9173 if Nkind (Parent (Decl)) = N_Package_Specification then
9174 declare
9175 Package_Spec_Entity : constant Entity_Id :=
9176 Defining_Unit_Name (Parent (Decl));
9177 begin
9178 if In_Package_Body (Package_Spec_Entity) then
9179 return;
9180 end if;
9181 end;
9182 end if;
9184 Redundant := Clause;
9185 Prev_Use := Cur_Use;
9187 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9188 declare
9189 Cur_Unit : constant Unit_Number_Type :=
9190 Get_Source_Unit (Cur_Use);
9191 New_Unit : constant Unit_Number_Type :=
9192 Get_Source_Unit (Clause);
9193 Scop : Entity_Id;
9195 begin
9196 if Cur_Unit = New_Unit then
9198 -- Redundant clause in same body
9200 Redundant := Clause;
9201 Prev_Use := Cur_Use;
9203 elsif Cur_Unit = Current_Sem_Unit then
9205 -- If the new clause is not in the current unit it has been
9206 -- analyzed first, and it makes the other one redundant.
9207 -- However, if the new clause appears in a subunit, Cur_Unit
9208 -- is still the parent, and in that case the redundant one
9209 -- is the one appearing in the subunit.
9211 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
9212 Redundant := Clause;
9213 Prev_Use := Cur_Use;
9215 -- Most common case: redundant clause in body,
9216 -- original clause in spec. Current scope is spec entity.
9218 elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then
9219 Redundant := Cur_Use;
9220 Prev_Use := Clause;
9222 else
9223 -- The new clause may appear in an unrelated unit, when
9224 -- the parents of a generic are being installed prior to
9225 -- instantiation. In this case there must be no warning.
9226 -- We detect this case by checking whether the current
9227 -- top of the stack is related to the current
9228 -- compilation.
9230 Scop := Current_Scope;
9231 while Present (Scop)
9232 and then Scop /= Standard_Standard
9233 loop
9234 if Is_Compilation_Unit (Scop)
9235 and then not Is_Child_Unit (Scop)
9236 then
9237 return;
9239 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
9240 exit;
9241 end if;
9243 Scop := Scope (Scop);
9244 end loop;
9246 Redundant := Cur_Use;
9247 Prev_Use := Clause;
9248 end if;
9250 elsif New_Unit = Current_Sem_Unit then
9251 Redundant := Clause;
9252 Prev_Use := Cur_Use;
9254 else
9255 -- Neither is the current unit, so they appear in parent or
9256 -- sibling units. Warning will be emitted elsewhere.
9258 return;
9259 end if;
9260 end;
9262 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
9263 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
9264 then
9265 -- Use_clause is in child unit of current unit, and the child unit
9266 -- appears in the context of the body of the parent, so it has
9267 -- been installed first, even though it is the redundant one.
9268 -- Depending on their placement in the context, the visible or the
9269 -- private parts of the two units, either might appear as
9270 -- redundant, but the message has to be on the current unit.
9272 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
9273 Redundant := Cur_Use;
9274 Prev_Use := Clause;
9275 else
9276 Redundant := Clause;
9277 Prev_Use := Cur_Use;
9278 end if;
9280 -- If the new use clause appears in the private part of a parent
9281 -- unit it may appear to be redundant w.r.t. a use clause in a
9282 -- child unit, but the previous use clause was needed in the
9283 -- visible part of the child, and no warning should be emitted.
9285 if Nkind (Parent (Decl)) = N_Package_Specification
9286 and then
9287 List_Containing (Decl) = Private_Declarations (Parent (Decl))
9288 then
9289 declare
9290 Par : constant Entity_Id := Defining_Entity (Parent (Decl));
9291 Spec : constant Node_Id :=
9292 Specification (Unit (Cunit (Current_Sem_Unit)));
9294 begin
9295 if Is_Compilation_Unit (Par)
9296 and then Par /= Cunit_Entity (Current_Sem_Unit)
9297 and then Parent (Cur_Use) = Spec
9298 and then
9299 List_Containing (Cur_Use) = Visible_Declarations (Spec)
9300 then
9301 return;
9302 end if;
9303 end;
9304 end if;
9306 -- Finally, if the current use clause is in the context then
9307 -- the clause is redundant when it is nested within the unit.
9309 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
9310 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
9311 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
9312 then
9313 Redundant := Clause;
9314 Prev_Use := Cur_Use;
9316 end if;
9318 if Present (Redundant) and then Parent (Redundant) /= Prev_Use then
9319 -- Make sure we are looking at most-descendant use_package_clause
9320 -- by traversing the chain with Find_Most_Prev and then verifying
9321 -- there is no scope manipulation via Most_Descendant_Use_Clause.
9323 if Nkind (Prev_Use) = N_Use_Package_Clause
9324 and then
9325 (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit
9326 or else Most_Descendant_Use_Clause
9327 (Prev_Use, Find_Most_Prev (Prev_Use)) /= Prev_Use)
9328 then
9329 Prev_Use := Find_Most_Prev (Prev_Use);
9330 end if;
9332 Error_Msg_Sloc := Sloc (Prev_Use);
9333 Error_Msg_NE -- CODEFIX
9334 ("& is already use-visible through previous use clause #??",
9335 Redundant, Pack_Name);
9336 end if;
9337 end Note_Redundant_Use;
9339 -- Local variables
9341 Id : Entity_Id;
9342 Prev : Entity_Id;
9343 Current_Instance : Entity_Id := Empty;
9344 Real_P : Entity_Id;
9345 Private_With_OK : Boolean := False;
9346 P : Entity_Id;
9348 -- Start of processing for Use_One_Package
9350 begin
9351 -- Use_One_Package may have been called recursively to handle an
9352 -- implicit use for a auxiliary system package, so set P accordingly
9353 -- and skip redundancy checks.
9355 if No (Pack_Name) and then Present_System_Aux (N) then
9356 P := System_Aux_Id;
9358 -- Check for redundant use_package_clauses
9360 else
9361 -- Ignore cases where we are dealing with a non user defined package
9362 -- like Standard_Standard or something other than a valid package.
9364 if not Is_Entity_Name (Pack_Name)
9365 or else No (Entity (Pack_Name))
9366 or else Ekind (Entity (Pack_Name)) /= E_Package
9367 then
9368 return;
9369 end if;
9371 -- When a renaming exists we must check it for redundancy. The
9372 -- original package would have already been seen at this point.
9374 if Present (Renamed_Object (Entity (Pack_Name))) then
9375 P := Renamed_Object (Entity (Pack_Name));
9376 else
9377 P := Entity (Pack_Name);
9378 end if;
9380 -- Check for redundant clauses then set the current use clause for
9381 -- P if were are not "forcing" an installation from a scope
9382 -- reinstallation that is done throughout analysis for various
9383 -- reasons.
9385 if In_Use (P) then
9386 Note_Redundant_Use (Pack_Name);
9387 if not Force then
9388 Set_Current_Use_Clause (P, N);
9389 end if;
9390 return;
9392 -- Warn about detected redundant clauses
9394 elsif In_Open_Scopes (P) and not Force then
9395 if Warn_On_Redundant_Constructs and then P = Current_Scope then
9396 Error_Msg_NE -- CODEFIX
9397 ("& is already use-visible within itself?r?",
9398 Pack_Name, P);
9399 end if;
9400 return;
9401 end if;
9403 -- Set P back to the non-renamed package so that visiblilty of the
9404 -- entities within the package can be properly set below.
9406 P := Entity (Pack_Name);
9407 end if;
9409 Set_In_Use (P);
9410 Set_Current_Use_Clause (P, N);
9412 -- Ada 2005 (AI-50217): Check restriction
9414 if From_Limited_With (P) then
9415 Error_Msg_N ("limited withed package cannot appear in use clause", N);
9416 end if;
9418 -- Find enclosing instance, if any
9420 if In_Instance then
9421 Current_Instance := Current_Scope;
9422 while not Is_Generic_Instance (Current_Instance) loop
9423 Current_Instance := Scope (Current_Instance);
9424 end loop;
9426 if No (Hidden_By_Use_Clause (N)) then
9427 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
9428 end if;
9429 end if;
9431 -- If unit is a package renaming, indicate that the renamed
9432 -- package is also in use (the flags on both entities must
9433 -- remain consistent, and a subsequent use of either of them
9434 -- should be recognized as redundant).
9436 if Present (Renamed_Object (P)) then
9437 Set_In_Use (Renamed_Object (P));
9438 Set_Current_Use_Clause (Renamed_Object (P), N);
9439 Real_P := Renamed_Object (P);
9440 else
9441 Real_P := P;
9442 end if;
9444 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
9445 -- found in the private part of a package specification
9447 if In_Private_Part (Current_Scope)
9448 and then Has_Private_With (P)
9449 and then Is_Child_Unit (Current_Scope)
9450 and then Is_Child_Unit (P)
9451 and then Is_Ancestor_Package (Scope (Current_Scope), P)
9452 then
9453 Private_With_OK := True;
9454 end if;
9456 -- Loop through entities in one package making them potentially
9457 -- use-visible.
9459 Id := First_Entity (P);
9460 while Present (Id)
9461 and then (Id /= First_Private_Entity (P)
9462 or else Private_With_OK) -- Ada 2005 (AI-262)
9463 loop
9464 Prev := Current_Entity (Id);
9465 while Present (Prev) loop
9466 if Is_Immediately_Visible (Prev)
9467 and then (not Is_Overloadable (Prev)
9468 or else not Is_Overloadable (Id)
9469 or else (Type_Conformant (Id, Prev)))
9470 then
9471 if No (Current_Instance) then
9473 -- Potentially use-visible entity remains hidden
9475 goto Next_Usable_Entity;
9477 -- A use clause within an instance hides outer global entities,
9478 -- which are not used to resolve local entities in the
9479 -- instance. Note that the predefined entities in Standard
9480 -- could not have been hidden in the generic by a use clause,
9481 -- and therefore remain visible. Other compilation units whose
9482 -- entities appear in Standard must be hidden in an instance.
9484 -- To determine whether an entity is external to the instance
9485 -- we compare the scope depth of its scope with that of the
9486 -- current instance. However, a generic actual of a subprogram
9487 -- instance is declared in the wrapper package but will not be
9488 -- hidden by a use-visible entity. similarly, an entity that is
9489 -- declared in an enclosing instance will not be hidden by an
9490 -- an entity declared in a generic actual, which can only have
9491 -- been use-visible in the generic and will not have hidden the
9492 -- entity in the generic parent.
9494 -- If Id is called Standard, the predefined package with the
9495 -- same name is in the homonym chain. It has to be ignored
9496 -- because it has no defined scope (being the only entity in
9497 -- the system with this mandated behavior).
9499 elsif not Is_Hidden (Id)
9500 and then Present (Scope (Prev))
9501 and then not Is_Wrapper_Package (Scope (Prev))
9502 and then Scope_Depth (Scope (Prev)) <
9503 Scope_Depth (Current_Instance)
9504 and then (Scope (Prev) /= Standard_Standard
9505 or else Sloc (Prev) > Standard_Location)
9506 then
9507 if In_Open_Scopes (Scope (Prev))
9508 and then Is_Generic_Instance (Scope (Prev))
9509 and then Present (Associated_Formal_Package (P))
9510 then
9511 null;
9513 else
9514 Set_Is_Potentially_Use_Visible (Id);
9515 Set_Is_Immediately_Visible (Prev, False);
9516 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
9517 end if;
9518 end if;
9520 -- A user-defined operator is not use-visible if the predefined
9521 -- operator for the type is immediately visible, which is the case
9522 -- if the type of the operand is in an open scope. This does not
9523 -- apply to user-defined operators that have operands of different
9524 -- types, because the predefined mixed mode operations (multiply
9525 -- and divide) apply to universal types and do not hide anything.
9527 elsif Ekind (Prev) = E_Operator
9528 and then Operator_Matches_Spec (Prev, Id)
9529 and then In_Open_Scopes
9530 (Scope (Base_Type (Etype (First_Formal (Id)))))
9531 and then (No (Next_Formal (First_Formal (Id)))
9532 or else Etype (First_Formal (Id)) =
9533 Etype (Next_Formal (First_Formal (Id)))
9534 or else Chars (Prev) = Name_Op_Expon)
9535 then
9536 goto Next_Usable_Entity;
9538 -- In an instance, two homonyms may become use_visible through the
9539 -- actuals of distinct formal packages. In the generic, only the
9540 -- current one would have been visible, so make the other one
9541 -- not use_visible.
9543 elsif Present (Current_Instance)
9544 and then Is_Potentially_Use_Visible (Prev)
9545 and then not Is_Overloadable (Prev)
9546 and then Scope (Id) /= Scope (Prev)
9547 and then Used_As_Generic_Actual (Scope (Prev))
9548 and then Used_As_Generic_Actual (Scope (Id))
9549 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
9550 Current_Use_Clause (Scope (Id)))
9551 then
9552 Set_Is_Potentially_Use_Visible (Prev, False);
9553 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
9554 end if;
9556 Prev := Homonym (Prev);
9557 end loop;
9559 -- On exit, we know entity is not hidden, unless it is private
9561 if not Is_Hidden (Id)
9562 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
9563 then
9564 Set_Is_Potentially_Use_Visible (Id);
9566 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
9567 Set_Is_Potentially_Use_Visible (Full_View (Id));
9568 end if;
9569 end if;
9571 <<Next_Usable_Entity>>
9572 Next_Entity (Id);
9573 end loop;
9575 -- Child units are also made use-visible by a use clause, but they may
9576 -- appear after all visible declarations in the parent entity list.
9578 while Present (Id) loop
9579 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
9580 Set_Is_Potentially_Use_Visible (Id);
9581 end if;
9583 Next_Entity (Id);
9584 end loop;
9586 if Chars (Real_P) = Name_System
9587 and then Scope (Real_P) = Standard_Standard
9588 and then Present_System_Aux (N)
9589 then
9590 Use_One_Package (N);
9591 end if;
9592 end Use_One_Package;
9594 ------------------
9595 -- Use_One_Type --
9596 ------------------
9598 procedure Use_One_Type
9599 (Id : Node_Id; Installed : Boolean := False; Force : Boolean := False)
9601 Elmt : Elmt_Id;
9602 Is_Known_Used : Boolean;
9603 Op_List : Elist_Id;
9604 T : Entity_Id;
9606 function Spec_Reloaded_For_Body return Boolean;
9607 -- Determine whether the compilation unit is a package body and the use
9608 -- type clause is in the spec of the same package. Even though the spec
9609 -- was analyzed first, its context is reloaded when analysing the body.
9611 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
9612 -- AI05-150: if the use_type_clause carries the "all" qualifier,
9613 -- class-wide operations of ancestor types are use-visible if the
9614 -- ancestor type is visible.
9616 ----------------------------
9617 -- Spec_Reloaded_For_Body --
9618 ----------------------------
9620 function Spec_Reloaded_For_Body return Boolean is
9621 begin
9622 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9623 declare
9624 Spec : constant Node_Id :=
9625 Parent (List_Containing (Parent (Id)));
9627 begin
9628 -- Check whether type is declared in a package specification,
9629 -- and current unit is the corresponding package body. The
9630 -- use clauses themselves may be within a nested package.
9632 return
9633 Nkind (Spec) = N_Package_Specification
9634 and then
9635 In_Same_Source_Unit (Corresponding_Body (Parent (Spec)),
9636 Cunit_Entity (Current_Sem_Unit));
9637 end;
9638 end if;
9640 return False;
9641 end Spec_Reloaded_For_Body;
9643 -------------------------------
9644 -- Use_Class_Wide_Operations --
9645 -------------------------------
9647 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
9648 Scop : Entity_Id;
9649 Ent : Entity_Id;
9651 function Is_Class_Wide_Operation_Of
9652 (Op : Entity_Id;
9653 T : Entity_Id) return Boolean;
9654 -- Determine whether a subprogram has a class-wide parameter or
9655 -- result that is T'Class.
9657 ---------------------------------
9658 -- Is_Class_Wide_Operation_Of --
9659 ---------------------------------
9661 function Is_Class_Wide_Operation_Of
9662 (Op : Entity_Id;
9663 T : Entity_Id) return Boolean
9665 Formal : Entity_Id;
9667 begin
9668 Formal := First_Formal (Op);
9669 while Present (Formal) loop
9670 if Etype (Formal) = Class_Wide_Type (T) then
9671 return True;
9672 end if;
9673 Next_Formal (Formal);
9674 end loop;
9676 if Etype (Op) = Class_Wide_Type (T) then
9677 return True;
9678 end if;
9680 return False;
9681 end Is_Class_Wide_Operation_Of;
9683 -- Start of processing for Use_Class_Wide_Operations
9685 begin
9686 Scop := Scope (Typ);
9687 if not Is_Hidden (Scop) then
9688 Ent := First_Entity (Scop);
9689 while Present (Ent) loop
9690 if Is_Overloadable (Ent)
9691 and then Is_Class_Wide_Operation_Of (Ent, Typ)
9692 and then not Is_Potentially_Use_Visible (Ent)
9693 then
9694 Set_Is_Potentially_Use_Visible (Ent);
9695 Append_Elmt (Ent, Used_Operations (Parent (Id)));
9696 end if;
9698 Next_Entity (Ent);
9699 end loop;
9700 end if;
9702 if Is_Derived_Type (Typ) then
9703 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
9704 end if;
9705 end Use_Class_Wide_Operations;
9707 -- Start of processing for Use_One_Type
9709 begin
9710 if Entity (Id) = Any_Type then
9711 return;
9712 end if;
9714 -- It is the type determined by the subtype mark (8.4(8)) whose
9715 -- operations become potentially use-visible.
9717 T := Base_Type (Entity (Id));
9719 -- Either the type itself is used, the package where it is declared is
9720 -- in use or the entity is declared in the current package, thus
9721 -- use-visible.
9723 Is_Known_Used := (In_Use (T)
9724 and then ((Present (Current_Use_Clause (T))
9725 and then All_Present
9726 (Current_Use_Clause (T)))
9727 or else not All_Present (Parent (Id))))
9728 or else In_Use (Scope (T))
9729 or else Scope (T) = Current_Scope;
9731 Set_Redundant_Use (Id,
9732 Is_Known_Used or else Is_Potentially_Use_Visible (T));
9734 if Ekind (T) = E_Incomplete_Type then
9735 Error_Msg_N ("premature usage of incomplete type", Id);
9737 elsif In_Open_Scopes (Scope (T)) then
9738 null;
9740 -- A limited view cannot appear in a use_type_clause. However, an access
9741 -- type whose designated type is limited has the flag but is not itself
9742 -- a limited view unless we only have a limited view of its enclosing
9743 -- package.
9745 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
9746 Error_Msg_N
9747 ("incomplete type from limited view cannot appear in use clause",
9748 Id);
9750 -- If the use clause is redundant, Used_Operations will usually be
9751 -- empty, but we need to set it to empty here in one case: If we are
9752 -- instantiating a generic library unit, then we install the ancestors
9753 -- of that unit in the scope stack, which involves reprocessing use
9754 -- clauses in those ancestors. Such a use clause will typically have a
9755 -- nonempty Used_Operations unless it was redundant in the generic unit,
9756 -- even if it is redundant at the place of the instantiation.
9758 elsif Redundant_Use (Id) then
9760 -- We must avoid incorrectly setting the Current_Use_Clause when we
9761 -- are working with a redundant clause that has already been linked
9762 -- in the Prev_Use_Clause chain, otherwise the chain will break.
9764 if Present (Current_Use_Clause (T))
9765 and then Present (Prev_Use_Clause (Current_Use_Clause (T)))
9766 and then Parent (Id) = Prev_Use_Clause (Current_Use_Clause (T))
9767 then
9768 null;
9769 else
9770 Set_Current_Use_Clause (T, Parent (Id));
9771 end if;
9773 Set_Used_Operations (Parent (Id), New_Elmt_List);
9775 -- If the subtype mark designates a subtype in a different package,
9776 -- we have to check that the parent type is visible, otherwise the
9777 -- use_type_clause is a no-op. Not clear how to do that???
9779 else
9780 Set_Current_Use_Clause (T, Parent (Id));
9781 Set_In_Use (T);
9783 -- If T is tagged, primitive operators on class-wide operands
9784 -- are also available.
9786 if Is_Tagged_Type (T) then
9787 Set_In_Use (Class_Wide_Type (T));
9788 end if;
9790 -- Iterate over primitive operations of the type. If an operation is
9791 -- already use_visible, it is the result of a previous use_clause,
9792 -- and already appears on the corresponding entity chain. If the
9793 -- clause is being reinstalled, operations are already use-visible.
9795 if Installed then
9796 null;
9798 else
9799 Op_List := Collect_Primitive_Operations (T);
9800 Elmt := First_Elmt (Op_List);
9801 while Present (Elmt) loop
9802 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
9803 or else Chars (Node (Elmt)) in Any_Operator_Name)
9804 and then not Is_Hidden (Node (Elmt))
9805 and then not Is_Potentially_Use_Visible (Node (Elmt))
9806 then
9807 Set_Is_Potentially_Use_Visible (Node (Elmt));
9808 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
9810 elsif Ada_Version >= Ada_2012
9811 and then All_Present (Parent (Id))
9812 and then not Is_Hidden (Node (Elmt))
9813 and then not Is_Potentially_Use_Visible (Node (Elmt))
9814 then
9815 Set_Is_Potentially_Use_Visible (Node (Elmt));
9816 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
9817 end if;
9819 Next_Elmt (Elmt);
9820 end loop;
9821 end if;
9823 if Ada_Version >= Ada_2012
9824 and then All_Present (Parent (Id))
9825 and then Is_Tagged_Type (T)
9826 then
9827 Use_Class_Wide_Operations (T);
9828 end if;
9829 end if;
9831 -- If warning on redundant constructs, check for unnecessary WITH
9833 if not Force
9834 and then Warn_On_Redundant_Constructs
9835 and then Is_Known_Used
9837 -- with P; with P; use P;
9838 -- package P is package X is package body X is
9839 -- type T ... use P.T;
9841 -- The compilation unit is the body of X. GNAT first compiles the
9842 -- spec of X, then proceeds to the body. At that point P is marked
9843 -- as use visible. The analysis then reinstalls the spec along with
9844 -- its context. The use clause P.T is now recognized as redundant,
9845 -- but in the wrong context. Do not emit a warning in such cases.
9846 -- Do not emit a warning either if we are in an instance, there is
9847 -- no redundancy between an outer use_clause and one that appears
9848 -- within the generic.
9850 and then not Spec_Reloaded_For_Body
9851 and then not In_Instance
9852 then
9853 -- The type already has a use clause
9855 if In_Use (T) then
9857 -- Case where we know the current use clause for the type
9859 if Present (Current_Use_Clause (T)) then
9860 Use_Clause_Known : declare
9861 Clause1 : constant Node_Id := Find_Most_Prev
9862 (Current_Use_Clause (T));
9863 Clause2 : constant Node_Id := Parent (Id);
9864 Ent1 : Entity_Id;
9865 Ent2 : Entity_Id;
9866 Err_No : Node_Id;
9867 Unit1 : Node_Id;
9868 Unit2 : Node_Id;
9870 -- Start of processing for Use_Clause_Known
9872 begin
9873 -- If both current use_type_clause and the use_type_clause
9874 -- for the type are at the compilation unit level, one of
9875 -- the units must be an ancestor of the other, and the
9876 -- warning belongs on the descendant.
9878 if Nkind (Parent (Clause1)) = N_Compilation_Unit
9879 and then
9880 Nkind (Parent (Clause2)) = N_Compilation_Unit
9881 then
9882 -- If the unit is a subprogram body that acts as spec,
9883 -- the context clause is shared with the constructed
9884 -- subprogram spec. Clearly there is no redundancy.
9886 if Clause1 = Clause2 then
9887 return;
9888 end if;
9890 Unit1 := Unit (Parent (Clause1));
9891 Unit2 := Unit (Parent (Clause2));
9893 -- If both clauses are on same unit, or one is the body
9894 -- of the other, or one of them is in a subunit, report
9895 -- redundancy on the later one.
9897 if Unit1 = Unit2 or else Nkind (Unit1) = N_Subunit then
9898 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
9899 Error_Msg_NE -- CODEFIX
9900 ("& is already use-visible through previous "
9901 & "use_type_clause #??", Clause1, T);
9902 return;
9904 elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body)
9905 and then Nkind (Unit1) /= Nkind (Unit2)
9906 and then Nkind (Unit1) /= N_Subunit
9907 then
9908 Error_Msg_Sloc := Sloc (Clause1);
9909 Error_Msg_NE -- CODEFIX
9910 ("& is already use-visible through previous "
9911 & "use_type_clause #??", Current_Use_Clause (T), T);
9912 return;
9913 end if;
9915 -- There is a redundant use_type_clause in a child unit.
9916 -- Determine which of the units is more deeply nested.
9917 -- If a unit is a package instance, retrieve the entity
9918 -- and its scope from the instance spec.
9920 Ent1 := Entity_Of_Unit (Unit1);
9921 Ent2 := Entity_Of_Unit (Unit2);
9923 if Scope (Ent2) = Standard_Standard then
9924 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
9925 Err_No := Clause1;
9927 elsif Scope (Ent1) = Standard_Standard then
9928 Error_Msg_Sloc := Sloc (Id);
9929 Err_No := Clause2;
9931 -- If both units are child units, we determine which one
9932 -- is the descendant by the scope distance to the
9933 -- ultimate parent unit.
9935 else
9936 declare
9937 S1, S2 : Entity_Id;
9939 begin
9940 S1 := Scope (Ent1);
9941 S2 := Scope (Ent2);
9942 while Present (S1)
9943 and then Present (S2)
9944 and then S1 /= Standard_Standard
9945 and then S2 /= Standard_Standard
9946 loop
9947 S1 := Scope (S1);
9948 S2 := Scope (S2);
9949 end loop;
9951 if S1 = Standard_Standard then
9952 Error_Msg_Sloc := Sloc (Id);
9953 Err_No := Clause2;
9954 else
9955 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
9956 Err_No := Clause1;
9957 end if;
9958 end;
9959 end if;
9961 if Parent (Id) /= Err_No then
9962 if Most_Descendant_Use_Clause
9963 (Err_No, Parent (Id)) = Parent (Id)
9964 then
9965 Error_Msg_Sloc := Sloc (Err_No);
9966 Err_No := Parent (Id);
9967 end if;
9969 Error_Msg_NE -- CODEFIX
9970 ("& is already use-visible through previous "
9971 & "use_type_clause #??", Err_No, Id);
9972 end if;
9974 -- Case where current use_type_clause and use_type_clause
9975 -- for the type are not both at the compilation unit level.
9976 -- In this case we don't have location information.
9978 else
9979 Error_Msg_NE -- CODEFIX
9980 ("& is already use-visible through previous "
9981 & "use type clause??", Id, T);
9982 end if;
9983 end Use_Clause_Known;
9985 -- Here if Current_Use_Clause is not set for T, another case
9986 -- where we do not have the location information available.
9988 else
9989 Error_Msg_NE -- CODEFIX
9990 ("& is already use-visible through previous "
9991 & "use type clause??", Id, T);
9992 end if;
9994 -- The package where T is declared is already used
9996 elsif In_Use (Scope (T)) then
9997 Error_Msg_Sloc := Sloc (Find_Most_Prev
9998 (Current_Use_Clause (Scope (T))));
9999 Error_Msg_NE -- CODEFIX
10000 ("& is already use-visible through package use clause #??",
10001 Id, T);
10003 -- The current scope is the package where T is declared
10005 else
10006 Error_Msg_Node_2 := Scope (T);
10007 Error_Msg_NE -- CODEFIX
10008 ("& is already use-visible inside package &??", Id, T);
10009 end if;
10010 end if;
10011 end Use_One_Type;
10013 ----------------
10014 -- Write_Info --
10015 ----------------
10017 procedure Write_Info is
10018 Id : Entity_Id := First_Entity (Current_Scope);
10020 begin
10021 -- No point in dumping standard entities
10023 if Current_Scope = Standard_Standard then
10024 return;
10025 end if;
10027 Write_Str ("========================================================");
10028 Write_Eol;
10029 Write_Str (" Defined Entities in ");
10030 Write_Name (Chars (Current_Scope));
10031 Write_Eol;
10032 Write_Str ("========================================================");
10033 Write_Eol;
10035 if No (Id) then
10036 Write_Str ("-- none --");
10037 Write_Eol;
10039 else
10040 while Present (Id) loop
10041 Write_Entity_Info (Id, " ");
10042 Next_Entity (Id);
10043 end loop;
10044 end if;
10046 if Scope (Current_Scope) = Standard_Standard then
10048 -- Print information on the current unit itself
10050 Write_Entity_Info (Current_Scope, " ");
10051 end if;
10053 Write_Eol;
10054 end Write_Info;
10056 --------
10057 -- ws --
10058 --------
10060 procedure ws is
10061 S : Entity_Id;
10062 begin
10063 for J in reverse 1 .. Scope_Stack.Last loop
10064 S := Scope_Stack.Table (J).Entity;
10065 Write_Int (Int (S));
10066 Write_Str (" === ");
10067 Write_Name (Chars (S));
10068 Write_Eol;
10069 end loop;
10070 end ws;
10072 --------
10073 -- we --
10074 --------
10076 procedure we (S : Entity_Id) is
10077 E : Entity_Id;
10078 begin
10079 E := First_Entity (S);
10080 while Present (E) loop
10081 Write_Int (Int (E));
10082 Write_Str (" === ");
10083 Write_Name (Chars (E));
10084 Write_Eol;
10085 Next_Entity (E);
10086 end loop;
10087 end we;
10088 end Sem_Ch8;