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