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