2014-12-12 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / sem_ch8.adb
blob4edeac9afde50f831bfc707bf6127402cddaaa76
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 8 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Tss; use Exp_Tss;
32 with Exp_Util; use Exp_Util;
33 with Fname; use Fname;
34 with Freeze; use Freeze;
35 with Impunit; use Impunit;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Namet.Sp; use Namet.Sp;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch4; use Sem_Ch4;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch12; use Sem_Ch12;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sem_Type; use Sem_Type;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinfo.CN; use Sinfo.CN;
66 with Snames; use Snames;
67 with Style; use Style;
68 with Table;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
73 package body Sem_Ch8 is
75 ------------------------------------
76 -- Visibility and Name Resolution --
77 ------------------------------------
79 -- This package handles name resolution and the collection of possible
80 -- interpretations for overloaded names, prior to overload resolution.
82 -- Name resolution is the process that establishes a mapping between source
83 -- identifiers and the entities they denote at each point in the program.
84 -- Each entity is represented by a defining occurrence. Each identifier
85 -- that denotes an entity points to the corresponding defining occurrence.
86 -- This is the entity of the applied occurrence. Each occurrence holds
87 -- an index into the names table, where source identifiers are stored.
89 -- Each entry in the names table for an identifier or designator uses the
90 -- Info pointer to hold a link to the currently visible entity that has
91 -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
92 -- in package Sem_Util). The visibility is initialized at the beginning of
93 -- semantic processing to make entities in package Standard immediately
94 -- visible. The visibility table is used in a more subtle way when
95 -- compiling subunits (see below).
97 -- Entities that have the same name (i.e. homonyms) are chained. In the
98 -- case of overloaded entities, this chain holds all the possible meanings
99 -- of a given identifier. The process of overload resolution uses type
100 -- information to select from this chain the unique meaning of a given
101 -- identifier.
103 -- Entities are also chained in their scope, through the Next_Entity link.
104 -- As a consequence, the name space is organized as a sparse matrix, where
105 -- each row corresponds to a scope, and each column to a source identifier.
106 -- Open scopes, that is to say scopes currently being compiled, have their
107 -- corresponding rows of entities in order, innermost scope first.
109 -- The scopes of packages that are mentioned in context clauses appear in
110 -- no particular order, interspersed among open scopes. This is because
111 -- in the course of analyzing the context of a compilation, a package
112 -- declaration is first an open scope, and subsequently an element of the
113 -- context. If subunits or child units are present, a parent unit may
114 -- appear under various guises at various times in the compilation.
116 -- When the compilation of the innermost scope is complete, the entities
117 -- defined therein are no longer visible. If the scope is not a package
118 -- declaration, these entities are never visible subsequently, and can be
119 -- removed from visibility chains. If the scope is a package declaration,
120 -- its visible declarations may still be accessible. Therefore the entities
121 -- defined in such a scope are left on the visibility chains, and only
122 -- their visibility (immediately visibility or potential use-visibility)
123 -- is affected.
125 -- The ordering of homonyms on their chain does not necessarily follow
126 -- the order of their corresponding scopes on the scope stack. For
127 -- example, if package P and the enclosing scope both contain entities
128 -- named E, then when compiling the package body the chain for E will
129 -- hold the global entity first, and the local one (corresponding to
130 -- the current inner scope) next. As a result, name resolution routines
131 -- do not assume any relative ordering of the homonym chains, either
132 -- for scope nesting or to order of appearance of context clauses.
134 -- When compiling a child unit, entities in the parent scope are always
135 -- immediately visible. When compiling the body of a child unit, private
136 -- entities in the parent must also be made immediately visible. There
137 -- are separate routines to make the visible and private declarations
138 -- visible at various times (see package Sem_Ch7).
140 -- +--------+ +-----+
141 -- | In use |-------->| EU1 |-------------------------->
142 -- +--------+ +-----+
143 -- | |
144 -- +--------+ +-----+ +-----+
145 -- | Stand. |---------------->| ES1 |--------------->| ES2 |--->
146 -- +--------+ +-----+ +-----+
147 -- | |
148 -- +---------+ | +-----+
149 -- | with'ed |------------------------------>| EW2 |--->
150 -- +---------+ | +-----+
151 -- | |
152 -- +--------+ +-----+ +-----+
153 -- | Scope2 |---------------->| E12 |--------------->| E22 |--->
154 -- +--------+ +-----+ +-----+
155 -- | |
156 -- +--------+ +-----+ +-----+
157 -- | Scope1 |---------------->| E11 |--------------->| E12 |--->
158 -- +--------+ +-----+ +-----+
159 -- ^ | |
160 -- | | |
161 -- | +---------+ | |
162 -- | | with'ed |----------------------------------------->
163 -- | +---------+ | |
164 -- | | |
165 -- Scope stack | |
166 -- (innermost first) | |
167 -- +----------------------------+
168 -- Names table => | Id1 | | | | Id2 |
169 -- +----------------------------+
171 -- Name resolution must deal with several syntactic forms: simple names,
172 -- qualified names, indexed names, and various forms of calls.
174 -- Each identifier points to an entry in the names table. The resolution
175 -- of a simple name consists in traversing the homonym chain, starting
176 -- from the names table. If an entry is immediately visible, it is the one
177 -- designated by the identifier. If only potentially use-visible entities
178 -- are on the chain, we must verify that they do not hide each other. If
179 -- the entity we find is overloadable, we collect all other overloadable
180 -- entities on the chain as long as they are not hidden.
182 -- To resolve expanded names, we must find the entity at the intersection
183 -- of the entity chain for the scope (the prefix) and the homonym chain
184 -- for the selector. In general, homonym chains will be much shorter than
185 -- entity chains, so it is preferable to start from the names table as
186 -- well. If the entity found is overloadable, we must collect all other
187 -- interpretations that are defined in the scope denoted by the prefix.
189 -- For records, protected types, and tasks, their local entities are
190 -- removed from visibility chains on exit from the corresponding scope.
191 -- From the outside, these entities are always accessed by selected
192 -- notation, and the entity chain for the record type, protected type,
193 -- etc. is traversed sequentially in order to find the designated entity.
195 -- The discriminants of a type and the operations of a protected type or
196 -- task are unchained on exit from the first view of the type, (such as
197 -- a private or incomplete type declaration, or a protected type speci-
198 -- fication) and re-chained when compiling the second view.
200 -- In the case of operators, we do not make operators on derived types
201 -- explicit. As a result, the notation P."+" may denote either a user-
202 -- defined function with name "+", or else an implicit declaration of the
203 -- operator "+" in package P. The resolution of expanded names always
204 -- tries to resolve an operator name as such an implicitly defined entity,
205 -- in addition to looking for explicit declarations.
207 -- All forms of names that denote entities (simple names, expanded names,
208 -- character literals in some cases) have a Entity attribute, which
209 -- identifies the entity denoted by the name.
211 ---------------------
212 -- The Scope Stack --
213 ---------------------
215 -- The Scope stack keeps track of the scopes currently been compiled.
216 -- Every entity that contains declarations (including records) is placed
217 -- on the scope stack while it is being processed, and removed at the end.
218 -- Whenever a non-package scope is exited, the entities defined therein
219 -- are removed from the visibility table, so that entities in outer scopes
220 -- become visible (see previous description). On entry to Sem, the scope
221 -- stack only contains the package Standard. As usual, subunits complicate
222 -- this picture ever so slightly.
224 -- The Rtsfind mechanism can force a call to Semantics while another
225 -- compilation is in progress. The unit retrieved by Rtsfind must be
226 -- compiled in its own context, and has no access to the visibility of
227 -- the unit currently being compiled. The procedures Save_Scope_Stack and
228 -- Restore_Scope_Stack make entities in current open scopes invisible
229 -- before compiling the retrieved unit, and restore the compilation
230 -- environment afterwards.
232 ------------------------
233 -- Compiling subunits --
234 ------------------------
236 -- Subunits must be compiled in the environment of the corresponding stub,
237 -- that is to say with the same visibility into the parent (and its
238 -- context) that is available at the point of the stub declaration, but
239 -- with the additional visibility provided by the context clause of the
240 -- subunit itself. As a result, compilation of a subunit forces compilation
241 -- of the parent (see description in lib-). At the point of the stub
242 -- declaration, Analyze is called recursively to compile the proper body of
243 -- the subunit, but without reinitializing the names table, nor the scope
244 -- stack (i.e. standard is not pushed on the stack). In this fashion the
245 -- context of the subunit is added to the context of the parent, and the
246 -- subunit is compiled in the correct environment. Note that in the course
247 -- of processing the context of a subunit, Standard will appear twice on
248 -- the scope stack: once for the parent of the subunit, and once for the
249 -- unit in the context clause being compiled. However, the two sets of
250 -- entities are not linked by homonym chains, so that the compilation of
251 -- any context unit happens in a fresh visibility environment.
253 -------------------------------
254 -- Processing of USE Clauses --
255 -------------------------------
257 -- Every defining occurrence has a flag indicating if it is potentially use
258 -- visible. Resolution of simple names examines this flag. The processing
259 -- of use clauses consists in setting this flag on all visible entities
260 -- defined in the corresponding package. On exit from the scope of the use
261 -- clause, the corresponding flag must be reset. However, a package may
262 -- appear in several nested use clauses (pathological but legal, alas)
263 -- which forces us to use a slightly more involved scheme:
265 -- a) The defining occurrence for a package holds a flag -In_Use- to
266 -- indicate that it is currently in the scope of a use clause. If a
267 -- redundant use clause is encountered, then the corresponding occurrence
268 -- of the package name is flagged -Redundant_Use-.
270 -- b) On exit from a scope, the use clauses in its declarative part are
271 -- scanned. The visibility flag is reset in all entities declared in
272 -- package named in a use clause, as long as the package is not flagged
273 -- as being in a redundant use clause (in which case the outer use
274 -- clause is still in effect, and the direct visibility of its entities
275 -- must be retained).
277 -- Note that entities are not removed from their homonym chains on exit
278 -- from the package specification. A subsequent use clause does not need
279 -- to rechain the visible entities, but only to establish their direct
280 -- visibility.
282 -----------------------------------
283 -- Handling private declarations --
284 -----------------------------------
286 -- The principle that each entity has a single defining occurrence clashes
287 -- with the presence of two separate definitions for private types: the
288 -- first is the private type declaration, and second is the full type
289 -- declaration. It is important that all references to the type point to
290 -- the same defining occurrence, namely the first one. To enforce the two
291 -- separate views of the entity, the corresponding information is swapped
292 -- between the two declarations. Outside of the package, the defining
293 -- occurrence only contains the private declaration information, while in
294 -- the private part and the body of the package the defining occurrence
295 -- contains the full declaration. To simplify the swap, the defining
296 -- occurrence that currently holds the private declaration points to the
297 -- full declaration. During semantic processing the defining occurrence
298 -- also points to a list of private dependents, that is to say access types
299 -- or composite types whose designated types or component types are
300 -- subtypes or derived types of the private type in question. After the
301 -- full declaration has been seen, the private dependents are updated to
302 -- indicate that they have full definitions.
304 ------------------------------------
305 -- Handling of Undefined Messages --
306 ------------------------------------
308 -- In normal mode, only the first use of an undefined identifier generates
309 -- a message. The table Urefs is used to record error messages that have
310 -- been issued so that second and subsequent ones do not generate further
311 -- messages. However, the second reference causes text to be added to the
312 -- original undefined message noting "(more references follow)". The
313 -- full error list option (-gnatf) forces messages to be generated for
314 -- every reference and disconnects the use of this table.
316 type Uref_Entry is record
317 Node : Node_Id;
318 -- Node for identifier for which original message was posted. The
319 -- Chars field of this identifier is used to detect later references
320 -- to the same identifier.
322 Err : Error_Msg_Id;
323 -- Records error message Id of original undefined message. Reset to
324 -- No_Error_Msg after the second occurrence, where it is used to add
325 -- text to the original message as described above.
327 Nvis : Boolean;
328 -- Set if the message is not visible rather than undefined
330 Loc : Source_Ptr;
331 -- Records location of error message. Used to make sure that we do
332 -- not consider a, b : undefined as two separate instances, which
333 -- would otherwise happen, since the parser converts this sequence
334 -- to a : undefined; b : undefined.
336 end record;
338 package Urefs is new Table.Table (
339 Table_Component_Type => Uref_Entry,
340 Table_Index_Type => Nat,
341 Table_Low_Bound => 1,
342 Table_Initial => 10,
343 Table_Increment => 100,
344 Table_Name => "Urefs");
346 Candidate_Renaming : Entity_Id;
347 -- Holds a candidate interpretation that appears in a subprogram renaming
348 -- declaration and does not match the given specification, but matches at
349 -- least on the first formal. Allows better error message when given
350 -- specification omits defaulted parameters, a common error.
352 -----------------------
353 -- Local Subprograms --
354 -----------------------
356 procedure Analyze_Generic_Renaming
357 (N : Node_Id;
358 K : Entity_Kind);
359 -- Common processing for all three kinds of generic renaming declarations.
360 -- Enter new name and indicate that it renames the generic unit.
362 procedure Analyze_Renamed_Character
363 (N : Node_Id;
364 New_S : Entity_Id;
365 Is_Body : Boolean);
366 -- Renamed entity is given by a character literal, which must belong
367 -- to the return type of the new entity. Is_Body indicates whether the
368 -- declaration is a renaming_as_body. If the original declaration has
369 -- already been frozen (because of an intervening body, e.g.) the body of
370 -- the function must be built now. The same applies to the following
371 -- various renaming procedures.
373 procedure Analyze_Renamed_Dereference
374 (N : Node_Id;
375 New_S : Entity_Id;
376 Is_Body : Boolean);
377 -- Renamed entity is given by an explicit dereference. Prefix must be a
378 -- conformant access_to_subprogram type.
380 procedure Analyze_Renamed_Entry
381 (N : Node_Id;
382 New_S : Entity_Id;
383 Is_Body : Boolean);
384 -- If the renamed entity in a subprogram renaming is an entry or protected
385 -- subprogram, build a body for the new entity whose only statement is a
386 -- call to the renamed entity.
388 procedure Analyze_Renamed_Family_Member
389 (N : Node_Id;
390 New_S : Entity_Id;
391 Is_Body : Boolean);
392 -- Used when the renamed entity is an indexed component. The prefix must
393 -- denote an entry family.
395 procedure Analyze_Renamed_Primitive_Operation
396 (N : Node_Id;
397 New_S : Entity_Id;
398 Is_Body : Boolean);
399 -- If the renamed entity in a subprogram renaming is a primitive operation
400 -- or a class-wide operation in prefix form, save the target object,
401 -- which must be added to the list of actuals in any subsequent call.
402 -- The renaming operation is intrinsic because the compiler must in
403 -- fact generate a wrapper for it (6.3.1 (10 1/2)).
405 function Applicable_Use (Pack_Name : Node_Id) return Boolean;
406 -- Common code to Use_One_Package and Set_Use, to determine whether use
407 -- clause must be processed. Pack_Name is an entity name that references
408 -- the package in question.
410 procedure Attribute_Renaming (N : Node_Id);
411 -- Analyze renaming of attribute as subprogram. The renaming declaration N
412 -- is rewritten as a subprogram body that returns the attribute reference
413 -- applied to the formals of the function.
415 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
416 -- Set Entity, with style check if need be. For a discriminant reference,
417 -- replace by the corresponding discriminal, i.e. the parameter of the
418 -- initialization procedure that corresponds to the discriminant.
420 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
421 -- A renaming_as_body may occur after the entity of the original decla-
422 -- ration has been frozen. In that case, the body of the new entity must
423 -- be built now, because the usual mechanism of building the renamed
424 -- body at the point of freezing will not work. Subp is the subprogram
425 -- for which N provides the Renaming_As_Body.
427 procedure Check_In_Previous_With_Clause
428 (N : Node_Id;
429 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 Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
473 -- Find a type derived from Character or Wide_Character in the prefix of N.
474 -- Used to resolved qualified names whose selector is a character literal.
476 function Has_Private_With (E : Entity_Id) return Boolean;
477 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
478 -- private with on E.
480 procedure Find_Expanded_Name (N : Node_Id);
481 -- The input is a selected component known to be an expanded name. Verify
482 -- legality of selector given the scope denoted by prefix, and change node
483 -- N into a expanded name with a properly set Entity field.
485 function Find_Renamed_Entity
486 (N : Node_Id;
487 Nam : Node_Id;
488 New_S : Entity_Id;
489 Is_Actual : Boolean := False) return Entity_Id;
490 -- Find the renamed entity that corresponds to the given parameter profile
491 -- in a subprogram renaming declaration. The renamed entity may be an
492 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
493 -- indicates that the renaming is the one generated for an actual subpro-
494 -- gram in an instance, for which special visibility checks apply.
496 function Has_Implicit_Operator (N : Node_Id) return Boolean;
497 -- N is an expanded name whose selector is an operator name (e.g. P."+").
498 -- declarative part contains an implicit declaration of an operator if it
499 -- has a declaration of a type to which one of the predefined operators
500 -- apply. The existence of this routine is an implementation artifact. A
501 -- more straightforward but more space-consuming choice would be to make
502 -- all inherited operators explicit in the symbol table.
504 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
505 -- A subprogram defined by a renaming declaration inherits the parameter
506 -- profile of the renamed entity. The subtypes given in the subprogram
507 -- specification are discarded and replaced with those of the renamed
508 -- subprogram, which are then used to recheck the default values.
510 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean;
511 -- Prefix is appropriate for record if it is of a record type, or an access
512 -- to such.
514 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean;
515 -- True if it is of a task type, a protected type, or else an access to one
516 -- of these types.
518 procedure Note_Redundant_Use (Clause : Node_Id);
519 -- Mark the name in a use clause as redundant if the corresponding entity
520 -- is already use-visible. Emit a warning if the use clause comes from
521 -- source and the proper warnings are enabled.
523 procedure Premature_Usage (N : Node_Id);
524 -- Diagnose usage of an entity before it is visible
526 procedure Use_One_Package (P : Entity_Id; N : Node_Id);
527 -- Make visible entities declared in package P potentially use-visible
528 -- in the current context. Also used in the analysis of subunits, when
529 -- re-installing use clauses of parent units. N is the use_clause that
530 -- names P (and possibly other packages).
532 procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False);
533 -- Id is the subtype mark from a use type clause. This procedure makes
534 -- the primitive operators of the type potentially use-visible. The
535 -- boolean flag Installed indicates that the clause is being reinstalled
536 -- after previous analysis, and primitive operations are already chained
537 -- on the Used_Operations list of the clause.
539 procedure Write_Info;
540 -- Write debugging information on entities declared in current scope
542 --------------------------------
543 -- Analyze_Exception_Renaming --
544 --------------------------------
546 -- The language only allows a single identifier, but the tree holds an
547 -- identifier list. The parser has already issued an error message if
548 -- there is more than one element in the list.
550 procedure Analyze_Exception_Renaming (N : Node_Id) is
551 Id : constant Node_Id := Defining_Identifier (N);
552 Nam : constant Node_Id := Name (N);
554 begin
555 Check_SPARK_05_Restriction ("exception renaming is not allowed", N);
557 Enter_Name (Id);
558 Analyze (Nam);
560 Set_Ekind (Id, E_Exception);
561 Set_Etype (Id, Standard_Exception_Type);
562 Set_Is_Pure (Id, Is_Pure (Current_Scope));
564 if not Is_Entity_Name (Nam)
565 or else Ekind (Entity (Nam)) /= E_Exception
566 then
567 Error_Msg_N ("invalid exception name in renaming", Nam);
568 else
569 if Present (Renamed_Object (Entity (Nam))) then
570 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
571 else
572 Set_Renamed_Object (Id, Entity (Nam));
573 end if;
575 -- An exception renaming is Ghost if the renamed entity is Ghost or
576 -- the construct appears within a Ghost scope.
578 if Is_Ghost_Entity (Entity (Nam)) or else Within_Ghost_Scope then
579 Set_Is_Ghost_Entity (Id);
580 end if;
581 end if;
583 -- Implementation-defined aspect specifications can appear in a renaming
584 -- declaration, but not language-defined ones. The call to procedure
585 -- Analyze_Aspect_Specifications will take care of this error check.
587 if Has_Aspects (N) then
588 Analyze_Aspect_Specifications (N, Id);
589 end if;
590 end Analyze_Exception_Renaming;
592 ---------------------------
593 -- Analyze_Expanded_Name --
594 ---------------------------
596 procedure Analyze_Expanded_Name (N : Node_Id) is
597 begin
598 -- If the entity pointer is already set, this is an internal node, or a
599 -- node that is analyzed more than once, after a tree modification. In
600 -- such a case there is no resolution to perform, just set the type. For
601 -- completeness, analyze prefix as well.
603 if Present (Entity (N)) then
604 if Is_Type (Entity (N)) then
605 Set_Etype (N, Entity (N));
606 else
607 Set_Etype (N, Etype (Entity (N)));
608 end if;
610 Analyze (Prefix (N));
611 return;
612 else
613 Find_Expanded_Name (N);
614 end if;
616 Analyze_Dimension (N);
617 end Analyze_Expanded_Name;
619 ---------------------------------------
620 -- Analyze_Generic_Function_Renaming --
621 ---------------------------------------
623 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
624 begin
625 Analyze_Generic_Renaming (N, E_Generic_Function);
626 end Analyze_Generic_Function_Renaming;
628 --------------------------------------
629 -- Analyze_Generic_Package_Renaming --
630 --------------------------------------
632 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
633 begin
634 -- Test for the Text_IO special unit case here, since we may be renaming
635 -- one of the subpackages of Text_IO, then join common routine.
637 Check_Text_IO_Special_Unit (Name (N));
639 Analyze_Generic_Renaming (N, E_Generic_Package);
640 end Analyze_Generic_Package_Renaming;
642 ----------------------------------------
643 -- Analyze_Generic_Procedure_Renaming --
644 ----------------------------------------
646 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
647 begin
648 Analyze_Generic_Renaming (N, E_Generic_Procedure);
649 end Analyze_Generic_Procedure_Renaming;
651 ------------------------------
652 -- Analyze_Generic_Renaming --
653 ------------------------------
655 procedure Analyze_Generic_Renaming
656 (N : Node_Id;
657 K : Entity_Kind)
659 New_P : constant Entity_Id := Defining_Entity (N);
660 Old_P : Entity_Id;
661 Inst : Boolean := False; -- prevent junk warning
663 begin
664 if Name (N) = Error then
665 return;
666 end if;
668 Check_SPARK_05_Restriction ("generic renaming is not allowed", N);
670 Generate_Definition (New_P);
672 if Current_Scope /= Standard_Standard then
673 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
674 end if;
676 if Nkind (Name (N)) = N_Selected_Component then
677 Check_Generic_Child_Unit (Name (N), Inst);
678 else
679 Analyze (Name (N));
680 end if;
682 if not Is_Entity_Name (Name (N)) then
683 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
684 Old_P := Any_Id;
685 else
686 Old_P := Entity (Name (N));
687 end if;
689 Enter_Name (New_P);
690 Set_Ekind (New_P, K);
692 if Etype (Old_P) = Any_Type then
693 null;
695 elsif Ekind (Old_P) /= K then
696 Error_Msg_N ("invalid generic unit name", Name (N));
698 else
699 if Present (Renamed_Object (Old_P)) then
700 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
701 else
702 Set_Renamed_Object (New_P, Old_P);
703 end if;
705 Set_Is_Pure (New_P, Is_Pure (Old_P));
706 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
708 Set_Etype (New_P, Etype (Old_P));
709 Set_Has_Completion (New_P);
711 -- An generic renaming is Ghost if the renamed entity is Ghost or the
712 -- construct appears within a Ghost scope.
714 if Is_Ghost_Entity (Old_P) or else Within_Ghost_Scope then
715 Set_Is_Ghost_Entity (New_P);
716 end if;
718 if In_Open_Scopes (Old_P) then
719 Error_Msg_N ("within its scope, generic denotes its instance", N);
720 end if;
722 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
723 -- renamings and subsequent instantiations of Unchecked_Conversion.
725 if Ekind_In (Old_P, E_Generic_Function, E_Generic_Procedure) then
726 Set_Is_Intrinsic_Subprogram
727 (New_P, Is_Intrinsic_Subprogram (Old_P));
728 end if;
730 Check_Library_Unit_Renaming (N, Old_P);
731 end if;
733 -- Implementation-defined aspect specifications can appear in a renaming
734 -- declaration, but not language-defined ones. The call to procedure
735 -- Analyze_Aspect_Specifications will take care of this error check.
737 if Has_Aspects (N) then
738 Analyze_Aspect_Specifications (N, New_P);
739 end if;
740 end Analyze_Generic_Renaming;
742 -----------------------------
743 -- Analyze_Object_Renaming --
744 -----------------------------
746 procedure Analyze_Object_Renaming (N : Node_Id) is
747 Loc : constant Source_Ptr := Sloc (N);
748 Id : constant Entity_Id := Defining_Identifier (N);
749 Dec : Node_Id;
750 Nam : constant Node_Id := Name (N);
751 T : Entity_Id;
752 T2 : Entity_Id;
754 procedure Check_Constrained_Object;
755 -- If the nominal type is unconstrained but the renamed object is
756 -- constrained, as can happen with renaming an explicit dereference or
757 -- a function return, build a constrained subtype from the object. If
758 -- the renaming is for a formal in an accept statement, the analysis
759 -- has already established its actual subtype. This is only relevant
760 -- if the renamed object is an explicit dereference.
762 function In_Generic_Scope (E : Entity_Id) return Boolean;
763 -- Determine whether entity E is inside a generic cope
765 ------------------------------
766 -- Check_Constrained_Object --
767 ------------------------------
769 procedure Check_Constrained_Object is
770 Typ : constant Entity_Id := Etype (Nam);
771 Subt : Entity_Id;
773 begin
774 if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference)
775 and then Is_Composite_Type (Etype (Nam))
776 and then not Is_Constrained (Etype (Nam))
777 and then not Has_Unknown_Discriminants (Etype (Nam))
778 and then Expander_Active
779 then
780 -- If Actual_Subtype is already set, nothing to do
782 if Ekind_In (Id, E_Variable, E_Constant)
783 and then Present (Actual_Subtype (Id))
784 then
785 null;
787 -- A renaming of an unchecked union has no actual subtype
789 elsif Is_Unchecked_Union (Typ) then
790 null;
792 -- If a record is limited its size is invariant. This is the case
793 -- in particular with record types with an access discirminant
794 -- that are used in iterators. This is an optimization, but it
795 -- also prevents typing anomalies when the prefix is further
796 -- expanded. Limited types with discriminants are included.
798 elsif Is_Limited_Record (Typ)
799 or else
800 (Ekind (Typ) = E_Limited_Private_Type
801 and then Has_Discriminants (Typ)
802 and then Is_Access_Type (Etype (First_Discriminant (Typ))))
803 then
804 null;
806 else
807 Subt := Make_Temporary (Loc, 'T');
808 Remove_Side_Effects (Nam);
809 Insert_Action (N,
810 Make_Subtype_Declaration (Loc,
811 Defining_Identifier => Subt,
812 Subtype_Indication =>
813 Make_Subtype_From_Expr (Nam, Typ)));
814 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
815 Set_Etype (Nam, Subt);
817 -- Freeze subtype at once, to prevent order of elaboration
818 -- issues in the backend. The renamed object exists, so its
819 -- type is already frozen in any case.
821 Freeze_Before (N, Subt);
822 end if;
823 end if;
824 end Check_Constrained_Object;
826 ----------------------
827 -- In_Generic_Scope --
828 ----------------------
830 function In_Generic_Scope (E : Entity_Id) return Boolean is
831 S : Entity_Id;
833 begin
834 S := Scope (E);
835 while Present (S) and then S /= Standard_Standard loop
836 if Is_Generic_Unit (S) then
837 return True;
838 end if;
840 S := Scope (S);
841 end loop;
843 return False;
844 end In_Generic_Scope;
846 -- Start of processing for Analyze_Object_Renaming
848 begin
849 if Nam = Error then
850 return;
851 end if;
853 Check_SPARK_05_Restriction ("object renaming is not allowed", N);
855 Set_Is_Pure (Id, Is_Pure (Current_Scope));
856 Enter_Name (Id);
858 -- The renaming of a component that depends on a discriminant requires
859 -- an actual subtype, because in subsequent use of the object Gigi will
860 -- be unable to locate the actual bounds. This explicit step is required
861 -- when the renaming is generated in removing side effects of an
862 -- already-analyzed expression.
864 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
865 T := Etype (Nam);
866 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
868 if Present (Dec) then
869 Insert_Action (N, Dec);
870 T := Defining_Identifier (Dec);
871 Set_Etype (Nam, T);
872 end if;
874 -- Complete analysis of the subtype mark in any case, for ASIS use
876 if Present (Subtype_Mark (N)) then
877 Find_Type (Subtype_Mark (N));
878 end if;
880 elsif Present (Subtype_Mark (N)) then
881 Find_Type (Subtype_Mark (N));
882 T := Entity (Subtype_Mark (N));
883 Analyze (Nam);
885 -- Reject renamings of conversions unless the type is tagged, or
886 -- the conversion is implicit (which can occur for cases of anonymous
887 -- access types in Ada 2012).
889 if Nkind (Nam) = N_Type_Conversion
890 and then Comes_From_Source (Nam)
891 and then not Is_Tagged_Type (T)
892 then
893 Error_Msg_N
894 ("renaming of conversion only allowed for tagged types", Nam);
895 end if;
897 Resolve (Nam, T);
899 -- If the renamed object is a function call of a limited type,
900 -- the expansion of the renaming is complicated by the presence
901 -- of various temporaries and subtypes that capture constraints
902 -- of the renamed object. Rewrite node as an object declaration,
903 -- whose expansion is simpler. Given that the object is limited
904 -- there is no copy involved and no performance hit.
906 if Nkind (Nam) = N_Function_Call
907 and then Is_Limited_View (Etype (Nam))
908 and then not Is_Constrained (Etype (Nam))
909 and then Comes_From_Source (N)
910 then
911 Set_Etype (Id, T);
912 Set_Ekind (Id, E_Constant);
913 Rewrite (N,
914 Make_Object_Declaration (Loc,
915 Defining_Identifier => Id,
916 Constant_Present => True,
917 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
918 Expression => Relocate_Node (Nam)));
919 return;
920 end if;
922 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
923 -- when renaming declaration has a named access type. The Ada 2012
924 -- coverage rules allow an anonymous access type in the context of
925 -- an expected named general access type, but the renaming rules
926 -- require the types to be the same. (An exception is when the type
927 -- of the renaming is also an anonymous access type, which can only
928 -- happen due to a renaming created by the expander.)
930 if Nkind (Nam) = N_Type_Conversion
931 and then not Comes_From_Source (Nam)
932 and then Ekind (Etype (Expression (Nam))) = E_Anonymous_Access_Type
933 and then Ekind (T) /= E_Anonymous_Access_Type
934 then
935 Wrong_Type (Expression (Nam), T); -- Should we give better error???
936 end if;
938 -- Check that a class-wide object is not being renamed as an object
939 -- of a specific type. The test for access types is needed to exclude
940 -- cases where the renamed object is a dynamically tagged access
941 -- result, such as occurs in certain expansions.
943 if Is_Tagged_Type (T) then
944 Check_Dynamically_Tagged_Expression
945 (Expr => Nam,
946 Typ => T,
947 Related_Nod => N);
948 end if;
950 -- Ada 2005 (AI-230/AI-254): Access renaming
952 else pragma Assert (Present (Access_Definition (N)));
953 T := Access_Definition
954 (Related_Nod => N,
955 N => Access_Definition (N));
957 Analyze (Nam);
959 -- Ada 2005 AI05-105: if the declaration has an anonymous access
960 -- type, the renamed object must also have an anonymous type, and
961 -- this is a name resolution rule. This was implicit in the last part
962 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
963 -- recent AI.
965 if not Is_Overloaded (Nam) then
966 if Ekind (Etype (Nam)) /= Ekind (T) then
967 Error_Msg_N
968 ("expect anonymous access type in object renaming", N);
969 end if;
971 else
972 declare
973 I : Interp_Index;
974 It : Interp;
975 Typ : Entity_Id := Empty;
976 Seen : Boolean := False;
978 begin
979 Get_First_Interp (Nam, I, It);
980 while Present (It.Typ) loop
982 -- Renaming is ambiguous if more than one candidate
983 -- interpretation is type-conformant with the context.
985 if Ekind (It.Typ) = Ekind (T) then
986 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
987 and then
988 Type_Conformant
989 (Designated_Type (T), Designated_Type (It.Typ))
990 then
991 if not Seen then
992 Seen := True;
993 else
994 Error_Msg_N
995 ("ambiguous expression in renaming", Nam);
996 end if;
998 elsif Ekind (T) = E_Anonymous_Access_Type
999 and then
1000 Covers (Designated_Type (T), Designated_Type (It.Typ))
1001 then
1002 if not Seen then
1003 Seen := True;
1004 else
1005 Error_Msg_N
1006 ("ambiguous expression in renaming", Nam);
1007 end if;
1008 end if;
1010 if Covers (T, It.Typ) then
1011 Typ := It.Typ;
1012 Set_Etype (Nam, Typ);
1013 Set_Is_Overloaded (Nam, False);
1014 end if;
1015 end if;
1017 Get_Next_Interp (I, It);
1018 end loop;
1019 end;
1020 end if;
1022 Resolve (Nam, T);
1024 -- Ada 2005 (AI-231): In the case where the type is defined by an
1025 -- access_definition, the renamed entity shall be of an access-to-
1026 -- constant type if and only if the access_definition defines an
1027 -- access-to-constant type. ARM 8.5.1(4)
1029 if Constant_Present (Access_Definition (N))
1030 and then not Is_Access_Constant (Etype (Nam))
1031 then
1032 Error_Msg_N ("(Ada 2005): the renamed object is not "
1033 & "access-to-constant (RM 8.5.1(6))", N);
1035 elsif not Constant_Present (Access_Definition (N))
1036 and then Is_Access_Constant (Etype (Nam))
1037 then
1038 Error_Msg_N ("(Ada 2005): the renamed object is not "
1039 & "access-to-variable (RM 8.5.1(6))", N);
1040 end if;
1042 if Is_Access_Subprogram_Type (Etype (Nam)) then
1043 Check_Subtype_Conformant
1044 (Designated_Type (T), Designated_Type (Etype (Nam)));
1046 elsif not Subtypes_Statically_Match
1047 (Designated_Type (T),
1048 Available_View (Designated_Type (Etype (Nam))))
1049 then
1050 Error_Msg_N
1051 ("subtype of renamed object does not statically match", N);
1052 end if;
1053 end if;
1055 -- Special processing for renaming function return object. Some errors
1056 -- and warnings are produced only for calls that come from source.
1058 if Nkind (Nam) = N_Function_Call then
1059 case Ada_Version is
1061 -- Usage is illegal in Ada 83, but renamings are also introduced
1062 -- during expansion, and error does not apply to those.
1064 when Ada_83 =>
1065 if Comes_From_Source (N) then
1066 Error_Msg_N
1067 ("(Ada 83) cannot rename function return object", Nam);
1068 end if;
1070 -- In Ada 95, warn for odd case of renaming parameterless function
1071 -- call if this is not a limited type (where this is useful).
1073 when others =>
1074 if Warn_On_Object_Renames_Function
1075 and then No (Parameter_Associations (Nam))
1076 and then not Is_Limited_Type (Etype (Nam))
1077 and then Comes_From_Source (Nam)
1078 then
1079 Error_Msg_N
1080 ("renaming function result object is suspicious?R?", Nam);
1081 Error_Msg_NE
1082 ("\function & will be called only once?R?", Nam,
1083 Entity (Name (Nam)));
1084 Error_Msg_N -- CODEFIX
1085 ("\suggest using an initialized constant "
1086 & "object instead?R?", Nam);
1087 end if;
1089 end case;
1090 end if;
1092 Check_Constrained_Object;
1094 -- An object renaming requires an exact match of the type. Class-wide
1095 -- matching is not allowed.
1097 if Is_Class_Wide_Type (T)
1098 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1099 then
1100 Wrong_Type (Nam, T);
1101 end if;
1103 T2 := Etype (Nam);
1105 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1107 if Nkind (Nam) = N_Explicit_Dereference
1108 and then Ekind (Etype (T2)) = E_Incomplete_Type
1109 then
1110 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1111 return;
1113 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1114 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1115 return;
1116 end if;
1118 -- Ada 2005 (AI-327)
1120 if Ada_Version >= Ada_2005
1121 and then Nkind (Nam) = N_Attribute_Reference
1122 and then Attribute_Name (Nam) = Name_Priority
1123 then
1124 null;
1126 elsif Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1127 declare
1128 Nam_Decl : Node_Id;
1129 Nam_Ent : Entity_Id;
1131 begin
1132 if Nkind (Nam) = N_Attribute_Reference then
1133 Nam_Ent := Entity (Prefix (Nam));
1134 else
1135 Nam_Ent := Entity (Nam);
1136 end if;
1138 Nam_Decl := Parent (Nam_Ent);
1140 if Has_Null_Exclusion (N)
1141 and then not Has_Null_Exclusion (Nam_Decl)
1142 then
1143 -- Ada 2005 (AI-423): If the object name denotes a generic
1144 -- formal object of a generic unit G, and the object renaming
1145 -- declaration occurs within the body of G or within the body
1146 -- of a generic unit declared within the declarative region
1147 -- of G, then the declaration of the formal object of G must
1148 -- have a null exclusion or a null-excluding subtype.
1150 if Is_Formal_Object (Nam_Ent)
1151 and then In_Generic_Scope (Id)
1152 then
1153 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1154 Error_Msg_N
1155 ("renamed formal does not exclude `NULL` "
1156 & "(RM 8.5.1(4.6/2))", N);
1158 elsif In_Package_Body (Scope (Id)) then
1159 Error_Msg_N
1160 ("formal object does not have a null exclusion"
1161 & "(RM 8.5.1(4.6/2))", N);
1162 end if;
1164 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1165 -- shall exclude null.
1167 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1168 Error_Msg_N
1169 ("renamed object does not exclude `NULL` "
1170 & "(RM 8.5.1(4.6/2))", N);
1172 -- An instance is illegal if it contains a renaming that
1173 -- excludes null, and the actual does not. The renaming
1174 -- declaration has already indicated that the declaration
1175 -- of the renamed actual in the instance will raise
1176 -- constraint_error.
1178 elsif Nkind (Nam_Decl) = N_Object_Declaration
1179 and then In_Instance
1180 and then
1181 Present (Corresponding_Generic_Association (Nam_Decl))
1182 and then Nkind (Expression (Nam_Decl)) =
1183 N_Raise_Constraint_Error
1184 then
1185 Error_Msg_N
1186 ("renamed actual does not exclude `NULL` "
1187 & "(RM 8.5.1(4.6/2))", N);
1189 -- Finally, if there is a null exclusion, the subtype mark
1190 -- must not be null-excluding.
1192 elsif No (Access_Definition (N))
1193 and then Can_Never_Be_Null (T)
1194 then
1195 Error_Msg_NE
1196 ("`NOT NULL` not allowed (& already excludes null)",
1197 N, T);
1199 end if;
1201 elsif Can_Never_Be_Null (T)
1202 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1203 then
1204 Error_Msg_N
1205 ("renamed object does not exclude `NULL` "
1206 & "(RM 8.5.1(4.6/2))", N);
1208 elsif Has_Null_Exclusion (N)
1209 and then No (Access_Definition (N))
1210 and then Can_Never_Be_Null (T)
1211 then
1212 Error_Msg_NE
1213 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1214 end if;
1215 end;
1216 end if;
1218 -- Set the Ekind of the entity, unless it has been set already, as is
1219 -- the case for the iteration object over a container with no variable
1220 -- indexing. In that case it's been marked as a constant, and we do not
1221 -- want to change it to a variable.
1223 if Ekind (Id) /= E_Constant then
1224 Set_Ekind (Id, E_Variable);
1225 end if;
1227 -- Initialize the object size and alignment. Note that we used to call
1228 -- Init_Size_Align here, but that's wrong for objects which have only
1229 -- an Esize, not an RM_Size field.
1231 Init_Object_Size_Align (Id);
1233 if T = Any_Type or else Etype (Nam) = Any_Type then
1234 return;
1236 -- Verify that the renamed entity is an object or a function call. It
1237 -- may have been rewritten in several ways.
1239 elsif Is_Object_Reference (Nam) then
1240 if Comes_From_Source (N) then
1241 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1242 Error_Msg_N
1243 ("illegal renaming of discriminant-dependent component", Nam);
1244 end if;
1246 -- If the renaming comes from source and the renamed object is a
1247 -- dereference, then mark the prefix as needing debug information,
1248 -- since it might have been rewritten hence internally generated
1249 -- and Debug_Renaming_Declaration will link the renaming to it.
1251 if Nkind (Nam) = N_Explicit_Dereference
1252 and then Is_Entity_Name (Prefix (Nam))
1253 then
1254 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1255 end if;
1256 end if;
1258 -- A static function call may have been folded into a literal
1260 elsif Nkind (Original_Node (Nam)) = N_Function_Call
1262 -- When expansion is disabled, attribute reference is not rewritten
1263 -- as function call. Otherwise it may be rewritten as a conversion,
1264 -- so check original node.
1266 or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference
1267 and then Is_Function_Attribute_Name
1268 (Attribute_Name (Original_Node (Nam))))
1270 -- Weird but legal, equivalent to renaming a function call. Illegal
1271 -- if the literal is the result of constant-folding an attribute
1272 -- reference that is not a function.
1274 or else (Is_Entity_Name (Nam)
1275 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1276 and then
1277 Nkind (Original_Node (Nam)) /= N_Attribute_Reference)
1279 or else (Nkind (Nam) = N_Type_Conversion
1280 and then Is_Tagged_Type (Entity (Subtype_Mark (Nam))))
1281 then
1282 null;
1284 elsif Nkind (Nam) = N_Type_Conversion then
1285 Error_Msg_N
1286 ("renaming of conversion only allowed for tagged types", Nam);
1288 -- Ada 2005 (AI-327)
1290 elsif Ada_Version >= Ada_2005
1291 and then Nkind (Nam) = N_Attribute_Reference
1292 and then Attribute_Name (Nam) = Name_Priority
1293 then
1294 null;
1296 -- Allow internally generated x'Reference expression
1298 elsif Nkind (Nam) = N_Reference then
1299 null;
1301 else
1302 Error_Msg_N ("expect object name in renaming", Nam);
1303 end if;
1305 Set_Etype (Id, T2);
1307 if not Is_Variable (Nam) then
1308 Set_Ekind (Id, E_Constant);
1309 Set_Never_Set_In_Source (Id, True);
1310 Set_Is_True_Constant (Id, True);
1311 end if;
1313 -- An object renaming is Ghost if the renamed entity is Ghost or the
1314 -- construct appears within a Ghost scope.
1316 if (Is_Entity_Name (Nam)
1317 and then Is_Ghost_Entity (Entity (Nam)))
1318 or else Within_Ghost_Scope
1319 then
1320 Set_Is_Ghost_Entity (Id);
1321 end if;
1323 -- The entity of the renaming declaration needs to reflect whether the
1324 -- renamed object is volatile. Is_Volatile is set if the renamed object
1325 -- is volatile in the RM legality sense.
1327 Set_Is_Volatile (Id, Is_Volatile_Object (Nam));
1329 -- Treat as volatile if we just set the Volatile flag
1331 if Is_Volatile (Id)
1333 -- Or if we are renaming an entity which was marked this way
1335 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1337 or else (Is_Entity_Name (Nam)
1338 and then Treat_As_Volatile (Entity (Nam)))
1339 then
1340 Set_Treat_As_Volatile (Id, True);
1341 end if;
1343 -- Now make the link to the renamed object
1345 Set_Renamed_Object (Id, Nam);
1347 -- Implementation-defined aspect specifications can appear in a renaming
1348 -- declaration, but not language-defined ones. The call to procedure
1349 -- Analyze_Aspect_Specifications will take care of this error check.
1351 if Has_Aspects (N) then
1352 Analyze_Aspect_Specifications (N, Id);
1353 end if;
1355 -- Deal with dimensions
1357 Analyze_Dimension (N);
1358 end Analyze_Object_Renaming;
1360 ------------------------------
1361 -- Analyze_Package_Renaming --
1362 ------------------------------
1364 procedure Analyze_Package_Renaming (N : Node_Id) is
1365 New_P : constant Entity_Id := Defining_Entity (N);
1366 Old_P : Entity_Id;
1367 Spec : Node_Id;
1369 begin
1370 if Name (N) = Error then
1371 return;
1372 end if;
1374 -- Check for Text_IO special unit (we may be renaming a Text_IO child)
1376 Check_Text_IO_Special_Unit (Name (N));
1378 if Current_Scope /= Standard_Standard then
1379 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1380 end if;
1382 Enter_Name (New_P);
1383 Analyze (Name (N));
1385 if Is_Entity_Name (Name (N)) then
1386 Old_P := Entity (Name (N));
1387 else
1388 Old_P := Any_Id;
1389 end if;
1391 if Etype (Old_P) = Any_Type then
1392 Error_Msg_N ("expect package name in renaming", Name (N));
1394 elsif Ekind (Old_P) /= E_Package
1395 and then not (Ekind (Old_P) = E_Generic_Package
1396 and then In_Open_Scopes (Old_P))
1397 then
1398 if Ekind (Old_P) = E_Generic_Package then
1399 Error_Msg_N
1400 ("generic package cannot be renamed as a package", Name (N));
1401 else
1402 Error_Msg_Sloc := Sloc (Old_P);
1403 Error_Msg_NE
1404 ("expect package name in renaming, found& declared#",
1405 Name (N), Old_P);
1406 end if;
1408 -- Set basic attributes to minimize cascaded errors
1410 Set_Ekind (New_P, E_Package);
1411 Set_Etype (New_P, Standard_Void_Type);
1413 -- Here for OK package renaming
1415 else
1416 -- Entities in the old package are accessible through the renaming
1417 -- entity. The simplest implementation is to have both packages share
1418 -- the entity list.
1420 Set_Ekind (New_P, E_Package);
1421 Set_Etype (New_P, Standard_Void_Type);
1423 if Present (Renamed_Object (Old_P)) then
1424 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1425 else
1426 Set_Renamed_Object (New_P, Old_P);
1427 end if;
1429 Set_Has_Completion (New_P);
1431 Set_First_Entity (New_P, First_Entity (Old_P));
1432 Set_Last_Entity (New_P, Last_Entity (Old_P));
1433 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1434 Check_Library_Unit_Renaming (N, Old_P);
1435 Generate_Reference (Old_P, Name (N));
1437 -- A package renaming is Ghost if the renamed entity is Ghost or
1438 -- the construct appears within a Ghost scope.
1440 if Is_Ghost_Entity (Old_P) or else Within_Ghost_Scope then
1441 Set_Is_Ghost_Entity (New_P);
1442 end if;
1444 -- If the renaming is in the visible part of a package, then we set
1445 -- Renamed_In_Spec for the renamed package, to prevent giving
1446 -- warnings about no entities referenced. Such a warning would be
1447 -- overenthusiastic, since clients can see entities in the renamed
1448 -- package via the visible package renaming.
1450 declare
1451 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1452 begin
1453 if Ekind (Ent) = E_Package
1454 and then not In_Private_Part (Ent)
1455 and then In_Extended_Main_Source_Unit (N)
1456 and then Ekind (Old_P) = E_Package
1457 then
1458 Set_Renamed_In_Spec (Old_P);
1459 end if;
1460 end;
1462 -- If this is the renaming declaration of a package instantiation
1463 -- within itself, it is the declaration that ends the list of actuals
1464 -- for the instantiation. At this point, the subtypes that rename
1465 -- the actuals are flagged as generic, to avoid spurious ambiguities
1466 -- if the actuals for two distinct formals happen to coincide. If
1467 -- the actual is a private type, the subtype has a private completion
1468 -- that is flagged in the same fashion.
1470 -- Resolution is identical to what is was in the original generic.
1471 -- On exit from the generic instance, these are turned into regular
1472 -- subtypes again, so they are compatible with types in their class.
1474 if not Is_Generic_Instance (Old_P) then
1475 return;
1476 else
1477 Spec := Specification (Unit_Declaration_Node (Old_P));
1478 end if;
1480 if Nkind (Spec) = N_Package_Specification
1481 and then Present (Generic_Parent (Spec))
1482 and then Old_P = Current_Scope
1483 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1484 then
1485 declare
1486 E : Entity_Id;
1488 begin
1489 E := First_Entity (Old_P);
1490 while Present (E) and then E /= New_P loop
1491 if Is_Type (E)
1492 and then Nkind (Parent (E)) = N_Subtype_Declaration
1493 then
1494 Set_Is_Generic_Actual_Type (E);
1496 if Is_Private_Type (E)
1497 and then Present (Full_View (E))
1498 then
1499 Set_Is_Generic_Actual_Type (Full_View (E));
1500 end if;
1501 end if;
1503 Next_Entity (E);
1504 end loop;
1505 end;
1506 end if;
1507 end if;
1509 -- Implementation-defined aspect specifications can appear in a renaming
1510 -- declaration, but not language-defined ones. The call to procedure
1511 -- Analyze_Aspect_Specifications will take care of this error check.
1513 if Has_Aspects (N) then
1514 Analyze_Aspect_Specifications (N, New_P);
1515 end if;
1516 end Analyze_Package_Renaming;
1518 -------------------------------
1519 -- Analyze_Renamed_Character --
1520 -------------------------------
1522 procedure Analyze_Renamed_Character
1523 (N : Node_Id;
1524 New_S : Entity_Id;
1525 Is_Body : Boolean)
1527 C : constant Node_Id := Name (N);
1529 begin
1530 if Ekind (New_S) = E_Function then
1531 Resolve (C, Etype (New_S));
1533 if Is_Body then
1534 Check_Frozen_Renaming (N, New_S);
1535 end if;
1537 else
1538 Error_Msg_N ("character literal can only be renamed as function", N);
1539 end if;
1540 end Analyze_Renamed_Character;
1542 ---------------------------------
1543 -- Analyze_Renamed_Dereference --
1544 ---------------------------------
1546 procedure Analyze_Renamed_Dereference
1547 (N : Node_Id;
1548 New_S : Entity_Id;
1549 Is_Body : Boolean)
1551 Nam : constant Node_Id := Name (N);
1552 P : constant Node_Id := Prefix (Nam);
1553 Typ : Entity_Id;
1554 Ind : Interp_Index;
1555 It : Interp;
1557 begin
1558 if not Is_Overloaded (P) then
1559 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1560 or else not Type_Conformant (Etype (Nam), New_S)
1561 then
1562 Error_Msg_N ("designated type does not match specification", P);
1563 else
1564 Resolve (P);
1565 end if;
1567 return;
1569 else
1570 Typ := Any_Type;
1571 Get_First_Interp (Nam, Ind, It);
1573 while Present (It.Nam) loop
1575 if Ekind (It.Nam) = E_Subprogram_Type
1576 and then Type_Conformant (It.Nam, New_S)
1577 then
1578 if Typ /= Any_Id then
1579 Error_Msg_N ("ambiguous renaming", P);
1580 return;
1581 else
1582 Typ := It.Nam;
1583 end if;
1584 end if;
1586 Get_Next_Interp (Ind, It);
1587 end loop;
1589 if Typ = Any_Type then
1590 Error_Msg_N ("designated type does not match specification", P);
1591 else
1592 Resolve (N, Typ);
1594 if Is_Body then
1595 Check_Frozen_Renaming (N, New_S);
1596 end if;
1597 end if;
1598 end if;
1599 end Analyze_Renamed_Dereference;
1601 ---------------------------
1602 -- Analyze_Renamed_Entry --
1603 ---------------------------
1605 procedure Analyze_Renamed_Entry
1606 (N : Node_Id;
1607 New_S : Entity_Id;
1608 Is_Body : Boolean)
1610 Nam : constant Node_Id := Name (N);
1611 Sel : constant Node_Id := Selector_Name (Nam);
1612 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1613 Old_S : Entity_Id;
1615 begin
1616 if Entity (Sel) = Any_Id then
1618 -- Selector is undefined on prefix. Error emitted already
1620 Set_Has_Completion (New_S);
1621 return;
1622 end if;
1624 -- Otherwise find renamed entity and build body of New_S as a call to it
1626 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1628 if Old_S = Any_Id then
1629 Error_Msg_N (" no subprogram or entry matches specification", N);
1630 else
1631 if Is_Body then
1632 Check_Subtype_Conformant (New_S, Old_S, N);
1633 Generate_Reference (New_S, Defining_Entity (N), 'b');
1634 Style.Check_Identifier (Defining_Entity (N), New_S);
1636 else
1637 -- Only mode conformance required for a renaming_as_declaration
1639 Check_Mode_Conformant (New_S, Old_S, N);
1640 end if;
1642 Inherit_Renamed_Profile (New_S, Old_S);
1644 -- The prefix can be an arbitrary expression that yields a task or
1645 -- protected object, so it must be resolved.
1647 Resolve (Prefix (Nam), Scope (Old_S));
1648 end if;
1650 Set_Convention (New_S, Convention (Old_S));
1651 Set_Has_Completion (New_S, Inside_A_Generic);
1653 -- AI05-0225: If the renamed entity is a procedure or entry of a
1654 -- protected object, the target object must be a variable.
1656 if Ekind (Scope (Old_S)) in Protected_Kind
1657 and then Ekind (New_S) = E_Procedure
1658 and then not Is_Variable (Prefix (Nam))
1659 then
1660 if Is_Actual then
1661 Error_Msg_N
1662 ("target object of protected operation used as actual for "
1663 & "formal procedure must be a variable", Nam);
1664 else
1665 Error_Msg_N
1666 ("target object of protected operation renamed as procedure, "
1667 & "must be a variable", Nam);
1668 end if;
1669 end if;
1671 if Is_Body then
1672 Check_Frozen_Renaming (N, New_S);
1673 end if;
1674 end Analyze_Renamed_Entry;
1676 -----------------------------------
1677 -- Analyze_Renamed_Family_Member --
1678 -----------------------------------
1680 procedure Analyze_Renamed_Family_Member
1681 (N : Node_Id;
1682 New_S : Entity_Id;
1683 Is_Body : Boolean)
1685 Nam : constant Node_Id := Name (N);
1686 P : constant Node_Id := Prefix (Nam);
1687 Old_S : Entity_Id;
1689 begin
1690 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1691 or else (Nkind (P) = N_Selected_Component
1692 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1693 then
1694 if Is_Entity_Name (P) then
1695 Old_S := Entity (P);
1696 else
1697 Old_S := Entity (Selector_Name (P));
1698 end if;
1700 if not Entity_Matches_Spec (Old_S, New_S) then
1701 Error_Msg_N ("entry family does not match specification", N);
1703 elsif Is_Body then
1704 Check_Subtype_Conformant (New_S, Old_S, N);
1705 Generate_Reference (New_S, Defining_Entity (N), 'b');
1706 Style.Check_Identifier (Defining_Entity (N), New_S);
1707 end if;
1709 else
1710 Error_Msg_N ("no entry family matches specification", N);
1711 end if;
1713 Set_Has_Completion (New_S, Inside_A_Generic);
1715 if Is_Body then
1716 Check_Frozen_Renaming (N, New_S);
1717 end if;
1718 end Analyze_Renamed_Family_Member;
1720 -----------------------------------------
1721 -- Analyze_Renamed_Primitive_Operation --
1722 -----------------------------------------
1724 procedure Analyze_Renamed_Primitive_Operation
1725 (N : Node_Id;
1726 New_S : Entity_Id;
1727 Is_Body : Boolean)
1729 Old_S : Entity_Id;
1731 function Conforms
1732 (Subp : Entity_Id;
1733 Ctyp : Conformance_Type) return Boolean;
1734 -- Verify that the signatures of the renamed entity and the new entity
1735 -- match. The first formal of the renamed entity is skipped because it
1736 -- is the target object in any subsequent call.
1738 --------------
1739 -- Conforms --
1740 --------------
1742 function Conforms
1743 (Subp : Entity_Id;
1744 Ctyp : Conformance_Type) return Boolean
1746 Old_F : Entity_Id;
1747 New_F : Entity_Id;
1749 begin
1750 if Ekind (Subp) /= Ekind (New_S) then
1751 return False;
1752 end if;
1754 Old_F := Next_Formal (First_Formal (Subp));
1755 New_F := First_Formal (New_S);
1756 while Present (Old_F) and then Present (New_F) loop
1757 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
1758 return False;
1759 end if;
1761 if Ctyp >= Mode_Conformant
1762 and then Ekind (Old_F) /= Ekind (New_F)
1763 then
1764 return False;
1765 end if;
1767 Next_Formal (New_F);
1768 Next_Formal (Old_F);
1769 end loop;
1771 return True;
1772 end Conforms;
1774 -- Start of processing for Analyze_Renamed_Primitive_Operation
1776 begin
1777 if not Is_Overloaded (Selector_Name (Name (N))) then
1778 Old_S := Entity (Selector_Name (Name (N)));
1780 if not Conforms (Old_S, Type_Conformant) then
1781 Old_S := Any_Id;
1782 end if;
1784 else
1785 -- Find the operation that matches the given signature
1787 declare
1788 It : Interp;
1789 Ind : Interp_Index;
1791 begin
1792 Old_S := Any_Id;
1793 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
1795 while Present (It.Nam) loop
1796 if Conforms (It.Nam, Type_Conformant) then
1797 Old_S := It.Nam;
1798 end if;
1800 Get_Next_Interp (Ind, It);
1801 end loop;
1802 end;
1803 end if;
1805 if Old_S = Any_Id then
1806 Error_Msg_N (" no subprogram or entry matches specification", N);
1808 else
1809 if Is_Body then
1810 if not Conforms (Old_S, Subtype_Conformant) then
1811 Error_Msg_N ("subtype conformance error in renaming", N);
1812 end if;
1814 Generate_Reference (New_S, Defining_Entity (N), 'b');
1815 Style.Check_Identifier (Defining_Entity (N), New_S);
1817 else
1818 -- Only mode conformance required for a renaming_as_declaration
1820 if not Conforms (Old_S, Mode_Conformant) then
1821 Error_Msg_N ("mode conformance error in renaming", N);
1822 end if;
1824 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
1825 -- view of a subprogram is intrinsic, because the compiler has
1826 -- to generate a wrapper for any call to it. If the name in a
1827 -- subprogram renaming is a prefixed view, the entity is thus
1828 -- intrinsic, and 'Access cannot be applied to it.
1830 Set_Convention (New_S, Convention_Intrinsic);
1831 end if;
1833 -- Inherit_Renamed_Profile (New_S, Old_S);
1835 -- The prefix can be an arbitrary expression that yields an
1836 -- object, so it must be resolved.
1838 Resolve (Prefix (Name (N)));
1839 end if;
1840 end Analyze_Renamed_Primitive_Operation;
1842 ---------------------------------
1843 -- Analyze_Subprogram_Renaming --
1844 ---------------------------------
1846 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1847 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
1848 Is_Actual : constant Boolean := Present (Formal_Spec);
1849 Nam : constant Node_Id := Name (N);
1850 Save_AV : constant Ada_Version_Type := Ada_Version;
1851 Save_AVP : constant Node_Id := Ada_Version_Pragma;
1852 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
1853 Spec : constant Node_Id := Specification (N);
1855 Old_S : Entity_Id := Empty;
1856 Rename_Spec : Entity_Id;
1858 procedure Build_Class_Wide_Wrapper
1859 (Ren_Id : out Entity_Id;
1860 Wrap_Id : out Entity_Id);
1861 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal
1862 -- type with unknown discriminants and a generic primitive operation of
1863 -- the said type with a box require special processing when the actual
1864 -- is a class-wide type:
1866 -- generic
1867 -- type Formal_Typ (<>) is private;
1868 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
1869 -- package Gen is ...
1871 -- package Inst is new Gen (Actual_Typ'Class);
1873 -- In this case the general renaming mechanism used in the prologue of
1874 -- an instance no longer applies:
1876 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
1878 -- The above is replaced the following wrapper/renaming combination:
1880 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
1881 -- begin
1882 -- Prim_Op (Param); -- primitive
1883 -- end Wrapper;
1885 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
1887 -- This transformation applies only if there is no explicit visible
1888 -- class-wide operation at the point of the instantiation. Ren_Id is
1889 -- the entity of the renaming declaration. Wrap_Id is the entity of
1890 -- the generated class-wide wrapper (or Any_Id).
1892 procedure Check_Null_Exclusion
1893 (Ren : Entity_Id;
1894 Sub : Entity_Id);
1895 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
1896 -- following AI rules:
1898 -- If Ren is a renaming of a formal subprogram and one of its
1899 -- parameters has a null exclusion, then the corresponding formal
1900 -- in Sub must also have one. Otherwise the subtype of the Sub's
1901 -- formal parameter must exclude null.
1903 -- If Ren is a renaming of a formal function and its return
1904 -- profile has a null exclusion, then Sub's return profile must
1905 -- have one. Otherwise the subtype of Sub's return profile must
1906 -- exclude null.
1908 procedure Freeze_Actual_Profile;
1909 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
1910 -- types: a callable entity freezes its profile, unless it has an
1911 -- incomplete untagged formal (RM 13.14(10.2/3)).
1913 function Has_Class_Wide_Actual return Boolean;
1914 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
1915 -- defaulted formal subprogram where the actual for the controlling
1916 -- formal type is class-wide.
1918 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
1919 -- Find renamed entity when the declaration is a renaming_as_body and
1920 -- the renamed entity may itself be a renaming_as_body. Used to enforce
1921 -- rule that a renaming_as_body is illegal if the declaration occurs
1922 -- before the subprogram it completes is frozen, and renaming indirectly
1923 -- renames the subprogram itself.(Defect Report 8652/0027).
1925 ------------------------------
1926 -- Build_Class_Wide_Wrapper --
1927 ------------------------------
1929 procedure Build_Class_Wide_Wrapper
1930 (Ren_Id : out Entity_Id;
1931 Wrap_Id : out Entity_Id)
1933 Loc : constant Source_Ptr := Sloc (N);
1935 function Build_Call
1936 (Subp_Id : Entity_Id;
1937 Params : List_Id) return Node_Id;
1938 -- Create a dispatching call to invoke routine Subp_Id with actuals
1939 -- built from the parameter specifications of list Params.
1941 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
1942 -- Create a subprogram specification based on the subprogram profile
1943 -- of Subp_Id.
1945 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
1946 -- Find a primitive subprogram of type Typ which matches the profile
1947 -- of the renaming declaration.
1949 procedure Interpretation_Error (Subp_Id : Entity_Id);
1950 -- Emit a continuation error message suggesting subprogram Subp_Id as
1951 -- a possible interpretation.
1953 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean;
1954 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
1955 -- operator.
1957 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean;
1958 -- Determine whether subprogram Subp_Id is a suitable candidate for
1959 -- the role of a wrapped subprogram.
1961 ----------------
1962 -- Build_Call --
1963 ----------------
1965 function Build_Call
1966 (Subp_Id : Entity_Id;
1967 Params : List_Id) return Node_Id
1969 Actuals : constant List_Id := New_List;
1970 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
1971 Formal : Node_Id;
1973 begin
1974 -- Build the actual parameters of the call
1976 Formal := First (Params);
1977 while Present (Formal) loop
1978 Append_To (Actuals,
1979 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
1980 Next (Formal);
1981 end loop;
1983 -- Generate:
1984 -- return Subp_Id (Actuals);
1986 if Ekind_In (Subp_Id, E_Function, E_Operator) then
1987 return
1988 Make_Simple_Return_Statement (Loc,
1989 Expression =>
1990 Make_Function_Call (Loc,
1991 Name => Call_Ref,
1992 Parameter_Associations => Actuals));
1994 -- Generate:
1995 -- Subp_Id (Actuals);
1997 else
1998 return
1999 Make_Procedure_Call_Statement (Loc,
2000 Name => Call_Ref,
2001 Parameter_Associations => Actuals);
2002 end if;
2003 end Build_Call;
2005 ----------------
2006 -- Build_Spec --
2007 ----------------
2009 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2010 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2011 Spec_Id : constant Entity_Id :=
2012 Make_Defining_Identifier (Loc,
2013 Chars => New_External_Name (Chars (Subp_Id), 'R'));
2015 begin
2016 if Ekind (Formal_Spec) = E_Procedure then
2017 return
2018 Make_Procedure_Specification (Loc,
2019 Defining_Unit_Name => Spec_Id,
2020 Parameter_Specifications => Params);
2021 else
2022 return
2023 Make_Function_Specification (Loc,
2024 Defining_Unit_Name => Spec_Id,
2025 Parameter_Specifications => Params,
2026 Result_Definition =>
2027 New_Copy_Tree (Result_Definition (Spec)));
2028 end if;
2029 end Build_Spec;
2031 --------------------
2032 -- Find_Primitive --
2033 --------------------
2035 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2036 procedure Replace_Parameter_Types (Spec : Node_Id);
2037 -- Given a specification Spec, replace all class-wide parameter
2038 -- types with reference to type Typ.
2040 -----------------------------
2041 -- Replace_Parameter_Types --
2042 -----------------------------
2044 procedure Replace_Parameter_Types (Spec : Node_Id) is
2045 Formal : Node_Id;
2046 Formal_Id : Entity_Id;
2047 Formal_Typ : Node_Id;
2049 begin
2050 Formal := First (Parameter_Specifications (Spec));
2051 while Present (Formal) loop
2052 Formal_Id := Defining_Identifier (Formal);
2053 Formal_Typ := Parameter_Type (Formal);
2055 -- Create a new entity for each class-wide formal to prevent
2056 -- aliasing with the original renaming. Replace the type of
2057 -- such a parameter with the candidate type.
2059 if Nkind (Formal_Typ) = N_Identifier
2060 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2061 then
2062 Set_Defining_Identifier (Formal,
2063 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2065 Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc));
2066 end if;
2068 Next (Formal);
2069 end loop;
2070 end Replace_Parameter_Types;
2072 -- Local variables
2074 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2075 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2076 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2077 Subp_Id : Entity_Id;
2079 -- Start of processing for Find_Primitive
2081 begin
2082 -- Each attempt to find a suitable primitive of a particular type
2083 -- operates on its own copy of the original renaming. As a result
2084 -- the original renaming is kept decoration and side-effect free.
2086 -- Inherit the overloaded status of the renamed subprogram name
2088 if Is_Overloaded (Nam) then
2089 Set_Is_Overloaded (Alt_Nam);
2090 Save_Interps (Nam, Alt_Nam);
2091 end if;
2093 -- The copied renaming is hidden from visibility to prevent the
2094 -- pollution of the enclosing context.
2096 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2098 -- The types of all class-wide parameters must be changed to the
2099 -- candidate type.
2101 Replace_Parameter_Types (Alt_Spec);
2103 -- Try to find a suitable primitive which matches the altered
2104 -- profile of the renaming specification.
2106 Subp_Id :=
2107 Find_Renamed_Entity
2108 (N => Alt_Ren,
2109 Nam => Name (Alt_Ren),
2110 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2111 Is_Actual => Is_Actual);
2113 -- Do not return Any_Id if the resolion of the altered profile
2114 -- failed as this complicates further checks on the caller side,
2115 -- return Empty instead.
2117 if Subp_Id = Any_Id then
2118 return Empty;
2119 else
2120 return Subp_Id;
2121 end if;
2122 end Find_Primitive;
2124 --------------------------
2125 -- Interpretation_Error --
2126 --------------------------
2128 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2129 begin
2130 Error_Msg_Sloc := Sloc (Subp_Id);
2132 if Is_Internal (Subp_Id) then
2133 Error_Msg_NE
2134 ("\\possible interpretation: predefined & #",
2135 Spec, Formal_Spec);
2136 else
2137 Error_Msg_NE
2138 ("\\possible interpretation: & defined #", Spec, Formal_Spec);
2139 end if;
2140 end Interpretation_Error;
2142 ---------------------------
2143 -- Is_Intrinsic_Equality --
2144 ---------------------------
2146 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is
2147 begin
2148 return
2149 Ekind (Subp_Id) = E_Operator
2150 and then Chars (Subp_Id) = Name_Op_Eq
2151 and then Is_Intrinsic_Subprogram (Subp_Id);
2152 end Is_Intrinsic_Equality;
2154 ---------------------------
2155 -- Is_Suitable_Candidate --
2156 ---------------------------
2158 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is
2159 begin
2160 if No (Subp_Id) then
2161 return False;
2163 -- An intrinsic subprogram is never a good candidate. This is an
2164 -- indication of a missing primitive, either defined directly or
2165 -- inherited from a parent tagged type.
2167 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2168 return False;
2170 else
2171 return True;
2172 end if;
2173 end Is_Suitable_Candidate;
2175 -- Local variables
2177 Actual_Typ : Entity_Id := Empty;
2178 -- The actual class-wide type for Formal_Typ
2180 CW_Prim_OK : Boolean;
2181 CW_Prim_Op : Entity_Id;
2182 -- The class-wide subprogram (if available) which corresponds to the
2183 -- renamed generic formal subprogram.
2185 Formal_Typ : Entity_Id := Empty;
2186 -- The generic formal type with unknown discriminants
2188 Root_Prim_OK : Boolean;
2189 Root_Prim_Op : Entity_Id;
2190 -- The root type primitive (if available) which corresponds to the
2191 -- renamed generic formal subprogram.
2193 Root_Typ : Entity_Id := Empty;
2194 -- The root type of Actual_Typ
2196 Body_Decl : Node_Id;
2197 Formal : Node_Id;
2198 Prim_Op : Entity_Id;
2199 Spec_Decl : Node_Id;
2201 -- Start of processing for Build_Class_Wide_Wrapper
2203 begin
2204 -- Analyze the specification of the renaming in case the generation
2205 -- of the class-wide wrapper fails.
2207 Ren_Id := Analyze_Subprogram_Specification (Spec);
2208 Wrap_Id := Any_Id;
2210 -- Do not attempt to build a wrapper if the renaming is in error
2212 if Error_Posted (Nam) then
2213 return;
2214 end if;
2216 -- Analyze the renamed name, but do not resolve it. The resolution is
2217 -- completed once a suitable subprogram is found.
2219 Analyze (Nam);
2221 -- When the renamed name denotes the intrinsic operator equals, the
2222 -- name must be treated as overloaded. This allows for a potential
2223 -- match against the root type's predefined equality function.
2225 if Is_Intrinsic_Equality (Entity (Nam)) then
2226 Set_Is_Overloaded (Nam);
2227 Collect_Interps (Nam);
2228 end if;
2230 -- Step 1: Find the generic formal type with unknown discriminants
2231 -- and its corresponding class-wide actual type from the renamed
2232 -- generic formal subprogram.
2234 Formal := First_Formal (Formal_Spec);
2235 while Present (Formal) loop
2236 if Has_Unknown_Discriminants (Etype (Formal))
2237 and then not Is_Class_Wide_Type (Etype (Formal))
2238 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2239 then
2240 Formal_Typ := Etype (Formal);
2241 Actual_Typ := Get_Instance_Of (Formal_Typ);
2242 Root_Typ := Etype (Actual_Typ);
2243 exit;
2244 end if;
2246 Next_Formal (Formal);
2247 end loop;
2249 -- The specification of the generic formal subprogram should always
2250 -- contain a formal type with unknown discriminants whose actual is
2251 -- a class-wide type, otherwise this indicates a failure in routine
2252 -- Has_Class_Wide_Actual.
2254 pragma Assert (Present (Formal_Typ));
2256 -- Step 2: Find the proper class-wide subprogram or primitive which
2257 -- corresponds to the renamed generic formal subprogram.
2259 CW_Prim_Op := Find_Primitive (Actual_Typ);
2260 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2261 Root_Prim_Op := Find_Primitive (Root_Typ);
2262 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2264 -- The class-wide actual type has two subprograms which correspond to
2265 -- the renamed generic formal subprogram:
2267 -- with procedure Prim_Op (Param : Formal_Typ);
2269 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2270 -- procedure Prim_Op (Param : Actual_Typ'Class);
2272 -- Even though the declaration of the two subprograms is legal, a
2273 -- call to either one is ambiguous and therefore illegal.
2275 if CW_Prim_OK and Root_Prim_OK then
2277 -- A user-defined primitive has precedence over a predefined one
2279 if Is_Internal (CW_Prim_Op)
2280 and then not Is_Internal (Root_Prim_Op)
2281 then
2282 Prim_Op := Root_Prim_Op;
2284 elsif Is_Internal (Root_Prim_Op)
2285 and then not Is_Internal (CW_Prim_Op)
2286 then
2287 Prim_Op := CW_Prim_Op;
2289 elsif CW_Prim_Op = Root_Prim_Op then
2290 Prim_Op := Root_Prim_Op;
2292 -- Otherwise both candidate subprograms are user-defined and
2293 -- ambiguous.
2295 else
2296 Error_Msg_NE
2297 ("ambiguous actual for generic subprogram &",
2298 Spec, Formal_Spec);
2299 Interpretation_Error (Root_Prim_Op);
2300 Interpretation_Error (CW_Prim_Op);
2301 return;
2302 end if;
2304 elsif CW_Prim_OK and not Root_Prim_OK then
2305 Prim_Op := CW_Prim_Op;
2307 elsif not CW_Prim_OK and Root_Prim_OK then
2308 Prim_Op := Root_Prim_Op;
2310 -- An intrinsic equality may act as a suitable candidate in the case
2311 -- of a null type extension where the parent's equality is hidden. A
2312 -- call to an intrinsic equality is expanded as dispatching.
2314 elsif Present (Root_Prim_Op)
2315 and then Is_Intrinsic_Equality (Root_Prim_Op)
2316 then
2317 Prim_Op := Root_Prim_Op;
2319 -- Otherwise there are no candidate subprograms. Let the caller
2320 -- diagnose the error.
2322 else
2323 return;
2324 end if;
2326 -- At this point resolution has taken place and the name is no longer
2327 -- overloaded. Mark the primitive as referenced.
2329 Set_Is_Overloaded (Name (N), False);
2330 Set_Referenced (Prim_Op);
2332 -- Step 3: Create the declaration and the body of the wrapper, insert
2333 -- all the pieces into the tree.
2335 Spec_Decl :=
2336 Make_Subprogram_Declaration (Loc,
2337 Specification => Build_Spec (Ren_Id));
2338 Insert_Before_And_Analyze (N, Spec_Decl);
2340 -- If the operator carries an Eliminated pragma, indicate that the
2341 -- wrapper is also to be eliminated, to prevent spurious error when
2342 -- using gnatelim on programs that include box-initialization of
2343 -- equality operators.
2345 Wrap_Id := Defining_Entity (Spec_Decl);
2346 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2348 Body_Decl :=
2349 Make_Subprogram_Body (Loc,
2350 Specification => Build_Spec (Ren_Id),
2351 Declarations => New_List,
2352 Handled_Statement_Sequence =>
2353 Make_Handled_Sequence_Of_Statements (Loc,
2354 Statements => New_List (
2355 Build_Call
2356 (Subp_Id => Prim_Op,
2357 Params =>
2358 Parameter_Specifications
2359 (Specification (Spec_Decl))))));
2361 -- The generated body does not freeze and must be analyzed when the
2362 -- class-wide wrapper is frozen. The body is only needed if expansion
2363 -- is enabled.
2365 if Expander_Active then
2366 Append_Freeze_Action (Wrap_Id, Body_Decl);
2367 end if;
2369 -- Step 4: The subprogram renaming aliases the wrapper
2371 Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc));
2372 end Build_Class_Wide_Wrapper;
2374 --------------------------
2375 -- Check_Null_Exclusion --
2376 --------------------------
2378 procedure Check_Null_Exclusion
2379 (Ren : Entity_Id;
2380 Sub : Entity_Id)
2382 Ren_Formal : Entity_Id;
2383 Sub_Formal : Entity_Id;
2385 begin
2386 -- Parameter check
2388 Ren_Formal := First_Formal (Ren);
2389 Sub_Formal := First_Formal (Sub);
2390 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2391 if Has_Null_Exclusion (Parent (Ren_Formal))
2392 and then
2393 not (Has_Null_Exclusion (Parent (Sub_Formal))
2394 or else Can_Never_Be_Null (Etype (Sub_Formal)))
2395 then
2396 Error_Msg_NE
2397 ("`NOT NULL` required for parameter &",
2398 Parent (Sub_Formal), Sub_Formal);
2399 end if;
2401 Next_Formal (Ren_Formal);
2402 Next_Formal (Sub_Formal);
2403 end loop;
2405 -- Return profile check
2407 if Nkind (Parent (Ren)) = N_Function_Specification
2408 and then Nkind (Parent (Sub)) = N_Function_Specification
2409 and then Has_Null_Exclusion (Parent (Ren))
2410 and then not (Has_Null_Exclusion (Parent (Sub))
2411 or else Can_Never_Be_Null (Etype (Sub)))
2412 then
2413 Error_Msg_N
2414 ("return must specify `NOT NULL`",
2415 Result_Definition (Parent (Sub)));
2416 end if;
2417 end Check_Null_Exclusion;
2419 ---------------------------
2420 -- Freeze_Actual_Profile --
2421 ---------------------------
2423 procedure Freeze_Actual_Profile is
2424 F : Entity_Id;
2425 Has_Untagged_Inc : Boolean;
2426 Instantiation_Node : constant Node_Id := Parent (N);
2428 begin
2429 if Ada_Version >= Ada_2012 then
2430 F := First_Formal (Formal_Spec);
2431 Has_Untagged_Inc := False;
2432 while Present (F) loop
2433 if Ekind (Etype (F)) = E_Incomplete_Type
2434 and then not Is_Tagged_Type (Etype (F))
2435 then
2436 Has_Untagged_Inc := True;
2437 exit;
2438 end if;
2440 F := Next_Formal (F);
2441 end loop;
2443 if Ekind (Formal_Spec) = E_Function
2444 and then Ekind (Etype (Formal_Spec)) = E_Incomplete_Type
2445 and then not Is_Tagged_Type (Etype (F))
2446 then
2447 Has_Untagged_Inc := True;
2448 end if;
2450 if not Has_Untagged_Inc then
2451 F := First_Formal (Old_S);
2452 while Present (F) loop
2453 Freeze_Before (Instantiation_Node, Etype (F));
2455 if Is_Incomplete_Or_Private_Type (Etype (F))
2456 and then No (Underlying_Type (Etype (F)))
2457 then
2458 -- Exclude generic types, or types derived from them.
2459 -- They will be frozen in the enclosing instance.
2461 if Is_Generic_Type (Etype (F))
2462 or else Is_Generic_Type (Root_Type (Etype (F)))
2463 then
2464 null;
2465 else
2466 Error_Msg_NE
2467 ("type& must be frozen before this point",
2468 Instantiation_Node, Etype (F));
2469 end if;
2470 end if;
2472 F := Next_Formal (F);
2473 end loop;
2474 end if;
2475 end if;
2476 end Freeze_Actual_Profile;
2478 ---------------------------
2479 -- Has_Class_Wide_Actual --
2480 ---------------------------
2482 function Has_Class_Wide_Actual return Boolean is
2483 Formal : Entity_Id;
2484 Formal_Typ : Entity_Id;
2486 begin
2487 if Is_Actual then
2488 Formal := First_Formal (Formal_Spec);
2489 while Present (Formal) loop
2490 Formal_Typ := Etype (Formal);
2492 if Has_Unknown_Discriminants (Formal_Typ)
2493 and then not Is_Class_Wide_Type (Formal_Typ)
2494 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2495 then
2496 return True;
2497 end if;
2499 Next_Formal (Formal);
2500 end loop;
2501 end if;
2503 return False;
2504 end Has_Class_Wide_Actual;
2506 -------------------------
2507 -- Original_Subprogram --
2508 -------------------------
2510 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2511 Orig_Decl : Node_Id;
2512 Orig_Subp : Entity_Id;
2514 begin
2515 -- First case: renamed entity is itself a renaming
2517 if Present (Alias (Subp)) then
2518 return Alias (Subp);
2520 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2521 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2522 then
2523 -- Check if renamed entity is a renaming_as_body
2525 Orig_Decl :=
2526 Unit_Declaration_Node
2527 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2529 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2530 Orig_Subp := Entity (Name (Orig_Decl));
2532 if Orig_Subp = Rename_Spec then
2534 -- Circularity detected
2536 return Orig_Subp;
2538 else
2539 return (Original_Subprogram (Orig_Subp));
2540 end if;
2541 else
2542 return Subp;
2543 end if;
2544 else
2545 return Subp;
2546 end if;
2547 end Original_Subprogram;
2549 -- Local variables
2551 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2552 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2553 -- defaulted formal subprogram when the actual for a related formal
2554 -- type is class-wide.
2556 Inst_Node : Node_Id := Empty;
2557 New_S : Entity_Id;
2559 -- Start of processing for Analyze_Subprogram_Renaming
2561 begin
2562 -- We must test for the attribute renaming case before the Analyze
2563 -- call because otherwise Sem_Attr will complain that the attribute
2564 -- is missing an argument when it is analyzed.
2566 if Nkind (Nam) = N_Attribute_Reference then
2568 -- In the case of an abstract formal subprogram association, rewrite
2569 -- an actual given by a stream attribute as the name of the
2570 -- corresponding stream primitive of the type.
2572 -- In a generic context the stream operations are not generated, and
2573 -- this must be treated as a normal attribute reference, to be
2574 -- expanded in subsequent instantiations.
2576 if Is_Actual
2577 and then Is_Abstract_Subprogram (Formal_Spec)
2578 and then Expander_Active
2579 then
2580 declare
2581 Stream_Prim : Entity_Id;
2582 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
2584 begin
2585 -- The class-wide forms of the stream attributes are not
2586 -- primitive dispatching operations (even though they
2587 -- internally dispatch to a stream attribute).
2589 if Is_Class_Wide_Type (Prefix_Type) then
2590 Error_Msg_N
2591 ("attribute must be a primitive dispatching operation",
2592 Nam);
2593 return;
2594 end if;
2596 -- Retrieve the primitive subprogram associated with the
2597 -- attribute. This can only be a stream attribute, since those
2598 -- are the only ones that are dispatching (and the actual for
2599 -- an abstract formal subprogram must be dispatching
2600 -- operation).
2602 begin
2603 case Attribute_Name (Nam) is
2604 when Name_Input =>
2605 Stream_Prim :=
2606 Find_Prim_Op (Prefix_Type, TSS_Stream_Input);
2607 when Name_Output =>
2608 Stream_Prim :=
2609 Find_Prim_Op (Prefix_Type, TSS_Stream_Output);
2610 when Name_Read =>
2611 Stream_Prim :=
2612 Find_Prim_Op (Prefix_Type, TSS_Stream_Read);
2613 when Name_Write =>
2614 Stream_Prim :=
2615 Find_Prim_Op (Prefix_Type, TSS_Stream_Write);
2616 when others =>
2617 Error_Msg_N
2618 ("attribute must be a primitive"
2619 & " dispatching operation", Nam);
2620 return;
2621 end case;
2623 exception
2625 -- If no operation was found, and the type is limited,
2626 -- the user should have defined one.
2628 when Program_Error =>
2629 if Is_Limited_Type (Prefix_Type) then
2630 Error_Msg_NE
2631 ("stream operation not defined for type&",
2632 N, Prefix_Type);
2633 return;
2635 -- Otherwise, compiler should have generated default
2637 else
2638 raise;
2639 end if;
2640 end;
2642 -- Rewrite the attribute into the name of its corresponding
2643 -- primitive dispatching subprogram. We can then proceed with
2644 -- the usual processing for subprogram renamings.
2646 declare
2647 Prim_Name : constant Node_Id :=
2648 Make_Identifier (Sloc (Nam),
2649 Chars => Chars (Stream_Prim));
2650 begin
2651 Set_Entity (Prim_Name, Stream_Prim);
2652 Rewrite (Nam, Prim_Name);
2653 Analyze (Nam);
2654 end;
2655 end;
2657 -- Normal processing for a renaming of an attribute
2659 else
2660 Attribute_Renaming (N);
2661 return;
2662 end if;
2663 end if;
2665 -- Check whether this declaration corresponds to the instantiation
2666 -- of a formal subprogram.
2668 -- If this is an instantiation, the corresponding actual is frozen and
2669 -- error messages can be made more precise. If this is a default
2670 -- subprogram, the entity is already established in the generic, and is
2671 -- not retrieved by visibility. If it is a default with a box, the
2672 -- candidate interpretations, if any, have been collected when building
2673 -- the renaming declaration. If overloaded, the proper interpretation is
2674 -- determined in Find_Renamed_Entity. If the entity is an operator,
2675 -- Find_Renamed_Entity applies additional visibility checks.
2677 if Is_Actual then
2678 Inst_Node := Unit_Declaration_Node (Formal_Spec);
2680 -- Check whether the renaming is for a defaulted actual subprogram
2681 -- with a class-wide actual.
2683 if CW_Actual and then Box_Present (Inst_Node) then
2684 Build_Class_Wide_Wrapper (New_S, Old_S);
2686 elsif Is_Entity_Name (Nam)
2687 and then Present (Entity (Nam))
2688 and then not Comes_From_Source (Nam)
2689 and then not Is_Overloaded (Nam)
2690 then
2691 Old_S := Entity (Nam);
2692 New_S := Analyze_Subprogram_Specification (Spec);
2694 -- Operator case
2696 if Ekind (Entity (Nam)) = E_Operator then
2698 -- Box present
2700 if Box_Present (Inst_Node) then
2701 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2703 -- If there is an immediately visible homonym of the operator
2704 -- and the declaration has a default, this is worth a warning
2705 -- because the user probably did not intend to get the pre-
2706 -- defined operator, visible in the generic declaration. To
2707 -- find if there is an intended candidate, analyze the renaming
2708 -- again in the current context.
2710 elsif Scope (Old_S) = Standard_Standard
2711 and then Present (Default_Name (Inst_Node))
2712 then
2713 declare
2714 Decl : constant Node_Id := New_Copy_Tree (N);
2715 Hidden : Entity_Id;
2717 begin
2718 Set_Entity (Name (Decl), Empty);
2719 Analyze (Name (Decl));
2720 Hidden :=
2721 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
2723 if Present (Hidden)
2724 and then In_Open_Scopes (Scope (Hidden))
2725 and then Is_Immediately_Visible (Hidden)
2726 and then Comes_From_Source (Hidden)
2727 and then Hidden /= Old_S
2728 then
2729 Error_Msg_Sloc := Sloc (Hidden);
2730 Error_Msg_N ("default subprogram is resolved " &
2731 "in the generic declaration " &
2732 "(RM 12.6(17))??", N);
2733 Error_Msg_NE ("\and will not use & #??", N, Hidden);
2734 end if;
2735 end;
2736 end if;
2737 end if;
2739 else
2740 Analyze (Nam);
2741 New_S := Analyze_Subprogram_Specification (Spec);
2742 end if;
2744 else
2745 -- Renamed entity must be analyzed first, to avoid being hidden by
2746 -- new name (which might be the same in a generic instance).
2748 Analyze (Nam);
2750 -- The renaming defines a new overloaded entity, which is analyzed
2751 -- like a subprogram declaration.
2753 New_S := Analyze_Subprogram_Specification (Spec);
2754 end if;
2756 if Current_Scope /= Standard_Standard then
2757 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
2758 end if;
2760 -- Set SPARK mode from current context
2762 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
2763 Set_SPARK_Pragma_Inherited (New_S, True);
2765 Rename_Spec := Find_Corresponding_Spec (N);
2767 -- Case of Renaming_As_Body
2769 if Present (Rename_Spec) then
2771 -- Renaming declaration is the completion of the declaration of
2772 -- Rename_Spec. We build an actual body for it at the freezing point.
2774 Set_Corresponding_Spec (N, Rename_Spec);
2776 -- Deal with special case of stream functions of abstract types
2777 -- and interfaces.
2779 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
2780 N_Abstract_Subprogram_Declaration
2781 then
2782 -- Input stream functions are abstract if the object type is
2783 -- abstract. Similarly, all default stream functions for an
2784 -- interface type are abstract. However, these subprograms may
2785 -- receive explicit declarations in representation clauses, making
2786 -- the attribute subprograms usable as defaults in subsequent
2787 -- type extensions.
2788 -- In this case we rewrite the declaration to make the subprogram
2789 -- non-abstract. We remove the previous declaration, and insert
2790 -- the new one at the point of the renaming, to prevent premature
2791 -- access to unfrozen types. The new declaration reuses the
2792 -- specification of the previous one, and must not be analyzed.
2794 pragma Assert
2795 (Is_Primitive (Entity (Nam))
2796 and then
2797 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
2798 declare
2799 Old_Decl : constant Node_Id :=
2800 Unit_Declaration_Node (Rename_Spec);
2801 New_Decl : constant Node_Id :=
2802 Make_Subprogram_Declaration (Sloc (N),
2803 Specification =>
2804 Relocate_Node (Specification (Old_Decl)));
2805 begin
2806 Remove (Old_Decl);
2807 Insert_After (N, New_Decl);
2808 Set_Is_Abstract_Subprogram (Rename_Spec, False);
2809 Set_Analyzed (New_Decl);
2810 end;
2811 end if;
2813 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
2815 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2816 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
2817 end if;
2819 Set_Convention (New_S, Convention (Rename_Spec));
2820 Check_Fully_Conformant (New_S, Rename_Spec);
2821 Set_Public_Status (New_S);
2823 -- The specification does not introduce new formals, but only
2824 -- repeats the formals of the original subprogram declaration.
2825 -- For cross-reference purposes, and for refactoring tools, we
2826 -- treat the formals of the renaming declaration as body formals.
2828 Reference_Body_Formals (Rename_Spec, New_S);
2830 -- Indicate that the entity in the declaration functions like the
2831 -- corresponding body, and is not a new entity. The body will be
2832 -- constructed later at the freeze point, so indicate that the
2833 -- completion has not been seen yet.
2835 Set_Contract (New_S, Empty);
2836 Set_Ekind (New_S, E_Subprogram_Body);
2837 New_S := Rename_Spec;
2838 Set_Has_Completion (Rename_Spec, False);
2840 -- Ada 2005: check overriding indicator
2842 if Present (Overridden_Operation (Rename_Spec)) then
2843 if Must_Not_Override (Specification (N)) then
2844 Error_Msg_NE
2845 ("subprogram& overrides inherited operation",
2846 N, Rename_Spec);
2847 elsif
2848 Style_Check and then not Must_Override (Specification (N))
2849 then
2850 Style.Missing_Overriding (N, Rename_Spec);
2851 end if;
2853 elsif Must_Override (Specification (N)) then
2854 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
2855 end if;
2857 -- Normal subprogram renaming (not renaming as body)
2859 else
2860 Generate_Definition (New_S);
2861 New_Overloaded_Entity (New_S);
2863 if Is_Entity_Name (Nam)
2864 and then Is_Intrinsic_Subprogram (Entity (Nam))
2865 then
2866 null;
2867 else
2868 Check_Delayed_Subprogram (New_S);
2869 end if;
2870 end if;
2872 -- There is no need for elaboration checks on the new entity, which may
2873 -- be called before the next freezing point where the body will appear.
2874 -- Elaboration checks refer to the real entity, not the one created by
2875 -- the renaming declaration.
2877 Set_Kill_Elaboration_Checks (New_S, True);
2879 -- If we had a previous error, indicate a completely is present to stop
2880 -- junk cascaded messages, but don't take any further action.
2882 if Etype (Nam) = Any_Type then
2883 Set_Has_Completion (New_S);
2884 return;
2886 -- Case where name has the form of a selected component
2888 elsif Nkind (Nam) = N_Selected_Component then
2890 -- A name which has the form A.B can designate an entry of task A, a
2891 -- protected operation of protected object A, or finally a primitive
2892 -- operation of object A. In the later case, A is an object of some
2893 -- tagged type, or an access type that denotes one such. To further
2894 -- distinguish these cases, note that the scope of a task entry or
2895 -- protected operation is type of the prefix.
2897 -- The prefix could be an overloaded function call that returns both
2898 -- kinds of operations. This overloading pathology is left to the
2899 -- dedicated reader ???
2901 declare
2902 T : constant Entity_Id := Etype (Prefix (Nam));
2904 begin
2905 if Present (T)
2906 and then
2907 (Is_Tagged_Type (T)
2908 or else
2909 (Is_Access_Type (T)
2910 and then Is_Tagged_Type (Designated_Type (T))))
2911 and then Scope (Entity (Selector_Name (Nam))) /= T
2912 then
2913 Analyze_Renamed_Primitive_Operation
2914 (N, New_S, Present (Rename_Spec));
2915 return;
2917 else
2918 -- Renamed entity is an entry or protected operation. For those
2919 -- cases an explicit body is built (at the point of freezing of
2920 -- this entity) that contains a call to the renamed entity.
2922 -- This is not allowed for renaming as body if the renamed
2923 -- spec is already frozen (see RM 8.5.4(5) for details).
2925 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
2926 Error_Msg_N
2927 ("renaming-as-body cannot rename entry as subprogram", N);
2928 Error_Msg_NE
2929 ("\since & is already frozen (RM 8.5.4(5))",
2930 N, Rename_Spec);
2931 else
2932 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
2933 end if;
2935 return;
2936 end if;
2937 end;
2939 -- Case where name is an explicit dereference X.all
2941 elsif Nkind (Nam) = N_Explicit_Dereference then
2943 -- Renamed entity is designated by access_to_subprogram expression.
2944 -- Must build body to encapsulate call, as in the entry case.
2946 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
2947 return;
2949 -- Indexed component
2951 elsif Nkind (Nam) = N_Indexed_Component then
2952 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
2953 return;
2955 -- Character literal
2957 elsif Nkind (Nam) = N_Character_Literal then
2958 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
2959 return;
2961 -- Only remaining case is where we have a non-entity name, or a renaming
2962 -- of some other non-overloadable entity.
2964 elsif not Is_Entity_Name (Nam)
2965 or else not Is_Overloadable (Entity (Nam))
2966 then
2967 -- Do not mention the renaming if it comes from an instance
2969 if not Is_Actual then
2970 Error_Msg_N ("expect valid subprogram name in renaming", N);
2971 else
2972 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
2973 end if;
2975 return;
2976 end if;
2978 -- Find the renamed entity that matches the given specification. Disable
2979 -- Ada_83 because there is no requirement of full conformance between
2980 -- renamed entity and new entity, even though the same circuit is used.
2982 -- This is a bit of an odd case, which introduces a really irregular use
2983 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
2984 -- this. ???
2986 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
2987 Ada_Version_Pragma := Empty;
2988 Ada_Version_Explicit := Ada_Version;
2990 if No (Old_S) then
2991 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2993 -- The visible operation may be an inherited abstract operation that
2994 -- was overridden in the private part, in which case a call will
2995 -- dispatch to the overriding operation. Use the overriding one in
2996 -- the renaming declaration, to prevent spurious errors below.
2998 if Is_Overloadable (Old_S)
2999 and then Is_Abstract_Subprogram (Old_S)
3000 and then No (DTC_Entity (Old_S))
3001 and then Present (Alias (Old_S))
3002 and then not Is_Abstract_Subprogram (Alias (Old_S))
3003 and then Present (Overridden_Operation (Alias (Old_S)))
3004 then
3005 Old_S := Alias (Old_S);
3006 end if;
3008 -- When the renamed subprogram is overloaded and used as an actual
3009 -- of a generic, its entity is set to the first available homonym.
3010 -- We must first disambiguate the name, then set the proper entity.
3012 if Is_Actual and then Is_Overloaded (Nam) then
3013 Set_Entity (Nam, Old_S);
3014 end if;
3015 end if;
3017 -- Most common case: subprogram renames subprogram. No body is generated
3018 -- in this case, so we must indicate the declaration is complete as is.
3019 -- and inherit various attributes of the renamed subprogram.
3021 if No (Rename_Spec) then
3022 Set_Has_Completion (New_S);
3023 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3024 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3025 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3027 -- A subprogram renaming is Ghost if the renamed entity is Ghost or
3028 -- the construct appears within a Ghost scope.
3030 if Is_Ghost_Entity (Entity (Nam)) or else Within_Ghost_Scope then
3031 Set_Is_Ghost_Entity (New_S);
3032 end if;
3034 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3035 -- between a subprogram and its correct renaming.
3037 -- Note: the Any_Id check is a guard that prevents compiler crashes
3038 -- when performing a null exclusion check between a renaming and a
3039 -- renamed subprogram that has been found to be illegal.
3041 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3042 Check_Null_Exclusion
3043 (Ren => New_S,
3044 Sub => Entity (Nam));
3045 end if;
3047 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3048 -- overriding. The flag Requires_Overriding is set very selectively
3049 -- and misses some other illegal cases. The additional conditions
3050 -- checked below are sufficient but not necessary ???
3052 -- The rule does not apply to the renaming generated for an actual
3053 -- subprogram in an instance.
3055 if Is_Actual then
3056 null;
3058 -- Guard against previous errors, and omit renamings of predefined
3059 -- operators.
3061 elsif not Ekind_In (Old_S, E_Function, E_Procedure) then
3062 null;
3064 elsif Requires_Overriding (Old_S)
3065 or else
3066 (Is_Abstract_Subprogram (Old_S)
3067 and then Present (Find_Dispatching_Type (Old_S))
3068 and then
3069 not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3070 then
3071 Error_Msg_N
3072 ("renamed entity cannot be "
3073 & "subprogram that requires overriding (RM 8.5.4 (5.1))", N);
3074 end if;
3075 end if;
3077 if Old_S /= Any_Id then
3078 if Is_Actual and then From_Default (N) then
3080 -- This is an implicit reference to the default actual
3082 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3084 else
3085 Generate_Reference (Old_S, Nam);
3086 end if;
3088 Check_Internal_Protected_Use (N, Old_S);
3090 -- For a renaming-as-body, require subtype conformance, but if the
3091 -- declaration being completed has not been frozen, then inherit the
3092 -- convention of the renamed subprogram prior to checking conformance
3093 -- (unless the renaming has an explicit convention established; the
3094 -- rule stated in the RM doesn't seem to address this ???).
3096 if Present (Rename_Spec) then
3097 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3098 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3100 if not Is_Frozen (Rename_Spec) then
3101 if not Has_Convention_Pragma (Rename_Spec) then
3102 Set_Convention (New_S, Convention (Old_S));
3103 end if;
3105 if Ekind (Old_S) /= E_Operator then
3106 Check_Mode_Conformant (New_S, Old_S, Spec);
3107 end if;
3109 if Original_Subprogram (Old_S) = Rename_Spec then
3110 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
3111 end if;
3112 else
3113 Check_Subtype_Conformant (New_S, Old_S, Spec);
3114 end if;
3116 Check_Frozen_Renaming (N, Rename_Spec);
3118 -- Check explicitly that renamed entity is not intrinsic, because
3119 -- in a generic the renamed body is not built. In this case,
3120 -- the renaming_as_body is a completion.
3122 if Inside_A_Generic then
3123 if Is_Frozen (Rename_Spec)
3124 and then Is_Intrinsic_Subprogram (Old_S)
3125 then
3126 Error_Msg_N
3127 ("subprogram in renaming_as_body cannot be intrinsic",
3128 Name (N));
3129 end if;
3131 Set_Has_Completion (Rename_Spec);
3132 end if;
3134 elsif Ekind (Old_S) /= E_Operator then
3136 -- If this a defaulted subprogram for a class-wide actual there is
3137 -- no check for mode conformance, given that the signatures don't
3138 -- match (the source mentions T but the actual mentions T'Class).
3140 if CW_Actual then
3141 null;
3142 elsif not Is_Actual or else No (Enclosing_Instance) then
3143 Check_Mode_Conformant (New_S, Old_S);
3144 end if;
3146 if Is_Actual and then Error_Posted (New_S) then
3147 Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
3148 end if;
3149 end if;
3151 if No (Rename_Spec) then
3153 -- The parameter profile of the new entity is that of the renamed
3154 -- entity: the subtypes given in the specification are irrelevant.
3156 Inherit_Renamed_Profile (New_S, Old_S);
3158 -- A call to the subprogram is transformed into a call to the
3159 -- renamed entity. This is transitive if the renamed entity is
3160 -- itself a renaming.
3162 if Present (Alias (Old_S)) then
3163 Set_Alias (New_S, Alias (Old_S));
3164 else
3165 Set_Alias (New_S, Old_S);
3166 end if;
3168 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3169 -- renaming as body, since the entity in this case is not an
3170 -- intrinsic (it calls an intrinsic, but we have a real body for
3171 -- this call, and it is in this body that the required intrinsic
3172 -- processing will take place).
3174 -- Also, if this is a renaming of inequality, the renamed operator
3175 -- is intrinsic, but what matters is the corresponding equality
3176 -- operator, which may be user-defined.
3178 Set_Is_Intrinsic_Subprogram
3179 (New_S,
3180 Is_Intrinsic_Subprogram (Old_S)
3181 and then
3182 (Chars (Old_S) /= Name_Op_Ne
3183 or else Ekind (Old_S) = E_Operator
3184 or else Is_Intrinsic_Subprogram
3185 (Corresponding_Equality (Old_S))));
3187 if Ekind (Alias (New_S)) = E_Operator then
3188 Set_Has_Delayed_Freeze (New_S, False);
3189 end if;
3191 -- If the renaming corresponds to an association for an abstract
3192 -- formal subprogram, then various attributes must be set to
3193 -- indicate that the renaming is an abstract dispatching operation
3194 -- with a controlling type.
3196 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
3198 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3199 -- see it as corresponding to a generic association for a
3200 -- formal abstract subprogram
3202 Set_Is_Abstract_Subprogram (New_S);
3204 declare
3205 New_S_Ctrl_Type : constant Entity_Id :=
3206 Find_Dispatching_Type (New_S);
3207 Old_S_Ctrl_Type : constant Entity_Id :=
3208 Find_Dispatching_Type (Old_S);
3210 begin
3211 if Old_S_Ctrl_Type /= New_S_Ctrl_Type then
3212 Error_Msg_NE
3213 ("actual must be dispatching subprogram for type&",
3214 Nam, New_S_Ctrl_Type);
3216 else
3217 Set_Is_Dispatching_Operation (New_S);
3218 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3220 -- If the actual in the formal subprogram is itself a
3221 -- formal abstract subprogram association, there's no
3222 -- dispatch table component or position to inherit.
3224 if Present (DTC_Entity (Old_S)) then
3225 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3226 Set_DT_Position (New_S, DT_Position (Old_S));
3227 end if;
3228 end if;
3229 end;
3230 end if;
3231 end if;
3233 if Is_Actual then
3234 null;
3236 -- The following is illegal, because F hides whatever other F may
3237 -- be around:
3238 -- function F (...) renames F;
3240 elsif Old_S = New_S
3241 or else (Nkind (Nam) /= N_Expanded_Name
3242 and then Chars (Old_S) = Chars (New_S))
3243 then
3244 Error_Msg_N ("subprogram cannot rename itself", N);
3246 -- This is illegal even if we use a selector:
3247 -- function F (...) renames Pkg.F;
3248 -- because F is still hidden.
3250 elsif Nkind (Nam) = N_Expanded_Name
3251 and then Entity (Prefix (Nam)) = Current_Scope
3252 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3253 then
3254 -- This is an error, but we overlook the error and accept the
3255 -- renaming if the special Overriding_Renamings mode is in effect.
3257 if not Overriding_Renamings then
3258 Error_Msg_NE
3259 ("implicit operation& is not visible (RM 8.3 (15))",
3260 Nam, Old_S);
3261 end if;
3262 end if;
3264 Set_Convention (New_S, Convention (Old_S));
3266 if Is_Abstract_Subprogram (Old_S) then
3267 if Present (Rename_Spec) then
3268 Error_Msg_N
3269 ("a renaming-as-body cannot rename an abstract subprogram",
3271 Set_Has_Completion (Rename_Spec);
3272 else
3273 Set_Is_Abstract_Subprogram (New_S);
3274 end if;
3275 end if;
3277 Check_Library_Unit_Renaming (N, Old_S);
3279 -- Pathological case: procedure renames entry in the scope of its
3280 -- task. Entry is given by simple name, but body must be built for
3281 -- procedure. Of course if called it will deadlock.
3283 if Ekind (Old_S) = E_Entry then
3284 Set_Has_Completion (New_S, False);
3285 Set_Alias (New_S, Empty);
3286 end if;
3288 if Is_Actual then
3289 Freeze_Before (N, Old_S);
3290 Freeze_Actual_Profile;
3291 Set_Has_Delayed_Freeze (New_S, False);
3292 Freeze_Before (N, New_S);
3294 -- An abstract subprogram is only allowed as an actual in the case
3295 -- where the formal subprogram is also abstract.
3297 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
3298 and then Is_Abstract_Subprogram (Old_S)
3299 and then not Is_Abstract_Subprogram (Formal_Spec)
3300 then
3301 Error_Msg_N
3302 ("abstract subprogram not allowed as generic actual", Nam);
3303 end if;
3304 end if;
3306 else
3307 -- A common error is to assume that implicit operators for types are
3308 -- defined in Standard, or in the scope of a subtype. In those cases
3309 -- where the renamed entity is given with an expanded name, it is
3310 -- worth mentioning that operators for the type are not declared in
3311 -- the scope given by the prefix.
3313 if Nkind (Nam) = N_Expanded_Name
3314 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
3315 and then Scope (Entity (Nam)) = Standard_Standard
3316 then
3317 declare
3318 T : constant Entity_Id :=
3319 Base_Type (Etype (First_Formal (New_S)));
3320 begin
3321 Error_Msg_Node_2 := Prefix (Nam);
3322 Error_Msg_NE
3323 ("operator for type& is not declared in&", Prefix (Nam), T);
3324 end;
3326 else
3327 Error_Msg_NE
3328 ("no visible subprogram matches the specification for&",
3329 Spec, New_S);
3330 end if;
3332 if Present (Candidate_Renaming) then
3333 declare
3334 F1 : Entity_Id;
3335 F2 : Entity_Id;
3336 T1 : Entity_Id;
3338 begin
3339 F1 := First_Formal (Candidate_Renaming);
3340 F2 := First_Formal (New_S);
3341 T1 := First_Subtype (Etype (F1));
3342 while Present (F1) and then Present (F2) loop
3343 Next_Formal (F1);
3344 Next_Formal (F2);
3345 end loop;
3347 if Present (F1) and then Present (Default_Value (F1)) then
3348 if Present (Next_Formal (F1)) then
3349 Error_Msg_NE
3350 ("\missing specification for &" &
3351 " and other formals with defaults", Spec, F1);
3352 else
3353 Error_Msg_NE
3354 ("\missing specification for &", Spec, F1);
3355 end if;
3356 end if;
3358 if Nkind (Nam) = N_Operator_Symbol
3359 and then From_Default (N)
3360 then
3361 Error_Msg_Node_2 := T1;
3362 Error_Msg_NE
3363 ("default & on & is not directly visible",
3364 Nam, Nam);
3365 end if;
3366 end;
3367 end if;
3368 end if;
3370 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
3371 -- controlling access parameters are known non-null for the renamed
3372 -- subprogram. Test also applies to a subprogram instantiation that
3373 -- is dispatching. Test is skipped if some previous error was detected
3374 -- that set Old_S to Any_Id.
3376 if Ada_Version >= Ada_2005
3377 and then Old_S /= Any_Id
3378 and then not Is_Dispatching_Operation (Old_S)
3379 and then Is_Dispatching_Operation (New_S)
3380 then
3381 declare
3382 Old_F : Entity_Id;
3383 New_F : Entity_Id;
3385 begin
3386 Old_F := First_Formal (Old_S);
3387 New_F := First_Formal (New_S);
3388 while Present (Old_F) loop
3389 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
3390 and then Is_Controlling_Formal (New_F)
3391 and then not Can_Never_Be_Null (Old_F)
3392 then
3393 Error_Msg_N ("access parameter is controlling,", New_F);
3394 Error_Msg_NE
3395 ("\corresponding parameter of& "
3396 & "must be explicitly null excluding", New_F, Old_S);
3397 end if;
3399 Next_Formal (Old_F);
3400 Next_Formal (New_F);
3401 end loop;
3402 end;
3403 end if;
3405 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
3406 -- is to warn if an operator is being renamed as a different operator.
3407 -- If the operator is predefined, examine the kind of the entity, not
3408 -- the abbreviated declaration in Standard.
3410 if Comes_From_Source (N)
3411 and then Present (Old_S)
3412 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3413 or else Ekind (Old_S) = E_Operator)
3414 and then Nkind (New_S) = N_Defining_Operator_Symbol
3415 and then Chars (Old_S) /= Chars (New_S)
3416 then
3417 Error_Msg_NE
3418 ("& is being renamed as a different operator??", N, Old_S);
3419 end if;
3421 -- Check for renaming of obsolescent subprogram
3423 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3425 -- Another warning or some utility: if the new subprogram as the same
3426 -- name as the old one, the old one is not hidden by an outer homograph,
3427 -- the new one is not a public symbol, and the old one is otherwise
3428 -- directly visible, the renaming is superfluous.
3430 if Chars (Old_S) = Chars (New_S)
3431 and then Comes_From_Source (N)
3432 and then Scope (Old_S) /= Standard_Standard
3433 and then Warn_On_Redundant_Constructs
3434 and then (Is_Immediately_Visible (Old_S)
3435 or else Is_Potentially_Use_Visible (Old_S))
3436 and then Is_Overloadable (Current_Scope)
3437 and then Chars (Current_Scope) /= Chars (Old_S)
3438 then
3439 Error_Msg_N
3440 ("redundant renaming, entity is directly visible?r?", Name (N));
3441 end if;
3443 -- Implementation-defined aspect specifications can appear in a renaming
3444 -- declaration, but not language-defined ones. The call to procedure
3445 -- Analyze_Aspect_Specifications will take care of this error check.
3447 if Has_Aspects (N) then
3448 Analyze_Aspect_Specifications (N, New_S);
3449 end if;
3451 Ada_Version := Save_AV;
3452 Ada_Version_Pragma := Save_AVP;
3453 Ada_Version_Explicit := Save_AV_Exp;
3454 end Analyze_Subprogram_Renaming;
3456 -------------------------
3457 -- Analyze_Use_Package --
3458 -------------------------
3460 -- Resolve the package names in the use clause, and make all the visible
3461 -- entities defined in the package potentially use-visible. If the package
3462 -- is already in use from a previous use clause, its visible entities are
3463 -- already use-visible. In that case, mark the occurrence as a redundant
3464 -- use. If the package is an open scope, i.e. if the use clause occurs
3465 -- within the package itself, ignore it.
3467 procedure Analyze_Use_Package (N : Node_Id) is
3468 Pack_Name : Node_Id;
3469 Pack : Entity_Id;
3471 -- Start of processing for Analyze_Use_Package
3473 begin
3474 Check_SPARK_05_Restriction ("use clause is not allowed", N);
3476 Set_Hidden_By_Use_Clause (N, No_Elist);
3478 -- Use clause not allowed in a spec of a predefined package declaration
3479 -- except that packages whose file name starts a-n are OK (these are
3480 -- children of Ada.Numerics, which are never loaded by Rtsfind).
3482 if Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
3483 and then Name_Buffer (1 .. 3) /= "a-n"
3484 and then
3485 Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3486 then
3487 Error_Msg_N ("use clause not allowed in predefined spec", N);
3488 end if;
3490 -- Chain clause to list of use clauses in current scope
3492 if Nkind (Parent (N)) /= N_Compilation_Unit then
3493 Chain_Use_Clause (N);
3494 end if;
3496 -- Loop through package names to identify referenced packages
3498 Pack_Name := First (Names (N));
3499 while Present (Pack_Name) loop
3500 Analyze (Pack_Name);
3502 if Nkind (Parent (N)) = N_Compilation_Unit
3503 and then Nkind (Pack_Name) = N_Expanded_Name
3504 then
3505 declare
3506 Pref : Node_Id;
3508 begin
3509 Pref := Prefix (Pack_Name);
3510 while Nkind (Pref) = N_Expanded_Name loop
3511 Pref := Prefix (Pref);
3512 end loop;
3514 if Entity (Pref) = Standard_Standard then
3515 Error_Msg_N
3516 ("predefined package Standard cannot appear"
3517 & " in a context clause", Pref);
3518 end if;
3519 end;
3520 end if;
3522 Next (Pack_Name);
3523 end loop;
3525 -- Loop through package names to mark all entities as potentially
3526 -- use visible.
3528 Pack_Name := First (Names (N));
3529 while Present (Pack_Name) loop
3530 if Is_Entity_Name (Pack_Name) then
3531 Pack := Entity (Pack_Name);
3533 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
3534 if Ekind (Pack) = E_Generic_Package then
3535 Error_Msg_N -- CODEFIX
3536 ("a generic package is not allowed in a use clause",
3537 Pack_Name);
3538 else
3539 Error_Msg_N ("& is not a usable package", Pack_Name);
3540 end if;
3542 else
3543 if Nkind (Parent (N)) = N_Compilation_Unit then
3544 Check_In_Previous_With_Clause (N, Pack_Name);
3545 end if;
3547 if Applicable_Use (Pack_Name) then
3548 Use_One_Package (Pack, N);
3549 end if;
3550 end if;
3552 -- Report error because name denotes something other than a package
3554 else
3555 Error_Msg_N ("& is not a package", Pack_Name);
3556 end if;
3558 Next (Pack_Name);
3559 end loop;
3560 end Analyze_Use_Package;
3562 ----------------------
3563 -- Analyze_Use_Type --
3564 ----------------------
3566 procedure Analyze_Use_Type (N : Node_Id) is
3567 E : Entity_Id;
3568 Id : Node_Id;
3570 begin
3571 Set_Hidden_By_Use_Clause (N, No_Elist);
3573 -- Chain clause to list of use clauses in current scope
3575 if Nkind (Parent (N)) /= N_Compilation_Unit then
3576 Chain_Use_Clause (N);
3577 end if;
3579 -- If the Used_Operations list is already initialized, the clause has
3580 -- been analyzed previously, and it is begin reinstalled, for example
3581 -- when the clause appears in a package spec and we are compiling the
3582 -- corresponding package body. In that case, make the entities on the
3583 -- existing list use_visible, and mark the corresponding types In_Use.
3585 if Present (Used_Operations (N)) then
3586 declare
3587 Mark : Node_Id;
3588 Elmt : Elmt_Id;
3590 begin
3591 Mark := First (Subtype_Marks (N));
3592 while Present (Mark) loop
3593 Use_One_Type (Mark, Installed => True);
3594 Next (Mark);
3595 end loop;
3597 Elmt := First_Elmt (Used_Operations (N));
3598 while Present (Elmt) loop
3599 Set_Is_Potentially_Use_Visible (Node (Elmt));
3600 Next_Elmt (Elmt);
3601 end loop;
3602 end;
3604 return;
3605 end if;
3607 -- Otherwise, create new list and attach to it the operations that
3608 -- are made use-visible by the clause.
3610 Set_Used_Operations (N, New_Elmt_List);
3611 Id := First (Subtype_Marks (N));
3612 while Present (Id) loop
3613 Find_Type (Id);
3614 E := Entity (Id);
3616 if E /= Any_Type then
3617 Use_One_Type (Id);
3619 if Nkind (Parent (N)) = N_Compilation_Unit then
3620 if Nkind (Id) = N_Identifier then
3621 Error_Msg_N ("type is not directly visible", Id);
3623 elsif Is_Child_Unit (Scope (E))
3624 and then Scope (E) /= System_Aux_Id
3625 then
3626 Check_In_Previous_With_Clause (N, Prefix (Id));
3627 end if;
3628 end if;
3630 else
3631 -- If the use_type_clause appears in a compilation unit context,
3632 -- check whether it comes from a unit that may appear in a
3633 -- limited_with_clause, for a better error message.
3635 if Nkind (Parent (N)) = N_Compilation_Unit
3636 and then Nkind (Id) /= N_Identifier
3637 then
3638 declare
3639 Item : Node_Id;
3640 Pref : Node_Id;
3642 function Mentioned (Nam : Node_Id) return Boolean;
3643 -- Check whether the prefix of expanded name for the type
3644 -- appears in the prefix of some limited_with_clause.
3646 ---------------
3647 -- Mentioned --
3648 ---------------
3650 function Mentioned (Nam : Node_Id) return Boolean is
3651 begin
3652 return Nkind (Name (Item)) = N_Selected_Component
3653 and then Chars (Prefix (Name (Item))) = Chars (Nam);
3654 end Mentioned;
3656 begin
3657 Pref := Prefix (Id);
3658 Item := First (Context_Items (Parent (N)));
3659 while Present (Item) and then Item /= N loop
3660 if Nkind (Item) = N_With_Clause
3661 and then Limited_Present (Item)
3662 and then Mentioned (Pref)
3663 then
3664 Change_Error_Text
3665 (Get_Msg_Id, "premature usage of incomplete type");
3666 end if;
3668 Next (Item);
3669 end loop;
3670 end;
3671 end if;
3672 end if;
3674 Next (Id);
3675 end loop;
3676 end Analyze_Use_Type;
3678 --------------------
3679 -- Applicable_Use --
3680 --------------------
3682 function Applicable_Use (Pack_Name : Node_Id) return Boolean is
3683 Pack : constant Entity_Id := Entity (Pack_Name);
3685 begin
3686 if In_Open_Scopes (Pack) then
3687 if Warn_On_Redundant_Constructs and then Pack = Current_Scope then
3688 Error_Msg_NE -- CODEFIX
3689 ("& is already use-visible within itself?r?", Pack_Name, Pack);
3690 end if;
3692 return False;
3694 elsif In_Use (Pack) then
3695 Note_Redundant_Use (Pack_Name);
3696 return False;
3698 elsif Present (Renamed_Object (Pack))
3699 and then In_Use (Renamed_Object (Pack))
3700 then
3701 Note_Redundant_Use (Pack_Name);
3702 return False;
3704 else
3705 return True;
3706 end if;
3707 end Applicable_Use;
3709 ------------------------
3710 -- Attribute_Renaming --
3711 ------------------------
3713 procedure Attribute_Renaming (N : Node_Id) is
3714 Loc : constant Source_Ptr := Sloc (N);
3715 Nam : constant Node_Id := Name (N);
3716 Spec : constant Node_Id := Specification (N);
3717 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
3718 Aname : constant Name_Id := Attribute_Name (Nam);
3720 Form_Num : Nat := 0;
3721 Expr_List : List_Id := No_List;
3723 Attr_Node : Node_Id;
3724 Body_Node : Node_Id;
3725 Param_Spec : Node_Id;
3727 begin
3728 Generate_Definition (New_S);
3730 -- This procedure is called in the context of subprogram renaming, and
3731 -- thus the attribute must be one that is a subprogram. All of those
3732 -- have at least one formal parameter, with the exceptions of the GNAT
3733 -- attribute 'Img, which GNAT treats as renameable.
3735 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
3736 if Aname /= Name_Img then
3737 Error_Msg_N
3738 ("subprogram renaming an attribute must have formals", N);
3739 return;
3740 end if;
3742 else
3743 Param_Spec := First (Parameter_Specifications (Spec));
3744 while Present (Param_Spec) loop
3745 Form_Num := Form_Num + 1;
3747 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
3748 Find_Type (Parameter_Type (Param_Spec));
3750 -- The profile of the new entity denotes the base type (s) of
3751 -- the types given in the specification. For access parameters
3752 -- there are no subtypes involved.
3754 Rewrite (Parameter_Type (Param_Spec),
3755 New_Occurrence_Of
3756 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
3757 end if;
3759 if No (Expr_List) then
3760 Expr_List := New_List;
3761 end if;
3763 Append_To (Expr_List,
3764 Make_Identifier (Loc,
3765 Chars => Chars (Defining_Identifier (Param_Spec))));
3767 -- The expressions in the attribute reference are not freeze
3768 -- points. Neither is the attribute as a whole, see below.
3770 Set_Must_Not_Freeze (Last (Expr_List));
3771 Next (Param_Spec);
3772 end loop;
3773 end if;
3775 -- Immediate error if too many formals. Other mismatches in number or
3776 -- types of parameters are detected when we analyze the body of the
3777 -- subprogram that we construct.
3779 if Form_Num > 2 then
3780 Error_Msg_N ("too many formals for attribute", N);
3782 -- Error if the attribute reference has expressions that look like
3783 -- formal parameters.
3785 elsif Present (Expressions (Nam)) then
3786 Error_Msg_N ("illegal expressions in attribute reference", Nam);
3788 elsif
3789 Nam_In (Aname, Name_Compose, Name_Exponent, Name_Leading_Part,
3790 Name_Pos, Name_Round, Name_Scaling,
3791 Name_Val)
3792 then
3793 if Nkind (N) = N_Subprogram_Renaming_Declaration
3794 and then Present (Corresponding_Formal_Spec (N))
3795 then
3796 Error_Msg_N
3797 ("generic actual cannot be attribute involving universal type",
3798 Nam);
3799 else
3800 Error_Msg_N
3801 ("attribute involving a universal type cannot be renamed",
3802 Nam);
3803 end if;
3804 end if;
3806 -- Rewrite attribute node to have a list of expressions corresponding to
3807 -- the subprogram formals. A renaming declaration is not a freeze point,
3808 -- and the analysis of the attribute reference should not freeze the
3809 -- type of the prefix. We use the original node in the renaming so that
3810 -- its source location is preserved, and checks on stream attributes are
3811 -- properly applied.
3813 Attr_Node := Relocate_Node (Nam);
3814 Set_Expressions (Attr_Node, Expr_List);
3816 Set_Must_Not_Freeze (Attr_Node);
3817 Set_Must_Not_Freeze (Prefix (Nam));
3819 -- Case of renaming a function
3821 if Nkind (Spec) = N_Function_Specification then
3822 if Is_Procedure_Attribute_Name (Aname) then
3823 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
3824 return;
3825 end if;
3827 Find_Type (Result_Definition (Spec));
3828 Rewrite (Result_Definition (Spec),
3829 New_Occurrence_Of
3830 (Base_Type (Entity (Result_Definition (Spec))), Loc));
3832 Body_Node :=
3833 Make_Subprogram_Body (Loc,
3834 Specification => Spec,
3835 Declarations => New_List,
3836 Handled_Statement_Sequence =>
3837 Make_Handled_Sequence_Of_Statements (Loc,
3838 Statements => New_List (
3839 Make_Simple_Return_Statement (Loc,
3840 Expression => Attr_Node))));
3842 -- Case of renaming a procedure
3844 else
3845 if not Is_Procedure_Attribute_Name (Aname) then
3846 Error_Msg_N ("attribute can only be renamed as function", Nam);
3847 return;
3848 end if;
3850 Body_Node :=
3851 Make_Subprogram_Body (Loc,
3852 Specification => Spec,
3853 Declarations => New_List,
3854 Handled_Statement_Sequence =>
3855 Make_Handled_Sequence_Of_Statements (Loc,
3856 Statements => New_List (Attr_Node)));
3857 end if;
3859 -- In case of tagged types we add the body of the generated function to
3860 -- the freezing actions of the type (because in the general case such
3861 -- type is still not frozen). We exclude from this processing generic
3862 -- formal subprograms found in instantiations.
3864 -- We must exclude VM targets and restricted run-time libraries because
3865 -- entity AST_Handler is defined in package System.Aux_Dec which is not
3866 -- available in those platforms. Note that we cannot use the function
3867 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
3868 -- the ZFP run-time library is not defined as a profile, and we do not
3869 -- want to deal with AST_Handler in ZFP mode.
3871 if VM_Target = No_VM
3872 and then not Configurable_Run_Time_Mode
3873 and then not Present (Corresponding_Formal_Spec (N))
3874 and then Etype (Nam) /= RTE (RE_AST_Handler)
3875 then
3876 declare
3877 P : constant Node_Id := Prefix (Nam);
3879 begin
3880 -- The prefix of 'Img is an object that is evaluated for each call
3881 -- of the function that renames it.
3883 if Aname = Name_Img then
3884 Preanalyze_And_Resolve (P);
3886 -- For all other attribute renamings, the prefix is a subtype
3888 else
3889 Find_Type (P);
3890 end if;
3892 -- If the target type is not yet frozen, add the body to the
3893 -- actions to be elaborated at freeze time.
3895 if Is_Tagged_Type (Etype (P))
3896 and then In_Open_Scopes (Scope (Etype (P)))
3897 then
3898 Ensure_Freeze_Node (Etype (P));
3899 Append_Freeze_Action (Etype (P), Body_Node);
3900 else
3901 Rewrite (N, Body_Node);
3902 Analyze (N);
3903 Set_Etype (New_S, Base_Type (Etype (New_S)));
3904 end if;
3905 end;
3907 -- Generic formal subprograms or AST_Handler renaming
3909 else
3910 Rewrite (N, Body_Node);
3911 Analyze (N);
3912 Set_Etype (New_S, Base_Type (Etype (New_S)));
3913 end if;
3915 if Is_Compilation_Unit (New_S) then
3916 Error_Msg_N
3917 ("a library unit can only rename another library unit", N);
3918 end if;
3920 -- We suppress elaboration warnings for the resulting entity, since
3921 -- clearly they are not needed, and more particularly, in the case
3922 -- of a generic formal subprogram, the resulting entity can appear
3923 -- after the instantiation itself, and thus look like a bogus case
3924 -- of access before elaboration.
3926 Set_Suppress_Elaboration_Warnings (New_S);
3928 end Attribute_Renaming;
3930 ----------------------
3931 -- Chain_Use_Clause --
3932 ----------------------
3934 procedure Chain_Use_Clause (N : Node_Id) is
3935 Pack : Entity_Id;
3936 Level : Int := Scope_Stack.Last;
3938 begin
3939 if not Is_Compilation_Unit (Current_Scope)
3940 or else not Is_Child_Unit (Current_Scope)
3941 then
3942 null; -- Common case
3944 elsif Defining_Entity (Parent (N)) = Current_Scope then
3945 null; -- Common case for compilation unit
3947 else
3948 -- If declaration appears in some other scope, it must be in some
3949 -- parent unit when compiling a child.
3951 Pack := Defining_Entity (Parent (N));
3952 if not In_Open_Scopes (Pack) then
3953 null; -- default as well
3955 else
3956 -- Find entry for parent unit in scope stack
3958 while Scope_Stack.Table (Level).Entity /= Pack loop
3959 Level := Level - 1;
3960 end loop;
3961 end if;
3962 end if;
3964 Set_Next_Use_Clause (N,
3965 Scope_Stack.Table (Level).First_Use_Clause);
3966 Scope_Stack.Table (Level).First_Use_Clause := N;
3967 end Chain_Use_Clause;
3969 ---------------------------
3970 -- Check_Frozen_Renaming --
3971 ---------------------------
3973 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
3974 B_Node : Node_Id;
3975 Old_S : Entity_Id;
3977 begin
3978 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
3979 B_Node :=
3980 Build_Renamed_Body
3981 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
3983 if Is_Entity_Name (Name (N)) then
3984 Old_S := Entity (Name (N));
3986 if not Is_Frozen (Old_S)
3987 and then Operating_Mode /= Check_Semantics
3988 then
3989 Append_Freeze_Action (Old_S, B_Node);
3990 else
3991 Insert_After (N, B_Node);
3992 Analyze (B_Node);
3993 end if;
3995 if Is_Intrinsic_Subprogram (Old_S) and then not In_Instance then
3996 Error_Msg_N
3997 ("subprogram used in renaming_as_body cannot be intrinsic",
3998 Name (N));
3999 end if;
4001 else
4002 Insert_After (N, B_Node);
4003 Analyze (B_Node);
4004 end if;
4005 end if;
4006 end Check_Frozen_Renaming;
4008 -------------------------------
4009 -- Set_Entity_Or_Discriminal --
4010 -------------------------------
4012 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4013 P : Node_Id;
4015 begin
4016 -- If the entity is not a discriminant, or else expansion is disabled,
4017 -- simply set the entity.
4019 if not In_Spec_Expression
4020 or else Ekind (E) /= E_Discriminant
4021 or else Inside_A_Generic
4022 then
4023 Set_Entity_With_Checks (N, E);
4025 -- The replacement of a discriminant by the corresponding discriminal
4026 -- is not done for a task discriminant that appears in a default
4027 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4028 -- for details on their handling.
4030 elsif Is_Concurrent_Type (Scope (E)) then
4031 P := Parent (N);
4032 while Present (P)
4033 and then not Nkind_In (P, N_Parameter_Specification,
4034 N_Component_Declaration)
4035 loop
4036 P := Parent (P);
4037 end loop;
4039 if Present (P)
4040 and then Nkind (P) = N_Parameter_Specification
4041 then
4042 null;
4044 else
4045 Set_Entity (N, Discriminal (E));
4046 end if;
4048 -- Otherwise, this is a discriminant in a context in which
4049 -- it is a reference to the corresponding parameter of the
4050 -- init proc for the enclosing type.
4052 else
4053 Set_Entity (N, Discriminal (E));
4054 end if;
4055 end Set_Entity_Or_Discriminal;
4057 -----------------------------------
4058 -- Check_In_Previous_With_Clause --
4059 -----------------------------------
4061 procedure Check_In_Previous_With_Clause
4062 (N : Node_Id;
4063 Nam : Entity_Id)
4065 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4066 Item : Node_Id;
4067 Par : Node_Id;
4069 begin
4070 Item := First (Context_Items (Parent (N)));
4071 while Present (Item) and then Item /= N loop
4072 if Nkind (Item) = N_With_Clause
4074 -- Protect the frontend against previous critical errors
4076 and then Nkind (Name (Item)) /= N_Selected_Component
4077 and then Entity (Name (Item)) = Pack
4078 then
4079 Par := Nam;
4081 -- Find root library unit in with_clause
4083 while Nkind (Par) = N_Expanded_Name loop
4084 Par := Prefix (Par);
4085 end loop;
4087 if Is_Child_Unit (Entity (Original_Node (Par))) then
4088 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4089 else
4090 return;
4091 end if;
4092 end if;
4094 Next (Item);
4095 end loop;
4097 -- On exit, package is not mentioned in a previous with_clause.
4098 -- Check if its prefix is.
4100 if Nkind (Nam) = N_Expanded_Name then
4101 Check_In_Previous_With_Clause (N, Prefix (Nam));
4103 elsif Pack /= Any_Id then
4104 Error_Msg_NE ("& is not visible", Nam, Pack);
4105 end if;
4106 end Check_In_Previous_With_Clause;
4108 ---------------------------------
4109 -- Check_Library_Unit_Renaming --
4110 ---------------------------------
4112 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4113 New_E : Entity_Id;
4115 begin
4116 if Nkind (Parent (N)) /= N_Compilation_Unit then
4117 return;
4119 -- Check for library unit. Note that we used to check for the scope
4120 -- being Standard here, but that was wrong for Standard itself.
4122 elsif not Is_Compilation_Unit (Old_E)
4123 and then not Is_Child_Unit (Old_E)
4124 then
4125 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4127 -- Entities defined in Standard (operators and boolean literals) cannot
4128 -- be renamed as library units.
4130 elsif Scope (Old_E) = Standard_Standard
4131 and then Sloc (Old_E) = Standard_Location
4132 then
4133 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4135 elsif Present (Parent_Spec (N))
4136 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
4137 and then not Is_Child_Unit (Old_E)
4138 then
4139 Error_Msg_N
4140 ("renamed unit must be a child unit of generic parent", Name (N));
4142 elsif Nkind (N) in N_Generic_Renaming_Declaration
4143 and then Nkind (Name (N)) = N_Expanded_Name
4144 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
4145 and then Is_Generic_Unit (Old_E)
4146 then
4147 Error_Msg_N
4148 ("renamed generic unit must be a library unit", Name (N));
4150 elsif Is_Package_Or_Generic_Package (Old_E) then
4152 -- Inherit categorization flags
4154 New_E := Defining_Entity (N);
4155 Set_Is_Pure (New_E, Is_Pure (Old_E));
4156 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
4157 Set_Is_Remote_Call_Interface (New_E,
4158 Is_Remote_Call_Interface (Old_E));
4159 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
4160 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
4161 end if;
4162 end Check_Library_Unit_Renaming;
4164 ------------------------
4165 -- Enclosing_Instance --
4166 ------------------------
4168 function Enclosing_Instance return Entity_Id is
4169 S : Entity_Id;
4171 begin
4172 if not Is_Generic_Instance (Current_Scope) then
4173 return Empty;
4174 end if;
4176 S := Scope (Current_Scope);
4177 while S /= Standard_Standard loop
4178 if Is_Generic_Instance (S) then
4179 return S;
4180 end if;
4182 S := Scope (S);
4183 end loop;
4185 return Empty;
4186 end Enclosing_Instance;
4188 ---------------
4189 -- End_Scope --
4190 ---------------
4192 procedure End_Scope is
4193 Id : Entity_Id;
4194 Prev : Entity_Id;
4195 Outer : Entity_Id;
4197 begin
4198 Id := First_Entity (Current_Scope);
4199 while Present (Id) loop
4200 -- An entity in the current scope is not necessarily the first one
4201 -- on its homonym chain. Find its predecessor if any,
4202 -- If it is an internal entity, it will not be in the visibility
4203 -- chain altogether, and there is nothing to unchain.
4205 if Id /= Current_Entity (Id) then
4206 Prev := Current_Entity (Id);
4207 while Present (Prev)
4208 and then Present (Homonym (Prev))
4209 and then Homonym (Prev) /= Id
4210 loop
4211 Prev := Homonym (Prev);
4212 end loop;
4214 -- Skip to end of loop if Id is not in the visibility chain
4216 if No (Prev) or else Homonym (Prev) /= Id then
4217 goto Next_Ent;
4218 end if;
4220 else
4221 Prev := Empty;
4222 end if;
4224 Set_Is_Immediately_Visible (Id, False);
4226 Outer := Homonym (Id);
4227 while Present (Outer) and then Scope (Outer) = Current_Scope loop
4228 Outer := Homonym (Outer);
4229 end loop;
4231 -- Reset homonym link of other entities, but do not modify link
4232 -- between entities in current scope, so that the back-end can have
4233 -- a proper count of local overloadings.
4235 if No (Prev) then
4236 Set_Name_Entity_Id (Chars (Id), Outer);
4238 elsif Scope (Prev) /= Scope (Id) then
4239 Set_Homonym (Prev, Outer);
4240 end if;
4242 <<Next_Ent>>
4243 Next_Entity (Id);
4244 end loop;
4246 -- If the scope generated freeze actions, place them before the
4247 -- current declaration and analyze them. Type declarations and
4248 -- the bodies of initialization procedures can generate such nodes.
4249 -- We follow the parent chain until we reach a list node, which is
4250 -- the enclosing list of declarations. If the list appears within
4251 -- a protected definition, move freeze nodes outside the protected
4252 -- type altogether.
4254 if Present
4255 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
4256 then
4257 declare
4258 Decl : Node_Id;
4259 L : constant List_Id := Scope_Stack.Table
4260 (Scope_Stack.Last).Pending_Freeze_Actions;
4262 begin
4263 if Is_Itype (Current_Scope) then
4264 Decl := Associated_Node_For_Itype (Current_Scope);
4265 else
4266 Decl := Parent (Current_Scope);
4267 end if;
4269 Pop_Scope;
4271 while not (Is_List_Member (Decl))
4272 or else Nkind_In (Parent (Decl), N_Protected_Definition,
4273 N_Task_Definition)
4274 loop
4275 Decl := Parent (Decl);
4276 end loop;
4278 Insert_List_Before_And_Analyze (Decl, L);
4279 end;
4281 else
4282 Pop_Scope;
4283 end if;
4284 end End_Scope;
4286 ---------------------
4287 -- End_Use_Clauses --
4288 ---------------------
4290 procedure End_Use_Clauses (Clause : Node_Id) is
4291 U : Node_Id;
4293 begin
4294 -- Remove Use_Type clauses first, because they affect the
4295 -- visibility of operators in subsequent used packages.
4297 U := Clause;
4298 while Present (U) loop
4299 if Nkind (U) = N_Use_Type_Clause then
4300 End_Use_Type (U);
4301 end if;
4303 Next_Use_Clause (U);
4304 end loop;
4306 U := Clause;
4307 while Present (U) loop
4308 if Nkind (U) = N_Use_Package_Clause then
4309 End_Use_Package (U);
4310 end if;
4312 Next_Use_Clause (U);
4313 end loop;
4314 end End_Use_Clauses;
4316 ---------------------
4317 -- End_Use_Package --
4318 ---------------------
4320 procedure End_Use_Package (N : Node_Id) is
4321 Pack_Name : Node_Id;
4322 Pack : Entity_Id;
4323 Id : Entity_Id;
4324 Elmt : Elmt_Id;
4326 function Is_Primitive_Operator_In_Use
4327 (Op : Entity_Id;
4328 F : Entity_Id) return Boolean;
4329 -- Check whether Op is a primitive operator of a use-visible type
4331 ----------------------------------
4332 -- Is_Primitive_Operator_In_Use --
4333 ----------------------------------
4335 function Is_Primitive_Operator_In_Use
4336 (Op : Entity_Id;
4337 F : Entity_Id) return Boolean
4339 T : constant Entity_Id := Base_Type (Etype (F));
4340 begin
4341 return In_Use (T) and then Scope (T) = Scope (Op);
4342 end Is_Primitive_Operator_In_Use;
4344 -- Start of processing for End_Use_Package
4346 begin
4347 Pack_Name := First (Names (N));
4348 while Present (Pack_Name) loop
4350 -- Test that Pack_Name actually denotes a package before processing
4352 if Is_Entity_Name (Pack_Name)
4353 and then Ekind (Entity (Pack_Name)) = E_Package
4354 then
4355 Pack := Entity (Pack_Name);
4357 if In_Open_Scopes (Pack) then
4358 null;
4360 elsif not Redundant_Use (Pack_Name) then
4361 Set_In_Use (Pack, False);
4362 Set_Current_Use_Clause (Pack, Empty);
4364 Id := First_Entity (Pack);
4365 while Present (Id) loop
4367 -- Preserve use-visibility of operators that are primitive
4368 -- operators of a type that is use-visible through an active
4369 -- use_type clause.
4371 if Nkind (Id) = N_Defining_Operator_Symbol
4372 and then
4373 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
4374 or else
4375 (Present (Next_Formal (First_Formal (Id)))
4376 and then
4377 Is_Primitive_Operator_In_Use
4378 (Id, Next_Formal (First_Formal (Id)))))
4379 then
4380 null;
4381 else
4382 Set_Is_Potentially_Use_Visible (Id, False);
4383 end if;
4385 if Is_Private_Type (Id)
4386 and then Present (Full_View (Id))
4387 then
4388 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4389 end if;
4391 Next_Entity (Id);
4392 end loop;
4394 if Present (Renamed_Object (Pack)) then
4395 Set_In_Use (Renamed_Object (Pack), False);
4396 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
4397 end if;
4399 if Chars (Pack) = Name_System
4400 and then Scope (Pack) = Standard_Standard
4401 and then Present_System_Aux
4402 then
4403 Id := First_Entity (System_Aux_Id);
4404 while Present (Id) loop
4405 Set_Is_Potentially_Use_Visible (Id, False);
4407 if Is_Private_Type (Id)
4408 and then Present (Full_View (Id))
4409 then
4410 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4411 end if;
4413 Next_Entity (Id);
4414 end loop;
4416 Set_In_Use (System_Aux_Id, False);
4417 end if;
4419 else
4420 Set_Redundant_Use (Pack_Name, False);
4421 end if;
4422 end if;
4424 Next (Pack_Name);
4425 end loop;
4427 if Present (Hidden_By_Use_Clause (N)) then
4428 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
4429 while Present (Elmt) loop
4430 declare
4431 E : constant Entity_Id := Node (Elmt);
4433 begin
4434 -- Reset either Use_Visibility or Direct_Visibility, depending
4435 -- on how the entity was hidden by the use clause.
4437 if In_Use (Scope (E))
4438 and then Used_As_Generic_Actual (Scope (E))
4439 then
4440 Set_Is_Potentially_Use_Visible (Node (Elmt));
4441 else
4442 Set_Is_Immediately_Visible (Node (Elmt));
4443 end if;
4445 Next_Elmt (Elmt);
4446 end;
4447 end loop;
4449 Set_Hidden_By_Use_Clause (N, No_Elist);
4450 end if;
4451 end End_Use_Package;
4453 ------------------
4454 -- End_Use_Type --
4455 ------------------
4457 procedure End_Use_Type (N : Node_Id) is
4458 Elmt : Elmt_Id;
4459 Id : Entity_Id;
4460 T : Entity_Id;
4462 -- Start of processing for End_Use_Type
4464 begin
4465 Id := First (Subtype_Marks (N));
4466 while Present (Id) loop
4468 -- A call to Rtsfind may occur while analyzing a use_type clause,
4469 -- in which case the type marks are not resolved yet, and there is
4470 -- nothing to remove.
4472 if not Is_Entity_Name (Id) or else No (Entity (Id)) then
4473 goto Continue;
4474 end if;
4476 T := Entity (Id);
4478 if T = Any_Type or else From_Limited_With (T) then
4479 null;
4481 -- Note that the use_type clause may mention a subtype of the type
4482 -- whose primitive operations have been made visible. Here as
4483 -- elsewhere, it is the base type that matters for visibility.
4485 elsif In_Open_Scopes (Scope (Base_Type (T))) then
4486 null;
4488 elsif not Redundant_Use (Id) then
4489 Set_In_Use (T, False);
4490 Set_In_Use (Base_Type (T), False);
4491 Set_Current_Use_Clause (T, Empty);
4492 Set_Current_Use_Clause (Base_Type (T), Empty);
4493 end if;
4495 <<Continue>>
4496 Next (Id);
4497 end loop;
4499 if Is_Empty_Elmt_List (Used_Operations (N)) then
4500 return;
4502 else
4503 Elmt := First_Elmt (Used_Operations (N));
4504 while Present (Elmt) loop
4505 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
4506 Next_Elmt (Elmt);
4507 end loop;
4508 end if;
4509 end End_Use_Type;
4511 ----------------------
4512 -- Find_Direct_Name --
4513 ----------------------
4515 procedure Find_Direct_Name (N : Node_Id) is
4516 E : Entity_Id;
4517 E2 : Entity_Id;
4518 Msg : Boolean;
4520 Inst : Entity_Id := Empty;
4521 -- Enclosing instance, if any
4523 Homonyms : Entity_Id;
4524 -- Saves start of homonym chain
4526 Nvis_Entity : Boolean;
4527 -- Set True to indicate that there is at least one entity on the homonym
4528 -- chain which, while not visible, is visible enough from the user point
4529 -- of view to warrant an error message of "not visible" rather than
4530 -- undefined.
4532 Nvis_Is_Private_Subprg : Boolean := False;
4533 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
4534 -- effect concerning library subprograms has been detected. Used to
4535 -- generate the precise error message.
4537 function From_Actual_Package (E : Entity_Id) return Boolean;
4538 -- Returns true if the entity is an actual for a package that is itself
4539 -- an actual for a formal package of the current instance. Such an
4540 -- entity requires special handling because it may be use-visible but
4541 -- hides directly visible entities defined outside the instance, because
4542 -- the corresponding formal did so in the generic.
4544 function Is_Actual_Parameter return Boolean;
4545 -- This function checks if the node N is an identifier that is an actual
4546 -- parameter of a procedure call. If so it returns True, otherwise it
4547 -- return False. The reason for this check is that at this stage we do
4548 -- not know what procedure is being called if the procedure might be
4549 -- overloaded, so it is premature to go setting referenced flags or
4550 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
4551 -- for that processing
4553 function Known_But_Invisible (E : Entity_Id) return Boolean;
4554 -- This function determines whether a reference to the entity E, which
4555 -- is not visible, can reasonably be considered to be known to the
4556 -- writer of the reference. This is a heuristic test, used only for
4557 -- the purposes of figuring out whether we prefer to complain that an
4558 -- entity is undefined or invisible (and identify the declaration of
4559 -- the invisible entity in the latter case). The point here is that we
4560 -- don't want to complain that something is invisible and then point to
4561 -- something entirely mysterious to the writer.
4563 procedure Nvis_Messages;
4564 -- Called if there are no visible entries for N, but there is at least
4565 -- one non-directly visible, or hidden declaration. This procedure
4566 -- outputs an appropriate set of error messages.
4568 procedure Undefined (Nvis : Boolean);
4569 -- This function is called if the current node has no corresponding
4570 -- visible entity or entities. The value set in Msg indicates whether
4571 -- an error message was generated (multiple error messages for the
4572 -- same variable are generally suppressed, see body for details).
4573 -- Msg is True if an error message was generated, False if not. This
4574 -- value is used by the caller to determine whether or not to output
4575 -- additional messages where appropriate. The parameter is set False
4576 -- to get the message "X is undefined", and True to get the message
4577 -- "X is not visible".
4579 -------------------------
4580 -- From_Actual_Package --
4581 -------------------------
4583 function From_Actual_Package (E : Entity_Id) return Boolean is
4584 Scop : constant Entity_Id := Scope (E);
4585 -- Declared scope of candidate entity
4587 Act : Entity_Id;
4589 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
4590 -- Recursive function that does the work and examines actuals of
4591 -- actual packages of current instance.
4593 ------------------------
4594 -- Declared_In_Actual --
4595 ------------------------
4597 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
4598 Act : Entity_Id;
4600 begin
4601 if No (Associated_Formal_Package (Pack)) then
4602 return False;
4604 else
4605 Act := First_Entity (Pack);
4606 while Present (Act) loop
4607 if Renamed_Object (Pack) = Scop then
4608 return True;
4610 -- Check for end of list of actuals.
4612 elsif Ekind (Act) = E_Package
4613 and then Renamed_Object (Act) = Pack
4614 then
4615 return False;
4617 elsif Ekind (Act) = E_Package
4618 and then Declared_In_Actual (Act)
4619 then
4620 return True;
4621 end if;
4623 Next_Entity (Act);
4624 end loop;
4626 return False;
4627 end if;
4628 end Declared_In_Actual;
4630 -- Start of processing for From_Actual_Package
4632 begin
4633 if not In_Instance then
4634 return False;
4636 else
4637 Inst := Current_Scope;
4638 while Present (Inst)
4639 and then Ekind (Inst) /= E_Package
4640 and then not Is_Generic_Instance (Inst)
4641 loop
4642 Inst := Scope (Inst);
4643 end loop;
4645 if No (Inst) then
4646 return False;
4647 end if;
4649 Act := First_Entity (Inst);
4650 while Present (Act) loop
4651 if Ekind (Act) = E_Package
4652 and then Declared_In_Actual (Act)
4653 then
4654 return True;
4655 end if;
4657 Next_Entity (Act);
4658 end loop;
4660 return False;
4661 end if;
4662 end From_Actual_Package;
4664 -------------------------
4665 -- Is_Actual_Parameter --
4666 -------------------------
4668 function Is_Actual_Parameter return Boolean is
4669 begin
4670 return
4671 Nkind (N) = N_Identifier
4672 and then
4673 (Nkind (Parent (N)) = N_Procedure_Call_Statement
4674 or else
4675 (Nkind (Parent (N)) = N_Parameter_Association
4676 and then N = Explicit_Actual_Parameter (Parent (N))
4677 and then Nkind (Parent (Parent (N))) =
4678 N_Procedure_Call_Statement));
4679 end Is_Actual_Parameter;
4681 -------------------------
4682 -- Known_But_Invisible --
4683 -------------------------
4685 function Known_But_Invisible (E : Entity_Id) return Boolean is
4686 Fname : File_Name_Type;
4688 begin
4689 -- Entities in Standard are always considered to be known
4691 if Sloc (E) <= Standard_Location then
4692 return True;
4694 -- An entity that does not come from source is always considered
4695 -- to be unknown, since it is an artifact of code expansion.
4697 elsif not Comes_From_Source (E) then
4698 return False;
4700 -- In gnat internal mode, we consider all entities known. The
4701 -- historical reason behind this discrepancy is not known??? But the
4702 -- only effect is to modify the error message given, so it is not
4703 -- critical. Since it only affects the exact wording of error
4704 -- messages in illegal programs, we do not mention this as an
4705 -- effect of -gnatg, since it is not a language modification.
4707 elsif GNAT_Mode then
4708 return True;
4709 end if;
4711 -- Here we have an entity that is not from package Standard, and
4712 -- which comes from Source. See if it comes from an internal file.
4714 Fname := Unit_File_Name (Get_Source_Unit (E));
4716 -- Case of from internal file
4718 if Is_Internal_File_Name (Fname) then
4720 -- Private part entities in internal files are never considered
4721 -- to be known to the writer of normal application code.
4723 if Is_Hidden (E) then
4724 return False;
4725 end if;
4727 -- Entities from System packages other than System and
4728 -- System.Storage_Elements are not considered to be known.
4729 -- System.Auxxxx files are also considered known to the user.
4731 -- Should refine this at some point to generally distinguish
4732 -- between known and unknown internal files ???
4734 Get_Name_String (Fname);
4736 return
4737 Name_Len < 2
4738 or else
4739 Name_Buffer (1 .. 2) /= "s-"
4740 or else
4741 Name_Buffer (3 .. 8) = "stoele"
4742 or else
4743 Name_Buffer (3 .. 5) = "aux";
4745 -- If not an internal file, then entity is definitely known,
4746 -- even if it is in a private part (the message generated will
4747 -- note that it is in a private part)
4749 else
4750 return True;
4751 end if;
4752 end Known_But_Invisible;
4754 -------------------
4755 -- Nvis_Messages --
4756 -------------------
4758 procedure Nvis_Messages is
4759 Comp_Unit : Node_Id;
4760 Ent : Entity_Id;
4761 Found : Boolean := False;
4762 Hidden : Boolean := False;
4763 Item : Node_Id;
4765 begin
4766 -- Ada 2005 (AI-262): Generate a precise error concerning the
4767 -- Beaujolais effect that was previously detected
4769 if Nvis_Is_Private_Subprg then
4771 pragma Assert (Nkind (E2) = N_Defining_Identifier
4772 and then Ekind (E2) = E_Function
4773 and then Scope (E2) = Standard_Standard
4774 and then Has_Private_With (E2));
4776 -- Find the sloc corresponding to the private with'ed unit
4778 Comp_Unit := Cunit (Current_Sem_Unit);
4779 Error_Msg_Sloc := No_Location;
4781 Item := First (Context_Items (Comp_Unit));
4782 while Present (Item) loop
4783 if Nkind (Item) = N_With_Clause
4784 and then Private_Present (Item)
4785 and then Entity (Name (Item)) = E2
4786 then
4787 Error_Msg_Sloc := Sloc (Item);
4788 exit;
4789 end if;
4791 Next (Item);
4792 end loop;
4794 pragma Assert (Error_Msg_Sloc /= No_Location);
4796 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
4797 return;
4798 end if;
4800 Undefined (Nvis => True);
4802 if Msg then
4804 -- First loop does hidden declarations
4806 Ent := Homonyms;
4807 while Present (Ent) loop
4808 if Is_Potentially_Use_Visible (Ent) then
4809 if not Hidden then
4810 Error_Msg_N -- CODEFIX
4811 ("multiple use clauses cause hiding!", N);
4812 Hidden := True;
4813 end if;
4815 Error_Msg_Sloc := Sloc (Ent);
4816 Error_Msg_N -- CODEFIX
4817 ("hidden declaration#!", N);
4818 end if;
4820 Ent := Homonym (Ent);
4821 end loop;
4823 -- If we found hidden declarations, then that's enough, don't
4824 -- bother looking for non-visible declarations as well.
4826 if Hidden then
4827 return;
4828 end if;
4830 -- Second loop does non-directly visible declarations
4832 Ent := Homonyms;
4833 while Present (Ent) loop
4834 if not Is_Potentially_Use_Visible (Ent) then
4836 -- Do not bother the user with unknown entities
4838 if not Known_But_Invisible (Ent) then
4839 goto Continue;
4840 end if;
4842 Error_Msg_Sloc := Sloc (Ent);
4844 -- Output message noting that there is a non-visible
4845 -- declaration, distinguishing the private part case.
4847 if Is_Hidden (Ent) then
4848 Error_Msg_N ("non-visible (private) declaration#!", N);
4850 -- If the entity is declared in a generic package, it
4851 -- cannot be visible, so there is no point in adding it
4852 -- to the list of candidates if another homograph from a
4853 -- non-generic package has been seen.
4855 elsif Ekind (Scope (Ent)) = E_Generic_Package
4856 and then Found
4857 then
4858 null;
4860 else
4861 Error_Msg_N -- CODEFIX
4862 ("non-visible declaration#!", N);
4864 if Ekind (Scope (Ent)) /= E_Generic_Package then
4865 Found := True;
4866 end if;
4868 if Is_Compilation_Unit (Ent)
4869 and then
4870 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
4871 then
4872 Error_Msg_Qual_Level := 99;
4873 Error_Msg_NE -- CODEFIX
4874 ("\\missing `WITH &;`", N, Ent);
4875 Error_Msg_Qual_Level := 0;
4876 end if;
4878 if Ekind (Ent) = E_Discriminant
4879 and then Present (Corresponding_Discriminant (Ent))
4880 and then Scope (Corresponding_Discriminant (Ent)) =
4881 Etype (Scope (Ent))
4882 then
4883 Error_Msg_N
4884 ("inherited discriminant not allowed here" &
4885 " (RM 3.8 (12), 3.8.1 (6))!", N);
4886 end if;
4887 end if;
4889 -- Set entity and its containing package as referenced. We
4890 -- can't be sure of this, but this seems a better choice
4891 -- to avoid unused entity messages.
4893 if Comes_From_Source (Ent) then
4894 Set_Referenced (Ent);
4895 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
4896 end if;
4897 end if;
4899 <<Continue>>
4900 Ent := Homonym (Ent);
4901 end loop;
4902 end if;
4903 end Nvis_Messages;
4905 ---------------
4906 -- Undefined --
4907 ---------------
4909 procedure Undefined (Nvis : Boolean) is
4910 Emsg : Error_Msg_Id;
4912 begin
4913 -- We should never find an undefined internal name. If we do, then
4914 -- see if we have previous errors. If so, ignore on the grounds that
4915 -- it is probably a cascaded message (e.g. a block label from a badly
4916 -- formed block). If no previous errors, then we have a real internal
4917 -- error of some kind so raise an exception.
4919 if Is_Internal_Name (Chars (N)) then
4920 if Total_Errors_Detected /= 0 then
4921 return;
4922 else
4923 raise Program_Error;
4924 end if;
4925 end if;
4927 -- A very specialized error check, if the undefined variable is
4928 -- a case tag, and the case type is an enumeration type, check
4929 -- for a possible misspelling, and if so, modify the identifier
4931 -- Named aggregate should also be handled similarly ???
4933 if Nkind (N) = N_Identifier
4934 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
4935 then
4936 declare
4937 Case_Stm : constant Node_Id := Parent (Parent (N));
4938 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
4940 Lit : Node_Id;
4942 begin
4943 if Is_Enumeration_Type (Case_Typ)
4944 and then not Is_Standard_Character_Type (Case_Typ)
4945 then
4946 Lit := First_Literal (Case_Typ);
4947 Get_Name_String (Chars (Lit));
4949 if Chars (Lit) /= Chars (N)
4950 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
4951 then
4952 Error_Msg_Node_2 := Lit;
4953 Error_Msg_N -- CODEFIX
4954 ("& is undefined, assume misspelling of &", N);
4955 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
4956 return;
4957 end if;
4959 Lit := Next_Literal (Lit);
4960 end if;
4961 end;
4962 end if;
4964 -- Normal processing
4966 Set_Entity (N, Any_Id);
4967 Set_Etype (N, Any_Type);
4969 -- We use the table Urefs to keep track of entities for which we
4970 -- have issued errors for undefined references. Multiple errors
4971 -- for a single name are normally suppressed, however we modify
4972 -- the error message to alert the programmer to this effect.
4974 for J in Urefs.First .. Urefs.Last loop
4975 if Chars (N) = Chars (Urefs.Table (J).Node) then
4976 if Urefs.Table (J).Err /= No_Error_Msg
4977 and then Sloc (N) /= Urefs.Table (J).Loc
4978 then
4979 Error_Msg_Node_1 := Urefs.Table (J).Node;
4981 if Urefs.Table (J).Nvis then
4982 Change_Error_Text (Urefs.Table (J).Err,
4983 "& is not visible (more references follow)");
4984 else
4985 Change_Error_Text (Urefs.Table (J).Err,
4986 "& is undefined (more references follow)");
4987 end if;
4989 Urefs.Table (J).Err := No_Error_Msg;
4990 end if;
4992 -- Although we will set Msg False, and thus suppress the
4993 -- message, we also set Error_Posted True, to avoid any
4994 -- cascaded messages resulting from the undefined reference.
4996 Msg := False;
4997 Set_Error_Posted (N, True);
4998 return;
4999 end if;
5000 end loop;
5002 -- If entry not found, this is first undefined occurrence
5004 if Nvis then
5005 Error_Msg_N ("& is not visible!", N);
5006 Emsg := Get_Msg_Id;
5008 else
5009 Error_Msg_N ("& is undefined!", N);
5010 Emsg := Get_Msg_Id;
5012 -- A very bizarre special check, if the undefined identifier
5013 -- is put or put_line, then add a special error message (since
5014 -- this is a very common error for beginners to make).
5016 if Nam_In (Chars (N), Name_Put, Name_Put_Line) then
5017 Error_Msg_N -- CODEFIX
5018 ("\\possible missing `WITH Ada.Text_'I'O; " &
5019 "USE Ada.Text_'I'O`!", N);
5021 -- Another special check if N is the prefix of a selected
5022 -- component which is a known unit, add message complaining
5023 -- about missing with for this unit.
5025 elsif Nkind (Parent (N)) = N_Selected_Component
5026 and then N = Prefix (Parent (N))
5027 and then Is_Known_Unit (Parent (N))
5028 then
5029 Error_Msg_Node_2 := Selector_Name (Parent (N));
5030 Error_Msg_N -- CODEFIX
5031 ("\\missing `WITH &.&;`", Prefix (Parent (N)));
5032 end if;
5034 -- Now check for possible misspellings
5036 declare
5037 E : Entity_Id;
5038 Ematch : Entity_Id := Empty;
5040 Last_Name_Id : constant Name_Id :=
5041 Name_Id (Nat (First_Name_Id) +
5042 Name_Entries_Count - 1);
5044 begin
5045 for Nam in First_Name_Id .. Last_Name_Id loop
5046 E := Get_Name_Entity_Id (Nam);
5048 if Present (E)
5049 and then (Is_Immediately_Visible (E)
5050 or else
5051 Is_Potentially_Use_Visible (E))
5052 then
5053 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5054 Ematch := E;
5055 exit;
5056 end if;
5057 end if;
5058 end loop;
5060 if Present (Ematch) then
5061 Error_Msg_NE -- CODEFIX
5062 ("\possible misspelling of&", N, Ematch);
5063 end if;
5064 end;
5065 end if;
5067 -- Make entry in undefined references table unless the full errors
5068 -- switch is set, in which case by refraining from generating the
5069 -- table entry, we guarantee that we get an error message for every
5070 -- undefined reference.
5072 if not All_Errors_Mode then
5073 Urefs.Append (
5074 (Node => N,
5075 Err => Emsg,
5076 Nvis => Nvis,
5077 Loc => Sloc (N)));
5078 end if;
5080 Msg := True;
5081 end Undefined;
5083 -- Start of processing for Find_Direct_Name
5085 begin
5086 -- If the entity pointer is already set, this is an internal node, or
5087 -- a node that is analyzed more than once, after a tree modification.
5088 -- In such a case there is no resolution to perform, just set the type.
5090 if Present (Entity (N)) then
5091 if Is_Type (Entity (N)) then
5092 Set_Etype (N, Entity (N));
5094 else
5095 declare
5096 Entyp : constant Entity_Id := Etype (Entity (N));
5098 begin
5099 -- One special case here. If the Etype field is already set,
5100 -- and references the packed array type corresponding to the
5101 -- etype of the referenced entity, then leave it alone. This
5102 -- happens for trees generated from Exp_Pakd, where expressions
5103 -- can be deliberately "mis-typed" to the packed array type.
5105 if Is_Array_Type (Entyp)
5106 and then Is_Packed (Entyp)
5107 and then Present (Etype (N))
5108 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5109 then
5110 null;
5112 -- If not that special case, then just reset the Etype
5114 else
5115 Set_Etype (N, Etype (Entity (N)));
5116 end if;
5117 end;
5118 end if;
5120 return;
5121 end if;
5123 -- Here if Entity pointer was not set, we need full visibility analysis
5124 -- First we generate debugging output if the debug E flag is set.
5126 if Debug_Flag_E then
5127 Write_Str ("Looking for ");
5128 Write_Name (Chars (N));
5129 Write_Eol;
5130 end if;
5132 Homonyms := Current_Entity (N);
5133 Nvis_Entity := False;
5135 E := Homonyms;
5136 while Present (E) loop
5138 -- If entity is immediately visible or potentially use visible, then
5139 -- process the entity and we are done.
5141 if Is_Immediately_Visible (E) then
5142 goto Immediately_Visible_Entity;
5144 elsif Is_Potentially_Use_Visible (E) then
5145 goto Potentially_Use_Visible_Entity;
5147 -- Note if a known but invisible entity encountered
5149 elsif Known_But_Invisible (E) then
5150 Nvis_Entity := True;
5151 end if;
5153 -- Move to next entity in chain and continue search
5155 E := Homonym (E);
5156 end loop;
5158 -- If no entries on homonym chain that were potentially visible,
5159 -- and no entities reasonably considered as non-visible, then
5160 -- we have a plain undefined reference, with no additional
5161 -- explanation required.
5163 if not Nvis_Entity then
5164 Undefined (Nvis => False);
5166 -- Otherwise there is at least one entry on the homonym chain that
5167 -- is reasonably considered as being known and non-visible.
5169 else
5170 Nvis_Messages;
5171 end if;
5173 return;
5175 -- Processing for a potentially use visible entry found. We must search
5176 -- the rest of the homonym chain for two reasons. First, if there is a
5177 -- directly visible entry, then none of the potentially use-visible
5178 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5179 -- for the case of multiple potentially use-visible entries hiding one
5180 -- another and as a result being non-directly visible (RM 8.4(11)).
5182 <<Potentially_Use_Visible_Entity>> declare
5183 Only_One_Visible : Boolean := True;
5184 All_Overloadable : Boolean := Is_Overloadable (E);
5186 begin
5187 E2 := Homonym (E);
5188 while Present (E2) loop
5189 if Is_Immediately_Visible (E2) then
5191 -- If the use-visible entity comes from the actual for a
5192 -- formal package, it hides a directly visible entity from
5193 -- outside the instance.
5195 if From_Actual_Package (E)
5196 and then Scope_Depth (E2) < Scope_Depth (Inst)
5197 then
5198 goto Found;
5199 else
5200 E := E2;
5201 goto Immediately_Visible_Entity;
5202 end if;
5204 elsif Is_Potentially_Use_Visible (E2) then
5205 Only_One_Visible := False;
5206 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5208 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5209 -- that can occur in private_with clauses. Example:
5211 -- with A;
5212 -- private with B; package A is
5213 -- package C is function B return Integer;
5214 -- use A; end A;
5215 -- V1 : Integer := B;
5216 -- private function B return Integer;
5217 -- V2 : Integer := B;
5218 -- end C;
5220 -- V1 resolves to A.B, but V2 resolves to library unit B
5222 elsif Ekind (E2) = E_Function
5223 and then Scope (E2) = Standard_Standard
5224 and then Has_Private_With (E2)
5225 then
5226 Only_One_Visible := False;
5227 All_Overloadable := False;
5228 Nvis_Is_Private_Subprg := True;
5229 exit;
5230 end if;
5232 E2 := Homonym (E2);
5233 end loop;
5235 -- On falling through this loop, we have checked that there are no
5236 -- immediately visible entities. Only_One_Visible is set if exactly
5237 -- one potentially use visible entity exists. All_Overloadable is
5238 -- set if all the potentially use visible entities are overloadable.
5239 -- The condition for legality is that either there is one potentially
5240 -- use visible entity, or if there is more than one, then all of them
5241 -- are overloadable.
5243 if Only_One_Visible or All_Overloadable then
5244 goto Found;
5246 -- If there is more than one potentially use-visible entity and at
5247 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5248 -- Note that E points to the first such entity on the homonym list.
5249 -- Special case: if one of the entities is declared in an actual
5250 -- package, it was visible in the generic, and takes precedence over
5251 -- other entities that are potentially use-visible. Same if it is
5252 -- declared in a local instantiation of the current instance.
5254 else
5255 if In_Instance then
5257 -- Find current instance
5259 Inst := Current_Scope;
5260 while Present (Inst) and then Inst /= Standard_Standard loop
5261 if Is_Generic_Instance (Inst) then
5262 exit;
5263 end if;
5265 Inst := Scope (Inst);
5266 end loop;
5268 E2 := E;
5269 while Present (E2) loop
5270 if From_Actual_Package (E2)
5271 or else
5272 (Is_Generic_Instance (Scope (E2))
5273 and then Scope_Depth (Scope (E2)) > Scope_Depth (Inst))
5274 then
5275 E := E2;
5276 goto Found;
5277 end if;
5279 E2 := Homonym (E2);
5280 end loop;
5282 Nvis_Messages;
5283 return;
5285 elsif
5286 Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
5287 then
5288 -- A use-clause in the body of a system file creates conflict
5289 -- with some entity in a user scope, while rtsfind is active.
5290 -- Keep only the entity coming from another predefined unit.
5292 E2 := E;
5293 while Present (E2) loop
5294 if Is_Predefined_File_Name
5295 (Unit_File_Name (Get_Source_Unit (Sloc (E2))))
5296 then
5297 E := E2;
5298 goto Found;
5299 end if;
5301 E2 := Homonym (E2);
5302 end loop;
5304 -- Entity must exist because predefined unit is correct
5306 raise Program_Error;
5308 else
5309 Nvis_Messages;
5310 return;
5311 end if;
5312 end if;
5313 end;
5315 -- Come here with E set to the first immediately visible entity on
5316 -- the homonym chain. This is the one we want unless there is another
5317 -- immediately visible entity further on in the chain for an inner
5318 -- scope (RM 8.3(8)).
5320 <<Immediately_Visible_Entity>> declare
5321 Level : Int;
5322 Scop : Entity_Id;
5324 begin
5325 -- Find scope level of initial entity. When compiling through
5326 -- Rtsfind, the previous context is not completely invisible, and
5327 -- an outer entity may appear on the chain, whose scope is below
5328 -- the entry for Standard that delimits the current scope stack.
5329 -- Indicate that the level for this spurious entry is outside of
5330 -- the current scope stack.
5332 Level := Scope_Stack.Last;
5333 loop
5334 Scop := Scope_Stack.Table (Level).Entity;
5335 exit when Scop = Scope (E);
5336 Level := Level - 1;
5337 exit when Scop = Standard_Standard;
5338 end loop;
5340 -- Now search remainder of homonym chain for more inner entry
5341 -- If the entity is Standard itself, it has no scope, and we
5342 -- compare it with the stack entry directly.
5344 E2 := Homonym (E);
5345 while Present (E2) loop
5346 if Is_Immediately_Visible (E2) then
5348 -- If a generic package contains a local declaration that
5349 -- has the same name as the generic, there may be a visibility
5350 -- conflict in an instance, where the local declaration must
5351 -- also hide the name of the corresponding package renaming.
5352 -- We check explicitly for a package declared by a renaming,
5353 -- whose renamed entity is an instance that is on the scope
5354 -- stack, and that contains a homonym in the same scope. Once
5355 -- we have found it, we know that the package renaming is not
5356 -- immediately visible, and that the identifier denotes the
5357 -- other entity (and its homonyms if overloaded).
5359 if Scope (E) = Scope (E2)
5360 and then Ekind (E) = E_Package
5361 and then Present (Renamed_Object (E))
5362 and then Is_Generic_Instance (Renamed_Object (E))
5363 and then In_Open_Scopes (Renamed_Object (E))
5364 and then Comes_From_Source (N)
5365 then
5366 Set_Is_Immediately_Visible (E, False);
5367 E := E2;
5369 else
5370 for J in Level + 1 .. Scope_Stack.Last loop
5371 if Scope_Stack.Table (J).Entity = Scope (E2)
5372 or else Scope_Stack.Table (J).Entity = E2
5373 then
5374 Level := J;
5375 E := E2;
5376 exit;
5377 end if;
5378 end loop;
5379 end if;
5380 end if;
5382 E2 := Homonym (E2);
5383 end loop;
5385 -- At the end of that loop, E is the innermost immediately
5386 -- visible entity, so we are all set.
5387 end;
5389 -- Come here with entity found, and stored in E
5391 <<Found>> begin
5393 -- Check violation of No_Wide_Characters restriction
5395 Check_Wide_Character_Restriction (E, N);
5397 -- When distribution features are available (Get_PCS_Name /=
5398 -- Name_No_DSA), a remote access-to-subprogram type is converted
5399 -- into a record type holding whatever information is needed to
5400 -- perform a remote call on an RCI subprogram. In that case we
5401 -- rewrite any occurrence of the RAS type into the equivalent record
5402 -- type here. 'Access attribute references and RAS dereferences are
5403 -- then implemented using specific TSSs. However when distribution is
5404 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
5405 -- generation of these TSSs, and we must keep the RAS type in its
5406 -- original access-to-subprogram form (since all calls through a
5407 -- value of such type will be local anyway in the absence of a PCS).
5409 if Comes_From_Source (N)
5410 and then Is_Remote_Access_To_Subprogram_Type (E)
5411 and then Ekind (E) = E_Access_Subprogram_Type
5412 and then Expander_Active
5413 and then Get_PCS_Name /= Name_No_DSA
5414 then
5415 Rewrite (N,
5416 New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
5417 return;
5418 end if;
5420 -- Set the entity. Note that the reason we call Set_Entity for the
5421 -- overloadable case, as opposed to Set_Entity_With_Checks is
5422 -- that in the overloaded case, the initial call can set the wrong
5423 -- homonym. The call that sets the right homonym is in Sem_Res and
5424 -- that call does use Set_Entity_With_Checks, so we don't miss
5425 -- a style check.
5427 if Is_Overloadable (E) then
5428 Set_Entity (N, E);
5429 else
5430 Set_Entity_With_Checks (N, E);
5431 end if;
5433 if Is_Type (E) then
5434 Set_Etype (N, E);
5435 else
5436 Set_Etype (N, Get_Full_View (Etype (E)));
5437 end if;
5439 if Debug_Flag_E then
5440 Write_Str (" found ");
5441 Write_Entity_Info (E, " ");
5442 end if;
5444 -- If the Ekind of the entity is Void, it means that all homonyms
5445 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
5446 -- test is skipped if the current scope is a record and the name is
5447 -- a pragma argument expression (case of Atomic and Volatile pragmas
5448 -- and possibly other similar pragmas added later, which are allowed
5449 -- to reference components in the current record).
5451 if Ekind (E) = E_Void
5452 and then
5453 (not Is_Record_Type (Current_Scope)
5454 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
5455 then
5456 Premature_Usage (N);
5458 -- If the entity is overloadable, collect all interpretations of the
5459 -- name for subsequent overload resolution. We optimize a bit here to
5460 -- do this only if we have an overloadable entity that is not on its
5461 -- own on the homonym chain.
5463 elsif Is_Overloadable (E)
5464 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
5465 then
5466 Collect_Interps (N);
5468 -- If no homonyms were visible, the entity is unambiguous
5470 if not Is_Overloaded (N) then
5471 if not Is_Actual_Parameter then
5472 Generate_Reference (E, N);
5473 end if;
5474 end if;
5476 -- Case of non-overloadable entity, set the entity providing that
5477 -- we do not have the case of a discriminant reference within a
5478 -- default expression. Such references are replaced with the
5479 -- corresponding discriminal, which is the formal corresponding to
5480 -- to the discriminant in the initialization procedure.
5482 else
5483 -- Entity is unambiguous, indicate that it is referenced here
5485 -- For a renaming of an object, always generate simple reference,
5486 -- we don't try to keep track of assignments in this case, except
5487 -- in SPARK mode where renamings are traversed for generating
5488 -- local effects of subprograms.
5490 if Is_Object (E)
5491 and then Present (Renamed_Object (E))
5492 and then not GNATprove_Mode
5493 then
5494 Generate_Reference (E, N);
5496 -- If the renamed entity is a private protected component,
5497 -- reference the original component as well. This needs to be
5498 -- done because the private renamings are installed before any
5499 -- analysis has occurred. Reference to a private component will
5500 -- resolve to the renaming and the original component will be
5501 -- left unreferenced, hence the following.
5503 if Is_Prival (E) then
5504 Generate_Reference (Prival_Link (E), N);
5505 end if;
5507 -- One odd case is that we do not want to set the Referenced flag
5508 -- if the entity is a label, and the identifier is the label in
5509 -- the source, since this is not a reference from the point of
5510 -- view of the user.
5512 elsif Nkind (Parent (N)) = N_Label then
5513 declare
5514 R : constant Boolean := Referenced (E);
5516 begin
5517 -- Generate reference unless this is an actual parameter
5518 -- (see comment below)
5520 if Is_Actual_Parameter then
5521 Generate_Reference (E, N);
5522 Set_Referenced (E, R);
5523 end if;
5524 end;
5526 -- Normal case, not a label: generate reference
5528 else
5529 if not Is_Actual_Parameter then
5531 -- Package or generic package is always a simple reference
5533 if Ekind_In (E, E_Package, E_Generic_Package) then
5534 Generate_Reference (E, N, 'r');
5536 -- Else see if we have a left hand side
5538 else
5539 case Is_LHS (N) is
5540 when Yes =>
5541 Generate_Reference (E, N, 'm');
5543 when No =>
5544 Generate_Reference (E, N, 'r');
5546 -- If we don't know now, generate reference later
5548 when Unknown =>
5549 Deferred_References.Append ((E, N));
5550 end case;
5551 end if;
5552 end if;
5554 Check_Nested_Access (E);
5555 end if;
5557 Set_Entity_Or_Discriminal (N, E);
5559 -- The name may designate a generalized reference, in which case
5560 -- the dereference interpretation will be included.
5562 if Ada_Version >= Ada_2012
5563 and then
5564 (Nkind (Parent (N)) in N_Subexpr
5565 or else Nkind_In (Parent (N), N_Object_Declaration,
5566 N_Assignment_Statement))
5567 then
5568 Check_Implicit_Dereference (N, Etype (E));
5569 end if;
5570 end if;
5571 end;
5572 end Find_Direct_Name;
5574 ------------------------
5575 -- Find_Expanded_Name --
5576 ------------------------
5578 -- This routine searches the homonym chain of the entity until it finds
5579 -- an entity declared in the scope denoted by the prefix. If the entity
5580 -- is private, it may nevertheless be immediately visible, if we are in
5581 -- the scope of its declaration.
5583 procedure Find_Expanded_Name (N : Node_Id) is
5584 function In_Pragmas_Depends_Or_Global (N : Node_Id) return Boolean;
5585 -- Determine whether an arbitrary node N appears in pragmas [Refined_]
5586 -- Depends or [Refined_]Global.
5588 ----------------------------------
5589 -- In_Pragmas_Depends_Or_Global --
5590 ----------------------------------
5592 function In_Pragmas_Depends_Or_Global (N : Node_Id) return Boolean is
5593 Par : Node_Id;
5595 begin
5596 -- Climb the parent chain looking for a pragma
5598 Par := N;
5599 while Present (Par) loop
5600 if Nkind (Par) = N_Pragma
5601 and then Nam_In (Pragma_Name (Par), Name_Depends,
5602 Name_Global,
5603 Name_Refined_Depends,
5604 Name_Refined_Global)
5605 then
5606 return True;
5608 -- Prevent the search from going too far
5610 elsif Is_Body_Or_Package_Declaration (Par) then
5611 return False;
5612 end if;
5614 Par := Parent (Par);
5615 end loop;
5617 return False;
5618 end In_Pragmas_Depends_Or_Global;
5620 -- Local variables
5622 Selector : constant Node_Id := Selector_Name (N);
5623 Candidate : Entity_Id := Empty;
5624 P_Name : Entity_Id;
5625 Id : Entity_Id;
5627 -- Start of processing for Find_Expanded_Name
5629 begin
5630 P_Name := Entity (Prefix (N));
5632 -- If the prefix is a renamed package, look for the entity in the
5633 -- original package.
5635 if Ekind (P_Name) = E_Package
5636 and then Present (Renamed_Object (P_Name))
5637 then
5638 P_Name := Renamed_Object (P_Name);
5640 -- Rewrite node with entity field pointing to renamed object
5642 Rewrite (Prefix (N), New_Copy (Prefix (N)));
5643 Set_Entity (Prefix (N), P_Name);
5645 -- If the prefix is an object of a concurrent type, look for
5646 -- the entity in the associated task or protected type.
5648 elsif Is_Concurrent_Type (Etype (P_Name)) then
5649 P_Name := Etype (P_Name);
5650 end if;
5652 Id := Current_Entity (Selector);
5654 declare
5655 Is_New_Candidate : Boolean;
5657 begin
5658 while Present (Id) loop
5659 if Scope (Id) = P_Name then
5660 Candidate := Id;
5661 Is_New_Candidate := True;
5663 -- Handle abstract views of states and variables. These are
5664 -- acceptable only when the reference to the view appears in
5665 -- pragmas [Refined_]Depends and [Refined_]Global.
5667 if Ekind (Id) = E_Abstract_State
5668 and then From_Limited_With (Id)
5669 and then Present (Non_Limited_View (Id))
5670 then
5671 if In_Pragmas_Depends_Or_Global (N) then
5672 Candidate := Non_Limited_View (Id);
5673 Is_New_Candidate := True;
5675 -- Hide candidate because it is not used in a proper context
5677 else
5678 Candidate := Empty;
5679 Is_New_Candidate := False;
5680 end if;
5681 end if;
5683 -- Ada 2005 (AI-217): Handle shadow entities associated with types
5684 -- declared in limited-withed nested packages. We don't need to
5685 -- handle E_Incomplete_Subtype entities because the entities in
5686 -- the limited view are always E_Incomplete_Type entities (see
5687 -- Build_Limited_Views). Regarding the expression used to evaluate
5688 -- the scope, it is important to note that the limited view also
5689 -- has shadow entities associated nested packages. For this reason
5690 -- the correct scope of the entity is the scope of the real entity
5691 -- The non-limited view may itself be incomplete, in which case
5692 -- get the full view if available.
5694 elsif Ekind (Id) = E_Incomplete_Type
5695 and then From_Limited_With (Id)
5696 and then Present (Non_Limited_View (Id))
5697 and then Scope (Non_Limited_View (Id)) = P_Name
5698 then
5699 Candidate := Get_Full_View (Non_Limited_View (Id));
5700 Is_New_Candidate := True;
5702 else
5703 Is_New_Candidate := False;
5704 end if;
5706 if Is_New_Candidate then
5707 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
5708 exit when Is_Visible_Lib_Unit (Id);
5709 else
5710 exit when not Is_Hidden (Id);
5711 end if;
5713 exit when Is_Immediately_Visible (Id);
5714 end if;
5716 Id := Homonym (Id);
5717 end loop;
5718 end;
5720 if No (Id)
5721 and then Ekind_In (P_Name, E_Procedure, E_Function)
5722 and then Is_Generic_Instance (P_Name)
5723 then
5724 -- Expanded name denotes entity in (instance of) generic subprogram.
5725 -- The entity may be in the subprogram instance, or may denote one of
5726 -- the formals, which is declared in the enclosing wrapper package.
5728 P_Name := Scope (P_Name);
5730 Id := Current_Entity (Selector);
5731 while Present (Id) loop
5732 exit when Scope (Id) = P_Name;
5733 Id := Homonym (Id);
5734 end loop;
5735 end if;
5737 if No (Id) or else Chars (Id) /= Chars (Selector) then
5738 Set_Etype (N, Any_Type);
5740 -- If we are looking for an entity defined in System, try to find it
5741 -- in the child package that may have been provided as an extension
5742 -- to System. The Extend_System pragma will have supplied the name of
5743 -- the extension, which may have to be loaded.
5745 if Chars (P_Name) = Name_System
5746 and then Scope (P_Name) = Standard_Standard
5747 and then Present (System_Extend_Unit)
5748 and then Present_System_Aux (N)
5749 then
5750 Set_Entity (Prefix (N), System_Aux_Id);
5751 Find_Expanded_Name (N);
5752 return;
5754 elsif Nkind (Selector) = N_Operator_Symbol
5755 and then Has_Implicit_Operator (N)
5756 then
5757 -- There is an implicit instance of the predefined operator in
5758 -- the given scope. The operator entity is defined in Standard.
5759 -- Has_Implicit_Operator makes the node into an Expanded_Name.
5761 return;
5763 elsif Nkind (Selector) = N_Character_Literal
5764 and then Has_Implicit_Character_Literal (N)
5765 then
5766 -- If there is no literal defined in the scope denoted by the
5767 -- prefix, the literal may belong to (a type derived from)
5768 -- Standard_Character, for which we have no explicit literals.
5770 return;
5772 else
5773 -- If the prefix is a single concurrent object, use its name in
5774 -- the error message, rather than that of the anonymous type.
5776 if Is_Concurrent_Type (P_Name)
5777 and then Is_Internal_Name (Chars (P_Name))
5778 then
5779 Error_Msg_Node_2 := Entity (Prefix (N));
5780 else
5781 Error_Msg_Node_2 := P_Name;
5782 end if;
5784 if P_Name = System_Aux_Id then
5785 P_Name := Scope (P_Name);
5786 Set_Entity (Prefix (N), P_Name);
5787 end if;
5789 if Present (Candidate) then
5791 -- If we know that the unit is a child unit we can give a more
5792 -- accurate error message.
5794 if Is_Child_Unit (Candidate) then
5796 -- If the candidate is a private child unit and we are in
5797 -- the visible part of a public unit, specialize the error
5798 -- message. There might be a private with_clause for it,
5799 -- but it is not currently active.
5801 if Is_Private_Descendant (Candidate)
5802 and then Ekind (Current_Scope) = E_Package
5803 and then not In_Private_Part (Current_Scope)
5804 and then not Is_Private_Descendant (Current_Scope)
5805 then
5806 Error_Msg_N ("private child unit& is not visible here",
5807 Selector);
5809 -- Normal case where we have a missing with for a child unit
5811 else
5812 Error_Msg_Qual_Level := 99;
5813 Error_Msg_NE -- CODEFIX
5814 ("missing `WITH &;`", Selector, Candidate);
5815 Error_Msg_Qual_Level := 0;
5816 end if;
5818 -- Here we don't know that this is a child unit
5820 else
5821 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
5822 end if;
5824 else
5825 -- Within the instantiation of a child unit, the prefix may
5826 -- denote the parent instance, but the selector has the name
5827 -- of the original child. That is to say, when A.B appears
5828 -- within an instantiation of generic child unit B, the scope
5829 -- stack includes an instance of A (P_Name) and an instance
5830 -- of B under some other name. We scan the scope to find this
5831 -- child instance, which is the desired entity.
5832 -- Note that the parent may itself be a child instance, if
5833 -- the reference is of the form A.B.C, in which case A.B has
5834 -- already been rewritten with the proper entity.
5836 if In_Open_Scopes (P_Name)
5837 and then Is_Generic_Instance (P_Name)
5838 then
5839 declare
5840 Gen_Par : constant Entity_Id :=
5841 Generic_Parent (Specification
5842 (Unit_Declaration_Node (P_Name)));
5843 S : Entity_Id := Current_Scope;
5844 P : Entity_Id;
5846 begin
5847 for J in reverse 0 .. Scope_Stack.Last loop
5848 S := Scope_Stack.Table (J).Entity;
5850 exit when S = Standard_Standard;
5852 if Ekind_In (S, E_Function,
5853 E_Package,
5854 E_Procedure)
5855 then
5856 P := Generic_Parent (Specification
5857 (Unit_Declaration_Node (S)));
5859 -- Check that P is a generic child of the generic
5860 -- parent of the prefix.
5862 if Present (P)
5863 and then Chars (P) = Chars (Selector)
5864 and then Scope (P) = Gen_Par
5865 then
5866 Id := S;
5867 goto Found;
5868 end if;
5869 end if;
5871 end loop;
5872 end;
5873 end if;
5875 -- If this is a selection from Ada, System or Interfaces, then
5876 -- we assume a missing with for the corresponding package.
5878 if Is_Known_Unit (N) then
5879 if not Error_Posted (N) then
5880 Error_Msg_Node_2 := Selector;
5881 Error_Msg_N -- CODEFIX
5882 ("missing `WITH &.&;`", Prefix (N));
5883 end if;
5885 -- If this is a selection from a dummy package, then suppress
5886 -- the error message, of course the entity is missing if the
5887 -- package is missing.
5889 elsif Sloc (Error_Msg_Node_2) = No_Location then
5890 null;
5892 -- Here we have the case of an undefined component
5894 else
5896 -- The prefix may hide a homonym in the context that
5897 -- declares the desired entity. This error can use a
5898 -- specialized message.
5900 if In_Open_Scopes (P_Name) then
5901 declare
5902 H : constant Entity_Id := Homonym (P_Name);
5904 begin
5905 if Present (H)
5906 and then Is_Compilation_Unit (H)
5907 and then
5908 (Is_Immediately_Visible (H)
5909 or else Is_Visible_Lib_Unit (H))
5910 then
5911 Id := First_Entity (H);
5912 while Present (Id) loop
5913 if Chars (Id) = Chars (Selector) then
5914 Error_Msg_Qual_Level := 99;
5915 Error_Msg_Name_1 := Chars (Selector);
5916 Error_Msg_NE
5917 ("% not declared in&", N, P_Name);
5918 Error_Msg_NE
5919 ("\use fully qualified name starting with "
5920 & "Standard to make& visible", N, H);
5921 Error_Msg_Qual_Level := 0;
5922 goto Done;
5923 end if;
5925 Next_Entity (Id);
5926 end loop;
5927 end if;
5929 -- If not found, standard error message
5931 Error_Msg_NE ("& not declared in&", N, Selector);
5933 <<Done>> null;
5934 end;
5936 else
5937 Error_Msg_NE ("& not declared in&", N, Selector);
5938 end if;
5940 -- Check for misspelling of some entity in prefix
5942 Id := First_Entity (P_Name);
5943 while Present (Id) loop
5944 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
5945 and then not Is_Internal_Name (Chars (Id))
5946 then
5947 Error_Msg_NE -- CODEFIX
5948 ("possible misspelling of&", Selector, Id);
5949 exit;
5950 end if;
5952 Next_Entity (Id);
5953 end loop;
5955 -- Specialize the message if this may be an instantiation
5956 -- of a child unit that was not mentioned in the context.
5958 if Nkind (Parent (N)) = N_Package_Instantiation
5959 and then Is_Generic_Instance (Entity (Prefix (N)))
5960 and then Is_Compilation_Unit
5961 (Generic_Parent (Parent (Entity (Prefix (N)))))
5962 then
5963 Error_Msg_Node_2 := Selector;
5964 Error_Msg_N -- CODEFIX
5965 ("\missing `WITH &.&;`", Prefix (N));
5966 end if;
5967 end if;
5968 end if;
5970 Id := Any_Id;
5971 end if;
5972 end if;
5974 <<Found>>
5975 if Comes_From_Source (N)
5976 and then Is_Remote_Access_To_Subprogram_Type (Id)
5977 and then Ekind (Id) = E_Access_Subprogram_Type
5978 and then Present (Equivalent_Type (Id))
5979 then
5980 -- If we are not actually generating distribution code (i.e. the
5981 -- current PCS is the dummy non-distributed version), then the
5982 -- Equivalent_Type will be missing, and Id should be treated as
5983 -- a regular access-to-subprogram type.
5985 Id := Equivalent_Type (Id);
5986 Set_Chars (Selector, Chars (Id));
5987 end if;
5989 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
5991 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
5992 if From_Limited_With (Id)
5993 or else Is_Type (Id)
5994 or else Ekind (Id) = E_Package
5995 then
5996 null;
5997 else
5998 Error_Msg_N
5999 ("limited withed package can only be used to access "
6000 & "incomplete types", N);
6001 end if;
6002 end if;
6004 if Is_Task_Type (P_Name)
6005 and then ((Ekind (Id) = E_Entry
6006 and then Nkind (Parent (N)) /= N_Attribute_Reference)
6007 or else
6008 (Ekind (Id) = E_Entry_Family
6009 and then
6010 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
6011 then
6012 -- If both the task type and the entry are in scope, this may still
6013 -- be the expanded name of an entry formal.
6015 if In_Open_Scopes (Id)
6016 and then Nkind (Parent (N)) = N_Selected_Component
6017 then
6018 null;
6020 else
6021 -- It is an entry call after all, either to the current task
6022 -- (which will deadlock) or to an enclosing task.
6024 Analyze_Selected_Component (N);
6025 return;
6026 end if;
6027 end if;
6029 Change_Selected_Component_To_Expanded_Name (N);
6031 -- Set appropriate type
6033 if Is_Type (Id) then
6034 Set_Etype (N, Id);
6035 else
6036 Set_Etype (N, Get_Full_View (Etype (Id)));
6037 end if;
6039 -- Do style check and generate reference, but skip both steps if this
6040 -- entity has homonyms, since we may not have the right homonym set yet.
6041 -- The proper homonym will be set during the resolve phase.
6043 if Has_Homonym (Id) then
6044 Set_Entity (N, Id);
6046 else
6047 Set_Entity_Or_Discriminal (N, Id);
6049 case Is_LHS (N) is
6050 when Yes =>
6051 Generate_Reference (Id, N, 'm');
6052 when No =>
6053 Generate_Reference (Id, N, 'r');
6054 when Unknown =>
6055 Deferred_References.Append ((Id, N));
6056 end case;
6057 end if;
6059 -- Check for violation of No_Wide_Characters
6061 Check_Wide_Character_Restriction (Id, N);
6063 -- If the Ekind of the entity is Void, it means that all homonyms are
6064 -- hidden from all visibility (RM 8.3(5,14-20)).
6066 if Ekind (Id) = E_Void then
6067 Premature_Usage (N);
6069 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6070 declare
6071 H : Entity_Id := Homonym (Id);
6073 begin
6074 while Present (H) loop
6075 if Scope (H) = Scope (Id)
6076 and then (not Is_Hidden (H)
6077 or else Is_Immediately_Visible (H))
6078 then
6079 Collect_Interps (N);
6080 exit;
6081 end if;
6083 H := Homonym (H);
6084 end loop;
6086 -- If an extension of System is present, collect possible explicit
6087 -- overloadings declared in the extension.
6089 if Chars (P_Name) = Name_System
6090 and then Scope (P_Name) = Standard_Standard
6091 and then Present (System_Extend_Unit)
6092 and then Present_System_Aux (N)
6093 then
6094 H := Current_Entity (Id);
6096 while Present (H) loop
6097 if Scope (H) = System_Aux_Id then
6098 Add_One_Interp (N, H, Etype (H));
6099 end if;
6101 H := Homonym (H);
6102 end loop;
6103 end if;
6104 end;
6105 end if;
6107 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6108 and then Scope (Id) /= Standard_Standard
6109 then
6110 -- In addition to user-defined operators in the given scope, there
6111 -- may be an implicit instance of the predefined operator. The
6112 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6113 -- and added to the interpretations. Procedure Add_One_Interp will
6114 -- determine which hides which.
6116 if Has_Implicit_Operator (N) then
6117 null;
6118 end if;
6119 end if;
6121 -- If there is a single interpretation for N we can generate a
6122 -- reference to the unique entity found.
6124 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6125 Generate_Reference (Id, N);
6126 end if;
6127 end Find_Expanded_Name;
6129 -------------------------
6130 -- Find_Renamed_Entity --
6131 -------------------------
6133 function Find_Renamed_Entity
6134 (N : Node_Id;
6135 Nam : Node_Id;
6136 New_S : Entity_Id;
6137 Is_Actual : Boolean := False) return Entity_Id
6139 Ind : Interp_Index;
6140 I1 : Interp_Index := 0; -- Suppress junk warnings
6141 It : Interp;
6142 It1 : Interp;
6143 Old_S : Entity_Id;
6144 Inst : Entity_Id;
6146 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
6147 -- If the renamed entity is an implicit operator, check whether it is
6148 -- visible because its operand type is properly visible. This check
6149 -- applies to explicit renamed entities that appear in the source in a
6150 -- renaming declaration or a formal subprogram instance, but not to
6151 -- default generic actuals with a name.
6153 function Report_Overload return Entity_Id;
6154 -- List possible interpretations, and specialize message in the
6155 -- case of a generic actual.
6157 function Within (Inner, Outer : Entity_Id) return Boolean;
6158 -- Determine whether a candidate subprogram is defined within the
6159 -- enclosing instance. If yes, it has precedence over outer candidates.
6161 --------------------------
6162 -- Is_Visible_Operation --
6163 --------------------------
6165 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
6166 Scop : Entity_Id;
6167 Typ : Entity_Id;
6168 Btyp : Entity_Id;
6170 begin
6171 if Ekind (Op) /= E_Operator
6172 or else Scope (Op) /= Standard_Standard
6173 or else (In_Instance
6174 and then (not Is_Actual
6175 or else Present (Enclosing_Instance)))
6176 then
6177 return True;
6179 else
6180 -- For a fixed point type operator, check the resulting type,
6181 -- because it may be a mixed mode integer * fixed operation.
6183 if Present (Next_Formal (First_Formal (New_S)))
6184 and then Is_Fixed_Point_Type (Etype (New_S))
6185 then
6186 Typ := Etype (New_S);
6187 else
6188 Typ := Etype (First_Formal (New_S));
6189 end if;
6191 Btyp := Base_Type (Typ);
6193 if Nkind (Nam) /= N_Expanded_Name then
6194 return (In_Open_Scopes (Scope (Btyp))
6195 or else Is_Potentially_Use_Visible (Btyp)
6196 or else In_Use (Btyp)
6197 or else In_Use (Scope (Btyp)));
6199 else
6200 Scop := Entity (Prefix (Nam));
6202 if Ekind (Scop) = E_Package
6203 and then Present (Renamed_Object (Scop))
6204 then
6205 Scop := Renamed_Object (Scop);
6206 end if;
6208 -- Operator is visible if prefix of expanded name denotes
6209 -- scope of type, or else type is defined in System_Aux
6210 -- and the prefix denotes System.
6212 return Scope (Btyp) = Scop
6213 or else (Scope (Btyp) = System_Aux_Id
6214 and then Scope (Scope (Btyp)) = Scop);
6215 end if;
6216 end if;
6217 end Is_Visible_Operation;
6219 ------------
6220 -- Within --
6221 ------------
6223 function Within (Inner, Outer : Entity_Id) return Boolean is
6224 Sc : Entity_Id;
6226 begin
6227 Sc := Scope (Inner);
6228 while Sc /= Standard_Standard loop
6229 if Sc = Outer then
6230 return True;
6231 else
6232 Sc := Scope (Sc);
6233 end if;
6234 end loop;
6236 return False;
6237 end Within;
6239 ---------------------
6240 -- Report_Overload --
6241 ---------------------
6243 function Report_Overload return Entity_Id is
6244 begin
6245 if Is_Actual then
6246 Error_Msg_NE -- CODEFIX
6247 ("ambiguous actual subprogram&, " &
6248 "possible interpretations:", N, Nam);
6249 else
6250 Error_Msg_N -- CODEFIX
6251 ("ambiguous subprogram, " &
6252 "possible interpretations:", N);
6253 end if;
6255 List_Interps (Nam, N);
6256 return Old_S;
6257 end Report_Overload;
6259 -- Start of processing for Find_Renamed_Entity
6261 begin
6262 Old_S := Any_Id;
6263 Candidate_Renaming := Empty;
6265 if Is_Overloaded (Nam) then
6266 Get_First_Interp (Nam, Ind, It);
6267 while Present (It.Nam) loop
6268 if Entity_Matches_Spec (It.Nam, New_S)
6269 and then Is_Visible_Operation (It.Nam)
6270 then
6271 if Old_S /= Any_Id then
6273 -- Note: The call to Disambiguate only happens if a
6274 -- previous interpretation was found, in which case I1
6275 -- has received a value.
6277 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
6279 if It1 = No_Interp then
6280 Inst := Enclosing_Instance;
6282 if Present (Inst) then
6283 if Within (It.Nam, Inst) then
6284 if Within (Old_S, Inst) then
6286 -- Choose the innermost subprogram, which would
6287 -- have hidden the outer one in the generic.
6289 if Scope_Depth (It.Nam) <
6290 Scope_Depth (Old_S)
6291 then
6292 return Old_S;
6293 else
6294 return It.Nam;
6295 end if;
6296 end if;
6298 elsif Within (Old_S, Inst) then
6299 return (Old_S);
6301 else
6302 return Report_Overload;
6303 end if;
6305 -- If not within an instance, ambiguity is real
6307 else
6308 return Report_Overload;
6309 end if;
6311 else
6312 Old_S := It1.Nam;
6313 exit;
6314 end if;
6316 else
6317 I1 := Ind;
6318 Old_S := It.Nam;
6319 end if;
6321 elsif
6322 Present (First_Formal (It.Nam))
6323 and then Present (First_Formal (New_S))
6324 and then (Base_Type (Etype (First_Formal (It.Nam))) =
6325 Base_Type (Etype (First_Formal (New_S))))
6326 then
6327 Candidate_Renaming := It.Nam;
6328 end if;
6330 Get_Next_Interp (Ind, It);
6331 end loop;
6333 Set_Entity (Nam, Old_S);
6335 if Old_S /= Any_Id then
6336 Set_Is_Overloaded (Nam, False);
6337 end if;
6339 -- Non-overloaded case
6341 else
6342 if Is_Actual and then Present (Enclosing_Instance) then
6343 Old_S := Entity (Nam);
6345 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
6346 Candidate_Renaming := New_S;
6348 if Is_Visible_Operation (Entity (Nam)) then
6349 Old_S := Entity (Nam);
6350 end if;
6352 elsif Present (First_Formal (Entity (Nam)))
6353 and then Present (First_Formal (New_S))
6354 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
6355 Base_Type (Etype (First_Formal (New_S))))
6356 then
6357 Candidate_Renaming := Entity (Nam);
6358 end if;
6359 end if;
6361 return Old_S;
6362 end Find_Renamed_Entity;
6364 -----------------------------
6365 -- Find_Selected_Component --
6366 -----------------------------
6368 procedure Find_Selected_Component (N : Node_Id) is
6369 P : constant Node_Id := Prefix (N);
6371 P_Name : Entity_Id;
6372 -- Entity denoted by prefix
6374 P_Type : Entity_Id;
6375 -- and its type
6377 Nam : Node_Id;
6379 function Is_Reference_In_Subunit return Boolean;
6380 -- In a subunit, the scope depth is not a proper measure of hiding,
6381 -- because the context of the proper body may itself hide entities in
6382 -- parent units. This rare case requires inspecting the tree directly
6383 -- because the proper body is inserted in the main unit and its context
6384 -- is simply added to that of the parent.
6386 -----------------------------
6387 -- Is_Reference_In_Subunit --
6388 -----------------------------
6390 function Is_Reference_In_Subunit return Boolean is
6391 Clause : Node_Id;
6392 Comp_Unit : Node_Id;
6394 begin
6395 Comp_Unit := N;
6396 while Present (Comp_Unit)
6397 and then Nkind (Comp_Unit) /= N_Compilation_Unit
6398 loop
6399 Comp_Unit := Parent (Comp_Unit);
6400 end loop;
6402 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
6403 return False;
6404 end if;
6406 -- Now check whether the package is in the context of the subunit
6408 Clause := First (Context_Items (Comp_Unit));
6409 while Present (Clause) loop
6410 if Nkind (Clause) = N_With_Clause
6411 and then Entity (Name (Clause)) = P_Name
6412 then
6413 return True;
6414 end if;
6416 Clause := Next (Clause);
6417 end loop;
6419 return False;
6420 end Is_Reference_In_Subunit;
6422 -- Start of processing for Find_Selected_Component
6424 begin
6425 Analyze (P);
6427 if Nkind (P) = N_Error then
6428 return;
6429 end if;
6431 -- Selector name cannot be a character literal or an operator symbol in
6432 -- SPARK, except for the operator symbol in a renaming.
6434 if Restriction_Check_Required (SPARK_05) then
6435 if Nkind (Selector_Name (N)) = N_Character_Literal then
6436 Check_SPARK_05_Restriction
6437 ("character literal cannot be prefixed", N);
6438 elsif Nkind (Selector_Name (N)) = N_Operator_Symbol
6439 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
6440 then
6441 Check_SPARK_05_Restriction
6442 ("operator symbol cannot be prefixed", N);
6443 end if;
6444 end if;
6446 -- If the selector already has an entity, the node has been constructed
6447 -- in the course of expansion, and is known to be valid. Do not verify
6448 -- that it is defined for the type (it may be a private component used
6449 -- in the expansion of record equality).
6451 if Present (Entity (Selector_Name (N))) then
6452 if No (Etype (N)) or else Etype (N) = Any_Type then
6453 declare
6454 Sel_Name : constant Node_Id := Selector_Name (N);
6455 Selector : constant Entity_Id := Entity (Sel_Name);
6456 C_Etype : Node_Id;
6458 begin
6459 Set_Etype (Sel_Name, Etype (Selector));
6461 if not Is_Entity_Name (P) then
6462 Resolve (P);
6463 end if;
6465 -- Build an actual subtype except for the first parameter
6466 -- of an init proc, where this actual subtype is by
6467 -- definition incorrect, since the object is uninitialized
6468 -- (and does not even have defined discriminants etc.)
6470 if Is_Entity_Name (P)
6471 and then Ekind (Entity (P)) = E_Function
6472 then
6473 Nam := New_Copy (P);
6475 if Is_Overloaded (P) then
6476 Save_Interps (P, Nam);
6477 end if;
6479 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
6480 Analyze_Call (P);
6481 Analyze_Selected_Component (N);
6482 return;
6484 elsif Ekind (Selector) = E_Component
6485 and then (not Is_Entity_Name (P)
6486 or else Chars (Entity (P)) /= Name_uInit)
6487 then
6488 -- Do not build the subtype when referencing components of
6489 -- dispatch table wrappers. Required to avoid generating
6490 -- elaboration code with HI runtimes. JVM and .NET use a
6491 -- modified version of Ada.Tags which does not contain RE_
6492 -- Dispatch_Table_Wrapper and RE_No_Dispatch_Table_Wrapper.
6493 -- Avoid raising RE_Not_Available exception in those cases.
6495 if VM_Target = No_VM
6496 and then RTU_Loaded (Ada_Tags)
6497 and then
6498 ((RTE_Available (RE_Dispatch_Table_Wrapper)
6499 and then Scope (Selector) =
6500 RTE (RE_Dispatch_Table_Wrapper))
6501 or else
6502 (RTE_Available (RE_No_Dispatch_Table_Wrapper)
6503 and then Scope (Selector) =
6504 RTE (RE_No_Dispatch_Table_Wrapper)))
6505 then
6506 C_Etype := Empty;
6507 else
6508 C_Etype :=
6509 Build_Actual_Subtype_Of_Component
6510 (Etype (Selector), N);
6511 end if;
6513 else
6514 C_Etype := Empty;
6515 end if;
6517 if No (C_Etype) then
6518 C_Etype := Etype (Selector);
6519 else
6520 Insert_Action (N, C_Etype);
6521 C_Etype := Defining_Identifier (C_Etype);
6522 end if;
6524 Set_Etype (N, C_Etype);
6525 end;
6527 -- If this is the name of an entry or protected operation, and
6528 -- the prefix is an access type, insert an explicit dereference,
6529 -- so that entry calls are treated uniformly.
6531 if Is_Access_Type (Etype (P))
6532 and then Is_Concurrent_Type (Designated_Type (Etype (P)))
6533 then
6534 declare
6535 New_P : constant Node_Id :=
6536 Make_Explicit_Dereference (Sloc (P),
6537 Prefix => Relocate_Node (P));
6538 begin
6539 Rewrite (P, New_P);
6540 Set_Etype (P, Designated_Type (Etype (Prefix (P))));
6541 end;
6542 end if;
6544 -- If the selected component appears within a default expression
6545 -- and it has an actual subtype, the pre-analysis has not yet
6546 -- completed its analysis, because Insert_Actions is disabled in
6547 -- that context. Within the init proc of the enclosing type we
6548 -- must complete this analysis, if an actual subtype was created.
6550 elsif Inside_Init_Proc then
6551 declare
6552 Typ : constant Entity_Id := Etype (N);
6553 Decl : constant Node_Id := Declaration_Node (Typ);
6554 begin
6555 if Nkind (Decl) = N_Subtype_Declaration
6556 and then not Analyzed (Decl)
6557 and then Is_List_Member (Decl)
6558 and then No (Parent (Decl))
6559 then
6560 Remove (Decl);
6561 Insert_Action (N, Decl);
6562 end if;
6563 end;
6564 end if;
6566 return;
6568 elsif Is_Entity_Name (P) then
6569 P_Name := Entity (P);
6571 -- The prefix may denote an enclosing type which is the completion
6572 -- of an incomplete type declaration.
6574 if Is_Type (P_Name) then
6575 Set_Entity (P, Get_Full_View (P_Name));
6576 Set_Etype (P, Entity (P));
6577 P_Name := Entity (P);
6578 end if;
6580 P_Type := Base_Type (Etype (P));
6582 if Debug_Flag_E then
6583 Write_Str ("Found prefix type to be ");
6584 Write_Entity_Info (P_Type, " "); Write_Eol;
6585 end if;
6587 -- The designated type may be a limited view with no components.
6588 -- Check whether the non-limited view is available, because in some
6589 -- cases this will not be set when instlling the context.
6591 if Is_Access_Type (P_Type) then
6592 declare
6593 D : constant Entity_Id := Directly_Designated_Type (P_Type);
6594 begin
6595 if Is_Incomplete_Type (D)
6596 and then not Is_Class_Wide_Type (D)
6597 and then From_Limited_With (D)
6598 and then Present (Non_Limited_View (D))
6599 and then not Is_Class_Wide_Type (Non_Limited_View (D))
6600 then
6601 Set_Directly_Designated_Type (P_Type, Non_Limited_View (D));
6602 end if;
6603 end;
6604 end if;
6606 -- First check for components of a record object (not the
6607 -- result of a call, which is handled below).
6609 if Is_Appropriate_For_Record (P_Type)
6610 and then not Is_Overloadable (P_Name)
6611 and then not Is_Type (P_Name)
6612 then
6613 -- Selected component of record. Type checking will validate
6614 -- name of selector.
6616 -- ??? Could we rewrite an implicit dereference into an explicit
6617 -- one here?
6619 Analyze_Selected_Component (N);
6621 -- Reference to type name in predicate/invariant expression
6623 elsif Is_Appropriate_For_Entry_Prefix (P_Type)
6624 and then not In_Open_Scopes (P_Name)
6625 and then (not Is_Concurrent_Type (Etype (P_Name))
6626 or else not In_Open_Scopes (Etype (P_Name)))
6627 then
6628 -- Call to protected operation or entry. Type checking is
6629 -- needed on the prefix.
6631 Analyze_Selected_Component (N);
6633 elsif (In_Open_Scopes (P_Name)
6634 and then Ekind (P_Name) /= E_Void
6635 and then not Is_Overloadable (P_Name))
6636 or else (Is_Concurrent_Type (Etype (P_Name))
6637 and then In_Open_Scopes (Etype (P_Name)))
6638 then
6639 -- Prefix denotes an enclosing loop, block, or task, i.e. an
6640 -- enclosing construct that is not a subprogram or accept.
6642 Find_Expanded_Name (N);
6644 elsif Ekind (P_Name) = E_Package then
6645 Find_Expanded_Name (N);
6647 elsif Is_Overloadable (P_Name) then
6649 -- The subprogram may be a renaming (of an enclosing scope) as
6650 -- in the case of the name of the generic within an instantiation.
6652 if Ekind_In (P_Name, E_Procedure, E_Function)
6653 and then Present (Alias (P_Name))
6654 and then Is_Generic_Instance (Alias (P_Name))
6655 then
6656 P_Name := Alias (P_Name);
6657 end if;
6659 if Is_Overloaded (P) then
6661 -- The prefix must resolve to a unique enclosing construct
6663 declare
6664 Found : Boolean := False;
6665 Ind : Interp_Index;
6666 It : Interp;
6668 begin
6669 Get_First_Interp (P, Ind, It);
6670 while Present (It.Nam) loop
6671 if In_Open_Scopes (It.Nam) then
6672 if Found then
6673 Error_Msg_N (
6674 "prefix must be unique enclosing scope", N);
6675 Set_Entity (N, Any_Id);
6676 Set_Etype (N, Any_Type);
6677 return;
6679 else
6680 Found := True;
6681 P_Name := It.Nam;
6682 end if;
6683 end if;
6685 Get_Next_Interp (Ind, It);
6686 end loop;
6687 end;
6688 end if;
6690 if In_Open_Scopes (P_Name) then
6691 Set_Entity (P, P_Name);
6692 Set_Is_Overloaded (P, False);
6693 Find_Expanded_Name (N);
6695 else
6696 -- If no interpretation as an expanded name is possible, it
6697 -- must be a selected component of a record returned by a
6698 -- function call. Reformat prefix as a function call, the rest
6699 -- is done by type resolution.
6701 -- Error if the prefix is procedure or entry, as is P.X
6703 if Ekind (P_Name) /= E_Function
6704 and then
6705 (not Is_Overloaded (P)
6706 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
6707 then
6708 -- Prefix may mention a package that is hidden by a local
6709 -- declaration: let the user know. Scan the full homonym
6710 -- chain, the candidate package may be anywhere on it.
6712 if Present (Homonym (Current_Entity (P_Name))) then
6713 P_Name := Current_Entity (P_Name);
6715 while Present (P_Name) loop
6716 exit when Ekind (P_Name) = E_Package;
6717 P_Name := Homonym (P_Name);
6718 end loop;
6720 if Present (P_Name) then
6721 if not Is_Reference_In_Subunit then
6722 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
6723 Error_Msg_NE
6724 ("package& is hidden by declaration#", N, P_Name);
6725 end if;
6727 Set_Entity (Prefix (N), P_Name);
6728 Find_Expanded_Name (N);
6729 return;
6731 else
6732 P_Name := Entity (Prefix (N));
6733 end if;
6734 end if;
6736 Error_Msg_NE
6737 ("invalid prefix in selected component&", N, P_Name);
6738 Change_Selected_Component_To_Expanded_Name (N);
6739 Set_Entity (N, Any_Id);
6740 Set_Etype (N, Any_Type);
6742 -- Here we have a function call, so do the reformatting
6744 else
6745 Nam := New_Copy (P);
6746 Save_Interps (P, Nam);
6748 -- We use Replace here because this is one of those cases
6749 -- where the parser has missclassified the node, and we
6750 -- fix things up and then do the semantic analysis on the
6751 -- fixed up node. Normally we do this using one of the
6752 -- Sinfo.CN routines, but this is too tricky for that.
6754 -- Note that using Rewrite would be wrong, because we
6755 -- would have a tree where the original node is unanalyzed,
6756 -- and this violates the required interface for ASIS.
6758 Replace (P,
6759 Make_Function_Call (Sloc (P), Name => Nam));
6761 -- Now analyze the reformatted node
6763 Analyze_Call (P);
6764 Analyze_Selected_Component (N);
6765 end if;
6766 end if;
6768 -- Remaining cases generate various error messages
6770 else
6771 -- Format node as expanded name, to avoid cascaded errors
6773 Change_Selected_Component_To_Expanded_Name (N);
6774 Set_Entity (N, Any_Id);
6775 Set_Etype (N, Any_Type);
6777 -- Issue error message, but avoid this if error issued already.
6778 -- Use identifier of prefix if one is available.
6780 if P_Name = Any_Id then
6781 null;
6783 elsif Ekind (P_Name) = E_Void then
6784 Premature_Usage (P);
6786 elsif Nkind (P) /= N_Attribute_Reference then
6787 Error_Msg_N (
6788 "invalid prefix in selected component&", P);
6790 if Is_Access_Type (P_Type)
6791 and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
6792 then
6793 Error_Msg_N
6794 ("\dereference must not be of an incomplete type " &
6795 "(RM 3.10.1)", P);
6796 end if;
6798 else
6799 Error_Msg_N (
6800 "invalid prefix in selected component", P);
6801 end if;
6802 end if;
6804 -- Selector name is restricted in SPARK
6806 if Nkind (N) = N_Expanded_Name
6807 and then Restriction_Check_Required (SPARK_05)
6808 then
6809 if Is_Subprogram (P_Name) then
6810 Check_SPARK_05_Restriction
6811 ("prefix of expanded name cannot be a subprogram", P);
6812 elsif Ekind (P_Name) = E_Loop then
6813 Check_SPARK_05_Restriction
6814 ("prefix of expanded name cannot be a loop statement", P);
6815 end if;
6816 end if;
6818 else
6819 -- If prefix is not the name of an entity, it must be an expression,
6820 -- whose type is appropriate for a record. This is determined by
6821 -- type resolution.
6823 Analyze_Selected_Component (N);
6824 end if;
6826 Analyze_Dimension (N);
6827 end Find_Selected_Component;
6829 ---------------
6830 -- Find_Type --
6831 ---------------
6833 procedure Find_Type (N : Node_Id) is
6834 C : Entity_Id;
6835 Typ : Entity_Id;
6836 T : Entity_Id;
6837 T_Name : Entity_Id;
6839 begin
6840 if N = Error then
6841 return;
6843 elsif Nkind (N) = N_Attribute_Reference then
6845 -- Class attribute. This is not valid in Ada 83 mode, but we do not
6846 -- need to enforce that at this point, since the declaration of the
6847 -- tagged type in the prefix would have been flagged already.
6849 if Attribute_Name (N) = Name_Class then
6850 Check_Restriction (No_Dispatch, N);
6851 Find_Type (Prefix (N));
6853 -- Propagate error from bad prefix
6855 if Etype (Prefix (N)) = Any_Type then
6856 Set_Entity (N, Any_Type);
6857 Set_Etype (N, Any_Type);
6858 return;
6859 end if;
6861 T := Base_Type (Entity (Prefix (N)));
6863 -- Case where type is not known to be tagged. Its appearance in
6864 -- the prefix of the 'Class attribute indicates that the full view
6865 -- will be tagged.
6867 if not Is_Tagged_Type (T) then
6868 if Ekind (T) = E_Incomplete_Type then
6870 -- It is legal to denote the class type of an incomplete
6871 -- type. The full type will have to be tagged, of course.
6872 -- In Ada 2005 this usage is declared obsolescent, so we
6873 -- warn accordingly. This usage is only legal if the type
6874 -- is completed in the current scope, and not for a limited
6875 -- view of a type.
6877 if Ada_Version >= Ada_2005 then
6879 -- Test whether the Available_View of a limited type view
6880 -- is tagged, since the limited view may not be marked as
6881 -- tagged if the type itself has an untagged incomplete
6882 -- type view in its package.
6884 if From_Limited_With (T)
6885 and then not Is_Tagged_Type (Available_View (T))
6886 then
6887 Error_Msg_N
6888 ("prefix of Class attribute must be tagged", N);
6889 Set_Etype (N, Any_Type);
6890 Set_Entity (N, Any_Type);
6891 return;
6893 -- ??? This test is temporarily disabled (always
6894 -- False) because it causes an unwanted warning on
6895 -- GNAT sources (built with -gnatg, which includes
6896 -- Warn_On_Obsolescent_ Feature). Once this issue
6897 -- is cleared in the sources, it can be enabled.
6899 elsif Warn_On_Obsolescent_Feature and then False then
6900 Error_Msg_N
6901 ("applying 'Class to an untagged incomplete type"
6902 & " is an obsolescent feature (RM J.11)?r?", N);
6903 end if;
6904 end if;
6906 Set_Is_Tagged_Type (T);
6907 Set_Direct_Primitive_Operations (T, New_Elmt_List);
6908 Make_Class_Wide_Type (T);
6909 Set_Entity (N, Class_Wide_Type (T));
6910 Set_Etype (N, Class_Wide_Type (T));
6912 elsif Ekind (T) = E_Private_Type
6913 and then not Is_Generic_Type (T)
6914 and then In_Private_Part (Scope (T))
6915 then
6916 -- The Class attribute can be applied to an untagged private
6917 -- type fulfilled by a tagged type prior to the full type
6918 -- declaration (but only within the parent package's private
6919 -- part). Create the class-wide type now and check that the
6920 -- full type is tagged later during its analysis. Note that
6921 -- we do not mark the private type as tagged, unlike the
6922 -- case of incomplete types, because the type must still
6923 -- appear untagged to outside units.
6925 if No (Class_Wide_Type (T)) then
6926 Make_Class_Wide_Type (T);
6927 end if;
6929 Set_Entity (N, Class_Wide_Type (T));
6930 Set_Etype (N, Class_Wide_Type (T));
6932 else
6933 -- Should we introduce a type Any_Tagged and use Wrong_Type
6934 -- here, it would be a bit more consistent???
6936 Error_Msg_NE
6937 ("tagged type required, found}",
6938 Prefix (N), First_Subtype (T));
6939 Set_Entity (N, Any_Type);
6940 return;
6941 end if;
6943 -- Case of tagged type
6945 else
6946 if Is_Concurrent_Type (T) then
6947 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
6949 -- Previous error. Use current type, which at least
6950 -- provides some operations.
6952 C := Entity (Prefix (N));
6954 else
6955 C := Class_Wide_Type
6956 (Corresponding_Record_Type (Entity (Prefix (N))));
6957 end if;
6959 else
6960 C := Class_Wide_Type (Entity (Prefix (N)));
6961 end if;
6963 Set_Entity_With_Checks (N, C);
6964 Generate_Reference (C, N);
6965 Set_Etype (N, C);
6966 end if;
6968 -- Base attribute, not allowed in Ada 83
6970 elsif Attribute_Name (N) = Name_Base then
6971 Error_Msg_Name_1 := Name_Base;
6972 Check_SPARK_05_Restriction
6973 ("attribute% is only allowed as prefix of another attribute", N);
6975 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
6976 Error_Msg_N
6977 ("(Ada 83) Base attribute not allowed in subtype mark", N);
6979 else
6980 Find_Type (Prefix (N));
6981 Typ := Entity (Prefix (N));
6983 if Ada_Version >= Ada_95
6984 and then not Is_Scalar_Type (Typ)
6985 and then not Is_Generic_Type (Typ)
6986 then
6987 Error_Msg_N
6988 ("prefix of Base attribute must be scalar type",
6989 Prefix (N));
6991 elsif Warn_On_Redundant_Constructs
6992 and then Base_Type (Typ) = Typ
6993 then
6994 Error_Msg_NE -- CODEFIX
6995 ("redundant attribute, & is its own base type?r?", N, Typ);
6996 end if;
6998 T := Base_Type (Typ);
7000 -- Rewrite attribute reference with type itself (see similar
7001 -- processing in Analyze_Attribute, case Base). Preserve prefix
7002 -- if present, for other legality checks.
7004 if Nkind (Prefix (N)) = N_Expanded_Name then
7005 Rewrite (N,
7006 Make_Expanded_Name (Sloc (N),
7007 Chars => Chars (T),
7008 Prefix => New_Copy (Prefix (Prefix (N))),
7009 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
7011 else
7012 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
7013 end if;
7015 Set_Entity (N, T);
7016 Set_Etype (N, T);
7017 end if;
7019 elsif Attribute_Name (N) = Name_Stub_Type then
7021 -- This is handled in Analyze_Attribute
7023 Analyze (N);
7025 -- All other attributes are invalid in a subtype mark
7027 else
7028 Error_Msg_N ("invalid attribute in subtype mark", N);
7029 end if;
7031 else
7032 Analyze (N);
7034 if Is_Entity_Name (N) then
7035 T_Name := Entity (N);
7036 else
7037 Error_Msg_N ("subtype mark required in this context", N);
7038 Set_Etype (N, Any_Type);
7039 return;
7040 end if;
7042 if T_Name = Any_Id or else Etype (N) = Any_Type then
7044 -- Undefined id. Make it into a valid type
7046 Set_Entity (N, Any_Type);
7048 elsif not Is_Type (T_Name)
7049 and then T_Name /= Standard_Void_Type
7050 then
7051 Error_Msg_Sloc := Sloc (T_Name);
7052 Error_Msg_N ("subtype mark required in this context", N);
7053 Error_Msg_NE ("\\found & declared#", N, T_Name);
7054 Set_Entity (N, Any_Type);
7056 else
7057 -- If the type is an incomplete type created to handle
7058 -- anonymous access components of a record type, then the
7059 -- incomplete type is the visible entity and subsequent
7060 -- references will point to it. Mark the original full
7061 -- type as referenced, to prevent spurious warnings.
7063 if Is_Incomplete_Type (T_Name)
7064 and then Present (Full_View (T_Name))
7065 and then not Comes_From_Source (T_Name)
7066 then
7067 Set_Referenced (Full_View (T_Name));
7068 end if;
7070 T_Name := Get_Full_View (T_Name);
7072 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
7073 -- limited-with clauses
7075 if From_Limited_With (T_Name)
7076 and then Ekind (T_Name) in Incomplete_Kind
7077 and then Present (Non_Limited_View (T_Name))
7078 and then Is_Interface (Non_Limited_View (T_Name))
7079 then
7080 T_Name := Non_Limited_View (T_Name);
7081 end if;
7083 if In_Open_Scopes (T_Name) then
7084 if Ekind (Base_Type (T_Name)) = E_Task_Type then
7086 -- In Ada 2005, a task name can be used in an access
7087 -- definition within its own body. It cannot be used
7088 -- in the discriminant part of the task declaration,
7089 -- nor anywhere else in the declaration because entries
7090 -- cannot have access parameters.
7092 if Ada_Version >= Ada_2005
7093 and then Nkind (Parent (N)) = N_Access_Definition
7094 then
7095 Set_Entity (N, T_Name);
7096 Set_Etype (N, T_Name);
7098 if Has_Completion (T_Name) then
7099 return;
7101 else
7102 Error_Msg_N
7103 ("task type cannot be used as type mark " &
7104 "within its own declaration", N);
7105 end if;
7107 else
7108 Error_Msg_N
7109 ("task type cannot be used as type mark " &
7110 "within its own spec or body", N);
7111 end if;
7113 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
7115 -- In Ada 2005, a protected name can be used in an access
7116 -- definition within its own body.
7118 if Ada_Version >= Ada_2005
7119 and then Nkind (Parent (N)) = N_Access_Definition
7120 then
7121 Set_Entity (N, T_Name);
7122 Set_Etype (N, T_Name);
7123 return;
7125 else
7126 Error_Msg_N
7127 ("protected type cannot be used as type mark " &
7128 "within its own spec or body", N);
7129 end if;
7131 else
7132 Error_Msg_N ("type declaration cannot refer to itself", N);
7133 end if;
7135 Set_Etype (N, Any_Type);
7136 Set_Entity (N, Any_Type);
7137 Set_Error_Posted (T_Name);
7138 return;
7139 end if;
7141 Set_Entity (N, T_Name);
7142 Set_Etype (N, T_Name);
7143 end if;
7144 end if;
7146 if Present (Etype (N)) and then Comes_From_Source (N) then
7147 if Is_Fixed_Point_Type (Etype (N)) then
7148 Check_Restriction (No_Fixed_Point, N);
7149 elsif Is_Floating_Point_Type (Etype (N)) then
7150 Check_Restriction (No_Floating_Point, N);
7151 end if;
7152 end if;
7153 end Find_Type;
7155 ------------------------------------
7156 -- Has_Implicit_Character_Literal --
7157 ------------------------------------
7159 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
7160 Id : Entity_Id;
7161 Found : Boolean := False;
7162 P : constant Entity_Id := Entity (Prefix (N));
7163 Priv_Id : Entity_Id := Empty;
7165 begin
7166 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
7167 Priv_Id := First_Private_Entity (P);
7168 end if;
7170 if P = Standard_Standard then
7171 Change_Selected_Component_To_Expanded_Name (N);
7172 Rewrite (N, Selector_Name (N));
7173 Analyze (N);
7174 Set_Etype (Original_Node (N), Standard_Character);
7175 return True;
7176 end if;
7178 Id := First_Entity (P);
7179 while Present (Id) and then Id /= Priv_Id loop
7180 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
7182 -- We replace the node with the literal itself, resolve as a
7183 -- character, and set the type correctly.
7185 if not Found then
7186 Change_Selected_Component_To_Expanded_Name (N);
7187 Rewrite (N, Selector_Name (N));
7188 Analyze (N);
7189 Set_Etype (N, Id);
7190 Set_Etype (Original_Node (N), Id);
7191 Found := True;
7193 else
7194 -- More than one type derived from Character in given scope.
7195 -- Collect all possible interpretations.
7197 Add_One_Interp (N, Id, Id);
7198 end if;
7199 end if;
7201 Next_Entity (Id);
7202 end loop;
7204 return Found;
7205 end Has_Implicit_Character_Literal;
7207 ----------------------
7208 -- Has_Private_With --
7209 ----------------------
7211 function Has_Private_With (E : Entity_Id) return Boolean is
7212 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
7213 Item : Node_Id;
7215 begin
7216 Item := First (Context_Items (Comp_Unit));
7217 while Present (Item) loop
7218 if Nkind (Item) = N_With_Clause
7219 and then Private_Present (Item)
7220 and then Entity (Name (Item)) = E
7221 then
7222 return True;
7223 end if;
7225 Next (Item);
7226 end loop;
7228 return False;
7229 end Has_Private_With;
7231 ---------------------------
7232 -- Has_Implicit_Operator --
7233 ---------------------------
7235 function Has_Implicit_Operator (N : Node_Id) return Boolean is
7236 Op_Id : constant Name_Id := Chars (Selector_Name (N));
7237 P : constant Entity_Id := Entity (Prefix (N));
7238 Id : Entity_Id;
7239 Priv_Id : Entity_Id := Empty;
7241 procedure Add_Implicit_Operator
7242 (T : Entity_Id;
7243 Op_Type : Entity_Id := Empty);
7244 -- Add implicit interpretation to node N, using the type for which a
7245 -- predefined operator exists. If the operator yields a boolean type,
7246 -- the Operand_Type is implicitly referenced by the operator, and a
7247 -- reference to it must be generated.
7249 ---------------------------
7250 -- Add_Implicit_Operator --
7251 ---------------------------
7253 procedure Add_Implicit_Operator
7254 (T : Entity_Id;
7255 Op_Type : Entity_Id := Empty)
7257 Predef_Op : Entity_Id;
7259 begin
7260 Predef_Op := Current_Entity (Selector_Name (N));
7261 while Present (Predef_Op)
7262 and then Scope (Predef_Op) /= Standard_Standard
7263 loop
7264 Predef_Op := Homonym (Predef_Op);
7265 end loop;
7267 if Nkind (N) = N_Selected_Component then
7268 Change_Selected_Component_To_Expanded_Name (N);
7269 end if;
7271 -- If the context is an unanalyzed function call, determine whether
7272 -- a binary or unary interpretation is required.
7274 if Nkind (Parent (N)) = N_Indexed_Component then
7275 declare
7276 Is_Binary_Call : constant Boolean :=
7277 Present
7278 (Next (First (Expressions (Parent (N)))));
7279 Is_Binary_Op : constant Boolean :=
7280 First_Entity
7281 (Predef_Op) /= Last_Entity (Predef_Op);
7282 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
7284 begin
7285 if Is_Binary_Call then
7286 if Is_Binary_Op then
7287 Add_One_Interp (N, Predef_Op, T);
7288 else
7289 Add_One_Interp (N, Predef_Op2, T);
7290 end if;
7292 else
7293 if not Is_Binary_Op then
7294 Add_One_Interp (N, Predef_Op, T);
7295 else
7296 Add_One_Interp (N, Predef_Op2, T);
7297 end if;
7298 end if;
7299 end;
7301 else
7302 Add_One_Interp (N, Predef_Op, T);
7304 -- For operators with unary and binary interpretations, if
7305 -- context is not a call, add both
7307 if Present (Homonym (Predef_Op)) then
7308 Add_One_Interp (N, Homonym (Predef_Op), T);
7309 end if;
7310 end if;
7312 -- The node is a reference to a predefined operator, and
7313 -- an implicit reference to the type of its operands.
7315 if Present (Op_Type) then
7316 Generate_Operator_Reference (N, Op_Type);
7317 else
7318 Generate_Operator_Reference (N, T);
7319 end if;
7320 end Add_Implicit_Operator;
7322 -- Start of processing for Has_Implicit_Operator
7324 begin
7325 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
7326 Priv_Id := First_Private_Entity (P);
7327 end if;
7329 Id := First_Entity (P);
7331 case Op_Id is
7333 -- Boolean operators: an implicit declaration exists if the scope
7334 -- contains a declaration for a derived Boolean type, or for an
7335 -- array of Boolean type.
7337 when Name_Op_And | Name_Op_Not | Name_Op_Or | Name_Op_Xor =>
7338 while Id /= Priv_Id loop
7339 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
7340 Add_Implicit_Operator (Id);
7341 return True;
7342 end if;
7344 Next_Entity (Id);
7345 end loop;
7347 -- Equality: look for any non-limited type (result is Boolean)
7349 when Name_Op_Eq | Name_Op_Ne =>
7350 while Id /= Priv_Id loop
7351 if Is_Type (Id)
7352 and then not Is_Limited_Type (Id)
7353 and then Is_Base_Type (Id)
7354 then
7355 Add_Implicit_Operator (Standard_Boolean, Id);
7356 return True;
7357 end if;
7359 Next_Entity (Id);
7360 end loop;
7362 -- Comparison operators: scalar type, or array of scalar
7364 when Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge =>
7365 while Id /= Priv_Id loop
7366 if (Is_Scalar_Type (Id)
7367 or else (Is_Array_Type (Id)
7368 and then Is_Scalar_Type (Component_Type (Id))))
7369 and then Is_Base_Type (Id)
7370 then
7371 Add_Implicit_Operator (Standard_Boolean, Id);
7372 return True;
7373 end if;
7375 Next_Entity (Id);
7376 end loop;
7378 -- Arithmetic operators: any numeric type
7380 when Name_Op_Abs |
7381 Name_Op_Add |
7382 Name_Op_Mod |
7383 Name_Op_Rem |
7384 Name_Op_Subtract |
7385 Name_Op_Multiply |
7386 Name_Op_Divide |
7387 Name_Op_Expon =>
7388 while Id /= Priv_Id loop
7389 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
7390 Add_Implicit_Operator (Id);
7391 return True;
7392 end if;
7394 Next_Entity (Id);
7395 end loop;
7397 -- Concatenation: any one-dimensional array type
7399 when Name_Op_Concat =>
7400 while Id /= Priv_Id loop
7401 if Is_Array_Type (Id)
7402 and then Number_Dimensions (Id) = 1
7403 and then Is_Base_Type (Id)
7404 then
7405 Add_Implicit_Operator (Id);
7406 return True;
7407 end if;
7409 Next_Entity (Id);
7410 end loop;
7412 -- What is the others condition here? Should we be using a
7413 -- subtype of Name_Id that would restrict to operators ???
7415 when others => null;
7416 end case;
7418 -- If we fall through, then we do not have an implicit operator
7420 return False;
7422 end Has_Implicit_Operator;
7424 -----------------------------------
7425 -- Has_Loop_In_Inner_Open_Scopes --
7426 -----------------------------------
7428 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
7429 begin
7430 -- Several scope stacks are maintained by Scope_Stack. The base of the
7431 -- currently active scope stack is denoted by the Is_Active_Stack_Base
7432 -- flag in the scope stack entry. Note that the scope stacks used to
7433 -- simply be delimited implicitly by the presence of Standard_Standard
7434 -- at their base, but there now are cases where this is not sufficient
7435 -- because Standard_Standard actually may appear in the middle of the
7436 -- active set of scopes.
7438 for J in reverse 0 .. Scope_Stack.Last loop
7440 -- S was reached without seing a loop scope first
7442 if Scope_Stack.Table (J).Entity = S then
7443 return False;
7445 -- S was not yet reached, so it contains at least one inner loop
7447 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
7448 return True;
7449 end if;
7451 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
7452 -- cases where Standard_Standard appears in the middle of the active
7453 -- set of scopes. This affects the declaration and overriding of
7454 -- private inherited operations in instantiations of generic child
7455 -- units.
7457 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
7458 end loop;
7460 raise Program_Error; -- unreachable
7461 end Has_Loop_In_Inner_Open_Scopes;
7463 --------------------
7464 -- In_Open_Scopes --
7465 --------------------
7467 function In_Open_Scopes (S : Entity_Id) return Boolean is
7468 begin
7469 -- Several scope stacks are maintained by Scope_Stack. The base of the
7470 -- currently active scope stack is denoted by the Is_Active_Stack_Base
7471 -- flag in the scope stack entry. Note that the scope stacks used to
7472 -- simply be delimited implicitly by the presence of Standard_Standard
7473 -- at their base, but there now are cases where this is not sufficient
7474 -- because Standard_Standard actually may appear in the middle of the
7475 -- active set of scopes.
7477 for J in reverse 0 .. Scope_Stack.Last loop
7478 if Scope_Stack.Table (J).Entity = S then
7479 return True;
7480 end if;
7482 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
7483 -- cases where Standard_Standard appears in the middle of the active
7484 -- set of scopes. This affects the declaration and overriding of
7485 -- private inherited operations in instantiations of generic child
7486 -- units.
7488 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
7489 end loop;
7491 return False;
7492 end In_Open_Scopes;
7494 -----------------------------
7495 -- Inherit_Renamed_Profile --
7496 -----------------------------
7498 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
7499 New_F : Entity_Id;
7500 Old_F : Entity_Id;
7501 Old_T : Entity_Id;
7502 New_T : Entity_Id;
7504 begin
7505 if Ekind (Old_S) = E_Operator then
7506 New_F := First_Formal (New_S);
7508 while Present (New_F) loop
7509 Set_Etype (New_F, Base_Type (Etype (New_F)));
7510 Next_Formal (New_F);
7511 end loop;
7513 Set_Etype (New_S, Base_Type (Etype (New_S)));
7515 else
7516 New_F := First_Formal (New_S);
7517 Old_F := First_Formal (Old_S);
7519 while Present (New_F) loop
7520 New_T := Etype (New_F);
7521 Old_T := Etype (Old_F);
7523 -- If the new type is a renaming of the old one, as is the
7524 -- case for actuals in instances, retain its name, to simplify
7525 -- later disambiguation.
7527 if Nkind (Parent (New_T)) = N_Subtype_Declaration
7528 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
7529 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
7530 then
7531 null;
7532 else
7533 Set_Etype (New_F, Old_T);
7534 end if;
7536 Next_Formal (New_F);
7537 Next_Formal (Old_F);
7538 end loop;
7540 if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then
7541 Set_Etype (New_S, Etype (Old_S));
7542 end if;
7543 end if;
7544 end Inherit_Renamed_Profile;
7546 ----------------
7547 -- Initialize --
7548 ----------------
7550 procedure Initialize is
7551 begin
7552 Urefs.Init;
7553 end Initialize;
7555 -------------------------
7556 -- Install_Use_Clauses --
7557 -------------------------
7559 procedure Install_Use_Clauses
7560 (Clause : Node_Id;
7561 Force_Installation : Boolean := False)
7563 U : Node_Id;
7564 P : Node_Id;
7565 Id : Entity_Id;
7567 begin
7568 U := Clause;
7569 while Present (U) loop
7571 -- Case of USE package
7573 if Nkind (U) = N_Use_Package_Clause then
7574 P := First (Names (U));
7575 while Present (P) loop
7576 Id := Entity (P);
7578 if Ekind (Id) = E_Package then
7579 if In_Use (Id) then
7580 Note_Redundant_Use (P);
7582 elsif Present (Renamed_Object (Id))
7583 and then In_Use (Renamed_Object (Id))
7584 then
7585 Note_Redundant_Use (P);
7587 elsif Force_Installation or else Applicable_Use (P) then
7588 Use_One_Package (Id, U);
7590 end if;
7591 end if;
7593 Next (P);
7594 end loop;
7596 -- Case of USE TYPE
7598 else
7599 P := First (Subtype_Marks (U));
7600 while Present (P) loop
7601 if not Is_Entity_Name (P)
7602 or else No (Entity (P))
7603 then
7604 null;
7606 elsif Entity (P) /= Any_Type then
7607 Use_One_Type (P);
7608 end if;
7610 Next (P);
7611 end loop;
7612 end if;
7614 Next_Use_Clause (U);
7615 end loop;
7616 end Install_Use_Clauses;
7618 -------------------------------------
7619 -- Is_Appropriate_For_Entry_Prefix --
7620 -------------------------------------
7622 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
7623 P_Type : Entity_Id := T;
7625 begin
7626 if Is_Access_Type (P_Type) then
7627 P_Type := Designated_Type (P_Type);
7628 end if;
7630 return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
7631 end Is_Appropriate_For_Entry_Prefix;
7633 -------------------------------
7634 -- Is_Appropriate_For_Record --
7635 -------------------------------
7637 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is
7639 function Has_Components (T1 : Entity_Id) return Boolean;
7640 -- Determine if given type has components (i.e. is either a record
7641 -- type or a type that has discriminants).
7643 --------------------
7644 -- Has_Components --
7645 --------------------
7647 function Has_Components (T1 : Entity_Id) return Boolean is
7648 begin
7649 return Is_Record_Type (T1)
7650 or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
7651 or else (Is_Task_Type (T1) and then Has_Discriminants (T1))
7652 or else (Is_Incomplete_Type (T1)
7653 and then From_Limited_With (T1)
7654 and then Present (Non_Limited_View (T1))
7655 and then Is_Record_Type
7656 (Get_Full_View (Non_Limited_View (T1))));
7657 end Has_Components;
7659 -- Start of processing for Is_Appropriate_For_Record
7661 begin
7662 return
7663 Present (T)
7664 and then (Has_Components (T)
7665 or else (Is_Access_Type (T)
7666 and then Has_Components (Designated_Type (T))));
7667 end Is_Appropriate_For_Record;
7669 ------------------------
7670 -- Note_Redundant_Use --
7671 ------------------------
7673 procedure Note_Redundant_Use (Clause : Node_Id) is
7674 Pack_Name : constant Entity_Id := Entity (Clause);
7675 Cur_Use : constant Node_Id := Current_Use_Clause (Pack_Name);
7676 Decl : constant Node_Id := Parent (Clause);
7678 Prev_Use : Node_Id := Empty;
7679 Redundant : Node_Id := Empty;
7680 -- The Use_Clause which is actually redundant. In the simplest case it
7681 -- is Pack itself, but when we compile a body we install its context
7682 -- before that of its spec, in which case it is the use_clause in the
7683 -- spec that will appear to be redundant, and we want the warning to be
7684 -- placed on the body. Similar complications appear when the redundancy
7685 -- is between a child unit and one of its ancestors.
7687 begin
7688 Set_Redundant_Use (Clause, True);
7690 if not Comes_From_Source (Clause)
7691 or else In_Instance
7692 or else not Warn_On_Redundant_Constructs
7693 then
7694 return;
7695 end if;
7697 if not Is_Compilation_Unit (Current_Scope) then
7699 -- If the use_clause is in an inner scope, it is made redundant by
7700 -- some clause in the current context, with one exception: If we're
7701 -- compiling a nested package body, and the use_clause comes from the
7702 -- corresponding spec, the clause is not necessarily fully redundant,
7703 -- so we should not warn. If a warning was warranted, it would have
7704 -- been given when the spec was processed.
7706 if Nkind (Parent (Decl)) = N_Package_Specification then
7707 declare
7708 Package_Spec_Entity : constant Entity_Id :=
7709 Defining_Unit_Name (Parent (Decl));
7710 begin
7711 if In_Package_Body (Package_Spec_Entity) then
7712 return;
7713 end if;
7714 end;
7715 end if;
7717 Redundant := Clause;
7718 Prev_Use := Cur_Use;
7720 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
7721 declare
7722 Cur_Unit : constant Unit_Number_Type := Get_Source_Unit (Cur_Use);
7723 New_Unit : constant Unit_Number_Type := Get_Source_Unit (Clause);
7724 Scop : Entity_Id;
7726 begin
7727 if Cur_Unit = New_Unit then
7729 -- Redundant clause in same body
7731 Redundant := Clause;
7732 Prev_Use := Cur_Use;
7734 elsif Cur_Unit = Current_Sem_Unit then
7736 -- If the new clause is not in the current unit it has been
7737 -- analyzed first, and it makes the other one redundant.
7738 -- However, if the new clause appears in a subunit, Cur_Unit
7739 -- is still the parent, and in that case the redundant one
7740 -- is the one appearing in the subunit.
7742 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
7743 Redundant := Clause;
7744 Prev_Use := Cur_Use;
7746 -- Most common case: redundant clause in body,
7747 -- original clause in spec. Current scope is spec entity.
7749 elsif
7750 Current_Scope =
7751 Defining_Entity (
7752 Unit (Library_Unit (Cunit (Current_Sem_Unit))))
7753 then
7754 Redundant := Cur_Use;
7755 Prev_Use := Clause;
7757 else
7758 -- The new clause may appear in an unrelated unit, when
7759 -- the parents of a generic are being installed prior to
7760 -- instantiation. In this case there must be no warning.
7761 -- We detect this case by checking whether the current top
7762 -- of the stack is related to the current compilation.
7764 Scop := Current_Scope;
7765 while Present (Scop) and then Scop /= Standard_Standard loop
7766 if Is_Compilation_Unit (Scop)
7767 and then not Is_Child_Unit (Scop)
7768 then
7769 return;
7771 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
7772 exit;
7773 end if;
7775 Scop := Scope (Scop);
7776 end loop;
7778 Redundant := Cur_Use;
7779 Prev_Use := Clause;
7780 end if;
7782 elsif New_Unit = Current_Sem_Unit then
7783 Redundant := Clause;
7784 Prev_Use := Cur_Use;
7786 else
7787 -- Neither is the current unit, so they appear in parent or
7788 -- sibling units. Warning will be emitted elsewhere.
7790 return;
7791 end if;
7792 end;
7794 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
7795 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
7796 then
7797 -- Use_clause is in child unit of current unit, and the child unit
7798 -- appears in the context of the body of the parent, so it has been
7799 -- installed first, even though it is the redundant one. Depending on
7800 -- their placement in the context, the visible or the private parts
7801 -- of the two units, either might appear as redundant, but the
7802 -- message has to be on the current unit.
7804 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
7805 Redundant := Cur_Use;
7806 Prev_Use := Clause;
7807 else
7808 Redundant := Clause;
7809 Prev_Use := Cur_Use;
7810 end if;
7812 -- If the new use clause appears in the private part of a parent unit
7813 -- it may appear to be redundant w.r.t. a use clause in a child unit,
7814 -- but the previous use clause was needed in the visible part of the
7815 -- child, and no warning should be emitted.
7817 if Nkind (Parent (Decl)) = N_Package_Specification
7818 and then
7819 List_Containing (Decl) = Private_Declarations (Parent (Decl))
7820 then
7821 declare
7822 Par : constant Entity_Id := Defining_Entity (Parent (Decl));
7823 Spec : constant Node_Id :=
7824 Specification (Unit (Cunit (Current_Sem_Unit)));
7826 begin
7827 if Is_Compilation_Unit (Par)
7828 and then Par /= Cunit_Entity (Current_Sem_Unit)
7829 and then Parent (Cur_Use) = Spec
7830 and then
7831 List_Containing (Cur_Use) = Visible_Declarations (Spec)
7832 then
7833 return;
7834 end if;
7835 end;
7836 end if;
7838 -- Finally, if the current use clause is in the context then
7839 -- the clause is redundant when it is nested within the unit.
7841 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
7842 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
7843 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
7844 then
7845 Redundant := Clause;
7846 Prev_Use := Cur_Use;
7848 else
7849 null;
7850 end if;
7852 if Present (Redundant) then
7853 Error_Msg_Sloc := Sloc (Prev_Use);
7854 Error_Msg_NE -- CODEFIX
7855 ("& is already use-visible through previous use clause #??",
7856 Redundant, Pack_Name);
7857 end if;
7858 end Note_Redundant_Use;
7860 ---------------
7861 -- Pop_Scope --
7862 ---------------
7864 procedure Pop_Scope is
7865 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
7866 S : constant Entity_Id := SST.Entity;
7868 begin
7869 if Debug_Flag_E then
7870 Write_Info;
7871 end if;
7873 -- Set Default_Storage_Pool field of the library unit if necessary
7875 if Ekind_In (S, E_Package, E_Generic_Package)
7876 and then
7877 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
7878 then
7879 declare
7880 Aux : constant Node_Id :=
7881 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
7882 begin
7883 if No (Default_Storage_Pool (Aux)) then
7884 Set_Default_Storage_Pool (Aux, Default_Pool);
7885 end if;
7886 end;
7887 end if;
7889 Scope_Suppress := SST.Save_Scope_Suppress;
7890 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
7891 Check_Policy_List := SST.Save_Check_Policy_List;
7892 Default_Pool := SST.Save_Default_Storage_Pool;
7893 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
7894 SPARK_Mode := SST.Save_SPARK_Mode;
7895 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
7896 Default_SSO := SST.Save_Default_SSO;
7897 Uneval_Old := SST.Save_Uneval_Old;
7899 if Debug_Flag_W then
7900 Write_Str ("<-- exiting scope: ");
7901 Write_Name (Chars (Current_Scope));
7902 Write_Str (", Depth=");
7903 Write_Int (Int (Scope_Stack.Last));
7904 Write_Eol;
7905 end if;
7907 End_Use_Clauses (SST.First_Use_Clause);
7909 -- If the actions to be wrapped are still there they will get lost
7910 -- causing incomplete code to be generated. It is better to abort in
7911 -- this case (and we do the abort even with assertions off since the
7912 -- penalty is incorrect code generation).
7914 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
7915 raise Program_Error;
7916 end if;
7918 -- Free last subprogram name if allocated, and pop scope
7920 Free (SST.Last_Subprogram_Name);
7921 Scope_Stack.Decrement_Last;
7922 end Pop_Scope;
7924 ---------------
7925 -- Push_Scope --
7926 ---------------
7928 procedure Push_Scope (S : Entity_Id) is
7929 E : constant Entity_Id := Scope (S);
7931 begin
7932 if Ekind (S) = E_Void then
7933 null;
7935 -- Set scope depth if not a non-concurrent type, and we have not yet set
7936 -- the scope depth. This means that we have the first occurrence of the
7937 -- scope, and this is where the depth is set.
7939 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
7940 and then not Scope_Depth_Set (S)
7941 then
7942 if S = Standard_Standard then
7943 Set_Scope_Depth_Value (S, Uint_0);
7945 elsif Is_Child_Unit (S) then
7946 Set_Scope_Depth_Value (S, Uint_1);
7948 elsif not Is_Record_Type (Current_Scope) then
7949 if Ekind (S) = E_Loop then
7950 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
7951 else
7952 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
7953 end if;
7954 end if;
7955 end if;
7957 Scope_Stack.Increment_Last;
7959 declare
7960 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
7962 begin
7963 SST.Entity := S;
7964 SST.Save_Scope_Suppress := Scope_Suppress;
7965 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
7966 SST.Save_Check_Policy_List := Check_Policy_List;
7967 SST.Save_Default_Storage_Pool := Default_Pool;
7968 SST.Save_No_Tagged_Streams := No_Tagged_Streams;
7969 SST.Save_SPARK_Mode := SPARK_Mode;
7970 SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma;
7971 SST.Save_Default_SSO := Default_SSO;
7972 SST.Save_Uneval_Old := Uneval_Old;
7974 if Scope_Stack.Last > Scope_Stack.First then
7975 SST.Component_Alignment_Default := Scope_Stack.Table
7976 (Scope_Stack.Last - 1).
7977 Component_Alignment_Default;
7978 end if;
7980 SST.Last_Subprogram_Name := null;
7981 SST.Is_Transient := False;
7982 SST.Node_To_Be_Wrapped := Empty;
7983 SST.Pending_Freeze_Actions := No_List;
7984 SST.Actions_To_Be_Wrapped := (others => No_List);
7985 SST.First_Use_Clause := Empty;
7986 SST.Is_Active_Stack_Base := False;
7987 SST.Previous_Visibility := False;
7988 SST.Locked_Shared_Objects := No_Elist;
7989 end;
7991 if Debug_Flag_W then
7992 Write_Str ("--> new scope: ");
7993 Write_Name (Chars (Current_Scope));
7994 Write_Str (", Id=");
7995 Write_Int (Int (Current_Scope));
7996 Write_Str (", Depth=");
7997 Write_Int (Int (Scope_Stack.Last));
7998 Write_Eol;
7999 end if;
8001 -- Deal with copying flags from the previous scope to this one. This is
8002 -- not necessary if either scope is standard, or if the new scope is a
8003 -- child unit.
8005 if S /= Standard_Standard
8006 and then Scope (S) /= Standard_Standard
8007 and then not Is_Child_Unit (S)
8008 then
8009 if Nkind (E) not in N_Entity then
8010 return;
8011 end if;
8013 -- Copy categorization flags from Scope (S) to S, this is not done
8014 -- when Scope (S) is Standard_Standard since propagation is from
8015 -- library unit entity inwards. Copy other relevant attributes as
8016 -- well (Discard_Names in particular).
8018 -- We only propagate inwards for library level entities,
8019 -- inner level subprograms do not inherit the categorization.
8021 if Is_Library_Level_Entity (S) then
8022 Set_Is_Preelaborated (S, Is_Preelaborated (E));
8023 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
8024 Set_Discard_Names (S, Discard_Names (E));
8025 Set_Suppress_Value_Tracking_On_Call
8026 (S, Suppress_Value_Tracking_On_Call (E));
8027 Set_Categorization_From_Scope (E => S, Scop => E);
8028 end if;
8029 end if;
8031 if Is_Child_Unit (S)
8032 and then Present (E)
8033 and then Ekind_In (E, E_Package, E_Generic_Package)
8034 and then
8035 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
8036 then
8037 declare
8038 Aux : constant Node_Id :=
8039 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
8040 begin
8041 if Present (Default_Storage_Pool (Aux)) then
8042 Default_Pool := Default_Storage_Pool (Aux);
8043 end if;
8044 end;
8045 end if;
8046 end Push_Scope;
8048 ---------------------
8049 -- Premature_Usage --
8050 ---------------------
8052 procedure Premature_Usage (N : Node_Id) is
8053 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
8054 E : Entity_Id := Entity (N);
8056 begin
8057 -- Within an instance, the analysis of the actual for a formal object
8058 -- does not see the name of the object itself. This is significant only
8059 -- if the object is an aggregate, where its analysis does not do any
8060 -- name resolution on component associations. (see 4717-008). In such a
8061 -- case, look for the visible homonym on the chain.
8063 if In_Instance and then Present (Homonym (E)) then
8064 E := Homonym (E);
8065 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
8066 E := Homonym (E);
8067 end loop;
8069 if Present (E) then
8070 Set_Entity (N, E);
8071 Set_Etype (N, Etype (E));
8072 return;
8073 end if;
8074 end if;
8076 if Kind = N_Component_Declaration then
8077 Error_Msg_N
8078 ("component&! cannot be used before end of record declaration", N);
8080 elsif Kind = N_Parameter_Specification then
8081 Error_Msg_N
8082 ("formal parameter&! cannot be used before end of specification",
8085 elsif Kind = N_Discriminant_Specification then
8086 Error_Msg_N
8087 ("discriminant&! cannot be used before end of discriminant part",
8090 elsif Kind = N_Procedure_Specification
8091 or else Kind = N_Function_Specification
8092 then
8093 Error_Msg_N
8094 ("subprogram&! cannot be used before end of its declaration",
8097 elsif Kind = N_Full_Type_Declaration then
8098 Error_Msg_N
8099 ("type& cannot be used before end of its declaration!", N);
8101 else
8102 Error_Msg_N
8103 ("object& cannot be used before end of its declaration!", N);
8104 end if;
8105 end Premature_Usage;
8107 ------------------------
8108 -- Present_System_Aux --
8109 ------------------------
8111 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
8112 Loc : Source_Ptr;
8113 Aux_Name : Unit_Name_Type;
8114 Unum : Unit_Number_Type;
8115 Withn : Node_Id;
8116 With_Sys : Node_Id;
8117 The_Unit : Node_Id;
8119 function Find_System (C_Unit : Node_Id) return Entity_Id;
8120 -- Scan context clause of compilation unit to find with_clause
8121 -- for System.
8123 -----------------
8124 -- Find_System --
8125 -----------------
8127 function Find_System (C_Unit : Node_Id) return Entity_Id is
8128 With_Clause : Node_Id;
8130 begin
8131 With_Clause := First (Context_Items (C_Unit));
8132 while Present (With_Clause) loop
8133 if (Nkind (With_Clause) = N_With_Clause
8134 and then Chars (Name (With_Clause)) = Name_System)
8135 and then Comes_From_Source (With_Clause)
8136 then
8137 return With_Clause;
8138 end if;
8140 Next (With_Clause);
8141 end loop;
8143 return Empty;
8144 end Find_System;
8146 -- Start of processing for Present_System_Aux
8148 begin
8149 -- The child unit may have been loaded and analyzed already
8151 if Present (System_Aux_Id) then
8152 return True;
8154 -- If no previous pragma for System.Aux, nothing to load
8156 elsif No (System_Extend_Unit) then
8157 return False;
8159 -- Use the unit name given in the pragma to retrieve the unit.
8160 -- Verify that System itself appears in the context clause of the
8161 -- current compilation. If System is not present, an error will
8162 -- have been reported already.
8164 else
8165 With_Sys := Find_System (Cunit (Current_Sem_Unit));
8167 The_Unit := Unit (Cunit (Current_Sem_Unit));
8169 if No (With_Sys)
8170 and then
8171 (Nkind (The_Unit) = N_Package_Body
8172 or else (Nkind (The_Unit) = N_Subprogram_Body
8173 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
8174 then
8175 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
8176 end if;
8178 if No (With_Sys) and then Present (N) then
8180 -- If we are compiling a subunit, we need to examine its
8181 -- context as well (Current_Sem_Unit is the parent unit);
8183 The_Unit := Parent (N);
8184 while Nkind (The_Unit) /= N_Compilation_Unit loop
8185 The_Unit := Parent (The_Unit);
8186 end loop;
8188 if Nkind (Unit (The_Unit)) = N_Subunit then
8189 With_Sys := Find_System (The_Unit);
8190 end if;
8191 end if;
8193 if No (With_Sys) then
8194 return False;
8195 end if;
8197 Loc := Sloc (With_Sys);
8198 Get_Name_String (Chars (Expression (System_Extend_Unit)));
8199 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
8200 Name_Buffer (1 .. 7) := "system.";
8201 Name_Buffer (Name_Len + 8) := '%';
8202 Name_Buffer (Name_Len + 9) := 's';
8203 Name_Len := Name_Len + 9;
8204 Aux_Name := Name_Find;
8206 Unum :=
8207 Load_Unit
8208 (Load_Name => Aux_Name,
8209 Required => False,
8210 Subunit => False,
8211 Error_Node => With_Sys);
8213 if Unum /= No_Unit then
8214 Semantics (Cunit (Unum));
8215 System_Aux_Id :=
8216 Defining_Entity (Specification (Unit (Cunit (Unum))));
8218 Withn :=
8219 Make_With_Clause (Loc,
8220 Name =>
8221 Make_Expanded_Name (Loc,
8222 Chars => Chars (System_Aux_Id),
8223 Prefix => New_Occurrence_Of (Scope (System_Aux_Id), Loc),
8224 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
8226 Set_Entity (Name (Withn), System_Aux_Id);
8228 Set_Library_Unit (Withn, Cunit (Unum));
8229 Set_Corresponding_Spec (Withn, System_Aux_Id);
8230 Set_First_Name (Withn, True);
8231 Set_Implicit_With (Withn, True);
8233 Insert_After (With_Sys, Withn);
8234 Mark_Rewrite_Insertion (Withn);
8235 Set_Context_Installed (Withn);
8237 return True;
8239 -- Here if unit load failed
8241 else
8242 Error_Msg_Name_1 := Name_System;
8243 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
8244 Error_Msg_N
8245 ("extension package `%.%` does not exist",
8246 Opt.System_Extend_Unit);
8247 return False;
8248 end if;
8249 end if;
8250 end Present_System_Aux;
8252 -------------------------
8253 -- Restore_Scope_Stack --
8254 -------------------------
8256 procedure Restore_Scope_Stack
8257 (List : Elist_Id;
8258 Handle_Use : Boolean := True)
8260 SS_Last : constant Int := Scope_Stack.Last;
8261 Elmt : Elmt_Id;
8263 begin
8264 -- Restore visibility of previous scope stack, if any, using the list
8265 -- we saved (we use Remove, since this list will not be used again).
8267 loop
8268 Elmt := Last_Elmt (List);
8269 exit when Elmt = No_Elmt;
8270 Set_Is_Immediately_Visible (Node (Elmt));
8271 Remove_Last_Elmt (List);
8272 end loop;
8274 -- Restore use clauses
8276 if SS_Last >= Scope_Stack.First
8277 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
8278 and then Handle_Use
8279 then
8280 Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
8281 end if;
8282 end Restore_Scope_Stack;
8284 ----------------------
8285 -- Save_Scope_Stack --
8286 ----------------------
8288 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
8289 -- consuming any memory. That is, Save_Scope_Stack took care of removing
8290 -- from immediate visibility entities and Restore_Scope_Stack took care
8291 -- of restoring their visibility analyzing the context of each entity. The
8292 -- problem of such approach is that it was fragile and caused unexpected
8293 -- visibility problems, and indeed one test was found where there was a
8294 -- real problem.
8296 -- Furthermore, the following experiment was carried out:
8298 -- - Save_Scope_Stack was modified to store in an Elist1 all those
8299 -- entities whose attribute Is_Immediately_Visible is modified
8300 -- from True to False.
8302 -- - Restore_Scope_Stack was modified to store in another Elist2
8303 -- all the entities whose attribute Is_Immediately_Visible is
8304 -- modified from False to True.
8306 -- - Extra code was added to verify that all the elements of Elist1
8307 -- are found in Elist2
8309 -- This test shows that there may be more occurrences of this problem which
8310 -- have not yet been detected. As a result, we replaced that approach by
8311 -- the current one in which Save_Scope_Stack returns the list of entities
8312 -- whose visibility is changed, and that list is passed to Restore_Scope_
8313 -- Stack to undo that change. This approach is simpler and safer, although
8314 -- it consumes more memory.
8316 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
8317 Result : constant Elist_Id := New_Elmt_List;
8318 E : Entity_Id;
8319 S : Entity_Id;
8320 SS_Last : constant Int := Scope_Stack.Last;
8322 procedure Remove_From_Visibility (E : Entity_Id);
8323 -- If E is immediately visible then append it to the result and remove
8324 -- it temporarily from visibility.
8326 ----------------------------
8327 -- Remove_From_Visibility --
8328 ----------------------------
8330 procedure Remove_From_Visibility (E : Entity_Id) is
8331 begin
8332 if Is_Immediately_Visible (E) then
8333 Append_Elmt (E, Result);
8334 Set_Is_Immediately_Visible (E, False);
8335 end if;
8336 end Remove_From_Visibility;
8338 -- Start of processing for Save_Scope_Stack
8340 begin
8341 if SS_Last >= Scope_Stack.First
8342 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
8343 then
8344 if Handle_Use then
8345 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
8346 end if;
8348 -- If the call is from within a compilation unit, as when called from
8349 -- Rtsfind, make current entries in scope stack invisible while we
8350 -- analyze the new unit.
8352 for J in reverse 0 .. SS_Last loop
8353 exit when Scope_Stack.Table (J).Entity = Standard_Standard
8354 or else No (Scope_Stack.Table (J).Entity);
8356 S := Scope_Stack.Table (J).Entity;
8358 Remove_From_Visibility (S);
8360 E := First_Entity (S);
8361 while Present (E) loop
8362 Remove_From_Visibility (E);
8363 Next_Entity (E);
8364 end loop;
8365 end loop;
8367 end if;
8369 return Result;
8370 end Save_Scope_Stack;
8372 -------------
8373 -- Set_Use --
8374 -------------
8376 procedure Set_Use (L : List_Id) is
8377 Decl : Node_Id;
8378 Pack_Name : Node_Id;
8379 Pack : Entity_Id;
8380 Id : Entity_Id;
8382 begin
8383 if Present (L) then
8384 Decl := First (L);
8385 while Present (Decl) loop
8386 if Nkind (Decl) = N_Use_Package_Clause then
8387 Chain_Use_Clause (Decl);
8389 Pack_Name := First (Names (Decl));
8390 while Present (Pack_Name) loop
8391 Pack := Entity (Pack_Name);
8393 if Ekind (Pack) = E_Package
8394 and then Applicable_Use (Pack_Name)
8395 then
8396 Use_One_Package (Pack, Decl);
8397 end if;
8399 Next (Pack_Name);
8400 end loop;
8402 elsif Nkind (Decl) = N_Use_Type_Clause then
8403 Chain_Use_Clause (Decl);
8405 Id := First (Subtype_Marks (Decl));
8406 while Present (Id) loop
8407 if Entity (Id) /= Any_Type then
8408 Use_One_Type (Id);
8409 end if;
8411 Next (Id);
8412 end loop;
8413 end if;
8415 Next (Decl);
8416 end loop;
8417 end if;
8418 end Set_Use;
8420 ---------------------
8421 -- Use_One_Package --
8422 ---------------------
8424 procedure Use_One_Package (P : Entity_Id; N : Node_Id) is
8425 Id : Entity_Id;
8426 Prev : Entity_Id;
8427 Current_Instance : Entity_Id := Empty;
8428 Real_P : Entity_Id;
8429 Private_With_OK : Boolean := False;
8431 begin
8432 if Ekind (P) /= E_Package then
8433 return;
8434 end if;
8436 Set_In_Use (P);
8437 Set_Current_Use_Clause (P, N);
8439 -- Ada 2005 (AI-50217): Check restriction
8441 if From_Limited_With (P) then
8442 Error_Msg_N ("limited withed package cannot appear in use clause", N);
8443 end if;
8445 -- Find enclosing instance, if any
8447 if In_Instance then
8448 Current_Instance := Current_Scope;
8449 while not Is_Generic_Instance (Current_Instance) loop
8450 Current_Instance := Scope (Current_Instance);
8451 end loop;
8453 if No (Hidden_By_Use_Clause (N)) then
8454 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
8455 end if;
8456 end if;
8458 -- If unit is a package renaming, indicate that the renamed
8459 -- package is also in use (the flags on both entities must
8460 -- remain consistent, and a subsequent use of either of them
8461 -- should be recognized as redundant).
8463 if Present (Renamed_Object (P)) then
8464 Set_In_Use (Renamed_Object (P));
8465 Set_Current_Use_Clause (Renamed_Object (P), N);
8466 Real_P := Renamed_Object (P);
8467 else
8468 Real_P := P;
8469 end if;
8471 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
8472 -- found in the private part of a package specification
8474 if In_Private_Part (Current_Scope)
8475 and then Has_Private_With (P)
8476 and then Is_Child_Unit (Current_Scope)
8477 and then Is_Child_Unit (P)
8478 and then Is_Ancestor_Package (Scope (Current_Scope), P)
8479 then
8480 Private_With_OK := True;
8481 end if;
8483 -- Loop through entities in one package making them potentially
8484 -- use-visible.
8486 Id := First_Entity (P);
8487 while Present (Id)
8488 and then (Id /= First_Private_Entity (P)
8489 or else Private_With_OK) -- Ada 2005 (AI-262)
8490 loop
8491 Prev := Current_Entity (Id);
8492 while Present (Prev) loop
8493 if Is_Immediately_Visible (Prev)
8494 and then (not Is_Overloadable (Prev)
8495 or else not Is_Overloadable (Id)
8496 or else (Type_Conformant (Id, Prev)))
8497 then
8498 if No (Current_Instance) then
8500 -- Potentially use-visible entity remains hidden
8502 goto Next_Usable_Entity;
8504 -- A use clause within an instance hides outer global entities,
8505 -- which are not used to resolve local entities in the
8506 -- instance. Note that the predefined entities in Standard
8507 -- could not have been hidden in the generic by a use clause,
8508 -- and therefore remain visible. Other compilation units whose
8509 -- entities appear in Standard must be hidden in an instance.
8511 -- To determine whether an entity is external to the instance
8512 -- we compare the scope depth of its scope with that of the
8513 -- current instance. However, a generic actual of a subprogram
8514 -- instance is declared in the wrapper package but will not be
8515 -- hidden by a use-visible entity. similarly, an entity that is
8516 -- declared in an enclosing instance will not be hidden by an
8517 -- an entity declared in a generic actual, which can only have
8518 -- been use-visible in the generic and will not have hidden the
8519 -- entity in the generic parent.
8521 -- If Id is called Standard, the predefined package with the
8522 -- same name is in the homonym chain. It has to be ignored
8523 -- because it has no defined scope (being the only entity in
8524 -- the system with this mandated behavior).
8526 elsif not Is_Hidden (Id)
8527 and then Present (Scope (Prev))
8528 and then not Is_Wrapper_Package (Scope (Prev))
8529 and then Scope_Depth (Scope (Prev)) <
8530 Scope_Depth (Current_Instance)
8531 and then (Scope (Prev) /= Standard_Standard
8532 or else Sloc (Prev) > Standard_Location)
8533 then
8534 if In_Open_Scopes (Scope (Prev))
8535 and then Is_Generic_Instance (Scope (Prev))
8536 and then Present (Associated_Formal_Package (P))
8537 then
8538 null;
8540 else
8541 Set_Is_Potentially_Use_Visible (Id);
8542 Set_Is_Immediately_Visible (Prev, False);
8543 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
8544 end if;
8545 end if;
8547 -- A user-defined operator is not use-visible if the predefined
8548 -- operator for the type is immediately visible, which is the case
8549 -- if the type of the operand is in an open scope. This does not
8550 -- apply to user-defined operators that have operands of different
8551 -- types, because the predefined mixed mode operations (multiply
8552 -- and divide) apply to universal types and do not hide anything.
8554 elsif Ekind (Prev) = E_Operator
8555 and then Operator_Matches_Spec (Prev, Id)
8556 and then In_Open_Scopes
8557 (Scope (Base_Type (Etype (First_Formal (Id)))))
8558 and then (No (Next_Formal (First_Formal (Id)))
8559 or else Etype (First_Formal (Id)) =
8560 Etype (Next_Formal (First_Formal (Id)))
8561 or else Chars (Prev) = Name_Op_Expon)
8562 then
8563 goto Next_Usable_Entity;
8565 -- In an instance, two homonyms may become use_visible through the
8566 -- actuals of distinct formal packages. In the generic, only the
8567 -- current one would have been visible, so make the other one
8568 -- not use_visible.
8570 elsif Present (Current_Instance)
8571 and then Is_Potentially_Use_Visible (Prev)
8572 and then not Is_Overloadable (Prev)
8573 and then Scope (Id) /= Scope (Prev)
8574 and then Used_As_Generic_Actual (Scope (Prev))
8575 and then Used_As_Generic_Actual (Scope (Id))
8576 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
8577 Current_Use_Clause (Scope (Id)))
8578 then
8579 Set_Is_Potentially_Use_Visible (Prev, False);
8580 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
8581 end if;
8583 Prev := Homonym (Prev);
8584 end loop;
8586 -- On exit, we know entity is not hidden, unless it is private
8588 if not Is_Hidden (Id)
8589 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
8590 then
8591 Set_Is_Potentially_Use_Visible (Id);
8593 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
8594 Set_Is_Potentially_Use_Visible (Full_View (Id));
8595 end if;
8596 end if;
8598 <<Next_Usable_Entity>>
8599 Next_Entity (Id);
8600 end loop;
8602 -- Child units are also made use-visible by a use clause, but they may
8603 -- appear after all visible declarations in the parent entity list.
8605 while Present (Id) loop
8606 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
8607 Set_Is_Potentially_Use_Visible (Id);
8608 end if;
8610 Next_Entity (Id);
8611 end loop;
8613 if Chars (Real_P) = Name_System
8614 and then Scope (Real_P) = Standard_Standard
8615 and then Present_System_Aux (N)
8616 then
8617 Use_One_Package (System_Aux_Id, N);
8618 end if;
8620 end Use_One_Package;
8622 ------------------
8623 -- Use_One_Type --
8624 ------------------
8626 procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False) is
8627 Elmt : Elmt_Id;
8628 Is_Known_Used : Boolean;
8629 Op_List : Elist_Id;
8630 T : Entity_Id;
8632 function Spec_Reloaded_For_Body return Boolean;
8633 -- Determine whether the compilation unit is a package body and the use
8634 -- type clause is in the spec of the same package. Even though the spec
8635 -- was analyzed first, its context is reloaded when analysing the body.
8637 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
8638 -- AI05-150: if the use_type_clause carries the "all" qualifier,
8639 -- class-wide operations of ancestor types are use-visible if the
8640 -- ancestor type is visible.
8642 ----------------------------
8643 -- Spec_Reloaded_For_Body --
8644 ----------------------------
8646 function Spec_Reloaded_For_Body return Boolean is
8647 begin
8648 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
8649 declare
8650 Spec : constant Node_Id :=
8651 Parent (List_Containing (Parent (Id)));
8653 begin
8654 -- Check whether type is declared in a package specification,
8655 -- and current unit is the corresponding package body. The
8656 -- use clauses themselves may be within a nested package.
8658 return
8659 Nkind (Spec) = N_Package_Specification
8660 and then
8661 In_Same_Source_Unit (Corresponding_Body (Parent (Spec)),
8662 Cunit_Entity (Current_Sem_Unit));
8663 end;
8664 end if;
8666 return False;
8667 end Spec_Reloaded_For_Body;
8669 -------------------------------
8670 -- Use_Class_Wide_Operations --
8671 -------------------------------
8673 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
8674 Scop : Entity_Id;
8675 Ent : Entity_Id;
8677 function Is_Class_Wide_Operation_Of
8678 (Op : Entity_Id;
8679 T : Entity_Id) return Boolean;
8680 -- Determine whether a subprogram has a class-wide parameter or
8681 -- result that is T'Class.
8683 ---------------------------------
8684 -- Is_Class_Wide_Operation_Of --
8685 ---------------------------------
8687 function Is_Class_Wide_Operation_Of
8688 (Op : Entity_Id;
8689 T : Entity_Id) return Boolean
8691 Formal : Entity_Id;
8693 begin
8694 Formal := First_Formal (Op);
8695 while Present (Formal) loop
8696 if Etype (Formal) = Class_Wide_Type (T) then
8697 return True;
8698 end if;
8699 Next_Formal (Formal);
8700 end loop;
8702 if Etype (Op) = Class_Wide_Type (T) then
8703 return True;
8704 end if;
8706 return False;
8707 end Is_Class_Wide_Operation_Of;
8709 -- Start of processing for Use_Class_Wide_Operations
8711 begin
8712 Scop := Scope (Typ);
8713 if not Is_Hidden (Scop) then
8714 Ent := First_Entity (Scop);
8715 while Present (Ent) loop
8716 if Is_Overloadable (Ent)
8717 and then Is_Class_Wide_Operation_Of (Ent, Typ)
8718 and then not Is_Potentially_Use_Visible (Ent)
8719 then
8720 Set_Is_Potentially_Use_Visible (Ent);
8721 Append_Elmt (Ent, Used_Operations (Parent (Id)));
8722 end if;
8724 Next_Entity (Ent);
8725 end loop;
8726 end if;
8728 if Is_Derived_Type (Typ) then
8729 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
8730 end if;
8731 end Use_Class_Wide_Operations;
8733 -- Start of processing for Use_One_Type
8735 begin
8736 -- It is the type determined by the subtype mark (8.4(8)) whose
8737 -- operations become potentially use-visible.
8739 T := Base_Type (Entity (Id));
8741 -- Either the type itself is used, the package where it is declared
8742 -- is in use or the entity is declared in the current package, thus
8743 -- use-visible.
8745 Is_Known_Used :=
8746 In_Use (T)
8747 or else In_Use (Scope (T))
8748 or else Scope (T) = Current_Scope;
8750 Set_Redundant_Use (Id,
8751 Is_Known_Used or else Is_Potentially_Use_Visible (T));
8753 if Ekind (T) = E_Incomplete_Type then
8754 Error_Msg_N ("premature usage of incomplete type", Id);
8756 elsif In_Open_Scopes (Scope (T)) then
8757 null;
8759 -- A limited view cannot appear in a use_type clause. However, an access
8760 -- type whose designated type is limited has the flag but is not itself
8761 -- a limited view unless we only have a limited view of its enclosing
8762 -- package.
8764 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
8765 Error_Msg_N
8766 ("incomplete type from limited view "
8767 & "cannot appear in use clause", Id);
8769 -- If the subtype mark designates a subtype in a different package,
8770 -- we have to check that the parent type is visible, otherwise the
8771 -- use type clause is a noop. Not clear how to do that???
8773 elsif not Redundant_Use (Id) then
8774 Set_In_Use (T);
8776 -- If T is tagged, primitive operators on class-wide operands
8777 -- are also available.
8779 if Is_Tagged_Type (T) then
8780 Set_In_Use (Class_Wide_Type (T));
8781 end if;
8783 Set_Current_Use_Clause (T, Parent (Id));
8785 -- Iterate over primitive operations of the type. If an operation is
8786 -- already use_visible, it is the result of a previous use_clause,
8787 -- and already appears on the corresponding entity chain. If the
8788 -- clause is being reinstalled, operations are already use-visible.
8790 if Installed then
8791 null;
8793 else
8794 Op_List := Collect_Primitive_Operations (T);
8795 Elmt := First_Elmt (Op_List);
8796 while Present (Elmt) loop
8797 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
8798 or else Chars (Node (Elmt)) in Any_Operator_Name)
8799 and then not Is_Hidden (Node (Elmt))
8800 and then not Is_Potentially_Use_Visible (Node (Elmt))
8801 then
8802 Set_Is_Potentially_Use_Visible (Node (Elmt));
8803 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
8805 elsif Ada_Version >= Ada_2012
8806 and then All_Present (Parent (Id))
8807 and then not Is_Hidden (Node (Elmt))
8808 and then not Is_Potentially_Use_Visible (Node (Elmt))
8809 then
8810 Set_Is_Potentially_Use_Visible (Node (Elmt));
8811 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
8812 end if;
8814 Next_Elmt (Elmt);
8815 end loop;
8816 end if;
8818 if Ada_Version >= Ada_2012
8819 and then All_Present (Parent (Id))
8820 and then Is_Tagged_Type (T)
8821 then
8822 Use_Class_Wide_Operations (T);
8823 end if;
8824 end if;
8826 -- If warning on redundant constructs, check for unnecessary WITH
8828 if Warn_On_Redundant_Constructs
8829 and then Is_Known_Used
8831 -- with P; with P; use P;
8832 -- package P is package X is package body X is
8833 -- type T ... use P.T;
8835 -- The compilation unit is the body of X. GNAT first compiles the
8836 -- spec of X, then proceeds to the body. At that point P is marked
8837 -- as use visible. The analysis then reinstalls the spec along with
8838 -- its context. The use clause P.T is now recognized as redundant,
8839 -- but in the wrong context. Do not emit a warning in such cases.
8840 -- Do not emit a warning either if we are in an instance, there is
8841 -- no redundancy between an outer use_clause and one that appears
8842 -- within the generic.
8844 and then not Spec_Reloaded_For_Body
8845 and then not In_Instance
8846 then
8847 -- The type already has a use clause
8849 if In_Use (T) then
8851 -- Case where we know the current use clause for the type
8853 if Present (Current_Use_Clause (T)) then
8854 Use_Clause_Known : declare
8855 Clause1 : constant Node_Id := Parent (Id);
8856 Clause2 : constant Node_Id := Current_Use_Clause (T);
8857 Ent1 : Entity_Id;
8858 Ent2 : Entity_Id;
8859 Err_No : Node_Id;
8860 Unit1 : Node_Id;
8861 Unit2 : Node_Id;
8863 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
8864 -- Return the appropriate entity for determining which unit
8865 -- has a deeper scope: the defining entity for U, unless U
8866 -- is a package instance, in which case we retrieve the
8867 -- entity of the instance spec.
8869 --------------------
8870 -- Entity_Of_Unit --
8871 --------------------
8873 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
8874 begin
8875 if Nkind (U) = N_Package_Instantiation
8876 and then Analyzed (U)
8877 then
8878 return Defining_Entity (Instance_Spec (U));
8879 else
8880 return Defining_Entity (U);
8881 end if;
8882 end Entity_Of_Unit;
8884 -- Start of processing for Use_Clause_Known
8886 begin
8887 -- If both current use type clause and the use type clause
8888 -- for the type are at the compilation unit level, one of
8889 -- the units must be an ancestor of the other, and the
8890 -- warning belongs on the descendant.
8892 if Nkind (Parent (Clause1)) = N_Compilation_Unit
8893 and then
8894 Nkind (Parent (Clause2)) = N_Compilation_Unit
8895 then
8896 -- If the unit is a subprogram body that acts as spec,
8897 -- the context clause is shared with the constructed
8898 -- subprogram spec. Clearly there is no redundancy.
8900 if Clause1 = Clause2 then
8901 return;
8902 end if;
8904 Unit1 := Unit (Parent (Clause1));
8905 Unit2 := Unit (Parent (Clause2));
8907 -- If both clauses are on same unit, or one is the body
8908 -- of the other, or one of them is in a subunit, report
8909 -- redundancy on the later one.
8911 if Unit1 = Unit2 then
8912 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8913 Error_Msg_NE -- CODEFIX
8914 ("& is already use-visible through previous "
8915 & "use_type_clause #??", Clause1, T);
8916 return;
8918 elsif Nkind (Unit1) = N_Subunit then
8919 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8920 Error_Msg_NE -- CODEFIX
8921 ("& is already use-visible through previous "
8922 & "use_type_clause #??", Clause1, T);
8923 return;
8925 elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body)
8926 and then Nkind (Unit1) /= Nkind (Unit2)
8927 and then Nkind (Unit1) /= N_Subunit
8928 then
8929 Error_Msg_Sloc := Sloc (Clause1);
8930 Error_Msg_NE -- CODEFIX
8931 ("& is already use-visible through previous "
8932 & "use_type_clause #??", Current_Use_Clause (T), T);
8933 return;
8934 end if;
8936 -- There is a redundant use type clause in a child unit.
8937 -- Determine which of the units is more deeply nested.
8938 -- If a unit is a package instance, retrieve the entity
8939 -- and its scope from the instance spec.
8941 Ent1 := Entity_Of_Unit (Unit1);
8942 Ent2 := Entity_Of_Unit (Unit2);
8944 if Scope (Ent2) = Standard_Standard then
8945 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8946 Err_No := Clause1;
8948 elsif Scope (Ent1) = Standard_Standard then
8949 Error_Msg_Sloc := Sloc (Id);
8950 Err_No := Clause2;
8952 -- If both units are child units, we determine which one
8953 -- is the descendant by the scope distance to the
8954 -- ultimate parent unit.
8956 else
8957 declare
8958 S1, S2 : Entity_Id;
8960 begin
8961 S1 := Scope (Ent1);
8962 S2 := Scope (Ent2);
8963 while Present (S1)
8964 and then Present (S2)
8965 and then S1 /= Standard_Standard
8966 and then S2 /= Standard_Standard
8967 loop
8968 S1 := Scope (S1);
8969 S2 := Scope (S2);
8970 end loop;
8972 if S1 = Standard_Standard then
8973 Error_Msg_Sloc := Sloc (Id);
8974 Err_No := Clause2;
8975 else
8976 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8977 Err_No := Clause1;
8978 end if;
8979 end;
8980 end if;
8982 Error_Msg_NE -- CODEFIX
8983 ("& is already use-visible through previous "
8984 & "use_type_clause #??", Err_No, Id);
8986 -- Case where current use type clause and the use type
8987 -- clause for the type are not both at the compilation unit
8988 -- level. In this case we don't have location information.
8990 else
8991 Error_Msg_NE -- CODEFIX
8992 ("& is already use-visible through previous "
8993 & "use type clause??", Id, T);
8994 end if;
8995 end Use_Clause_Known;
8997 -- Here if Current_Use_Clause is not set for T, another case
8998 -- where we do not have the location information available.
9000 else
9001 Error_Msg_NE -- CODEFIX
9002 ("& is already use-visible through previous "
9003 & "use type clause??", Id, T);
9004 end if;
9006 -- The package where T is declared is already used
9008 elsif In_Use (Scope (T)) then
9009 Error_Msg_Sloc := Sloc (Current_Use_Clause (Scope (T)));
9010 Error_Msg_NE -- CODEFIX
9011 ("& is already use-visible through package use clause #??",
9012 Id, T);
9014 -- The current scope is the package where T is declared
9016 else
9017 Error_Msg_Node_2 := Scope (T);
9018 Error_Msg_NE -- CODEFIX
9019 ("& is already use-visible inside package &??", Id, T);
9020 end if;
9021 end if;
9022 end Use_One_Type;
9024 ----------------
9025 -- Write_Info --
9026 ----------------
9028 procedure Write_Info is
9029 Id : Entity_Id := First_Entity (Current_Scope);
9031 begin
9032 -- No point in dumping standard entities
9034 if Current_Scope = Standard_Standard then
9035 return;
9036 end if;
9038 Write_Str ("========================================================");
9039 Write_Eol;
9040 Write_Str (" Defined Entities in ");
9041 Write_Name (Chars (Current_Scope));
9042 Write_Eol;
9043 Write_Str ("========================================================");
9044 Write_Eol;
9046 if No (Id) then
9047 Write_Str ("-- none --");
9048 Write_Eol;
9050 else
9051 while Present (Id) loop
9052 Write_Entity_Info (Id, " ");
9053 Next_Entity (Id);
9054 end loop;
9055 end if;
9057 if Scope (Current_Scope) = Standard_Standard then
9059 -- Print information on the current unit itself
9061 Write_Entity_Info (Current_Scope, " ");
9062 end if;
9064 Write_Eol;
9065 end Write_Info;
9067 --------
9068 -- ws --
9069 --------
9071 procedure ws is
9072 S : Entity_Id;
9073 begin
9074 for J in reverse 1 .. Scope_Stack.Last loop
9075 S := Scope_Stack.Table (J).Entity;
9076 Write_Int (Int (S));
9077 Write_Str (" === ");
9078 Write_Name (Chars (S));
9079 Write_Eol;
9080 end loop;
9081 end ws;
9083 --------
9084 -- we --
9085 --------
9087 procedure we (S : Entity_Id) is
9088 E : Entity_Id;
9089 begin
9090 E := First_Entity (S);
9091 while Present (E) loop
9092 Write_Int (Int (E));
9093 Write_Str (" === ");
9094 Write_Name (Chars (E));
9095 Write_Eol;
9096 Next_Entity (E);
9097 end loop;
9098 end we;
9099 end Sem_Ch8;