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