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