ada: Fix renaming of predefined equality operator for unchecked union types
[official-gcc.git] / gcc / ada / sem_ch8.adb
blob6e0db366db8912f0ced6efbbd673b7882b22a4bf
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-2023, 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 Einfo.Entities; use Einfo.Entities;
30 with Einfo.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Impunit; use Impunit;
39 with Lib; use Lib;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Namet.Sp; use Namet.Sp;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Output; use Output;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch4; use Sem_Ch4;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch12; use Sem_Ch12;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Dim; use Sem_Dim;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Elab; use Sem_Elab;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Prag; use Sem_Prag;
66 with Sem_Res; use Sem_Res;
67 with Sem_Util; use Sem_Util;
68 with Sem_Type; use Sem_Type;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Sinfo.Nodes; use Sinfo.Nodes;
72 with Sinfo.Utils; use Sinfo.Utils;
73 with Sinfo.CN; use Sinfo.CN;
74 with Snames; use Snames;
75 with Style;
76 with Table;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Warnsw; use Warnsw;
81 package body Sem_Ch8 is
83 ------------------------------------
84 -- Visibility and Name Resolution --
85 ------------------------------------
87 -- This package handles name resolution and the collection of possible
88 -- interpretations for overloaded names, prior to overload resolution.
90 -- Name resolution is the process that establishes a mapping between source
91 -- identifiers and the entities they denote at each point in the program.
92 -- Each entity is represented by a defining occurrence. Each identifier
93 -- that denotes an entity points to the corresponding defining occurrence.
94 -- This is the entity of the applied occurrence. Each occurrence holds
95 -- an index into the names table, where source identifiers are stored.
97 -- Each entry in the names table for an identifier or designator uses the
98 -- Info pointer to hold a link to the currently visible entity that has
99 -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
100 -- in package Sem_Util). The visibility is initialized at the beginning of
101 -- semantic processing to make entities in package Standard immediately
102 -- visible. The visibility table is used in a more subtle way when
103 -- compiling subunits (see below).
105 -- Entities that have the same name (i.e. homonyms) are chained. In the
106 -- case of overloaded entities, this chain holds all the possible meanings
107 -- of a given identifier. The process of overload resolution uses type
108 -- information to select from this chain the unique meaning of a given
109 -- identifier.
111 -- Entities are also chained in their scope, through the Next_Entity link.
112 -- As a consequence, the name space is organized as a sparse matrix, where
113 -- each row corresponds to a scope, and each column to a source identifier.
114 -- Open scopes, that is to say scopes currently being compiled, have their
115 -- corresponding rows of entities in order, innermost scope first.
117 -- The scopes of packages that are mentioned in context clauses appear in
118 -- no particular order, interspersed among open scopes. This is because
119 -- in the course of analyzing the context of a compilation, a package
120 -- declaration is first an open scope, and subsequently an element of the
121 -- context. If subunits or child units are present, a parent unit may
122 -- appear under various guises at various times in the compilation.
124 -- When the compilation of the innermost scope is complete, the entities
125 -- defined therein are no longer visible. If the scope is not a package
126 -- declaration, these entities are never visible subsequently, and can be
127 -- removed from visibility chains. If the scope is a package declaration,
128 -- its visible declarations may still be accessible. Therefore the entities
129 -- defined in such a scope are left on the visibility chains, and only
130 -- their visibility (immediately visibility or potential use-visibility)
131 -- is affected.
133 -- The ordering of homonyms on their chain does not necessarily follow
134 -- the order of their corresponding scopes on the scope stack. For
135 -- example, if package P and the enclosing scope both contain entities
136 -- named E, then when compiling the package body the chain for E will
137 -- hold the global entity first, and the local one (corresponding to
138 -- the current inner scope) next. As a result, name resolution routines
139 -- do not assume any relative ordering of the homonym chains, either
140 -- for scope nesting or to order of appearance of context clauses.
142 -- When compiling a child unit, entities in the parent scope are always
143 -- immediately visible. When compiling the body of a child unit, private
144 -- entities in the parent must also be made immediately visible. There
145 -- are separate routines to make the visible and private declarations
146 -- visible at various times (see package Sem_Ch7).
148 -- +--------+ +-----+
149 -- | In use |-------->| EU1 |-------------------------->
150 -- +--------+ +-----+
151 -- | |
152 -- +--------+ +-----+ +-----+
153 -- | Stand. |---------------->| ES1 |--------------->| ES2 |--->
154 -- +--------+ +-----+ +-----+
155 -- | |
156 -- +---------+ | +-----+
157 -- | with'ed |------------------------------>| EW2 |--->
158 -- +---------+ | +-----+
159 -- | |
160 -- +--------+ +-----+ +-----+
161 -- | Scope2 |---------------->| E12 |--------------->| E22 |--->
162 -- +--------+ +-----+ +-----+
163 -- | |
164 -- +--------+ +-----+ +-----+
165 -- | Scope1 |---------------->| E11 |--------------->| E12 |--->
166 -- +--------+ +-----+ +-----+
167 -- ^ | |
168 -- | | |
169 -- | +---------+ | |
170 -- | | with'ed |----------------------------------------->
171 -- | +---------+ | |
172 -- | | |
173 -- Scope stack | |
174 -- (innermost first) | |
175 -- +----------------------------+
176 -- Names table => | Id1 | | | | Id2 |
177 -- +----------------------------+
179 -- Name resolution must deal with several syntactic forms: simple names,
180 -- qualified names, indexed names, and various forms of calls.
182 -- Each identifier points to an entry in the names table. The resolution
183 -- of a simple name consists in traversing the homonym chain, starting
184 -- from the names table. If an entry is immediately visible, it is the one
185 -- designated by the identifier. If only potentially use-visible entities
186 -- are on the chain, we must verify that they do not hide each other. If
187 -- the entity we find is overloadable, we collect all other overloadable
188 -- entities on the chain as long as they are not hidden.
190 -- To resolve expanded names, we must find the entity at the intersection
191 -- of the entity chain for the scope (the prefix) and the homonym chain
192 -- for the selector. In general, homonym chains will be much shorter than
193 -- entity chains, so it is preferable to start from the names table as
194 -- well. If the entity found is overloadable, we must collect all other
195 -- interpretations that are defined in the scope denoted by the prefix.
197 -- For records, protected types, and tasks, their local entities are
198 -- removed from visibility chains on exit from the corresponding scope.
199 -- From the outside, these entities are always accessed by selected
200 -- notation, and the entity chain for the record type, protected type,
201 -- etc. is traversed sequentially in order to find the designated entity.
203 -- The discriminants of a type and the operations of a protected type or
204 -- task are unchained on exit from the first view of the type, (such as
205 -- a private or incomplete type declaration, or a protected type speci-
206 -- fication) and re-chained when compiling the second view.
208 -- In the case of operators, we do not make operators on derived types
209 -- explicit. As a result, the notation P."+" may denote either a user-
210 -- defined function with name "+", or else an implicit declaration of the
211 -- operator "+" in package P. The resolution of expanded names always
212 -- tries to resolve an operator name as such an implicitly defined entity,
213 -- in addition to looking for explicit declarations.
215 -- All forms of names that denote entities (simple names, expanded names,
216 -- character literals in some cases) have a Entity attribute, which
217 -- identifies the entity denoted by the name.
219 ---------------------
220 -- The Scope Stack --
221 ---------------------
223 -- The Scope stack keeps track of the scopes currently been compiled.
224 -- Every entity that contains declarations (including records) is placed
225 -- on the scope stack while it is being processed, and removed at the end.
226 -- Whenever a non-package scope is exited, the entities defined therein
227 -- are removed from the visibility table, so that entities in outer scopes
228 -- become visible (see previous description). On entry to Sem, the scope
229 -- stack only contains the package Standard. As usual, subunits complicate
230 -- this picture ever so slightly.
232 -- The Rtsfind mechanism can force a call to Semantics while another
233 -- compilation is in progress. The unit retrieved by Rtsfind must be
234 -- compiled in its own context, and has no access to the visibility of
235 -- the unit currently being compiled. The procedures Save_Scope_Stack and
236 -- Restore_Scope_Stack make entities in current open scopes invisible
237 -- before compiling the retrieved unit, and restore the compilation
238 -- environment afterwards.
240 ------------------------
241 -- Compiling subunits --
242 ------------------------
244 -- Subunits must be compiled in the environment of the corresponding stub,
245 -- that is to say with the same visibility into the parent (and its
246 -- context) that is available at the point of the stub declaration, but
247 -- with the additional visibility provided by the context clause of the
248 -- subunit itself. As a result, compilation of a subunit forces compilation
249 -- of the parent (see description in lib-). At the point of the stub
250 -- declaration, Analyze is called recursively to compile the proper body of
251 -- the subunit, but without reinitializing the names table, nor the scope
252 -- stack (i.e. standard is not pushed on the stack). In this fashion the
253 -- context of the subunit is added to the context of the parent, and the
254 -- subunit is compiled in the correct environment. Note that in the course
255 -- of processing the context of a subunit, Standard will appear twice on
256 -- the scope stack: once for the parent of the subunit, and once for the
257 -- unit in the context clause being compiled. However, the two sets of
258 -- entities are not linked by homonym chains, so that the compilation of
259 -- any context unit happens in a fresh visibility environment.
261 -------------------------------
262 -- Processing of USE Clauses --
263 -------------------------------
265 -- Every defining occurrence has a flag indicating if it is potentially use
266 -- visible. Resolution of simple names examines this flag. The processing
267 -- of use clauses consists in setting this flag on all visible entities
268 -- defined in the corresponding package. On exit from the scope of the use
269 -- clause, the corresponding flag must be reset. However, a package may
270 -- appear in several nested use clauses (pathological but legal, alas)
271 -- which forces us to use a slightly more involved scheme:
273 -- a) The defining occurrence for a package holds a flag -In_Use- to
274 -- indicate that it is currently in the scope of a use clause. If a
275 -- redundant use clause is encountered, then the corresponding occurrence
276 -- of the package name is flagged -Redundant_Use-.
278 -- b) On exit from a scope, the use clauses in its declarative part are
279 -- scanned. The visibility flag is reset in all entities declared in
280 -- package named in a use clause, as long as the package is not flagged
281 -- as being in a redundant use clause (in which case the outer use
282 -- clause is still in effect, and the direct visibility of its entities
283 -- must be retained).
285 -- Note that entities are not removed from their homonym chains on exit
286 -- from the package specification. A subsequent use clause does not need
287 -- to rechain the visible entities, but only to establish their direct
288 -- visibility.
290 -----------------------------------
291 -- Handling private declarations --
292 -----------------------------------
294 -- The principle that each entity has a single defining occurrence clashes
295 -- with the presence of two separate definitions for private types: the
296 -- first is the private type declaration, and second is the full type
297 -- declaration. It is important that all references to the type point to
298 -- the same defining occurrence, namely the first one. To enforce the two
299 -- separate views of the entity, the corresponding information is swapped
300 -- between the two declarations. Outside of the package, the defining
301 -- occurrence only contains the private declaration information, while in
302 -- the private part and the body of the package the defining occurrence
303 -- contains the full declaration. To simplify the swap, the defining
304 -- occurrence that currently holds the private declaration points to the
305 -- full declaration. During semantic processing the defining occurrence
306 -- also points to a list of private dependents, that is to say access types
307 -- or composite types whose designated types or component types are
308 -- subtypes or derived types of the private type in question. After the
309 -- full declaration has been seen, the private dependents are updated to
310 -- indicate that they have full definitions.
312 ------------------------------------
313 -- Handling of Undefined Messages --
314 ------------------------------------
316 -- In normal mode, only the first use of an undefined identifier generates
317 -- a message. The table Urefs is used to record error messages that have
318 -- been issued so that second and subsequent ones do not generate further
319 -- messages. However, the second reference causes text to be added to the
320 -- original undefined message noting "(more references follow)". The
321 -- full error list option (-gnatf) forces messages to be generated for
322 -- every reference and disconnects the use of this table.
324 type Uref_Entry is record
325 Node : Node_Id;
326 -- Node for identifier for which original message was posted. The
327 -- Chars field of this identifier is used to detect later references
328 -- to the same identifier.
330 Err : Error_Msg_Id;
331 -- Records error message Id of original undefined message. Reset to
332 -- No_Error_Msg after the second occurrence, where it is used to add
333 -- text to the original message as described above.
335 Nvis : Boolean;
336 -- Set if the message is not visible rather than undefined
338 Loc : Source_Ptr;
339 -- Records location of error message. Used to make sure that we do
340 -- not consider a, b : undefined as two separate instances, which
341 -- would otherwise happen, since the parser converts this sequence
342 -- to a : undefined; b : undefined.
344 end record;
346 package Urefs is new Table.Table (
347 Table_Component_Type => Uref_Entry,
348 Table_Index_Type => Nat,
349 Table_Low_Bound => 1,
350 Table_Initial => 10,
351 Table_Increment => 100,
352 Table_Name => "Urefs");
354 Candidate_Renaming : Entity_Id;
355 -- Holds a candidate interpretation that appears in a subprogram renaming
356 -- declaration and does not match the given specification, but matches at
357 -- least on the first formal. Allows better error message when given
358 -- specification omits defaulted parameters, a common error.
360 -----------------------
361 -- Local Subprograms --
362 -----------------------
364 procedure Analyze_Generic_Renaming
365 (N : Node_Id;
366 K : Entity_Kind);
367 -- Common processing for all three kinds of generic renaming declarations.
368 -- Enter new name and indicate that it renames the generic unit.
370 procedure Analyze_Renamed_Character
371 (N : Node_Id;
372 New_S : Entity_Id;
373 Is_Body : Boolean);
374 -- Renamed entity is given by a character literal, which must belong
375 -- to the return type of the new entity. Is_Body indicates whether the
376 -- declaration is a renaming_as_body. If the original declaration has
377 -- already been frozen (because of an intervening body, e.g.) the body of
378 -- the function must be built now. The same applies to the following
379 -- various renaming procedures.
381 procedure Analyze_Renamed_Dereference
382 (N : Node_Id;
383 New_S : Entity_Id;
384 Is_Body : Boolean);
385 -- Renamed entity is given by an explicit dereference. Prefix must be a
386 -- conformant access_to_subprogram type.
388 procedure Analyze_Renamed_Entry
389 (N : Node_Id;
390 New_S : Entity_Id;
391 Is_Body : Boolean);
392 -- If the renamed entity in a subprogram renaming is an entry or protected
393 -- subprogram, build a body for the new entity whose only statement is a
394 -- call to the renamed entity.
396 procedure Analyze_Renamed_Family_Member
397 (N : Node_Id;
398 New_S : Entity_Id;
399 Is_Body : Boolean);
400 -- Used when the renamed entity is an indexed component. The prefix must
401 -- denote an entry family.
403 procedure Analyze_Renamed_Primitive_Operation
404 (N : Node_Id;
405 New_S : Entity_Id;
406 Is_Body : Boolean);
407 -- If the renamed entity in a subprogram renaming is a primitive operation
408 -- or a class-wide operation in prefix form, save the target object,
409 -- which must be added to the list of actuals in any subsequent call.
410 -- The renaming operation is intrinsic because the compiler must in
411 -- fact generate a wrapper for it (6.3.1 (10 1/2)).
413 procedure Attribute_Renaming (N : Node_Id);
414 -- Analyze renaming of attribute as subprogram. The renaming declaration N
415 -- is rewritten as a subprogram body that returns the attribute reference
416 -- applied to the formals of the function.
418 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
419 -- Set Entity, with style check if need be. For a discriminant reference,
420 -- replace by the corresponding discriminal, i.e. the parameter of the
421 -- initialization procedure that corresponds to the discriminant.
423 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
424 -- A renaming_as_body may occur after the entity of the original decla-
425 -- ration has been frozen. In that case, the body of the new entity must
426 -- be built now, because the usual mechanism of building the renamed
427 -- body at the point of freezing will not work. Subp is the subprogram
428 -- for which N provides the Renaming_As_Body.
430 procedure Check_In_Previous_With_Clause (N, Nam : Node_Id);
431 -- N is a use_package clause and Nam the package name, or N is a use_type
432 -- clause and Nam is the prefix of the type name. In either case, verify
433 -- that the package is visible at that point in the context: either it
434 -- appears in a previous with_clause, or because it is a fully qualified
435 -- name and the root ancestor appears in a previous with_clause.
437 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
438 -- Verify that the entity in a renaming declaration that is a library unit
439 -- is itself a library unit and not a nested unit or subunit. Also check
440 -- that if the renaming is a child unit of a generic parent, then the
441 -- renamed unit must also be a child unit of that parent. Finally, verify
442 -- that a renamed generic unit is not an implicit child declared within
443 -- an instance of the parent.
445 procedure Chain_Use_Clause (N : Node_Id);
446 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
447 -- the proper scope table entry. This is usually the current scope, but it
448 -- will be an inner scope when installing the use clauses of the private
449 -- declarations of a parent unit prior to compiling the private part of a
450 -- child unit. This chain is traversed when installing/removing use clauses
451 -- when compiling a subunit or instantiating a generic body on the fly,
452 -- when it is necessary to save and restore full environments.
454 function Enclosing_Instance return Entity_Id;
455 -- In an instance nested within another one, several semantic checks are
456 -- unnecessary because the legality of the nested instance has been checked
457 -- in the enclosing generic unit. This applies in particular to legality
458 -- checks on actuals for formal subprograms of the inner instance, which
459 -- are checked as subprogram renamings, and may be complicated by confusion
460 -- in private/full views. This function returns the instance enclosing the
461 -- current one if there is such, else it returns Empty.
463 -- If the renaming determines the entity for the default of a formal
464 -- subprogram nested within another instance, choose the innermost
465 -- candidate. This is because if the formal has a box, and we are within
466 -- an enclosing instance where some candidate interpretations are local
467 -- to this enclosing instance, we know that the default was properly
468 -- resolved when analyzing the generic, so we prefer the local
469 -- candidates to those that are external. This is not always the case
470 -- but is a reasonable heuristic on the use of nested generics. The
471 -- proper solution requires a full renaming model.
473 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
474 -- Return the appropriate entity for determining which unit has a deeper
475 -- scope: the defining entity for U, unless U is a package instance, in
476 -- which case we retrieve the entity of the instance spec.
478 procedure Error_Missing_With_Of_Known_Unit (Pkg : Node_Id);
479 -- Display an error message denoting a "with" is missing for a given known
480 -- package Pkg with its full path name.
482 procedure Find_Expanded_Name (N : Node_Id);
483 -- The input is a selected component known to be an expanded name. Verify
484 -- legality of selector given the scope denoted by prefix, and change node
485 -- N into a expanded name with a properly set Entity field.
487 function Find_First_Use (Use_Clause : Node_Id) return Node_Id;
488 -- Find the most previous use clause (that is, the first one to appear in
489 -- the source) by traversing the previous clause chain that exists in both
490 -- N_Use_Package_Clause nodes and N_Use_Type_Clause nodes.
492 function Find_Renamed_Entity
493 (N : Node_Id;
494 Nam : Node_Id;
495 New_S : Entity_Id;
496 Is_Actual : Boolean := False) return Entity_Id;
497 -- Find the renamed entity that corresponds to the given parameter profile
498 -- in a subprogram renaming declaration. The renamed entity may be an
499 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
500 -- indicates that the renaming is the one generated for an actual subpro-
501 -- gram in an instance, for which special visibility checks apply.
503 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
504 -- Find a type derived from Character or Wide_Character in the prefix of N.
505 -- Used to resolved qualified names whose selector is a character literal.
507 function Has_Private_With (E : Entity_Id) return Boolean;
508 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
509 -- private with on E.
511 function Has_Components (Typ : Entity_Id) return Boolean;
512 -- Determine if given type has components, i.e. is either a record type or
513 -- type or a type that has discriminants.
515 function Has_Implicit_Operator (N : Node_Id) return Boolean;
516 -- N is an expanded name whose selector is an operator name (e.g. P."+").
517 -- Determine if N denotes an operator implicitly declared in prefix P: P's
518 -- declarative part contains an implicit declaration of an operator if it
519 -- has a declaration of a type to which one of the predefined operators
520 -- apply. The existence of this routine is an implementation artifact. A
521 -- more straightforward but more space-consuming choice would be to make
522 -- all inherited operators explicit in the symbol table.
524 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
525 -- A subprogram defined by a renaming declaration inherits the parameter
526 -- profile of the renamed entity. The subtypes given in the subprogram
527 -- specification are discarded and replaced with those of the renamed
528 -- subprogram, which are then used to recheck the default values.
530 function Most_Descendant_Use_Clause
531 (Clause1 : Entity_Id;
532 Clause2 : Entity_Id) return Entity_Id;
533 -- Determine which use clause parameter is the most descendant in terms of
534 -- scope.
536 procedure Premature_Usage (N : Node_Id);
537 -- Diagnose usage of an entity before it is visible
539 function Is_Self_Hidden (E : Entity_Id) return Boolean;
540 -- True within a declaration if it is hidden from all visibility by itself
541 -- (see RM-8.3(16-18)). This is mostly just "not Is_Not_Self_Hidden", but
542 -- we need to check for E_Void in case of errors.
544 procedure Use_One_Package
545 (N : Node_Id;
546 Pack_Name : Entity_Id := Empty;
547 Force : Boolean := False);
548 -- Make visible entities declared in package P potentially use-visible
549 -- in the current context. Also used in the analysis of subunits, when
550 -- re-installing use clauses of parent units. N is the use_clause that
551 -- names P (and possibly other packages).
553 procedure Use_One_Type
554 (Id : Node_Id;
555 Installed : Boolean := False;
556 Force : Boolean := False);
557 -- Id is the subtype mark from a use_type_clause. This procedure makes
558 -- the primitive operators of the type potentially use-visible. The
559 -- boolean flag Installed indicates that the clause is being reinstalled
560 -- after previous analysis, and primitive operations are already chained
561 -- on the Used_Operations list of the clause.
563 procedure Write_Info;
564 -- Write debugging information on entities declared in current scope
566 --------------------------------
567 -- Analyze_Exception_Renaming --
568 --------------------------------
570 -- The language only allows a single identifier, but the tree holds an
571 -- identifier list. The parser has already issued an error message if
572 -- there is more than one element in the list.
574 procedure Analyze_Exception_Renaming (N : Node_Id) is
575 Id : constant Entity_Id := Defining_Entity (N);
576 Nam : constant Node_Id := Name (N);
578 begin
579 Enter_Name (Id);
580 Analyze (Nam);
582 Mutate_Ekind (Id, E_Exception);
583 Set_Etype (Id, Standard_Exception_Type);
584 Set_Is_Pure (Id, Is_Pure (Current_Scope));
586 if Is_Entity_Name (Nam)
587 and then Present (Entity (Nam))
588 and then Ekind (Entity (Nam)) = E_Exception
589 then
590 if Present (Renamed_Entity (Entity (Nam))) then
591 Set_Renamed_Entity (Id, Renamed_Entity (Entity (Nam)));
592 else
593 Set_Renamed_Entity (Id, Entity (Nam));
594 end if;
596 -- The exception renaming declaration may become Ghost if it renames
597 -- a Ghost entity.
599 Mark_Ghost_Renaming (N, Entity (Nam));
600 else
601 Error_Msg_N ("invalid exception name in renaming", Nam);
602 end if;
604 -- Implementation-defined aspect specifications can appear in a renaming
605 -- declaration, but not language-defined ones. The call to procedure
606 -- Analyze_Aspect_Specifications will take care of this error check.
608 if Has_Aspects (N) then
609 Analyze_Aspect_Specifications (N, Id);
610 end if;
611 end Analyze_Exception_Renaming;
613 ---------------------------
614 -- Analyze_Expanded_Name --
615 ---------------------------
617 procedure Analyze_Expanded_Name (N : Node_Id) is
618 begin
619 -- If the entity pointer is already set, this is an internal node, or a
620 -- node that is analyzed more than once, after a tree modification. In
621 -- such a case there is no resolution to perform, just set the type. In
622 -- either case, start by analyzing the prefix.
624 Analyze (Prefix (N));
626 if Present (Entity (N)) then
627 if Is_Type (Entity (N)) then
628 Set_Etype (N, Entity (N));
629 else
630 Set_Etype (N, Etype (Entity (N)));
631 end if;
633 else
634 Find_Expanded_Name (N);
635 end if;
637 -- In either case, propagate dimension of entity to expanded name
639 Analyze_Dimension (N);
640 end Analyze_Expanded_Name;
642 ---------------------------------------
643 -- Analyze_Generic_Function_Renaming --
644 ---------------------------------------
646 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
647 begin
648 Analyze_Generic_Renaming (N, E_Generic_Function);
649 end Analyze_Generic_Function_Renaming;
651 --------------------------------------
652 -- Analyze_Generic_Package_Renaming --
653 --------------------------------------
655 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
656 begin
657 -- Test for the Text_IO special unit case here, since we may be renaming
658 -- one of the subpackages of Text_IO, then join common routine.
660 Check_Text_IO_Special_Unit (Name (N));
662 Analyze_Generic_Renaming (N, E_Generic_Package);
663 end Analyze_Generic_Package_Renaming;
665 ----------------------------------------
666 -- Analyze_Generic_Procedure_Renaming --
667 ----------------------------------------
669 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
670 begin
671 Analyze_Generic_Renaming (N, E_Generic_Procedure);
672 end Analyze_Generic_Procedure_Renaming;
674 ------------------------------
675 -- Analyze_Generic_Renaming --
676 ------------------------------
678 procedure Analyze_Generic_Renaming
679 (N : Node_Id;
680 K : Entity_Kind)
682 New_P : constant Entity_Id := Defining_Entity (N);
683 Inst : Boolean := False;
684 Old_P : Entity_Id;
686 begin
687 if Name (N) = Error then
688 return;
689 end if;
691 Generate_Definition (New_P);
693 if Current_Scope /= Standard_Standard then
694 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
695 end if;
697 if Nkind (Name (N)) = N_Selected_Component then
698 Check_Generic_Child_Unit (Name (N), Inst);
699 else
700 Analyze (Name (N));
701 end if;
703 if not Is_Entity_Name (Name (N)) then
704 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
705 Old_P := Any_Id;
706 else
707 Old_P := Entity (Name (N));
708 end if;
710 Enter_Name (New_P);
711 Mutate_Ekind (New_P, K);
713 if Etype (Old_P) = Any_Type then
714 null;
716 elsif Ekind (Old_P) /= K then
717 Error_Msg_N ("invalid generic unit name", Name (N));
719 else
720 if Present (Renamed_Entity (Old_P)) then
721 Set_Renamed_Entity (New_P, Renamed_Entity (Old_P));
722 else
723 Set_Renamed_Entity (New_P, Old_P);
724 end if;
726 -- The generic renaming declaration may become Ghost if it renames a
727 -- Ghost entity.
729 Mark_Ghost_Renaming (N, Old_P);
731 Set_Is_Pure (New_P, Is_Pure (Old_P));
732 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
734 Set_Etype (New_P, Etype (Old_P));
735 Set_Has_Completion (New_P);
737 if In_Open_Scopes (Old_P) then
738 Error_Msg_N ("within its scope, generic denotes its instance", N);
739 end if;
741 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
742 -- renamings and subsequent instantiations of Unchecked_Conversion.
744 if Is_Generic_Subprogram (Old_P) then
745 Set_Is_Intrinsic_Subprogram
746 (New_P, Is_Intrinsic_Subprogram (Old_P));
747 end if;
749 Check_Library_Unit_Renaming (N, Old_P);
750 end if;
752 -- Implementation-defined aspect specifications can appear in a renaming
753 -- declaration, but not language-defined ones. The call to procedure
754 -- Analyze_Aspect_Specifications will take care of this error check.
756 if Has_Aspects (N) then
757 Analyze_Aspect_Specifications (N, New_P);
758 end if;
759 end Analyze_Generic_Renaming;
761 -----------------------------
762 -- Analyze_Object_Renaming --
763 -----------------------------
765 procedure Analyze_Object_Renaming (N : Node_Id) is
766 Id : constant Entity_Id := Defining_Identifier (N);
767 Loc : constant Source_Ptr := Sloc (N);
768 Nam : constant Node_Id := Name (N);
769 Is_Object_Ref : Boolean;
770 Dec : Node_Id;
771 T : Entity_Id;
772 T2 : Entity_Id;
773 Q : Node_Id;
775 procedure Check_Constrained_Object;
776 -- If the nominal type is unconstrained but the renamed object is
777 -- constrained, as can happen with renaming an explicit dereference or
778 -- a function return, build a constrained subtype from the object. If
779 -- the renaming is for a formal in an accept statement, the analysis
780 -- has already established its actual subtype. This is only relevant
781 -- if the renamed object is an explicit dereference.
783 function Get_Object_Name (Nod : Node_Id) return Node_Id;
784 -- Obtain the name of the object from node Nod which is being renamed by
785 -- the object renaming declaration N.
787 function Find_Raise_Node (N : Node_Id) return Traverse_Result;
788 -- Process one node in search for N_Raise_xxx_Error nodes.
789 -- Return Abandon if found, OK otherwise.
791 ---------------------
792 -- Find_Raise_Node --
793 ---------------------
795 function Find_Raise_Node (N : Node_Id) return Traverse_Result is
796 begin
797 if Nkind (N) in N_Raise_xxx_Error then
798 return Abandon;
799 else
800 return OK;
801 end if;
802 end Find_Raise_Node;
804 ------------------------
805 -- No_Raise_xxx_Error --
806 ------------------------
808 function No_Raise_xxx_Error is new Traverse_Func (Find_Raise_Node);
809 -- Traverse tree to look for a N_Raise_xxx_Error node and returns
810 -- Abandon if so and OK if none found.
812 ------------------------------
813 -- Check_Constrained_Object --
814 ------------------------------
816 procedure Check_Constrained_Object is
817 Typ : constant Entity_Id := Etype (Nam);
818 Subt : Entity_Id;
819 Loop_Scheme : Node_Id;
821 begin
822 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference
823 and then Is_Composite_Type (Typ)
824 and then not Is_Constrained (Typ)
825 and then not Has_Unknown_Discriminants (Typ)
826 and then Expander_Active
827 then
828 -- If Actual_Subtype is already set, nothing to do
830 if Ekind (Id) in E_Variable | E_Constant
831 and then Present (Actual_Subtype (Id))
832 then
833 null;
835 -- A renaming of an unchecked union has no actual subtype
837 elsif Is_Unchecked_Union (Typ) then
838 null;
840 -- If a record is limited its size is invariant. This is the case
841 -- in particular with record types with an access discriminant
842 -- that are used in iterators. This is an optimization, but it
843 -- also prevents typing anomalies when the prefix is further
844 -- expanded.
846 -- Note that we cannot just use the Is_Limited_Record flag because
847 -- it does not apply to records with limited components, for which
848 -- this syntactic flag is not set, but whose size is also fixed.
850 -- Note also that we need to build the constrained subtype for an
851 -- array in order to make the bounds explicit in most cases, but
852 -- not if the object comes from an extended return statement, as
853 -- this would create dangling references to them later on.
855 elsif Is_Limited_Type (Typ)
856 and then (not Is_Array_Type (Typ) or else Is_Return_Object (Id))
857 then
858 null;
860 else
861 Subt := Make_Temporary (Loc, 'T');
862 Remove_Side_Effects (Nam);
863 Insert_Action (N,
864 Make_Subtype_Declaration (Loc,
865 Defining_Identifier => Subt,
866 Subtype_Indication =>
867 Make_Subtype_From_Expr (Nam, Typ)));
868 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
869 Set_Etype (Nam, Subt);
871 -- Suppress discriminant checks on this subtype if the original
872 -- type has defaulted discriminants and Id is a "for of" loop
873 -- iterator.
875 if Has_Defaulted_Discriminants (Typ)
876 and then Nkind (Original_Node (Parent (N))) = N_Loop_Statement
877 then
878 Loop_Scheme := Iteration_Scheme (Original_Node (Parent (N)));
880 if Present (Loop_Scheme)
881 and then Present (Iterator_Specification (Loop_Scheme))
882 and then
883 Defining_Identifier
884 (Iterator_Specification (Loop_Scheme)) = Id
885 then
886 Set_Checks_May_Be_Suppressed (Subt);
887 Push_Local_Suppress_Stack_Entry
888 (Entity => Subt,
889 Check => Discriminant_Check,
890 Suppress => True);
891 end if;
892 end if;
894 -- Freeze subtype at once, to prevent order of elaboration
895 -- issues in the backend. The renamed object exists, so its
896 -- type is already frozen in any case.
898 Freeze_Before (N, Subt);
899 end if;
900 end if;
901 end Check_Constrained_Object;
903 ---------------------
904 -- Get_Object_Name --
905 ---------------------
907 function Get_Object_Name (Nod : Node_Id) return Node_Id is
908 Obj_Nam : Node_Id;
910 begin
911 Obj_Nam := Nod;
912 while Present (Obj_Nam) loop
913 case Nkind (Obj_Nam) is
914 when N_Attribute_Reference
915 | N_Explicit_Dereference
916 | N_Indexed_Component
917 | N_Slice
919 Obj_Nam := Prefix (Obj_Nam);
921 when N_Selected_Component =>
922 Obj_Nam := Selector_Name (Obj_Nam);
924 when N_Qualified_Expression | N_Type_Conversion =>
925 Obj_Nam := Expression (Obj_Nam);
927 when others =>
928 exit;
929 end case;
930 end loop;
932 return Obj_Nam;
933 end Get_Object_Name;
935 -- Start of processing for Analyze_Object_Renaming
937 begin
938 if Nam = Error then
939 return;
940 end if;
942 Set_Is_Pure (Id, Is_Pure (Current_Scope));
943 Enter_Name (Id);
945 -- The renaming of a component that depends on a discriminant requires
946 -- an actual subtype, because in subsequent use of the object Gigi will
947 -- be unable to locate the actual bounds. This explicit step is required
948 -- when the renaming is generated in removing side effects of an
949 -- already-analyzed expression.
951 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
953 -- The object renaming declaration may become Ghost if it renames a
954 -- Ghost entity.
956 if Is_Entity_Name (Nam) then
957 Mark_Ghost_Renaming (N, Entity (Nam));
958 end if;
960 T := Etype (Nam);
961 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
963 if Present (Dec) then
964 Insert_Action (N, Dec);
965 T := Defining_Identifier (Dec);
966 Set_Etype (Nam, T);
967 end if;
968 elsif Present (Subtype_Mark (N))
969 or else No (Access_Definition (N))
970 then
971 if Present (Subtype_Mark (N)) then
972 Find_Type (Subtype_Mark (N));
973 T := Entity (Subtype_Mark (N));
974 Analyze (Nam);
976 -- AI12-0275: Case of object renaming without a subtype_mark
978 else
979 Analyze (Nam);
981 -- Normal case of no overloading in object name
983 if not Is_Overloaded (Nam) then
985 -- Catch error cases (such as attempting to rename a procedure
986 -- or package) using the shorthand form.
988 if No (Etype (Nam))
989 or else Etype (Nam) = Standard_Void_Type
990 then
991 Error_Msg_N
992 ("object name or value expected in renaming", Nam);
994 Mutate_Ekind (Id, E_Variable);
995 Set_Etype (Id, Any_Type);
997 return;
999 else
1000 T := Etype (Nam);
1001 end if;
1003 -- Case of overloaded name, which will be illegal if there's more
1004 -- than one acceptable interpretation (such as overloaded function
1005 -- calls).
1007 else
1008 declare
1009 I : Interp_Index;
1010 I1 : Interp_Index;
1011 It : Interp;
1012 It1 : Interp;
1013 Nam1 : Entity_Id;
1015 begin
1016 -- More than one candidate interpretation is available
1018 -- Remove procedure calls, which syntactically cannot appear
1019 -- in this context, but which cannot be removed by type
1020 -- checking, because the context does not impose a type.
1022 Get_First_Interp (Nam, I, It);
1023 while Present (It.Typ) loop
1024 if It.Typ = Standard_Void_Type then
1025 Remove_Interp (I);
1026 end if;
1028 Get_Next_Interp (I, It);
1029 end loop;
1031 Get_First_Interp (Nam, I, It);
1032 I1 := I;
1033 It1 := It;
1035 -- If there's no type present, we have an error case (such
1036 -- as overloaded procedures named in the object renaming).
1038 if No (It.Typ) then
1039 Error_Msg_N
1040 ("object name or value expected in renaming", Nam);
1042 Mutate_Ekind (Id, E_Variable);
1043 Set_Etype (Id, Any_Type);
1045 return;
1046 end if;
1048 Get_Next_Interp (I, It);
1050 if Present (It.Typ) then
1051 Nam1 := It1.Nam;
1052 It1 := Disambiguate (Nam, I1, I, Any_Type);
1054 if It1 = No_Interp then
1055 Error_Msg_N ("ambiguous name in object renaming", Nam);
1057 Error_Msg_Sloc := Sloc (It.Nam);
1058 Error_Msg_N ("\\possible interpretation#!", Nam);
1060 Error_Msg_Sloc := Sloc (Nam1);
1061 Error_Msg_N ("\\possible interpretation#!", Nam);
1063 return;
1064 end if;
1065 end if;
1067 Set_Etype (Nam, It1.Typ);
1068 T := It1.Typ;
1069 end;
1070 end if;
1072 if Etype (Nam) = Standard_Exception_Type then
1073 Error_Msg_N
1074 ("exception requires a subtype mark in renaming", Nam);
1075 return;
1076 end if;
1077 end if;
1079 -- The object renaming declaration may become Ghost if it renames a
1080 -- Ghost entity.
1082 if Is_Entity_Name (Nam) then
1083 Mark_Ghost_Renaming (N, Entity (Nam));
1084 end if;
1086 -- Check against AI12-0401 here before Resolve may rewrite Nam and
1087 -- potentially generate spurious warnings.
1089 -- In the case where the object_name is a qualified_expression with
1090 -- a nominal subtype T and whose expression is a name that denotes
1091 -- an object Q:
1092 -- * if T is an elementary subtype, then:
1093 -- * Q shall be a constant other than a dereference of an access
1094 -- type; or
1095 -- * the nominal subtype of Q shall be statically compatible with
1096 -- T; or
1097 -- * T shall statically match the base subtype of its type if
1098 -- scalar, or the first subtype of its type if an access type.
1099 -- * if T is a composite subtype, then Q shall be known to be
1100 -- constrained or T shall statically match the first subtype of
1101 -- its type.
1103 if Nkind (Nam) = N_Qualified_Expression
1104 and then Is_Object_Reference (Expression (Nam))
1105 then
1106 Q := Expression (Nam);
1108 if (Is_Elementary_Type (T)
1109 and then
1110 not ((not Is_Variable (Q)
1111 and then Nkind (Q) /= N_Explicit_Dereference)
1112 or else Subtypes_Statically_Compatible (Etype (Q), T)
1113 or else (Is_Scalar_Type (T)
1114 and then Subtypes_Statically_Match
1115 (T, Base_Type (T)))
1116 or else (Is_Access_Type (T)
1117 and then Subtypes_Statically_Match
1118 (T, First_Subtype (T)))))
1119 or else (Is_Composite_Type (T)
1120 and then
1122 -- If Q is an aggregate, Is_Constrained may not be set
1123 -- yet and its type may not be resolved yet.
1124 -- This doesn't quite correspond to the complex notion
1125 -- of "known to be constrained" but this is good enough
1126 -- for a rule which is in any case too complex.
1128 not (Is_Constrained (Etype (Q))
1129 or else Nkind (Q) = N_Aggregate
1130 or else Subtypes_Statically_Match
1131 (T, First_Subtype (T))))
1132 then
1133 Error_Msg_N
1134 ("subtype of renamed qualified expression does not " &
1135 "statically match", N);
1136 return;
1137 end if;
1138 end if;
1140 Resolve (Nam, T);
1142 -- If the renamed object is a function call of a limited type,
1143 -- the expansion of the renaming is complicated by the presence
1144 -- of various temporaries and subtypes that capture constraints
1145 -- of the renamed object. Rewrite node as an object declaration,
1146 -- whose expansion is simpler. Given that the object is limited
1147 -- there is no copy involved and no performance hit.
1149 if Nkind (Nam) = N_Function_Call
1150 and then Is_Limited_View (Etype (Nam))
1151 and then not Is_Constrained (Etype (Nam))
1152 and then Comes_From_Source (N)
1153 then
1154 Set_Etype (Id, T);
1155 Mutate_Ekind (Id, E_Constant);
1156 Rewrite (N,
1157 Make_Object_Declaration (Loc,
1158 Defining_Identifier => Id,
1159 Constant_Present => True,
1160 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
1161 Expression => Relocate_Node (Nam)));
1162 return;
1163 end if;
1165 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
1166 -- when renaming declaration has a named access type. The Ada 2012
1167 -- coverage rules allow an anonymous access type in the context of
1168 -- an expected named general access type, but the renaming rules
1169 -- require the types to be the same. (An exception is when the type
1170 -- of the renaming is also an anonymous access type, which can only
1171 -- happen due to a renaming created by the expander.)
1173 if Nkind (Nam) = N_Type_Conversion
1174 and then not Comes_From_Source (Nam)
1175 and then Is_Anonymous_Access_Type (Etype (Expression (Nam)))
1176 and then not Is_Anonymous_Access_Type (T)
1177 then
1178 Error_Msg_NE
1179 ("cannot rename anonymous access object "
1180 & "as a named access type", Expression (Nam), T);
1181 end if;
1183 -- Check that a class-wide object is not being renamed as an object
1184 -- of a specific type. The test for access types is needed to exclude
1185 -- cases where the renamed object is a dynamically tagged access
1186 -- result, such as occurs in certain expansions.
1188 if Is_Tagged_Type (T) then
1189 Check_Dynamically_Tagged_Expression
1190 (Expr => Nam,
1191 Typ => T,
1192 Related_Nod => N);
1193 end if;
1195 -- Ada 2005 (AI-230/AI-254): Access renaming
1197 else pragma Assert (Present (Access_Definition (N)));
1198 T :=
1199 Access_Definition
1200 (Related_Nod => N,
1201 N => Access_Definition (N));
1203 Analyze (Nam);
1205 -- The object renaming declaration may become Ghost if it renames a
1206 -- Ghost entity.
1208 if Is_Entity_Name (Nam) then
1209 Mark_Ghost_Renaming (N, Entity (Nam));
1210 end if;
1212 -- Ada 2005 AI05-105: if the declaration has an anonymous access
1213 -- type, the renamed object must also have an anonymous type, and
1214 -- this is a name resolution rule. This was implicit in the last part
1215 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
1216 -- recent AI.
1218 if not Is_Overloaded (Nam) then
1219 if Ekind (Etype (Nam)) /= Ekind (T) then
1220 Error_Msg_N
1221 ("expect anonymous access type in object renaming", N);
1222 end if;
1224 else
1225 declare
1226 I : Interp_Index;
1227 It : Interp;
1228 Typ : Entity_Id := Empty;
1229 Seen : Boolean := False;
1231 begin
1232 Get_First_Interp (Nam, I, It);
1233 while Present (It.Typ) loop
1235 -- Renaming is ambiguous if more than one candidate
1236 -- interpretation is type-conformant with the context.
1238 if Ekind (It.Typ) = Ekind (T) then
1239 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
1240 and then
1241 Type_Conformant
1242 (Designated_Type (T), Designated_Type (It.Typ))
1243 then
1244 if not Seen then
1245 Seen := True;
1246 else
1247 Error_Msg_N
1248 ("ambiguous expression in renaming", Nam);
1249 end if;
1251 elsif Ekind (T) = E_Anonymous_Access_Type
1252 and then
1253 Covers (Designated_Type (T), Designated_Type (It.Typ))
1254 then
1255 if not Seen then
1256 Seen := True;
1257 else
1258 Error_Msg_N
1259 ("ambiguous expression in renaming", Nam);
1260 end if;
1261 end if;
1263 if Covers (T, It.Typ) then
1264 Typ := It.Typ;
1265 Set_Etype (Nam, Typ);
1266 Set_Is_Overloaded (Nam, False);
1267 end if;
1268 end if;
1270 Get_Next_Interp (I, It);
1271 end loop;
1272 end;
1273 end if;
1275 Resolve (Nam, T);
1277 -- Do not perform the legality checks below when the resolution of
1278 -- the renaming name failed because the associated type is Any_Type.
1280 if Etype (Nam) = Any_Type then
1281 null;
1283 -- Ada 2005 (AI-231): In the case where the type is defined by an
1284 -- access_definition, the renamed entity shall be of an access-to-
1285 -- constant type if and only if the access_definition defines an
1286 -- access-to-constant type. ARM 8.5.1(4)
1288 elsif Constant_Present (Access_Definition (N))
1289 and then not Is_Access_Constant (Etype (Nam))
1290 then
1291 Error_Msg_N
1292 ("(Ada 2005): the renamed object is not access-to-constant "
1293 & "(RM 8.5.1(6))", N);
1295 elsif not Constant_Present (Access_Definition (N))
1296 and then Is_Access_Constant (Etype (Nam))
1297 then
1298 Error_Msg_N
1299 ("(Ada 2005): the renamed object is not access-to-variable "
1300 & "(RM 8.5.1(6))", N);
1301 end if;
1303 if Is_Access_Subprogram_Type (Etype (Nam)) then
1304 Check_Subtype_Conformant
1305 (Designated_Type (T), Designated_Type (Etype (Nam)));
1307 elsif not Subtypes_Statically_Match
1308 (Designated_Type (T),
1309 Available_View (Designated_Type (Etype (Nam))))
1310 then
1311 Error_Msg_N
1312 ("subtype of renamed object does not statically match", N);
1313 end if;
1314 end if;
1316 -- Special processing for renaming function return object. Some errors
1317 -- and warnings are produced only for calls that come from source.
1319 if Nkind (Nam) = N_Function_Call then
1320 case Ada_Version is
1322 -- Usage is illegal in Ada 83, but renamings are also introduced
1323 -- during expansion, and error does not apply to those.
1325 when Ada_83 =>
1326 if Comes_From_Source (N) then
1327 Error_Msg_N
1328 ("(Ada 83) cannot rename function return object", Nam);
1329 end if;
1331 -- In Ada 95, warn for odd case of renaming parameterless function
1332 -- call if this is not a limited type (where this is useful).
1334 when others =>
1335 if Warn_On_Object_Renames_Function
1336 and then No (Parameter_Associations (Nam))
1337 and then not Is_Limited_Type (Etype (Nam))
1338 and then Comes_From_Source (Nam)
1339 then
1340 Error_Msg_N
1341 ("renaming function result object is suspicious?.r?", Nam);
1342 Error_Msg_NE
1343 ("\function & will be called only once?.r?", Nam,
1344 Entity (Name (Nam)));
1345 Error_Msg_N -- CODEFIX
1346 ("\suggest using an initialized constant object "
1347 & "instead?.r?", Nam);
1348 end if;
1349 end case;
1350 end if;
1352 Check_Constrained_Object;
1354 -- An object renaming requires an exact match of the type. Class-wide
1355 -- matching is not allowed.
1357 if Is_Class_Wide_Type (T)
1358 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1359 then
1360 Wrong_Type (Nam, T);
1361 end if;
1363 -- We must search for an actual subtype here so that the bounds of
1364 -- objects of unconstrained types don't get dropped on the floor - such
1365 -- as with renamings of formal parameters.
1367 T2 := Get_Actual_Subtype_If_Available (Nam);
1369 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1371 if Nkind (Nam) = N_Explicit_Dereference
1372 and then Ekind (Etype (T2)) = E_Incomplete_Type
1373 then
1374 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1375 return;
1377 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1378 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1379 return;
1380 end if;
1382 if Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1383 declare
1384 Nam_Ent : constant Entity_Id := Entity (Get_Object_Name (Nam));
1385 Nam_Decl : constant Node_Id := Declaration_Node (Nam_Ent);
1387 begin
1388 if Has_Null_Exclusion (N)
1389 and then not Has_Null_Exclusion (Nam_Decl)
1390 then
1391 -- Ada 2005 (AI-423): If the object name denotes a generic
1392 -- formal object of a generic unit G, and the object renaming
1393 -- declaration occurs within the body of G or within the body
1394 -- of a generic unit declared within the declarative region
1395 -- of G, then the declaration of the formal object of G must
1396 -- have a null exclusion or a null-excluding subtype.
1398 if Is_Formal_Object (Nam_Ent)
1399 and then In_Generic_Scope (Id)
1400 then
1401 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1402 Error_Msg_N
1403 ("object does not exclude `NULL` "
1404 & "(RM 8.5.1(4.6/2))", N);
1406 elsif In_Package_Body (Scope (Id)) then
1407 Error_Msg_N
1408 ("formal object does not have a null exclusion"
1409 & "(RM 8.5.1(4.6/2))", N);
1410 end if;
1412 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1413 -- shall exclude null.
1415 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1416 Error_Msg_N
1417 ("object does not exclude `NULL` "
1418 & "(RM 8.5.1(4.6/2))", N);
1420 -- An instance is illegal if it contains a renaming that
1421 -- excludes null, and the actual does not. The renaming
1422 -- declaration has already indicated that the declaration
1423 -- of the renamed actual in the instance will raise
1424 -- constraint_error.
1426 elsif Nkind (Nam_Decl) = N_Object_Declaration
1427 and then In_Instance
1428 and then
1429 Present (Corresponding_Generic_Association (Nam_Decl))
1430 and then Nkind (Expression (Nam_Decl)) =
1431 N_Raise_Constraint_Error
1432 then
1433 Error_Msg_N
1434 ("actual does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1436 -- Finally, if there is a null exclusion, the subtype mark
1437 -- must not be null-excluding.
1439 elsif No (Access_Definition (N))
1440 and then Can_Never_Be_Null (T)
1441 then
1442 Error_Msg_NE
1443 ("`NOT NULL` not allowed (& already excludes null)",
1444 N, T);
1446 end if;
1448 elsif Can_Never_Be_Null (T)
1449 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1450 then
1451 Error_Msg_N
1452 ("object does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1454 elsif Has_Null_Exclusion (N)
1455 and then No (Access_Definition (N))
1456 and then Can_Never_Be_Null (T)
1457 then
1458 Error_Msg_NE
1459 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1460 end if;
1461 end;
1462 end if;
1464 -- Set the Ekind of the entity, unless it has been set already, as is
1465 -- the case for the iteration object over a container with no variable
1466 -- indexing. In that case it's been marked as a constant, and we do not
1467 -- want to change it to a variable.
1469 if Ekind (Id) /= E_Constant then
1470 Mutate_Ekind (Id, E_Variable);
1471 end if;
1473 Reinit_Object_Size_Align (Id);
1475 -- If N comes from source then check that the original node is an
1476 -- object reference since there may have been several rewritting and
1477 -- folding. Do not do this for N_Function_Call or N_Explicit_Dereference
1478 -- which might correspond to rewrites of e.g. N_Selected_Component
1479 -- (for example Object.Method rewriting).
1480 -- If N does not come from source then assume the tree is properly
1481 -- formed and accept any object reference. In such cases we do support
1482 -- more cases of renamings anyway, so the actual check on which renaming
1483 -- is valid is better left to the code generator as a last sanity
1484 -- check.
1486 if Comes_From_Source (N) then
1487 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference then
1488 Is_Object_Ref := Is_Object_Reference (Nam);
1489 else
1490 Is_Object_Ref := Is_Object_Reference (Original_Node (Nam));
1491 end if;
1492 else
1493 Is_Object_Ref := True;
1494 end if;
1496 if T = Any_Type or else Etype (Nam) = Any_Type then
1497 return;
1499 -- Verify that the renamed entity is an object or function call
1501 elsif Is_Object_Ref then
1502 if Comes_From_Source (N) then
1503 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1504 Error_Msg_N
1505 ("illegal renaming of discriminant-dependent component", Nam);
1506 end if;
1508 -- If the renaming comes from source and the renamed object is a
1509 -- dereference, then mark the prefix as needing debug information,
1510 -- since it might have been rewritten hence internally generated
1511 -- and Debug_Renaming_Declaration will link the renaming to it.
1513 if Nkind (Nam) = N_Explicit_Dereference
1514 and then Is_Entity_Name (Prefix (Nam))
1515 then
1516 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1517 end if;
1518 end if;
1520 -- Weird but legal, equivalent to renaming a function call. Illegal
1521 -- if the literal is the result of constant-folding an attribute
1522 -- reference that is not a function.
1524 elsif Is_Entity_Name (Nam)
1525 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1526 and then Nkind (Original_Node (Nam)) /= N_Attribute_Reference
1527 then
1528 null;
1530 -- A named number can only be renamed without a subtype mark
1532 elsif Nkind (Nam) in N_Real_Literal | N_Integer_Literal
1533 and then Present (Subtype_Mark (N))
1534 and then Present (Original_Entity (Nam))
1535 then
1536 Error_Msg_N ("incompatible types in renaming", Nam);
1538 -- AI12-0383: Names that denote values can be renamed.
1539 -- Ignore (accept) N_Raise_xxx_Error nodes in this context.
1541 elsif No_Raise_xxx_Error (Nam) = OK then
1542 Error_Msg_Ada_2022_Feature ("value in renaming", Sloc (Nam));
1543 end if;
1545 Set_Etype (Id, T2);
1547 if not Is_Variable (Nam) then
1548 Mutate_Ekind (Id, E_Constant);
1549 Set_Never_Set_In_Source (Id, True);
1550 Set_Is_True_Constant (Id, True);
1551 end if;
1553 -- The entity of the renaming declaration needs to reflect whether the
1554 -- renamed object is atomic, independent, volatile or VFA. These flags
1555 -- are set on the renamed object in the RM legality sense.
1557 Set_Is_Atomic (Id, Is_Atomic_Object (Nam));
1558 Set_Is_Independent (Id, Is_Independent_Object (Nam));
1559 Set_Is_Volatile (Id, Is_Volatile_Object_Ref (Nam));
1560 Set_Is_Volatile_Full_Access
1561 (Id, Is_Volatile_Full_Access_Object_Ref (Nam));
1563 -- Treat as volatile if we just set the Volatile flag
1565 if Is_Volatile (Id)
1567 -- Or if we are renaming an entity which was marked this way
1569 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1571 or else (Is_Entity_Name (Nam)
1572 and then Treat_As_Volatile (Entity (Nam)))
1573 then
1574 Set_Treat_As_Volatile (Id, True);
1575 end if;
1577 -- Now make the link to the renamed object
1579 Set_Renamed_Object (Id, Nam);
1581 -- Implementation-defined aspect specifications can appear in a renaming
1582 -- declaration, but not language-defined ones. The call to procedure
1583 -- Analyze_Aspect_Specifications will take care of this error check.
1585 if Has_Aspects (N) then
1586 Analyze_Aspect_Specifications (N, Id);
1587 end if;
1589 -- Deal with dimensions
1591 Analyze_Dimension (N);
1592 end Analyze_Object_Renaming;
1594 ------------------------------
1595 -- Analyze_Package_Renaming --
1596 ------------------------------
1598 procedure Analyze_Package_Renaming (N : Node_Id) is
1599 New_P : constant Entity_Id := Defining_Entity (N);
1600 Old_P : Entity_Id;
1601 Spec : Node_Id;
1603 begin
1604 if Name (N) = Error then
1605 return;
1606 end if;
1608 -- Check for Text_IO special units (we may be renaming a Text_IO child),
1609 -- but make sure not to catch renamings generated for package instances
1610 -- that have nothing to do with them but are nevertheless homonyms.
1612 if Is_Entity_Name (Name (N))
1613 and then Present (Entity (Name (N)))
1614 and then Is_Generic_Instance (Entity (Name (N)))
1615 then
1616 null;
1617 else
1618 Check_Text_IO_Special_Unit (Name (N));
1619 end if;
1621 if Current_Scope /= Standard_Standard then
1622 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1623 end if;
1625 Enter_Name (New_P);
1626 Analyze (Name (N));
1628 if Is_Entity_Name (Name (N)) then
1629 Old_P := Entity (Name (N));
1630 else
1631 Old_P := Any_Id;
1632 end if;
1634 if Etype (Old_P) = Any_Type then
1635 Error_Msg_N ("expect package name in renaming", Name (N));
1637 elsif Ekind (Old_P) /= E_Package
1638 and then not (Ekind (Old_P) = E_Generic_Package
1639 and then In_Open_Scopes (Old_P))
1640 then
1641 if Ekind (Old_P) = E_Generic_Package then
1642 Error_Msg_N
1643 ("generic package cannot be renamed as a package", Name (N));
1644 else
1645 Error_Msg_Sloc := Sloc (Old_P);
1646 Error_Msg_NE
1647 ("expect package name in renaming, found& declared#",
1648 Name (N), Old_P);
1649 end if;
1651 -- Set basic attributes to minimize cascaded errors
1653 Mutate_Ekind (New_P, E_Package);
1654 Set_Etype (New_P, Standard_Void_Type);
1656 elsif Present (Renamed_Entity (Old_P))
1657 and then (From_Limited_With (Renamed_Entity (Old_P))
1658 or else Has_Limited_View (Renamed_Entity (Old_P)))
1659 and then not
1660 Unit_Is_Visible (Cunit (Get_Source_Unit (Renamed_Entity (Old_P))))
1661 then
1662 Error_Msg_NE
1663 ("renaming of limited view of package & not usable in this context"
1664 & " (RM 8.5.3(3.1/2))", Name (N), Renamed_Entity (Old_P));
1666 -- Set basic attributes to minimize cascaded errors
1668 Mutate_Ekind (New_P, E_Package);
1669 Set_Etype (New_P, Standard_Void_Type);
1671 -- Here for OK package renaming
1673 else
1674 -- Entities in the old package are accessible through the renaming
1675 -- entity. The simplest implementation is to have both packages share
1676 -- the entity list.
1678 Mutate_Ekind (New_P, E_Package);
1679 Set_Etype (New_P, Standard_Void_Type);
1681 if Present (Renamed_Entity (Old_P)) then
1682 Set_Renamed_Entity (New_P, Renamed_Entity (Old_P));
1683 else
1684 Set_Renamed_Entity (New_P, Old_P);
1685 end if;
1687 -- The package renaming declaration may become Ghost if it renames a
1688 -- Ghost entity.
1690 Mark_Ghost_Renaming (N, Old_P);
1692 Set_Has_Completion (New_P);
1693 Set_First_Entity (New_P, First_Entity (Old_P));
1694 Set_Last_Entity (New_P, Last_Entity (Old_P));
1695 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1696 Check_Library_Unit_Renaming (N, Old_P);
1697 Generate_Reference (Old_P, Name (N));
1699 -- If the renaming is in the visible part of a package, then we set
1700 -- Renamed_In_Spec for the renamed package, to prevent giving
1701 -- warnings about no entities referenced. Such a warning would be
1702 -- overenthusiastic, since clients can see entities in the renamed
1703 -- package via the visible package renaming.
1705 declare
1706 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1707 begin
1708 if Ekind (Ent) = E_Package
1709 and then not In_Private_Part (Ent)
1710 and then In_Extended_Main_Source_Unit (N)
1711 and then Ekind (Old_P) = E_Package
1712 then
1713 Set_Renamed_In_Spec (Old_P);
1714 end if;
1715 end;
1717 -- If this is the renaming declaration of a package instantiation
1718 -- within itself, it is the declaration that ends the list of actuals
1719 -- for the instantiation. At this point, the subtypes that rename
1720 -- the actuals are flagged as generic, to avoid spurious ambiguities
1721 -- if the actuals for two distinct formals happen to coincide. If
1722 -- the actual is a private type, the subtype has a private completion
1723 -- that is flagged in the same fashion.
1725 -- Resolution is identical to what is was in the original generic.
1726 -- On exit from the generic instance, these are turned into regular
1727 -- subtypes again, so they are compatible with types in their class.
1729 if not Is_Generic_Instance (Old_P) then
1730 return;
1731 else
1732 Spec := Specification (Unit_Declaration_Node (Old_P));
1733 end if;
1735 if Nkind (Spec) = N_Package_Specification
1736 and then Present (Generic_Parent (Spec))
1737 and then Old_P = Current_Scope
1738 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1739 then
1740 declare
1741 E : Entity_Id;
1743 begin
1744 E := First_Entity (Old_P);
1745 while Present (E) and then E /= New_P loop
1746 if Is_Type (E)
1747 and then Nkind (Parent (E)) = N_Subtype_Declaration
1748 then
1749 Set_Is_Generic_Actual_Type (E);
1751 if Is_Private_Type (E)
1752 and then Present (Full_View (E))
1753 then
1754 Set_Is_Generic_Actual_Type (Full_View (E));
1755 end if;
1756 end if;
1758 Next_Entity (E);
1759 end loop;
1760 end;
1761 end if;
1762 end if;
1764 -- Implementation-defined aspect specifications can appear in a renaming
1765 -- declaration, but not language-defined ones. The call to procedure
1766 -- Analyze_Aspect_Specifications will take care of this error check.
1768 if Has_Aspects (N) then
1769 Analyze_Aspect_Specifications (N, New_P);
1770 end if;
1771 end Analyze_Package_Renaming;
1773 -------------------------------
1774 -- Analyze_Renamed_Character --
1775 -------------------------------
1777 procedure Analyze_Renamed_Character
1778 (N : Node_Id;
1779 New_S : Entity_Id;
1780 Is_Body : Boolean)
1782 C : constant Node_Id := Name (N);
1784 begin
1785 if Ekind (New_S) = E_Function then
1786 Resolve (C, Etype (New_S));
1788 if Is_Body then
1789 Check_Frozen_Renaming (N, New_S);
1790 end if;
1792 else
1793 Error_Msg_N ("character literal can only be renamed as function", N);
1794 end if;
1795 end Analyze_Renamed_Character;
1797 ---------------------------------
1798 -- Analyze_Renamed_Dereference --
1799 ---------------------------------
1801 procedure Analyze_Renamed_Dereference
1802 (N : Node_Id;
1803 New_S : Entity_Id;
1804 Is_Body : Boolean)
1806 Nam : constant Node_Id := Name (N);
1807 P : constant Node_Id := Prefix (Nam);
1808 Typ : Entity_Id;
1809 Ind : Interp_Index;
1810 It : Interp;
1812 begin
1813 if not Is_Overloaded (P) then
1814 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1815 or else not Type_Conformant (Etype (Nam), New_S)
1816 then
1817 Error_Msg_N ("designated type does not match specification", P);
1818 else
1819 Resolve (P);
1820 end if;
1822 return;
1824 else
1825 Typ := Any_Type;
1826 Get_First_Interp (Nam, Ind, It);
1828 while Present (It.Nam) loop
1830 if Ekind (It.Nam) = E_Subprogram_Type
1831 and then Type_Conformant (It.Nam, New_S)
1832 then
1833 if Typ /= Any_Id then
1834 Error_Msg_N ("ambiguous renaming", P);
1835 return;
1836 else
1837 Typ := It.Nam;
1838 end if;
1839 end if;
1841 Get_Next_Interp (Ind, It);
1842 end loop;
1844 if Typ = Any_Type then
1845 Error_Msg_N ("designated type does not match specification", P);
1846 else
1847 Resolve (N, Typ);
1849 if Is_Body then
1850 Check_Frozen_Renaming (N, New_S);
1851 end if;
1852 end if;
1853 end if;
1854 end Analyze_Renamed_Dereference;
1856 ---------------------------
1857 -- Analyze_Renamed_Entry --
1858 ---------------------------
1860 procedure Analyze_Renamed_Entry
1861 (N : Node_Id;
1862 New_S : Entity_Id;
1863 Is_Body : Boolean)
1865 Nam : constant Node_Id := Name (N);
1866 Sel : constant Node_Id := Selector_Name (Nam);
1867 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1868 Old_S : Entity_Id;
1870 begin
1871 if Entity (Sel) = Any_Id then
1873 -- Selector is undefined on prefix. Error emitted already
1875 Set_Has_Completion (New_S);
1876 return;
1877 end if;
1879 -- Otherwise find renamed entity and build body of New_S as a call to it
1881 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1883 if Old_S = Any_Id then
1884 Error_Msg_N ("no subprogram or entry matches specification", N);
1885 else
1886 if Is_Body then
1887 Check_Subtype_Conformant (New_S, Old_S, N);
1888 Generate_Reference (New_S, Defining_Entity (N), 'b');
1889 Style.Check_Identifier (Defining_Entity (N), New_S);
1891 else
1892 -- Only mode conformance required for a renaming_as_declaration
1894 Check_Mode_Conformant (New_S, Old_S, N);
1895 end if;
1897 Inherit_Renamed_Profile (New_S, Old_S);
1899 -- The prefix can be an arbitrary expression that yields a task or
1900 -- protected object, so it must be resolved.
1902 if Is_Access_Type (Etype (Prefix (Nam))) then
1903 Insert_Explicit_Dereference (Prefix (Nam));
1904 end if;
1905 Resolve (Prefix (Nam), Scope (Old_S));
1906 end if;
1908 Set_Convention (New_S, Convention (Old_S));
1909 Set_Has_Completion (New_S, Inside_A_Generic);
1911 -- AI05-0225: If the renamed entity is a procedure or entry of a
1912 -- protected object, the target object must be a variable.
1914 if Is_Protected_Type (Scope (Old_S))
1915 and then Ekind (New_S) = E_Procedure
1916 and then not Is_Variable (Prefix (Nam))
1917 then
1918 if Is_Actual then
1919 Error_Msg_N
1920 ("target object of protected operation used as actual for "
1921 & "formal procedure must be a variable", Nam);
1922 else
1923 Error_Msg_N
1924 ("target object of protected operation renamed as procedure, "
1925 & "must be a variable", Nam);
1926 end if;
1927 end if;
1929 if Is_Body then
1930 Check_Frozen_Renaming (N, New_S);
1931 end if;
1932 end Analyze_Renamed_Entry;
1934 -----------------------------------
1935 -- Analyze_Renamed_Family_Member --
1936 -----------------------------------
1938 procedure Analyze_Renamed_Family_Member
1939 (N : Node_Id;
1940 New_S : Entity_Id;
1941 Is_Body : Boolean)
1943 Nam : constant Node_Id := Name (N);
1944 P : constant Node_Id := Prefix (Nam);
1945 Old_S : Entity_Id;
1947 begin
1948 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1949 or else (Nkind (P) = N_Selected_Component
1950 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1951 then
1952 if Is_Entity_Name (P) then
1953 Old_S := Entity (P);
1954 else
1955 Old_S := Entity (Selector_Name (P));
1956 end if;
1958 if not Entity_Matches_Spec (Old_S, New_S) then
1959 Error_Msg_N ("entry family does not match specification", N);
1961 elsif Is_Body then
1962 Check_Subtype_Conformant (New_S, Old_S, N);
1963 Generate_Reference (New_S, Defining_Entity (N), 'b');
1964 Style.Check_Identifier (Defining_Entity (N), New_S);
1965 end if;
1967 else
1968 Error_Msg_N ("no entry family matches specification", N);
1969 end if;
1971 Set_Has_Completion (New_S, Inside_A_Generic);
1973 if Is_Body then
1974 Check_Frozen_Renaming (N, New_S);
1975 end if;
1976 end Analyze_Renamed_Family_Member;
1978 -----------------------------------------
1979 -- Analyze_Renamed_Primitive_Operation --
1980 -----------------------------------------
1982 procedure Analyze_Renamed_Primitive_Operation
1983 (N : Node_Id;
1984 New_S : Entity_Id;
1985 Is_Body : Boolean)
1987 Old_S : Entity_Id;
1988 Nam : Entity_Id;
1990 function Conforms
1991 (Subp : Entity_Id;
1992 Ctyp : Conformance_Type) return Boolean;
1993 -- Verify that the signatures of the renamed entity and the new entity
1994 -- match. The first formal of the renamed entity is skipped because it
1995 -- is the target object in any subsequent call.
1997 --------------
1998 -- Conforms --
1999 --------------
2001 function Conforms
2002 (Subp : Entity_Id;
2003 Ctyp : Conformance_Type) return Boolean
2005 Old_F : Entity_Id;
2006 New_F : Entity_Id;
2008 begin
2009 if Ekind (Subp) /= Ekind (New_S) then
2010 return False;
2011 end if;
2013 Old_F := Next_Formal (First_Formal (Subp));
2014 New_F := First_Formal (New_S);
2015 while Present (Old_F) and then Present (New_F) loop
2016 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
2017 return False;
2018 end if;
2020 if Ctyp >= Mode_Conformant
2021 and then Ekind (Old_F) /= Ekind (New_F)
2022 then
2023 return False;
2024 end if;
2026 Next_Formal (New_F);
2027 Next_Formal (Old_F);
2028 end loop;
2030 return True;
2031 end Conforms;
2033 -- Start of processing for Analyze_Renamed_Primitive_Operation
2035 begin
2036 if not Is_Overloaded (Selector_Name (Name (N))) then
2037 Old_S := Entity (Selector_Name (Name (N)));
2039 if not Conforms (Old_S, Type_Conformant) then
2040 Old_S := Any_Id;
2041 end if;
2043 else
2044 -- Find the operation that matches the given signature
2046 declare
2047 It : Interp;
2048 Ind : Interp_Index;
2050 begin
2051 Old_S := Any_Id;
2052 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
2054 while Present (It.Nam) loop
2055 if Conforms (It.Nam, Type_Conformant) then
2056 Old_S := It.Nam;
2057 end if;
2059 Get_Next_Interp (Ind, It);
2060 end loop;
2061 end;
2062 end if;
2064 if Old_S = Any_Id then
2065 Error_Msg_N ("no subprogram or entry matches specification", N);
2067 else
2068 if Is_Body then
2069 if not Conforms (Old_S, Subtype_Conformant) then
2070 Error_Msg_N ("subtype conformance error in renaming", N);
2071 end if;
2073 Generate_Reference (New_S, Defining_Entity (N), 'b');
2074 Style.Check_Identifier (Defining_Entity (N), New_S);
2076 else
2077 -- Only mode conformance required for a renaming_as_declaration
2079 if not Conforms (Old_S, Mode_Conformant) then
2080 Error_Msg_N ("mode conformance error in renaming", N);
2081 end if;
2083 -- AI12-0204: The prefix of a prefixed view that is renamed or
2084 -- passed as a formal subprogram must be renamable as an object.
2086 Nam := Prefix (Name (N));
2088 if Is_Object_Reference (Nam) then
2089 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
2090 Error_Msg_N
2091 ("illegal renaming of discriminant-dependent component",
2092 Nam);
2093 end if;
2094 else
2095 Error_Msg_N ("expect object name in renaming", Nam);
2096 end if;
2098 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
2099 -- view of a subprogram is intrinsic, because the compiler has
2100 -- to generate a wrapper for any call to it. If the name in a
2101 -- subprogram renaming is a prefixed view, the entity is thus
2102 -- intrinsic, and 'Access cannot be applied to it.
2104 Set_Convention (New_S, Convention_Intrinsic);
2105 end if;
2107 -- Inherit_Renamed_Profile (New_S, Old_S);
2109 -- The prefix can be an arbitrary expression that yields an
2110 -- object, so it must be resolved.
2112 Resolve (Prefix (Name (N)));
2113 end if;
2114 end Analyze_Renamed_Primitive_Operation;
2116 ---------------------------------
2117 -- Analyze_Subprogram_Renaming --
2118 ---------------------------------
2120 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
2121 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
2122 Is_Actual : constant Boolean := Present (Formal_Spec);
2123 Nam : constant Node_Id := Name (N);
2124 Save_AV : constant Ada_Version_Type := Ada_Version;
2125 Save_AVP : constant Node_Id := Ada_Version_Pragma;
2126 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
2127 Spec : constant Node_Id := Specification (N);
2129 Old_S : Entity_Id := Empty;
2130 Rename_Spec : Entity_Id;
2132 procedure Check_Null_Exclusion
2133 (Ren : Entity_Id;
2134 Sub : Entity_Id);
2135 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
2136 -- following AI rules:
2138 -- If Ren denotes a generic formal object of a generic unit G, and the
2139 -- renaming (or instantiation containing the actual) occurs within the
2140 -- body of G or within the body of a generic unit declared within the
2141 -- declarative region of G, then the corresponding parameter of G
2142 -- shall have a null_exclusion; Otherwise the subtype of the Sub's
2143 -- formal parameter shall exclude null.
2145 -- Similarly for its return profile.
2147 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id);
2148 -- Ensure that a SPARK renaming denoted by its entity Subp_Id does not
2149 -- declare a primitive operation of a tagged type (SPARK RM 6.1.1(3)).
2151 procedure Freeze_Actual_Profile;
2152 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
2153 -- types: a callable entity freezes its profile, unless it has an
2154 -- incomplete untagged formal (RM 13.14(10.2/3)).
2156 function Has_Class_Wide_Actual return Boolean;
2157 -- Ada 2012 (AI05-071, AI05-0131) and Ada 2022 (AI12-0165): True if N is
2158 -- the renaming for a defaulted formal subprogram where the actual for
2159 -- the controlling formal type is class-wide.
2161 procedure Handle_Instance_With_Class_Wide_Type
2162 (Inst_Node : Node_Id;
2163 Ren_Id : Entity_Id;
2164 Wrapped_Prim : out Entity_Id;
2165 Wrap_Id : out Entity_Id);
2166 -- Ada 2012 (AI05-0071), Ada 2022 (AI12-0165): when the actual type
2167 -- of an instantiation is a class-wide type T'Class we may need to
2168 -- wrap a primitive operation of T; this routine looks for a suitable
2169 -- primitive to be wrapped and (if the wrapper is required) returns the
2170 -- Id of the wrapped primitive and the Id of the built wrapper. Ren_Id
2171 -- is the defining entity for the renamed subprogram specification.
2173 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
2174 -- Find renamed entity when the declaration is a renaming_as_body and
2175 -- the renamed entity may itself be a renaming_as_body. Used to enforce
2176 -- rule that a renaming_as_body is illegal if the declaration occurs
2177 -- before the subprogram it completes is frozen, and renaming indirectly
2178 -- renames the subprogram itself.(Defect Report 8652/0027).
2180 --------------------------
2181 -- Check_Null_Exclusion --
2182 --------------------------
2184 procedure Check_Null_Exclusion
2185 (Ren : Entity_Id;
2186 Sub : Entity_Id)
2188 Ren_Formal : Entity_Id;
2189 Sub_Formal : Entity_Id;
2191 function Null_Exclusion_Mismatch
2192 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean;
2193 -- Return True if there is a null exclusion mismatch between
2194 -- Renaming and Renamed, False otherwise.
2196 -----------------------------
2197 -- Null_Exclusion_Mismatch --
2198 -----------------------------
2200 function Null_Exclusion_Mismatch
2201 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean is
2202 begin
2203 return Has_Null_Exclusion (Parent (Renaming))
2204 and then
2205 not (Has_Null_Exclusion (Parent (Renamed))
2206 or else (Can_Never_Be_Null (Etype (Renamed))
2207 and then not
2208 (Is_Formal_Subprogram (Sub)
2209 and then In_Generic_Body (Current_Scope))));
2210 end Null_Exclusion_Mismatch;
2212 begin
2213 -- Parameter check
2215 Ren_Formal := First_Formal (Ren);
2216 Sub_Formal := First_Formal (Sub);
2217 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2218 if Null_Exclusion_Mismatch (Ren_Formal, Sub_Formal) then
2219 Error_Msg_Sloc := Sloc (Sub_Formal);
2220 Error_Msg_NE
2221 ("`NOT NULL` required for parameter &#",
2222 Ren_Formal, Sub_Formal);
2223 end if;
2225 Next_Formal (Ren_Formal);
2226 Next_Formal (Sub_Formal);
2227 end loop;
2229 -- Return profile check
2231 if Nkind (Parent (Ren)) = N_Function_Specification
2232 and then Nkind (Parent (Sub)) = N_Function_Specification
2233 and then Null_Exclusion_Mismatch (Ren, Sub)
2234 then
2235 Error_Msg_Sloc := Sloc (Sub);
2236 Error_Msg_N ("return must specify `NOT NULL`#", Ren);
2237 end if;
2238 end Check_Null_Exclusion;
2240 -------------------------------------
2241 -- Check_SPARK_Primitive_Operation --
2242 -------------------------------------
2244 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id) is
2245 Prag : constant Node_Id := SPARK_Pragma (Subp_Id);
2246 Typ : Entity_Id;
2248 begin
2249 -- Nothing to do when the subprogram is not subject to SPARK_Mode On
2250 -- because this check applies to SPARK code only.
2252 if not (Present (Prag)
2253 and then Get_SPARK_Mode_From_Annotation (Prag) = On)
2254 then
2255 return;
2257 -- Nothing to do when the subprogram is not a primitive operation
2259 elsif not Is_Primitive (Subp_Id) then
2260 return;
2261 end if;
2263 Typ := Find_Dispatching_Type (Subp_Id);
2265 -- Nothing to do when the subprogram is a primitive operation of an
2266 -- untagged type.
2268 if No (Typ) then
2269 return;
2270 end if;
2272 -- At this point a renaming declaration introduces a new primitive
2273 -- operation for a tagged type.
2275 Error_Msg_Node_2 := Typ;
2276 Error_Msg_NE
2277 ("subprogram renaming & cannot declare primitive for type & "
2278 & "(SPARK RM 6.1.1(3))", N, Subp_Id);
2279 end Check_SPARK_Primitive_Operation;
2281 ---------------------------
2282 -- Freeze_Actual_Profile --
2283 ---------------------------
2285 procedure Freeze_Actual_Profile is
2286 F : Entity_Id;
2287 Has_Untagged_Inc : Boolean;
2288 Instantiation_Node : constant Node_Id := Parent (N);
2290 begin
2291 if Ada_Version >= Ada_2012 then
2292 F := First_Formal (Formal_Spec);
2293 Has_Untagged_Inc := False;
2294 while Present (F) loop
2295 if Ekind (Etype (F)) = E_Incomplete_Type
2296 and then not Is_Tagged_Type (Etype (F))
2297 then
2298 Has_Untagged_Inc := True;
2299 exit;
2300 end if;
2302 Next_Formal (F);
2303 end loop;
2305 if Ekind (Formal_Spec) = E_Function
2306 and then not Is_Tagged_Type (Etype (Formal_Spec))
2307 then
2308 Has_Untagged_Inc := True;
2309 end if;
2311 if not Has_Untagged_Inc then
2312 F := First_Formal (Old_S);
2313 while Present (F) loop
2314 Freeze_Before (Instantiation_Node, Etype (F));
2316 if Is_Incomplete_Or_Private_Type (Etype (F))
2317 and then No (Underlying_Type (Etype (F)))
2318 then
2319 -- Exclude generic types, or types derived from them.
2320 -- They will be frozen in the enclosing instance.
2322 if Is_Generic_Type (Etype (F))
2323 or else Is_Generic_Type (Root_Type (Etype (F)))
2324 then
2325 null;
2327 -- A limited view of a type declared elsewhere needs no
2328 -- freezing actions.
2330 elsif From_Limited_With (Etype (F)) then
2331 null;
2333 else
2334 Error_Msg_NE
2335 ("type& must be frozen before this point",
2336 Instantiation_Node, Etype (F));
2337 end if;
2338 end if;
2340 Next_Formal (F);
2341 end loop;
2342 end if;
2343 end if;
2344 end Freeze_Actual_Profile;
2346 ---------------------------
2347 -- Has_Class_Wide_Actual --
2348 ---------------------------
2350 function Has_Class_Wide_Actual return Boolean is
2351 Formal : Entity_Id;
2352 Formal_Typ : Entity_Id;
2354 begin
2355 if Is_Actual then
2356 Formal := First_Formal (Formal_Spec);
2357 while Present (Formal) loop
2358 Formal_Typ := Etype (Formal);
2360 if Has_Unknown_Discriminants (Formal_Typ)
2361 and then not Is_Class_Wide_Type (Formal_Typ)
2362 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2363 then
2364 return True;
2365 end if;
2367 Next_Formal (Formal);
2368 end loop;
2369 end if;
2371 return False;
2372 end Has_Class_Wide_Actual;
2374 ------------------------------------------
2375 -- Handle_Instance_With_Class_Wide_Type --
2376 ------------------------------------------
2378 procedure Handle_Instance_With_Class_Wide_Type
2379 (Inst_Node : Node_Id;
2380 Ren_Id : Entity_Id;
2381 Wrapped_Prim : out Entity_Id;
2382 Wrap_Id : out Entity_Id)
2384 procedure Build_Class_Wide_Wrapper
2385 (Ren_Id : Entity_Id;
2386 Prim_Op : Entity_Id;
2387 Wrap_Id : out Entity_Id);
2388 -- Build a wrapper for the renaming Ren_Id of subprogram Prim_Op.
2390 procedure Find_Suitable_Candidate
2391 (Prim_Op : out Entity_Id;
2392 Is_CW_Prim : out Boolean);
2393 -- Look for a suitable primitive to be wrapped (Prim_Op); Is_CW_Prim
2394 -- indicates that the found candidate is a class-wide primitive (to
2395 -- help the caller decide if the wrapper is required).
2397 ------------------------------
2398 -- Build_Class_Wide_Wrapper --
2399 ------------------------------
2401 procedure Build_Class_Wide_Wrapper
2402 (Ren_Id : Entity_Id;
2403 Prim_Op : Entity_Id;
2404 Wrap_Id : out Entity_Id)
2406 Loc : constant Source_Ptr := Sloc (N);
2408 function Build_Call
2409 (Subp_Id : Entity_Id;
2410 Params : List_Id) return Node_Id;
2411 -- Create a dispatching call to invoke routine Subp_Id with
2412 -- actuals built from the parameter specifications of list Params.
2414 function Build_Expr_Fun_Call
2415 (Subp_Id : Entity_Id;
2416 Params : List_Id) return Node_Id;
2417 -- Create a dispatching call to invoke function Subp_Id with
2418 -- actuals built from the parameter specifications of list Params.
2419 -- Directly return the call, so that it can be used inside an
2420 -- expression function. This is a requirement of GNATprove mode.
2422 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
2423 -- Create a subprogram specification based on the subprogram
2424 -- profile of Subp_Id.
2426 ----------------
2427 -- Build_Call --
2428 ----------------
2430 function Build_Call
2431 (Subp_Id : Entity_Id;
2432 Params : List_Id) return Node_Id
2434 Actuals : constant List_Id := New_List;
2435 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2436 Formal : Node_Id;
2438 begin
2439 -- Build the actual parameters of the call
2441 Formal := First (Params);
2442 while Present (Formal) loop
2443 Append_To (Actuals,
2444 Make_Identifier (Loc,
2445 Chars (Defining_Identifier (Formal))));
2446 Next (Formal);
2447 end loop;
2449 -- Generate:
2450 -- return Subp_Id (Actuals);
2452 if Ekind (Subp_Id) in E_Function | E_Operator then
2453 return
2454 Make_Simple_Return_Statement (Loc,
2455 Expression =>
2456 Make_Function_Call (Loc,
2457 Name => Call_Ref,
2458 Parameter_Associations => Actuals));
2460 -- Generate:
2461 -- Subp_Id (Actuals);
2463 else
2464 return
2465 Make_Procedure_Call_Statement (Loc,
2466 Name => Call_Ref,
2467 Parameter_Associations => Actuals);
2468 end if;
2469 end Build_Call;
2471 -------------------------
2472 -- Build_Expr_Fun_Call --
2473 -------------------------
2475 function Build_Expr_Fun_Call
2476 (Subp_Id : Entity_Id;
2477 Params : List_Id) return Node_Id
2479 Actuals : constant List_Id := New_List;
2480 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2481 Formal : Node_Id;
2483 begin
2484 pragma Assert (Ekind (Subp_Id) in E_Function | E_Operator);
2486 -- Build the actual parameters of the call
2488 Formal := First (Params);
2489 while Present (Formal) loop
2490 Append_To (Actuals,
2491 Make_Identifier (Loc,
2492 Chars (Defining_Identifier (Formal))));
2493 Next (Formal);
2494 end loop;
2496 -- Generate:
2497 -- Subp_Id (Actuals);
2499 return
2500 Make_Function_Call (Loc,
2501 Name => Call_Ref,
2502 Parameter_Associations => Actuals);
2503 end Build_Expr_Fun_Call;
2505 ----------------
2506 -- Build_Spec --
2507 ----------------
2509 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2510 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2511 Spec_Id : constant Entity_Id :=
2512 Make_Defining_Identifier (Loc,
2513 New_External_Name (Chars (Subp_Id), 'R'));
2515 begin
2516 if Ekind (Formal_Spec) = E_Procedure then
2517 return
2518 Make_Procedure_Specification (Loc,
2519 Defining_Unit_Name => Spec_Id,
2520 Parameter_Specifications => Params);
2521 else
2522 return
2523 Make_Function_Specification (Loc,
2524 Defining_Unit_Name => Spec_Id,
2525 Parameter_Specifications => Params,
2526 Result_Definition =>
2527 New_Copy_Tree (Result_Definition (Spec)));
2528 end if;
2529 end Build_Spec;
2531 -- Local variables
2533 Body_Decl : Node_Id;
2534 Spec_Decl : Node_Id;
2535 New_Spec : Node_Id;
2537 -- Start of processing for Build_Class_Wide_Wrapper
2539 begin
2540 pragma Assert (not Error_Posted (Nam));
2542 -- Step 1: Create the declaration and the body of the wrapper,
2543 -- insert all the pieces into the tree.
2545 -- In GNATprove mode, create a function wrapper in the form of an
2546 -- expression function, so that an implicit postcondition relating
2547 -- the result of calling the wrapper function and the result of
2548 -- the dispatching call to the wrapped function is known during
2549 -- proof.
2551 if GNATprove_Mode
2552 and then Ekind (Ren_Id) in E_Function | E_Operator
2553 then
2554 New_Spec := Build_Spec (Ren_Id);
2555 Body_Decl :=
2556 Make_Expression_Function (Loc,
2557 Specification => New_Spec,
2558 Expression =>
2559 Build_Expr_Fun_Call
2560 (Subp_Id => Prim_Op,
2561 Params => Parameter_Specifications (New_Spec)));
2563 Wrap_Id := Defining_Entity (Body_Decl);
2565 -- Otherwise, create separate spec and body for the subprogram
2567 else
2568 Spec_Decl :=
2569 Make_Subprogram_Declaration (Loc,
2570 Specification => Build_Spec (Ren_Id));
2571 Insert_Before_And_Analyze (N, Spec_Decl);
2573 Wrap_Id := Defining_Entity (Spec_Decl);
2575 Body_Decl :=
2576 Make_Subprogram_Body (Loc,
2577 Specification => Build_Spec (Ren_Id),
2578 Declarations => New_List,
2579 Handled_Statement_Sequence =>
2580 Make_Handled_Sequence_Of_Statements (Loc,
2581 Statements => New_List (
2582 Build_Call
2583 (Subp_Id => Prim_Op,
2584 Params =>
2585 Parameter_Specifications
2586 (Specification (Spec_Decl))))));
2588 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
2589 end if;
2591 Set_Is_Class_Wide_Wrapper (Wrap_Id);
2593 -- If the operator carries an Eliminated pragma, indicate that
2594 -- the wrapper is also to be eliminated, to prevent spurious
2595 -- errors when using gnatelim on programs that include box-
2596 -- defaulted initialization of equality operators.
2598 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2600 -- In GNATprove mode, insert the body in the tree for analysis
2602 if GNATprove_Mode then
2603 Insert_Before_And_Analyze (N, Body_Decl);
2604 end if;
2606 -- The generated body does not freeze and must be analyzed when
2607 -- the class-wide wrapper is frozen. The body is only needed if
2608 -- expansion is enabled.
2610 if Expander_Active then
2611 Append_Freeze_Action (Wrap_Id, Body_Decl);
2612 end if;
2614 -- Step 2: The subprogram renaming aliases the wrapper
2616 Rewrite (Name (N), New_Occurrence_Of (Wrap_Id, Loc));
2617 end Build_Class_Wide_Wrapper;
2619 -----------------------------
2620 -- Find_Suitable_Candidate --
2621 -----------------------------
2623 procedure Find_Suitable_Candidate
2624 (Prim_Op : out Entity_Id;
2625 Is_CW_Prim : out Boolean)
2627 Loc : constant Source_Ptr := Sloc (N);
2629 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
2630 -- Find a primitive subprogram of type Typ which matches the
2631 -- profile of the renaming declaration.
2633 procedure Interpretation_Error (Subp_Id : Entity_Id);
2634 -- Emit a continuation error message suggesting subprogram Subp_Id
2635 -- as a possible interpretation.
2637 function Is_Intrinsic_Equality
2638 (Subp_Id : Entity_Id) return Boolean;
2639 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
2640 -- operator.
2642 function Is_Suitable_Candidate
2643 (Subp_Id : Entity_Id) return Boolean;
2644 -- Determine whether subprogram Subp_Id is a suitable candidate
2645 -- for the role of a wrapped subprogram.
2647 --------------------
2648 -- Find_Primitive --
2649 --------------------
2651 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2652 procedure Replace_Parameter_Types (Spec : Node_Id);
2653 -- Given a specification Spec, replace all class-wide parameter
2654 -- types with reference to type Typ.
2656 -----------------------------
2657 -- Replace_Parameter_Types --
2658 -----------------------------
2660 procedure Replace_Parameter_Types (Spec : Node_Id) is
2661 Formal : Node_Id;
2662 Formal_Id : Entity_Id;
2663 Formal_Typ : Node_Id;
2665 begin
2666 Formal := First (Parameter_Specifications (Spec));
2667 while Present (Formal) loop
2668 Formal_Id := Defining_Identifier (Formal);
2669 Formal_Typ := Parameter_Type (Formal);
2671 -- Create a new entity for each class-wide formal to
2672 -- prevent aliasing with the original renaming. Replace
2673 -- the type of such a parameter with the candidate type.
2675 if Nkind (Formal_Typ) = N_Identifier
2676 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2677 then
2678 Set_Defining_Identifier (Formal,
2679 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2681 Set_Parameter_Type (Formal,
2682 New_Occurrence_Of (Typ, Loc));
2683 end if;
2685 Next (Formal);
2686 end loop;
2687 end Replace_Parameter_Types;
2689 -- Local variables
2691 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2692 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2693 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2694 Subp_Id : Entity_Id;
2696 -- Start of processing for Find_Primitive
2698 begin
2699 -- Each attempt to find a suitable primitive of a particular
2700 -- type operates on its own copy of the original renaming.
2701 -- As a result the original renaming is kept decoration and
2702 -- side-effect free.
2704 -- Inherit the overloaded status of the renamed subprogram name
2706 if Is_Overloaded (Nam) then
2707 Set_Is_Overloaded (Alt_Nam);
2708 Save_Interps (Nam, Alt_Nam);
2709 end if;
2711 -- The copied renaming is hidden from visibility to prevent the
2712 -- pollution of the enclosing context.
2714 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2716 -- The types of all class-wide parameters must be changed to
2717 -- the candidate type.
2719 Replace_Parameter_Types (Alt_Spec);
2721 -- Try to find a suitable primitive that matches the altered
2722 -- profile of the renaming specification.
2724 Subp_Id :=
2725 Find_Renamed_Entity
2726 (N => Alt_Ren,
2727 Nam => Name (Alt_Ren),
2728 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2729 Is_Actual => Is_Actual);
2731 -- Do not return Any_Id if the resolution of the altered
2732 -- profile failed as this complicates further checks on
2733 -- the caller side; return Empty instead.
2735 if Subp_Id = Any_Id then
2736 return Empty;
2737 else
2738 return Subp_Id;
2739 end if;
2740 end Find_Primitive;
2742 --------------------------
2743 -- Interpretation_Error --
2744 --------------------------
2746 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2747 begin
2748 Error_Msg_Sloc := Sloc (Subp_Id);
2750 if Is_Internal (Subp_Id) then
2751 Error_Msg_NE
2752 ("\\possible interpretation: predefined & #",
2753 Spec, Formal_Spec);
2754 else
2755 Error_Msg_NE
2756 ("\\possible interpretation: & defined #",
2757 Spec, Formal_Spec);
2758 end if;
2759 end Interpretation_Error;
2761 ---------------------------
2762 -- Is_Intrinsic_Equality --
2763 ---------------------------
2765 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean
2767 begin
2768 return
2769 Ekind (Subp_Id) = E_Operator
2770 and then Chars (Subp_Id) = Name_Op_Eq
2771 and then Is_Intrinsic_Subprogram (Subp_Id);
2772 end Is_Intrinsic_Equality;
2774 ---------------------------
2775 -- Is_Suitable_Candidate --
2776 ---------------------------
2778 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean
2780 begin
2781 if No (Subp_Id) then
2782 return False;
2784 -- An intrinsic subprogram is never a good candidate. This
2785 -- is an indication of a missing primitive, either defined
2786 -- directly or inherited from a parent tagged type.
2788 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2789 return False;
2791 else
2792 return True;
2793 end if;
2794 end Is_Suitable_Candidate;
2796 -- Local variables
2798 Actual_Typ : Entity_Id := Empty;
2799 -- The actual class-wide type for Formal_Typ
2801 CW_Prim_OK : Boolean;
2802 CW_Prim_Op : Entity_Id;
2803 -- The class-wide subprogram (if available) that corresponds to
2804 -- the renamed generic formal subprogram.
2806 Formal_Typ : Entity_Id := Empty;
2807 -- The generic formal type with unknown discriminants
2809 Root_Prim_OK : Boolean;
2810 Root_Prim_Op : Entity_Id;
2811 -- The root type primitive (if available) that corresponds to the
2812 -- renamed generic formal subprogram.
2814 Root_Typ : Entity_Id := Empty;
2815 -- The root type of Actual_Typ
2817 Formal : Node_Id;
2819 -- Start of processing for Find_Suitable_Candidate
2821 begin
2822 pragma Assert (not Error_Posted (Nam));
2824 Prim_Op := Empty;
2825 Is_CW_Prim := False;
2827 -- Analyze the renamed name, but do not resolve it. The resolution
2828 -- is completed once a suitable subprogram is found.
2830 Analyze (Nam);
2832 -- When the renamed name denotes the intrinsic operator equals,
2833 -- the name must be treated as overloaded. This allows for a
2834 -- potential match against the root type's predefined equality
2835 -- function.
2837 if Is_Intrinsic_Equality (Entity (Nam)) then
2838 Set_Is_Overloaded (Nam);
2839 Collect_Interps (Nam);
2840 end if;
2842 -- Step 1: Find the generic formal type and its corresponding
2843 -- class-wide actual type from the renamed generic formal
2844 -- subprogram.
2846 Formal := First_Formal (Formal_Spec);
2847 while Present (Formal) loop
2848 if Has_Unknown_Discriminants (Etype (Formal))
2849 and then not Is_Class_Wide_Type (Etype (Formal))
2850 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2851 then
2852 Formal_Typ := Etype (Formal);
2853 Actual_Typ := Base_Type (Get_Instance_Of (Formal_Typ));
2854 Root_Typ := Root_Type (Actual_Typ);
2855 exit;
2856 end if;
2858 Next_Formal (Formal);
2859 end loop;
2861 -- The specification of the generic formal subprogram should
2862 -- always contain a formal type with unknown discriminants whose
2863 -- actual is a class-wide type; otherwise this indicates a failure
2864 -- in function Has_Class_Wide_Actual.
2866 pragma Assert (Present (Formal_Typ));
2868 -- Step 2: Find the proper class-wide subprogram or primitive
2869 -- that corresponds to the renamed generic formal subprogram.
2871 CW_Prim_Op := Find_Primitive (Actual_Typ);
2872 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2873 Root_Prim_Op := Find_Primitive (Root_Typ);
2874 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2876 -- The class-wide actual type has two subprograms that correspond
2877 -- to the renamed generic formal subprogram:
2879 -- with procedure Prim_Op (Param : Formal_Typ);
2881 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2882 -- procedure Prim_Op (Param : Actual_Typ'Class);
2884 -- Even though the declaration of the two subprograms is legal, a
2885 -- call to either one is ambiguous and therefore illegal.
2887 if CW_Prim_OK and Root_Prim_OK then
2889 -- A user-defined primitive has precedence over a predefined
2890 -- one.
2892 if Is_Internal (CW_Prim_Op)
2893 and then not Is_Internal (Root_Prim_Op)
2894 then
2895 Prim_Op := Root_Prim_Op;
2897 elsif Is_Internal (Root_Prim_Op)
2898 and then not Is_Internal (CW_Prim_Op)
2899 then
2900 Prim_Op := CW_Prim_Op;
2901 Is_CW_Prim := True;
2903 elsif CW_Prim_Op = Root_Prim_Op then
2904 Prim_Op := Root_Prim_Op;
2906 -- The two subprograms are legal but the class-wide subprogram
2907 -- is a class-wide wrapper built for a previous instantiation;
2908 -- the wrapper has precedence.
2910 elsif Present (Alias (CW_Prim_Op))
2911 and then Is_Class_Wide_Wrapper (Ultimate_Alias (CW_Prim_Op))
2912 then
2913 Prim_Op := CW_Prim_Op;
2914 Is_CW_Prim := True;
2916 -- Otherwise both candidate subprograms are user-defined and
2917 -- ambiguous.
2919 else
2920 Error_Msg_NE
2921 ("ambiguous actual for generic subprogram &",
2922 Spec, Formal_Spec);
2923 Interpretation_Error (Root_Prim_Op);
2924 Interpretation_Error (CW_Prim_Op);
2925 return;
2926 end if;
2928 elsif CW_Prim_OK and not Root_Prim_OK then
2929 Prim_Op := CW_Prim_Op;
2930 Is_CW_Prim := True;
2932 elsif not CW_Prim_OK and Root_Prim_OK then
2933 Prim_Op := Root_Prim_Op;
2935 -- An intrinsic equality may act as a suitable candidate in the
2936 -- case of a null type extension where the parent's equality
2937 -- is hidden. A call to an intrinsic equality is expanded as
2938 -- dispatching.
2940 elsif Present (Root_Prim_Op)
2941 and then Is_Intrinsic_Equality (Root_Prim_Op)
2942 then
2943 Prim_Op := Root_Prim_Op;
2945 -- Otherwise there are no candidate subprograms. Let the caller
2946 -- diagnose the error.
2948 else
2949 return;
2950 end if;
2952 -- At this point resolution has taken place and the name is no
2953 -- longer overloaded. Mark the primitive as referenced.
2955 Set_Is_Overloaded (Name (N), False);
2956 Set_Referenced (Prim_Op);
2957 end Find_Suitable_Candidate;
2959 -- Local variables
2961 Is_CW_Prim : Boolean;
2963 -- Start of processing for Handle_Instance_With_Class_Wide_Type
2965 begin
2966 Wrapped_Prim := Empty;
2967 Wrap_Id := Empty;
2969 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a
2970 -- formal type with unknown discriminants and a generic primitive
2971 -- operation of the said type with a box require special processing
2972 -- when the actual is a class-wide type:
2974 -- generic
2975 -- type Formal_Typ (<>) is private;
2976 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
2977 -- package Gen is ...
2979 -- package Inst is new Gen (Actual_Typ'Class);
2981 -- In this case the general renaming mechanism used in the prologue
2982 -- of an instance no longer applies:
2984 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
2986 -- The above is replaced the following wrapper/renaming combination:
2988 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
2989 -- begin
2990 -- Prim_Op (Param); -- primitive
2991 -- end Wrapper;
2993 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
2995 -- This transformation applies only if there is no explicit visible
2996 -- class-wide operation at the point of the instantiation. Ren_Id is
2997 -- the entity of the renaming declaration. When the transformation
2998 -- applies, Wrapped_Prim is the entity of the wrapped primitive.
3000 if Box_Present (Inst_Node) then
3001 Find_Suitable_Candidate
3002 (Prim_Op => Wrapped_Prim,
3003 Is_CW_Prim => Is_CW_Prim);
3005 if Present (Wrapped_Prim) then
3006 if not Is_CW_Prim then
3007 Build_Class_Wide_Wrapper (Ren_Id, Wrapped_Prim, Wrap_Id);
3009 -- Small optimization: When the candidate is a class-wide
3010 -- subprogram we don't build the wrapper; we modify the
3011 -- renaming declaration to directly map the actual to the
3012 -- generic formal and discard the candidate.
3014 else
3015 Rewrite (Nam, New_Occurrence_Of (Wrapped_Prim, Sloc (N)));
3016 Wrapped_Prim := Empty;
3017 end if;
3018 end if;
3020 -- Ada 2022 (AI12-0165, RM 12.6(8.5/3)): The actual subprogram for a
3021 -- formal_abstract_subprogram_declaration shall be:
3022 -- a) a dispatching operation of the controlling type; or
3023 -- b) if the controlling type is a formal type, and the actual
3024 -- type corresponding to that formal type is a specific type T,
3025 -- a dispatching operation of type T; or
3026 -- c) if the controlling type is a formal type, and the actual
3027 -- type is a class-wide type T'Class, an implicitly declared
3028 -- subprogram corresponding to a primitive operation of type T.
3030 elsif Nkind (Inst_Node) = N_Formal_Abstract_Subprogram_Declaration
3031 and then Is_Entity_Name (Nam)
3032 then
3033 Find_Suitable_Candidate
3034 (Prim_Op => Wrapped_Prim,
3035 Is_CW_Prim => Is_CW_Prim);
3037 if Present (Wrapped_Prim) then
3039 -- Cases (a) and (b); see previous description.
3041 if not Is_CW_Prim then
3042 Build_Class_Wide_Wrapper (Ren_Id, Wrapped_Prim, Wrap_Id);
3044 -- Case (c); see previous description.
3046 -- Implicit operations of T'Class for subtype declarations
3047 -- are built by Derive_Subprogram, and their Alias attribute
3048 -- references the primitive operation of T.
3050 elsif not Comes_From_Source (Wrapped_Prim)
3051 and then Nkind (Parent (Wrapped_Prim)) = N_Subtype_Declaration
3052 and then Present (Alias (Wrapped_Prim))
3053 then
3054 -- We don't need to build the wrapper; we modify the
3055 -- renaming declaration to directly map the actual to
3056 -- the generic formal and discard the candidate.
3058 Rewrite (Nam,
3059 New_Occurrence_Of (Alias (Wrapped_Prim), Sloc (N)));
3060 Wrapped_Prim := Empty;
3062 -- Legality rules do not apply; discard the candidate.
3064 else
3065 Wrapped_Prim := Empty;
3066 end if;
3067 end if;
3068 end if;
3069 end Handle_Instance_With_Class_Wide_Type;
3071 -------------------------
3072 -- Original_Subprogram --
3073 -------------------------
3075 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
3076 Orig_Decl : Node_Id;
3077 Orig_Subp : Entity_Id;
3079 begin
3080 -- First case: renamed entity is itself a renaming
3082 if Present (Alias (Subp)) then
3083 return Alias (Subp);
3085 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
3086 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
3087 then
3088 -- Check if renamed entity is a renaming_as_body
3090 Orig_Decl :=
3091 Unit_Declaration_Node
3092 (Corresponding_Body (Unit_Declaration_Node (Subp)));
3094 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
3095 Orig_Subp := Entity (Name (Orig_Decl));
3097 if Orig_Subp = Rename_Spec then
3099 -- Circularity detected
3101 return Orig_Subp;
3103 else
3104 return (Original_Subprogram (Orig_Subp));
3105 end if;
3106 else
3107 return Subp;
3108 end if;
3109 else
3110 return Subp;
3111 end if;
3112 end Original_Subprogram;
3114 -- Local variables
3116 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
3117 -- Ada 2012 (AI05-071, AI05-0131) and Ada 2022 (AI12-0165): True if the
3118 -- renaming is for a defaulted formal subprogram when the actual for a
3119 -- related formal type is class-wide.
3121 Inst_Node : Node_Id := Empty;
3122 New_S : Entity_Id := Empty;
3123 Wrapped_Prim : Entity_Id := Empty;
3125 -- Start of processing for Analyze_Subprogram_Renaming
3127 begin
3128 -- We must test for the attribute renaming case before the Analyze
3129 -- call because otherwise Sem_Attr will complain that the attribute
3130 -- is missing an argument when it is analyzed.
3132 if Nkind (Nam) = N_Attribute_Reference then
3134 -- In the case of an abstract formal subprogram association, rewrite
3135 -- an actual given by a stream or Put_Image attribute as the name of
3136 -- the corresponding stream or Put_Image primitive of the type.
3138 -- In a generic context the stream and Put_Image operations are not
3139 -- generated, and this must be treated as a normal attribute
3140 -- reference, to be expanded in subsequent instantiations.
3142 if Is_Actual
3143 and then Is_Abstract_Subprogram (Formal_Spec)
3144 and then Expander_Active
3145 then
3146 declare
3147 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
3148 Prim : Entity_Id;
3150 begin
3151 -- The class-wide forms of the stream and Put_Image attributes
3152 -- are not primitive dispatching operations (even though they
3153 -- internally dispatch).
3155 if Is_Class_Wide_Type (Prefix_Type) then
3156 Error_Msg_N
3157 ("attribute must be a primitive dispatching operation",
3158 Nam);
3159 return;
3160 end if;
3162 -- Retrieve the primitive subprogram associated with the
3163 -- attribute. This can only be a stream attribute, since those
3164 -- are the only ones that are dispatching (and the actual for
3165 -- an abstract formal subprogram must be dispatching
3166 -- operation).
3168 case Attribute_Name (Nam) is
3169 when Name_Input =>
3170 Prim :=
3171 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input);
3173 when Name_Output =>
3174 Prim :=
3175 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output);
3177 when Name_Read =>
3178 Prim :=
3179 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read);
3181 when Name_Write =>
3182 Prim :=
3183 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write);
3185 when Name_Put_Image =>
3186 Prim :=
3187 Find_Optional_Prim_Op (Prefix_Type, TSS_Put_Image);
3189 when others =>
3190 Error_Msg_N
3191 ("attribute must be a primitive dispatching operation",
3192 Nam);
3193 return;
3194 end case;
3196 -- If no stream operation was found, and the type is limited,
3197 -- the user should have defined one. This rule does not apply
3198 -- to Put_Image.
3200 if No (Prim)
3201 and then Attribute_Name (Nam) /= Name_Put_Image
3202 then
3203 if Is_Limited_Type (Prefix_Type) then
3204 Error_Msg_NE
3205 ("stream operation not defined for type&",
3206 N, Prefix_Type);
3207 return;
3209 -- Otherwise, compiler should have generated default
3211 else
3212 raise Program_Error;
3213 end if;
3214 end if;
3216 -- Rewrite the attribute into the name of its corresponding
3217 -- primitive dispatching subprogram. We can then proceed with
3218 -- the usual processing for subprogram renamings.
3220 declare
3221 Prim_Name : constant Node_Id :=
3222 Make_Identifier (Sloc (Nam),
3223 Chars => Chars (Prim));
3224 begin
3225 Set_Entity (Prim_Name, Prim);
3226 Rewrite (Nam, Prim_Name);
3227 Analyze (Nam);
3228 end;
3229 end;
3231 -- Normal processing for a renaming of an attribute
3233 else
3234 Attribute_Renaming (N);
3235 return;
3236 end if;
3237 end if;
3239 -- Check whether this declaration corresponds to the instantiation of a
3240 -- formal subprogram.
3242 -- If this is an instantiation, the corresponding actual is frozen and
3243 -- error messages can be made more precise. If this is a default
3244 -- subprogram, the entity is already established in the generic, and is
3245 -- not retrieved by visibility. If it is a default with a box, the
3246 -- candidate interpretations, if any, have been collected when building
3247 -- the renaming declaration. If overloaded, the proper interpretation is
3248 -- determined in Find_Renamed_Entity. If the entity is an operator,
3249 -- Find_Renamed_Entity applies additional visibility checks.
3251 if Is_Actual then
3252 Inst_Node := Unit_Declaration_Node (Formal_Spec);
3254 -- Ada 2012 (AI05-0071) and Ada 2022 (AI12-0165): when the actual
3255 -- type is a class-wide type T'Class we may need to wrap a primitive
3256 -- operation of T. Search for the wrapped primitive and (if required)
3257 -- build a wrapper whose body consists of a dispatching call to the
3258 -- wrapped primitive of T, with its formal parameters as the actual
3259 -- parameters.
3261 if CW_Actual and then
3263 -- Ada 2012 (AI05-0071): Check whether the renaming is for a
3264 -- defaulted actual subprogram with a class-wide actual.
3266 (Box_Present (Inst_Node)
3268 or else
3270 -- Ada 2022 (AI12-0165): Check whether the renaming is for a formal
3271 -- abstract subprogram declaration with a class-wide actual.
3273 (Nkind (Inst_Node) = N_Formal_Abstract_Subprogram_Declaration
3274 and then Is_Entity_Name (Nam)))
3275 then
3276 New_S := Analyze_Subprogram_Specification (Spec);
3278 -- Do not attempt to build the wrapper if the renaming is in error
3280 if not Error_Posted (Nam) then
3281 Handle_Instance_With_Class_Wide_Type
3282 (Inst_Node => Inst_Node,
3283 Ren_Id => New_S,
3284 Wrapped_Prim => Wrapped_Prim,
3285 Wrap_Id => Old_S);
3287 -- If several candidates were found, then we reported the
3288 -- ambiguity; stop processing the renaming declaration to
3289 -- avoid reporting further (spurious) errors.
3291 if Error_Posted (Spec) then
3292 return;
3293 end if;
3295 end if;
3296 end if;
3298 if Present (Wrapped_Prim) then
3300 -- When the wrapper is built, the subprogram renaming aliases
3301 -- the wrapper.
3303 Analyze (Nam);
3305 pragma Assert (Old_S = Entity (Nam)
3306 and then Is_Class_Wide_Wrapper (Old_S));
3308 -- The subprogram renaming declaration may become Ghost if it
3309 -- renames a wrapper of a Ghost entity.
3311 Mark_Ghost_Renaming (N, Wrapped_Prim);
3313 elsif Is_Entity_Name (Nam)
3314 and then Present (Entity (Nam))
3315 and then not Comes_From_Source (Nam)
3316 and then not Is_Overloaded (Nam)
3317 then
3318 Old_S := Entity (Nam);
3320 -- The subprogram renaming declaration may become Ghost if it
3321 -- renames a Ghost entity.
3323 Mark_Ghost_Renaming (N, Old_S);
3325 New_S := Analyze_Subprogram_Specification (Spec);
3327 -- Operator case
3329 if Ekind (Old_S) = E_Operator then
3331 -- Box present
3333 if Box_Present (Inst_Node) then
3334 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3336 -- If there is an immediately visible homonym of the operator
3337 -- and the declaration has a default, this is worth a warning
3338 -- because the user probably did not intend to get the pre-
3339 -- defined operator, visible in the generic declaration. To
3340 -- find if there is an intended candidate, analyze the renaming
3341 -- again in the current context.
3343 elsif Scope (Old_S) = Standard_Standard
3344 and then Present (Default_Name (Inst_Node))
3345 then
3346 declare
3347 Decl : constant Node_Id := New_Copy_Tree (N);
3348 Hidden : Entity_Id;
3350 begin
3351 Set_Entity (Name (Decl), Empty);
3352 Analyze (Name (Decl));
3353 Hidden :=
3354 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
3356 if Present (Hidden)
3357 and then In_Open_Scopes (Scope (Hidden))
3358 and then Is_Immediately_Visible (Hidden)
3359 and then Comes_From_Source (Hidden)
3360 and then Hidden /= Old_S
3361 then
3362 Error_Msg_Sloc := Sloc (Hidden);
3363 Error_Msg_N
3364 ("default subprogram is resolved in the generic "
3365 & "declaration (RM 12.6(17))??", N);
3366 Error_Msg_NE ("\and will not use & #??", N, Hidden);
3367 end if;
3368 end;
3369 end if;
3370 end if;
3372 else
3373 Analyze (Nam);
3375 -- The subprogram renaming declaration may become Ghost if it
3376 -- renames a Ghost entity.
3378 if Is_Entity_Name (Nam) then
3379 Mark_Ghost_Renaming (N, Entity (Nam));
3380 end if;
3382 New_S := Analyze_Subprogram_Specification (Spec);
3383 end if;
3385 else
3386 -- Renamed entity must be analyzed first, to avoid being hidden by
3387 -- new name (which might be the same in a generic instance).
3389 Analyze (Nam);
3391 -- The subprogram renaming declaration may become Ghost if it renames
3392 -- a Ghost entity.
3394 if Is_Entity_Name (Nam) then
3395 Mark_Ghost_Renaming (N, Entity (Nam));
3396 end if;
3398 -- The renaming defines a new overloaded entity, which is analyzed
3399 -- like a subprogram declaration.
3401 New_S := Analyze_Subprogram_Specification (Spec);
3402 end if;
3404 if Current_Scope /= Standard_Standard then
3405 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
3406 end if;
3408 -- Set SPARK mode from current context
3410 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
3411 Set_SPARK_Pragma_Inherited (New_S);
3413 Rename_Spec := Find_Corresponding_Spec (N);
3415 -- Case of Renaming_As_Body
3417 if Present (Rename_Spec) then
3418 Check_Previous_Null_Procedure (N, Rename_Spec);
3420 -- Renaming declaration is the completion of the declaration of
3421 -- Rename_Spec. We build an actual body for it at the freezing point.
3423 Set_Corresponding_Spec (N, Rename_Spec);
3425 -- Deal with special case of stream functions of abstract types
3426 -- and interfaces.
3428 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
3429 N_Abstract_Subprogram_Declaration
3430 then
3431 -- Input stream functions are abstract if the object type is
3432 -- abstract. Similarly, all default stream functions for an
3433 -- interface type are abstract. However, these subprograms may
3434 -- receive explicit declarations in representation clauses, making
3435 -- the attribute subprograms usable as defaults in subsequent
3436 -- type extensions.
3437 -- In this case we rewrite the declaration to make the subprogram
3438 -- non-abstract. We remove the previous declaration, and insert
3439 -- the new one at the point of the renaming, to prevent premature
3440 -- access to unfrozen types. The new declaration reuses the
3441 -- specification of the previous one, and must not be analyzed.
3443 pragma Assert
3444 (Is_Primitive (Entity (Nam))
3445 and then
3446 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
3447 declare
3448 Old_Decl : constant Node_Id :=
3449 Unit_Declaration_Node (Rename_Spec);
3450 New_Decl : constant Node_Id :=
3451 Make_Subprogram_Declaration (Sloc (N),
3452 Specification =>
3453 Relocate_Node (Specification (Old_Decl)));
3454 begin
3455 Remove (Old_Decl);
3456 Insert_After (N, New_Decl);
3457 Set_Is_Abstract_Subprogram (Rename_Spec, False);
3458 Set_Analyzed (New_Decl);
3459 end;
3460 end if;
3462 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
3464 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3465 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
3466 end if;
3468 Set_Convention (New_S, Convention (Rename_Spec));
3469 Check_Fully_Conformant (New_S, Rename_Spec);
3470 Set_Public_Status (New_S);
3472 if No_Return (Rename_Spec)
3473 and then not No_Return (Entity (Nam))
3474 then
3475 Error_Msg_NE
3476 ("renamed subprogram & must be No_Return", N, Entity (Nam));
3477 Error_Msg_N
3478 ("\since renaming subprogram is No_Return (RM 6.5.1(7/2))", N);
3479 end if;
3481 -- The specification does not introduce new formals, but only
3482 -- repeats the formals of the original subprogram declaration.
3483 -- For cross-reference purposes, and for refactoring tools, we
3484 -- treat the formals of the renaming declaration as body formals.
3486 Reference_Body_Formals (Rename_Spec, New_S);
3488 -- Indicate that the entity in the declaration functions like the
3489 -- corresponding body, and is not a new entity. The body will be
3490 -- constructed later at the freeze point, so indicate that the
3491 -- completion has not been seen yet.
3493 Reinit_Field_To_Zero (New_S, F_Has_Out_Or_In_Out_Parameter,
3494 Old_Ekind => (E_Function | E_Procedure => True, others => False));
3495 Reinit_Field_To_Zero (New_S, F_Needs_No_Actuals);
3496 Reinit_Field_To_Zero (New_S, F_Is_Predicate_Function);
3497 Reinit_Field_To_Zero (New_S, F_Protected_Subprogram);
3498 Reinit_Field_To_Zero (New_S, F_Is_Inlined_Always);
3499 Reinit_Field_To_Zero (New_S, F_Is_Generic_Actual_Subprogram);
3500 Mutate_Ekind (New_S, E_Subprogram_Body);
3501 New_S := Rename_Spec;
3502 Set_Has_Completion (Rename_Spec, False);
3504 -- Ada 2005: check overriding indicator
3506 if Present (Overridden_Operation (Rename_Spec)) then
3507 if Must_Not_Override (Specification (N)) then
3508 Error_Msg_NE
3509 ("subprogram& overrides inherited operation",
3510 N, Rename_Spec);
3512 elsif Style_Check
3513 and then not Must_Override (Specification (N))
3514 then
3515 Style.Missing_Overriding (N, Rename_Spec);
3516 end if;
3518 elsif Must_Override (Specification (N))
3519 and then not Can_Override_Operator (Rename_Spec)
3520 then
3521 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
3522 end if;
3524 -- AI12-0132: a renames-as-body freezes the expression of any
3525 -- expression function that it renames.
3527 if Is_Entity_Name (Nam)
3528 and then Is_Expression_Function (Entity (Nam))
3529 and then not Inside_A_Generic
3530 then
3531 Freeze_Expr_Types
3532 (Def_Id => Entity (Nam),
3533 Typ => Etype (Entity (Nam)),
3534 Expr =>
3535 Expression
3536 (Original_Node (Unit_Declaration_Node (Entity (Nam)))),
3537 N => N);
3538 end if;
3540 -- Normal subprogram renaming (not renaming as body)
3542 else
3543 Generate_Definition (New_S);
3544 New_Overloaded_Entity (New_S);
3546 if not (Is_Entity_Name (Nam)
3547 and then Is_Intrinsic_Subprogram (Entity (Nam)))
3548 then
3549 Check_Delayed_Subprogram (New_S);
3550 end if;
3552 -- Verify that a SPARK renaming does not declare a primitive
3553 -- operation of a tagged type.
3555 Check_SPARK_Primitive_Operation (New_S);
3556 end if;
3558 -- There is no need for elaboration checks on the new entity, which may
3559 -- be called before the next freezing point where the body will appear.
3560 -- Elaboration checks refer to the real entity, not the one created by
3561 -- the renaming declaration.
3563 Set_Kill_Elaboration_Checks (New_S, True);
3565 -- If we had a previous error, indicate a completion is present to stop
3566 -- junk cascaded messages, but don't take any further action.
3568 if Etype (Nam) = Any_Type then
3569 Set_Has_Completion (New_S);
3570 return;
3572 -- Case where name has the form of a selected component
3574 elsif Nkind (Nam) = N_Selected_Component then
3576 -- A name which has the form A.B can designate an entry of task A, a
3577 -- protected operation of protected object A, or finally a primitive
3578 -- operation of object A. In the later case, A is an object of some
3579 -- tagged type, or an access type that denotes one such. To further
3580 -- distinguish these cases, note that the scope of a task entry or
3581 -- protected operation is type of the prefix.
3583 -- The prefix could be an overloaded function call that returns both
3584 -- kinds of operations. This overloading pathology is left to the
3585 -- dedicated reader ???
3587 declare
3588 T : constant Entity_Id := Etype (Prefix (Nam));
3590 begin
3591 if Present (T)
3592 and then
3593 (Is_Tagged_Type (T)
3594 or else
3595 (Is_Access_Type (T)
3596 and then Is_Tagged_Type (Designated_Type (T))))
3597 and then Scope (Entity (Selector_Name (Nam))) /= T
3598 then
3599 Analyze_Renamed_Primitive_Operation
3600 (N, New_S, Present (Rename_Spec));
3601 return;
3603 else
3604 -- Renamed entity is an entry or protected operation. For those
3605 -- cases an explicit body is built (at the point of freezing of
3606 -- this entity) that contains a call to the renamed entity.
3608 -- This is not allowed for renaming as body if the renamed
3609 -- spec is already frozen (see RM 8.5.4(5) for details).
3611 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
3612 Error_Msg_N
3613 ("renaming-as-body cannot rename entry as subprogram", N);
3614 Error_Msg_NE
3615 ("\since & is already frozen (RM 8.5.4(5))",
3616 N, Rename_Spec);
3617 else
3618 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
3619 end if;
3621 return;
3622 end if;
3623 end;
3625 -- Case where name is an explicit dereference X.all
3627 elsif Nkind (Nam) = N_Explicit_Dereference then
3629 -- Renamed entity is designated by access_to_subprogram expression.
3630 -- Must build body to encapsulate call, as in the entry case.
3632 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
3633 return;
3635 -- Indexed component
3637 elsif Nkind (Nam) = N_Indexed_Component then
3638 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
3639 return;
3641 -- Character literal
3643 elsif Nkind (Nam) = N_Character_Literal then
3644 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
3645 return;
3647 -- Only remaining case is where we have a non-entity name, or a renaming
3648 -- of some other non-overloadable entity.
3650 elsif not Is_Entity_Name (Nam)
3651 or else not Is_Overloadable (Entity (Nam))
3652 then
3653 -- Do not mention the renaming if it comes from an instance
3655 if not Is_Actual then
3656 Error_Msg_N ("expect valid subprogram name in renaming", N);
3657 else
3658 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
3659 end if;
3661 return;
3662 end if;
3664 -- Find the renamed entity that matches the given specification. Disable
3665 -- Ada_83 because there is no requirement of full conformance between
3666 -- renamed entity and new entity, even though the same circuit is used.
3668 -- This is a bit of an odd case, which introduces a really irregular use
3669 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
3670 -- this. ???
3672 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
3673 Ada_Version_Pragma := Empty;
3674 Ada_Version_Explicit := Ada_Version;
3676 if No (Old_S) then
3677 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3679 -- The visible operation may be an inherited abstract operation that
3680 -- was overridden in the private part, in which case a call will
3681 -- dispatch to the overriding operation. Use the overriding one in
3682 -- the renaming declaration, to prevent spurious errors below.
3684 if Is_Overloadable (Old_S)
3685 and then Is_Abstract_Subprogram (Old_S)
3686 and then No (DTC_Entity (Old_S))
3687 and then Present (Alias (Old_S))
3688 and then not Is_Abstract_Subprogram (Alias (Old_S))
3689 and then Present (Overridden_Operation (Alias (Old_S)))
3690 then
3691 Old_S := Alias (Old_S);
3692 end if;
3694 -- When the renamed subprogram is overloaded and used as an actual
3695 -- of a generic, its entity is set to the first available homonym.
3696 -- We must first disambiguate the name, then set the proper entity.
3698 if Is_Actual and then Is_Overloaded (Nam) then
3699 Set_Entity (Nam, Old_S);
3700 end if;
3701 end if;
3703 -- Most common case: subprogram renames subprogram. No body is generated
3704 -- in this case, so we must indicate the declaration is complete as is.
3705 -- and inherit various attributes of the renamed subprogram.
3707 if No (Rename_Spec) then
3708 Set_Has_Completion (New_S);
3709 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3710 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3711 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3713 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3714 -- between a subprogram and its correct renaming.
3716 -- Note: the Any_Id check is a guard that prevents compiler crashes
3717 -- when performing a null exclusion check between a renaming and a
3718 -- renamed subprogram that has been found to be illegal.
3720 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3721 Check_Null_Exclusion
3722 (Ren => New_S,
3723 Sub => Entity (Nam));
3724 end if;
3726 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3727 -- overriding. The flag Requires_Overriding is set very selectively
3728 -- and misses some other illegal cases. The additional conditions
3729 -- checked below are sufficient but not necessary ???
3731 -- The rule does not apply to the renaming generated for an actual
3732 -- subprogram in an instance.
3734 if Is_Actual then
3735 null;
3737 -- Guard against previous errors, and omit renamings of predefined
3738 -- operators.
3740 elsif Ekind (Old_S) not in E_Function | E_Procedure then
3741 null;
3743 elsif Requires_Overriding (Old_S)
3744 or else
3745 (Is_Abstract_Subprogram (Old_S)
3746 and then Present (Find_Dispatching_Type (Old_S))
3747 and then not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3748 then
3749 Error_Msg_N
3750 ("renamed entity cannot be subprogram that requires overriding "
3751 & "(RM 8.5.4 (5.1))", N);
3752 end if;
3754 declare
3755 Prev : constant Entity_Id := Overridden_Operation (New_S);
3756 begin
3757 if Present (Prev)
3758 and then
3759 (Has_Non_Trivial_Precondition (Prev)
3760 or else Has_Non_Trivial_Precondition (Old_S))
3761 then
3762 Error_Msg_NE
3763 ("conflicting inherited classwide preconditions in renaming "
3764 & "of& (RM 6.1.1 (17)", N, Old_S);
3765 end if;
3766 end;
3767 end if;
3769 if Old_S /= Any_Id then
3770 if Is_Actual and then From_Default (N) then
3772 -- This is an implicit reference to the default actual
3774 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3776 else
3777 Generate_Reference (Old_S, Nam);
3778 end if;
3780 Check_Internal_Protected_Use (N, Old_S);
3782 -- For a renaming-as-body, require subtype conformance, but if the
3783 -- declaration being completed has not been frozen, then inherit the
3784 -- convention of the renamed subprogram prior to checking conformance
3785 -- (unless the renaming has an explicit convention established; the
3786 -- rule stated in the RM doesn't seem to address this ???).
3788 if Present (Rename_Spec) then
3789 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3790 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3792 if not Is_Frozen (Rename_Spec) then
3793 if not Has_Convention_Pragma (Rename_Spec) then
3794 Set_Convention (New_S, Convention (Old_S));
3795 end if;
3797 if Ekind (Old_S) /= E_Operator then
3798 Check_Mode_Conformant (New_S, Old_S, Spec);
3799 end if;
3801 if Original_Subprogram (Old_S) = Rename_Spec then
3802 Error_Msg_N ("unfrozen subprogram cannot rename itself", N);
3803 else
3804 Check_Formal_Subprogram_Conformance (New_S, Old_S, Spec);
3805 end if;
3806 else
3807 Check_Subtype_Conformant (New_S, Old_S, Spec);
3808 end if;
3810 Check_Frozen_Renaming (N, Rename_Spec);
3812 -- Check explicitly that renamed entity is not intrinsic, because
3813 -- in a generic the renamed body is not built. In this case,
3814 -- the renaming_as_body is a completion.
3816 if Inside_A_Generic then
3817 if Is_Frozen (Rename_Spec)
3818 and then Is_Intrinsic_Subprogram (Old_S)
3819 then
3820 Error_Msg_N
3821 ("subprogram in renaming_as_body cannot be intrinsic",
3822 Name (N));
3823 end if;
3825 Set_Has_Completion (Rename_Spec);
3826 end if;
3828 elsif Ekind (Old_S) /= E_Operator then
3830 -- If this a defaulted subprogram for a class-wide actual there is
3831 -- no check for mode conformance, given that the signatures don't
3832 -- match (the source mentions T but the actual mentions T'Class).
3834 if CW_Actual then
3835 null;
3837 -- No need for a redundant error message if this is a nested
3838 -- instance, unless the current instantiation (of a child unit)
3839 -- is a compilation unit, which is not analyzed when the parent
3840 -- generic is analyzed.
3842 elsif not Is_Actual
3843 or else No (Enclosing_Instance)
3844 or else Is_Compilation_Unit (Current_Scope)
3845 then
3846 Check_Mode_Conformant (New_S, Old_S);
3847 end if;
3848 end if;
3850 if No (Rename_Spec) then
3852 -- The parameter profile of the new entity is that of the renamed
3853 -- entity: the subtypes given in the specification are irrelevant.
3855 Inherit_Renamed_Profile (New_S, Old_S);
3857 -- A call to the subprogram is transformed into a call to the
3858 -- renamed entity. This is transitive if the renamed entity is
3859 -- itself a renaming.
3861 if Present (Alias (Old_S)) then
3862 Set_Alias (New_S, Alias (Old_S));
3863 else
3864 Set_Alias (New_S, Old_S);
3865 end if;
3867 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3868 -- renaming as body, since the entity in this case is not an
3869 -- intrinsic (it calls an intrinsic, but we have a real body for
3870 -- this call, and it is in this body that the required intrinsic
3871 -- processing will take place).
3873 -- Also, if this is a renaming of inequality, the renamed operator
3874 -- is intrinsic, but what matters is the corresponding equality
3875 -- operator, which may be user-defined.
3877 Set_Is_Intrinsic_Subprogram
3878 (New_S,
3879 Is_Intrinsic_Subprogram (Old_S)
3880 and then
3881 (Chars (Old_S) /= Name_Op_Ne
3882 or else Ekind (Old_S) = E_Operator
3883 or else Is_Intrinsic_Subprogram
3884 (Corresponding_Equality (Old_S))));
3886 if Ekind (Alias (New_S)) = E_Operator then
3887 Set_Has_Delayed_Freeze (New_S, False);
3888 end if;
3890 -- If the renaming corresponds to an association for an abstract
3891 -- formal subprogram, then various attributes must be set to
3892 -- indicate that the renaming is an abstract dispatching operation
3893 -- with a controlling type.
3895 -- Skip this decoration when the renaming corresponds to an
3896 -- association with class-wide wrapper (see above) because such
3897 -- wrapper is neither abstract nor a dispatching operation (its
3898 -- body has the dispatching call to the wrapped primitive).
3900 if Is_Actual
3901 and then Is_Abstract_Subprogram (Formal_Spec)
3902 and then No (Wrapped_Prim)
3903 then
3905 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3906 -- see it as corresponding to a generic association for a
3907 -- formal abstract subprogram
3909 Set_Is_Abstract_Subprogram (New_S);
3911 declare
3912 New_S_Ctrl_Type : constant Entity_Id :=
3913 Find_Dispatching_Type (New_S);
3914 Old_S_Ctrl_Type : constant Entity_Id :=
3915 Find_Dispatching_Type (Old_S);
3917 begin
3919 -- The actual must match the (instance of the) formal,
3920 -- and must be a controlling type.
3922 if Old_S_Ctrl_Type /= New_S_Ctrl_Type
3923 or else No (New_S_Ctrl_Type)
3924 then
3925 if No (New_S_Ctrl_Type) then
3926 Error_Msg_N
3927 ("actual must be dispatching subprogram", Nam);
3928 else
3929 Error_Msg_NE
3930 ("actual must be dispatching subprogram for type&",
3931 Nam, New_S_Ctrl_Type);
3932 end if;
3934 else
3935 Set_Is_Dispatching_Operation (New_S);
3936 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3938 -- If the actual in the formal subprogram is itself a
3939 -- formal abstract subprogram association, there's no
3940 -- dispatch table component or position to inherit.
3942 if Present (DTC_Entity (Old_S)) then
3943 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3944 Set_DT_Position_Value (New_S, DT_Position (Old_S));
3945 end if;
3946 end if;
3947 end;
3948 end if;
3949 end if;
3951 if Is_Actual then
3952 null;
3954 -- The following is illegal, because F hides whatever other F may
3955 -- be around:
3956 -- function F (...) renames F;
3958 elsif Old_S = New_S
3959 or else (Nkind (Nam) /= N_Expanded_Name
3960 and then Chars (Old_S) = Chars (New_S))
3961 then
3962 Error_Msg_N ("subprogram cannot rename itself", N);
3964 -- This is illegal even if we use a selector:
3965 -- function F (...) renames Pkg.F;
3966 -- because F is still hidden.
3968 elsif Nkind (Nam) = N_Expanded_Name
3969 and then Entity (Prefix (Nam)) = Current_Scope
3970 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3971 then
3972 -- This is an error, but we overlook the error and accept the
3973 -- renaming if the special Overriding_Renamings mode is in effect.
3975 if not Overriding_Renamings then
3976 Error_Msg_NE
3977 ("implicit operation& is not visible (RM 8.3 (15))",
3978 Nam, Old_S);
3979 end if;
3981 -- Check whether an expanded name used for the renamed subprogram
3982 -- begins with the same name as the renaming itself, and if so,
3983 -- issue an error about the prefix being hidden by the renaming.
3984 -- We exclude generic instances from this checking, since such
3985 -- normally illegal renamings can be constructed when expanding
3986 -- instantiations.
3988 elsif Nkind (Nam) = N_Expanded_Name and then not In_Instance then
3989 declare
3990 function Ult_Expanded_Prefix (N : Node_Id) return Node_Id is
3991 (if Nkind (N) /= N_Expanded_Name
3992 then N
3993 else Ult_Expanded_Prefix (Prefix (N)));
3994 -- Returns the ultimate prefix of an expanded name
3996 begin
3997 if Chars (Entity (Ult_Expanded_Prefix (Nam))) = Chars (New_S)
3998 then
3999 Error_Msg_Sloc := Sloc (N);
4000 Error_Msg_NE
4001 ("& is hidden by declaration#", Nam, New_S);
4002 end if;
4003 end;
4004 end if;
4006 Set_Convention (New_S, Convention (Old_S));
4008 if Is_Abstract_Subprogram (Old_S) then
4009 if Present (Rename_Spec) then
4010 Error_Msg_N
4011 ("a renaming-as-body cannot rename an abstract subprogram",
4013 Set_Has_Completion (Rename_Spec);
4014 else
4015 Set_Is_Abstract_Subprogram (New_S);
4016 end if;
4017 end if;
4019 Check_Library_Unit_Renaming (N, Old_S);
4021 -- Pathological case: procedure renames entry in the scope of its
4022 -- task. Entry is given by simple name, but body must be built for
4023 -- procedure. Of course if called it will deadlock.
4025 if Ekind (Old_S) = E_Entry then
4026 Set_Has_Completion (New_S, False);
4027 Set_Alias (New_S, Empty);
4028 end if;
4030 -- Do not freeze the renaming nor the renamed entity when the context
4031 -- is an enclosing generic. Freezing is an expansion activity, and in
4032 -- addition the renamed entity may depend on the generic formals of
4033 -- the enclosing generic.
4035 if Is_Actual and not Inside_A_Generic then
4036 Freeze_Before (N, Old_S);
4037 Freeze_Actual_Profile;
4038 Set_Has_Delayed_Freeze (New_S, False);
4039 Freeze_Before (N, New_S);
4041 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
4042 and then not Is_Abstract_Subprogram (Formal_Spec)
4043 then
4044 -- An abstract subprogram is only allowed as an actual in the
4045 -- case where the formal subprogram is also abstract.
4047 if Is_Abstract_Subprogram (Old_S) then
4048 Error_Msg_N
4049 ("abstract subprogram not allowed as generic actual", Nam);
4050 end if;
4052 -- AI12-0412: A primitive of an abstract type with Pre'Class
4053 -- or Post'Class aspects specified with nonstatic expressions
4054 -- is not allowed as actual for a nonabstract formal subprogram
4055 -- (see RM 6.1.1(18.2/5).
4057 if Is_Dispatching_Operation (Old_S)
4058 and then
4059 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post (Old_S)
4060 then
4061 Error_Msg_N
4062 ("primitive of abstract type with nonstatic class-wide "
4063 & "pre/postconditions not allowed as actual",
4064 Nam);
4065 end if;
4066 end if;
4067 end if;
4069 else
4070 -- A common error is to assume that implicit operators for types are
4071 -- defined in Standard, or in the scope of a subtype. In those cases
4072 -- where the renamed entity is given with an expanded name, it is
4073 -- worth mentioning that operators for the type are not declared in
4074 -- the scope given by the prefix.
4076 if Nkind (Nam) = N_Expanded_Name
4077 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
4078 and then Scope (Entity (Nam)) = Standard_Standard
4079 then
4080 declare
4081 T : constant Entity_Id :=
4082 Base_Type (Etype (First_Formal (New_S)));
4083 begin
4084 Error_Msg_Node_2 := Prefix (Nam);
4085 Error_Msg_NE
4086 ("operator for type& is not declared in&", Prefix (Nam), T);
4087 end;
4089 else
4090 Error_Msg_NE
4091 ("no visible subprogram matches the specification for&",
4092 Spec, New_S);
4093 end if;
4095 if Present (Candidate_Renaming) then
4096 declare
4097 F1 : Entity_Id;
4098 F2 : Entity_Id;
4099 T1 : Entity_Id;
4101 begin
4102 F1 := First_Formal (Candidate_Renaming);
4103 F2 := First_Formal (New_S);
4104 T1 := First_Subtype (Etype (F1));
4105 while Present (F1) and then Present (F2) loop
4106 Next_Formal (F1);
4107 Next_Formal (F2);
4108 end loop;
4110 if Present (F1) and then Present (Default_Value (F1)) then
4111 if Present (Next_Formal (F1)) then
4112 Error_Msg_NE
4113 ("\missing specification for & and other formals with "
4114 & "defaults", Spec, F1);
4115 else
4116 Error_Msg_NE ("\missing specification for &", Spec, F1);
4117 end if;
4118 end if;
4120 if Nkind (Nam) = N_Operator_Symbol
4121 and then From_Default (N)
4122 then
4123 Error_Msg_Node_2 := T1;
4124 Error_Msg_NE
4125 ("default & on & is not directly visible", Nam, Nam);
4126 end if;
4127 end;
4128 end if;
4129 end if;
4131 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
4132 -- controlling access parameters are known non-null for the renamed
4133 -- subprogram. Test also applies to a subprogram instantiation that
4134 -- is dispatching. Test is skipped if some previous error was detected
4135 -- that set Old_S to Any_Id.
4137 if Ada_Version >= Ada_2005
4138 and then Old_S /= Any_Id
4139 and then not Is_Dispatching_Operation (Old_S)
4140 and then Is_Dispatching_Operation (New_S)
4141 then
4142 declare
4143 Old_F : Entity_Id;
4144 New_F : Entity_Id;
4146 begin
4147 Old_F := First_Formal (Old_S);
4148 New_F := First_Formal (New_S);
4149 while Present (Old_F) loop
4150 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
4151 and then Is_Controlling_Formal (New_F)
4152 and then not Can_Never_Be_Null (Old_F)
4153 then
4154 Error_Msg_N ("access parameter is controlling,", New_F);
4155 Error_Msg_NE
4156 ("\corresponding parameter of& must be explicitly null "
4157 & "excluding", New_F, Old_S);
4158 end if;
4160 Next_Formal (Old_F);
4161 Next_Formal (New_F);
4162 end loop;
4163 end;
4164 end if;
4166 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
4167 -- is to warn if an operator is being renamed as a different operator.
4168 -- If the operator is predefined, examine the kind of the entity, not
4169 -- the abbreviated declaration in Standard.
4171 if Comes_From_Source (N)
4172 and then Present (Old_S)
4173 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
4174 or else Ekind (Old_S) = E_Operator)
4175 and then Nkind (New_S) = N_Defining_Operator_Symbol
4176 and then Chars (Old_S) /= Chars (New_S)
4177 then
4178 Error_Msg_NE
4179 ("& is being renamed as a different operator??", N, Old_S);
4180 end if;
4182 -- Check for renaming of obsolescent subprogram
4184 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
4186 -- Another warning or some utility: if the new subprogram as the same
4187 -- name as the old one, the old one is not hidden by an outer homograph,
4188 -- the new one is not a public symbol, and the old one is otherwise
4189 -- directly visible, the renaming is superfluous.
4191 if Chars (Old_S) = Chars (New_S)
4192 and then Comes_From_Source (N)
4193 and then Scope (Old_S) /= Standard_Standard
4194 and then Warn_On_Redundant_Constructs
4195 and then (Is_Immediately_Visible (Old_S)
4196 or else Is_Potentially_Use_Visible (Old_S))
4197 and then Is_Overloadable (Current_Scope)
4198 and then Chars (Current_Scope) /= Chars (Old_S)
4199 then
4200 Error_Msg_N
4201 ("redundant renaming, entity is directly visible?r?", Name (N));
4202 end if;
4204 -- Implementation-defined aspect specifications can appear in a renaming
4205 -- declaration, but not language-defined ones. The call to procedure
4206 -- Analyze_Aspect_Specifications will take care of this error check.
4208 if Has_Aspects (N) then
4209 Analyze_Aspect_Specifications (N, New_S);
4210 end if;
4212 -- AI12-0279
4214 if Is_Actual
4215 and then Has_Yield_Aspect (Formal_Spec)
4216 and then not Has_Yield_Aspect (Old_S)
4217 then
4218 Error_Msg_Name_1 := Name_Yield;
4219 Error_Msg_N
4220 ("actual subprogram& must have aspect% to match formal", Name (N));
4221 end if;
4223 Ada_Version := Save_AV;
4224 Ada_Version_Pragma := Save_AVP;
4225 Ada_Version_Explicit := Save_AV_Exp;
4227 -- Check if we are looking at an Ada 2012 defaulted formal subprogram
4228 -- and mark any use_package_clauses that affect the visibility of the
4229 -- implicit generic actual.
4231 -- Also, we may be looking at an internal renaming of a user-defined
4232 -- subprogram created for a generic formal subprogram association,
4233 -- which will also have to be marked here. This can occur when the
4234 -- corresponding formal subprogram contains references to other generic
4235 -- formals.
4237 if Is_Generic_Actual_Subprogram (New_S)
4238 and then (Is_Intrinsic_Subprogram (New_S)
4239 or else From_Default (N)
4240 or else Nkind (N) = N_Subprogram_Renaming_Declaration)
4241 then
4242 Mark_Use_Clauses (New_S);
4244 -- Handle overloaded subprograms
4246 if Present (Alias (New_S)) then
4247 Mark_Use_Clauses (Alias (New_S));
4248 end if;
4249 end if;
4250 end Analyze_Subprogram_Renaming;
4252 -------------------------
4253 -- Analyze_Use_Package --
4254 -------------------------
4256 -- Resolve the package names in the use clause, and make all the visible
4257 -- entities defined in the package potentially use-visible. If the package
4258 -- is already in use from a previous use clause, its visible entities are
4259 -- already use-visible. In that case, mark the occurrence as a redundant
4260 -- use. If the package is an open scope, i.e. if the use clause occurs
4261 -- within the package itself, ignore it.
4263 procedure Analyze_Use_Package (N : Node_Id; Chain : Boolean := True) is
4264 procedure Analyze_Package_Name (Clause : Node_Id);
4265 -- Perform analysis on a package name from a use_package_clause
4267 procedure Analyze_Package_Name_List (Head_Clause : Node_Id);
4268 -- Similar to Analyze_Package_Name but iterates over all the names
4269 -- in a use clause.
4271 --------------------------
4272 -- Analyze_Package_Name --
4273 --------------------------
4275 procedure Analyze_Package_Name (Clause : Node_Id) is
4276 Pack : constant Node_Id := Name (Clause);
4277 Pref : Node_Id;
4279 begin
4280 pragma Assert (Nkind (Clause) = N_Use_Package_Clause);
4281 Analyze (Pack);
4283 -- Verify that the package standard is not directly named in a
4284 -- use_package_clause.
4286 if Nkind (Parent (Clause)) = N_Compilation_Unit
4287 and then Nkind (Pack) = N_Expanded_Name
4288 then
4289 Pref := Prefix (Pack);
4291 while Nkind (Pref) = N_Expanded_Name loop
4292 Pref := Prefix (Pref);
4293 end loop;
4295 if Entity (Pref) = Standard_Standard then
4296 Error_Msg_N
4297 ("predefined package Standard cannot appear in a context "
4298 & "clause", Pref);
4299 end if;
4300 end if;
4301 end Analyze_Package_Name;
4303 -------------------------------
4304 -- Analyze_Package_Name_List --
4305 -------------------------------
4307 procedure Analyze_Package_Name_List (Head_Clause : Node_Id) is
4308 Curr : Node_Id;
4310 begin
4311 -- Due to the way source use clauses are split during parsing we are
4312 -- forced to simply iterate through all entities in scope until the
4313 -- clause representing the last name in the list is found.
4315 Curr := Head_Clause;
4316 while Present (Curr) loop
4317 Analyze_Package_Name (Curr);
4319 -- Stop iterating over the names in the use clause when we are at
4320 -- the last one.
4322 exit when not More_Ids (Curr) and then Prev_Ids (Curr);
4323 Next (Curr);
4324 end loop;
4325 end Analyze_Package_Name_List;
4327 -- Local variables
4329 Pack : Entity_Id;
4331 -- Start of processing for Analyze_Use_Package
4333 begin
4334 Set_Hidden_By_Use_Clause (N, No_Elist);
4336 -- Use clause not allowed in a spec of a predefined package declaration
4337 -- except that packages whose file name starts a-n are OK (these are
4338 -- children of Ada.Numerics, which are never loaded by Rtsfind).
4340 if Is_Predefined_Unit (Current_Sem_Unit)
4341 and then Get_Name_String
4342 (Unit_File_Name (Current_Sem_Unit)) (1 .. 3) /= "a-n"
4343 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
4344 N_Package_Declaration
4345 then
4346 Error_Msg_N ("use clause not allowed in predefined spec", N);
4347 end if;
4349 -- Loop through all package names from the original use clause in
4350 -- order to analyze referenced packages. A use_package_clause with only
4351 -- one name does not have More_Ids or Prev_Ids set, while a clause with
4352 -- More_Ids only starts the chain produced by the parser.
4354 if not More_Ids (N) and then not Prev_Ids (N) then
4355 Analyze_Package_Name (N);
4357 elsif More_Ids (N) and then not Prev_Ids (N) then
4358 Analyze_Package_Name_List (N);
4359 end if;
4361 if not Is_Entity_Name (Name (N)) then
4362 Error_Msg_N ("& is not a package", Name (N));
4364 return;
4365 end if;
4367 if Chain then
4368 Chain_Use_Clause (N);
4369 end if;
4371 Pack := Entity (Name (N));
4373 -- There are many cases where scopes are manipulated during analysis, so
4374 -- check that Pack's current use clause has not already been chained
4375 -- before setting its previous use clause.
4377 if Ekind (Pack) = E_Package
4378 and then Present (Current_Use_Clause (Pack))
4379 and then Current_Use_Clause (Pack) /= N
4380 and then No (Prev_Use_Clause (N))
4381 and then Prev_Use_Clause (Current_Use_Clause (Pack)) /= N
4382 then
4383 Set_Prev_Use_Clause (N, Current_Use_Clause (Pack));
4384 end if;
4386 -- Mark all entities as potentially use visible
4388 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
4389 if Ekind (Pack) = E_Generic_Package then
4390 Error_Msg_N -- CODEFIX
4391 ("a generic package is not allowed in a use clause", Name (N));
4393 elsif Is_Generic_Subprogram (Pack) then
4394 Error_Msg_N -- CODEFIX
4395 ("a generic subprogram is not allowed in a use clause",
4396 Name (N));
4398 elsif Is_Subprogram (Pack) then
4399 Error_Msg_N -- CODEFIX
4400 ("a subprogram is not allowed in a use clause", Name (N));
4402 else
4403 Error_Msg_N ("& is not allowed in a use clause", Name (N));
4404 end if;
4406 else
4407 if Nkind (Parent (N)) = N_Compilation_Unit then
4408 Check_In_Previous_With_Clause (N, Name (N));
4409 end if;
4411 Use_One_Package (N, Name (N));
4412 end if;
4414 Mark_Ghost_Clause (N);
4415 end Analyze_Use_Package;
4417 ----------------------
4418 -- Analyze_Use_Type --
4419 ----------------------
4421 procedure Analyze_Use_Type (N : Node_Id; Chain : Boolean := True) is
4422 E : Entity_Id;
4423 Id : Node_Id;
4425 begin
4426 Set_Hidden_By_Use_Clause (N, No_Elist);
4428 -- Chain clause to list of use clauses in current scope when flagged
4430 if Chain then
4431 Chain_Use_Clause (N);
4432 end if;
4434 -- Obtain the base type of the type denoted within the use_type_clause's
4435 -- subtype mark.
4437 Id := Subtype_Mark (N);
4438 Find_Type (Id);
4439 E := Base_Type (Entity (Id));
4441 -- There are many cases where a use_type_clause may be reanalyzed due to
4442 -- manipulation of the scope stack so we much guard against those cases
4443 -- here, otherwise, we must add the new use_type_clause to the previous
4444 -- use_type_clause chain in order to mark redundant use_type_clauses as
4445 -- used. When the redundant use-type clauses appear in a parent unit and
4446 -- a child unit we must prevent a circularity in the chain that would
4447 -- otherwise result from the separate steps of analysis and installation
4448 -- of the parent context.
4450 if Present (Current_Use_Clause (E))
4451 and then Current_Use_Clause (E) /= N
4452 and then Prev_Use_Clause (Current_Use_Clause (E)) /= N
4453 and then No (Prev_Use_Clause (N))
4454 then
4455 Set_Prev_Use_Clause (N, Current_Use_Clause (E));
4456 end if;
4458 -- If the Used_Operations list is already initialized, the clause has
4459 -- been analyzed previously, and it is being reinstalled, for example
4460 -- when the clause appears in a package spec and we are compiling the
4461 -- corresponding package body. In that case, make the entities on the
4462 -- existing list use_visible, and mark the corresponding types In_Use.
4464 if Present (Used_Operations (N)) then
4465 declare
4466 Elmt : Elmt_Id;
4468 begin
4469 Use_One_Type (Subtype_Mark (N), Installed => True);
4471 Elmt := First_Elmt (Used_Operations (N));
4472 while Present (Elmt) loop
4473 Set_Is_Potentially_Use_Visible (Node (Elmt));
4474 Next_Elmt (Elmt);
4475 end loop;
4476 end;
4478 return;
4479 end if;
4481 -- Otherwise, create new list and attach to it the operations that are
4482 -- made use-visible by the clause.
4484 Set_Used_Operations (N, New_Elmt_List);
4485 E := Entity (Id);
4487 if E /= Any_Type then
4488 Use_One_Type (Id);
4490 if Nkind (Parent (N)) = N_Compilation_Unit then
4491 if Nkind (Id) = N_Identifier then
4492 Error_Msg_N ("type is not directly visible", Id);
4494 elsif Is_Child_Unit (Scope (E))
4495 and then Scope (E) /= System_Aux_Id
4496 then
4497 Check_In_Previous_With_Clause (N, Prefix (Id));
4498 end if;
4499 end if;
4501 else
4502 -- If the use_type_clause appears in a compilation unit context,
4503 -- check whether it comes from a unit that may appear in a
4504 -- limited_with_clause, for a better error message.
4506 if Nkind (Parent (N)) = N_Compilation_Unit
4507 and then Nkind (Id) /= N_Identifier
4508 then
4509 declare
4510 Item : Node_Id;
4511 Pref : Node_Id;
4513 function Mentioned (Nam : Node_Id) return Boolean;
4514 -- Check whether the prefix of expanded name for the type
4515 -- appears in the prefix of some limited_with_clause.
4517 ---------------
4518 -- Mentioned --
4519 ---------------
4521 function Mentioned (Nam : Node_Id) return Boolean is
4522 begin
4523 return Nkind (Name (Item)) = N_Selected_Component
4524 and then Chars (Prefix (Name (Item))) = Chars (Nam);
4525 end Mentioned;
4527 begin
4528 Pref := Prefix (Id);
4529 Item := First (Context_Items (Parent (N)));
4530 while Present (Item) and then Item /= N loop
4531 if Nkind (Item) = N_With_Clause
4532 and then Limited_Present (Item)
4533 and then Mentioned (Pref)
4534 then
4535 Change_Error_Text
4536 (Get_Msg_Id, "premature usage of incomplete type");
4537 end if;
4539 Next (Item);
4540 end loop;
4541 end;
4542 end if;
4543 end if;
4545 Mark_Ghost_Clause (N);
4546 end Analyze_Use_Type;
4548 ------------------------
4549 -- Attribute_Renaming --
4550 ------------------------
4552 procedure Attribute_Renaming (N : Node_Id) is
4553 Loc : constant Source_Ptr := Sloc (N);
4554 Nam : constant Node_Id := Name (N);
4555 Spec : constant Node_Id := Specification (N);
4556 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
4557 Aname : constant Name_Id := Attribute_Name (Nam);
4559 Form_Num : Nat := 0;
4560 Expr_List : List_Id := No_List;
4562 Attr_Node : Node_Id;
4563 Body_Node : Node_Id;
4564 Param_Spec : Node_Id;
4566 begin
4567 Generate_Definition (New_S);
4569 -- This procedure is called in the context of subprogram renaming, and
4570 -- thus the attribute must be one that is a subprogram. All of those
4571 -- have at least one formal parameter, with the exceptions of the GNAT
4572 -- attribute 'Img, which GNAT treats as renameable.
4574 if Is_Empty_List (Parameter_Specifications (Spec)) then
4575 if Aname /= Name_Img then
4576 Error_Msg_N
4577 ("subprogram renaming an attribute must have formals", N);
4578 return;
4579 end if;
4581 else
4582 Param_Spec := First (Parameter_Specifications (Spec));
4583 while Present (Param_Spec) loop
4584 Form_Num := Form_Num + 1;
4586 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4587 Find_Type (Parameter_Type (Param_Spec));
4589 -- The profile of the new entity denotes the base type (s) of
4590 -- the types given in the specification. For access parameters
4591 -- there are no subtypes involved.
4593 Rewrite (Parameter_Type (Param_Spec),
4594 New_Occurrence_Of
4595 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
4596 end if;
4598 if No (Expr_List) then
4599 Expr_List := New_List;
4600 end if;
4602 Append_To (Expr_List,
4603 Make_Identifier (Loc,
4604 Chars => Chars (Defining_Identifier (Param_Spec))));
4606 -- The expressions in the attribute reference are not freeze
4607 -- points. Neither is the attribute as a whole, see below.
4609 Set_Must_Not_Freeze (Last (Expr_List));
4610 Next (Param_Spec);
4611 end loop;
4612 end if;
4614 -- Immediate error if too many formals. Other mismatches in number or
4615 -- types of parameters are detected when we analyze the body of the
4616 -- subprogram that we construct.
4618 if Form_Num > 2 then
4619 Error_Msg_N ("too many formals for attribute", N);
4621 -- Error if the attribute reference has expressions that look like
4622 -- formal parameters.
4624 elsif Present (Expressions (Nam)) then
4625 Error_Msg_N ("illegal expressions in attribute reference", Nam);
4627 elsif Aname in Name_Compose | Name_Exponent | Name_Leading_Part |
4628 Name_Pos | Name_Round | Name_Scaling |
4629 Name_Val
4630 then
4631 if Nkind (N) = N_Subprogram_Renaming_Declaration
4632 and then Present (Corresponding_Formal_Spec (N))
4633 then
4634 Error_Msg_N
4635 ("generic actual cannot be attribute involving universal type",
4636 Nam);
4637 else
4638 Error_Msg_N
4639 ("attribute involving a universal type cannot be renamed",
4640 Nam);
4641 end if;
4642 end if;
4644 -- Rewrite attribute node to have a list of expressions corresponding to
4645 -- the subprogram formals. A renaming declaration is not a freeze point,
4646 -- and the analysis of the attribute reference should not freeze the
4647 -- type of the prefix. We use the original node in the renaming so that
4648 -- its source location is preserved, and checks on stream attributes are
4649 -- properly applied.
4651 Attr_Node := Relocate_Node (Nam);
4652 Set_Expressions (Attr_Node, Expr_List);
4654 Set_Must_Not_Freeze (Attr_Node);
4655 Set_Must_Not_Freeze (Prefix (Nam));
4657 -- Case of renaming a function
4659 if Nkind (Spec) = N_Function_Specification then
4660 if Is_Procedure_Attribute_Name (Aname) then
4661 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
4662 return;
4663 end if;
4665 Find_Type (Result_Definition (Spec));
4666 Rewrite (Result_Definition (Spec),
4667 New_Occurrence_Of
4668 (Base_Type (Entity (Result_Definition (Spec))), Loc));
4670 Body_Node :=
4671 Make_Subprogram_Body (Loc,
4672 Specification => Spec,
4673 Declarations => New_List,
4674 Handled_Statement_Sequence =>
4675 Make_Handled_Sequence_Of_Statements (Loc,
4676 Statements => New_List (
4677 Make_Simple_Return_Statement (Loc,
4678 Expression => Attr_Node))));
4680 -- Case of renaming a procedure
4682 else
4683 if not Is_Procedure_Attribute_Name (Aname) then
4684 Error_Msg_N ("attribute can only be renamed as function", Nam);
4685 return;
4686 end if;
4688 Body_Node :=
4689 Make_Subprogram_Body (Loc,
4690 Specification => Spec,
4691 Declarations => New_List,
4692 Handled_Statement_Sequence =>
4693 Make_Handled_Sequence_Of_Statements (Loc,
4694 Statements => New_List (Attr_Node)));
4695 end if;
4697 -- Signal the ABE mechanism that the generated subprogram body has not
4698 -- ABE ramifications.
4700 Set_Was_Attribute_Reference (Body_Node);
4702 -- In case of tagged types we add the body of the generated function to
4703 -- the freezing actions of the type (because in the general case such
4704 -- type is still not frozen). We exclude from this processing generic
4705 -- formal subprograms found in instantiations.
4707 -- We must exclude restricted run-time libraries because
4708 -- entity AST_Handler is defined in package System.Aux_Dec which is not
4709 -- available in those platforms. Note that we cannot use the function
4710 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
4711 -- the ZFP run-time library is not defined as a profile, and we do not
4712 -- want to deal with AST_Handler in ZFP mode.
4714 if not Configurable_Run_Time_Mode
4715 and then No (Corresponding_Formal_Spec (N))
4716 and then not Is_RTE (Etype (Nam), RE_AST_Handler)
4717 then
4718 declare
4719 P : constant Node_Id := Prefix (Nam);
4721 begin
4722 -- The prefix of 'Img is an object that is evaluated for each call
4723 -- of the function that renames it.
4725 if Aname = Name_Img then
4726 Preanalyze_And_Resolve (P);
4728 -- For all other attribute renamings, the prefix is a subtype
4730 else
4731 Find_Type (P);
4732 end if;
4734 -- If the target type is not yet frozen, add the body to the
4735 -- actions to be elaborated at freeze time.
4737 if Is_Tagged_Type (Etype (P))
4738 and then In_Open_Scopes (Scope (Etype (P)))
4739 then
4740 Append_Freeze_Action (Etype (P), Body_Node);
4741 else
4742 Rewrite (N, Body_Node);
4743 Analyze (N);
4744 Set_Etype (New_S, Base_Type (Etype (New_S)));
4745 end if;
4746 end;
4748 -- Generic formal subprograms or AST_Handler renaming
4750 else
4751 Rewrite (N, Body_Node);
4752 Analyze (N);
4753 Set_Etype (New_S, Base_Type (Etype (New_S)));
4754 end if;
4756 if Is_Compilation_Unit (New_S) then
4757 Error_Msg_N
4758 ("a library unit can only rename another library unit", N);
4759 end if;
4761 -- We suppress elaboration warnings for the resulting entity, since
4762 -- clearly they are not needed, and more particularly, in the case
4763 -- of a generic formal subprogram, the resulting entity can appear
4764 -- after the instantiation itself, and thus look like a bogus case
4765 -- of access before elaboration.
4767 if Legacy_Elaboration_Checks then
4768 Set_Suppress_Elaboration_Warnings (New_S);
4769 end if;
4770 end Attribute_Renaming;
4772 ----------------------
4773 -- Chain_Use_Clause --
4774 ----------------------
4776 procedure Chain_Use_Clause (N : Node_Id) is
4777 Level : Int := Scope_Stack.Last;
4778 Pack : Entity_Id;
4780 begin
4781 -- Common case
4783 if not Is_Compilation_Unit (Current_Scope)
4784 or else not Is_Child_Unit (Current_Scope)
4785 then
4786 null;
4788 -- Common case for compilation unit
4790 elsif Defining_Entity (Parent (N)) = Current_Scope then
4791 null;
4793 else
4794 -- If declaration appears in some other scope, it must be in some
4795 -- parent unit when compiling a child.
4797 Pack := Defining_Entity (Parent (N));
4799 if not In_Open_Scopes (Pack) then
4800 null;
4802 -- If the use clause appears in an ancestor and we are in the
4803 -- private part of the immediate parent, the use clauses are
4804 -- already installed.
4806 elsif Pack /= Scope (Current_Scope)
4807 and then In_Private_Part (Scope (Current_Scope))
4808 then
4809 null;
4811 else
4812 -- Find entry for parent unit in scope stack
4814 while Scope_Stack.Table (Level).Entity /= Pack loop
4815 Level := Level - 1;
4816 end loop;
4817 end if;
4818 end if;
4820 Set_Next_Use_Clause (N,
4821 Scope_Stack.Table (Level).First_Use_Clause);
4822 Scope_Stack.Table (Level).First_Use_Clause := N;
4823 end Chain_Use_Clause;
4825 ---------------------------
4826 -- Check_Frozen_Renaming --
4827 ---------------------------
4829 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
4830 B_Node : Node_Id;
4831 Old_S : Entity_Id;
4833 begin
4834 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
4835 B_Node :=
4836 Build_Renamed_Body
4837 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
4839 if Is_Entity_Name (Name (N)) then
4840 Old_S := Entity (Name (N));
4842 if not Is_Frozen (Old_S)
4843 and then Operating_Mode /= Check_Semantics
4844 then
4845 Append_Freeze_Action (Old_S, B_Node);
4846 else
4847 Insert_After (N, B_Node);
4848 Analyze (B_Node);
4849 end if;
4851 if Is_Intrinsic_Subprogram (Old_S)
4852 and then not In_Instance
4853 and then not Relaxed_RM_Semantics
4854 then
4855 Error_Msg_N
4856 ("subprogram used in renaming_as_body cannot be intrinsic",
4857 Name (N));
4858 end if;
4860 else
4861 Insert_After (N, B_Node);
4862 Analyze (B_Node);
4863 end if;
4864 end if;
4865 end Check_Frozen_Renaming;
4867 -------------------------------
4868 -- Set_Entity_Or_Discriminal --
4869 -------------------------------
4871 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4872 P : Node_Id;
4874 begin
4875 -- If the entity is not a discriminant, or else expansion is disabled,
4876 -- simply set the entity.
4878 if not In_Spec_Expression
4879 or else Ekind (E) /= E_Discriminant
4880 or else Inside_A_Generic
4881 then
4882 Set_Entity_With_Checks (N, E);
4884 -- The replacement of a discriminant by the corresponding discriminal
4885 -- is not done for a task discriminant that appears in a default
4886 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4887 -- for details on their handling.
4889 elsif Is_Concurrent_Type (Scope (E)) then
4890 P := Parent (N);
4891 while Present (P)
4892 and then Nkind (P) not in
4893 N_Parameter_Specification | N_Component_Declaration
4894 loop
4895 P := Parent (P);
4896 end loop;
4898 if Present (P)
4899 and then Nkind (P) = N_Parameter_Specification
4900 then
4901 null;
4903 -- Don't replace a non-qualified discriminant in strict preanalysis
4904 -- mode since it can lead to errors during full analysis when the
4905 -- discriminant gets referenced later.
4907 -- This can occur in situations where a protected type contains
4908 -- an expression function which references a non-prefixed
4909 -- discriminant.
4911 elsif No (P)
4912 and then Preanalysis_Active
4913 and then Inside_Preanalysis_Without_Freezing = 0
4914 then
4915 null;
4917 else
4918 Set_Entity (N, Discriminal (E));
4919 end if;
4921 -- Otherwise, this is a discriminant in a context in which
4922 -- it is a reference to the corresponding parameter of the
4923 -- init proc for the enclosing type.
4925 else
4926 Set_Entity (N, Discriminal (E));
4927 end if;
4928 end Set_Entity_Or_Discriminal;
4930 -----------------------------------
4931 -- Check_In_Previous_With_Clause --
4932 -----------------------------------
4934 procedure Check_In_Previous_With_Clause (N, Nam : Node_Id) is
4935 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4936 Item : Node_Id;
4937 Par : Node_Id;
4939 begin
4940 Item := First (Context_Items (Parent (N)));
4941 while Present (Item) and then Item /= N loop
4942 if Nkind (Item) = N_With_Clause
4944 -- Protect the frontend against previous critical errors
4946 and then Nkind (Name (Item)) /= N_Selected_Component
4947 and then Entity (Name (Item)) = Pack
4948 then
4949 Par := Nam;
4951 -- Find root library unit in with_clause
4953 while Nkind (Par) = N_Expanded_Name loop
4954 Par := Prefix (Par);
4955 end loop;
4957 if Is_Child_Unit (Entity (Original_Node (Par))) then
4958 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4959 else
4960 return;
4961 end if;
4962 end if;
4964 Next (Item);
4965 end loop;
4967 -- On exit, package is not mentioned in a previous with_clause.
4968 -- Check if its prefix is.
4970 if Nkind (Nam) = N_Expanded_Name then
4971 Check_In_Previous_With_Clause (N, Prefix (Nam));
4973 elsif Pack /= Any_Id then
4974 Error_Msg_NE ("& is not visible", Nam, Pack);
4975 end if;
4976 end Check_In_Previous_With_Clause;
4978 ---------------------------------
4979 -- Check_Library_Unit_Renaming --
4980 ---------------------------------
4982 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4983 New_E : Entity_Id;
4985 begin
4986 if Nkind (Parent (N)) /= N_Compilation_Unit then
4987 return;
4989 -- Check for library unit. Note that we used to check for the scope
4990 -- being Standard here, but that was wrong for Standard itself.
4992 elsif not Is_Compilation_Unit (Old_E)
4993 and then not Is_Child_Unit (Old_E)
4994 then
4995 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4997 -- Entities defined in Standard (operators and boolean literals) cannot
4998 -- be renamed as library units.
5000 elsif Scope (Old_E) = Standard_Standard
5001 and then Sloc (Old_E) = Standard_Location
5002 then
5003 Error_Msg_N ("renamed unit must be a library unit", Name (N));
5005 elsif Present (Parent_Spec (N))
5006 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
5007 and then not Is_Child_Unit (Old_E)
5008 then
5009 Error_Msg_N
5010 ("renamed unit must be a child unit of generic parent", Name (N));
5012 elsif Nkind (N) in N_Generic_Renaming_Declaration
5013 and then Nkind (Name (N)) = N_Expanded_Name
5014 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
5015 and then Is_Generic_Unit (Old_E)
5016 then
5017 Error_Msg_N
5018 ("renamed generic unit must be a library unit", Name (N));
5020 elsif Is_Package_Or_Generic_Package (Old_E) then
5022 -- Inherit categorization flags
5024 New_E := Defining_Entity (N);
5025 Set_Is_Pure (New_E, Is_Pure (Old_E));
5026 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
5027 Set_Is_Remote_Call_Interface (New_E,
5028 Is_Remote_Call_Interface (Old_E));
5029 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
5030 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
5031 end if;
5032 end Check_Library_Unit_Renaming;
5034 ------------------------
5035 -- Enclosing_Instance --
5036 ------------------------
5038 function Enclosing_Instance return Entity_Id is
5039 S : Entity_Id;
5041 begin
5042 if not Is_Generic_Instance (Current_Scope) then
5043 return Empty;
5044 end if;
5046 S := Scope (Current_Scope);
5047 while S /= Standard_Standard loop
5048 if Is_Generic_Instance (S) then
5049 return S;
5050 end if;
5052 S := Scope (S);
5053 end loop;
5055 return Empty;
5056 end Enclosing_Instance;
5058 ---------------
5059 -- End_Scope --
5060 ---------------
5062 procedure End_Scope is
5063 Id : Entity_Id;
5064 Prev : Entity_Id;
5065 Outer : Entity_Id;
5067 begin
5068 Id := First_Entity (Current_Scope);
5069 while Present (Id) loop
5070 -- An entity in the current scope is not necessarily the first one
5071 -- on its homonym chain. Find its predecessor if any,
5072 -- If it is an internal entity, it will not be in the visibility
5073 -- chain altogether, and there is nothing to unchain.
5075 if Id /= Current_Entity (Id) then
5076 Prev := Current_Entity (Id);
5077 while Present (Prev)
5078 and then Homonym (Prev) /= Id
5079 loop
5080 Prev := Homonym (Prev);
5081 end loop;
5083 -- Skip to end of loop if Id is not in the visibility chain
5085 if No (Prev) then
5086 goto Next_Ent;
5087 end if;
5089 else
5090 Prev := Empty;
5091 end if;
5093 Set_Is_Immediately_Visible (Id, False);
5095 Outer := Homonym (Id);
5096 while Present (Outer) and then Scope (Outer) = Current_Scope loop
5097 Outer := Homonym (Outer);
5098 end loop;
5100 -- Reset homonym link of other entities, but do not modify link
5101 -- between entities in current scope, so that the back-end can have
5102 -- a proper count of local overloadings.
5104 if No (Prev) then
5105 Set_Name_Entity_Id (Chars (Id), Outer);
5107 elsif Scope (Prev) /= Scope (Id) then
5108 Set_Homonym (Prev, Outer);
5109 end if;
5111 <<Next_Ent>>
5112 Next_Entity (Id);
5113 end loop;
5115 -- If the scope generated freeze actions, place them before the
5116 -- current declaration and analyze them. Type declarations and
5117 -- the bodies of initialization procedures can generate such nodes.
5118 -- We follow the parent chain until we reach a list node, which is
5119 -- the enclosing list of declarations. If the list appears within
5120 -- a protected definition, move freeze nodes outside the protected
5121 -- type altogether.
5123 if Present
5124 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
5125 then
5126 declare
5127 Decl : Node_Id;
5128 L : constant List_Id := Scope_Stack.Table
5129 (Scope_Stack.Last).Pending_Freeze_Actions;
5131 begin
5132 if Is_Itype (Current_Scope) then
5133 Decl := Associated_Node_For_Itype (Current_Scope);
5134 else
5135 Decl := Parent (Current_Scope);
5136 end if;
5138 Pop_Scope;
5140 while not Is_List_Member (Decl)
5141 or else Nkind (Parent (Decl)) in N_Protected_Definition
5142 | N_Task_Definition
5143 loop
5144 Decl := Parent (Decl);
5145 end loop;
5147 Insert_List_Before_And_Analyze (Decl, L);
5148 end;
5150 else
5151 Pop_Scope;
5152 end if;
5153 end End_Scope;
5155 ---------------------
5156 -- End_Use_Clauses --
5157 ---------------------
5159 procedure End_Use_Clauses (Clause : Node_Id) is
5160 U : Node_Id;
5162 begin
5163 -- Remove use_type_clauses first, because they affect the visibility of
5164 -- operators in subsequent used packages.
5166 U := Clause;
5167 while Present (U) loop
5168 if Nkind (U) = N_Use_Type_Clause then
5169 End_Use_Type (U);
5170 end if;
5172 Next_Use_Clause (U);
5173 end loop;
5175 U := Clause;
5176 while Present (U) loop
5177 if Nkind (U) = N_Use_Package_Clause then
5178 End_Use_Package (U);
5179 end if;
5181 Next_Use_Clause (U);
5182 end loop;
5183 end End_Use_Clauses;
5185 ---------------------
5186 -- End_Use_Package --
5187 ---------------------
5189 procedure End_Use_Package (N : Node_Id) is
5190 Pack : Entity_Id;
5191 Pack_Name : Node_Id;
5192 Id : Entity_Id;
5193 Elmt : Elmt_Id;
5195 function Is_Primitive_Operator_In_Use
5196 (Op : Entity_Id;
5197 F : Entity_Id) return Boolean;
5198 -- Check whether Op is a primitive operator of a use-visible type
5200 ----------------------------------
5201 -- Is_Primitive_Operator_In_Use --
5202 ----------------------------------
5204 function Is_Primitive_Operator_In_Use
5205 (Op : Entity_Id;
5206 F : Entity_Id) return Boolean
5208 T : constant Entity_Id := Base_Type (Etype (F));
5209 begin
5210 return In_Use (T) and then Scope (T) = Scope (Op);
5211 end Is_Primitive_Operator_In_Use;
5213 -- Start of processing for End_Use_Package
5215 begin
5216 Pack_Name := Name (N);
5218 -- Test that Pack_Name actually denotes a package before processing
5220 if Is_Entity_Name (Pack_Name)
5221 and then Ekind (Entity (Pack_Name)) = E_Package
5222 then
5223 Pack := Entity (Pack_Name);
5225 if In_Open_Scopes (Pack) then
5226 null;
5228 elsif not Redundant_Use (Pack_Name) then
5229 Set_In_Use (Pack, False);
5230 Set_Current_Use_Clause (Pack, Empty);
5232 Id := First_Entity (Pack);
5233 while Present (Id) loop
5235 -- Preserve use-visibility of operators that are primitive
5236 -- operators of a type that is use-visible through an active
5237 -- use_type_clause.
5239 if Nkind (Id) = N_Defining_Operator_Symbol
5240 and then
5241 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
5242 or else
5243 (Present (Next_Formal (First_Formal (Id)))
5244 and then
5245 Is_Primitive_Operator_In_Use
5246 (Id, Next_Formal (First_Formal (Id)))))
5247 then
5248 null;
5249 else
5250 Set_Is_Potentially_Use_Visible (Id, False);
5251 end if;
5253 if Is_Private_Type (Id)
5254 and then Present (Full_View (Id))
5255 then
5256 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
5257 end if;
5259 Next_Entity (Id);
5260 end loop;
5262 if Present (Renamed_Entity (Pack)) then
5263 Set_In_Use (Renamed_Entity (Pack), False);
5264 Set_Current_Use_Clause (Renamed_Entity (Pack), Empty);
5265 end if;
5267 if Chars (Pack) = Name_System
5268 and then Scope (Pack) = Standard_Standard
5269 and then Present_System_Aux
5270 then
5271 Id := First_Entity (System_Aux_Id);
5272 while Present (Id) loop
5273 Set_Is_Potentially_Use_Visible (Id, False);
5275 if Is_Private_Type (Id)
5276 and then Present (Full_View (Id))
5277 then
5278 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
5279 end if;
5281 Next_Entity (Id);
5282 end loop;
5284 Set_In_Use (System_Aux_Id, False);
5285 end if;
5286 else
5287 Set_Redundant_Use (Pack_Name, False);
5288 end if;
5289 end if;
5291 if Present (Hidden_By_Use_Clause (N)) then
5292 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
5293 while Present (Elmt) loop
5294 declare
5295 E : constant Entity_Id := Node (Elmt);
5297 begin
5298 -- Reset either Use_Visibility or Direct_Visibility, depending
5299 -- on how the entity was hidden by the use clause.
5301 if In_Use (Scope (E))
5302 and then Used_As_Generic_Actual (Scope (E))
5303 then
5304 Set_Is_Potentially_Use_Visible (Node (Elmt));
5305 else
5306 Set_Is_Immediately_Visible (Node (Elmt));
5307 end if;
5309 Next_Elmt (Elmt);
5310 end;
5311 end loop;
5313 Set_Hidden_By_Use_Clause (N, No_Elist);
5314 end if;
5315 end End_Use_Package;
5317 ------------------
5318 -- End_Use_Type --
5319 ------------------
5321 procedure End_Use_Type (N : Node_Id) is
5322 Elmt : Elmt_Id;
5323 Id : Entity_Id;
5324 T : Entity_Id;
5326 -- Start of processing for End_Use_Type
5328 begin
5329 Id := Subtype_Mark (N);
5331 -- A call to Rtsfind may occur while analyzing a use_type_clause, in
5332 -- which case the type marks are not resolved yet, so guard against that
5333 -- here.
5335 if Is_Entity_Name (Id) and then Present (Entity (Id)) then
5336 T := Entity (Id);
5338 if T = Any_Type or else From_Limited_With (T) then
5339 null;
5341 -- Note that the use_type_clause may mention a subtype of the type
5342 -- whose primitive operations have been made visible. Here as
5343 -- elsewhere, it is the base type that matters for visibility.
5345 elsif In_Open_Scopes (Scope (Base_Type (T))) then
5346 null;
5348 elsif not Redundant_Use (Id) then
5349 Set_In_Use (T, False);
5350 Set_In_Use (Base_Type (T), False);
5351 Set_Current_Use_Clause (T, Empty);
5352 Set_Current_Use_Clause (Base_Type (T), Empty);
5354 -- See Use_One_Type for the rationale. This is a bit on the naive
5355 -- side, but should be good enough in practice.
5357 if Is_Tagged_Type (T) then
5358 Set_In_Use (Class_Wide_Type (T), False);
5359 end if;
5360 end if;
5361 end if;
5363 if Is_Empty_Elmt_List (Used_Operations (N)) then
5364 return;
5366 else
5367 Elmt := First_Elmt (Used_Operations (N));
5368 while Present (Elmt) loop
5369 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
5370 Next_Elmt (Elmt);
5371 end loop;
5372 end if;
5373 end End_Use_Type;
5375 --------------------
5376 -- Entity_Of_Unit --
5377 --------------------
5379 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
5380 begin
5381 if Nkind (U) = N_Package_Instantiation and then Analyzed (U) then
5382 return Defining_Entity (Instance_Spec (U));
5383 else
5384 return Defining_Entity (U);
5385 end if;
5386 end Entity_Of_Unit;
5388 --------------------------------------
5389 -- Error_Missing_With_Of_Known_Unit --
5390 --------------------------------------
5392 procedure Error_Missing_With_Of_Known_Unit (Pkg : Node_Id) is
5393 Selectors : array (1 .. 6) of Node_Id;
5394 -- Contains the chars of the full package name up to maximum number
5395 -- allowed as per Errout.Error_Msg_Name_# variables.
5397 Count : Integer := Selectors'First;
5398 -- Count of selector names forming the full package name
5400 Current_Pkg : Node_Id := Parent (Pkg);
5402 begin
5403 Selectors (Count) := Pkg;
5405 -- Gather all the selectors we can display
5407 while Nkind (Current_Pkg) = N_Selected_Component
5408 and then Is_Known_Unit (Current_Pkg)
5409 and then Count < Selectors'Length
5410 loop
5411 Count := Count + 1;
5412 Selectors (Count) := Selector_Name (Current_Pkg);
5413 Current_Pkg := Parent (Current_Pkg);
5414 end loop;
5416 -- Display the error message based on the number of selectors found
5418 case Count is
5419 when 1 =>
5420 Error_Msg_Node_1 := Selectors (1);
5421 Error_Msg_N -- CODEFIX
5422 ("\\missing `WITH &;`", Pkg);
5423 when 2 =>
5424 Error_Msg_Node_1 := Selectors (1);
5425 Error_Msg_Node_2 := Selectors (2);
5426 Error_Msg_N -- CODEFIX
5427 ("\\missing `WITH &.&;`", Pkg);
5428 when 3 =>
5429 Error_Msg_Node_1 := Selectors (1);
5430 Error_Msg_Node_2 := Selectors (2);
5431 Error_Msg_Node_3 := Selectors (3);
5432 Error_Msg_N -- CODEFIX
5433 ("\\missing `WITH &.&.&;`", Pkg);
5434 when 4 =>
5435 Error_Msg_Node_1 := Selectors (1);
5436 Error_Msg_Node_2 := Selectors (2);
5437 Error_Msg_Node_3 := Selectors (3);
5438 Error_Msg_Node_3 := Selectors (4);
5439 Error_Msg_N -- CODEFIX
5440 ("\\missing `WITH &.&.&.&;`", Pkg);
5441 when 5 =>
5442 Error_Msg_Node_1 := Selectors (1);
5443 Error_Msg_Node_2 := Selectors (2);
5444 Error_Msg_Node_3 := Selectors (3);
5445 Error_Msg_Node_3 := Selectors (4);
5446 Error_Msg_Node_3 := Selectors (5);
5447 Error_Msg_N -- CODEFIX
5448 ("\\missing `WITH &.&.&.&.&;`", Pkg);
5449 when 6 =>
5450 Error_Msg_Node_1 := Selectors (1);
5451 Error_Msg_Node_2 := Selectors (2);
5452 Error_Msg_Node_3 := Selectors (3);
5453 Error_Msg_Node_4 := Selectors (4);
5454 Error_Msg_Node_5 := Selectors (5);
5455 Error_Msg_Node_6 := Selectors (6);
5456 Error_Msg_N -- CODEFIX
5457 ("\\missing `WITH &.&.&.&.&.&;`", Pkg);
5458 when others =>
5459 raise Program_Error;
5460 end case;
5461 end Error_Missing_With_Of_Known_Unit;
5463 --------------------
5464 -- Is_Self_Hidden --
5465 --------------------
5467 function Is_Self_Hidden (E : Entity_Id) return Boolean is
5468 begin
5469 if Is_Not_Self_Hidden (E) then
5470 return Ekind (E) = E_Void;
5471 else
5472 return True;
5473 end if;
5474 end Is_Self_Hidden;
5476 ----------------------
5477 -- Find_Direct_Name --
5478 ----------------------
5480 procedure Find_Direct_Name (N : Node_Id) is
5481 E : Entity_Id;
5482 E2 : Entity_Id;
5483 Msg : Boolean;
5485 Homonyms : Entity_Id;
5486 -- Saves start of homonym chain
5488 Inst : Entity_Id := Empty;
5489 -- Enclosing instance, if any
5491 Nvis_Entity : Boolean;
5492 -- Set True to indicate that there is at least one entity on the homonym
5493 -- chain which, while not visible, is visible enough from the user point
5494 -- of view to warrant an error message of "not visible" rather than
5495 -- undefined.
5497 Nvis_Is_Private_Subprg : Boolean := False;
5498 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
5499 -- effect concerning library subprograms has been detected. Used to
5500 -- generate the precise error message.
5502 function From_Actual_Package (E : Entity_Id) return Boolean;
5503 -- Returns true if the entity is an actual for a package that is itself
5504 -- an actual for a formal package of the current instance. Such an
5505 -- entity requires special handling because it may be use-visible but
5506 -- hides directly visible entities defined outside the instance, because
5507 -- the corresponding formal did so in the generic.
5509 function Is_Actual_Parameter return Boolean;
5510 -- This function checks if the node N is an identifier that is an actual
5511 -- parameter of a procedure call. If so it returns True, otherwise it
5512 -- return False. The reason for this check is that at this stage we do
5513 -- not know what procedure is being called if the procedure might be
5514 -- overloaded, so it is premature to go setting referenced flags or
5515 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
5516 -- for that processing.
5517 -- Note: there is a similar routine Sem_Util.Is_Actual_Parameter, but
5518 -- it works for both function and procedure calls, while here we are
5519 -- only concerned with procedure calls (and with entry calls as well,
5520 -- but they are parsed as procedure calls and only later rewritten to
5521 -- entry calls).
5523 function Known_But_Invisible (E : Entity_Id) return Boolean;
5524 -- This function determines whether a reference to the entity E, which
5525 -- is not visible, can reasonably be considered to be known to the
5526 -- writer of the reference. This is a heuristic test, used only for
5527 -- the purposes of figuring out whether we prefer to complain that an
5528 -- entity is undefined or invisible (and identify the declaration of
5529 -- the invisible entity in the latter case). The point here is that we
5530 -- don't want to complain that something is invisible and then point to
5531 -- something entirely mysterious to the writer.
5533 procedure Nvis_Messages;
5534 -- Called if there are no visible entries for N, but there is at least
5535 -- one non-directly visible, or hidden declaration. This procedure
5536 -- outputs an appropriate set of error messages.
5538 procedure Undefined (Nvis : Boolean);
5539 -- This function is called if the current node has no corresponding
5540 -- visible entity or entities. The value set in Msg indicates whether
5541 -- an error message was generated (multiple error messages for the
5542 -- same variable are generally suppressed, see body for details).
5543 -- Msg is True if an error message was generated, False if not. This
5544 -- value is used by the caller to determine whether or not to output
5545 -- additional messages where appropriate. The parameter is set False
5546 -- to get the message "X is undefined", and True to get the message
5547 -- "X is not visible".
5549 -------------------------
5550 -- From_Actual_Package --
5551 -------------------------
5553 function From_Actual_Package (E : Entity_Id) return Boolean is
5554 Scop : constant Entity_Id := Scope (E);
5555 -- Declared scope of candidate entity
5557 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
5558 -- Recursive function that does the work and examines actuals of
5559 -- actual packages of current instance.
5561 ------------------------
5562 -- Declared_In_Actual --
5563 ------------------------
5565 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
5566 pragma Assert (Ekind (Pack) = E_Package);
5567 Act : Entity_Id;
5568 begin
5569 if No (Associated_Formal_Package (Pack)) then
5570 return False;
5572 else
5573 Act := First_Entity (Pack);
5574 while Present (Act) loop
5575 if Renamed_Entity (Pack) = Scop then
5576 return True;
5578 -- Check for end of list of actuals
5580 elsif Ekind (Act) = E_Package
5581 and then Renamed_Entity (Act) = Pack
5582 then
5583 return False;
5585 elsif Ekind (Act) = E_Package
5586 and then Declared_In_Actual (Act)
5587 then
5588 return True;
5589 end if;
5591 Next_Entity (Act);
5592 end loop;
5594 return False;
5595 end if;
5596 end Declared_In_Actual;
5598 -- Local variables
5600 Act : Entity_Id;
5602 -- Start of processing for From_Actual_Package
5604 begin
5605 if not In_Instance then
5606 return False;
5608 else
5609 Inst := Current_Scope;
5610 while Present (Inst)
5611 and then Ekind (Inst) /= E_Package
5612 and then not Is_Generic_Instance (Inst)
5613 loop
5614 Inst := Scope (Inst);
5615 end loop;
5617 if No (Inst) then
5618 return False;
5619 end if;
5621 Act := First_Entity (Inst);
5622 while Present (Act) loop
5623 if Ekind (Act) = E_Package
5624 and then Declared_In_Actual (Act)
5625 then
5626 return True;
5627 end if;
5629 Next_Entity (Act);
5630 end loop;
5632 return False;
5633 end if;
5634 end From_Actual_Package;
5636 -------------------------
5637 -- Is_Actual_Parameter --
5638 -------------------------
5640 function Is_Actual_Parameter return Boolean is
5641 begin
5642 if Nkind (N) = N_Identifier then
5643 case Nkind (Parent (N)) is
5644 when N_Procedure_Call_Statement =>
5645 return Is_List_Member (N)
5646 and then List_Containing (N) =
5647 Parameter_Associations (Parent (N));
5649 when N_Parameter_Association =>
5650 return N = Explicit_Actual_Parameter (Parent (N))
5651 and then Nkind (Parent (Parent (N))) =
5652 N_Procedure_Call_Statement;
5654 when others =>
5655 return False;
5656 end case;
5657 else
5658 return False;
5659 end if;
5660 end Is_Actual_Parameter;
5662 -------------------------
5663 -- Known_But_Invisible --
5664 -------------------------
5666 function Known_But_Invisible (E : Entity_Id) return Boolean is
5667 Fname : File_Name_Type;
5669 begin
5670 -- Entities in Standard are always considered to be known
5672 if Sloc (E) <= Standard_Location then
5673 return True;
5675 -- An entity that does not come from source is always considered
5676 -- to be unknown, since it is an artifact of code expansion.
5678 elsif not Comes_From_Source (E) then
5679 return False;
5680 end if;
5682 -- Here we have an entity that is not from package Standard, and
5683 -- which comes from Source. See if it comes from an internal file.
5685 Fname := Unit_File_Name (Get_Source_Unit (E));
5687 -- Case of from internal file
5689 if In_Internal_Unit (E) then
5691 -- Private part entities in internal files are never considered
5692 -- to be known to the writer of normal application code.
5694 if Is_Hidden (E) then
5695 return False;
5696 end if;
5698 -- Entities from System packages other than System and
5699 -- System.Storage_Elements are not considered to be known.
5700 -- System.Auxxxx files are also considered known to the user.
5702 -- Should refine this at some point to generally distinguish
5703 -- between known and unknown internal files ???
5705 Get_Name_String (Fname);
5707 return
5708 Name_Len < 2
5709 or else
5710 Name_Buffer (1 .. 2) /= "s-"
5711 or else
5712 Name_Buffer (3 .. 8) = "stoele"
5713 or else
5714 Name_Buffer (3 .. 5) = "aux";
5716 -- If not an internal file, then entity is definitely known, even if
5717 -- it is in a private part (the message generated will note that it
5718 -- is in a private part).
5720 else
5721 return True;
5722 end if;
5723 end Known_But_Invisible;
5725 -------------------
5726 -- Nvis_Messages --
5727 -------------------
5729 procedure Nvis_Messages is
5730 Comp_Unit : Node_Id;
5731 Ent : Entity_Id;
5732 Found : Boolean := False;
5733 Hidden : Boolean := False;
5734 Item : Node_Id;
5736 begin
5737 -- Ada 2005 (AI-262): Generate a precise error concerning the
5738 -- Beaujolais effect that was previously detected
5740 if Nvis_Is_Private_Subprg then
5742 pragma Assert (Nkind (E2) = N_Defining_Identifier
5743 and then Ekind (E2) = E_Function
5744 and then Scope (E2) = Standard_Standard
5745 and then Has_Private_With (E2));
5747 -- Find the sloc corresponding to the private with'ed unit
5749 Comp_Unit := Cunit (Current_Sem_Unit);
5750 Error_Msg_Sloc := No_Location;
5752 Item := First (Context_Items (Comp_Unit));
5753 while Present (Item) loop
5754 if Nkind (Item) = N_With_Clause
5755 and then Private_Present (Item)
5756 and then Entity (Name (Item)) = E2
5757 then
5758 Error_Msg_Sloc := Sloc (Item);
5759 exit;
5760 end if;
5762 Next (Item);
5763 end loop;
5765 pragma Assert (Error_Msg_Sloc /= No_Location);
5767 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
5768 return;
5769 end if;
5771 Undefined (Nvis => True);
5773 if Msg then
5775 -- First loop does hidden declarations
5777 Ent := Homonyms;
5778 while Present (Ent) loop
5779 if Is_Potentially_Use_Visible (Ent) then
5780 if not Hidden then
5781 Error_Msg_N -- CODEFIX
5782 ("multiple use clauses cause hiding!", N);
5783 Hidden := True;
5784 end if;
5786 Error_Msg_Sloc := Sloc (Ent);
5787 Error_Msg_N -- CODEFIX
5788 ("hidden declaration#!", N);
5789 end if;
5791 Ent := Homonym (Ent);
5792 end loop;
5794 -- If we found hidden declarations, then that's enough, don't
5795 -- bother looking for non-visible declarations as well.
5797 if Hidden then
5798 return;
5799 end if;
5801 -- Second loop does non-directly visible declarations
5803 Ent := Homonyms;
5804 while Present (Ent) loop
5805 if not Is_Potentially_Use_Visible (Ent) then
5807 -- Do not bother the user with unknown entities
5809 if not Known_But_Invisible (Ent) then
5810 goto Continue;
5811 end if;
5813 Error_Msg_Sloc := Sloc (Ent);
5815 -- Output message noting that there is a non-visible
5816 -- declaration, distinguishing the private part case.
5818 if Is_Hidden (Ent) then
5819 Error_Msg_N ("non-visible (private) declaration#!", N);
5821 -- If the entity is declared in a generic package, it
5822 -- cannot be visible, so there is no point in adding it
5823 -- to the list of candidates if another homograph from a
5824 -- non-generic package has been seen.
5826 elsif Ekind (Scope (Ent)) = E_Generic_Package
5827 and then Found
5828 then
5829 null;
5831 else
5832 -- When the entity comes from a generic instance the
5833 -- normal error message machinery will give the line
5834 -- number of the generic package and the location of
5835 -- the generic instance, but not the name of the
5836 -- the instance.
5838 -- So, in order to give more descriptive error messages
5839 -- in this case, we include the name of the generic
5840 -- package.
5842 if Is_Generic_Instance (Scope (Ent)) then
5843 Error_Msg_Name_1 := Chars (Scope (Ent));
5844 Error_Msg_N -- CODEFIX
5845 ("non-visible declaration from %#!", N);
5847 -- Otherwise print the message normally
5849 else
5850 Error_Msg_N -- CODEFIX
5851 ("non-visible declaration#!", N);
5852 end if;
5854 if Ekind (Scope (Ent)) /= E_Generic_Package then
5855 Found := True;
5856 end if;
5858 if Is_Compilation_Unit (Ent)
5859 and then
5860 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5861 then
5862 Error_Msg_Qual_Level := 99;
5863 Error_Msg_NE -- CODEFIX
5864 ("\\missing `WITH &;`", N, Ent);
5865 Error_Msg_Qual_Level := 0;
5866 end if;
5868 if Ekind (Ent) = E_Discriminant
5869 and then Present (Corresponding_Discriminant (Ent))
5870 and then Scope (Corresponding_Discriminant (Ent)) =
5871 Etype (Scope (Ent))
5872 then
5873 Error_Msg_N
5874 ("inherited discriminant not allowed here" &
5875 " (RM 3.8 (12), 3.8.1 (6))!", N);
5876 end if;
5877 end if;
5879 -- Set entity and its containing package as referenced. We
5880 -- can't be sure of this, but this seems a better choice
5881 -- to avoid unused entity messages.
5883 if Comes_From_Source (Ent) then
5884 Set_Referenced (Ent);
5885 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5886 end if;
5887 end if;
5889 <<Continue>>
5890 Ent := Homonym (Ent);
5891 end loop;
5892 end if;
5893 end Nvis_Messages;
5895 ---------------
5896 -- Undefined --
5897 ---------------
5899 procedure Undefined (Nvis : Boolean) is
5900 Emsg : Error_Msg_Id;
5902 begin
5903 -- We should never find an undefined internal name. If we do, then
5904 -- see if we have previous errors. If so, ignore on the grounds that
5905 -- it is probably a cascaded message (e.g. a block label from a badly
5906 -- formed block). If no previous errors, then we have a real internal
5907 -- error of some kind so raise an exception.
5909 if Is_Internal_Name (Chars (N)) then
5910 if Total_Errors_Detected /= 0 then
5911 return;
5912 else
5913 raise Program_Error;
5914 end if;
5915 end if;
5917 -- A very specialized error check, if the undefined variable is
5918 -- a case tag, and the case type is an enumeration type, check
5919 -- for a possible misspelling, and if so, modify the identifier
5921 -- Named aggregate should also be handled similarly ???
5923 if Nkind (N) = N_Identifier
5924 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5925 then
5926 declare
5927 Case_Stm : constant Node_Id := Parent (Parent (N));
5928 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5930 Lit : Node_Id;
5932 begin
5933 if Is_Enumeration_Type (Case_Typ)
5934 and then not Is_Standard_Character_Type (Case_Typ)
5935 then
5936 Lit := First_Literal (Case_Typ);
5937 Get_Name_String (Chars (Lit));
5939 if Chars (Lit) /= Chars (N)
5940 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5941 then
5942 Error_Msg_Node_2 := Lit;
5943 Error_Msg_N -- CODEFIX
5944 ("& is undefined, assume misspelling of &", N);
5945 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5946 return;
5947 end if;
5949 Next_Literal (Lit);
5950 end if;
5951 end;
5952 end if;
5954 -- Normal processing
5956 Set_Entity (N, Any_Id);
5957 Set_Etype (N, Any_Type);
5959 -- We use the table Urefs to keep track of entities for which we
5960 -- have issued errors for undefined references. Multiple errors
5961 -- for a single name are normally suppressed, however we modify
5962 -- the error message to alert the programmer to this effect.
5964 for J in Urefs.First .. Urefs.Last loop
5965 if Chars (N) = Chars (Urefs.Table (J).Node) then
5966 if Urefs.Table (J).Err /= No_Error_Msg
5967 and then Sloc (N) /= Urefs.Table (J).Loc
5968 then
5969 Error_Msg_Node_1 := Urefs.Table (J).Node;
5971 if Urefs.Table (J).Nvis then
5972 Change_Error_Text (Urefs.Table (J).Err,
5973 "& is not visible (more references follow)");
5974 else
5975 Change_Error_Text (Urefs.Table (J).Err,
5976 "& is undefined (more references follow)");
5977 end if;
5979 Urefs.Table (J).Err := No_Error_Msg;
5980 end if;
5982 -- Although we will set Msg False, and thus suppress the
5983 -- message, we also set Error_Posted True, to avoid any
5984 -- cascaded messages resulting from the undefined reference.
5986 Msg := False;
5987 Set_Error_Posted (N);
5988 return;
5989 end if;
5990 end loop;
5992 -- If entry not found, this is first undefined occurrence
5994 if Nvis then
5995 Error_Msg_N ("& is not visible!", N);
5996 Emsg := Get_Msg_Id;
5998 else
5999 Error_Msg_N ("& is undefined!", N);
6000 Emsg := Get_Msg_Id;
6002 -- A very bizarre special check, if the undefined identifier
6003 -- is Put or Put_Line, then add a special error message (since
6004 -- this is a very common error for beginners to make).
6006 if Chars (N) in Name_Put | Name_Put_Line then
6007 Error_Msg_N -- CODEFIX
6008 ("\\possible missing `WITH Ada.Text_'I'O; " &
6009 "USE Ada.Text_'I'O`!", N);
6011 -- Another special check if N is the prefix of a selected
6012 -- component which is a known unit: add message complaining
6013 -- about missing with for this unit.
6015 elsif Nkind (Parent (N)) = N_Selected_Component
6016 and then N = Prefix (Parent (N))
6017 and then Is_Known_Unit (Parent (N))
6018 then
6019 Error_Missing_With_Of_Known_Unit (N);
6020 end if;
6022 -- Now check for possible misspellings
6024 declare
6025 E : Entity_Id;
6026 Ematch : Entity_Id := Empty;
6027 begin
6028 for Nam in First_Name_Id .. Last_Name_Id loop
6029 E := Get_Name_Entity_Id (Nam);
6031 if Present (E)
6032 and then (Is_Immediately_Visible (E)
6033 or else
6034 Is_Potentially_Use_Visible (E))
6035 then
6036 if Is_Bad_Spelling_Of (Chars (N), Nam) then
6037 Ematch := E;
6038 exit;
6039 end if;
6040 end if;
6041 end loop;
6043 if Present (Ematch) then
6044 Error_Msg_NE -- CODEFIX
6045 ("\possible misspelling of&", N, Ematch);
6046 end if;
6047 end;
6048 end if;
6050 -- Make entry in undefined references table unless the full errors
6051 -- switch is set, in which case by refraining from generating the
6052 -- table entry we guarantee that we get an error message for every
6053 -- undefined reference. The entry is not added if we are ignoring
6054 -- errors.
6056 if not All_Errors_Mode
6057 and then Ignore_Errors_Enable = 0
6058 and then not Get_Ignore_Errors
6059 then
6060 Urefs.Append (
6061 (Node => N,
6062 Err => Emsg,
6063 Nvis => Nvis,
6064 Loc => Sloc (N)));
6065 end if;
6067 Msg := True;
6068 end Undefined;
6070 -- Local variables
6072 Nested_Inst : Entity_Id := Empty;
6073 -- The entity of a nested instance which appears within Inst (if any)
6075 -- Start of processing for Find_Direct_Name
6077 begin
6078 -- If the entity pointer is already set, this is an internal node, or
6079 -- a node that is analyzed more than once, after a tree modification.
6080 -- In such a case there is no resolution to perform, just set the type.
6082 if Present (Entity (N)) then
6083 if Is_Type (Entity (N)) then
6084 Set_Etype (N, Entity (N));
6086 else
6087 declare
6088 Entyp : constant Entity_Id := Etype (Entity (N));
6090 begin
6091 -- One special case here. If the Etype field is already set,
6092 -- and references the packed array type corresponding to the
6093 -- etype of the referenced entity, then leave it alone. This
6094 -- happens for trees generated from Exp_Pakd, where expressions
6095 -- can be deliberately "mis-typed" to the packed array type.
6097 if Is_Packed_Array (Entyp)
6098 and then Present (Etype (N))
6099 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
6100 then
6101 null;
6103 -- If not that special case, then just reset the Etype
6105 else
6106 Set_Etype (N, Entyp);
6107 end if;
6108 end;
6109 end if;
6111 -- Although the marking of use clauses happens at the end of
6112 -- Find_Direct_Name, a certain case where a generic actual satisfies
6113 -- a use clause must be checked here due to how the generic machinery
6114 -- handles the analysis of said actuals.
6116 if In_Instance
6117 and then Nkind (Parent (N)) = N_Generic_Association
6118 then
6119 Mark_Use_Clauses (Entity (N));
6120 end if;
6122 return;
6123 end if;
6125 -- Preserve relevant elaboration-related attributes of the context which
6126 -- are no longer available or very expensive to recompute once analysis,
6127 -- resolution, and expansion are over.
6129 if Nkind (N) = N_Identifier then
6130 Mark_Elaboration_Attributes
6131 (N_Id => N,
6132 Checks => True,
6133 Modes => True,
6134 Warnings => True);
6135 end if;
6137 -- Here if Entity pointer was not set, we need full visibility analysis
6138 -- First we generate debugging output if the debug E flag is set.
6140 if Debug_Flag_E then
6141 Write_Str ("Looking for ");
6142 Write_Name (Chars (N));
6143 Write_Eol;
6144 end if;
6146 Homonyms := Current_Entity (N);
6147 Nvis_Entity := False;
6149 E := Homonyms;
6150 while Present (E) loop
6152 -- If entity is immediately visible or potentially use visible, then
6153 -- process the entity and we are done.
6155 if Is_Immediately_Visible (E) then
6156 goto Immediately_Visible_Entity;
6158 elsif Is_Potentially_Use_Visible (E) then
6159 goto Potentially_Use_Visible_Entity;
6161 -- Note if a known but invisible entity encountered
6163 elsif Known_But_Invisible (E) then
6164 Nvis_Entity := True;
6165 end if;
6167 -- Move to next entity in chain and continue search
6169 E := Homonym (E);
6170 end loop;
6172 -- If no entries on homonym chain that were potentially visible,
6173 -- and no entities reasonably considered as non-visible, then
6174 -- we have a plain undefined reference, with no additional
6175 -- explanation required.
6177 if not Nvis_Entity then
6178 Undefined (Nvis => False);
6180 -- Otherwise there is at least one entry on the homonym chain that
6181 -- is reasonably considered as being known and non-visible.
6183 else
6184 Nvis_Messages;
6185 end if;
6187 goto Done;
6189 -- Processing for a potentially use visible entry found. We must search
6190 -- the rest of the homonym chain for two reasons. First, if there is a
6191 -- directly visible entry, then none of the potentially use-visible
6192 -- entities are directly visible (RM 8.4(10)). Second, we need to check
6193 -- for the case of multiple potentially use-visible entries hiding one
6194 -- another and as a result being non-directly visible (RM 8.4(11)).
6196 <<Potentially_Use_Visible_Entity>> declare
6197 Only_One_Visible : Boolean := True;
6198 All_Overloadable : Boolean := Is_Overloadable (E);
6200 begin
6201 E2 := Homonym (E);
6202 while Present (E2) loop
6203 if Is_Immediately_Visible (E2) then
6205 -- If the use-visible entity comes from the actual for a
6206 -- formal package, it hides a directly visible entity from
6207 -- outside the instance.
6209 if From_Actual_Package (E)
6210 and then Scope_Depth (Scope (E2)) < Scope_Depth (Inst)
6211 then
6212 goto Found;
6213 else
6214 E := E2;
6215 goto Immediately_Visible_Entity;
6216 end if;
6218 elsif Is_Potentially_Use_Visible (E2) then
6219 Only_One_Visible := False;
6220 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
6222 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
6223 -- that can occur in private_with clauses. Example:
6225 -- with A;
6226 -- private with B; package A is
6227 -- package C is function B return Integer;
6228 -- use A; end A;
6229 -- V1 : Integer := B;
6230 -- private function B return Integer;
6231 -- V2 : Integer := B;
6232 -- end C;
6234 -- V1 resolves to A.B, but V2 resolves to library unit B
6236 elsif Ekind (E2) = E_Function
6237 and then Scope (E2) = Standard_Standard
6238 and then Has_Private_With (E2)
6239 then
6240 Only_One_Visible := False;
6241 All_Overloadable := False;
6242 Nvis_Is_Private_Subprg := True;
6243 exit;
6244 end if;
6246 E2 := Homonym (E2);
6247 end loop;
6249 -- On falling through this loop, we have checked that there are no
6250 -- immediately visible entities. Only_One_Visible is set if exactly
6251 -- one potentially use visible entity exists. All_Overloadable is
6252 -- set if all the potentially use visible entities are overloadable.
6253 -- The condition for legality is that either there is one potentially
6254 -- use visible entity, or if there is more than one, then all of them
6255 -- are overloadable.
6257 if Only_One_Visible or All_Overloadable then
6258 goto Found;
6260 -- If there is more than one potentially use-visible entity and at
6261 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
6262 -- Note that E points to the first such entity on the homonym list.
6264 else
6265 -- If one of the entities is declared in an actual package, it
6266 -- was visible in the generic, and takes precedence over other
6267 -- entities that are potentially use-visible. The same applies
6268 -- if the entity is declared in a local instantiation of the
6269 -- current instance.
6271 if In_Instance then
6273 -- Find the current instance
6275 Inst := Current_Scope;
6276 while Present (Inst) and then Inst /= Standard_Standard loop
6277 if Is_Generic_Instance (Inst) then
6278 exit;
6279 end if;
6281 Inst := Scope (Inst);
6282 end loop;
6284 -- Reexamine the candidate entities, giving priority to those
6285 -- that were visible within the generic.
6287 E2 := E;
6288 while Present (E2) loop
6289 Nested_Inst := Nearest_Enclosing_Instance (E2);
6291 -- The entity is declared within an actual package, or in a
6292 -- nested instance. The ">=" accounts for the case where the
6293 -- current instance and the nested instance are the same.
6295 if From_Actual_Package (E2)
6296 or else (Present (Nested_Inst)
6297 and then Scope_Depth (Nested_Inst) >=
6298 Scope_Depth (Inst))
6299 then
6300 E := E2;
6301 goto Found;
6302 end if;
6304 E2 := Homonym (E2);
6305 end loop;
6307 Nvis_Messages;
6308 goto Done;
6310 elsif Is_Predefined_Unit (Current_Sem_Unit) then
6311 -- A use clause in the body of a system file creates conflict
6312 -- with some entity in a user scope, while rtsfind is active.
6313 -- Keep only the entity coming from another predefined unit.
6315 E2 := E;
6316 while Present (E2) loop
6317 if In_Predefined_Unit (E2) then
6318 E := E2;
6319 goto Found;
6320 end if;
6322 E2 := Homonym (E2);
6323 end loop;
6325 -- Entity must exist because predefined unit is correct
6327 raise Program_Error;
6329 else
6330 Nvis_Messages;
6331 goto Done;
6332 end if;
6333 end if;
6334 end;
6336 -- Come here with E set to the first immediately visible entity on
6337 -- the homonym chain. This is the one we want unless there is another
6338 -- immediately visible entity further on in the chain for an inner
6339 -- scope (RM 8.3(8)).
6341 <<Immediately_Visible_Entity>> declare
6342 Level : Int;
6343 Scop : Entity_Id;
6345 begin
6346 -- Find scope level of initial entity. When compiling through
6347 -- Rtsfind, the previous context is not completely invisible, and
6348 -- an outer entity may appear on the chain, whose scope is below
6349 -- the entry for Standard that delimits the current scope stack.
6350 -- Indicate that the level for this spurious entry is outside of
6351 -- the current scope stack.
6353 Level := Scope_Stack.Last;
6354 loop
6355 Scop := Scope_Stack.Table (Level).Entity;
6356 exit when Scop = Scope (E);
6357 Level := Level - 1;
6358 exit when Scop = Standard_Standard;
6359 end loop;
6361 -- Now search remainder of homonym chain for more inner entry
6362 -- If the entity is Standard itself, it has no scope, and we
6363 -- compare it with the stack entry directly.
6365 E2 := Homonym (E);
6366 while Present (E2) loop
6367 if Is_Immediately_Visible (E2) then
6369 -- If a generic package contains a local declaration that
6370 -- has the same name as the generic, there may be a visibility
6371 -- conflict in an instance, where the local declaration must
6372 -- also hide the name of the corresponding package renaming.
6373 -- We check explicitly for a package declared by a renaming,
6374 -- whose renamed entity is an instance that is on the scope
6375 -- stack, and that contains a homonym in the same scope. Once
6376 -- we have found it, we know that the package renaming is not
6377 -- immediately visible, and that the identifier denotes the
6378 -- other entity (and its homonyms if overloaded).
6380 if Scope (E) = Scope (E2)
6381 and then Ekind (E) = E_Package
6382 and then Present (Renamed_Entity (E))
6383 and then Is_Generic_Instance (Renamed_Entity (E))
6384 and then In_Open_Scopes (Renamed_Entity (E))
6385 and then Comes_From_Source (N)
6386 then
6387 Set_Is_Immediately_Visible (E, False);
6388 E := E2;
6390 else
6391 for J in Level + 1 .. Scope_Stack.Last loop
6392 if Scope_Stack.Table (J).Entity = Scope (E2)
6393 or else Scope_Stack.Table (J).Entity = E2
6394 then
6395 Level := J;
6396 E := E2;
6397 exit;
6398 end if;
6399 end loop;
6400 end if;
6401 end if;
6403 E2 := Homonym (E2);
6404 end loop;
6406 -- At the end of that loop, E is the innermost immediately
6407 -- visible entity, so we are all set.
6408 end;
6410 -- Come here with entity found, and stored in E
6412 <<Found>> begin
6414 -- Check violation of No_Wide_Characters restriction
6416 Check_Wide_Character_Restriction (E, N);
6418 -- When distribution features are available (Get_PCS_Name /=
6419 -- Name_No_DSA), a remote access-to-subprogram type is converted
6420 -- into a record type holding whatever information is needed to
6421 -- perform a remote call on an RCI subprogram. In that case we
6422 -- rewrite any occurrence of the RAS type into the equivalent record
6423 -- type here. 'Access attribute references and RAS dereferences are
6424 -- then implemented using specific TSSs. However when distribution is
6425 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
6426 -- generation of these TSSs, and we must keep the RAS type in its
6427 -- original access-to-subprogram form (since all calls through a
6428 -- value of such type will be local anyway in the absence of a PCS).
6430 if Comes_From_Source (N)
6431 and then Is_Remote_Access_To_Subprogram_Type (E)
6432 and then Ekind (E) = E_Access_Subprogram_Type
6433 and then Expander_Active
6434 and then Get_PCS_Name /= Name_No_DSA
6435 then
6436 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
6437 goto Done;
6438 end if;
6440 -- Set the entity. Note that the reason we call Set_Entity for the
6441 -- overloadable case, as opposed to Set_Entity_With_Checks is
6442 -- that in the overloaded case, the initial call can set the wrong
6443 -- homonym. The call that sets the right homonym is in Sem_Res and
6444 -- that call does use Set_Entity_With_Checks, so we don't miss
6445 -- a style check.
6447 if Is_Overloadable (E) then
6448 Set_Entity (N, E);
6449 else
6450 Set_Entity_With_Checks (N, E);
6451 end if;
6453 if Is_Type (E) then
6454 Set_Etype (N, E);
6455 else
6456 Set_Etype (N, Get_Full_View (Etype (E)));
6457 end if;
6459 if Debug_Flag_E then
6460 Write_Str (" found ");
6461 Write_Entity_Info (E, " ");
6462 end if;
6464 if Is_Self_Hidden (E)
6465 and then
6466 (not Is_Record_Type (Current_Scope)
6467 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
6468 then
6469 Premature_Usage (N);
6471 -- If the entity is overloadable, collect all interpretations of the
6472 -- name for subsequent overload resolution. We optimize a bit here to
6473 -- do this only if we have an overloadable entity that is not on its
6474 -- own on the homonym chain.
6476 elsif Is_Overloadable (E)
6477 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
6478 then
6479 Collect_Interps (N);
6481 -- If no homonyms were visible, the entity is unambiguous
6483 if not Is_Overloaded (N) then
6484 if not Is_Actual_Parameter then
6485 Generate_Reference (E, N);
6486 end if;
6487 end if;
6489 -- Case of non-overloadable entity, set the entity providing that
6490 -- we do not have the case of a discriminant reference within a
6491 -- default expression. Such references are replaced with the
6492 -- corresponding discriminal, which is the formal corresponding to
6493 -- to the discriminant in the initialization procedure.
6495 else
6496 -- Entity is unambiguous, indicate that it is referenced here
6498 -- For a renaming of an object, always generate simple reference,
6499 -- we don't try to keep track of assignments in this case, except
6500 -- in SPARK mode where renamings are traversed for generating
6501 -- local effects of subprograms.
6503 if Is_Object (E)
6504 and then Present (Renamed_Object (E))
6505 and then not GNATprove_Mode
6506 then
6507 Generate_Reference (E, N);
6509 -- If the renamed entity is a private protected component,
6510 -- reference the original component as well. This needs to be
6511 -- done because the private renamings are installed before any
6512 -- analysis has occurred. Reference to a private component will
6513 -- resolve to the renaming and the original component will be
6514 -- left unreferenced, hence the following.
6516 if Is_Prival (E) then
6517 Generate_Reference (Prival_Link (E), N);
6518 end if;
6520 -- One odd case is that we do not want to set the Referenced flag
6521 -- if the entity is a label, and the identifier is the label in
6522 -- the source, since this is not a reference from the point of
6523 -- view of the user.
6525 elsif Nkind (Parent (N)) = N_Label then
6526 declare
6527 R : constant Boolean := Referenced (E);
6529 begin
6530 -- Generate reference unless this is an actual parameter
6531 -- (see comment below).
6533 if not Is_Actual_Parameter then
6534 Generate_Reference (E, N);
6535 Set_Referenced (E, R);
6536 end if;
6537 end;
6539 -- Normal case, not a label: generate reference
6541 else
6542 if not Is_Actual_Parameter then
6544 -- Package or generic package is always a simple reference
6546 if Is_Package_Or_Generic_Package (E) then
6547 Generate_Reference (E, N, 'r');
6549 -- Else see if we have a left hand side
6551 else
6552 case Known_To_Be_Assigned (N, Only_LHS => True) is
6553 when True =>
6554 Generate_Reference (E, N, 'm');
6556 when False =>
6557 Generate_Reference (E, N, 'r');
6559 end case;
6560 end if;
6561 end if;
6562 end if;
6564 Set_Entity_Or_Discriminal (N, E);
6566 -- The name may designate a generalized reference, in which case
6567 -- the dereference interpretation will be included. Context is
6568 -- one in which a name is legal.
6570 if Ada_Version >= Ada_2012
6571 and then
6572 (Nkind (Parent (N)) in N_Subexpr
6573 or else Nkind (Parent (N)) in N_Assignment_Statement
6574 | N_Object_Declaration
6575 | N_Parameter_Association)
6576 then
6577 Check_Implicit_Dereference (N, Etype (E));
6578 end if;
6579 end if;
6580 end;
6582 -- Mark relevant use-type and use-package clauses as effective if the
6583 -- node in question is not overloaded and therefore does not require
6584 -- resolution.
6586 -- Note: Generic actual subprograms do not follow the normal resolution
6587 -- path, so ignore the fact that they are overloaded and mark them
6588 -- anyway.
6590 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6591 Mark_Use_Clauses (N);
6592 end if;
6594 -- Come here with entity set
6596 <<Done>>
6597 Check_Restriction_No_Use_Of_Entity (N);
6599 -- Annotate the tree by creating a variable reference marker in case the
6600 -- original variable reference is folded or optimized away. The variable
6601 -- reference marker is automatically saved for later examination by the
6602 -- ABE Processing phase. Variable references which act as actuals in a
6603 -- call require special processing and are left to Resolve_Actuals. The
6604 -- reference is a write when it appears on the left hand side of an
6605 -- assignment.
6607 if Needs_Variable_Reference_Marker (N => N, Calls_OK => False) then
6608 declare
6609 Is_Assignment_LHS : constant Boolean := Known_To_Be_Assigned (N);
6611 begin
6612 Build_Variable_Reference_Marker
6613 (N => N,
6614 Read => not Is_Assignment_LHS,
6615 Write => Is_Assignment_LHS);
6616 end;
6617 end if;
6618 end Find_Direct_Name;
6620 ------------------------
6621 -- Find_Expanded_Name --
6622 ------------------------
6624 -- This routine searches the homonym chain of the entity until it finds
6625 -- an entity declared in the scope denoted by the prefix. If the entity
6626 -- is private, it may nevertheless be immediately visible, if we are in
6627 -- the scope of its declaration.
6629 procedure Find_Expanded_Name (N : Node_Id) is
6630 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean;
6631 -- Determine whether expanded name Nod appears within a pragma which is
6632 -- a suitable context for an abstract view of a state or variable. The
6633 -- following pragmas fall in this category:
6634 -- Depends
6635 -- Global
6636 -- Initializes
6637 -- Refined_Depends
6638 -- Refined_Global
6640 -- In addition, pragma Abstract_State is also considered suitable even
6641 -- though it is an illegal context for an abstract view as this allows
6642 -- for proper resolution of abstract views of variables. This illegal
6643 -- context is later flagged in the analysis of indicator Part_Of.
6645 -----------------------------
6646 -- In_Abstract_View_Pragma --
6647 -----------------------------
6649 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is
6650 Par : Node_Id;
6652 begin
6653 -- Climb the parent chain looking for a pragma
6655 Par := Nod;
6656 while Present (Par) loop
6657 if Nkind (Par) = N_Pragma then
6658 if Pragma_Name_Unmapped (Par)
6659 in Name_Abstract_State
6660 | Name_Depends
6661 | Name_Global
6662 | Name_Initializes
6663 | Name_Refined_Depends
6664 | Name_Refined_Global
6665 then
6666 return True;
6668 -- Otherwise the pragma is not a legal context for an abstract
6669 -- view.
6671 else
6672 exit;
6673 end if;
6675 -- Prevent the search from going too far
6677 elsif Is_Body_Or_Package_Declaration (Par) then
6678 exit;
6679 end if;
6681 Par := Parent (Par);
6682 end loop;
6684 return False;
6685 end In_Abstract_View_Pragma;
6687 -- Local variables
6689 Selector : constant Node_Id := Selector_Name (N);
6691 Candidate : Entity_Id := Empty;
6692 P_Name : Entity_Id;
6693 Id : Entity_Id;
6695 -- Start of processing for Find_Expanded_Name
6697 begin
6698 P_Name := Entity (Prefix (N));
6700 -- If the prefix is a renamed package, look for the entity in the
6701 -- original package.
6703 if Ekind (P_Name) = E_Package
6704 and then Present (Renamed_Entity (P_Name))
6705 then
6706 P_Name := Renamed_Entity (P_Name);
6708 if From_Limited_With (P_Name)
6709 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6710 then
6711 Error_Msg_NE
6712 ("renaming of limited view of package & not usable in this"
6713 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
6715 elsif Has_Limited_View (P_Name)
6716 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6717 and then not Is_Visible_Through_Renamings (P_Name)
6718 then
6719 Error_Msg_NE
6720 ("renaming of limited view of package & not usable in this"
6721 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
6722 end if;
6724 -- Rewrite node with entity field pointing to renamed object
6726 Rewrite (Prefix (N), New_Copy (Prefix (N)));
6727 Set_Entity (Prefix (N), P_Name);
6729 -- If the prefix is an object of a concurrent type, look for
6730 -- the entity in the associated task or protected type.
6732 elsif Is_Concurrent_Type (Etype (P_Name)) then
6733 P_Name := Etype (P_Name);
6734 end if;
6736 Id := Current_Entity (Selector);
6738 declare
6739 Is_New_Candidate : Boolean;
6741 begin
6742 while Present (Id) loop
6743 if Scope (Id) = P_Name then
6744 Candidate := Id;
6745 Is_New_Candidate := True;
6747 -- Handle abstract views of states and variables. These are
6748 -- acceptable candidates only when the reference to the view
6749 -- appears in certain pragmas.
6751 if Ekind (Id) = E_Abstract_State
6752 and then From_Limited_With (Id)
6753 and then Present (Non_Limited_View (Id))
6754 then
6755 if In_Abstract_View_Pragma (N) then
6756 Candidate := Non_Limited_View (Id);
6757 Is_New_Candidate := True;
6759 -- Hide the candidate because it is not used in a proper
6760 -- context.
6762 else
6763 Candidate := Empty;
6764 Is_New_Candidate := False;
6765 end if;
6766 end if;
6768 -- Ada 2005 (AI-217): Handle shadow entities associated with
6769 -- types declared in limited-withed nested packages. We don't need
6770 -- to handle E_Incomplete_Subtype entities because the entities
6771 -- in the limited view are always E_Incomplete_Type and
6772 -- E_Class_Wide_Type entities (see Build_Limited_Views).
6774 -- Regarding the expression used to evaluate the scope, it
6775 -- is important to note that the limited view also has shadow
6776 -- entities associated nested packages. For this reason the
6777 -- correct scope of the entity is the scope of the real entity.
6778 -- The non-limited view may itself be incomplete, in which case
6779 -- get the full view if available.
6781 elsif Ekind (Id) in E_Incomplete_Type | E_Class_Wide_Type
6782 and then From_Limited_With (Id)
6783 and then Present (Non_Limited_View (Id))
6784 and then Scope (Non_Limited_View (Id)) = P_Name
6785 then
6786 Candidate := Get_Full_View (Non_Limited_View (Id));
6787 Is_New_Candidate := True;
6789 -- Handle special case where the prefix is a renaming of a shadow
6790 -- package which is visible. Required to avoid reporting spurious
6791 -- errors.
6793 elsif Ekind (P_Name) = E_Package
6794 and then From_Limited_With (P_Name)
6795 and then not From_Limited_With (Id)
6796 and then Sloc (Scope (Id)) = Sloc (P_Name)
6797 and then Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6798 then
6799 Candidate := Get_Full_View (Id);
6800 Is_New_Candidate := True;
6802 -- An unusual case arises with a fully qualified name for an
6803 -- entity local to a generic child unit package, within an
6804 -- instantiation of that package. The name of the unit now
6805 -- denotes the renaming created within the instance. This is
6806 -- only relevant in an instance body, see below.
6808 elsif Is_Generic_Instance (Scope (Id))
6809 and then In_Open_Scopes (Scope (Id))
6810 and then In_Instance_Body
6811 and then Ekind (Scope (Id)) = E_Package
6812 and then Ekind (Id) = E_Package
6813 and then Renamed_Entity (Id) = Scope (Id)
6814 and then Is_Immediately_Visible (P_Name)
6815 then
6816 Is_New_Candidate := True;
6818 else
6819 Is_New_Candidate := False;
6820 end if;
6822 if Is_New_Candidate then
6824 -- If entity is a child unit, either it is a visible child of
6825 -- the prefix, or we are in the body of a generic prefix, as
6826 -- will happen when a child unit is instantiated in the body
6827 -- of a generic parent. This is because the instance body does
6828 -- not restore the full compilation context, given that all
6829 -- non-local references have been captured.
6831 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
6832 exit when Is_Visible_Lib_Unit (Id)
6833 or else (Is_Child_Unit (Id)
6834 and then In_Open_Scopes (Scope (Id))
6835 and then In_Instance_Body);
6836 else
6837 exit when not Is_Hidden (Id);
6838 end if;
6840 exit when Is_Immediately_Visible (Id);
6841 end if;
6843 Id := Homonym (Id);
6844 end loop;
6845 end;
6847 if No (Id)
6848 and then Ekind (P_Name) in E_Procedure | E_Function
6849 and then Is_Generic_Instance (P_Name)
6850 then
6851 -- Expanded name denotes entity in (instance of) generic subprogram.
6852 -- The entity may be in the subprogram instance, or may denote one of
6853 -- the formals, which is declared in the enclosing wrapper package.
6855 P_Name := Scope (P_Name);
6857 Id := Current_Entity (Selector);
6858 while Present (Id) loop
6859 exit when Scope (Id) = P_Name;
6860 Id := Homonym (Id);
6861 end loop;
6862 end if;
6864 if No (Id) or else Chars (Id) /= Chars (Selector) then
6865 Set_Etype (N, Any_Type);
6867 -- If we are looking for an entity defined in System, try to find it
6868 -- in the child package that may have been provided as an extension
6869 -- to System. The Extend_System pragma will have supplied the name of
6870 -- the extension, which may have to be loaded.
6872 if Chars (P_Name) = Name_System
6873 and then Scope (P_Name) = Standard_Standard
6874 and then Present (System_Extend_Unit)
6875 and then Present_System_Aux (N)
6876 then
6877 Set_Entity (Prefix (N), System_Aux_Id);
6878 Find_Expanded_Name (N);
6879 return;
6881 -- There is an implicit instance of the predefined operator in
6882 -- the given scope. The operator entity is defined in Standard.
6883 -- Has_Implicit_Operator makes the node into an Expanded_Name.
6885 elsif Nkind (Selector) = N_Operator_Symbol
6886 and then Has_Implicit_Operator (N)
6887 then
6888 return;
6890 -- If there is no literal defined in the scope denoted by the
6891 -- prefix, the literal may belong to (a type derived from)
6892 -- Standard_Character, for which we have no explicit literals.
6894 elsif Nkind (Selector) = N_Character_Literal
6895 and then Has_Implicit_Character_Literal (N)
6896 then
6897 return;
6899 else
6900 -- If the prefix is a single concurrent object, use its name in
6901 -- the error message, rather than that of the anonymous type.
6903 if Is_Concurrent_Type (P_Name)
6904 and then Is_Internal_Name (Chars (P_Name))
6905 then
6906 Error_Msg_Node_2 := Entity (Prefix (N));
6907 else
6908 Error_Msg_Node_2 := P_Name;
6909 end if;
6911 if P_Name = System_Aux_Id then
6912 P_Name := Scope (P_Name);
6913 Set_Entity (Prefix (N), P_Name);
6914 end if;
6916 if Present (Candidate) then
6918 -- If we know that the unit is a child unit we can give a more
6919 -- accurate error message.
6921 if Is_Child_Unit (Candidate) then
6923 -- If the candidate is a private child unit and we are in
6924 -- the visible part of a public unit, specialize the error
6925 -- message. There might be a private with_clause for it,
6926 -- but it is not currently active.
6928 if Is_Private_Descendant (Candidate)
6929 and then Ekind (Current_Scope) = E_Package
6930 and then not In_Private_Part (Current_Scope)
6931 and then not Is_Private_Descendant (Current_Scope)
6932 then
6933 Error_Msg_N
6934 ("private child unit& is not visible here", Selector);
6936 -- Normal case where we have a missing with for a child unit
6938 else
6939 Error_Msg_Qual_Level := 99;
6940 Error_Msg_NE -- CODEFIX
6941 ("missing `WITH &;`", Selector, Candidate);
6942 Error_Msg_Qual_Level := 0;
6943 end if;
6945 -- Here we don't know that this is a child unit
6947 else
6948 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
6949 end if;
6951 else
6952 -- Within the instantiation of a child unit, the prefix may
6953 -- denote the parent instance, but the selector has the name
6954 -- of the original child. That is to say, when A.B appears
6955 -- within an instantiation of generic child unit B, the scope
6956 -- stack includes an instance of A (P_Name) and an instance
6957 -- of B under some other name. We scan the scope to find this
6958 -- child instance, which is the desired entity.
6959 -- Note that the parent may itself be a child instance, if
6960 -- the reference is of the form A.B.C, in which case A.B has
6961 -- already been rewritten with the proper entity.
6963 if In_Open_Scopes (P_Name)
6964 and then Is_Generic_Instance (P_Name)
6965 then
6966 declare
6967 Gen_Par : constant Entity_Id :=
6968 Generic_Parent (Specification
6969 (Unit_Declaration_Node (P_Name)));
6970 S : Entity_Id := Current_Scope;
6971 P : Entity_Id;
6973 begin
6974 for J in reverse 0 .. Scope_Stack.Last loop
6975 S := Scope_Stack.Table (J).Entity;
6977 exit when S = Standard_Standard;
6979 if Ekind (S) in E_Function | E_Package | E_Procedure
6980 then
6981 P :=
6982 Generic_Parent (Specification
6983 (Unit_Declaration_Node (S)));
6985 -- Check that P is a generic child of the generic
6986 -- parent of the prefix.
6988 if Present (P)
6989 and then Chars (P) = Chars (Selector)
6990 and then Scope (P) = Gen_Par
6991 then
6992 Id := S;
6993 goto Found;
6994 end if;
6995 end if;
6997 end loop;
6998 end;
6999 end if;
7001 -- If this is a selection from Ada, System or Interfaces, then
7002 -- we assume a missing with for the corresponding package.
7004 if Is_Known_Unit (N)
7005 and then not (Present (Entity (Prefix (N)))
7006 and then Scope (Entity (Prefix (N))) /=
7007 Standard_Standard)
7008 then
7009 if not Error_Posted (N) then
7010 Error_Msg_NE
7011 ("& is not a visible entity of&", Prefix (N), Selector);
7012 Error_Missing_With_Of_Known_Unit (Prefix (N));
7013 end if;
7015 -- If this is a selection from a dummy package, then suppress
7016 -- the error message, of course the entity is missing if the
7017 -- package is missing.
7019 elsif Sloc (Error_Msg_Node_2) = No_Location then
7020 null;
7022 -- Here we have the case of an undefined component
7024 else
7025 -- The prefix may hide a homonym in the context that
7026 -- declares the desired entity. This error can use a
7027 -- specialized message.
7029 if In_Open_Scopes (P_Name) then
7030 declare
7031 H : constant Entity_Id := Homonym (P_Name);
7033 begin
7034 if Present (H)
7035 and then Is_Compilation_Unit (H)
7036 and then
7037 (Is_Immediately_Visible (H)
7038 or else Is_Visible_Lib_Unit (H))
7039 then
7040 Id := First_Entity (H);
7041 while Present (Id) loop
7042 if Chars (Id) = Chars (Selector) then
7043 Error_Msg_Qual_Level := 99;
7044 Error_Msg_Name_1 := Chars (Selector);
7045 Error_Msg_NE
7046 ("% not declared in&", N, P_Name);
7047 Error_Msg_NE
7048 ("\use fully qualified name starting with "
7049 & "Standard to make& visible", N, H);
7050 Error_Msg_Qual_Level := 0;
7051 goto Done;
7052 end if;
7054 Next_Entity (Id);
7055 end loop;
7056 end if;
7058 -- If not found, standard error message
7060 Error_Msg_NE ("& not declared in&", N, Selector);
7062 <<Done>> null;
7063 end;
7065 else
7066 -- Might be worth specializing the case when the prefix
7067 -- is a limited view.
7068 -- ... not declared in limited view of...
7070 Error_Msg_NE ("& not declared in&", N, Selector);
7071 end if;
7073 -- Check for misspelling of some entity in prefix
7075 Id := First_Entity (P_Name);
7076 while Present (Id) loop
7077 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
7078 and then not Is_Internal_Name (Chars (Id))
7079 then
7080 Error_Msg_NE -- CODEFIX
7081 ("possible misspelling of&", Selector, Id);
7082 exit;
7083 end if;
7085 Next_Entity (Id);
7086 end loop;
7088 -- Specialize the message if this may be an instantiation
7089 -- of a child unit that was not mentioned in the context.
7091 if Nkind (Parent (N)) = N_Package_Instantiation
7092 and then Is_Generic_Instance (Entity (Prefix (N)))
7093 and then Is_Compilation_Unit
7094 (Generic_Parent (Parent (Entity (Prefix (N)))))
7095 then
7096 Error_Msg_Node_2 := Selector;
7097 Error_Msg_N -- CODEFIX
7098 ("\missing `WITH &.&;`", Prefix (N));
7099 end if;
7100 end if;
7101 end if;
7103 Id := Any_Id;
7104 end if;
7105 end if;
7107 <<Found>>
7108 if Comes_From_Source (N)
7109 and then Is_Remote_Access_To_Subprogram_Type (Id)
7110 and then Ekind (Id) = E_Access_Subprogram_Type
7111 and then Present (Equivalent_Type (Id))
7112 then
7113 -- If we are not actually generating distribution code (i.e. the
7114 -- current PCS is the dummy non-distributed version), then the
7115 -- Equivalent_Type will be missing, and Id should be treated as
7116 -- a regular access-to-subprogram type.
7118 Id := Equivalent_Type (Id);
7119 Set_Chars (Selector, Chars (Id));
7120 end if;
7122 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
7124 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
7125 if From_Limited_With (Id)
7126 or else Is_Type (Id)
7127 or else Ekind (Id) = E_Package
7128 then
7129 null;
7130 else
7131 Error_Msg_N
7132 ("limited withed package can only be used to access incomplete "
7133 & "types", N);
7134 end if;
7135 end if;
7137 if Is_Task_Type (P_Name)
7138 and then ((Ekind (Id) = E_Entry
7139 and then Nkind (Parent (N)) /= N_Attribute_Reference)
7140 or else
7141 (Ekind (Id) = E_Entry_Family
7142 and then
7143 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
7144 then
7145 -- If both the task type and the entry are in scope, this may still
7146 -- be the expanded name of an entry formal.
7148 if In_Open_Scopes (Id)
7149 and then Nkind (Parent (N)) = N_Selected_Component
7150 then
7151 null;
7153 else
7154 -- It is an entry call after all, either to the current task
7155 -- (which will deadlock) or to an enclosing task.
7157 Analyze_Selected_Component (N);
7158 return;
7159 end if;
7160 end if;
7162 case Nkind (N) is
7163 when N_Selected_Component =>
7164 Reinit_Field_To_Zero (N, F_Is_Prefixed_Call);
7165 Change_Selected_Component_To_Expanded_Name (N);
7167 when N_Expanded_Name =>
7168 null;
7170 when others =>
7171 pragma Assert (False);
7172 end case;
7174 -- Preserve relevant elaboration-related attributes of the context which
7175 -- are no longer available or very expensive to recompute once analysis,
7176 -- resolution, and expansion are over.
7178 Mark_Elaboration_Attributes
7179 (N_Id => N,
7180 Checks => True,
7181 Modes => True,
7182 Warnings => True);
7184 -- Set appropriate type
7186 if Is_Type (Id) then
7187 Set_Etype (N, Id);
7188 else
7189 Set_Etype (N, Get_Full_View (Etype (Id)));
7190 end if;
7192 -- Do style check and generate reference, but skip both steps if this
7193 -- entity has homonyms, since we may not have the right homonym set yet.
7194 -- The proper homonym will be set during the resolve phase.
7196 if Has_Homonym (Id) then
7197 Set_Entity (N, Id);
7199 else
7200 Set_Entity_Or_Discriminal (N, Id);
7202 case Known_To_Be_Assigned (N, Only_LHS => True) is
7203 when True =>
7204 Generate_Reference (Id, N, 'm');
7206 when False =>
7207 Generate_Reference (Id, N, 'r');
7209 end case;
7210 end if;
7212 -- Check for violation of No_Wide_Characters
7214 Check_Wide_Character_Restriction (Id, N);
7216 if Is_Self_Hidden (Id) then
7217 Premature_Usage (N);
7219 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
7220 declare
7221 H : Entity_Id := Homonym (Id);
7223 begin
7224 while Present (H) loop
7225 if Scope (H) = Scope (Id)
7226 and then (not Is_Hidden (H)
7227 or else Is_Immediately_Visible (H))
7228 then
7229 Collect_Interps (N);
7230 exit;
7231 end if;
7233 H := Homonym (H);
7234 end loop;
7236 -- If an extension of System is present, collect possible explicit
7237 -- overloadings declared in the extension.
7239 if Chars (P_Name) = Name_System
7240 and then Scope (P_Name) = Standard_Standard
7241 and then Present (System_Extend_Unit)
7242 and then Present_System_Aux (N)
7243 then
7244 H := Current_Entity (Id);
7246 while Present (H) loop
7247 if Scope (H) = System_Aux_Id then
7248 Add_One_Interp (N, H, Etype (H));
7249 end if;
7251 H := Homonym (H);
7252 end loop;
7253 end if;
7254 end;
7255 end if;
7257 if Nkind (Selector_Name (N)) = N_Operator_Symbol
7258 and then Scope (Id) /= Standard_Standard
7259 then
7260 -- In addition to user-defined operators in the given scope, there
7261 -- may be an implicit instance of the predefined operator. The
7262 -- operator (defined in Standard) is found in Has_Implicit_Operator,
7263 -- and added to the interpretations. Procedure Add_One_Interp will
7264 -- determine which hides which.
7266 if Has_Implicit_Operator (N) then
7267 null;
7268 end if;
7269 end if;
7271 -- If there is a single interpretation for N we can generate a
7272 -- reference to the unique entity found.
7274 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
7275 Generate_Reference (Id, N);
7276 end if;
7278 -- Mark relevant use-type and use-package clauses as effective if the
7279 -- node in question is not overloaded and therefore does not require
7280 -- resolution.
7282 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
7283 Mark_Use_Clauses (N);
7284 end if;
7286 Check_Restriction_No_Use_Of_Entity (N);
7288 -- Annotate the tree by creating a variable reference marker in case the
7289 -- original variable reference is folded or optimized away. The variable
7290 -- reference marker is automatically saved for later examination by the
7291 -- ABE Processing phase. Variable references which act as actuals in a
7292 -- call require special processing and are left to Resolve_Actuals. The
7293 -- reference is a write when it appears on the left hand side of an
7294 -- assignment.
7296 if Needs_Variable_Reference_Marker
7297 (N => N,
7298 Calls_OK => False)
7299 then
7300 declare
7301 Is_Assignment_LHS : constant Boolean := Known_To_Be_Assigned (N);
7303 begin
7304 Build_Variable_Reference_Marker
7305 (N => N,
7306 Read => not Is_Assignment_LHS,
7307 Write => Is_Assignment_LHS);
7308 end;
7309 end if;
7310 end Find_Expanded_Name;
7312 --------------------
7313 -- Find_First_Use --
7314 --------------------
7316 function Find_First_Use (Use_Clause : Node_Id) return Node_Id is
7317 Curr : Node_Id;
7319 begin
7320 -- Loop through the Prev_Use_Clause chain
7322 Curr := Use_Clause;
7323 while Present (Prev_Use_Clause (Curr)) loop
7324 Curr := Prev_Use_Clause (Curr);
7325 end loop;
7327 return Curr;
7328 end Find_First_Use;
7330 -------------------------
7331 -- Find_Renamed_Entity --
7332 -------------------------
7334 function Find_Renamed_Entity
7335 (N : Node_Id;
7336 Nam : Node_Id;
7337 New_S : Entity_Id;
7338 Is_Actual : Boolean := False) return Entity_Id
7340 Ind : Interp_Index;
7341 I1 : Interp_Index := 0; -- Suppress junk warnings
7342 It : Interp;
7343 It1 : Interp;
7344 Old_S : Entity_Id;
7345 Inst : Entity_Id;
7347 function Find_Nearer_Entity
7348 (New_S : Entity_Id;
7349 Old1_S : Entity_Id;
7350 Old2_S : Entity_Id) return Entity_Id;
7351 -- Determine whether one of Old_S1 and Old_S2 is nearer to New_S than
7352 -- the other, and return it if so. Return Empty otherwise. We use this
7353 -- in conjunction with Inherit_Renamed_Profile to simplify later type
7354 -- disambiguation for actual subprograms in instances.
7356 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
7357 -- If the renamed entity is an implicit operator, check whether it is
7358 -- visible because its operand type is properly visible. This check
7359 -- applies to explicit renamed entities that appear in the source in a
7360 -- renaming declaration or a formal subprogram instance, but not to
7361 -- default generic actuals with a name.
7363 function Report_Overload return Entity_Id;
7364 -- List possible interpretations, and specialize message in the
7365 -- case of a generic actual.
7367 function Within (Inner, Outer : Entity_Id) return Boolean;
7368 -- Determine whether a candidate subprogram is defined within the
7369 -- enclosing instance. If yes, it has precedence over outer candidates.
7371 --------------------------
7372 -- Find_Nearer_Entity --
7373 --------------------------
7375 function Find_Nearer_Entity
7376 (New_S : Entity_Id;
7377 Old1_S : Entity_Id;
7378 Old2_S : Entity_Id) return Entity_Id
7380 New_F : Entity_Id;
7381 Old1_F : Entity_Id;
7382 Old2_F : Entity_Id;
7383 Anc_T : Entity_Id;
7385 begin
7386 New_F := First_Formal (New_S);
7387 Old1_F := First_Formal (Old1_S);
7388 Old2_F := First_Formal (Old2_S);
7390 -- The criterion is whether the type of the formals of one of Old1_S
7391 -- and Old2_S is an ancestor subtype of the type of the corresponding
7392 -- formals of New_S while the other is not (we already know that they
7393 -- are all subtypes of the same base type).
7395 -- This makes it possible to find the more correct renamed entity in
7396 -- the case of a generic instantiation nested in an enclosing one for
7397 -- which different formal types get the same actual type, which will
7398 -- in turn make it possible for Inherit_Renamed_Profile to preserve
7399 -- types on formal parameters and ultimately simplify disambiguation.
7401 -- Consider the follow package G:
7403 -- generic
7404 -- type Item_T is private;
7405 -- with function Compare (L, R: Item_T) return Boolean is <>;
7407 -- type Bound_T is private;
7408 -- with function Compare (L, R : Bound_T) return Boolean is <>;
7409 -- package G is
7410 -- ...
7411 -- end G;
7413 -- package body G is
7414 -- package My_Inner is Inner_G (Bound_T);
7415 -- ...
7416 -- end G;
7418 -- with the following package Inner_G:
7420 -- generic
7421 -- type T is private;
7422 -- with function Compare (L, R: T) return Boolean is <>;
7423 -- package Inner_G is
7424 -- function "<" (L, R: T) return Boolean is (Compare (L, R));
7425 -- end Inner_G;
7427 -- If G is instantiated on the same actual type with a single Compare
7428 -- function:
7430 -- type T is ...
7431 -- function Compare (L, R : T) return Boolean;
7432 -- package My_G is new (T, T);
7434 -- then the renaming generated for Compare in the inner instantiation
7435 -- is ambiguous: it can rename either of the renamings generated for
7436 -- the outer instantiation. Now if the first one is picked up, then
7437 -- the subtypes of the formal parameters of the renaming will not be
7438 -- preserved in Inherit_Renamed_Profile because they are subtypes of
7439 -- the Bound_T formal type and not of the Item_T formal type, so we
7440 -- need to arrange for the second one to be picked up instead.
7442 while Present (New_F) loop
7443 if Etype (Old1_F) /= Etype (Old2_F) then
7444 Anc_T := Ancestor_Subtype (Etype (New_F));
7446 if Etype (Old1_F) = Anc_T then
7447 return Old1_S;
7448 elsif Etype (Old2_F) = Anc_T then
7449 return Old2_S;
7450 end if;
7451 end if;
7453 Next_Formal (New_F);
7454 Next_Formal (Old1_F);
7455 Next_Formal (Old2_F);
7456 end loop;
7458 pragma Assert (No (Old1_F));
7459 pragma Assert (No (Old2_F));
7461 return Empty;
7462 end Find_Nearer_Entity;
7464 --------------------------
7465 -- Is_Visible_Operation --
7466 --------------------------
7468 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
7469 Scop : Entity_Id;
7470 Typ : Entity_Id;
7471 Btyp : Entity_Id;
7473 begin
7474 if Ekind (Op) /= E_Operator
7475 or else Scope (Op) /= Standard_Standard
7476 or else (In_Instance
7477 and then (not Is_Actual
7478 or else Present (Enclosing_Instance)))
7479 then
7480 return True;
7482 else
7483 -- For a fixed point type operator, check the resulting type,
7484 -- because it may be a mixed mode integer * fixed operation.
7486 if Present (Next_Formal (First_Formal (New_S)))
7487 and then Is_Fixed_Point_Type (Etype (New_S))
7488 then
7489 Typ := Etype (New_S);
7490 else
7491 Typ := Etype (First_Formal (New_S));
7492 end if;
7494 Btyp := Base_Type (Typ);
7496 if Nkind (Nam) /= N_Expanded_Name then
7497 return (In_Open_Scopes (Scope (Btyp))
7498 or else Is_Potentially_Use_Visible (Btyp)
7499 or else In_Use (Btyp)
7500 or else In_Use (Scope (Btyp)));
7502 else
7503 Scop := Entity (Prefix (Nam));
7505 if Ekind (Scop) = E_Package
7506 and then Present (Renamed_Entity (Scop))
7507 then
7508 Scop := Renamed_Entity (Scop);
7509 end if;
7511 -- Operator is visible if prefix of expanded name denotes
7512 -- scope of type, or else type is defined in System_Aux
7513 -- and the prefix denotes System.
7515 return Scope (Btyp) = Scop
7516 or else (Scope (Btyp) = System_Aux_Id
7517 and then Scope (Scope (Btyp)) = Scop);
7518 end if;
7519 end if;
7520 end Is_Visible_Operation;
7522 ------------
7523 -- Within --
7524 ------------
7526 function Within (Inner, Outer : Entity_Id) return Boolean is
7527 Sc : Entity_Id;
7529 begin
7530 Sc := Scope (Inner);
7531 while Sc /= Standard_Standard loop
7532 if Sc = Outer then
7533 return True;
7534 else
7535 Sc := Scope (Sc);
7536 end if;
7537 end loop;
7539 return False;
7540 end Within;
7542 ---------------------
7543 -- Report_Overload --
7544 ---------------------
7546 function Report_Overload return Entity_Id is
7547 begin
7548 if Is_Actual then
7549 Error_Msg_NE -- CODEFIX
7550 ("ambiguous actual subprogram&, " &
7551 "possible interpretations:", N, Nam);
7552 else
7553 Error_Msg_N -- CODEFIX
7554 ("ambiguous subprogram, " &
7555 "possible interpretations:", N);
7556 end if;
7558 List_Interps (Nam, N);
7559 return Old_S;
7560 end Report_Overload;
7562 -- Start of processing for Find_Renamed_Entity
7564 begin
7565 Old_S := Any_Id;
7566 Candidate_Renaming := Empty;
7568 if Is_Overloaded (Nam) then
7569 Get_First_Interp (Nam, Ind, It);
7570 while Present (It.Nam) loop
7571 if Entity_Matches_Spec (It.Nam, New_S)
7572 and then Is_Visible_Operation (It.Nam)
7573 then
7574 if Old_S /= Any_Id then
7576 -- Note: The call to Disambiguate only happens if a
7577 -- previous interpretation was found, in which case I1
7578 -- has received a value.
7580 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
7582 if It1 = No_Interp then
7583 Inst := Enclosing_Instance;
7585 if Present (Inst) then
7586 if Within (It.Nam, Inst) then
7587 if Within (Old_S, Inst) then
7588 declare
7589 It_D : constant Uint :=
7590 Scope_Depth_Default_0 (It.Nam);
7591 Old_D : constant Uint :=
7592 Scope_Depth_Default_0 (Old_S);
7593 N_Ent : Entity_Id;
7594 begin
7595 -- Choose the innermost subprogram, which
7596 -- would hide the outer one in the generic.
7598 if Old_D > It_D then
7599 return Old_S;
7600 elsif It_D > Old_D then
7601 return It.Nam;
7602 end if;
7604 -- Otherwise, if we can determine that one
7605 -- of the entities is nearer to the renaming
7606 -- than the other, choose it. If not, then
7607 -- return the newer one as done historically.
7609 N_Ent :=
7610 Find_Nearer_Entity (New_S, Old_S, It.Nam);
7611 if Present (N_Ent) then
7612 return N_Ent;
7613 else
7614 return It.Nam;
7615 end if;
7616 end;
7617 end if;
7619 elsif Within (Old_S, Inst) then
7620 return Old_S;
7622 else
7623 return Report_Overload;
7624 end if;
7626 -- If not within an instance, ambiguity is real
7628 else
7629 return Report_Overload;
7630 end if;
7632 else
7633 Old_S := It1.Nam;
7634 exit;
7635 end if;
7637 else
7638 I1 := Ind;
7639 Old_S := It.Nam;
7640 end if;
7642 elsif
7643 Present (First_Formal (It.Nam))
7644 and then Present (First_Formal (New_S))
7645 and then Base_Type (Etype (First_Formal (It.Nam))) =
7646 Base_Type (Etype (First_Formal (New_S)))
7647 then
7648 Candidate_Renaming := It.Nam;
7649 end if;
7651 Get_Next_Interp (Ind, It);
7652 end loop;
7654 Set_Entity (Nam, Old_S);
7656 if Old_S /= Any_Id then
7657 Set_Is_Overloaded (Nam, False);
7658 end if;
7660 -- Non-overloaded case
7662 else
7663 if Is_Actual
7664 and then Present (Enclosing_Instance)
7665 and then Entity_Matches_Spec (Entity (Nam), New_S)
7666 then
7667 Old_S := Entity (Nam);
7669 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
7670 Candidate_Renaming := New_S;
7672 if Is_Visible_Operation (Entity (Nam)) then
7673 Old_S := Entity (Nam);
7674 end if;
7676 elsif Present (First_Formal (Entity (Nam)))
7677 and then Present (First_Formal (New_S))
7678 and then Base_Type (Etype (First_Formal (Entity (Nam)))) =
7679 Base_Type (Etype (First_Formal (New_S)))
7680 then
7681 Candidate_Renaming := Entity (Nam);
7682 end if;
7683 end if;
7685 return Old_S;
7686 end Find_Renamed_Entity;
7688 -----------------------------
7689 -- Find_Selected_Component --
7690 -----------------------------
7692 procedure Find_Selected_Component (N : Node_Id) is
7693 P : constant Node_Id := Prefix (N);
7695 P_Name : Entity_Id;
7696 -- Entity denoted by prefix
7698 P_Type : Entity_Id;
7699 -- and its type
7701 Nam : Node_Id;
7703 function Available_Subtype return Boolean;
7704 -- A small optimization: if the prefix is constrained and the component
7705 -- is an array type we may already have a usable subtype for it, so we
7706 -- can use it rather than generating a new one, because the bounds
7707 -- will be the values of the discriminants and not discriminant refs.
7708 -- This simplifies value tracing in GNATprove. For consistency, both
7709 -- the entity name and the subtype come from the constrained component.
7711 -- This is only used in GNATprove mode: when generating code it may be
7712 -- necessary to create an itype in the scope of use of the selected
7713 -- component, e.g. in the context of a expanded record equality.
7715 function Is_Reference_In_Subunit return Boolean;
7716 -- In a subunit, the scope depth is not a proper measure of hiding,
7717 -- because the context of the proper body may itself hide entities in
7718 -- parent units. This rare case requires inspecting the tree directly
7719 -- because the proper body is inserted in the main unit and its context
7720 -- is simply added to that of the parent.
7722 -----------------------
7723 -- Available_Subtype --
7724 -----------------------
7726 function Available_Subtype return Boolean is
7727 Comp : Entity_Id;
7729 begin
7730 if GNATprove_Mode then
7731 Comp := First_Entity (Etype (P));
7732 while Present (Comp) loop
7733 if Chars (Comp) = Chars (Selector_Name (N)) then
7734 Set_Etype (N, Etype (Comp));
7735 Set_Entity (Selector_Name (N), Comp);
7736 Set_Etype (Selector_Name (N), Etype (Comp));
7737 return True;
7738 end if;
7740 Next_Component (Comp);
7741 end loop;
7742 end if;
7744 return False;
7745 end Available_Subtype;
7747 -----------------------------
7748 -- Is_Reference_In_Subunit --
7749 -----------------------------
7751 function Is_Reference_In_Subunit return Boolean is
7752 Clause : Node_Id;
7753 Comp_Unit : Node_Id;
7755 begin
7756 Comp_Unit := N;
7757 while Present (Comp_Unit)
7758 and then Nkind (Comp_Unit) /= N_Compilation_Unit
7759 loop
7760 Comp_Unit := Parent (Comp_Unit);
7761 end loop;
7763 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
7764 return False;
7765 end if;
7767 -- Now check whether the package is in the context of the subunit
7769 Clause := First (Context_Items (Comp_Unit));
7770 while Present (Clause) loop
7771 if Nkind (Clause) = N_With_Clause
7772 and then Entity (Name (Clause)) = P_Name
7773 then
7774 return True;
7775 end if;
7777 Next (Clause);
7778 end loop;
7780 return False;
7781 end Is_Reference_In_Subunit;
7783 -- Start of processing for Find_Selected_Component
7785 begin
7786 Analyze (P);
7788 if Nkind (P) = N_Error then
7789 return;
7790 end if;
7792 -- If the selector already has an entity, the node has been constructed
7793 -- in the course of expansion, and is known to be valid. Do not verify
7794 -- that it is defined for the type (it may be a private component used
7795 -- in the expansion of record equality).
7797 if Present (Entity (Selector_Name (N))) then
7798 if No (Etype (N)) or else Etype (N) = Any_Type then
7799 declare
7800 Sel_Name : constant Node_Id := Selector_Name (N);
7801 Selector : constant Entity_Id := Entity (Sel_Name);
7802 C_Etype : Node_Id;
7804 begin
7805 Set_Etype (Sel_Name, Etype (Selector));
7807 if not Is_Entity_Name (P) then
7808 Resolve (P);
7809 end if;
7811 -- Build an actual subtype except for the first parameter
7812 -- of an init proc, where this actual subtype is by
7813 -- definition incorrect, since the object is uninitialized
7814 -- (and does not even have defined discriminants etc.)
7816 if Is_Entity_Name (P)
7817 and then Ekind (Entity (P)) = E_Function
7818 then
7819 Nam := New_Copy (P);
7821 if Is_Overloaded (P) then
7822 Save_Interps (P, Nam);
7823 end if;
7825 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
7826 Analyze_Call (P);
7827 Analyze_Selected_Component (N);
7828 return;
7830 elsif Ekind (Selector) = E_Component
7831 and then (not Is_Entity_Name (P)
7832 or else Chars (Entity (P)) /= Name_uInit)
7833 then
7834 -- Check if we already have an available subtype we can use
7836 if Ekind (Etype (P)) = E_Record_Subtype
7837 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
7838 and then Is_Array_Type (Etype (Selector))
7839 and then not Is_Packed (Etype (Selector))
7840 and then Available_Subtype
7841 then
7842 return;
7844 -- Do not build the subtype when referencing components of
7845 -- dispatch table wrappers. Required to avoid generating
7846 -- elaboration code with HI runtimes.
7848 elsif Is_RTE (Scope (Selector), RE_Dispatch_Table_Wrapper)
7849 or else
7850 Is_RTE (Scope (Selector), RE_No_Dispatch_Table_Wrapper)
7851 then
7852 C_Etype := Empty;
7853 else
7854 C_Etype :=
7855 Build_Actual_Subtype_Of_Component
7856 (Etype (Selector), N);
7857 end if;
7859 else
7860 C_Etype := Empty;
7861 end if;
7863 if No (C_Etype) then
7864 C_Etype := Etype (Selector);
7865 else
7866 Insert_Action (N, C_Etype);
7867 C_Etype := Defining_Identifier (C_Etype);
7868 end if;
7870 Set_Etype (N, C_Etype);
7871 end;
7873 -- If the selected component appears within a default expression
7874 -- and it has an actual subtype, the preanalysis has not yet
7875 -- completed its analysis, because Insert_Actions is disabled in
7876 -- that context. Within the init proc of the enclosing type we
7877 -- must complete this analysis, if an actual subtype was created.
7879 elsif Inside_Init_Proc then
7880 declare
7881 Typ : constant Entity_Id := Etype (N);
7882 Decl : constant Node_Id := Declaration_Node (Typ);
7883 begin
7884 if Nkind (Decl) = N_Subtype_Declaration
7885 and then not Analyzed (Decl)
7886 and then Is_List_Member (Decl)
7887 and then No (Parent (Decl))
7888 then
7889 Remove (Decl);
7890 Insert_Action (N, Decl);
7891 end if;
7892 end;
7893 end if;
7895 return;
7897 elsif Is_Entity_Name (P) then
7898 P_Name := Entity (P);
7900 -- The prefix may denote an enclosing type which is the completion
7901 -- of an incomplete type declaration.
7903 if Is_Type (P_Name) then
7904 Set_Entity (P, Get_Full_View (P_Name));
7905 Set_Etype (P, Entity (P));
7906 P_Name := Entity (P);
7907 end if;
7909 P_Type := Base_Type (Etype (P));
7911 if Debug_Flag_E then
7912 Write_Str ("Found prefix type to be ");
7913 Write_Entity_Info (P_Type, " "); Write_Eol;
7914 end if;
7916 -- If the prefix's type is an access type, get to the record type
7918 if Is_Access_Type (P_Type) then
7919 P_Type := Implicitly_Designated_Type (P_Type);
7920 end if;
7922 -- First check for components of a record object (not the result of
7923 -- a call, which is handled below). This also covers the case where
7924 -- the extension feature that supports the prefixed form of calls
7925 -- for primitives of untagged types is enabled (excluding concurrent
7926 -- cases, which are handled further below).
7928 if Is_Type (P_Type)
7929 and then (Has_Components (P_Type)
7930 or else (Core_Extensions_Allowed
7931 and then not Is_Concurrent_Type (P_Type)))
7932 and then not Is_Overloadable (P_Name)
7933 and then not Is_Type (P_Name)
7934 then
7935 -- Selected component of record. Type checking will validate
7936 -- name of selector.
7938 -- ??? Could we rewrite an implicit dereference into an explicit
7939 -- one here?
7941 Analyze_Selected_Component (N);
7943 -- Reference to type name in predicate/invariant expression
7945 elsif Is_Concurrent_Type (P_Type)
7946 and then not In_Open_Scopes (P_Name)
7947 and then (not Is_Concurrent_Type (Etype (P_Name))
7948 or else not In_Open_Scopes (Etype (P_Name)))
7949 then
7950 -- Call to protected operation or entry. Type checking is
7951 -- needed on the prefix.
7953 Analyze_Selected_Component (N);
7955 elsif (In_Open_Scopes (P_Name)
7956 and then Ekind (P_Name) /= E_Void
7957 and then not Is_Overloadable (P_Name))
7958 or else (Is_Concurrent_Type (Etype (P_Name))
7959 and then In_Open_Scopes (Etype (P_Name)))
7960 then
7961 -- Prefix denotes an enclosing loop, block, or task, i.e. an
7962 -- enclosing construct that is not a subprogram or accept.
7964 -- A special case: a protected body may call an operation
7965 -- on an external object of the same type, in which case it
7966 -- is not an expanded name. If the prefix is the type itself,
7967 -- or the context is a single synchronized object it can only
7968 -- be interpreted as an expanded name.
7970 if Is_Concurrent_Type (Etype (P_Name)) then
7971 if Is_Type (P_Name)
7972 or else Present (Anonymous_Object (Etype (P_Name)))
7973 then
7974 Find_Expanded_Name (N);
7976 else
7977 Analyze_Selected_Component (N);
7978 return;
7979 end if;
7981 else
7982 Find_Expanded_Name (N);
7983 end if;
7985 elsif Ekind (P_Name) = E_Package then
7986 Find_Expanded_Name (N);
7988 elsif Is_Overloadable (P_Name) then
7990 -- The subprogram may be a renaming (of an enclosing scope) as
7991 -- in the case of the name of the generic within an instantiation.
7993 if Ekind (P_Name) in E_Procedure | E_Function
7994 and then Present (Alias (P_Name))
7995 and then Is_Generic_Instance (Alias (P_Name))
7996 then
7997 P_Name := Alias (P_Name);
7998 end if;
8000 if Is_Overloaded (P) then
8002 -- The prefix must resolve to a unique enclosing construct
8004 declare
8005 Found : Boolean := False;
8006 Ind : Interp_Index;
8007 It : Interp;
8009 begin
8010 Get_First_Interp (P, Ind, It);
8011 while Present (It.Nam) loop
8012 if In_Open_Scopes (It.Nam) then
8013 if Found then
8014 Error_Msg_N (
8015 "prefix must be unique enclosing scope", N);
8016 Set_Entity (N, Any_Id);
8017 Set_Etype (N, Any_Type);
8018 return;
8020 else
8021 Found := True;
8022 P_Name := It.Nam;
8023 end if;
8024 end if;
8026 Get_Next_Interp (Ind, It);
8027 end loop;
8028 end;
8029 end if;
8031 if In_Open_Scopes (P_Name) then
8032 Set_Entity (P, P_Name);
8033 Set_Is_Overloaded (P, False);
8034 Find_Expanded_Name (N);
8036 else
8037 -- If no interpretation as an expanded name is possible, it
8038 -- must be a selected component of a record returned by a
8039 -- function call. Reformat prefix as a function call, the rest
8040 -- is done by type resolution.
8042 -- Error if the prefix is procedure or entry, as is P.X
8044 if Ekind (P_Name) /= E_Function
8045 and then
8046 (not Is_Overloaded (P)
8047 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
8048 then
8049 -- Prefix may mention a package that is hidden by a local
8050 -- declaration: let the user know. Scan the full homonym
8051 -- chain, the candidate package may be anywhere on it.
8053 if Present (Homonym (Current_Entity (P_Name))) then
8054 P_Name := Current_Entity (P_Name);
8056 while Present (P_Name) loop
8057 exit when Ekind (P_Name) = E_Package;
8058 P_Name := Homonym (P_Name);
8059 end loop;
8061 if Present (P_Name) then
8062 if not Is_Reference_In_Subunit then
8063 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
8064 Error_Msg_NE
8065 ("package& is hidden by declaration#", N, P_Name);
8066 end if;
8068 Set_Entity (Prefix (N), P_Name);
8069 Find_Expanded_Name (N);
8070 return;
8072 else
8073 P_Name := Entity (Prefix (N));
8074 end if;
8075 end if;
8077 Error_Msg_NE
8078 ("invalid prefix in selected component&", N, P_Name);
8079 Change_Selected_Component_To_Expanded_Name (N);
8080 Set_Entity (N, Any_Id);
8081 Set_Etype (N, Any_Type);
8083 -- Here we have a function call, so do the reformatting
8085 else
8086 Nam := New_Copy (P);
8087 Save_Interps (P, Nam);
8089 -- We use Replace here because this is one of those cases
8090 -- where the parser has missclassified the node, and we fix
8091 -- things up and then do the semantic analysis on the fixed
8092 -- up node. Normally we do this using one of the Sinfo.CN
8093 -- routines, but this is too tricky for that.
8095 -- Note that using Rewrite would be wrong, because we would
8096 -- have a tree where the original node is unanalyzed.
8098 Replace (P,
8099 Make_Function_Call (Sloc (P), Name => Nam));
8101 -- Now analyze the reformatted node
8103 Analyze_Call (P);
8105 -- If the prefix is illegal after this transformation, there
8106 -- may be visibility errors on the prefix. The safest is to
8107 -- treat the selected component as an error.
8109 if Error_Posted (P) then
8110 Set_Etype (N, Any_Type);
8111 return;
8113 else
8114 Analyze_Selected_Component (N);
8115 end if;
8116 end if;
8117 end if;
8119 -- Remaining cases generate various error messages
8121 else
8122 -- Format node as expanded name, to avoid cascaded errors
8124 Change_Selected_Component_To_Expanded_Name (N);
8125 Set_Entity (N, Any_Id);
8126 Set_Etype (N, Any_Type);
8128 -- Issue error message, but avoid this if error issued already.
8129 -- Use identifier of prefix if one is available.
8131 if P_Name = Any_Id then
8132 null;
8134 -- It is not an error if the prefix is the current instance of
8135 -- type name, e.g. the expression of a type aspect, when it is
8136 -- analyzed within a generic unit. We still have to verify that a
8137 -- component of that name exists, and decorate the node
8138 -- accordingly.
8140 elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then
8141 declare
8142 Comp : Entity_Id;
8144 begin
8145 Comp := First_Entity (Entity (P));
8146 while Present (Comp) loop
8147 if Chars (Comp) = Chars (Selector_Name (N)) then
8148 Set_Entity (N, Comp);
8149 Set_Etype (N, Etype (Comp));
8150 Set_Entity (Selector_Name (N), Comp);
8151 Set_Etype (Selector_Name (N), Etype (Comp));
8152 return;
8153 end if;
8155 Next_Entity (Comp);
8156 end loop;
8157 end;
8159 elsif Is_Self_Hidden (P_Name) then
8160 Premature_Usage (P);
8162 elsif Ekind (P_Name) = E_Generic_Package then
8163 Error_Msg_N ("prefix must not be a generic package", N);
8164 Error_Msg_N ("\use package instantiation as prefix instead", N);
8166 elsif Nkind (P) /= N_Attribute_Reference then
8168 -- This may have been meant as a prefixed call to a primitive
8169 -- of an untagged type. If it is a function call check type of
8170 -- its first formal and add explanation.
8172 declare
8173 F : constant Entity_Id :=
8174 Current_Entity (Selector_Name (N));
8175 begin
8176 if Present (F)
8177 and then Is_Overloadable (F)
8178 and then Present (First_Entity (F))
8179 and then not Is_Tagged_Type (Etype (First_Entity (F)))
8180 then
8181 Error_Msg_N
8182 ("prefixed call is only allowed for objects of a "
8183 & "tagged type unless -gnatX is used", N);
8185 if not Core_Extensions_Allowed
8186 and then
8187 Try_Object_Operation (N, Allow_Extensions => True)
8188 then
8189 Error_Msg_N
8190 ("\using -gnatX would make the prefixed call legal",
8192 end if;
8193 end if;
8194 end;
8196 Error_Msg_N ("invalid prefix in selected component&", P);
8198 if Is_Incomplete_Type (P_Type)
8199 and then Is_Access_Type (Etype (P))
8200 then
8201 Error_Msg_N
8202 ("\dereference must not be of an incomplete type "
8203 & "(RM 3.10.1)", P);
8204 end if;
8206 else
8207 Error_Msg_N ("invalid prefix in selected component", P);
8208 end if;
8209 end if;
8210 else
8211 -- If prefix is not the name of an entity, it must be an expression,
8212 -- whose type is appropriate for a record. This is determined by
8213 -- type resolution.
8215 Analyze_Selected_Component (N);
8216 end if;
8218 Analyze_Dimension (N);
8219 end Find_Selected_Component;
8221 ---------------
8222 -- Find_Type --
8223 ---------------
8225 procedure Find_Type (N : Node_Id) is
8226 C : Entity_Id;
8227 Typ : Entity_Id;
8228 T : Entity_Id;
8229 T_Name : Entity_Id;
8231 begin
8232 if N = Error then
8233 return;
8235 elsif Nkind (N) = N_Attribute_Reference then
8237 -- Class attribute. This is not valid in Ada 83 mode, but we do not
8238 -- need to enforce that at this point, since the declaration of the
8239 -- tagged type in the prefix would have been flagged already.
8241 if Attribute_Name (N) = Name_Class then
8242 Check_Restriction (No_Dispatch, N);
8243 Find_Type (Prefix (N));
8245 -- Propagate error from bad prefix
8247 if Etype (Prefix (N)) = Any_Type then
8248 Set_Entity (N, Any_Type);
8249 Set_Etype (N, Any_Type);
8250 return;
8251 end if;
8253 T := Base_Type (Entity (Prefix (N)));
8255 -- Case where type is not known to be tagged. Its appearance in
8256 -- the prefix of the 'Class attribute indicates that the full view
8257 -- will be tagged.
8259 if not Is_Tagged_Type (T) then
8260 if Ekind (T) = E_Incomplete_Type then
8262 -- It is legal to denote the class type of an incomplete
8263 -- type. The full type will have to be tagged, of course.
8264 -- In Ada 2005 this usage is declared obsolescent, so we
8265 -- warn accordingly. This usage is only legal if the type
8266 -- is completed in the current scope, and not for a limited
8267 -- view of a type.
8269 if Ada_Version >= Ada_2005 then
8271 -- Test whether the Available_View of a limited type view
8272 -- is tagged, since the limited view may not be marked as
8273 -- tagged if the type itself has an untagged incomplete
8274 -- type view in its package.
8276 if From_Limited_With (T)
8277 and then not Is_Tagged_Type (Available_View (T))
8278 then
8279 Error_Msg_N
8280 ("prefix of Class attribute must be tagged", N);
8281 Set_Etype (N, Any_Type);
8282 Set_Entity (N, Any_Type);
8283 return;
8285 else
8286 if Restriction_Check_Required (No_Obsolescent_Features)
8287 then
8288 Check_Restriction
8289 (No_Obsolescent_Features, Prefix (N));
8290 end if;
8292 if Warn_On_Obsolescent_Feature then
8293 Error_Msg_N
8294 ("applying ''Class to an untagged incomplete type"
8295 & " is an obsolescent feature (RM J.11)?r?", N);
8296 end if;
8297 end if;
8298 end if;
8300 Set_Is_Tagged_Type (T);
8301 Set_Direct_Primitive_Operations (T, New_Elmt_List);
8302 Make_Class_Wide_Type (T);
8303 Set_Entity (N, Class_Wide_Type (T));
8304 Set_Etype (N, Class_Wide_Type (T));
8306 elsif Ekind (T) = E_Private_Type
8307 and then not Is_Generic_Type (T)
8308 and then In_Private_Part (Scope (T))
8309 then
8310 -- The Class attribute can be applied to an untagged private
8311 -- type fulfilled by a tagged type prior to the full type
8312 -- declaration (but only within the parent package's private
8313 -- part). Create the class-wide type now and check that the
8314 -- full type is tagged later during its analysis. Note that
8315 -- we do not mark the private type as tagged, unlike the
8316 -- case of incomplete types, because the type must still
8317 -- appear untagged to outside units.
8319 if No (Class_Wide_Type (T)) then
8320 Make_Class_Wide_Type (T);
8321 end if;
8323 Set_Entity (N, Class_Wide_Type (T));
8324 Set_Etype (N, Class_Wide_Type (T));
8326 else
8327 -- Should we introduce a type Any_Tagged and use Wrong_Type
8328 -- here, it would be a bit more consistent???
8330 Error_Msg_NE
8331 ("tagged type required, found}",
8332 Prefix (N), First_Subtype (T));
8333 Set_Entity (N, Any_Type);
8334 return;
8335 end if;
8337 -- Case of tagged type
8339 else
8340 if Is_Concurrent_Type (T) then
8341 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
8343 -- Previous error. Create a class-wide type for the
8344 -- synchronized type itself, with minimal semantic
8345 -- attributes, to catch other errors in some ACATS tests.
8347 pragma Assert (Serious_Errors_Detected /= 0);
8348 Make_Class_Wide_Type (T);
8349 C := Class_Wide_Type (T);
8350 Set_First_Entity (C, First_Entity (T));
8352 else
8353 C := Class_Wide_Type
8354 (Corresponding_Record_Type (Entity (Prefix (N))));
8355 end if;
8357 else
8358 C := Class_Wide_Type (Entity (Prefix (N)));
8359 end if;
8361 Set_Entity_With_Checks (N, C);
8362 Generate_Reference (C, N);
8363 Set_Etype (N, C);
8364 end if;
8366 -- Base attribute, not allowed in Ada 83
8368 elsif Attribute_Name (N) = Name_Base then
8369 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
8370 Error_Msg_N
8371 ("(Ada 83) Base attribute not allowed in subtype mark", N);
8373 else
8374 Find_Type (Prefix (N));
8375 Typ := Entity (Prefix (N));
8377 if Ada_Version >= Ada_95
8378 and then not Is_Scalar_Type (Typ)
8379 and then not Is_Generic_Type (Typ)
8380 then
8381 Error_Msg_N
8382 ("prefix of Base attribute must be scalar type",
8383 Prefix (N));
8385 elsif Warn_On_Redundant_Constructs
8386 and then Base_Type (Typ) = Typ
8387 then
8388 Error_Msg_NE -- CODEFIX
8389 ("redundant attribute, & is its own base type?r?", N, Typ);
8390 end if;
8392 T := Base_Type (Typ);
8394 -- Rewrite attribute reference with type itself (see similar
8395 -- processing in Analyze_Attribute, case Base). Preserve prefix
8396 -- if present, for other legality checks.
8398 if Nkind (Prefix (N)) = N_Expanded_Name then
8399 Rewrite (N,
8400 Make_Expanded_Name (Sloc (N),
8401 Chars => Chars (T),
8402 Prefix => New_Copy (Prefix (Prefix (N))),
8403 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
8405 else
8406 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
8407 end if;
8409 Set_Entity (N, T);
8410 Set_Etype (N, T);
8411 end if;
8413 elsif Attribute_Name (N) = Name_Stub_Type then
8415 -- This is handled in Analyze_Attribute
8417 Analyze (N);
8419 -- All other attributes are invalid in a subtype mark
8421 else
8422 Error_Msg_N ("invalid attribute in subtype mark", N);
8423 end if;
8425 else
8426 Analyze (N);
8428 if Is_Entity_Name (N) then
8429 T_Name := Entity (N);
8430 else
8431 Error_Msg_N ("subtype mark required in this context", N);
8432 Set_Etype (N, Any_Type);
8433 return;
8434 end if;
8436 if T_Name = Any_Id or else Etype (N) = Any_Type then
8438 -- Undefined id. Make it into a valid type
8440 Set_Entity (N, Any_Type);
8442 elsif not Is_Type (T_Name)
8443 and then T_Name /= Standard_Void_Type
8444 then
8445 Error_Msg_Sloc := Sloc (T_Name);
8446 Error_Msg_N ("subtype mark required in this context", N);
8447 Error_Msg_NE ("\\found & declared#", N, T_Name);
8448 Set_Entity (N, Any_Type);
8450 else
8451 -- If the type is an incomplete type created to handle
8452 -- anonymous access components of a record type, then the
8453 -- incomplete type is the visible entity and subsequent
8454 -- references will point to it. Mark the original full
8455 -- type as referenced, to prevent spurious warnings.
8457 if Is_Incomplete_Type (T_Name)
8458 and then Present (Full_View (T_Name))
8459 and then not Comes_From_Source (T_Name)
8460 then
8461 Set_Referenced (Full_View (T_Name));
8462 end if;
8464 T_Name := Get_Full_View (T_Name);
8466 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
8467 -- limited-with clauses
8469 if From_Limited_With (T_Name)
8470 and then Is_Incomplete_Type (T_Name)
8471 and then Present (Non_Limited_View (T_Name))
8472 and then Is_Interface (Non_Limited_View (T_Name))
8473 then
8474 T_Name := Non_Limited_View (T_Name);
8475 end if;
8477 if In_Open_Scopes (T_Name) then
8478 if Ekind (Base_Type (T_Name)) = E_Task_Type then
8480 -- In Ada 2005, a task name can be used in an access
8481 -- definition within its own body.
8483 if Ada_Version >= Ada_2005
8484 and then Nkind (Parent (N)) = N_Access_Definition
8485 then
8486 Set_Entity (N, T_Name);
8487 Set_Etype (N, T_Name);
8488 return;
8490 else
8491 Error_Msg_N
8492 ("task type cannot be used as type mark " &
8493 "within its own spec or body", N);
8494 end if;
8496 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
8498 -- In Ada 2005, a protected name can be used in an access
8499 -- definition within its own body.
8501 if Ada_Version >= Ada_2005
8502 and then Nkind (Parent (N)) = N_Access_Definition
8503 then
8504 Set_Entity (N, T_Name);
8505 Set_Etype (N, T_Name);
8506 return;
8508 else
8509 Error_Msg_N
8510 ("protected type cannot be used as type mark " &
8511 "within its own spec or body", N);
8512 end if;
8514 else
8515 Error_Msg_N ("type declaration cannot refer to itself", N);
8516 end if;
8518 Set_Etype (N, Any_Type);
8519 Set_Entity (N, Any_Type);
8520 Set_Error_Posted (T_Name);
8521 return;
8522 end if;
8524 Set_Entity (N, T_Name);
8525 Set_Etype (N, T_Name);
8526 end if;
8527 end if;
8529 if Present (Etype (N)) and then Comes_From_Source (N) then
8530 if Is_Fixed_Point_Type (Etype (N)) then
8531 Check_Restriction (No_Fixed_Point, N);
8532 elsif Is_Floating_Point_Type (Etype (N)) then
8533 Check_Restriction (No_Floating_Point, N);
8534 end if;
8536 -- A Ghost type must appear in a specific context
8538 if Is_Ghost_Entity (Etype (N)) then
8539 Check_Ghost_Context (Etype (N), N);
8540 end if;
8541 end if;
8542 end Find_Type;
8544 --------------------
8545 -- Has_Components --
8546 --------------------
8548 function Has_Components (Typ : Entity_Id) return Boolean is
8549 begin
8550 return Is_Record_Type (Typ)
8551 or else (Is_Private_Type (Typ) and then Has_Discriminants (Typ))
8552 or else (Is_Task_Type (Typ) and then Has_Discriminants (Typ))
8553 or else (Is_Incomplete_Type (Typ)
8554 and then From_Limited_With (Typ)
8555 and then Is_Record_Type (Available_View (Typ)));
8556 end Has_Components;
8558 ------------------------------------
8559 -- Has_Implicit_Character_Literal --
8560 ------------------------------------
8562 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
8563 Id : Entity_Id;
8564 Found : Boolean := False;
8565 P : constant Entity_Id := Entity (Prefix (N));
8566 Priv_Id : Entity_Id := Empty;
8568 begin
8569 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8570 Priv_Id := First_Private_Entity (P);
8571 end if;
8573 if P = Standard_Standard then
8574 Change_Selected_Component_To_Expanded_Name (N);
8575 Rewrite (N, Selector_Name (N));
8576 Analyze (N);
8577 Set_Etype (Original_Node (N), Standard_Character);
8578 return True;
8579 end if;
8581 Id := First_Entity (P);
8582 while Present (Id) and then Id /= Priv_Id loop
8583 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
8585 -- We replace the node with the literal itself, resolve as a
8586 -- character, and set the type correctly.
8588 if not Found then
8589 Change_Selected_Component_To_Expanded_Name (N);
8590 Rewrite (N, Selector_Name (N));
8591 Analyze (N);
8592 Set_Etype (N, Id);
8593 Set_Etype (Original_Node (N), Id);
8594 Found := True;
8596 else
8597 -- More than one type derived from Character in given scope.
8598 -- Collect all possible interpretations.
8600 Add_One_Interp (N, Id, Id);
8601 end if;
8602 end if;
8604 Next_Entity (Id);
8605 end loop;
8607 return Found;
8608 end Has_Implicit_Character_Literal;
8610 ----------------------
8611 -- Has_Private_With --
8612 ----------------------
8614 function Has_Private_With (E : Entity_Id) return Boolean is
8615 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
8616 Item : Node_Id;
8618 begin
8619 Item := First (Context_Items (Comp_Unit));
8620 while Present (Item) loop
8621 if Nkind (Item) = N_With_Clause
8622 and then Private_Present (Item)
8623 and then Entity (Name (Item)) = E
8624 then
8625 return True;
8626 end if;
8628 Next (Item);
8629 end loop;
8631 return False;
8632 end Has_Private_With;
8634 ---------------------------
8635 -- Has_Implicit_Operator --
8636 ---------------------------
8638 function Has_Implicit_Operator (N : Node_Id) return Boolean is
8639 Op_Id : constant Name_Id := Chars (Selector_Name (N));
8640 P : constant Entity_Id := Entity (Prefix (N));
8641 Id : Entity_Id;
8642 Priv_Id : Entity_Id := Empty;
8644 procedure Add_Implicit_Operator
8645 (T : Entity_Id;
8646 Op_Type : Entity_Id := Empty);
8647 -- Add implicit interpretation to node N, using the type for which a
8648 -- predefined operator exists. If the operator yields a boolean type,
8649 -- the Operand_Type is implicitly referenced by the operator, and a
8650 -- reference to it must be generated.
8652 ---------------------------
8653 -- Add_Implicit_Operator --
8654 ---------------------------
8656 procedure Add_Implicit_Operator
8657 (T : Entity_Id;
8658 Op_Type : Entity_Id := Empty)
8660 Predef_Op : Entity_Id;
8662 begin
8663 Predef_Op := Current_Entity (Selector_Name (N));
8664 while Present (Predef_Op)
8665 and then Scope (Predef_Op) /= Standard_Standard
8666 loop
8667 Predef_Op := Homonym (Predef_Op);
8668 end loop;
8670 if Nkind (N) = N_Selected_Component then
8671 Change_Selected_Component_To_Expanded_Name (N);
8672 end if;
8674 -- If the context is an unanalyzed function call, determine whether
8675 -- a binary or unary interpretation is required.
8677 if Nkind (Parent (N)) = N_Indexed_Component then
8678 declare
8679 Is_Binary_Call : constant Boolean :=
8680 Present
8681 (Next (First (Expressions (Parent (N)))));
8682 Is_Binary_Op : constant Boolean :=
8683 First_Entity
8684 (Predef_Op) /= Last_Entity (Predef_Op);
8685 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
8687 begin
8688 if Is_Binary_Call then
8689 if Is_Binary_Op then
8690 Add_One_Interp (N, Predef_Op, T);
8691 else
8692 Add_One_Interp (N, Predef_Op2, T);
8693 end if;
8694 else
8695 if not Is_Binary_Op then
8696 Add_One_Interp (N, Predef_Op, T);
8698 -- Predef_Op2 may be empty in case of previous errors
8700 elsif Present (Predef_Op2) then
8701 Add_One_Interp (N, Predef_Op2, T);
8702 end if;
8703 end if;
8704 end;
8706 else
8707 Add_One_Interp (N, Predef_Op, T);
8709 -- For operators with unary and binary interpretations, if
8710 -- context is not a call, add both
8712 if Present (Homonym (Predef_Op)) then
8713 Add_One_Interp (N, Homonym (Predef_Op), T);
8714 end if;
8715 end if;
8717 -- The node is a reference to a predefined operator, and
8718 -- an implicit reference to the type of its operands.
8720 if Present (Op_Type) then
8721 Generate_Operator_Reference (N, Op_Type);
8722 else
8723 Generate_Operator_Reference (N, T);
8724 end if;
8725 end Add_Implicit_Operator;
8727 -- Start of processing for Has_Implicit_Operator
8729 begin
8730 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8731 Priv_Id := First_Private_Entity (P);
8732 end if;
8734 Id := First_Entity (P);
8736 case Op_Id is
8738 -- Boolean operators: an implicit declaration exists if the scope
8739 -- contains a declaration for a derived Boolean type, or for an
8740 -- array of Boolean type.
8742 when Name_Op_And
8743 | Name_Op_Not
8744 | Name_Op_Or
8745 | Name_Op_Xor
8747 while Id /= Priv_Id loop
8748 if Is_Type (Id)
8749 and then Valid_Boolean_Arg (Id)
8750 and then Is_Base_Type (Id)
8751 then
8752 Add_Implicit_Operator (Id);
8753 return True;
8754 end if;
8756 Next_Entity (Id);
8757 end loop;
8759 -- Equality: look for any non-limited type (result is Boolean)
8761 when Name_Op_Eq
8762 | Name_Op_Ne
8764 while Id /= Priv_Id loop
8765 if Is_Type (Id)
8766 and then Valid_Equality_Arg (Id)
8767 and then Is_Base_Type (Id)
8768 then
8769 Add_Implicit_Operator (Standard_Boolean, Id);
8770 return True;
8771 end if;
8773 Next_Entity (Id);
8774 end loop;
8776 -- Comparison operators: scalar type, or array of scalar
8778 when Name_Op_Ge
8779 | Name_Op_Gt
8780 | Name_Op_Le
8781 | Name_Op_Lt
8783 while Id /= Priv_Id loop
8784 if Is_Type (Id)
8785 and then Valid_Comparison_Arg (Id)
8786 and then Is_Base_Type (Id)
8787 then
8788 Add_Implicit_Operator (Standard_Boolean, Id);
8789 return True;
8790 end if;
8792 Next_Entity (Id);
8793 end loop;
8795 -- Arithmetic operators: any numeric type
8797 when Name_Op_Abs
8798 | Name_Op_Add
8799 | Name_Op_Divide
8800 | Name_Op_Expon
8801 | Name_Op_Mod
8802 | Name_Op_Multiply
8803 | Name_Op_Rem
8804 | Name_Op_Subtract
8806 while Id /= Priv_Id loop
8807 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
8808 Add_Implicit_Operator (Id);
8809 return True;
8810 end if;
8812 Next_Entity (Id);
8813 end loop;
8815 -- Concatenation: any one-dimensional array type
8817 when Name_Op_Concat =>
8818 while Id /= Priv_Id loop
8819 if Is_Array_Type (Id)
8820 and then Number_Dimensions (Id) = 1
8821 and then Is_Base_Type (Id)
8822 then
8823 Add_Implicit_Operator (Id);
8824 return True;
8825 end if;
8827 Next_Entity (Id);
8828 end loop;
8830 -- What is the others condition here? Should we be using a
8831 -- subtype of Name_Id that would restrict to operators ???
8833 when others =>
8834 null;
8835 end case;
8837 -- If we fall through, then we do not have an implicit operator
8839 return False;
8840 end Has_Implicit_Operator;
8842 -----------------------------------
8843 -- Has_Loop_In_Inner_Open_Scopes --
8844 -----------------------------------
8846 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
8847 begin
8848 -- Several scope stacks are maintained by Scope_Stack. The base of the
8849 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8850 -- flag in the scope stack entry. Note that the scope stacks used to
8851 -- simply be delimited implicitly by the presence of Standard_Standard
8852 -- at their base, but there now are cases where this is not sufficient
8853 -- because Standard_Standard actually may appear in the middle of the
8854 -- active set of scopes.
8856 for J in reverse 0 .. Scope_Stack.Last loop
8858 -- S was reached without seing a loop scope first
8860 if Scope_Stack.Table (J).Entity = S then
8861 return False;
8863 -- S was not yet reached, so it contains at least one inner loop
8865 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
8866 return True;
8867 end if;
8869 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8870 -- cases where Standard_Standard appears in the middle of the active
8871 -- set of scopes. This affects the declaration and overriding of
8872 -- private inherited operations in instantiations of generic child
8873 -- units.
8875 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
8876 end loop;
8878 raise Program_Error; -- unreachable
8879 end Has_Loop_In_Inner_Open_Scopes;
8881 --------------------
8882 -- In_Open_Scopes --
8883 --------------------
8885 function In_Open_Scopes (S : Entity_Id) return Boolean is
8886 begin
8887 -- Several scope stacks are maintained by Scope_Stack. The base of the
8888 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8889 -- flag in the scope stack entry. Note that the scope stacks used to
8890 -- simply be delimited implicitly by the presence of Standard_Standard
8891 -- at their base, but there now are cases where this is not sufficient
8892 -- because Standard_Standard actually may appear in the middle of the
8893 -- active set of scopes.
8895 for J in reverse 0 .. Scope_Stack.Last loop
8896 if Scope_Stack.Table (J).Entity = S then
8897 return True;
8898 end if;
8900 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8901 -- cases where Standard_Standard appears in the middle of the active
8902 -- set of scopes. This affects the declaration and overriding of
8903 -- private inherited operations in instantiations of generic child
8904 -- units.
8906 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
8907 end loop;
8909 return False;
8910 end In_Open_Scopes;
8912 -----------------------------
8913 -- Inherit_Renamed_Profile --
8914 -----------------------------
8916 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
8917 New_F : Entity_Id;
8918 Old_F : Entity_Id;
8919 Old_T : Entity_Id;
8920 New_T : Entity_Id;
8922 begin
8923 if Ekind (Old_S) = E_Operator then
8924 New_F := First_Formal (New_S);
8926 while Present (New_F) loop
8927 Set_Etype (New_F, Base_Type (Etype (New_F)));
8928 Next_Formal (New_F);
8929 end loop;
8931 Set_Etype (New_S, Base_Type (Etype (New_S)));
8933 else
8934 New_F := First_Formal (New_S);
8935 Old_F := First_Formal (Old_S);
8937 while Present (New_F) loop
8938 New_T := Etype (New_F);
8939 Old_T := Etype (Old_F);
8941 -- If the new type is a renaming of the old one, as is the case
8942 -- for actuals in instances, retain its name, to simplify later
8943 -- disambiguation.
8945 if Nkind (Parent (New_T)) = N_Subtype_Declaration
8946 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
8947 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
8948 then
8949 null;
8950 else
8951 Set_Etype (New_F, Old_T);
8952 end if;
8954 Next_Formal (New_F);
8955 Next_Formal (Old_F);
8956 end loop;
8958 pragma Assert (No (Old_F));
8960 if Ekind (Old_S) in E_Function | E_Enumeration_Literal then
8961 Set_Etype (New_S, Etype (Old_S));
8962 end if;
8963 end if;
8964 end Inherit_Renamed_Profile;
8966 ----------------
8967 -- Initialize --
8968 ----------------
8970 procedure Initialize is
8971 begin
8972 Urefs.Init;
8973 end Initialize;
8975 -------------------------
8976 -- Install_Use_Clauses --
8977 -------------------------
8979 procedure Install_Use_Clauses
8980 (Clause : Node_Id;
8981 Force_Installation : Boolean := False)
8983 U : Node_Id;
8985 begin
8986 U := Clause;
8987 while Present (U) loop
8989 -- Case of USE package
8991 if Nkind (U) = N_Use_Package_Clause then
8992 Use_One_Package (U, Name (U), True);
8994 -- Case of USE TYPE
8996 else
8997 Use_One_Type (Subtype_Mark (U), Force => Force_Installation);
8999 end if;
9001 Next_Use_Clause (U);
9002 end loop;
9003 end Install_Use_Clauses;
9005 ----------------------
9006 -- Mark_Use_Clauses --
9007 ----------------------
9009 procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is
9010 procedure Mark_Parameters (Call : Entity_Id);
9011 -- Perform use_type_clause marking for all parameters in a subprogram
9012 -- or operator call.
9014 procedure Mark_Use_Package (Pak : Entity_Id);
9015 -- Move up the Prev_Use_Clause chain for packages denoted by Pak -
9016 -- marking each clause in the chain as effective in the process.
9018 procedure Mark_Use_Type (E : Entity_Id);
9019 -- Similar to Do_Use_Package_Marking except we move up the
9020 -- Prev_Use_Clause chain for the type denoted by E.
9022 ---------------------
9023 -- Mark_Parameters --
9024 ---------------------
9026 procedure Mark_Parameters (Call : Entity_Id) is
9027 Curr : Node_Id;
9029 begin
9030 -- Move through all of the formals
9032 Curr := First_Formal (Call);
9033 while Present (Curr) loop
9034 Mark_Use_Type (Curr);
9036 Next_Formal (Curr);
9037 end loop;
9039 -- Handle the return type
9041 Mark_Use_Type (Call);
9042 end Mark_Parameters;
9044 ----------------------
9045 -- Mark_Use_Package --
9046 ----------------------
9048 procedure Mark_Use_Package (Pak : Entity_Id) is
9049 Curr : Node_Id;
9051 begin
9052 -- Ignore cases where the scope of the type is not a package (e.g.
9053 -- Standard_Standard).
9055 if Ekind (Pak) /= E_Package then
9056 return;
9057 end if;
9059 Curr := Current_Use_Clause (Pak);
9060 while Present (Curr)
9061 and then not Is_Effective_Use_Clause (Curr)
9062 loop
9063 -- We need to mark the previous use clauses as effective, but
9064 -- each use clause may in turn render other use_package_clauses
9065 -- effective. Additionally, it is possible to have a parent
9066 -- package renamed as a child of itself so we must check the
9067 -- prefix entity is not the same as the package we are marking.
9069 if Nkind (Name (Curr)) /= N_Identifier
9070 and then Present (Prefix (Name (Curr)))
9071 and then Entity (Prefix (Name (Curr))) /= Pak
9072 then
9073 Mark_Use_Package (Entity (Prefix (Name (Curr))));
9075 -- It is also possible to have a child package without a prefix
9076 -- that relies on a previous use_package_clause.
9078 elsif Nkind (Name (Curr)) = N_Identifier
9079 and then Is_Child_Unit (Entity (Name (Curr)))
9080 then
9081 Mark_Use_Package (Scope (Entity (Name (Curr))));
9082 end if;
9084 -- Mark the use_package_clause as effective and move up the chain
9086 Set_Is_Effective_Use_Clause (Curr);
9088 Curr := Prev_Use_Clause (Curr);
9089 end loop;
9090 end Mark_Use_Package;
9092 -------------------
9093 -- Mark_Use_Type --
9094 -------------------
9096 procedure Mark_Use_Type (E : Entity_Id) is
9097 Curr : Node_Id;
9098 Base : Entity_Id;
9100 begin
9101 -- Ignore void types and unresolved string literals and primitives
9103 if Nkind (E) = N_String_Literal
9104 or else Nkind (Etype (E)) not in N_Entity
9105 or else not Is_Type (Etype (E))
9106 then
9107 return;
9108 end if;
9110 -- Primitives with class-wide operands might additionally render
9111 -- their base type's use_clauses effective - so do a recursive check
9112 -- here.
9114 Base := Base_Type (Etype (E));
9116 if Ekind (Base) = E_Class_Wide_Type then
9117 Mark_Use_Type (Base);
9118 end if;
9120 -- The package containing the type or operator function being used
9121 -- may be in use as well, so mark any use_package_clauses for it as
9122 -- effective. There are also additional sanity checks performed here
9123 -- for ignoring previous errors.
9125 Mark_Use_Package (Scope (Base));
9127 if Nkind (E) in N_Op
9128 and then Present (Entity (E))
9129 and then Present (Scope (Entity (E)))
9130 then
9131 Mark_Use_Package (Scope (Entity (E)));
9132 end if;
9134 Curr := Current_Use_Clause (Base);
9135 while Present (Curr)
9136 and then not Is_Effective_Use_Clause (Curr)
9137 loop
9138 -- Current use_type_clause may render other use_package_clauses
9139 -- effective.
9141 if Nkind (Subtype_Mark (Curr)) /= N_Identifier
9142 and then Present (Prefix (Subtype_Mark (Curr)))
9143 then
9144 Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr))));
9145 end if;
9147 -- Mark the use_type_clause as effective and move up the chain
9149 Set_Is_Effective_Use_Clause (Curr);
9151 Curr := Prev_Use_Clause (Curr);
9152 end loop;
9153 end Mark_Use_Type;
9155 -- Start of processing for Mark_Use_Clauses
9157 begin
9158 -- Use clauses in and of themselves do not count as a "use" of a
9159 -- package.
9161 if Nkind (Parent (Id)) in N_Use_Package_Clause | N_Use_Type_Clause then
9162 return;
9163 end if;
9165 -- Handle entities
9167 if Nkind (Id) in N_Entity then
9169 -- Mark the entity's package
9171 if Is_Potentially_Use_Visible (Id) then
9172 Mark_Use_Package (Scope (Id));
9173 end if;
9175 -- Mark enumeration literals
9177 if Ekind (Id) = E_Enumeration_Literal then
9178 Mark_Use_Type (Id);
9180 -- Mark primitives
9182 elsif (Is_Overloadable (Id)
9183 or else Is_Generic_Subprogram (Id))
9184 and then (Is_Potentially_Use_Visible (Id)
9185 or else Is_Intrinsic_Subprogram (Id)
9186 or else (Ekind (Id) in E_Function | E_Procedure
9187 and then Is_Generic_Actual_Subprogram (Id)))
9188 then
9189 Mark_Parameters (Id);
9190 end if;
9192 -- Handle nodes
9194 else
9195 -- Mark operators
9197 if Nkind (Id) in N_Op then
9199 -- At this point the left operand may not be resolved if we are
9200 -- encountering multiple operators next to eachother in an
9201 -- expression.
9203 if Nkind (Id) in N_Binary_Op
9204 and then not (Nkind (Left_Opnd (Id)) in N_Op)
9205 then
9206 Mark_Use_Type (Left_Opnd (Id));
9207 end if;
9209 Mark_Use_Type (Right_Opnd (Id));
9210 Mark_Use_Type (Id);
9212 -- Mark entity identifiers
9214 elsif Nkind (Id) in N_Has_Entity
9215 and then (Is_Potentially_Use_Visible (Entity (Id))
9216 or else (Is_Generic_Instance (Entity (Id))
9217 and then Is_Immediately_Visible (Entity (Id))))
9218 then
9219 -- Ignore fully qualified names as they do not count as a "use" of
9220 -- a package.
9222 if Nkind (Id) in N_Identifier | N_Operator_Symbol
9223 or else (Present (Prefix (Id))
9224 and then Scope (Entity (Id)) /= Entity (Prefix (Id)))
9225 then
9226 Mark_Use_Clauses (Entity (Id));
9227 end if;
9228 end if;
9229 end if;
9230 end Mark_Use_Clauses;
9232 --------------------------------
9233 -- Most_Descendant_Use_Clause --
9234 --------------------------------
9236 function Most_Descendant_Use_Clause
9237 (Clause1 : Entity_Id;
9238 Clause2 : Entity_Id) return Entity_Id
9240 function Determine_Package_Scope (Clause : Node_Id) return Entity_Id;
9241 -- Given a use clause, determine which package it belongs to
9243 -----------------------------
9244 -- Determine_Package_Scope --
9245 -----------------------------
9247 function Determine_Package_Scope (Clause : Node_Id) return Entity_Id is
9248 begin
9249 -- Check if the clause appears in the context area
9251 -- Note we cannot employ Enclosing_Packge for use clauses within
9252 -- context clauses since they are not actually "enclosed."
9254 if Nkind (Parent (Clause)) = N_Compilation_Unit then
9255 return Entity_Of_Unit (Unit (Parent (Clause)));
9256 end if;
9258 -- Otherwise, obtain the enclosing package normally
9260 return Enclosing_Package (Clause);
9261 end Determine_Package_Scope;
9263 Scope1 : Entity_Id;
9264 Scope2 : Entity_Id;
9266 -- Start of processing for Most_Descendant_Use_Clause
9268 begin
9269 if Clause1 = Clause2 then
9270 return Clause1;
9271 end if;
9273 -- We determine which one is the most descendant by the scope distance
9274 -- to the ultimate parent unit.
9276 Scope1 := Determine_Package_Scope (Clause1);
9277 Scope2 := Determine_Package_Scope (Clause2);
9278 while Scope1 /= Standard_Standard
9279 and then Scope2 /= Standard_Standard
9280 loop
9281 Scope1 := Scope (Scope1);
9282 Scope2 := Scope (Scope2);
9284 if No (Scope1) then
9285 return Clause1;
9286 elsif No (Scope2) then
9287 return Clause2;
9288 end if;
9289 end loop;
9291 if Scope1 = Standard_Standard then
9292 return Clause1;
9293 end if;
9295 return Clause2;
9296 end Most_Descendant_Use_Clause;
9298 ---------------
9299 -- Pop_Scope --
9300 ---------------
9302 procedure Pop_Scope is
9303 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
9304 S : constant Entity_Id := SST.Entity;
9306 begin
9307 if Debug_Flag_E then
9308 Write_Info;
9309 end if;
9311 -- Set Default_Storage_Pool field of the library unit if necessary
9313 if Is_Package_Or_Generic_Package (S)
9314 and then
9315 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
9316 then
9317 declare
9318 Aux : constant Node_Id :=
9319 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
9320 begin
9321 if No (Default_Storage_Pool (Aux)) then
9322 Set_Default_Storage_Pool (Aux, Default_Pool);
9323 end if;
9324 end;
9325 end if;
9327 Scope_Suppress := SST.Save_Scope_Suppress;
9328 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
9329 Check_Policy_List := SST.Save_Check_Policy_List;
9330 Default_Pool := SST.Save_Default_Storage_Pool;
9331 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
9332 SPARK_Mode := SST.Save_SPARK_Mode;
9333 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
9334 Default_SSO := SST.Save_Default_SSO;
9335 Uneval_Old := SST.Save_Uneval_Old;
9337 if Debug_Flag_W then
9338 Write_Str ("<-- exiting scope: ");
9339 Write_Name (Chars (Current_Scope));
9340 Write_Str (", Depth=");
9341 Write_Int (Int (Scope_Stack.Last));
9342 Write_Eol;
9343 end if;
9345 End_Use_Clauses (SST.First_Use_Clause);
9347 -- If the actions to be wrapped are still there they will get lost
9348 -- causing incomplete code to be generated. It is better to abort in
9349 -- this case (and we do the abort even with assertions off since the
9350 -- penalty is incorrect code generation).
9352 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
9353 raise Program_Error;
9354 end if;
9356 -- Free last subprogram name if allocated, and pop scope
9358 Free (SST.Last_Subprogram_Name);
9359 Scope_Stack.Decrement_Last;
9360 end Pop_Scope;
9362 ----------------
9363 -- Push_Scope --
9364 ----------------
9366 procedure Push_Scope (S : Entity_Id) is
9367 E : constant Entity_Id := Scope (S);
9369 function Component_Alignment_Default return Component_Alignment_Kind;
9370 -- Return Component_Alignment_Kind for the newly-pushed scope.
9372 function Component_Alignment_Default return Component_Alignment_Kind is
9373 begin
9374 -- Each new scope pushed onto the scope stack inherits the component
9375 -- alignment of the previous scope. This emulates the "visibility"
9376 -- semantics of pragma Component_Alignment.
9378 if Scope_Stack.Last > Scope_Stack.First then
9379 return Scope_Stack.Table
9380 (Scope_Stack.Last - 1).Component_Alignment_Default;
9382 -- Otherwise, this is the first scope being pushed on the scope
9383 -- stack. Inherit the component alignment from the configuration
9384 -- form of pragma Component_Alignment (if any).
9386 else
9387 return Configuration_Component_Alignment;
9388 end if;
9389 end Component_Alignment_Default;
9391 begin
9392 if Ekind (S) = E_Void then
9393 null;
9395 -- Set scope depth if not a nonconcurrent type, and we have not yet set
9396 -- the scope depth. This means that we have the first occurrence of the
9397 -- scope, and this is where the depth is set.
9399 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
9400 and then not Scope_Depth_Set (S)
9401 then
9402 if S = Standard_Standard then
9403 Set_Scope_Depth_Value (S, Uint_0);
9405 elsif Is_Child_Unit (S) then
9406 Set_Scope_Depth_Value (S, Uint_1);
9408 elsif not Is_Record_Type (Current_Scope) then
9409 if Scope_Depth_Set (Current_Scope) then
9410 if Ekind (S) = E_Loop then
9411 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
9412 else
9413 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
9414 end if;
9415 end if;
9416 end if;
9417 end if;
9419 Scope_Stack.Increment_Last;
9421 Scope_Stack.Table (Scope_Stack.Last) :=
9422 (Entity => S,
9423 Save_Scope_Suppress => Scope_Suppress,
9424 Save_Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
9425 Save_Check_Policy_List => Check_Policy_List,
9426 Save_Default_Storage_Pool => Default_Pool,
9427 Save_No_Tagged_Streams => No_Tagged_Streams,
9428 Save_SPARK_Mode => SPARK_Mode,
9429 Save_SPARK_Mode_Pragma => SPARK_Mode_Pragma,
9430 Save_Default_SSO => Default_SSO,
9431 Save_Uneval_Old => Uneval_Old,
9432 Component_Alignment_Default => Component_Alignment_Default,
9433 Last_Subprogram_Name => null,
9434 Is_Transient => False,
9435 Node_To_Be_Wrapped => Empty,
9436 Pending_Freeze_Actions => No_List,
9437 Actions_To_Be_Wrapped => (others => No_List),
9438 First_Use_Clause => Empty,
9439 Is_Active_Stack_Base => False,
9440 Previous_Visibility => False,
9441 Locked_Shared_Objects => No_Elist);
9443 if Debug_Flag_W then
9444 Write_Str ("--> new scope: ");
9445 Write_Name (Chars (Current_Scope));
9446 Write_Str (", Id=");
9447 Write_Int (Int (Current_Scope));
9448 Write_Str (", Depth=");
9449 Write_Int (Int (Scope_Stack.Last));
9450 Write_Eol;
9451 end if;
9453 -- Deal with copying flags from the previous scope to this one. This is
9454 -- not necessary if either scope is standard, or if the new scope is a
9455 -- child unit.
9457 if S /= Standard_Standard
9458 and then Scope (S) /= Standard_Standard
9459 and then not Is_Child_Unit (S)
9460 then
9461 if Nkind (E) not in N_Entity then
9462 return;
9463 end if;
9465 -- Copy categorization flags from Scope (S) to S, this is not done
9466 -- when Scope (S) is Standard_Standard since propagation is from
9467 -- library unit entity inwards. Copy other relevant attributes as
9468 -- well (Discard_Names in particular).
9470 -- We only propagate inwards for library level entities,
9471 -- inner level subprograms do not inherit the categorization.
9473 if Is_Library_Level_Entity (S) then
9474 Set_Is_Preelaborated (S, Is_Preelaborated (E));
9475 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
9476 Set_Discard_Names (S, Discard_Names (E));
9477 Set_Suppress_Value_Tracking_On_Call
9478 (S, Suppress_Value_Tracking_On_Call (E));
9479 Set_Categorization_From_Scope (E => S, Scop => E);
9480 end if;
9481 end if;
9483 if Is_Child_Unit (S)
9484 and then Present (E)
9485 and then Is_Package_Or_Generic_Package (E)
9486 and then
9487 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9488 then
9489 declare
9490 Aux : constant Node_Id :=
9491 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
9492 begin
9493 if Present (Default_Storage_Pool (Aux)) then
9494 Default_Pool := Default_Storage_Pool (Aux);
9495 end if;
9496 end;
9497 end if;
9498 end Push_Scope;
9500 ---------------------
9501 -- Premature_Usage --
9502 ---------------------
9504 procedure Premature_Usage (N : Node_Id) is
9505 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
9506 E : Entity_Id := Entity (N);
9508 begin
9509 -- Within an instance, the analysis of the actual for a formal object
9510 -- does not see the name of the object itself. This is significant only
9511 -- if the object is an aggregate, where its analysis does not do any
9512 -- name resolution on component associations. (see 4717-008). In such a
9513 -- case, look for the visible homonym on the chain.
9515 if In_Instance and then Present (Homonym (E)) then
9516 E := Homonym (E);
9517 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
9518 E := Homonym (E);
9519 end loop;
9521 if Present (E) then
9522 Set_Entity (N, E);
9523 Set_Etype (N, Etype (E));
9524 return;
9525 end if;
9526 end if;
9528 case Kind is
9529 when N_Component_Declaration =>
9530 Error_Msg_N
9531 ("component&! cannot be used before end of record declaration",
9534 when N_Parameter_Specification =>
9535 Error_Msg_N
9536 ("formal parameter&! cannot be used before end of specification",
9539 when N_Discriminant_Specification =>
9540 Error_Msg_N
9541 ("discriminant&! cannot be used before end of discriminant part",
9544 when N_Procedure_Specification | N_Function_Specification =>
9545 Error_Msg_N
9546 ("subprogram&! cannot be used before end of its declaration",
9549 when N_Full_Type_Declaration | N_Subtype_Declaration =>
9550 Error_Msg_N
9551 ("type& cannot be used before end of its declaration!", N);
9553 when others =>
9554 Error_Msg_N
9555 ("object& cannot be used before end of its declaration!", N);
9557 -- If the premature reference appears as the expression in its own
9558 -- declaration, rewrite it to prevent compiler loops in subsequent
9559 -- uses of this mangled declaration in address clauses.
9561 if Nkind (Parent (N)) = N_Object_Declaration then
9562 Set_Entity (N, Any_Id);
9563 end if;
9564 end case;
9565 end Premature_Usage;
9567 ------------------------
9568 -- Present_System_Aux --
9569 ------------------------
9571 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
9572 Loc : Source_Ptr;
9573 Aux_Name : Unit_Name_Type;
9574 Unum : Unit_Number_Type;
9575 Withn : Node_Id;
9576 With_Sys : Node_Id;
9577 The_Unit : Node_Id;
9579 function Find_System (C_Unit : Node_Id) return Entity_Id;
9580 -- Scan context clause of compilation unit to find with_clause
9581 -- for System.
9583 -----------------
9584 -- Find_System --
9585 -----------------
9587 function Find_System (C_Unit : Node_Id) return Entity_Id is
9588 With_Clause : Node_Id;
9590 begin
9591 With_Clause := First (Context_Items (C_Unit));
9592 while Present (With_Clause) loop
9593 if (Nkind (With_Clause) = N_With_Clause
9594 and then Chars (Name (With_Clause)) = Name_System)
9595 and then Comes_From_Source (With_Clause)
9596 then
9597 return With_Clause;
9598 end if;
9600 Next (With_Clause);
9601 end loop;
9603 return Empty;
9604 end Find_System;
9606 -- Start of processing for Present_System_Aux
9608 begin
9609 -- The child unit may have been loaded and analyzed already
9611 if Present (System_Aux_Id) then
9612 return True;
9614 -- If no previous pragma for System.Aux, nothing to load
9616 elsif No (System_Extend_Unit) then
9617 return False;
9619 -- Use the unit name given in the pragma to retrieve the unit.
9620 -- Verify that System itself appears in the context clause of the
9621 -- current compilation. If System is not present, an error will
9622 -- have been reported already.
9624 else
9625 With_Sys := Find_System (Cunit (Current_Sem_Unit));
9627 The_Unit := Unit (Cunit (Current_Sem_Unit));
9629 if No (With_Sys)
9630 and then
9631 (Nkind (The_Unit) = N_Package_Body
9632 or else (Nkind (The_Unit) = N_Subprogram_Body
9633 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
9634 then
9635 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
9636 end if;
9638 if No (With_Sys) and then Present (N) then
9640 -- If we are compiling a subunit, we need to examine its
9641 -- context as well (Current_Sem_Unit is the parent unit);
9643 The_Unit := Parent (N);
9644 while Nkind (The_Unit) /= N_Compilation_Unit loop
9645 The_Unit := Parent (The_Unit);
9646 end loop;
9648 if Nkind (Unit (The_Unit)) = N_Subunit then
9649 With_Sys := Find_System (The_Unit);
9650 end if;
9651 end if;
9653 if No (With_Sys) then
9654 return False;
9655 end if;
9657 Loc := Sloc (With_Sys);
9658 Get_Name_String (Chars (Expression (System_Extend_Unit)));
9659 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
9660 Name_Buffer (1 .. 7) := "system.";
9661 Name_Buffer (Name_Len + 8) := '%';
9662 Name_Buffer (Name_Len + 9) := 's';
9663 Name_Len := Name_Len + 9;
9664 Aux_Name := Name_Find;
9666 Unum :=
9667 Load_Unit
9668 (Load_Name => Aux_Name,
9669 Required => False,
9670 Subunit => False,
9671 Error_Node => With_Sys);
9673 if Unum /= No_Unit then
9674 Semantics (Cunit (Unum));
9675 System_Aux_Id :=
9676 Defining_Entity (Specification (Unit (Cunit (Unum))));
9678 Withn :=
9679 Make_With_Clause (Loc,
9680 Name =>
9681 Make_Expanded_Name (Loc,
9682 Chars => Chars (System_Aux_Id),
9683 Prefix =>
9684 New_Occurrence_Of (Scope (System_Aux_Id), Loc),
9685 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
9687 Set_Entity (Name (Withn), System_Aux_Id);
9689 Set_Corresponding_Spec (Withn, System_Aux_Id);
9690 Set_First_Name (Withn);
9691 Set_Implicit_With (Withn);
9692 Set_Library_Unit (Withn, Cunit (Unum));
9694 Insert_After (With_Sys, Withn);
9695 Mark_Rewrite_Insertion (Withn);
9696 Set_Context_Installed (Withn);
9698 return True;
9700 -- Here if unit load failed
9702 else
9703 Error_Msg_Name_1 := Name_System;
9704 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
9705 Error_Msg_N
9706 ("extension package `%.%` does not exist",
9707 Opt.System_Extend_Unit);
9708 return False;
9709 end if;
9710 end if;
9711 end Present_System_Aux;
9713 -------------------------
9714 -- Restore_Scope_Stack --
9715 -------------------------
9717 procedure Restore_Scope_Stack
9718 (List : Elist_Id;
9719 Handle_Use : Boolean := True)
9721 SS_Last : constant Int := Scope_Stack.Last;
9722 Elmt : Elmt_Id;
9724 begin
9725 -- Restore visibility of previous scope stack, if any, using the list
9726 -- we saved (we use Remove, since this list will not be used again).
9728 loop
9729 Elmt := First_Elmt (List);
9730 exit when Elmt = No_Elmt;
9731 Set_Is_Immediately_Visible (Node (Elmt));
9732 Remove_Elmt (List, Elmt);
9733 end loop;
9735 -- Restore use clauses
9737 if SS_Last >= Scope_Stack.First
9738 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9739 and then Handle_Use
9740 then
9741 Install_Use_Clauses
9742 (Scope_Stack.Table (SS_Last).First_Use_Clause,
9743 Force_Installation => True);
9744 end if;
9745 end Restore_Scope_Stack;
9747 ----------------------
9748 -- Save_Scope_Stack --
9749 ----------------------
9751 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
9752 -- consuming any memory. That is, Save_Scope_Stack took care of removing
9753 -- from immediate visibility entities and Restore_Scope_Stack took care
9754 -- of restoring their visibility analyzing the context of each entity. The
9755 -- problem of such approach is that it was fragile and caused unexpected
9756 -- visibility problems, and indeed one test was found where there was a
9757 -- real problem.
9759 -- Furthermore, the following experiment was carried out:
9761 -- - Save_Scope_Stack was modified to store in an Elist1 all those
9762 -- entities whose attribute Is_Immediately_Visible is modified
9763 -- from True to False.
9765 -- - Restore_Scope_Stack was modified to store in another Elist2
9766 -- all the entities whose attribute Is_Immediately_Visible is
9767 -- modified from False to True.
9769 -- - Extra code was added to verify that all the elements of Elist1
9770 -- are found in Elist2
9772 -- This test shows that there may be more occurrences of this problem which
9773 -- have not yet been detected. As a result, we replaced that approach by
9774 -- the current one in which Save_Scope_Stack returns the list of entities
9775 -- whose visibility is changed, and that list is passed to Restore_Scope_
9776 -- Stack to undo that change. This approach is simpler and safer, although
9777 -- it consumes more memory.
9779 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
9780 Result : constant Elist_Id := New_Elmt_List;
9781 E : Entity_Id;
9782 S : Entity_Id;
9783 SS_Last : constant Int := Scope_Stack.Last;
9785 procedure Remove_From_Visibility (E : Entity_Id);
9786 -- If E is immediately visible then append it to the result and remove
9787 -- it temporarily from visibility.
9789 ----------------------------
9790 -- Remove_From_Visibility --
9791 ----------------------------
9793 procedure Remove_From_Visibility (E : Entity_Id) is
9794 begin
9795 if Is_Immediately_Visible (E) then
9796 Append_Elmt (E, Result);
9797 Set_Is_Immediately_Visible (E, False);
9798 end if;
9799 end Remove_From_Visibility;
9801 -- Start of processing for Save_Scope_Stack
9803 begin
9804 if SS_Last >= Scope_Stack.First
9805 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9806 then
9807 if Handle_Use then
9808 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
9809 end if;
9811 -- If the call is from within a compilation unit, as when called from
9812 -- Rtsfind, make current entries in scope stack invisible while we
9813 -- analyze the new unit.
9815 for J in reverse 0 .. SS_Last loop
9816 exit when Scope_Stack.Table (J).Entity = Standard_Standard
9817 or else No (Scope_Stack.Table (J).Entity);
9819 S := Scope_Stack.Table (J).Entity;
9821 Remove_From_Visibility (S);
9823 E := First_Entity (S);
9824 while Present (E) loop
9825 Remove_From_Visibility (E);
9826 Next_Entity (E);
9827 end loop;
9828 end loop;
9830 end if;
9832 return Result;
9833 end Save_Scope_Stack;
9835 -------------
9836 -- Set_Use --
9837 -------------
9839 procedure Set_Use (L : List_Id) is
9840 Decl : Node_Id;
9842 begin
9843 Decl := First (L);
9844 while Present (Decl) loop
9845 if Nkind (Decl) = N_Use_Package_Clause then
9846 Chain_Use_Clause (Decl);
9847 Use_One_Package (Decl, Name (Decl));
9849 elsif Nkind (Decl) = N_Use_Type_Clause then
9850 Chain_Use_Clause (Decl);
9851 Use_One_Type (Subtype_Mark (Decl));
9853 end if;
9855 Next (Decl);
9856 end loop;
9857 end Set_Use;
9859 -----------------------------
9860 -- Update_Use_Clause_Chain --
9861 -----------------------------
9863 procedure Update_Use_Clause_Chain is
9865 procedure Update_Chain_In_Scope (Level : Int);
9866 -- Iterate through one level in the scope stack verifying each use-type
9867 -- clause within said level is used then reset the Current_Use_Clause
9868 -- to a redundant use clause outside of the current ending scope if such
9869 -- a clause exists.
9871 ---------------------------
9872 -- Update_Chain_In_Scope --
9873 ---------------------------
9875 procedure Update_Chain_In_Scope (Level : Int) is
9876 Curr : Node_Id;
9877 N : Node_Id;
9879 begin
9880 -- Loop through all use clauses within the scope dictated by Level
9882 Curr := Scope_Stack.Table (Level).First_Use_Clause;
9883 while Present (Curr) loop
9885 -- Retrieve the subtype mark or name within the current current
9886 -- use clause.
9888 if Nkind (Curr) = N_Use_Type_Clause then
9889 N := Subtype_Mark (Curr);
9890 else
9891 N := Name (Curr);
9892 end if;
9894 -- If warnings for unreferenced entities are enabled and the
9895 -- current use clause has not been marked effective.
9897 if Check_Unreferenced
9898 and then Comes_From_Source (Curr)
9899 and then not Is_Effective_Use_Clause (Curr)
9900 and then not In_Instance
9901 and then not In_Inlined_Body
9902 then
9903 -- We are dealing with a potentially unused use_package_clause
9905 if Nkind (Curr) = N_Use_Package_Clause then
9907 -- Renamings and formal subprograms may cause the associated
9908 -- node to be marked as effective instead of the original.
9910 if not (Present (Associated_Node (N))
9911 and then Present
9912 (Current_Use_Clause
9913 (Associated_Node (N)))
9914 and then Is_Effective_Use_Clause
9915 (Current_Use_Clause
9916 (Associated_Node (N))))
9917 then
9918 Error_Msg_Node_1 := Entity (N);
9919 Error_Msg_NE
9920 ("use clause for package & has no effect?u?",
9921 Curr, Entity (N));
9922 end if;
9924 -- We are dealing with an unused use_type_clause
9926 else
9927 Error_Msg_Node_1 := Etype (N);
9928 Error_Msg_NE
9929 ("use clause for } has no effect?u?", Curr, Etype (N));
9930 end if;
9931 end if;
9933 -- Verify that we haven't already processed a redundant
9934 -- use_type_clause within the same scope before we move the
9935 -- current use clause up to a previous one for type T.
9937 if Present (Prev_Use_Clause (Curr)) then
9938 Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr));
9939 end if;
9941 Next_Use_Clause (Curr);
9942 end loop;
9943 end Update_Chain_In_Scope;
9945 -- Start of processing for Update_Use_Clause_Chain
9947 begin
9948 Update_Chain_In_Scope (Scope_Stack.Last);
9950 -- Deal with use clauses within the context area if the current
9951 -- scope is a compilation unit.
9953 if Is_Compilation_Unit (Current_Scope)
9954 and then Sloc (Scope_Stack.Table
9955 (Scope_Stack.Last - 1).Entity) = Standard_Location
9956 then
9957 Update_Chain_In_Scope (Scope_Stack.Last - 1);
9958 end if;
9959 end Update_Use_Clause_Chain;
9961 ---------------------
9962 -- Use_One_Package --
9963 ---------------------
9965 procedure Use_One_Package
9966 (N : Node_Id;
9967 Pack_Name : Entity_Id := Empty;
9968 Force : Boolean := False)
9970 procedure Note_Redundant_Use (Clause : Node_Id);
9971 -- Mark the name in a use clause as redundant if the corresponding
9972 -- entity is already use-visible. Emit a warning if the use clause comes
9973 -- from source and the proper warnings are enabled.
9975 ------------------------
9976 -- Note_Redundant_Use --
9977 ------------------------
9979 procedure Note_Redundant_Use (Clause : Node_Id) is
9980 Decl : constant Node_Id := Parent (Clause);
9981 Pack_Name : constant Entity_Id := Entity (Clause);
9983 Cur_Use : Node_Id := Current_Use_Clause (Pack_Name);
9984 Prev_Use : Node_Id := Empty;
9985 Redundant : Node_Id := Empty;
9986 -- The Use_Clause which is actually redundant. In the simplest case
9987 -- it is Pack itself, but when we compile a body we install its
9988 -- context before that of its spec, in which case it is the
9989 -- use_clause in the spec that will appear to be redundant, and we
9990 -- want the warning to be placed on the body. Similar complications
9991 -- appear when the redundancy is between a child unit and one of its
9992 -- ancestors.
9994 begin
9995 -- Could be renamed...
9997 if No (Cur_Use) then
9998 Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name));
9999 end if;
10001 Set_Redundant_Use (Clause, True);
10003 -- Do not check for redundant use if clause is generated, or in an
10004 -- instance, or in a predefined unit to avoid misleading warnings
10005 -- that may occur as part of a rtsfind load.
10007 if not Comes_From_Source (Clause)
10008 or else In_Instance
10009 or else not Warn_On_Redundant_Constructs
10010 or else Is_Predefined_Unit (Current_Sem_Unit)
10011 then
10012 return;
10013 end if;
10015 if not Is_Compilation_Unit (Current_Scope) then
10017 -- If the use_clause is in an inner scope, it is made redundant by
10018 -- some clause in the current context, with one exception: If we
10019 -- are compiling a nested package body, and the use_clause comes
10020 -- from then corresponding spec, the clause is not necessarily
10021 -- fully redundant, so we should not warn. If a warning was
10022 -- warranted, it would have been given when the spec was
10023 -- processed.
10025 if Nkind (Parent (Decl)) = N_Package_Specification then
10026 declare
10027 Package_Spec_Entity : constant Entity_Id :=
10028 Defining_Unit_Name (Parent (Decl));
10029 begin
10030 if In_Package_Body (Package_Spec_Entity) then
10031 return;
10032 end if;
10033 end;
10034 end if;
10036 Redundant := Clause;
10037 Prev_Use := Cur_Use;
10039 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
10040 declare
10041 Cur_Unit : constant Unit_Number_Type :=
10042 Get_Source_Unit (Cur_Use);
10043 New_Unit : constant Unit_Number_Type :=
10044 Get_Source_Unit (Clause);
10046 Scop : Entity_Id;
10048 begin
10049 if Cur_Unit = New_Unit then
10051 -- Redundant clause in same body
10053 Redundant := Clause;
10054 Prev_Use := Cur_Use;
10056 elsif Cur_Unit = Current_Sem_Unit then
10058 -- If the new clause is not in the current unit it has been
10059 -- analyzed first, and it makes the other one redundant.
10060 -- However, if the new clause appears in a subunit, Cur_Unit
10061 -- is still the parent, and in that case the redundant one
10062 -- is the one appearing in the subunit.
10064 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
10065 Redundant := Clause;
10066 Prev_Use := Cur_Use;
10068 -- Most common case: redundant clause in body, original
10069 -- clause in spec. Current scope is spec entity.
10071 elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then
10072 Redundant := Cur_Use;
10073 Prev_Use := Clause;
10075 else
10076 -- The new clause may appear in an unrelated unit, when
10077 -- the parents of a generic are being installed prior to
10078 -- instantiation. In this case there must be no warning.
10079 -- We detect this case by checking whether the current
10080 -- top of the stack is related to the current
10081 -- compilation.
10083 Scop := Current_Scope;
10084 while Present (Scop)
10085 and then Scop /= Standard_Standard
10086 loop
10087 if Is_Compilation_Unit (Scop)
10088 and then not Is_Child_Unit (Scop)
10089 then
10090 return;
10092 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
10093 exit;
10094 end if;
10096 Scop := Scope (Scop);
10097 end loop;
10099 Redundant := Cur_Use;
10100 Prev_Use := Clause;
10101 end if;
10103 elsif New_Unit = Current_Sem_Unit then
10104 Redundant := Clause;
10105 Prev_Use := Cur_Use;
10107 else
10108 -- Neither is the current unit, so they appear in parent or
10109 -- sibling units. Warning will be emitted elsewhere.
10111 return;
10112 end if;
10113 end;
10115 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
10116 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
10117 then
10118 -- Use_clause is in child unit of current unit, and the child unit
10119 -- appears in the context of the body of the parent, so it has
10120 -- been installed first, even though it is the redundant one.
10121 -- Depending on their placement in the context, the visible or the
10122 -- private parts of the two units, either might appear as
10123 -- redundant, but the message has to be on the current unit.
10125 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
10126 Redundant := Cur_Use;
10127 Prev_Use := Clause;
10128 else
10129 Redundant := Clause;
10130 Prev_Use := Cur_Use;
10131 end if;
10133 -- If the new use clause appears in the private part of a parent
10134 -- unit it may appear to be redundant w.r.t. a use clause in a
10135 -- child unit, but the previous use clause was needed in the
10136 -- visible part of the child, and no warning should be emitted.
10138 if Nkind (Parent (Decl)) = N_Package_Specification
10139 and then List_Containing (Decl) =
10140 Private_Declarations (Parent (Decl))
10141 then
10142 declare
10143 Par : constant Entity_Id :=
10144 Defining_Entity (Parent (Decl));
10145 Spec : constant Node_Id :=
10146 Specification (Unit (Cunit (Current_Sem_Unit)));
10147 Cur_List : constant List_Id := List_Containing (Cur_Use);
10149 begin
10150 if Is_Compilation_Unit (Par)
10151 and then Par /= Cunit_Entity (Current_Sem_Unit)
10152 then
10153 if Cur_List = Context_Items (Cunit (Current_Sem_Unit))
10154 or else Cur_List = Visible_Declarations (Spec)
10155 then
10156 return;
10157 end if;
10158 end if;
10159 end;
10160 end if;
10162 -- Finally, if the current use clause is in the context then the
10163 -- clause is redundant when it is nested within the unit.
10165 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
10166 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
10167 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
10168 then
10169 Redundant := Clause;
10170 Prev_Use := Cur_Use;
10171 end if;
10173 if Present (Redundant) and then Parent (Redundant) /= Prev_Use then
10175 -- Make sure we are looking at most-descendant use_package_clause
10176 -- by traversing the chain with Find_First_Use and then verifying
10177 -- there is no scope manipulation via Most_Descendant_Use_Clause.
10179 if Nkind (Prev_Use) = N_Use_Package_Clause
10180 and then
10181 (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit
10182 or else Most_Descendant_Use_Clause
10183 (Prev_Use, Find_First_Use (Prev_Use)) /= Prev_Use)
10184 then
10185 Prev_Use := Find_First_Use (Prev_Use);
10186 end if;
10188 Error_Msg_Sloc := Sloc (Prev_Use);
10189 Error_Msg_NE -- CODEFIX
10190 ("& is already use-visible through previous use_clause #?r?",
10191 Redundant, Pack_Name);
10192 end if;
10193 end Note_Redundant_Use;
10195 -- Local variables
10197 Current_Instance : Entity_Id := Empty;
10198 Id : Entity_Id;
10199 P : Entity_Id;
10200 Prev : Entity_Id;
10201 Private_With_OK : Boolean := False;
10202 Real_P : Entity_Id;
10204 -- Start of processing for Use_One_Package
10206 begin
10207 -- Use_One_Package may have been called recursively to handle an
10208 -- implicit use for a auxiliary system package, so set P accordingly
10209 -- and skip redundancy checks.
10211 if No (Pack_Name) and then Present_System_Aux (N) then
10212 P := System_Aux_Id;
10214 -- Check for redundant use_package_clauses
10216 else
10217 -- Ignore cases where we are dealing with a non user defined package
10218 -- like Standard_Standard or something other than a valid package.
10220 if not Is_Entity_Name (Pack_Name)
10221 or else No (Entity (Pack_Name))
10222 or else Ekind (Entity (Pack_Name)) /= E_Package
10223 then
10224 return;
10225 end if;
10227 -- When a renaming exists we must check it for redundancy. The
10228 -- original package would have already been seen at this point.
10230 if Present (Renamed_Entity (Entity (Pack_Name))) then
10231 P := Renamed_Entity (Entity (Pack_Name));
10232 else
10233 P := Entity (Pack_Name);
10234 end if;
10236 -- Check for redundant clauses then set the current use clause for
10237 -- P if were are not "forcing" an installation from a scope
10238 -- reinstallation that is done throughout analysis for various
10239 -- reasons.
10241 if In_Use (P) then
10242 Note_Redundant_Use (Pack_Name);
10244 if not Force then
10245 Set_Current_Use_Clause (P, N);
10246 end if;
10248 return;
10250 -- Warn about detected redundant clauses
10252 elsif not Force
10253 and then In_Open_Scopes (P)
10254 and then not Is_Hidden_Open_Scope (P)
10255 then
10256 if Warn_On_Redundant_Constructs and then P = Current_Scope then
10257 Error_Msg_NE -- CODEFIX
10258 ("& is already use-visible within itself?r?",
10259 Pack_Name, P);
10260 end if;
10262 return;
10263 end if;
10265 -- Set P back to the non-renamed package so that visibility of the
10266 -- entities within the package can be properly set below.
10268 P := Entity (Pack_Name);
10269 end if;
10271 Set_In_Use (P);
10272 Set_Current_Use_Clause (P, N);
10274 -- Ada 2005 (AI-50217): Check restriction
10276 if From_Limited_With (P) then
10277 Error_Msg_N ("limited withed package cannot appear in use clause", N);
10278 end if;
10280 -- Find enclosing instance, if any
10282 if In_Instance then
10283 Current_Instance := Current_Scope;
10284 while not Is_Generic_Instance (Current_Instance) loop
10285 Current_Instance := Scope (Current_Instance);
10286 end loop;
10288 if No (Hidden_By_Use_Clause (N)) then
10289 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
10290 end if;
10291 end if;
10293 -- If unit is a package renaming, indicate that the renamed package is
10294 -- also in use (the flags on both entities must remain consistent, and a
10295 -- subsequent use of either of them should be recognized as redundant).
10297 if Present (Renamed_Entity (P)) then
10298 Set_In_Use (Renamed_Entity (P));
10299 Set_Current_Use_Clause (Renamed_Entity (P), N);
10300 Real_P := Renamed_Entity (P);
10301 else
10302 Real_P := P;
10303 end if;
10305 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
10306 -- found in the private part of a package specification
10308 if In_Private_Part (Current_Scope)
10309 and then Has_Private_With (P)
10310 and then Is_Child_Unit (Current_Scope)
10311 and then Is_Child_Unit (P)
10312 and then Is_Ancestor_Package (Scope (Current_Scope), P)
10313 then
10314 Private_With_OK := True;
10315 end if;
10317 -- Loop through entities in one package making them potentially
10318 -- use-visible.
10320 Id := First_Entity (P);
10321 while Present (Id)
10322 and then (Id /= First_Private_Entity (P)
10323 or else Private_With_OK) -- Ada 2005 (AI-262)
10324 loop
10325 Prev := Current_Entity (Id);
10326 while Present (Prev) loop
10327 if Is_Immediately_Visible (Prev)
10328 and then (not Is_Overloadable (Prev)
10329 or else not Is_Overloadable (Id)
10330 or else Type_Conformant (Id, Prev))
10331 then
10332 if No (Current_Instance) then
10334 -- Potentially use-visible entity remains hidden
10336 if Warn_On_Hiding then
10337 Warn_On_Hiding_Entity (N, Hidden => Id, Visible => Prev,
10338 On_Use_Clause => True);
10339 end if;
10341 goto Next_Usable_Entity;
10343 -- A use clause within an instance hides outer global entities,
10344 -- which are not used to resolve local entities in the
10345 -- instance. Note that the predefined entities in Standard
10346 -- could not have been hidden in the generic by a use clause,
10347 -- and therefore remain visible. Other compilation units whose
10348 -- entities appear in Standard must be hidden in an instance.
10350 -- To determine whether an entity is external to the instance
10351 -- we compare the scope depth of its scope with that of the
10352 -- current instance. However, a generic actual of a subprogram
10353 -- instance is declared in the wrapper package but will not be
10354 -- hidden by a use-visible entity. similarly, an entity that is
10355 -- declared in an enclosing instance will not be hidden by an
10356 -- an entity declared in a generic actual, which can only have
10357 -- been use-visible in the generic and will not have hidden the
10358 -- entity in the generic parent.
10360 -- If Id is called Standard, the predefined package with the
10361 -- same name is in the homonym chain. It has to be ignored
10362 -- because it has no defined scope (being the only entity in
10363 -- the system with this mandated behavior).
10365 elsif not Is_Hidden (Id)
10366 and then Present (Scope (Prev))
10367 and then not Is_Wrapper_Package (Scope (Prev))
10368 and then Scope_Depth (Scope (Prev)) <
10369 Scope_Depth (Current_Instance)
10370 and then (Scope (Prev) /= Standard_Standard
10371 or else Sloc (Prev) > Standard_Location)
10372 then
10373 if In_Open_Scopes (Scope (Prev))
10374 and then Is_Generic_Instance (Scope (Prev))
10375 and then Present (Associated_Formal_Package (P))
10376 then
10377 null;
10379 else
10380 Set_Is_Potentially_Use_Visible (Id);
10381 Set_Is_Immediately_Visible (Prev, False);
10382 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10383 end if;
10384 end if;
10386 -- A user-defined operator is not use-visible if the predefined
10387 -- operator for the type is immediately visible, which is the case
10388 -- if the type of the operand is in an open scope. This does not
10389 -- apply to user-defined operators that have operands of different
10390 -- types, because the predefined mixed mode operations (multiply
10391 -- and divide) apply to universal types and do not hide anything.
10393 elsif Ekind (Prev) = E_Operator
10394 and then Operator_Matches_Spec (Prev, Id)
10395 and then In_Open_Scopes
10396 (Scope (Base_Type (Etype (First_Formal (Id)))))
10397 and then (No (Next_Formal (First_Formal (Id)))
10398 or else Etype (First_Formal (Id)) =
10399 Etype (Next_Formal (First_Formal (Id)))
10400 or else Chars (Prev) = Name_Op_Expon)
10401 then
10402 goto Next_Usable_Entity;
10404 -- In an instance, two homonyms may become use_visible through the
10405 -- actuals of distinct formal packages. In the generic, only the
10406 -- current one would have been visible, so make the other one
10407 -- not use_visible.
10409 -- In certain pathological cases it is possible that unrelated
10410 -- homonyms from distinct formal packages may exist in an
10411 -- uninstalled scope. We must test for that here.
10413 elsif Present (Current_Instance)
10414 and then Is_Potentially_Use_Visible (Prev)
10415 and then not Is_Overloadable (Prev)
10416 and then Scope (Id) /= Scope (Prev)
10417 and then Used_As_Generic_Actual (Scope (Prev))
10418 and then Used_As_Generic_Actual (Scope (Id))
10419 and then Is_List_Member (Scope (Prev))
10420 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
10421 Current_Use_Clause (Scope (Id)))
10422 then
10423 Set_Is_Potentially_Use_Visible (Prev, False);
10424 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10425 end if;
10427 Prev := Homonym (Prev);
10428 end loop;
10430 -- On exit, we know entity is not hidden, unless it is private
10432 if not Is_Hidden (Id)
10433 and then (not Is_Child_Unit (Id) or else Is_Visible_Lib_Unit (Id))
10434 then
10435 Set_Is_Potentially_Use_Visible (Id);
10437 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
10438 Set_Is_Potentially_Use_Visible (Full_View (Id));
10439 end if;
10440 end if;
10442 <<Next_Usable_Entity>>
10443 Next_Entity (Id);
10444 end loop;
10446 -- Child units are also made use-visible by a use clause, but they may
10447 -- appear after all visible declarations in the parent entity list.
10449 while Present (Id) loop
10450 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
10451 Set_Is_Potentially_Use_Visible (Id);
10452 end if;
10454 Next_Entity (Id);
10455 end loop;
10457 if Chars (Real_P) = Name_System
10458 and then Scope (Real_P) = Standard_Standard
10459 and then Present_System_Aux (N)
10460 then
10461 Use_One_Package (N);
10462 end if;
10463 end Use_One_Package;
10465 ------------------
10466 -- Use_One_Type --
10467 ------------------
10469 procedure Use_One_Type
10470 (Id : Node_Id;
10471 Installed : Boolean := False;
10472 Force : Boolean := False)
10474 function Spec_Reloaded_For_Body return Boolean;
10475 -- Determine whether the compilation unit is a package body and the use
10476 -- type clause is in the spec of the same package. Even though the spec
10477 -- was analyzed first, its context is reloaded when analysing the body.
10479 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
10480 -- AI05-150: if the use_type_clause carries the "all" qualifier,
10481 -- class-wide operations of ancestor types are use-visible if the
10482 -- ancestor type is visible.
10484 ----------------------------
10485 -- Spec_Reloaded_For_Body --
10486 ----------------------------
10488 function Spec_Reloaded_For_Body return Boolean is
10489 begin
10490 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
10491 declare
10492 Spec : constant Node_Id :=
10493 Parent (List_Containing (Parent (Id)));
10495 begin
10496 -- Check whether type is declared in a package specification,
10497 -- and current unit is the corresponding package body. The
10498 -- use clauses themselves may be within a nested package.
10500 return
10501 Nkind (Spec) = N_Package_Specification
10502 and then In_Same_Source_Unit
10503 (Corresponding_Body (Parent (Spec)),
10504 Cunit_Entity (Current_Sem_Unit));
10505 end;
10506 end if;
10508 return False;
10509 end Spec_Reloaded_For_Body;
10511 -------------------------------
10512 -- Use_Class_Wide_Operations --
10513 -------------------------------
10515 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
10516 function Is_Class_Wide_Operation_Of
10517 (Op : Entity_Id;
10518 T : Entity_Id) return Boolean;
10519 -- Determine whether a subprogram has a class-wide parameter or
10520 -- result that is T'Class.
10522 ---------------------------------
10523 -- Is_Class_Wide_Operation_Of --
10524 ---------------------------------
10526 function Is_Class_Wide_Operation_Of
10527 (Op : Entity_Id;
10528 T : Entity_Id) return Boolean
10530 Formal : Entity_Id;
10532 begin
10533 Formal := First_Formal (Op);
10534 while Present (Formal) loop
10535 if Etype (Formal) = Class_Wide_Type (T) then
10536 return True;
10537 end if;
10539 Next_Formal (Formal);
10540 end loop;
10542 if Etype (Op) = Class_Wide_Type (T) then
10543 return True;
10544 end if;
10546 return False;
10547 end Is_Class_Wide_Operation_Of;
10549 -- Local variables
10551 Ent : Entity_Id;
10552 Scop : Entity_Id;
10554 -- Start of processing for Use_Class_Wide_Operations
10556 begin
10557 Scop := Scope (Typ);
10558 if not Is_Hidden (Scop) then
10559 Ent := First_Entity (Scop);
10560 while Present (Ent) loop
10561 if Is_Overloadable (Ent)
10562 and then Is_Class_Wide_Operation_Of (Ent, Typ)
10563 and then not Is_Potentially_Use_Visible (Ent)
10564 then
10565 Set_Is_Potentially_Use_Visible (Ent);
10566 Append_Elmt (Ent, Used_Operations (Parent (Id)));
10567 end if;
10569 Next_Entity (Ent);
10570 end loop;
10571 end if;
10573 if Is_Derived_Type (Typ) then
10574 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
10575 end if;
10576 end Use_Class_Wide_Operations;
10578 -- Local variables
10580 Elmt : Elmt_Id;
10581 Is_Known_Used : Boolean;
10582 Op_List : Elist_Id;
10583 T : Entity_Id;
10585 -- Start of processing for Use_One_Type
10587 begin
10588 if Entity (Id) = Any_Type then
10589 return;
10590 end if;
10592 -- It is the type determined by the subtype mark (8.4(8)) whose
10593 -- operations become potentially use-visible.
10595 T := Base_Type (Entity (Id));
10597 -- Either the type itself is used, the package where it is declared is
10598 -- in use or the entity is declared in the current package, thus
10599 -- use-visible.
10601 Is_Known_Used :=
10602 (In_Use (T)
10603 and then ((Present (Current_Use_Clause (T))
10604 and then All_Present (Current_Use_Clause (T)))
10605 or else not All_Present (Parent (Id))))
10606 or else In_Use (Scope (T))
10607 or else Scope (T) = Current_Scope;
10609 Set_Redundant_Use (Id,
10610 Is_Known_Used or else Is_Potentially_Use_Visible (T));
10612 if Ekind (T) = E_Incomplete_Type then
10613 Error_Msg_N ("premature usage of incomplete type", Id);
10615 elsif In_Open_Scopes (Scope (T)) then
10616 null;
10618 -- A limited view cannot appear in a use_type_clause. However, an access
10619 -- type whose designated type is limited has the flag but is not itself
10620 -- a limited view unless we only have a limited view of its enclosing
10621 -- package.
10623 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
10624 Error_Msg_N
10625 ("incomplete type from limited view cannot appear in use clause",
10626 Id);
10628 -- If the use clause is redundant, Used_Operations will usually be
10629 -- empty, but we need to set it to empty here in one case: If we are
10630 -- instantiating a generic library unit, then we install the ancestors
10631 -- of that unit in the scope stack, which involves reprocessing use
10632 -- clauses in those ancestors. Such a use clause will typically have a
10633 -- nonempty Used_Operations unless it was redundant in the generic unit,
10634 -- even if it is redundant at the place of the instantiation.
10636 elsif Redundant_Use (Id) then
10637 Set_Used_Operations (Parent (Id), New_Elmt_List);
10639 -- If the subtype mark designates a subtype in a different package,
10640 -- we have to check that the parent type is visible, otherwise the
10641 -- use_type_clause is a no-op. Not clear how to do that???
10643 else
10644 Set_Current_Use_Clause (T, Parent (Id));
10645 Set_In_Use (T);
10647 -- If T is tagged, primitive operators on class-wide operands are
10648 -- also deemed available. Note that this is really necessary only
10649 -- in semantics-only mode, because the primitive operators are not
10650 -- fully constructed in this mode, but we do it in all modes for the
10651 -- sake of uniformity, as this should not matter in practice.
10653 if Is_Tagged_Type (T) then
10654 Set_In_Use (Class_Wide_Type (T));
10655 end if;
10657 -- Iterate over primitive operations of the type. If an operation is
10658 -- already use_visible, it is the result of a previous use_clause,
10659 -- and already appears on the corresponding entity chain. If the
10660 -- clause is being reinstalled, operations are already use-visible.
10662 if Installed then
10663 null;
10665 else
10666 Op_List := Collect_Primitive_Operations (T);
10667 Elmt := First_Elmt (Op_List);
10668 while Present (Elmt) loop
10669 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
10670 or else Chars (Node (Elmt)) in Any_Operator_Name)
10671 and then not Is_Hidden (Node (Elmt))
10672 and then not Is_Potentially_Use_Visible (Node (Elmt))
10673 then
10674 Set_Is_Potentially_Use_Visible (Node (Elmt));
10675 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10677 elsif Ada_Version >= Ada_2012
10678 and then All_Present (Parent (Id))
10679 and then not Is_Hidden (Node (Elmt))
10680 and then not Is_Potentially_Use_Visible (Node (Elmt))
10681 then
10682 Set_Is_Potentially_Use_Visible (Node (Elmt));
10683 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10684 end if;
10686 Next_Elmt (Elmt);
10687 end loop;
10688 end if;
10690 if Ada_Version >= Ada_2012
10691 and then All_Present (Parent (Id))
10692 and then Is_Tagged_Type (T)
10693 then
10694 Use_Class_Wide_Operations (T);
10695 end if;
10696 end if;
10698 -- If warning on redundant constructs, check for unnecessary WITH
10700 if not Force
10701 and then Warn_On_Redundant_Constructs
10702 and then Is_Known_Used
10704 -- with P; with P; use P;
10705 -- package P is package X is package body X is
10706 -- type T ... use P.T;
10708 -- The compilation unit is the body of X. GNAT first compiles the
10709 -- spec of X, then proceeds to the body. At that point P is marked
10710 -- as use visible. The analysis then reinstalls the spec along with
10711 -- its context. The use clause P.T is now recognized as redundant,
10712 -- but in the wrong context. Do not emit a warning in such cases.
10713 -- Do not emit a warning either if we are in an instance, there is
10714 -- no redundancy between an outer use_clause and one that appears
10715 -- within the generic.
10717 and then not Spec_Reloaded_For_Body
10718 and then not In_Instance
10719 and then not In_Inlined_Body
10720 then
10721 -- The type already has a use clause
10723 if In_Use (T) then
10725 -- Case where we know the current use clause for the type
10727 if Present (Current_Use_Clause (T)) then
10728 Use_Clause_Known : declare
10729 Clause1 : constant Node_Id :=
10730 Find_First_Use (Current_Use_Clause (T));
10731 Clause2 : constant Node_Id := Parent (Id);
10732 Ent1 : Entity_Id;
10733 Ent2 : Entity_Id;
10734 Err_No : Node_Id;
10735 Unit1 : Node_Id;
10736 Unit2 : Node_Id;
10738 -- Start of processing for Use_Clause_Known
10740 begin
10741 -- If the unit is a subprogram body that acts as spec, the
10742 -- context clause is shared with the constructed subprogram
10743 -- spec. Clearly there is no redundancy.
10745 if Clause1 = Clause2 then
10746 return;
10747 end if;
10749 Unit1 := Unit (Enclosing_Comp_Unit_Node (Clause1));
10750 Unit2 := Unit (Enclosing_Comp_Unit_Node (Clause2));
10752 -- If both clauses are on same unit, or one is the body of
10753 -- the other, or one of them is in a subunit, report
10754 -- redundancy on the later one.
10756 if Unit1 = Unit2
10757 or else Nkind (Unit1) = N_Subunit
10758 or else
10759 (Nkind (Unit2) in N_Package_Body | N_Subprogram_Body
10760 and then Nkind (Unit1) /= Nkind (Unit2)
10761 and then Nkind (Unit1) /= N_Subunit)
10762 then
10763 Error_Msg_Sloc := Sloc (Clause1);
10764 Error_Msg_NE -- CODEFIX
10765 ("& is already use-visible through previous "
10766 & "use_type_clause #?r?", Clause2, T);
10767 return;
10768 end if;
10770 -- If there is a redundant use_type_clause in a child unit
10771 -- determine which of the units is more deeply nested. If a
10772 -- unit is a package instance, retrieve the entity and its
10773 -- scope from the instance spec.
10775 Ent1 := Entity_Of_Unit (Unit1);
10776 Ent2 := Entity_Of_Unit (Unit2);
10778 -- When the scope of both units' entities are
10779 -- Standard_Standard then neither Unit1 or Unit2 are child
10780 -- units - so return in that case.
10782 if Scope (Ent1) = Standard_Standard
10783 and then Scope (Ent2) = Standard_Standard
10784 then
10785 return;
10787 -- Otherwise, determine if one of the units is not a child
10789 elsif Scope (Ent2) = Standard_Standard then
10790 Error_Msg_Sloc := Sloc (Clause2);
10791 Err_No := Clause1;
10793 elsif Scope (Ent1) = Standard_Standard then
10794 Error_Msg_Sloc := Sloc (Id);
10795 Err_No := Clause2;
10797 -- If both units are child units, we determine which one is
10798 -- the descendant by the scope distance to the ultimate
10799 -- parent unit.
10801 else
10802 declare
10803 S1 : Entity_Id;
10804 S2 : Entity_Id;
10806 begin
10807 S1 := Scope (Ent1);
10808 S2 := Scope (Ent2);
10809 while Present (S1)
10810 and then Present (S2)
10811 and then S1 /= Standard_Standard
10812 and then S2 /= Standard_Standard
10813 loop
10814 S1 := Scope (S1);
10815 S2 := Scope (S2);
10816 end loop;
10818 if S1 = Standard_Standard then
10819 Error_Msg_Sloc := Sloc (Id);
10820 Err_No := Clause2;
10821 else
10822 Error_Msg_Sloc := Sloc (Clause2);
10823 Err_No := Clause1;
10824 end if;
10825 end;
10826 end if;
10828 if Parent (Id) /= Err_No then
10829 if Most_Descendant_Use_Clause
10830 (Err_No, Parent (Id)) = Parent (Id)
10831 then
10832 Error_Msg_Sloc := Sloc (Err_No);
10833 Err_No := Parent (Id);
10834 end if;
10836 Error_Msg_NE -- CODEFIX
10837 ("& is already use-visible through previous "
10838 & "use_type_clause #?r?", Err_No, Id);
10839 end if;
10840 end Use_Clause_Known;
10842 -- Here Current_Use_Clause is not set for T, so we do not have the
10843 -- location information available.
10845 else
10846 Error_Msg_NE -- CODEFIX
10847 ("& is already use-visible through previous "
10848 & "use_type_clause?r?", Id, T);
10849 end if;
10851 -- The package where T is declared is already used
10853 elsif In_Use (Scope (T)) then
10854 -- Due to expansion of contracts we could be attempting to issue
10855 -- a spurious warning - so verify there is a previous use clause.
10857 if Current_Use_Clause (Scope (T)) /=
10858 Find_First_Use (Current_Use_Clause (Scope (T)))
10859 then
10860 Error_Msg_Sloc :=
10861 Sloc (Find_First_Use (Current_Use_Clause (Scope (T))));
10862 Error_Msg_NE -- CODEFIX
10863 ("& is already use-visible through package use clause #?r?",
10864 Id, T);
10865 end if;
10867 -- The current scope is the package where T is declared
10869 else
10870 Error_Msg_Node_2 := Scope (T);
10871 Error_Msg_NE -- CODEFIX
10872 ("& is already use-visible inside package &?r?", Id, T);
10873 end if;
10874 end if;
10875 end Use_One_Type;
10877 ----------------
10878 -- Write_Info --
10879 ----------------
10881 procedure Write_Info is
10882 Id : Entity_Id := First_Entity (Current_Scope);
10884 begin
10885 -- No point in dumping standard entities
10887 if Current_Scope = Standard_Standard then
10888 return;
10889 end if;
10891 Write_Str ("========================================================");
10892 Write_Eol;
10893 Write_Str (" Defined Entities in ");
10894 Write_Name (Chars (Current_Scope));
10895 Write_Eol;
10896 Write_Str ("========================================================");
10897 Write_Eol;
10899 if No (Id) then
10900 Write_Str ("-- none --");
10901 Write_Eol;
10903 else
10904 while Present (Id) loop
10905 Write_Entity_Info (Id, " ");
10906 Next_Entity (Id);
10907 end loop;
10908 end if;
10910 if Scope (Current_Scope) = Standard_Standard then
10912 -- Print information on the current unit itself
10914 Write_Entity_Info (Current_Scope, " ");
10915 end if;
10917 Write_Eol;
10918 end Write_Info;
10920 --------
10921 -- ws --
10922 --------
10924 procedure ws is
10925 S : Entity_Id;
10926 begin
10927 for J in reverse 1 .. Scope_Stack.Last loop
10928 S := Scope_Stack.Table (J).Entity;
10929 Write_Int (Int (S));
10930 Write_Str (" === ");
10931 Write_Name (Chars (S));
10932 Write_Eol;
10933 end loop;
10934 end ws;
10936 --------
10937 -- we --
10938 --------
10940 procedure we (S : Entity_Id) is
10941 E : Entity_Id;
10942 begin
10943 E := First_Entity (S);
10944 while Present (E) loop
10945 Write_Int (Int (E));
10946 Write_Str (" === ");
10947 Write_Name (Chars (E));
10948 Write_Eol;
10949 Next_Entity (E);
10950 end loop;
10951 end we;
10952 end Sem_Ch8;