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