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