]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch8.adb
[Ada] Rename Ada 202* to Ada 2022
[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_Most_Prev (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 -- ??? a better subprogram name is in order
489
490 function Find_Renamed_Entity
491 (N : Node_Id;
492 Nam : Node_Id;
493 New_S : Entity_Id;
494 Is_Actual : Boolean := False) return Entity_Id;
495 -- Find the renamed entity that corresponds to the given parameter profile
496 -- in a subprogram renaming declaration. The renamed entity may be an
497 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
498 -- indicates that the renaming is the one generated for an actual subpro-
499 -- gram in an instance, for which special visibility checks apply.
500
501 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
502 -- Find a type derived from Character or Wide_Character in the prefix of N.
503 -- Used to resolved qualified names whose selector is a character literal.
504
505 function Has_Private_With (E : Entity_Id) return Boolean;
506 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
507 -- private with on E.
508
509 function Has_Components (Typ : Entity_Id) return Boolean;
510 -- Determine if given type has components, i.e. is either a record type or
511 -- type or a type that has discriminants.
512
513 function Has_Implicit_Operator (N : Node_Id) return Boolean;
514 -- N is an expanded name whose selector is an operator name (e.g. P."+").
515 -- declarative part contains an implicit declaration of an operator if it
516 -- has a declaration of a type to which one of the predefined operators
517 -- apply. The existence of this routine is an implementation artifact. A
518 -- more straightforward but more space-consuming choice would be to make
519 -- all inherited operators explicit in the symbol table.
520
521 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
522 -- A subprogram defined by a renaming declaration inherits the parameter
523 -- profile of the renamed entity. The subtypes given in the subprogram
524 -- specification are discarded and replaced with those of the renamed
525 -- subprogram, which are then used to recheck the default values.
526
527 function Most_Descendant_Use_Clause
528 (Clause1 : Entity_Id;
529 Clause2 : Entity_Id) return Entity_Id;
530 -- Determine which use clause parameter is the most descendant in terms of
531 -- scope.
532 -- ??? a better subprogram name is in order
533
534 procedure Premature_Usage (N : Node_Id);
535 -- Diagnose usage of an entity before it is visible
536
537 procedure Use_One_Package
538 (N : Node_Id;
539 Pack_Name : Entity_Id := Empty;
540 Force : Boolean := False);
541 -- Make visible entities declared in package P potentially use-visible
542 -- in the current context. Also used in the analysis of subunits, when
543 -- re-installing use clauses of parent units. N is the use_clause that
544 -- names P (and possibly other packages).
545
546 procedure Use_One_Type
547 (Id : Node_Id;
548 Installed : Boolean := False;
549 Force : Boolean := False);
550 -- Id is the subtype mark from a use_type_clause. This procedure makes
551 -- the primitive operators of the type potentially use-visible. The
552 -- boolean flag Installed indicates that the clause is being reinstalled
553 -- after previous analysis, and primitive operations are already chained
554 -- on the Used_Operations list of the clause.
555
556 procedure Write_Info;
557 -- Write debugging information on entities declared in current scope
558
559 --------------------------------
560 -- Analyze_Exception_Renaming --
561 --------------------------------
562
563 -- The language only allows a single identifier, but the tree holds an
564 -- identifier list. The parser has already issued an error message if
565 -- there is more than one element in the list.
566
567 procedure Analyze_Exception_Renaming (N : Node_Id) is
568 Id : constant Entity_Id := Defining_Entity (N);
569 Nam : constant Node_Id := Name (N);
570
571 begin
572 Enter_Name (Id);
573 Analyze (Nam);
574
575 Mutate_Ekind (Id, E_Exception);
576 Set_Etype (Id, Standard_Exception_Type);
577 Set_Is_Pure (Id, Is_Pure (Current_Scope));
578
579 if Is_Entity_Name (Nam)
580 and then Present (Entity (Nam))
581 and then Ekind (Entity (Nam)) = E_Exception
582 then
583 if Present (Renamed_Object (Entity (Nam))) then
584 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
585 else
586 Set_Renamed_Object (Id, Entity (Nam));
587 end if;
588
589 -- The exception renaming declaration may become Ghost if it renames
590 -- a Ghost entity.
591
592 Mark_Ghost_Renaming (N, Entity (Nam));
593 else
594 Error_Msg_N ("invalid exception name in renaming", Nam);
595 end if;
596
597 -- Implementation-defined aspect specifications can appear in a renaming
598 -- declaration, but not language-defined ones. The call to procedure
599 -- Analyze_Aspect_Specifications will take care of this error check.
600
601 if Has_Aspects (N) then
602 Analyze_Aspect_Specifications (N, Id);
603 end if;
604 end Analyze_Exception_Renaming;
605
606 ---------------------------
607 -- Analyze_Expanded_Name --
608 ---------------------------
609
610 procedure Analyze_Expanded_Name (N : Node_Id) is
611 begin
612 -- If the entity pointer is already set, this is an internal node, or a
613 -- node that is analyzed more than once, after a tree modification. In
614 -- such a case there is no resolution to perform, just set the type. In
615 -- either case, start by analyzing the prefix.
616
617 Analyze (Prefix (N));
618
619 if Present (Entity (N)) then
620 if Is_Type (Entity (N)) then
621 Set_Etype (N, Entity (N));
622 else
623 Set_Etype (N, Etype (Entity (N)));
624 end if;
625
626 else
627 Find_Expanded_Name (N);
628 end if;
629
630 -- In either case, propagate dimension of entity to expanded name
631
632 Analyze_Dimension (N);
633 end Analyze_Expanded_Name;
634
635 ---------------------------------------
636 -- Analyze_Generic_Function_Renaming --
637 ---------------------------------------
638
639 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
640 begin
641 Analyze_Generic_Renaming (N, E_Generic_Function);
642 end Analyze_Generic_Function_Renaming;
643
644 --------------------------------------
645 -- Analyze_Generic_Package_Renaming --
646 --------------------------------------
647
648 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
649 begin
650 -- Test for the Text_IO special unit case here, since we may be renaming
651 -- one of the subpackages of Text_IO, then join common routine.
652
653 Check_Text_IO_Special_Unit (Name (N));
654
655 Analyze_Generic_Renaming (N, E_Generic_Package);
656 end Analyze_Generic_Package_Renaming;
657
658 ----------------------------------------
659 -- Analyze_Generic_Procedure_Renaming --
660 ----------------------------------------
661
662 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
663 begin
664 Analyze_Generic_Renaming (N, E_Generic_Procedure);
665 end Analyze_Generic_Procedure_Renaming;
666
667 ------------------------------
668 -- Analyze_Generic_Renaming --
669 ------------------------------
670
671 procedure Analyze_Generic_Renaming
672 (N : Node_Id;
673 K : Entity_Kind)
674 is
675 New_P : constant Entity_Id := Defining_Entity (N);
676 Inst : Boolean := False;
677 Old_P : Entity_Id;
678
679 begin
680 if Name (N) = Error then
681 return;
682 end if;
683
684 Generate_Definition (New_P);
685
686 if Current_Scope /= Standard_Standard then
687 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
688 end if;
689
690 if Nkind (Name (N)) = N_Selected_Component then
691 Check_Generic_Child_Unit (Name (N), Inst);
692 else
693 Analyze (Name (N));
694 end if;
695
696 if not Is_Entity_Name (Name (N)) then
697 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
698 Old_P := Any_Id;
699 else
700 Old_P := Entity (Name (N));
701 end if;
702
703 Enter_Name (New_P);
704 Mutate_Ekind (New_P, K);
705
706 if Etype (Old_P) = Any_Type then
707 null;
708
709 elsif Ekind (Old_P) /= K then
710 Error_Msg_N ("invalid generic unit name", Name (N));
711
712 else
713 if Present (Renamed_Object (Old_P)) then
714 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
715 else
716 Set_Renamed_Object (New_P, Old_P);
717 end if;
718
719 -- The generic renaming declaration may become Ghost if it renames a
720 -- Ghost entity.
721
722 Mark_Ghost_Renaming (N, Old_P);
723
724 Set_Is_Pure (New_P, Is_Pure (Old_P));
725 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
726
727 Set_Etype (New_P, Etype (Old_P));
728 Set_Has_Completion (New_P);
729
730 if In_Open_Scopes (Old_P) then
731 Error_Msg_N ("within its scope, generic denotes its instance", N);
732 end if;
733
734 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
735 -- renamings and subsequent instantiations of Unchecked_Conversion.
736
737 if Is_Generic_Subprogram (Old_P) then
738 Set_Is_Intrinsic_Subprogram
739 (New_P, Is_Intrinsic_Subprogram (Old_P));
740 end if;
741
742 Check_Library_Unit_Renaming (N, Old_P);
743 end if;
744
745 -- Implementation-defined aspect specifications can appear in a renaming
746 -- declaration, but not language-defined ones. The call to procedure
747 -- Analyze_Aspect_Specifications will take care of this error check.
748
749 if Has_Aspects (N) then
750 Analyze_Aspect_Specifications (N, New_P);
751 end if;
752 end Analyze_Generic_Renaming;
753
754 -----------------------------
755 -- Analyze_Object_Renaming --
756 -----------------------------
757
758 procedure Analyze_Object_Renaming (N : Node_Id) is
759 Id : constant Entity_Id := Defining_Identifier (N);
760 Loc : constant Source_Ptr := Sloc (N);
761 Nam : constant Node_Id := Name (N);
762 Is_Object_Ref : Boolean;
763 Dec : Node_Id;
764 T : Entity_Id;
765 T2 : Entity_Id;
766 Q : Node_Id;
767
768 procedure Check_Constrained_Object;
769 -- If the nominal type is unconstrained but the renamed object is
770 -- constrained, as can happen with renaming an explicit dereference or
771 -- a function return, build a constrained subtype from the object. If
772 -- the renaming is for a formal in an accept statement, the analysis
773 -- has already established its actual subtype. This is only relevant
774 -- if the renamed object is an explicit dereference.
775
776 function Get_Object_Name (Nod : Node_Id) return Node_Id;
777 -- Obtain the name of the object from node Nod which is being renamed by
778 -- the object renaming declaration N.
779
780 function Find_Raise_Node (N : Node_Id) return Traverse_Result;
781 -- Process one node in search for N_Raise_xxx_Error nodes.
782 -- Return Abandon if found, OK otherwise.
783
784 ---------------------
785 -- Find_Raise_Node --
786 ---------------------
787
788 function Find_Raise_Node (N : Node_Id) return Traverse_Result is
789 begin
790 if Nkind (N) in N_Raise_xxx_Error then
791 return Abandon;
792 else
793 return OK;
794 end if;
795 end Find_Raise_Node;
796
797 ------------------------
798 -- No_Raise_xxx_Error --
799 ------------------------
800
801 function No_Raise_xxx_Error is new Traverse_Func (Find_Raise_Node);
802 -- Traverse tree to look for a N_Raise_xxx_Error node and returns
803 -- Abandon if so and OK if none found.
804
805 ------------------------------
806 -- Check_Constrained_Object --
807 ------------------------------
808
809 procedure Check_Constrained_Object is
810 Typ : constant Entity_Id := Etype (Nam);
811 Subt : Entity_Id;
812 Loop_Scheme : Node_Id;
813
814 begin
815 if Nkind (Nam) in N_Function_Call | N_Explicit_Dereference
816 and then Is_Composite_Type (Typ)
817 and then not Is_Constrained (Typ)
818 and then not Has_Unknown_Discriminants (Typ)
819 and then Expander_Active
820 then
821 -- If Actual_Subtype is already set, nothing to do
822
823 if Ekind (Id) in E_Variable | E_Constant
824 and then Present (Actual_Subtype (Id))
825 then
826 null;
827
828 -- A renaming of an unchecked union has no actual subtype
829
830 elsif Is_Unchecked_Union (Typ) then
831 null;
832
833 -- If a record is limited its size is invariant. This is the case
834 -- in particular with record types with an access discriminant
835 -- that are used in iterators. This is an optimization, but it
836 -- also prevents typing anomalies when the prefix is further
837 -- expanded.
838
839 -- Note that we cannot just use the Is_Limited_Record flag because
840 -- it does not apply to records with limited components, for which
841 -- this syntactic flag is not set, but whose size is also fixed.
842
843 -- Note also that we need to build the constrained subtype for an
844 -- array in order to make the bounds explicit in most cases, but
845 -- not if the object comes from an extended return statement, as
846 -- this would create dangling references to them later on.
847
848 elsif Is_Limited_Type (Typ)
849 and then (not Is_Array_Type (Typ) or else Is_Return_Object (Id))
850 then
851 null;
852
853 else
854 Subt := Make_Temporary (Loc, 'T');
855 Remove_Side_Effects (Nam);
856 Insert_Action (N,
857 Make_Subtype_Declaration (Loc,
858 Defining_Identifier => Subt,
859 Subtype_Indication =>
860 Make_Subtype_From_Expr (Nam, Typ)));
861 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
862 Set_Etype (Nam, Subt);
863
864 -- Suppress discriminant checks on this subtype if the original
865 -- type has defaulted discriminants and Id is a "for of" loop
866 -- iterator.
867
868 if Has_Defaulted_Discriminants (Typ)
869 and then Nkind (Original_Node (Parent (N))) = N_Loop_Statement
870 then
871 Loop_Scheme := Iteration_Scheme (Original_Node (Parent (N)));
872
873 if Present (Loop_Scheme)
874 and then Present (Iterator_Specification (Loop_Scheme))
875 and then
876 Defining_Identifier
877 (Iterator_Specification (Loop_Scheme)) = Id
878 then
879 Set_Checks_May_Be_Suppressed (Subt);
880 Push_Local_Suppress_Stack_Entry
881 (Entity => Subt,
882 Check => Discriminant_Check,
883 Suppress => True);
884 end if;
885 end if;
886
887 -- Freeze subtype at once, to prevent order of elaboration
888 -- issues in the backend. The renamed object exists, so its
889 -- type is already frozen in any case.
890
891 Freeze_Before (N, Subt);
892 end if;
893 end if;
894 end Check_Constrained_Object;
895
896 ---------------------
897 -- Get_Object_Name --
898 ---------------------
899
900 function Get_Object_Name (Nod : Node_Id) return Node_Id is
901 Obj_Nam : Node_Id;
902
903 begin
904 Obj_Nam := Nod;
905 while Present (Obj_Nam) loop
906 case Nkind (Obj_Nam) is
907 when N_Attribute_Reference
908 | N_Explicit_Dereference
909 | N_Indexed_Component
910 | N_Slice
911 =>
912 Obj_Nam := Prefix (Obj_Nam);
913
914 when N_Selected_Component =>
915 Obj_Nam := Selector_Name (Obj_Nam);
916
917 when N_Qualified_Expression | N_Type_Conversion =>
918 Obj_Nam := Expression (Obj_Nam);
919
920 when others =>
921 exit;
922 end case;
923 end loop;
924
925 return Obj_Nam;
926 end Get_Object_Name;
927
928 -- Start of processing for Analyze_Object_Renaming
929
930 begin
931 if Nam = Error then
932 return;
933 end if;
934
935 Set_Is_Pure (Id, Is_Pure (Current_Scope));
936 Enter_Name (Id);
937
938 -- The renaming of a component that depends on a discriminant requires
939 -- an actual subtype, because in subsequent use of the object Gigi will
940 -- be unable to locate the actual bounds. This explicit step is required
941 -- when the renaming is generated in removing side effects of an
942 -- already-analyzed expression.
943
944 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
945
946 -- The object renaming declaration may become Ghost if it renames a
947 -- Ghost entity.
948
949 if Is_Entity_Name (Nam) then
950 Mark_Ghost_Renaming (N, Entity (Nam));
951 end if;
952
953 T := Etype (Nam);
954 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
955
956 if Present (Dec) then
957 Insert_Action (N, Dec);
958 T := Defining_Identifier (Dec);
959 Set_Etype (Nam, T);
960 end if;
961 elsif Present (Subtype_Mark (N))
962 or else not Present (Access_Definition (N))
963 then
964 if Present (Subtype_Mark (N)) then
965 Find_Type (Subtype_Mark (N));
966 T := Entity (Subtype_Mark (N));
967 Analyze (Nam);
968
969 -- AI12-0275: Case of object renaming without a subtype_mark
970
971 else
972 Analyze (Nam);
973
974 -- Normal case of no overloading in object name
975
976 if not Is_Overloaded (Nam) then
977
978 -- Catch error cases (such as attempting to rename a procedure
979 -- or package) using the shorthand form.
980
981 if No (Etype (Nam))
982 or else Etype (Nam) = Standard_Void_Type
983 then
984 Error_Msg_N
985 ("object name or value expected in renaming", Nam);
986
987 Mutate_Ekind (Id, E_Variable);
988 Set_Etype (Id, Any_Type);
989
990 return;
991
992 else
993 T := Etype (Nam);
994 end if;
995
996 -- Case of overloaded name, which will be illegal if there's more
997 -- than one acceptable interpretation (such as overloaded function
998 -- calls).
999
1000 else
1001 declare
1002 I : Interp_Index;
1003 I1 : Interp_Index;
1004 It : Interp;
1005 It1 : Interp;
1006 Nam1 : Entity_Id;
1007
1008 begin
1009 -- More than one candidate interpretation is available
1010
1011 -- Remove procedure calls, which syntactically cannot appear
1012 -- in this context, but which cannot be removed by type
1013 -- checking, because the context does not impose a type.
1014
1015 Get_First_Interp (Nam, I, It);
1016 while Present (It.Typ) loop
1017 if It.Typ = Standard_Void_Type then
1018 Remove_Interp (I);
1019 end if;
1020
1021 Get_Next_Interp (I, It);
1022 end loop;
1023
1024 Get_First_Interp (Nam, I, It);
1025 I1 := I;
1026 It1 := It;
1027
1028 -- If there's no type present, we have an error case (such
1029 -- as overloaded procedures named in the object renaming).
1030
1031 if No (It.Typ) then
1032 Error_Msg_N
1033 ("object name or value expected in renaming", Nam);
1034
1035 Mutate_Ekind (Id, E_Variable);
1036 Set_Etype (Id, Any_Type);
1037
1038 return;
1039 end if;
1040
1041 Get_Next_Interp (I, It);
1042
1043 if Present (It.Typ) then
1044 Nam1 := It1.Nam;
1045 It1 := Disambiguate (Nam, I1, I, Any_Type);
1046
1047 if It1 = No_Interp then
1048 Error_Msg_N ("ambiguous name in object renaming", Nam);
1049
1050 Error_Msg_Sloc := Sloc (It.Nam);
1051 Error_Msg_N ("\\possible interpretation#!", Nam);
1052
1053 Error_Msg_Sloc := Sloc (Nam1);
1054 Error_Msg_N ("\\possible interpretation#!", Nam);
1055
1056 return;
1057 end if;
1058 end if;
1059
1060 Set_Etype (Nam, It1.Typ);
1061 T := It1.Typ;
1062 end;
1063 end if;
1064
1065 if Etype (Nam) = Standard_Exception_Type then
1066 Error_Msg_N
1067 ("exception requires a subtype mark in renaming", Nam);
1068 return;
1069 end if;
1070 end if;
1071
1072 -- The object renaming declaration may become Ghost if it renames a
1073 -- Ghost entity.
1074
1075 if Is_Entity_Name (Nam) then
1076 Mark_Ghost_Renaming (N, Entity (Nam));
1077 end if;
1078
1079 -- Check against AI12-0401 here before Resolve may rewrite Nam and
1080 -- potentially generate spurious warnings.
1081
1082 -- In the case where the object_name is a qualified_expression with
1083 -- a nominal subtype T and whose expression is a name that denotes
1084 -- an object Q:
1085 -- * if T is an elementary subtype, then:
1086 -- * Q shall be a constant other than a dereference of an access
1087 -- type; or
1088 -- * the nominal subtype of Q shall be statically compatible with
1089 -- T; or
1090 -- * T shall statically match the base subtype of its type if
1091 -- scalar, or the first subtype of its type if an access type.
1092 -- * if T is a composite subtype, then Q shall be known to be
1093 -- constrained or T shall statically match the first subtype of
1094 -- its type.
1095
1096 if Nkind (Nam) = N_Qualified_Expression
1097 and then Is_Object_Reference (Expression (Nam))
1098 then
1099 Q := Expression (Nam);
1100
1101 if (Is_Elementary_Type (T)
1102 and then
1103 not ((not Is_Variable (Q)
1104 and then Nkind (Q) /= N_Explicit_Dereference)
1105 or else Subtypes_Statically_Compatible (Etype (Q), T)
1106 or else (Is_Scalar_Type (T)
1107 and then Subtypes_Statically_Match
1108 (T, Base_Type (T)))
1109 or else (Is_Access_Type (T)
1110 and then Subtypes_Statically_Match
1111 (T, First_Subtype (T)))))
1112 or else (Is_Composite_Type (T)
1113 and then
1114
1115 -- If Q is an aggregate, Is_Constrained may not be set
1116 -- yet and its type may not be resolved yet.
1117 -- This doesn't quite correspond to the complex notion
1118 -- of "known to be constrained" but this is good enough
1119 -- for a rule which is in any case too complex.
1120
1121 not (Is_Constrained (Etype (Q))
1122 or else Nkind (Q) = N_Aggregate
1123 or else Subtypes_Statically_Match
1124 (T, First_Subtype (T))))
1125 then
1126 Error_Msg_N
1127 ("subtype of renamed qualified expression does not " &
1128 "statically match", N);
1129 return;
1130 end if;
1131 end if;
1132
1133 Resolve (Nam, T);
1134
1135 -- If the renamed object is a function call of a limited type,
1136 -- the expansion of the renaming is complicated by the presence
1137 -- of various temporaries and subtypes that capture constraints
1138 -- of the renamed object. Rewrite node as an object declaration,
1139 -- whose expansion is simpler. Given that the object is limited
1140 -- there is no copy involved and no performance hit.
1141
1142 if Nkind (Nam) = N_Function_Call
1143 and then Is_Limited_View (Etype (Nam))
1144 and then not Is_Constrained (Etype (Nam))
1145 and then Comes_From_Source (N)
1146 then
1147 Set_Etype (Id, T);
1148 Mutate_Ekind (Id, E_Constant);
1149 Rewrite (N,
1150 Make_Object_Declaration (Loc,
1151 Defining_Identifier => Id,
1152 Constant_Present => True,
1153 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
1154 Expression => Relocate_Node (Nam)));
1155 return;
1156 end if;
1157
1158 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
1159 -- when renaming declaration has a named access type. The Ada 2012
1160 -- coverage rules allow an anonymous access type in the context of
1161 -- an expected named general access type, but the renaming rules
1162 -- require the types to be the same. (An exception is when the type
1163 -- of the renaming is also an anonymous access type, which can only
1164 -- happen due to a renaming created by the expander.)
1165
1166 if Nkind (Nam) = N_Type_Conversion
1167 and then not Comes_From_Source (Nam)
1168 and then Is_Anonymous_Access_Type (Etype (Expression (Nam)))
1169 and then not Is_Anonymous_Access_Type (T)
1170 then
1171 Wrong_Type (Expression (Nam), T); -- Should we give better error???
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, Has_Out_Or_In_Out_Parameter);
3282 Reinit_Field_To_Zero (New_S, 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
5318 -- In gnat internal mode, we consider all entities known. The
5319 -- historical reason behind this discrepancy is not known??? But the
5320 -- only effect is to modify the error message given, so it is not
5321 -- critical. Since it only affects the exact wording of error
5322 -- messages in illegal programs, we do not mention this as an
5323 -- effect of -gnatg, since it is not a language modification.
5324
5325 elsif GNAT_Mode then
5326 return True;
5327 end if;
5328
5329 -- Here we have an entity that is not from package Standard, and
5330 -- which comes from Source. See if it comes from an internal file.
5331
5332 Fname := Unit_File_Name (Get_Source_Unit (E));
5333
5334 -- Case of from internal file
5335
5336 if In_Internal_Unit (E) then
5337
5338 -- Private part entities in internal files are never considered
5339 -- to be known to the writer of normal application code.
5340
5341 if Is_Hidden (E) then
5342 return False;
5343 end if;
5344
5345 -- Entities from System packages other than System and
5346 -- System.Storage_Elements are not considered to be known.
5347 -- System.Auxxxx files are also considered known to the user.
5348
5349 -- Should refine this at some point to generally distinguish
5350 -- between known and unknown internal files ???
5351
5352 Get_Name_String (Fname);
5353
5354 return
5355 Name_Len < 2
5356 or else
5357 Name_Buffer (1 .. 2) /= "s-"
5358 or else
5359 Name_Buffer (3 .. 8) = "stoele"
5360 or else
5361 Name_Buffer (3 .. 5) = "aux";
5362
5363 -- If not an internal file, then entity is definitely known, even if
5364 -- it is in a private part (the message generated will note that it
5365 -- is in a private part).
5366
5367 else
5368 return True;
5369 end if;
5370 end Known_But_Invisible;
5371
5372 -------------------
5373 -- Nvis_Messages --
5374 -------------------
5375
5376 procedure Nvis_Messages is
5377 Comp_Unit : Node_Id;
5378 Ent : Entity_Id;
5379 Found : Boolean := False;
5380 Hidden : Boolean := False;
5381 Item : Node_Id;
5382
5383 begin
5384 -- Ada 2005 (AI-262): Generate a precise error concerning the
5385 -- Beaujolais effect that was previously detected
5386
5387 if Nvis_Is_Private_Subprg then
5388
5389 pragma Assert (Nkind (E2) = N_Defining_Identifier
5390 and then Ekind (E2) = E_Function
5391 and then Scope (E2) = Standard_Standard
5392 and then Has_Private_With (E2));
5393
5394 -- Find the sloc corresponding to the private with'ed unit
5395
5396 Comp_Unit := Cunit (Current_Sem_Unit);
5397 Error_Msg_Sloc := No_Location;
5398
5399 Item := First (Context_Items (Comp_Unit));
5400 while Present (Item) loop
5401 if Nkind (Item) = N_With_Clause
5402 and then Private_Present (Item)
5403 and then Entity (Name (Item)) = E2
5404 then
5405 Error_Msg_Sloc := Sloc (Item);
5406 exit;
5407 end if;
5408
5409 Next (Item);
5410 end loop;
5411
5412 pragma Assert (Error_Msg_Sloc /= No_Location);
5413
5414 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
5415 return;
5416 end if;
5417
5418 Undefined (Nvis => True);
5419
5420 if Msg then
5421
5422 -- First loop does hidden declarations
5423
5424 Ent := Homonyms;
5425 while Present (Ent) loop
5426 if Is_Potentially_Use_Visible (Ent) then
5427 if not Hidden then
5428 Error_Msg_N -- CODEFIX
5429 ("multiple use clauses cause hiding!", N);
5430 Hidden := True;
5431 end if;
5432
5433 Error_Msg_Sloc := Sloc (Ent);
5434 Error_Msg_N -- CODEFIX
5435 ("hidden declaration#!", N);
5436 end if;
5437
5438 Ent := Homonym (Ent);
5439 end loop;
5440
5441 -- If we found hidden declarations, then that's enough, don't
5442 -- bother looking for non-visible declarations as well.
5443
5444 if Hidden then
5445 return;
5446 end if;
5447
5448 -- Second loop does non-directly visible declarations
5449
5450 Ent := Homonyms;
5451 while Present (Ent) loop
5452 if not Is_Potentially_Use_Visible (Ent) then
5453
5454 -- Do not bother the user with unknown entities
5455
5456 if not Known_But_Invisible (Ent) then
5457 goto Continue;
5458 end if;
5459
5460 Error_Msg_Sloc := Sloc (Ent);
5461
5462 -- Output message noting that there is a non-visible
5463 -- declaration, distinguishing the private part case.
5464
5465 if Is_Hidden (Ent) then
5466 Error_Msg_N ("non-visible (private) declaration#!", N);
5467
5468 -- If the entity is declared in a generic package, it
5469 -- cannot be visible, so there is no point in adding it
5470 -- to the list of candidates if another homograph from a
5471 -- non-generic package has been seen.
5472
5473 elsif Ekind (Scope (Ent)) = E_Generic_Package
5474 and then Found
5475 then
5476 null;
5477
5478 else
5479 Error_Msg_N -- CODEFIX
5480 ("non-visible declaration#!", N);
5481
5482 if Ekind (Scope (Ent)) /= E_Generic_Package then
5483 Found := True;
5484 end if;
5485
5486 if Is_Compilation_Unit (Ent)
5487 and then
5488 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5489 then
5490 Error_Msg_Qual_Level := 99;
5491 Error_Msg_NE -- CODEFIX
5492 ("\\missing `WITH &;`", N, Ent);
5493 Error_Msg_Qual_Level := 0;
5494 end if;
5495
5496 if Ekind (Ent) = E_Discriminant
5497 and then Present (Corresponding_Discriminant (Ent))
5498 and then Scope (Corresponding_Discriminant (Ent)) =
5499 Etype (Scope (Ent))
5500 then
5501 Error_Msg_N
5502 ("inherited discriminant not allowed here" &
5503 " (RM 3.8 (12), 3.8.1 (6))!", N);
5504 end if;
5505 end if;
5506
5507 -- Set entity and its containing package as referenced. We
5508 -- can't be sure of this, but this seems a better choice
5509 -- to avoid unused entity messages.
5510
5511 if Comes_From_Source (Ent) then
5512 Set_Referenced (Ent);
5513 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5514 end if;
5515 end if;
5516
5517 <<Continue>>
5518 Ent := Homonym (Ent);
5519 end loop;
5520 end if;
5521 end Nvis_Messages;
5522
5523 ---------------
5524 -- Undefined --
5525 ---------------
5526
5527 procedure Undefined (Nvis : Boolean) is
5528 Emsg : Error_Msg_Id;
5529
5530 begin
5531 -- We should never find an undefined internal name. If we do, then
5532 -- see if we have previous errors. If so, ignore on the grounds that
5533 -- it is probably a cascaded message (e.g. a block label from a badly
5534 -- formed block). If no previous errors, then we have a real internal
5535 -- error of some kind so raise an exception.
5536
5537 if Is_Internal_Name (Chars (N)) then
5538 if Total_Errors_Detected /= 0 then
5539 return;
5540 else
5541 raise Program_Error;
5542 end if;
5543 end if;
5544
5545 -- A very specialized error check, if the undefined variable is
5546 -- a case tag, and the case type is an enumeration type, check
5547 -- for a possible misspelling, and if so, modify the identifier
5548
5549 -- Named aggregate should also be handled similarly ???
5550
5551 if Nkind (N) = N_Identifier
5552 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5553 then
5554 declare
5555 Case_Stm : constant Node_Id := Parent (Parent (N));
5556 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5557
5558 Lit : Node_Id;
5559
5560 begin
5561 if Is_Enumeration_Type (Case_Typ)
5562 and then not Is_Standard_Character_Type (Case_Typ)
5563 then
5564 Lit := First_Literal (Case_Typ);
5565 Get_Name_String (Chars (Lit));
5566
5567 if Chars (Lit) /= Chars (N)
5568 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5569 then
5570 Error_Msg_Node_2 := Lit;
5571 Error_Msg_N -- CODEFIX
5572 ("& is undefined, assume misspelling of &", N);
5573 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5574 return;
5575 end if;
5576
5577 Next_Literal (Lit);
5578 end if;
5579 end;
5580 end if;
5581
5582 -- Normal processing
5583
5584 Set_Entity (N, Any_Id);
5585 Set_Etype (N, Any_Type);
5586
5587 -- We use the table Urefs to keep track of entities for which we
5588 -- have issued errors for undefined references. Multiple errors
5589 -- for a single name are normally suppressed, however we modify
5590 -- the error message to alert the programmer to this effect.
5591
5592 for J in Urefs.First .. Urefs.Last loop
5593 if Chars (N) = Chars (Urefs.Table (J).Node) then
5594 if Urefs.Table (J).Err /= No_Error_Msg
5595 and then Sloc (N) /= Urefs.Table (J).Loc
5596 then
5597 Error_Msg_Node_1 := Urefs.Table (J).Node;
5598
5599 if Urefs.Table (J).Nvis then
5600 Change_Error_Text (Urefs.Table (J).Err,
5601 "& is not visible (more references follow)");
5602 else
5603 Change_Error_Text (Urefs.Table (J).Err,
5604 "& is undefined (more references follow)");
5605 end if;
5606
5607 Urefs.Table (J).Err := No_Error_Msg;
5608 end if;
5609
5610 -- Although we will set Msg False, and thus suppress the
5611 -- message, we also set Error_Posted True, to avoid any
5612 -- cascaded messages resulting from the undefined reference.
5613
5614 Msg := False;
5615 Set_Error_Posted (N);
5616 return;
5617 end if;
5618 end loop;
5619
5620 -- If entry not found, this is first undefined occurrence
5621
5622 if Nvis then
5623 Error_Msg_N ("& is not visible!", N);
5624 Emsg := Get_Msg_Id;
5625
5626 else
5627 Error_Msg_N ("& is undefined!", N);
5628 Emsg := Get_Msg_Id;
5629
5630 -- A very bizarre special check, if the undefined identifier
5631 -- is Put or Put_Line, then add a special error message (since
5632 -- this is a very common error for beginners to make).
5633
5634 if Chars (N) in Name_Put | Name_Put_Line then
5635 Error_Msg_N -- CODEFIX
5636 ("\\possible missing `WITH Ada.Text_'I'O; " &
5637 "USE Ada.Text_'I'O`!", N);
5638
5639 -- Another special check if N is the prefix of a selected
5640 -- component which is a known unit: add message complaining
5641 -- about missing with for this unit.
5642
5643 elsif Nkind (Parent (N)) = N_Selected_Component
5644 and then N = Prefix (Parent (N))
5645 and then Is_Known_Unit (Parent (N))
5646 then
5647 declare
5648 P : Node_Id := Parent (N);
5649 begin
5650 Error_Msg_Name_1 := Chars (N);
5651 Error_Msg_Name_2 := Chars (Selector_Name (P));
5652
5653 if Nkind (Parent (P)) = N_Selected_Component
5654 and then Is_Known_Unit (Parent (P))
5655 then
5656 P := Parent (P);
5657 Error_Msg_Name_3 := Chars (Selector_Name (P));
5658 Error_Msg_N -- CODEFIX
5659 ("\\missing `WITH %.%.%;`", N);
5660
5661 else
5662 Error_Msg_N -- CODEFIX
5663 ("\\missing `WITH %.%;`", N);
5664 end if;
5665 end;
5666 end if;
5667
5668 -- Now check for possible misspellings
5669
5670 declare
5671 E : Entity_Id;
5672 Ematch : Entity_Id := Empty;
5673 begin
5674 for Nam in First_Name_Id .. Last_Name_Id loop
5675 E := Get_Name_Entity_Id (Nam);
5676
5677 if Present (E)
5678 and then (Is_Immediately_Visible (E)
5679 or else
5680 Is_Potentially_Use_Visible (E))
5681 then
5682 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5683 Ematch := E;
5684 exit;
5685 end if;
5686 end if;
5687 end loop;
5688
5689 if Present (Ematch) then
5690 Error_Msg_NE -- CODEFIX
5691 ("\possible misspelling of&", N, Ematch);
5692 end if;
5693 end;
5694 end if;
5695
5696 -- Make entry in undefined references table unless the full errors
5697 -- switch is set, in which case by refraining from generating the
5698 -- table entry we guarantee that we get an error message for every
5699 -- undefined reference. The entry is not added if we are ignoring
5700 -- errors.
5701
5702 if not All_Errors_Mode
5703 and then Ignore_Errors_Enable = 0
5704 and then not Get_Ignore_Errors
5705 then
5706 Urefs.Append (
5707 (Node => N,
5708 Err => Emsg,
5709 Nvis => Nvis,
5710 Loc => Sloc (N)));
5711 end if;
5712
5713 Msg := True;
5714 end Undefined;
5715
5716 -- Local variables
5717
5718 Nested_Inst : Entity_Id := Empty;
5719 -- The entity of a nested instance which appears within Inst (if any)
5720
5721 -- Start of processing for Find_Direct_Name
5722
5723 begin
5724 -- If the entity pointer is already set, this is an internal node, or
5725 -- a node that is analyzed more than once, after a tree modification.
5726 -- In such a case there is no resolution to perform, just set the type.
5727
5728 if Present (Entity (N)) then
5729 if Is_Type (Entity (N)) then
5730 Set_Etype (N, Entity (N));
5731
5732 -- The exception to this general rule are constants associated with
5733 -- discriminals of protected types because for each protected op
5734 -- a new set of discriminals is internally created by the frontend
5735 -- (see Exp_Ch9.Set_Discriminals), and the current decoration of the
5736 -- entity pointer may have been set as part of a preanalysis, where
5737 -- discriminals still reference the first subprogram or entry to be
5738 -- expanded (see Expand_Protected_Body_Declarations).
5739
5740 elsif Full_Analysis
5741 and then Ekind (Entity (N)) = E_Constant
5742 and then Present (Discriminal_Link (Entity (N)))
5743 and then Is_Protected_Type (Scope (Discriminal_Link (Entity (N))))
5744 then
5745 goto Find_Name;
5746
5747 else
5748 declare
5749 Entyp : constant Entity_Id := Etype (Entity (N));
5750
5751 begin
5752 -- One special case here. If the Etype field is already set,
5753 -- and references the packed array type corresponding to the
5754 -- etype of the referenced entity, then leave it alone. This
5755 -- happens for trees generated from Exp_Pakd, where expressions
5756 -- can be deliberately "mis-typed" to the packed array type.
5757
5758 if Is_Packed_Array (Entyp)
5759 and then Present (Etype (N))
5760 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5761 then
5762 null;
5763
5764 -- If not that special case, then just reset the Etype
5765
5766 else
5767 Set_Etype (N, Etype (Entity (N)));
5768 end if;
5769 end;
5770 end if;
5771
5772 -- Although the marking of use clauses happens at the end of
5773 -- Find_Direct_Name, a certain case where a generic actual satisfies
5774 -- a use clause must be checked here due to how the generic machinery
5775 -- handles the analysis of said actuals.
5776
5777 if In_Instance
5778 and then Nkind (Parent (N)) = N_Generic_Association
5779 then
5780 Mark_Use_Clauses (Entity (N));
5781 end if;
5782
5783 return;
5784 end if;
5785
5786 <<Find_Name>>
5787
5788 -- Preserve relevant elaboration-related attributes of the context which
5789 -- are no longer available or very expensive to recompute once analysis,
5790 -- resolution, and expansion are over.
5791
5792 if Nkind (N) = N_Identifier then
5793 Mark_Elaboration_Attributes
5794 (N_Id => N,
5795 Checks => True,
5796 Modes => True,
5797 Warnings => True);
5798 end if;
5799
5800 -- Here if Entity pointer was not set, we need full visibility analysis
5801 -- First we generate debugging output if the debug E flag is set.
5802
5803 if Debug_Flag_E then
5804 Write_Str ("Looking for ");
5805 Write_Name (Chars (N));
5806 Write_Eol;
5807 end if;
5808
5809 Homonyms := Current_Entity (N);
5810 Nvis_Entity := False;
5811
5812 E := Homonyms;
5813 while Present (E) loop
5814
5815 -- If entity is immediately visible or potentially use visible, then
5816 -- process the entity and we are done.
5817
5818 if Is_Immediately_Visible (E) then
5819 goto Immediately_Visible_Entity;
5820
5821 elsif Is_Potentially_Use_Visible (E) then
5822 goto Potentially_Use_Visible_Entity;
5823
5824 -- Note if a known but invisible entity encountered
5825
5826 elsif Known_But_Invisible (E) then
5827 Nvis_Entity := True;
5828 end if;
5829
5830 -- Move to next entity in chain and continue search
5831
5832 E := Homonym (E);
5833 end loop;
5834
5835 -- If no entries on homonym chain that were potentially visible,
5836 -- and no entities reasonably considered as non-visible, then
5837 -- we have a plain undefined reference, with no additional
5838 -- explanation required.
5839
5840 if not Nvis_Entity then
5841 Undefined (Nvis => False);
5842
5843 -- Otherwise there is at least one entry on the homonym chain that
5844 -- is reasonably considered as being known and non-visible.
5845
5846 else
5847 Nvis_Messages;
5848 end if;
5849
5850 goto Done;
5851
5852 -- Processing for a potentially use visible entry found. We must search
5853 -- the rest of the homonym chain for two reasons. First, if there is a
5854 -- directly visible entry, then none of the potentially use-visible
5855 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5856 -- for the case of multiple potentially use-visible entries hiding one
5857 -- another and as a result being non-directly visible (RM 8.4(11)).
5858
5859 <<Potentially_Use_Visible_Entity>> declare
5860 Only_One_Visible : Boolean := True;
5861 All_Overloadable : Boolean := Is_Overloadable (E);
5862
5863 begin
5864 E2 := Homonym (E);
5865 while Present (E2) loop
5866 if Is_Immediately_Visible (E2) then
5867
5868 -- If the use-visible entity comes from the actual for a
5869 -- formal package, it hides a directly visible entity from
5870 -- outside the instance.
5871
5872 if From_Actual_Package (E)
5873 and then Scope_Depth (Scope (E2)) < Scope_Depth (Inst)
5874 then
5875 goto Found;
5876 else
5877 E := E2;
5878 goto Immediately_Visible_Entity;
5879 end if;
5880
5881 elsif Is_Potentially_Use_Visible (E2) then
5882 Only_One_Visible := False;
5883 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5884
5885 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5886 -- that can occur in private_with clauses. Example:
5887
5888 -- with A;
5889 -- private with B; package A is
5890 -- package C is function B return Integer;
5891 -- use A; end A;
5892 -- V1 : Integer := B;
5893 -- private function B return Integer;
5894 -- V2 : Integer := B;
5895 -- end C;
5896
5897 -- V1 resolves to A.B, but V2 resolves to library unit B
5898
5899 elsif Ekind (E2) = E_Function
5900 and then Scope (E2) = Standard_Standard
5901 and then Has_Private_With (E2)
5902 then
5903 Only_One_Visible := False;
5904 All_Overloadable := False;
5905 Nvis_Is_Private_Subprg := True;
5906 exit;
5907 end if;
5908
5909 E2 := Homonym (E2);
5910 end loop;
5911
5912 -- On falling through this loop, we have checked that there are no
5913 -- immediately visible entities. Only_One_Visible is set if exactly
5914 -- one potentially use visible entity exists. All_Overloadable is
5915 -- set if all the potentially use visible entities are overloadable.
5916 -- The condition for legality is that either there is one potentially
5917 -- use visible entity, or if there is more than one, then all of them
5918 -- are overloadable.
5919
5920 if Only_One_Visible or All_Overloadable then
5921 goto Found;
5922
5923 -- If there is more than one potentially use-visible entity and at
5924 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5925 -- Note that E points to the first such entity on the homonym list.
5926
5927 else
5928 -- If one of the entities is declared in an actual package, it
5929 -- was visible in the generic, and takes precedence over other
5930 -- entities that are potentially use-visible. The same applies
5931 -- if the entity is declared in a local instantiation of the
5932 -- current instance.
5933
5934 if In_Instance then
5935
5936 -- Find the current instance
5937
5938 Inst := Current_Scope;
5939 while Present (Inst) and then Inst /= Standard_Standard loop
5940 if Is_Generic_Instance (Inst) then
5941 exit;
5942 end if;
5943
5944 Inst := Scope (Inst);
5945 end loop;
5946
5947 -- Reexamine the candidate entities, giving priority to those
5948 -- that were visible within the generic.
5949
5950 E2 := E;
5951 while Present (E2) loop
5952 Nested_Inst := Nearest_Enclosing_Instance (E2);
5953
5954 -- The entity is declared within an actual package, or in a
5955 -- nested instance. The ">=" accounts for the case where the
5956 -- current instance and the nested instance are the same.
5957
5958 if From_Actual_Package (E2)
5959 or else (Present (Nested_Inst)
5960 and then Scope_Depth (Nested_Inst) >=
5961 Scope_Depth (Inst))
5962 then
5963 E := E2;
5964 goto Found;
5965 end if;
5966
5967 E2 := Homonym (E2);
5968 end loop;
5969
5970 Nvis_Messages;
5971 goto Done;
5972
5973 elsif Is_Predefined_Unit (Current_Sem_Unit) then
5974 -- A use clause in the body of a system file creates conflict
5975 -- with some entity in a user scope, while rtsfind is active.
5976 -- Keep only the entity coming from another predefined unit.
5977
5978 E2 := E;
5979 while Present (E2) loop
5980 if In_Predefined_Unit (E2) then
5981 E := E2;
5982 goto Found;
5983 end if;
5984
5985 E2 := Homonym (E2);
5986 end loop;
5987
5988 -- Entity must exist because predefined unit is correct
5989
5990 raise Program_Error;
5991
5992 else
5993 Nvis_Messages;
5994 goto Done;
5995 end if;
5996 end if;
5997 end;
5998
5999 -- Come here with E set to the first immediately visible entity on
6000 -- the homonym chain. This is the one we want unless there is another
6001 -- immediately visible entity further on in the chain for an inner
6002 -- scope (RM 8.3(8)).
6003
6004 <<Immediately_Visible_Entity>> declare
6005 Level : Int;
6006 Scop : Entity_Id;
6007
6008 begin
6009 -- Find scope level of initial entity. When compiling through
6010 -- Rtsfind, the previous context is not completely invisible, and
6011 -- an outer entity may appear on the chain, whose scope is below
6012 -- the entry for Standard that delimits the current scope stack.
6013 -- Indicate that the level for this spurious entry is outside of
6014 -- the current scope stack.
6015
6016 Level := Scope_Stack.Last;
6017 loop
6018 Scop := Scope_Stack.Table (Level).Entity;
6019 exit when Scop = Scope (E);
6020 Level := Level - 1;
6021 exit when Scop = Standard_Standard;
6022 end loop;
6023
6024 -- Now search remainder of homonym chain for more inner entry
6025 -- If the entity is Standard itself, it has no scope, and we
6026 -- compare it with the stack entry directly.
6027
6028 E2 := Homonym (E);
6029 while Present (E2) loop
6030 if Is_Immediately_Visible (E2) then
6031
6032 -- If a generic package contains a local declaration that
6033 -- has the same name as the generic, there may be a visibility
6034 -- conflict in an instance, where the local declaration must
6035 -- also hide the name of the corresponding package renaming.
6036 -- We check explicitly for a package declared by a renaming,
6037 -- whose renamed entity is an instance that is on the scope
6038 -- stack, and that contains a homonym in the same scope. Once
6039 -- we have found it, we know that the package renaming is not
6040 -- immediately visible, and that the identifier denotes the
6041 -- other entity (and its homonyms if overloaded).
6042
6043 if Scope (E) = Scope (E2)
6044 and then Ekind (E) = E_Package
6045 and then Present (Renamed_Object (E))
6046 and then Is_Generic_Instance (Renamed_Object (E))
6047 and then In_Open_Scopes (Renamed_Object (E))
6048 and then Comes_From_Source (N)
6049 then
6050 Set_Is_Immediately_Visible (E, False);
6051 E := E2;
6052
6053 else
6054 for J in Level + 1 .. Scope_Stack.Last loop
6055 if Scope_Stack.Table (J).Entity = Scope (E2)
6056 or else Scope_Stack.Table (J).Entity = E2
6057 then
6058 Level := J;
6059 E := E2;
6060 exit;
6061 end if;
6062 end loop;
6063 end if;
6064 end if;
6065
6066 E2 := Homonym (E2);
6067 end loop;
6068
6069 -- At the end of that loop, E is the innermost immediately
6070 -- visible entity, so we are all set.
6071 end;
6072
6073 -- Come here with entity found, and stored in E
6074
6075 <<Found>> begin
6076
6077 -- Check violation of No_Wide_Characters restriction
6078
6079 Check_Wide_Character_Restriction (E, N);
6080
6081 -- When distribution features are available (Get_PCS_Name /=
6082 -- Name_No_DSA), a remote access-to-subprogram type is converted
6083 -- into a record type holding whatever information is needed to
6084 -- perform a remote call on an RCI subprogram. In that case we
6085 -- rewrite any occurrence of the RAS type into the equivalent record
6086 -- type here. 'Access attribute references and RAS dereferences are
6087 -- then implemented using specific TSSs. However when distribution is
6088 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
6089 -- generation of these TSSs, and we must keep the RAS type in its
6090 -- original access-to-subprogram form (since all calls through a
6091 -- value of such type will be local anyway in the absence of a PCS).
6092
6093 if Comes_From_Source (N)
6094 and then Is_Remote_Access_To_Subprogram_Type (E)
6095 and then Ekind (E) = E_Access_Subprogram_Type
6096 and then Expander_Active
6097 and then Get_PCS_Name /= Name_No_DSA
6098 then
6099 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
6100 goto Done;
6101 end if;
6102
6103 -- Set the entity. Note that the reason we call Set_Entity for the
6104 -- overloadable case, as opposed to Set_Entity_With_Checks is
6105 -- that in the overloaded case, the initial call can set the wrong
6106 -- homonym. The call that sets the right homonym is in Sem_Res and
6107 -- that call does use Set_Entity_With_Checks, so we don't miss
6108 -- a style check.
6109
6110 if Is_Overloadable (E) then
6111 Set_Entity (N, E);
6112 else
6113 Set_Entity_With_Checks (N, E);
6114 end if;
6115
6116 if Is_Type (E) then
6117 Set_Etype (N, E);
6118 else
6119 Set_Etype (N, Get_Full_View (Etype (E)));
6120 end if;
6121
6122 if Debug_Flag_E then
6123 Write_Str (" found ");
6124 Write_Entity_Info (E, " ");
6125 end if;
6126
6127 -- If the Ekind of the entity is Void, it means that all homonyms
6128 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
6129 -- test is skipped if the current scope is a record and the name is
6130 -- a pragma argument expression (case of Atomic and Volatile pragmas
6131 -- and possibly other similar pragmas added later, which are allowed
6132 -- to reference components in the current record).
6133
6134 if Ekind (E) = E_Void
6135 and then
6136 (not Is_Record_Type (Current_Scope)
6137 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
6138 then
6139 Premature_Usage (N);
6140
6141 -- If the entity is overloadable, collect all interpretations of the
6142 -- name for subsequent overload resolution. We optimize a bit here to
6143 -- do this only if we have an overloadable entity that is not on its
6144 -- own on the homonym chain.
6145
6146 elsif Is_Overloadable (E)
6147 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
6148 then
6149 Collect_Interps (N);
6150
6151 -- If no homonyms were visible, the entity is unambiguous
6152
6153 if not Is_Overloaded (N) then
6154 if not Is_Actual_Parameter then
6155 Generate_Reference (E, N);
6156 end if;
6157 end if;
6158
6159 -- Case of non-overloadable entity, set the entity providing that
6160 -- we do not have the case of a discriminant reference within a
6161 -- default expression. Such references are replaced with the
6162 -- corresponding discriminal, which is the formal corresponding to
6163 -- to the discriminant in the initialization procedure.
6164
6165 else
6166 -- Entity is unambiguous, indicate that it is referenced here
6167
6168 -- For a renaming of an object, always generate simple reference,
6169 -- we don't try to keep track of assignments in this case, except
6170 -- in SPARK mode where renamings are traversed for generating
6171 -- local effects of subprograms.
6172
6173 if Is_Object (E)
6174 and then Present (Renamed_Object (E))
6175 and then not GNATprove_Mode
6176 then
6177 Generate_Reference (E, N);
6178
6179 -- If the renamed entity is a private protected component,
6180 -- reference the original component as well. This needs to be
6181 -- done because the private renamings are installed before any
6182 -- analysis has occurred. Reference to a private component will
6183 -- resolve to the renaming and the original component will be
6184 -- left unreferenced, hence the following.
6185
6186 if Is_Prival (E) then
6187 Generate_Reference (Prival_Link (E), N);
6188 end if;
6189
6190 -- One odd case is that we do not want to set the Referenced flag
6191 -- if the entity is a label, and the identifier is the label in
6192 -- the source, since this is not a reference from the point of
6193 -- view of the user.
6194
6195 elsif Nkind (Parent (N)) = N_Label then
6196 declare
6197 R : constant Boolean := Referenced (E);
6198
6199 begin
6200 -- Generate reference unless this is an actual parameter
6201 -- (see comment below).
6202
6203 if not Is_Actual_Parameter then
6204 Generate_Reference (E, N);
6205 Set_Referenced (E, R);
6206 end if;
6207 end;
6208
6209 -- Normal case, not a label: generate reference
6210
6211 else
6212 if not Is_Actual_Parameter then
6213
6214 -- Package or generic package is always a simple reference
6215
6216 if Is_Package_Or_Generic_Package (E) then
6217 Generate_Reference (E, N, 'r');
6218
6219 -- Else see if we have a left hand side
6220
6221 else
6222 case Is_LHS (N) is
6223 when Yes =>
6224 Generate_Reference (E, N, 'm');
6225
6226 when No =>
6227 Generate_Reference (E, N, 'r');
6228
6229 -- If we don't know now, generate reference later
6230
6231 when Unknown =>
6232 Defer_Reference ((E, N));
6233 end case;
6234 end if;
6235 end if;
6236 end if;
6237
6238 Set_Entity_Or_Discriminal (N, E);
6239
6240 -- The name may designate a generalized reference, in which case
6241 -- the dereference interpretation will be included. Context is
6242 -- one in which a name is legal.
6243
6244 if Ada_Version >= Ada_2012
6245 and then
6246 (Nkind (Parent (N)) in N_Subexpr
6247 or else Nkind (Parent (N)) in N_Assignment_Statement
6248 | N_Object_Declaration
6249 | N_Parameter_Association)
6250 then
6251 Check_Implicit_Dereference (N, Etype (E));
6252 end if;
6253 end if;
6254 end;
6255
6256 -- Mark relevant use-type and use-package clauses as effective if the
6257 -- node in question is not overloaded and therefore does not require
6258 -- resolution.
6259 --
6260 -- Note: Generic actual subprograms do not follow the normal resolution
6261 -- path, so ignore the fact that they are overloaded and mark them
6262 -- anyway.
6263
6264 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6265 Mark_Use_Clauses (N);
6266 end if;
6267
6268 -- Come here with entity set
6269
6270 <<Done>>
6271 Check_Restriction_No_Use_Of_Entity (N);
6272
6273 -- Annotate the tree by creating a variable reference marker in case the
6274 -- original variable reference is folded or optimized away. The variable
6275 -- reference marker is automatically saved for later examination by the
6276 -- ABE Processing phase. Variable references which act as actuals in a
6277 -- call require special processing and are left to Resolve_Actuals. The
6278 -- reference is a write when it appears on the left hand side of an
6279 -- assignment.
6280
6281 if Needs_Variable_Reference_Marker (N => N, Calls_OK => False) then
6282 declare
6283 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6284
6285 begin
6286 Build_Variable_Reference_Marker
6287 (N => N,
6288 Read => not Is_Assignment_LHS,
6289 Write => Is_Assignment_LHS);
6290 end;
6291 end if;
6292 end Find_Direct_Name;
6293
6294 ------------------------
6295 -- Find_Expanded_Name --
6296 ------------------------
6297
6298 -- This routine searches the homonym chain of the entity until it finds
6299 -- an entity declared in the scope denoted by the prefix. If the entity
6300 -- is private, it may nevertheless be immediately visible, if we are in
6301 -- the scope of its declaration.
6302
6303 procedure Find_Expanded_Name (N : Node_Id) is
6304 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean;
6305 -- Determine whether expanded name Nod appears within a pragma which is
6306 -- a suitable context for an abstract view of a state or variable. The
6307 -- following pragmas fall in this category:
6308 -- Depends
6309 -- Global
6310 -- Initializes
6311 -- Refined_Depends
6312 -- Refined_Global
6313 --
6314 -- In addition, pragma Abstract_State is also considered suitable even
6315 -- though it is an illegal context for an abstract view as this allows
6316 -- for proper resolution of abstract views of variables. This illegal
6317 -- context is later flagged in the analysis of indicator Part_Of.
6318
6319 -----------------------------
6320 -- In_Abstract_View_Pragma --
6321 -----------------------------
6322
6323 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is
6324 Par : Node_Id;
6325
6326 begin
6327 -- Climb the parent chain looking for a pragma
6328
6329 Par := Nod;
6330 while Present (Par) loop
6331 if Nkind (Par) = N_Pragma then
6332 if Pragma_Name_Unmapped (Par)
6333 in Name_Abstract_State
6334 | Name_Depends
6335 | Name_Global
6336 | Name_Initializes
6337 | Name_Refined_Depends
6338 | Name_Refined_Global
6339 then
6340 return True;
6341
6342 -- Otherwise the pragma is not a legal context for an abstract
6343 -- view.
6344
6345 else
6346 exit;
6347 end if;
6348
6349 -- Prevent the search from going too far
6350
6351 elsif Is_Body_Or_Package_Declaration (Par) then
6352 exit;
6353 end if;
6354
6355 Par := Parent (Par);
6356 end loop;
6357
6358 return False;
6359 end In_Abstract_View_Pragma;
6360
6361 -- Local variables
6362
6363 Selector : constant Node_Id := Selector_Name (N);
6364
6365 Candidate : Entity_Id := Empty;
6366 P_Name : Entity_Id;
6367 Id : Entity_Id;
6368
6369 -- Start of processing for Find_Expanded_Name
6370
6371 begin
6372 P_Name := Entity (Prefix (N));
6373
6374 -- If the prefix is a renamed package, look for the entity in the
6375 -- original package.
6376
6377 if Ekind (P_Name) = E_Package
6378 and then Present (Renamed_Object (P_Name))
6379 then
6380 P_Name := Renamed_Object (P_Name);
6381
6382 if From_Limited_With (P_Name)
6383 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6384 then
6385 Error_Msg_NE
6386 ("renaming of limited view of package & not usable in this"
6387 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
6388
6389 elsif Has_Limited_View (P_Name)
6390 and then not Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6391 and then not Is_Visible_Through_Renamings (P_Name)
6392 then
6393 Error_Msg_NE
6394 ("renaming of limited view of package & not usable in this"
6395 & " context (RM 8.5.3(3.1/2))", Prefix (N), P_Name);
6396 end if;
6397
6398 -- Rewrite node with entity field pointing to renamed object
6399
6400 Rewrite (Prefix (N), New_Copy (Prefix (N)));
6401 Set_Entity (Prefix (N), P_Name);
6402
6403 -- If the prefix is an object of a concurrent type, look for
6404 -- the entity in the associated task or protected type.
6405
6406 elsif Is_Concurrent_Type (Etype (P_Name)) then
6407 P_Name := Etype (P_Name);
6408 end if;
6409
6410 Id := Current_Entity (Selector);
6411
6412 declare
6413 Is_New_Candidate : Boolean;
6414
6415 begin
6416 while Present (Id) loop
6417 if Scope (Id) = P_Name then
6418 Candidate := Id;
6419 Is_New_Candidate := True;
6420
6421 -- Handle abstract views of states and variables. These are
6422 -- acceptable candidates only when the reference to the view
6423 -- appears in certain pragmas.
6424
6425 if Ekind (Id) = E_Abstract_State
6426 and then From_Limited_With (Id)
6427 and then Present (Non_Limited_View (Id))
6428 then
6429 if In_Abstract_View_Pragma (N) then
6430 Candidate := Non_Limited_View (Id);
6431 Is_New_Candidate := True;
6432
6433 -- Hide the candidate because it is not used in a proper
6434 -- context.
6435
6436 else
6437 Candidate := Empty;
6438 Is_New_Candidate := False;
6439 end if;
6440 end if;
6441
6442 -- Ada 2005 (AI-217): Handle shadow entities associated with
6443 -- types declared in limited-withed nested packages. We don't need
6444 -- to handle E_Incomplete_Subtype entities because the entities
6445 -- in the limited view are always E_Incomplete_Type and
6446 -- E_Class_Wide_Type entities (see Build_Limited_Views).
6447
6448 -- Regarding the expression used to evaluate the scope, it
6449 -- is important to note that the limited view also has shadow
6450 -- entities associated nested packages. For this reason the
6451 -- correct scope of the entity is the scope of the real entity.
6452 -- The non-limited view may itself be incomplete, in which case
6453 -- get the full view if available.
6454
6455 elsif Ekind (Id) in E_Incomplete_Type | E_Class_Wide_Type
6456 and then From_Limited_With (Id)
6457 and then Present (Non_Limited_View (Id))
6458 and then Scope (Non_Limited_View (Id)) = P_Name
6459 then
6460 Candidate := Get_Full_View (Non_Limited_View (Id));
6461 Is_New_Candidate := True;
6462
6463 -- Handle special case where the prefix is a renaming of a shadow
6464 -- package which is visible. Required to avoid reporting spurious
6465 -- errors.
6466
6467 elsif Ekind (P_Name) = E_Package
6468 and then From_Limited_With (P_Name)
6469 and then not From_Limited_With (Id)
6470 and then Sloc (Scope (Id)) = Sloc (P_Name)
6471 and then Unit_Is_Visible (Cunit (Get_Source_Unit (P_Name)))
6472 then
6473 Candidate := Get_Full_View (Id);
6474 Is_New_Candidate := True;
6475
6476 -- An unusual case arises with a fully qualified name for an
6477 -- entity local to a generic child unit package, within an
6478 -- instantiation of that package. The name of the unit now
6479 -- denotes the renaming created within the instance. This is
6480 -- only relevant in an instance body, see below.
6481
6482 elsif Is_Generic_Instance (Scope (Id))
6483 and then In_Open_Scopes (Scope (Id))
6484 and then In_Instance_Body
6485 and then Ekind (Scope (Id)) = E_Package
6486 and then Ekind (Id) = E_Package
6487 and then Renamed_Entity (Id) = Scope (Id)
6488 and then Is_Immediately_Visible (P_Name)
6489 then
6490 Is_New_Candidate := True;
6491
6492 else
6493 Is_New_Candidate := False;
6494 end if;
6495
6496 if Is_New_Candidate then
6497
6498 -- If entity is a child unit, either it is a visible child of
6499 -- the prefix, or we are in the body of a generic prefix, as
6500 -- will happen when a child unit is instantiated in the body
6501 -- of a generic parent. This is because the instance body does
6502 -- not restore the full compilation context, given that all
6503 -- non-local references have been captured.
6504
6505 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
6506 exit when Is_Visible_Lib_Unit (Id)
6507 or else (Is_Child_Unit (Id)
6508 and then In_Open_Scopes (Scope (Id))
6509 and then In_Instance_Body);
6510 else
6511 exit when not Is_Hidden (Id);
6512 end if;
6513
6514 exit when Is_Immediately_Visible (Id);
6515 end if;
6516
6517 Id := Homonym (Id);
6518 end loop;
6519 end;
6520
6521 if No (Id)
6522 and then Ekind (P_Name) in E_Procedure | E_Function
6523 and then Is_Generic_Instance (P_Name)
6524 then
6525 -- Expanded name denotes entity in (instance of) generic subprogram.
6526 -- The entity may be in the subprogram instance, or may denote one of
6527 -- the formals, which is declared in the enclosing wrapper package.
6528
6529 P_Name := Scope (P_Name);
6530
6531 Id := Current_Entity (Selector);
6532 while Present (Id) loop
6533 exit when Scope (Id) = P_Name;
6534 Id := Homonym (Id);
6535 end loop;
6536 end if;
6537
6538 if No (Id) or else Chars (Id) /= Chars (Selector) then
6539 Set_Etype (N, Any_Type);
6540
6541 -- If we are looking for an entity defined in System, try to find it
6542 -- in the child package that may have been provided as an extension
6543 -- to System. The Extend_System pragma will have supplied the name of
6544 -- the extension, which may have to be loaded.
6545
6546 if Chars (P_Name) = Name_System
6547 and then Scope (P_Name) = Standard_Standard
6548 and then Present (System_Extend_Unit)
6549 and then Present_System_Aux (N)
6550 then
6551 Set_Entity (Prefix (N), System_Aux_Id);
6552 Find_Expanded_Name (N);
6553 return;
6554
6555 -- There is an implicit instance of the predefined operator in
6556 -- the given scope. The operator entity is defined in Standard.
6557 -- Has_Implicit_Operator makes the node into an Expanded_Name.
6558
6559 elsif Nkind (Selector) = N_Operator_Symbol
6560 and then Has_Implicit_Operator (N)
6561 then
6562 return;
6563
6564 -- If there is no literal defined in the scope denoted by the
6565 -- prefix, the literal may belong to (a type derived from)
6566 -- Standard_Character, for which we have no explicit literals.
6567
6568 elsif Nkind (Selector) = N_Character_Literal
6569 and then Has_Implicit_Character_Literal (N)
6570 then
6571 return;
6572
6573 else
6574 -- If the prefix is a single concurrent object, use its name in
6575 -- the error message, rather than that of the anonymous type.
6576
6577 if Is_Concurrent_Type (P_Name)
6578 and then Is_Internal_Name (Chars (P_Name))
6579 then
6580 Error_Msg_Node_2 := Entity (Prefix (N));
6581 else
6582 Error_Msg_Node_2 := P_Name;
6583 end if;
6584
6585 if P_Name = System_Aux_Id then
6586 P_Name := Scope (P_Name);
6587 Set_Entity (Prefix (N), P_Name);
6588 end if;
6589
6590 if Present (Candidate) then
6591
6592 -- If we know that the unit is a child unit we can give a more
6593 -- accurate error message.
6594
6595 if Is_Child_Unit (Candidate) then
6596
6597 -- If the candidate is a private child unit and we are in
6598 -- the visible part of a public unit, specialize the error
6599 -- message. There might be a private with_clause for it,
6600 -- but it is not currently active.
6601
6602 if Is_Private_Descendant (Candidate)
6603 and then Ekind (Current_Scope) = E_Package
6604 and then not In_Private_Part (Current_Scope)
6605 and then not Is_Private_Descendant (Current_Scope)
6606 then
6607 Error_Msg_N
6608 ("private child unit& is not visible here", Selector);
6609
6610 -- Normal case where we have a missing with for a child unit
6611
6612 else
6613 Error_Msg_Qual_Level := 99;
6614 Error_Msg_NE -- CODEFIX
6615 ("missing `WITH &;`", Selector, Candidate);
6616 Error_Msg_Qual_Level := 0;
6617 end if;
6618
6619 -- Here we don't know that this is a child unit
6620
6621 else
6622 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
6623 end if;
6624
6625 else
6626 -- Within the instantiation of a child unit, the prefix may
6627 -- denote the parent instance, but the selector has the name
6628 -- of the original child. That is to say, when A.B appears
6629 -- within an instantiation of generic child unit B, the scope
6630 -- stack includes an instance of A (P_Name) and an instance
6631 -- of B under some other name. We scan the scope to find this
6632 -- child instance, which is the desired entity.
6633 -- Note that the parent may itself be a child instance, if
6634 -- the reference is of the form A.B.C, in which case A.B has
6635 -- already been rewritten with the proper entity.
6636
6637 if In_Open_Scopes (P_Name)
6638 and then Is_Generic_Instance (P_Name)
6639 then
6640 declare
6641 Gen_Par : constant Entity_Id :=
6642 Generic_Parent (Specification
6643 (Unit_Declaration_Node (P_Name)));
6644 S : Entity_Id := Current_Scope;
6645 P : Entity_Id;
6646
6647 begin
6648 for J in reverse 0 .. Scope_Stack.Last loop
6649 S := Scope_Stack.Table (J).Entity;
6650
6651 exit when S = Standard_Standard;
6652
6653 if Ekind (S) in E_Function | E_Package | E_Procedure
6654 then
6655 P :=
6656 Generic_Parent (Specification
6657 (Unit_Declaration_Node (S)));
6658
6659 -- Check that P is a generic child of the generic
6660 -- parent of the prefix.
6661
6662 if Present (P)
6663 and then Chars (P) = Chars (Selector)
6664 and then Scope (P) = Gen_Par
6665 then
6666 Id := S;
6667 goto Found;
6668 end if;
6669 end if;
6670
6671 end loop;
6672 end;
6673 end if;
6674
6675 -- If this is a selection from Ada, System or Interfaces, then
6676 -- we assume a missing with for the corresponding package.
6677
6678 if Is_Known_Unit (N)
6679 and then not (Present (Entity (Prefix (N)))
6680 and then Scope (Entity (Prefix (N))) /=
6681 Standard_Standard)
6682 then
6683 if not Error_Posted (N) then
6684 Error_Msg_Node_2 := Selector;
6685 Error_Msg_N -- CODEFIX
6686 ("missing `WITH &.&;`", Prefix (N));
6687 end if;
6688
6689 -- If this is a selection from a dummy package, then suppress
6690 -- the error message, of course the entity is missing if the
6691 -- package is missing.
6692
6693 elsif Sloc (Error_Msg_Node_2) = No_Location then
6694 null;
6695
6696 -- Here we have the case of an undefined component
6697
6698 else
6699 -- The prefix may hide a homonym in the context that
6700 -- declares the desired entity. This error can use a
6701 -- specialized message.
6702
6703 if In_Open_Scopes (P_Name) then
6704 declare
6705 H : constant Entity_Id := Homonym (P_Name);
6706
6707 begin
6708 if Present (H)
6709 and then Is_Compilation_Unit (H)
6710 and then
6711 (Is_Immediately_Visible (H)
6712 or else Is_Visible_Lib_Unit (H))
6713 then
6714 Id := First_Entity (H);
6715 while Present (Id) loop
6716 if Chars (Id) = Chars (Selector) then
6717 Error_Msg_Qual_Level := 99;
6718 Error_Msg_Name_1 := Chars (Selector);
6719 Error_Msg_NE
6720 ("% not declared in&", N, P_Name);
6721 Error_Msg_NE
6722 ("\use fully qualified name starting with "
6723 & "Standard to make& visible", N, H);
6724 Error_Msg_Qual_Level := 0;
6725 goto Done;
6726 end if;
6727
6728 Next_Entity (Id);
6729 end loop;
6730 end if;
6731
6732 -- If not found, standard error message
6733
6734 Error_Msg_NE ("& not declared in&", N, Selector);
6735
6736 <<Done>> null;
6737 end;
6738
6739 else
6740 -- Might be worth specializing the case when the prefix
6741 -- is a limited view.
6742 -- ... not declared in limited view of...
6743
6744 Error_Msg_NE ("& not declared in&", N, Selector);
6745 end if;
6746
6747 -- Check for misspelling of some entity in prefix
6748
6749 Id := First_Entity (P_Name);
6750 while Present (Id) loop
6751 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
6752 and then not Is_Internal_Name (Chars (Id))
6753 then
6754 Error_Msg_NE -- CODEFIX
6755 ("possible misspelling of&", Selector, Id);
6756 exit;
6757 end if;
6758
6759 Next_Entity (Id);
6760 end loop;
6761
6762 -- Specialize the message if this may be an instantiation
6763 -- of a child unit that was not mentioned in the context.
6764
6765 if Nkind (Parent (N)) = N_Package_Instantiation
6766 and then Is_Generic_Instance (Entity (Prefix (N)))
6767 and then Is_Compilation_Unit
6768 (Generic_Parent (Parent (Entity (Prefix (N)))))
6769 then
6770 Error_Msg_Node_2 := Selector;
6771 Error_Msg_N -- CODEFIX
6772 ("\missing `WITH &.&;`", Prefix (N));
6773 end if;
6774 end if;
6775 end if;
6776
6777 Id := Any_Id;
6778 end if;
6779 end if;
6780
6781 <<Found>>
6782 if Comes_From_Source (N)
6783 and then Is_Remote_Access_To_Subprogram_Type (Id)
6784 and then Ekind (Id) = E_Access_Subprogram_Type
6785 and then Present (Equivalent_Type (Id))
6786 then
6787 -- If we are not actually generating distribution code (i.e. the
6788 -- current PCS is the dummy non-distributed version), then the
6789 -- Equivalent_Type will be missing, and Id should be treated as
6790 -- a regular access-to-subprogram type.
6791
6792 Id := Equivalent_Type (Id);
6793 Set_Chars (Selector, Chars (Id));
6794 end if;
6795
6796 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
6797
6798 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
6799 if From_Limited_With (Id)
6800 or else Is_Type (Id)
6801 or else Ekind (Id) = E_Package
6802 then
6803 null;
6804 else
6805 Error_Msg_N
6806 ("limited withed package can only be used to access incomplete "
6807 & "types", N);
6808 end if;
6809 end if;
6810
6811 if Is_Task_Type (P_Name)
6812 and then ((Ekind (Id) = E_Entry
6813 and then Nkind (Parent (N)) /= N_Attribute_Reference)
6814 or else
6815 (Ekind (Id) = E_Entry_Family
6816 and then
6817 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
6818 then
6819 -- If both the task type and the entry are in scope, this may still
6820 -- be the expanded name of an entry formal.
6821
6822 if In_Open_Scopes (Id)
6823 and then Nkind (Parent (N)) = N_Selected_Component
6824 then
6825 null;
6826
6827 else
6828 -- It is an entry call after all, either to the current task
6829 -- (which will deadlock) or to an enclosing task.
6830
6831 Analyze_Selected_Component (N);
6832 return;
6833 end if;
6834 end if;
6835
6836 case Nkind (N) is
6837 when N_Selected_Component =>
6838 Reinit_Field_To_Zero (N, Is_Prefixed_Call);
6839 Change_Selected_Component_To_Expanded_Name (N);
6840
6841 when N_Expanded_Name =>
6842 null;
6843
6844 when others =>
6845 pragma Assert (False);
6846 end case;
6847
6848 -- Preserve relevant elaboration-related attributes of the context which
6849 -- are no longer available or very expensive to recompute once analysis,
6850 -- resolution, and expansion are over.
6851
6852 Mark_Elaboration_Attributes
6853 (N_Id => N,
6854 Checks => True,
6855 Modes => True,
6856 Warnings => True);
6857
6858 -- Set appropriate type
6859
6860 if Is_Type (Id) then
6861 Set_Etype (N, Id);
6862 else
6863 Set_Etype (N, Get_Full_View (Etype (Id)));
6864 end if;
6865
6866 -- Do style check and generate reference, but skip both steps if this
6867 -- entity has homonyms, since we may not have the right homonym set yet.
6868 -- The proper homonym will be set during the resolve phase.
6869
6870 if Has_Homonym (Id) then
6871 Set_Entity (N, Id);
6872
6873 else
6874 Set_Entity_Or_Discriminal (N, Id);
6875
6876 case Is_LHS (N) is
6877 when Yes =>
6878 Generate_Reference (Id, N, 'm');
6879
6880 when No =>
6881 Generate_Reference (Id, N, 'r');
6882
6883 when Unknown =>
6884 Defer_Reference ((Id, N));
6885 end case;
6886 end if;
6887
6888 -- Check for violation of No_Wide_Characters
6889
6890 Check_Wide_Character_Restriction (Id, N);
6891
6892 -- If the Ekind of the entity is Void, it means that all homonyms are
6893 -- hidden from all visibility (RM 8.3(5,14-20)).
6894
6895 if Ekind (Id) = E_Void then
6896 Premature_Usage (N);
6897
6898 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6899 declare
6900 H : Entity_Id := Homonym (Id);
6901
6902 begin
6903 while Present (H) loop
6904 if Scope (H) = Scope (Id)
6905 and then (not Is_Hidden (H)
6906 or else Is_Immediately_Visible (H))
6907 then
6908 Collect_Interps (N);
6909 exit;
6910 end if;
6911
6912 H := Homonym (H);
6913 end loop;
6914
6915 -- If an extension of System is present, collect possible explicit
6916 -- overloadings declared in the extension.
6917
6918 if Chars (P_Name) = Name_System
6919 and then Scope (P_Name) = Standard_Standard
6920 and then Present (System_Extend_Unit)
6921 and then Present_System_Aux (N)
6922 then
6923 H := Current_Entity (Id);
6924
6925 while Present (H) loop
6926 if Scope (H) = System_Aux_Id then
6927 Add_One_Interp (N, H, Etype (H));
6928 end if;
6929
6930 H := Homonym (H);
6931 end loop;
6932 end if;
6933 end;
6934 end if;
6935
6936 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6937 and then Scope (Id) /= Standard_Standard
6938 then
6939 -- In addition to user-defined operators in the given scope, there
6940 -- may be an implicit instance of the predefined operator. The
6941 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6942 -- and added to the interpretations. Procedure Add_One_Interp will
6943 -- determine which hides which.
6944
6945 if Has_Implicit_Operator (N) then
6946 null;
6947 end if;
6948 end if;
6949
6950 -- If there is a single interpretation for N we can generate a
6951 -- reference to the unique entity found.
6952
6953 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6954 Generate_Reference (Id, N);
6955 end if;
6956
6957 -- Mark relevant use-type and use-package clauses as effective if the
6958 -- node in question is not overloaded and therefore does not require
6959 -- resolution.
6960
6961 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6962 Mark_Use_Clauses (N);
6963 end if;
6964
6965 Check_Restriction_No_Use_Of_Entity (N);
6966
6967 -- Annotate the tree by creating a variable reference marker in case the
6968 -- original variable reference is folded or optimized away. The variable
6969 -- reference marker is automatically saved for later examination by the
6970 -- ABE Processing phase. Variable references which act as actuals in a
6971 -- call require special processing and are left to Resolve_Actuals. The
6972 -- reference is a write when it appears on the left hand side of an
6973 -- assignment.
6974
6975 if Needs_Variable_Reference_Marker
6976 (N => N,
6977 Calls_OK => False)
6978 then
6979 declare
6980 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6981
6982 begin
6983 Build_Variable_Reference_Marker
6984 (N => N,
6985 Read => not Is_Assignment_LHS,
6986 Write => Is_Assignment_LHS);
6987 end;
6988 end if;
6989 end Find_Expanded_Name;
6990
6991 --------------------
6992 -- Find_Most_Prev --
6993 --------------------
6994
6995 function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id is
6996 Curr : Node_Id;
6997
6998 begin
6999 -- Loop through the Prev_Use_Clause chain
7000
7001 Curr := Use_Clause;
7002 while Present (Prev_Use_Clause (Curr)) loop
7003 Curr := Prev_Use_Clause (Curr);
7004 end loop;
7005
7006 return Curr;
7007 end Find_Most_Prev;
7008
7009 -------------------------
7010 -- Find_Renamed_Entity --
7011 -------------------------
7012
7013 function Find_Renamed_Entity
7014 (N : Node_Id;
7015 Nam : Node_Id;
7016 New_S : Entity_Id;
7017 Is_Actual : Boolean := False) return Entity_Id
7018 is
7019 Ind : Interp_Index;
7020 I1 : Interp_Index := 0; -- Suppress junk warnings
7021 It : Interp;
7022 It1 : Interp;
7023 Old_S : Entity_Id;
7024 Inst : Entity_Id;
7025
7026 function Find_Nearer_Entity
7027 (New_S : Entity_Id;
7028 Old1_S : Entity_Id;
7029 Old2_S : Entity_Id) return Entity_Id;
7030 -- Determine whether one of Old_S1 and Old_S2 is nearer to New_S than
7031 -- the other, and return it if so. Return Empty otherwise. We use this
7032 -- in conjunction with Inherit_Renamed_Profile to simplify later type
7033 -- disambiguation for actual subprograms in instances.
7034
7035 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
7036 -- If the renamed entity is an implicit operator, check whether it is
7037 -- visible because its operand type is properly visible. This check
7038 -- applies to explicit renamed entities that appear in the source in a
7039 -- renaming declaration or a formal subprogram instance, but not to
7040 -- default generic actuals with a name.
7041
7042 function Report_Overload return Entity_Id;
7043 -- List possible interpretations, and specialize message in the
7044 -- case of a generic actual.
7045
7046 function Within (Inner, Outer : Entity_Id) return Boolean;
7047 -- Determine whether a candidate subprogram is defined within the
7048 -- enclosing instance. If yes, it has precedence over outer candidates.
7049
7050 --------------------------
7051 -- Find_Nearer_Entity --
7052 --------------------------
7053
7054 function Find_Nearer_Entity
7055 (New_S : Entity_Id;
7056 Old1_S : Entity_Id;
7057 Old2_S : Entity_Id) return Entity_Id
7058 is
7059 New_F : Entity_Id;
7060 Old1_F : Entity_Id;
7061 Old2_F : Entity_Id;
7062 Anc_T : Entity_Id;
7063
7064 begin
7065 New_F := First_Formal (New_S);
7066 Old1_F := First_Formal (Old1_S);
7067 Old2_F := First_Formal (Old2_S);
7068
7069 -- The criterion is whether the type of the formals of one of Old1_S
7070 -- and Old2_S is an ancestor subtype of the type of the corresponding
7071 -- formals of New_S while the other is not (we already know that they
7072 -- are all subtypes of the same base type).
7073
7074 -- This makes it possible to find the more correct renamed entity in
7075 -- the case of a generic instantiation nested in an enclosing one for
7076 -- which different formal types get the same actual type, which will
7077 -- in turn make it possible for Inherit_Renamed_Profile to preserve
7078 -- types on formal parameters and ultimately simplify disambiguation.
7079
7080 -- Consider the follow package G:
7081
7082 -- generic
7083 -- type Item_T is private;
7084 -- with function Compare (L, R: Item_T) return Boolean is <>;
7085
7086 -- type Bound_T is private;
7087 -- with function Compare (L, R : Bound_T) return Boolean is <>;
7088 -- package G is
7089 -- ...
7090 -- end G;
7091
7092 -- package body G is
7093 -- package My_Inner is Inner_G (Bound_T);
7094 -- ...
7095 -- end G;
7096
7097 -- with the following package Inner_G:
7098
7099 -- generic
7100 -- type T is private;
7101 -- with function Compare (L, R: T) return Boolean is <>;
7102 -- package Inner_G is
7103 -- function "<" (L, R: T) return Boolean is (Compare (L, R));
7104 -- end Inner_G;
7105
7106 -- If G is instantiated on the same actual type with a single Compare
7107 -- function:
7108
7109 -- type T is ...
7110 -- function Compare (L, R : T) return Boolean;
7111 -- package My_G is new (T, T);
7112
7113 -- then the renaming generated for Compare in the inner instantiation
7114 -- is ambiguous: it can rename either of the renamings generated for
7115 -- the outer instantiation. Now if the first one is picked up, then
7116 -- the subtypes of the formal parameters of the renaming will not be
7117 -- preserved in Inherit_Renamed_Profile because they are subtypes of
7118 -- the Bound_T formal type and not of the Item_T formal type, so we
7119 -- need to arrange for the second one to be picked up instead.
7120
7121 while Present (New_F) loop
7122 if Etype (Old1_F) /= Etype (Old2_F) then
7123 Anc_T := Ancestor_Subtype (Etype (New_F));
7124
7125 if Etype (Old1_F) = Anc_T then
7126 return Old1_S;
7127 elsif Etype (Old2_F) = Anc_T then
7128 return Old2_S;
7129 end if;
7130 end if;
7131
7132 Next_Formal (New_F);
7133 Next_Formal (Old1_F);
7134 Next_Formal (Old2_F);
7135 end loop;
7136
7137 pragma Assert (No (Old1_F));
7138 pragma Assert (No (Old2_F));
7139
7140 return Empty;
7141 end Find_Nearer_Entity;
7142
7143 --------------------------
7144 -- Is_Visible_Operation --
7145 --------------------------
7146
7147 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
7148 Scop : Entity_Id;
7149 Typ : Entity_Id;
7150 Btyp : Entity_Id;
7151
7152 begin
7153 if Ekind (Op) /= E_Operator
7154 or else Scope (Op) /= Standard_Standard
7155 or else (In_Instance
7156 and then (not Is_Actual
7157 or else Present (Enclosing_Instance)))
7158 then
7159 return True;
7160
7161 else
7162 -- For a fixed point type operator, check the resulting type,
7163 -- because it may be a mixed mode integer * fixed operation.
7164
7165 if Present (Next_Formal (First_Formal (New_S)))
7166 and then Is_Fixed_Point_Type (Etype (New_S))
7167 then
7168 Typ := Etype (New_S);
7169 else
7170 Typ := Etype (First_Formal (New_S));
7171 end if;
7172
7173 Btyp := Base_Type (Typ);
7174
7175 if Nkind (Nam) /= N_Expanded_Name then
7176 return (In_Open_Scopes (Scope (Btyp))
7177 or else Is_Potentially_Use_Visible (Btyp)
7178 or else In_Use (Btyp)
7179 or else In_Use (Scope (Btyp)));
7180
7181 else
7182 Scop := Entity (Prefix (Nam));
7183
7184 if Ekind (Scop) = E_Package
7185 and then Present (Renamed_Object (Scop))
7186 then
7187 Scop := Renamed_Object (Scop);
7188 end if;
7189
7190 -- Operator is visible if prefix of expanded name denotes
7191 -- scope of type, or else type is defined in System_Aux
7192 -- and the prefix denotes System.
7193
7194 return Scope (Btyp) = Scop
7195 or else (Scope (Btyp) = System_Aux_Id
7196 and then Scope (Scope (Btyp)) = Scop);
7197 end if;
7198 end if;
7199 end Is_Visible_Operation;
7200
7201 ------------
7202 -- Within --
7203 ------------
7204
7205 function Within (Inner, Outer : Entity_Id) return Boolean is
7206 Sc : Entity_Id;
7207
7208 begin
7209 Sc := Scope (Inner);
7210 while Sc /= Standard_Standard loop
7211 if Sc = Outer then
7212 return True;
7213 else
7214 Sc := Scope (Sc);
7215 end if;
7216 end loop;
7217
7218 return False;
7219 end Within;
7220
7221 ---------------------
7222 -- Report_Overload --
7223 ---------------------
7224
7225 function Report_Overload return Entity_Id is
7226 begin
7227 if Is_Actual then
7228 Error_Msg_NE -- CODEFIX
7229 ("ambiguous actual subprogram&, " &
7230 "possible interpretations:", N, Nam);
7231 else
7232 Error_Msg_N -- CODEFIX
7233 ("ambiguous subprogram, " &
7234 "possible interpretations:", N);
7235 end if;
7236
7237 List_Interps (Nam, N);
7238 return Old_S;
7239 end Report_Overload;
7240
7241 -- Start of processing for Find_Renamed_Entity
7242
7243 begin
7244 Old_S := Any_Id;
7245 Candidate_Renaming := Empty;
7246
7247 if Is_Overloaded (Nam) then
7248 Get_First_Interp (Nam, Ind, It);
7249 while Present (It.Nam) loop
7250 if Entity_Matches_Spec (It.Nam, New_S)
7251 and then Is_Visible_Operation (It.Nam)
7252 then
7253 if Old_S /= Any_Id then
7254
7255 -- Note: The call to Disambiguate only happens if a
7256 -- previous interpretation was found, in which case I1
7257 -- has received a value.
7258
7259 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
7260
7261 if It1 = No_Interp then
7262 Inst := Enclosing_Instance;
7263
7264 if Present (Inst) then
7265 if Within (It.Nam, Inst) then
7266 if Within (Old_S, Inst) then
7267 declare
7268 It_D : constant Uint := Scope_Depth (It.Nam);
7269 Old_D : constant Uint := Scope_Depth (Old_S);
7270 N_Ent : Entity_Id;
7271 begin
7272 -- Choose the innermost subprogram, which
7273 -- would hide the outer one in the generic.
7274
7275 if Old_D > It_D then
7276 return Old_S;
7277 elsif It_D > Old_D then
7278 return It.Nam;
7279 end if;
7280
7281 -- Otherwise, if we can determine that one
7282 -- of the entities is nearer to the renaming
7283 -- than the other, choose it. If not, then
7284 -- return the newer one as done historically.
7285
7286 N_Ent :=
7287 Find_Nearer_Entity (New_S, Old_S, It.Nam);
7288 if Present (N_Ent) then
7289 return N_Ent;
7290 else
7291 return It.Nam;
7292 end if;
7293 end;
7294 end if;
7295
7296 elsif Within (Old_S, Inst) then
7297 return Old_S;
7298
7299 else
7300 return Report_Overload;
7301 end if;
7302
7303 -- If not within an instance, ambiguity is real
7304
7305 else
7306 return Report_Overload;
7307 end if;
7308
7309 else
7310 Old_S := It1.Nam;
7311 exit;
7312 end if;
7313
7314 else
7315 I1 := Ind;
7316 Old_S := It.Nam;
7317 end if;
7318
7319 elsif
7320 Present (First_Formal (It.Nam))
7321 and then Present (First_Formal (New_S))
7322 and then (Base_Type (Etype (First_Formal (It.Nam))) =
7323 Base_Type (Etype (First_Formal (New_S))))
7324 then
7325 Candidate_Renaming := It.Nam;
7326 end if;
7327
7328 Get_Next_Interp (Ind, It);
7329 end loop;
7330
7331 Set_Entity (Nam, Old_S);
7332
7333 if Old_S /= Any_Id then
7334 Set_Is_Overloaded (Nam, False);
7335 end if;
7336
7337 -- Non-overloaded case
7338
7339 else
7340 if Is_Actual
7341 and then Present (Enclosing_Instance)
7342 and then Entity_Matches_Spec (Entity (Nam), New_S)
7343 then
7344 Old_S := Entity (Nam);
7345
7346 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
7347 Candidate_Renaming := New_S;
7348
7349 if Is_Visible_Operation (Entity (Nam)) then
7350 Old_S := Entity (Nam);
7351 end if;
7352
7353 elsif Present (First_Formal (Entity (Nam)))
7354 and then Present (First_Formal (New_S))
7355 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
7356 Base_Type (Etype (First_Formal (New_S))))
7357 then
7358 Candidate_Renaming := Entity (Nam);
7359 end if;
7360 end if;
7361
7362 return Old_S;
7363 end Find_Renamed_Entity;
7364
7365 -----------------------------
7366 -- Find_Selected_Component --
7367 -----------------------------
7368
7369 procedure Find_Selected_Component (N : Node_Id) is
7370 P : constant Node_Id := Prefix (N);
7371
7372 P_Name : Entity_Id;
7373 -- Entity denoted by prefix
7374
7375 P_Type : Entity_Id;
7376 -- and its type
7377
7378 Nam : Node_Id;
7379
7380 function Available_Subtype return Boolean;
7381 -- A small optimization: if the prefix is constrained and the component
7382 -- is an array type we may already have a usable subtype for it, so we
7383 -- can use it rather than generating a new one, because the bounds
7384 -- will be the values of the discriminants and not discriminant refs.
7385 -- This simplifies value tracing in GNATprove. For consistency, both
7386 -- the entity name and the subtype come from the constrained component.
7387
7388 -- This is only used in GNATprove mode: when generating code it may be
7389 -- necessary to create an itype in the scope of use of the selected
7390 -- component, e.g. in the context of a expanded record equality.
7391
7392 function Is_Reference_In_Subunit return Boolean;
7393 -- In a subunit, the scope depth is not a proper measure of hiding,
7394 -- because the context of the proper body may itself hide entities in
7395 -- parent units. This rare case requires inspecting the tree directly
7396 -- because the proper body is inserted in the main unit and its context
7397 -- is simply added to that of the parent.
7398
7399 -----------------------
7400 -- Available_Subtype --
7401 -----------------------
7402
7403 function Available_Subtype return Boolean is
7404 Comp : Entity_Id;
7405
7406 begin
7407 if GNATprove_Mode then
7408 Comp := First_Entity (Etype (P));
7409 while Present (Comp) loop
7410 if Chars (Comp) = Chars (Selector_Name (N)) then
7411 Set_Etype (N, Etype (Comp));
7412 Set_Entity (Selector_Name (N), Comp);
7413 Set_Etype (Selector_Name (N), Etype (Comp));
7414 return True;
7415 end if;
7416
7417 Next_Component (Comp);
7418 end loop;
7419 end if;
7420
7421 return False;
7422 end Available_Subtype;
7423
7424 -----------------------------
7425 -- Is_Reference_In_Subunit --
7426 -----------------------------
7427
7428 function Is_Reference_In_Subunit return Boolean is
7429 Clause : Node_Id;
7430 Comp_Unit : Node_Id;
7431
7432 begin
7433 Comp_Unit := N;
7434 while Present (Comp_Unit)
7435 and then Nkind (Comp_Unit) /= N_Compilation_Unit
7436 loop
7437 Comp_Unit := Parent (Comp_Unit);
7438 end loop;
7439
7440 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
7441 return False;
7442 end if;
7443
7444 -- Now check whether the package is in the context of the subunit
7445
7446 Clause := First (Context_Items (Comp_Unit));
7447 while Present (Clause) loop
7448 if Nkind (Clause) = N_With_Clause
7449 and then Entity (Name (Clause)) = P_Name
7450 then
7451 return True;
7452 end if;
7453
7454 Next (Clause);
7455 end loop;
7456
7457 return False;
7458 end Is_Reference_In_Subunit;
7459
7460 -- Start of processing for Find_Selected_Component
7461
7462 begin
7463 Analyze (P);
7464
7465 if Nkind (P) = N_Error then
7466 return;
7467 end if;
7468
7469 -- If the selector already has an entity, the node has been constructed
7470 -- in the course of expansion, and is known to be valid. Do not verify
7471 -- that it is defined for the type (it may be a private component used
7472 -- in the expansion of record equality).
7473
7474 if Present (Entity (Selector_Name (N))) then
7475 if No (Etype (N)) or else Etype (N) = Any_Type then
7476 declare
7477 Sel_Name : constant Node_Id := Selector_Name (N);
7478 Selector : constant Entity_Id := Entity (Sel_Name);
7479 C_Etype : Node_Id;
7480
7481 begin
7482 Set_Etype (Sel_Name, Etype (Selector));
7483
7484 if not Is_Entity_Name (P) then
7485 Resolve (P);
7486 end if;
7487
7488 -- Build an actual subtype except for the first parameter
7489 -- of an init proc, where this actual subtype is by
7490 -- definition incorrect, since the object is uninitialized
7491 -- (and does not even have defined discriminants etc.)
7492
7493 if Is_Entity_Name (P)
7494 and then Ekind (Entity (P)) = E_Function
7495 then
7496 Nam := New_Copy (P);
7497
7498 if Is_Overloaded (P) then
7499 Save_Interps (P, Nam);
7500 end if;
7501
7502 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
7503 Analyze_Call (P);
7504 Analyze_Selected_Component (N);
7505 return;
7506
7507 elsif Ekind (Selector) = E_Component
7508 and then (not Is_Entity_Name (P)
7509 or else Chars (Entity (P)) /= Name_uInit)
7510 then
7511 -- Check if we already have an available subtype we can use
7512
7513 if Ekind (Etype (P)) = E_Record_Subtype
7514 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
7515 and then Is_Array_Type (Etype (Selector))
7516 and then not Is_Packed (Etype (Selector))
7517 and then Available_Subtype
7518 then
7519 return;
7520
7521 -- Do not build the subtype when referencing components of
7522 -- dispatch table wrappers. Required to avoid generating
7523 -- elaboration code with HI runtimes.
7524
7525 elsif Is_RTE (Scope (Selector), RE_Dispatch_Table_Wrapper)
7526 or else
7527 Is_RTE (Scope (Selector), RE_No_Dispatch_Table_Wrapper)
7528 then
7529 C_Etype := Empty;
7530 else
7531 C_Etype :=
7532 Build_Actual_Subtype_Of_Component
7533 (Etype (Selector), N);
7534 end if;
7535
7536 else
7537 C_Etype := Empty;
7538 end if;
7539
7540 if No (C_Etype) then
7541 C_Etype := Etype (Selector);
7542 else
7543 Insert_Action (N, C_Etype);
7544 C_Etype := Defining_Identifier (C_Etype);
7545 end if;
7546
7547 Set_Etype (N, C_Etype);
7548 end;
7549
7550 -- If the selected component appears within a default expression
7551 -- and it has an actual subtype, the preanalysis has not yet
7552 -- completed its analysis, because Insert_Actions is disabled in
7553 -- that context. Within the init proc of the enclosing type we
7554 -- must complete this analysis, if an actual subtype was created.
7555
7556 elsif Inside_Init_Proc then
7557 declare
7558 Typ : constant Entity_Id := Etype (N);
7559 Decl : constant Node_Id := Declaration_Node (Typ);
7560 begin
7561 if Nkind (Decl) = N_Subtype_Declaration
7562 and then not Analyzed (Decl)
7563 and then Is_List_Member (Decl)
7564 and then No (Parent (Decl))
7565 then
7566 Remove (Decl);
7567 Insert_Action (N, Decl);
7568 end if;
7569 end;
7570 end if;
7571
7572 return;
7573
7574 elsif Is_Entity_Name (P) then
7575 P_Name := Entity (P);
7576
7577 -- The prefix may denote an enclosing type which is the completion
7578 -- of an incomplete type declaration.
7579
7580 if Is_Type (P_Name) then
7581 Set_Entity (P, Get_Full_View (P_Name));
7582 Set_Etype (P, Entity (P));
7583 P_Name := Entity (P);
7584 end if;
7585
7586 P_Type := Base_Type (Etype (P));
7587
7588 if Debug_Flag_E then
7589 Write_Str ("Found prefix type to be ");
7590 Write_Entity_Info (P_Type, " "); Write_Eol;
7591 end if;
7592
7593 -- If the prefix's type is an access type, get to the record type
7594
7595 if Is_Access_Type (P_Type) then
7596 P_Type := Implicitly_Designated_Type (P_Type);
7597 end if;
7598
7599 -- First check for components of a record object (not the
7600 -- result of a call, which is handled below).
7601
7602 if Has_Components (P_Type)
7603 and then not Is_Overloadable (P_Name)
7604 and then not Is_Type (P_Name)
7605 then
7606 -- Selected component of record. Type checking will validate
7607 -- name of selector.
7608
7609 -- ??? Could we rewrite an implicit dereference into an explicit
7610 -- one here?
7611
7612 Analyze_Selected_Component (N);
7613
7614 -- Reference to type name in predicate/invariant expression
7615
7616 elsif Is_Concurrent_Type (P_Type)
7617 and then not In_Open_Scopes (P_Name)
7618 and then (not Is_Concurrent_Type (Etype (P_Name))
7619 or else not In_Open_Scopes (Etype (P_Name)))
7620 then
7621 -- Call to protected operation or entry. Type checking is
7622 -- needed on the prefix.
7623
7624 Analyze_Selected_Component (N);
7625
7626 elsif (In_Open_Scopes (P_Name)
7627 and then Ekind (P_Name) /= E_Void
7628 and then not Is_Overloadable (P_Name))
7629 or else (Is_Concurrent_Type (Etype (P_Name))
7630 and then In_Open_Scopes (Etype (P_Name)))
7631 then
7632 -- Prefix denotes an enclosing loop, block, or task, i.e. an
7633 -- enclosing construct that is not a subprogram or accept.
7634
7635 -- A special case: a protected body may call an operation
7636 -- on an external object of the same type, in which case it
7637 -- is not an expanded name. If the prefix is the type itself,
7638 -- or the context is a single synchronized object it can only
7639 -- be interpreted as an expanded name.
7640
7641 if Is_Concurrent_Type (Etype (P_Name)) then
7642 if Is_Type (P_Name)
7643 or else Present (Anonymous_Object (Etype (P_Name)))
7644 then
7645 Find_Expanded_Name (N);
7646
7647 else
7648 Analyze_Selected_Component (N);
7649 return;
7650 end if;
7651
7652 else
7653 Find_Expanded_Name (N);
7654 end if;
7655
7656 elsif Ekind (P_Name) = E_Package then
7657 Find_Expanded_Name (N);
7658
7659 elsif Is_Overloadable (P_Name) then
7660
7661 -- The subprogram may be a renaming (of an enclosing scope) as
7662 -- in the case of the name of the generic within an instantiation.
7663
7664 if Ekind (P_Name) in E_Procedure | E_Function
7665 and then Present (Alias (P_Name))
7666 and then Is_Generic_Instance (Alias (P_Name))
7667 then
7668 P_Name := Alias (P_Name);
7669 end if;
7670
7671 if Is_Overloaded (P) then
7672
7673 -- The prefix must resolve to a unique enclosing construct
7674
7675 declare
7676 Found : Boolean := False;
7677 Ind : Interp_Index;
7678 It : Interp;
7679
7680 begin
7681 Get_First_Interp (P, Ind, It);
7682 while Present (It.Nam) loop
7683 if In_Open_Scopes (It.Nam) then
7684 if Found then
7685 Error_Msg_N (
7686 "prefix must be unique enclosing scope", N);
7687 Set_Entity (N, Any_Id);
7688 Set_Etype (N, Any_Type);
7689 return;
7690
7691 else
7692 Found := True;
7693 P_Name := It.Nam;
7694 end if;
7695 end if;
7696
7697 Get_Next_Interp (Ind, It);
7698 end loop;
7699 end;
7700 end if;
7701
7702 if In_Open_Scopes (P_Name) then
7703 Set_Entity (P, P_Name);
7704 Set_Is_Overloaded (P, False);
7705 Find_Expanded_Name (N);
7706
7707 else
7708 -- If no interpretation as an expanded name is possible, it
7709 -- must be a selected component of a record returned by a
7710 -- function call. Reformat prefix as a function call, the rest
7711 -- is done by type resolution.
7712
7713 -- Error if the prefix is procedure or entry, as is P.X
7714
7715 if Ekind (P_Name) /= E_Function
7716 and then
7717 (not Is_Overloaded (P)
7718 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
7719 then
7720 -- Prefix may mention a package that is hidden by a local
7721 -- declaration: let the user know. Scan the full homonym
7722 -- chain, the candidate package may be anywhere on it.
7723
7724 if Present (Homonym (Current_Entity (P_Name))) then
7725 P_Name := Current_Entity (P_Name);
7726
7727 while Present (P_Name) loop
7728 exit when Ekind (P_Name) = E_Package;
7729 P_Name := Homonym (P_Name);
7730 end loop;
7731
7732 if Present (P_Name) then
7733 if not Is_Reference_In_Subunit then
7734 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
7735 Error_Msg_NE
7736 ("package& is hidden by declaration#", N, P_Name);
7737 end if;
7738
7739 Set_Entity (Prefix (N), P_Name);
7740 Find_Expanded_Name (N);
7741 return;
7742
7743 else
7744 P_Name := Entity (Prefix (N));
7745 end if;
7746 end if;
7747
7748 Error_Msg_NE
7749 ("invalid prefix in selected component&", N, P_Name);
7750 Change_Selected_Component_To_Expanded_Name (N);
7751 Set_Entity (N, Any_Id);
7752 Set_Etype (N, Any_Type);
7753
7754 -- Here we have a function call, so do the reformatting
7755
7756 else
7757 Nam := New_Copy (P);
7758 Save_Interps (P, Nam);
7759
7760 -- We use Replace here because this is one of those cases
7761 -- where the parser has missclassified the node, and we fix
7762 -- things up and then do the semantic analysis on the fixed
7763 -- up node. Normally we do this using one of the Sinfo.CN
7764 -- routines, but this is too tricky for that.
7765
7766 -- Note that using Rewrite would be wrong, because we would
7767 -- have a tree where the original node is unanalyzed.
7768
7769 Replace (P,
7770 Make_Function_Call (Sloc (P), Name => Nam));
7771
7772 -- Now analyze the reformatted node
7773
7774 Analyze_Call (P);
7775
7776 -- If the prefix is illegal after this transformation, there
7777 -- may be visibility errors on the prefix. The safest is to
7778 -- treat the selected component as an error.
7779
7780 if Error_Posted (P) then
7781 Set_Etype (N, Any_Type);
7782 return;
7783
7784 else
7785 Analyze_Selected_Component (N);
7786 end if;
7787 end if;
7788 end if;
7789
7790 -- Remaining cases generate various error messages
7791
7792 else
7793 -- Format node as expanded name, to avoid cascaded errors
7794
7795 Change_Selected_Component_To_Expanded_Name (N);
7796 Set_Entity (N, Any_Id);
7797 Set_Etype (N, Any_Type);
7798
7799 -- Issue error message, but avoid this if error issued already.
7800 -- Use identifier of prefix if one is available.
7801
7802 if P_Name = Any_Id then
7803 null;
7804
7805 -- It is not an error if the prefix is the current instance of
7806 -- type name, e.g. the expression of a type aspect, when it is
7807 -- analyzed within a generic unit. We still have to verify that a
7808 -- component of that name exists, and decorate the node
7809 -- accordingly.
7810
7811 elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then
7812 declare
7813 Comp : Entity_Id;
7814
7815 begin
7816 Comp := First_Entity (Entity (P));
7817 while Present (Comp) loop
7818 if Chars (Comp) = Chars (Selector_Name (N)) then
7819 Set_Entity (N, Comp);
7820 Set_Etype (N, Etype (Comp));
7821 Set_Entity (Selector_Name (N), Comp);
7822 Set_Etype (Selector_Name (N), Etype (Comp));
7823 return;
7824 end if;
7825
7826 Next_Entity (Comp);
7827 end loop;
7828 end;
7829
7830 elsif Ekind (P_Name) = E_Void then
7831 Premature_Usage (P);
7832
7833 elsif Nkind (P) /= N_Attribute_Reference then
7834
7835 -- This may have been meant as a prefixed call to a primitive
7836 -- of an untagged type. If it is a function call check type of
7837 -- its first formal and add explanation.
7838
7839 declare
7840 F : constant Entity_Id :=
7841 Current_Entity (Selector_Name (N));
7842 begin
7843 if Present (F)
7844 and then Is_Overloadable (F)
7845 and then Present (First_Entity (F))
7846 and then not Is_Tagged_Type (Etype (First_Entity (F)))
7847 then
7848 Error_Msg_N
7849 ("prefixed call is only allowed for objects of a "
7850 & "tagged type", N);
7851 end if;
7852 end;
7853
7854 Error_Msg_N ("invalid prefix in selected component&", P);
7855
7856 if Is_Incomplete_Type (P_Type)
7857 and then Is_Access_Type (Etype (P))
7858 then
7859 Error_Msg_N
7860 ("\dereference must not be of an incomplete type "
7861 & "(RM 3.10.1)", P);
7862 end if;
7863
7864 else
7865 Error_Msg_N ("invalid prefix in selected component", P);
7866 end if;
7867 end if;
7868 else
7869 -- If prefix is not the name of an entity, it must be an expression,
7870 -- whose type is appropriate for a record. This is determined by
7871 -- type resolution.
7872
7873 Analyze_Selected_Component (N);
7874 end if;
7875
7876 Analyze_Dimension (N);
7877 end Find_Selected_Component;
7878
7879 ---------------
7880 -- Find_Type --
7881 ---------------
7882
7883 procedure Find_Type (N : Node_Id) is
7884 C : Entity_Id;
7885 Typ : Entity_Id;
7886 T : Entity_Id;
7887 T_Name : Entity_Id;
7888
7889 begin
7890 if N = Error then
7891 return;
7892
7893 elsif Nkind (N) = N_Attribute_Reference then
7894
7895 -- Class attribute. This is not valid in Ada 83 mode, but we do not
7896 -- need to enforce that at this point, since the declaration of the
7897 -- tagged type in the prefix would have been flagged already.
7898
7899 if Attribute_Name (N) = Name_Class then
7900 Check_Restriction (No_Dispatch, N);
7901 Find_Type (Prefix (N));
7902
7903 -- Propagate error from bad prefix
7904
7905 if Etype (Prefix (N)) = Any_Type then
7906 Set_Entity (N, Any_Type);
7907 Set_Etype (N, Any_Type);
7908 return;
7909 end if;
7910
7911 T := Base_Type (Entity (Prefix (N)));
7912
7913 -- Case where type is not known to be tagged. Its appearance in
7914 -- the prefix of the 'Class attribute indicates that the full view
7915 -- will be tagged.
7916
7917 if not Is_Tagged_Type (T) then
7918 if Ekind (T) = E_Incomplete_Type then
7919
7920 -- It is legal to denote the class type of an incomplete
7921 -- type. The full type will have to be tagged, of course.
7922 -- In Ada 2005 this usage is declared obsolescent, so we
7923 -- warn accordingly. This usage is only legal if the type
7924 -- is completed in the current scope, and not for a limited
7925 -- view of a type.
7926
7927 if Ada_Version >= Ada_2005 then
7928
7929 -- Test whether the Available_View of a limited type view
7930 -- is tagged, since the limited view may not be marked as
7931 -- tagged if the type itself has an untagged incomplete
7932 -- type view in its package.
7933
7934 if From_Limited_With (T)
7935 and then not Is_Tagged_Type (Available_View (T))
7936 then
7937 Error_Msg_N
7938 ("prefix of Class attribute must be tagged", N);
7939 Set_Etype (N, Any_Type);
7940 Set_Entity (N, Any_Type);
7941 return;
7942
7943 else
7944 if Restriction_Check_Required (No_Obsolescent_Features)
7945 then
7946 Check_Restriction
7947 (No_Obsolescent_Features, Prefix (N));
7948 end if;
7949
7950 if Warn_On_Obsolescent_Feature then
7951 Error_Msg_N
7952 ("applying ''Class to an untagged incomplete type"
7953 & " is an obsolescent feature (RM J.11)?r?", N);
7954 end if;
7955 end if;
7956 end if;
7957
7958 Set_Is_Tagged_Type (T);
7959 Set_Direct_Primitive_Operations (T, New_Elmt_List);
7960 Make_Class_Wide_Type (T);
7961 Set_Entity (N, Class_Wide_Type (T));
7962 Set_Etype (N, Class_Wide_Type (T));
7963
7964 elsif Ekind (T) = E_Private_Type
7965 and then not Is_Generic_Type (T)
7966 and then In_Private_Part (Scope (T))
7967 then
7968 -- The Class attribute can be applied to an untagged private
7969 -- type fulfilled by a tagged type prior to the full type
7970 -- declaration (but only within the parent package's private
7971 -- part). Create the class-wide type now and check that the
7972 -- full type is tagged later during its analysis. Note that
7973 -- we do not mark the private type as tagged, unlike the
7974 -- case of incomplete types, because the type must still
7975 -- appear untagged to outside units.
7976
7977 if No (Class_Wide_Type (T)) then
7978 Make_Class_Wide_Type (T);
7979 end if;
7980
7981 Set_Entity (N, Class_Wide_Type (T));
7982 Set_Etype (N, Class_Wide_Type (T));
7983
7984 else
7985 -- Should we introduce a type Any_Tagged and use Wrong_Type
7986 -- here, it would be a bit more consistent???
7987
7988 Error_Msg_NE
7989 ("tagged type required, found}",
7990 Prefix (N), First_Subtype (T));
7991 Set_Entity (N, Any_Type);
7992 return;
7993 end if;
7994
7995 -- Case of tagged type
7996
7997 else
7998 if Is_Concurrent_Type (T) then
7999 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
8000
8001 -- Previous error. Create a class-wide type for the
8002 -- synchronized type itself, with minimal semantic
8003 -- attributes, to catch other errors in some ACATS tests.
8004
8005 pragma Assert (Serious_Errors_Detected /= 0);
8006 Make_Class_Wide_Type (T);
8007 C := Class_Wide_Type (T);
8008 Set_First_Entity (C, First_Entity (T));
8009
8010 else
8011 C := Class_Wide_Type
8012 (Corresponding_Record_Type (Entity (Prefix (N))));
8013 end if;
8014
8015 else
8016 C := Class_Wide_Type (Entity (Prefix (N)));
8017 end if;
8018
8019 Set_Entity_With_Checks (N, C);
8020 Generate_Reference (C, N);
8021 Set_Etype (N, C);
8022 end if;
8023
8024 -- Base attribute, not allowed in Ada 83
8025
8026 elsif Attribute_Name (N) = Name_Base then
8027 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
8028 Error_Msg_N
8029 ("(Ada 83) Base attribute not allowed in subtype mark", N);
8030
8031 else
8032 Find_Type (Prefix (N));
8033 Typ := Entity (Prefix (N));
8034
8035 if Ada_Version >= Ada_95
8036 and then not Is_Scalar_Type (Typ)
8037 and then not Is_Generic_Type (Typ)
8038 then
8039 Error_Msg_N
8040 ("prefix of Base attribute must be scalar type",
8041 Prefix (N));
8042
8043 elsif Warn_On_Redundant_Constructs
8044 and then Base_Type (Typ) = Typ
8045 then
8046 Error_Msg_NE -- CODEFIX
8047 ("redundant attribute, & is its own base type?r?", N, Typ);
8048 end if;
8049
8050 T := Base_Type (Typ);
8051
8052 -- Rewrite attribute reference with type itself (see similar
8053 -- processing in Analyze_Attribute, case Base). Preserve prefix
8054 -- if present, for other legality checks.
8055
8056 if Nkind (Prefix (N)) = N_Expanded_Name then
8057 Rewrite (N,
8058 Make_Expanded_Name (Sloc (N),
8059 Chars => Chars (T),
8060 Prefix => New_Copy (Prefix (Prefix (N))),
8061 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
8062
8063 else
8064 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
8065 end if;
8066
8067 Set_Entity (N, T);
8068 Set_Etype (N, T);
8069 end if;
8070
8071 elsif Attribute_Name (N) = Name_Stub_Type then
8072
8073 -- This is handled in Analyze_Attribute
8074
8075 Analyze (N);
8076
8077 -- All other attributes are invalid in a subtype mark
8078
8079 else
8080 Error_Msg_N ("invalid attribute in subtype mark", N);
8081 end if;
8082
8083 else
8084 Analyze (N);
8085
8086 if Is_Entity_Name (N) then
8087 T_Name := Entity (N);
8088 else
8089 Error_Msg_N ("subtype mark required in this context", N);
8090 Set_Etype (N, Any_Type);
8091 return;
8092 end if;
8093
8094 if T_Name = Any_Id or else Etype (N) = Any_Type then
8095
8096 -- Undefined id. Make it into a valid type
8097
8098 Set_Entity (N, Any_Type);
8099
8100 elsif not Is_Type (T_Name)
8101 and then T_Name /= Standard_Void_Type
8102 then
8103 Error_Msg_Sloc := Sloc (T_Name);
8104 Error_Msg_N ("subtype mark required in this context", N);
8105 Error_Msg_NE ("\\found & declared#", N, T_Name);
8106 Set_Entity (N, Any_Type);
8107
8108 else
8109 -- If the type is an incomplete type created to handle
8110 -- anonymous access components of a record type, then the
8111 -- incomplete type is the visible entity and subsequent
8112 -- references will point to it. Mark the original full
8113 -- type as referenced, to prevent spurious warnings.
8114
8115 if Is_Incomplete_Type (T_Name)
8116 and then Present (Full_View (T_Name))
8117 and then not Comes_From_Source (T_Name)
8118 then
8119 Set_Referenced (Full_View (T_Name));
8120 end if;
8121
8122 T_Name := Get_Full_View (T_Name);
8123
8124 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
8125 -- limited-with clauses
8126
8127 if From_Limited_With (T_Name)
8128 and then Is_Incomplete_Type (T_Name)
8129 and then Present (Non_Limited_View (T_Name))
8130 and then Is_Interface (Non_Limited_View (T_Name))
8131 then
8132 T_Name := Non_Limited_View (T_Name);
8133 end if;
8134
8135 if In_Open_Scopes (T_Name) then
8136 if Ekind (Base_Type (T_Name)) = E_Task_Type then
8137
8138 -- In Ada 2005, a task name can be used in an access
8139 -- definition within its own body.
8140
8141 if Ada_Version >= Ada_2005
8142 and then Nkind (Parent (N)) = N_Access_Definition
8143 then
8144 Set_Entity (N, T_Name);
8145 Set_Etype (N, T_Name);
8146 return;
8147
8148 else
8149 Error_Msg_N
8150 ("task type cannot be used as type mark " &
8151 "within its own spec or body", N);
8152 end if;
8153
8154 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
8155
8156 -- In Ada 2005, a protected name can be used in an access
8157 -- definition within its own body.
8158
8159 if Ada_Version >= Ada_2005
8160 and then Nkind (Parent (N)) = N_Access_Definition
8161 then
8162 Set_Entity (N, T_Name);
8163 Set_Etype (N, T_Name);
8164 return;
8165
8166 else
8167 Error_Msg_N
8168 ("protected type cannot be used as type mark " &
8169 "within its own spec or body", N);
8170 end if;
8171
8172 else
8173 Error_Msg_N ("type declaration cannot refer to itself", N);
8174 end if;
8175
8176 Set_Etype (N, Any_Type);
8177 Set_Entity (N, Any_Type);
8178 Set_Error_Posted (T_Name);
8179 return;
8180 end if;
8181
8182 Set_Entity (N, T_Name);
8183 Set_Etype (N, T_Name);
8184 end if;
8185 end if;
8186
8187 if Present (Etype (N)) and then Comes_From_Source (N) then
8188 if Is_Fixed_Point_Type (Etype (N)) then
8189 Check_Restriction (No_Fixed_Point, N);
8190 elsif Is_Floating_Point_Type (Etype (N)) then
8191 Check_Restriction (No_Floating_Point, N);
8192 end if;
8193
8194 -- A Ghost type must appear in a specific context
8195
8196 if Is_Ghost_Entity (Etype (N)) then
8197 Check_Ghost_Context (Etype (N), N);
8198 end if;
8199 end if;
8200 end Find_Type;
8201
8202 --------------------
8203 -- Has_Components --
8204 --------------------
8205
8206 function Has_Components (Typ : Entity_Id) return Boolean is
8207 begin
8208 return Is_Record_Type (Typ)
8209 or else (Is_Private_Type (Typ) and then Has_Discriminants (Typ))
8210 or else (Is_Task_Type (Typ) and then Has_Discriminants (Typ))
8211 or else (Is_Incomplete_Type (Typ)
8212 and then From_Limited_With (Typ)
8213 and then Is_Record_Type (Available_View (Typ)));
8214 end Has_Components;
8215
8216 ------------------------------------
8217 -- Has_Implicit_Character_Literal --
8218 ------------------------------------
8219
8220 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
8221 Id : Entity_Id;
8222 Found : Boolean := False;
8223 P : constant Entity_Id := Entity (Prefix (N));
8224 Priv_Id : Entity_Id := Empty;
8225
8226 begin
8227 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8228 Priv_Id := First_Private_Entity (P);
8229 end if;
8230
8231 if P = Standard_Standard then
8232 Change_Selected_Component_To_Expanded_Name (N);
8233 Rewrite (N, Selector_Name (N));
8234 Analyze (N);
8235 Set_Etype (Original_Node (N), Standard_Character);
8236 return True;
8237 end if;
8238
8239 Id := First_Entity (P);
8240 while Present (Id) and then Id /= Priv_Id loop
8241 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
8242
8243 -- We replace the node with the literal itself, resolve as a
8244 -- character, and set the type correctly.
8245
8246 if not Found then
8247 Change_Selected_Component_To_Expanded_Name (N);
8248 Rewrite (N, Selector_Name (N));
8249 Analyze (N);
8250 Set_Etype (N, Id);
8251 Set_Etype (Original_Node (N), Id);
8252 Found := True;
8253
8254 else
8255 -- More than one type derived from Character in given scope.
8256 -- Collect all possible interpretations.
8257
8258 Add_One_Interp (N, Id, Id);
8259 end if;
8260 end if;
8261
8262 Next_Entity (Id);
8263 end loop;
8264
8265 return Found;
8266 end Has_Implicit_Character_Literal;
8267
8268 ----------------------
8269 -- Has_Private_With --
8270 ----------------------
8271
8272 function Has_Private_With (E : Entity_Id) return Boolean is
8273 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
8274 Item : Node_Id;
8275
8276 begin
8277 Item := First (Context_Items (Comp_Unit));
8278 while Present (Item) loop
8279 if Nkind (Item) = N_With_Clause
8280 and then Private_Present (Item)
8281 and then Entity (Name (Item)) = E
8282 then
8283 return True;
8284 end if;
8285
8286 Next (Item);
8287 end loop;
8288
8289 return False;
8290 end Has_Private_With;
8291
8292 ---------------------------
8293 -- Has_Implicit_Operator --
8294 ---------------------------
8295
8296 function Has_Implicit_Operator (N : Node_Id) return Boolean is
8297 Op_Id : constant Name_Id := Chars (Selector_Name (N));
8298 P : constant Entity_Id := Entity (Prefix (N));
8299 Id : Entity_Id;
8300 Priv_Id : Entity_Id := Empty;
8301
8302 procedure Add_Implicit_Operator
8303 (T : Entity_Id;
8304 Op_Type : Entity_Id := Empty);
8305 -- Add implicit interpretation to node N, using the type for which a
8306 -- predefined operator exists. If the operator yields a boolean type,
8307 -- the Operand_Type is implicitly referenced by the operator, and a
8308 -- reference to it must be generated.
8309
8310 ---------------------------
8311 -- Add_Implicit_Operator --
8312 ---------------------------
8313
8314 procedure Add_Implicit_Operator
8315 (T : Entity_Id;
8316 Op_Type : Entity_Id := Empty)
8317 is
8318 Predef_Op : Entity_Id;
8319
8320 begin
8321 Predef_Op := Current_Entity (Selector_Name (N));
8322 while Present (Predef_Op)
8323 and then Scope (Predef_Op) /= Standard_Standard
8324 loop
8325 Predef_Op := Homonym (Predef_Op);
8326 end loop;
8327
8328 if Nkind (N) = N_Selected_Component then
8329 Change_Selected_Component_To_Expanded_Name (N);
8330 end if;
8331
8332 -- If the context is an unanalyzed function call, determine whether
8333 -- a binary or unary interpretation is required.
8334
8335 if Nkind (Parent (N)) = N_Indexed_Component then
8336 declare
8337 Is_Binary_Call : constant Boolean :=
8338 Present
8339 (Next (First (Expressions (Parent (N)))));
8340 Is_Binary_Op : constant Boolean :=
8341 First_Entity
8342 (Predef_Op) /= Last_Entity (Predef_Op);
8343 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
8344
8345 begin
8346 if Is_Binary_Call then
8347 if Is_Binary_Op then
8348 Add_One_Interp (N, Predef_Op, T);
8349 else
8350 Add_One_Interp (N, Predef_Op2, T);
8351 end if;
8352 else
8353 if not Is_Binary_Op then
8354 Add_One_Interp (N, Predef_Op, T);
8355
8356 -- Predef_Op2 may be empty in case of previous errors
8357
8358 elsif Present (Predef_Op2) then
8359 Add_One_Interp (N, Predef_Op2, T);
8360 end if;
8361 end if;
8362 end;
8363
8364 else
8365 Add_One_Interp (N, Predef_Op, T);
8366
8367 -- For operators with unary and binary interpretations, if
8368 -- context is not a call, add both
8369
8370 if Present (Homonym (Predef_Op)) then
8371 Add_One_Interp (N, Homonym (Predef_Op), T);
8372 end if;
8373 end if;
8374
8375 -- The node is a reference to a predefined operator, and
8376 -- an implicit reference to the type of its operands.
8377
8378 if Present (Op_Type) then
8379 Generate_Operator_Reference (N, Op_Type);
8380 else
8381 Generate_Operator_Reference (N, T);
8382 end if;
8383 end Add_Implicit_Operator;
8384
8385 -- Start of processing for Has_Implicit_Operator
8386
8387 begin
8388 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8389 Priv_Id := First_Private_Entity (P);
8390 end if;
8391
8392 Id := First_Entity (P);
8393
8394 case Op_Id is
8395
8396 -- Boolean operators: an implicit declaration exists if the scope
8397 -- contains a declaration for a derived Boolean type, or for an
8398 -- array of Boolean type.
8399
8400 when Name_Op_And
8401 | Name_Op_Not
8402 | Name_Op_Or
8403 | Name_Op_Xor
8404 =>
8405 while Id /= Priv_Id loop
8406 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
8407 Add_Implicit_Operator (Id);
8408 return True;
8409 end if;
8410
8411 Next_Entity (Id);
8412 end loop;
8413
8414 -- Equality: look for any non-limited type (result is Boolean)
8415
8416 when Name_Op_Eq
8417 | Name_Op_Ne
8418 =>
8419 while Id /= Priv_Id loop
8420 if Is_Type (Id)
8421 and then not Is_Limited_Type (Id)
8422 and then Is_Base_Type (Id)
8423 then
8424 Add_Implicit_Operator (Standard_Boolean, Id);
8425 return True;
8426 end if;
8427
8428 Next_Entity (Id);
8429 end loop;
8430
8431 -- Comparison operators: scalar type, or array of scalar
8432
8433 when Name_Op_Ge
8434 | Name_Op_Gt
8435 | Name_Op_Le
8436 | Name_Op_Lt
8437 =>
8438 while Id /= Priv_Id loop
8439 if (Is_Scalar_Type (Id)
8440 or else (Is_Array_Type (Id)
8441 and then Is_Scalar_Type (Component_Type (Id))))
8442 and then Is_Base_Type (Id)
8443 then
8444 Add_Implicit_Operator (Standard_Boolean, Id);
8445 return True;
8446 end if;
8447
8448 Next_Entity (Id);
8449 end loop;
8450
8451 -- Arithmetic operators: any numeric type
8452
8453 when Name_Op_Abs
8454 | Name_Op_Add
8455 | Name_Op_Divide
8456 | Name_Op_Expon
8457 | Name_Op_Mod
8458 | Name_Op_Multiply
8459 | Name_Op_Rem
8460 | Name_Op_Subtract
8461 =>
8462 while Id /= Priv_Id loop
8463 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
8464 Add_Implicit_Operator (Id);
8465 return True;
8466 end if;
8467
8468 Next_Entity (Id);
8469 end loop;
8470
8471 -- Concatenation: any one-dimensional array type
8472
8473 when Name_Op_Concat =>
8474 while Id /= Priv_Id loop
8475 if Is_Array_Type (Id)
8476 and then Number_Dimensions (Id) = 1
8477 and then Is_Base_Type (Id)
8478 then
8479 Add_Implicit_Operator (Id);
8480 return True;
8481 end if;
8482
8483 Next_Entity (Id);
8484 end loop;
8485
8486 -- What is the others condition here? Should we be using a
8487 -- subtype of Name_Id that would restrict to operators ???
8488
8489 when others =>
8490 null;
8491 end case;
8492
8493 -- If we fall through, then we do not have an implicit operator
8494
8495 return False;
8496 end Has_Implicit_Operator;
8497
8498 -----------------------------------
8499 -- Has_Loop_In_Inner_Open_Scopes --
8500 -----------------------------------
8501
8502 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
8503 begin
8504 -- Several scope stacks are maintained by Scope_Stack. The base of the
8505 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8506 -- flag in the scope stack entry. Note that the scope stacks used to
8507 -- simply be delimited implicitly by the presence of Standard_Standard
8508 -- at their base, but there now are cases where this is not sufficient
8509 -- because Standard_Standard actually may appear in the middle of the
8510 -- active set of scopes.
8511
8512 for J in reverse 0 .. Scope_Stack.Last loop
8513
8514 -- S was reached without seing a loop scope first
8515
8516 if Scope_Stack.Table (J).Entity = S then
8517 return False;
8518
8519 -- S was not yet reached, so it contains at least one inner loop
8520
8521 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
8522 return True;
8523 end if;
8524
8525 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8526 -- cases where Standard_Standard appears in the middle of the active
8527 -- set of scopes. This affects the declaration and overriding of
8528 -- private inherited operations in instantiations of generic child
8529 -- units.
8530
8531 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
8532 end loop;
8533
8534 raise Program_Error; -- unreachable
8535 end Has_Loop_In_Inner_Open_Scopes;
8536
8537 --------------------
8538 -- In_Open_Scopes --
8539 --------------------
8540
8541 function In_Open_Scopes (S : Entity_Id) return Boolean is
8542 begin
8543 -- Several scope stacks are maintained by Scope_Stack. The base of the
8544 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8545 -- flag in the scope stack entry. Note that the scope stacks used to
8546 -- simply be delimited implicitly by the presence of Standard_Standard
8547 -- at their base, but there now are cases where this is not sufficient
8548 -- because Standard_Standard actually may appear in the middle of the
8549 -- active set of scopes.
8550
8551 for J in reverse 0 .. Scope_Stack.Last loop
8552 if Scope_Stack.Table (J).Entity = S then
8553 return True;
8554 end if;
8555
8556 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8557 -- cases where Standard_Standard appears in the middle of the active
8558 -- set of scopes. This affects the declaration and overriding of
8559 -- private inherited operations in instantiations of generic child
8560 -- units.
8561
8562 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
8563 end loop;
8564
8565 return False;
8566 end In_Open_Scopes;
8567
8568 -----------------------------
8569 -- Inherit_Renamed_Profile --
8570 -----------------------------
8571
8572 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
8573 New_F : Entity_Id;
8574 Old_F : Entity_Id;
8575 Old_T : Entity_Id;
8576 New_T : Entity_Id;
8577
8578 begin
8579 if Ekind (Old_S) = E_Operator then
8580 New_F := First_Formal (New_S);
8581
8582 while Present (New_F) loop
8583 Set_Etype (New_F, Base_Type (Etype (New_F)));
8584 Next_Formal (New_F);
8585 end loop;
8586
8587 Set_Etype (New_S, Base_Type (Etype (New_S)));
8588
8589 else
8590 New_F := First_Formal (New_S);
8591 Old_F := First_Formal (Old_S);
8592
8593 while Present (New_F) loop
8594 New_T := Etype (New_F);
8595 Old_T := Etype (Old_F);
8596
8597 -- If the new type is a renaming of the old one, as is the case
8598 -- for actuals in instances, retain its name, to simplify later
8599 -- disambiguation.
8600
8601 if Nkind (Parent (New_T)) = N_Subtype_Declaration
8602 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
8603 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
8604 then
8605 null;
8606 else
8607 Set_Etype (New_F, Old_T);
8608 end if;
8609
8610 Next_Formal (New_F);
8611 Next_Formal (Old_F);
8612 end loop;
8613
8614 pragma Assert (No (Old_F));
8615
8616 if Ekind (Old_S) in E_Function | E_Enumeration_Literal then
8617 Set_Etype (New_S, Etype (Old_S));
8618 end if;
8619 end if;
8620 end Inherit_Renamed_Profile;
8621
8622 ----------------
8623 -- Initialize --
8624 ----------------
8625
8626 procedure Initialize is
8627 begin
8628 Urefs.Init;
8629 end Initialize;
8630
8631 -------------------------
8632 -- Install_Use_Clauses --
8633 -------------------------
8634
8635 procedure Install_Use_Clauses
8636 (Clause : Node_Id;
8637 Force_Installation : Boolean := False)
8638 is
8639 U : Node_Id;
8640
8641 begin
8642 U := Clause;
8643 while Present (U) loop
8644
8645 -- Case of USE package
8646
8647 if Nkind (U) = N_Use_Package_Clause then
8648 Use_One_Package (U, Name (U), True);
8649
8650 -- Case of USE TYPE
8651
8652 else
8653 Use_One_Type (Subtype_Mark (U), Force => Force_Installation);
8654
8655 end if;
8656
8657 Next_Use_Clause (U);
8658 end loop;
8659 end Install_Use_Clauses;
8660
8661 ----------------------
8662 -- Mark_Use_Clauses --
8663 ----------------------
8664
8665 procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is
8666 procedure Mark_Parameters (Call : Entity_Id);
8667 -- Perform use_type_clause marking for all parameters in a subprogram
8668 -- or operator call.
8669
8670 procedure Mark_Use_Package (Pak : Entity_Id);
8671 -- Move up the Prev_Use_Clause chain for packages denoted by Pak -
8672 -- marking each clause in the chain as effective in the process.
8673
8674 procedure Mark_Use_Type (E : Entity_Id);
8675 -- Similar to Do_Use_Package_Marking except we move up the
8676 -- Prev_Use_Clause chain for the type denoted by E.
8677
8678 ---------------------
8679 -- Mark_Parameters --
8680 ---------------------
8681
8682 procedure Mark_Parameters (Call : Entity_Id) is
8683 Curr : Node_Id;
8684
8685 begin
8686 -- Move through all of the formals
8687
8688 Curr := First_Formal (Call);
8689 while Present (Curr) loop
8690 Mark_Use_Type (Curr);
8691
8692 Next_Formal (Curr);
8693 end loop;
8694
8695 -- Handle the return type
8696
8697 Mark_Use_Type (Call);
8698 end Mark_Parameters;
8699
8700 ----------------------
8701 -- Mark_Use_Package --
8702 ----------------------
8703
8704 procedure Mark_Use_Package (Pak : Entity_Id) is
8705 Curr : Node_Id;
8706
8707 begin
8708 -- Ignore cases where the scope of the type is not a package (e.g.
8709 -- Standard_Standard).
8710
8711 if Ekind (Pak) /= E_Package then
8712 return;
8713 end if;
8714
8715 Curr := Current_Use_Clause (Pak);
8716 while Present (Curr)
8717 and then not Is_Effective_Use_Clause (Curr)
8718 loop
8719 -- We need to mark the previous use clauses as effective, but
8720 -- each use clause may in turn render other use_package_clauses
8721 -- effective. Additionally, it is possible to have a parent
8722 -- package renamed as a child of itself so we must check the
8723 -- prefix entity is not the same as the package we are marking.
8724
8725 if Nkind (Name (Curr)) /= N_Identifier
8726 and then Present (Prefix (Name (Curr)))
8727 and then Entity (Prefix (Name (Curr))) /= Pak
8728 then
8729 Mark_Use_Package (Entity (Prefix (Name (Curr))));
8730
8731 -- It is also possible to have a child package without a prefix
8732 -- that relies on a previous use_package_clause.
8733
8734 elsif Nkind (Name (Curr)) = N_Identifier
8735 and then Is_Child_Unit (Entity (Name (Curr)))
8736 then
8737 Mark_Use_Package (Scope (Entity (Name (Curr))));
8738 end if;
8739
8740 -- Mark the use_package_clause as effective and move up the chain
8741
8742 Set_Is_Effective_Use_Clause (Curr);
8743
8744 Curr := Prev_Use_Clause (Curr);
8745 end loop;
8746 end Mark_Use_Package;
8747
8748 -------------------
8749 -- Mark_Use_Type --
8750 -------------------
8751
8752 procedure Mark_Use_Type (E : Entity_Id) is
8753 Curr : Node_Id;
8754 Base : Entity_Id;
8755
8756 begin
8757 -- Ignore void types and unresolved string literals and primitives
8758
8759 if Nkind (E) = N_String_Literal
8760 or else Nkind (Etype (E)) not in N_Entity
8761 or else not Is_Type (Etype (E))
8762 then
8763 return;
8764 end if;
8765
8766 -- Primitives with class-wide operands might additionally render
8767 -- their base type's use_clauses effective - so do a recursive check
8768 -- here.
8769
8770 Base := Base_Type (Etype (E));
8771
8772 if Ekind (Base) = E_Class_Wide_Type then
8773 Mark_Use_Type (Base);
8774 end if;
8775
8776 -- The package containing the type or operator function being used
8777 -- may be in use as well, so mark any use_package_clauses for it as
8778 -- effective. There are also additional sanity checks performed here
8779 -- for ignoring previous errors.
8780
8781 Mark_Use_Package (Scope (Base));
8782
8783 if Nkind (E) in N_Op
8784 and then Present (Entity (E))
8785 and then Present (Scope (Entity (E)))
8786 then
8787 Mark_Use_Package (Scope (Entity (E)));
8788 end if;
8789
8790 Curr := Current_Use_Clause (Base);
8791 while Present (Curr)
8792 and then not Is_Effective_Use_Clause (Curr)
8793 loop
8794 -- Current use_type_clause may render other use_package_clauses
8795 -- effective.
8796
8797 if Nkind (Subtype_Mark (Curr)) /= N_Identifier
8798 and then Present (Prefix (Subtype_Mark (Curr)))
8799 then
8800 Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr))));
8801 end if;
8802
8803 -- Mark the use_type_clause as effective and move up the chain
8804
8805 Set_Is_Effective_Use_Clause (Curr);
8806
8807 Curr := Prev_Use_Clause (Curr);
8808 end loop;
8809 end Mark_Use_Type;
8810
8811 -- Start of processing for Mark_Use_Clauses
8812
8813 begin
8814 -- Use clauses in and of themselves do not count as a "use" of a
8815 -- package.
8816
8817 if Nkind (Parent (Id)) in N_Use_Package_Clause | N_Use_Type_Clause then
8818 return;
8819 end if;
8820
8821 -- Handle entities
8822
8823 if Nkind (Id) in N_Entity then
8824
8825 -- Mark the entity's package
8826
8827 if Is_Potentially_Use_Visible (Id) then
8828 Mark_Use_Package (Scope (Id));
8829 end if;
8830
8831 -- Mark enumeration literals
8832
8833 if Ekind (Id) = E_Enumeration_Literal then
8834 Mark_Use_Type (Id);
8835
8836 -- Mark primitives
8837
8838 elsif (Is_Overloadable (Id)
8839 or else Is_Generic_Subprogram (Id))
8840 and then (Is_Potentially_Use_Visible (Id)
8841 or else Is_Intrinsic_Subprogram (Id)
8842 or else (Ekind (Id) in E_Function | E_Procedure
8843 and then Is_Generic_Actual_Subprogram (Id)))
8844 then
8845 Mark_Parameters (Id);
8846 end if;
8847
8848 -- Handle nodes
8849
8850 else
8851 -- Mark operators
8852
8853 if Nkind (Id) in N_Op then
8854
8855 -- At this point the left operand may not be resolved if we are
8856 -- encountering multiple operators next to eachother in an
8857 -- expression.
8858
8859 if Nkind (Id) in N_Binary_Op
8860 and then not (Nkind (Left_Opnd (Id)) in N_Op)
8861 then
8862 Mark_Use_Type (Left_Opnd (Id));
8863 end if;
8864
8865 Mark_Use_Type (Right_Opnd (Id));
8866 Mark_Use_Type (Id);
8867
8868 -- Mark entity identifiers
8869
8870 elsif Nkind (Id) in N_Has_Entity
8871 and then (Is_Potentially_Use_Visible (Entity (Id))
8872 or else (Is_Generic_Instance (Entity (Id))
8873 and then Is_Immediately_Visible (Entity (Id))))
8874 then
8875 -- Ignore fully qualified names as they do not count as a "use" of
8876 -- a package.
8877
8878 if Nkind (Id) in N_Identifier | N_Operator_Symbol
8879 or else (Present (Prefix (Id))
8880 and then Scope (Entity (Id)) /= Entity (Prefix (Id)))
8881 then
8882 Mark_Use_Clauses (Entity (Id));
8883 end if;
8884 end if;
8885 end if;
8886 end Mark_Use_Clauses;
8887
8888 --------------------------------
8889 -- Most_Descendant_Use_Clause --
8890 --------------------------------
8891
8892 function Most_Descendant_Use_Clause
8893 (Clause1 : Entity_Id;
8894 Clause2 : Entity_Id) return Entity_Id
8895 is
8896 Scope1 : Entity_Id;
8897 Scope2 : Entity_Id;
8898
8899 begin
8900 if Clause1 = Clause2 then
8901 return Clause1;
8902 end if;
8903
8904 -- We determine which one is the most descendant by the scope distance
8905 -- to the ultimate parent unit.
8906
8907 Scope1 := Entity_Of_Unit (Unit (Parent (Clause1)));
8908 Scope2 := Entity_Of_Unit (Unit (Parent (Clause2)));
8909 while Scope1 /= Standard_Standard
8910 and then Scope2 /= Standard_Standard
8911 loop
8912 Scope1 := Scope (Scope1);
8913 Scope2 := Scope (Scope2);
8914
8915 if not Present (Scope1) then
8916 return Clause1;
8917 elsif not Present (Scope2) then
8918 return Clause2;
8919 end if;
8920 end loop;
8921
8922 if Scope1 = Standard_Standard then
8923 return Clause1;
8924 end if;
8925
8926 return Clause2;
8927 end Most_Descendant_Use_Clause;
8928
8929 ---------------
8930 -- Pop_Scope --
8931 ---------------
8932
8933 procedure Pop_Scope is
8934 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8935 S : constant Entity_Id := SST.Entity;
8936
8937 begin
8938 if Debug_Flag_E then
8939 Write_Info;
8940 end if;
8941
8942 -- Set Default_Storage_Pool field of the library unit if necessary
8943
8944 if Is_Package_Or_Generic_Package (S)
8945 and then
8946 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
8947 then
8948 declare
8949 Aux : constant Node_Id :=
8950 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
8951 begin
8952 if No (Default_Storage_Pool (Aux)) then
8953 Set_Default_Storage_Pool (Aux, Default_Pool);
8954 end if;
8955 end;
8956 end if;
8957
8958 Scope_Suppress := SST.Save_Scope_Suppress;
8959 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
8960 Check_Policy_List := SST.Save_Check_Policy_List;
8961 Default_Pool := SST.Save_Default_Storage_Pool;
8962 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
8963 SPARK_Mode := SST.Save_SPARK_Mode;
8964 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
8965 Default_SSO := SST.Save_Default_SSO;
8966 Uneval_Old := SST.Save_Uneval_Old;
8967
8968 if Debug_Flag_W then
8969 Write_Str ("<-- exiting scope: ");
8970 Write_Name (Chars (Current_Scope));
8971 Write_Str (", Depth=");
8972 Write_Int (Int (Scope_Stack.Last));
8973 Write_Eol;
8974 end if;
8975
8976 End_Use_Clauses (SST.First_Use_Clause);
8977
8978 -- If the actions to be wrapped are still there they will get lost
8979 -- causing incomplete code to be generated. It is better to abort in
8980 -- this case (and we do the abort even with assertions off since the
8981 -- penalty is incorrect code generation).
8982
8983 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
8984 raise Program_Error;
8985 end if;
8986
8987 -- Free last subprogram name if allocated, and pop scope
8988
8989 Free (SST.Last_Subprogram_Name);
8990 Scope_Stack.Decrement_Last;
8991 end Pop_Scope;
8992
8993 ----------------
8994 -- Push_Scope --
8995 ----------------
8996
8997 procedure Push_Scope (S : Entity_Id) is
8998 E : constant Entity_Id := Scope (S);
8999
9000 begin
9001 if Ekind (S) = E_Void then
9002 null;
9003
9004 -- Set scope depth if not a non-concurrent type, and we have not yet set
9005 -- the scope depth. This means that we have the first occurrence of the
9006 -- scope, and this is where the depth is set.
9007
9008 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
9009 and then not Scope_Depth_Set (S)
9010 then
9011 if S = Standard_Standard then
9012 Set_Scope_Depth_Value (S, Uint_0);
9013
9014 elsif Is_Child_Unit (S) then
9015 Set_Scope_Depth_Value (S, Uint_1);
9016
9017 elsif not Is_Record_Type (Current_Scope) then
9018 if Ekind (S) = E_Loop then
9019 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
9020 else
9021 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
9022 end if;
9023 end if;
9024 end if;
9025
9026 Scope_Stack.Increment_Last;
9027
9028 declare
9029 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
9030
9031 begin
9032 SST.Entity := S;
9033 SST.Save_Scope_Suppress := Scope_Suppress;
9034 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
9035 SST.Save_Check_Policy_List := Check_Policy_List;
9036 SST.Save_Default_Storage_Pool := Default_Pool;
9037 SST.Save_No_Tagged_Streams := No_Tagged_Streams;
9038 SST.Save_SPARK_Mode := SPARK_Mode;
9039 SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma;
9040 SST.Save_Default_SSO := Default_SSO;
9041 SST.Save_Uneval_Old := Uneval_Old;
9042
9043 -- Each new scope pushed onto the scope stack inherits the component
9044 -- alignment of the previous scope. This emulates the "visibility"
9045 -- semantics of pragma Component_Alignment.
9046
9047 if Scope_Stack.Last > Scope_Stack.First then
9048 SST.Component_Alignment_Default :=
9049 Scope_Stack.Table
9050 (Scope_Stack.Last - 1).Component_Alignment_Default;
9051
9052 -- Otherwise, this is the first scope being pushed on the scope
9053 -- stack. Inherit the component alignment from the configuration
9054 -- form of pragma Component_Alignment (if any).
9055
9056 else
9057 SST.Component_Alignment_Default :=
9058 Configuration_Component_Alignment;
9059 end if;
9060
9061 SST.Last_Subprogram_Name := null;
9062 SST.Is_Transient := False;
9063 SST.Node_To_Be_Wrapped := Empty;
9064 SST.Pending_Freeze_Actions := No_List;
9065 SST.Actions_To_Be_Wrapped := (others => No_List);
9066 SST.First_Use_Clause := Empty;
9067 SST.Is_Active_Stack_Base := False;
9068 SST.Previous_Visibility := False;
9069 SST.Locked_Shared_Objects := No_Elist;
9070 end;
9071
9072 if Debug_Flag_W then
9073 Write_Str ("--> new scope: ");
9074 Write_Name (Chars (Current_Scope));
9075 Write_Str (", Id=");
9076 Write_Int (Int (Current_Scope));
9077 Write_Str (", Depth=");
9078 Write_Int (Int (Scope_Stack.Last));
9079 Write_Eol;
9080 end if;
9081
9082 -- Deal with copying flags from the previous scope to this one. This is
9083 -- not necessary if either scope is standard, or if the new scope is a
9084 -- child unit.
9085
9086 if S /= Standard_Standard
9087 and then Scope (S) /= Standard_Standard
9088 and then not Is_Child_Unit (S)
9089 then
9090 if Nkind (E) not in N_Entity then
9091 return;
9092 end if;
9093
9094 -- Copy categorization flags from Scope (S) to S, this is not done
9095 -- when Scope (S) is Standard_Standard since propagation is from
9096 -- library unit entity inwards. Copy other relevant attributes as
9097 -- well (Discard_Names in particular).
9098
9099 -- We only propagate inwards for library level entities,
9100 -- inner level subprograms do not inherit the categorization.
9101
9102 if Is_Library_Level_Entity (S) then
9103 Set_Is_Preelaborated (S, Is_Preelaborated (E));
9104 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
9105 Set_Discard_Names (S, Discard_Names (E));
9106 Set_Suppress_Value_Tracking_On_Call
9107 (S, Suppress_Value_Tracking_On_Call (E));
9108 Set_Categorization_From_Scope (E => S, Scop => E);
9109 end if;
9110 end if;
9111
9112 if Is_Child_Unit (S)
9113 and then Present (E)
9114 and then Is_Package_Or_Generic_Package (E)
9115 and then
9116 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9117 then
9118 declare
9119 Aux : constant Node_Id :=
9120 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
9121 begin
9122 if Present (Default_Storage_Pool (Aux)) then
9123 Default_Pool := Default_Storage_Pool (Aux);
9124 end if;
9125 end;
9126 end if;
9127 end Push_Scope;
9128
9129 ---------------------
9130 -- Premature_Usage --
9131 ---------------------
9132
9133 procedure Premature_Usage (N : Node_Id) is
9134 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
9135 E : Entity_Id := Entity (N);
9136
9137 begin
9138 -- Within an instance, the analysis of the actual for a formal object
9139 -- does not see the name of the object itself. This is significant only
9140 -- if the object is an aggregate, where its analysis does not do any
9141 -- name resolution on component associations. (see 4717-008). In such a
9142 -- case, look for the visible homonym on the chain.
9143
9144 if In_Instance and then Present (Homonym (E)) then
9145 E := Homonym (E);
9146 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
9147 E := Homonym (E);
9148 end loop;
9149
9150 if Present (E) then
9151 Set_Entity (N, E);
9152 Set_Etype (N, Etype (E));
9153 return;
9154 end if;
9155 end if;
9156
9157 case Kind is
9158 when N_Component_Declaration =>
9159 Error_Msg_N
9160 ("component&! cannot be used before end of record declaration",
9161 N);
9162
9163 when N_Parameter_Specification =>
9164 Error_Msg_N
9165 ("formal parameter&! cannot be used before end of specification",
9166 N);
9167
9168 when N_Discriminant_Specification =>
9169 Error_Msg_N
9170 ("discriminant&! cannot be used before end of discriminant part",
9171 N);
9172
9173 when N_Procedure_Specification | N_Function_Specification =>
9174 Error_Msg_N
9175 ("subprogram&! cannot be used before end of its declaration",
9176 N);
9177
9178 when N_Full_Type_Declaration | N_Subtype_Declaration =>
9179 Error_Msg_N
9180 ("type& cannot be used before end of its declaration!", N);
9181
9182 when others =>
9183 Error_Msg_N
9184 ("object& cannot be used before end of its declaration!", N);
9185
9186 -- If the premature reference appears as the expression in its own
9187 -- declaration, rewrite it to prevent compiler loops in subsequent
9188 -- uses of this mangled declaration in address clauses.
9189
9190 if Nkind (Parent (N)) = N_Object_Declaration then
9191 Set_Entity (N, Any_Id);
9192 end if;
9193 end case;
9194 end Premature_Usage;
9195
9196 ------------------------
9197 -- Present_System_Aux --
9198 ------------------------
9199
9200 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
9201 Loc : Source_Ptr;
9202 Aux_Name : Unit_Name_Type;
9203 Unum : Unit_Number_Type;
9204 Withn : Node_Id;
9205 With_Sys : Node_Id;
9206 The_Unit : Node_Id;
9207
9208 function Find_System (C_Unit : Node_Id) return Entity_Id;
9209 -- Scan context clause of compilation unit to find with_clause
9210 -- for System.
9211
9212 -----------------
9213 -- Find_System --
9214 -----------------
9215
9216 function Find_System (C_Unit : Node_Id) return Entity_Id is
9217 With_Clause : Node_Id;
9218
9219 begin
9220 With_Clause := First (Context_Items (C_Unit));
9221 while Present (With_Clause) loop
9222 if (Nkind (With_Clause) = N_With_Clause
9223 and then Chars (Name (With_Clause)) = Name_System)
9224 and then Comes_From_Source (With_Clause)
9225 then
9226 return With_Clause;
9227 end if;
9228
9229 Next (With_Clause);
9230 end loop;
9231
9232 return Empty;
9233 end Find_System;
9234
9235 -- Start of processing for Present_System_Aux
9236
9237 begin
9238 -- The child unit may have been loaded and analyzed already
9239
9240 if Present (System_Aux_Id) then
9241 return True;
9242
9243 -- If no previous pragma for System.Aux, nothing to load
9244
9245 elsif No (System_Extend_Unit) then
9246 return False;
9247
9248 -- Use the unit name given in the pragma to retrieve the unit.
9249 -- Verify that System itself appears in the context clause of the
9250 -- current compilation. If System is not present, an error will
9251 -- have been reported already.
9252
9253 else
9254 With_Sys := Find_System (Cunit (Current_Sem_Unit));
9255
9256 The_Unit := Unit (Cunit (Current_Sem_Unit));
9257
9258 if No (With_Sys)
9259 and then
9260 (Nkind (The_Unit) = N_Package_Body
9261 or else (Nkind (The_Unit) = N_Subprogram_Body
9262 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
9263 then
9264 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
9265 end if;
9266
9267 if No (With_Sys) and then Present (N) then
9268
9269 -- If we are compiling a subunit, we need to examine its
9270 -- context as well (Current_Sem_Unit is the parent unit);
9271
9272 The_Unit := Parent (N);
9273 while Nkind (The_Unit) /= N_Compilation_Unit loop
9274 The_Unit := Parent (The_Unit);
9275 end loop;
9276
9277 if Nkind (Unit (The_Unit)) = N_Subunit then
9278 With_Sys := Find_System (The_Unit);
9279 end if;
9280 end if;
9281
9282 if No (With_Sys) then
9283 return False;
9284 end if;
9285
9286 Loc := Sloc (With_Sys);
9287 Get_Name_String (Chars (Expression (System_Extend_Unit)));
9288 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
9289 Name_Buffer (1 .. 7) := "system.";
9290 Name_Buffer (Name_Len + 8) := '%';
9291 Name_Buffer (Name_Len + 9) := 's';
9292 Name_Len := Name_Len + 9;
9293 Aux_Name := Name_Find;
9294
9295 Unum :=
9296 Load_Unit
9297 (Load_Name => Aux_Name,
9298 Required => False,
9299 Subunit => False,
9300 Error_Node => With_Sys);
9301
9302 if Unum /= No_Unit then
9303 Semantics (Cunit (Unum));
9304 System_Aux_Id :=
9305 Defining_Entity (Specification (Unit (Cunit (Unum))));
9306
9307 Withn :=
9308 Make_With_Clause (Loc,
9309 Name =>
9310 Make_Expanded_Name (Loc,
9311 Chars => Chars (System_Aux_Id),
9312 Prefix =>
9313 New_Occurrence_Of (Scope (System_Aux_Id), Loc),
9314 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
9315
9316 Set_Entity (Name (Withn), System_Aux_Id);
9317
9318 Set_Corresponding_Spec (Withn, System_Aux_Id);
9319 Set_First_Name (Withn);
9320 Set_Implicit_With (Withn);
9321 Set_Library_Unit (Withn, Cunit (Unum));
9322
9323 Insert_After (With_Sys, Withn);
9324 Mark_Rewrite_Insertion (Withn);
9325 Set_Context_Installed (Withn);
9326
9327 return True;
9328
9329 -- Here if unit load failed
9330
9331 else
9332 Error_Msg_Name_1 := Name_System;
9333 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
9334 Error_Msg_N
9335 ("extension package `%.%` does not exist",
9336 Opt.System_Extend_Unit);
9337 return False;
9338 end if;
9339 end if;
9340 end Present_System_Aux;
9341
9342 -------------------------
9343 -- Restore_Scope_Stack --
9344 -------------------------
9345
9346 procedure Restore_Scope_Stack
9347 (List : Elist_Id;
9348 Handle_Use : Boolean := True)
9349 is
9350 SS_Last : constant Int := Scope_Stack.Last;
9351 Elmt : Elmt_Id;
9352
9353 begin
9354 -- Restore visibility of previous scope stack, if any, using the list
9355 -- we saved (we use Remove, since this list will not be used again).
9356
9357 loop
9358 Elmt := Last_Elmt (List);
9359 exit when Elmt = No_Elmt;
9360 Set_Is_Immediately_Visible (Node (Elmt));
9361 Remove_Last_Elmt (List);
9362 end loop;
9363
9364 -- Restore use clauses
9365
9366 if SS_Last >= Scope_Stack.First
9367 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9368 and then Handle_Use
9369 then
9370 Install_Use_Clauses
9371 (Scope_Stack.Table (SS_Last).First_Use_Clause,
9372 Force_Installation => True);
9373 end if;
9374 end Restore_Scope_Stack;
9375
9376 ----------------------
9377 -- Save_Scope_Stack --
9378 ----------------------
9379
9380 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
9381 -- consuming any memory. That is, Save_Scope_Stack took care of removing
9382 -- from immediate visibility entities and Restore_Scope_Stack took care
9383 -- of restoring their visibility analyzing the context of each entity. The
9384 -- problem of such approach is that it was fragile and caused unexpected
9385 -- visibility problems, and indeed one test was found where there was a
9386 -- real problem.
9387
9388 -- Furthermore, the following experiment was carried out:
9389
9390 -- - Save_Scope_Stack was modified to store in an Elist1 all those
9391 -- entities whose attribute Is_Immediately_Visible is modified
9392 -- from True to False.
9393
9394 -- - Restore_Scope_Stack was modified to store in another Elist2
9395 -- all the entities whose attribute Is_Immediately_Visible is
9396 -- modified from False to True.
9397
9398 -- - Extra code was added to verify that all the elements of Elist1
9399 -- are found in Elist2
9400
9401 -- This test shows that there may be more occurrences of this problem which
9402 -- have not yet been detected. As a result, we replaced that approach by
9403 -- the current one in which Save_Scope_Stack returns the list of entities
9404 -- whose visibility is changed, and that list is passed to Restore_Scope_
9405 -- Stack to undo that change. This approach is simpler and safer, although
9406 -- it consumes more memory.
9407
9408 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
9409 Result : constant Elist_Id := New_Elmt_List;
9410 E : Entity_Id;
9411 S : Entity_Id;
9412 SS_Last : constant Int := Scope_Stack.Last;
9413
9414 procedure Remove_From_Visibility (E : Entity_Id);
9415 -- If E is immediately visible then append it to the result and remove
9416 -- it temporarily from visibility.
9417
9418 ----------------------------
9419 -- Remove_From_Visibility --
9420 ----------------------------
9421
9422 procedure Remove_From_Visibility (E : Entity_Id) is
9423 begin
9424 if Is_Immediately_Visible (E) then
9425 Append_Elmt (E, Result);
9426 Set_Is_Immediately_Visible (E, False);
9427 end if;
9428 end Remove_From_Visibility;
9429
9430 -- Start of processing for Save_Scope_Stack
9431
9432 begin
9433 if SS_Last >= Scope_Stack.First
9434 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9435 then
9436 if Handle_Use then
9437 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
9438 end if;
9439
9440 -- If the call is from within a compilation unit, as when called from
9441 -- Rtsfind, make current entries in scope stack invisible while we
9442 -- analyze the new unit.
9443
9444 for J in reverse 0 .. SS_Last loop
9445 exit when Scope_Stack.Table (J).Entity = Standard_Standard
9446 or else No (Scope_Stack.Table (J).Entity);
9447
9448 S := Scope_Stack.Table (J).Entity;
9449
9450 Remove_From_Visibility (S);
9451
9452 E := First_Entity (S);
9453 while Present (E) loop
9454 Remove_From_Visibility (E);
9455 Next_Entity (E);
9456 end loop;
9457 end loop;
9458
9459 end if;
9460
9461 return Result;
9462 end Save_Scope_Stack;
9463
9464 -------------
9465 -- Set_Use --
9466 -------------
9467
9468 procedure Set_Use (L : List_Id) is
9469 Decl : Node_Id;
9470
9471 begin
9472 if Present (L) then
9473 Decl := First (L);
9474 while Present (Decl) loop
9475 if Nkind (Decl) = N_Use_Package_Clause then
9476 Chain_Use_Clause (Decl);
9477 Use_One_Package (Decl, Name (Decl));
9478
9479 elsif Nkind (Decl) = N_Use_Type_Clause then
9480 Chain_Use_Clause (Decl);
9481 Use_One_Type (Subtype_Mark (Decl));
9482
9483 end if;
9484
9485 Next (Decl);
9486 end loop;
9487 end if;
9488 end Set_Use;
9489
9490 -----------------------------
9491 -- Update_Use_Clause_Chain --
9492 -----------------------------
9493
9494 procedure Update_Use_Clause_Chain is
9495
9496 procedure Update_Chain_In_Scope (Level : Int);
9497 -- Iterate through one level in the scope stack verifying each use-type
9498 -- clause within said level is used then reset the Current_Use_Clause
9499 -- to a redundant use clause outside of the current ending scope if such
9500 -- a clause exists.
9501
9502 ---------------------------
9503 -- Update_Chain_In_Scope --
9504 ---------------------------
9505
9506 procedure Update_Chain_In_Scope (Level : Int) is
9507 Curr : Node_Id;
9508 N : Node_Id;
9509
9510 begin
9511 -- Loop through all use clauses within the scope dictated by Level
9512
9513 Curr := Scope_Stack.Table (Level).First_Use_Clause;
9514 while Present (Curr) loop
9515
9516 -- Retrieve the subtype mark or name within the current current
9517 -- use clause.
9518
9519 if Nkind (Curr) = N_Use_Type_Clause then
9520 N := Subtype_Mark (Curr);
9521 else
9522 N := Name (Curr);
9523 end if;
9524
9525 -- If warnings for unreferenced entities are enabled and the
9526 -- current use clause has not been marked effective.
9527
9528 if Check_Unreferenced
9529 and then Comes_From_Source (Curr)
9530 and then not Is_Effective_Use_Clause (Curr)
9531 and then not In_Instance
9532 and then not In_Inlined_Body
9533 then
9534 -- We are dealing with a potentially unused use_package_clause
9535
9536 if Nkind (Curr) = N_Use_Package_Clause then
9537
9538 -- Renamings and formal subprograms may cause the associated
9539 -- node to be marked as effective instead of the original.
9540
9541 if not (Present (Associated_Node (N))
9542 and then Present
9543 (Current_Use_Clause
9544 (Associated_Node (N)))
9545 and then Is_Effective_Use_Clause
9546 (Current_Use_Clause
9547 (Associated_Node (N))))
9548 then
9549 Error_Msg_Node_1 := Entity (N);
9550 Error_Msg_NE
9551 ("use clause for package & has no effect?u?",
9552 Curr, Entity (N));
9553 end if;
9554
9555 -- We are dealing with an unused use_type_clause
9556
9557 else
9558 Error_Msg_Node_1 := Etype (N);
9559 Error_Msg_NE
9560 ("use clause for } has no effect?u?", Curr, Etype (N));
9561 end if;
9562 end if;
9563
9564 -- Verify that we haven't already processed a redundant
9565 -- use_type_clause within the same scope before we move the
9566 -- current use clause up to a previous one for type T.
9567
9568 if Present (Prev_Use_Clause (Curr)) then
9569 Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr));
9570 end if;
9571
9572 Next_Use_Clause (Curr);
9573 end loop;
9574 end Update_Chain_In_Scope;
9575
9576 -- Start of processing for Update_Use_Clause_Chain
9577
9578 begin
9579 Update_Chain_In_Scope (Scope_Stack.Last);
9580
9581 -- Deal with use clauses within the context area if the current
9582 -- scope is a compilation unit.
9583
9584 if Is_Compilation_Unit (Current_Scope)
9585 and then Sloc (Scope_Stack.Table
9586 (Scope_Stack.Last - 1).Entity) = Standard_Location
9587 then
9588 Update_Chain_In_Scope (Scope_Stack.Last - 1);
9589 end if;
9590 end Update_Use_Clause_Chain;
9591
9592 ---------------------
9593 -- Use_One_Package --
9594 ---------------------
9595
9596 procedure Use_One_Package
9597 (N : Node_Id;
9598 Pack_Name : Entity_Id := Empty;
9599 Force : Boolean := False)
9600 is
9601 procedure Note_Redundant_Use (Clause : Node_Id);
9602 -- Mark the name in a use clause as redundant if the corresponding
9603 -- entity is already use-visible. Emit a warning if the use clause comes
9604 -- from source and the proper warnings are enabled.
9605
9606 ------------------------
9607 -- Note_Redundant_Use --
9608 ------------------------
9609
9610 procedure Note_Redundant_Use (Clause : Node_Id) is
9611 Decl : constant Node_Id := Parent (Clause);
9612 Pack_Name : constant Entity_Id := Entity (Clause);
9613
9614 Cur_Use : Node_Id := Current_Use_Clause (Pack_Name);
9615 Prev_Use : Node_Id := Empty;
9616 Redundant : Node_Id := Empty;
9617 -- The Use_Clause which is actually redundant. In the simplest case
9618 -- it is Pack itself, but when we compile a body we install its
9619 -- context before that of its spec, in which case it is the
9620 -- use_clause in the spec that will appear to be redundant, and we
9621 -- want the warning to be placed on the body. Similar complications
9622 -- appear when the redundancy is between a child unit and one of its
9623 -- ancestors.
9624
9625 begin
9626 -- Could be renamed...
9627
9628 if No (Cur_Use) then
9629 Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name));
9630 end if;
9631
9632 Set_Redundant_Use (Clause, True);
9633
9634 -- Do not check for redundant use if clause is generated, or in an
9635 -- instance, or in a predefined unit to avoid misleading warnings
9636 -- that may occur as part of a rtsfind load.
9637
9638 if not Comes_From_Source (Clause)
9639 or else In_Instance
9640 or else not Warn_On_Redundant_Constructs
9641 or else Is_Predefined_Unit (Current_Sem_Unit)
9642 then
9643 return;
9644 end if;
9645
9646 if not Is_Compilation_Unit (Current_Scope) then
9647
9648 -- If the use_clause is in an inner scope, it is made redundant by
9649 -- some clause in the current context, with one exception: If we
9650 -- are compiling a nested package body, and the use_clause comes
9651 -- from then corresponding spec, the clause is not necessarily
9652 -- fully redundant, so we should not warn. If a warning was
9653 -- warranted, it would have been given when the spec was
9654 -- processed.
9655
9656 if Nkind (Parent (Decl)) = N_Package_Specification then
9657 declare
9658 Package_Spec_Entity : constant Entity_Id :=
9659 Defining_Unit_Name (Parent (Decl));
9660 begin
9661 if In_Package_Body (Package_Spec_Entity) then
9662 return;
9663 end if;
9664 end;
9665 end if;
9666
9667 Redundant := Clause;
9668 Prev_Use := Cur_Use;
9669
9670 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9671 declare
9672 Cur_Unit : constant Unit_Number_Type :=
9673 Get_Source_Unit (Cur_Use);
9674 New_Unit : constant Unit_Number_Type :=
9675 Get_Source_Unit (Clause);
9676
9677 Scop : Entity_Id;
9678
9679 begin
9680 if Cur_Unit = New_Unit then
9681
9682 -- Redundant clause in same body
9683
9684 Redundant := Clause;
9685 Prev_Use := Cur_Use;
9686
9687 elsif Cur_Unit = Current_Sem_Unit then
9688
9689 -- If the new clause is not in the current unit it has been
9690 -- analyzed first, and it makes the other one redundant.
9691 -- However, if the new clause appears in a subunit, Cur_Unit
9692 -- is still the parent, and in that case the redundant one
9693 -- is the one appearing in the subunit.
9694
9695 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
9696 Redundant := Clause;
9697 Prev_Use := Cur_Use;
9698
9699 -- Most common case: redundant clause in body, original
9700 -- clause in spec. Current scope is spec entity.
9701
9702 elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then
9703 Redundant := Cur_Use;
9704 Prev_Use := Clause;
9705
9706 else
9707 -- The new clause may appear in an unrelated unit, when
9708 -- the parents of a generic are being installed prior to
9709 -- instantiation. In this case there must be no warning.
9710 -- We detect this case by checking whether the current
9711 -- top of the stack is related to the current
9712 -- compilation.
9713
9714 Scop := Current_Scope;
9715 while Present (Scop)
9716 and then Scop /= Standard_Standard
9717 loop
9718 if Is_Compilation_Unit (Scop)
9719 and then not Is_Child_Unit (Scop)
9720 then
9721 return;
9722
9723 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
9724 exit;
9725 end if;
9726
9727 Scop := Scope (Scop);
9728 end loop;
9729
9730 Redundant := Cur_Use;
9731 Prev_Use := Clause;
9732 end if;
9733
9734 elsif New_Unit = Current_Sem_Unit then
9735 Redundant := Clause;
9736 Prev_Use := Cur_Use;
9737
9738 else
9739 -- Neither is the current unit, so they appear in parent or
9740 -- sibling units. Warning will be emitted elsewhere.
9741
9742 return;
9743 end if;
9744 end;
9745
9746 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
9747 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
9748 then
9749 -- Use_clause is in child unit of current unit, and the child unit
9750 -- appears in the context of the body of the parent, so it has
9751 -- been installed first, even though it is the redundant one.
9752 -- Depending on their placement in the context, the visible or the
9753 -- private parts of the two units, either might appear as
9754 -- redundant, but the message has to be on the current unit.
9755
9756 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
9757 Redundant := Cur_Use;
9758 Prev_Use := Clause;
9759 else
9760 Redundant := Clause;
9761 Prev_Use := Cur_Use;
9762 end if;
9763
9764 -- If the new use clause appears in the private part of a parent
9765 -- unit it may appear to be redundant w.r.t. a use clause in a
9766 -- child unit, but the previous use clause was needed in the
9767 -- visible part of the child, and no warning should be emitted.
9768
9769 if Nkind (Parent (Decl)) = N_Package_Specification
9770 and then List_Containing (Decl) =
9771 Private_Declarations (Parent (Decl))
9772 then
9773 declare
9774 Par : constant Entity_Id :=
9775 Defining_Entity (Parent (Decl));
9776 Spec : constant Node_Id :=
9777 Specification (Unit (Cunit (Current_Sem_Unit)));
9778 Cur_List : constant List_Id := List_Containing (Cur_Use);
9779
9780 begin
9781 if Is_Compilation_Unit (Par)
9782 and then Par /= Cunit_Entity (Current_Sem_Unit)
9783 then
9784 if Cur_List = Context_Items (Cunit (Current_Sem_Unit))
9785 or else Cur_List = Visible_Declarations (Spec)
9786 then
9787 return;
9788 end if;
9789 end if;
9790 end;
9791 end if;
9792
9793 -- Finally, if the current use clause is in the context then the
9794 -- clause is redundant when it is nested within the unit.
9795
9796 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
9797 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
9798 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
9799 then
9800 Redundant := Clause;
9801 Prev_Use := Cur_Use;
9802 end if;
9803
9804 if Present (Redundant) and then Parent (Redundant) /= Prev_Use then
9805
9806 -- Make sure we are looking at most-descendant use_package_clause
9807 -- by traversing the chain with Find_Most_Prev and then verifying
9808 -- there is no scope manipulation via Most_Descendant_Use_Clause.
9809
9810 if Nkind (Prev_Use) = N_Use_Package_Clause
9811 and then
9812 (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit
9813 or else Most_Descendant_Use_Clause
9814 (Prev_Use, Find_Most_Prev (Prev_Use)) /= Prev_Use)
9815 then
9816 Prev_Use := Find_Most_Prev (Prev_Use);
9817 end if;
9818
9819 Error_Msg_Sloc := Sloc (Prev_Use);
9820 Error_Msg_NE -- CODEFIX
9821 ("& is already use-visible through previous use_clause #?r?",
9822 Redundant, Pack_Name);
9823 end if;
9824 end Note_Redundant_Use;
9825
9826 -- Local variables
9827
9828 Current_Instance : Entity_Id := Empty;
9829 Id : Entity_Id;
9830 P : Entity_Id;
9831 Prev : Entity_Id;
9832 Private_With_OK : Boolean := False;
9833 Real_P : Entity_Id;
9834
9835 -- Start of processing for Use_One_Package
9836
9837 begin
9838 -- Use_One_Package may have been called recursively to handle an
9839 -- implicit use for a auxiliary system package, so set P accordingly
9840 -- and skip redundancy checks.
9841
9842 if No (Pack_Name) and then Present_System_Aux (N) then
9843 P := System_Aux_Id;
9844
9845 -- Check for redundant use_package_clauses
9846
9847 else
9848 -- Ignore cases where we are dealing with a non user defined package
9849 -- like Standard_Standard or something other than a valid package.
9850
9851 if not Is_Entity_Name (Pack_Name)
9852 or else No (Entity (Pack_Name))
9853 or else Ekind (Entity (Pack_Name)) /= E_Package
9854 then
9855 return;
9856 end if;
9857
9858 -- When a renaming exists we must check it for redundancy. The
9859 -- original package would have already been seen at this point.
9860
9861 if Present (Renamed_Object (Entity (Pack_Name))) then
9862 P := Renamed_Object (Entity (Pack_Name));
9863 else
9864 P := Entity (Pack_Name);
9865 end if;
9866
9867 -- Check for redundant clauses then set the current use clause for
9868 -- P if were are not "forcing" an installation from a scope
9869 -- reinstallation that is done throughout analysis for various
9870 -- reasons.
9871
9872 if In_Use (P) then
9873 Note_Redundant_Use (Pack_Name);
9874
9875 if not Force then
9876 Set_Current_Use_Clause (P, N);
9877 end if;
9878
9879 return;
9880
9881 -- Warn about detected redundant clauses
9882
9883 elsif not Force
9884 and then In_Open_Scopes (P)
9885 and then not Is_Hidden_Open_Scope (P)
9886 then
9887 if Warn_On_Redundant_Constructs and then P = Current_Scope then
9888 Error_Msg_NE -- CODEFIX
9889 ("& is already use-visible within itself?r?",
9890 Pack_Name, P);
9891 end if;
9892
9893 return;
9894 end if;
9895
9896 -- Set P back to the non-renamed package so that visiblilty of the
9897 -- entities within the package can be properly set below.
9898
9899 P := Entity (Pack_Name);
9900 end if;
9901
9902 Set_In_Use (P);
9903 Set_Current_Use_Clause (P, N);
9904
9905 -- Ada 2005 (AI-50217): Check restriction
9906
9907 if From_Limited_With (P) then
9908 Error_Msg_N ("limited withed package cannot appear in use clause", N);
9909 end if;
9910
9911 -- Find enclosing instance, if any
9912
9913 if In_Instance then
9914 Current_Instance := Current_Scope;
9915 while not Is_Generic_Instance (Current_Instance) loop
9916 Current_Instance := Scope (Current_Instance);
9917 end loop;
9918
9919 if No (Hidden_By_Use_Clause (N)) then
9920 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
9921 end if;
9922 end if;
9923
9924 -- If unit is a package renaming, indicate that the renamed package is
9925 -- also in use (the flags on both entities must remain consistent, and a
9926 -- subsequent use of either of them should be recognized as redundant).
9927
9928 if Present (Renamed_Object (P)) then
9929 Set_In_Use (Renamed_Object (P));
9930 Set_Current_Use_Clause (Renamed_Object (P), N);
9931 Real_P := Renamed_Object (P);
9932 else
9933 Real_P := P;
9934 end if;
9935
9936 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
9937 -- found in the private part of a package specification
9938
9939 if In_Private_Part (Current_Scope)
9940 and then Has_Private_With (P)
9941 and then Is_Child_Unit (Current_Scope)
9942 and then Is_Child_Unit (P)
9943 and then Is_Ancestor_Package (Scope (Current_Scope), P)
9944 then
9945 Private_With_OK := True;
9946 end if;
9947
9948 -- Loop through entities in one package making them potentially
9949 -- use-visible.
9950
9951 Id := First_Entity (P);
9952 while Present (Id)
9953 and then (Id /= First_Private_Entity (P)
9954 or else Private_With_OK) -- Ada 2005 (AI-262)
9955 loop
9956 Prev := Current_Entity (Id);
9957 while Present (Prev) loop
9958 if Is_Immediately_Visible (Prev)
9959 and then (not Is_Overloadable (Prev)
9960 or else not Is_Overloadable (Id)
9961 or else (Type_Conformant (Id, Prev)))
9962 then
9963 if No (Current_Instance) then
9964
9965 -- Potentially use-visible entity remains hidden
9966
9967 goto Next_Usable_Entity;
9968
9969 -- A use clause within an instance hides outer global entities,
9970 -- which are not used to resolve local entities in the
9971 -- instance. Note that the predefined entities in Standard
9972 -- could not have been hidden in the generic by a use clause,
9973 -- and therefore remain visible. Other compilation units whose
9974 -- entities appear in Standard must be hidden in an instance.
9975
9976 -- To determine whether an entity is external to the instance
9977 -- we compare the scope depth of its scope with that of the
9978 -- current instance. However, a generic actual of a subprogram
9979 -- instance is declared in the wrapper package but will not be
9980 -- hidden by a use-visible entity. similarly, an entity that is
9981 -- declared in an enclosing instance will not be hidden by an
9982 -- an entity declared in a generic actual, which can only have
9983 -- been use-visible in the generic and will not have hidden the
9984 -- entity in the generic parent.
9985
9986 -- If Id is called Standard, the predefined package with the
9987 -- same name is in the homonym chain. It has to be ignored
9988 -- because it has no defined scope (being the only entity in
9989 -- the system with this mandated behavior).
9990
9991 elsif not Is_Hidden (Id)
9992 and then Present (Scope (Prev))
9993 and then not Is_Wrapper_Package (Scope (Prev))
9994 and then Scope_Depth (Scope (Prev)) <
9995 Scope_Depth (Current_Instance)
9996 and then (Scope (Prev) /= Standard_Standard
9997 or else Sloc (Prev) > Standard_Location)
9998 then
9999 if In_Open_Scopes (Scope (Prev))
10000 and then Is_Generic_Instance (Scope (Prev))
10001 and then Present (Associated_Formal_Package (P))
10002 then
10003 null;
10004
10005 else
10006 Set_Is_Potentially_Use_Visible (Id);
10007 Set_Is_Immediately_Visible (Prev, False);
10008 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10009 end if;
10010 end if;
10011
10012 -- A user-defined operator is not use-visible if the predefined
10013 -- operator for the type is immediately visible, which is the case
10014 -- if the type of the operand is in an open scope. This does not
10015 -- apply to user-defined operators that have operands of different
10016 -- types, because the predefined mixed mode operations (multiply
10017 -- and divide) apply to universal types and do not hide anything.
10018
10019 elsif Ekind (Prev) = E_Operator
10020 and then Operator_Matches_Spec (Prev, Id)
10021 and then In_Open_Scopes
10022 (Scope (Base_Type (Etype (First_Formal (Id)))))
10023 and then (No (Next_Formal (First_Formal (Id)))
10024 or else Etype (First_Formal (Id)) =
10025 Etype (Next_Formal (First_Formal (Id)))
10026 or else Chars (Prev) = Name_Op_Expon)
10027 then
10028 goto Next_Usable_Entity;
10029
10030 -- In an instance, two homonyms may become use_visible through the
10031 -- actuals of distinct formal packages. In the generic, only the
10032 -- current one would have been visible, so make the other one
10033 -- not use_visible.
10034
10035 -- In certain pathological cases it is possible that unrelated
10036 -- homonyms from distinct formal packages may exist in an
10037 -- uninstalled scope. We must test for that here.
10038
10039 elsif Present (Current_Instance)
10040 and then Is_Potentially_Use_Visible (Prev)
10041 and then not Is_Overloadable (Prev)
10042 and then Scope (Id) /= Scope (Prev)
10043 and then Used_As_Generic_Actual (Scope (Prev))
10044 and then Used_As_Generic_Actual (Scope (Id))
10045 and then Is_List_Member (Scope (Prev))
10046 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
10047 Current_Use_Clause (Scope (Id)))
10048 then
10049 Set_Is_Potentially_Use_Visible (Prev, False);
10050 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
10051 end if;
10052
10053 Prev := Homonym (Prev);
10054 end loop;
10055
10056 -- On exit, we know entity is not hidden, unless it is private
10057
10058 if not Is_Hidden (Id)
10059 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
10060 then
10061 Set_Is_Potentially_Use_Visible (Id);
10062
10063 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
10064 Set_Is_Potentially_Use_Visible (Full_View (Id));
10065 end if;
10066 end if;
10067
10068 <<Next_Usable_Entity>>
10069 Next_Entity (Id);
10070 end loop;
10071
10072 -- Child units are also made use-visible by a use clause, but they may
10073 -- appear after all visible declarations in the parent entity list.
10074
10075 while Present (Id) loop
10076 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
10077 Set_Is_Potentially_Use_Visible (Id);
10078 end if;
10079
10080 Next_Entity (Id);
10081 end loop;
10082
10083 if Chars (Real_P) = Name_System
10084 and then Scope (Real_P) = Standard_Standard
10085 and then Present_System_Aux (N)
10086 then
10087 Use_One_Package (N);
10088 end if;
10089 end Use_One_Package;
10090
10091 ------------------
10092 -- Use_One_Type --
10093 ------------------
10094
10095 procedure Use_One_Type
10096 (Id : Node_Id;
10097 Installed : Boolean := False;
10098 Force : Boolean := False)
10099 is
10100 function Spec_Reloaded_For_Body return Boolean;
10101 -- Determine whether the compilation unit is a package body and the use
10102 -- type clause is in the spec of the same package. Even though the spec
10103 -- was analyzed first, its context is reloaded when analysing the body.
10104
10105 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
10106 -- AI05-150: if the use_type_clause carries the "all" qualifier,
10107 -- class-wide operations of ancestor types are use-visible if the
10108 -- ancestor type is visible.
10109
10110 ----------------------------
10111 -- Spec_Reloaded_For_Body --
10112 ----------------------------
10113
10114 function Spec_Reloaded_For_Body return Boolean is
10115 begin
10116 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
10117 declare
10118 Spec : constant Node_Id :=
10119 Parent (List_Containing (Parent (Id)));
10120
10121 begin
10122 -- Check whether type is declared in a package specification,
10123 -- and current unit is the corresponding package body. The
10124 -- use clauses themselves may be within a nested package.
10125
10126 return
10127 Nkind (Spec) = N_Package_Specification
10128 and then In_Same_Source_Unit
10129 (Corresponding_Body (Parent (Spec)),
10130 Cunit_Entity (Current_Sem_Unit));
10131 end;
10132 end if;
10133
10134 return False;
10135 end Spec_Reloaded_For_Body;
10136
10137 -------------------------------
10138 -- Use_Class_Wide_Operations --
10139 -------------------------------
10140
10141 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
10142 function Is_Class_Wide_Operation_Of
10143 (Op : Entity_Id;
10144 T : Entity_Id) return Boolean;
10145 -- Determine whether a subprogram has a class-wide parameter or
10146 -- result that is T'Class.
10147
10148 ---------------------------------
10149 -- Is_Class_Wide_Operation_Of --
10150 ---------------------------------
10151
10152 function Is_Class_Wide_Operation_Of
10153 (Op : Entity_Id;
10154 T : Entity_Id) return Boolean
10155 is
10156 Formal : Entity_Id;
10157
10158 begin
10159 Formal := First_Formal (Op);
10160 while Present (Formal) loop
10161 if Etype (Formal) = Class_Wide_Type (T) then
10162 return True;
10163 end if;
10164
10165 Next_Formal (Formal);
10166 end loop;
10167
10168 if Etype (Op) = Class_Wide_Type (T) then
10169 return True;
10170 end if;
10171
10172 return False;
10173 end Is_Class_Wide_Operation_Of;
10174
10175 -- Local variables
10176
10177 Ent : Entity_Id;
10178 Scop : Entity_Id;
10179
10180 -- Start of processing for Use_Class_Wide_Operations
10181
10182 begin
10183 Scop := Scope (Typ);
10184 if not Is_Hidden (Scop) then
10185 Ent := First_Entity (Scop);
10186 while Present (Ent) loop
10187 if Is_Overloadable (Ent)
10188 and then Is_Class_Wide_Operation_Of (Ent, Typ)
10189 and then not Is_Potentially_Use_Visible (Ent)
10190 then
10191 Set_Is_Potentially_Use_Visible (Ent);
10192 Append_Elmt (Ent, Used_Operations (Parent (Id)));
10193 end if;
10194
10195 Next_Entity (Ent);
10196 end loop;
10197 end if;
10198
10199 if Is_Derived_Type (Typ) then
10200 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
10201 end if;
10202 end Use_Class_Wide_Operations;
10203
10204 -- Local variables
10205
10206 Elmt : Elmt_Id;
10207 Is_Known_Used : Boolean;
10208 Op_List : Elist_Id;
10209 T : Entity_Id;
10210
10211 -- Start of processing for Use_One_Type
10212
10213 begin
10214 if Entity (Id) = Any_Type then
10215 return;
10216 end if;
10217
10218 -- It is the type determined by the subtype mark (8.4(8)) whose
10219 -- operations become potentially use-visible.
10220
10221 T := Base_Type (Entity (Id));
10222
10223 -- Either the type itself is used, the package where it is declared is
10224 -- in use or the entity is declared in the current package, thus
10225 -- use-visible.
10226
10227 Is_Known_Used :=
10228 (In_Use (T)
10229 and then ((Present (Current_Use_Clause (T))
10230 and then All_Present (Current_Use_Clause (T)))
10231 or else not All_Present (Parent (Id))))
10232 or else In_Use (Scope (T))
10233 or else Scope (T) = Current_Scope;
10234
10235 Set_Redundant_Use (Id,
10236 Is_Known_Used or else Is_Potentially_Use_Visible (T));
10237
10238 if Ekind (T) = E_Incomplete_Type then
10239 Error_Msg_N ("premature usage of incomplete type", Id);
10240
10241 elsif In_Open_Scopes (Scope (T)) then
10242 null;
10243
10244 -- A limited view cannot appear in a use_type_clause. However, an access
10245 -- type whose designated type is limited has the flag but is not itself
10246 -- a limited view unless we only have a limited view of its enclosing
10247 -- package.
10248
10249 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
10250 Error_Msg_N
10251 ("incomplete type from limited view cannot appear in use clause",
10252 Id);
10253
10254 -- If the use clause is redundant, Used_Operations will usually be
10255 -- empty, but we need to set it to empty here in one case: If we are
10256 -- instantiating a generic library unit, then we install the ancestors
10257 -- of that unit in the scope stack, which involves reprocessing use
10258 -- clauses in those ancestors. Such a use clause will typically have a
10259 -- nonempty Used_Operations unless it was redundant in the generic unit,
10260 -- even if it is redundant at the place of the instantiation.
10261
10262 elsif Redundant_Use (Id) then
10263
10264 -- We must avoid incorrectly setting the Current_Use_Clause when we
10265 -- are working with a redundant clause that has already been linked
10266 -- in the Prev_Use_Clause chain, otherwise the chain will break.
10267
10268 if Present (Current_Use_Clause (T))
10269 and then Present (Prev_Use_Clause (Current_Use_Clause (T)))
10270 and then Parent (Id) = Prev_Use_Clause (Current_Use_Clause (T))
10271 then
10272 null;
10273 else
10274 Set_Current_Use_Clause (T, Parent (Id));
10275 end if;
10276
10277 Set_Used_Operations (Parent (Id), New_Elmt_List);
10278
10279 -- If the subtype mark designates a subtype in a different package,
10280 -- we have to check that the parent type is visible, otherwise the
10281 -- use_type_clause is a no-op. Not clear how to do that???
10282
10283 else
10284 Set_Current_Use_Clause (T, Parent (Id));
10285 Set_In_Use (T);
10286
10287 -- If T is tagged, primitive operators on class-wide operands are
10288 -- also deemed available. Note that this is really necessary only
10289 -- in semantics-only mode, because the primitive operators are not
10290 -- fully constructed in this mode, but we do it in all modes for the
10291 -- sake of uniformity, as this should not matter in practice.
10292
10293 if Is_Tagged_Type (T) then
10294 Set_In_Use (Class_Wide_Type (T));
10295 end if;
10296
10297 -- Iterate over primitive operations of the type. If an operation is
10298 -- already use_visible, it is the result of a previous use_clause,
10299 -- and already appears on the corresponding entity chain. If the
10300 -- clause is being reinstalled, operations are already use-visible.
10301
10302 if Installed then
10303 null;
10304
10305 else
10306 Op_List := Collect_Primitive_Operations (T);
10307 Elmt := First_Elmt (Op_List);
10308 while Present (Elmt) loop
10309 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
10310 or else Chars (Node (Elmt)) in Any_Operator_Name)
10311 and then not Is_Hidden (Node (Elmt))
10312 and then not Is_Potentially_Use_Visible (Node (Elmt))
10313 then
10314 Set_Is_Potentially_Use_Visible (Node (Elmt));
10315 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10316
10317 elsif Ada_Version >= Ada_2012
10318 and then All_Present (Parent (Id))
10319 and then not Is_Hidden (Node (Elmt))
10320 and then not Is_Potentially_Use_Visible (Node (Elmt))
10321 then
10322 Set_Is_Potentially_Use_Visible (Node (Elmt));
10323 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10324 end if;
10325
10326 Next_Elmt (Elmt);
10327 end loop;
10328 end if;
10329
10330 if Ada_Version >= Ada_2012
10331 and then All_Present (Parent (Id))
10332 and then Is_Tagged_Type (T)
10333 then
10334 Use_Class_Wide_Operations (T);
10335 end if;
10336 end if;
10337
10338 -- If warning on redundant constructs, check for unnecessary WITH
10339
10340 if not Force
10341 and then Warn_On_Redundant_Constructs
10342 and then Is_Known_Used
10343
10344 -- with P; with P; use P;
10345 -- package P is package X is package body X is
10346 -- type T ... use P.T;
10347
10348 -- The compilation unit is the body of X. GNAT first compiles the
10349 -- spec of X, then proceeds to the body. At that point P is marked
10350 -- as use visible. The analysis then reinstalls the spec along with
10351 -- its context. The use clause P.T is now recognized as redundant,
10352 -- but in the wrong context. Do not emit a warning in such cases.
10353 -- Do not emit a warning either if we are in an instance, there is
10354 -- no redundancy between an outer use_clause and one that appears
10355 -- within the generic.
10356
10357 and then not Spec_Reloaded_For_Body
10358 and then not In_Instance
10359 and then not In_Inlined_Body
10360 then
10361 -- The type already has a use clause
10362
10363 if In_Use (T) then
10364
10365 -- Case where we know the current use clause for the type
10366
10367 if Present (Current_Use_Clause (T)) then
10368 Use_Clause_Known : declare
10369 Clause1 : constant Node_Id :=
10370 Find_Most_Prev (Current_Use_Clause (T));
10371 Clause2 : constant Node_Id := Parent (Id);
10372 Ent1 : Entity_Id;
10373 Ent2 : Entity_Id;
10374 Err_No : Node_Id;
10375 Unit1 : Node_Id;
10376 Unit2 : Node_Id;
10377
10378 -- Start of processing for Use_Clause_Known
10379
10380 begin
10381 -- If both current use_type_clause and the use_type_clause
10382 -- for the type are at the compilation unit level, one of
10383 -- the units must be an ancestor of the other, and the
10384 -- warning belongs on the descendant.
10385
10386 if Nkind (Parent (Clause1)) = N_Compilation_Unit
10387 and then
10388 Nkind (Parent (Clause2)) = N_Compilation_Unit
10389 then
10390 -- If the unit is a subprogram body that acts as spec,
10391 -- the context clause is shared with the constructed
10392 -- subprogram spec. Clearly there is no redundancy.
10393
10394 if Clause1 = Clause2 then
10395 return;
10396 end if;
10397
10398 Unit1 := Unit (Parent (Clause1));
10399 Unit2 := Unit (Parent (Clause2));
10400
10401 -- If both clauses are on same unit, or one is the body
10402 -- of the other, or one of them is in a subunit, report
10403 -- redundancy on the later one.
10404
10405 if Unit1 = Unit2 or else Nkind (Unit1) = N_Subunit then
10406 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10407 Error_Msg_NE -- CODEFIX
10408 ("& is already use-visible through previous "
10409 & "use_type_clause #??", Clause1, T);
10410 return;
10411
10412 elsif Nkind (Unit2) in N_Package_Body | N_Subprogram_Body
10413 and then Nkind (Unit1) /= Nkind (Unit2)
10414 and then Nkind (Unit1) /= N_Subunit
10415 then
10416 Error_Msg_Sloc := Sloc (Clause1);
10417 Error_Msg_NE -- CODEFIX
10418 ("& is already use-visible through previous "
10419 & "use_type_clause #??", Current_Use_Clause (T), T);
10420 return;
10421 end if;
10422
10423 -- There is a redundant use_type_clause in a child unit.
10424 -- Determine which of the units is more deeply nested.
10425 -- If a unit is a package instance, retrieve the entity
10426 -- and its scope from the instance spec.
10427
10428 Ent1 := Entity_Of_Unit (Unit1);
10429 Ent2 := Entity_Of_Unit (Unit2);
10430
10431 if Scope (Ent2) = Standard_Standard then
10432 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10433 Err_No := Clause1;
10434
10435 elsif Scope (Ent1) = Standard_Standard then
10436 Error_Msg_Sloc := Sloc (Id);
10437 Err_No := Clause2;
10438
10439 -- If both units are child units, we determine which one
10440 -- is the descendant by the scope distance to the
10441 -- ultimate parent unit.
10442
10443 else
10444 declare
10445 S1 : Entity_Id;
10446 S2 : Entity_Id;
10447
10448 begin
10449 S1 := Scope (Ent1);
10450 S2 := Scope (Ent2);
10451 while Present (S1)
10452 and then Present (S2)
10453 and then S1 /= Standard_Standard
10454 and then S2 /= Standard_Standard
10455 loop
10456 S1 := Scope (S1);
10457 S2 := Scope (S2);
10458 end loop;
10459
10460 if S1 = Standard_Standard then
10461 Error_Msg_Sloc := Sloc (Id);
10462 Err_No := Clause2;
10463 else
10464 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10465 Err_No := Clause1;
10466 end if;
10467 end;
10468 end if;
10469
10470 if Parent (Id) /= Err_No then
10471 if Most_Descendant_Use_Clause
10472 (Err_No, Parent (Id)) = Parent (Id)
10473 then
10474 Error_Msg_Sloc := Sloc (Err_No);
10475 Err_No := Parent (Id);
10476 end if;
10477
10478 Error_Msg_NE -- CODEFIX
10479 ("& is already use-visible through previous "
10480 & "use_type_clause #??", Err_No, Id);
10481 end if;
10482
10483 -- Case where current use_type_clause and use_type_clause
10484 -- for the type are not both at the compilation unit level.
10485 -- In this case we don't have location information.
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 end Use_Clause_Known;
10493
10494 -- Here if Current_Use_Clause is not set for T, another case where
10495 -- we do not have the location information available.
10496
10497 else
10498 Error_Msg_NE -- CODEFIX
10499 ("& is already use-visible through previous "
10500 & "use_type_clause??", Id, T);
10501 end if;
10502
10503 -- The package where T is declared is already used
10504
10505 elsif In_Use (Scope (T)) then
10506 -- Due to expansion of contracts we could be attempting to issue
10507 -- a spurious warning - so verify there is a previous use clause.
10508
10509 if Current_Use_Clause (Scope (T)) /=
10510 Find_Most_Prev (Current_Use_Clause (Scope (T)))
10511 then
10512 Error_Msg_Sloc :=
10513 Sloc (Find_Most_Prev (Current_Use_Clause (Scope (T))));
10514 Error_Msg_NE -- CODEFIX
10515 ("& is already use-visible through package use clause #??",
10516 Id, T);
10517 end if;
10518
10519 -- The current scope is the package where T is declared
10520
10521 else
10522 Error_Msg_Node_2 := Scope (T);
10523 Error_Msg_NE -- CODEFIX
10524 ("& is already use-visible inside package &??", Id, T);
10525 end if;
10526 end if;
10527 end Use_One_Type;
10528
10529 ----------------
10530 -- Write_Info --
10531 ----------------
10532
10533 procedure Write_Info is
10534 Id : Entity_Id := First_Entity (Current_Scope);
10535
10536 begin
10537 -- No point in dumping standard entities
10538
10539 if Current_Scope = Standard_Standard then
10540 return;
10541 end if;
10542
10543 Write_Str ("========================================================");
10544 Write_Eol;
10545 Write_Str (" Defined Entities in ");
10546 Write_Name (Chars (Current_Scope));
10547 Write_Eol;
10548 Write_Str ("========================================================");
10549 Write_Eol;
10550
10551 if No (Id) then
10552 Write_Str ("-- none --");
10553 Write_Eol;
10554
10555 else
10556 while Present (Id) loop
10557 Write_Entity_Info (Id, " ");
10558 Next_Entity (Id);
10559 end loop;
10560 end if;
10561
10562 if Scope (Current_Scope) = Standard_Standard then
10563
10564 -- Print information on the current unit itself
10565
10566 Write_Entity_Info (Current_Scope, " ");
10567 end if;
10568
10569 Write_Eol;
10570 end Write_Info;
10571
10572 --------
10573 -- ws --
10574 --------
10575
10576 procedure ws is
10577 S : Entity_Id;
10578 begin
10579 for J in reverse 1 .. Scope_Stack.Last loop
10580 S := Scope_Stack.Table (J).Entity;
10581 Write_Int (Int (S));
10582 Write_Str (" === ");
10583 Write_Name (Chars (S));
10584 Write_Eol;
10585 end loop;
10586 end ws;
10587
10588 --------
10589 -- we --
10590 --------
10591
10592 procedure we (S : Entity_Id) is
10593 E : Entity_Id;
10594 begin
10595 E := First_Entity (S);
10596 while Present (E) loop
10597 Write_Int (Int (E));
10598 Write_Str (" === ");
10599 Write_Name (Chars (E));
10600 Write_Eol;
10601 Next_Entity (E);
10602 end loop;
10603 end we;
10604 end Sem_Ch8;