1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Disp; use Exp_Disp;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Ghost; use Ghost;
36 with Impunit; use Impunit;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Namet.Sp; use Namet.Sp;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
45 with Output; use Output;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch4; use Sem_Ch4;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch12; use Sem_Ch12;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Dist; use Sem_Dist;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Util; use Sem_Util;
65 with Sem_Type; use Sem_Type;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinfo.CN; use Sinfo.CN;
69 with Snames; use Snames;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
75 package body Sem_Ch8 is
77 ------------------------------------
78 -- Visibility and Name Resolution --
79 ------------------------------------
81 -- This package handles name resolution and the collection of possible
82 -- interpretations for overloaded names, prior to overload resolution.
84 -- Name resolution is the process that establishes a mapping between source
85 -- identifiers and the entities they denote at each point in the program.
86 -- Each entity is represented by a defining occurrence. Each identifier
87 -- that denotes an entity points to the corresponding defining occurrence.
88 -- This is the entity of the applied occurrence. Each occurrence holds
89 -- an index into the names table, where source identifiers are stored.
91 -- Each entry in the names table for an identifier or designator uses the
92 -- Info pointer to hold a link to the currently visible entity that has
93 -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
94 -- in package Sem_Util). The visibility is initialized at the beginning of
95 -- semantic processing to make entities in package Standard immediately
96 -- visible. The visibility table is used in a more subtle way when
97 -- compiling subunits (see below).
99 -- Entities that have the same name (i.e. homonyms) are chained. In the
100 -- case of overloaded entities, this chain holds all the possible meanings
101 -- of a given identifier. The process of overload resolution uses type
102 -- information to select from this chain the unique meaning of a given
105 -- Entities are also chained in their scope, through the Next_Entity link.
106 -- As a consequence, the name space is organized as a sparse matrix, where
107 -- each row corresponds to a scope, and each column to a source identifier.
108 -- Open scopes, that is to say scopes currently being compiled, have their
109 -- corresponding rows of entities in order, innermost scope first.
111 -- The scopes of packages that are mentioned in context clauses appear in
112 -- no particular order, interspersed among open scopes. This is because
113 -- in the course of analyzing the context of a compilation, a package
114 -- declaration is first an open scope, and subsequently an element of the
115 -- context. If subunits or child units are present, a parent unit may
116 -- appear under various guises at various times in the compilation.
118 -- When the compilation of the innermost scope is complete, the entities
119 -- defined therein are no longer visible. If the scope is not a package
120 -- declaration, these entities are never visible subsequently, and can be
121 -- removed from visibility chains. If the scope is a package declaration,
122 -- its visible declarations may still be accessible. Therefore the entities
123 -- defined in such a scope are left on the visibility chains, and only
124 -- their visibility (immediately visibility or potential use-visibility)
127 -- The ordering of homonyms on their chain does not necessarily follow
128 -- the order of their corresponding scopes on the scope stack. For
129 -- example, if package P and the enclosing scope both contain entities
130 -- named E, then when compiling the package body the chain for E will
131 -- hold the global entity first, and the local one (corresponding to
132 -- the current inner scope) next. As a result, name resolution routines
133 -- do not assume any relative ordering of the homonym chains, either
134 -- for scope nesting or to order of appearance of context clauses.
136 -- When compiling a child unit, entities in the parent scope are always
137 -- immediately visible. When compiling the body of a child unit, private
138 -- entities in the parent must also be made immediately visible. There
139 -- are separate routines to make the visible and private declarations
140 -- visible at various times (see package Sem_Ch7).
142 -- +--------+ +-----+
143 -- | In use |-------->| EU1 |-------------------------->
144 -- +--------+ +-----+
146 -- +--------+ +-----+ +-----+
147 -- | Stand. |---------------->| ES1 |--------------->| ES2 |--->
148 -- +--------+ +-----+ +-----+
150 -- +---------+ | +-----+
151 -- | with'ed |------------------------------>| EW2 |--->
152 -- +---------+ | +-----+
154 -- +--------+ +-----+ +-----+
155 -- | Scope2 |---------------->| E12 |--------------->| E22 |--->
156 -- +--------+ +-----+ +-----+
158 -- +--------+ +-----+ +-----+
159 -- | Scope1 |---------------->| E11 |--------------->| E12 |--->
160 -- +--------+ +-----+ +-----+
164 -- | | with'ed |----------------------------------------->
168 -- (innermost first) | |
169 -- +----------------------------+
170 -- Names table => | Id1 | | | | Id2 |
171 -- +----------------------------+
173 -- Name resolution must deal with several syntactic forms: simple names,
174 -- qualified names, indexed names, and various forms of calls.
176 -- Each identifier points to an entry in the names table. The resolution
177 -- of a simple name consists in traversing the homonym chain, starting
178 -- from the names table. If an entry is immediately visible, it is the one
179 -- designated by the identifier. If only potentially use-visible entities
180 -- are on the chain, we must verify that they do not hide each other. If
181 -- the entity we find is overloadable, we collect all other overloadable
182 -- entities on the chain as long as they are not hidden.
184 -- To resolve expanded names, we must find the entity at the intersection
185 -- of the entity chain for the scope (the prefix) and the homonym chain
186 -- for the selector. In general, homonym chains will be much shorter than
187 -- entity chains, so it is preferable to start from the names table as
188 -- well. If the entity found is overloadable, we must collect all other
189 -- interpretations that are defined in the scope denoted by the prefix.
191 -- For records, protected types, and tasks, their local entities are
192 -- removed from visibility chains on exit from the corresponding scope.
193 -- From the outside, these entities are always accessed by selected
194 -- notation, and the entity chain for the record type, protected type,
195 -- etc. is traversed sequentially in order to find the designated entity.
197 -- The discriminants of a type and the operations of a protected type or
198 -- task are unchained on exit from the first view of the type, (such as
199 -- a private or incomplete type declaration, or a protected type speci-
200 -- fication) and re-chained when compiling the second view.
202 -- In the case of operators, we do not make operators on derived types
203 -- explicit. As a result, the notation P."+" may denote either a user-
204 -- defined function with name "+", or else an implicit declaration of the
205 -- operator "+" in package P. The resolution of expanded names always
206 -- tries to resolve an operator name as such an implicitly defined entity,
207 -- in addition to looking for explicit declarations.
209 -- All forms of names that denote entities (simple names, expanded names,
210 -- character literals in some cases) have a Entity attribute, which
211 -- identifies the entity denoted by the name.
213 ---------------------
214 -- The Scope Stack --
215 ---------------------
217 -- The Scope stack keeps track of the scopes currently been compiled.
218 -- Every entity that contains declarations (including records) is placed
219 -- on the scope stack while it is being processed, and removed at the end.
220 -- Whenever a non-package scope is exited, the entities defined therein
221 -- are removed from the visibility table, so that entities in outer scopes
222 -- become visible (see previous description). On entry to Sem, the scope
223 -- stack only contains the package Standard. As usual, subunits complicate
224 -- this picture ever so slightly.
226 -- The Rtsfind mechanism can force a call to Semantics while another
227 -- compilation is in progress. The unit retrieved by Rtsfind must be
228 -- compiled in its own context, and has no access to the visibility of
229 -- the unit currently being compiled. The procedures Save_Scope_Stack and
230 -- Restore_Scope_Stack make entities in current open scopes invisible
231 -- before compiling the retrieved unit, and restore the compilation
232 -- environment afterwards.
234 ------------------------
235 -- Compiling subunits --
236 ------------------------
238 -- Subunits must be compiled in the environment of the corresponding stub,
239 -- that is to say with the same visibility into the parent (and its
240 -- context) that is available at the point of the stub declaration, but
241 -- with the additional visibility provided by the context clause of the
242 -- subunit itself. As a result, compilation of a subunit forces compilation
243 -- of the parent (see description in lib-). At the point of the stub
244 -- declaration, Analyze is called recursively to compile the proper body of
245 -- the subunit, but without reinitializing the names table, nor the scope
246 -- stack (i.e. standard is not pushed on the stack). In this fashion the
247 -- context of the subunit is added to the context of the parent, and the
248 -- subunit is compiled in the correct environment. Note that in the course
249 -- of processing the context of a subunit, Standard will appear twice on
250 -- the scope stack: once for the parent of the subunit, and once for the
251 -- unit in the context clause being compiled. However, the two sets of
252 -- entities are not linked by homonym chains, so that the compilation of
253 -- any context unit happens in a fresh visibility environment.
255 -------------------------------
256 -- Processing of USE Clauses --
257 -------------------------------
259 -- Every defining occurrence has a flag indicating if it is potentially use
260 -- visible. Resolution of simple names examines this flag. The processing
261 -- of use clauses consists in setting this flag on all visible entities
262 -- defined in the corresponding package. On exit from the scope of the use
263 -- clause, the corresponding flag must be reset. However, a package may
264 -- appear in several nested use clauses (pathological but legal, alas)
265 -- which forces us to use a slightly more involved scheme:
267 -- a) The defining occurrence for a package holds a flag -In_Use- to
268 -- indicate that it is currently in the scope of a use clause. If a
269 -- redundant use clause is encountered, then the corresponding occurrence
270 -- of the package name is flagged -Redundant_Use-.
272 -- b) On exit from a scope, the use clauses in its declarative part are
273 -- scanned. The visibility flag is reset in all entities declared in
274 -- package named in a use clause, as long as the package is not flagged
275 -- as being in a redundant use clause (in which case the outer use
276 -- clause is still in effect, and the direct visibility of its entities
277 -- must be retained).
279 -- Note that entities are not removed from their homonym chains on exit
280 -- from the package specification. A subsequent use clause does not need
281 -- to rechain the visible entities, but only to establish their direct
284 -----------------------------------
285 -- Handling private declarations --
286 -----------------------------------
288 -- The principle that each entity has a single defining occurrence clashes
289 -- with the presence of two separate definitions for private types: the
290 -- first is the private type declaration, and second is the full type
291 -- declaration. It is important that all references to the type point to
292 -- the same defining occurrence, namely the first one. To enforce the two
293 -- separate views of the entity, the corresponding information is swapped
294 -- between the two declarations. Outside of the package, the defining
295 -- occurrence only contains the private declaration information, while in
296 -- the private part and the body of the package the defining occurrence
297 -- contains the full declaration. To simplify the swap, the defining
298 -- occurrence that currently holds the private declaration points to the
299 -- full declaration. During semantic processing the defining occurrence
300 -- also points to a list of private dependents, that is to say access types
301 -- or composite types whose designated types or component types are
302 -- subtypes or derived types of the private type in question. After the
303 -- full declaration has been seen, the private dependents are updated to
304 -- indicate that they have full definitions.
306 ------------------------------------
307 -- Handling of Undefined Messages --
308 ------------------------------------
310 -- In normal mode, only the first use of an undefined identifier generates
311 -- a message. The table Urefs is used to record error messages that have
312 -- been issued so that second and subsequent ones do not generate further
313 -- messages. However, the second reference causes text to be added to the
314 -- original undefined message noting "(more references follow)". The
315 -- full error list option (-gnatf) forces messages to be generated for
316 -- every reference and disconnects the use of this table.
318 type Uref_Entry is record
320 -- Node for identifier for which original message was posted. The
321 -- Chars field of this identifier is used to detect later references
322 -- to the same identifier.
325 -- Records error message Id of original undefined message. Reset to
326 -- No_Error_Msg after the second occurrence, where it is used to add
327 -- text to the original message as described above.
330 -- Set if the message is not visible rather than undefined
333 -- Records location of error message. Used to make sure that we do
334 -- not consider a, b : undefined as two separate instances, which
335 -- would otherwise happen, since the parser converts this sequence
336 -- to a : undefined; b : undefined.
340 package Urefs is new Table.Table (
341 Table_Component_Type => Uref_Entry,
342 Table_Index_Type => Nat,
343 Table_Low_Bound => 1,
345 Table_Increment => 100,
346 Table_Name => "Urefs");
348 Candidate_Renaming : Entity_Id;
349 -- Holds a candidate interpretation that appears in a subprogram renaming
350 -- declaration and does not match the given specification, but matches at
351 -- least on the first formal. Allows better error message when given
352 -- specification omits defaulted parameters, a common error.
354 -----------------------
355 -- Local Subprograms --
356 -----------------------
358 procedure Analyze_Generic_Renaming
361 -- Common processing for all three kinds of generic renaming declarations.
362 -- Enter new name and indicate that it renames the generic unit.
364 procedure Analyze_Renamed_Character
368 -- Renamed entity is given by a character literal, which must belong
369 -- to the return type of the new entity. Is_Body indicates whether the
370 -- declaration is a renaming_as_body. If the original declaration has
371 -- already been frozen (because of an intervening body, e.g.) the body of
372 -- the function must be built now. The same applies to the following
373 -- various renaming procedures.
375 procedure Analyze_Renamed_Dereference
379 -- Renamed entity is given by an explicit dereference. Prefix must be a
380 -- conformant access_to_subprogram type.
382 procedure Analyze_Renamed_Entry
386 -- If the renamed entity in a subprogram renaming is an entry or protected
387 -- subprogram, build a body for the new entity whose only statement is a
388 -- call to the renamed entity.
390 procedure Analyze_Renamed_Family_Member
394 -- Used when the renamed entity is an indexed component. The prefix must
395 -- denote an entry family.
397 procedure Analyze_Renamed_Primitive_Operation
401 -- If the renamed entity in a subprogram renaming is a primitive operation
402 -- or a class-wide operation in prefix form, save the target object,
403 -- which must be added to the list of actuals in any subsequent call.
404 -- The renaming operation is intrinsic because the compiler must in
405 -- fact generate a wrapper for it (6.3.1 (10 1/2)).
407 procedure Attribute_Renaming (N : Node_Id);
408 -- Analyze renaming of attribute as subprogram. The renaming declaration N
409 -- is rewritten as a subprogram body that returns the attribute reference
410 -- applied to the formals of the function.
412 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
413 -- Set Entity, with style check if need be. For a discriminant reference,
414 -- replace by the corresponding discriminal, i.e. the parameter of the
415 -- initialization procedure that corresponds to the discriminant.
417 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
418 -- A renaming_as_body may occur after the entity of the original decla-
419 -- ration has been frozen. In that case, the body of the new entity must
420 -- be built now, because the usual mechanism of building the renamed
421 -- body at the point of freezing will not work. Subp is the subprogram
422 -- for which N provides the Renaming_As_Body.
424 procedure Check_In_Previous_With_Clause
427 -- N is a use_package clause and Nam the package name, or N is a use_type
428 -- clause and Nam is the prefix of the type name. In either case, verify
429 -- that the package is visible at that point in the context: either it
430 -- appears in a previous with_clause, or because it is a fully qualified
431 -- name and the root ancestor appears in a previous with_clause.
433 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
434 -- Verify that the entity in a renaming declaration that is a library unit
435 -- is itself a library unit and not a nested unit or subunit. Also check
436 -- that if the renaming is a child unit of a generic parent, then the
437 -- renamed unit must also be a child unit of that parent. Finally, verify
438 -- that a renamed generic unit is not an implicit child declared within
439 -- an instance of the parent.
441 procedure Chain_Use_Clause (N : Node_Id);
442 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
443 -- the proper scope table entry. This is usually the current scope, but it
444 -- will be an inner scope when installing the use clauses of the private
445 -- declarations of a parent unit prior to compiling the private part of a
446 -- child unit. This chain is traversed when installing/removing use clauses
447 -- when compiling a subunit or instantiating a generic body on the fly,
448 -- when it is necessary to save and restore full environments.
450 function Enclosing_Instance return Entity_Id;
451 -- In an instance nested within another one, several semantic checks are
452 -- unnecessary because the legality of the nested instance has been checked
453 -- in the enclosing generic unit. This applies in particular to legality
454 -- checks on actuals for formal subprograms of the inner instance, which
455 -- are checked as subprogram renamings, and may be complicated by confusion
456 -- in private/full views. This function returns the instance enclosing the
457 -- current one if there is such, else it returns Empty.
459 -- If the renaming determines the entity for the default of a formal
460 -- subprogram nested within another instance, choose the innermost
461 -- candidate. This is because if the formal has a box, and we are within
462 -- an enclosing instance where some candidate interpretations are local
463 -- to this enclosing instance, we know that the default was properly
464 -- resolved when analyzing the generic, so we prefer the local
465 -- candidates to those that are external. This is not always the case
466 -- but is a reasonable heuristic on the use of nested generics. The
467 -- proper solution requires a full renaming model.
469 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
470 -- Return the appropriate entity for determining which unit has a deeper
471 -- scope: the defining entity for U, unless U is a package instance, in
472 -- which case we retrieve the entity of the instance spec.
474 procedure Find_Expanded_Name (N : Node_Id);
475 -- The input is a selected component known to be an expanded name. Verify
476 -- legality of selector given the scope denoted by prefix, and change node
477 -- N into a expanded name with a properly set Entity field.
479 function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id;
480 -- Find the most previous use clause (that is, the first one to appear in
481 -- the source) by traversing the previous clause chain that exists in both
482 -- N_Use_Package_Clause nodes and N_Use_Type_Clause nodes.
483 -- ??? a better subprogram name is in order
485 function Find_Renamed_Entity
489 Is_Actual : Boolean := False) return Entity_Id;
490 -- Find the renamed entity that corresponds to the given parameter profile
491 -- in a subprogram renaming declaration. The renamed entity may be an
492 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
493 -- indicates that the renaming is the one generated for an actual subpro-
494 -- gram in an instance, for which special visibility checks apply.
496 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
497 -- Find a type derived from Character or Wide_Character in the prefix of N.
498 -- Used to resolved qualified names whose selector is a character literal.
500 function Has_Private_With (E : Entity_Id) return Boolean;
501 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
502 -- private with on E.
504 function Has_Components (Typ : Entity_Id) return Boolean;
505 -- Determine if given type has components, i.e. is either a record type or
506 -- type or a type that has discriminants.
508 function Has_Implicit_Operator (N : Node_Id) return Boolean;
509 -- N is an expanded name whose selector is an operator name (e.g. P."+").
510 -- declarative part contains an implicit declaration of an operator if it
511 -- has a declaration of a type to which one of the predefined operators
512 -- apply. The existence of this routine is an implementation artifact. A
513 -- more straightforward but more space-consuming choice would be to make
514 -- all inherited operators explicit in the symbol table.
516 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
517 -- A subprogram defined by a renaming declaration inherits the parameter
518 -- profile of the renamed entity. The subtypes given in the subprogram
519 -- specification are discarded and replaced with those of the renamed
520 -- subprogram, which are then used to recheck the default values.
522 function Most_Descendant_Use_Clause
523 (Clause1 : Entity_Id;
524 Clause2 : Entity_Id) return Entity_Id;
525 -- Determine which use clause parameter is the most descendant in terms of
527 -- ??? a better subprogram name is in order
529 procedure Premature_Usage (N : Node_Id);
530 -- Diagnose usage of an entity before it is visible
532 procedure Use_One_Package
534 Pack_Name : Entity_Id := Empty;
535 Force : Boolean := False);
536 -- Make visible entities declared in package P potentially use-visible
537 -- in the current context. Also used in the analysis of subunits, when
538 -- re-installing use clauses of parent units. N is the use_clause that
539 -- names P (and possibly other packages).
541 procedure Use_One_Type
543 Installed : Boolean := False;
544 Force : Boolean := False);
545 -- Id is the subtype mark from a use_type_clause. This procedure makes
546 -- the primitive operators of the type potentially use-visible. The
547 -- boolean flag Installed indicates that the clause is being reinstalled
548 -- after previous analysis, and primitive operations are already chained
549 -- on the Used_Operations list of the clause.
551 procedure Write_Info;
552 -- Write debugging information on entities declared in current scope
554 --------------------------------
555 -- Analyze_Exception_Renaming --
556 --------------------------------
558 -- The language only allows a single identifier, but the tree holds an
559 -- identifier list. The parser has already issued an error message if
560 -- there is more than one element in the list.
562 procedure Analyze_Exception_Renaming (N : Node_Id) is
563 Id : constant Entity_Id := Defining_Entity (N);
564 Nam : constant Node_Id := Name (N);
570 Set_Ekind (Id, E_Exception);
571 Set_Etype (Id, Standard_Exception_Type);
572 Set_Is_Pure (Id, Is_Pure (Current_Scope));
574 if Is_Entity_Name (Nam)
575 and then Present (Entity (Nam))
576 and then Ekind (Entity (Nam)) = E_Exception
578 if Present (Renamed_Object (Entity (Nam))) then
579 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
581 Set_Renamed_Object (Id, Entity (Nam));
584 -- The exception renaming declaration may become Ghost if it renames
587 Mark_Ghost_Renaming (N, Entity (Nam));
589 Error_Msg_N ("invalid exception name in renaming", Nam);
592 -- Implementation-defined aspect specifications can appear in a renaming
593 -- declaration, but not language-defined ones. The call to procedure
594 -- Analyze_Aspect_Specifications will take care of this error check.
596 if Has_Aspects (N) then
597 Analyze_Aspect_Specifications (N, Id);
599 end Analyze_Exception_Renaming;
601 ---------------------------
602 -- Analyze_Expanded_Name --
603 ---------------------------
605 procedure Analyze_Expanded_Name (N : Node_Id) is
607 -- If the entity pointer is already set, this is an internal node, or a
608 -- node that is analyzed more than once, after a tree modification. In
609 -- such a case there is no resolution to perform, just set the type. In
610 -- either case, start by analyzing the prefix.
612 Analyze (Prefix (N));
614 if Present (Entity (N)) then
615 if Is_Type (Entity (N)) then
616 Set_Etype (N, Entity (N));
618 Set_Etype (N, Etype (Entity (N)));
622 Find_Expanded_Name (N);
625 -- In either case, propagate dimension of entity to expanded name
627 Analyze_Dimension (N);
628 end Analyze_Expanded_Name;
630 ---------------------------------------
631 -- Analyze_Generic_Function_Renaming --
632 ---------------------------------------
634 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
636 Analyze_Generic_Renaming (N, E_Generic_Function);
637 end Analyze_Generic_Function_Renaming;
639 --------------------------------------
640 -- Analyze_Generic_Package_Renaming --
641 --------------------------------------
643 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
645 -- Test for the Text_IO special unit case here, since we may be renaming
646 -- one of the subpackages of Text_IO, then join common routine.
648 Check_Text_IO_Special_Unit (Name (N));
650 Analyze_Generic_Renaming (N, E_Generic_Package);
651 end Analyze_Generic_Package_Renaming;
653 ----------------------------------------
654 -- Analyze_Generic_Procedure_Renaming --
655 ----------------------------------------
657 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
659 Analyze_Generic_Renaming (N, E_Generic_Procedure);
660 end Analyze_Generic_Procedure_Renaming;
662 ------------------------------
663 -- Analyze_Generic_Renaming --
664 ------------------------------
666 procedure Analyze_Generic_Renaming
670 New_P : constant Entity_Id := Defining_Entity (N);
671 Inst : Boolean := False;
675 if Name (N) = Error then
679 Generate_Definition (New_P);
681 if Current_Scope /= Standard_Standard then
682 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
685 if Nkind (Name (N)) = N_Selected_Component then
686 Check_Generic_Child_Unit (Name (N), Inst);
691 if not Is_Entity_Name (Name (N)) then
692 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
695 Old_P := Entity (Name (N));
699 Set_Ekind (New_P, K);
701 if Etype (Old_P) = Any_Type then
704 elsif Ekind (Old_P) /= K then
705 Error_Msg_N ("invalid generic unit name", Name (N));
708 if Present (Renamed_Object (Old_P)) then
709 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
711 Set_Renamed_Object (New_P, Old_P);
714 -- The generic renaming declaration may become Ghost if it renames a
717 Mark_Ghost_Renaming (N, Old_P);
719 Set_Is_Pure (New_P, Is_Pure (Old_P));
720 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
722 Set_Etype (New_P, Etype (Old_P));
723 Set_Has_Completion (New_P);
725 if In_Open_Scopes (Old_P) then
726 Error_Msg_N ("within its scope, generic denotes its instance", N);
729 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
730 -- renamings and subsequent instantiations of Unchecked_Conversion.
732 if Is_Generic_Subprogram (Old_P) then
733 Set_Is_Intrinsic_Subprogram
734 (New_P, Is_Intrinsic_Subprogram (Old_P));
737 Check_Library_Unit_Renaming (N, Old_P);
740 -- Implementation-defined aspect specifications can appear in a renaming
741 -- declaration, but not language-defined ones. The call to procedure
742 -- Analyze_Aspect_Specifications will take care of this error check.
744 if Has_Aspects (N) then
745 Analyze_Aspect_Specifications (N, New_P);
747 end Analyze_Generic_Renaming;
749 -----------------------------
750 -- Analyze_Object_Renaming --
751 -----------------------------
753 procedure Analyze_Object_Renaming (N : Node_Id) is
754 Id : constant Entity_Id := Defining_Identifier (N);
755 Loc : constant Source_Ptr := Sloc (N);
756 Nam : constant Node_Id := Name (N);
757 Is_Object_Ref : Boolean;
762 procedure Check_Constrained_Object;
763 -- If the nominal type is unconstrained but the renamed object is
764 -- constrained, as can happen with renaming an explicit dereference or
765 -- a function return, build a constrained subtype from the object. If
766 -- the renaming is for a formal in an accept statement, the analysis
767 -- has already established its actual subtype. This is only relevant
768 -- if the renamed object is an explicit dereference.
770 function Get_Object_Name (Nod : Node_Id) return Node_Id;
771 -- Obtain the name of the object from node Nod which is being renamed by
772 -- the object renaming declaration N.
774 ------------------------------
775 -- Check_Constrained_Object --
776 ------------------------------
778 procedure Check_Constrained_Object is
779 Typ : constant Entity_Id := Etype (Nam);
783 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference
784 and then Is_Composite_Type (Typ)
785 and then not Is_Constrained (Typ)
786 and then not Has_Unknown_Discriminants (Typ)
787 and then Expander_Active
789 -- If Actual_Subtype is already set, nothing to do
791 if Ekind (Id) in E_Variable | E_Constant
792 and then Present (Actual_Subtype (Id))
796 -- A renaming of an unchecked union has no actual subtype
798 elsif Is_Unchecked_Union (Typ) then
801 -- If a record is limited its size is invariant. This is the case
802 -- in particular with record types with an access discriminant
803 -- that are used in iterators. This is an optimization, but it
804 -- also prevents typing anomalies when the prefix is further
806 -- Note that we cannot just use the Is_Limited_Record flag because
807 -- it does not apply to records with limited components, for which
808 -- this syntactic flag is not set, but whose size is also fixed.
810 elsif Is_Limited_Type (Typ) then
814 Subt := Make_Temporary (Loc, 'T');
815 Remove_Side_Effects (Nam);
817 Make_Subtype_Declaration (Loc,
818 Defining_Identifier => Subt,
819 Subtype_Indication =>
820 Make_Subtype_From_Expr (Nam, Typ)));
821 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
822 Set_Etype (Nam, Subt);
824 -- Freeze subtype at once, to prevent order of elaboration
825 -- issues in the backend. The renamed object exists, so its
826 -- type is already frozen in any case.
828 Freeze_Before (N, Subt);
831 end Check_Constrained_Object;
833 ---------------------
834 -- Get_Object_Name --
835 ---------------------
837 function Get_Object_Name (Nod : Node_Id) return Node_Id is
842 while Present (Obj_Nam) loop
843 case Nkind (Obj_Nam) is
844 when N_Attribute_Reference
845 | N_Explicit_Dereference
846 | N_Indexed_Component
849 Obj_Nam := Prefix (Obj_Nam);
851 when N_Selected_Component =>
852 Obj_Nam := Selector_Name (Obj_Nam);
854 when N_Qualified_Expression | N_Type_Conversion =>
855 Obj_Nam := Expression (Obj_Nam);
865 -- Start of processing for Analyze_Object_Renaming
872 Set_Is_Pure (Id, Is_Pure (Current_Scope));
875 -- The renaming of a component that depends on a discriminant requires
876 -- an actual subtype, because in subsequent use of the object Gigi will
877 -- be unable to locate the actual bounds. This explicit step is required
878 -- when the renaming is generated in removing side effects of an
879 -- already-analyzed expression.
881 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
883 -- The object renaming declaration may become Ghost if it renames a
886 if Is_Entity_Name (Nam) then
887 Mark_Ghost_Renaming (N, Entity (Nam));
891 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
893 if Present (Dec) then
894 Insert_Action (N, Dec);
895 T := Defining_Identifier (Dec);
898 elsif Present (Subtype_Mark (N))
899 or else not Present (Access_Definition (N))
901 if Present (Subtype_Mark (N)) then
902 Find_Type (Subtype_Mark (N));
903 T := Entity (Subtype_Mark (N));
906 -- AI12-0275: Case of object renaming without a subtype_mark
911 -- Normal case of no overloading in object name
913 if not Is_Overloaded (Nam) then
915 -- Catch error cases (such as attempting to rename a procedure
916 -- or package) using the shorthand form.
919 or else Etype (Nam) = Standard_Void_Type
922 ("object name or value expected in renaming", Nam);
924 Set_Ekind (Id, E_Variable);
925 Set_Etype (Id, Any_Type);
933 -- Case of overloaded name, which will be illegal if there's more
934 -- than one acceptable interpretation (such as overloaded function
946 -- More than one candidate interpretation is available
948 -- Remove procedure calls, which syntactically cannot appear
949 -- in this context, but which cannot be removed by type
950 -- checking, because the context does not impose a type.
952 Get_First_Interp (Nam, I, It);
953 while Present (It.Typ) loop
954 if It.Typ = Standard_Void_Type then
958 Get_Next_Interp (I, It);
961 Get_First_Interp (Nam, I, It);
965 -- If there's no type present, we have an error case (such
966 -- as overloaded procedures named in the object renaming).
970 ("object name or value expected in renaming", Nam);
972 Set_Ekind (Id, E_Variable);
973 Set_Etype (Id, Any_Type);
978 Get_Next_Interp (I, It);
980 if Present (It.Typ) then
982 It1 := Disambiguate (Nam, I1, I, Any_Type);
984 if It1 = No_Interp then
985 Error_Msg_N ("ambiguous name in object renaming", Nam);
987 Error_Msg_Sloc := Sloc (It.Nam);
988 Error_Msg_N ("\\possible interpretation#!", Nam);
990 Error_Msg_Sloc := Sloc (Nam1);
991 Error_Msg_N ("\\possible interpretation#!", Nam);
997 Set_Etype (Nam, It1.Typ);
1002 if Etype (Nam) = Standard_Exception_Type then
1004 ("exception requires a subtype mark in renaming", Nam);
1009 -- The object renaming declaration may become Ghost if it renames a
1012 if Is_Entity_Name (Nam) then
1013 Mark_Ghost_Renaming (N, Entity (Nam));
1018 -- If the renamed object is a function call of a limited type,
1019 -- the expansion of the renaming is complicated by the presence
1020 -- of various temporaries and subtypes that capture constraints
1021 -- of the renamed object. Rewrite node as an object declaration,
1022 -- whose expansion is simpler. Given that the object is limited
1023 -- there is no copy involved and no performance hit.
1025 if Nkind (Nam) = N_Function_Call
1026 and then Is_Limited_View (Etype (Nam))
1027 and then not Is_Constrained (Etype (Nam))
1028 and then Comes_From_Source (N)
1031 Set_Ekind (Id, E_Constant);
1033 Make_Object_Declaration (Loc,
1034 Defining_Identifier => Id,
1035 Constant_Present => True,
1036 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
1037 Expression => Relocate_Node (Nam)));
1041 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
1042 -- when renaming declaration has a named access type. The Ada 2012
1043 -- coverage rules allow an anonymous access type in the context of
1044 -- an expected named general access type, but the renaming rules
1045 -- require the types to be the same. (An exception is when the type
1046 -- of the renaming is also an anonymous access type, which can only
1047 -- happen due to a renaming created by the expander.)
1049 if Nkind (Nam) = N_Type_Conversion
1050 and then not Comes_From_Source (Nam)
1051 and then Is_Anonymous_Access_Type (Etype (Expression (Nam)))
1052 and then not Is_Anonymous_Access_Type (T)
1054 Wrong_Type (Expression (Nam), T); -- Should we give better error???
1057 -- Check that a class-wide object is not being renamed as an object
1058 -- of a specific type. The test for access types is needed to exclude
1059 -- cases where the renamed object is a dynamically tagged access
1060 -- result, such as occurs in certain expansions.
1062 if Is_Tagged_Type (T) then
1063 Check_Dynamically_Tagged_Expression
1069 -- Ada 2005 (AI-230/AI-254): Access renaming
1071 else pragma Assert (Present (Access_Definition (N)));
1075 N => Access_Definition (N));
1079 -- The object renaming declaration may become Ghost if it renames a
1082 if Is_Entity_Name (Nam) then
1083 Mark_Ghost_Renaming (N, Entity (Nam));
1086 -- Ada 2005 AI05-105: if the declaration has an anonymous access
1087 -- type, the renamed object must also have an anonymous type, and
1088 -- this is a name resolution rule. This was implicit in the last part
1089 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
1092 if not Is_Overloaded (Nam) then
1093 if Ekind (Etype (Nam)) /= Ekind (T) then
1095 ("expect anonymous access type in object renaming", N);
1102 Typ : Entity_Id := Empty;
1103 Seen : Boolean := False;
1106 Get_First_Interp (Nam, I, It);
1107 while Present (It.Typ) loop
1109 -- Renaming is ambiguous if more than one candidate
1110 -- interpretation is type-conformant with the context.
1112 if Ekind (It.Typ) = Ekind (T) then
1113 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
1116 (Designated_Type (T), Designated_Type (It.Typ))
1122 ("ambiguous expression in renaming", Nam);
1125 elsif Ekind (T) = E_Anonymous_Access_Type
1127 Covers (Designated_Type (T), Designated_Type (It.Typ))
1133 ("ambiguous expression in renaming", Nam);
1137 if Covers (T, It.Typ) then
1139 Set_Etype (Nam, Typ);
1140 Set_Is_Overloaded (Nam, False);
1144 Get_Next_Interp (I, It);
1151 -- Do not perform the legality checks below when the resolution of
1152 -- the renaming name failed because the associated type is Any_Type.
1154 if Etype (Nam) = Any_Type then
1157 -- Ada 2005 (AI-231): In the case where the type is defined by an
1158 -- access_definition, the renamed entity shall be of an access-to-
1159 -- constant type if and only if the access_definition defines an
1160 -- access-to-constant type. ARM 8.5.1(4)
1162 elsif Constant_Present (Access_Definition (N))
1163 and then not Is_Access_Constant (Etype (Nam))
1166 ("(Ada 2005): the renamed object is not access-to-constant "
1167 & "(RM 8.5.1(6))", N);
1169 elsif not Constant_Present (Access_Definition (N))
1170 and then Is_Access_Constant (Etype (Nam))
1173 ("(Ada 2005): the renamed object is not access-to-variable "
1174 & "(RM 8.5.1(6))", N);
1177 if Is_Access_Subprogram_Type (Etype (Nam)) then
1178 Check_Subtype_Conformant
1179 (Designated_Type (T), Designated_Type (Etype (Nam)));
1181 elsif not Subtypes_Statically_Match
1182 (Designated_Type (T),
1183 Available_View (Designated_Type (Etype (Nam))))
1186 ("subtype of renamed object does not statically match", N);
1190 -- Special processing for renaming function return object. Some errors
1191 -- and warnings are produced only for calls that come from source.
1193 if Nkind (Nam) = N_Function_Call then
1196 -- Usage is illegal in Ada 83, but renamings are also introduced
1197 -- during expansion, and error does not apply to those.
1200 if Comes_From_Source (N) then
1202 ("(Ada 83) cannot rename function return object", Nam);
1205 -- In Ada 95, warn for odd case of renaming parameterless function
1206 -- call if this is not a limited type (where this is useful).
1209 if Warn_On_Object_Renames_Function
1210 and then No (Parameter_Associations (Nam))
1211 and then not Is_Limited_Type (Etype (Nam))
1212 and then Comes_From_Source (Nam)
1215 ("renaming function result object is suspicious?R?", Nam);
1217 ("\function & will be called only once?R?", Nam,
1218 Entity (Name (Nam)));
1219 Error_Msg_N -- CODEFIX
1220 ("\suggest using an initialized constant object "
1221 & "instead?R?", Nam);
1226 Check_Constrained_Object;
1228 -- An object renaming requires an exact match of the type. Class-wide
1229 -- matching is not allowed.
1231 if Is_Class_Wide_Type (T)
1232 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1234 Wrong_Type (Nam, T);
1237 -- We must search for an actual subtype here so that the bounds of
1238 -- objects of unconstrained types don't get dropped on the floor - such
1239 -- as with renamings of formal parameters.
1241 T2 := Get_Actual_Subtype_If_Available (Nam);
1243 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1245 if Nkind (Nam) = N_Explicit_Dereference
1246 and then Ekind (Etype (T2)) = E_Incomplete_Type
1248 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1251 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1252 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1256 if Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1258 Nam_Ent : constant Entity_Id := Entity (Get_Object_Name (Nam));
1259 Nam_Decl : constant Node_Id := Declaration_Node (Nam_Ent);
1262 if Has_Null_Exclusion (N)
1263 and then not Has_Null_Exclusion (Nam_Decl)
1265 -- Ada 2005 (AI-423): If the object name denotes a generic
1266 -- formal object of a generic unit G, and the object renaming
1267 -- declaration occurs within the body of G or within the body
1268 -- of a generic unit declared within the declarative region
1269 -- of G, then the declaration of the formal object of G must
1270 -- have a null exclusion or a null-excluding subtype.
1272 if Is_Formal_Object (Nam_Ent)
1273 and then In_Generic_Scope (Id)
1275 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1277 ("object does not exclude `NULL` "
1278 & "(RM 8.5.1(4.6/2))", N);
1280 elsif In_Package_Body (Scope (Id)) then
1282 ("formal object does not have a null exclusion"
1283 & "(RM 8.5.1(4.6/2))", N);
1286 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1287 -- shall exclude null.
1289 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1291 ("object does not exclude `NULL` "
1292 & "(RM 8.5.1(4.6/2))", N);
1294 -- An instance is illegal if it contains a renaming that
1295 -- excludes null, and the actual does not. The renaming
1296 -- declaration has already indicated that the declaration
1297 -- of the renamed actual in the instance will raise
1298 -- constraint_error.
1300 elsif Nkind (Nam_Decl) = N_Object_Declaration
1301 and then In_Instance
1303 Present (Corresponding_Generic_Association (Nam_Decl))
1304 and then Nkind (Expression (Nam_Decl)) =
1305 N_Raise_Constraint_Error
1308 ("actual does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1310 -- Finally, if there is a null exclusion, the subtype mark
1311 -- must not be null-excluding.
1313 elsif No (Access_Definition (N))
1314 and then Can_Never_Be_Null (T)
1317 ("`NOT NULL` not allowed (& already excludes null)",
1322 elsif Can_Never_Be_Null (T)
1323 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1326 ("object does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1328 elsif Has_Null_Exclusion (N)
1329 and then No (Access_Definition (N))
1330 and then Can_Never_Be_Null (T)
1333 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1338 -- Set the Ekind of the entity, unless it has been set already, as is
1339 -- the case for the iteration object over a container with no variable
1340 -- indexing. In that case it's been marked as a constant, and we do not
1341 -- want to change it to a variable.
1343 if Ekind (Id) /= E_Constant then
1344 Set_Ekind (Id, E_Variable);
1347 -- Initialize the object size and alignment. Note that we used to call
1348 -- Init_Size_Align here, but that's wrong for objects which have only
1349 -- an Esize, not an RM_Size field.
1351 Init_Object_Size_Align (Id);
1353 -- If N comes from source then check that the original node is an
1354 -- object reference since there may have been several rewritting and
1355 -- folding. Do not do this for N_Function_Call or N_Explicit_Dereference
1356 -- which might correspond to rewrites of e.g. N_Selected_Component
1357 -- (for example Object.Method rewriting).
1358 -- If N does not come from source then assume the tree is properly
1359 -- formed and accept any object reference. In such cases we do support
1360 -- more cases of renamings anyway, so the actual check on which renaming
1361 -- is valid is better left to the code generator as a last sanity
1364 if Comes_From_Source (N) then
1365 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference then
1366 Is_Object_Ref := Is_Object_Reference (Nam);
1368 Is_Object_Ref := Is_Object_Reference (Original_Node (Nam));
1371 Is_Object_Ref := True;
1374 if T = Any_Type or else Etype (Nam) = Any_Type then
1377 -- Verify that the renamed entity is an object or function call
1379 elsif Is_Object_Ref then
1380 if Comes_From_Source (N) then
1381 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1383 ("illegal renaming of discriminant-dependent component", Nam);
1386 -- If the renaming comes from source and the renamed object is a
1387 -- dereference, then mark the prefix as needing debug information,
1388 -- since it might have been rewritten hence internally generated
1389 -- and Debug_Renaming_Declaration will link the renaming to it.
1391 if Nkind (Nam) = N_Explicit_Dereference
1392 and then Is_Entity_Name (Prefix (Nam))
1394 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1398 -- Weird but legal, equivalent to renaming a function call. Illegal
1399 -- if the literal is the result of constant-folding an attribute
1400 -- reference that is not a function.
1402 elsif Is_Entity_Name (Nam)
1403 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1404 and then Nkind (Original_Node (Nam)) /= N_Attribute_Reference
1408 -- A named number can only be renamed without a subtype mark
1410 elsif Nkind (Nam) in N_Real_Literal | N_Integer_Literal
1411 and then Present (Subtype_Mark (N))
1412 and then Present (Original_Entity (Nam))
1414 Error_Msg_N ("incompatible types in renaming", Nam);
1416 -- AI12-0383: Names that denote values can be renamed
1418 elsif Ada_Version < Ada_2020 then
1419 Error_Msg_N ("value in renaming requires -gnat2020", Nam);
1424 if not Is_Variable (Nam) then
1425 Set_Ekind (Id, E_Constant);
1426 Set_Never_Set_In_Source (Id, True);
1427 Set_Is_True_Constant (Id, True);
1430 -- The entity of the renaming declaration needs to reflect whether the
1431 -- renamed object is atomic, independent, volatile or VFA. These flags
1432 -- are set on the renamed object in the RM legality sense.
1434 Set_Is_Atomic (Id, Is_Atomic_Object (Nam));
1435 Set_Is_Independent (Id, Is_Independent_Object (Nam));
1436 Set_Is_Volatile (Id, Is_Volatile_Object (Nam));
1437 Set_Is_Volatile_Full_Access (Id, Is_Volatile_Full_Access_Object (Nam));
1439 -- Treat as volatile if we just set the Volatile flag
1443 -- Or if we are renaming an entity which was marked this way
1445 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1447 or else (Is_Entity_Name (Nam)
1448 and then Treat_As_Volatile (Entity (Nam)))
1450 Set_Treat_As_Volatile (Id, True);
1453 -- Now make the link to the renamed object
1455 Set_Renamed_Object (Id, Nam);
1457 -- Implementation-defined aspect specifications can appear in a renaming
1458 -- declaration, but not language-defined ones. The call to procedure
1459 -- Analyze_Aspect_Specifications will take care of this error check.
1461 if Has_Aspects (N) then
1462 Analyze_Aspect_Specifications (N, Id);
1465 -- Deal with dimensions
1467 Analyze_Dimension (N);
1468 end Analyze_Object_Renaming;
1470 ------------------------------
1471 -- Analyze_Package_Renaming --
1472 ------------------------------
1474 procedure Analyze_Package_Renaming (N : Node_Id) is
1475 New_P : constant Entity_Id := Defining_Entity (N);
1480 if Name (N) = Error then
1484 -- Check for Text_IO special unit (we may be renaming a Text_IO child)
1486 Check_Text_IO_Special_Unit (Name (N));
1488 if Current_Scope /= Standard_Standard then
1489 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1495 if Is_Entity_Name (Name (N)) then
1496 Old_P := Entity (Name (N));
1501 if Etype (Old_P) = Any_Type then
1502 Error_Msg_N ("expect package name in renaming", Name (N));
1504 elsif Ekind (Old_P) /= E_Package
1505 and then not (Ekind (Old_P) = E_Generic_Package
1506 and then In_Open_Scopes (Old_P))
1508 if Ekind (Old_P) = E_Generic_Package then
1510 ("generic package cannot be renamed as a package", Name (N));
1512 Error_Msg_Sloc := Sloc (Old_P);
1514 ("expect package name in renaming, found& declared#",
1518 -- Set basic attributes to minimize cascaded errors
1520 Set_Ekind (New_P, E_Package);
1521 Set_Etype (New_P, Standard_Void_Type);
1523 -- Here for OK package renaming
1526 -- Entities in the old package are accessible through the renaming
1527 -- entity. The simplest implementation is to have both packages share
1530 Set_Ekind (New_P, E_Package);
1531 Set_Etype (New_P, Standard_Void_Type);
1533 if Present (Renamed_Object (Old_P)) then
1534 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1536 Set_Renamed_Object (New_P, Old_P);
1539 -- The package renaming declaration may become Ghost if it renames a
1542 Mark_Ghost_Renaming (N, Old_P);
1544 Set_Has_Completion (New_P);
1545 Set_First_Entity (New_P, First_Entity (Old_P));
1546 Set_Last_Entity (New_P, Last_Entity (Old_P));
1547 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1548 Check_Library_Unit_Renaming (N, Old_P);
1549 Generate_Reference (Old_P, Name (N));
1551 -- If the renaming is in the visible part of a package, then we set
1552 -- Renamed_In_Spec for the renamed package, to prevent giving
1553 -- warnings about no entities referenced. Such a warning would be
1554 -- overenthusiastic, since clients can see entities in the renamed
1555 -- package via the visible package renaming.
1558 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1560 if Ekind (Ent) = E_Package
1561 and then not In_Private_Part (Ent)
1562 and then In_Extended_Main_Source_Unit (N)
1563 and then Ekind (Old_P) = E_Package
1565 Set_Renamed_In_Spec (Old_P);
1569 -- If this is the renaming declaration of a package instantiation
1570 -- within itself, it is the declaration that ends the list of actuals
1571 -- for the instantiation. At this point, the subtypes that rename
1572 -- the actuals are flagged as generic, to avoid spurious ambiguities
1573 -- if the actuals for two distinct formals happen to coincide. If
1574 -- the actual is a private type, the subtype has a private completion
1575 -- that is flagged in the same fashion.
1577 -- Resolution is identical to what is was in the original generic.
1578 -- On exit from the generic instance, these are turned into regular
1579 -- subtypes again, so they are compatible with types in their class.
1581 if not Is_Generic_Instance (Old_P) then
1584 Spec := Specification (Unit_Declaration_Node (Old_P));
1587 if Nkind (Spec) = N_Package_Specification
1588 and then Present (Generic_Parent (Spec))
1589 and then Old_P = Current_Scope
1590 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1596 E := First_Entity (Old_P);
1597 while Present (E) and then E /= New_P loop
1599 and then Nkind (Parent (E)) = N_Subtype_Declaration
1601 Set_Is_Generic_Actual_Type (E);
1603 if Is_Private_Type (E)
1604 and then Present (Full_View (E))
1606 Set_Is_Generic_Actual_Type (Full_View (E));
1616 -- Implementation-defined aspect specifications can appear in a renaming
1617 -- declaration, but not language-defined ones. The call to procedure
1618 -- Analyze_Aspect_Specifications will take care of this error check.
1620 if Has_Aspects (N) then
1621 Analyze_Aspect_Specifications (N, New_P);
1623 end Analyze_Package_Renaming;
1625 -------------------------------
1626 -- Analyze_Renamed_Character --
1627 -------------------------------
1629 procedure Analyze_Renamed_Character
1634 C : constant Node_Id := Name (N);
1637 if Ekind (New_S) = E_Function then
1638 Resolve (C, Etype (New_S));
1641 Check_Frozen_Renaming (N, New_S);
1645 Error_Msg_N ("character literal can only be renamed as function", N);
1647 end Analyze_Renamed_Character;
1649 ---------------------------------
1650 -- Analyze_Renamed_Dereference --
1651 ---------------------------------
1653 procedure Analyze_Renamed_Dereference
1658 Nam : constant Node_Id := Name (N);
1659 P : constant Node_Id := Prefix (Nam);
1665 if not Is_Overloaded (P) then
1666 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1667 or else not Type_Conformant (Etype (Nam), New_S)
1669 Error_Msg_N ("designated type does not match specification", P);
1678 Get_First_Interp (Nam, Ind, It);
1680 while Present (It.Nam) loop
1682 if Ekind (It.Nam) = E_Subprogram_Type
1683 and then Type_Conformant (It.Nam, New_S)
1685 if Typ /= Any_Id then
1686 Error_Msg_N ("ambiguous renaming", P);
1693 Get_Next_Interp (Ind, It);
1696 if Typ = Any_Type then
1697 Error_Msg_N ("designated type does not match specification", P);
1702 Check_Frozen_Renaming (N, New_S);
1706 end Analyze_Renamed_Dereference;
1708 ---------------------------
1709 -- Analyze_Renamed_Entry --
1710 ---------------------------
1712 procedure Analyze_Renamed_Entry
1717 Nam : constant Node_Id := Name (N);
1718 Sel : constant Node_Id := Selector_Name (Nam);
1719 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1723 if Entity (Sel) = Any_Id then
1725 -- Selector is undefined on prefix. Error emitted already
1727 Set_Has_Completion (New_S);
1731 -- Otherwise find renamed entity and build body of New_S as a call to it
1733 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1735 if Old_S = Any_Id then
1736 Error_Msg_N (" no subprogram or entry matches specification", N);
1739 Check_Subtype_Conformant (New_S, Old_S, N);
1740 Generate_Reference (New_S, Defining_Entity (N), 'b');
1741 Style.Check_Identifier (Defining_Entity (N), New_S);
1744 -- Only mode conformance required for a renaming_as_declaration
1746 Check_Mode_Conformant (New_S, Old_S, N);
1749 Inherit_Renamed_Profile (New_S, Old_S);
1751 -- The prefix can be an arbitrary expression that yields a task or
1752 -- protected object, so it must be resolved.
1754 if Is_Access_Type (Etype (Prefix (Nam))) then
1755 Insert_Explicit_Dereference (Prefix (Nam));
1757 Resolve (Prefix (Nam), Scope (Old_S));
1760 Set_Convention (New_S, Convention (Old_S));
1761 Set_Has_Completion (New_S, Inside_A_Generic);
1763 -- AI05-0225: If the renamed entity is a procedure or entry of a
1764 -- protected object, the target object must be a variable.
1766 if Is_Protected_Type (Scope (Old_S))
1767 and then Ekind (New_S) = E_Procedure
1768 and then not Is_Variable (Prefix (Nam))
1772 ("target object of protected operation used as actual for "
1773 & "formal procedure must be a variable", Nam);
1776 ("target object of protected operation renamed as procedure, "
1777 & "must be a variable", Nam);
1782 Check_Frozen_Renaming (N, New_S);
1784 end Analyze_Renamed_Entry;
1786 -----------------------------------
1787 -- Analyze_Renamed_Family_Member --
1788 -----------------------------------
1790 procedure Analyze_Renamed_Family_Member
1795 Nam : constant Node_Id := Name (N);
1796 P : constant Node_Id := Prefix (Nam);
1800 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1801 or else (Nkind (P) = N_Selected_Component
1802 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1804 if Is_Entity_Name (P) then
1805 Old_S := Entity (P);
1807 Old_S := Entity (Selector_Name (P));
1810 if not Entity_Matches_Spec (Old_S, New_S) then
1811 Error_Msg_N ("entry family does not match specification", N);
1814 Check_Subtype_Conformant (New_S, Old_S, N);
1815 Generate_Reference (New_S, Defining_Entity (N), 'b');
1816 Style.Check_Identifier (Defining_Entity (N), New_S);
1820 Error_Msg_N ("no entry family matches specification", N);
1823 Set_Has_Completion (New_S, Inside_A_Generic);
1826 Check_Frozen_Renaming (N, New_S);
1828 end Analyze_Renamed_Family_Member;
1830 -----------------------------------------
1831 -- Analyze_Renamed_Primitive_Operation --
1832 -----------------------------------------
1834 procedure Analyze_Renamed_Primitive_Operation
1844 Ctyp : Conformance_Type) return Boolean;
1845 -- Verify that the signatures of the renamed entity and the new entity
1846 -- match. The first formal of the renamed entity is skipped because it
1847 -- is the target object in any subsequent call.
1855 Ctyp : Conformance_Type) return Boolean
1861 if Ekind (Subp) /= Ekind (New_S) then
1865 Old_F := Next_Formal (First_Formal (Subp));
1866 New_F := First_Formal (New_S);
1867 while Present (Old_F) and then Present (New_F) loop
1868 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
1872 if Ctyp >= Mode_Conformant
1873 and then Ekind (Old_F) /= Ekind (New_F)
1878 Next_Formal (New_F);
1879 Next_Formal (Old_F);
1885 -- Start of processing for Analyze_Renamed_Primitive_Operation
1888 if not Is_Overloaded (Selector_Name (Name (N))) then
1889 Old_S := Entity (Selector_Name (Name (N)));
1891 if not Conforms (Old_S, Type_Conformant) then
1896 -- Find the operation that matches the given signature
1904 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
1906 while Present (It.Nam) loop
1907 if Conforms (It.Nam, Type_Conformant) then
1911 Get_Next_Interp (Ind, It);
1916 if Old_S = Any_Id then
1917 Error_Msg_N ("no subprogram or entry matches specification", N);
1921 if not Conforms (Old_S, Subtype_Conformant) then
1922 Error_Msg_N ("subtype conformance error in renaming", N);
1925 Generate_Reference (New_S, Defining_Entity (N), 'b');
1926 Style.Check_Identifier (Defining_Entity (N), New_S);
1929 -- Only mode conformance required for a renaming_as_declaration
1931 if not Conforms (Old_S, Mode_Conformant) then
1932 Error_Msg_N ("mode conformance error in renaming", N);
1935 -- AI12-0204: The prefix of a prefixed view that is renamed or
1936 -- passed as a formal subprogram must be renamable as an object.
1938 Nam := Prefix (Name (N));
1940 if Is_Object_Reference (Nam) then
1941 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1943 ("illegal renaming of discriminant-dependent component",
1947 Error_Msg_N ("expect object name in renaming", Nam);
1950 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
1951 -- view of a subprogram is intrinsic, because the compiler has
1952 -- to generate a wrapper for any call to it. If the name in a
1953 -- subprogram renaming is a prefixed view, the entity is thus
1954 -- intrinsic, and 'Access cannot be applied to it.
1956 Set_Convention (New_S, Convention_Intrinsic);
1959 -- Inherit_Renamed_Profile (New_S, Old_S);
1961 -- The prefix can be an arbitrary expression that yields an
1962 -- object, so it must be resolved.
1964 Resolve (Prefix (Name (N)));
1966 end Analyze_Renamed_Primitive_Operation;
1968 ---------------------------------
1969 -- Analyze_Subprogram_Renaming --
1970 ---------------------------------
1972 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1973 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
1974 Is_Actual : constant Boolean := Present (Formal_Spec);
1975 Nam : constant Node_Id := Name (N);
1976 Save_AV : constant Ada_Version_Type := Ada_Version;
1977 Save_AVP : constant Node_Id := Ada_Version_Pragma;
1978 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
1979 Spec : constant Node_Id := Specification (N);
1981 Old_S : Entity_Id := Empty;
1982 Rename_Spec : Entity_Id;
1984 procedure Build_Class_Wide_Wrapper
1985 (Ren_Id : out Entity_Id;
1986 Wrap_Id : out Entity_Id);
1987 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal
1988 -- type with unknown discriminants and a generic primitive operation of
1989 -- the said type with a box require special processing when the actual
1990 -- is a class-wide type:
1993 -- type Formal_Typ (<>) is private;
1994 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
1995 -- package Gen is ...
1997 -- package Inst is new Gen (Actual_Typ'Class);
1999 -- In this case the general renaming mechanism used in the prologue of
2000 -- an instance no longer applies:
2002 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
2004 -- The above is replaced the following wrapper/renaming combination:
2006 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
2008 -- Prim_Op (Param); -- primitive
2011 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
2013 -- This transformation applies only if there is no explicit visible
2014 -- class-wide operation at the point of the instantiation. Ren_Id is
2015 -- the entity of the renaming declaration. When the transformation
2016 -- applies, Wrap_Id is the entity of the generated class-wide wrapper
2017 -- (or Any_Id). Otherwise, Wrap_Id is the entity of the class-wide
2020 procedure Check_Null_Exclusion
2023 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
2024 -- following AI rules:
2026 -- If Ren denotes a generic formal object of a generic unit G, and the
2027 -- renaming (or instantiation containing the actual) occurs within the
2028 -- body of G or within the body of a generic unit declared within the
2029 -- declarative region of G, then the corresponding parameter of G
2030 -- shall have a null_exclusion; Otherwise the subtype of the Sub's
2031 -- formal parameter shall exclude null.
2033 -- Similarly for its return profile.
2035 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id);
2036 -- Ensure that a SPARK renaming denoted by its entity Subp_Id does not
2037 -- declare a primitive operation of a tagged type (SPARK RM 6.1.1(3)).
2039 procedure Freeze_Actual_Profile;
2040 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
2041 -- types: a callable entity freezes its profile, unless it has an
2042 -- incomplete untagged formal (RM 13.14(10.2/3)).
2044 function Has_Class_Wide_Actual return Boolean;
2045 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
2046 -- defaulted formal subprogram where the actual for the controlling
2047 -- formal type is class-wide.
2049 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
2050 -- Find renamed entity when the declaration is a renaming_as_body and
2051 -- the renamed entity may itself be a renaming_as_body. Used to enforce
2052 -- rule that a renaming_as_body is illegal if the declaration occurs
2053 -- before the subprogram it completes is frozen, and renaming indirectly
2054 -- renames the subprogram itself.(Defect Report 8652/0027).
2056 ------------------------------
2057 -- Build_Class_Wide_Wrapper --
2058 ------------------------------
2060 procedure Build_Class_Wide_Wrapper
2061 (Ren_Id : out Entity_Id;
2062 Wrap_Id : out Entity_Id)
2064 Loc : constant Source_Ptr := Sloc (N);
2067 (Subp_Id : Entity_Id;
2068 Params : List_Id) return Node_Id;
2069 -- Create a dispatching call to invoke routine Subp_Id with actuals
2070 -- built from the parameter specifications of list Params.
2072 function Build_Expr_Fun_Call
2073 (Subp_Id : Entity_Id;
2074 Params : List_Id) return Node_Id;
2075 -- Create a dispatching call to invoke function Subp_Id with actuals
2076 -- built from the parameter specifications of list Params. Return
2077 -- directly the call, so that it can be used inside an expression
2078 -- function. This is a specificity of the GNATprove mode.
2080 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
2081 -- Create a subprogram specification based on the subprogram profile
2084 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
2085 -- Find a primitive subprogram of type Typ which matches the profile
2086 -- of the renaming declaration.
2088 procedure Interpretation_Error (Subp_Id : Entity_Id);
2089 -- Emit a continuation error message suggesting subprogram Subp_Id as
2090 -- a possible interpretation.
2092 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean;
2093 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
2096 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean;
2097 -- Determine whether subprogram Subp_Id is a suitable candidate for
2098 -- the role of a wrapped subprogram.
2105 (Subp_Id : Entity_Id;
2106 Params : List_Id) return Node_Id
2108 Actuals : constant List_Id := New_List;
2109 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2113 -- Build the actual parameters of the call
2115 Formal := First (Params);
2116 while Present (Formal) loop
2118 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2123 -- return Subp_Id (Actuals);
2125 if Ekind (Subp_Id) in E_Function | E_Operator then
2127 Make_Simple_Return_Statement (Loc,
2129 Make_Function_Call (Loc,
2131 Parameter_Associations => Actuals));
2134 -- Subp_Id (Actuals);
2138 Make_Procedure_Call_Statement (Loc,
2140 Parameter_Associations => Actuals);
2144 -------------------------
2145 -- Build_Expr_Fun_Call --
2146 -------------------------
2148 function Build_Expr_Fun_Call
2149 (Subp_Id : Entity_Id;
2150 Params : List_Id) return Node_Id
2152 Actuals : constant List_Id := New_List;
2153 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2157 pragma Assert (Ekind (Subp_Id) in E_Function | E_Operator);
2159 -- Build the actual parameters of the call
2161 Formal := First (Params);
2162 while Present (Formal) loop
2164 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2169 -- Subp_Id (Actuals);
2172 Make_Function_Call (Loc,
2174 Parameter_Associations => Actuals);
2175 end Build_Expr_Fun_Call;
2181 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2182 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2183 Spec_Id : constant Entity_Id :=
2184 Make_Defining_Identifier (Loc,
2185 Chars => New_External_Name (Chars (Subp_Id), 'R'));
2188 if Ekind (Formal_Spec) = E_Procedure then
2190 Make_Procedure_Specification (Loc,
2191 Defining_Unit_Name => Spec_Id,
2192 Parameter_Specifications => Params);
2195 Make_Function_Specification (Loc,
2196 Defining_Unit_Name => Spec_Id,
2197 Parameter_Specifications => Params,
2198 Result_Definition =>
2199 New_Copy_Tree (Result_Definition (Spec)));
2203 --------------------
2204 -- Find_Primitive --
2205 --------------------
2207 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2208 procedure Replace_Parameter_Types (Spec : Node_Id);
2209 -- Given a specification Spec, replace all class-wide parameter
2210 -- types with reference to type Typ.
2212 -----------------------------
2213 -- Replace_Parameter_Types --
2214 -----------------------------
2216 procedure Replace_Parameter_Types (Spec : Node_Id) is
2218 Formal_Id : Entity_Id;
2219 Formal_Typ : Node_Id;
2222 Formal := First (Parameter_Specifications (Spec));
2223 while Present (Formal) loop
2224 Formal_Id := Defining_Identifier (Formal);
2225 Formal_Typ := Parameter_Type (Formal);
2227 -- Create a new entity for each class-wide formal to prevent
2228 -- aliasing with the original renaming. Replace the type of
2229 -- such a parameter with the candidate type.
2231 if Nkind (Formal_Typ) = N_Identifier
2232 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2234 Set_Defining_Identifier (Formal,
2235 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2237 Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc));
2242 end Replace_Parameter_Types;
2246 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2247 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2248 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2249 Subp_Id : Entity_Id;
2251 -- Start of processing for Find_Primitive
2254 -- Each attempt to find a suitable primitive of a particular type
2255 -- operates on its own copy of the original renaming. As a result
2256 -- the original renaming is kept decoration and side-effect free.
2258 -- Inherit the overloaded status of the renamed subprogram name
2260 if Is_Overloaded (Nam) then
2261 Set_Is_Overloaded (Alt_Nam);
2262 Save_Interps (Nam, Alt_Nam);
2265 -- The copied renaming is hidden from visibility to prevent the
2266 -- pollution of the enclosing context.
2268 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2270 -- The types of all class-wide parameters must be changed to the
2273 Replace_Parameter_Types (Alt_Spec);
2275 -- Try to find a suitable primitive which matches the altered
2276 -- profile of the renaming specification.
2281 Nam => Name (Alt_Ren),
2282 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2283 Is_Actual => Is_Actual);
2285 -- Do not return Any_Id if the resolion of the altered profile
2286 -- failed as this complicates further checks on the caller side,
2287 -- return Empty instead.
2289 if Subp_Id = Any_Id then
2296 --------------------------
2297 -- Interpretation_Error --
2298 --------------------------
2300 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2302 Error_Msg_Sloc := Sloc (Subp_Id);
2304 if Is_Internal (Subp_Id) then
2306 ("\\possible interpretation: predefined & #",
2310 ("\\possible interpretation: & defined #", Spec, Formal_Spec);
2312 end Interpretation_Error;
2314 ---------------------------
2315 -- Is_Intrinsic_Equality --
2316 ---------------------------
2318 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is
2321 Ekind (Subp_Id) = E_Operator
2322 and then Chars (Subp_Id) = Name_Op_Eq
2323 and then Is_Intrinsic_Subprogram (Subp_Id);
2324 end Is_Intrinsic_Equality;
2326 ---------------------------
2327 -- Is_Suitable_Candidate --
2328 ---------------------------
2330 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is
2332 if No (Subp_Id) then
2335 -- An intrinsic subprogram is never a good candidate. This is an
2336 -- indication of a missing primitive, either defined directly or
2337 -- inherited from a parent tagged type.
2339 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2345 end Is_Suitable_Candidate;
2349 Actual_Typ : Entity_Id := Empty;
2350 -- The actual class-wide type for Formal_Typ
2352 CW_Prim_OK : Boolean;
2353 CW_Prim_Op : Entity_Id;
2354 -- The class-wide subprogram (if available) which corresponds to the
2355 -- renamed generic formal subprogram.
2357 Formal_Typ : Entity_Id := Empty;
2358 -- The generic formal type with unknown discriminants
2360 Root_Prim_OK : Boolean;
2361 Root_Prim_Op : Entity_Id;
2362 -- The root type primitive (if available) which corresponds to the
2363 -- renamed generic formal subprogram.
2365 Root_Typ : Entity_Id := Empty;
2366 -- The root type of Actual_Typ
2368 Body_Decl : Node_Id;
2370 Prim_Op : Entity_Id;
2371 Spec_Decl : Node_Id;
2374 -- Start of processing for Build_Class_Wide_Wrapper
2377 -- Analyze the specification of the renaming in case the generation
2378 -- of the class-wide wrapper fails.
2380 Ren_Id := Analyze_Subprogram_Specification (Spec);
2383 -- Do not attempt to build a wrapper if the renaming is in error
2385 if Error_Posted (Nam) then
2389 -- Analyze the renamed name, but do not resolve it. The resolution is
2390 -- completed once a suitable subprogram is found.
2394 -- When the renamed name denotes the intrinsic operator equals, the
2395 -- name must be treated as overloaded. This allows for a potential
2396 -- match against the root type's predefined equality function.
2398 if Is_Intrinsic_Equality (Entity (Nam)) then
2399 Set_Is_Overloaded (Nam);
2400 Collect_Interps (Nam);
2403 -- Step 1: Find the generic formal type with unknown discriminants
2404 -- and its corresponding class-wide actual type from the renamed
2405 -- generic formal subprogram.
2407 Formal := First_Formal (Formal_Spec);
2408 while Present (Formal) loop
2409 if Has_Unknown_Discriminants (Etype (Formal))
2410 and then not Is_Class_Wide_Type (Etype (Formal))
2411 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2413 Formal_Typ := Etype (Formal);
2414 Actual_Typ := Get_Instance_Of (Formal_Typ);
2415 Root_Typ := Etype (Actual_Typ);
2419 Next_Formal (Formal);
2422 -- The specification of the generic formal subprogram should always
2423 -- contain a formal type with unknown discriminants whose actual is
2424 -- a class-wide type, otherwise this indicates a failure in routine
2425 -- Has_Class_Wide_Actual.
2427 pragma Assert (Present (Formal_Typ));
2429 -- Step 2: Find the proper class-wide subprogram or primitive which
2430 -- corresponds to the renamed generic formal subprogram.
2432 CW_Prim_Op := Find_Primitive (Actual_Typ);
2433 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2434 Root_Prim_Op := Find_Primitive (Root_Typ);
2435 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2437 -- The class-wide actual type has two subprograms which correspond to
2438 -- the renamed generic formal subprogram:
2440 -- with procedure Prim_Op (Param : Formal_Typ);
2442 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2443 -- procedure Prim_Op (Param : Actual_Typ'Class);
2445 -- Even though the declaration of the two subprograms is legal, a
2446 -- call to either one is ambiguous and therefore illegal.
2448 if CW_Prim_OK and Root_Prim_OK then
2450 -- A user-defined primitive has precedence over a predefined one
2452 if Is_Internal (CW_Prim_Op)
2453 and then not Is_Internal (Root_Prim_Op)
2455 Prim_Op := Root_Prim_Op;
2457 elsif Is_Internal (Root_Prim_Op)
2458 and then not Is_Internal (CW_Prim_Op)
2460 Prim_Op := CW_Prim_Op;
2462 elsif CW_Prim_Op = Root_Prim_Op then
2463 Prim_Op := Root_Prim_Op;
2465 -- Otherwise both candidate subprograms are user-defined and
2470 ("ambiguous actual for generic subprogram &",
2472 Interpretation_Error (Root_Prim_Op);
2473 Interpretation_Error (CW_Prim_Op);
2477 elsif CW_Prim_OK and not Root_Prim_OK then
2478 Prim_Op := CW_Prim_Op;
2480 elsif not CW_Prim_OK and Root_Prim_OK then
2481 Prim_Op := Root_Prim_Op;
2483 -- An intrinsic equality may act as a suitable candidate in the case
2484 -- of a null type extension where the parent's equality is hidden. A
2485 -- call to an intrinsic equality is expanded as dispatching.
2487 elsif Present (Root_Prim_Op)
2488 and then Is_Intrinsic_Equality (Root_Prim_Op)
2490 Prim_Op := Root_Prim_Op;
2492 -- Otherwise there are no candidate subprograms. Let the caller
2493 -- diagnose the error.
2499 -- At this point resolution has taken place and the name is no longer
2500 -- overloaded. Mark the primitive as referenced.
2502 Set_Is_Overloaded (Name (N), False);
2503 Set_Referenced (Prim_Op);
2505 -- Do not generate a wrapper when the only candidate is a class-wide
2506 -- subprogram. Instead modify the renaming to directly map the actual
2507 -- to the generic formal.
2509 if CW_Prim_OK and then Prim_Op = CW_Prim_Op then
2511 Rewrite (Nam, New_Occurrence_Of (Prim_Op, Loc));
2515 -- Step 3: Create the declaration and the body of the wrapper, insert
2516 -- all the pieces into the tree.
2518 -- In GNATprove mode, create a function wrapper in the form of an
2519 -- expression function, so that an implicit postcondition relating
2520 -- the result of calling the wrapper function and the result of the
2521 -- dispatching call to the wrapped function is known during proof.
2524 and then Ekind (Ren_Id) in E_Function | E_Operator
2526 New_Spec := Build_Spec (Ren_Id);
2528 Make_Expression_Function (Loc,
2529 Specification => New_Spec,
2532 (Subp_Id => Prim_Op,
2533 Params => Parameter_Specifications (New_Spec)));
2535 Wrap_Id := Defining_Entity (Body_Decl);
2537 -- Otherwise, create separate spec and body for the subprogram
2541 Make_Subprogram_Declaration (Loc,
2542 Specification => Build_Spec (Ren_Id));
2543 Insert_Before_And_Analyze (N, Spec_Decl);
2545 Wrap_Id := Defining_Entity (Spec_Decl);
2548 Make_Subprogram_Body (Loc,
2549 Specification => Build_Spec (Ren_Id),
2550 Declarations => New_List,
2551 Handled_Statement_Sequence =>
2552 Make_Handled_Sequence_Of_Statements (Loc,
2553 Statements => New_List (
2555 (Subp_Id => Prim_Op,
2557 Parameter_Specifications
2558 (Specification (Spec_Decl))))));
2560 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
2563 -- If the operator carries an Eliminated pragma, indicate that the
2564 -- wrapper is also to be eliminated, to prevent spurious error when
2565 -- using gnatelim on programs that include box-initialization of
2566 -- equality operators.
2568 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2570 -- In GNATprove mode, insert the body in the tree for analysis
2572 if GNATprove_Mode then
2573 Insert_Before_And_Analyze (N, Body_Decl);
2576 -- The generated body does not freeze and must be analyzed when the
2577 -- class-wide wrapper is frozen. The body is only needed if expansion
2580 if Expander_Active then
2581 Append_Freeze_Action (Wrap_Id, Body_Decl);
2584 -- Step 4: The subprogram renaming aliases the wrapper
2586 Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc));
2587 end Build_Class_Wide_Wrapper;
2589 --------------------------
2590 -- Check_Null_Exclusion --
2591 --------------------------
2593 procedure Check_Null_Exclusion
2597 Ren_Formal : Entity_Id;
2598 Sub_Formal : Entity_Id;
2600 function Null_Exclusion_Mismatch
2601 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean;
2602 -- Return True if there is a null exclusion mismatch between
2603 -- Renaming and Renamed, False otherwise.
2605 -----------------------------
2606 -- Null_Exclusion_Mismatch --
2607 -----------------------------
2609 function Null_Exclusion_Mismatch
2610 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean is
2612 return Has_Null_Exclusion (Parent (Renaming))
2614 not (Has_Null_Exclusion (Parent (Renamed))
2615 or else (Can_Never_Be_Null (Etype (Renamed))
2617 (Is_Formal_Subprogram (Sub)
2618 and then In_Generic_Body (Current_Scope))));
2619 end Null_Exclusion_Mismatch;
2624 Ren_Formal := First_Formal (Ren);
2625 Sub_Formal := First_Formal (Sub);
2626 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2627 if Null_Exclusion_Mismatch (Ren_Formal, Sub_Formal) then
2628 Error_Msg_Sloc := Sloc (Sub_Formal);
2630 ("`NOT NULL` required for parameter &#",
2631 Ren_Formal, Sub_Formal);
2634 Next_Formal (Ren_Formal);
2635 Next_Formal (Sub_Formal);
2638 -- Return profile check
2640 if Nkind (Parent (Ren)) = N_Function_Specification
2641 and then Nkind (Parent (Sub)) = N_Function_Specification
2642 and then Null_Exclusion_Mismatch (Ren, Sub)
2644 Error_Msg_Sloc := Sloc (Sub);
2645 Error_Msg_N ("return must specify `NOT NULL`#", Ren);
2647 end Check_Null_Exclusion;
2649 -------------------------------------
2650 -- Check_SPARK_Primitive_Operation --
2651 -------------------------------------
2653 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id) is
2654 Prag : constant Node_Id := SPARK_Pragma (Subp_Id);
2658 -- Nothing to do when the subprogram is not subject to SPARK_Mode On
2659 -- because this check applies to SPARK code only.
2661 if not (Present (Prag)
2662 and then Get_SPARK_Mode_From_Annotation (Prag) = On)
2666 -- Nothing to do when the subprogram is not a primitive operation
2668 elsif not Is_Primitive (Subp_Id) then
2672 Typ := Find_Dispatching_Type (Subp_Id);
2674 -- Nothing to do when the subprogram is a primitive operation of an
2681 -- At this point a renaming declaration introduces a new primitive
2682 -- operation for a tagged type.
2684 Error_Msg_Node_2 := Typ;
2686 ("subprogram renaming & cannot declare primitive for type & "
2687 & "(SPARK RM 6.1.1(3))", N, Subp_Id);
2688 end Check_SPARK_Primitive_Operation;
2690 ---------------------------
2691 -- Freeze_Actual_Profile --
2692 ---------------------------
2694 procedure Freeze_Actual_Profile is
2696 Has_Untagged_Inc : Boolean;
2697 Instantiation_Node : constant Node_Id := Parent (N);
2700 if Ada_Version >= Ada_2012 then
2701 F := First_Formal (Formal_Spec);
2702 Has_Untagged_Inc := False;
2703 while Present (F) loop
2704 if Ekind (Etype (F)) = E_Incomplete_Type
2705 and then not Is_Tagged_Type (Etype (F))
2707 Has_Untagged_Inc := True;
2714 if Ekind (Formal_Spec) = E_Function
2715 and then not Is_Tagged_Type (Etype (Formal_Spec))
2717 Has_Untagged_Inc := True;
2720 if not Has_Untagged_Inc then
2721 F := First_Formal (Old_S);
2722 while Present (F) loop
2723 Freeze_Before (Instantiation_Node, Etype (F));
2725 if Is_Incomplete_Or_Private_Type (Etype (F))
2726 and then No (Underlying_Type (Etype (F)))
2728 -- Exclude generic types, or types derived from them.
2729 -- They will be frozen in the enclosing instance.
2731 if Is_Generic_Type (Etype (F))
2732 or else Is_Generic_Type (Root_Type (Etype (F)))
2736 -- A limited view of a type declared elsewhere needs no
2737 -- freezing actions.
2739 elsif From_Limited_With (Etype (F)) then
2744 ("type& must be frozen before this point",
2745 Instantiation_Node, Etype (F));
2753 end Freeze_Actual_Profile;
2755 ---------------------------
2756 -- Has_Class_Wide_Actual --
2757 ---------------------------
2759 function Has_Class_Wide_Actual return Boolean is
2761 Formal_Typ : Entity_Id;
2765 Formal := First_Formal (Formal_Spec);
2766 while Present (Formal) loop
2767 Formal_Typ := Etype (Formal);
2769 if Has_Unknown_Discriminants (Formal_Typ)
2770 and then not Is_Class_Wide_Type (Formal_Typ)
2771 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2776 Next_Formal (Formal);
2781 end Has_Class_Wide_Actual;
2783 -------------------------
2784 -- Original_Subprogram --
2785 -------------------------
2787 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2788 Orig_Decl : Node_Id;
2789 Orig_Subp : Entity_Id;
2792 -- First case: renamed entity is itself a renaming
2794 if Present (Alias (Subp)) then
2795 return Alias (Subp);
2797 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2798 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2800 -- Check if renamed entity is a renaming_as_body
2803 Unit_Declaration_Node
2804 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2806 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2807 Orig_Subp := Entity (Name (Orig_Decl));
2809 if Orig_Subp = Rename_Spec then
2811 -- Circularity detected
2816 return (Original_Subprogram (Orig_Subp));
2824 end Original_Subprogram;
2828 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2829 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2830 -- defaulted formal subprogram when the actual for a related formal
2831 -- type is class-wide.
2833 Inst_Node : Node_Id := Empty;
2836 -- Start of processing for Analyze_Subprogram_Renaming
2839 -- We must test for the attribute renaming case before the Analyze
2840 -- call because otherwise Sem_Attr will complain that the attribute
2841 -- is missing an argument when it is analyzed.
2843 if Nkind (Nam) = N_Attribute_Reference then
2845 -- In the case of an abstract formal subprogram association, rewrite
2846 -- an actual given by a stream or Put_Image attribute as the name of
2847 -- the corresponding stream or Put_Image primitive of the type.
2849 -- In a generic context the stream and Put_Image operations are not
2850 -- generated, and this must be treated as a normal attribute
2851 -- reference, to be expanded in subsequent instantiations.
2854 and then Is_Abstract_Subprogram (Formal_Spec)
2855 and then Expander_Active
2858 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
2862 -- The class-wide forms of the stream and Put_Image attributes
2863 -- are not primitive dispatching operations (even though they
2864 -- internally dispatch).
2866 if Is_Class_Wide_Type (Prefix_Type) then
2868 ("attribute must be a primitive dispatching operation",
2873 -- Retrieve the primitive subprogram associated with the
2874 -- attribute. This can only be a stream attribute, since those
2875 -- are the only ones that are dispatching (and the actual for
2876 -- an abstract formal subprogram must be dispatching
2879 case Attribute_Name (Nam) is
2882 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input);
2886 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output);
2890 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read);
2894 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write);
2896 when Name_Put_Image =>
2898 Find_Optional_Prim_Op (Prefix_Type, TSS_Put_Image);
2902 ("attribute must be a primitive dispatching operation",
2907 -- If no stream operation was found, and the type is limited,
2908 -- the user should have defined one. This rule does not apply
2912 and then Attribute_Name (Nam) /= Name_Put_Image
2914 if Is_Limited_Type (Prefix_Type) then
2916 ("stream operation not defined for type&",
2920 -- Otherwise, compiler should have generated default
2923 raise Program_Error;
2927 -- Rewrite the attribute into the name of its corresponding
2928 -- primitive dispatching subprogram. We can then proceed with
2929 -- the usual processing for subprogram renamings.
2932 Prim_Name : constant Node_Id :=
2933 Make_Identifier (Sloc (Nam),
2934 Chars => Chars (Prim));
2936 Set_Entity (Prim_Name, Prim);
2937 Rewrite (Nam, Prim_Name);
2942 -- Normal processing for a renaming of an attribute
2945 Attribute_Renaming (N);
2950 -- Check whether this declaration corresponds to the instantiation of a
2951 -- formal subprogram.
2953 -- If this is an instantiation, the corresponding actual is frozen and
2954 -- error messages can be made more precise. If this is a default
2955 -- subprogram, the entity is already established in the generic, and is
2956 -- not retrieved by visibility. If it is a default with a box, the
2957 -- candidate interpretations, if any, have been collected when building
2958 -- the renaming declaration. If overloaded, the proper interpretation is
2959 -- determined in Find_Renamed_Entity. If the entity is an operator,
2960 -- Find_Renamed_Entity applies additional visibility checks.
2963 Inst_Node := Unit_Declaration_Node (Formal_Spec);
2965 -- Check whether the renaming is for a defaulted actual subprogram
2966 -- with a class-wide actual.
2968 -- The class-wide wrapper is not needed in GNATprove_Mode and there
2969 -- is an external axiomatization on the package.
2972 and then Box_Present (Inst_Node)
2976 Present (Containing_Package_With_Ext_Axioms (Formal_Spec)))
2978 Build_Class_Wide_Wrapper (New_S, Old_S);
2980 elsif Is_Entity_Name (Nam)
2981 and then Present (Entity (Nam))
2982 and then not Comes_From_Source (Nam)
2983 and then not Is_Overloaded (Nam)
2985 Old_S := Entity (Nam);
2987 -- The subprogram renaming declaration may become Ghost if it
2988 -- renames a Ghost entity.
2990 Mark_Ghost_Renaming (N, Old_S);
2992 New_S := Analyze_Subprogram_Specification (Spec);
2996 if Ekind (Old_S) = E_Operator then
3000 if Box_Present (Inst_Node) then
3001 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3003 -- If there is an immediately visible homonym of the operator
3004 -- and the declaration has a default, this is worth a warning
3005 -- because the user probably did not intend to get the pre-
3006 -- defined operator, visible in the generic declaration. To
3007 -- find if there is an intended candidate, analyze the renaming
3008 -- again in the current context.
3010 elsif Scope (Old_S) = Standard_Standard
3011 and then Present (Default_Name (Inst_Node))
3014 Decl : constant Node_Id := New_Copy_Tree (N);
3018 Set_Entity (Name (Decl), Empty);
3019 Analyze (Name (Decl));
3021 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
3024 and then In_Open_Scopes (Scope (Hidden))
3025 and then Is_Immediately_Visible (Hidden)
3026 and then Comes_From_Source (Hidden)
3027 and then Hidden /= Old_S
3029 Error_Msg_Sloc := Sloc (Hidden);
3031 ("default subprogram is resolved in the generic "
3032 & "declaration (RM 12.6(17))??", N);
3033 Error_Msg_NE ("\and will not use & #??", N, Hidden);
3042 -- The subprogram renaming declaration may become Ghost if it
3043 -- renames a Ghost entity.
3045 if Is_Entity_Name (Nam) then
3046 Mark_Ghost_Renaming (N, Entity (Nam));
3049 New_S := Analyze_Subprogram_Specification (Spec);
3053 -- Renamed entity must be analyzed first, to avoid being hidden by
3054 -- new name (which might be the same in a generic instance).
3058 -- The subprogram renaming declaration may become Ghost if it renames
3061 if Is_Entity_Name (Nam) then
3062 Mark_Ghost_Renaming (N, Entity (Nam));
3065 -- The renaming defines a new overloaded entity, which is analyzed
3066 -- like a subprogram declaration.
3068 New_S := Analyze_Subprogram_Specification (Spec);
3071 if Current_Scope /= Standard_Standard then
3072 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
3075 -- Set SPARK mode from current context
3077 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
3078 Set_SPARK_Pragma_Inherited (New_S);
3080 Rename_Spec := Find_Corresponding_Spec (N);
3082 -- Case of Renaming_As_Body
3084 if Present (Rename_Spec) then
3085 Check_Previous_Null_Procedure (N, Rename_Spec);
3087 -- Renaming declaration is the completion of the declaration of
3088 -- Rename_Spec. We build an actual body for it at the freezing point.
3090 Set_Corresponding_Spec (N, Rename_Spec);
3092 -- Deal with special case of stream functions of abstract types
3095 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
3096 N_Abstract_Subprogram_Declaration
3098 -- Input stream functions are abstract if the object type is
3099 -- abstract. Similarly, all default stream functions for an
3100 -- interface type are abstract. However, these subprograms may
3101 -- receive explicit declarations in representation clauses, making
3102 -- the attribute subprograms usable as defaults in subsequent
3104 -- In this case we rewrite the declaration to make the subprogram
3105 -- non-abstract. We remove the previous declaration, and insert
3106 -- the new one at the point of the renaming, to prevent premature
3107 -- access to unfrozen types. The new declaration reuses the
3108 -- specification of the previous one, and must not be analyzed.
3111 (Is_Primitive (Entity (Nam))
3113 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
3115 Old_Decl : constant Node_Id :=
3116 Unit_Declaration_Node (Rename_Spec);
3117 New_Decl : constant Node_Id :=
3118 Make_Subprogram_Declaration (Sloc (N),
3120 Relocate_Node (Specification (Old_Decl)));
3123 Insert_After (N, New_Decl);
3124 Set_Is_Abstract_Subprogram (Rename_Spec, False);
3125 Set_Analyzed (New_Decl);
3129 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
3131 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3132 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
3135 Set_Convention (New_S, Convention (Rename_Spec));
3136 Check_Fully_Conformant (New_S, Rename_Spec);
3137 Set_Public_Status (New_S);
3139 if No_Return (Rename_Spec)
3140 and then not No_Return (Entity (Nam))
3143 ("renamed subprogram & must be No_Return", N, Entity (Nam));
3145 ("\since renaming subprogram is No_Return (RM 6.5.1(7/2))", N);
3148 -- The specification does not introduce new formals, but only
3149 -- repeats the formals of the original subprogram declaration.
3150 -- For cross-reference purposes, and for refactoring tools, we
3151 -- treat the formals of the renaming declaration as body formals.
3153 Reference_Body_Formals (Rename_Spec, New_S);
3155 -- Indicate that the entity in the declaration functions like the
3156 -- corresponding body, and is not a new entity. The body will be
3157 -- constructed later at the freeze point, so indicate that the
3158 -- completion has not been seen yet.
3160 Set_Ekind (New_S, E_Subprogram_Body);
3161 New_S := Rename_Spec;
3162 Set_Has_Completion (Rename_Spec, False);
3164 -- Ada 2005: check overriding indicator
3166 if Present (Overridden_Operation (Rename_Spec)) then
3167 if Must_Not_Override (Specification (N)) then
3169 ("subprogram& overrides inherited operation",
3173 and then not Must_Override (Specification (N))
3175 Style.Missing_Overriding (N, Rename_Spec);
3178 elsif Must_Override (Specification (N)) then
3179 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
3182 -- AI12-0132: a renames-as-body freezes the expression of any
3183 -- expression function that it renames.
3185 if Is_Entity_Name (Nam)
3186 and then Is_Expression_Function (Entity (Nam))
3187 and then not Inside_A_Generic
3190 (Def_Id => Entity (Nam),
3191 Typ => Etype (Entity (Nam)),
3194 (Original_Node (Unit_Declaration_Node (Entity (Nam)))),
3198 -- Normal subprogram renaming (not renaming as body)
3201 Generate_Definition (New_S);
3202 New_Overloaded_Entity (New_S);
3204 if not (Is_Entity_Name (Nam)
3205 and then Is_Intrinsic_Subprogram (Entity (Nam)))
3207 Check_Delayed_Subprogram (New_S);
3210 -- Verify that a SPARK renaming does not declare a primitive
3211 -- operation of a tagged type.
3213 Check_SPARK_Primitive_Operation (New_S);
3216 -- There is no need for elaboration checks on the new entity, which may
3217 -- be called before the next freezing point where the body will appear.
3218 -- Elaboration checks refer to the real entity, not the one created by
3219 -- the renaming declaration.
3221 Set_Kill_Elaboration_Checks (New_S, True);
3223 -- If we had a previous error, indicate a completion is present to stop
3224 -- junk cascaded messages, but don't take any further action.
3226 if Etype (Nam) = Any_Type then
3227 Set_Has_Completion (New_S);
3230 -- Case where name has the form of a selected component
3232 elsif Nkind (Nam) = N_Selected_Component then
3234 -- A name which has the form A.B can designate an entry of task A, a
3235 -- protected operation of protected object A, or finally a primitive
3236 -- operation of object A. In the later case, A is an object of some
3237 -- tagged type, or an access type that denotes one such. To further
3238 -- distinguish these cases, note that the scope of a task entry or
3239 -- protected operation is type of the prefix.
3241 -- The prefix could be an overloaded function call that returns both
3242 -- kinds of operations. This overloading pathology is left to the
3243 -- dedicated reader ???
3246 T : constant Entity_Id := Etype (Prefix (Nam));
3254 and then Is_Tagged_Type (Designated_Type (T))))
3255 and then Scope (Entity (Selector_Name (Nam))) /= T
3257 Analyze_Renamed_Primitive_Operation
3258 (N, New_S, Present (Rename_Spec));
3262 -- Renamed entity is an entry or protected operation. For those
3263 -- cases an explicit body is built (at the point of freezing of
3264 -- this entity) that contains a call to the renamed entity.
3266 -- This is not allowed for renaming as body if the renamed
3267 -- spec is already frozen (see RM 8.5.4(5) for details).
3269 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
3271 ("renaming-as-body cannot rename entry as subprogram", N);
3273 ("\since & is already frozen (RM 8.5.4(5))",
3276 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
3283 -- Case where name is an explicit dereference X.all
3285 elsif Nkind (Nam) = N_Explicit_Dereference then
3287 -- Renamed entity is designated by access_to_subprogram expression.
3288 -- Must build body to encapsulate call, as in the entry case.
3290 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
3293 -- Indexed component
3295 elsif Nkind (Nam) = N_Indexed_Component then
3296 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
3299 -- Character literal
3301 elsif Nkind (Nam) = N_Character_Literal then
3302 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
3305 -- Only remaining case is where we have a non-entity name, or a renaming
3306 -- of some other non-overloadable entity.
3308 elsif not Is_Entity_Name (Nam)
3309 or else not Is_Overloadable (Entity (Nam))
3311 -- Do not mention the renaming if it comes from an instance
3313 if not Is_Actual then
3314 Error_Msg_N ("expect valid subprogram name in renaming", N);
3316 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
3322 -- Find the renamed entity that matches the given specification. Disable
3323 -- Ada_83 because there is no requirement of full conformance between
3324 -- renamed entity and new entity, even though the same circuit is used.
3326 -- This is a bit of an odd case, which introduces a really irregular use
3327 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
3330 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
3331 Ada_Version_Pragma := Empty;
3332 Ada_Version_Explicit := Ada_Version;
3335 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3337 -- The visible operation may be an inherited abstract operation that
3338 -- was overridden in the private part, in which case a call will
3339 -- dispatch to the overriding operation. Use the overriding one in
3340 -- the renaming declaration, to prevent spurious errors below.
3342 if Is_Overloadable (Old_S)
3343 and then Is_Abstract_Subprogram (Old_S)
3344 and then No (DTC_Entity (Old_S))
3345 and then Present (Alias (Old_S))
3346 and then not Is_Abstract_Subprogram (Alias (Old_S))
3347 and then Present (Overridden_Operation (Alias (Old_S)))
3349 Old_S := Alias (Old_S);
3352 -- When the renamed subprogram is overloaded and used as an actual
3353 -- of a generic, its entity is set to the first available homonym.
3354 -- We must first disambiguate the name, then set the proper entity.
3356 if Is_Actual and then Is_Overloaded (Nam) then
3357 Set_Entity (Nam, Old_S);
3361 -- Most common case: subprogram renames subprogram. No body is generated
3362 -- in this case, so we must indicate the declaration is complete as is.
3363 -- and inherit various attributes of the renamed subprogram.
3365 if No (Rename_Spec) then
3366 Set_Has_Completion (New_S);
3367 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3368 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3369 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3371 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3372 -- between a subprogram and its correct renaming.
3374 -- Note: the Any_Id check is a guard that prevents compiler crashes
3375 -- when performing a null exclusion check between a renaming and a
3376 -- renamed subprogram that has been found to be illegal.
3378 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3379 Check_Null_Exclusion
3381 Sub => Entity (Nam));
3384 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3385 -- overriding. The flag Requires_Overriding is set very selectively
3386 -- and misses some other illegal cases. The additional conditions
3387 -- checked below are sufficient but not necessary ???
3389 -- The rule does not apply to the renaming generated for an actual
3390 -- subprogram in an instance.
3395 -- Guard against previous errors, and omit renamings of predefined
3398 elsif Ekind (Old_S) not in E_Function | E_Procedure then
3401 elsif Requires_Overriding (Old_S)
3403 (Is_Abstract_Subprogram (Old_S)
3404 and then Present (Find_Dispatching_Type (Old_S))
3405 and then not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3408 ("renamed entity cannot be subprogram that requires overriding "
3409 & "(RM 8.5.4 (5.1))", N);
3413 Prev : constant Entity_Id := Overridden_Operation (New_S);
3417 (Has_Non_Trivial_Precondition (Prev)
3418 or else Has_Non_Trivial_Precondition (Old_S))
3421 ("conflicting inherited classwide preconditions in renaming "
3422 & "of& (RM 6.1.1 (17)", N, Old_S);
3427 if Old_S /= Any_Id then
3428 if Is_Actual and then From_Default (N) then
3430 -- This is an implicit reference to the default actual
3432 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3435 Generate_Reference (Old_S, Nam);
3438 Check_Internal_Protected_Use (N, Old_S);
3440 -- For a renaming-as-body, require subtype conformance, but if the
3441 -- declaration being completed has not been frozen, then inherit the
3442 -- convention of the renamed subprogram prior to checking conformance
3443 -- (unless the renaming has an explicit convention established; the
3444 -- rule stated in the RM doesn't seem to address this ???).
3446 if Present (Rename_Spec) then
3447 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3448 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3450 if not Is_Frozen (Rename_Spec) then
3451 if not Has_Convention_Pragma (Rename_Spec) then
3452 Set_Convention (New_S, Convention (Old_S));
3455 if Ekind (Old_S) /= E_Operator then
3456 Check_Mode_Conformant (New_S, Old_S, Spec);
3459 if Original_Subprogram (Old_S) = Rename_Spec then
3460 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
3462 Check_Formal_Subprogram_Conformance (New_S, Old_S, Spec);
3465 Check_Subtype_Conformant (New_S, Old_S, Spec);
3468 Check_Frozen_Renaming (N, Rename_Spec);
3470 -- Check explicitly that renamed entity is not intrinsic, because
3471 -- in a generic the renamed body is not built. In this case,
3472 -- the renaming_as_body is a completion.
3474 if Inside_A_Generic then
3475 if Is_Frozen (Rename_Spec)
3476 and then Is_Intrinsic_Subprogram (Old_S)
3479 ("subprogram in renaming_as_body cannot be intrinsic",
3483 Set_Has_Completion (Rename_Spec);
3486 elsif Ekind (Old_S) /= E_Operator then
3488 -- If this a defaulted subprogram for a class-wide actual there is
3489 -- no check for mode conformance, given that the signatures don't
3490 -- match (the source mentions T but the actual mentions T'Class).
3495 -- No need for a redundant error message if this is a nested
3496 -- instance, unless the current instantiation (of a child unit)
3497 -- is a compilation unit, which is not analyzed when the parent
3498 -- generic is analyzed.
3501 or else No (Enclosing_Instance)
3502 or else Is_Compilation_Unit (Current_Scope)
3504 Check_Mode_Conformant (New_S, Old_S);
3508 if No (Rename_Spec) then
3510 -- The parameter profile of the new entity is that of the renamed
3511 -- entity: the subtypes given in the specification are irrelevant.
3513 Inherit_Renamed_Profile (New_S, Old_S);
3515 -- A call to the subprogram is transformed into a call to the
3516 -- renamed entity. This is transitive if the renamed entity is
3517 -- itself a renaming.
3519 if Present (Alias (Old_S)) then
3520 Set_Alias (New_S, Alias (Old_S));
3522 Set_Alias (New_S, Old_S);
3525 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3526 -- renaming as body, since the entity in this case is not an
3527 -- intrinsic (it calls an intrinsic, but we have a real body for
3528 -- this call, and it is in this body that the required intrinsic
3529 -- processing will take place).
3531 -- Also, if this is a renaming of inequality, the renamed operator
3532 -- is intrinsic, but what matters is the corresponding equality
3533 -- operator, which may be user-defined.
3535 Set_Is_Intrinsic_Subprogram
3537 Is_Intrinsic_Subprogram (Old_S)
3539 (Chars (Old_S) /= Name_Op_Ne
3540 or else Ekind (Old_S) = E_Operator
3541 or else Is_Intrinsic_Subprogram
3542 (Corresponding_Equality (Old_S))));
3544 if Ekind (Alias (New_S)) = E_Operator then
3545 Set_Has_Delayed_Freeze (New_S, False);
3548 -- If the renaming corresponds to an association for an abstract
3549 -- formal subprogram, then various attributes must be set to
3550 -- indicate that the renaming is an abstract dispatching operation
3551 -- with a controlling type.
3553 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
3555 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3556 -- see it as corresponding to a generic association for a
3557 -- formal abstract subprogram
3559 Set_Is_Abstract_Subprogram (New_S);
3562 New_S_Ctrl_Type : constant Entity_Id :=
3563 Find_Dispatching_Type (New_S);
3564 Old_S_Ctrl_Type : constant Entity_Id :=
3565 Find_Dispatching_Type (Old_S);
3569 -- The actual must match the (instance of the) formal,
3570 -- and must be a controlling type.
3572 if Old_S_Ctrl_Type /= New_S_Ctrl_Type
3573 or else No (New_S_Ctrl_Type)
3575 if No (New_S_Ctrl_Type) then
3577 ("actual must be dispatching subprogram", Nam);
3580 ("actual must be dispatching subprogram for type&",
3581 Nam, New_S_Ctrl_Type);
3585 Set_Is_Dispatching_Operation (New_S);
3586 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3588 -- If the actual in the formal subprogram is itself a
3589 -- formal abstract subprogram association, there's no
3590 -- dispatch table component or position to inherit.
3592 if Present (DTC_Entity (Old_S)) then
3593 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3594 Set_DT_Position_Value (New_S, DT_Position (Old_S));
3604 -- The following is illegal, because F hides whatever other F may
3606 -- function F (...) renames F;
3609 or else (Nkind (Nam) /= N_Expanded_Name
3610 and then Chars (Old_S) = Chars (New_S))
3612 Error_Msg_N ("subprogram cannot rename itself", N);
3614 -- This is illegal even if we use a selector:
3615 -- function F (...) renames Pkg.F;
3616 -- because F is still hidden.
3618 elsif Nkind (Nam) = N_Expanded_Name
3619 and then Entity (Prefix (Nam)) = Current_Scope
3620 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3622 -- This is an error, but we overlook the error and accept the
3623 -- renaming if the special Overriding_Renamings mode is in effect.
3625 if not Overriding_Renamings then
3627 ("implicit operation& is not visible (RM 8.3 (15))",
3632 Set_Convention (New_S, Convention (Old_S));
3634 if Is_Abstract_Subprogram (Old_S) then
3635 if Present (Rename_Spec) then
3637 ("a renaming-as-body cannot rename an abstract subprogram",
3639 Set_Has_Completion (Rename_Spec);
3641 Set_Is_Abstract_Subprogram (New_S);
3645 Check_Library_Unit_Renaming (N, Old_S);
3647 -- Pathological case: procedure renames entry in the scope of its
3648 -- task. Entry is given by simple name, but body must be built for
3649 -- procedure. Of course if called it will deadlock.
3651 if Ekind (Old_S) = E_Entry then
3652 Set_Has_Completion (New_S, False);
3653 Set_Alias (New_S, Empty);
3656 -- Do not freeze the renaming nor the renamed entity when the context
3657 -- is an enclosing generic. Freezing is an expansion activity, and in
3658 -- addition the renamed entity may depend on the generic formals of
3659 -- the enclosing generic.
3661 if Is_Actual and not Inside_A_Generic then
3662 Freeze_Before (N, Old_S);
3663 Freeze_Actual_Profile;
3664 Set_Has_Delayed_Freeze (New_S, False);
3665 Freeze_Before (N, New_S);
3667 -- An abstract subprogram is only allowed as an actual in the case
3668 -- where the formal subprogram is also abstract.
3670 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
3671 and then Is_Abstract_Subprogram (Old_S)
3672 and then not Is_Abstract_Subprogram (Formal_Spec)
3675 ("abstract subprogram not allowed as generic actual", Nam);
3680 -- A common error is to assume that implicit operators for types are
3681 -- defined in Standard, or in the scope of a subtype. In those cases
3682 -- where the renamed entity is given with an expanded name, it is
3683 -- worth mentioning that operators for the type are not declared in
3684 -- the scope given by the prefix.
3686 if Nkind (Nam) = N_Expanded_Name
3687 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
3688 and then Scope (Entity (Nam)) = Standard_Standard
3691 T : constant Entity_Id :=
3692 Base_Type (Etype (First_Formal (New_S)));
3694 Error_Msg_Node_2 := Prefix (Nam);
3696 ("operator for type& is not declared in&", Prefix (Nam), T);
3701 ("no visible subprogram matches the specification for&",
3705 if Present (Candidate_Renaming) then
3712 F1 := First_Formal (Candidate_Renaming);
3713 F2 := First_Formal (New_S);
3714 T1 := First_Subtype (Etype (F1));
3715 while Present (F1) and then Present (F2) loop
3720 if Present (F1) and then Present (Default_Value (F1)) then
3721 if Present (Next_Formal (F1)) then
3723 ("\missing specification for & and other formals with "
3724 & "defaults", Spec, F1);
3726 Error_Msg_NE ("\missing specification for &", Spec, F1);
3730 if Nkind (Nam) = N_Operator_Symbol
3731 and then From_Default (N)
3733 Error_Msg_Node_2 := T1;
3735 ("default & on & is not directly visible", Nam, Nam);
3741 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
3742 -- controlling access parameters are known non-null for the renamed
3743 -- subprogram. Test also applies to a subprogram instantiation that
3744 -- is dispatching. Test is skipped if some previous error was detected
3745 -- that set Old_S to Any_Id.
3747 if Ada_Version >= Ada_2005
3748 and then Old_S /= Any_Id
3749 and then not Is_Dispatching_Operation (Old_S)
3750 and then Is_Dispatching_Operation (New_S)
3757 Old_F := First_Formal (Old_S);
3758 New_F := First_Formal (New_S);
3759 while Present (Old_F) loop
3760 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
3761 and then Is_Controlling_Formal (New_F)
3762 and then not Can_Never_Be_Null (Old_F)
3764 Error_Msg_N ("access parameter is controlling,", New_F);
3766 ("\corresponding parameter of& must be explicitly null "
3767 & "excluding", New_F, Old_S);
3770 Next_Formal (Old_F);
3771 Next_Formal (New_F);
3776 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
3777 -- is to warn if an operator is being renamed as a different operator.
3778 -- If the operator is predefined, examine the kind of the entity, not
3779 -- the abbreviated declaration in Standard.
3781 if Comes_From_Source (N)
3782 and then Present (Old_S)
3783 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3784 or else Ekind (Old_S) = E_Operator)
3785 and then Nkind (New_S) = N_Defining_Operator_Symbol
3786 and then Chars (Old_S) /= Chars (New_S)
3789 ("& is being renamed as a different operator??", N, Old_S);
3792 -- Check for renaming of obsolescent subprogram
3794 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3796 -- Another warning or some utility: if the new subprogram as the same
3797 -- name as the old one, the old one is not hidden by an outer homograph,
3798 -- the new one is not a public symbol, and the old one is otherwise
3799 -- directly visible, the renaming is superfluous.
3801 if Chars (Old_S) = Chars (New_S)
3802 and then Comes_From_Source (N)
3803 and then Scope (Old_S) /= Standard_Standard
3804 and then Warn_On_Redundant_Constructs
3805 and then (Is_Immediately_Visible (Old_S)
3806 or else Is_Potentially_Use_Visible (Old_S))
3807 and then Is_Overloadable (Current_Scope)
3808 and then Chars (Current_Scope) /= Chars (Old_S)
3811 ("redundant renaming, entity is directly visible?r?", Name (N));
3814 -- Implementation-defined aspect specifications can appear in a renaming
3815 -- declaration, but not language-defined ones. The call to procedure
3816 -- Analyze_Aspect_Specifications will take care of this error check.
3818 if Has_Aspects (N) then
3819 Analyze_Aspect_Specifications (N, New_S);
3825 and then Has_Yield_Aspect (Formal_Spec)
3826 and then not Has_Yield_Aspect (Old_S)
3828 Error_Msg_Name_1 := Name_Yield;
3830 ("actual subprogram& must have aspect% to match formal", Name (N));
3833 Ada_Version := Save_AV;
3834 Ada_Version_Pragma := Save_AVP;
3835 Ada_Version_Explicit := Save_AV_Exp;
3837 -- In GNATprove mode, the renamings of actual subprograms are replaced
3838 -- with wrapper functions that make it easier to propagate axioms to the
3839 -- points of call within an instance. Wrappers are generated if formal
3840 -- subprogram is subject to axiomatization.
3842 -- The types in the wrapper profiles are obtained from (instances of)
3843 -- the types of the formal subprogram.
3846 and then GNATprove_Mode
3847 and then Present (Containing_Package_With_Ext_Axioms (Formal_Spec))
3848 and then not Inside_A_Generic
3850 if Ekind (Old_S) = E_Function then
3851 Rewrite (N, Build_Function_Wrapper (Formal_Spec, Old_S));
3854 elsif Ekind (Old_S) = E_Operator then
3855 Rewrite (N, Build_Operator_Wrapper (Formal_Spec, Old_S));
3860 -- Check if we are looking at an Ada 2012 defaulted formal subprogram
3861 -- and mark any use_package_clauses that affect the visibility of the
3862 -- implicit generic actual.
3864 -- Also, we may be looking at an internal renaming of a user-defined
3865 -- subprogram created for a generic formal subprogram association,
3866 -- which will also have to be marked here. This can occur when the
3867 -- corresponding formal subprogram contains references to other generic
3870 if Is_Generic_Actual_Subprogram (New_S)
3871 and then (Is_Intrinsic_Subprogram (New_S)
3872 or else From_Default (N)
3873 or else Nkind (N) = N_Subprogram_Renaming_Declaration)
3875 Mark_Use_Clauses (New_S);
3877 -- Handle overloaded subprograms
3879 if Present (Alias (New_S)) then
3880 Mark_Use_Clauses (Alias (New_S));
3883 end Analyze_Subprogram_Renaming;
3885 -------------------------
3886 -- Analyze_Use_Package --
3887 -------------------------
3889 -- Resolve the package names in the use clause, and make all the visible
3890 -- entities defined in the package potentially use-visible. If the package
3891 -- is already in use from a previous use clause, its visible entities are
3892 -- already use-visible. In that case, mark the occurrence as a redundant
3893 -- use. If the package is an open scope, i.e. if the use clause occurs
3894 -- within the package itself, ignore it.
3896 procedure Analyze_Use_Package (N : Node_Id; Chain : Boolean := True) is
3897 procedure Analyze_Package_Name (Clause : Node_Id);
3898 -- Perform analysis on a package name from a use_package_clause
3900 procedure Analyze_Package_Name_List (Head_Clause : Node_Id);
3901 -- Similar to Analyze_Package_Name but iterates over all the names
3904 --------------------------
3905 -- Analyze_Package_Name --
3906 --------------------------
3908 procedure Analyze_Package_Name (Clause : Node_Id) is
3909 Pack : constant Node_Id := Name (Clause);
3913 pragma Assert (Nkind (Clause) = N_Use_Package_Clause);
3916 -- Verify that the package standard is not directly named in a
3917 -- use_package_clause.
3919 if Nkind (Parent (Clause)) = N_Compilation_Unit
3920 and then Nkind (Pack) = N_Expanded_Name
3922 Pref := Prefix (Pack);
3924 while Nkind (Pref) = N_Expanded_Name loop
3925 Pref := Prefix (Pref);
3928 if Entity (Pref) = Standard_Standard then
3930 ("predefined package Standard cannot appear in a context "
3934 end Analyze_Package_Name;
3936 -------------------------------
3937 -- Analyze_Package_Name_List --
3938 -------------------------------
3940 procedure Analyze_Package_Name_List (Head_Clause : Node_Id) is
3944 -- Due to the way source use clauses are split during parsing we are
3945 -- forced to simply iterate through all entities in scope until the
3946 -- clause representing the last name in the list is found.
3948 Curr := Head_Clause;
3949 while Present (Curr) loop
3950 Analyze_Package_Name (Curr);
3952 -- Stop iterating over the names in the use clause when we are at
3955 exit when not More_Ids (Curr) and then Prev_Ids (Curr);
3958 end Analyze_Package_Name_List;
3964 -- Start of processing for Analyze_Use_Package
3967 Set_Hidden_By_Use_Clause (N, No_Elist);
3969 -- Use clause not allowed in a spec of a predefined package declaration
3970 -- except that packages whose file name starts a-n are OK (these are
3971 -- children of Ada.Numerics, which are never loaded by Rtsfind).
3973 if Is_Predefined_Unit (Current_Sem_Unit)
3974 and then Get_Name_String
3975 (Unit_File_Name (Current_Sem_Unit)) (1 .. 3) /= "a-n"
3976 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
3977 N_Package_Declaration
3979 Error_Msg_N ("use clause not allowed in predefined spec", N);
3982 -- Loop through all package names from the original use clause in
3983 -- order to analyze referenced packages. A use_package_clause with only
3984 -- one name does not have More_Ids or Prev_Ids set, while a clause with
3985 -- More_Ids only starts the chain produced by the parser.
3987 if not More_Ids (N) and then not Prev_Ids (N) then
3988 Analyze_Package_Name (N);
3990 elsif More_Ids (N) and then not Prev_Ids (N) then
3991 Analyze_Package_Name_List (N);
3994 if not Is_Entity_Name (Name (N)) then
3995 Error_Msg_N ("& is not a package", Name (N));
4001 Chain_Use_Clause (N);
4004 Pack := Entity (Name (N));
4006 -- There are many cases where scopes are manipulated during analysis, so
4007 -- check that Pack's current use clause has not already been chained
4008 -- before setting its previous use clause.
4010 if Ekind (Pack) = E_Package
4011 and then Present (Current_Use_Clause (Pack))
4012 and then Current_Use_Clause (Pack) /= N
4013 and then No (Prev_Use_Clause (N))
4014 and then Prev_Use_Clause (Current_Use_Clause (Pack)) /= N
4016 Set_Prev_Use_Clause (N, Current_Use_Clause (Pack));
4019 -- Mark all entities as potentially use visible
4021 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
4022 if Ekind (Pack) = E_Generic_Package then
4023 Error_Msg_N -- CODEFIX
4024 ("a generic package is not allowed in a use clause", Name (N));
4026 elsif Is_Generic_Subprogram (Pack) then
4027 Error_Msg_N -- CODEFIX
4028 ("a generic subprogram is not allowed in a use clause",
4031 elsif Is_Subprogram (Pack) then
4032 Error_Msg_N -- CODEFIX
4033 ("a subprogram is not allowed in a use clause", Name (N));
4036 Error_Msg_N ("& is not allowed in a use clause", Name (N));
4040 if Nkind (Parent (N)) = N_Compilation_Unit then
4041 Check_In_Previous_With_Clause (N, Name (N));
4044 Use_One_Package (N, Name (N));
4047 Mark_Ghost_Clause (N);
4048 end Analyze_Use_Package;
4050 ----------------------
4051 -- Analyze_Use_Type --
4052 ----------------------
4054 procedure Analyze_Use_Type (N : Node_Id; Chain : Boolean := True) is
4059 Set_Hidden_By_Use_Clause (N, No_Elist);
4061 -- Chain clause to list of use clauses in current scope when flagged
4064 Chain_Use_Clause (N);
4067 -- Obtain the base type of the type denoted within the use_type_clause's
4070 Id := Subtype_Mark (N);
4072 E := Base_Type (Entity (Id));
4074 -- There are many cases where a use_type_clause may be reanalyzed due to
4075 -- manipulation of the scope stack so we much guard against those cases
4076 -- here, otherwise, we must add the new use_type_clause to the previous
4077 -- use_type_clause chain in order to mark redundant use_type_clauses as
4078 -- used. When the redundant use-type clauses appear in a parent unit and
4079 -- a child unit we must prevent a circularity in the chain that would
4080 -- otherwise result from the separate steps of analysis and installation
4081 -- of the parent context.
4083 if Present (Current_Use_Clause (E))
4084 and then Current_Use_Clause (E) /= N
4085 and then Prev_Use_Clause (Current_Use_Clause (E)) /= N
4086 and then No (Prev_Use_Clause (N))
4088 Set_Prev_Use_Clause (N, Current_Use_Clause (E));
4091 -- If the Used_Operations list is already initialized, the clause has
4092 -- been analyzed previously, and it is being reinstalled, for example
4093 -- when the clause appears in a package spec and we are compiling the
4094 -- corresponding package body. In that case, make the entities on the
4095 -- existing list use_visible, and mark the corresponding types In_Use.
4097 if Present (Used_Operations (N)) then
4102 Use_One_Type (Subtype_Mark (N), Installed => True);
4104 Elmt := First_Elmt (Used_Operations (N));
4105 while Present (Elmt) loop
4106 Set_Is_Potentially_Use_Visible (Node (Elmt));
4114 -- Otherwise, create new list and attach to it the operations that are
4115 -- made use-visible by the clause.
4117 Set_Used_Operations (N, New_Elmt_List);
4120 if E /= Any_Type then
4123 if Nkind (Parent (N)) = N_Compilation_Unit then
4124 if Nkind (Id) = N_Identifier then
4125 Error_Msg_N ("type is not directly visible", Id);
4127 elsif Is_Child_Unit (Scope (E))
4128 and then Scope (E) /= System_Aux_Id
4130 Check_In_Previous_With_Clause (N, Prefix (Id));
4135 -- If the use_type_clause appears in a compilation unit context,
4136 -- check whether it comes from a unit that may appear in a
4137 -- limited_with_clause, for a better error message.
4139 if Nkind (Parent (N)) = N_Compilation_Unit
4140 and then Nkind (Id) /= N_Identifier
4146 function Mentioned (Nam : Node_Id) return Boolean;
4147 -- Check whether the prefix of expanded name for the type
4148 -- appears in the prefix of some limited_with_clause.
4154 function Mentioned (Nam : Node_Id) return Boolean is
4156 return Nkind (Name (Item)) = N_Selected_Component
4157 and then Chars (Prefix (Name (Item))) = Chars (Nam);
4161 Pref := Prefix (Id);
4162 Item := First (Context_Items (Parent (N)));
4163 while Present (Item) and then Item /= N loop
4164 if Nkind (Item) = N_With_Clause
4165 and then Limited_Present (Item)
4166 and then Mentioned (Pref)
4169 (Get_Msg_Id, "premature usage of incomplete type");
4178 Mark_Ghost_Clause (N);
4179 end Analyze_Use_Type;
4181 ------------------------
4182 -- Attribute_Renaming --
4183 ------------------------
4185 procedure Attribute_Renaming (N : Node_Id) is
4186 Loc : constant Source_Ptr := Sloc (N);
4187 Nam : constant Node_Id := Name (N);
4188 Spec : constant Node_Id := Specification (N);
4189 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
4190 Aname : constant Name_Id := Attribute_Name (Nam);
4192 Form_Num : Nat := 0;
4193 Expr_List : List_Id := No_List;
4195 Attr_Node : Node_Id;
4196 Body_Node : Node_Id;
4197 Param_Spec : Node_Id;
4200 Generate_Definition (New_S);
4202 -- This procedure is called in the context of subprogram renaming, and
4203 -- thus the attribute must be one that is a subprogram. All of those
4204 -- have at least one formal parameter, with the exceptions of the GNAT
4205 -- attribute 'Img, which GNAT treats as renameable.
4207 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
4208 if Aname /= Name_Img then
4210 ("subprogram renaming an attribute must have formals", N);
4215 Param_Spec := First (Parameter_Specifications (Spec));
4216 while Present (Param_Spec) loop
4217 Form_Num := Form_Num + 1;
4219 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4220 Find_Type (Parameter_Type (Param_Spec));
4222 -- The profile of the new entity denotes the base type (s) of
4223 -- the types given in the specification. For access parameters
4224 -- there are no subtypes involved.
4226 Rewrite (Parameter_Type (Param_Spec),
4228 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
4231 if No (Expr_List) then
4232 Expr_List := New_List;
4235 Append_To (Expr_List,
4236 Make_Identifier (Loc,
4237 Chars => Chars (Defining_Identifier (Param_Spec))));
4239 -- The expressions in the attribute reference are not freeze
4240 -- points. Neither is the attribute as a whole, see below.
4242 Set_Must_Not_Freeze (Last (Expr_List));
4247 -- Immediate error if too many formals. Other mismatches in number or
4248 -- types of parameters are detected when we analyze the body of the
4249 -- subprogram that we construct.
4251 if Form_Num > 2 then
4252 Error_Msg_N ("too many formals for attribute", N);
4254 -- Error if the attribute reference has expressions that look like
4255 -- formal parameters.
4257 elsif Present (Expressions (Nam)) then
4258 Error_Msg_N ("illegal expressions in attribute reference", Nam);
4260 elsif Aname in Name_Compose | Name_Exponent | Name_Leading_Part |
4261 Name_Pos | Name_Round | Name_Scaling |
4264 if Nkind (N) = N_Subprogram_Renaming_Declaration
4265 and then Present (Corresponding_Formal_Spec (N))
4268 ("generic actual cannot be attribute involving universal type",
4272 ("attribute involving a universal type cannot be renamed",
4277 -- Rewrite attribute node to have a list of expressions corresponding to
4278 -- the subprogram formals. A renaming declaration is not a freeze point,
4279 -- and the analysis of the attribute reference should not freeze the
4280 -- type of the prefix. We use the original node in the renaming so that
4281 -- its source location is preserved, and checks on stream attributes are
4282 -- properly applied.
4284 Attr_Node := Relocate_Node (Nam);
4285 Set_Expressions (Attr_Node, Expr_List);
4287 Set_Must_Not_Freeze (Attr_Node);
4288 Set_Must_Not_Freeze (Prefix (Nam));
4290 -- Case of renaming a function
4292 if Nkind (Spec) = N_Function_Specification then
4293 if Is_Procedure_Attribute_Name (Aname) then
4294 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
4298 Find_Type (Result_Definition (Spec));
4299 Rewrite (Result_Definition (Spec),
4301 (Base_Type (Entity (Result_Definition (Spec))), Loc));
4304 Make_Subprogram_Body (Loc,
4305 Specification => Spec,
4306 Declarations => New_List,
4307 Handled_Statement_Sequence =>
4308 Make_Handled_Sequence_Of_Statements (Loc,
4309 Statements => New_List (
4310 Make_Simple_Return_Statement (Loc,
4311 Expression => Attr_Node))));
4313 -- Case of renaming a procedure
4316 if not Is_Procedure_Attribute_Name (Aname) then
4317 Error_Msg_N ("attribute can only be renamed as function", Nam);
4322 Make_Subprogram_Body (Loc,
4323 Specification => Spec,
4324 Declarations => New_List,
4325 Handled_Statement_Sequence =>
4326 Make_Handled_Sequence_Of_Statements (Loc,
4327 Statements => New_List (Attr_Node)));
4330 -- Signal the ABE mechanism that the generated subprogram body has not
4331 -- ABE ramifications.
4333 Set_Was_Attribute_Reference (Body_Node);
4335 -- In case of tagged types we add the body of the generated function to
4336 -- the freezing actions of the type (because in the general case such
4337 -- type is still not frozen). We exclude from this processing generic
4338 -- formal subprograms found in instantiations.
4340 -- We must exclude restricted run-time libraries because
4341 -- entity AST_Handler is defined in package System.Aux_Dec which is not
4342 -- available in those platforms. Note that we cannot use the function
4343 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
4344 -- the ZFP run-time library is not defined as a profile, and we do not
4345 -- want to deal with AST_Handler in ZFP mode.
4347 if not Configurable_Run_Time_Mode
4348 and then not Present (Corresponding_Formal_Spec (N))
4349 and then Etype (Nam) /= RTE (RE_AST_Handler)
4352 P : constant Node_Id := Prefix (Nam);
4355 -- The prefix of 'Img is an object that is evaluated for each call
4356 -- of the function that renames it.
4358 if Aname = Name_Img then
4359 Preanalyze_And_Resolve (P);
4361 -- For all other attribute renamings, the prefix is a subtype
4367 -- If the target type is not yet frozen, add the body to the
4368 -- actions to be elaborated at freeze time.
4370 if Is_Tagged_Type (Etype (P))
4371 and then In_Open_Scopes (Scope (Etype (P)))
4373 Ensure_Freeze_Node (Etype (P));
4374 Append_Freeze_Action (Etype (P), Body_Node);
4376 Rewrite (N, Body_Node);
4378 Set_Etype (New_S, Base_Type (Etype (New_S)));
4382 -- Generic formal subprograms or AST_Handler renaming
4385 Rewrite (N, Body_Node);
4387 Set_Etype (New_S, Base_Type (Etype (New_S)));
4390 if Is_Compilation_Unit (New_S) then
4392 ("a library unit can only rename another library unit", N);
4395 -- We suppress elaboration warnings for the resulting entity, since
4396 -- clearly they are not needed, and more particularly, in the case
4397 -- of a generic formal subprogram, the resulting entity can appear
4398 -- after the instantiation itself, and thus look like a bogus case
4399 -- of access before elaboration.
4401 if Legacy_Elaboration_Checks then
4402 Set_Suppress_Elaboration_Warnings (New_S);
4404 end Attribute_Renaming;
4406 ----------------------
4407 -- Chain_Use_Clause --
4408 ----------------------
4410 procedure Chain_Use_Clause (N : Node_Id) is
4411 Level : Int := Scope_Stack.Last;
4417 if not Is_Compilation_Unit (Current_Scope)
4418 or else not Is_Child_Unit (Current_Scope)
4422 -- Common case for compilation unit
4424 elsif Defining_Entity (Parent (N)) = Current_Scope then
4428 -- If declaration appears in some other scope, it must be in some
4429 -- parent unit when compiling a child.
4431 Pack := Defining_Entity (Parent (N));
4433 if not In_Open_Scopes (Pack) then
4436 -- If the use clause appears in an ancestor and we are in the
4437 -- private part of the immediate parent, the use clauses are
4438 -- already installed.
4440 elsif Pack /= Scope (Current_Scope)
4441 and then In_Private_Part (Scope (Current_Scope))
4446 -- Find entry for parent unit in scope stack
4448 while Scope_Stack.Table (Level).Entity /= Pack loop
4454 Set_Next_Use_Clause (N,
4455 Scope_Stack.Table (Level).First_Use_Clause);
4456 Scope_Stack.Table (Level).First_Use_Clause := N;
4457 end Chain_Use_Clause;
4459 ---------------------------
4460 -- Check_Frozen_Renaming --
4461 ---------------------------
4463 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
4468 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
4471 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
4473 if Is_Entity_Name (Name (N)) then
4474 Old_S := Entity (Name (N));
4476 if not Is_Frozen (Old_S)
4477 and then Operating_Mode /= Check_Semantics
4479 Append_Freeze_Action (Old_S, B_Node);
4481 Insert_After (N, B_Node);
4485 if Is_Intrinsic_Subprogram (Old_S)
4486 and then not In_Instance
4487 and then not Relaxed_RM_Semantics
4490 ("subprogram used in renaming_as_body cannot be intrinsic",
4495 Insert_After (N, B_Node);
4499 end Check_Frozen_Renaming;
4501 -------------------------------
4502 -- Set_Entity_Or_Discriminal --
4503 -------------------------------
4505 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4509 -- If the entity is not a discriminant, or else expansion is disabled,
4510 -- simply set the entity.
4512 if not In_Spec_Expression
4513 or else Ekind (E) /= E_Discriminant
4514 or else Inside_A_Generic
4516 Set_Entity_With_Checks (N, E);
4518 -- The replacement of a discriminant by the corresponding discriminal
4519 -- is not done for a task discriminant that appears in a default
4520 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4521 -- for details on their handling.
4523 elsif Is_Concurrent_Type (Scope (E)) then
4526 and then Nkind (P) not in
4527 N_Parameter_Specification | N_Component_Declaration
4533 and then Nkind (P) = N_Parameter_Specification
4538 Set_Entity (N, Discriminal (E));
4541 -- Otherwise, this is a discriminant in a context in which
4542 -- it is a reference to the corresponding parameter of the
4543 -- init proc for the enclosing type.
4546 Set_Entity (N, Discriminal (E));
4548 end Set_Entity_Or_Discriminal;
4550 -----------------------------------
4551 -- Check_In_Previous_With_Clause --
4552 -----------------------------------
4554 procedure Check_In_Previous_With_Clause
4558 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4563 Item := First (Context_Items (Parent (N)));
4564 while Present (Item) and then Item /= N loop
4565 if Nkind (Item) = N_With_Clause
4567 -- Protect the frontend against previous critical errors
4569 and then Nkind (Name (Item)) /= N_Selected_Component
4570 and then Entity (Name (Item)) = Pack
4574 -- Find root library unit in with_clause
4576 while Nkind (Par) = N_Expanded_Name loop
4577 Par := Prefix (Par);
4580 if Is_Child_Unit (Entity (Original_Node (Par))) then
4581 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4590 -- On exit, package is not mentioned in a previous with_clause.
4591 -- Check if its prefix is.
4593 if Nkind (Nam) = N_Expanded_Name then
4594 Check_In_Previous_With_Clause (N, Prefix (Nam));
4596 elsif Pack /= Any_Id then
4597 Error_Msg_NE ("& is not visible", Nam, Pack);
4599 end Check_In_Previous_With_Clause;
4601 ---------------------------------
4602 -- Check_Library_Unit_Renaming --
4603 ---------------------------------
4605 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4609 if Nkind (Parent (N)) /= N_Compilation_Unit then
4612 -- Check for library unit. Note that we used to check for the scope
4613 -- being Standard here, but that was wrong for Standard itself.
4615 elsif not Is_Compilation_Unit (Old_E)
4616 and then not Is_Child_Unit (Old_E)
4618 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4620 -- Entities defined in Standard (operators and boolean literals) cannot
4621 -- be renamed as library units.
4623 elsif Scope (Old_E) = Standard_Standard
4624 and then Sloc (Old_E) = Standard_Location
4626 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4628 elsif Present (Parent_Spec (N))
4629 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
4630 and then not Is_Child_Unit (Old_E)
4633 ("renamed unit must be a child unit of generic parent", Name (N));
4635 elsif Nkind (N) in N_Generic_Renaming_Declaration
4636 and then Nkind (Name (N)) = N_Expanded_Name
4637 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
4638 and then Is_Generic_Unit (Old_E)
4641 ("renamed generic unit must be a library unit", Name (N));
4643 elsif Is_Package_Or_Generic_Package (Old_E) then
4645 -- Inherit categorization flags
4647 New_E := Defining_Entity (N);
4648 Set_Is_Pure (New_E, Is_Pure (Old_E));
4649 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
4650 Set_Is_Remote_Call_Interface (New_E,
4651 Is_Remote_Call_Interface (Old_E));
4652 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
4653 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
4655 end Check_Library_Unit_Renaming;
4657 ------------------------
4658 -- Enclosing_Instance --
4659 ------------------------
4661 function Enclosing_Instance return Entity_Id is
4665 if not Is_Generic_Instance (Current_Scope) then
4669 S := Scope (Current_Scope);
4670 while S /= Standard_Standard loop
4671 if Is_Generic_Instance (S) then
4679 end Enclosing_Instance;
4685 procedure End_Scope is
4691 Id := First_Entity (Current_Scope);
4692 while Present (Id) loop
4693 -- An entity in the current scope is not necessarily the first one
4694 -- on its homonym chain. Find its predecessor if any,
4695 -- If it is an internal entity, it will not be in the visibility
4696 -- chain altogether, and there is nothing to unchain.
4698 if Id /= Current_Entity (Id) then
4699 Prev := Current_Entity (Id);
4700 while Present (Prev)
4701 and then Present (Homonym (Prev))
4702 and then Homonym (Prev) /= Id
4704 Prev := Homonym (Prev);
4707 -- Skip to end of loop if Id is not in the visibility chain
4709 if No (Prev) or else Homonym (Prev) /= Id then
4717 Set_Is_Immediately_Visible (Id, False);
4719 Outer := Homonym (Id);
4720 while Present (Outer) and then Scope (Outer) = Current_Scope loop
4721 Outer := Homonym (Outer);
4724 -- Reset homonym link of other entities, but do not modify link
4725 -- between entities in current scope, so that the back-end can have
4726 -- a proper count of local overloadings.
4729 Set_Name_Entity_Id (Chars (Id), Outer);
4731 elsif Scope (Prev) /= Scope (Id) then
4732 Set_Homonym (Prev, Outer);
4739 -- If the scope generated freeze actions, place them before the
4740 -- current declaration and analyze them. Type declarations and
4741 -- the bodies of initialization procedures can generate such nodes.
4742 -- We follow the parent chain until we reach a list node, which is
4743 -- the enclosing list of declarations. If the list appears within
4744 -- a protected definition, move freeze nodes outside the protected
4748 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
4752 L : constant List_Id := Scope_Stack.Table
4753 (Scope_Stack.Last).Pending_Freeze_Actions;
4756 if Is_Itype (Current_Scope) then
4757 Decl := Associated_Node_For_Itype (Current_Scope);
4759 Decl := Parent (Current_Scope);
4764 while not (Is_List_Member (Decl))
4765 or else Nkind (Parent (Decl)) in N_Protected_Definition
4768 Decl := Parent (Decl);
4771 Insert_List_Before_And_Analyze (Decl, L);
4779 ---------------------
4780 -- End_Use_Clauses --
4781 ---------------------
4783 procedure End_Use_Clauses (Clause : Node_Id) is
4787 -- Remove use_type_clauses first, because they affect the visibility of
4788 -- operators in subsequent used packages.
4791 while Present (U) loop
4792 if Nkind (U) = N_Use_Type_Clause then
4796 Next_Use_Clause (U);
4800 while Present (U) loop
4801 if Nkind (U) = N_Use_Package_Clause then
4802 End_Use_Package (U);
4805 Next_Use_Clause (U);
4807 end End_Use_Clauses;
4809 ---------------------
4810 -- End_Use_Package --
4811 ---------------------
4813 procedure End_Use_Package (N : Node_Id) is
4815 Pack_Name : Node_Id;
4819 function Is_Primitive_Operator_In_Use
4821 F : Entity_Id) return Boolean;
4822 -- Check whether Op is a primitive operator of a use-visible type
4824 ----------------------------------
4825 -- Is_Primitive_Operator_In_Use --
4826 ----------------------------------
4828 function Is_Primitive_Operator_In_Use
4830 F : Entity_Id) return Boolean
4832 T : constant Entity_Id := Base_Type (Etype (F));
4834 return In_Use (T) and then Scope (T) = Scope (Op);
4835 end Is_Primitive_Operator_In_Use;
4837 -- Start of processing for End_Use_Package
4840 Pack_Name := Name (N);
4842 -- Test that Pack_Name actually denotes a package before processing
4844 if Is_Entity_Name (Pack_Name)
4845 and then Ekind (Entity (Pack_Name)) = E_Package
4847 Pack := Entity (Pack_Name);
4849 if In_Open_Scopes (Pack) then
4852 elsif not Redundant_Use (Pack_Name) then
4853 Set_In_Use (Pack, False);
4854 Set_Current_Use_Clause (Pack, Empty);
4856 Id := First_Entity (Pack);
4857 while Present (Id) loop
4859 -- Preserve use-visibility of operators that are primitive
4860 -- operators of a type that is use-visible through an active
4863 if Nkind (Id) = N_Defining_Operator_Symbol
4865 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
4867 (Present (Next_Formal (First_Formal (Id)))
4869 Is_Primitive_Operator_In_Use
4870 (Id, Next_Formal (First_Formal (Id)))))
4874 Set_Is_Potentially_Use_Visible (Id, False);
4877 if Is_Private_Type (Id)
4878 and then Present (Full_View (Id))
4880 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4886 if Present (Renamed_Object (Pack)) then
4887 Set_In_Use (Renamed_Object (Pack), False);
4888 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
4891 if Chars (Pack) = Name_System
4892 and then Scope (Pack) = Standard_Standard
4893 and then Present_System_Aux
4895 Id := First_Entity (System_Aux_Id);
4896 while Present (Id) loop
4897 Set_Is_Potentially_Use_Visible (Id, False);
4899 if Is_Private_Type (Id)
4900 and then Present (Full_View (Id))
4902 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4908 Set_In_Use (System_Aux_Id, False);
4911 Set_Redundant_Use (Pack_Name, False);
4915 if Present (Hidden_By_Use_Clause (N)) then
4916 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
4917 while Present (Elmt) loop
4919 E : constant Entity_Id := Node (Elmt);
4922 -- Reset either Use_Visibility or Direct_Visibility, depending
4923 -- on how the entity was hidden by the use clause.
4925 if In_Use (Scope (E))
4926 and then Used_As_Generic_Actual (Scope (E))
4928 Set_Is_Potentially_Use_Visible (Node (Elmt));
4930 Set_Is_Immediately_Visible (Node (Elmt));
4937 Set_Hidden_By_Use_Clause (N, No_Elist);
4939 end End_Use_Package;
4945 procedure End_Use_Type (N : Node_Id) is
4950 -- Start of processing for End_Use_Type
4953 Id := Subtype_Mark (N);
4955 -- A call to Rtsfind may occur while analyzing a use_type_clause, in
4956 -- which case the type marks are not resolved yet, so guard against that
4959 if Is_Entity_Name (Id) and then Present (Entity (Id)) then
4962 if T = Any_Type or else From_Limited_With (T) then
4965 -- Note that the use_type_clause may mention a subtype of the type
4966 -- whose primitive operations have been made visible. Here as
4967 -- elsewhere, it is the base type that matters for visibility.
4969 elsif In_Open_Scopes (Scope (Base_Type (T))) then
4972 elsif not Redundant_Use (Id) then
4973 Set_In_Use (T, False);
4974 Set_In_Use (Base_Type (T), False);
4975 Set_Current_Use_Clause (T, Empty);
4976 Set_Current_Use_Clause (Base_Type (T), Empty);
4978 -- See Use_One_Type for the rationale. This is a bit on the naive
4979 -- side, but should be good enough in practice.
4981 if Is_Tagged_Type (T) then
4982 Set_In_Use (Class_Wide_Type (T), False);
4987 if Is_Empty_Elmt_List (Used_Operations (N)) then
4991 Elmt := First_Elmt (Used_Operations (N));
4992 while Present (Elmt) loop
4993 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
4999 --------------------
5000 -- Entity_Of_Unit --
5001 --------------------
5003 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
5005 if Nkind (U) = N_Package_Instantiation and then Analyzed (U) then
5006 return Defining_Entity (Instance_Spec (U));
5008 return Defining_Entity (U);
5012 ----------------------
5013 -- Find_Direct_Name --
5014 ----------------------
5016 procedure Find_Direct_Name
5018 Errors_OK : Boolean := True;
5019 Marker_OK : Boolean := True;
5020 Reference_OK : Boolean := True)
5026 Homonyms : Entity_Id;
5027 -- Saves start of homonym chain
5029 Inst : Entity_Id := Empty;
5030 -- Enclosing instance, if any
5032 Nvis_Entity : Boolean;
5033 -- Set True to indicate that there is at least one entity on the homonym
5034 -- chain which, while not visible, is visible enough from the user point
5035 -- of view to warrant an error message of "not visible" rather than
5038 Nvis_Is_Private_Subprg : Boolean := False;
5039 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
5040 -- effect concerning library subprograms has been detected. Used to
5041 -- generate the precise error message.
5043 function From_Actual_Package (E : Entity_Id) return Boolean;
5044 -- Returns true if the entity is an actual for a package that is itself
5045 -- an actual for a formal package of the current instance. Such an
5046 -- entity requires special handling because it may be use-visible but
5047 -- hides directly visible entities defined outside the instance, because
5048 -- the corresponding formal did so in the generic.
5050 function Is_Actual_Parameter return Boolean;
5051 -- This function checks if the node N is an identifier that is an actual
5052 -- parameter of a procedure call. If so it returns True, otherwise it
5053 -- return False. The reason for this check is that at this stage we do
5054 -- not know what procedure is being called if the procedure might be
5055 -- overloaded, so it is premature to go setting referenced flags or
5056 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
5057 -- for that processing.
5058 -- Note: there is a similar routine Sem_Util.Is_Actual_Parameter, but
5059 -- it works for both function and procedure calls, while here we are
5060 -- only concerned with procedure calls (and with entry calls as well,
5061 -- but they are parsed as procedure calls and only later rewritten to
5064 function Known_But_Invisible (E : Entity_Id) return Boolean;
5065 -- This function determines whether a reference to the entity E, which
5066 -- is not visible, can reasonably be considered to be known to the
5067 -- writer of the reference. This is a heuristic test, used only for
5068 -- the purposes of figuring out whether we prefer to complain that an
5069 -- entity is undefined or invisible (and identify the declaration of
5070 -- the invisible entity in the latter case). The point here is that we
5071 -- don't want to complain that something is invisible and then point to
5072 -- something entirely mysterious to the writer.
5074 procedure Nvis_Messages;
5075 -- Called if there are no visible entries for N, but there is at least
5076 -- one non-directly visible, or hidden declaration. This procedure
5077 -- outputs an appropriate set of error messages.
5079 procedure Undefined (Nvis : Boolean);
5080 -- This function is called if the current node has no corresponding
5081 -- visible entity or entities. The value set in Msg indicates whether
5082 -- an error message was generated (multiple error messages for the
5083 -- same variable are generally suppressed, see body for details).
5084 -- Msg is True if an error message was generated, False if not. This
5085 -- value is used by the caller to determine whether or not to output
5086 -- additional messages where appropriate. The parameter is set False
5087 -- to get the message "X is undefined", and True to get the message
5088 -- "X is not visible".
5090 -------------------------
5091 -- From_Actual_Package --
5092 -------------------------
5094 function From_Actual_Package (E : Entity_Id) return Boolean is
5095 Scop : constant Entity_Id := Scope (E);
5096 -- Declared scope of candidate entity
5098 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
5099 -- Recursive function that does the work and examines actuals of
5100 -- actual packages of current instance.
5102 ------------------------
5103 -- Declared_In_Actual --
5104 ------------------------
5106 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
5110 if No (Associated_Formal_Package (Pack)) then
5114 Act := First_Entity (Pack);
5115 while Present (Act) loop
5116 if Renamed_Object (Pack) = Scop then
5119 -- Check for end of list of actuals
5121 elsif Ekind (Act) = E_Package
5122 and then Renamed_Object (Act) = Pack
5126 elsif Ekind (Act) = E_Package
5127 and then Declared_In_Actual (Act)
5137 end Declared_In_Actual;
5143 -- Start of processing for From_Actual_Package
5146 if not In_Instance then
5150 Inst := Current_Scope;
5151 while Present (Inst)
5152 and then Ekind (Inst) /= E_Package
5153 and then not Is_Generic_Instance (Inst)
5155 Inst := Scope (Inst);
5162 Act := First_Entity (Inst);
5163 while Present (Act) loop
5164 if Ekind (Act) = E_Package
5165 and then Declared_In_Actual (Act)
5175 end From_Actual_Package;
5177 -------------------------
5178 -- Is_Actual_Parameter --
5179 -------------------------
5181 function Is_Actual_Parameter return Boolean is
5183 if Nkind (N) = N_Identifier then
5184 case Nkind (Parent (N)) is
5185 when N_Procedure_Call_Statement =>
5186 return Is_List_Member (N)
5187 and then List_Containing (N) =
5188 Parameter_Associations (Parent (N));
5190 when N_Parameter_Association =>
5191 return N = Explicit_Actual_Parameter (Parent (N))
5192 and then Nkind (Parent (Parent (N))) =
5193 N_Procedure_Call_Statement;
5201 end Is_Actual_Parameter;
5203 -------------------------
5204 -- Known_But_Invisible --
5205 -------------------------
5207 function Known_But_Invisible (E : Entity_Id) return Boolean is
5208 Fname : File_Name_Type;
5211 -- Entities in Standard are always considered to be known
5213 if Sloc (E) <= Standard_Location then
5216 -- An entity that does not come from source is always considered
5217 -- to be unknown, since it is an artifact of code expansion.
5219 elsif not Comes_From_Source (E) then
5222 -- In gnat internal mode, we consider all entities known. The
5223 -- historical reason behind this discrepancy is not known??? But the
5224 -- only effect is to modify the error message given, so it is not
5225 -- critical. Since it only affects the exact wording of error
5226 -- messages in illegal programs, we do not mention this as an
5227 -- effect of -gnatg, since it is not a language modification.
5229 elsif GNAT_Mode then
5233 -- Here we have an entity that is not from package Standard, and
5234 -- which comes from Source. See if it comes from an internal file.
5236 Fname := Unit_File_Name (Get_Source_Unit (E));
5238 -- Case of from internal file
5240 if In_Internal_Unit (E) then
5242 -- Private part entities in internal files are never considered
5243 -- to be known to the writer of normal application code.
5245 if Is_Hidden (E) then
5249 -- Entities from System packages other than System and
5250 -- System.Storage_Elements are not considered to be known.
5251 -- System.Auxxxx files are also considered known to the user.
5253 -- Should refine this at some point to generally distinguish
5254 -- between known and unknown internal files ???
5256 Get_Name_String (Fname);
5261 Name_Buffer (1 .. 2) /= "s-"
5263 Name_Buffer (3 .. 8) = "stoele"
5265 Name_Buffer (3 .. 5) = "aux";
5267 -- If not an internal file, then entity is definitely known, even if
5268 -- it is in a private part (the message generated will note that it
5269 -- is in a private part).
5274 end Known_But_Invisible;
5280 procedure Nvis_Messages is
5281 Comp_Unit : Node_Id;
5283 Found : Boolean := False;
5284 Hidden : Boolean := False;
5288 if not Errors_OK then
5292 -- Ada 2005 (AI-262): Generate a precise error concerning the
5293 -- Beaujolais effect that was previously detected
5295 if Nvis_Is_Private_Subprg then
5297 pragma Assert (Nkind (E2) = N_Defining_Identifier
5298 and then Ekind (E2) = E_Function
5299 and then Scope (E2) = Standard_Standard
5300 and then Has_Private_With (E2));
5302 -- Find the sloc corresponding to the private with'ed unit
5304 Comp_Unit := Cunit (Current_Sem_Unit);
5305 Error_Msg_Sloc := No_Location;
5307 Item := First (Context_Items (Comp_Unit));
5308 while Present (Item) loop
5309 if Nkind (Item) = N_With_Clause
5310 and then Private_Present (Item)
5311 and then Entity (Name (Item)) = E2
5313 Error_Msg_Sloc := Sloc (Item);
5320 pragma Assert (Error_Msg_Sloc /= No_Location);
5322 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
5326 Undefined (Nvis => True);
5330 -- First loop does hidden declarations
5333 while Present (Ent) loop
5334 if Is_Potentially_Use_Visible (Ent) then
5336 Error_Msg_N -- CODEFIX
5337 ("multiple use clauses cause hiding!", N);
5341 Error_Msg_Sloc := Sloc (Ent);
5342 Error_Msg_N -- CODEFIX
5343 ("hidden declaration#!", N);
5346 Ent := Homonym (Ent);
5349 -- If we found hidden declarations, then that's enough, don't
5350 -- bother looking for non-visible declarations as well.
5356 -- Second loop does non-directly visible declarations
5359 while Present (Ent) loop
5360 if not Is_Potentially_Use_Visible (Ent) then
5362 -- Do not bother the user with unknown entities
5364 if not Known_But_Invisible (Ent) then
5368 Error_Msg_Sloc := Sloc (Ent);
5370 -- Output message noting that there is a non-visible
5371 -- declaration, distinguishing the private part case.
5373 if Is_Hidden (Ent) then
5374 Error_Msg_N ("non-visible (private) declaration#!", N);
5376 -- If the entity is declared in a generic package, it
5377 -- cannot be visible, so there is no point in adding it
5378 -- to the list of candidates if another homograph from a
5379 -- non-generic package has been seen.
5381 elsif Ekind (Scope (Ent)) = E_Generic_Package
5387 Error_Msg_N -- CODEFIX
5388 ("non-visible declaration#!", N);
5390 if Ekind (Scope (Ent)) /= E_Generic_Package then
5394 if Is_Compilation_Unit (Ent)
5396 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5398 Error_Msg_Qual_Level := 99;
5399 Error_Msg_NE -- CODEFIX
5400 ("\\missing `WITH &;`", N, Ent);
5401 Error_Msg_Qual_Level := 0;
5404 if Ekind (Ent) = E_Discriminant
5405 and then Present (Corresponding_Discriminant (Ent))
5406 and then Scope (Corresponding_Discriminant (Ent)) =
5410 ("inherited discriminant not allowed here" &
5411 " (RM 3.8 (12), 3.8.1 (6))!", N);
5415 -- Set entity and its containing package as referenced. We
5416 -- can't be sure of this, but this seems a better choice
5417 -- to avoid unused entity messages.
5419 if Comes_From_Source (Ent) then
5420 Set_Referenced (Ent);
5421 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5426 Ent := Homonym (Ent);
5435 procedure Undefined (Nvis : Boolean) is
5436 Emsg : Error_Msg_Id;
5439 -- We should never find an undefined internal name. If we do, then
5440 -- see if we have previous errors. If so, ignore on the grounds that
5441 -- it is probably a cascaded message (e.g. a block label from a badly
5442 -- formed block). If no previous errors, then we have a real internal
5443 -- error of some kind so raise an exception.
5445 if Is_Internal_Name (Chars (N)) then
5446 if Total_Errors_Detected /= 0 then
5449 raise Program_Error;
5453 -- A very specialized error check, if the undefined variable is
5454 -- a case tag, and the case type is an enumeration type, check
5455 -- for a possible misspelling, and if so, modify the identifier
5457 -- Named aggregate should also be handled similarly ???
5460 and then Nkind (N) = N_Identifier
5461 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5464 Case_Stm : constant Node_Id := Parent (Parent (N));
5465 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5470 if Is_Enumeration_Type (Case_Typ)
5471 and then not Is_Standard_Character_Type (Case_Typ)
5473 Lit := First_Literal (Case_Typ);
5474 Get_Name_String (Chars (Lit));
5476 if Chars (Lit) /= Chars (N)
5477 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5479 Error_Msg_Node_2 := Lit;
5480 Error_Msg_N -- CODEFIX
5481 ("& is undefined, assume misspelling of &", N);
5482 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5491 -- Normal processing
5493 Set_Entity (N, Any_Id);
5494 Set_Etype (N, Any_Type);
5498 -- We use the table Urefs to keep track of entities for which we
5499 -- have issued errors for undefined references. Multiple errors
5500 -- for a single name are normally suppressed, however we modify
5501 -- the error message to alert the programmer to this effect.
5503 for J in Urefs.First .. Urefs.Last loop
5504 if Chars (N) = Chars (Urefs.Table (J).Node) then
5505 if Urefs.Table (J).Err /= No_Error_Msg
5506 and then Sloc (N) /= Urefs.Table (J).Loc
5508 Error_Msg_Node_1 := Urefs.Table (J).Node;
5510 if Urefs.Table (J).Nvis then
5511 Change_Error_Text (Urefs.Table (J).Err,
5512 "& is not visible (more references follow)");
5514 Change_Error_Text (Urefs.Table (J).Err,
5515 "& is undefined (more references follow)");
5518 Urefs.Table (J).Err := No_Error_Msg;
5521 -- Although we will set Msg False, and thus suppress the
5522 -- message, we also set Error_Posted True, to avoid any
5523 -- cascaded messages resulting from the undefined reference.
5526 Set_Error_Posted (N);
5531 -- If entry not found, this is first undefined occurrence
5534 Error_Msg_N ("& is not visible!", N);
5538 Error_Msg_N ("& is undefined!", N);
5541 -- A very bizarre special check, if the undefined identifier
5542 -- is Put or Put_Line, then add a special error message (since
5543 -- this is a very common error for beginners to make).
5545 if Chars (N) in Name_Put | Name_Put_Line then
5546 Error_Msg_N -- CODEFIX
5547 ("\\possible missing `WITH Ada.Text_'I'O; " &
5548 "USE Ada.Text_'I'O`!", N);
5550 -- Another special check if N is the prefix of a selected
5551 -- component which is a known unit: add message complaining
5552 -- about missing with for this unit.
5554 elsif Nkind (Parent (N)) = N_Selected_Component
5555 and then N = Prefix (Parent (N))
5556 and then Is_Known_Unit (Parent (N))
5558 Error_Msg_Node_2 := Selector_Name (Parent (N));
5559 Error_Msg_N -- CODEFIX
5560 ("\\missing `WITH &.&;`", Prefix (Parent (N)));
5563 -- Now check for possible misspellings
5567 Ematch : Entity_Id := Empty;
5569 Last_Name_Id : constant Name_Id :=
5570 Name_Id (Nat (First_Name_Id) +
5571 Name_Entries_Count - 1);
5574 for Nam in First_Name_Id .. Last_Name_Id loop
5575 E := Get_Name_Entity_Id (Nam);
5578 and then (Is_Immediately_Visible (E)
5580 Is_Potentially_Use_Visible (E))
5582 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5589 if Present (Ematch) then
5590 Error_Msg_NE -- CODEFIX
5591 ("\possible misspelling of&", N, Ematch);
5596 -- Make entry in undefined references table unless the full errors
5597 -- switch is set, in which case by refraining from generating the
5598 -- table entry we guarantee that we get an error message for every
5599 -- undefined reference. The entry is not added if we are ignoring
5602 if not All_Errors_Mode and then Ignore_Errors_Enable = 0 then
5616 Nested_Inst : Entity_Id := Empty;
5617 -- The entity of a nested instance which appears within Inst (if any)
5619 -- Start of processing for Find_Direct_Name
5622 -- If the entity pointer is already set, this is an internal node, or
5623 -- a node that is analyzed more than once, after a tree modification.
5624 -- In such a case there is no resolution to perform, just set the type.
5626 if Present (Entity (N)) then
5627 if Is_Type (Entity (N)) then
5628 Set_Etype (N, Entity (N));
5632 Entyp : constant Entity_Id := Etype (Entity (N));
5635 -- One special case here. If the Etype field is already set,
5636 -- and references the packed array type corresponding to the
5637 -- etype of the referenced entity, then leave it alone. This
5638 -- happens for trees generated from Exp_Pakd, where expressions
5639 -- can be deliberately "mis-typed" to the packed array type.
5641 if Is_Array_Type (Entyp)
5642 and then Is_Packed (Entyp)
5643 and then Present (Etype (N))
5644 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5648 -- If not that special case, then just reset the Etype
5651 Set_Etype (N, Etype (Entity (N)));
5656 -- Although the marking of use clauses happens at the end of
5657 -- Find_Direct_Name, a certain case where a generic actual satisfies
5658 -- a use clause must be checked here due to how the generic machinery
5659 -- handles the analysis of said actuals.
5662 and then Nkind (Parent (N)) = N_Generic_Association
5664 Mark_Use_Clauses (Entity (N));
5670 -- Preserve relevant elaboration-related attributes of the context which
5671 -- are no longer available or very expensive to recompute once analysis,
5672 -- resolution, and expansion are over.
5674 if Nkind (N) = N_Identifier then
5675 Mark_Elaboration_Attributes
5682 -- Here if Entity pointer was not set, we need full visibility analysis
5683 -- First we generate debugging output if the debug E flag is set.
5685 if Debug_Flag_E then
5686 Write_Str ("Looking for ");
5687 Write_Name (Chars (N));
5691 Homonyms := Current_Entity (N);
5692 Nvis_Entity := False;
5695 while Present (E) loop
5697 -- If entity is immediately visible or potentially use visible, then
5698 -- process the entity and we are done.
5700 if Is_Immediately_Visible (E) then
5701 goto Immediately_Visible_Entity;
5703 elsif Is_Potentially_Use_Visible (E) then
5704 goto Potentially_Use_Visible_Entity;
5706 -- Note if a known but invisible entity encountered
5708 elsif Known_But_Invisible (E) then
5709 Nvis_Entity := True;
5712 -- Move to next entity in chain and continue search
5717 -- If no entries on homonym chain that were potentially visible,
5718 -- and no entities reasonably considered as non-visible, then
5719 -- we have a plain undefined reference, with no additional
5720 -- explanation required.
5722 if not Nvis_Entity then
5723 Undefined (Nvis => False);
5725 -- Otherwise there is at least one entry on the homonym chain that
5726 -- is reasonably considered as being known and non-visible.
5734 -- Processing for a potentially use visible entry found. We must search
5735 -- the rest of the homonym chain for two reasons. First, if there is a
5736 -- directly visible entry, then none of the potentially use-visible
5737 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5738 -- for the case of multiple potentially use-visible entries hiding one
5739 -- another and as a result being non-directly visible (RM 8.4(11)).
5741 <<Potentially_Use_Visible_Entity>> declare
5742 Only_One_Visible : Boolean := True;
5743 All_Overloadable : Boolean := Is_Overloadable (E);
5747 while Present (E2) loop
5748 if Is_Immediately_Visible (E2) then
5750 -- If the use-visible entity comes from the actual for a
5751 -- formal package, it hides a directly visible entity from
5752 -- outside the instance.
5754 if From_Actual_Package (E)
5755 and then Scope_Depth (Scope (E2)) < Scope_Depth (Inst)
5760 goto Immediately_Visible_Entity;
5763 elsif Is_Potentially_Use_Visible (E2) then
5764 Only_One_Visible := False;
5765 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5767 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5768 -- that can occur in private_with clauses. Example:
5771 -- private with B; package A is
5772 -- package C is function B return Integer;
5774 -- V1 : Integer := B;
5775 -- private function B return Integer;
5776 -- V2 : Integer := B;
5779 -- V1 resolves to A.B, but V2 resolves to library unit B
5781 elsif Ekind (E2) = E_Function
5782 and then Scope (E2) = Standard_Standard
5783 and then Has_Private_With (E2)
5785 Only_One_Visible := False;
5786 All_Overloadable := False;
5787 Nvis_Is_Private_Subprg := True;
5794 -- On falling through this loop, we have checked that there are no
5795 -- immediately visible entities. Only_One_Visible is set if exactly
5796 -- one potentially use visible entity exists. All_Overloadable is
5797 -- set if all the potentially use visible entities are overloadable.
5798 -- The condition for legality is that either there is one potentially
5799 -- use visible entity, or if there is more than one, then all of them
5800 -- are overloadable.
5802 if Only_One_Visible or All_Overloadable then
5805 -- If there is more than one potentially use-visible entity and at
5806 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5807 -- Note that E points to the first such entity on the homonym list.
5810 -- If one of the entities is declared in an actual package, it
5811 -- was visible in the generic, and takes precedence over other
5812 -- entities that are potentially use-visible. The same applies
5813 -- if the entity is declared in a local instantiation of the
5814 -- current instance.
5818 -- Find the current instance
5820 Inst := Current_Scope;
5821 while Present (Inst) and then Inst /= Standard_Standard loop
5822 if Is_Generic_Instance (Inst) then
5826 Inst := Scope (Inst);
5829 -- Reexamine the candidate entities, giving priority to those
5830 -- that were visible within the generic.
5833 while Present (E2) loop
5834 Nested_Inst := Nearest_Enclosing_Instance (E2);
5836 -- The entity is declared within an actual package, or in a
5837 -- nested instance. The ">=" accounts for the case where the
5838 -- current instance and the nested instance are the same.
5840 if From_Actual_Package (E2)
5841 or else (Present (Nested_Inst)
5842 and then Scope_Depth (Nested_Inst) >=
5855 elsif Is_Predefined_Unit (Current_Sem_Unit) then
5856 -- A use clause in the body of a system file creates conflict
5857 -- with some entity in a user scope, while rtsfind is active.
5858 -- Keep only the entity coming from another predefined unit.
5861 while Present (E2) loop
5862 if In_Predefined_Unit (E2) then
5870 -- Entity must exist because predefined unit is correct
5872 raise Program_Error;
5881 -- Come here with E set to the first immediately visible entity on
5882 -- the homonym chain. This is the one we want unless there is another
5883 -- immediately visible entity further on in the chain for an inner
5884 -- scope (RM 8.3(8)).
5886 <<Immediately_Visible_Entity>> declare
5891 -- Find scope level of initial entity. When compiling through
5892 -- Rtsfind, the previous context is not completely invisible, and
5893 -- an outer entity may appear on the chain, whose scope is below
5894 -- the entry for Standard that delimits the current scope stack.
5895 -- Indicate that the level for this spurious entry is outside of
5896 -- the current scope stack.
5898 Level := Scope_Stack.Last;
5900 Scop := Scope_Stack.Table (Level).Entity;
5901 exit when Scop = Scope (E);
5903 exit when Scop = Standard_Standard;
5906 -- Now search remainder of homonym chain for more inner entry
5907 -- If the entity is Standard itself, it has no scope, and we
5908 -- compare it with the stack entry directly.
5911 while Present (E2) loop
5912 if Is_Immediately_Visible (E2) then
5914 -- If a generic package contains a local declaration that
5915 -- has the same name as the generic, there may be a visibility
5916 -- conflict in an instance, where the local declaration must
5917 -- also hide the name of the corresponding package renaming.
5918 -- We check explicitly for a package declared by a renaming,
5919 -- whose renamed entity is an instance that is on the scope
5920 -- stack, and that contains a homonym in the same scope. Once
5921 -- we have found it, we know that the package renaming is not
5922 -- immediately visible, and that the identifier denotes the
5923 -- other entity (and its homonyms if overloaded).
5925 if Scope (E) = Scope (E2)
5926 and then Ekind (E) = E_Package
5927 and then Present (Renamed_Object (E))
5928 and then Is_Generic_Instance (Renamed_Object (E))
5929 and then In_Open_Scopes (Renamed_Object (E))
5930 and then Comes_From_Source (N)
5932 Set_Is_Immediately_Visible (E, False);
5936 for J in Level + 1 .. Scope_Stack.Last loop
5937 if Scope_Stack.Table (J).Entity = Scope (E2)
5938 or else Scope_Stack.Table (J).Entity = E2
5951 -- At the end of that loop, E is the innermost immediately
5952 -- visible entity, so we are all set.
5955 -- Come here with entity found, and stored in E
5959 -- Check violation of No_Wide_Characters restriction
5961 Check_Wide_Character_Restriction (E, N);
5963 -- When distribution features are available (Get_PCS_Name /=
5964 -- Name_No_DSA), a remote access-to-subprogram type is converted
5965 -- into a record type holding whatever information is needed to
5966 -- perform a remote call on an RCI subprogram. In that case we
5967 -- rewrite any occurrence of the RAS type into the equivalent record
5968 -- type here. 'Access attribute references and RAS dereferences are
5969 -- then implemented using specific TSSs. However when distribution is
5970 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
5971 -- generation of these TSSs, and we must keep the RAS type in its
5972 -- original access-to-subprogram form (since all calls through a
5973 -- value of such type will be local anyway in the absence of a PCS).
5975 if Comes_From_Source (N)
5976 and then Is_Remote_Access_To_Subprogram_Type (E)
5977 and then Ekind (E) = E_Access_Subprogram_Type
5978 and then Expander_Active
5979 and then Get_PCS_Name /= Name_No_DSA
5981 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
5985 -- Set the entity. Note that the reason we call Set_Entity for the
5986 -- overloadable case, as opposed to Set_Entity_With_Checks is
5987 -- that in the overloaded case, the initial call can set the wrong
5988 -- homonym. The call that sets the right homonym is in Sem_Res and
5989 -- that call does use Set_Entity_With_Checks, so we don't miss
5992 if Is_Overloadable (E) then
5995 Set_Entity_With_Checks (N, E);
6001 Set_Etype (N, Get_Full_View (Etype (E)));
6004 if Debug_Flag_E then
6005 Write_Str (" found ");
6006 Write_Entity_Info (E, " ");
6009 -- If the Ekind of the entity is Void, it means that all homonyms
6010 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
6011 -- test is skipped if the current scope is a record and the name is
6012 -- a pragma argument expression (case of Atomic and Volatile pragmas
6013 -- and possibly other similar pragmas added later, which are allowed
6014 -- to reference components in the current record).
6016 if Ekind (E) = E_Void
6018 (not Is_Record_Type (Current_Scope)
6019 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
6021 Premature_Usage (N);
6023 -- If the entity is overloadable, collect all interpretations of the
6024 -- name for subsequent overload resolution. We optimize a bit here to
6025 -- do this only if we have an overloadable entity that is not on its
6026 -- own on the homonym chain.
6028 elsif Is_Overloadable (E)
6029 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
6031 Collect_Interps (N);
6033 -- If no homonyms were visible, the entity is unambiguous
6035 if not Is_Overloaded (N) then
6036 if Reference_OK and then not Is_Actual_Parameter then
6037 Generate_Reference (E, N);
6041 -- Case of non-overloadable entity, set the entity providing that
6042 -- we do not have the case of a discriminant reference within a
6043 -- default expression. Such references are replaced with the
6044 -- corresponding discriminal, which is the formal corresponding to
6045 -- to the discriminant in the initialization procedure.
6048 -- Entity is unambiguous, indicate that it is referenced here
6050 -- For a renaming of an object, always generate simple reference,
6051 -- we don't try to keep track of assignments in this case, except
6052 -- in SPARK mode where renamings are traversed for generating
6053 -- local effects of subprograms.
6056 and then Is_Object (E)
6057 and then Present (Renamed_Object (E))
6058 and then not GNATprove_Mode
6060 Generate_Reference (E, N);
6062 -- If the renamed entity is a private protected component,
6063 -- reference the original component as well. This needs to be
6064 -- done because the private renamings are installed before any
6065 -- analysis has occurred. Reference to a private component will
6066 -- resolve to the renaming and the original component will be
6067 -- left unreferenced, hence the following.
6069 if Is_Prival (E) then
6070 Generate_Reference (Prival_Link (E), N);
6073 -- One odd case is that we do not want to set the Referenced flag
6074 -- if the entity is a label, and the identifier is the label in
6075 -- the source, since this is not a reference from the point of
6076 -- view of the user.
6078 elsif Nkind (Parent (N)) = N_Label then
6080 R : constant Boolean := Referenced (E);
6083 -- Generate reference unless this is an actual parameter
6084 -- (see comment below).
6086 if Reference_OK and then not Is_Actual_Parameter then
6087 Generate_Reference (E, N);
6088 Set_Referenced (E, R);
6092 -- Normal case, not a label: generate reference
6095 if Reference_OK and then not Is_Actual_Parameter then
6097 -- Package or generic package is always a simple reference
6099 if Is_Package_Or_Generic_Package (E) then
6100 Generate_Reference (E, N, 'r');
6102 -- Else see if we have a left hand side
6107 Generate_Reference (E, N, 'm');
6110 Generate_Reference (E, N, 'r');
6112 -- If we don't know now, generate reference later
6115 Deferred_References.Append ((E, N));
6121 Set_Entity_Or_Discriminal (N, E);
6123 -- The name may designate a generalized reference, in which case
6124 -- the dereference interpretation will be included. Context is
6125 -- one in which a name is legal.
6127 if Ada_Version >= Ada_2012
6129 (Nkind (Parent (N)) in N_Subexpr
6130 or else Nkind (Parent (N)) in N_Assignment_Statement
6131 | N_Object_Declaration
6132 | N_Parameter_Association)
6134 Check_Implicit_Dereference (N, Etype (E));
6139 -- Mark relevant use-type and use-package clauses as effective if the
6140 -- node in question is not overloaded and therefore does not require
6143 -- Note: Generic actual subprograms do not follow the normal resolution
6144 -- path, so ignore the fact that they are overloaded and mark them
6147 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6148 Mark_Use_Clauses (N);
6151 -- Come here with entity set
6154 Check_Restriction_No_Use_Of_Entity (N);
6156 -- Annotate the tree by creating a variable reference marker in case the
6157 -- original variable reference is folded or optimized away. The variable
6158 -- reference marker is automatically saved for later examination by the
6159 -- ABE Processing phase. Variable references which act as actuals in a
6160 -- call require special processing and are left to Resolve_Actuals. The
6161 -- reference is a write when it appears on the left hand side of an
6165 and then Needs_Variable_Reference_Marker
6170 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6173 Build_Variable_Reference_Marker
6175 Read => not Is_Assignment_LHS,
6176 Write => Is_Assignment_LHS);
6179 end Find_Direct_Name;
6181 ------------------------
6182 -- Find_Expanded_Name --
6183 ------------------------
6185 -- This routine searches the homonym chain of the entity until it finds
6186 -- an entity declared in the scope denoted by the prefix. If the entity
6187 -- is private, it may nevertheless be immediately visible, if we are in
6188 -- the scope of its declaration.
6190 procedure Find_Expanded_Name (N : Node_Id) is
6191 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean;
6192 -- Determine whether expanded name Nod appears within a pragma which is
6193 -- a suitable context for an abstract view of a state or variable. The
6194 -- following pragmas fall in this category:
6201 -- In addition, pragma Abstract_State is also considered suitable even
6202 -- though it is an illegal context for an abstract view as this allows
6203 -- for proper resolution of abstract views of variables. This illegal
6204 -- context is later flagged in the analysis of indicator Part_Of.
6206 -----------------------------
6207 -- In_Abstract_View_Pragma --
6208 -----------------------------
6210 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is
6214 -- Climb the parent chain looking for a pragma
6217 while Present (Par) loop
6218 if Nkind (Par) = N_Pragma then
6219 if Pragma_Name_Unmapped (Par)
6220 in Name_Abstract_State
6224 | Name_Refined_Depends
6225 | Name_Refined_Global
6229 -- Otherwise the pragma is not a legal context for an abstract
6236 -- Prevent the search from going too far
6238 elsif Is_Body_Or_Package_Declaration (Par) then
6242 Par := Parent (Par);
6246 end In_Abstract_View_Pragma;
6250 Selector : constant Node_Id := Selector_Name (N);
6252 Candidate : Entity_Id := Empty;
6256 -- Start of processing for Find_Expanded_Name
6259 P_Name := Entity (Prefix (N));
6261 -- If the prefix is a renamed package, look for the entity in the
6262 -- original package.
6264 if Ekind (P_Name) = E_Package
6265 and then Present (Renamed_Object (P_Name))
6267 P_Name := Renamed_Object (P_Name);
6269 -- Rewrite node with entity field pointing to renamed object
6271 Rewrite (Prefix (N), New_Copy (Prefix (N)));
6272 Set_Entity (Prefix (N), P_Name);
6274 -- If the prefix is an object of a concurrent type, look for
6275 -- the entity in the associated task or protected type.
6277 elsif Is_Concurrent_Type (Etype (P_Name)) then
6278 P_Name := Etype (P_Name);
6281 Id := Current_Entity (Selector);
6284 Is_New_Candidate : Boolean;
6287 while Present (Id) loop
6288 if Scope (Id) = P_Name then
6290 Is_New_Candidate := True;
6292 -- Handle abstract views of states and variables. These are
6293 -- acceptable candidates only when the reference to the view
6294 -- appears in certain pragmas.
6296 if Ekind (Id) = E_Abstract_State
6297 and then From_Limited_With (Id)
6298 and then Present (Non_Limited_View (Id))
6300 if In_Abstract_View_Pragma (N) then
6301 Candidate := Non_Limited_View (Id);
6302 Is_New_Candidate := True;
6304 -- Hide the candidate because it is not used in a proper
6309 Is_New_Candidate := False;
6313 -- Ada 2005 (AI-217): Handle shadow entities associated with
6314 -- types declared in limited-withed nested packages. We don't need
6315 -- to handle E_Incomplete_Subtype entities because the entities
6316 -- in the limited view are always E_Incomplete_Type and
6317 -- E_Class_Wide_Type entities (see Build_Limited_Views).
6319 -- Regarding the expression used to evaluate the scope, it
6320 -- is important to note that the limited view also has shadow
6321 -- entities associated nested packages. For this reason the
6322 -- correct scope of the entity is the scope of the real entity.
6323 -- The non-limited view may itself be incomplete, in which case
6324 -- get the full view if available.
6326 elsif Ekind (Id) in E_Incomplete_Type | E_Class_Wide_Type
6327 and then From_Limited_With (Id)
6328 and then Present (Non_Limited_View (Id))
6329 and then Scope (Non_Limited_View (Id)) = P_Name
6331 Candidate := Get_Full_View (Non_Limited_View (Id));
6332 Is_New_Candidate := True;
6334 -- An unusual case arises with a fully qualified name for an
6335 -- entity local to a generic child unit package, within an
6336 -- instantiation of that package. The name of the unit now
6337 -- denotes the renaming created within the instance. This is
6338 -- only relevant in an instance body, see below.
6340 elsif Is_Generic_Instance (Scope (Id))
6341 and then In_Open_Scopes (Scope (Id))
6342 and then In_Instance_Body
6343 and then Ekind (Scope (Id)) = E_Package
6344 and then Ekind (Id) = E_Package
6345 and then Renamed_Entity (Id) = Scope (Id)
6346 and then Is_Immediately_Visible (P_Name)
6348 Is_New_Candidate := True;
6351 Is_New_Candidate := False;
6354 if Is_New_Candidate then
6356 -- If entity is a child unit, either it is a visible child of
6357 -- the prefix, or we are in the body of a generic prefix, as
6358 -- will happen when a child unit is instantiated in the body
6359 -- of a generic parent. This is because the instance body does
6360 -- not restore the full compilation context, given that all
6361 -- non-local references have been captured.
6363 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
6364 exit when Is_Visible_Lib_Unit (Id)
6365 or else (Is_Child_Unit (Id)
6366 and then In_Open_Scopes (Scope (Id))
6367 and then In_Instance_Body);
6369 exit when not Is_Hidden (Id);
6372 exit when Is_Immediately_Visible (Id);
6380 and then Ekind (P_Name) in E_Procedure | E_Function
6381 and then Is_Generic_Instance (P_Name)
6383 -- Expanded name denotes entity in (instance of) generic subprogram.
6384 -- The entity may be in the subprogram instance, or may denote one of
6385 -- the formals, which is declared in the enclosing wrapper package.
6387 P_Name := Scope (P_Name);
6389 Id := Current_Entity (Selector);
6390 while Present (Id) loop
6391 exit when Scope (Id) = P_Name;
6396 if No (Id) or else Chars (Id) /= Chars (Selector) then
6397 Set_Etype (N, Any_Type);
6399 -- If we are looking for an entity defined in System, try to find it
6400 -- in the child package that may have been provided as an extension
6401 -- to System. The Extend_System pragma will have supplied the name of
6402 -- the extension, which may have to be loaded.
6404 if Chars (P_Name) = Name_System
6405 and then Scope (P_Name) = Standard_Standard
6406 and then Present (System_Extend_Unit)
6407 and then Present_System_Aux (N)
6409 Set_Entity (Prefix (N), System_Aux_Id);
6410 Find_Expanded_Name (N);
6413 -- There is an implicit instance of the predefined operator in
6414 -- the given scope. The operator entity is defined in Standard.
6415 -- Has_Implicit_Operator makes the node into an Expanded_Name.
6417 elsif Nkind (Selector) = N_Operator_Symbol
6418 and then Has_Implicit_Operator (N)
6422 -- If there is no literal defined in the scope denoted by the
6423 -- prefix, the literal may belong to (a type derived from)
6424 -- Standard_Character, for which we have no explicit literals.
6426 elsif Nkind (Selector) = N_Character_Literal
6427 and then Has_Implicit_Character_Literal (N)
6432 -- If the prefix is a single concurrent object, use its name in
6433 -- the error message, rather than that of the anonymous type.
6435 if Is_Concurrent_Type (P_Name)
6436 and then Is_Internal_Name (Chars (P_Name))
6438 Error_Msg_Node_2 := Entity (Prefix (N));
6440 Error_Msg_Node_2 := P_Name;
6443 if P_Name = System_Aux_Id then
6444 P_Name := Scope (P_Name);
6445 Set_Entity (Prefix (N), P_Name);
6448 if Present (Candidate) then
6450 -- If we know that the unit is a child unit we can give a more
6451 -- accurate error message.
6453 if Is_Child_Unit (Candidate) then
6455 -- If the candidate is a private child unit and we are in
6456 -- the visible part of a public unit, specialize the error
6457 -- message. There might be a private with_clause for it,
6458 -- but it is not currently active.
6460 if Is_Private_Descendant (Candidate)
6461 and then Ekind (Current_Scope) = E_Package
6462 and then not In_Private_Part (Current_Scope)
6463 and then not Is_Private_Descendant (Current_Scope)
6466 ("private child unit& is not visible here", Selector);
6468 -- Normal case where we have a missing with for a child unit
6471 Error_Msg_Qual_Level := 99;
6472 Error_Msg_NE -- CODEFIX
6473 ("missing `WITH &;`", Selector, Candidate);
6474 Error_Msg_Qual_Level := 0;
6477 -- Here we don't know that this is a child unit
6480 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
6484 -- Within the instantiation of a child unit, the prefix may
6485 -- denote the parent instance, but the selector has the name
6486 -- of the original child. That is to say, when A.B appears
6487 -- within an instantiation of generic child unit B, the scope
6488 -- stack includes an instance of A (P_Name) and an instance
6489 -- of B under some other name. We scan the scope to find this
6490 -- child instance, which is the desired entity.
6491 -- Note that the parent may itself be a child instance, if
6492 -- the reference is of the form A.B.C, in which case A.B has
6493 -- already been rewritten with the proper entity.
6495 if In_Open_Scopes (P_Name)
6496 and then Is_Generic_Instance (P_Name)
6499 Gen_Par : constant Entity_Id :=
6500 Generic_Parent (Specification
6501 (Unit_Declaration_Node (P_Name)));
6502 S : Entity_Id := Current_Scope;
6506 for J in reverse 0 .. Scope_Stack.Last loop
6507 S := Scope_Stack.Table (J).Entity;
6509 exit when S = Standard_Standard;
6511 if Ekind (S) in E_Function | E_Package | E_Procedure
6514 Generic_Parent (Specification
6515 (Unit_Declaration_Node (S)));
6517 -- Check that P is a generic child of the generic
6518 -- parent of the prefix.
6521 and then Chars (P) = Chars (Selector)
6522 and then Scope (P) = Gen_Par
6533 -- If this is a selection from Ada, System or Interfaces, then
6534 -- we assume a missing with for the corresponding package.
6536 if Is_Known_Unit (N)
6537 and then not (Present (Entity (Prefix (N)))
6538 and then Scope (Entity (Prefix (N))) /=
6541 if not Error_Posted (N) then
6542 Error_Msg_Node_2 := Selector;
6543 Error_Msg_N -- CODEFIX
6544 ("missing `WITH &.&;`", Prefix (N));
6547 -- If this is a selection from a dummy package, then suppress
6548 -- the error message, of course the entity is missing if the
6549 -- package is missing.
6551 elsif Sloc (Error_Msg_Node_2) = No_Location then
6554 -- Here we have the case of an undefined component
6557 -- The prefix may hide a homonym in the context that
6558 -- declares the desired entity. This error can use a
6559 -- specialized message.
6561 if In_Open_Scopes (P_Name) then
6563 H : constant Entity_Id := Homonym (P_Name);
6567 and then Is_Compilation_Unit (H)
6569 (Is_Immediately_Visible (H)
6570 or else Is_Visible_Lib_Unit (H))
6572 Id := First_Entity (H);
6573 while Present (Id) loop
6574 if Chars (Id) = Chars (Selector) then
6575 Error_Msg_Qual_Level := 99;
6576 Error_Msg_Name_1 := Chars (Selector);
6578 ("% not declared in&", N, P_Name);
6580 ("\use fully qualified name starting with "
6581 & "Standard to make& visible", N, H);
6582 Error_Msg_Qual_Level := 0;
6590 -- If not found, standard error message
6592 Error_Msg_NE ("& not declared in&", N, Selector);
6598 -- Might be worth specializing the case when the prefix
6599 -- is a limited view.
6600 -- ... not declared in limited view of...
6602 Error_Msg_NE ("& not declared in&", N, Selector);
6605 -- Check for misspelling of some entity in prefix
6607 Id := First_Entity (P_Name);
6608 while Present (Id) loop
6609 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
6610 and then not Is_Internal_Name (Chars (Id))
6612 Error_Msg_NE -- CODEFIX
6613 ("possible misspelling of&", Selector, Id);
6620 -- Specialize the message if this may be an instantiation
6621 -- of a child unit that was not mentioned in the context.
6623 if Nkind (Parent (N)) = N_Package_Instantiation
6624 and then Is_Generic_Instance (Entity (Prefix (N)))
6625 and then Is_Compilation_Unit
6626 (Generic_Parent (Parent (Entity (Prefix (N)))))
6628 Error_Msg_Node_2 := Selector;
6629 Error_Msg_N -- CODEFIX
6630 ("\missing `WITH &.&;`", Prefix (N));
6640 if Comes_From_Source (N)
6641 and then Is_Remote_Access_To_Subprogram_Type (Id)
6642 and then Ekind (Id) = E_Access_Subprogram_Type
6643 and then Present (Equivalent_Type (Id))
6645 -- If we are not actually generating distribution code (i.e. the
6646 -- current PCS is the dummy non-distributed version), then the
6647 -- Equivalent_Type will be missing, and Id should be treated as
6648 -- a regular access-to-subprogram type.
6650 Id := Equivalent_Type (Id);
6651 Set_Chars (Selector, Chars (Id));
6654 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
6656 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
6657 if From_Limited_With (Id)
6658 or else Is_Type (Id)
6659 or else Ekind (Id) = E_Package
6664 ("limited withed package can only be used to access incomplete "
6669 if Is_Task_Type (P_Name)
6670 and then ((Ekind (Id) = E_Entry
6671 and then Nkind (Parent (N)) /= N_Attribute_Reference)
6673 (Ekind (Id) = E_Entry_Family
6675 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
6677 -- If both the task type and the entry are in scope, this may still
6678 -- be the expanded name of an entry formal.
6680 if In_Open_Scopes (Id)
6681 and then Nkind (Parent (N)) = N_Selected_Component
6686 -- It is an entry call after all, either to the current task
6687 -- (which will deadlock) or to an enclosing task.
6689 Analyze_Selected_Component (N);
6694 Change_Selected_Component_To_Expanded_Name (N);
6696 -- Preserve relevant elaboration-related attributes of the context which
6697 -- are no longer available or very expensive to recompute once analysis,
6698 -- resolution, and expansion are over.
6700 Mark_Elaboration_Attributes
6706 -- Set appropriate type
6708 if Is_Type (Id) then
6711 Set_Etype (N, Get_Full_View (Etype (Id)));
6714 -- Do style check and generate reference, but skip both steps if this
6715 -- entity has homonyms, since we may not have the right homonym set yet.
6716 -- The proper homonym will be set during the resolve phase.
6718 if Has_Homonym (Id) then
6722 Set_Entity_Or_Discriminal (N, Id);
6726 Generate_Reference (Id, N, 'm');
6729 Generate_Reference (Id, N, 'r');
6732 Deferred_References.Append ((Id, N));
6736 -- Check for violation of No_Wide_Characters
6738 Check_Wide_Character_Restriction (Id, N);
6740 -- If the Ekind of the entity is Void, it means that all homonyms are
6741 -- hidden from all visibility (RM 8.3(5,14-20)).
6743 if Ekind (Id) = E_Void then
6744 Premature_Usage (N);
6746 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6748 H : Entity_Id := Homonym (Id);
6751 while Present (H) loop
6752 if Scope (H) = Scope (Id)
6753 and then (not Is_Hidden (H)
6754 or else Is_Immediately_Visible (H))
6756 Collect_Interps (N);
6763 -- If an extension of System is present, collect possible explicit
6764 -- overloadings declared in the extension.
6766 if Chars (P_Name) = Name_System
6767 and then Scope (P_Name) = Standard_Standard
6768 and then Present (System_Extend_Unit)
6769 and then Present_System_Aux (N)
6771 H := Current_Entity (Id);
6773 while Present (H) loop
6774 if Scope (H) = System_Aux_Id then
6775 Add_One_Interp (N, H, Etype (H));
6784 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6785 and then Scope (Id) /= Standard_Standard
6787 -- In addition to user-defined operators in the given scope, there
6788 -- may be an implicit instance of the predefined operator. The
6789 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6790 -- and added to the interpretations. Procedure Add_One_Interp will
6791 -- determine which hides which.
6793 if Has_Implicit_Operator (N) then
6798 -- If there is a single interpretation for N we can generate a
6799 -- reference to the unique entity found.
6801 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6802 Generate_Reference (Id, N);
6805 -- Mark relevant use-type and use-package clauses as effective if the
6806 -- node in question is not overloaded and therefore does not require
6809 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6810 Mark_Use_Clauses (N);
6813 Check_Restriction_No_Use_Of_Entity (N);
6815 -- Annotate the tree by creating a variable reference marker in case the
6816 -- original variable reference is folded or optimized away. The variable
6817 -- reference marker is automatically saved for later examination by the
6818 -- ABE Processing phase. Variable references which act as actuals in a
6819 -- call require special processing and are left to Resolve_Actuals. The
6820 -- reference is a write when it appears on the left hand side of an
6823 if Needs_Variable_Reference_Marker
6828 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6831 Build_Variable_Reference_Marker
6833 Read => not Is_Assignment_LHS,
6834 Write => Is_Assignment_LHS);
6837 end Find_Expanded_Name;
6839 --------------------
6840 -- Find_Most_Prev --
6841 --------------------
6843 function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id is
6847 -- Loop through the Prev_Use_Clause chain
6850 while Present (Prev_Use_Clause (Curr)) loop
6851 Curr := Prev_Use_Clause (Curr);
6857 -------------------------
6858 -- Find_Renamed_Entity --
6859 -------------------------
6861 function Find_Renamed_Entity
6865 Is_Actual : Boolean := False) return Entity_Id
6868 I1 : Interp_Index := 0; -- Suppress junk warnings
6874 function Find_Nearer_Entity
6877 Old2_S : Entity_Id) return Entity_Id;
6878 -- Determine whether one of Old_S1 and Old_S2 is nearer to New_S than
6879 -- the other, and return it if so. Return Empty otherwise. We use this
6880 -- in conjunction with Inherit_Renamed_Profile to simplify later type
6881 -- disambiguation for actual subprograms in instances.
6883 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
6884 -- If the renamed entity is an implicit operator, check whether it is
6885 -- visible because its operand type is properly visible. This check
6886 -- applies to explicit renamed entities that appear in the source in a
6887 -- renaming declaration or a formal subprogram instance, but not to
6888 -- default generic actuals with a name.
6890 function Report_Overload return Entity_Id;
6891 -- List possible interpretations, and specialize message in the
6892 -- case of a generic actual.
6894 function Within (Inner, Outer : Entity_Id) return Boolean;
6895 -- Determine whether a candidate subprogram is defined within the
6896 -- enclosing instance. If yes, it has precedence over outer candidates.
6898 --------------------------
6899 -- Find_Nearer_Entity --
6900 --------------------------
6902 function Find_Nearer_Entity
6905 Old2_S : Entity_Id) return Entity_Id
6913 New_F := First_Formal (New_S);
6914 Old1_F := First_Formal (Old1_S);
6915 Old2_F := First_Formal (Old2_S);
6917 -- The criterion is whether the type of the formals of one of Old1_S
6918 -- and Old2_S is an ancestor subtype of the type of the corresponding
6919 -- formals of New_S while the other is not (we already know that they
6920 -- are all subtypes of the same base type).
6922 -- This makes it possible to find the more correct renamed entity in
6923 -- the case of a generic instantiation nested in an enclosing one for
6924 -- which different formal types get the same actual type, which will
6925 -- in turn make it possible for Inherit_Renamed_Profile to preserve
6926 -- types on formal parameters and ultimately simplify disambiguation.
6928 -- Consider the follow package G:
6931 -- type Item_T is private;
6932 -- with function Compare (L, R: Item_T) return Boolean is <>;
6934 -- type Bound_T is private;
6935 -- with function Compare (L, R : Bound_T) return Boolean is <>;
6940 -- package body G is
6941 -- package My_Inner is Inner_G (Bound_T);
6945 -- with the following package Inner_G:
6948 -- type T is private;
6949 -- with function Compare (L, R: T) return Boolean is <>;
6950 -- package Inner_G is
6951 -- function "<" (L, R: T) return Boolean is (Compare (L, R));
6954 -- If G is instantiated on the same actual type with a single Compare
6958 -- function Compare (L, R : T) return Boolean;
6959 -- package My_G is new (T, T);
6961 -- then the renaming generated for Compare in the inner instantiation
6962 -- is ambiguous: it can rename either of the renamings generated for
6963 -- the outer instantiation. Now if the first one is picked up, then
6964 -- the subtypes of the formal parameters of the renaming will not be
6965 -- preserved in Inherit_Renamed_Profile because they are subtypes of
6966 -- the Bound_T formal type and not of the Item_T formal type, so we
6967 -- need to arrange for the second one to be picked up instead.
6969 while Present (New_F) loop
6970 if Etype (Old1_F) /= Etype (Old2_F) then
6971 Anc_T := Ancestor_Subtype (Etype (New_F));
6973 if Etype (Old1_F) = Anc_T then
6975 elsif Etype (Old2_F) = Anc_T then
6980 Next_Formal (New_F);
6981 Next_Formal (Old1_F);
6982 Next_Formal (Old2_F);
6985 pragma Assert (No (Old1_F));
6986 pragma Assert (No (Old2_F));
6989 end Find_Nearer_Entity;
6991 --------------------------
6992 -- Is_Visible_Operation --
6993 --------------------------
6995 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
7001 if Ekind (Op) /= E_Operator
7002 or else Scope (Op) /= Standard_Standard
7003 or else (In_Instance
7004 and then (not Is_Actual
7005 or else Present (Enclosing_Instance)))
7010 -- For a fixed point type operator, check the resulting type,
7011 -- because it may be a mixed mode integer * fixed operation.
7013 if Present (Next_Formal (First_Formal (New_S)))
7014 and then Is_Fixed_Point_Type (Etype (New_S))
7016 Typ := Etype (New_S);
7018 Typ := Etype (First_Formal (New_S));
7021 Btyp := Base_Type (Typ);
7023 if Nkind (Nam) /= N_Expanded_Name then
7024 return (In_Open_Scopes (Scope (Btyp))
7025 or else Is_Potentially_Use_Visible (Btyp)
7026 or else In_Use (Btyp)
7027 or else In_Use (Scope (Btyp)));
7030 Scop := Entity (Prefix (Nam));
7032 if Ekind (Scop) = E_Package
7033 and then Present (Renamed_Object (Scop))
7035 Scop := Renamed_Object (Scop);
7038 -- Operator is visible if prefix of expanded name denotes
7039 -- scope of type, or else type is defined in System_Aux
7040 -- and the prefix denotes System.
7042 return Scope (Btyp) = Scop
7043 or else (Scope (Btyp) = System_Aux_Id
7044 and then Scope (Scope (Btyp)) = Scop);
7047 end Is_Visible_Operation;
7053 function Within (Inner, Outer : Entity_Id) return Boolean is
7057 Sc := Scope (Inner);
7058 while Sc /= Standard_Standard loop
7069 ---------------------
7070 -- Report_Overload --
7071 ---------------------
7073 function Report_Overload return Entity_Id is
7076 Error_Msg_NE -- CODEFIX
7077 ("ambiguous actual subprogram&, " &
7078 "possible interpretations:", N, Nam);
7080 Error_Msg_N -- CODEFIX
7081 ("ambiguous subprogram, " &
7082 "possible interpretations:", N);
7085 List_Interps (Nam, N);
7087 end Report_Overload;
7089 -- Start of processing for Find_Renamed_Entity
7093 Candidate_Renaming := Empty;
7095 if Is_Overloaded (Nam) then
7096 Get_First_Interp (Nam, Ind, It);
7097 while Present (It.Nam) loop
7098 if Entity_Matches_Spec (It.Nam, New_S)
7099 and then Is_Visible_Operation (It.Nam)
7101 if Old_S /= Any_Id then
7103 -- Note: The call to Disambiguate only happens if a
7104 -- previous interpretation was found, in which case I1
7105 -- has received a value.
7107 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
7109 if It1 = No_Interp then
7110 Inst := Enclosing_Instance;
7112 if Present (Inst) then
7113 if Within (It.Nam, Inst) then
7114 if Within (Old_S, Inst) then
7116 It_D : constant Uint := Scope_Depth (It.Nam);
7117 Old_D : constant Uint := Scope_Depth (Old_S);
7120 -- Choose the innermost subprogram, which
7121 -- would hide the outer one in the generic.
7123 if Old_D > It_D then
7125 elsif It_D > Old_D then
7129 -- Otherwise, if we can determine that one
7130 -- of the entities is nearer to the renaming
7131 -- than the other, choose it. If not, then
7132 -- return the newer one as done historically.
7135 Find_Nearer_Entity (New_S, Old_S, It.Nam);
7136 if Present (N_Ent) then
7144 elsif Within (Old_S, Inst) then
7148 return Report_Overload;
7151 -- If not within an instance, ambiguity is real
7154 return Report_Overload;
7168 Present (First_Formal (It.Nam))
7169 and then Present (First_Formal (New_S))
7170 and then (Base_Type (Etype (First_Formal (It.Nam))) =
7171 Base_Type (Etype (First_Formal (New_S))))
7173 Candidate_Renaming := It.Nam;
7176 Get_Next_Interp (Ind, It);
7179 Set_Entity (Nam, Old_S);
7181 if Old_S /= Any_Id then
7182 Set_Is_Overloaded (Nam, False);
7185 -- Non-overloaded case
7189 and then Present (Enclosing_Instance)
7190 and then Entity_Matches_Spec (Entity (Nam), New_S)
7192 Old_S := Entity (Nam);
7194 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
7195 Candidate_Renaming := New_S;
7197 if Is_Visible_Operation (Entity (Nam)) then
7198 Old_S := Entity (Nam);
7201 elsif Present (First_Formal (Entity (Nam)))
7202 and then Present (First_Formal (New_S))
7203 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
7204 Base_Type (Etype (First_Formal (New_S))))
7206 Candidate_Renaming := Entity (Nam);
7211 end Find_Renamed_Entity;
7213 -----------------------------
7214 -- Find_Selected_Component --
7215 -----------------------------
7217 procedure Find_Selected_Component (N : Node_Id) is
7218 P : constant Node_Id := Prefix (N);
7221 -- Entity denoted by prefix
7228 function Available_Subtype return Boolean;
7229 -- A small optimization: if the prefix is constrained and the component
7230 -- is an array type we may already have a usable subtype for it, so we
7231 -- can use it rather than generating a new one, because the bounds
7232 -- will be the values of the discriminants and not discriminant refs.
7233 -- This simplifies value tracing in GNATprove. For consistency, both
7234 -- the entity name and the subtype come from the constrained component.
7236 -- This is only used in GNATprove mode: when generating code it may be
7237 -- necessary to create an itype in the scope of use of the selected
7238 -- component, e.g. in the context of a expanded record equality.
7240 function Is_Reference_In_Subunit return Boolean;
7241 -- In a subunit, the scope depth is not a proper measure of hiding,
7242 -- because the context of the proper body may itself hide entities in
7243 -- parent units. This rare case requires inspecting the tree directly
7244 -- because the proper body is inserted in the main unit and its context
7245 -- is simply added to that of the parent.
7247 -----------------------
7248 -- Available_Subtype --
7249 -----------------------
7251 function Available_Subtype return Boolean is
7255 if GNATprove_Mode then
7256 Comp := First_Entity (Etype (P));
7257 while Present (Comp) loop
7258 if Chars (Comp) = Chars (Selector_Name (N)) then
7259 Set_Etype (N, Etype (Comp));
7260 Set_Entity (Selector_Name (N), Comp);
7261 Set_Etype (Selector_Name (N), Etype (Comp));
7265 Next_Component (Comp);
7270 end Available_Subtype;
7272 -----------------------------
7273 -- Is_Reference_In_Subunit --
7274 -----------------------------
7276 function Is_Reference_In_Subunit return Boolean is
7278 Comp_Unit : Node_Id;
7282 while Present (Comp_Unit)
7283 and then Nkind (Comp_Unit) /= N_Compilation_Unit
7285 Comp_Unit := Parent (Comp_Unit);
7288 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
7292 -- Now check whether the package is in the context of the subunit
7294 Clause := First (Context_Items (Comp_Unit));
7295 while Present (Clause) loop
7296 if Nkind (Clause) = N_With_Clause
7297 and then Entity (Name (Clause)) = P_Name
7306 end Is_Reference_In_Subunit;
7308 -- Start of processing for Find_Selected_Component
7313 if Nkind (P) = N_Error then
7317 -- If the selector already has an entity, the node has been constructed
7318 -- in the course of expansion, and is known to be valid. Do not verify
7319 -- that it is defined for the type (it may be a private component used
7320 -- in the expansion of record equality).
7322 if Present (Entity (Selector_Name (N))) then
7323 if No (Etype (N)) or else Etype (N) = Any_Type then
7325 Sel_Name : constant Node_Id := Selector_Name (N);
7326 Selector : constant Entity_Id := Entity (Sel_Name);
7330 Set_Etype (Sel_Name, Etype (Selector));
7332 if not Is_Entity_Name (P) then
7336 -- Build an actual subtype except for the first parameter
7337 -- of an init proc, where this actual subtype is by
7338 -- definition incorrect, since the object is uninitialized
7339 -- (and does not even have defined discriminants etc.)
7341 if Is_Entity_Name (P)
7342 and then Ekind (Entity (P)) = E_Function
7344 Nam := New_Copy (P);
7346 if Is_Overloaded (P) then
7347 Save_Interps (P, Nam);
7350 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
7352 Analyze_Selected_Component (N);
7355 elsif Ekind (Selector) = E_Component
7356 and then (not Is_Entity_Name (P)
7357 or else Chars (Entity (P)) /= Name_uInit)
7359 -- Check if we already have an available subtype we can use
7361 if Ekind (Etype (P)) = E_Record_Subtype
7362 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
7363 and then Is_Array_Type (Etype (Selector))
7364 and then not Is_Packed (Etype (Selector))
7365 and then Available_Subtype
7369 -- Do not build the subtype when referencing components of
7370 -- dispatch table wrappers. Required to avoid generating
7371 -- elaboration code with HI runtimes.
7373 elsif RTU_Loaded (Ada_Tags)
7375 ((RTE_Available (RE_Dispatch_Table_Wrapper)
7376 and then Scope (Selector) =
7377 RTE (RE_Dispatch_Table_Wrapper))
7379 (RTE_Available (RE_No_Dispatch_Table_Wrapper)
7380 and then Scope (Selector) =
7381 RTE (RE_No_Dispatch_Table_Wrapper)))
7386 Build_Actual_Subtype_Of_Component
7387 (Etype (Selector), N);
7394 if No (C_Etype) then
7395 C_Etype := Etype (Selector);
7397 Insert_Action (N, C_Etype);
7398 C_Etype := Defining_Identifier (C_Etype);
7401 Set_Etype (N, C_Etype);
7404 -- If the selected component appears within a default expression
7405 -- and it has an actual subtype, the preanalysis has not yet
7406 -- completed its analysis, because Insert_Actions is disabled in
7407 -- that context. Within the init proc of the enclosing type we
7408 -- must complete this analysis, if an actual subtype was created.
7410 elsif Inside_Init_Proc then
7412 Typ : constant Entity_Id := Etype (N);
7413 Decl : constant Node_Id := Declaration_Node (Typ);
7415 if Nkind (Decl) = N_Subtype_Declaration
7416 and then not Analyzed (Decl)
7417 and then Is_List_Member (Decl)
7418 and then No (Parent (Decl))
7421 Insert_Action (N, Decl);
7428 elsif Is_Entity_Name (P) then
7429 P_Name := Entity (P);
7431 -- The prefix may denote an enclosing type which is the completion
7432 -- of an incomplete type declaration.
7434 if Is_Type (P_Name) then
7435 Set_Entity (P, Get_Full_View (P_Name));
7436 Set_Etype (P, Entity (P));
7437 P_Name := Entity (P);
7440 P_Type := Base_Type (Etype (P));
7442 if Debug_Flag_E then
7443 Write_Str ("Found prefix type to be ");
7444 Write_Entity_Info (P_Type, " "); Write_Eol;
7447 -- If the prefix's type is an access type, get to the record type
7449 if Is_Access_Type (P_Type) then
7450 P_Type := Implicitly_Designated_Type (P_Type);
7453 -- First check for components of a record object (not the
7454 -- result of a call, which is handled below).
7456 if Has_Components (P_Type)
7457 and then not Is_Overloadable (P_Name)
7458 and then not Is_Type (P_Name)
7460 -- Selected component of record. Type checking will validate
7461 -- name of selector.
7463 -- ??? Could we rewrite an implicit dereference into an explicit
7466 Analyze_Selected_Component (N);
7468 -- Reference to type name in predicate/invariant expression
7470 elsif (Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type))
7471 and then not In_Open_Scopes (P_Name)
7472 and then (not Is_Concurrent_Type (Etype (P_Name))
7473 or else not In_Open_Scopes (Etype (P_Name)))
7475 -- Call to protected operation or entry. Type checking is
7476 -- needed on the prefix.
7478 Analyze_Selected_Component (N);
7480 elsif (In_Open_Scopes (P_Name)
7481 and then Ekind (P_Name) /= E_Void
7482 and then not Is_Overloadable (P_Name))
7483 or else (Is_Concurrent_Type (Etype (P_Name))
7484 and then In_Open_Scopes (Etype (P_Name)))
7486 -- Prefix denotes an enclosing loop, block, or task, i.e. an
7487 -- enclosing construct that is not a subprogram or accept.
7489 -- A special case: a protected body may call an operation
7490 -- on an external object of the same type, in which case it
7491 -- is not an expanded name. If the prefix is the type itself,
7492 -- or the context is a single synchronized object it can only
7493 -- be interpreted as an expanded name.
7495 if Is_Concurrent_Type (Etype (P_Name)) then
7497 or else Present (Anonymous_Object (Etype (P_Name)))
7499 Find_Expanded_Name (N);
7502 Analyze_Selected_Component (N);
7507 Find_Expanded_Name (N);
7510 elsif Ekind (P_Name) = E_Package then
7511 Find_Expanded_Name (N);
7513 elsif Is_Overloadable (P_Name) then
7515 -- The subprogram may be a renaming (of an enclosing scope) as
7516 -- in the case of the name of the generic within an instantiation.
7518 if Ekind (P_Name) in E_Procedure | E_Function
7519 and then Present (Alias (P_Name))
7520 and then Is_Generic_Instance (Alias (P_Name))
7522 P_Name := Alias (P_Name);
7525 if Is_Overloaded (P) then
7527 -- The prefix must resolve to a unique enclosing construct
7530 Found : Boolean := False;
7535 Get_First_Interp (P, Ind, It);
7536 while Present (It.Nam) loop
7537 if In_Open_Scopes (It.Nam) then
7540 "prefix must be unique enclosing scope", N);
7541 Set_Entity (N, Any_Id);
7542 Set_Etype (N, Any_Type);
7551 Get_Next_Interp (Ind, It);
7556 if In_Open_Scopes (P_Name) then
7557 Set_Entity (P, P_Name);
7558 Set_Is_Overloaded (P, False);
7559 Find_Expanded_Name (N);
7562 -- If no interpretation as an expanded name is possible, it
7563 -- must be a selected component of a record returned by a
7564 -- function call. Reformat prefix as a function call, the rest
7565 -- is done by type resolution.
7567 -- Error if the prefix is procedure or entry, as is P.X
7569 if Ekind (P_Name) /= E_Function
7571 (not Is_Overloaded (P)
7572 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
7574 -- Prefix may mention a package that is hidden by a local
7575 -- declaration: let the user know. Scan the full homonym
7576 -- chain, the candidate package may be anywhere on it.
7578 if Present (Homonym (Current_Entity (P_Name))) then
7579 P_Name := Current_Entity (P_Name);
7581 while Present (P_Name) loop
7582 exit when Ekind (P_Name) = E_Package;
7583 P_Name := Homonym (P_Name);
7586 if Present (P_Name) then
7587 if not Is_Reference_In_Subunit then
7588 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
7590 ("package& is hidden by declaration#", N, P_Name);
7593 Set_Entity (Prefix (N), P_Name);
7594 Find_Expanded_Name (N);
7598 P_Name := Entity (Prefix (N));
7603 ("invalid prefix in selected component&", N, P_Name);
7604 Change_Selected_Component_To_Expanded_Name (N);
7605 Set_Entity (N, Any_Id);
7606 Set_Etype (N, Any_Type);
7608 -- Here we have a function call, so do the reformatting
7611 Nam := New_Copy (P);
7612 Save_Interps (P, Nam);
7614 -- We use Replace here because this is one of those cases
7615 -- where the parser has missclassified the node, and we fix
7616 -- things up and then do the semantic analysis on the fixed
7617 -- up node. Normally we do this using one of the Sinfo.CN
7618 -- routines, but this is too tricky for that.
7620 -- Note that using Rewrite would be wrong, because we would
7621 -- have a tree where the original node is unanalyzed.
7624 Make_Function_Call (Sloc (P), Name => Nam));
7626 -- Now analyze the reformatted node
7630 -- If the prefix is illegal after this transformation, there
7631 -- may be visibility errors on the prefix. The safest is to
7632 -- treat the selected component as an error.
7634 if Error_Posted (P) then
7635 Set_Etype (N, Any_Type);
7639 Analyze_Selected_Component (N);
7644 -- Remaining cases generate various error messages
7647 -- Format node as expanded name, to avoid cascaded errors
7649 Change_Selected_Component_To_Expanded_Name (N);
7650 Set_Entity (N, Any_Id);
7651 Set_Etype (N, Any_Type);
7653 -- Issue error message, but avoid this if error issued already.
7654 -- Use identifier of prefix if one is available.
7656 if P_Name = Any_Id then
7659 -- It is not an error if the prefix is the current instance of
7660 -- type name, e.g. the expression of a type aspect, when it is
7661 -- analyzed within a generic unit. We still have to verify that a
7662 -- component of that name exists, and decorate the node
7665 elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then
7670 Comp := First_Entity (Entity (P));
7671 while Present (Comp) loop
7672 if Chars (Comp) = Chars (Selector_Name (N)) then
7673 Set_Entity (N, Comp);
7674 Set_Etype (N, Etype (Comp));
7675 Set_Entity (Selector_Name (N), Comp);
7676 Set_Etype (Selector_Name (N), Etype (Comp));
7684 elsif Ekind (P_Name) = E_Void then
7685 Premature_Usage (P);
7687 elsif Nkind (P) /= N_Attribute_Reference then
7689 -- This may have been meant as a prefixed call to a primitive
7690 -- of an untagged type. If it is a function call check type of
7691 -- its first formal and add explanation.
7694 F : constant Entity_Id :=
7695 Current_Entity (Selector_Name (N));
7698 and then Is_Overloadable (F)
7699 and then Present (First_Entity (F))
7700 and then not Is_Tagged_Type (Etype (First_Entity (F)))
7703 ("prefixed call is only allowed for objects of a "
7704 & "tagged type", N);
7708 Error_Msg_N ("invalid prefix in selected component&", P);
7710 if Is_Incomplete_Type (P_Type)
7711 and then Is_Access_Type (Etype (P))
7714 ("\dereference must not be of an incomplete type "
7715 & "(RM 3.10.1)", P);
7719 Error_Msg_N ("invalid prefix in selected component", P);
7723 -- If prefix is not the name of an entity, it must be an expression,
7724 -- whose type is appropriate for a record. This is determined by
7727 Analyze_Selected_Component (N);
7730 Analyze_Dimension (N);
7731 end Find_Selected_Component;
7737 procedure Find_Type (N : Node_Id) is
7747 elsif Nkind (N) = N_Attribute_Reference then
7749 -- Class attribute. This is not valid in Ada 83 mode, but we do not
7750 -- need to enforce that at this point, since the declaration of the
7751 -- tagged type in the prefix would have been flagged already.
7753 if Attribute_Name (N) = Name_Class then
7754 Check_Restriction (No_Dispatch, N);
7755 Find_Type (Prefix (N));
7757 -- Propagate error from bad prefix
7759 if Etype (Prefix (N)) = Any_Type then
7760 Set_Entity (N, Any_Type);
7761 Set_Etype (N, Any_Type);
7765 T := Base_Type (Entity (Prefix (N)));
7767 -- Case where type is not known to be tagged. Its appearance in
7768 -- the prefix of the 'Class attribute indicates that the full view
7771 if not Is_Tagged_Type (T) then
7772 if Ekind (T) = E_Incomplete_Type then
7774 -- It is legal to denote the class type of an incomplete
7775 -- type. The full type will have to be tagged, of course.
7776 -- In Ada 2005 this usage is declared obsolescent, so we
7777 -- warn accordingly. This usage is only legal if the type
7778 -- is completed in the current scope, and not for a limited
7781 if Ada_Version >= Ada_2005 then
7783 -- Test whether the Available_View of a limited type view
7784 -- is tagged, since the limited view may not be marked as
7785 -- tagged if the type itself has an untagged incomplete
7786 -- type view in its package.
7788 if From_Limited_With (T)
7789 and then not Is_Tagged_Type (Available_View (T))
7792 ("prefix of Class attribute must be tagged", N);
7793 Set_Etype (N, Any_Type);
7794 Set_Entity (N, Any_Type);
7797 -- ??? This test is temporarily disabled (always
7798 -- False) because it causes an unwanted warning on
7799 -- GNAT sources (built with -gnatg, which includes
7800 -- Warn_On_Obsolescent_ Feature). Once this issue
7801 -- is cleared in the sources, it can be enabled.
7803 elsif Warn_On_Obsolescent_Feature and then False then
7805 ("applying 'Class to an untagged incomplete type"
7806 & " is an obsolescent feature (RM J.11)?r?", N);
7810 Set_Is_Tagged_Type (T);
7811 Set_Direct_Primitive_Operations (T, New_Elmt_List);
7812 Make_Class_Wide_Type (T);
7813 Set_Entity (N, Class_Wide_Type (T));
7814 Set_Etype (N, Class_Wide_Type (T));
7816 elsif Ekind (T) = E_Private_Type
7817 and then not Is_Generic_Type (T)
7818 and then In_Private_Part (Scope (T))
7820 -- The Class attribute can be applied to an untagged private
7821 -- type fulfilled by a tagged type prior to the full type
7822 -- declaration (but only within the parent package's private
7823 -- part). Create the class-wide type now and check that the
7824 -- full type is tagged later during its analysis. Note that
7825 -- we do not mark the private type as tagged, unlike the
7826 -- case of incomplete types, because the type must still
7827 -- appear untagged to outside units.
7829 if No (Class_Wide_Type (T)) then
7830 Make_Class_Wide_Type (T);
7833 Set_Entity (N, Class_Wide_Type (T));
7834 Set_Etype (N, Class_Wide_Type (T));
7837 -- Should we introduce a type Any_Tagged and use Wrong_Type
7838 -- here, it would be a bit more consistent???
7841 ("tagged type required, found}",
7842 Prefix (N), First_Subtype (T));
7843 Set_Entity (N, Any_Type);
7847 -- Case of tagged type
7850 if Is_Concurrent_Type (T) then
7851 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
7853 -- Previous error. Create a class-wide type for the
7854 -- synchronized type itself, with minimal semantic
7855 -- attributes, to catch other errors in some ACATS tests.
7857 pragma Assert (Serious_Errors_Detected /= 0);
7858 Make_Class_Wide_Type (T);
7859 C := Class_Wide_Type (T);
7860 Set_First_Entity (C, First_Entity (T));
7863 C := Class_Wide_Type
7864 (Corresponding_Record_Type (Entity (Prefix (N))));
7868 C := Class_Wide_Type (Entity (Prefix (N)));
7871 Set_Entity_With_Checks (N, C);
7872 Generate_Reference (C, N);
7876 -- Base attribute, not allowed in Ada 83
7878 elsif Attribute_Name (N) = Name_Base then
7879 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
7881 ("(Ada 83) Base attribute not allowed in subtype mark", N);
7884 Find_Type (Prefix (N));
7885 Typ := Entity (Prefix (N));
7887 if Ada_Version >= Ada_95
7888 and then not Is_Scalar_Type (Typ)
7889 and then not Is_Generic_Type (Typ)
7892 ("prefix of Base attribute must be scalar type",
7895 elsif Warn_On_Redundant_Constructs
7896 and then Base_Type (Typ) = Typ
7898 Error_Msg_NE -- CODEFIX
7899 ("redundant attribute, & is its own base type?r?", N, Typ);
7902 T := Base_Type (Typ);
7904 -- Rewrite attribute reference with type itself (see similar
7905 -- processing in Analyze_Attribute, case Base). Preserve prefix
7906 -- if present, for other legality checks.
7908 if Nkind (Prefix (N)) = N_Expanded_Name then
7910 Make_Expanded_Name (Sloc (N),
7912 Prefix => New_Copy (Prefix (Prefix (N))),
7913 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
7916 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
7923 elsif Attribute_Name (N) = Name_Stub_Type then
7925 -- This is handled in Analyze_Attribute
7929 -- All other attributes are invalid in a subtype mark
7932 Error_Msg_N ("invalid attribute in subtype mark", N);
7938 if Is_Entity_Name (N) then
7939 T_Name := Entity (N);
7941 Error_Msg_N ("subtype mark required in this context", N);
7942 Set_Etype (N, Any_Type);
7946 if T_Name = Any_Id or else Etype (N) = Any_Type then
7948 -- Undefined id. Make it into a valid type
7950 Set_Entity (N, Any_Type);
7952 elsif not Is_Type (T_Name)
7953 and then T_Name /= Standard_Void_Type
7955 Error_Msg_Sloc := Sloc (T_Name);
7956 Error_Msg_N ("subtype mark required in this context", N);
7957 Error_Msg_NE ("\\found & declared#", N, T_Name);
7958 Set_Entity (N, Any_Type);
7961 -- If the type is an incomplete type created to handle
7962 -- anonymous access components of a record type, then the
7963 -- incomplete type is the visible entity and subsequent
7964 -- references will point to it. Mark the original full
7965 -- type as referenced, to prevent spurious warnings.
7967 if Is_Incomplete_Type (T_Name)
7968 and then Present (Full_View (T_Name))
7969 and then not Comes_From_Source (T_Name)
7971 Set_Referenced (Full_View (T_Name));
7974 T_Name := Get_Full_View (T_Name);
7976 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
7977 -- limited-with clauses
7979 if From_Limited_With (T_Name)
7980 and then Is_Incomplete_Type (T_Name)
7981 and then Present (Non_Limited_View (T_Name))
7982 and then Is_Interface (Non_Limited_View (T_Name))
7984 T_Name := Non_Limited_View (T_Name);
7987 if In_Open_Scopes (T_Name) then
7988 if Ekind (Base_Type (T_Name)) = E_Task_Type then
7990 -- In Ada 2005, a task name can be used in an access
7991 -- definition within its own body. It cannot be used
7992 -- in the discriminant part of the task declaration,
7993 -- nor anywhere else in the declaration because entries
7994 -- cannot have access parameters.
7996 if Ada_Version >= Ada_2005
7997 and then Nkind (Parent (N)) = N_Access_Definition
7999 Set_Entity (N, T_Name);
8000 Set_Etype (N, T_Name);
8002 if Has_Completion (T_Name) then
8007 ("task type cannot be used as type mark " &
8008 "within its own declaration", N);
8013 ("task type cannot be used as type mark " &
8014 "within its own spec or body", N);
8017 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
8019 -- In Ada 2005, a protected name can be used in an access
8020 -- definition within its own body.
8022 if Ada_Version >= Ada_2005
8023 and then Nkind (Parent (N)) = N_Access_Definition
8025 Set_Entity (N, T_Name);
8026 Set_Etype (N, T_Name);
8031 ("protected type cannot be used as type mark " &
8032 "within its own spec or body", N);
8036 Error_Msg_N ("type declaration cannot refer to itself", N);
8039 Set_Etype (N, Any_Type);
8040 Set_Entity (N, Any_Type);
8041 Set_Error_Posted (T_Name);
8045 Set_Entity (N, T_Name);
8046 Set_Etype (N, T_Name);
8050 if Present (Etype (N)) and then Comes_From_Source (N) then
8051 if Is_Fixed_Point_Type (Etype (N)) then
8052 Check_Restriction (No_Fixed_Point, N);
8053 elsif Is_Floating_Point_Type (Etype (N)) then
8054 Check_Restriction (No_Floating_Point, N);
8057 -- A Ghost type must appear in a specific context
8059 if Is_Ghost_Entity (Etype (N)) then
8060 Check_Ghost_Context (Etype (N), N);
8065 --------------------
8066 -- Has_Components --
8067 --------------------
8069 function Has_Components (Typ : Entity_Id) return Boolean is
8071 return Is_Record_Type (Typ)
8072 or else (Is_Private_Type (Typ) and then Has_Discriminants (Typ))
8073 or else (Is_Task_Type (Typ) and then Has_Discriminants (Typ))
8074 or else (Is_Incomplete_Type (Typ)
8075 and then From_Limited_With (Typ)
8076 and then Is_Record_Type (Available_View (Typ)));
8079 ------------------------------------
8080 -- Has_Implicit_Character_Literal --
8081 ------------------------------------
8083 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
8085 Found : Boolean := False;
8086 P : constant Entity_Id := Entity (Prefix (N));
8087 Priv_Id : Entity_Id := Empty;
8090 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8091 Priv_Id := First_Private_Entity (P);
8094 if P = Standard_Standard then
8095 Change_Selected_Component_To_Expanded_Name (N);
8096 Rewrite (N, Selector_Name (N));
8098 Set_Etype (Original_Node (N), Standard_Character);
8102 Id := First_Entity (P);
8103 while Present (Id) and then Id /= Priv_Id loop
8104 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
8106 -- We replace the node with the literal itself, resolve as a
8107 -- character, and set the type correctly.
8110 Change_Selected_Component_To_Expanded_Name (N);
8111 Rewrite (N, Selector_Name (N));
8114 Set_Etype (Original_Node (N), Id);
8118 -- More than one type derived from Character in given scope.
8119 -- Collect all possible interpretations.
8121 Add_One_Interp (N, Id, Id);
8129 end Has_Implicit_Character_Literal;
8131 ----------------------
8132 -- Has_Private_With --
8133 ----------------------
8135 function Has_Private_With (E : Entity_Id) return Boolean is
8136 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
8140 Item := First (Context_Items (Comp_Unit));
8141 while Present (Item) loop
8142 if Nkind (Item) = N_With_Clause
8143 and then Private_Present (Item)
8144 and then Entity (Name (Item)) = E
8153 end Has_Private_With;
8155 ---------------------------
8156 -- Has_Implicit_Operator --
8157 ---------------------------
8159 function Has_Implicit_Operator (N : Node_Id) return Boolean is
8160 Op_Id : constant Name_Id := Chars (Selector_Name (N));
8161 P : constant Entity_Id := Entity (Prefix (N));
8163 Priv_Id : Entity_Id := Empty;
8165 procedure Add_Implicit_Operator
8167 Op_Type : Entity_Id := Empty);
8168 -- Add implicit interpretation to node N, using the type for which a
8169 -- predefined operator exists. If the operator yields a boolean type,
8170 -- the Operand_Type is implicitly referenced by the operator, and a
8171 -- reference to it must be generated.
8173 ---------------------------
8174 -- Add_Implicit_Operator --
8175 ---------------------------
8177 procedure Add_Implicit_Operator
8179 Op_Type : Entity_Id := Empty)
8181 Predef_Op : Entity_Id;
8184 Predef_Op := Current_Entity (Selector_Name (N));
8185 while Present (Predef_Op)
8186 and then Scope (Predef_Op) /= Standard_Standard
8188 Predef_Op := Homonym (Predef_Op);
8191 if Nkind (N) = N_Selected_Component then
8192 Change_Selected_Component_To_Expanded_Name (N);
8195 -- If the context is an unanalyzed function call, determine whether
8196 -- a binary or unary interpretation is required.
8198 if Nkind (Parent (N)) = N_Indexed_Component then
8200 Is_Binary_Call : constant Boolean :=
8202 (Next (First (Expressions (Parent (N)))));
8203 Is_Binary_Op : constant Boolean :=
8205 (Predef_Op) /= Last_Entity (Predef_Op);
8206 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
8209 if Is_Binary_Call then
8210 if Is_Binary_Op then
8211 Add_One_Interp (N, Predef_Op, T);
8213 Add_One_Interp (N, Predef_Op2, T);
8216 if not Is_Binary_Op then
8217 Add_One_Interp (N, Predef_Op, T);
8219 -- Predef_Op2 may be empty in case of previous errors
8221 elsif Present (Predef_Op2) then
8222 Add_One_Interp (N, Predef_Op2, T);
8228 Add_One_Interp (N, Predef_Op, T);
8230 -- For operators with unary and binary interpretations, if
8231 -- context is not a call, add both
8233 if Present (Homonym (Predef_Op)) then
8234 Add_One_Interp (N, Homonym (Predef_Op), T);
8238 -- The node is a reference to a predefined operator, and
8239 -- an implicit reference to the type of its operands.
8241 if Present (Op_Type) then
8242 Generate_Operator_Reference (N, Op_Type);
8244 Generate_Operator_Reference (N, T);
8246 end Add_Implicit_Operator;
8248 -- Start of processing for Has_Implicit_Operator
8251 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8252 Priv_Id := First_Private_Entity (P);
8255 Id := First_Entity (P);
8259 -- Boolean operators: an implicit declaration exists if the scope
8260 -- contains a declaration for a derived Boolean type, or for an
8261 -- array of Boolean type.
8268 while Id /= Priv_Id loop
8269 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
8270 Add_Implicit_Operator (Id);
8277 -- Equality: look for any non-limited type (result is Boolean)
8282 while Id /= Priv_Id loop
8284 and then not Is_Limited_Type (Id)
8285 and then Is_Base_Type (Id)
8287 Add_Implicit_Operator (Standard_Boolean, Id);
8294 -- Comparison operators: scalar type, or array of scalar
8301 while Id /= Priv_Id loop
8302 if (Is_Scalar_Type (Id)
8303 or else (Is_Array_Type (Id)
8304 and then Is_Scalar_Type (Component_Type (Id))))
8305 and then Is_Base_Type (Id)
8307 Add_Implicit_Operator (Standard_Boolean, Id);
8314 -- Arithmetic operators: any numeric type
8325 while Id /= Priv_Id loop
8326 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
8327 Add_Implicit_Operator (Id);
8334 -- Concatenation: any one-dimensional array type
8336 when Name_Op_Concat =>
8337 while Id /= Priv_Id loop
8338 if Is_Array_Type (Id)
8339 and then Number_Dimensions (Id) = 1
8340 and then Is_Base_Type (Id)
8342 Add_Implicit_Operator (Id);
8349 -- What is the others condition here? Should we be using a
8350 -- subtype of Name_Id that would restrict to operators ???
8356 -- If we fall through, then we do not have an implicit operator
8359 end Has_Implicit_Operator;
8361 -----------------------------------
8362 -- Has_Loop_In_Inner_Open_Scopes --
8363 -----------------------------------
8365 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
8367 -- Several scope stacks are maintained by Scope_Stack. The base of the
8368 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8369 -- flag in the scope stack entry. Note that the scope stacks used to
8370 -- simply be delimited implicitly by the presence of Standard_Standard
8371 -- at their base, but there now are cases where this is not sufficient
8372 -- because Standard_Standard actually may appear in the middle of the
8373 -- active set of scopes.
8375 for J in reverse 0 .. Scope_Stack.Last loop
8377 -- S was reached without seing a loop scope first
8379 if Scope_Stack.Table (J).Entity = S then
8382 -- S was not yet reached, so it contains at least one inner loop
8384 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
8388 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8389 -- cases where Standard_Standard appears in the middle of the active
8390 -- set of scopes. This affects the declaration and overriding of
8391 -- private inherited operations in instantiations of generic child
8394 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
8397 raise Program_Error; -- unreachable
8398 end Has_Loop_In_Inner_Open_Scopes;
8400 --------------------
8401 -- In_Open_Scopes --
8402 --------------------
8404 function In_Open_Scopes (S : Entity_Id) return Boolean is
8406 -- Several scope stacks are maintained by Scope_Stack. The base of the
8407 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8408 -- flag in the scope stack entry. Note that the scope stacks used to
8409 -- simply be delimited implicitly by the presence of Standard_Standard
8410 -- at their base, but there now are cases where this is not sufficient
8411 -- because Standard_Standard actually may appear in the middle of the
8412 -- active set of scopes.
8414 for J in reverse 0 .. Scope_Stack.Last loop
8415 if Scope_Stack.Table (J).Entity = S then
8419 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8420 -- cases where Standard_Standard appears in the middle of the active
8421 -- set of scopes. This affects the declaration and overriding of
8422 -- private inherited operations in instantiations of generic child
8425 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
8431 -----------------------------
8432 -- Inherit_Renamed_Profile --
8433 -----------------------------
8435 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
8442 if Ekind (Old_S) = E_Operator then
8443 New_F := First_Formal (New_S);
8445 while Present (New_F) loop
8446 Set_Etype (New_F, Base_Type (Etype (New_F)));
8447 Next_Formal (New_F);
8450 Set_Etype (New_S, Base_Type (Etype (New_S)));
8453 New_F := First_Formal (New_S);
8454 Old_F := First_Formal (Old_S);
8456 while Present (New_F) loop
8457 New_T := Etype (New_F);
8458 Old_T := Etype (Old_F);
8460 -- If the new type is a renaming of the old one, as is the case
8461 -- for actuals in instances, retain its name, to simplify later
8464 if Nkind (Parent (New_T)) = N_Subtype_Declaration
8465 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
8466 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
8470 Set_Etype (New_F, Old_T);
8473 Next_Formal (New_F);
8474 Next_Formal (Old_F);
8477 pragma Assert (No (Old_F));
8479 if Ekind (Old_S) in E_Function | E_Enumeration_Literal then
8480 Set_Etype (New_S, Etype (Old_S));
8483 end Inherit_Renamed_Profile;
8489 procedure Initialize is
8494 -------------------------
8495 -- Install_Use_Clauses --
8496 -------------------------
8498 procedure Install_Use_Clauses
8500 Force_Installation : Boolean := False)
8506 while Present (U) loop
8508 -- Case of USE package
8510 if Nkind (U) = N_Use_Package_Clause then
8511 Use_One_Package (U, Name (U), True);
8516 Use_One_Type (Subtype_Mark (U), Force => Force_Installation);
8520 Next_Use_Clause (U);
8522 end Install_Use_Clauses;
8524 ----------------------
8525 -- Mark_Use_Clauses --
8526 ----------------------
8528 procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is
8529 procedure Mark_Parameters (Call : Entity_Id);
8530 -- Perform use_type_clause marking for all parameters in a subprogram
8531 -- or operator call.
8533 procedure Mark_Use_Package (Pak : Entity_Id);
8534 -- Move up the Prev_Use_Clause chain for packages denoted by Pak -
8535 -- marking each clause in the chain as effective in the process.
8537 procedure Mark_Use_Type (E : Entity_Id);
8538 -- Similar to Do_Use_Package_Marking except we move up the
8539 -- Prev_Use_Clause chain for the type denoted by E.
8541 ---------------------
8542 -- Mark_Parameters --
8543 ---------------------
8545 procedure Mark_Parameters (Call : Entity_Id) is
8549 -- Move through all of the formals
8551 Curr := First_Formal (Call);
8552 while Present (Curr) loop
8553 Mark_Use_Type (Curr);
8558 -- Handle the return type
8560 Mark_Use_Type (Call);
8561 end Mark_Parameters;
8563 ----------------------
8564 -- Mark_Use_Package --
8565 ----------------------
8567 procedure Mark_Use_Package (Pak : Entity_Id) is
8571 -- Ignore cases where the scope of the type is not a package (e.g.
8572 -- Standard_Standard).
8574 if Ekind (Pak) /= E_Package then
8578 Curr := Current_Use_Clause (Pak);
8579 while Present (Curr)
8580 and then not Is_Effective_Use_Clause (Curr)
8582 -- We need to mark the previous use clauses as effective, but
8583 -- each use clause may in turn render other use_package_clauses
8584 -- effective. Additionally, it is possible to have a parent
8585 -- package renamed as a child of itself so we must check the
8586 -- prefix entity is not the same as the package we are marking.
8588 if Nkind (Name (Curr)) /= N_Identifier
8589 and then Present (Prefix (Name (Curr)))
8590 and then Entity (Prefix (Name (Curr))) /= Pak
8592 Mark_Use_Package (Entity (Prefix (Name (Curr))));
8594 -- It is also possible to have a child package without a prefix
8595 -- that relies on a previous use_package_clause.
8597 elsif Nkind (Name (Curr)) = N_Identifier
8598 and then Is_Child_Unit (Entity (Name (Curr)))
8600 Mark_Use_Package (Scope (Entity (Name (Curr))));
8603 -- Mark the use_package_clause as effective and move up the chain
8605 Set_Is_Effective_Use_Clause (Curr);
8607 Curr := Prev_Use_Clause (Curr);
8609 end Mark_Use_Package;
8615 procedure Mark_Use_Type (E : Entity_Id) is
8620 -- Ignore void types and unresolved string literals and primitives
8622 if Nkind (E) = N_String_Literal
8623 or else Nkind (Etype (E)) not in N_Entity
8624 or else not Is_Type (Etype (E))
8629 -- Primitives with class-wide operands might additionally render
8630 -- their base type's use_clauses effective - so do a recursive check
8633 Base := Base_Type (Etype (E));
8635 if Ekind (Base) = E_Class_Wide_Type then
8636 Mark_Use_Type (Base);
8639 -- The package containing the type or operator function being used
8640 -- may be in use as well, so mark any use_package_clauses for it as
8641 -- effective. There are also additional sanity checks performed here
8642 -- for ignoring previous errors.
8644 Mark_Use_Package (Scope (Base));
8646 if Nkind (E) in N_Op
8647 and then Present (Entity (E))
8648 and then Present (Scope (Entity (E)))
8650 Mark_Use_Package (Scope (Entity (E)));
8653 Curr := Current_Use_Clause (Base);
8654 while Present (Curr)
8655 and then not Is_Effective_Use_Clause (Curr)
8657 -- Current use_type_clause may render other use_package_clauses
8660 if Nkind (Subtype_Mark (Curr)) /= N_Identifier
8661 and then Present (Prefix (Subtype_Mark (Curr)))
8663 Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr))));
8666 -- Mark the use_type_clause as effective and move up the chain
8668 Set_Is_Effective_Use_Clause (Curr);
8670 Curr := Prev_Use_Clause (Curr);
8674 -- Start of processing for Mark_Use_Clauses
8677 -- Use clauses in and of themselves do not count as a "use" of a
8680 if Nkind (Parent (Id)) in N_Use_Package_Clause | N_Use_Type_Clause then
8686 if Nkind (Id) in N_Entity then
8688 -- Mark the entity's package
8690 if Is_Potentially_Use_Visible (Id) then
8691 Mark_Use_Package (Scope (Id));
8694 -- Mark enumeration literals
8696 if Ekind (Id) = E_Enumeration_Literal then
8701 elsif (Ekind (Id) in Overloadable_Kind
8702 or else Ekind (Id) in
8703 E_Generic_Function | E_Generic_Procedure)
8704 and then (Is_Potentially_Use_Visible (Id)
8705 or else Is_Intrinsic_Subprogram (Id)
8706 or else (Ekind (Id) in E_Function | E_Procedure
8707 and then Is_Generic_Actual_Subprogram (Id)))
8709 Mark_Parameters (Id);
8717 if Nkind (Id) in N_Op then
8719 -- At this point the left operand may not be resolved if we are
8720 -- encountering multiple operators next to eachother in an
8723 if Nkind (Id) in N_Binary_Op
8724 and then not (Nkind (Left_Opnd (Id)) in N_Op)
8726 Mark_Use_Type (Left_Opnd (Id));
8729 Mark_Use_Type (Right_Opnd (Id));
8732 -- Mark entity identifiers
8734 elsif Nkind (Id) in N_Has_Entity
8735 and then (Is_Potentially_Use_Visible (Entity (Id))
8736 or else (Is_Generic_Instance (Entity (Id))
8737 and then Is_Immediately_Visible (Entity (Id))))
8739 -- Ignore fully qualified names as they do not count as a "use" of
8742 if Nkind (Id) in N_Identifier | N_Operator_Symbol
8743 or else (Present (Prefix (Id))
8744 and then Scope (Entity (Id)) /= Entity (Prefix (Id)))
8746 Mark_Use_Clauses (Entity (Id));
8750 end Mark_Use_Clauses;
8752 --------------------------------
8753 -- Most_Descendant_Use_Clause --
8754 --------------------------------
8756 function Most_Descendant_Use_Clause
8757 (Clause1 : Entity_Id;
8758 Clause2 : Entity_Id) return Entity_Id
8764 if Clause1 = Clause2 then
8768 -- We determine which one is the most descendant by the scope distance
8769 -- to the ultimate parent unit.
8771 Scope1 := Entity_Of_Unit (Unit (Parent (Clause1)));
8772 Scope2 := Entity_Of_Unit (Unit (Parent (Clause2)));
8773 while Scope1 /= Standard_Standard
8774 and then Scope2 /= Standard_Standard
8776 Scope1 := Scope (Scope1);
8777 Scope2 := Scope (Scope2);
8779 if not Present (Scope1) then
8781 elsif not Present (Scope2) then
8786 if Scope1 = Standard_Standard then
8791 end Most_Descendant_Use_Clause;
8797 procedure Pop_Scope is
8798 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8799 S : constant Entity_Id := SST.Entity;
8802 if Debug_Flag_E then
8806 -- Set Default_Storage_Pool field of the library unit if necessary
8808 if Is_Package_Or_Generic_Package (S)
8810 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
8813 Aux : constant Node_Id :=
8814 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
8816 if No (Default_Storage_Pool (Aux)) then
8817 Set_Default_Storage_Pool (Aux, Default_Pool);
8822 Scope_Suppress := SST.Save_Scope_Suppress;
8823 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
8824 Check_Policy_List := SST.Save_Check_Policy_List;
8825 Default_Pool := SST.Save_Default_Storage_Pool;
8826 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
8827 SPARK_Mode := SST.Save_SPARK_Mode;
8828 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
8829 Default_SSO := SST.Save_Default_SSO;
8830 Uneval_Old := SST.Save_Uneval_Old;
8832 if Debug_Flag_W then
8833 Write_Str ("<-- exiting scope: ");
8834 Write_Name (Chars (Current_Scope));
8835 Write_Str (", Depth=");
8836 Write_Int (Int (Scope_Stack.Last));
8840 End_Use_Clauses (SST.First_Use_Clause);
8842 -- If the actions to be wrapped are still there they will get lost
8843 -- causing incomplete code to be generated. It is better to abort in
8844 -- this case (and we do the abort even with assertions off since the
8845 -- penalty is incorrect code generation).
8847 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
8848 raise Program_Error;
8851 -- Free last subprogram name if allocated, and pop scope
8853 Free (SST.Last_Subprogram_Name);
8854 Scope_Stack.Decrement_Last;
8861 procedure Push_Scope (S : Entity_Id) is
8862 E : constant Entity_Id := Scope (S);
8865 if Ekind (S) = E_Void then
8868 -- Set scope depth if not a non-concurrent type, and we have not yet set
8869 -- the scope depth. This means that we have the first occurrence of the
8870 -- scope, and this is where the depth is set.
8872 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
8873 and then not Scope_Depth_Set (S)
8875 if S = Standard_Standard then
8876 Set_Scope_Depth_Value (S, Uint_0);
8878 elsif Is_Child_Unit (S) then
8879 Set_Scope_Depth_Value (S, Uint_1);
8881 elsif not Is_Record_Type (Current_Scope) then
8882 if Ekind (S) = E_Loop then
8883 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
8885 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
8890 Scope_Stack.Increment_Last;
8893 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8897 SST.Save_Scope_Suppress := Scope_Suppress;
8898 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
8899 SST.Save_Check_Policy_List := Check_Policy_List;
8900 SST.Save_Default_Storage_Pool := Default_Pool;
8901 SST.Save_No_Tagged_Streams := No_Tagged_Streams;
8902 SST.Save_SPARK_Mode := SPARK_Mode;
8903 SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma;
8904 SST.Save_Default_SSO := Default_SSO;
8905 SST.Save_Uneval_Old := Uneval_Old;
8907 -- Each new scope pushed onto the scope stack inherits the component
8908 -- alignment of the previous scope. This emulates the "visibility"
8909 -- semantics of pragma Component_Alignment.
8911 if Scope_Stack.Last > Scope_Stack.First then
8912 SST.Component_Alignment_Default :=
8914 (Scope_Stack.Last - 1).Component_Alignment_Default;
8916 -- Otherwise, this is the first scope being pushed on the scope
8917 -- stack. Inherit the component alignment from the configuration
8918 -- form of pragma Component_Alignment (if any).
8921 SST.Component_Alignment_Default :=
8922 Configuration_Component_Alignment;
8925 SST.Last_Subprogram_Name := null;
8926 SST.Is_Transient := False;
8927 SST.Node_To_Be_Wrapped := Empty;
8928 SST.Pending_Freeze_Actions := No_List;
8929 SST.Actions_To_Be_Wrapped := (others => No_List);
8930 SST.First_Use_Clause := Empty;
8931 SST.Is_Active_Stack_Base := False;
8932 SST.Previous_Visibility := False;
8933 SST.Locked_Shared_Objects := No_Elist;
8936 if Debug_Flag_W then
8937 Write_Str ("--> new scope: ");
8938 Write_Name (Chars (Current_Scope));
8939 Write_Str (", Id=");
8940 Write_Int (Int (Current_Scope));
8941 Write_Str (", Depth=");
8942 Write_Int (Int (Scope_Stack.Last));
8946 -- Deal with copying flags from the previous scope to this one. This is
8947 -- not necessary if either scope is standard, or if the new scope is a
8950 if S /= Standard_Standard
8951 and then Scope (S) /= Standard_Standard
8952 and then not Is_Child_Unit (S)
8954 if Nkind (E) not in N_Entity then
8958 -- Copy categorization flags from Scope (S) to S, this is not done
8959 -- when Scope (S) is Standard_Standard since propagation is from
8960 -- library unit entity inwards. Copy other relevant attributes as
8961 -- well (Discard_Names in particular).
8963 -- We only propagate inwards for library level entities,
8964 -- inner level subprograms do not inherit the categorization.
8966 if Is_Library_Level_Entity (S) then
8967 Set_Is_Preelaborated (S, Is_Preelaborated (E));
8968 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
8969 Set_Discard_Names (S, Discard_Names (E));
8970 Set_Suppress_Value_Tracking_On_Call
8971 (S, Suppress_Value_Tracking_On_Call (E));
8972 Set_Categorization_From_Scope (E => S, Scop => E);
8976 if Is_Child_Unit (S)
8977 and then Present (E)
8978 and then Is_Package_Or_Generic_Package (E)
8980 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
8983 Aux : constant Node_Id :=
8984 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
8986 if Present (Default_Storage_Pool (Aux)) then
8987 Default_Pool := Default_Storage_Pool (Aux);
8993 ---------------------
8994 -- Premature_Usage --
8995 ---------------------
8997 procedure Premature_Usage (N : Node_Id) is
8998 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
8999 E : Entity_Id := Entity (N);
9002 -- Within an instance, the analysis of the actual for a formal object
9003 -- does not see the name of the object itself. This is significant only
9004 -- if the object is an aggregate, where its analysis does not do any
9005 -- name resolution on component associations. (see 4717-008). In such a
9006 -- case, look for the visible homonym on the chain.
9008 if In_Instance and then Present (Homonym (E)) then
9010 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
9016 Set_Etype (N, Etype (E));
9022 when N_Component_Declaration =>
9024 ("component&! cannot be used before end of record declaration",
9027 when N_Parameter_Specification =>
9029 ("formal parameter&! cannot be used before end of specification",
9032 when N_Discriminant_Specification =>
9034 ("discriminant&! cannot be used before end of discriminant part",
9037 when N_Procedure_Specification | N_Function_Specification =>
9039 ("subprogram&! cannot be used before end of its declaration",
9042 when N_Full_Type_Declaration | N_Subtype_Declaration =>
9044 ("type& cannot be used before end of its declaration!", N);
9048 ("object& cannot be used before end of its declaration!", N);
9050 -- If the premature reference appears as the expression in its own
9051 -- declaration, rewrite it to prevent compiler loops in subsequent
9052 -- uses of this mangled declaration in address clauses.
9054 if Nkind (Parent (N)) = N_Object_Declaration then
9055 Set_Entity (N, Any_Id);
9058 end Premature_Usage;
9060 ------------------------
9061 -- Present_System_Aux --
9062 ------------------------
9064 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
9066 Aux_Name : Unit_Name_Type;
9067 Unum : Unit_Number_Type;
9072 function Find_System (C_Unit : Node_Id) return Entity_Id;
9073 -- Scan context clause of compilation unit to find with_clause
9080 function Find_System (C_Unit : Node_Id) return Entity_Id is
9081 With_Clause : Node_Id;
9084 With_Clause := First (Context_Items (C_Unit));
9085 while Present (With_Clause) loop
9086 if (Nkind (With_Clause) = N_With_Clause
9087 and then Chars (Name (With_Clause)) = Name_System)
9088 and then Comes_From_Source (With_Clause)
9099 -- Start of processing for Present_System_Aux
9102 -- The child unit may have been loaded and analyzed already
9104 if Present (System_Aux_Id) then
9107 -- If no previous pragma for System.Aux, nothing to load
9109 elsif No (System_Extend_Unit) then
9112 -- Use the unit name given in the pragma to retrieve the unit.
9113 -- Verify that System itself appears in the context clause of the
9114 -- current compilation. If System is not present, an error will
9115 -- have been reported already.
9118 With_Sys := Find_System (Cunit (Current_Sem_Unit));
9120 The_Unit := Unit (Cunit (Current_Sem_Unit));
9124 (Nkind (The_Unit) = N_Package_Body
9125 or else (Nkind (The_Unit) = N_Subprogram_Body
9126 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
9128 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
9131 if No (With_Sys) and then Present (N) then
9133 -- If we are compiling a subunit, we need to examine its
9134 -- context as well (Current_Sem_Unit is the parent unit);
9136 The_Unit := Parent (N);
9137 while Nkind (The_Unit) /= N_Compilation_Unit loop
9138 The_Unit := Parent (The_Unit);
9141 if Nkind (Unit (The_Unit)) = N_Subunit then
9142 With_Sys := Find_System (The_Unit);
9146 if No (With_Sys) then
9150 Loc := Sloc (With_Sys);
9151 Get_Name_String (Chars (Expression (System_Extend_Unit)));
9152 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
9153 Name_Buffer (1 .. 7) := "system.";
9154 Name_Buffer (Name_Len + 8) := '%';
9155 Name_Buffer (Name_Len + 9) := 's';
9156 Name_Len := Name_Len + 9;
9157 Aux_Name := Name_Find;
9161 (Load_Name => Aux_Name,
9164 Error_Node => With_Sys);
9166 if Unum /= No_Unit then
9167 Semantics (Cunit (Unum));
9169 Defining_Entity (Specification (Unit (Cunit (Unum))));
9172 Make_With_Clause (Loc,
9174 Make_Expanded_Name (Loc,
9175 Chars => Chars (System_Aux_Id),
9177 New_Occurrence_Of (Scope (System_Aux_Id), Loc),
9178 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
9180 Set_Entity (Name (Withn), System_Aux_Id);
9182 Set_Corresponding_Spec (Withn, System_Aux_Id);
9183 Set_First_Name (Withn);
9184 Set_Implicit_With (Withn);
9185 Set_Library_Unit (Withn, Cunit (Unum));
9187 Insert_After (With_Sys, Withn);
9188 Mark_Rewrite_Insertion (Withn);
9189 Set_Context_Installed (Withn);
9193 -- Here if unit load failed
9196 Error_Msg_Name_1 := Name_System;
9197 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
9199 ("extension package `%.%` does not exist",
9200 Opt.System_Extend_Unit);
9204 end Present_System_Aux;
9206 -------------------------
9207 -- Restore_Scope_Stack --
9208 -------------------------
9210 procedure Restore_Scope_Stack
9212 Handle_Use : Boolean := True)
9214 SS_Last : constant Int := Scope_Stack.Last;
9218 -- Restore visibility of previous scope stack, if any, using the list
9219 -- we saved (we use Remove, since this list will not be used again).
9222 Elmt := Last_Elmt (List);
9223 exit when Elmt = No_Elmt;
9224 Set_Is_Immediately_Visible (Node (Elmt));
9225 Remove_Last_Elmt (List);
9228 -- Restore use clauses
9230 if SS_Last >= Scope_Stack.First
9231 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9235 (Scope_Stack.Table (SS_Last).First_Use_Clause,
9236 Force_Installation => True);
9238 end Restore_Scope_Stack;
9240 ----------------------
9241 -- Save_Scope_Stack --
9242 ----------------------
9244 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
9245 -- consuming any memory. That is, Save_Scope_Stack took care of removing
9246 -- from immediate visibility entities and Restore_Scope_Stack took care
9247 -- of restoring their visibility analyzing the context of each entity. The
9248 -- problem of such approach is that it was fragile and caused unexpected
9249 -- visibility problems, and indeed one test was found where there was a
9252 -- Furthermore, the following experiment was carried out:
9254 -- - Save_Scope_Stack was modified to store in an Elist1 all those
9255 -- entities whose attribute Is_Immediately_Visible is modified
9256 -- from True to False.
9258 -- - Restore_Scope_Stack was modified to store in another Elist2
9259 -- all the entities whose attribute Is_Immediately_Visible is
9260 -- modified from False to True.
9262 -- - Extra code was added to verify that all the elements of Elist1
9263 -- are found in Elist2
9265 -- This test shows that there may be more occurrences of this problem which
9266 -- have not yet been detected. As a result, we replaced that approach by
9267 -- the current one in which Save_Scope_Stack returns the list of entities
9268 -- whose visibility is changed, and that list is passed to Restore_Scope_
9269 -- Stack to undo that change. This approach is simpler and safer, although
9270 -- it consumes more memory.
9272 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
9273 Result : constant Elist_Id := New_Elmt_List;
9276 SS_Last : constant Int := Scope_Stack.Last;
9278 procedure Remove_From_Visibility (E : Entity_Id);
9279 -- If E is immediately visible then append it to the result and remove
9280 -- it temporarily from visibility.
9282 ----------------------------
9283 -- Remove_From_Visibility --
9284 ----------------------------
9286 procedure Remove_From_Visibility (E : Entity_Id) is
9288 if Is_Immediately_Visible (E) then
9289 Append_Elmt (E, Result);
9290 Set_Is_Immediately_Visible (E, False);
9292 end Remove_From_Visibility;
9294 -- Start of processing for Save_Scope_Stack
9297 if SS_Last >= Scope_Stack.First
9298 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9301 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
9304 -- If the call is from within a compilation unit, as when called from
9305 -- Rtsfind, make current entries in scope stack invisible while we
9306 -- analyze the new unit.
9308 for J in reverse 0 .. SS_Last loop
9309 exit when Scope_Stack.Table (J).Entity = Standard_Standard
9310 or else No (Scope_Stack.Table (J).Entity);
9312 S := Scope_Stack.Table (J).Entity;
9314 Remove_From_Visibility (S);
9316 E := First_Entity (S);
9317 while Present (E) loop
9318 Remove_From_Visibility (E);
9326 end Save_Scope_Stack;
9332 procedure Set_Use (L : List_Id) is
9338 while Present (Decl) loop
9339 if Nkind (Decl) = N_Use_Package_Clause then
9340 Chain_Use_Clause (Decl);
9341 Use_One_Package (Decl, Name (Decl));
9343 elsif Nkind (Decl) = N_Use_Type_Clause then
9344 Chain_Use_Clause (Decl);
9345 Use_One_Type (Subtype_Mark (Decl));
9354 -----------------------------
9355 -- Update_Use_Clause_Chain --
9356 -----------------------------
9358 procedure Update_Use_Clause_Chain is
9360 procedure Update_Chain_In_Scope (Level : Int);
9361 -- Iterate through one level in the scope stack verifying each use-type
9362 -- clause within said level is used then reset the Current_Use_Clause
9363 -- to a redundant use clause outside of the current ending scope if such
9366 ---------------------------
9367 -- Update_Chain_In_Scope --
9368 ---------------------------
9370 procedure Update_Chain_In_Scope (Level : Int) is
9375 -- Loop through all use clauses within the scope dictated by Level
9377 Curr := Scope_Stack.Table (Level).First_Use_Clause;
9378 while Present (Curr) loop
9380 -- Retrieve the subtype mark or name within the current current
9383 if Nkind (Curr) = N_Use_Type_Clause then
9384 N := Subtype_Mark (Curr);
9389 -- If warnings for unreferenced entities are enabled and the
9390 -- current use clause has not been marked effective.
9392 if Check_Unreferenced
9393 and then Comes_From_Source (Curr)
9394 and then not Is_Effective_Use_Clause (Curr)
9395 and then not In_Instance
9396 and then not In_Inlined_Body
9398 -- We are dealing with a potentially unused use_package_clause
9400 if Nkind (Curr) = N_Use_Package_Clause then
9402 -- Renamings and formal subprograms may cause the associated
9403 -- node to be marked as effective instead of the original.
9405 if not (Present (Associated_Node (N))
9408 (Associated_Node (N)))
9409 and then Is_Effective_Use_Clause
9411 (Associated_Node (N))))
9413 Error_Msg_Node_1 := Entity (N);
9415 ("use clause for package & has no effect?u?",
9419 -- We are dealing with an unused use_type_clause
9422 Error_Msg_Node_1 := Etype (N);
9424 ("use clause for } has no effect?u?", Curr, Etype (N));
9428 -- Verify that we haven't already processed a redundant
9429 -- use_type_clause within the same scope before we move the
9430 -- current use clause up to a previous one for type T.
9432 if Present (Prev_Use_Clause (Curr)) then
9433 Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr));
9436 Next_Use_Clause (Curr);
9438 end Update_Chain_In_Scope;
9440 -- Start of processing for Update_Use_Clause_Chain
9443 Update_Chain_In_Scope (Scope_Stack.Last);
9445 -- Deal with use clauses within the context area if the current
9446 -- scope is a compilation unit.
9448 if Is_Compilation_Unit (Current_Scope)
9449 and then Sloc (Scope_Stack.Table
9450 (Scope_Stack.Last - 1).Entity) = Standard_Location
9452 Update_Chain_In_Scope (Scope_Stack.Last - 1);
9454 end Update_Use_Clause_Chain;
9456 ---------------------
9457 -- Use_One_Package --
9458 ---------------------
9460 procedure Use_One_Package
9462 Pack_Name : Entity_Id := Empty;
9463 Force : Boolean := False)
9465 procedure Note_Redundant_Use (Clause : Node_Id);
9466 -- Mark the name in a use clause as redundant if the corresponding
9467 -- entity is already use-visible. Emit a warning if the use clause comes
9468 -- from source and the proper warnings are enabled.
9470 ------------------------
9471 -- Note_Redundant_Use --
9472 ------------------------
9474 procedure Note_Redundant_Use (Clause : Node_Id) is
9475 Decl : constant Node_Id := Parent (Clause);
9476 Pack_Name : constant Entity_Id := Entity (Clause);
9478 Cur_Use : Node_Id := Current_Use_Clause (Pack_Name);
9479 Prev_Use : Node_Id := Empty;
9480 Redundant : Node_Id := Empty;
9481 -- The Use_Clause which is actually redundant. In the simplest case
9482 -- it is Pack itself, but when we compile a body we install its
9483 -- context before that of its spec, in which case it is the
9484 -- use_clause in the spec that will appear to be redundant, and we
9485 -- want the warning to be placed on the body. Similar complications
9486 -- appear when the redundancy is between a child unit and one of its
9490 -- Could be renamed...
9492 if No (Cur_Use) then
9493 Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name));
9496 Set_Redundant_Use (Clause, True);
9498 -- Do not check for redundant use if clause is generated, or in an
9499 -- instance, or in a predefined unit to avoid misleading warnings
9500 -- that may occur as part of a rtsfind load.
9502 if not Comes_From_Source (Clause)
9504 or else not Warn_On_Redundant_Constructs
9505 or else Is_Predefined_Unit (Current_Sem_Unit)
9510 if not Is_Compilation_Unit (Current_Scope) then
9512 -- If the use_clause is in an inner scope, it is made redundant by
9513 -- some clause in the current context, with one exception: If we
9514 -- are compiling a nested package body, and the use_clause comes
9515 -- from then corresponding spec, the clause is not necessarily
9516 -- fully redundant, so we should not warn. If a warning was
9517 -- warranted, it would have been given when the spec was
9520 if Nkind (Parent (Decl)) = N_Package_Specification then
9522 Package_Spec_Entity : constant Entity_Id :=
9523 Defining_Unit_Name (Parent (Decl));
9525 if In_Package_Body (Package_Spec_Entity) then
9531 Redundant := Clause;
9532 Prev_Use := Cur_Use;
9534 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9536 Cur_Unit : constant Unit_Number_Type :=
9537 Get_Source_Unit (Cur_Use);
9538 New_Unit : constant Unit_Number_Type :=
9539 Get_Source_Unit (Clause);
9544 if Cur_Unit = New_Unit then
9546 -- Redundant clause in same body
9548 Redundant := Clause;
9549 Prev_Use := Cur_Use;
9551 elsif Cur_Unit = Current_Sem_Unit then
9553 -- If the new clause is not in the current unit it has been
9554 -- analyzed first, and it makes the other one redundant.
9555 -- However, if the new clause appears in a subunit, Cur_Unit
9556 -- is still the parent, and in that case the redundant one
9557 -- is the one appearing in the subunit.
9559 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
9560 Redundant := Clause;
9561 Prev_Use := Cur_Use;
9563 -- Most common case: redundant clause in body, original
9564 -- clause in spec. Current scope is spec entity.
9566 elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then
9567 Redundant := Cur_Use;
9571 -- The new clause may appear in an unrelated unit, when
9572 -- the parents of a generic are being installed prior to
9573 -- instantiation. In this case there must be no warning.
9574 -- We detect this case by checking whether the current
9575 -- top of the stack is related to the current
9578 Scop := Current_Scope;
9579 while Present (Scop)
9580 and then Scop /= Standard_Standard
9582 if Is_Compilation_Unit (Scop)
9583 and then not Is_Child_Unit (Scop)
9587 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
9591 Scop := Scope (Scop);
9594 Redundant := Cur_Use;
9598 elsif New_Unit = Current_Sem_Unit then
9599 Redundant := Clause;
9600 Prev_Use := Cur_Use;
9603 -- Neither is the current unit, so they appear in parent or
9604 -- sibling units. Warning will be emitted elsewhere.
9610 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
9611 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
9613 -- Use_clause is in child unit of current unit, and the child unit
9614 -- appears in the context of the body of the parent, so it has
9615 -- been installed first, even though it is the redundant one.
9616 -- Depending on their placement in the context, the visible or the
9617 -- private parts of the two units, either might appear as
9618 -- redundant, but the message has to be on the current unit.
9620 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
9621 Redundant := Cur_Use;
9624 Redundant := Clause;
9625 Prev_Use := Cur_Use;
9628 -- If the new use clause appears in the private part of a parent
9629 -- unit it may appear to be redundant w.r.t. a use clause in a
9630 -- child unit, but the previous use clause was needed in the
9631 -- visible part of the child, and no warning should be emitted.
9633 if Nkind (Parent (Decl)) = N_Package_Specification
9634 and then List_Containing (Decl) =
9635 Private_Declarations (Parent (Decl))
9638 Par : constant Entity_Id :=
9639 Defining_Entity (Parent (Decl));
9640 Spec : constant Node_Id :=
9641 Specification (Unit (Cunit (Current_Sem_Unit)));
9642 Cur_List : constant List_Id := List_Containing (Cur_Use);
9645 if Is_Compilation_Unit (Par)
9646 and then Par /= Cunit_Entity (Current_Sem_Unit)
9648 if Cur_List = Context_Items (Cunit (Current_Sem_Unit))
9649 or else Cur_List = Visible_Declarations (Spec)
9657 -- Finally, if the current use clause is in the context then the
9658 -- clause is redundant when it is nested within the unit.
9660 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
9661 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
9662 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
9664 Redundant := Clause;
9665 Prev_Use := Cur_Use;
9668 if Present (Redundant) and then Parent (Redundant) /= Prev_Use then
9670 -- Make sure we are looking at most-descendant use_package_clause
9671 -- by traversing the chain with Find_Most_Prev and then verifying
9672 -- there is no scope manipulation via Most_Descendant_Use_Clause.
9674 if Nkind (Prev_Use) = N_Use_Package_Clause
9676 (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit
9677 or else Most_Descendant_Use_Clause
9678 (Prev_Use, Find_Most_Prev (Prev_Use)) /= Prev_Use)
9680 Prev_Use := Find_Most_Prev (Prev_Use);
9683 Error_Msg_Sloc := Sloc (Prev_Use);
9684 Error_Msg_NE -- CODEFIX
9685 ("& is already use-visible through previous use_clause #?r?",
9686 Redundant, Pack_Name);
9688 end Note_Redundant_Use;
9692 Current_Instance : Entity_Id := Empty;
9696 Private_With_OK : Boolean := False;
9699 -- Start of processing for Use_One_Package
9702 -- Use_One_Package may have been called recursively to handle an
9703 -- implicit use for a auxiliary system package, so set P accordingly
9704 -- and skip redundancy checks.
9706 if No (Pack_Name) and then Present_System_Aux (N) then
9709 -- Check for redundant use_package_clauses
9712 -- Ignore cases where we are dealing with a non user defined package
9713 -- like Standard_Standard or something other than a valid package.
9715 if not Is_Entity_Name (Pack_Name)
9716 or else No (Entity (Pack_Name))
9717 or else Ekind (Entity (Pack_Name)) /= E_Package
9722 -- When a renaming exists we must check it for redundancy. The
9723 -- original package would have already been seen at this point.
9725 if Present (Renamed_Object (Entity (Pack_Name))) then
9726 P := Renamed_Object (Entity (Pack_Name));
9728 P := Entity (Pack_Name);
9731 -- Check for redundant clauses then set the current use clause for
9732 -- P if were are not "forcing" an installation from a scope
9733 -- reinstallation that is done throughout analysis for various
9737 Note_Redundant_Use (Pack_Name);
9740 Set_Current_Use_Clause (P, N);
9745 -- Warn about detected redundant clauses
9748 and then In_Open_Scopes (P)
9749 and then not Is_Hidden_Open_Scope (P)
9751 if Warn_On_Redundant_Constructs and then P = Current_Scope then
9752 Error_Msg_NE -- CODEFIX
9753 ("& is already use-visible within itself?r?",
9760 -- Set P back to the non-renamed package so that visiblilty of the
9761 -- entities within the package can be properly set below.
9763 P := Entity (Pack_Name);
9767 Set_Current_Use_Clause (P, N);
9769 -- Ada 2005 (AI-50217): Check restriction
9771 if From_Limited_With (P) then
9772 Error_Msg_N ("limited withed package cannot appear in use clause", N);
9775 -- Find enclosing instance, if any
9778 Current_Instance := Current_Scope;
9779 while not Is_Generic_Instance (Current_Instance) loop
9780 Current_Instance := Scope (Current_Instance);
9783 if No (Hidden_By_Use_Clause (N)) then
9784 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
9788 -- If unit is a package renaming, indicate that the renamed package is
9789 -- also in use (the flags on both entities must remain consistent, and a
9790 -- subsequent use of either of them should be recognized as redundant).
9792 if Present (Renamed_Object (P)) then
9793 Set_In_Use (Renamed_Object (P));
9794 Set_Current_Use_Clause (Renamed_Object (P), N);
9795 Real_P := Renamed_Object (P);
9800 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
9801 -- found in the private part of a package specification
9803 if In_Private_Part (Current_Scope)
9804 and then Has_Private_With (P)
9805 and then Is_Child_Unit (Current_Scope)
9806 and then Is_Child_Unit (P)
9807 and then Is_Ancestor_Package (Scope (Current_Scope), P)
9809 Private_With_OK := True;
9812 -- Loop through entities in one package making them potentially
9815 Id := First_Entity (P);
9817 and then (Id /= First_Private_Entity (P)
9818 or else Private_With_OK) -- Ada 2005 (AI-262)
9820 Prev := Current_Entity (Id);
9821 while Present (Prev) loop
9822 if Is_Immediately_Visible (Prev)
9823 and then (not Is_Overloadable (Prev)
9824 or else not Is_Overloadable (Id)
9825 or else (Type_Conformant (Id, Prev)))
9827 if No (Current_Instance) then
9829 -- Potentially use-visible entity remains hidden
9831 goto Next_Usable_Entity;
9833 -- A use clause within an instance hides outer global entities,
9834 -- which are not used to resolve local entities in the
9835 -- instance. Note that the predefined entities in Standard
9836 -- could not have been hidden in the generic by a use clause,
9837 -- and therefore remain visible. Other compilation units whose
9838 -- entities appear in Standard must be hidden in an instance.
9840 -- To determine whether an entity is external to the instance
9841 -- we compare the scope depth of its scope with that of the
9842 -- current instance. However, a generic actual of a subprogram
9843 -- instance is declared in the wrapper package but will not be
9844 -- hidden by a use-visible entity. similarly, an entity that is
9845 -- declared in an enclosing instance will not be hidden by an
9846 -- an entity declared in a generic actual, which can only have
9847 -- been use-visible in the generic and will not have hidden the
9848 -- entity in the generic parent.
9850 -- If Id is called Standard, the predefined package with the
9851 -- same name is in the homonym chain. It has to be ignored
9852 -- because it has no defined scope (being the only entity in
9853 -- the system with this mandated behavior).
9855 elsif not Is_Hidden (Id)
9856 and then Present (Scope (Prev))
9857 and then not Is_Wrapper_Package (Scope (Prev))
9858 and then Scope_Depth (Scope (Prev)) <
9859 Scope_Depth (Current_Instance)
9860 and then (Scope (Prev) /= Standard_Standard
9861 or else Sloc (Prev) > Standard_Location)
9863 if In_Open_Scopes (Scope (Prev))
9864 and then Is_Generic_Instance (Scope (Prev))
9865 and then Present (Associated_Formal_Package (P))
9870 Set_Is_Potentially_Use_Visible (Id);
9871 Set_Is_Immediately_Visible (Prev, False);
9872 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
9876 -- A user-defined operator is not use-visible if the predefined
9877 -- operator for the type is immediately visible, which is the case
9878 -- if the type of the operand is in an open scope. This does not
9879 -- apply to user-defined operators that have operands of different
9880 -- types, because the predefined mixed mode operations (multiply
9881 -- and divide) apply to universal types and do not hide anything.
9883 elsif Ekind (Prev) = E_Operator
9884 and then Operator_Matches_Spec (Prev, Id)
9885 and then In_Open_Scopes
9886 (Scope (Base_Type (Etype (First_Formal (Id)))))
9887 and then (No (Next_Formal (First_Formal (Id)))
9888 or else Etype (First_Formal (Id)) =
9889 Etype (Next_Formal (First_Formal (Id)))
9890 or else Chars (Prev) = Name_Op_Expon)
9892 goto Next_Usable_Entity;
9894 -- In an instance, two homonyms may become use_visible through the
9895 -- actuals of distinct formal packages. In the generic, only the
9896 -- current one would have been visible, so make the other one
9899 -- In certain pathological cases it is possible that unrelated
9900 -- homonyms from distinct formal packages may exist in an
9901 -- uninstalled scope. We must test for that here.
9903 elsif Present (Current_Instance)
9904 and then Is_Potentially_Use_Visible (Prev)
9905 and then not Is_Overloadable (Prev)
9906 and then Scope (Id) /= Scope (Prev)
9907 and then Used_As_Generic_Actual (Scope (Prev))
9908 and then Used_As_Generic_Actual (Scope (Id))
9909 and then Is_List_Member (Scope (Prev))
9910 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
9911 Current_Use_Clause (Scope (Id)))
9913 Set_Is_Potentially_Use_Visible (Prev, False);
9914 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
9917 Prev := Homonym (Prev);
9920 -- On exit, we know entity is not hidden, unless it is private
9922 if not Is_Hidden (Id)
9923 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
9925 Set_Is_Potentially_Use_Visible (Id);
9927 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
9928 Set_Is_Potentially_Use_Visible (Full_View (Id));
9932 <<Next_Usable_Entity>>
9936 -- Child units are also made use-visible by a use clause, but they may
9937 -- appear after all visible declarations in the parent entity list.
9939 while Present (Id) loop
9940 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
9941 Set_Is_Potentially_Use_Visible (Id);
9947 if Chars (Real_P) = Name_System
9948 and then Scope (Real_P) = Standard_Standard
9949 and then Present_System_Aux (N)
9951 Use_One_Package (N);
9953 end Use_One_Package;
9959 procedure Use_One_Type
9961 Installed : Boolean := False;
9962 Force : Boolean := False)
9964 function Spec_Reloaded_For_Body return Boolean;
9965 -- Determine whether the compilation unit is a package body and the use
9966 -- type clause is in the spec of the same package. Even though the spec
9967 -- was analyzed first, its context is reloaded when analysing the body.
9969 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
9970 -- AI05-150: if the use_type_clause carries the "all" qualifier,
9971 -- class-wide operations of ancestor types are use-visible if the
9972 -- ancestor type is visible.
9974 ----------------------------
9975 -- Spec_Reloaded_For_Body --
9976 ----------------------------
9978 function Spec_Reloaded_For_Body return Boolean is
9980 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9982 Spec : constant Node_Id :=
9983 Parent (List_Containing (Parent (Id)));
9986 -- Check whether type is declared in a package specification,
9987 -- and current unit is the corresponding package body. The
9988 -- use clauses themselves may be within a nested package.
9991 Nkind (Spec) = N_Package_Specification
9992 and then In_Same_Source_Unit
9993 (Corresponding_Body (Parent (Spec)),
9994 Cunit_Entity (Current_Sem_Unit));
9999 end Spec_Reloaded_For_Body;
10001 -------------------------------
10002 -- Use_Class_Wide_Operations --
10003 -------------------------------
10005 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
10006 function Is_Class_Wide_Operation_Of
10008 T : Entity_Id) return Boolean;
10009 -- Determine whether a subprogram has a class-wide parameter or
10010 -- result that is T'Class.
10012 ---------------------------------
10013 -- Is_Class_Wide_Operation_Of --
10014 ---------------------------------
10016 function Is_Class_Wide_Operation_Of
10018 T : Entity_Id) return Boolean
10020 Formal : Entity_Id;
10023 Formal := First_Formal (Op);
10024 while Present (Formal) loop
10025 if Etype (Formal) = Class_Wide_Type (T) then
10029 Next_Formal (Formal);
10032 if Etype (Op) = Class_Wide_Type (T) then
10037 end Is_Class_Wide_Operation_Of;
10044 -- Start of processing for Use_Class_Wide_Operations
10047 Scop := Scope (Typ);
10048 if not Is_Hidden (Scop) then
10049 Ent := First_Entity (Scop);
10050 while Present (Ent) loop
10051 if Is_Overloadable (Ent)
10052 and then Is_Class_Wide_Operation_Of (Ent, Typ)
10053 and then not Is_Potentially_Use_Visible (Ent)
10055 Set_Is_Potentially_Use_Visible (Ent);
10056 Append_Elmt (Ent, Used_Operations (Parent (Id)));
10063 if Is_Derived_Type (Typ) then
10064 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
10066 end Use_Class_Wide_Operations;
10071 Is_Known_Used : Boolean;
10072 Op_List : Elist_Id;
10075 -- Start of processing for Use_One_Type
10078 if Entity (Id) = Any_Type then
10082 -- It is the type determined by the subtype mark (8.4(8)) whose
10083 -- operations become potentially use-visible.
10085 T := Base_Type (Entity (Id));
10087 -- Either the type itself is used, the package where it is declared is
10088 -- in use or the entity is declared in the current package, thus
10093 and then ((Present (Current_Use_Clause (T))
10094 and then All_Present (Current_Use_Clause (T)))
10095 or else not All_Present (Parent (Id))))
10096 or else In_Use (Scope (T))
10097 or else Scope (T) = Current_Scope;
10099 Set_Redundant_Use (Id,
10100 Is_Known_Used or else Is_Potentially_Use_Visible (T));
10102 if Ekind (T) = E_Incomplete_Type then
10103 Error_Msg_N ("premature usage of incomplete type", Id);
10105 elsif In_Open_Scopes (Scope (T)) then
10108 -- A limited view cannot appear in a use_type_clause. However, an access
10109 -- type whose designated type is limited has the flag but is not itself
10110 -- a limited view unless we only have a limited view of its enclosing
10113 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
10115 ("incomplete type from limited view cannot appear in use clause",
10118 -- If the use clause is redundant, Used_Operations will usually be
10119 -- empty, but we need to set it to empty here in one case: If we are
10120 -- instantiating a generic library unit, then we install the ancestors
10121 -- of that unit in the scope stack, which involves reprocessing use
10122 -- clauses in those ancestors. Such a use clause will typically have a
10123 -- nonempty Used_Operations unless it was redundant in the generic unit,
10124 -- even if it is redundant at the place of the instantiation.
10126 elsif Redundant_Use (Id) then
10128 -- We must avoid incorrectly setting the Current_Use_Clause when we
10129 -- are working with a redundant clause that has already been linked
10130 -- in the Prev_Use_Clause chain, otherwise the chain will break.
10132 if Present (Current_Use_Clause (T))
10133 and then Present (Prev_Use_Clause (Current_Use_Clause (T)))
10134 and then Parent (Id) = Prev_Use_Clause (Current_Use_Clause (T))
10138 Set_Current_Use_Clause (T, Parent (Id));
10141 Set_Used_Operations (Parent (Id), New_Elmt_List);
10143 -- If the subtype mark designates a subtype in a different package,
10144 -- we have to check that the parent type is visible, otherwise the
10145 -- use_type_clause is a no-op. Not clear how to do that???
10148 Set_Current_Use_Clause (T, Parent (Id));
10151 -- If T is tagged, primitive operators on class-wide operands are
10152 -- also deemed available. Note that this is really necessary only
10153 -- in semantics-only mode, because the primitive operators are not
10154 -- fully constructed in this mode, but we do it in all modes for the
10155 -- sake of uniformity, as this should not matter in practice.
10157 if Is_Tagged_Type (T) then
10158 Set_In_Use (Class_Wide_Type (T));
10161 -- Iterate over primitive operations of the type. If an operation is
10162 -- already use_visible, it is the result of a previous use_clause,
10163 -- and already appears on the corresponding entity chain. If the
10164 -- clause is being reinstalled, operations are already use-visible.
10170 Op_List := Collect_Primitive_Operations (T);
10171 Elmt := First_Elmt (Op_List);
10172 while Present (Elmt) loop
10173 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
10174 or else Chars (Node (Elmt)) in Any_Operator_Name)
10175 and then not Is_Hidden (Node (Elmt))
10176 and then not Is_Potentially_Use_Visible (Node (Elmt))
10178 Set_Is_Potentially_Use_Visible (Node (Elmt));
10179 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10181 elsif Ada_Version >= Ada_2012
10182 and then All_Present (Parent (Id))
10183 and then not Is_Hidden (Node (Elmt))
10184 and then not Is_Potentially_Use_Visible (Node (Elmt))
10186 Set_Is_Potentially_Use_Visible (Node (Elmt));
10187 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10194 if Ada_Version >= Ada_2012
10195 and then All_Present (Parent (Id))
10196 and then Is_Tagged_Type (T)
10198 Use_Class_Wide_Operations (T);
10202 -- If warning on redundant constructs, check for unnecessary WITH
10205 and then Warn_On_Redundant_Constructs
10206 and then Is_Known_Used
10208 -- with P; with P; use P;
10209 -- package P is package X is package body X is
10210 -- type T ... use P.T;
10212 -- The compilation unit is the body of X. GNAT first compiles the
10213 -- spec of X, then proceeds to the body. At that point P is marked
10214 -- as use visible. The analysis then reinstalls the spec along with
10215 -- its context. The use clause P.T is now recognized as redundant,
10216 -- but in the wrong context. Do not emit a warning in such cases.
10217 -- Do not emit a warning either if we are in an instance, there is
10218 -- no redundancy between an outer use_clause and one that appears
10219 -- within the generic.
10221 and then not Spec_Reloaded_For_Body
10222 and then not In_Instance
10223 and then not In_Inlined_Body
10225 -- The type already has a use clause
10229 -- Case where we know the current use clause for the type
10231 if Present (Current_Use_Clause (T)) then
10232 Use_Clause_Known : declare
10233 Clause1 : constant Node_Id :=
10234 Find_Most_Prev (Current_Use_Clause (T));
10235 Clause2 : constant Node_Id := Parent (Id);
10242 -- Start of processing for Use_Clause_Known
10245 -- If both current use_type_clause and the use_type_clause
10246 -- for the type are at the compilation unit level, one of
10247 -- the units must be an ancestor of the other, and the
10248 -- warning belongs on the descendant.
10250 if Nkind (Parent (Clause1)) = N_Compilation_Unit
10252 Nkind (Parent (Clause2)) = N_Compilation_Unit
10254 -- If the unit is a subprogram body that acts as spec,
10255 -- the context clause is shared with the constructed
10256 -- subprogram spec. Clearly there is no redundancy.
10258 if Clause1 = Clause2 then
10262 Unit1 := Unit (Parent (Clause1));
10263 Unit2 := Unit (Parent (Clause2));
10265 -- If both clauses are on same unit, or one is the body
10266 -- of the other, or one of them is in a subunit, report
10267 -- redundancy on the later one.
10269 if Unit1 = Unit2 or else Nkind (Unit1) = N_Subunit then
10270 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10271 Error_Msg_NE -- CODEFIX
10272 ("& is already use-visible through previous "
10273 & "use_type_clause #??", Clause1, T);
10276 elsif Nkind (Unit2) in N_Package_Body | N_Subprogram_Body
10277 and then Nkind (Unit1) /= Nkind (Unit2)
10278 and then Nkind (Unit1) /= N_Subunit
10280 Error_Msg_Sloc := Sloc (Clause1);
10281 Error_Msg_NE -- CODEFIX
10282 ("& is already use-visible through previous "
10283 & "use_type_clause #??", Current_Use_Clause (T), T);
10287 -- There is a redundant use_type_clause in a child unit.
10288 -- Determine which of the units is more deeply nested.
10289 -- If a unit is a package instance, retrieve the entity
10290 -- and its scope from the instance spec.
10292 Ent1 := Entity_Of_Unit (Unit1);
10293 Ent2 := Entity_Of_Unit (Unit2);
10295 if Scope (Ent2) = Standard_Standard then
10296 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10299 elsif Scope (Ent1) = Standard_Standard then
10300 Error_Msg_Sloc := Sloc (Id);
10303 -- If both units are child units, we determine which one
10304 -- is the descendant by the scope distance to the
10305 -- ultimate parent unit.
10313 S1 := Scope (Ent1);
10314 S2 := Scope (Ent2);
10316 and then Present (S2)
10317 and then S1 /= Standard_Standard
10318 and then S2 /= Standard_Standard
10324 if S1 = Standard_Standard then
10325 Error_Msg_Sloc := Sloc (Id);
10328 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10334 if Parent (Id) /= Err_No then
10335 if Most_Descendant_Use_Clause
10336 (Err_No, Parent (Id)) = Parent (Id)
10338 Error_Msg_Sloc := Sloc (Err_No);
10339 Err_No := Parent (Id);
10342 Error_Msg_NE -- CODEFIX
10343 ("& is already use-visible through previous "
10344 & "use_type_clause #??", Err_No, Id);
10347 -- Case where current use_type_clause and use_type_clause
10348 -- for the type are not both at the compilation unit level.
10349 -- In this case we don't have location information.
10352 Error_Msg_NE -- CODEFIX
10353 ("& is already use-visible through previous "
10354 & "use_type_clause??", Id, T);
10356 end Use_Clause_Known;
10358 -- Here if Current_Use_Clause is not set for T, another case where
10359 -- we do not have the location information available.
10362 Error_Msg_NE -- CODEFIX
10363 ("& is already use-visible through previous "
10364 & "use_type_clause??", Id, T);
10367 -- The package where T is declared is already used
10369 elsif In_Use (Scope (T)) then
10370 -- Due to expansion of contracts we could be attempting to issue
10371 -- a spurious warning - so verify there is a previous use clause.
10373 if Current_Use_Clause (Scope (T)) /=
10374 Find_Most_Prev (Current_Use_Clause (Scope (T)))
10377 Sloc (Find_Most_Prev (Current_Use_Clause (Scope (T))));
10378 Error_Msg_NE -- CODEFIX
10379 ("& is already use-visible through package use clause #??",
10383 -- The current scope is the package where T is declared
10386 Error_Msg_Node_2 := Scope (T);
10387 Error_Msg_NE -- CODEFIX
10388 ("& is already use-visible inside package &??", Id, T);
10397 procedure Write_Info is
10398 Id : Entity_Id := First_Entity (Current_Scope);
10401 -- No point in dumping standard entities
10403 if Current_Scope = Standard_Standard then
10407 Write_Str ("========================================================");
10409 Write_Str (" Defined Entities in ");
10410 Write_Name (Chars (Current_Scope));
10412 Write_Str ("========================================================");
10416 Write_Str ("-- none --");
10420 while Present (Id) loop
10421 Write_Entity_Info (Id, " ");
10426 if Scope (Current_Scope) = Standard_Standard then
10428 -- Print information on the current unit itself
10430 Write_Entity_Info (Current_Scope, " ");
10443 for J in reverse 1 .. Scope_Stack.Last loop
10444 S := Scope_Stack.Table (J).Entity;
10445 Write_Int (Int (S));
10446 Write_Str (" === ");
10447 Write_Name (Chars (S));
10456 procedure we (S : Entity_Id) is
10459 E := First_Entity (S);
10460 while Present (E) loop
10461 Write_Int (Int (E));
10462 Write_Str (" === ");
10463 Write_Name (Chars (E));