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