1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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 Einfo.Entities; use Einfo.Entities;
30 with Einfo.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Impunit; use Impunit;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Namet.Sp; use Namet.Sp;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
47 with Output; use Output;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch4; use Sem_Ch4;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch12; use Sem_Ch12;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Dim; use Sem_Dim;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Elab; use Sem_Elab;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Prag; use Sem_Prag;
66 with Sem_Res; use Sem_Res;
67 with Sem_Util; use Sem_Util;
68 with Sem_Type; use Sem_Type;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Sinfo.Nodes; use Sinfo.Nodes;
72 with Sinfo.Utils; use Sinfo.Utils;
73 with Sinfo.CN; use Sinfo.CN;
74 with Snames; use Snames;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
80 package body Sem_Ch8 is
82 ------------------------------------
83 -- Visibility and Name Resolution --
84 ------------------------------------
86 -- This package handles name resolution and the collection of possible
87 -- interpretations for overloaded names, prior to overload resolution.
89 -- Name resolution is the process that establishes a mapping between source
90 -- identifiers and the entities they denote at each point in the program.
91 -- Each entity is represented by a defining occurrence. Each identifier
92 -- that denotes an entity points to the corresponding defining occurrence.
93 -- This is the entity of the applied occurrence. Each occurrence holds
94 -- an index into the names table, where source identifiers are stored.
96 -- Each entry in the names table for an identifier or designator uses the
97 -- Info pointer to hold a link to the currently visible entity that has
98 -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
99 -- in package Sem_Util). The visibility is initialized at the beginning of
100 -- semantic processing to make entities in package Standard immediately
101 -- visible. The visibility table is used in a more subtle way when
102 -- compiling subunits (see below).
104 -- Entities that have the same name (i.e. homonyms) are chained. In the
105 -- case of overloaded entities, this chain holds all the possible meanings
106 -- of a given identifier. The process of overload resolution uses type
107 -- information to select from this chain the unique meaning of a given
110 -- Entities are also chained in their scope, through the Next_Entity link.
111 -- As a consequence, the name space is organized as a sparse matrix, where
112 -- each row corresponds to a scope, and each column to a source identifier.
113 -- Open scopes, that is to say scopes currently being compiled, have their
114 -- corresponding rows of entities in order, innermost scope first.
116 -- The scopes of packages that are mentioned in context clauses appear in
117 -- no particular order, interspersed among open scopes. This is because
118 -- in the course of analyzing the context of a compilation, a package
119 -- declaration is first an open scope, and subsequently an element of the
120 -- context. If subunits or child units are present, a parent unit may
121 -- appear under various guises at various times in the compilation.
123 -- When the compilation of the innermost scope is complete, the entities
124 -- defined therein are no longer visible. If the scope is not a package
125 -- declaration, these entities are never visible subsequently, and can be
126 -- removed from visibility chains. If the scope is a package declaration,
127 -- its visible declarations may still be accessible. Therefore the entities
128 -- defined in such a scope are left on the visibility chains, and only
129 -- their visibility (immediately visibility or potential use-visibility)
132 -- The ordering of homonyms on their chain does not necessarily follow
133 -- the order of their corresponding scopes on the scope stack. For
134 -- example, if package P and the enclosing scope both contain entities
135 -- named E, then when compiling the package body the chain for E will
136 -- hold the global entity first, and the local one (corresponding to
137 -- the current inner scope) next. As a result, name resolution routines
138 -- do not assume any relative ordering of the homonym chains, either
139 -- for scope nesting or to order of appearance of context clauses.
141 -- When compiling a child unit, entities in the parent scope are always
142 -- immediately visible. When compiling the body of a child unit, private
143 -- entities in the parent must also be made immediately visible. There
144 -- are separate routines to make the visible and private declarations
145 -- visible at various times (see package Sem_Ch7).
147 -- +--------+ +-----+
148 -- | In use |-------->| EU1 |-------------------------->
149 -- +--------+ +-----+
151 -- +--------+ +-----+ +-----+
152 -- | Stand. |---------------->| ES1 |--------------->| ES2 |--->
153 -- +--------+ +-----+ +-----+
155 -- +---------+ | +-----+
156 -- | with'ed |------------------------------>| EW2 |--->
157 -- +---------+ | +-----+
159 -- +--------+ +-----+ +-----+
160 -- | Scope2 |---------------->| E12 |--------------->| E22 |--->
161 -- +--------+ +-----+ +-----+
163 -- +--------+ +-----+ +-----+
164 -- | Scope1 |---------------->| E11 |--------------->| E12 |--->
165 -- +--------+ +-----+ +-----+
169 -- | | with'ed |----------------------------------------->
173 -- (innermost first) | |
174 -- +----------------------------+
175 -- Names table => | Id1 | | | | Id2 |
176 -- +----------------------------+
178 -- Name resolution must deal with several syntactic forms: simple names,
179 -- qualified names, indexed names, and various forms of calls.
181 -- Each identifier points to an entry in the names table. The resolution
182 -- of a simple name consists in traversing the homonym chain, starting
183 -- from the names table. If an entry is immediately visible, it is the one
184 -- designated by the identifier. If only potentially use-visible entities
185 -- are on the chain, we must verify that they do not hide each other. If
186 -- the entity we find is overloadable, we collect all other overloadable
187 -- entities on the chain as long as they are not hidden.
189 -- To resolve expanded names, we must find the entity at the intersection
190 -- of the entity chain for the scope (the prefix) and the homonym chain
191 -- for the selector. In general, homonym chains will be much shorter than
192 -- entity chains, so it is preferable to start from the names table as
193 -- well. If the entity found is overloadable, we must collect all other
194 -- interpretations that are defined in the scope denoted by the prefix.
196 -- For records, protected types, and tasks, their local entities are
197 -- removed from visibility chains on exit from the corresponding scope.
198 -- From the outside, these entities are always accessed by selected
199 -- notation, and the entity chain for the record type, protected type,
200 -- etc. is traversed sequentially in order to find the designated entity.
202 -- The discriminants of a type and the operations of a protected type or
203 -- task are unchained on exit from the first view of the type, (such as
204 -- a private or incomplete type declaration, or a protected type speci-
205 -- fication) and re-chained when compiling the second view.
207 -- In the case of operators, we do not make operators on derived types
208 -- explicit. As a result, the notation P."+" may denote either a user-
209 -- defined function with name "+", or else an implicit declaration of the
210 -- operator "+" in package P. The resolution of expanded names always
211 -- tries to resolve an operator name as such an implicitly defined entity,
212 -- in addition to looking for explicit declarations.
214 -- All forms of names that denote entities (simple names, expanded names,
215 -- character literals in some cases) have a Entity attribute, which
216 -- identifies the entity denoted by the name.
218 ---------------------
219 -- The Scope Stack --
220 ---------------------
222 -- The Scope stack keeps track of the scopes currently been compiled.
223 -- Every entity that contains declarations (including records) is placed
224 -- on the scope stack while it is being processed, and removed at the end.
225 -- Whenever a non-package scope is exited, the entities defined therein
226 -- are removed from the visibility table, so that entities in outer scopes
227 -- become visible (see previous description). On entry to Sem, the scope
228 -- stack only contains the package Standard. As usual, subunits complicate
229 -- this picture ever so slightly.
231 -- The Rtsfind mechanism can force a call to Semantics while another
232 -- compilation is in progress. The unit retrieved by Rtsfind must be
233 -- compiled in its own context, and has no access to the visibility of
234 -- the unit currently being compiled. The procedures Save_Scope_Stack and
235 -- Restore_Scope_Stack make entities in current open scopes invisible
236 -- before compiling the retrieved unit, and restore the compilation
237 -- environment afterwards.
239 ------------------------
240 -- Compiling subunits --
241 ------------------------
243 -- Subunits must be compiled in the environment of the corresponding stub,
244 -- that is to say with the same visibility into the parent (and its
245 -- context) that is available at the point of the stub declaration, but
246 -- with the additional visibility provided by the context clause of the
247 -- subunit itself. As a result, compilation of a subunit forces compilation
248 -- of the parent (see description in lib-). At the point of the stub
249 -- declaration, Analyze is called recursively to compile the proper body of
250 -- the subunit, but without reinitializing the names table, nor the scope
251 -- stack (i.e. standard is not pushed on the stack). In this fashion the
252 -- context of the subunit is added to the context of the parent, and the
253 -- subunit is compiled in the correct environment. Note that in the course
254 -- of processing the context of a subunit, Standard will appear twice on
255 -- the scope stack: once for the parent of the subunit, and once for the
256 -- unit in the context clause being compiled. However, the two sets of
257 -- entities are not linked by homonym chains, so that the compilation of
258 -- any context unit happens in a fresh visibility environment.
260 -------------------------------
261 -- Processing of USE Clauses --
262 -------------------------------
264 -- Every defining occurrence has a flag indicating if it is potentially use
265 -- visible. Resolution of simple names examines this flag. The processing
266 -- of use clauses consists in setting this flag on all visible entities
267 -- defined in the corresponding package. On exit from the scope of the use
268 -- clause, the corresponding flag must be reset. However, a package may
269 -- appear in several nested use clauses (pathological but legal, alas)
270 -- which forces us to use a slightly more involved scheme:
272 -- a) The defining occurrence for a package holds a flag -In_Use- to
273 -- indicate that it is currently in the scope of a use clause. If a
274 -- redundant use clause is encountered, then the corresponding occurrence
275 -- of the package name is flagged -Redundant_Use-.
277 -- b) On exit from a scope, the use clauses in its declarative part are
278 -- scanned. The visibility flag is reset in all entities declared in
279 -- package named in a use clause, as long as the package is not flagged
280 -- as being in a redundant use clause (in which case the outer use
281 -- clause is still in effect, and the direct visibility of its entities
282 -- must be retained).
284 -- Note that entities are not removed from their homonym chains on exit
285 -- from the package specification. A subsequent use clause does not need
286 -- to rechain the visible entities, but only to establish their direct
289 -----------------------------------
290 -- Handling private declarations --
291 -----------------------------------
293 -- The principle that each entity has a single defining occurrence clashes
294 -- with the presence of two separate definitions for private types: the
295 -- first is the private type declaration, and second is the full type
296 -- declaration. It is important that all references to the type point to
297 -- the same defining occurrence, namely the first one. To enforce the two
298 -- separate views of the entity, the corresponding information is swapped
299 -- between the two declarations. Outside of the package, the defining
300 -- occurrence only contains the private declaration information, while in
301 -- the private part and the body of the package the defining occurrence
302 -- contains the full declaration. To simplify the swap, the defining
303 -- occurrence that currently holds the private declaration points to the
304 -- full declaration. During semantic processing the defining occurrence
305 -- also points to a list of private dependents, that is to say access types
306 -- or composite types whose designated types or component types are
307 -- subtypes or derived types of the private type in question. After the
308 -- full declaration has been seen, the private dependents are updated to
309 -- indicate that they have full definitions.
311 ------------------------------------
312 -- Handling of Undefined Messages --
313 ------------------------------------
315 -- In normal mode, only the first use of an undefined identifier generates
316 -- a message. The table Urefs is used to record error messages that have
317 -- been issued so that second and subsequent ones do not generate further
318 -- messages. However, the second reference causes text to be added to the
319 -- original undefined message noting "(more references follow)". The
320 -- full error list option (-gnatf) forces messages to be generated for
321 -- every reference and disconnects the use of this table.
323 type Uref_Entry is record
325 -- Node for identifier for which original message was posted. The
326 -- Chars field of this identifier is used to detect later references
327 -- to the same identifier.
330 -- Records error message Id of original undefined message. Reset to
331 -- No_Error_Msg after the second occurrence, where it is used to add
332 -- text to the original message as described above.
335 -- Set if the message is not visible rather than undefined
338 -- Records location of error message. Used to make sure that we do
339 -- not consider a, b : undefined as two separate instances, which
340 -- would otherwise happen, since the parser converts this sequence
341 -- to a : undefined; b : undefined.
345 package Urefs is new Table.Table (
346 Table_Component_Type => Uref_Entry,
347 Table_Index_Type => Nat,
348 Table_Low_Bound => 1,
350 Table_Increment => 100,
351 Table_Name => "Urefs");
353 Candidate_Renaming : Entity_Id;
354 -- Holds a candidate interpretation that appears in a subprogram renaming
355 -- declaration and does not match the given specification, but matches at
356 -- least on the first formal. Allows better error message when given
357 -- specification omits defaulted parameters, a common error.
359 -----------------------
360 -- Local Subprograms --
361 -----------------------
363 procedure Analyze_Generic_Renaming
366 -- Common processing for all three kinds of generic renaming declarations.
367 -- Enter new name and indicate that it renames the generic unit.
369 procedure Analyze_Renamed_Character
373 -- Renamed entity is given by a character literal, which must belong
374 -- to the return type of the new entity. Is_Body indicates whether the
375 -- declaration is a renaming_as_body. If the original declaration has
376 -- already been frozen (because of an intervening body, e.g.) the body of
377 -- the function must be built now. The same applies to the following
378 -- various renaming procedures.
380 procedure Analyze_Renamed_Dereference
384 -- Renamed entity is given by an explicit dereference. Prefix must be a
385 -- conformant access_to_subprogram type.
387 procedure Analyze_Renamed_Entry
391 -- If the renamed entity in a subprogram renaming is an entry or protected
392 -- subprogram, build a body for the new entity whose only statement is a
393 -- call to the renamed entity.
395 procedure Analyze_Renamed_Family_Member
399 -- Used when the renamed entity is an indexed component. The prefix must
400 -- denote an entry family.
402 procedure Analyze_Renamed_Primitive_Operation
406 -- If the renamed entity in a subprogram renaming is a primitive operation
407 -- or a class-wide operation in prefix form, save the target object,
408 -- which must be added to the list of actuals in any subsequent call.
409 -- The renaming operation is intrinsic because the compiler must in
410 -- fact generate a wrapper for it (6.3.1 (10 1/2)).
412 procedure Attribute_Renaming (N : Node_Id);
413 -- Analyze renaming of attribute as subprogram. The renaming declaration N
414 -- is rewritten as a subprogram body that returns the attribute reference
415 -- applied to the formals of the function.
417 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
418 -- Set Entity, with style check if need be. For a discriminant reference,
419 -- replace by the corresponding discriminal, i.e. the parameter of the
420 -- initialization procedure that corresponds to the discriminant.
422 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
423 -- A renaming_as_body may occur after the entity of the original decla-
424 -- ration has been frozen. In that case, the body of the new entity must
425 -- be built now, because the usual mechanism of building the renamed
426 -- body at the point of freezing will not work. Subp is the subprogram
427 -- for which N provides the Renaming_As_Body.
429 procedure Check_In_Previous_With_Clause
432 -- N is a use_package clause and Nam the package name, or N is a use_type
433 -- clause and Nam is the prefix of the type name. In either case, verify
434 -- that the package is visible at that point in the context: either it
435 -- appears in a previous with_clause, or because it is a fully qualified
436 -- name and the root ancestor appears in a previous with_clause.
438 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
439 -- Verify that the entity in a renaming declaration that is a library unit
440 -- is itself a library unit and not a nested unit or subunit. Also check
441 -- that if the renaming is a child unit of a generic parent, then the
442 -- renamed unit must also be a child unit of that parent. Finally, verify
443 -- that a renamed generic unit is not an implicit child declared within
444 -- an instance of the parent.
446 procedure Chain_Use_Clause (N : Node_Id);
447 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
448 -- the proper scope table entry. This is usually the current scope, but it
449 -- will be an inner scope when installing the use clauses of the private
450 -- declarations of a parent unit prior to compiling the private part of a
451 -- child unit. This chain is traversed when installing/removing use clauses
452 -- when compiling a subunit or instantiating a generic body on the fly,
453 -- when it is necessary to save and restore full environments.
455 function Enclosing_Instance return Entity_Id;
456 -- In an instance nested within another one, several semantic checks are
457 -- unnecessary because the legality of the nested instance has been checked
458 -- in the enclosing generic unit. This applies in particular to legality
459 -- checks on actuals for formal subprograms of the inner instance, which
460 -- are checked as subprogram renamings, and may be complicated by confusion
461 -- in private/full views. This function returns the instance enclosing the
462 -- current one if there is such, else it returns Empty.
464 -- If the renaming determines the entity for the default of a formal
465 -- subprogram nested within another instance, choose the innermost
466 -- candidate. This is because if the formal has a box, and we are within
467 -- an enclosing instance where some candidate interpretations are local
468 -- to this enclosing instance, we know that the default was properly
469 -- resolved when analyzing the generic, so we prefer the local
470 -- candidates to those that are external. This is not always the case
471 -- but is a reasonable heuristic on the use of nested generics. The
472 -- proper solution requires a full renaming model.
474 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
475 -- Return the appropriate entity for determining which unit has a deeper
476 -- scope: the defining entity for U, unless U is a package instance, in
477 -- which case we retrieve the entity of the instance spec.
479 procedure Find_Expanded_Name (N : Node_Id);
480 -- The input is a selected component known to be an expanded name. Verify
481 -- legality of selector given the scope denoted by prefix, and change node
482 -- N into a expanded name with a properly set Entity field.
484 function Find_First_Use (Use_Clause : Node_Id) return Node_Id;
485 -- Find the most previous use clause (that is, the first one to appear in
486 -- the source) by traversing the previous clause chain that exists in both
487 -- N_Use_Package_Clause nodes and N_Use_Type_Clause nodes.
489 function Find_Renamed_Entity
493 Is_Actual : Boolean := False) return Entity_Id;
494 -- Find the renamed entity that corresponds to the given parameter profile
495 -- in a subprogram renaming declaration. The renamed entity may be an
496 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
497 -- indicates that the renaming is the one generated for an actual subpro-
498 -- gram in an instance, for which special visibility checks apply.
500 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
501 -- Find a type derived from Character or Wide_Character in the prefix of N.
502 -- Used to resolved qualified names whose selector is a character literal.
504 function Has_Private_With (E : Entity_Id) return Boolean;
505 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
506 -- private with on E.
508 function Has_Components (Typ : Entity_Id) return Boolean;
509 -- Determine if given type has components, i.e. is either a record type or
510 -- type or a type that has discriminants.
512 function Has_Implicit_Operator (N : Node_Id) return Boolean;
513 -- N is an expanded name whose selector is an operator name (e.g. P."+").
514 -- declarative part contains an implicit declaration of an operator if it
515 -- has a declaration of a type to which one of the predefined operators
516 -- apply. The existence of this routine is an implementation artifact. A
517 -- more straightforward but more space-consuming choice would be to make
518 -- all inherited operators explicit in the symbol table.
520 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
521 -- A subprogram defined by a renaming declaration inherits the parameter
522 -- profile of the renamed entity. The subtypes given in the subprogram
523 -- specification are discarded and replaced with those of the renamed
524 -- subprogram, which are then used to recheck the default values.
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
532 procedure Premature_Usage (N : Node_Id);
533 -- Diagnose usage of an entity before it is visible
535 procedure Use_One_Package
537 Pack_Name : Entity_Id := Empty;
538 Force : Boolean := False);
539 -- Make visible entities declared in package P potentially use-visible
540 -- in the current context. Also used in the analysis of subunits, when
541 -- re-installing use clauses of parent units. N is the use_clause that
542 -- names P (and possibly other packages).
544 procedure Use_One_Type
546 Installed : Boolean := False;
547 Force : Boolean := False);
548 -- Id is the subtype mark from a use_type_clause. This procedure makes
549 -- the primitive operators of the type potentially use-visible. The
550 -- boolean flag Installed indicates that the clause is being reinstalled
551 -- after previous analysis, and primitive operations are already chained
552 -- on the Used_Operations list of the clause.
554 procedure Write_Info;
555 -- Write debugging information on entities declared in current scope
557 --------------------------------
558 -- Analyze_Exception_Renaming --
559 --------------------------------
561 -- The language only allows a single identifier, but the tree holds an
562 -- identifier list. The parser has already issued an error message if
563 -- there is more than one element in the list.
565 procedure Analyze_Exception_Renaming (N : Node_Id) is
566 Id : constant Entity_Id := Defining_Entity (N);
567 Nam : constant Node_Id := Name (N);
573 Mutate_Ekind (Id, E_Exception);
574 Set_Etype (Id, Standard_Exception_Type);
575 Set_Is_Pure (Id, Is_Pure (Current_Scope));
577 if Is_Entity_Name (Nam)
578 and then Present (Entity (Nam))
579 and then Ekind (Entity (Nam)) = E_Exception
581 if Present (Renamed_Object (Entity (Nam))) then
582 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
584 Set_Renamed_Object (Id, Entity (Nam));
587 -- The exception renaming declaration may become Ghost if it renames
590 Mark_Ghost_Renaming (N, Entity (Nam));
592 Error_Msg_N ("invalid exception name in renaming", Nam);
595 -- Implementation-defined aspect specifications can appear in a renaming
596 -- declaration, but not language-defined ones. The call to procedure
597 -- Analyze_Aspect_Specifications will take care of this error check.
599 if Has_Aspects (N) then
600 Analyze_Aspect_Specifications (N, Id);
602 end Analyze_Exception_Renaming;
604 ---------------------------
605 -- Analyze_Expanded_Name --
606 ---------------------------
608 procedure Analyze_Expanded_Name (N : Node_Id) is
610 -- If the entity pointer is already set, this is an internal node, or a
611 -- node that is analyzed more than once, after a tree modification. In
612 -- such a case there is no resolution to perform, just set the type. In
613 -- either case, start by analyzing the prefix.
615 Analyze (Prefix (N));
617 if Present (Entity (N)) then
618 if Is_Type (Entity (N)) then
619 Set_Etype (N, Entity (N));
621 Set_Etype (N, Etype (Entity (N)));
625 Find_Expanded_Name (N);
628 -- In either case, propagate dimension of entity to expanded name
630 Analyze_Dimension (N);
631 end Analyze_Expanded_Name;
633 ---------------------------------------
634 -- Analyze_Generic_Function_Renaming --
635 ---------------------------------------
637 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
639 Analyze_Generic_Renaming (N, E_Generic_Function);
640 end Analyze_Generic_Function_Renaming;
642 --------------------------------------
643 -- Analyze_Generic_Package_Renaming --
644 --------------------------------------
646 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
648 -- Test for the Text_IO special unit case here, since we may be renaming
649 -- one of the subpackages of Text_IO, then join common routine.
651 Check_Text_IO_Special_Unit (Name (N));
653 Analyze_Generic_Renaming (N, E_Generic_Package);
654 end Analyze_Generic_Package_Renaming;
656 ----------------------------------------
657 -- Analyze_Generic_Procedure_Renaming --
658 ----------------------------------------
660 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
662 Analyze_Generic_Renaming (N, E_Generic_Procedure);
663 end Analyze_Generic_Procedure_Renaming;
665 ------------------------------
666 -- Analyze_Generic_Renaming --
667 ------------------------------
669 procedure Analyze_Generic_Renaming
673 New_P : constant Entity_Id := Defining_Entity (N);
674 Inst : Boolean := False;
678 if Name (N) = Error then
682 Generate_Definition (New_P);
684 if Current_Scope /= Standard_Standard then
685 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
688 if Nkind (Name (N)) = N_Selected_Component then
689 Check_Generic_Child_Unit (Name (N), Inst);
694 if not Is_Entity_Name (Name (N)) then
695 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
698 Old_P := Entity (Name (N));
702 Mutate_Ekind (New_P, K);
704 if Etype (Old_P) = Any_Type then
707 elsif Ekind (Old_P) /= K then
708 Error_Msg_N ("invalid generic unit name", Name (N));
711 if Present (Renamed_Object (Old_P)) then
712 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
714 Set_Renamed_Object (New_P, Old_P);
717 -- The generic renaming declaration may become Ghost if it renames a
720 Mark_Ghost_Renaming (N, Old_P);
722 Set_Is_Pure (New_P, Is_Pure (Old_P));
723 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
725 Set_Etype (New_P, Etype (Old_P));
726 Set_Has_Completion (New_P);
728 if In_Open_Scopes (Old_P) then
729 Error_Msg_N ("within its scope, generic denotes its instance", N);
732 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
733 -- renamings and subsequent instantiations of Unchecked_Conversion.
735 if Is_Generic_Subprogram (Old_P) then
736 Set_Is_Intrinsic_Subprogram
737 (New_P, Is_Intrinsic_Subprogram (Old_P));
740 Check_Library_Unit_Renaming (N, Old_P);
743 -- Implementation-defined aspect specifications can appear in a renaming
744 -- declaration, but not language-defined ones. The call to procedure
745 -- Analyze_Aspect_Specifications will take care of this error check.
747 if Has_Aspects (N) then
748 Analyze_Aspect_Specifications (N, New_P);
750 end Analyze_Generic_Renaming;
752 -----------------------------
753 -- Analyze_Object_Renaming --
754 -----------------------------
756 procedure Analyze_Object_Renaming (N : Node_Id) is
757 Id : constant Entity_Id := Defining_Identifier (N);
758 Loc : constant Source_Ptr := Sloc (N);
759 Nam : constant Node_Id := Name (N);
760 Is_Object_Ref : Boolean;
766 procedure Check_Constrained_Object;
767 -- If the nominal type is unconstrained but the renamed object is
768 -- constrained, as can happen with renaming an explicit dereference or
769 -- a function return, build a constrained subtype from the object. If
770 -- the renaming is for a formal in an accept statement, the analysis
771 -- has already established its actual subtype. This is only relevant
772 -- if the renamed object is an explicit dereference.
774 function Get_Object_Name (Nod : Node_Id) return Node_Id;
775 -- Obtain the name of the object from node Nod which is being renamed by
776 -- the object renaming declaration N.
778 function Find_Raise_Node (N : Node_Id) return Traverse_Result;
779 -- Process one node in search for N_Raise_xxx_Error nodes.
780 -- Return Abandon if found, OK otherwise.
782 ---------------------
783 -- Find_Raise_Node --
784 ---------------------
786 function Find_Raise_Node (N : Node_Id) return Traverse_Result is
788 if Nkind (N) in N_Raise_xxx_Error then
795 ------------------------
796 -- No_Raise_xxx_Error --
797 ------------------------
799 function No_Raise_xxx_Error is new Traverse_Func (Find_Raise_Node);
800 -- Traverse tree to look for a N_Raise_xxx_Error node and returns
801 -- Abandon if so and OK if none found.
803 ------------------------------
804 -- Check_Constrained_Object --
805 ------------------------------
807 procedure Check_Constrained_Object is
808 Typ : constant Entity_Id := Etype (Nam);
810 Loop_Scheme : Node_Id;
813 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference
814 and then Is_Composite_Type (Typ)
815 and then not Is_Constrained (Typ)
816 and then not Has_Unknown_Discriminants (Typ)
817 and then Expander_Active
819 -- If Actual_Subtype is already set, nothing to do
821 if Ekind (Id) in E_Variable | E_Constant
822 and then Present (Actual_Subtype (Id))
826 -- A renaming of an unchecked union has no actual subtype
828 elsif Is_Unchecked_Union (Typ) then
831 -- If a record is limited its size is invariant. This is the case
832 -- in particular with record types with an access discriminant
833 -- that are used in iterators. This is an optimization, but it
834 -- also prevents typing anomalies when the prefix is further
837 -- Note that we cannot just use the Is_Limited_Record flag because
838 -- it does not apply to records with limited components, for which
839 -- this syntactic flag is not set, but whose size is also fixed.
841 -- Note also that we need to build the constrained subtype for an
842 -- array in order to make the bounds explicit in most cases, but
843 -- not if the object comes from an extended return statement, as
844 -- this would create dangling references to them later on.
846 elsif Is_Limited_Type (Typ)
847 and then (not Is_Array_Type (Typ) or else Is_Return_Object (Id))
852 Subt := Make_Temporary (Loc, 'T');
853 Remove_Side_Effects (Nam);
855 Make_Subtype_Declaration (Loc,
856 Defining_Identifier => Subt,
857 Subtype_Indication =>
858 Make_Subtype_From_Expr (Nam, Typ)));
859 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
860 Set_Etype (Nam, Subt);
862 -- Suppress discriminant checks on this subtype if the original
863 -- type has defaulted discriminants and Id is a "for of" loop
866 if Has_Defaulted_Discriminants (Typ)
867 and then Nkind (Original_Node (Parent (N))) = N_Loop_Statement
869 Loop_Scheme := Iteration_Scheme (Original_Node (Parent (N)));
871 if Present (Loop_Scheme)
872 and then Present (Iterator_Specification (Loop_Scheme))
875 (Iterator_Specification (Loop_Scheme)) = Id
877 Set_Checks_May_Be_Suppressed (Subt);
878 Push_Local_Suppress_Stack_Entry
880 Check => Discriminant_Check,
885 -- Freeze subtype at once, to prevent order of elaboration
886 -- issues in the backend. The renamed object exists, so its
887 -- type is already frozen in any case.
889 Freeze_Before (N, Subt);
892 end Check_Constrained_Object;
894 ---------------------
895 -- Get_Object_Name --
896 ---------------------
898 function Get_Object_Name (Nod : Node_Id) return Node_Id is
903 while Present (Obj_Nam) loop
904 case Nkind (Obj_Nam) is
905 when N_Attribute_Reference
906 | N_Explicit_Dereference
907 | N_Indexed_Component
910 Obj_Nam := Prefix (Obj_Nam);
912 when N_Selected_Component =>
913 Obj_Nam := Selector_Name (Obj_Nam);
915 when N_Qualified_Expression | N_Type_Conversion =>
916 Obj_Nam := Expression (Obj_Nam);
926 -- Start of processing for Analyze_Object_Renaming
933 Set_Is_Pure (Id, Is_Pure (Current_Scope));
936 -- The renaming of a component that depends on a discriminant requires
937 -- an actual subtype, because in subsequent use of the object Gigi will
938 -- be unable to locate the actual bounds. This explicit step is required
939 -- when the renaming is generated in removing side effects of an
940 -- already-analyzed expression.
942 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
944 -- The object renaming declaration may become Ghost if it renames a
947 if Is_Entity_Name (Nam) then
948 Mark_Ghost_Renaming (N, Entity (Nam));
952 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
954 if Present (Dec) then
955 Insert_Action (N, Dec);
956 T := Defining_Identifier (Dec);
959 elsif Present (Subtype_Mark (N))
960 or else not Present (Access_Definition (N))
962 if Present (Subtype_Mark (N)) then
963 Find_Type (Subtype_Mark (N));
964 T := Entity (Subtype_Mark (N));
967 -- AI12-0275: Case of object renaming without a subtype_mark
972 -- Normal case of no overloading in object name
974 if not Is_Overloaded (Nam) then
976 -- Catch error cases (such as attempting to rename a procedure
977 -- or package) using the shorthand form.
980 or else Etype (Nam) = Standard_Void_Type
983 ("object name or value expected in renaming", Nam);
985 Mutate_Ekind (Id, E_Variable);
986 Set_Etype (Id, Any_Type);
994 -- Case of overloaded name, which will be illegal if there's more
995 -- than one acceptable interpretation (such as overloaded function
1007 -- More than one candidate interpretation is available
1009 -- Remove procedure calls, which syntactically cannot appear
1010 -- in this context, but which cannot be removed by type
1011 -- checking, because the context does not impose a type.
1013 Get_First_Interp (Nam, I, It);
1014 while Present (It.Typ) loop
1015 if It.Typ = Standard_Void_Type then
1019 Get_Next_Interp (I, It);
1022 Get_First_Interp (Nam, I, It);
1026 -- If there's no type present, we have an error case (such
1027 -- as overloaded procedures named in the object renaming).
1031 ("object name or value expected in renaming", Nam);
1033 Mutate_Ekind (Id, E_Variable);
1034 Set_Etype (Id, Any_Type);
1039 Get_Next_Interp (I, It);
1041 if Present (It.Typ) then
1043 It1 := Disambiguate (Nam, I1, I, Any_Type);
1045 if It1 = No_Interp then
1046 Error_Msg_N ("ambiguous name in object renaming", Nam);
1048 Error_Msg_Sloc := Sloc (It.Nam);
1049 Error_Msg_N ("\\possible interpretation#!", Nam);
1051 Error_Msg_Sloc := Sloc (Nam1);
1052 Error_Msg_N ("\\possible interpretation#!", Nam);
1058 Set_Etype (Nam, It1.Typ);
1063 if Etype (Nam) = Standard_Exception_Type then
1065 ("exception requires a subtype mark in renaming", Nam);
1070 -- The object renaming declaration may become Ghost if it renames a
1073 if Is_Entity_Name (Nam) then
1074 Mark_Ghost_Renaming (N, Entity (Nam));
1077 -- Check against AI12-0401 here before Resolve may rewrite Nam and
1078 -- potentially generate spurious warnings.
1080 -- In the case where the object_name is a qualified_expression with
1081 -- a nominal subtype T and whose expression is a name that denotes
1083 -- * if T is an elementary subtype, then:
1084 -- * Q shall be a constant other than a dereference of an access
1086 -- * the nominal subtype of Q shall be statically compatible with
1088 -- * T shall statically match the base subtype of its type if
1089 -- scalar, or the first subtype of its type if an access type.
1090 -- * if T is a composite subtype, then Q shall be known to be
1091 -- constrained or T shall statically match the first subtype of
1094 if Nkind (Nam) = N_Qualified_Expression
1095 and then Is_Object_Reference (Expression (Nam))
1097 Q := Expression (Nam);
1099 if (Is_Elementary_Type (T)
1101 not ((not Is_Variable (Q)
1102 and then Nkind (Q) /= N_Explicit_Dereference)
1103 or else Subtypes_Statically_Compatible (Etype (Q), T)
1104 or else (Is_Scalar_Type (T)
1105 and then Subtypes_Statically_Match
1107 or else (Is_Access_Type (T)
1108 and then Subtypes_Statically_Match
1109 (T, First_Subtype (T)))))
1110 or else (Is_Composite_Type (T)
1113 -- If Q is an aggregate, Is_Constrained may not be set
1114 -- yet and its type may not be resolved yet.
1115 -- This doesn't quite correspond to the complex notion
1116 -- of "known to be constrained" but this is good enough
1117 -- for a rule which is in any case too complex.
1119 not (Is_Constrained (Etype (Q))
1120 or else Nkind (Q) = N_Aggregate
1121 or else Subtypes_Statically_Match
1122 (T, First_Subtype (T))))
1125 ("subtype of renamed qualified expression does not " &
1126 "statically match", N);
1133 -- If the renamed object is a function call of a limited type,
1134 -- the expansion of the renaming is complicated by the presence
1135 -- of various temporaries and subtypes that capture constraints
1136 -- of the renamed object. Rewrite node as an object declaration,
1137 -- whose expansion is simpler. Given that the object is limited
1138 -- there is no copy involved and no performance hit.
1140 if Nkind (Nam) = N_Function_Call
1141 and then Is_Limited_View (Etype (Nam))
1142 and then not Is_Constrained (Etype (Nam))
1143 and then Comes_From_Source (N)
1146 Mutate_Ekind (Id, E_Constant);
1148 Make_Object_Declaration (Loc,
1149 Defining_Identifier => Id,
1150 Constant_Present => True,
1151 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
1152 Expression => Relocate_Node (Nam)));
1156 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
1157 -- when renaming declaration has a named access type. The Ada 2012
1158 -- coverage rules allow an anonymous access type in the context of
1159 -- an expected named general access type, but the renaming rules
1160 -- require the types to be the same. (An exception is when the type
1161 -- of the renaming is also an anonymous access type, which can only
1162 -- happen due to a renaming created by the expander.)
1164 if Nkind (Nam) = N_Type_Conversion
1165 and then not Comes_From_Source (Nam)
1166 and then Is_Anonymous_Access_Type (Etype (Expression (Nam)))
1167 and then not Is_Anonymous_Access_Type (T)
1170 ("cannot rename anonymous access object "
1171 & "as a named access type", Expression (Nam), T);
1174 -- Check that a class-wide object is not being renamed as an object
1175 -- of a specific type. The test for access types is needed to exclude
1176 -- cases where the renamed object is a dynamically tagged access
1177 -- result, such as occurs in certain expansions.
1179 if Is_Tagged_Type (T) then
1180 Check_Dynamically_Tagged_Expression
1186 -- Ada 2005 (AI-230/AI-254): Access renaming
1188 else pragma Assert (Present (Access_Definition (N)));
1192 N => Access_Definition (N));
1196 -- The object renaming declaration may become Ghost if it renames a
1199 if Is_Entity_Name (Nam) then
1200 Mark_Ghost_Renaming (N, Entity (Nam));
1203 -- Ada 2005 AI05-105: if the declaration has an anonymous access
1204 -- type, the renamed object must also have an anonymous type, and
1205 -- this is a name resolution rule. This was implicit in the last part
1206 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
1209 if not Is_Overloaded (Nam) then
1210 if Ekind (Etype (Nam)) /= Ekind (T) then
1212 ("expect anonymous access type in object renaming", N);
1219 Typ : Entity_Id := Empty;
1220 Seen : Boolean := False;
1223 Get_First_Interp (Nam, I, It);
1224 while Present (It.Typ) loop
1226 -- Renaming is ambiguous if more than one candidate
1227 -- interpretation is type-conformant with the context.
1229 if Ekind (It.Typ) = Ekind (T) then
1230 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
1233 (Designated_Type (T), Designated_Type (It.Typ))
1239 ("ambiguous expression in renaming", Nam);
1242 elsif Ekind (T) = E_Anonymous_Access_Type
1244 Covers (Designated_Type (T), Designated_Type (It.Typ))
1250 ("ambiguous expression in renaming", Nam);
1254 if Covers (T, It.Typ) then
1256 Set_Etype (Nam, Typ);
1257 Set_Is_Overloaded (Nam, False);
1261 Get_Next_Interp (I, It);
1268 -- Do not perform the legality checks below when the resolution of
1269 -- the renaming name failed because the associated type is Any_Type.
1271 if Etype (Nam) = Any_Type then
1274 -- Ada 2005 (AI-231): In the case where the type is defined by an
1275 -- access_definition, the renamed entity shall be of an access-to-
1276 -- constant type if and only if the access_definition defines an
1277 -- access-to-constant type. ARM 8.5.1(4)
1279 elsif Constant_Present (Access_Definition (N))
1280 and then not Is_Access_Constant (Etype (Nam))
1283 ("(Ada 2005): the renamed object is not access-to-constant "
1284 & "(RM 8.5.1(6))", N);
1286 elsif not Constant_Present (Access_Definition (N))
1287 and then Is_Access_Constant (Etype (Nam))
1290 ("(Ada 2005): the renamed object is not access-to-variable "
1291 & "(RM 8.5.1(6))", N);
1294 if Is_Access_Subprogram_Type (Etype (Nam)) then
1295 Check_Subtype_Conformant
1296 (Designated_Type (T), Designated_Type (Etype (Nam)));
1298 elsif not Subtypes_Statically_Match
1299 (Designated_Type (T),
1300 Available_View (Designated_Type (Etype (Nam))))
1303 ("subtype of renamed object does not statically match", N);
1307 -- Special processing for renaming function return object. Some errors
1308 -- and warnings are produced only for calls that come from source.
1310 if Nkind (Nam) = N_Function_Call then
1313 -- Usage is illegal in Ada 83, but renamings are also introduced
1314 -- during expansion, and error does not apply to those.
1317 if Comes_From_Source (N) then
1319 ("(Ada 83) cannot rename function return object", Nam);
1322 -- In Ada 95, warn for odd case of renaming parameterless function
1323 -- call if this is not a limited type (where this is useful).
1326 if Warn_On_Object_Renames_Function
1327 and then No (Parameter_Associations (Nam))
1328 and then not Is_Limited_Type (Etype (Nam))
1329 and then Comes_From_Source (Nam)
1332 ("renaming function result object is suspicious?R?", Nam);
1334 ("\function & will be called only once?R?", Nam,
1335 Entity (Name (Nam)));
1336 Error_Msg_N -- CODEFIX
1337 ("\suggest using an initialized constant object "
1338 & "instead?R?", Nam);
1343 Check_Constrained_Object;
1345 -- An object renaming requires an exact match of the type. Class-wide
1346 -- matching is not allowed.
1348 if Is_Class_Wide_Type (T)
1349 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1351 Wrong_Type (Nam, T);
1354 -- We must search for an actual subtype here so that the bounds of
1355 -- objects of unconstrained types don't get dropped on the floor - such
1356 -- as with renamings of formal parameters.
1358 T2 := Get_Actual_Subtype_If_Available (Nam);
1360 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1362 if Nkind (Nam) = N_Explicit_Dereference
1363 and then Ekind (Etype (T2)) = E_Incomplete_Type
1365 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1368 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1369 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1373 if Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1375 Nam_Ent : constant Entity_Id := Entity (Get_Object_Name (Nam));
1376 Nam_Decl : constant Node_Id := Declaration_Node (Nam_Ent);
1379 if Has_Null_Exclusion (N)
1380 and then not Has_Null_Exclusion (Nam_Decl)
1382 -- Ada 2005 (AI-423): If the object name denotes a generic
1383 -- formal object of a generic unit G, and the object renaming
1384 -- declaration occurs within the body of G or within the body
1385 -- of a generic unit declared within the declarative region
1386 -- of G, then the declaration of the formal object of G must
1387 -- have a null exclusion or a null-excluding subtype.
1389 if Is_Formal_Object (Nam_Ent)
1390 and then In_Generic_Scope (Id)
1392 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1394 ("object does not exclude `NULL` "
1395 & "(RM 8.5.1(4.6/2))", N);
1397 elsif In_Package_Body (Scope (Id)) then
1399 ("formal object does not have a null exclusion"
1400 & "(RM 8.5.1(4.6/2))", N);
1403 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1404 -- shall exclude null.
1406 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1408 ("object does not exclude `NULL` "
1409 & "(RM 8.5.1(4.6/2))", N);
1411 -- An instance is illegal if it contains a renaming that
1412 -- excludes null, and the actual does not. The renaming
1413 -- declaration has already indicated that the declaration
1414 -- of the renamed actual in the instance will raise
1415 -- constraint_error.
1417 elsif Nkind (Nam_Decl) = N_Object_Declaration
1418 and then In_Instance
1420 Present (Corresponding_Generic_Association (Nam_Decl))
1421 and then Nkind (Expression (Nam_Decl)) =
1422 N_Raise_Constraint_Error
1425 ("actual does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1427 -- Finally, if there is a null exclusion, the subtype mark
1428 -- must not be null-excluding.
1430 elsif No (Access_Definition (N))
1431 and then Can_Never_Be_Null (T)
1434 ("`NOT NULL` not allowed (& already excludes null)",
1439 elsif Can_Never_Be_Null (T)
1440 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1443 ("object does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1445 elsif Has_Null_Exclusion (N)
1446 and then No (Access_Definition (N))
1447 and then Can_Never_Be_Null (T)
1450 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1455 -- Set the Ekind of the entity, unless it has been set already, as is
1456 -- the case for the iteration object over a container with no variable
1457 -- indexing. In that case it's been marked as a constant, and we do not
1458 -- want to change it to a variable.
1460 if Ekind (Id) /= E_Constant then
1461 Mutate_Ekind (Id, E_Variable);
1464 Init_Object_Size_Align (Id);
1466 -- If N comes from source then check that the original node is an
1467 -- object reference since there may have been several rewritting and
1468 -- folding. Do not do this for N_Function_Call or N_Explicit_Dereference
1469 -- which might correspond to rewrites of e.g. N_Selected_Component
1470 -- (for example Object.Method rewriting).
1471 -- If N does not come from source then assume the tree is properly
1472 -- formed and accept any object reference. In such cases we do support
1473 -- more cases of renamings anyway, so the actual check on which renaming
1474 -- is valid is better left to the code generator as a last sanity
1477 if Comes_From_Source (N) then
1478 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference then
1479 Is_Object_Ref := Is_Object_Reference (Nam);
1481 Is_Object_Ref := Is_Object_Reference (Original_Node (Nam));
1484 Is_Object_Ref := True;
1487 if T = Any_Type or else Etype (Nam) = Any_Type then
1490 -- Verify that the renamed entity is an object or function call
1492 elsif Is_Object_Ref then
1493 if Comes_From_Source (N) then
1494 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1496 ("illegal renaming of discriminant-dependent component", Nam);
1499 -- If the renaming comes from source and the renamed object is a
1500 -- dereference, then mark the prefix as needing debug information,
1501 -- since it might have been rewritten hence internally generated
1502 -- and Debug_Renaming_Declaration will link the renaming to it.
1504 if Nkind (Nam) = N_Explicit_Dereference
1505 and then Is_Entity_Name (Prefix (Nam))
1507 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1511 -- Weird but legal, equivalent to renaming a function call. Illegal
1512 -- if the literal is the result of constant-folding an attribute
1513 -- reference that is not a function.
1515 elsif Is_Entity_Name (Nam)
1516 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1517 and then Nkind (Original_Node (Nam)) /= N_Attribute_Reference
1521 -- A named number can only be renamed without a subtype mark
1523 elsif Nkind (Nam) in N_Real_Literal | N_Integer_Literal
1524 and then Present (Subtype_Mark (N))
1525 and then Present (Original_Entity (Nam))
1527 Error_Msg_N ("incompatible types in renaming", Nam);
1529 -- AI12-0383: Names that denote values can be renamed.
1530 -- Ignore (accept) N_Raise_xxx_Error nodes in this context.
1532 elsif No_Raise_xxx_Error (Nam) = OK then
1533 Error_Msg_Ada_2022_Feature ("value in renaming", Sloc (Nam));
1538 if not Is_Variable (Nam) then
1539 Mutate_Ekind (Id, E_Constant);
1540 Set_Never_Set_In_Source (Id, True);
1541 Set_Is_True_Constant (Id, True);
1544 -- The entity of the renaming declaration needs to reflect whether the
1545 -- renamed object is atomic, independent, volatile or VFA. These flags
1546 -- are set on the renamed object in the RM legality sense.
1548 Set_Is_Atomic (Id, Is_Atomic_Object (Nam));
1549 Set_Is_Independent (Id, Is_Independent_Object (Nam));
1550 Set_Is_Volatile (Id, Is_Volatile_Object_Ref (Nam));
1551 Set_Is_Volatile_Full_Access
1552 (Id, Is_Volatile_Full_Access_Object_Ref (Nam));
1554 -- Treat as volatile if we just set the Volatile flag
1558 -- Or if we are renaming an entity which was marked this way
1560 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1562 or else (Is_Entity_Name (Nam)
1563 and then Treat_As_Volatile (Entity (Nam)))
1565 Set_Treat_As_Volatile (Id, True);
1568 -- Now make the link to the renamed object
1570 Set_Renamed_Object (Id, Nam);
1572 -- Implementation-defined aspect specifications can appear in a renaming
1573 -- declaration, but not language-defined ones. The call to procedure
1574 -- Analyze_Aspect_Specifications will take care of this error check.
1576 if Has_Aspects (N) then
1577 Analyze_Aspect_Specifications (N, Id);
1580 -- Deal with dimensions
1582 Analyze_Dimension (N);
1583 end Analyze_Object_Renaming;
1585 ------------------------------
1586 -- Analyze_Package_Renaming --
1587 ------------------------------
1589 procedure Analyze_Package_Renaming (N : Node_Id) is
1590 New_P : constant Entity_Id := Defining_Entity (N);
1595 if Name (N) = Error then
1599 -- Check for Text_IO special unit (we may be renaming a Text_IO child)
1601 Check_Text_IO_Special_Unit (Name (N));
1603 if Current_Scope /= Standard_Standard then
1604 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1610 if Is_Entity_Name (Name (N)) then
1611 Old_P := Entity (Name (N));
1616 if Etype (Old_P) = Any_Type then
1617 Error_Msg_N ("expect package name in renaming", Name (N));
1619 elsif Ekind (Old_P) /= E_Package
1620 and then not (Ekind (Old_P) = E_Generic_Package
1621 and then In_Open_Scopes (Old_P))
1623 if Ekind (Old_P) = E_Generic_Package then
1625 ("generic package cannot be renamed as a package", Name (N));
1627 Error_Msg_Sloc := Sloc (Old_P);
1629 ("expect package name in renaming, found& declared#",
1633 -- Set basic attributes to minimize cascaded errors
1635 Mutate_Ekind (New_P, E_Package);
1636 Set_Etype (New_P, Standard_Void_Type);
1638 elsif Present (Renamed_Entity (Old_P))
1639 and then (From_Limited_With (Renamed_Entity (Old_P))
1640 or else Has_Limited_View (Renamed_Entity (Old_P)))
1642 Unit_Is_Visible (Cunit (Get_Source_Unit (Renamed_Entity (Old_P))))
1645 ("renaming of limited view of package & not usable in this context"
1646 & " (RM 8.5.3(3.1/2))", Name (N), Renamed_Entity (Old_P));
1648 -- Set basic attributes to minimize cascaded errors
1650 Mutate_Ekind (New_P, E_Package);
1651 Set_Etype (New_P, Standard_Void_Type);
1653 -- Here for OK package renaming
1656 -- Entities in the old package are accessible through the renaming
1657 -- entity. The simplest implementation is to have both packages share
1660 Mutate_Ekind (New_P, E_Package);
1661 Set_Etype (New_P, Standard_Void_Type);
1663 if Present (Renamed_Object (Old_P)) then
1664 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1666 Set_Renamed_Object (New_P, Old_P);
1669 -- The package renaming declaration may become Ghost if it renames a
1672 Mark_Ghost_Renaming (N, Old_P);
1674 Set_Has_Completion (New_P);
1675 Set_First_Entity (New_P, First_Entity (Old_P));
1676 Set_Last_Entity (New_P, Last_Entity (Old_P));
1677 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1678 Check_Library_Unit_Renaming (N, Old_P);
1679 Generate_Reference (Old_P, Name (N));
1681 -- If the renaming is in the visible part of a package, then we set
1682 -- Renamed_In_Spec for the renamed package, to prevent giving
1683 -- warnings about no entities referenced. Such a warning would be
1684 -- overenthusiastic, since clients can see entities in the renamed
1685 -- package via the visible package renaming.
1688 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1690 if Ekind (Ent) = E_Package
1691 and then not In_Private_Part (Ent)
1692 and then In_Extended_Main_Source_Unit (N)
1693 and then Ekind (Old_P) = E_Package
1695 Set_Renamed_In_Spec (Old_P);
1699 -- If this is the renaming declaration of a package instantiation
1700 -- within itself, it is the declaration that ends the list of actuals
1701 -- for the instantiation. At this point, the subtypes that rename
1702 -- the actuals are flagged as generic, to avoid spurious ambiguities
1703 -- if the actuals for two distinct formals happen to coincide. If
1704 -- the actual is a private type, the subtype has a private completion
1705 -- that is flagged in the same fashion.
1707 -- Resolution is identical to what is was in the original generic.
1708 -- On exit from the generic instance, these are turned into regular
1709 -- subtypes again, so they are compatible with types in their class.
1711 if not Is_Generic_Instance (Old_P) then
1714 Spec := Specification (Unit_Declaration_Node (Old_P));
1717 if Nkind (Spec) = N_Package_Specification
1718 and then Present (Generic_Parent (Spec))
1719 and then Old_P = Current_Scope
1720 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1726 E := First_Entity (Old_P);
1727 while Present (E) and then E /= New_P loop
1729 and then Nkind (Parent (E)) = N_Subtype_Declaration
1731 Set_Is_Generic_Actual_Type (E);
1733 if Is_Private_Type (E)
1734 and then Present (Full_View (E))
1736 Set_Is_Generic_Actual_Type (Full_View (E));
1746 -- Implementation-defined aspect specifications can appear in a renaming
1747 -- declaration, but not language-defined ones. The call to procedure
1748 -- Analyze_Aspect_Specifications will take care of this error check.
1750 if Has_Aspects (N) then
1751 Analyze_Aspect_Specifications (N, New_P);
1753 end Analyze_Package_Renaming;
1755 -------------------------------
1756 -- Analyze_Renamed_Character --
1757 -------------------------------
1759 procedure Analyze_Renamed_Character
1764 C : constant Node_Id := Name (N);
1767 if Ekind (New_S) = E_Function then
1768 Resolve (C, Etype (New_S));
1771 Check_Frozen_Renaming (N, New_S);
1775 Error_Msg_N ("character literal can only be renamed as function", N);
1777 end Analyze_Renamed_Character;
1779 ---------------------------------
1780 -- Analyze_Renamed_Dereference --
1781 ---------------------------------
1783 procedure Analyze_Renamed_Dereference
1788 Nam : constant Node_Id := Name (N);
1789 P : constant Node_Id := Prefix (Nam);
1795 if not Is_Overloaded (P) then
1796 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1797 or else not Type_Conformant (Etype (Nam), New_S)
1799 Error_Msg_N ("designated type does not match specification", P);
1808 Get_First_Interp (Nam, Ind, It);
1810 while Present (It.Nam) loop
1812 if Ekind (It.Nam) = E_Subprogram_Type
1813 and then Type_Conformant (It.Nam, New_S)
1815 if Typ /= Any_Id then
1816 Error_Msg_N ("ambiguous renaming", P);
1823 Get_Next_Interp (Ind, It);
1826 if Typ = Any_Type then
1827 Error_Msg_N ("designated type does not match specification", P);
1832 Check_Frozen_Renaming (N, New_S);
1836 end Analyze_Renamed_Dereference;
1838 ---------------------------
1839 -- Analyze_Renamed_Entry --
1840 ---------------------------
1842 procedure Analyze_Renamed_Entry
1847 Nam : constant Node_Id := Name (N);
1848 Sel : constant Node_Id := Selector_Name (Nam);
1849 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1853 if Entity (Sel) = Any_Id then
1855 -- Selector is undefined on prefix. Error emitted already
1857 Set_Has_Completion (New_S);
1861 -- Otherwise find renamed entity and build body of New_S as a call to it
1863 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1865 if Old_S = Any_Id then
1866 Error_Msg_N ("no subprogram or entry matches specification", N);
1869 Check_Subtype_Conformant (New_S, Old_S, N);
1870 Generate_Reference (New_S, Defining_Entity (N), 'b');
1871 Style.Check_Identifier (Defining_Entity (N), New_S);
1874 -- Only mode conformance required for a renaming_as_declaration
1876 Check_Mode_Conformant (New_S, Old_S, N);
1879 Inherit_Renamed_Profile (New_S, Old_S);
1881 -- The prefix can be an arbitrary expression that yields a task or
1882 -- protected object, so it must be resolved.
1884 if Is_Access_Type (Etype (Prefix (Nam))) then
1885 Insert_Explicit_Dereference (Prefix (Nam));
1887 Resolve (Prefix (Nam), Scope (Old_S));
1890 Set_Convention (New_S, Convention (Old_S));
1891 Set_Has_Completion (New_S, Inside_A_Generic);
1893 -- AI05-0225: If the renamed entity is a procedure or entry of a
1894 -- protected object, the target object must be a variable.
1896 if Is_Protected_Type (Scope (Old_S))
1897 and then Ekind (New_S) = E_Procedure
1898 and then not Is_Variable (Prefix (Nam))
1902 ("target object of protected operation used as actual for "
1903 & "formal procedure must be a variable", Nam);
1906 ("target object of protected operation renamed as procedure, "
1907 & "must be a variable", Nam);
1912 Check_Frozen_Renaming (N, New_S);
1914 end Analyze_Renamed_Entry;
1916 -----------------------------------
1917 -- Analyze_Renamed_Family_Member --
1918 -----------------------------------
1920 procedure Analyze_Renamed_Family_Member
1925 Nam : constant Node_Id := Name (N);
1926 P : constant Node_Id := Prefix (Nam);
1930 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1931 or else (Nkind (P) = N_Selected_Component
1932 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1934 if Is_Entity_Name (P) then
1935 Old_S := Entity (P);
1937 Old_S := Entity (Selector_Name (P));
1940 if not Entity_Matches_Spec (Old_S, New_S) then
1941 Error_Msg_N ("entry family does not match specification", N);
1944 Check_Subtype_Conformant (New_S, Old_S, N);
1945 Generate_Reference (New_S, Defining_Entity (N), 'b');
1946 Style.Check_Identifier (Defining_Entity (N), New_S);
1950 Error_Msg_N ("no entry family matches specification", N);
1953 Set_Has_Completion (New_S, Inside_A_Generic);
1956 Check_Frozen_Renaming (N, New_S);
1958 end Analyze_Renamed_Family_Member;
1960 -----------------------------------------
1961 -- Analyze_Renamed_Primitive_Operation --
1962 -----------------------------------------
1964 procedure Analyze_Renamed_Primitive_Operation
1974 Ctyp : Conformance_Type) return Boolean;
1975 -- Verify that the signatures of the renamed entity and the new entity
1976 -- match. The first formal of the renamed entity is skipped because it
1977 -- is the target object in any subsequent call.
1985 Ctyp : Conformance_Type) return Boolean
1991 if Ekind (Subp) /= Ekind (New_S) then
1995 Old_F := Next_Formal (First_Formal (Subp));
1996 New_F := First_Formal (New_S);
1997 while Present (Old_F) and then Present (New_F) loop
1998 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
2002 if Ctyp >= Mode_Conformant
2003 and then Ekind (Old_F) /= Ekind (New_F)
2008 Next_Formal (New_F);
2009 Next_Formal (Old_F);
2015 -- Start of processing for Analyze_Renamed_Primitive_Operation
2018 if not Is_Overloaded (Selector_Name (Name (N))) then
2019 Old_S := Entity (Selector_Name (Name (N)));
2021 if not Conforms (Old_S, Type_Conformant) then
2026 -- Find the operation that matches the given signature
2034 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
2036 while Present (It.Nam) loop
2037 if Conforms (It.Nam, Type_Conformant) then
2041 Get_Next_Interp (Ind, It);
2046 if Old_S = Any_Id then
2047 Error_Msg_N ("no subprogram or entry matches specification", N);
2051 if not Conforms (Old_S, Subtype_Conformant) then
2052 Error_Msg_N ("subtype conformance error in renaming", N);
2055 Generate_Reference (New_S, Defining_Entity (N), 'b');
2056 Style.Check_Identifier (Defining_Entity (N), New_S);
2059 -- Only mode conformance required for a renaming_as_declaration
2061 if not Conforms (Old_S, Mode_Conformant) then
2062 Error_Msg_N ("mode conformance error in renaming", N);
2065 -- AI12-0204: The prefix of a prefixed view that is renamed or
2066 -- passed as a formal subprogram must be renamable as an object.
2068 Nam := Prefix (Name (N));
2070 if Is_Object_Reference (Nam) then
2071 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
2073 ("illegal renaming of discriminant-dependent component",
2077 Error_Msg_N ("expect object name in renaming", Nam);
2080 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
2081 -- view of a subprogram is intrinsic, because the compiler has
2082 -- to generate a wrapper for any call to it. If the name in a
2083 -- subprogram renaming is a prefixed view, the entity is thus
2084 -- intrinsic, and 'Access cannot be applied to it.
2086 Set_Convention (New_S, Convention_Intrinsic);
2089 -- Inherit_Renamed_Profile (New_S, Old_S);
2091 -- The prefix can be an arbitrary expression that yields an
2092 -- object, so it must be resolved.
2094 Resolve (Prefix (Name (N)));
2096 end Analyze_Renamed_Primitive_Operation;
2098 ---------------------------------
2099 -- Analyze_Subprogram_Renaming --
2100 ---------------------------------
2102 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
2103 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
2104 Is_Actual : constant Boolean := Present (Formal_Spec);
2105 Nam : constant Node_Id := Name (N);
2106 Save_AV : constant Ada_Version_Type := Ada_Version;
2107 Save_AVP : constant Node_Id := Ada_Version_Pragma;
2108 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
2109 Spec : constant Node_Id := Specification (N);
2111 Old_S : Entity_Id := Empty;
2112 Rename_Spec : Entity_Id;
2114 procedure Build_Class_Wide_Wrapper
2115 (Ren_Id : out Entity_Id;
2116 Wrap_Id : out Entity_Id);
2117 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal
2118 -- type with unknown discriminants and a generic primitive operation of
2119 -- the said type with a box require special processing when the actual
2120 -- is a class-wide type:
2123 -- type Formal_Typ (<>) is private;
2124 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
2125 -- package Gen is ...
2127 -- package Inst is new Gen (Actual_Typ'Class);
2129 -- In this case the general renaming mechanism used in the prologue of
2130 -- an instance no longer applies:
2132 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
2134 -- The above is replaced the following wrapper/renaming combination:
2136 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
2138 -- Prim_Op (Param); -- primitive
2141 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
2143 -- This transformation applies only if there is no explicit visible
2144 -- class-wide operation at the point of the instantiation. Ren_Id is
2145 -- the entity of the renaming declaration. When the transformation
2146 -- applies, Wrap_Id is the entity of the generated class-wide wrapper
2147 -- (or Any_Id). Otherwise, Wrap_Id is the entity of the class-wide
2150 procedure Check_Null_Exclusion
2153 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
2154 -- following AI rules:
2156 -- If Ren denotes a generic formal object of a generic unit G, and the
2157 -- renaming (or instantiation containing the actual) occurs within the
2158 -- body of G or within the body of a generic unit declared within the
2159 -- declarative region of G, then the corresponding parameter of G
2160 -- shall have a null_exclusion; Otherwise the subtype of the Sub's
2161 -- formal parameter shall exclude null.
2163 -- Similarly for its return profile.
2165 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id);
2166 -- Ensure that a SPARK renaming denoted by its entity Subp_Id does not
2167 -- declare a primitive operation of a tagged type (SPARK RM 6.1.1(3)).
2169 procedure Freeze_Actual_Profile;
2170 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
2171 -- types: a callable entity freezes its profile, unless it has an
2172 -- incomplete untagged formal (RM 13.14(10.2/3)).
2174 function Has_Class_Wide_Actual return Boolean;
2175 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
2176 -- defaulted formal subprogram where the actual for the controlling
2177 -- formal type is class-wide.
2179 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
2180 -- Find renamed entity when the declaration is a renaming_as_body and
2181 -- the renamed entity may itself be a renaming_as_body. Used to enforce
2182 -- rule that a renaming_as_body is illegal if the declaration occurs
2183 -- before the subprogram it completes is frozen, and renaming indirectly
2184 -- renames the subprogram itself.(Defect Report 8652/0027).
2186 ------------------------------
2187 -- Build_Class_Wide_Wrapper --
2188 ------------------------------
2190 procedure Build_Class_Wide_Wrapper
2191 (Ren_Id : out Entity_Id;
2192 Wrap_Id : out Entity_Id)
2194 Loc : constant Source_Ptr := Sloc (N);
2197 (Subp_Id : Entity_Id;
2198 Params : List_Id) return Node_Id;
2199 -- Create a dispatching call to invoke routine Subp_Id with actuals
2200 -- built from the parameter specifications of list Params.
2202 function Build_Expr_Fun_Call
2203 (Subp_Id : Entity_Id;
2204 Params : List_Id) return Node_Id;
2205 -- Create a dispatching call to invoke function Subp_Id with actuals
2206 -- built from the parameter specifications of list Params. Return
2207 -- directly the call, so that it can be used inside an expression
2208 -- function. This is a specificity of the GNATprove mode.
2210 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
2211 -- Create a subprogram specification based on the subprogram profile
2214 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
2215 -- Find a primitive subprogram of type Typ which matches the profile
2216 -- of the renaming declaration.
2218 procedure Interpretation_Error (Subp_Id : Entity_Id);
2219 -- Emit a continuation error message suggesting subprogram Subp_Id as
2220 -- a possible interpretation.
2222 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean;
2223 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
2226 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean;
2227 -- Determine whether subprogram Subp_Id is a suitable candidate for
2228 -- the role of a wrapped subprogram.
2235 (Subp_Id : Entity_Id;
2236 Params : List_Id) return Node_Id
2238 Actuals : constant List_Id := New_List;
2239 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2243 -- Build the actual parameters of the call
2245 Formal := First (Params);
2246 while Present (Formal) loop
2248 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2253 -- return Subp_Id (Actuals);
2255 if Ekind (Subp_Id) in E_Function | E_Operator then
2257 Make_Simple_Return_Statement (Loc,
2259 Make_Function_Call (Loc,
2261 Parameter_Associations => Actuals));
2264 -- Subp_Id (Actuals);
2268 Make_Procedure_Call_Statement (Loc,
2270 Parameter_Associations => Actuals);
2274 -------------------------
2275 -- Build_Expr_Fun_Call --
2276 -------------------------
2278 function Build_Expr_Fun_Call
2279 (Subp_Id : Entity_Id;
2280 Params : List_Id) return Node_Id
2282 Actuals : constant List_Id := New_List;
2283 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2287 pragma Assert (Ekind (Subp_Id) in E_Function | E_Operator);
2289 -- Build the actual parameters of the call
2291 Formal := First (Params);
2292 while Present (Formal) loop
2294 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2299 -- Subp_Id (Actuals);
2302 Make_Function_Call (Loc,
2304 Parameter_Associations => Actuals);
2305 end Build_Expr_Fun_Call;
2311 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2312 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2313 Spec_Id : constant Entity_Id :=
2314 Make_Defining_Identifier (Loc,
2315 Chars => New_External_Name (Chars (Subp_Id), 'R'));
2318 if Ekind (Formal_Spec) = E_Procedure then
2320 Make_Procedure_Specification (Loc,
2321 Defining_Unit_Name => Spec_Id,
2322 Parameter_Specifications => Params);
2325 Make_Function_Specification (Loc,
2326 Defining_Unit_Name => Spec_Id,
2327 Parameter_Specifications => Params,
2328 Result_Definition =>
2329 New_Copy_Tree (Result_Definition (Spec)));
2333 --------------------
2334 -- Find_Primitive --
2335 --------------------
2337 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2338 procedure Replace_Parameter_Types (Spec : Node_Id);
2339 -- Given a specification Spec, replace all class-wide parameter
2340 -- types with reference to type Typ.
2342 -----------------------------
2343 -- Replace_Parameter_Types --
2344 -----------------------------
2346 procedure Replace_Parameter_Types (Spec : Node_Id) is
2348 Formal_Id : Entity_Id;
2349 Formal_Typ : Node_Id;
2352 Formal := First (Parameter_Specifications (Spec));
2353 while Present (Formal) loop
2354 Formal_Id := Defining_Identifier (Formal);
2355 Formal_Typ := Parameter_Type (Formal);
2357 -- Create a new entity for each class-wide formal to prevent
2358 -- aliasing with the original renaming. Replace the type of
2359 -- such a parameter with the candidate type.
2361 if Nkind (Formal_Typ) = N_Identifier
2362 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2364 Set_Defining_Identifier (Formal,
2365 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2367 Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc));
2372 end Replace_Parameter_Types;
2376 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2377 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2378 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2379 Subp_Id : Entity_Id;
2381 -- Start of processing for Find_Primitive
2384 -- Each attempt to find a suitable primitive of a particular type
2385 -- operates on its own copy of the original renaming. As a result
2386 -- the original renaming is kept decoration and side-effect free.
2388 -- Inherit the overloaded status of the renamed subprogram name
2390 if Is_Overloaded (Nam) then
2391 Set_Is_Overloaded (Alt_Nam);
2392 Save_Interps (Nam, Alt_Nam);
2395 -- The copied renaming is hidden from visibility to prevent the
2396 -- pollution of the enclosing context.
2398 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2400 -- The types of all class-wide parameters must be changed to the
2403 Replace_Parameter_Types (Alt_Spec);
2405 -- Try to find a suitable primitive which matches the altered
2406 -- profile of the renaming specification.
2411 Nam => Name (Alt_Ren),
2412 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2413 Is_Actual => Is_Actual);
2415 -- Do not return Any_Id if the resolion of the altered profile
2416 -- failed as this complicates further checks on the caller side,
2417 -- return Empty instead.
2419 if Subp_Id = Any_Id then
2426 --------------------------
2427 -- Interpretation_Error --
2428 --------------------------
2430 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2432 Error_Msg_Sloc := Sloc (Subp_Id);
2434 if Is_Internal (Subp_Id) then
2436 ("\\possible interpretation: predefined & #",
2440 ("\\possible interpretation: & defined #", Spec, Formal_Spec);
2442 end Interpretation_Error;
2444 ---------------------------
2445 -- Is_Intrinsic_Equality --
2446 ---------------------------
2448 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is
2451 Ekind (Subp_Id) = E_Operator
2452 and then Chars (Subp_Id) = Name_Op_Eq
2453 and then Is_Intrinsic_Subprogram (Subp_Id);
2454 end Is_Intrinsic_Equality;
2456 ---------------------------
2457 -- Is_Suitable_Candidate --
2458 ---------------------------
2460 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is
2462 if No (Subp_Id) then
2465 -- An intrinsic subprogram is never a good candidate. This is an
2466 -- indication of a missing primitive, either defined directly or
2467 -- inherited from a parent tagged type.
2469 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2475 end Is_Suitable_Candidate;
2479 Actual_Typ : Entity_Id := Empty;
2480 -- The actual class-wide type for Formal_Typ
2482 CW_Prim_OK : Boolean;
2483 CW_Prim_Op : Entity_Id;
2484 -- The class-wide subprogram (if available) which corresponds to the
2485 -- renamed generic formal subprogram.
2487 Formal_Typ : Entity_Id := Empty;
2488 -- The generic formal type with unknown discriminants
2490 Root_Prim_OK : Boolean;
2491 Root_Prim_Op : Entity_Id;
2492 -- The root type primitive (if available) which corresponds to the
2493 -- renamed generic formal subprogram.
2495 Root_Typ : Entity_Id := Empty;
2496 -- The root type of Actual_Typ
2498 Body_Decl : Node_Id;
2500 Prim_Op : Entity_Id;
2501 Spec_Decl : Node_Id;
2504 -- Start of processing for Build_Class_Wide_Wrapper
2507 -- Analyze the specification of the renaming in case the generation
2508 -- of the class-wide wrapper fails.
2510 Ren_Id := Analyze_Subprogram_Specification (Spec);
2513 -- Do not attempt to build a wrapper if the renaming is in error
2515 if Error_Posted (Nam) then
2519 -- Analyze the renamed name, but do not resolve it. The resolution is
2520 -- completed once a suitable subprogram is found.
2524 -- When the renamed name denotes the intrinsic operator equals, the
2525 -- name must be treated as overloaded. This allows for a potential
2526 -- match against the root type's predefined equality function.
2528 if Is_Intrinsic_Equality (Entity (Nam)) then
2529 Set_Is_Overloaded (Nam);
2530 Collect_Interps (Nam);
2533 -- Step 1: Find the generic formal type with unknown discriminants
2534 -- and its corresponding class-wide actual type from the renamed
2535 -- generic formal subprogram.
2537 Formal := First_Formal (Formal_Spec);
2538 while Present (Formal) loop
2539 if Has_Unknown_Discriminants (Etype (Formal))
2540 and then not Is_Class_Wide_Type (Etype (Formal))
2541 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2543 Formal_Typ := Etype (Formal);
2544 Actual_Typ := Get_Instance_Of (Formal_Typ);
2545 Root_Typ := Etype (Actual_Typ);
2549 Next_Formal (Formal);
2552 -- The specification of the generic formal subprogram should always
2553 -- contain a formal type with unknown discriminants whose actual is
2554 -- a class-wide type, otherwise this indicates a failure in routine
2555 -- Has_Class_Wide_Actual.
2557 pragma Assert (Present (Formal_Typ));
2559 -- Step 2: Find the proper class-wide subprogram or primitive which
2560 -- corresponds to the renamed generic formal subprogram.
2562 CW_Prim_Op := Find_Primitive (Actual_Typ);
2563 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2564 Root_Prim_Op := Find_Primitive (Root_Typ);
2565 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2567 -- The class-wide actual type has two subprograms which correspond to
2568 -- the renamed generic formal subprogram:
2570 -- with procedure Prim_Op (Param : Formal_Typ);
2572 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2573 -- procedure Prim_Op (Param : Actual_Typ'Class);
2575 -- Even though the declaration of the two subprograms is legal, a
2576 -- call to either one is ambiguous and therefore illegal.
2578 if CW_Prim_OK and Root_Prim_OK then
2580 -- A user-defined primitive has precedence over a predefined one
2582 if Is_Internal (CW_Prim_Op)
2583 and then not Is_Internal (Root_Prim_Op)
2585 Prim_Op := Root_Prim_Op;
2587 elsif Is_Internal (Root_Prim_Op)
2588 and then not Is_Internal (CW_Prim_Op)
2590 Prim_Op := CW_Prim_Op;
2592 elsif CW_Prim_Op = Root_Prim_Op then
2593 Prim_Op := Root_Prim_Op;
2595 -- Otherwise both candidate subprograms are user-defined and
2600 ("ambiguous actual for generic subprogram &",
2602 Interpretation_Error (Root_Prim_Op);
2603 Interpretation_Error (CW_Prim_Op);
2607 elsif CW_Prim_OK and not Root_Prim_OK then
2608 Prim_Op := CW_Prim_Op;
2610 elsif not CW_Prim_OK and Root_Prim_OK then
2611 Prim_Op := Root_Prim_Op;
2613 -- An intrinsic equality may act as a suitable candidate in the case
2614 -- of a null type extension where the parent's equality is hidden. A
2615 -- call to an intrinsic equality is expanded as dispatching.
2617 elsif Present (Root_Prim_Op)
2618 and then Is_Intrinsic_Equality (Root_Prim_Op)
2620 Prim_Op := Root_Prim_Op;
2622 -- Otherwise there are no candidate subprograms. Let the caller
2623 -- diagnose the error.
2629 -- At this point resolution has taken place and the name is no longer
2630 -- overloaded. Mark the primitive as referenced.
2632 Set_Is_Overloaded (Name (N), False);
2633 Set_Referenced (Prim_Op);
2635 -- Do not generate a wrapper when the only candidate is a class-wide
2636 -- subprogram. Instead modify the renaming to directly map the actual
2637 -- to the generic formal.
2639 if CW_Prim_OK and then Prim_Op = CW_Prim_Op then
2641 Rewrite (Nam, New_Occurrence_Of (Prim_Op, Loc));
2645 -- Step 3: Create the declaration and the body of the wrapper, insert
2646 -- all the pieces into the tree.
2648 -- In GNATprove mode, create a function wrapper in the form of an
2649 -- expression function, so that an implicit postcondition relating
2650 -- the result of calling the wrapper function and the result of the
2651 -- dispatching call to the wrapped function is known during proof.
2654 and then Ekind (Ren_Id) in E_Function | E_Operator
2656 New_Spec := Build_Spec (Ren_Id);
2658 Make_Expression_Function (Loc,
2659 Specification => New_Spec,
2662 (Subp_Id => Prim_Op,
2663 Params => Parameter_Specifications (New_Spec)));
2665 Wrap_Id := Defining_Entity (Body_Decl);
2667 -- Otherwise, create separate spec and body for the subprogram
2671 Make_Subprogram_Declaration (Loc,
2672 Specification => Build_Spec (Ren_Id));
2673 Insert_Before_And_Analyze (N, Spec_Decl);
2675 Wrap_Id := Defining_Entity (Spec_Decl);
2678 Make_Subprogram_Body (Loc,
2679 Specification => Build_Spec (Ren_Id),
2680 Declarations => New_List,
2681 Handled_Statement_Sequence =>
2682 Make_Handled_Sequence_Of_Statements (Loc,
2683 Statements => New_List (
2685 (Subp_Id => Prim_Op,
2687 Parameter_Specifications
2688 (Specification (Spec_Decl))))));
2690 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
2693 -- If the operator carries an Eliminated pragma, indicate that the
2694 -- wrapper is also to be eliminated, to prevent spurious error when
2695 -- using gnatelim on programs that include box-initialization of
2696 -- equality operators.
2698 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2700 -- In GNATprove mode, insert the body in the tree for analysis
2702 if GNATprove_Mode then
2703 Insert_Before_And_Analyze (N, Body_Decl);
2706 -- The generated body does not freeze and must be analyzed when the
2707 -- class-wide wrapper is frozen. The body is only needed if expansion
2710 if Expander_Active then
2711 Append_Freeze_Action (Wrap_Id, Body_Decl);
2714 -- Step 4: The subprogram renaming aliases the wrapper
2716 Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc));
2717 end Build_Class_Wide_Wrapper;
2719 --------------------------
2720 -- Check_Null_Exclusion --
2721 --------------------------
2723 procedure Check_Null_Exclusion
2727 Ren_Formal : Entity_Id;
2728 Sub_Formal : Entity_Id;
2730 function Null_Exclusion_Mismatch
2731 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean;
2732 -- Return True if there is a null exclusion mismatch between
2733 -- Renaming and Renamed, False otherwise.
2735 -----------------------------
2736 -- Null_Exclusion_Mismatch --
2737 -----------------------------
2739 function Null_Exclusion_Mismatch
2740 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean is
2742 return Has_Null_Exclusion (Parent (Renaming))
2744 not (Has_Null_Exclusion (Parent (Renamed))
2745 or else (Can_Never_Be_Null (Etype (Renamed))
2747 (Is_Formal_Subprogram (Sub)
2748 and then In_Generic_Body (Current_Scope))));
2749 end Null_Exclusion_Mismatch;
2754 Ren_Formal := First_Formal (Ren);
2755 Sub_Formal := First_Formal (Sub);
2756 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2757 if Null_Exclusion_Mismatch (Ren_Formal, Sub_Formal) then
2758 Error_Msg_Sloc := Sloc (Sub_Formal);
2760 ("`NOT NULL` required for parameter &#",
2761 Ren_Formal, Sub_Formal);
2764 Next_Formal (Ren_Formal);
2765 Next_Formal (Sub_Formal);
2768 -- Return profile check
2770 if Nkind (Parent (Ren)) = N_Function_Specification
2771 and then Nkind (Parent (Sub)) = N_Function_Specification
2772 and then Null_Exclusion_Mismatch (Ren, Sub)
2774 Error_Msg_Sloc := Sloc (Sub);
2775 Error_Msg_N ("return must specify `NOT NULL`#", Ren);
2777 end Check_Null_Exclusion;
2779 -------------------------------------
2780 -- Check_SPARK_Primitive_Operation --
2781 -------------------------------------
2783 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id) is
2784 Prag : constant Node_Id := SPARK_Pragma (Subp_Id);
2788 -- Nothing to do when the subprogram is not subject to SPARK_Mode On
2789 -- because this check applies to SPARK code only.
2791 if not (Present (Prag)
2792 and then Get_SPARK_Mode_From_Annotation (Prag) = On)
2796 -- Nothing to do when the subprogram is not a primitive operation
2798 elsif not Is_Primitive (Subp_Id) then
2802 Typ := Find_Dispatching_Type (Subp_Id);
2804 -- Nothing to do when the subprogram is a primitive operation of an
2811 -- At this point a renaming declaration introduces a new primitive
2812 -- operation for a tagged type.
2814 Error_Msg_Node_2 := Typ;
2816 ("subprogram renaming & cannot declare primitive for type & "
2817 & "(SPARK RM 6.1.1(3))", N, Subp_Id);
2818 end Check_SPARK_Primitive_Operation;
2820 ---------------------------
2821 -- Freeze_Actual_Profile --
2822 ---------------------------
2824 procedure Freeze_Actual_Profile is
2826 Has_Untagged_Inc : Boolean;
2827 Instantiation_Node : constant Node_Id := Parent (N);
2830 if Ada_Version >= Ada_2012 then
2831 F := First_Formal (Formal_Spec);
2832 Has_Untagged_Inc := False;
2833 while Present (F) loop
2834 if Ekind (Etype (F)) = E_Incomplete_Type
2835 and then not Is_Tagged_Type (Etype (F))
2837 Has_Untagged_Inc := True;
2844 if Ekind (Formal_Spec) = E_Function
2845 and then not Is_Tagged_Type (Etype (Formal_Spec))
2847 Has_Untagged_Inc := True;
2850 if not Has_Untagged_Inc then
2851 F := First_Formal (Old_S);
2852 while Present (F) loop
2853 Freeze_Before (Instantiation_Node, Etype (F));
2855 if Is_Incomplete_Or_Private_Type (Etype (F))
2856 and then No (Underlying_Type (Etype (F)))
2858 -- Exclude generic types, or types derived from them.
2859 -- They will be frozen in the enclosing instance.
2861 if Is_Generic_Type (Etype (F))
2862 or else Is_Generic_Type (Root_Type (Etype (F)))
2866 -- A limited view of a type declared elsewhere needs no
2867 -- freezing actions.
2869 elsif From_Limited_With (Etype (F)) then
2874 ("type& must be frozen before this point",
2875 Instantiation_Node, Etype (F));
2883 end Freeze_Actual_Profile;
2885 ---------------------------
2886 -- Has_Class_Wide_Actual --
2887 ---------------------------
2889 function Has_Class_Wide_Actual return Boolean is
2891 Formal_Typ : Entity_Id;
2895 Formal := First_Formal (Formal_Spec);
2896 while Present (Formal) loop
2897 Formal_Typ := Etype (Formal);
2899 if Has_Unknown_Discriminants (Formal_Typ)
2900 and then not Is_Class_Wide_Type (Formal_Typ)
2901 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2906 Next_Formal (Formal);
2911 end Has_Class_Wide_Actual;
2913 -------------------------
2914 -- Original_Subprogram --
2915 -------------------------
2917 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2918 Orig_Decl : Node_Id;
2919 Orig_Subp : Entity_Id;
2922 -- First case: renamed entity is itself a renaming
2924 if Present (Alias (Subp)) then
2925 return Alias (Subp);
2927 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2928 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2930 -- Check if renamed entity is a renaming_as_body
2933 Unit_Declaration_Node
2934 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2936 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2937 Orig_Subp := Entity (Name (Orig_Decl));
2939 if Orig_Subp = Rename_Spec then
2941 -- Circularity detected
2946 return (Original_Subprogram (Orig_Subp));
2954 end Original_Subprogram;
2958 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2959 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2960 -- defaulted formal subprogram when the actual for a related formal
2961 -- type is class-wide.
2963 Inst_Node : Node_Id := Empty;
2966 -- Start of processing for Analyze_Subprogram_Renaming
2969 -- We must test for the attribute renaming case before the Analyze
2970 -- call because otherwise Sem_Attr will complain that the attribute
2971 -- is missing an argument when it is analyzed.
2973 if Nkind (Nam) = N_Attribute_Reference then
2975 -- In the case of an abstract formal subprogram association, rewrite
2976 -- an actual given by a stream or Put_Image attribute as the name of
2977 -- the corresponding stream or Put_Image primitive of the type.
2979 -- In a generic context the stream and Put_Image operations are not
2980 -- generated, and this must be treated as a normal attribute
2981 -- reference, to be expanded in subsequent instantiations.
2984 and then Is_Abstract_Subprogram (Formal_Spec)
2985 and then Expander_Active
2988 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
2992 -- The class-wide forms of the stream and Put_Image attributes
2993 -- are not primitive dispatching operations (even though they
2994 -- internally dispatch).
2996 if Is_Class_Wide_Type (Prefix_Type) then
2998 ("attribute must be a primitive dispatching operation",
3003 -- Retrieve the primitive subprogram associated with the
3004 -- attribute. This can only be a stream attribute, since those
3005 -- are the only ones that are dispatching (and the actual for
3006 -- an abstract formal subprogram must be dispatching
3009 case Attribute_Name (Nam) is
3012 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input);
3016 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output);
3020 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read);
3024 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write);
3026 when Name_Put_Image =>
3028 Find_Optional_Prim_Op (Prefix_Type, TSS_Put_Image);
3032 ("attribute must be a primitive dispatching operation",
3037 -- If no stream operation was found, and the type is limited,
3038 -- the user should have defined one. This rule does not apply
3042 and then Attribute_Name (Nam) /= Name_Put_Image
3044 if Is_Limited_Type (Prefix_Type) then
3046 ("stream operation not defined for type&",
3050 -- Otherwise, compiler should have generated default
3053 raise Program_Error;
3057 -- Rewrite the attribute into the name of its corresponding
3058 -- primitive dispatching subprogram. We can then proceed with
3059 -- the usual processing for subprogram renamings.
3062 Prim_Name : constant Node_Id :=
3063 Make_Identifier (Sloc (Nam),
3064 Chars => Chars (Prim));
3066 Set_Entity (Prim_Name, Prim);
3067 Rewrite (Nam, Prim_Name);
3072 -- Normal processing for a renaming of an attribute
3075 Attribute_Renaming (N);
3080 -- Check whether this declaration corresponds to the instantiation of a
3081 -- formal subprogram.
3083 -- If this is an instantiation, the corresponding actual is frozen and
3084 -- error messages can be made more precise. If this is a default
3085 -- subprogram, the entity is already established in the generic, and is
3086 -- not retrieved by visibility. If it is a default with a box, the
3087 -- candidate interpretations, if any, have been collected when building
3088 -- the renaming declaration. If overloaded, the proper interpretation is
3089 -- determined in Find_Renamed_Entity. If the entity is an operator,
3090 -- Find_Renamed_Entity applies additional visibility checks.
3093 Inst_Node := Unit_Declaration_Node (Formal_Spec);
3095 -- Check whether the renaming is for a defaulted actual subprogram
3096 -- with a class-wide actual.
3098 if CW_Actual and then Box_Present (Inst_Node) then
3099 Build_Class_Wide_Wrapper (New_S, Old_S);
3101 elsif Is_Entity_Name (Nam)
3102 and then Present (Entity (Nam))
3103 and then not Comes_From_Source (Nam)
3104 and then not Is_Overloaded (Nam)
3106 Old_S := Entity (Nam);
3108 -- The subprogram renaming declaration may become Ghost if it
3109 -- renames a Ghost entity.
3111 Mark_Ghost_Renaming (N, Old_S);
3113 New_S := Analyze_Subprogram_Specification (Spec);
3117 if Ekind (Old_S) = E_Operator then
3121 if Box_Present (Inst_Node) then
3122 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3124 -- If there is an immediately visible homonym of the operator
3125 -- and the declaration has a default, this is worth a warning
3126 -- because the user probably did not intend to get the pre-
3127 -- defined operator, visible in the generic declaration. To
3128 -- find if there is an intended candidate, analyze the renaming
3129 -- again in the current context.
3131 elsif Scope (Old_S) = Standard_Standard
3132 and then Present (Default_Name (Inst_Node))
3135 Decl : constant Node_Id := New_Copy_Tree (N);
3139 Set_Entity (Name (Decl), Empty);
3140 Analyze (Name (Decl));
3142 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
3145 and then In_Open_Scopes (Scope (Hidden))
3146 and then Is_Immediately_Visible (Hidden)
3147 and then Comes_From_Source (Hidden)
3148 and then Hidden /= Old_S
3150 Error_Msg_Sloc := Sloc (Hidden);
3152 ("default subprogram is resolved in the generic "
3153 & "declaration (RM 12.6(17))??", N);
3154 Error_Msg_NE ("\and will not use & #??", N, Hidden);
3163 -- The subprogram renaming declaration may become Ghost if it
3164 -- renames a Ghost entity.
3166 if Is_Entity_Name (Nam) then
3167 Mark_Ghost_Renaming (N, Entity (Nam));
3170 New_S := Analyze_Subprogram_Specification (Spec);
3174 -- Renamed entity must be analyzed first, to avoid being hidden by
3175 -- new name (which might be the same in a generic instance).
3179 -- The subprogram renaming declaration may become Ghost if it renames
3182 if Is_Entity_Name (Nam) then
3183 Mark_Ghost_Renaming (N, Entity (Nam));
3186 -- The renaming defines a new overloaded entity, which is analyzed
3187 -- like a subprogram declaration.
3189 New_S := Analyze_Subprogram_Specification (Spec);
3192 if Current_Scope /= Standard_Standard then
3193 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
3196 -- Set SPARK mode from current context
3198 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
3199 Set_SPARK_Pragma_Inherited (New_S);
3201 Rename_Spec := Find_Corresponding_Spec (N);
3203 -- Case of Renaming_As_Body
3205 if Present (Rename_Spec) then
3206 Check_Previous_Null_Procedure (N, Rename_Spec);
3208 -- Renaming declaration is the completion of the declaration of
3209 -- Rename_Spec. We build an actual body for it at the freezing point.
3211 Set_Corresponding_Spec (N, Rename_Spec);
3213 -- Deal with special case of stream functions of abstract types
3216 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
3217 N_Abstract_Subprogram_Declaration
3219 -- Input stream functions are abstract if the object type is
3220 -- abstract. Similarly, all default stream functions for an
3221 -- interface type are abstract. However, these subprograms may
3222 -- receive explicit declarations in representation clauses, making
3223 -- the attribute subprograms usable as defaults in subsequent
3225 -- In this case we rewrite the declaration to make the subprogram
3226 -- non-abstract. We remove the previous declaration, and insert
3227 -- the new one at the point of the renaming, to prevent premature
3228 -- access to unfrozen types. The new declaration reuses the
3229 -- specification of the previous one, and must not be analyzed.
3232 (Is_Primitive (Entity (Nam))
3234 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
3236 Old_Decl : constant Node_Id :=
3237 Unit_Declaration_Node (Rename_Spec);
3238 New_Decl : constant Node_Id :=
3239 Make_Subprogram_Declaration (Sloc (N),
3241 Relocate_Node (Specification (Old_Decl)));
3244 Insert_After (N, New_Decl);
3245 Set_Is_Abstract_Subprogram (Rename_Spec, False);
3246 Set_Analyzed (New_Decl);
3250 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
3252 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3253 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
3256 Set_Convention (New_S, Convention (Rename_Spec));
3257 Check_Fully_Conformant (New_S, Rename_Spec);
3258 Set_Public_Status (New_S);
3260 if No_Return (Rename_Spec)
3261 and then not No_Return (Entity (Nam))
3264 ("renamed subprogram & must be No_Return", N, Entity (Nam));
3266 ("\since renaming subprogram is No_Return (RM 6.5.1(7/2))", N);
3269 -- The specification does not introduce new formals, but only
3270 -- repeats the formals of the original subprogram declaration.
3271 -- For cross-reference purposes, and for refactoring tools, we
3272 -- treat the formals of the renaming declaration as body formals.
3274 Reference_Body_Formals (Rename_Spec, New_S);
3276 -- Indicate that the entity in the declaration functions like the
3277 -- corresponding body, and is not a new entity. The body will be
3278 -- constructed later at the freeze point, so indicate that the
3279 -- completion has not been seen yet.
3281 Reinit_Field_To_Zero (New_S, F_Has_Out_Or_In_Out_Parameter);
3282 Reinit_Field_To_Zero (New_S, F_Needs_No_Actuals,
3283 Old_Ekind => (E_Function | E_Procedure => True, others => False));
3284 Mutate_Ekind (New_S, E_Subprogram_Body);
3285 New_S := Rename_Spec;
3286 Set_Has_Completion (Rename_Spec, False);
3288 -- Ada 2005: check overriding indicator
3290 if Present (Overridden_Operation (Rename_Spec)) then
3291 if Must_Not_Override (Specification (N)) then
3293 ("subprogram& overrides inherited operation",
3297 and then not Must_Override (Specification (N))
3299 Style.Missing_Overriding (N, Rename_Spec);
3302 elsif Must_Override (Specification (N)) then
3303 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
3306 -- AI12-0132: a renames-as-body freezes the expression of any
3307 -- expression function that it renames.
3309 if Is_Entity_Name (Nam)
3310 and then Is_Expression_Function (Entity (Nam))
3311 and then not Inside_A_Generic
3314 (Def_Id => Entity (Nam),
3315 Typ => Etype (Entity (Nam)),
3318 (Original_Node (Unit_Declaration_Node (Entity (Nam)))),
3322 -- Normal subprogram renaming (not renaming as body)
3325 Generate_Definition (New_S);
3326 New_Overloaded_Entity (New_S);
3328 if not (Is_Entity_Name (Nam)
3329 and then Is_Intrinsic_Subprogram (Entity (Nam)))
3331 Check_Delayed_Subprogram (New_S);
3334 -- Verify that a SPARK renaming does not declare a primitive
3335 -- operation of a tagged type.
3337 Check_SPARK_Primitive_Operation (New_S);
3340 -- There is no need for elaboration checks on the new entity, which may
3341 -- be called before the next freezing point where the body will appear.
3342 -- Elaboration checks refer to the real entity, not the one created by
3343 -- the renaming declaration.
3345 Set_Kill_Elaboration_Checks (New_S, True);
3347 -- If we had a previous error, indicate a completion is present to stop
3348 -- junk cascaded messages, but don't take any further action.
3350 if Etype (Nam) = Any_Type then
3351 Set_Has_Completion (New_S);
3354 -- Case where name has the form of a selected component
3356 elsif Nkind (Nam) = N_Selected_Component then
3358 -- A name which has the form A.B can designate an entry of task A, a
3359 -- protected operation of protected object A, or finally a primitive
3360 -- operation of object A. In the later case, A is an object of some
3361 -- tagged type, or an access type that denotes one such. To further
3362 -- distinguish these cases, note that the scope of a task entry or
3363 -- protected operation is type of the prefix.
3365 -- The prefix could be an overloaded function call that returns both
3366 -- kinds of operations. This overloading pathology is left to the
3367 -- dedicated reader ???
3370 T : constant Entity_Id := Etype (Prefix (Nam));
3378 and then Is_Tagged_Type (Designated_Type (T))))
3379 and then Scope (Entity (Selector_Name (Nam))) /= T
3381 Analyze_Renamed_Primitive_Operation
3382 (N, New_S, Present (Rename_Spec));
3386 -- Renamed entity is an entry or protected operation. For those
3387 -- cases an explicit body is built (at the point of freezing of
3388 -- this entity) that contains a call to the renamed entity.
3390 -- This is not allowed for renaming as body if the renamed
3391 -- spec is already frozen (see RM 8.5.4(5) for details).
3393 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
3395 ("renaming-as-body cannot rename entry as subprogram", N);
3397 ("\since & is already frozen (RM 8.5.4(5))",
3400 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
3407 -- Case where name is an explicit dereference X.all
3409 elsif Nkind (Nam) = N_Explicit_Dereference then
3411 -- Renamed entity is designated by access_to_subprogram expression.
3412 -- Must build body to encapsulate call, as in the entry case.
3414 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
3417 -- Indexed component
3419 elsif Nkind (Nam) = N_Indexed_Component then
3420 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
3423 -- Character literal
3425 elsif Nkind (Nam) = N_Character_Literal then
3426 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
3429 -- Only remaining case is where we have a non-entity name, or a renaming
3430 -- of some other non-overloadable entity.
3432 elsif not Is_Entity_Name (Nam)
3433 or else not Is_Overloadable (Entity (Nam))
3435 -- Do not mention the renaming if it comes from an instance
3437 if not Is_Actual then
3438 Error_Msg_N ("expect valid subprogram name in renaming", N);
3440 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
3446 -- Find the renamed entity that matches the given specification. Disable
3447 -- Ada_83 because there is no requirement of full conformance between
3448 -- renamed entity and new entity, even though the same circuit is used.
3450 -- This is a bit of an odd case, which introduces a really irregular use
3451 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
3454 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
3455 Ada_Version_Pragma := Empty;
3456 Ada_Version_Explicit := Ada_Version;
3459 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3461 -- The visible operation may be an inherited abstract operation that
3462 -- was overridden in the private part, in which case a call will
3463 -- dispatch to the overriding operation. Use the overriding one in
3464 -- the renaming declaration, to prevent spurious errors below.
3466 if Is_Overloadable (Old_S)
3467 and then Is_Abstract_Subprogram (Old_S)
3468 and then No (DTC_Entity (Old_S))
3469 and then Present (Alias (Old_S))
3470 and then not Is_Abstract_Subprogram (Alias (Old_S))
3471 and then Present (Overridden_Operation (Alias (Old_S)))
3473 Old_S := Alias (Old_S);
3476 -- When the renamed subprogram is overloaded and used as an actual
3477 -- of a generic, its entity is set to the first available homonym.
3478 -- We must first disambiguate the name, then set the proper entity.
3480 if Is_Actual and then Is_Overloaded (Nam) then
3481 Set_Entity (Nam, Old_S);
3485 -- Most common case: subprogram renames subprogram. No body is generated
3486 -- in this case, so we must indicate the declaration is complete as is.
3487 -- and inherit various attributes of the renamed subprogram.
3489 if No (Rename_Spec) then
3490 Set_Has_Completion (New_S);
3491 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3492 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3493 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3495 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3496 -- between a subprogram and its correct renaming.
3498 -- Note: the Any_Id check is a guard that prevents compiler crashes
3499 -- when performing a null exclusion check between a renaming and a
3500 -- renamed subprogram that has been found to be illegal.
3502 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3503 Check_Null_Exclusion
3505 Sub => Entity (Nam));
3508 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3509 -- overriding. The flag Requires_Overriding is set very selectively
3510 -- and misses some other illegal cases. The additional conditions
3511 -- checked below are sufficient but not necessary ???
3513 -- The rule does not apply to the renaming generated for an actual
3514 -- subprogram in an instance.
3519 -- Guard against previous errors, and omit renamings of predefined
3522 elsif Ekind (Old_S) not in E_Function | E_Procedure then
3525 elsif Requires_Overriding (Old_S)
3527 (Is_Abstract_Subprogram (Old_S)
3528 and then Present (Find_Dispatching_Type (Old_S))
3529 and then not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3532 ("renamed entity cannot be subprogram that requires overriding "
3533 & "(RM 8.5.4 (5.1))", N);
3537 Prev : constant Entity_Id := Overridden_Operation (New_S);
3541 (Has_Non_Trivial_Precondition (Prev)
3542 or else Has_Non_Trivial_Precondition (Old_S))
3545 ("conflicting inherited classwide preconditions in renaming "
3546 & "of& (RM 6.1.1 (17)", N, Old_S);
3551 if Old_S /= Any_Id then
3552 if Is_Actual and then From_Default (N) then
3554 -- This is an implicit reference to the default actual
3556 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3559 Generate_Reference (Old_S, Nam);
3562 Check_Internal_Protected_Use (N, Old_S);
3564 -- For a renaming-as-body, require subtype conformance, but if the
3565 -- declaration being completed has not been frozen, then inherit the
3566 -- convention of the renamed subprogram prior to checking conformance
3567 -- (unless the renaming has an explicit convention established; the
3568 -- rule stated in the RM doesn't seem to address this ???).
3570 if Present (Rename_Spec) then
3571 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3572 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3574 if not Is_Frozen (Rename_Spec) then
3575 if not Has_Convention_Pragma (Rename_Spec) then
3576 Set_Convention (New_S, Convention (Old_S));
3579 if Ekind (Old_S) /= E_Operator then
3580 Check_Mode_Conformant (New_S, Old_S, Spec);
3583 if Original_Subprogram (Old_S) = Rename_Spec then
3584 Error_Msg_N ("unfrozen subprogram cannot rename itself", N);
3586 Check_Formal_Subprogram_Conformance (New_S, Old_S, Spec);
3589 Check_Subtype_Conformant (New_S, Old_S, Spec);
3592 Check_Frozen_Renaming (N, Rename_Spec);
3594 -- Check explicitly that renamed entity is not intrinsic, because
3595 -- in a generic the renamed body is not built. In this case,
3596 -- the renaming_as_body is a completion.
3598 if Inside_A_Generic then
3599 if Is_Frozen (Rename_Spec)
3600 and then Is_Intrinsic_Subprogram (Old_S)
3603 ("subprogram in renaming_as_body cannot be intrinsic",
3607 Set_Has_Completion (Rename_Spec);
3610 elsif Ekind (Old_S) /= E_Operator then
3612 -- If this a defaulted subprogram for a class-wide actual there is
3613 -- no check for mode conformance, given that the signatures don't
3614 -- match (the source mentions T but the actual mentions T'Class).
3619 -- No need for a redundant error message if this is a nested
3620 -- instance, unless the current instantiation (of a child unit)
3621 -- is a compilation unit, which is not analyzed when the parent
3622 -- generic is analyzed.
3625 or else No (Enclosing_Instance)
3626 or else Is_Compilation_Unit (Current_Scope)
3628 Check_Mode_Conformant (New_S, Old_S);
3632 if No (Rename_Spec) then
3634 -- The parameter profile of the new entity is that of the renamed
3635 -- entity: the subtypes given in the specification are irrelevant.
3637 Inherit_Renamed_Profile (New_S, Old_S);
3639 -- A call to the subprogram is transformed into a call to the
3640 -- renamed entity. This is transitive if the renamed entity is
3641 -- itself a renaming.
3643 if Present (Alias (Old_S)) then
3644 Set_Alias (New_S, Alias (Old_S));
3646 Set_Alias (New_S, Old_S);
3649 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3650 -- renaming as body, since the entity in this case is not an
3651 -- intrinsic (it calls an intrinsic, but we have a real body for
3652 -- this call, and it is in this body that the required intrinsic
3653 -- processing will take place).
3655 -- Also, if this is a renaming of inequality, the renamed operator
3656 -- is intrinsic, but what matters is the corresponding equality
3657 -- operator, which may be user-defined.
3659 Set_Is_Intrinsic_Subprogram
3661 Is_Intrinsic_Subprogram (Old_S)
3663 (Chars (Old_S) /= Name_Op_Ne
3664 or else Ekind (Old_S) = E_Operator
3665 or else Is_Intrinsic_Subprogram
3666 (Corresponding_Equality (Old_S))));
3668 if Ekind (Alias (New_S)) = E_Operator then
3669 Set_Has_Delayed_Freeze (New_S, False);
3672 -- If the renaming corresponds to an association for an abstract
3673 -- formal subprogram, then various attributes must be set to
3674 -- indicate that the renaming is an abstract dispatching operation
3675 -- with a controlling type.
3677 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
3679 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3680 -- see it as corresponding to a generic association for a
3681 -- formal abstract subprogram
3683 Set_Is_Abstract_Subprogram (New_S);
3686 New_S_Ctrl_Type : constant Entity_Id :=
3687 Find_Dispatching_Type (New_S);
3688 Old_S_Ctrl_Type : constant Entity_Id :=
3689 Find_Dispatching_Type (Old_S);
3693 -- The actual must match the (instance of the) formal,
3694 -- and must be a controlling type.
3696 if Old_S_Ctrl_Type /= New_S_Ctrl_Type
3697 or else No (New_S_Ctrl_Type)
3699 if No (New_S_Ctrl_Type) then
3701 ("actual must be dispatching subprogram", Nam);
3704 ("actual must be dispatching subprogram for type&",
3705 Nam, New_S_Ctrl_Type);
3709 Set_Is_Dispatching_Operation (New_S);
3710 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3712 -- If the actual in the formal subprogram is itself a
3713 -- formal abstract subprogram association, there's no
3714 -- dispatch table component or position to inherit.
3716 if Present (DTC_Entity (Old_S)) then
3717 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3718 Set_DT_Position_Value (New_S, DT_Position (Old_S));
3728 -- The following is illegal, because F hides whatever other F may
3730 -- function F (...) renames F;
3733 or else (Nkind (Nam) /= N_Expanded_Name
3734 and then Chars (Old_S) = Chars (New_S))
3736 Error_Msg_N ("subprogram cannot rename itself", N);
3738 -- This is illegal even if we use a selector:
3739 -- function F (...) renames Pkg.F;
3740 -- because F is still hidden.
3742 elsif Nkind (Nam) = N_Expanded_Name
3743 and then Entity (Prefix (Nam)) = Current_Scope
3744 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3746 -- This is an error, but we overlook the error and accept the
3747 -- renaming if the special Overriding_Renamings mode is in effect.
3749 if not Overriding_Renamings then
3751 ("implicit operation& is not visible (RM 8.3 (15))",
3756 Set_Convention (New_S, Convention (Old_S));
3758 if Is_Abstract_Subprogram (Old_S) then
3759 if Present (Rename_Spec) then
3761 ("a renaming-as-body cannot rename an abstract subprogram",
3763 Set_Has_Completion (Rename_Spec);
3765 Set_Is_Abstract_Subprogram (New_S);
3769 Check_Library_Unit_Renaming (N, Old_S);
3771 -- Pathological case: procedure renames entry in the scope of its
3772 -- task. Entry is given by simple name, but body must be built for
3773 -- procedure. Of course if called it will deadlock.
3775 if Ekind (Old_S) = E_Entry then
3776 Set_Has_Completion (New_S, False);
3777 Set_Alias (New_S, Empty);
3780 -- Do not freeze the renaming nor the renamed entity when the context
3781 -- is an enclosing generic. Freezing is an expansion activity, and in
3782 -- addition the renamed entity may depend on the generic formals of
3783 -- the enclosing generic.
3785 if Is_Actual and not Inside_A_Generic then
3786 Freeze_Before (N, Old_S);
3787 Freeze_Actual_Profile;
3788 Set_Has_Delayed_Freeze (New_S, False);
3789 Freeze_Before (N, New_S);
3791 -- An abstract subprogram is only allowed as an actual in the case
3792 -- where the formal subprogram is also abstract.
3794 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
3795 and then Is_Abstract_Subprogram (Old_S)
3796 and then not Is_Abstract_Subprogram (Formal_Spec)
3799 ("abstract subprogram not allowed as generic actual", Nam);
3804 -- A common error is to assume that implicit operators for types are
3805 -- defined in Standard, or in the scope of a subtype. In those cases
3806 -- where the renamed entity is given with an expanded name, it is
3807 -- worth mentioning that operators for the type are not declared in
3808 -- the scope given by the prefix.
3810 if Nkind (Nam) = N_Expanded_Name
3811 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
3812 and then Scope (Entity (Nam)) = Standard_Standard
3815 T : constant Entity_Id :=
3816 Base_Type (Etype (First_Formal (New_S)));
3818 Error_Msg_Node_2 := Prefix (Nam);
3820 ("operator for type& is not declared in&", Prefix (Nam), T);
3825 ("no visible subprogram matches the specification for&",
3829 if Present (Candidate_Renaming) then
3836 F1 := First_Formal (Candidate_Renaming);
3837 F2 := First_Formal (New_S);
3838 T1 := First_Subtype (Etype (F1));
3839 while Present (F1) and then Present (F2) loop
3844 if Present (F1) and then Present (Default_Value (F1)) then
3845 if Present (Next_Formal (F1)) then
3847 ("\missing specification for & and other formals with "
3848 & "defaults", Spec, F1);
3850 Error_Msg_NE ("\missing specification for &", Spec, F1);
3854 if Nkind (Nam) = N_Operator_Symbol
3855 and then From_Default (N)
3857 Error_Msg_Node_2 := T1;
3859 ("default & on & is not directly visible", Nam, Nam);
3865 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
3866 -- controlling access parameters are known non-null for the renamed
3867 -- subprogram. Test also applies to a subprogram instantiation that
3868 -- is dispatching. Test is skipped if some previous error was detected
3869 -- that set Old_S to Any_Id.
3871 if Ada_Version >= Ada_2005
3872 and then Old_S /= Any_Id
3873 and then not Is_Dispatching_Operation (Old_S)
3874 and then Is_Dispatching_Operation (New_S)
3881 Old_F := First_Formal (Old_S);
3882 New_F := First_Formal (New_S);
3883 while Present (Old_F) loop
3884 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
3885 and then Is_Controlling_Formal (New_F)
3886 and then not Can_Never_Be_Null (Old_F)
3888 Error_Msg_N ("access parameter is controlling,", New_F);
3890 ("\corresponding parameter of& must be explicitly null "
3891 & "excluding", New_F, Old_S);
3894 Next_Formal (Old_F);
3895 Next_Formal (New_F);
3900 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
3901 -- is to warn if an operator is being renamed as a different operator.
3902 -- If the operator is predefined, examine the kind of the entity, not
3903 -- the abbreviated declaration in Standard.
3905 if Comes_From_Source (N)
3906 and then Present (Old_S)
3907 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3908 or else Ekind (Old_S) = E_Operator)
3909 and then Nkind (New_S) = N_Defining_Operator_Symbol
3910 and then Chars (Old_S) /= Chars (New_S)
3913 ("& is being renamed as a different operator??", N, Old_S);
3916 -- Check for renaming of obsolescent subprogram
3918 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3920 -- Another warning or some utility: if the new subprogram as the same
3921 -- name as the old one, the old one is not hidden by an outer homograph,
3922 -- the new one is not a public symbol, and the old one is otherwise
3923 -- directly visible, the renaming is superfluous.
3925 if Chars (Old_S) = Chars (New_S)
3926 and then Comes_From_Source (N)
3927 and then Scope (Old_S) /= Standard_Standard
3928 and then Warn_On_Redundant_Constructs
3929 and then (Is_Immediately_Visible (Old_S)
3930 or else Is_Potentially_Use_Visible (Old_S))
3931 and then Is_Overloadable (Current_Scope)
3932 and then Chars (Current_Scope) /= Chars (Old_S)
3935 ("redundant renaming, entity is directly visible?r?", Name (N));
3938 -- Implementation-defined aspect specifications can appear in a renaming
3939 -- declaration, but not language-defined ones. The call to procedure
3940 -- Analyze_Aspect_Specifications will take care of this error check.
3942 if Has_Aspects (N) then
3943 Analyze_Aspect_Specifications (N, New_S);
3949 and then Has_Yield_Aspect (Formal_Spec)
3950 and then not Has_Yield_Aspect (Old_S)
3952 Error_Msg_Name_1 := Name_Yield;
3954 ("actual subprogram& must have aspect% to match formal", Name (N));
3957 Ada_Version := Save_AV;
3958 Ada_Version_Pragma := Save_AVP;
3959 Ada_Version_Explicit := Save_AV_Exp;
3961 -- Check if we are looking at an Ada 2012 defaulted formal subprogram
3962 -- and mark any use_package_clauses that affect the visibility of the
3963 -- implicit generic actual.
3965 -- Also, we may be looking at an internal renaming of a user-defined
3966 -- subprogram created for a generic formal subprogram association,
3967 -- which will also have to be marked here. This can occur when the
3968 -- corresponding formal subprogram contains references to other generic
3971 if Is_Generic_Actual_Subprogram (New_S)
3972 and then (Is_Intrinsic_Subprogram (New_S)
3973 or else From_Default (N)
3974 or else Nkind (N) = N_Subprogram_Renaming_Declaration)
3976 Mark_Use_Clauses (New_S);
3978 -- Handle overloaded subprograms
3980 if Present (Alias (New_S)) then
3981 Mark_Use_Clauses (Alias (New_S));
3984 end Analyze_Subprogram_Renaming;
3986 -------------------------
3987 -- Analyze_Use_Package --
3988 -------------------------
3990 -- Resolve the package names in the use clause, and make all the visible
3991 -- entities defined in the package potentially use-visible. If the package
3992 -- is already in use from a previous use clause, its visible entities are
3993 -- already use-visible. In that case, mark the occurrence as a redundant
3994 -- use. If the package is an open scope, i.e. if the use clause occurs
3995 -- within the package itself, ignore it.
3997 procedure Analyze_Use_Package (N : Node_Id; Chain : Boolean := True) is
3998 procedure Analyze_Package_Name (Clause : Node_Id);
3999 -- Perform analysis on a package name from a use_package_clause
4001 procedure Analyze_Package_Name_List (Head_Clause : Node_Id);
4002 -- Similar to Analyze_Package_Name but iterates over all the names
4005 --------------------------
4006 -- Analyze_Package_Name --
4007 --------------------------
4009 procedure Analyze_Package_Name (Clause : Node_Id) is
4010 Pack : constant Node_Id := Name (Clause);
4014 pragma Assert (Nkind (Clause) = N_Use_Package_Clause);
4017 -- Verify that the package standard is not directly named in a
4018 -- use_package_clause.
4020 if Nkind (Parent (Clause)) = N_Compilation_Unit
4021 and then Nkind (Pack) = N_Expanded_Name
4023 Pref := Prefix (Pack);
4025 while Nkind (Pref) = N_Expanded_Name loop
4026 Pref := Prefix (Pref);
4029 if Entity (Pref) = Standard_Standard then
4031 ("predefined package Standard cannot appear in a context "
4035 end Analyze_Package_Name;
4037 -------------------------------
4038 -- Analyze_Package_Name_List --
4039 -------------------------------
4041 procedure Analyze_Package_Name_List (Head_Clause : Node_Id) is
4045 -- Due to the way source use clauses are split during parsing we are
4046 -- forced to simply iterate through all entities in scope until the
4047 -- clause representing the last name in the list is found.
4049 Curr := Head_Clause;
4050 while Present (Curr) loop
4051 Analyze_Package_Name (Curr);
4053 -- Stop iterating over the names in the use clause when we are at
4056 exit when not More_Ids (Curr) and then Prev_Ids (Curr);
4059 end Analyze_Package_Name_List;
4065 -- Start of processing for Analyze_Use_Package
4068 Set_Hidden_By_Use_Clause (N, No_Elist);
4070 -- Use clause not allowed in a spec of a predefined package declaration
4071 -- except that packages whose file name starts a-n are OK (these are
4072 -- children of Ada.Numerics, which are never loaded by Rtsfind).
4074 if Is_Predefined_Unit (Current_Sem_Unit)
4075 and then Get_Name_String
4076 (Unit_File_Name (Current_Sem_Unit)) (1 .. 3) /= "a-n"
4077 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
4078 N_Package_Declaration
4080 Error_Msg_N ("use clause not allowed in predefined spec", N);
4083 -- Loop through all package names from the original use clause in
4084 -- order to analyze referenced packages. A use_package_clause with only
4085 -- one name does not have More_Ids or Prev_Ids set, while a clause with
4086 -- More_Ids only starts the chain produced by the parser.
4088 if not More_Ids (N) and then not Prev_Ids (N) then
4089 Analyze_Package_Name (N);
4091 elsif More_Ids (N) and then not Prev_Ids (N) then
4092 Analyze_Package_Name_List (N);
4095 if not Is_Entity_Name (Name (N)) then
4096 Error_Msg_N ("& is not a package", Name (N));
4102 Chain_Use_Clause (N);
4105 Pack := Entity (Name (N));
4107 -- There are many cases where scopes are manipulated during analysis, so
4108 -- check that Pack's current use clause has not already been chained
4109 -- before setting its previous use clause.
4111 if Ekind (Pack) = E_Package
4112 and then Present (Current_Use_Clause (Pack))
4113 and then Current_Use_Clause (Pack) /= N
4114 and then No (Prev_Use_Clause (N))
4115 and then Prev_Use_Clause (Current_Use_Clause (Pack)) /= N
4117 Set_Prev_Use_Clause (N, Current_Use_Clause (Pack));
4120 -- Mark all entities as potentially use visible
4122 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
4123 if Ekind (Pack) = E_Generic_Package then
4124 Error_Msg_N -- CODEFIX
4125 ("a generic package is not allowed in a use clause", Name (N));
4127 elsif Is_Generic_Subprogram (Pack) then
4128 Error_Msg_N -- CODEFIX
4129 ("a generic subprogram is not allowed in a use clause",
4132 elsif Is_Subprogram (Pack) then
4133 Error_Msg_N -- CODEFIX
4134 ("a subprogram is not allowed in a use clause", Name (N));
4137 Error_Msg_N ("& is not allowed in a use clause", Name (N));
4141 if Nkind (Parent (N)) = N_Compilation_Unit then
4142 Check_In_Previous_With_Clause (N, Name (N));
4145 Use_One_Package (N, Name (N));
4148 Mark_Ghost_Clause (N);
4149 end Analyze_Use_Package;
4151 ----------------------
4152 -- Analyze_Use_Type --
4153 ----------------------
4155 procedure Analyze_Use_Type (N : Node_Id; Chain : Boolean := True) is
4160 Set_Hidden_By_Use_Clause (N, No_Elist);
4162 -- Chain clause to list of use clauses in current scope when flagged
4165 Chain_Use_Clause (N);
4168 -- Obtain the base type of the type denoted within the use_type_clause's
4171 Id := Subtype_Mark (N);
4173 E := Base_Type (Entity (Id));
4175 -- There are many cases where a use_type_clause may be reanalyzed due to
4176 -- manipulation of the scope stack so we much guard against those cases
4177 -- here, otherwise, we must add the new use_type_clause to the previous
4178 -- use_type_clause chain in order to mark redundant use_type_clauses as
4179 -- used. When the redundant use-type clauses appear in a parent unit and
4180 -- a child unit we must prevent a circularity in the chain that would
4181 -- otherwise result from the separate steps of analysis and installation
4182 -- of the parent context.
4184 if Present (Current_Use_Clause (E))
4185 and then Current_Use_Clause (E) /= N
4186 and then Prev_Use_Clause (Current_Use_Clause (E)) /= N
4187 and then No (Prev_Use_Clause (N))
4189 Set_Prev_Use_Clause (N, Current_Use_Clause (E));
4192 -- If the Used_Operations list is already initialized, the clause has
4193 -- been analyzed previously, and it is being reinstalled, for example
4194 -- when the clause appears in a package spec and we are compiling the
4195 -- corresponding package body. In that case, make the entities on the
4196 -- existing list use_visible, and mark the corresponding types In_Use.
4198 if Present (Used_Operations (N)) then
4203 Use_One_Type (Subtype_Mark (N), Installed => True);
4205 Elmt := First_Elmt (Used_Operations (N));
4206 while Present (Elmt) loop
4207 Set_Is_Potentially_Use_Visible (Node (Elmt));
4215 -- Otherwise, create new list and attach to it the operations that are
4216 -- made use-visible by the clause.
4218 Set_Used_Operations (N, New_Elmt_List);
4221 if E /= Any_Type then
4224 if Nkind (Parent (N)) = N_Compilation_Unit then
4225 if Nkind (Id) = N_Identifier then
4226 Error_Msg_N ("type is not directly visible", Id);
4228 elsif Is_Child_Unit (Scope (E))
4229 and then Scope (E) /= System_Aux_Id
4231 Check_In_Previous_With_Clause (N, Prefix (Id));
4236 -- If the use_type_clause appears in a compilation unit context,
4237 -- check whether it comes from a unit that may appear in a
4238 -- limited_with_clause, for a better error message.
4240 if Nkind (Parent (N)) = N_Compilation_Unit
4241 and then Nkind (Id) /= N_Identifier
4247 function Mentioned (Nam : Node_Id) return Boolean;
4248 -- Check whether the prefix of expanded name for the type
4249 -- appears in the prefix of some limited_with_clause.
4255 function Mentioned (Nam : Node_Id) return Boolean is
4257 return Nkind (Name (Item)) = N_Selected_Component
4258 and then Chars (Prefix (Name (Item))) = Chars (Nam);
4262 Pref := Prefix (Id);
4263 Item := First (Context_Items (Parent (N)));
4264 while Present (Item) and then Item /= N loop
4265 if Nkind (Item) = N_With_Clause
4266 and then Limited_Present (Item)
4267 and then Mentioned (Pref)
4270 (Get_Msg_Id, "premature usage of incomplete type");
4279 Mark_Ghost_Clause (N);
4280 end Analyze_Use_Type;
4282 ------------------------
4283 -- Attribute_Renaming --
4284 ------------------------
4286 procedure Attribute_Renaming (N : Node_Id) is
4287 Loc : constant Source_Ptr := Sloc (N);
4288 Nam : constant Node_Id := Name (N);
4289 Spec : constant Node_Id := Specification (N);
4290 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
4291 Aname : constant Name_Id := Attribute_Name (Nam);
4293 Form_Num : Nat := 0;
4294 Expr_List : List_Id := No_List;
4296 Attr_Node : Node_Id;
4297 Body_Node : Node_Id;
4298 Param_Spec : Node_Id;
4301 Generate_Definition (New_S);
4303 -- This procedure is called in the context of subprogram renaming, and
4304 -- thus the attribute must be one that is a subprogram. All of those
4305 -- have at least one formal parameter, with the exceptions of the GNAT
4306 -- attribute 'Img, which GNAT treats as renameable.
4308 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
4309 if Aname /= Name_Img then
4311 ("subprogram renaming an attribute must have formals", N);
4316 Param_Spec := First (Parameter_Specifications (Spec));
4317 while Present (Param_Spec) loop
4318 Form_Num := Form_Num + 1;
4320 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4321 Find_Type (Parameter_Type (Param_Spec));
4323 -- The profile of the new entity denotes the base type (s) of
4324 -- the types given in the specification. For access parameters
4325 -- there are no subtypes involved.
4327 Rewrite (Parameter_Type (Param_Spec),
4329 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
4332 if No (Expr_List) then
4333 Expr_List := New_List;
4336 Append_To (Expr_List,
4337 Make_Identifier (Loc,
4338 Chars => Chars (Defining_Identifier (Param_Spec))));
4340 -- The expressions in the attribute reference are not freeze
4341 -- points. Neither is the attribute as a whole, see below.
4343 Set_Must_Not_Freeze (Last (Expr_List));
4348 -- Immediate error if too many formals. Other mismatches in number or
4349 -- types of parameters are detected when we analyze the body of the
4350 -- subprogram that we construct.
4352 if Form_Num > 2 then
4353 Error_Msg_N ("too many formals for attribute", N);
4355 -- Error if the attribute reference has expressions that look like
4356 -- formal parameters.
4358 elsif Present (Expressions (Nam)) then
4359 Error_Msg_N ("illegal expressions in attribute reference", Nam);
4361 elsif Aname in Name_Compose | Name_Exponent | Name_Leading_Part |
4362 Name_Pos | Name_Round | Name_Scaling |
4365 if Nkind (N) = N_Subprogram_Renaming_Declaration
4366 and then Present (Corresponding_Formal_Spec (N))
4369 ("generic actual cannot be attribute involving universal type",
4373 ("attribute involving a universal type cannot be renamed",
4378 -- Rewrite attribute node to have a list of expressions corresponding to
4379 -- the subprogram formals. A renaming declaration is not a freeze point,
4380 -- and the analysis of the attribute reference should not freeze the
4381 -- type of the prefix. We use the original node in the renaming so that
4382 -- its source location is preserved, and checks on stream attributes are
4383 -- properly applied.
4385 Attr_Node := Relocate_Node (Nam);
4386 Set_Expressions (Attr_Node, Expr_List);
4388 Set_Must_Not_Freeze (Attr_Node);
4389 Set_Must_Not_Freeze (Prefix (Nam));
4391 -- Case of renaming a function
4393 if Nkind (Spec) = N_Function_Specification then
4394 if Is_Procedure_Attribute_Name (Aname) then
4395 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
4399 Find_Type (Result_Definition (Spec));
4400 Rewrite (Result_Definition (Spec),
4402 (Base_Type (Entity (Result_Definition (Spec))), Loc));
4405 Make_Subprogram_Body (Loc,
4406 Specification => Spec,
4407 Declarations => New_List,
4408 Handled_Statement_Sequence =>
4409 Make_Handled_Sequence_Of_Statements (Loc,
4410 Statements => New_List (
4411 Make_Simple_Return_Statement (Loc,
4412 Expression => Attr_Node))));
4414 -- Case of renaming a procedure
4417 if not Is_Procedure_Attribute_Name (Aname) then
4418 Error_Msg_N ("attribute can only be renamed as function", Nam);
4423 Make_Subprogram_Body (Loc,
4424 Specification => Spec,
4425 Declarations => New_List,
4426 Handled_Statement_Sequence =>
4427 Make_Handled_Sequence_Of_Statements (Loc,
4428 Statements => New_List (Attr_Node)));
4431 -- Signal the ABE mechanism that the generated subprogram body has not
4432 -- ABE ramifications.
4434 Set_Was_Attribute_Reference (Body_Node);
4436 -- In case of tagged types we add the body of the generated function to
4437 -- the freezing actions of the type (because in the general case such
4438 -- type is still not frozen). We exclude from this processing generic
4439 -- formal subprograms found in instantiations.
4441 -- We must exclude restricted run-time libraries because
4442 -- entity AST_Handler is defined in package System.Aux_Dec which is not
4443 -- available in those platforms. Note that we cannot use the function
4444 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
4445 -- the ZFP run-time library is not defined as a profile, and we do not
4446 -- want to deal with AST_Handler in ZFP mode.
4448 if not Configurable_Run_Time_Mode
4449 and then not Present (Corresponding_Formal_Spec (N))
4450 and then not Is_RTE (Etype (Nam), RE_AST_Handler)
4453 P : constant Node_Id := Prefix (Nam);
4456 -- The prefix of 'Img is an object that is evaluated for each call
4457 -- of the function that renames it.
4459 if Aname = Name_Img then
4460 Preanalyze_And_Resolve (P);
4462 -- For all other attribute renamings, the prefix is a subtype
4468 -- If the target type is not yet frozen, add the body to the
4469 -- actions to be elaborated at freeze time.
4471 if Is_Tagged_Type (Etype (P))
4472 and then In_Open_Scopes (Scope (Etype (P)))
4474 Ensure_Freeze_Node (Etype (P));
4475 Append_Freeze_Action (Etype (P), Body_Node);
4477 Rewrite (N, Body_Node);
4479 Set_Etype (New_S, Base_Type (Etype (New_S)));
4483 -- Generic formal subprograms or AST_Handler renaming
4486 Rewrite (N, Body_Node);
4488 Set_Etype (New_S, Base_Type (Etype (New_S)));
4491 if Is_Compilation_Unit (New_S) then
4493 ("a library unit can only rename another library unit", N);
4496 -- We suppress elaboration warnings for the resulting entity, since
4497 -- clearly they are not needed, and more particularly, in the case
4498 -- of a generic formal subprogram, the resulting entity can appear
4499 -- after the instantiation itself, and thus look like a bogus case
4500 -- of access before elaboration.
4502 if Legacy_Elaboration_Checks then
4503 Set_Suppress_Elaboration_Warnings (New_S);
4505 end Attribute_Renaming;
4507 ----------------------
4508 -- Chain_Use_Clause --
4509 ----------------------
4511 procedure Chain_Use_Clause (N : Node_Id) is
4512 Level : Int := Scope_Stack.Last;
4518 if not Is_Compilation_Unit (Current_Scope)
4519 or else not Is_Child_Unit (Current_Scope)
4523 -- Common case for compilation unit
4525 elsif Defining_Entity (Parent (N)) = Current_Scope then
4529 -- If declaration appears in some other scope, it must be in some
4530 -- parent unit when compiling a child.
4532 Pack := Defining_Entity (Parent (N));
4534 if not In_Open_Scopes (Pack) then
4537 -- If the use clause appears in an ancestor and we are in the
4538 -- private part of the immediate parent, the use clauses are
4539 -- already installed.
4541 elsif Pack /= Scope (Current_Scope)
4542 and then In_Private_Part (Scope (Current_Scope))
4547 -- Find entry for parent unit in scope stack
4549 while Scope_Stack.Table (Level).Entity /= Pack loop
4555 Set_Next_Use_Clause (N,
4556 Scope_Stack.Table (Level).First_Use_Clause);
4557 Scope_Stack.Table (Level).First_Use_Clause := N;
4558 end Chain_Use_Clause;
4560 ---------------------------
4561 -- Check_Frozen_Renaming --
4562 ---------------------------
4564 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
4569 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
4572 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
4574 if Is_Entity_Name (Name (N)) then
4575 Old_S := Entity (Name (N));
4577 if not Is_Frozen (Old_S)
4578 and then Operating_Mode /= Check_Semantics
4580 Append_Freeze_Action (Old_S, B_Node);
4582 Insert_After (N, B_Node);
4586 if Is_Intrinsic_Subprogram (Old_S)
4587 and then not In_Instance
4588 and then not Relaxed_RM_Semantics
4591 ("subprogram used in renaming_as_body cannot be intrinsic",
4596 Insert_After (N, B_Node);
4600 end Check_Frozen_Renaming;
4602 -------------------------------
4603 -- Set_Entity_Or_Discriminal --
4604 -------------------------------
4606 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4610 -- If the entity is not a discriminant, or else expansion is disabled,
4611 -- simply set the entity.
4613 if not In_Spec_Expression
4614 or else Ekind (E) /= E_Discriminant
4615 or else Inside_A_Generic
4617 Set_Entity_With_Checks (N, E);
4619 -- The replacement of a discriminant by the corresponding discriminal
4620 -- is not done for a task discriminant that appears in a default
4621 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4622 -- for details on their handling.
4624 elsif Is_Concurrent_Type (Scope (E)) then
4627 and then Nkind (P) not in
4628 N_Parameter_Specification | N_Component_Declaration
4634 and then Nkind (P) = N_Parameter_Specification
4639 Set_Entity (N, Discriminal (E));
4642 -- Otherwise, this is a discriminant in a context in which
4643 -- it is a reference to the corresponding parameter of the
4644 -- init proc for the enclosing type.
4647 Set_Entity (N, Discriminal (E));
4649 end Set_Entity_Or_Discriminal;
4651 -----------------------------------
4652 -- Check_In_Previous_With_Clause --
4653 -----------------------------------
4655 procedure Check_In_Previous_With_Clause
4659 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4664 Item := First (Context_Items (Parent (N)));
4665 while Present (Item) and then Item /= N loop
4666 if Nkind (Item) = N_With_Clause
4668 -- Protect the frontend against previous critical errors
4670 and then Nkind (Name (Item)) /= N_Selected_Component
4671 and then Entity (Name (Item)) = Pack
4675 -- Find root library unit in with_clause
4677 while Nkind (Par) = N_Expanded_Name loop
4678 Par := Prefix (Par);
4681 if Is_Child_Unit (Entity (Original_Node (Par))) then
4682 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4691 -- On exit, package is not mentioned in a previous with_clause.
4692 -- Check if its prefix is.
4694 if Nkind (Nam) = N_Expanded_Name then
4695 Check_In_Previous_With_Clause (N, Prefix (Nam));
4697 elsif Pack /= Any_Id then
4698 Error_Msg_NE ("& is not visible", Nam, Pack);
4700 end Check_In_Previous_With_Clause;
4702 ---------------------------------
4703 -- Check_Library_Unit_Renaming --
4704 ---------------------------------
4706 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4710 if Nkind (Parent (N)) /= N_Compilation_Unit then
4713 -- Check for library unit. Note that we used to check for the scope
4714 -- being Standard here, but that was wrong for Standard itself.
4716 elsif not Is_Compilation_Unit (Old_E)
4717 and then not Is_Child_Unit (Old_E)
4719 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4721 -- Entities defined in Standard (operators and boolean literals) cannot
4722 -- be renamed as library units.
4724 elsif Scope (Old_E) = Standard_Standard
4725 and then Sloc (Old_E) = Standard_Location
4727 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4729 elsif Present (Parent_Spec (N))
4730 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
4731 and then not Is_Child_Unit (Old_E)
4734 ("renamed unit must be a child unit of generic parent", Name (N));
4736 elsif Nkind (N) in N_Generic_Renaming_Declaration
4737 and then Nkind (Name (N)) = N_Expanded_Name
4738 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
4739 and then Is_Generic_Unit (Old_E)
4742 ("renamed generic unit must be a library unit", Name (N));
4744 elsif Is_Package_Or_Generic_Package (Old_E) then
4746 -- Inherit categorization flags
4748 New_E := Defining_Entity (N);
4749 Set_Is_Pure (New_E, Is_Pure (Old_E));
4750 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
4751 Set_Is_Remote_Call_Interface (New_E,
4752 Is_Remote_Call_Interface (Old_E));
4753 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
4754 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
4756 end Check_Library_Unit_Renaming;
4758 ------------------------
4759 -- Enclosing_Instance --
4760 ------------------------
4762 function Enclosing_Instance return Entity_Id is
4766 if not Is_Generic_Instance (Current_Scope) then
4770 S := Scope (Current_Scope);
4771 while S /= Standard_Standard loop
4772 if Is_Generic_Instance (S) then
4780 end Enclosing_Instance;
4786 procedure End_Scope is
4792 Id := First_Entity (Current_Scope);
4793 while Present (Id) loop
4794 -- An entity in the current scope is not necessarily the first one
4795 -- on its homonym chain. Find its predecessor if any,
4796 -- If it is an internal entity, it will not be in the visibility
4797 -- chain altogether, and there is nothing to unchain.
4799 if Id /= Current_Entity (Id) then
4800 Prev := Current_Entity (Id);
4801 while Present (Prev)
4802 and then Present (Homonym (Prev))
4803 and then Homonym (Prev) /= Id
4805 Prev := Homonym (Prev);
4808 -- Skip to end of loop if Id is not in the visibility chain
4810 if No (Prev) or else Homonym (Prev) /= Id then
4818 Set_Is_Immediately_Visible (Id, False);
4820 Outer := Homonym (Id);
4821 while Present (Outer) and then Scope (Outer) = Current_Scope loop
4822 Outer := Homonym (Outer);
4825 -- Reset homonym link of other entities, but do not modify link
4826 -- between entities in current scope, so that the back-end can have
4827 -- a proper count of local overloadings.
4830 Set_Name_Entity_Id (Chars (Id), Outer);
4832 elsif Scope (Prev) /= Scope (Id) then
4833 Set_Homonym (Prev, Outer);
4840 -- If the scope generated freeze actions, place them before the
4841 -- current declaration and analyze them. Type declarations and
4842 -- the bodies of initialization procedures can generate such nodes.
4843 -- We follow the parent chain until we reach a list node, which is
4844 -- the enclosing list of declarations. If the list appears within
4845 -- a protected definition, move freeze nodes outside the protected
4849 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
4853 L : constant List_Id := Scope_Stack.Table
4854 (Scope_Stack.Last).Pending_Freeze_Actions;
4857 if Is_Itype (Current_Scope) then
4858 Decl := Associated_Node_For_Itype (Current_Scope);
4860 Decl := Parent (Current_Scope);
4865 while not Is_List_Member (Decl)
4866 or else Nkind (Parent (Decl)) in N_Protected_Definition
4869 Decl := Parent (Decl);
4872 Insert_List_Before_And_Analyze (Decl, L);
4880 ---------------------
4881 -- End_Use_Clauses --
4882 ---------------------
4884 procedure End_Use_Clauses (Clause : Node_Id) is
4888 -- Remove use_type_clauses first, because they affect the visibility of
4889 -- operators in subsequent used packages.
4892 while Present (U) loop
4893 if Nkind (U) = N_Use_Type_Clause then
4897 Next_Use_Clause (U);
4901 while Present (U) loop
4902 if Nkind (U) = N_Use_Package_Clause then
4903 End_Use_Package (U);
4906 Next_Use_Clause (U);
4908 end End_Use_Clauses;
4910 ---------------------
4911 -- End_Use_Package --
4912 ---------------------
4914 procedure End_Use_Package (N : Node_Id) is
4916 Pack_Name : Node_Id;
4920 function Is_Primitive_Operator_In_Use
4922 F : Entity_Id) return Boolean;
4923 -- Check whether Op is a primitive operator of a use-visible type
4925 ----------------------------------
4926 -- Is_Primitive_Operator_In_Use --
4927 ----------------------------------
4929 function Is_Primitive_Operator_In_Use
4931 F : Entity_Id) return Boolean
4933 T : constant Entity_Id := Base_Type (Etype (F));
4935 return In_Use (T) and then Scope (T) = Scope (Op);
4936 end Is_Primitive_Operator_In_Use;
4938 -- Start of processing for End_Use_Package
4941 Pack_Name := Name (N);
4943 -- Test that Pack_Name actually denotes a package before processing
4945 if Is_Entity_Name (Pack_Name)
4946 and then Ekind (Entity (Pack_Name)) = E_Package
4948 Pack := Entity (Pack_Name);
4950 if In_Open_Scopes (Pack) then
4953 elsif not Redundant_Use (Pack_Name) then
4954 Set_In_Use (Pack, False);
4955 Set_Current_Use_Clause (Pack, Empty);
4957 Id := First_Entity (Pack);
4958 while Present (Id) loop
4960 -- Preserve use-visibility of operators that are primitive
4961 -- operators of a type that is use-visible through an active
4964 if Nkind (Id) = N_Defining_Operator_Symbol
4966 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
4968 (Present (Next_Formal (First_Formal (Id)))
4970 Is_Primitive_Operator_In_Use
4971 (Id, Next_Formal (First_Formal (Id)))))
4975 Set_Is_Potentially_Use_Visible (Id, False);
4978 if Is_Private_Type (Id)
4979 and then Present (Full_View (Id))
4981 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4987 if Present (Renamed_Object (Pack)) then
4988 Set_In_Use (Renamed_Object (Pack), False);
4989 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
4992 if Chars (Pack) = Name_System
4993 and then Scope (Pack) = Standard_Standard
4994 and then Present_System_Aux
4996 Id := First_Entity (System_Aux_Id);
4997 while Present (Id) loop
4998 Set_Is_Potentially_Use_Visible (Id, False);
5000 if Is_Private_Type (Id)
5001 and then Present (Full_View (Id))
5003 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
5009 Set_In_Use (System_Aux_Id, False);
5012 Set_Redundant_Use (Pack_Name, False);
5016 if Present (Hidden_By_Use_Clause (N)) then
5017 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
5018 while Present (Elmt) loop
5020 E : constant Entity_Id := Node (Elmt);
5023 -- Reset either Use_Visibility or Direct_Visibility, depending
5024 -- on how the entity was hidden by the use clause.
5026 if In_Use (Scope (E))
5027 and then Used_As_Generic_Actual (Scope (E))
5029 Set_Is_Potentially_Use_Visible (Node (Elmt));
5031 Set_Is_Immediately_Visible (Node (Elmt));
5038 Set_Hidden_By_Use_Clause (N, No_Elist);
5040 end End_Use_Package;
5046 procedure End_Use_Type (N : Node_Id) is
5051 -- Start of processing for End_Use_Type
5054 Id := Subtype_Mark (N);
5056 -- A call to Rtsfind may occur while analyzing a use_type_clause, in
5057 -- which case the type marks are not resolved yet, so guard against that
5060 if Is_Entity_Name (Id) and then Present (Entity (Id)) then
5063 if T = Any_Type or else From_Limited_With (T) then
5066 -- Note that the use_type_clause may mention a subtype of the type
5067 -- whose primitive operations have been made visible. Here as
5068 -- elsewhere, it is the base type that matters for visibility.
5070 elsif In_Open_Scopes (Scope (Base_Type (T))) then
5073 elsif not Redundant_Use (Id) then
5074 Set_In_Use (T, False);
5075 Set_In_Use (Base_Type (T), False);
5076 Set_Current_Use_Clause (T, Empty);
5077 Set_Current_Use_Clause (Base_Type (T), Empty);
5079 -- See Use_One_Type for the rationale. This is a bit on the naive
5080 -- side, but should be good enough in practice.
5082 if Is_Tagged_Type (T) then
5083 Set_In_Use (Class_Wide_Type (T), False);
5088 if Is_Empty_Elmt_List (Used_Operations (N)) then
5092 Elmt := First_Elmt (Used_Operations (N));
5093 while Present (Elmt) loop
5094 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
5100 --------------------
5101 -- Entity_Of_Unit --
5102 --------------------
5104 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
5106 if Nkind (U) = N_Package_Instantiation and then Analyzed (U) then
5107 return Defining_Entity (Instance_Spec (U));
5109 return Defining_Entity (U);
5113 ----------------------
5114 -- Find_Direct_Name --
5115 ----------------------
5117 procedure Find_Direct_Name (N : Node_Id) is
5122 Homonyms : Entity_Id;
5123 -- Saves start of homonym chain
5125 Inst : Entity_Id := Empty;
5126 -- Enclosing instance, if any
5128 Nvis_Entity : Boolean;
5129 -- Set True to indicate that there is at least one entity on the homonym
5130 -- chain which, while not visible, is visible enough from the user point
5131 -- of view to warrant an error message of "not visible" rather than
5134 Nvis_Is_Private_Subprg : Boolean := False;
5135 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
5136 -- effect concerning library subprograms has been detected. Used to
5137 -- generate the precise error message.
5139 function From_Actual_Package (E : Entity_Id) return Boolean;
5140 -- Returns true if the entity is an actual for a package that is itself
5141 -- an actual for a formal package of the current instance. Such an
5142 -- entity requires special handling because it may be use-visible but
5143 -- hides directly visible entities defined outside the instance, because
5144 -- the corresponding formal did so in the generic.
5146 function Is_Actual_Parameter return Boolean;
5147 -- This function checks if the node N is an identifier that is an actual
5148 -- parameter of a procedure call. If so it returns True, otherwise it
5149 -- return False. The reason for this check is that at this stage we do
5150 -- not know what procedure is being called if the procedure might be
5151 -- overloaded, so it is premature to go setting referenced flags or
5152 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
5153 -- for that processing.
5154 -- Note: there is a similar routine Sem_Util.Is_Actual_Parameter, but
5155 -- it works for both function and procedure calls, while here we are
5156 -- only concerned with procedure calls (and with entry calls as well,
5157 -- but they are parsed as procedure calls and only later rewritten to
5160 function Known_But_Invisible (E : Entity_Id) return Boolean;
5161 -- This function determines whether a reference to the entity E, which
5162 -- is not visible, can reasonably be considered to be known to the
5163 -- writer of the reference. This is a heuristic test, used only for
5164 -- the purposes of figuring out whether we prefer to complain that an
5165 -- entity is undefined or invisible (and identify the declaration of
5166 -- the invisible entity in the latter case). The point here is that we
5167 -- don't want to complain that something is invisible and then point to
5168 -- something entirely mysterious to the writer.
5170 procedure Nvis_Messages;
5171 -- Called if there are no visible entries for N, but there is at least
5172 -- one non-directly visible, or hidden declaration. This procedure
5173 -- outputs an appropriate set of error messages.
5175 procedure Undefined (Nvis : Boolean);
5176 -- This function is called if the current node has no corresponding
5177 -- visible entity or entities. The value set in Msg indicates whether
5178 -- an error message was generated (multiple error messages for the
5179 -- same variable are generally suppressed, see body for details).
5180 -- Msg is True if an error message was generated, False if not. This
5181 -- value is used by the caller to determine whether or not to output
5182 -- additional messages where appropriate. The parameter is set False
5183 -- to get the message "X is undefined", and True to get the message
5184 -- "X is not visible".
5186 -------------------------
5187 -- From_Actual_Package --
5188 -------------------------
5190 function From_Actual_Package (E : Entity_Id) return Boolean is
5191 Scop : constant Entity_Id := Scope (E);
5192 -- Declared scope of candidate entity
5194 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
5195 -- Recursive function that does the work and examines actuals of
5196 -- actual packages of current instance.
5198 ------------------------
5199 -- Declared_In_Actual --
5200 ------------------------
5202 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
5206 if No (Associated_Formal_Package (Pack)) then
5210 Act := First_Entity (Pack);
5211 while Present (Act) loop
5212 if Renamed_Object (Pack) = Scop then
5215 -- Check for end of list of actuals
5217 elsif Ekind (Act) = E_Package
5218 and then Renamed_Object (Act) = Pack
5222 elsif Ekind (Act) = E_Package
5223 and then Declared_In_Actual (Act)
5233 end Declared_In_Actual;
5239 -- Start of processing for From_Actual_Package
5242 if not In_Instance then
5246 Inst := Current_Scope;
5247 while Present (Inst)
5248 and then Ekind (Inst) /= E_Package
5249 and then not Is_Generic_Instance (Inst)
5251 Inst := Scope (Inst);
5258 Act := First_Entity (Inst);
5259 while Present (Act) loop
5260 if Ekind (Act) = E_Package
5261 and then Declared_In_Actual (Act)
5271 end From_Actual_Package;
5273 -------------------------
5274 -- Is_Actual_Parameter --
5275 -------------------------
5277 function Is_Actual_Parameter return Boolean is
5279 if Nkind (N) = N_Identifier then
5280 case Nkind (Parent (N)) is
5281 when N_Procedure_Call_Statement =>
5282 return Is_List_Member (N)
5283 and then List_Containing (N) =
5284 Parameter_Associations (Parent (N));
5286 when N_Parameter_Association =>
5287 return N = Explicit_Actual_Parameter (Parent (N))
5288 and then Nkind (Parent (Parent (N))) =
5289 N_Procedure_Call_Statement;
5297 end Is_Actual_Parameter;
5299 -------------------------
5300 -- Known_But_Invisible --
5301 -------------------------
5303 function Known_But_Invisible (E : Entity_Id) return Boolean is
5304 Fname : File_Name_Type;
5307 -- Entities in Standard are always considered to be known
5309 if Sloc (E) <= Standard_Location then
5312 -- An entity that does not come from source is always considered
5313 -- to be unknown, since it is an artifact of code expansion.
5315 elsif not Comes_From_Source (E) then
5319 -- Here we have an entity that is not from package Standard, and
5320 -- which comes from Source. See if it comes from an internal file.
5322 Fname := Unit_File_Name (Get_Source_Unit (E));
5324 -- Case of from internal file
5326 if In_Internal_Unit (E) then
5328 -- Private part entities in internal files are never considered
5329 -- to be known to the writer of normal application code.
5331 if Is_Hidden (E) then
5335 -- Entities from System packages other than System and
5336 -- System.Storage_Elements are not considered to be known.
5337 -- System.Auxxxx files are also considered known to the user.
5339 -- Should refine this at some point to generally distinguish
5340 -- between known and unknown internal files ???
5342 Get_Name_String (Fname);
5347 Name_Buffer (1 .. 2) /= "s-"
5349 Name_Buffer (3 .. 8) = "stoele"
5351 Name_Buffer (3 .. 5) = "aux";
5353 -- If not an internal file, then entity is definitely known, even if
5354 -- it is in a private part (the message generated will note that it
5355 -- is in a private part).
5360 end Known_But_Invisible;
5366 procedure Nvis_Messages is
5367 Comp_Unit : Node_Id;
5369 Found : Boolean := False;
5370 Hidden : Boolean := False;
5374 -- Ada 2005 (AI-262): Generate a precise error concerning the
5375 -- Beaujolais effect that was previously detected
5377 if Nvis_Is_Private_Subprg then
5379 pragma Assert (Nkind (E2) = N_Defining_Identifier
5380 and then Ekind (E2) = E_Function
5381 and then Scope (E2) = Standard_Standard
5382 and then Has_Private_With (E2));
5384 -- Find the sloc corresponding to the private with'ed unit
5386 Comp_Unit := Cunit (Current_Sem_Unit);
5387 Error_Msg_Sloc := No_Location;
5389 Item := First (Context_Items (Comp_Unit));
5390 while Present (Item) loop
5391 if Nkind (Item) = N_With_Clause
5392 and then Private_Present (Item)
5393 and then Entity (Name (Item)) = E2
5395 Error_Msg_Sloc := Sloc (Item);
5402 pragma Assert (Error_Msg_Sloc /= No_Location);
5404 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
5408 Undefined (Nvis => True);
5412 -- First loop does hidden declarations
5415 while Present (Ent) loop
5416 if Is_Potentially_Use_Visible (Ent) then
5418 Error_Msg_N -- CODEFIX
5419 ("multiple use clauses cause hiding!", N);
5423 Error_Msg_Sloc := Sloc (Ent);
5424 Error_Msg_N -- CODEFIX
5425 ("hidden declaration#!", N);
5428 Ent := Homonym (Ent);
5431 -- If we found hidden declarations, then that's enough, don't
5432 -- bother looking for non-visible declarations as well.
5438 -- Second loop does non-directly visible declarations
5441 while Present (Ent) loop
5442 if not Is_Potentially_Use_Visible (Ent) then
5444 -- Do not bother the user with unknown entities
5446 if not Known_But_Invisible (Ent) then
5450 Error_Msg_Sloc := Sloc (Ent);
5452 -- Output message noting that there is a non-visible
5453 -- declaration, distinguishing the private part case.
5455 if Is_Hidden (Ent) then
5456 Error_Msg_N ("non-visible (private) declaration#!", N);
5458 -- If the entity is declared in a generic package, it
5459 -- cannot be visible, so there is no point in adding it
5460 -- to the list of candidates if another homograph from a
5461 -- non-generic package has been seen.
5463 elsif Ekind (Scope (Ent)) = E_Generic_Package
5469 Error_Msg_N -- CODEFIX
5470 ("non-visible declaration#!", N);
5472 if Ekind (Scope (Ent)) /= E_Generic_Package then
5476 if Is_Compilation_Unit (Ent)
5478 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5480 Error_Msg_Qual_Level := 99;
5481 Error_Msg_NE -- CODEFIX
5482 ("\\missing `WITH &;`", N, Ent);
5483 Error_Msg_Qual_Level := 0;
5486 if Ekind (Ent) = E_Discriminant
5487 and then Present (Corresponding_Discriminant (Ent))
5488 and then Scope (Corresponding_Discriminant (Ent)) =
5492 ("inherited discriminant not allowed here" &
5493 " (RM 3.8 (12), 3.8.1 (6))!", N);
5497 -- Set entity and its containing package as referenced. We
5498 -- can't be sure of this, but this seems a better choice
5499 -- to avoid unused entity messages.
5501 if Comes_From_Source (Ent) then
5502 Set_Referenced (Ent);
5503 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5508 Ent := Homonym (Ent);
5517 procedure Undefined (Nvis : Boolean) is
5518 Emsg : Error_Msg_Id;
5521 -- We should never find an undefined internal name. If we do, then
5522 -- see if we have previous errors. If so, ignore on the grounds that
5523 -- it is probably a cascaded message (e.g. a block label from a badly
5524 -- formed block). If no previous errors, then we have a real internal
5525 -- error of some kind so raise an exception.
5527 if Is_Internal_Name (Chars (N)) then
5528 if Total_Errors_Detected /= 0 then
5531 raise Program_Error;
5535 -- A very specialized error check, if the undefined variable is
5536 -- a case tag, and the case type is an enumeration type, check
5537 -- for a possible misspelling, and if so, modify the identifier
5539 -- Named aggregate should also be handled similarly ???
5541 if Nkind (N) = N_Identifier
5542 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5545 Case_Stm : constant Node_Id := Parent (Parent (N));
5546 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5551 if Is_Enumeration_Type (Case_Typ)
5552 and then not Is_Standard_Character_Type (Case_Typ)
5554 Lit := First_Literal (Case_Typ);
5555 Get_Name_String (Chars (Lit));
5557 if Chars (Lit) /= Chars (N)
5558 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5560 Error_Msg_Node_2 := Lit;
5561 Error_Msg_N -- CODEFIX
5562 ("& is undefined, assume misspelling of &", N);
5563 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5572 -- Normal processing
5574 Set_Entity (N, Any_Id);
5575 Set_Etype (N, Any_Type);
5577 -- We use the table Urefs to keep track of entities for which we
5578 -- have issued errors for undefined references. Multiple errors
5579 -- for a single name are normally suppressed, however we modify
5580 -- the error message to alert the programmer to this effect.
5582 for J in Urefs.First .. Urefs.Last loop
5583 if Chars (N) = Chars (Urefs.Table (J).Node) then
5584 if Urefs.Table (J).Err /= No_Error_Msg
5585 and then Sloc (N) /= Urefs.Table (J).Loc
5587 Error_Msg_Node_1 := Urefs.Table (J).Node;
5589 if Urefs.Table (J).Nvis then
5590 Change_Error_Text (Urefs.Table (J).Err,
5591 "& is not visible (more references follow)");
5593 Change_Error_Text (Urefs.Table (J).Err,
5594 "& is undefined (more references follow)");
5597 Urefs.Table (J).Err := No_Error_Msg;
5600 -- Although we will set Msg False, and thus suppress the
5601 -- message, we also set Error_Posted True, to avoid any
5602 -- cascaded messages resulting from the undefined reference.
5605 Set_Error_Posted (N);
5610 -- If entry not found, this is first undefined occurrence
5613 Error_Msg_N ("& is not visible!", N);
5617 Error_Msg_N ("& is undefined!", N);
5620 -- A very bizarre special check, if the undefined identifier
5621 -- is Put or Put_Line, then add a special error message (since
5622 -- this is a very common error for beginners to make).
5624 if Chars (N) in Name_Put | Name_Put_Line then
5625 Error_Msg_N -- CODEFIX
5626 ("\\possible missing `WITH Ada.Text_'I'O; " &
5627 "USE Ada.Text_'I'O`!", N);
5629 -- Another special check if N is the prefix of a selected
5630 -- component which is a known unit: add message complaining
5631 -- about missing with for this unit.
5633 elsif Nkind (Parent (N)) = N_Selected_Component
5634 and then N = Prefix (Parent (N))
5635 and then Is_Known_Unit (Parent (N))
5638 P : Node_Id := Parent (N);
5640 Error_Msg_Name_1 := Chars (N);
5641 Error_Msg_Name_2 := Chars (Selector_Name (P));
5643 if Nkind (Parent (P)) = N_Selected_Component
5644 and then Is_Known_Unit (Parent (P))
5647 Error_Msg_Name_3 := Chars (Selector_Name (P));
5648 Error_Msg_N -- CODEFIX
5649 ("\\missing `WITH %.%.%;`", N);
5652 Error_Msg_N -- CODEFIX
5653 ("\\missing `WITH %.%;`", N);
5658 -- Now check for possible misspellings
5662 Ematch : Entity_Id := Empty;
5664 for Nam in First_Name_Id .. Last_Name_Id loop
5665 E := Get_Name_Entity_Id (Nam);
5668 and then (Is_Immediately_Visible (E)
5670 Is_Potentially_Use_Visible (E))
5672 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5679 if Present (Ematch) then
5680 Error_Msg_NE -- CODEFIX
5681 ("\possible misspelling of&", N, Ematch);
5686 -- Make entry in undefined references table unless the full errors
5687 -- switch is set, in which case by refraining from generating the
5688 -- table entry we guarantee that we get an error message for every
5689 -- undefined reference. The entry is not added if we are ignoring
5692 if not All_Errors_Mode
5693 and then Ignore_Errors_Enable = 0
5694 and then not Get_Ignore_Errors
5708 Nested_Inst : Entity_Id := Empty;
5709 -- The entity of a nested instance which appears within Inst (if any)
5711 -- Start of processing for Find_Direct_Name
5714 -- If the entity pointer is already set, this is an internal node, or
5715 -- a node that is analyzed more than once, after a tree modification.
5716 -- In such a case there is no resolution to perform, just set the type.
5718 if Present (Entity (N)) then
5719 if Is_Type (Entity (N)) then
5720 Set_Etype (N, Entity (N));
5722 -- The exception to this general rule are constants associated with
5723 -- discriminals of protected types because for each protected op
5724 -- a new set of discriminals is internally created by the frontend
5725 -- (see Exp_Ch9.Set_Discriminals), and the current decoration of the
5726 -- entity pointer may have been set as part of a preanalysis, where
5727 -- discriminals still reference the first subprogram or entry to be
5728 -- expanded (see Expand_Protected_Body_Declarations).
5731 and then Ekind (Entity (N)) = E_Constant
5732 and then Present (Discriminal_Link (Entity (N)))
5733 and then Is_Protected_Type (Scope (Discriminal_Link (Entity (N))))
5739 Entyp : constant Entity_Id := Etype (Entity (N));
5742 -- One special case here. If the Etype field is already set,
5743 -- and references the packed array type corresponding to the
5744 -- etype of the referenced entity, then leave it alone. This
5745 -- happens for trees generated from Exp_Pakd, where expressions
5746 -- can be deliberately "mis-typed" to the packed array type.
5748 if Is_Packed_Array (Entyp)
5749 and then Present (Etype (N))
5750 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5754 -- If not that special case, then just reset the Etype
5757 Set_Etype (N, Etype (Entity (N)));
5762 -- Although the marking of use clauses happens at the end of
5763 -- Find_Direct_Name, a certain case where a generic actual satisfies
5764 -- a use clause must be checked here due to how the generic machinery
5765 -- handles the analysis of said actuals.
5768 and then Nkind (Parent (N)) = N_Generic_Association
5770 Mark_Use_Clauses (Entity (N));
5778 -- Preserve relevant elaboration-related attributes of the context which
5779 -- are no longer available or very expensive to recompute once analysis,
5780 -- resolution, and expansion are over.
5782 if Nkind (N) = N_Identifier then
5783 Mark_Elaboration_Attributes
5790 -- Here if Entity pointer was not set, we need full visibility analysis
5791 -- First we generate debugging output if the debug E flag is set.
5793 if Debug_Flag_E then
5794 Write_Str ("Looking for ");
5795 Write_Name (Chars (N));
5799 Homonyms := Current_Entity (N);
5800 Nvis_Entity := False;
5803 while Present (E) loop
5805 -- If entity is immediately visible or potentially use visible, then
5806 -- process the entity and we are done.
5808 if Is_Immediately_Visible (E) then
5809 goto Immediately_Visible_Entity;
5811 elsif Is_Potentially_Use_Visible (E) then
5812 goto Potentially_Use_Visible_Entity;
5814 -- Note if a known but invisible entity encountered
5816 elsif Known_But_Invisible (E) then
5817 Nvis_Entity := True;
5820 -- Move to next entity in chain and continue search
5825 -- If no entries on homonym chain that were potentially visible,
5826 -- and no entities reasonably considered as non-visible, then
5827 -- we have a plain undefined reference, with no additional
5828 -- explanation required.
5830 if not Nvis_Entity then
5831 Undefined (Nvis => False);
5833 -- Otherwise there is at least one entry on the homonym chain that
5834 -- is reasonably considered as being known and non-visible.
5842 -- Processing for a potentially use visible entry found. We must search
5843 -- the rest of the homonym chain for two reasons. First, if there is a
5844 -- directly visible entry, then none of the potentially use-visible
5845 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5846 -- for the case of multiple potentially use-visible entries hiding one
5847 -- another and as a result being non-directly visible (RM 8.4(11)).
5849 <<Potentially_Use_Visible_Entity>> declare
5850 Only_One_Visible : Boolean := True;
5851 All_Overloadable : Boolean := Is_Overloadable (E);
5855 while Present (E2) loop
5856 if Is_Immediately_Visible (E2) then
5858 -- If the use-visible entity comes from the actual for a
5859 -- formal package, it hides a directly visible entity from
5860 -- outside the instance.
5862 if From_Actual_Package (E)
5863 and then Scope_Depth (Scope (E2)) < Scope_Depth (Inst)
5868 goto Immediately_Visible_Entity;
5871 elsif Is_Potentially_Use_Visible (E2) then
5872 Only_One_Visible := False;
5873 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5875 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5876 -- that can occur in private_with clauses. Example:
5879 -- private with B; package A is
5880 -- package C is function B return Integer;
5882 -- V1 : Integer := B;
5883 -- private function B return Integer;
5884 -- V2 : Integer := B;
5887 -- V1 resolves to A.B, but V2 resolves to library unit B
5889 elsif Ekind (E2) = E_Function
5890 and then Scope (E2) = Standard_Standard
5891 and then Has_Private_With (E2)
5893 Only_One_Visible := False;
5894 All_Overloadable := False;
5895 Nvis_Is_Private_Subprg := True;
5902 -- On falling through this loop, we have checked that there are no
5903 -- immediately visible entities. Only_One_Visible is set if exactly
5904 -- one potentially use visible entity exists. All_Overloadable is
5905 -- set if all the potentially use visible entities are overloadable.
5906 -- The condition for legality is that either there is one potentially
5907 -- use visible entity, or if there is more than one, then all of them
5908 -- are overloadable.
5910 if Only_One_Visible or All_Overloadable then
5913 -- If there is more than one potentially use-visible entity and at
5914 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5915 -- Note that E points to the first such entity on the homonym list.
5918 -- If one of the entities is declared in an actual package, it
5919 -- was visible in the generic, and takes precedence over other
5920 -- entities that are potentially use-visible. The same applies
5921 -- if the entity is declared in a local instantiation of the
5922 -- current instance.
5926 -- Find the current instance
5928 Inst := Current_Scope;
5929 while Present (Inst) and then Inst /= Standard_Standard loop
5930 if Is_Generic_Instance (Inst) then
5934 Inst := Scope (Inst);
5937 -- Reexamine the candidate entities, giving priority to those
5938 -- that were visible within the generic.
5941 while Present (E2) loop
5942 Nested_Inst := Nearest_Enclosing_Instance (E2);
5944 -- The entity is declared within an actual package, or in a
5945 -- nested instance. The ">=" accounts for the case where the
5946 -- current instance and the nested instance are the same.
5948 if From_Actual_Package (E2)
5949 or else (Present (Nested_Inst)
5950 and then Scope_Depth (Nested_Inst) >=
5963 elsif Is_Predefined_Unit (Current_Sem_Unit) then
5964 -- A use clause in the body of a system file creates conflict
5965 -- with some entity in a user scope, while rtsfind is active.
5966 -- Keep only the entity coming from another predefined unit.
5969 while Present (E2) loop
5970 if In_Predefined_Unit (E2) then
5978 -- Entity must exist because predefined unit is correct
5980 raise Program_Error;
5989 -- Come here with E set to the first immediately visible entity on
5990 -- the homonym chain. This is the one we want unless there is another
5991 -- immediately visible entity further on in the chain for an inner
5992 -- scope (RM 8.3(8)).
5994 <<Immediately_Visible_Entity>> declare
5999 -- Find scope level of initial entity. When compiling through
6000 -- Rtsfind, the previous context is not completely invisible, and
6001 -- an outer entity may appear on the chain, whose scope is below
6002 -- the entry for Standard that delimits the current scope stack.
6003 -- Indicate that the level for this spurious entry is outside of
6004 -- the current scope stack.
6006 Level := Scope_Stack.Last;
6008 Scop := Scope_Stack.Table (Level).Entity;
6009 exit when Scop = Scope (E);
6011 exit when Scop = Standard_Standard;
6014 -- Now search remainder of homonym chain for more inner entry
6015 -- If the entity is Standard itself, it has no scope, and we
6016 -- compare it with the stack entry directly.
6019 while Present (E2) loop
6020 if Is_Immediately_Visible (E2) then
6022 -- If a generic package contains a local declaration that
6023 -- has the same name as the generic, there may be a visibility
6024 -- conflict in an instance, where the local declaration must
6025 -- also hide the name of the corresponding package renaming.
6026 -- We check explicitly for a package declared by a renaming,
6027 -- whose renamed entity is an instance that is on the scope
6028 -- stack, and that contains a homonym in the same scope. Once
6029 -- we have found it, we know that the package renaming is not
6030 -- immediately visible, and that the identifier denotes the
6031 -- other entity (and its homonyms if overloaded).
6033 if Scope (E) = Scope (E2)
6034 and then Ekind (E) = E_Package
6035 and then Present (Renamed_Object (E))
6036 and then Is_Generic_Instance (Renamed_Object (E))
6037 and then In_Open_Scopes (Renamed_Object (E))
6038 and then Comes_From_Source (N)
6040 Set_Is_Immediately_Visible (E, False);
6044 for J in Level + 1 .. Scope_Stack.Last loop
6045 if Scope_Stack.Table (J).Entity = Scope (E2)
6046 or else Scope_Stack.Table (J).Entity = E2
6059 -- At the end of that loop, E is the innermost immediately
6060 -- visible entity, so we are all set.
6063 -- Come here with entity found, and stored in E
6067 -- Check violation of No_Wide_Characters restriction
6069 Check_Wide_Character_Restriction (E, N);
6071 -- When distribution features are available (Get_PCS_Name /=
6072 -- Name_No_DSA), a remote access-to-subprogram type is converted
6073 -- into a record type holding whatever information is needed to
6074 -- perform a remote call on an RCI subprogram. In that case we
6075 -- rewrite any occurrence of the RAS type into the equivalent record
6076 -- type here. 'Access attribute references and RAS dereferences are
6077 -- then implemented using specific TSSs. However when distribution is
6078 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
6079 -- generation of these TSSs, and we must keep the RAS type in its
6080 -- original access-to-subprogram form (since all calls through a
6081 -- value of such type will be local anyway in the absence of a PCS).
6083 if Comes_From_Source (N)
6084 and then Is_Remote_Access_To_Subprogram_Type (E)
6085 and then Ekind (E) = E_Access_Subprogram_Type
6086 and then Expander_Active
6087 and then Get_PCS_Name /= Name_No_DSA
6089 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
6093 -- Set the entity. Note that the reason we call Set_Entity for the
6094 -- overloadable case, as opposed to Set_Entity_With_Checks is
6095 -- that in the overloaded case, the initial call can set the wrong
6096 -- homonym. The call that sets the right homonym is in Sem_Res and
6097 -- that call does use Set_Entity_With_Checks, so we don't miss
6100 if Is_Overloadable (E) then
6103 Set_Entity_With_Checks (N, E);
6109 Set_Etype (N, Get_Full_View (Etype (E)));
6112 if Debug_Flag_E then
6113 Write_Str (" found ");
6114 Write_Entity_Info (E, " ");
6117 -- If the Ekind of the entity is Void, it means that all homonyms
6118 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
6119 -- test is skipped if the current scope is a record and the name is
6120 -- a pragma argument expression (case of Atomic and Volatile pragmas
6121 -- and possibly other similar pragmas added later, which are allowed
6122 -- to reference components in the current record).
6124 if Ekind (E) = E_Void
6126 (not Is_Record_Type (Current_Scope)
6127 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
6129 Premature_Usage (N);
6131 -- If the entity is overloadable, collect all interpretations of the
6132 -- name for subsequent overload resolution. We optimize a bit here to
6133 -- do this only if we have an overloadable entity that is not on its
6134 -- own on the homonym chain.
6136 elsif Is_Overloadable (E)
6137 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
6139 Collect_Interps (N);
6141 -- If no homonyms were visible, the entity is unambiguous
6143 if not Is_Overloaded (N) then
6144 if not Is_Actual_Parameter then
6145 Generate_Reference (E, N);
6149 -- Case of non-overloadable entity, set the entity providing that
6150 -- we do not have the case of a discriminant reference within a
6151 -- default expression. Such references are replaced with the
6152 -- corresponding discriminal, which is the formal corresponding to
6153 -- to the discriminant in the initialization procedure.
6156 -- Entity is unambiguous, indicate that it is referenced here
6158 -- For a renaming of an object, always generate simple reference,
6159 -- we don't try to keep track of assignments in this case, except
6160 -- in SPARK mode where renamings are traversed for generating
6161 -- local effects of subprograms.
6164 and then Present (Renamed_Object (E))
6165 and then not GNATprove_Mode
6167 Generate_Reference (E, N);
6169 -- If the renamed entity is a private protected component,
6170 -- reference the original component as well. This needs to be
6171 -- done because the private renamings are installed before any
6172 -- analysis has occurred. Reference to a private component will
6173 -- resolve to the renaming and the original component will be
6174 -- left unreferenced, hence the following.
6176 if Is_Prival (E) then
6177 Generate_Reference (Prival_Link (E), N);
6180 -- One odd case is that we do not want to set the Referenced flag
6181 -- if the entity is a label, and the identifier is the label in
6182 -- the source, since this is not a reference from the point of
6183 -- view of the user.
6185 elsif Nkind (Parent (N)) = N_Label then
6187 R : constant Boolean := Referenced (E);
6190 -- Generate reference unless this is an actual parameter
6191 -- (see comment below).
6193 if not Is_Actual_Parameter then
6194 Generate_Reference (E, N);
6195 Set_Referenced (E, R);
6199 -- Normal case, not a label: generate reference
6202 if not Is_Actual_Parameter then
6204 -- Package or generic package is always a simple reference
6206 if Is_Package_Or_Generic_Package (E) then
6207 Generate_Reference (E, N, 'r');
6209 -- Else see if we have a left hand side
6214 Generate_Reference (E, N, 'm');
6217 Generate_Reference (E, N, 'r');
6219 -- If we don't know now, generate reference later
6222 Defer_Reference ((E, N));
6228 Set_Entity_Or_Discriminal (N, E);
6230 -- The name may designate a generalized reference, in which case
6231 -- the dereference interpretation will be included. Context is
6232 -- one in which a name is legal.
6234 if Ada_Version >= Ada_2012
6236 (Nkind (Parent (N)) in N_Subexpr
6237 or else Nkind (Parent (N)) in N_Assignment_Statement
6238 | N_Object_Declaration
6239 | N_Parameter_Association)
6241 Check_Implicit_Dereference (N, Etype (E));
6246 -- Mark relevant use-type and use-package clauses as effective if the
6247 -- node in question is not overloaded and therefore does not require
6250 -- Note: Generic actual subprograms do not follow the normal resolution
6251 -- path, so ignore the fact that they are overloaded and mark them
6254 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6255 Mark_Use_Clauses (N);
6258 -- Come here with entity set
6261 Check_Restriction_No_Use_Of_Entity (N);
6263 -- Annotate the tree by creating a variable reference marker in case the
6264 -- original variable reference is folded or optimized away. The variable
6265 -- reference marker is automatically saved for later examination by the
6266 -- ABE Processing phase. Variable references which act as actuals in a
6267 -- call require special processing and are left to Resolve_Actuals. The
6268 -- reference is a write when it appears on the left hand side of an
6271 if Needs_Variable_Reference_Marker (N => N, Calls_OK => False) then
6273 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6276 Build_Variable_Reference_Marker
6278 Read => not Is_Assignment_LHS,
6279 Write => Is_Assignment_LHS);
6282 end Find_Direct_Name;
6284 ------------------------
6285 -- Find_Expanded_Name --
6286 ------------------------
6288 -- This routine searches the homonym chain of the entity until it finds
6289 -- an entity declared in the scope denoted by the prefix. If the entity
6290 -- is private, it may nevertheless be immediately visible, if we are in
6291 -- the scope of its declaration.
6293 procedure Find_Expanded_Name (N : Node_Id) is
6294 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean;
6295 -- Determine whether expanded name Nod appears within a pragma which is
6296 -- a suitable context for an abstract view of a state or variable. The
6297 -- following pragmas fall in this category:
6304 -- In addition, pragma Abstract_State is also considered suitable even
6305 -- though it is an illegal context for an abstract view as this allows
6306 -- for proper resolution of abstract views of variables. This illegal
6307 -- context is later flagged in the analysis of indicator Part_Of.
6309 -----------------------------
6310 -- In_Abstract_View_Pragma --
6311 -----------------------------
6313 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is
6317 -- Climb the parent chain looking for a pragma
6320 while Present (Par) loop
6321 if Nkind (Par) = N_Pragma then
6322 if Pragma_Name_Unmapped (Par)
6323 in Name_Abstract_State
6327 | Name_Refined_Depends
6328 | Name_Refined_Global
6332 -- Otherwise the pragma is not a legal context for an abstract
6339 -- Prevent the search from going too far
6341 elsif Is_Body_Or_Package_Declaration (Par) then
6345 Par := Parent (Par);
6349 end In_Abstract_View_Pragma;
6353 Selector : constant Node_Id := Selector_Name (N);
6355 Candidate : Entity_Id := Empty;
6359 -- Start of processing for Find_Expanded_Name
6362 P_Name := Entity (Prefix (N));
6364 -- If the prefix is a renamed package, look for the entity in the
6365 -- original package.
6367 if Ekind (P_Name) = E_Package
6368 and then Present (Renamed_Object (P_Name))
6370 P_Name := Renamed_Object (P_Name);
6372 if From_Limited_With (P_Name)
6373 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6376 ("renaming of limited view of package & not usable in this"
6377 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
6379 elsif Has_Limited_View (P_Name)
6380 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6381 and then not Is_Visible_Through_Renamings (P_Name)
6384 ("renaming of limited view of package & not usable in this"
6385 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
6388 -- Rewrite node with entity field pointing to renamed object
6390 Rewrite (Prefix (N), New_Copy (Prefix (N)));
6391 Set_Entity (Prefix (N), P_Name);
6393 -- If the prefix is an object of a concurrent type, look for
6394 -- the entity in the associated task or protected type.
6396 elsif Is_Concurrent_Type (Etype (P_Name)) then
6397 P_Name := Etype (P_Name);
6400 Id := Current_Entity (Selector);
6403 Is_New_Candidate : Boolean;
6406 while Present (Id) loop
6407 if Scope (Id) = P_Name then
6409 Is_New_Candidate := True;
6411 -- Handle abstract views of states and variables. These are
6412 -- acceptable candidates only when the reference to the view
6413 -- appears in certain pragmas.
6415 if Ekind (Id) = E_Abstract_State
6416 and then From_Limited_With (Id)
6417 and then Present (Non_Limited_View (Id))
6419 if In_Abstract_View_Pragma (N) then
6420 Candidate := Non_Limited_View (Id);
6421 Is_New_Candidate := True;
6423 -- Hide the candidate because it is not used in a proper
6428 Is_New_Candidate := False;
6432 -- Ada 2005 (AI-217): Handle shadow entities associated with
6433 -- types declared in limited-withed nested packages. We don't need
6434 -- to handle E_Incomplete_Subtype entities because the entities
6435 -- in the limited view are always E_Incomplete_Type and
6436 -- E_Class_Wide_Type entities (see Build_Limited_Views).
6438 -- Regarding the expression used to evaluate the scope, it
6439 -- is important to note that the limited view also has shadow
6440 -- entities associated nested packages. For this reason the
6441 -- correct scope of the entity is the scope of the real entity.
6442 -- The non-limited view may itself be incomplete, in which case
6443 -- get the full view if available.
6445 elsif Ekind (Id) in E_Incomplete_Type | E_Class_Wide_Type
6446 and then From_Limited_With (Id)
6447 and then Present (Non_Limited_View (Id))
6448 and then Scope (Non_Limited_View (Id)) = P_Name
6450 Candidate := Get_Full_View (Non_Limited_View (Id));
6451 Is_New_Candidate := True;
6453 -- Handle special case where the prefix is a renaming of a shadow
6454 -- package which is visible. Required to avoid reporting spurious
6457 elsif Ekind (P_Name) = E_Package
6458 and then From_Limited_With (P_Name)
6459 and then not From_Limited_With (Id)
6460 and then Sloc (Scope (Id)) = Sloc (P_Name)
6461 and then Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6463 Candidate := Get_Full_View (Id);
6464 Is_New_Candidate := True;
6466 -- An unusual case arises with a fully qualified name for an
6467 -- entity local to a generic child unit package, within an
6468 -- instantiation of that package. The name of the unit now
6469 -- denotes the renaming created within the instance. This is
6470 -- only relevant in an instance body, see below.
6472 elsif Is_Generic_Instance (Scope (Id))
6473 and then In_Open_Scopes (Scope (Id))
6474 and then In_Instance_Body
6475 and then Ekind (Scope (Id)) = E_Package
6476 and then Ekind (Id) = E_Package
6477 and then Renamed_Entity (Id) = Scope (Id)
6478 and then Is_Immediately_Visible (P_Name)
6480 Is_New_Candidate := True;
6483 Is_New_Candidate := False;
6486 if Is_New_Candidate then
6488 -- If entity is a child unit, either it is a visible child of
6489 -- the prefix, or we are in the body of a generic prefix, as
6490 -- will happen when a child unit is instantiated in the body
6491 -- of a generic parent. This is because the instance body does
6492 -- not restore the full compilation context, given that all
6493 -- non-local references have been captured.
6495 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
6496 exit when Is_Visible_Lib_Unit (Id)
6497 or else (Is_Child_Unit (Id)
6498 and then In_Open_Scopes (Scope (Id))
6499 and then In_Instance_Body);
6501 exit when not Is_Hidden (Id);
6504 exit when Is_Immediately_Visible (Id);
6512 and then Ekind (P_Name) in E_Procedure | E_Function
6513 and then Is_Generic_Instance (P_Name)
6515 -- Expanded name denotes entity in (instance of) generic subprogram.
6516 -- The entity may be in the subprogram instance, or may denote one of
6517 -- the formals, which is declared in the enclosing wrapper package.
6519 P_Name := Scope (P_Name);
6521 Id := Current_Entity (Selector);
6522 while Present (Id) loop
6523 exit when Scope (Id) = P_Name;
6528 if No (Id) or else Chars (Id) /= Chars (Selector) then
6529 Set_Etype (N, Any_Type);
6531 -- If we are looking for an entity defined in System, try to find it
6532 -- in the child package that may have been provided as an extension
6533 -- to System. The Extend_System pragma will have supplied the name of
6534 -- the extension, which may have to be loaded.
6536 if Chars (P_Name) = Name_System
6537 and then Scope (P_Name) = Standard_Standard
6538 and then Present (System_Extend_Unit)
6539 and then Present_System_Aux (N)
6541 Set_Entity (Prefix (N), System_Aux_Id);
6542 Find_Expanded_Name (N);
6545 -- There is an implicit instance of the predefined operator in
6546 -- the given scope. The operator entity is defined in Standard.
6547 -- Has_Implicit_Operator makes the node into an Expanded_Name.
6549 elsif Nkind (Selector) = N_Operator_Symbol
6550 and then Has_Implicit_Operator (N)
6554 -- If there is no literal defined in the scope denoted by the
6555 -- prefix, the literal may belong to (a type derived from)
6556 -- Standard_Character, for which we have no explicit literals.
6558 elsif Nkind (Selector) = N_Character_Literal
6559 and then Has_Implicit_Character_Literal (N)
6564 -- If the prefix is a single concurrent object, use its name in
6565 -- the error message, rather than that of the anonymous type.
6567 if Is_Concurrent_Type (P_Name)
6568 and then Is_Internal_Name (Chars (P_Name))
6570 Error_Msg_Node_2 := Entity (Prefix (N));
6572 Error_Msg_Node_2 := P_Name;
6575 if P_Name = System_Aux_Id then
6576 P_Name := Scope (P_Name);
6577 Set_Entity (Prefix (N), P_Name);
6580 if Present (Candidate) then
6582 -- If we know that the unit is a child unit we can give a more
6583 -- accurate error message.
6585 if Is_Child_Unit (Candidate) then
6587 -- If the candidate is a private child unit and we are in
6588 -- the visible part of a public unit, specialize the error
6589 -- message. There might be a private with_clause for it,
6590 -- but it is not currently active.
6592 if Is_Private_Descendant (Candidate)
6593 and then Ekind (Current_Scope) = E_Package
6594 and then not In_Private_Part (Current_Scope)
6595 and then not Is_Private_Descendant (Current_Scope)
6598 ("private child unit& is not visible here", Selector);
6600 -- Normal case where we have a missing with for a child unit
6603 Error_Msg_Qual_Level := 99;
6604 Error_Msg_NE -- CODEFIX
6605 ("missing `WITH &;`", Selector, Candidate);
6606 Error_Msg_Qual_Level := 0;
6609 -- Here we don't know that this is a child unit
6612 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
6616 -- Within the instantiation of a child unit, the prefix may
6617 -- denote the parent instance, but the selector has the name
6618 -- of the original child. That is to say, when A.B appears
6619 -- within an instantiation of generic child unit B, the scope
6620 -- stack includes an instance of A (P_Name) and an instance
6621 -- of B under some other name. We scan the scope to find this
6622 -- child instance, which is the desired entity.
6623 -- Note that the parent may itself be a child instance, if
6624 -- the reference is of the form A.B.C, in which case A.B has
6625 -- already been rewritten with the proper entity.
6627 if In_Open_Scopes (P_Name)
6628 and then Is_Generic_Instance (P_Name)
6631 Gen_Par : constant Entity_Id :=
6632 Generic_Parent (Specification
6633 (Unit_Declaration_Node (P_Name)));
6634 S : Entity_Id := Current_Scope;
6638 for J in reverse 0 .. Scope_Stack.Last loop
6639 S := Scope_Stack.Table (J).Entity;
6641 exit when S = Standard_Standard;
6643 if Ekind (S) in E_Function | E_Package | E_Procedure
6646 Generic_Parent (Specification
6647 (Unit_Declaration_Node (S)));
6649 -- Check that P is a generic child of the generic
6650 -- parent of the prefix.
6653 and then Chars (P) = Chars (Selector)
6654 and then Scope (P) = Gen_Par
6665 -- If this is a selection from Ada, System or Interfaces, then
6666 -- we assume a missing with for the corresponding package.
6668 if Is_Known_Unit (N)
6669 and then not (Present (Entity (Prefix (N)))
6670 and then Scope (Entity (Prefix (N))) /=
6673 if not Error_Posted (N) then
6674 Error_Msg_Node_2 := Selector;
6675 Error_Msg_N -- CODEFIX
6676 ("missing `WITH &.&;`", Prefix (N));
6679 -- If this is a selection from a dummy package, then suppress
6680 -- the error message, of course the entity is missing if the
6681 -- package is missing.
6683 elsif Sloc (Error_Msg_Node_2) = No_Location then
6686 -- Here we have the case of an undefined component
6689 -- The prefix may hide a homonym in the context that
6690 -- declares the desired entity. This error can use a
6691 -- specialized message.
6693 if In_Open_Scopes (P_Name) then
6695 H : constant Entity_Id := Homonym (P_Name);
6699 and then Is_Compilation_Unit (H)
6701 (Is_Immediately_Visible (H)
6702 or else Is_Visible_Lib_Unit (H))
6704 Id := First_Entity (H);
6705 while Present (Id) loop
6706 if Chars (Id) = Chars (Selector) then
6707 Error_Msg_Qual_Level := 99;
6708 Error_Msg_Name_1 := Chars (Selector);
6710 ("% not declared in&", N, P_Name);
6712 ("\use fully qualified name starting with "
6713 & "Standard to make& visible", N, H);
6714 Error_Msg_Qual_Level := 0;
6722 -- If not found, standard error message
6724 Error_Msg_NE ("& not declared in&", N, Selector);
6730 -- Might be worth specializing the case when the prefix
6731 -- is a limited view.
6732 -- ... not declared in limited view of...
6734 Error_Msg_NE ("& not declared in&", N, Selector);
6737 -- Check for misspelling of some entity in prefix
6739 Id := First_Entity (P_Name);
6740 while Present (Id) loop
6741 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
6742 and then not Is_Internal_Name (Chars (Id))
6744 Error_Msg_NE -- CODEFIX
6745 ("possible misspelling of&", Selector, Id);
6752 -- Specialize the message if this may be an instantiation
6753 -- of a child unit that was not mentioned in the context.
6755 if Nkind (Parent (N)) = N_Package_Instantiation
6756 and then Is_Generic_Instance (Entity (Prefix (N)))
6757 and then Is_Compilation_Unit
6758 (Generic_Parent (Parent (Entity (Prefix (N)))))
6760 Error_Msg_Node_2 := Selector;
6761 Error_Msg_N -- CODEFIX
6762 ("\missing `WITH &.&;`", Prefix (N));
6772 if Comes_From_Source (N)
6773 and then Is_Remote_Access_To_Subprogram_Type (Id)
6774 and then Ekind (Id) = E_Access_Subprogram_Type
6775 and then Present (Equivalent_Type (Id))
6777 -- If we are not actually generating distribution code (i.e. the
6778 -- current PCS is the dummy non-distributed version), then the
6779 -- Equivalent_Type will be missing, and Id should be treated as
6780 -- a regular access-to-subprogram type.
6782 Id := Equivalent_Type (Id);
6783 Set_Chars (Selector, Chars (Id));
6786 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
6788 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
6789 if From_Limited_With (Id)
6790 or else Is_Type (Id)
6791 or else Ekind (Id) = E_Package
6796 ("limited withed package can only be used to access incomplete "
6801 if Is_Task_Type (P_Name)
6802 and then ((Ekind (Id) = E_Entry
6803 and then Nkind (Parent (N)) /= N_Attribute_Reference)
6805 (Ekind (Id) = E_Entry_Family
6807 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
6809 -- If both the task type and the entry are in scope, this may still
6810 -- be the expanded name of an entry formal.
6812 if In_Open_Scopes (Id)
6813 and then Nkind (Parent (N)) = N_Selected_Component
6818 -- It is an entry call after all, either to the current task
6819 -- (which will deadlock) or to an enclosing task.
6821 Analyze_Selected_Component (N);
6827 when N_Selected_Component =>
6828 Reinit_Field_To_Zero (N, F_Is_Prefixed_Call);
6829 Change_Selected_Component_To_Expanded_Name (N);
6831 when N_Expanded_Name =>
6835 pragma Assert (False);
6838 -- Preserve relevant elaboration-related attributes of the context which
6839 -- are no longer available or very expensive to recompute once analysis,
6840 -- resolution, and expansion are over.
6842 Mark_Elaboration_Attributes
6848 -- Set appropriate type
6850 if Is_Type (Id) then
6853 Set_Etype (N, Get_Full_View (Etype (Id)));
6856 -- Do style check and generate reference, but skip both steps if this
6857 -- entity has homonyms, since we may not have the right homonym set yet.
6858 -- The proper homonym will be set during the resolve phase.
6860 if Has_Homonym (Id) then
6864 Set_Entity_Or_Discriminal (N, Id);
6868 Generate_Reference (Id, N, 'm');
6871 Generate_Reference (Id, N, 'r');
6874 Defer_Reference ((Id, N));
6878 -- Check for violation of No_Wide_Characters
6880 Check_Wide_Character_Restriction (Id, N);
6882 -- If the Ekind of the entity is Void, it means that all homonyms are
6883 -- hidden from all visibility (RM 8.3(5,14-20)).
6885 if Ekind (Id) = E_Void then
6886 Premature_Usage (N);
6888 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6890 H : Entity_Id := Homonym (Id);
6893 while Present (H) loop
6894 if Scope (H) = Scope (Id)
6895 and then (not Is_Hidden (H)
6896 or else Is_Immediately_Visible (H))
6898 Collect_Interps (N);
6905 -- If an extension of System is present, collect possible explicit
6906 -- overloadings declared in the extension.
6908 if Chars (P_Name) = Name_System
6909 and then Scope (P_Name) = Standard_Standard
6910 and then Present (System_Extend_Unit)
6911 and then Present_System_Aux (N)
6913 H := Current_Entity (Id);
6915 while Present (H) loop
6916 if Scope (H) = System_Aux_Id then
6917 Add_One_Interp (N, H, Etype (H));
6926 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6927 and then Scope (Id) /= Standard_Standard
6929 -- In addition to user-defined operators in the given scope, there
6930 -- may be an implicit instance of the predefined operator. The
6931 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6932 -- and added to the interpretations. Procedure Add_One_Interp will
6933 -- determine which hides which.
6935 if Has_Implicit_Operator (N) then
6940 -- If there is a single interpretation for N we can generate a
6941 -- reference to the unique entity found.
6943 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6944 Generate_Reference (Id, N);
6947 -- Mark relevant use-type and use-package clauses as effective if the
6948 -- node in question is not overloaded and therefore does not require
6951 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6952 Mark_Use_Clauses (N);
6955 Check_Restriction_No_Use_Of_Entity (N);
6957 -- Annotate the tree by creating a variable reference marker in case the
6958 -- original variable reference is folded or optimized away. The variable
6959 -- reference marker is automatically saved for later examination by the
6960 -- ABE Processing phase. Variable references which act as actuals in a
6961 -- call require special processing and are left to Resolve_Actuals. The
6962 -- reference is a write when it appears on the left hand side of an
6965 if Needs_Variable_Reference_Marker
6970 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6973 Build_Variable_Reference_Marker
6975 Read => not Is_Assignment_LHS,
6976 Write => Is_Assignment_LHS);
6979 end Find_Expanded_Name;
6981 --------------------
6982 -- Find_First_Use --
6983 --------------------
6985 function Find_First_Use (Use_Clause : Node_Id) return Node_Id is
6989 -- Loop through the Prev_Use_Clause chain
6992 while Present (Prev_Use_Clause (Curr)) loop
6993 Curr := Prev_Use_Clause (Curr);
6999 -------------------------
7000 -- Find_Renamed_Entity --
7001 -------------------------
7003 function Find_Renamed_Entity
7007 Is_Actual : Boolean := False) return Entity_Id
7010 I1 : Interp_Index := 0; -- Suppress junk warnings
7016 function Find_Nearer_Entity
7019 Old2_S : Entity_Id) return Entity_Id;
7020 -- Determine whether one of Old_S1 and Old_S2 is nearer to New_S than
7021 -- the other, and return it if so. Return Empty otherwise. We use this
7022 -- in conjunction with Inherit_Renamed_Profile to simplify later type
7023 -- disambiguation for actual subprograms in instances.
7025 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
7026 -- If the renamed entity is an implicit operator, check whether it is
7027 -- visible because its operand type is properly visible. This check
7028 -- applies to explicit renamed entities that appear in the source in a
7029 -- renaming declaration or a formal subprogram instance, but not to
7030 -- default generic actuals with a name.
7032 function Report_Overload return Entity_Id;
7033 -- List possible interpretations, and specialize message in the
7034 -- case of a generic actual.
7036 function Within (Inner, Outer : Entity_Id) return Boolean;
7037 -- Determine whether a candidate subprogram is defined within the
7038 -- enclosing instance. If yes, it has precedence over outer candidates.
7040 --------------------------
7041 -- Find_Nearer_Entity --
7042 --------------------------
7044 function Find_Nearer_Entity
7047 Old2_S : Entity_Id) return Entity_Id
7055 New_F := First_Formal (New_S);
7056 Old1_F := First_Formal (Old1_S);
7057 Old2_F := First_Formal (Old2_S);
7059 -- The criterion is whether the type of the formals of one of Old1_S
7060 -- and Old2_S is an ancestor subtype of the type of the corresponding
7061 -- formals of New_S while the other is not (we already know that they
7062 -- are all subtypes of the same base type).
7064 -- This makes it possible to find the more correct renamed entity in
7065 -- the case of a generic instantiation nested in an enclosing one for
7066 -- which different formal types get the same actual type, which will
7067 -- in turn make it possible for Inherit_Renamed_Profile to preserve
7068 -- types on formal parameters and ultimately simplify disambiguation.
7070 -- Consider the follow package G:
7073 -- type Item_T is private;
7074 -- with function Compare (L, R: Item_T) return Boolean is <>;
7076 -- type Bound_T is private;
7077 -- with function Compare (L, R : Bound_T) return Boolean is <>;
7082 -- package body G is
7083 -- package My_Inner is Inner_G (Bound_T);
7087 -- with the following package Inner_G:
7090 -- type T is private;
7091 -- with function Compare (L, R: T) return Boolean is <>;
7092 -- package Inner_G is
7093 -- function "<" (L, R: T) return Boolean is (Compare (L, R));
7096 -- If G is instantiated on the same actual type with a single Compare
7100 -- function Compare (L, R : T) return Boolean;
7101 -- package My_G is new (T, T);
7103 -- then the renaming generated for Compare in the inner instantiation
7104 -- is ambiguous: it can rename either of the renamings generated for
7105 -- the outer instantiation. Now if the first one is picked up, then
7106 -- the subtypes of the formal parameters of the renaming will not be
7107 -- preserved in Inherit_Renamed_Profile because they are subtypes of
7108 -- the Bound_T formal type and not of the Item_T formal type, so we
7109 -- need to arrange for the second one to be picked up instead.
7111 while Present (New_F) loop
7112 if Etype (Old1_F) /= Etype (Old2_F) then
7113 Anc_T := Ancestor_Subtype (Etype (New_F));
7115 if Etype (Old1_F) = Anc_T then
7117 elsif Etype (Old2_F) = Anc_T then
7122 Next_Formal (New_F);
7123 Next_Formal (Old1_F);
7124 Next_Formal (Old2_F);
7127 pragma Assert (No (Old1_F));
7128 pragma Assert (No (Old2_F));
7131 end Find_Nearer_Entity;
7133 --------------------------
7134 -- Is_Visible_Operation --
7135 --------------------------
7137 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
7143 if Ekind (Op) /= E_Operator
7144 or else Scope (Op) /= Standard_Standard
7145 or else (In_Instance
7146 and then (not Is_Actual
7147 or else Present (Enclosing_Instance)))
7152 -- For a fixed point type operator, check the resulting type,
7153 -- because it may be a mixed mode integer * fixed operation.
7155 if Present (Next_Formal (First_Formal (New_S)))
7156 and then Is_Fixed_Point_Type (Etype (New_S))
7158 Typ := Etype (New_S);
7160 Typ := Etype (First_Formal (New_S));
7163 Btyp := Base_Type (Typ);
7165 if Nkind (Nam) /= N_Expanded_Name then
7166 return (In_Open_Scopes (Scope (Btyp))
7167 or else Is_Potentially_Use_Visible (Btyp)
7168 or else In_Use (Btyp)
7169 or else In_Use (Scope (Btyp)));
7172 Scop := Entity (Prefix (Nam));
7174 if Ekind (Scop) = E_Package
7175 and then Present (Renamed_Object (Scop))
7177 Scop := Renamed_Object (Scop);
7180 -- Operator is visible if prefix of expanded name denotes
7181 -- scope of type, or else type is defined in System_Aux
7182 -- and the prefix denotes System.
7184 return Scope (Btyp) = Scop
7185 or else (Scope (Btyp) = System_Aux_Id
7186 and then Scope (Scope (Btyp)) = Scop);
7189 end Is_Visible_Operation;
7195 function Within (Inner, Outer : Entity_Id) return Boolean is
7199 Sc := Scope (Inner);
7200 while Sc /= Standard_Standard loop
7211 ---------------------
7212 -- Report_Overload --
7213 ---------------------
7215 function Report_Overload return Entity_Id is
7218 Error_Msg_NE -- CODEFIX
7219 ("ambiguous actual subprogram&, " &
7220 "possible interpretations:", N, Nam);
7222 Error_Msg_N -- CODEFIX
7223 ("ambiguous subprogram, " &
7224 "possible interpretations:", N);
7227 List_Interps (Nam, N);
7229 end Report_Overload;
7231 -- Start of processing for Find_Renamed_Entity
7235 Candidate_Renaming := Empty;
7237 if Is_Overloaded (Nam) then
7238 Get_First_Interp (Nam, Ind, It);
7239 while Present (It.Nam) loop
7240 if Entity_Matches_Spec (It.Nam, New_S)
7241 and then Is_Visible_Operation (It.Nam)
7243 if Old_S /= Any_Id then
7245 -- Note: The call to Disambiguate only happens if a
7246 -- previous interpretation was found, in which case I1
7247 -- has received a value.
7249 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
7251 if It1 = No_Interp then
7252 Inst := Enclosing_Instance;
7254 if Present (Inst) then
7255 if Within (It.Nam, Inst) then
7256 if Within (Old_S, Inst) then
7258 It_D : constant Uint := Scope_Depth (It.Nam);
7259 Old_D : constant Uint := Scope_Depth (Old_S);
7262 -- Choose the innermost subprogram, which
7263 -- would hide the outer one in the generic.
7265 if Old_D > It_D then
7267 elsif It_D > Old_D then
7271 -- Otherwise, if we can determine that one
7272 -- of the entities is nearer to the renaming
7273 -- than the other, choose it. If not, then
7274 -- return the newer one as done historically.
7277 Find_Nearer_Entity (New_S, Old_S, It.Nam);
7278 if Present (N_Ent) then
7286 elsif Within (Old_S, Inst) then
7290 return Report_Overload;
7293 -- If not within an instance, ambiguity is real
7296 return Report_Overload;
7310 Present (First_Formal (It.Nam))
7311 and then Present (First_Formal (New_S))
7312 and then (Base_Type (Etype (First_Formal (It.Nam))) =
7313 Base_Type (Etype (First_Formal (New_S))))
7315 Candidate_Renaming := It.Nam;
7318 Get_Next_Interp (Ind, It);
7321 Set_Entity (Nam, Old_S);
7323 if Old_S /= Any_Id then
7324 Set_Is_Overloaded (Nam, False);
7327 -- Non-overloaded case
7331 and then Present (Enclosing_Instance)
7332 and then Entity_Matches_Spec (Entity (Nam), New_S)
7334 Old_S := Entity (Nam);
7336 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
7337 Candidate_Renaming := New_S;
7339 if Is_Visible_Operation (Entity (Nam)) then
7340 Old_S := Entity (Nam);
7343 elsif Present (First_Formal (Entity (Nam)))
7344 and then Present (First_Formal (New_S))
7345 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
7346 Base_Type (Etype (First_Formal (New_S))))
7348 Candidate_Renaming := Entity (Nam);
7353 end Find_Renamed_Entity;
7355 -----------------------------
7356 -- Find_Selected_Component --
7357 -----------------------------
7359 procedure Find_Selected_Component (N : Node_Id) is
7360 P : constant Node_Id := Prefix (N);
7363 -- Entity denoted by prefix
7370 function Available_Subtype return Boolean;
7371 -- A small optimization: if the prefix is constrained and the component
7372 -- is an array type we may already have a usable subtype for it, so we
7373 -- can use it rather than generating a new one, because the bounds
7374 -- will be the values of the discriminants and not discriminant refs.
7375 -- This simplifies value tracing in GNATprove. For consistency, both
7376 -- the entity name and the subtype come from the constrained component.
7378 -- This is only used in GNATprove mode: when generating code it may be
7379 -- necessary to create an itype in the scope of use of the selected
7380 -- component, e.g. in the context of a expanded record equality.
7382 function Is_Reference_In_Subunit return Boolean;
7383 -- In a subunit, the scope depth is not a proper measure of hiding,
7384 -- because the context of the proper body may itself hide entities in
7385 -- parent units. This rare case requires inspecting the tree directly
7386 -- because the proper body is inserted in the main unit and its context
7387 -- is simply added to that of the parent.
7389 -----------------------
7390 -- Available_Subtype --
7391 -----------------------
7393 function Available_Subtype return Boolean is
7397 if GNATprove_Mode then
7398 Comp := First_Entity (Etype (P));
7399 while Present (Comp) loop
7400 if Chars (Comp) = Chars (Selector_Name (N)) then
7401 Set_Etype (N, Etype (Comp));
7402 Set_Entity (Selector_Name (N), Comp);
7403 Set_Etype (Selector_Name (N), Etype (Comp));
7407 Next_Component (Comp);
7412 end Available_Subtype;
7414 -----------------------------
7415 -- Is_Reference_In_Subunit --
7416 -----------------------------
7418 function Is_Reference_In_Subunit return Boolean is
7420 Comp_Unit : Node_Id;
7424 while Present (Comp_Unit)
7425 and then Nkind (Comp_Unit) /= N_Compilation_Unit
7427 Comp_Unit := Parent (Comp_Unit);
7430 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
7434 -- Now check whether the package is in the context of the subunit
7436 Clause := First (Context_Items (Comp_Unit));
7437 while Present (Clause) loop
7438 if Nkind (Clause) = N_With_Clause
7439 and then Entity (Name (Clause)) = P_Name
7448 end Is_Reference_In_Subunit;
7450 -- Start of processing for Find_Selected_Component
7455 if Nkind (P) = N_Error then
7459 -- If the selector already has an entity, the node has been constructed
7460 -- in the course of expansion, and is known to be valid. Do not verify
7461 -- that it is defined for the type (it may be a private component used
7462 -- in the expansion of record equality).
7464 if Present (Entity (Selector_Name (N))) then
7465 if No (Etype (N)) or else Etype (N) = Any_Type then
7467 Sel_Name : constant Node_Id := Selector_Name (N);
7468 Selector : constant Entity_Id := Entity (Sel_Name);
7472 Set_Etype (Sel_Name, Etype (Selector));
7474 if not Is_Entity_Name (P) then
7478 -- Build an actual subtype except for the first parameter
7479 -- of an init proc, where this actual subtype is by
7480 -- definition incorrect, since the object is uninitialized
7481 -- (and does not even have defined discriminants etc.)
7483 if Is_Entity_Name (P)
7484 and then Ekind (Entity (P)) = E_Function
7486 Nam := New_Copy (P);
7488 if Is_Overloaded (P) then
7489 Save_Interps (P, Nam);
7492 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
7494 Analyze_Selected_Component (N);
7497 elsif Ekind (Selector) = E_Component
7498 and then (not Is_Entity_Name (P)
7499 or else Chars (Entity (P)) /= Name_uInit)
7501 -- Check if we already have an available subtype we can use
7503 if Ekind (Etype (P)) = E_Record_Subtype
7504 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
7505 and then Is_Array_Type (Etype (Selector))
7506 and then not Is_Packed (Etype (Selector))
7507 and then Available_Subtype
7511 -- Do not build the subtype when referencing components of
7512 -- dispatch table wrappers. Required to avoid generating
7513 -- elaboration code with HI runtimes.
7515 elsif Is_RTE (Scope (Selector), RE_Dispatch_Table_Wrapper)
7517 Is_RTE (Scope (Selector), RE_No_Dispatch_Table_Wrapper)
7522 Build_Actual_Subtype_Of_Component
7523 (Etype (Selector), N);
7530 if No (C_Etype) then
7531 C_Etype := Etype (Selector);
7533 Insert_Action (N, C_Etype);
7534 C_Etype := Defining_Identifier (C_Etype);
7537 Set_Etype (N, C_Etype);
7540 -- If the selected component appears within a default expression
7541 -- and it has an actual subtype, the preanalysis has not yet
7542 -- completed its analysis, because Insert_Actions is disabled in
7543 -- that context. Within the init proc of the enclosing type we
7544 -- must complete this analysis, if an actual subtype was created.
7546 elsif Inside_Init_Proc then
7548 Typ : constant Entity_Id := Etype (N);
7549 Decl : constant Node_Id := Declaration_Node (Typ);
7551 if Nkind (Decl) = N_Subtype_Declaration
7552 and then not Analyzed (Decl)
7553 and then Is_List_Member (Decl)
7554 and then No (Parent (Decl))
7557 Insert_Action (N, Decl);
7564 elsif Is_Entity_Name (P) then
7565 P_Name := Entity (P);
7567 -- The prefix may denote an enclosing type which is the completion
7568 -- of an incomplete type declaration.
7570 if Is_Type (P_Name) then
7571 Set_Entity (P, Get_Full_View (P_Name));
7572 Set_Etype (P, Entity (P));
7573 P_Name := Entity (P);
7576 P_Type := Base_Type (Etype (P));
7578 if Debug_Flag_E then
7579 Write_Str ("Found prefix type to be ");
7580 Write_Entity_Info (P_Type, " "); Write_Eol;
7583 -- If the prefix's type is an access type, get to the record type
7585 if Is_Access_Type (P_Type) then
7586 P_Type := Implicitly_Designated_Type (P_Type);
7589 -- First check for components of a record object (not the
7590 -- result of a call, which is handled below).
7592 if Has_Components (P_Type)
7593 and then not Is_Overloadable (P_Name)
7594 and then not Is_Type (P_Name)
7596 -- Selected component of record. Type checking will validate
7597 -- name of selector.
7599 -- ??? Could we rewrite an implicit dereference into an explicit
7602 Analyze_Selected_Component (N);
7604 -- Reference to type name in predicate/invariant expression
7606 elsif Is_Concurrent_Type (P_Type)
7607 and then not In_Open_Scopes (P_Name)
7608 and then (not Is_Concurrent_Type (Etype (P_Name))
7609 or else not In_Open_Scopes (Etype (P_Name)))
7611 -- Call to protected operation or entry. Type checking is
7612 -- needed on the prefix.
7614 Analyze_Selected_Component (N);
7616 elsif (In_Open_Scopes (P_Name)
7617 and then Ekind (P_Name) /= E_Void
7618 and then not Is_Overloadable (P_Name))
7619 or else (Is_Concurrent_Type (Etype (P_Name))
7620 and then In_Open_Scopes (Etype (P_Name)))
7622 -- Prefix denotes an enclosing loop, block, or task, i.e. an
7623 -- enclosing construct that is not a subprogram or accept.
7625 -- A special case: a protected body may call an operation
7626 -- on an external object of the same type, in which case it
7627 -- is not an expanded name. If the prefix is the type itself,
7628 -- or the context is a single synchronized object it can only
7629 -- be interpreted as an expanded name.
7631 if Is_Concurrent_Type (Etype (P_Name)) then
7633 or else Present (Anonymous_Object (Etype (P_Name)))
7635 Find_Expanded_Name (N);
7638 Analyze_Selected_Component (N);
7643 Find_Expanded_Name (N);
7646 elsif Ekind (P_Name) = E_Package then
7647 Find_Expanded_Name (N);
7649 elsif Is_Overloadable (P_Name) then
7651 -- The subprogram may be a renaming (of an enclosing scope) as
7652 -- in the case of the name of the generic within an instantiation.
7654 if Ekind (P_Name) in E_Procedure | E_Function
7655 and then Present (Alias (P_Name))
7656 and then Is_Generic_Instance (Alias (P_Name))
7658 P_Name := Alias (P_Name);
7661 if Is_Overloaded (P) then
7663 -- The prefix must resolve to a unique enclosing construct
7666 Found : Boolean := False;
7671 Get_First_Interp (P, Ind, It);
7672 while Present (It.Nam) loop
7673 if In_Open_Scopes (It.Nam) then
7676 "prefix must be unique enclosing scope", N);
7677 Set_Entity (N, Any_Id);
7678 Set_Etype (N, Any_Type);
7687 Get_Next_Interp (Ind, It);
7692 if In_Open_Scopes (P_Name) then
7693 Set_Entity (P, P_Name);
7694 Set_Is_Overloaded (P, False);
7695 Find_Expanded_Name (N);
7698 -- If no interpretation as an expanded name is possible, it
7699 -- must be a selected component of a record returned by a
7700 -- function call. Reformat prefix as a function call, the rest
7701 -- is done by type resolution.
7703 -- Error if the prefix is procedure or entry, as is P.X
7705 if Ekind (P_Name) /= E_Function
7707 (not Is_Overloaded (P)
7708 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
7710 -- Prefix may mention a package that is hidden by a local
7711 -- declaration: let the user know. Scan the full homonym
7712 -- chain, the candidate package may be anywhere on it.
7714 if Present (Homonym (Current_Entity (P_Name))) then
7715 P_Name := Current_Entity (P_Name);
7717 while Present (P_Name) loop
7718 exit when Ekind (P_Name) = E_Package;
7719 P_Name := Homonym (P_Name);
7722 if Present (P_Name) then
7723 if not Is_Reference_In_Subunit then
7724 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
7726 ("package& is hidden by declaration#", N, P_Name);
7729 Set_Entity (Prefix (N), P_Name);
7730 Find_Expanded_Name (N);
7734 P_Name := Entity (Prefix (N));
7739 ("invalid prefix in selected component&", N, P_Name);
7740 Change_Selected_Component_To_Expanded_Name (N);
7741 Set_Entity (N, Any_Id);
7742 Set_Etype (N, Any_Type);
7744 -- Here we have a function call, so do the reformatting
7747 Nam := New_Copy (P);
7748 Save_Interps (P, Nam);
7750 -- We use Replace here because this is one of those cases
7751 -- where the parser has missclassified the node, and we fix
7752 -- things up and then do the semantic analysis on the fixed
7753 -- up node. Normally we do this using one of the Sinfo.CN
7754 -- routines, but this is too tricky for that.
7756 -- Note that using Rewrite would be wrong, because we would
7757 -- have a tree where the original node is unanalyzed.
7760 Make_Function_Call (Sloc (P), Name => Nam));
7762 -- Now analyze the reformatted node
7766 -- If the prefix is illegal after this transformation, there
7767 -- may be visibility errors on the prefix. The safest is to
7768 -- treat the selected component as an error.
7770 if Error_Posted (P) then
7771 Set_Etype (N, Any_Type);
7775 Analyze_Selected_Component (N);
7780 -- Remaining cases generate various error messages
7783 -- Format node as expanded name, to avoid cascaded errors
7785 Change_Selected_Component_To_Expanded_Name (N);
7786 Set_Entity (N, Any_Id);
7787 Set_Etype (N, Any_Type);
7789 -- Issue error message, but avoid this if error issued already.
7790 -- Use identifier of prefix if one is available.
7792 if P_Name = Any_Id then
7795 -- It is not an error if the prefix is the current instance of
7796 -- type name, e.g. the expression of a type aspect, when it is
7797 -- analyzed within a generic unit. We still have to verify that a
7798 -- component of that name exists, and decorate the node
7801 elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then
7806 Comp := First_Entity (Entity (P));
7807 while Present (Comp) loop
7808 if Chars (Comp) = Chars (Selector_Name (N)) then
7809 Set_Entity (N, Comp);
7810 Set_Etype (N, Etype (Comp));
7811 Set_Entity (Selector_Name (N), Comp);
7812 Set_Etype (Selector_Name (N), Etype (Comp));
7820 elsif Ekind (P_Name) = E_Void then
7821 Premature_Usage (P);
7823 elsif Nkind (P) /= N_Attribute_Reference then
7825 -- This may have been meant as a prefixed call to a primitive
7826 -- of an untagged type. If it is a function call check type of
7827 -- its first formal and add explanation.
7830 F : constant Entity_Id :=
7831 Current_Entity (Selector_Name (N));
7834 and then Is_Overloadable (F)
7835 and then Present (First_Entity (F))
7836 and then not Is_Tagged_Type (Etype (First_Entity (F)))
7839 ("prefixed call is only allowed for objects of a "
7840 & "tagged type", N);
7844 Error_Msg_N ("invalid prefix in selected component&", P);
7846 if Is_Incomplete_Type (P_Type)
7847 and then Is_Access_Type (Etype (P))
7850 ("\dereference must not be of an incomplete type "
7851 & "(RM 3.10.1)", P);
7855 Error_Msg_N ("invalid prefix in selected component", P);
7859 -- If prefix is not the name of an entity, it must be an expression,
7860 -- whose type is appropriate for a record. This is determined by
7863 Analyze_Selected_Component (N);
7866 Analyze_Dimension (N);
7867 end Find_Selected_Component;
7873 procedure Find_Type (N : Node_Id) is
7883 elsif Nkind (N) = N_Attribute_Reference then
7885 -- Class attribute. This is not valid in Ada 83 mode, but we do not
7886 -- need to enforce that at this point, since the declaration of the
7887 -- tagged type in the prefix would have been flagged already.
7889 if Attribute_Name (N) = Name_Class then
7890 Check_Restriction (No_Dispatch, N);
7891 Find_Type (Prefix (N));
7893 -- Propagate error from bad prefix
7895 if Etype (Prefix (N)) = Any_Type then
7896 Set_Entity (N, Any_Type);
7897 Set_Etype (N, Any_Type);
7901 T := Base_Type (Entity (Prefix (N)));
7903 -- Case where type is not known to be tagged. Its appearance in
7904 -- the prefix of the 'Class attribute indicates that the full view
7907 if not Is_Tagged_Type (T) then
7908 if Ekind (T) = E_Incomplete_Type then
7910 -- It is legal to denote the class type of an incomplete
7911 -- type. The full type will have to be tagged, of course.
7912 -- In Ada 2005 this usage is declared obsolescent, so we
7913 -- warn accordingly. This usage is only legal if the type
7914 -- is completed in the current scope, and not for a limited
7917 if Ada_Version >= Ada_2005 then
7919 -- Test whether the Available_View of a limited type view
7920 -- is tagged, since the limited view may not be marked as
7921 -- tagged if the type itself has an untagged incomplete
7922 -- type view in its package.
7924 if From_Limited_With (T)
7925 and then not Is_Tagged_Type (Available_View (T))
7928 ("prefix of Class attribute must be tagged", N);
7929 Set_Etype (N, Any_Type);
7930 Set_Entity (N, Any_Type);
7934 if Restriction_Check_Required (No_Obsolescent_Features)
7937 (No_Obsolescent_Features, Prefix (N));
7940 if Warn_On_Obsolescent_Feature then
7942 ("applying ''Class to an untagged incomplete type"
7943 & " is an obsolescent feature (RM J.11)?r?", N);
7948 Set_Is_Tagged_Type (T);
7949 Set_Direct_Primitive_Operations (T, New_Elmt_List);
7950 Make_Class_Wide_Type (T);
7951 Set_Entity (N, Class_Wide_Type (T));
7952 Set_Etype (N, Class_Wide_Type (T));
7954 elsif Ekind (T) = E_Private_Type
7955 and then not Is_Generic_Type (T)
7956 and then In_Private_Part (Scope (T))
7958 -- The Class attribute can be applied to an untagged private
7959 -- type fulfilled by a tagged type prior to the full type
7960 -- declaration (but only within the parent package's private
7961 -- part). Create the class-wide type now and check that the
7962 -- full type is tagged later during its analysis. Note that
7963 -- we do not mark the private type as tagged, unlike the
7964 -- case of incomplete types, because the type must still
7965 -- appear untagged to outside units.
7967 if No (Class_Wide_Type (T)) then
7968 Make_Class_Wide_Type (T);
7971 Set_Entity (N, Class_Wide_Type (T));
7972 Set_Etype (N, Class_Wide_Type (T));
7975 -- Should we introduce a type Any_Tagged and use Wrong_Type
7976 -- here, it would be a bit more consistent???
7979 ("tagged type required, found}",
7980 Prefix (N), First_Subtype (T));
7981 Set_Entity (N, Any_Type);
7985 -- Case of tagged type
7988 if Is_Concurrent_Type (T) then
7989 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
7991 -- Previous error. Create a class-wide type for the
7992 -- synchronized type itself, with minimal semantic
7993 -- attributes, to catch other errors in some ACATS tests.
7995 pragma Assert (Serious_Errors_Detected /= 0);
7996 Make_Class_Wide_Type (T);
7997 C := Class_Wide_Type (T);
7998 Set_First_Entity (C, First_Entity (T));
8001 C := Class_Wide_Type
8002 (Corresponding_Record_Type (Entity (Prefix (N))));
8006 C := Class_Wide_Type (Entity (Prefix (N)));
8009 Set_Entity_With_Checks (N, C);
8010 Generate_Reference (C, N);
8014 -- Base attribute, not allowed in Ada 83
8016 elsif Attribute_Name (N) = Name_Base then
8017 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
8019 ("(Ada 83) Base attribute not allowed in subtype mark", N);
8022 Find_Type (Prefix (N));
8023 Typ := Entity (Prefix (N));
8025 if Ada_Version >= Ada_95
8026 and then not Is_Scalar_Type (Typ)
8027 and then not Is_Generic_Type (Typ)
8030 ("prefix of Base attribute must be scalar type",
8033 elsif Warn_On_Redundant_Constructs
8034 and then Base_Type (Typ) = Typ
8036 Error_Msg_NE -- CODEFIX
8037 ("redundant attribute, & is its own base type?r?", N, Typ);
8040 T := Base_Type (Typ);
8042 -- Rewrite attribute reference with type itself (see similar
8043 -- processing in Analyze_Attribute, case Base). Preserve prefix
8044 -- if present, for other legality checks.
8046 if Nkind (Prefix (N)) = N_Expanded_Name then
8048 Make_Expanded_Name (Sloc (N),
8050 Prefix => New_Copy (Prefix (Prefix (N))),
8051 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
8054 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
8061 elsif Attribute_Name (N) = Name_Stub_Type then
8063 -- This is handled in Analyze_Attribute
8067 -- All other attributes are invalid in a subtype mark
8070 Error_Msg_N ("invalid attribute in subtype mark", N);
8076 if Is_Entity_Name (N) then
8077 T_Name := Entity (N);
8079 Error_Msg_N ("subtype mark required in this context", N);
8080 Set_Etype (N, Any_Type);
8084 if T_Name = Any_Id or else Etype (N) = Any_Type then
8086 -- Undefined id. Make it into a valid type
8088 Set_Entity (N, Any_Type);
8090 elsif not Is_Type (T_Name)
8091 and then T_Name /= Standard_Void_Type
8093 Error_Msg_Sloc := Sloc (T_Name);
8094 Error_Msg_N ("subtype mark required in this context", N);
8095 Error_Msg_NE ("\\found & declared#", N, T_Name);
8096 Set_Entity (N, Any_Type);
8099 -- If the type is an incomplete type created to handle
8100 -- anonymous access components of a record type, then the
8101 -- incomplete type is the visible entity and subsequent
8102 -- references will point to it. Mark the original full
8103 -- type as referenced, to prevent spurious warnings.
8105 if Is_Incomplete_Type (T_Name)
8106 and then Present (Full_View (T_Name))
8107 and then not Comes_From_Source (T_Name)
8109 Set_Referenced (Full_View (T_Name));
8112 T_Name := Get_Full_View (T_Name);
8114 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
8115 -- limited-with clauses
8117 if From_Limited_With (T_Name)
8118 and then Is_Incomplete_Type (T_Name)
8119 and then Present (Non_Limited_View (T_Name))
8120 and then Is_Interface (Non_Limited_View (T_Name))
8122 T_Name := Non_Limited_View (T_Name);
8125 if In_Open_Scopes (T_Name) then
8126 if Ekind (Base_Type (T_Name)) = E_Task_Type then
8128 -- In Ada 2005, a task name can be used in an access
8129 -- definition within its own body.
8131 if Ada_Version >= Ada_2005
8132 and then Nkind (Parent (N)) = N_Access_Definition
8134 Set_Entity (N, T_Name);
8135 Set_Etype (N, T_Name);
8140 ("task type cannot be used as type mark " &
8141 "within its own spec or body", N);
8144 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
8146 -- In Ada 2005, a protected name can be used in an access
8147 -- definition within its own body.
8149 if Ada_Version >= Ada_2005
8150 and then Nkind (Parent (N)) = N_Access_Definition
8152 Set_Entity (N, T_Name);
8153 Set_Etype (N, T_Name);
8158 ("protected type cannot be used as type mark " &
8159 "within its own spec or body", N);
8163 Error_Msg_N ("type declaration cannot refer to itself", N);
8166 Set_Etype (N, Any_Type);
8167 Set_Entity (N, Any_Type);
8168 Set_Error_Posted (T_Name);
8172 Set_Entity (N, T_Name);
8173 Set_Etype (N, T_Name);
8177 if Present (Etype (N)) and then Comes_From_Source (N) then
8178 if Is_Fixed_Point_Type (Etype (N)) then
8179 Check_Restriction (No_Fixed_Point, N);
8180 elsif Is_Floating_Point_Type (Etype (N)) then
8181 Check_Restriction (No_Floating_Point, N);
8184 -- A Ghost type must appear in a specific context
8186 if Is_Ghost_Entity (Etype (N)) then
8187 Check_Ghost_Context (Etype (N), N);
8192 --------------------
8193 -- Has_Components --
8194 --------------------
8196 function Has_Components (Typ : Entity_Id) return Boolean is
8198 return Is_Record_Type (Typ)
8199 or else (Is_Private_Type (Typ) and then Has_Discriminants (Typ))
8200 or else (Is_Task_Type (Typ) and then Has_Discriminants (Typ))
8201 or else (Is_Incomplete_Type (Typ)
8202 and then From_Limited_With (Typ)
8203 and then Is_Record_Type (Available_View (Typ)));
8206 ------------------------------------
8207 -- Has_Implicit_Character_Literal --
8208 ------------------------------------
8210 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
8212 Found : Boolean := False;
8213 P : constant Entity_Id := Entity (Prefix (N));
8214 Priv_Id : Entity_Id := Empty;
8217 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8218 Priv_Id := First_Private_Entity (P);
8221 if P = Standard_Standard then
8222 Change_Selected_Component_To_Expanded_Name (N);
8223 Rewrite (N, Selector_Name (N));
8225 Set_Etype (Original_Node (N), Standard_Character);
8229 Id := First_Entity (P);
8230 while Present (Id) and then Id /= Priv_Id loop
8231 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
8233 -- We replace the node with the literal itself, resolve as a
8234 -- character, and set the type correctly.
8237 Change_Selected_Component_To_Expanded_Name (N);
8238 Rewrite (N, Selector_Name (N));
8241 Set_Etype (Original_Node (N), Id);
8245 -- More than one type derived from Character in given scope.
8246 -- Collect all possible interpretations.
8248 Add_One_Interp (N, Id, Id);
8256 end Has_Implicit_Character_Literal;
8258 ----------------------
8259 -- Has_Private_With --
8260 ----------------------
8262 function Has_Private_With (E : Entity_Id) return Boolean is
8263 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
8267 Item := First (Context_Items (Comp_Unit));
8268 while Present (Item) loop
8269 if Nkind (Item) = N_With_Clause
8270 and then Private_Present (Item)
8271 and then Entity (Name (Item)) = E
8280 end Has_Private_With;
8282 ---------------------------
8283 -- Has_Implicit_Operator --
8284 ---------------------------
8286 function Has_Implicit_Operator (N : Node_Id) return Boolean is
8287 Op_Id : constant Name_Id := Chars (Selector_Name (N));
8288 P : constant Entity_Id := Entity (Prefix (N));
8290 Priv_Id : Entity_Id := Empty;
8292 procedure Add_Implicit_Operator
8294 Op_Type : Entity_Id := Empty);
8295 -- Add implicit interpretation to node N, using the type for which a
8296 -- predefined operator exists. If the operator yields a boolean type,
8297 -- the Operand_Type is implicitly referenced by the operator, and a
8298 -- reference to it must be generated.
8300 ---------------------------
8301 -- Add_Implicit_Operator --
8302 ---------------------------
8304 procedure Add_Implicit_Operator
8306 Op_Type : Entity_Id := Empty)
8308 Predef_Op : Entity_Id;
8311 Predef_Op := Current_Entity (Selector_Name (N));
8312 while Present (Predef_Op)
8313 and then Scope (Predef_Op) /= Standard_Standard
8315 Predef_Op := Homonym (Predef_Op);
8318 if Nkind (N) = N_Selected_Component then
8319 Change_Selected_Component_To_Expanded_Name (N);
8322 -- If the context is an unanalyzed function call, determine whether
8323 -- a binary or unary interpretation is required.
8325 if Nkind (Parent (N)) = N_Indexed_Component then
8327 Is_Binary_Call : constant Boolean :=
8329 (Next (First (Expressions (Parent (N)))));
8330 Is_Binary_Op : constant Boolean :=
8332 (Predef_Op) /= Last_Entity (Predef_Op);
8333 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
8336 if Is_Binary_Call then
8337 if Is_Binary_Op then
8338 Add_One_Interp (N, Predef_Op, T);
8340 Add_One_Interp (N, Predef_Op2, T);
8343 if not Is_Binary_Op then
8344 Add_One_Interp (N, Predef_Op, T);
8346 -- Predef_Op2 may be empty in case of previous errors
8348 elsif Present (Predef_Op2) then
8349 Add_One_Interp (N, Predef_Op2, T);
8355 Add_One_Interp (N, Predef_Op, T);
8357 -- For operators with unary and binary interpretations, if
8358 -- context is not a call, add both
8360 if Present (Homonym (Predef_Op)) then
8361 Add_One_Interp (N, Homonym (Predef_Op), T);
8365 -- The node is a reference to a predefined operator, and
8366 -- an implicit reference to the type of its operands.
8368 if Present (Op_Type) then
8369 Generate_Operator_Reference (N, Op_Type);
8371 Generate_Operator_Reference (N, T);
8373 end Add_Implicit_Operator;
8375 -- Start of processing for Has_Implicit_Operator
8378 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8379 Priv_Id := First_Private_Entity (P);
8382 Id := First_Entity (P);
8386 -- Boolean operators: an implicit declaration exists if the scope
8387 -- contains a declaration for a derived Boolean type, or for an
8388 -- array of Boolean type.
8395 while Id /= Priv_Id loop
8396 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
8397 Add_Implicit_Operator (Id);
8404 -- Equality: look for any non-limited type (result is Boolean)
8409 while Id /= Priv_Id loop
8411 and then not Is_Limited_Type (Id)
8412 and then Is_Base_Type (Id)
8414 Add_Implicit_Operator (Standard_Boolean, Id);
8421 -- Comparison operators: scalar type, or array of scalar
8428 while Id /= Priv_Id loop
8429 if (Is_Scalar_Type (Id)
8430 or else (Is_Array_Type (Id)
8431 and then Is_Scalar_Type (Component_Type (Id))))
8432 and then Is_Base_Type (Id)
8434 Add_Implicit_Operator (Standard_Boolean, Id);
8441 -- Arithmetic operators: any numeric type
8452 while Id /= Priv_Id loop
8453 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
8454 Add_Implicit_Operator (Id);
8461 -- Concatenation: any one-dimensional array type
8463 when Name_Op_Concat =>
8464 while Id /= Priv_Id loop
8465 if Is_Array_Type (Id)
8466 and then Number_Dimensions (Id) = 1
8467 and then Is_Base_Type (Id)
8469 Add_Implicit_Operator (Id);
8476 -- What is the others condition here? Should we be using a
8477 -- subtype of Name_Id that would restrict to operators ???
8483 -- If we fall through, then we do not have an implicit operator
8486 end Has_Implicit_Operator;
8488 -----------------------------------
8489 -- Has_Loop_In_Inner_Open_Scopes --
8490 -----------------------------------
8492 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
8494 -- Several scope stacks are maintained by Scope_Stack. The base of the
8495 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8496 -- flag in the scope stack entry. Note that the scope stacks used to
8497 -- simply be delimited implicitly by the presence of Standard_Standard
8498 -- at their base, but there now are cases where this is not sufficient
8499 -- because Standard_Standard actually may appear in the middle of the
8500 -- active set of scopes.
8502 for J in reverse 0 .. Scope_Stack.Last loop
8504 -- S was reached without seing a loop scope first
8506 if Scope_Stack.Table (J).Entity = S then
8509 -- S was not yet reached, so it contains at least one inner loop
8511 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
8515 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8516 -- cases where Standard_Standard appears in the middle of the active
8517 -- set of scopes. This affects the declaration and overriding of
8518 -- private inherited operations in instantiations of generic child
8521 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
8524 raise Program_Error; -- unreachable
8525 end Has_Loop_In_Inner_Open_Scopes;
8527 --------------------
8528 -- In_Open_Scopes --
8529 --------------------
8531 function In_Open_Scopes (S : Entity_Id) return Boolean is
8533 -- Several scope stacks are maintained by Scope_Stack. The base of the
8534 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8535 -- flag in the scope stack entry. Note that the scope stacks used to
8536 -- simply be delimited implicitly by the presence of Standard_Standard
8537 -- at their base, but there now are cases where this is not sufficient
8538 -- because Standard_Standard actually may appear in the middle of the
8539 -- active set of scopes.
8541 for J in reverse 0 .. Scope_Stack.Last loop
8542 if Scope_Stack.Table (J).Entity = S then
8546 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8547 -- cases where Standard_Standard appears in the middle of the active
8548 -- set of scopes. This affects the declaration and overriding of
8549 -- private inherited operations in instantiations of generic child
8552 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
8558 -----------------------------
8559 -- Inherit_Renamed_Profile --
8560 -----------------------------
8562 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
8569 if Ekind (Old_S) = E_Operator then
8570 New_F := First_Formal (New_S);
8572 while Present (New_F) loop
8573 Set_Etype (New_F, Base_Type (Etype (New_F)));
8574 Next_Formal (New_F);
8577 Set_Etype (New_S, Base_Type (Etype (New_S)));
8580 New_F := First_Formal (New_S);
8581 Old_F := First_Formal (Old_S);
8583 while Present (New_F) loop
8584 New_T := Etype (New_F);
8585 Old_T := Etype (Old_F);
8587 -- If the new type is a renaming of the old one, as is the case
8588 -- for actuals in instances, retain its name, to simplify later
8591 if Nkind (Parent (New_T)) = N_Subtype_Declaration
8592 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
8593 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
8597 Set_Etype (New_F, Old_T);
8600 Next_Formal (New_F);
8601 Next_Formal (Old_F);
8604 pragma Assert (No (Old_F));
8606 if Ekind (Old_S) in E_Function | E_Enumeration_Literal then
8607 Set_Etype (New_S, Etype (Old_S));
8610 end Inherit_Renamed_Profile;
8616 procedure Initialize is
8621 -------------------------
8622 -- Install_Use_Clauses --
8623 -------------------------
8625 procedure Install_Use_Clauses
8627 Force_Installation : Boolean := False)
8633 while Present (U) loop
8635 -- Case of USE package
8637 if Nkind (U) = N_Use_Package_Clause then
8638 Use_One_Package (U, Name (U), True);
8643 Use_One_Type (Subtype_Mark (U), Force => Force_Installation);
8647 Next_Use_Clause (U);
8649 end Install_Use_Clauses;
8651 ----------------------
8652 -- Mark_Use_Clauses --
8653 ----------------------
8655 procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is
8656 procedure Mark_Parameters (Call : Entity_Id);
8657 -- Perform use_type_clause marking for all parameters in a subprogram
8658 -- or operator call.
8660 procedure Mark_Use_Package (Pak : Entity_Id);
8661 -- Move up the Prev_Use_Clause chain for packages denoted by Pak -
8662 -- marking each clause in the chain as effective in the process.
8664 procedure Mark_Use_Type (E : Entity_Id);
8665 -- Similar to Do_Use_Package_Marking except we move up the
8666 -- Prev_Use_Clause chain for the type denoted by E.
8668 ---------------------
8669 -- Mark_Parameters --
8670 ---------------------
8672 procedure Mark_Parameters (Call : Entity_Id) is
8676 -- Move through all of the formals
8678 Curr := First_Formal (Call);
8679 while Present (Curr) loop
8680 Mark_Use_Type (Curr);
8685 -- Handle the return type
8687 Mark_Use_Type (Call);
8688 end Mark_Parameters;
8690 ----------------------
8691 -- Mark_Use_Package --
8692 ----------------------
8694 procedure Mark_Use_Package (Pak : Entity_Id) is
8698 -- Ignore cases where the scope of the type is not a package (e.g.
8699 -- Standard_Standard).
8701 if Ekind (Pak) /= E_Package then
8705 Curr := Current_Use_Clause (Pak);
8706 while Present (Curr)
8707 and then not Is_Effective_Use_Clause (Curr)
8709 -- We need to mark the previous use clauses as effective, but
8710 -- each use clause may in turn render other use_package_clauses
8711 -- effective. Additionally, it is possible to have a parent
8712 -- package renamed as a child of itself so we must check the
8713 -- prefix entity is not the same as the package we are marking.
8715 if Nkind (Name (Curr)) /= N_Identifier
8716 and then Present (Prefix (Name (Curr)))
8717 and then Entity (Prefix (Name (Curr))) /= Pak
8719 Mark_Use_Package (Entity (Prefix (Name (Curr))));
8721 -- It is also possible to have a child package without a prefix
8722 -- that relies on a previous use_package_clause.
8724 elsif Nkind (Name (Curr)) = N_Identifier
8725 and then Is_Child_Unit (Entity (Name (Curr)))
8727 Mark_Use_Package (Scope (Entity (Name (Curr))));
8730 -- Mark the use_package_clause as effective and move up the chain
8732 Set_Is_Effective_Use_Clause (Curr);
8734 Curr := Prev_Use_Clause (Curr);
8736 end Mark_Use_Package;
8742 procedure Mark_Use_Type (E : Entity_Id) is
8747 -- Ignore void types and unresolved string literals and primitives
8749 if Nkind (E) = N_String_Literal
8750 or else Nkind (Etype (E)) not in N_Entity
8751 or else not Is_Type (Etype (E))
8756 -- Primitives with class-wide operands might additionally render
8757 -- their base type's use_clauses effective - so do a recursive check
8760 Base := Base_Type (Etype (E));
8762 if Ekind (Base) = E_Class_Wide_Type then
8763 Mark_Use_Type (Base);
8766 -- The package containing the type or operator function being used
8767 -- may be in use as well, so mark any use_package_clauses for it as
8768 -- effective. There are also additional sanity checks performed here
8769 -- for ignoring previous errors.
8771 Mark_Use_Package (Scope (Base));
8773 if Nkind (E) in N_Op
8774 and then Present (Entity (E))
8775 and then Present (Scope (Entity (E)))
8777 Mark_Use_Package (Scope (Entity (E)));
8780 Curr := Current_Use_Clause (Base);
8781 while Present (Curr)
8782 and then not Is_Effective_Use_Clause (Curr)
8784 -- Current use_type_clause may render other use_package_clauses
8787 if Nkind (Subtype_Mark (Curr)) /= N_Identifier
8788 and then Present (Prefix (Subtype_Mark (Curr)))
8790 Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr))));
8793 -- Mark the use_type_clause as effective and move up the chain
8795 Set_Is_Effective_Use_Clause (Curr);
8797 Curr := Prev_Use_Clause (Curr);
8801 -- Start of processing for Mark_Use_Clauses
8804 -- Use clauses in and of themselves do not count as a "use" of a
8807 if Nkind (Parent (Id)) in N_Use_Package_Clause | N_Use_Type_Clause then
8813 if Nkind (Id) in N_Entity then
8815 -- Mark the entity's package
8817 if Is_Potentially_Use_Visible (Id) then
8818 Mark_Use_Package (Scope (Id));
8821 -- Mark enumeration literals
8823 if Ekind (Id) = E_Enumeration_Literal then
8828 elsif (Is_Overloadable (Id)
8829 or else Is_Generic_Subprogram (Id))
8830 and then (Is_Potentially_Use_Visible (Id)
8831 or else Is_Intrinsic_Subprogram (Id)
8832 or else (Ekind (Id) in E_Function | E_Procedure
8833 and then Is_Generic_Actual_Subprogram (Id)))
8835 Mark_Parameters (Id);
8843 if Nkind (Id) in N_Op then
8845 -- At this point the left operand may not be resolved if we are
8846 -- encountering multiple operators next to eachother in an
8849 if Nkind (Id) in N_Binary_Op
8850 and then not (Nkind (Left_Opnd (Id)) in N_Op)
8852 Mark_Use_Type (Left_Opnd (Id));
8855 Mark_Use_Type (Right_Opnd (Id));
8858 -- Mark entity identifiers
8860 elsif Nkind (Id) in N_Has_Entity
8861 and then (Is_Potentially_Use_Visible (Entity (Id))
8862 or else (Is_Generic_Instance (Entity (Id))
8863 and then Is_Immediately_Visible (Entity (Id))))
8865 -- Ignore fully qualified names as they do not count as a "use" of
8868 if Nkind (Id) in N_Identifier | N_Operator_Symbol
8869 or else (Present (Prefix (Id))
8870 and then Scope (Entity (Id)) /= Entity (Prefix (Id)))
8872 Mark_Use_Clauses (Entity (Id));
8876 end Mark_Use_Clauses;
8878 --------------------------------
8879 -- Most_Descendant_Use_Clause --
8880 --------------------------------
8882 function Most_Descendant_Use_Clause
8883 (Clause1 : Entity_Id;
8884 Clause2 : Entity_Id) return Entity_Id
8890 if Clause1 = Clause2 then
8894 -- We determine which one is the most descendant by the scope distance
8895 -- to the ultimate parent unit.
8897 Scope1 := Entity_Of_Unit (Unit (Parent (Clause1)));
8898 Scope2 := Entity_Of_Unit (Unit (Parent (Clause2)));
8899 while Scope1 /= Standard_Standard
8900 and then Scope2 /= Standard_Standard
8902 Scope1 := Scope (Scope1);
8903 Scope2 := Scope (Scope2);
8905 if not Present (Scope1) then
8907 elsif not Present (Scope2) then
8912 if Scope1 = Standard_Standard then
8917 end Most_Descendant_Use_Clause;
8923 procedure Pop_Scope is
8924 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8925 S : constant Entity_Id := SST.Entity;
8928 if Debug_Flag_E then
8932 -- Set Default_Storage_Pool field of the library unit if necessary
8934 if Is_Package_Or_Generic_Package (S)
8936 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
8939 Aux : constant Node_Id :=
8940 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
8942 if No (Default_Storage_Pool (Aux)) then
8943 Set_Default_Storage_Pool (Aux, Default_Pool);
8948 Scope_Suppress := SST.Save_Scope_Suppress;
8949 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
8950 Check_Policy_List := SST.Save_Check_Policy_List;
8951 Default_Pool := SST.Save_Default_Storage_Pool;
8952 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
8953 SPARK_Mode := SST.Save_SPARK_Mode;
8954 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
8955 Default_SSO := SST.Save_Default_SSO;
8956 Uneval_Old := SST.Save_Uneval_Old;
8958 if Debug_Flag_W then
8959 Write_Str ("<-- exiting scope: ");
8960 Write_Name (Chars (Current_Scope));
8961 Write_Str (", Depth=");
8962 Write_Int (Int (Scope_Stack.Last));
8966 End_Use_Clauses (SST.First_Use_Clause);
8968 -- If the actions to be wrapped are still there they will get lost
8969 -- causing incomplete code to be generated. It is better to abort in
8970 -- this case (and we do the abort even with assertions off since the
8971 -- penalty is incorrect code generation).
8973 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
8974 raise Program_Error;
8977 -- Free last subprogram name if allocated, and pop scope
8979 Free (SST.Last_Subprogram_Name);
8980 Scope_Stack.Decrement_Last;
8987 procedure Push_Scope (S : Entity_Id) is
8988 E : constant Entity_Id := Scope (S);
8991 if Ekind (S) = E_Void then
8994 -- Set scope depth if not a non-concurrent type, and we have not yet set
8995 -- the scope depth. This means that we have the first occurrence of the
8996 -- scope, and this is where the depth is set.
8998 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
8999 and then not Scope_Depth_Set (S)
9001 if S = Standard_Standard then
9002 Set_Scope_Depth_Value (S, Uint_0);
9004 elsif Is_Child_Unit (S) then
9005 Set_Scope_Depth_Value (S, Uint_1);
9007 elsif not Is_Record_Type (Current_Scope) then
9008 if Ekind (S) = E_Loop then
9009 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
9011 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
9016 Scope_Stack.Increment_Last;
9019 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
9023 SST.Save_Scope_Suppress := Scope_Suppress;
9024 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
9025 SST.Save_Check_Policy_List := Check_Policy_List;
9026 SST.Save_Default_Storage_Pool := Default_Pool;
9027 SST.Save_No_Tagged_Streams := No_Tagged_Streams;
9028 SST.Save_SPARK_Mode := SPARK_Mode;
9029 SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma;
9030 SST.Save_Default_SSO := Default_SSO;
9031 SST.Save_Uneval_Old := Uneval_Old;
9033 -- Each new scope pushed onto the scope stack inherits the component
9034 -- alignment of the previous scope. This emulates the "visibility"
9035 -- semantics of pragma Component_Alignment.
9037 if Scope_Stack.Last > Scope_Stack.First then
9038 SST.Component_Alignment_Default :=
9040 (Scope_Stack.Last - 1).Component_Alignment_Default;
9042 -- Otherwise, this is the first scope being pushed on the scope
9043 -- stack. Inherit the component alignment from the configuration
9044 -- form of pragma Component_Alignment (if any).
9047 SST.Component_Alignment_Default :=
9048 Configuration_Component_Alignment;
9051 SST.Last_Subprogram_Name := null;
9052 SST.Is_Transient := False;
9053 SST.Node_To_Be_Wrapped := Empty;
9054 SST.Pending_Freeze_Actions := No_List;
9055 SST.Actions_To_Be_Wrapped := (others => No_List);
9056 SST.First_Use_Clause := Empty;
9057 SST.Is_Active_Stack_Base := False;
9058 SST.Previous_Visibility := False;
9059 SST.Locked_Shared_Objects := No_Elist;
9062 if Debug_Flag_W then
9063 Write_Str ("--> new scope: ");
9064 Write_Name (Chars (Current_Scope));
9065 Write_Str (", Id=");
9066 Write_Int (Int (Current_Scope));
9067 Write_Str (", Depth=");
9068 Write_Int (Int (Scope_Stack.Last));
9072 -- Deal with copying flags from the previous scope to this one. This is
9073 -- not necessary if either scope is standard, or if the new scope is a
9076 if S /= Standard_Standard
9077 and then Scope (S) /= Standard_Standard
9078 and then not Is_Child_Unit (S)
9080 if Nkind (E) not in N_Entity then
9084 -- Copy categorization flags from Scope (S) to S, this is not done
9085 -- when Scope (S) is Standard_Standard since propagation is from
9086 -- library unit entity inwards. Copy other relevant attributes as
9087 -- well (Discard_Names in particular).
9089 -- We only propagate inwards for library level entities,
9090 -- inner level subprograms do not inherit the categorization.
9092 if Is_Library_Level_Entity (S) then
9093 Set_Is_Preelaborated (S, Is_Preelaborated (E));
9094 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
9095 Set_Discard_Names (S, Discard_Names (E));
9096 Set_Suppress_Value_Tracking_On_Call
9097 (S, Suppress_Value_Tracking_On_Call (E));
9098 Set_Categorization_From_Scope (E => S, Scop => E);
9102 if Is_Child_Unit (S)
9103 and then Present (E)
9104 and then Is_Package_Or_Generic_Package (E)
9106 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9109 Aux : constant Node_Id :=
9110 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
9112 if Present (Default_Storage_Pool (Aux)) then
9113 Default_Pool := Default_Storage_Pool (Aux);
9119 ---------------------
9120 -- Premature_Usage --
9121 ---------------------
9123 procedure Premature_Usage (N : Node_Id) is
9124 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
9125 E : Entity_Id := Entity (N);
9128 -- Within an instance, the analysis of the actual for a formal object
9129 -- does not see the name of the object itself. This is significant only
9130 -- if the object is an aggregate, where its analysis does not do any
9131 -- name resolution on component associations. (see 4717-008). In such a
9132 -- case, look for the visible homonym on the chain.
9134 if In_Instance and then Present (Homonym (E)) then
9136 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
9142 Set_Etype (N, Etype (E));
9148 when N_Component_Declaration =>
9150 ("component&! cannot be used before end of record declaration",
9153 when N_Parameter_Specification =>
9155 ("formal parameter&! cannot be used before end of specification",
9158 when N_Discriminant_Specification =>
9160 ("discriminant&! cannot be used before end of discriminant part",
9163 when N_Procedure_Specification | N_Function_Specification =>
9165 ("subprogram&! cannot be used before end of its declaration",
9168 when N_Full_Type_Declaration | N_Subtype_Declaration =>
9170 ("type& cannot be used before end of its declaration!", N);
9174 ("object& cannot be used before end of its declaration!", N);
9176 -- If the premature reference appears as the expression in its own
9177 -- declaration, rewrite it to prevent compiler loops in subsequent
9178 -- uses of this mangled declaration in address clauses.
9180 if Nkind (Parent (N)) = N_Object_Declaration then
9181 Set_Entity (N, Any_Id);
9184 end Premature_Usage;
9186 ------------------------
9187 -- Present_System_Aux --
9188 ------------------------
9190 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
9192 Aux_Name : Unit_Name_Type;
9193 Unum : Unit_Number_Type;
9198 function Find_System (C_Unit : Node_Id) return Entity_Id;
9199 -- Scan context clause of compilation unit to find with_clause
9206 function Find_System (C_Unit : Node_Id) return Entity_Id is
9207 With_Clause : Node_Id;
9210 With_Clause := First (Context_Items (C_Unit));
9211 while Present (With_Clause) loop
9212 if (Nkind (With_Clause) = N_With_Clause
9213 and then Chars (Name (With_Clause)) = Name_System)
9214 and then Comes_From_Source (With_Clause)
9225 -- Start of processing for Present_System_Aux
9228 -- The child unit may have been loaded and analyzed already
9230 if Present (System_Aux_Id) then
9233 -- If no previous pragma for System.Aux, nothing to load
9235 elsif No (System_Extend_Unit) then
9238 -- Use the unit name given in the pragma to retrieve the unit.
9239 -- Verify that System itself appears in the context clause of the
9240 -- current compilation. If System is not present, an error will
9241 -- have been reported already.
9244 With_Sys := Find_System (Cunit (Current_Sem_Unit));
9246 The_Unit := Unit (Cunit (Current_Sem_Unit));
9250 (Nkind (The_Unit) = N_Package_Body
9251 or else (Nkind (The_Unit) = N_Subprogram_Body
9252 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
9254 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
9257 if No (With_Sys) and then Present (N) then
9259 -- If we are compiling a subunit, we need to examine its
9260 -- context as well (Current_Sem_Unit is the parent unit);
9262 The_Unit := Parent (N);
9263 while Nkind (The_Unit) /= N_Compilation_Unit loop
9264 The_Unit := Parent (The_Unit);
9267 if Nkind (Unit (The_Unit)) = N_Subunit then
9268 With_Sys := Find_System (The_Unit);
9272 if No (With_Sys) then
9276 Loc := Sloc (With_Sys);
9277 Get_Name_String (Chars (Expression (System_Extend_Unit)));
9278 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
9279 Name_Buffer (1 .. 7) := "system.";
9280 Name_Buffer (Name_Len + 8) := '%';
9281 Name_Buffer (Name_Len + 9) := 's';
9282 Name_Len := Name_Len + 9;
9283 Aux_Name := Name_Find;
9287 (Load_Name => Aux_Name,
9290 Error_Node => With_Sys);
9292 if Unum /= No_Unit then
9293 Semantics (Cunit (Unum));
9295 Defining_Entity (Specification (Unit (Cunit (Unum))));
9298 Make_With_Clause (Loc,
9300 Make_Expanded_Name (Loc,
9301 Chars => Chars (System_Aux_Id),
9303 New_Occurrence_Of (Scope (System_Aux_Id), Loc),
9304 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
9306 Set_Entity (Name (Withn), System_Aux_Id);
9308 Set_Corresponding_Spec (Withn, System_Aux_Id);
9309 Set_First_Name (Withn);
9310 Set_Implicit_With (Withn);
9311 Set_Library_Unit (Withn, Cunit (Unum));
9313 Insert_After (With_Sys, Withn);
9314 Mark_Rewrite_Insertion (Withn);
9315 Set_Context_Installed (Withn);
9319 -- Here if unit load failed
9322 Error_Msg_Name_1 := Name_System;
9323 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
9325 ("extension package `%.%` does not exist",
9326 Opt.System_Extend_Unit);
9330 end Present_System_Aux;
9332 -------------------------
9333 -- Restore_Scope_Stack --
9334 -------------------------
9336 procedure Restore_Scope_Stack
9338 Handle_Use : Boolean := True)
9340 SS_Last : constant Int := Scope_Stack.Last;
9344 -- Restore visibility of previous scope stack, if any, using the list
9345 -- we saved (we use Remove, since this list will not be used again).
9348 Elmt := Last_Elmt (List);
9349 exit when Elmt = No_Elmt;
9350 Set_Is_Immediately_Visible (Node (Elmt));
9351 Remove_Last_Elmt (List);
9354 -- Restore use clauses
9356 if SS_Last >= Scope_Stack.First
9357 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9361 (Scope_Stack.Table (SS_Last).First_Use_Clause,
9362 Force_Installation => True);
9364 end Restore_Scope_Stack;
9366 ----------------------
9367 -- Save_Scope_Stack --
9368 ----------------------
9370 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
9371 -- consuming any memory. That is, Save_Scope_Stack took care of removing
9372 -- from immediate visibility entities and Restore_Scope_Stack took care
9373 -- of restoring their visibility analyzing the context of each entity. The
9374 -- problem of such approach is that it was fragile and caused unexpected
9375 -- visibility problems, and indeed one test was found where there was a
9378 -- Furthermore, the following experiment was carried out:
9380 -- - Save_Scope_Stack was modified to store in an Elist1 all those
9381 -- entities whose attribute Is_Immediately_Visible is modified
9382 -- from True to False.
9384 -- - Restore_Scope_Stack was modified to store in another Elist2
9385 -- all the entities whose attribute Is_Immediately_Visible is
9386 -- modified from False to True.
9388 -- - Extra code was added to verify that all the elements of Elist1
9389 -- are found in Elist2
9391 -- This test shows that there may be more occurrences of this problem which
9392 -- have not yet been detected. As a result, we replaced that approach by
9393 -- the current one in which Save_Scope_Stack returns the list of entities
9394 -- whose visibility is changed, and that list is passed to Restore_Scope_
9395 -- Stack to undo that change. This approach is simpler and safer, although
9396 -- it consumes more memory.
9398 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
9399 Result : constant Elist_Id := New_Elmt_List;
9402 SS_Last : constant Int := Scope_Stack.Last;
9404 procedure Remove_From_Visibility (E : Entity_Id);
9405 -- If E is immediately visible then append it to the result and remove
9406 -- it temporarily from visibility.
9408 ----------------------------
9409 -- Remove_From_Visibility --
9410 ----------------------------
9412 procedure Remove_From_Visibility (E : Entity_Id) is
9414 if Is_Immediately_Visible (E) then
9415 Append_Elmt (E, Result);
9416 Set_Is_Immediately_Visible (E, False);
9418 end Remove_From_Visibility;
9420 -- Start of processing for Save_Scope_Stack
9423 if SS_Last >= Scope_Stack.First
9424 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9427 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
9430 -- If the call is from within a compilation unit, as when called from
9431 -- Rtsfind, make current entries in scope stack invisible while we
9432 -- analyze the new unit.
9434 for J in reverse 0 .. SS_Last loop
9435 exit when Scope_Stack.Table (J).Entity = Standard_Standard
9436 or else No (Scope_Stack.Table (J).Entity);
9438 S := Scope_Stack.Table (J).Entity;
9440 Remove_From_Visibility (S);
9442 E := First_Entity (S);
9443 while Present (E) loop
9444 Remove_From_Visibility (E);
9452 end Save_Scope_Stack;
9458 procedure Set_Use (L : List_Id) is
9464 while Present (Decl) loop
9465 if Nkind (Decl) = N_Use_Package_Clause then
9466 Chain_Use_Clause (Decl);
9467 Use_One_Package (Decl, Name (Decl));
9469 elsif Nkind (Decl) = N_Use_Type_Clause then
9470 Chain_Use_Clause (Decl);
9471 Use_One_Type (Subtype_Mark (Decl));
9480 -----------------------------
9481 -- Update_Use_Clause_Chain --
9482 -----------------------------
9484 procedure Update_Use_Clause_Chain is
9486 procedure Update_Chain_In_Scope (Level : Int);
9487 -- Iterate through one level in the scope stack verifying each use-type
9488 -- clause within said level is used then reset the Current_Use_Clause
9489 -- to a redundant use clause outside of the current ending scope if such
9492 ---------------------------
9493 -- Update_Chain_In_Scope --
9494 ---------------------------
9496 procedure Update_Chain_In_Scope (Level : Int) is
9501 -- Loop through all use clauses within the scope dictated by Level
9503 Curr := Scope_Stack.Table (Level).First_Use_Clause;
9504 while Present (Curr) loop
9506 -- Retrieve the subtype mark or name within the current current
9509 if Nkind (Curr) = N_Use_Type_Clause then
9510 N := Subtype_Mark (Curr);
9515 -- If warnings for unreferenced entities are enabled and the
9516 -- current use clause has not been marked effective.
9518 if Check_Unreferenced
9519 and then Comes_From_Source (Curr)
9520 and then not Is_Effective_Use_Clause (Curr)
9521 and then not In_Instance
9522 and then not In_Inlined_Body
9524 -- We are dealing with a potentially unused use_package_clause
9526 if Nkind (Curr) = N_Use_Package_Clause then
9528 -- Renamings and formal subprograms may cause the associated
9529 -- node to be marked as effective instead of the original.
9531 if not (Present (Associated_Node (N))
9534 (Associated_Node (N)))
9535 and then Is_Effective_Use_Clause
9537 (Associated_Node (N))))
9539 Error_Msg_Node_1 := Entity (N);
9541 ("use clause for package & has no effect?u?",
9545 -- We are dealing with an unused use_type_clause
9548 Error_Msg_Node_1 := Etype (N);
9550 ("use clause for } has no effect?u?", Curr, Etype (N));
9554 -- Verify that we haven't already processed a redundant
9555 -- use_type_clause within the same scope before we move the
9556 -- current use clause up to a previous one for type T.
9558 if Present (Prev_Use_Clause (Curr)) then
9559 Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr));
9562 Next_Use_Clause (Curr);
9564 end Update_Chain_In_Scope;
9566 -- Start of processing for Update_Use_Clause_Chain
9569 Update_Chain_In_Scope (Scope_Stack.Last);
9571 -- Deal with use clauses within the context area if the current
9572 -- scope is a compilation unit.
9574 if Is_Compilation_Unit (Current_Scope)
9575 and then Sloc (Scope_Stack.Table
9576 (Scope_Stack.Last - 1).Entity) = Standard_Location
9578 Update_Chain_In_Scope (Scope_Stack.Last - 1);
9580 end Update_Use_Clause_Chain;
9582 ---------------------
9583 -- Use_One_Package --
9584 ---------------------
9586 procedure Use_One_Package
9588 Pack_Name : Entity_Id := Empty;
9589 Force : Boolean := False)
9591 procedure Note_Redundant_Use (Clause : Node_Id);
9592 -- Mark the name in a use clause as redundant if the corresponding
9593 -- entity is already use-visible. Emit a warning if the use clause comes
9594 -- from source and the proper warnings are enabled.
9596 ------------------------
9597 -- Note_Redundant_Use --
9598 ------------------------
9600 procedure Note_Redundant_Use (Clause : Node_Id) is
9601 Decl : constant Node_Id := Parent (Clause);
9602 Pack_Name : constant Entity_Id := Entity (Clause);
9604 Cur_Use : Node_Id := Current_Use_Clause (Pack_Name);
9605 Prev_Use : Node_Id := Empty;
9606 Redundant : Node_Id := Empty;
9607 -- The Use_Clause which is actually redundant. In the simplest case
9608 -- it is Pack itself, but when we compile a body we install its
9609 -- context before that of its spec, in which case it is the
9610 -- use_clause in the spec that will appear to be redundant, and we
9611 -- want the warning to be placed on the body. Similar complications
9612 -- appear when the redundancy is between a child unit and one of its
9616 -- Could be renamed...
9618 if No (Cur_Use) then
9619 Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name));
9622 Set_Redundant_Use (Clause, True);
9624 -- Do not check for redundant use if clause is generated, or in an
9625 -- instance, or in a predefined unit to avoid misleading warnings
9626 -- that may occur as part of a rtsfind load.
9628 if not Comes_From_Source (Clause)
9630 or else not Warn_On_Redundant_Constructs
9631 or else Is_Predefined_Unit (Current_Sem_Unit)
9636 if not Is_Compilation_Unit (Current_Scope) then
9638 -- If the use_clause is in an inner scope, it is made redundant by
9639 -- some clause in the current context, with one exception: If we
9640 -- are compiling a nested package body, and the use_clause comes
9641 -- from then corresponding spec, the clause is not necessarily
9642 -- fully redundant, so we should not warn. If a warning was
9643 -- warranted, it would have been given when the spec was
9646 if Nkind (Parent (Decl)) = N_Package_Specification then
9648 Package_Spec_Entity : constant Entity_Id :=
9649 Defining_Unit_Name (Parent (Decl));
9651 if In_Package_Body (Package_Spec_Entity) then
9657 Redundant := Clause;
9658 Prev_Use := Cur_Use;
9660 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9662 Cur_Unit : constant Unit_Number_Type :=
9663 Get_Source_Unit (Cur_Use);
9664 New_Unit : constant Unit_Number_Type :=
9665 Get_Source_Unit (Clause);
9670 if Cur_Unit = New_Unit then
9672 -- Redundant clause in same body
9674 Redundant := Clause;
9675 Prev_Use := Cur_Use;
9677 elsif Cur_Unit = Current_Sem_Unit then
9679 -- If the new clause is not in the current unit it has been
9680 -- analyzed first, and it makes the other one redundant.
9681 -- However, if the new clause appears in a subunit, Cur_Unit
9682 -- is still the parent, and in that case the redundant one
9683 -- is the one appearing in the subunit.
9685 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
9686 Redundant := Clause;
9687 Prev_Use := Cur_Use;
9689 -- Most common case: redundant clause in body, original
9690 -- clause in spec. Current scope is spec entity.
9692 elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then
9693 Redundant := Cur_Use;
9697 -- The new clause may appear in an unrelated unit, when
9698 -- the parents of a generic are being installed prior to
9699 -- instantiation. In this case there must be no warning.
9700 -- We detect this case by checking whether the current
9701 -- top of the stack is related to the current
9704 Scop := Current_Scope;
9705 while Present (Scop)
9706 and then Scop /= Standard_Standard
9708 if Is_Compilation_Unit (Scop)
9709 and then not Is_Child_Unit (Scop)
9713 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
9717 Scop := Scope (Scop);
9720 Redundant := Cur_Use;
9724 elsif New_Unit = Current_Sem_Unit then
9725 Redundant := Clause;
9726 Prev_Use := Cur_Use;
9729 -- Neither is the current unit, so they appear in parent or
9730 -- sibling units. Warning will be emitted elsewhere.
9736 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
9737 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
9739 -- Use_clause is in child unit of current unit, and the child unit
9740 -- appears in the context of the body of the parent, so it has
9741 -- been installed first, even though it is the redundant one.
9742 -- Depending on their placement in the context, the visible or the
9743 -- private parts of the two units, either might appear as
9744 -- redundant, but the message has to be on the current unit.
9746 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
9747 Redundant := Cur_Use;
9750 Redundant := Clause;
9751 Prev_Use := Cur_Use;
9754 -- If the new use clause appears in the private part of a parent
9755 -- unit it may appear to be redundant w.r.t. a use clause in a
9756 -- child unit, but the previous use clause was needed in the
9757 -- visible part of the child, and no warning should be emitted.
9759 if Nkind (Parent (Decl)) = N_Package_Specification
9760 and then List_Containing (Decl) =
9761 Private_Declarations (Parent (Decl))
9764 Par : constant Entity_Id :=
9765 Defining_Entity (Parent (Decl));
9766 Spec : constant Node_Id :=
9767 Specification (Unit (Cunit (Current_Sem_Unit)));
9768 Cur_List : constant List_Id := List_Containing (Cur_Use);
9771 if Is_Compilation_Unit (Par)
9772 and then Par /= Cunit_Entity (Current_Sem_Unit)
9774 if Cur_List = Context_Items (Cunit (Current_Sem_Unit))
9775 or else Cur_List = Visible_Declarations (Spec)
9783 -- Finally, if the current use clause is in the context then the
9784 -- clause is redundant when it is nested within the unit.
9786 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
9787 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
9788 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
9790 Redundant := Clause;
9791 Prev_Use := Cur_Use;
9794 if Present (Redundant) and then Parent (Redundant) /= Prev_Use then
9796 -- Make sure we are looking at most-descendant use_package_clause
9797 -- by traversing the chain with Find_First_Use and then verifying
9798 -- there is no scope manipulation via Most_Descendant_Use_Clause.
9800 if Nkind (Prev_Use) = N_Use_Package_Clause
9802 (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit
9803 or else Most_Descendant_Use_Clause
9804 (Prev_Use, Find_First_Use (Prev_Use)) /= Prev_Use)
9806 Prev_Use := Find_First_Use (Prev_Use);
9809 Error_Msg_Sloc := Sloc (Prev_Use);
9810 Error_Msg_NE -- CODEFIX
9811 ("& is already use-visible through previous use_clause #?r?",
9812 Redundant, Pack_Name);
9814 end Note_Redundant_Use;
9818 Current_Instance : Entity_Id := Empty;
9822 Private_With_OK : Boolean := False;
9825 -- Start of processing for Use_One_Package
9828 -- Use_One_Package may have been called recursively to handle an
9829 -- implicit use for a auxiliary system package, so set P accordingly
9830 -- and skip redundancy checks.
9832 if No (Pack_Name) and then Present_System_Aux (N) then
9835 -- Check for redundant use_package_clauses
9838 -- Ignore cases where we are dealing with a non user defined package
9839 -- like Standard_Standard or something other than a valid package.
9841 if not Is_Entity_Name (Pack_Name)
9842 or else No (Entity (Pack_Name))
9843 or else Ekind (Entity (Pack_Name)) /= E_Package
9848 -- When a renaming exists we must check it for redundancy. The
9849 -- original package would have already been seen at this point.
9851 if Present (Renamed_Object (Entity (Pack_Name))) then
9852 P := Renamed_Object (Entity (Pack_Name));
9854 P := Entity (Pack_Name);
9857 -- Check for redundant clauses then set the current use clause for
9858 -- P if were are not "forcing" an installation from a scope
9859 -- reinstallation that is done throughout analysis for various
9863 Note_Redundant_Use (Pack_Name);
9866 Set_Current_Use_Clause (P, N);
9871 -- Warn about detected redundant clauses
9874 and then In_Open_Scopes (P)
9875 and then not Is_Hidden_Open_Scope (P)
9877 if Warn_On_Redundant_Constructs and then P = Current_Scope then
9878 Error_Msg_NE -- CODEFIX
9879 ("& is already use-visible within itself?r?",
9886 -- Set P back to the non-renamed package so that visiblilty of the
9887 -- entities within the package can be properly set below.
9889 P := Entity (Pack_Name);
9893 Set_Current_Use_Clause (P, N);
9895 -- Ada 2005 (AI-50217): Check restriction
9897 if From_Limited_With (P) then
9898 Error_Msg_N ("limited withed package cannot appear in use clause", N);
9901 -- Find enclosing instance, if any
9904 Current_Instance := Current_Scope;
9905 while not Is_Generic_Instance (Current_Instance) loop
9906 Current_Instance := Scope (Current_Instance);
9909 if No (Hidden_By_Use_Clause (N)) then
9910 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
9914 -- If unit is a package renaming, indicate that the renamed package is
9915 -- also in use (the flags on both entities must remain consistent, and a
9916 -- subsequent use of either of them should be recognized as redundant).
9918 if Present (Renamed_Object (P)) then
9919 Set_In_Use (Renamed_Object (P));
9920 Set_Current_Use_Clause (Renamed_Object (P), N);
9921 Real_P := Renamed_Object (P);
9926 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
9927 -- found in the private part of a package specification
9929 if In_Private_Part (Current_Scope)
9930 and then Has_Private_With (P)
9931 and then Is_Child_Unit (Current_Scope)
9932 and then Is_Child_Unit (P)
9933 and then Is_Ancestor_Package (Scope (Current_Scope), P)
9935 Private_With_OK := True;
9938 -- Loop through entities in one package making them potentially
9941 Id := First_Entity (P);
9943 and then (Id /= First_Private_Entity (P)
9944 or else Private_With_OK) -- Ada 2005 (AI-262)
9946 Prev := Current_Entity (Id);
9947 while Present (Prev) loop
9948 if Is_Immediately_Visible (Prev)
9949 and then (not Is_Overloadable (Prev)
9950 or else not Is_Overloadable (Id)
9951 or else (Type_Conformant (Id, Prev)))
9953 if No (Current_Instance) then
9955 -- Potentially use-visible entity remains hidden
9957 goto Next_Usable_Entity;
9959 -- A use clause within an instance hides outer global entities,
9960 -- which are not used to resolve local entities in the
9961 -- instance. Note that the predefined entities in Standard
9962 -- could not have been hidden in the generic by a use clause,
9963 -- and therefore remain visible. Other compilation units whose
9964 -- entities appear in Standard must be hidden in an instance.
9966 -- To determine whether an entity is external to the instance
9967 -- we compare the scope depth of its scope with that of the
9968 -- current instance. However, a generic actual of a subprogram
9969 -- instance is declared in the wrapper package but will not be
9970 -- hidden by a use-visible entity. similarly, an entity that is
9971 -- declared in an enclosing instance will not be hidden by an
9972 -- an entity declared in a generic actual, which can only have
9973 -- been use-visible in the generic and will not have hidden the
9974 -- entity in the generic parent.
9976 -- If Id is called Standard, the predefined package with the
9977 -- same name is in the homonym chain. It has to be ignored
9978 -- because it has no defined scope (being the only entity in
9979 -- the system with this mandated behavior).
9981 elsif not Is_Hidden (Id)
9982 and then Present (Scope (Prev))
9983 and then not Is_Wrapper_Package (Scope (Prev))
9984 and then Scope_Depth (Scope (Prev)) <
9985 Scope_Depth (Current_Instance)
9986 and then (Scope (Prev) /= Standard_Standard
9987 or else Sloc (Prev) > Standard_Location)
9989 if In_Open_Scopes (Scope (Prev))
9990 and then Is_Generic_Instance (Scope (Prev))
9991 and then Present (Associated_Formal_Package (P))
9996 Set_Is_Potentially_Use_Visible (Id);
9997 Set_Is_Immediately_Visible (Prev, False);
9998 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10002 -- A user-defined operator is not use-visible if the predefined
10003 -- operator for the type is immediately visible, which is the case
10004 -- if the type of the operand is in an open scope. This does not
10005 -- apply to user-defined operators that have operands of different
10006 -- types, because the predefined mixed mode operations (multiply
10007 -- and divide) apply to universal types and do not hide anything.
10009 elsif Ekind (Prev) = E_Operator
10010 and then Operator_Matches_Spec (Prev, Id)
10011 and then In_Open_Scopes
10012 (Scope (Base_Type (Etype (First_Formal (Id)))))
10013 and then (No (Next_Formal (First_Formal (Id)))
10014 or else Etype (First_Formal (Id)) =
10015 Etype (Next_Formal (First_Formal (Id)))
10016 or else Chars (Prev) = Name_Op_Expon)
10018 goto Next_Usable_Entity;
10020 -- In an instance, two homonyms may become use_visible through the
10021 -- actuals of distinct formal packages. In the generic, only the
10022 -- current one would have been visible, so make the other one
10023 -- not use_visible.
10025 -- In certain pathological cases it is possible that unrelated
10026 -- homonyms from distinct formal packages may exist in an
10027 -- uninstalled scope. We must test for that here.
10029 elsif Present (Current_Instance)
10030 and then Is_Potentially_Use_Visible (Prev)
10031 and then not Is_Overloadable (Prev)
10032 and then Scope (Id) /= Scope (Prev)
10033 and then Used_As_Generic_Actual (Scope (Prev))
10034 and then Used_As_Generic_Actual (Scope (Id))
10035 and then Is_List_Member (Scope (Prev))
10036 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
10037 Current_Use_Clause (Scope (Id)))
10039 Set_Is_Potentially_Use_Visible (Prev, False);
10040 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10043 Prev := Homonym (Prev);
10046 -- On exit, we know entity is not hidden, unless it is private
10048 if not Is_Hidden (Id)
10049 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
10051 Set_Is_Potentially_Use_Visible (Id);
10053 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
10054 Set_Is_Potentially_Use_Visible (Full_View (Id));
10058 <<Next_Usable_Entity>>
10062 -- Child units are also made use-visible by a use clause, but they may
10063 -- appear after all visible declarations in the parent entity list.
10065 while Present (Id) loop
10066 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
10067 Set_Is_Potentially_Use_Visible (Id);
10073 if Chars (Real_P) = Name_System
10074 and then Scope (Real_P) = Standard_Standard
10075 and then Present_System_Aux (N)
10077 Use_One_Package (N);
10079 end Use_One_Package;
10085 procedure Use_One_Type
10087 Installed : Boolean := False;
10088 Force : Boolean := False)
10090 function Spec_Reloaded_For_Body return Boolean;
10091 -- Determine whether the compilation unit is a package body and the use
10092 -- type clause is in the spec of the same package. Even though the spec
10093 -- was analyzed first, its context is reloaded when analysing the body.
10095 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
10096 -- AI05-150: if the use_type_clause carries the "all" qualifier,
10097 -- class-wide operations of ancestor types are use-visible if the
10098 -- ancestor type is visible.
10100 ----------------------------
10101 -- Spec_Reloaded_For_Body --
10102 ----------------------------
10104 function Spec_Reloaded_For_Body return Boolean is
10106 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
10108 Spec : constant Node_Id :=
10109 Parent (List_Containing (Parent (Id)));
10112 -- Check whether type is declared in a package specification,
10113 -- and current unit is the corresponding package body. The
10114 -- use clauses themselves may be within a nested package.
10117 Nkind (Spec) = N_Package_Specification
10118 and then In_Same_Source_Unit
10119 (Corresponding_Body (Parent (Spec)),
10120 Cunit_Entity (Current_Sem_Unit));
10125 end Spec_Reloaded_For_Body;
10127 -------------------------------
10128 -- Use_Class_Wide_Operations --
10129 -------------------------------
10131 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
10132 function Is_Class_Wide_Operation_Of
10134 T : Entity_Id) return Boolean;
10135 -- Determine whether a subprogram has a class-wide parameter or
10136 -- result that is T'Class.
10138 ---------------------------------
10139 -- Is_Class_Wide_Operation_Of --
10140 ---------------------------------
10142 function Is_Class_Wide_Operation_Of
10144 T : Entity_Id) return Boolean
10146 Formal : Entity_Id;
10149 Formal := First_Formal (Op);
10150 while Present (Formal) loop
10151 if Etype (Formal) = Class_Wide_Type (T) then
10155 Next_Formal (Formal);
10158 if Etype (Op) = Class_Wide_Type (T) then
10163 end Is_Class_Wide_Operation_Of;
10170 -- Start of processing for Use_Class_Wide_Operations
10173 Scop := Scope (Typ);
10174 if not Is_Hidden (Scop) then
10175 Ent := First_Entity (Scop);
10176 while Present (Ent) loop
10177 if Is_Overloadable (Ent)
10178 and then Is_Class_Wide_Operation_Of (Ent, Typ)
10179 and then not Is_Potentially_Use_Visible (Ent)
10181 Set_Is_Potentially_Use_Visible (Ent);
10182 Append_Elmt (Ent, Used_Operations (Parent (Id)));
10189 if Is_Derived_Type (Typ) then
10190 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
10192 end Use_Class_Wide_Operations;
10197 Is_Known_Used : Boolean;
10198 Op_List : Elist_Id;
10201 -- Start of processing for Use_One_Type
10204 if Entity (Id) = Any_Type then
10208 -- It is the type determined by the subtype mark (8.4(8)) whose
10209 -- operations become potentially use-visible.
10211 T := Base_Type (Entity (Id));
10213 -- Either the type itself is used, the package where it is declared is
10214 -- in use or the entity is declared in the current package, thus
10219 and then ((Present (Current_Use_Clause (T))
10220 and then All_Present (Current_Use_Clause (T)))
10221 or else not All_Present (Parent (Id))))
10222 or else In_Use (Scope (T))
10223 or else Scope (T) = Current_Scope;
10225 Set_Redundant_Use (Id,
10226 Is_Known_Used or else Is_Potentially_Use_Visible (T));
10228 if Ekind (T) = E_Incomplete_Type then
10229 Error_Msg_N ("premature usage of incomplete type", Id);
10231 elsif In_Open_Scopes (Scope (T)) then
10234 -- A limited view cannot appear in a use_type_clause. However, an access
10235 -- type whose designated type is limited has the flag but is not itself
10236 -- a limited view unless we only have a limited view of its enclosing
10239 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
10241 ("incomplete type from limited view cannot appear in use clause",
10244 -- If the use clause is redundant, Used_Operations will usually be
10245 -- empty, but we need to set it to empty here in one case: If we are
10246 -- instantiating a generic library unit, then we install the ancestors
10247 -- of that unit in the scope stack, which involves reprocessing use
10248 -- clauses in those ancestors. Such a use clause will typically have a
10249 -- nonempty Used_Operations unless it was redundant in the generic unit,
10250 -- even if it is redundant at the place of the instantiation.
10252 elsif Redundant_Use (Id) then
10254 -- We must avoid incorrectly setting the Current_Use_Clause when we
10255 -- are working with a redundant clause that has already been linked
10256 -- in the Prev_Use_Clause chain, otherwise the chain will break.
10258 if Present (Current_Use_Clause (T))
10259 and then Present (Prev_Use_Clause (Current_Use_Clause (T)))
10260 and then Parent (Id) = Prev_Use_Clause (Current_Use_Clause (T))
10264 Set_Current_Use_Clause (T, Parent (Id));
10267 Set_Used_Operations (Parent (Id), New_Elmt_List);
10269 -- If the subtype mark designates a subtype in a different package,
10270 -- we have to check that the parent type is visible, otherwise the
10271 -- use_type_clause is a no-op. Not clear how to do that???
10274 Set_Current_Use_Clause (T, Parent (Id));
10277 -- If T is tagged, primitive operators on class-wide operands are
10278 -- also deemed available. Note that this is really necessary only
10279 -- in semantics-only mode, because the primitive operators are not
10280 -- fully constructed in this mode, but we do it in all modes for the
10281 -- sake of uniformity, as this should not matter in practice.
10283 if Is_Tagged_Type (T) then
10284 Set_In_Use (Class_Wide_Type (T));
10287 -- Iterate over primitive operations of the type. If an operation is
10288 -- already use_visible, it is the result of a previous use_clause,
10289 -- and already appears on the corresponding entity chain. If the
10290 -- clause is being reinstalled, operations are already use-visible.
10296 Op_List := Collect_Primitive_Operations (T);
10297 Elmt := First_Elmt (Op_List);
10298 while Present (Elmt) loop
10299 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
10300 or else Chars (Node (Elmt)) in Any_Operator_Name)
10301 and then not Is_Hidden (Node (Elmt))
10302 and then not Is_Potentially_Use_Visible (Node (Elmt))
10304 Set_Is_Potentially_Use_Visible (Node (Elmt));
10305 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10307 elsif Ada_Version >= Ada_2012
10308 and then All_Present (Parent (Id))
10309 and then not Is_Hidden (Node (Elmt))
10310 and then not Is_Potentially_Use_Visible (Node (Elmt))
10312 Set_Is_Potentially_Use_Visible (Node (Elmt));
10313 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10320 if Ada_Version >= Ada_2012
10321 and then All_Present (Parent (Id))
10322 and then Is_Tagged_Type (T)
10324 Use_Class_Wide_Operations (T);
10328 -- If warning on redundant constructs, check for unnecessary WITH
10331 and then Warn_On_Redundant_Constructs
10332 and then Is_Known_Used
10334 -- with P; with P; use P;
10335 -- package P is package X is package body X is
10336 -- type T ... use P.T;
10338 -- The compilation unit is the body of X. GNAT first compiles the
10339 -- spec of X, then proceeds to the body. At that point P is marked
10340 -- as use visible. The analysis then reinstalls the spec along with
10341 -- its context. The use clause P.T is now recognized as redundant,
10342 -- but in the wrong context. Do not emit a warning in such cases.
10343 -- Do not emit a warning either if we are in an instance, there is
10344 -- no redundancy between an outer use_clause and one that appears
10345 -- within the generic.
10347 and then not Spec_Reloaded_For_Body
10348 and then not In_Instance
10349 and then not In_Inlined_Body
10351 -- The type already has a use clause
10355 -- Case where we know the current use clause for the type
10357 if Present (Current_Use_Clause (T)) then
10358 Use_Clause_Known : declare
10359 Clause1 : constant Node_Id :=
10360 Find_First_Use (Current_Use_Clause (T));
10361 Clause2 : constant Node_Id := Parent (Id);
10368 -- Start of processing for Use_Clause_Known
10371 -- If both current use_type_clause and the use_type_clause
10372 -- for the type are at the compilation unit level, one of
10373 -- the units must be an ancestor of the other, and the
10374 -- warning belongs on the descendant.
10376 if Nkind (Parent (Clause1)) = N_Compilation_Unit
10378 Nkind (Parent (Clause2)) = N_Compilation_Unit
10380 -- If the unit is a subprogram body that acts as spec,
10381 -- the context clause is shared with the constructed
10382 -- subprogram spec. Clearly there is no redundancy.
10384 if Clause1 = Clause2 then
10388 Unit1 := Unit (Parent (Clause1));
10389 Unit2 := Unit (Parent (Clause2));
10391 -- If both clauses are on same unit, or one is the body
10392 -- of the other, or one of them is in a subunit, report
10393 -- redundancy on the later one.
10395 if Unit1 = Unit2 or else Nkind (Unit1) = N_Subunit then
10396 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10397 Error_Msg_NE -- CODEFIX
10398 ("& is already use-visible through previous "
10399 & "use_type_clause #??", Clause1, T);
10402 elsif Nkind (Unit2) in N_Package_Body | N_Subprogram_Body
10403 and then Nkind (Unit1) /= Nkind (Unit2)
10404 and then Nkind (Unit1) /= N_Subunit
10406 Error_Msg_Sloc := Sloc (Clause1);
10407 Error_Msg_NE -- CODEFIX
10408 ("& is already use-visible through previous "
10409 & "use_type_clause #??", Current_Use_Clause (T), T);
10413 -- There is a redundant use_type_clause in a child unit.
10414 -- Determine which of the units is more deeply nested.
10415 -- If a unit is a package instance, retrieve the entity
10416 -- and its scope from the instance spec.
10418 Ent1 := Entity_Of_Unit (Unit1);
10419 Ent2 := Entity_Of_Unit (Unit2);
10421 if Scope (Ent2) = Standard_Standard then
10422 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10425 elsif Scope (Ent1) = Standard_Standard then
10426 Error_Msg_Sloc := Sloc (Id);
10429 -- If both units are child units, we determine which one
10430 -- is the descendant by the scope distance to the
10431 -- ultimate parent unit.
10439 S1 := Scope (Ent1);
10440 S2 := Scope (Ent2);
10442 and then Present (S2)
10443 and then S1 /= Standard_Standard
10444 and then S2 /= Standard_Standard
10450 if S1 = Standard_Standard then
10451 Error_Msg_Sloc := Sloc (Id);
10454 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10460 if Parent (Id) /= Err_No then
10461 if Most_Descendant_Use_Clause
10462 (Err_No, Parent (Id)) = Parent (Id)
10464 Error_Msg_Sloc := Sloc (Err_No);
10465 Err_No := Parent (Id);
10468 Error_Msg_NE -- CODEFIX
10469 ("& is already use-visible through previous "
10470 & "use_type_clause #??", Err_No, Id);
10473 -- Case where current use_type_clause and use_type_clause
10474 -- for the type are not both at the compilation unit level.
10475 -- In this case we don't have location information.
10478 Error_Msg_NE -- CODEFIX
10479 ("& is already use-visible through previous "
10480 & "use_type_clause??", Id, T);
10482 end Use_Clause_Known;
10484 -- Here if Current_Use_Clause is not set for T, another case where
10485 -- we do not have the location information available.
10488 Error_Msg_NE -- CODEFIX
10489 ("& is already use-visible through previous "
10490 & "use_type_clause??", Id, T);
10493 -- The package where T is declared is already used
10495 elsif In_Use (Scope (T)) then
10496 -- Due to expansion of contracts we could be attempting to issue
10497 -- a spurious warning - so verify there is a previous use clause.
10499 if Current_Use_Clause (Scope (T)) /=
10500 Find_First_Use (Current_Use_Clause (Scope (T)))
10503 Sloc (Find_First_Use (Current_Use_Clause (Scope (T))));
10504 Error_Msg_NE -- CODEFIX
10505 ("& is already use-visible through package use clause #??",
10509 -- The current scope is the package where T is declared
10512 Error_Msg_Node_2 := Scope (T);
10513 Error_Msg_NE -- CODEFIX
10514 ("& is already use-visible inside package &??", Id, T);
10523 procedure Write_Info is
10524 Id : Entity_Id := First_Entity (Current_Scope);
10527 -- No point in dumping standard entities
10529 if Current_Scope = Standard_Standard then
10533 Write_Str ("========================================================");
10535 Write_Str (" Defined Entities in ");
10536 Write_Name (Chars (Current_Scope));
10538 Write_Str ("========================================================");
10542 Write_Str ("-- none --");
10546 while Present (Id) loop
10547 Write_Entity_Info (Id, " ");
10552 if Scope (Current_Scope) = Standard_Standard then
10554 -- Print information on the current unit itself
10556 Write_Entity_Info (Current_Scope, " ");
10569 for J in reverse 1 .. Scope_Stack.Last loop
10570 S := Scope_Stack.Table (J).Entity;
10571 Write_Int (Int (S));
10572 Write_Str (" === ");
10573 Write_Name (Chars (S));
10582 procedure we (S : Entity_Id) is
10585 E := First_Entity (S);
10586 while Present (E) loop
10587 Write_Int (Int (E));
10588 Write_Str (" === ");
10589 Write_Name (Chars (E));