1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, 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_Implicit_Operator (N : Node_Id) return Boolean;
505 -- N is an expanded name whose selector is an operator name (e.g. P."+").
506 -- declarative part contains an implicit declaration of an operator if it
507 -- has a declaration of a type to which one of the predefined operators
508 -- apply. The existence of this routine is an implementation artifact. A
509 -- more straightforward but more space-consuming choice would be to make
510 -- all inherited operators explicit in the symbol table.
512 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
513 -- A subprogram defined by a renaming declaration inherits the parameter
514 -- profile of the renamed entity. The subtypes given in the subprogram
515 -- specification are discarded and replaced with those of the renamed
516 -- subprogram, which are then used to recheck the default values.
518 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean;
519 -- True if it is of a task type, a protected type, or else an access to one
522 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean;
523 -- Prefix is appropriate for record if it is of a record type, or an access
526 function Most_Descendant_Use_Clause
527 (Clause1 : Entity_Id;
528 Clause2 : Entity_Id) return Entity_Id;
529 -- Determine which use clause parameter is the most descendant in terms of
531 -- ??? a better subprogram name is in order
533 procedure Premature_Usage (N : Node_Id);
534 -- Diagnose usage of an entity before it is visible
536 procedure Use_One_Package
538 Pack_Name : Entity_Id := Empty;
539 Force : Boolean := False);
540 -- Make visible entities declared in package P potentially use-visible
541 -- in the current context. Also used in the analysis of subunits, when
542 -- re-installing use clauses of parent units. N is the use_clause that
543 -- names P (and possibly other packages).
545 procedure Use_One_Type
547 Installed : Boolean := False;
548 Force : Boolean := False);
549 -- Id is the subtype mark from a use_type_clause. This procedure makes
550 -- the primitive operators of the type potentially use-visible. The
551 -- boolean flag Installed indicates that the clause is being reinstalled
552 -- after previous analysis, and primitive operations are already chained
553 -- on the Used_Operations list of the clause.
555 procedure Write_Info;
556 -- Write debugging information on entities declared in current scope
558 --------------------------------
559 -- Analyze_Exception_Renaming --
560 --------------------------------
562 -- The language only allows a single identifier, but the tree holds an
563 -- identifier list. The parser has already issued an error message if
564 -- there is more than one element in the list.
566 procedure Analyze_Exception_Renaming (N : Node_Id) is
567 Id : constant Entity_Id := Defining_Entity (N);
568 Nam : constant Node_Id := Name (N);
571 Check_SPARK_05_Restriction ("exception renaming is not allowed", N);
576 Set_Ekind (Id, E_Exception);
577 Set_Etype (Id, Standard_Exception_Type);
578 Set_Is_Pure (Id, Is_Pure (Current_Scope));
580 if Is_Entity_Name (Nam)
581 and then Present (Entity (Nam))
582 and then Ekind (Entity (Nam)) = E_Exception
584 if Present (Renamed_Object (Entity (Nam))) then
585 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
587 Set_Renamed_Object (Id, Entity (Nam));
590 -- The exception renaming declaration may become Ghost if it renames
593 Mark_Ghost_Renaming (N, Entity (Nam));
595 Error_Msg_N ("invalid exception name in renaming", Nam);
598 -- Implementation-defined aspect specifications can appear in a renaming
599 -- declaration, but not language-defined ones. The call to procedure
600 -- Analyze_Aspect_Specifications will take care of this error check.
602 if Has_Aspects (N) then
603 Analyze_Aspect_Specifications (N, Id);
605 end Analyze_Exception_Renaming;
607 ---------------------------
608 -- Analyze_Expanded_Name --
609 ---------------------------
611 procedure Analyze_Expanded_Name (N : Node_Id) is
613 -- If the entity pointer is already set, this is an internal node, or a
614 -- node that is analyzed more than once, after a tree modification. In
615 -- such a case there is no resolution to perform, just set the type. In
616 -- either case, start by analyzing the prefix.
618 Analyze (Prefix (N));
620 if Present (Entity (N)) then
621 if Is_Type (Entity (N)) then
622 Set_Etype (N, Entity (N));
624 Set_Etype (N, Etype (Entity (N)));
628 Find_Expanded_Name (N);
631 -- In either case, propagate dimension of entity to expanded name
633 Analyze_Dimension (N);
634 end Analyze_Expanded_Name;
636 ---------------------------------------
637 -- Analyze_Generic_Function_Renaming --
638 ---------------------------------------
640 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
642 Analyze_Generic_Renaming (N, E_Generic_Function);
643 end Analyze_Generic_Function_Renaming;
645 --------------------------------------
646 -- Analyze_Generic_Package_Renaming --
647 --------------------------------------
649 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
651 -- Test for the Text_IO special unit case here, since we may be renaming
652 -- one of the subpackages of Text_IO, then join common routine.
654 Check_Text_IO_Special_Unit (Name (N));
656 Analyze_Generic_Renaming (N, E_Generic_Package);
657 end Analyze_Generic_Package_Renaming;
659 ----------------------------------------
660 -- Analyze_Generic_Procedure_Renaming --
661 ----------------------------------------
663 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
665 Analyze_Generic_Renaming (N, E_Generic_Procedure);
666 end Analyze_Generic_Procedure_Renaming;
668 ------------------------------
669 -- Analyze_Generic_Renaming --
670 ------------------------------
672 procedure Analyze_Generic_Renaming
676 New_P : constant Entity_Id := Defining_Entity (N);
677 Inst : Boolean := False;
681 if Name (N) = Error then
685 Check_SPARK_05_Restriction ("generic renaming is not allowed", N);
687 Generate_Definition (New_P);
689 if Current_Scope /= Standard_Standard then
690 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
693 if Nkind (Name (N)) = N_Selected_Component then
694 Check_Generic_Child_Unit (Name (N), Inst);
699 if not Is_Entity_Name (Name (N)) then
700 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
703 Old_P := Entity (Name (N));
707 Set_Ekind (New_P, K);
709 if Etype (Old_P) = Any_Type then
712 elsif Ekind (Old_P) /= K then
713 Error_Msg_N ("invalid generic unit name", Name (N));
716 if Present (Renamed_Object (Old_P)) then
717 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
719 Set_Renamed_Object (New_P, Old_P);
722 -- The generic renaming declaration may become Ghost if it renames a
725 Mark_Ghost_Renaming (N, Old_P);
727 Set_Is_Pure (New_P, Is_Pure (Old_P));
728 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
730 Set_Etype (New_P, Etype (Old_P));
731 Set_Has_Completion (New_P);
733 if In_Open_Scopes (Old_P) then
734 Error_Msg_N ("within its scope, generic denotes its instance", N);
737 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
738 -- renamings and subsequent instantiations of Unchecked_Conversion.
740 if Ekind_In (Old_P, E_Generic_Function, E_Generic_Procedure) then
741 Set_Is_Intrinsic_Subprogram
742 (New_P, Is_Intrinsic_Subprogram (Old_P));
745 Check_Library_Unit_Renaming (N, Old_P);
748 -- Implementation-defined aspect specifications can appear in a renaming
749 -- declaration, but not language-defined ones. The call to procedure
750 -- Analyze_Aspect_Specifications will take care of this error check.
752 if Has_Aspects (N) then
753 Analyze_Aspect_Specifications (N, New_P);
755 end Analyze_Generic_Renaming;
757 -----------------------------
758 -- Analyze_Object_Renaming --
759 -----------------------------
761 procedure Analyze_Object_Renaming (N : Node_Id) is
762 Id : constant Entity_Id := Defining_Identifier (N);
763 Loc : constant Source_Ptr := Sloc (N);
764 Nam : constant Node_Id := Name (N);
769 procedure Check_Constrained_Object;
770 -- If the nominal type is unconstrained but the renamed object is
771 -- constrained, as can happen with renaming an explicit dereference or
772 -- a function return, build a constrained subtype from the object. If
773 -- the renaming is for a formal in an accept statement, the analysis
774 -- has already established its actual subtype. This is only relevant
775 -- if the renamed object is an explicit dereference.
777 function Get_Object_Name (Nod : Node_Id) return Node_Id;
778 -- Obtain the name of the object from node Nod which is being renamed by
779 -- the object renaming declaration N.
781 ------------------------------
782 -- Check_Constrained_Object --
783 ------------------------------
785 procedure Check_Constrained_Object is
786 Typ : constant Entity_Id := Etype (Nam);
790 if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference)
791 and then Is_Composite_Type (Typ)
792 and then not Is_Constrained (Typ)
793 and then not Has_Unknown_Discriminants (Typ)
794 and then Expander_Active
796 -- If Actual_Subtype is already set, nothing to do
798 if Ekind_In (Id, E_Variable, E_Constant)
799 and then Present (Actual_Subtype (Id))
803 -- A renaming of an unchecked union has no actual subtype
805 elsif Is_Unchecked_Union (Typ) then
808 -- If a record is limited its size is invariant. This is the case
809 -- in particular with record types with an access discriminant
810 -- that are used in iterators. This is an optimization, but it
811 -- also prevents typing anomalies when the prefix is further
813 -- Note that we cannot just use the Is_Limited_Record flag because
814 -- it does not apply to records with limited components, for which
815 -- this syntactic flag is not set, but whose size is also fixed.
817 elsif Is_Limited_Type (Typ) then
821 Subt := Make_Temporary (Loc, 'T');
822 Remove_Side_Effects (Nam);
824 Make_Subtype_Declaration (Loc,
825 Defining_Identifier => Subt,
826 Subtype_Indication =>
827 Make_Subtype_From_Expr (Nam, Typ)));
828 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
829 Set_Etype (Nam, Subt);
831 -- Freeze subtype at once, to prevent order of elaboration
832 -- issues in the backend. The renamed object exists, so its
833 -- type is already frozen in any case.
835 Freeze_Before (N, Subt);
838 end Check_Constrained_Object;
840 ---------------------
841 -- Get_Object_Name --
842 ---------------------
844 function Get_Object_Name (Nod : Node_Id) return Node_Id is
849 while Present (Obj_Nam) loop
850 if Nkind_In (Obj_Nam, N_Attribute_Reference,
851 N_Explicit_Dereference,
855 Obj_Nam := Prefix (Obj_Nam);
857 elsif Nkind (Obj_Nam) = N_Selected_Component then
858 Obj_Nam := Selector_Name (Obj_Nam);
867 -- Start of processing for Analyze_Object_Renaming
874 Check_SPARK_05_Restriction ("object renaming is not allowed", N);
876 Set_Is_Pure (Id, Is_Pure (Current_Scope));
879 -- The renaming of a component that depends on a discriminant requires
880 -- an actual subtype, because in subsequent use of the object Gigi will
881 -- be unable to locate the actual bounds. This explicit step is required
882 -- when the renaming is generated in removing side effects of an
883 -- already-analyzed expression.
885 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
887 -- The object renaming declaration may become Ghost if it renames a
890 if Is_Entity_Name (Nam) then
891 Mark_Ghost_Renaming (N, Entity (Nam));
895 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
897 if Present (Dec) then
898 Insert_Action (N, Dec);
899 T := Defining_Identifier (Dec);
903 -- Complete analysis of the subtype mark in any case, for ASIS use
905 if Present (Subtype_Mark (N)) then
906 Find_Type (Subtype_Mark (N));
909 elsif Present (Subtype_Mark (N)) then
910 Find_Type (Subtype_Mark (N));
911 T := Entity (Subtype_Mark (N));
914 -- The object renaming declaration may become Ghost if it renames a
917 if Is_Entity_Name (Nam) then
918 Mark_Ghost_Renaming (N, Entity (Nam));
921 -- Reject renamings of conversions unless the type is tagged, or
922 -- the conversion is implicit (which can occur for cases of anonymous
923 -- access types in Ada 2012).
925 if Nkind (Nam) = N_Type_Conversion
926 and then Comes_From_Source (Nam)
927 and then not Is_Tagged_Type (T)
930 ("renaming of conversion only allowed for tagged types", Nam);
935 -- If the renamed object is a function call of a limited type,
936 -- the expansion of the renaming is complicated by the presence
937 -- of various temporaries and subtypes that capture constraints
938 -- of the renamed object. Rewrite node as an object declaration,
939 -- whose expansion is simpler. Given that the object is limited
940 -- there is no copy involved and no performance hit.
942 if Nkind (Nam) = N_Function_Call
943 and then Is_Limited_View (Etype (Nam))
944 and then not Is_Constrained (Etype (Nam))
945 and then Comes_From_Source (N)
948 Set_Ekind (Id, E_Constant);
950 Make_Object_Declaration (Loc,
951 Defining_Identifier => Id,
952 Constant_Present => True,
953 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
954 Expression => Relocate_Node (Nam)));
958 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
959 -- when renaming declaration has a named access type. The Ada 2012
960 -- coverage rules allow an anonymous access type in the context of
961 -- an expected named general access type, but the renaming rules
962 -- require the types to be the same. (An exception is when the type
963 -- of the renaming is also an anonymous access type, which can only
964 -- happen due to a renaming created by the expander.)
966 if Nkind (Nam) = N_Type_Conversion
967 and then not Comes_From_Source (Nam)
968 and then Ekind (Etype (Expression (Nam))) = E_Anonymous_Access_Type
969 and then Ekind (T) /= E_Anonymous_Access_Type
971 Wrong_Type (Expression (Nam), T); -- Should we give better error???
974 -- Check that a class-wide object is not being renamed as an object
975 -- of a specific type. The test for access types is needed to exclude
976 -- cases where the renamed object is a dynamically tagged access
977 -- result, such as occurs in certain expansions.
979 if Is_Tagged_Type (T) then
980 Check_Dynamically_Tagged_Expression
986 -- Ada 2005 (AI-230/AI-254): Access renaming
988 else pragma Assert (Present (Access_Definition (N)));
992 N => Access_Definition (N));
996 -- The object renaming declaration may become Ghost if it renames a
999 if Is_Entity_Name (Nam) then
1000 Mark_Ghost_Renaming (N, Entity (Nam));
1003 -- Ada 2005 AI05-105: if the declaration has an anonymous access
1004 -- type, the renamed object must also have an anonymous type, and
1005 -- this is a name resolution rule. This was implicit in the last part
1006 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
1009 if not Is_Overloaded (Nam) then
1010 if Ekind (Etype (Nam)) /= Ekind (T) then
1012 ("expect anonymous access type in object renaming", N);
1019 Typ : Entity_Id := Empty;
1020 Seen : Boolean := False;
1023 Get_First_Interp (Nam, I, It);
1024 while Present (It.Typ) loop
1026 -- Renaming is ambiguous if more than one candidate
1027 -- interpretation is type-conformant with the context.
1029 if Ekind (It.Typ) = Ekind (T) then
1030 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
1033 (Designated_Type (T), Designated_Type (It.Typ))
1039 ("ambiguous expression in renaming", Nam);
1042 elsif Ekind (T) = E_Anonymous_Access_Type
1044 Covers (Designated_Type (T), Designated_Type (It.Typ))
1050 ("ambiguous expression in renaming", Nam);
1054 if Covers (T, It.Typ) then
1056 Set_Etype (Nam, Typ);
1057 Set_Is_Overloaded (Nam, False);
1061 Get_Next_Interp (I, It);
1068 -- Do not perform the legality checks below when the resolution of
1069 -- the renaming name failed because the associated type is Any_Type.
1071 if Etype (Nam) = Any_Type then
1074 -- Ada 2005 (AI-231): In the case where the type is defined by an
1075 -- access_definition, the renamed entity shall be of an access-to-
1076 -- constant type if and only if the access_definition defines an
1077 -- access-to-constant type. ARM 8.5.1(4)
1079 elsif Constant_Present (Access_Definition (N))
1080 and then not Is_Access_Constant (Etype (Nam))
1083 ("(Ada 2005): the renamed object is not access-to-constant "
1084 & "(RM 8.5.1(6))", N);
1086 elsif not Constant_Present (Access_Definition (N))
1087 and then Is_Access_Constant (Etype (Nam))
1090 ("(Ada 2005): the renamed object is not access-to-variable "
1091 & "(RM 8.5.1(6))", N);
1094 if Is_Access_Subprogram_Type (Etype (Nam)) then
1095 Check_Subtype_Conformant
1096 (Designated_Type (T), Designated_Type (Etype (Nam)));
1098 elsif not Subtypes_Statically_Match
1099 (Designated_Type (T),
1100 Available_View (Designated_Type (Etype (Nam))))
1103 ("subtype of renamed object does not statically match", N);
1107 -- Special processing for renaming function return object. Some errors
1108 -- and warnings are produced only for calls that come from source.
1110 if Nkind (Nam) = N_Function_Call then
1113 -- Usage is illegal in Ada 83, but renamings are also introduced
1114 -- during expansion, and error does not apply to those.
1117 if Comes_From_Source (N) then
1119 ("(Ada 83) cannot rename function return object", Nam);
1122 -- In Ada 95, warn for odd case of renaming parameterless function
1123 -- call if this is not a limited type (where this is useful).
1126 if Warn_On_Object_Renames_Function
1127 and then No (Parameter_Associations (Nam))
1128 and then not Is_Limited_Type (Etype (Nam))
1129 and then Comes_From_Source (Nam)
1132 ("renaming function result object is suspicious?R?", Nam);
1134 ("\function & will be called only once?R?", Nam,
1135 Entity (Name (Nam)));
1136 Error_Msg_N -- CODEFIX
1137 ("\suggest using an initialized constant object "
1138 & "instead?R?", Nam);
1143 Check_Constrained_Object;
1145 -- An object renaming requires an exact match of the type. Class-wide
1146 -- matching is not allowed.
1148 if Is_Class_Wide_Type (T)
1149 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1151 Wrong_Type (Nam, T);
1154 -- We must search for an actual subtype here so that the bounds of
1155 -- objects of unconstrained types don't get dropped on the floor - such
1156 -- as with renamings of formal parameters.
1158 T2 := Get_Actual_Subtype_If_Available (Nam);
1160 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1162 if Nkind (Nam) = N_Explicit_Dereference
1163 and then Ekind (Etype (T2)) = E_Incomplete_Type
1165 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1168 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1169 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1173 -- Ada 2005 (AI-327)
1175 if Ada_Version >= Ada_2005
1176 and then Nkind (Nam) = N_Attribute_Reference
1177 and then Attribute_Name (Nam) = Name_Priority
1181 elsif Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1183 Nam_Ent : constant Entity_Id := Entity (Get_Object_Name (Nam));
1184 Nam_Decl : constant Node_Id := Declaration_Node (Nam_Ent);
1187 if Has_Null_Exclusion (N)
1188 and then not Has_Null_Exclusion (Nam_Decl)
1190 -- Ada 2005 (AI-423): If the object name denotes a generic
1191 -- formal object of a generic unit G, and the object renaming
1192 -- declaration occurs within the body of G or within the body
1193 -- of a generic unit declared within the declarative region
1194 -- of G, then the declaration of the formal object of G must
1195 -- have a null exclusion or a null-excluding subtype.
1197 if Is_Formal_Object (Nam_Ent)
1198 and then In_Generic_Scope (Id)
1200 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1202 ("renamed formal does not exclude `NULL` "
1203 & "(RM 8.5.1(4.6/2))", N);
1205 elsif In_Package_Body (Scope (Id)) then
1207 ("formal object does not have a null exclusion"
1208 & "(RM 8.5.1(4.6/2))", N);
1211 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1212 -- shall exclude null.
1214 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1216 ("renamed object does not exclude `NULL` "
1217 & "(RM 8.5.1(4.6/2))", N);
1219 -- An instance is illegal if it contains a renaming that
1220 -- excludes null, and the actual does not. The renaming
1221 -- declaration has already indicated that the declaration
1222 -- of the renamed actual in the instance will raise
1223 -- constraint_error.
1225 elsif Nkind (Nam_Decl) = N_Object_Declaration
1226 and then In_Instance
1228 Present (Corresponding_Generic_Association (Nam_Decl))
1229 and then Nkind (Expression (Nam_Decl)) =
1230 N_Raise_Constraint_Error
1233 ("renamed actual does not exclude `NULL` "
1234 & "(RM 8.5.1(4.6/2))", N);
1236 -- Finally, if there is a null exclusion, the subtype mark
1237 -- must not be null-excluding.
1239 elsif No (Access_Definition (N))
1240 and then Can_Never_Be_Null (T)
1243 ("`NOT NULL` not allowed (& already excludes null)",
1248 elsif Can_Never_Be_Null (T)
1249 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1252 ("renamed object does not exclude `NULL` "
1253 & "(RM 8.5.1(4.6/2))", N);
1255 elsif Has_Null_Exclusion (N)
1256 and then No (Access_Definition (N))
1257 and then Can_Never_Be_Null (T)
1260 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1265 -- Set the Ekind of the entity, unless it has been set already, as is
1266 -- the case for the iteration object over a container with no variable
1267 -- indexing. In that case it's been marked as a constant, and we do not
1268 -- want to change it to a variable.
1270 if Ekind (Id) /= E_Constant then
1271 Set_Ekind (Id, E_Variable);
1274 -- Initialize the object size and alignment. Note that we used to call
1275 -- Init_Size_Align here, but that's wrong for objects which have only
1276 -- an Esize, not an RM_Size field.
1278 Init_Object_Size_Align (Id);
1280 if T = Any_Type or else Etype (Nam) = Any_Type then
1283 -- Verify that the renamed entity is an object or a function call. It
1284 -- may have been rewritten in several ways.
1286 elsif Is_Object_Reference (Nam) then
1287 if Comes_From_Source (N) then
1288 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1290 ("illegal renaming of discriminant-dependent component", Nam);
1293 -- If the renaming comes from source and the renamed object is a
1294 -- dereference, then mark the prefix as needing debug information,
1295 -- since it might have been rewritten hence internally generated
1296 -- and Debug_Renaming_Declaration will link the renaming to it.
1298 if Nkind (Nam) = N_Explicit_Dereference
1299 and then Is_Entity_Name (Prefix (Nam))
1301 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1305 -- A static function call may have been folded into a literal
1307 elsif Nkind (Original_Node (Nam)) = N_Function_Call
1309 -- When expansion is disabled, attribute reference is not rewritten
1310 -- as function call. Otherwise it may be rewritten as a conversion,
1311 -- so check original node.
1313 or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference
1314 and then Is_Function_Attribute_Name
1315 (Attribute_Name (Original_Node (Nam))))
1317 -- Weird but legal, equivalent to renaming a function call. Illegal
1318 -- if the literal is the result of constant-folding an attribute
1319 -- reference that is not a function.
1321 or else (Is_Entity_Name (Nam)
1322 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1324 Nkind (Original_Node (Nam)) /= N_Attribute_Reference)
1326 or else (Nkind (Nam) = N_Type_Conversion
1327 and then Is_Tagged_Type (Entity (Subtype_Mark (Nam))))
1331 elsif Nkind (Nam) = N_Type_Conversion then
1333 ("renaming of conversion only allowed for tagged types", Nam);
1335 -- Ada 2005 (AI-327)
1337 elsif Ada_Version >= Ada_2005
1338 and then Nkind (Nam) = N_Attribute_Reference
1339 and then Attribute_Name (Nam) = Name_Priority
1343 -- Allow internally generated x'Ref resulting in N_Reference node
1345 elsif Nkind (Nam) = N_Reference then
1349 Error_Msg_N ("expect object name in renaming", Nam);
1354 if not Is_Variable (Nam) then
1355 Set_Ekind (Id, E_Constant);
1356 Set_Never_Set_In_Source (Id, True);
1357 Set_Is_True_Constant (Id, True);
1360 -- The entity of the renaming declaration needs to reflect whether the
1361 -- renamed object is atomic, independent, volatile or VFA. These flags
1362 -- are set on the renamed object in the RM legality sense.
1364 Set_Is_Atomic (Id, Is_Atomic_Object (Nam));
1365 Set_Is_Independent (Id, Is_Independent_Object (Nam));
1366 Set_Is_Volatile (Id, Is_Volatile_Object (Nam));
1367 Set_Is_Volatile_Full_Access (Id, Is_Volatile_Full_Access_Object (Nam));
1369 -- Treat as volatile if we just set the Volatile flag
1373 -- Or if we are renaming an entity which was marked this way
1375 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1377 or else (Is_Entity_Name (Nam)
1378 and then Treat_As_Volatile (Entity (Nam)))
1380 Set_Treat_As_Volatile (Id, True);
1383 -- Now make the link to the renamed object
1385 Set_Renamed_Object (Id, Nam);
1387 -- Implementation-defined aspect specifications can appear in a renaming
1388 -- declaration, but not language-defined ones. The call to procedure
1389 -- Analyze_Aspect_Specifications will take care of this error check.
1391 if Has_Aspects (N) then
1392 Analyze_Aspect_Specifications (N, Id);
1395 -- Deal with dimensions
1397 Analyze_Dimension (N);
1398 end Analyze_Object_Renaming;
1400 ------------------------------
1401 -- Analyze_Package_Renaming --
1402 ------------------------------
1404 procedure Analyze_Package_Renaming (N : Node_Id) is
1405 New_P : constant Entity_Id := Defining_Entity (N);
1410 if Name (N) = Error then
1414 -- Check for Text_IO special unit (we may be renaming a Text_IO child)
1416 Check_Text_IO_Special_Unit (Name (N));
1418 if Current_Scope /= Standard_Standard then
1419 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1425 if Is_Entity_Name (Name (N)) then
1426 Old_P := Entity (Name (N));
1431 if Etype (Old_P) = Any_Type then
1432 Error_Msg_N ("expect package name in renaming", Name (N));
1434 elsif Ekind (Old_P) /= E_Package
1435 and then not (Ekind (Old_P) = E_Generic_Package
1436 and then In_Open_Scopes (Old_P))
1438 if Ekind (Old_P) = E_Generic_Package then
1440 ("generic package cannot be renamed as a package", Name (N));
1442 Error_Msg_Sloc := Sloc (Old_P);
1444 ("expect package name in renaming, found& declared#",
1448 -- Set basic attributes to minimize cascaded errors
1450 Set_Ekind (New_P, E_Package);
1451 Set_Etype (New_P, Standard_Void_Type);
1453 -- Here for OK package renaming
1456 -- Entities in the old package are accessible through the renaming
1457 -- entity. The simplest implementation is to have both packages share
1460 Set_Ekind (New_P, E_Package);
1461 Set_Etype (New_P, Standard_Void_Type);
1463 if Present (Renamed_Object (Old_P)) then
1464 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1466 Set_Renamed_Object (New_P, Old_P);
1469 -- The package renaming declaration may become Ghost if it renames a
1472 Mark_Ghost_Renaming (N, Old_P);
1474 Set_Has_Completion (New_P);
1475 Set_First_Entity (New_P, First_Entity (Old_P));
1476 Set_Last_Entity (New_P, Last_Entity (Old_P));
1477 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1478 Check_Library_Unit_Renaming (N, Old_P);
1479 Generate_Reference (Old_P, Name (N));
1481 -- If the renaming is in the visible part of a package, then we set
1482 -- Renamed_In_Spec for the renamed package, to prevent giving
1483 -- warnings about no entities referenced. Such a warning would be
1484 -- overenthusiastic, since clients can see entities in the renamed
1485 -- package via the visible package renaming.
1488 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1490 if Ekind (Ent) = E_Package
1491 and then not In_Private_Part (Ent)
1492 and then In_Extended_Main_Source_Unit (N)
1493 and then Ekind (Old_P) = E_Package
1495 Set_Renamed_In_Spec (Old_P);
1499 -- If this is the renaming declaration of a package instantiation
1500 -- within itself, it is the declaration that ends the list of actuals
1501 -- for the instantiation. At this point, the subtypes that rename
1502 -- the actuals are flagged as generic, to avoid spurious ambiguities
1503 -- if the actuals for two distinct formals happen to coincide. If
1504 -- the actual is a private type, the subtype has a private completion
1505 -- that is flagged in the same fashion.
1507 -- Resolution is identical to what is was in the original generic.
1508 -- On exit from the generic instance, these are turned into regular
1509 -- subtypes again, so they are compatible with types in their class.
1511 if not Is_Generic_Instance (Old_P) then
1514 Spec := Specification (Unit_Declaration_Node (Old_P));
1517 if Nkind (Spec) = N_Package_Specification
1518 and then Present (Generic_Parent (Spec))
1519 and then Old_P = Current_Scope
1520 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1526 E := First_Entity (Old_P);
1527 while Present (E) and then E /= New_P loop
1529 and then Nkind (Parent (E)) = N_Subtype_Declaration
1531 Set_Is_Generic_Actual_Type (E);
1533 if Is_Private_Type (E)
1534 and then Present (Full_View (E))
1536 Set_Is_Generic_Actual_Type (Full_View (E));
1546 -- Implementation-defined aspect specifications can appear in a renaming
1547 -- declaration, but not language-defined ones. The call to procedure
1548 -- Analyze_Aspect_Specifications will take care of this error check.
1550 if Has_Aspects (N) then
1551 Analyze_Aspect_Specifications (N, New_P);
1553 end Analyze_Package_Renaming;
1555 -------------------------------
1556 -- Analyze_Renamed_Character --
1557 -------------------------------
1559 procedure Analyze_Renamed_Character
1564 C : constant Node_Id := Name (N);
1567 if Ekind (New_S) = E_Function then
1568 Resolve (C, Etype (New_S));
1571 Check_Frozen_Renaming (N, New_S);
1575 Error_Msg_N ("character literal can only be renamed as function", N);
1577 end Analyze_Renamed_Character;
1579 ---------------------------------
1580 -- Analyze_Renamed_Dereference --
1581 ---------------------------------
1583 procedure Analyze_Renamed_Dereference
1588 Nam : constant Node_Id := Name (N);
1589 P : constant Node_Id := Prefix (Nam);
1595 if not Is_Overloaded (P) then
1596 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1597 or else not Type_Conformant (Etype (Nam), New_S)
1599 Error_Msg_N ("designated type does not match specification", P);
1608 Get_First_Interp (Nam, Ind, It);
1610 while Present (It.Nam) loop
1612 if Ekind (It.Nam) = E_Subprogram_Type
1613 and then Type_Conformant (It.Nam, New_S)
1615 if Typ /= Any_Id then
1616 Error_Msg_N ("ambiguous renaming", P);
1623 Get_Next_Interp (Ind, It);
1626 if Typ = Any_Type then
1627 Error_Msg_N ("designated type does not match specification", P);
1632 Check_Frozen_Renaming (N, New_S);
1636 end Analyze_Renamed_Dereference;
1638 ---------------------------
1639 -- Analyze_Renamed_Entry --
1640 ---------------------------
1642 procedure Analyze_Renamed_Entry
1647 Nam : constant Node_Id := Name (N);
1648 Sel : constant Node_Id := Selector_Name (Nam);
1649 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1653 if Entity (Sel) = Any_Id then
1655 -- Selector is undefined on prefix. Error emitted already
1657 Set_Has_Completion (New_S);
1661 -- Otherwise find renamed entity and build body of New_S as a call to it
1663 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1665 if Old_S = Any_Id then
1666 Error_Msg_N (" no subprogram or entry matches specification", N);
1669 Check_Subtype_Conformant (New_S, Old_S, N);
1670 Generate_Reference (New_S, Defining_Entity (N), 'b');
1671 Style.Check_Identifier (Defining_Entity (N), New_S);
1674 -- Only mode conformance required for a renaming_as_declaration
1676 Check_Mode_Conformant (New_S, Old_S, N);
1679 Inherit_Renamed_Profile (New_S, Old_S);
1681 -- The prefix can be an arbitrary expression that yields a task or
1682 -- protected object, so it must be resolved.
1684 Resolve (Prefix (Nam), Scope (Old_S));
1687 Set_Convention (New_S, Convention (Old_S));
1688 Set_Has_Completion (New_S, Inside_A_Generic);
1690 -- AI05-0225: If the renamed entity is a procedure or entry of a
1691 -- protected object, the target object must be a variable.
1693 if Is_Protected_Type (Scope (Old_S))
1694 and then Ekind (New_S) = E_Procedure
1695 and then not Is_Variable (Prefix (Nam))
1699 ("target object of protected operation used as actual for "
1700 & "formal procedure must be a variable", Nam);
1703 ("target object of protected operation renamed as procedure, "
1704 & "must be a variable", Nam);
1709 Check_Frozen_Renaming (N, New_S);
1711 end Analyze_Renamed_Entry;
1713 -----------------------------------
1714 -- Analyze_Renamed_Family_Member --
1715 -----------------------------------
1717 procedure Analyze_Renamed_Family_Member
1722 Nam : constant Node_Id := Name (N);
1723 P : constant Node_Id := Prefix (Nam);
1727 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1728 or else (Nkind (P) = N_Selected_Component
1729 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1731 if Is_Entity_Name (P) then
1732 Old_S := Entity (P);
1734 Old_S := Entity (Selector_Name (P));
1737 if not Entity_Matches_Spec (Old_S, New_S) then
1738 Error_Msg_N ("entry family does not match specification", N);
1741 Check_Subtype_Conformant (New_S, Old_S, N);
1742 Generate_Reference (New_S, Defining_Entity (N), 'b');
1743 Style.Check_Identifier (Defining_Entity (N), New_S);
1747 Error_Msg_N ("no entry family matches specification", N);
1750 Set_Has_Completion (New_S, Inside_A_Generic);
1753 Check_Frozen_Renaming (N, New_S);
1755 end Analyze_Renamed_Family_Member;
1757 -----------------------------------------
1758 -- Analyze_Renamed_Primitive_Operation --
1759 -----------------------------------------
1761 procedure Analyze_Renamed_Primitive_Operation
1770 Ctyp : Conformance_Type) return Boolean;
1771 -- Verify that the signatures of the renamed entity and the new entity
1772 -- match. The first formal of the renamed entity is skipped because it
1773 -- is the target object in any subsequent call.
1781 Ctyp : Conformance_Type) return Boolean
1787 if Ekind (Subp) /= Ekind (New_S) then
1791 Old_F := Next_Formal (First_Formal (Subp));
1792 New_F := First_Formal (New_S);
1793 while Present (Old_F) and then Present (New_F) loop
1794 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
1798 if Ctyp >= Mode_Conformant
1799 and then Ekind (Old_F) /= Ekind (New_F)
1804 Next_Formal (New_F);
1805 Next_Formal (Old_F);
1811 -- Start of processing for Analyze_Renamed_Primitive_Operation
1814 if not Is_Overloaded (Selector_Name (Name (N))) then
1815 Old_S := Entity (Selector_Name (Name (N)));
1817 if not Conforms (Old_S, Type_Conformant) then
1822 -- Find the operation that matches the given signature
1830 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
1832 while Present (It.Nam) loop
1833 if Conforms (It.Nam, Type_Conformant) then
1837 Get_Next_Interp (Ind, It);
1842 if Old_S = Any_Id then
1843 Error_Msg_N (" no subprogram or entry matches specification", N);
1847 if not Conforms (Old_S, Subtype_Conformant) then
1848 Error_Msg_N ("subtype conformance error in renaming", N);
1851 Generate_Reference (New_S, Defining_Entity (N), 'b');
1852 Style.Check_Identifier (Defining_Entity (N), New_S);
1855 -- Only mode conformance required for a renaming_as_declaration
1857 if not Conforms (Old_S, Mode_Conformant) then
1858 Error_Msg_N ("mode conformance error in renaming", N);
1861 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
1862 -- view of a subprogram is intrinsic, because the compiler has
1863 -- to generate a wrapper for any call to it. If the name in a
1864 -- subprogram renaming is a prefixed view, the entity is thus
1865 -- intrinsic, and 'Access cannot be applied to it.
1867 Set_Convention (New_S, Convention_Intrinsic);
1870 -- Inherit_Renamed_Profile (New_S, Old_S);
1872 -- The prefix can be an arbitrary expression that yields an
1873 -- object, so it must be resolved.
1875 Resolve (Prefix (Name (N)));
1877 end Analyze_Renamed_Primitive_Operation;
1879 ---------------------------------
1880 -- Analyze_Subprogram_Renaming --
1881 ---------------------------------
1883 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1884 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
1885 Is_Actual : constant Boolean := Present (Formal_Spec);
1886 Nam : constant Node_Id := Name (N);
1887 Save_AV : constant Ada_Version_Type := Ada_Version;
1888 Save_AVP : constant Node_Id := Ada_Version_Pragma;
1889 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
1890 Spec : constant Node_Id := Specification (N);
1892 Old_S : Entity_Id := Empty;
1893 Rename_Spec : Entity_Id;
1895 procedure Build_Class_Wide_Wrapper
1896 (Ren_Id : out Entity_Id;
1897 Wrap_Id : out Entity_Id);
1898 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal
1899 -- type with unknown discriminants and a generic primitive operation of
1900 -- the said type with a box require special processing when the actual
1901 -- is a class-wide type:
1904 -- type Formal_Typ (<>) is private;
1905 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
1906 -- package Gen is ...
1908 -- package Inst is new Gen (Actual_Typ'Class);
1910 -- In this case the general renaming mechanism used in the prologue of
1911 -- an instance no longer applies:
1913 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
1915 -- The above is replaced the following wrapper/renaming combination:
1917 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
1919 -- Prim_Op (Param); -- primitive
1922 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
1924 -- This transformation applies only if there is no explicit visible
1925 -- class-wide operation at the point of the instantiation. Ren_Id is
1926 -- the entity of the renaming declaration. When the transformation
1927 -- applies, Wrap_Id is the entity of the generated class-wide wrapper
1928 -- (or Any_Id). Otherwise, Wrap_Id is the entity of the class-wide
1931 procedure Check_Null_Exclusion
1934 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
1935 -- following AI rules:
1937 -- If Ren is a renaming of a formal subprogram and one of its
1938 -- parameters has a null exclusion, then the corresponding formal
1939 -- in Sub must also have one. Otherwise the subtype of the Sub's
1940 -- formal parameter must exclude null.
1942 -- If Ren is a renaming of a formal function and its return
1943 -- profile has a null exclusion, then Sub's return profile must
1944 -- have one. Otherwise the subtype of Sub's return profile must
1947 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id);
1948 -- Ensure that a SPARK renaming denoted by its entity Subp_Id does not
1949 -- declare a primitive operation of a tagged type (SPARK RM 6.1.1(3)).
1951 procedure Freeze_Actual_Profile;
1952 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
1953 -- types: a callable entity freezes its profile, unless it has an
1954 -- incomplete untagged formal (RM 13.14(10.2/3)).
1956 function Has_Class_Wide_Actual return Boolean;
1957 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
1958 -- defaulted formal subprogram where the actual for the controlling
1959 -- formal type is class-wide.
1961 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
1962 -- Find renamed entity when the declaration is a renaming_as_body and
1963 -- the renamed entity may itself be a renaming_as_body. Used to enforce
1964 -- rule that a renaming_as_body is illegal if the declaration occurs
1965 -- before the subprogram it completes is frozen, and renaming indirectly
1966 -- renames the subprogram itself.(Defect Report 8652/0027).
1968 ------------------------------
1969 -- Build_Class_Wide_Wrapper --
1970 ------------------------------
1972 procedure Build_Class_Wide_Wrapper
1973 (Ren_Id : out Entity_Id;
1974 Wrap_Id : out Entity_Id)
1976 Loc : constant Source_Ptr := Sloc (N);
1979 (Subp_Id : Entity_Id;
1980 Params : List_Id) return Node_Id;
1981 -- Create a dispatching call to invoke routine Subp_Id with actuals
1982 -- built from the parameter specifications of list Params.
1984 function Build_Expr_Fun_Call
1985 (Subp_Id : Entity_Id;
1986 Params : List_Id) return Node_Id;
1987 -- Create a dispatching call to invoke function Subp_Id with actuals
1988 -- built from the parameter specifications of list Params. Return
1989 -- directly the call, so that it can be used inside an expression
1990 -- function. This is a specificity of the GNATprove mode.
1992 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
1993 -- Create a subprogram specification based on the subprogram profile
1996 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
1997 -- Find a primitive subprogram of type Typ which matches the profile
1998 -- of the renaming declaration.
2000 procedure Interpretation_Error (Subp_Id : Entity_Id);
2001 -- Emit a continuation error message suggesting subprogram Subp_Id as
2002 -- a possible interpretation.
2004 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean;
2005 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
2008 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean;
2009 -- Determine whether subprogram Subp_Id is a suitable candidate for
2010 -- the role of a wrapped subprogram.
2017 (Subp_Id : Entity_Id;
2018 Params : List_Id) return Node_Id
2020 Actuals : constant List_Id := New_List;
2021 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2025 -- Build the actual parameters of the call
2027 Formal := First (Params);
2028 while Present (Formal) loop
2030 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2035 -- return Subp_Id (Actuals);
2037 if Ekind_In (Subp_Id, E_Function, E_Operator) then
2039 Make_Simple_Return_Statement (Loc,
2041 Make_Function_Call (Loc,
2043 Parameter_Associations => Actuals));
2046 -- Subp_Id (Actuals);
2050 Make_Procedure_Call_Statement (Loc,
2052 Parameter_Associations => Actuals);
2056 -------------------------
2057 -- Build_Expr_Fun_Call --
2058 -------------------------
2060 function Build_Expr_Fun_Call
2061 (Subp_Id : Entity_Id;
2062 Params : List_Id) return Node_Id
2064 Actuals : constant List_Id := New_List;
2065 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2069 pragma Assert (Ekind_In (Subp_Id, E_Function, E_Operator));
2071 -- Build the actual parameters of the call
2073 Formal := First (Params);
2074 while Present (Formal) loop
2076 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2081 -- Subp_Id (Actuals);
2084 Make_Function_Call (Loc,
2086 Parameter_Associations => Actuals);
2087 end Build_Expr_Fun_Call;
2093 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2094 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2095 Spec_Id : constant Entity_Id :=
2096 Make_Defining_Identifier (Loc,
2097 Chars => New_External_Name (Chars (Subp_Id), 'R'));
2100 if Ekind (Formal_Spec) = E_Procedure then
2102 Make_Procedure_Specification (Loc,
2103 Defining_Unit_Name => Spec_Id,
2104 Parameter_Specifications => Params);
2107 Make_Function_Specification (Loc,
2108 Defining_Unit_Name => Spec_Id,
2109 Parameter_Specifications => Params,
2110 Result_Definition =>
2111 New_Copy_Tree (Result_Definition (Spec)));
2115 --------------------
2116 -- Find_Primitive --
2117 --------------------
2119 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2120 procedure Replace_Parameter_Types (Spec : Node_Id);
2121 -- Given a specification Spec, replace all class-wide parameter
2122 -- types with reference to type Typ.
2124 -----------------------------
2125 -- Replace_Parameter_Types --
2126 -----------------------------
2128 procedure Replace_Parameter_Types (Spec : Node_Id) is
2130 Formal_Id : Entity_Id;
2131 Formal_Typ : Node_Id;
2134 Formal := First (Parameter_Specifications (Spec));
2135 while Present (Formal) loop
2136 Formal_Id := Defining_Identifier (Formal);
2137 Formal_Typ := Parameter_Type (Formal);
2139 -- Create a new entity for each class-wide formal to prevent
2140 -- aliasing with the original renaming. Replace the type of
2141 -- such a parameter with the candidate type.
2143 if Nkind (Formal_Typ) = N_Identifier
2144 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2146 Set_Defining_Identifier (Formal,
2147 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2149 Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc));
2154 end Replace_Parameter_Types;
2158 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2159 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2160 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2161 Subp_Id : Entity_Id;
2163 -- Start of processing for Find_Primitive
2166 -- Each attempt to find a suitable primitive of a particular type
2167 -- operates on its own copy of the original renaming. As a result
2168 -- the original renaming is kept decoration and side-effect free.
2170 -- Inherit the overloaded status of the renamed subprogram name
2172 if Is_Overloaded (Nam) then
2173 Set_Is_Overloaded (Alt_Nam);
2174 Save_Interps (Nam, Alt_Nam);
2177 -- The copied renaming is hidden from visibility to prevent the
2178 -- pollution of the enclosing context.
2180 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2182 -- The types of all class-wide parameters must be changed to the
2185 Replace_Parameter_Types (Alt_Spec);
2187 -- Try to find a suitable primitive which matches the altered
2188 -- profile of the renaming specification.
2193 Nam => Name (Alt_Ren),
2194 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2195 Is_Actual => Is_Actual);
2197 -- Do not return Any_Id if the resolion of the altered profile
2198 -- failed as this complicates further checks on the caller side,
2199 -- return Empty instead.
2201 if Subp_Id = Any_Id then
2208 --------------------------
2209 -- Interpretation_Error --
2210 --------------------------
2212 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2214 Error_Msg_Sloc := Sloc (Subp_Id);
2216 if Is_Internal (Subp_Id) then
2218 ("\\possible interpretation: predefined & #",
2222 ("\\possible interpretation: & defined #", Spec, Formal_Spec);
2224 end Interpretation_Error;
2226 ---------------------------
2227 -- Is_Intrinsic_Equality --
2228 ---------------------------
2230 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is
2233 Ekind (Subp_Id) = E_Operator
2234 and then Chars (Subp_Id) = Name_Op_Eq
2235 and then Is_Intrinsic_Subprogram (Subp_Id);
2236 end Is_Intrinsic_Equality;
2238 ---------------------------
2239 -- Is_Suitable_Candidate --
2240 ---------------------------
2242 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is
2244 if No (Subp_Id) then
2247 -- An intrinsic subprogram is never a good candidate. This is an
2248 -- indication of a missing primitive, either defined directly or
2249 -- inherited from a parent tagged type.
2251 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2257 end Is_Suitable_Candidate;
2261 Actual_Typ : Entity_Id := Empty;
2262 -- The actual class-wide type for Formal_Typ
2264 CW_Prim_OK : Boolean;
2265 CW_Prim_Op : Entity_Id;
2266 -- The class-wide subprogram (if available) which corresponds to the
2267 -- renamed generic formal subprogram.
2269 Formal_Typ : Entity_Id := Empty;
2270 -- The generic formal type with unknown discriminants
2272 Root_Prim_OK : Boolean;
2273 Root_Prim_Op : Entity_Id;
2274 -- The root type primitive (if available) which corresponds to the
2275 -- renamed generic formal subprogram.
2277 Root_Typ : Entity_Id := Empty;
2278 -- The root type of Actual_Typ
2280 Body_Decl : Node_Id;
2282 Prim_Op : Entity_Id;
2283 Spec_Decl : Node_Id;
2286 -- Start of processing for Build_Class_Wide_Wrapper
2289 -- Analyze the specification of the renaming in case the generation
2290 -- of the class-wide wrapper fails.
2292 Ren_Id := Analyze_Subprogram_Specification (Spec);
2295 -- Do not attempt to build a wrapper if the renaming is in error
2297 if Error_Posted (Nam) then
2301 -- Analyze the renamed name, but do not resolve it. The resolution is
2302 -- completed once a suitable subprogram is found.
2306 -- When the renamed name denotes the intrinsic operator equals, the
2307 -- name must be treated as overloaded. This allows for a potential
2308 -- match against the root type's predefined equality function.
2310 if Is_Intrinsic_Equality (Entity (Nam)) then
2311 Set_Is_Overloaded (Nam);
2312 Collect_Interps (Nam);
2315 -- Step 1: Find the generic formal type with unknown discriminants
2316 -- and its corresponding class-wide actual type from the renamed
2317 -- generic formal subprogram.
2319 Formal := First_Formal (Formal_Spec);
2320 while Present (Formal) loop
2321 if Has_Unknown_Discriminants (Etype (Formal))
2322 and then not Is_Class_Wide_Type (Etype (Formal))
2323 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2325 Formal_Typ := Etype (Formal);
2326 Actual_Typ := Get_Instance_Of (Formal_Typ);
2327 Root_Typ := Etype (Actual_Typ);
2331 Next_Formal (Formal);
2334 -- The specification of the generic formal subprogram should always
2335 -- contain a formal type with unknown discriminants whose actual is
2336 -- a class-wide type, otherwise this indicates a failure in routine
2337 -- Has_Class_Wide_Actual.
2339 pragma Assert (Present (Formal_Typ));
2341 -- Step 2: Find the proper class-wide subprogram or primitive which
2342 -- corresponds to the renamed generic formal subprogram.
2344 CW_Prim_Op := Find_Primitive (Actual_Typ);
2345 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2346 Root_Prim_Op := Find_Primitive (Root_Typ);
2347 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2349 -- The class-wide actual type has two subprograms which correspond to
2350 -- the renamed generic formal subprogram:
2352 -- with procedure Prim_Op (Param : Formal_Typ);
2354 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2355 -- procedure Prim_Op (Param : Actual_Typ'Class);
2357 -- Even though the declaration of the two subprograms is legal, a
2358 -- call to either one is ambiguous and therefore illegal.
2360 if CW_Prim_OK and Root_Prim_OK then
2362 -- A user-defined primitive has precedence over a predefined one
2364 if Is_Internal (CW_Prim_Op)
2365 and then not Is_Internal (Root_Prim_Op)
2367 Prim_Op := Root_Prim_Op;
2369 elsif Is_Internal (Root_Prim_Op)
2370 and then not Is_Internal (CW_Prim_Op)
2372 Prim_Op := CW_Prim_Op;
2374 elsif CW_Prim_Op = Root_Prim_Op then
2375 Prim_Op := Root_Prim_Op;
2377 -- Otherwise both candidate subprograms are user-defined and
2382 ("ambiguous actual for generic subprogram &",
2384 Interpretation_Error (Root_Prim_Op);
2385 Interpretation_Error (CW_Prim_Op);
2389 elsif CW_Prim_OK and not Root_Prim_OK then
2390 Prim_Op := CW_Prim_Op;
2392 elsif not CW_Prim_OK and Root_Prim_OK then
2393 Prim_Op := Root_Prim_Op;
2395 -- An intrinsic equality may act as a suitable candidate in the case
2396 -- of a null type extension where the parent's equality is hidden. A
2397 -- call to an intrinsic equality is expanded as dispatching.
2399 elsif Present (Root_Prim_Op)
2400 and then Is_Intrinsic_Equality (Root_Prim_Op)
2402 Prim_Op := Root_Prim_Op;
2404 -- Otherwise there are no candidate subprograms. Let the caller
2405 -- diagnose the error.
2411 -- At this point resolution has taken place and the name is no longer
2412 -- overloaded. Mark the primitive as referenced.
2414 Set_Is_Overloaded (Name (N), False);
2415 Set_Referenced (Prim_Op);
2417 -- Do not generate a wrapper when the only candidate is a class-wide
2418 -- subprogram. Instead modify the renaming to directly map the actual
2419 -- to the generic formal.
2421 if CW_Prim_OK and then Prim_Op = CW_Prim_Op then
2423 Rewrite (Nam, New_Occurrence_Of (Prim_Op, Loc));
2427 -- Step 3: Create the declaration and the body of the wrapper, insert
2428 -- all the pieces into the tree.
2430 -- In GNATprove mode, create a function wrapper in the form of an
2431 -- expression function, so that an implicit postcondition relating
2432 -- the result of calling the wrapper function and the result of the
2433 -- dispatching call to the wrapped function is known during proof.
2436 and then Ekind_In (Ren_Id, E_Function, E_Operator)
2438 New_Spec := Build_Spec (Ren_Id);
2440 Make_Expression_Function (Loc,
2441 Specification => New_Spec,
2444 (Subp_Id => Prim_Op,
2445 Params => Parameter_Specifications (New_Spec)));
2447 Wrap_Id := Defining_Entity (Body_Decl);
2449 -- Otherwise, create separate spec and body for the subprogram
2453 Make_Subprogram_Declaration (Loc,
2454 Specification => Build_Spec (Ren_Id));
2455 Insert_Before_And_Analyze (N, Spec_Decl);
2457 Wrap_Id := Defining_Entity (Spec_Decl);
2460 Make_Subprogram_Body (Loc,
2461 Specification => Build_Spec (Ren_Id),
2462 Declarations => New_List,
2463 Handled_Statement_Sequence =>
2464 Make_Handled_Sequence_Of_Statements (Loc,
2465 Statements => New_List (
2467 (Subp_Id => Prim_Op,
2469 Parameter_Specifications
2470 (Specification (Spec_Decl))))));
2472 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
2475 -- If the operator carries an Eliminated pragma, indicate that the
2476 -- wrapper is also to be eliminated, to prevent spurious error when
2477 -- using gnatelim on programs that include box-initialization of
2478 -- equality operators.
2480 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2482 -- In GNATprove mode, insert the body in the tree for analysis
2484 if GNATprove_Mode then
2485 Insert_Before_And_Analyze (N, Body_Decl);
2488 -- The generated body does not freeze and must be analyzed when the
2489 -- class-wide wrapper is frozen. The body is only needed if expansion
2492 if Expander_Active then
2493 Append_Freeze_Action (Wrap_Id, Body_Decl);
2496 -- Step 4: The subprogram renaming aliases the wrapper
2498 Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc));
2499 end Build_Class_Wide_Wrapper;
2501 --------------------------
2502 -- Check_Null_Exclusion --
2503 --------------------------
2505 procedure Check_Null_Exclusion
2509 Ren_Formal : Entity_Id;
2510 Sub_Formal : Entity_Id;
2515 Ren_Formal := First_Formal (Ren);
2516 Sub_Formal := First_Formal (Sub);
2517 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2518 if Has_Null_Exclusion (Parent (Ren_Formal))
2520 not (Has_Null_Exclusion (Parent (Sub_Formal))
2521 or else Can_Never_Be_Null (Etype (Sub_Formal)))
2524 ("`NOT NULL` required for parameter &",
2525 Parent (Sub_Formal), Sub_Formal);
2528 Next_Formal (Ren_Formal);
2529 Next_Formal (Sub_Formal);
2532 -- Return profile check
2534 if Nkind (Parent (Ren)) = N_Function_Specification
2535 and then Nkind (Parent (Sub)) = N_Function_Specification
2536 and then Has_Null_Exclusion (Parent (Ren))
2537 and then not (Has_Null_Exclusion (Parent (Sub))
2538 or else Can_Never_Be_Null (Etype (Sub)))
2541 ("return must specify `NOT NULL`",
2542 Result_Definition (Parent (Sub)));
2544 end Check_Null_Exclusion;
2546 -------------------------------------
2547 -- Check_SPARK_Primitive_Operation --
2548 -------------------------------------
2550 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id) is
2551 Prag : constant Node_Id := SPARK_Pragma (Subp_Id);
2555 -- Nothing to do when the subprogram is not subject to SPARK_Mode On
2556 -- because this check applies to SPARK code only.
2558 if not (Present (Prag)
2559 and then Get_SPARK_Mode_From_Annotation (Prag) = On)
2563 -- Nothing to do when the subprogram is not a primitive operation
2565 elsif not Is_Primitive (Subp_Id) then
2569 Typ := Find_Dispatching_Type (Subp_Id);
2571 -- Nothing to do when the subprogram is a primitive operation of an
2578 -- At this point a renaming declaration introduces a new primitive
2579 -- operation for a tagged type.
2581 Error_Msg_Node_2 := Typ;
2583 ("subprogram renaming & cannot declare primitive for type & "
2584 & "(SPARK RM 6.1.1(3))", N, Subp_Id);
2585 end Check_SPARK_Primitive_Operation;
2587 ---------------------------
2588 -- Freeze_Actual_Profile --
2589 ---------------------------
2591 procedure Freeze_Actual_Profile is
2593 Has_Untagged_Inc : Boolean;
2594 Instantiation_Node : constant Node_Id := Parent (N);
2597 if Ada_Version >= Ada_2012 then
2598 F := First_Formal (Formal_Spec);
2599 Has_Untagged_Inc := False;
2600 while Present (F) loop
2601 if Ekind (Etype (F)) = E_Incomplete_Type
2602 and then not Is_Tagged_Type (Etype (F))
2604 Has_Untagged_Inc := True;
2608 F := Next_Formal (F);
2611 if Ekind (Formal_Spec) = E_Function
2612 and then not Is_Tagged_Type (Etype (Formal_Spec))
2614 Has_Untagged_Inc := True;
2617 if not Has_Untagged_Inc then
2618 F := First_Formal (Old_S);
2619 while Present (F) loop
2620 Freeze_Before (Instantiation_Node, Etype (F));
2622 if Is_Incomplete_Or_Private_Type (Etype (F))
2623 and then No (Underlying_Type (Etype (F)))
2625 -- Exclude generic types, or types derived from them.
2626 -- They will be frozen in the enclosing instance.
2628 if Is_Generic_Type (Etype (F))
2629 or else Is_Generic_Type (Root_Type (Etype (F)))
2633 -- A limited view of a type declared elsewhere needs no
2634 -- freezing actions.
2636 elsif From_Limited_With (Etype (F)) then
2641 ("type& must be frozen before this point",
2642 Instantiation_Node, Etype (F));
2646 F := Next_Formal (F);
2650 end Freeze_Actual_Profile;
2652 ---------------------------
2653 -- Has_Class_Wide_Actual --
2654 ---------------------------
2656 function Has_Class_Wide_Actual return Boolean is
2658 Formal_Typ : Entity_Id;
2662 Formal := First_Formal (Formal_Spec);
2663 while Present (Formal) loop
2664 Formal_Typ := Etype (Formal);
2666 if Has_Unknown_Discriminants (Formal_Typ)
2667 and then not Is_Class_Wide_Type (Formal_Typ)
2668 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2673 Next_Formal (Formal);
2678 end Has_Class_Wide_Actual;
2680 -------------------------
2681 -- Original_Subprogram --
2682 -------------------------
2684 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2685 Orig_Decl : Node_Id;
2686 Orig_Subp : Entity_Id;
2689 -- First case: renamed entity is itself a renaming
2691 if Present (Alias (Subp)) then
2692 return Alias (Subp);
2694 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2695 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2697 -- Check if renamed entity is a renaming_as_body
2700 Unit_Declaration_Node
2701 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2703 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2704 Orig_Subp := Entity (Name (Orig_Decl));
2706 if Orig_Subp = Rename_Spec then
2708 -- Circularity detected
2713 return (Original_Subprogram (Orig_Subp));
2721 end Original_Subprogram;
2725 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2726 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2727 -- defaulted formal subprogram when the actual for a related formal
2728 -- type is class-wide.
2730 Inst_Node : Node_Id := Empty;
2733 -- Start of processing for Analyze_Subprogram_Renaming
2736 -- We must test for the attribute renaming case before the Analyze
2737 -- call because otherwise Sem_Attr will complain that the attribute
2738 -- is missing an argument when it is analyzed.
2740 if Nkind (Nam) = N_Attribute_Reference then
2742 -- In the case of an abstract formal subprogram association, rewrite
2743 -- an actual given by a stream attribute as the name of the
2744 -- corresponding stream primitive of the type.
2746 -- In a generic context the stream operations are not generated, and
2747 -- this must be treated as a normal attribute reference, to be
2748 -- expanded in subsequent instantiations.
2751 and then Is_Abstract_Subprogram (Formal_Spec)
2752 and then Expander_Active
2755 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
2756 Stream_Prim : Entity_Id;
2759 -- The class-wide forms of the stream attributes are not
2760 -- primitive dispatching operations (even though they
2761 -- internally dispatch to a stream attribute).
2763 if Is_Class_Wide_Type (Prefix_Type) then
2765 ("attribute must be a primitive dispatching operation",
2770 -- Retrieve the primitive subprogram associated with the
2771 -- attribute. This can only be a stream attribute, since those
2772 -- are the only ones that are dispatching (and the actual for
2773 -- an abstract formal subprogram must be dispatching
2776 case Attribute_Name (Nam) is
2779 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input);
2783 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output);
2787 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read);
2791 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write);
2795 ("attribute must be a primitive dispatching operation",
2800 -- If no operation was found, and the type is limited, the user
2801 -- should have defined one.
2803 if No (Stream_Prim) then
2804 if Is_Limited_Type (Prefix_Type) then
2806 ("stream operation not defined for type&",
2810 -- Otherwise, compiler should have generated default
2813 raise Program_Error;
2817 -- Rewrite the attribute into the name of its corresponding
2818 -- primitive dispatching subprogram. We can then proceed with
2819 -- the usual processing for subprogram renamings.
2822 Prim_Name : constant Node_Id :=
2823 Make_Identifier (Sloc (Nam),
2824 Chars => Chars (Stream_Prim));
2826 Set_Entity (Prim_Name, Stream_Prim);
2827 Rewrite (Nam, Prim_Name);
2832 -- Normal processing for a renaming of an attribute
2835 Attribute_Renaming (N);
2840 -- Check whether this declaration corresponds to the instantiation of a
2841 -- formal subprogram.
2843 -- If this is an instantiation, the corresponding actual is frozen and
2844 -- error messages can be made more precise. If this is a default
2845 -- subprogram, the entity is already established in the generic, and is
2846 -- not retrieved by visibility. If it is a default with a box, the
2847 -- candidate interpretations, if any, have been collected when building
2848 -- the renaming declaration. If overloaded, the proper interpretation is
2849 -- determined in Find_Renamed_Entity. If the entity is an operator,
2850 -- Find_Renamed_Entity applies additional visibility checks.
2853 Inst_Node := Unit_Declaration_Node (Formal_Spec);
2855 -- Check whether the renaming is for a defaulted actual subprogram
2856 -- with a class-wide actual.
2858 -- The class-wide wrapper is not needed in GNATprove_Mode and there
2859 -- is an external axiomatization on the package.
2862 and then Box_Present (Inst_Node)
2866 Present (Containing_Package_With_Ext_Axioms (Formal_Spec)))
2868 Build_Class_Wide_Wrapper (New_S, Old_S);
2870 elsif Is_Entity_Name (Nam)
2871 and then Present (Entity (Nam))
2872 and then not Comes_From_Source (Nam)
2873 and then not Is_Overloaded (Nam)
2875 Old_S := Entity (Nam);
2877 -- The subprogram renaming declaration may become Ghost if it
2878 -- renames a Ghost entity.
2880 Mark_Ghost_Renaming (N, Old_S);
2882 New_S := Analyze_Subprogram_Specification (Spec);
2886 if Ekind (Old_S) = E_Operator then
2890 if Box_Present (Inst_Node) then
2891 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2893 -- If there is an immediately visible homonym of the operator
2894 -- and the declaration has a default, this is worth a warning
2895 -- because the user probably did not intend to get the pre-
2896 -- defined operator, visible in the generic declaration. To
2897 -- find if there is an intended candidate, analyze the renaming
2898 -- again in the current context.
2900 elsif Scope (Old_S) = Standard_Standard
2901 and then Present (Default_Name (Inst_Node))
2904 Decl : constant Node_Id := New_Copy_Tree (N);
2908 Set_Entity (Name (Decl), Empty);
2909 Analyze (Name (Decl));
2911 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
2914 and then In_Open_Scopes (Scope (Hidden))
2915 and then Is_Immediately_Visible (Hidden)
2916 and then Comes_From_Source (Hidden)
2917 and then Hidden /= Old_S
2919 Error_Msg_Sloc := Sloc (Hidden);
2921 ("default subprogram is resolved in the generic "
2922 & "declaration (RM 12.6(17))??", N);
2923 Error_Msg_NE ("\and will not use & #??", N, Hidden);
2932 -- The subprogram renaming declaration may become Ghost if it
2933 -- renames a Ghost entity.
2935 if Is_Entity_Name (Nam) then
2936 Mark_Ghost_Renaming (N, Entity (Nam));
2939 New_S := Analyze_Subprogram_Specification (Spec);
2943 -- Renamed entity must be analyzed first, to avoid being hidden by
2944 -- new name (which might be the same in a generic instance).
2948 -- The subprogram renaming declaration may become Ghost if it renames
2951 if Is_Entity_Name (Nam) then
2952 Mark_Ghost_Renaming (N, Entity (Nam));
2955 -- The renaming defines a new overloaded entity, which is analyzed
2956 -- like a subprogram declaration.
2958 New_S := Analyze_Subprogram_Specification (Spec);
2961 if Current_Scope /= Standard_Standard then
2962 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
2965 -- Set SPARK mode from current context
2967 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
2968 Set_SPARK_Pragma_Inherited (New_S);
2970 Rename_Spec := Find_Corresponding_Spec (N);
2972 -- Case of Renaming_As_Body
2974 if Present (Rename_Spec) then
2975 Check_Previous_Null_Procedure (N, Rename_Spec);
2977 -- Renaming declaration is the completion of the declaration of
2978 -- Rename_Spec. We build an actual body for it at the freezing point.
2980 Set_Corresponding_Spec (N, Rename_Spec);
2982 -- Deal with special case of stream functions of abstract types
2985 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
2986 N_Abstract_Subprogram_Declaration
2988 -- Input stream functions are abstract if the object type is
2989 -- abstract. Similarly, all default stream functions for an
2990 -- interface type are abstract. However, these subprograms may
2991 -- receive explicit declarations in representation clauses, making
2992 -- the attribute subprograms usable as defaults in subsequent
2994 -- In this case we rewrite the declaration to make the subprogram
2995 -- non-abstract. We remove the previous declaration, and insert
2996 -- the new one at the point of the renaming, to prevent premature
2997 -- access to unfrozen types. The new declaration reuses the
2998 -- specification of the previous one, and must not be analyzed.
3001 (Is_Primitive (Entity (Nam))
3003 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
3005 Old_Decl : constant Node_Id :=
3006 Unit_Declaration_Node (Rename_Spec);
3007 New_Decl : constant Node_Id :=
3008 Make_Subprogram_Declaration (Sloc (N),
3010 Relocate_Node (Specification (Old_Decl)));
3013 Insert_After (N, New_Decl);
3014 Set_Is_Abstract_Subprogram (Rename_Spec, False);
3015 Set_Analyzed (New_Decl);
3019 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
3021 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3022 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
3025 Set_Convention (New_S, Convention (Rename_Spec));
3026 Check_Fully_Conformant (New_S, Rename_Spec);
3027 Set_Public_Status (New_S);
3029 if No_Return (Rename_Spec)
3030 and then not No_Return (Entity (Nam))
3032 Error_Msg_N ("renaming completes a No_Return procedure", N);
3034 ("\renamed procedure must be nonreturning (RM 6.5.1 (7/2))", N);
3037 -- The specification does not introduce new formals, but only
3038 -- repeats the formals of the original subprogram declaration.
3039 -- For cross-reference purposes, and for refactoring tools, we
3040 -- treat the formals of the renaming declaration as body formals.
3042 Reference_Body_Formals (Rename_Spec, New_S);
3044 -- Indicate that the entity in the declaration functions like the
3045 -- corresponding body, and is not a new entity. The body will be
3046 -- constructed later at the freeze point, so indicate that the
3047 -- completion has not been seen yet.
3049 Set_Ekind (New_S, E_Subprogram_Body);
3050 New_S := Rename_Spec;
3051 Set_Has_Completion (Rename_Spec, False);
3053 -- Ada 2005: check overriding indicator
3055 if Present (Overridden_Operation (Rename_Spec)) then
3056 if Must_Not_Override (Specification (N)) then
3058 ("subprogram& overrides inherited operation",
3062 and then not Must_Override (Specification (N))
3064 Style.Missing_Overriding (N, Rename_Spec);
3067 elsif Must_Override (Specification (N)) then
3068 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
3071 -- Normal subprogram renaming (not renaming as body)
3074 Generate_Definition (New_S);
3075 New_Overloaded_Entity (New_S);
3077 if not (Is_Entity_Name (Nam)
3078 and then Is_Intrinsic_Subprogram (Entity (Nam)))
3080 Check_Delayed_Subprogram (New_S);
3083 -- Verify that a SPARK renaming does not declare a primitive
3084 -- operation of a tagged type.
3086 Check_SPARK_Primitive_Operation (New_S);
3089 -- There is no need for elaboration checks on the new entity, which may
3090 -- be called before the next freezing point where the body will appear.
3091 -- Elaboration checks refer to the real entity, not the one created by
3092 -- the renaming declaration.
3094 Set_Kill_Elaboration_Checks (New_S, True);
3096 -- If we had a previous error, indicate a completely is present to stop
3097 -- junk cascaded messages, but don't take any further action.
3099 if Etype (Nam) = Any_Type then
3100 Set_Has_Completion (New_S);
3103 -- Case where name has the form of a selected component
3105 elsif Nkind (Nam) = N_Selected_Component then
3107 -- A name which has the form A.B can designate an entry of task A, a
3108 -- protected operation of protected object A, or finally a primitive
3109 -- operation of object A. In the later case, A is an object of some
3110 -- tagged type, or an access type that denotes one such. To further
3111 -- distinguish these cases, note that the scope of a task entry or
3112 -- protected operation is type of the prefix.
3114 -- The prefix could be an overloaded function call that returns both
3115 -- kinds of operations. This overloading pathology is left to the
3116 -- dedicated reader ???
3119 T : constant Entity_Id := Etype (Prefix (Nam));
3127 and then Is_Tagged_Type (Designated_Type (T))))
3128 and then Scope (Entity (Selector_Name (Nam))) /= T
3130 Analyze_Renamed_Primitive_Operation
3131 (N, New_S, Present (Rename_Spec));
3135 -- Renamed entity is an entry or protected operation. For those
3136 -- cases an explicit body is built (at the point of freezing of
3137 -- this entity) that contains a call to the renamed entity.
3139 -- This is not allowed for renaming as body if the renamed
3140 -- spec is already frozen (see RM 8.5.4(5) for details).
3142 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
3144 ("renaming-as-body cannot rename entry as subprogram", N);
3146 ("\since & is already frozen (RM 8.5.4(5))",
3149 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
3156 -- Case where name is an explicit dereference X.all
3158 elsif Nkind (Nam) = N_Explicit_Dereference then
3160 -- Renamed entity is designated by access_to_subprogram expression.
3161 -- Must build body to encapsulate call, as in the entry case.
3163 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
3166 -- Indexed component
3168 elsif Nkind (Nam) = N_Indexed_Component then
3169 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
3172 -- Character literal
3174 elsif Nkind (Nam) = N_Character_Literal then
3175 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
3178 -- Only remaining case is where we have a non-entity name, or a renaming
3179 -- of some other non-overloadable entity.
3181 elsif not Is_Entity_Name (Nam)
3182 or else not Is_Overloadable (Entity (Nam))
3184 -- Do not mention the renaming if it comes from an instance
3186 if not Is_Actual then
3187 Error_Msg_N ("expect valid subprogram name in renaming", N);
3189 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
3195 -- Find the renamed entity that matches the given specification. Disable
3196 -- Ada_83 because there is no requirement of full conformance between
3197 -- renamed entity and new entity, even though the same circuit is used.
3199 -- This is a bit of an odd case, which introduces a really irregular use
3200 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
3203 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
3204 Ada_Version_Pragma := Empty;
3205 Ada_Version_Explicit := Ada_Version;
3208 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3210 -- The visible operation may be an inherited abstract operation that
3211 -- was overridden in the private part, in which case a call will
3212 -- dispatch to the overriding operation. Use the overriding one in
3213 -- the renaming declaration, to prevent spurious errors below.
3215 if Is_Overloadable (Old_S)
3216 and then Is_Abstract_Subprogram (Old_S)
3217 and then No (DTC_Entity (Old_S))
3218 and then Present (Alias (Old_S))
3219 and then not Is_Abstract_Subprogram (Alias (Old_S))
3220 and then Present (Overridden_Operation (Alias (Old_S)))
3222 Old_S := Alias (Old_S);
3225 -- When the renamed subprogram is overloaded and used as an actual
3226 -- of a generic, its entity is set to the first available homonym.
3227 -- We must first disambiguate the name, then set the proper entity.
3229 if Is_Actual and then Is_Overloaded (Nam) then
3230 Set_Entity (Nam, Old_S);
3234 -- Most common case: subprogram renames subprogram. No body is generated
3235 -- in this case, so we must indicate the declaration is complete as is.
3236 -- and inherit various attributes of the renamed subprogram.
3238 if No (Rename_Spec) then
3239 Set_Has_Completion (New_S);
3240 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3241 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3242 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3244 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3245 -- between a subprogram and its correct renaming.
3247 -- Note: the Any_Id check is a guard that prevents compiler crashes
3248 -- when performing a null exclusion check between a renaming and a
3249 -- renamed subprogram that has been found to be illegal.
3251 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3252 Check_Null_Exclusion
3254 Sub => Entity (Nam));
3257 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3258 -- overriding. The flag Requires_Overriding is set very selectively
3259 -- and misses some other illegal cases. The additional conditions
3260 -- checked below are sufficient but not necessary ???
3262 -- The rule does not apply to the renaming generated for an actual
3263 -- subprogram in an instance.
3268 -- Guard against previous errors, and omit renamings of predefined
3271 elsif not Ekind_In (Old_S, E_Function, E_Procedure) then
3274 elsif Requires_Overriding (Old_S)
3276 (Is_Abstract_Subprogram (Old_S)
3277 and then Present (Find_Dispatching_Type (Old_S))
3278 and then not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3281 ("renamed entity cannot be subprogram that requires overriding "
3282 & "(RM 8.5.4 (5.1))", N);
3286 Prev : constant Entity_Id := Overridden_Operation (New_S);
3290 (Has_Non_Trivial_Precondition (Prev)
3291 or else Has_Non_Trivial_Precondition (Old_S))
3294 ("conflicting inherited classwide preconditions in renaming "
3295 & "of& (RM 6.1.1 (17)", N, Old_S);
3300 if Old_S /= Any_Id then
3301 if Is_Actual and then From_Default (N) then
3303 -- This is an implicit reference to the default actual
3305 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3308 Generate_Reference (Old_S, Nam);
3311 Check_Internal_Protected_Use (N, Old_S);
3313 -- For a renaming-as-body, require subtype conformance, but if the
3314 -- declaration being completed has not been frozen, then inherit the
3315 -- convention of the renamed subprogram prior to checking conformance
3316 -- (unless the renaming has an explicit convention established; the
3317 -- rule stated in the RM doesn't seem to address this ???).
3319 if Present (Rename_Spec) then
3320 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3321 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3323 if not Is_Frozen (Rename_Spec) then
3324 if not Has_Convention_Pragma (Rename_Spec) then
3325 Set_Convention (New_S, Convention (Old_S));
3328 if Ekind (Old_S) /= E_Operator then
3329 Check_Mode_Conformant (New_S, Old_S, Spec);
3332 if Original_Subprogram (Old_S) = Rename_Spec then
3333 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
3336 Check_Subtype_Conformant (New_S, Old_S, Spec);
3339 Check_Frozen_Renaming (N, Rename_Spec);
3341 -- Check explicitly that renamed entity is not intrinsic, because
3342 -- in a generic the renamed body is not built. In this case,
3343 -- the renaming_as_body is a completion.
3345 if Inside_A_Generic then
3346 if Is_Frozen (Rename_Spec)
3347 and then Is_Intrinsic_Subprogram (Old_S)
3350 ("subprogram in renaming_as_body cannot be intrinsic",
3354 Set_Has_Completion (Rename_Spec);
3357 elsif Ekind (Old_S) /= E_Operator then
3359 -- If this a defaulted subprogram for a class-wide actual there is
3360 -- no check for mode conformance, given that the signatures don't
3361 -- match (the source mentions T but the actual mentions T'Class).
3366 -- No need for a redundant error message if this is a nested
3367 -- instance, unless the current instantiation (of a child unit)
3368 -- is a compilation unit, which is not analyzed when the parent
3369 -- generic is analyzed.
3372 or else No (Enclosing_Instance)
3373 or else Is_Compilation_Unit (Current_Scope)
3375 Check_Mode_Conformant (New_S, Old_S);
3378 if Is_Actual and then Error_Posted (New_S) then
3379 Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
3383 if No (Rename_Spec) then
3385 -- The parameter profile of the new entity is that of the renamed
3386 -- entity: the subtypes given in the specification are irrelevant.
3388 Inherit_Renamed_Profile (New_S, Old_S);
3390 -- A call to the subprogram is transformed into a call to the
3391 -- renamed entity. This is transitive if the renamed entity is
3392 -- itself a renaming.
3394 if Present (Alias (Old_S)) then
3395 Set_Alias (New_S, Alias (Old_S));
3397 Set_Alias (New_S, Old_S);
3400 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3401 -- renaming as body, since the entity in this case is not an
3402 -- intrinsic (it calls an intrinsic, but we have a real body for
3403 -- this call, and it is in this body that the required intrinsic
3404 -- processing will take place).
3406 -- Also, if this is a renaming of inequality, the renamed operator
3407 -- is intrinsic, but what matters is the corresponding equality
3408 -- operator, which may be user-defined.
3410 Set_Is_Intrinsic_Subprogram
3412 Is_Intrinsic_Subprogram (Old_S)
3414 (Chars (Old_S) /= Name_Op_Ne
3415 or else Ekind (Old_S) = E_Operator
3416 or else Is_Intrinsic_Subprogram
3417 (Corresponding_Equality (Old_S))));
3419 if Ekind (Alias (New_S)) = E_Operator then
3420 Set_Has_Delayed_Freeze (New_S, False);
3423 -- If the renaming corresponds to an association for an abstract
3424 -- formal subprogram, then various attributes must be set to
3425 -- indicate that the renaming is an abstract dispatching operation
3426 -- with a controlling type.
3428 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
3430 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3431 -- see it as corresponding to a generic association for a
3432 -- formal abstract subprogram
3434 Set_Is_Abstract_Subprogram (New_S);
3437 New_S_Ctrl_Type : constant Entity_Id :=
3438 Find_Dispatching_Type (New_S);
3439 Old_S_Ctrl_Type : constant Entity_Id :=
3440 Find_Dispatching_Type (Old_S);
3444 -- The actual must match the (instance of the) formal,
3445 -- and must be a controlling type.
3447 if Old_S_Ctrl_Type /= New_S_Ctrl_Type
3448 or else No (New_S_Ctrl_Type)
3450 if No (New_S_Ctrl_Type) then
3452 ("actual must be dispatching subprogram", Nam);
3455 ("actual must be dispatching subprogram for type&",
3456 Nam, New_S_Ctrl_Type);
3460 Set_Is_Dispatching_Operation (New_S);
3461 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3463 -- If the actual in the formal subprogram is itself a
3464 -- formal abstract subprogram association, there's no
3465 -- dispatch table component or position to inherit.
3467 if Present (DTC_Entity (Old_S)) then
3468 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3469 Set_DT_Position_Value (New_S, DT_Position (Old_S));
3479 -- The following is illegal, because F hides whatever other F may
3481 -- function F (...) renames F;
3484 or else (Nkind (Nam) /= N_Expanded_Name
3485 and then Chars (Old_S) = Chars (New_S))
3487 Error_Msg_N ("subprogram cannot rename itself", N);
3489 -- This is illegal even if we use a selector:
3490 -- function F (...) renames Pkg.F;
3491 -- because F is still hidden.
3493 elsif Nkind (Nam) = N_Expanded_Name
3494 and then Entity (Prefix (Nam)) = Current_Scope
3495 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3497 -- This is an error, but we overlook the error and accept the
3498 -- renaming if the special Overriding_Renamings mode is in effect.
3500 if not Overriding_Renamings then
3502 ("implicit operation& is not visible (RM 8.3 (15))",
3507 Set_Convention (New_S, Convention (Old_S));
3509 if Is_Abstract_Subprogram (Old_S) then
3510 if Present (Rename_Spec) then
3512 ("a renaming-as-body cannot rename an abstract subprogram",
3514 Set_Has_Completion (Rename_Spec);
3516 Set_Is_Abstract_Subprogram (New_S);
3520 Check_Library_Unit_Renaming (N, Old_S);
3522 -- Pathological case: procedure renames entry in the scope of its
3523 -- task. Entry is given by simple name, but body must be built for
3524 -- procedure. Of course if called it will deadlock.
3526 if Ekind (Old_S) = E_Entry then
3527 Set_Has_Completion (New_S, False);
3528 Set_Alias (New_S, Empty);
3531 -- Do not freeze the renaming nor the renamed entity when the context
3532 -- is an enclosing generic. Freezing is an expansion activity, and in
3533 -- addition the renamed entity may depend on the generic formals of
3534 -- the enclosing generic.
3536 if Is_Actual and not Inside_A_Generic then
3537 Freeze_Before (N, Old_S);
3538 Freeze_Actual_Profile;
3539 Set_Has_Delayed_Freeze (New_S, False);
3540 Freeze_Before (N, New_S);
3542 -- An abstract subprogram is only allowed as an actual in the case
3543 -- where the formal subprogram is also abstract.
3545 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
3546 and then Is_Abstract_Subprogram (Old_S)
3547 and then not Is_Abstract_Subprogram (Formal_Spec)
3550 ("abstract subprogram not allowed as generic actual", Nam);
3555 -- A common error is to assume that implicit operators for types are
3556 -- defined in Standard, or in the scope of a subtype. In those cases
3557 -- where the renamed entity is given with an expanded name, it is
3558 -- worth mentioning that operators for the type are not declared in
3559 -- the scope given by the prefix.
3561 if Nkind (Nam) = N_Expanded_Name
3562 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
3563 and then Scope (Entity (Nam)) = Standard_Standard
3566 T : constant Entity_Id :=
3567 Base_Type (Etype (First_Formal (New_S)));
3569 Error_Msg_Node_2 := Prefix (Nam);
3571 ("operator for type& is not declared in&", Prefix (Nam), T);
3576 ("no visible subprogram matches the specification for&",
3580 if Present (Candidate_Renaming) then
3587 F1 := First_Formal (Candidate_Renaming);
3588 F2 := First_Formal (New_S);
3589 T1 := First_Subtype (Etype (F1));
3590 while Present (F1) and then Present (F2) loop
3595 if Present (F1) and then Present (Default_Value (F1)) then
3596 if Present (Next_Formal (F1)) then
3598 ("\missing specification for & and other formals with "
3599 & "defaults", Spec, F1);
3601 Error_Msg_NE ("\missing specification for &", Spec, F1);
3605 if Nkind (Nam) = N_Operator_Symbol
3606 and then From_Default (N)
3608 Error_Msg_Node_2 := T1;
3610 ("default & on & is not directly visible", Nam, Nam);
3616 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
3617 -- controlling access parameters are known non-null for the renamed
3618 -- subprogram. Test also applies to a subprogram instantiation that
3619 -- is dispatching. Test is skipped if some previous error was detected
3620 -- that set Old_S to Any_Id.
3622 if Ada_Version >= Ada_2005
3623 and then Old_S /= Any_Id
3624 and then not Is_Dispatching_Operation (Old_S)
3625 and then Is_Dispatching_Operation (New_S)
3632 Old_F := First_Formal (Old_S);
3633 New_F := First_Formal (New_S);
3634 while Present (Old_F) loop
3635 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
3636 and then Is_Controlling_Formal (New_F)
3637 and then not Can_Never_Be_Null (Old_F)
3639 Error_Msg_N ("access parameter is controlling,", New_F);
3641 ("\corresponding parameter of& must be explicitly null "
3642 & "excluding", New_F, Old_S);
3645 Next_Formal (Old_F);
3646 Next_Formal (New_F);
3651 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
3652 -- is to warn if an operator is being renamed as a different operator.
3653 -- If the operator is predefined, examine the kind of the entity, not
3654 -- the abbreviated declaration in Standard.
3656 if Comes_From_Source (N)
3657 and then Present (Old_S)
3658 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3659 or else Ekind (Old_S) = E_Operator)
3660 and then Nkind (New_S) = N_Defining_Operator_Symbol
3661 and then Chars (Old_S) /= Chars (New_S)
3664 ("& is being renamed as a different operator??", N, Old_S);
3667 -- Check for renaming of obsolescent subprogram
3669 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3671 -- Another warning or some utility: if the new subprogram as the same
3672 -- name as the old one, the old one is not hidden by an outer homograph,
3673 -- the new one is not a public symbol, and the old one is otherwise
3674 -- directly visible, the renaming is superfluous.
3676 if Chars (Old_S) = Chars (New_S)
3677 and then Comes_From_Source (N)
3678 and then Scope (Old_S) /= Standard_Standard
3679 and then Warn_On_Redundant_Constructs
3680 and then (Is_Immediately_Visible (Old_S)
3681 or else Is_Potentially_Use_Visible (Old_S))
3682 and then Is_Overloadable (Current_Scope)
3683 and then Chars (Current_Scope) /= Chars (Old_S)
3686 ("redundant renaming, entity is directly visible?r?", Name (N));
3689 -- Implementation-defined aspect specifications can appear in a renaming
3690 -- declaration, but not language-defined ones. The call to procedure
3691 -- Analyze_Aspect_Specifications will take care of this error check.
3693 if Has_Aspects (N) then
3694 Analyze_Aspect_Specifications (N, New_S);
3697 Ada_Version := Save_AV;
3698 Ada_Version_Pragma := Save_AVP;
3699 Ada_Version_Explicit := Save_AV_Exp;
3701 -- In GNATprove mode, the renamings of actual subprograms are replaced
3702 -- with wrapper functions that make it easier to propagate axioms to the
3703 -- points of call within an instance. Wrappers are generated if formal
3704 -- subprogram is subject to axiomatization.
3706 -- The types in the wrapper profiles are obtained from (instances of)
3707 -- the types of the formal subprogram.
3710 and then GNATprove_Mode
3711 and then Present (Containing_Package_With_Ext_Axioms (Formal_Spec))
3712 and then not Inside_A_Generic
3714 if Ekind (Old_S) = E_Function then
3715 Rewrite (N, Build_Function_Wrapper (Formal_Spec, Old_S));
3718 elsif Ekind (Old_S) = E_Operator then
3719 Rewrite (N, Build_Operator_Wrapper (Formal_Spec, Old_S));
3724 -- Check if we are looking at an Ada 2012 defaulted formal subprogram
3725 -- and mark any use_package_clauses that affect the visibility of the
3726 -- implicit generic actual.
3728 -- Also, we may be looking at an internal renaming of a user-defined
3729 -- subprogram created for a generic formal subprogram association,
3730 -- which will also have to be marked here. This can occur when the
3731 -- corresponding formal subprogram contains references to other generic
3734 if Is_Generic_Actual_Subprogram (New_S)
3735 and then (Is_Intrinsic_Subprogram (New_S)
3736 or else From_Default (N)
3737 or else Nkind (N) = N_Subprogram_Renaming_Declaration)
3739 Mark_Use_Clauses (New_S);
3741 -- Handle overloaded subprograms
3743 if Present (Alias (New_S)) then
3744 Mark_Use_Clauses (Alias (New_S));
3747 end Analyze_Subprogram_Renaming;
3749 -------------------------
3750 -- Analyze_Use_Package --
3751 -------------------------
3753 -- Resolve the package names in the use clause, and make all the visible
3754 -- entities defined in the package potentially use-visible. If the package
3755 -- is already in use from a previous use clause, its visible entities are
3756 -- already use-visible. In that case, mark the occurrence as a redundant
3757 -- use. If the package is an open scope, i.e. if the use clause occurs
3758 -- within the package itself, ignore it.
3760 procedure Analyze_Use_Package (N : Node_Id; Chain : Boolean := True) is
3761 procedure Analyze_Package_Name (Clause : Node_Id);
3762 -- Perform analysis on a package name from a use_package_clause
3764 procedure Analyze_Package_Name_List (Head_Clause : Node_Id);
3765 -- Similar to Analyze_Package_Name but iterates over all the names
3768 --------------------------
3769 -- Analyze_Package_Name --
3770 --------------------------
3772 procedure Analyze_Package_Name (Clause : Node_Id) is
3773 Pack : constant Node_Id := Name (Clause);
3777 pragma Assert (Nkind (Clause) = N_Use_Package_Clause);
3780 -- Verify that the package standard is not directly named in a
3781 -- use_package_clause.
3783 if Nkind (Parent (Clause)) = N_Compilation_Unit
3784 and then Nkind (Pack) = N_Expanded_Name
3786 Pref := Prefix (Pack);
3788 while Nkind (Pref) = N_Expanded_Name loop
3789 Pref := Prefix (Pref);
3792 if Entity (Pref) = Standard_Standard then
3794 ("predefined package Standard cannot appear in a context "
3798 end Analyze_Package_Name;
3800 -------------------------------
3801 -- Analyze_Package_Name_List --
3802 -------------------------------
3804 procedure Analyze_Package_Name_List (Head_Clause : Node_Id) is
3808 -- Due to the way source use clauses are split during parsing we are
3809 -- forced to simply iterate through all entities in scope until the
3810 -- clause representing the last name in the list is found.
3812 Curr := Head_Clause;
3813 while Present (Curr) loop
3814 Analyze_Package_Name (Curr);
3816 -- Stop iterating over the names in the use clause when we are at
3819 exit when not More_Ids (Curr) and then Prev_Ids (Curr);
3822 end Analyze_Package_Name_List;
3828 -- Start of processing for Analyze_Use_Package
3831 Check_SPARK_05_Restriction ("use clause is not allowed", N);
3833 Set_Hidden_By_Use_Clause (N, No_Elist);
3835 -- Use clause not allowed in a spec of a predefined package declaration
3836 -- except that packages whose file name starts a-n are OK (these are
3837 -- children of Ada.Numerics, which are never loaded by Rtsfind).
3839 if Is_Predefined_Unit (Current_Sem_Unit)
3840 and then Get_Name_String
3841 (Unit_File_Name (Current_Sem_Unit)) (1 .. 3) /= "a-n"
3842 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
3843 N_Package_Declaration
3845 Error_Msg_N ("use clause not allowed in predefined spec", N);
3848 -- Loop through all package names from the original use clause in
3849 -- order to analyze referenced packages. A use_package_clause with only
3850 -- one name does not have More_Ids or Prev_Ids set, while a clause with
3851 -- More_Ids only starts the chain produced by the parser.
3853 if not More_Ids (N) and then not Prev_Ids (N) then
3854 Analyze_Package_Name (N);
3856 elsif More_Ids (N) and then not Prev_Ids (N) then
3857 Analyze_Package_Name_List (N);
3860 if not Is_Entity_Name (Name (N)) then
3861 Error_Msg_N ("& is not a package", Name (N));
3867 Chain_Use_Clause (N);
3870 Pack := Entity (Name (N));
3872 -- There are many cases where scopes are manipulated during analysis, so
3873 -- check that Pack's current use clause has not already been chained
3874 -- before setting its previous use clause.
3876 if Ekind (Pack) = E_Package
3877 and then Present (Current_Use_Clause (Pack))
3878 and then Current_Use_Clause (Pack) /= N
3879 and then No (Prev_Use_Clause (N))
3880 and then Prev_Use_Clause (Current_Use_Clause (Pack)) /= N
3882 Set_Prev_Use_Clause (N, Current_Use_Clause (Pack));
3885 -- Mark all entities as potentially use visible.
3887 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
3888 if Ekind (Pack) = E_Generic_Package then
3889 Error_Msg_N -- CODEFIX
3890 ("a generic package is not allowed in a use clause", Name (N));
3892 elsif Ekind_In (Pack, E_Generic_Function, E_Generic_Package)
3894 Error_Msg_N -- CODEFIX
3895 ("a generic subprogram is not allowed in a use clause",
3898 elsif Ekind_In (Pack, E_Function, E_Procedure, E_Operator) then
3899 Error_Msg_N -- CODEFIX
3900 ("a subprogram is not allowed in a use clause", Name (N));
3903 Error_Msg_N ("& is not allowed in a use clause", Name (N));
3907 if Nkind (Parent (N)) = N_Compilation_Unit then
3908 Check_In_Previous_With_Clause (N, Name (N));
3911 Use_One_Package (N, Name (N));
3914 Mark_Ghost_Clause (N);
3915 end Analyze_Use_Package;
3917 ----------------------
3918 -- Analyze_Use_Type --
3919 ----------------------
3921 procedure Analyze_Use_Type (N : Node_Id; Chain : Boolean := True) is
3926 Set_Hidden_By_Use_Clause (N, No_Elist);
3928 -- Chain clause to list of use clauses in current scope when flagged
3931 Chain_Use_Clause (N);
3934 -- Obtain the base type of the type denoted within the use_type_clause's
3937 Id := Subtype_Mark (N);
3939 E := Base_Type (Entity (Id));
3941 -- There are many cases where a use_type_clause may be reanalyzed due to
3942 -- manipulation of the scope stack so we much guard against those cases
3943 -- here, otherwise, we must add the new use_type_clause to the previous
3944 -- use_type_clause chain in order to mark redundant use_type_clauses as
3945 -- used. When the redundant use-type clauses appear in a parent unit and
3946 -- a child unit we must prevent a circularity in the chain that would
3947 -- otherwise result from the separate steps of analysis and installation
3948 -- of the parent context.
3950 if Present (Current_Use_Clause (E))
3951 and then Current_Use_Clause (E) /= N
3952 and then Prev_Use_Clause (Current_Use_Clause (E)) /= N
3953 and then No (Prev_Use_Clause (N))
3955 Set_Prev_Use_Clause (N, Current_Use_Clause (E));
3958 -- If the Used_Operations list is already initialized, the clause has
3959 -- been analyzed previously, and it is being reinstalled, for example
3960 -- when the clause appears in a package spec and we are compiling the
3961 -- corresponding package body. In that case, make the entities on the
3962 -- existing list use_visible, and mark the corresponding types In_Use.
3964 if Present (Used_Operations (N)) then
3969 Use_One_Type (Subtype_Mark (N), Installed => True);
3971 Elmt := First_Elmt (Used_Operations (N));
3972 while Present (Elmt) loop
3973 Set_Is_Potentially_Use_Visible (Node (Elmt));
3981 -- Otherwise, create new list and attach to it the operations that are
3982 -- made use-visible by the clause.
3984 Set_Used_Operations (N, New_Elmt_List);
3987 if E /= Any_Type then
3990 if Nkind (Parent (N)) = N_Compilation_Unit then
3991 if Nkind (Id) = N_Identifier then
3992 Error_Msg_N ("type is not directly visible", Id);
3994 elsif Is_Child_Unit (Scope (E))
3995 and then Scope (E) /= System_Aux_Id
3997 Check_In_Previous_With_Clause (N, Prefix (Id));
4002 -- If the use_type_clause appears in a compilation unit context,
4003 -- check whether it comes from a unit that may appear in a
4004 -- limited_with_clause, for a better error message.
4006 if Nkind (Parent (N)) = N_Compilation_Unit
4007 and then Nkind (Id) /= N_Identifier
4013 function Mentioned (Nam : Node_Id) return Boolean;
4014 -- Check whether the prefix of expanded name for the type
4015 -- appears in the prefix of some limited_with_clause.
4021 function Mentioned (Nam : Node_Id) return Boolean is
4023 return Nkind (Name (Item)) = N_Selected_Component
4024 and then Chars (Prefix (Name (Item))) = Chars (Nam);
4028 Pref := Prefix (Id);
4029 Item := First (Context_Items (Parent (N)));
4030 while Present (Item) and then Item /= N loop
4031 if Nkind (Item) = N_With_Clause
4032 and then Limited_Present (Item)
4033 and then Mentioned (Pref)
4036 (Get_Msg_Id, "premature usage of incomplete type");
4045 Mark_Ghost_Clause (N);
4046 end Analyze_Use_Type;
4048 ------------------------
4049 -- Attribute_Renaming --
4050 ------------------------
4052 procedure Attribute_Renaming (N : Node_Id) is
4053 Loc : constant Source_Ptr := Sloc (N);
4054 Nam : constant Node_Id := Name (N);
4055 Spec : constant Node_Id := Specification (N);
4056 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
4057 Aname : constant Name_Id := Attribute_Name (Nam);
4059 Form_Num : Nat := 0;
4060 Expr_List : List_Id := No_List;
4062 Attr_Node : Node_Id;
4063 Body_Node : Node_Id;
4064 Param_Spec : Node_Id;
4067 Generate_Definition (New_S);
4069 -- This procedure is called in the context of subprogram renaming, and
4070 -- thus the attribute must be one that is a subprogram. All of those
4071 -- have at least one formal parameter, with the exceptions of the GNAT
4072 -- attribute 'Img, which GNAT treats as renameable.
4074 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
4075 if Aname /= Name_Img then
4077 ("subprogram renaming an attribute must have formals", N);
4082 Param_Spec := First (Parameter_Specifications (Spec));
4083 while Present (Param_Spec) loop
4084 Form_Num := Form_Num + 1;
4086 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4087 Find_Type (Parameter_Type (Param_Spec));
4089 -- The profile of the new entity denotes the base type (s) of
4090 -- the types given in the specification. For access parameters
4091 -- there are no subtypes involved.
4093 Rewrite (Parameter_Type (Param_Spec),
4095 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
4098 if No (Expr_List) then
4099 Expr_List := New_List;
4102 Append_To (Expr_List,
4103 Make_Identifier (Loc,
4104 Chars => Chars (Defining_Identifier (Param_Spec))));
4106 -- The expressions in the attribute reference are not freeze
4107 -- points. Neither is the attribute as a whole, see below.
4109 Set_Must_Not_Freeze (Last (Expr_List));
4114 -- Immediate error if too many formals. Other mismatches in number or
4115 -- types of parameters are detected when we analyze the body of the
4116 -- subprogram that we construct.
4118 if Form_Num > 2 then
4119 Error_Msg_N ("too many formals for attribute", N);
4121 -- Error if the attribute reference has expressions that look like
4122 -- formal parameters.
4124 elsif Present (Expressions (Nam)) then
4125 Error_Msg_N ("illegal expressions in attribute reference", Nam);
4128 Nam_In (Aname, Name_Compose, Name_Exponent, Name_Leading_Part,
4129 Name_Pos, Name_Round, Name_Scaling,
4132 if Nkind (N) = N_Subprogram_Renaming_Declaration
4133 and then Present (Corresponding_Formal_Spec (N))
4136 ("generic actual cannot be attribute involving universal type",
4140 ("attribute involving a universal type cannot be renamed",
4145 -- Rewrite attribute node to have a list of expressions corresponding to
4146 -- the subprogram formals. A renaming declaration is not a freeze point,
4147 -- and the analysis of the attribute reference should not freeze the
4148 -- type of the prefix. We use the original node in the renaming so that
4149 -- its source location is preserved, and checks on stream attributes are
4150 -- properly applied.
4152 Attr_Node := Relocate_Node (Nam);
4153 Set_Expressions (Attr_Node, Expr_List);
4155 Set_Must_Not_Freeze (Attr_Node);
4156 Set_Must_Not_Freeze (Prefix (Nam));
4158 -- Case of renaming a function
4160 if Nkind (Spec) = N_Function_Specification then
4161 if Is_Procedure_Attribute_Name (Aname) then
4162 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
4166 Find_Type (Result_Definition (Spec));
4167 Rewrite (Result_Definition (Spec),
4169 (Base_Type (Entity (Result_Definition (Spec))), Loc));
4172 Make_Subprogram_Body (Loc,
4173 Specification => Spec,
4174 Declarations => New_List,
4175 Handled_Statement_Sequence =>
4176 Make_Handled_Sequence_Of_Statements (Loc,
4177 Statements => New_List (
4178 Make_Simple_Return_Statement (Loc,
4179 Expression => Attr_Node))));
4181 -- Case of renaming a procedure
4184 if not Is_Procedure_Attribute_Name (Aname) then
4185 Error_Msg_N ("attribute can only be renamed as function", Nam);
4190 Make_Subprogram_Body (Loc,
4191 Specification => Spec,
4192 Declarations => New_List,
4193 Handled_Statement_Sequence =>
4194 Make_Handled_Sequence_Of_Statements (Loc,
4195 Statements => New_List (Attr_Node)));
4198 -- Signal the ABE mechanism that the generated subprogram body has not
4199 -- ABE ramifications.
4201 Set_Was_Attribute_Reference (Body_Node);
4203 -- In case of tagged types we add the body of the generated function to
4204 -- the freezing actions of the type (because in the general case such
4205 -- type is still not frozen). We exclude from this processing generic
4206 -- formal subprograms found in instantiations.
4208 -- We must exclude restricted run-time libraries because
4209 -- entity AST_Handler is defined in package System.Aux_Dec which is not
4210 -- available in those platforms. Note that we cannot use the function
4211 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
4212 -- the ZFP run-time library is not defined as a profile, and we do not
4213 -- want to deal with AST_Handler in ZFP mode.
4215 if not Configurable_Run_Time_Mode
4216 and then not Present (Corresponding_Formal_Spec (N))
4217 and then Etype (Nam) /= RTE (RE_AST_Handler)
4220 P : constant Node_Id := Prefix (Nam);
4223 -- The prefix of 'Img is an object that is evaluated for each call
4224 -- of the function that renames it.
4226 if Aname = Name_Img then
4227 Preanalyze_And_Resolve (P);
4229 -- For all other attribute renamings, the prefix is a subtype
4235 -- If the target type is not yet frozen, add the body to the
4236 -- actions to be elaborated at freeze time.
4238 if Is_Tagged_Type (Etype (P))
4239 and then In_Open_Scopes (Scope (Etype (P)))
4241 Ensure_Freeze_Node (Etype (P));
4242 Append_Freeze_Action (Etype (P), Body_Node);
4244 Rewrite (N, Body_Node);
4246 Set_Etype (New_S, Base_Type (Etype (New_S)));
4250 -- Generic formal subprograms or AST_Handler renaming
4253 Rewrite (N, Body_Node);
4255 Set_Etype (New_S, Base_Type (Etype (New_S)));
4258 if Is_Compilation_Unit (New_S) then
4260 ("a library unit can only rename another library unit", N);
4263 -- We suppress elaboration warnings for the resulting entity, since
4264 -- clearly they are not needed, and more particularly, in the case
4265 -- of a generic formal subprogram, the resulting entity can appear
4266 -- after the instantiation itself, and thus look like a bogus case
4267 -- of access before elaboration.
4269 if Legacy_Elaboration_Checks then
4270 Set_Suppress_Elaboration_Warnings (New_S);
4272 end Attribute_Renaming;
4274 ----------------------
4275 -- Chain_Use_Clause --
4276 ----------------------
4278 procedure Chain_Use_Clause (N : Node_Id) is
4279 Level : Int := Scope_Stack.Last;
4285 if not Is_Compilation_Unit (Current_Scope)
4286 or else not Is_Child_Unit (Current_Scope)
4290 -- Common case for compilation unit
4292 elsif Defining_Entity (Parent (N)) = Current_Scope then
4296 -- If declaration appears in some other scope, it must be in some
4297 -- parent unit when compiling a child.
4299 Pack := Defining_Entity (Parent (N));
4301 if not In_Open_Scopes (Pack) then
4304 -- If the use clause appears in an ancestor and we are in the
4305 -- private part of the immediate parent, the use clauses are
4306 -- already installed.
4308 elsif Pack /= Scope (Current_Scope)
4309 and then In_Private_Part (Scope (Current_Scope))
4314 -- Find entry for parent unit in scope stack
4316 while Scope_Stack.Table (Level).Entity /= Pack loop
4322 Set_Next_Use_Clause (N,
4323 Scope_Stack.Table (Level).First_Use_Clause);
4324 Scope_Stack.Table (Level).First_Use_Clause := N;
4325 end Chain_Use_Clause;
4327 ---------------------------
4328 -- Check_Frozen_Renaming --
4329 ---------------------------
4331 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
4336 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
4339 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
4341 if Is_Entity_Name (Name (N)) then
4342 Old_S := Entity (Name (N));
4344 if not Is_Frozen (Old_S)
4345 and then Operating_Mode /= Check_Semantics
4347 Append_Freeze_Action (Old_S, B_Node);
4349 Insert_After (N, B_Node);
4353 if Is_Intrinsic_Subprogram (Old_S)
4354 and then not In_Instance
4355 and then not Relaxed_RM_Semantics
4358 ("subprogram used in renaming_as_body cannot be intrinsic",
4363 Insert_After (N, B_Node);
4367 end Check_Frozen_Renaming;
4369 -------------------------------
4370 -- Set_Entity_Or_Discriminal --
4371 -------------------------------
4373 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4377 -- If the entity is not a discriminant, or else expansion is disabled,
4378 -- simply set the entity.
4380 if not In_Spec_Expression
4381 or else Ekind (E) /= E_Discriminant
4382 or else Inside_A_Generic
4384 Set_Entity_With_Checks (N, E);
4386 -- The replacement of a discriminant by the corresponding discriminal
4387 -- is not done for a task discriminant that appears in a default
4388 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4389 -- for details on their handling.
4391 elsif Is_Concurrent_Type (Scope (E)) then
4394 and then not Nkind_In (P, N_Parameter_Specification,
4395 N_Component_Declaration)
4401 and then Nkind (P) = N_Parameter_Specification
4406 Set_Entity (N, Discriminal (E));
4409 -- Otherwise, this is a discriminant in a context in which
4410 -- it is a reference to the corresponding parameter of the
4411 -- init proc for the enclosing type.
4414 Set_Entity (N, Discriminal (E));
4416 end Set_Entity_Or_Discriminal;
4418 -----------------------------------
4419 -- Check_In_Previous_With_Clause --
4420 -----------------------------------
4422 procedure Check_In_Previous_With_Clause
4426 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4431 Item := First (Context_Items (Parent (N)));
4432 while Present (Item) and then Item /= N loop
4433 if Nkind (Item) = N_With_Clause
4435 -- Protect the frontend against previous critical errors
4437 and then Nkind (Name (Item)) /= N_Selected_Component
4438 and then Entity (Name (Item)) = Pack
4442 -- Find root library unit in with_clause
4444 while Nkind (Par) = N_Expanded_Name loop
4445 Par := Prefix (Par);
4448 if Is_Child_Unit (Entity (Original_Node (Par))) then
4449 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4458 -- On exit, package is not mentioned in a previous with_clause.
4459 -- Check if its prefix is.
4461 if Nkind (Nam) = N_Expanded_Name then
4462 Check_In_Previous_With_Clause (N, Prefix (Nam));
4464 elsif Pack /= Any_Id then
4465 Error_Msg_NE ("& is not visible", Nam, Pack);
4467 end Check_In_Previous_With_Clause;
4469 ---------------------------------
4470 -- Check_Library_Unit_Renaming --
4471 ---------------------------------
4473 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4477 if Nkind (Parent (N)) /= N_Compilation_Unit then
4480 -- Check for library unit. Note that we used to check for the scope
4481 -- being Standard here, but that was wrong for Standard itself.
4483 elsif not Is_Compilation_Unit (Old_E)
4484 and then not Is_Child_Unit (Old_E)
4486 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4488 -- Entities defined in Standard (operators and boolean literals) cannot
4489 -- be renamed as library units.
4491 elsif Scope (Old_E) = Standard_Standard
4492 and then Sloc (Old_E) = Standard_Location
4494 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4496 elsif Present (Parent_Spec (N))
4497 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
4498 and then not Is_Child_Unit (Old_E)
4501 ("renamed unit must be a child unit of generic parent", Name (N));
4503 elsif Nkind (N) in N_Generic_Renaming_Declaration
4504 and then Nkind (Name (N)) = N_Expanded_Name
4505 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
4506 and then Is_Generic_Unit (Old_E)
4509 ("renamed generic unit must be a library unit", Name (N));
4511 elsif Is_Package_Or_Generic_Package (Old_E) then
4513 -- Inherit categorization flags
4515 New_E := Defining_Entity (N);
4516 Set_Is_Pure (New_E, Is_Pure (Old_E));
4517 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
4518 Set_Is_Remote_Call_Interface (New_E,
4519 Is_Remote_Call_Interface (Old_E));
4520 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
4521 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
4523 end Check_Library_Unit_Renaming;
4525 ------------------------
4526 -- Enclosing_Instance --
4527 ------------------------
4529 function Enclosing_Instance return Entity_Id is
4533 if not Is_Generic_Instance (Current_Scope) then
4537 S := Scope (Current_Scope);
4538 while S /= Standard_Standard loop
4539 if Is_Generic_Instance (S) then
4547 end Enclosing_Instance;
4553 procedure End_Scope is
4559 Id := First_Entity (Current_Scope);
4560 while Present (Id) loop
4561 -- An entity in the current scope is not necessarily the first one
4562 -- on its homonym chain. Find its predecessor if any,
4563 -- If it is an internal entity, it will not be in the visibility
4564 -- chain altogether, and there is nothing to unchain.
4566 if Id /= Current_Entity (Id) then
4567 Prev := Current_Entity (Id);
4568 while Present (Prev)
4569 and then Present (Homonym (Prev))
4570 and then Homonym (Prev) /= Id
4572 Prev := Homonym (Prev);
4575 -- Skip to end of loop if Id is not in the visibility chain
4577 if No (Prev) or else Homonym (Prev) /= Id then
4585 Set_Is_Immediately_Visible (Id, False);
4587 Outer := Homonym (Id);
4588 while Present (Outer) and then Scope (Outer) = Current_Scope loop
4589 Outer := Homonym (Outer);
4592 -- Reset homonym link of other entities, but do not modify link
4593 -- between entities in current scope, so that the back-end can have
4594 -- a proper count of local overloadings.
4597 Set_Name_Entity_Id (Chars (Id), Outer);
4599 elsif Scope (Prev) /= Scope (Id) then
4600 Set_Homonym (Prev, Outer);
4607 -- If the scope generated freeze actions, place them before the
4608 -- current declaration and analyze them. Type declarations and
4609 -- the bodies of initialization procedures can generate such nodes.
4610 -- We follow the parent chain until we reach a list node, which is
4611 -- the enclosing list of declarations. If the list appears within
4612 -- a protected definition, move freeze nodes outside the protected
4616 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
4620 L : constant List_Id := Scope_Stack.Table
4621 (Scope_Stack.Last).Pending_Freeze_Actions;
4624 if Is_Itype (Current_Scope) then
4625 Decl := Associated_Node_For_Itype (Current_Scope);
4627 Decl := Parent (Current_Scope);
4632 while not (Is_List_Member (Decl))
4633 or else Nkind_In (Parent (Decl), N_Protected_Definition,
4636 Decl := Parent (Decl);
4639 Insert_List_Before_And_Analyze (Decl, L);
4647 ---------------------
4648 -- End_Use_Clauses --
4649 ---------------------
4651 procedure End_Use_Clauses (Clause : Node_Id) is
4655 -- Remove use_type_clauses first, because they affect the visibility of
4656 -- operators in subsequent used packages.
4659 while Present (U) loop
4660 if Nkind (U) = N_Use_Type_Clause then
4664 Next_Use_Clause (U);
4668 while Present (U) loop
4669 if Nkind (U) = N_Use_Package_Clause then
4670 End_Use_Package (U);
4673 Next_Use_Clause (U);
4675 end End_Use_Clauses;
4677 ---------------------
4678 -- End_Use_Package --
4679 ---------------------
4681 procedure End_Use_Package (N : Node_Id) is
4683 Pack_Name : Node_Id;
4687 function Is_Primitive_Operator_In_Use
4689 F : Entity_Id) return Boolean;
4690 -- Check whether Op is a primitive operator of a use-visible type
4692 ----------------------------------
4693 -- Is_Primitive_Operator_In_Use --
4694 ----------------------------------
4696 function Is_Primitive_Operator_In_Use
4698 F : Entity_Id) return Boolean
4700 T : constant Entity_Id := Base_Type (Etype (F));
4702 return In_Use (T) and then Scope (T) = Scope (Op);
4703 end Is_Primitive_Operator_In_Use;
4705 -- Start of processing for End_Use_Package
4708 Pack_Name := Name (N);
4710 -- Test that Pack_Name actually denotes a package before processing
4712 if Is_Entity_Name (Pack_Name)
4713 and then Ekind (Entity (Pack_Name)) = E_Package
4715 Pack := Entity (Pack_Name);
4717 if In_Open_Scopes (Pack) then
4720 elsif not Redundant_Use (Pack_Name) then
4721 Set_In_Use (Pack, False);
4722 Set_Current_Use_Clause (Pack, Empty);
4724 Id := First_Entity (Pack);
4725 while Present (Id) loop
4727 -- Preserve use-visibility of operators that are primitive
4728 -- operators of a type that is use-visible through an active
4731 if Nkind (Id) = N_Defining_Operator_Symbol
4733 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
4735 (Present (Next_Formal (First_Formal (Id)))
4737 Is_Primitive_Operator_In_Use
4738 (Id, Next_Formal (First_Formal (Id)))))
4742 Set_Is_Potentially_Use_Visible (Id, False);
4745 if Is_Private_Type (Id)
4746 and then Present (Full_View (Id))
4748 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4754 if Present (Renamed_Object (Pack)) then
4755 Set_In_Use (Renamed_Object (Pack), False);
4756 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
4759 if Chars (Pack) = Name_System
4760 and then Scope (Pack) = Standard_Standard
4761 and then Present_System_Aux
4763 Id := First_Entity (System_Aux_Id);
4764 while Present (Id) loop
4765 Set_Is_Potentially_Use_Visible (Id, False);
4767 if Is_Private_Type (Id)
4768 and then Present (Full_View (Id))
4770 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4776 Set_In_Use (System_Aux_Id, False);
4779 Set_Redundant_Use (Pack_Name, False);
4783 if Present (Hidden_By_Use_Clause (N)) then
4784 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
4785 while Present (Elmt) loop
4787 E : constant Entity_Id := Node (Elmt);
4790 -- Reset either Use_Visibility or Direct_Visibility, depending
4791 -- on how the entity was hidden by the use clause.
4793 if In_Use (Scope (E))
4794 and then Used_As_Generic_Actual (Scope (E))
4796 Set_Is_Potentially_Use_Visible (Node (Elmt));
4798 Set_Is_Immediately_Visible (Node (Elmt));
4805 Set_Hidden_By_Use_Clause (N, No_Elist);
4807 end End_Use_Package;
4813 procedure End_Use_Type (N : Node_Id) is
4818 -- Start of processing for End_Use_Type
4821 Id := Subtype_Mark (N);
4823 -- A call to Rtsfind may occur while analyzing a use_type_clause, in
4824 -- which case the type marks are not resolved yet, so guard against that
4827 if Is_Entity_Name (Id) and then Present (Entity (Id)) then
4830 if T = Any_Type or else From_Limited_With (T) then
4833 -- Note that the use_type_clause may mention a subtype of the type
4834 -- whose primitive operations have been made visible. Here as
4835 -- elsewhere, it is the base type that matters for visibility.
4837 elsif In_Open_Scopes (Scope (Base_Type (T))) then
4840 elsif not Redundant_Use (Id) then
4841 Set_In_Use (T, False);
4842 Set_In_Use (Base_Type (T), False);
4843 Set_Current_Use_Clause (T, Empty);
4844 Set_Current_Use_Clause (Base_Type (T), Empty);
4846 -- See Use_One_Type for the rationale. This is a bit on the naive
4847 -- side, but should be good enough in practice.
4849 if Is_Tagged_Type (T) then
4850 Set_In_Use (Class_Wide_Type (T), False);
4855 if Is_Empty_Elmt_List (Used_Operations (N)) then
4859 Elmt := First_Elmt (Used_Operations (N));
4860 while Present (Elmt) loop
4861 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
4867 --------------------
4868 -- Entity_Of_Unit --
4869 --------------------
4871 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
4873 if Nkind (U) = N_Package_Instantiation and then Analyzed (U) then
4874 return Defining_Entity (Instance_Spec (U));
4876 return Defining_Entity (U);
4880 ----------------------
4881 -- Find_Direct_Name --
4882 ----------------------
4884 procedure Find_Direct_Name
4886 Errors_OK : Boolean := True;
4887 Marker_OK : Boolean := True;
4888 Reference_OK : Boolean := True)
4894 Homonyms : Entity_Id;
4895 -- Saves start of homonym chain
4897 Inst : Entity_Id := Empty;
4898 -- Enclosing instance, if any
4900 Nvis_Entity : Boolean;
4901 -- Set True to indicate that there is at least one entity on the homonym
4902 -- chain which, while not visible, is visible enough from the user point
4903 -- of view to warrant an error message of "not visible" rather than
4906 Nvis_Is_Private_Subprg : Boolean := False;
4907 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
4908 -- effect concerning library subprograms has been detected. Used to
4909 -- generate the precise error message.
4911 function From_Actual_Package (E : Entity_Id) return Boolean;
4912 -- Returns true if the entity is an actual for a package that is itself
4913 -- an actual for a formal package of the current instance. Such an
4914 -- entity requires special handling because it may be use-visible but
4915 -- hides directly visible entities defined outside the instance, because
4916 -- the corresponding formal did so in the generic.
4918 function Is_Actual_Parameter return Boolean;
4919 -- This function checks if the node N is an identifier that is an actual
4920 -- parameter of a procedure call. If so it returns True, otherwise it
4921 -- return False. The reason for this check is that at this stage we do
4922 -- not know what procedure is being called if the procedure might be
4923 -- overloaded, so it is premature to go setting referenced flags or
4924 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
4925 -- for that processing
4927 function Known_But_Invisible (E : Entity_Id) return Boolean;
4928 -- This function determines whether a reference to the entity E, which
4929 -- is not visible, can reasonably be considered to be known to the
4930 -- writer of the reference. This is a heuristic test, used only for
4931 -- the purposes of figuring out whether we prefer to complain that an
4932 -- entity is undefined or invisible (and identify the declaration of
4933 -- the invisible entity in the latter case). The point here is that we
4934 -- don't want to complain that something is invisible and then point to
4935 -- something entirely mysterious to the writer.
4937 procedure Nvis_Messages;
4938 -- Called if there are no visible entries for N, but there is at least
4939 -- one non-directly visible, or hidden declaration. This procedure
4940 -- outputs an appropriate set of error messages.
4942 procedure Undefined (Nvis : Boolean);
4943 -- This function is called if the current node has no corresponding
4944 -- visible entity or entities. The value set in Msg indicates whether
4945 -- an error message was generated (multiple error messages for the
4946 -- same variable are generally suppressed, see body for details).
4947 -- Msg is True if an error message was generated, False if not. This
4948 -- value is used by the caller to determine whether or not to output
4949 -- additional messages where appropriate. The parameter is set False
4950 -- to get the message "X is undefined", and True to get the message
4951 -- "X is not visible".
4953 -------------------------
4954 -- From_Actual_Package --
4955 -------------------------
4957 function From_Actual_Package (E : Entity_Id) return Boolean is
4958 Scop : constant Entity_Id := Scope (E);
4959 -- Declared scope of candidate entity
4961 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
4962 -- Recursive function that does the work and examines actuals of
4963 -- actual packages of current instance.
4965 ------------------------
4966 -- Declared_In_Actual --
4967 ------------------------
4969 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
4973 if No (Associated_Formal_Package (Pack)) then
4977 Act := First_Entity (Pack);
4978 while Present (Act) loop
4979 if Renamed_Object (Pack) = Scop then
4982 -- Check for end of list of actuals
4984 elsif Ekind (Act) = E_Package
4985 and then Renamed_Object (Act) = Pack
4989 elsif Ekind (Act) = E_Package
4990 and then Declared_In_Actual (Act)
5000 end Declared_In_Actual;
5006 -- Start of processing for From_Actual_Package
5009 if not In_Instance then
5013 Inst := Current_Scope;
5014 while Present (Inst)
5015 and then Ekind (Inst) /= E_Package
5016 and then not Is_Generic_Instance (Inst)
5018 Inst := Scope (Inst);
5025 Act := First_Entity (Inst);
5026 while Present (Act) loop
5027 if Ekind (Act) = E_Package
5028 and then Declared_In_Actual (Act)
5038 end From_Actual_Package;
5040 -------------------------
5041 -- Is_Actual_Parameter --
5042 -------------------------
5044 function Is_Actual_Parameter return Boolean is
5047 Nkind (N) = N_Identifier
5049 (Nkind (Parent (N)) = N_Procedure_Call_Statement
5051 (Nkind (Parent (N)) = N_Parameter_Association
5052 and then N = Explicit_Actual_Parameter (Parent (N))
5053 and then Nkind (Parent (Parent (N))) =
5054 N_Procedure_Call_Statement));
5055 end Is_Actual_Parameter;
5057 -------------------------
5058 -- Known_But_Invisible --
5059 -------------------------
5061 function Known_But_Invisible (E : Entity_Id) return Boolean is
5062 Fname : File_Name_Type;
5065 -- Entities in Standard are always considered to be known
5067 if Sloc (E) <= Standard_Location then
5070 -- An entity that does not come from source is always considered
5071 -- to be unknown, since it is an artifact of code expansion.
5073 elsif not Comes_From_Source (E) then
5076 -- In gnat internal mode, we consider all entities known. The
5077 -- historical reason behind this discrepancy is not known??? But the
5078 -- only effect is to modify the error message given, so it is not
5079 -- critical. Since it only affects the exact wording of error
5080 -- messages in illegal programs, we do not mention this as an
5081 -- effect of -gnatg, since it is not a language modification.
5083 elsif GNAT_Mode then
5087 -- Here we have an entity that is not from package Standard, and
5088 -- which comes from Source. See if it comes from an internal file.
5090 Fname := Unit_File_Name (Get_Source_Unit (E));
5092 -- Case of from internal file
5094 if In_Internal_Unit (E) then
5096 -- Private part entities in internal files are never considered
5097 -- to be known to the writer of normal application code.
5099 if Is_Hidden (E) then
5103 -- Entities from System packages other than System and
5104 -- System.Storage_Elements are not considered to be known.
5105 -- System.Auxxxx files are also considered known to the user.
5107 -- Should refine this at some point to generally distinguish
5108 -- between known and unknown internal files ???
5110 Get_Name_String (Fname);
5115 Name_Buffer (1 .. 2) /= "s-"
5117 Name_Buffer (3 .. 8) = "stoele"
5119 Name_Buffer (3 .. 5) = "aux";
5121 -- If not an internal file, then entity is definitely known, even if
5122 -- it is in a private part (the message generated will note that it
5123 -- is in a private part).
5128 end Known_But_Invisible;
5134 procedure Nvis_Messages is
5135 Comp_Unit : Node_Id;
5137 Found : Boolean := False;
5138 Hidden : Boolean := False;
5142 if not Errors_OK then
5146 -- Ada 2005 (AI-262): Generate a precise error concerning the
5147 -- Beaujolais effect that was previously detected
5149 if Nvis_Is_Private_Subprg then
5151 pragma Assert (Nkind (E2) = N_Defining_Identifier
5152 and then Ekind (E2) = E_Function
5153 and then Scope (E2) = Standard_Standard
5154 and then Has_Private_With (E2));
5156 -- Find the sloc corresponding to the private with'ed unit
5158 Comp_Unit := Cunit (Current_Sem_Unit);
5159 Error_Msg_Sloc := No_Location;
5161 Item := First (Context_Items (Comp_Unit));
5162 while Present (Item) loop
5163 if Nkind (Item) = N_With_Clause
5164 and then Private_Present (Item)
5165 and then Entity (Name (Item)) = E2
5167 Error_Msg_Sloc := Sloc (Item);
5174 pragma Assert (Error_Msg_Sloc /= No_Location);
5176 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
5180 Undefined (Nvis => True);
5184 -- First loop does hidden declarations
5187 while Present (Ent) loop
5188 if Is_Potentially_Use_Visible (Ent) then
5190 Error_Msg_N -- CODEFIX
5191 ("multiple use clauses cause hiding!", N);
5195 Error_Msg_Sloc := Sloc (Ent);
5196 Error_Msg_N -- CODEFIX
5197 ("hidden declaration#!", N);
5200 Ent := Homonym (Ent);
5203 -- If we found hidden declarations, then that's enough, don't
5204 -- bother looking for non-visible declarations as well.
5210 -- Second loop does non-directly visible declarations
5213 while Present (Ent) loop
5214 if not Is_Potentially_Use_Visible (Ent) then
5216 -- Do not bother the user with unknown entities
5218 if not Known_But_Invisible (Ent) then
5222 Error_Msg_Sloc := Sloc (Ent);
5224 -- Output message noting that there is a non-visible
5225 -- declaration, distinguishing the private part case.
5227 if Is_Hidden (Ent) then
5228 Error_Msg_N ("non-visible (private) declaration#!", N);
5230 -- If the entity is declared in a generic package, it
5231 -- cannot be visible, so there is no point in adding it
5232 -- to the list of candidates if another homograph from a
5233 -- non-generic package has been seen.
5235 elsif Ekind (Scope (Ent)) = E_Generic_Package
5241 Error_Msg_N -- CODEFIX
5242 ("non-visible declaration#!", N);
5244 if Ekind (Scope (Ent)) /= E_Generic_Package then
5248 if Is_Compilation_Unit (Ent)
5250 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5252 Error_Msg_Qual_Level := 99;
5253 Error_Msg_NE -- CODEFIX
5254 ("\\missing `WITH &;`", N, Ent);
5255 Error_Msg_Qual_Level := 0;
5258 if Ekind (Ent) = E_Discriminant
5259 and then Present (Corresponding_Discriminant (Ent))
5260 and then Scope (Corresponding_Discriminant (Ent)) =
5264 ("inherited discriminant not allowed here" &
5265 " (RM 3.8 (12), 3.8.1 (6))!", N);
5269 -- Set entity and its containing package as referenced. We
5270 -- can't be sure of this, but this seems a better choice
5271 -- to avoid unused entity messages.
5273 if Comes_From_Source (Ent) then
5274 Set_Referenced (Ent);
5275 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5280 Ent := Homonym (Ent);
5289 procedure Undefined (Nvis : Boolean) is
5290 Emsg : Error_Msg_Id;
5293 -- We should never find an undefined internal name. If we do, then
5294 -- see if we have previous errors. If so, ignore on the grounds that
5295 -- it is probably a cascaded message (e.g. a block label from a badly
5296 -- formed block). If no previous errors, then we have a real internal
5297 -- error of some kind so raise an exception.
5299 if Is_Internal_Name (Chars (N)) then
5300 if Total_Errors_Detected /= 0 then
5303 raise Program_Error;
5307 -- A very specialized error check, if the undefined variable is
5308 -- a case tag, and the case type is an enumeration type, check
5309 -- for a possible misspelling, and if so, modify the identifier
5311 -- Named aggregate should also be handled similarly ???
5314 and then Nkind (N) = N_Identifier
5315 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5318 Case_Stm : constant Node_Id := Parent (Parent (N));
5319 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5324 if Is_Enumeration_Type (Case_Typ)
5325 and then not Is_Standard_Character_Type (Case_Typ)
5327 Lit := First_Literal (Case_Typ);
5328 Get_Name_String (Chars (Lit));
5330 if Chars (Lit) /= Chars (N)
5331 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5333 Error_Msg_Node_2 := Lit;
5334 Error_Msg_N -- CODEFIX
5335 ("& is undefined, assume misspelling of &", N);
5336 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5340 Lit := Next_Literal (Lit);
5345 -- Normal processing
5347 Set_Entity (N, Any_Id);
5348 Set_Etype (N, Any_Type);
5352 -- We use the table Urefs to keep track of entities for which we
5353 -- have issued errors for undefined references. Multiple errors
5354 -- for a single name are normally suppressed, however we modify
5355 -- the error message to alert the programmer to this effect.
5357 for J in Urefs.First .. Urefs.Last loop
5358 if Chars (N) = Chars (Urefs.Table (J).Node) then
5359 if Urefs.Table (J).Err /= No_Error_Msg
5360 and then Sloc (N) /= Urefs.Table (J).Loc
5362 Error_Msg_Node_1 := Urefs.Table (J).Node;
5364 if Urefs.Table (J).Nvis then
5365 Change_Error_Text (Urefs.Table (J).Err,
5366 "& is not visible (more references follow)");
5368 Change_Error_Text (Urefs.Table (J).Err,
5369 "& is undefined (more references follow)");
5372 Urefs.Table (J).Err := No_Error_Msg;
5375 -- Although we will set Msg False, and thus suppress the
5376 -- message, we also set Error_Posted True, to avoid any
5377 -- cascaded messages resulting from the undefined reference.
5380 Set_Error_Posted (N);
5385 -- If entry not found, this is first undefined occurrence
5388 Error_Msg_N ("& is not visible!", N);
5392 Error_Msg_N ("& is undefined!", N);
5395 -- A very bizarre special check, if the undefined identifier
5396 -- is Put or Put_Line, then add a special error message (since
5397 -- this is a very common error for beginners to make).
5399 if Nam_In (Chars (N), Name_Put, Name_Put_Line) then
5400 Error_Msg_N -- CODEFIX
5401 ("\\possible missing `WITH Ada.Text_'I'O; " &
5402 "USE Ada.Text_'I'O`!", N);
5404 -- Another special check if N is the prefix of a selected
5405 -- component which is a known unit: add message complaining
5406 -- about missing with for this unit.
5408 elsif Nkind (Parent (N)) = N_Selected_Component
5409 and then N = Prefix (Parent (N))
5410 and then Is_Known_Unit (Parent (N))
5412 Error_Msg_Node_2 := Selector_Name (Parent (N));
5413 Error_Msg_N -- CODEFIX
5414 ("\\missing `WITH &.&;`", Prefix (Parent (N)));
5417 -- Now check for possible misspellings
5421 Ematch : Entity_Id := Empty;
5423 Last_Name_Id : constant Name_Id :=
5424 Name_Id (Nat (First_Name_Id) +
5425 Name_Entries_Count - 1);
5428 for Nam in First_Name_Id .. Last_Name_Id loop
5429 E := Get_Name_Entity_Id (Nam);
5432 and then (Is_Immediately_Visible (E)
5434 Is_Potentially_Use_Visible (E))
5436 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5443 if Present (Ematch) then
5444 Error_Msg_NE -- CODEFIX
5445 ("\possible misspelling of&", N, Ematch);
5450 -- Make entry in undefined references table unless the full errors
5451 -- switch is set, in which case by refraining from generating the
5452 -- table entry we guarantee that we get an error message for every
5453 -- undefined reference. The entry is not added if we are ignoring
5456 if not All_Errors_Mode and then Ignore_Errors_Enable = 0 then
5470 Nested_Inst : Entity_Id := Empty;
5471 -- The entity of a nested instance which appears within Inst (if any)
5473 -- Start of processing for Find_Direct_Name
5476 -- If the entity pointer is already set, this is an internal node, or
5477 -- a node that is analyzed more than once, after a tree modification.
5478 -- In such a case there is no resolution to perform, just set the type.
5480 if Present (Entity (N)) then
5481 if Is_Type (Entity (N)) then
5482 Set_Etype (N, Entity (N));
5486 Entyp : constant Entity_Id := Etype (Entity (N));
5489 -- One special case here. If the Etype field is already set,
5490 -- and references the packed array type corresponding to the
5491 -- etype of the referenced entity, then leave it alone. This
5492 -- happens for trees generated from Exp_Pakd, where expressions
5493 -- can be deliberately "mis-typed" to the packed array type.
5495 if Is_Array_Type (Entyp)
5496 and then Is_Packed (Entyp)
5497 and then Present (Etype (N))
5498 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5502 -- If not that special case, then just reset the Etype
5505 Set_Etype (N, Etype (Entity (N)));
5510 -- Although the marking of use clauses happens at the end of
5511 -- Find_Direct_Name, a certain case where a generic actual satisfies
5512 -- a use clause must be checked here due to how the generic machinery
5513 -- handles the analysis of said actuals.
5516 and then Nkind (Parent (N)) = N_Generic_Association
5518 Mark_Use_Clauses (Entity (N));
5524 -- Preserve relevant elaboration-related attributes of the context which
5525 -- are no longer available or very expensive to recompute once analysis,
5526 -- resolution, and expansion are over.
5528 if Nkind (N) = N_Identifier then
5529 Mark_Elaboration_Attributes
5536 -- Here if Entity pointer was not set, we need full visibility analysis
5537 -- First we generate debugging output if the debug E flag is set.
5539 if Debug_Flag_E then
5540 Write_Str ("Looking for ");
5541 Write_Name (Chars (N));
5545 Homonyms := Current_Entity (N);
5546 Nvis_Entity := False;
5549 while Present (E) loop
5551 -- If entity is immediately visible or potentially use visible, then
5552 -- process the entity and we are done.
5554 if Is_Immediately_Visible (E) then
5555 goto Immediately_Visible_Entity;
5557 elsif Is_Potentially_Use_Visible (E) then
5558 goto Potentially_Use_Visible_Entity;
5560 -- Note if a known but invisible entity encountered
5562 elsif Known_But_Invisible (E) then
5563 Nvis_Entity := True;
5566 -- Move to next entity in chain and continue search
5571 -- If no entries on homonym chain that were potentially visible,
5572 -- and no entities reasonably considered as non-visible, then
5573 -- we have a plain undefined reference, with no additional
5574 -- explanation required.
5576 if not Nvis_Entity then
5577 Undefined (Nvis => False);
5579 -- Otherwise there is at least one entry on the homonym chain that
5580 -- is reasonably considered as being known and non-visible.
5588 -- Processing for a potentially use visible entry found. We must search
5589 -- the rest of the homonym chain for two reasons. First, if there is a
5590 -- directly visible entry, then none of the potentially use-visible
5591 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5592 -- for the case of multiple potentially use-visible entries hiding one
5593 -- another and as a result being non-directly visible (RM 8.4(11)).
5595 <<Potentially_Use_Visible_Entity>> declare
5596 Only_One_Visible : Boolean := True;
5597 All_Overloadable : Boolean := Is_Overloadable (E);
5601 while Present (E2) loop
5602 if Is_Immediately_Visible (E2) then
5604 -- If the use-visible entity comes from the actual for a
5605 -- formal package, it hides a directly visible entity from
5606 -- outside the instance.
5608 if From_Actual_Package (E)
5609 and then Scope_Depth (E2) < Scope_Depth (Inst)
5614 goto Immediately_Visible_Entity;
5617 elsif Is_Potentially_Use_Visible (E2) then
5618 Only_One_Visible := False;
5619 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5621 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5622 -- that can occur in private_with clauses. Example:
5625 -- private with B; package A is
5626 -- package C is function B return Integer;
5628 -- V1 : Integer := B;
5629 -- private function B return Integer;
5630 -- V2 : Integer := B;
5633 -- V1 resolves to A.B, but V2 resolves to library unit B
5635 elsif Ekind (E2) = E_Function
5636 and then Scope (E2) = Standard_Standard
5637 and then Has_Private_With (E2)
5639 Only_One_Visible := False;
5640 All_Overloadable := False;
5641 Nvis_Is_Private_Subprg := True;
5648 -- On falling through this loop, we have checked that there are no
5649 -- immediately visible entities. Only_One_Visible is set if exactly
5650 -- one potentially use visible entity exists. All_Overloadable is
5651 -- set if all the potentially use visible entities are overloadable.
5652 -- The condition for legality is that either there is one potentially
5653 -- use visible entity, or if there is more than one, then all of them
5654 -- are overloadable.
5656 if Only_One_Visible or All_Overloadable then
5659 -- If there is more than one potentially use-visible entity and at
5660 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5661 -- Note that E points to the first such entity on the homonym list.
5664 -- If one of the entities is declared in an actual package, it
5665 -- was visible in the generic, and takes precedence over other
5666 -- entities that are potentially use-visible. The same applies
5667 -- if the entity is declared in a local instantiation of the
5668 -- current instance.
5672 -- Find the current instance
5674 Inst := Current_Scope;
5675 while Present (Inst) and then Inst /= Standard_Standard loop
5676 if Is_Generic_Instance (Inst) then
5680 Inst := Scope (Inst);
5683 -- Reexamine the candidate entities, giving priority to those
5684 -- that were visible within the generic.
5687 while Present (E2) loop
5688 Nested_Inst := Nearest_Enclosing_Instance (E2);
5690 -- The entity is declared within an actual package, or in a
5691 -- nested instance. The ">=" accounts for the case where the
5692 -- current instance and the nested instance are the same.
5694 if From_Actual_Package (E2)
5695 or else (Present (Nested_Inst)
5696 and then Scope_Depth (Nested_Inst) >=
5709 elsif Is_Predefined_Unit (Current_Sem_Unit) then
5710 -- A use clause in the body of a system file creates conflict
5711 -- with some entity in a user scope, while rtsfind is active.
5712 -- Keep only the entity coming from another predefined unit.
5715 while Present (E2) loop
5716 if In_Predefined_Unit (E2) then
5724 -- Entity must exist because predefined unit is correct
5726 raise Program_Error;
5735 -- Come here with E set to the first immediately visible entity on
5736 -- the homonym chain. This is the one we want unless there is another
5737 -- immediately visible entity further on in the chain for an inner
5738 -- scope (RM 8.3(8)).
5740 <<Immediately_Visible_Entity>> declare
5745 -- Find scope level of initial entity. When compiling through
5746 -- Rtsfind, the previous context is not completely invisible, and
5747 -- an outer entity may appear on the chain, whose scope is below
5748 -- the entry for Standard that delimits the current scope stack.
5749 -- Indicate that the level for this spurious entry is outside of
5750 -- the current scope stack.
5752 Level := Scope_Stack.Last;
5754 Scop := Scope_Stack.Table (Level).Entity;
5755 exit when Scop = Scope (E);
5757 exit when Scop = Standard_Standard;
5760 -- Now search remainder of homonym chain for more inner entry
5761 -- If the entity is Standard itself, it has no scope, and we
5762 -- compare it with the stack entry directly.
5765 while Present (E2) loop
5766 if Is_Immediately_Visible (E2) then
5768 -- If a generic package contains a local declaration that
5769 -- has the same name as the generic, there may be a visibility
5770 -- conflict in an instance, where the local declaration must
5771 -- also hide the name of the corresponding package renaming.
5772 -- We check explicitly for a package declared by a renaming,
5773 -- whose renamed entity is an instance that is on the scope
5774 -- stack, and that contains a homonym in the same scope. Once
5775 -- we have found it, we know that the package renaming is not
5776 -- immediately visible, and that the identifier denotes the
5777 -- other entity (and its homonyms if overloaded).
5779 if Scope (E) = Scope (E2)
5780 and then Ekind (E) = E_Package
5781 and then Present (Renamed_Object (E))
5782 and then Is_Generic_Instance (Renamed_Object (E))
5783 and then In_Open_Scopes (Renamed_Object (E))
5784 and then Comes_From_Source (N)
5786 Set_Is_Immediately_Visible (E, False);
5790 for J in Level + 1 .. Scope_Stack.Last loop
5791 if Scope_Stack.Table (J).Entity = Scope (E2)
5792 or else Scope_Stack.Table (J).Entity = E2
5805 -- At the end of that loop, E is the innermost immediately
5806 -- visible entity, so we are all set.
5809 -- Come here with entity found, and stored in E
5813 -- Check violation of No_Wide_Characters restriction
5815 Check_Wide_Character_Restriction (E, N);
5817 -- When distribution features are available (Get_PCS_Name /=
5818 -- Name_No_DSA), a remote access-to-subprogram type is converted
5819 -- into a record type holding whatever information is needed to
5820 -- perform a remote call on an RCI subprogram. In that case we
5821 -- rewrite any occurrence of the RAS type into the equivalent record
5822 -- type here. 'Access attribute references and RAS dereferences are
5823 -- then implemented using specific TSSs. However when distribution is
5824 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
5825 -- generation of these TSSs, and we must keep the RAS type in its
5826 -- original access-to-subprogram form (since all calls through a
5827 -- value of such type will be local anyway in the absence of a PCS).
5829 if Comes_From_Source (N)
5830 and then Is_Remote_Access_To_Subprogram_Type (E)
5831 and then Ekind (E) = E_Access_Subprogram_Type
5832 and then Expander_Active
5833 and then Get_PCS_Name /= Name_No_DSA
5835 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
5839 -- Set the entity. Note that the reason we call Set_Entity for the
5840 -- overloadable case, as opposed to Set_Entity_With_Checks is
5841 -- that in the overloaded case, the initial call can set the wrong
5842 -- homonym. The call that sets the right homonym is in Sem_Res and
5843 -- that call does use Set_Entity_With_Checks, so we don't miss
5846 if Is_Overloadable (E) then
5849 Set_Entity_With_Checks (N, E);
5855 Set_Etype (N, Get_Full_View (Etype (E)));
5858 if Debug_Flag_E then
5859 Write_Str (" found ");
5860 Write_Entity_Info (E, " ");
5863 -- If the Ekind of the entity is Void, it means that all homonyms
5864 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
5865 -- test is skipped if the current scope is a record and the name is
5866 -- a pragma argument expression (case of Atomic and Volatile pragmas
5867 -- and possibly other similar pragmas added later, which are allowed
5868 -- to reference components in the current record).
5870 if Ekind (E) = E_Void
5872 (not Is_Record_Type (Current_Scope)
5873 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
5875 Premature_Usage (N);
5877 -- If the entity is overloadable, collect all interpretations of the
5878 -- name for subsequent overload resolution. We optimize a bit here to
5879 -- do this only if we have an overloadable entity that is not on its
5880 -- own on the homonym chain.
5882 elsif Is_Overloadable (E)
5883 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
5885 Collect_Interps (N);
5887 -- If no homonyms were visible, the entity is unambiguous
5889 if not Is_Overloaded (N) then
5890 if Reference_OK and then not Is_Actual_Parameter then
5891 Generate_Reference (E, N);
5895 -- Case of non-overloadable entity, set the entity providing that
5896 -- we do not have the case of a discriminant reference within a
5897 -- default expression. Such references are replaced with the
5898 -- corresponding discriminal, which is the formal corresponding to
5899 -- to the discriminant in the initialization procedure.
5902 -- Entity is unambiguous, indicate that it is referenced here
5904 -- For a renaming of an object, always generate simple reference,
5905 -- we don't try to keep track of assignments in this case, except
5906 -- in SPARK mode where renamings are traversed for generating
5907 -- local effects of subprograms.
5910 and then Is_Object (E)
5911 and then Present (Renamed_Object (E))
5912 and then not GNATprove_Mode
5914 Generate_Reference (E, N);
5916 -- If the renamed entity is a private protected component,
5917 -- reference the original component as well. This needs to be
5918 -- done because the private renamings are installed before any
5919 -- analysis has occurred. Reference to a private component will
5920 -- resolve to the renaming and the original component will be
5921 -- left unreferenced, hence the following.
5923 if Is_Prival (E) then
5924 Generate_Reference (Prival_Link (E), N);
5927 -- One odd case is that we do not want to set the Referenced flag
5928 -- if the entity is a label, and the identifier is the label in
5929 -- the source, since this is not a reference from the point of
5930 -- view of the user.
5932 elsif Nkind (Parent (N)) = N_Label then
5934 R : constant Boolean := Referenced (E);
5937 -- Generate reference unless this is an actual parameter
5938 -- (see comment below)
5940 if Reference_OK and then Is_Actual_Parameter then
5941 Generate_Reference (E, N);
5942 Set_Referenced (E, R);
5946 -- Normal case, not a label: generate reference
5949 if Reference_OK and then not Is_Actual_Parameter then
5951 -- Package or generic package is always a simple reference
5953 if Ekind_In (E, E_Package, E_Generic_Package) then
5954 Generate_Reference (E, N, 'r');
5956 -- Else see if we have a left hand side
5961 Generate_Reference (E, N, 'm');
5964 Generate_Reference (E, N, 'r');
5966 -- If we don't know now, generate reference later
5969 Deferred_References.Append ((E, N));
5975 Set_Entity_Or_Discriminal (N, E);
5977 -- The name may designate a generalized reference, in which case
5978 -- the dereference interpretation will be included. Context is
5979 -- one in which a name is legal.
5981 if Ada_Version >= Ada_2012
5983 (Nkind (Parent (N)) in N_Subexpr
5984 or else Nkind_In (Parent (N), N_Assignment_Statement,
5985 N_Object_Declaration,
5986 N_Parameter_Association))
5988 Check_Implicit_Dereference (N, Etype (E));
5993 -- Mark relevant use-type and use-package clauses as effective if the
5994 -- node in question is not overloaded and therefore does not require
5997 -- Note: Generic actual subprograms do not follow the normal resolution
5998 -- path, so ignore the fact that they are overloaded and mark them
6001 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6002 Mark_Use_Clauses (N);
6005 -- Come here with entity set
6008 Check_Restriction_No_Use_Of_Entity (N);
6010 -- Annotate the tree by creating a variable reference marker in case the
6011 -- original variable reference is folded or optimized away. The variable
6012 -- reference marker is automatically saved for later examination by the
6013 -- ABE Processing phase. Variable references which act as actuals in a
6014 -- call require special processing and are left to Resolve_Actuals. The
6015 -- reference is a write when it appears on the left hand side of an
6019 and then Needs_Variable_Reference_Marker
6024 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6027 Build_Variable_Reference_Marker
6029 Read => not Is_Assignment_LHS,
6030 Write => Is_Assignment_LHS);
6033 end Find_Direct_Name;
6035 ------------------------
6036 -- Find_Expanded_Name --
6037 ------------------------
6039 -- This routine searches the homonym chain of the entity until it finds
6040 -- an entity declared in the scope denoted by the prefix. If the entity
6041 -- is private, it may nevertheless be immediately visible, if we are in
6042 -- the scope of its declaration.
6044 procedure Find_Expanded_Name (N : Node_Id) is
6045 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean;
6046 -- Determine whether expanded name Nod appears within a pragma which is
6047 -- a suitable context for an abstract view of a state or variable. The
6048 -- following pragmas fall in this category:
6055 -- In addition, pragma Abstract_State is also considered suitable even
6056 -- though it is an illegal context for an abstract view as this allows
6057 -- for proper resolution of abstract views of variables. This illegal
6058 -- context is later flagged in the analysis of indicator Part_Of.
6060 -----------------------------
6061 -- In_Abstract_View_Pragma --
6062 -----------------------------
6064 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is
6068 -- Climb the parent chain looking for a pragma
6071 while Present (Par) loop
6072 if Nkind (Par) = N_Pragma then
6073 if Nam_In (Pragma_Name_Unmapped (Par),
6074 Name_Abstract_State,
6078 Name_Refined_Depends,
6079 Name_Refined_Global)
6083 -- Otherwise the pragma is not a legal context for an abstract
6090 -- Prevent the search from going too far
6092 elsif Is_Body_Or_Package_Declaration (Par) then
6096 Par := Parent (Par);
6100 end In_Abstract_View_Pragma;
6104 Selector : constant Node_Id := Selector_Name (N);
6106 Candidate : Entity_Id := Empty;
6110 -- Start of processing for Find_Expanded_Name
6113 P_Name := Entity (Prefix (N));
6115 -- If the prefix is a renamed package, look for the entity in the
6116 -- original package.
6118 if Ekind (P_Name) = E_Package
6119 and then Present (Renamed_Object (P_Name))
6121 P_Name := Renamed_Object (P_Name);
6123 -- Rewrite node with entity field pointing to renamed object
6125 Rewrite (Prefix (N), New_Copy (Prefix (N)));
6126 Set_Entity (Prefix (N), P_Name);
6128 -- If the prefix is an object of a concurrent type, look for
6129 -- the entity in the associated task or protected type.
6131 elsif Is_Concurrent_Type (Etype (P_Name)) then
6132 P_Name := Etype (P_Name);
6135 Id := Current_Entity (Selector);
6138 Is_New_Candidate : Boolean;
6141 while Present (Id) loop
6142 if Scope (Id) = P_Name then
6144 Is_New_Candidate := True;
6146 -- Handle abstract views of states and variables. These are
6147 -- acceptable candidates only when the reference to the view
6148 -- appears in certain pragmas.
6150 if Ekind (Id) = E_Abstract_State
6151 and then From_Limited_With (Id)
6152 and then Present (Non_Limited_View (Id))
6154 if In_Abstract_View_Pragma (N) then
6155 Candidate := Non_Limited_View (Id);
6156 Is_New_Candidate := True;
6158 -- Hide the candidate because it is not used in a proper
6163 Is_New_Candidate := False;
6167 -- Ada 2005 (AI-217): Handle shadow entities associated with
6168 -- types declared in limited-withed nested packages. We don't need
6169 -- to handle E_Incomplete_Subtype entities because the entities
6170 -- in the limited view are always E_Incomplete_Type and
6171 -- E_Class_Wide_Type entities (see Build_Limited_Views).
6173 -- Regarding the expression used to evaluate the scope, it
6174 -- is important to note that the limited view also has shadow
6175 -- entities associated nested packages. For this reason the
6176 -- correct scope of the entity is the scope of the real entity.
6177 -- The non-limited view may itself be incomplete, in which case
6178 -- get the full view if available.
6180 elsif Ekind_In (Id, E_Incomplete_Type, E_Class_Wide_Type)
6181 and then From_Limited_With (Id)
6182 and then Present (Non_Limited_View (Id))
6183 and then Scope (Non_Limited_View (Id)) = P_Name
6185 Candidate := Get_Full_View (Non_Limited_View (Id));
6186 Is_New_Candidate := True;
6188 -- An unusual case arises with a fully qualified name for an
6189 -- entity local to a generic child unit package, within an
6190 -- instantiation of that package. The name of the unit now
6191 -- denotes the renaming created within the instance. This is
6192 -- only relevant in an instance body, see below.
6194 elsif Is_Generic_Instance (Scope (Id))
6195 and then In_Open_Scopes (Scope (Id))
6196 and then In_Instance_Body
6197 and then Ekind (Scope (Id)) = E_Package
6198 and then Ekind (Id) = E_Package
6199 and then Renamed_Entity (Id) = Scope (Id)
6200 and then Is_Immediately_Visible (P_Name)
6202 Is_New_Candidate := True;
6205 Is_New_Candidate := False;
6208 if Is_New_Candidate then
6210 -- If entity is a child unit, either it is a visible child of
6211 -- the prefix, or we are in the body of a generic prefix, as
6212 -- will happen when a child unit is instantiated in the body
6213 -- of a generic parent. This is because the instance body does
6214 -- not restore the full compilation context, given that all
6215 -- non-local references have been captured.
6217 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
6218 exit when Is_Visible_Lib_Unit (Id)
6219 or else (Is_Child_Unit (Id)
6220 and then In_Open_Scopes (Scope (Id))
6221 and then In_Instance_Body);
6223 exit when not Is_Hidden (Id);
6226 exit when Is_Immediately_Visible (Id);
6234 and then Ekind_In (P_Name, E_Procedure, E_Function)
6235 and then Is_Generic_Instance (P_Name)
6237 -- Expanded name denotes entity in (instance of) generic subprogram.
6238 -- The entity may be in the subprogram instance, or may denote one of
6239 -- the formals, which is declared in the enclosing wrapper package.
6241 P_Name := Scope (P_Name);
6243 Id := Current_Entity (Selector);
6244 while Present (Id) loop
6245 exit when Scope (Id) = P_Name;
6250 if No (Id) or else Chars (Id) /= Chars (Selector) then
6251 Set_Etype (N, Any_Type);
6253 -- If we are looking for an entity defined in System, try to find it
6254 -- in the child package that may have been provided as an extension
6255 -- to System. The Extend_System pragma will have supplied the name of
6256 -- the extension, which may have to be loaded.
6258 if Chars (P_Name) = Name_System
6259 and then Scope (P_Name) = Standard_Standard
6260 and then Present (System_Extend_Unit)
6261 and then Present_System_Aux (N)
6263 Set_Entity (Prefix (N), System_Aux_Id);
6264 Find_Expanded_Name (N);
6267 -- There is an implicit instance of the predefined operator in
6268 -- the given scope. The operator entity is defined in Standard.
6269 -- Has_Implicit_Operator makes the node into an Expanded_Name.
6271 elsif Nkind (Selector) = N_Operator_Symbol
6272 and then Has_Implicit_Operator (N)
6276 -- If there is no literal defined in the scope denoted by the
6277 -- prefix, the literal may belong to (a type derived from)
6278 -- Standard_Character, for which we have no explicit literals.
6280 elsif Nkind (Selector) = N_Character_Literal
6281 and then Has_Implicit_Character_Literal (N)
6286 -- If the prefix is a single concurrent object, use its name in
6287 -- the error message, rather than that of the anonymous type.
6289 if Is_Concurrent_Type (P_Name)
6290 and then Is_Internal_Name (Chars (P_Name))
6292 Error_Msg_Node_2 := Entity (Prefix (N));
6294 Error_Msg_Node_2 := P_Name;
6297 if P_Name = System_Aux_Id then
6298 P_Name := Scope (P_Name);
6299 Set_Entity (Prefix (N), P_Name);
6302 if Present (Candidate) then
6304 -- If we know that the unit is a child unit we can give a more
6305 -- accurate error message.
6307 if Is_Child_Unit (Candidate) then
6309 -- If the candidate is a private child unit and we are in
6310 -- the visible part of a public unit, specialize the error
6311 -- message. There might be a private with_clause for it,
6312 -- but it is not currently active.
6314 if Is_Private_Descendant (Candidate)
6315 and then Ekind (Current_Scope) = E_Package
6316 and then not In_Private_Part (Current_Scope)
6317 and then not Is_Private_Descendant (Current_Scope)
6320 ("private child unit& is not visible here", Selector);
6322 -- Normal case where we have a missing with for a child unit
6325 Error_Msg_Qual_Level := 99;
6326 Error_Msg_NE -- CODEFIX
6327 ("missing `WITH &;`", Selector, Candidate);
6328 Error_Msg_Qual_Level := 0;
6331 -- Here we don't know that this is a child unit
6334 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
6338 -- Within the instantiation of a child unit, the prefix may
6339 -- denote the parent instance, but the selector has the name
6340 -- of the original child. That is to say, when A.B appears
6341 -- within an instantiation of generic child unit B, the scope
6342 -- stack includes an instance of A (P_Name) and an instance
6343 -- of B under some other name. We scan the scope to find this
6344 -- child instance, which is the desired entity.
6345 -- Note that the parent may itself be a child instance, if
6346 -- the reference is of the form A.B.C, in which case A.B has
6347 -- already been rewritten with the proper entity.
6349 if In_Open_Scopes (P_Name)
6350 and then Is_Generic_Instance (P_Name)
6353 Gen_Par : constant Entity_Id :=
6354 Generic_Parent (Specification
6355 (Unit_Declaration_Node (P_Name)));
6356 S : Entity_Id := Current_Scope;
6360 for J in reverse 0 .. Scope_Stack.Last loop
6361 S := Scope_Stack.Table (J).Entity;
6363 exit when S = Standard_Standard;
6365 if Ekind_In (S, E_Function,
6370 Generic_Parent (Specification
6371 (Unit_Declaration_Node (S)));
6373 -- Check that P is a generic child of the generic
6374 -- parent of the prefix.
6377 and then Chars (P) = Chars (Selector)
6378 and then Scope (P) = Gen_Par
6389 -- If this is a selection from Ada, System or Interfaces, then
6390 -- we assume a missing with for the corresponding package.
6392 if Is_Known_Unit (N)
6393 and then not (Present (Entity (Prefix (N)))
6394 and then Scope (Entity (Prefix (N))) /=
6397 if not Error_Posted (N) then
6398 Error_Msg_Node_2 := Selector;
6399 Error_Msg_N -- CODEFIX
6400 ("missing `WITH &.&;`", Prefix (N));
6403 -- If this is a selection from a dummy package, then suppress
6404 -- the error message, of course the entity is missing if the
6405 -- package is missing.
6407 elsif Sloc (Error_Msg_Node_2) = No_Location then
6410 -- Here we have the case of an undefined component
6413 -- The prefix may hide a homonym in the context that
6414 -- declares the desired entity. This error can use a
6415 -- specialized message.
6417 if In_Open_Scopes (P_Name) then
6419 H : constant Entity_Id := Homonym (P_Name);
6423 and then Is_Compilation_Unit (H)
6425 (Is_Immediately_Visible (H)
6426 or else Is_Visible_Lib_Unit (H))
6428 Id := First_Entity (H);
6429 while Present (Id) loop
6430 if Chars (Id) = Chars (Selector) then
6431 Error_Msg_Qual_Level := 99;
6432 Error_Msg_Name_1 := Chars (Selector);
6434 ("% not declared in&", N, P_Name);
6436 ("\use fully qualified name starting with "
6437 & "Standard to make& visible", N, H);
6438 Error_Msg_Qual_Level := 0;
6446 -- If not found, standard error message
6448 Error_Msg_NE ("& not declared in&", N, Selector);
6454 -- Might be worth specializing the case when the prefix
6455 -- is a limited view.
6456 -- ... not declared in limited view of...
6458 Error_Msg_NE ("& not declared in&", N, Selector);
6461 -- Check for misspelling of some entity in prefix
6463 Id := First_Entity (P_Name);
6464 while Present (Id) loop
6465 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
6466 and then not Is_Internal_Name (Chars (Id))
6468 Error_Msg_NE -- CODEFIX
6469 ("possible misspelling of&", Selector, Id);
6476 -- Specialize the message if this may be an instantiation
6477 -- of a child unit that was not mentioned in the context.
6479 if Nkind (Parent (N)) = N_Package_Instantiation
6480 and then Is_Generic_Instance (Entity (Prefix (N)))
6481 and then Is_Compilation_Unit
6482 (Generic_Parent (Parent (Entity (Prefix (N)))))
6484 Error_Msg_Node_2 := Selector;
6485 Error_Msg_N -- CODEFIX
6486 ("\missing `WITH &.&;`", Prefix (N));
6496 if Comes_From_Source (N)
6497 and then Is_Remote_Access_To_Subprogram_Type (Id)
6498 and then Ekind (Id) = E_Access_Subprogram_Type
6499 and then Present (Equivalent_Type (Id))
6501 -- If we are not actually generating distribution code (i.e. the
6502 -- current PCS is the dummy non-distributed version), then the
6503 -- Equivalent_Type will be missing, and Id should be treated as
6504 -- a regular access-to-subprogram type.
6506 Id := Equivalent_Type (Id);
6507 Set_Chars (Selector, Chars (Id));
6510 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
6512 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
6513 if From_Limited_With (Id)
6514 or else Is_Type (Id)
6515 or else Ekind (Id) = E_Package
6520 ("limited withed package can only be used to access incomplete "
6525 if Is_Task_Type (P_Name)
6526 and then ((Ekind (Id) = E_Entry
6527 and then Nkind (Parent (N)) /= N_Attribute_Reference)
6529 (Ekind (Id) = E_Entry_Family
6531 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
6533 -- If both the task type and the entry are in scope, this may still
6534 -- be the expanded name of an entry formal.
6536 if In_Open_Scopes (Id)
6537 and then Nkind (Parent (N)) = N_Selected_Component
6542 -- It is an entry call after all, either to the current task
6543 -- (which will deadlock) or to an enclosing task.
6545 Analyze_Selected_Component (N);
6550 Change_Selected_Component_To_Expanded_Name (N);
6552 -- Preserve relevant elaboration-related attributes of the context which
6553 -- are no longer available or very expensive to recompute once analysis,
6554 -- resolution, and expansion are over.
6556 Mark_Elaboration_Attributes
6562 -- Set appropriate type
6564 if Is_Type (Id) then
6567 Set_Etype (N, Get_Full_View (Etype (Id)));
6570 -- Do style check and generate reference, but skip both steps if this
6571 -- entity has homonyms, since we may not have the right homonym set yet.
6572 -- The proper homonym will be set during the resolve phase.
6574 if Has_Homonym (Id) then
6578 Set_Entity_Or_Discriminal (N, Id);
6582 Generate_Reference (Id, N, 'm');
6585 Generate_Reference (Id, N, 'r');
6588 Deferred_References.Append ((Id, N));
6592 -- Check for violation of No_Wide_Characters
6594 Check_Wide_Character_Restriction (Id, N);
6596 -- If the Ekind of the entity is Void, it means that all homonyms are
6597 -- hidden from all visibility (RM 8.3(5,14-20)).
6599 if Ekind (Id) = E_Void then
6600 Premature_Usage (N);
6602 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6604 H : Entity_Id := Homonym (Id);
6607 while Present (H) loop
6608 if Scope (H) = Scope (Id)
6609 and then (not Is_Hidden (H)
6610 or else Is_Immediately_Visible (H))
6612 Collect_Interps (N);
6619 -- If an extension of System is present, collect possible explicit
6620 -- overloadings declared in the extension.
6622 if Chars (P_Name) = Name_System
6623 and then Scope (P_Name) = Standard_Standard
6624 and then Present (System_Extend_Unit)
6625 and then Present_System_Aux (N)
6627 H := Current_Entity (Id);
6629 while Present (H) loop
6630 if Scope (H) = System_Aux_Id then
6631 Add_One_Interp (N, H, Etype (H));
6640 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6641 and then Scope (Id) /= Standard_Standard
6643 -- In addition to user-defined operators in the given scope, there
6644 -- may be an implicit instance of the predefined operator. The
6645 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6646 -- and added to the interpretations. Procedure Add_One_Interp will
6647 -- determine which hides which.
6649 if Has_Implicit_Operator (N) then
6654 -- If there is a single interpretation for N we can generate a
6655 -- reference to the unique entity found.
6657 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6658 Generate_Reference (Id, N);
6661 -- Mark relevant use-type and use-package clauses as effective if the
6662 -- node in question is not overloaded and therefore does not require
6665 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6666 Mark_Use_Clauses (N);
6669 Check_Restriction_No_Use_Of_Entity (N);
6671 -- Annotate the tree by creating a variable reference marker in case the
6672 -- original variable reference is folded or optimized away. The variable
6673 -- reference marker is automatically saved for later examination by the
6674 -- ABE Processing phase. Variable references which act as actuals in a
6675 -- call require special processing and are left to Resolve_Actuals. The
6676 -- reference is a write when it appears on the left hand side of an
6679 if Needs_Variable_Reference_Marker
6684 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6687 Build_Variable_Reference_Marker
6689 Read => not Is_Assignment_LHS,
6690 Write => Is_Assignment_LHS);
6693 end Find_Expanded_Name;
6695 --------------------
6696 -- Find_Most_Prev --
6697 --------------------
6699 function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id is
6703 -- Loop through the Prev_Use_Clause chain
6706 while Present (Prev_Use_Clause (Curr)) loop
6707 Curr := Prev_Use_Clause (Curr);
6713 -------------------------
6714 -- Find_Renamed_Entity --
6715 -------------------------
6717 function Find_Renamed_Entity
6721 Is_Actual : Boolean := False) return Entity_Id
6724 I1 : Interp_Index := 0; -- Suppress junk warnings
6730 function Find_Nearer_Entity
6733 Old2_S : Entity_Id) return Entity_Id;
6734 -- Determine whether one of Old_S1 and Old_S2 is nearer to New_S than
6735 -- the other, and return it if so. Return Empty otherwise. We use this
6736 -- in conjunction with Inherit_Renamed_Profile to simplify later type
6737 -- disambiguation for actual subprograms in instances.
6739 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
6740 -- If the renamed entity is an implicit operator, check whether it is
6741 -- visible because its operand type is properly visible. This check
6742 -- applies to explicit renamed entities that appear in the source in a
6743 -- renaming declaration or a formal subprogram instance, but not to
6744 -- default generic actuals with a name.
6746 function Report_Overload return Entity_Id;
6747 -- List possible interpretations, and specialize message in the
6748 -- case of a generic actual.
6750 function Within (Inner, Outer : Entity_Id) return Boolean;
6751 -- Determine whether a candidate subprogram is defined within the
6752 -- enclosing instance. If yes, it has precedence over outer candidates.
6754 --------------------------
6755 -- Find_Nearer_Entity --
6756 --------------------------
6758 function Find_Nearer_Entity
6761 Old2_S : Entity_Id) return Entity_Id
6769 New_F := First_Formal (New_S);
6770 Old1_F := First_Formal (Old1_S);
6771 Old2_F := First_Formal (Old2_S);
6773 -- The criterion is whether the type of the formals of one of Old1_S
6774 -- and Old2_S is an ancestor subtype of the type of the corresponding
6775 -- formals of New_S while the other is not (we already know that they
6776 -- are all subtypes of the same base type).
6778 -- This makes it possible to find the more correct renamed entity in
6779 -- the case of a generic instantiation nested in an enclosing one for
6780 -- which different formal types get the same actual type, which will
6781 -- in turn make it possible for Inherit_Renamed_Profile to preserve
6782 -- types on formal parameters and ultimately simplify disambiguation.
6784 -- Consider the follow package G:
6787 -- type Item_T is private;
6788 -- with function Compare (L, R: Item_T) return Boolean is <>;
6790 -- type Bound_T is private;
6791 -- with function Compare (L, R : Bound_T) return Boolean is <>;
6796 -- package body G is
6797 -- package My_Inner is Inner_G (Bound_T);
6801 -- with the following package Inner_G:
6804 -- type T is private;
6805 -- with function Compare (L, R: T) return Boolean is <>;
6806 -- package Inner_G is
6807 -- function "<" (L, R: T) return Boolean is (Compare (L, R));
6810 -- If G is instantiated on the same actual type with a single Compare
6814 -- function Compare (L, R : T) return Boolean;
6815 -- package My_G is new (T, T);
6817 -- then the renaming generated for Compare in the inner instantiation
6818 -- is ambiguous: it can rename either of the renamings generated for
6819 -- the outer instantiation. Now if the first one is picked up, then
6820 -- the subtypes of the formal parameters of the renaming will not be
6821 -- preserved in Inherit_Renamed_Profile because they are subtypes of
6822 -- the Bound_T formal type and not of the Item_T formal type, so we
6823 -- need to arrange for the second one to be picked up instead.
6825 while Present (New_F) loop
6826 if Etype (Old1_F) /= Etype (Old2_F) then
6827 Anc_T := Ancestor_Subtype (Etype (New_F));
6829 if Etype (Old1_F) = Anc_T then
6831 elsif Etype (Old2_F) = Anc_T then
6836 Next_Formal (New_F);
6837 Next_Formal (Old1_F);
6838 Next_Formal (Old2_F);
6841 pragma Assert (No (Old1_F));
6842 pragma Assert (No (Old2_F));
6845 end Find_Nearer_Entity;
6847 --------------------------
6848 -- Is_Visible_Operation --
6849 --------------------------
6851 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
6857 if Ekind (Op) /= E_Operator
6858 or else Scope (Op) /= Standard_Standard
6859 or else (In_Instance
6860 and then (not Is_Actual
6861 or else Present (Enclosing_Instance)))
6866 -- For a fixed point type operator, check the resulting type,
6867 -- because it may be a mixed mode integer * fixed operation.
6869 if Present (Next_Formal (First_Formal (New_S)))
6870 and then Is_Fixed_Point_Type (Etype (New_S))
6872 Typ := Etype (New_S);
6874 Typ := Etype (First_Formal (New_S));
6877 Btyp := Base_Type (Typ);
6879 if Nkind (Nam) /= N_Expanded_Name then
6880 return (In_Open_Scopes (Scope (Btyp))
6881 or else Is_Potentially_Use_Visible (Btyp)
6882 or else In_Use (Btyp)
6883 or else In_Use (Scope (Btyp)));
6886 Scop := Entity (Prefix (Nam));
6888 if Ekind (Scop) = E_Package
6889 and then Present (Renamed_Object (Scop))
6891 Scop := Renamed_Object (Scop);
6894 -- Operator is visible if prefix of expanded name denotes
6895 -- scope of type, or else type is defined in System_Aux
6896 -- and the prefix denotes System.
6898 return Scope (Btyp) = Scop
6899 or else (Scope (Btyp) = System_Aux_Id
6900 and then Scope (Scope (Btyp)) = Scop);
6903 end Is_Visible_Operation;
6909 function Within (Inner, Outer : Entity_Id) return Boolean is
6913 Sc := Scope (Inner);
6914 while Sc /= Standard_Standard loop
6925 ---------------------
6926 -- Report_Overload --
6927 ---------------------
6929 function Report_Overload return Entity_Id is
6932 Error_Msg_NE -- CODEFIX
6933 ("ambiguous actual subprogram&, " &
6934 "possible interpretations:", N, Nam);
6936 Error_Msg_N -- CODEFIX
6937 ("ambiguous subprogram, " &
6938 "possible interpretations:", N);
6941 List_Interps (Nam, N);
6943 end Report_Overload;
6945 -- Start of processing for Find_Renamed_Entity
6949 Candidate_Renaming := Empty;
6951 if Is_Overloaded (Nam) then
6952 Get_First_Interp (Nam, Ind, It);
6953 while Present (It.Nam) loop
6954 if Entity_Matches_Spec (It.Nam, New_S)
6955 and then Is_Visible_Operation (It.Nam)
6957 if Old_S /= Any_Id then
6959 -- Note: The call to Disambiguate only happens if a
6960 -- previous interpretation was found, in which case I1
6961 -- has received a value.
6963 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
6965 if It1 = No_Interp then
6966 Inst := Enclosing_Instance;
6968 if Present (Inst) then
6969 if Within (It.Nam, Inst) then
6970 if Within (Old_S, Inst) then
6972 It_D : constant Uint := Scope_Depth (It.Nam);
6973 Old_D : constant Uint := Scope_Depth (Old_S);
6976 -- Choose the innermost subprogram, which
6977 -- would hide the outer one in the generic.
6979 if Old_D > It_D then
6981 elsif It_D > Old_D then
6985 -- Otherwise, if we can determine that one
6986 -- of the entities is nearer to the renaming
6987 -- than the other, choose it. If not, then
6988 -- return the newer one as done historically.
6991 Find_Nearer_Entity (New_S, Old_S, It.Nam);
6992 if Present (N_Ent) then
7000 elsif Within (Old_S, Inst) then
7004 return Report_Overload;
7007 -- If not within an instance, ambiguity is real
7010 return Report_Overload;
7024 Present (First_Formal (It.Nam))
7025 and then Present (First_Formal (New_S))
7026 and then (Base_Type (Etype (First_Formal (It.Nam))) =
7027 Base_Type (Etype (First_Formal (New_S))))
7029 Candidate_Renaming := It.Nam;
7032 Get_Next_Interp (Ind, It);
7035 Set_Entity (Nam, Old_S);
7037 if Old_S /= Any_Id then
7038 Set_Is_Overloaded (Nam, False);
7041 -- Non-overloaded case
7045 and then Present (Enclosing_Instance)
7046 and then Entity_Matches_Spec (Entity (Nam), New_S)
7048 Old_S := Entity (Nam);
7050 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
7051 Candidate_Renaming := New_S;
7053 if Is_Visible_Operation (Entity (Nam)) then
7054 Old_S := Entity (Nam);
7057 elsif Present (First_Formal (Entity (Nam)))
7058 and then Present (First_Formal (New_S))
7059 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
7060 Base_Type (Etype (First_Formal (New_S))))
7062 Candidate_Renaming := Entity (Nam);
7067 end Find_Renamed_Entity;
7069 -----------------------------
7070 -- Find_Selected_Component --
7071 -----------------------------
7073 procedure Find_Selected_Component (N : Node_Id) is
7074 P : constant Node_Id := Prefix (N);
7077 -- Entity denoted by prefix
7084 function Available_Subtype return Boolean;
7085 -- A small optimization: if the prefix is constrained and the component
7086 -- is an array type we may already have a usable subtype for it, so we
7087 -- can use it rather than generating a new one, because the bounds
7088 -- will be the values of the discriminants and not discriminant refs.
7089 -- This simplifies value tracing in GNATProve. For consistency, both
7090 -- the entity name and the subtype come from the constrained component.
7092 -- This is only used in GNATProve mode: when generating code it may be
7093 -- necessary to create an itype in the scope of use of the selected
7094 -- component, e.g. in the context of a expanded record equality.
7096 function Is_Reference_In_Subunit return Boolean;
7097 -- In a subunit, the scope depth is not a proper measure of hiding,
7098 -- because the context of the proper body may itself hide entities in
7099 -- parent units. This rare case requires inspecting the tree directly
7100 -- because the proper body is inserted in the main unit and its context
7101 -- is simply added to that of the parent.
7103 -----------------------
7104 -- Available_Subtype --
7105 -----------------------
7107 function Available_Subtype return Boolean is
7111 if GNATprove_Mode then
7112 Comp := First_Entity (Etype (P));
7113 while Present (Comp) loop
7114 if Chars (Comp) = Chars (Selector_Name (N)) then
7115 Set_Etype (N, Etype (Comp));
7116 Set_Entity (Selector_Name (N), Comp);
7117 Set_Etype (Selector_Name (N), Etype (Comp));
7121 Next_Component (Comp);
7126 end Available_Subtype;
7128 -----------------------------
7129 -- Is_Reference_In_Subunit --
7130 -----------------------------
7132 function Is_Reference_In_Subunit return Boolean is
7134 Comp_Unit : Node_Id;
7138 while Present (Comp_Unit)
7139 and then Nkind (Comp_Unit) /= N_Compilation_Unit
7141 Comp_Unit := Parent (Comp_Unit);
7144 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
7148 -- Now check whether the package is in the context of the subunit
7150 Clause := First (Context_Items (Comp_Unit));
7151 while Present (Clause) loop
7152 if Nkind (Clause) = N_With_Clause
7153 and then Entity (Name (Clause)) = P_Name
7158 Clause := Next (Clause);
7162 end Is_Reference_In_Subunit;
7164 -- Start of processing for Find_Selected_Component
7169 if Nkind (P) = N_Error then
7173 -- Selector name cannot be a character literal or an operator symbol in
7174 -- SPARK, except for the operator symbol in a renaming.
7176 if Restriction_Check_Required (SPARK_05) then
7177 if Nkind (Selector_Name (N)) = N_Character_Literal then
7178 Check_SPARK_05_Restriction
7179 ("character literal cannot be prefixed", N);
7180 elsif Nkind (Selector_Name (N)) = N_Operator_Symbol
7181 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
7183 Check_SPARK_05_Restriction
7184 ("operator symbol cannot be prefixed", N);
7188 -- If the selector already has an entity, the node has been constructed
7189 -- in the course of expansion, and is known to be valid. Do not verify
7190 -- that it is defined for the type (it may be a private component used
7191 -- in the expansion of record equality).
7193 if Present (Entity (Selector_Name (N))) then
7194 if No (Etype (N)) or else Etype (N) = Any_Type then
7196 Sel_Name : constant Node_Id := Selector_Name (N);
7197 Selector : constant Entity_Id := Entity (Sel_Name);
7201 Set_Etype (Sel_Name, Etype (Selector));
7203 if not Is_Entity_Name (P) then
7207 -- Build an actual subtype except for the first parameter
7208 -- of an init proc, where this actual subtype is by
7209 -- definition incorrect, since the object is uninitialized
7210 -- (and does not even have defined discriminants etc.)
7212 if Is_Entity_Name (P)
7213 and then Ekind (Entity (P)) = E_Function
7215 Nam := New_Copy (P);
7217 if Is_Overloaded (P) then
7218 Save_Interps (P, Nam);
7221 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
7223 Analyze_Selected_Component (N);
7226 elsif Ekind (Selector) = E_Component
7227 and then (not Is_Entity_Name (P)
7228 or else Chars (Entity (P)) /= Name_uInit)
7230 -- Check if we already have an available subtype we can use
7232 if Ekind (Etype (P)) = E_Record_Subtype
7233 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
7234 and then Is_Array_Type (Etype (Selector))
7235 and then not Is_Packed (Etype (Selector))
7236 and then Available_Subtype
7240 -- Do not build the subtype when referencing components of
7241 -- dispatch table wrappers. Required to avoid generating
7242 -- elaboration code with HI runtimes.
7244 elsif RTU_Loaded (Ada_Tags)
7246 ((RTE_Available (RE_Dispatch_Table_Wrapper)
7247 and then Scope (Selector) =
7248 RTE (RE_Dispatch_Table_Wrapper))
7250 (RTE_Available (RE_No_Dispatch_Table_Wrapper)
7251 and then Scope (Selector) =
7252 RTE (RE_No_Dispatch_Table_Wrapper)))
7257 Build_Actual_Subtype_Of_Component
7258 (Etype (Selector), N);
7265 if No (C_Etype) then
7266 C_Etype := Etype (Selector);
7268 Insert_Action (N, C_Etype);
7269 C_Etype := Defining_Identifier (C_Etype);
7272 Set_Etype (N, C_Etype);
7275 -- If this is the name of an entry or protected operation, and
7276 -- the prefix is an access type, insert an explicit dereference,
7277 -- so that entry calls are treated uniformly.
7279 if Is_Access_Type (Etype (P))
7280 and then Is_Concurrent_Type (Designated_Type (Etype (P)))
7283 New_P : constant Node_Id :=
7284 Make_Explicit_Dereference (Sloc (P),
7285 Prefix => Relocate_Node (P));
7288 Set_Etype (P, Designated_Type (Etype (Prefix (P))));
7292 -- If the selected component appears within a default expression
7293 -- and it has an actual subtype, the preanalysis has not yet
7294 -- completed its analysis, because Insert_Actions is disabled in
7295 -- that context. Within the init proc of the enclosing type we
7296 -- must complete this analysis, if an actual subtype was created.
7298 elsif Inside_Init_Proc then
7300 Typ : constant Entity_Id := Etype (N);
7301 Decl : constant Node_Id := Declaration_Node (Typ);
7303 if Nkind (Decl) = N_Subtype_Declaration
7304 and then not Analyzed (Decl)
7305 and then Is_List_Member (Decl)
7306 and then No (Parent (Decl))
7309 Insert_Action (N, Decl);
7316 elsif Is_Entity_Name (P) then
7317 P_Name := Entity (P);
7319 -- The prefix may denote an enclosing type which is the completion
7320 -- of an incomplete type declaration.
7322 if Is_Type (P_Name) then
7323 Set_Entity (P, Get_Full_View (P_Name));
7324 Set_Etype (P, Entity (P));
7325 P_Name := Entity (P);
7328 P_Type := Base_Type (Etype (P));
7330 if Debug_Flag_E then
7331 Write_Str ("Found prefix type to be ");
7332 Write_Entity_Info (P_Type, " "); Write_Eol;
7335 -- The designated type may be a limited view with no components.
7336 -- Check whether the non-limited view is available, because in some
7337 -- cases this will not be set when installing the context. Rewrite
7338 -- the node by introducing an explicit dereference at once, and
7339 -- setting the type of the rewritten prefix to the non-limited view
7340 -- of the original designated type.
7342 if Is_Access_Type (P_Type) then
7344 Desig_Typ : constant Entity_Id :=
7345 Directly_Designated_Type (P_Type);
7348 if Is_Incomplete_Type (Desig_Typ)
7349 and then From_Limited_With (Desig_Typ)
7350 and then Present (Non_Limited_View (Desig_Typ))
7353 Make_Explicit_Dereference (Sloc (P),
7354 Prefix => Relocate_Node (P)));
7356 Set_Etype (P, Get_Full_View (Non_Limited_View (Desig_Typ)));
7357 P_Type := Etype (P);
7362 -- First check for components of a record object (not the
7363 -- result of a call, which is handled below).
7365 if Is_Appropriate_For_Record (P_Type)
7366 and then not Is_Overloadable (P_Name)
7367 and then not Is_Type (P_Name)
7369 -- Selected component of record. Type checking will validate
7370 -- name of selector.
7372 -- ??? Could we rewrite an implicit dereference into an explicit
7375 Analyze_Selected_Component (N);
7377 -- Reference to type name in predicate/invariant expression
7379 elsif Is_Appropriate_For_Entry_Prefix (P_Type)
7380 and then not In_Open_Scopes (P_Name)
7381 and then (not Is_Concurrent_Type (Etype (P_Name))
7382 or else not In_Open_Scopes (Etype (P_Name)))
7384 -- Call to protected operation or entry. Type checking is
7385 -- needed on the prefix.
7387 Analyze_Selected_Component (N);
7389 elsif (In_Open_Scopes (P_Name)
7390 and then Ekind (P_Name) /= E_Void
7391 and then not Is_Overloadable (P_Name))
7392 or else (Is_Concurrent_Type (Etype (P_Name))
7393 and then In_Open_Scopes (Etype (P_Name)))
7395 -- Prefix denotes an enclosing loop, block, or task, i.e. an
7396 -- enclosing construct that is not a subprogram or accept.
7398 -- A special case: a protected body may call an operation
7399 -- on an external object of the same type, in which case it
7400 -- is not an expanded name. If the prefix is the type itself,
7401 -- or the context is a single synchronized object it can only
7402 -- be interpreted as an expanded name.
7404 if Is_Concurrent_Type (Etype (P_Name)) then
7406 or else Present (Anonymous_Object (Etype (P_Name)))
7408 Find_Expanded_Name (N);
7411 Analyze_Selected_Component (N);
7416 Find_Expanded_Name (N);
7419 elsif Ekind (P_Name) = E_Package then
7420 Find_Expanded_Name (N);
7422 elsif Is_Overloadable (P_Name) then
7424 -- The subprogram may be a renaming (of an enclosing scope) as
7425 -- in the case of the name of the generic within an instantiation.
7427 if Ekind_In (P_Name, E_Procedure, E_Function)
7428 and then Present (Alias (P_Name))
7429 and then Is_Generic_Instance (Alias (P_Name))
7431 P_Name := Alias (P_Name);
7434 if Is_Overloaded (P) then
7436 -- The prefix must resolve to a unique enclosing construct
7439 Found : Boolean := False;
7444 Get_First_Interp (P, Ind, It);
7445 while Present (It.Nam) loop
7446 if In_Open_Scopes (It.Nam) then
7449 "prefix must be unique enclosing scope", N);
7450 Set_Entity (N, Any_Id);
7451 Set_Etype (N, Any_Type);
7460 Get_Next_Interp (Ind, It);
7465 if In_Open_Scopes (P_Name) then
7466 Set_Entity (P, P_Name);
7467 Set_Is_Overloaded (P, False);
7468 Find_Expanded_Name (N);
7471 -- If no interpretation as an expanded name is possible, it
7472 -- must be a selected component of a record returned by a
7473 -- function call. Reformat prefix as a function call, the rest
7474 -- is done by type resolution.
7476 -- Error if the prefix is procedure or entry, as is P.X
7478 if Ekind (P_Name) /= E_Function
7480 (not Is_Overloaded (P)
7481 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
7483 -- Prefix may mention a package that is hidden by a local
7484 -- declaration: let the user know. Scan the full homonym
7485 -- chain, the candidate package may be anywhere on it.
7487 if Present (Homonym (Current_Entity (P_Name))) then
7488 P_Name := Current_Entity (P_Name);
7490 while Present (P_Name) loop
7491 exit when Ekind (P_Name) = E_Package;
7492 P_Name := Homonym (P_Name);
7495 if Present (P_Name) then
7496 if not Is_Reference_In_Subunit then
7497 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
7499 ("package& is hidden by declaration#", N, P_Name);
7502 Set_Entity (Prefix (N), P_Name);
7503 Find_Expanded_Name (N);
7507 P_Name := Entity (Prefix (N));
7512 ("invalid prefix in selected component&", N, P_Name);
7513 Change_Selected_Component_To_Expanded_Name (N);
7514 Set_Entity (N, Any_Id);
7515 Set_Etype (N, Any_Type);
7517 -- Here we have a function call, so do the reformatting
7520 Nam := New_Copy (P);
7521 Save_Interps (P, Nam);
7523 -- We use Replace here because this is one of those cases
7524 -- where the parser has missclassified the node, and we fix
7525 -- things up and then do the semantic analysis on the fixed
7526 -- up node. Normally we do this using one of the Sinfo.CN
7527 -- routines, but this is too tricky for that.
7529 -- Note that using Rewrite would be wrong, because we would
7530 -- have a tree where the original node is unanalyzed, and
7531 -- this violates the required interface for ASIS.
7534 Make_Function_Call (Sloc (P), Name => Nam));
7536 -- Now analyze the reformatted node
7540 -- If the prefix is illegal after this transformation, there
7541 -- may be visibility errors on the prefix. The safest is to
7542 -- treat the selected component as an error.
7544 if Error_Posted (P) then
7545 Set_Etype (N, Any_Type);
7549 Analyze_Selected_Component (N);
7554 -- Remaining cases generate various error messages
7557 -- Format node as expanded name, to avoid cascaded errors
7559 -- If the limited_with transformation was applied earlier, restore
7560 -- source for proper error reporting.
7562 if not Comes_From_Source (P)
7563 and then Nkind (P) = N_Explicit_Dereference
7565 Rewrite (P, Prefix (P));
7566 P_Type := Etype (P);
7569 Change_Selected_Component_To_Expanded_Name (N);
7570 Set_Entity (N, Any_Id);
7571 Set_Etype (N, Any_Type);
7573 -- Issue error message, but avoid this if error issued already.
7574 -- Use identifier of prefix if one is available.
7576 if P_Name = Any_Id then
7579 -- It is not an error if the prefix is the current instance of
7580 -- type name, e.g. the expression of a type aspect, when it is
7581 -- analyzed for ASIS use, or within a generic unit. We still
7582 -- have to verify that a component of that name exists, and
7583 -- decorate the node accordingly.
7585 elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then
7590 Comp := First_Entity (Entity (P));
7591 while Present (Comp) loop
7592 if Chars (Comp) = Chars (Selector_Name (N)) then
7593 Set_Entity (N, Comp);
7594 Set_Etype (N, Etype (Comp));
7595 Set_Entity (Selector_Name (N), Comp);
7596 Set_Etype (Selector_Name (N), Etype (Comp));
7604 elsif Ekind (P_Name) = E_Void then
7605 Premature_Usage (P);
7607 elsif Nkind (P) /= N_Attribute_Reference then
7609 -- This may have been meant as a prefixed call to a primitive
7610 -- of an untagged type. If it is a function call check type of
7611 -- its first formal and add explanation.
7614 F : constant Entity_Id :=
7615 Current_Entity (Selector_Name (N));
7618 and then Is_Overloadable (F)
7619 and then Present (First_Entity (F))
7620 and then not Is_Tagged_Type (Etype (First_Entity (F)))
7623 ("prefixed call is only allowed for objects of a "
7624 & "tagged type", N);
7628 Error_Msg_N ("invalid prefix in selected component&", P);
7630 if Is_Access_Type (P_Type)
7631 and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
7634 ("\dereference must not be of an incomplete type "
7635 & "(RM 3.10.1)", P);
7639 Error_Msg_N ("invalid prefix in selected component", P);
7643 -- Selector name is restricted in SPARK
7645 if Nkind (N) = N_Expanded_Name
7646 and then Restriction_Check_Required (SPARK_05)
7648 if Is_Subprogram (P_Name) then
7649 Check_SPARK_05_Restriction
7650 ("prefix of expanded name cannot be a subprogram", P);
7651 elsif Ekind (P_Name) = E_Loop then
7652 Check_SPARK_05_Restriction
7653 ("prefix of expanded name cannot be a loop statement", P);
7658 -- If prefix is not the name of an entity, it must be an expression,
7659 -- whose type is appropriate for a record. This is determined by
7662 Analyze_Selected_Component (N);
7665 Analyze_Dimension (N);
7666 end Find_Selected_Component;
7672 procedure Find_Type (N : Node_Id) is
7682 elsif Nkind (N) = N_Attribute_Reference then
7684 -- Class attribute. This is not valid in Ada 83 mode, but we do not
7685 -- need to enforce that at this point, since the declaration of the
7686 -- tagged type in the prefix would have been flagged already.
7688 if Attribute_Name (N) = Name_Class then
7689 Check_Restriction (No_Dispatch, N);
7690 Find_Type (Prefix (N));
7692 -- Propagate error from bad prefix
7694 if Etype (Prefix (N)) = Any_Type then
7695 Set_Entity (N, Any_Type);
7696 Set_Etype (N, Any_Type);
7700 T := Base_Type (Entity (Prefix (N)));
7702 -- Case where type is not known to be tagged. Its appearance in
7703 -- the prefix of the 'Class attribute indicates that the full view
7706 if not Is_Tagged_Type (T) then
7707 if Ekind (T) = E_Incomplete_Type then
7709 -- It is legal to denote the class type of an incomplete
7710 -- type. The full type will have to be tagged, of course.
7711 -- In Ada 2005 this usage is declared obsolescent, so we
7712 -- warn accordingly. This usage is only legal if the type
7713 -- is completed in the current scope, and not for a limited
7716 if Ada_Version >= Ada_2005 then
7718 -- Test whether the Available_View of a limited type view
7719 -- is tagged, since the limited view may not be marked as
7720 -- tagged if the type itself has an untagged incomplete
7721 -- type view in its package.
7723 if From_Limited_With (T)
7724 and then not Is_Tagged_Type (Available_View (T))
7727 ("prefix of Class attribute must be tagged", N);
7728 Set_Etype (N, Any_Type);
7729 Set_Entity (N, Any_Type);
7732 -- ??? This test is temporarily disabled (always
7733 -- False) because it causes an unwanted warning on
7734 -- GNAT sources (built with -gnatg, which includes
7735 -- Warn_On_Obsolescent_ Feature). Once this issue
7736 -- is cleared in the sources, it can be enabled.
7738 elsif Warn_On_Obsolescent_Feature and then False then
7740 ("applying 'Class to an untagged incomplete type"
7741 & " is an obsolescent feature (RM J.11)?r?", N);
7745 Set_Is_Tagged_Type (T);
7746 Set_Direct_Primitive_Operations (T, New_Elmt_List);
7747 Make_Class_Wide_Type (T);
7748 Set_Entity (N, Class_Wide_Type (T));
7749 Set_Etype (N, Class_Wide_Type (T));
7751 elsif Ekind (T) = E_Private_Type
7752 and then not Is_Generic_Type (T)
7753 and then In_Private_Part (Scope (T))
7755 -- The Class attribute can be applied to an untagged private
7756 -- type fulfilled by a tagged type prior to the full type
7757 -- declaration (but only within the parent package's private
7758 -- part). Create the class-wide type now and check that the
7759 -- full type is tagged later during its analysis. Note that
7760 -- we do not mark the private type as tagged, unlike the
7761 -- case of incomplete types, because the type must still
7762 -- appear untagged to outside units.
7764 if No (Class_Wide_Type (T)) then
7765 Make_Class_Wide_Type (T);
7768 Set_Entity (N, Class_Wide_Type (T));
7769 Set_Etype (N, Class_Wide_Type (T));
7772 -- Should we introduce a type Any_Tagged and use Wrong_Type
7773 -- here, it would be a bit more consistent???
7776 ("tagged type required, found}",
7777 Prefix (N), First_Subtype (T));
7778 Set_Entity (N, Any_Type);
7782 -- Case of tagged type
7785 if Is_Concurrent_Type (T) then
7786 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
7788 -- Previous error. Create a class-wide type for the
7789 -- synchronized type itself, with minimal semantic
7790 -- attributes, to catch other errors in some ACATS tests.
7792 pragma Assert (Serious_Errors_Detected /= 0);
7793 Make_Class_Wide_Type (T);
7794 C := Class_Wide_Type (T);
7795 Set_First_Entity (C, First_Entity (T));
7798 C := Class_Wide_Type
7799 (Corresponding_Record_Type (Entity (Prefix (N))));
7803 C := Class_Wide_Type (Entity (Prefix (N)));
7806 Set_Entity_With_Checks (N, C);
7807 Generate_Reference (C, N);
7811 -- Base attribute, not allowed in Ada 83
7813 elsif Attribute_Name (N) = Name_Base then
7814 Error_Msg_Name_1 := Name_Base;
7815 Check_SPARK_05_Restriction
7816 ("attribute% is only allowed as prefix of another attribute", N);
7818 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
7820 ("(Ada 83) Base attribute not allowed in subtype mark", N);
7823 Find_Type (Prefix (N));
7824 Typ := Entity (Prefix (N));
7826 if Ada_Version >= Ada_95
7827 and then not Is_Scalar_Type (Typ)
7828 and then not Is_Generic_Type (Typ)
7831 ("prefix of Base attribute must be scalar type",
7834 elsif Warn_On_Redundant_Constructs
7835 and then Base_Type (Typ) = Typ
7837 Error_Msg_NE -- CODEFIX
7838 ("redundant attribute, & is its own base type?r?", N, Typ);
7841 T := Base_Type (Typ);
7843 -- Rewrite attribute reference with type itself (see similar
7844 -- processing in Analyze_Attribute, case Base). Preserve prefix
7845 -- if present, for other legality checks.
7847 if Nkind (Prefix (N)) = N_Expanded_Name then
7849 Make_Expanded_Name (Sloc (N),
7851 Prefix => New_Copy (Prefix (Prefix (N))),
7852 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
7855 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
7862 elsif Attribute_Name (N) = Name_Stub_Type then
7864 -- This is handled in Analyze_Attribute
7868 -- All other attributes are invalid in a subtype mark
7871 Error_Msg_N ("invalid attribute in subtype mark", N);
7877 if Is_Entity_Name (N) then
7878 T_Name := Entity (N);
7880 Error_Msg_N ("subtype mark required in this context", N);
7881 Set_Etype (N, Any_Type);
7885 if T_Name = Any_Id or else Etype (N) = Any_Type then
7887 -- Undefined id. Make it into a valid type
7889 Set_Entity (N, Any_Type);
7891 elsif not Is_Type (T_Name)
7892 and then T_Name /= Standard_Void_Type
7894 Error_Msg_Sloc := Sloc (T_Name);
7895 Error_Msg_N ("subtype mark required in this context", N);
7896 Error_Msg_NE ("\\found & declared#", N, T_Name);
7897 Set_Entity (N, Any_Type);
7900 -- If the type is an incomplete type created to handle
7901 -- anonymous access components of a record type, then the
7902 -- incomplete type is the visible entity and subsequent
7903 -- references will point to it. Mark the original full
7904 -- type as referenced, to prevent spurious warnings.
7906 if Is_Incomplete_Type (T_Name)
7907 and then Present (Full_View (T_Name))
7908 and then not Comes_From_Source (T_Name)
7910 Set_Referenced (Full_View (T_Name));
7913 T_Name := Get_Full_View (T_Name);
7915 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
7916 -- limited-with clauses
7918 if From_Limited_With (T_Name)
7919 and then Ekind (T_Name) in Incomplete_Kind
7920 and then Present (Non_Limited_View (T_Name))
7921 and then Is_Interface (Non_Limited_View (T_Name))
7923 T_Name := Non_Limited_View (T_Name);
7926 if In_Open_Scopes (T_Name) then
7927 if Ekind (Base_Type (T_Name)) = E_Task_Type then
7929 -- In Ada 2005, a task name can be used in an access
7930 -- definition within its own body. It cannot be used
7931 -- in the discriminant part of the task declaration,
7932 -- nor anywhere else in the declaration because entries
7933 -- cannot have access parameters.
7935 if Ada_Version >= Ada_2005
7936 and then Nkind (Parent (N)) = N_Access_Definition
7938 Set_Entity (N, T_Name);
7939 Set_Etype (N, T_Name);
7941 if Has_Completion (T_Name) then
7946 ("task type cannot be used as type mark " &
7947 "within its own declaration", N);
7952 ("task type cannot be used as type mark " &
7953 "within its own spec or body", N);
7956 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
7958 -- In Ada 2005, a protected name can be used in an access
7959 -- definition within its own body.
7961 if Ada_Version >= Ada_2005
7962 and then Nkind (Parent (N)) = N_Access_Definition
7964 Set_Entity (N, T_Name);
7965 Set_Etype (N, T_Name);
7970 ("protected type cannot be used as type mark " &
7971 "within its own spec or body", N);
7975 Error_Msg_N ("type declaration cannot refer to itself", N);
7978 Set_Etype (N, Any_Type);
7979 Set_Entity (N, Any_Type);
7980 Set_Error_Posted (T_Name);
7984 Set_Entity (N, T_Name);
7985 Set_Etype (N, T_Name);
7989 if Present (Etype (N)) and then Comes_From_Source (N) then
7990 if Is_Fixed_Point_Type (Etype (N)) then
7991 Check_Restriction (No_Fixed_Point, N);
7992 elsif Is_Floating_Point_Type (Etype (N)) then
7993 Check_Restriction (No_Floating_Point, N);
7996 -- A Ghost type must appear in a specific context
7998 if Is_Ghost_Entity (Etype (N)) then
7999 Check_Ghost_Context (Etype (N), N);
8004 ------------------------------------
8005 -- Has_Implicit_Character_Literal --
8006 ------------------------------------
8008 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
8010 Found : Boolean := False;
8011 P : constant Entity_Id := Entity (Prefix (N));
8012 Priv_Id : Entity_Id := Empty;
8015 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8016 Priv_Id := First_Private_Entity (P);
8019 if P = Standard_Standard then
8020 Change_Selected_Component_To_Expanded_Name (N);
8021 Rewrite (N, Selector_Name (N));
8023 Set_Etype (Original_Node (N), Standard_Character);
8027 Id := First_Entity (P);
8028 while Present (Id) and then Id /= Priv_Id loop
8029 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
8031 -- We replace the node with the literal itself, resolve as a
8032 -- character, and set the type correctly.
8035 Change_Selected_Component_To_Expanded_Name (N);
8036 Rewrite (N, Selector_Name (N));
8039 Set_Etype (Original_Node (N), Id);
8043 -- More than one type derived from Character in given scope.
8044 -- Collect all possible interpretations.
8046 Add_One_Interp (N, Id, Id);
8054 end Has_Implicit_Character_Literal;
8056 ----------------------
8057 -- Has_Private_With --
8058 ----------------------
8060 function Has_Private_With (E : Entity_Id) return Boolean is
8061 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
8065 Item := First (Context_Items (Comp_Unit));
8066 while Present (Item) loop
8067 if Nkind (Item) = N_With_Clause
8068 and then Private_Present (Item)
8069 and then Entity (Name (Item)) = E
8078 end Has_Private_With;
8080 ---------------------------
8081 -- Has_Implicit_Operator --
8082 ---------------------------
8084 function Has_Implicit_Operator (N : Node_Id) return Boolean is
8085 Op_Id : constant Name_Id := Chars (Selector_Name (N));
8086 P : constant Entity_Id := Entity (Prefix (N));
8088 Priv_Id : Entity_Id := Empty;
8090 procedure Add_Implicit_Operator
8092 Op_Type : Entity_Id := Empty);
8093 -- Add implicit interpretation to node N, using the type for which a
8094 -- predefined operator exists. If the operator yields a boolean type,
8095 -- the Operand_Type is implicitly referenced by the operator, and a
8096 -- reference to it must be generated.
8098 ---------------------------
8099 -- Add_Implicit_Operator --
8100 ---------------------------
8102 procedure Add_Implicit_Operator
8104 Op_Type : Entity_Id := Empty)
8106 Predef_Op : Entity_Id;
8109 Predef_Op := Current_Entity (Selector_Name (N));
8110 while Present (Predef_Op)
8111 and then Scope (Predef_Op) /= Standard_Standard
8113 Predef_Op := Homonym (Predef_Op);
8116 if Nkind (N) = N_Selected_Component then
8117 Change_Selected_Component_To_Expanded_Name (N);
8120 -- If the context is an unanalyzed function call, determine whether
8121 -- a binary or unary interpretation is required.
8123 if Nkind (Parent (N)) = N_Indexed_Component then
8125 Is_Binary_Call : constant Boolean :=
8127 (Next (First (Expressions (Parent (N)))));
8128 Is_Binary_Op : constant Boolean :=
8130 (Predef_Op) /= Last_Entity (Predef_Op);
8131 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
8134 if Is_Binary_Call then
8135 if Is_Binary_Op then
8136 Add_One_Interp (N, Predef_Op, T);
8138 Add_One_Interp (N, Predef_Op2, T);
8142 if not Is_Binary_Op then
8143 Add_One_Interp (N, Predef_Op, T);
8145 Add_One_Interp (N, Predef_Op2, T);
8151 Add_One_Interp (N, Predef_Op, T);
8153 -- For operators with unary and binary interpretations, if
8154 -- context is not a call, add both
8156 if Present (Homonym (Predef_Op)) then
8157 Add_One_Interp (N, Homonym (Predef_Op), T);
8161 -- The node is a reference to a predefined operator, and
8162 -- an implicit reference to the type of its operands.
8164 if Present (Op_Type) then
8165 Generate_Operator_Reference (N, Op_Type);
8167 Generate_Operator_Reference (N, T);
8169 end Add_Implicit_Operator;
8171 -- Start of processing for Has_Implicit_Operator
8174 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8175 Priv_Id := First_Private_Entity (P);
8178 Id := First_Entity (P);
8182 -- Boolean operators: an implicit declaration exists if the scope
8183 -- contains a declaration for a derived Boolean type, or for an
8184 -- array of Boolean type.
8191 while Id /= Priv_Id loop
8192 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
8193 Add_Implicit_Operator (Id);
8200 -- Equality: look for any non-limited type (result is Boolean)
8205 while Id /= Priv_Id loop
8207 and then not Is_Limited_Type (Id)
8208 and then Is_Base_Type (Id)
8210 Add_Implicit_Operator (Standard_Boolean, Id);
8217 -- Comparison operators: scalar type, or array of scalar
8224 while Id /= Priv_Id loop
8225 if (Is_Scalar_Type (Id)
8226 or else (Is_Array_Type (Id)
8227 and then Is_Scalar_Type (Component_Type (Id))))
8228 and then Is_Base_Type (Id)
8230 Add_Implicit_Operator (Standard_Boolean, Id);
8237 -- Arithmetic operators: any numeric type
8248 while Id /= Priv_Id loop
8249 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
8250 Add_Implicit_Operator (Id);
8257 -- Concatenation: any one-dimensional array type
8259 when Name_Op_Concat =>
8260 while Id /= Priv_Id loop
8261 if Is_Array_Type (Id)
8262 and then Number_Dimensions (Id) = 1
8263 and then Is_Base_Type (Id)
8265 Add_Implicit_Operator (Id);
8272 -- What is the others condition here? Should we be using a
8273 -- subtype of Name_Id that would restrict to operators ???
8279 -- If we fall through, then we do not have an implicit operator
8282 end Has_Implicit_Operator;
8284 -----------------------------------
8285 -- Has_Loop_In_Inner_Open_Scopes --
8286 -----------------------------------
8288 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
8290 -- Several scope stacks are maintained by Scope_Stack. The base of the
8291 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8292 -- flag in the scope stack entry. Note that the scope stacks used to
8293 -- simply be delimited implicitly by the presence of Standard_Standard
8294 -- at their base, but there now are cases where this is not sufficient
8295 -- because Standard_Standard actually may appear in the middle of the
8296 -- active set of scopes.
8298 for J in reverse 0 .. Scope_Stack.Last loop
8300 -- S was reached without seing a loop scope first
8302 if Scope_Stack.Table (J).Entity = S then
8305 -- S was not yet reached, so it contains at least one inner loop
8307 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
8311 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8312 -- cases where Standard_Standard appears in the middle of the active
8313 -- set of scopes. This affects the declaration and overriding of
8314 -- private inherited operations in instantiations of generic child
8317 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
8320 raise Program_Error; -- unreachable
8321 end Has_Loop_In_Inner_Open_Scopes;
8323 --------------------
8324 -- In_Open_Scopes --
8325 --------------------
8327 function In_Open_Scopes (S : Entity_Id) return Boolean is
8329 -- Several scope stacks are maintained by Scope_Stack. The base of the
8330 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8331 -- flag in the scope stack entry. Note that the scope stacks used to
8332 -- simply be delimited implicitly by the presence of Standard_Standard
8333 -- at their base, but there now are cases where this is not sufficient
8334 -- because Standard_Standard actually may appear in the middle of the
8335 -- active set of scopes.
8337 for J in reverse 0 .. Scope_Stack.Last loop
8338 if Scope_Stack.Table (J).Entity = S then
8342 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8343 -- cases where Standard_Standard appears in the middle of the active
8344 -- set of scopes. This affects the declaration and overriding of
8345 -- private inherited operations in instantiations of generic child
8348 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
8354 -----------------------------
8355 -- Inherit_Renamed_Profile --
8356 -----------------------------
8358 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
8365 if Ekind (Old_S) = E_Operator then
8366 New_F := First_Formal (New_S);
8368 while Present (New_F) loop
8369 Set_Etype (New_F, Base_Type (Etype (New_F)));
8370 Next_Formal (New_F);
8373 Set_Etype (New_S, Base_Type (Etype (New_S)));
8376 New_F := First_Formal (New_S);
8377 Old_F := First_Formal (Old_S);
8379 while Present (New_F) loop
8380 New_T := Etype (New_F);
8381 Old_T := Etype (Old_F);
8383 -- If the new type is a renaming of the old one, as is the case
8384 -- for actuals in instances, retain its name, to simplify later
8387 if Nkind (Parent (New_T)) = N_Subtype_Declaration
8388 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
8389 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
8393 Set_Etype (New_F, Old_T);
8396 Next_Formal (New_F);
8397 Next_Formal (Old_F);
8400 pragma Assert (No (Old_F));
8402 if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then
8403 Set_Etype (New_S, Etype (Old_S));
8406 end Inherit_Renamed_Profile;
8412 procedure Initialize is
8417 -------------------------
8418 -- Install_Use_Clauses --
8419 -------------------------
8421 procedure Install_Use_Clauses
8423 Force_Installation : Boolean := False)
8429 while Present (U) loop
8431 -- Case of USE package
8433 if Nkind (U) = N_Use_Package_Clause then
8434 Use_One_Package (U, Name (U), True);
8439 Use_One_Type (Subtype_Mark (U), Force => Force_Installation);
8443 Next_Use_Clause (U);
8445 end Install_Use_Clauses;
8447 -------------------------------------
8448 -- Is_Appropriate_For_Entry_Prefix --
8449 -------------------------------------
8451 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
8452 P_Type : Entity_Id := T;
8455 if Is_Access_Type (P_Type) then
8456 P_Type := Designated_Type (P_Type);
8459 return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
8460 end Is_Appropriate_For_Entry_Prefix;
8462 -------------------------------
8463 -- Is_Appropriate_For_Record --
8464 -------------------------------
8466 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is
8468 function Has_Components (T1 : Entity_Id) return Boolean;
8469 -- Determine if given type has components (i.e. is either a record
8470 -- type or a type that has discriminants).
8472 --------------------
8473 -- Has_Components --
8474 --------------------
8476 function Has_Components (T1 : Entity_Id) return Boolean is
8478 return Is_Record_Type (T1)
8479 or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
8480 or else (Is_Task_Type (T1) and then Has_Discriminants (T1))
8481 or else (Is_Incomplete_Type (T1)
8482 and then From_Limited_With (T1)
8483 and then Present (Non_Limited_View (T1))
8484 and then Is_Record_Type
8485 (Get_Full_View (Non_Limited_View (T1))));
8488 -- Start of processing for Is_Appropriate_For_Record
8493 and then (Has_Components (T)
8494 or else (Is_Access_Type (T)
8495 and then Has_Components (Designated_Type (T))));
8496 end Is_Appropriate_For_Record;
8498 ----------------------
8499 -- Mark_Use_Clauses --
8500 ----------------------
8502 procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is
8503 procedure Mark_Parameters (Call : Entity_Id);
8504 -- Perform use_type_clause marking for all parameters in a subprogram
8505 -- or operator call.
8507 procedure Mark_Use_Package (Pak : Entity_Id);
8508 -- Move up the Prev_Use_Clause chain for packages denoted by Pak -
8509 -- marking each clause in the chain as effective in the process.
8511 procedure Mark_Use_Type (E : Entity_Id);
8512 -- Similar to Do_Use_Package_Marking except we move up the
8513 -- Prev_Use_Clause chain for the type denoted by E.
8515 ---------------------
8516 -- Mark_Parameters --
8517 ---------------------
8519 procedure Mark_Parameters (Call : Entity_Id) is
8523 -- Move through all of the formals
8525 Curr := First_Formal (Call);
8526 while Present (Curr) loop
8527 Mark_Use_Type (Curr);
8529 Curr := Next_Formal (Curr);
8532 -- Handle the return type
8534 Mark_Use_Type (Call);
8535 end Mark_Parameters;
8537 ----------------------
8538 -- Mark_Use_Package --
8539 ----------------------
8541 procedure Mark_Use_Package (Pak : Entity_Id) is
8545 -- Ignore cases where the scope of the type is not a package (e.g.
8546 -- Standard_Standard).
8548 if Ekind (Pak) /= E_Package then
8552 Curr := Current_Use_Clause (Pak);
8553 while Present (Curr)
8554 and then not Is_Effective_Use_Clause (Curr)
8556 -- We need to mark the previous use clauses as effective, but
8557 -- each use clause may in turn render other use_package_clauses
8558 -- effective. Additionally, it is possible to have a parent
8559 -- package renamed as a child of itself so we must check the
8560 -- prefix entity is not the same as the package we are marking.
8562 if Nkind (Name (Curr)) /= N_Identifier
8563 and then Present (Prefix (Name (Curr)))
8564 and then Entity (Prefix (Name (Curr))) /= Pak
8566 Mark_Use_Package (Entity (Prefix (Name (Curr))));
8568 -- It is also possible to have a child package without a prefix
8569 -- that relies on a previous use_package_clause.
8571 elsif Nkind (Name (Curr)) = N_Identifier
8572 and then Is_Child_Unit (Entity (Name (Curr)))
8574 Mark_Use_Package (Scope (Entity (Name (Curr))));
8577 -- Mark the use_package_clause as effective and move up the chain
8579 Set_Is_Effective_Use_Clause (Curr);
8581 Curr := Prev_Use_Clause (Curr);
8583 end Mark_Use_Package;
8589 procedure Mark_Use_Type (E : Entity_Id) is
8594 -- Ignore void types and unresolved string literals and primitives
8596 if Nkind (E) = N_String_Literal
8597 or else Nkind (Etype (E)) not in N_Entity
8598 or else not Is_Type (Etype (E))
8603 -- Primitives with class-wide operands might additionally render
8604 -- their base type's use_clauses effective - so do a recursive check
8607 Base := Base_Type (Etype (E));
8609 if Ekind (Base) = E_Class_Wide_Type then
8610 Mark_Use_Type (Base);
8613 -- The package containing the type or operator function being used
8614 -- may be in use as well, so mark any use_package_clauses for it as
8615 -- effective. There are also additional sanity checks performed here
8616 -- for ignoring previous errors.
8618 Mark_Use_Package (Scope (Base));
8620 if Nkind (E) in N_Op
8621 and then Present (Entity (E))
8622 and then Present (Scope (Entity (E)))
8624 Mark_Use_Package (Scope (Entity (E)));
8627 Curr := Current_Use_Clause (Base);
8628 while Present (Curr)
8629 and then not Is_Effective_Use_Clause (Curr)
8631 -- Current use_type_clause may render other use_package_clauses
8634 if Nkind (Subtype_Mark (Curr)) /= N_Identifier
8635 and then Present (Prefix (Subtype_Mark (Curr)))
8637 Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr))));
8640 -- Mark the use_type_clause as effective and move up the chain
8642 Set_Is_Effective_Use_Clause (Curr);
8644 Curr := Prev_Use_Clause (Curr);
8648 -- Start of processing for Mark_Use_Clauses
8651 -- Use clauses in and of themselves do not count as a "use" of a
8654 if Nkind_In (Parent (Id), N_Use_Package_Clause, N_Use_Type_Clause) then
8660 if Nkind (Id) in N_Entity then
8662 -- Mark the entity's package
8664 if Is_Potentially_Use_Visible (Id) then
8665 Mark_Use_Package (Scope (Id));
8668 -- Mark enumeration literals
8670 if Ekind (Id) = E_Enumeration_Literal then
8675 elsif (Ekind (Id) in Overloadable_Kind
8676 or else Ekind_In (Id, E_Generic_Function,
8677 E_Generic_Procedure))
8678 and then (Is_Potentially_Use_Visible (Id)
8679 or else Is_Intrinsic_Subprogram (Id)
8680 or else (Ekind_In (Id, E_Function, E_Procedure)
8681 and then Is_Generic_Actual_Subprogram (Id)))
8683 Mark_Parameters (Id);
8691 if Nkind (Id) in N_Op then
8693 -- At this point the left operand may not be resolved if we are
8694 -- encountering multiple operators next to eachother in an
8697 if Nkind (Id) in N_Binary_Op
8698 and then not (Nkind (Left_Opnd (Id)) in N_Op)
8700 Mark_Use_Type (Left_Opnd (Id));
8703 Mark_Use_Type (Right_Opnd (Id));
8706 -- Mark entity identifiers
8708 elsif Nkind (Id) in N_Has_Entity
8709 and then (Is_Potentially_Use_Visible (Entity (Id))
8710 or else (Is_Generic_Instance (Entity (Id))
8711 and then Is_Immediately_Visible (Entity (Id))))
8713 -- Ignore fully qualified names as they do not count as a "use" of
8716 if Nkind_In (Id, N_Identifier, N_Operator_Symbol)
8717 or else (Present (Prefix (Id))
8718 and then Scope (Entity (Id)) /= Entity (Prefix (Id)))
8720 Mark_Use_Clauses (Entity (Id));
8724 end Mark_Use_Clauses;
8726 --------------------------------
8727 -- Most_Descendant_Use_Clause --
8728 --------------------------------
8730 function Most_Descendant_Use_Clause
8731 (Clause1 : Entity_Id;
8732 Clause2 : Entity_Id) return Entity_Id
8738 if Clause1 = Clause2 then
8742 -- We determine which one is the most descendant by the scope distance
8743 -- to the ultimate parent unit.
8745 Scope1 := Entity_Of_Unit (Unit (Parent (Clause1)));
8746 Scope2 := Entity_Of_Unit (Unit (Parent (Clause2)));
8747 while Scope1 /= Standard_Standard
8748 and then Scope2 /= Standard_Standard
8750 Scope1 := Scope (Scope1);
8751 Scope2 := Scope (Scope2);
8753 if not Present (Scope1) then
8755 elsif not Present (Scope2) then
8760 if Scope1 = Standard_Standard then
8765 end Most_Descendant_Use_Clause;
8771 procedure Pop_Scope is
8772 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8773 S : constant Entity_Id := SST.Entity;
8776 if Debug_Flag_E then
8780 -- Set Default_Storage_Pool field of the library unit if necessary
8782 if Ekind_In (S, E_Package, E_Generic_Package)
8784 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
8787 Aux : constant Node_Id :=
8788 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
8790 if No (Default_Storage_Pool (Aux)) then
8791 Set_Default_Storage_Pool (Aux, Default_Pool);
8796 Scope_Suppress := SST.Save_Scope_Suppress;
8797 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
8798 Check_Policy_List := SST.Save_Check_Policy_List;
8799 Default_Pool := SST.Save_Default_Storage_Pool;
8800 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
8801 SPARK_Mode := SST.Save_SPARK_Mode;
8802 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
8803 Default_SSO := SST.Save_Default_SSO;
8804 Uneval_Old := SST.Save_Uneval_Old;
8806 if Debug_Flag_W then
8807 Write_Str ("<-- exiting scope: ");
8808 Write_Name (Chars (Current_Scope));
8809 Write_Str (", Depth=");
8810 Write_Int (Int (Scope_Stack.Last));
8814 End_Use_Clauses (SST.First_Use_Clause);
8816 -- If the actions to be wrapped are still there they will get lost
8817 -- causing incomplete code to be generated. It is better to abort in
8818 -- this case (and we do the abort even with assertions off since the
8819 -- penalty is incorrect code generation).
8821 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
8822 raise Program_Error;
8825 -- Free last subprogram name if allocated, and pop scope
8827 Free (SST.Last_Subprogram_Name);
8828 Scope_Stack.Decrement_Last;
8835 procedure Push_Scope (S : Entity_Id) is
8836 E : constant Entity_Id := Scope (S);
8839 if Ekind (S) = E_Void then
8842 -- Set scope depth if not a non-concurrent type, and we have not yet set
8843 -- the scope depth. This means that we have the first occurrence of the
8844 -- scope, and this is where the depth is set.
8846 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
8847 and then not Scope_Depth_Set (S)
8849 if S = Standard_Standard then
8850 Set_Scope_Depth_Value (S, Uint_0);
8852 elsif Is_Child_Unit (S) then
8853 Set_Scope_Depth_Value (S, Uint_1);
8855 elsif not Is_Record_Type (Current_Scope) then
8856 if Ekind (S) = E_Loop then
8857 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
8859 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
8864 Scope_Stack.Increment_Last;
8867 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8871 SST.Save_Scope_Suppress := Scope_Suppress;
8872 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
8873 SST.Save_Check_Policy_List := Check_Policy_List;
8874 SST.Save_Default_Storage_Pool := Default_Pool;
8875 SST.Save_No_Tagged_Streams := No_Tagged_Streams;
8876 SST.Save_SPARK_Mode := SPARK_Mode;
8877 SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma;
8878 SST.Save_Default_SSO := Default_SSO;
8879 SST.Save_Uneval_Old := Uneval_Old;
8881 -- Each new scope pushed onto the scope stack inherits the component
8882 -- alignment of the previous scope. This emulates the "visibility"
8883 -- semantics of pragma Component_Alignment.
8885 if Scope_Stack.Last > Scope_Stack.First then
8886 SST.Component_Alignment_Default :=
8888 (Scope_Stack.Last - 1).Component_Alignment_Default;
8890 -- Otherwise, this is the first scope being pushed on the scope
8891 -- stack. Inherit the component alignment from the configuration
8892 -- form of pragma Component_Alignment (if any).
8895 SST.Component_Alignment_Default :=
8896 Configuration_Component_Alignment;
8899 SST.Last_Subprogram_Name := null;
8900 SST.Is_Transient := False;
8901 SST.Node_To_Be_Wrapped := Empty;
8902 SST.Pending_Freeze_Actions := No_List;
8903 SST.Actions_To_Be_Wrapped := (others => No_List);
8904 SST.First_Use_Clause := Empty;
8905 SST.Is_Active_Stack_Base := False;
8906 SST.Previous_Visibility := False;
8907 SST.Locked_Shared_Objects := No_Elist;
8910 if Debug_Flag_W then
8911 Write_Str ("--> new scope: ");
8912 Write_Name (Chars (Current_Scope));
8913 Write_Str (", Id=");
8914 Write_Int (Int (Current_Scope));
8915 Write_Str (", Depth=");
8916 Write_Int (Int (Scope_Stack.Last));
8920 -- Deal with copying flags from the previous scope to this one. This is
8921 -- not necessary if either scope is standard, or if the new scope is a
8924 if S /= Standard_Standard
8925 and then Scope (S) /= Standard_Standard
8926 and then not Is_Child_Unit (S)
8928 if Nkind (E) not in N_Entity then
8932 -- Copy categorization flags from Scope (S) to S, this is not done
8933 -- when Scope (S) is Standard_Standard since propagation is from
8934 -- library unit entity inwards. Copy other relevant attributes as
8935 -- well (Discard_Names in particular).
8937 -- We only propagate inwards for library level entities,
8938 -- inner level subprograms do not inherit the categorization.
8940 if Is_Library_Level_Entity (S) then
8941 Set_Is_Preelaborated (S, Is_Preelaborated (E));
8942 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
8943 Set_Discard_Names (S, Discard_Names (E));
8944 Set_Suppress_Value_Tracking_On_Call
8945 (S, Suppress_Value_Tracking_On_Call (E));
8946 Set_Categorization_From_Scope (E => S, Scop => E);
8950 if Is_Child_Unit (S)
8951 and then Present (E)
8952 and then Ekind_In (E, E_Package, E_Generic_Package)
8954 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
8957 Aux : constant Node_Id :=
8958 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
8960 if Present (Default_Storage_Pool (Aux)) then
8961 Default_Pool := Default_Storage_Pool (Aux);
8967 ---------------------
8968 -- Premature_Usage --
8969 ---------------------
8971 procedure Premature_Usage (N : Node_Id) is
8972 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
8973 E : Entity_Id := Entity (N);
8976 -- Within an instance, the analysis of the actual for a formal object
8977 -- does not see the name of the object itself. This is significant only
8978 -- if the object is an aggregate, where its analysis does not do any
8979 -- name resolution on component associations. (see 4717-008). In such a
8980 -- case, look for the visible homonym on the chain.
8982 if In_Instance and then Present (Homonym (E)) then
8984 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
8990 Set_Etype (N, Etype (E));
8995 if Kind = N_Component_Declaration then
8997 ("component&! cannot be used before end of record declaration", N);
8999 elsif Kind = N_Parameter_Specification then
9001 ("formal parameter&! cannot be used before end of specification",
9004 elsif Kind = N_Discriminant_Specification then
9006 ("discriminant&! cannot be used before end of discriminant part",
9009 elsif Kind = N_Procedure_Specification
9010 or else Kind = N_Function_Specification
9013 ("subprogram&! cannot be used before end of its declaration",
9016 elsif Kind = N_Full_Type_Declaration then
9018 ("type& cannot be used before end of its declaration!", N);
9022 ("object& cannot be used before end of its declaration!", N);
9024 -- If the premature reference appears as the expression in its own
9025 -- declaration, rewrite it to prevent compiler loops in subsequent
9026 -- uses of this mangled declaration in address clauses.
9028 if Nkind (Parent (N)) = N_Object_Declaration then
9029 Set_Entity (N, Any_Id);
9032 end Premature_Usage;
9034 ------------------------
9035 -- Present_System_Aux --
9036 ------------------------
9038 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
9040 Aux_Name : Unit_Name_Type;
9041 Unum : Unit_Number_Type;
9046 function Find_System (C_Unit : Node_Id) return Entity_Id;
9047 -- Scan context clause of compilation unit to find with_clause
9054 function Find_System (C_Unit : Node_Id) return Entity_Id is
9055 With_Clause : Node_Id;
9058 With_Clause := First (Context_Items (C_Unit));
9059 while Present (With_Clause) loop
9060 if (Nkind (With_Clause) = N_With_Clause
9061 and then Chars (Name (With_Clause)) = Name_System)
9062 and then Comes_From_Source (With_Clause)
9073 -- Start of processing for Present_System_Aux
9076 -- The child unit may have been loaded and analyzed already
9078 if Present (System_Aux_Id) then
9081 -- If no previous pragma for System.Aux, nothing to load
9083 elsif No (System_Extend_Unit) then
9086 -- Use the unit name given in the pragma to retrieve the unit.
9087 -- Verify that System itself appears in the context clause of the
9088 -- current compilation. If System is not present, an error will
9089 -- have been reported already.
9092 With_Sys := Find_System (Cunit (Current_Sem_Unit));
9094 The_Unit := Unit (Cunit (Current_Sem_Unit));
9098 (Nkind (The_Unit) = N_Package_Body
9099 or else (Nkind (The_Unit) = N_Subprogram_Body
9100 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
9102 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
9105 if No (With_Sys) and then Present (N) then
9107 -- If we are compiling a subunit, we need to examine its
9108 -- context as well (Current_Sem_Unit is the parent unit);
9110 The_Unit := Parent (N);
9111 while Nkind (The_Unit) /= N_Compilation_Unit loop
9112 The_Unit := Parent (The_Unit);
9115 if Nkind (Unit (The_Unit)) = N_Subunit then
9116 With_Sys := Find_System (The_Unit);
9120 if No (With_Sys) then
9124 Loc := Sloc (With_Sys);
9125 Get_Name_String (Chars (Expression (System_Extend_Unit)));
9126 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
9127 Name_Buffer (1 .. 7) := "system.";
9128 Name_Buffer (Name_Len + 8) := '%';
9129 Name_Buffer (Name_Len + 9) := 's';
9130 Name_Len := Name_Len + 9;
9131 Aux_Name := Name_Find;
9135 (Load_Name => Aux_Name,
9138 Error_Node => With_Sys);
9140 if Unum /= No_Unit then
9141 Semantics (Cunit (Unum));
9143 Defining_Entity (Specification (Unit (Cunit (Unum))));
9146 Make_With_Clause (Loc,
9148 Make_Expanded_Name (Loc,
9149 Chars => Chars (System_Aux_Id),
9151 New_Occurrence_Of (Scope (System_Aux_Id), Loc),
9152 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
9154 Set_Entity (Name (Withn), System_Aux_Id);
9156 Set_Corresponding_Spec (Withn, System_Aux_Id);
9157 Set_First_Name (Withn);
9158 Set_Implicit_With (Withn);
9159 Set_Library_Unit (Withn, Cunit (Unum));
9161 Insert_After (With_Sys, Withn);
9162 Mark_Rewrite_Insertion (Withn);
9163 Set_Context_Installed (Withn);
9167 -- Here if unit load failed
9170 Error_Msg_Name_1 := Name_System;
9171 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
9173 ("extension package `%.%` does not exist",
9174 Opt.System_Extend_Unit);
9178 end Present_System_Aux;
9180 -------------------------
9181 -- Restore_Scope_Stack --
9182 -------------------------
9184 procedure Restore_Scope_Stack
9186 Handle_Use : Boolean := True)
9188 SS_Last : constant Int := Scope_Stack.Last;
9192 -- Restore visibility of previous scope stack, if any, using the list
9193 -- we saved (we use Remove, since this list will not be used again).
9196 Elmt := Last_Elmt (List);
9197 exit when Elmt = No_Elmt;
9198 Set_Is_Immediately_Visible (Node (Elmt));
9199 Remove_Last_Elmt (List);
9202 -- Restore use clauses
9204 if SS_Last >= Scope_Stack.First
9205 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9209 (Scope_Stack.Table (SS_Last).First_Use_Clause,
9210 Force_Installation => True);
9212 end Restore_Scope_Stack;
9214 ----------------------
9215 -- Save_Scope_Stack --
9216 ----------------------
9218 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
9219 -- consuming any memory. That is, Save_Scope_Stack took care of removing
9220 -- from immediate visibility entities and Restore_Scope_Stack took care
9221 -- of restoring their visibility analyzing the context of each entity. The
9222 -- problem of such approach is that it was fragile and caused unexpected
9223 -- visibility problems, and indeed one test was found where there was a
9226 -- Furthermore, the following experiment was carried out:
9228 -- - Save_Scope_Stack was modified to store in an Elist1 all those
9229 -- entities whose attribute Is_Immediately_Visible is modified
9230 -- from True to False.
9232 -- - Restore_Scope_Stack was modified to store in another Elist2
9233 -- all the entities whose attribute Is_Immediately_Visible is
9234 -- modified from False to True.
9236 -- - Extra code was added to verify that all the elements of Elist1
9237 -- are found in Elist2
9239 -- This test shows that there may be more occurrences of this problem which
9240 -- have not yet been detected. As a result, we replaced that approach by
9241 -- the current one in which Save_Scope_Stack returns the list of entities
9242 -- whose visibility is changed, and that list is passed to Restore_Scope_
9243 -- Stack to undo that change. This approach is simpler and safer, although
9244 -- it consumes more memory.
9246 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
9247 Result : constant Elist_Id := New_Elmt_List;
9250 SS_Last : constant Int := Scope_Stack.Last;
9252 procedure Remove_From_Visibility (E : Entity_Id);
9253 -- If E is immediately visible then append it to the result and remove
9254 -- it temporarily from visibility.
9256 ----------------------------
9257 -- Remove_From_Visibility --
9258 ----------------------------
9260 procedure Remove_From_Visibility (E : Entity_Id) is
9262 if Is_Immediately_Visible (E) then
9263 Append_Elmt (E, Result);
9264 Set_Is_Immediately_Visible (E, False);
9266 end Remove_From_Visibility;
9268 -- Start of processing for Save_Scope_Stack
9271 if SS_Last >= Scope_Stack.First
9272 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9275 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
9278 -- If the call is from within a compilation unit, as when called from
9279 -- Rtsfind, make current entries in scope stack invisible while we
9280 -- analyze the new unit.
9282 for J in reverse 0 .. SS_Last loop
9283 exit when Scope_Stack.Table (J).Entity = Standard_Standard
9284 or else No (Scope_Stack.Table (J).Entity);
9286 S := Scope_Stack.Table (J).Entity;
9288 Remove_From_Visibility (S);
9290 E := First_Entity (S);
9291 while Present (E) loop
9292 Remove_From_Visibility (E);
9300 end Save_Scope_Stack;
9306 procedure Set_Use (L : List_Id) is
9312 while Present (Decl) loop
9313 if Nkind (Decl) = N_Use_Package_Clause then
9314 Chain_Use_Clause (Decl);
9315 Use_One_Package (Decl, Name (Decl));
9317 elsif Nkind (Decl) = N_Use_Type_Clause then
9318 Chain_Use_Clause (Decl);
9319 Use_One_Type (Subtype_Mark (Decl));
9328 -----------------------------
9329 -- Update_Use_Clause_Chain --
9330 -----------------------------
9332 procedure Update_Use_Clause_Chain is
9334 procedure Update_Chain_In_Scope (Level : Int);
9335 -- Iterate through one level in the scope stack verifying each use-type
9336 -- clause within said level is used then reset the Current_Use_Clause
9337 -- to a redundant use clause outside of the current ending scope if such
9340 ---------------------------
9341 -- Update_Chain_In_Scope --
9342 ---------------------------
9344 procedure Update_Chain_In_Scope (Level : Int) is
9349 -- Loop through all use clauses within the scope dictated by Level
9351 Curr := Scope_Stack.Table (Level).First_Use_Clause;
9352 while Present (Curr) loop
9354 -- Retrieve the subtype mark or name within the current current
9357 if Nkind (Curr) = N_Use_Type_Clause then
9358 N := Subtype_Mark (Curr);
9363 -- If warnings for unreferenced entities are enabled and the
9364 -- current use clause has not been marked effective.
9366 if Check_Unreferenced
9367 and then Comes_From_Source (Curr)
9368 and then not Is_Effective_Use_Clause (Curr)
9369 and then not In_Instance
9370 and then not In_Inlined_Body
9372 -- We are dealing with a potentially unused use_package_clause
9374 if Nkind (Curr) = N_Use_Package_Clause then
9376 -- Renamings and formal subprograms may cause the associated
9377 -- node to be marked as effective instead of the original.
9379 if not (Present (Associated_Node (N))
9382 (Associated_Node (N)))
9383 and then Is_Effective_Use_Clause
9385 (Associated_Node (N))))
9387 Error_Msg_Node_1 := Entity (N);
9389 ("use clause for package & has no effect?u?",
9393 -- We are dealing with an unused use_type_clause
9396 Error_Msg_Node_1 := Etype (N);
9398 ("use clause for } has no effect?u?", Curr, Etype (N));
9402 -- Verify that we haven't already processed a redundant
9403 -- use_type_clause within the same scope before we move the
9404 -- current use clause up to a previous one for type T.
9406 if Present (Prev_Use_Clause (Curr)) then
9407 Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr));
9410 Curr := Next_Use_Clause (Curr);
9412 end Update_Chain_In_Scope;
9414 -- Start of processing for Update_Use_Clause_Chain
9417 Update_Chain_In_Scope (Scope_Stack.Last);
9419 -- Deal with use clauses within the context area if the current
9420 -- scope is a compilation unit.
9422 if Is_Compilation_Unit (Current_Scope)
9423 and then Sloc (Scope_Stack.Table
9424 (Scope_Stack.Last - 1).Entity) = Standard_Location
9426 Update_Chain_In_Scope (Scope_Stack.Last - 1);
9428 end Update_Use_Clause_Chain;
9430 ---------------------
9431 -- Use_One_Package --
9432 ---------------------
9434 procedure Use_One_Package
9436 Pack_Name : Entity_Id := Empty;
9437 Force : Boolean := False)
9439 procedure Note_Redundant_Use (Clause : Node_Id);
9440 -- Mark the name in a use clause as redundant if the corresponding
9441 -- entity is already use-visible. Emit a warning if the use clause comes
9442 -- from source and the proper warnings are enabled.
9444 ------------------------
9445 -- Note_Redundant_Use --
9446 ------------------------
9448 procedure Note_Redundant_Use (Clause : Node_Id) is
9449 Decl : constant Node_Id := Parent (Clause);
9450 Pack_Name : constant Entity_Id := Entity (Clause);
9452 Cur_Use : Node_Id := Current_Use_Clause (Pack_Name);
9453 Prev_Use : Node_Id := Empty;
9454 Redundant : Node_Id := Empty;
9455 -- The Use_Clause which is actually redundant. In the simplest case
9456 -- it is Pack itself, but when we compile a body we install its
9457 -- context before that of its spec, in which case it is the
9458 -- use_clause in the spec that will appear to be redundant, and we
9459 -- want the warning to be placed on the body. Similar complications
9460 -- appear when the redundancy is between a child unit and one of its
9464 -- Could be renamed...
9466 if No (Cur_Use) then
9467 Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name));
9470 Set_Redundant_Use (Clause, True);
9472 if not Comes_From_Source (Clause)
9474 or else not Warn_On_Redundant_Constructs
9479 if not Is_Compilation_Unit (Current_Scope) then
9481 -- If the use_clause is in an inner scope, it is made redundant by
9482 -- some clause in the current context, with one exception: If we
9483 -- are compiling a nested package body, and the use_clause comes
9484 -- from then corresponding spec, the clause is not necessarily
9485 -- fully redundant, so we should not warn. If a warning was
9486 -- warranted, it would have been given when the spec was
9489 if Nkind (Parent (Decl)) = N_Package_Specification then
9491 Package_Spec_Entity : constant Entity_Id :=
9492 Defining_Unit_Name (Parent (Decl));
9494 if In_Package_Body (Package_Spec_Entity) then
9500 Redundant := Clause;
9501 Prev_Use := Cur_Use;
9503 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9505 Cur_Unit : constant Unit_Number_Type :=
9506 Get_Source_Unit (Cur_Use);
9507 New_Unit : constant Unit_Number_Type :=
9508 Get_Source_Unit (Clause);
9513 if Cur_Unit = New_Unit then
9515 -- Redundant clause in same body
9517 Redundant := Clause;
9518 Prev_Use := Cur_Use;
9520 elsif Cur_Unit = Current_Sem_Unit then
9522 -- If the new clause is not in the current unit it has been
9523 -- analyzed first, and it makes the other one redundant.
9524 -- However, if the new clause appears in a subunit, Cur_Unit
9525 -- is still the parent, and in that case the redundant one
9526 -- is the one appearing in the subunit.
9528 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
9529 Redundant := Clause;
9530 Prev_Use := Cur_Use;
9532 -- Most common case: redundant clause in body, original
9533 -- clause in spec. Current scope is spec entity.
9535 elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then
9536 Redundant := Cur_Use;
9540 -- The new clause may appear in an unrelated unit, when
9541 -- the parents of a generic are being installed prior to
9542 -- instantiation. In this case there must be no warning.
9543 -- We detect this case by checking whether the current
9544 -- top of the stack is related to the current
9547 Scop := Current_Scope;
9548 while Present (Scop)
9549 and then Scop /= Standard_Standard
9551 if Is_Compilation_Unit (Scop)
9552 and then not Is_Child_Unit (Scop)
9556 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
9560 Scop := Scope (Scop);
9563 Redundant := Cur_Use;
9567 elsif New_Unit = Current_Sem_Unit then
9568 Redundant := Clause;
9569 Prev_Use := Cur_Use;
9572 -- Neither is the current unit, so they appear in parent or
9573 -- sibling units. Warning will be emitted elsewhere.
9579 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
9580 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
9582 -- Use_clause is in child unit of current unit, and the child unit
9583 -- appears in the context of the body of the parent, so it has
9584 -- been installed first, even though it is the redundant one.
9585 -- Depending on their placement in the context, the visible or the
9586 -- private parts of the two units, either might appear as
9587 -- redundant, but the message has to be on the current unit.
9589 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
9590 Redundant := Cur_Use;
9593 Redundant := Clause;
9594 Prev_Use := Cur_Use;
9597 -- If the new use clause appears in the private part of a parent
9598 -- unit it may appear to be redundant w.r.t. a use clause in a
9599 -- child unit, but the previous use clause was needed in the
9600 -- visible part of the child, and no warning should be emitted.
9602 if Nkind (Parent (Decl)) = N_Package_Specification
9603 and then List_Containing (Decl) =
9604 Private_Declarations (Parent (Decl))
9607 Par : constant Entity_Id := Defining_Entity (Parent (Decl));
9608 Spec : constant Node_Id :=
9609 Specification (Unit (Cunit (Current_Sem_Unit)));
9612 if Is_Compilation_Unit (Par)
9613 and then Par /= Cunit_Entity (Current_Sem_Unit)
9614 and then Parent (Cur_Use) = Spec
9615 and then List_Containing (Cur_Use) =
9616 Visible_Declarations (Spec)
9623 -- Finally, if the current use clause is in the context then the
9624 -- clause is redundant when it is nested within the unit.
9626 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
9627 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
9628 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
9630 Redundant := Clause;
9631 Prev_Use := Cur_Use;
9635 if Present (Redundant) and then Parent (Redundant) /= Prev_Use then
9637 -- Make sure we are looking at most-descendant use_package_clause
9638 -- by traversing the chain with Find_Most_Prev and then verifying
9639 -- there is no scope manipulation via Most_Descendant_Use_Clause.
9641 if Nkind (Prev_Use) = N_Use_Package_Clause
9643 (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit
9644 or else Most_Descendant_Use_Clause
9645 (Prev_Use, Find_Most_Prev (Prev_Use)) /= Prev_Use)
9647 Prev_Use := Find_Most_Prev (Prev_Use);
9650 Error_Msg_Sloc := Sloc (Prev_Use);
9651 Error_Msg_NE -- CODEFIX
9652 ("& is already use-visible through previous use_clause #??",
9653 Redundant, Pack_Name);
9655 end Note_Redundant_Use;
9659 Current_Instance : Entity_Id := Empty;
9663 Private_With_OK : Boolean := False;
9666 -- Start of processing for Use_One_Package
9669 -- Use_One_Package may have been called recursively to handle an
9670 -- implicit use for a auxiliary system package, so set P accordingly
9671 -- and skip redundancy checks.
9673 if No (Pack_Name) and then Present_System_Aux (N) then
9676 -- Check for redundant use_package_clauses
9679 -- Ignore cases where we are dealing with a non user defined package
9680 -- like Standard_Standard or something other than a valid package.
9682 if not Is_Entity_Name (Pack_Name)
9683 or else No (Entity (Pack_Name))
9684 or else Ekind (Entity (Pack_Name)) /= E_Package
9689 -- When a renaming exists we must check it for redundancy. The
9690 -- original package would have already been seen at this point.
9692 if Present (Renamed_Object (Entity (Pack_Name))) then
9693 P := Renamed_Object (Entity (Pack_Name));
9695 P := Entity (Pack_Name);
9698 -- Check for redundant clauses then set the current use clause for
9699 -- P if were are not "forcing" an installation from a scope
9700 -- reinstallation that is done throughout analysis for various
9704 Note_Redundant_Use (Pack_Name);
9707 Set_Current_Use_Clause (P, N);
9712 -- Warn about detected redundant clauses
9715 and then In_Open_Scopes (P)
9716 and then not Is_Hidden_Open_Scope (P)
9718 if Warn_On_Redundant_Constructs and then P = Current_Scope then
9719 Error_Msg_NE -- CODEFIX
9720 ("& is already use-visible within itself?r?",
9727 -- Set P back to the non-renamed package so that visiblilty of the
9728 -- entities within the package can be properly set below.
9730 P := Entity (Pack_Name);
9734 Set_Current_Use_Clause (P, N);
9736 -- Ada 2005 (AI-50217): Check restriction
9738 if From_Limited_With (P) then
9739 Error_Msg_N ("limited withed package cannot appear in use clause", N);
9742 -- Find enclosing instance, if any
9745 Current_Instance := Current_Scope;
9746 while not Is_Generic_Instance (Current_Instance) loop
9747 Current_Instance := Scope (Current_Instance);
9750 if No (Hidden_By_Use_Clause (N)) then
9751 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
9755 -- If unit is a package renaming, indicate that the renamed package is
9756 -- also in use (the flags on both entities must remain consistent, and a
9757 -- subsequent use of either of them should be recognized as redundant).
9759 if Present (Renamed_Object (P)) then
9760 Set_In_Use (Renamed_Object (P));
9761 Set_Current_Use_Clause (Renamed_Object (P), N);
9762 Real_P := Renamed_Object (P);
9767 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
9768 -- found in the private part of a package specification
9770 if In_Private_Part (Current_Scope)
9771 and then Has_Private_With (P)
9772 and then Is_Child_Unit (Current_Scope)
9773 and then Is_Child_Unit (P)
9774 and then Is_Ancestor_Package (Scope (Current_Scope), P)
9776 Private_With_OK := True;
9779 -- Loop through entities in one package making them potentially
9782 Id := First_Entity (P);
9784 and then (Id /= First_Private_Entity (P)
9785 or else Private_With_OK) -- Ada 2005 (AI-262)
9787 Prev := Current_Entity (Id);
9788 while Present (Prev) loop
9789 if Is_Immediately_Visible (Prev)
9790 and then (not Is_Overloadable (Prev)
9791 or else not Is_Overloadable (Id)
9792 or else (Type_Conformant (Id, Prev)))
9794 if No (Current_Instance) then
9796 -- Potentially use-visible entity remains hidden
9798 goto Next_Usable_Entity;
9800 -- A use clause within an instance hides outer global entities,
9801 -- which are not used to resolve local entities in the
9802 -- instance. Note that the predefined entities in Standard
9803 -- could not have been hidden in the generic by a use clause,
9804 -- and therefore remain visible. Other compilation units whose
9805 -- entities appear in Standard must be hidden in an instance.
9807 -- To determine whether an entity is external to the instance
9808 -- we compare the scope depth of its scope with that of the
9809 -- current instance. However, a generic actual of a subprogram
9810 -- instance is declared in the wrapper package but will not be
9811 -- hidden by a use-visible entity. similarly, an entity that is
9812 -- declared in an enclosing instance will not be hidden by an
9813 -- an entity declared in a generic actual, which can only have
9814 -- been use-visible in the generic and will not have hidden the
9815 -- entity in the generic parent.
9817 -- If Id is called Standard, the predefined package with the
9818 -- same name is in the homonym chain. It has to be ignored
9819 -- because it has no defined scope (being the only entity in
9820 -- the system with this mandated behavior).
9822 elsif not Is_Hidden (Id)
9823 and then Present (Scope (Prev))
9824 and then not Is_Wrapper_Package (Scope (Prev))
9825 and then Scope_Depth (Scope (Prev)) <
9826 Scope_Depth (Current_Instance)
9827 and then (Scope (Prev) /= Standard_Standard
9828 or else Sloc (Prev) > Standard_Location)
9830 if In_Open_Scopes (Scope (Prev))
9831 and then Is_Generic_Instance (Scope (Prev))
9832 and then Present (Associated_Formal_Package (P))
9837 Set_Is_Potentially_Use_Visible (Id);
9838 Set_Is_Immediately_Visible (Prev, False);
9839 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
9843 -- A user-defined operator is not use-visible if the predefined
9844 -- operator for the type is immediately visible, which is the case
9845 -- if the type of the operand is in an open scope. This does not
9846 -- apply to user-defined operators that have operands of different
9847 -- types, because the predefined mixed mode operations (multiply
9848 -- and divide) apply to universal types and do not hide anything.
9850 elsif Ekind (Prev) = E_Operator
9851 and then Operator_Matches_Spec (Prev, Id)
9852 and then In_Open_Scopes
9853 (Scope (Base_Type (Etype (First_Formal (Id)))))
9854 and then (No (Next_Formal (First_Formal (Id)))
9855 or else Etype (First_Formal (Id)) =
9856 Etype (Next_Formal (First_Formal (Id)))
9857 or else Chars (Prev) = Name_Op_Expon)
9859 goto Next_Usable_Entity;
9861 -- In an instance, two homonyms may become use_visible through the
9862 -- actuals of distinct formal packages. In the generic, only the
9863 -- current one would have been visible, so make the other one
9866 -- In certain pathological cases it is possible that unrelated
9867 -- homonyms from distinct formal packages may exist in an
9868 -- uninstalled scope. We must test for that here.
9870 elsif Present (Current_Instance)
9871 and then Is_Potentially_Use_Visible (Prev)
9872 and then not Is_Overloadable (Prev)
9873 and then Scope (Id) /= Scope (Prev)
9874 and then Used_As_Generic_Actual (Scope (Prev))
9875 and then Used_As_Generic_Actual (Scope (Id))
9876 and then Is_List_Member (Scope (Prev))
9877 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
9878 Current_Use_Clause (Scope (Id)))
9880 Set_Is_Potentially_Use_Visible (Prev, False);
9881 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
9884 Prev := Homonym (Prev);
9887 -- On exit, we know entity is not hidden, unless it is private
9889 if not Is_Hidden (Id)
9890 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
9892 Set_Is_Potentially_Use_Visible (Id);
9894 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
9895 Set_Is_Potentially_Use_Visible (Full_View (Id));
9899 <<Next_Usable_Entity>>
9903 -- Child units are also made use-visible by a use clause, but they may
9904 -- appear after all visible declarations in the parent entity list.
9906 while Present (Id) loop
9907 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
9908 Set_Is_Potentially_Use_Visible (Id);
9914 if Chars (Real_P) = Name_System
9915 and then Scope (Real_P) = Standard_Standard
9916 and then Present_System_Aux (N)
9918 Use_One_Package (N);
9920 end Use_One_Package;
9926 procedure Use_One_Type
9928 Installed : Boolean := False;
9929 Force : Boolean := False)
9931 function Spec_Reloaded_For_Body return Boolean;
9932 -- Determine whether the compilation unit is a package body and the use
9933 -- type clause is in the spec of the same package. Even though the spec
9934 -- was analyzed first, its context is reloaded when analysing the body.
9936 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
9937 -- AI05-150: if the use_type_clause carries the "all" qualifier,
9938 -- class-wide operations of ancestor types are use-visible if the
9939 -- ancestor type is visible.
9941 ----------------------------
9942 -- Spec_Reloaded_For_Body --
9943 ----------------------------
9945 function Spec_Reloaded_For_Body return Boolean is
9947 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9949 Spec : constant Node_Id :=
9950 Parent (List_Containing (Parent (Id)));
9953 -- Check whether type is declared in a package specification,
9954 -- and current unit is the corresponding package body. The
9955 -- use clauses themselves may be within a nested package.
9958 Nkind (Spec) = N_Package_Specification
9959 and then In_Same_Source_Unit
9960 (Corresponding_Body (Parent (Spec)),
9961 Cunit_Entity (Current_Sem_Unit));
9966 end Spec_Reloaded_For_Body;
9968 -------------------------------
9969 -- Use_Class_Wide_Operations --
9970 -------------------------------
9972 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
9973 function Is_Class_Wide_Operation_Of
9975 T : Entity_Id) return Boolean;
9976 -- Determine whether a subprogram has a class-wide parameter or
9977 -- result that is T'Class.
9979 ---------------------------------
9980 -- Is_Class_Wide_Operation_Of --
9981 ---------------------------------
9983 function Is_Class_Wide_Operation_Of
9985 T : Entity_Id) return Boolean
9990 Formal := First_Formal (Op);
9991 while Present (Formal) loop
9992 if Etype (Formal) = Class_Wide_Type (T) then
9996 Next_Formal (Formal);
9999 if Etype (Op) = Class_Wide_Type (T) then
10004 end Is_Class_Wide_Operation_Of;
10011 -- Start of processing for Use_Class_Wide_Operations
10014 Scop := Scope (Typ);
10015 if not Is_Hidden (Scop) then
10016 Ent := First_Entity (Scop);
10017 while Present (Ent) loop
10018 if Is_Overloadable (Ent)
10019 and then Is_Class_Wide_Operation_Of (Ent, Typ)
10020 and then not Is_Potentially_Use_Visible (Ent)
10022 Set_Is_Potentially_Use_Visible (Ent);
10023 Append_Elmt (Ent, Used_Operations (Parent (Id)));
10030 if Is_Derived_Type (Typ) then
10031 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
10033 end Use_Class_Wide_Operations;
10038 Is_Known_Used : Boolean;
10039 Op_List : Elist_Id;
10042 -- Start of processing for Use_One_Type
10045 if Entity (Id) = Any_Type then
10049 -- It is the type determined by the subtype mark (8.4(8)) whose
10050 -- operations become potentially use-visible.
10052 T := Base_Type (Entity (Id));
10054 -- Either the type itself is used, the package where it is declared is
10055 -- in use or the entity is declared in the current package, thus
10060 and then ((Present (Current_Use_Clause (T))
10061 and then All_Present (Current_Use_Clause (T)))
10062 or else not All_Present (Parent (Id))))
10063 or else In_Use (Scope (T))
10064 or else Scope (T) = Current_Scope;
10066 Set_Redundant_Use (Id,
10067 Is_Known_Used or else Is_Potentially_Use_Visible (T));
10069 if Ekind (T) = E_Incomplete_Type then
10070 Error_Msg_N ("premature usage of incomplete type", Id);
10072 elsif In_Open_Scopes (Scope (T)) then
10075 -- A limited view cannot appear in a use_type_clause. However, an access
10076 -- type whose designated type is limited has the flag but is not itself
10077 -- a limited view unless we only have a limited view of its enclosing
10080 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
10082 ("incomplete type from limited view cannot appear in use clause",
10085 -- If the use clause is redundant, Used_Operations will usually be
10086 -- empty, but we need to set it to empty here in one case: If we are
10087 -- instantiating a generic library unit, then we install the ancestors
10088 -- of that unit in the scope stack, which involves reprocessing use
10089 -- clauses in those ancestors. Such a use clause will typically have a
10090 -- nonempty Used_Operations unless it was redundant in the generic unit,
10091 -- even if it is redundant at the place of the instantiation.
10093 elsif Redundant_Use (Id) then
10095 -- We must avoid incorrectly setting the Current_Use_Clause when we
10096 -- are working with a redundant clause that has already been linked
10097 -- in the Prev_Use_Clause chain, otherwise the chain will break.
10099 if Present (Current_Use_Clause (T))
10100 and then Present (Prev_Use_Clause (Current_Use_Clause (T)))
10101 and then Parent (Id) = Prev_Use_Clause (Current_Use_Clause (T))
10105 Set_Current_Use_Clause (T, Parent (Id));
10108 Set_Used_Operations (Parent (Id), New_Elmt_List);
10110 -- If the subtype mark designates a subtype in a different package,
10111 -- we have to check that the parent type is visible, otherwise the
10112 -- use_type_clause is a no-op. Not clear how to do that???
10115 Set_Current_Use_Clause (T, Parent (Id));
10118 -- If T is tagged, primitive operators on class-wide operands are
10119 -- also deemed available. Note that this is really necessary only
10120 -- in semantics-only mode, because the primitive operators are not
10121 -- fully constructed in this mode, but we do it in all modes for the
10122 -- sake of uniformity, as this should not matter in practice.
10124 if Is_Tagged_Type (T) then
10125 Set_In_Use (Class_Wide_Type (T));
10128 -- Iterate over primitive operations of the type. If an operation is
10129 -- already use_visible, it is the result of a previous use_clause,
10130 -- and already appears on the corresponding entity chain. If the
10131 -- clause is being reinstalled, operations are already use-visible.
10137 Op_List := Collect_Primitive_Operations (T);
10138 Elmt := First_Elmt (Op_List);
10139 while Present (Elmt) loop
10140 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
10141 or else Chars (Node (Elmt)) in Any_Operator_Name)
10142 and then not Is_Hidden (Node (Elmt))
10143 and then not Is_Potentially_Use_Visible (Node (Elmt))
10145 Set_Is_Potentially_Use_Visible (Node (Elmt));
10146 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10148 elsif Ada_Version >= Ada_2012
10149 and then All_Present (Parent (Id))
10150 and then not Is_Hidden (Node (Elmt))
10151 and then not Is_Potentially_Use_Visible (Node (Elmt))
10153 Set_Is_Potentially_Use_Visible (Node (Elmt));
10154 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10161 if Ada_Version >= Ada_2012
10162 and then All_Present (Parent (Id))
10163 and then Is_Tagged_Type (T)
10165 Use_Class_Wide_Operations (T);
10169 -- If warning on redundant constructs, check for unnecessary WITH
10172 and then Warn_On_Redundant_Constructs
10173 and then Is_Known_Used
10175 -- with P; with P; use P;
10176 -- package P is package X is package body X is
10177 -- type T ... use P.T;
10179 -- The compilation unit is the body of X. GNAT first compiles the
10180 -- spec of X, then proceeds to the body. At that point P is marked
10181 -- as use visible. The analysis then reinstalls the spec along with
10182 -- its context. The use clause P.T is now recognized as redundant,
10183 -- but in the wrong context. Do not emit a warning in such cases.
10184 -- Do not emit a warning either if we are in an instance, there is
10185 -- no redundancy between an outer use_clause and one that appears
10186 -- within the generic.
10188 and then not Spec_Reloaded_For_Body
10189 and then not In_Instance
10190 and then not In_Inlined_Body
10192 -- The type already has a use clause
10196 -- Case where we know the current use clause for the type
10198 if Present (Current_Use_Clause (T)) then
10199 Use_Clause_Known : declare
10200 Clause1 : constant Node_Id :=
10201 Find_Most_Prev (Current_Use_Clause (T));
10202 Clause2 : constant Node_Id := Parent (Id);
10209 -- Start of processing for Use_Clause_Known
10212 -- If both current use_type_clause and the use_type_clause
10213 -- for the type are at the compilation unit level, one of
10214 -- the units must be an ancestor of the other, and the
10215 -- warning belongs on the descendant.
10217 if Nkind (Parent (Clause1)) = N_Compilation_Unit
10219 Nkind (Parent (Clause2)) = N_Compilation_Unit
10221 -- If the unit is a subprogram body that acts as spec,
10222 -- the context clause is shared with the constructed
10223 -- subprogram spec. Clearly there is no redundancy.
10225 if Clause1 = Clause2 then
10229 Unit1 := Unit (Parent (Clause1));
10230 Unit2 := Unit (Parent (Clause2));
10232 -- If both clauses are on same unit, or one is the body
10233 -- of the other, or one of them is in a subunit, report
10234 -- redundancy on the later one.
10236 if Unit1 = Unit2 or else Nkind (Unit1) = N_Subunit then
10237 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10238 Error_Msg_NE -- CODEFIX
10239 ("& is already use-visible through previous "
10240 & "use_type_clause #??", Clause1, T);
10243 elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body)
10244 and then Nkind (Unit1) /= Nkind (Unit2)
10245 and then Nkind (Unit1) /= N_Subunit
10247 Error_Msg_Sloc := Sloc (Clause1);
10248 Error_Msg_NE -- CODEFIX
10249 ("& is already use-visible through previous "
10250 & "use_type_clause #??", Current_Use_Clause (T), T);
10254 -- There is a redundant use_type_clause in a child unit.
10255 -- Determine which of the units is more deeply nested.
10256 -- If a unit is a package instance, retrieve the entity
10257 -- and its scope from the instance spec.
10259 Ent1 := Entity_Of_Unit (Unit1);
10260 Ent2 := Entity_Of_Unit (Unit2);
10262 if Scope (Ent2) = Standard_Standard then
10263 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10266 elsif Scope (Ent1) = Standard_Standard then
10267 Error_Msg_Sloc := Sloc (Id);
10270 -- If both units are child units, we determine which one
10271 -- is the descendant by the scope distance to the
10272 -- ultimate parent unit.
10280 S1 := Scope (Ent1);
10281 S2 := Scope (Ent2);
10283 and then Present (S2)
10284 and then S1 /= Standard_Standard
10285 and then S2 /= Standard_Standard
10291 if S1 = Standard_Standard then
10292 Error_Msg_Sloc := Sloc (Id);
10295 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10301 if Parent (Id) /= Err_No then
10302 if Most_Descendant_Use_Clause
10303 (Err_No, Parent (Id)) = Parent (Id)
10305 Error_Msg_Sloc := Sloc (Err_No);
10306 Err_No := Parent (Id);
10309 Error_Msg_NE -- CODEFIX
10310 ("& is already use-visible through previous "
10311 & "use_type_clause #??", Err_No, Id);
10314 -- Case where current use_type_clause and use_type_clause
10315 -- for the type are not both at the compilation unit level.
10316 -- In this case we don't have location information.
10319 Error_Msg_NE -- CODEFIX
10320 ("& is already use-visible through previous "
10321 & "use_type_clause??", Id, T);
10323 end Use_Clause_Known;
10325 -- Here if Current_Use_Clause is not set for T, another case where
10326 -- we do not have the location information available.
10329 Error_Msg_NE -- CODEFIX
10330 ("& is already use-visible through previous "
10331 & "use_type_clause??", Id, T);
10334 -- The package where T is declared is already used
10336 elsif In_Use (Scope (T)) then
10337 -- Due to expansion of contracts we could be attempting to issue
10338 -- a spurious warning - so verify there is a previous use clause.
10340 if Current_Use_Clause (Scope (T)) /=
10341 Find_Most_Prev (Current_Use_Clause (Scope (T)))
10344 Sloc (Find_Most_Prev (Current_Use_Clause (Scope (T))));
10345 Error_Msg_NE -- CODEFIX
10346 ("& is already use-visible through package use clause #??",
10350 -- The current scope is the package where T is declared
10353 Error_Msg_Node_2 := Scope (T);
10354 Error_Msg_NE -- CODEFIX
10355 ("& is already use-visible inside package &??", Id, T);
10364 procedure Write_Info is
10365 Id : Entity_Id := First_Entity (Current_Scope);
10368 -- No point in dumping standard entities
10370 if Current_Scope = Standard_Standard then
10374 Write_Str ("========================================================");
10376 Write_Str (" Defined Entities in ");
10377 Write_Name (Chars (Current_Scope));
10379 Write_Str ("========================================================");
10383 Write_Str ("-- none --");
10387 while Present (Id) loop
10388 Write_Entity_Info (Id, " ");
10393 if Scope (Current_Scope) = Standard_Standard then
10395 -- Print information on the current unit itself
10397 Write_Entity_Info (Current_Scope, " ");
10410 for J in reverse 1 .. Scope_Stack.Last loop
10411 S := Scope_Stack.Table (J).Entity;
10412 Write_Int (Int (S));
10413 Write_Str (" === ");
10414 Write_Name (Chars (S));
10423 procedure we (S : Entity_Id) is
10426 E := First_Entity (S);
10427 while Present (E) loop
10428 Write_Int (Int (E));
10429 Write_Str (" === ");
10430 Write_Name (Chars (E));