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