]>
Commit | Line | Data |
---|---|---|
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 | ||
26 | with Atree; use Atree; | |
27 | with Debug; use Debug; | |
28 | with Einfo; use Einfo; | |
29 | with Elists; use Elists; | |
30 | with Errout; use Errout; | |
024d33d8 | 31 | with Exp_Disp; use Exp_Disp; |
d239991f | 32 | with Exp_Tss; use Exp_Tss; |
996ae0b0 | 33 | with Exp_Util; use Exp_Util; |
996ae0b0 | 34 | with Freeze; use Freeze; |
8636f52f | 35 | with Ghost; use Ghost; |
16ca248a | 36 | with Impunit; use Impunit; |
996ae0b0 RK |
37 | with Lib; use Lib; |
38 | with Lib.Load; use Lib.Load; | |
39 | with Lib.Xref; use Lib.Xref; | |
40 | with Namet; use Namet; | |
294ccb21 | 41 | with Namet.Sp; use Namet.Sp; |
996ae0b0 RK |
42 | with Nlists; use Nlists; |
43 | with Nmake; use Nmake; | |
44 | with Opt; use Opt; | |
45 | with Output; use Output; | |
46 | with Restrict; use Restrict; | |
6e937c1c | 47 | with Rident; use Rident; |
996ae0b0 RK |
48 | with Rtsfind; use Rtsfind; |
49 | with Sem; use Sem; | |
a4100e55 | 50 | with Sem_Aux; use Sem_Aux; |
fbf5a39b | 51 | with Sem_Cat; use Sem_Cat; |
996ae0b0 RK |
52 | with Sem_Ch3; use Sem_Ch3; |
53 | with Sem_Ch4; use Sem_Ch4; | |
54 | with Sem_Ch6; use Sem_Ch6; | |
55 | with Sem_Ch12; use Sem_Ch12; | |
ef992452 | 56 | with Sem_Ch13; use Sem_Ch13; |
dec6faf1 | 57 | with Sem_Dim; use Sem_Dim; |
82c80734 | 58 | with Sem_Disp; use Sem_Disp; |
a77842bd | 59 | with Sem_Dist; use Sem_Dist; |
a2dc5812 | 60 | with Sem_Eval; use Sem_Eval; |
996ae0b0 RK |
61 | with Sem_Res; use Sem_Res; |
62 | with Sem_Util; use Sem_Util; | |
63 | with Sem_Type; use Sem_Type; | |
64 | with Stand; use Stand; | |
65 | with Sinfo; use Sinfo; | |
66 | with Sinfo.CN; use Sinfo.CN; | |
67 | with Snames; use Snames; | |
68 | with Style; use Style; | |
69 | with Table; | |
70 | with Tbuild; use Tbuild; | |
71 | with Uintp; use Uintp; | |
72 | ||
996ae0b0 RK |
73 | package 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 | ||
780d73d7 | 3440 | if Is_Actual and then 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 | |
683af98c AC |
5331 | -- undefined reference. The entry is not added if we are ignoring |
5332 | -- errors. | |
996ae0b0 | 5333 | |
683af98c | 5334 | if not All_Errors_Mode and then Ignore_Errors_Enable = 0 then |
11560bcc TQ |
5335 | Urefs.Append ( |
5336 | (Node => N, | |
5337 | Err => Emsg, | |
5338 | Nvis => Nvis, | |
5339 | Loc => Sloc (N))); | |
996ae0b0 RK |
5340 | end if; |
5341 | ||
5342 | Msg := True; | |
5343 | end Undefined; | |
5344 | ||
522aa6ee AC |
5345 | -- Local variables |
5346 | ||
5347 | Nested_Inst : Entity_Id := Empty; | |
5348 | -- The entity of a nested instance which appears within Inst (if any) | |
5349 | ||
996ae0b0 RK |
5350 | -- Start of processing for Find_Direct_Name |
5351 | ||
5352 | begin | |
5353 | -- If the entity pointer is already set, this is an internal node, or | |
5354 | -- a node that is analyzed more than once, after a tree modification. | |
5355 | -- In such a case there is no resolution to perform, just set the type. | |
5356 | ||
5357 | if Present (Entity (N)) then | |
5358 | if Is_Type (Entity (N)) then | |
5359 | Set_Etype (N, Entity (N)); | |
5360 | ||
5361 | else | |
5362 | declare | |
5363 | Entyp : constant Entity_Id := Etype (Entity (N)); | |
5364 | ||
5365 | begin | |
5366 | -- One special case here. If the Etype field is already set, | |
5367 | -- and references the packed array type corresponding to the | |
5368 | -- etype of the referenced entity, then leave it alone. This | |
5369 | -- happens for trees generated from Exp_Pakd, where expressions | |
5370 | -- can be deliberately "mis-typed" to the packed array type. | |
5371 | ||
5372 | if Is_Array_Type (Entyp) | |
5373 | and then Is_Packed (Entyp) | |
5374 | and then Present (Etype (N)) | |
8ca597af | 5375 | and then Etype (N) = Packed_Array_Impl_Type (Entyp) |
996ae0b0 RK |
5376 | then |
5377 | null; | |
5378 | ||
5379 | -- If not that special case, then just reset the Etype | |
5380 | ||
5381 | else | |
5382 | Set_Etype (N, Etype (Entity (N))); | |
5383 | end if; | |
5384 | end; | |
5385 | end if; | |
5386 | ||
5387 | return; | |
5388 | end if; | |
5389 | ||
5390 | -- Here if Entity pointer was not set, we need full visibility analysis | |
5391 | -- First we generate debugging output if the debug E flag is set. | |
5392 | ||
5393 | if Debug_Flag_E then | |
5394 | Write_Str ("Looking for "); | |
5395 | Write_Name (Chars (N)); | |
5396 | Write_Eol; | |
5397 | end if; | |
5398 | ||
5399 | Homonyms := Current_Entity (N); | |
5400 | Nvis_Entity := False; | |
5401 | ||
5402 | E := Homonyms; | |
5403 | while Present (E) loop | |
5404 | ||
ff81221b ES |
5405 | -- If entity is immediately visible or potentially use visible, then |
5406 | -- process the entity and we are done. | |
996ae0b0 RK |
5407 | |
5408 | if Is_Immediately_Visible (E) then | |
5409 | goto Immediately_Visible_Entity; | |
5410 | ||
5411 | elsif Is_Potentially_Use_Visible (E) then | |
5412 | goto Potentially_Use_Visible_Entity; | |
5413 | ||
5414 | -- Note if a known but invisible entity encountered | |
5415 | ||
5416 | elsif Known_But_Invisible (E) then | |
5417 | Nvis_Entity := True; | |
5418 | end if; | |
5419 | ||
5420 | -- Move to next entity in chain and continue search | |
5421 | ||
5422 | E := Homonym (E); | |
5423 | end loop; | |
5424 | ||
5425 | -- If no entries on homonym chain that were potentially visible, | |
5426 | -- and no entities reasonably considered as non-visible, then | |
5427 | -- we have a plain undefined reference, with no additional | |
a90bd866 | 5428 | -- explanation required. |
996ae0b0 RK |
5429 | |
5430 | if not Nvis_Entity then | |
5431 | Undefined (Nvis => False); | |
996ae0b0 RK |
5432 | |
5433 | -- Otherwise there is at least one entry on the homonym chain that | |
5434 | -- is reasonably considered as being known and non-visible. | |
5435 | ||
5436 | else | |
5437 | Nvis_Messages; | |
996ae0b0 RK |
5438 | end if; |
5439 | ||
18dae814 | 5440 | goto Done; |
fbf5a39b | 5441 | |
996ae0b0 RK |
5442 | -- Processing for a potentially use visible entry found. We must search |
5443 | -- the rest of the homonym chain for two reasons. First, if there is a | |
5444 | -- directly visible entry, then none of the potentially use-visible | |
5445 | -- entities are directly visible (RM 8.4(10)). Second, we need to check | |
5446 | -- for the case of multiple potentially use-visible entries hiding one | |
5447 | -- another and as a result being non-directly visible (RM 8.4(11)). | |
5448 | ||
5449 | <<Potentially_Use_Visible_Entity>> declare | |
5450 | Only_One_Visible : Boolean := True; | |
5451 | All_Overloadable : Boolean := Is_Overloadable (E); | |
5452 | ||
5453 | begin | |
5454 | E2 := Homonym (E); | |
996ae0b0 RK |
5455 | while Present (E2) loop |
5456 | if Is_Immediately_Visible (E2) then | |
5457 | ||
5458 | -- If the use-visible entity comes from the actual for a | |
5459 | -- formal package, it hides a directly visible entity from | |
5460 | -- outside the instance. | |
5461 | ||
5462 | if From_Actual_Package (E) | |
5463 | and then Scope_Depth (E2) < Scope_Depth (Inst) | |
5464 | then | |
5465 | goto Found; | |
5466 | else | |
5467 | E := E2; | |
5468 | goto Immediately_Visible_Entity; | |
5469 | end if; | |
5470 | ||
5471 | elsif Is_Potentially_Use_Visible (E2) then | |
5472 | Only_One_Visible := False; | |
5473 | All_Overloadable := All_Overloadable and Is_Overloadable (E2); | |
9bc856dd | 5474 | |
30783513 | 5475 | -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect |
f3d57416 | 5476 | -- that can occur in private_with clauses. Example: |
9bc856dd AC |
5477 | |
5478 | -- with A; | |
5479 | -- private with B; package A is | |
5480 | -- package C is function B return Integer; | |
5481 | -- use A; end A; | |
5482 | -- V1 : Integer := B; | |
5483 | -- private function B return Integer; | |
5484 | -- V2 : Integer := B; | |
5485 | -- end C; | |
5486 | ||
bc41faa2 | 5487 | -- V1 resolves to A.B, but V2 resolves to library unit B |
9bc856dd AC |
5488 | |
5489 | elsif Ekind (E2) = E_Function | |
5490 | and then Scope (E2) = Standard_Standard | |
5491 | and then Has_Private_With (E2) | |
5492 | then | |
5493 | Only_One_Visible := False; | |
5494 | All_Overloadable := False; | |
5495 | Nvis_Is_Private_Subprg := True; | |
5496 | exit; | |
996ae0b0 RK |
5497 | end if; |
5498 | ||
5499 | E2 := Homonym (E2); | |
5500 | end loop; | |
5501 | ||
5502 | -- On falling through this loop, we have checked that there are no | |
5503 | -- immediately visible entities. Only_One_Visible is set if exactly | |
5504 | -- one potentially use visible entity exists. All_Overloadable is | |
5505 | -- set if all the potentially use visible entities are overloadable. | |
5506 | -- The condition for legality is that either there is one potentially | |
5507 | -- use visible entity, or if there is more than one, then all of them | |
5508 | -- are overloadable. | |
5509 | ||
5510 | if Only_One_Visible or All_Overloadable then | |
5511 | goto Found; | |
5512 | ||
5513 | -- If there is more than one potentially use-visible entity and at | |
4ff4293f | 5514 | -- least one of them non-overloadable, we have an error (RM 8.4(11)). |
996ae0b0 | 5515 | -- Note that E points to the first such entity on the homonym list. |
996ae0b0 RK |
5516 | |
5517 | else | |
522aa6ee AC |
5518 | -- If one of the entities is declared in an actual package, it |
5519 | -- was visible in the generic, and takes precedence over other | |
5520 | -- entities that are potentially use-visible. The same applies | |
5521 | -- if the entity is declared in a local instantiation of the | |
5522 | -- current instance. | |
5523 | ||
996ae0b0 | 5524 | if In_Instance then |
fbf5a39b | 5525 | |
522aa6ee | 5526 | -- Find the current instance |
fbf5a39b | 5527 | |
16ca248a | 5528 | Inst := Current_Scope; |
ac7d724d | 5529 | while Present (Inst) and then Inst /= Standard_Standard loop |
fbf5a39b AC |
5530 | if Is_Generic_Instance (Inst) then |
5531 | exit; | |
5532 | end if; | |
5533 | ||
5534 | Inst := Scope (Inst); | |
5535 | end loop; | |
5536 | ||
522aa6ee AC |
5537 | -- Reexamine the candidate entities, giving priority to those |
5538 | -- that were visible within the generic. | |
5539 | ||
996ae0b0 | 5540 | E2 := E; |
996ae0b0 | 5541 | while Present (E2) loop |
522aa6ee AC |
5542 | Nested_Inst := Nearest_Enclosing_Instance (E2); |
5543 | ||
5544 | -- The entity is declared within an actual package, or in a | |
5545 | -- nested instance. The ">=" accounts for the case where the | |
5546 | -- current instance and the nested instance are the same. | |
5547 | ||
fbf5a39b | 5548 | if From_Actual_Package (E2) |
522aa6ee AC |
5549 | or else (Present (Nested_Inst) |
5550 | and then Scope_Depth (Nested_Inst) >= | |
5551 | Scope_Depth (Inst)) | |
fbf5a39b | 5552 | then |
996ae0b0 RK |
5553 | E := E2; |
5554 | goto Found; | |
5555 | end if; | |
5556 | ||
5557 | E2 := Homonym (E2); | |
5558 | end loop; | |
5559 | ||
5560 | Nvis_Messages; | |
18dae814 | 5561 | goto Done; |
996ae0b0 | 5562 | |
8ab31c0c | 5563 | elsif Is_Predefined_Unit (Current_Sem_Unit) then |
4de287c4 ES |
5564 | -- A use-clause in the body of a system file creates conflict |
5565 | -- with some entity in a user scope, while rtsfind is active. | |
5566 | -- Keep only the entity coming from another predefined unit. | |
5eb10f25 ES |
5567 | |
5568 | E2 := E; | |
5569 | while Present (E2) loop | |
8ab31c0c | 5570 | if In_Predefined_Unit (E2) then |
5eb10f25 ES |
5571 | E := E2; |
5572 | goto Found; | |
5573 | end if; | |
5574 | ||
5575 | E2 := Homonym (E2); | |
5576 | end loop; | |
5577 | ||
4de287c4 | 5578 | -- Entity must exist because predefined unit is correct |
5eb10f25 ES |
5579 | |
5580 | raise Program_Error; | |
5581 | ||
996ae0b0 RK |
5582 | else |
5583 | Nvis_Messages; | |
18dae814 | 5584 | goto Done; |
996ae0b0 RK |
5585 | end if; |
5586 | end if; | |
5587 | end; | |
5588 | ||
5589 | -- Come here with E set to the first immediately visible entity on | |
5590 | -- the homonym chain. This is the one we want unless there is another | |
ff81221b ES |
5591 | -- immediately visible entity further on in the chain for an inner |
5592 | -- scope (RM 8.3(8)). | |
996ae0b0 RK |
5593 | |
5594 | <<Immediately_Visible_Entity>> declare | |
5595 | Level : Int; | |
5596 | Scop : Entity_Id; | |
5597 | ||
5598 | begin | |
ff81221b | 5599 | -- Find scope level of initial entity. When compiling through |
996ae0b0 RK |
5600 | -- Rtsfind, the previous context is not completely invisible, and |
5601 | -- an outer entity may appear on the chain, whose scope is below | |
5602 | -- the entry for Standard that delimits the current scope stack. | |
5603 | -- Indicate that the level for this spurious entry is outside of | |
5604 | -- the current scope stack. | |
5605 | ||
5606 | Level := Scope_Stack.Last; | |
5607 | loop | |
5608 | Scop := Scope_Stack.Table (Level).Entity; | |
5609 | exit when Scop = Scope (E); | |
5610 | Level := Level - 1; | |
5611 | exit when Scop = Standard_Standard; | |
5612 | end loop; | |
5613 | ||
5614 | -- Now search remainder of homonym chain for more inner entry | |
5615 | -- If the entity is Standard itself, it has no scope, and we | |
5616 | -- compare it with the stack entry directly. | |
5617 | ||
5618 | E2 := Homonym (E); | |
5619 | while Present (E2) loop | |
5620 | if Is_Immediately_Visible (E2) then | |
4de287c4 ES |
5621 | |
5622 | -- If a generic package contains a local declaration that | |
5623 | -- has the same name as the generic, there may be a visibility | |
5624 | -- conflict in an instance, where the local declaration must | |
5625 | -- also hide the name of the corresponding package renaming. | |
5626 | -- We check explicitly for a package declared by a renaming, | |
5627 | -- whose renamed entity is an instance that is on the scope | |
5628 | -- stack, and that contains a homonym in the same scope. Once | |
5629 | -- we have found it, we know that the package renaming is not | |
5630 | -- immediately visible, and that the identifier denotes the | |
5631 | -- other entity (and its homonyms if overloaded). | |
5632 | ||
5633 | if Scope (E) = Scope (E2) | |
5634 | and then Ekind (E) = E_Package | |
5635 | and then Present (Renamed_Object (E)) | |
5636 | and then Is_Generic_Instance (Renamed_Object (E)) | |
5637 | and then In_Open_Scopes (Renamed_Object (E)) | |
5638 | and then Comes_From_Source (N) | |
5639 | then | |
5640 | Set_Is_Immediately_Visible (E, False); | |
5641 | E := E2; | |
5642 | ||
5643 | else | |
5644 | for J in Level + 1 .. Scope_Stack.Last loop | |
5645 | if Scope_Stack.Table (J).Entity = Scope (E2) | |
5646 | or else Scope_Stack.Table (J).Entity = E2 | |
5647 | then | |
5648 | Level := J; | |
5649 | E := E2; | |
5650 | exit; | |
5651 | end if; | |
5652 | end loop; | |
5653 | end if; | |
996ae0b0 RK |
5654 | end if; |
5655 | ||
5656 | E2 := Homonym (E2); | |
5657 | end loop; | |
5658 | ||
5659 | -- At the end of that loop, E is the innermost immediately | |
5660 | -- visible entity, so we are all set. | |
5661 | end; | |
5662 | ||
5663 | -- Come here with entity found, and stored in E | |
5664 | ||
5665 | <<Found>> begin | |
5666 | ||
30196a76 RD |
5667 | -- Check violation of No_Wide_Characters restriction |
5668 | ||
5669 | Check_Wide_Character_Restriction (E, N); | |
5670 | ||
294ccb21 RD |
5671 | -- When distribution features are available (Get_PCS_Name /= |
5672 | -- Name_No_DSA), a remote access-to-subprogram type is converted | |
5673 | -- into a record type holding whatever information is needed to | |
f3d57416 | 5674 | -- perform a remote call on an RCI subprogram. In that case we |
294ccb21 RD |
5675 | -- rewrite any occurrence of the RAS type into the equivalent record |
5676 | -- type here. 'Access attribute references and RAS dereferences are | |
5677 | -- then implemented using specific TSSs. However when distribution is | |
5678 | -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the | |
5679 | -- generation of these TSSs, and we must keep the RAS type in its | |
5680 | -- original access-to-subprogram form (since all calls through a | |
5681 | -- value of such type will be local anyway in the absence of a PCS). | |
5682 | ||
996ae0b0 RK |
5683 | if Comes_From_Source (N) |
5684 | and then Is_Remote_Access_To_Subprogram_Type (E) | |
d1d81616 | 5685 | and then Ekind (E) = E_Access_Subprogram_Type |
996ae0b0 | 5686 | and then Expander_Active |
a77842bd | 5687 | and then Get_PCS_Name /= Name_No_DSA |
996ae0b0 | 5688 | then |
18dae814 RD |
5689 | Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N))); |
5690 | goto Done; | |
996ae0b0 RK |
5691 | end if; |
5692 | ||
6989bc1f | 5693 | -- Set the entity. Note that the reason we call Set_Entity for the |
e7ba564f | 5694 | -- overloadable case, as opposed to Set_Entity_With_Checks is |
6989bc1f AC |
5695 | -- that in the overloaded case, the initial call can set the wrong |
5696 | -- homonym. The call that sets the right homonym is in Sem_Res and | |
e7ba564f | 5697 | -- that call does use Set_Entity_With_Checks, so we don't miss |
6989bc1f AC |
5698 | -- a style check. |
5699 | ||
5700 | if Is_Overloadable (E) then | |
5701 | Set_Entity (N, E); | |
5702 | else | |
e7ba564f | 5703 | Set_Entity_With_Checks (N, E); |
6989bc1f | 5704 | end if; |
996ae0b0 RK |
5705 | |
5706 | if Is_Type (E) then | |
5707 | Set_Etype (N, E); | |
5708 | else | |
5709 | Set_Etype (N, Get_Full_View (Etype (E))); | |
5710 | end if; | |
5711 | ||
5712 | if Debug_Flag_E then | |
5713 | Write_Str (" found "); | |
5714 | Write_Entity_Info (E, " "); | |
5715 | end if; | |
5716 | ||
5717 | -- If the Ekind of the entity is Void, it means that all homonyms | |
5718 | -- are hidden from all visibility (RM 8.3(5,14-20)). However, this | |
5719 | -- test is skipped if the current scope is a record and the name is | |
5720 | -- a pragma argument expression (case of Atomic and Volatile pragmas | |
5721 | -- and possibly other similar pragmas added later, which are allowed | |
5722 | -- to reference components in the current record). | |
5723 | ||
5724 | if Ekind (E) = E_Void | |
5725 | and then | |
5726 | (not Is_Record_Type (Current_Scope) | |
5727 | or else Nkind (Parent (N)) /= N_Pragma_Argument_Association) | |
5728 | then | |
5729 | Premature_Usage (N); | |
5730 | ||
16ca248a ES |
5731 | -- If the entity is overloadable, collect all interpretations of the |
5732 | -- name for subsequent overload resolution. We optimize a bit here to | |
5733 | -- do this only if we have an overloadable entity that is not on its | |
5734 | -- own on the homonym chain. | |
996ae0b0 RK |
5735 | |
5736 | elsif Is_Overloadable (E) | |
5737 | and then (Present (Homonym (E)) or else Current_Entity (N) /= E) | |
5738 | then | |
5739 | Collect_Interps (N); | |
5740 | ||
bc41faa2 | 5741 | -- If no homonyms were visible, the entity is unambiguous |
996ae0b0 RK |
5742 | |
5743 | if not Is_Overloaded (N) then | |
67ce0d7e RD |
5744 | if not Is_Actual_Parameter then |
5745 | Generate_Reference (E, N); | |
5746 | end if; | |
996ae0b0 RK |
5747 | end if; |
5748 | ||
5749 | -- Case of non-overloadable entity, set the entity providing that | |
5750 | -- we do not have the case of a discriminant reference within a | |
5751 | -- default expression. Such references are replaced with the | |
5752 | -- corresponding discriminal, which is the formal corresponding to | |
5753 | -- to the discriminant in the initialization procedure. | |
5754 | ||
996ae0b0 | 5755 | else |
294ccb21 RD |
5756 | -- Entity is unambiguous, indicate that it is referenced here |
5757 | ||
5758 | -- For a renaming of an object, always generate simple reference, | |
5114f3ff AC |
5759 | -- we don't try to keep track of assignments in this case, except |
5760 | -- in SPARK mode where renamings are traversed for generating | |
5761 | -- local effects of subprograms. | |
294ccb21 | 5762 | |
5114f3ff AC |
5763 | if Is_Object (E) |
5764 | and then Present (Renamed_Object (E)) | |
f5da7a97 | 5765 | and then not GNATprove_Mode |
5114f3ff | 5766 | then |
294ccb21 | 5767 | Generate_Reference (E, N); |
996ae0b0 | 5768 | |
21d27997 RD |
5769 | -- If the renamed entity is a private protected component, |
5770 | -- reference the original component as well. This needs to be | |
5771 | -- done because the private renamings are installed before any | |
f3d0f304 | 5772 | -- analysis has occurred. Reference to a private component will |
21d27997 RD |
5773 | -- resolve to the renaming and the original component will be |
5774 | -- left unreferenced, hence the following. | |
5775 | ||
5776 | if Is_Prival (E) then | |
5777 | Generate_Reference (Prival_Link (E), N); | |
5778 | end if; | |
5779 | ||
294ccb21 RD |
5780 | -- One odd case is that we do not want to set the Referenced flag |
5781 | -- if the entity is a label, and the identifier is the label in | |
5782 | -- the source, since this is not a reference from the point of | |
5783 | -- view of the user. | |
5784 | ||
5785 | elsif Nkind (Parent (N)) = N_Label then | |
996ae0b0 RK |
5786 | declare |
5787 | R : constant Boolean := Referenced (E); | |
294ccb21 | 5788 | |
996ae0b0 | 5789 | begin |
294ccb21 RD |
5790 | -- Generate reference unless this is an actual parameter |
5791 | -- (see comment below) | |
5792 | ||
5793 | if Is_Actual_Parameter then | |
67ce0d7e RD |
5794 | Generate_Reference (E, N); |
5795 | Set_Referenced (E, R); | |
5796 | end if; | |
996ae0b0 RK |
5797 | end; |
5798 | ||
11560bcc TQ |
5799 | -- Normal case, not a label: generate reference |
5800 | ||
a54ffd6c AC |
5801 | else |
5802 | if not Is_Actual_Parameter then | |
61c161b2 | 5803 | |
a54ffd6c | 5804 | -- Package or generic package is always a simple reference |
61c161b2 | 5805 | |
a54ffd6c AC |
5806 | if Ekind_In (E, E_Package, E_Generic_Package) then |
5807 | Generate_Reference (E, N, 'r'); | |
5808 | ||
5809 | -- Else see if we have a left hand side | |
fbf5a39b | 5810 | |
84df40f7 | 5811 | else |
a54ffd6c AC |
5812 | case Is_LHS (N) is |
5813 | when Yes => | |
5814 | Generate_Reference (E, N, 'm'); | |
5815 | ||
5816 | when No => | |
5817 | Generate_Reference (E, N, 'r'); | |
5818 | ||
5819 | -- If we don't know now, generate reference later | |
5820 | ||
d8f43ee6 HK |
5821 | when Unknown => |
5822 | Deferred_References.Append ((E, N)); | |
a54ffd6c | 5823 | end case; |
84df40f7 | 5824 | end if; |
67ce0d7e | 5825 | end if; |
996ae0b0 RK |
5826 | end if; |
5827 | ||
4c484f40 | 5828 | Set_Entity_Or_Discriminal (N, E); |
d50f4827 | 5829 | |
27dd0dde | 5830 | -- The name may designate a generalized reference, in which case |
f6f40114 AC |
5831 | -- the dereference interpretation will be included. Context is |
5832 | -- one in which a name is legal. | |
27dd0dde | 5833 | |
d50f4827 | 5834 | if Ada_Version >= Ada_2012 |
444acbdd AC |
5835 | and then |
5836 | (Nkind (Parent (N)) in N_Subexpr | |
cdabbb52 HK |
5837 | or else Nkind_In (Parent (N), N_Assignment_Statement, |
5838 | N_Object_Declaration, | |
5839 | N_Parameter_Association)) | |
d50f4827 AC |
5840 | then |
5841 | Check_Implicit_Dereference (N, Etype (E)); | |
5842 | end if; | |
996ae0b0 RK |
5843 | end if; |
5844 | end; | |
18dae814 RD |
5845 | |
5846 | -- Come here with entity set | |
5847 | ||
5848 | <<Done>> | |
5849 | Check_Restriction_No_Use_Of_Entity (N); | |
996ae0b0 RK |
5850 | end Find_Direct_Name; |
5851 | ||
5852 | ------------------------ | |
5853 | -- Find_Expanded_Name -- | |
5854 | ------------------------ | |
5855 | ||
5856 | -- This routine searches the homonym chain of the entity until it finds | |
5857 | -- an entity declared in the scope denoted by the prefix. If the entity | |
5858 | -- is private, it may nevertheless be immediately visible, if we are in | |
5859 | -- the scope of its declaration. | |
5860 | ||
5861 | procedure Find_Expanded_Name (N : Node_Id) is | |
95fef24f AC |
5862 | function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean; |
5863 | -- Determine whether expanded name Nod appears within a pragma which is | |
5864 | -- a suitable context for an abstract view of a state or variable. The | |
5865 | -- following pragmas fall in this category: | |
5866 | -- Depends | |
5867 | -- Global | |
5868 | -- Initializes | |
5869 | -- Refined_Depends | |
5870 | -- Refined_Global | |
5871 | -- | |
5872 | -- In addition, pragma Abstract_State is also considered suitable even | |
5873 | -- though it is an illegal context for an abstract view as this allows | |
5874 | -- for proper resolution of abstract views of variables. This illegal | |
5875 | -- context is later flagged in the analysis of indicator Part_Of. | |
dc726757 | 5876 | |
95fef24f AC |
5877 | ----------------------------- |
5878 | -- In_Abstract_View_Pragma -- | |
5879 | ----------------------------- | |
dc726757 | 5880 | |
95fef24f | 5881 | function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is |
dc726757 HK |
5882 | Par : Node_Id; |
5883 | ||
5884 | begin | |
5885 | -- Climb the parent chain looking for a pragma | |
5886 | ||
95fef24f | 5887 | Par := Nod; |
dc726757 | 5888 | while Present (Par) loop |
95fef24f | 5889 | if Nkind (Par) = N_Pragma then |
6e759c2a BD |
5890 | if Nam_In (Pragma_Name_Unmapped (Par), |
5891 | Name_Abstract_State, | |
5892 | Name_Depends, | |
5893 | Name_Global, | |
5894 | Name_Initializes, | |
5895 | Name_Refined_Depends, | |
5896 | Name_Refined_Global) | |
95fef24f AC |
5897 | then |
5898 | return True; | |
5899 | ||
5900 | -- Otherwise the pragma is not a legal context for an abstract | |
5901 | -- view. | |
5902 | ||
5903 | else | |
5904 | exit; | |
5905 | end if; | |
dc726757 HK |
5906 | |
5907 | -- Prevent the search from going too far | |
5908 | ||
5909 | elsif Is_Body_Or_Package_Declaration (Par) then | |
95fef24f | 5910 | exit; |
dc726757 HK |
5911 | end if; |
5912 | ||
5913 | Par := Parent (Par); | |
5914 | end loop; | |
5915 | ||
5916 | return False; | |
95fef24f | 5917 | end In_Abstract_View_Pragma; |
dc726757 HK |
5918 | |
5919 | -- Local variables | |
5920 | ||
07fc65c4 GB |
5921 | Selector : constant Node_Id := Selector_Name (N); |
5922 | Candidate : Entity_Id := Empty; | |
996ae0b0 | 5923 | P_Name : Entity_Id; |
996ae0b0 RK |
5924 | Id : Entity_Id; |
5925 | ||
dc726757 HK |
5926 | -- Start of processing for Find_Expanded_Name |
5927 | ||
996ae0b0 RK |
5928 | begin |
5929 | P_Name := Entity (Prefix (N)); | |
996ae0b0 | 5930 | |
ff81221b ES |
5931 | -- If the prefix is a renamed package, look for the entity in the |
5932 | -- original package. | |
996ae0b0 RK |
5933 | |
5934 | if Ekind (P_Name) = E_Package | |
5935 | and then Present (Renamed_Object (P_Name)) | |
5936 | then | |
5937 | P_Name := Renamed_Object (P_Name); | |
5938 | ||
5939 | -- Rewrite node with entity field pointing to renamed object | |
5940 | ||
5941 | Rewrite (Prefix (N), New_Copy (Prefix (N))); | |
5942 | Set_Entity (Prefix (N), P_Name); | |
5943 | ||
5944 | -- If the prefix is an object of a concurrent type, look for | |
5945 | -- the entity in the associated task or protected type. | |
5946 | ||
5947 | elsif Is_Concurrent_Type (Etype (P_Name)) then | |
5948 | P_Name := Etype (P_Name); | |
5949 | end if; | |
5950 | ||
5951 | Id := Current_Entity (Selector); | |
5952 | ||
923fa078 RD |
5953 | declare |
5954 | Is_New_Candidate : Boolean; | |
996ae0b0 | 5955 | |
923fa078 RD |
5956 | begin |
5957 | while Present (Id) loop | |
5958 | if Scope (Id) = P_Name then | |
5959 | Candidate := Id; | |
5960 | Is_New_Candidate := True; | |
5961 | ||
dc726757 | 5962 | -- Handle abstract views of states and variables. These are |
95fef24f AC |
5963 | -- acceptable candidates only when the reference to the view |
5964 | -- appears in certain pragmas. | |
dc726757 HK |
5965 | |
5966 | if Ekind (Id) = E_Abstract_State | |
5967 | and then From_Limited_With (Id) | |
5968 | and then Present (Non_Limited_View (Id)) | |
5969 | then | |
95fef24f | 5970 | if In_Abstract_View_Pragma (N) then |
dc726757 HK |
5971 | Candidate := Non_Limited_View (Id); |
5972 | Is_New_Candidate := True; | |
5973 | ||
95fef24f AC |
5974 | -- Hide the candidate because it is not used in a proper |
5975 | -- context. | |
dc726757 HK |
5976 | |
5977 | else | |
5978 | Candidate := Empty; | |
5979 | Is_New_Candidate := False; | |
5980 | end if; | |
5981 | end if; | |
5982 | ||
47346923 AC |
5983 | -- Ada 2005 (AI-217): Handle shadow entities associated with |
5984 | -- types declared in limited-withed nested packages. We don't need | |
5985 | -- to handle E_Incomplete_Subtype entities because the entities | |
5986 | -- in the limited view are always E_Incomplete_Type and | |
5987 | -- E_Class_Wide_Type entities (see Build_Limited_Views). | |
5988 | ||
5989 | -- Regarding the expression used to evaluate the scope, it | |
5990 | -- is important to note that the limited view also has shadow | |
5991 | -- entities associated nested packages. For this reason the | |
5992 | -- correct scope of the entity is the scope of the real entity. | |
11560bcc TQ |
5993 | -- The non-limited view may itself be incomplete, in which case |
5994 | -- get the full view if available. | |
923fa078 | 5995 | |
47346923 | 5996 | elsif Ekind_In (Id, E_Incomplete_Type, E_Class_Wide_Type) |
dc726757 | 5997 | and then From_Limited_With (Id) |
923fa078 RD |
5998 | and then Present (Non_Limited_View (Id)) |
5999 | and then Scope (Non_Limited_View (Id)) = P_Name | |
6000 | then | |
11560bcc | 6001 | Candidate := Get_Full_View (Non_Limited_View (Id)); |
923fa078 | 6002 | Is_New_Candidate := True; |
996ae0b0 | 6003 | |
780d73d7 AC |
6004 | -- An unusual case arises with a fully qualified name for an |
6005 | -- entity local to a generic child unit package, within an | |
6006 | -- instantiation of that package. The name of the unit now | |
6007 | -- denotes the renaming created within the instance. This is | |
6008 | -- only relevant in an instance body, see below. | |
6009 | ||
6010 | elsif Is_Generic_Instance (Scope (Id)) | |
6011 | and then In_Open_Scopes (Scope (Id)) | |
6012 | and then In_Instance_Body | |
6013 | and then Ekind (Scope (Id)) = E_Package | |
6014 | and then Ekind (Id) = E_Package | |
6015 | and then Renamed_Entity (Id) = Scope (Id) | |
0cb81445 | 6016 | and then Is_Immediately_Visible (P_Name) |
780d73d7 AC |
6017 | then |
6018 | Is_New_Candidate := True; | |
6019 | ||
996ae0b0 | 6020 | else |
923fa078 | 6021 | Is_New_Candidate := False; |
996ae0b0 | 6022 | end if; |
996ae0b0 | 6023 | |
923fa078 | 6024 | if Is_New_Candidate then |
ddbc55d8 AC |
6025 | |
6026 | -- If entity is a child unit, either it is a visible child of | |
6027 | -- the prefix, or we are in the body of a generic prefix, as | |
6028 | -- will happen when a child unit is instantiated in the body | |
6029 | -- of a generic parent. This is because the instance body does | |
6030 | -- not restore the full compilation context, given that all | |
6031 | -- non-local references have been captured. | |
6032 | ||
8ca1ee5d | 6033 | if Is_Child_Unit (Id) or else P_Name = Standard_Standard then |
ddbc55d8 AC |
6034 | exit when Is_Visible_Lib_Unit (Id) |
6035 | or else (Is_Child_Unit (Id) | |
6036 | and then In_Open_Scopes (Scope (Id)) | |
6037 | and then In_Instance_Body); | |
923fa078 | 6038 | else |
8398e82e | 6039 | exit when not Is_Hidden (Id); |
923fa078 | 6040 | end if; |
8398e82e AC |
6041 | |
6042 | exit when Is_Immediately_Visible (Id); | |
923fa078 RD |
6043 | end if; |
6044 | ||
6045 | Id := Homonym (Id); | |
6046 | end loop; | |
6047 | end; | |
996ae0b0 RK |
6048 | |
6049 | if No (Id) | |
ac7d724d | 6050 | and then Ekind_In (P_Name, E_Procedure, E_Function) |
996ae0b0 RK |
6051 | and then Is_Generic_Instance (P_Name) |
6052 | then | |
6053 | -- Expanded name denotes entity in (instance of) generic subprogram. | |
6054 | -- The entity may be in the subprogram instance, or may denote one of | |
6055 | -- the formals, which is declared in the enclosing wrapper package. | |
6056 | ||
6057 | P_Name := Scope (P_Name); | |
996ae0b0 | 6058 | |
bc41faa2 | 6059 | Id := Current_Entity (Selector); |
996ae0b0 | 6060 | while Present (Id) loop |
bc41faa2 | 6061 | exit when Scope (Id) = P_Name; |
996ae0b0 RK |
6062 | Id := Homonym (Id); |
6063 | end loop; | |
6064 | end if; | |
6065 | ||
bc41faa2 | 6066 | if No (Id) or else Chars (Id) /= Chars (Selector) then |
996ae0b0 RK |
6067 | Set_Etype (N, Any_Type); |
6068 | ||
ff81221b ES |
6069 | -- If we are looking for an entity defined in System, try to find it |
6070 | -- in the child package that may have been provided as an extension | |
6071 | -- to System. The Extend_System pragma will have supplied the name of | |
6072 | -- the extension, which may have to be loaded. | |
996ae0b0 RK |
6073 | |
6074 | if Chars (P_Name) = Name_System | |
6075 | and then Scope (P_Name) = Standard_Standard | |
fbf5a39b | 6076 | and then Present (System_Extend_Unit) |
996ae0b0 RK |
6077 | and then Present_System_Aux (N) |
6078 | then | |
6079 | Set_Entity (Prefix (N), System_Aux_Id); | |
6080 | Find_Expanded_Name (N); | |
6081 | return; | |
6082 | ||
95fef24f AC |
6083 | -- There is an implicit instance of the predefined operator in |
6084 | -- the given scope. The operator entity is defined in Standard. | |
6085 | -- Has_Implicit_Operator makes the node into an Expanded_Name. | |
6086 | ||
fbf5a39b AC |
6087 | elsif Nkind (Selector) = N_Operator_Symbol |
6088 | and then Has_Implicit_Operator (N) | |
996ae0b0 | 6089 | then |
996ae0b0 RK |
6090 | return; |
6091 | ||
95fef24f AC |
6092 | -- If there is no literal defined in the scope denoted by the |
6093 | -- prefix, the literal may belong to (a type derived from) | |
6094 | -- Standard_Character, for which we have no explicit literals. | |
6095 | ||
996ae0b0 RK |
6096 | elsif Nkind (Selector) = N_Character_Literal |
6097 | and then Has_Implicit_Character_Literal (N) | |
6098 | then | |
996ae0b0 RK |
6099 | return; |
6100 | ||
6101 | else | |
ff81221b ES |
6102 | -- If the prefix is a single concurrent object, use its name in |
6103 | -- the error message, rather than that of the anonymous type. | |
996ae0b0 RK |
6104 | |
6105 | if Is_Concurrent_Type (P_Name) | |
6106 | and then Is_Internal_Name (Chars (P_Name)) | |
6107 | then | |
6108 | Error_Msg_Node_2 := Entity (Prefix (N)); | |
6109 | else | |
6110 | Error_Msg_Node_2 := P_Name; | |
6111 | end if; | |
6112 | ||
6113 | if P_Name = System_Aux_Id then | |
6114 | P_Name := Scope (P_Name); | |
6115 | Set_Entity (Prefix (N), P_Name); | |
6116 | end if; | |
6117 | ||
6118 | if Present (Candidate) then | |
6119 | ||
16ca248a ES |
6120 | -- If we know that the unit is a child unit we can give a more |
6121 | -- accurate error message. | |
6122 | ||
996ae0b0 | 6123 | if Is_Child_Unit (Candidate) then |
edd63e9b | 6124 | |
16ca248a ES |
6125 | -- If the candidate is a private child unit and we are in |
6126 | -- the visible part of a public unit, specialize the error | |
6127 | -- message. There might be a private with_clause for it, | |
6128 | -- but it is not currently active. | |
edd63e9b ES |
6129 | |
6130 | if Is_Private_Descendant (Candidate) | |
6131 | and then Ekind (Current_Scope) = E_Package | |
6132 | and then not In_Private_Part (Current_Scope) | |
6133 | and then not Is_Private_Descendant (Current_Scope) | |
6134 | then | |
95fef24f AC |
6135 | Error_Msg_N |
6136 | ("private child unit& is not visible here", Selector); | |
16ca248a ES |
6137 | |
6138 | -- Normal case where we have a missing with for a child unit | |
6139 | ||
edd63e9b | 6140 | else |
16ca248a | 6141 | Error_Msg_Qual_Level := 99; |
ed2233dc AC |
6142 | Error_Msg_NE -- CODEFIX |
6143 | ("missing `WITH &;`", Selector, Candidate); | |
16ca248a | 6144 | Error_Msg_Qual_Level := 0; |
edd63e9b | 6145 | end if; |
16ca248a ES |
6146 | |
6147 | -- Here we don't know that this is a child unit | |
6148 | ||
996ae0b0 RK |
6149 | else |
6150 | Error_Msg_NE ("& is not a visible entity of&", N, Selector); | |
6151 | end if; | |
6152 | ||
6153 | else | |
6154 | -- Within the instantiation of a child unit, the prefix may | |
16ca248a | 6155 | -- denote the parent instance, but the selector has the name |
58747e48 AC |
6156 | -- of the original child. That is to say, when A.B appears |
6157 | -- within an instantiation of generic child unit B, the scope | |
6158 | -- stack includes an instance of A (P_Name) and an instance | |
6159 | -- of B under some other name. We scan the scope to find this | |
6160 | -- child instance, which is the desired entity. | |
6161 | -- Note that the parent may itself be a child instance, if | |
6162 | -- the reference is of the form A.B.C, in which case A.B has | |
6163 | -- already been rewritten with the proper entity. | |
6164 | ||
6165 | if In_Open_Scopes (P_Name) | |
996ae0b0 RK |
6166 | and then Is_Generic_Instance (P_Name) |
6167 | then | |
6168 | declare | |
58747e48 AC |
6169 | Gen_Par : constant Entity_Id := |
6170 | Generic_Parent (Specification | |
6171 | (Unit_Declaration_Node (P_Name))); | |
996ae0b0 RK |
6172 | S : Entity_Id := Current_Scope; |
6173 | P : Entity_Id; | |
6174 | ||
6175 | begin | |
6176 | for J in reverse 0 .. Scope_Stack.Last loop | |
6177 | S := Scope_Stack.Table (J).Entity; | |
6178 | ||
6179 | exit when S = Standard_Standard; | |
6180 | ||
bce79204 AC |
6181 | if Ekind_In (S, E_Function, |
6182 | E_Package, | |
6183 | E_Procedure) | |
996ae0b0 | 6184 | then |
95fef24f AC |
6185 | P := |
6186 | Generic_Parent (Specification | |
6187 | (Unit_Declaration_Node (S))); | |
996ae0b0 | 6188 | |
58747e48 AC |
6189 | -- Check that P is a generic child of the generic |
6190 | -- parent of the prefix. | |
6191 | ||
996ae0b0 | 6192 | if Present (P) |
996ae0b0 | 6193 | and then Chars (P) = Chars (Selector) |
58747e48 | 6194 | and then Scope (P) = Gen_Par |
996ae0b0 RK |
6195 | then |
6196 | Id := S; | |
a77842bd | 6197 | goto Found; |
996ae0b0 RK |
6198 | end if; |
6199 | end if; | |
6200 | ||
6201 | end loop; | |
6202 | end; | |
6203 | end if; | |
6204 | ||
16ca248a ES |
6205 | -- If this is a selection from Ada, System or Interfaces, then |
6206 | -- we assume a missing with for the corresponding package. | |
6207 | ||
6208 | if Is_Known_Unit (N) then | |
fbe627af RD |
6209 | if not Error_Posted (N) then |
6210 | Error_Msg_Node_2 := Selector; | |
ed2233dc AC |
6211 | Error_Msg_N -- CODEFIX |
6212 | ("missing `WITH &.&;`", Prefix (N)); | |
fbe627af | 6213 | end if; |
996ae0b0 | 6214 | |
16ca248a ES |
6215 | -- If this is a selection from a dummy package, then suppress |
6216 | -- the error message, of course the entity is missing if the | |
a90bd866 | 6217 | -- package is missing. |
996ae0b0 RK |
6218 | |
6219 | elsif Sloc (Error_Msg_Node_2) = No_Location then | |
6220 | null; | |
6221 | ||
6222 | -- Here we have the case of an undefined component | |
6223 | ||
6224 | else | |
d6a24cdb AC |
6225 | -- The prefix may hide a homonym in the context that |
6226 | -- declares the desired entity. This error can use a | |
6227 | -- specialized message. | |
6228 | ||
8398e82e | 6229 | if In_Open_Scopes (P_Name) then |
d6a24cdb AC |
6230 | declare |
6231 | H : constant Entity_Id := Homonym (P_Name); | |
0d354370 | 6232 | |
d6a24cdb | 6233 | begin |
8398e82e AC |
6234 | if Present (H) |
6235 | and then Is_Compilation_Unit (H) | |
6236 | and then | |
6237 | (Is_Immediately_Visible (H) | |
6238 | or else Is_Visible_Lib_Unit (H)) | |
6239 | then | |
6240 | Id := First_Entity (H); | |
6241 | while Present (Id) loop | |
6242 | if Chars (Id) = Chars (Selector) then | |
6243 | Error_Msg_Qual_Level := 99; | |
6244 | Error_Msg_Name_1 := Chars (Selector); | |
6245 | Error_Msg_NE | |
6246 | ("% not declared in&", N, P_Name); | |
6247 | Error_Msg_NE | |
6248 | ("\use fully qualified name starting with " | |
6249 | & "Standard to make& visible", N, H); | |
6250 | Error_Msg_Qual_Level := 0; | |
6251 | goto Done; | |
6252 | end if; | |
d6a24cdb | 6253 | |
8398e82e AC |
6254 | Next_Entity (Id); |
6255 | end loop; | |
6256 | end if; | |
689cb4ac | 6257 | |
690943fc | 6258 | -- If not found, standard error message |
689cb4ac AC |
6259 | |
6260 | Error_Msg_NE ("& not declared in&", N, Selector); | |
6261 | ||
6262 | <<Done>> null; | |
d6a24cdb AC |
6263 | end; |
6264 | ||
6265 | else | |
780d73d7 AC |
6266 | -- Might be worth specializing the case when the prefix |
6267 | -- is a limited view. | |
6268 | -- ... not declared in limited view of... | |
6269 | ||
d6a24cdb AC |
6270 | Error_Msg_NE ("& not declared in&", N, Selector); |
6271 | end if; | |
996ae0b0 | 6272 | |
bc41faa2 | 6273 | -- Check for misspelling of some entity in prefix |
996ae0b0 RK |
6274 | |
6275 | Id := First_Entity (P_Name); | |
294ccb21 RD |
6276 | while Present (Id) loop |
6277 | if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector)) | |
6278 | and then not Is_Internal_Name (Chars (Id)) | |
6279 | then | |
deef4289 | 6280 | Error_Msg_NE -- CODEFIX |
294ccb21 RD |
6281 | ("possible misspelling of&", Selector, Id); |
6282 | exit; | |
6283 | end if; | |
996ae0b0 | 6284 | |
294ccb21 RD |
6285 | Next_Entity (Id); |
6286 | end loop; | |
996ae0b0 RK |
6287 | |
6288 | -- Specialize the message if this may be an instantiation | |
6289 | -- of a child unit that was not mentioned in the context. | |
6290 | ||
6291 | if Nkind (Parent (N)) = N_Package_Instantiation | |
6292 | and then Is_Generic_Instance (Entity (Prefix (N))) | |
6293 | and then Is_Compilation_Unit | |
294ccb21 | 6294 | (Generic_Parent (Parent (Entity (Prefix (N))))) |
996ae0b0 | 6295 | then |
16ca248a | 6296 | Error_Msg_Node_2 := Selector; |
ed2233dc AC |
6297 | Error_Msg_N -- CODEFIX |
6298 | ("\missing `WITH &.&;`", Prefix (N)); | |
996ae0b0 RK |
6299 | end if; |
6300 | end if; | |
6301 | end if; | |
6302 | ||
6303 | Id := Any_Id; | |
6304 | end if; | |
6305 | end if; | |
6306 | ||
a77842bd | 6307 | <<Found>> |
996ae0b0 RK |
6308 | if Comes_From_Source (N) |
6309 | and then Is_Remote_Access_To_Subprogram_Type (Id) | |
d1d81616 | 6310 | and then Ekind (Id) = E_Access_Subprogram_Type |
a77842bd | 6311 | and then Present (Equivalent_Type (Id)) |
996ae0b0 | 6312 | then |
16ca248a ES |
6313 | -- If we are not actually generating distribution code (i.e. the |
6314 | -- current PCS is the dummy non-distributed version), then the | |
6315 | -- Equivalent_Type will be missing, and Id should be treated as | |
6316 | -- a regular access-to-subprogram type. | |
a77842bd | 6317 | |
996ae0b0 RK |
6318 | Id := Equivalent_Type (Id); |
6319 | Set_Chars (Selector, Chars (Id)); | |
6320 | end if; | |
6321 | ||
0ab80019 | 6322 | -- Ada 2005 (AI-50217): Check usage of entities in limited withed units |
19f0526a | 6323 | |
7b56a91b AC |
6324 | if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then |
6325 | if From_Limited_With (Id) | |
0fb2ea01 AC |
6326 | or else Is_Type (Id) |
6327 | or else Ekind (Id) = E_Package | |
996ae0b0 RK |
6328 | then |
6329 | null; | |
6330 | else | |
6331 | Error_Msg_N | |
497a660d AC |
6332 | ("limited withed package can only be used to access incomplete " |
6333 | & "types", N); | |
996ae0b0 RK |
6334 | end if; |
6335 | end if; | |
6336 | ||
6337 | if Is_Task_Type (P_Name) | |
6338 | and then ((Ekind (Id) = E_Entry | |
ac7d724d | 6339 | and then Nkind (Parent (N)) /= N_Attribute_Reference) |
294ccb21 | 6340 | or else |
ac7d724d ES |
6341 | (Ekind (Id) = E_Entry_Family |
6342 | and then | |
6343 | Nkind (Parent (Parent (N))) /= N_Attribute_Reference)) | |
996ae0b0 | 6344 | then |
a53c5613 AC |
6345 | -- If both the task type and the entry are in scope, this may still |
6346 | -- be the expanded name of an entry formal. | |
996ae0b0 | 6347 | |
a53c5613 AC |
6348 | if In_Open_Scopes (Id) |
6349 | and then Nkind (Parent (N)) = N_Selected_Component | |
6350 | then | |
6351 | null; | |
6352 | ||
6353 | else | |
6354 | -- It is an entry call after all, either to the current task | |
6355 | -- (which will deadlock) or to an enclosing task. | |
6356 | ||
6357 | Analyze_Selected_Component (N); | |
6358 | return; | |
6359 | end if; | |
996ae0b0 RK |
6360 | end if; |
6361 | ||
6362 | Change_Selected_Component_To_Expanded_Name (N); | |
07fc65c4 | 6363 | |
a54ffd6c AC |
6364 | -- Set appropriate type |
6365 | ||
6366 | if Is_Type (Id) then | |
6367 | Set_Etype (N, Id); | |
6368 | else | |
6369 | Set_Etype (N, Get_Full_View (Etype (Id))); | |
6370 | end if; | |
6371 | ||
07fc65c4 | 6372 | -- Do style check and generate reference, but skip both steps if this |
16ca248a ES |
6373 | -- entity has homonyms, since we may not have the right homonym set yet. |
6374 | -- The proper homonym will be set during the resolve phase. | |
07fc65c4 GB |
6375 | |
6376 | if Has_Homonym (Id) then | |
6377 | Set_Entity (N, Id); | |
a54ffd6c | 6378 | |
07fc65c4 | 6379 | else |
4c484f40 | 6380 | Set_Entity_Or_Discriminal (N, Id); |
84df40f7 | 6381 | |
a54ffd6c AC |
6382 | case Is_LHS (N) is |
6383 | when Yes => | |
6384 | Generate_Reference (Id, N, 'm'); | |
d8f43ee6 | 6385 | |
a54ffd6c AC |
6386 | when No => |
6387 | Generate_Reference (Id, N, 'r'); | |
d8f43ee6 | 6388 | |
a54ffd6c AC |
6389 | when Unknown => |
6390 | Deferred_References.Append ((Id, N)); | |
6391 | end case; | |
996ae0b0 RK |
6392 | end if; |
6393 | ||
30196a76 RD |
6394 | -- Check for violation of No_Wide_Characters |
6395 | ||
6396 | Check_Wide_Character_Restriction (Id, N); | |
6397 | ||
16ca248a ES |
6398 | -- If the Ekind of the entity is Void, it means that all homonyms are |
6399 | -- hidden from all visibility (RM 8.3(5,14-20)). | |
996ae0b0 RK |
6400 | |
6401 | if Ekind (Id) = E_Void then | |
6402 | Premature_Usage (N); | |
6403 | ||
ac7d724d | 6404 | elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then |
996ae0b0 RK |
6405 | declare |
6406 | H : Entity_Id := Homonym (Id); | |
6407 | ||
6408 | begin | |
6409 | while Present (H) loop | |
35ae2ed8 | 6410 | if Scope (H) = Scope (Id) |
ac7d724d ES |
6411 | and then (not Is_Hidden (H) |
6412 | or else Is_Immediately_Visible (H)) | |
35ae2ed8 | 6413 | then |
996ae0b0 RK |
6414 | Collect_Interps (N); |
6415 | exit; | |
6416 | end if; | |
6417 | ||
6418 | H := Homonym (H); | |
6419 | end loop; | |
fbf5a39b | 6420 | |
16ca248a ES |
6421 | -- If an extension of System is present, collect possible explicit |
6422 | -- overloadings declared in the extension. | |
fbf5a39b AC |
6423 | |
6424 | if Chars (P_Name) = Name_System | |
6425 | and then Scope (P_Name) = Standard_Standard | |
6426 | and then Present (System_Extend_Unit) | |
6427 | and then Present_System_Aux (N) | |
6428 | then | |
6429 | H := Current_Entity (Id); | |
6430 | ||
6431 | while Present (H) loop | |
6432 | if Scope (H) = System_Aux_Id then | |
6433 | Add_One_Interp (N, H, Etype (H)); | |
6434 | end if; | |
6435 | ||
6436 | H := Homonym (H); | |
6437 | end loop; | |
6438 | end if; | |
996ae0b0 RK |
6439 | end; |
6440 | end if; | |
6441 | ||
6442 | if Nkind (Selector_Name (N)) = N_Operator_Symbol | |
6443 | and then Scope (Id) /= Standard_Standard | |
6444 | then | |
16ca248a ES |
6445 | -- In addition to user-defined operators in the given scope, there |
6446 | -- may be an implicit instance of the predefined operator. The | |
6447 | -- operator (defined in Standard) is found in Has_Implicit_Operator, | |
6448 | -- and added to the interpretations. Procedure Add_One_Interp will | |
6449 | -- determine which hides which. | |
996ae0b0 RK |
6450 | |
6451 | if Has_Implicit_Operator (N) then | |
6452 | null; | |
6453 | end if; | |
6454 | end if; | |
be4c5193 AC |
6455 | |
6456 | -- If there is a single interpretation for N we can generate a | |
6457 | -- reference to the unique entity found. | |
6458 | ||
6459 | if Is_Overloadable (Id) and then not Is_Overloaded (N) then | |
6460 | Generate_Reference (Id, N); | |
6461 | end if; | |
fb757f7d AC |
6462 | |
6463 | Check_Restriction_No_Use_Of_Entity (N); | |
996ae0b0 RK |
6464 | end Find_Expanded_Name; |
6465 | ||
6466 | ------------------------- | |
6467 | -- Find_Renamed_Entity -- | |
6468 | ------------------------- | |
6469 | ||
6470 | function Find_Renamed_Entity | |
6471 | (N : Node_Id; | |
6472 | Nam : Node_Id; | |
6473 | New_S : Entity_Id; | |
6474 | Is_Actual : Boolean := False) return Entity_Id | |
6475 | is | |
fbf5a39b | 6476 | Ind : Interp_Index; |
996ae0b0 RK |
6477 | I1 : Interp_Index := 0; -- Suppress junk warnings |
6478 | It : Interp; | |
6479 | It1 : Interp; | |
6480 | Old_S : Entity_Id; | |
6481 | Inst : Entity_Id; | |
6482 | ||
996ae0b0 RK |
6483 | function Is_Visible_Operation (Op : Entity_Id) return Boolean; |
6484 | -- If the renamed entity is an implicit operator, check whether it is | |
16ca248a ES |
6485 | -- visible because its operand type is properly visible. This check |
6486 | -- applies to explicit renamed entities that appear in the source in a | |
6487 | -- renaming declaration or a formal subprogram instance, but not to | |
6488 | -- default generic actuals with a name. | |
996ae0b0 | 6489 | |
923fa078 RD |
6490 | function Report_Overload return Entity_Id; |
6491 | -- List possible interpretations, and specialize message in the | |
6492 | -- case of a generic actual. | |
6493 | ||
6494 | function Within (Inner, Outer : Entity_Id) return Boolean; | |
16ca248a ES |
6495 | -- Determine whether a candidate subprogram is defined within the |
6496 | -- enclosing instance. If yes, it has precedence over outer candidates. | |
923fa078 | 6497 | |
996ae0b0 RK |
6498 | -------------------------- |
6499 | -- Is_Visible_Operation -- | |
6500 | -------------------------- | |
6501 | ||
6502 | function Is_Visible_Operation (Op : Entity_Id) return Boolean is | |
6503 | Scop : Entity_Id; | |
6504 | Typ : Entity_Id; | |
6505 | Btyp : Entity_Id; | |
6506 | ||
6507 | begin | |
6508 | if Ekind (Op) /= E_Operator | |
6509 | or else Scope (Op) /= Standard_Standard | |
6510 | or else (In_Instance | |
ac7d724d ES |
6511 | and then (not Is_Actual |
6512 | or else Present (Enclosing_Instance))) | |
996ae0b0 RK |
6513 | then |
6514 | return True; | |
6515 | ||
6516 | else | |
6517 | -- For a fixed point type operator, check the resulting type, | |
6518 | -- because it may be a mixed mode integer * fixed operation. | |
6519 | ||
6520 | if Present (Next_Formal (First_Formal (New_S))) | |
6521 | and then Is_Fixed_Point_Type (Etype (New_S)) | |
6522 | then | |
6523 | Typ := Etype (New_S); | |
6524 | else | |
6525 | Typ := Etype (First_Formal (New_S)); | |
6526 | end if; | |
6527 | ||
6528 | Btyp := Base_Type (Typ); | |
6529 | ||
6530 | if Nkind (Nam) /= N_Expanded_Name then | |
6531 | return (In_Open_Scopes (Scope (Btyp)) | |
6532 | or else Is_Potentially_Use_Visible (Btyp) | |
6533 | or else In_Use (Btyp) | |
6534 | or else In_Use (Scope (Btyp))); | |
6535 | ||
6536 | else | |
6537 | Scop := Entity (Prefix (Nam)); | |
6538 | ||
6539 | if Ekind (Scop) = E_Package | |
6540 | and then Present (Renamed_Object (Scop)) | |
6541 | then | |
6542 | Scop := Renamed_Object (Scop); | |
6543 | end if; | |
6544 | ||
6545 | -- Operator is visible if prefix of expanded name denotes | |
16b05213 | 6546 | -- scope of type, or else type is defined in System_Aux |
996ae0b0 RK |
6547 | -- and the prefix denotes System. |
6548 | ||
6549 | return Scope (Btyp) = Scop | |
6550 | or else (Scope (Btyp) = System_Aux_Id | |
6551 | and then Scope (Scope (Btyp)) = Scop); | |
6552 | end if; | |
6553 | end if; | |
6554 | end Is_Visible_Operation; | |
6555 | ||
6556 | ------------ | |
6557 | -- Within -- | |
6558 | ------------ | |
6559 | ||
6560 | function Within (Inner, Outer : Entity_Id) return Boolean is | |
16ca248a | 6561 | Sc : Entity_Id; |
996ae0b0 RK |
6562 | |
6563 | begin | |
16ca248a | 6564 | Sc := Scope (Inner); |
996ae0b0 | 6565 | while Sc /= Standard_Standard loop |
996ae0b0 RK |
6566 | if Sc = Outer then |
6567 | return True; | |
6568 | else | |
6569 | Sc := Scope (Sc); | |
6570 | end if; | |
6571 | end loop; | |
6572 | ||
6573 | return False; | |
6574 | end Within; | |
6575 | ||
923fa078 RD |
6576 | --------------------- |
6577 | -- Report_Overload -- | |
6578 | --------------------- | |
fbf5a39b AC |
6579 | |
6580 | function Report_Overload return Entity_Id is | |
6581 | begin | |
6582 | if Is_Actual then | |
ed2233dc | 6583 | Error_Msg_NE -- CODEFIX |
fbf5a39b | 6584 | ("ambiguous actual subprogram&, " & |
923fa078 | 6585 | "possible interpretations:", N, Nam); |
fbf5a39b | 6586 | else |
ed2233dc | 6587 | Error_Msg_N -- CODEFIX |
fbf5a39b | 6588 | ("ambiguous subprogram, " & |
923fa078 | 6589 | "possible interpretations:", N); |
fbf5a39b AC |
6590 | end if; |
6591 | ||
6592 | List_Interps (Nam, N); | |
6593 | return Old_S; | |
6594 | end Report_Overload; | |
6595 | ||
0187b60e | 6596 | -- Start of processing for Find_Renamed_Entity |
996ae0b0 RK |
6597 | |
6598 | begin | |
6599 | Old_S := Any_Id; | |
6600 | Candidate_Renaming := Empty; | |
6601 | ||
fc193526 | 6602 | if Is_Overloaded (Nam) then |
fbf5a39b | 6603 | Get_First_Interp (Nam, Ind, It); |
996ae0b0 | 6604 | while Present (It.Nam) loop |
996ae0b0 | 6605 | if Entity_Matches_Spec (It.Nam, New_S) |
fc193526 | 6606 | and then Is_Visible_Operation (It.Nam) |
996ae0b0 RK |
6607 | then |
6608 | if Old_S /= Any_Id then | |
6609 | ||
6610 | -- Note: The call to Disambiguate only happens if a | |
6611 | -- previous interpretation was found, in which case I1 | |
6612 | -- has received a value. | |
6613 | ||
fbf5a39b | 6614 | It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S)); |
996ae0b0 RK |
6615 | |
6616 | if It1 = No_Interp then | |
996ae0b0 RK |
6617 | Inst := Enclosing_Instance; |
6618 | ||
6619 | if Present (Inst) then | |
996ae0b0 | 6620 | if Within (It.Nam, Inst) then |
444acbdd AC |
6621 | if Within (Old_S, Inst) then |
6622 | ||
6623 | -- Choose the innermost subprogram, which would | |
6624 | -- have hidden the outer one in the generic. | |
6625 | ||
6626 | if Scope_Depth (It.Nam) < | |
6627 | Scope_Depth (Old_S) | |
6628 | then | |
6629 | return Old_S; | |
444acbdd AC |
6630 | else |
6631 | return It.Nam; | |
6632 | end if; | |
6633 | end if; | |
6634 | ||
996ae0b0 RK |
6635 | elsif Within (Old_S, Inst) then |
6636 | return (Old_S); | |
444acbdd | 6637 | |
996ae0b0 | 6638 | else |
fbf5a39b | 6639 | return Report_Overload; |
996ae0b0 RK |
6640 | end if; |
6641 | ||
3ddd922e | 6642 | -- If not within an instance, ambiguity is real |
444acbdd | 6643 | |
996ae0b0 | 6644 | else |
fbf5a39b | 6645 | return Report_Overload; |
996ae0b0 RK |
6646 | end if; |
6647 | ||
6648 | else | |
6649 | Old_S := It1.Nam; | |
6650 | exit; | |
6651 | end if; | |
6652 | ||
6653 | else | |
fbf5a39b | 6654 | I1 := Ind; |
996ae0b0 RK |
6655 | Old_S := It.Nam; |
6656 | end if; | |
6657 | ||
6658 | elsif | |
6659 | Present (First_Formal (It.Nam)) | |
6660 | and then Present (First_Formal (New_S)) | |
ac7d724d ES |
6661 | and then (Base_Type (Etype (First_Formal (It.Nam))) = |
6662 | Base_Type (Etype (First_Formal (New_S)))) | |
996ae0b0 RK |
6663 | then |
6664 | Candidate_Renaming := It.Nam; | |
6665 | end if; | |
6666 | ||
fbf5a39b | 6667 | Get_Next_Interp (Ind, It); |
996ae0b0 RK |
6668 | end loop; |
6669 | ||
6670 | Set_Entity (Nam, Old_S); | |
1138cf59 AC |
6671 | |
6672 | if Old_S /= Any_Id then | |
6673 | Set_Is_Overloaded (Nam, False); | |
6674 | end if; | |
fc193526 HK |
6675 | |
6676 | -- Non-overloaded case | |
6677 | ||
6678 | else | |
60aa5228 AC |
6679 | if Is_Actual |
6680 | and then Present (Enclosing_Instance) | |
6681 | and then Entity_Matches_Spec (Entity (Nam), New_S) | |
6682 | then | |
fc193526 HK |
6683 | Old_S := Entity (Nam); |
6684 | ||
6685 | elsif Entity_Matches_Spec (Entity (Nam), New_S) then | |
6686 | Candidate_Renaming := New_S; | |
6687 | ||
6688 | if Is_Visible_Operation (Entity (Nam)) then | |
6689 | Old_S := Entity (Nam); | |
6690 | end if; | |
6691 | ||
6692 | elsif Present (First_Formal (Entity (Nam))) | |
6693 | and then Present (First_Formal (New_S)) | |
6694 | and then (Base_Type (Etype (First_Formal (Entity (Nam)))) = | |
6695 | Base_Type (Etype (First_Formal (New_S)))) | |
6696 | then | |
6697 | Candidate_Renaming := Entity (Nam); | |
6698 | end if; | |
996ae0b0 RK |
6699 | end if; |
6700 | ||
6701 | return Old_S; | |
6702 | end Find_Renamed_Entity; | |
6703 | ||
6704 | ----------------------------- | |
6705 | -- Find_Selected_Component -- | |
6706 | ----------------------------- | |
6707 | ||
6708 | procedure Find_Selected_Component (N : Node_Id) is | |
fbf5a39b | 6709 | P : constant Node_Id := Prefix (N); |
996ae0b0 RK |
6710 | |
6711 | P_Name : Entity_Id; | |
6712 | -- Entity denoted by prefix | |
6713 | ||
6714 | P_Type : Entity_Id; | |
6715 | -- and its type | |
6716 | ||
6717 | Nam : Node_Id; | |
6718 | ||
95e00a3a ES |
6719 | function Available_Subtype return Boolean; |
6720 | -- A small optimization: if the prefix is constrained and the component | |
6721 | -- is an array type we may already have a usable subtype for it, so we | |
6722 | -- can use it rather than generating a new one, because the bounds | |
6723 | -- will be the values of the discriminants and not discriminant refs. | |
c9d70ab1 AC |
6724 | -- This simplifies value tracing in GNATProve. For consistency, both |
6725 | -- the entity name and the subtype come from the constrained component. | |
95e00a3a | 6726 | |
c0dd5b38 AC |
6727 | -- This is only used in GNATProve mode: when generating code it may be |
6728 | -- necessary to create an itype in the scope of use of the selected | |
6729 | -- component, e.g. in the context of a expanded record equality. | |
6730 | ||
2e70d415 AC |
6731 | function Is_Reference_In_Subunit return Boolean; |
6732 | -- In a subunit, the scope depth is not a proper measure of hiding, | |
6733 | -- because the context of the proper body may itself hide entities in | |
6734 | -- parent units. This rare case requires inspecting the tree directly | |
6735 | -- because the proper body is inserted in the main unit and its context | |
6736 | -- is simply added to that of the parent. | |
6737 | ||
95e00a3a ES |
6738 | ----------------------- |
6739 | -- Available_Subtype -- | |
6740 | ----------------------- | |
6741 | ||
6742 | function Available_Subtype return Boolean is | |
6743 | Comp : Entity_Id; | |
c9d70ab1 | 6744 | |
95e00a3a | 6745 | begin |
c0dd5b38 AC |
6746 | if GNATprove_Mode then |
6747 | Comp := First_Entity (Etype (P)); | |
6748 | while Present (Comp) loop | |
6749 | if Chars (Comp) = Chars (Selector_Name (N)) then | |
6750 | Set_Etype (N, Etype (Comp)); | |
6751 | Set_Entity (Selector_Name (N), Comp); | |
6752 | Set_Etype (Selector_Name (N), Etype (Comp)); | |
6753 | return True; | |
6754 | end if; | |
95e00a3a | 6755 | |
c0dd5b38 AC |
6756 | Next_Component (Comp); |
6757 | end loop; | |
6758 | end if; | |
95e00a3a ES |
6759 | |
6760 | return False; | |
6761 | end Available_Subtype; | |
6762 | ||
2e70d415 AC |
6763 | ----------------------------- |
6764 | -- Is_Reference_In_Subunit -- | |
6765 | ----------------------------- | |
6766 | ||
6767 | function Is_Reference_In_Subunit return Boolean is | |
6768 | Clause : Node_Id; | |
6769 | Comp_Unit : Node_Id; | |
6770 | ||
6771 | begin | |
6772 | Comp_Unit := N; | |
6773 | while Present (Comp_Unit) | |
f5f6d8d7 | 6774 | and then Nkind (Comp_Unit) /= N_Compilation_Unit |
2e70d415 AC |
6775 | loop |
6776 | Comp_Unit := Parent (Comp_Unit); | |
6777 | end loop; | |
6778 | ||
f5f6d8d7 | 6779 | if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then |
2e70d415 AC |
6780 | return False; |
6781 | end if; | |
6782 | ||
6783 | -- Now check whether the package is in the context of the subunit | |
6784 | ||
6785 | Clause := First (Context_Items (Comp_Unit)); | |
2e70d415 AC |
6786 | while Present (Clause) loop |
6787 | if Nkind (Clause) = N_With_Clause | |
6788 | and then Entity (Name (Clause)) = P_Name | |
6789 | then | |
6790 | return True; | |
6791 | end if; | |
6792 | ||
6793 | Clause := Next (Clause); | |
6794 | end loop; | |
6795 | ||
6796 | return False; | |
6797 | end Is_Reference_In_Subunit; | |
6798 | ||
f5f6d8d7 AC |
6799 | -- Start of processing for Find_Selected_Component |
6800 | ||
996ae0b0 RK |
6801 | begin |
6802 | Analyze (P); | |
6803 | ||
6804 | if Nkind (P) = N_Error then | |
6805 | return; | |
23685ae6 AC |
6806 | end if; |
6807 | ||
6808 | -- Selector name cannot be a character literal or an operator symbol in | |
db72f10a | 6809 | -- SPARK, except for the operator symbol in a renaming. |
23685ae6 | 6810 | |
6480338a | 6811 | if Restriction_Check_Required (SPARK_05) then |
23685ae6 | 6812 | if Nkind (Selector_Name (N)) = N_Character_Literal then |
ce5ba43a | 6813 | Check_SPARK_05_Restriction |
23685ae6 | 6814 | ("character literal cannot be prefixed", N); |
db72f10a AC |
6815 | elsif Nkind (Selector_Name (N)) = N_Operator_Symbol |
6816 | and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration | |
6817 | then | |
ce5ba43a AC |
6818 | Check_SPARK_05_Restriction |
6819 | ("operator symbol cannot be prefixed", N); | |
23685ae6 AC |
6820 | end if; |
6821 | end if; | |
996ae0b0 | 6822 | |
16ca248a ES |
6823 | -- If the selector already has an entity, the node has been constructed |
6824 | -- in the course of expansion, and is known to be valid. Do not verify | |
6825 | -- that it is defined for the type (it may be a private component used | |
6826 | -- in the expansion of record equality). | |
996ae0b0 | 6827 | |
23685ae6 | 6828 | if Present (Entity (Selector_Name (N))) then |
f5f6d8d7 | 6829 | if No (Etype (N)) or else Etype (N) = Any_Type then |
996ae0b0 | 6830 | declare |
fbf5a39b AC |
6831 | Sel_Name : constant Node_Id := Selector_Name (N); |
6832 | Selector : constant Entity_Id := Entity (Sel_Name); | |
996ae0b0 RK |
6833 | C_Etype : Node_Id; |
6834 | ||
6835 | begin | |
6836 | Set_Etype (Sel_Name, Etype (Selector)); | |
6837 | ||
6838 | if not Is_Entity_Name (P) then | |
fbf5a39b | 6839 | Resolve (P); |
996ae0b0 RK |
6840 | end if; |
6841 | ||
6842 | -- Build an actual subtype except for the first parameter | |
fbf5a39b | 6843 | -- of an init proc, where this actual subtype is by |
996ae0b0 RK |
6844 | -- definition incorrect, since the object is uninitialized |
6845 | -- (and does not even have defined discriminants etc.) | |
6846 | ||
6847 | if Is_Entity_Name (P) | |
6848 | and then Ekind (Entity (P)) = E_Function | |
6849 | then | |
6850 | Nam := New_Copy (P); | |
6851 | ||
6852 | if Is_Overloaded (P) then | |
6853 | Save_Interps (P, Nam); | |
6854 | end if; | |
6855 | ||
f5f6d8d7 | 6856 | Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam)); |
996ae0b0 RK |
6857 | Analyze_Call (P); |
6858 | Analyze_Selected_Component (N); | |
6859 | return; | |
6860 | ||
6861 | elsif Ekind (Selector) = E_Component | |
6862 | and then (not Is_Entity_Name (P) | |
6863 | or else Chars (Entity (P)) /= Name_uInit) | |
6864 | then | |
5a271a7f RD |
6865 | -- Check if we already have an available subtype we can use |
6866 | ||
e0c23ac7 | 6867 | if Ekind (Etype (P)) = E_Record_Subtype |
95e00a3a ES |
6868 | and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration |
6869 | and then Is_Array_Type (Etype (Selector)) | |
6870 | and then not Is_Packed (Etype (Selector)) | |
6871 | and then Available_Subtype | |
6872 | then | |
6873 | return; | |
6874 | ||
9fc2854d AC |
6875 | -- Do not build the subtype when referencing components of |
6876 | -- dispatch table wrappers. Required to avoid generating | |
535a8637 | 6877 | -- elaboration code with HI runtimes. |
9fc2854d | 6878 | |
535a8637 | 6879 | elsif RTU_Loaded (Ada_Tags) |
df3e68b1 HK |
6880 | and then |
6881 | ((RTE_Available (RE_Dispatch_Table_Wrapper) | |
2c1b72d7 | 6882 | and then Scope (Selector) = |
df3e68b1 | 6883 | RTE (RE_Dispatch_Table_Wrapper)) |
f5f6d8d7 AC |
6884 | or else |
6885 | (RTE_Available (RE_No_Dispatch_Table_Wrapper) | |
6886 | and then Scope (Selector) = | |
6887 | RTE (RE_No_Dispatch_Table_Wrapper))) | |
9fc2854d AC |
6888 | then |
6889 | C_Etype := Empty; | |
9fc2854d AC |
6890 | else |
6891 | C_Etype := | |
2c1b72d7 AC |
6892 | Build_Actual_Subtype_Of_Component |
6893 | (Etype (Selector), N); | |
9fc2854d AC |
6894 | end if; |
6895 | ||
996ae0b0 RK |
6896 | else |
6897 | C_Etype := Empty; | |
6898 | end if; | |
6899 | ||
6900 | if No (C_Etype) then | |
6901 | C_Etype := Etype (Selector); | |
6902 | else | |
6903 | Insert_Action (N, C_Etype); | |
6904 | C_Etype := Defining_Identifier (C_Etype); | |
6905 | end if; | |
6906 | ||
6907 | Set_Etype (N, C_Etype); | |
6908 | end; | |
6909 | ||
6910 | -- If this is the name of an entry or protected operation, and | |
6911 | -- the prefix is an access type, insert an explicit dereference, | |
6912 | -- so that entry calls are treated uniformly. | |
6913 | ||
6914 | if Is_Access_Type (Etype (P)) | |
6915 | and then Is_Concurrent_Type (Designated_Type (Etype (P))) | |
6916 | then | |
6917 | declare | |
fbf5a39b AC |
6918 | New_P : constant Node_Id := |
6919 | Make_Explicit_Dereference (Sloc (P), | |
6920 | Prefix => Relocate_Node (P)); | |
996ae0b0 RK |
6921 | begin |
6922 | Rewrite (P, New_P); | |
6923 | Set_Etype (P, Designated_Type (Etype (Prefix (P)))); | |
6924 | end; | |
6925 | end if; | |
6926 | ||
6927 | -- If the selected component appears within a default expression | |
6928 | -- and it has an actual subtype, the pre-analysis has not yet | |
6929 | -- completed its analysis, because Insert_Actions is disabled in | |
fbf5a39b | 6930 | -- that context. Within the init proc of the enclosing type we |
996ae0b0 RK |
6931 | -- must complete this analysis, if an actual subtype was created. |
6932 | ||
6933 | elsif Inside_Init_Proc then | |
6934 | declare | |
6935 | Typ : constant Entity_Id := Etype (N); | |
6936 | Decl : constant Node_Id := Declaration_Node (Typ); | |
996ae0b0 RK |
6937 | begin |
6938 | if Nkind (Decl) = N_Subtype_Declaration | |
6939 | and then not Analyzed (Decl) | |
6940 | and then Is_List_Member (Decl) | |
6941 | and then No (Parent (Decl)) | |
6942 | then | |
6943 | Remove (Decl); | |
6944 | Insert_Action (N, Decl); | |
6945 | end if; | |
6946 | end; | |
6947 | end if; | |
6948 | ||
6949 | return; | |
6950 | ||
6951 | elsif Is_Entity_Name (P) then | |
6952 | P_Name := Entity (P); | |
6953 | ||
6954 | -- The prefix may denote an enclosing type which is the completion | |
6955 | -- of an incomplete type declaration. | |
6956 | ||
6957 | if Is_Type (P_Name) then | |
6958 | Set_Entity (P, Get_Full_View (P_Name)); | |
6959 | Set_Etype (P, Entity (P)); | |
6960 | P_Name := Entity (P); | |
6961 | end if; | |
6962 | ||
6963 | P_Type := Base_Type (Etype (P)); | |
6964 | ||
6965 | if Debug_Flag_E then | |
6966 | Write_Str ("Found prefix type to be "); | |
6967 | Write_Entity_Info (P_Type, " "); Write_Eol; | |
6968 | end if; | |
6969 | ||
38cea80d ES |
6970 | -- The designated type may be a limited view with no components. |
6971 | -- Check whether the non-limited view is available, because in some | |
b943a971 AC |
6972 | -- cases this will not be set when installing the context. Rewrite |
6973 | -- the node by introducing an explicit dereference at once, and | |
6974 | -- setting the type of the rewritten prefix to the non-limited view | |
6975 | -- of the original designated type. | |
38cea80d ES |
6976 | |
6977 | if Is_Access_Type (P_Type) then | |
6978 | declare | |
b943a971 AC |
6979 | Desig_Typ : constant Entity_Id := |
6980 | Directly_Designated_Type (P_Type); | |
6981 | ||
38cea80d | 6982 | begin |
b943a971 AC |
6983 | if Is_Incomplete_Type (Desig_Typ) |
6984 | and then From_Limited_With (Desig_Typ) | |
6985 | and then Present (Non_Limited_View (Desig_Typ)) | |
38cea80d | 6986 | then |
b943a971 AC |
6987 | Rewrite (P, |
6988 | Make_Explicit_Dereference (Sloc (P), | |
6989 | Prefix => Relocate_Node (P))); | |
6990 | ||
6991 | Set_Etype (P, Get_Full_View (Non_Limited_View (Desig_Typ))); | |
6992 | P_Type := Etype (P); | |
38cea80d ES |
6993 | end if; |
6994 | end; | |
6995 | end if; | |
6996 | ||
996ae0b0 RK |
6997 | -- First check for components of a record object (not the |
6998 | -- result of a call, which is handled below). | |
6999 | ||
7000 | if Is_Appropriate_For_Record (P_Type) | |
7001 | and then not Is_Overloadable (P_Name) | |
7002 | and then not Is_Type (P_Name) | |
7003 | then | |
7004 | -- Selected component of record. Type checking will validate | |
7005 | -- name of selector. | |
dbfeb4fa RD |
7006 | |
7007 | -- ??? Could we rewrite an implicit dereference into an explicit | |
11560bcc | 7008 | -- one here? |
996ae0b0 RK |
7009 | |
7010 | Analyze_Selected_Component (N); | |
7011 | ||
f6b5dc8e AC |
7012 | -- Reference to type name in predicate/invariant expression |
7013 | ||
996ae0b0 RK |
7014 | elsif Is_Appropriate_For_Entry_Prefix (P_Type) |
7015 | and then not In_Open_Scopes (P_Name) | |
7016 | and then (not Is_Concurrent_Type (Etype (P_Name)) | |
ac7d724d | 7017 | or else not In_Open_Scopes (Etype (P_Name))) |
996ae0b0 RK |
7018 | then |
7019 | -- Call to protected operation or entry. Type checking is | |
7020 | -- needed on the prefix. | |
7021 | ||
7022 | Analyze_Selected_Component (N); | |
7023 | ||
7024 | elsif (In_Open_Scopes (P_Name) | |
f6b5dc8e AC |
7025 | and then Ekind (P_Name) /= E_Void |
7026 | and then not Is_Overloadable (P_Name)) | |
996ae0b0 | 7027 | or else (Is_Concurrent_Type (Etype (P_Name)) |
f6b5dc8e | 7028 | and then In_Open_Scopes (Etype (P_Name))) |
996ae0b0 RK |
7029 | then |
7030 | -- Prefix denotes an enclosing loop, block, or task, i.e. an | |
7031 | -- enclosing construct that is not a subprogram or accept. | |
7032 | ||
549cc9c2 AC |
7033 | -- A special case: a protected body may call an operation |
7034 | -- on an external object of the same type, in which case it | |
7035 | -- is not an expanded name. If the prefix is the type itself, | |
7036 | -- or the context is a single synchronized object it can only | |
7037 | -- be interpreted as an expanded name. | |
7038 | ||
7039 | if Is_Concurrent_Type (Etype (P_Name)) then | |
7040 | if Is_Type (P_Name) | |
7041 | or else Present (Anonymous_Object (Etype (P_Name))) | |
7042 | then | |
7043 | Find_Expanded_Name (N); | |
7044 | ||
7045 | else | |
7046 | Analyze_Selected_Component (N); | |
7047 | return; | |
7048 | end if; | |
7049 | ||
7050 | else | |
7051 | Find_Expanded_Name (N); | |
7052 | end if; | |
996ae0b0 RK |
7053 | |
7054 | elsif Ekind (P_Name) = E_Package then | |
7055 | Find_Expanded_Name (N); | |
7056 | ||
7057 | elsif Is_Overloadable (P_Name) then | |
7058 | ||
7059 | -- The subprogram may be a renaming (of an enclosing scope) as | |
7060 | -- in the case of the name of the generic within an instantiation. | |
7061 | ||
f6b5dc8e | 7062 | if Ekind_In (P_Name, E_Procedure, E_Function) |
996ae0b0 RK |
7063 | and then Present (Alias (P_Name)) |
7064 | and then Is_Generic_Instance (Alias (P_Name)) | |
7065 | then | |
7066 | P_Name := Alias (P_Name); | |
7067 | end if; | |
7068 | ||
7069 | if Is_Overloaded (P) then | |
7070 | ||
bc41faa2 | 7071 | -- The prefix must resolve to a unique enclosing construct |
996ae0b0 RK |
7072 | |
7073 | declare | |
7074 | Found : Boolean := False; | |
fbf5a39b | 7075 | Ind : Interp_Index; |
996ae0b0 RK |
7076 | It : Interp; |
7077 | ||
7078 | begin | |
fbf5a39b | 7079 | Get_First_Interp (P, Ind, It); |
996ae0b0 | 7080 | while Present (It.Nam) loop |
996ae0b0 RK |
7081 | if In_Open_Scopes (It.Nam) then |
7082 | if Found then | |
7083 | Error_Msg_N ( | |
7084 | "prefix must be unique enclosing scope", N); | |
7085 | Set_Entity (N, Any_Id); | |
7086 | Set_Etype (N, Any_Type); | |
7087 | return; | |
7088 | ||
7089 | else | |
7090 | Found := True; | |
7091 | P_Name := It.Nam; | |
7092 | end if; | |
7093 | end if; | |
7094 | ||
fbf5a39b | 7095 | Get_Next_Interp (Ind, It); |
996ae0b0 RK |
7096 | end loop; |
7097 | end; | |
7098 | end if; | |
7099 | ||
7100 | if In_Open_Scopes (P_Name) then | |
7101 | Set_Entity (P, P_Name); | |
7102 | Set_Is_Overloaded (P, False); | |
7103 | Find_Expanded_Name (N); | |
7104 | ||
7105 | else | |
7106 | -- If no interpretation as an expanded name is possible, it | |
7107 | -- must be a selected component of a record returned by a | |
16ca248a | 7108 | -- function call. Reformat prefix as a function call, the rest |
2757c5bf AC |
7109 | -- is done by type resolution. |
7110 | ||
7111 | -- Error if the prefix is procedure or entry, as is P.X | |
996ae0b0 RK |
7112 | |
7113 | if Ekind (P_Name) /= E_Function | |
ac7d724d ES |
7114 | and then |
7115 | (not Is_Overloaded (P) | |
7116 | or else Nkind (Parent (N)) = N_Procedure_Call_Statement) | |
996ae0b0 | 7117 | then |
996ae0b0 | 7118 | -- Prefix may mention a package that is hidden by a local |
290986ed GB |
7119 | -- declaration: let the user know. Scan the full homonym |
7120 | -- chain, the candidate package may be anywhere on it. | |
996ae0b0 | 7121 | |
290986ed | 7122 | if Present (Homonym (Current_Entity (P_Name))) then |
290986ed | 7123 | P_Name := Current_Entity (P_Name); |
996ae0b0 RK |
7124 | |
7125 | while Present (P_Name) loop | |
7126 | exit when Ekind (P_Name) = E_Package; | |
7127 | P_Name := Homonym (P_Name); | |
7128 | end loop; | |
7129 | ||
7130 | if Present (P_Name) then | |
2e70d415 AC |
7131 | if not Is_Reference_In_Subunit then |
7132 | Error_Msg_Sloc := Sloc (Entity (Prefix (N))); | |
2e70d415 | 7133 | Error_Msg_NE |
f5f6d8d7 | 7134 | ("package& is hidden by declaration#", N, P_Name); |
2e70d415 | 7135 | end if; |
996ae0b0 RK |
7136 | |
7137 | Set_Entity (Prefix (N), P_Name); | |
7138 | Find_Expanded_Name (N); | |
7139 | return; | |
2757c5bf | 7140 | |
996ae0b0 RK |
7141 | else |
7142 | P_Name := Entity (Prefix (N)); | |
7143 | end if; | |
7144 | end if; | |
7145 | ||
7146 | Error_Msg_NE | |
7147 | ("invalid prefix in selected component&", N, P_Name); | |
7148 | Change_Selected_Component_To_Expanded_Name (N); | |
7149 | Set_Entity (N, Any_Id); | |
7150 | Set_Etype (N, Any_Type); | |
7151 | ||
2757c5bf AC |
7152 | -- Here we have a function call, so do the reformatting |
7153 | ||
996ae0b0 RK |
7154 | else |
7155 | Nam := New_Copy (P); | |
7156 | Save_Interps (P, Nam); | |
2757c5bf AC |
7157 | |
7158 | -- We use Replace here because this is one of those cases | |
c63a2ad6 AC |
7159 | -- where the parser has missclassified the node, and we fix |
7160 | -- things up and then do the semantic analysis on the fixed | |
7161 | -- up node. Normally we do this using one of the Sinfo.CN | |
7162 | -- routines, but this is too tricky for that. | |
2757c5bf | 7163 | |
c63a2ad6 AC |
7164 | -- Note that using Rewrite would be wrong, because we would |
7165 | -- have a tree where the original node is unanalyzed, and | |
7166 | -- this violates the required interface for ASIS. | |
2757c5bf AC |
7167 | |
7168 | Replace (P, | |
996ae0b0 | 7169 | Make_Function_Call (Sloc (P), Name => Nam)); |
2757c5bf AC |
7170 | |
7171 | -- Now analyze the reformatted node | |
7172 | ||
996ae0b0 | 7173 | Analyze_Call (P); |
40bf00b1 | 7174 | |
c63a2ad6 AC |
7175 | -- If the prefix is illegal after this transformation, there |
7176 | -- may be visibility errors on the prefix. The safest is to | |
7177 | -- treat the selected component as an error. | |
40bf00b1 AC |
7178 | |
7179 | if Error_Posted (P) then | |
7180 | Set_Etype (N, Any_Type); | |
7181 | return; | |
7182 | ||
7183 | else | |
7184 | Analyze_Selected_Component (N); | |
7185 | end if; | |
996ae0b0 RK |
7186 | end if; |
7187 | end if; | |
7188 | ||
7189 | -- Remaining cases generate various error messages | |
7190 | ||
7191 | else | |
7192 | -- Format node as expanded name, to avoid cascaded errors | |
7193 | ||
c63a2ad6 AC |
7194 | -- If the limited_with transformation was applied earlier, restore |
7195 | -- source for proper error reporting. | |
b943a971 AC |
7196 | |
7197 | if not Comes_From_Source (P) | |
7198 | and then Nkind (P) = N_Explicit_Dereference | |
7199 | then | |
7200 | Rewrite (P, Prefix (P)); | |
7201 | P_Type := Etype (P); | |
7202 | end if; | |
7203 | ||
fbf5a39b | 7204 | Change_Selected_Component_To_Expanded_Name (N); |
b943a971 AC |
7205 | Set_Entity (N, Any_Id); |
7206 | Set_Etype (N, Any_Type); | |
996ae0b0 | 7207 | |
996ae0b0 RK |
7208 | -- Issue error message, but avoid this if error issued already. |
7209 | -- Use identifier of prefix if one is available. | |
7210 | ||
bcb0389e | 7211 | if P_Name = Any_Id then |
996ae0b0 RK |
7212 | null; |
7213 | ||
596b25f9 AC |
7214 | -- It is not an error if the prefix is the current instance of |
7215 | -- type name, e.g. the expression of a type aspect, when it is | |
7216 | -- analyzed for ASIS use. | |
7217 | ||
7218 | elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then | |
7219 | null; | |
7220 | ||
996ae0b0 RK |
7221 | elsif Ekind (P_Name) = E_Void then |
7222 | Premature_Usage (P); | |
7223 | ||
7224 | elsif Nkind (P) /= N_Attribute_Reference then | |
67c0e662 RD |
7225 | |
7226 | -- This may have been meant as a prefixed call to a primitive | |
96df3ff4 AC |
7227 | -- of an untagged type. If it is a function call check type of |
7228 | -- its first formal and add explanation. | |
67c0e662 RD |
7229 | |
7230 | declare | |
7231 | F : constant Entity_Id := | |
7232 | Current_Entity (Selector_Name (N)); | |
7233 | begin | |
7234 | if Present (F) | |
7235 | and then Is_Overloadable (F) | |
7236 | and then Present (First_Entity (F)) | |
96df3ff4 | 7237 | and then not Is_Tagged_Type (Etype (First_Entity (F))) |
67c0e662 RD |
7238 | then |
7239 | Error_Msg_N | |
c308e762 HK |
7240 | ("prefixed call is only allowed for objects of a " |
7241 | & "tagged type", N); | |
67c0e662 RD |
7242 | end if; |
7243 | end; | |
7244 | ||
7245 | Error_Msg_N ("invalid prefix in selected component&", P); | |
996ae0b0 | 7246 | |
9596236a AC |
7247 | if Is_Access_Type (P_Type) |
7248 | and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type | |
7249 | then | |
65356e64 | 7250 | Error_Msg_N |
67c0e662 RD |
7251 | ("\dereference must not be of an incomplete type " |
7252 | & "(RM 3.10.1)", P); | |
9596236a AC |
7253 | end if; |
7254 | ||
996ae0b0 | 7255 | else |
67c0e662 | 7256 | Error_Msg_N ("invalid prefix in selected component", P); |
996ae0b0 RK |
7257 | end if; |
7258 | end if; | |
7259 | ||
db72f10a AC |
7260 | -- Selector name is restricted in SPARK |
7261 | ||
7262 | if Nkind (N) = N_Expanded_Name | |
6480338a | 7263 | and then Restriction_Check_Required (SPARK_05) |
db72f10a AC |
7264 | then |
7265 | if Is_Subprogram (P_Name) then | |
ce5ba43a | 7266 | Check_SPARK_05_Restriction |
db72f10a AC |
7267 | ("prefix of expanded name cannot be a subprogram", P); |
7268 | elsif Ekind (P_Name) = E_Loop then | |
ce5ba43a | 7269 | Check_SPARK_05_Restriction |
db72f10a AC |
7270 | ("prefix of expanded name cannot be a loop statement", P); |
7271 | end if; | |
7272 | end if; | |
7273 | ||
996ae0b0 RK |
7274 | else |
7275 | -- If prefix is not the name of an entity, it must be an expression, | |
7276 | -- whose type is appropriate for a record. This is determined by | |
7277 | -- type resolution. | |
7278 | ||
7279 | Analyze_Selected_Component (N); | |
7280 | end if; | |
0929eaeb AC |
7281 | |
7282 | Analyze_Dimension (N); | |
996ae0b0 RK |
7283 | end Find_Selected_Component; |
7284 | ||
7285 | --------------- | |
7286 | -- Find_Type -- | |
7287 | --------------- | |
7288 | ||
7289 | procedure Find_Type (N : Node_Id) is | |
7290 | C : Entity_Id; | |
7291 | Typ : Entity_Id; | |
7292 | T : Entity_Id; | |
7293 | T_Name : Entity_Id; | |
7294 | ||
7295 | begin | |
7296 | if N = Error then | |
7297 | return; | |
7298 | ||
7299 | elsif Nkind (N) = N_Attribute_Reference then | |
7300 | ||
11560bcc TQ |
7301 | -- Class attribute. This is not valid in Ada 83 mode, but we do not |
7302 | -- need to enforce that at this point, since the declaration of the | |
7303 | -- tagged type in the prefix would have been flagged already. | |
996ae0b0 RK |
7304 | |
7305 | if Attribute_Name (N) = Name_Class then | |
7306 | Check_Restriction (No_Dispatch, N); | |
7307 | Find_Type (Prefix (N)); | |
7308 | ||
7309 | -- Propagate error from bad prefix | |
7310 | ||
7311 | if Etype (Prefix (N)) = Any_Type then | |
7312 | Set_Entity (N, Any_Type); | |
7313 | Set_Etype (N, Any_Type); | |
7314 | return; | |
7315 | end if; | |
7316 | ||
7317 | T := Base_Type (Entity (Prefix (N))); | |
7318 | ||
11560bcc TQ |
7319 | -- Case where type is not known to be tagged. Its appearance in |
7320 | -- the prefix of the 'Class attribute indicates that the full view | |
16ca248a | 7321 | -- will be tagged. |
996ae0b0 RK |
7322 | |
7323 | if not Is_Tagged_Type (T) then | |
7324 | if Ekind (T) = E_Incomplete_Type then | |
7325 | ||
7326 | -- It is legal to denote the class type of an incomplete | |
7327 | -- type. The full type will have to be tagged, of course. | |
90067a15 | 7328 | -- In Ada 2005 this usage is declared obsolescent, so we |
ae247488 ES |
7329 | -- warn accordingly. This usage is only legal if the type |
7330 | -- is completed in the current scope, and not for a limited | |
7331 | -- view of a type. | |
7332 | ||
2f7b7467 AC |
7333 | if Ada_Version >= Ada_2005 then |
7334 | ||
7335 | -- Test whether the Available_View of a limited type view | |
7336 | -- is tagged, since the limited view may not be marked as | |
7337 | -- tagged if the type itself has an untagged incomplete | |
7338 | -- type view in its package. | |
7339 | ||
7b56a91b | 7340 | if From_Limited_With (T) |
2f7b7467 AC |
7341 | and then not Is_Tagged_Type (Available_View (T)) |
7342 | then | |
ae247488 ES |
7343 | Error_Msg_N |
7344 | ("prefix of Class attribute must be tagged", N); | |
7345 | Set_Etype (N, Any_Type); | |
7346 | Set_Entity (N, Any_Type); | |
7347 | return; | |
11560bcc | 7348 | |
dbfeb4fa RD |
7349 | -- ??? This test is temporarily disabled (always |
7350 | -- False) because it causes an unwanted warning on | |
7351 | -- GNAT sources (built with -gnatg, which includes | |
7352 | -- Warn_On_Obsolescent_ Feature). Once this issue | |
7353 | -- is cleared in the sources, it can be enabled. | |
11560bcc | 7354 | |
ac7d724d | 7355 | elsif Warn_On_Obsolescent_Feature and then False then |
ae247488 ES |
7356 | Error_Msg_N |
7357 | ("applying 'Class to an untagged incomplete type" | |
dbfeb4fa | 7358 | & " is an obsolescent feature (RM J.11)?r?", N); |
ae247488 | 7359 | end if; |
11560bcc | 7360 | end if; |
996ae0b0 RK |
7361 | |
7362 | Set_Is_Tagged_Type (T); | |
ef2a63ba | 7363 | Set_Direct_Primitive_Operations (T, New_Elmt_List); |
996ae0b0 RK |
7364 | Make_Class_Wide_Type (T); |
7365 | Set_Entity (N, Class_Wide_Type (T)); | |
7366 | Set_Etype (N, Class_Wide_Type (T)); | |
7367 | ||
7368 | elsif Ekind (T) = E_Private_Type | |
7369 | and then not Is_Generic_Type (T) | |
7370 | and then In_Private_Part (Scope (T)) | |
7371 | then | |
16ca248a ES |
7372 | -- The Class attribute can be applied to an untagged private |
7373 | -- type fulfilled by a tagged type prior to the full type | |
7374 | -- declaration (but only within the parent package's private | |
7375 | -- part). Create the class-wide type now and check that the | |
7376 | -- full type is tagged later during its analysis. Note that | |
7377 | -- we do not mark the private type as tagged, unlike the | |
7378 | -- case of incomplete types, because the type must still | |
996ae0b0 RK |
7379 | -- appear untagged to outside units. |
7380 | ||
cdc8c54c | 7381 | if No (Class_Wide_Type (T)) then |
996ae0b0 RK |
7382 | Make_Class_Wide_Type (T); |
7383 | end if; | |
7384 | ||
7385 | Set_Entity (N, Class_Wide_Type (T)); | |
7386 | Set_Etype (N, Class_Wide_Type (T)); | |
7387 | ||
7388 | else | |
16ca248a ES |
7389 | -- Should we introduce a type Any_Tagged and use Wrong_Type |
7390 | -- here, it would be a bit more consistent??? | |
996ae0b0 RK |
7391 | |
7392 | Error_Msg_NE | |
7393 | ("tagged type required, found}", | |
7394 | Prefix (N), First_Subtype (T)); | |
7395 | Set_Entity (N, Any_Type); | |
7396 | return; | |
7397 | end if; | |
7398 | ||
7399 | -- Case of tagged type | |
7400 | ||
7401 | else | |
39edfb45 | 7402 | if Is_Concurrent_Type (T) then |
cdc8c54c BD |
7403 | if No (Corresponding_Record_Type (Entity (Prefix (N)))) then |
7404 | ||
f2a54683 AC |
7405 | -- Previous error. Create a class-wide type for the |
7406 | -- synchronized type itself, with minimal semantic | |
7407 | -- attributes, to catch other errors in some ACATS tests. | |
cdc8c54c | 7408 | |
c48e0f27 | 7409 | pragma Assert (Serious_Errors_Detected /= 0); |
f2a54683 AC |
7410 | Make_Class_Wide_Type (T); |
7411 | C := Class_Wide_Type (T); | |
7412 | Set_First_Entity (C, First_Entity (T)); | |
cdc8c54c BD |
7413 | |
7414 | else | |
7415 | C := Class_Wide_Type | |
7416 | (Corresponding_Record_Type (Entity (Prefix (N)))); | |
7417 | end if; | |
7418 | ||
39edfb45 JM |
7419 | else |
7420 | C := Class_Wide_Type (Entity (Prefix (N))); | |
7421 | end if; | |
7422 | ||
e7ba564f | 7423 | Set_Entity_With_Checks (N, C); |
996ae0b0 RK |
7424 | Generate_Reference (C, N); |
7425 | Set_Etype (N, C); | |
996ae0b0 RK |
7426 | end if; |
7427 | ||
0ab80019 | 7428 | -- Base attribute, not allowed in Ada 83 |
996ae0b0 RK |
7429 | |
7430 | elsif Attribute_Name (N) = Name_Base then | |
7a489a2b | 7431 | Error_Msg_Name_1 := Name_Base; |
ce5ba43a | 7432 | Check_SPARK_05_Restriction |
7a489a2b AC |
7433 | ("attribute% is only allowed as prefix of another attribute", N); |
7434 | ||
0ab80019 | 7435 | if Ada_Version = Ada_83 and then Comes_From_Source (N) then |
996ae0b0 RK |
7436 | Error_Msg_N |
7437 | ("(Ada 83) Base attribute not allowed in subtype mark", N); | |
7438 | ||
7439 | else | |
7440 | Find_Type (Prefix (N)); | |
7441 | Typ := Entity (Prefix (N)); | |
7442 | ||
0ab80019 | 7443 | if Ada_Version >= Ada_95 |
fbf5a39b AC |
7444 | and then not Is_Scalar_Type (Typ) |
7445 | and then not Is_Generic_Type (Typ) | |
7446 | then | |
7447 | Error_Msg_N | |
bc41faa2 AC |
7448 | ("prefix of Base attribute must be scalar type", |
7449 | Prefix (N)); | |
fbf5a39b | 7450 | |
a8930b80 | 7451 | elsif Warn_On_Redundant_Constructs |
996ae0b0 | 7452 | and then Base_Type (Typ) = Typ |
996ae0b0 | 7453 | then |
ed2233dc | 7454 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 7455 | ("redundant attribute, & is its own base type?r?", N, Typ); |
996ae0b0 RK |
7456 | end if; |
7457 | ||
7458 | T := Base_Type (Typ); | |
996ae0b0 RK |
7459 | |
7460 | -- Rewrite attribute reference with type itself (see similar | |
a8930b80 AC |
7461 | -- processing in Analyze_Attribute, case Base). Preserve prefix |
7462 | -- if present, for other legality checks. | |
fbf5a39b AC |
7463 | |
7464 | if Nkind (Prefix (N)) = N_Expanded_Name then | |
7465 | Rewrite (N, | |
7466 | Make_Expanded_Name (Sloc (N), | |
11560bcc TQ |
7467 | Chars => Chars (T), |
7468 | Prefix => New_Copy (Prefix (Prefix (N))), | |
e4494292 | 7469 | Selector_Name => New_Occurrence_Of (T, Sloc (N)))); |
996ae0b0 | 7470 | |
fbf5a39b | 7471 | else |
e4494292 | 7472 | Rewrite (N, New_Occurrence_Of (T, Sloc (N))); |
fbf5a39b AC |
7473 | end if; |
7474 | ||
7475 | Set_Entity (N, T); | |
996ae0b0 RK |
7476 | Set_Etype (N, T); |
7477 | end if; | |
7478 | ||
923fa078 RD |
7479 | elsif Attribute_Name (N) = Name_Stub_Type then |
7480 | ||
7481 | -- This is handled in Analyze_Attribute | |
7482 | ||
7483 | Analyze (N); | |
7484 | ||
996ae0b0 RK |
7485 | -- All other attributes are invalid in a subtype mark |
7486 | ||
7487 | else | |
7488 | Error_Msg_N ("invalid attribute in subtype mark", N); | |
7489 | end if; | |
7490 | ||
7491 | else | |
7492 | Analyze (N); | |
7493 | ||
7494 | if Is_Entity_Name (N) then | |
7495 | T_Name := Entity (N); | |
7496 | else | |
7497 | Error_Msg_N ("subtype mark required in this context", N); | |
7498 | Set_Etype (N, Any_Type); | |
7499 | return; | |
7500 | end if; | |
7501 | ||
7502 | if T_Name = Any_Id or else Etype (N) = Any_Type then | |
7503 | ||
7504 | -- Undefined id. Make it into a valid type | |
7505 | ||
7506 | Set_Entity (N, Any_Type); | |
7507 | ||
7508 | elsif not Is_Type (T_Name) | |
7509 | and then T_Name /= Standard_Void_Type | |
7510 | then | |
7511 | Error_Msg_Sloc := Sloc (T_Name); | |
7512 | Error_Msg_N ("subtype mark required in this context", N); | |
923fa078 | 7513 | Error_Msg_NE ("\\found & declared#", N, T_Name); |
996ae0b0 RK |
7514 | Set_Entity (N, Any_Type); |
7515 | ||
7516 | else | |
11560bcc TQ |
7517 | -- If the type is an incomplete type created to handle |
7518 | -- anonymous access components of a record type, then the | |
7519 | -- incomplete type is the visible entity and subsequent | |
7520 | -- references will point to it. Mark the original full | |
7521 | -- type as referenced, to prevent spurious warnings. | |
7522 | ||
7523 | if Is_Incomplete_Type (T_Name) | |
7524 | and then Present (Full_View (T_Name)) | |
7525 | and then not Comes_From_Source (T_Name) | |
7526 | then | |
7527 | Set_Referenced (Full_View (T_Name)); | |
7528 | end if; | |
7529 | ||
996ae0b0 RK |
7530 | T_Name := Get_Full_View (T_Name); |
7531 | ||
11560bcc TQ |
7532 | -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through |
7533 | -- limited-with clauses | |
7534 | ||
7b56a91b | 7535 | if From_Limited_With (T_Name) |
11560bcc TQ |
7536 | and then Ekind (T_Name) in Incomplete_Kind |
7537 | and then Present (Non_Limited_View (T_Name)) | |
7538 | and then Is_Interface (Non_Limited_View (T_Name)) | |
7539 | then | |
7540 | T_Name := Non_Limited_View (T_Name); | |
7541 | end if; | |
7542 | ||
996ae0b0 RK |
7543 | if In_Open_Scopes (T_Name) then |
7544 | if Ekind (Base_Type (T_Name)) = E_Task_Type then | |
923fa078 RD |
7545 | |
7546 | -- In Ada 2005, a task name can be used in an access | |
e264efcc AC |
7547 | -- definition within its own body. It cannot be used |
7548 | -- in the discriminant part of the task declaration, | |
7549 | -- nor anywhere else in the declaration because entries | |
7550 | -- cannot have access parameters. | |
923fa078 | 7551 | |
0791fbe9 | 7552 | if Ada_Version >= Ada_2005 |
923fa078 RD |
7553 | and then Nkind (Parent (N)) = N_Access_Definition |
7554 | then | |
7555 | Set_Entity (N, T_Name); | |
7556 | Set_Etype (N, T_Name); | |
e264efcc AC |
7557 | |
7558 | if Has_Completion (T_Name) then | |
7559 | return; | |
7560 | ||
7561 | else | |
7562 | Error_Msg_N | |
7563 | ("task type cannot be used as type mark " & | |
7564 | "within its own declaration", N); | |
7565 | end if; | |
923fa078 RD |
7566 | |
7567 | else | |
7568 | Error_Msg_N | |
7569 | ("task type cannot be used as type mark " & | |
fbe627af | 7570 | "within its own spec or body", N); |
923fa078 | 7571 | end if; |
fbe627af RD |
7572 | |
7573 | elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then | |
7574 | ||
7575 | -- In Ada 2005, a protected name can be used in an access | |
7576 | -- definition within its own body. | |
7577 | ||
0791fbe9 | 7578 | if Ada_Version >= Ada_2005 |
fbe627af RD |
7579 | and then Nkind (Parent (N)) = N_Access_Definition |
7580 | then | |
7581 | Set_Entity (N, T_Name); | |
7582 | Set_Etype (N, T_Name); | |
7583 | return; | |
7584 | ||
7585 | else | |
7586 | Error_Msg_N | |
7587 | ("protected type cannot be used as type mark " & | |
7588 | "within its own spec or body", N); | |
7589 | end if; | |
7590 | ||
996ae0b0 RK |
7591 | else |
7592 | Error_Msg_N ("type declaration cannot refer to itself", N); | |
7593 | end if; | |
7594 | ||
7595 | Set_Etype (N, Any_Type); | |
7596 | Set_Entity (N, Any_Type); | |
7597 | Set_Error_Posted (T_Name); | |
7598 | return; | |
7599 | end if; | |
7600 | ||
7601 | Set_Entity (N, T_Name); | |
7602 | Set_Etype (N, T_Name); | |
7603 | end if; | |
7604 | end if; | |
7605 | ||
07fc65c4 | 7606 | if Present (Etype (N)) and then Comes_From_Source (N) then |
996ae0b0 RK |
7607 | if Is_Fixed_Point_Type (Etype (N)) then |
7608 | Check_Restriction (No_Fixed_Point, N); | |
7609 | elsif Is_Floating_Point_Type (Etype (N)) then | |
7610 | Check_Restriction (No_Floating_Point, N); | |
7611 | end if; | |
8636f52f HK |
7612 | |
7613 | -- A Ghost type must appear in a specific context | |
7614 | ||
7615 | if Is_Ghost_Entity (Etype (N)) then | |
7616 | Check_Ghost_Context (Etype (N), N); | |
7617 | end if; | |
996ae0b0 RK |
7618 | end if; |
7619 | end Find_Type; | |
7620 | ||
996ae0b0 RK |
7621 | ------------------------------------ |
7622 | -- Has_Implicit_Character_Literal -- | |
7623 | ------------------------------------ | |
7624 | ||
7625 | function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is | |
7626 | Id : Entity_Id; | |
7627 | Found : Boolean := False; | |
7628 | P : constant Entity_Id := Entity (Prefix (N)); | |
7629 | Priv_Id : Entity_Id := Empty; | |
7630 | ||
7631 | begin | |
ac7d724d | 7632 | if Ekind (P) = E_Package and then not In_Open_Scopes (P) then |
996ae0b0 RK |
7633 | Priv_Id := First_Private_Entity (P); |
7634 | end if; | |
7635 | ||
7636 | if P = Standard_Standard then | |
7637 | Change_Selected_Component_To_Expanded_Name (N); | |
7638 | Rewrite (N, Selector_Name (N)); | |
7639 | Analyze (N); | |
7640 | Set_Etype (Original_Node (N), Standard_Character); | |
7641 | return True; | |
7642 | end if; | |
7643 | ||
7644 | Id := First_Entity (P); | |
ac7d724d | 7645 | while Present (Id) and then Id /= Priv_Id loop |
d347f572 AC |
7646 | if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then |
7647 | ||
996ae0b0 RK |
7648 | -- We replace the node with the literal itself, resolve as a |
7649 | -- character, and set the type correctly. | |
7650 | ||
7651 | if not Found then | |
7652 | Change_Selected_Component_To_Expanded_Name (N); | |
7653 | Rewrite (N, Selector_Name (N)); | |
7654 | Analyze (N); | |
7655 | Set_Etype (N, Id); | |
7656 | Set_Etype (Original_Node (N), Id); | |
7657 | Found := True; | |
7658 | ||
7659 | else | |
7660 | -- More than one type derived from Character in given scope. | |
7661 | -- Collect all possible interpretations. | |
7662 | ||
7663 | Add_One_Interp (N, Id, Id); | |
7664 | end if; | |
7665 | end if; | |
7666 | ||
7667 | Next_Entity (Id); | |
7668 | end loop; | |
7669 | ||
7670 | return Found; | |
7671 | end Has_Implicit_Character_Literal; | |
7672 | ||
9bc856dd AC |
7673 | ---------------------- |
7674 | -- Has_Private_With -- | |
7675 | ---------------------- | |
7676 | ||
7677 | function Has_Private_With (E : Entity_Id) return Boolean is | |
7678 | Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit); | |
7679 | Item : Node_Id; | |
7680 | ||
7681 | begin | |
7682 | Item := First (Context_Items (Comp_Unit)); | |
7683 | while Present (Item) loop | |
7684 | if Nkind (Item) = N_With_Clause | |
7685 | and then Private_Present (Item) | |
7686 | and then Entity (Name (Item)) = E | |
7687 | then | |
7688 | return True; | |
7689 | end if; | |
7690 | ||
7691 | Next (Item); | |
7692 | end loop; | |
7693 | ||
7694 | return False; | |
7695 | end Has_Private_With; | |
7696 | ||
996ae0b0 RK |
7697 | --------------------------- |
7698 | -- Has_Implicit_Operator -- | |
7699 | --------------------------- | |
7700 | ||
7701 | function Has_Implicit_Operator (N : Node_Id) return Boolean is | |
7702 | Op_Id : constant Name_Id := Chars (Selector_Name (N)); | |
7703 | P : constant Entity_Id := Entity (Prefix (N)); | |
7704 | Id : Entity_Id; | |
7705 | Priv_Id : Entity_Id := Empty; | |
7706 | ||
fbf5a39b AC |
7707 | procedure Add_Implicit_Operator |
7708 | (T : Entity_Id; | |
7709 | Op_Type : Entity_Id := Empty); | |
fbe627af RD |
7710 | -- Add implicit interpretation to node N, using the type for which a |
7711 | -- predefined operator exists. If the operator yields a boolean type, | |
7712 | -- the Operand_Type is implicitly referenced by the operator, and a | |
7713 | -- reference to it must be generated. | |
996ae0b0 RK |
7714 | |
7715 | --------------------------- | |
7716 | -- Add_Implicit_Operator -- | |
7717 | --------------------------- | |
7718 | ||
fbf5a39b AC |
7719 | procedure Add_Implicit_Operator |
7720 | (T : Entity_Id; | |
7721 | Op_Type : Entity_Id := Empty) | |
7722 | is | |
996ae0b0 RK |
7723 | Predef_Op : Entity_Id; |
7724 | ||
7725 | begin | |
7726 | Predef_Op := Current_Entity (Selector_Name (N)); | |
996ae0b0 RK |
7727 | while Present (Predef_Op) |
7728 | and then Scope (Predef_Op) /= Standard_Standard | |
7729 | loop | |
7730 | Predef_Op := Homonym (Predef_Op); | |
7731 | end loop; | |
7732 | ||
7733 | if Nkind (N) = N_Selected_Component then | |
7734 | Change_Selected_Component_To_Expanded_Name (N); | |
7735 | end if; | |
7736 | ||
719aaf4d AC |
7737 | -- If the context is an unanalyzed function call, determine whether |
7738 | -- a binary or unary interpretation is required. | |
996ae0b0 | 7739 | |
719aaf4d AC |
7740 | if Nkind (Parent (N)) = N_Indexed_Component then |
7741 | declare | |
964f13da RD |
7742 | Is_Binary_Call : constant Boolean := |
7743 | Present | |
7744 | (Next (First (Expressions (Parent (N))))); | |
7745 | Is_Binary_Op : constant Boolean := | |
7746 | First_Entity | |
7747 | (Predef_Op) /= Last_Entity (Predef_Op); | |
719aaf4d AC |
7748 | Predef_Op2 : constant Entity_Id := Homonym (Predef_Op); |
7749 | ||
7750 | begin | |
7751 | if Is_Binary_Call then | |
7752 | if Is_Binary_Op then | |
7753 | Add_One_Interp (N, Predef_Op, T); | |
7754 | else | |
7755 | Add_One_Interp (N, Predef_Op2, T); | |
7756 | end if; | |
996ae0b0 | 7757 | |
719aaf4d AC |
7758 | else |
7759 | if not Is_Binary_Op then | |
7760 | Add_One_Interp (N, Predef_Op, T); | |
7761 | else | |
7762 | Add_One_Interp (N, Predef_Op2, T); | |
7763 | end if; | |
7764 | end if; | |
7765 | end; | |
7766 | ||
7767 | else | |
7768 | Add_One_Interp (N, Predef_Op, T); | |
7769 | ||
7770 | -- For operators with unary and binary interpretations, if | |
7771 | -- context is not a call, add both | |
7772 | ||
7773 | if Present (Homonym (Predef_Op)) then | |
7774 | Add_One_Interp (N, Homonym (Predef_Op), T); | |
7775 | end if; | |
996ae0b0 | 7776 | end if; |
fbf5a39b AC |
7777 | |
7778 | -- The node is a reference to a predefined operator, and | |
7779 | -- an implicit reference to the type of its operands. | |
7780 | ||
7781 | if Present (Op_Type) then | |
7782 | Generate_Operator_Reference (N, Op_Type); | |
7783 | else | |
7784 | Generate_Operator_Reference (N, T); | |
7785 | end if; | |
996ae0b0 RK |
7786 | end Add_Implicit_Operator; |
7787 | ||
7788 | -- Start of processing for Has_Implicit_Operator | |
7789 | ||
7790 | begin | |
ac7d724d | 7791 | if Ekind (P) = E_Package and then not In_Open_Scopes (P) then |
996ae0b0 RK |
7792 | Priv_Id := First_Private_Entity (P); |
7793 | end if; | |
7794 | ||
7795 | Id := First_Entity (P); | |
7796 | ||
7797 | case Op_Id is | |
7798 | ||
7799 | -- Boolean operators: an implicit declaration exists if the scope | |
7800 | -- contains a declaration for a derived Boolean type, or for an | |
7801 | -- array of Boolean type. | |
7802 | ||
d8f43ee6 HK |
7803 | when Name_Op_And |
7804 | | Name_Op_Not | |
7805 | | Name_Op_Or | |
7806 | | Name_Op_Xor | |
7807 | => | |
fd22e260 | 7808 | while Id /= Priv_Id loop |
d347f572 | 7809 | if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then |
996ae0b0 RK |
7810 | Add_Implicit_Operator (Id); |
7811 | return True; | |
7812 | end if; | |
7813 | ||
7814 | Next_Entity (Id); | |
7815 | end loop; | |
7816 | ||
bc41faa2 | 7817 | -- Equality: look for any non-limited type (result is Boolean) |
996ae0b0 | 7818 | |
d8f43ee6 HK |
7819 | when Name_Op_Eq |
7820 | | Name_Op_Ne | |
7821 | => | |
fd22e260 | 7822 | while Id /= Priv_Id loop |
996ae0b0 RK |
7823 | if Is_Type (Id) |
7824 | and then not Is_Limited_Type (Id) | |
d347f572 | 7825 | and then Is_Base_Type (Id) |
996ae0b0 | 7826 | then |
fbf5a39b | 7827 | Add_Implicit_Operator (Standard_Boolean, Id); |
996ae0b0 RK |
7828 | return True; |
7829 | end if; | |
7830 | ||
7831 | Next_Entity (Id); | |
7832 | end loop; | |
7833 | ||
bc41faa2 | 7834 | -- Comparison operators: scalar type, or array of scalar |
996ae0b0 | 7835 | |
d8f43ee6 HK |
7836 | when Name_Op_Ge |
7837 | | Name_Op_Gt | |
7838 | | Name_Op_Le | |
7839 | | Name_Op_Lt | |
7840 | => | |
fd22e260 | 7841 | while Id /= Priv_Id loop |
996ae0b0 | 7842 | if (Is_Scalar_Type (Id) |
d347f572 AC |
7843 | or else (Is_Array_Type (Id) |
7844 | and then Is_Scalar_Type (Component_Type (Id)))) | |
7845 | and then Is_Base_Type (Id) | |
996ae0b0 | 7846 | then |
fbf5a39b | 7847 | Add_Implicit_Operator (Standard_Boolean, Id); |
996ae0b0 RK |
7848 | return True; |
7849 | end if; | |
7850 | ||
7851 | Next_Entity (Id); | |
7852 | end loop; | |
7853 | ||
7854 | -- Arithmetic operators: any numeric type | |
7855 | ||
d8f43ee6 HK |
7856 | when Name_Op_Abs |
7857 | | Name_Op_Add | |
7858 | | Name_Op_Divide | |
7859 | | Name_Op_Expon | |
7860 | | Name_Op_Mod | |
7861 | | Name_Op_Multiply | |
7862 | | Name_Op_Rem | |
7863 | | Name_Op_Subtract | |
7864 | => | |
fd22e260 | 7865 | while Id /= Priv_Id loop |
d347f572 | 7866 | if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then |
996ae0b0 RK |
7867 | Add_Implicit_Operator (Id); |
7868 | return True; | |
7869 | end if; | |
7870 | ||
7871 | Next_Entity (Id); | |
7872 | end loop; | |
7873 | ||
7874 | -- Concatenation: any one-dimensional array type | |
7875 | ||
7876 | when Name_Op_Concat => | |
fd22e260 | 7877 | while Id /= Priv_Id loop |
d347f572 AC |
7878 | if Is_Array_Type (Id) |
7879 | and then Number_Dimensions (Id) = 1 | |
7880 | and then Is_Base_Type (Id) | |
996ae0b0 RK |
7881 | then |
7882 | Add_Implicit_Operator (Id); | |
7883 | return True; | |
7884 | end if; | |
7885 | ||
7886 | Next_Entity (Id); | |
7887 | end loop; | |
7888 | ||
7889 | -- What is the others condition here? Should we be using a | |
7890 | -- subtype of Name_Id that would restrict to operators ??? | |
7891 | ||
d8f43ee6 HK |
7892 | when others => |
7893 | null; | |
996ae0b0 RK |
7894 | end case; |
7895 | ||
7896 | -- If we fall through, then we do not have an implicit operator | |
7897 | ||
7898 | return False; | |
996ae0b0 RK |
7899 | end Has_Implicit_Operator; |
7900 | ||
607d0635 AC |
7901 | ----------------------------------- |
7902 | -- Has_Loop_In_Inner_Open_Scopes -- | |
7903 | ----------------------------------- | |
7904 | ||
7905 | function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is | |
7906 | begin | |
7907 | -- Several scope stacks are maintained by Scope_Stack. The base of the | |
7908 | -- currently active scope stack is denoted by the Is_Active_Stack_Base | |
7909 | -- flag in the scope stack entry. Note that the scope stacks used to | |
7910 | -- simply be delimited implicitly by the presence of Standard_Standard | |
7911 | -- at their base, but there now are cases where this is not sufficient | |
7912 | -- because Standard_Standard actually may appear in the middle of the | |
7913 | -- active set of scopes. | |
7914 | ||
7915 | for J in reverse 0 .. Scope_Stack.Last loop | |
8d606a78 RD |
7916 | |
7917 | -- S was reached without seing a loop scope first | |
7918 | ||
607d0635 | 7919 | if Scope_Stack.Table (J).Entity = S then |
607d0635 | 7920 | return False; |
8d606a78 RD |
7921 | |
7922 | -- S was not yet reached, so it contains at least one inner loop | |
7923 | ||
607d0635 | 7924 | elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then |
607d0635 AC |
7925 | return True; |
7926 | end if; | |
7927 | ||
7928 | -- Check Is_Active_Stack_Base to tell us when to stop, as there are | |
7929 | -- cases where Standard_Standard appears in the middle of the active | |
7930 | -- set of scopes. This affects the declaration and overriding of | |
7931 | -- private inherited operations in instantiations of generic child | |
7932 | -- units. | |
7933 | ||
7934 | pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base); | |
7935 | end loop; | |
7936 | ||
2010d078 | 7937 | raise Program_Error; -- unreachable |
607d0635 AC |
7938 | end Has_Loop_In_Inner_Open_Scopes; |
7939 | ||
996ae0b0 RK |
7940 | -------------------- |
7941 | -- In_Open_Scopes -- | |
7942 | -------------------- | |
7943 | ||
7944 | function In_Open_Scopes (S : Entity_Id) return Boolean is | |
7945 | begin | |
923fa078 RD |
7946 | -- Several scope stacks are maintained by Scope_Stack. The base of the |
7947 | -- currently active scope stack is denoted by the Is_Active_Stack_Base | |
7948 | -- flag in the scope stack entry. Note that the scope stacks used to | |
7949 | -- simply be delimited implicitly by the presence of Standard_Standard | |
7950 | -- at their base, but there now are cases where this is not sufficient | |
7951 | -- because Standard_Standard actually may appear in the middle of the | |
7952 | -- active set of scopes. | |
996ae0b0 RK |
7953 | |
7954 | for J in reverse 0 .. Scope_Stack.Last loop | |
7955 | if Scope_Stack.Table (J).Entity = S then | |
7956 | return True; | |
7957 | end if; | |
7958 | ||
923fa078 RD |
7959 | -- Check Is_Active_Stack_Base to tell us when to stop, as there are |
7960 | -- cases where Standard_Standard appears in the middle of the active | |
7961 | -- set of scopes. This affects the declaration and overriding of | |
7962 | -- private inherited operations in instantiations of generic child | |
7963 | -- units. | |
996ae0b0 RK |
7964 | |
7965 | exit when Scope_Stack.Table (J).Is_Active_Stack_Base; | |
7966 | end loop; | |
7967 | ||
7968 | return False; | |
7969 | end In_Open_Scopes; | |
7970 | ||
7971 | ----------------------------- | |
7972 | -- Inherit_Renamed_Profile -- | |
7973 | ----------------------------- | |
7974 | ||
7975 | procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is | |
7976 | New_F : Entity_Id; | |
7977 | Old_F : Entity_Id; | |
7978 | Old_T : Entity_Id; | |
7979 | New_T : Entity_Id; | |
7980 | ||
7981 | begin | |
7982 | if Ekind (Old_S) = E_Operator then | |
996ae0b0 RK |
7983 | New_F := First_Formal (New_S); |
7984 | ||
7985 | while Present (New_F) loop | |
7986 | Set_Etype (New_F, Base_Type (Etype (New_F))); | |
7987 | Next_Formal (New_F); | |
7988 | end loop; | |
7989 | ||
7990 | Set_Etype (New_S, Base_Type (Etype (New_S))); | |
7991 | ||
7992 | else | |
7993 | New_F := First_Formal (New_S); | |
7994 | Old_F := First_Formal (Old_S); | |
7995 | ||
7996 | while Present (New_F) loop | |
7997 | New_T := Etype (New_F); | |
7998 | Old_T := Etype (Old_F); | |
7999 | ||
ed3fe8cc AC |
8000 | -- If the new type is a renaming of the old one, as is the case |
8001 | -- for actuals in instances, retain its name, to simplify later | |
8002 | -- disambiguation. | |
996ae0b0 RK |
8003 | |
8004 | if Nkind (Parent (New_T)) = N_Subtype_Declaration | |
8005 | and then Is_Entity_Name (Subtype_Indication (Parent (New_T))) | |
8006 | and then Entity (Subtype_Indication (Parent (New_T))) = Old_T | |
8007 | then | |
8008 | null; | |
8009 | else | |
8010 | Set_Etype (New_F, Old_T); | |
8011 | end if; | |
8012 | ||
8013 | Next_Formal (New_F); | |
8014 | Next_Formal (Old_F); | |
8015 | end loop; | |
ed3fe8cc | 8016 | |
60aa5228 | 8017 | pragma Assert (No (Old_F)); |
996ae0b0 | 8018 | |
bce79204 | 8019 | if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then |
996ae0b0 RK |
8020 | Set_Etype (New_S, Etype (Old_S)); |
8021 | end if; | |
8022 | end if; | |
8023 | end Inherit_Renamed_Profile; | |
8024 | ||
8025 | ---------------- | |
8026 | -- Initialize -- | |
8027 | ---------------- | |
8028 | ||
8029 | procedure Initialize is | |
8030 | begin | |
8031 | Urefs.Init; | |
8032 | end Initialize; | |
8033 | ||
8034 | ------------------------- | |
8035 | -- Install_Use_Clauses -- | |
8036 | ------------------------- | |
8037 | ||
0da2c8ac AC |
8038 | procedure Install_Use_Clauses |
8039 | (Clause : Node_Id; | |
8040 | Force_Installation : Boolean := False) | |
8041 | is | |
16ca248a | 8042 | U : Node_Id; |
996ae0b0 RK |
8043 | P : Node_Id; |
8044 | Id : Entity_Id; | |
8045 | ||
8046 | begin | |
16ca248a | 8047 | U := Clause; |
996ae0b0 RK |
8048 | while Present (U) loop |
8049 | ||
8050 | -- Case of USE package | |
8051 | ||
8052 | if Nkind (U) = N_Use_Package_Clause then | |
8053 | P := First (Names (U)); | |
996ae0b0 RK |
8054 | while Present (P) loop |
8055 | Id := Entity (P); | |
8056 | ||
8057 | if Ekind (Id) = E_Package then | |
996ae0b0 | 8058 | if In_Use (Id) then |
d4810530 | 8059 | Note_Redundant_Use (P); |
996ae0b0 RK |
8060 | |
8061 | elsif Present (Renamed_Object (Id)) | |
8062 | and then In_Use (Renamed_Object (Id)) | |
8063 | then | |
d4810530 | 8064 | Note_Redundant_Use (P); |
996ae0b0 | 8065 | |
0da2c8ac | 8066 | elsif Force_Installation or else Applicable_Use (P) then |
996ae0b0 | 8067 | Use_One_Package (Id, U); |
0da2c8ac | 8068 | |
996ae0b0 RK |
8069 | end if; |
8070 | end if; | |
8071 | ||
8072 | Next (P); | |
8073 | end loop; | |
8074 | ||
16ca248a | 8075 | -- Case of USE TYPE |
996ae0b0 RK |
8076 | |
8077 | else | |
8078 | P := First (Subtype_Marks (U)); | |
996ae0b0 | 8079 | while Present (P) loop |
fbf5a39b AC |
8080 | if not Is_Entity_Name (P) |
8081 | or else No (Entity (P)) | |
8082 | then | |
8083 | null; | |
996ae0b0 | 8084 | |
fbf5a39b | 8085 | elsif Entity (P) /= Any_Type then |
07fc65c4 | 8086 | Use_One_Type (P); |
996ae0b0 RK |
8087 | end if; |
8088 | ||
8089 | Next (P); | |
8090 | end loop; | |
8091 | end if; | |
8092 | ||
8093 | Next_Use_Clause (U); | |
8094 | end loop; | |
8095 | end Install_Use_Clauses; | |
8096 | ||
8097 | ------------------------------------- | |
8098 | -- Is_Appropriate_For_Entry_Prefix -- | |
8099 | ------------------------------------- | |
8100 | ||
8101 | function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is | |
8102 | P_Type : Entity_Id := T; | |
8103 | ||
8104 | begin | |
8105 | if Is_Access_Type (P_Type) then | |
8106 | P_Type := Designated_Type (P_Type); | |
8107 | end if; | |
8108 | ||
8109 | return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type); | |
8110 | end Is_Appropriate_For_Entry_Prefix; | |
8111 | ||
8112 | ------------------------------- | |
8113 | -- Is_Appropriate_For_Record -- | |
8114 | ------------------------------- | |
8115 | ||
2e071734 AC |
8116 | function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is |
8117 | ||
996ae0b0 RK |
8118 | function Has_Components (T1 : Entity_Id) return Boolean; |
8119 | -- Determine if given type has components (i.e. is either a record | |
8120 | -- type or a type that has discriminants). | |
8121 | ||
16ca248a ES |
8122 | -------------------- |
8123 | -- Has_Components -- | |
8124 | -------------------- | |
8125 | ||
996ae0b0 RK |
8126 | function Has_Components (T1 : Entity_Id) return Boolean is |
8127 | begin | |
8128 | return Is_Record_Type (T1) | |
8129 | or else (Is_Private_Type (T1) and then Has_Discriminants (T1)) | |
16ca248a ES |
8130 | or else (Is_Task_Type (T1) and then Has_Discriminants (T1)) |
8131 | or else (Is_Incomplete_Type (T1) | |
7b56a91b | 8132 | and then From_Limited_With (T1) |
16ca248a | 8133 | and then Present (Non_Limited_View (T1)) |
11560bcc TQ |
8134 | and then Is_Record_Type |
8135 | (Get_Full_View (Non_Limited_View (T1)))); | |
996ae0b0 RK |
8136 | end Has_Components; |
8137 | ||
8138 | -- Start of processing for Is_Appropriate_For_Record | |
8139 | ||
8140 | begin | |
8141 | return | |
8142 | Present (T) | |
8143 | and then (Has_Components (T) | |
16ca248a ES |
8144 | or else (Is_Access_Type (T) |
8145 | and then Has_Components (Designated_Type (T)))); | |
996ae0b0 RK |
8146 | end Is_Appropriate_For_Record; |
8147 | ||
d4810530 ES |
8148 | ------------------------ |
8149 | -- Note_Redundant_Use -- | |
8150 | ------------------------ | |
8151 | ||
8152 | procedure Note_Redundant_Use (Clause : Node_Id) is | |
8153 | Pack_Name : constant Entity_Id := Entity (Clause); | |
8154 | Cur_Use : constant Node_Id := Current_Use_Clause (Pack_Name); | |
8155 | Decl : constant Node_Id := Parent (Clause); | |
8156 | ||
8157 | Prev_Use : Node_Id := Empty; | |
8158 | Redundant : Node_Id := Empty; | |
f7ca1d04 AC |
8159 | -- The Use_Clause which is actually redundant. In the simplest case it |
8160 | -- is Pack itself, but when we compile a body we install its context | |
8161 | -- before that of its spec, in which case it is the use_clause in the | |
8162 | -- spec that will appear to be redundant, and we want the warning to be | |
8163 | -- placed on the body. Similar complications appear when the redundancy | |
8164 | -- is between a child unit and one of its ancestors. | |
d4810530 ES |
8165 | |
8166 | begin | |
8167 | Set_Redundant_Use (Clause, True); | |
8168 | ||
8169 | if not Comes_From_Source (Clause) | |
8170 | or else In_Instance | |
8171 | or else not Warn_On_Redundant_Constructs | |
8172 | then | |
8173 | return; | |
8174 | end if; | |
8175 | ||
8176 | if not Is_Compilation_Unit (Current_Scope) then | |
8177 | ||
f7ca1d04 AC |
8178 | -- If the use_clause is in an inner scope, it is made redundant by |
8179 | -- some clause in the current context, with one exception: If we're | |
8180 | -- compiling a nested package body, and the use_clause comes from the | |
8181 | -- corresponding spec, the clause is not necessarily fully redundant, | |
8182 | -- so we should not warn. If a warning was warranted, it would have | |
8183 | -- been given when the spec was processed. | |
cdc8c54c BD |
8184 | |
8185 | if Nkind (Parent (Decl)) = N_Package_Specification then | |
8186 | declare | |
8187 | Package_Spec_Entity : constant Entity_Id := | |
8188 | Defining_Unit_Name (Parent (Decl)); | |
8189 | begin | |
8190 | if In_Package_Body (Package_Spec_Entity) then | |
8191 | return; | |
8192 | end if; | |
8193 | end; | |
8194 | end if; | |
d4810530 ES |
8195 | |
8196 | Redundant := Clause; | |
8197 | Prev_Use := Cur_Use; | |
8198 | ||
8199 | elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then | |
8200 | declare | |
8201 | Cur_Unit : constant Unit_Number_Type := Get_Source_Unit (Cur_Use); | |
8202 | New_Unit : constant Unit_Number_Type := Get_Source_Unit (Clause); | |
8203 | Scop : Entity_Id; | |
8204 | ||
8205 | begin | |
8206 | if Cur_Unit = New_Unit then | |
8207 | ||
8208 | -- Redundant clause in same body | |
8209 | ||
8210 | Redundant := Clause; | |
8211 | Prev_Use := Cur_Use; | |
8212 | ||
8213 | elsif Cur_Unit = Current_Sem_Unit then | |
8214 | ||
8215 | -- If the new clause is not in the current unit it has been | |
8216 | -- analyzed first, and it makes the other one redundant. | |
8217 | -- However, if the new clause appears in a subunit, Cur_Unit | |
8218 | -- is still the parent, and in that case the redundant one | |
8219 | -- is the one appearing in the subunit. | |
8220 | ||
8221 | if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then | |
8222 | Redundant := Clause; | |
8223 | Prev_Use := Cur_Use; | |
8224 | ||
8225 | -- Most common case: redundant clause in body, | |
8226 | -- original clause in spec. Current scope is spec entity. | |
8227 | ||
8228 | elsif | |
8229 | Current_Scope = | |
8230 | Defining_Entity ( | |
8231 | Unit (Library_Unit (Cunit (Current_Sem_Unit)))) | |
8232 | then | |
8233 | Redundant := Cur_Use; | |
8234 | Prev_Use := Clause; | |
8235 | ||
8236 | else | |
8237 | -- The new clause may appear in an unrelated unit, when | |
8238 | -- the parents of a generic are being installed prior to | |
8239 | -- instantiation. In this case there must be no warning. | |
8240 | -- We detect this case by checking whether the current top | |
8241 | -- of the stack is related to the current compilation. | |
8242 | ||
8243 | Scop := Current_Scope; | |
ac7d724d | 8244 | while Present (Scop) and then Scop /= Standard_Standard loop |
d4810530 ES |
8245 | if Is_Compilation_Unit (Scop) |
8246 | and then not Is_Child_Unit (Scop) | |
8247 | then | |
8248 | return; | |
8249 | ||
8250 | elsif Scop = Cunit_Entity (Current_Sem_Unit) then | |
8251 | exit; | |
8252 | end if; | |
8253 | ||
8254 | Scop := Scope (Scop); | |
8255 | end loop; | |
8256 | ||
8257 | Redundant := Cur_Use; | |
8258 | Prev_Use := Clause; | |
8259 | end if; | |
8260 | ||
8261 | elsif New_Unit = Current_Sem_Unit then | |
8262 | Redundant := Clause; | |
8263 | Prev_Use := Cur_Use; | |
8264 | ||
8265 | else | |
8266 | -- Neither is the current unit, so they appear in parent or | |
8267 | -- sibling units. Warning will be emitted elsewhere. | |
8268 | ||
8269 | return; | |
8270 | end if; | |
8271 | end; | |
8272 | ||
8273 | elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration | |
8274 | and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit)))) | |
8275 | then | |
f7ca1d04 AC |
8276 | -- Use_clause is in child unit of current unit, and the child unit |
8277 | -- appears in the context of the body of the parent, so it has been | |
8278 | -- installed first, even though it is the redundant one. Depending on | |
8279 | -- their placement in the context, the visible or the private parts | |
8280 | -- of the two units, either might appear as redundant, but the | |
8281 | -- message has to be on the current unit. | |
d4810530 ES |
8282 | |
8283 | if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then | |
8284 | Redundant := Cur_Use; | |
8285 | Prev_Use := Clause; | |
8286 | else | |
8287 | Redundant := Clause; | |
8288 | Prev_Use := Cur_Use; | |
8289 | end if; | |
8290 | ||
8291 | -- If the new use clause appears in the private part of a parent unit | |
f3d57416 | 8292 | -- it may appear to be redundant w.r.t. a use clause in a child unit, |
d4810530 ES |
8293 | -- but the previous use clause was needed in the visible part of the |
8294 | -- child, and no warning should be emitted. | |
8295 | ||
8296 | if Nkind (Parent (Decl)) = N_Package_Specification | |
8297 | and then | |
8298 | List_Containing (Decl) = Private_Declarations (Parent (Decl)) | |
8299 | then | |
8300 | declare | |
8301 | Par : constant Entity_Id := Defining_Entity (Parent (Decl)); | |
8302 | Spec : constant Node_Id := | |
8303 | Specification (Unit (Cunit (Current_Sem_Unit))); | |
8304 | ||
8305 | begin | |
8306 | if Is_Compilation_Unit (Par) | |
8307 | and then Par /= Cunit_Entity (Current_Sem_Unit) | |
8308 | and then Parent (Cur_Use) = Spec | |
8309 | and then | |
8310 | List_Containing (Cur_Use) = Visible_Declarations (Spec) | |
8311 | then | |
8312 | return; | |
8313 | end if; | |
8314 | end; | |
8315 | end if; | |
8316 | ||
ff81221b ES |
8317 | -- Finally, if the current use clause is in the context then |
8318 | -- the clause is redundant when it is nested within the unit. | |
8319 | ||
8320 | elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit | |
8321 | and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit | |
8322 | and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause) | |
8323 | then | |
8324 | Redundant := Clause; | |
8325 | Prev_Use := Cur_Use; | |
8326 | ||
d4810530 ES |
8327 | else |
8328 | null; | |
8329 | end if; | |
8330 | ||
8331 | if Present (Redundant) then | |
8332 | Error_Msg_Sloc := Sloc (Prev_Use); | |
ed2233dc | 8333 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 8334 | ("& is already use-visible through previous use clause #??", |
954c111a | 8335 | Redundant, Pack_Name); |
d4810530 ES |
8336 | end if; |
8337 | end Note_Redundant_Use; | |
8338 | ||
996ae0b0 RK |
8339 | --------------- |
8340 | -- Pop_Scope -- | |
8341 | --------------- | |
8342 | ||
8343 | procedure Pop_Scope is | |
fbf5a39b | 8344 | SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last); |
fab2daeb | 8345 | S : constant Entity_Id := SST.Entity; |
996ae0b0 RK |
8346 | |
8347 | begin | |
8348 | if Debug_Flag_E then | |
8349 | Write_Info; | |
8350 | end if; | |
8351 | ||
fab2daeb AC |
8352 | -- Set Default_Storage_Pool field of the library unit if necessary |
8353 | ||
8354 | if Ekind_In (S, E_Package, E_Generic_Package) | |
8355 | and then | |
8356 | Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit | |
8357 | then | |
8358 | declare | |
8359 | Aux : constant Node_Id := | |
4adf3c50 | 8360 | Aux_Decls_Node (Parent (Unit_Declaration_Node (S))); |
fab2daeb AC |
8361 | begin |
8362 | if No (Default_Storage_Pool (Aux)) then | |
8363 | Set_Default_Storage_Pool (Aux, Default_Pool); | |
8364 | end if; | |
8365 | end; | |
8366 | end if; | |
8367 | ||
21d27997 | 8368 | Scope_Suppress := SST.Save_Scope_Suppress; |
11560bcc | 8369 | Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top; |
21d27997 | 8370 | Check_Policy_List := SST.Save_Check_Policy_List; |
fab2daeb | 8371 | Default_Pool := SST.Save_Default_Storage_Pool; |
49d41397 | 8372 | No_Tagged_Streams := SST.Save_No_Tagged_Streams; |
43417b90 | 8373 | SPARK_Mode := SST.Save_SPARK_Mode; |
579847c2 | 8374 | SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma; |
220d1fd9 | 8375 | Default_SSO := SST.Save_Default_SSO; |
96e90ac1 | 8376 | Uneval_Old := SST.Save_Uneval_Old; |
996ae0b0 RK |
8377 | |
8378 | if Debug_Flag_W then | |
fab2daeb | 8379 | Write_Str ("<-- exiting scope: "); |
996ae0b0 RK |
8380 | Write_Name (Chars (Current_Scope)); |
8381 | Write_Str (", Depth="); | |
8382 | Write_Int (Int (Scope_Stack.Last)); | |
8383 | Write_Eol; | |
8384 | end if; | |
8385 | ||
fbf5a39b | 8386 | End_Use_Clauses (SST.First_Use_Clause); |
996ae0b0 RK |
8387 | |
8388 | -- If the actions to be wrapped are still there they will get lost | |
8389 | -- causing incomplete code to be generated. It is better to abort in | |
fbf5a39b | 8390 | -- this case (and we do the abort even with assertions off since the |
5eeeed5e | 8391 | -- penalty is incorrect code generation). |
996ae0b0 | 8392 | |
36295779 | 8393 | if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then |
46202729 | 8394 | raise Program_Error; |
fbf5a39b | 8395 | end if; |
996ae0b0 RK |
8396 | |
8397 | -- Free last subprogram name if allocated, and pop scope | |
8398 | ||
fbf5a39b | 8399 | Free (SST.Last_Subprogram_Name); |
996ae0b0 RK |
8400 | Scope_Stack.Decrement_Last; |
8401 | end Pop_Scope; | |
8402 | ||
fbe627af RD |
8403 | --------------- |
8404 | -- Push_Scope -- | |
8405 | --------------- | |
8406 | ||
8407 | procedure Push_Scope (S : Entity_Id) is | |
fab2daeb | 8408 | E : constant Entity_Id := Scope (S); |
fbe627af RD |
8409 | |
8410 | begin | |
8411 | if Ekind (S) = E_Void then | |
8412 | null; | |
8413 | ||
f7ca1d04 AC |
8414 | -- Set scope depth if not a non-concurrent type, and we have not yet set |
8415 | -- the scope depth. This means that we have the first occurrence of the | |
8416 | -- scope, and this is where the depth is set. | |
fbe627af RD |
8417 | |
8418 | elsif (not Is_Type (S) or else Is_Concurrent_Type (S)) | |
8419 | and then not Scope_Depth_Set (S) | |
8420 | then | |
8421 | if S = Standard_Standard then | |
8422 | Set_Scope_Depth_Value (S, Uint_0); | |
8423 | ||
8424 | elsif Is_Child_Unit (S) then | |
8425 | Set_Scope_Depth_Value (S, Uint_1); | |
8426 | ||
8427 | elsif not Is_Record_Type (Current_Scope) then | |
8428 | if Ekind (S) = E_Loop then | |
8429 | Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope)); | |
8430 | else | |
8431 | Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1); | |
8432 | end if; | |
8433 | end if; | |
8434 | end if; | |
8435 | ||
8436 | Scope_Stack.Increment_Last; | |
8437 | ||
8438 | declare | |
8439 | SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last); | |
8440 | ||
8441 | begin | |
11560bcc TQ |
8442 | SST.Entity := S; |
8443 | SST.Save_Scope_Suppress := Scope_Suppress; | |
8444 | SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top; | |
21d27997 | 8445 | SST.Save_Check_Policy_List := Check_Policy_List; |
fab2daeb | 8446 | SST.Save_Default_Storage_Pool := Default_Pool; |
49d41397 | 8447 | SST.Save_No_Tagged_Streams := No_Tagged_Streams; |
43417b90 | 8448 | SST.Save_SPARK_Mode := SPARK_Mode; |
579847c2 | 8449 | SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma; |
220d1fd9 | 8450 | SST.Save_Default_SSO := Default_SSO; |
96e90ac1 | 8451 | SST.Save_Uneval_Old := Uneval_Old; |
fbe627af | 8452 | |
194d6f3f JS |
8453 | -- Each new scope pushed onto the scope stack inherits the component |
8454 | -- alignment of the previous scope. This emulates the "visibility" | |
8455 | -- semantics of pragma Component_Alignment. | |
8456 | ||
fbe627af | 8457 | if Scope_Stack.Last > Scope_Stack.First then |
5dc203d2 AC |
8458 | SST.Component_Alignment_Default := |
8459 | Scope_Stack.Table | |
8460 | (Scope_Stack.Last - 1). Component_Alignment_Default; | |
194d6f3f JS |
8461 | |
8462 | -- Otherwise, this is the first scope being pushed on the scope | |
8463 | -- stack. Inherit the component alignment from the configuration | |
8464 | -- form of pragma Component_Alignment (if any). | |
8465 | ||
8466 | else | |
8467 | SST.Component_Alignment_Default := | |
8468 | Configuration_Component_Alignment; | |
fbe627af RD |
8469 | end if; |
8470 | ||
8471 | SST.Last_Subprogram_Name := null; | |
8472 | SST.Is_Transient := False; | |
8473 | SST.Node_To_Be_Wrapped := Empty; | |
8474 | SST.Pending_Freeze_Actions := No_List; | |
36295779 | 8475 | SST.Actions_To_Be_Wrapped := (others => No_List); |
fbe627af RD |
8476 | SST.First_Use_Clause := Empty; |
8477 | SST.Is_Active_Stack_Base := False; | |
8478 | SST.Previous_Visibility := False; | |
8071b771 | 8479 | SST.Locked_Shared_Objects := No_Elist; |
fbe627af RD |
8480 | end; |
8481 | ||
8482 | if Debug_Flag_W then | |
8483 | Write_Str ("--> new scope: "); | |
8484 | Write_Name (Chars (Current_Scope)); | |
8485 | Write_Str (", Id="); | |
8486 | Write_Int (Int (Current_Scope)); | |
8487 | Write_Str (", Depth="); | |
8488 | Write_Int (Int (Scope_Stack.Last)); | |
8489 | Write_Eol; | |
8490 | end if; | |
8491 | ||
f7ca1d04 AC |
8492 | -- Deal with copying flags from the previous scope to this one. This is |
8493 | -- not necessary if either scope is standard, or if the new scope is a | |
8494 | -- child unit. | |
fbe627af RD |
8495 | |
8496 | if S /= Standard_Standard | |
8497 | and then Scope (S) /= Standard_Standard | |
8498 | and then not Is_Child_Unit (S) | |
8499 | then | |
fbe627af RD |
8500 | if Nkind (E) not in N_Entity then |
8501 | return; | |
8502 | end if; | |
8503 | ||
8504 | -- Copy categorization flags from Scope (S) to S, this is not done | |
8505 | -- when Scope (S) is Standard_Standard since propagation is from | |
8506 | -- library unit entity inwards. Copy other relevant attributes as | |
8507 | -- well (Discard_Names in particular). | |
8508 | ||
8509 | -- We only propagate inwards for library level entities, | |
8510 | -- inner level subprograms do not inherit the categorization. | |
8511 | ||
8512 | if Is_Library_Level_Entity (S) then | |
8513 | Set_Is_Preelaborated (S, Is_Preelaborated (E)); | |
8514 | Set_Is_Shared_Passive (S, Is_Shared_Passive (E)); | |
8515 | Set_Discard_Names (S, Discard_Names (E)); | |
8516 | Set_Suppress_Value_Tracking_On_Call | |
8517 | (S, Suppress_Value_Tracking_On_Call (E)); | |
8518 | Set_Categorization_From_Scope (E => S, Scop => E); | |
8519 | end if; | |
8520 | end if; | |
fab2daeb AC |
8521 | |
8522 | if Is_Child_Unit (S) | |
8523 | and then Present (E) | |
8524 | and then Ekind_In (E, E_Package, E_Generic_Package) | |
8525 | and then | |
8526 | Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit | |
8527 | then | |
8528 | declare | |
8529 | Aux : constant Node_Id := | |
4adf3c50 | 8530 | Aux_Decls_Node (Parent (Unit_Declaration_Node (E))); |
fab2daeb AC |
8531 | begin |
8532 | if Present (Default_Storage_Pool (Aux)) then | |
8533 | Default_Pool := Default_Storage_Pool (Aux); | |
8534 | end if; | |
8535 | end; | |
8536 | end if; | |
fbe627af RD |
8537 | end Push_Scope; |
8538 | ||
996ae0b0 RK |
8539 | --------------------- |
8540 | -- Premature_Usage -- | |
8541 | --------------------- | |
8542 | ||
8543 | procedure Premature_Usage (N : Node_Id) is | |
fbf5a39b | 8544 | Kind : constant Node_Kind := Nkind (Parent (Entity (N))); |
996ae0b0 RK |
8545 | E : Entity_Id := Entity (N); |
8546 | ||
8547 | begin | |
8548 | -- Within an instance, the analysis of the actual for a formal object | |
16ca248a ES |
8549 | -- does not see the name of the object itself. This is significant only |
8550 | -- if the object is an aggregate, where its analysis does not do any | |
8551 | -- name resolution on component associations. (see 4717-008). In such a | |
8552 | -- case, look for the visible homonym on the chain. | |
996ae0b0 | 8553 | |
ac7d724d | 8554 | if In_Instance and then Present (Homonym (E)) then |
996ae0b0 | 8555 | E := Homonym (E); |
ac7d724d | 8556 | while Present (E) and then not In_Open_Scopes (Scope (E)) loop |
996ae0b0 RK |
8557 | E := Homonym (E); |
8558 | end loop; | |
8559 | ||
8560 | if Present (E) then | |
8561 | Set_Entity (N, E); | |
8562 | Set_Etype (N, Etype (E)); | |
8563 | return; | |
8564 | end if; | |
8565 | end if; | |
8566 | ||
8567 | if Kind = N_Component_Declaration then | |
8568 | Error_Msg_N | |
8569 | ("component&! cannot be used before end of record declaration", N); | |
8570 | ||
8571 | elsif Kind = N_Parameter_Specification then | |
8572 | Error_Msg_N | |
8573 | ("formal parameter&! cannot be used before end of specification", | |
8574 | N); | |
8575 | ||
8576 | elsif Kind = N_Discriminant_Specification then | |
8577 | Error_Msg_N | |
8578 | ("discriminant&! cannot be used before end of discriminant part", | |
8579 | N); | |
8580 | ||
8581 | elsif Kind = N_Procedure_Specification | |
8582 | or else Kind = N_Function_Specification | |
8583 | then | |
8584 | Error_Msg_N | |
8585 | ("subprogram&! cannot be used before end of its declaration", | |
8586 | N); | |
90067a15 ES |
8587 | |
8588 | elsif Kind = N_Full_Type_Declaration then | |
8589 | Error_Msg_N | |
8590 | ("type& cannot be used before end of its declaration!", N); | |
8591 | ||
996ae0b0 RK |
8592 | else |
8593 | Error_Msg_N | |
8594 | ("object& cannot be used before end of its declaration!", N); | |
42fe76e0 AC |
8595 | |
8596 | -- If the premature reference appears as the expression in its own | |
8597 | -- declaration, rewrite it to prevent compiler loops in subsequent | |
8598 | -- uses of this mangled declaration in address clauses. | |
8599 | ||
8600 | if Nkind (Parent (N)) = N_Object_Declaration then | |
8601 | Set_Entity (N, Any_Id); | |
8602 | end if; | |
996ae0b0 RK |
8603 | end if; |
8604 | end Premature_Usage; | |
8605 | ||
8606 | ------------------------ | |
8607 | -- Present_System_Aux -- | |
8608 | ------------------------ | |
8609 | ||
8610 | function Present_System_Aux (N : Node_Id := Empty) return Boolean is | |
8611 | Loc : Source_Ptr; | |
fbe627af | 8612 | Aux_Name : Unit_Name_Type; |
996ae0b0 RK |
8613 | Unum : Unit_Number_Type; |
8614 | Withn : Node_Id; | |
8615 | With_Sys : Node_Id; | |
8616 | The_Unit : Node_Id; | |
8617 | ||
8618 | function Find_System (C_Unit : Node_Id) return Entity_Id; | |
16ca248a | 8619 | -- Scan context clause of compilation unit to find with_clause |
996ae0b0 RK |
8620 | -- for System. |
8621 | ||
2e071734 AC |
8622 | ----------------- |
8623 | -- Find_System -- | |
8624 | ----------------- | |
8625 | ||
996ae0b0 RK |
8626 | function Find_System (C_Unit : Node_Id) return Entity_Id is |
8627 | With_Clause : Node_Id; | |
8628 | ||
8629 | begin | |
8630 | With_Clause := First (Context_Items (C_Unit)); | |
996ae0b0 RK |
8631 | while Present (With_Clause) loop |
8632 | if (Nkind (With_Clause) = N_With_Clause | |
8633 | and then Chars (Name (With_Clause)) = Name_System) | |
8634 | and then Comes_From_Source (With_Clause) | |
8635 | then | |
8636 | return With_Clause; | |
8637 | end if; | |
8638 | ||
8639 | Next (With_Clause); | |
8640 | end loop; | |
8641 | ||
8642 | return Empty; | |
8643 | end Find_System; | |
8644 | ||
8645 | -- Start of processing for Present_System_Aux | |
8646 | ||
8647 | begin | |
bc41faa2 | 8648 | -- The child unit may have been loaded and analyzed already |
996ae0b0 RK |
8649 | |
8650 | if Present (System_Aux_Id) then | |
8651 | return True; | |
8652 | ||
8653 | -- If no previous pragma for System.Aux, nothing to load | |
8654 | ||
fbf5a39b | 8655 | elsif No (System_Extend_Unit) then |
996ae0b0 RK |
8656 | return False; |
8657 | ||
8658 | -- Use the unit name given in the pragma to retrieve the unit. | |
8659 | -- Verify that System itself appears in the context clause of the | |
8660 | -- current compilation. If System is not present, an error will | |
8661 | -- have been reported already. | |
8662 | ||
8663 | else | |
8664 | With_Sys := Find_System (Cunit (Current_Sem_Unit)); | |
8665 | ||
8666 | The_Unit := Unit (Cunit (Current_Sem_Unit)); | |
8667 | ||
8668 | if No (With_Sys) | |
294ccb21 RD |
8669 | and then |
8670 | (Nkind (The_Unit) = N_Package_Body | |
ac7d724d ES |
8671 | or else (Nkind (The_Unit) = N_Subprogram_Body |
8672 | and then not Acts_As_Spec (Cunit (Current_Sem_Unit)))) | |
996ae0b0 RK |
8673 | then |
8674 | With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit))); | |
8675 | end if; | |
8676 | ||
ac7d724d ES |
8677 | if No (With_Sys) and then Present (N) then |
8678 | ||
996ae0b0 RK |
8679 | -- If we are compiling a subunit, we need to examine its |
8680 | -- context as well (Current_Sem_Unit is the parent unit); | |
8681 | ||
8682 | The_Unit := Parent (N); | |
996ae0b0 RK |
8683 | while Nkind (The_Unit) /= N_Compilation_Unit loop |
8684 | The_Unit := Parent (The_Unit); | |
8685 | end loop; | |
8686 | ||
8687 | if Nkind (Unit (The_Unit)) = N_Subunit then | |
8688 | With_Sys := Find_System (The_Unit); | |
8689 | end if; | |
8690 | end if; | |
8691 | ||
8692 | if No (With_Sys) then | |
8693 | return False; | |
8694 | end if; | |
8695 | ||
8696 | Loc := Sloc (With_Sys); | |
fbf5a39b | 8697 | Get_Name_String (Chars (Expression (System_Extend_Unit))); |
996ae0b0 RK |
8698 | Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len); |
8699 | Name_Buffer (1 .. 7) := "system."; | |
8700 | Name_Buffer (Name_Len + 8) := '%'; | |
8701 | Name_Buffer (Name_Len + 9) := 's'; | |
8702 | Name_Len := Name_Len + 9; | |
8703 | Aux_Name := Name_Find; | |
8704 | ||
8705 | Unum := | |
8706 | Load_Unit | |
8707 | (Load_Name => Aux_Name, | |
8708 | Required => False, | |
8709 | Subunit => False, | |
8710 | Error_Node => With_Sys); | |
8711 | ||
8712 | if Unum /= No_Unit then | |
8713 | Semantics (Cunit (Unum)); | |
8714 | System_Aux_Id := | |
8715 | Defining_Entity (Specification (Unit (Cunit (Unum)))); | |
8716 | ||
16ca248a ES |
8717 | Withn := |
8718 | Make_With_Clause (Loc, | |
8719 | Name => | |
8720 | Make_Expanded_Name (Loc, | |
8721 | Chars => Chars (System_Aux_Id), | |
e4494292 RD |
8722 | Prefix => New_Occurrence_Of (Scope (System_Aux_Id), Loc), |
8723 | Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc))); | |
996ae0b0 RK |
8724 | |
8725 | Set_Entity (Name (Withn), System_Aux_Id); | |
8726 | ||
16ca248a ES |
8727 | Set_Library_Unit (Withn, Cunit (Unum)); |
8728 | Set_Corresponding_Spec (Withn, System_Aux_Id); | |
8729 | Set_First_Name (Withn, True); | |
8730 | Set_Implicit_With (Withn, True); | |
996ae0b0 RK |
8731 | |
8732 | Insert_After (With_Sys, Withn); | |
8733 | Mark_Rewrite_Insertion (Withn); | |
8734 | Set_Context_Installed (Withn); | |
8735 | ||
8736 | return True; | |
8737 | ||
8738 | -- Here if unit load failed | |
8739 | ||
8740 | else | |
8741 | Error_Msg_Name_1 := Name_System; | |
fbf5a39b | 8742 | Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit)); |
996ae0b0 RK |
8743 | Error_Msg_N |
8744 | ("extension package `%.%` does not exist", | |
fbf5a39b | 8745 | Opt.System_Extend_Unit); |
996ae0b0 RK |
8746 | return False; |
8747 | end if; | |
8748 | end if; | |
8749 | end Present_System_Aux; | |
8750 | ||
8751 | ------------------------- | |
8752 | -- Restore_Scope_Stack -- | |
8753 | ------------------------- | |
8754 | ||
e530a2d1 AC |
8755 | procedure Restore_Scope_Stack |
8756 | (List : Elist_Id; | |
8757 | Handle_Use : Boolean := True) | |
8758 | is | |
8759 | SS_Last : constant Int := Scope_Stack.Last; | |
8760 | Elmt : Elmt_Id; | |
996ae0b0 RK |
8761 | |
8762 | begin | |
9e40f163 AC |
8763 | -- Restore visibility of previous scope stack, if any, using the list |
8764 | -- we saved (we use Remove, since this list will not be used again). | |
996ae0b0 | 8765 | |
9e40f163 AC |
8766 | loop |
8767 | Elmt := Last_Elmt (List); | |
8768 | exit when Elmt = No_Elmt; | |
e530a2d1 | 8769 | Set_Is_Immediately_Visible (Node (Elmt)); |
9e40f163 | 8770 | Remove_Last_Elmt (List); |
996ae0b0 | 8771 | end loop; |
fbf5a39b | 8772 | |
9e40f163 AC |
8773 | -- Restore use clauses |
8774 | ||
fbf5a39b AC |
8775 | if SS_Last >= Scope_Stack.First |
8776 | and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard | |
ecad994d | 8777 | and then Handle_Use |
fbf5a39b AC |
8778 | then |
8779 | Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause); | |
8780 | end if; | |
996ae0b0 RK |
8781 | end Restore_Scope_Stack; |
8782 | ||
8783 | ---------------------- | |
8784 | -- Save_Scope_Stack -- | |
8785 | ---------------------- | |
8786 | ||
9e40f163 AC |
8787 | -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid |
8788 | -- consuming any memory. That is, Save_Scope_Stack took care of removing | |
8789 | -- from immediate visibility entities and Restore_Scope_Stack took care | |
8790 | -- of restoring their visibility analyzing the context of each entity. The | |
8791 | -- problem of such approach is that it was fragile and caused unexpected | |
8792 | -- visibility problems, and indeed one test was found where there was a | |
8793 | -- real problem. | |
8794 | ||
8795 | -- Furthermore, the following experiment was carried out: | |
8796 | ||
8797 | -- - Save_Scope_Stack was modified to store in an Elist1 all those | |
8798 | -- entities whose attribute Is_Immediately_Visible is modified | |
8799 | -- from True to False. | |
8800 | ||
8801 | -- - Restore_Scope_Stack was modified to store in another Elist2 | |
8802 | -- all the entities whose attribute Is_Immediately_Visible is | |
8803 | -- modified from False to True. | |
8804 | ||
8805 | -- - Extra code was added to verify that all the elements of Elist1 | |
8806 | -- are found in Elist2 | |
8807 | ||
a35017dc | 8808 | -- This test shows that there may be more occurrences of this problem which |
9e40f163 AC |
8809 | -- have not yet been detected. As a result, we replaced that approach by |
8810 | -- the current one in which Save_Scope_Stack returns the list of entities | |
a35017dc | 8811 | -- whose visibility is changed, and that list is passed to Restore_Scope_ |
9e40f163 AC |
8812 | -- Stack to undo that change. This approach is simpler and safer, although |
8813 | -- it consumes more memory. | |
8814 | ||
e530a2d1 AC |
8815 | function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is |
8816 | Result : constant Elist_Id := New_Elmt_List; | |
996ae0b0 RK |
8817 | E : Entity_Id; |
8818 | S : Entity_Id; | |
8819 | SS_Last : constant Int := Scope_Stack.Last; | |
8820 | ||
e530a2d1 AC |
8821 | procedure Remove_From_Visibility (E : Entity_Id); |
8822 | -- If E is immediately visible then append it to the result and remove | |
85f6a831 RD |
8823 | -- it temporarily from visibility. |
8824 | ||
8825 | ---------------------------- | |
8826 | -- Remove_From_Visibility -- | |
8827 | ---------------------------- | |
8828 | ||
e530a2d1 AC |
8829 | procedure Remove_From_Visibility (E : Entity_Id) is |
8830 | begin | |
8831 | if Is_Immediately_Visible (E) then | |
8832 | Append_Elmt (E, Result); | |
8833 | Set_Is_Immediately_Visible (E, False); | |
8834 | end if; | |
8835 | end Remove_From_Visibility; | |
8836 | ||
85f6a831 RD |
8837 | -- Start of processing for Save_Scope_Stack |
8838 | ||
996ae0b0 RK |
8839 | begin |
8840 | if SS_Last >= Scope_Stack.First | |
8841 | and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard | |
8842 | then | |
ecad994d AC |
8843 | if Handle_Use then |
8844 | End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause); | |
8845 | end if; | |
fbf5a39b | 8846 | |
16ca248a ES |
8847 | -- If the call is from within a compilation unit, as when called from |
8848 | -- Rtsfind, make current entries in scope stack invisible while we | |
8849 | -- analyze the new unit. | |
996ae0b0 RK |
8850 | |
8851 | for J in reverse 0 .. SS_Last loop | |
8852 | exit when Scope_Stack.Table (J).Entity = Standard_Standard | |
8853 | or else No (Scope_Stack.Table (J).Entity); | |
8854 | ||
8855 | S := Scope_Stack.Table (J).Entity; | |
e530a2d1 AC |
8856 | |
8857 | Remove_From_Visibility (S); | |
996ae0b0 | 8858 | |
16ca248a | 8859 | E := First_Entity (S); |
996ae0b0 | 8860 | while Present (E) loop |
e530a2d1 | 8861 | Remove_From_Visibility (E); |
996ae0b0 RK |
8862 | Next_Entity (E); |
8863 | end loop; | |
8864 | end loop; | |
8865 | ||
8866 | end if; | |
e530a2d1 AC |
8867 | |
8868 | return Result; | |
996ae0b0 RK |
8869 | end Save_Scope_Stack; |
8870 | ||
8871 | ------------- | |
8872 | -- Set_Use -- | |
8873 | ------------- | |
8874 | ||
8875 | procedure Set_Use (L : List_Id) is | |
8876 | Decl : Node_Id; | |
8877 | Pack_Name : Node_Id; | |
8878 | Pack : Entity_Id; | |
8879 | Id : Entity_Id; | |
8880 | ||
8881 | begin | |
8882 | if Present (L) then | |
8883 | Decl := First (L); | |
996ae0b0 RK |
8884 | while Present (Decl) loop |
8885 | if Nkind (Decl) = N_Use_Package_Clause then | |
8886 | Chain_Use_Clause (Decl); | |
996ae0b0 | 8887 | |
16ca248a | 8888 | Pack_Name := First (Names (Decl)); |
996ae0b0 RK |
8889 | while Present (Pack_Name) loop |
8890 | Pack := Entity (Pack_Name); | |
8891 | ||
8892 | if Ekind (Pack) = E_Package | |
8893 | and then Applicable_Use (Pack_Name) | |
8894 | then | |
8895 | Use_One_Package (Pack, Decl); | |
8896 | end if; | |
8897 | ||
8898 | Next (Pack_Name); | |
8899 | end loop; | |
8900 | ||
bcb0389e | 8901 | elsif Nkind (Decl) = N_Use_Type_Clause then |
996ae0b0 | 8902 | Chain_Use_Clause (Decl); |
996ae0b0 | 8903 | |
16ca248a | 8904 | Id := First (Subtype_Marks (Decl)); |
996ae0b0 RK |
8905 | while Present (Id) loop |
8906 | if Entity (Id) /= Any_Type then | |
07fc65c4 | 8907 | Use_One_Type (Id); |
996ae0b0 RK |
8908 | end if; |
8909 | ||
8910 | Next (Id); | |
8911 | end loop; | |
8912 | end if; | |
8913 | ||
8914 | Next (Decl); | |
8915 | end loop; | |
8916 | end if; | |
8917 | end Set_Use; | |
8918 | ||
8919 | --------------------- | |
8920 | -- Use_One_Package -- | |
8921 | --------------------- | |
8922 | ||
8923 | procedure Use_One_Package (P : Entity_Id; N : Node_Id) is | |
8924 | Id : Entity_Id; | |
8925 | Prev : Entity_Id; | |
8926 | Current_Instance : Entity_Id := Empty; | |
8927 | Real_P : Entity_Id; | |
9bc856dd | 8928 | Private_With_OK : Boolean := False; |
996ae0b0 RK |
8929 | |
8930 | begin | |
8931 | if Ekind (P) /= E_Package then | |
8932 | return; | |
8933 | end if; | |
8934 | ||
8935 | Set_In_Use (P); | |
d4810530 | 8936 | Set_Current_Use_Clause (P, N); |
996ae0b0 | 8937 | |
0ab80019 | 8938 | -- Ada 2005 (AI-50217): Check restriction |
19f0526a | 8939 | |
7b56a91b | 8940 | if From_Limited_With (P) then |
657a9dd9 | 8941 | Error_Msg_N ("limited withed package cannot appear in use clause", N); |
996ae0b0 RK |
8942 | end if; |
8943 | ||
bc41faa2 | 8944 | -- Find enclosing instance, if any |
996ae0b0 RK |
8945 | |
8946 | if In_Instance then | |
8947 | Current_Instance := Current_Scope; | |
996ae0b0 RK |
8948 | while not Is_Generic_Instance (Current_Instance) loop |
8949 | Current_Instance := Scope (Current_Instance); | |
8950 | end loop; | |
8951 | ||
8952 | if No (Hidden_By_Use_Clause (N)) then | |
8953 | Set_Hidden_By_Use_Clause (N, New_Elmt_List); | |
8954 | end if; | |
8955 | end if; | |
8956 | ||
8957 | -- If unit is a package renaming, indicate that the renamed | |
8958 | -- package is also in use (the flags on both entities must | |
8959 | -- remain consistent, and a subsequent use of either of them | |
8960 | -- should be recognized as redundant). | |
8961 | ||
8962 | if Present (Renamed_Object (P)) then | |
8963 | Set_In_Use (Renamed_Object (P)); | |
d4810530 | 8964 | Set_Current_Use_Clause (Renamed_Object (P), N); |
996ae0b0 RK |
8965 | Real_P := Renamed_Object (P); |
8966 | else | |
8967 | Real_P := P; | |
8968 | end if; | |
8969 | ||
0ab80019 | 8970 | -- Ada 2005 (AI-262): Check the use_clause of a private withed package |
9bc856dd AC |
8971 | -- found in the private part of a package specification |
8972 | ||
8973 | if In_Private_Part (Current_Scope) | |
8974 | and then Has_Private_With (P) | |
8975 | and then Is_Child_Unit (Current_Scope) | |
8976 | and then Is_Child_Unit (P) | |
8977 | and then Is_Ancestor_Package (Scope (Current_Scope), P) | |
8978 | then | |
8979 | Private_With_OK := True; | |
8980 | end if; | |
8981 | ||
996ae0b0 RK |
8982 | -- Loop through entities in one package making them potentially |
8983 | -- use-visible. | |
8984 | ||
8985 | Id := First_Entity (P); | |
8986 | while Present (Id) | |
9bc856dd | 8987 | and then (Id /= First_Private_Entity (P) |
ac7d724d | 8988 | or else Private_With_OK) -- Ada 2005 (AI-262) |
996ae0b0 RK |
8989 | loop |
8990 | Prev := Current_Entity (Id); | |
996ae0b0 RK |
8991 | while Present (Prev) loop |
8992 | if Is_Immediately_Visible (Prev) | |
8993 | and then (not Is_Overloadable (Prev) | |
8994 | or else not Is_Overloadable (Id) | |
8995 | or else (Type_Conformant (Id, Prev))) | |
8996 | then | |
8997 | if No (Current_Instance) then | |
8998 | ||
8999 | -- Potentially use-visible entity remains hidden | |
9000 | ||
9001 | goto Next_Usable_Entity; | |
9002 | ||
16ca248a ES |
9003 | -- A use clause within an instance hides outer global entities, |
9004 | -- which are not used to resolve local entities in the | |
9005 | -- instance. Note that the predefined entities in Standard | |
9006 | -- could not have been hidden in the generic by a use clause, | |
9007 | -- and therefore remain visible. Other compilation units whose | |
9008 | -- entities appear in Standard must be hidden in an instance. | |
996ae0b0 RK |
9009 | |
9010 | -- To determine whether an entity is external to the instance | |
9011 | -- we compare the scope depth of its scope with that of the | |
9012 | -- current instance. However, a generic actual of a subprogram | |
9013 | -- instance is declared in the wrapper package but will not be | |
e074d476 AC |
9014 | -- hidden by a use-visible entity. similarly, an entity that is |
9015 | -- declared in an enclosing instance will not be hidden by an | |
9016 | -- an entity declared in a generic actual, which can only have | |
9017 | -- been use-visible in the generic and will not have hidden the | |
9018 | -- entity in the generic parent. | |
996ae0b0 | 9019 | |
82c80734 RD |
9020 | -- If Id is called Standard, the predefined package with the |
9021 | -- same name is in the homonym chain. It has to be ignored | |
9022 | -- because it has no defined scope (being the only entity in | |
9023 | -- the system with this mandated behavior). | |
9024 | ||
996ae0b0 | 9025 | elsif not Is_Hidden (Id) |
82c80734 | 9026 | and then Present (Scope (Prev)) |
996ae0b0 RK |
9027 | and then not Is_Wrapper_Package (Scope (Prev)) |
9028 | and then Scope_Depth (Scope (Prev)) < | |
9029 | Scope_Depth (Current_Instance) | |
9030 | and then (Scope (Prev) /= Standard_Standard | |
9031 | or else Sloc (Prev) > Standard_Location) | |
9032 | then | |
e074d476 AC |
9033 | if In_Open_Scopes (Scope (Prev)) |
9034 | and then Is_Generic_Instance (Scope (Prev)) | |
c94a0b9d AC |
9035 | and then Present (Associated_Formal_Package (P)) |
9036 | then | |
9037 | null; | |
9038 | ||
9039 | else | |
9040 | Set_Is_Potentially_Use_Visible (Id); | |
9041 | Set_Is_Immediately_Visible (Prev, False); | |
9042 | Append_Elmt (Prev, Hidden_By_Use_Clause (N)); | |
9043 | end if; | |
996ae0b0 RK |
9044 | end if; |
9045 | ||
16ca248a ES |
9046 | -- A user-defined operator is not use-visible if the predefined |
9047 | -- operator for the type is immediately visible, which is the case | |
9048 | -- if the type of the operand is in an open scope. This does not | |
9049 | -- apply to user-defined operators that have operands of different | |
9050 | -- types, because the predefined mixed mode operations (multiply | |
9051 | -- and divide) apply to universal types and do not hide anything. | |
996ae0b0 RK |
9052 | |
9053 | elsif Ekind (Prev) = E_Operator | |
9054 | and then Operator_Matches_Spec (Prev, Id) | |
9055 | and then In_Open_Scopes | |
ac7d724d | 9056 | (Scope (Base_Type (Etype (First_Formal (Id))))) |
996ae0b0 | 9057 | and then (No (Next_Formal (First_Formal (Id))) |
ac7d724d ES |
9058 | or else Etype (First_Formal (Id)) = |
9059 | Etype (Next_Formal (First_Formal (Id))) | |
996ae0b0 RK |
9060 | or else Chars (Prev) = Name_Op_Expon) |
9061 | then | |
9062 | goto Next_Usable_Entity; | |
e69614ad AC |
9063 | |
9064 | -- In an instance, two homonyms may become use_visible through the | |
9065 | -- actuals of distinct formal packages. In the generic, only the | |
9066 | -- current one would have been visible, so make the other one | |
9067 | -- not use_visible. | |
9068 | ||
9069 | elsif Present (Current_Instance) | |
9070 | and then Is_Potentially_Use_Visible (Prev) | |
9071 | and then not Is_Overloadable (Prev) | |
9072 | and then Scope (Id) /= Scope (Prev) | |
9073 | and then Used_As_Generic_Actual (Scope (Prev)) | |
9074 | and then Used_As_Generic_Actual (Scope (Id)) | |
30196a76 RD |
9075 | and then not In_Same_List (Current_Use_Clause (Scope (Prev)), |
9076 | Current_Use_Clause (Scope (Id))) | |
e69614ad AC |
9077 | then |
9078 | Set_Is_Potentially_Use_Visible (Prev, False); | |
9079 | Append_Elmt (Prev, Hidden_By_Use_Clause (N)); | |
996ae0b0 RK |
9080 | end if; |
9081 | ||
9082 | Prev := Homonym (Prev); | |
9083 | end loop; | |
9084 | ||
bc41faa2 | 9085 | -- On exit, we know entity is not hidden, unless it is private |
996ae0b0 RK |
9086 | |
9087 | if not Is_Hidden (Id) | |
ac7d724d | 9088 | and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id)) |
996ae0b0 RK |
9089 | then |
9090 | Set_Is_Potentially_Use_Visible (Id); | |
9091 | ||
ac7d724d | 9092 | if Is_Private_Type (Id) and then Present (Full_View (Id)) then |
996ae0b0 RK |
9093 | Set_Is_Potentially_Use_Visible (Full_View (Id)); |
9094 | end if; | |
9095 | end if; | |
9096 | ||
9097 | <<Next_Usable_Entity>> | |
9098 | Next_Entity (Id); | |
9099 | end loop; | |
9100 | ||
16ca248a ES |
9101 | -- Child units are also made use-visible by a use clause, but they may |
9102 | -- appear after all visible declarations in the parent entity list. | |
996ae0b0 RK |
9103 | |
9104 | while Present (Id) loop | |
8398e82e | 9105 | if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then |
996ae0b0 RK |
9106 | Set_Is_Potentially_Use_Visible (Id); |
9107 | end if; | |
9108 | ||
9109 | Next_Entity (Id); | |
9110 | end loop; | |
9111 | ||
9112 | if Chars (Real_P) = Name_System | |
9113 | and then Scope (Real_P) = Standard_Standard | |
9114 | and then Present_System_Aux (N) | |
9115 | then | |
9116 | Use_One_Package (System_Aux_Id, N); | |
9117 | end if; | |
9118 | ||
9119 | end Use_One_Package; | |
9120 | ||
9121 | ------------------ | |
9122 | -- Use_One_Type -- | |
9123 | ------------------ | |
9124 | ||
7ff2d234 | 9125 | procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False) is |
954c111a HK |
9126 | Elmt : Elmt_Id; |
9127 | Is_Known_Used : Boolean; | |
9128 | Op_List : Elist_Id; | |
9129 | T : Entity_Id; | |
9130 | ||
9131 | function Spec_Reloaded_For_Body return Boolean; | |
9132 | -- Determine whether the compilation unit is a package body and the use | |
9133 | -- type clause is in the spec of the same package. Even though the spec | |
9134 | -- was analyzed first, its context is reloaded when analysing the body. | |
9135 | ||
29efbb8c ES |
9136 | procedure Use_Class_Wide_Operations (Typ : Entity_Id); |
9137 | -- AI05-150: if the use_type_clause carries the "all" qualifier, | |
9138 | -- class-wide operations of ancestor types are use-visible if the | |
9139 | -- ancestor type is visible. | |
9140 | ||
954c111a HK |
9141 | ---------------------------- |
9142 | -- Spec_Reloaded_For_Body -- | |
9143 | ---------------------------- | |
9144 | ||
9145 | function Spec_Reloaded_For_Body return Boolean is | |
9146 | begin | |
9147 | if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then | |
9148 | declare | |
9149 | Spec : constant Node_Id := | |
9150 | Parent (List_Containing (Parent (Id))); | |
f146302c | 9151 | |
d99ff0f4 | 9152 | begin |
f146302c AC |
9153 | -- Check whether type is declared in a package specification, |
9154 | -- and current unit is the corresponding package body. The | |
9155 | -- use clauses themselves may be within a nested package. | |
9156 | ||
954c111a HK |
9157 | return |
9158 | Nkind (Spec) = N_Package_Specification | |
d99ff0f4 AC |
9159 | and then |
9160 | In_Same_Source_Unit (Corresponding_Body (Parent (Spec)), | |
9161 | Cunit_Entity (Current_Sem_Unit)); | |
954c111a HK |
9162 | end; |
9163 | end if; | |
9164 | ||
9165 | return False; | |
9166 | end Spec_Reloaded_For_Body; | |
9167 | ||
29efbb8c ES |
9168 | ------------------------------- |
9169 | -- Use_Class_Wide_Operations -- | |
9170 | ------------------------------- | |
9171 | ||
9172 | procedure Use_Class_Wide_Operations (Typ : Entity_Id) is | |
9173 | Scop : Entity_Id; | |
9174 | Ent : Entity_Id; | |
9175 | ||
9176 | function Is_Class_Wide_Operation_Of | |
9177 | (Op : Entity_Id; | |
9178 | T : Entity_Id) return Boolean; | |
9179 | -- Determine whether a subprogram has a class-wide parameter or | |
9180 | -- result that is T'Class. | |
9181 | ||
9182 | --------------------------------- | |
9183 | -- Is_Class_Wide_Operation_Of -- | |
9184 | --------------------------------- | |
9185 | ||
9186 | function Is_Class_Wide_Operation_Of | |
9187 | (Op : Entity_Id; | |
9188 | T : Entity_Id) return Boolean | |
9189 | is | |
9190 | Formal : Entity_Id; | |
9191 | ||
9192 | begin | |
9193 | Formal := First_Formal (Op); | |
9194 | while Present (Formal) loop | |
9195 | if Etype (Formal) = Class_Wide_Type (T) then | |
9196 | return True; | |
9197 | end if; | |
9198 | Next_Formal (Formal); | |
9199 | end loop; | |
9200 | ||
9201 | if Etype (Op) = Class_Wide_Type (T) then | |
9202 | return True; | |
9203 | end if; | |
9204 | ||
9205 | return False; | |
9206 | end Is_Class_Wide_Operation_Of; | |
9207 | ||
9208 | -- Start of processing for Use_Class_Wide_Operations | |
9209 | ||
9210 | begin | |
9211 | Scop := Scope (Typ); | |
9212 | if not Is_Hidden (Scop) then | |
9213 | Ent := First_Entity (Scop); | |
9214 | while Present (Ent) loop | |
9215 | if Is_Overloadable (Ent) | |
9216 | and then Is_Class_Wide_Operation_Of (Ent, Typ) | |
9217 | and then not Is_Potentially_Use_Visible (Ent) | |
9218 | then | |
9219 | Set_Is_Potentially_Use_Visible (Ent); | |
9220 | Append_Elmt (Ent, Used_Operations (Parent (Id))); | |
9221 | end if; | |
9222 | ||
9223 | Next_Entity (Ent); | |
9224 | end loop; | |
9225 | end if; | |
9226 | ||
9227 | if Is_Derived_Type (Typ) then | |
9228 | Use_Class_Wide_Operations (Etype (Base_Type (Typ))); | |
9229 | end if; | |
9230 | end Use_Class_Wide_Operations; | |
9231 | ||
dea1d3dc | 9232 | -- Start of processing for Use_One_Type |
996ae0b0 RK |
9233 | |
9234 | begin | |
9235 | -- It is the type determined by the subtype mark (8.4(8)) whose | |
9236 | -- operations become potentially use-visible. | |
9237 | ||
9238 | T := Base_Type (Entity (Id)); | |
9239 | ||
954c111a HK |
9240 | -- Either the type itself is used, the package where it is declared |
9241 | -- is in use or the entity is declared in the current package, thus | |
9242 | -- use-visible. | |
9243 | ||
9244 | Is_Known_Used := | |
9245 | In_Use (T) | |
9246 | or else In_Use (Scope (T)) | |
9247 | or else Scope (T) = Current_Scope; | |
9248 | ||
9249 | Set_Redundant_Use (Id, | |
9250 | Is_Known_Used or else Is_Potentially_Use_Visible (T)); | |
996ae0b0 | 9251 | |
ecc4ddde AC |
9252 | if Ekind (T) = E_Incomplete_Type then |
9253 | Error_Msg_N ("premature usage of incomplete type", Id); | |
9254 | ||
9255 | elsif In_Open_Scopes (Scope (T)) then | |
996ae0b0 RK |
9256 | null; |
9257 | ||
f7ca1d04 AC |
9258 | -- A limited view cannot appear in a use_type clause. However, an access |
9259 | -- type whose designated type is limited has the flag but is not itself | |
9260 | -- a limited view unless we only have a limited view of its enclosing | |
9261 | -- package. | |
294ccb21 | 9262 | |
7b56a91b | 9263 | elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then |
923fa078 | 9264 | Error_Msg_N |
cc0b3bac AC |
9265 | ("incomplete type from limited view cannot appear in use clause", |
9266 | Id); | |
923fa078 | 9267 | |
c6840e83 AC |
9268 | -- If the use clause is redundant, Used_Operations will usually be |
9269 | -- empty, but we need to set it to empty here in one case: If we are | |
9270 | -- instantiating a generic library unit, then we install the ancestors | |
9271 | -- of that unit in the scope stack, which involves reprocessing use | |
9272 | -- clauses in those ancestors. Such a use clause will typically have a | |
9273 | -- nonempty Used_Operations unless it was redundant in the generic unit, | |
9274 | -- even if it is redundant at the place of the instantiation. | |
9275 | ||
9276 | elsif Redundant_Use (Id) then | |
9277 | Set_Used_Operations (Parent (Id), New_Elmt_List); | |
9278 | ||
fbf5a39b AC |
9279 | -- If the subtype mark designates a subtype in a different package, |
9280 | -- we have to check that the parent type is visible, otherwise the | |
9281 | -- use type clause is a noop. Not clear how to do that??? | |
9282 | ||
c6840e83 | 9283 | else |
996ae0b0 | 9284 | Set_In_Use (T); |
c3b36d48 AC |
9285 | |
9286 | -- If T is tagged, primitive operators on class-wide operands | |
9287 | -- are also available. | |
9288 | ||
9289 | if Is_Tagged_Type (T) then | |
9290 | Set_In_Use (Class_Wide_Type (T)); | |
9291 | end if; | |
9292 | ||
21d27997 | 9293 | Set_Current_Use_Clause (T, Parent (Id)); |
996ae0b0 | 9294 | |
29efbb8c ES |
9295 | -- Iterate over primitive operations of the type. If an operation is |
9296 | -- already use_visible, it is the result of a previous use_clause, | |
7ff2d234 AC |
9297 | -- and already appears on the corresponding entity chain. If the |
9298 | -- clause is being reinstalled, operations are already use-visible. | |
29efbb8c | 9299 | |
7ff2d234 AC |
9300 | if Installed then |
9301 | null; | |
29efbb8c | 9302 | |
7ff2d234 AC |
9303 | else |
9304 | Op_List := Collect_Primitive_Operations (T); | |
9305 | Elmt := First_Elmt (Op_List); | |
9306 | while Present (Elmt) loop | |
9307 | if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol | |
9308 | or else Chars (Node (Elmt)) in Any_Operator_Name) | |
9309 | and then not Is_Hidden (Node (Elmt)) | |
9310 | and then not Is_Potentially_Use_Visible (Node (Elmt)) | |
9311 | then | |
9312 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
9313 | Append_Elmt (Node (Elmt), Used_Operations (Parent (Id))); | |
996ae0b0 | 9314 | |
7ff2d234 AC |
9315 | elsif Ada_Version >= Ada_2012 |
9316 | and then All_Present (Parent (Id)) | |
9317 | and then not Is_Hidden (Node (Elmt)) | |
9318 | and then not Is_Potentially_Use_Visible (Node (Elmt)) | |
9319 | then | |
9320 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
9321 | Append_Elmt (Node (Elmt), Used_Operations (Parent (Id))); | |
9322 | end if; | |
954c111a | 9323 | |
7ff2d234 AC |
9324 | Next_Elmt (Elmt); |
9325 | end loop; | |
9326 | end if; | |
9327 | ||
9328 | if Ada_Version >= Ada_2012 | |
9329 | and then All_Present (Parent (Id)) | |
9330 | and then Is_Tagged_Type (T) | |
9331 | then | |
9332 | Use_Class_Wide_Operations (T); | |
9333 | end if; | |
29efbb8c ES |
9334 | end if; |
9335 | ||
954c111a HK |
9336 | -- If warning on redundant constructs, check for unnecessary WITH |
9337 | ||
9338 | if Warn_On_Redundant_Constructs | |
9339 | and then Is_Known_Used | |
9340 | ||
ac7d724d ES |
9341 | -- with P; with P; use P; |
9342 | -- package P is package X is package body X is | |
9343 | -- type T ... use P.T; | |
954c111a | 9344 | |
ac7d724d ES |
9345 | -- The compilation unit is the body of X. GNAT first compiles the |
9346 | -- spec of X, then proceeds to the body. At that point P is marked | |
9347 | -- as use visible. The analysis then reinstalls the spec along with | |
9348 | -- its context. The use clause P.T is now recognized as redundant, | |
9349 | -- but in the wrong context. Do not emit a warning in such cases. | |
9350 | -- Do not emit a warning either if we are in an instance, there is | |
9351 | -- no redundancy between an outer use_clause and one that appears | |
9352 | -- within the generic. | |
954c111a HK |
9353 | |
9354 | and then not Spec_Reloaded_For_Body | |
c56094bd | 9355 | and then not In_Instance |
954c111a HK |
9356 | then |
9357 | -- The type already has a use clause | |
9358 | ||
9359 | if In_Use (T) then | |
3ea52b2e ES |
9360 | |
9361 | -- Case where we know the current use clause for the type | |
9362 | ||
21d27997 | 9363 | if Present (Current_Use_Clause (T)) then |
44b90160 | 9364 | Use_Clause_Known : declare |
21d27997 RD |
9365 | Clause1 : constant Node_Id := Parent (Id); |
9366 | Clause2 : constant Node_Id := Current_Use_Clause (T); | |
3ea52b2e ES |
9367 | Ent1 : Entity_Id; |
9368 | Ent2 : Entity_Id; | |
21d27997 RD |
9369 | Err_No : Node_Id; |
9370 | Unit1 : Node_Id; | |
9371 | Unit2 : Node_Id; | |
9372 | ||
7ca139d3 TQ |
9373 | function Entity_Of_Unit (U : Node_Id) return Entity_Id; |
9374 | -- Return the appropriate entity for determining which unit | |
9375 | -- has a deeper scope: the defining entity for U, unless U | |
9376 | -- is a package instance, in which case we retrieve the | |
9377 | -- entity of the instance spec. | |
9378 | ||
9379 | -------------------- | |
9380 | -- Entity_Of_Unit -- | |
9381 | -------------------- | |
9382 | ||
9383 | function Entity_Of_Unit (U : Node_Id) return Entity_Id is | |
9384 | begin | |
1b1d88b1 | 9385 | if Nkind (U) = N_Package_Instantiation |
7ca139d3 TQ |
9386 | and then Analyzed (U) |
9387 | then | |
9388 | return Defining_Entity (Instance_Spec (U)); | |
9389 | else | |
9390 | return Defining_Entity (U); | |
9391 | end if; | |
9392 | end Entity_Of_Unit; | |
9393 | ||
44b90160 RD |
9394 | -- Start of processing for Use_Clause_Known |
9395 | ||
21d27997 | 9396 | begin |
f7ca1d04 AC |
9397 | -- If both current use type clause and the use type clause |
9398 | -- for the type are at the compilation unit level, one of | |
9399 | -- the units must be an ancestor of the other, and the | |
9400 | -- warning belongs on the descendant. | |
3ea52b2e | 9401 | |
21d27997 | 9402 | if Nkind (Parent (Clause1)) = N_Compilation_Unit |
3ea52b2e ES |
9403 | and then |
9404 | Nkind (Parent (Clause2)) = N_Compilation_Unit | |
21d27997 | 9405 | then |
b0d3b11d AC |
9406 | -- If the unit is a subprogram body that acts as spec, |
9407 | -- the context clause is shared with the constructed | |
9408 | -- subprogram spec. Clearly there is no redundancy. | |
9409 | ||
9410 | if Clause1 = Clause2 then | |
9411 | return; | |
9412 | end if; | |
9413 | ||
3ea52b2e ES |
9414 | Unit1 := Unit (Parent (Clause1)); |
9415 | Unit2 := Unit (Parent (Clause2)); | |
9416 | ||
32beb1f3 AC |
9417 | -- If both clauses are on same unit, or one is the body |
9418 | -- of the other, or one of them is in a subunit, report | |
9419 | -- redundancy on the later one. | |
f7ca1d04 AC |
9420 | |
9421 | if Unit1 = Unit2 then | |
9422 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); | |
ed2233dc | 9423 | Error_Msg_NE -- CODEFIX |
f7ca1d04 | 9424 | ("& is already use-visible through previous " |
dbfeb4fa | 9425 | & "use_type_clause #??", Clause1, T); |
f7ca1d04 | 9426 | return; |
32beb1f3 AC |
9427 | |
9428 | elsif Nkind (Unit1) = N_Subunit then | |
9429 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); | |
ed2233dc | 9430 | Error_Msg_NE -- CODEFIX |
32beb1f3 | 9431 | ("& is already use-visible through previous " |
dbfeb4fa | 9432 | & "use_type_clause #??", Clause1, T); |
32beb1f3 AC |
9433 | return; |
9434 | ||
9435 | elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body) | |
9436 | and then Nkind (Unit1) /= Nkind (Unit2) | |
9437 | and then Nkind (Unit1) /= N_Subunit | |
9438 | then | |
9439 | Error_Msg_Sloc := Sloc (Clause1); | |
ed2233dc | 9440 | Error_Msg_NE -- CODEFIX |
32beb1f3 | 9441 | ("& is already use-visible through previous " |
dbfeb4fa | 9442 | & "use_type_clause #??", Current_Use_Clause (T), T); |
32beb1f3 | 9443 | return; |
f7ca1d04 AC |
9444 | end if; |
9445 | ||
21d27997 RD |
9446 | -- There is a redundant use type clause in a child unit. |
9447 | -- Determine which of the units is more deeply nested. | |
3ea52b2e | 9448 | -- If a unit is a package instance, retrieve the entity |
7ca139d3 | 9449 | -- and its scope from the instance spec. |
21d27997 | 9450 | |
7ca139d3 TQ |
9451 | Ent1 := Entity_Of_Unit (Unit1); |
9452 | Ent2 := Entity_Of_Unit (Unit2); | |
3ea52b2e | 9453 | |
bcb0389e | 9454 | if Scope (Ent2) = Standard_Standard then |
21d27997 RD |
9455 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); |
9456 | Err_No := Clause1; | |
9457 | ||
3ea52b2e | 9458 | elsif Scope (Ent1) = Standard_Standard then |
21d27997 RD |
9459 | Error_Msg_Sloc := Sloc (Id); |
9460 | Err_No := Clause2; | |
9461 | ||
7ca139d3 TQ |
9462 | -- If both units are child units, we determine which one |
9463 | -- is the descendant by the scope distance to the | |
3ea52b2e | 9464 | -- ultimate parent unit. |
21d27997 | 9465 | |
3ea52b2e | 9466 | else |
21d27997 RD |
9467 | declare |
9468 | S1, S2 : Entity_Id; | |
9469 | ||
9470 | begin | |
3ea52b2e ES |
9471 | S1 := Scope (Ent1); |
9472 | S2 := Scope (Ent2); | |
c97c0163 AC |
9473 | while Present (S1) |
9474 | and then Present (S2) | |
9475 | and then S1 /= Standard_Standard | |
9476 | and then S2 /= Standard_Standard | |
21d27997 RD |
9477 | loop |
9478 | S1 := Scope (S1); | |
9479 | S2 := Scope (S2); | |
9480 | end loop; | |
9481 | ||
9482 | if S1 = Standard_Standard then | |
9483 | Error_Msg_Sloc := Sloc (Id); | |
9484 | Err_No := Clause2; | |
9485 | else | |
9486 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); | |
9487 | Err_No := Clause1; | |
9488 | end if; | |
9489 | end; | |
9490 | end if; | |
9491 | ||
ed2233dc | 9492 | Error_Msg_NE -- CODEFIX |
21d27997 | 9493 | ("& is already use-visible through previous " |
dbfeb4fa | 9494 | & "use_type_clause #??", Err_No, Id); |
3ea52b2e ES |
9495 | |
9496 | -- Case where current use type clause and the use type | |
9497 | -- clause for the type are not both at the compilation unit | |
9498 | -- level. In this case we don't have location information. | |
9499 | ||
21d27997 | 9500 | else |
ed2233dc | 9501 | Error_Msg_NE -- CODEFIX |
3ea52b2e | 9502 | ("& is already use-visible through previous " |
dbfeb4fa | 9503 | & "use type clause??", Id, T); |
21d27997 | 9504 | end if; |
44b90160 | 9505 | end Use_Clause_Known; |
3ea52b2e ES |
9506 | |
9507 | -- Here if Current_Use_Clause is not set for T, another case | |
9508 | -- where we do not have the location information available. | |
9509 | ||
21d27997 | 9510 | else |
ed2233dc | 9511 | Error_Msg_NE -- CODEFIX |
3ea52b2e | 9512 | ("& is already use-visible through previous " |
dbfeb4fa | 9513 | & "use type clause??", Id, T); |
21d27997 | 9514 | end if; |
954c111a HK |
9515 | |
9516 | -- The package where T is declared is already used | |
9517 | ||
9518 | elsif In_Use (Scope (T)) then | |
9519 | Error_Msg_Sloc := Sloc (Current_Use_Clause (Scope (T))); | |
ed2233dc | 9520 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 9521 | ("& is already use-visible through package use clause #??", |
37951d8e | 9522 | Id, T); |
954c111a HK |
9523 | |
9524 | -- The current scope is the package where T is declared | |
9525 | ||
9526 | else | |
9527 | Error_Msg_Node_2 := Scope (T); | |
ed2233dc | 9528 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 9529 | ("& is already use-visible inside package &??", Id, T); |
954c111a HK |
9530 | end if; |
9531 | end if; | |
996ae0b0 RK |
9532 | end Use_One_Type; |
9533 | ||
9534 | ---------------- | |
9535 | -- Write_Info -- | |
9536 | ---------------- | |
9537 | ||
9538 | procedure Write_Info is | |
9539 | Id : Entity_Id := First_Entity (Current_Scope); | |
9540 | ||
9541 | begin | |
9542 | -- No point in dumping standard entities | |
9543 | ||
9544 | if Current_Scope = Standard_Standard then | |
9545 | return; | |
9546 | end if; | |
9547 | ||
9548 | Write_Str ("========================================================"); | |
9549 | Write_Eol; | |
9550 | Write_Str (" Defined Entities in "); | |
9551 | Write_Name (Chars (Current_Scope)); | |
9552 | Write_Eol; | |
9553 | Write_Str ("========================================================"); | |
9554 | Write_Eol; | |
9555 | ||
9556 | if No (Id) then | |
9557 | Write_Str ("-- none --"); | |
9558 | Write_Eol; | |
9559 | ||
9560 | else | |
9561 | while Present (Id) loop | |
9562 | Write_Entity_Info (Id, " "); | |
9563 | Next_Entity (Id); | |
9564 | end loop; | |
9565 | end if; | |
9566 | ||
9567 | if Scope (Current_Scope) = Standard_Standard then | |
9568 | ||
9569 | -- Print information on the current unit itself | |
9570 | ||
9571 | Write_Entity_Info (Current_Scope, " "); | |
9572 | end if; | |
9573 | ||
9574 | Write_Eol; | |
9575 | end Write_Info; | |
9576 | ||
67536dcb RD |
9577 | -------- |
9578 | -- ws -- | |
9579 | -------- | |
996ae0b0 | 9580 | |
67536dcb | 9581 | procedure ws is |
996ae0b0 | 9582 | S : Entity_Id; |
996ae0b0 RK |
9583 | begin |
9584 | for J in reverse 1 .. Scope_Stack.Last loop | |
c8307596 | 9585 | S := Scope_Stack.Table (J).Entity; |
996ae0b0 RK |
9586 | Write_Int (Int (S)); |
9587 | Write_Str (" === "); | |
9588 | Write_Name (Chars (S)); | |
9589 | Write_Eol; | |
9590 | end loop; | |
67536dcb | 9591 | end ws; |
996ae0b0 | 9592 | |
c1ce0691 ES |
9593 | -------- |
9594 | -- we -- | |
9595 | -------- | |
9596 | ||
9597 | procedure we (S : Entity_Id) is | |
9598 | E : Entity_Id; | |
9599 | begin | |
9600 | E := First_Entity (S); | |
9601 | while Present (E) loop | |
9602 | Write_Int (Int (E)); | |
9603 | Write_Str (" === "); | |
9604 | Write_Name (Chars (E)); | |
9605 | Write_Eol; | |
c1ce0691 ES |
9606 | Next_Entity (E); |
9607 | end loop; | |
9608 | end we; | |
996ae0b0 | 9609 | end Sem_Ch8; |