Debian Codegraph @

gcc/ada/sem_ch8.adb

raw
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-2025, 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 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Einfo.Utils; use Einfo.Utils;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
35 with Freeze; use Freeze;
36 with Ghost; use Ghost;
37 with Impunit; use Impunit;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Local_Restrict;
42 with Mutably_Tagged; use Mutably_Tagged;
43 with Namet; use Namet;
44 with Namet.Sp; use Namet.Sp;
45 with Nlists; use Nlists;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Output; use Output;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch10; use Sem_Ch10;
59 with Sem_Ch12; use Sem_Ch12;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Prag; use Sem_Prag;
67 with Sem_Res; use Sem_Res;
68 with Sem_Util; use Sem_Util;
69 with Sem_Type; use Sem_Type;
70 with Stand; use Stand;
71 with Sinfo; use Sinfo;
72 with Sinfo.Nodes; use Sinfo.Nodes;
73 with Sinfo.Utils; use Sinfo.Utils;
74 with Sinfo.CN; use Sinfo.CN;
75 with Snames; use Snames;
76 with Style;
77 with Table;
78 with Tbuild; use Tbuild;
79 with Uintp; use Uintp;
80 with Warnsw; use Warnsw;
81
82 package body Sem_Ch8 is
83
84 ------------------------------------
85 -- Visibility and Name Resolution --
86 ------------------------------------
87
88 -- This package handles name resolution and the collection of possible
89 -- interpretations for overloaded names, prior to overload resolution.
90
91 -- Name resolution is the process that establishes a mapping between source
92 -- identifiers and the entities they denote at each point in the program.
93 -- Each entity is represented by a defining occurrence. Each identifier
94 -- that denotes an entity points to the corresponding defining occurrence.
95 -- This is the entity of the applied occurrence. Each occurrence holds
96 -- an index into the names table, where source identifiers are stored.
97
98 -- Each entry in the names table for an identifier or designator uses the
99 -- Info pointer to hold a link to the currently visible entity that has
100 -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
101 -- in package Sem_Util). The visibility is initialized at the beginning of
102 -- semantic processing to make entities in package Standard immediately
103 -- visible. The visibility table is used in a more subtle way when
104 -- compiling subunits (see below).
105
106 -- Entities that have the same name (i.e. homonyms) are chained. In the
107 -- case of overloaded entities, this chain holds all the possible meanings
108 -- of a given identifier. The process of overload resolution uses type
109 -- information to select from this chain the unique meaning of a given
110 -- identifier.
111
112 -- Entities are also chained in their scope, through the Next_Entity link.
113 -- As a consequence, the name space is organized as a sparse matrix, where
114 -- each row corresponds to a scope, and each column to a source identifier.
115 -- Open scopes, that is to say scopes currently being compiled, have their
116 -- corresponding rows of entities in order, innermost scope first.
117
118 -- The scopes of packages that are mentioned in context clauses appear in
119 -- no particular order, interspersed among open scopes. This is because
120 -- in the course of analyzing the context of a compilation, a package
121 -- declaration is first an open scope, and subsequently an element of the
122 -- context. If subunits or child units are present, a parent unit may
123 -- appear under various guises at various times in the compilation.
124
125 -- When the compilation of the innermost scope is complete, the entities
126 -- defined therein are no longer visible. If the scope is not a package
127 -- declaration, these entities are never visible subsequently, and can be
128 -- removed from visibility chains. If the scope is a package declaration,
129 -- its visible declarations may still be accessible. Therefore the entities
130 -- defined in such a scope are left on the visibility chains, and only
131 -- their visibility (immediately visibility or potential use-visibility)
132 -- is affected.
133
134 -- The ordering of homonyms on their chain does not necessarily follow
135 -- the order of their corresponding scopes on the scope stack. For
136 -- example, if package P and the enclosing scope both contain entities
137 -- named E, then when compiling the package body the chain for E will
138 -- hold the global entity first, and the local one (corresponding to
139 -- the current inner scope) next. As a result, name resolution routines
140 -- do not assume any relative ordering of the homonym chains, either
141 -- for scope nesting or to order of appearance of context clauses.
142
143 -- When compiling a child unit, entities in the parent scope are always
144 -- immediately visible. When compiling the body of a child unit, private
145 -- entities in the parent must also be made immediately visible. There
146 -- are separate routines to make the visible and private declarations
147 -- visible at various times (see package Sem_Ch7).
148
149 -- +--------+ +-----+
150 -- | In use |-------->| EU1 |-------------------------->
151 -- +--------+ +-----+
152 -- | |
153 -- +--------+ +-----+ +-----+
154 -- | Stand. |---------------->| ES1 |--------------->| ES2 |--->
155 -- +--------+ +-----+ +-----+
156 -- | |
157 -- +---------+ | +-----+
158 -- | with'ed |------------------------------>| EW2 |--->
159 -- +---------+ | +-----+
160 -- | |
161 -- +--------+ +-----+ +-----+
162 -- | Scope2 |---------------->| E12 |--------------->| E22 |--->
163 -- +--------+ +-----+ +-----+
164 -- | |
165 -- +--------+ +-----+ +-----+
166 -- | Scope1 |---------------->| E11 |--------------->| E12 |--->
167 -- +--------+ +-----+ +-----+
168 -- ^ | |
169 -- | | |
170 -- | +---------+ | |
171 -- | | with'ed |----------------------------------------->
172 -- | +---------+ | |
173 -- | | |
174 -- Scope stack | |
175 -- (innermost first) | |
176 -- +----------------------------+
177 -- Names table => | Id1 | | | | Id2 |
178 -- +----------------------------+
179
180 -- Name resolution must deal with several syntactic forms: simple names,
181 -- qualified names, indexed names, and various forms of calls.
182
183 -- Each identifier points to an entry in the names table. The resolution
184 -- of a simple name consists in traversing the homonym chain, starting
185 -- from the names table. If an entry is immediately visible, it is the one
186 -- designated by the identifier. If only potentially use-visible entities
187 -- are on the chain, we must verify that they do not hide each other. If
188 -- the entity we find is overloadable, we collect all other overloadable
189 -- entities on the chain as long as they are not hidden.
190 --
191 -- To resolve expanded names, we must find the entity at the intersection
192 -- of the entity chain for the scope (the prefix) and the homonym chain
193 -- for the selector. In general, homonym chains will be much shorter than
194 -- entity chains, so it is preferable to start from the names table as
195 -- well. If the entity found is overloadable, we must collect all other
196 -- interpretations that are defined in the scope denoted by the prefix.
197
198 -- For records, protected types, and tasks, their local entities are
199 -- removed from visibility chains on exit from the corresponding scope.
200 -- From the outside, these entities are always accessed by selected
201 -- notation, and the entity chain for the record type, protected type,
202 -- etc. is traversed sequentially in order to find the designated entity.
203
204 -- The discriminants of a type and the operations of a protected type or
205 -- task are unchained on exit from the first view of the type, (such as
206 -- a private or incomplete type declaration, or a protected type speci-
207 -- fication) and re-chained when compiling the second view.
208
209 -- In the case of operators, we do not make operators on derived types
210 -- explicit. As a result, the notation P."+" may denote either a user-
211 -- defined function with name "+", or else an implicit declaration of the
212 -- operator "+" in package P. The resolution of expanded names always
213 -- tries to resolve an operator name as such an implicitly defined entity,
214 -- in addition to looking for explicit declarations.
215
216 -- All forms of names that denote entities (simple names, expanded names,
217 -- character literals in some cases) have a Entity attribute, which
218 -- identifies the entity denoted by the name.
219
220 ---------------------
221 -- The Scope Stack --
222 ---------------------
223
224 -- The Scope stack keeps track of the scopes currently been compiled.
225 -- Every entity that contains declarations (including records) is placed
226 -- on the scope stack while it is being processed, and removed at the end.
227 -- Whenever a non-package scope is exited, the entities defined therein
228 -- are removed from the visibility table, so that entities in outer scopes
229 -- become visible (see previous description). On entry to Sem, the scope
230 -- stack only contains the package Standard. As usual, subunits complicate
231 -- this picture ever so slightly.
232
233 -- The Rtsfind mechanism can force a call to Semantics while another
234 -- compilation is in progress. The unit retrieved by Rtsfind must be
235 -- compiled in its own context, and has no access to the visibility of
236 -- the unit currently being compiled. The procedures Save_Scope_Stack and
237 -- Restore_Scope_Stack make entities in current open scopes invisible
238 -- before compiling the retrieved unit, and restore the compilation
239 -- environment afterwards.
240
241 ------------------------
242 -- Compiling subunits --
243 ------------------------
244
245 -- Subunits must be compiled in the environment of the corresponding stub,
246 -- that is to say with the same visibility into the parent (and its
247 -- context) that is available at the point of the stub declaration, but
248 -- with the additional visibility provided by the context clause of the
249 -- subunit itself. As a result, compilation of a subunit forces compilation
250 -- of the parent (see description in lib-). At the point of the stub
251 -- declaration, Analyze is called recursively to compile the proper body of
252 -- the subunit, but without reinitializing the names table, nor the scope
253 -- stack (i.e. standard is not pushed on the stack). In this fashion the
254 -- context of the subunit is added to the context of the parent, and the
255 -- subunit is compiled in the correct environment. Note that in the course
256 -- of processing the context of a subunit, Standard will appear twice on
257 -- the scope stack: once for the parent of the subunit, and once for the
258 -- unit in the context clause being compiled. However, the two sets of
259 -- entities are not linked by homonym chains, so that the compilation of
260 -- any context unit happens in a fresh visibility environment.
261
262 -------------------------------
263 -- Processing of USE Clauses --
264 -------------------------------
265
266 -- Every defining occurrence has a flag indicating if it is potentially use
267 -- visible. Resolution of simple names examines this flag. The processing
268 -- of use clauses consists in setting this flag on all visible entities
269 -- defined in the corresponding package. On exit from the scope of the use
270 -- clause, the corresponding flag must be reset. However, a package may
271 -- appear in several nested use clauses (pathological but legal, alas)
272 -- which forces us to use a slightly more involved scheme:
273
274 -- a) The defining occurrence for a package holds a flag -In_Use- to
275 -- indicate that it is currently in the scope of a use clause. If a
276 -- redundant use clause is encountered, then the corresponding occurrence
277 -- of the package name is flagged -Redundant_Use-.
278
279 -- b) On exit from a scope, the use clauses in its declarative part are
280 -- scanned. The visibility flag is reset in all entities declared in
281 -- package named in a use clause, as long as the package is not flagged
282 -- as being in a redundant use clause (in which case the outer use
283 -- clause is still in effect, and the direct visibility of its entities
284 -- must be retained).
285
286 -- Note that entities are not removed from their homonym chains on exit
287 -- from the package specification. A subsequent use clause does not need
288 -- to rechain the visible entities, but only to establish their direct
289 -- visibility.
290
291 -----------------------------------
292 -- Handling private declarations --
293 -----------------------------------
294
295 -- The principle that each entity has a single defining occurrence clashes
296 -- with the presence of two separate definitions for private types: the
297 -- first is the private type declaration, and second is the full type
298 -- declaration. It is important that all references to the type point to
299 -- the same defining occurrence, namely the first one. To enforce the two
300 -- separate views of the entity, the corresponding information is swapped
301 -- between the two declarations. Outside of the package, the defining
302 -- occurrence only contains the private declaration information, while in
303 -- the private part and the body of the package the defining occurrence
304 -- contains the full declaration. To simplify the swap, the defining
305 -- occurrence that currently holds the private declaration points to the
306 -- full declaration. During semantic processing the defining occurrence
307 -- also points to a list of private dependents, that is to say access types
308 -- or composite types whose designated types or component types are
309 -- subtypes or derived types of the private type in question. After the
310 -- full declaration has been seen, the private dependents are updated to
311 -- indicate that they have full definitions.
312
313 ------------------------------------
314 -- Handling of Undefined Messages --
315 ------------------------------------
316
317 -- In normal mode, only the first use of an undefined identifier generates
318 -- a message. The table Urefs is used to record error messages that have
319 -- been issued so that second and subsequent ones do not generate further
320 -- messages. However, the second reference causes text to be added to the
321 -- original undefined message noting "(more references follow)". The
322 -- full error list option (-gnatf) forces messages to be generated for
323 -- every reference and disconnects the use of this table.
324
325 type Uref_Entry is record
326 Node : Node_Id;
327 -- Node for identifier for which original message was posted. The
328 -- Chars field of this identifier is used to detect later references
329 -- to the same identifier.
330
331 Err : Error_Msg_Id;
332 -- Records error message Id of original undefined message. Reset to
333 -- No_Error_Msg after the second occurrence, where it is used to add
334 -- text to the original message as described above.
335
336 Nvis : Boolean;
337 -- Set if the message is not visible rather than undefined
338
339 Loc : Source_Ptr;
340 -- Records location of error message. Used to make sure that we do
341 -- not consider a, b : undefined as two separate instances, which
342 -- would otherwise happen, since the parser converts this sequence
343 -- to a : undefined; b : undefined.
344
345 end record;
346
347 package Urefs is new Table.Table (
348 Table_Component_Type => Uref_Entry,
349 Table_Index_Type => Nat,
350 Table_Low_Bound => 1,
351 Table_Initial => 10,
352 Table_Increment => 100,
353 Table_Name => "Urefs");
354
355 Candidate_Renaming : Entity_Id;
356 -- Holds a candidate interpretation that appears in a subprogram renaming
357 -- declaration and does not match the given specification, but matches at
358 -- least on the first formal. Allows better error message when given
359 -- specification omits defaulted parameters, a common error.
360
361 -----------------------
362 -- Local Subprograms --
363 -----------------------
364
365 procedure Analyze_Generic_Renaming
366 (N : Node_Id;
367 K : Entity_Kind);
368 -- Common processing for all three kinds of generic renaming declarations.
369 -- Enter new name and indicate that it renames the generic unit.
370
371 procedure Analyze_Renamed_Character
372 (N : Node_Id;
373 New_S : Entity_Id;
374 Is_Body : Boolean);
375 -- Renamed entity is given by a character literal, which must belong
376 -- to the return type of the new entity. Is_Body indicates whether the
377 -- declaration is a renaming_as_body. If the original declaration has
378 -- already been frozen (because of an intervening body, e.g.) the body of
379 -- the function must be built now. The same applies to the following
380 -- various renaming procedures.
381
382 procedure Analyze_Renamed_Dereference
383 (N : Node_Id;
384 New_S : Entity_Id;
385 Is_Body : Boolean);
386 -- Renamed entity is given by an explicit dereference. Prefix must be a
387 -- conformant access_to_subprogram type.
388
389 procedure Analyze_Renamed_Entry
390 (N : Node_Id;
391 New_S : Entity_Id;
392 Is_Body : Boolean);
393 -- If the renamed entity in a subprogram renaming is an entry or protected
394 -- subprogram, build a body for the new entity whose only statement is a
395 -- call to the renamed entity.
396
397 procedure Analyze_Renamed_Family_Member
398 (N : Node_Id;
399 New_S : Entity_Id;
400 Is_Body : Boolean);
401 -- Used when the renamed entity is an indexed component. The prefix must
402 -- denote an entry family.
403
404 procedure Analyze_Renamed_Primitive_Operation
405 (N : Node_Id;
406 New_S : Entity_Id;
407 Is_Body : Boolean);
408 -- If the renamed entity in a subprogram renaming is a primitive operation
409 -- or a class-wide operation in prefix form, save the target object,
410 -- which must be added to the list of actuals in any subsequent call.
411 -- The renaming operation is intrinsic because the compiler must in
412 -- fact generate a wrapper for it (6.3.1 (10 1/2)).
413
414 procedure Attribute_Renaming (N : Node_Id);
415 -- Analyze renaming of attribute as subprogram. The renaming declaration N
416 -- is rewritten as a subprogram body that returns the attribute reference
417 -- applied to the formals of the function.
418
419 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
420 -- Set Entity, with style check if need be. For a discriminant reference,
421 -- replace by the corresponding discriminal, i.e. the parameter of the
422 -- initialization procedure that corresponds to the discriminant.
423
424 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
425 -- A renaming_as_body may occur after the entity of the original decla-
426 -- ration has been frozen. In that case, the body of the new entity must
427 -- be built now, because the usual mechanism of building the renamed
428 -- body at the point of freezing will not work. Subp is the subprogram
429 -- for which N provides the Renaming_As_Body.
430
431 procedure Check_In_Previous_With_Clause (N, Nam : Node_Id);
432 -- N is a use_package clause and Nam the package name, or N is a use_type
433 -- clause and Nam is the prefix of the type name. In either case, verify
434 -- that the package is visible at that point in the context: either it
435 -- appears in a previous with_clause, or because it is a fully qualified
436 -- name and the root ancestor appears in a previous with_clause.
437
438 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
439 -- Verify that the entity in a renaming declaration that is a library unit
440 -- is itself a library unit and not a nested unit or subunit. Also check
441 -- that if the renaming is a child unit of a generic parent, then the
442 -- renamed unit must also be a child unit of that parent. Finally, verify
443 -- that a renamed generic unit is not an implicit child declared within
444 -- an instance of the parent.
445
446 procedure Chain_Use_Clause (N : Node_Id);
447 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
448 -- the proper scope table entry. This is usually the current scope, but it
449 -- will be an inner scope when installing the use clauses of the private
450 -- declarations of a parent unit prior to compiling the private part of a
451 -- child unit. This chain is traversed when installing/removing use clauses
452 -- when compiling a subunit or instantiating a generic body on the fly,
453 -- when it is necessary to save and restore full environments.
454
455 function Enclosing_Instance return Entity_Id;
456 -- In an instance nested within another one, several semantic checks are
457 -- unnecessary because the legality of the nested instance has been checked
458 -- in the enclosing generic unit. This applies in particular to legality
459 -- checks on actuals for formal subprograms of the inner instance, which
460 -- are checked as subprogram renamings, and may be complicated by confusion
461 -- in private/full views. This function returns the instance enclosing the
462 -- current one if there is such, else it returns Empty.
463 --
464 -- If the renaming determines the entity for the default of a formal
465 -- subprogram nested within another instance, choose the innermost
466 -- candidate. This is because if the formal has a box, and we are within
467 -- an enclosing instance where some candidate interpretations are local
468 -- to this enclosing instance, we know that the default was properly
469 -- resolved when analyzing the generic, so we prefer the local
470 -- candidates to those that are external. This is not always the case
471 -- but is a reasonable heuristic on the use of nested generics. The
472 -- proper solution requires a full renaming model.
473
474 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
475 -- Return the appropriate entity for determining which unit has a deeper
476 -- scope: the defining entity for U, unless U is a package instance, in
477 -- which case we retrieve the entity of the instance spec.
478
479 procedure Error_Missing_With_Of_Known_Unit (Pkg : Node_Id);
480 -- Display an error message denoting a "with" is missing for a given known
481 -- package Pkg with its full path name.
482
483 procedure Find_Expanded_Name (N : Node_Id);
484 -- The input is a selected component known to be an expanded name. Verify
485 -- legality of selector given the scope denoted by prefix, and change node
486 -- N into a expanded name with a properly set Entity field.
487
488 function Find_First_Use (Use_Clause : Node_Id) return Node_Id;
489 -- Find the most previous use clause (that is, the first one to appear in
490 -- the source) by traversing the previous clause chain that exists in both
491 -- N_Use_Package_Clause nodes and N_Use_Type_Clause nodes.
492
493 function Find_Renamed_Entity
494 (N : Node_Id;
495 Nam : Node_Id;
496 New_S : Entity_Id;
497 Is_Actual : Boolean := False) return Entity_Id;
498 -- Find the renamed entity that corresponds to the given parameter profile
499 -- in a subprogram renaming declaration. The renamed entity may be an
500 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
501 -- indicates that the renaming is the one generated for an actual subpro-
502 -- gram in an instance, for which special visibility checks apply.
503
504 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
505 -- Find a type derived from Character or Wide_Character in the prefix of N.
506 -- Used to resolved qualified names whose selector is a character literal.
507
508 function Has_Private_With (E : Entity_Id) return Boolean;
509 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
510 -- private with on E.
511
512 function Has_Components (Typ : Entity_Id) return Boolean;
513 -- Determine if given type has components, i.e. is either a record type or
514 -- type or a type that has discriminants.
515
516 function Has_Implicit_Operator (N : Node_Id) return Boolean;
517 -- N is an expanded name whose selector is an operator name (e.g. P."+").
518 -- Determine if N denotes an operator implicitly declared in prefix P: P's
519 -- declarative part contains an implicit declaration of an operator if it
520 -- has a declaration of a type to which one of the predefined operators
521 -- apply. The existence of this routine is an implementation artifact. A
522 -- more straightforward but more space-consuming choice would be to make
523 -- all inherited operators explicit in the symbol table.
524
525 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
526 -- A subprogram defined by a renaming declaration inherits the parameter
527 -- profile of the renamed entity. The subtypes given in the subprogram
528 -- specification are discarded and replaced with those of the renamed
529 -- subprogram, which are then used to recheck the default values.
530
531 function Most_Descendant_Use_Clause
532 (Clause1 : Entity_Id;
533 Clause2 : Entity_Id) return Entity_Id;
534 -- Determine which use clause parameter is the most descendant in terms of
535 -- scope.
536
537 procedure Premature_Usage (N : Node_Id);
538 -- Diagnose usage of an entity before it is visible
539
540 function Is_Self_Hidden (E : Entity_Id) return Boolean;
541 -- True within a declaration if it is hidden from all visibility by itself
542 -- (see RM-8.3(16-18)). This is mostly just "not Is_Not_Self_Hidden", but
543 -- we need to check for E_Void in case of errors.
544
545 procedure Use_One_Package
546 (N : Node_Id;
547 Pack_Name : Entity_Id := Empty;
548 Force : Boolean := False);
549 -- Make visible entities declared in package P potentially use-visible
550 -- in the current context. Also used in the analysis of subunits, when
551 -- re-installing use clauses of parent units. N is the use_clause that
552 -- names P (and possibly other packages).
553
554 procedure Use_One_Type
555 (Id : Node_Id;
556 Installed : Boolean := False;
557 Force : Boolean := False);
558 -- Id is the subtype mark from a use_type_clause. This procedure makes
559 -- the primitive operators of the type potentially use-visible. The
560 -- boolean flag Installed indicates that the clause is being reinstalled
561 -- after previous analysis, and primitive operations are already chained
562 -- on the Used_Operations list of the clause.
563
564 procedure Write_Info;
565 -- Write debugging information on entities declared in current scope
566
567 --------------------------------
568 -- Analyze_Exception_Renaming --
569 --------------------------------
570
571 -- The language only allows a single identifier, but the tree holds an
572 -- identifier list. The parser has already issued an error message if
573 -- there is more than one element in the list.
574
575 procedure Analyze_Exception_Renaming (N : Node_Id) is
576 Id : constant Entity_Id := Defining_Entity (N);
577 Nam : constant Node_Id := Name (N);
578
579 begin
580 Enter_Name (Id);
581 Analyze (Nam);
582
583 Mutate_Ekind (Id, E_Exception);
584 Set_Etype (Id, Standard_Exception_Type);
585 Set_Is_Pure (Id, Is_Pure (Current_Scope));
586
587 if Is_Entity_Name (Nam)
588 and then Present (Entity (Nam))
589 and then Ekind (Entity (Nam)) = E_Exception
590 then
591 if Present (Renamed_Entity (Entity (Nam))) then
592 Set_Renamed_Entity (Id, Renamed_Entity (Entity (Nam)));
593 else
594 Set_Renamed_Entity (Id, Entity (Nam));
595 end if;
596
597 -- The exception renaming declaration may become Ghost if it renames
598 -- a Ghost entity.
599
600 Mark_Ghost_Renaming (N, Entity (Nam));
601 else
602 Error_Msg_N ("invalid exception name in renaming", Nam);
603 end if;
604
605 -- Implementation-defined aspect specifications can appear in a renaming
606 -- declaration, but not language-defined ones. The call to procedure
607 -- Analyze_Aspect_Specifications will take care of this error check.
608
609 Analyze_Aspect_Specifications (N, Id);
610 end Analyze_Exception_Renaming;
611
612 ---------------------------
613 -- Analyze_Expanded_Name --
614 ---------------------------
615
616 procedure Analyze_Expanded_Name (N : Node_Id) is
617 begin
618 -- If the entity pointer is already set, this is an internal node, or a
619 -- node that is analyzed more than once, after a tree modification. In
620 -- such a case there is no resolution to perform, just set the type. In
621 -- either case, start by analyzing the prefix.
622
623 Analyze (Prefix (N));
624
625 if Present (Entity (N)) then
626 if Is_Type (Entity (N)) then
627 Set_Etype (N, Entity (N));
628 else
629 Set_Etype (N, Etype (Entity (N)));
630 end if;
631
632 else
633 Find_Expanded_Name (N);
634 end if;
635
636 -- In either case, propagate dimension of entity to expanded name
637
638 Analyze_Dimension (N);
639 end Analyze_Expanded_Name;
640
641 ---------------------------------------
642 -- Analyze_Generic_Function_Renaming --
643 ---------------------------------------
644
645 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
646 begin
647 Analyze_Generic_Renaming (N, E_Generic_Function);
648 end Analyze_Generic_Function_Renaming;
649
650 --------------------------------------
651 -- Analyze_Generic_Package_Renaming --
652 --------------------------------------
653
654 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
655 begin
656 -- Test for the Text_IO special unit case here, since we may be renaming
657 -- one of the subpackages of Text_IO, then join common routine.
658
659 Check_Text_IO_Special_Unit (Name (N));
660
661 Analyze_Generic_Renaming (N, E_Generic_Package);
662 end Analyze_Generic_Package_Renaming;
663
664 ----------------------------------------
665 -- Analyze_Generic_Procedure_Renaming --
666 ----------------------------------------
667
668 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
669 begin
670 Analyze_Generic_Renaming (N, E_Generic_Procedure);
671 end Analyze_Generic_Procedure_Renaming;
672
673 ------------------------------
674 -- Analyze_Generic_Renaming --
675 ------------------------------
676
677 procedure Analyze_Generic_Renaming
678 (N : Node_Id;
679 K : Entity_Kind)
680 is
681 New_P : constant Entity_Id := Defining_Entity (N);
682 Inst : Boolean := False;
683 Old_P : Entity_Id;
684
685 begin
686 if Name (N) = Error then
687 return;
688 end if;
689
690 Generate_Definition (New_P);
691
692 if Current_Scope /= Standard_Standard then
693 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
694 end if;
695
696 if Nkind (Name (N)) = N_Selected_Component then
697 Check_Generic_Child_Unit (Name (N), Inst);
698 else
699 Analyze (Name (N));
700 end if;
701
702 if not Is_Entity_Name (Name (N)) then
703 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
704 Old_P := Any_Id;
705 else
706 Old_P := Entity (Name (N));
707 end if;
708
709 Enter_Name (New_P);
710 Mutate_Ekind (New_P, K);
711
712 if Etype (Old_P) = Any_Type then
713 null;
714
715 elsif Ekind (Old_P) /= K then
716 Error_Msg_N ("invalid generic unit name", Name (N));
717
718 else
719 if Present (Renamed_Entity (Old_P)) then
720 Set_Renamed_Entity (New_P, Renamed_Entity (Old_P));
721 else
722 Set_Renamed_Entity (New_P, Old_P);
723 end if;
724
725 -- The generic renaming declaration may become Ghost if it renames a
726 -- Ghost entity.
727
728 Mark_Ghost_Renaming (N, Old_P);
729
730 Set_Is_Pure (New_P, Is_Pure (Old_P));
731 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
732
733 Set_Etype (New_P, Etype (Old_P));
734 Set_Has_Completion (New_P);
735
736 if In_Open_Scopes (Old_P) then
737 Error_Msg_N ("within its scope, generic denotes its instance", N);
738 end if;
739
740 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
741 -- renamings and subsequent instantiations of Unchecked_Conversion.
742
743 if Is_Generic_Subprogram (Old_P) then
744 Set_Is_Intrinsic_Subprogram
745 (New_P, Is_Intrinsic_Subprogram (Old_P));
746 end if;
747
748 Check_Library_Unit_Renaming (N, Old_P);
749 end if;
750
751 -- Implementation-defined aspect specifications can appear in a renaming
752 -- declaration, but not language-defined ones. The call to procedure
753 -- Analyze_Aspect_Specifications will take care of this error check.
754
755 Analyze_Aspect_Specifications (N, New_P);
756 end Analyze_Generic_Renaming;
757
758 -----------------------------
759 -- Analyze_Object_Renaming --
760 -----------------------------
761
762 procedure Analyze_Object_Renaming (N : Node_Id) is
763 Id : constant Entity_Id := Defining_Identifier (N);
764 Loc : constant Source_Ptr := Sloc (N);
765 Nam : constant Node_Id := Name (N);
766 Is_Object_Ref : Boolean;
767 Dec : Node_Id;
768 T : Entity_Id;
769 T2 : Entity_Id;
770 Q : Node_Id;
771
772 procedure Check_Constrained_Object;
773 -- If the nominal type is unconstrained but the renamed object is
774 -- constrained, as can happen with renaming an explicit dereference or
775 -- a function return, build a constrained subtype from the object. If
776 -- the renaming is for a formal in an accept statement, the analysis
777 -- has already established its actual subtype. This is only relevant
778 -- if the renamed object is an explicit dereference.
779
780 function Get_Object_Name (Nod : Node_Id) return Node_Id;
781 -- Obtain the name of the object from node Nod which is being renamed by
782 -- the object renaming declaration N.
783
784 function Find_Raise_Node (N : Node_Id) return Traverse_Result;
785 -- Process one node in search for N_Raise_xxx_Error nodes.
786 -- Return Abandon if found, OK otherwise.
787
788 ---------------------
789 -- Find_Raise_Node --
790 ---------------------
791
792 function Find_Raise_Node (N : Node_Id) return Traverse_Result is
793 begin
794 if Nkind (N) in N_Raise_xxx_Error then
795 return Abandon;
796 else
797 return OK;
798 end if;
799 end Find_Raise_Node;
800
801 ------------------------
802 -- No_Raise_xxx_Error --
803 ------------------------
804
805 function No_Raise_xxx_Error is new Traverse_Func (Find_Raise_Node);
806 -- Traverse tree to look for a N_Raise_xxx_Error node and returns
807 -- Abandon if so and OK if none found.
808
809 ------------------------------
810 -- Check_Constrained_Object --
811 ------------------------------
812
813 procedure Check_Constrained_Object is
814 Typ : constant Entity_Id := Etype (Nam);
815 Subt : Entity_Id;
816 Loop_Scheme : Node_Id;
817
818 begin
819 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference
820 and then Is_Composite_Type (Typ)
821 and then not Is_Constrained (Typ)
822 and then not Has_Unknown_Discriminants (Typ)
823 and then Expander_Active
824 then
825 -- If Actual_Subtype is already set, nothing to do
826
827 if Ekind (Id) in E_Variable | E_Constant
828 and then Present (Actual_Subtype (Id))
829 then
830 null;
831
832 -- A renaming of an unchecked union has no actual subtype
833
834 elsif Is_Unchecked_Union (Typ) then
835 null;
836
837 -- If a record is limited its size is invariant. This is the case
838 -- in particular with record types with an access discriminant
839 -- that are used in iterators. This is an optimization, but it
840 -- also prevents typing anomalies when the prefix is further
841 -- expanded.
842
843 -- Note that we cannot just use the Is_Limited_Record flag because
844 -- it does not apply to records with limited components, for which
845 -- this syntactic flag is not set, but whose size is also fixed.
846
847 -- Note also that we need to build the constrained subtype for an
848 -- array in order to make the bounds explicit in most cases, but
849 -- not if the object comes from an extended return statement, as
850 -- this would create dangling references to them later on.
851
852 elsif Is_Limited_Type (Typ)
853 and then (not Is_Array_Type (Typ) or else Is_Return_Object (Id))
854 then
855 null;
856
857 else
858 Subt := Make_Temporary (Loc, 'T');
859 Remove_Side_Effects (Nam);
860 Insert_Action (N,
861 Make_Subtype_Declaration (Loc,
862 Defining_Identifier => Subt,
863 Subtype_Indication =>
864 Make_Subtype_From_Expr (Nam, Typ)));
865
866 declare
867 New_Subtype_Mark : constant Node_Id :=
868 New_Occurrence_Of (Subt, Loc);
869 begin
870 if Present (Subtype_Mark (N)) then
871 Rewrite (Subtype_Mark (N), New_Subtype_Mark);
872 else
873 -- An Ada2022 renaming with no subtype mark
874 Set_Subtype_Mark (N, New_Subtype_Mark);
875 end if;
876 end;
877
878 Set_Etype (Nam, Subt);
879
880 -- Suppress discriminant checks on this subtype if the original
881 -- type has defaulted discriminants and Id is a "for of" loop
882 -- iterator.
883
884 if Has_Defaulted_Discriminants (Typ)
885 and then Nkind (Original_Node (Parent (N))) = N_Loop_Statement
886 then
887 Loop_Scheme := Iteration_Scheme (Original_Node (Parent (N)));
888
889 if Present (Loop_Scheme)
890 and then Present (Iterator_Specification (Loop_Scheme))
891 and then
892 Defining_Identifier
893 (Iterator_Specification (Loop_Scheme)) = Id
894 then
895 Set_Checks_May_Be_Suppressed (Subt);
896 Push_Local_Suppress_Stack_Entry
897 (Entity => Subt,
898 Check => Discriminant_Check,
899 Suppress => True);
900 end if;
901 end if;
902
903 -- Freeze subtype at once, to prevent order of elaboration
904 -- issues in the backend. The renamed object exists, so its
905 -- type is already frozen in any case.
906
907 Freeze_Before (N, Subt);
908 end if;
909 end if;
910 end Check_Constrained_Object;
911
912 ---------------------
913 -- Get_Object_Name --
914 ---------------------
915
916 function Get_Object_Name (Nod : Node_Id) return Node_Id is
917 Obj_Nam : Node_Id;
918
919 begin
920 Obj_Nam := Nod;
921 while Present (Obj_Nam) loop
922 case Nkind (Obj_Nam) is
923 when N_Attribute_Reference
924 | N_Explicit_Dereference
925 | N_Indexed_Component
926 | N_Slice
927 =>
928 Obj_Nam := Prefix (Obj_Nam);
929
930 when N_Selected_Component =>
931 Obj_Nam := Selector_Name (Obj_Nam);
932
933 when N_Qualified_Expression | N_Type_Conversion =>
934 Obj_Nam := Expression (Obj_Nam);
935
936 when others =>
937 exit;
938 end case;
939 end loop;
940
941 return Obj_Nam;
942 end Get_Object_Name;
943
944 -- Start of processing for Analyze_Object_Renaming
945
946 begin
947 if Nam = Error then
948 return;
949 end if;
950
951 Set_Is_Pure (Id, Is_Pure (Current_Scope));
952 Enter_Name (Id);
953
954 -- The renaming of a component that depends on a discriminant requires
955 -- an actual subtype, because in subsequent use of the object Gigi will
956 -- be unable to locate the actual bounds. This explicit step is required
957 -- when the renaming is generated in removing side effects of an
958 -- already-analyzed expression.
959
960 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
961
962 -- The object renaming declaration may become Ghost if it renames a
963 -- Ghost entity.
964
965 if Is_Entity_Name (Nam) then
966 Mark_Ghost_Renaming (N, Entity (Nam));
967 end if;
968
969 T := Etype (Nam);
970 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
971
972 if Present (Dec) then
973 Insert_Action (N, Dec);
974 T := Defining_Identifier (Dec);
975 Set_Etype (Nam, T);
976 end if;
977 elsif Present (Subtype_Mark (N))
978 or else No (Access_Definition (N))
979 then
980 if Present (Subtype_Mark (N)) then
981 Find_Type (Subtype_Mark (N));
982 T := Entity (Subtype_Mark (N));
983 Analyze (Nam);
984
985 -- AI12-0275: Case of object renaming without a subtype_mark
986
987 else
988 Analyze (Nam);
989
990 -- Normal case of no overloading in object name
991
992 if not Is_Overloaded (Nam) then
993
994 -- Catch error cases (such as attempting to rename a procedure
995 -- or package) using the shorthand form.
996
997 if No (Etype (Nam))
998 or else Etype (Nam) = Standard_Void_Type
999 then
1000 Error_Msg_N
1001 ("object name or value expected in renaming", Nam);
1002
1003 Mutate_Ekind (Id, E_Variable);
1004 Set_Etype (Id, Any_Type);
1005
1006 return;
1007
1008 else
1009 T := Etype (Nam);
1010 end if;
1011
1012 -- Case of overloaded name, which will be illegal if there's more
1013 -- than one acceptable interpretation (such as overloaded function
1014 -- calls).
1015
1016 else
1017 declare
1018 I : Interp_Index;
1019 I1 : Interp_Index;
1020 It : Interp;
1021 It1 : Interp;
1022 Nam1 : Entity_Id;
1023
1024 begin
1025 -- More than one candidate interpretation is available
1026
1027 -- Remove procedure calls, which syntactically cannot appear
1028 -- in this context, but which cannot be removed by type
1029 -- checking, because the context does not impose a type.
1030
1031 Get_First_Interp (Nam, I, It);
1032 while Present (It.Typ) loop
1033 if It.Typ = Standard_Void_Type then
1034 Remove_Interp (I);
1035 end if;
1036
1037 Get_Next_Interp (I, It);
1038 end loop;
1039
1040 Get_First_Interp (Nam, I, It);
1041 I1 := I;
1042 It1 := It;
1043
1044 -- If there's no type present, we have an error case (such
1045 -- as overloaded procedures named in the object renaming).
1046
1047 if No (It.Typ) then
1048 Error_Msg_N
1049 ("object name or value expected in renaming", Nam);
1050
1051 Mutate_Ekind (Id, E_Variable);
1052 Set_Etype (Id, Any_Type);
1053
1054 return;
1055 end if;
1056
1057 Get_Next_Interp (I, It);
1058
1059 if Present (It.Typ) then
1060 Nam1 := It1.Nam;
1061 It1 := Disambiguate (Nam, I1, I, Any_Type);
1062
1063 if It1 = No_Interp then
1064 Error_Msg_N ("ambiguous name in object renaming", Nam);
1065
1066 Error_Msg_Sloc := Sloc (It.Nam);
1067 Error_Msg_N ("\\possible interpretation#!", Nam);
1068
1069 Error_Msg_Sloc := Sloc (Nam1);
1070 Error_Msg_N ("\\possible interpretation#!", Nam);
1071
1072 return;
1073 end if;
1074 end if;
1075
1076 Set_Etype (Nam, It1.Typ);
1077 T := It1.Typ;
1078 end;
1079 end if;
1080
1081 if Etype (Nam) = Standard_Exception_Type then
1082 Error_Msg_N
1083 ("exception requires a subtype mark in renaming", Nam);
1084 return;
1085 end if;
1086 end if;
1087
1088 -- The object renaming declaration may become Ghost if it renames a
1089 -- Ghost entity.
1090
1091 if Is_Entity_Name (Nam) then
1092 Mark_Ghost_Renaming (N, Entity (Nam));
1093 end if;
1094
1095 -- Check against AI12-0401 here before Resolve may rewrite Nam and
1096 -- potentially generate spurious warnings.
1097
1098 -- In the case where the object_name is a qualified_expression with
1099 -- a nominal subtype T and whose expression is a name that denotes
1100 -- an object Q:
1101 -- * if T is an elementary subtype, then:
1102 -- * Q shall be a constant other than a dereference of an access
1103 -- type; or
1104 -- * the nominal subtype of Q shall be statically compatible with
1105 -- T; or
1106 -- * T shall statically match the base subtype of its type if
1107 -- scalar, or the first subtype of its type if an access type.
1108 -- * if T is a composite subtype, then Q shall be known to be
1109 -- constrained or T shall statically match the first subtype of
1110 -- its type.
1111
1112 if Nkind (Nam) = N_Qualified_Expression
1113 and then Is_Object_Reference (Expression (Nam))
1114 then
1115 Q := Expression (Nam);
1116
1117 if (Is_Elementary_Type (T)
1118 and then
1119 not ((not Is_Variable (Q)
1120 and then Nkind (Q) /= N_Explicit_Dereference)
1121 or else Subtypes_Statically_Compatible (Etype (Q), T)
1122 or else (Is_Scalar_Type (T)
1123 and then Subtypes_Statically_Match
1124 (T, Base_Type (T)))
1125 or else (Is_Access_Type (T)
1126 and then Subtypes_Statically_Match
1127 (T, First_Subtype (T)))))
1128 or else (Is_Composite_Type (T)
1129 and then
1130
1131 -- If Q is an aggregate, Is_Constrained may not be set
1132 -- yet and its type may not be resolved yet.
1133 -- This doesn't quite correspond to the complex notion
1134 -- of "known to be constrained" but this is good enough
1135 -- for a rule which is in any case too complex.
1136
1137 not (Is_Constrained (Etype (Q))
1138 or else Nkind (Q) = N_Aggregate
1139 or else Subtypes_Statically_Match
1140 (T, First_Subtype (T))))
1141 then
1142 Error_Msg_N
1143 ("subtype of renamed qualified expression does not " &
1144 "statically match", N);
1145 return;
1146 end if;
1147 end if;
1148
1149 Resolve (Nam, T);
1150
1151 -- If the renamed object is a function call of a limited type,
1152 -- the expansion of the renaming is complicated by the presence
1153 -- of various temporaries and subtypes that capture constraints
1154 -- of the renamed object. Rewrite node as an object declaration,
1155 -- whose expansion is simpler. Given that the object is limited
1156 -- there is no copy involved and no performance hit.
1157
1158 if Nkind (Nam) = N_Function_Call
1159 and then Is_Inherently_Limited_Type (Etype (Nam))
1160 and then not Is_Constrained (Etype (Nam))
1161 and then Comes_From_Source (N)
1162 then
1163 Set_Etype (Id, T);
1164 Mutate_Ekind (Id, E_Constant);
1165 Rewrite (N,
1166 Make_Object_Declaration (Loc,
1167 Defining_Identifier => Id,
1168 Constant_Present => True,
1169 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
1170 Expression => Relocate_Node (Nam)));
1171 return;
1172 end if;
1173
1174 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
1175 -- when renaming declaration has a named access type. The Ada 2012
1176 -- coverage rules allow an anonymous access type in the context of
1177 -- an expected named general access type, but the renaming rules
1178 -- require the types to be the same. (An exception is when the type
1179 -- of the renaming is also an anonymous access type, which can only
1180 -- happen due to a renaming created by the expander.)
1181
1182 if Nkind (Nam) = N_Type_Conversion
1183 and then not Comes_From_Source (Nam)
1184 and then Is_Anonymous_Access_Type (Etype (Expression (Nam)))
1185 and then not Is_Anonymous_Access_Type (T)
1186 then
1187 Error_Msg_NE
1188 ("cannot rename anonymous access object "
1189 & "as a named access type", Expression (Nam), T);
1190 end if;
1191
1192 -- Check that a class-wide object is not being renamed as an object
1193 -- of a specific type. The test for access types is needed to exclude
1194 -- cases where the renamed object is a dynamically tagged access
1195 -- result, such as occurs in certain expansions.
1196
1197 if Is_Tagged_Type (T) then
1198 Check_Dynamically_Tagged_Expression
1199 (Expr => Nam,
1200 Typ => T,
1201 Related_Nod => N);
1202 end if;
1203
1204 -- Ada 2005 (AI-230/AI-254): Access renaming
1205
1206 else pragma Assert (Present (Access_Definition (N)));
1207 T :=
1208 Access_Definition
1209 (Related_Nod => N,
1210 N => Access_Definition (N));
1211
1212 Analyze (Nam);
1213
1214 -- The object renaming declaration may become Ghost if it renames a
1215 -- Ghost entity.
1216
1217 if Is_Entity_Name (Nam) then
1218 Mark_Ghost_Renaming (N, Entity (Nam));
1219 end if;
1220
1221 -- Ada 2005 AI05-105: if the declaration has an anonymous access
1222 -- type, the renamed object must also have an anonymous type, and
1223 -- this is a name resolution rule. This was implicit in the last part
1224 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
1225 -- recent AI.
1226
1227 if not Is_Overloaded (Nam) then
1228 if Ekind (Etype (Nam)) /= Ekind (T) then
1229 Error_Msg_N
1230 ("expect anonymous access type in object renaming", N);
1231 end if;
1232
1233 else
1234 declare
1235 I : Interp_Index;
1236 It : Interp;
1237 Typ : Entity_Id := Empty;
1238 Seen : Boolean := False;
1239
1240 begin
1241 Get_First_Interp (Nam, I, It);
1242 while Present (It.Typ) loop
1243
1244 -- Renaming is ambiguous if more than one candidate
1245 -- interpretation is type-conformant with the context.
1246
1247 if Ekind (It.Typ) = Ekind (T) then
1248 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
1249 and then
1250 Type_Conformant
1251 (Designated_Type (T), Designated_Type (It.Typ))
1252 then
1253 if not Seen then
1254 Seen := True;
1255 else
1256 Error_Msg_N
1257 ("ambiguous expression in renaming", Nam);
1258 end if;
1259
1260 elsif Ekind (T) = E_Anonymous_Access_Type
1261 and then
1262 Covers (Designated_Type (T), Designated_Type (It.Typ))
1263 then
1264 if not Seen then
1265 Seen := True;
1266 else
1267 Error_Msg_N
1268 ("ambiguous expression in renaming", Nam);
1269 end if;
1270 end if;
1271
1272 if Covers (T, It.Typ) then
1273 Typ := It.Typ;
1274 Set_Etype (Nam, Typ);
1275 Set_Is_Overloaded (Nam, False);
1276 end if;
1277 end if;
1278
1279 Get_Next_Interp (I, It);
1280 end loop;
1281 end;
1282 end if;
1283
1284 Resolve (Nam, T);
1285
1286 -- Do not perform the legality checks below when the resolution of
1287 -- the renaming name failed because the associated type is Any_Type.
1288
1289 if Etype (Nam) = Any_Type then
1290 null;
1291
1292 -- Ada 2005 (AI-231): In the case where the type is defined by an
1293 -- access_definition, the renamed entity shall be of an access-to-
1294 -- constant type if and only if the access_definition defines an
1295 -- access-to-constant type. ARM 8.5.1(4)
1296
1297 elsif Constant_Present (Access_Definition (N))
1298 and then not Is_Access_Constant (Etype (Nam))
1299 then
1300 Error_Msg_N
1301 ("(Ada 2005): the renamed object is not access-to-constant "
1302 & "(RM 8.5.1(6))", N);
1303
1304 elsif not Constant_Present (Access_Definition (N))
1305 and then Is_Access_Constant (Etype (Nam))
1306 then
1307 Error_Msg_N
1308 ("(Ada 2005): the renamed object is not access-to-variable "
1309 & "(RM 8.5.1(6))", N);
1310 end if;
1311
1312 if Is_Access_Subprogram_Type (Etype (Nam)) then
1313 Check_Subtype_Conformant
1314 (Designated_Type (T), Designated_Type (Etype (Nam)));
1315
1316 elsif not Subtypes_Statically_Match
1317 (Designated_Type (T),
1318 Available_View (Designated_Type (Etype (Nam))))
1319 then
1320 Error_Msg_N
1321 ("subtype of renamed object does not statically match", N);
1322 end if;
1323 end if;
1324
1325 -- Special processing for renaming function return object. Some errors
1326 -- and warnings are produced only for calls that come from source.
1327
1328 if Nkind (Nam) = N_Function_Call then
1329 case Ada_Version is
1330
1331 -- Usage is illegal in Ada 83, but renamings are also introduced
1332 -- during expansion, and error does not apply to those.
1333
1334 when Ada_83 =>
1335 if Comes_From_Source (N) then
1336 Error_Msg_N
1337 ("(Ada 83) cannot rename function return object", Nam);
1338 end if;
1339
1340 -- In Ada 95, warn for odd case of renaming parameterless function
1341 -- call if this is not a limited type (where this is useful).
1342
1343 when others =>
1344 if Warn_On_Object_Renames_Function
1345 and then No (Parameter_Associations (Nam))
1346 and then not Is_Limited_Type (Etype (Nam))
1347 and then Comes_From_Source (Nam)
1348 then
1349 Error_Msg_N
1350 ("renaming function result object is suspicious?.r?", Nam);
1351 Error_Msg_NE
1352 ("\function & will be called only once?.r?", Nam,
1353 Entity (Name (Nam)));
1354 Error_Msg_N -- CODEFIX
1355 ("\suggest using an initialized constant object "
1356 & "instead?.r?", Nam);
1357 end if;
1358 end case;
1359 end if;
1360
1361 Check_Constrained_Object;
1362
1363 -- An object renaming requires an exact match of the type. Class-wide
1364 -- matching is not allowed.
1365
1366 if Is_Class_Wide_Type (T)
1367 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1368 then
1369 Wrong_Type (Nam, T);
1370 end if;
1371
1372 -- We must search for an actual subtype here so that the bounds of
1373 -- objects of unconstrained types don't get dropped on the floor - such
1374 -- as with renamings of formal parameters.
1375
1376 T2 := Get_Actual_Subtype_If_Available (Nam);
1377
1378 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1379
1380 if Nkind (Nam) = N_Explicit_Dereference
1381 and then Ekind (Etype (T2)) = E_Incomplete_Type
1382 then
1383 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1384 return;
1385
1386 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1387 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1388 return;
1389 end if;
1390
1391 if Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1392 declare
1393 Nam_Ent : constant Entity_Id := Entity (Get_Object_Name (Nam));
1394 Nam_Decl : constant Node_Id := Declaration_Node (Nam_Ent);
1395
1396 begin
1397 if Has_Null_Exclusion (N)
1398 and then not Has_Null_Exclusion (Nam_Decl)
1399 then
1400 -- Ada 2005 (AI-423): If the object name denotes a generic
1401 -- formal object of a generic unit G, and the object renaming
1402 -- declaration occurs within the body of G or within the body
1403 -- of a generic unit declared within the declarative region
1404 -- of G, then the declaration of the formal object of G must
1405 -- have a null exclusion or a null-excluding subtype.
1406
1407 if Is_Formal_Object (Nam_Ent)
1408 and then In_Generic_Scope (Id)
1409 then
1410 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1411 Error_Msg_N
1412 ("object does not exclude `NULL` "
1413 & "(RM 8.5.1(4.6/2))", N);
1414
1415 elsif In_Package_Body (Scope (Id)) then
1416 Error_Msg_N
1417 ("formal object does not have a null exclusion"
1418 & "(RM 8.5.1(4.6/2))", N);
1419 end if;
1420
1421 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1422 -- shall exclude null.
1423
1424 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1425 Error_Msg_N
1426 ("object does not exclude `NULL` "
1427 & "(RM 8.5.1(4.6/2))", N);
1428
1429 -- An instance is illegal if it contains a renaming that
1430 -- excludes null, and the actual does not. The renaming
1431 -- declaration has already indicated that the declaration
1432 -- of the renamed actual in the instance will raise
1433 -- constraint_error.
1434
1435 elsif Nkind (Nam_Decl) = N_Object_Declaration
1436 and then In_Instance
1437 and then
1438 Present (Corresponding_Generic_Association (Nam_Decl))
1439 and then Nkind (Expression (Nam_Decl)) =
1440 N_Raise_Constraint_Error
1441 then
1442 Error_Msg_N
1443 ("actual does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1444
1445 -- Finally, if there is a null exclusion, the subtype mark
1446 -- must not be null-excluding.
1447
1448 elsif No (Access_Definition (N))
1449 and then Can_Never_Be_Null (T)
1450 then
1451 Error_Msg_NE
1452 ("`NOT NULL` not allowed (& already excludes null)",
1453 N, T);
1454
1455 end if;
1456
1457 elsif Can_Never_Be_Null (T)
1458 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1459 then
1460 Error_Msg_N
1461 ("object does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1462
1463 elsif Has_Null_Exclusion (N)
1464 and then No (Access_Definition (N))
1465 and then Can_Never_Be_Null (T)
1466 then
1467 Error_Msg_NE
1468 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1469 end if;
1470 end;
1471 end if;
1472
1473 -- Set the Ekind of the entity, unless it has been set already, as is
1474 -- the case for the iteration object over a container with no variable
1475 -- indexing. In that case it's been marked as a constant, and we do not
1476 -- want to change it to a variable.
1477
1478 if Ekind (Id) /= E_Constant then
1479 Mutate_Ekind (Id, E_Variable);
1480 end if;
1481
1482 Reinit_Object_Size_Align (Id);
1483
1484 -- If N comes from source then check that the original node is an
1485 -- object reference since there may have been several rewritting and
1486 -- folding. Do not do this for N_Function_Call or N_Explicit_Dereference
1487 -- which might correspond to rewrites of e.g. N_Selected_Component
1488 -- (for example Object.Method rewriting).
1489 -- If N does not come from source then assume the tree is properly
1490 -- formed and accept any object reference. In such cases we do support
1491 -- more cases of renamings anyway, so the actual check on which renaming
1492 -- is valid is better left to the code generator as a last sanity
1493 -- check.
1494
1495 if Comes_From_Source (N) then
1496 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference then
1497 Is_Object_Ref := Is_Object_Reference (Nam);
1498 else
1499 Is_Object_Ref := Is_Object_Reference (Original_Node (Nam));
1500 end if;
1501 else
1502 Is_Object_Ref := True;
1503 end if;
1504
1505 if T = Any_Type or else Etype (Nam) = Any_Type then
1506 return;
1507
1508 -- Verify that the renamed entity is an object or function call
1509
1510 elsif Is_Object_Ref then
1511 if Comes_From_Source (N) then
1512 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1513 Error_Msg_N
1514 ("illegal renaming of discriminant-dependent component", Nam);
1515 elsif Depends_On_Mutably_Tagged_Ext_Comp (Nam) then
1516 Error_Msg_N
1517 ("illegal renaming of mutably tagged dependent component",
1518 Nam);
1519 end if;
1520
1521 -- If the renaming comes from source and the renamed object is a
1522 -- dereference, then mark the prefix as needing debug information,
1523 -- since it might have been rewritten hence internally generated
1524 -- and Debug_Renaming_Declaration will link the renaming to it.
1525
1526 if Nkind (Nam) = N_Explicit_Dereference
1527 and then Is_Entity_Name (Prefix (Nam))
1528 then
1529 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1530 end if;
1531 end if;
1532
1533 -- Weird but legal, equivalent to renaming a function call. Illegal
1534 -- if the literal is the result of constant-folding an attribute
1535 -- reference that is not a function.
1536
1537 elsif Is_Entity_Name (Nam)
1538 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1539 and then Nkind (Original_Node (Nam)) /= N_Attribute_Reference
1540 then
1541 null;
1542
1543 -- A named number can only be renamed without a subtype mark
1544
1545 elsif Nkind (Nam) in N_Real_Literal | N_Integer_Literal
1546 and then Present (Subtype_Mark (N))
1547 and then Present (Original_Entity (Nam))
1548 then
1549 Error_Msg_N ("incompatible types in renaming", Nam);
1550
1551 -- AI12-0383: Names that denote values can be renamed.
1552 -- Ignore (accept) N_Raise_xxx_Error nodes in this context.
1553
1554 elsif No_Raise_xxx_Error (Nam) = OK then
1555 Error_Msg_Ada_2022_Feature ("value in renaming", Sloc (Nam));
1556 end if;
1557
1558 Set_Etype (Id, T2);
1559
1560 if not Is_Variable (Nam) then
1561 Mutate_Ekind (Id, E_Constant);
1562 Set_Never_Set_In_Source (Id, True);
1563 Set_Is_True_Constant (Id, True);
1564 end if;
1565
1566 -- The entity of the renaming declaration needs to reflect whether the
1567 -- renamed object is atomic, independent, volatile or VFA. These flags
1568 -- are set on the renamed object in the RM legality sense.
1569
1570 Set_Is_Atomic (Id, Is_Atomic_Object (Nam));
1571 Set_Is_Independent (Id, Is_Independent_Object (Nam));
1572 Set_Is_Volatile (Id, Is_Volatile_Object_Ref (Nam));
1573 Set_Is_Volatile_Full_Access
1574 (Id, Is_Volatile_Full_Access_Object_Ref (Nam));
1575
1576 -- Treat as volatile if we just set the Volatile flag
1577
1578 if Is_Volatile (Id)
1579
1580 -- Or if we are renaming an entity which was marked this way
1581
1582 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1583
1584 or else (Is_Entity_Name (Nam)
1585 and then Treat_As_Volatile (Entity (Nam)))
1586 then
1587 Set_Treat_As_Volatile (Id, True);
1588 end if;
1589
1590 -- Now make the link to the renamed object
1591
1592 Set_Renamed_Object (Id, Nam);
1593
1594 -- Implementation-defined aspect specifications can appear in a renaming
1595 -- declaration, but not language-defined ones. The call to procedure
1596 -- Analyze_Aspect_Specifications will take care of this error check.
1597
1598 Analyze_Aspect_Specifications (N, Id);
1599
1600 -- Deal with dimensions
1601
1602 Analyze_Dimension (N);
1603 end Analyze_Object_Renaming;
1604
1605 ------------------------------
1606 -- Analyze_Package_Renaming --
1607 ------------------------------
1608
1609 procedure Analyze_Package_Renaming (N : Node_Id) is
1610 New_P : constant Entity_Id := Defining_Entity (N);
1611 Old_P : Entity_Id;
1612 Spec : Node_Id;
1613
1614 begin
1615 if Name (N) = Error then
1616 return;
1617 end if;
1618
1619 -- Check for Text_IO special units (we may be renaming a Text_IO child),
1620 -- but make sure not to catch renamings generated for package instances
1621 -- that have nothing to do with them but are nevertheless homonyms.
1622
1623 if Is_Entity_Name (Name (N))
1624 and then Present (Entity (Name (N)))
1625 and then Is_Generic_Instance (Entity (Name (N)))
1626 then
1627 null;
1628 else
1629 Check_Text_IO_Special_Unit (Name (N));
1630 end if;
1631
1632 if Current_Scope /= Standard_Standard then
1633 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1634 end if;
1635
1636 Enter_Name (New_P);
1637 Analyze (Name (N));
1638
1639 if Is_Entity_Name (Name (N)) then
1640 Old_P := Entity (Name (N));
1641 else
1642 Old_P := Any_Id;
1643 end if;
1644
1645 if Etype (Old_P) = Any_Type then
1646 Error_Msg_N ("expect package name in renaming", Name (N));
1647
1648 elsif Ekind (Old_P) /= E_Package
1649 and then not (Ekind (Old_P) = E_Generic_Package
1650 and then In_Open_Scopes (Old_P))
1651 then
1652 if Ekind (Old_P) = E_Generic_Package then
1653 Error_Msg_N
1654 ("generic package cannot be renamed as a package", Name (N));
1655 else
1656 Error_Msg_Sloc := Sloc (Old_P);
1657 Error_Msg_NE
1658 ("expect package name in renaming, found& declared#",
1659 Name (N), Old_P);
1660 end if;
1661
1662 -- Set basic attributes to minimize cascaded errors
1663
1664 Mutate_Ekind (New_P, E_Package);
1665 Set_Etype (New_P, Standard_Void_Type);
1666
1667 elsif Present (Renamed_Entity (Old_P))
1668 and then Renames_Limited_View (Old_P)
1669 and then not
1670 Unit_Is_Visible (Cunit (Get_Source_Unit (Renamed_Entity (Old_P))))
1671 then
1672 Error_Msg_NE
1673 ("renaming of limited view of package & not usable in this context"
1674 & " (RM 8.5.3(3.1/2))", Name (N), Renamed_Entity (Old_P));
1675
1676 -- Set basic attributes to minimize cascaded errors
1677
1678 Mutate_Ekind (New_P, E_Package);
1679 Set_Etype (New_P, Standard_Void_Type);
1680
1681 -- Here for OK package renaming
1682
1683 else
1684 -- Entities in the old package are accessible through the renaming
1685 -- entity. The simplest implementation is to have both packages share
1686 -- the entity list.
1687
1688 Mutate_Ekind (New_P, E_Package);
1689 Set_Etype (New_P, Standard_Void_Type);
1690
1691 if Present (Renamed_Entity (Old_P)) then
1692 Set_Renamed_Entity (New_P, Renamed_Entity (Old_P));
1693 Set_Renames_Limited_View (New_P, Renames_Limited_View (Old_P));
1694 else
1695 Set_Renamed_Entity (New_P, Old_P);
1696 Set_Renames_Limited_View (New_P, From_Limited_With (Old_P));
1697 end if;
1698
1699 -- The package renaming declaration may become Ghost if it renames a
1700 -- Ghost entity.
1701
1702 Mark_Ghost_Renaming (N, Old_P);
1703
1704 Set_Has_Completion (New_P);
1705 Set_First_Entity (New_P, First_Entity (Old_P));
1706 Set_Last_Entity (New_P, Last_Entity (Old_P));
1707 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1708 Check_Library_Unit_Renaming (N, Old_P);
1709 Generate_Reference (Old_P, Name (N));
1710
1711 -- If the renaming is in the visible part of a package, then we set
1712 -- Renamed_In_Spec for the renamed package, to prevent giving
1713 -- warnings about no entities referenced. Such a warning would be
1714 -- overenthusiastic, since clients can see entities in the renamed
1715 -- package via the visible package renaming.
1716
1717 declare
1718 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1719 begin
1720 if Ekind (Ent) = E_Package
1721 and then not In_Private_Part (Ent)
1722 and then In_Extended_Main_Source_Unit (N)
1723 and then Ekind (Old_P) = E_Package
1724 then
1725 Set_Renamed_In_Spec (Old_P);
1726 end if;
1727 end;
1728
1729 -- If this is the renaming declaration of a package instantiation
1730 -- within itself, it is the declaration that ends the list of actuals
1731 -- for the instantiation. At this point, the subtypes that rename
1732 -- the actuals are flagged as generic, to avoid spurious ambiguities
1733 -- if the actuals for two distinct formals happen to coincide. If
1734 -- the actual is a private type, the subtype has a private completion
1735 -- that is flagged in the same fashion.
1736
1737 -- Resolution is identical to what is was in the original generic.
1738 -- On exit from the generic instance, these are turned into regular
1739 -- subtypes again, so they are compatible with types in their class.
1740
1741 if not Is_Generic_Instance (Old_P) then
1742 return;
1743 else
1744 Spec := Specification (Unit_Declaration_Node (Old_P));
1745 end if;
1746
1747 if Nkind (Spec) = N_Package_Specification
1748 and then Present (Generic_Parent (Spec))
1749 and then Old_P = Current_Scope
1750 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1751 then
1752 declare
1753 E : Entity_Id;
1754
1755 begin
1756 E := First_Entity (Old_P);
1757 while Present (E) and then E /= New_P loop
1758 if Is_Type (E)
1759 and then Nkind (Parent (E)) = N_Subtype_Declaration
1760 then
1761 Set_Is_Generic_Actual_Type (E);
1762
1763 if Is_Private_Type (E)
1764 and then Present (Full_View (E))
1765 then
1766 Set_Is_Generic_Actual_Type (Full_View (E));
1767 end if;
1768 end if;
1769
1770 Next_Entity (E);
1771 end loop;
1772 end;
1773 end if;
1774 end if;
1775
1776 -- Implementation-defined aspect specifications can appear in a renaming
1777 -- declaration, but not language-defined ones. The call to procedure
1778 -- Analyze_Aspect_Specifications will take care of this error check.
1779
1780 Analyze_Aspect_Specifications (N, New_P);
1781 end Analyze_Package_Renaming;
1782
1783 -------------------------------
1784 -- Analyze_Renamed_Character --
1785 -------------------------------
1786
1787 procedure Analyze_Renamed_Character
1788 (N : Node_Id;
1789 New_S : Entity_Id;
1790 Is_Body : Boolean)
1791 is
1792 C : constant Node_Id := Name (N);
1793
1794 begin
1795 if Ekind (New_S) = E_Function then
1796 Resolve (C, Etype (New_S));
1797
1798 if Is_Body then
1799 Check_Frozen_Renaming (N, New_S);
1800 end if;
1801
1802 else
1803 Error_Msg_N ("character literal can only be renamed as function", N);
1804 end if;
1805 end Analyze_Renamed_Character;
1806
1807 ---------------------------------
1808 -- Analyze_Renamed_Dereference --
1809 ---------------------------------
1810
1811 procedure Analyze_Renamed_Dereference
1812 (N : Node_Id;
1813 New_S : Entity_Id;
1814 Is_Body : Boolean)
1815 is
1816 Nam : constant Node_Id := Name (N);
1817 P : constant Node_Id := Prefix (Nam);
1818 Typ : Entity_Id;
1819 Ind : Interp_Index;
1820 It : Interp;
1821
1822 begin
1823 if not Is_Overloaded (P) then
1824 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1825 or else not Type_Conformant (Etype (Nam), New_S)
1826 then
1827 Error_Msg_N ("designated type does not match specification", P);
1828 else
1829 Resolve (P);
1830 end if;
1831
1832 return;
1833
1834 else
1835 Typ := Any_Type;
1836 Get_First_Interp (Nam, Ind, It);
1837
1838 while Present (It.Nam) loop
1839
1840 if Ekind (It.Nam) = E_Subprogram_Type
1841 and then Type_Conformant (It.Nam, New_S)
1842 then
1843 if Typ /= Any_Id then
1844 Error_Msg_N ("ambiguous renaming", P);
1845 return;
1846 else
1847 Typ := It.Nam;
1848 end if;
1849 end if;
1850
1851 Get_Next_Interp (Ind, It);
1852 end loop;
1853
1854 if Typ = Any_Type then
1855 Error_Msg_N ("designated type does not match specification", P);
1856 else
1857 Resolve (N, Typ);
1858
1859 if Is_Body then
1860 Check_Frozen_Renaming (N, New_S);
1861 end if;
1862 end if;
1863 end if;
1864 end Analyze_Renamed_Dereference;
1865
1866 ---------------------------
1867 -- Analyze_Renamed_Entry --
1868 ---------------------------
1869
1870 procedure Analyze_Renamed_Entry
1871 (N : Node_Id;
1872 New_S : Entity_Id;
1873 Is_Body : Boolean)
1874 is
1875 Nam : constant Node_Id := Name (N);
1876 Sel : constant Node_Id := Selector_Name (Nam);
1877 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1878 Old_S : Entity_Id;
1879
1880 begin
1881 if Entity (Sel) = Any_Id then
1882
1883 -- Selector is undefined on prefix. Error emitted already
1884
1885 Set_Has_Completion (New_S);
1886 return;
1887 end if;
1888
1889 -- Otherwise find renamed entity and build body of New_S as a call to it
1890
1891 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1892
1893 if Old_S = Any_Id then
1894 Error_Msg_N ("no subprogram or entry matches specification", N);
1895 else
1896 if Is_Body then
1897 Check_Subtype_Conformant (New_S, Old_S, N);
1898 Generate_Reference (New_S, Defining_Entity (N), 'b');
1899 Style.Check_Identifier (Defining_Entity (N), New_S);
1900
1901 else
1902 -- Only mode conformance required for a renaming_as_declaration
1903
1904 Check_Mode_Conformant (New_S, Old_S, N);
1905 end if;
1906
1907 Inherit_Renamed_Profile (New_S, Old_S);
1908
1909 -- The prefix can be an arbitrary expression that yields a task or
1910 -- protected object, so it must be resolved.
1911
1912 if Is_Access_Type (Etype (Prefix (Nam))) then
1913 Insert_Explicit_Dereference (Prefix (Nam));
1914 end if;
1915 Resolve (Prefix (Nam), Scope (Old_S));
1916 end if;
1917
1918 Set_Convention (New_S, Convention (Old_S));
1919 Set_Has_Completion (New_S, Inside_A_Generic);
1920
1921 -- AI05-0225: If the renamed entity is a procedure or entry of a
1922 -- protected object, the target object must be a variable.
1923
1924 if Is_Protected_Type (Scope (Old_S))
1925 and then Ekind (New_S) = E_Procedure
1926 and then not Is_Variable (Prefix (Nam))
1927 then
1928 if Is_Actual then
1929 Error_Msg_N
1930 ("target object of protected operation used as actual for "
1931 & "formal procedure must be a variable", Nam);
1932 else
1933 Error_Msg_N
1934 ("target object of protected operation renamed as procedure, "
1935 & "must be a variable", Nam);
1936 end if;
1937 end if;
1938
1939 if Is_Body then
1940 Check_Frozen_Renaming (N, New_S);
1941 end if;
1942 end Analyze_Renamed_Entry;
1943
1944 -----------------------------------
1945 -- Analyze_Renamed_Family_Member --
1946 -----------------------------------
1947
1948 procedure Analyze_Renamed_Family_Member
1949 (N : Node_Id;
1950 New_S : Entity_Id;
1951 Is_Body : Boolean)
1952 is
1953 Nam : constant Node_Id := Name (N);
1954 P : constant Node_Id := Prefix (Nam);
1955 Old_S : Entity_Id;
1956
1957 begin
1958 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1959 or else (Nkind (P) = N_Selected_Component
1960 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1961 then
1962 if Is_Entity_Name (P) then
1963 Old_S := Entity (P);
1964 else
1965 Old_S := Entity (Selector_Name (P));
1966 end if;
1967
1968 if not Entity_Matches_Spec (Old_S, New_S) then
1969 Error_Msg_N ("entry family does not match specification", N);
1970
1971 elsif Is_Body then
1972 Check_Subtype_Conformant (New_S, Old_S, N);
1973 Generate_Reference (New_S, Defining_Entity (N), 'b');
1974 Style.Check_Identifier (Defining_Entity (N), New_S);
1975 end if;
1976
1977 else
1978 Error_Msg_N ("no entry family matches specification", N);
1979 end if;
1980
1981 Set_Has_Completion (New_S, Inside_A_Generic);
1982
1983 if Is_Body then
1984 Check_Frozen_Renaming (N, New_S);
1985 end if;
1986 end Analyze_Renamed_Family_Member;
1987
1988 -----------------------------------------
1989 -- Analyze_Renamed_Primitive_Operation --
1990 -----------------------------------------
1991
1992 procedure Analyze_Renamed_Primitive_Operation
1993 (N : Node_Id;
1994 New_S : Entity_Id;
1995 Is_Body : Boolean)
1996 is
1997 Old_S : Entity_Id;
1998 Nam : Entity_Id;
1999
2000 function Conforms
2001 (Subp : Entity_Id;
2002 Ctyp : Conformance_Type) return Boolean;
2003 -- Verify that the signatures of the renamed entity and the new entity
2004 -- match. The first formal of the renamed entity is skipped because it
2005 -- is the target object in any subsequent call.
2006
2007 --------------
2008 -- Conforms --
2009 --------------
2010
2011 function Conforms
2012 (Subp : Entity_Id;
2013 Ctyp : Conformance_Type) return Boolean
2014 is
2015 Old_F : Entity_Id;
2016 New_F : Entity_Id;
2017
2018 begin
2019 if Ekind (Subp) /= Ekind (New_S) then
2020 return False;
2021 end if;
2022
2023 Old_F := Next_Formal (First_Formal (Subp));
2024 New_F := First_Formal (New_S);
2025 while Present (Old_F) and then Present (New_F) loop
2026 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
2027 return False;
2028 end if;
2029
2030 if Ctyp >= Mode_Conformant
2031 and then Ekind (Old_F) /= Ekind (New_F)
2032 then
2033 return False;
2034 end if;
2035
2036 Next_Formal (New_F);
2037 Next_Formal (Old_F);
2038 end loop;
2039
2040 return True;
2041 end Conforms;
2042
2043 -- Start of processing for Analyze_Renamed_Primitive_Operation
2044
2045 begin
2046 if not Is_Overloaded (Selector_Name (Name (N))) then
2047 Old_S := Entity (Selector_Name (Name (N)));
2048
2049 if not Conforms (Old_S, Type_Conformant) then
2050 Old_S := Any_Id;
2051 end if;
2052
2053 else
2054 -- Find the operation that matches the given signature
2055
2056 declare
2057 It : Interp;
2058 Ind : Interp_Index;
2059
2060 begin
2061 Old_S := Any_Id;
2062 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
2063
2064 while Present (It.Nam) loop
2065 if Conforms (It.Nam, Type_Conformant) then
2066 Old_S := It.Nam;
2067 end if;
2068
2069 Get_Next_Interp (Ind, It);
2070 end loop;
2071 end;
2072 end if;
2073
2074 if Old_S = Any_Id then
2075 Error_Msg_N ("no subprogram or entry matches specification", N);
2076
2077 else
2078 if Is_Body then
2079 if not Conforms (Old_S, Subtype_Conformant) then
2080 Error_Msg_N ("subtype conformance error in renaming", N);
2081 end if;
2082
2083 Generate_Reference (New_S, Defining_Entity (N), 'b');
2084 Style.Check_Identifier (Defining_Entity (N), New_S);
2085
2086 else
2087 -- Only mode conformance required for a renaming_as_declaration
2088
2089 if not Conforms (Old_S, Mode_Conformant) then
2090 Error_Msg_N ("mode conformance error in renaming", N);
2091 end if;
2092
2093 -- AI12-0204: The prefix of a prefixed view that is renamed or
2094 -- passed as a formal subprogram must be renamable as an object.
2095
2096 Nam := Prefix (Name (N));
2097
2098 if Is_Object_Reference (Nam) then
2099 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
2100 Error_Msg_N
2101 ("illegal renaming of discriminant-dependent component",
2102 Nam);
2103 elsif Depends_On_Mutably_Tagged_Ext_Comp (Nam) then
2104 Error_Msg_N
2105 ("illegal renaming of mutably tagged dependent component",
2106 Nam);
2107 end if;
2108 else
2109 Error_Msg_N ("expect object name in renaming", Nam);
2110 end if;
2111
2112 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
2113 -- view of a subprogram is intrinsic, because the compiler has
2114 -- to generate a wrapper for any call to it. If the name in a
2115 -- subprogram renaming is a prefixed view, the entity is thus
2116 -- intrinsic, and 'Access cannot be applied to it.
2117
2118 Set_Convention (New_S, Convention_Intrinsic);
2119 end if;
2120
2121 -- Inherit_Renamed_Profile (New_S, Old_S);
2122
2123 -- The prefix can be an arbitrary expression that yields an
2124 -- object, so it must be resolved.
2125
2126 Resolve (Prefix (Name (N)));
2127 end if;
2128 end Analyze_Renamed_Primitive_Operation;
2129
2130 ---------------------------------
2131 -- Analyze_Subprogram_Renaming --
2132 ---------------------------------
2133
2134 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
2135 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
2136 Is_Actual : constant Boolean := Present (Formal_Spec);
2137 Nam : constant Node_Id := Name (N);
2138 Save_AV : constant Ada_Version_Type := Ada_Version;
2139 Save_AVP : constant Node_Id := Ada_Version_Pragma;
2140 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
2141 Spec : constant Node_Id := Specification (N);
2142
2143 Old_S : Entity_Id := Empty;
2144 Rename_Spec : Entity_Id;
2145
2146 procedure Check_Null_Exclusion
2147 (Ren : Entity_Id;
2148 Sub : Entity_Id);
2149 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
2150 -- following AI rules:
2151 --
2152 -- If Ren denotes a generic formal object of a generic unit G, and the
2153 -- renaming (or instantiation containing the actual) occurs within the
2154 -- body of G or within the body of a generic unit declared within the
2155 -- declarative region of G, then the corresponding parameter of G
2156 -- shall have a null_exclusion; Otherwise the subtype of the Sub's
2157 -- formal parameter shall exclude null.
2158 --
2159 -- Similarly for its return profile.
2160
2161 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id);
2162 -- Ensure that a SPARK renaming denoted by its entity Subp_Id does not
2163 -- declare a primitive operation of a tagged type (SPARK RM 6.1.1(3)).
2164
2165 procedure Freeze_Actual_Profile;
2166 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
2167 -- types: a callable entity freezes its profile, unless it has an
2168 -- incomplete untagged formal (RM 13.14(10.2/3)).
2169
2170 function Has_Class_Wide_Actual return Boolean;
2171 -- Ada 2012 (AI05-071, AI05-0131) and Ada 2022 (AI12-0165): True if N is
2172 -- the renaming for a defaulted formal subprogram where the actual for
2173 -- the controlling formal type is class-wide.
2174
2175 procedure Handle_Instance_With_Class_Wide_Type
2176 (Inst_Node : Node_Id;
2177 Ren_Id : Entity_Id;
2178 Wrapped_Prim : out Entity_Id;
2179 Wrap_Id : out Entity_Id);
2180 -- Ada 2012 (AI05-0071), Ada 2022 (AI12-0165): when the actual type
2181 -- of an instantiation is a class-wide type T'Class we may need to
2182 -- wrap a primitive operation of T; this routine looks for a suitable
2183 -- primitive to be wrapped and (if the wrapper is required) returns the
2184 -- Id of the wrapped primitive and the Id of the built wrapper. Ren_Id
2185 -- is the defining entity for the renamed subprogram specification.
2186
2187 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
2188 -- Find renamed entity when the declaration is a renaming_as_body and
2189 -- the renamed entity may itself be a renaming_as_body. Used to enforce
2190 -- rule that a renaming_as_body is illegal if the declaration occurs
2191 -- before the subprogram it completes is frozen, and renaming indirectly
2192 -- renames the subprogram itself.(Defect Report 8652/0027).
2193
2194 --------------------------
2195 -- Check_Null_Exclusion --
2196 --------------------------
2197
2198 procedure Check_Null_Exclusion
2199 (Ren : Entity_Id;
2200 Sub : Entity_Id)
2201 is
2202 Ren_Formal : Entity_Id;
2203 Sub_Formal : Entity_Id;
2204
2205 function Null_Exclusion_Mismatch
2206 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean;
2207 -- Return True if there is a null exclusion mismatch between
2208 -- Renaming and Renamed, False otherwise.
2209
2210 -----------------------------
2211 -- Null_Exclusion_Mismatch --
2212 -----------------------------
2213
2214 function Null_Exclusion_Mismatch
2215 (Renaming : Entity_Id; Renamed : Entity_Id) return Boolean is
2216 begin
2217 return Has_Null_Exclusion (Parent (Renaming))
2218 and then
2219 not (Has_Null_Exclusion (Parent (Renamed))
2220 or else (Can_Never_Be_Null (Etype (Renamed))
2221 and then not
2222 (Is_Formal_Subprogram (Sub)
2223 and then In_Generic_Body (Current_Scope))));
2224 end Null_Exclusion_Mismatch;
2225
2226 begin
2227 -- Parameter check
2228
2229 Ren_Formal := First_Formal (Ren);
2230 Sub_Formal := First_Formal (Sub);
2231 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2232 if Null_Exclusion_Mismatch (Ren_Formal, Sub_Formal) then
2233 Error_Msg_Sloc := Sloc (Sub_Formal);
2234 Error_Msg_NE
2235 ("`NOT NULL` required for parameter &#",
2236 Ren_Formal, Sub_Formal);
2237 end if;
2238
2239 Next_Formal (Ren_Formal);
2240 Next_Formal (Sub_Formal);
2241 end loop;
2242
2243 -- Return profile check
2244
2245 if Nkind (Parent (Ren)) = N_Function_Specification
2246 and then Nkind (Parent (Sub)) = N_Function_Specification
2247 and then Null_Exclusion_Mismatch (Ren, Sub)
2248 then
2249 Error_Msg_Sloc := Sloc (Sub);
2250 Error_Msg_N ("return must specify `NOT NULL`#", Ren);
2251 end if;
2252 end Check_Null_Exclusion;
2253
2254 -------------------------------------
2255 -- Check_SPARK_Primitive_Operation --
2256 -------------------------------------
2257
2258 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id) is
2259 Prag : constant Node_Id := SPARK_Pragma (Subp_Id);
2260 Typ : Entity_Id;
2261
2262 begin
2263 -- Nothing to do when the subprogram is not subject to SPARK_Mode On
2264 -- because this check applies to SPARK code only.
2265
2266 if not (Present (Prag)
2267 and then Get_SPARK_Mode_From_Annotation (Prag) = On)
2268 then
2269 return;
2270
2271 -- Nothing to do when the subprogram is not a primitive operation
2272
2273 elsif not Is_Primitive (Subp_Id) then
2274 return;
2275 end if;
2276
2277 Typ := Find_Dispatching_Type (Subp_Id);
2278
2279 -- Nothing to do when the subprogram is a primitive operation of an
2280 -- untagged type.
2281
2282 if No (Typ) then
2283 return;
2284 end if;
2285
2286 -- At this point a renaming declaration introduces a new primitive
2287 -- operation for a tagged type.
2288
2289 Error_Msg_Node_2 := Typ;
2290 Error_Msg_NE
2291 ("subprogram renaming & cannot declare primitive for type & "
2292 & "(SPARK RM 6.1.1(3))", N, Subp_Id);
2293 end Check_SPARK_Primitive_Operation;
2294
2295 ---------------------------
2296 -- Freeze_Actual_Profile --
2297 ---------------------------
2298
2299 procedure Freeze_Actual_Profile is
2300 F : Entity_Id;
2301 Has_Untagged_Inc : Boolean;
2302 Instantiation_Node : constant Node_Id := Parent (N);
2303
2304 begin
2305 if Ada_Version >= Ada_2012 then
2306 F := First_Formal (Formal_Spec);
2307 Has_Untagged_Inc := False;
2308 while Present (F) loop
2309 if Ekind (Etype (F)) = E_Incomplete_Type
2310 and then not Is_Tagged_Type (Etype (F))
2311 then
2312 Has_Untagged_Inc := True;
2313 exit;
2314 end if;
2315
2316 Next_Formal (F);
2317 end loop;
2318
2319 if Ekind (Formal_Spec) = E_Function
2320 and then not Is_Tagged_Type (Etype (Formal_Spec))
2321 then
2322 Has_Untagged_Inc := True;
2323 end if;
2324
2325 if not Has_Untagged_Inc then
2326 F := First_Formal (Old_S);
2327 while Present (F) loop
2328 Freeze_Before (Instantiation_Node, Etype (F));
2329
2330 if Is_Incomplete_Or_Private_Type (Etype (F))
2331 and then No (Underlying_Type (Etype (F)))
2332 then
2333 -- Exclude generic types, or types derived from them.
2334 -- They will be frozen in the enclosing instance.
2335
2336 if Is_Generic_Type (Etype (F))
2337 or else Is_Generic_Type (Root_Type (Etype (F)))
2338 then
2339 null;
2340
2341 -- A limited view of a type declared elsewhere needs no
2342 -- freezing actions.
2343
2344 elsif From_Limited_With (Etype (F)) then
2345 null;
2346
2347 -- Incomplete types are never frozen (AI12-0155-1)
2348
2349 elsif Is_Incomplete_Type (Etype (F)) then
2350 null;
2351
2352 else
2353 Error_Msg_NE
2354 ("type& must be frozen before this point",
2355 Instantiation_Node, Etype (F));
2356 end if;
2357 end if;
2358
2359 Next_Formal (F);
2360 end loop;
2361 end if;
2362 end if;
2363 end Freeze_Actual_Profile;
2364
2365 ---------------------------
2366 -- Has_Class_Wide_Actual --
2367 ---------------------------
2368
2369 function Has_Class_Wide_Actual return Boolean is
2370 Formal : Entity_Id;
2371 Formal_Typ : Entity_Id;
2372
2373 begin
2374 if Is_Actual then
2375 Formal := First_Formal (Formal_Spec);
2376 while Present (Formal) loop
2377 Formal_Typ := Etype (Formal);
2378
2379 if Has_Unknown_Discriminants (Formal_Typ)
2380 and then not Is_Class_Wide_Type (Formal_Typ)
2381 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2382 then
2383 return True;
2384 end if;
2385
2386 Next_Formal (Formal);
2387 end loop;
2388 end if;
2389
2390 return False;
2391 end Has_Class_Wide_Actual;
2392
2393 ------------------------------------------
2394 -- Handle_Instance_With_Class_Wide_Type --
2395 ------------------------------------------
2396
2397 procedure Handle_Instance_With_Class_Wide_Type
2398 (Inst_Node : Node_Id;
2399 Ren_Id : Entity_Id;
2400 Wrapped_Prim : out Entity_Id;
2401 Wrap_Id : out Entity_Id)
2402 is
2403 procedure Build_Class_Wide_Wrapper
2404 (Ren_Id : Entity_Id;
2405 Prim_Op : Entity_Id;
2406 Wrap_Id : out Entity_Id);
2407 -- Build a wrapper for the renaming Ren_Id of subprogram Prim_Op.
2408
2409 procedure Find_Suitable_Candidate
2410 (Prim_Op : out Entity_Id;
2411 Is_CW_Prim : out Boolean);
2412 -- Look for a suitable primitive to be wrapped (Prim_Op); Is_CW_Prim
2413 -- indicates that the found candidate is a class-wide primitive (to
2414 -- help the caller decide if the wrapper is required).
2415
2416 ------------------------------
2417 -- Build_Class_Wide_Wrapper --
2418 ------------------------------
2419
2420 procedure Build_Class_Wide_Wrapper
2421 (Ren_Id : Entity_Id;
2422 Prim_Op : Entity_Id;
2423 Wrap_Id : out Entity_Id)
2424 is
2425 Loc : constant Source_Ptr := Sloc (N);
2426
2427 function Build_Call
2428 (Subp_Id : Entity_Id;
2429 Params : List_Id) return Node_Id;
2430 -- Create a dispatching call to invoke routine Subp_Id with
2431 -- actuals built from the parameter specifications of list Params.
2432
2433 function Build_Expr_Fun_Call
2434 (Subp_Id : Entity_Id;
2435 Params : List_Id) return Node_Id;
2436 -- Create a dispatching call to invoke function Subp_Id with
2437 -- actuals built from the parameter specifications of list Params.
2438 -- Directly return the call, so that it can be used inside an
2439 -- expression function. This is a requirement of GNATprove mode.
2440
2441 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
2442 -- Create a subprogram specification based on the subprogram
2443 -- profile of Subp_Id.
2444
2445 ----------------
2446 -- Build_Call --
2447 ----------------
2448
2449 function Build_Call
2450 (Subp_Id : Entity_Id;
2451 Params : List_Id) return Node_Id
2452 is
2453 Actuals : constant List_Id := New_List;
2454 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2455 Formal : Node_Id;
2456
2457 begin
2458 -- Build the actual parameters of the call
2459
2460 Formal := First (Params);
2461 while Present (Formal) loop
2462 Append_To (Actuals,
2463 Make_Identifier (Loc,
2464 Chars (Defining_Identifier (Formal))));
2465 Next (Formal);
2466 end loop;
2467
2468 -- Generate:
2469 -- return Subp_Id (Actuals);
2470
2471 if Ekind (Subp_Id) in E_Function | E_Operator then
2472 return
2473 Make_Simple_Return_Statement (Loc,
2474 Expression =>
2475 Make_Function_Call (Loc,
2476 Name => Call_Ref,
2477 Parameter_Associations => Actuals));
2478
2479 -- Generate:
2480 -- Subp_Id (Actuals);
2481
2482 else
2483 return
2484 Make_Procedure_Call_Statement (Loc,
2485 Name => Call_Ref,
2486 Parameter_Associations => Actuals);
2487 end if;
2488 end Build_Call;
2489
2490 -------------------------
2491 -- Build_Expr_Fun_Call --
2492 -------------------------
2493
2494 function Build_Expr_Fun_Call
2495 (Subp_Id : Entity_Id;
2496 Params : List_Id) return Node_Id
2497 is
2498 Actuals : constant List_Id := New_List;
2499 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2500 Formal : Node_Id;
2501
2502 begin
2503 pragma Assert (Ekind (Subp_Id) in E_Function | E_Operator);
2504
2505 -- Build the actual parameters of the call
2506
2507 Formal := First (Params);
2508 while Present (Formal) loop
2509 Append_To (Actuals,
2510 Make_Identifier (Loc,
2511 Chars (Defining_Identifier (Formal))));
2512 Next (Formal);
2513 end loop;
2514
2515 -- Generate:
2516 -- Subp_Id (Actuals);
2517
2518 return
2519 Make_Function_Call (Loc,
2520 Name => Call_Ref,
2521 Parameter_Associations => Actuals);
2522 end Build_Expr_Fun_Call;
2523
2524 ----------------
2525 -- Build_Spec --
2526 ----------------
2527
2528 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2529 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2530 Spec_Id : constant Entity_Id :=
2531 Make_Defining_Identifier (Loc,
2532 New_External_Name (Chars (Subp_Id), 'R'));
2533
2534 begin
2535 if Ekind (Formal_Spec) = E_Procedure then
2536 return
2537 Make_Procedure_Specification (Loc,
2538 Defining_Unit_Name => Spec_Id,
2539 Parameter_Specifications => Params);
2540 else
2541 return
2542 Make_Function_Specification (Loc,
2543 Defining_Unit_Name => Spec_Id,
2544 Parameter_Specifications => Params,
2545 Result_Definition =>
2546 New_Copy_Tree (Result_Definition (Spec)));
2547 end if;
2548 end Build_Spec;
2549
2550 -- Local variables
2551
2552 Body_Decl : Node_Id;
2553 Spec_Decl : Node_Id;
2554 New_Spec : Node_Id;
2555
2556 -- Start of processing for Build_Class_Wide_Wrapper
2557
2558 begin
2559 pragma Assert (not Error_Posted (Nam));
2560
2561 -- Step 1: Create the declaration and the body of the wrapper,
2562 -- insert all the pieces into the tree.
2563
2564 -- In GNATprove mode, create a function wrapper in the form of an
2565 -- expression function, so that an implicit postcondition relating
2566 -- the result of calling the wrapper function and the result of
2567 -- the dispatching call to the wrapped function is known during
2568 -- proof.
2569
2570 if GNATprove_Mode
2571 and then Ekind (Ren_Id) in E_Function | E_Operator
2572 then
2573 New_Spec := Build_Spec (Ren_Id);
2574 Body_Decl :=
2575 Make_Expression_Function (Loc,
2576 Specification => New_Spec,
2577 Expression =>
2578 Build_Expr_Fun_Call
2579 (Subp_Id => Prim_Op,
2580 Params => Parameter_Specifications (New_Spec)));
2581
2582 Wrap_Id := Defining_Entity (Body_Decl);
2583
2584 -- Otherwise, create separate spec and body for the subprogram
2585
2586 else
2587 Spec_Decl :=
2588 Make_Subprogram_Declaration (Loc,
2589 Specification => Build_Spec (Ren_Id));
2590 Insert_Before_And_Analyze (N, Spec_Decl);
2591
2592 Wrap_Id := Defining_Entity (Spec_Decl);
2593
2594 Body_Decl :=
2595 Make_Subprogram_Body (Loc,
2596 Specification => Build_Spec (Ren_Id),
2597 Declarations => New_List,
2598 Handled_Statement_Sequence =>
2599 Make_Handled_Sequence_Of_Statements (Loc,
2600 Statements => New_List (
2601 Build_Call
2602 (Subp_Id => Prim_Op,
2603 Params =>
2604 Parameter_Specifications
2605 (Specification (Spec_Decl))))));
2606
2607 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
2608 end if;
2609
2610 Set_Is_Class_Wide_Wrapper (Wrap_Id);
2611
2612 -- If the operator carries an Eliminated pragma, indicate that
2613 -- the wrapper is also to be eliminated, to prevent spurious
2614 -- errors when using gnatelim on programs that include box-
2615 -- defaulted initialization of equality operators.
2616
2617 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2618
2619 -- In GNATprove mode, insert the body in the tree for analysis
2620
2621 if GNATprove_Mode then
2622 Insert_Before_And_Analyze (N, Body_Decl);
2623 end if;
2624
2625 -- The generated body does not freeze and must be analyzed when
2626 -- the class-wide wrapper is frozen. The body is only needed if
2627 -- expansion is enabled.
2628
2629 if Expander_Active then
2630 Append_Freeze_Action (Wrap_Id, Body_Decl);
2631 end if;
2632
2633 -- Step 2: The subprogram renaming aliases the wrapper
2634
2635 Rewrite (Name (N), New_Occurrence_Of (Wrap_Id, Loc));
2636 end Build_Class_Wide_Wrapper;
2637
2638 -----------------------------
2639 -- Find_Suitable_Candidate --
2640 -----------------------------
2641
2642 procedure Find_Suitable_Candidate
2643 (Prim_Op : out Entity_Id;
2644 Is_CW_Prim : out Boolean)
2645 is
2646 Loc : constant Source_Ptr := Sloc (N);
2647
2648 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
2649 -- Find a primitive subprogram of type Typ which matches the
2650 -- profile of the renaming declaration.
2651
2652 procedure Interpretation_Error (Subp_Id : Entity_Id);
2653 -- Emit a continuation error message suggesting subprogram Subp_Id
2654 -- as a possible interpretation.
2655
2656 function Is_Intrinsic_Equality
2657 (Subp_Id : Entity_Id) return Boolean;
2658 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
2659 -- operator.
2660
2661 function Is_Suitable_Candidate
2662 (Subp_Id : Entity_Id) return Boolean;
2663 -- Determine whether subprogram Subp_Id is a suitable candidate
2664 -- for the role of a wrapped subprogram.
2665
2666 --------------------
2667 -- Find_Primitive --
2668 --------------------
2669
2670 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2671 procedure Replace_Parameter_Types (Spec : Node_Id);
2672 -- Given a specification Spec, replace all class-wide parameter
2673 -- types with reference to type Typ.
2674
2675 -----------------------------
2676 -- Replace_Parameter_Types --
2677 -----------------------------
2678
2679 procedure Replace_Parameter_Types (Spec : Node_Id) is
2680 Formal : Node_Id;
2681 Formal_Id : Entity_Id;
2682 Formal_Typ : Node_Id;
2683
2684 begin
2685 Formal := First (Parameter_Specifications (Spec));
2686 while Present (Formal) loop
2687 Formal_Id := Defining_Identifier (Formal);
2688 Formal_Typ := Parameter_Type (Formal);
2689
2690 -- Create a new entity for each class-wide formal to
2691 -- prevent aliasing with the original renaming. Replace
2692 -- the type of such a parameter with the candidate type.
2693
2694 if Nkind (Formal_Typ) = N_Identifier
2695 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2696 then
2697 Set_Defining_Identifier (Formal,
2698 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2699
2700 Set_Parameter_Type (Formal,
2701 New_Occurrence_Of (Typ, Loc));
2702 end if;
2703
2704 Next (Formal);
2705 end loop;
2706 end Replace_Parameter_Types;
2707
2708 -- Local variables
2709
2710 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2711 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2712 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2713 Subp_Id : Entity_Id;
2714
2715 -- Start of processing for Find_Primitive
2716
2717 begin
2718 -- Each attempt to find a suitable primitive of a particular
2719 -- type operates on its own copy of the original renaming.
2720 -- As a result the original renaming is kept decoration and
2721 -- side-effect free.
2722
2723 -- Inherit the overloaded status of the renamed subprogram name
2724
2725 if Is_Overloaded (Nam) then
2726 Set_Is_Overloaded (Alt_Nam);
2727 Save_Interps (Nam, Alt_Nam);
2728 end if;
2729
2730 -- The copied renaming is hidden from visibility to prevent the
2731 -- pollution of the enclosing context.
2732
2733 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2734
2735 -- The types of all class-wide parameters must be changed to
2736 -- the candidate type.
2737
2738 Replace_Parameter_Types (Alt_Spec);
2739
2740 -- Try to find a suitable primitive that matches the altered
2741 -- profile of the renaming specification.
2742
2743 Subp_Id :=
2744 Find_Renamed_Entity
2745 (N => Alt_Ren,
2746 Nam => Name (Alt_Ren),
2747 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2748 Is_Actual => Is_Actual);
2749
2750 -- Do not return Any_Id if the resolution of the altered
2751 -- profile failed as this complicates further checks on
2752 -- the caller side; return Empty instead.
2753
2754 if Subp_Id = Any_Id then
2755 return Empty;
2756 else
2757 return Subp_Id;
2758 end if;
2759 end Find_Primitive;
2760
2761 --------------------------
2762 -- Interpretation_Error --
2763 --------------------------
2764
2765 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2766 begin
2767 Error_Msg_Sloc := Sloc (Subp_Id);
2768
2769 if Is_Internal (Subp_Id) then
2770 Error_Msg_NE
2771 ("\\possible interpretation: predefined & #",
2772 Spec, Formal_Spec);
2773 else
2774 Error_Msg_NE
2775 ("\\possible interpretation: & defined #",
2776 Spec, Formal_Spec);
2777 end if;
2778 end Interpretation_Error;
2779
2780 ---------------------------
2781 -- Is_Intrinsic_Equality --
2782 ---------------------------
2783
2784 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean
2785 is
2786 begin
2787 return
2788 Ekind (Subp_Id) = E_Operator
2789 and then Chars (Subp_Id) = Name_Op_Eq
2790 and then Is_Intrinsic_Subprogram (Subp_Id);
2791 end Is_Intrinsic_Equality;
2792
2793 ---------------------------
2794 -- Is_Suitable_Candidate --
2795 ---------------------------
2796
2797 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean
2798 is
2799 begin
2800 if No (Subp_Id) then
2801 return False;
2802
2803 -- An intrinsic subprogram is never a good candidate. This
2804 -- is an indication of a missing primitive, either defined
2805 -- directly or inherited from a parent tagged type.
2806
2807 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2808 return False;
2809
2810 else
2811 return True;
2812 end if;
2813 end Is_Suitable_Candidate;
2814
2815 -- Local variables
2816
2817 Actual_Typ : Entity_Id := Empty;
2818 -- The actual class-wide type for Formal_Typ
2819
2820 CW_Prim_OK : Boolean;
2821 CW_Prim_Op : Entity_Id;
2822 -- The class-wide subprogram (if available) that corresponds to
2823 -- the renamed generic formal subprogram.
2824
2825 Formal_Typ : Entity_Id := Empty;
2826 -- The generic formal type with unknown discriminants
2827
2828 Root_Prim_OK : Boolean;
2829 Root_Prim_Op : Entity_Id;
2830 -- The root type primitive (if available) that corresponds to the
2831 -- renamed generic formal subprogram.
2832
2833 Root_Typ : Entity_Id := Empty;
2834 -- The root type of Actual_Typ
2835
2836 Formal : Node_Id;
2837
2838 -- Start of processing for Find_Suitable_Candidate
2839
2840 begin
2841 pragma Assert (not Error_Posted (Nam));
2842
2843 Prim_Op := Empty;
2844 Is_CW_Prim := False;
2845
2846 -- Analyze the renamed name, but do not resolve it. The resolution
2847 -- is completed once a suitable subprogram is found.
2848
2849 Analyze (Nam);
2850
2851 -- When the renamed name denotes the intrinsic operator equals,
2852 -- the name must be treated as overloaded. This allows for a
2853 -- potential match against the root type's predefined equality
2854 -- function.
2855
2856 if Is_Intrinsic_Equality (Entity (Nam)) then
2857 Set_Is_Overloaded (Nam);
2858 Collect_Interps (Nam);
2859 end if;
2860
2861 -- Step 1: Find the generic formal type and its corresponding
2862 -- class-wide actual type from the renamed generic formal
2863 -- subprogram.
2864
2865 Formal := First_Formal (Formal_Spec);
2866 while Present (Formal) loop
2867 if Has_Unknown_Discriminants (Etype (Formal))
2868 and then not Is_Class_Wide_Type (Etype (Formal))
2869 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2870 then
2871 Formal_Typ := Etype (Formal);
2872 Actual_Typ := Base_Type (Get_Instance_Of (Formal_Typ));
2873 Root_Typ := Root_Type (Actual_Typ);
2874 exit;
2875 end if;
2876
2877 Next_Formal (Formal);
2878 end loop;
2879
2880 -- The specification of the generic formal subprogram should
2881 -- always contain a formal type with unknown discriminants whose
2882 -- actual is a class-wide type; otherwise this indicates a failure
2883 -- in function Has_Class_Wide_Actual.
2884
2885 pragma Assert (Present (Formal_Typ));
2886
2887 -- Step 2: Find the proper class-wide subprogram or primitive
2888 -- that corresponds to the renamed generic formal subprogram.
2889
2890 CW_Prim_Op := Find_Primitive (Actual_Typ);
2891 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2892 Root_Prim_Op := Find_Primitive (Root_Typ);
2893 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2894
2895 -- The class-wide actual type has two subprograms that correspond
2896 -- to the renamed generic formal subprogram:
2897
2898 -- with procedure Prim_Op (Param : Formal_Typ);
2899
2900 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2901 -- procedure Prim_Op (Param : Actual_Typ'Class);
2902
2903 -- Even though the declaration of the two subprograms is legal, a
2904 -- call to either one is ambiguous and therefore illegal.
2905
2906 if CW_Prim_OK and Root_Prim_OK then
2907
2908 -- A user-defined primitive has precedence over a predefined
2909 -- one.
2910
2911 if Is_Internal (CW_Prim_Op)
2912 and then not Is_Internal (Root_Prim_Op)
2913 then
2914 Prim_Op := Root_Prim_Op;
2915
2916 elsif Is_Internal (Root_Prim_Op)
2917 and then not Is_Internal (CW_Prim_Op)
2918 then
2919 Prim_Op := CW_Prim_Op;
2920 Is_CW_Prim := True;
2921
2922 elsif CW_Prim_Op = Root_Prim_Op then
2923 Prim_Op := Root_Prim_Op;
2924
2925 -- The two subprograms are legal but the class-wide subprogram
2926 -- is a class-wide wrapper built for a previous instantiation;
2927 -- the wrapper has precedence.
2928
2929 elsif Present (Alias (CW_Prim_Op))
2930 and then Is_Class_Wide_Wrapper (Ultimate_Alias (CW_Prim_Op))
2931 then
2932 Prim_Op := CW_Prim_Op;
2933 Is_CW_Prim := True;
2934
2935 -- Otherwise both candidate subprograms are user-defined and
2936 -- ambiguous.
2937
2938 else
2939 Error_Msg_NE
2940 ("ambiguous actual for generic subprogram &",
2941 Spec, Formal_Spec);
2942 Interpretation_Error (Root_Prim_Op);
2943 Interpretation_Error (CW_Prim_Op);
2944 return;
2945 end if;
2946
2947 elsif CW_Prim_OK and not Root_Prim_OK then
2948 Prim_Op := CW_Prim_Op;
2949 Is_CW_Prim := True;
2950
2951 elsif not CW_Prim_OK and Root_Prim_OK then
2952 Prim_Op := Root_Prim_Op;
2953
2954 -- An intrinsic equality may act as a suitable candidate in the
2955 -- case of a null type extension where the parent's equality
2956 -- is hidden. A call to an intrinsic equality is expanded as
2957 -- dispatching.
2958
2959 elsif Present (Root_Prim_Op)
2960 and then Is_Intrinsic_Equality (Root_Prim_Op)
2961 then
2962 Prim_Op := Root_Prim_Op;
2963
2964 -- Otherwise there are no candidate subprograms. Let the caller
2965 -- diagnose the error.
2966
2967 else
2968 return;
2969 end if;
2970
2971 -- At this point resolution has taken place and the name is no
2972 -- longer overloaded. Mark the primitive as referenced.
2973
2974 Set_Is_Overloaded (Name (N), False);
2975 Set_Referenced (Prim_Op);
2976 end Find_Suitable_Candidate;
2977
2978 -- Local variables
2979
2980 Is_CW_Prim : Boolean;
2981
2982 -- Start of processing for Handle_Instance_With_Class_Wide_Type
2983
2984 begin
2985 Wrapped_Prim := Empty;
2986 Wrap_Id := Empty;
2987
2988 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a
2989 -- formal type with unknown discriminants and a generic primitive
2990 -- operation of the said type with a box require special processing
2991 -- when the actual is a class-wide type:
2992 --
2993 -- generic
2994 -- type Formal_Typ (<>) is private;
2995 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
2996 -- package Gen is ...
2997 --
2998 -- package Inst is new Gen (Actual_Typ'Class);
2999 --
3000 -- In this case the general renaming mechanism used in the prologue
3001 -- of an instance no longer applies:
3002 --
3003 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
3004 --
3005 -- The above is replaced the following wrapper/renaming combination:
3006 --
3007 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
3008 -- begin
3009 -- Prim_Op (Param); -- primitive
3010 -- end Wrapper;
3011 --
3012 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
3013 --
3014 -- This transformation applies only if there is no explicit visible
3015 -- class-wide operation at the point of the instantiation. Ren_Id is
3016 -- the entity of the renaming declaration. When the transformation
3017 -- applies, Wrapped_Prim is the entity of the wrapped primitive.
3018
3019 if Box_Present (Inst_Node) then
3020 Find_Suitable_Candidate
3021 (Prim_Op => Wrapped_Prim,
3022 Is_CW_Prim => Is_CW_Prim);
3023
3024 if Present (Wrapped_Prim) then
3025 if not Is_CW_Prim then
3026 Build_Class_Wide_Wrapper (Ren_Id, Wrapped_Prim, Wrap_Id);
3027
3028 -- Small optimization: When the candidate is a class-wide
3029 -- subprogram we don't build the wrapper; we modify the
3030 -- renaming declaration to directly map the actual to the
3031 -- generic formal and discard the candidate.
3032
3033 else
3034 Rewrite (Nam, New_Occurrence_Of (Wrapped_Prim, Sloc (N)));
3035 Wrapped_Prim := Empty;
3036 end if;
3037 end if;
3038
3039 -- Ada 2022 (AI12-0165, RM 12.6(8.5/3)): The actual subprogram for a
3040 -- formal_abstract_subprogram_declaration shall be:
3041 -- a) a dispatching operation of the controlling type; or
3042 -- b) if the controlling type is a formal type, and the actual
3043 -- type corresponding to that formal type is a specific type T,
3044 -- a dispatching operation of type T; or
3045 -- c) if the controlling type is a formal type, and the actual
3046 -- type is a class-wide type T'Class, an implicitly declared
3047 -- subprogram corresponding to a primitive operation of type T.
3048
3049 elsif Nkind (Inst_Node) = N_Formal_Abstract_Subprogram_Declaration
3050 and then Is_Entity_Name (Nam)
3051 then
3052 Find_Suitable_Candidate
3053 (Prim_Op => Wrapped_Prim,
3054 Is_CW_Prim => Is_CW_Prim);
3055
3056 if Present (Wrapped_Prim) then
3057
3058 -- Cases (a) and (b); see previous description.
3059
3060 if not Is_CW_Prim then
3061 Build_Class_Wide_Wrapper (Ren_Id, Wrapped_Prim, Wrap_Id);
3062
3063 -- Case (c); see previous description.
3064
3065 -- Implicit operations of T'Class for subtype declarations
3066 -- are built by Derive_Subprogram, and their Alias attribute
3067 -- references the primitive operation of T.
3068
3069 elsif not Comes_From_Source (Wrapped_Prim)
3070 and then Nkind (Parent (Wrapped_Prim)) = N_Subtype_Declaration
3071 and then Present (Alias (Wrapped_Prim))
3072 then
3073 -- We don't need to build the wrapper; we modify the
3074 -- renaming declaration to directly map the actual to
3075 -- the generic formal and discard the candidate.
3076
3077 Rewrite (Nam,
3078 New_Occurrence_Of (Alias (Wrapped_Prim), Sloc (N)));
3079 Wrapped_Prim := Empty;
3080
3081 -- Legality rules do not apply; discard the candidate.
3082
3083 else
3084 Wrapped_Prim := Empty;
3085 end if;
3086 end if;
3087 end if;
3088 end Handle_Instance_With_Class_Wide_Type;
3089
3090 -------------------------
3091 -- Original_Subprogram --
3092 -------------------------
3093
3094 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
3095 Orig_Decl : Node_Id;
3096 Orig_Subp : Entity_Id;
3097
3098 begin
3099 -- First case: renamed entity is itself a renaming
3100
3101 if Present (Alias (Subp)) then
3102 return Alias (Subp);
3103
3104 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
3105 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
3106 then
3107 -- Check if renamed entity is a renaming_as_body
3108
3109 Orig_Decl :=
3110 Unit_Declaration_Node
3111 (Corresponding_Body (Unit_Declaration_Node (Subp)));
3112
3113 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
3114 Orig_Subp := Entity (Name (Orig_Decl));
3115
3116 if Orig_Subp = Rename_Spec then
3117
3118 -- Circularity detected
3119
3120 return Orig_Subp;
3121
3122 else
3123 return (Original_Subprogram (Orig_Subp));
3124 end if;
3125 else
3126 return Subp;
3127 end if;
3128 else
3129 return Subp;
3130 end if;
3131 end Original_Subprogram;
3132
3133 -- Local variables
3134
3135 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
3136 -- Ada 2012 (AI05-071, AI05-0131) and Ada 2022 (AI12-0165): True if the
3137 -- renaming is for a defaulted formal subprogram when the actual for a
3138 -- related formal type is class-wide.
3139
3140 Inst_Node : Node_Id := Empty;
3141 New_S : Entity_Id := Empty;
3142 Wrapped_Prim : Entity_Id := Empty;
3143
3144 -- Start of processing for Analyze_Subprogram_Renaming
3145
3146 begin
3147 -- We must test for the attribute renaming case before the Analyze
3148 -- call because otherwise Sem_Attr will complain that the attribute
3149 -- is missing an argument when it is analyzed.
3150
3151 if Nkind (Nam) = N_Attribute_Reference then
3152
3153 -- In the case of an abstract formal subprogram association, rewrite
3154 -- an actual given by a stream or Put_Image attribute as the name of
3155 -- the corresponding stream or Put_Image primitive of the type.
3156
3157 -- In a generic context the stream and Put_Image operations are not
3158 -- generated, and this must be treated as a normal attribute
3159 -- reference, to be expanded in subsequent instantiations.
3160
3161 if Is_Actual
3162 and then Is_Abstract_Subprogram (Formal_Spec)
3163 and then Expander_Active
3164 then
3165 declare
3166 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
3167 Prim : Entity_Id;
3168
3169 begin
3170 -- The class-wide forms of the stream and Put_Image attributes
3171 -- are not primitive dispatching operations (even though they
3172 -- internally dispatch).
3173
3174 if Is_Class_Wide_Type (Prefix_Type) then
3175 Error_Msg_N
3176 ("attribute must be a primitive dispatching operation",
3177 Nam);
3178 return;
3179 end if;
3180
3181 -- Retrieve the primitive subprogram associated with the
3182 -- attribute. This can only be a stream attribute, since those
3183 -- are the only ones that are dispatching (and the actual for
3184 -- an abstract formal subprogram must be dispatching
3185 -- operation).
3186
3187 case Attribute_Name (Nam) is
3188 when Name_Input =>
3189 Prim :=
3190 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input);
3191
3192 when Name_Output =>
3193 Prim :=
3194 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output);
3195
3196 when Name_Read =>
3197 Prim :=
3198 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read);
3199
3200 when Name_Write =>
3201 Prim :=
3202 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write);
3203
3204 when Name_Put_Image =>
3205 Prim :=
3206 Find_Optional_Prim_Op (Prefix_Type, TSS_Put_Image);
3207
3208 when others =>
3209 Error_Msg_N
3210 ("attribute must be a primitive dispatching operation",
3211 Nam);
3212 return;
3213 end case;
3214
3215 -- If no stream operation was found, and the type is limited,
3216 -- the user should have defined one. This rule does not apply
3217 -- to Put_Image.
3218
3219 if No (Prim)
3220 and then Attribute_Name (Nam) /= Name_Put_Image
3221 then
3222 if Is_Limited_Type (Prefix_Type) then
3223 Error_Msg_NE
3224 ("stream operation not defined for type&",
3225 N, Prefix_Type);
3226 return;
3227
3228 -- Otherwise, compiler should have generated default
3229
3230 else
3231 raise Program_Error;
3232 end if;
3233 end if;
3234
3235 -- Rewrite the attribute into the name of its corresponding
3236 -- primitive dispatching subprogram. We can then proceed with
3237 -- the usual processing for subprogram renamings.
3238
3239 declare
3240 Prim_Name : constant Node_Id :=
3241 Make_Identifier (Sloc (Nam),
3242 Chars => Chars (Prim));
3243 begin
3244 Set_Entity (Prim_Name, Prim);
3245 Rewrite (Nam, Prim_Name);
3246 Analyze (Nam);
3247 end;
3248 end;
3249
3250 -- Normal processing for a renaming of an attribute
3251
3252 else
3253 Attribute_Renaming (N);
3254 return;
3255 end if;
3256 end if;
3257
3258 -- Check whether this declaration corresponds to the instantiation of a
3259 -- formal subprogram.
3260
3261 -- If this is an instantiation, the corresponding actual is frozen and
3262 -- error messages can be made more precise. If this is a default
3263 -- subprogram, the entity is already established in the generic, and is
3264 -- not retrieved by visibility. If it is a default with a box, the
3265 -- candidate interpretations, if any, have been collected when building
3266 -- the renaming declaration. If overloaded, the proper interpretation is
3267 -- determined in Find_Renamed_Entity. If the entity is an operator,
3268 -- Find_Renamed_Entity applies additional visibility checks.
3269
3270 if Is_Actual then
3271 Inst_Node := Unit_Declaration_Node (Formal_Spec);
3272
3273 -- Ada 2012 (AI05-0071) and Ada 2022 (AI12-0165): when the actual
3274 -- type is a class-wide type T'Class we may need to wrap a primitive
3275 -- operation of T. Search for the wrapped primitive and (if required)
3276 -- build a wrapper whose body consists of a dispatching call to the
3277 -- wrapped primitive of T, with its formal parameters as the actual
3278 -- parameters.
3279
3280 if CW_Actual and then
3281
3282 -- Ada 2012 (AI05-0071): Check whether the renaming is for a
3283 -- defaulted actual subprogram with a class-wide actual.
3284
3285 (Box_Present (Inst_Node)
3286
3287 or else
3288
3289 -- Ada 2022 (AI12-0165): Check whether the renaming is for a formal
3290 -- abstract subprogram declaration with a class-wide actual.
3291
3292 (Nkind (Inst_Node) = N_Formal_Abstract_Subprogram_Declaration
3293 and then Is_Entity_Name (Nam)))
3294 then
3295 New_S := Analyze_Subprogram_Specification (Spec);
3296
3297 -- Do not attempt to build the wrapper if the renaming is in error
3298
3299 if not Error_Posted (Nam) then
3300 Handle_Instance_With_Class_Wide_Type
3301 (Inst_Node => Inst_Node,
3302 Ren_Id => New_S,
3303 Wrapped_Prim => Wrapped_Prim,
3304 Wrap_Id => Old_S);
3305
3306 -- If several candidates were found, then we reported the
3307 -- ambiguity; stop processing the renaming declaration to
3308 -- avoid reporting further (spurious) errors.
3309
3310 if Error_Posted (Spec) then
3311 return;
3312 end if;
3313
3314 end if;
3315 end if;
3316
3317 if Present (Wrapped_Prim) then
3318
3319 -- When the wrapper is built, the subprogram renaming aliases
3320 -- the wrapper.
3321
3322 Analyze (Nam);
3323
3324 pragma Assert (Old_S = Entity (Nam)
3325 and then Is_Class_Wide_Wrapper (Old_S));
3326
3327 -- The subprogram renaming declaration may become Ghost if it
3328 -- renames a wrapper of a Ghost entity.
3329
3330 Mark_Ghost_Renaming (N, Wrapped_Prim);
3331
3332 elsif Is_Entity_Name (Nam)
3333 and then Present (Entity (Nam))
3334 and then not Comes_From_Source (Nam)
3335 and then not Is_Overloaded (Nam)
3336 then
3337 Old_S := Entity (Nam);
3338
3339 -- The subprogram renaming declaration may become Ghost if it
3340 -- renames a Ghost entity.
3341
3342 Mark_Ghost_Renaming (N, Old_S);
3343
3344 New_S := Analyze_Subprogram_Specification (Spec);
3345
3346 -- Operator case
3347
3348 if Ekind (Old_S) = E_Operator then
3349
3350 -- Box present
3351
3352 if Box_Present (Inst_Node) then
3353 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3354
3355 -- If there is an immediately visible homonym of the operator
3356 -- and the declaration has a default, this is worth a warning
3357 -- because the user probably did not intend to get the pre-
3358 -- defined operator, visible in the generic declaration. To
3359 -- find if there is an intended candidate, analyze the renaming
3360 -- again in the current context.
3361
3362 elsif Scope (Old_S) = Standard_Standard
3363 and then Present (Default_Name (Inst_Node))
3364 then
3365 declare
3366 Decl : constant Node_Id := New_Copy_Tree (N);
3367 Hidden : Entity_Id;
3368
3369 begin
3370 Set_Entity (Name (Decl), Empty);
3371 Analyze (Name (Decl));
3372 Hidden :=
3373 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
3374
3375 if Present (Hidden)
3376 and then In_Open_Scopes (Scope (Hidden))
3377 and then Is_Immediately_Visible (Hidden)
3378 and then Comes_From_Source (Hidden)
3379 and then Hidden /= Old_S
3380 then
3381 Error_Msg_Sloc := Sloc (Hidden);
3382 Error_Msg_N
3383 ("default subprogram is resolved in the generic "
3384 & "declaration (RM 12.6(17))??", N);
3385 Error_Msg_NE ("\and will not use & #??", N, Hidden);
3386 end if;
3387 end;
3388 end if;
3389 end if;
3390
3391 else
3392 Analyze (Nam);
3393
3394 -- The subprogram renaming declaration may become Ghost if it
3395 -- renames a Ghost entity.
3396
3397 if Is_Entity_Name (Nam) then
3398 Mark_Ghost_Renaming (N, Entity (Nam));
3399 end if;
3400
3401 New_S := Analyze_Subprogram_Specification (Spec);
3402 end if;
3403
3404 else
3405 -- Renamed entity must be analyzed first, to avoid being hidden by
3406 -- new name (which might be the same in a generic instance).
3407
3408 Analyze (Nam);
3409
3410 -- The subprogram renaming declaration may become Ghost if it renames
3411 -- a Ghost entity.
3412
3413 if Is_Entity_Name (Nam) then
3414 Mark_Ghost_Renaming (N, Entity (Nam));
3415 end if;
3416
3417 -- The renaming defines a new overloaded entity, which is analyzed
3418 -- like a subprogram declaration.
3419
3420 New_S := Analyze_Subprogram_Specification (Spec);
3421 end if;
3422
3423 if Current_Scope /= Standard_Standard then
3424 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
3425 end if;
3426
3427 -- Set SPARK mode from current context
3428
3429 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
3430 Set_SPARK_Pragma_Inherited (New_S);
3431
3432 Rename_Spec := Find_Corresponding_Spec (N);
3433
3434 -- Case of Renaming_As_Body
3435
3436 if Present (Rename_Spec) then
3437 Check_Previous_Null_Procedure (N, Rename_Spec);
3438
3439 -- Renaming declaration is the completion of the declaration of
3440 -- Rename_Spec. We build an actual body for it at the freezing point.
3441
3442 Set_Corresponding_Spec (N, Rename_Spec);
3443
3444 -- Deal with special case of stream functions of abstract types
3445 -- and interfaces.
3446
3447 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
3448 N_Abstract_Subprogram_Declaration
3449 then
3450 -- Input stream functions are abstract if the object type is
3451 -- abstract. Similarly, all default stream functions for an
3452 -- interface type are abstract. However, these subprograms may
3453 -- receive explicit declarations in representation clauses, making
3454 -- the attribute subprograms usable as defaults in subsequent
3455 -- type extensions.
3456 -- In this case we rewrite the declaration to make the subprogram
3457 -- non-abstract. We remove the previous declaration, and insert
3458 -- the new one at the point of the renaming, to prevent premature
3459 -- access to unfrozen types. The new declaration reuses the
3460 -- specification of the previous one, and must not be analyzed.
3461
3462 pragma Assert
3463 (Is_Primitive (Entity (Nam))
3464 and then
3465 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
3466 declare
3467 Old_Decl : constant Node_Id :=
3468 Unit_Declaration_Node (Rename_Spec);
3469 New_Decl : constant Node_Id :=
3470 Make_Subprogram_Declaration (Sloc (N),
3471 Specification =>
3472 Relocate_Node (Specification (Old_Decl)));
3473 begin
3474 Remove (Old_Decl);
3475 Insert_After (N, New_Decl);
3476 Set_Is_Abstract_Subprogram (Rename_Spec, False);
3477 Set_Analyzed (New_Decl);
3478 end;
3479 end if;
3480
3481 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
3482
3483 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3484 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
3485 end if;
3486
3487 Set_Convention (New_S, Convention (Rename_Spec));
3488 Check_Fully_Conformant (New_S, Rename_Spec);
3489 Set_Public_Status (New_S);
3490
3491 if No_Return (Rename_Spec)
3492 and then not No_Return (Entity (Nam))
3493 then
3494 Error_Msg_NE
3495 ("renamed subprogram & must be No_Return", N, Entity (Nam));
3496 Error_Msg_N
3497 ("\since renaming subprogram is No_Return (RM 6.5.1(7/2))", N);
3498 end if;
3499
3500 -- The specification does not introduce new formals, but only
3501 -- repeats the formals of the original subprogram declaration.
3502 -- For cross-reference purposes, and for refactoring tools, we
3503 -- treat the formals of the renaming declaration as body formals.
3504
3505 Reference_Body_Formals (Rename_Spec, New_S);
3506
3507 -- Indicate that the entity in the declaration functions like the
3508 -- corresponding body, and is not a new entity. The body will be
3509 -- constructed later at the freeze point, so indicate that the
3510 -- completion has not been seen yet.
3511
3512 Reinit_Field_To_Zero (New_S, F_Has_Out_Or_In_Out_Parameter,
3513 Old_Ekind => (E_Function | E_Procedure => True, others => False));
3514 Reinit_Field_To_Zero (New_S, F_Needs_No_Actuals);
3515 Reinit_Field_To_Zero (New_S, F_Is_Predicate_Function);
3516 Reinit_Field_To_Zero (New_S, F_Protected_Subprogram);
3517 Reinit_Field_To_Zero (New_S, F_Is_Inlined_Always);
3518 Reinit_Field_To_Zero (New_S, F_Is_Generic_Actual_Subprogram);
3519 Mutate_Ekind (New_S, E_Subprogram_Body);
3520 New_S := Rename_Spec;
3521 Set_Has_Completion (Rename_Spec, False);
3522
3523 -- Ada 2005: check overriding indicator
3524
3525 if Present (Overridden_Operation (Rename_Spec)) then
3526 if Must_Not_Override (Specification (N)) then
3527 Error_Msg_NE
3528 ("subprogram& overrides inherited operation",
3529 N, Rename_Spec);
3530
3531 elsif Style_Check
3532 and then not Must_Override (Specification (N))
3533 then
3534 Style.Missing_Overriding (N, Rename_Spec);
3535 end if;
3536
3537 elsif Must_Override (Specification (N))
3538 and then not Can_Override_Operator (Rename_Spec)
3539 then
3540 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
3541 end if;
3542
3543 -- AI12-0132: a renames-as-body freezes the expression of any
3544 -- expression function that it renames.
3545
3546 if Is_Entity_Name (Nam)
3547 and then Is_Expression_Function (Entity (Nam))
3548 and then not Inside_A_Generic
3549 then
3550 Freeze_Expr_Types
3551 (Def_Id => Entity (Nam),
3552 Typ => Etype (Entity (Nam)),
3553 Expr =>
3554 Expression
3555 (Original_Node (Unit_Declaration_Node (Entity (Nam)))),
3556 N => N);
3557 end if;
3558
3559 -- Normal subprogram renaming (not renaming as body)
3560
3561 else
3562 Generate_Definition (New_S);
3563 New_Overloaded_Entity (New_S);
3564
3565 if not (Is_Entity_Name (Nam)
3566 and then Is_Intrinsic_Subprogram (Entity (Nam)))
3567 then
3568 Check_Delayed_Subprogram (New_S);
3569 end if;
3570
3571 -- Verify that a SPARK renaming does not declare a primitive
3572 -- operation of a tagged type.
3573
3574 Check_SPARK_Primitive_Operation (New_S);
3575 end if;
3576
3577 -- There is no need for elaboration checks on the new entity, which may
3578 -- be called before the next freezing point where the body will appear.
3579 -- Elaboration checks refer to the real entity, not the one created by
3580 -- the renaming declaration.
3581
3582 Set_Kill_Elaboration_Checks (New_S, True);
3583
3584 -- If we had a previous error, indicate a completion is present to stop
3585 -- junk cascaded messages, but don't take any further action.
3586
3587 if Etype (Nam) = Any_Type then
3588 Set_Has_Completion (New_S);
3589 return;
3590
3591 -- Case where name has the form of a selected component
3592
3593 elsif Nkind (Nam) = N_Selected_Component then
3594
3595 -- A name which has the form A.B can designate an entry of task A, a
3596 -- protected operation of protected object A, or finally a primitive
3597 -- operation of object A. In the later case, A is an object of some
3598 -- tagged type, or an access type that denotes one such. To further
3599 -- distinguish these cases, note that the scope of a task entry or
3600 -- protected operation is type of the prefix.
3601
3602 -- The prefix could be an overloaded function call that returns both
3603 -- kinds of operations. This overloading pathology is left to the
3604 -- dedicated reader ???
3605
3606 declare
3607 T : constant Entity_Id := Etype (Prefix (Nam));
3608
3609 begin
3610 if Present (T)
3611 and then
3612 (Is_Tagged_Type (T)
3613 or else
3614 (Is_Access_Type (T)
3615 and then Is_Tagged_Type (Designated_Type (T))))
3616 and then Scope (Entity (Selector_Name (Nam))) /= T
3617 then
3618 Analyze_Renamed_Primitive_Operation
3619 (N, New_S, Present (Rename_Spec));
3620 return;
3621
3622 else
3623 -- Renamed entity is an entry or protected operation. For those
3624 -- cases an explicit body is built (at the point of freezing of
3625 -- this entity) that contains a call to the renamed entity.
3626
3627 -- This is not allowed for renaming as body if the renamed
3628 -- spec is already frozen (see RM 8.5.4(5) for details).
3629
3630 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
3631 Error_Msg_N
3632 ("renaming-as-body cannot rename entry as subprogram", N);
3633 Error_Msg_NE
3634 ("\since & is already frozen (RM 8.5.4(5))",
3635 N, Rename_Spec);
3636 else
3637 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
3638 end if;
3639
3640 return;
3641 end if;
3642 end;
3643
3644 -- Case where name is an explicit dereference X.all
3645
3646 elsif Nkind (Nam) = N_Explicit_Dereference then
3647
3648 -- Renamed entity is designated by access_to_subprogram expression.
3649 -- Must build body to encapsulate call, as in the entry case.
3650
3651 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
3652 return;
3653
3654 -- Indexed component
3655
3656 elsif Nkind (Nam) = N_Indexed_Component then
3657 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
3658 return;
3659
3660 -- Character literal
3661
3662 elsif Nkind (Nam) = N_Character_Literal then
3663 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
3664 return;
3665
3666 -- Only remaining case is where we have a non-entity name, or a renaming
3667 -- of some other non-overloadable entity.
3668
3669 elsif not Is_Entity_Name (Nam)
3670 or else not Is_Overloadable (Entity (Nam))
3671 then
3672 -- Do not mention the renaming if it comes from an instance
3673
3674 if not Is_Actual then
3675 Error_Msg_N ("expect valid subprogram name in renaming", N);
3676 else
3677 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
3678 end if;
3679
3680 return;
3681 end if;
3682
3683 -- Find the renamed entity that matches the given specification. Disable
3684 -- Ada_83 because there is no requirement of full conformance between
3685 -- renamed entity and new entity, even though the same circuit is used.
3686
3687 -- This is a bit of an odd case, which introduces a really irregular use
3688 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
3689 -- this. ???
3690
3691 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
3692 Ada_Version_Pragma := Empty;
3693 Ada_Version_Explicit := Ada_Version;
3694
3695 if No (Old_S) then
3696 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3697
3698 -- The visible operation may be an inherited abstract operation that
3699 -- was overridden in the private part, in which case a call will
3700 -- dispatch to the overriding operation. Use the overriding one in
3701 -- the renaming declaration, to prevent spurious errors below.
3702
3703 if Is_Overloadable (Old_S)
3704 and then Is_Abstract_Subprogram (Old_S)
3705 and then No (DTC_Entity (Old_S))
3706 and then Present (Alias (Old_S))
3707 and then not Is_Abstract_Subprogram (Alias (Old_S))
3708 and then Present (Overridden_Operation (Alias (Old_S)))
3709 then
3710 Old_S := Alias (Old_S);
3711 end if;
3712
3713 -- When the renamed subprogram is overloaded and used as an actual
3714 -- of a generic, its entity is set to the first available homonym.
3715 -- We must first disambiguate the name, then set the proper entity.
3716
3717 if Is_Actual and then Is_Overloaded (Nam) then
3718 Set_Entity (Nam, Old_S);
3719 end if;
3720 end if;
3721
3722 -- Most common case: subprogram renames subprogram. No body is generated
3723 -- in this case, so we must indicate the declaration is complete as is.
3724 -- and inherit various attributes of the renamed subprogram.
3725
3726 if No (Rename_Spec) then
3727 Set_Has_Completion (New_S);
3728 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3729 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3730 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3731
3732 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3733 -- between a subprogram and its correct renaming.
3734
3735 -- Note: the Any_Id check is a guard that prevents compiler crashes
3736 -- when performing a null exclusion check between a renaming and a
3737 -- renamed subprogram that has been found to be illegal.
3738
3739 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3740 Check_Null_Exclusion
3741 (Ren => New_S,
3742 Sub => Entity (Nam));
3743 end if;
3744
3745 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3746 -- overriding. The flag Requires_Overriding is set very selectively
3747 -- and misses some other illegal cases. The additional conditions
3748 -- checked below are sufficient but not necessary ???
3749
3750 -- The rule does not apply to the renaming generated for an actual
3751 -- subprogram in an instance.
3752
3753 if Is_Actual then
3754 null;
3755
3756 -- Guard against previous errors, and omit renamings of predefined
3757 -- operators.
3758
3759 elsif Ekind (Old_S) not in E_Function | E_Procedure then
3760 null;
3761
3762 elsif Requires_Overriding (Old_S)
3763 or else
3764 (Is_Abstract_Subprogram (Old_S)
3765 and then Present (Find_Dispatching_Type (Old_S))
3766 and then not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3767 then
3768 Error_Msg_N
3769 ("renamed entity cannot be subprogram that requires overriding "
3770 & "(RM 8.5.4 (5.1))", N);
3771 end if;
3772
3773 declare
3774 Prev : constant Entity_Id := Overridden_Operation (New_S);
3775 begin
3776 if Present (Prev)
3777 and then
3778 (Has_Non_Trivial_Precondition (Prev)
3779 or else Has_Non_Trivial_Precondition (Old_S))
3780 then
3781 Error_Msg_NE
3782 ("conflicting inherited classwide preconditions in renaming "
3783 & "of& (RM 6.1.1 (17)", N, Old_S);
3784 end if;
3785 end;
3786 end if;
3787
3788 if Old_S /= Any_Id then
3789 if Is_Actual and then From_Default (N) then
3790
3791 -- This is an implicit reference to the default actual
3792
3793 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3794
3795 else
3796 Generate_Reference (Old_S, Nam);
3797 end if;
3798
3799 Check_Internal_Protected_Use (N, Old_S);
3800
3801 -- For a renaming-as-body, require subtype conformance, but if the
3802 -- declaration being completed has not been frozen, then inherit the
3803 -- convention of the renamed subprogram prior to checking conformance
3804 -- (unless the renaming has an explicit convention established; the
3805 -- rule stated in the RM doesn't seem to address this ???).
3806
3807 if Present (Rename_Spec) then
3808 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3809 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3810
3811 if not Is_Frozen (Rename_Spec) then
3812 if not Has_Convention_Pragma (Rename_Spec) then
3813 Set_Convention (New_S, Convention (Old_S));
3814 end if;
3815
3816 if Ekind (Old_S) /= E_Operator then
3817 Check_Mode_Conformant (New_S, Old_S, Spec);
3818 end if;
3819
3820 if Original_Subprogram (Old_S) = Rename_Spec then
3821 Error_Msg_N ("unfrozen subprogram cannot rename itself", N);
3822 else
3823 Check_Formal_Subprogram_Conformance (New_S, Old_S, Spec);
3824 end if;
3825 else
3826 Check_Subtype_Conformant (New_S, Old_S, Spec);
3827 end if;
3828
3829 Check_Frozen_Renaming (N, Rename_Spec);
3830
3831 -- Check explicitly that renamed entity is not intrinsic, because
3832 -- in a generic the renamed body is not built. In this case,
3833 -- the renaming_as_body is a completion.
3834
3835 if Inside_A_Generic then
3836 if Is_Frozen (Rename_Spec)
3837 and then Is_Intrinsic_Subprogram (Old_S)
3838 then
3839 Error_Msg_N
3840 ("subprogram in renaming_as_body cannot be intrinsic",
3841 Name (N));
3842 end if;
3843
3844 Set_Has_Completion (Rename_Spec);
3845 end if;
3846
3847 elsif Ekind (Old_S) /= E_Operator then
3848
3849 -- If this a defaulted subprogram for a class-wide actual there is
3850 -- no check for mode conformance, given that the signatures don't
3851 -- match (the source mentions T but the actual mentions T'Class).
3852
3853 if CW_Actual then
3854 null;
3855
3856 -- No need for a redundant error message if this is a nested
3857 -- instance, unless the current instantiation (of a child unit)
3858 -- is a compilation unit, which is not analyzed when the parent
3859 -- generic is analyzed.
3860
3861 elsif not Is_Actual
3862 or else No (Enclosing_Instance)
3863 or else Is_Compilation_Unit (Current_Scope)
3864 then
3865 Check_Mode_Conformant (New_S, Old_S);
3866 end if;
3867 end if;
3868
3869 if No (Rename_Spec) then
3870
3871 -- The parameter profile of the new entity is that of the renamed
3872 -- entity: the subtypes given in the specification are irrelevant.
3873
3874 Inherit_Renamed_Profile (New_S, Old_S);
3875
3876 -- A call to the subprogram is transformed into a call to the
3877 -- renamed entity. This is transitive if the renamed entity is
3878 -- itself a renaming.
3879
3880 if Present (Alias (Old_S)) then
3881 Set_Alias (New_S, Alias (Old_S));
3882 else
3883 Set_Alias (New_S, Old_S);
3884 end if;
3885
3886 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3887 -- renaming as body, since the entity in this case is not an
3888 -- intrinsic (it calls an intrinsic, but we have a real body for
3889 -- this call, and it is in this body that the required intrinsic
3890 -- processing will take place).
3891
3892 -- Also, if this is a renaming of inequality, the renamed operator
3893 -- is intrinsic, but what matters is the corresponding equality
3894 -- operator, which may be user-defined.
3895
3896 Set_Is_Intrinsic_Subprogram
3897 (New_S,
3898 Is_Intrinsic_Subprogram (Old_S)
3899 and then
3900 (Chars (Old_S) /= Name_Op_Ne
3901 or else Ekind (Old_S) = E_Operator
3902 or else Is_Intrinsic_Subprogram
3903 (Corresponding_Equality (Old_S))));
3904
3905 if Ekind (Alias (New_S)) = E_Operator then
3906 Set_Has_Delayed_Freeze (New_S, False);
3907 end if;
3908
3909 -- If the renaming corresponds to an association for an abstract
3910 -- formal subprogram, then various attributes must be set to
3911 -- indicate that the renaming is an abstract dispatching operation
3912 -- with a controlling type.
3913
3914 -- Skip this decoration when the renaming corresponds to an
3915 -- association with class-wide wrapper (see above) because such
3916 -- wrapper is neither abstract nor a dispatching operation (its
3917 -- body has the dispatching call to the wrapped primitive).
3918
3919 if Is_Actual
3920 and then Is_Abstract_Subprogram (Formal_Spec)
3921 and then No (Wrapped_Prim)
3922 then
3923
3924 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3925 -- see it as corresponding to a generic association for a
3926 -- formal abstract subprogram
3927
3928 Set_Is_Abstract_Subprogram (New_S);
3929
3930 declare
3931 New_S_Ctrl_Type : constant Entity_Id :=
3932 Find_Dispatching_Type (New_S);
3933 Old_S_Ctrl_Type : constant Entity_Id :=
3934 Find_Dispatching_Type (Old_S);
3935
3936 begin
3937
3938 -- The actual must match the (instance of the) formal,
3939 -- and must be a controlling type.
3940
3941 if Old_S_Ctrl_Type /= New_S_Ctrl_Type
3942 or else No (New_S_Ctrl_Type)
3943 then
3944 if No (New_S_Ctrl_Type) then
3945 Error_Msg_N
3946 ("actual must be dispatching subprogram", Nam);
3947 else
3948 Error_Msg_NE
3949 ("actual must be dispatching subprogram for type&",
3950 Nam, New_S_Ctrl_Type);
3951 end if;
3952
3953 else
3954 Set_Is_Dispatching_Operation (New_S);
3955 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3956
3957 -- If the actual in the formal subprogram is itself a
3958 -- formal abstract subprogram association, there's no
3959 -- dispatch table component or position to inherit.
3960
3961 if Present (DTC_Entity (Old_S)) then
3962 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3963 Set_DT_Position_Value (New_S, DT_Position (Old_S));
3964 end if;
3965 end if;
3966 end;
3967 end if;
3968 end if;
3969
3970 if Is_Actual then
3971 null;
3972
3973 -- The following is illegal, because F hides whatever other F may
3974 -- be around:
3975 -- function F (...) renames F;
3976
3977 elsif Old_S = New_S
3978 or else (Nkind (Nam) /= N_Expanded_Name
3979 and then Chars (Old_S) = Chars (New_S))
3980 then
3981 Error_Msg_N ("subprogram cannot rename itself", N);
3982
3983 -- This is illegal even if we use a selector:
3984 -- function F (...) renames Pkg.F;
3985 -- because F is still hidden.
3986
3987 elsif Nkind (Nam) = N_Expanded_Name
3988 and then Entity (Prefix (Nam)) = Current_Scope
3989 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3990 then
3991 -- This is an error, but we overlook the error and accept the
3992 -- renaming if the special Overriding_Renamings mode is in effect.
3993
3994 if not Overriding_Renamings then
3995 Error_Msg_NE
3996 ("implicit operation& is not visible (RM 8.3 (15))",
3997 Nam, Old_S);
3998 end if;
3999
4000 -- Check whether an expanded name used for the renamed subprogram
4001 -- begins with the same name as the renaming itself, and if so,
4002 -- issue an error about the prefix being hidden by the renaming.
4003 -- We exclude generic instances from this checking, since such
4004 -- normally illegal renamings can be constructed when expanding
4005 -- instantiations.
4006
4007 elsif Nkind (Nam) = N_Expanded_Name and then not In_Instance then
4008 declare
4009 function Ult_Expanded_Prefix (N : Node_Id) return Node_Id is
4010 (if Nkind (N) /= N_Expanded_Name
4011 then N
4012 else Ult_Expanded_Prefix (Prefix (N)));
4013 -- Returns the ultimate prefix of an expanded name
4014
4015 begin
4016 if Chars (Entity (Ult_Expanded_Prefix (Nam))) = Chars (New_S)
4017 then
4018 Error_Msg_Sloc := Sloc (N);
4019 Error_Msg_NE
4020 ("& is hidden by declaration#", Nam, New_S);
4021 end if;
4022 end;
4023 end if;
4024
4025 Set_Convention (New_S, Convention (Old_S));
4026
4027 if Is_Abstract_Subprogram (Old_S) then
4028 if Present (Rename_Spec) then
4029 Error_Msg_N
4030 ("a renaming-as-body cannot rename an abstract subprogram",
4031 N);
4032 Set_Has_Completion (Rename_Spec);
4033 else
4034 Set_Is_Abstract_Subprogram (New_S);
4035 end if;
4036 end if;
4037
4038 Check_Library_Unit_Renaming (N, Old_S);
4039
4040 -- Pathological case: procedure renames entry in the scope of its
4041 -- task. Entry is given by simple name, but body must be built for
4042 -- procedure. Of course if called it will deadlock.
4043
4044 if Ekind (Old_S) = E_Entry then
4045 Set_Has_Completion (New_S, False);
4046 Set_Alias (New_S, Empty);
4047 end if;
4048
4049 -- Do not freeze the renaming nor the renamed entity when the context
4050 -- is an enclosing generic. Freezing is an expansion activity, and in
4051 -- addition the renamed entity may depend on the generic formals of
4052 -- the enclosing generic.
4053
4054 if Is_Actual and not Inside_A_Generic then
4055 Freeze_Before (N, Old_S);
4056 Freeze_Actual_Profile;
4057 Set_Has_Delayed_Freeze (New_S, False);
4058 Freeze_Before (N, New_S);
4059
4060 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
4061 and then not Is_Abstract_Subprogram (Formal_Spec)
4062 then
4063 -- An abstract subprogram is only allowed as an actual in the
4064 -- case where the formal subprogram is also abstract.
4065
4066 if Is_Abstract_Subprogram (Old_S) then
4067 Error_Msg_N
4068 ("abstract subprogram not allowed as generic actual", Nam);
4069 end if;
4070
4071 -- AI12-0412: A primitive of an abstract type with Pre'Class
4072 -- or Post'Class aspects specified with nonstatic expressions
4073 -- is not allowed as actual for a nonabstract formal subprogram
4074 -- (see RM 6.1.1(18.2/5).
4075
4076 if Is_Dispatching_Operation (Old_S)
4077 and then
4078 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post (Old_S)
4079 then
4080 Error_Msg_N
4081 ("primitive of abstract type with nonstatic class-wide "
4082 & "pre/postconditions not allowed as actual",
4083 Nam);
4084 end if;
4085 end if;
4086 end if;
4087
4088 else
4089 -- A common error is to assume that implicit operators for types are
4090 -- defined in Standard, or in the scope of a subtype. In those cases
4091 -- where the renamed entity is given with an expanded name, it is
4092 -- worth mentioning that operators for the type are not declared in
4093 -- the scope given by the prefix.
4094
4095 if Nkind (Nam) = N_Expanded_Name
4096 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
4097 and then Scope (Entity (Nam)) = Standard_Standard
4098 then
4099 declare
4100 T : constant Entity_Id :=
4101 Base_Type (Etype (First_Formal (New_S)));
4102 begin
4103 Error_Msg_Node_2 := Prefix (Nam);
4104 Error_Msg_NE
4105 ("operator for type& is not declared in&", Prefix (Nam), T);
4106 end;
4107
4108 else
4109 Error_Msg_NE
4110 ("no visible subprogram matches the specification for&",
4111 Spec, New_S);
4112 end if;
4113
4114 if Present (Candidate_Renaming) then
4115 declare
4116 F1 : Entity_Id;
4117 F2 : Entity_Id;
4118 T1 : Entity_Id;
4119
4120 begin
4121 F1 := First_Formal (Candidate_Renaming);
4122 F2 := First_Formal (New_S);
4123 T1 := First_Subtype (Etype (F1));
4124 while Present (F1) and then Present (F2) loop
4125 Next_Formal (F1);
4126 Next_Formal (F2);
4127 end loop;
4128
4129 if Present (F1) and then Present (Default_Value (F1)) then
4130 if Present (Next_Formal (F1)) then
4131 Error_Msg_NE
4132 ("\missing specification for & and other formals with "
4133 & "defaults", Spec, F1);
4134 else
4135 Error_Msg_NE ("\missing specification for &", Spec, F1);
4136 end if;
4137 end if;
4138
4139 if Nkind (Nam) = N_Operator_Symbol
4140 and then From_Default (N)
4141 then
4142 Error_Msg_Node_2 := T1;
4143 Error_Msg_NE
4144 ("default & on & is not directly visible", Nam, Nam);
4145 end if;
4146 end;
4147 end if;
4148 end if;
4149
4150 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
4151 -- controlling access parameters are known non-null for the renamed
4152 -- subprogram. Test also applies to a subprogram instantiation that
4153 -- is dispatching. Test is skipped if some previous error was detected
4154 -- that set Old_S to Any_Id.
4155
4156 if Ada_Version >= Ada_2005
4157 and then Old_S /= Any_Id
4158 and then not Is_Dispatching_Operation (Old_S)
4159 and then Is_Dispatching_Operation (New_S)
4160 then
4161 declare
4162 Old_F : Entity_Id;
4163 New_F : Entity_Id;
4164
4165 begin
4166 Old_F := First_Formal (Old_S);
4167 New_F := First_Formal (New_S);
4168 while Present (Old_F) loop
4169 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
4170 and then Is_Controlling_Formal (New_F)
4171 and then not Can_Never_Be_Null (Old_F)
4172 then
4173 Error_Msg_N ("access parameter is controlling,", New_F);
4174 Error_Msg_NE
4175 ("\corresponding parameter of& must be explicitly null "
4176 & "excluding", New_F, Old_S);
4177 end if;
4178
4179 Next_Formal (Old_F);
4180 Next_Formal (New_F);
4181 end loop;
4182 end;
4183 end if;
4184
4185 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
4186 -- is to warn if an operator is being renamed as a different operator.
4187 -- If the operator is predefined, examine the kind of the entity, not
4188 -- the abbreviated declaration in Standard.
4189
4190 if Comes_From_Source (N)
4191 and then Present (Old_S)
4192 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
4193 or else Ekind (Old_S) = E_Operator)
4194 and then Nkind (New_S) = N_Defining_Operator_Symbol
4195 and then Chars (Old_S) /= Chars (New_S)
4196 then
4197 Error_Msg_NE
4198 ("& is being renamed as a different operator??", N, Old_S);
4199 end if;
4200
4201 -- Check for renaming of obsolescent subprogram
4202
4203 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
4204
4205 -- Another warning or some utility: if the new subprogram as the same
4206 -- name as the old one, the old one is not hidden by an outer homograph,
4207 -- the new one is not a public symbol, and the old one is otherwise
4208 -- directly visible, the renaming is superfluous.
4209
4210 if Chars (Old_S) = Chars (New_S)
4211 and then Comes_From_Source (N)
4212 and then Scope (Old_S) /= Standard_Standard
4213 and then Warn_On_Redundant_Constructs
4214 and then (Is_Immediately_Visible (Old_S)
4215 or else Is_Potentially_Use_Visible (Old_S))
4216 and then Is_Overloadable (Current_Scope)
4217 and then Chars (Current_Scope) /= Chars (Old_S)
4218 then
4219 Error_Msg_N
4220 ("redundant renaming, entity is directly visible?r?", Name (N));
4221 end if;
4222
4223 -- Implementation-defined aspect specifications can appear in a renaming
4224 -- declaration, but not language-defined ones. The call to procedure
4225 -- Analyze_Aspect_Specifications will take care of this error check.
4226
4227 Analyze_Aspect_Specifications (N, New_S);
4228
4229 -- AI12-0279
4230
4231 if Is_Actual
4232 and then Has_Yield_Aspect (Formal_Spec)
4233 and then not Has_Yield_Aspect (Old_S)
4234 then
4235 Error_Msg_Name_1 := Name_Yield;
4236 Error_Msg_N
4237 ("actual subprogram& must have aspect% to match formal", Name (N));
4238 end if;
4239
4240 Ada_Version := Save_AV;
4241 Ada_Version_Pragma := Save_AVP;
4242 Ada_Version_Explicit := Save_AV_Exp;
4243
4244 -- Check if we are looking at an Ada 2012 defaulted formal subprogram
4245 -- and mark any use_package_clauses that affect the visibility of the
4246 -- implicit generic actual.
4247
4248 -- Also, we may be looking at an internal renaming of a user-defined
4249 -- subprogram created for a generic formal subprogram association,
4250 -- which will also have to be marked here. This can occur when the
4251 -- corresponding formal subprogram contains references to other generic
4252 -- formals.
4253
4254 if Is_Generic_Actual_Subprogram (New_S)
4255 and then (Is_Intrinsic_Subprogram (New_S)
4256 or else From_Default (N)
4257 or else Nkind (N) = N_Subprogram_Renaming_Declaration)
4258 then
4259 Mark_Use_Clauses (New_S);
4260
4261 -- Handle overloaded subprograms
4262
4263 if Present (Alias (New_S)) then
4264 Mark_Use_Clauses (Alias (New_S));
4265 end if;
4266 end if;
4267
4268 if Is_Actual then
4269 Local_Restrict.Check_Actual_Subprogram_For_Instance
4270 (Actual_Subp_Name => Nam, Formal_Subp => Formal_Spec);
4271 end if;
4272 end Analyze_Subprogram_Renaming;
4273
4274 -------------------------
4275 -- Analyze_Use_Package --
4276 -------------------------
4277
4278 -- Resolve the package names in the use clause, and make all the visible
4279 -- entities defined in the package potentially use-visible. If the package
4280 -- is already in use from a previous use clause, its visible entities are
4281 -- already use-visible. In that case, mark the occurrence as a redundant
4282 -- use. If the package is an open scope, i.e. if the use clause occurs
4283 -- within the package itself, ignore it.
4284
4285 procedure Analyze_Use_Package (N : Node_Id; Chain : Boolean := True) is
4286 procedure Analyze_Package_Name (Clause : Node_Id);
4287 -- Perform analysis on a package name from a use_package_clause
4288
4289 procedure Analyze_Package_Name_List (Head_Clause : Node_Id);
4290 -- Similar to Analyze_Package_Name but iterates over all the names
4291 -- in a use clause.
4292
4293 --------------------------
4294 -- Analyze_Package_Name --
4295 --------------------------
4296
4297 procedure Analyze_Package_Name (Clause : Node_Id) is
4298 Pack : constant Node_Id := Name (Clause);
4299 Pref : Node_Id;
4300
4301 begin
4302 pragma Assert (Nkind (Clause) = N_Use_Package_Clause);
4303 Analyze (Pack);
4304
4305 -- Verify that the package standard is not directly named in a
4306 -- use_package_clause.
4307
4308 if Nkind (Parent (Clause)) = N_Compilation_Unit
4309 and then Nkind (Pack) = N_Expanded_Name
4310 then
4311 Pref := Prefix (Pack);
4312
4313 while Nkind (Pref) = N_Expanded_Name loop
4314 Pref := Prefix (Pref);
4315 end loop;
4316
4317 if Entity (Pref) = Standard_Standard then
4318 Error_Msg_N
4319 ("predefined package Standard cannot appear in a context "
4320 & "clause", Pref);
4321 end if;
4322 end if;
4323 end Analyze_Package_Name;
4324
4325 -------------------------------
4326 -- Analyze_Package_Name_List --
4327 -------------------------------
4328
4329 procedure Analyze_Package_Name_List (Head_Clause : Node_Id) is
4330 Curr : Node_Id;
4331
4332 begin
4333 -- Due to the way source use clauses are split during parsing we are
4334 -- forced to simply iterate through all entities in scope until the
4335 -- clause representing the last name in the list is found.
4336
4337 Curr := Head_Clause;
4338 while Present (Curr) loop
4339 Analyze_Package_Name (Curr);
4340
4341 -- Stop iterating over the names in the use clause when we are at
4342 -- the last one.
4343
4344 exit when not More_Ids (Curr) and then Prev_Ids (Curr);
4345 Next (Curr);
4346 end loop;
4347 end Analyze_Package_Name_List;
4348
4349 -- Local variables
4350
4351 Pack : Entity_Id;
4352
4353 -- Start of processing for Analyze_Use_Package
4354
4355 begin
4356 Set_Hidden_By_Use_Clause (N, No_Elist);
4357
4358 -- Use clause not allowed in a spec of a predefined package declaration
4359 -- except that packages whose file name starts a-n are OK (these are
4360 -- children of Ada.Numerics, which are never loaded by Rtsfind).
4361
4362 if Is_Predefined_Unit (Current_Sem_Unit)
4363 and then Get_Name_String
4364 (Unit_File_Name (Current_Sem_Unit)) (1 .. 3) /= "a-n"
4365 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
4366 N_Package_Declaration
4367 then
4368 Error_Msg_N ("use clause not allowed in predefined spec", N);
4369 end if;
4370
4371 -- Loop through all package names from the original use clause in
4372 -- order to analyze referenced packages. A use_package_clause with only
4373 -- one name does not have More_Ids or Prev_Ids set, while a clause with
4374 -- More_Ids only starts the chain produced by the parser.
4375
4376 if not More_Ids (N) and then not Prev_Ids (N) then
4377 Analyze_Package_Name (N);
4378
4379 elsif More_Ids (N) and then not Prev_Ids (N) then
4380 Analyze_Package_Name_List (N);
4381 end if;
4382
4383 if not Is_Entity_Name (Name (N)) then
4384 Error_Msg_N ("& is not a package", Name (N));
4385
4386 return;
4387 end if;
4388
4389 if Chain then
4390 Chain_Use_Clause (N);
4391 end if;
4392
4393 Pack := Entity (Name (N));
4394
4395 -- There are many cases where scopes are manipulated during analysis, so
4396 -- check that Pack's current use clause has not already been chained
4397 -- before setting its previous use clause.
4398
4399 if Ekind (Pack) = E_Package
4400 and then Present (Current_Use_Clause (Pack))
4401 and then Current_Use_Clause (Pack) /= N
4402 and then No (Prev_Use_Clause (N))
4403 and then Prev_Use_Clause (Current_Use_Clause (Pack)) /= N
4404 then
4405 Set_Prev_Use_Clause (N, Current_Use_Clause (Pack));
4406 end if;
4407
4408 -- Mark all entities as potentially use visible
4409
4410 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
4411 if Ekind (Pack) = E_Generic_Package then
4412 Error_Msg_N -- CODEFIX
4413 ("a generic package is not allowed in a use clause", Name (N));
4414
4415 elsif Is_Generic_Subprogram (Pack) then
4416 Error_Msg_N -- CODEFIX
4417 ("a generic subprogram is not allowed in a use clause",
4418 Name (N));
4419
4420 elsif Is_Subprogram (Pack) then
4421 Error_Msg_N -- CODEFIX
4422 ("a subprogram is not allowed in a use clause", Name (N));
4423
4424 else
4425 Error_Msg_N ("& is not allowed in a use clause", Name (N));
4426 end if;
4427
4428 else
4429 if Nkind (Parent (N)) = N_Compilation_Unit then
4430 Check_In_Previous_With_Clause (N, Name (N));
4431 end if;
4432
4433 Use_One_Package (N, Name (N));
4434 end if;
4435
4436 Mark_Ghost_Clause (N);
4437 end Analyze_Use_Package;
4438
4439 ----------------------
4440 -- Analyze_Use_Type --
4441 ----------------------
4442
4443 procedure Analyze_Use_Type (N : Node_Id; Chain : Boolean := True) is
4444 E : Entity_Id;
4445 Id : Node_Id;
4446
4447 begin
4448 Set_Hidden_By_Use_Clause (N, No_Elist);
4449
4450 -- Chain clause to list of use clauses in current scope when flagged
4451
4452 if Chain then
4453 Chain_Use_Clause (N);
4454 end if;
4455
4456 -- Obtain the base type of the type denoted within the use_type_clause's
4457 -- subtype mark.
4458
4459 Id := Subtype_Mark (N);
4460 Find_Type (Id);
4461 E := Base_Type (Entity (Id));
4462
4463 -- There are many cases where a use_type_clause may be reanalyzed due to
4464 -- manipulation of the scope stack so we much guard against those cases
4465 -- here, otherwise, we must add the new use_type_clause to the previous
4466 -- use_type_clause chain in order to mark redundant use_type_clauses as
4467 -- used. When the redundant use-type clauses appear in a parent unit and
4468 -- a child unit we must prevent a circularity in the chain that would
4469 -- otherwise result from the separate steps of analysis and installation
4470 -- of the parent context.
4471
4472 if Present (Current_Use_Clause (E))
4473 and then Current_Use_Clause (E) /= N
4474 and then Prev_Use_Clause (Current_Use_Clause (E)) /= N
4475 and then No (Prev_Use_Clause (N))
4476 then
4477 Set_Prev_Use_Clause (N, Current_Use_Clause (E));
4478 end if;
4479
4480 -- If the Used_Operations list is already initialized, the clause has
4481 -- been analyzed previously, and it is being reinstalled, for example
4482 -- when the clause appears in a package spec and we are compiling the
4483 -- corresponding package body. In that case, make the entities on the
4484 -- existing list use_visible, and mark the corresponding types In_Use.
4485
4486 if Present (Used_Operations (N)) then
4487 declare
4488 Elmt : Elmt_Id;
4489
4490 begin
4491 Use_One_Type (Subtype_Mark (N), Installed => True);
4492
4493 Elmt := First_Elmt (Used_Operations (N));
4494 while Present (Elmt) loop
4495 Set_Is_Potentially_Use_Visible (Node (Elmt));
4496 Next_Elmt (Elmt);
4497 end loop;
4498 end;
4499
4500 return;
4501 end if;
4502
4503 -- Otherwise, create new list and attach to it the operations that are
4504 -- made use-visible by the clause.
4505
4506 Set_Used_Operations (N, New_Elmt_List);
4507 E := Entity (Id);
4508
4509 if E /= Any_Type then
4510 Use_One_Type (Id);
4511
4512 if Nkind (Parent (N)) = N_Compilation_Unit then
4513 if Nkind (Id) = N_Identifier then
4514 Error_Msg_N ("type is not directly visible", Id);
4515
4516 elsif Is_Child_Unit (Scope (E))
4517 and then Scope (E) /= System_Aux_Id
4518 then
4519 Check_In_Previous_With_Clause (N, Prefix (Id));
4520 end if;
4521 end if;
4522
4523 else
4524 -- If the use_type_clause appears in a compilation unit context,
4525 -- check whether it comes from a unit that may appear in a
4526 -- limited_with_clause, for a better error message.
4527
4528 if Nkind (Parent (N)) = N_Compilation_Unit
4529 and then Nkind (Id) /= N_Identifier
4530 then
4531 declare
4532 Item : Node_Id;
4533 Pref : Node_Id;
4534
4535 function Mentioned (Nam : Node_Id) return Boolean;
4536 -- Check whether the prefix of expanded name for the type
4537 -- appears in the prefix of some limited_with_clause.
4538
4539 ---------------
4540 -- Mentioned --
4541 ---------------
4542
4543 function Mentioned (Nam : Node_Id) return Boolean is
4544 begin
4545 return Nkind (Name (Item)) = N_Selected_Component
4546 and then Chars (Prefix (Name (Item))) = Chars (Nam);
4547 end Mentioned;
4548
4549 begin
4550 Pref := Prefix (Id);
4551 Item := First (Context_Items (Parent (N)));
4552 while Present (Item) and then Item /= N loop
4553 if Nkind (Item) = N_With_Clause
4554 and then Limited_Present (Item)
4555 and then Mentioned (Pref)
4556 then
4557 Change_Error_Text
4558 (Get_Msg_Id, "premature usage of incomplete type");
4559 end if;
4560
4561 Next (Item);
4562 end loop;
4563 end;
4564 end if;
4565 end if;
4566
4567 Mark_Ghost_Clause (N);
4568 end Analyze_Use_Type;
4569
4570 ------------------------
4571 -- Attribute_Renaming --
4572 ------------------------
4573
4574 procedure Attribute_Renaming (N : Node_Id) is
4575 Loc : constant Source_Ptr := Sloc (N);
4576 Nam : constant Node_Id := Name (N);
4577 Spec : constant Node_Id := Specification (N);
4578 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
4579 Aname : constant Name_Id := Attribute_Name (Nam);
4580
4581 Form_Num : Nat := 0;
4582 Expr_List : List_Id := No_List;
4583
4584 Attr_Node : Node_Id;
4585 Body_Node : Node_Id;
4586 Param_Spec : Node_Id;
4587
4588 begin
4589 Generate_Definition (New_S);
4590
4591 -- This procedure is called in the context of subprogram renaming, and
4592 -- thus the attribute must be one that is a subprogram. All of those
4593 -- have at least one formal parameter, with the exceptions of the GNAT
4594 -- attribute 'Img, which GNAT treats as renameable.
4595
4596 if Is_Empty_List (Parameter_Specifications (Spec)) then
4597 if Aname /= Name_Img then
4598 Error_Msg_N
4599 ("subprogram renaming an attribute must have formals", N);
4600 return;
4601 end if;
4602
4603 else
4604 Param_Spec := First (Parameter_Specifications (Spec));
4605 while Present (Param_Spec) loop
4606 Form_Num := Form_Num + 1;
4607
4608 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4609 Find_Type (Parameter_Type (Param_Spec));
4610
4611 -- The profile of the new entity denotes the base type (s) of
4612 -- the types given in the specification. For access parameters
4613 -- there are no subtypes involved.
4614
4615 Rewrite (Parameter_Type (Param_Spec),
4616 New_Occurrence_Of
4617 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
4618 end if;
4619
4620 if No (Expr_List) then
4621 Expr_List := New_List;
4622 end if;
4623
4624 Append_To (Expr_List,
4625 Make_Identifier (Loc,
4626 Chars => Chars (Defining_Identifier (Param_Spec))));
4627
4628 -- The expressions in the attribute reference are not freeze
4629 -- points. Neither is the attribute as a whole, see below.
4630
4631 Set_Must_Not_Freeze (Last (Expr_List));
4632 Next (Param_Spec);
4633 end loop;
4634 end if;
4635
4636 -- Immediate error if too many formals. Other mismatches in number or
4637 -- types of parameters are detected when we analyze the body of the
4638 -- subprogram that we construct.
4639
4640 if Form_Num > 2 then
4641 Error_Msg_N ("too many formals for attribute", N);
4642
4643 -- Error if the attribute reference has expressions that look like
4644 -- formal parameters.
4645
4646 elsif Present (Expressions (Nam)) then
4647 Error_Msg_N ("illegal expressions in attribute reference", Nam);
4648
4649 elsif Aname in Name_Compose | Name_Exponent | Name_Leading_Part |
4650 Name_Pos | Name_Round | Name_Scaling |
4651 Name_Val
4652 then
4653 if Nkind (N) = N_Subprogram_Renaming_Declaration
4654 and then Present (Corresponding_Formal_Spec (N))
4655 then
4656 Error_Msg_N
4657 ("generic actual cannot be attribute involving universal type",
4658 Nam);
4659 else
4660 Error_Msg_N
4661 ("attribute involving a universal type cannot be renamed",
4662 Nam);
4663 end if;
4664 end if;
4665
4666 -- Rewrite attribute node to have a list of expressions corresponding to
4667 -- the subprogram formals. A renaming declaration is not a freeze point,
4668 -- and the analysis of the attribute reference should not freeze the
4669 -- type of the prefix. We use the original node in the renaming so that
4670 -- its source location is preserved, and checks on stream attributes are
4671 -- properly applied.
4672
4673 Attr_Node := Relocate_Node (Nam);
4674 Set_Expressions (Attr_Node, Expr_List);
4675
4676 Set_Must_Not_Freeze (Attr_Node);
4677 Set_Must_Not_Freeze (Prefix (Nam));
4678
4679 -- Case of renaming a function
4680
4681 if Nkind (Spec) = N_Function_Specification then
4682 if Is_Procedure_Attribute_Name (Aname) then
4683 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
4684 return;
4685 end if;
4686
4687 Find_Type (Result_Definition (Spec));
4688 Rewrite (Result_Definition (Spec),
4689 New_Occurrence_Of
4690 (Base_Type (Entity (Result_Definition (Spec))), Loc));
4691
4692 Body_Node :=
4693 Make_Subprogram_Body (Loc,
4694 Specification => Spec,
4695 Declarations => New_List,
4696 Handled_Statement_Sequence =>
4697 Make_Handled_Sequence_Of_Statements (Loc,
4698 Statements => New_List (
4699 Make_Simple_Return_Statement (Loc,
4700 Expression => Attr_Node))));
4701
4702 -- Case of renaming a procedure
4703
4704 else
4705 if not Is_Procedure_Attribute_Name (Aname) then
4706 Error_Msg_N ("attribute can only be renamed as function", Nam);
4707 return;
4708 end if;
4709
4710 Body_Node :=
4711 Make_Subprogram_Body (Loc,
4712 Specification => Spec,
4713 Declarations => New_List,
4714 Handled_Statement_Sequence =>
4715 Make_Handled_Sequence_Of_Statements (Loc,
4716 Statements => New_List (Attr_Node)));
4717 end if;
4718
4719 -- Signal the ABE mechanism that the generated subprogram body has not
4720 -- ABE ramifications.
4721
4722 Set_Was_Attribute_Reference (Body_Node);
4723
4724 -- In case of tagged types we add the body of the generated function to
4725 -- the freezing actions of the type (because in the general case such
4726 -- type is still not frozen). We exclude from this processing generic
4727 -- formal subprograms found in instantiations.
4728
4729 -- We must exclude restricted run-time libraries because
4730 -- entity AST_Handler is defined in package System.Aux_Dec which is not
4731 -- available in those platforms. Note that we cannot use the function
4732 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
4733 -- the ZFP run-time library is not defined as a profile, and we do not
4734 -- want to deal with AST_Handler in ZFP mode.
4735
4736 if not Configurable_Run_Time_Mode
4737 and then No (Corresponding_Formal_Spec (N))
4738 and then not Is_RTE (Etype (Nam), RE_AST_Handler)
4739 then
4740 declare
4741 P : constant Node_Id := Prefix (Nam);
4742
4743 begin
4744 -- The prefix of 'Img is an object that is evaluated for each call
4745 -- of the function that renames it.
4746
4747 if Aname = Name_Img then
4748 Preanalyze_And_Resolve (P);
4749
4750 -- For all other attribute renamings, the prefix is a subtype
4751
4752 else
4753 Find_Type (P);
4754 end if;
4755
4756 -- If the target type is not yet frozen, add the body to the
4757 -- actions to be elaborated at freeze time.
4758
4759 if Is_Tagged_Type (Etype (P))
4760 and then In_Open_Scopes (Scope (Etype (P)))
4761 then
4762 Append_Freeze_Action (Etype (P), Body_Node);
4763 else
4764 Rewrite (N, Body_Node);
4765 Analyze (N);
4766 Set_Etype (New_S, Base_Type (Etype (New_S)));
4767 end if;
4768 end;
4769
4770 -- Generic formal subprograms or AST_Handler renaming
4771
4772 else
4773 Rewrite (N, Body_Node);
4774 Analyze (N);
4775 Set_Etype (New_S, Base_Type (Etype (New_S)));
4776 end if;
4777
4778 if Is_Compilation_Unit (New_S) then
4779 Error_Msg_N
4780 ("a library unit can only rename another library unit", N);
4781 end if;
4782
4783 -- We suppress elaboration warnings for the resulting entity, since
4784 -- clearly they are not needed, and more particularly, in the case
4785 -- of a generic formal subprogram, the resulting entity can appear
4786 -- after the instantiation itself, and thus look like a bogus case
4787 -- of access before elaboration.
4788
4789 if Legacy_Elaboration_Checks then
4790 Set_Suppress_Elaboration_Warnings (New_S);
4791 end if;
4792 end Attribute_Renaming;
4793
4794 ----------------------
4795 -- Chain_Use_Clause --
4796 ----------------------
4797
4798 procedure Chain_Use_Clause (N : Node_Id) is
4799 Level : Int := Scope_Stack.Last;
4800 Pack : Entity_Id;
4801
4802 begin
4803 -- Common case
4804
4805 if not Is_Compilation_Unit (Current_Scope)
4806 or else not Is_Child_Unit (Current_Scope)
4807 then
4808 null;
4809
4810 -- Common case for compilation unit
4811
4812 elsif Defining_Entity (Parent (N)) = Current_Scope then
4813 null;
4814
4815 else
4816 -- If declaration appears in some other scope, it must be in some
4817 -- parent unit when compiling a child.
4818
4819 Pack := Defining_Entity (Parent (N));
4820
4821 if not In_Open_Scopes (Pack) then
4822 null;
4823
4824 -- If the use clause appears in an ancestor and we are in the
4825 -- private part of the immediate parent, the use clauses are
4826 -- already installed.
4827
4828 elsif Pack /= Scope (Current_Scope)
4829 and then In_Private_Part (Scope (Current_Scope))
4830 then
4831 null;
4832
4833 else
4834 -- Find entry for parent unit in scope stack
4835
4836 while Scope_Stack.Table (Level).Entity /= Pack loop
4837 Level := Level - 1;
4838 end loop;
4839 end if;
4840 end if;
4841
4842 Set_Next_Use_Clause (N,
4843 Scope_Stack.Table (Level).First_Use_Clause);
4844 Scope_Stack.Table (Level).First_Use_Clause := N;
4845 end Chain_Use_Clause;
4846
4847 ---------------------------
4848 -- Check_Frozen_Renaming --
4849 ---------------------------
4850
4851 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
4852 B_Node : Node_Id;
4853 Old_S : Entity_Id;
4854
4855 begin
4856 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
4857 B_Node :=
4858 Build_Renamed_Body
4859 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
4860
4861 if Is_Entity_Name (Name (N)) then
4862 Old_S := Entity (Name (N));
4863
4864 if not Is_Frozen (Old_S)
4865 and then Operating_Mode /= Check_Semantics
4866 then
4867 Append_Freeze_Action (Old_S, B_Node);
4868 else
4869 Insert_After (N, B_Node);
4870 Analyze (B_Node);
4871 end if;
4872
4873 if Is_Intrinsic_Subprogram (Old_S)
4874 and then not In_Instance
4875 and then not Relaxed_RM_Semantics
4876 then
4877 Error_Msg_N
4878 ("subprogram used in renaming_as_body cannot be intrinsic",
4879 Name (N));
4880 end if;
4881
4882 else
4883 Insert_After (N, B_Node);
4884 Analyze (B_Node);
4885 end if;
4886 end if;
4887 end Check_Frozen_Renaming;
4888
4889 -------------------------------
4890 -- Set_Entity_Or_Discriminal --
4891 -------------------------------
4892
4893 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4894 P : Node_Id;
4895
4896 begin
4897 -- If the entity is not a discriminant, or else expansion is disabled,
4898 -- simply set the entity.
4899
4900 if not In_Spec_Expression
4901 or else Ekind (E) /= E_Discriminant
4902 or else Inside_A_Generic
4903 then
4904 Set_Entity_With_Checks (N, E);
4905
4906 -- The replacement of a discriminant by the corresponding discriminal
4907 -- is not done for a task discriminant that appears in a default
4908 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4909 -- for details on their handling.
4910
4911 elsif Is_Concurrent_Type (Scope (E)) then
4912 P := Parent (N);
4913 while Present (P)
4914 and then Nkind (P) not in
4915 N_Parameter_Specification | N_Component_Declaration
4916 loop
4917 P := Parent (P);
4918 end loop;
4919
4920 if Present (P)
4921 and then Nkind (P) = N_Parameter_Specification
4922 then
4923 null;
4924
4925 -- Don't replace a non-qualified discriminant in preanalysis mode
4926 -- since we may be preanalyzing a subtree that it is still not
4927 -- placed in its final position in the tree, and therefore it can
4928 -- lead to errors during full analysis when the discriminant gets
4929 -- referenced later. We exclude preanalysis of pragma expressions
4930 -- because they will not be moved.
4931
4932 -- This can occur in situations where a protected type contains
4933 -- an expression function which references a non-prefixed
4934 -- discriminant, or a task type sets the size of its secondary
4935 -- stack with a value provided in a discriminant.
4936
4937 elsif No (P)
4938 and then Preanalysis_Active
4939 and then not In_Pragma_Expression (N)
4940 then
4941 null;
4942
4943 else
4944 Set_Entity (N, Discriminal (E));
4945 end if;
4946
4947 -- Otherwise, this is a discriminant in a context in which
4948 -- it is a reference to the corresponding parameter of the
4949 -- init proc for the enclosing type.
4950
4951 else
4952 Set_Entity (N, Discriminal (E));
4953 end if;
4954 end Set_Entity_Or_Discriminal;
4955
4956 -----------------------------------
4957 -- Check_In_Previous_With_Clause --
4958 -----------------------------------
4959
4960 procedure Check_In_Previous_With_Clause (N, Nam : Node_Id) is
4961 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4962 Item : Node_Id;
4963 Par : Node_Id;
4964
4965 begin
4966 Item := First (Context_Items (Parent (N)));
4967 while Present (Item) and then Item /= N loop
4968 if Nkind (Item) = N_With_Clause
4969
4970 -- Protect the frontend against previous critical errors
4971
4972 and then Nkind (Name (Item)) /= N_Selected_Component
4973 and then Entity (Name (Item)) = Pack
4974 then
4975 Par := Nam;
4976
4977 -- Find root library unit in with_clause
4978
4979 while Nkind (Par) = N_Expanded_Name loop
4980 Par := Prefix (Par);
4981 end loop;
4982
4983 if Is_Child_Unit (Entity (Original_Node (Par))) then
4984 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4985 else
4986 return;
4987 end if;
4988 end if;
4989
4990 Next (Item);
4991 end loop;
4992
4993 -- On exit, package is not mentioned in a previous with_clause.
4994 -- Check if its prefix is.
4995
4996 if Nkind (Nam) = N_Expanded_Name then
4997 Check_In_Previous_With_Clause (N, Prefix (Nam));
4998
4999 elsif Pack /= Any_Id then
5000 Error_Msg_NE ("& is not visible", Nam, Pack);
5001 end if;
5002 end Check_In_Previous_With_Clause;
5003
5004 ---------------------------------
5005 -- Check_Library_Unit_Renaming --
5006 ---------------------------------
5007
5008 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
5009 New_E : Entity_Id;
5010
5011 begin
5012 if Nkind (Parent (N)) /= N_Compilation_Unit then
5013 return;
5014
5015 -- Check for library unit. Note that we used to check for the scope
5016 -- being Standard here, but that was wrong for Standard itself.
5017
5018 elsif not Is_Compilation_Unit (Old_E)
5019 and then not Is_Child_Unit (Old_E)
5020 then
5021 Error_Msg_N ("renamed unit must be a library unit", Name (N));
5022
5023 -- Entities defined in Standard (operators and boolean literals) cannot
5024 -- be renamed as library units.
5025
5026 elsif Scope (Old_E) = Standard_Standard
5027 and then Sloc (Old_E) = Standard_Location
5028 then
5029 Error_Msg_N ("renamed unit must be a library unit", Name (N));
5030
5031 elsif Present (Parent_Spec (N))
5032 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
5033 and then not Is_Child_Unit (Old_E)
5034 then
5035 Error_Msg_N
5036 ("renamed unit must be a child unit of generic parent", Name (N));
5037
5038 elsif Nkind (N) in N_Generic_Renaming_Declaration
5039 and then Nkind (Name (N)) = N_Expanded_Name
5040 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
5041 and then Is_Generic_Unit (Old_E)
5042 then
5043 Error_Msg_N
5044 ("renamed generic unit must be a library unit", Name (N));
5045
5046 elsif Is_Package_Or_Generic_Package (Old_E) then
5047
5048 -- Inherit categorization flags
5049
5050 New_E := Defining_Entity (N);
5051 Set_Is_Pure (New_E, Is_Pure (Old_E));
5052 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
5053 Set_Is_Remote_Call_Interface (New_E,
5054 Is_Remote_Call_Interface (Old_E));
5055 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
5056 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
5057 end if;
5058 end Check_Library_Unit_Renaming;
5059
5060 ------------------------
5061 -- Enclosing_Instance --
5062 ------------------------
5063
5064 function Enclosing_Instance return Entity_Id is
5065 S : Entity_Id;
5066
5067 begin
5068 if not Is_Generic_Instance (Current_Scope) then
5069 return Empty;
5070 end if;
5071
5072 S := Scope (Current_Scope);
5073 while S /= Standard_Standard loop
5074 if Is_Generic_Instance (S) then
5075 return S;
5076 end if;
5077
5078 S := Scope (S);
5079 end loop;
5080
5081 return Empty;
5082 end Enclosing_Instance;
5083
5084 ---------------
5085 -- End_Scope --
5086 ---------------
5087
5088 procedure End_Scope is
5089 Id : Entity_Id;
5090 Prev : Entity_Id;
5091 Outer : Entity_Id;
5092
5093 begin
5094 Id := First_Entity (Current_Scope);
5095 while Present (Id) loop
5096 -- An entity in the current scope is not necessarily the first one
5097 -- on its homonym chain. Find its predecessor if any,
5098 -- If it is an internal entity, it will not be in the visibility
5099 -- chain altogether, and there is nothing to unchain.
5100
5101 if Id /= Current_Entity (Id) then
5102 Prev := Current_Entity (Id);
5103 while Present (Prev)
5104 and then Homonym (Prev) /= Id
5105 loop
5106 Prev := Homonym (Prev);
5107 end loop;
5108
5109 -- Skip to end of loop if Id is not in the visibility chain
5110
5111 if No (Prev) then
5112 goto Next_Ent;
5113 end if;
5114
5115 else
5116 Prev := Empty;
5117 end if;
5118
5119 Set_Is_Immediately_Visible (Id, False);
5120
5121 Outer := Homonym (Id);
5122 while Present (Outer) and then Scope (Outer) = Current_Scope loop
5123 Outer := Homonym (Outer);
5124 end loop;
5125
5126 -- Reset homonym link of other entities, but do not modify link
5127 -- between entities in current scope, so that the back-end can have
5128 -- a proper count of local overloadings.
5129
5130 if No (Prev) then
5131 Set_Name_Entity_Id (Chars (Id), Outer);
5132
5133 elsif Scope (Prev) /= Scope (Id) then
5134 Set_Homonym (Prev, Outer);
5135 end if;
5136
5137 <<Next_Ent>>
5138 Next_Entity (Id);
5139 end loop;
5140
5141 -- If the scope generated freeze actions, place them before the
5142 -- current declaration and analyze them. Type declarations and
5143 -- the bodies of initialization procedures can generate such nodes.
5144 -- We follow the parent chain until we reach a list node, which is
5145 -- the enclosing list of declarations. If the list appears within
5146 -- a protected definition, move freeze nodes outside the protected
5147 -- type altogether.
5148
5149 if Present
5150 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
5151 then
5152 declare
5153 Decl : Node_Id;
5154 L : constant List_Id := Scope_Stack.Table
5155 (Scope_Stack.Last).Pending_Freeze_Actions;
5156
5157 begin
5158 if Is_Itype (Current_Scope) then
5159 Decl := Associated_Node_For_Itype (Current_Scope);
5160 else
5161 Decl := Parent (Current_Scope);
5162 end if;
5163
5164 Pop_Scope;
5165
5166 while not Is_List_Member (Decl)
5167 or else Nkind (Parent (Decl)) in N_Protected_Definition
5168 | N_Task_Definition
5169 loop
5170 Decl := Parent (Decl);
5171 end loop;
5172
5173 Insert_List_Before_And_Analyze (Decl, L);
5174 end;
5175
5176 else
5177 Pop_Scope;
5178 end if;
5179 end End_Scope;
5180
5181 ---------------------
5182 -- End_Use_Clauses --
5183 ---------------------
5184
5185 procedure End_Use_Clauses (Clause : Node_Id) is
5186 U : Node_Id;
5187
5188 begin
5189 -- Remove use_type_clauses first, because they affect the visibility of
5190 -- operators in subsequent used packages.
5191
5192 U := Clause;
5193 while Present (U) loop
5194 if Nkind (U) = N_Use_Type_Clause then
5195 End_Use_Type (U);
5196 end if;
5197
5198 Next_Use_Clause (U);
5199 end loop;
5200
5201 U := Clause;
5202 while Present (U) loop
5203 if Nkind (U) = N_Use_Package_Clause then
5204 End_Use_Package (U);
5205 end if;
5206
5207 Next_Use_Clause (U);
5208 end loop;
5209 end End_Use_Clauses;
5210
5211 ---------------------
5212 -- End_Use_Package --
5213 ---------------------
5214
5215 procedure End_Use_Package (N : Node_Id) is
5216 Pack : Entity_Id;
5217 Pack_Name : Node_Id;
5218 Id : Entity_Id;
5219 Elmt : Elmt_Id;
5220
5221 function Is_Primitive_Operator_In_Use
5222 (Op : Entity_Id;
5223 F : Entity_Id) return Boolean;
5224 -- Check whether Op is a primitive operator of a use-visible type
5225
5226 ----------------------------------
5227 -- Is_Primitive_Operator_In_Use --
5228 ----------------------------------
5229
5230 function Is_Primitive_Operator_In_Use
5231 (Op : Entity_Id;
5232 F : Entity_Id) return Boolean
5233 is
5234 T : constant Entity_Id := Base_Type (Etype (F));
5235 begin
5236 return In_Use (T) and then Scope (T) = Scope (Op);
5237 end Is_Primitive_Operator_In_Use;
5238
5239 -- Start of processing for End_Use_Package
5240
5241 begin
5242 Pack_Name := Name (N);
5243
5244 -- Test that Pack_Name actually denotes a package before processing
5245
5246 if Is_Entity_Name (Pack_Name)
5247 and then Ekind (Entity (Pack_Name)) = E_Package
5248 then
5249 Pack := Entity (Pack_Name);
5250
5251 if In_Open_Scopes (Pack) then
5252 null;
5253
5254 elsif not Redundant_Use (Pack_Name) then
5255 Set_In_Use (Pack, False);
5256 Set_Current_Use_Clause (Pack, Empty);
5257
5258 Id := First_Entity (Pack);
5259 while Present (Id) loop
5260
5261 -- Preserve use-visibility of operators that are primitive
5262 -- operators of a type that is use-visible through an active
5263 -- use_type_clause.
5264
5265 if Nkind (Id) = N_Defining_Operator_Symbol
5266 and then
5267 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
5268 or else
5269 (Present (Next_Formal (First_Formal (Id)))
5270 and then
5271 Is_Primitive_Operator_In_Use
5272 (Id, Next_Formal (First_Formal (Id)))))
5273 then
5274 null;
5275 else
5276 Set_Is_Potentially_Use_Visible (Id, False);
5277 end if;
5278
5279 if Is_Private_Type (Id)
5280 and then Present (Full_View (Id))
5281 then
5282 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
5283 end if;
5284
5285 Next_Entity (Id);
5286 end loop;
5287
5288 if Present (Renamed_Entity (Pack)) then
5289 Set_In_Use (Renamed_Entity (Pack), False);
5290 Set_Current_Use_Clause (Renamed_Entity (Pack), Empty);
5291 end if;
5292
5293 if Chars (Pack) = Name_System
5294 and then Scope (Pack) = Standard_Standard
5295 and then Present_System_Aux
5296 then
5297 Id := First_Entity (System_Aux_Id);
5298 while Present (Id) loop
5299 Set_Is_Potentially_Use_Visible (Id, False);
5300
5301 if Is_Private_Type (Id)
5302 and then Present (Full_View (Id))
5303 then
5304 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
5305 end if;
5306
5307 Next_Entity (Id);
5308 end loop;
5309
5310 Set_In_Use (System_Aux_Id, False);
5311 end if;
5312 else
5313 Set_Redundant_Use (Pack_Name, False);
5314 end if;
5315 end if;
5316
5317 if Present (Hidden_By_Use_Clause (N)) then
5318 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
5319 while Present (Elmt) loop
5320 declare
5321 E : constant Entity_Id := Node (Elmt);
5322
5323 begin
5324 -- Reset either Use_Visibility or Direct_Visibility, depending
5325 -- on how the entity was hidden by the use clause.
5326
5327 if In_Use (Scope (E))
5328 and then Used_As_Generic_Actual (Scope (E))
5329 then
5330 Set_Is_Potentially_Use_Visible (Node (Elmt));
5331 else
5332 Set_Is_Immediately_Visible (Node (Elmt));
5333 end if;
5334
5335 Next_Elmt (Elmt);
5336 end;
5337 end loop;
5338
5339 Set_Hidden_By_Use_Clause (N, No_Elist);
5340 end if;
5341 end End_Use_Package;
5342
5343 ------------------
5344 -- End_Use_Type --
5345 ------------------
5346
5347 procedure End_Use_Type (N : Node_Id) is
5348 Elmt : Elmt_Id;
5349 Id : Entity_Id;
5350 T : Entity_Id;
5351
5352 -- Start of processing for End_Use_Type
5353
5354 begin
5355 Id := Subtype_Mark (N);
5356
5357 -- A call to Rtsfind may occur while analyzing a use_type_clause, in
5358 -- which case the type marks are not resolved yet, so guard against that
5359 -- here.
5360
5361 if Is_Entity_Name (Id) and then Present (Entity (Id)) then
5362 T := Entity (Id);
5363
5364 if T = Any_Type or else From_Limited_With (T) then
5365 null;
5366
5367 -- Note that the use_type_clause may mention a subtype of the type
5368 -- whose primitive operations have been made visible. Here as
5369 -- elsewhere, it is the base type that matters for visibility.
5370
5371 elsif In_Open_Scopes (Scope (Base_Type (T))) then
5372 null;
5373
5374 elsif not Redundant_Use (Id) then
5375 Set_In_Use (T, False);
5376 Set_In_Use (Base_Type (T), False);
5377 Set_Current_Use_Clause (T, Empty);
5378 Set_Current_Use_Clause (Base_Type (T), Empty);
5379
5380 -- See Use_One_Type for the rationale. This is a bit on the naive
5381 -- side, but should be good enough in practice.
5382
5383 if Is_Tagged_Type (T) then
5384 Set_In_Use (Class_Wide_Type (T), False);
5385 end if;
5386 end if;
5387 end if;
5388
5389 if Is_Empty_Elmt_List (Used_Operations (N)) then
5390 return;
5391
5392 else
5393 Elmt := First_Elmt (Used_Operations (N));
5394 while Present (Elmt) loop
5395 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
5396 Next_Elmt (Elmt);
5397 end loop;
5398 end if;
5399 end End_Use_Type;
5400
5401 --------------------
5402 -- Entity_Of_Unit --
5403 --------------------
5404
5405 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
5406 begin
5407 if Nkind (U) = N_Package_Instantiation and then Analyzed (U) then
5408 return Defining_Entity (Instance_Spec (U));
5409 else
5410 return Defining_Entity (U);
5411 end if;
5412 end Entity_Of_Unit;
5413
5414 --------------------------------------
5415 -- Error_Missing_With_Of_Known_Unit --
5416 --------------------------------------
5417
5418 procedure Error_Missing_With_Of_Known_Unit (Pkg : Node_Id) is
5419 Selectors : array (1 .. 6) of Node_Id;
5420 -- Contains the chars of the full package name up to maximum number
5421 -- allowed as per Errout.Error_Msg_Name_# variables.
5422
5423 Count : Integer := Selectors'First;
5424 -- Count of selector names forming the full package name
5425
5426 Current_Pkg : Node_Id := Parent (Pkg);
5427
5428 begin
5429 Selectors (Count) := Pkg;
5430
5431 -- Gather all the selectors we can display
5432
5433 while Nkind (Current_Pkg) = N_Selected_Component
5434 and then Is_Known_Unit (Current_Pkg)
5435 and then Count < Selectors'Length
5436 loop
5437 Count := Count + 1;
5438 Selectors (Count) := Selector_Name (Current_Pkg);
5439 Current_Pkg := Parent (Current_Pkg);
5440 end loop;
5441
5442 -- Display the error message based on the number of selectors found
5443
5444 case Count is
5445 when 1 =>
5446 Error_Msg_Node_1 := Selectors (1);
5447 Error_Msg_N -- CODEFIX
5448 ("\\missing `WITH &;`", Pkg);
5449 when 2 =>
5450 Error_Msg_Node_1 := Selectors (1);
5451 Error_Msg_Node_2 := Selectors (2);
5452 Error_Msg_N -- CODEFIX
5453 ("\\missing `WITH &.&;`", Pkg);
5454 when 3 =>
5455 Error_Msg_Node_1 := Selectors (1);
5456 Error_Msg_Node_2 := Selectors (2);
5457 Error_Msg_Node_3 := Selectors (3);
5458 Error_Msg_N -- CODEFIX
5459 ("\\missing `WITH &.&.&;`", Pkg);
5460 when 4 =>
5461 Error_Msg_Node_1 := Selectors (1);
5462 Error_Msg_Node_2 := Selectors (2);
5463 Error_Msg_Node_3 := Selectors (3);
5464 Error_Msg_Node_3 := Selectors (4);
5465 Error_Msg_N -- CODEFIX
5466 ("\\missing `WITH &.&.&.&;`", Pkg);
5467 when 5 =>
5468 Error_Msg_Node_1 := Selectors (1);
5469 Error_Msg_Node_2 := Selectors (2);
5470 Error_Msg_Node_3 := Selectors (3);
5471 Error_Msg_Node_3 := Selectors (4);
5472 Error_Msg_Node_3 := Selectors (5);
5473 Error_Msg_N -- CODEFIX
5474 ("\\missing `WITH &.&.&.&.&;`", Pkg);
5475 when 6 =>
5476 Error_Msg_Node_1 := Selectors (1);
5477 Error_Msg_Node_2 := Selectors (2);
5478 Error_Msg_Node_3 := Selectors (3);
5479 Error_Msg_Node_4 := Selectors (4);
5480 Error_Msg_Node_5 := Selectors (5);
5481 Error_Msg_Node_6 := Selectors (6);
5482 Error_Msg_N -- CODEFIX
5483 ("\\missing `WITH &.&.&.&.&.&;`", Pkg);
5484 when others =>
5485 raise Program_Error;
5486 end case;
5487 end Error_Missing_With_Of_Known_Unit;
5488
5489 --------------------
5490 -- Is_Self_Hidden --
5491 --------------------
5492
5493 function Is_Self_Hidden (E : Entity_Id) return Boolean is
5494 begin
5495 if Is_Not_Self_Hidden (E) then
5496 return Ekind (E) = E_Void;
5497 else
5498 return True;
5499 end if;
5500 end Is_Self_Hidden;
5501
5502 ----------------------
5503 -- Find_Direct_Name --
5504 ----------------------
5505
5506 procedure Find_Direct_Name (N : Node_Id) is
5507 E : Entity_Id;
5508 E2 : Entity_Id;
5509 Msg : Boolean;
5510
5511 Homonyms : Entity_Id;
5512 -- Saves start of homonym chain
5513
5514 Inst : Entity_Id := Empty;
5515 -- Enclosing instance, if any
5516
5517 Nvis_Entity : Boolean;
5518 -- Set True to indicate that there is at least one entity on the homonym
5519 -- chain which, while not visible, is visible enough from the user point
5520 -- of view to warrant an error message of "not visible" rather than
5521 -- undefined.
5522
5523 Nvis_Is_Private_Subprg : Boolean := False;
5524 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
5525 -- effect concerning library subprograms has been detected. Used to
5526 -- generate the precise error message.
5527
5528 function From_Actual_Package (E : Entity_Id) return Boolean;
5529 -- Returns true if the entity is an actual for a package that is itself
5530 -- an actual for a formal package of the current instance. Such an
5531 -- entity requires special handling because it may be use-visible but
5532 -- hides directly visible entities defined outside the instance, because
5533 -- the corresponding formal did so in the generic.
5534
5535 function Is_Actual_Parameter return Boolean;
5536 -- This function checks if the node N is an identifier that is an actual
5537 -- parameter of a procedure call. If so it returns True, otherwise it
5538 -- return False. The reason for this check is that at this stage we do
5539 -- not know what procedure is being called if the procedure might be
5540 -- overloaded, so it is premature to go setting referenced flags or
5541 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
5542 -- for that processing.
5543 -- Note: there is a similar routine Sem_Util.Is_Actual_Parameter, but
5544 -- it works for both function and procedure calls, while here we are
5545 -- only concerned with procedure calls (and with entry calls as well,
5546 -- but they are parsed as procedure calls and only later rewritten to
5547 -- entry calls).
5548
5549 function Known_But_Invisible (E : Entity_Id) return Boolean;
5550 -- This function determines whether a reference to the entity E, which
5551 -- is not visible, can reasonably be considered to be known to the
5552 -- writer of the reference. This is a heuristic test, used only for
5553 -- the purposes of figuring out whether we prefer to complain that an
5554 -- entity is undefined or invisible (and identify the declaration of
5555 -- the invisible entity in the latter case). The point here is that we
5556 -- don't want to complain that something is invisible and then point to
5557 -- something entirely mysterious to the writer.
5558
5559 procedure Nvis_Messages;
5560 -- Called if there are no visible entries for N, but there is at least
5561 -- one non-directly visible, or hidden declaration. This procedure
5562 -- outputs an appropriate set of error messages.
5563
5564 procedure Undefined (Nvis : Boolean);
5565 -- This function is called if the current node has no corresponding
5566 -- visible entity or entities. The value set in Msg indicates whether
5567 -- an error message was generated (multiple error messages for the
5568 -- same variable are generally suppressed, see body for details).
5569 -- Msg is True if an error message was generated, False if not. This
5570 -- value is used by the caller to determine whether or not to output
5571 -- additional messages where appropriate. The parameter is set False
5572 -- to get the message "X is undefined", and True to get the message
5573 -- "X is not visible".
5574
5575 -------------------------
5576 -- From_Actual_Package --
5577 -------------------------
5578
5579 function From_Actual_Package (E : Entity_Id) return Boolean is
5580 Scop : constant Entity_Id := Scope (E);
5581 -- Declared scope of candidate entity
5582
5583 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
5584 -- Recursive function that does the work and examines actuals of
5585 -- actual packages of current instance.
5586
5587 ------------------------
5588 -- Declared_In_Actual --
5589 ------------------------
5590
5591 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
5592 pragma Assert (Ekind (Pack) = E_Package);
5593 Act : Entity_Id;
5594 begin
5595 if No (Associated_Formal_Package (Pack)) then
5596 return False;
5597
5598 else
5599 Act := First_Entity (Pack);
5600 while Present (Act) loop
5601 if Renamed_Entity (Pack) = Scop then
5602 return True;
5603
5604 -- Check for end of list of actuals
5605
5606 elsif Ekind (Act) = E_Package
5607 and then Renamed_Entity (Act) = Pack
5608 then
5609 return False;
5610
5611 elsif Ekind (Act) = E_Package
5612 and then Declared_In_Actual (Act)
5613 then
5614 return True;
5615 end if;
5616
5617 Next_Entity (Act);
5618 end loop;
5619
5620 return False;
5621 end if;
5622 end Declared_In_Actual;
5623
5624 -- Local variables
5625
5626 Act : Entity_Id;
5627
5628 -- Start of processing for From_Actual_Package
5629
5630 begin
5631 if not In_Instance then
5632 return False;
5633
5634 else
5635 Inst := Current_Scope;
5636 while Present (Inst)
5637 and then Ekind (Inst) /= E_Package
5638 and then not Is_Generic_Instance (Inst)
5639 loop
5640 Inst := Scope (Inst);
5641 end loop;
5642
5643 if No (Inst) then
5644 return False;
5645 end if;
5646
5647 Act := First_Entity (Inst);
5648 while Present (Act) loop
5649 if Ekind (Act) = E_Package
5650 and then Declared_In_Actual (Act)
5651 then
5652 return True;
5653 end if;
5654
5655 Next_Entity (Act);
5656 end loop;
5657
5658 return False;
5659 end if;
5660 end From_Actual_Package;
5661
5662 -------------------------
5663 -- Is_Actual_Parameter --
5664 -------------------------
5665
5666 function Is_Actual_Parameter return Boolean is
5667 begin
5668 if Nkind (N) = N_Identifier then
5669 case Nkind (Parent (N)) is
5670 when N_Procedure_Call_Statement =>
5671 return Is_List_Member (N)
5672 and then List_Containing (N) =
5673 Parameter_Associations (Parent (N));
5674
5675 when N_Parameter_Association =>
5676 return N = Explicit_Actual_Parameter (Parent (N))
5677 and then Nkind (Parent (Parent (N))) =
5678 N_Procedure_Call_Statement;
5679
5680 when others =>
5681 return False;
5682 end case;
5683 else
5684 return False;
5685 end if;
5686 end Is_Actual_Parameter;
5687
5688 -------------------------
5689 -- Known_But_Invisible --
5690 -------------------------
5691
5692 function Known_But_Invisible (E : Entity_Id) return Boolean is
5693 Fname : File_Name_Type;
5694
5695 begin
5696 -- Entities in Standard are always considered to be known
5697
5698 if Sloc (E) <= Standard_Location then
5699 return True;
5700
5701 -- An entity that does not come from source is always considered
5702 -- to be unknown, since it is an artifact of code expansion.
5703
5704 elsif not Comes_From_Source (E) then
5705 return False;
5706 end if;
5707
5708 -- Here we have an entity that is not from package Standard, and
5709 -- which comes from Source. See if it comes from an internal file.
5710
5711 Fname := Unit_File_Name (Get_Source_Unit (E));
5712
5713 -- Case of from internal file
5714
5715 if In_Internal_Unit (E) then
5716
5717 -- Private part entities in internal files are never considered
5718 -- to be known to the writer of normal application code.
5719
5720 if Is_Hidden (E) then
5721 return False;
5722 end if;
5723
5724 -- Entities from System packages other than System and
5725 -- System.Storage_Elements are not considered to be known.
5726 -- System.Auxxxx files are also considered known to the user.
5727
5728 -- Should refine this at some point to generally distinguish
5729 -- between known and unknown internal files ???
5730
5731 Get_Name_String (Fname);
5732
5733 return
5734 Name_Len < 2
5735 or else
5736 Name_Buffer (1 .. 2) /= "s-"
5737 or else
5738 Name_Buffer (3 .. 8) = "stoele"
5739 or else
5740 Name_Buffer (3 .. 5) = "aux";
5741
5742 -- If not an internal file, then entity is definitely known, even if
5743 -- it is in a private part (the message generated will note that it
5744 -- is in a private part).
5745
5746 else
5747 return True;
5748 end if;
5749 end Known_But_Invisible;
5750
5751 -------------------
5752 -- Nvis_Messages --
5753 -------------------
5754
5755 procedure Nvis_Messages is
5756 Comp_Unit : Node_Id;
5757 Ent : Entity_Id;
5758 Found : Boolean := False;
5759 Hidden : Boolean := False;
5760 Item : Node_Id;
5761
5762 begin
5763 -- Ada 2005 (AI-262): Generate a precise error concerning the
5764 -- Beaujolais effect that was previously detected
5765
5766 if Nvis_Is_Private_Subprg then
5767
5768 pragma Assert (Nkind (E2) = N_Defining_Identifier
5769 and then Ekind (E2) = E_Function
5770 and then Scope (E2) = Standard_Standard
5771 and then Has_Private_With (E2));
5772
5773 -- Find the sloc corresponding to the private with'ed unit
5774
5775 Comp_Unit := Cunit (Current_Sem_Unit);
5776 Error_Msg_Sloc := No_Location;
5777
5778 Item := First (Context_Items (Comp_Unit));
5779 while Present (Item) loop
5780 if Nkind (Item) = N_With_Clause
5781 and then Private_Present (Item)
5782 and then Entity (Name (Item)) = E2
5783 then
5784 Error_Msg_Sloc := Sloc (Item);
5785 exit;
5786 end if;
5787
5788 Next (Item);
5789 end loop;
5790
5791 pragma Assert (Error_Msg_Sloc /= No_Location);
5792
5793 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
5794 return;
5795 end if;
5796
5797 Undefined (Nvis => True);
5798
5799 if Msg then
5800
5801 -- First loop does hidden declarations
5802
5803 Ent := Homonyms;
5804 while Present (Ent) loop
5805 if Is_Potentially_Use_Visible (Ent) then
5806 if not Hidden then
5807 Error_Msg_N -- CODEFIX
5808 ("multiple use clauses cause hiding!", N);
5809 Hidden := True;
5810 end if;
5811
5812 Error_Msg_Sloc := Sloc (Ent);
5813 Error_Msg_N -- CODEFIX
5814 ("hidden declaration#!", N);
5815 end if;
5816
5817 Ent := Homonym (Ent);
5818 end loop;
5819
5820 -- If we found hidden declarations, then that's enough, don't
5821 -- bother looking for non-visible declarations as well.
5822
5823 if Hidden then
5824 return;
5825 end if;
5826
5827 -- Second loop does non-directly visible declarations
5828
5829 Ent := Homonyms;
5830 while Present (Ent) loop
5831 if not Is_Potentially_Use_Visible (Ent) then
5832
5833 -- Do not bother the user with unknown entities
5834
5835 if not Known_But_Invisible (Ent) then
5836 goto Continue;
5837 end if;
5838
5839 Error_Msg_Sloc := Sloc (Ent);
5840
5841 -- Output message noting that there is a non-visible
5842 -- declaration, distinguishing the private part case.
5843
5844 if Is_Hidden (Ent) then
5845 Error_Msg_N ("non-visible (private) declaration#!", N);
5846
5847 -- If the entity is declared in a generic package, it
5848 -- cannot be visible, so there is no point in adding it
5849 -- to the list of candidates if another homograph from a
5850 -- non-generic package has been seen.
5851
5852 elsif Ekind (Scope (Ent)) = E_Generic_Package
5853 and then Found
5854 then
5855 null;
5856
5857 else
5858 -- When the entity comes from a generic instance the
5859 -- normal error message machinery will give the line
5860 -- number of the generic package and the location of
5861 -- the generic instance, but not the name of the
5862 -- the instance.
5863
5864 -- So, in order to give more descriptive error messages
5865 -- in this case, we include the name of the generic
5866 -- package.
5867
5868 if Is_Generic_Instance (Scope (Ent)) then
5869 Error_Msg_Name_1 := Chars (Scope (Ent));
5870 Error_Msg_N -- CODEFIX
5871 ("non-visible declaration from %#!", N);
5872
5873 -- Otherwise print the message normally
5874
5875 else
5876 Error_Msg_N -- CODEFIX
5877 ("non-visible declaration#!", N);
5878 end if;
5879
5880 if Ekind (Scope (Ent)) /= E_Generic_Package then
5881 Found := True;
5882 end if;
5883
5884 if Is_Compilation_Unit (Ent)
5885 and then
5886 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5887 then
5888 Error_Msg_Qual_Level := 99;
5889 Error_Msg_NE -- CODEFIX
5890 ("\\missing `WITH &;`", N, Ent);
5891 Error_Msg_Qual_Level := 0;
5892 end if;
5893
5894 if Ekind (Ent) = E_Discriminant
5895 and then Present (Corresponding_Discriminant (Ent))
5896 and then Scope (Corresponding_Discriminant (Ent)) =
5897 Etype (Scope (Ent))
5898 then
5899 Error_Msg_N
5900 ("inherited discriminant not allowed here" &
5901 " (RM 3.8 (12), 3.8.1 (6))!", N);
5902 end if;
5903 end if;
5904
5905 -- Set entity and its containing package as referenced. We
5906 -- can't be sure of this, but this seems a better choice
5907 -- to avoid unused entity messages.
5908
5909 if Comes_From_Source (Ent) then
5910 Set_Referenced (Ent);
5911 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5912 end if;
5913 end if;
5914
5915 <<Continue>>
5916 Ent := Homonym (Ent);
5917 end loop;
5918 end if;
5919 end Nvis_Messages;
5920
5921 ---------------
5922 -- Undefined --
5923 ---------------
5924
5925 procedure Undefined (Nvis : Boolean) is
5926 Emsg : Error_Msg_Id;
5927
5928 begin
5929 -- We should never find an undefined internal name. If we do, then
5930 -- see if we have previous errors. If so, ignore on the grounds that
5931 -- it is probably a cascaded message (e.g. a block label from a badly
5932 -- formed block). If no previous errors, then we have a real internal
5933 -- error of some kind so raise an exception.
5934
5935 if Is_Internal_Name (Chars (N)) then
5936 if Total_Errors_Detected /= 0 then
5937 return;
5938 else
5939 raise Program_Error;
5940 end if;
5941 end if;
5942
5943 -- A very specialized error check, if the undefined variable is
5944 -- a case tag, and the case type is an enumeration type, check
5945 -- for a possible misspelling, and if so, modify the identifier
5946
5947 -- Named aggregate should also be handled similarly ???
5948
5949 if Nkind (N) = N_Identifier
5950 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5951 then
5952 declare
5953 Case_Stm : constant Node_Id := Parent (Parent (N));
5954 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5955
5956 Lit : Node_Id;
5957
5958 begin
5959 if Is_Enumeration_Type (Case_Typ)
5960 and then not Is_Standard_Character_Type (Case_Typ)
5961 then
5962 Lit := First_Literal (Case_Typ);
5963 Get_Name_String (Chars (Lit));
5964
5965 if Chars (Lit) /= Chars (N)
5966 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5967 then
5968 Error_Msg_Node_2 := Lit;
5969 Error_Msg_N -- CODEFIX
5970 ("& is undefined, assume misspelling of &", N);
5971 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5972 return;
5973 end if;
5974
5975 Next_Literal (Lit);
5976 end if;
5977 end;
5978 end if;
5979
5980 -- Normal processing
5981
5982 Set_Entity (N, Any_Id);
5983 Set_Etype (N, Any_Type);
5984
5985 -- We use the table Urefs to keep track of entities for which we
5986 -- have issued errors for undefined references. Multiple errors
5987 -- for a single name are normally suppressed, however we modify
5988 -- the error message to alert the programmer to this effect.
5989
5990 for J in Urefs.First .. Urefs.Last loop
5991 if Chars (N) = Chars (Urefs.Table (J).Node) then
5992 if Urefs.Table (J).Err /= No_Error_Msg
5993 and then Sloc (N) /= Urefs.Table (J).Loc
5994 then
5995 Error_Msg_Node_1 := Urefs.Table (J).Node;
5996
5997 if Urefs.Table (J).Nvis then
5998 Change_Error_Text (Urefs.Table (J).Err,
5999 "& is not visible (more references follow)");
6000 else
6001 Change_Error_Text (Urefs.Table (J).Err,
6002 "& is undefined (more references follow)");
6003 end if;
6004
6005 Urefs.Table (J).Err := No_Error_Msg;
6006 end if;
6007
6008 -- Although we will set Msg False, and thus suppress the
6009 -- message, we also set Error_Posted True, to avoid any
6010 -- cascaded messages resulting from the undefined reference.
6011
6012 Msg := False;
6013 Set_Error_Posted (N);
6014 return;
6015 end if;
6016 end loop;
6017
6018 -- If entry not found, this is first undefined occurrence
6019
6020 if Nvis then
6021 Error_Msg_N ("& is not visible!", N);
6022 Emsg := Get_Msg_Id;
6023
6024 else
6025 Error_Msg_N ("& is undefined!", N);
6026 Emsg := Get_Msg_Id;
6027
6028 -- A very bizarre special check, if the undefined identifier
6029 -- is Put or Put_Line, then add a special error message (since
6030 -- this is a very common error for beginners to make).
6031
6032 if Chars (N) in Name_Put | Name_Put_Line then
6033 Error_Msg_N -- CODEFIX
6034 ("\\possible missing `WITH Ada.Text_'I'O; " &
6035 "USE Ada.Text_'I'O`!", N);
6036
6037 -- Another special check if N is the prefix of a selected
6038 -- component which is a known unit: add message complaining
6039 -- about missing with for this unit.
6040
6041 elsif Nkind (Parent (N)) = N_Selected_Component
6042 and then N = Prefix (Parent (N))
6043 and then Is_Known_Unit (Parent (N))
6044 then
6045 Error_Missing_With_Of_Known_Unit (N);
6046 end if;
6047
6048 -- Now check for possible misspellings
6049
6050 declare
6051 E : Entity_Id;
6052 Ematch : Entity_Id := Empty;
6053 begin
6054 for Nam in First_Name_Id .. Last_Name_Id loop
6055 E := Get_Name_Entity_Id (Nam);
6056
6057 if Present (E)
6058 and then (Is_Immediately_Visible (E)
6059 or else
6060 Is_Potentially_Use_Visible (E))
6061 then
6062 if Is_Bad_Spelling_Of (Chars (N), Nam) then
6063 Ematch := E;
6064 exit;
6065 end if;
6066 end if;
6067 end loop;
6068
6069 if Present (Ematch) then
6070 Error_Msg_NE -- CODEFIX
6071 ("\possible misspelling of&", N, Ematch);
6072 end if;
6073 end;
6074 end if;
6075
6076 -- Make entry in undefined references table unless the full errors
6077 -- switch is set, in which case by refraining from generating the
6078 -- table entry we guarantee that we get an error message for every
6079 -- undefined reference. The entry is not added if we are ignoring
6080 -- errors.
6081
6082 if not All_Errors_Mode
6083 and then Ignore_Errors_Enable = 0
6084 and then not Get_Ignore_Errors
6085 then
6086 Urefs.Append (
6087 (Node => N,
6088 Err => Emsg,
6089 Nvis => Nvis,
6090 Loc => Sloc (N)));
6091 end if;
6092
6093 Msg := True;
6094 end Undefined;
6095
6096 -- Local variables
6097
6098 Nested_Inst : Entity_Id := Empty;
6099 -- The entity of a nested instance which appears within Inst (if any)
6100
6101 -- Start of processing for Find_Direct_Name
6102
6103 begin
6104 -- If the entity pointer is already set, this is an internal node, or
6105 -- a node that is analyzed more than once, after a tree modification.
6106 -- In such a case there is no resolution to perform, just set the type.
6107
6108 if Present (Entity (N)) then
6109 if Is_Type (Entity (N)) then
6110 Set_Etype (N, Entity (N));
6111
6112 else
6113 declare
6114 Entyp : constant Entity_Id := Etype (Entity (N));
6115
6116 begin
6117 -- One special case here. If the Etype field is already set,
6118 -- and references the packed array type corresponding to the
6119 -- etype of the referenced entity, then leave it alone. This
6120 -- happens for trees generated from Exp_Pakd, where expressions
6121 -- can be deliberately "mis-typed" to the packed array type.
6122
6123 if Is_Packed_Array (Entyp)
6124 and then Present (Etype (N))
6125 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
6126 then
6127 null;
6128
6129 -- If not that special case, then just reset the Etype
6130
6131 else
6132 Set_Etype (N, Entyp);
6133 end if;
6134 end;
6135 end if;
6136
6137 -- Although the marking of use clauses happens at the end of
6138 -- Find_Direct_Name, a certain case where a generic actual satisfies
6139 -- a use clause must be checked here due to how the generic machinery
6140 -- handles the analysis of said actuals.
6141
6142 if In_Instance
6143 and then Nkind (Parent (N)) = N_Generic_Association
6144 then
6145 Mark_Use_Clauses (Entity (N));
6146 end if;
6147
6148 return;
6149 end if;
6150
6151 -- Preserve relevant elaboration-related attributes of the context which
6152 -- are no longer available or very expensive to recompute once analysis,
6153 -- resolution, and expansion are over.
6154
6155 if Nkind (N) = N_Identifier then
6156 Mark_Elaboration_Attributes
6157 (N_Id => N,
6158 Checks => True,
6159 Modes => True,
6160 Warnings => True);
6161 end if;
6162
6163 -- Here if Entity pointer was not set, we need full visibility analysis
6164 -- First we generate debugging output if the debug E flag is set.
6165
6166 if Debug_Flag_E then
6167 Write_Str ("Looking for ");
6168 Write_Name (Chars (N));
6169 Write_Eol;
6170 end if;
6171
6172 Homonyms := Current_Entity (N);
6173 Nvis_Entity := False;
6174
6175 E := Homonyms;
6176 while Present (E) loop
6177
6178 -- If entity is immediately visible or potentially use visible, then
6179 -- process the entity and we are done.
6180
6181 if Is_Immediately_Visible (E) then
6182 goto Immediately_Visible_Entity;
6183
6184 elsif Is_Potentially_Use_Visible (E) then
6185 goto Potentially_Use_Visible_Entity;
6186
6187 -- Note if a known but invisible entity encountered
6188
6189 elsif Known_But_Invisible (E) then
6190 Nvis_Entity := True;
6191 end if;
6192
6193 -- Move to next entity in chain and continue search
6194
6195 E := Homonym (E);
6196 end loop;
6197
6198 -- If no entries on homonym chain that were potentially visible,
6199 -- and no entities reasonably considered as non-visible, then
6200 -- we have a plain undefined reference, with no additional
6201 -- explanation required.
6202
6203 if not Nvis_Entity then
6204 Undefined (Nvis => False);
6205
6206 -- Otherwise there is at least one entry on the homonym chain that
6207 -- is reasonably considered as being known and non-visible.
6208
6209 else
6210 Nvis_Messages;
6211 end if;
6212
6213 goto Done;
6214
6215 -- Processing for a potentially use visible entry found. We must search
6216 -- the rest of the homonym chain for two reasons. First, if there is a
6217 -- directly visible entry, then none of the potentially use-visible
6218 -- entities are directly visible (RM 8.4(10)). Second, we need to check
6219 -- for the case of multiple potentially use-visible entries hiding one
6220 -- another and as a result being non-directly visible (RM 8.4(11)).
6221
6222 <<Potentially_Use_Visible_Entity>> declare
6223 Only_One_Visible : Boolean := True;
6224 All_Overloadable : Boolean := Is_Overloadable (E);
6225
6226 begin
6227 E2 := Homonym (E);
6228 while Present (E2) loop
6229 if Is_Immediately_Visible (E2) then
6230
6231 -- If the use-visible entity comes from the actual for a
6232 -- formal package, it hides a directly visible entity from
6233 -- outside the instance.
6234
6235 if From_Actual_Package (E)
6236 and then Scope_Depth (Scope (E2)) < Scope_Depth (Inst)
6237 then
6238 goto Found;
6239 else
6240 E := E2;
6241 goto Immediately_Visible_Entity;
6242 end if;
6243
6244 elsif Is_Potentially_Use_Visible (E2) then
6245 Only_One_Visible := False;
6246 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
6247
6248 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
6249 -- that can occur in private_with clauses. Example:
6250
6251 -- with A;
6252 -- private with B; package A is
6253 -- package C is function B return Integer;
6254 -- use A; end A;
6255 -- V1 : Integer := B;
6256 -- private function B return Integer;
6257 -- V2 : Integer := B;
6258 -- end C;
6259
6260 -- V1 resolves to A.B, but V2 resolves to library unit B
6261
6262 elsif Ekind (E2) = E_Function
6263 and then Scope (E2) = Standard_Standard
6264 and then Has_Private_With (E2)
6265 then
6266 Only_One_Visible := False;
6267 All_Overloadable := False;
6268 Nvis_Is_Private_Subprg := True;
6269 exit;
6270 end if;
6271
6272 E2 := Homonym (E2);
6273 end loop;
6274
6275 -- On falling through this loop, we have checked that there are no
6276 -- immediately visible entities. Only_One_Visible is set if exactly
6277 -- one potentially use visible entity exists. All_Overloadable is
6278 -- set if all the potentially use visible entities are overloadable.
6279 -- The condition for legality is that either there is one potentially
6280 -- use visible entity, or if there is more than one, then all of them
6281 -- are overloadable.
6282
6283 if Only_One_Visible or All_Overloadable then
6284 goto Found;
6285
6286 -- If there is more than one potentially use-visible entity and at
6287 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
6288 -- Note that E points to the first such entity on the homonym list.
6289
6290 else
6291 -- If one of the entities is declared in an actual package, it
6292 -- was visible in the generic, and takes precedence over other
6293 -- entities that are potentially use-visible. The same applies
6294 -- if the entity is declared in a local instantiation of the
6295 -- current instance.
6296
6297 if In_Instance then
6298
6299 -- Find the current instance
6300
6301 Inst := Current_Scope;
6302 while Present (Inst) and then Inst /= Standard_Standard loop
6303 if Is_Generic_Instance (Inst) then
6304 exit;
6305 end if;
6306
6307 Inst := Scope (Inst);
6308 end loop;
6309
6310 -- Reexamine the candidate entities, giving priority to those
6311 -- that were visible within the generic.
6312
6313 E2 := E;
6314 while Present (E2) loop
6315 Nested_Inst := Nearest_Enclosing_Instance (E2);
6316
6317 -- The entity is declared within an actual package, or in a
6318 -- nested instance. The ">=" accounts for the case where the
6319 -- current instance and the nested instance are the same.
6320
6321 if From_Actual_Package (E2)
6322 or else (Present (Nested_Inst)
6323 and then Scope_Depth (Nested_Inst) >=
6324 Scope_Depth (Inst))
6325 then
6326 E := E2;
6327 goto Found;
6328 end if;
6329
6330 E2 := Homonym (E2);
6331 end loop;
6332
6333 Nvis_Messages;
6334 goto Done;
6335
6336 elsif Is_Predefined_Unit (Current_Sem_Unit)
6337 and then not Is_Predefined_Unit (Main_Unit)
6338 then
6339 -- A use clause in the body of a system file creates conflict
6340 -- with some entity in a user scope, while rtsfind is active.
6341 -- Keep only the entity coming from another predefined unit.
6342
6343 E2 := E;
6344 while Present (E2) loop
6345 if In_Predefined_Unit (E2) then
6346 E := E2;
6347 goto Found;
6348 end if;
6349
6350 E2 := Homonym (E2);
6351 end loop;
6352
6353 -- Entity must exist because predefined unit is correct
6354
6355 raise Program_Error;
6356
6357 else
6358 Nvis_Messages;
6359 goto Done;
6360 end if;
6361 end if;
6362 end;
6363
6364 -- Come here with E set to the first immediately visible entity on
6365 -- the homonym chain. This is the one we want unless there is another
6366 -- immediately visible entity further on in the chain for an inner
6367 -- scope (RM 8.3(8)).
6368
6369 <<Immediately_Visible_Entity>> declare
6370 Level : Int;
6371 Scop : Entity_Id;
6372
6373 begin
6374 -- Find scope level of initial entity. When compiling through
6375 -- Rtsfind, the previous context is not completely invisible, and
6376 -- an outer entity may appear on the chain, whose scope is below
6377 -- the entry for Standard that delimits the current scope stack.
6378 -- Indicate that the level for this spurious entry is outside of
6379 -- the current scope stack.
6380
6381 Level := Scope_Stack.Last;
6382 loop
6383 Scop := Scope_Stack.Table (Level).Entity;
6384 exit when Scop = Scope (E);
6385 Level := Level - 1;
6386 exit when Scop = Standard_Standard;
6387 end loop;
6388
6389 -- Now search remainder of homonym chain for more inner entry
6390 -- If the entity is Standard itself, it has no scope, and we
6391 -- compare it with the stack entry directly.
6392
6393 E2 := Homonym (E);
6394 while Present (E2) loop
6395 if Is_Immediately_Visible (E2) then
6396
6397 -- If a generic package contains a local declaration that
6398 -- has the same name as the generic, there may be a visibility
6399 -- conflict in an instance, where the local declaration must
6400 -- also hide the name of the corresponding package renaming.
6401 -- We check explicitly for a package declared by a renaming,
6402 -- whose renamed entity is an instance that is on the scope
6403 -- stack, and that contains a homonym in the same scope. Once
6404 -- we have found it, we know that the package renaming is not
6405 -- immediately visible, and that the identifier denotes the
6406 -- other entity (and its homonyms if overloaded).
6407
6408 if Scope (E) = Scope (E2)
6409 and then Ekind (E) = E_Package
6410 and then Present (Renamed_Entity (E))
6411 and then Is_Generic_Instance (Renamed_Entity (E))
6412 and then In_Open_Scopes (Renamed_Entity (E))
6413 and then Comes_From_Source (N)
6414 then
6415 Set_Is_Immediately_Visible (E, False);
6416 E := E2;
6417
6418 else
6419 for J in Level + 1 .. Scope_Stack.Last loop
6420 if Scope_Stack.Table (J).Entity = Scope (E2)
6421 or else Scope_Stack.Table (J).Entity = E2
6422 then
6423 Level := J;
6424 E := E2;
6425 exit;
6426 end if;
6427 end loop;
6428 end if;
6429 end if;
6430
6431 E2 := Homonym (E2);
6432 end loop;
6433
6434 -- At the end of that loop, E is the innermost immediately
6435 -- visible entity, so we are all set.
6436 end;
6437
6438 -- Come here with entity found, and stored in E
6439
6440 <<Found>> begin
6441
6442 -- Check violation of No_Wide_Characters restriction
6443
6444 Check_Wide_Character_Restriction (E, N);
6445
6446 -- When distribution features are available (Get_PCS_Name /=
6447 -- Name_No_DSA), a remote access-to-subprogram type is converted
6448 -- into a record type holding whatever information is needed to
6449 -- perform a remote call on an RCI subprogram. In that case we
6450 -- rewrite any occurrence of the RAS type into the equivalent record
6451 -- type here. 'Access attribute references and RAS dereferences are
6452 -- then implemented using specific TSSs. However when distribution is
6453 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
6454 -- generation of these TSSs, and we must keep the RAS type in its
6455 -- original access-to-subprogram form (since all calls through a
6456 -- value of such type will be local anyway in the absence of a PCS).
6457
6458 if Comes_From_Source (N)
6459 and then Is_Remote_Access_To_Subprogram_Type (E)
6460 and then Ekind (E) = E_Access_Subprogram_Type
6461 and then Expander_Active
6462 and then Get_PCS_Name /= Name_No_DSA
6463 then
6464 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
6465 goto Done;
6466 end if;
6467
6468 -- Set the entity. Note that the reason we call Set_Entity for the
6469 -- overloadable case, as opposed to Set_Entity_With_Checks is
6470 -- that in the overloaded case, the initial call can set the wrong
6471 -- homonym. The call that sets the right homonym is in Sem_Res and
6472 -- that call does use Set_Entity_With_Checks, so we don't miss
6473 -- a style check.
6474
6475 if Is_Overloadable (E) then
6476 Set_Entity (N, E);
6477 else
6478 Set_Entity_With_Checks (N, E);
6479 end if;
6480
6481 if Is_Type (E) then
6482 Set_Etype (N, E);
6483 else
6484 Set_Etype (N, Get_Full_View (Etype (E)));
6485 end if;
6486
6487 if Debug_Flag_E then
6488 Write_Str (" found ");
6489 Write_Entity_Info (E, " ");
6490 end if;
6491
6492 if Is_Self_Hidden (E)
6493 and then
6494 (not Is_Record_Type (Current_Scope)
6495 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
6496 then
6497 Premature_Usage (N);
6498
6499 -- If the entity is overloadable, collect all interpretations of the
6500 -- name for subsequent overload resolution. We optimize a bit here to
6501 -- do this only if we have an overloadable entity that is not on its
6502 -- own on the homonym chain.
6503
6504 elsif Is_Overloadable (E)
6505 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
6506 then
6507 Collect_Interps (N);
6508
6509 -- Background: for an instance of a generic, expansion sets
6510 -- entity fields on names that refer to things declared
6511 -- outside of the instance, but leaves the entity field
6512 -- unset on names that should end up referring to things
6513 -- declared within the instance. These will instead be set by
6514 -- analysis - the idea is that if a name resolves a certain
6515 -- way in the generic, then we should get corresponding results
6516 -- if we resolve the corresponding name in an instance. For this
6517 -- to work, we have to prevent unrelated declarations that
6518 -- happen to be visible at the point of the instantiation from
6519 -- participating in resolution and causing problems (typically
6520 -- ambiguities, but incorrect resolutions are also probably
6521 -- possible). So here we filter out such unwanted interpretations.
6522 --
6523 -- Note that there are other problems with this approach to
6524 -- implementing generic instances that are not addressed here.
6525 -- Inside a generic, we might have no trouble resolving a call
6526 -- where the two candidates are a function that returns a
6527 -- formal type and a function that returns Standard.Integer.
6528 -- If we instantiate that generic and the corresponding actual
6529 -- type is Standard.Integer, then we may incorrectly reject the
6530 -- corresponding call in the instance as ambiguous (or worse,
6531 -- we may quietly choose the wrong resolution).
6532 --
6533 -- Another such problem can occur with a type derived from a
6534 -- formal derived type. In an instance, such a type may have
6535 -- inherited subprograms that are not present in the generic.
6536 -- These can then interfere with name resolution (e.g., if
6537 -- some declaration is visible via a use-clause in the generic
6538 -- and some name in the generic refers to it, then the
6539 -- corresponding declaration in an instance may be hidden by
6540 -- a directly visible inherited subprogram and the corresponding
6541 -- name in the instance may then incorrectly refer to the
6542 -- inherited subprogram).
6543
6544 if In_Instance then
6545 declare
6546 function Is_Actual_Subp_Of_Inst
6547 (E : Entity_Id; Inst : Entity_Id) return Boolean;
6548 -- Return True if E is an actual parameter
6549 -- corresponding to a formal subprogram of the
6550 -- instantiation Inst.
6551
6552 function Is_Extraneously_Visible
6553 (E : Entity_Id; Inst : Entity_Id) return Boolean;
6554 -- Return True if E is an interpretation that should
6555 -- be filtered out. That is, if E is an "unwanted"
6556 -- resolution candidate as described in the
6557 -- preceding "Background:" commment.
6558
6559 function Is_Generic_Actual_Subp_Name
6560 (N : Node_Id) return Boolean;
6561 -- Return True if N is the name of a subprogram
6562 -- renaming generated for a generic actual.
6563
6564 ----------------------------
6565 -- Is_Actual_Subp_Of_Inst --
6566 ----------------------------
6567
6568 function Is_Actual_Subp_Of_Inst
6569 (E : Entity_Id; Inst : Entity_Id) return Boolean
6570 is
6571 Decl : Node_Id;
6572 Generic_From_E, Generic_From_Inst : Entity_Id;
6573 begin
6574 -- ???
6575 -- Why is Is_Generic_Actual_Subprogram undefined
6576 -- in the E_Operator case?
6577
6578 if Ekind (E) not in E_Function | E_Procedure
6579 or else not Is_Generic_Actual_Subprogram (E)
6580 then
6581 return False;
6582 end if;
6583
6584 Decl := Enclosing_Declaration (E);
6585
6586 -- Look for the suprogram renaming declaration built
6587 -- for a generic actual subprogram. Unclear why
6588 -- Original_Node call is needed, but sometimes it is.
6589
6590 if Decl not in N_Subprogram_Renaming_Declaration_Id then
6591 Decl := Original_Node (Decl);
6592 end if;
6593
6594 if Decl in N_Subprogram_Renaming_Declaration_Id then
6595 Generic_From_E :=
6596 Scope (Corresponding_Formal_Spec (Decl));
6597 else
6598 -- ??? In the case of a generic formal subprogram
6599 -- which has a pre/post condition, it is unclear how
6600 -- to find the Corresponding_Formal_Spec-bearing node.
6601
6602 Generic_From_E := Empty;
6603 end if;
6604
6605 declare
6606 Inst_Parent : Node_Id := Parent (Inst);
6607 begin
6608 if Nkind (Inst_Parent) = N_Defining_Program_Unit_Name
6609 then
6610 Inst_Parent := Parent (Inst_Parent);
6611 end if;
6612
6613 Generic_From_Inst := Generic_Parent (Inst_Parent);
6614 end;
6615
6616 return Generic_From_E = Generic_From_Inst
6617 and then Present (Generic_From_E);
6618 end Is_Actual_Subp_Of_Inst;
6619
6620 -----------------------------
6621 -- Is_Extraneously_Visible --
6622 -----------------------------
6623
6624 function Is_Extraneously_Visible
6625 (E : Entity_Id; Inst : Entity_Id) return Boolean is
6626 begin
6627 -- Return False in various non-extraneous cases.
6628 -- If none of those apply, then return True.
6629
6630 if Within_Scope (E, Inst) then
6631 -- return False if E declared within Inst
6632 return False;
6633
6634 elsif Is_Actual_Subp_Of_Inst (E, Inst) then
6635 -- Return False if E is an actual subprogram,
6636 -- and therefore may be referenced within Inst.
6637 return False;
6638
6639 elsif Nkind (Parent (E)) = N_Subtype_Declaration
6640 and then Defining_Identifier (Parent (E)) /= E
6641 then
6642 -- Return False for a primitive subp of an
6643 -- actual corresponding to a formal type.
6644
6645 return False;
6646
6647 elsif not In_Open_Scopes (Scope (E)) then
6648 -- Return False if this candidate is not
6649 -- declared in a currently open scope.
6650
6651 return False;
6652
6653 else
6654 declare
6655 -- We want to know whether the declaration of
6656 -- E comes textually after the declaration of
6657 -- the generic that Inst is an instance of
6658 -- (and after the generic body if there is one).
6659 -- To compare, we climb up the deeper of the two
6660 -- scope chains until we the levels match.
6661 -- There is a separate loop for each starting
6662 -- point, but we will execute zero iterations
6663 -- for at least one of the two loops.
6664 -- For each Xxx_Scope, we have a corresponding
6665 -- Xxx_Trailer; the latter is the predecessor of
6666 -- the former in the scope traversal.
6667
6668 E_Trailer : Entity_Id := E;
6669 E_Scope : Entity_Id := Scope (E);
6670 pragma Assert (Present (E_Scope));
6671
6672 -- the generic that Inst is an instance of
6673 Gen_Trailer : Entity_Id :=
6674 Generic_Parent (Specification
6675 (Unit_Declaration_Node (Inst)));
6676 Gen_Scope : Entity_Id;
6677
6678 function Has_Formal_Package_Parameter
6679 (Generic_Id : Entity_Id) return Boolean;
6680 -- Return True iff given generic has at least one
6681 -- formal package parameter.
6682
6683 ----------------------------------
6684 -- Has_Formal_Package_Parameter --
6685 ----------------------------------
6686
6687 function Has_Formal_Package_Parameter
6688 (Generic_Id : Entity_Id) return Boolean is
6689 Formal_Decl : Node_Id :=
6690 First (Generic_Formal_Declarations
6691 (Enclosing_Generic_Unit (Generic_Id)));
6692 begin
6693 while Present (Formal_Decl) loop
6694 if Nkind (Original_Node (Formal_Decl)) =
6695 N_Formal_Package_Declaration
6696 then
6697 return True;
6698 end if;
6699
6700 Next (Formal_Decl);
6701 end loop;
6702 return False;
6703 end Has_Formal_Package_Parameter;
6704
6705 begin
6706 if No (Gen_Trailer) then
6707 -- Dunno how this can happen, but it can.
6708 return False;
6709 else
6710 if Has_Formal_Package_Parameter (Gen_Trailer)
6711 then
6712 -- Punt on sorting out what is visible via a
6713 -- formal package.
6714
6715 return False;
6716 end if;
6717
6718 if Is_Child_Unit (Gen_Trailer)
6719 and then Is_Generic_Unit
6720 (Entity (Name
6721 (Parent (Gen_Trailer))))
6722 then
6723 -- Punt on dealing with how the FE fails
6724 -- to build a tree for a "sprouted" generic
6725 -- so that what should be a reference to
6726 -- I1.G2 instead points into G1.G2 .
6727
6728 return False;
6729 end if;
6730
6731 Gen_Scope := Scope (Gen_Trailer);
6732
6733 while Scope_Depth (E_Scope)
6734 > Scope_Depth (Gen_Scope)
6735 loop
6736 E_Trailer := E_Scope;
6737 E_Scope := Scope (E_Scope);
6738 end loop;
6739 while Scope_Depth (E_Scope)
6740 < Scope_Depth (Gen_Scope)
6741 loop
6742 Gen_Trailer := Gen_Scope;
6743 Gen_Scope := Scope (Gen_Scope);
6744 end loop;
6745 end if;
6746
6747 if Gen_Scope = E_Scope then
6748 -- if Gen_Trailer and E_Trailer are declared
6749 -- in the same declarative part and E_Trailer
6750 -- occurs after the declaration (and body, if
6751 -- there is one) of Gen_Trailer, then
6752 -- return True because E was declared after
6753 -- the generic that Inst is an instance of
6754 -- (and also after that generic's body, if it
6755 -- has one).
6756
6757 if Is_Package_Or_Generic_Package (Gen_Trailer)
6758 and then Present (Package_Body (Gen_Trailer))
6759 then
6760 Gen_Trailer :=
6761 Corresponding_Body
6762 (Package_Spec (Gen_Trailer));
6763 end if;
6764
6765 declare
6766 Id : Entity_Id := Gen_Trailer;
6767 begin
6768 loop
6769 if No (Id) then
6770 -- E_Trailer presumably occurred
6771 -- earlier on the entity list than
6772 -- Gen_Trailer. So E preceded the
6773 -- generic that Inst is an instance
6774 -- of (or the body of that generic if
6775 -- it has one) and so could have
6776 -- been referenced within the generic.
6777 return False;
6778 end if;
6779 exit when Id = E_Trailer;
6780 Next_Entity (Id);
6781 end loop;
6782 end;
6783 end if;
6784 end;
6785 end if;
6786
6787 if Present (Nearest_Enclosing_Instance (Inst)) then
6788 return Is_Extraneously_Visible
6789 (E => E, Inst => Nearest_Enclosing_Instance (Inst));
6790
6791 -- The preceding Nearest_Enclosing_Instance test
6792 -- doesn't handle the case of an instance of a
6793 -- "sprouted" generic. For example, if Inst=I2 in
6794 -- generic package G1
6795 -- generic package G1.G2;
6796 -- package I1 is new G1;
6797 -- package I2 is new I1.G2;
6798 -- then N_E_I (Inst) = Empty. So deal with that case.
6799
6800 elsif Present (Nearest_Enclosing_Instance (E)) then
6801 return Is_Extraneously_Visible
6802 (E => Nearest_Enclosing_Instance (E),
6803 Inst => Inst);
6804 end if;
6805
6806 return True;
6807 end Is_Extraneously_Visible;
6808
6809 ---------------------------------
6810 -- Is_Generic_Actual_Subp_Name --
6811 ---------------------------------
6812
6813 function Is_Generic_Actual_Subp_Name
6814 (N : Node_Id) return Boolean
6815 is
6816 Decl : constant Node_Id := Enclosing_Declaration (N);
6817 begin
6818 return Nkind (Decl) = N_Subprogram_Renaming_Declaration
6819 and then Present (Corresponding_Formal_Spec (Decl));
6820 end Is_Generic_Actual_Subp_Name;
6821
6822 I : Interp_Index;
6823 It : Interp;
6824 Inst : Entity_Id := Current_Scope;
6825
6826 begin
6827 while Present (Inst)
6828 and then not Is_Generic_Instance (Inst)
6829 loop
6830 Inst := Scope (Inst);
6831 end loop;
6832
6833 if Present (Inst) then
6834 Get_First_Interp (N, I, It);
6835 while Present (It.Nam) loop
6836 if Is_Extraneously_Visible (E => It.Nam, Inst => Inst)
6837 and then not Is_Generic_Actual_Subp_Name (N)
6838 then
6839 Remove_Interp (I);
6840 end if;
6841 Get_Next_Interp (I, It);
6842 end loop;
6843 end if;
6844 end;
6845 end if;
6846
6847 -- If no homonyms were visible, the entity is unambiguous
6848
6849 if not Is_Overloaded (N) then
6850 if not Is_Actual_Parameter then
6851 Generate_Reference (E, N);
6852 end if;
6853 end if;
6854
6855 -- Case of non-overloadable entity, set the entity providing that
6856 -- we do not have the case of a discriminant reference within a
6857 -- default expression. Such references are replaced with the
6858 -- corresponding discriminal, which is the formal corresponding to
6859 -- to the discriminant in the initialization procedure.
6860
6861 else
6862 -- Entity is unambiguous, indicate that it is referenced here
6863
6864 -- For a renaming of an object, always generate simple reference,
6865 -- we don't try to keep track of assignments in this case, except
6866 -- in SPARK mode where renamings are traversed for generating
6867 -- local effects of subprograms.
6868
6869 if Is_Object (E)
6870 and then Present (Renamed_Object (E))
6871 and then not GNATprove_Mode
6872 then
6873 Generate_Reference (E, N);
6874
6875 -- If the renamed entity is a private protected component,
6876 -- reference the original component as well. This needs to be
6877 -- done because the private renamings are installed before any
6878 -- analysis has occurred. Reference to a private component will
6879 -- resolve to the renaming and the original component will be
6880 -- left unreferenced, hence the following.
6881
6882 if Is_Prival (E) then
6883 Generate_Reference (Prival_Link (E), N);
6884 end if;
6885
6886 -- One odd case is that we do not want to set the Referenced flag
6887 -- if the entity is a label, and the identifier is the label in
6888 -- the source, since this is not a reference from the point of
6889 -- view of the user.
6890
6891 elsif Nkind (Parent (N)) = N_Label then
6892 declare
6893 R : constant Boolean := Referenced (E);
6894
6895 begin
6896 -- Generate reference unless this is an actual parameter
6897 -- (see comment below).
6898
6899 if not Is_Actual_Parameter then
6900 Generate_Reference (E, N);
6901 Set_Referenced (E, R);
6902 end if;
6903 end;
6904
6905 -- Normal case, not a label: generate reference
6906
6907 else
6908 if not Is_Actual_Parameter then
6909
6910 -- Package or generic package is always a simple reference
6911
6912 if Is_Package_Or_Generic_Package (E) then
6913 Generate_Reference (E, N, 'r');
6914
6915 -- Else see if we have a left hand side
6916
6917 else
6918 case Known_To_Be_Assigned (N, Only_LHS => True) is
6919 when True =>
6920 Generate_Reference (E, N, 'm');
6921
6922 when False =>
6923 Generate_Reference (E, N, 'r');
6924
6925 end case;
6926 end if;
6927 end if;
6928 end if;
6929
6930 Set_Entity_Or_Discriminal (N, E);
6931
6932 -- The name may designate a generalized reference, in which case
6933 -- the dereference interpretation will be included. Context is
6934 -- one in which a name is legal.
6935
6936 if Ada_Version >= Ada_2012
6937 and then
6938 (Nkind (Parent (N)) in N_Subexpr
6939 or else Nkind (Parent (N)) in N_Assignment_Statement
6940 | N_Object_Declaration
6941 | N_Parameter_Association)
6942 then
6943 Check_Implicit_Dereference (N, Etype (E));
6944 end if;
6945 end if;
6946 end;
6947
6948 -- Mark relevant use-type and use-package clauses as effective if the
6949 -- node in question is not overloaded and therefore does not require
6950 -- resolution.
6951 --
6952 -- Note: Generic actual subprograms do not follow the normal resolution
6953 -- path, so ignore the fact that they are overloaded and mark them
6954 -- anyway.
6955
6956 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6957 Mark_Use_Clauses (N);
6958 end if;
6959
6960 -- Come here with entity set
6961
6962 <<Done>>
6963 Check_Restriction_No_Use_Of_Entity (N);
6964
6965 -- Annotate the tree by creating a variable reference marker in case the
6966 -- original variable reference is folded or optimized away. The variable
6967 -- reference marker is automatically saved for later examination by the
6968 -- ABE Processing phase. Variable references which act as actuals in a
6969 -- call require special processing and are left to Resolve_Actuals. The
6970 -- reference is a write when it appears on the left hand side of an
6971 -- assignment.
6972
6973 if Needs_Variable_Reference_Marker (N => N, Calls_OK => False) then
6974 declare
6975 Is_Assignment_LHS : constant Boolean := Known_To_Be_Assigned (N);
6976
6977 begin
6978 Build_Variable_Reference_Marker
6979 (N => N,
6980 Read => not Is_Assignment_LHS,
6981 Write => Is_Assignment_LHS);
6982 end;
6983 end if;
6984 end Find_Direct_Name;
6985
6986 ------------------------
6987 -- Find_Expanded_Name --
6988 ------------------------
6989
6990 -- This routine searches the homonym chain of the entity until it finds
6991 -- an entity declared in the scope denoted by the prefix. If the entity
6992 -- is private, it may nevertheless be immediately visible, if we are in
6993 -- the scope of its declaration.
6994
6995 procedure Find_Expanded_Name (N : Node_Id) is
6996 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean;
6997 -- Determine whether expanded name Nod appears within a pragma which is
6998 -- a suitable context for an abstract view of a state or variable. The
6999 -- following pragmas fall in this category:
7000 -- Depends
7001 -- Global
7002 -- Initializes
7003 -- Refined_Depends
7004 -- Refined_Global
7005 --
7006 -- In addition, pragma Abstract_State is also considered suitable even
7007 -- though it is an illegal context for an abstract view as this allows
7008 -- for proper resolution of abstract views of variables. This illegal
7009 -- context is later flagged in the analysis of indicator Part_Of.
7010
7011 -----------------------------
7012 -- In_Abstract_View_Pragma --
7013 -----------------------------
7014
7015 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is
7016 Par : Node_Id;
7017
7018 begin
7019 -- Climb the parent chain looking for a pragma
7020
7021 Par := Nod;
7022 while Present (Par) loop
7023 if Nkind (Par) = N_Pragma then
7024 if Pragma_Name_Unmapped (Par)
7025 in Name_Abstract_State
7026 | Name_Depends
7027 | Name_Global
7028 | Name_Initializes
7029 | Name_Refined_Depends
7030 | Name_Refined_Global
7031 then
7032 return True;
7033
7034 -- Otherwise the pragma is not a legal context for an abstract
7035 -- view.
7036
7037 else
7038 exit;
7039 end if;
7040
7041 -- Prevent the search from going too far
7042
7043 elsif Is_Body_Or_Package_Declaration (Par) then
7044 exit;
7045 end if;
7046
7047 Par := Parent (Par);
7048 end loop;
7049
7050 return False;
7051 end In_Abstract_View_Pragma;
7052
7053 -- Local variables
7054
7055 Selector : constant Node_Id := Selector_Name (N);
7056
7057 Candidate : Entity_Id := Empty;
7058 P_Name : Entity_Id;
7059 Id : Entity_Id;
7060
7061 -- Start of processing for Find_Expanded_Name
7062
7063 begin
7064 P_Name := Entity (Prefix (N));
7065
7066 -- If the prefix is a renamed package, look for the entity in the
7067 -- original package.
7068
7069 if Ekind (P_Name) = E_Package
7070 and then Present (Renamed_Entity (P_Name))
7071 then
7072 P_Name := Renamed_Entity (P_Name);
7073
7074 if From_Limited_With (P_Name)
7075 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
7076 then
7077 Error_Msg_NE
7078 ("renaming of limited view of package & not usable in this"
7079 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
7080
7081 elsif Renames_Limited_View (Entity (Prefix (N)))
7082 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
7083 and then not Is_Visible_Through_Renamings (P_Name)
7084 then
7085 Error_Msg_NE
7086 ("renaming of limited view of package & not usable in this"
7087 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
7088 end if;
7089
7090 -- Rewrite node with entity field pointing to renamed object
7091
7092 Rewrite (Prefix (N), New_Copy (Prefix (N)));
7093 Set_Entity (Prefix (N), P_Name);
7094
7095 -- If the prefix is an object of a concurrent type, look for
7096 -- the entity in the associated task or protected type.
7097
7098 elsif Is_Concurrent_Type (Etype (P_Name)) then
7099 P_Name := Etype (P_Name);
7100 end if;
7101
7102 Id := Current_Entity (Selector);
7103
7104 declare
7105 Is_New_Candidate : Boolean;
7106
7107 begin
7108 while Present (Id) loop
7109 if Scope (Id) = P_Name then
7110 Candidate := Id;
7111 Is_New_Candidate := True;
7112
7113 -- Handle abstract views of states and variables. These are
7114 -- acceptable candidates only when the reference to the view
7115 -- appears in certain pragmas.
7116
7117 if Ekind (Id) = E_Abstract_State
7118 and then From_Limited_With (Id)
7119 and then Present (Non_Limited_View (Id))
7120 then
7121 if In_Abstract_View_Pragma (N) then
7122 Candidate := Non_Limited_View (Id);
7123 Is_New_Candidate := True;
7124
7125 -- Hide the candidate because it is not used in a proper
7126 -- context.
7127
7128 else
7129 Candidate := Empty;
7130 Is_New_Candidate := False;
7131 end if;
7132 end if;
7133
7134 -- Ada 2005 (AI-217): Handle shadow entities associated with
7135 -- types declared in limited-withed nested packages. We don't need
7136 -- to handle E_Incomplete_Subtype entities because the entities
7137 -- in the limited view are always E_Incomplete_Type and
7138 -- E_Class_Wide_Type entities (see Build_Limited_Views).
7139
7140 -- Regarding the expression used to evaluate the scope, it
7141 -- is important to note that the limited view also has shadow
7142 -- entities associated nested packages. For this reason the
7143 -- correct scope of the entity is the scope of the real entity.
7144 -- The non-limited view may itself be incomplete, in which case
7145 -- get the full view if available.
7146
7147 elsif Ekind (Id) in E_Incomplete_Type | E_Class_Wide_Type
7148 and then From_Limited_With (Id)
7149 and then Present (Non_Limited_View (Id))
7150 and then Scope (Non_Limited_View (Id)) = P_Name
7151 then
7152 Candidate := Get_Full_View (Non_Limited_View (Id));
7153 Is_New_Candidate := True;
7154
7155 -- Handle special case where the prefix is a renaming of a shadow
7156 -- package which is visible. Required to avoid reporting spurious
7157 -- errors.
7158
7159 elsif Ekind (P_Name) = E_Package
7160 and then From_Limited_With (P_Name)
7161 and then not From_Limited_With (Id)
7162 and then Sloc (Scope (Id)) = Sloc (P_Name)
7163 and then Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
7164 then
7165 Candidate := Get_Full_View (Id);
7166 Is_New_Candidate := True;
7167
7168 -- An unusual case arises with a fully qualified name for an
7169 -- entity local to a generic child unit package, within an
7170 -- instantiation of that package. The name of the unit now
7171 -- denotes the renaming created within the instance. This is
7172 -- only relevant in an instance body, see below.
7173
7174 elsif Is_Generic_Instance (Scope (Id))
7175 and then In_Open_Scopes (Scope (Id))
7176 and then In_Instance_Body
7177 and then Ekind (Scope (Id)) = E_Package
7178 and then Ekind (Id) = E_Package
7179 and then Renamed_Entity (Id) = Scope (Id)
7180 and then Is_Immediately_Visible (P_Name)
7181 then
7182 Is_New_Candidate := True;
7183
7184 else
7185 Is_New_Candidate := False;
7186 end if;
7187
7188 if Is_New_Candidate then
7189
7190 -- If entity is a child unit, either it is a visible child of
7191 -- the prefix, or we are in the body of a generic prefix, as
7192 -- will happen when a child unit is instantiated in the body
7193 -- of a generic parent. This is because the instance body does
7194 -- not restore the full compilation context, given that all
7195 -- non-local references have been captured.
7196
7197 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
7198 exit when Is_Visible_Lib_Unit (Id)
7199 or else (Is_Child_Unit (Id)
7200 and then In_Open_Scopes (Scope (Id))
7201 and then In_Instance_Body);
7202 else
7203 exit when not Is_Hidden (Id);
7204 end if;
7205
7206 exit when Is_Immediately_Visible (Id);
7207 end if;
7208
7209 Id := Homonym (Id);
7210 end loop;
7211 end;
7212
7213 if No (Id)
7214 and then Ekind (P_Name) in E_Procedure | E_Function
7215 and then Is_Generic_Instance (P_Name)
7216 then
7217 -- Expanded name denotes entity in (instance of) generic subprogram.
7218 -- The entity may be in the subprogram instance, or may denote one of
7219 -- the formals, which is declared in the enclosing wrapper package.
7220
7221 P_Name := Scope (P_Name);
7222
7223 Id := Current_Entity (Selector);
7224 while Present (Id) loop
7225 exit when Scope (Id) = P_Name;
7226 Id := Homonym (Id);
7227 end loop;
7228 end if;
7229
7230 if No (Id) or else Chars (Id) /= Chars (Selector) then
7231 Set_Etype (N, Any_Type);
7232
7233 -- If we are looking for an entity defined in System, try to find it
7234 -- in the child package that may have been provided as an extension
7235 -- to System. The Extend_System pragma will have supplied the name of
7236 -- the extension, which may have to be loaded.
7237
7238 if Chars (P_Name) = Name_System
7239 and then Scope (P_Name) = Standard_Standard
7240 and then Present (System_Extend_Unit)
7241 and then Present_System_Aux (N)
7242 then
7243 Set_Entity (Prefix (N), System_Aux_Id);
7244 Find_Expanded_Name (N);
7245 return;
7246
7247 -- There is an implicit instance of the predefined operator in
7248 -- the given scope. The operator entity is defined in Standard.
7249 -- Has_Implicit_Operator makes the node into an Expanded_Name.
7250
7251 elsif Nkind (Selector) = N_Operator_Symbol
7252 and then Has_Implicit_Operator (N)
7253 then
7254 return;
7255
7256 -- If there is no literal defined in the scope denoted by the
7257 -- prefix, the literal may belong to (a type derived from)
7258 -- Standard_Character, for which we have no explicit literals.
7259
7260 elsif Nkind (Selector) = N_Character_Literal
7261 and then Has_Implicit_Character_Literal (N)
7262 then
7263 return;
7264
7265 else
7266 -- If the prefix is a single concurrent object, use its name in
7267 -- the error message, rather than that of the anonymous type.
7268
7269 if Is_Concurrent_Type (P_Name)
7270 and then Is_Internal_Name (Chars (P_Name))
7271 then
7272 Error_Msg_Node_2 := Entity (Prefix (N));
7273 else
7274 Error_Msg_Node_2 := P_Name;
7275 end if;
7276
7277 if P_Name = System_Aux_Id then
7278 P_Name := Scope (P_Name);
7279 Set_Entity (Prefix (N), P_Name);
7280 end if;
7281
7282 if Present (Candidate) then
7283
7284 -- If we know that the unit is a child unit we can give a more
7285 -- accurate error message.
7286
7287 if Is_Child_Unit (Candidate) then
7288
7289 -- If the candidate is a private child unit and we are in
7290 -- the visible part of a public unit, specialize the error
7291 -- message. There might be a private with_clause for it,
7292 -- but it is not currently active.
7293
7294 if Is_Private_Descendant (Candidate)
7295 and then Ekind (Current_Scope) = E_Package
7296 and then not In_Private_Part (Current_Scope)
7297 and then not Is_Private_Descendant (Current_Scope)
7298 then
7299 Error_Msg_N
7300 ("private child unit& is not visible here", Selector);
7301
7302 -- Normal case where we have a missing with for a child unit
7303
7304 else
7305 Error_Msg_Qual_Level := 99;
7306 Error_Msg_NE -- CODEFIX
7307 ("missing `WITH &;`", Selector, Candidate);
7308 Error_Msg_Qual_Level := 0;
7309 end if;
7310
7311 -- Here we don't know that this is a child unit
7312
7313 else
7314 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
7315 end if;
7316
7317 else
7318 -- Within the instantiation of a child unit, the prefix may
7319 -- denote the parent instance, but the selector has the name
7320 -- of the original child. That is to say, when A.B appears
7321 -- within an instantiation of generic child unit B, the scope
7322 -- stack includes an instance of A (P_Name) and an instance
7323 -- of B under some other name. We scan the scope to find this
7324 -- child instance, which is the desired entity.
7325 -- Note that the parent may itself be a child instance, if
7326 -- the reference is of the form A.B.C, in which case A.B has
7327 -- already been rewritten with the proper entity.
7328
7329 if In_Open_Scopes (P_Name)
7330 and then Is_Generic_Instance (P_Name)
7331 then
7332 declare
7333 Gen_Par : constant Entity_Id :=
7334 Generic_Parent (Specification
7335 (Unit_Declaration_Node (P_Name)));
7336 S : Entity_Id := Current_Scope;
7337 P : Entity_Id;
7338
7339 begin
7340 for J in reverse 0 .. Scope_Stack.Last loop
7341 S := Scope_Stack.Table (J).Entity;
7342
7343 exit when S = Standard_Standard;
7344
7345 if Ekind (S) in E_Function | E_Package | E_Procedure
7346 then
7347 P :=
7348 Generic_Parent (Specification
7349 (Unit_Declaration_Node (S)));
7350
7351 -- Check that P is a generic child of the generic
7352 -- parent of the prefix.
7353
7354 if Present (P)
7355 and then Chars (P) = Chars (Selector)
7356 and then Scope (P) = Gen_Par
7357 then
7358 Id := S;
7359 goto Found;
7360 end if;
7361 end if;
7362
7363 end loop;
7364 end;
7365 end if;
7366
7367 -- If this is a selection from Ada, System or Interfaces, then
7368 -- we assume a missing with for the corresponding package.
7369
7370 if Is_Known_Unit (N)
7371 and then not (Present (Entity (Prefix (N)))
7372 and then Scope (Entity (Prefix (N))) /=
7373 Standard_Standard)
7374 then
7375 if not Error_Posted (N) then
7376 Error_Msg_NE
7377 ("& is not a visible entity of&", Prefix (N), Selector);
7378 Error_Missing_With_Of_Known_Unit (Prefix (N));
7379 end if;
7380
7381 -- If this is a selection from a dummy package, then suppress
7382 -- the error message, of course the entity is missing if the
7383 -- package is missing.
7384
7385 elsif Sloc (Error_Msg_Node_2) = No_Location then
7386 null;
7387
7388 -- Here we have the case of an undefined component
7389
7390 else
7391 -- The prefix may hide a homonym in the context that
7392 -- declares the desired entity. This error can use a
7393 -- specialized message.
7394
7395 if In_Open_Scopes (P_Name) then
7396 declare
7397 H : constant Entity_Id := Homonym (P_Name);
7398
7399 begin
7400 if Present (H)
7401 and then Is_Compilation_Unit (H)
7402 and then
7403 (Is_Immediately_Visible (H)
7404 or else Is_Visible_Lib_Unit (H))
7405 then
7406 Id := First_Entity (H);
7407 while Present (Id) loop
7408 if Chars (Id) = Chars (Selector) then
7409 Error_Msg_Qual_Level := 99;
7410 Error_Msg_Name_1 := Chars (Selector);
7411 Error_Msg_NE
7412 ("% not declared in&", N, P_Name);
7413 Error_Msg_NE
7414 ("\use fully qualified name starting with "
7415 & "Standard to make& visible", N, H);
7416 Error_Msg_Qual_Level := 0;
7417 goto Done;
7418 end if;
7419
7420 Next_Entity (Id);
7421 end loop;
7422 end if;
7423
7424 -- If not found, standard error message
7425
7426 Error_Msg_NE ("& not declared in&", N, Selector);
7427
7428 <<Done>> null;
7429 end;
7430
7431 else
7432 -- Might be worth specializing the case when the prefix
7433 -- is a limited view.
7434 -- ... not declared in limited view of...
7435
7436 Error_Msg_NE ("& not declared in&", N, Selector);
7437 end if;
7438
7439 -- Check for misspelling of some entity in prefix
7440
7441 Id := First_Entity (P_Name);
7442 while Present (Id) loop
7443 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
7444 and then not Is_Internal_Name (Chars (Id))
7445 then
7446 Error_Msg_NE -- CODEFIX
7447 ("possible misspelling of&", Selector, Id);
7448 exit;
7449 end if;
7450
7451 Next_Entity (Id);
7452 end loop;
7453
7454 -- Specialize the message if this may be an instantiation
7455 -- of a child unit that was not mentioned in the context.
7456
7457 if Nkind (Parent (N)) = N_Package_Instantiation
7458 and then Is_Generic_Instance (Entity (Prefix (N)))
7459 and then Is_Compilation_Unit
7460 (Generic_Parent (Parent (Entity (Prefix (N)))))
7461 then
7462 Error_Msg_Node_2 := Selector;
7463 Error_Msg_N -- CODEFIX
7464 ("\missing `WITH &.&;`", Prefix (N));
7465 end if;
7466 end if;
7467 end if;
7468
7469 Id := Any_Id;
7470 end if;
7471 end if;
7472
7473 <<Found>>
7474 if Comes_From_Source (N)
7475 and then Is_Remote_Access_To_Subprogram_Type (Id)
7476 and then Ekind (Id) = E_Access_Subprogram_Type
7477 and then Present (Equivalent_Type (Id))
7478 then
7479 -- If we are not actually generating distribution code (i.e. the
7480 -- current PCS is the dummy non-distributed version), then the
7481 -- Equivalent_Type will be missing, and Id should be treated as
7482 -- a regular access-to-subprogram type.
7483
7484 Id := Equivalent_Type (Id);
7485 Set_Chars (Selector, Chars (Id));
7486 end if;
7487
7488 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
7489
7490 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
7491 if From_Limited_With (Id)
7492 or else Is_Type (Id)
7493 or else Ekind (Id) = E_Package
7494 then
7495 null;
7496 else
7497 Error_Msg_N
7498 ("limited withed package can only be used to access incomplete "
7499 & "types", N);
7500 end if;
7501 end if;
7502
7503 if Is_Task_Type (P_Name)
7504 and then ((Ekind (Id) = E_Entry
7505 and then Nkind (Parent (N)) /= N_Attribute_Reference)
7506 or else
7507 (Ekind (Id) = E_Entry_Family
7508 and then
7509 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
7510 then
7511 -- If both the task type and the entry are in scope, this may still
7512 -- be the expanded name of an entry formal.
7513
7514 if In_Open_Scopes (Id)
7515 and then Nkind (Parent (N)) = N_Selected_Component
7516 then
7517 null;
7518
7519 else
7520 -- It is an entry call after all, either to the current task
7521 -- (which will deadlock) or to an enclosing task.
7522
7523 Analyze_Selected_Component (N);
7524 return;
7525 end if;
7526 end if;
7527
7528 case Nkind (N) is
7529 when N_Selected_Component =>
7530 Reinit_Field_To_Zero (N, F_Is_Prefixed_Call);
7531 Change_Selected_Component_To_Expanded_Name (N);
7532
7533 when N_Expanded_Name =>
7534 null;
7535
7536 when others =>
7537 pragma Assert (False);
7538 end case;
7539
7540 -- Preserve relevant elaboration-related attributes of the context which
7541 -- are no longer available or very expensive to recompute once analysis,
7542 -- resolution, and expansion are over.
7543
7544 Mark_Elaboration_Attributes
7545 (N_Id => N,
7546 Checks => True,
7547 Modes => True,
7548 Warnings => True);
7549
7550 -- Set appropriate type
7551
7552 if Is_Type (Id) then
7553 Set_Etype (N, Id);
7554 else
7555 Set_Etype (N, Get_Full_View (Etype (Id)));
7556 end if;
7557
7558 -- Do style check and generate reference, but skip both steps if this
7559 -- entity has homonyms, since we may not have the right homonym set yet.
7560 -- The proper homonym will be set during the resolve phase.
7561
7562 if Has_Homonym (Id) then
7563 Set_Entity (N, Id);
7564
7565 else
7566 Set_Entity_Or_Discriminal (N, Id);
7567
7568 case Known_To_Be_Assigned (N, Only_LHS => True) is
7569 when True =>
7570 Generate_Reference (Id, N, 'm');
7571
7572 when False =>
7573 Generate_Reference (Id, N, 'r');
7574
7575 end case;
7576 end if;
7577
7578 -- Check for violation of No_Wide_Characters
7579
7580 Check_Wide_Character_Restriction (Id, N);
7581
7582 if Is_Self_Hidden (Id) then
7583 Premature_Usage (N);
7584
7585 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
7586 declare
7587 H : Entity_Id := Homonym (Id);
7588
7589 begin
7590 while Present (H) loop
7591 if Scope (H) = Scope (Id)
7592 and then (not Is_Hidden (H)
7593 or else Is_Immediately_Visible (H))
7594 then
7595 Collect_Interps (N);
7596 exit;
7597 end if;
7598
7599 H := Homonym (H);
7600 end loop;
7601
7602 -- If an extension of System is present, collect possible explicit
7603 -- overloadings declared in the extension.
7604
7605 if Chars (P_Name) = Name_System
7606 and then Scope (P_Name) = Standard_Standard
7607 and then Present (System_Extend_Unit)
7608 and then Present_System_Aux (N)
7609 then
7610 H := Current_Entity (Id);
7611
7612 while Present (H) loop
7613 if Scope (H) = System_Aux_Id then
7614 Add_One_Interp (N, H, Etype (H));
7615 end if;
7616
7617 H := Homonym (H);
7618 end loop;
7619 end if;
7620 end;
7621 end if;
7622
7623 if Nkind (Selector_Name (N)) = N_Operator_Symbol
7624 and then Scope (Id) /= Standard_Standard
7625 then
7626 -- In addition to user-defined operators in the given scope, there
7627 -- may be an implicit instance of the predefined operator. The
7628 -- operator (defined in Standard) is found in Has_Implicit_Operator,
7629 -- and added to the interpretations. Procedure Add_One_Interp will
7630 -- determine which hides which.
7631
7632 if Has_Implicit_Operator (N) then
7633 null;
7634 end if;
7635 end if;
7636
7637 -- If there is a single interpretation for N we can generate a
7638 -- reference to the unique entity found.
7639
7640 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
7641 Generate_Reference (Id, N);
7642 end if;
7643
7644 -- Mark relevant use-type and use-package clauses as effective if the
7645 -- node in question is not overloaded and therefore does not require
7646 -- resolution.
7647
7648 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
7649 Mark_Use_Clauses (N);
7650 end if;
7651
7652 Check_Restriction_No_Use_Of_Entity (N);
7653
7654 -- Annotate the tree by creating a variable reference marker in case the
7655 -- original variable reference is folded or optimized away. The variable
7656 -- reference marker is automatically saved for later examination by the
7657 -- ABE Processing phase. Variable references which act as actuals in a
7658 -- call require special processing and are left to Resolve_Actuals. The
7659 -- reference is a write when it appears on the left hand side of an
7660 -- assignment.
7661
7662 if Needs_Variable_Reference_Marker
7663 (N => N,
7664 Calls_OK => False)
7665 then
7666 declare
7667 Is_Assignment_LHS : constant Boolean := Known_To_Be_Assigned (N);
7668
7669 begin
7670 Build_Variable_Reference_Marker
7671 (N => N,
7672 Read => not Is_Assignment_LHS,
7673 Write => Is_Assignment_LHS);
7674 end;
7675 end if;
7676 end Find_Expanded_Name;
7677
7678 --------------------
7679 -- Find_First_Use --
7680 --------------------
7681
7682 function Find_First_Use (Use_Clause : Node_Id) return Node_Id is
7683 Curr : Node_Id;
7684
7685 begin
7686 -- Loop through the Prev_Use_Clause chain
7687
7688 Curr := Use_Clause;
7689 while Present (Prev_Use_Clause (Curr)) loop
7690 Curr := Prev_Use_Clause (Curr);
7691 end loop;
7692
7693 return Curr;
7694 end Find_First_Use;
7695
7696 -------------------------
7697 -- Find_Renamed_Entity --
7698 -------------------------
7699
7700 function Find_Renamed_Entity
7701 (N : Node_Id;
7702 Nam : Node_Id;
7703 New_S : Entity_Id;
7704 Is_Actual : Boolean := False) return Entity_Id
7705 is
7706 Ind : Interp_Index;
7707 I1 : Interp_Index := 0; -- Suppress junk warnings
7708 It : Interp;
7709 It1 : Interp;
7710 Old_S : Entity_Id;
7711 Inst : Entity_Id;
7712
7713 function Find_Nearer_Entity
7714 (New_S : Entity_Id;
7715 Old1_S : Entity_Id;
7716 Old2_S : Entity_Id) return Entity_Id;
7717 -- Determine whether one of Old_S1 and Old_S2 is nearer to New_S than
7718 -- the other, and return it if so. Return Empty otherwise. We use this
7719 -- in conjunction with Inherit_Renamed_Profile to simplify later type
7720 -- disambiguation for actual subprograms in instances.
7721
7722 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
7723 -- If the renamed entity is an implicit operator, check whether it is
7724 -- visible because its operand type is properly visible. This check
7725 -- applies to explicit renamed entities that appear in the source in a
7726 -- renaming declaration or a formal subprogram instance, but not to
7727 -- default generic actuals with a name.
7728
7729 function Report_Overload return Entity_Id;
7730 -- List possible interpretations, and specialize message in the
7731 -- case of a generic actual.
7732
7733 function Within (Inner, Outer : Entity_Id) return Boolean;
7734 -- Determine whether a candidate subprogram is defined within the
7735 -- enclosing instance. If yes, it has precedence over outer candidates.
7736
7737 --------------------------
7738 -- Find_Nearer_Entity --
7739 --------------------------
7740
7741 function Find_Nearer_Entity
7742 (New_S : Entity_Id;
7743 Old1_S : Entity_Id;
7744 Old2_S : Entity_Id) return Entity_Id
7745 is
7746 New_F : Entity_Id;
7747 Old1_F : Entity_Id;
7748 Old2_F : Entity_Id;
7749 Anc_T : Entity_Id;
7750
7751 begin
7752 New_F := First_Formal (New_S);
7753 Old1_F := First_Formal (Old1_S);
7754 Old2_F := First_Formal (Old2_S);
7755
7756 -- The criterion is whether the type of the formals of one of Old1_S
7757 -- and Old2_S is an ancestor subtype of the type of the corresponding
7758 -- formals of New_S while the other is not (we already know that they
7759 -- are all subtypes of the same base type).
7760
7761 -- This makes it possible to find the more correct renamed entity in
7762 -- the case of a generic instantiation nested in an enclosing one for
7763 -- which different formal types get the same actual type, which will
7764 -- in turn make it possible for Inherit_Renamed_Profile to preserve
7765 -- types on formal parameters and ultimately simplify disambiguation.
7766
7767 -- Consider the follow package G:
7768
7769 -- generic
7770 -- type Item_T is private;
7771 -- with function Compare (L, R: Item_T) return Boolean is <>;
7772
7773 -- type Bound_T is private;
7774 -- with function Compare (L, R : Bound_T) return Boolean is <>;
7775 -- package G is
7776 -- ...
7777 -- end G;
7778
7779 -- package body G is
7780 -- package My_Inner is Inner_G (Bound_T);
7781 -- ...
7782 -- end G;
7783
7784 -- with the following package Inner_G:
7785
7786 -- generic
7787 -- type T is private;
7788 -- with function Compare (L, R: T) return Boolean is <>;
7789 -- package Inner_G is
7790 -- function "<" (L, R: T) return Boolean is (Compare (L, R));
7791 -- end Inner_G;
7792
7793 -- If G is instantiated on the same actual type with a single Compare
7794 -- function:
7795
7796 -- type T is ...
7797 -- function Compare (L, R : T) return Boolean;
7798 -- package My_G is new (T, T);
7799
7800 -- then the renaming generated for Compare in the inner instantiation
7801 -- is ambiguous: it can rename either of the renamings generated for
7802 -- the outer instantiation. Now if the first one is picked up, then
7803 -- the subtypes of the formal parameters of the renaming will not be
7804 -- preserved in Inherit_Renamed_Profile because they are subtypes of
7805 -- the Bound_T formal type and not of the Item_T formal type, so we
7806 -- need to arrange for the second one to be picked up instead.
7807
7808 while Present (New_F) loop
7809 if Etype (Old1_F) /= Etype (Old2_F) then
7810 Anc_T := Ancestor_Subtype (Etype (New_F));
7811
7812 if Etype (Old1_F) = Anc_T then
7813 return Old1_S;
7814 elsif Etype (Old2_F) = Anc_T then
7815 return Old2_S;
7816 end if;
7817 end if;
7818
7819 Next_Formal (New_F);
7820 Next_Formal (Old1_F);
7821 Next_Formal (Old2_F);
7822 end loop;
7823
7824 pragma Assert (No (Old1_F));
7825 pragma Assert (No (Old2_F));
7826
7827 return Empty;
7828 end Find_Nearer_Entity;
7829
7830 --------------------------
7831 -- Is_Visible_Operation --
7832 --------------------------
7833
7834 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
7835 Scop : Entity_Id;
7836 Typ : Entity_Id;
7837 Btyp : Entity_Id;
7838
7839 begin
7840 if Ekind (Op) /= E_Operator
7841 or else Scope (Op) /= Standard_Standard
7842 or else (In_Instance
7843 and then (not Is_Actual
7844 or else Present (Enclosing_Instance)))
7845 then
7846 return True;
7847
7848 else
7849 -- For a fixed point type operator, check the resulting type,
7850 -- because it may be a mixed mode integer * fixed operation.
7851
7852 if Present (Next_Formal (First_Formal (New_S)))
7853 and then Is_Fixed_Point_Type (Etype (New_S))
7854 then
7855 Typ := Etype (New_S);
7856 else
7857 Typ := Etype (First_Formal (New_S));
7858 end if;
7859
7860 Btyp := Base_Type (Typ);
7861
7862 if Nkind (Nam) /= N_Expanded_Name then
7863 return (In_Open_Scopes (Scope (Btyp))
7864 or else Is_Potentially_Use_Visible (Btyp)
7865 or else In_Use (Btyp)
7866 or else In_Use (Scope (Btyp)));
7867
7868 else
7869 Scop := Entity (Prefix (Nam));
7870
7871 if Ekind (Scop) = E_Package
7872 and then Present (Renamed_Entity (Scop))
7873 then
7874 Scop := Renamed_Entity (Scop);
7875 end if;
7876
7877 -- Operator is visible if prefix of expanded name denotes
7878 -- scope of type, or else type is defined in System_Aux
7879 -- and the prefix denotes System.
7880
7881 return Scope (Btyp) = Scop
7882 or else (Scope (Btyp) = System_Aux_Id
7883 and then Scope (Scope (Btyp)) = Scop);
7884 end if;
7885 end if;
7886 end Is_Visible_Operation;
7887
7888 ------------
7889 -- Within --
7890 ------------
7891
7892 function Within (Inner, Outer : Entity_Id) return Boolean is
7893 Sc : Entity_Id;
7894
7895 begin
7896 Sc := Scope (Inner);
7897 while Sc /= Standard_Standard loop
7898 if Sc = Outer then
7899 return True;
7900 else
7901 Sc := Scope (Sc);
7902 end if;
7903 end loop;
7904
7905 return False;
7906 end Within;
7907
7908 ---------------------
7909 -- Report_Overload --
7910 ---------------------
7911
7912 function Report_Overload return Entity_Id is
7913 begin
7914 if Is_Actual then
7915 Error_Msg_NE -- CODEFIX
7916 ("ambiguous actual subprogram&, " &
7917 "possible interpretations:", N, Nam);
7918 else
7919 Error_Msg_N -- CODEFIX
7920 ("ambiguous subprogram, " &
7921 "possible interpretations:", N);
7922 end if;
7923
7924 List_Interps (Nam, N);
7925 return Old_S;
7926 end Report_Overload;
7927
7928 -- Start of processing for Find_Renamed_Entity
7929
7930 begin
7931 Old_S := Any_Id;
7932 Candidate_Renaming := Empty;
7933
7934 if Is_Overloaded (Nam) then
7935 Get_First_Interp (Nam, Ind, It);
7936 while Present (It.Nam) loop
7937 if Entity_Matches_Spec (It.Nam, New_S)
7938 and then Is_Visible_Operation (It.Nam)
7939 then
7940 if Old_S /= Any_Id then
7941
7942 -- Note: The call to Disambiguate only happens if a
7943 -- previous interpretation was found, in which case I1
7944 -- has received a value.
7945
7946 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
7947
7948 if It1 = No_Interp then
7949 Inst := Enclosing_Instance;
7950
7951 if Present (Inst) then
7952 if Within (It.Nam, Inst) then
7953 if Within (Old_S, Inst) then
7954 declare
7955 It_D : constant Uint :=
7956 Scope_Depth_Default_0 (It.Nam);
7957 Old_D : constant Uint :=
7958 Scope_Depth_Default_0 (Old_S);
7959 N_Ent : Entity_Id;
7960 begin
7961 -- Choose the innermost subprogram, which
7962 -- would hide the outer one in the generic.
7963
7964 if Old_D > It_D then
7965 return Old_S;
7966 elsif It_D > Old_D then
7967 return It.Nam;
7968 end if;
7969
7970 -- Otherwise, if we can determine that one
7971 -- of the entities is nearer to the renaming
7972 -- than the other, choose it. If not, then
7973 -- return the newer one as done historically.
7974
7975 N_Ent :=
7976 Find_Nearer_Entity (New_S, Old_S, It.Nam);
7977 if Present (N_Ent) then
7978 return N_Ent;
7979 else
7980 return It.Nam;
7981 end if;
7982 end;
7983 end if;
7984
7985 elsif Within (Old_S, Inst) then
7986 return Old_S;
7987
7988 else
7989 return Report_Overload;
7990 end if;
7991
7992 -- If not within an instance, ambiguity is real
7993
7994 else
7995 return Report_Overload;
7996 end if;
7997
7998 else
7999 Old_S := It1.Nam;
8000 exit;
8001 end if;
8002
8003 else
8004 I1 := Ind;
8005 Old_S := It.Nam;
8006 end if;
8007
8008 elsif
8009 Present (First_Formal (It.Nam))
8010 and then Present (First_Formal (New_S))
8011 and then Base_Type (Etype (First_Formal (It.Nam))) =
8012 Base_Type (Etype (First_Formal (New_S)))
8013 then
8014 Candidate_Renaming := It.Nam;
8015 end if;
8016
8017 Get_Next_Interp (Ind, It);
8018 end loop;
8019
8020 Set_Entity (Nam, Old_S);
8021
8022 if Old_S /= Any_Id then
8023 Set_Is_Overloaded (Nam, False);
8024 end if;
8025
8026 -- Non-overloaded case
8027
8028 else
8029 if Is_Actual
8030 and then Present (Enclosing_Instance)
8031 and then Entity_Matches_Spec (Entity (Nam), New_S)
8032 then
8033 Old_S := Entity (Nam);
8034
8035 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
8036 Candidate_Renaming := New_S;
8037
8038 if Is_Visible_Operation (Entity (Nam)) then
8039 Old_S := Entity (Nam);
8040 end if;
8041
8042 elsif Present (First_Formal (Entity (Nam)))
8043 and then Present (First_Formal (New_S))
8044 and then Base_Type (Etype (First_Formal (Entity (Nam)))) =
8045 Base_Type (Etype (First_Formal (New_S)))
8046 then
8047 Candidate_Renaming := Entity (Nam);
8048 end if;
8049 end if;
8050
8051 return Old_S;
8052 end Find_Renamed_Entity;
8053
8054 -----------------------------
8055 -- Find_Selected_Component --
8056 -----------------------------
8057
8058 procedure Find_Selected_Component (N : Node_Id) is
8059 P : constant Node_Id := Prefix (N);
8060
8061 P_Name : Entity_Id;
8062 -- Entity denoted by prefix
8063
8064 P_Type : Entity_Id;
8065 -- and its type
8066
8067 Nam : Node_Id;
8068
8069 function Available_Subtype return Boolean;
8070 -- A small optimization: if the prefix is constrained and the component
8071 -- is an array type we may already have a usable subtype for it, so we
8072 -- can use it rather than generating a new one, because the bounds
8073 -- will be the values of the discriminants and not discriminant refs.
8074 -- This simplifies value tracing in GNATprove. For consistency, both
8075 -- the entity name and the subtype come from the constrained component.
8076
8077 -- This is only used in GNATprove mode: when generating code it may be
8078 -- necessary to create an itype in the scope of use of the selected
8079 -- component, e.g. in the context of a expanded record equality.
8080
8081 function Is_Reference_In_Subunit return Boolean;
8082 -- In a subunit, the scope depth is not a proper measure of hiding,
8083 -- because the context of the proper body may itself hide entities in
8084 -- parent units. This rare case requires inspecting the tree directly
8085 -- because the proper body is inserted in the main unit and its context
8086 -- is simply added to that of the parent.
8087
8088 -----------------------
8089 -- Available_Subtype --
8090 -----------------------
8091
8092 function Available_Subtype return Boolean is
8093 Comp : Entity_Id;
8094
8095 begin
8096 if GNATprove_Mode then
8097 Comp := First_Entity (Etype (P));
8098 while Present (Comp) loop
8099 if Chars (Comp) = Chars (Selector_Name (N)) then
8100 Set_Etype (N, Etype (Comp));
8101 Set_Entity (Selector_Name (N), Comp);
8102 Set_Etype (Selector_Name (N), Etype (Comp));
8103 return True;
8104 end if;
8105
8106 Next_Component (Comp);
8107 end loop;
8108 end if;
8109
8110 return False;
8111 end Available_Subtype;
8112
8113 -----------------------------
8114 -- Is_Reference_In_Subunit --
8115 -----------------------------
8116
8117 function Is_Reference_In_Subunit return Boolean is
8118 Clause : Node_Id;
8119 Comp_Unit : Node_Id;
8120
8121 begin
8122 Comp_Unit := N;
8123 while Present (Comp_Unit)
8124 and then Nkind (Comp_Unit) /= N_Compilation_Unit
8125 loop
8126 Comp_Unit := Parent (Comp_Unit);
8127 end loop;
8128
8129 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
8130 return False;
8131 end if;
8132
8133 -- Now check whether the package is in the context of the subunit
8134
8135 Clause := First (Context_Items (Comp_Unit));
8136 while Present (Clause) loop
8137 if Nkind (Clause) = N_With_Clause
8138 and then Entity (Name (Clause)) = P_Name
8139 then
8140 return True;
8141 end if;
8142
8143 Next (Clause);
8144 end loop;
8145
8146 return False;
8147 end Is_Reference_In_Subunit;
8148
8149 -- Start of processing for Find_Selected_Component
8150
8151 begin
8152 Analyze (P);
8153
8154 if Nkind (P) = N_Error then
8155 return;
8156 end if;
8157
8158 -- If the selector already has an entity, the node has been constructed
8159 -- in the course of expansion, and is known to be valid. Do not verify
8160 -- that it is defined for the type (it may be a private component used
8161 -- in the expansion of record equality).
8162
8163 if Present (Entity (Selector_Name (N))) then
8164 if No (Etype (N)) or else Etype (N) = Any_Type then
8165 declare
8166 Sel_Name : constant Node_Id := Selector_Name (N);
8167 Selector : constant Entity_Id := Entity (Sel_Name);
8168 C_Etype : Node_Id;
8169
8170 begin
8171 Set_Etype (Sel_Name, Etype (Selector));
8172
8173 if not Is_Entity_Name (P) then
8174 Resolve (P);
8175 end if;
8176
8177 -- Build an actual subtype except for the first parameter
8178 -- of an init proc, where this actual subtype is by
8179 -- definition incorrect, since the object is uninitialized
8180 -- (and does not even have defined discriminants etc.)
8181
8182 if Is_Entity_Name (P)
8183 and then Ekind (Entity (P)) = E_Function
8184 then
8185 Nam := New_Copy (P);
8186
8187 if Is_Overloaded (P) then
8188 Save_Interps (P, Nam);
8189 end if;
8190
8191 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
8192 Analyze_Call (P);
8193 Analyze_Selected_Component (N);
8194 return;
8195
8196 elsif Ekind (Selector) = E_Component
8197 and then (not Is_Entity_Name (P)
8198 or else Chars (Entity (P)) /= Name_uInit)
8199 then
8200 -- Check if we already have an available subtype we can use
8201
8202 if Ekind (Etype (P)) = E_Record_Subtype
8203 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
8204 and then Is_Array_Type (Etype (Selector))
8205 and then not Is_Packed (Etype (Selector))
8206 and then Available_Subtype
8207 then
8208 return;
8209
8210 -- Do not build the subtype when referencing components of
8211 -- dispatch table wrappers. Required to avoid generating
8212 -- elaboration code with HI runtimes.
8213
8214 elsif Is_RTE (Scope (Selector), RE_Dispatch_Table_Wrapper)
8215 or else
8216 Is_RTE (Scope (Selector), RE_No_Dispatch_Table_Wrapper)
8217 then
8218 C_Etype := Empty;
8219 else
8220 C_Etype :=
8221 Build_Actual_Subtype_Of_Component
8222 (Etype (Selector), N);
8223 end if;
8224
8225 else
8226 C_Etype := Empty;
8227 end if;
8228
8229 if No (C_Etype) then
8230 C_Etype := Etype (Selector);
8231 else
8232 Insert_Action (N, C_Etype);
8233 C_Etype := Defining_Identifier (C_Etype);
8234 end if;
8235
8236 Set_Etype (N, C_Etype);
8237 end;
8238
8239 -- If the selected component appears within a default expression
8240 -- and it has an actual subtype, the preanalysis has not yet
8241 -- completed its analysis, because Insert_Actions is disabled in
8242 -- that context. Within the init proc of the enclosing type we
8243 -- must complete this analysis, if an actual subtype was created.
8244
8245 elsif Inside_Init_Proc then
8246 declare
8247 Typ : constant Entity_Id := Etype (N);
8248 Decl : constant Node_Id := Declaration_Node (Typ);
8249 begin
8250 if Nkind (Decl) = N_Subtype_Declaration
8251 and then not Analyzed (Decl)
8252 and then Is_List_Member (Decl)
8253 and then No (Parent (Decl))
8254 then
8255 Remove (Decl);
8256 Insert_Action (N, Decl);
8257 end if;
8258 end;
8259 end if;
8260
8261 return;
8262
8263 elsif Is_Entity_Name (P) then
8264 P_Name := Entity (P);
8265
8266 -- The prefix may denote an enclosing type which is the completion
8267 -- of an incomplete type declaration.
8268
8269 if Is_Type (P_Name) then
8270 Set_Entity (P, Get_Full_View (P_Name));
8271 Set_Etype (P, Entity (P));
8272 P_Name := Entity (P);
8273 end if;
8274
8275 P_Type := Base_Type (Etype (P));
8276
8277 if Debug_Flag_E then
8278 Write_Str ("Found prefix type to be ");
8279 Write_Entity_Info (P_Type, " "); Write_Eol;
8280 end if;
8281
8282 -- If the prefix's type is an access type, get to the record type
8283
8284 if Is_Access_Type (P_Type) then
8285 P_Type := Implicitly_Designated_Type (P_Type);
8286 end if;
8287
8288 -- First check for components of a record object (not the result of
8289 -- a call, which is handled below). This also covers the case where
8290 -- the extension feature that supports the prefixed form of calls
8291 -- for primitives of untagged types is enabled (excluding concurrent
8292 -- cases, which are handled further below).
8293
8294 if Is_Type (P_Type)
8295 and then (Has_Components (P_Type)
8296 or else (Core_Extensions_Allowed
8297 and then not Is_Concurrent_Type (P_Type)))
8298 and then not Is_Overloadable (P_Name)
8299 and then not Is_Type (P_Name)
8300 then
8301 -- Selected component of record. Type checking will validate
8302 -- name of selector.
8303
8304 -- ??? Could we rewrite an implicit dereference into an explicit
8305 -- one here?
8306
8307 Analyze_Selected_Component (N);
8308
8309 -- Reference to type name in predicate/invariant expression
8310
8311 elsif Is_Concurrent_Type (P_Type)
8312 and then not In_Open_Scopes (P_Name)
8313 and then (not Is_Concurrent_Type (Etype (P_Name))
8314 or else not In_Open_Scopes (Etype (P_Name)))
8315 then
8316 -- Call to protected operation or entry. Type checking is
8317 -- needed on the prefix.
8318
8319 Analyze_Selected_Component (N);
8320
8321 elsif (In_Open_Scopes (P_Name)
8322 and then Ekind (P_Name) /= E_Void
8323 and then not Is_Overloadable (P_Name))
8324 or else (Is_Concurrent_Type (Etype (P_Name))
8325 and then In_Open_Scopes (Etype (P_Name)))
8326 then
8327 -- Prefix denotes an enclosing loop, block, or task, i.e. an
8328 -- enclosing construct that is not a subprogram or accept.
8329
8330 -- A special case: a protected body may call an operation
8331 -- on an external object of the same type, in which case it
8332 -- is not an expanded name. If the prefix is the type itself,
8333 -- or the context is a single synchronized object it can only
8334 -- be interpreted as an expanded name.
8335
8336 if Is_Concurrent_Type (Etype (P_Name)) then
8337 if Is_Type (P_Name)
8338 or else Present (Anonymous_Object (Etype (P_Name)))
8339 then
8340 Find_Expanded_Name (N);
8341
8342 else
8343 Analyze_Selected_Component (N);
8344 return;
8345 end if;
8346
8347 else
8348 Find_Expanded_Name (N);
8349 end if;
8350
8351 elsif Ekind (P_Name) = E_Package then
8352 Find_Expanded_Name (N);
8353
8354 elsif Is_Overloadable (P_Name) then
8355
8356 -- The subprogram may be a renaming (of an enclosing scope) as
8357 -- in the case of the name of the generic within an instantiation.
8358
8359 if Ekind (P_Name) in E_Procedure | E_Function
8360 and then Present (Alias (P_Name))
8361 and then Is_Generic_Instance (Alias (P_Name))
8362 then
8363 P_Name := Alias (P_Name);
8364 end if;
8365
8366 if Is_Overloaded (P) then
8367
8368 -- The prefix must resolve to a unique enclosing construct
8369
8370 declare
8371 Found : Boolean := False;
8372 Ind : Interp_Index;
8373 It : Interp;
8374
8375 begin
8376 Get_First_Interp (P, Ind, It);
8377 while Present (It.Nam) loop
8378 if In_Open_Scopes (It.Nam) then
8379 if Found then
8380 Error_Msg_N (
8381 "prefix must be unique enclosing scope", N);
8382 Set_Entity (N, Any_Id);
8383 Set_Etype (N, Any_Type);
8384 return;
8385
8386 else
8387 Found := True;
8388 P_Name := It.Nam;
8389 end if;
8390 end if;
8391
8392 Get_Next_Interp (Ind, It);
8393 end loop;
8394 end;
8395 end if;
8396
8397 if In_Open_Scopes (P_Name) then
8398 Set_Entity (P, P_Name);
8399 Set_Is_Overloaded (P, False);
8400 Find_Expanded_Name (N);
8401
8402 else
8403 -- If no interpretation as an expanded name is possible, it
8404 -- must be a selected component of a record returned by a
8405 -- function call. Reformat prefix as a function call, the rest
8406 -- is done by type resolution.
8407
8408 -- Error if the prefix is procedure or entry, as is P.X
8409
8410 if Ekind (P_Name) /= E_Function
8411 and then
8412 (not Is_Overloaded (P)
8413 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
8414 then
8415 -- Prefix may mention a package that is hidden by a local
8416 -- declaration: let the user know. Scan the full homonym
8417 -- chain, the candidate package may be anywhere on it.
8418
8419 if Present (Homonym (Current_Entity (P_Name))) then
8420 P_Name := Current_Entity (P_Name);
8421
8422 while Present (P_Name) loop
8423 exit when Ekind (P_Name) = E_Package;
8424 P_Name := Homonym (P_Name);
8425 end loop;
8426
8427 if Present (P_Name) then
8428 if not Is_Reference_In_Subunit then
8429 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
8430 Error_Msg_NE
8431 ("package& is hidden by declaration#", N, P_Name);
8432 end if;
8433
8434 Set_Entity (Prefix (N), P_Name);
8435 Find_Expanded_Name (N);
8436 return;
8437
8438 else
8439 P_Name := Entity (Prefix (N));
8440 end if;
8441 end if;
8442
8443 Error_Msg_NE
8444 ("invalid prefix in selected component&", N, P_Name);
8445 Change_Selected_Component_To_Expanded_Name (N);
8446 Set_Entity (N, Any_Id);
8447 Set_Etype (N, Any_Type);
8448
8449 -- Here we have a function call, so do the reformatting
8450
8451 else
8452 Nam := New_Copy (P);
8453 Save_Interps (P, Nam);
8454
8455 -- We use Replace here because this is one of those cases
8456 -- where the parser has missclassified the node, and we fix
8457 -- things up and then do the semantic analysis on the fixed
8458 -- up node. Normally we do this using one of the Sinfo.CN
8459 -- routines, but this is too tricky for that.
8460
8461 -- Note that using Rewrite would be wrong, because we would
8462 -- have a tree where the original node is unanalyzed.
8463
8464 Replace (P,
8465 Make_Function_Call (Sloc (P), Name => Nam));
8466
8467 -- Now analyze the reformatted node
8468
8469 Analyze_Call (P);
8470
8471 -- If the prefix is illegal after this transformation, there
8472 -- may be visibility errors on the prefix. The safest is to
8473 -- treat the selected component as an error.
8474
8475 if Error_Posted (P) then
8476 Set_Etype (N, Any_Type);
8477 return;
8478
8479 else
8480 Analyze_Selected_Component (N);
8481 end if;
8482 end if;
8483 end if;
8484
8485 -- Remaining cases generate various error messages
8486
8487 else
8488 -- Format node as expanded name, to avoid cascaded errors
8489
8490 Change_Selected_Component_To_Expanded_Name (N);
8491 Set_Entity (N, Any_Id);
8492 Set_Etype (N, Any_Type);
8493
8494 -- Issue error message, but avoid this if error issued already.
8495 -- Use identifier of prefix if one is available.
8496
8497 if P_Name = Any_Id then
8498 null;
8499
8500 -- It is not an error if the prefix is the current instance of
8501 -- type name, e.g. the expression of a type aspect, when it is
8502 -- analyzed within a generic unit. We still have to verify that
8503 -- a component of that name exists, and decorate the node
8504 -- accordingly.
8505
8506 elsif Inside_A_Generic
8507 and then Is_Entity_Name (P)
8508 and then Is_Current_Instance (P)
8509 then
8510 declare
8511 Comp : Entity_Id;
8512
8513 begin
8514 Comp := First_Entity (Entity (P));
8515 while Present (Comp) loop
8516 if Chars (Comp) = Chars (Selector_Name (N)) then
8517 Set_Entity (N, Comp);
8518 Set_Etype (N, Etype (Comp));
8519 Set_Entity (Selector_Name (N), Comp);
8520 Set_Etype (Selector_Name (N), Etype (Comp));
8521 return;
8522 end if;
8523
8524 Next_Entity (Comp);
8525 end loop;
8526 end;
8527
8528 elsif Is_Self_Hidden (P_Name) then
8529 Premature_Usage (P);
8530
8531 elsif Ekind (P_Name) = E_Generic_Package then
8532 Error_Msg_N ("prefix must not be a generic package", N);
8533 Error_Msg_N ("\use package instantiation as prefix instead", N);
8534
8535 elsif Nkind (P) /= N_Attribute_Reference then
8536
8537 -- This may have been meant as a prefixed call to a primitive
8538 -- of an untagged type. If it is a function call check type of
8539 -- its first formal and add explanation.
8540
8541 declare
8542 F : constant Entity_Id :=
8543 Current_Entity (Selector_Name (N));
8544 begin
8545 if Present (F)
8546 and then Is_Subprogram (F)
8547 and then Present (First_Entity (F))
8548 and then not Is_Tagged_Type (Etype (First_Entity (F)))
8549 then
8550 Error_Msg_N
8551 ("prefixed call is only allowed for objects of a "
8552 & "tagged type unless -gnatX is used", N);
8553
8554 if not Core_Extensions_Allowed
8555 and then
8556 Try_Object_Operation (N, Allow_Extensions => True)
8557 then
8558 Error_Msg_N
8559 ("\using -gnatX would make the prefixed call legal",
8560 N);
8561 end if;
8562 end if;
8563 end;
8564
8565 Error_Msg_N ("invalid prefix in selected component&", P);
8566
8567 if Is_Incomplete_Type (P_Type)
8568 and then Is_Access_Type (Etype (P))
8569 then
8570 Error_Msg_N
8571 ("\dereference must not be of an incomplete type "
8572 & "(RM 3.10.1)", P);
8573 end if;
8574
8575 else
8576 Error_Msg_N ("invalid prefix in selected component", P);
8577 end if;
8578 end if;
8579 else
8580 -- If prefix is not the name of an entity, it must be an expression,
8581 -- whose type is appropriate for a record. This is determined by
8582 -- type resolution.
8583
8584 Analyze_Selected_Component (N);
8585 end if;
8586
8587 Analyze_Dimension (N);
8588 end Find_Selected_Component;
8589
8590 ---------------
8591 -- Find_Type --
8592 ---------------
8593
8594 procedure Find_Type (N : Node_Id) is
8595 C : Entity_Id;
8596 Typ : Entity_Id;
8597 T : Entity_Id;
8598 T_Name : Entity_Id;
8599
8600 begin
8601 if N = Error then
8602 return;
8603
8604 elsif Nkind (N) = N_Attribute_Reference then
8605
8606 -- Class attribute. This is not valid in Ada 83 mode, but we do not
8607 -- need to enforce that at this point, since the declaration of the
8608 -- tagged type in the prefix would have been flagged already.
8609
8610 if Attribute_Name (N) = Name_Class then
8611 Check_Restriction (No_Dispatch, N);
8612 Find_Type (Prefix (N));
8613
8614 -- Propagate error from bad prefix
8615
8616 if Etype (Prefix (N)) = Any_Type then
8617 Set_Entity (N, Any_Type);
8618 Set_Etype (N, Any_Type);
8619 return;
8620 end if;
8621
8622 T := Base_Type (Entity (Prefix (N)));
8623
8624 -- Case where type is not known to be tagged. Its appearance in
8625 -- the prefix of the 'Class attribute indicates that the full view
8626 -- will be tagged.
8627
8628 if not Is_Tagged_Type (T) then
8629 if Ekind (T) = E_Incomplete_Type then
8630
8631 -- It is legal to denote the class type of an incomplete
8632 -- type. The full type will have to be tagged, of course.
8633 -- In Ada 2005 this usage is declared obsolescent, so we
8634 -- warn accordingly. This usage is only legal if the type
8635 -- is completed in the current scope, and not for a limited
8636 -- view of a type.
8637
8638 if Ada_Version >= Ada_2005 then
8639
8640 -- Test whether the Available_View of a limited type view
8641 -- is tagged, since the limited view may not be marked as
8642 -- tagged if the type itself has an untagged incomplete
8643 -- type view in its package.
8644
8645 if From_Limited_With (T)
8646 and then not Is_Tagged_Type (Available_View (T))
8647 then
8648 Error_Msg_N
8649 ("prefix of Class attribute must be tagged", N);
8650 Set_Etype (N, Any_Type);
8651 Set_Entity (N, Any_Type);
8652 return;
8653
8654 else
8655 if Restriction_Check_Required (No_Obsolescent_Features)
8656 then
8657 Check_Restriction
8658 (No_Obsolescent_Features, Prefix (N));
8659 end if;
8660
8661 if Warn_On_Obsolescent_Feature then
8662 Error_Msg_N
8663 ("applying ''Class to an untagged incomplete type"
8664 & " is an obsolescent feature (RM J.11)?r?", N);
8665 end if;
8666 end if;
8667 end if;
8668
8669 Set_Is_Tagged_Type (T);
8670 Set_Direct_Primitive_Operations (T, New_Elmt_List);
8671 Make_Class_Wide_Type (T);
8672 Set_Entity (N, Class_Wide_Type (T));
8673 Set_Etype (N, Class_Wide_Type (T));
8674
8675 elsif Ekind (T) = E_Private_Type
8676 and then not Is_Generic_Type (T)
8677 and then In_Private_Part (Scope (T))
8678 then
8679 -- The Class attribute can be applied to an untagged private
8680 -- type fulfilled by a tagged type prior to the full type
8681 -- declaration (but only within the parent package's private
8682 -- part). Create the class-wide type now and check that the
8683 -- full type is tagged later during its analysis. Note that
8684 -- we do not mark the private type as tagged, unlike the
8685 -- case of incomplete types, because the type must still
8686 -- appear untagged to outside units.
8687
8688 if No (Class_Wide_Type (T)) then
8689 Make_Class_Wide_Type (T);
8690 end if;
8691
8692 Set_Entity (N, Class_Wide_Type (T));
8693 Set_Etype (N, Class_Wide_Type (T));
8694
8695 else
8696 -- Should we introduce a type Any_Tagged and use Wrong_Type
8697 -- here, it would be a bit more consistent???
8698
8699 Error_Msg_NE
8700 ("tagged type required, found}",
8701 Prefix (N), First_Subtype (T));
8702 Set_Entity (N, Any_Type);
8703 return;
8704 end if;
8705
8706 -- Case of tagged type
8707
8708 else
8709 if Is_Concurrent_Type (T) then
8710 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
8711
8712 -- Previous error. Create a class-wide type for the
8713 -- synchronized type itself, with minimal semantic
8714 -- attributes, to catch other errors in some ACATS tests.
8715
8716 pragma Assert (Serious_Errors_Detected /= 0);
8717 Make_Class_Wide_Type (T);
8718 C := Class_Wide_Type (T);
8719 Set_First_Entity (C, First_Entity (T));
8720
8721 else
8722 C := Class_Wide_Type
8723 (Corresponding_Record_Type (Entity (Prefix (N))));
8724 end if;
8725
8726 else
8727 C := Class_Wide_Type (Entity (Prefix (N)));
8728 end if;
8729
8730 Set_Entity_With_Checks (N, C);
8731 Generate_Reference (C, N);
8732 Set_Etype (N, C);
8733 end if;
8734
8735 -- Base attribute, not allowed in Ada 83
8736
8737 elsif Attribute_Name (N) = Name_Base then
8738 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
8739 Error_Msg_N
8740 ("(Ada 83) Base attribute not allowed in subtype mark", N);
8741
8742 else
8743 Find_Type (Prefix (N));
8744 Typ := Entity (Prefix (N));
8745
8746 if Ada_Version >= Ada_95
8747 and then not Is_Scalar_Type (Typ)
8748 and then not Is_Generic_Type (Typ)
8749 then
8750 Error_Msg_N
8751 ("prefix of Base attribute must be scalar type",
8752 Prefix (N));
8753
8754 elsif Warn_On_Redundant_Constructs
8755 and then Base_Type (Typ) = Typ
8756 then
8757 Error_Msg_NE -- CODEFIX
8758 ("redundant attribute, & is its own base type?r?", N, Typ);
8759 end if;
8760
8761 T := Base_Type (Typ);
8762
8763 -- Rewrite attribute reference with type itself (see similar
8764 -- processing in Analyze_Attribute, case Base). Preserve prefix
8765 -- if present, for other legality checks.
8766
8767 if Nkind (Prefix (N)) = N_Expanded_Name then
8768 Rewrite (N,
8769 Make_Expanded_Name (Sloc (N),
8770 Chars => Chars (T),
8771 Prefix => New_Copy (Prefix (Prefix (N))),
8772 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
8773
8774 else
8775 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
8776 end if;
8777
8778 Set_Entity (N, T);
8779 Set_Etype (N, T);
8780 end if;
8781
8782 elsif Attribute_Name (N) = Name_Stub_Type then
8783
8784 -- This is handled in Analyze_Attribute
8785
8786 Analyze (N);
8787
8788 -- All other attributes are invalid in a subtype mark
8789
8790 else
8791 Error_Msg_N ("invalid attribute in subtype mark", N);
8792 end if;
8793
8794 else
8795 Analyze (N);
8796
8797 if Is_Entity_Name (N) then
8798 T_Name := Entity (N);
8799 else
8800 Error_Msg_N ("subtype mark required in this context", N);
8801 Set_Etype (N, Any_Type);
8802 return;
8803 end if;
8804
8805 if T_Name = Any_Id or else Etype (N) = Any_Type then
8806
8807 -- Undefined id. Make it into a valid type
8808
8809 Set_Entity (N, Any_Type);
8810
8811 elsif not Is_Type (T_Name)
8812 and then T_Name /= Standard_Void_Type
8813 then
8814 Error_Msg_Sloc := Sloc (T_Name);
8815 Error_Msg_N ("subtype mark required in this context", N);
8816 Error_Msg_NE ("\\found & declared#", N, T_Name);
8817 Set_Entity (N, Any_Type);
8818
8819 elsif Is_Current_Instance (N) and then Comes_From_Source (N) then
8820 if Nkind (Parent (T_Name)) = N_Subtype_Declaration then
8821 Error_Msg_N ("reference to current instance of subtype" &
8822 " does not denote a subtype (RM 8.6)", N);
8823 else
8824 Error_Msg_N ("reference to current instance of type" &
8825 " does not denote a type (RM 8.6)", N);
8826 end if;
8827 Set_Entity (N, Any_Type);
8828
8829 else
8830 -- If the type is an incomplete type created to handle
8831 -- anonymous access components of a record type, then the
8832 -- incomplete type is the visible entity and subsequent
8833 -- references will point to it. Mark the original full
8834 -- type as referenced, to prevent spurious warnings.
8835
8836 if Is_Incomplete_Type (T_Name)
8837 and then Present (Full_View (T_Name))
8838 and then not Comes_From_Source (T_Name)
8839 then
8840 Set_Referenced (Full_View (T_Name));
8841 end if;
8842
8843 T_Name := Get_Full_View (T_Name);
8844
8845 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
8846 -- limited-with clauses
8847
8848 if From_Limited_With (T_Name)
8849 and then Is_Incomplete_Type (T_Name)
8850 and then Present (Non_Limited_View (T_Name))
8851 and then Is_Interface (Non_Limited_View (T_Name))
8852 then
8853 T_Name := Non_Limited_View (T_Name);
8854 end if;
8855
8856 if In_Open_Scopes (T_Name) then
8857 if Ekind (Base_Type (T_Name)) = E_Task_Type then
8858
8859 -- OK if the "current instance" rule does not apply.
8860
8861 if not Is_Current_Instance (N) then
8862 Set_Entity (N, T_Name);
8863 Set_Etype (N, T_Name);
8864 return;
8865
8866 else
8867 Error_Msg_N
8868 ("task type cannot be used as type mark " &
8869 "within its own spec or body", N);
8870 end if;
8871
8872 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
8873
8874 -- OK if the "current instance" rule does not apply.
8875
8876 if not Is_Current_Instance (N) then
8877 Set_Entity (N, T_Name);
8878 Set_Etype (N, T_Name);
8879 return;
8880
8881 else
8882 Error_Msg_N
8883 ("protected type cannot be used as type mark " &
8884 "within its own spec or body", N);
8885 end if;
8886
8887 else
8888 Error_Msg_N ("type declaration cannot refer to itself", N);
8889 end if;
8890
8891 Set_Etype (N, Any_Type);
8892 Set_Entity (N, Any_Type);
8893 Set_Error_Posted (T_Name);
8894 return;
8895 end if;
8896
8897 Set_Entity (N, T_Name);
8898 Set_Etype (N, T_Name);
8899 end if;
8900 end if;
8901
8902 if Present (Etype (N)) and then Comes_From_Source (N) then
8903 if Is_Fixed_Point_Type (Etype (N)) then
8904 Check_Restriction (No_Fixed_Point, N);
8905 elsif Is_Floating_Point_Type (Etype (N)) then
8906 Check_Restriction (No_Floating_Point, N);
8907 end if;
8908
8909 -- A Ghost type must appear in a specific context
8910
8911 if Is_Ghost_Entity (Etype (N)) then
8912 Check_Ghost_Context (Etype (N), N);
8913 end if;
8914 end if;
8915 end Find_Type;
8916
8917 --------------------
8918 -- Has_Components --
8919 --------------------
8920
8921 function Has_Components (Typ : Entity_Id) return Boolean is
8922 begin
8923 return Is_Record_Type (Typ)
8924 or else (Is_Private_Type (Typ) and then Has_Discriminants (Typ))
8925 or else (Is_Task_Type (Typ) and then Has_Discriminants (Typ))
8926 or else (Is_Incomplete_Type (Typ)
8927 and then From_Limited_With (Typ)
8928 and then Is_Record_Type (Available_View (Typ)));
8929 end Has_Components;
8930
8931 ------------------------------------
8932 -- Has_Implicit_Character_Literal --
8933 ------------------------------------
8934
8935 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
8936 Id : Entity_Id;
8937 Found : Boolean := False;
8938 P : constant Entity_Id := Entity (Prefix (N));
8939 Priv_Id : Entity_Id := Empty;
8940
8941 begin
8942 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8943 Priv_Id := First_Private_Entity (P);
8944 end if;
8945
8946 if P = Standard_Standard then
8947 Change_Selected_Component_To_Expanded_Name (N);
8948 Rewrite (N, Selector_Name (N));
8949 Analyze (N);
8950 Set_Etype (Original_Node (N), Standard_Character);
8951 return True;
8952 end if;
8953
8954 Id := First_Entity (P);
8955 while Present (Id) and then Id /= Priv_Id loop
8956 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
8957
8958 -- We replace the node with the literal itself, resolve as a
8959 -- character, and set the type correctly.
8960
8961 if not Found then
8962 Change_Selected_Component_To_Expanded_Name (N);
8963 Rewrite (N, Selector_Name (N));
8964 Analyze (N);
8965 Set_Etype (N, Id);
8966 Set_Etype (Original_Node (N), Id);
8967 Found := True;
8968
8969 else
8970 -- More than one type derived from Character in given scope.
8971 -- Collect all possible interpretations.
8972
8973 Add_One_Interp (N, Id, Id);
8974 end if;
8975 end if;
8976
8977 Next_Entity (Id);
8978 end loop;
8979
8980 return Found;
8981 end Has_Implicit_Character_Literal;
8982
8983 ----------------------
8984 -- Has_Private_With --
8985 ----------------------
8986
8987 function Has_Private_With (E : Entity_Id) return Boolean is
8988 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
8989 Item : Node_Id;
8990
8991 begin
8992 Item := First (Context_Items (Comp_Unit));
8993 while Present (Item) loop
8994 if Nkind (Item) = N_With_Clause
8995 and then Private_Present (Item)
8996 and then Is_Entity_Name (Name (Item))
8997 and then Entity (Name (Item)) = E
8998 then
8999 return True;
9000 end if;
9001
9002 Next (Item);
9003 end loop;
9004
9005 return False;
9006 end Has_Private_With;
9007
9008 ---------------------------
9009 -- Has_Implicit_Operator --
9010 ---------------------------
9011
9012 function Has_Implicit_Operator (N : Node_Id) return Boolean is
9013 Op_Id : constant Name_Id := Chars (Selector_Name (N));
9014 P : constant Entity_Id := Entity (Prefix (N));
9015 Id : Entity_Id;
9016 Priv_Id : Entity_Id := Empty;
9017
9018 procedure Add_Implicit_Operator
9019 (T : Entity_Id;
9020 Op_Type : Entity_Id := Empty);
9021 -- Add implicit interpretation to node N, using the type for which a
9022 -- predefined operator exists. If the operator yields a boolean type,
9023 -- the Operand_Type is implicitly referenced by the operator, and a
9024 -- reference to it must be generated.
9025
9026 ---------------------------
9027 -- Add_Implicit_Operator --
9028 ---------------------------
9029
9030 procedure Add_Implicit_Operator
9031 (T : Entity_Id;
9032 Op_Type : Entity_Id := Empty)
9033 is
9034 Predef_Op : Entity_Id;
9035
9036 begin
9037 Predef_Op := Current_Entity (Selector_Name (N));
9038 while Present (Predef_Op)
9039 and then Scope (Predef_Op) /= Standard_Standard
9040 loop
9041 Predef_Op := Homonym (Predef_Op);
9042 end loop;
9043
9044 if Nkind (N) = N_Selected_Component then
9045 Change_Selected_Component_To_Expanded_Name (N);
9046 end if;
9047
9048 -- If the context is an unanalyzed function call, determine whether
9049 -- a binary or unary interpretation is required.
9050
9051 if Nkind (Parent (N)) = N_Indexed_Component then
9052 declare
9053 Is_Binary_Call : constant Boolean :=
9054 Present
9055 (Next (First (Expressions (Parent (N)))));
9056 Is_Binary_Op : constant Boolean :=
9057 First_Entity
9058 (Predef_Op) /= Last_Entity (Predef_Op);
9059 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
9060
9061 begin
9062 if Is_Binary_Call then
9063 if Is_Binary_Op then
9064 Add_One_Interp (N, Predef_Op, T);
9065 else
9066 Add_One_Interp (N, Predef_Op2, T);
9067 end if;
9068 else
9069 if not Is_Binary_Op then
9070 Add_One_Interp (N, Predef_Op, T);
9071
9072 -- Predef_Op2 may be empty in case of previous errors
9073
9074 elsif Present (Predef_Op2) then
9075 Add_One_Interp (N, Predef_Op2, T);
9076 end if;
9077 end if;
9078 end;
9079
9080 else
9081 Add_One_Interp (N, Predef_Op, T);
9082
9083 -- For operators with unary and binary interpretations, if
9084 -- context is not a call, add both
9085
9086 if Present (Homonym (Predef_Op)) then
9087 Add_One_Interp (N, Homonym (Predef_Op), T);
9088 end if;
9089 end if;
9090
9091 -- The node is a reference to a predefined operator, and
9092 -- an implicit reference to the type of its operands.
9093
9094 if Present (Op_Type) then
9095 Generate_Operator_Reference (N, Op_Type);
9096 else
9097 Generate_Operator_Reference (N, T);
9098 end if;
9099 end Add_Implicit_Operator;
9100
9101 -- Start of processing for Has_Implicit_Operator
9102
9103 begin
9104 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
9105 Priv_Id := First_Private_Entity (P);
9106 end if;
9107
9108 Id := First_Entity (P);
9109
9110 case Op_Id is
9111
9112 -- Boolean operators: an implicit declaration exists if the scope
9113 -- contains a declaration for a derived Boolean type, or for an
9114 -- array of Boolean type.
9115
9116 when Name_Op_And
9117 | Name_Op_Not
9118 | Name_Op_Or
9119 | Name_Op_Xor
9120 =>
9121 while Id /= Priv_Id loop
9122 if Is_Type (Id)
9123 and then Valid_Boolean_Arg (Id)
9124 and then Is_Base_Type (Id)
9125 then
9126 Add_Implicit_Operator (Id);
9127 return True;
9128 end if;
9129
9130 Next_Entity (Id);
9131 end loop;
9132
9133 -- Equality: look for any non-limited type (result is Boolean)
9134
9135 when Name_Op_Eq
9136 | Name_Op_Ne
9137 =>
9138 while Id /= Priv_Id loop
9139 if Is_Type (Id)
9140 and then Valid_Equality_Arg (Id)
9141 and then Is_Base_Type (Id)
9142 then
9143 Add_Implicit_Operator (Standard_Boolean, Id);
9144 return True;
9145 end if;
9146
9147 Next_Entity (Id);
9148 end loop;
9149
9150 -- Comparison operators: scalar type, or array of scalar
9151
9152 when Name_Op_Ge
9153 | Name_Op_Gt
9154 | Name_Op_Le
9155 | Name_Op_Lt
9156 =>
9157 while Id /= Priv_Id loop
9158 if Is_Type (Id)
9159 and then Valid_Comparison_Arg (Id)
9160 and then Is_Base_Type (Id)
9161 then
9162 Add_Implicit_Operator (Standard_Boolean, Id);
9163 return True;
9164 end if;
9165
9166 Next_Entity (Id);
9167 end loop;
9168
9169 -- Arithmetic operators: any numeric type
9170
9171 when Name_Op_Abs
9172 | Name_Op_Add
9173 | Name_Op_Divide
9174 | Name_Op_Expon
9175 | Name_Op_Mod
9176 | Name_Op_Multiply
9177 | Name_Op_Rem
9178 | Name_Op_Subtract
9179 =>
9180 while Id /= Priv_Id loop
9181 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
9182 Add_Implicit_Operator (Id);
9183 return True;
9184 end if;
9185
9186 Next_Entity (Id);
9187 end loop;
9188
9189 -- Concatenation: any one-dimensional array type
9190
9191 when Name_Op_Concat =>
9192 while Id /= Priv_Id loop
9193 if Is_Array_Type (Id)
9194 and then Number_Dimensions (Id) = 1
9195 and then Is_Base_Type (Id)
9196 then
9197 Add_Implicit_Operator (Id);
9198 return True;
9199 end if;
9200
9201 Next_Entity (Id);
9202 end loop;
9203
9204 -- What is the others condition here? Should we be using a
9205 -- subtype of Name_Id that would restrict to operators ???
9206
9207 when others =>
9208 null;
9209 end case;
9210
9211 -- If we fall through, then we do not have an implicit operator
9212
9213 return False;
9214 end Has_Implicit_Operator;
9215
9216 -----------------------------------
9217 -- Has_Loop_In_Inner_Open_Scopes --
9218 -----------------------------------
9219
9220 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
9221 begin
9222 -- Several scope stacks are maintained by Scope_Stack. The base of the
9223 -- currently active scope stack is denoted by the Is_Active_Stack_Base
9224 -- flag in the scope stack entry. Note that the scope stacks used to
9225 -- simply be delimited implicitly by the presence of Standard_Standard
9226 -- at their base, but there now are cases where this is not sufficient
9227 -- because Standard_Standard actually may appear in the middle of the
9228 -- active set of scopes.
9229
9230 for J in reverse 0 .. Scope_Stack.Last loop
9231
9232 -- S was reached without seing a loop scope first
9233
9234 if Scope_Stack.Table (J).Entity = S then
9235 return False;
9236
9237 -- S was not yet reached, so it contains at least one inner loop
9238
9239 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
9240 return True;
9241 end if;
9242
9243 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
9244 -- cases where Standard_Standard appears in the middle of the active
9245 -- set of scopes. This affects the declaration and overriding of
9246 -- private inherited operations in instantiations of generic child
9247 -- units.
9248
9249 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
9250 end loop;
9251
9252 raise Program_Error; -- unreachable
9253 end Has_Loop_In_Inner_Open_Scopes;
9254
9255 --------------------
9256 -- In_Open_Scopes --
9257 --------------------
9258
9259 function In_Open_Scopes (S : Entity_Id) return Boolean is
9260 begin
9261 -- Several scope stacks are maintained by Scope_Stack. The base of the
9262 -- currently active scope stack is denoted by the Is_Active_Stack_Base
9263 -- flag in the scope stack entry. Note that the scope stacks used to
9264 -- simply be delimited implicitly by the presence of Standard_Standard
9265 -- at their base, but there now are cases where this is not sufficient
9266 -- because Standard_Standard actually may appear in the middle of the
9267 -- active set of scopes.
9268
9269 for J in reverse 0 .. Scope_Stack.Last loop
9270 if Scope_Stack.Table (J).Entity = S then
9271 return True;
9272 end if;
9273
9274 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
9275 -- cases where Standard_Standard appears in the middle of the active
9276 -- set of scopes. This affects the declaration and overriding of
9277 -- private inherited operations in instantiations of generic child
9278 -- units.
9279
9280 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
9281 end loop;
9282
9283 return False;
9284 end In_Open_Scopes;
9285
9286 -----------------------------
9287 -- Inherit_Renamed_Profile --
9288 -----------------------------
9289
9290 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
9291 New_F : Entity_Id;
9292 Old_F : Entity_Id;
9293 Old_T : Entity_Id;
9294 New_T : Entity_Id;
9295
9296 begin
9297 if Ekind (Old_S) = E_Operator then
9298 New_F := First_Formal (New_S);
9299
9300 while Present (New_F) loop
9301 Set_Etype (New_F, Base_Type (Etype (New_F)));
9302 Next_Formal (New_F);
9303 end loop;
9304
9305 Set_Etype (New_S, Base_Type (Etype (New_S)));
9306
9307 else
9308 New_F := First_Formal (New_S);
9309 Old_F := First_Formal (Old_S);
9310
9311 while Present (New_F) loop
9312 New_T := Etype (New_F);
9313 Old_T := Etype (Old_F);
9314
9315 -- If the new type is a renaming of the old one, as is the case
9316 -- for actuals in instances, retain its name, to simplify later
9317 -- disambiguation. Beware of Natural and Positive, see Cstand.
9318
9319 if Nkind (Parent (New_T)) = N_Subtype_Declaration
9320 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
9321 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
9322 and then Scope (New_T) /= Standard_Standard
9323 then
9324 null;
9325 else
9326 Set_Etype (New_F, Old_T);
9327 end if;
9328
9329 Next_Formal (New_F);
9330 Next_Formal (Old_F);
9331 end loop;
9332
9333 pragma Assert (No (Old_F));
9334
9335 if Ekind (Old_S) in E_Function | E_Enumeration_Literal then
9336 Set_Etype (New_S, Etype (Old_S));
9337 end if;
9338 end if;
9339 end Inherit_Renamed_Profile;
9340
9341 ----------------
9342 -- Initialize --
9343 ----------------
9344
9345 procedure Initialize is
9346 begin
9347 Urefs.Init;
9348 end Initialize;
9349
9350 -------------------------
9351 -- Install_Use_Clauses --
9352 -------------------------
9353
9354 procedure Install_Use_Clauses
9355 (Clause : Node_Id;
9356 Force_Installation : Boolean := False)
9357 is
9358 U : Node_Id;
9359
9360 begin
9361 U := Clause;
9362 while Present (U) loop
9363
9364 -- Case of USE package
9365
9366 if Nkind (U) = N_Use_Package_Clause then
9367 Use_One_Package (U, Name (U), True);
9368
9369 -- Case of USE TYPE
9370
9371 else
9372 Use_One_Type (Subtype_Mark (U), Force => Force_Installation);
9373
9374 end if;
9375
9376 Next_Use_Clause (U);
9377 end loop;
9378 end Install_Use_Clauses;
9379
9380 ----------------------
9381 -- Mark_Use_Clauses --
9382 ----------------------
9383
9384 procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is
9385 procedure Mark_Parameters (Call : Entity_Id);
9386 -- Perform use_type_clause marking for all parameters in a subprogram
9387 -- or operator call.
9388
9389 procedure Mark_Use_Package (Pak : Entity_Id);
9390 -- Move up the Prev_Use_Clause chain for packages denoted by Pak -
9391 -- marking each clause in the chain as effective in the process.
9392
9393 procedure Mark_Use_Type (E : Entity_Id);
9394 -- Similar to Do_Use_Package_Marking except we move up the
9395 -- Prev_Use_Clause chain for the type denoted by E.
9396
9397 ---------------------
9398 -- Mark_Parameters --
9399 ---------------------
9400
9401 procedure Mark_Parameters (Call : Entity_Id) is
9402 Curr : Node_Id;
9403
9404 begin
9405 -- Move through all of the formals
9406
9407 Curr := First_Formal (Call);
9408 while Present (Curr) loop
9409 Mark_Use_Type (Curr);
9410
9411 Next_Formal (Curr);
9412 end loop;
9413
9414 -- Handle the return type
9415
9416 Mark_Use_Type (Call);
9417 end Mark_Parameters;
9418
9419 ----------------------
9420 -- Mark_Use_Package --
9421 ----------------------
9422
9423 procedure Mark_Use_Package (Pak : Entity_Id) is
9424 Curr : Node_Id;
9425
9426 begin
9427 -- Ignore cases where the scope of the type is not a package (e.g.
9428 -- Standard_Standard).
9429
9430 if Ekind (Pak) /= E_Package then
9431 return;
9432 end if;
9433
9434 Curr := Current_Use_Clause (Pak);
9435 while Present (Curr)
9436 and then not Is_Effective_Use_Clause (Curr)
9437 loop
9438 -- We need to mark the previous use clauses as effective, but
9439 -- each use clause may in turn render other use_package_clauses
9440 -- effective. Additionally, it is possible to have a parent
9441 -- package renamed as a child of itself so we must check the
9442 -- prefix entity is not the same as the package we are marking.
9443
9444 if Nkind (Name (Curr)) /= N_Identifier
9445 and then Present (Prefix (Name (Curr)))
9446 and then Entity (Prefix (Name (Curr))) /= Pak
9447 then
9448 Mark_Use_Package (Entity (Prefix (Name (Curr))));
9449
9450 -- It is also possible to have a child package without a prefix
9451 -- that relies on a previous use_package_clause.
9452
9453 elsif Nkind (Name (Curr)) = N_Identifier
9454 and then Is_Child_Unit (Entity (Name (Curr)))
9455 then
9456 Mark_Use_Package (Scope (Entity (Name (Curr))));
9457 end if;
9458
9459 -- Mark the use_package_clause as effective and move up the chain
9460
9461 Set_Is_Effective_Use_Clause (Curr);
9462
9463 Curr := Prev_Use_Clause (Curr);
9464 end loop;
9465 end Mark_Use_Package;
9466
9467 -------------------
9468 -- Mark_Use_Type --
9469 -------------------
9470
9471 procedure Mark_Use_Type (E : Entity_Id) is
9472 Curr : Node_Id;
9473 Base : Entity_Id;
9474
9475 begin
9476 -- Ignore void types and unresolved string literals and primitives
9477
9478 if Nkind (E) = N_String_Literal
9479 or else Nkind (Etype (E)) not in N_Entity
9480 or else not Is_Type (Etype (E))
9481 then
9482 return;
9483 end if;
9484
9485 -- Primitives with class-wide operands might additionally render
9486 -- their base type's use_clauses effective - so do a recursive check
9487 -- here.
9488
9489 Base := Base_Type (Etype (E));
9490
9491 if Ekind (Base) = E_Class_Wide_Type then
9492 Mark_Use_Type (Base);
9493 end if;
9494
9495 -- The package containing the type or operator function being used
9496 -- may be in use as well, so mark any use_package_clauses for it as
9497 -- effective. There are also additional sanity checks performed here
9498 -- for ignoring previous errors.
9499
9500 Mark_Use_Package (Scope (Base));
9501
9502 if Nkind (E) in N_Op
9503 and then Present (Entity (E))
9504 and then Present (Scope (Entity (E)))
9505 then
9506 Mark_Use_Package (Scope (Entity (E)));
9507 end if;
9508
9509 Curr := Current_Use_Clause (Base);
9510 while Present (Curr)
9511 and then not Is_Effective_Use_Clause (Curr)
9512 loop
9513 -- Current use_type_clause may render other use_package_clauses
9514 -- effective.
9515
9516 if Nkind (Subtype_Mark (Curr)) /= N_Identifier
9517 and then Present (Prefix (Subtype_Mark (Curr)))
9518 then
9519 Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr))));
9520 end if;
9521
9522 -- Mark the use_type_clause as effective and move up the chain
9523
9524 Set_Is_Effective_Use_Clause (Curr);
9525
9526 Curr := Prev_Use_Clause (Curr);
9527 end loop;
9528 end Mark_Use_Type;
9529
9530 -- Start of processing for Mark_Use_Clauses
9531
9532 begin
9533 -- Use clauses in and of themselves do not count as a "use" of a
9534 -- package.
9535
9536 if Nkind (Parent (Id)) in N_Use_Package_Clause | N_Use_Type_Clause then
9537 return;
9538 end if;
9539
9540 -- Handle entities
9541
9542 if Nkind (Id) in N_Entity then
9543
9544 -- Mark the entity's package
9545
9546 if Is_Potentially_Use_Visible (Id) then
9547 Mark_Use_Package (Scope (Id));
9548 end if;
9549
9550 -- Mark enumeration literals
9551
9552 if Ekind (Id) = E_Enumeration_Literal then
9553 Mark_Use_Type (Id);
9554
9555 -- Mark primitives
9556
9557 elsif (Is_Overloadable (Id)
9558 or else Is_Generic_Subprogram (Id))
9559 and then (Is_Potentially_Use_Visible (Id)
9560 or else Is_Intrinsic_Subprogram (Id)
9561 or else (Ekind (Id) in E_Function | E_Procedure
9562 and then Is_Generic_Actual_Subprogram (Id)))
9563 then
9564 Mark_Parameters (Id);
9565 end if;
9566
9567 -- Handle nodes
9568
9569 else
9570 -- Mark operators
9571
9572 if Nkind (Id) in N_Op then
9573
9574 -- At this point the left operand may not be resolved if we are
9575 -- encountering multiple operators next to eachother in an
9576 -- expression.
9577
9578 if Nkind (Id) in N_Binary_Op
9579 and then not (Nkind (Left_Opnd (Id)) in N_Op)
9580 then
9581 Mark_Use_Type (Left_Opnd (Id));
9582 end if;
9583
9584 Mark_Use_Type (Right_Opnd (Id));
9585 Mark_Use_Type (Id);
9586
9587 -- Mark entity identifiers
9588
9589 elsif Nkind (Id) in N_Has_Entity
9590 and then (Is_Potentially_Use_Visible (Entity (Id))
9591 or else (Is_Generic_Instance (Entity (Id))
9592 and then Is_Immediately_Visible (Entity (Id))))
9593 then
9594 -- Ignore fully qualified names as they do not count as a "use" of
9595 -- a package.
9596
9597 if Nkind (Id) in N_Identifier | N_Operator_Symbol
9598 or else (Present (Prefix (Id))
9599 and then Scope (Entity (Id)) /= Entity (Prefix (Id)))
9600 then
9601 Mark_Use_Clauses (Entity (Id));
9602 end if;
9603 end if;
9604 end if;
9605 end Mark_Use_Clauses;
9606
9607 --------------------------------
9608 -- Most_Descendant_Use_Clause --
9609 --------------------------------
9610
9611 function Most_Descendant_Use_Clause
9612 (Clause1 : Entity_Id;
9613 Clause2 : Entity_Id) return Entity_Id
9614 is
9615 function Determine_Package_Scope (Clause : Node_Id) return Entity_Id;
9616 -- Given a use clause, determine which package it belongs to
9617
9618 -----------------------------
9619 -- Determine_Package_Scope --
9620 -----------------------------
9621
9622 function Determine_Package_Scope (Clause : Node_Id) return Entity_Id is
9623 begin
9624 -- Check if the clause appears in the context area
9625
9626 -- Note we cannot employ Enclosing_Packge for use clauses within
9627 -- context clauses since they are not actually "enclosed."
9628
9629 if Nkind (Parent (Clause)) = N_Compilation_Unit then
9630 return Entity_Of_Unit (Unit (Parent (Clause)));
9631 end if;
9632
9633 -- Otherwise, obtain the enclosing package normally
9634
9635 return Enclosing_Package (Clause);
9636 end Determine_Package_Scope;
9637
9638 Scope1 : Entity_Id;
9639 Scope2 : Entity_Id;
9640
9641 -- Start of processing for Most_Descendant_Use_Clause
9642
9643 begin
9644 if Clause1 = Clause2 then
9645 return Clause1;
9646 end if;
9647
9648 -- We determine which one is the most descendant by the scope distance
9649 -- to the ultimate parent unit.
9650
9651 Scope1 := Determine_Package_Scope (Clause1);
9652 Scope2 := Determine_Package_Scope (Clause2);
9653 while Scope1 /= Standard_Standard
9654 and then Scope2 /= Standard_Standard
9655 loop
9656 Scope1 := Scope (Scope1);
9657 Scope2 := Scope (Scope2);
9658
9659 if No (Scope1) then
9660 return Clause1;
9661 elsif No (Scope2) then
9662 return Clause2;
9663 end if;
9664 end loop;
9665
9666 if Scope1 = Standard_Standard then
9667 return Clause1;
9668 end if;
9669
9670 return Clause2;
9671 end Most_Descendant_Use_Clause;
9672
9673 ---------------
9674 -- Pop_Scope --
9675 ---------------
9676
9677 procedure Pop_Scope is
9678 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
9679 S : constant Scope_Kind_Id := SST.Entity;
9680
9681 begin
9682 if Debug_Flag_E then
9683 Write_Info;
9684 end if;
9685
9686 -- Set Default_Storage_Pool field of the library unit if necessary
9687
9688 if Is_Package_Or_Generic_Package (S)
9689 and then
9690 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
9691 then
9692 declare
9693 Aux : constant Node_Id :=
9694 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
9695 begin
9696 if No (Default_Storage_Pool (Aux)) then
9697 Set_Default_Storage_Pool (Aux, Default_Pool);
9698 end if;
9699 end;
9700 end if;
9701
9702 Scope_Suppress := SST.Save_Scope_Suppress;
9703 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
9704 Check_Policy_List := SST.Save_Check_Policy_List;
9705 Default_Pool := SST.Save_Default_Storage_Pool;
9706 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
9707 SPARK_Mode := SST.Save_SPARK_Mode;
9708 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
9709 Default_SSO := SST.Save_Default_SSO;
9710 Uneval_Old := SST.Save_Uneval_Old;
9711
9712 if Debug_Flag_W then
9713 Write_Str ("<-- exiting scope: ");
9714 Write_Name (Chars (Current_Scope));
9715 Write_Str (", Depth=");
9716 Write_Int (Int (Scope_Stack.Last));
9717 Write_Eol;
9718 end if;
9719
9720 End_Use_Clauses (SST.First_Use_Clause);
9721
9722 -- If the actions to be wrapped are still there they will get lost
9723 -- causing incomplete code to be generated. It is better to abort in
9724 -- this case (and we do the abort even with assertions off since the
9725 -- penalty is incorrect code generation).
9726
9727 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
9728 raise Program_Error;
9729 end if;
9730
9731 -- Free last subprogram name if allocated, and pop scope
9732
9733 Free (SST.Last_Subprogram_Name);
9734 Scope_Stack.Decrement_Last;
9735 end Pop_Scope;
9736
9737 ----------------
9738 -- Push_Scope --
9739 ----------------
9740
9741 procedure Push_Scope (S : Scope_Kind_Id) is
9742 E : constant Entity_Id := Scope (S);
9743
9744 function Component_Alignment_Default return Component_Alignment_Kind;
9745 -- Return Component_Alignment_Kind for the newly-pushed scope.
9746
9747 function Component_Alignment_Default return Component_Alignment_Kind is
9748 begin
9749 -- Each new scope pushed onto the scope stack inherits the component
9750 -- alignment of the previous scope. This emulates the "visibility"
9751 -- semantics of pragma Component_Alignment.
9752
9753 if Scope_Stack.Last > Scope_Stack.First then
9754 return Scope_Stack.Table
9755 (Scope_Stack.Last - 1).Component_Alignment_Default;
9756
9757 -- Otherwise, this is the first scope being pushed on the scope
9758 -- stack. Inherit the component alignment from the configuration
9759 -- form of pragma Component_Alignment (if any).
9760
9761 else
9762 return Configuration_Component_Alignment;
9763 end if;
9764 end Component_Alignment_Default;
9765
9766 begin
9767 if Ekind (S) = E_Void then
9768 null;
9769
9770 -- Set scope depth if not a nonconcurrent type, and we have not yet set
9771 -- the scope depth. This means that we have the first occurrence of the
9772 -- scope, and this is where the depth is set.
9773
9774 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
9775 and then not Scope_Depth_Set (S)
9776 then
9777 if S = Standard_Standard then
9778 Set_Scope_Depth_Value (S, Uint_0);
9779
9780 elsif Is_Child_Unit (S) then
9781 Set_Scope_Depth_Value (S, Uint_1);
9782
9783 elsif not Is_Record_Type (Current_Scope) then
9784 if Scope_Depth_Set (Current_Scope) then
9785 if Ekind (S) = E_Loop then
9786 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
9787 else
9788 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
9789 end if;
9790 end if;
9791 end if;
9792 end if;
9793
9794 Scope_Stack.Increment_Last;
9795
9796 Scope_Stack.Table (Scope_Stack.Last) :=
9797 (Entity => S,
9798 Save_Scope_Suppress => Scope_Suppress,
9799 Save_Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
9800 Save_Check_Policy_List => Check_Policy_List,
9801 Save_Default_Storage_Pool => Default_Pool,
9802 Save_No_Tagged_Streams => No_Tagged_Streams,
9803 Save_SPARK_Mode => SPARK_Mode,
9804 Save_SPARK_Mode_Pragma => SPARK_Mode_Pragma,
9805 Save_Default_SSO => Default_SSO,
9806 Save_Uneval_Old => Uneval_Old,
9807 Component_Alignment_Default => Component_Alignment_Default,
9808 Last_Subprogram_Name => null,
9809 Is_Transient => False,
9810 Node_To_Be_Wrapped => Empty,
9811 Pending_Freeze_Actions => No_List,
9812 Actions_To_Be_Wrapped => (others => No_List),
9813 First_Use_Clause => Empty,
9814 Is_Active_Stack_Base => False,
9815 Previous_Visibility => False,
9816 Locked_Shared_Objects => No_Elist);
9817
9818 if Debug_Flag_W then
9819 Write_Str ("--> new scope: ");
9820 Write_Name (Chars (Current_Scope));
9821 Write_Str (", Id=");
9822 Write_Int (Int (Current_Scope));
9823 Write_Str (", Depth=");
9824 Write_Int (Int (Scope_Stack.Last));
9825 Write_Eol;
9826 end if;
9827
9828 -- Deal with copying flags from the previous scope to this one. This is
9829 -- not necessary if either scope is standard, or if the new scope is a
9830 -- child unit.
9831
9832 if S /= Standard_Standard
9833 and then Scope (S) /= Standard_Standard
9834 and then not Is_Child_Unit (S)
9835 then
9836 if Nkind (E) not in N_Entity then
9837 return;
9838 end if;
9839
9840 -- Copy categorization flags from Scope (S) to S, this is not done
9841 -- when Scope (S) is Standard_Standard since propagation is from
9842 -- library unit entity inwards. Copy other relevant attributes as
9843 -- well (Discard_Names in particular).
9844
9845 -- We only propagate inwards for library level entities,
9846 -- inner level subprograms do not inherit the categorization.
9847
9848 if Is_Library_Level_Entity (S) then
9849 Set_Is_Preelaborated (S, Is_Preelaborated (E));
9850 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
9851 Set_Discard_Names (S, Discard_Names (E));
9852 Set_Suppress_Value_Tracking_On_Call
9853 (S, Suppress_Value_Tracking_On_Call (E));
9854 Set_Categorization_From_Scope (E => S, Scop => E);
9855 end if;
9856 end if;
9857
9858 if Is_Child_Unit (S)
9859 and then Present (E)
9860 and then Is_Package_Or_Generic_Package (E)
9861 and then
9862 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9863 then
9864 declare
9865 Aux : constant Node_Id :=
9866 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
9867 begin
9868 if Present (Default_Storage_Pool (Aux)) then
9869 Default_Pool := Default_Storage_Pool (Aux);
9870 end if;
9871 end;
9872 end if;
9873 end Push_Scope;
9874
9875 ---------------------
9876 -- Premature_Usage --
9877 ---------------------
9878
9879 procedure Premature_Usage (N : Node_Id) is
9880 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
9881 E : Entity_Id := Entity (N);
9882
9883 begin
9884 -- Within an instance, the analysis of the actual for a formal object
9885 -- does not see the name of the object itself. This is significant only
9886 -- if the object is an aggregate, where its analysis does not do any
9887 -- name resolution on component associations. (see 4717-008). In such a
9888 -- case, look for the visible homonym on the chain.
9889
9890 if In_Instance and then Present (Homonym (E)) then
9891 E := Homonym (E);
9892 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
9893 E := Homonym (E);
9894 end loop;
9895
9896 if Present (E) then
9897 Set_Entity (N, E);
9898 Set_Etype (N, Etype (E));
9899 return;
9900 end if;
9901 end if;
9902
9903 case Kind is
9904 when N_Component_Declaration =>
9905 Error_Msg_N
9906 ("component&! cannot be used before end of record declaration",
9907 N);
9908
9909 when N_Parameter_Specification =>
9910 Error_Msg_N
9911 ("formal parameter&! cannot be used before end of specification",
9912 N);
9913
9914 when N_Discriminant_Specification =>
9915 Error_Msg_N
9916 ("discriminant&! cannot be used before end of discriminant part",
9917 N);
9918
9919 when N_Procedure_Specification | N_Function_Specification =>
9920 Error_Msg_N
9921 ("subprogram&! cannot be used before end of its declaration",
9922 N);
9923
9924 when N_Full_Type_Declaration | N_Subtype_Declaration =>
9925 Error_Msg_N
9926 ("type& cannot be used before end of its declaration!", N);
9927
9928 when others =>
9929 Error_Msg_N
9930 ("object& cannot be used before end of its declaration!", N);
9931
9932 -- If the premature reference appears as the expression in its own
9933 -- declaration, rewrite it to prevent compiler loops in subsequent
9934 -- uses of this mangled declaration in address clauses.
9935
9936 if Nkind (Parent (N)) = N_Object_Declaration then
9937 Set_Entity (N, Any_Id);
9938 end if;
9939 end case;
9940 end Premature_Usage;
9941
9942 ------------------------
9943 -- Present_System_Aux --
9944 ------------------------
9945
9946 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
9947 Loc : Source_Ptr;
9948 Aux_Name : Unit_Name_Type;
9949 Unum : Unit_Number_Type;
9950 Withn : Node_Id;
9951 With_Sys : Node_Id;
9952 The_Unit : Node_Id;
9953
9954 function Find_System (C_Unit : Node_Id) return Entity_Id;
9955 -- Scan context clause of compilation unit to find with_clause
9956 -- for System.
9957
9958 -----------------
9959 -- Find_System --
9960 -----------------
9961
9962 function Find_System (C_Unit : Node_Id) return Entity_Id is
9963 With_Clause : Node_Id;
9964
9965 begin
9966 With_Clause := First (Context_Items (C_Unit));
9967 while Present (With_Clause) loop
9968 if (Nkind (With_Clause) = N_With_Clause
9969 and then Chars (Name (With_Clause)) = Name_System)
9970 and then Comes_From_Source (With_Clause)
9971 then
9972 return With_Clause;
9973 end if;
9974
9975 Next (With_Clause);
9976 end loop;
9977
9978 return Empty;
9979 end Find_System;
9980
9981 -- Start of processing for Present_System_Aux
9982
9983 begin
9984 -- The child unit may have been loaded and analyzed already
9985
9986 if Present (System_Aux_Id) then
9987 return True;
9988
9989 -- If no previous pragma for System.Aux, nothing to load
9990
9991 elsif No (System_Extend_Unit) then
9992 return False;
9993
9994 -- Use the unit name given in the pragma to retrieve the unit.
9995 -- Verify that System itself appears in the context clause of the
9996 -- current compilation. If System is not present, an error will
9997 -- have been reported already.
9998
9999 else
10000 With_Sys := Find_System (Cunit (Current_Sem_Unit));
10001
10002 The_Unit := Unit (Cunit (Current_Sem_Unit));
10003
10004 if No (With_Sys)
10005 and then
10006 (Nkind (The_Unit) = N_Package_Body
10007 or else (Nkind (The_Unit) = N_Subprogram_Body
10008 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
10009 then
10010 With_Sys :=
10011 Find_System (Spec_Or_Body_Lib_Unit (Cunit (Current_Sem_Unit)));
10012 end if;
10013
10014 if No (With_Sys) and then Present (N) then
10015
10016 -- If we are compiling a subunit, we need to examine its
10017 -- context as well (Current_Sem_Unit is the parent unit);
10018
10019 The_Unit := Parent (N);
10020 while Nkind (The_Unit) /= N_Compilation_Unit loop
10021 The_Unit := Parent (The_Unit);
10022 end loop;
10023
10024 if Nkind (Unit (The_Unit)) = N_Subunit then
10025 With_Sys := Find_System (The_Unit);
10026 end if;
10027 end if;
10028
10029 if No (With_Sys) then
10030 return False;
10031 end if;
10032
10033 Loc := Sloc (With_Sys);
10034 Get_Name_String (Chars (Expression (System_Extend_Unit)));
10035 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
10036 Name_Buffer (1 .. 7) := "system.";
10037 Name_Buffer (Name_Len + 8) := '%';
10038 Name_Buffer (Name_Len + 9) := 's';
10039 Name_Len := Name_Len + 9;
10040 Aux_Name := Name_Find;
10041
10042 Unum :=
10043 Load_Unit
10044 (Load_Name => Aux_Name,
10045 Required => False,
10046 Subunit => False,
10047 Error_Node => With_Sys);
10048
10049 if Unum /= No_Unit then
10050 Semantics (Cunit (Unum));
10051 System_Aux_Id :=
10052 Defining_Entity (Specification (Unit (Cunit (Unum))));
10053
10054 Withn :=
10055 Make_With_Clause (Loc,
10056 Name =>
10057 Make_Expanded_Name (Loc,
10058 Chars => Chars (System_Aux_Id),
10059 Prefix =>
10060 New_Occurrence_Of (Scope (System_Aux_Id), Loc),
10061 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
10062
10063 Set_Entity (Name (Withn), System_Aux_Id);
10064
10065 Set_Corresponding_Spec (Withn, System_Aux_Id);
10066 Set_First_Name (Withn);
10067 Set_Is_Implicit_With (Withn);
10068 Set_Withed_Lib_Unit (Withn, Cunit (Unum));
10069
10070 Insert_After (With_Sys, Withn);
10071 Mark_Rewrite_Insertion (Withn);
10072 Set_Context_Installed (Withn);
10073
10074 return True;
10075
10076 -- Here if unit load failed
10077
10078 else
10079 Error_Msg_Name_1 := Name_System;
10080 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
10081 Error_Msg_N
10082 ("extension package `%.%` does not exist",
10083 Opt.System_Extend_Unit);
10084 return False;
10085 end if;
10086 end if;
10087 end Present_System_Aux;
10088
10089 -------------------------
10090 -- Restore_Scope_Stack --
10091 -------------------------
10092
10093 procedure Restore_Scope_Stack
10094 (List : Elist_Id;
10095 Handle_Use : Boolean := True)
10096 is
10097 SS_Last : constant Int := Scope_Stack.Last;
10098 Elmt : Elmt_Id;
10099
10100 begin
10101 -- Restore visibility of previous scope stack, if any, using the list
10102 -- we saved (we use Remove, since this list will not be used again).
10103
10104 loop
10105 Elmt := First_Elmt (List);
10106 exit when Elmt = No_Elmt;
10107 Set_Is_Immediately_Visible (Node (Elmt));
10108 Remove_Elmt (List, Elmt);
10109 end loop;
10110
10111 -- Restore use clauses
10112
10113 if SS_Last >= Scope_Stack.First
10114 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
10115 and then Handle_Use
10116 then
10117 Install_Use_Clauses
10118 (Scope_Stack.Table (SS_Last).First_Use_Clause,
10119 Force_Installation => True);
10120 end if;
10121 end Restore_Scope_Stack;
10122
10123 ----------------------
10124 -- Save_Scope_Stack --
10125 ----------------------
10126
10127 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
10128 -- consuming any memory. That is, Save_Scope_Stack took care of removing
10129 -- from immediate visibility entities and Restore_Scope_Stack took care
10130 -- of restoring their visibility analyzing the context of each entity. The
10131 -- problem of such approach is that it was fragile and caused unexpected
10132 -- visibility problems, and indeed one test was found where there was a
10133 -- real problem.
10134
10135 -- Furthermore, the following experiment was carried out:
10136
10137 -- - Save_Scope_Stack was modified to store in an Elist1 all those
10138 -- entities whose attribute Is_Immediately_Visible is modified
10139 -- from True to False.
10140
10141 -- - Restore_Scope_Stack was modified to store in another Elist2
10142 -- all the entities whose attribute Is_Immediately_Visible is
10143 -- modified from False to True.
10144
10145 -- - Extra code was added to verify that all the elements of Elist1
10146 -- are found in Elist2
10147
10148 -- This test shows that there may be more occurrences of this problem which
10149 -- have not yet been detected. As a result, we replaced that approach by
10150 -- the current one in which Save_Scope_Stack returns the list of entities
10151 -- whose visibility is changed, and that list is passed to Restore_Scope_
10152 -- Stack to undo that change. This approach is simpler and safer, although
10153 -- it consumes more memory.
10154
10155 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
10156 Result : constant Elist_Id := New_Elmt_List;
10157 E : Entity_Id;
10158 S : Entity_Id;
10159 SS_Last : constant Int := Scope_Stack.Last;
10160
10161 procedure Remove_From_Visibility (E : Entity_Id);
10162 -- If E is immediately visible then append it to the result and remove
10163 -- it temporarily from visibility.
10164
10165 ----------------------------
10166 -- Remove_From_Visibility --
10167 ----------------------------
10168
10169 procedure Remove_From_Visibility (E : Entity_Id) is
10170 begin
10171 if Is_Immediately_Visible (E) then
10172 Append_Elmt (E, Result);
10173 Set_Is_Immediately_Visible (E, False);
10174 end if;
10175 end Remove_From_Visibility;
10176
10177 -- Start of processing for Save_Scope_Stack
10178
10179 begin
10180 if SS_Last >= Scope_Stack.First
10181 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
10182 then
10183 if Handle_Use then
10184 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
10185 end if;
10186
10187 -- If the call is from within a compilation unit, as when called from
10188 -- Rtsfind, make current entries in scope stack invisible while we
10189 -- analyze the new unit.
10190
10191 for J in reverse 0 .. SS_Last loop
10192 exit when Scope_Stack.Table (J).Entity = Standard_Standard
10193 or else No (Scope_Stack.Table (J).Entity);
10194
10195 S := Scope_Stack.Table (J).Entity;
10196
10197 Remove_From_Visibility (S);
10198
10199 E := First_Entity (S);
10200 while Present (E) loop
10201 Remove_From_Visibility (E);
10202 Next_Entity (E);
10203 end loop;
10204 end loop;
10205
10206 end if;
10207
10208 return Result;
10209 end Save_Scope_Stack;
10210
10211 -------------
10212 -- Set_Use --
10213 -------------
10214
10215 procedure Set_Use (L : List_Id) is
10216 Decl : Node_Id;
10217
10218 begin
10219 Decl := First (L);
10220 while Present (Decl) loop
10221 if Nkind (Decl) = N_Use_Package_Clause then
10222 Chain_Use_Clause (Decl);
10223 Use_One_Package (Decl, Name (Decl));
10224
10225 elsif Nkind (Decl) = N_Use_Type_Clause then
10226 Chain_Use_Clause (Decl);
10227 Use_One_Type (Subtype_Mark (Decl));
10228
10229 end if;
10230
10231 Next (Decl);
10232 end loop;
10233 end Set_Use;
10234
10235 -----------------------------
10236 -- Update_Use_Clause_Chain --
10237 -----------------------------
10238
10239 procedure Update_Use_Clause_Chain is
10240
10241 procedure Update_Chain_In_Scope (Level : Int);
10242 -- Iterate through one level in the scope stack verifying each use-type
10243 -- clause within said level is used then reset the Current_Use_Clause
10244 -- to a redundant use clause outside of the current ending scope if such
10245 -- a clause exists.
10246
10247 ---------------------------
10248 -- Update_Chain_In_Scope --
10249 ---------------------------
10250
10251 procedure Update_Chain_In_Scope (Level : Int) is
10252 Curr : Node_Id;
10253 N : Node_Id;
10254
10255 begin
10256 -- Loop through all use clauses within the scope dictated by Level
10257
10258 Curr := Scope_Stack.Table (Level).First_Use_Clause;
10259 while Present (Curr) loop
10260
10261 -- Retrieve the subtype mark or name within the current current
10262 -- use clause.
10263
10264 if Nkind (Curr) = N_Use_Type_Clause then
10265 N := Subtype_Mark (Curr);
10266 else
10267 N := Name (Curr);
10268 end if;
10269
10270 -- If warnings for unreferenced entities are enabled and the
10271 -- current use clause has not been marked effective.
10272
10273 if Check_Unreferenced
10274 and then Comes_From_Source (Curr)
10275 and then not Is_Effective_Use_Clause (Curr)
10276 and then not In_Instance
10277 and then not In_Inlined_Body
10278 then
10279 -- We are dealing with a potentially unused use_package_clause
10280
10281 if Nkind (Curr) = N_Use_Package_Clause then
10282
10283 -- Renamings and formal subprograms may cause the associated
10284 -- node to be marked as effective instead of the original.
10285
10286 if not (Present (Associated_Node (N))
10287 and then Present
10288 (Current_Use_Clause
10289 (Associated_Node (N)))
10290 and then Is_Effective_Use_Clause
10291 (Current_Use_Clause
10292 (Associated_Node (N))))
10293 then
10294 Error_Msg_Node_1 := Entity (N);
10295 Error_Msg_NE
10296 ("use clause for package & has no effect?u?",
10297 Curr, Entity (N));
10298 end if;
10299
10300 -- We are dealing with an unused use_type_clause
10301
10302 else
10303 Error_Msg_Node_1 := Etype (N);
10304 Error_Msg_NE
10305 ("use clause for } has no effect?u?", Curr, Etype (N));
10306 end if;
10307 end if;
10308
10309 -- Verify that we haven't already processed a redundant
10310 -- use_type_clause within the same scope before we move the
10311 -- current use clause up to a previous one for type T.
10312
10313 if Present (Prev_Use_Clause (Curr)) then
10314 Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr));
10315 end if;
10316
10317 Next_Use_Clause (Curr);
10318 end loop;
10319 end Update_Chain_In_Scope;
10320
10321 -- Start of processing for Update_Use_Clause_Chain
10322
10323 begin
10324 Update_Chain_In_Scope (Scope_Stack.Last);
10325
10326 -- Deal with use clauses within the context area if the current
10327 -- scope is a compilation unit.
10328
10329 if Is_Compilation_Unit (Current_Scope)
10330 and then Sloc (Scope_Stack.Table
10331 (Scope_Stack.Last - 1).Entity) = Standard_Location
10332 then
10333 Update_Chain_In_Scope (Scope_Stack.Last - 1);
10334 end if;
10335 end Update_Use_Clause_Chain;
10336
10337 ---------------------
10338 -- Use_One_Package --
10339 ---------------------
10340
10341 procedure Use_One_Package
10342 (N : Node_Id;
10343 Pack_Name : Entity_Id := Empty;
10344 Force : Boolean := False)
10345 is
10346 procedure Note_Redundant_Use (Clause : Node_Id);
10347 -- Mark the name in a use clause as redundant if the corresponding
10348 -- entity is already use-visible. Emit a warning if the use clause comes
10349 -- from source and the proper warnings are enabled.
10350
10351 ------------------------
10352 -- Note_Redundant_Use --
10353 ------------------------
10354
10355 procedure Note_Redundant_Use (Clause : Node_Id) is
10356 Decl : constant Node_Id := Parent (Clause);
10357 Pack_Name : constant Entity_Id := Entity (Clause);
10358
10359 Cur_Use : Node_Id := Current_Use_Clause (Pack_Name);
10360 Prev_Use : Node_Id := Empty;
10361 Redundant : Node_Id := Empty;
10362 -- The Use_Clause which is actually redundant. In the simplest case
10363 -- it is Pack itself, but when we compile a body we install its
10364 -- context before that of its spec, in which case it is the
10365 -- use_clause in the spec that will appear to be redundant, and we
10366 -- want the warning to be placed on the body. Similar complications
10367 -- appear when the redundancy is between a child unit and one of its
10368 -- ancestors.
10369
10370 begin
10371 -- Could be renamed...
10372
10373 if No (Cur_Use) then
10374 Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name));
10375 end if;
10376
10377 Set_Redundant_Use (Clause, True);
10378
10379 -- Do not check for redundant use if clause is generated, or in an
10380 -- instance, or in a predefined unit to avoid misleading warnings
10381 -- that may occur as part of a rtsfind load.
10382
10383 if not Comes_From_Source (Clause)
10384 or else In_Instance
10385 or else not Warn_On_Redundant_Constructs
10386 or else Is_Predefined_Unit (Current_Sem_Unit)
10387 then
10388 return;
10389 end if;
10390
10391 if not Is_Compilation_Unit (Current_Scope) then
10392
10393 -- If the use_clause is in an inner scope, it is made redundant by
10394 -- some clause in the current context, with one exception: If we
10395 -- are compiling a nested package body, and the use_clause comes
10396 -- from then corresponding spec, the clause is not necessarily
10397 -- fully redundant, so we should not warn. If a warning was
10398 -- warranted, it would have been given when the spec was
10399 -- processed.
10400
10401 if Nkind (Parent (Decl)) = N_Package_Specification then
10402 declare
10403 Package_Spec_Entity : constant Entity_Id :=
10404 Defining_Unit_Name (Parent (Decl));
10405 begin
10406 if In_Package_Body (Package_Spec_Entity) then
10407 return;
10408 end if;
10409 end;
10410 end if;
10411
10412 Redundant := Clause;
10413 Prev_Use := Cur_Use;
10414
10415 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
10416 declare
10417 Cur_Unit : constant Unit_Number_Type :=
10418 Get_Source_Unit (Cur_Use);
10419 New_Unit : constant Unit_Number_Type :=
10420 Get_Source_Unit (Clause);
10421
10422 Scop : Entity_Id;
10423
10424 begin
10425 if Cur_Unit = New_Unit then
10426
10427 -- Redundant clause in same body
10428
10429 Redundant := Clause;
10430 Prev_Use := Cur_Use;
10431
10432 elsif Cur_Unit = Current_Sem_Unit then
10433
10434 -- If the new clause is not in the current unit it has been
10435 -- analyzed first, and it makes the other one redundant.
10436 -- However, if the new clause appears in a subunit, Cur_Unit
10437 -- is still the parent, and in that case the redundant one
10438 -- is the one appearing in the subunit.
10439
10440 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
10441 Redundant := Clause;
10442 Prev_Use := Cur_Use;
10443
10444 -- Most common case: redundant clause in body, original
10445 -- clause in spec. Current scope is spec entity.
10446
10447 elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then
10448 Redundant := Cur_Use;
10449 Prev_Use := Clause;
10450
10451 else
10452 -- The new clause may appear in an unrelated unit, when
10453 -- the parents of a generic are being installed prior to
10454 -- instantiation. In this case there must be no warning.
10455 -- We detect this case by checking whether the current
10456 -- top of the stack is related to the current
10457 -- compilation.
10458
10459 Scop := Current_Scope;
10460 while Present (Scop)
10461 and then Scop /= Standard_Standard
10462 loop
10463 if Is_Compilation_Unit (Scop)
10464 and then not Is_Child_Unit (Scop)
10465 then
10466 return;
10467
10468 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
10469 exit;
10470 end if;
10471
10472 Scop := Scope (Scop);
10473 end loop;
10474
10475 Redundant := Cur_Use;
10476 Prev_Use := Clause;
10477 end if;
10478
10479 elsif New_Unit = Current_Sem_Unit then
10480 Redundant := Clause;
10481 Prev_Use := Cur_Use;
10482
10483 else
10484 -- Neither is the current unit, so they appear in parent or
10485 -- sibling units. Warning will be emitted elsewhere.
10486
10487 return;
10488 end if;
10489 end;
10490
10491 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
10492 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
10493 then
10494 -- Use_clause is in child unit of current unit, and the child unit
10495 -- appears in the context of the body of the parent, so it has
10496 -- been installed first, even though it is the redundant one.
10497 -- Depending on their placement in the context, the visible or the
10498 -- private parts of the two units, either might appear as
10499 -- redundant, but the message has to be on the current unit.
10500
10501 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
10502 Redundant := Cur_Use;
10503 Prev_Use := Clause;
10504 else
10505 Redundant := Clause;
10506 Prev_Use := Cur_Use;
10507 end if;
10508
10509 -- If the new use clause appears in the private part of a parent
10510 -- unit it may appear to be redundant w.r.t. a use clause in a
10511 -- child unit, but the previous use clause was needed in the
10512 -- visible part of the child, and no warning should be emitted.
10513
10514 if Nkind (Parent (Decl)) = N_Package_Specification
10515 and then List_Containing (Decl) =
10516 Private_Declarations (Parent (Decl))
10517 then
10518 declare
10519 Par : constant Entity_Id :=
10520 Defining_Entity (Parent (Decl));
10521 Spec : constant Node_Id :=
10522 Specification (Unit (Cunit (Current_Sem_Unit)));
10523 Cur_List : constant List_Id := List_Containing (Cur_Use);
10524
10525 begin
10526 if Is_Compilation_Unit (Par)
10527 and then Par /= Cunit_Entity (Current_Sem_Unit)
10528 then
10529 if Cur_List = Context_Items (Cunit (Current_Sem_Unit))
10530 or else Cur_List = Visible_Declarations (Spec)
10531 then
10532 return;
10533 end if;
10534 end if;
10535 end;
10536 end if;
10537
10538 -- Finally, if the current use clause is in the context then the
10539 -- clause is redundant when it is nested within the unit.
10540
10541 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
10542 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
10543 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
10544 then
10545 Redundant := Clause;
10546 Prev_Use := Cur_Use;
10547 end if;
10548
10549 if Present (Redundant) and then Parent (Redundant) /= Prev_Use then
10550
10551 -- Make sure we are looking at most-descendant use_package_clause
10552 -- by traversing the chain with Find_First_Use and then verifying
10553 -- there is no scope manipulation via Most_Descendant_Use_Clause.
10554
10555 if Nkind (Prev_Use) = N_Use_Package_Clause
10556 and then
10557 (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit
10558 or else Most_Descendant_Use_Clause
10559 (Prev_Use, Find_First_Use (Prev_Use)) /= Prev_Use)
10560 then
10561 Prev_Use := Find_First_Use (Prev_Use);
10562 end if;
10563
10564 Error_Msg_Sloc := Sloc (Prev_Use);
10565 Error_Msg_NE -- CODEFIX
10566 ("& is already use-visible through previous use_clause #?r?",
10567 Redundant, Pack_Name);
10568 end if;
10569 end Note_Redundant_Use;
10570
10571 -- Local variables
10572
10573 Current_Instance : Entity_Id := Empty;
10574 Id : Entity_Id;
10575 P : Entity_Id;
10576 Prev : Entity_Id;
10577 Private_With_OK : Boolean := False;
10578 Real_P : Entity_Id;
10579
10580 -- Start of processing for Use_One_Package
10581
10582 begin
10583 -- Use_One_Package may have been called recursively to handle an
10584 -- implicit use for a auxiliary system package, so set P accordingly
10585 -- and skip redundancy checks.
10586
10587 if No (Pack_Name) and then Present_System_Aux (N) then
10588 P := System_Aux_Id;
10589
10590 -- Check for redundant use_package_clauses
10591
10592 else
10593 -- Ignore cases where we are dealing with a non user defined package
10594 -- like Standard_Standard or something other than a valid package.
10595
10596 if not Is_Entity_Name (Pack_Name)
10597 or else No (Entity (Pack_Name))
10598 or else Ekind (Entity (Pack_Name)) /= E_Package
10599 then
10600 return;
10601 end if;
10602
10603 -- When a renaming exists we must check it for redundancy. The
10604 -- original package would have already been seen at this point.
10605
10606 if Present (Renamed_Entity (Entity (Pack_Name))) then
10607 P := Renamed_Entity (Entity (Pack_Name));
10608 else
10609 P := Entity (Pack_Name);
10610 end if;
10611
10612 -- Check for redundant clauses then set the current use clause for
10613 -- P if were are not "forcing" an installation from a scope
10614 -- reinstallation that is done throughout analysis for various
10615 -- reasons.
10616
10617 if In_Use (P) then
10618 Note_Redundant_Use (Pack_Name);
10619
10620 if not Force then
10621 Set_Current_Use_Clause (P, N);
10622 end if;
10623
10624 return;
10625
10626 -- Warn about detected redundant clauses
10627
10628 elsif not Force
10629 and then In_Open_Scopes (P)
10630 and then not Is_Hidden_Open_Scope (P)
10631 then
10632 if Warn_On_Redundant_Constructs and then P = Current_Scope then
10633 Error_Msg_NE -- CODEFIX
10634 ("& is already use-visible within itself?r?",
10635 Pack_Name, P);
10636 end if;
10637
10638 return;
10639 end if;
10640
10641 -- Set P back to the non-renamed package so that visibility of the
10642 -- entities within the package can be properly set below.
10643
10644 P := Entity (Pack_Name);
10645 end if;
10646
10647 Set_In_Use (P);
10648 Set_Current_Use_Clause (P, N);
10649
10650 -- Ada 2005 (AI-50217): Check restriction
10651
10652 if From_Limited_With (P) then
10653 Error_Msg_N ("limited withed package cannot appear in use clause", N);
10654 end if;
10655
10656 -- Find enclosing instance, if any
10657
10658 if In_Instance then
10659 Current_Instance := Current_Scope;
10660 while not Is_Generic_Instance (Current_Instance) loop
10661 Current_Instance := Scope (Current_Instance);
10662 end loop;
10663
10664 if No (Hidden_By_Use_Clause (N)) then
10665 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
10666 end if;
10667 end if;
10668
10669 -- If unit is a package renaming, indicate that the renamed package is
10670 -- also in use (the flags on both entities must remain consistent, and a
10671 -- subsequent use of either of them should be recognized as redundant).
10672
10673 if Present (Renamed_Entity (P)) then
10674 Set_In_Use (Renamed_Entity (P));
10675 Set_Current_Use_Clause (Renamed_Entity (P), N);
10676 Real_P := Renamed_Entity (P);
10677 else
10678 Real_P := P;
10679 end if;
10680
10681 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
10682 -- found in the private part of a package specification
10683
10684 if In_Private_Part (Current_Scope)
10685 and then Has_Private_With (P)
10686 and then Is_Child_Unit (Current_Scope)
10687 and then Is_Child_Unit (P)
10688 and then Is_Ancestor_Package (Scope (Current_Scope), P)
10689 then
10690 Private_With_OK := True;
10691 end if;
10692
10693 -- Loop through entities in one package making them potentially
10694 -- use-visible.
10695
10696 Id := First_Entity (P);
10697 while Present (Id)
10698 and then (Id /= First_Private_Entity (P)
10699 or else Private_With_OK) -- Ada 2005 (AI-262)
10700 loop
10701 Prev := Current_Entity (Id);
10702 while Present (Prev) loop
10703 if Is_Immediately_Visible (Prev)
10704 and then (not Is_Overloadable (Prev)
10705 or else not Is_Overloadable (Id)
10706 or else Type_Conformant (Id, Prev))
10707 then
10708 if No (Current_Instance) then
10709
10710 -- Potentially use-visible entity remains hidden
10711
10712 if Warn_On_Hiding then
10713 Warn_On_Hiding_Entity (N, Hidden => Id, Visible => Prev,
10714 On_Use_Clause => True);
10715 end if;
10716
10717 goto Next_Usable_Entity;
10718
10719 -- A use clause within an instance hides outer global entities,
10720 -- which are not used to resolve local entities in the
10721 -- instance. Note that the predefined entities in Standard
10722 -- could not have been hidden in the generic by a use clause,
10723 -- and therefore remain visible. Other compilation units whose
10724 -- entities appear in Standard must be hidden in an instance.
10725
10726 -- To determine whether an entity is external to the instance
10727 -- we compare the scope depth of its scope with that of the
10728 -- current instance. However, a generic actual of a subprogram
10729 -- instance is declared in the wrapper package but will not be
10730 -- hidden by a use-visible entity. similarly, an entity that is
10731 -- declared in an enclosing instance will not be hidden by an
10732 -- an entity declared in a generic actual, which can only have
10733 -- been use-visible in the generic and will not have hidden the
10734 -- entity in the generic parent.
10735
10736 -- If Id is called Standard, the predefined package with the
10737 -- same name is in the homonym chain. It has to be ignored
10738 -- because it has no defined scope (being the only entity in
10739 -- the system with this mandated behavior).
10740
10741 elsif not Is_Hidden (Id)
10742 and then Present (Scope (Prev))
10743 and then not Is_Wrapper_Package (Scope (Prev))
10744 and then Scope_Depth (Scope (Prev)) <
10745 Scope_Depth (Current_Instance)
10746 and then (Scope (Prev) /= Standard_Standard
10747 or else Sloc (Prev) > Standard_Location)
10748 then
10749 if In_Open_Scopes (Scope (Prev))
10750 and then Is_Generic_Instance (Scope (Prev))
10751 and then Present (Associated_Formal_Package (P))
10752 then
10753 null;
10754
10755 else
10756 Set_Is_Potentially_Use_Visible (Id);
10757 Set_Is_Immediately_Visible (Prev, False);
10758 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10759 end if;
10760 end if;
10761
10762 -- A user-defined operator is not use-visible if the predefined
10763 -- operator for the type is immediately visible, which is the case
10764 -- if the type of the operand is in an open scope. This does not
10765 -- apply to user-defined operators that have operands of different
10766 -- types, because the predefined mixed mode operations (multiply
10767 -- and divide) apply to universal types and do not hide anything.
10768
10769 elsif Ekind (Prev) = E_Operator
10770 and then Operator_Matches_Spec (Prev, Id)
10771 and then In_Open_Scopes
10772 (Scope (Base_Type (Etype (First_Formal (Id)))))
10773 and then (No (Next_Formal (First_Formal (Id)))
10774 or else Etype (First_Formal (Id)) =
10775 Etype (Next_Formal (First_Formal (Id)))
10776 or else Chars (Prev) = Name_Op_Expon)
10777 then
10778 goto Next_Usable_Entity;
10779
10780 -- In an instance, two homonyms may become use_visible through the
10781 -- actuals of distinct formal packages. In the generic, only the
10782 -- current one would have been visible, so make the other one
10783 -- not use_visible.
10784
10785 -- In certain pathological cases it is possible that unrelated
10786 -- homonyms from distinct formal packages may exist in an
10787 -- uninstalled scope. We must test for that here.
10788
10789 elsif Present (Current_Instance)
10790 and then Is_Potentially_Use_Visible (Prev)
10791 and then not Is_Overloadable (Prev)
10792 and then Scope (Id) /= Scope (Prev)
10793 and then Used_As_Generic_Actual (Scope (Prev))
10794 and then Used_As_Generic_Actual (Scope (Id))
10795 and then Is_List_Member (Scope (Prev))
10796 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
10797 Current_Use_Clause (Scope (Id)))
10798 then
10799 Set_Is_Potentially_Use_Visible (Prev, False);
10800 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10801 end if;
10802
10803 Prev := Homonym (Prev);
10804 end loop;
10805
10806 -- On exit, we know entity is not hidden, unless it is private
10807
10808 if not Is_Hidden (Id)
10809 and then (not Is_Child_Unit (Id) or else Is_Visible_Lib_Unit (Id))
10810 then
10811 Set_Is_Potentially_Use_Visible (Id);
10812
10813 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
10814 Set_Is_Potentially_Use_Visible (Full_View (Id));
10815 end if;
10816 end if;
10817
10818 <<Next_Usable_Entity>>
10819 Next_Entity (Id);
10820 end loop;
10821
10822 -- Child units are also made use-visible by a use clause, but they may
10823 -- appear after all visible declarations in the parent entity list.
10824
10825 while Present (Id) loop
10826 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
10827 Set_Is_Potentially_Use_Visible (Id);
10828 end if;
10829
10830 Next_Entity (Id);
10831 end loop;
10832
10833 if Chars (Real_P) = Name_System
10834 and then Scope (Real_P) = Standard_Standard
10835 and then Present_System_Aux (N)
10836 then
10837 Use_One_Package (N);
10838 end if;
10839 end Use_One_Package;
10840
10841 ------------------
10842 -- Use_One_Type --
10843 ------------------
10844
10845 procedure Use_One_Type
10846 (Id : Node_Id;
10847 Installed : Boolean := False;
10848 Force : Boolean := False)
10849 is
10850 function Spec_Reloaded_For_Body return Boolean;
10851 -- Determine whether the compilation unit is a package body and the use
10852 -- type clause is in the spec of the same package. Even though the spec
10853 -- was analyzed first, its context is reloaded when analysing the body.
10854
10855 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
10856 -- AI05-150: if the use_type_clause carries the "all" qualifier,
10857 -- class-wide operations of ancestor types are use-visible if the
10858 -- ancestor type is visible.
10859
10860 ----------------------------
10861 -- Spec_Reloaded_For_Body --
10862 ----------------------------
10863
10864 function Spec_Reloaded_For_Body return Boolean is
10865 begin
10866 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
10867 declare
10868 Spec : constant Node_Id :=
10869 Parent (List_Containing (Parent (Id)));
10870
10871 begin
10872 -- Check whether type is declared in a package specification,
10873 -- and current unit is the corresponding package body. The
10874 -- use clauses themselves may be within a nested package.
10875
10876 return
10877 Nkind (Spec) = N_Package_Specification
10878 and then In_Same_Source_Unit
10879 (Corresponding_Body (Parent (Spec)),
10880 Cunit_Entity (Current_Sem_Unit));
10881 end;
10882 end if;
10883
10884 return False;
10885 end Spec_Reloaded_For_Body;
10886
10887 -------------------------------
10888 -- Use_Class_Wide_Operations --
10889 -------------------------------
10890
10891 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
10892 function Is_Class_Wide_Operation_Of
10893 (Op : Entity_Id;
10894 T : Entity_Id) return Boolean;
10895 -- Determine whether a subprogram has a class-wide parameter or
10896 -- result that is T'Class.
10897
10898 ---------------------------------
10899 -- Is_Class_Wide_Operation_Of --
10900 ---------------------------------
10901
10902 function Is_Class_Wide_Operation_Of
10903 (Op : Entity_Id;
10904 T : Entity_Id) return Boolean
10905 is
10906 Formal : Entity_Id;
10907
10908 begin
10909 Formal := First_Formal (Op);
10910 while Present (Formal) loop
10911 if Etype (Formal) = Class_Wide_Type (T) then
10912 return True;
10913 end if;
10914
10915 Next_Formal (Formal);
10916 end loop;
10917
10918 if Etype (Op) = Class_Wide_Type (T) then
10919 return True;
10920 end if;
10921
10922 return False;
10923 end Is_Class_Wide_Operation_Of;
10924
10925 -- Local variables
10926
10927 Ent : Entity_Id;
10928 Scop : Entity_Id;
10929
10930 -- Start of processing for Use_Class_Wide_Operations
10931
10932 begin
10933 Scop := Scope (Typ);
10934 if not Is_Hidden (Scop) then
10935 Ent := First_Entity (Scop);
10936 while Present (Ent) loop
10937 if Is_Overloadable (Ent)
10938 and then Is_Class_Wide_Operation_Of (Ent, Typ)
10939 and then not Is_Potentially_Use_Visible (Ent)
10940 then
10941 Set_Is_Potentially_Use_Visible (Ent);
10942 Append_Elmt (Ent, Used_Operations (Parent (Id)));
10943 end if;
10944
10945 Next_Entity (Ent);
10946 end loop;
10947 end if;
10948
10949 if Is_Derived_Type (Typ) then
10950 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
10951 end if;
10952 end Use_Class_Wide_Operations;
10953
10954 -- Local variables
10955
10956 Elmt : Elmt_Id;
10957 Is_Known_Used : Boolean;
10958 Op_List : Elist_Id;
10959 T : Entity_Id;
10960
10961 -- Start of processing for Use_One_Type
10962
10963 begin
10964 if Entity (Id) = Any_Type then
10965 return;
10966 end if;
10967
10968 -- It is the type determined by the subtype mark (8.4(8)) whose
10969 -- operations become potentially use-visible.
10970
10971 T := Base_Type (Entity (Id));
10972
10973 -- Either the type itself is used, the package where it is declared is
10974 -- in use or the entity is declared in the current package, thus
10975 -- use-visible.
10976
10977 Is_Known_Used :=
10978 (In_Use (T)
10979 and then ((Present (Current_Use_Clause (T))
10980 and then All_Present (Current_Use_Clause (T)))
10981 or else not All_Present (Parent (Id))))
10982 or else In_Use (Scope (T))
10983 or else Scope (T) = Current_Scope;
10984
10985 Set_Redundant_Use (Id,
10986 Is_Known_Used or else Is_Potentially_Use_Visible (T));
10987
10988 if Ekind (T) = E_Incomplete_Type then
10989 Error_Msg_N ("premature usage of incomplete type", Id);
10990
10991 elsif In_Open_Scopes (Scope (T)) then
10992 null;
10993
10994 -- A limited view cannot appear in a use_type_clause. However, an access
10995 -- type whose designated type is limited has the flag but is not itself
10996 -- a limited view unless we only have a limited view of its enclosing
10997 -- package.
10998
10999 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
11000 Error_Msg_N
11001 ("incomplete type from limited view cannot appear in use clause",
11002 Id);
11003
11004 -- If the use clause is redundant, Used_Operations will usually be
11005 -- empty, but we need to set it to empty here in one case: If we are
11006 -- instantiating a generic library unit, then we install the ancestors
11007 -- of that unit in the scope stack, which involves reprocessing use
11008 -- clauses in those ancestors. Such a use clause will typically have a
11009 -- nonempty Used_Operations unless it was redundant in the generic unit,
11010 -- even if it is redundant at the place of the instantiation.
11011
11012 elsif Redundant_Use (Id) then
11013 Set_Used_Operations (Parent (Id), New_Elmt_List);
11014
11015 -- If the subtype mark designates a subtype in a different package,
11016 -- we have to check that the parent type is visible, otherwise the
11017 -- use_type_clause is a no-op. Not clear how to do that???
11018
11019 else
11020 Set_Current_Use_Clause (T, Parent (Id));
11021 Set_In_Use (T);
11022
11023 -- If T is tagged, primitive operators on class-wide operands are
11024 -- also deemed available. Note that this is really necessary only
11025 -- in semantics-only mode, because the primitive operators are not
11026 -- fully constructed in this mode, but we do it in all modes for the
11027 -- sake of uniformity, as this should not matter in practice.
11028
11029 if Is_Tagged_Type (T) then
11030 Set_In_Use (Class_Wide_Type (T));
11031 end if;
11032
11033 -- Iterate over primitive operations of the type. If an operation is
11034 -- already use_visible, it is the result of a previous use_clause,
11035 -- and already appears on the corresponding entity chain. If the
11036 -- clause is being reinstalled, operations are already use-visible.
11037
11038 if Installed then
11039 null;
11040
11041 else
11042 Op_List := Collect_Primitive_Operations (T);
11043 Elmt := First_Elmt (Op_List);
11044 while Present (Elmt) loop
11045 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
11046 or else Chars (Node (Elmt)) in Any_Operator_Name)
11047 and then not Is_Hidden (Node (Elmt))
11048 and then not Is_Potentially_Use_Visible (Node (Elmt))
11049 then
11050 Set_Is_Potentially_Use_Visible (Node (Elmt));
11051 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
11052
11053 elsif Ada_Version >= Ada_2012
11054 and then All_Present (Parent (Id))
11055 and then not Is_Hidden (Node (Elmt))
11056 and then not Is_Potentially_Use_Visible (Node (Elmt))
11057 then
11058 Set_Is_Potentially_Use_Visible (Node (Elmt));
11059 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
11060 end if;
11061
11062 Next_Elmt (Elmt);
11063 end loop;
11064 end if;
11065
11066 if Ada_Version >= Ada_2012
11067 and then All_Present (Parent (Id))
11068 and then Is_Tagged_Type (T)
11069 then
11070 Use_Class_Wide_Operations (T);
11071 end if;
11072 end if;
11073
11074 -- If warning on redundant constructs, check for unnecessary WITH
11075
11076 if not Force
11077 and then Warn_On_Redundant_Constructs
11078 and then Is_Known_Used
11079
11080 -- with P; with P; use P;
11081 -- package P is package X is package body X is
11082 -- type T ... use P.T;
11083
11084 -- The compilation unit is the body of X. GNAT first compiles the
11085 -- spec of X, then proceeds to the body. At that point P is marked
11086 -- as use visible. The analysis then reinstalls the spec along with
11087 -- its context. The use clause P.T is now recognized as redundant,
11088 -- but in the wrong context. Do not emit a warning in such cases.
11089 -- Do not emit a warning either if we are in an instance, there is
11090 -- no redundancy between an outer use_clause and one that appears
11091 -- within the generic.
11092
11093 and then not Spec_Reloaded_For_Body
11094 and then not In_Instance
11095 and then not In_Inlined_Body
11096 then
11097 -- The type already has a use clause
11098
11099 if In_Use (T) then
11100
11101 -- Case where we know the current use clause for the type
11102
11103 if Present (Current_Use_Clause (T)) then
11104 Use_Clause_Known : declare
11105 Clause1 : constant Node_Id :=
11106 Find_First_Use (Current_Use_Clause (T));
11107 Clause2 : constant Node_Id := Parent (Id);
11108 Ent1 : Entity_Id;
11109 Ent2 : Entity_Id;
11110 Err_No : Node_Id;
11111 Unit1 : Node_Id;
11112 Unit2 : Node_Id;
11113
11114 -- Start of processing for Use_Clause_Known
11115
11116 begin
11117 -- If the unit is a subprogram body that acts as spec, the
11118 -- context clause is shared with the constructed subprogram
11119 -- spec. Clearly there is no redundancy.
11120
11121 if Clause1 = Clause2 then
11122 return;
11123 end if;
11124
11125 Unit1 := Unit (Enclosing_Comp_Unit_Node (Clause1));
11126 Unit2 := Unit (Enclosing_Comp_Unit_Node (Clause2));
11127
11128 -- If both clauses are on same unit, or one is the body of
11129 -- the other, or one of them is in a subunit, report
11130 -- redundancy on the later one.
11131
11132 if Unit1 = Unit2
11133 or else Nkind (Unit1) = N_Subunit
11134 or else
11135 (Nkind (Unit2) in N_Package_Body | N_Subprogram_Body
11136 and then Nkind (Unit1) /= Nkind (Unit2)
11137 and then Nkind (Unit1) /= N_Subunit)
11138 then
11139 Error_Msg_Sloc := Sloc (Clause1);
11140 Error_Msg_NE -- CODEFIX
11141 ("& is already use-visible through previous "
11142 & "use_type_clause #?r?", Clause2, T);
11143 return;
11144 end if;
11145
11146 -- If there is a redundant use_type_clause in a child unit
11147 -- determine which of the units is more deeply nested. If a
11148 -- unit is a package instance, retrieve the entity and its
11149 -- scope from the instance spec.
11150
11151 Ent1 := Entity_Of_Unit (Unit1);
11152 Ent2 := Entity_Of_Unit (Unit2);
11153
11154 -- When the scope of both units' entities are
11155 -- Standard_Standard then neither Unit1 or Unit2 are child
11156 -- units - so return in that case.
11157
11158 if Scope (Ent1) = Standard_Standard
11159 and then Scope (Ent2) = Standard_Standard
11160 then
11161 return;
11162
11163 -- Otherwise, determine if one of the units is not a child
11164
11165 elsif Scope (Ent2) = Standard_Standard then
11166 Error_Msg_Sloc := Sloc (Clause2);
11167 Err_No := Clause1;
11168
11169 elsif Scope (Ent1) = Standard_Standard then
11170 Error_Msg_Sloc := Sloc (Id);
11171 Err_No := Clause2;
11172
11173 -- If both units are child units, we determine which one is
11174 -- the descendant by the scope distance to the ultimate
11175 -- parent unit.
11176
11177 else
11178 declare
11179 S1 : Entity_Id;
11180 S2 : Entity_Id;
11181
11182 begin
11183 S1 := Scope (Ent1);
11184 S2 := Scope (Ent2);
11185 while Present (S1)
11186 and then Present (S2)
11187 and then S1 /= Standard_Standard
11188 and then S2 /= Standard_Standard
11189 loop
11190 S1 := Scope (S1);
11191 S2 := Scope (S2);
11192 end loop;
11193
11194 if S1 = Standard_Standard then
11195 Error_Msg_Sloc := Sloc (Id);
11196 Err_No := Clause2;
11197 else
11198 Error_Msg_Sloc := Sloc (Clause2);
11199 Err_No := Clause1;
11200 end if;
11201 end;
11202 end if;
11203
11204 if Parent (Id) /= Err_No then
11205 if Most_Descendant_Use_Clause
11206 (Err_No, Parent (Id)) = Parent (Id)
11207 then
11208 Error_Msg_Sloc := Sloc (Err_No);
11209 Err_No := Parent (Id);
11210 end if;
11211
11212 Error_Msg_NE -- CODEFIX
11213 ("& is already use-visible through previous "
11214 & "use_type_clause #?r?", Err_No, Id);
11215 end if;
11216 end Use_Clause_Known;
11217
11218 -- Here Current_Use_Clause is not set for T, so we do not have the
11219 -- location information available.
11220
11221 else
11222 Error_Msg_NE -- CODEFIX
11223 ("& is already use-visible through previous "
11224 & "use_type_clause?r?", Id, T);
11225 end if;
11226
11227 -- The package where T is declared is already used
11228
11229 elsif In_Use (Scope (T)) then
11230 -- Due to expansion of contracts we could be attempting to issue
11231 -- a spurious warning - so verify there is a previous use clause.
11232
11233 if Current_Use_Clause (Scope (T)) /=
11234 Find_First_Use (Current_Use_Clause (Scope (T)))
11235 then
11236 Error_Msg_Sloc :=
11237 Sloc (Find_First_Use (Current_Use_Clause (Scope (T))));
11238 Error_Msg_NE -- CODEFIX
11239 ("& is already use-visible through package use clause #?r?",
11240 Id, T);
11241 end if;
11242
11243 -- The current scope is the package where T is declared
11244
11245 else
11246 Error_Msg_Node_2 := Scope (T);
11247 Error_Msg_NE -- CODEFIX
11248 ("& is already use-visible inside package &?r?", Id, T);
11249 end if;
11250 end if;
11251 end Use_One_Type;
11252
11253 ----------------
11254 -- Write_Info --
11255 ----------------
11256
11257 procedure Write_Info is
11258 Id : Entity_Id := First_Entity (Current_Scope);
11259
11260 begin
11261 -- No point in dumping standard entities
11262
11263 if Current_Scope = Standard_Standard then
11264 return;
11265 end if;
11266
11267 Write_Str ("========================================================");
11268 Write_Eol;
11269 Write_Str (" Defined Entities in ");
11270 Write_Name (Chars (Current_Scope));
11271 Write_Eol;
11272 Write_Str ("========================================================");
11273 Write_Eol;
11274
11275 if No (Id) then
11276 Write_Str ("-- none --");
11277 Write_Eol;
11278
11279 else
11280 while Present (Id) loop
11281 Write_Entity_Info (Id, " ");
11282 Next_Entity (Id);
11283 end loop;
11284 end if;
11285
11286 if Scope (Current_Scope) = Standard_Standard then
11287
11288 -- Print information on the current unit itself
11289
11290 Write_Entity_Info (Current_Scope, " ");
11291 end if;
11292
11293 Write_Eol;
11294 end Write_Info;
11295
11296 --------
11297 -- ws --
11298 --------
11299
11300 procedure ws is
11301 S : Entity_Id;
11302 begin
11303 for J in reverse 1 .. Scope_Stack.Last loop
11304 S := Scope_Stack.Table (J).Entity;
11305 Write_Int (Int (S));
11306 Write_Str (" === ");
11307 Write_Name (Chars (S));
11308 Write_Eol;
11309 end loop;
11310 end ws;
11311
11312 --------
11313 -- we --
11314 --------
11315
11316 procedure we (S : Entity_Id) is
11317 E : Entity_Id;
11318 begin
11319 E := First_Entity (S);
11320 while Present (E) loop
11321 Write_Int (Int (E));
11322 Write_Str (" === ");
11323 Write_Name (Chars (E));
11324 Write_Eol;
11325 Next_Entity (E);
11326 end loop;
11327 end we;
11328 end Sem_Ch8;