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