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 (N, Nam : Node_Id);
430 -- N is a use_package clause and Nam the package name, or N is a use_type
431 -- clause and Nam is the prefix of the type name. In either case, verify
432 -- that the package is visible at that point in the context: either it
433 -- appears in a previous with_clause, or because it is a fully qualified
434 -- name and the root ancestor appears in a previous with_clause.
436 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
437 -- Verify that the entity in a renaming declaration that is a library unit
438 -- is itself a library unit and not a nested unit or subunit. Also check
439 -- that if the renaming is a child unit of a generic parent, then the
440 -- renamed unit must also be a child unit of that parent. Finally, verify
441 -- that a renamed generic unit is not an implicit child declared within
442 -- an instance of the parent.
444 procedure Chain_Use_Clause (N : Node_Id);
445 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
446 -- the proper scope table entry. This is usually the current scope, but it
447 -- will be an inner scope when installing the use clauses of the private
448 -- declarations of a parent unit prior to compiling the private part of a
449 -- child unit. This chain is traversed when installing/removing use clauses
450 -- when compiling a subunit or instantiating a generic body on the fly,
451 -- when it is necessary to save and restore full environments.
453 function Enclosing_Instance return Entity_Id;
454 -- In an instance nested within another one, several semantic checks are
455 -- unnecessary because the legality of the nested instance has been checked
456 -- in the enclosing generic unit. This applies in particular to legality
457 -- checks on actuals for formal subprograms of the inner instance, which
458 -- are checked as subprogram renamings, and may be complicated by confusion
459 -- in private/full views. This function returns the instance enclosing the
460 -- current one if there is such, else it returns Empty.
462 -- If the renaming determines the entity for the default of a formal
463 -- subprogram nested within another instance, choose the innermost
464 -- candidate. This is because if the formal has a box, and we are within
465 -- an enclosing instance where some candidate interpretations are local
466 -- to this enclosing instance, we know that the default was properly
467 -- resolved when analyzing the generic, so we prefer the local
468 -- candidates to those that are external. This is not always the case
469 -- but is a reasonable heuristic on the use of nested generics. The
470 -- proper solution requires a full renaming model.
472 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
473 -- Return the appropriate entity for determining which unit has a deeper
474 -- scope: the defining entity for U, unless U is a package instance, in
475 -- which case we retrieve the entity of the instance spec.
477 procedure Find_Expanded_Name (N : Node_Id);
478 -- The input is a selected component known to be an expanded name. Verify
479 -- legality of selector given the scope denoted by prefix, and change node
480 -- N into a expanded name with a properly set Entity field.
482 function Find_First_Use (Use_Clause : Node_Id) return Node_Id;
483 -- Find the most previous use clause (that is, the first one to appear in
484 -- the source) by traversing the previous clause chain that exists in both
485 -- N_Use_Package_Clause nodes and N_Use_Type_Clause nodes.
487 function Find_Renamed_Entity
491 Is_Actual : Boolean := False) return Entity_Id;
492 -- Find the renamed entity that corresponds to the given parameter profile
493 -- in a subprogram renaming declaration. The renamed entity may be an
494 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
495 -- indicates that the renaming is the one generated for an actual subpro-
496 -- gram in an instance, for which special visibility checks apply.
498 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
499 -- Find a type derived from Character or Wide_Character in the prefix of N.
500 -- Used to resolved qualified names whose selector is a character literal.
502 function Has_Private_With (E : Entity_Id) return Boolean;
503 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
504 -- private with on E.
506 function Has_Components (Typ : Entity_Id) return Boolean;
507 -- Determine if given type has components, i.e. is either a record type or
508 -- type or a type that has discriminants.
510 function Has_Implicit_Operator (N : Node_Id) return Boolean;
511 -- N is an expanded name whose selector is an operator name (e.g. P."+").
512 -- declarative part contains an implicit declaration of an operator if it
513 -- has a declaration of a type to which one of the predefined operators
514 -- apply. The existence of this routine is an implementation artifact. A
515 -- more straightforward but more space-consuming choice would be to make
516 -- all inherited operators explicit in the symbol table.
518 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
519 -- A subprogram defined by a renaming declaration inherits the parameter
520 -- profile of the renamed entity. The subtypes given in the subprogram
521 -- specification are discarded and replaced with those of the renamed
522 -- subprogram, which are then used to recheck the default values.
524 function Most_Descendant_Use_Clause
525 (Clause1 : Entity_Id;
526 Clause2 : Entity_Id) return Entity_Id;
527 -- Determine which use clause parameter is the most descendant in terms of
530 procedure Premature_Usage (N : Node_Id);
531 -- Diagnose usage of an entity before it is visible
533 procedure Use_One_Package
535 Pack_Name : Entity_Id := Empty;
536 Force : Boolean := False);
537 -- Make visible entities declared in package P potentially use-visible
538 -- in the current context. Also used in the analysis of subunits, when
539 -- re-installing use clauses of parent units. N is the use_clause that
540 -- names P (and possibly other packages).
542 procedure Use_One_Type
544 Installed : Boolean := False;
545 Force : Boolean := False);
546 -- Id is the subtype mark from a use_type_clause. This procedure makes
547 -- the primitive operators of the type potentially use-visible. The
548 -- boolean flag Installed indicates that the clause is being reinstalled
549 -- after previous analysis, and primitive operations are already chained
550 -- on the Used_Operations list of the clause.
552 procedure Write_Info;
553 -- Write debugging information on entities declared in current scope
555 --------------------------------
556 -- Analyze_Exception_Renaming --
557 --------------------------------
559 -- The language only allows a single identifier, but the tree holds an
560 -- identifier list. The parser has already issued an error message if
561 -- there is more than one element in the list.
563 procedure Analyze_Exception_Renaming (N : Node_Id) is
564 Id : constant Entity_Id := Defining_Entity (N);
565 Nam : constant Node_Id := Name (N);
571 Mutate_Ekind (Id, E_Exception);
572 Set_Etype (Id, Standard_Exception_Type);
573 Set_Is_Pure (Id, Is_Pure (Current_Scope));
575 if Is_Entity_Name (Nam)
576 and then Present (Entity (Nam))
577 and then Ekind (Entity (Nam)) = E_Exception
579 if Present (Renamed_Object (Entity (Nam))) then
580 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
582 Set_Renamed_Object (Id, Entity (Nam));
585 -- The exception renaming declaration may become Ghost if it renames
588 Mark_Ghost_Renaming (N, Entity (Nam));
590 Error_Msg_N ("invalid exception name in renaming", Nam);
593 -- Implementation-defined aspect specifications can appear in a renaming
594 -- declaration, but not language-defined ones. The call to procedure
595 -- Analyze_Aspect_Specifications will take care of this error check.
597 if Has_Aspects (N) then
598 Analyze_Aspect_Specifications (N, Id);
600 end Analyze_Exception_Renaming;
602 ---------------------------
603 -- Analyze_Expanded_Name --
604 ---------------------------
606 procedure Analyze_Expanded_Name (N : Node_Id) is
608 -- If the entity pointer is already set, this is an internal node, or a
609 -- node that is analyzed more than once, after a tree modification. In
610 -- such a case there is no resolution to perform, just set the type. In
611 -- either case, start by analyzing the prefix.
613 Analyze (Prefix (N));
615 if Present (Entity (N)) then
616 if Is_Type (Entity (N)) then
617 Set_Etype (N, Entity (N));
619 Set_Etype (N, Etype (Entity (N)));
623 Find_Expanded_Name (N);
626 -- In either case, propagate dimension of entity to expanded name
628 Analyze_Dimension (N);
629 end Analyze_Expanded_Name;
631 ---------------------------------------
632 -- Analyze_Generic_Function_Renaming --
633 ---------------------------------------
635 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
637 Analyze_Generic_Renaming (N, E_Generic_Function);
638 end Analyze_Generic_Function_Renaming;
640 --------------------------------------
641 -- Analyze_Generic_Package_Renaming --
642 --------------------------------------
644 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
646 -- Test for the Text_IO special unit case here, since we may be renaming
647 -- one of the subpackages of Text_IO, then join common routine.
649 Check_Text_IO_Special_Unit (Name (N));
651 Analyze_Generic_Renaming (N, E_Generic_Package);
652 end Analyze_Generic_Package_Renaming;
654 ----------------------------------------
655 -- Analyze_Generic_Procedure_Renaming --
656 ----------------------------------------
658 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
660 Analyze_Generic_Renaming (N, E_Generic_Procedure);
661 end Analyze_Generic_Procedure_Renaming;
663 ------------------------------
664 -- Analyze_Generic_Renaming --
665 ------------------------------
667 procedure Analyze_Generic_Renaming
671 New_P : constant Entity_Id := Defining_Entity (N);
672 Inst : Boolean := False;
676 if Name (N) = Error then
680 Generate_Definition (New_P);
682 if Current_Scope /= Standard_Standard then
683 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
686 if Nkind (Name (N)) = N_Selected_Component then
687 Check_Generic_Child_Unit (Name (N), Inst);
692 if not Is_Entity_Name (Name (N)) then
693 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
696 Old_P := Entity (Name (N));
700 Mutate_Ekind (New_P, K);
702 if Etype (Old_P) = Any_Type then
705 elsif Ekind (Old_P) /= K then
706 Error_Msg_N ("invalid generic unit name", Name (N));
709 if Present (Renamed_Object (Old_P)) then
710 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
712 Set_Renamed_Object (New_P, Old_P);
715 -- The generic renaming declaration may become Ghost if it renames a
718 Mark_Ghost_Renaming (N, Old_P);
720 Set_Is_Pure (New_P, Is_Pure (Old_P));
721 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
723 Set_Etype (New_P, Etype (Old_P));
724 Set_Has_Completion (New_P);
726 if In_Open_Scopes (Old_P) then
727 Error_Msg_N ("within its scope, generic denotes its instance", N);
730 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
731 -- renamings and subsequent instantiations of Unchecked_Conversion.
733 if Is_Generic_Subprogram (Old_P) then
734 Set_Is_Intrinsic_Subprogram
735 (New_P, Is_Intrinsic_Subprogram (Old_P));
738 Check_Library_Unit_Renaming (N, Old_P);
741 -- Implementation-defined aspect specifications can appear in a renaming
742 -- declaration, but not language-defined ones. The call to procedure
743 -- Analyze_Aspect_Specifications will take care of this error check.
745 if Has_Aspects (N) then
746 Analyze_Aspect_Specifications (N, New_P);
748 end Analyze_Generic_Renaming;
750 -----------------------------
751 -- Analyze_Object_Renaming --
752 -----------------------------
754 procedure Analyze_Object_Renaming (N : Node_Id) is
755 Id : constant Entity_Id := Defining_Identifier (N);
756 Loc : constant Source_Ptr := Sloc (N);
757 Nam : constant Node_Id := Name (N);
758 Is_Object_Ref : Boolean;
764 procedure Check_Constrained_Object;
765 -- If the nominal type is unconstrained but the renamed object is
766 -- constrained, as can happen with renaming an explicit dereference or
767 -- a function return, build a constrained subtype from the object. If
768 -- the renaming is for a formal in an accept statement, the analysis
769 -- has already established its actual subtype. This is only relevant
770 -- if the renamed object is an explicit dereference.
772 function Get_Object_Name (Nod : Node_Id) return Node_Id;
773 -- Obtain the name of the object from node Nod which is being renamed by
774 -- the object renaming declaration N.
776 function Find_Raise_Node (N : Node_Id) return Traverse_Result;
777 -- Process one node in search for N_Raise_xxx_Error nodes.
778 -- Return Abandon if found, OK otherwise.
780 ---------------------
781 -- Find_Raise_Node --
782 ---------------------
784 function Find_Raise_Node (N : Node_Id) return Traverse_Result is
786 if Nkind (N) in N_Raise_xxx_Error then
793 ------------------------
794 -- No_Raise_xxx_Error --
795 ------------------------
797 function No_Raise_xxx_Error is new Traverse_Func (Find_Raise_Node);
798 -- Traverse tree to look for a N_Raise_xxx_Error node and returns
799 -- Abandon if so and OK if none found.
801 ------------------------------
802 -- Check_Constrained_Object --
803 ------------------------------
805 procedure Check_Constrained_Object is
806 Typ : constant Entity_Id := Etype (Nam);
808 Loop_Scheme : Node_Id;
811 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference
812 and then Is_Composite_Type (Typ)
813 and then not Is_Constrained (Typ)
814 and then not Has_Unknown_Discriminants (Typ)
815 and then Expander_Active
817 -- If Actual_Subtype is already set, nothing to do
819 if Ekind (Id) in E_Variable | E_Constant
820 and then Present (Actual_Subtype (Id))
824 -- A renaming of an unchecked union has no actual subtype
826 elsif Is_Unchecked_Union (Typ) then
829 -- If a record is limited its size is invariant. This is the case
830 -- in particular with record types with an access discriminant
831 -- that are used in iterators. This is an optimization, but it
832 -- also prevents typing anomalies when the prefix is further
835 -- Note that we cannot just use the Is_Limited_Record flag because
836 -- it does not apply to records with limited components, for which
837 -- this syntactic flag is not set, but whose size is also fixed.
839 -- Note also that we need to build the constrained subtype for an
840 -- array in order to make the bounds explicit in most cases, but
841 -- not if the object comes from an extended return statement, as
842 -- this would create dangling references to them later on.
844 elsif Is_Limited_Type (Typ)
845 and then (not Is_Array_Type (Typ) or else Is_Return_Object (Id))
850 Subt := Make_Temporary (Loc, 'T');
851 Remove_Side_Effects (Nam);
853 Make_Subtype_Declaration (Loc,
854 Defining_Identifier => Subt,
855 Subtype_Indication =>
856 Make_Subtype_From_Expr (Nam, Typ)));
857 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
858 Set_Etype (Nam, Subt);
860 -- Suppress discriminant checks on this subtype if the original
861 -- type has defaulted discriminants and Id is a "for of" loop
864 if Has_Defaulted_Discriminants (Typ)
865 and then Nkind (Original_Node (Parent (N))) = N_Loop_Statement
867 Loop_Scheme := Iteration_Scheme (Original_Node (Parent (N)));
869 if Present (Loop_Scheme)
870 and then Present (Iterator_Specification (Loop_Scheme))
873 (Iterator_Specification (Loop_Scheme)) = Id
875 Set_Checks_May_Be_Suppressed (Subt);
876 Push_Local_Suppress_Stack_Entry
878 Check => Discriminant_Check,
883 -- Freeze subtype at once, to prevent order of elaboration
884 -- issues in the backend. The renamed object exists, so its
885 -- type is already frozen in any case.
887 Freeze_Before (N, Subt);
890 end Check_Constrained_Object;
892 ---------------------
893 -- Get_Object_Name --
894 ---------------------
896 function Get_Object_Name (Nod : Node_Id) return Node_Id is
901 while Present (Obj_Nam) loop
902 case Nkind (Obj_Nam) is
903 when N_Attribute_Reference
904 | N_Explicit_Dereference
905 | N_Indexed_Component
908 Obj_Nam := Prefix (Obj_Nam);
910 when N_Selected_Component =>
911 Obj_Nam := Selector_Name (Obj_Nam);
913 when N_Qualified_Expression | N_Type_Conversion =>
914 Obj_Nam := Expression (Obj_Nam);
924 -- Start of processing for Analyze_Object_Renaming
931 Set_Is_Pure (Id, Is_Pure (Current_Scope));
934 -- The renaming of a component that depends on a discriminant requires
935 -- an actual subtype, because in subsequent use of the object Gigi will
936 -- be unable to locate the actual bounds. This explicit step is required
937 -- when the renaming is generated in removing side effects of an
938 -- already-analyzed expression.
940 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
942 -- The object renaming declaration may become Ghost if it renames a
945 if Is_Entity_Name (Nam) then
946 Mark_Ghost_Renaming (N, Entity (Nam));
950 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
952 if Present (Dec) then
953 Insert_Action (N, Dec);
954 T := Defining_Identifier (Dec);
957 elsif Present (Subtype_Mark (N))
958 or else not Present (Access_Definition (N))
960 if Present (Subtype_Mark (N)) then
961 Find_Type (Subtype_Mark (N));
962 T := Entity (Subtype_Mark (N));
965 -- AI12-0275: Case of object renaming without a subtype_mark
970 -- Normal case of no overloading in object name
972 if not Is_Overloaded (Nam) then
974 -- Catch error cases (such as attempting to rename a procedure
975 -- or package) using the shorthand form.
978 or else Etype (Nam) = Standard_Void_Type
981 ("object name or value expected in renaming", Nam);
983 Mutate_Ekind (Id, E_Variable);
984 Set_Etype (Id, Any_Type);
992 -- Case of overloaded name, which will be illegal if there's more
993 -- than one acceptable interpretation (such as overloaded function
1005 -- More than one candidate interpretation is available
1007 -- Remove procedure calls, which syntactically cannot appear
1008 -- in this context, but which cannot be removed by type
1009 -- checking, because the context does not impose a type.
1011 Get_First_Interp (Nam, I, It);
1012 while Present (It.Typ) loop
1013 if It.Typ = Standard_Void_Type then
1017 Get_Next_Interp (I, It);
1020 Get_First_Interp (Nam, I, It);
1024 -- If there's no type present, we have an error case (such
1025 -- as overloaded procedures named in the object renaming).
1029 ("object name or value expected in renaming", Nam);
1031 Mutate_Ekind (Id, E_Variable);
1032 Set_Etype (Id, Any_Type);
1037 Get_Next_Interp (I, It);
1039 if Present (It.Typ) then
1041 It1 := Disambiguate (Nam, I1, I, Any_Type);
1043 if It1 = No_Interp then
1044 Error_Msg_N ("ambiguous name in object renaming", Nam);
1046 Error_Msg_Sloc := Sloc (It.Nam);
1047 Error_Msg_N ("\\possible interpretation#!", Nam);
1049 Error_Msg_Sloc := Sloc (Nam1);
1050 Error_Msg_N ("\\possible interpretation#!", Nam);
1056 Set_Etype (Nam, It1.Typ);
1061 if Etype (Nam) = Standard_Exception_Type then
1063 ("exception requires a subtype mark in renaming", Nam);
1068 -- The object renaming declaration may become Ghost if it renames a
1071 if Is_Entity_Name (Nam) then
1072 Mark_Ghost_Renaming (N, Entity (Nam));
1075 -- Check against AI12-0401 here before Resolve may rewrite Nam and
1076 -- potentially generate spurious warnings.
1078 -- In the case where the object_name is a qualified_expression with
1079 -- a nominal subtype T and whose expression is a name that denotes
1081 -- * if T is an elementary subtype, then:
1082 -- * Q shall be a constant other than a dereference of an access
1084 -- * the nominal subtype of Q shall be statically compatible with
1086 -- * T shall statically match the base subtype of its type if
1087 -- scalar, or the first subtype of its type if an access type.
1088 -- * if T is a composite subtype, then Q shall be known to be
1089 -- constrained or T shall statically match the first subtype of
1092 if Nkind (Nam) = N_Qualified_Expression
1093 and then Is_Object_Reference (Expression (Nam))
1095 Q := Expression (Nam);
1097 if (Is_Elementary_Type (T)
1099 not ((not Is_Variable (Q)
1100 and then Nkind (Q) /= N_Explicit_Dereference)
1101 or else Subtypes_Statically_Compatible (Etype (Q), T)
1102 or else (Is_Scalar_Type (T)
1103 and then Subtypes_Statically_Match
1105 or else (Is_Access_Type (T)
1106 and then Subtypes_Statically_Match
1107 (T, First_Subtype (T)))))
1108 or else (Is_Composite_Type (T)
1111 -- If Q is an aggregate, Is_Constrained may not be set
1112 -- yet and its type may not be resolved yet.
1113 -- This doesn't quite correspond to the complex notion
1114 -- of "known to be constrained" but this is good enough
1115 -- for a rule which is in any case too complex.
1117 not (Is_Constrained (Etype (Q))
1118 or else Nkind (Q) = N_Aggregate
1119 or else Subtypes_Statically_Match
1120 (T, First_Subtype (T))))
1123 ("subtype of renamed qualified expression does not " &
1124 "statically match", N);
1131 -- If the renamed object is a function call of a limited type,
1132 -- the expansion of the renaming is complicated by the presence
1133 -- of various temporaries and subtypes that capture constraints
1134 -- of the renamed object. Rewrite node as an object declaration,
1135 -- whose expansion is simpler. Given that the object is limited
1136 -- there is no copy involved and no performance hit.
1138 if Nkind (Nam) = N_Function_Call
1139 and then Is_Limited_View (Etype (Nam))
1140 and then not Is_Constrained (Etype (Nam))
1141 and then Comes_From_Source (N)
1144 Mutate_Ekind (Id, E_Constant);
1146 Make_Object_Declaration (Loc,
1147 Defining_Identifier => Id,
1148 Constant_Present => True,
1149 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
1150 Expression => Relocate_Node (Nam)));
1154 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
1155 -- when renaming declaration has a named access type. The Ada 2012
1156 -- coverage rules allow an anonymous access type in the context of
1157 -- an expected named general access type, but the renaming rules
1158 -- require the types to be the same. (An exception is when the type
1159 -- of the renaming is also an anonymous access type, which can only
1160 -- happen due to a renaming created by the expander.)
1162 if Nkind (Nam) = N_Type_Conversion
1163 and then not Comes_From_Source (Nam)
1164 and then Is_Anonymous_Access_Type (Etype (Expression (Nam)))
1165 and then not Is_Anonymous_Access_Type (T)
1168 ("cannot rename anonymous access object "
1169 & "as a named access type", Expression (Nam), T);
1172 -- Check that a class-wide object is not being renamed as an object
1173 -- of a specific type. The test for access types is needed to exclude
1174 -- cases where the renamed object is a dynamically tagged access
1175 -- result, such as occurs in certain expansions.
1177 if Is_Tagged_Type (T) then
1178 Check_Dynamically_Tagged_Expression
1184 -- Ada 2005 (AI-230/AI-254): Access renaming
1186 else pragma Assert (Present (Access_Definition (N)));
1190 N => Access_Definition (N));
1194 -- The object renaming declaration may become Ghost if it renames a
1197 if Is_Entity_Name (Nam) then
1198 Mark_Ghost_Renaming (N, Entity (Nam));
1201 -- Ada 2005 AI05-105: if the declaration has an anonymous access
1202 -- type, the renamed object must also have an anonymous type, and
1203 -- this is a name resolution rule. This was implicit in the last part
1204 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
1207 if not Is_Overloaded (Nam) then
1208 if Ekind (Etype (Nam)) /= Ekind (T) then
1210 ("expect anonymous access type in object renaming", N);
1217 Typ : Entity_Id := Empty;
1218 Seen : Boolean := False;
1221 Get_First_Interp (Nam, I, It);
1222 while Present (It.Typ) loop
1224 -- Renaming is ambiguous if more than one candidate
1225 -- interpretation is type-conformant with the context.
1227 if Ekind (It.Typ) = Ekind (T) then
1228 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
1231 (Designated_Type (T), Designated_Type (It.Typ))
1237 ("ambiguous expression in renaming", Nam);
1240 elsif Ekind (T) = E_Anonymous_Access_Type
1242 Covers (Designated_Type (T), Designated_Type (It.Typ))
1248 ("ambiguous expression in renaming", Nam);
1252 if Covers (T, It.Typ) then
1254 Set_Etype (Nam, Typ);
1255 Set_Is_Overloaded (Nam, False);
1259 Get_Next_Interp (I, It);
1266 -- Do not perform the legality checks below when the resolution of
1267 -- the renaming name failed because the associated type is Any_Type.
1269 if Etype (Nam) = Any_Type then
1272 -- Ada 2005 (AI-231): In the case where the type is defined by an
1273 -- access_definition, the renamed entity shall be of an access-to-
1274 -- constant type if and only if the access_definition defines an
1275 -- access-to-constant type. ARM 8.5.1(4)
1277 elsif Constant_Present (Access_Definition (N))
1278 and then not Is_Access_Constant (Etype (Nam))
1281 ("(Ada 2005): the renamed object is not access-to-constant "
1282 & "(RM 8.5.1(6))", N);
1284 elsif not Constant_Present (Access_Definition (N))
1285 and then Is_Access_Constant (Etype (Nam))
1288 ("(Ada 2005): the renamed object is not access-to-variable "
1289 & "(RM 8.5.1(6))", N);
1292 if Is_Access_Subprogram_Type (Etype (Nam)) then
1293 Check_Subtype_Conformant
1294 (Designated_Type (T), Designated_Type (Etype (Nam)));
1296 elsif not Subtypes_Statically_Match
1297 (Designated_Type (T),
1298 Available_View (Designated_Type (Etype (Nam))))
1301 ("subtype of renamed object does not statically match", N);
1305 -- Special processing for renaming function return object. Some errors
1306 -- and warnings are produced only for calls that come from source.
1308 if Nkind (Nam) = N_Function_Call then
1311 -- Usage is illegal in Ada 83, but renamings are also introduced
1312 -- during expansion, and error does not apply to those.
1315 if Comes_From_Source (N) then
1317 ("(Ada 83) cannot rename function return object", Nam);
1320 -- In Ada 95, warn for odd case of renaming parameterless function
1321 -- call if this is not a limited type (where this is useful).
1324 if Warn_On_Object_Renames_Function
1325 and then No (Parameter_Associations (Nam))
1326 and then not Is_Limited_Type (Etype (Nam))
1327 and then Comes_From_Source (Nam)
1330 ("renaming function result object is suspicious?R?", Nam);
1332 ("\function & will be called only once?R?", Nam,
1333 Entity (Name (Nam)));
1334 Error_Msg_N -- CODEFIX
1335 ("\suggest using an initialized constant object "
1336 & "instead?R?", Nam);
1341 Check_Constrained_Object;
1343 -- An object renaming requires an exact match of the type. Class-wide
1344 -- matching is not allowed.
1346 if Is_Class_Wide_Type (T)
1347 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1349 Wrong_Type (Nam, T);
1352 -- We must search for an actual subtype here so that the bounds of
1353 -- objects of unconstrained types don't get dropped on the floor - such
1354 -- as with renamings of formal parameters.
1356 T2 := Get_Actual_Subtype_If_Available (Nam);
1358 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1360 if Nkind (Nam) = N_Explicit_Dereference
1361 and then Ekind (Etype (T2)) = E_Incomplete_Type
1363 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1366 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1367 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1371 if Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1373 Nam_Ent : constant Entity_Id := Entity (Get_Object_Name (Nam));
1374 Nam_Decl : constant Node_Id := Declaration_Node (Nam_Ent);
1377 if Has_Null_Exclusion (N)
1378 and then not Has_Null_Exclusion (Nam_Decl)
1380 -- Ada 2005 (AI-423): If the object name denotes a generic
1381 -- formal object of a generic unit G, and the object renaming
1382 -- declaration occurs within the body of G or within the body
1383 -- of a generic unit declared within the declarative region
1384 -- of G, then the declaration of the formal object of G must
1385 -- have a null exclusion or a null-excluding subtype.
1387 if Is_Formal_Object (Nam_Ent)
1388 and then In_Generic_Scope (Id)
1390 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1392 ("object does not exclude `NULL` "
1393 & "(RM 8.5.1(4.6/2))", N);
1395 elsif In_Package_Body (Scope (Id)) then
1397 ("formal object does not have a null exclusion"
1398 & "(RM 8.5.1(4.6/2))", N);
1401 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1402 -- shall exclude null.
1404 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1406 ("object does not exclude `NULL` "
1407 & "(RM 8.5.1(4.6/2))", N);
1409 -- An instance is illegal if it contains a renaming that
1410 -- excludes null, and the actual does not. The renaming
1411 -- declaration has already indicated that the declaration
1412 -- of the renamed actual in the instance will raise
1413 -- constraint_error.
1415 elsif Nkind (Nam_Decl) = N_Object_Declaration
1416 and then In_Instance
1418 Present (Corresponding_Generic_Association (Nam_Decl))
1419 and then Nkind (Expression (Nam_Decl)) =
1420 N_Raise_Constraint_Error
1423 ("actual does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1425 -- Finally, if there is a null exclusion, the subtype mark
1426 -- must not be null-excluding.
1428 elsif No (Access_Definition (N))
1429 and then Can_Never_Be_Null (T)
1432 ("`NOT NULL` not allowed (& already excludes null)",
1437 elsif Can_Never_Be_Null (T)
1438 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1441 ("object does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1443 elsif Has_Null_Exclusion (N)
1444 and then No (Access_Definition (N))
1445 and then Can_Never_Be_Null (T)
1448 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1453 -- Set the Ekind of the entity, unless it has been set already, as is
1454 -- the case for the iteration object over a container with no variable
1455 -- indexing. In that case it's been marked as a constant, and we do not
1456 -- want to change it to a variable.
1458 if Ekind (Id) /= E_Constant then
1459 Mutate_Ekind (Id, E_Variable);
1462 Reinit_Object_Size_Align (Id);
1464 -- If N comes from source then check that the original node is an
1465 -- object reference since there may have been several rewritting and
1466 -- folding. Do not do this for N_Function_Call or N_Explicit_Dereference
1467 -- which might correspond to rewrites of e.g. N_Selected_Component
1468 -- (for example Object.Method rewriting).
1469 -- If N does not come from source then assume the tree is properly
1470 -- formed and accept any object reference. In such cases we do support
1471 -- more cases of renamings anyway, so the actual check on which renaming
1472 -- is valid is better left to the code generator as a last sanity
1475 if Comes_From_Source (N) then
1476 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference then
1477 Is_Object_Ref := Is_Object_Reference (Nam);
1479 Is_Object_Ref := Is_Object_Reference (Original_Node (Nam));
1482 Is_Object_Ref := True;
1485 if T = Any_Type or else Etype (Nam) = Any_Type then
1488 -- Verify that the renamed entity is an object or function call
1490 elsif Is_Object_Ref then
1491 if Comes_From_Source (N) then
1492 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1494 ("illegal renaming of discriminant-dependent component", Nam);
1497 -- If the renaming comes from source and the renamed object is a
1498 -- dereference, then mark the prefix as needing debug information,
1499 -- since it might have been rewritten hence internally generated
1500 -- and Debug_Renaming_Declaration will link the renaming to it.
1502 if Nkind (Nam) = N_Explicit_Dereference
1503 and then Is_Entity_Name (Prefix (Nam))
1505 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1509 -- Weird but legal, equivalent to renaming a function call. Illegal
1510 -- if the literal is the result of constant-folding an attribute
1511 -- reference that is not a function.
1513 elsif Is_Entity_Name (Nam)
1514 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1515 and then Nkind (Original_Node (Nam)) /= N_Attribute_Reference
1519 -- A named number can only be renamed without a subtype mark
1521 elsif Nkind (Nam) in N_Real_Literal | N_Integer_Literal
1522 and then Present (Subtype_Mark (N))
1523 and then Present (Original_Entity (Nam))
1525 Error_Msg_N ("incompatible types in renaming", Nam);
1527 -- AI12-0383: Names that denote values can be renamed.
1528 -- Ignore (accept) N_Raise_xxx_Error nodes in this context.
1530 elsif No_Raise_xxx_Error (Nam) = OK then
1531 Error_Msg_Ada_2022_Feature ("value in renaming", Sloc (Nam));
1536 if not Is_Variable (Nam) then
1537 Mutate_Ekind (Id, E_Constant);
1538 Set_Never_Set_In_Source (Id, True);
1539 Set_Is_True_Constant (Id, True);
1542 -- The entity of the renaming declaration needs to reflect whether the
1543 -- renamed object is atomic, independent, volatile or VFA. These flags
1544 -- are set on the renamed object in the RM legality sense.
1546 Set_Is_Atomic (Id, Is_Atomic_Object (Nam));
1547 Set_Is_Independent (Id, Is_Independent_Object (Nam));
1548 Set_Is_Volatile (Id, Is_Volatile_Object_Ref (Nam));
1549 Set_Is_Volatile_Full_Access
1550 (Id, Is_Volatile_Full_Access_Object_Ref (Nam));
1552 -- Treat as volatile if we just set the Volatile flag
1556 -- Or if we are renaming an entity which was marked this way
1558 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1560 or else (Is_Entity_Name (Nam)
1561 and then Treat_As_Volatile (Entity (Nam)))
1563 Set_Treat_As_Volatile (Id, True);
1566 -- Now make the link to the renamed object
1568 Set_Renamed_Object (Id, Nam);
1570 -- Implementation-defined aspect specifications can appear in a renaming
1571 -- declaration, but not language-defined ones. The call to procedure
1572 -- Analyze_Aspect_Specifications will take care of this error check.
1574 if Has_Aspects (N) then
1575 Analyze_Aspect_Specifications (N, Id);
1578 -- Deal with dimensions
1580 Analyze_Dimension (N);
1581 end Analyze_Object_Renaming;
1583 ------------------------------
1584 -- Analyze_Package_Renaming --
1585 ------------------------------
1587 procedure Analyze_Package_Renaming (N : Node_Id) is
1588 New_P : constant Entity_Id := Defining_Entity (N);
1593 if Name (N) = Error then
1597 -- Check for Text_IO special unit (we may be renaming a Text_IO child)
1599 Check_Text_IO_Special_Unit (Name (N));
1601 if Current_Scope /= Standard_Standard then
1602 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1608 if Is_Entity_Name (Name (N)) then
1609 Old_P := Entity (Name (N));
1614 if Etype (Old_P) = Any_Type then
1615 Error_Msg_N ("expect package name in renaming", Name (N));
1617 elsif Ekind (Old_P) /= E_Package
1618 and then not (Ekind (Old_P) = E_Generic_Package
1619 and then In_Open_Scopes (Old_P))
1621 if Ekind (Old_P) = E_Generic_Package then
1623 ("generic package cannot be renamed as a package", Name (N));
1625 Error_Msg_Sloc := Sloc (Old_P);
1627 ("expect package name in renaming, found& declared#",
1631 -- Set basic attributes to minimize cascaded errors
1633 Mutate_Ekind (New_P, E_Package);
1634 Set_Etype (New_P, Standard_Void_Type);
1636 elsif Present (Renamed_Entity (Old_P))
1637 and then (From_Limited_With (Renamed_Entity (Old_P))
1638 or else Has_Limited_View (Renamed_Entity (Old_P)))
1640 Unit_Is_Visible (Cunit (Get_Source_Unit (Renamed_Entity (Old_P))))
1643 ("renaming of limited view of package & not usable in this context"
1644 & " (RM 8.5.3(3.1/2))", Name (N), Renamed_Entity (Old_P));
1646 -- Set basic attributes to minimize cascaded errors
1648 Mutate_Ekind (New_P, E_Package);
1649 Set_Etype (New_P, Standard_Void_Type);
1651 -- Here for OK package renaming
1654 -- Entities in the old package are accessible through the renaming
1655 -- entity. The simplest implementation is to have both packages share
1658 Mutate_Ekind (New_P, E_Package);
1659 Set_Etype (New_P, Standard_Void_Type);
1661 if Present (Renamed_Object (Old_P)) then
1662 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1664 Set_Renamed_Object (New_P, Old_P);
1667 -- The package renaming declaration may become Ghost if it renames a
1670 Mark_Ghost_Renaming (N, Old_P);
1672 Set_Has_Completion (New_P);
1673 Set_First_Entity (New_P, First_Entity (Old_P));
1674 Set_Last_Entity (New_P, Last_Entity (Old_P));
1675 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1676 Check_Library_Unit_Renaming (N, Old_P);
1677 Generate_Reference (Old_P, Name (N));
1679 -- If the renaming is in the visible part of a package, then we set
1680 -- Renamed_In_Spec for the renamed package, to prevent giving
1681 -- warnings about no entities referenced. Such a warning would be
1682 -- overenthusiastic, since clients can see entities in the renamed
1683 -- package via the visible package renaming.
1686 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1688 if Ekind (Ent) = E_Package
1689 and then not In_Private_Part (Ent)
1690 and then In_Extended_Main_Source_Unit (N)
1691 and then Ekind (Old_P) = E_Package
1693 Set_Renamed_In_Spec (Old_P);
1697 -- If this is the renaming declaration of a package instantiation
1698 -- within itself, it is the declaration that ends the list of actuals
1699 -- for the instantiation. At this point, the subtypes that rename
1700 -- the actuals are flagged as generic, to avoid spurious ambiguities
1701 -- if the actuals for two distinct formals happen to coincide. If
1702 -- the actual is a private type, the subtype has a private completion
1703 -- that is flagged in the same fashion.
1705 -- Resolution is identical to what is was in the original generic.
1706 -- On exit from the generic instance, these are turned into regular
1707 -- subtypes again, so they are compatible with types in their class.
1709 if not Is_Generic_Instance (Old_P) then
1712 Spec := Specification (Unit_Declaration_Node (Old_P));
1715 if Nkind (Spec) = N_Package_Specification
1716 and then Present (Generic_Parent (Spec))
1717 and then Old_P = Current_Scope
1718 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1724 E := First_Entity (Old_P);
1725 while Present (E) and then E /= New_P loop
1727 and then Nkind (Parent (E)) = N_Subtype_Declaration
1729 Set_Is_Generic_Actual_Type (E);
1731 if Is_Private_Type (E)
1732 and then Present (Full_View (E))
1734 Set_Is_Generic_Actual_Type (Full_View (E));
1744 -- Implementation-defined aspect specifications can appear in a renaming
1745 -- declaration, but not language-defined ones. The call to procedure
1746 -- Analyze_Aspect_Specifications will take care of this error check.
1748 if Has_Aspects (N) then
1749 Analyze_Aspect_Specifications (N, New_P);
1751 end Analyze_Package_Renaming;
1753 -------------------------------
1754 -- Analyze_Renamed_Character --
1755 -------------------------------
1757 procedure Analyze_Renamed_Character
1762 C : constant Node_Id := Name (N);
1765 if Ekind (New_S) = E_Function then
1766 Resolve (C, Etype (New_S));
1769 Check_Frozen_Renaming (N, New_S);
1773 Error_Msg_N ("character literal can only be renamed as function", N);
1775 end Analyze_Renamed_Character;
1777 ---------------------------------
1778 -- Analyze_Renamed_Dereference --
1779 ---------------------------------
1781 procedure Analyze_Renamed_Dereference
1786 Nam : constant Node_Id := Name (N);
1787 P : constant Node_Id := Prefix (Nam);
1793 if not Is_Overloaded (P) then
1794 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1795 or else not Type_Conformant (Etype (Nam), New_S)
1797 Error_Msg_N ("designated type does not match specification", P);
1806 Get_First_Interp (Nam, Ind, It);
1808 while Present (It.Nam) loop
1810 if Ekind (It.Nam) = E_Subprogram_Type
1811 and then Type_Conformant (It.Nam, New_S)
1813 if Typ /= Any_Id then
1814 Error_Msg_N ("ambiguous renaming", P);
1821 Get_Next_Interp (Ind, It);
1824 if Typ = Any_Type then
1825 Error_Msg_N ("designated type does not match specification", P);
1830 Check_Frozen_Renaming (N, New_S);
1834 end Analyze_Renamed_Dereference;
1836 ---------------------------
1837 -- Analyze_Renamed_Entry --
1838 ---------------------------
1840 procedure Analyze_Renamed_Entry
1845 Nam : constant Node_Id := Name (N);
1846 Sel : constant Node_Id := Selector_Name (Nam);
1847 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1851 if Entity (Sel) = Any_Id then
1853 -- Selector is undefined on prefix. Error emitted already
1855 Set_Has_Completion (New_S);
1859 -- Otherwise find renamed entity and build body of New_S as a call to it
1861 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1863 if Old_S = Any_Id then
1864 Error_Msg_N ("no subprogram or entry matches specification", N);
1867 Check_Subtype_Conformant (New_S, Old_S, N);
1868 Generate_Reference (New_S, Defining_Entity (N), 'b');
1869 Style.Check_Identifier (Defining_Entity (N), New_S);
1872 -- Only mode conformance required for a renaming_as_declaration
1874 Check_Mode_Conformant (New_S, Old_S, N);
1877 Inherit_Renamed_Profile (New_S, Old_S);
1879 -- The prefix can be an arbitrary expression that yields a task or
1880 -- protected object, so it must be resolved.
1882 if Is_Access_Type (Etype (Prefix (Nam))) then
1883 Insert_Explicit_Dereference (Prefix (Nam));
1885 Resolve (Prefix (Nam), Scope (Old_S));
1888 Set_Convention (New_S, Convention (Old_S));
1889 Set_Has_Completion (New_S, Inside_A_Generic);
1891 -- AI05-0225: If the renamed entity is a procedure or entry of a
1892 -- protected object, the target object must be a variable.
1894 if Is_Protected_Type (Scope (Old_S))
1895 and then Ekind (New_S) = E_Procedure
1896 and then not Is_Variable (Prefix (Nam))
1900 ("target object of protected operation used as actual for "
1901 & "formal procedure must be a variable", Nam);
1904 ("target object of protected operation renamed as procedure, "
1905 & "must be a variable", Nam);
1910 Check_Frozen_Renaming (N, New_S);
1912 end Analyze_Renamed_Entry;
1914 -----------------------------------
1915 -- Analyze_Renamed_Family_Member --
1916 -----------------------------------
1918 procedure Analyze_Renamed_Family_Member
1923 Nam : constant Node_Id := Name (N);
1924 P : constant Node_Id := Prefix (Nam);
1928 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1929 or else (Nkind (P) = N_Selected_Component
1930 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1932 if Is_Entity_Name (P) then
1933 Old_S := Entity (P);
1935 Old_S := Entity (Selector_Name (P));
1938 if not Entity_Matches_Spec (Old_S, New_S) then
1939 Error_Msg_N ("entry family does not match specification", N);
1942 Check_Subtype_Conformant (New_S, Old_S, N);
1943 Generate_Reference (New_S, Defining_Entity (N), 'b');
1944 Style.Check_Identifier (Defining_Entity (N), New_S);
1948 Error_Msg_N ("no entry family matches specification", N);
1951 Set_Has_Completion (New_S, Inside_A_Generic);
1954 Check_Frozen_Renaming (N, New_S);
1956 end Analyze_Renamed_Family_Member;
1958 -----------------------------------------
1959 -- Analyze_Renamed_Primitive_Operation --
1960 -----------------------------------------
1962 procedure Analyze_Renamed_Primitive_Operation
1972 Ctyp : Conformance_Type) return Boolean;
1973 -- Verify that the signatures of the renamed entity and the new entity
1974 -- match. The first formal of the renamed entity is skipped because it
1975 -- is the target object in any subsequent call.
1983 Ctyp : Conformance_Type) return Boolean
1989 if Ekind (Subp) /= Ekind (New_S) then
1993 Old_F := Next_Formal (First_Formal (Subp));
1994 New_F := First_Formal (New_S);
1995 while Present (Old_F) and then Present (New_F) loop
1996 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
2000 if Ctyp >= Mode_Conformant
2001 and then Ekind (Old_F) /= Ekind (New_F)
2006 Next_Formal (New_F);
2007 Next_Formal (Old_F);
2013 -- Start of processing for Analyze_Renamed_Primitive_Operation
2016 if not Is_Overloaded (Selector_Name (Name (N))) then
2017 Old_S := Entity (Selector_Name (Name (N)));
2019 if not Conforms (Old_S, Type_Conformant) then
2024 -- Find the operation that matches the given signature
2032 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
2034 while Present (It.Nam) loop
2035 if Conforms (It.Nam, Type_Conformant) then
2039 Get_Next_Interp (Ind, It);
2044 if Old_S = Any_Id then
2045 Error_Msg_N ("no subprogram or entry matches specification", N);
2049 if not Conforms (Old_S, Subtype_Conformant) then
2050 Error_Msg_N ("subtype conformance error in renaming", N);
2053 Generate_Reference (New_S, Defining_Entity (N), 'b');
2054 Style.Check_Identifier (Defining_Entity (N), New_S);
2057 -- Only mode conformance required for a renaming_as_declaration
2059 if not Conforms (Old_S, Mode_Conformant) then
2060 Error_Msg_N ("mode conformance error in renaming", N);
2063 -- AI12-0204: The prefix of a prefixed view that is renamed or
2064 -- passed as a formal subprogram must be renamable as an object.
2066 Nam := Prefix (Name (N));
2068 if Is_Object_Reference (Nam) then
2069 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
2071 ("illegal renaming of discriminant-dependent component",
2075 Error_Msg_N ("expect object name in renaming", Nam);
2078 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
2079 -- view of a subprogram is intrinsic, because the compiler has
2080 -- to generate a wrapper for any call to it. If the name in a
2081 -- subprogram renaming is a prefixed view, the entity is thus
2082 -- intrinsic, and 'Access cannot be applied to it.
2084 Set_Convention (New_S, Convention_Intrinsic);
2087 -- Inherit_Renamed_Profile (New_S, Old_S);
2089 -- The prefix can be an arbitrary expression that yields an
2090 -- object, so it must be resolved.
2092 Resolve (Prefix (Name (N)));
2094 end Analyze_Renamed_Primitive_Operation;
2096 ---------------------------------
2097 -- Analyze_Subprogram_Renaming --
2098 ---------------------------------
2100 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
2101 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
2102 Is_Actual : constant Boolean := Present (Formal_Spec);
2103 Nam : constant Node_Id := Name (N);
2104 Save_AV : constant Ada_Version_Type := Ada_Version;
2105 Save_AVP : constant Node_Id := Ada_Version_Pragma;
2106 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
2107 Spec : constant Node_Id := Specification (N);
2109 Old_S : Entity_Id := Empty;
2110 Rename_Spec : Entity_Id;
2112 procedure Build_Class_Wide_Wrapper
2113 (Ren_Id : out Entity_Id;
2114 Wrap_Id : out Entity_Id);
2115 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal
2116 -- type with unknown discriminants and a generic primitive operation of
2117 -- the said type with a box require special processing when the actual
2118 -- is a class-wide type:
2121 -- type Formal_Typ (<>) is private;
2122 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
2123 -- package Gen is ...
2125 -- package Inst is new Gen (Actual_Typ'Class);
2127 -- In this case the general renaming mechanism used in the prologue of
2128 -- an instance no longer applies:
2130 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
2132 -- The above is replaced the following wrapper/renaming combination:
2134 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
2136 -- Prim_Op (Param); -- primitive
2139 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
2141 -- This transformation applies only if there is no explicit visible
2142 -- class-wide operation at the point of the instantiation. Ren_Id is
2143 -- the entity of the renaming declaration. When the transformation
2144 -- applies, Wrap_Id is the entity of the generated class-wide wrapper
2145 -- (or Any_Id). Otherwise, Wrap_Id is the entity of the class-wide
2148 procedure Check_Null_Exclusion
2151 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
2152 -- following AI rules:
2154 -- If Ren denotes a generic formal object of a generic unit G, and the
2155 -- renaming (or instantiation containing the actual) occurs within the
2156 -- body of G or within the body of a generic unit declared within the
2157 -- declarative region of G, then the corresponding parameter of G
2158 -- shall have a null_exclusion; Otherwise the subtype of the Sub's
2159 -- formal parameter shall exclude null.
2161 -- Similarly for its return profile.
2163 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id);
2164 -- Ensure that a SPARK renaming denoted by its entity Subp_Id does not
2165 -- declare a primitive operation of a tagged type (SPARK RM 6.1.1(3)).
2167 procedure Freeze_Actual_Profile;
2168 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
2169 -- types: a callable entity freezes its profile, unless it has an
2170 -- incomplete untagged formal (RM 13.14(10.2/3)).
2172 function Has_Class_Wide_Actual return Boolean;
2173 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
2174 -- defaulted formal subprogram where the actual for the controlling
2175 -- formal type is class-wide.
2177 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
2178 -- Find renamed entity when the declaration is a renaming_as_body and
2179 -- the renamed entity may itself be a renaming_as_body. Used to enforce
2180 -- rule that a renaming_as_body is illegal if the declaration occurs
2181 -- before the subprogram it completes is frozen, and renaming indirectly
2182 -- renames the subprogram itself.(Defect Report 8652/0027).
2184 ------------------------------
2185 -- Build_Class_Wide_Wrapper --
2186 ------------------------------
2188 procedure Build_Class_Wide_Wrapper
2189 (Ren_Id : out Entity_Id;
2190 Wrap_Id : out Entity_Id)
2192 Loc : constant Source_Ptr := Sloc (N);
2195 (Subp_Id : Entity_Id;
2196 Params : List_Id) return Node_Id;
2197 -- Create a dispatching call to invoke routine Subp_Id with actuals
2198 -- built from the parameter specifications of list Params.
2200 function Build_Expr_Fun_Call
2201 (Subp_Id : Entity_Id;
2202 Params : List_Id) return Node_Id;
2203 -- Create a dispatching call to invoke function Subp_Id with actuals
2204 -- built from the parameter specifications of list Params. Return
2205 -- directly the call, so that it can be used inside an expression
2206 -- function. This is a specificity of the GNATprove mode.
2208 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
2209 -- Create a subprogram specification based on the subprogram profile
2212 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
2213 -- Find a primitive subprogram of type Typ which matches the profile
2214 -- of the renaming declaration.
2216 procedure Interpretation_Error (Subp_Id : Entity_Id);
2217 -- Emit a continuation error message suggesting subprogram Subp_Id as
2218 -- a possible interpretation.
2220 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean;
2221 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
2224 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean;
2225 -- Determine whether subprogram Subp_Id is a suitable candidate for
2226 -- the role of a wrapped subprogram.
2233 (Subp_Id : Entity_Id;
2234 Params : List_Id) return Node_Id
2236 Actuals : constant List_Id := New_List;
2237 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2241 -- Build the actual parameters of the call
2243 Formal := First (Params);
2244 while Present (Formal) loop
2246 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2251 -- return Subp_Id (Actuals);
2253 if Ekind (Subp_Id) in E_Function | E_Operator then
2255 Make_Simple_Return_Statement (Loc,
2257 Make_Function_Call (Loc,
2259 Parameter_Associations => Actuals));
2262 -- Subp_Id (Actuals);
2266 Make_Procedure_Call_Statement (Loc,
2268 Parameter_Associations => Actuals);
2272 -------------------------
2273 -- Build_Expr_Fun_Call --
2274 -------------------------
2276 function Build_Expr_Fun_Call
2277 (Subp_Id : Entity_Id;
2278 Params : List_Id) return Node_Id
2280 Actuals : constant List_Id := New_List;
2281 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2285 pragma Assert (Ekind (Subp_Id) in E_Function | E_Operator);
2287 -- Build the actual parameters of the call
2289 Formal := First (Params);
2290 while Present (Formal) loop
2292 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2297 -- Subp_Id (Actuals);
2300 Make_Function_Call (Loc,
2302 Parameter_Associations => Actuals);
2303 end Build_Expr_Fun_Call;
2309 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2310 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2311 Spec_Id : constant Entity_Id :=
2312 Make_Defining_Identifier (Loc,
2313 Chars => New_External_Name (Chars (Subp_Id), 'R'));
2316 if Ekind (Formal_Spec) = E_Procedure then
2318 Make_Procedure_Specification (Loc,
2319 Defining_Unit_Name => Spec_Id,
2320 Parameter_Specifications => Params);
2323 Make_Function_Specification (Loc,
2324 Defining_Unit_Name => Spec_Id,
2325 Parameter_Specifications => Params,
2326 Result_Definition =>
2327 New_Copy_Tree (Result_Definition (Spec)));
2331 --------------------
2332 -- Find_Primitive --
2333 --------------------
2335 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2336 procedure Replace_Parameter_Types (Spec : Node_Id);
2337 -- Given a specification Spec, replace all class-wide parameter
2338 -- types with reference to type Typ.
2340 -----------------------------
2341 -- Replace_Parameter_Types --
2342 -----------------------------
2344 procedure Replace_Parameter_Types (Spec : Node_Id) is
2346 Formal_Id : Entity_Id;
2347 Formal_Typ : Node_Id;
2350 Formal := First (Parameter_Specifications (Spec));
2351 while Present (Formal) loop
2352 Formal_Id := Defining_Identifier (Formal);
2353 Formal_Typ := Parameter_Type (Formal);
2355 -- Create a new entity for each class-wide formal to prevent
2356 -- aliasing with the original renaming. Replace the type of
2357 -- such a parameter with the candidate type.
2359 if Nkind (Formal_Typ) = N_Identifier
2360 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2362 Set_Defining_Identifier (Formal,
2363 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2365 Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc));
2370 end Replace_Parameter_Types;
2374 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2375 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2376 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2377 Subp_Id : Entity_Id;
2379 -- Start of processing for Find_Primitive
2382 -- Each attempt to find a suitable primitive of a particular type
2383 -- operates on its own copy of the original renaming. As a result
2384 -- the original renaming is kept decoration and side-effect free.
2386 -- Inherit the overloaded status of the renamed subprogram name
2388 if Is_Overloaded (Nam) then
2389 Set_Is_Overloaded (Alt_Nam);
2390 Save_Interps (Nam, Alt_Nam);
2393 -- The copied renaming is hidden from visibility to prevent the
2394 -- pollution of the enclosing context.
2396 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2398 -- The types of all class-wide parameters must be changed to the
2401 Replace_Parameter_Types (Alt_Spec);
2403 -- Try to find a suitable primitive which matches the altered
2404 -- profile of the renaming specification.
2409 Nam => Name (Alt_Ren),
2410 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2411 Is_Actual => Is_Actual);
2413 -- Do not return Any_Id if the resolion of the altered profile
2414 -- failed as this complicates further checks on the caller side,
2415 -- return Empty instead.
2417 if Subp_Id = Any_Id then
2424 --------------------------
2425 -- Interpretation_Error --
2426 --------------------------
2428 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2430 Error_Msg_Sloc := Sloc (Subp_Id);
2432 if Is_Internal (Subp_Id) then
2434 ("\\possible interpretation: predefined & #",
2438 ("\\possible interpretation: & defined #", Spec, Formal_Spec);
2440 end Interpretation_Error;
2442 ---------------------------
2443 -- Is_Intrinsic_Equality --
2444 ---------------------------
2446 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is
2449 Ekind (Subp_Id) = E_Operator
2450 and then Chars (Subp_Id) = Name_Op_Eq
2451 and then Is_Intrinsic_Subprogram (Subp_Id);
2452 end Is_Intrinsic_Equality;
2454 ---------------------------
2455 -- Is_Suitable_Candidate --
2456 ---------------------------
2458 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is
2460 if No (Subp_Id) then
2463 -- An intrinsic subprogram is never a good candidate. This is an
2464 -- indication of a missing primitive, either defined directly or
2465 -- inherited from a parent tagged type.
2467 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2473 end Is_Suitable_Candidate;
2477 Actual_Typ : Entity_Id := Empty;
2478 -- The actual class-wide type for Formal_Typ
2480 CW_Prim_OK : Boolean;
2481 CW_Prim_Op : Entity_Id;
2482 -- The class-wide subprogram (if available) which corresponds to the
2483 -- renamed generic formal subprogram.
2485 Formal_Typ : Entity_Id := Empty;
2486 -- The generic formal type with unknown discriminants
2488 Root_Prim_OK : Boolean;
2489 Root_Prim_Op : Entity_Id;
2490 -- The root type primitive (if available) which corresponds to the
2491 -- renamed generic formal subprogram.
2493 Root_Typ : Entity_Id := Empty;
2494 -- The root type of Actual_Typ
2496 Body_Decl : Node_Id;
2498 Prim_Op : Entity_Id;
2499 Spec_Decl : Node_Id;
2502 -- Start of processing for Build_Class_Wide_Wrapper
2505 -- Analyze the specification of the renaming in case the generation
2506 -- of the class-wide wrapper fails.
2508 Ren_Id := Analyze_Subprogram_Specification (Spec);
2511 -- Do not attempt to build a wrapper if the renaming is in error
2513 if Error_Posted (Nam) then
2517 -- Analyze the renamed name, but do not resolve it. The resolution is
2518 -- completed once a suitable subprogram is found.
2522 -- When the renamed name denotes the intrinsic operator equals, the
2523 -- name must be treated as overloaded. This allows for a potential
2524 -- match against the root type's predefined equality function.
2526 if Is_Intrinsic_Equality (Entity (Nam)) then
2527 Set_Is_Overloaded (Nam);
2528 Collect_Interps (Nam);
2531 -- Step 1: Find the generic formal type with unknown discriminants
2532 -- and its corresponding class-wide actual type from the renamed
2533 -- generic formal subprogram.
2535 Formal := First_Formal (Formal_Spec);
2536 while Present (Formal) loop
2537 if Has_Unknown_Discriminants (Etype (Formal))
2538 and then not Is_Class_Wide_Type (Etype (Formal))
2539 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2541 Formal_Typ := Etype (Formal);
2542 Actual_Typ := Base_Type (Get_Instance_Of (Formal_Typ));
2543 Root_Typ := Root_Type (Actual_Typ);
2547 Next_Formal (Formal);
2550 -- The specification of the generic formal subprogram should always
2551 -- contain a formal type with unknown discriminants whose actual is
2552 -- a class-wide type, otherwise this indicates a failure in routine
2553 -- Has_Class_Wide_Actual.
2555 pragma Assert (Present (Formal_Typ));
2557 -- Step 2: Find the proper class-wide subprogram or primitive which
2558 -- corresponds to the renamed generic formal subprogram.
2560 CW_Prim_Op := Find_Primitive (Actual_Typ);
2561 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2562 Root_Prim_Op := Find_Primitive (Root_Typ);
2563 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2565 -- The class-wide actual type has two subprograms which correspond to
2566 -- the renamed generic formal subprogram:
2568 -- with procedure Prim_Op (Param : Formal_Typ);
2570 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2571 -- procedure Prim_Op (Param : Actual_Typ'Class);
2573 -- Even though the declaration of the two subprograms is legal, a
2574 -- call to either one is ambiguous and therefore illegal.
2576 if CW_Prim_OK and Root_Prim_OK then
2578 -- A user-defined primitive has precedence over a predefined one
2580 if Is_Internal (CW_Prim_Op)
2581 and then not Is_Internal (Root_Prim_Op)
2583 Prim_Op := Root_Prim_Op;
2585 elsif Is_Internal (Root_Prim_Op)
2586 and then not Is_Internal (CW_Prim_Op)
2588 Prim_Op := CW_Prim_Op;
2590 elsif CW_Prim_Op = Root_Prim_Op then
2591 Prim_Op := Root_Prim_Op;
2593 -- The two subprograms are legal but the class-wide subprogram is
2594 -- a class-wide wrapper built for a previous instantiation; the
2595 -- wrapper has precedence.
2597 elsif Present (Alias (CW_Prim_Op))
2598 and then Is_Class_Wide_Wrapper (Ultimate_Alias (CW_Prim_Op))
2600 Prim_Op := CW_Prim_Op;
2602 -- Otherwise both candidate subprograms are user-defined and
2607 ("ambiguous actual for generic subprogram &",
2609 Interpretation_Error (Root_Prim_Op);
2610 Interpretation_Error (CW_Prim_Op);
2614 elsif CW_Prim_OK and not Root_Prim_OK then
2615 Prim_Op := CW_Prim_Op;
2617 elsif not CW_Prim_OK and Root_Prim_OK then
2618 Prim_Op := Root_Prim_Op;
2620 -- An intrinsic equality may act as a suitable candidate in the case
2621 -- of a null type extension where the parent's equality is hidden. A
2622 -- call to an intrinsic equality is expanded as dispatching.
2624 elsif Present (Root_Prim_Op)
2625 and then Is_Intrinsic_Equality (Root_Prim_Op)
2627 Prim_Op := Root_Prim_Op;
2629 -- Otherwise there are no candidate subprograms. Let the caller
2630 -- diagnose the error.
2636 -- At this point resolution has taken place and the name is no longer
2637 -- overloaded. Mark the primitive as referenced.
2639 Set_Is_Overloaded (Name (N), False);
2640 Set_Referenced (Prim_Op);
2642 -- Do not generate a wrapper when the only candidate is a class-wide
2643 -- subprogram. Instead modify the renaming to directly map the actual
2644 -- to the generic formal.
2646 if CW_Prim_OK and then Prim_Op = CW_Prim_Op then
2648 Rewrite (Nam, New_Occurrence_Of (Prim_Op, Loc));
2652 -- Step 3: Create the declaration and the body of the wrapper, insert
2653 -- all the pieces into the tree.
2655 -- In GNATprove mode, create a function wrapper in the form of an
2656 -- expression function, so that an implicit postcondition relating
2657 -- the result of calling the wrapper function and the result of the
2658 -- dispatching call to the wrapped function is known during proof.
2661 and then Ekind (Ren_Id) in E_Function | E_Operator
2663 New_Spec := Build_Spec (Ren_Id);
2665 Make_Expression_Function (Loc,
2666 Specification => New_Spec,
2669 (Subp_Id => Prim_Op,
2670 Params => Parameter_Specifications (New_Spec)));
2672 Wrap_Id := Defining_Entity (Body_Decl);
2674 -- Otherwise, create separate spec and body for the subprogram
2678 Make_Subprogram_Declaration (Loc,
2679 Specification => Build_Spec (Ren_Id));
2680 Insert_Before_And_Analyze (N, Spec_Decl);
2682 Wrap_Id := Defining_Entity (Spec_Decl);
2685 Make_Subprogram_Body (Loc,
2686 Specification => Build_Spec (Ren_Id),
2687 Declarations => New_List,
2688 Handled_Statement_Sequence =>
2689 Make_Handled_Sequence_Of_Statements (Loc,
2690 Statements => New_List (
2692 (Subp_Id => Prim_Op,
2694 Parameter_Specifications
2695 (Specification (Spec_Decl))))));
2697 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
2700 Set_Is_Class_Wide_Wrapper (Wrap_Id);
2702 -- If the operator carries an Eliminated pragma, indicate that the
2703 -- wrapper is also to be eliminated, to prevent spurious error when
2704 -- using gnatelim on programs that include box-initialization of
2705 -- equality operators.
2707 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2709 -- In GNATprove mode, insert the body in the tree for analysis
2711 if GNATprove_Mode then
2712 Insert_Before_And_Analyze (N, Body_Decl);
2715 -- The generated body does not freeze and must be analyzed when the
2716 -- class-wide wrapper is frozen. The body is only needed if expansion
2719 if Expander_Active then
2720 Append_Freeze_Action (Wrap_Id, Body_Decl);
2723 -- Step 4: The subprogram renaming aliases the wrapper
2725 Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc));
2726 end Build_Class_Wide_Wrapper;
2728 --------------------------
2729 -- Check_Null_Exclusion --
2730 --------------------------
2732 procedure Check_Null_Exclusion
2736 Ren_Formal : Entity_Id;
2737 Sub_Formal : Entity_Id;
2739 function Null_Exclusion_Mismatch
2740 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean;
2741 -- Return True if there is a null exclusion mismatch between
2742 -- Renaming and Renamed, False otherwise.
2744 -----------------------------
2745 -- Null_Exclusion_Mismatch --
2746 -----------------------------
2748 function Null_Exclusion_Mismatch
2749 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean is
2751 return Has_Null_Exclusion (Parent (Renaming))
2753 not (Has_Null_Exclusion (Parent (Renamed))
2754 or else (Can_Never_Be_Null (Etype (Renamed))
2756 (Is_Formal_Subprogram (Sub)
2757 and then In_Generic_Body (Current_Scope))));
2758 end Null_Exclusion_Mismatch;
2763 Ren_Formal := First_Formal (Ren);
2764 Sub_Formal := First_Formal (Sub);
2765 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2766 if Null_Exclusion_Mismatch (Ren_Formal, Sub_Formal) then
2767 Error_Msg_Sloc := Sloc (Sub_Formal);
2769 ("`NOT NULL` required for parameter &#",
2770 Ren_Formal, Sub_Formal);
2773 Next_Formal (Ren_Formal);
2774 Next_Formal (Sub_Formal);
2777 -- Return profile check
2779 if Nkind (Parent (Ren)) = N_Function_Specification
2780 and then Nkind (Parent (Sub)) = N_Function_Specification
2781 and then Null_Exclusion_Mismatch (Ren, Sub)
2783 Error_Msg_Sloc := Sloc (Sub);
2784 Error_Msg_N ("return must specify `NOT NULL`#", Ren);
2786 end Check_Null_Exclusion;
2788 -------------------------------------
2789 -- Check_SPARK_Primitive_Operation --
2790 -------------------------------------
2792 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id) is
2793 Prag : constant Node_Id := SPARK_Pragma (Subp_Id);
2797 -- Nothing to do when the subprogram is not subject to SPARK_Mode On
2798 -- because this check applies to SPARK code only.
2800 if not (Present (Prag)
2801 and then Get_SPARK_Mode_From_Annotation (Prag) = On)
2805 -- Nothing to do when the subprogram is not a primitive operation
2807 elsif not Is_Primitive (Subp_Id) then
2811 Typ := Find_Dispatching_Type (Subp_Id);
2813 -- Nothing to do when the subprogram is a primitive operation of an
2820 -- At this point a renaming declaration introduces a new primitive
2821 -- operation for a tagged type.
2823 Error_Msg_Node_2 := Typ;
2825 ("subprogram renaming & cannot declare primitive for type & "
2826 & "(SPARK RM 6.1.1(3))", N, Subp_Id);
2827 end Check_SPARK_Primitive_Operation;
2829 ---------------------------
2830 -- Freeze_Actual_Profile --
2831 ---------------------------
2833 procedure Freeze_Actual_Profile is
2835 Has_Untagged_Inc : Boolean;
2836 Instantiation_Node : constant Node_Id := Parent (N);
2839 if Ada_Version >= Ada_2012 then
2840 F := First_Formal (Formal_Spec);
2841 Has_Untagged_Inc := False;
2842 while Present (F) loop
2843 if Ekind (Etype (F)) = E_Incomplete_Type
2844 and then not Is_Tagged_Type (Etype (F))
2846 Has_Untagged_Inc := True;
2853 if Ekind (Formal_Spec) = E_Function
2854 and then not Is_Tagged_Type (Etype (Formal_Spec))
2856 Has_Untagged_Inc := True;
2859 if not Has_Untagged_Inc then
2860 F := First_Formal (Old_S);
2861 while Present (F) loop
2862 Freeze_Before (Instantiation_Node, Etype (F));
2864 if Is_Incomplete_Or_Private_Type (Etype (F))
2865 and then No (Underlying_Type (Etype (F)))
2867 -- Exclude generic types, or types derived from them.
2868 -- They will be frozen in the enclosing instance.
2870 if Is_Generic_Type (Etype (F))
2871 or else Is_Generic_Type (Root_Type (Etype (F)))
2875 -- A limited view of a type declared elsewhere needs no
2876 -- freezing actions.
2878 elsif From_Limited_With (Etype (F)) then
2883 ("type& must be frozen before this point",
2884 Instantiation_Node, Etype (F));
2892 end Freeze_Actual_Profile;
2894 ---------------------------
2895 -- Has_Class_Wide_Actual --
2896 ---------------------------
2898 function Has_Class_Wide_Actual return Boolean is
2900 Formal_Typ : Entity_Id;
2904 Formal := First_Formal (Formal_Spec);
2905 while Present (Formal) loop
2906 Formal_Typ := Etype (Formal);
2908 if Has_Unknown_Discriminants (Formal_Typ)
2909 and then not Is_Class_Wide_Type (Formal_Typ)
2910 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2915 Next_Formal (Formal);
2920 end Has_Class_Wide_Actual;
2922 -------------------------
2923 -- Original_Subprogram --
2924 -------------------------
2926 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2927 Orig_Decl : Node_Id;
2928 Orig_Subp : Entity_Id;
2931 -- First case: renamed entity is itself a renaming
2933 if Present (Alias (Subp)) then
2934 return Alias (Subp);
2936 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2937 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2939 -- Check if renamed entity is a renaming_as_body
2942 Unit_Declaration_Node
2943 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2945 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2946 Orig_Subp := Entity (Name (Orig_Decl));
2948 if Orig_Subp = Rename_Spec then
2950 -- Circularity detected
2955 return (Original_Subprogram (Orig_Subp));
2963 end Original_Subprogram;
2967 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2968 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2969 -- defaulted formal subprogram when the actual for a related formal
2970 -- type is class-wide.
2972 Inst_Node : Node_Id := Empty;
2975 -- Start of processing for Analyze_Subprogram_Renaming
2978 -- We must test for the attribute renaming case before the Analyze
2979 -- call because otherwise Sem_Attr will complain that the attribute
2980 -- is missing an argument when it is analyzed.
2982 if Nkind (Nam) = N_Attribute_Reference then
2984 -- In the case of an abstract formal subprogram association, rewrite
2985 -- an actual given by a stream or Put_Image attribute as the name of
2986 -- the corresponding stream or Put_Image primitive of the type.
2988 -- In a generic context the stream and Put_Image operations are not
2989 -- generated, and this must be treated as a normal attribute
2990 -- reference, to be expanded in subsequent instantiations.
2993 and then Is_Abstract_Subprogram (Formal_Spec)
2994 and then Expander_Active
2997 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
3001 -- The class-wide forms of the stream and Put_Image attributes
3002 -- are not primitive dispatching operations (even though they
3003 -- internally dispatch).
3005 if Is_Class_Wide_Type (Prefix_Type) then
3007 ("attribute must be a primitive dispatching operation",
3012 -- Retrieve the primitive subprogram associated with the
3013 -- attribute. This can only be a stream attribute, since those
3014 -- are the only ones that are dispatching (and the actual for
3015 -- an abstract formal subprogram must be dispatching
3018 case Attribute_Name (Nam) is
3021 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input);
3025 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output);
3029 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read);
3033 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write);
3035 when Name_Put_Image =>
3037 Find_Optional_Prim_Op (Prefix_Type, TSS_Put_Image);
3041 ("attribute must be a primitive dispatching operation",
3046 -- If no stream operation was found, and the type is limited,
3047 -- the user should have defined one. This rule does not apply
3051 and then Attribute_Name (Nam) /= Name_Put_Image
3053 if Is_Limited_Type (Prefix_Type) then
3055 ("stream operation not defined for type&",
3059 -- Otherwise, compiler should have generated default
3062 raise Program_Error;
3066 -- Rewrite the attribute into the name of its corresponding
3067 -- primitive dispatching subprogram. We can then proceed with
3068 -- the usual processing for subprogram renamings.
3071 Prim_Name : constant Node_Id :=
3072 Make_Identifier (Sloc (Nam),
3073 Chars => Chars (Prim));
3075 Set_Entity (Prim_Name, Prim);
3076 Rewrite (Nam, Prim_Name);
3081 -- Normal processing for a renaming of an attribute
3084 Attribute_Renaming (N);
3089 -- Check whether this declaration corresponds to the instantiation of a
3090 -- formal subprogram.
3092 -- If this is an instantiation, the corresponding actual is frozen and
3093 -- error messages can be made more precise. If this is a default
3094 -- subprogram, the entity is already established in the generic, and is
3095 -- not retrieved by visibility. If it is a default with a box, the
3096 -- candidate interpretations, if any, have been collected when building
3097 -- the renaming declaration. If overloaded, the proper interpretation is
3098 -- determined in Find_Renamed_Entity. If the entity is an operator,
3099 -- Find_Renamed_Entity applies additional visibility checks.
3102 Inst_Node := Unit_Declaration_Node (Formal_Spec);
3104 -- Check whether the renaming is for a defaulted actual subprogram
3105 -- with a class-wide actual.
3107 if CW_Actual and then Box_Present (Inst_Node) then
3108 Build_Class_Wide_Wrapper (New_S, Old_S);
3110 elsif Is_Entity_Name (Nam)
3111 and then Present (Entity (Nam))
3112 and then not Comes_From_Source (Nam)
3113 and then not Is_Overloaded (Nam)
3115 Old_S := Entity (Nam);
3117 -- The subprogram renaming declaration may become Ghost if it
3118 -- renames a Ghost entity.
3120 Mark_Ghost_Renaming (N, Old_S);
3122 New_S := Analyze_Subprogram_Specification (Spec);
3126 if Ekind (Old_S) = E_Operator then
3130 if Box_Present (Inst_Node) then
3131 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3133 -- If there is an immediately visible homonym of the operator
3134 -- and the declaration has a default, this is worth a warning
3135 -- because the user probably did not intend to get the pre-
3136 -- defined operator, visible in the generic declaration. To
3137 -- find if there is an intended candidate, analyze the renaming
3138 -- again in the current context.
3140 elsif Scope (Old_S) = Standard_Standard
3141 and then Present (Default_Name (Inst_Node))
3144 Decl : constant Node_Id := New_Copy_Tree (N);
3148 Set_Entity (Name (Decl), Empty);
3149 Analyze (Name (Decl));
3151 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
3154 and then In_Open_Scopes (Scope (Hidden))
3155 and then Is_Immediately_Visible (Hidden)
3156 and then Comes_From_Source (Hidden)
3157 and then Hidden /= Old_S
3159 Error_Msg_Sloc := Sloc (Hidden);
3161 ("default subprogram is resolved in the generic "
3162 & "declaration (RM 12.6(17))??", N);
3163 Error_Msg_NE ("\and will not use & #??", N, Hidden);
3172 -- The subprogram renaming declaration may become Ghost if it
3173 -- renames a Ghost entity.
3175 if Is_Entity_Name (Nam) then
3176 Mark_Ghost_Renaming (N, Entity (Nam));
3179 New_S := Analyze_Subprogram_Specification (Spec);
3183 -- Renamed entity must be analyzed first, to avoid being hidden by
3184 -- new name (which might be the same in a generic instance).
3188 -- The subprogram renaming declaration may become Ghost if it renames
3191 if Is_Entity_Name (Nam) then
3192 Mark_Ghost_Renaming (N, Entity (Nam));
3195 -- The renaming defines a new overloaded entity, which is analyzed
3196 -- like a subprogram declaration.
3198 New_S := Analyze_Subprogram_Specification (Spec);
3201 if Current_Scope /= Standard_Standard then
3202 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
3205 -- Set SPARK mode from current context
3207 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
3208 Set_SPARK_Pragma_Inherited (New_S);
3210 Rename_Spec := Find_Corresponding_Spec (N);
3212 -- Case of Renaming_As_Body
3214 if Present (Rename_Spec) then
3215 Check_Previous_Null_Procedure (N, Rename_Spec);
3217 -- Renaming declaration is the completion of the declaration of
3218 -- Rename_Spec. We build an actual body for it at the freezing point.
3220 Set_Corresponding_Spec (N, Rename_Spec);
3222 -- Deal with special case of stream functions of abstract types
3225 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
3226 N_Abstract_Subprogram_Declaration
3228 -- Input stream functions are abstract if the object type is
3229 -- abstract. Similarly, all default stream functions for an
3230 -- interface type are abstract. However, these subprograms may
3231 -- receive explicit declarations in representation clauses, making
3232 -- the attribute subprograms usable as defaults in subsequent
3234 -- In this case we rewrite the declaration to make the subprogram
3235 -- non-abstract. We remove the previous declaration, and insert
3236 -- the new one at the point of the renaming, to prevent premature
3237 -- access to unfrozen types. The new declaration reuses the
3238 -- specification of the previous one, and must not be analyzed.
3241 (Is_Primitive (Entity (Nam))
3243 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
3245 Old_Decl : constant Node_Id :=
3246 Unit_Declaration_Node (Rename_Spec);
3247 New_Decl : constant Node_Id :=
3248 Make_Subprogram_Declaration (Sloc (N),
3250 Relocate_Node (Specification (Old_Decl)));
3253 Insert_After (N, New_Decl);
3254 Set_Is_Abstract_Subprogram (Rename_Spec, False);
3255 Set_Analyzed (New_Decl);
3259 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
3261 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3262 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
3265 Set_Convention (New_S, Convention (Rename_Spec));
3266 Check_Fully_Conformant (New_S, Rename_Spec);
3267 Set_Public_Status (New_S);
3269 if No_Return (Rename_Spec)
3270 and then not No_Return (Entity (Nam))
3273 ("renamed subprogram & must be No_Return", N, Entity (Nam));
3275 ("\since renaming subprogram is No_Return (RM 6.5.1(7/2))", N);
3278 -- The specification does not introduce new formals, but only
3279 -- repeats the formals of the original subprogram declaration.
3280 -- For cross-reference purposes, and for refactoring tools, we
3281 -- treat the formals of the renaming declaration as body formals.
3283 Reference_Body_Formals (Rename_Spec, New_S);
3285 -- Indicate that the entity in the declaration functions like the
3286 -- corresponding body, and is not a new entity. The body will be
3287 -- constructed later at the freeze point, so indicate that the
3288 -- completion has not been seen yet.
3290 Reinit_Field_To_Zero (New_S, F_Has_Out_Or_In_Out_Parameter);
3291 Reinit_Field_To_Zero (New_S, F_Needs_No_Actuals,
3292 Old_Ekind => (E_Function | E_Procedure => True, others => False));
3293 Mutate_Ekind (New_S, E_Subprogram_Body);
3294 New_S := Rename_Spec;
3295 Set_Has_Completion (Rename_Spec, False);
3297 -- Ada 2005: check overriding indicator
3299 if Present (Overridden_Operation (Rename_Spec)) then
3300 if Must_Not_Override (Specification (N)) then
3302 ("subprogram& overrides inherited operation",
3306 and then not Must_Override (Specification (N))
3308 Style.Missing_Overriding (N, Rename_Spec);
3311 elsif Must_Override (Specification (N))
3312 and then not Can_Override_Operator (Rename_Spec)
3314 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
3317 -- AI12-0132: a renames-as-body freezes the expression of any
3318 -- expression function that it renames.
3320 if Is_Entity_Name (Nam)
3321 and then Is_Expression_Function (Entity (Nam))
3322 and then not Inside_A_Generic
3325 (Def_Id => Entity (Nam),
3326 Typ => Etype (Entity (Nam)),
3329 (Original_Node (Unit_Declaration_Node (Entity (Nam)))),
3333 -- Normal subprogram renaming (not renaming as body)
3336 Generate_Definition (New_S);
3337 New_Overloaded_Entity (New_S);
3339 if not (Is_Entity_Name (Nam)
3340 and then Is_Intrinsic_Subprogram (Entity (Nam)))
3342 Check_Delayed_Subprogram (New_S);
3345 -- Verify that a SPARK renaming does not declare a primitive
3346 -- operation of a tagged type.
3348 Check_SPARK_Primitive_Operation (New_S);
3351 -- There is no need for elaboration checks on the new entity, which may
3352 -- be called before the next freezing point where the body will appear.
3353 -- Elaboration checks refer to the real entity, not the one created by
3354 -- the renaming declaration.
3356 Set_Kill_Elaboration_Checks (New_S, True);
3358 -- If we had a previous error, indicate a completion is present to stop
3359 -- junk cascaded messages, but don't take any further action.
3361 if Etype (Nam) = Any_Type then
3362 Set_Has_Completion (New_S);
3365 -- Case where name has the form of a selected component
3367 elsif Nkind (Nam) = N_Selected_Component then
3369 -- A name which has the form A.B can designate an entry of task A, a
3370 -- protected operation of protected object A, or finally a primitive
3371 -- operation of object A. In the later case, A is an object of some
3372 -- tagged type, or an access type that denotes one such. To further
3373 -- distinguish these cases, note that the scope of a task entry or
3374 -- protected operation is type of the prefix.
3376 -- The prefix could be an overloaded function call that returns both
3377 -- kinds of operations. This overloading pathology is left to the
3378 -- dedicated reader ???
3381 T : constant Entity_Id := Etype (Prefix (Nam));
3389 and then Is_Tagged_Type (Designated_Type (T))))
3390 and then Scope (Entity (Selector_Name (Nam))) /= T
3392 Analyze_Renamed_Primitive_Operation
3393 (N, New_S, Present (Rename_Spec));
3397 -- Renamed entity is an entry or protected operation. For those
3398 -- cases an explicit body is built (at the point of freezing of
3399 -- this entity) that contains a call to the renamed entity.
3401 -- This is not allowed for renaming as body if the renamed
3402 -- spec is already frozen (see RM 8.5.4(5) for details).
3404 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
3406 ("renaming-as-body cannot rename entry as subprogram", N);
3408 ("\since & is already frozen (RM 8.5.4(5))",
3411 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
3418 -- Case where name is an explicit dereference X.all
3420 elsif Nkind (Nam) = N_Explicit_Dereference then
3422 -- Renamed entity is designated by access_to_subprogram expression.
3423 -- Must build body to encapsulate call, as in the entry case.
3425 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
3428 -- Indexed component
3430 elsif Nkind (Nam) = N_Indexed_Component then
3431 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
3434 -- Character literal
3436 elsif Nkind (Nam) = N_Character_Literal then
3437 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
3440 -- Only remaining case is where we have a non-entity name, or a renaming
3441 -- of some other non-overloadable entity.
3443 elsif not Is_Entity_Name (Nam)
3444 or else not Is_Overloadable (Entity (Nam))
3446 -- Do not mention the renaming if it comes from an instance
3448 if not Is_Actual then
3449 Error_Msg_N ("expect valid subprogram name in renaming", N);
3451 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
3457 -- Find the renamed entity that matches the given specification. Disable
3458 -- Ada_83 because there is no requirement of full conformance between
3459 -- renamed entity and new entity, even though the same circuit is used.
3461 -- This is a bit of an odd case, which introduces a really irregular use
3462 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
3465 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
3466 Ada_Version_Pragma := Empty;
3467 Ada_Version_Explicit := Ada_Version;
3470 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3472 -- The visible operation may be an inherited abstract operation that
3473 -- was overridden in the private part, in which case a call will
3474 -- dispatch to the overriding operation. Use the overriding one in
3475 -- the renaming declaration, to prevent spurious errors below.
3477 if Is_Overloadable (Old_S)
3478 and then Is_Abstract_Subprogram (Old_S)
3479 and then No (DTC_Entity (Old_S))
3480 and then Present (Alias (Old_S))
3481 and then not Is_Abstract_Subprogram (Alias (Old_S))
3482 and then Present (Overridden_Operation (Alias (Old_S)))
3484 Old_S := Alias (Old_S);
3487 -- When the renamed subprogram is overloaded and used as an actual
3488 -- of a generic, its entity is set to the first available homonym.
3489 -- We must first disambiguate the name, then set the proper entity.
3491 if Is_Actual and then Is_Overloaded (Nam) then
3492 Set_Entity (Nam, Old_S);
3496 -- Most common case: subprogram renames subprogram. No body is generated
3497 -- in this case, so we must indicate the declaration is complete as is.
3498 -- and inherit various attributes of the renamed subprogram.
3500 if No (Rename_Spec) then
3501 Set_Has_Completion (New_S);
3502 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3503 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3504 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3506 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3507 -- between a subprogram and its correct renaming.
3509 -- Note: the Any_Id check is a guard that prevents compiler crashes
3510 -- when performing a null exclusion check between a renaming and a
3511 -- renamed subprogram that has been found to be illegal.
3513 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3514 Check_Null_Exclusion
3516 Sub => Entity (Nam));
3519 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3520 -- overriding. The flag Requires_Overriding is set very selectively
3521 -- and misses some other illegal cases. The additional conditions
3522 -- checked below are sufficient but not necessary ???
3524 -- The rule does not apply to the renaming generated for an actual
3525 -- subprogram in an instance.
3530 -- Guard against previous errors, and omit renamings of predefined
3533 elsif Ekind (Old_S) not in E_Function | E_Procedure then
3536 elsif Requires_Overriding (Old_S)
3538 (Is_Abstract_Subprogram (Old_S)
3539 and then Present (Find_Dispatching_Type (Old_S))
3540 and then not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3543 ("renamed entity cannot be subprogram that requires overriding "
3544 & "(RM 8.5.4 (5.1))", N);
3548 Prev : constant Entity_Id := Overridden_Operation (New_S);
3552 (Has_Non_Trivial_Precondition (Prev)
3553 or else Has_Non_Trivial_Precondition (Old_S))
3556 ("conflicting inherited classwide preconditions in renaming "
3557 & "of& (RM 6.1.1 (17)", N, Old_S);
3562 if Old_S /= Any_Id then
3563 if Is_Actual and then From_Default (N) then
3565 -- This is an implicit reference to the default actual
3567 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3570 Generate_Reference (Old_S, Nam);
3573 Check_Internal_Protected_Use (N, Old_S);
3575 -- For a renaming-as-body, require subtype conformance, but if the
3576 -- declaration being completed has not been frozen, then inherit the
3577 -- convention of the renamed subprogram prior to checking conformance
3578 -- (unless the renaming has an explicit convention established; the
3579 -- rule stated in the RM doesn't seem to address this ???).
3581 if Present (Rename_Spec) then
3582 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3583 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3585 if not Is_Frozen (Rename_Spec) then
3586 if not Has_Convention_Pragma (Rename_Spec) then
3587 Set_Convention (New_S, Convention (Old_S));
3590 if Ekind (Old_S) /= E_Operator then
3591 Check_Mode_Conformant (New_S, Old_S, Spec);
3594 if Original_Subprogram (Old_S) = Rename_Spec then
3595 Error_Msg_N ("unfrozen subprogram cannot rename itself", N);
3597 Check_Formal_Subprogram_Conformance (New_S, Old_S, Spec);
3600 Check_Subtype_Conformant (New_S, Old_S, Spec);
3603 Check_Frozen_Renaming (N, Rename_Spec);
3605 -- Check explicitly that renamed entity is not intrinsic, because
3606 -- in a generic the renamed body is not built. In this case,
3607 -- the renaming_as_body is a completion.
3609 if Inside_A_Generic then
3610 if Is_Frozen (Rename_Spec)
3611 and then Is_Intrinsic_Subprogram (Old_S)
3614 ("subprogram in renaming_as_body cannot be intrinsic",
3618 Set_Has_Completion (Rename_Spec);
3621 elsif Ekind (Old_S) /= E_Operator then
3623 -- If this a defaulted subprogram for a class-wide actual there is
3624 -- no check for mode conformance, given that the signatures don't
3625 -- match (the source mentions T but the actual mentions T'Class).
3630 -- No need for a redundant error message if this is a nested
3631 -- instance, unless the current instantiation (of a child unit)
3632 -- is a compilation unit, which is not analyzed when the parent
3633 -- generic is analyzed.
3636 or else No (Enclosing_Instance)
3637 or else Is_Compilation_Unit (Current_Scope)
3639 Check_Mode_Conformant (New_S, Old_S);
3643 if No (Rename_Spec) then
3645 -- The parameter profile of the new entity is that of the renamed
3646 -- entity: the subtypes given in the specification are irrelevant.
3648 Inherit_Renamed_Profile (New_S, Old_S);
3650 -- A call to the subprogram is transformed into a call to the
3651 -- renamed entity. This is transitive if the renamed entity is
3652 -- itself a renaming.
3654 if Present (Alias (Old_S)) then
3655 Set_Alias (New_S, Alias (Old_S));
3657 Set_Alias (New_S, Old_S);
3660 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3661 -- renaming as body, since the entity in this case is not an
3662 -- intrinsic (it calls an intrinsic, but we have a real body for
3663 -- this call, and it is in this body that the required intrinsic
3664 -- processing will take place).
3666 -- Also, if this is a renaming of inequality, the renamed operator
3667 -- is intrinsic, but what matters is the corresponding equality
3668 -- operator, which may be user-defined.
3670 Set_Is_Intrinsic_Subprogram
3672 Is_Intrinsic_Subprogram (Old_S)
3674 (Chars (Old_S) /= Name_Op_Ne
3675 or else Ekind (Old_S) = E_Operator
3676 or else Is_Intrinsic_Subprogram
3677 (Corresponding_Equality (Old_S))));
3679 if Ekind (Alias (New_S)) = E_Operator then
3680 Set_Has_Delayed_Freeze (New_S, False);
3683 -- If the renaming corresponds to an association for an abstract
3684 -- formal subprogram, then various attributes must be set to
3685 -- indicate that the renaming is an abstract dispatching operation
3686 -- with a controlling type.
3688 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
3690 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3691 -- see it as corresponding to a generic association for a
3692 -- formal abstract subprogram
3694 Set_Is_Abstract_Subprogram (New_S);
3697 New_S_Ctrl_Type : constant Entity_Id :=
3698 Find_Dispatching_Type (New_S);
3699 Old_S_Ctrl_Type : constant Entity_Id :=
3700 Find_Dispatching_Type (Old_S);
3704 -- The actual must match the (instance of the) formal,
3705 -- and must be a controlling type.
3707 if Old_S_Ctrl_Type /= New_S_Ctrl_Type
3708 or else No (New_S_Ctrl_Type)
3710 if No (New_S_Ctrl_Type) then
3712 ("actual must be dispatching subprogram", Nam);
3715 ("actual must be dispatching subprogram for type&",
3716 Nam, New_S_Ctrl_Type);
3720 Set_Is_Dispatching_Operation (New_S);
3721 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3723 -- If the actual in the formal subprogram is itself a
3724 -- formal abstract subprogram association, there's no
3725 -- dispatch table component or position to inherit.
3727 if Present (DTC_Entity (Old_S)) then
3728 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3729 Set_DT_Position_Value (New_S, DT_Position (Old_S));
3739 -- The following is illegal, because F hides whatever other F may
3741 -- function F (...) renames F;
3744 or else (Nkind (Nam) /= N_Expanded_Name
3745 and then Chars (Old_S) = Chars (New_S))
3747 Error_Msg_N ("subprogram cannot rename itself", N);
3749 -- This is illegal even if we use a selector:
3750 -- function F (...) renames Pkg.F;
3751 -- because F is still hidden.
3753 elsif Nkind (Nam) = N_Expanded_Name
3754 and then Entity (Prefix (Nam)) = Current_Scope
3755 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3757 -- This is an error, but we overlook the error and accept the
3758 -- renaming if the special Overriding_Renamings mode is in effect.
3760 if not Overriding_Renamings then
3762 ("implicit operation& is not visible (RM 8.3 (15))",
3767 Set_Convention (New_S, Convention (Old_S));
3769 if Is_Abstract_Subprogram (Old_S) then
3770 if Present (Rename_Spec) then
3772 ("a renaming-as-body cannot rename an abstract subprogram",
3774 Set_Has_Completion (Rename_Spec);
3776 Set_Is_Abstract_Subprogram (New_S);
3780 Check_Library_Unit_Renaming (N, Old_S);
3782 -- Pathological case: procedure renames entry in the scope of its
3783 -- task. Entry is given by simple name, but body must be built for
3784 -- procedure. Of course if called it will deadlock.
3786 if Ekind (Old_S) = E_Entry then
3787 Set_Has_Completion (New_S, False);
3788 Set_Alias (New_S, Empty);
3791 -- Do not freeze the renaming nor the renamed entity when the context
3792 -- is an enclosing generic. Freezing is an expansion activity, and in
3793 -- addition the renamed entity may depend on the generic formals of
3794 -- the enclosing generic.
3796 if Is_Actual and not Inside_A_Generic then
3797 Freeze_Before (N, Old_S);
3798 Freeze_Actual_Profile;
3799 Set_Has_Delayed_Freeze (New_S, False);
3800 Freeze_Before (N, New_S);
3802 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
3803 and then not Is_Abstract_Subprogram (Formal_Spec)
3805 -- An abstract subprogram is only allowed as an actual in the
3806 -- case where the formal subprogram is also abstract.
3808 if Is_Abstract_Subprogram (Old_S) then
3810 ("abstract subprogram not allowed as generic actual", Nam);
3813 -- AI12-0412: A primitive of an abstract type with Pre'Class
3814 -- or Post'Class aspects specified with nonstatic expressions
3815 -- is not allowed as actual for a nonabstract formal subprogram
3816 -- (see RM 6.1.1(18.2/5).
3818 if Is_Dispatching_Operation (Old_S)
3820 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post (Old_S)
3823 ("primitive of abstract type with nonstatic class-wide "
3824 & "pre/postconditions not allowed as actual",
3831 -- A common error is to assume that implicit operators for types are
3832 -- defined in Standard, or in the scope of a subtype. In those cases
3833 -- where the renamed entity is given with an expanded name, it is
3834 -- worth mentioning that operators for the type are not declared in
3835 -- the scope given by the prefix.
3837 if Nkind (Nam) = N_Expanded_Name
3838 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
3839 and then Scope (Entity (Nam)) = Standard_Standard
3842 T : constant Entity_Id :=
3843 Base_Type (Etype (First_Formal (New_S)));
3845 Error_Msg_Node_2 := Prefix (Nam);
3847 ("operator for type& is not declared in&", Prefix (Nam), T);
3852 ("no visible subprogram matches the specification for&",
3856 if Present (Candidate_Renaming) then
3863 F1 := First_Formal (Candidate_Renaming);
3864 F2 := First_Formal (New_S);
3865 T1 := First_Subtype (Etype (F1));
3866 while Present (F1) and then Present (F2) loop
3871 if Present (F1) and then Present (Default_Value (F1)) then
3872 if Present (Next_Formal (F1)) then
3874 ("\missing specification for & and other formals with "
3875 & "defaults", Spec, F1);
3877 Error_Msg_NE ("\missing specification for &", Spec, F1);
3881 if Nkind (Nam) = N_Operator_Symbol
3882 and then From_Default (N)
3884 Error_Msg_Node_2 := T1;
3886 ("default & on & is not directly visible", Nam, Nam);
3892 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
3893 -- controlling access parameters are known non-null for the renamed
3894 -- subprogram. Test also applies to a subprogram instantiation that
3895 -- is dispatching. Test is skipped if some previous error was detected
3896 -- that set Old_S to Any_Id.
3898 if Ada_Version >= Ada_2005
3899 and then Old_S /= Any_Id
3900 and then not Is_Dispatching_Operation (Old_S)
3901 and then Is_Dispatching_Operation (New_S)
3908 Old_F := First_Formal (Old_S);
3909 New_F := First_Formal (New_S);
3910 while Present (Old_F) loop
3911 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
3912 and then Is_Controlling_Formal (New_F)
3913 and then not Can_Never_Be_Null (Old_F)
3915 Error_Msg_N ("access parameter is controlling,", New_F);
3917 ("\corresponding parameter of& must be explicitly null "
3918 & "excluding", New_F, Old_S);
3921 Next_Formal (Old_F);
3922 Next_Formal (New_F);
3927 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
3928 -- is to warn if an operator is being renamed as a different operator.
3929 -- If the operator is predefined, examine the kind of the entity, not
3930 -- the abbreviated declaration in Standard.
3932 if Comes_From_Source (N)
3933 and then Present (Old_S)
3934 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3935 or else Ekind (Old_S) = E_Operator)
3936 and then Nkind (New_S) = N_Defining_Operator_Symbol
3937 and then Chars (Old_S) /= Chars (New_S)
3940 ("& is being renamed as a different operator??", N, Old_S);
3943 -- Check for renaming of obsolescent subprogram
3945 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3947 -- Another warning or some utility: if the new subprogram as the same
3948 -- name as the old one, the old one is not hidden by an outer homograph,
3949 -- the new one is not a public symbol, and the old one is otherwise
3950 -- directly visible, the renaming is superfluous.
3952 if Chars (Old_S) = Chars (New_S)
3953 and then Comes_From_Source (N)
3954 and then Scope (Old_S) /= Standard_Standard
3955 and then Warn_On_Redundant_Constructs
3956 and then (Is_Immediately_Visible (Old_S)
3957 or else Is_Potentially_Use_Visible (Old_S))
3958 and then Is_Overloadable (Current_Scope)
3959 and then Chars (Current_Scope) /= Chars (Old_S)
3962 ("redundant renaming, entity is directly visible?r?", Name (N));
3965 -- Implementation-defined aspect specifications can appear in a renaming
3966 -- declaration, but not language-defined ones. The call to procedure
3967 -- Analyze_Aspect_Specifications will take care of this error check.
3969 if Has_Aspects (N) then
3970 Analyze_Aspect_Specifications (N, New_S);
3976 and then Has_Yield_Aspect (Formal_Spec)
3977 and then not Has_Yield_Aspect (Old_S)
3979 Error_Msg_Name_1 := Name_Yield;
3981 ("actual subprogram& must have aspect% to match formal", Name (N));
3984 Ada_Version := Save_AV;
3985 Ada_Version_Pragma := Save_AVP;
3986 Ada_Version_Explicit := Save_AV_Exp;
3988 -- Check if we are looking at an Ada 2012 defaulted formal subprogram
3989 -- and mark any use_package_clauses that affect the visibility of the
3990 -- implicit generic actual.
3992 -- Also, we may be looking at an internal renaming of a user-defined
3993 -- subprogram created for a generic formal subprogram association,
3994 -- which will also have to be marked here. This can occur when the
3995 -- corresponding formal subprogram contains references to other generic
3998 if Is_Generic_Actual_Subprogram (New_S)
3999 and then (Is_Intrinsic_Subprogram (New_S)
4000 or else From_Default (N)
4001 or else Nkind (N) = N_Subprogram_Renaming_Declaration)
4003 Mark_Use_Clauses (New_S);
4005 -- Handle overloaded subprograms
4007 if Present (Alias (New_S)) then
4008 Mark_Use_Clauses (Alias (New_S));
4011 end Analyze_Subprogram_Renaming;
4013 -------------------------
4014 -- Analyze_Use_Package --
4015 -------------------------
4017 -- Resolve the package names in the use clause, and make all the visible
4018 -- entities defined in the package potentially use-visible. If the package
4019 -- is already in use from a previous use clause, its visible entities are
4020 -- already use-visible. In that case, mark the occurrence as a redundant
4021 -- use. If the package is an open scope, i.e. if the use clause occurs
4022 -- within the package itself, ignore it.
4024 procedure Analyze_Use_Package (N : Node_Id; Chain : Boolean := True) is
4025 procedure Analyze_Package_Name (Clause : Node_Id);
4026 -- Perform analysis on a package name from a use_package_clause
4028 procedure Analyze_Package_Name_List (Head_Clause : Node_Id);
4029 -- Similar to Analyze_Package_Name but iterates over all the names
4032 --------------------------
4033 -- Analyze_Package_Name --
4034 --------------------------
4036 procedure Analyze_Package_Name (Clause : Node_Id) is
4037 Pack : constant Node_Id := Name (Clause);
4041 pragma Assert (Nkind (Clause) = N_Use_Package_Clause);
4044 -- Verify that the package standard is not directly named in a
4045 -- use_package_clause.
4047 if Nkind (Parent (Clause)) = N_Compilation_Unit
4048 and then Nkind (Pack) = N_Expanded_Name
4050 Pref := Prefix (Pack);
4052 while Nkind (Pref) = N_Expanded_Name loop
4053 Pref := Prefix (Pref);
4056 if Entity (Pref) = Standard_Standard then
4058 ("predefined package Standard cannot appear in a context "
4062 end Analyze_Package_Name;
4064 -------------------------------
4065 -- Analyze_Package_Name_List --
4066 -------------------------------
4068 procedure Analyze_Package_Name_List (Head_Clause : Node_Id) is
4072 -- Due to the way source use clauses are split during parsing we are
4073 -- forced to simply iterate through all entities in scope until the
4074 -- clause representing the last name in the list is found.
4076 Curr := Head_Clause;
4077 while Present (Curr) loop
4078 Analyze_Package_Name (Curr);
4080 -- Stop iterating over the names in the use clause when we are at
4083 exit when not More_Ids (Curr) and then Prev_Ids (Curr);
4086 end Analyze_Package_Name_List;
4092 -- Start of processing for Analyze_Use_Package
4095 Set_Hidden_By_Use_Clause (N, No_Elist);
4097 -- Use clause not allowed in a spec of a predefined package declaration
4098 -- except that packages whose file name starts a-n are OK (these are
4099 -- children of Ada.Numerics, which are never loaded by Rtsfind).
4101 if Is_Predefined_Unit (Current_Sem_Unit)
4102 and then Get_Name_String
4103 (Unit_File_Name (Current_Sem_Unit)) (1 .. 3) /= "a-n"
4104 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
4105 N_Package_Declaration
4107 Error_Msg_N ("use clause not allowed in predefined spec", N);
4110 -- Loop through all package names from the original use clause in
4111 -- order to analyze referenced packages. A use_package_clause with only
4112 -- one name does not have More_Ids or Prev_Ids set, while a clause with
4113 -- More_Ids only starts the chain produced by the parser.
4115 if not More_Ids (N) and then not Prev_Ids (N) then
4116 Analyze_Package_Name (N);
4118 elsif More_Ids (N) and then not Prev_Ids (N) then
4119 Analyze_Package_Name_List (N);
4122 if not Is_Entity_Name (Name (N)) then
4123 Error_Msg_N ("& is not a package", Name (N));
4129 Chain_Use_Clause (N);
4132 Pack := Entity (Name (N));
4134 -- There are many cases where scopes are manipulated during analysis, so
4135 -- check that Pack's current use clause has not already been chained
4136 -- before setting its previous use clause.
4138 if Ekind (Pack) = E_Package
4139 and then Present (Current_Use_Clause (Pack))
4140 and then Current_Use_Clause (Pack) /= N
4141 and then No (Prev_Use_Clause (N))
4142 and then Prev_Use_Clause (Current_Use_Clause (Pack)) /= N
4144 Set_Prev_Use_Clause (N, Current_Use_Clause (Pack));
4147 -- Mark all entities as potentially use visible
4149 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
4150 if Ekind (Pack) = E_Generic_Package then
4151 Error_Msg_N -- CODEFIX
4152 ("a generic package is not allowed in a use clause", Name (N));
4154 elsif Is_Generic_Subprogram (Pack) then
4155 Error_Msg_N -- CODEFIX
4156 ("a generic subprogram is not allowed in a use clause",
4159 elsif Is_Subprogram (Pack) then
4160 Error_Msg_N -- CODEFIX
4161 ("a subprogram is not allowed in a use clause", Name (N));
4164 Error_Msg_N ("& is not allowed in a use clause", Name (N));
4168 if Nkind (Parent (N)) = N_Compilation_Unit then
4169 Check_In_Previous_With_Clause (N, Name (N));
4172 Use_One_Package (N, Name (N));
4175 Mark_Ghost_Clause (N);
4176 end Analyze_Use_Package;
4178 ----------------------
4179 -- Analyze_Use_Type --
4180 ----------------------
4182 procedure Analyze_Use_Type (N : Node_Id; Chain : Boolean := True) is
4187 Set_Hidden_By_Use_Clause (N, No_Elist);
4189 -- Chain clause to list of use clauses in current scope when flagged
4192 Chain_Use_Clause (N);
4195 -- Obtain the base type of the type denoted within the use_type_clause's
4198 Id := Subtype_Mark (N);
4200 E := Base_Type (Entity (Id));
4202 -- There are many cases where a use_type_clause may be reanalyzed due to
4203 -- manipulation of the scope stack so we much guard against those cases
4204 -- here, otherwise, we must add the new use_type_clause to the previous
4205 -- use_type_clause chain in order to mark redundant use_type_clauses as
4206 -- used. When the redundant use-type clauses appear in a parent unit and
4207 -- a child unit we must prevent a circularity in the chain that would
4208 -- otherwise result from the separate steps of analysis and installation
4209 -- of the parent context.
4211 if Present (Current_Use_Clause (E))
4212 and then Current_Use_Clause (E) /= N
4213 and then Prev_Use_Clause (Current_Use_Clause (E)) /= N
4214 and then No (Prev_Use_Clause (N))
4216 Set_Prev_Use_Clause (N, Current_Use_Clause (E));
4219 -- If the Used_Operations list is already initialized, the clause has
4220 -- been analyzed previously, and it is being reinstalled, for example
4221 -- when the clause appears in a package spec and we are compiling the
4222 -- corresponding package body. In that case, make the entities on the
4223 -- existing list use_visible, and mark the corresponding types In_Use.
4225 if Present (Used_Operations (N)) then
4230 Use_One_Type (Subtype_Mark (N), Installed => True);
4232 Elmt := First_Elmt (Used_Operations (N));
4233 while Present (Elmt) loop
4234 Set_Is_Potentially_Use_Visible (Node (Elmt));
4242 -- Otherwise, create new list and attach to it the operations that are
4243 -- made use-visible by the clause.
4245 Set_Used_Operations (N, New_Elmt_List);
4248 if E /= Any_Type then
4251 if Nkind (Parent (N)) = N_Compilation_Unit then
4252 if Nkind (Id) = N_Identifier then
4253 Error_Msg_N ("type is not directly visible", Id);
4255 elsif Is_Child_Unit (Scope (E))
4256 and then Scope (E) /= System_Aux_Id
4258 Check_In_Previous_With_Clause (N, Prefix (Id));
4263 -- If the use_type_clause appears in a compilation unit context,
4264 -- check whether it comes from a unit that may appear in a
4265 -- limited_with_clause, for a better error message.
4267 if Nkind (Parent (N)) = N_Compilation_Unit
4268 and then Nkind (Id) /= N_Identifier
4274 function Mentioned (Nam : Node_Id) return Boolean;
4275 -- Check whether the prefix of expanded name for the type
4276 -- appears in the prefix of some limited_with_clause.
4282 function Mentioned (Nam : Node_Id) return Boolean is
4284 return Nkind (Name (Item)) = N_Selected_Component
4285 and then Chars (Prefix (Name (Item))) = Chars (Nam);
4289 Pref := Prefix (Id);
4290 Item := First (Context_Items (Parent (N)));
4291 while Present (Item) and then Item /= N loop
4292 if Nkind (Item) = N_With_Clause
4293 and then Limited_Present (Item)
4294 and then Mentioned (Pref)
4297 (Get_Msg_Id, "premature usage of incomplete type");
4306 Mark_Ghost_Clause (N);
4307 end Analyze_Use_Type;
4309 ------------------------
4310 -- Attribute_Renaming --
4311 ------------------------
4313 procedure Attribute_Renaming (N : Node_Id) is
4314 Loc : constant Source_Ptr := Sloc (N);
4315 Nam : constant Node_Id := Name (N);
4316 Spec : constant Node_Id := Specification (N);
4317 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
4318 Aname : constant Name_Id := Attribute_Name (Nam);
4320 Form_Num : Nat := 0;
4321 Expr_List : List_Id := No_List;
4323 Attr_Node : Node_Id;
4324 Body_Node : Node_Id;
4325 Param_Spec : Node_Id;
4328 Generate_Definition (New_S);
4330 -- This procedure is called in the context of subprogram renaming, and
4331 -- thus the attribute must be one that is a subprogram. All of those
4332 -- have at least one formal parameter, with the exceptions of the GNAT
4333 -- attribute 'Img, which GNAT treats as renameable.
4335 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
4336 if Aname /= Name_Img then
4338 ("subprogram renaming an attribute must have formals", N);
4343 Param_Spec := First (Parameter_Specifications (Spec));
4344 while Present (Param_Spec) loop
4345 Form_Num := Form_Num + 1;
4347 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4348 Find_Type (Parameter_Type (Param_Spec));
4350 -- The profile of the new entity denotes the base type (s) of
4351 -- the types given in the specification. For access parameters
4352 -- there are no subtypes involved.
4354 Rewrite (Parameter_Type (Param_Spec),
4356 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
4359 if No (Expr_List) then
4360 Expr_List := New_List;
4363 Append_To (Expr_List,
4364 Make_Identifier (Loc,
4365 Chars => Chars (Defining_Identifier (Param_Spec))));
4367 -- The expressions in the attribute reference are not freeze
4368 -- points. Neither is the attribute as a whole, see below.
4370 Set_Must_Not_Freeze (Last (Expr_List));
4375 -- Immediate error if too many formals. Other mismatches in number or
4376 -- types of parameters are detected when we analyze the body of the
4377 -- subprogram that we construct.
4379 if Form_Num > 2 then
4380 Error_Msg_N ("too many formals for attribute", N);
4382 -- Error if the attribute reference has expressions that look like
4383 -- formal parameters.
4385 elsif Present (Expressions (Nam)) then
4386 Error_Msg_N ("illegal expressions in attribute reference", Nam);
4388 elsif Aname in Name_Compose | Name_Exponent | Name_Leading_Part |
4389 Name_Pos | Name_Round | Name_Scaling |
4392 if Nkind (N) = N_Subprogram_Renaming_Declaration
4393 and then Present (Corresponding_Formal_Spec (N))
4396 ("generic actual cannot be attribute involving universal type",
4400 ("attribute involving a universal type cannot be renamed",
4405 -- Rewrite attribute node to have a list of expressions corresponding to
4406 -- the subprogram formals. A renaming declaration is not a freeze point,
4407 -- and the analysis of the attribute reference should not freeze the
4408 -- type of the prefix. We use the original node in the renaming so that
4409 -- its source location is preserved, and checks on stream attributes are
4410 -- properly applied.
4412 Attr_Node := Relocate_Node (Nam);
4413 Set_Expressions (Attr_Node, Expr_List);
4415 Set_Must_Not_Freeze (Attr_Node);
4416 Set_Must_Not_Freeze (Prefix (Nam));
4418 -- Case of renaming a function
4420 if Nkind (Spec) = N_Function_Specification then
4421 if Is_Procedure_Attribute_Name (Aname) then
4422 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
4426 Find_Type (Result_Definition (Spec));
4427 Rewrite (Result_Definition (Spec),
4429 (Base_Type (Entity (Result_Definition (Spec))), Loc));
4432 Make_Subprogram_Body (Loc,
4433 Specification => Spec,
4434 Declarations => New_List,
4435 Handled_Statement_Sequence =>
4436 Make_Handled_Sequence_Of_Statements (Loc,
4437 Statements => New_List (
4438 Make_Simple_Return_Statement (Loc,
4439 Expression => Attr_Node))));
4441 -- Case of renaming a procedure
4444 if not Is_Procedure_Attribute_Name (Aname) then
4445 Error_Msg_N ("attribute can only be renamed as function", Nam);
4450 Make_Subprogram_Body (Loc,
4451 Specification => Spec,
4452 Declarations => New_List,
4453 Handled_Statement_Sequence =>
4454 Make_Handled_Sequence_Of_Statements (Loc,
4455 Statements => New_List (Attr_Node)));
4458 -- Signal the ABE mechanism that the generated subprogram body has not
4459 -- ABE ramifications.
4461 Set_Was_Attribute_Reference (Body_Node);
4463 -- In case of tagged types we add the body of the generated function to
4464 -- the freezing actions of the type (because in the general case such
4465 -- type is still not frozen). We exclude from this processing generic
4466 -- formal subprograms found in instantiations.
4468 -- We must exclude restricted run-time libraries because
4469 -- entity AST_Handler is defined in package System.Aux_Dec which is not
4470 -- available in those platforms. Note that we cannot use the function
4471 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
4472 -- the ZFP run-time library is not defined as a profile, and we do not
4473 -- want to deal with AST_Handler in ZFP mode.
4475 if not Configurable_Run_Time_Mode
4476 and then not Present (Corresponding_Formal_Spec (N))
4477 and then not Is_RTE (Etype (Nam), RE_AST_Handler)
4480 P : constant Node_Id := Prefix (Nam);
4483 -- The prefix of 'Img is an object that is evaluated for each call
4484 -- of the function that renames it.
4486 if Aname = Name_Img then
4487 Preanalyze_And_Resolve (P);
4489 -- For all other attribute renamings, the prefix is a subtype
4495 -- If the target type is not yet frozen, add the body to the
4496 -- actions to be elaborated at freeze time.
4498 if Is_Tagged_Type (Etype (P))
4499 and then In_Open_Scopes (Scope (Etype (P)))
4501 Ensure_Freeze_Node (Etype (P));
4502 Append_Freeze_Action (Etype (P), Body_Node);
4504 Rewrite (N, Body_Node);
4506 Set_Etype (New_S, Base_Type (Etype (New_S)));
4510 -- Generic formal subprograms or AST_Handler renaming
4513 Rewrite (N, Body_Node);
4515 Set_Etype (New_S, Base_Type (Etype (New_S)));
4518 if Is_Compilation_Unit (New_S) then
4520 ("a library unit can only rename another library unit", N);
4523 -- We suppress elaboration warnings for the resulting entity, since
4524 -- clearly they are not needed, and more particularly, in the case
4525 -- of a generic formal subprogram, the resulting entity can appear
4526 -- after the instantiation itself, and thus look like a bogus case
4527 -- of access before elaboration.
4529 if Legacy_Elaboration_Checks then
4530 Set_Suppress_Elaboration_Warnings (New_S);
4532 end Attribute_Renaming;
4534 ----------------------
4535 -- Chain_Use_Clause --
4536 ----------------------
4538 procedure Chain_Use_Clause (N : Node_Id) is
4539 Level : Int := Scope_Stack.Last;
4545 if not Is_Compilation_Unit (Current_Scope)
4546 or else not Is_Child_Unit (Current_Scope)
4550 -- Common case for compilation unit
4552 elsif Defining_Entity (Parent (N)) = Current_Scope then
4556 -- If declaration appears in some other scope, it must be in some
4557 -- parent unit when compiling a child.
4559 Pack := Defining_Entity (Parent (N));
4561 if not In_Open_Scopes (Pack) then
4564 -- If the use clause appears in an ancestor and we are in the
4565 -- private part of the immediate parent, the use clauses are
4566 -- already installed.
4568 elsif Pack /= Scope (Current_Scope)
4569 and then In_Private_Part (Scope (Current_Scope))
4574 -- Find entry for parent unit in scope stack
4576 while Scope_Stack.Table (Level).Entity /= Pack loop
4582 Set_Next_Use_Clause (N,
4583 Scope_Stack.Table (Level).First_Use_Clause);
4584 Scope_Stack.Table (Level).First_Use_Clause := N;
4585 end Chain_Use_Clause;
4587 ---------------------------
4588 -- Check_Frozen_Renaming --
4589 ---------------------------
4591 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
4596 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
4599 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
4601 if Is_Entity_Name (Name (N)) then
4602 Old_S := Entity (Name (N));
4604 if not Is_Frozen (Old_S)
4605 and then Operating_Mode /= Check_Semantics
4607 Append_Freeze_Action (Old_S, B_Node);
4609 Insert_After (N, B_Node);
4613 if Is_Intrinsic_Subprogram (Old_S)
4614 and then not In_Instance
4615 and then not Relaxed_RM_Semantics
4618 ("subprogram used in renaming_as_body cannot be intrinsic",
4623 Insert_After (N, B_Node);
4627 end Check_Frozen_Renaming;
4629 -------------------------------
4630 -- Set_Entity_Or_Discriminal --
4631 -------------------------------
4633 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4637 -- If the entity is not a discriminant, or else expansion is disabled,
4638 -- simply set the entity.
4640 if not In_Spec_Expression
4641 or else Ekind (E) /= E_Discriminant
4642 or else Inside_A_Generic
4644 Set_Entity_With_Checks (N, E);
4646 -- The replacement of a discriminant by the corresponding discriminal
4647 -- is not done for a task discriminant that appears in a default
4648 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4649 -- for details on their handling.
4651 elsif Is_Concurrent_Type (Scope (E)) then
4654 and then Nkind (P) not in
4655 N_Parameter_Specification | N_Component_Declaration
4661 and then Nkind (P) = N_Parameter_Specification
4666 Set_Entity (N, Discriminal (E));
4669 -- Otherwise, this is a discriminant in a context in which
4670 -- it is a reference to the corresponding parameter of the
4671 -- init proc for the enclosing type.
4674 Set_Entity (N, Discriminal (E));
4676 end Set_Entity_Or_Discriminal;
4678 -----------------------------------
4679 -- Check_In_Previous_With_Clause --
4680 -----------------------------------
4682 procedure Check_In_Previous_With_Clause (N, Nam : Node_Id) is
4683 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4688 Item := First (Context_Items (Parent (N)));
4689 while Present (Item) and then Item /= N loop
4690 if Nkind (Item) = N_With_Clause
4692 -- Protect the frontend against previous critical errors
4694 and then Nkind (Name (Item)) /= N_Selected_Component
4695 and then Entity (Name (Item)) = Pack
4699 -- Find root library unit in with_clause
4701 while Nkind (Par) = N_Expanded_Name loop
4702 Par := Prefix (Par);
4705 if Is_Child_Unit (Entity (Original_Node (Par))) then
4706 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4715 -- On exit, package is not mentioned in a previous with_clause.
4716 -- Check if its prefix is.
4718 if Nkind (Nam) = N_Expanded_Name then
4719 Check_In_Previous_With_Clause (N, Prefix (Nam));
4721 elsif Pack /= Any_Id then
4722 Error_Msg_NE ("& is not visible", Nam, Pack);
4724 end Check_In_Previous_With_Clause;
4726 ---------------------------------
4727 -- Check_Library_Unit_Renaming --
4728 ---------------------------------
4730 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4734 if Nkind (Parent (N)) /= N_Compilation_Unit then
4737 -- Check for library unit. Note that we used to check for the scope
4738 -- being Standard here, but that was wrong for Standard itself.
4740 elsif not Is_Compilation_Unit (Old_E)
4741 and then not Is_Child_Unit (Old_E)
4743 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4745 -- Entities defined in Standard (operators and boolean literals) cannot
4746 -- be renamed as library units.
4748 elsif Scope (Old_E) = Standard_Standard
4749 and then Sloc (Old_E) = Standard_Location
4751 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4753 elsif Present (Parent_Spec (N))
4754 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
4755 and then not Is_Child_Unit (Old_E)
4758 ("renamed unit must be a child unit of generic parent", Name (N));
4760 elsif Nkind (N) in N_Generic_Renaming_Declaration
4761 and then Nkind (Name (N)) = N_Expanded_Name
4762 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
4763 and then Is_Generic_Unit (Old_E)
4766 ("renamed generic unit must be a library unit", Name (N));
4768 elsif Is_Package_Or_Generic_Package (Old_E) then
4770 -- Inherit categorization flags
4772 New_E := Defining_Entity (N);
4773 Set_Is_Pure (New_E, Is_Pure (Old_E));
4774 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
4775 Set_Is_Remote_Call_Interface (New_E,
4776 Is_Remote_Call_Interface (Old_E));
4777 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
4778 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
4780 end Check_Library_Unit_Renaming;
4782 ------------------------
4783 -- Enclosing_Instance --
4784 ------------------------
4786 function Enclosing_Instance return Entity_Id is
4790 if not Is_Generic_Instance (Current_Scope) then
4794 S := Scope (Current_Scope);
4795 while S /= Standard_Standard loop
4796 if Is_Generic_Instance (S) then
4804 end Enclosing_Instance;
4810 procedure End_Scope is
4816 Id := First_Entity (Current_Scope);
4817 while Present (Id) loop
4818 -- An entity in the current scope is not necessarily the first one
4819 -- on its homonym chain. Find its predecessor if any,
4820 -- If it is an internal entity, it will not be in the visibility
4821 -- chain altogether, and there is nothing to unchain.
4823 if Id /= Current_Entity (Id) then
4824 Prev := Current_Entity (Id);
4825 while Present (Prev)
4826 and then Present (Homonym (Prev))
4827 and then Homonym (Prev) /= Id
4829 Prev := Homonym (Prev);
4832 -- Skip to end of loop if Id is not in the visibility chain
4834 if No (Prev) or else Homonym (Prev) /= Id then
4842 Set_Is_Immediately_Visible (Id, False);
4844 Outer := Homonym (Id);
4845 while Present (Outer) and then Scope (Outer) = Current_Scope loop
4846 Outer := Homonym (Outer);
4849 -- Reset homonym link of other entities, but do not modify link
4850 -- between entities in current scope, so that the back-end can have
4851 -- a proper count of local overloadings.
4854 Set_Name_Entity_Id (Chars (Id), Outer);
4856 elsif Scope (Prev) /= Scope (Id) then
4857 Set_Homonym (Prev, Outer);
4864 -- If the scope generated freeze actions, place them before the
4865 -- current declaration and analyze them. Type declarations and
4866 -- the bodies of initialization procedures can generate such nodes.
4867 -- We follow the parent chain until we reach a list node, which is
4868 -- the enclosing list of declarations. If the list appears within
4869 -- a protected definition, move freeze nodes outside the protected
4873 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
4877 L : constant List_Id := Scope_Stack.Table
4878 (Scope_Stack.Last).Pending_Freeze_Actions;
4881 if Is_Itype (Current_Scope) then
4882 Decl := Associated_Node_For_Itype (Current_Scope);
4884 Decl := Parent (Current_Scope);
4889 while not Is_List_Member (Decl)
4890 or else Nkind (Parent (Decl)) in N_Protected_Definition
4893 Decl := Parent (Decl);
4896 Insert_List_Before_And_Analyze (Decl, L);
4904 ---------------------
4905 -- End_Use_Clauses --
4906 ---------------------
4908 procedure End_Use_Clauses (Clause : Node_Id) is
4912 -- Remove use_type_clauses first, because they affect the visibility of
4913 -- operators in subsequent used packages.
4916 while Present (U) loop
4917 if Nkind (U) = N_Use_Type_Clause then
4921 Next_Use_Clause (U);
4925 while Present (U) loop
4926 if Nkind (U) = N_Use_Package_Clause then
4927 End_Use_Package (U);
4930 Next_Use_Clause (U);
4932 end End_Use_Clauses;
4934 ---------------------
4935 -- End_Use_Package --
4936 ---------------------
4938 procedure End_Use_Package (N : Node_Id) is
4940 Pack_Name : Node_Id;
4944 function Is_Primitive_Operator_In_Use
4946 F : Entity_Id) return Boolean;
4947 -- Check whether Op is a primitive operator of a use-visible type
4949 ----------------------------------
4950 -- Is_Primitive_Operator_In_Use --
4951 ----------------------------------
4953 function Is_Primitive_Operator_In_Use
4955 F : Entity_Id) return Boolean
4957 T : constant Entity_Id := Base_Type (Etype (F));
4959 return In_Use (T) and then Scope (T) = Scope (Op);
4960 end Is_Primitive_Operator_In_Use;
4962 -- Start of processing for End_Use_Package
4965 Pack_Name := Name (N);
4967 -- Test that Pack_Name actually denotes a package before processing
4969 if Is_Entity_Name (Pack_Name)
4970 and then Ekind (Entity (Pack_Name)) = E_Package
4972 Pack := Entity (Pack_Name);
4974 if In_Open_Scopes (Pack) then
4977 elsif not Redundant_Use (Pack_Name) then
4978 Set_In_Use (Pack, False);
4979 Set_Current_Use_Clause (Pack, Empty);
4981 Id := First_Entity (Pack);
4982 while Present (Id) loop
4984 -- Preserve use-visibility of operators that are primitive
4985 -- operators of a type that is use-visible through an active
4988 if Nkind (Id) = N_Defining_Operator_Symbol
4990 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
4992 (Present (Next_Formal (First_Formal (Id)))
4994 Is_Primitive_Operator_In_Use
4995 (Id, Next_Formal (First_Formal (Id)))))
4999 Set_Is_Potentially_Use_Visible (Id, False);
5002 if Is_Private_Type (Id)
5003 and then Present (Full_View (Id))
5005 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
5011 if Present (Renamed_Object (Pack)) then
5012 Set_In_Use (Renamed_Object (Pack), False);
5013 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
5016 if Chars (Pack) = Name_System
5017 and then Scope (Pack) = Standard_Standard
5018 and then Present_System_Aux
5020 Id := First_Entity (System_Aux_Id);
5021 while Present (Id) loop
5022 Set_Is_Potentially_Use_Visible (Id, False);
5024 if Is_Private_Type (Id)
5025 and then Present (Full_View (Id))
5027 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
5033 Set_In_Use (System_Aux_Id, False);
5036 Set_Redundant_Use (Pack_Name, False);
5040 if Present (Hidden_By_Use_Clause (N)) then
5041 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
5042 while Present (Elmt) loop
5044 E : constant Entity_Id := Node (Elmt);
5047 -- Reset either Use_Visibility or Direct_Visibility, depending
5048 -- on how the entity was hidden by the use clause.
5050 if In_Use (Scope (E))
5051 and then Used_As_Generic_Actual (Scope (E))
5053 Set_Is_Potentially_Use_Visible (Node (Elmt));
5055 Set_Is_Immediately_Visible (Node (Elmt));
5062 Set_Hidden_By_Use_Clause (N, No_Elist);
5064 end End_Use_Package;
5070 procedure End_Use_Type (N : Node_Id) is
5075 -- Start of processing for End_Use_Type
5078 Id := Subtype_Mark (N);
5080 -- A call to Rtsfind may occur while analyzing a use_type_clause, in
5081 -- which case the type marks are not resolved yet, so guard against that
5084 if Is_Entity_Name (Id) and then Present (Entity (Id)) then
5087 if T = Any_Type or else From_Limited_With (T) then
5090 -- Note that the use_type_clause may mention a subtype of the type
5091 -- whose primitive operations have been made visible. Here as
5092 -- elsewhere, it is the base type that matters for visibility.
5094 elsif In_Open_Scopes (Scope (Base_Type (T))) then
5097 elsif not Redundant_Use (Id) then
5098 Set_In_Use (T, False);
5099 Set_In_Use (Base_Type (T), False);
5100 Set_Current_Use_Clause (T, Empty);
5101 Set_Current_Use_Clause (Base_Type (T), Empty);
5103 -- See Use_One_Type for the rationale. This is a bit on the naive
5104 -- side, but should be good enough in practice.
5106 if Is_Tagged_Type (T) then
5107 Set_In_Use (Class_Wide_Type (T), False);
5112 if Is_Empty_Elmt_List (Used_Operations (N)) then
5116 Elmt := First_Elmt (Used_Operations (N));
5117 while Present (Elmt) loop
5118 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
5124 --------------------
5125 -- Entity_Of_Unit --
5126 --------------------
5128 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
5130 if Nkind (U) = N_Package_Instantiation and then Analyzed (U) then
5131 return Defining_Entity (Instance_Spec (U));
5133 return Defining_Entity (U);
5137 ----------------------
5138 -- Find_Direct_Name --
5139 ----------------------
5141 procedure Find_Direct_Name (N : Node_Id) is
5146 Homonyms : Entity_Id;
5147 -- Saves start of homonym chain
5149 Inst : Entity_Id := Empty;
5150 -- Enclosing instance, if any
5152 Nvis_Entity : Boolean;
5153 -- Set True to indicate that there is at least one entity on the homonym
5154 -- chain which, while not visible, is visible enough from the user point
5155 -- of view to warrant an error message of "not visible" rather than
5158 Nvis_Is_Private_Subprg : Boolean := False;
5159 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
5160 -- effect concerning library subprograms has been detected. Used to
5161 -- generate the precise error message.
5163 function From_Actual_Package (E : Entity_Id) return Boolean;
5164 -- Returns true if the entity is an actual for a package that is itself
5165 -- an actual for a formal package of the current instance. Such an
5166 -- entity requires special handling because it may be use-visible but
5167 -- hides directly visible entities defined outside the instance, because
5168 -- the corresponding formal did so in the generic.
5170 function Is_Actual_Parameter return Boolean;
5171 -- This function checks if the node N is an identifier that is an actual
5172 -- parameter of a procedure call. If so it returns True, otherwise it
5173 -- return False. The reason for this check is that at this stage we do
5174 -- not know what procedure is being called if the procedure might be
5175 -- overloaded, so it is premature to go setting referenced flags or
5176 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
5177 -- for that processing.
5178 -- Note: there is a similar routine Sem_Util.Is_Actual_Parameter, but
5179 -- it works for both function and procedure calls, while here we are
5180 -- only concerned with procedure calls (and with entry calls as well,
5181 -- but they are parsed as procedure calls and only later rewritten to
5184 function Known_But_Invisible (E : Entity_Id) return Boolean;
5185 -- This function determines whether a reference to the entity E, which
5186 -- is not visible, can reasonably be considered to be known to the
5187 -- writer of the reference. This is a heuristic test, used only for
5188 -- the purposes of figuring out whether we prefer to complain that an
5189 -- entity is undefined or invisible (and identify the declaration of
5190 -- the invisible entity in the latter case). The point here is that we
5191 -- don't want to complain that something is invisible and then point to
5192 -- something entirely mysterious to the writer.
5194 procedure Nvis_Messages;
5195 -- Called if there are no visible entries for N, but there is at least
5196 -- one non-directly visible, or hidden declaration. This procedure
5197 -- outputs an appropriate set of error messages.
5199 procedure Undefined (Nvis : Boolean);
5200 -- This function is called if the current node has no corresponding
5201 -- visible entity or entities. The value set in Msg indicates whether
5202 -- an error message was generated (multiple error messages for the
5203 -- same variable are generally suppressed, see body for details).
5204 -- Msg is True if an error message was generated, False if not. This
5205 -- value is used by the caller to determine whether or not to output
5206 -- additional messages where appropriate. The parameter is set False
5207 -- to get the message "X is undefined", and True to get the message
5208 -- "X is not visible".
5210 -------------------------
5211 -- From_Actual_Package --
5212 -------------------------
5214 function From_Actual_Package (E : Entity_Id) return Boolean is
5215 Scop : constant Entity_Id := Scope (E);
5216 -- Declared scope of candidate entity
5218 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
5219 -- Recursive function that does the work and examines actuals of
5220 -- actual packages of current instance.
5222 ------------------------
5223 -- Declared_In_Actual --
5224 ------------------------
5226 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
5230 if No (Associated_Formal_Package (Pack)) then
5234 Act := First_Entity (Pack);
5235 while Present (Act) loop
5236 if Renamed_Object (Pack) = Scop then
5239 -- Check for end of list of actuals
5241 elsif Ekind (Act) = E_Package
5242 and then Renamed_Object (Act) = Pack
5246 elsif Ekind (Act) = E_Package
5247 and then Declared_In_Actual (Act)
5257 end Declared_In_Actual;
5263 -- Start of processing for From_Actual_Package
5266 if not In_Instance then
5270 Inst := Current_Scope;
5271 while Present (Inst)
5272 and then Ekind (Inst) /= E_Package
5273 and then not Is_Generic_Instance (Inst)
5275 Inst := Scope (Inst);
5282 Act := First_Entity (Inst);
5283 while Present (Act) loop
5284 if Ekind (Act) = E_Package
5285 and then Declared_In_Actual (Act)
5295 end From_Actual_Package;
5297 -------------------------
5298 -- Is_Actual_Parameter --
5299 -------------------------
5301 function Is_Actual_Parameter return Boolean is
5303 if Nkind (N) = N_Identifier then
5304 case Nkind (Parent (N)) is
5305 when N_Procedure_Call_Statement =>
5306 return Is_List_Member (N)
5307 and then List_Containing (N) =
5308 Parameter_Associations (Parent (N));
5310 when N_Parameter_Association =>
5311 return N = Explicit_Actual_Parameter (Parent (N))
5312 and then Nkind (Parent (Parent (N))) =
5313 N_Procedure_Call_Statement;
5321 end Is_Actual_Parameter;
5323 -------------------------
5324 -- Known_But_Invisible --
5325 -------------------------
5327 function Known_But_Invisible (E : Entity_Id) return Boolean is
5328 Fname : File_Name_Type;
5331 -- Entities in Standard are always considered to be known
5333 if Sloc (E) <= Standard_Location then
5336 -- An entity that does not come from source is always considered
5337 -- to be unknown, since it is an artifact of code expansion.
5339 elsif not Comes_From_Source (E) then
5343 -- Here we have an entity that is not from package Standard, and
5344 -- which comes from Source. See if it comes from an internal file.
5346 Fname := Unit_File_Name (Get_Source_Unit (E));
5348 -- Case of from internal file
5350 if In_Internal_Unit (E) then
5352 -- Private part entities in internal files are never considered
5353 -- to be known to the writer of normal application code.
5355 if Is_Hidden (E) then
5359 -- Entities from System packages other than System and
5360 -- System.Storage_Elements are not considered to be known.
5361 -- System.Auxxxx files are also considered known to the user.
5363 -- Should refine this at some point to generally distinguish
5364 -- between known and unknown internal files ???
5366 Get_Name_String (Fname);
5371 Name_Buffer (1 .. 2) /= "s-"
5373 Name_Buffer (3 .. 8) = "stoele"
5375 Name_Buffer (3 .. 5) = "aux";
5377 -- If not an internal file, then entity is definitely known, even if
5378 -- it is in a private part (the message generated will note that it
5379 -- is in a private part).
5384 end Known_But_Invisible;
5390 procedure Nvis_Messages is
5391 Comp_Unit : Node_Id;
5393 Found : Boolean := False;
5394 Hidden : Boolean := False;
5398 -- Ada 2005 (AI-262): Generate a precise error concerning the
5399 -- Beaujolais effect that was previously detected
5401 if Nvis_Is_Private_Subprg then
5403 pragma Assert (Nkind (E2) = N_Defining_Identifier
5404 and then Ekind (E2) = E_Function
5405 and then Scope (E2) = Standard_Standard
5406 and then Has_Private_With (E2));
5408 -- Find the sloc corresponding to the private with'ed unit
5410 Comp_Unit := Cunit (Current_Sem_Unit);
5411 Error_Msg_Sloc := No_Location;
5413 Item := First (Context_Items (Comp_Unit));
5414 while Present (Item) loop
5415 if Nkind (Item) = N_With_Clause
5416 and then Private_Present (Item)
5417 and then Entity (Name (Item)) = E2
5419 Error_Msg_Sloc := Sloc (Item);
5426 pragma Assert (Error_Msg_Sloc /= No_Location);
5428 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
5432 Undefined (Nvis => True);
5436 -- First loop does hidden declarations
5439 while Present (Ent) loop
5440 if Is_Potentially_Use_Visible (Ent) then
5442 Error_Msg_N -- CODEFIX
5443 ("multiple use clauses cause hiding!", N);
5447 Error_Msg_Sloc := Sloc (Ent);
5448 Error_Msg_N -- CODEFIX
5449 ("hidden declaration#!", N);
5452 Ent := Homonym (Ent);
5455 -- If we found hidden declarations, then that's enough, don't
5456 -- bother looking for non-visible declarations as well.
5462 -- Second loop does non-directly visible declarations
5465 while Present (Ent) loop
5466 if not Is_Potentially_Use_Visible (Ent) then
5468 -- Do not bother the user with unknown entities
5470 if not Known_But_Invisible (Ent) then
5474 Error_Msg_Sloc := Sloc (Ent);
5476 -- Output message noting that there is a non-visible
5477 -- declaration, distinguishing the private part case.
5479 if Is_Hidden (Ent) then
5480 Error_Msg_N ("non-visible (private) declaration#!", N);
5482 -- If the entity is declared in a generic package, it
5483 -- cannot be visible, so there is no point in adding it
5484 -- to the list of candidates if another homograph from a
5485 -- non-generic package has been seen.
5487 elsif Ekind (Scope (Ent)) = E_Generic_Package
5493 Error_Msg_N -- CODEFIX
5494 ("non-visible declaration#!", N);
5496 if Ekind (Scope (Ent)) /= E_Generic_Package then
5500 if Is_Compilation_Unit (Ent)
5502 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5504 Error_Msg_Qual_Level := 99;
5505 Error_Msg_NE -- CODEFIX
5506 ("\\missing `WITH &;`", N, Ent);
5507 Error_Msg_Qual_Level := 0;
5510 if Ekind (Ent) = E_Discriminant
5511 and then Present (Corresponding_Discriminant (Ent))
5512 and then Scope (Corresponding_Discriminant (Ent)) =
5516 ("inherited discriminant not allowed here" &
5517 " (RM 3.8 (12), 3.8.1 (6))!", N);
5521 -- Set entity and its containing package as referenced. We
5522 -- can't be sure of this, but this seems a better choice
5523 -- to avoid unused entity messages.
5525 if Comes_From_Source (Ent) then
5526 Set_Referenced (Ent);
5527 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5532 Ent := Homonym (Ent);
5541 procedure Undefined (Nvis : Boolean) is
5542 Emsg : Error_Msg_Id;
5545 -- We should never find an undefined internal name. If we do, then
5546 -- see if we have previous errors. If so, ignore on the grounds that
5547 -- it is probably a cascaded message (e.g. a block label from a badly
5548 -- formed block). If no previous errors, then we have a real internal
5549 -- error of some kind so raise an exception.
5551 if Is_Internal_Name (Chars (N)) then
5552 if Total_Errors_Detected /= 0 then
5555 raise Program_Error;
5559 -- A very specialized error check, if the undefined variable is
5560 -- a case tag, and the case type is an enumeration type, check
5561 -- for a possible misspelling, and if so, modify the identifier
5563 -- Named aggregate should also be handled similarly ???
5565 if Nkind (N) = N_Identifier
5566 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5569 Case_Stm : constant Node_Id := Parent (Parent (N));
5570 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5575 if Is_Enumeration_Type (Case_Typ)
5576 and then not Is_Standard_Character_Type (Case_Typ)
5578 Lit := First_Literal (Case_Typ);
5579 Get_Name_String (Chars (Lit));
5581 if Chars (Lit) /= Chars (N)
5582 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5584 Error_Msg_Node_2 := Lit;
5585 Error_Msg_N -- CODEFIX
5586 ("& is undefined, assume misspelling of &", N);
5587 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5596 -- Normal processing
5598 Set_Entity (N, Any_Id);
5599 Set_Etype (N, Any_Type);
5601 -- We use the table Urefs to keep track of entities for which we
5602 -- have issued errors for undefined references. Multiple errors
5603 -- for a single name are normally suppressed, however we modify
5604 -- the error message to alert the programmer to this effect.
5606 for J in Urefs.First .. Urefs.Last loop
5607 if Chars (N) = Chars (Urefs.Table (J).Node) then
5608 if Urefs.Table (J).Err /= No_Error_Msg
5609 and then Sloc (N) /= Urefs.Table (J).Loc
5611 Error_Msg_Node_1 := Urefs.Table (J).Node;
5613 if Urefs.Table (J).Nvis then
5614 Change_Error_Text (Urefs.Table (J).Err,
5615 "& is not visible (more references follow)");
5617 Change_Error_Text (Urefs.Table (J).Err,
5618 "& is undefined (more references follow)");
5621 Urefs.Table (J).Err := No_Error_Msg;
5624 -- Although we will set Msg False, and thus suppress the
5625 -- message, we also set Error_Posted True, to avoid any
5626 -- cascaded messages resulting from the undefined reference.
5629 Set_Error_Posted (N);
5634 -- If entry not found, this is first undefined occurrence
5637 Error_Msg_N ("& is not visible!", N);
5641 Error_Msg_N ("& is undefined!", N);
5644 -- A very bizarre special check, if the undefined identifier
5645 -- is Put or Put_Line, then add a special error message (since
5646 -- this is a very common error for beginners to make).
5648 if Chars (N) in Name_Put | Name_Put_Line then
5649 Error_Msg_N -- CODEFIX
5650 ("\\possible missing `WITH Ada.Text_'I'O; " &
5651 "USE Ada.Text_'I'O`!", N);
5653 -- Another special check if N is the prefix of a selected
5654 -- component which is a known unit: add message complaining
5655 -- about missing with for this unit.
5657 elsif Nkind (Parent (N)) = N_Selected_Component
5658 and then N = Prefix (Parent (N))
5659 and then Is_Known_Unit (Parent (N))
5662 P : Node_Id := Parent (N);
5664 Error_Msg_Name_1 := Chars (N);
5665 Error_Msg_Name_2 := Chars (Selector_Name (P));
5667 if Nkind (Parent (P)) = N_Selected_Component
5668 and then Is_Known_Unit (Parent (P))
5671 Error_Msg_Name_3 := Chars (Selector_Name (P));
5672 Error_Msg_N -- CODEFIX
5673 ("\\missing `WITH %.%.%;`", N);
5676 Error_Msg_N -- CODEFIX
5677 ("\\missing `WITH %.%;`", N);
5682 -- Now check for possible misspellings
5686 Ematch : Entity_Id := Empty;
5688 for Nam in First_Name_Id .. Last_Name_Id loop
5689 E := Get_Name_Entity_Id (Nam);
5692 and then (Is_Immediately_Visible (E)
5694 Is_Potentially_Use_Visible (E))
5696 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5703 if Present (Ematch) then
5704 Error_Msg_NE -- CODEFIX
5705 ("\possible misspelling of&", N, Ematch);
5710 -- Make entry in undefined references table unless the full errors
5711 -- switch is set, in which case by refraining from generating the
5712 -- table entry we guarantee that we get an error message for every
5713 -- undefined reference. The entry is not added if we are ignoring
5716 if not All_Errors_Mode
5717 and then Ignore_Errors_Enable = 0
5718 and then not Get_Ignore_Errors
5732 Nested_Inst : Entity_Id := Empty;
5733 -- The entity of a nested instance which appears within Inst (if any)
5735 -- Start of processing for Find_Direct_Name
5738 -- If the entity pointer is already set, this is an internal node, or
5739 -- a node that is analyzed more than once, after a tree modification.
5740 -- In such a case there is no resolution to perform, just set the type.
5742 if Present (Entity (N)) then
5743 if Is_Type (Entity (N)) then
5744 Set_Etype (N, Entity (N));
5746 -- The exception to this general rule are constants associated with
5747 -- discriminals of protected types because for each protected op
5748 -- a new set of discriminals is internally created by the frontend
5749 -- (see Exp_Ch9.Set_Discriminals), and the current decoration of the
5750 -- entity pointer may have been set as part of a preanalysis, where
5751 -- discriminals still reference the first subprogram or entry to be
5752 -- expanded (see Expand_Protected_Body_Declarations).
5755 and then Ekind (Entity (N)) = E_Constant
5756 and then Present (Discriminal_Link (Entity (N)))
5757 and then Is_Protected_Type (Scope (Discriminal_Link (Entity (N))))
5763 Entyp : constant Entity_Id := Etype (Entity (N));
5766 -- One special case here. If the Etype field is already set,
5767 -- and references the packed array type corresponding to the
5768 -- etype of the referenced entity, then leave it alone. This
5769 -- happens for trees generated from Exp_Pakd, where expressions
5770 -- can be deliberately "mis-typed" to the packed array type.
5772 if Is_Packed_Array (Entyp)
5773 and then Present (Etype (N))
5774 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5778 -- If not that special case, then just reset the Etype
5781 Set_Etype (N, Etype (Entity (N)));
5786 -- Although the marking of use clauses happens at the end of
5787 -- Find_Direct_Name, a certain case where a generic actual satisfies
5788 -- a use clause must be checked here due to how the generic machinery
5789 -- handles the analysis of said actuals.
5792 and then Nkind (Parent (N)) = N_Generic_Association
5794 Mark_Use_Clauses (Entity (N));
5802 -- Preserve relevant elaboration-related attributes of the context which
5803 -- are no longer available or very expensive to recompute once analysis,
5804 -- resolution, and expansion are over.
5806 if Nkind (N) = N_Identifier then
5807 Mark_Elaboration_Attributes
5814 -- Here if Entity pointer was not set, we need full visibility analysis
5815 -- First we generate debugging output if the debug E flag is set.
5817 if Debug_Flag_E then
5818 Write_Str ("Looking for ");
5819 Write_Name (Chars (N));
5823 Homonyms := Current_Entity (N);
5824 Nvis_Entity := False;
5827 while Present (E) loop
5829 -- If entity is immediately visible or potentially use visible, then
5830 -- process the entity and we are done.
5832 if Is_Immediately_Visible (E) then
5833 goto Immediately_Visible_Entity;
5835 elsif Is_Potentially_Use_Visible (E) then
5836 goto Potentially_Use_Visible_Entity;
5838 -- Note if a known but invisible entity encountered
5840 elsif Known_But_Invisible (E) then
5841 Nvis_Entity := True;
5844 -- Move to next entity in chain and continue search
5849 -- If no entries on homonym chain that were potentially visible,
5850 -- and no entities reasonably considered as non-visible, then
5851 -- we have a plain undefined reference, with no additional
5852 -- explanation required.
5854 if not Nvis_Entity then
5855 Undefined (Nvis => False);
5857 -- Otherwise there is at least one entry on the homonym chain that
5858 -- is reasonably considered as being known and non-visible.
5866 -- Processing for a potentially use visible entry found. We must search
5867 -- the rest of the homonym chain for two reasons. First, if there is a
5868 -- directly visible entry, then none of the potentially use-visible
5869 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5870 -- for the case of multiple potentially use-visible entries hiding one
5871 -- another and as a result being non-directly visible (RM 8.4(11)).
5873 <<Potentially_Use_Visible_Entity>> declare
5874 Only_One_Visible : Boolean := True;
5875 All_Overloadable : Boolean := Is_Overloadable (E);
5879 while Present (E2) loop
5880 if Is_Immediately_Visible (E2) then
5882 -- If the use-visible entity comes from the actual for a
5883 -- formal package, it hides a directly visible entity from
5884 -- outside the instance.
5886 if From_Actual_Package (E)
5887 and then Scope_Depth (Scope (E2)) < Scope_Depth (Inst)
5892 goto Immediately_Visible_Entity;
5895 elsif Is_Potentially_Use_Visible (E2) then
5896 Only_One_Visible := False;
5897 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5899 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5900 -- that can occur in private_with clauses. Example:
5903 -- private with B; package A is
5904 -- package C is function B return Integer;
5906 -- V1 : Integer := B;
5907 -- private function B return Integer;
5908 -- V2 : Integer := B;
5911 -- V1 resolves to A.B, but V2 resolves to library unit B
5913 elsif Ekind (E2) = E_Function
5914 and then Scope (E2) = Standard_Standard
5915 and then Has_Private_With (E2)
5917 Only_One_Visible := False;
5918 All_Overloadable := False;
5919 Nvis_Is_Private_Subprg := True;
5926 -- On falling through this loop, we have checked that there are no
5927 -- immediately visible entities. Only_One_Visible is set if exactly
5928 -- one potentially use visible entity exists. All_Overloadable is
5929 -- set if all the potentially use visible entities are overloadable.
5930 -- The condition for legality is that either there is one potentially
5931 -- use visible entity, or if there is more than one, then all of them
5932 -- are overloadable.
5934 if Only_One_Visible or All_Overloadable then
5937 -- If there is more than one potentially use-visible entity and at
5938 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5939 -- Note that E points to the first such entity on the homonym list.
5942 -- If one of the entities is declared in an actual package, it
5943 -- was visible in the generic, and takes precedence over other
5944 -- entities that are potentially use-visible. The same applies
5945 -- if the entity is declared in a local instantiation of the
5946 -- current instance.
5950 -- Find the current instance
5952 Inst := Current_Scope;
5953 while Present (Inst) and then Inst /= Standard_Standard loop
5954 if Is_Generic_Instance (Inst) then
5958 Inst := Scope (Inst);
5961 -- Reexamine the candidate entities, giving priority to those
5962 -- that were visible within the generic.
5965 while Present (E2) loop
5966 Nested_Inst := Nearest_Enclosing_Instance (E2);
5968 -- The entity is declared within an actual package, or in a
5969 -- nested instance. The ">=" accounts for the case where the
5970 -- current instance and the nested instance are the same.
5972 if From_Actual_Package (E2)
5973 or else (Present (Nested_Inst)
5974 and then Scope_Depth (Nested_Inst) >=
5987 elsif Is_Predefined_Unit (Current_Sem_Unit) then
5988 -- A use clause in the body of a system file creates conflict
5989 -- with some entity in a user scope, while rtsfind is active.
5990 -- Keep only the entity coming from another predefined unit.
5993 while Present (E2) loop
5994 if In_Predefined_Unit (E2) then
6002 -- Entity must exist because predefined unit is correct
6004 raise Program_Error;
6013 -- Come here with E set to the first immediately visible entity on
6014 -- the homonym chain. This is the one we want unless there is another
6015 -- immediately visible entity further on in the chain for an inner
6016 -- scope (RM 8.3(8)).
6018 <<Immediately_Visible_Entity>> declare
6023 -- Find scope level of initial entity. When compiling through
6024 -- Rtsfind, the previous context is not completely invisible, and
6025 -- an outer entity may appear on the chain, whose scope is below
6026 -- the entry for Standard that delimits the current scope stack.
6027 -- Indicate that the level for this spurious entry is outside of
6028 -- the current scope stack.
6030 Level := Scope_Stack.Last;
6032 Scop := Scope_Stack.Table (Level).Entity;
6033 exit when Scop = Scope (E);
6035 exit when Scop = Standard_Standard;
6038 -- Now search remainder of homonym chain for more inner entry
6039 -- If the entity is Standard itself, it has no scope, and we
6040 -- compare it with the stack entry directly.
6043 while Present (E2) loop
6044 if Is_Immediately_Visible (E2) then
6046 -- If a generic package contains a local declaration that
6047 -- has the same name as the generic, there may be a visibility
6048 -- conflict in an instance, where the local declaration must
6049 -- also hide the name of the corresponding package renaming.
6050 -- We check explicitly for a package declared by a renaming,
6051 -- whose renamed entity is an instance that is on the scope
6052 -- stack, and that contains a homonym in the same scope. Once
6053 -- we have found it, we know that the package renaming is not
6054 -- immediately visible, and that the identifier denotes the
6055 -- other entity (and its homonyms if overloaded).
6057 if Scope (E) = Scope (E2)
6058 and then Ekind (E) = E_Package
6059 and then Present (Renamed_Object (E))
6060 and then Is_Generic_Instance (Renamed_Object (E))
6061 and then In_Open_Scopes (Renamed_Object (E))
6062 and then Comes_From_Source (N)
6064 Set_Is_Immediately_Visible (E, False);
6068 for J in Level + 1 .. Scope_Stack.Last loop
6069 if Scope_Stack.Table (J).Entity = Scope (E2)
6070 or else Scope_Stack.Table (J).Entity = E2
6083 -- At the end of that loop, E is the innermost immediately
6084 -- visible entity, so we are all set.
6087 -- Come here with entity found, and stored in E
6091 -- Check violation of No_Wide_Characters restriction
6093 Check_Wide_Character_Restriction (E, N);
6095 -- When distribution features are available (Get_PCS_Name /=
6096 -- Name_No_DSA), a remote access-to-subprogram type is converted
6097 -- into a record type holding whatever information is needed to
6098 -- perform a remote call on an RCI subprogram. In that case we
6099 -- rewrite any occurrence of the RAS type into the equivalent record
6100 -- type here. 'Access attribute references and RAS dereferences are
6101 -- then implemented using specific TSSs. However when distribution is
6102 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
6103 -- generation of these TSSs, and we must keep the RAS type in its
6104 -- original access-to-subprogram form (since all calls through a
6105 -- value of such type will be local anyway in the absence of a PCS).
6107 if Comes_From_Source (N)
6108 and then Is_Remote_Access_To_Subprogram_Type (E)
6109 and then Ekind (E) = E_Access_Subprogram_Type
6110 and then Expander_Active
6111 and then Get_PCS_Name /= Name_No_DSA
6113 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
6117 -- Set the entity. Note that the reason we call Set_Entity for the
6118 -- overloadable case, as opposed to Set_Entity_With_Checks is
6119 -- that in the overloaded case, the initial call can set the wrong
6120 -- homonym. The call that sets the right homonym is in Sem_Res and
6121 -- that call does use Set_Entity_With_Checks, so we don't miss
6124 if Is_Overloadable (E) then
6127 Set_Entity_With_Checks (N, E);
6133 Set_Etype (N, Get_Full_View (Etype (E)));
6136 if Debug_Flag_E then
6137 Write_Str (" found ");
6138 Write_Entity_Info (E, " ");
6141 -- If the Ekind of the entity is Void, it means that all homonyms
6142 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
6143 -- test is skipped if the current scope is a record and the name is
6144 -- a pragma argument expression (case of Atomic and Volatile pragmas
6145 -- and possibly other similar pragmas added later, which are allowed
6146 -- to reference components in the current record).
6148 if Ekind (E) = E_Void
6150 (not Is_Record_Type (Current_Scope)
6151 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
6153 Premature_Usage (N);
6155 -- If the entity is overloadable, collect all interpretations of the
6156 -- name for subsequent overload resolution. We optimize a bit here to
6157 -- do this only if we have an overloadable entity that is not on its
6158 -- own on the homonym chain.
6160 elsif Is_Overloadable (E)
6161 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
6163 Collect_Interps (N);
6165 -- If no homonyms were visible, the entity is unambiguous
6167 if not Is_Overloaded (N) then
6168 if not Is_Actual_Parameter then
6169 Generate_Reference (E, N);
6173 -- Case of non-overloadable entity, set the entity providing that
6174 -- we do not have the case of a discriminant reference within a
6175 -- default expression. Such references are replaced with the
6176 -- corresponding discriminal, which is the formal corresponding to
6177 -- to the discriminant in the initialization procedure.
6180 -- Entity is unambiguous, indicate that it is referenced here
6182 -- For a renaming of an object, always generate simple reference,
6183 -- we don't try to keep track of assignments in this case, except
6184 -- in SPARK mode where renamings are traversed for generating
6185 -- local effects of subprograms.
6188 and then Present (Renamed_Object (E))
6189 and then not GNATprove_Mode
6191 Generate_Reference (E, N);
6193 -- If the renamed entity is a private protected component,
6194 -- reference the original component as well. This needs to be
6195 -- done because the private renamings are installed before any
6196 -- analysis has occurred. Reference to a private component will
6197 -- resolve to the renaming and the original component will be
6198 -- left unreferenced, hence the following.
6200 if Is_Prival (E) then
6201 Generate_Reference (Prival_Link (E), N);
6204 -- One odd case is that we do not want to set the Referenced flag
6205 -- if the entity is a label, and the identifier is the label in
6206 -- the source, since this is not a reference from the point of
6207 -- view of the user.
6209 elsif Nkind (Parent (N)) = N_Label then
6211 R : constant Boolean := Referenced (E);
6214 -- Generate reference unless this is an actual parameter
6215 -- (see comment below).
6217 if not Is_Actual_Parameter then
6218 Generate_Reference (E, N);
6219 Set_Referenced (E, R);
6223 -- Normal case, not a label: generate reference
6226 if not Is_Actual_Parameter then
6228 -- Package or generic package is always a simple reference
6230 if Is_Package_Or_Generic_Package (E) then
6231 Generate_Reference (E, N, 'r');
6233 -- Else see if we have a left hand side
6238 Generate_Reference (E, N, 'm');
6241 Generate_Reference (E, N, 'r');
6243 -- If we don't know now, generate reference later
6246 Defer_Reference ((E, N));
6252 Set_Entity_Or_Discriminal (N, E);
6254 -- The name may designate a generalized reference, in which case
6255 -- the dereference interpretation will be included. Context is
6256 -- one in which a name is legal.
6258 if Ada_Version >= Ada_2012
6260 (Nkind (Parent (N)) in N_Subexpr
6261 or else Nkind (Parent (N)) in N_Assignment_Statement
6262 | N_Object_Declaration
6263 | N_Parameter_Association)
6265 Check_Implicit_Dereference (N, Etype (E));
6270 -- Mark relevant use-type and use-package clauses as effective if the
6271 -- node in question is not overloaded and therefore does not require
6274 -- Note: Generic actual subprograms do not follow the normal resolution
6275 -- path, so ignore the fact that they are overloaded and mark them
6278 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6279 Mark_Use_Clauses (N);
6282 -- Come here with entity set
6285 Check_Restriction_No_Use_Of_Entity (N);
6287 -- Annotate the tree by creating a variable reference marker in case the
6288 -- original variable reference is folded or optimized away. The variable
6289 -- reference marker is automatically saved for later examination by the
6290 -- ABE Processing phase. Variable references which act as actuals in a
6291 -- call require special processing and are left to Resolve_Actuals. The
6292 -- reference is a write when it appears on the left hand side of an
6295 if Needs_Variable_Reference_Marker (N => N, Calls_OK => False) then
6297 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6300 Build_Variable_Reference_Marker
6302 Read => not Is_Assignment_LHS,
6303 Write => Is_Assignment_LHS);
6306 end Find_Direct_Name;
6308 ------------------------
6309 -- Find_Expanded_Name --
6310 ------------------------
6312 -- This routine searches the homonym chain of the entity until it finds
6313 -- an entity declared in the scope denoted by the prefix. If the entity
6314 -- is private, it may nevertheless be immediately visible, if we are in
6315 -- the scope of its declaration.
6317 procedure Find_Expanded_Name (N : Node_Id) is
6318 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean;
6319 -- Determine whether expanded name Nod appears within a pragma which is
6320 -- a suitable context for an abstract view of a state or variable. The
6321 -- following pragmas fall in this category:
6328 -- In addition, pragma Abstract_State is also considered suitable even
6329 -- though it is an illegal context for an abstract view as this allows
6330 -- for proper resolution of abstract views of variables. This illegal
6331 -- context is later flagged in the analysis of indicator Part_Of.
6333 -----------------------------
6334 -- In_Abstract_View_Pragma --
6335 -----------------------------
6337 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is
6341 -- Climb the parent chain looking for a pragma
6344 while Present (Par) loop
6345 if Nkind (Par) = N_Pragma then
6346 if Pragma_Name_Unmapped (Par)
6347 in Name_Abstract_State
6351 | Name_Refined_Depends
6352 | Name_Refined_Global
6356 -- Otherwise the pragma is not a legal context for an abstract
6363 -- Prevent the search from going too far
6365 elsif Is_Body_Or_Package_Declaration (Par) then
6369 Par := Parent (Par);
6373 end In_Abstract_View_Pragma;
6377 Selector : constant Node_Id := Selector_Name (N);
6379 Candidate : Entity_Id := Empty;
6383 -- Start of processing for Find_Expanded_Name
6386 P_Name := Entity (Prefix (N));
6388 -- If the prefix is a renamed package, look for the entity in the
6389 -- original package.
6391 if Ekind (P_Name) = E_Package
6392 and then Present (Renamed_Object (P_Name))
6394 P_Name := Renamed_Object (P_Name);
6396 if From_Limited_With (P_Name)
6397 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6400 ("renaming of limited view of package & not usable in this"
6401 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
6403 elsif Has_Limited_View (P_Name)
6404 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6405 and then not Is_Visible_Through_Renamings (P_Name)
6408 ("renaming of limited view of package & not usable in this"
6409 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
6412 -- Rewrite node with entity field pointing to renamed object
6414 Rewrite (Prefix (N), New_Copy (Prefix (N)));
6415 Set_Entity (Prefix (N), P_Name);
6417 -- If the prefix is an object of a concurrent type, look for
6418 -- the entity in the associated task or protected type.
6420 elsif Is_Concurrent_Type (Etype (P_Name)) then
6421 P_Name := Etype (P_Name);
6424 Id := Current_Entity (Selector);
6427 Is_New_Candidate : Boolean;
6430 while Present (Id) loop
6431 if Scope (Id) = P_Name then
6433 Is_New_Candidate := True;
6435 -- Handle abstract views of states and variables. These are
6436 -- acceptable candidates only when the reference to the view
6437 -- appears in certain pragmas.
6439 if Ekind (Id) = E_Abstract_State
6440 and then From_Limited_With (Id)
6441 and then Present (Non_Limited_View (Id))
6443 if In_Abstract_View_Pragma (N) then
6444 Candidate := Non_Limited_View (Id);
6445 Is_New_Candidate := True;
6447 -- Hide the candidate because it is not used in a proper
6452 Is_New_Candidate := False;
6456 -- Ada 2005 (AI-217): Handle shadow entities associated with
6457 -- types declared in limited-withed nested packages. We don't need
6458 -- to handle E_Incomplete_Subtype entities because the entities
6459 -- in the limited view are always E_Incomplete_Type and
6460 -- E_Class_Wide_Type entities (see Build_Limited_Views).
6462 -- Regarding the expression used to evaluate the scope, it
6463 -- is important to note that the limited view also has shadow
6464 -- entities associated nested packages. For this reason the
6465 -- correct scope of the entity is the scope of the real entity.
6466 -- The non-limited view may itself be incomplete, in which case
6467 -- get the full view if available.
6469 elsif Ekind (Id) in E_Incomplete_Type | E_Class_Wide_Type
6470 and then From_Limited_With (Id)
6471 and then Present (Non_Limited_View (Id))
6472 and then Scope (Non_Limited_View (Id)) = P_Name
6474 Candidate := Get_Full_View (Non_Limited_View (Id));
6475 Is_New_Candidate := True;
6477 -- Handle special case where the prefix is a renaming of a shadow
6478 -- package which is visible. Required to avoid reporting spurious
6481 elsif Ekind (P_Name) = E_Package
6482 and then From_Limited_With (P_Name)
6483 and then not From_Limited_With (Id)
6484 and then Sloc (Scope (Id)) = Sloc (P_Name)
6485 and then Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6487 Candidate := Get_Full_View (Id);
6488 Is_New_Candidate := True;
6490 -- An unusual case arises with a fully qualified name for an
6491 -- entity local to a generic child unit package, within an
6492 -- instantiation of that package. The name of the unit now
6493 -- denotes the renaming created within the instance. This is
6494 -- only relevant in an instance body, see below.
6496 elsif Is_Generic_Instance (Scope (Id))
6497 and then In_Open_Scopes (Scope (Id))
6498 and then In_Instance_Body
6499 and then Ekind (Scope (Id)) = E_Package
6500 and then Ekind (Id) = E_Package
6501 and then Renamed_Entity (Id) = Scope (Id)
6502 and then Is_Immediately_Visible (P_Name)
6504 Is_New_Candidate := True;
6507 Is_New_Candidate := False;
6510 if Is_New_Candidate then
6512 -- If entity is a child unit, either it is a visible child of
6513 -- the prefix, or we are in the body of a generic prefix, as
6514 -- will happen when a child unit is instantiated in the body
6515 -- of a generic parent. This is because the instance body does
6516 -- not restore the full compilation context, given that all
6517 -- non-local references have been captured.
6519 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
6520 exit when Is_Visible_Lib_Unit (Id)
6521 or else (Is_Child_Unit (Id)
6522 and then In_Open_Scopes (Scope (Id))
6523 and then In_Instance_Body);
6525 exit when not Is_Hidden (Id);
6528 exit when Is_Immediately_Visible (Id);
6536 and then Ekind (P_Name) in E_Procedure | E_Function
6537 and then Is_Generic_Instance (P_Name)
6539 -- Expanded name denotes entity in (instance of) generic subprogram.
6540 -- The entity may be in the subprogram instance, or may denote one of
6541 -- the formals, which is declared in the enclosing wrapper package.
6543 P_Name := Scope (P_Name);
6545 Id := Current_Entity (Selector);
6546 while Present (Id) loop
6547 exit when Scope (Id) = P_Name;
6552 if No (Id) or else Chars (Id) /= Chars (Selector) then
6553 Set_Etype (N, Any_Type);
6555 -- If we are looking for an entity defined in System, try to find it
6556 -- in the child package that may have been provided as an extension
6557 -- to System. The Extend_System pragma will have supplied the name of
6558 -- the extension, which may have to be loaded.
6560 if Chars (P_Name) = Name_System
6561 and then Scope (P_Name) = Standard_Standard
6562 and then Present (System_Extend_Unit)
6563 and then Present_System_Aux (N)
6565 Set_Entity (Prefix (N), System_Aux_Id);
6566 Find_Expanded_Name (N);
6569 -- There is an implicit instance of the predefined operator in
6570 -- the given scope. The operator entity is defined in Standard.
6571 -- Has_Implicit_Operator makes the node into an Expanded_Name.
6573 elsif Nkind (Selector) = N_Operator_Symbol
6574 and then Has_Implicit_Operator (N)
6578 -- If there is no literal defined in the scope denoted by the
6579 -- prefix, the literal may belong to (a type derived from)
6580 -- Standard_Character, for which we have no explicit literals.
6582 elsif Nkind (Selector) = N_Character_Literal
6583 and then Has_Implicit_Character_Literal (N)
6588 -- If the prefix is a single concurrent object, use its name in
6589 -- the error message, rather than that of the anonymous type.
6591 if Is_Concurrent_Type (P_Name)
6592 and then Is_Internal_Name (Chars (P_Name))
6594 Error_Msg_Node_2 := Entity (Prefix (N));
6596 Error_Msg_Node_2 := P_Name;
6599 if P_Name = System_Aux_Id then
6600 P_Name := Scope (P_Name);
6601 Set_Entity (Prefix (N), P_Name);
6604 if Present (Candidate) then
6606 -- If we know that the unit is a child unit we can give a more
6607 -- accurate error message.
6609 if Is_Child_Unit (Candidate) then
6611 -- If the candidate is a private child unit and we are in
6612 -- the visible part of a public unit, specialize the error
6613 -- message. There might be a private with_clause for it,
6614 -- but it is not currently active.
6616 if Is_Private_Descendant (Candidate)
6617 and then Ekind (Current_Scope) = E_Package
6618 and then not In_Private_Part (Current_Scope)
6619 and then not Is_Private_Descendant (Current_Scope)
6622 ("private child unit& is not visible here", Selector);
6624 -- Normal case where we have a missing with for a child unit
6627 Error_Msg_Qual_Level := 99;
6628 Error_Msg_NE -- CODEFIX
6629 ("missing `WITH &;`", Selector, Candidate);
6630 Error_Msg_Qual_Level := 0;
6633 -- Here we don't know that this is a child unit
6636 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
6640 -- Within the instantiation of a child unit, the prefix may
6641 -- denote the parent instance, but the selector has the name
6642 -- of the original child. That is to say, when A.B appears
6643 -- within an instantiation of generic child unit B, the scope
6644 -- stack includes an instance of A (P_Name) and an instance
6645 -- of B under some other name. We scan the scope to find this
6646 -- child instance, which is the desired entity.
6647 -- Note that the parent may itself be a child instance, if
6648 -- the reference is of the form A.B.C, in which case A.B has
6649 -- already been rewritten with the proper entity.
6651 if In_Open_Scopes (P_Name)
6652 and then Is_Generic_Instance (P_Name)
6655 Gen_Par : constant Entity_Id :=
6656 Generic_Parent (Specification
6657 (Unit_Declaration_Node (P_Name)));
6658 S : Entity_Id := Current_Scope;
6662 for J in reverse 0 .. Scope_Stack.Last loop
6663 S := Scope_Stack.Table (J).Entity;
6665 exit when S = Standard_Standard;
6667 if Ekind (S) in E_Function | E_Package | E_Procedure
6670 Generic_Parent (Specification
6671 (Unit_Declaration_Node (S)));
6673 -- Check that P is a generic child of the generic
6674 -- parent of the prefix.
6677 and then Chars (P) = Chars (Selector)
6678 and then Scope (P) = Gen_Par
6689 -- If this is a selection from Ada, System or Interfaces, then
6690 -- we assume a missing with for the corresponding package.
6692 if Is_Known_Unit (N)
6693 and then not (Present (Entity (Prefix (N)))
6694 and then Scope (Entity (Prefix (N))) /=
6697 if not Error_Posted (N) then
6698 Error_Msg_Node_2 := Selector;
6699 Error_Msg_N -- CODEFIX
6700 ("missing `WITH &.&;`", Prefix (N));
6703 -- If this is a selection from a dummy package, then suppress
6704 -- the error message, of course the entity is missing if the
6705 -- package is missing.
6707 elsif Sloc (Error_Msg_Node_2) = No_Location then
6710 -- Here we have the case of an undefined component
6713 -- The prefix may hide a homonym in the context that
6714 -- declares the desired entity. This error can use a
6715 -- specialized message.
6717 if In_Open_Scopes (P_Name) then
6719 H : constant Entity_Id := Homonym (P_Name);
6723 and then Is_Compilation_Unit (H)
6725 (Is_Immediately_Visible (H)
6726 or else Is_Visible_Lib_Unit (H))
6728 Id := First_Entity (H);
6729 while Present (Id) loop
6730 if Chars (Id) = Chars (Selector) then
6731 Error_Msg_Qual_Level := 99;
6732 Error_Msg_Name_1 := Chars (Selector);
6734 ("% not declared in&", N, P_Name);
6736 ("\use fully qualified name starting with "
6737 & "Standard to make& visible", N, H);
6738 Error_Msg_Qual_Level := 0;
6746 -- If not found, standard error message
6748 Error_Msg_NE ("& not declared in&", N, Selector);
6754 -- Might be worth specializing the case when the prefix
6755 -- is a limited view.
6756 -- ... not declared in limited view of...
6758 Error_Msg_NE ("& not declared in&", N, Selector);
6761 -- Check for misspelling of some entity in prefix
6763 Id := First_Entity (P_Name);
6764 while Present (Id) loop
6765 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
6766 and then not Is_Internal_Name (Chars (Id))
6768 Error_Msg_NE -- CODEFIX
6769 ("possible misspelling of&", Selector, Id);
6776 -- Specialize the message if this may be an instantiation
6777 -- of a child unit that was not mentioned in the context.
6779 if Nkind (Parent (N)) = N_Package_Instantiation
6780 and then Is_Generic_Instance (Entity (Prefix (N)))
6781 and then Is_Compilation_Unit
6782 (Generic_Parent (Parent (Entity (Prefix (N)))))
6784 Error_Msg_Node_2 := Selector;
6785 Error_Msg_N -- CODEFIX
6786 ("\missing `WITH &.&;`", Prefix (N));
6796 if Comes_From_Source (N)
6797 and then Is_Remote_Access_To_Subprogram_Type (Id)
6798 and then Ekind (Id) = E_Access_Subprogram_Type
6799 and then Present (Equivalent_Type (Id))
6801 -- If we are not actually generating distribution code (i.e. the
6802 -- current PCS is the dummy non-distributed version), then the
6803 -- Equivalent_Type will be missing, and Id should be treated as
6804 -- a regular access-to-subprogram type.
6806 Id := Equivalent_Type (Id);
6807 Set_Chars (Selector, Chars (Id));
6810 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
6812 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
6813 if From_Limited_With (Id)
6814 or else Is_Type (Id)
6815 or else Ekind (Id) = E_Package
6820 ("limited withed package can only be used to access incomplete "
6825 if Is_Task_Type (P_Name)
6826 and then ((Ekind (Id) = E_Entry
6827 and then Nkind (Parent (N)) /= N_Attribute_Reference)
6829 (Ekind (Id) = E_Entry_Family
6831 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
6833 -- If both the task type and the entry are in scope, this may still
6834 -- be the expanded name of an entry formal.
6836 if In_Open_Scopes (Id)
6837 and then Nkind (Parent (N)) = N_Selected_Component
6842 -- It is an entry call after all, either to the current task
6843 -- (which will deadlock) or to an enclosing task.
6845 Analyze_Selected_Component (N);
6851 when N_Selected_Component =>
6852 Reinit_Field_To_Zero (N, F_Is_Prefixed_Call);
6853 Change_Selected_Component_To_Expanded_Name (N);
6855 when N_Expanded_Name =>
6859 pragma Assert (False);
6862 -- Preserve relevant elaboration-related attributes of the context which
6863 -- are no longer available or very expensive to recompute once analysis,
6864 -- resolution, and expansion are over.
6866 Mark_Elaboration_Attributes
6872 -- Set appropriate type
6874 if Is_Type (Id) then
6877 Set_Etype (N, Get_Full_View (Etype (Id)));
6880 -- Do style check and generate reference, but skip both steps if this
6881 -- entity has homonyms, since we may not have the right homonym set yet.
6882 -- The proper homonym will be set during the resolve phase.
6884 if Has_Homonym (Id) then
6888 Set_Entity_Or_Discriminal (N, Id);
6892 Generate_Reference (Id, N, 'm');
6895 Generate_Reference (Id, N, 'r');
6898 Defer_Reference ((Id, N));
6902 -- Check for violation of No_Wide_Characters
6904 Check_Wide_Character_Restriction (Id, N);
6906 -- If the Ekind of the entity is Void, it means that all homonyms are
6907 -- hidden from all visibility (RM 8.3(5,14-20)).
6909 if Ekind (Id) = E_Void then
6910 Premature_Usage (N);
6912 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6914 H : Entity_Id := Homonym (Id);
6917 while Present (H) loop
6918 if Scope (H) = Scope (Id)
6919 and then (not Is_Hidden (H)
6920 or else Is_Immediately_Visible (H))
6922 Collect_Interps (N);
6929 -- If an extension of System is present, collect possible explicit
6930 -- overloadings declared in the extension.
6932 if Chars (P_Name) = Name_System
6933 and then Scope (P_Name) = Standard_Standard
6934 and then Present (System_Extend_Unit)
6935 and then Present_System_Aux (N)
6937 H := Current_Entity (Id);
6939 while Present (H) loop
6940 if Scope (H) = System_Aux_Id then
6941 Add_One_Interp (N, H, Etype (H));
6950 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6951 and then Scope (Id) /= Standard_Standard
6953 -- In addition to user-defined operators in the given scope, there
6954 -- may be an implicit instance of the predefined operator. The
6955 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6956 -- and added to the interpretations. Procedure Add_One_Interp will
6957 -- determine which hides which.
6959 if Has_Implicit_Operator (N) then
6964 -- If there is a single interpretation for N we can generate a
6965 -- reference to the unique entity found.
6967 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6968 Generate_Reference (Id, N);
6971 -- Mark relevant use-type and use-package clauses as effective if the
6972 -- node in question is not overloaded and therefore does not require
6975 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6976 Mark_Use_Clauses (N);
6979 Check_Restriction_No_Use_Of_Entity (N);
6981 -- Annotate the tree by creating a variable reference marker in case the
6982 -- original variable reference is folded or optimized away. The variable
6983 -- reference marker is automatically saved for later examination by the
6984 -- ABE Processing phase. Variable references which act as actuals in a
6985 -- call require special processing and are left to Resolve_Actuals. The
6986 -- reference is a write when it appears on the left hand side of an
6989 if Needs_Variable_Reference_Marker
6994 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6997 Build_Variable_Reference_Marker
6999 Read => not Is_Assignment_LHS,
7000 Write => Is_Assignment_LHS);
7003 end Find_Expanded_Name;
7005 --------------------
7006 -- Find_First_Use --
7007 --------------------
7009 function Find_First_Use (Use_Clause : Node_Id) return Node_Id is
7013 -- Loop through the Prev_Use_Clause chain
7016 while Present (Prev_Use_Clause (Curr)) loop
7017 Curr := Prev_Use_Clause (Curr);
7023 -------------------------
7024 -- Find_Renamed_Entity --
7025 -------------------------
7027 function Find_Renamed_Entity
7031 Is_Actual : Boolean := False) return Entity_Id
7034 I1 : Interp_Index := 0; -- Suppress junk warnings
7040 function Find_Nearer_Entity
7043 Old2_S : Entity_Id) return Entity_Id;
7044 -- Determine whether one of Old_S1 and Old_S2 is nearer to New_S than
7045 -- the other, and return it if so. Return Empty otherwise. We use this
7046 -- in conjunction with Inherit_Renamed_Profile to simplify later type
7047 -- disambiguation for actual subprograms in instances.
7049 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
7050 -- If the renamed entity is an implicit operator, check whether it is
7051 -- visible because its operand type is properly visible. This check
7052 -- applies to explicit renamed entities that appear in the source in a
7053 -- renaming declaration or a formal subprogram instance, but not to
7054 -- default generic actuals with a name.
7056 function Report_Overload return Entity_Id;
7057 -- List possible interpretations, and specialize message in the
7058 -- case of a generic actual.
7060 function Within (Inner, Outer : Entity_Id) return Boolean;
7061 -- Determine whether a candidate subprogram is defined within the
7062 -- enclosing instance. If yes, it has precedence over outer candidates.
7064 --------------------------
7065 -- Find_Nearer_Entity --
7066 --------------------------
7068 function Find_Nearer_Entity
7071 Old2_S : Entity_Id) return Entity_Id
7079 New_F := First_Formal (New_S);
7080 Old1_F := First_Formal (Old1_S);
7081 Old2_F := First_Formal (Old2_S);
7083 -- The criterion is whether the type of the formals of one of Old1_S
7084 -- and Old2_S is an ancestor subtype of the type of the corresponding
7085 -- formals of New_S while the other is not (we already know that they
7086 -- are all subtypes of the same base type).
7088 -- This makes it possible to find the more correct renamed entity in
7089 -- the case of a generic instantiation nested in an enclosing one for
7090 -- which different formal types get the same actual type, which will
7091 -- in turn make it possible for Inherit_Renamed_Profile to preserve
7092 -- types on formal parameters and ultimately simplify disambiguation.
7094 -- Consider the follow package G:
7097 -- type Item_T is private;
7098 -- with function Compare (L, R: Item_T) return Boolean is <>;
7100 -- type Bound_T is private;
7101 -- with function Compare (L, R : Bound_T) return Boolean is <>;
7106 -- package body G is
7107 -- package My_Inner is Inner_G (Bound_T);
7111 -- with the following package Inner_G:
7114 -- type T is private;
7115 -- with function Compare (L, R: T) return Boolean is <>;
7116 -- package Inner_G is
7117 -- function "<" (L, R: T) return Boolean is (Compare (L, R));
7120 -- If G is instantiated on the same actual type with a single Compare
7124 -- function Compare (L, R : T) return Boolean;
7125 -- package My_G is new (T, T);
7127 -- then the renaming generated for Compare in the inner instantiation
7128 -- is ambiguous: it can rename either of the renamings generated for
7129 -- the outer instantiation. Now if the first one is picked up, then
7130 -- the subtypes of the formal parameters of the renaming will not be
7131 -- preserved in Inherit_Renamed_Profile because they are subtypes of
7132 -- the Bound_T formal type and not of the Item_T formal type, so we
7133 -- need to arrange for the second one to be picked up instead.
7135 while Present (New_F) loop
7136 if Etype (Old1_F) /= Etype (Old2_F) then
7137 Anc_T := Ancestor_Subtype (Etype (New_F));
7139 if Etype (Old1_F) = Anc_T then
7141 elsif Etype (Old2_F) = Anc_T then
7146 Next_Formal (New_F);
7147 Next_Formal (Old1_F);
7148 Next_Formal (Old2_F);
7151 pragma Assert (No (Old1_F));
7152 pragma Assert (No (Old2_F));
7155 end Find_Nearer_Entity;
7157 --------------------------
7158 -- Is_Visible_Operation --
7159 --------------------------
7161 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
7167 if Ekind (Op) /= E_Operator
7168 or else Scope (Op) /= Standard_Standard
7169 or else (In_Instance
7170 and then (not Is_Actual
7171 or else Present (Enclosing_Instance)))
7176 -- For a fixed point type operator, check the resulting type,
7177 -- because it may be a mixed mode integer * fixed operation.
7179 if Present (Next_Formal (First_Formal (New_S)))
7180 and then Is_Fixed_Point_Type (Etype (New_S))
7182 Typ := Etype (New_S);
7184 Typ := Etype (First_Formal (New_S));
7187 Btyp := Base_Type (Typ);
7189 if Nkind (Nam) /= N_Expanded_Name then
7190 return (In_Open_Scopes (Scope (Btyp))
7191 or else Is_Potentially_Use_Visible (Btyp)
7192 or else In_Use (Btyp)
7193 or else In_Use (Scope (Btyp)));
7196 Scop := Entity (Prefix (Nam));
7198 if Ekind (Scop) = E_Package
7199 and then Present (Renamed_Object (Scop))
7201 Scop := Renamed_Object (Scop);
7204 -- Operator is visible if prefix of expanded name denotes
7205 -- scope of type, or else type is defined in System_Aux
7206 -- and the prefix denotes System.
7208 return Scope (Btyp) = Scop
7209 or else (Scope (Btyp) = System_Aux_Id
7210 and then Scope (Scope (Btyp)) = Scop);
7213 end Is_Visible_Operation;
7219 function Within (Inner, Outer : Entity_Id) return Boolean is
7223 Sc := Scope (Inner);
7224 while Sc /= Standard_Standard loop
7235 ---------------------
7236 -- Report_Overload --
7237 ---------------------
7239 function Report_Overload return Entity_Id is
7242 Error_Msg_NE -- CODEFIX
7243 ("ambiguous actual subprogram&, " &
7244 "possible interpretations:", N, Nam);
7246 Error_Msg_N -- CODEFIX
7247 ("ambiguous subprogram, " &
7248 "possible interpretations:", N);
7251 List_Interps (Nam, N);
7253 end Report_Overload;
7255 -- Start of processing for Find_Renamed_Entity
7259 Candidate_Renaming := Empty;
7261 if Is_Overloaded (Nam) then
7262 Get_First_Interp (Nam, Ind, It);
7263 while Present (It.Nam) loop
7264 if Entity_Matches_Spec (It.Nam, New_S)
7265 and then Is_Visible_Operation (It.Nam)
7267 if Old_S /= Any_Id then
7269 -- Note: The call to Disambiguate only happens if a
7270 -- previous interpretation was found, in which case I1
7271 -- has received a value.
7273 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
7275 if It1 = No_Interp then
7276 Inst := Enclosing_Instance;
7278 if Present (Inst) then
7279 if Within (It.Nam, Inst) then
7280 if Within (Old_S, Inst) then
7282 It_D : constant Uint :=
7283 Scope_Depth_Default_0 (It.Nam);
7284 Old_D : constant Uint :=
7285 Scope_Depth_Default_0 (Old_S);
7288 -- Choose the innermost subprogram, which
7289 -- would hide the outer one in the generic.
7291 if Old_D > It_D then
7293 elsif It_D > Old_D then
7297 -- Otherwise, if we can determine that one
7298 -- of the entities is nearer to the renaming
7299 -- than the other, choose it. If not, then
7300 -- return the newer one as done historically.
7303 Find_Nearer_Entity (New_S, Old_S, It.Nam);
7304 if Present (N_Ent) then
7312 elsif Within (Old_S, Inst) then
7316 return Report_Overload;
7319 -- If not within an instance, ambiguity is real
7322 return Report_Overload;
7336 Present (First_Formal (It.Nam))
7337 and then Present (First_Formal (New_S))
7338 and then (Base_Type (Etype (First_Formal (It.Nam))) =
7339 Base_Type (Etype (First_Formal (New_S))))
7341 Candidate_Renaming := It.Nam;
7344 Get_Next_Interp (Ind, It);
7347 Set_Entity (Nam, Old_S);
7349 if Old_S /= Any_Id then
7350 Set_Is_Overloaded (Nam, False);
7353 -- Non-overloaded case
7357 and then Present (Enclosing_Instance)
7358 and then Entity_Matches_Spec (Entity (Nam), New_S)
7360 Old_S := Entity (Nam);
7362 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
7363 Candidate_Renaming := New_S;
7365 if Is_Visible_Operation (Entity (Nam)) then
7366 Old_S := Entity (Nam);
7369 elsif Present (First_Formal (Entity (Nam)))
7370 and then Present (First_Formal (New_S))
7371 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
7372 Base_Type (Etype (First_Formal (New_S))))
7374 Candidate_Renaming := Entity (Nam);
7379 end Find_Renamed_Entity;
7381 -----------------------------
7382 -- Find_Selected_Component --
7383 -----------------------------
7385 procedure Find_Selected_Component (N : Node_Id) is
7386 P : constant Node_Id := Prefix (N);
7389 -- Entity denoted by prefix
7396 function Available_Subtype return Boolean;
7397 -- A small optimization: if the prefix is constrained and the component
7398 -- is an array type we may already have a usable subtype for it, so we
7399 -- can use it rather than generating a new one, because the bounds
7400 -- will be the values of the discriminants and not discriminant refs.
7401 -- This simplifies value tracing in GNATprove. For consistency, both
7402 -- the entity name and the subtype come from the constrained component.
7404 -- This is only used in GNATprove mode: when generating code it may be
7405 -- necessary to create an itype in the scope of use of the selected
7406 -- component, e.g. in the context of a expanded record equality.
7408 function Is_Reference_In_Subunit return Boolean;
7409 -- In a subunit, the scope depth is not a proper measure of hiding,
7410 -- because the context of the proper body may itself hide entities in
7411 -- parent units. This rare case requires inspecting the tree directly
7412 -- because the proper body is inserted in the main unit and its context
7413 -- is simply added to that of the parent.
7415 -----------------------
7416 -- Available_Subtype --
7417 -----------------------
7419 function Available_Subtype return Boolean is
7423 if GNATprove_Mode then
7424 Comp := First_Entity (Etype (P));
7425 while Present (Comp) loop
7426 if Chars (Comp) = Chars (Selector_Name (N)) then
7427 Set_Etype (N, Etype (Comp));
7428 Set_Entity (Selector_Name (N), Comp);
7429 Set_Etype (Selector_Name (N), Etype (Comp));
7433 Next_Component (Comp);
7438 end Available_Subtype;
7440 -----------------------------
7441 -- Is_Reference_In_Subunit --
7442 -----------------------------
7444 function Is_Reference_In_Subunit return Boolean is
7446 Comp_Unit : Node_Id;
7450 while Present (Comp_Unit)
7451 and then Nkind (Comp_Unit) /= N_Compilation_Unit
7453 Comp_Unit := Parent (Comp_Unit);
7456 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
7460 -- Now check whether the package is in the context of the subunit
7462 Clause := First (Context_Items (Comp_Unit));
7463 while Present (Clause) loop
7464 if Nkind (Clause) = N_With_Clause
7465 and then Entity (Name (Clause)) = P_Name
7474 end Is_Reference_In_Subunit;
7476 -- Start of processing for Find_Selected_Component
7481 if Nkind (P) = N_Error then
7485 -- If the selector already has an entity, the node has been constructed
7486 -- in the course of expansion, and is known to be valid. Do not verify
7487 -- that it is defined for the type (it may be a private component used
7488 -- in the expansion of record equality).
7490 if Present (Entity (Selector_Name (N))) then
7491 if No (Etype (N)) or else Etype (N) = Any_Type then
7493 Sel_Name : constant Node_Id := Selector_Name (N);
7494 Selector : constant Entity_Id := Entity (Sel_Name);
7498 Set_Etype (Sel_Name, Etype (Selector));
7500 if not Is_Entity_Name (P) then
7504 -- Build an actual subtype except for the first parameter
7505 -- of an init proc, where this actual subtype is by
7506 -- definition incorrect, since the object is uninitialized
7507 -- (and does not even have defined discriminants etc.)
7509 if Is_Entity_Name (P)
7510 and then Ekind (Entity (P)) = E_Function
7512 Nam := New_Copy (P);
7514 if Is_Overloaded (P) then
7515 Save_Interps (P, Nam);
7518 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
7520 Analyze_Selected_Component (N);
7523 elsif Ekind (Selector) = E_Component
7524 and then (not Is_Entity_Name (P)
7525 or else Chars (Entity (P)) /= Name_uInit)
7527 -- Check if we already have an available subtype we can use
7529 if Ekind (Etype (P)) = E_Record_Subtype
7530 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
7531 and then Is_Array_Type (Etype (Selector))
7532 and then not Is_Packed (Etype (Selector))
7533 and then Available_Subtype
7537 -- Do not build the subtype when referencing components of
7538 -- dispatch table wrappers. Required to avoid generating
7539 -- elaboration code with HI runtimes.
7541 elsif Is_RTE (Scope (Selector), RE_Dispatch_Table_Wrapper)
7543 Is_RTE (Scope (Selector), RE_No_Dispatch_Table_Wrapper)
7548 Build_Actual_Subtype_Of_Component
7549 (Etype (Selector), N);
7556 if No (C_Etype) then
7557 C_Etype := Etype (Selector);
7559 Insert_Action (N, C_Etype);
7560 C_Etype := Defining_Identifier (C_Etype);
7563 Set_Etype (N, C_Etype);
7566 -- If the selected component appears within a default expression
7567 -- and it has an actual subtype, the preanalysis has not yet
7568 -- completed its analysis, because Insert_Actions is disabled in
7569 -- that context. Within the init proc of the enclosing type we
7570 -- must complete this analysis, if an actual subtype was created.
7572 elsif Inside_Init_Proc then
7574 Typ : constant Entity_Id := Etype (N);
7575 Decl : constant Node_Id := Declaration_Node (Typ);
7577 if Nkind (Decl) = N_Subtype_Declaration
7578 and then not Analyzed (Decl)
7579 and then Is_List_Member (Decl)
7580 and then No (Parent (Decl))
7583 Insert_Action (N, Decl);
7590 elsif Is_Entity_Name (P) then
7591 P_Name := Entity (P);
7593 -- The prefix may denote an enclosing type which is the completion
7594 -- of an incomplete type declaration.
7596 if Is_Type (P_Name) then
7597 Set_Entity (P, Get_Full_View (P_Name));
7598 Set_Etype (P, Entity (P));
7599 P_Name := Entity (P);
7602 P_Type := Base_Type (Etype (P));
7604 if Debug_Flag_E then
7605 Write_Str ("Found prefix type to be ");
7606 Write_Entity_Info (P_Type, " "); Write_Eol;
7609 -- If the prefix's type is an access type, get to the record type
7611 if Is_Access_Type (P_Type) then
7612 P_Type := Implicitly_Designated_Type (P_Type);
7615 -- First check for components of a record object (not the result of
7616 -- a call, which is handled below). This also covers the case where
7617 -- where the extension feature that supports the prefixed form of
7618 -- calls for primitives of untagged types is enabled (excluding
7619 -- concurrent cases, which are handled further below).
7622 and then (Has_Components (P_Type)
7623 or else (Extensions_Allowed
7624 and then not Is_Concurrent_Type (P_Type)))
7625 and then not Is_Overloadable (P_Name)
7626 and then not Is_Type (P_Name)
7628 -- Selected component of record. Type checking will validate
7629 -- name of selector.
7631 -- ??? Could we rewrite an implicit dereference into an explicit
7634 Analyze_Selected_Component (N);
7636 -- Reference to type name in predicate/invariant expression
7638 elsif Is_Concurrent_Type (P_Type)
7639 and then not In_Open_Scopes (P_Name)
7640 and then (not Is_Concurrent_Type (Etype (P_Name))
7641 or else not In_Open_Scopes (Etype (P_Name)))
7643 -- Call to protected operation or entry. Type checking is
7644 -- needed on the prefix.
7646 Analyze_Selected_Component (N);
7648 elsif (In_Open_Scopes (P_Name)
7649 and then Ekind (P_Name) /= E_Void
7650 and then not Is_Overloadable (P_Name))
7651 or else (Is_Concurrent_Type (Etype (P_Name))
7652 and then In_Open_Scopes (Etype (P_Name)))
7654 -- Prefix denotes an enclosing loop, block, or task, i.e. an
7655 -- enclosing construct that is not a subprogram or accept.
7657 -- A special case: a protected body may call an operation
7658 -- on an external object of the same type, in which case it
7659 -- is not an expanded name. If the prefix is the type itself,
7660 -- or the context is a single synchronized object it can only
7661 -- be interpreted as an expanded name.
7663 if Is_Concurrent_Type (Etype (P_Name)) then
7665 or else Present (Anonymous_Object (Etype (P_Name)))
7667 Find_Expanded_Name (N);
7670 Analyze_Selected_Component (N);
7675 Find_Expanded_Name (N);
7678 elsif Ekind (P_Name) = E_Package then
7679 Find_Expanded_Name (N);
7681 elsif Is_Overloadable (P_Name) then
7683 -- The subprogram may be a renaming (of an enclosing scope) as
7684 -- in the case of the name of the generic within an instantiation.
7686 if Ekind (P_Name) in E_Procedure | E_Function
7687 and then Present (Alias (P_Name))
7688 and then Is_Generic_Instance (Alias (P_Name))
7690 P_Name := Alias (P_Name);
7693 if Is_Overloaded (P) then
7695 -- The prefix must resolve to a unique enclosing construct
7698 Found : Boolean := False;
7703 Get_First_Interp (P, Ind, It);
7704 while Present (It.Nam) loop
7705 if In_Open_Scopes (It.Nam) then
7708 "prefix must be unique enclosing scope", N);
7709 Set_Entity (N, Any_Id);
7710 Set_Etype (N, Any_Type);
7719 Get_Next_Interp (Ind, It);
7724 if In_Open_Scopes (P_Name) then
7725 Set_Entity (P, P_Name);
7726 Set_Is_Overloaded (P, False);
7727 Find_Expanded_Name (N);
7730 -- If no interpretation as an expanded name is possible, it
7731 -- must be a selected component of a record returned by a
7732 -- function call. Reformat prefix as a function call, the rest
7733 -- is done by type resolution.
7735 -- Error if the prefix is procedure or entry, as is P.X
7737 if Ekind (P_Name) /= E_Function
7739 (not Is_Overloaded (P)
7740 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
7742 -- Prefix may mention a package that is hidden by a local
7743 -- declaration: let the user know. Scan the full homonym
7744 -- chain, the candidate package may be anywhere on it.
7746 if Present (Homonym (Current_Entity (P_Name))) then
7747 P_Name := Current_Entity (P_Name);
7749 while Present (P_Name) loop
7750 exit when Ekind (P_Name) = E_Package;
7751 P_Name := Homonym (P_Name);
7754 if Present (P_Name) then
7755 if not Is_Reference_In_Subunit then
7756 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
7758 ("package& is hidden by declaration#", N, P_Name);
7761 Set_Entity (Prefix (N), P_Name);
7762 Find_Expanded_Name (N);
7766 P_Name := Entity (Prefix (N));
7771 ("invalid prefix in selected component&", N, P_Name);
7772 Change_Selected_Component_To_Expanded_Name (N);
7773 Set_Entity (N, Any_Id);
7774 Set_Etype (N, Any_Type);
7776 -- Here we have a function call, so do the reformatting
7779 Nam := New_Copy (P);
7780 Save_Interps (P, Nam);
7782 -- We use Replace here because this is one of those cases
7783 -- where the parser has missclassified the node, and we fix
7784 -- things up and then do the semantic analysis on the fixed
7785 -- up node. Normally we do this using one of the Sinfo.CN
7786 -- routines, but this is too tricky for that.
7788 -- Note that using Rewrite would be wrong, because we would
7789 -- have a tree where the original node is unanalyzed.
7792 Make_Function_Call (Sloc (P), Name => Nam));
7794 -- Now analyze the reformatted node
7798 -- If the prefix is illegal after this transformation, there
7799 -- may be visibility errors on the prefix. The safest is to
7800 -- treat the selected component as an error.
7802 if Error_Posted (P) then
7803 Set_Etype (N, Any_Type);
7807 Analyze_Selected_Component (N);
7812 -- Remaining cases generate various error messages
7815 -- Format node as expanded name, to avoid cascaded errors
7817 Change_Selected_Component_To_Expanded_Name (N);
7818 Set_Entity (N, Any_Id);
7819 Set_Etype (N, Any_Type);
7821 -- Issue error message, but avoid this if error issued already.
7822 -- Use identifier of prefix if one is available.
7824 if P_Name = Any_Id then
7827 -- It is not an error if the prefix is the current instance of
7828 -- type name, e.g. the expression of a type aspect, when it is
7829 -- analyzed within a generic unit. We still have to verify that a
7830 -- component of that name exists, and decorate the node
7833 elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then
7838 Comp := First_Entity (Entity (P));
7839 while Present (Comp) loop
7840 if Chars (Comp) = Chars (Selector_Name (N)) then
7841 Set_Entity (N, Comp);
7842 Set_Etype (N, Etype (Comp));
7843 Set_Entity (Selector_Name (N), Comp);
7844 Set_Etype (Selector_Name (N), Etype (Comp));
7852 elsif Ekind (P_Name) = E_Void then
7853 Premature_Usage (P);
7855 elsif Nkind (P) /= N_Attribute_Reference then
7857 -- This may have been meant as a prefixed call to a primitive
7858 -- of an untagged type. If it is a function call check type of
7859 -- its first formal and add explanation.
7862 F : constant Entity_Id :=
7863 Current_Entity (Selector_Name (N));
7866 and then Is_Overloadable (F)
7867 and then Present (First_Entity (F))
7868 and then not Is_Tagged_Type (Etype (First_Entity (F)))
7871 ("prefixed call is only allowed for objects of a "
7872 & "tagged type", N);
7876 Error_Msg_N ("invalid prefix in selected component&", P);
7878 if Is_Incomplete_Type (P_Type)
7879 and then Is_Access_Type (Etype (P))
7882 ("\dereference must not be of an incomplete type "
7883 & "(RM 3.10.1)", P);
7887 Error_Msg_N ("invalid prefix in selected component", P);
7891 -- If prefix is not the name of an entity, it must be an expression,
7892 -- whose type is appropriate for a record. This is determined by
7895 Analyze_Selected_Component (N);
7898 Analyze_Dimension (N);
7899 end Find_Selected_Component;
7905 procedure Find_Type (N : Node_Id) is
7915 elsif Nkind (N) = N_Attribute_Reference then
7917 -- Class attribute. This is not valid in Ada 83 mode, but we do not
7918 -- need to enforce that at this point, since the declaration of the
7919 -- tagged type in the prefix would have been flagged already.
7921 if Attribute_Name (N) = Name_Class then
7922 Check_Restriction (No_Dispatch, N);
7923 Find_Type (Prefix (N));
7925 -- Propagate error from bad prefix
7927 if Etype (Prefix (N)) = Any_Type then
7928 Set_Entity (N, Any_Type);
7929 Set_Etype (N, Any_Type);
7933 T := Base_Type (Entity (Prefix (N)));
7935 -- Case where type is not known to be tagged. Its appearance in
7936 -- the prefix of the 'Class attribute indicates that the full view
7939 if not Is_Tagged_Type (T) then
7940 if Ekind (T) = E_Incomplete_Type then
7942 -- It is legal to denote the class type of an incomplete
7943 -- type. The full type will have to be tagged, of course.
7944 -- In Ada 2005 this usage is declared obsolescent, so we
7945 -- warn accordingly. This usage is only legal if the type
7946 -- is completed in the current scope, and not for a limited
7949 if Ada_Version >= Ada_2005 then
7951 -- Test whether the Available_View of a limited type view
7952 -- is tagged, since the limited view may not be marked as
7953 -- tagged if the type itself has an untagged incomplete
7954 -- type view in its package.
7956 if From_Limited_With (T)
7957 and then not Is_Tagged_Type (Available_View (T))
7960 ("prefix of Class attribute must be tagged", N);
7961 Set_Etype (N, Any_Type);
7962 Set_Entity (N, Any_Type);
7966 if Restriction_Check_Required (No_Obsolescent_Features)
7969 (No_Obsolescent_Features, Prefix (N));
7972 if Warn_On_Obsolescent_Feature then
7974 ("applying ''Class to an untagged incomplete type"
7975 & " is an obsolescent feature (RM J.11)?r?", N);
7980 Set_Is_Tagged_Type (T);
7981 Set_Direct_Primitive_Operations (T, New_Elmt_List);
7982 Make_Class_Wide_Type (T);
7983 Set_Entity (N, Class_Wide_Type (T));
7984 Set_Etype (N, Class_Wide_Type (T));
7986 elsif Ekind (T) = E_Private_Type
7987 and then not Is_Generic_Type (T)
7988 and then In_Private_Part (Scope (T))
7990 -- The Class attribute can be applied to an untagged private
7991 -- type fulfilled by a tagged type prior to the full type
7992 -- declaration (but only within the parent package's private
7993 -- part). Create the class-wide type now and check that the
7994 -- full type is tagged later during its analysis. Note that
7995 -- we do not mark the private type as tagged, unlike the
7996 -- case of incomplete types, because the type must still
7997 -- appear untagged to outside units.
7999 if No (Class_Wide_Type (T)) then
8000 Make_Class_Wide_Type (T);
8003 Set_Entity (N, Class_Wide_Type (T));
8004 Set_Etype (N, Class_Wide_Type (T));
8007 -- Should we introduce a type Any_Tagged and use Wrong_Type
8008 -- here, it would be a bit more consistent???
8011 ("tagged type required, found}",
8012 Prefix (N), First_Subtype (T));
8013 Set_Entity (N, Any_Type);
8017 -- Case of tagged type
8020 if Is_Concurrent_Type (T) then
8021 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
8023 -- Previous error. Create a class-wide type for the
8024 -- synchronized type itself, with minimal semantic
8025 -- attributes, to catch other errors in some ACATS tests.
8027 pragma Assert (Serious_Errors_Detected /= 0);
8028 Make_Class_Wide_Type (T);
8029 C := Class_Wide_Type (T);
8030 Set_First_Entity (C, First_Entity (T));
8033 C := Class_Wide_Type
8034 (Corresponding_Record_Type (Entity (Prefix (N))));
8038 C := Class_Wide_Type (Entity (Prefix (N)));
8041 Set_Entity_With_Checks (N, C);
8042 Generate_Reference (C, N);
8046 -- Base attribute, not allowed in Ada 83
8048 elsif Attribute_Name (N) = Name_Base then
8049 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
8051 ("(Ada 83) Base attribute not allowed in subtype mark", N);
8054 Find_Type (Prefix (N));
8055 Typ := Entity (Prefix (N));
8057 if Ada_Version >= Ada_95
8058 and then not Is_Scalar_Type (Typ)
8059 and then not Is_Generic_Type (Typ)
8062 ("prefix of Base attribute must be scalar type",
8065 elsif Warn_On_Redundant_Constructs
8066 and then Base_Type (Typ) = Typ
8068 Error_Msg_NE -- CODEFIX
8069 ("redundant attribute, & is its own base type?r?", N, Typ);
8072 T := Base_Type (Typ);
8074 -- Rewrite attribute reference with type itself (see similar
8075 -- processing in Analyze_Attribute, case Base). Preserve prefix
8076 -- if present, for other legality checks.
8078 if Nkind (Prefix (N)) = N_Expanded_Name then
8080 Make_Expanded_Name (Sloc (N),
8082 Prefix => New_Copy (Prefix (Prefix (N))),
8083 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
8086 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
8093 elsif Attribute_Name (N) = Name_Stub_Type then
8095 -- This is handled in Analyze_Attribute
8099 -- All other attributes are invalid in a subtype mark
8102 Error_Msg_N ("invalid attribute in subtype mark", N);
8108 if Is_Entity_Name (N) then
8109 T_Name := Entity (N);
8111 Error_Msg_N ("subtype mark required in this context", N);
8112 Set_Etype (N, Any_Type);
8116 if T_Name = Any_Id or else Etype (N) = Any_Type then
8118 -- Undefined id. Make it into a valid type
8120 Set_Entity (N, Any_Type);
8122 elsif not Is_Type (T_Name)
8123 and then T_Name /= Standard_Void_Type
8125 Error_Msg_Sloc := Sloc (T_Name);
8126 Error_Msg_N ("subtype mark required in this context", N);
8127 Error_Msg_NE ("\\found & declared#", N, T_Name);
8128 Set_Entity (N, Any_Type);
8131 -- If the type is an incomplete type created to handle
8132 -- anonymous access components of a record type, then the
8133 -- incomplete type is the visible entity and subsequent
8134 -- references will point to it. Mark the original full
8135 -- type as referenced, to prevent spurious warnings.
8137 if Is_Incomplete_Type (T_Name)
8138 and then Present (Full_View (T_Name))
8139 and then not Comes_From_Source (T_Name)
8141 Set_Referenced (Full_View (T_Name));
8144 T_Name := Get_Full_View (T_Name);
8146 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
8147 -- limited-with clauses
8149 if From_Limited_With (T_Name)
8150 and then Is_Incomplete_Type (T_Name)
8151 and then Present (Non_Limited_View (T_Name))
8152 and then Is_Interface (Non_Limited_View (T_Name))
8154 T_Name := Non_Limited_View (T_Name);
8157 if In_Open_Scopes (T_Name) then
8158 if Ekind (Base_Type (T_Name)) = E_Task_Type then
8160 -- In Ada 2005, a task name can be used in an access
8161 -- definition within its own body.
8163 if Ada_Version >= Ada_2005
8164 and then Nkind (Parent (N)) = N_Access_Definition
8166 Set_Entity (N, T_Name);
8167 Set_Etype (N, T_Name);
8172 ("task type cannot be used as type mark " &
8173 "within its own spec or body", N);
8176 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
8178 -- In Ada 2005, a protected name can be used in an access
8179 -- definition within its own body.
8181 if Ada_Version >= Ada_2005
8182 and then Nkind (Parent (N)) = N_Access_Definition
8184 Set_Entity (N, T_Name);
8185 Set_Etype (N, T_Name);
8190 ("protected type cannot be used as type mark " &
8191 "within its own spec or body", N);
8195 Error_Msg_N ("type declaration cannot refer to itself", N);
8198 Set_Etype (N, Any_Type);
8199 Set_Entity (N, Any_Type);
8200 Set_Error_Posted (T_Name);
8204 Set_Entity (N, T_Name);
8205 Set_Etype (N, T_Name);
8209 if Present (Etype (N)) and then Comes_From_Source (N) then
8210 if Is_Fixed_Point_Type (Etype (N)) then
8211 Check_Restriction (No_Fixed_Point, N);
8212 elsif Is_Floating_Point_Type (Etype (N)) then
8213 Check_Restriction (No_Floating_Point, N);
8216 -- A Ghost type must appear in a specific context
8218 if Is_Ghost_Entity (Etype (N)) then
8219 Check_Ghost_Context (Etype (N), N);
8224 --------------------
8225 -- Has_Components --
8226 --------------------
8228 function Has_Components (Typ : Entity_Id) return Boolean is
8230 return Is_Record_Type (Typ)
8231 or else (Is_Private_Type (Typ) and then Has_Discriminants (Typ))
8232 or else (Is_Task_Type (Typ) and then Has_Discriminants (Typ))
8233 or else (Is_Incomplete_Type (Typ)
8234 and then From_Limited_With (Typ)
8235 and then Is_Record_Type (Available_View (Typ)));
8238 ------------------------------------
8239 -- Has_Implicit_Character_Literal --
8240 ------------------------------------
8242 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
8244 Found : Boolean := False;
8245 P : constant Entity_Id := Entity (Prefix (N));
8246 Priv_Id : Entity_Id := Empty;
8249 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8250 Priv_Id := First_Private_Entity (P);
8253 if P = Standard_Standard then
8254 Change_Selected_Component_To_Expanded_Name (N);
8255 Rewrite (N, Selector_Name (N));
8257 Set_Etype (Original_Node (N), Standard_Character);
8261 Id := First_Entity (P);
8262 while Present (Id) and then Id /= Priv_Id loop
8263 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
8265 -- We replace the node with the literal itself, resolve as a
8266 -- character, and set the type correctly.
8269 Change_Selected_Component_To_Expanded_Name (N);
8270 Rewrite (N, Selector_Name (N));
8273 Set_Etype (Original_Node (N), Id);
8277 -- More than one type derived from Character in given scope.
8278 -- Collect all possible interpretations.
8280 Add_One_Interp (N, Id, Id);
8288 end Has_Implicit_Character_Literal;
8290 ----------------------
8291 -- Has_Private_With --
8292 ----------------------
8294 function Has_Private_With (E : Entity_Id) return Boolean is
8295 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
8299 Item := First (Context_Items (Comp_Unit));
8300 while Present (Item) loop
8301 if Nkind (Item) = N_With_Clause
8302 and then Private_Present (Item)
8303 and then Entity (Name (Item)) = E
8312 end Has_Private_With;
8314 ---------------------------
8315 -- Has_Implicit_Operator --
8316 ---------------------------
8318 function Has_Implicit_Operator (N : Node_Id) return Boolean is
8319 Op_Id : constant Name_Id := Chars (Selector_Name (N));
8320 P : constant Entity_Id := Entity (Prefix (N));
8322 Priv_Id : Entity_Id := Empty;
8324 procedure Add_Implicit_Operator
8326 Op_Type : Entity_Id := Empty);
8327 -- Add implicit interpretation to node N, using the type for which a
8328 -- predefined operator exists. If the operator yields a boolean type,
8329 -- the Operand_Type is implicitly referenced by the operator, and a
8330 -- reference to it must be generated.
8332 ---------------------------
8333 -- Add_Implicit_Operator --
8334 ---------------------------
8336 procedure Add_Implicit_Operator
8338 Op_Type : Entity_Id := Empty)
8340 Predef_Op : Entity_Id;
8343 Predef_Op := Current_Entity (Selector_Name (N));
8344 while Present (Predef_Op)
8345 and then Scope (Predef_Op) /= Standard_Standard
8347 Predef_Op := Homonym (Predef_Op);
8350 if Nkind (N) = N_Selected_Component then
8351 Change_Selected_Component_To_Expanded_Name (N);
8354 -- If the context is an unanalyzed function call, determine whether
8355 -- a binary or unary interpretation is required.
8357 if Nkind (Parent (N)) = N_Indexed_Component then
8359 Is_Binary_Call : constant Boolean :=
8361 (Next (First (Expressions (Parent (N)))));
8362 Is_Binary_Op : constant Boolean :=
8364 (Predef_Op) /= Last_Entity (Predef_Op);
8365 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
8368 if Is_Binary_Call then
8369 if Is_Binary_Op then
8370 Add_One_Interp (N, Predef_Op, T);
8372 Add_One_Interp (N, Predef_Op2, T);
8375 if not Is_Binary_Op then
8376 Add_One_Interp (N, Predef_Op, T);
8378 -- Predef_Op2 may be empty in case of previous errors
8380 elsif Present (Predef_Op2) then
8381 Add_One_Interp (N, Predef_Op2, T);
8387 Add_One_Interp (N, Predef_Op, T);
8389 -- For operators with unary and binary interpretations, if
8390 -- context is not a call, add both
8392 if Present (Homonym (Predef_Op)) then
8393 Add_One_Interp (N, Homonym (Predef_Op), T);
8397 -- The node is a reference to a predefined operator, and
8398 -- an implicit reference to the type of its operands.
8400 if Present (Op_Type) then
8401 Generate_Operator_Reference (N, Op_Type);
8403 Generate_Operator_Reference (N, T);
8405 end Add_Implicit_Operator;
8407 -- Start of processing for Has_Implicit_Operator
8410 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8411 Priv_Id := First_Private_Entity (P);
8414 Id := First_Entity (P);
8418 -- Boolean operators: an implicit declaration exists if the scope
8419 -- contains a declaration for a derived Boolean type, or for an
8420 -- array of Boolean type.
8427 while Id /= Priv_Id loop
8428 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
8429 Add_Implicit_Operator (Id);
8436 -- Equality: look for any non-limited type (result is Boolean)
8441 while Id /= Priv_Id loop
8443 and then not Is_Limited_Type (Id)
8444 and then Is_Base_Type (Id)
8446 Add_Implicit_Operator (Standard_Boolean, Id);
8453 -- Comparison operators: scalar type, or array of scalar
8460 while Id /= Priv_Id loop
8461 if (Is_Scalar_Type (Id)
8462 or else (Is_Array_Type (Id)
8463 and then Is_Scalar_Type (Component_Type (Id))))
8464 and then Is_Base_Type (Id)
8466 Add_Implicit_Operator (Standard_Boolean, Id);
8473 -- Arithmetic operators: any numeric type
8484 while Id /= Priv_Id loop
8485 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
8486 Add_Implicit_Operator (Id);
8493 -- Concatenation: any one-dimensional array type
8495 when Name_Op_Concat =>
8496 while Id /= Priv_Id loop
8497 if Is_Array_Type (Id)
8498 and then Number_Dimensions (Id) = 1
8499 and then Is_Base_Type (Id)
8501 Add_Implicit_Operator (Id);
8508 -- What is the others condition here? Should we be using a
8509 -- subtype of Name_Id that would restrict to operators ???
8515 -- If we fall through, then we do not have an implicit operator
8518 end Has_Implicit_Operator;
8520 -----------------------------------
8521 -- Has_Loop_In_Inner_Open_Scopes --
8522 -----------------------------------
8524 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
8526 -- Several scope stacks are maintained by Scope_Stack. The base of the
8527 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8528 -- flag in the scope stack entry. Note that the scope stacks used to
8529 -- simply be delimited implicitly by the presence of Standard_Standard
8530 -- at their base, but there now are cases where this is not sufficient
8531 -- because Standard_Standard actually may appear in the middle of the
8532 -- active set of scopes.
8534 for J in reverse 0 .. Scope_Stack.Last loop
8536 -- S was reached without seing a loop scope first
8538 if Scope_Stack.Table (J).Entity = S then
8541 -- S was not yet reached, so it contains at least one inner loop
8543 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
8547 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8548 -- cases where Standard_Standard appears in the middle of the active
8549 -- set of scopes. This affects the declaration and overriding of
8550 -- private inherited operations in instantiations of generic child
8553 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
8556 raise Program_Error; -- unreachable
8557 end Has_Loop_In_Inner_Open_Scopes;
8559 --------------------
8560 -- In_Open_Scopes --
8561 --------------------
8563 function In_Open_Scopes (S : Entity_Id) return Boolean is
8565 -- Several scope stacks are maintained by Scope_Stack. The base of the
8566 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8567 -- flag in the scope stack entry. Note that the scope stacks used to
8568 -- simply be delimited implicitly by the presence of Standard_Standard
8569 -- at their base, but there now are cases where this is not sufficient
8570 -- because Standard_Standard actually may appear in the middle of the
8571 -- active set of scopes.
8573 for J in reverse 0 .. Scope_Stack.Last loop
8574 if Scope_Stack.Table (J).Entity = S then
8578 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8579 -- cases where Standard_Standard appears in the middle of the active
8580 -- set of scopes. This affects the declaration and overriding of
8581 -- private inherited operations in instantiations of generic child
8584 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
8590 -----------------------------
8591 -- Inherit_Renamed_Profile --
8592 -----------------------------
8594 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
8601 if Ekind (Old_S) = E_Operator then
8602 New_F := First_Formal (New_S);
8604 while Present (New_F) loop
8605 Set_Etype (New_F, Base_Type (Etype (New_F)));
8606 Next_Formal (New_F);
8609 Set_Etype (New_S, Base_Type (Etype (New_S)));
8612 New_F := First_Formal (New_S);
8613 Old_F := First_Formal (Old_S);
8615 while Present (New_F) loop
8616 New_T := Etype (New_F);
8617 Old_T := Etype (Old_F);
8619 -- If the new type is a renaming of the old one, as is the case
8620 -- for actuals in instances, retain its name, to simplify later
8623 if Nkind (Parent (New_T)) = N_Subtype_Declaration
8624 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
8625 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
8629 Set_Etype (New_F, Old_T);
8632 Next_Formal (New_F);
8633 Next_Formal (Old_F);
8636 pragma Assert (No (Old_F));
8638 if Ekind (Old_S) in E_Function | E_Enumeration_Literal then
8639 Set_Etype (New_S, Etype (Old_S));
8642 end Inherit_Renamed_Profile;
8648 procedure Initialize is
8653 -------------------------
8654 -- Install_Use_Clauses --
8655 -------------------------
8657 procedure Install_Use_Clauses
8659 Force_Installation : Boolean := False)
8665 while Present (U) loop
8667 -- Case of USE package
8669 if Nkind (U) = N_Use_Package_Clause then
8670 Use_One_Package (U, Name (U), True);
8675 Use_One_Type (Subtype_Mark (U), Force => Force_Installation);
8679 Next_Use_Clause (U);
8681 end Install_Use_Clauses;
8683 ----------------------
8684 -- Mark_Use_Clauses --
8685 ----------------------
8687 procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is
8688 procedure Mark_Parameters (Call : Entity_Id);
8689 -- Perform use_type_clause marking for all parameters in a subprogram
8690 -- or operator call.
8692 procedure Mark_Use_Package (Pak : Entity_Id);
8693 -- Move up the Prev_Use_Clause chain for packages denoted by Pak -
8694 -- marking each clause in the chain as effective in the process.
8696 procedure Mark_Use_Type (E : Entity_Id);
8697 -- Similar to Do_Use_Package_Marking except we move up the
8698 -- Prev_Use_Clause chain for the type denoted by E.
8700 ---------------------
8701 -- Mark_Parameters --
8702 ---------------------
8704 procedure Mark_Parameters (Call : Entity_Id) is
8708 -- Move through all of the formals
8710 Curr := First_Formal (Call);
8711 while Present (Curr) loop
8712 Mark_Use_Type (Curr);
8717 -- Handle the return type
8719 Mark_Use_Type (Call);
8720 end Mark_Parameters;
8722 ----------------------
8723 -- Mark_Use_Package --
8724 ----------------------
8726 procedure Mark_Use_Package (Pak : Entity_Id) is
8730 -- Ignore cases where the scope of the type is not a package (e.g.
8731 -- Standard_Standard).
8733 if Ekind (Pak) /= E_Package then
8737 Curr := Current_Use_Clause (Pak);
8738 while Present (Curr)
8739 and then not Is_Effective_Use_Clause (Curr)
8741 -- We need to mark the previous use clauses as effective, but
8742 -- each use clause may in turn render other use_package_clauses
8743 -- effective. Additionally, it is possible to have a parent
8744 -- package renamed as a child of itself so we must check the
8745 -- prefix entity is not the same as the package we are marking.
8747 if Nkind (Name (Curr)) /= N_Identifier
8748 and then Present (Prefix (Name (Curr)))
8749 and then Entity (Prefix (Name (Curr))) /= Pak
8751 Mark_Use_Package (Entity (Prefix (Name (Curr))));
8753 -- It is also possible to have a child package without a prefix
8754 -- that relies on a previous use_package_clause.
8756 elsif Nkind (Name (Curr)) = N_Identifier
8757 and then Is_Child_Unit (Entity (Name (Curr)))
8759 Mark_Use_Package (Scope (Entity (Name (Curr))));
8762 -- Mark the use_package_clause as effective and move up the chain
8764 Set_Is_Effective_Use_Clause (Curr);
8766 Curr := Prev_Use_Clause (Curr);
8768 end Mark_Use_Package;
8774 procedure Mark_Use_Type (E : Entity_Id) is
8779 -- Ignore void types and unresolved string literals and primitives
8781 if Nkind (E) = N_String_Literal
8782 or else Nkind (Etype (E)) not in N_Entity
8783 or else not Is_Type (Etype (E))
8788 -- Primitives with class-wide operands might additionally render
8789 -- their base type's use_clauses effective - so do a recursive check
8792 Base := Base_Type (Etype (E));
8794 if Ekind (Base) = E_Class_Wide_Type then
8795 Mark_Use_Type (Base);
8798 -- The package containing the type or operator function being used
8799 -- may be in use as well, so mark any use_package_clauses for it as
8800 -- effective. There are also additional sanity checks performed here
8801 -- for ignoring previous errors.
8803 Mark_Use_Package (Scope (Base));
8805 if Nkind (E) in N_Op
8806 and then Present (Entity (E))
8807 and then Present (Scope (Entity (E)))
8809 Mark_Use_Package (Scope (Entity (E)));
8812 Curr := Current_Use_Clause (Base);
8813 while Present (Curr)
8814 and then not Is_Effective_Use_Clause (Curr)
8816 -- Current use_type_clause may render other use_package_clauses
8819 if Nkind (Subtype_Mark (Curr)) /= N_Identifier
8820 and then Present (Prefix (Subtype_Mark (Curr)))
8822 Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr))));
8825 -- Mark the use_type_clause as effective and move up the chain
8827 Set_Is_Effective_Use_Clause (Curr);
8829 Curr := Prev_Use_Clause (Curr);
8833 -- Start of processing for Mark_Use_Clauses
8836 -- Use clauses in and of themselves do not count as a "use" of a
8839 if Nkind (Parent (Id)) in N_Use_Package_Clause | N_Use_Type_Clause then
8845 if Nkind (Id) in N_Entity then
8847 -- Mark the entity's package
8849 if Is_Potentially_Use_Visible (Id) then
8850 Mark_Use_Package (Scope (Id));
8853 -- Mark enumeration literals
8855 if Ekind (Id) = E_Enumeration_Literal then
8860 elsif (Is_Overloadable (Id)
8861 or else Is_Generic_Subprogram (Id))
8862 and then (Is_Potentially_Use_Visible (Id)
8863 or else Is_Intrinsic_Subprogram (Id)
8864 or else (Ekind (Id) in E_Function | E_Procedure
8865 and then Is_Generic_Actual_Subprogram (Id)))
8867 Mark_Parameters (Id);
8875 if Nkind (Id) in N_Op then
8877 -- At this point the left operand may not be resolved if we are
8878 -- encountering multiple operators next to eachother in an
8881 if Nkind (Id) in N_Binary_Op
8882 and then not (Nkind (Left_Opnd (Id)) in N_Op)
8884 Mark_Use_Type (Left_Opnd (Id));
8887 Mark_Use_Type (Right_Opnd (Id));
8890 -- Mark entity identifiers
8892 elsif Nkind (Id) in N_Has_Entity
8893 and then (Is_Potentially_Use_Visible (Entity (Id))
8894 or else (Is_Generic_Instance (Entity (Id))
8895 and then Is_Immediately_Visible (Entity (Id))))
8897 -- Ignore fully qualified names as they do not count as a "use" of
8900 if Nkind (Id) in N_Identifier | N_Operator_Symbol
8901 or else (Present (Prefix (Id))
8902 and then Scope (Entity (Id)) /= Entity (Prefix (Id)))
8904 Mark_Use_Clauses (Entity (Id));
8908 end Mark_Use_Clauses;
8910 --------------------------------
8911 -- Most_Descendant_Use_Clause --
8912 --------------------------------
8914 function Most_Descendant_Use_Clause
8915 (Clause1 : Entity_Id;
8916 Clause2 : Entity_Id) return Entity_Id
8922 if Clause1 = Clause2 then
8926 -- We determine which one is the most descendant by the scope distance
8927 -- to the ultimate parent unit.
8929 Scope1 := Entity_Of_Unit (Unit (Parent (Clause1)));
8930 Scope2 := Entity_Of_Unit (Unit (Parent (Clause2)));
8931 while Scope1 /= Standard_Standard
8932 and then Scope2 /= Standard_Standard
8934 Scope1 := Scope (Scope1);
8935 Scope2 := Scope (Scope2);
8937 if not Present (Scope1) then
8939 elsif not Present (Scope2) then
8944 if Scope1 = Standard_Standard then
8949 end Most_Descendant_Use_Clause;
8955 procedure Pop_Scope is
8956 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8957 S : constant Entity_Id := SST.Entity;
8960 if Debug_Flag_E then
8964 -- Set Default_Storage_Pool field of the library unit if necessary
8966 if Is_Package_Or_Generic_Package (S)
8968 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
8971 Aux : constant Node_Id :=
8972 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
8974 if No (Default_Storage_Pool (Aux)) then
8975 Set_Default_Storage_Pool (Aux, Default_Pool);
8980 Scope_Suppress := SST.Save_Scope_Suppress;
8981 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
8982 Check_Policy_List := SST.Save_Check_Policy_List;
8983 Default_Pool := SST.Save_Default_Storage_Pool;
8984 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
8985 SPARK_Mode := SST.Save_SPARK_Mode;
8986 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
8987 Default_SSO := SST.Save_Default_SSO;
8988 Uneval_Old := SST.Save_Uneval_Old;
8990 if Debug_Flag_W then
8991 Write_Str ("<-- exiting scope: ");
8992 Write_Name (Chars (Current_Scope));
8993 Write_Str (", Depth=");
8994 Write_Int (Int (Scope_Stack.Last));
8998 End_Use_Clauses (SST.First_Use_Clause);
9000 -- If the actions to be wrapped are still there they will get lost
9001 -- causing incomplete code to be generated. It is better to abort in
9002 -- this case (and we do the abort even with assertions off since the
9003 -- penalty is incorrect code generation).
9005 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
9006 raise Program_Error;
9009 -- Free last subprogram name if allocated, and pop scope
9011 Free (SST.Last_Subprogram_Name);
9012 Scope_Stack.Decrement_Last;
9019 procedure Push_Scope (S : Entity_Id) is
9020 E : constant Entity_Id := Scope (S);
9022 function Component_Alignment_Default return Component_Alignment_Kind;
9023 -- Return Component_Alignment_Kind for the newly-pushed scope.
9025 function Component_Alignment_Default return Component_Alignment_Kind is
9027 -- Each new scope pushed onto the scope stack inherits the component
9028 -- alignment of the previous scope. This emulates the "visibility"
9029 -- semantics of pragma Component_Alignment.
9031 if Scope_Stack.Last > Scope_Stack.First then
9032 return Scope_Stack.Table
9033 (Scope_Stack.Last - 1).Component_Alignment_Default;
9035 -- Otherwise, this is the first scope being pushed on the scope
9036 -- stack. Inherit the component alignment from the configuration
9037 -- form of pragma Component_Alignment (if any).
9040 return Configuration_Component_Alignment;
9042 end Component_Alignment_Default;
9045 if Ekind (S) = E_Void then
9048 -- Set scope depth if not a non-concurrent type, and we have not yet set
9049 -- the scope depth. This means that we have the first occurrence of the
9050 -- scope, and this is where the depth is set.
9052 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
9053 and then not Scope_Depth_Set (S)
9055 if S = Standard_Standard then
9056 Set_Scope_Depth_Value (S, Uint_0);
9058 elsif Is_Child_Unit (S) then
9059 Set_Scope_Depth_Value (S, Uint_1);
9061 elsif not Is_Record_Type (Current_Scope) then
9062 if Scope_Depth_Set (Current_Scope) then
9063 if Ekind (S) = E_Loop then
9064 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
9066 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
9072 Scope_Stack.Increment_Last;
9074 Scope_Stack.Table (Scope_Stack.Last) :=
9076 Save_Scope_Suppress => Scope_Suppress,
9077 Save_Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
9078 Save_Check_Policy_List => Check_Policy_List,
9079 Save_Default_Storage_Pool => Default_Pool,
9080 Save_No_Tagged_Streams => No_Tagged_Streams,
9081 Save_SPARK_Mode => SPARK_Mode,
9082 Save_SPARK_Mode_Pragma => SPARK_Mode_Pragma,
9083 Save_Default_SSO => Default_SSO,
9084 Save_Uneval_Old => Uneval_Old,
9085 Component_Alignment_Default => Component_Alignment_Default,
9086 Last_Subprogram_Name => null,
9087 Is_Transient => False,
9088 Node_To_Be_Wrapped => Empty,
9089 Pending_Freeze_Actions => No_List,
9090 Actions_To_Be_Wrapped => (others => No_List),
9091 First_Use_Clause => Empty,
9092 Is_Active_Stack_Base => False,
9093 Previous_Visibility => False,
9094 Locked_Shared_Objects => No_Elist);
9096 if Debug_Flag_W then
9097 Write_Str ("--> new scope: ");
9098 Write_Name (Chars (Current_Scope));
9099 Write_Str (", Id=");
9100 Write_Int (Int (Current_Scope));
9101 Write_Str (", Depth=");
9102 Write_Int (Int (Scope_Stack.Last));
9106 -- Deal with copying flags from the previous scope to this one. This is
9107 -- not necessary if either scope is standard, or if the new scope is a
9110 if S /= Standard_Standard
9111 and then Scope (S) /= Standard_Standard
9112 and then not Is_Child_Unit (S)
9114 if Nkind (E) not in N_Entity then
9118 -- Copy categorization flags from Scope (S) to S, this is not done
9119 -- when Scope (S) is Standard_Standard since propagation is from
9120 -- library unit entity inwards. Copy other relevant attributes as
9121 -- well (Discard_Names in particular).
9123 -- We only propagate inwards for library level entities,
9124 -- inner level subprograms do not inherit the categorization.
9126 if Is_Library_Level_Entity (S) then
9127 Set_Is_Preelaborated (S, Is_Preelaborated (E));
9128 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
9129 Set_Discard_Names (S, Discard_Names (E));
9130 Set_Suppress_Value_Tracking_On_Call
9131 (S, Suppress_Value_Tracking_On_Call (E));
9132 Set_Categorization_From_Scope (E => S, Scop => E);
9136 if Is_Child_Unit (S)
9137 and then Present (E)
9138 and then Is_Package_Or_Generic_Package (E)
9140 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9143 Aux : constant Node_Id :=
9144 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
9146 if Present (Default_Storage_Pool (Aux)) then
9147 Default_Pool := Default_Storage_Pool (Aux);
9153 ---------------------
9154 -- Premature_Usage --
9155 ---------------------
9157 procedure Premature_Usage (N : Node_Id) is
9158 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
9159 E : Entity_Id := Entity (N);
9162 -- Within an instance, the analysis of the actual for a formal object
9163 -- does not see the name of the object itself. This is significant only
9164 -- if the object is an aggregate, where its analysis does not do any
9165 -- name resolution on component associations. (see 4717-008). In such a
9166 -- case, look for the visible homonym on the chain.
9168 if In_Instance and then Present (Homonym (E)) then
9170 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
9176 Set_Etype (N, Etype (E));
9182 when N_Component_Declaration =>
9184 ("component&! cannot be used before end of record declaration",
9187 when N_Parameter_Specification =>
9189 ("formal parameter&! cannot be used before end of specification",
9192 when N_Discriminant_Specification =>
9194 ("discriminant&! cannot be used before end of discriminant part",
9197 when N_Procedure_Specification | N_Function_Specification =>
9199 ("subprogram&! cannot be used before end of its declaration",
9202 when N_Full_Type_Declaration | N_Subtype_Declaration =>
9204 ("type& cannot be used before end of its declaration!", N);
9208 ("object& cannot be used before end of its declaration!", N);
9210 -- If the premature reference appears as the expression in its own
9211 -- declaration, rewrite it to prevent compiler loops in subsequent
9212 -- uses of this mangled declaration in address clauses.
9214 if Nkind (Parent (N)) = N_Object_Declaration then
9215 Set_Entity (N, Any_Id);
9218 end Premature_Usage;
9220 ------------------------
9221 -- Present_System_Aux --
9222 ------------------------
9224 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
9226 Aux_Name : Unit_Name_Type;
9227 Unum : Unit_Number_Type;
9232 function Find_System (C_Unit : Node_Id) return Entity_Id;
9233 -- Scan context clause of compilation unit to find with_clause
9240 function Find_System (C_Unit : Node_Id) return Entity_Id is
9241 With_Clause : Node_Id;
9244 With_Clause := First (Context_Items (C_Unit));
9245 while Present (With_Clause) loop
9246 if (Nkind (With_Clause) = N_With_Clause
9247 and then Chars (Name (With_Clause)) = Name_System)
9248 and then Comes_From_Source (With_Clause)
9259 -- Start of processing for Present_System_Aux
9262 -- The child unit may have been loaded and analyzed already
9264 if Present (System_Aux_Id) then
9267 -- If no previous pragma for System.Aux, nothing to load
9269 elsif No (System_Extend_Unit) then
9272 -- Use the unit name given in the pragma to retrieve the unit.
9273 -- Verify that System itself appears in the context clause of the
9274 -- current compilation. If System is not present, an error will
9275 -- have been reported already.
9278 With_Sys := Find_System (Cunit (Current_Sem_Unit));
9280 The_Unit := Unit (Cunit (Current_Sem_Unit));
9284 (Nkind (The_Unit) = N_Package_Body
9285 or else (Nkind (The_Unit) = N_Subprogram_Body
9286 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
9288 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
9291 if No (With_Sys) and then Present (N) then
9293 -- If we are compiling a subunit, we need to examine its
9294 -- context as well (Current_Sem_Unit is the parent unit);
9296 The_Unit := Parent (N);
9297 while Nkind (The_Unit) /= N_Compilation_Unit loop
9298 The_Unit := Parent (The_Unit);
9301 if Nkind (Unit (The_Unit)) = N_Subunit then
9302 With_Sys := Find_System (The_Unit);
9306 if No (With_Sys) then
9310 Loc := Sloc (With_Sys);
9311 Get_Name_String (Chars (Expression (System_Extend_Unit)));
9312 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
9313 Name_Buffer (1 .. 7) := "system.";
9314 Name_Buffer (Name_Len + 8) := '%';
9315 Name_Buffer (Name_Len + 9) := 's';
9316 Name_Len := Name_Len + 9;
9317 Aux_Name := Name_Find;
9321 (Load_Name => Aux_Name,
9324 Error_Node => With_Sys);
9326 if Unum /= No_Unit then
9327 Semantics (Cunit (Unum));
9329 Defining_Entity (Specification (Unit (Cunit (Unum))));
9332 Make_With_Clause (Loc,
9334 Make_Expanded_Name (Loc,
9335 Chars => Chars (System_Aux_Id),
9337 New_Occurrence_Of (Scope (System_Aux_Id), Loc),
9338 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
9340 Set_Entity (Name (Withn), System_Aux_Id);
9342 Set_Corresponding_Spec (Withn, System_Aux_Id);
9343 Set_First_Name (Withn);
9344 Set_Implicit_With (Withn);
9345 Set_Library_Unit (Withn, Cunit (Unum));
9347 Insert_After (With_Sys, Withn);
9348 Mark_Rewrite_Insertion (Withn);
9349 Set_Context_Installed (Withn);
9353 -- Here if unit load failed
9356 Error_Msg_Name_1 := Name_System;
9357 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
9359 ("extension package `%.%` does not exist",
9360 Opt.System_Extend_Unit);
9364 end Present_System_Aux;
9366 -------------------------
9367 -- Restore_Scope_Stack --
9368 -------------------------
9370 procedure Restore_Scope_Stack
9372 Handle_Use : Boolean := True)
9374 SS_Last : constant Int := Scope_Stack.Last;
9378 -- Restore visibility of previous scope stack, if any, using the list
9379 -- we saved (we use Remove, since this list will not be used again).
9382 Elmt := Last_Elmt (List);
9383 exit when Elmt = No_Elmt;
9384 Set_Is_Immediately_Visible (Node (Elmt));
9385 Remove_Last_Elmt (List);
9388 -- Restore use clauses
9390 if SS_Last >= Scope_Stack.First
9391 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9395 (Scope_Stack.Table (SS_Last).First_Use_Clause,
9396 Force_Installation => True);
9398 end Restore_Scope_Stack;
9400 ----------------------
9401 -- Save_Scope_Stack --
9402 ----------------------
9404 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
9405 -- consuming any memory. That is, Save_Scope_Stack took care of removing
9406 -- from immediate visibility entities and Restore_Scope_Stack took care
9407 -- of restoring their visibility analyzing the context of each entity. The
9408 -- problem of such approach is that it was fragile and caused unexpected
9409 -- visibility problems, and indeed one test was found where there was a
9412 -- Furthermore, the following experiment was carried out:
9414 -- - Save_Scope_Stack was modified to store in an Elist1 all those
9415 -- entities whose attribute Is_Immediately_Visible is modified
9416 -- from True to False.
9418 -- - Restore_Scope_Stack was modified to store in another Elist2
9419 -- all the entities whose attribute Is_Immediately_Visible is
9420 -- modified from False to True.
9422 -- - Extra code was added to verify that all the elements of Elist1
9423 -- are found in Elist2
9425 -- This test shows that there may be more occurrences of this problem which
9426 -- have not yet been detected. As a result, we replaced that approach by
9427 -- the current one in which Save_Scope_Stack returns the list of entities
9428 -- whose visibility is changed, and that list is passed to Restore_Scope_
9429 -- Stack to undo that change. This approach is simpler and safer, although
9430 -- it consumes more memory.
9432 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
9433 Result : constant Elist_Id := New_Elmt_List;
9436 SS_Last : constant Int := Scope_Stack.Last;
9438 procedure Remove_From_Visibility (E : Entity_Id);
9439 -- If E is immediately visible then append it to the result and remove
9440 -- it temporarily from visibility.
9442 ----------------------------
9443 -- Remove_From_Visibility --
9444 ----------------------------
9446 procedure Remove_From_Visibility (E : Entity_Id) is
9448 if Is_Immediately_Visible (E) then
9449 Append_Elmt (E, Result);
9450 Set_Is_Immediately_Visible (E, False);
9452 end Remove_From_Visibility;
9454 -- Start of processing for Save_Scope_Stack
9457 if SS_Last >= Scope_Stack.First
9458 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9461 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
9464 -- If the call is from within a compilation unit, as when called from
9465 -- Rtsfind, make current entries in scope stack invisible while we
9466 -- analyze the new unit.
9468 for J in reverse 0 .. SS_Last loop
9469 exit when Scope_Stack.Table (J).Entity = Standard_Standard
9470 or else No (Scope_Stack.Table (J).Entity);
9472 S := Scope_Stack.Table (J).Entity;
9474 Remove_From_Visibility (S);
9476 E := First_Entity (S);
9477 while Present (E) loop
9478 Remove_From_Visibility (E);
9486 end Save_Scope_Stack;
9492 procedure Set_Use (L : List_Id) is
9498 while Present (Decl) loop
9499 if Nkind (Decl) = N_Use_Package_Clause then
9500 Chain_Use_Clause (Decl);
9501 Use_One_Package (Decl, Name (Decl));
9503 elsif Nkind (Decl) = N_Use_Type_Clause then
9504 Chain_Use_Clause (Decl);
9505 Use_One_Type (Subtype_Mark (Decl));
9514 -----------------------------
9515 -- Update_Use_Clause_Chain --
9516 -----------------------------
9518 procedure Update_Use_Clause_Chain is
9520 procedure Update_Chain_In_Scope (Level : Int);
9521 -- Iterate through one level in the scope stack verifying each use-type
9522 -- clause within said level is used then reset the Current_Use_Clause
9523 -- to a redundant use clause outside of the current ending scope if such
9526 ---------------------------
9527 -- Update_Chain_In_Scope --
9528 ---------------------------
9530 procedure Update_Chain_In_Scope (Level : Int) is
9535 -- Loop through all use clauses within the scope dictated by Level
9537 Curr := Scope_Stack.Table (Level).First_Use_Clause;
9538 while Present (Curr) loop
9540 -- Retrieve the subtype mark or name within the current current
9543 if Nkind (Curr) = N_Use_Type_Clause then
9544 N := Subtype_Mark (Curr);
9549 -- If warnings for unreferenced entities are enabled and the
9550 -- current use clause has not been marked effective.
9552 if Check_Unreferenced
9553 and then Comes_From_Source (Curr)
9554 and then not Is_Effective_Use_Clause (Curr)
9555 and then not In_Instance
9556 and then not In_Inlined_Body
9558 -- We are dealing with a potentially unused use_package_clause
9560 if Nkind (Curr) = N_Use_Package_Clause then
9562 -- Renamings and formal subprograms may cause the associated
9563 -- node to be marked as effective instead of the original.
9565 if not (Present (Associated_Node (N))
9568 (Associated_Node (N)))
9569 and then Is_Effective_Use_Clause
9571 (Associated_Node (N))))
9573 Error_Msg_Node_1 := Entity (N);
9575 ("use clause for package & has no effect?u?",
9579 -- We are dealing with an unused use_type_clause
9582 Error_Msg_Node_1 := Etype (N);
9584 ("use clause for } has no effect?u?", Curr, Etype (N));
9588 -- Verify that we haven't already processed a redundant
9589 -- use_type_clause within the same scope before we move the
9590 -- current use clause up to a previous one for type T.
9592 if Present (Prev_Use_Clause (Curr)) then
9593 Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr));
9596 Next_Use_Clause (Curr);
9598 end Update_Chain_In_Scope;
9600 -- Start of processing for Update_Use_Clause_Chain
9603 Update_Chain_In_Scope (Scope_Stack.Last);
9605 -- Deal with use clauses within the context area if the current
9606 -- scope is a compilation unit.
9608 if Is_Compilation_Unit (Current_Scope)
9609 and then Sloc (Scope_Stack.Table
9610 (Scope_Stack.Last - 1).Entity) = Standard_Location
9612 Update_Chain_In_Scope (Scope_Stack.Last - 1);
9614 end Update_Use_Clause_Chain;
9616 ---------------------
9617 -- Use_One_Package --
9618 ---------------------
9620 procedure Use_One_Package
9622 Pack_Name : Entity_Id := Empty;
9623 Force : Boolean := False)
9625 procedure Note_Redundant_Use (Clause : Node_Id);
9626 -- Mark the name in a use clause as redundant if the corresponding
9627 -- entity is already use-visible. Emit a warning if the use clause comes
9628 -- from source and the proper warnings are enabled.
9630 ------------------------
9631 -- Note_Redundant_Use --
9632 ------------------------
9634 procedure Note_Redundant_Use (Clause : Node_Id) is
9635 Decl : constant Node_Id := Parent (Clause);
9636 Pack_Name : constant Entity_Id := Entity (Clause);
9638 Cur_Use : Node_Id := Current_Use_Clause (Pack_Name);
9639 Prev_Use : Node_Id := Empty;
9640 Redundant : Node_Id := Empty;
9641 -- The Use_Clause which is actually redundant. In the simplest case
9642 -- it is Pack itself, but when we compile a body we install its
9643 -- context before that of its spec, in which case it is the
9644 -- use_clause in the spec that will appear to be redundant, and we
9645 -- want the warning to be placed on the body. Similar complications
9646 -- appear when the redundancy is between a child unit and one of its
9650 -- Could be renamed...
9652 if No (Cur_Use) then
9653 Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name));
9656 Set_Redundant_Use (Clause, True);
9658 -- Do not check for redundant use if clause is generated, or in an
9659 -- instance, or in a predefined unit to avoid misleading warnings
9660 -- that may occur as part of a rtsfind load.
9662 if not Comes_From_Source (Clause)
9664 or else not Warn_On_Redundant_Constructs
9665 or else Is_Predefined_Unit (Current_Sem_Unit)
9670 if not Is_Compilation_Unit (Current_Scope) then
9672 -- If the use_clause is in an inner scope, it is made redundant by
9673 -- some clause in the current context, with one exception: If we
9674 -- are compiling a nested package body, and the use_clause comes
9675 -- from then corresponding spec, the clause is not necessarily
9676 -- fully redundant, so we should not warn. If a warning was
9677 -- warranted, it would have been given when the spec was
9680 if Nkind (Parent (Decl)) = N_Package_Specification then
9682 Package_Spec_Entity : constant Entity_Id :=
9683 Defining_Unit_Name (Parent (Decl));
9685 if In_Package_Body (Package_Spec_Entity) then
9691 Redundant := Clause;
9692 Prev_Use := Cur_Use;
9694 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9696 Cur_Unit : constant Unit_Number_Type :=
9697 Get_Source_Unit (Cur_Use);
9698 New_Unit : constant Unit_Number_Type :=
9699 Get_Source_Unit (Clause);
9704 if Cur_Unit = New_Unit then
9706 -- Redundant clause in same body
9708 Redundant := Clause;
9709 Prev_Use := Cur_Use;
9711 elsif Cur_Unit = Current_Sem_Unit then
9713 -- If the new clause is not in the current unit it has been
9714 -- analyzed first, and it makes the other one redundant.
9715 -- However, if the new clause appears in a subunit, Cur_Unit
9716 -- is still the parent, and in that case the redundant one
9717 -- is the one appearing in the subunit.
9719 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
9720 Redundant := Clause;
9721 Prev_Use := Cur_Use;
9723 -- Most common case: redundant clause in body, original
9724 -- clause in spec. Current scope is spec entity.
9726 elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then
9727 Redundant := Cur_Use;
9731 -- The new clause may appear in an unrelated unit, when
9732 -- the parents of a generic are being installed prior to
9733 -- instantiation. In this case there must be no warning.
9734 -- We detect this case by checking whether the current
9735 -- top of the stack is related to the current
9738 Scop := Current_Scope;
9739 while Present (Scop)
9740 and then Scop /= Standard_Standard
9742 if Is_Compilation_Unit (Scop)
9743 and then not Is_Child_Unit (Scop)
9747 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
9751 Scop := Scope (Scop);
9754 Redundant := Cur_Use;
9758 elsif New_Unit = Current_Sem_Unit then
9759 Redundant := Clause;
9760 Prev_Use := Cur_Use;
9763 -- Neither is the current unit, so they appear in parent or
9764 -- sibling units. Warning will be emitted elsewhere.
9770 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
9771 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
9773 -- Use_clause is in child unit of current unit, and the child unit
9774 -- appears in the context of the body of the parent, so it has
9775 -- been installed first, even though it is the redundant one.
9776 -- Depending on their placement in the context, the visible or the
9777 -- private parts of the two units, either might appear as
9778 -- redundant, but the message has to be on the current unit.
9780 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
9781 Redundant := Cur_Use;
9784 Redundant := Clause;
9785 Prev_Use := Cur_Use;
9788 -- If the new use clause appears in the private part of a parent
9789 -- unit it may appear to be redundant w.r.t. a use clause in a
9790 -- child unit, but the previous use clause was needed in the
9791 -- visible part of the child, and no warning should be emitted.
9793 if Nkind (Parent (Decl)) = N_Package_Specification
9794 and then List_Containing (Decl) =
9795 Private_Declarations (Parent (Decl))
9798 Par : constant Entity_Id :=
9799 Defining_Entity (Parent (Decl));
9800 Spec : constant Node_Id :=
9801 Specification (Unit (Cunit (Current_Sem_Unit)));
9802 Cur_List : constant List_Id := List_Containing (Cur_Use);
9805 if Is_Compilation_Unit (Par)
9806 and then Par /= Cunit_Entity (Current_Sem_Unit)
9808 if Cur_List = Context_Items (Cunit (Current_Sem_Unit))
9809 or else Cur_List = Visible_Declarations (Spec)
9817 -- Finally, if the current use clause is in the context then the
9818 -- clause is redundant when it is nested within the unit.
9820 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
9821 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
9822 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
9824 Redundant := Clause;
9825 Prev_Use := Cur_Use;
9828 if Present (Redundant) and then Parent (Redundant) /= Prev_Use then
9830 -- Make sure we are looking at most-descendant use_package_clause
9831 -- by traversing the chain with Find_First_Use and then verifying
9832 -- there is no scope manipulation via Most_Descendant_Use_Clause.
9834 if Nkind (Prev_Use) = N_Use_Package_Clause
9836 (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit
9837 or else Most_Descendant_Use_Clause
9838 (Prev_Use, Find_First_Use (Prev_Use)) /= Prev_Use)
9840 Prev_Use := Find_First_Use (Prev_Use);
9843 Error_Msg_Sloc := Sloc (Prev_Use);
9844 Error_Msg_NE -- CODEFIX
9845 ("& is already use-visible through previous use_clause #?r?",
9846 Redundant, Pack_Name);
9848 end Note_Redundant_Use;
9852 Current_Instance : Entity_Id := Empty;
9856 Private_With_OK : Boolean := False;
9859 -- Start of processing for Use_One_Package
9862 -- Use_One_Package may have been called recursively to handle an
9863 -- implicit use for a auxiliary system package, so set P accordingly
9864 -- and skip redundancy checks.
9866 if No (Pack_Name) and then Present_System_Aux (N) then
9869 -- Check for redundant use_package_clauses
9872 -- Ignore cases where we are dealing with a non user defined package
9873 -- like Standard_Standard or something other than a valid package.
9875 if not Is_Entity_Name (Pack_Name)
9876 or else No (Entity (Pack_Name))
9877 or else Ekind (Entity (Pack_Name)) /= E_Package
9882 -- When a renaming exists we must check it for redundancy. The
9883 -- original package would have already been seen at this point.
9885 if Present (Renamed_Object (Entity (Pack_Name))) then
9886 P := Renamed_Object (Entity (Pack_Name));
9888 P := Entity (Pack_Name);
9891 -- Check for redundant clauses then set the current use clause for
9892 -- P if were are not "forcing" an installation from a scope
9893 -- reinstallation that is done throughout analysis for various
9897 Note_Redundant_Use (Pack_Name);
9900 Set_Current_Use_Clause (P, N);
9905 -- Warn about detected redundant clauses
9908 and then In_Open_Scopes (P)
9909 and then not Is_Hidden_Open_Scope (P)
9911 if Warn_On_Redundant_Constructs and then P = Current_Scope then
9912 Error_Msg_NE -- CODEFIX
9913 ("& is already use-visible within itself?r?",
9920 -- Set P back to the non-renamed package so that visiblilty of the
9921 -- entities within the package can be properly set below.
9923 P := Entity (Pack_Name);
9927 Set_Current_Use_Clause (P, N);
9929 -- Ada 2005 (AI-50217): Check restriction
9931 if From_Limited_With (P) then
9932 Error_Msg_N ("limited withed package cannot appear in use clause", N);
9935 -- Find enclosing instance, if any
9938 Current_Instance := Current_Scope;
9939 while not Is_Generic_Instance (Current_Instance) loop
9940 Current_Instance := Scope (Current_Instance);
9943 if No (Hidden_By_Use_Clause (N)) then
9944 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
9948 -- If unit is a package renaming, indicate that the renamed package is
9949 -- also in use (the flags on both entities must remain consistent, and a
9950 -- subsequent use of either of them should be recognized as redundant).
9952 if Present (Renamed_Object (P)) then
9953 Set_In_Use (Renamed_Object (P));
9954 Set_Current_Use_Clause (Renamed_Object (P), N);
9955 Real_P := Renamed_Object (P);
9960 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
9961 -- found in the private part of a package specification
9963 if In_Private_Part (Current_Scope)
9964 and then Has_Private_With (P)
9965 and then Is_Child_Unit (Current_Scope)
9966 and then Is_Child_Unit (P)
9967 and then Is_Ancestor_Package (Scope (Current_Scope), P)
9969 Private_With_OK := True;
9972 -- Loop through entities in one package making them potentially
9975 Id := First_Entity (P);
9977 and then (Id /= First_Private_Entity (P)
9978 or else Private_With_OK) -- Ada 2005 (AI-262)
9980 Prev := Current_Entity (Id);
9981 while Present (Prev) loop
9982 if Is_Immediately_Visible (Prev)
9983 and then (not Is_Overloadable (Prev)
9984 or else not Is_Overloadable (Id)
9985 or else (Type_Conformant (Id, Prev)))
9987 if No (Current_Instance) then
9989 -- Potentially use-visible entity remains hidden
9991 goto Next_Usable_Entity;
9993 -- A use clause within an instance hides outer global entities,
9994 -- which are not used to resolve local entities in the
9995 -- instance. Note that the predefined entities in Standard
9996 -- could not have been hidden in the generic by a use clause,
9997 -- and therefore remain visible. Other compilation units whose
9998 -- entities appear in Standard must be hidden in an instance.
10000 -- To determine whether an entity is external to the instance
10001 -- we compare the scope depth of its scope with that of the
10002 -- current instance. However, a generic actual of a subprogram
10003 -- instance is declared in the wrapper package but will not be
10004 -- hidden by a use-visible entity. similarly, an entity that is
10005 -- declared in an enclosing instance will not be hidden by an
10006 -- an entity declared in a generic actual, which can only have
10007 -- been use-visible in the generic and will not have hidden the
10008 -- entity in the generic parent.
10010 -- If Id is called Standard, the predefined package with the
10011 -- same name is in the homonym chain. It has to be ignored
10012 -- because it has no defined scope (being the only entity in
10013 -- the system with this mandated behavior).
10015 elsif not Is_Hidden (Id)
10016 and then Present (Scope (Prev))
10017 and then not Is_Wrapper_Package (Scope (Prev))
10018 and then Scope_Depth (Scope (Prev)) <
10019 Scope_Depth (Current_Instance)
10020 and then (Scope (Prev) /= Standard_Standard
10021 or else Sloc (Prev) > Standard_Location)
10023 if In_Open_Scopes (Scope (Prev))
10024 and then Is_Generic_Instance (Scope (Prev))
10025 and then Present (Associated_Formal_Package (P))
10030 Set_Is_Potentially_Use_Visible (Id);
10031 Set_Is_Immediately_Visible (Prev, False);
10032 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10036 -- A user-defined operator is not use-visible if the predefined
10037 -- operator for the type is immediately visible, which is the case
10038 -- if the type of the operand is in an open scope. This does not
10039 -- apply to user-defined operators that have operands of different
10040 -- types, because the predefined mixed mode operations (multiply
10041 -- and divide) apply to universal types and do not hide anything.
10043 elsif Ekind (Prev) = E_Operator
10044 and then Operator_Matches_Spec (Prev, Id)
10045 and then In_Open_Scopes
10046 (Scope (Base_Type (Etype (First_Formal (Id)))))
10047 and then (No (Next_Formal (First_Formal (Id)))
10048 or else Etype (First_Formal (Id)) =
10049 Etype (Next_Formal (First_Formal (Id)))
10050 or else Chars (Prev) = Name_Op_Expon)
10052 goto Next_Usable_Entity;
10054 -- In an instance, two homonyms may become use_visible through the
10055 -- actuals of distinct formal packages. In the generic, only the
10056 -- current one would have been visible, so make the other one
10057 -- not use_visible.
10059 -- In certain pathological cases it is possible that unrelated
10060 -- homonyms from distinct formal packages may exist in an
10061 -- uninstalled scope. We must test for that here.
10063 elsif Present (Current_Instance)
10064 and then Is_Potentially_Use_Visible (Prev)
10065 and then not Is_Overloadable (Prev)
10066 and then Scope (Id) /= Scope (Prev)
10067 and then Used_As_Generic_Actual (Scope (Prev))
10068 and then Used_As_Generic_Actual (Scope (Id))
10069 and then Is_List_Member (Scope (Prev))
10070 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
10071 Current_Use_Clause (Scope (Id)))
10073 Set_Is_Potentially_Use_Visible (Prev, False);
10074 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10077 Prev := Homonym (Prev);
10080 -- On exit, we know entity is not hidden, unless it is private
10082 if not Is_Hidden (Id)
10083 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
10085 Set_Is_Potentially_Use_Visible (Id);
10087 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
10088 Set_Is_Potentially_Use_Visible (Full_View (Id));
10092 <<Next_Usable_Entity>>
10096 -- Child units are also made use-visible by a use clause, but they may
10097 -- appear after all visible declarations in the parent entity list.
10099 while Present (Id) loop
10100 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
10101 Set_Is_Potentially_Use_Visible (Id);
10107 if Chars (Real_P) = Name_System
10108 and then Scope (Real_P) = Standard_Standard
10109 and then Present_System_Aux (N)
10111 Use_One_Package (N);
10113 end Use_One_Package;
10119 procedure Use_One_Type
10121 Installed : Boolean := False;
10122 Force : Boolean := False)
10124 function Spec_Reloaded_For_Body return Boolean;
10125 -- Determine whether the compilation unit is a package body and the use
10126 -- type clause is in the spec of the same package. Even though the spec
10127 -- was analyzed first, its context is reloaded when analysing the body.
10129 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
10130 -- AI05-150: if the use_type_clause carries the "all" qualifier,
10131 -- class-wide operations of ancestor types are use-visible if the
10132 -- ancestor type is visible.
10134 ----------------------------
10135 -- Spec_Reloaded_For_Body --
10136 ----------------------------
10138 function Spec_Reloaded_For_Body return Boolean is
10140 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
10142 Spec : constant Node_Id :=
10143 Parent (List_Containing (Parent (Id)));
10146 -- Check whether type is declared in a package specification,
10147 -- and current unit is the corresponding package body. The
10148 -- use clauses themselves may be within a nested package.
10151 Nkind (Spec) = N_Package_Specification
10152 and then In_Same_Source_Unit
10153 (Corresponding_Body (Parent (Spec)),
10154 Cunit_Entity (Current_Sem_Unit));
10159 end Spec_Reloaded_For_Body;
10161 -------------------------------
10162 -- Use_Class_Wide_Operations --
10163 -------------------------------
10165 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
10166 function Is_Class_Wide_Operation_Of
10168 T : Entity_Id) return Boolean;
10169 -- Determine whether a subprogram has a class-wide parameter or
10170 -- result that is T'Class.
10172 ---------------------------------
10173 -- Is_Class_Wide_Operation_Of --
10174 ---------------------------------
10176 function Is_Class_Wide_Operation_Of
10178 T : Entity_Id) return Boolean
10180 Formal : Entity_Id;
10183 Formal := First_Formal (Op);
10184 while Present (Formal) loop
10185 if Etype (Formal) = Class_Wide_Type (T) then
10189 Next_Formal (Formal);
10192 if Etype (Op) = Class_Wide_Type (T) then
10197 end Is_Class_Wide_Operation_Of;
10204 -- Start of processing for Use_Class_Wide_Operations
10207 Scop := Scope (Typ);
10208 if not Is_Hidden (Scop) then
10209 Ent := First_Entity (Scop);
10210 while Present (Ent) loop
10211 if Is_Overloadable (Ent)
10212 and then Is_Class_Wide_Operation_Of (Ent, Typ)
10213 and then not Is_Potentially_Use_Visible (Ent)
10215 Set_Is_Potentially_Use_Visible (Ent);
10216 Append_Elmt (Ent, Used_Operations (Parent (Id)));
10223 if Is_Derived_Type (Typ) then
10224 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
10226 end Use_Class_Wide_Operations;
10231 Is_Known_Used : Boolean;
10232 Op_List : Elist_Id;
10235 -- Start of processing for Use_One_Type
10238 if Entity (Id) = Any_Type then
10242 -- It is the type determined by the subtype mark (8.4(8)) whose
10243 -- operations become potentially use-visible.
10245 T := Base_Type (Entity (Id));
10247 -- Either the type itself is used, the package where it is declared is
10248 -- in use or the entity is declared in the current package, thus
10253 and then ((Present (Current_Use_Clause (T))
10254 and then All_Present (Current_Use_Clause (T)))
10255 or else not All_Present (Parent (Id))))
10256 or else In_Use (Scope (T))
10257 or else Scope (T) = Current_Scope;
10259 Set_Redundant_Use (Id,
10260 Is_Known_Used or else Is_Potentially_Use_Visible (T));
10262 if Ekind (T) = E_Incomplete_Type then
10263 Error_Msg_N ("premature usage of incomplete type", Id);
10265 elsif In_Open_Scopes (Scope (T)) then
10268 -- A limited view cannot appear in a use_type_clause. However, an access
10269 -- type whose designated type is limited has the flag but is not itself
10270 -- a limited view unless we only have a limited view of its enclosing
10273 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
10275 ("incomplete type from limited view cannot appear in use clause",
10278 -- If the use clause is redundant, Used_Operations will usually be
10279 -- empty, but we need to set it to empty here in one case: If we are
10280 -- instantiating a generic library unit, then we install the ancestors
10281 -- of that unit in the scope stack, which involves reprocessing use
10282 -- clauses in those ancestors. Such a use clause will typically have a
10283 -- nonempty Used_Operations unless it was redundant in the generic unit,
10284 -- even if it is redundant at the place of the instantiation.
10286 elsif Redundant_Use (Id) then
10288 -- We must avoid incorrectly setting the Current_Use_Clause when we
10289 -- are working with a redundant clause that has already been linked
10290 -- in the Prev_Use_Clause chain, otherwise the chain will break.
10292 if Present (Current_Use_Clause (T))
10293 and then Present (Prev_Use_Clause (Current_Use_Clause (T)))
10294 and then Parent (Id) = Prev_Use_Clause (Current_Use_Clause (T))
10298 Set_Current_Use_Clause (T, Parent (Id));
10301 Set_Used_Operations (Parent (Id), New_Elmt_List);
10303 -- If the subtype mark designates a subtype in a different package,
10304 -- we have to check that the parent type is visible, otherwise the
10305 -- use_type_clause is a no-op. Not clear how to do that???
10308 Set_Current_Use_Clause (T, Parent (Id));
10311 -- If T is tagged, primitive operators on class-wide operands are
10312 -- also deemed available. Note that this is really necessary only
10313 -- in semantics-only mode, because the primitive operators are not
10314 -- fully constructed in this mode, but we do it in all modes for the
10315 -- sake of uniformity, as this should not matter in practice.
10317 if Is_Tagged_Type (T) then
10318 Set_In_Use (Class_Wide_Type (T));
10321 -- Iterate over primitive operations of the type. If an operation is
10322 -- already use_visible, it is the result of a previous use_clause,
10323 -- and already appears on the corresponding entity chain. If the
10324 -- clause is being reinstalled, operations are already use-visible.
10330 Op_List := Collect_Primitive_Operations (T);
10331 Elmt := First_Elmt (Op_List);
10332 while Present (Elmt) loop
10333 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
10334 or else Chars (Node (Elmt)) in Any_Operator_Name)
10335 and then not Is_Hidden (Node (Elmt))
10336 and then not Is_Potentially_Use_Visible (Node (Elmt))
10338 Set_Is_Potentially_Use_Visible (Node (Elmt));
10339 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10341 elsif Ada_Version >= Ada_2012
10342 and then All_Present (Parent (Id))
10343 and then not Is_Hidden (Node (Elmt))
10344 and then not Is_Potentially_Use_Visible (Node (Elmt))
10346 Set_Is_Potentially_Use_Visible (Node (Elmt));
10347 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10354 if Ada_Version >= Ada_2012
10355 and then All_Present (Parent (Id))
10356 and then Is_Tagged_Type (T)
10358 Use_Class_Wide_Operations (T);
10362 -- If warning on redundant constructs, check for unnecessary WITH
10365 and then Warn_On_Redundant_Constructs
10366 and then Is_Known_Used
10368 -- with P; with P; use P;
10369 -- package P is package X is package body X is
10370 -- type T ... use P.T;
10372 -- The compilation unit is the body of X. GNAT first compiles the
10373 -- spec of X, then proceeds to the body. At that point P is marked
10374 -- as use visible. The analysis then reinstalls the spec along with
10375 -- its context. The use clause P.T is now recognized as redundant,
10376 -- but in the wrong context. Do not emit a warning in such cases.
10377 -- Do not emit a warning either if we are in an instance, there is
10378 -- no redundancy between an outer use_clause and one that appears
10379 -- within the generic.
10381 and then not Spec_Reloaded_For_Body
10382 and then not In_Instance
10383 and then not In_Inlined_Body
10385 -- The type already has a use clause
10389 -- Case where we know the current use clause for the type
10391 if Present (Current_Use_Clause (T)) then
10392 Use_Clause_Known : declare
10393 Clause1 : constant Node_Id :=
10394 Find_First_Use (Current_Use_Clause (T));
10395 Clause2 : constant Node_Id := Parent (Id);
10402 -- Start of processing for Use_Clause_Known
10405 -- If both current use_type_clause and the use_type_clause
10406 -- for the type are at the compilation unit level, one of
10407 -- the units must be an ancestor of the other, and the
10408 -- warning belongs on the descendant.
10410 if Nkind (Parent (Clause1)) = N_Compilation_Unit
10412 Nkind (Parent (Clause2)) = N_Compilation_Unit
10414 -- If the unit is a subprogram body that acts as spec,
10415 -- the context clause is shared with the constructed
10416 -- subprogram spec. Clearly there is no redundancy.
10418 if Clause1 = Clause2 then
10422 Unit1 := Unit (Parent (Clause1));
10423 Unit2 := Unit (Parent (Clause2));
10425 -- If both clauses are on same unit, or one is the body
10426 -- of the other, or one of them is in a subunit, report
10427 -- redundancy on the later one.
10429 if Unit1 = Unit2 or else Nkind (Unit1) = N_Subunit then
10430 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10431 Error_Msg_NE -- CODEFIX
10432 ("& is already use-visible through previous "
10433 & "use_type_clause #??", Clause1, T);
10436 elsif Nkind (Unit2) in N_Package_Body | N_Subprogram_Body
10437 and then Nkind (Unit1) /= Nkind (Unit2)
10438 and then Nkind (Unit1) /= N_Subunit
10440 Error_Msg_Sloc := Sloc (Clause1);
10441 Error_Msg_NE -- CODEFIX
10442 ("& is already use-visible through previous "
10443 & "use_type_clause #??", Current_Use_Clause (T), T);
10447 -- There is a redundant use_type_clause in a child unit.
10448 -- Determine which of the units is more deeply nested.
10449 -- If a unit is a package instance, retrieve the entity
10450 -- and its scope from the instance spec.
10452 Ent1 := Entity_Of_Unit (Unit1);
10453 Ent2 := Entity_Of_Unit (Unit2);
10455 if Scope (Ent2) = Standard_Standard then
10456 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10459 elsif Scope (Ent1) = Standard_Standard then
10460 Error_Msg_Sloc := Sloc (Id);
10463 -- If both units are child units, we determine which one
10464 -- is the descendant by the scope distance to the
10465 -- ultimate parent unit.
10473 S1 := Scope (Ent1);
10474 S2 := Scope (Ent2);
10476 and then Present (S2)
10477 and then S1 /= Standard_Standard
10478 and then S2 /= Standard_Standard
10484 if S1 = Standard_Standard then
10485 Error_Msg_Sloc := Sloc (Id);
10488 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10494 if Parent (Id) /= Err_No then
10495 if Most_Descendant_Use_Clause
10496 (Err_No, Parent (Id)) = Parent (Id)
10498 Error_Msg_Sloc := Sloc (Err_No);
10499 Err_No := Parent (Id);
10502 Error_Msg_NE -- CODEFIX
10503 ("& is already use-visible through previous "
10504 & "use_type_clause #??", Err_No, Id);
10507 -- Case where current use_type_clause and use_type_clause
10508 -- for the type are not both at the compilation unit level.
10509 -- In this case we don't have location information.
10512 Error_Msg_NE -- CODEFIX
10513 ("& is already use-visible through previous "
10514 & "use_type_clause??", Id, T);
10516 end Use_Clause_Known;
10518 -- Here if Current_Use_Clause is not set for T, another case where
10519 -- we do not have the location information available.
10522 Error_Msg_NE -- CODEFIX
10523 ("& is already use-visible through previous "
10524 & "use_type_clause??", Id, T);
10527 -- The package where T is declared is already used
10529 elsif In_Use (Scope (T)) then
10530 -- Due to expansion of contracts we could be attempting to issue
10531 -- a spurious warning - so verify there is a previous use clause.
10533 if Current_Use_Clause (Scope (T)) /=
10534 Find_First_Use (Current_Use_Clause (Scope (T)))
10537 Sloc (Find_First_Use (Current_Use_Clause (Scope (T))));
10538 Error_Msg_NE -- CODEFIX
10539 ("& is already use-visible through package use clause #??",
10543 -- The current scope is the package where T is declared
10546 Error_Msg_Node_2 := Scope (T);
10547 Error_Msg_NE -- CODEFIX
10548 ("& is already use-visible inside package &??", Id, T);
10557 procedure Write_Info is
10558 Id : Entity_Id := First_Entity (Current_Scope);
10561 -- No point in dumping standard entities
10563 if Current_Scope = Standard_Standard then
10567 Write_Str ("========================================================");
10569 Write_Str (" Defined Entities in ");
10570 Write_Name (Chars (Current_Scope));
10572 Write_Str ("========================================================");
10576 Write_Str ("-- none --");
10580 while Present (Id) loop
10581 Write_Entity_Info (Id, " ");
10586 if Scope (Current_Scope) = Standard_Standard then
10588 -- Print information on the current unit itself
10590 Write_Entity_Info (Current_Scope, " ");
10603 for J in reverse 1 .. Scope_Stack.Last loop
10604 S := Scope_Stack.Table (J).Entity;
10605 Write_Int (Int (S));
10606 Write_Str (" === ");
10607 Write_Name (Chars (S));
10616 procedure we (S : Entity_Id) is
10619 E := First_Entity (S);
10620 while Present (E) loop
10621 Write_Int (Int (E));
10622 Write_Str (" === ");
10623 Write_Name (Chars (E));