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