]>
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 | -- -- | |
1d005acc | 9 | -- Copyright (C) 1992-2019, Free Software Foundation, Inc. -- |
996ae0b0 RK |
10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
b5c84c3c | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
996ae0b0 RK |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
b5c84c3c RD |
18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
996ae0b0 RK |
20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
996ae0b0 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
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; |
90e491a7 | 60 | with Sem_Elab; use Sem_Elab; |
a2dc5812 | 61 | with Sem_Eval; use Sem_Eval; |
19c6e49c | 62 | with Sem_Prag; use Sem_Prag; |
996ae0b0 RK |
63 | with Sem_Res; use Sem_Res; |
64 | with Sem_Util; use Sem_Util; | |
65 | with Sem_Type; use Sem_Type; | |
66 | with Stand; use Stand; | |
67 | with Sinfo; use Sinfo; | |
68 | with Sinfo.CN; use Sinfo.CN; | |
69 | with Snames; use Snames; | |
851e9f19 | 70 | with Style; |
996ae0b0 RK |
71 | with Table; |
72 | with Tbuild; use Tbuild; | |
73 | with Uintp; use Uintp; | |
74 | ||
996ae0b0 RK |
75 | package body Sem_Ch8 is |
76 | ||
77 | ------------------------------------ | |
78 | -- Visibility and Name Resolution -- | |
79 | ------------------------------------ | |
80 | ||
2c1b72d7 | 81 | -- This package handles name resolution and the collection of possible |
996ae0b0 RK |
82 | -- interpretations for overloaded names, prior to overload resolution. |
83 | ||
84 | -- Name resolution is the process that establishes a mapping between source | |
85 | -- identifiers and the entities they denote at each point in the program. | |
86 | -- Each entity is represented by a defining occurrence. Each identifier | |
87 | -- that denotes an entity points to the corresponding defining occurrence. | |
88 | -- This is the entity of the applied occurrence. Each occurrence holds | |
89 | -- an index into the names table, where source identifiers are stored. | |
90 | ||
91 | -- Each entry in the names table for an identifier or designator uses the | |
92 | -- Info pointer to hold a link to the currently visible entity that has | |
93 | -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id | |
94 | -- in package Sem_Util). The visibility is initialized at the beginning of | |
95 | -- semantic processing to make entities in package Standard immediately | |
96 | -- visible. The visibility table is used in a more subtle way when | |
97 | -- compiling subunits (see below). | |
98 | ||
99 | -- Entities that have the same name (i.e. homonyms) are chained. In the | |
100 | -- case of overloaded entities, this chain holds all the possible meanings | |
101 | -- of a given identifier. The process of overload resolution uses type | |
102 | -- information to select from this chain the unique meaning of a given | |
103 | -- identifier. | |
104 | ||
105 | -- Entities are also chained in their scope, through the Next_Entity link. | |
106 | -- As a consequence, the name space is organized as a sparse matrix, where | |
107 | -- each row corresponds to a scope, and each column to a source identifier. | |
108 | -- Open scopes, that is to say scopes currently being compiled, have their | |
109 | -- corresponding rows of entities in order, innermost scope first. | |
110 | ||
21d7ef70 | 111 | -- The scopes of packages that are mentioned in context clauses appear in |
996ae0b0 RK |
112 | -- no particular order, interspersed among open scopes. This is because |
113 | -- in the course of analyzing the context of a compilation, a package | |
114 | -- declaration is first an open scope, and subsequently an element of the | |
115 | -- context. If subunits or child units are present, a parent unit may | |
116 | -- appear under various guises at various times in the compilation. | |
117 | ||
118 | -- When the compilation of the innermost scope is complete, the entities | |
119 | -- defined therein are no longer visible. If the scope is not a package | |
120 | -- declaration, these entities are never visible subsequently, and can be | |
121 | -- removed from visibility chains. If the scope is a package declaration, | |
122 | -- its visible declarations may still be accessible. Therefore the entities | |
123 | -- defined in such a scope are left on the visibility chains, and only | |
124 | -- their visibility (immediately visibility or potential use-visibility) | |
125 | -- is affected. | |
126 | ||
127 | -- The ordering of homonyms on their chain does not necessarily follow | |
128 | -- the order of their corresponding scopes on the scope stack. For | |
129 | -- example, if package P and the enclosing scope both contain entities | |
130 | -- named E, then when compiling the package body the chain for E will | |
131 | -- hold the global entity first, and the local one (corresponding to | |
132 | -- the current inner scope) next. As a result, name resolution routines | |
133 | -- do not assume any relative ordering of the homonym chains, either | |
134 | -- for scope nesting or to order of appearance of context clauses. | |
135 | ||
136 | -- When compiling a child unit, entities in the parent scope are always | |
137 | -- immediately visible. When compiling the body of a child unit, private | |
138 | -- entities in the parent must also be made immediately visible. There | |
139 | -- are separate routines to make the visible and private declarations | |
140 | -- visible at various times (see package Sem_Ch7). | |
141 | ||
142 | -- +--------+ +-----+ | |
143 | -- | In use |-------->| EU1 |--------------------------> | |
144 | -- +--------+ +-----+ | |
145 | -- | | | |
146 | -- +--------+ +-----+ +-----+ | |
147 | -- | Stand. |---------------->| ES1 |--------------->| ES2 |---> | |
148 | -- +--------+ +-----+ +-----+ | |
149 | -- | | | |
150 | -- +---------+ | +-----+ | |
151 | -- | with'ed |------------------------------>| EW2 |---> | |
152 | -- +---------+ | +-----+ | |
153 | -- | | | |
154 | -- +--------+ +-----+ +-----+ | |
155 | -- | Scope2 |---------------->| E12 |--------------->| E22 |---> | |
156 | -- +--------+ +-----+ +-----+ | |
157 | -- | | | |
158 | -- +--------+ +-----+ +-----+ | |
159 | -- | Scope1 |---------------->| E11 |--------------->| E12 |---> | |
160 | -- +--------+ +-----+ +-----+ | |
161 | -- ^ | | | |
162 | -- | | | | |
163 | -- | +---------+ | | | |
164 | -- | | with'ed |-----------------------------------------> | |
165 | -- | +---------+ | | | |
166 | -- | | | | |
167 | -- Scope stack | | | |
168 | -- (innermost first) | | | |
169 | -- +----------------------------+ | |
170 | -- Names table => | Id1 | | | | Id2 | | |
171 | -- +----------------------------+ | |
172 | ||
173 | -- Name resolution must deal with several syntactic forms: simple names, | |
174 | -- qualified names, indexed names, and various forms of calls. | |
175 | ||
176 | -- Each identifier points to an entry in the names table. The resolution | |
177 | -- of a simple name consists in traversing the homonym chain, starting | |
178 | -- from the names table. If an entry is immediately visible, it is the one | |
fbf5a39b | 179 | -- designated by the identifier. If only potentially use-visible entities |
996ae0b0 RK |
180 | -- are on the chain, we must verify that they do not hide each other. If |
181 | -- the entity we find is overloadable, we collect all other overloadable | |
182 | -- entities on the chain as long as they are not hidden. | |
183 | -- | |
184 | -- To resolve expanded names, we must find the entity at the intersection | |
185 | -- of the entity chain for the scope (the prefix) and the homonym chain | |
186 | -- for the selector. In general, homonym chains will be much shorter than | |
187 | -- entity chains, so it is preferable to start from the names table as | |
188 | -- well. If the entity found is overloadable, we must collect all other | |
189 | -- interpretations that are defined in the scope denoted by the prefix. | |
190 | ||
191 | -- For records, protected types, and tasks, their local entities are | |
192 | -- removed from visibility chains on exit from the corresponding scope. | |
193 | -- From the outside, these entities are always accessed by selected | |
194 | -- notation, and the entity chain for the record type, protected type, | |
21d7ef70 | 195 | -- etc. is traversed sequentially in order to find the designated entity. |
996ae0b0 RK |
196 | |
197 | -- The discriminants of a type and the operations of a protected type or | |
198 | -- task are unchained on exit from the first view of the type, (such as | |
199 | -- a private or incomplete type declaration, or a protected type speci- | |
fbf5a39b | 200 | -- fication) and re-chained when compiling the second view. |
996ae0b0 RK |
201 | |
202 | -- In the case of operators, we do not make operators on derived types | |
203 | -- explicit. As a result, the notation P."+" may denote either a user- | |
204 | -- defined function with name "+", or else an implicit declaration of the | |
205 | -- operator "+" in package P. The resolution of expanded names always | |
206 | -- tries to resolve an operator name as such an implicitly defined entity, | |
207 | -- in addition to looking for explicit declarations. | |
208 | ||
209 | -- All forms of names that denote entities (simple names, expanded names, | |
210 | -- character literals in some cases) have a Entity attribute, which | |
211 | -- identifies the entity denoted by the name. | |
212 | ||
213 | --------------------- | |
214 | -- The Scope Stack -- | |
215 | --------------------- | |
216 | ||
217 | -- The Scope stack keeps track of the scopes currently been compiled. | |
218 | -- Every entity that contains declarations (including records) is placed | |
219 | -- on the scope stack while it is being processed, and removed at the end. | |
220 | -- Whenever a non-package scope is exited, the entities defined therein | |
221 | -- are removed from the visibility table, so that entities in outer scopes | |
222 | -- become visible (see previous description). On entry to Sem, the scope | |
223 | -- stack only contains the package Standard. As usual, subunits complicate | |
224 | -- this picture ever so slightly. | |
225 | ||
226 | -- The Rtsfind mechanism can force a call to Semantics while another | |
227 | -- compilation is in progress. The unit retrieved by Rtsfind must be | |
21d7ef70 | 228 | -- compiled in its own context, and has no access to the visibility of |
996ae0b0 RK |
229 | -- the unit currently being compiled. The procedures Save_Scope_Stack and |
230 | -- Restore_Scope_Stack make entities in current open scopes invisible | |
231 | -- before compiling the retrieved unit, and restore the compilation | |
232 | -- environment afterwards. | |
233 | ||
234 | ------------------------ | |
235 | -- Compiling subunits -- | |
236 | ------------------------ | |
237 | ||
16ca248a ES |
238 | -- Subunits must be compiled in the environment of the corresponding stub, |
239 | -- that is to say with the same visibility into the parent (and its | |
996ae0b0 RK |
240 | -- context) that is available at the point of the stub declaration, but |
241 | -- with the additional visibility provided by the context clause of the | |
242 | -- subunit itself. As a result, compilation of a subunit forces compilation | |
243 | -- of the parent (see description in lib-). At the point of the stub | |
16ca248a ES |
244 | -- declaration, Analyze is called recursively to compile the proper body of |
245 | -- the subunit, but without reinitializing the names table, nor the scope | |
246 | -- stack (i.e. standard is not pushed on the stack). In this fashion the | |
247 | -- context of the subunit is added to the context of the parent, and the | |
248 | -- subunit is compiled in the correct environment. Note that in the course | |
249 | -- of processing the context of a subunit, Standard will appear twice on | |
250 | -- the scope stack: once for the parent of the subunit, and once for the | |
251 | -- unit in the context clause being compiled. However, the two sets of | |
252 | -- entities are not linked by homonym chains, so that the compilation of | |
253 | -- any context unit happens in a fresh visibility environment. | |
996ae0b0 RK |
254 | |
255 | ------------------------------- | |
256 | -- Processing of USE Clauses -- | |
257 | ------------------------------- | |
258 | ||
259 | -- Every defining occurrence has a flag indicating if it is potentially use | |
260 | -- visible. Resolution of simple names examines this flag. The processing | |
261 | -- of use clauses consists in setting this flag on all visible entities | |
262 | -- defined in the corresponding package. On exit from the scope of the use | |
263 | -- clause, the corresponding flag must be reset. However, a package may | |
a90bd866 | 264 | -- appear in several nested use clauses (pathological but legal, alas) |
996ae0b0 RK |
265 | -- which forces us to use a slightly more involved scheme: |
266 | ||
267 | -- a) The defining occurrence for a package holds a flag -In_Use- to | |
268 | -- indicate that it is currently in the scope of a use clause. If a | |
44d6a706 | 269 | -- redundant use clause is encountered, then the corresponding occurrence |
996ae0b0 RK |
270 | -- of the package name is flagged -Redundant_Use-. |
271 | ||
272 | -- b) On exit from a scope, the use clauses in its declarative part are | |
273 | -- scanned. The visibility flag is reset in all entities declared in | |
274 | -- package named in a use clause, as long as the package is not flagged | |
275 | -- as being in a redundant use clause (in which case the outer use | |
276 | -- clause is still in effect, and the direct visibility of its entities | |
277 | -- must be retained). | |
278 | ||
279 | -- Note that entities are not removed from their homonym chains on exit | |
280 | -- from the package specification. A subsequent use clause does not need | |
281 | -- to rechain the visible entities, but only to establish their direct | |
282 | -- visibility. | |
283 | ||
284 | ----------------------------------- | |
285 | -- Handling private declarations -- | |
286 | ----------------------------------- | |
287 | ||
288 | -- The principle that each entity has a single defining occurrence clashes | |
289 | -- with the presence of two separate definitions for private types: the | |
290 | -- first is the private type declaration, and second is the full type | |
291 | -- declaration. It is important that all references to the type point to | |
44d6a706 | 292 | -- the same defining occurrence, namely the first one. To enforce the two |
996ae0b0 RK |
293 | -- separate views of the entity, the corresponding information is swapped |
294 | -- between the two declarations. Outside of the package, the defining | |
44d6a706 | 295 | -- occurrence only contains the private declaration information, while in |
996ae0b0 RK |
296 | -- the private part and the body of the package the defining occurrence |
297 | -- contains the full declaration. To simplify the swap, the defining | |
298 | -- occurrence that currently holds the private declaration points to the | |
44d6a706 | 299 | -- full declaration. During semantic processing the defining occurrence |
16ca248a ES |
300 | -- also points to a list of private dependents, that is to say access types |
301 | -- or composite types whose designated types or component types are | |
996ae0b0 RK |
302 | -- subtypes or derived types of the private type in question. After the |
303 | -- full declaration has been seen, the private dependents are updated to | |
304 | -- indicate that they have full definitions. | |
305 | ||
306 | ------------------------------------ | |
307 | -- Handling of Undefined Messages -- | |
308 | ------------------------------------ | |
309 | ||
310 | -- In normal mode, only the first use of an undefined identifier generates | |
311 | -- a message. The table Urefs is used to record error messages that have | |
312 | -- been issued so that second and subsequent ones do not generate further | |
313 | -- messages. However, the second reference causes text to be added to the | |
314 | -- original undefined message noting "(more references follow)". The | |
315 | -- full error list option (-gnatf) forces messages to be generated for | |
316 | -- every reference and disconnects the use of this table. | |
317 | ||
318 | type Uref_Entry is record | |
319 | Node : Node_Id; | |
320 | -- Node for identifier for which original message was posted. The | |
321 | -- Chars field of this identifier is used to detect later references | |
322 | -- to the same identifier. | |
323 | ||
324 | Err : Error_Msg_Id; | |
325 | -- Records error message Id of original undefined message. Reset to | |
326 | -- No_Error_Msg after the second occurrence, where it is used to add | |
327 | -- text to the original message as described above. | |
328 | ||
329 | Nvis : Boolean; | |
330 | -- Set if the message is not visible rather than undefined | |
331 | ||
332 | Loc : Source_Ptr; | |
333 | -- Records location of error message. Used to make sure that we do | |
334 | -- not consider a, b : undefined as two separate instances, which | |
335 | -- would otherwise happen, since the parser converts this sequence | |
336 | -- to a : undefined; b : undefined. | |
337 | ||
338 | end record; | |
339 | ||
340 | package Urefs is new Table.Table ( | |
341 | Table_Component_Type => Uref_Entry, | |
342 | Table_Index_Type => Nat, | |
343 | Table_Low_Bound => 1, | |
344 | Table_Initial => 10, | |
345 | Table_Increment => 100, | |
346 | Table_Name => "Urefs"); | |
347 | ||
348 | Candidate_Renaming : Entity_Id; | |
349 | -- Holds a candidate interpretation that appears in a subprogram renaming | |
350 | -- declaration and does not match the given specification, but matches at | |
351 | -- least on the first formal. Allows better error message when given | |
352 | -- specification omits defaulted parameters, a common error. | |
353 | ||
354 | ----------------------- | |
355 | -- Local Subprograms -- | |
356 | ----------------------- | |
357 | ||
358 | procedure Analyze_Generic_Renaming | |
359 | (N : Node_Id; | |
360 | K : Entity_Kind); | |
361 | -- Common processing for all three kinds of generic renaming declarations. | |
362 | -- Enter new name and indicate that it renames the generic unit. | |
363 | ||
364 | procedure Analyze_Renamed_Character | |
365 | (N : Node_Id; | |
366 | New_S : Entity_Id; | |
367 | Is_Body : Boolean); | |
368 | -- Renamed entity is given by a character literal, which must belong | |
369 | -- to the return type of the new entity. Is_Body indicates whether the | |
370 | -- declaration is a renaming_as_body. If the original declaration has | |
371 | -- already been frozen (because of an intervening body, e.g.) the body of | |
372 | -- the function must be built now. The same applies to the following | |
373 | -- various renaming procedures. | |
374 | ||
375 | procedure Analyze_Renamed_Dereference | |
376 | (N : Node_Id; | |
377 | New_S : Entity_Id; | |
378 | Is_Body : Boolean); | |
379 | -- Renamed entity is given by an explicit dereference. Prefix must be a | |
380 | -- conformant access_to_subprogram type. | |
381 | ||
382 | procedure Analyze_Renamed_Entry | |
383 | (N : Node_Id; | |
384 | New_S : Entity_Id; | |
385 | Is_Body : Boolean); | |
386 | -- If the renamed entity in a subprogram renaming is an entry or protected | |
387 | -- subprogram, build a body for the new entity whose only statement is a | |
388 | -- call to the renamed entity. | |
389 | ||
390 | procedure Analyze_Renamed_Family_Member | |
391 | (N : Node_Id; | |
392 | New_S : Entity_Id; | |
393 | Is_Body : Boolean); | |
394 | -- Used when the renamed entity is an indexed component. The prefix must | |
395 | -- denote an entry family. | |
396 | ||
294ccb21 RD |
397 | procedure Analyze_Renamed_Primitive_Operation |
398 | (N : Node_Id; | |
399 | New_S : Entity_Id; | |
400 | Is_Body : Boolean); | |
401 | -- If the renamed entity in a subprogram renaming is a primitive operation | |
c5d00db0 AC |
402 | -- or a class-wide operation in prefix form, save the target object, |
403 | -- which must be added to the list of actuals in any subsequent call. | |
404 | -- The renaming operation is intrinsic because the compiler must in | |
405 | -- fact generate a wrapper for it (6.3.1 (10 1/2)). | |
294ccb21 | 406 | |
996ae0b0 | 407 | procedure Attribute_Renaming (N : Node_Id); |
4c8a5bb8 AC |
408 | -- Analyze renaming of attribute as subprogram. The renaming declaration N |
409 | -- is rewritten as a subprogram body that returns the attribute reference | |
996ae0b0 RK |
410 | -- applied to the formals of the function. |
411 | ||
4c484f40 | 412 | procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id); |
c42bfef2 RD |
413 | -- Set Entity, with style check if need be. For a discriminant reference, |
414 | -- replace by the corresponding discriminal, i.e. the parameter of the | |
415 | -- initialization procedure that corresponds to the discriminant. | |
4c484f40 | 416 | |
996ae0b0 RK |
417 | procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id); |
418 | -- A renaming_as_body may occur after the entity of the original decla- | |
419 | -- ration has been frozen. In that case, the body of the new entity must | |
420 | -- be built now, because the usual mechanism of building the renamed | |
421 | -- body at the point of freezing will not work. Subp is the subprogram | |
422 | -- for which N provides the Renaming_As_Body. | |
423 | ||
fbf5a39b AC |
424 | procedure Check_In_Previous_With_Clause |
425 | (N : Node_Id; | |
426 | Nam : Node_Id); | |
427 | -- N is a use_package clause and Nam the package name, or N is a use_type | |
428 | -- clause and Nam is the prefix of the type name. In either case, verify | |
429 | -- that the package is visible at that point in the context: either it | |
430 | -- appears in a previous with_clause, or because it is a fully qualified | |
431 | -- name and the root ancestor appears in a previous with_clause. | |
432 | ||
996ae0b0 RK |
433 | procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id); |
434 | -- Verify that the entity in a renaming declaration that is a library unit | |
435 | -- is itself a library unit and not a nested unit or subunit. Also check | |
436 | -- that if the renaming is a child unit of a generic parent, then the | |
437 | -- renamed unit must also be a child unit of that parent. Finally, verify | |
438 | -- that a renamed generic unit is not an implicit child declared within | |
439 | -- an instance of the parent. | |
440 | ||
441 | procedure Chain_Use_Clause (N : Node_Id); | |
d4810530 ES |
442 | -- Chain use clause onto list of uses clauses headed by First_Use_Clause in |
443 | -- the proper scope table entry. This is usually the current scope, but it | |
444 | -- will be an inner scope when installing the use clauses of the private | |
445 | -- declarations of a parent unit prior to compiling the private part of a | |
446 | -- child unit. This chain is traversed when installing/removing use clauses | |
447 | -- when compiling a subunit or instantiating a generic body on the fly, | |
448 | -- when it is necessary to save and restore full environments. | |
996ae0b0 | 449 | |
ac7d724d ES |
450 | function Enclosing_Instance return Entity_Id; |
451 | -- In an instance nested within another one, several semantic checks are | |
452 | -- unnecessary because the legality of the nested instance has been checked | |
453 | -- in the enclosing generic unit. This applies in particular to legality | |
454 | -- checks on actuals for formal subprograms of the inner instance, which | |
455 | -- are checked as subprogram renamings, and may be complicated by confusion | |
456 | -- in private/full views. This function returns the instance enclosing the | |
457 | -- current one if there is such, else it returns Empty. | |
458 | -- | |
459 | -- If the renaming determines the entity for the default of a formal | |
460 | -- subprogram nested within another instance, choose the innermost | |
461 | -- candidate. This is because if the formal has a box, and we are within | |
462 | -- an enclosing instance where some candidate interpretations are local | |
463 | -- to this enclosing instance, we know that the default was properly | |
464 | -- resolved when analyzing the generic, so we prefer the local | |
465 | -- candidates to those that are external. This is not always the case | |
466 | -- but is a reasonable heuristic on the use of nested generics. The | |
467 | -- proper solution requires a full renaming model. | |
468 | ||
851e9f19 PMR |
469 | function Entity_Of_Unit (U : Node_Id) return Entity_Id; |
470 | -- Return the appropriate entity for determining which unit has a deeper | |
471 | -- scope: the defining entity for U, unless U is a package instance, in | |
472 | -- which case we retrieve the entity of the instance spec. | |
9bc856dd | 473 | |
fbf5a39b | 474 | procedure Find_Expanded_Name (N : Node_Id); |
1d801f21 | 475 | -- The input is a selected component known to be an expanded name. Verify |
30196a76 RD |
476 | -- legality of selector given the scope denoted by prefix, and change node |
477 | -- N into a expanded name with a properly set Entity field. | |
fbf5a39b | 478 | |
851e9f19 PMR |
479 | function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id; |
480 | -- Find the most previous use clause (that is, the first one to appear in | |
481 | -- the source) by traversing the previous clause chain that exists in both | |
482 | -- N_Use_Package_Clause nodes and N_Use_Type_Clause nodes. | |
7f5e671b | 483 | -- ??? a better subprogram name is in order |
851e9f19 | 484 | |
996ae0b0 RK |
485 | function Find_Renamed_Entity |
486 | (N : Node_Id; | |
487 | Nam : Node_Id; | |
488 | New_S : Entity_Id; | |
489 | Is_Actual : Boolean := False) return Entity_Id; | |
490 | -- Find the renamed entity that corresponds to the given parameter profile | |
491 | -- in a subprogram renaming declaration. The renamed entity may be an | |
492 | -- operator, a subprogram, an entry, or a protected operation. Is_Actual | |
493 | -- indicates that the renaming is the one generated for an actual subpro- | |
494 | -- gram in an instance, for which special visibility checks apply. | |
495 | ||
851e9f19 PMR |
496 | function Has_Implicit_Character_Literal (N : Node_Id) return Boolean; |
497 | -- Find a type derived from Character or Wide_Character in the prefix of N. | |
498 | -- Used to resolved qualified names whose selector is a character literal. | |
499 | ||
500 | function Has_Private_With (E : Entity_Id) return Boolean; | |
501 | -- Ada 2005 (AI-262): Determines if the current compilation unit has a | |
502 | -- private with on E. | |
503 | ||
fbf5a39b | 504 | function Has_Implicit_Operator (N : Node_Id) return Boolean; |
f3d57416 | 505 | -- N is an expanded name whose selector is an operator name (e.g. P."+"). |
16ca248a ES |
506 | -- declarative part contains an implicit declaration of an operator if it |
507 | -- has a declaration of a type to which one of the predefined operators | |
508 | -- apply. The existence of this routine is an implementation artifact. A | |
509 | -- more straightforward but more space-consuming choice would be to make | |
510 | -- all inherited operators explicit in the symbol table. | |
fbf5a39b | 511 | |
996ae0b0 RK |
512 | procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id); |
513 | -- A subprogram defined by a renaming declaration inherits the parameter | |
514 | -- profile of the renamed entity. The subtypes given in the subprogram | |
515 | -- specification are discarded and replaced with those of the renamed | |
516 | -- subprogram, which are then used to recheck the default values. | |
517 | ||
fbf5a39b | 518 | function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean; |
16ca248a ES |
519 | -- True if it is of a task type, a protected type, or else an access to one |
520 | -- of these types. | |
fbf5a39b | 521 | |
851e9f19 PMR |
522 | function Is_Appropriate_For_Record (T : Entity_Id) return Boolean; |
523 | -- Prefix is appropriate for record if it is of a record type, or an access | |
524 | -- to such. | |
525 | ||
526 | function Most_Descendant_Use_Clause | |
527 | (Clause1 : Entity_Id; | |
528 | Clause2 : Entity_Id) return Entity_Id; | |
529 | -- Determine which use clause parameter is the most descendant in terms of | |
530 | -- scope. | |
7f5e671b | 531 | -- ??? a better subprogram name is in order |
d4810530 | 532 | |
996ae0b0 | 533 | procedure Premature_Usage (N : Node_Id); |
bc41faa2 | 534 | -- Diagnose usage of an entity before it is visible |
996ae0b0 | 535 | |
851e9f19 | 536 | procedure Use_One_Package |
7f5e671b PMR |
537 | (N : Node_Id; |
538 | Pack_Name : Entity_Id := Empty; | |
539 | Force : Boolean := False); | |
fbf5a39b AC |
540 | -- Make visible entities declared in package P potentially use-visible |
541 | -- in the current context. Also used in the analysis of subunits, when | |
542 | -- re-installing use clauses of parent units. N is the use_clause that | |
543 | -- names P (and possibly other packages). | |
544 | ||
851e9f19 | 545 | procedure Use_One_Type |
7f5e671b PMR |
546 | (Id : Node_Id; |
547 | Installed : Boolean := False; | |
548 | Force : Boolean := False); | |
851e9f19 | 549 | -- Id is the subtype mark from a use_type_clause. This procedure makes |
7ff2d234 AC |
550 | -- the primitive operators of the type potentially use-visible. The |
551 | -- boolean flag Installed indicates that the clause is being reinstalled | |
552 | -- after previous analysis, and primitive operations are already chained | |
553 | -- on the Used_Operations list of the clause. | |
fbf5a39b | 554 | |
996ae0b0 RK |
555 | procedure Write_Info; |
556 | -- Write debugging information on entities declared in current scope | |
557 | ||
996ae0b0 RK |
558 | -------------------------------- |
559 | -- Analyze_Exception_Renaming -- | |
560 | -------------------------------- | |
561 | ||
16ca248a ES |
562 | -- The language only allows a single identifier, but the tree holds an |
563 | -- identifier list. The parser has already issued an error message if | |
564 | -- there is more than one element in the list. | |
996ae0b0 RK |
565 | |
566 | procedure Analyze_Exception_Renaming (N : Node_Id) is | |
1af4455a HK |
567 | Id : constant Entity_Id := Defining_Entity (N); |
568 | Nam : constant Node_Id := Name (N); | |
996ae0b0 RK |
569 | |
570 | begin | |
ce5ba43a | 571 | Check_SPARK_05_Restriction ("exception renaming is not allowed", N); |
1d801f21 | 572 | |
996ae0b0 RK |
573 | Enter_Name (Id); |
574 | Analyze (Nam); | |
575 | ||
241ebe89 HK |
576 | Set_Ekind (Id, E_Exception); |
577 | Set_Etype (Id, Standard_Exception_Type); | |
578 | Set_Is_Pure (Id, Is_Pure (Current_Scope)); | |
996ae0b0 | 579 | |
241ebe89 HK |
580 | if Is_Entity_Name (Nam) |
581 | and then Present (Entity (Nam)) | |
582 | and then Ekind (Entity (Nam)) = E_Exception | |
996ae0b0 | 583 | then |
996ae0b0 RK |
584 | if Present (Renamed_Object (Entity (Nam))) then |
585 | Set_Renamed_Object (Id, Renamed_Object (Entity (Nam))); | |
586 | else | |
587 | Set_Renamed_Object (Id, Entity (Nam)); | |
588 | end if; | |
c5cec2fe | 589 | |
241ebe89 HK |
590 | -- The exception renaming declaration may become Ghost if it renames |
591 | -- a Ghost entity. | |
c5cec2fe | 592 | |
d65a80fd | 593 | Mark_Ghost_Renaming (N, Entity (Nam)); |
241ebe89 HK |
594 | else |
595 | Error_Msg_N ("invalid exception name in renaming", Nam); | |
996ae0b0 | 596 | end if; |
42f1d661 AC |
597 | |
598 | -- Implementation-defined aspect specifications can appear in a renaming | |
599 | -- declaration, but not language-defined ones. The call to procedure | |
600 | -- Analyze_Aspect_Specifications will take care of this error check. | |
601 | ||
602 | if Has_Aspects (N) then | |
603 | Analyze_Aspect_Specifications (N, Id); | |
604 | end if; | |
996ae0b0 RK |
605 | end Analyze_Exception_Renaming; |
606 | ||
607 | --------------------------- | |
608 | -- Analyze_Expanded_Name -- | |
609 | --------------------------- | |
610 | ||
611 | procedure Analyze_Expanded_Name (N : Node_Id) is | |
612 | begin | |
16ca248a ES |
613 | -- If the entity pointer is already set, this is an internal node, or a |
614 | -- node that is analyzed more than once, after a tree modification. In | |
cdabbb52 HK |
615 | -- such a case there is no resolution to perform, just set the type. In |
616 | -- either case, start by analyzing the prefix. | |
f6f40114 AC |
617 | |
618 | Analyze (Prefix (N)); | |
996ae0b0 RK |
619 | |
620 | if Present (Entity (N)) then | |
621 | if Is_Type (Entity (N)) then | |
622 | Set_Etype (N, Entity (N)); | |
623 | else | |
624 | Set_Etype (N, Etype (Entity (N))); | |
625 | end if; | |
626 | ||
996ae0b0 RK |
627 | else |
628 | Find_Expanded_Name (N); | |
629 | end if; | |
0929eaeb | 630 | |
b5f3c913 AC |
631 | -- In either case, propagate dimension of entity to expanded name |
632 | ||
0929eaeb | 633 | Analyze_Dimension (N); |
996ae0b0 RK |
634 | end Analyze_Expanded_Name; |
635 | ||
15ce9ca2 AC |
636 | --------------------------------------- |
637 | -- Analyze_Generic_Function_Renaming -- | |
638 | --------------------------------------- | |
996ae0b0 RK |
639 | |
640 | procedure Analyze_Generic_Function_Renaming (N : Node_Id) is | |
641 | begin | |
642 | Analyze_Generic_Renaming (N, E_Generic_Function); | |
643 | end Analyze_Generic_Function_Renaming; | |
644 | ||
15ce9ca2 AC |
645 | -------------------------------------- |
646 | -- Analyze_Generic_Package_Renaming -- | |
647 | -------------------------------------- | |
996ae0b0 RK |
648 | |
649 | procedure Analyze_Generic_Package_Renaming (N : Node_Id) is | |
650 | begin | |
e80f0cb0 RD |
651 | -- Test for the Text_IO special unit case here, since we may be renaming |
652 | -- one of the subpackages of Text_IO, then join common routine. | |
996ae0b0 | 653 | |
2bd67690 | 654 | Check_Text_IO_Special_Unit (Name (N)); |
996ae0b0 RK |
655 | |
656 | Analyze_Generic_Renaming (N, E_Generic_Package); | |
657 | end Analyze_Generic_Package_Renaming; | |
658 | ||
15ce9ca2 AC |
659 | ---------------------------------------- |
660 | -- Analyze_Generic_Procedure_Renaming -- | |
661 | ---------------------------------------- | |
996ae0b0 RK |
662 | |
663 | procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is | |
664 | begin | |
665 | Analyze_Generic_Renaming (N, E_Generic_Procedure); | |
666 | end Analyze_Generic_Procedure_Renaming; | |
667 | ||
668 | ------------------------------ | |
669 | -- Analyze_Generic_Renaming -- | |
670 | ------------------------------ | |
671 | ||
672 | procedure Analyze_Generic_Renaming | |
673 | (N : Node_Id; | |
674 | K : Entity_Kind) | |
675 | is | |
1af4455a | 676 | New_P : constant Entity_Id := Defining_Entity (N); |
ad4ba28b | 677 | Inst : Boolean := False; |
d65a80fd | 678 | Old_P : Entity_Id; |
996ae0b0 RK |
679 | |
680 | begin | |
ed4a1468 RD |
681 | if Name (N) = Error then |
682 | return; | |
683 | end if; | |
684 | ||
ce5ba43a | 685 | Check_SPARK_05_Restriction ("generic renaming is not allowed", N); |
fe5d3068 | 686 | |
996ae0b0 RK |
687 | Generate_Definition (New_P); |
688 | ||
689 | if Current_Scope /= Standard_Standard then | |
690 | Set_Is_Pure (New_P, Is_Pure (Current_Scope)); | |
691 | end if; | |
692 | ||
693 | if Nkind (Name (N)) = N_Selected_Component then | |
694 | Check_Generic_Child_Unit (Name (N), Inst); | |
695 | else | |
696 | Analyze (Name (N)); | |
697 | end if; | |
698 | ||
699 | if not Is_Entity_Name (Name (N)) then | |
700 | Error_Msg_N ("expect entity name in renaming declaration", Name (N)); | |
701 | Old_P := Any_Id; | |
702 | else | |
703 | Old_P := Entity (Name (N)); | |
704 | end if; | |
705 | ||
706 | Enter_Name (New_P); | |
707 | Set_Ekind (New_P, K); | |
708 | ||
709 | if Etype (Old_P) = Any_Type then | |
710 | null; | |
711 | ||
712 | elsif Ekind (Old_P) /= K then | |
713 | Error_Msg_N ("invalid generic unit name", Name (N)); | |
714 | ||
715 | else | |
716 | if Present (Renamed_Object (Old_P)) then | |
241ebe89 | 717 | Set_Renamed_Object (New_P, Renamed_Object (Old_P)); |
996ae0b0 RK |
718 | else |
719 | Set_Renamed_Object (New_P, Old_P); | |
720 | end if; | |
721 | ||
d65a80fd HK |
722 | -- The generic renaming declaration may become Ghost if it renames a |
723 | -- Ghost entity. | |
724 | ||
725 | Mark_Ghost_Renaming (N, Old_P); | |
726 | ||
923fa078 RD |
727 | Set_Is_Pure (New_P, Is_Pure (Old_P)); |
728 | Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P)); | |
729 | ||
996ae0b0 RK |
730 | Set_Etype (New_P, Etype (Old_P)); |
731 | Set_Has_Completion (New_P); | |
732 | ||
733 | if In_Open_Scopes (Old_P) then | |
734 | Error_Msg_N ("within its scope, generic denotes its instance", N); | |
735 | end if; | |
736 | ||
7b966a95 AC |
737 | -- For subprograms, propagate the Intrinsic flag, to allow, e.g. |
738 | -- renamings and subsequent instantiations of Unchecked_Conversion. | |
739 | ||
740 | if Ekind_In (Old_P, E_Generic_Function, E_Generic_Procedure) then | |
741 | Set_Is_Intrinsic_Subprogram | |
742 | (New_P, Is_Intrinsic_Subprogram (Old_P)); | |
743 | end if; | |
744 | ||
996ae0b0 RK |
745 | Check_Library_Unit_Renaming (N, Old_P); |
746 | end if; | |
42f1d661 AC |
747 | |
748 | -- Implementation-defined aspect specifications can appear in a renaming | |
749 | -- declaration, but not language-defined ones. The call to procedure | |
750 | -- Analyze_Aspect_Specifications will take care of this error check. | |
751 | ||
752 | if Has_Aspects (N) then | |
753 | Analyze_Aspect_Specifications (N, New_P); | |
754 | end if; | |
996ae0b0 RK |
755 | end Analyze_Generic_Renaming; |
756 | ||
757 | ----------------------------- | |
758 | -- Analyze_Object_Renaming -- | |
759 | ----------------------------- | |
760 | ||
761 | procedure Analyze_Object_Renaming (N : Node_Id) is | |
1378bf10 | 762 | Id : constant Entity_Id := Defining_Identifier (N); |
241ebe89 | 763 | Loc : constant Source_Ptr := Sloc (N); |
1378bf10 | 764 | Nam : constant Node_Id := Name (N); |
241ebe89 | 765 | Dec : Node_Id; |
996ae0b0 RK |
766 | T : Entity_Id; |
767 | T2 : Entity_Id; | |
768 | ||
3f5a8fee AC |
769 | procedure Check_Constrained_Object; |
770 | -- If the nominal type is unconstrained but the renamed object is | |
771 | -- constrained, as can happen with renaming an explicit dereference or | |
772 | -- a function return, build a constrained subtype from the object. If | |
773 | -- the renaming is for a formal in an accept statement, the analysis | |
774 | -- has already established its actual subtype. This is only relevant | |
775 | -- if the renamed object is an explicit dereference. | |
776 | ||
bce6accb HK |
777 | function Get_Object_Name (Nod : Node_Id) return Node_Id; |
778 | -- Obtain the name of the object from node Nod which is being renamed by | |
779 | -- the object renaming declaration N. | |
780 | ||
3f5a8fee AC |
781 | ------------------------------ |
782 | -- Check_Constrained_Object -- | |
783 | ------------------------------ | |
784 | ||
785 | procedure Check_Constrained_Object is | |
b887f1a0 | 786 | Typ : constant Entity_Id := Etype (Nam); |
3f5a8fee AC |
787 | Subt : Entity_Id; |
788 | ||
789 | begin | |
e917aec2 | 790 | if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference) |
eee51f3d JS |
791 | and then Is_Composite_Type (Typ) |
792 | and then not Is_Constrained (Typ) | |
793 | and then not Has_Unknown_Discriminants (Typ) | |
3f5a8fee AC |
794 | and then Expander_Active |
795 | then | |
e917aec2 | 796 | -- If Actual_Subtype is already set, nothing to do |
3f5a8fee | 797 | |
e917aec2 | 798 | if Ekind_In (Id, E_Variable, E_Constant) |
3f5a8fee AC |
799 | and then Present (Actual_Subtype (Id)) |
800 | then | |
801 | null; | |
802 | ||
42f1d661 | 803 | -- A renaming of an unchecked union has no actual subtype |
14848f57 | 804 | |
b887f1a0 | 805 | elsif Is_Unchecked_Union (Typ) then |
14848f57 AC |
806 | null; |
807 | ||
2780d174 | 808 | -- If a record is limited its size is invariant. This is the case |
c14dc27e | 809 | -- in particular with record types with an access discriminant |
2780d174 AC |
810 | -- that are used in iterators. This is an optimization, but it |
811 | -- also prevents typing anomalies when the prefix is further | |
e7f4682a | 812 | -- expanded. |
c14dc27e ES |
813 | -- Note that we cannot just use the Is_Limited_Record flag because |
814 | -- it does not apply to records with limited components, for which | |
815 | -- this syntactic flag is not set, but whose size is also fixed. | |
816 | ||
e7f4682a | 817 | elsif Is_Limited_Type (Typ) then |
2780d174 AC |
818 | null; |
819 | ||
3f5a8fee AC |
820 | else |
821 | Subt := Make_Temporary (Loc, 'T'); | |
822 | Remove_Side_Effects (Nam); | |
823 | Insert_Action (N, | |
824 | Make_Subtype_Declaration (Loc, | |
825 | Defining_Identifier => Subt, | |
826 | Subtype_Indication => | |
b887f1a0 | 827 | Make_Subtype_From_Expr (Nam, Typ))); |
3f5a8fee AC |
828 | Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc)); |
829 | Set_Etype (Nam, Subt); | |
303fbb20 AC |
830 | |
831 | -- Freeze subtype at once, to prevent order of elaboration | |
832 | -- issues in the backend. The renamed object exists, so its | |
833 | -- type is already frozen in any case. | |
834 | ||
835 | Freeze_Before (N, Subt); | |
3f5a8fee AC |
836 | end if; |
837 | end if; | |
838 | end Check_Constrained_Object; | |
839 | ||
bce6accb HK |
840 | --------------------- |
841 | -- Get_Object_Name -- | |
842 | --------------------- | |
843 | ||
844 | function Get_Object_Name (Nod : Node_Id) return Node_Id is | |
845 | Obj_Nam : Node_Id; | |
846 | ||
847 | begin | |
848 | Obj_Nam := Nod; | |
849 | while Present (Obj_Nam) loop | |
850 | if Nkind_In (Obj_Nam, N_Attribute_Reference, | |
851 | N_Explicit_Dereference, | |
852 | N_Indexed_Component, | |
853 | N_Slice) | |
854 | then | |
855 | Obj_Nam := Prefix (Obj_Nam); | |
856 | ||
857 | elsif Nkind (Obj_Nam) = N_Selected_Component then | |
858 | Obj_Nam := Selector_Name (Obj_Nam); | |
859 | else | |
860 | exit; | |
861 | end if; | |
862 | end loop; | |
863 | ||
864 | return Obj_Nam; | |
865 | end Get_Object_Name; | |
866 | ||
923fa078 RD |
867 | -- Start of processing for Analyze_Object_Renaming |
868 | ||
996ae0b0 | 869 | begin |
ed4a1468 RD |
870 | if Nam = Error then |
871 | return; | |
872 | end if; | |
873 | ||
ce5ba43a | 874 | Check_SPARK_05_Restriction ("object renaming is not allowed", N); |
fe5d3068 | 875 | |
996ae0b0 RK |
876 | Set_Is_Pure (Id, Is_Pure (Current_Scope)); |
877 | Enter_Name (Id); | |
878 | ||
16ca248a ES |
879 | -- The renaming of a component that depends on a discriminant requires |
880 | -- an actual subtype, because in subsequent use of the object Gigi will | |
881 | -- be unable to locate the actual bounds. This explicit step is required | |
882 | -- when the renaming is generated in removing side effects of an | |
883 | -- already-analyzed expression. | |
996ae0b0 | 884 | |
42f1d661 | 885 | if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then |
d65a80fd HK |
886 | |
887 | -- The object renaming declaration may become Ghost if it renames a | |
888 | -- Ghost entity. | |
889 | ||
890 | if Is_Entity_Name (Nam) then | |
891 | Mark_Ghost_Renaming (N, Entity (Nam)); | |
892 | end if; | |
893 | ||
894 | T := Etype (Nam); | |
c5cec2fe | 895 | Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam); |
996ae0b0 RK |
896 | |
897 | if Present (Dec) then | |
898 | Insert_Action (N, Dec); | |
899 | T := Defining_Identifier (Dec); | |
900 | Set_Etype (Nam, T); | |
901 | end if; | |
902 | ||
11560bcc | 903 | -- Complete analysis of the subtype mark in any case, for ASIS use |
fbe627af RD |
904 | |
905 | if Present (Subtype_Mark (N)) then | |
906 | Find_Type (Subtype_Mark (N)); | |
907 | end if; | |
908 | ||
6e937c1c AC |
909 | elsif Present (Subtype_Mark (N)) then |
910 | Find_Type (Subtype_Mark (N)); | |
911 | T := Entity (Subtype_Mark (N)); | |
fbe627af RD |
912 | Analyze (Nam); |
913 | ||
d65a80fd HK |
914 | -- The object renaming declaration may become Ghost if it renames a |
915 | -- Ghost entity. | |
916 | ||
917 | if Is_Entity_Name (Nam) then | |
918 | Mark_Ghost_Renaming (N, Entity (Nam)); | |
919 | end if; | |
920 | ||
6cce2156 GD |
921 | -- Reject renamings of conversions unless the type is tagged, or |
922 | -- the conversion is implicit (which can occur for cases of anonymous | |
923 | -- access types in Ada 2012). | |
924 | ||
fbe627af | 925 | if Nkind (Nam) = N_Type_Conversion |
6cce2156 GD |
926 | and then Comes_From_Source (Nam) |
927 | and then not Is_Tagged_Type (T) | |
fbe627af RD |
928 | then |
929 | Error_Msg_N | |
930 | ("renaming of conversion only allowed for tagged types", Nam); | |
931 | end if; | |
932 | ||
933 | Resolve (Nam, T); | |
6e937c1c | 934 | |
1378bf10 AC |
935 | -- If the renamed object is a function call of a limited type, |
936 | -- the expansion of the renaming is complicated by the presence | |
937 | -- of various temporaries and subtypes that capture constraints | |
938 | -- of the renamed object. Rewrite node as an object declaration, | |
939 | -- whose expansion is simpler. Given that the object is limited | |
940 | -- there is no copy involved and no performance hit. | |
941 | ||
942 | if Nkind (Nam) = N_Function_Call | |
51245e2d | 943 | and then Is_Limited_View (Etype (Nam)) |
92db5dee | 944 | and then not Is_Constrained (Etype (Nam)) |
1378bf10 AC |
945 | and then Comes_From_Source (N) |
946 | then | |
947 | Set_Etype (Id, T); | |
948 | Set_Ekind (Id, E_Constant); | |
949 | Rewrite (N, | |
950 | Make_Object_Declaration (Loc, | |
951 | Defining_Identifier => Id, | |
952 | Constant_Present => True, | |
92db5dee | 953 | Object_Definition => New_Occurrence_Of (Etype (Nam), Loc), |
1378bf10 AC |
954 | Expression => Relocate_Node (Nam))); |
955 | return; | |
956 | end if; | |
957 | ||
6cce2156 GD |
958 | -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object |
959 | -- when renaming declaration has a named access type. The Ada 2012 | |
960 | -- coverage rules allow an anonymous access type in the context of | |
961 | -- an expected named general access type, but the renaming rules | |
962 | -- require the types to be the same. (An exception is when the type | |
963 | -- of the renaming is also an anonymous access type, which can only | |
964 | -- happen due to a renaming created by the expander.) | |
965 | ||
966 | if Nkind (Nam) = N_Type_Conversion | |
967 | and then not Comes_From_Source (Nam) | |
968 | and then Ekind (Etype (Expression (Nam))) = E_Anonymous_Access_Type | |
969 | and then Ekind (T) /= E_Anonymous_Access_Type | |
970 | then | |
971 | Wrong_Type (Expression (Nam), T); -- Should we give better error??? | |
972 | end if; | |
973 | ||
7d823354 ES |
974 | -- Check that a class-wide object is not being renamed as an object |
975 | -- of a specific type. The test for access types is needed to exclude | |
976 | -- cases where the renamed object is a dynamically tagged access | |
977 | -- result, such as occurs in certain expansions. | |
978 | ||
4755cce9 JM |
979 | if Is_Tagged_Type (T) then |
980 | Check_Dynamically_Tagged_Expression | |
981 | (Expr => Nam, | |
982 | Typ => T, | |
983 | Related_Nod => N); | |
7d823354 ES |
984 | end if; |
985 | ||
0ab80019 | 986 | -- Ada 2005 (AI-230/AI-254): Access renaming |
6e937c1c | 987 | |
9bc856dd | 988 | else pragma Assert (Present (Access_Definition (N))); |
d65a80fd HK |
989 | T := |
990 | Access_Definition | |
991 | (Related_Nod => N, | |
992 | N => Access_Definition (N)); | |
35b7fa6a | 993 | |
f16d05d9 AC |
994 | Analyze (Nam); |
995 | ||
d65a80fd HK |
996 | -- The object renaming declaration may become Ghost if it renames a |
997 | -- Ghost entity. | |
998 | ||
999 | if Is_Entity_Name (Nam) then | |
1000 | Mark_Ghost_Renaming (N, Entity (Nam)); | |
1001 | end if; | |
1002 | ||
f16d05d9 AC |
1003 | -- Ada 2005 AI05-105: if the declaration has an anonymous access |
1004 | -- type, the renamed object must also have an anonymous type, and | |
92db5dee AC |
1005 | -- this is a name resolution rule. This was implicit in the last part |
1006 | -- of the first sentence in 8.5.1(3/2), and is made explicit by this | |
1007 | -- recent AI. | |
f16d05d9 AC |
1008 | |
1009 | if not Is_Overloaded (Nam) then | |
1010 | if Ekind (Etype (Nam)) /= Ekind (T) then | |
1011 | Error_Msg_N | |
a2dc5812 | 1012 | ("expect anonymous access type in object renaming", N); |
f16d05d9 | 1013 | end if; |
3c19e9be | 1014 | |
f16d05d9 AC |
1015 | else |
1016 | declare | |
3c19e9be ES |
1017 | I : Interp_Index; |
1018 | It : Interp; | |
1019 | Typ : Entity_Id := Empty; | |
e34ca162 | 1020 | Seen : Boolean := False; |
f16d05d9 AC |
1021 | |
1022 | begin | |
1023 | Get_First_Interp (Nam, I, It); | |
1024 | while Present (It.Typ) loop | |
3c19e9be ES |
1025 | |
1026 | -- Renaming is ambiguous if more than one candidate | |
1027 | -- interpretation is type-conformant with the context. | |
1028 | ||
1029 | if Ekind (It.Typ) = Ekind (T) then | |
1030 | if Ekind (T) = E_Anonymous_Access_Subprogram_Type | |
e34ca162 AC |
1031 | and then |
1032 | Type_Conformant | |
1033 | (Designated_Type (T), Designated_Type (It.Typ)) | |
f16d05d9 | 1034 | then |
3c19e9be ES |
1035 | if not Seen then |
1036 | Seen := True; | |
1037 | else | |
1038 | Error_Msg_N | |
1039 | ("ambiguous expression in renaming", Nam); | |
1040 | end if; | |
1041 | ||
1042 | elsif Ekind (T) = E_Anonymous_Access_Type | |
e34ca162 AC |
1043 | and then |
1044 | Covers (Designated_Type (T), Designated_Type (It.Typ)) | |
3c19e9be ES |
1045 | then |
1046 | if not Seen then | |
1047 | Seen := True; | |
1048 | else | |
1049 | Error_Msg_N | |
1050 | ("ambiguous expression in renaming", Nam); | |
1051 | end if; | |
1052 | end if; | |
1053 | ||
1054 | if Covers (T, It.Typ) then | |
f16d05d9 AC |
1055 | Typ := It.Typ; |
1056 | Set_Etype (Nam, Typ); | |
1057 | Set_Is_Overloaded (Nam, False); | |
1058 | end if; | |
f16d05d9 AC |
1059 | end if; |
1060 | ||
1061 | Get_Next_Interp (I, It); | |
1062 | end loop; | |
1063 | end; | |
1064 | end if; | |
1065 | ||
1066 | Resolve (Nam, T); | |
6e937c1c | 1067 | |
405b907c AC |
1068 | -- Do not perform the legality checks below when the resolution of |
1069 | -- the renaming name failed because the associated type is Any_Type. | |
1070 | ||
1071 | if Etype (Nam) = Any_Type then | |
1072 | null; | |
1073 | ||
d18bbd25 | 1074 | -- Ada 2005 (AI-231): In the case where the type is defined by an |
0fb2ea01 AC |
1075 | -- access_definition, the renamed entity shall be of an access-to- |
1076 | -- constant type if and only if the access_definition defines an | |
d18bbd25 | 1077 | -- access-to-constant type. ARM 8.5.1(4) |
35b7fa6a | 1078 | |
405b907c | 1079 | elsif Constant_Present (Access_Definition (N)) |
35b7fa6a AC |
1080 | and then not Is_Access_Constant (Etype (Nam)) |
1081 | then | |
405b907c | 1082 | Error_Msg_N |
d1aea5dd AC |
1083 | ("(Ada 2005): the renamed object is not access-to-constant " |
1084 | & "(RM 8.5.1(6))", N); | |
a2dc5812 AC |
1085 | |
1086 | elsif not Constant_Present (Access_Definition (N)) | |
1087 | and then Is_Access_Constant (Etype (Nam)) | |
1088 | then | |
405b907c AC |
1089 | Error_Msg_N |
1090 | ("(Ada 2005): the renamed object is not access-to-variable " | |
1091 | & "(RM 8.5.1(6))", N); | |
a2dc5812 AC |
1092 | end if; |
1093 | ||
1094 | if Is_Access_Subprogram_Type (Etype (Nam)) then | |
1095 | Check_Subtype_Conformant | |
1096 | (Designated_Type (T), Designated_Type (Etype (Nam))); | |
1097 | ||
1098 | elsif not Subtypes_Statically_Match | |
df3e68b1 HK |
1099 | (Designated_Type (T), |
1100 | Available_View (Designated_Type (Etype (Nam)))) | |
a2dc5812 AC |
1101 | then |
1102 | Error_Msg_N | |
1103 | ("subtype of renamed object does not statically match", N); | |
35b7fa6a | 1104 | end if; |
996ae0b0 RK |
1105 | end if; |
1106 | ||
426d2717 AC |
1107 | -- Special processing for renaming function return object. Some errors |
1108 | -- and warnings are produced only for calls that come from source. | |
fbe627af | 1109 | |
426d2717 | 1110 | if Nkind (Nam) = N_Function_Call then |
fbe627af RD |
1111 | case Ada_Version is |
1112 | ||
c05ba1f1 AC |
1113 | -- Usage is illegal in Ada 83, but renamings are also introduced |
1114 | -- during expansion, and error does not apply to those. | |
fbe627af RD |
1115 | |
1116 | when Ada_83 => | |
c05ba1f1 | 1117 | if Comes_From_Source (N) then |
426d2717 AC |
1118 | Error_Msg_N |
1119 | ("(Ada 83) cannot rename function return object", Nam); | |
1120 | end if; | |
fbe627af RD |
1121 | |
1122 | -- In Ada 95, warn for odd case of renaming parameterless function | |
426d2717 | 1123 | -- call if this is not a limited type (where this is useful). |
fbe627af RD |
1124 | |
1125 | when others => | |
1126 | if Warn_On_Object_Renames_Function | |
1127 | and then No (Parameter_Associations (Nam)) | |
1128 | and then not Is_Limited_Type (Etype (Nam)) | |
426d2717 | 1129 | and then Comes_From_Source (Nam) |
fbe627af RD |
1130 | then |
1131 | Error_Msg_N | |
dbfeb4fa | 1132 | ("renaming function result object is suspicious?R?", Nam); |
fbe627af | 1133 | Error_Msg_NE |
dbfeb4fa | 1134 | ("\function & will be called only once?R?", Nam, |
426d2717 | 1135 | Entity (Name (Nam))); |
ed2233dc | 1136 | Error_Msg_N -- CODEFIX |
cc0b3bac AC |
1137 | ("\suggest using an initialized constant object " |
1138 | & "instead?R?", Nam); | |
fbe627af RD |
1139 | end if; |
1140 | end case; | |
1141 | end if; | |
1142 | ||
3f5a8fee AC |
1143 | Check_Constrained_Object; |
1144 | ||
16ca248a ES |
1145 | -- An object renaming requires an exact match of the type. Class-wide |
1146 | -- matching is not allowed. | |
996ae0b0 RK |
1147 | |
1148 | if Is_Class_Wide_Type (T) | |
1149 | and then Base_Type (Etype (Nam)) /= Base_Type (T) | |
1150 | then | |
1151 | Wrong_Type (Nam, T); | |
1152 | end if; | |
1153 | ||
eee51f3d JS |
1154 | -- We must search for an actual subtype here so that the bounds of |
1155 | -- objects of unconstrained types don't get dropped on the floor - such | |
1156 | -- as with renamings of formal parameters. | |
1157 | ||
1158 | T2 := Get_Actual_Subtype_If_Available (Nam); | |
4de287c4 | 1159 | |
92db5dee | 1160 | -- Ada 2005 (AI-326): Handle wrong use of incomplete type |
4de287c4 ES |
1161 | |
1162 | if Nkind (Nam) = N_Explicit_Dereference | |
1163 | and then Ekind (Etype (T2)) = E_Incomplete_Type | |
1164 | then | |
3c829e3c ES |
1165 | Error_Msg_NE ("invalid use of incomplete type&", Id, T2); |
1166 | return; | |
426d2717 | 1167 | |
3c829e3c ES |
1168 | elsif Ekind (Etype (T)) = E_Incomplete_Type then |
1169 | Error_Msg_NE ("invalid use of incomplete type&", Id, T); | |
4de287c4 ES |
1170 | return; |
1171 | end if; | |
1172 | ||
923fa078 RD |
1173 | -- Ada 2005 (AI-327) |
1174 | ||
0791fbe9 | 1175 | if Ada_Version >= Ada_2005 |
923fa078 RD |
1176 | and then Nkind (Nam) = N_Attribute_Reference |
1177 | and then Attribute_Name (Nam) = Name_Priority | |
1178 | then | |
1179 | null; | |
1180 | ||
ac7d724d | 1181 | elsif Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then |
923fa078 | 1182 | declare |
bce6accb HK |
1183 | Nam_Ent : constant Entity_Id := Entity (Get_Object_Name (Nam)); |
1184 | Nam_Decl : constant Node_Id := Declaration_Node (Nam_Ent); | |
923fa078 RD |
1185 | |
1186 | begin | |
923fa078 RD |
1187 | if Has_Null_Exclusion (N) |
1188 | and then not Has_Null_Exclusion (Nam_Decl) | |
1189 | then | |
1190 | -- Ada 2005 (AI-423): If the object name denotes a generic | |
1191 | -- formal object of a generic unit G, and the object renaming | |
1192 | -- declaration occurs within the body of G or within the body | |
1193 | -- of a generic unit declared within the declarative region | |
fbe627af | 1194 | -- of G, then the declaration of the formal object of G must |
a3c39f83 | 1195 | -- have a null exclusion or a null-excluding subtype. |
923fa078 RD |
1196 | |
1197 | if Is_Formal_Object (Nam_Ent) | |
ac7d724d | 1198 | and then In_Generic_Scope (Id) |
923fa078 | 1199 | then |
a3c39f83 ES |
1200 | if not Can_Never_Be_Null (Etype (Nam_Ent)) then |
1201 | Error_Msg_N | |
1202 | ("renamed formal does not exclude `NULL` " | |
1203 | & "(RM 8.5.1(4.6/2))", N); | |
1204 | ||
1205 | elsif In_Package_Body (Scope (Id)) then | |
1206 | Error_Msg_N | |
1207 | ("formal object does not have a null exclusion" | |
1208 | & "(RM 8.5.1(4.6/2))", N); | |
1209 | end if; | |
923fa078 | 1210 | |
16ca248a ES |
1211 | -- Ada 2005 (AI-423): Otherwise, the subtype of the object name |
1212 | -- shall exclude null. | |
923fa078 | 1213 | |
21d27997 | 1214 | elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then |
fbe627af | 1215 | Error_Msg_N |
21d27997 RD |
1216 | ("renamed object does not exclude `NULL` " |
1217 | & "(RM 8.5.1(4.6/2))", N); | |
aecf0203 | 1218 | |
a3c39f83 ES |
1219 | -- An instance is illegal if it contains a renaming that |
1220 | -- excludes null, and the actual does not. The renaming | |
1221 | -- declaration has already indicated that the declaration | |
1222 | -- of the renamed actual in the instance will raise | |
1223 | -- constraint_error. | |
1224 | ||
426d2717 | 1225 | elsif Nkind (Nam_Decl) = N_Object_Declaration |
a3c39f83 | 1226 | and then In_Instance |
ac7d724d ES |
1227 | and then |
1228 | Present (Corresponding_Generic_Association (Nam_Decl)) | |
1229 | and then Nkind (Expression (Nam_Decl)) = | |
1230 | N_Raise_Constraint_Error | |
a3c39f83 ES |
1231 | then |
1232 | Error_Msg_N | |
1233 | ("renamed actual does not exclude `NULL` " | |
1234 | & "(RM 8.5.1(4.6/2))", N); | |
1235 | ||
1236 | -- Finally, if there is a null exclusion, the subtype mark | |
1237 | -- must not be null-excluding. | |
1238 | ||
1239 | elsif No (Access_Definition (N)) | |
426d2717 | 1240 | and then Can_Never_Be_Null (T) |
a3c39f83 | 1241 | then |
aecf0203 | 1242 | Error_Msg_NE |
a3c39f83 ES |
1243 | ("`NOT NULL` not allowed (& already excludes null)", |
1244 | N, T); | |
fa961f76 | 1245 | |
923fa078 | 1246 | end if; |
fa961f76 | 1247 | |
a3c39f83 ES |
1248 | elsif Can_Never_Be_Null (T) |
1249 | and then not Can_Never_Be_Null (Etype (Nam_Ent)) | |
1250 | then | |
1251 | Error_Msg_N | |
1252 | ("renamed object does not exclude `NULL` " | |
1253 | & "(RM 8.5.1(4.6/2))", N); | |
1254 | ||
fa961f76 ES |
1255 | elsif Has_Null_Exclusion (N) |
1256 | and then No (Access_Definition (N)) | |
1257 | and then Can_Never_Be_Null (T) | |
1258 | then | |
1259 | Error_Msg_NE | |
1260 | ("`NOT NULL` not allowed (& already excludes null)", N, T); | |
923fa078 RD |
1261 | end if; |
1262 | end; | |
1263 | end if; | |
1264 | ||
2db5b47e AC |
1265 | -- Set the Ekind of the entity, unless it has been set already, as is |
1266 | -- the case for the iteration object over a container with no variable | |
3e247e58 RD |
1267 | -- indexing. In that case it's been marked as a constant, and we do not |
1268 | -- want to change it to a variable. | |
2db5b47e AC |
1269 | |
1270 | if Ekind (Id) /= E_Constant then | |
1271 | Set_Ekind (Id, E_Variable); | |
1272 | end if; | |
996c8821 RD |
1273 | |
1274 | -- Initialize the object size and alignment. Note that we used to call | |
1275 | -- Init_Size_Align here, but that's wrong for objects which have only | |
a90bd866 | 1276 | -- an Esize, not an RM_Size field. |
996c8821 | 1277 | |
d15f9422 | 1278 | Init_Object_Size_Align (Id); |
996ae0b0 RK |
1279 | |
1280 | if T = Any_Type or else Etype (Nam) = Any_Type then | |
1281 | return; | |
1282 | ||
923fa078 RD |
1283 | -- Verify that the renamed entity is an object or a function call. It |
1284 | -- may have been rewritten in several ways. | |
996ae0b0 RK |
1285 | |
1286 | elsif Is_Object_Reference (Nam) then | |
124092ee AC |
1287 | if Comes_From_Source (N) then |
1288 | if Is_Dependent_Component_Of_Mutable_Object (Nam) then | |
1289 | Error_Msg_N | |
1290 | ("illegal renaming of discriminant-dependent component", Nam); | |
1291 | end if; | |
1292 | ||
1293 | -- If the renaming comes from source and the renamed object is a | |
1294 | -- dereference, then mark the prefix as needing debug information, | |
1295 | -- since it might have been rewritten hence internally generated | |
1296 | -- and Debug_Renaming_Declaration will link the renaming to it. | |
1297 | ||
1298 | if Nkind (Nam) = N_Explicit_Dereference | |
1299 | and then Is_Entity_Name (Prefix (Nam)) | |
1300 | then | |
1301 | Set_Debug_Info_Needed (Entity (Prefix (Nam))); | |
1302 | end if; | |
996ae0b0 RK |
1303 | end if; |
1304 | ||
1305 | -- A static function call may have been folded into a literal | |
1306 | ||
1307 | elsif Nkind (Original_Node (Nam)) = N_Function_Call | |
1308 | ||
28e18b4f AC |
1309 | -- When expansion is disabled, attribute reference is not rewritten |
1310 | -- as function call. Otherwise it may be rewritten as a conversion, | |
1311 | -- so check original node. | |
996ae0b0 RK |
1312 | |
1313 | or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference | |
1314 | and then Is_Function_Attribute_Name | |
294ccb21 | 1315 | (Attribute_Name (Original_Node (Nam)))) |
996ae0b0 | 1316 | |
28e18b4f AC |
1317 | -- Weird but legal, equivalent to renaming a function call. Illegal |
1318 | -- if the literal is the result of constant-folding an attribute | |
1319 | -- reference that is not a function. | |
996ae0b0 RK |
1320 | |
1321 | or else (Is_Entity_Name (Nam) | |
d4810530 ES |
1322 | and then Ekind (Entity (Nam)) = E_Enumeration_Literal |
1323 | and then | |
1324 | Nkind (Original_Node (Nam)) /= N_Attribute_Reference) | |
996ae0b0 RK |
1325 | |
1326 | or else (Nkind (Nam) = N_Type_Conversion | |
ac7d724d | 1327 | and then Is_Tagged_Type (Entity (Subtype_Mark (Nam)))) |
996ae0b0 RK |
1328 | then |
1329 | null; | |
1330 | ||
923fa078 RD |
1331 | elsif Nkind (Nam) = N_Type_Conversion then |
1332 | Error_Msg_N | |
1333 | ("renaming of conversion only allowed for tagged types", Nam); | |
996ae0b0 | 1334 | |
923fa078 RD |
1335 | -- Ada 2005 (AI-327) |
1336 | ||
0791fbe9 | 1337 | elsif Ada_Version >= Ada_2005 |
923fa078 RD |
1338 | and then Nkind (Nam) = N_Attribute_Reference |
1339 | and then Attribute_Name (Nam) = Name_Priority | |
1340 | then | |
1341 | null; | |
1342 | ||
c42e1b17 | 1343 | -- Allow internally generated x'Ref resulting in N_Reference node |
21d27997 RD |
1344 | |
1345 | elsif Nkind (Nam) = N_Reference then | |
1346 | null; | |
1347 | ||
923fa078 RD |
1348 | else |
1349 | Error_Msg_N ("expect object name in renaming", Nam); | |
996ae0b0 RK |
1350 | end if; |
1351 | ||
1352 | Set_Etype (Id, T2); | |
1353 | ||
1354 | if not Is_Variable (Nam) then | |
1355 | Set_Ekind (Id, E_Constant); | |
fbf5a39b | 1356 | Set_Never_Set_In_Source (Id, True); |
996ae0b0 RK |
1357 | Set_Is_True_Constant (Id, True); |
1358 | end if; | |
1359 | ||
28e18b4f AC |
1360 | -- The entity of the renaming declaration needs to reflect whether the |
1361 | -- renamed object is volatile. Is_Volatile is set if the renamed object | |
1362 | -- is volatile in the RM legality sense. | |
1363 | ||
1364 | Set_Is_Volatile (Id, Is_Volatile_Object (Nam)); | |
1365 | ||
c2cfccb1 | 1366 | -- Also copy settings of Atomic/Independent/Volatile_Full_Access |
6333ad3d AC |
1367 | |
1368 | if Is_Entity_Name (Nam) then | |
57abdadd EB |
1369 | Set_Is_Atomic (Id, Is_Atomic (Entity (Nam))); |
1370 | Set_Is_Independent (Id, Is_Independent (Entity (Nam))); | |
1371 | Set_Is_Volatile_Full_Access (Id, | |
1372 | Is_Volatile_Full_Access (Entity (Nam))); | |
6333ad3d AC |
1373 | end if; |
1374 | ||
28e18b4f AC |
1375 | -- Treat as volatile if we just set the Volatile flag |
1376 | ||
1377 | if Is_Volatile (Id) | |
1378 | ||
1379 | -- Or if we are renaming an entity which was marked this way | |
1380 | ||
1381 | -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ??? | |
1382 | ||
1383 | or else (Is_Entity_Name (Nam) | |
1384 | and then Treat_As_Volatile (Entity (Nam))) | |
1385 | then | |
1386 | Set_Treat_As_Volatile (Id, True); | |
1387 | end if; | |
1388 | ||
1389 | -- Now make the link to the renamed object | |
1390 | ||
996ae0b0 | 1391 | Set_Renamed_Object (Id, Nam); |
42f1d661 AC |
1392 | |
1393 | -- Implementation-defined aspect specifications can appear in a renaming | |
1394 | -- declaration, but not language-defined ones. The call to procedure | |
1395 | -- Analyze_Aspect_Specifications will take care of this error check. | |
1396 | ||
1397 | if Has_Aspects (N) then | |
1398 | Analyze_Aspect_Specifications (N, Id); | |
1399 | end if; | |
1400 | ||
1401 | -- Deal with dimensions | |
1402 | ||
dec6faf1 | 1403 | Analyze_Dimension (N); |
996ae0b0 RK |
1404 | end Analyze_Object_Renaming; |
1405 | ||
1406 | ------------------------------ | |
1407 | -- Analyze_Package_Renaming -- | |
1408 | ------------------------------ | |
1409 | ||
1410 | procedure Analyze_Package_Renaming (N : Node_Id) is | |
1411 | New_P : constant Entity_Id := Defining_Entity (N); | |
1412 | Old_P : Entity_Id; | |
1413 | Spec : Node_Id; | |
1414 | ||
1415 | begin | |
ed4a1468 RD |
1416 | if Name (N) = Error then |
1417 | return; | |
1418 | end if; | |
1419 | ||
e80f0cb0 | 1420 | -- Check for Text_IO special unit (we may be renaming a Text_IO child) |
996ae0b0 | 1421 | |
2bd67690 | 1422 | Check_Text_IO_Special_Unit (Name (N)); |
996ae0b0 RK |
1423 | |
1424 | if Current_Scope /= Standard_Standard then | |
1425 | Set_Is_Pure (New_P, Is_Pure (Current_Scope)); | |
1426 | end if; | |
1427 | ||
1428 | Enter_Name (New_P); | |
1429 | Analyze (Name (N)); | |
16ca248a | 1430 | |
996ae0b0 RK |
1431 | if Is_Entity_Name (Name (N)) then |
1432 | Old_P := Entity (Name (N)); | |
1433 | else | |
1434 | Old_P := Any_Id; | |
1435 | end if; | |
1436 | ||
1437 | if Etype (Old_P) = Any_Type then | |
426d2717 | 1438 | Error_Msg_N ("expect package name in renaming", Name (N)); |
657a9dd9 | 1439 | |
996ae0b0 RK |
1440 | elsif Ekind (Old_P) /= E_Package |
1441 | and then not (Ekind (Old_P) = E_Generic_Package | |
1442 | and then In_Open_Scopes (Old_P)) | |
1443 | then | |
1444 | if Ekind (Old_P) = E_Generic_Package then | |
1445 | Error_Msg_N | |
1446 | ("generic package cannot be renamed as a package", Name (N)); | |
1447 | else | |
1448 | Error_Msg_Sloc := Sloc (Old_P); | |
1449 | Error_Msg_NE | |
d65a80fd | 1450 | ("expect package name in renaming, found& declared#", |
996ae0b0 RK |
1451 | Name (N), Old_P); |
1452 | end if; | |
1453 | ||
bc41faa2 | 1454 | -- Set basic attributes to minimize cascaded errors |
996ae0b0 RK |
1455 | |
1456 | Set_Ekind (New_P, E_Package); | |
1457 | Set_Etype (New_P, Standard_Void_Type); | |
1458 | ||
294ccb21 RD |
1459 | -- Here for OK package renaming |
1460 | ||
996ae0b0 | 1461 | else |
923fa078 RD |
1462 | -- Entities in the old package are accessible through the renaming |
1463 | -- entity. The simplest implementation is to have both packages share | |
1464 | -- the entity list. | |
996ae0b0 RK |
1465 | |
1466 | Set_Ekind (New_P, E_Package); | |
1467 | Set_Etype (New_P, Standard_Void_Type); | |
1468 | ||
1469 | if Present (Renamed_Object (Old_P)) then | |
caa64a44 | 1470 | Set_Renamed_Object (New_P, Renamed_Object (Old_P)); |
996ae0b0 | 1471 | else |
4de287c4 | 1472 | Set_Renamed_Object (New_P, Old_P); |
996ae0b0 RK |
1473 | end if; |
1474 | ||
d65a80fd HK |
1475 | -- The package renaming declaration may become Ghost if it renames a |
1476 | -- Ghost entity. | |
1477 | ||
1478 | Mark_Ghost_Renaming (N, Old_P); | |
996ae0b0 | 1479 | |
d65a80fd HK |
1480 | Set_Has_Completion (New_P); |
1481 | Set_First_Entity (New_P, First_Entity (Old_P)); | |
1482 | Set_Last_Entity (New_P, Last_Entity (Old_P)); | |
996ae0b0 RK |
1483 | Set_First_Private_Entity (New_P, First_Private_Entity (Old_P)); |
1484 | Check_Library_Unit_Renaming (N, Old_P); | |
1485 | Generate_Reference (Old_P, Name (N)); | |
1486 | ||
294ccb21 | 1487 | -- If the renaming is in the visible part of a package, then we set |
7d823354 | 1488 | -- Renamed_In_Spec for the renamed package, to prevent giving |
294ccb21 RD |
1489 | -- warnings about no entities referenced. Such a warning would be |
1490 | -- overenthusiastic, since clients can see entities in the renamed | |
1491 | -- package via the visible package renaming. | |
1492 | ||
1493 | declare | |
1494 | Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit); | |
1495 | begin | |
1496 | if Ekind (Ent) = E_Package | |
1497 | and then not In_Private_Part (Ent) | |
1498 | and then In_Extended_Main_Source_Unit (N) | |
1499 | and then Ekind (Old_P) = E_Package | |
1500 | then | |
1501 | Set_Renamed_In_Spec (Old_P); | |
1502 | end if; | |
1503 | end; | |
1504 | ||
996ae0b0 RK |
1505 | -- If this is the renaming declaration of a package instantiation |
1506 | -- within itself, it is the declaration that ends the list of actuals | |
1507 | -- for the instantiation. At this point, the subtypes that rename | |
1508 | -- the actuals are flagged as generic, to avoid spurious ambiguities | |
1509 | -- if the actuals for two distinct formals happen to coincide. If | |
1510 | -- the actual is a private type, the subtype has a private completion | |
1511 | -- that is flagged in the same fashion. | |
1512 | ||
1513 | -- Resolution is identical to what is was in the original generic. | |
1514 | -- On exit from the generic instance, these are turned into regular | |
1515 | -- subtypes again, so they are compatible with types in their class. | |
1516 | ||
1517 | if not Is_Generic_Instance (Old_P) then | |
1518 | return; | |
1519 | else | |
1520 | Spec := Specification (Unit_Declaration_Node (Old_P)); | |
1521 | end if; | |
1522 | ||
1523 | if Nkind (Spec) = N_Package_Specification | |
1524 | and then Present (Generic_Parent (Spec)) | |
1525 | and then Old_P = Current_Scope | |
1526 | and then Chars (New_P) = Chars (Generic_Parent (Spec)) | |
1527 | then | |
1528 | declare | |
16ca248a ES |
1529 | E : Entity_Id; |
1530 | ||
996ae0b0 | 1531 | begin |
16ca248a | 1532 | E := First_Entity (Old_P); |
ac7d724d | 1533 | while Present (E) and then E /= New_P loop |
996ae0b0 RK |
1534 | if Is_Type (E) |
1535 | and then Nkind (Parent (E)) = N_Subtype_Declaration | |
1536 | then | |
1537 | Set_Is_Generic_Actual_Type (E); | |
1538 | ||
1539 | if Is_Private_Type (E) | |
1540 | and then Present (Full_View (E)) | |
1541 | then | |
1542 | Set_Is_Generic_Actual_Type (Full_View (E)); | |
1543 | end if; | |
1544 | end if; | |
1545 | ||
1546 | Next_Entity (E); | |
1547 | end loop; | |
1548 | end; | |
1549 | end if; | |
1550 | end if; | |
42f1d661 AC |
1551 | |
1552 | -- Implementation-defined aspect specifications can appear in a renaming | |
1553 | -- declaration, but not language-defined ones. The call to procedure | |
1554 | -- Analyze_Aspect_Specifications will take care of this error check. | |
1555 | ||
1556 | if Has_Aspects (N) then | |
1557 | Analyze_Aspect_Specifications (N, New_P); | |
1558 | end if; | |
996ae0b0 RK |
1559 | end Analyze_Package_Renaming; |
1560 | ||
1561 | ------------------------------- | |
1562 | -- Analyze_Renamed_Character -- | |
1563 | ------------------------------- | |
1564 | ||
1565 | procedure Analyze_Renamed_Character | |
1566 | (N : Node_Id; | |
1567 | New_S : Entity_Id; | |
1568 | Is_Body : Boolean) | |
1569 | is | |
1570 | C : constant Node_Id := Name (N); | |
1571 | ||
1572 | begin | |
1573 | if Ekind (New_S) = E_Function then | |
1574 | Resolve (C, Etype (New_S)); | |
1575 | ||
1576 | if Is_Body then | |
1577 | Check_Frozen_Renaming (N, New_S); | |
1578 | end if; | |
1579 | ||
1580 | else | |
1581 | Error_Msg_N ("character literal can only be renamed as function", N); | |
1582 | end if; | |
1583 | end Analyze_Renamed_Character; | |
1584 | ||
1585 | --------------------------------- | |
1586 | -- Analyze_Renamed_Dereference -- | |
1587 | --------------------------------- | |
1588 | ||
1589 | procedure Analyze_Renamed_Dereference | |
1590 | (N : Node_Id; | |
1591 | New_S : Entity_Id; | |
1592 | Is_Body : Boolean) | |
1593 | is | |
1594 | Nam : constant Node_Id := Name (N); | |
1595 | P : constant Node_Id := Prefix (Nam); | |
1596 | Typ : Entity_Id; | |
fbf5a39b | 1597 | Ind : Interp_Index; |
996ae0b0 RK |
1598 | It : Interp; |
1599 | ||
1600 | begin | |
1601 | if not Is_Overloaded (P) then | |
996ae0b0 | 1602 | if Ekind (Etype (Nam)) /= E_Subprogram_Type |
bce79204 AC |
1603 | or else not Type_Conformant (Etype (Nam), New_S) |
1604 | then | |
996ae0b0 RK |
1605 | Error_Msg_N ("designated type does not match specification", P); |
1606 | else | |
fbf5a39b | 1607 | Resolve (P); |
996ae0b0 RK |
1608 | end if; |
1609 | ||
1610 | return; | |
1611 | ||
1612 | else | |
1613 | Typ := Any_Type; | |
fbf5a39b | 1614 | Get_First_Interp (Nam, Ind, It); |
996ae0b0 RK |
1615 | |
1616 | while Present (It.Nam) loop | |
1617 | ||
1618 | if Ekind (It.Nam) = E_Subprogram_Type | |
bce79204 AC |
1619 | and then Type_Conformant (It.Nam, New_S) |
1620 | then | |
996ae0b0 RK |
1621 | if Typ /= Any_Id then |
1622 | Error_Msg_N ("ambiguous renaming", P); | |
1623 | return; | |
1624 | else | |
1625 | Typ := It.Nam; | |
1626 | end if; | |
1627 | end if; | |
1628 | ||
fbf5a39b | 1629 | Get_Next_Interp (Ind, It); |
996ae0b0 RK |
1630 | end loop; |
1631 | ||
1632 | if Typ = Any_Type then | |
1633 | Error_Msg_N ("designated type does not match specification", P); | |
1634 | else | |
1635 | Resolve (N, Typ); | |
1636 | ||
1637 | if Is_Body then | |
1638 | Check_Frozen_Renaming (N, New_S); | |
1639 | end if; | |
1640 | end if; | |
1641 | end if; | |
1642 | end Analyze_Renamed_Dereference; | |
1643 | ||
1644 | --------------------------- | |
1645 | -- Analyze_Renamed_Entry -- | |
1646 | --------------------------- | |
1647 | ||
1648 | procedure Analyze_Renamed_Entry | |
1649 | (N : Node_Id; | |
1650 | New_S : Entity_Id; | |
1651 | Is_Body : Boolean) | |
1652 | is | |
c92e8586 AC |
1653 | Nam : constant Node_Id := Name (N); |
1654 | Sel : constant Node_Id := Selector_Name (Nam); | |
1655 | Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N)); | |
1656 | Old_S : Entity_Id; | |
996ae0b0 RK |
1657 | |
1658 | begin | |
1659 | if Entity (Sel) = Any_Id then | |
1660 | ||
bc41faa2 | 1661 | -- Selector is undefined on prefix. Error emitted already |
996ae0b0 RK |
1662 | |
1663 | Set_Has_Completion (New_S); | |
1664 | return; | |
1665 | end if; | |
1666 | ||
16ca248a | 1667 | -- Otherwise find renamed entity and build body of New_S as a call to it |
996ae0b0 RK |
1668 | |
1669 | Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S); | |
1670 | ||
1671 | if Old_S = Any_Id then | |
1672 | Error_Msg_N (" no subprogram or entry matches specification", N); | |
1673 | else | |
1674 | if Is_Body then | |
1675 | Check_Subtype_Conformant (New_S, Old_S, N); | |
1676 | Generate_Reference (New_S, Defining_Entity (N), 'b'); | |
1677 | Style.Check_Identifier (Defining_Entity (N), New_S); | |
725393ea ES |
1678 | |
1679 | else | |
d4810530 | 1680 | -- Only mode conformance required for a renaming_as_declaration |
725393ea ES |
1681 | |
1682 | Check_Mode_Conformant (New_S, Old_S, N); | |
996ae0b0 RK |
1683 | end if; |
1684 | ||
1685 | Inherit_Renamed_Profile (New_S, Old_S); | |
294ccb21 | 1686 | |
c92e8586 AC |
1687 | -- The prefix can be an arbitrary expression that yields a task or |
1688 | -- protected object, so it must be resolved. | |
294ccb21 RD |
1689 | |
1690 | Resolve (Prefix (Nam), Scope (Old_S)); | |
996ae0b0 RK |
1691 | end if; |
1692 | ||
1693 | Set_Convention (New_S, Convention (Old_S)); | |
1694 | Set_Has_Completion (New_S, Inside_A_Generic); | |
1695 | ||
c92e8586 AC |
1696 | -- AI05-0225: If the renamed entity is a procedure or entry of a |
1697 | -- protected object, the target object must be a variable. | |
1698 | ||
1699 | if Ekind (Scope (Old_S)) in Protected_Kind | |
1700 | and then Ekind (New_S) = E_Procedure | |
1701 | and then not Is_Variable (Prefix (Nam)) | |
1702 | then | |
1703 | if Is_Actual then | |
1704 | Error_Msg_N | |
1705 | ("target object of protected operation used as actual for " | |
1706 | & "formal procedure must be a variable", Nam); | |
1707 | else | |
1708 | Error_Msg_N | |
1709 | ("target object of protected operation renamed as procedure, " | |
1710 | & "must be a variable", Nam); | |
1711 | end if; | |
1712 | end if; | |
1713 | ||
996ae0b0 RK |
1714 | if Is_Body then |
1715 | Check_Frozen_Renaming (N, New_S); | |
1716 | end if; | |
1717 | end Analyze_Renamed_Entry; | |
1718 | ||
1719 | ----------------------------------- | |
1720 | -- Analyze_Renamed_Family_Member -- | |
1721 | ----------------------------------- | |
1722 | ||
1723 | procedure Analyze_Renamed_Family_Member | |
1724 | (N : Node_Id; | |
1725 | New_S : Entity_Id; | |
1726 | Is_Body : Boolean) | |
1727 | is | |
fbf5a39b AC |
1728 | Nam : constant Node_Id := Name (N); |
1729 | P : constant Node_Id := Prefix (Nam); | |
996ae0b0 RK |
1730 | Old_S : Entity_Id; |
1731 | ||
1732 | begin | |
1733 | if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family) | |
1734 | or else (Nkind (P) = N_Selected_Component | |
ac7d724d | 1735 | and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family) |
996ae0b0 RK |
1736 | then |
1737 | if Is_Entity_Name (P) then | |
1738 | Old_S := Entity (P); | |
1739 | else | |
1740 | Old_S := Entity (Selector_Name (P)); | |
1741 | end if; | |
1742 | ||
1743 | if not Entity_Matches_Spec (Old_S, New_S) then | |
1744 | Error_Msg_N ("entry family does not match specification", N); | |
1745 | ||
1746 | elsif Is_Body then | |
1747 | Check_Subtype_Conformant (New_S, Old_S, N); | |
1748 | Generate_Reference (New_S, Defining_Entity (N), 'b'); | |
1749 | Style.Check_Identifier (Defining_Entity (N), New_S); | |
1750 | end if; | |
16ca248a | 1751 | |
996ae0b0 RK |
1752 | else |
1753 | Error_Msg_N ("no entry family matches specification", N); | |
1754 | end if; | |
1755 | ||
1756 | Set_Has_Completion (New_S, Inside_A_Generic); | |
1757 | ||
1758 | if Is_Body then | |
1759 | Check_Frozen_Renaming (N, New_S); | |
1760 | end if; | |
1761 | end Analyze_Renamed_Family_Member; | |
1762 | ||
294ccb21 RD |
1763 | ----------------------------------------- |
1764 | -- Analyze_Renamed_Primitive_Operation -- | |
1765 | ----------------------------------------- | |
1766 | ||
1767 | procedure Analyze_Renamed_Primitive_Operation | |
1768 | (N : Node_Id; | |
1769 | New_S : Entity_Id; | |
1770 | Is_Body : Boolean) | |
1771 | is | |
1772 | Old_S : Entity_Id; | |
1773 | ||
1774 | function Conforms | |
1775 | (Subp : Entity_Id; | |
1776 | Ctyp : Conformance_Type) return Boolean; | |
1777 | -- Verify that the signatures of the renamed entity and the new entity | |
1778 | -- match. The first formal of the renamed entity is skipped because it | |
1779 | -- is the target object in any subsequent call. | |
1780 | ||
c5d00db0 AC |
1781 | -------------- |
1782 | -- Conforms -- | |
1783 | -------------- | |
1784 | ||
294ccb21 RD |
1785 | function Conforms |
1786 | (Subp : Entity_Id; | |
1787 | Ctyp : Conformance_Type) return Boolean | |
1788 | is | |
1789 | Old_F : Entity_Id; | |
1790 | New_F : Entity_Id; | |
1791 | ||
1792 | begin | |
1793 | if Ekind (Subp) /= Ekind (New_S) then | |
1794 | return False; | |
1795 | end if; | |
1796 | ||
1797 | Old_F := Next_Formal (First_Formal (Subp)); | |
1798 | New_F := First_Formal (New_S); | |
1799 | while Present (Old_F) and then Present (New_F) loop | |
1800 | if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then | |
1801 | return False; | |
1802 | end if; | |
1803 | ||
1804 | if Ctyp >= Mode_Conformant | |
1805 | and then Ekind (Old_F) /= Ekind (New_F) | |
1806 | then | |
1807 | return False; | |
1808 | end if; | |
1809 | ||
1810 | Next_Formal (New_F); | |
1811 | Next_Formal (Old_F); | |
1812 | end loop; | |
1813 | ||
1814 | return True; | |
1815 | end Conforms; | |
1816 | ||
c5d00db0 AC |
1817 | -- Start of processing for Analyze_Renamed_Primitive_Operation |
1818 | ||
294ccb21 RD |
1819 | begin |
1820 | if not Is_Overloaded (Selector_Name (Name (N))) then | |
1821 | Old_S := Entity (Selector_Name (Name (N))); | |
1822 | ||
1823 | if not Conforms (Old_S, Type_Conformant) then | |
1824 | Old_S := Any_Id; | |
1825 | end if; | |
1826 | ||
1827 | else | |
1828 | -- Find the operation that matches the given signature | |
1829 | ||
1830 | declare | |
1831 | It : Interp; | |
1832 | Ind : Interp_Index; | |
1833 | ||
1834 | begin | |
1835 | Old_S := Any_Id; | |
1836 | Get_First_Interp (Selector_Name (Name (N)), Ind, It); | |
1837 | ||
1838 | while Present (It.Nam) loop | |
1839 | if Conforms (It.Nam, Type_Conformant) then | |
1840 | Old_S := It.Nam; | |
1841 | end if; | |
1842 | ||
1843 | Get_Next_Interp (Ind, It); | |
1844 | end loop; | |
1845 | end; | |
1846 | end if; | |
1847 | ||
1848 | if Old_S = Any_Id then | |
1849 | Error_Msg_N (" no subprogram or entry matches specification", N); | |
1850 | ||
1851 | else | |
1852 | if Is_Body then | |
1853 | if not Conforms (Old_S, Subtype_Conformant) then | |
1854 | Error_Msg_N ("subtype conformance error in renaming", N); | |
1855 | end if; | |
1856 | ||
1857 | Generate_Reference (New_S, Defining_Entity (N), 'b'); | |
1858 | Style.Check_Identifier (Defining_Entity (N), New_S); | |
1859 | ||
1860 | else | |
1861 | -- Only mode conformance required for a renaming_as_declaration | |
1862 | ||
1863 | if not Conforms (Old_S, Mode_Conformant) then | |
1864 | Error_Msg_N ("mode conformance error in renaming", N); | |
1865 | end if; | |
c5d00db0 AC |
1866 | |
1867 | -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed | |
1868 | -- view of a subprogram is intrinsic, because the compiler has | |
1869 | -- to generate a wrapper for any call to it. If the name in a | |
1870 | -- subprogram renaming is a prefixed view, the entity is thus | |
1871 | -- intrinsic, and 'Access cannot be applied to it. | |
1872 | ||
1873 | Set_Convention (New_S, Convention_Intrinsic); | |
294ccb21 RD |
1874 | end if; |
1875 | ||
1876 | -- Inherit_Renamed_Profile (New_S, Old_S); | |
1877 | ||
1878 | -- The prefix can be an arbitrary expression that yields an | |
1879 | -- object, so it must be resolved. | |
1880 | ||
1881 | Resolve (Prefix (Name (N))); | |
1882 | end if; | |
1883 | end Analyze_Renamed_Primitive_Operation; | |
1884 | ||
996ae0b0 RK |
1885 | --------------------------------- |
1886 | -- Analyze_Subprogram_Renaming -- | |
1887 | --------------------------------- | |
1888 | ||
1889 | procedure Analyze_Subprogram_Renaming (N : Node_Id) is | |
fc193526 HK |
1890 | Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N); |
1891 | Is_Actual : constant Boolean := Present (Formal_Spec); | |
0ab80019 | 1892 | Nam : constant Node_Id := Name (N); |
923fa078 | 1893 | Save_AV : constant Ada_Version_Type := Ada_Version; |
fb620b37 | 1894 | Save_AVP : constant Node_Id := Ada_Version_Pragma; |
923fa078 RD |
1895 | Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit; |
1896 | Spec : constant Node_Id := Specification (N); | |
1897 | ||
fc193526 HK |
1898 | Old_S : Entity_Id := Empty; |
1899 | Rename_Spec : Entity_Id; | |
1900 | ||
1901 | procedure Build_Class_Wide_Wrapper | |
1902 | (Ren_Id : out Entity_Id; | |
1903 | Wrap_Id : out Entity_Id); | |
1904 | -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal | |
1905 | -- type with unknown discriminants and a generic primitive operation of | |
1906 | -- the said type with a box require special processing when the actual | |
1907 | -- is a class-wide type: | |
2feb1f84 | 1908 | -- |
fc193526 HK |
1909 | -- generic |
1910 | -- type Formal_Typ (<>) is private; | |
1911 | -- with procedure Prim_Op (Param : Formal_Typ) is <>; | |
1912 | -- package Gen is ... | |
2feb1f84 | 1913 | -- |
fc193526 | 1914 | -- package Inst is new Gen (Actual_Typ'Class); |
2feb1f84 | 1915 | -- |
fc193526 HK |
1916 | -- In this case the general renaming mechanism used in the prologue of |
1917 | -- an instance no longer applies: | |
2feb1f84 | 1918 | -- |
fc193526 | 1919 | -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op; |
2feb1f84 | 1920 | -- |
fc193526 | 1921 | -- The above is replaced the following wrapper/renaming combination: |
2feb1f84 | 1922 | -- |
6d0b56ad | 1923 | -- procedure Wrapper (Param : Formal_Typ) is -- wrapper |
fc193526 HK |
1924 | -- begin |
1925 | -- Prim_Op (Param); -- primitive | |
1926 | -- end Wrapper; | |
2feb1f84 | 1927 | -- |
6d0b56ad | 1928 | -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper; |
2feb1f84 | 1929 | -- |
fc193526 HK |
1930 | -- This transformation applies only if there is no explicit visible |
1931 | -- class-wide operation at the point of the instantiation. Ren_Id is | |
66340e0e AC |
1932 | -- the entity of the renaming declaration. When the transformation |
1933 | -- applies, Wrap_Id is the entity of the generated class-wide wrapper | |
1934 | -- (or Any_Id). Otherwise, Wrap_Id is the entity of the class-wide | |
1935 | -- operation. | |
fc193526 | 1936 | |
923fa078 RD |
1937 | procedure Check_Null_Exclusion |
1938 | (Ren : Entity_Id; | |
1939 | Sub : Entity_Id); | |
1940 | -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the | |
1941 | -- following AI rules: | |
16ca248a ES |
1942 | -- |
1943 | -- If Ren is a renaming of a formal subprogram and one of its | |
1944 | -- parameters has a null exclusion, then the corresponding formal | |
1945 | -- in Sub must also have one. Otherwise the subtype of the Sub's | |
1946 | -- formal parameter must exclude null. | |
1947 | -- | |
f3d57416 | 1948 | -- If Ren is a renaming of a formal function and its return |
16ca248a ES |
1949 | -- profile has a null exclusion, then Sub's return profile must |
1950 | -- have one. Otherwise the subtype of Sub's return profile must | |
1951 | -- exclude null. | |
996ae0b0 | 1952 | |
19c6e49c PMR |
1953 | procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id); |
1954 | -- Ensure that a SPARK renaming denoted by its entity Subp_Id does not | |
1955 | -- declare a primitive operation of a tagged type (SPARK RM 6.1.1(3)). | |
1956 | ||
02e4edea AC |
1957 | procedure Freeze_Actual_Profile; |
1958 | -- In Ada 2012, enforce the freezing rule concerning formal incomplete | |
1959 | -- types: a callable entity freezes its profile, unless it has an | |
1960 | -- incomplete untagged formal (RM 13.14(10.2/3)). | |
1961 | ||
fc193526 HK |
1962 | function Has_Class_Wide_Actual return Boolean; |
1963 | -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a | |
1964 | -- defaulted formal subprogram where the actual for the controlling | |
1965 | -- formal type is class-wide. | |
1966 | ||
07fc65c4 | 1967 | function Original_Subprogram (Subp : Entity_Id) return Entity_Id; |
16ca248a ES |
1968 | -- Find renamed entity when the declaration is a renaming_as_body and |
1969 | -- the renamed entity may itself be a renaming_as_body. Used to enforce | |
1970 | -- rule that a renaming_as_body is illegal if the declaration occurs | |
1971 | -- before the subprogram it completes is frozen, and renaming indirectly | |
1972 | -- renames the subprogram itself.(Defect Report 8652/0027). | |
07fc65c4 | 1973 | |
fc193526 HK |
1974 | ------------------------------ |
1975 | -- Build_Class_Wide_Wrapper -- | |
1976 | ------------------------------ | |
1138cf59 | 1977 | |
fc193526 HK |
1978 | procedure Build_Class_Wide_Wrapper |
1979 | (Ren_Id : out Entity_Id; | |
1980 | Wrap_Id : out Entity_Id) | |
1981 | is | |
1138cf59 AC |
1982 | Loc : constant Source_Ptr := Sloc (N); |
1983 | ||
e4d04166 AC |
1984 | function Build_Call |
1985 | (Subp_Id : Entity_Id; | |
1986 | Params : List_Id) return Node_Id; | |
1987 | -- Create a dispatching call to invoke routine Subp_Id with actuals | |
1988 | -- built from the parameter specifications of list Params. | |
1989 | ||
27bb7941 AC |
1990 | function Build_Expr_Fun_Call |
1991 | (Subp_Id : Entity_Id; | |
1992 | Params : List_Id) return Node_Id; | |
1993 | -- Create a dispatching call to invoke function Subp_Id with actuals | |
1994 | -- built from the parameter specifications of list Params. Return | |
1995 | -- directly the call, so that it can be used inside an expression | |
1996 | -- function. This is a specificity of the GNATprove mode. | |
1997 | ||
fc193526 HK |
1998 | function Build_Spec (Subp_Id : Entity_Id) return Node_Id; |
1999 | -- Create a subprogram specification based on the subprogram profile | |
2000 | -- of Subp_Id. | |
1138cf59 | 2001 | |
fc193526 HK |
2002 | function Find_Primitive (Typ : Entity_Id) return Entity_Id; |
2003 | -- Find a primitive subprogram of type Typ which matches the profile | |
2004 | -- of the renaming declaration. | |
1138cf59 | 2005 | |
fc193526 HK |
2006 | procedure Interpretation_Error (Subp_Id : Entity_Id); |
2007 | -- Emit a continuation error message suggesting subprogram Subp_Id as | |
2008 | -- a possible interpretation. | |
1138cf59 | 2009 | |
b6621d10 AC |
2010 | function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean; |
2011 | -- Determine whether subprogram Subp_Id denotes the intrinsic "=" | |
2012 | -- operator. | |
2013 | ||
2014 | function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean; | |
2015 | -- Determine whether subprogram Subp_Id is a suitable candidate for | |
2016 | -- the role of a wrapped subprogram. | |
2017 | ||
fc193526 HK |
2018 | ---------------- |
2019 | -- Build_Call -- | |
2020 | ---------------- | |
2021 | ||
2022 | function Build_Call | |
2023 | (Subp_Id : Entity_Id; | |
2024 | Params : List_Id) return Node_Id | |
2025 | is | |
2026 | Actuals : constant List_Id := New_List; | |
2027 | Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc); | |
2028 | Formal : Node_Id; | |
1138cf59 AC |
2029 | |
2030 | begin | |
fc193526 HK |
2031 | -- Build the actual parameters of the call |
2032 | ||
2033 | Formal := First (Params); | |
2034 | while Present (Formal) loop | |
1138cf59 | 2035 | Append_To (Actuals, |
fc193526 | 2036 | Make_Identifier (Loc, Chars (Defining_Identifier (Formal)))); |
fc193526 | 2037 | Next (Formal); |
1138cf59 AC |
2038 | end loop; |
2039 | ||
fc193526 HK |
2040 | -- Generate: |
2041 | -- return Subp_Id (Actuals); | |
2042 | ||
2043 | if Ekind_In (Subp_Id, E_Function, E_Operator) then | |
2044 | return | |
2045 | Make_Simple_Return_Statement (Loc, | |
2046 | Expression => | |
2047 | Make_Function_Call (Loc, | |
2048 | Name => Call_Ref, | |
2049 | Parameter_Associations => Actuals)); | |
2050 | ||
2051 | -- Generate: | |
2052 | -- Subp_Id (Actuals); | |
2053 | ||
1138cf59 AC |
2054 | else |
2055 | return | |
2056 | Make_Procedure_Call_Statement (Loc, | |
fc193526 HK |
2057 | Name => Call_Ref, |
2058 | Parameter_Associations => Actuals); | |
1138cf59 | 2059 | end if; |
fc193526 | 2060 | end Build_Call; |
1138cf59 | 2061 | |
27bb7941 AC |
2062 | ------------------------- |
2063 | -- Build_Expr_Fun_Call -- | |
2064 | ------------------------- | |
2065 | ||
2066 | function Build_Expr_Fun_Call | |
2067 | (Subp_Id : Entity_Id; | |
2068 | Params : List_Id) return Node_Id | |
2069 | is | |
2070 | Actuals : constant List_Id := New_List; | |
2071 | Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc); | |
2072 | Formal : Node_Id; | |
2073 | ||
2074 | begin | |
e4d04166 AC |
2075 | pragma Assert (Ekind_In (Subp_Id, E_Function, E_Operator)); |
2076 | ||
27bb7941 AC |
2077 | -- Build the actual parameters of the call |
2078 | ||
2079 | Formal := First (Params); | |
2080 | while Present (Formal) loop | |
2081 | Append_To (Actuals, | |
2082 | Make_Identifier (Loc, Chars (Defining_Identifier (Formal)))); | |
2083 | Next (Formal); | |
2084 | end loop; | |
2085 | ||
2086 | -- Generate: | |
2087 | -- Subp_Id (Actuals); | |
2088 | ||
e4d04166 AC |
2089 | return |
2090 | Make_Function_Call (Loc, | |
2091 | Name => Call_Ref, | |
2092 | Parameter_Associations => Actuals); | |
27bb7941 AC |
2093 | end Build_Expr_Fun_Call; |
2094 | ||
fc193526 HK |
2095 | ---------------- |
2096 | -- Build_Spec -- | |
2097 | ---------------- | |
1138cf59 | 2098 | |
fc193526 HK |
2099 | function Build_Spec (Subp_Id : Entity_Id) return Node_Id is |
2100 | Params : constant List_Id := Copy_Parameter_List (Subp_Id); | |
2101 | Spec_Id : constant Entity_Id := | |
6d0b56ad AC |
2102 | Make_Defining_Identifier (Loc, |
2103 | Chars => New_External_Name (Chars (Subp_Id), 'R')); | |
1138cf59 AC |
2104 | |
2105 | begin | |
fc193526 | 2106 | if Ekind (Formal_Spec) = E_Procedure then |
1138cf59 AC |
2107 | return |
2108 | Make_Procedure_Specification (Loc, | |
fc193526 HK |
2109 | Defining_Unit_Name => Spec_Id, |
2110 | Parameter_Specifications => Params); | |
1138cf59 AC |
2111 | else |
2112 | return | |
fc193526 HK |
2113 | Make_Function_Specification (Loc, |
2114 | Defining_Unit_Name => Spec_Id, | |
2115 | Parameter_Specifications => Params, | |
2116 | Result_Definition => | |
2117 | New_Copy_Tree (Result_Definition (Spec))); | |
1138cf59 | 2118 | end if; |
fc193526 HK |
2119 | end Build_Spec; |
2120 | ||
2121 | -------------------- | |
2122 | -- Find_Primitive -- | |
2123 | -------------------- | |
2124 | ||
2125 | function Find_Primitive (Typ : Entity_Id) return Entity_Id is | |
2126 | procedure Replace_Parameter_Types (Spec : Node_Id); | |
2127 | -- Given a specification Spec, replace all class-wide parameter | |
2128 | -- types with reference to type Typ. | |
1138cf59 | 2129 | |
fc193526 HK |
2130 | ----------------------------- |
2131 | -- Replace_Parameter_Types -- | |
2132 | ----------------------------- | |
2133 | ||
2134 | procedure Replace_Parameter_Types (Spec : Node_Id) is | |
2135 | Formal : Node_Id; | |
2136 | Formal_Id : Entity_Id; | |
2137 | Formal_Typ : Node_Id; | |
2138 | ||
2139 | begin | |
2140 | Formal := First (Parameter_Specifications (Spec)); | |
2141 | while Present (Formal) loop | |
2142 | Formal_Id := Defining_Identifier (Formal); | |
2143 | Formal_Typ := Parameter_Type (Formal); | |
2144 | ||
2145 | -- Create a new entity for each class-wide formal to prevent | |
2146 | -- aliasing with the original renaming. Replace the type of | |
2147 | -- such a parameter with the candidate type. | |
2148 | ||
2149 | if Nkind (Formal_Typ) = N_Identifier | |
2150 | and then Is_Class_Wide_Type (Etype (Formal_Typ)) | |
2151 | then | |
2152 | Set_Defining_Identifier (Formal, | |
2153 | Make_Defining_Identifier (Loc, Chars (Formal_Id))); | |
2154 | ||
2155 | Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc)); | |
2156 | end if; | |
2157 | ||
2158 | Next (Formal); | |
2159 | end loop; | |
2160 | end Replace_Parameter_Types; | |
2161 | ||
2162 | -- Local variables | |
2163 | ||
2164 | Alt_Ren : constant Node_Id := New_Copy_Tree (N); | |
2165 | Alt_Nam : constant Node_Id := Name (Alt_Ren); | |
2166 | Alt_Spec : constant Node_Id := Specification (Alt_Ren); | |
2167 | Subp_Id : Entity_Id; | |
2168 | ||
2169 | -- Start of processing for Find_Primitive | |
2170 | ||
2171 | begin | |
2172 | -- Each attempt to find a suitable primitive of a particular type | |
2173 | -- operates on its own copy of the original renaming. As a result | |
2174 | -- the original renaming is kept decoration and side-effect free. | |
2175 | ||
2176 | -- Inherit the overloaded status of the renamed subprogram name | |
2177 | ||
2178 | if Is_Overloaded (Nam) then | |
2179 | Set_Is_Overloaded (Alt_Nam); | |
2180 | Save_Interps (Nam, Alt_Nam); | |
2181 | end if; | |
2182 | ||
2183 | -- The copied renaming is hidden from visibility to prevent the | |
2184 | -- pollution of the enclosing context. | |
2185 | ||
2186 | Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R')); | |
2187 | ||
2188 | -- The types of all class-wide parameters must be changed to the | |
2189 | -- candidate type. | |
2190 | ||
2191 | Replace_Parameter_Types (Alt_Spec); | |
2192 | ||
2193 | -- Try to find a suitable primitive which matches the altered | |
2194 | -- profile of the renaming specification. | |
2195 | ||
2196 | Subp_Id := | |
2197 | Find_Renamed_Entity | |
2198 | (N => Alt_Ren, | |
2199 | Nam => Name (Alt_Ren), | |
2200 | New_S => Analyze_Subprogram_Specification (Alt_Spec), | |
2201 | Is_Actual => Is_Actual); | |
2202 | ||
2203 | -- Do not return Any_Id if the resolion of the altered profile | |
2204 | -- failed as this complicates further checks on the caller side, | |
2205 | -- return Empty instead. | |
2206 | ||
2207 | if Subp_Id = Any_Id then | |
2208 | return Empty; | |
2209 | else | |
2210 | return Subp_Id; | |
2211 | end if; | |
2212 | end Find_Primitive; | |
2213 | ||
2214 | -------------------------- | |
2215 | -- Interpretation_Error -- | |
2216 | -------------------------- | |
2217 | ||
2218 | procedure Interpretation_Error (Subp_Id : Entity_Id) is | |
2219 | begin | |
2220 | Error_Msg_Sloc := Sloc (Subp_Id); | |
b6621d10 AC |
2221 | |
2222 | if Is_Internal (Subp_Id) then | |
2223 | Error_Msg_NE | |
2224 | ("\\possible interpretation: predefined & #", | |
2225 | Spec, Formal_Spec); | |
2226 | else | |
2227 | Error_Msg_NE | |
2228 | ("\\possible interpretation: & defined #", Spec, Formal_Spec); | |
2229 | end if; | |
fc193526 HK |
2230 | end Interpretation_Error; |
2231 | ||
b6621d10 AC |
2232 | --------------------------- |
2233 | -- Is_Intrinsic_Equality -- | |
2234 | --------------------------- | |
2235 | ||
2236 | function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is | |
2237 | begin | |
2238 | return | |
2239 | Ekind (Subp_Id) = E_Operator | |
2240 | and then Chars (Subp_Id) = Name_Op_Eq | |
2241 | and then Is_Intrinsic_Subprogram (Subp_Id); | |
2242 | end Is_Intrinsic_Equality; | |
2243 | ||
2244 | --------------------------- | |
2245 | -- Is_Suitable_Candidate -- | |
2246 | --------------------------- | |
2247 | ||
2248 | function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is | |
2249 | begin | |
2250 | if No (Subp_Id) then | |
2251 | return False; | |
2252 | ||
2253 | -- An intrinsic subprogram is never a good candidate. This is an | |
2254 | -- indication of a missing primitive, either defined directly or | |
2255 | -- inherited from a parent tagged type. | |
2256 | ||
2257 | elsif Is_Intrinsic_Subprogram (Subp_Id) then | |
2258 | return False; | |
2259 | ||
2260 | else | |
2261 | return True; | |
2262 | end if; | |
2263 | end Is_Suitable_Candidate; | |
2264 | ||
fc193526 HK |
2265 | -- Local variables |
2266 | ||
2267 | Actual_Typ : Entity_Id := Empty; | |
2268 | -- The actual class-wide type for Formal_Typ | |
2269 | ||
b6621d10 | 2270 | CW_Prim_OK : Boolean; |
fc193526 | 2271 | CW_Prim_Op : Entity_Id; |
b6621d10 AC |
2272 | -- The class-wide subprogram (if available) which corresponds to the |
2273 | -- renamed generic formal subprogram. | |
fc193526 HK |
2274 | |
2275 | Formal_Typ : Entity_Id := Empty; | |
b6621d10 | 2276 | -- The generic formal type with unknown discriminants |
fc193526 | 2277 | |
b6621d10 | 2278 | Root_Prim_OK : Boolean; |
fc193526 | 2279 | Root_Prim_Op : Entity_Id; |
b6621d10 AC |
2280 | -- The root type primitive (if available) which corresponds to the |
2281 | -- renamed generic formal subprogram. | |
2282 | ||
2283 | Root_Typ : Entity_Id := Empty; | |
2284 | -- The root type of Actual_Typ | |
fc193526 HK |
2285 | |
2286 | Body_Decl : Node_Id; | |
2287 | Formal : Node_Id; | |
2288 | Prim_Op : Entity_Id; | |
2289 | Spec_Decl : Node_Id; | |
27bb7941 | 2290 | New_Spec : Node_Id; |
fc193526 HK |
2291 | |
2292 | -- Start of processing for Build_Class_Wide_Wrapper | |
1138cf59 AC |
2293 | |
2294 | begin | |
fc193526 HK |
2295 | -- Analyze the specification of the renaming in case the generation |
2296 | -- of the class-wide wrapper fails. | |
2297 | ||
2298 | Ren_Id := Analyze_Subprogram_Specification (Spec); | |
2299 | Wrap_Id := Any_Id; | |
2300 | ||
2301 | -- Do not attempt to build a wrapper if the renaming is in error | |
2302 | ||
2303 | if Error_Posted (Nam) then | |
2304 | return; | |
2305 | end if; | |
2306 | ||
2307 | -- Analyze the renamed name, but do not resolve it. The resolution is | |
b6621d10 | 2308 | -- completed once a suitable subprogram is found. |
fc193526 HK |
2309 | |
2310 | Analyze (Nam); | |
2311 | ||
b6621d10 AC |
2312 | -- When the renamed name denotes the intrinsic operator equals, the |
2313 | -- name must be treated as overloaded. This allows for a potential | |
2314 | -- match against the root type's predefined equality function. | |
2315 | ||
2316 | if Is_Intrinsic_Equality (Entity (Nam)) then | |
2317 | Set_Is_Overloaded (Nam); | |
2318 | Collect_Interps (Nam); | |
2319 | end if; | |
2320 | ||
fc193526 HK |
2321 | -- Step 1: Find the generic formal type with unknown discriminants |
2322 | -- and its corresponding class-wide actual type from the renamed | |
2323 | -- generic formal subprogram. | |
2324 | ||
2325 | Formal := First_Formal (Formal_Spec); | |
2326 | while Present (Formal) loop | |
2327 | if Has_Unknown_Discriminants (Etype (Formal)) | |
2328 | and then not Is_Class_Wide_Type (Etype (Formal)) | |
2329 | and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal))) | |
1138cf59 | 2330 | then |
fc193526 HK |
2331 | Formal_Typ := Etype (Formal); |
2332 | Actual_Typ := Get_Instance_Of (Formal_Typ); | |
b6621d10 | 2333 | Root_Typ := Etype (Actual_Typ); |
1138cf59 AC |
2334 | exit; |
2335 | end if; | |
2336 | ||
fc193526 | 2337 | Next_Formal (Formal); |
1138cf59 AC |
2338 | end loop; |
2339 | ||
fc193526 HK |
2340 | -- The specification of the generic formal subprogram should always |
2341 | -- contain a formal type with unknown discriminants whose actual is | |
2342 | -- a class-wide type, otherwise this indicates a failure in routine | |
2343 | -- Has_Class_Wide_Actual. | |
1138cf59 | 2344 | |
fc193526 | 2345 | pragma Assert (Present (Formal_Typ)); |
1138cf59 | 2346 | |
b6621d10 AC |
2347 | -- Step 2: Find the proper class-wide subprogram or primitive which |
2348 | -- corresponds to the renamed generic formal subprogram. | |
1138cf59 | 2349 | |
fc193526 | 2350 | CW_Prim_Op := Find_Primitive (Actual_Typ); |
b6621d10 AC |
2351 | CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op); |
2352 | Root_Prim_Op := Find_Primitive (Root_Typ); | |
2353 | Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op); | |
1138cf59 | 2354 | |
b6621d10 | 2355 | -- The class-wide actual type has two subprograms which correspond to |
fc193526 | 2356 | -- the renamed generic formal subprogram: |
1138cf59 | 2357 | |
fc193526 HK |
2358 | -- with procedure Prim_Op (Param : Formal_Typ); |
2359 | ||
2360 | -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited | |
2361 | -- procedure Prim_Op (Param : Actual_Typ'Class); | |
2362 | ||
b6621d10 AC |
2363 | -- Even though the declaration of the two subprograms is legal, a |
2364 | -- call to either one is ambiguous and therefore illegal. | |
fc193526 | 2365 | |
b6621d10 | 2366 | if CW_Prim_OK and Root_Prim_OK then |
fc193526 | 2367 | |
b6621d10 | 2368 | -- A user-defined primitive has precedence over a predefined one |
fc193526 | 2369 | |
b6621d10 AC |
2370 | if Is_Internal (CW_Prim_Op) |
2371 | and then not Is_Internal (Root_Prim_Op) | |
fc193526 | 2372 | then |
fc193526 | 2373 | Prim_Op := Root_Prim_Op; |
1138cf59 | 2374 | |
b6621d10 AC |
2375 | elsif Is_Internal (Root_Prim_Op) |
2376 | and then not Is_Internal (CW_Prim_Op) | |
2377 | then | |
fc193526 | 2378 | Prim_Op := CW_Prim_Op; |
1138cf59 | 2379 | |
fc193526 HK |
2380 | elsif CW_Prim_Op = Root_Prim_Op then |
2381 | Prim_Op := Root_Prim_Op; | |
1138cf59 | 2382 | |
b6621d10 AC |
2383 | -- Otherwise both candidate subprograms are user-defined and |
2384 | -- ambiguous. | |
1138cf59 | 2385 | |
fc193526 HK |
2386 | else |
2387 | Error_Msg_NE | |
2388 | ("ambiguous actual for generic subprogram &", | |
b6621d10 | 2389 | Spec, Formal_Spec); |
fc193526 | 2390 | Interpretation_Error (Root_Prim_Op); |
b6621d10 | 2391 | Interpretation_Error (CW_Prim_Op); |
fc193526 | 2392 | return; |
f4f92d9d | 2393 | end if; |
1138cf59 | 2394 | |
b6621d10 | 2395 | elsif CW_Prim_OK and not Root_Prim_OK then |
fc193526 HK |
2396 | Prim_Op := CW_Prim_Op; |
2397 | ||
b6621d10 AC |
2398 | elsif not CW_Prim_OK and Root_Prim_OK then |
2399 | Prim_Op := Root_Prim_Op; | |
2400 | ||
2401 | -- An intrinsic equality may act as a suitable candidate in the case | |
2402 | -- of a null type extension where the parent's equality is hidden. A | |
2403 | -- call to an intrinsic equality is expanded as dispatching. | |
2404 | ||
2405 | elsif Present (Root_Prim_Op) | |
2406 | and then Is_Intrinsic_Equality (Root_Prim_Op) | |
2407 | then | |
fc193526 HK |
2408 | Prim_Op := Root_Prim_Op; |
2409 | ||
b6621d10 | 2410 | -- Otherwise there are no candidate subprograms. Let the caller |
fc193526 HK |
2411 | -- diagnose the error. |
2412 | ||
2413 | else | |
2414 | return; | |
2415 | end if; | |
2416 | ||
6d0b56ad AC |
2417 | -- At this point resolution has taken place and the name is no longer |
2418 | -- overloaded. Mark the primitive as referenced. | |
fc193526 | 2419 | |
6d0b56ad | 2420 | Set_Is_Overloaded (Name (N), False); |
ad5edba5 | 2421 | Set_Referenced (Prim_Op); |
fc193526 | 2422 | |
66340e0e AC |
2423 | -- Do not generate a wrapper when the only candidate is a class-wide |
2424 | -- subprogram. Instead modify the renaming to directly map the actual | |
2425 | -- to the generic formal. | |
2426 | ||
2427 | if CW_Prim_OK and then Prim_Op = CW_Prim_Op then | |
2428 | Wrap_Id := Prim_Op; | |
2429 | Rewrite (Nam, New_Occurrence_Of (Prim_Op, Loc)); | |
2430 | return; | |
2431 | end if; | |
2432 | ||
fc193526 HK |
2433 | -- Step 3: Create the declaration and the body of the wrapper, insert |
2434 | -- all the pieces into the tree. | |
2435 | ||
27bb7941 AC |
2436 | -- In GNATprove mode, create a function wrapper in the form of an |
2437 | -- expression function, so that an implicit postcondition relating | |
2438 | -- the result of calling the wrapper function and the result of the | |
2439 | -- dispatching call to the wrapped function is known during proof. | |
2440 | ||
2441 | if GNATprove_Mode | |
2442 | and then Ekind_In (Ren_Id, E_Function, E_Operator) | |
2443 | then | |
2444 | New_Spec := Build_Spec (Ren_Id); | |
2445 | Body_Decl := | |
2446 | Make_Expression_Function (Loc, | |
2447 | Specification => New_Spec, | |
e4d04166 AC |
2448 | Expression => |
2449 | Build_Expr_Fun_Call | |
2450 | (Subp_Id => Prim_Op, | |
2451 | Params => Parameter_Specifications (New_Spec))); | |
27bb7941 AC |
2452 | |
2453 | Wrap_Id := Defining_Entity (Body_Decl); | |
2454 | ||
2455 | -- Otherwise, create separate spec and body for the subprogram | |
2456 | ||
2457 | else | |
2458 | Spec_Decl := | |
2459 | Make_Subprogram_Declaration (Loc, | |
2460 | Specification => Build_Spec (Ren_Id)); | |
2461 | Insert_Before_And_Analyze (N, Spec_Decl); | |
2462 | ||
2463 | Wrap_Id := Defining_Entity (Spec_Decl); | |
2464 | ||
2465 | Body_Decl := | |
2466 | Make_Subprogram_Body (Loc, | |
2467 | Specification => Build_Spec (Ren_Id), | |
2468 | Declarations => New_List, | |
2469 | Handled_Statement_Sequence => | |
2470 | Make_Handled_Sequence_Of_Statements (Loc, | |
2471 | Statements => New_List ( | |
2472 | Build_Call | |
2473 | (Subp_Id => Prim_Op, | |
2474 | Params => | |
2475 | Parameter_Specifications | |
2476 | (Specification (Spec_Decl)))))); | |
2477 | ||
2478 | Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl)); | |
2479 | end if; | |
6d0b56ad AC |
2480 | |
2481 | -- If the operator carries an Eliminated pragma, indicate that the | |
2482 | -- wrapper is also to be eliminated, to prevent spurious error when | |
2483 | -- using gnatelim on programs that include box-initialization of | |
2484 | -- equality operators. | |
2485 | ||
6d0b56ad | 2486 | Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op)); |
fc193526 | 2487 | |
27bb7941 AC |
2488 | -- In GNATprove mode, insert the body in the tree for analysis |
2489 | ||
2490 | if GNATprove_Mode then | |
2491 | Insert_Before_And_Analyze (N, Body_Decl); | |
2492 | end if; | |
fc193526 | 2493 | |
fc193526 HK |
2494 | -- The generated body does not freeze and must be analyzed when the |
2495 | -- class-wide wrapper is frozen. The body is only needed if expansion | |
2496 | -- is enabled. | |
2497 | ||
2498 | if Expander_Active then | |
2499 | Append_Freeze_Action (Wrap_Id, Body_Decl); | |
1138cf59 AC |
2500 | end if; |
2501 | ||
6d0b56ad | 2502 | -- Step 4: The subprogram renaming aliases the wrapper |
1138cf59 | 2503 | |
6d0b56ad | 2504 | Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc)); |
fc193526 | 2505 | end Build_Class_Wide_Wrapper; |
1138cf59 | 2506 | |
923fa078 RD |
2507 | -------------------------- |
2508 | -- Check_Null_Exclusion -- | |
2509 | -------------------------- | |
2510 | ||
2511 | procedure Check_Null_Exclusion | |
2512 | (Ren : Entity_Id; | |
2513 | Sub : Entity_Id) | |
2514 | is | |
16ca248a ES |
2515 | Ren_Formal : Entity_Id; |
2516 | Sub_Formal : Entity_Id; | |
923fa078 RD |
2517 | |
2518 | begin | |
2519 | -- Parameter check | |
2520 | ||
16ca248a ES |
2521 | Ren_Formal := First_Formal (Ren); |
2522 | Sub_Formal := First_Formal (Sub); | |
ac7d724d | 2523 | while Present (Ren_Formal) and then Present (Sub_Formal) loop |
923fa078 RD |
2524 | if Has_Null_Exclusion (Parent (Ren_Formal)) |
2525 | and then | |
2526 | not (Has_Null_Exclusion (Parent (Sub_Formal)) | |
ac7d724d | 2527 | or else Can_Never_Be_Null (Etype (Sub_Formal))) |
923fa078 | 2528 | then |
fbe627af RD |
2529 | Error_Msg_NE |
2530 | ("`NOT NULL` required for parameter &", | |
2531 | Parent (Sub_Formal), Sub_Formal); | |
923fa078 RD |
2532 | end if; |
2533 | ||
2534 | Next_Formal (Ren_Formal); | |
2535 | Next_Formal (Sub_Formal); | |
2536 | end loop; | |
2537 | ||
2538 | -- Return profile check | |
2539 | ||
2540 | if Nkind (Parent (Ren)) = N_Function_Specification | |
2541 | and then Nkind (Parent (Sub)) = N_Function_Specification | |
2542 | and then Has_Null_Exclusion (Parent (Ren)) | |
ac7d724d ES |
2543 | and then not (Has_Null_Exclusion (Parent (Sub)) |
2544 | or else Can_Never_Be_Null (Etype (Sub))) | |
923fa078 | 2545 | then |
fbe627af RD |
2546 | Error_Msg_N |
2547 | ("return must specify `NOT NULL`", | |
2548 | Result_Definition (Parent (Sub))); | |
923fa078 RD |
2549 | end if; |
2550 | end Check_Null_Exclusion; | |
2551 | ||
19c6e49c PMR |
2552 | ------------------------------------- |
2553 | -- Check_SPARK_Primitive_Operation -- | |
2554 | ------------------------------------- | |
2555 | ||
2556 | procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id) is | |
2557 | Prag : constant Node_Id := SPARK_Pragma (Subp_Id); | |
2558 | Typ : Entity_Id; | |
2559 | ||
2560 | begin | |
19c6e49c PMR |
2561 | -- Nothing to do when the subprogram is not subject to SPARK_Mode On |
2562 | -- because this check applies to SPARK code only. | |
2563 | ||
1554ed7e PMR |
2564 | if not (Present (Prag) |
2565 | and then Get_SPARK_Mode_From_Annotation (Prag) = On) | |
19c6e49c PMR |
2566 | then |
2567 | return; | |
2568 | ||
2569 | -- Nothing to do when the subprogram is not a primitive operation | |
2570 | ||
2571 | elsif not Is_Primitive (Subp_Id) then | |
2572 | return; | |
2573 | end if; | |
2574 | ||
2575 | Typ := Find_Dispatching_Type (Subp_Id); | |
2576 | ||
2577 | -- Nothing to do when the subprogram is a primitive operation of an | |
2578 | -- untagged type. | |
2579 | ||
2580 | if No (Typ) then | |
2581 | return; | |
2582 | end if; | |
2583 | ||
2584 | -- At this point a renaming declaration introduces a new primitive | |
2585 | -- operation for a tagged type. | |
2586 | ||
2587 | Error_Msg_Node_2 := Typ; | |
2588 | Error_Msg_NE | |
2589 | ("subprogram renaming & cannot declare primitive for type & " | |
2590 | & "(SPARK RM 6.1.1(3))", N, Subp_Id); | |
2591 | end Check_SPARK_Primitive_Operation; | |
2592 | ||
02e4edea AC |
2593 | --------------------------- |
2594 | -- Freeze_Actual_Profile -- | |
2595 | --------------------------- | |
2596 | ||
2597 | procedure Freeze_Actual_Profile is | |
2598 | F : Entity_Id; | |
2599 | Has_Untagged_Inc : Boolean; | |
2600 | Instantiation_Node : constant Node_Id := Parent (N); | |
2601 | ||
2602 | begin | |
2603 | if Ada_Version >= Ada_2012 then | |
2604 | F := First_Formal (Formal_Spec); | |
2605 | Has_Untagged_Inc := False; | |
2606 | while Present (F) loop | |
2607 | if Ekind (Etype (F)) = E_Incomplete_Type | |
2608 | and then not Is_Tagged_Type (Etype (F)) | |
2609 | then | |
2610 | Has_Untagged_Inc := True; | |
2611 | exit; | |
2612 | end if; | |
2613 | ||
2614 | F := Next_Formal (F); | |
2615 | end loop; | |
2616 | ||
2617 | if Ekind (Formal_Spec) = E_Function | |
598a56c0 | 2618 | and then not Is_Tagged_Type (Etype (Formal_Spec)) |
02e4edea AC |
2619 | then |
2620 | Has_Untagged_Inc := True; | |
2621 | end if; | |
2622 | ||
2623 | if not Has_Untagged_Inc then | |
2624 | F := First_Formal (Old_S); | |
2625 | while Present (F) loop | |
2626 | Freeze_Before (Instantiation_Node, Etype (F)); | |
2627 | ||
2628 | if Is_Incomplete_Or_Private_Type (Etype (F)) | |
2629 | and then No (Underlying_Type (Etype (F))) | |
02e4edea | 2630 | then |
6ee07c61 AC |
2631 | -- Exclude generic types, or types derived from them. |
2632 | -- They will be frozen in the enclosing instance. | |
2633 | ||
2634 | if Is_Generic_Type (Etype (F)) | |
2635 | or else Is_Generic_Type (Root_Type (Etype (F))) | |
2636 | then | |
2637 | null; | |
598a56c0 ES |
2638 | |
2639 | -- A limited view of a type declared elsewhere needs no | |
2640 | -- freezing actions. | |
2641 | ||
2642 | elsif From_Limited_With (Etype (F)) then | |
2643 | null; | |
2644 | ||
6ee07c61 AC |
2645 | else |
2646 | Error_Msg_NE | |
2647 | ("type& must be frozen before this point", | |
67a90476 | 2648 | Instantiation_Node, Etype (F)); |
6ee07c61 | 2649 | end if; |
02e4edea AC |
2650 | end if; |
2651 | ||
2652 | F := Next_Formal (F); | |
2653 | end loop; | |
2654 | end if; | |
2655 | end if; | |
2656 | end Freeze_Actual_Profile; | |
2657 | ||
fd3d2680 AC |
2658 | --------------------------- |
2659 | -- Has_Class_Wide_Actual -- | |
2660 | --------------------------- | |
2661 | ||
2662 | function Has_Class_Wide_Actual return Boolean is | |
fc193526 HK |
2663 | Formal : Entity_Id; |
2664 | Formal_Typ : Entity_Id; | |
fd3d2680 AC |
2665 | |
2666 | begin | |
fc193526 HK |
2667 | if Is_Actual then |
2668 | Formal := First_Formal (Formal_Spec); | |
2669 | while Present (Formal) loop | |
2670 | Formal_Typ := Etype (Formal); | |
2671 | ||
2672 | if Has_Unknown_Discriminants (Formal_Typ) | |
2673 | and then not Is_Class_Wide_Type (Formal_Typ) | |
2674 | and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ)) | |
fd3d2680 AC |
2675 | then |
2676 | return True; | |
2677 | end if; | |
2678 | ||
fc193526 | 2679 | Next_Formal (Formal); |
fd3d2680 AC |
2680 | end loop; |
2681 | end if; | |
2682 | ||
2683 | return False; | |
2684 | end Has_Class_Wide_Actual; | |
2685 | ||
07fc65c4 GB |
2686 | ------------------------- |
2687 | -- Original_Subprogram -- | |
2688 | ------------------------- | |
2689 | ||
2690 | function Original_Subprogram (Subp : Entity_Id) return Entity_Id is | |
2691 | Orig_Decl : Node_Id; | |
2692 | Orig_Subp : Entity_Id; | |
2693 | ||
2694 | begin | |
2695 | -- First case: renamed entity is itself a renaming | |
2696 | ||
2697 | if Present (Alias (Subp)) then | |
2698 | return Alias (Subp); | |
2699 | ||
ac7d724d ES |
2700 | elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration |
2701 | and then Present (Corresponding_Body (Unit_Declaration_Node (Subp))) | |
07fc65c4 GB |
2702 | then |
2703 | -- Check if renamed entity is a renaming_as_body | |
2704 | ||
2705 | Orig_Decl := | |
2706 | Unit_Declaration_Node | |
2707 | (Corresponding_Body (Unit_Declaration_Node (Subp))); | |
2708 | ||
2709 | if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then | |
2710 | Orig_Subp := Entity (Name (Orig_Decl)); | |
2711 | ||
2712 | if Orig_Subp = Rename_Spec then | |
2713 | ||
bc41faa2 | 2714 | -- Circularity detected |
07fc65c4 GB |
2715 | |
2716 | return Orig_Subp; | |
2717 | ||
2718 | else | |
2719 | return (Original_Subprogram (Orig_Subp)); | |
2720 | end if; | |
2721 | else | |
2722 | return Subp; | |
2723 | end if; | |
2724 | else | |
2725 | return Subp; | |
2726 | end if; | |
2727 | end Original_Subprogram; | |
2728 | ||
fc193526 HK |
2729 | -- Local variables |
2730 | ||
fd3d2680 AC |
2731 | CW_Actual : constant Boolean := Has_Class_Wide_Actual; |
2732 | -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a | |
2733 | -- defaulted formal subprogram when the actual for a related formal | |
2734 | -- type is class-wide. | |
2735 | ||
1af4455a | 2736 | Inst_Node : Node_Id := Empty; |
fc193526 HK |
2737 | New_S : Entity_Id; |
2738 | ||
fbf5a39b | 2739 | -- Start of processing for Analyze_Subprogram_Renaming |
07fc65c4 | 2740 | |
996ae0b0 RK |
2741 | begin |
2742 | -- We must test for the attribute renaming case before the Analyze | |
2743 | -- call because otherwise Sem_Attr will complain that the attribute | |
2744 | -- is missing an argument when it is analyzed. | |
2745 | ||
2746 | if Nkind (Nam) = N_Attribute_Reference then | |
d239991f | 2747 | |
16ca248a ES |
2748 | -- In the case of an abstract formal subprogram association, rewrite |
2749 | -- an actual given by a stream attribute as the name of the | |
2750 | -- corresponding stream primitive of the type. | |
d239991f | 2751 | |
16ca248a ES |
2752 | -- In a generic context the stream operations are not generated, and |
2753 | -- this must be treated as a normal attribute reference, to be | |
2754 | -- expanded in subsequent instantiations. | |
d4810530 | 2755 | |
ac7d724d ES |
2756 | if Is_Actual |
2757 | and then Is_Abstract_Subprogram (Formal_Spec) | |
4460a9bc | 2758 | and then Expander_Active |
d4810530 | 2759 | then |
d239991f | 2760 | declare |
d239991f | 2761 | Prefix_Type : constant Entity_Id := Entity (Prefix (Nam)); |
d65a80fd | 2762 | Stream_Prim : Entity_Id; |
d239991f GD |
2763 | |
2764 | begin | |
2765 | -- The class-wide forms of the stream attributes are not | |
2766 | -- primitive dispatching operations (even though they | |
2767 | -- internally dispatch to a stream attribute). | |
2768 | ||
2769 | if Is_Class_Wide_Type (Prefix_Type) then | |
2770 | Error_Msg_N | |
2771 | ("attribute must be a primitive dispatching operation", | |
2772 | Nam); | |
2773 | return; | |
2774 | end if; | |
2775 | ||
2776 | -- Retrieve the primitive subprogram associated with the | |
16ca248a ES |
2777 | -- attribute. This can only be a stream attribute, since those |
2778 | -- are the only ones that are dispatching (and the actual for | |
2779 | -- an abstract formal subprogram must be dispatching | |
2780 | -- operation). | |
d239991f | 2781 | |
ca811241 | 2782 | case Attribute_Name (Nam) is |
d8f43ee6 | 2783 | when Name_Input => |
ca811241 BD |
2784 | Stream_Prim := |
2785 | Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input); | |
d8f43ee6 | 2786 | |
ca811241 BD |
2787 | when Name_Output => |
2788 | Stream_Prim := | |
2789 | Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output); | |
d8f43ee6 HK |
2790 | |
2791 | when Name_Read => | |
ca811241 BD |
2792 | Stream_Prim := |
2793 | Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read); | |
d8f43ee6 HK |
2794 | |
2795 | when Name_Write => | |
ca811241 BD |
2796 | Stream_Prim := |
2797 | Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write); | |
d8f43ee6 HK |
2798 | |
2799 | when others => | |
ca811241 | 2800 | Error_Msg_N |
d65a80fd HK |
2801 | ("attribute must be a primitive dispatching operation", |
2802 | Nam); | |
ca811241 BD |
2803 | return; |
2804 | end case; | |
6a4d72a6 | 2805 | |
d65a80fd HK |
2806 | -- If no operation was found, and the type is limited, the user |
2807 | -- should have defined one. | |
6a4d72a6 | 2808 | |
ca811241 BD |
2809 | if No (Stream_Prim) then |
2810 | if Is_Limited_Type (Prefix_Type) then | |
2811 | Error_Msg_NE | |
2812 | ("stream operation not defined for type&", | |
2813 | N, Prefix_Type); | |
2814 | return; | |
2815 | ||
2816 | -- Otherwise, compiler should have generated default | |
2817 | ||
2818 | else | |
2819 | raise Program_Error; | |
2820 | end if; | |
2821 | end if; | |
d239991f GD |
2822 | |
2823 | -- Rewrite the attribute into the name of its corresponding | |
2824 | -- primitive dispatching subprogram. We can then proceed with | |
2825 | -- the usual processing for subprogram renamings. | |
2826 | ||
2827 | declare | |
2828 | Prim_Name : constant Node_Id := | |
2829 | Make_Identifier (Sloc (Nam), | |
2830 | Chars => Chars (Stream_Prim)); | |
2831 | begin | |
2832 | Set_Entity (Prim_Name, Stream_Prim); | |
2833 | Rewrite (Nam, Prim_Name); | |
2834 | Analyze (Nam); | |
2835 | end; | |
2836 | end; | |
2837 | ||
2838 | -- Normal processing for a renaming of an attribute | |
2839 | ||
2840 | else | |
2841 | Attribute_Renaming (N); | |
2842 | return; | |
2843 | end if; | |
996ae0b0 RK |
2844 | end if; |
2845 | ||
d65a80fd HK |
2846 | -- Check whether this declaration corresponds to the instantiation of a |
2847 | -- formal subprogram. | |
996ae0b0 | 2848 | |
16ca248a ES |
2849 | -- If this is an instantiation, the corresponding actual is frozen and |
2850 | -- error messages can be made more precise. If this is a default | |
2851 | -- subprogram, the entity is already established in the generic, and is | |
2852 | -- not retrieved by visibility. If it is a default with a box, the | |
996ae0b0 | 2853 | -- candidate interpretations, if any, have been collected when building |
16ca248a ES |
2854 | -- the renaming declaration. If overloaded, the proper interpretation is |
2855 | -- determined in Find_Renamed_Entity. If the entity is an operator, | |
996ae0b0 RK |
2856 | -- Find_Renamed_Entity applies additional visibility checks. |
2857 | ||
d239991f GD |
2858 | if Is_Actual then |
2859 | Inst_Node := Unit_Declaration_Node (Formal_Spec); | |
996ae0b0 | 2860 | |
fd3d2680 AC |
2861 | -- Check whether the renaming is for a defaulted actual subprogram |
2862 | -- with a class-wide actual. | |
2863 | ||
1c85591c AC |
2864 | -- The class-wide wrapper is not needed in GNATprove_Mode and there |
2865 | -- is an external axiomatization on the package. | |
c3831524 AC |
2866 | |
2867 | if CW_Actual | |
d65a80fd HK |
2868 | and then Box_Present (Inst_Node) |
2869 | and then not | |
1c85591c AC |
2870 | (GNATprove_Mode |
2871 | and then | |
2872 | Present (Containing_Package_With_Ext_Axioms (Formal_Spec))) | |
c3831524 | 2873 | then |
fc193526 | 2874 | Build_Class_Wide_Wrapper (New_S, Old_S); |
fd3d2680 AC |
2875 | |
2876 | elsif Is_Entity_Name (Nam) | |
996ae0b0 RK |
2877 | and then Present (Entity (Nam)) |
2878 | and then not Comes_From_Source (Nam) | |
2879 | and then not Is_Overloaded (Nam) | |
2880 | then | |
2881 | Old_S := Entity (Nam); | |
d65a80fd HK |
2882 | |
2883 | -- The subprogram renaming declaration may become Ghost if it | |
2884 | -- renames a Ghost entity. | |
2885 | ||
2886 | Mark_Ghost_Renaming (N, Old_S); | |
2887 | ||
fbf5a39b | 2888 | New_S := Analyze_Subprogram_Specification (Spec); |
996ae0b0 | 2889 | |
2e071734 AC |
2890 | -- Operator case |
2891 | ||
d65a80fd | 2892 | if Ekind (Old_S) = E_Operator then |
2e071734 AC |
2893 | |
2894 | -- Box present | |
2895 | ||
2896 | if Box_Present (Inst_Node) then | |
2897 | Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual); | |
2898 | ||
2899 | -- If there is an immediately visible homonym of the operator | |
2900 | -- and the declaration has a default, this is worth a warning | |
2901 | -- because the user probably did not intend to get the pre- | |
16ca248a ES |
2902 | -- defined operator, visible in the generic declaration. To |
2903 | -- find if there is an intended candidate, analyze the renaming | |
2904 | -- again in the current context. | |
2e071734 AC |
2905 | |
2906 | elsif Scope (Old_S) = Standard_Standard | |
2907 | and then Present (Default_Name (Inst_Node)) | |
2908 | then | |
2909 | declare | |
2910 | Decl : constant Node_Id := New_Copy_Tree (N); | |
2911 | Hidden : Entity_Id; | |
2912 | ||
2913 | begin | |
2914 | Set_Entity (Name (Decl), Empty); | |
2915 | Analyze (Name (Decl)); | |
2916 | Hidden := | |
2917 | Find_Renamed_Entity (Decl, Name (Decl), New_S, True); | |
2918 | ||
2919 | if Present (Hidden) | |
2920 | and then In_Open_Scopes (Scope (Hidden)) | |
2921 | and then Is_Immediately_Visible (Hidden) | |
2922 | and then Comes_From_Source (Hidden) | |
923fa078 | 2923 | and then Hidden /= Old_S |
2e071734 AC |
2924 | then |
2925 | Error_Msg_Sloc := Sloc (Hidden); | |
d65a80fd HK |
2926 | Error_Msg_N |
2927 | ("default subprogram is resolved in the generic " | |
2928 | & "declaration (RM 12.6(17))??", N); | |
dbfeb4fa | 2929 | Error_Msg_NE ("\and will not use & #??", N, Hidden); |
2e071734 AC |
2930 | end if; |
2931 | end; | |
2932 | end if; | |
996ae0b0 RK |
2933 | end if; |
2934 | ||
2935 | else | |
2936 | Analyze (Nam); | |
d65a80fd HK |
2937 | |
2938 | -- The subprogram renaming declaration may become Ghost if it | |
2939 | -- renames a Ghost entity. | |
2940 | ||
2941 | if Is_Entity_Name (Nam) then | |
2942 | Mark_Ghost_Renaming (N, Entity (Nam)); | |
2943 | end if; | |
2944 | ||
fbf5a39b | 2945 | New_S := Analyze_Subprogram_Specification (Spec); |
996ae0b0 RK |
2946 | end if; |
2947 | ||
996ae0b0 RK |
2948 | else |
2949 | -- Renamed entity must be analyzed first, to avoid being hidden by | |
2950 | -- new name (which might be the same in a generic instance). | |
2951 | ||
2952 | Analyze (Nam); | |
2953 | ||
d65a80fd HK |
2954 | -- The subprogram renaming declaration may become Ghost if it renames |
2955 | -- a Ghost entity. | |
2956 | ||
2957 | if Is_Entity_Name (Nam) then | |
2958 | Mark_Ghost_Renaming (N, Entity (Nam)); | |
2959 | end if; | |
2960 | ||
996ae0b0 RK |
2961 | -- The renaming defines a new overloaded entity, which is analyzed |
2962 | -- like a subprogram declaration. | |
2963 | ||
fbf5a39b | 2964 | New_S := Analyze_Subprogram_Specification (Spec); |
996ae0b0 RK |
2965 | end if; |
2966 | ||
2967 | if Current_Scope /= Standard_Standard then | |
2968 | Set_Is_Pure (New_S, Is_Pure (Current_Scope)); | |
2969 | end if; | |
2970 | ||
74c10109 AC |
2971 | -- Set SPARK mode from current context |
2972 | ||
19c6e49c | 2973 | Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma); |
f145ece7 | 2974 | Set_SPARK_Pragma_Inherited (New_S); |
74c10109 | 2975 | |
996ae0b0 RK |
2976 | Rename_Spec := Find_Corresponding_Spec (N); |
2977 | ||
294ccb21 RD |
2978 | -- Case of Renaming_As_Body |
2979 | ||
996ae0b0 | 2980 | if Present (Rename_Spec) then |
8489c295 AC |
2981 | Check_Previous_Null_Procedure (N, Rename_Spec); |
2982 | ||
294ccb21 RD |
2983 | -- Renaming declaration is the completion of the declaration of |
2984 | -- Rename_Spec. We build an actual body for it at the freezing point. | |
996ae0b0 RK |
2985 | |
2986 | Set_Corresponding_Spec (N, Rename_Spec); | |
fbe627af | 2987 | |
ff81221b ES |
2988 | -- Deal with special case of stream functions of abstract types |
2989 | -- and interfaces. | |
294ccb21 | 2990 | |
d4810530 ES |
2991 | if Nkind (Unit_Declaration_Node (Rename_Spec)) = |
2992 | N_Abstract_Subprogram_Declaration | |
2993 | then | |
ff81221b ES |
2994 | -- Input stream functions are abstract if the object type is |
2995 | -- abstract. Similarly, all default stream functions for an | |
30783513 | 2996 | -- interface type are abstract. However, these subprograms may |
ff81221b ES |
2997 | -- receive explicit declarations in representation clauses, making |
2998 | -- the attribute subprograms usable as defaults in subsequent | |
2999 | -- type extensions. | |
d4810530 ES |
3000 | -- In this case we rewrite the declaration to make the subprogram |
3001 | -- non-abstract. We remove the previous declaration, and insert | |
3002 | -- the new one at the point of the renaming, to prevent premature | |
3003 | -- access to unfrozen types. The new declaration reuses the | |
3004 | -- specification of the previous one, and must not be analyzed. | |
3005 | ||
ff81221b ES |
3006 | pragma Assert |
3007 | (Is_Primitive (Entity (Nam)) | |
ac7d724d ES |
3008 | and then |
3009 | Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam)))); | |
d4810530 ES |
3010 | declare |
3011 | Old_Decl : constant Node_Id := | |
3012 | Unit_Declaration_Node (Rename_Spec); | |
3013 | New_Decl : constant Node_Id := | |
3014 | Make_Subprogram_Declaration (Sloc (N), | |
3015 | Specification => | |
3016 | Relocate_Node (Specification (Old_Decl))); | |
3017 | begin | |
3018 | Remove (Old_Decl); | |
3019 | Insert_After (N, New_Decl); | |
16ca248a | 3020 | Set_Is_Abstract_Subprogram (Rename_Spec, False); |
d4810530 ES |
3021 | Set_Analyzed (New_Decl); |
3022 | end; | |
3023 | end if; | |
3024 | ||
996ae0b0 RK |
3025 | Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S); |
3026 | ||
0ab80019 | 3027 | if Ada_Version = Ada_83 and then Comes_From_Source (N) then |
996ae0b0 RK |
3028 | Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N); |
3029 | end if; | |
3030 | ||
923fa078 | 3031 | Set_Convention (New_S, Convention (Rename_Spec)); |
996ae0b0 RK |
3032 | Check_Fully_Conformant (New_S, Rename_Spec); |
3033 | Set_Public_Status (New_S); | |
3034 | ||
e9238cc1 | 3035 | if No_Return (Rename_Spec) |
c99ab5f9 | 3036 | and then not No_Return (Entity (Nam)) |
e9238cc1 AC |
3037 | then |
3038 | Error_Msg_N ("renaming completes a No_Return procedure", N); | |
3039 | Error_Msg_N | |
3040 | ("\renamed procedure must be nonreturning (RM 6.5.1 (7/2))", N); | |
3041 | end if; | |
3042 | ||
294ccb21 RD |
3043 | -- The specification does not introduce new formals, but only |
3044 | -- repeats the formals of the original subprogram declaration. | |
3045 | -- For cross-reference purposes, and for refactoring tools, we | |
3046 | -- treat the formals of the renaming declaration as body formals. | |
3047 | ||
3048 | Reference_Body_Formals (Rename_Spec, New_S); | |
3049 | ||
4de287c4 ES |
3050 | -- Indicate that the entity in the declaration functions like the |
3051 | -- corresponding body, and is not a new entity. The body will be | |
3052 | -- constructed later at the freeze point, so indicate that the | |
3053 | -- completion has not been seen yet. | |
996ae0b0 RK |
3054 | |
3055 | Set_Ekind (New_S, E_Subprogram_Body); | |
3056 | New_S := Rename_Spec; | |
5eb10f25 | 3057 | Set_Has_Completion (Rename_Spec, False); |
996ae0b0 | 3058 | |
4de287c4 | 3059 | -- Ada 2005: check overriding indicator |
edd63e9b | 3060 | |
038140ed | 3061 | if Present (Overridden_Operation (Rename_Spec)) then |
235f4375 AC |
3062 | if Must_Not_Override (Specification (N)) then |
3063 | Error_Msg_NE | |
3064 | ("subprogram& overrides inherited operation", | |
3065 | N, Rename_Spec); | |
d65a80fd HK |
3066 | |
3067 | elsif Style_Check | |
3068 | and then not Must_Override (Specification (N)) | |
235f4375 AC |
3069 | then |
3070 | Style.Missing_Overriding (N, Rename_Spec); | |
3071 | end if; | |
edd63e9b | 3072 | |
235f4375 AC |
3073 | elsif Must_Override (Specification (N)) then |
3074 | Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec); | |
edd63e9b ES |
3075 | end if; |
3076 | ||
294ccb21 RD |
3077 | -- Normal subprogram renaming (not renaming as body) |
3078 | ||
996ae0b0 RK |
3079 | else |
3080 | Generate_Definition (New_S); | |
3081 | New_Overloaded_Entity (New_S); | |
edd63e9b | 3082 | |
19c6e49c PMR |
3083 | if not (Is_Entity_Name (Nam) |
3084 | and then Is_Intrinsic_Subprogram (Entity (Nam))) | |
996ae0b0 | 3085 | then |
996ae0b0 RK |
3086 | Check_Delayed_Subprogram (New_S); |
3087 | end if; | |
19c6e49c PMR |
3088 | |
3089 | -- Verify that a SPARK renaming does not declare a primitive | |
3090 | -- operation of a tagged type. | |
3091 | ||
3092 | Check_SPARK_Primitive_Operation (New_S); | |
996ae0b0 RK |
3093 | end if; |
3094 | ||
4de287c4 ES |
3095 | -- There is no need for elaboration checks on the new entity, which may |
3096 | -- be called before the next freezing point where the body will appear. | |
3097 | -- Elaboration checks refer to the real entity, not the one created by | |
3098 | -- the renaming declaration. | |
996ae0b0 | 3099 | |
ee6208f2 AC |
3100 | Set_Kill_Elaboration_Checks (New_S, True); |
3101 | ||
3102 | -- If we had a previous error, indicate a completely is present to stop | |
3103 | -- junk cascaded messages, but don't take any further action. | |
996ae0b0 RK |
3104 | |
3105 | if Etype (Nam) = Any_Type then | |
3106 | Set_Has_Completion (New_S); | |
3107 | return; | |
3108 | ||
ee6208f2 AC |
3109 | -- Case where name has the form of a selected component |
3110 | ||
996ae0b0 RK |
3111 | elsif Nkind (Nam) = N_Selected_Component then |
3112 | ||
ee6208f2 | 3113 | -- A name which has the form A.B can designate an entry of task A, a |
294ccb21 RD |
3114 | -- protected operation of protected object A, or finally a primitive |
3115 | -- operation of object A. In the later case, A is an object of some | |
3116 | -- tagged type, or an access type that denotes one such. To further | |
3117 | -- distinguish these cases, note that the scope of a task entry or | |
3118 | -- protected operation is type of the prefix. | |
996ae0b0 | 3119 | |
294ccb21 RD |
3120 | -- The prefix could be an overloaded function call that returns both |
3121 | -- kinds of operations. This overloading pathology is left to the | |
3122 | -- dedicated reader ??? | |
3123 | ||
3124 | declare | |
3125 | T : constant Entity_Id := Etype (Prefix (Nam)); | |
3126 | ||
3127 | begin | |
3128 | if Present (T) | |
3129 | and then | |
3130 | (Is_Tagged_Type (T) | |
3131 | or else | |
3132 | (Is_Access_Type (T) | |
ac7d724d | 3133 | and then Is_Tagged_Type (Designated_Type (T)))) |
294ccb21 RD |
3134 | and then Scope (Entity (Selector_Name (Nam))) /= T |
3135 | then | |
3136 | Analyze_Renamed_Primitive_Operation | |
3137 | (N, New_S, Present (Rename_Spec)); | |
3138 | return; | |
3139 | ||
3140 | else | |
3141 | -- Renamed entity is an entry or protected operation. For those | |
3142 | -- cases an explicit body is built (at the point of freezing of | |
3143 | -- this entity) that contains a call to the renamed entity. | |
3144 | ||
3145 | -- This is not allowed for renaming as body if the renamed | |
3146 | -- spec is already frozen (see RM 8.5.4(5) for details). | |
3147 | ||
ac7d724d | 3148 | if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then |
294ccb21 RD |
3149 | Error_Msg_N |
3150 | ("renaming-as-body cannot rename entry as subprogram", N); | |
3151 | Error_Msg_NE | |
3152 | ("\since & is already frozen (RM 8.5.4(5))", | |
3153 | N, Rename_Spec); | |
3154 | else | |
3155 | Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec)); | |
3156 | end if; | |
3157 | ||
3158 | return; | |
3159 | end if; | |
3160 | end; | |
996ae0b0 | 3161 | |
ee6208f2 AC |
3162 | -- Case where name is an explicit dereference X.all |
3163 | ||
996ae0b0 RK |
3164 | elsif Nkind (Nam) = N_Explicit_Dereference then |
3165 | ||
3166 | -- Renamed entity is designated by access_to_subprogram expression. | |
3167 | -- Must build body to encapsulate call, as in the entry case. | |
3168 | ||
3169 | Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec)); | |
3170 | return; | |
3171 | ||
ee6208f2 AC |
3172 | -- Indexed component |
3173 | ||
996ae0b0 RK |
3174 | elsif Nkind (Nam) = N_Indexed_Component then |
3175 | Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec)); | |
3176 | return; | |
3177 | ||
ee6208f2 AC |
3178 | -- Character literal |
3179 | ||
996ae0b0 RK |
3180 | elsif Nkind (Nam) = N_Character_Literal then |
3181 | Analyze_Renamed_Character (N, New_S, Present (Rename_Spec)); | |
3182 | return; | |
3183 | ||
fc193526 HK |
3184 | -- Only remaining case is where we have a non-entity name, or a renaming |
3185 | -- of some other non-overloadable entity. | |
ee6208f2 | 3186 | |
a3f2babd | 3187 | elsif not Is_Entity_Name (Nam) |
996ae0b0 RK |
3188 | or else not Is_Overloadable (Entity (Nam)) |
3189 | then | |
4674dd21 AC |
3190 | -- Do not mention the renaming if it comes from an instance |
3191 | ||
3192 | if not Is_Actual then | |
3193 | Error_Msg_N ("expect valid subprogram name in renaming", N); | |
4674dd21 AC |
3194 | else |
3195 | Error_Msg_NE ("no visible subprogram for formal&", N, Nam); | |
3196 | end if; | |
3197 | ||
996ae0b0 | 3198 | return; |
996ae0b0 RK |
3199 | end if; |
3200 | ||
11560bcc TQ |
3201 | -- Find the renamed entity that matches the given specification. Disable |
3202 | -- Ada_83 because there is no requirement of full conformance between | |
3203 | -- renamed entity and new entity, even though the same circuit is used. | |
3204 | ||
e80f0cb0 RD |
3205 | -- This is a bit of an odd case, which introduces a really irregular use |
3206 | -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do | |
3207 | -- this. ??? | |
11560bcc TQ |
3208 | |
3209 | Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95); | |
fb620b37 | 3210 | Ada_Version_Pragma := Empty; |
11560bcc TQ |
3211 | Ada_Version_Explicit := Ada_Version; |
3212 | ||
3213 | if No (Old_S) then | |
3214 | Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual); | |
3215 | ||
1366997b AC |
3216 | -- The visible operation may be an inherited abstract operation that |
3217 | -- was overridden in the private part, in which case a call will | |
3218 | -- dispatch to the overriding operation. Use the overriding one in | |
3219 | -- the renaming declaration, to prevent spurious errors below. | |
3220 | ||
3221 | if Is_Overloadable (Old_S) | |
3222 | and then Is_Abstract_Subprogram (Old_S) | |
3223 | and then No (DTC_Entity (Old_S)) | |
3224 | and then Present (Alias (Old_S)) | |
3225 | and then not Is_Abstract_Subprogram (Alias (Old_S)) | |
038140ed | 3226 | and then Present (Overridden_Operation (Alias (Old_S))) |
1366997b AC |
3227 | then |
3228 | Old_S := Alias (Old_S); | |
3229 | end if; | |
3230 | ||
11560bcc TQ |
3231 | -- When the renamed subprogram is overloaded and used as an actual |
3232 | -- of a generic, its entity is set to the first available homonym. | |
3233 | -- We must first disambiguate the name, then set the proper entity. | |
3234 | ||
1378bf10 | 3235 | if Is_Actual and then Is_Overloaded (Nam) then |
11560bcc TQ |
3236 | Set_Entity (Nam, Old_S); |
3237 | end if; | |
3238 | end if; | |
3239 | ||
4de287c4 ES |
3240 | -- Most common case: subprogram renames subprogram. No body is generated |
3241 | -- in this case, so we must indicate the declaration is complete as is. | |
b2c4d56d | 3242 | -- and inherit various attributes of the renamed subprogram. |
996ae0b0 RK |
3243 | |
3244 | if No (Rename_Spec) then | |
923fa078 | 3245 | Set_Has_Completion (New_S); |
b2c4d56d | 3246 | Set_Is_Imported (New_S, Is_Imported (Entity (Nam))); |
923fa078 RD |
3247 | Set_Is_Pure (New_S, Is_Pure (Entity (Nam))); |
3248 | Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam))); | |
3249 | ||
3250 | -- Ada 2005 (AI-423): Check the consistency of null exclusions | |
11560bcc | 3251 | -- between a subprogram and its correct renaming. |
923fa078 | 3252 | |
11560bcc TQ |
3253 | -- Note: the Any_Id check is a guard that prevents compiler crashes |
3254 | -- when performing a null exclusion check between a renaming and a | |
3255 | -- renamed subprogram that has been found to be illegal. | |
3256 | ||
ac7d724d | 3257 | if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then |
923fa078 RD |
3258 | Check_Null_Exclusion |
3259 | (Ren => New_S, | |
3260 | Sub => Entity (Nam)); | |
3261 | end if; | |
90067a15 ES |
3262 | |
3263 | -- Enforce the Ada 2005 rule that the renamed entity cannot require | |
3264 | -- overriding. The flag Requires_Overriding is set very selectively | |
3265 | -- and misses some other illegal cases. The additional conditions | |
3266 | -- checked below are sufficient but not necessary ??? | |
3267 | ||
3268 | -- The rule does not apply to the renaming generated for an actual | |
3269 | -- subprogram in an instance. | |
3270 | ||
3271 | if Is_Actual then | |
3272 | null; | |
3273 | ||
f3d57416 | 3274 | -- Guard against previous errors, and omit renamings of predefined |
90067a15 ES |
3275 | -- operators. |
3276 | ||
bce79204 | 3277 | elsif not Ekind_In (Old_S, E_Function, E_Procedure) then |
90067a15 ES |
3278 | null; |
3279 | ||
3280 | elsif Requires_Overriding (Old_S) | |
3281 | or else | |
19c6e49c PMR |
3282 | (Is_Abstract_Subprogram (Old_S) |
3283 | and then Present (Find_Dispatching_Type (Old_S)) | |
3284 | and then not Is_Abstract_Type (Find_Dispatching_Type (Old_S))) | |
90067a15 ES |
3285 | then |
3286 | Error_Msg_N | |
d65a80fd HK |
3287 | ("renamed entity cannot be subprogram that requires overriding " |
3288 | & "(RM 8.5.4 (5.1))", N); | |
90067a15 | 3289 | end if; |
321c24f7 AC |
3290 | |
3291 | declare | |
3292 | Prev : constant Entity_Id := Overridden_Operation (New_S); | |
3293 | begin | |
3294 | if Present (Prev) | |
3295 | and then | |
8f34c90b AC |
3296 | (Has_Non_Trivial_Precondition (Prev) |
3297 | or else Has_Non_Trivial_Precondition (Old_S)) | |
321c24f7 | 3298 | then |
8f34c90b AC |
3299 | Error_Msg_NE |
3300 | ("conflicting inherited classwide preconditions in renaming " | |
3301 | & "of& (RM 6.1.1 (17)", N, Old_S); | |
321c24f7 AC |
3302 | end if; |
3303 | end; | |
996ae0b0 RK |
3304 | end if; |
3305 | ||
996ae0b0 | 3306 | if Old_S /= Any_Id then |
1378bf10 | 3307 | if Is_Actual and then From_Default (N) then |
ff4f0ed0 | 3308 | |
996ae0b0 RK |
3309 | -- This is an implicit reference to the default actual |
3310 | ||
3311 | Generate_Reference (Old_S, Nam, Typ => 'i', Force => True); | |
ff4f0ed0 | 3312 | |
996ae0b0 RK |
3313 | else |
3314 | Generate_Reference (Old_S, Nam); | |
3315 | end if; | |
3316 | ||
c92e8586 AC |
3317 | Check_Internal_Protected_Use (N, Old_S); |
3318 | ||
edd63e9b ES |
3319 | -- For a renaming-as-body, require subtype conformance, but if the |
3320 | -- declaration being completed has not been frozen, then inherit the | |
3321 | -- convention of the renamed subprogram prior to checking conformance | |
3322 | -- (unless the renaming has an explicit convention established; the | |
996ae0b0 RK |
3323 | -- rule stated in the RM doesn't seem to address this ???). |
3324 | ||
3325 | if Present (Rename_Spec) then | |
3326 | Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b'); | |
3327 | Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec); | |
3328 | ||
07fc65c4 GB |
3329 | if not Is_Frozen (Rename_Spec) then |
3330 | if not Has_Convention_Pragma (Rename_Spec) then | |
3331 | Set_Convention (New_S, Convention (Old_S)); | |
3332 | end if; | |
3333 | ||
3334 | if Ekind (Old_S) /= E_Operator then | |
3335 | Check_Mode_Conformant (New_S, Old_S, Spec); | |
3336 | end if; | |
3337 | ||
3338 | if Original_Subprogram (Old_S) = Rename_Spec then | |
3339 | Error_Msg_N ("unfrozen subprogram cannot rename itself ", N); | |
3340 | end if; | |
3341 | else | |
3342 | Check_Subtype_Conformant (New_S, Old_S, Spec); | |
996ae0b0 RK |
3343 | end if; |
3344 | ||
3345 | Check_Frozen_Renaming (N, Rename_Spec); | |
996ae0b0 | 3346 | |
5eb10f25 | 3347 | -- Check explicitly that renamed entity is not intrinsic, because |
16b05213 | 3348 | -- in a generic the renamed body is not built. In this case, |
5eb10f25 ES |
3349 | -- the renaming_as_body is a completion. |
3350 | ||
3351 | if Inside_A_Generic then | |
3352 | if Is_Frozen (Rename_Spec) | |
3353 | and then Is_Intrinsic_Subprogram (Old_S) | |
3354 | then | |
3355 | Error_Msg_N | |
3356 | ("subprogram in renaming_as_body cannot be intrinsic", | |
d65a80fd | 3357 | Name (N)); |
5eb10f25 ES |
3358 | end if; |
3359 | ||
3360 | Set_Has_Completion (Rename_Spec); | |
3361 | end if; | |
3362 | ||
996ae0b0 | 3363 | elsif Ekind (Old_S) /= E_Operator then |
1138cf59 | 3364 | |
2ba431e5 YM |
3365 | -- If this a defaulted subprogram for a class-wide actual there is |
3366 | -- no check for mode conformance, given that the signatures don't | |
3e24afaa | 3367 | -- match (the source mentions T but the actual mentions T'Class). |
1138cf59 | 3368 | |
afc8324d | 3369 | if CW_Actual then |
1138cf59 | 3370 | null; |
9e42b192 ES |
3371 | |
3372 | -- No need for a redundant error message if this is a nested | |
3373 | -- instance, unless the current instantiation (of a child unit) | |
3374 | -- is a compilation unit, which is not analyzed when the parent | |
3375 | -- generic is analyzed. | |
3376 | ||
3377 | elsif not Is_Actual | |
3378 | or else No (Enclosing_Instance) | |
3379 | or else Is_Compilation_Unit (Current_Scope) | |
3380 | then | |
1138cf59 AC |
3381 | Check_Mode_Conformant (New_S, Old_S); |
3382 | end if; | |
996ae0b0 | 3383 | |
ac7d724d | 3384 | if Is_Actual and then Error_Posted (New_S) then |
996ae0b0 RK |
3385 | Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S); |
3386 | end if; | |
3387 | end if; | |
3388 | ||
3389 | if No (Rename_Spec) then | |
3390 | ||
3391 | -- The parameter profile of the new entity is that of the renamed | |
3392 | -- entity: the subtypes given in the specification are irrelevant. | |
3393 | ||
3394 | Inherit_Renamed_Profile (New_S, Old_S); | |
3395 | ||
3396 | -- A call to the subprogram is transformed into a call to the | |
3397 | -- renamed entity. This is transitive if the renamed entity is | |
3398 | -- itself a renaming. | |
3399 | ||
3400 | if Present (Alias (Old_S)) then | |
3401 | Set_Alias (New_S, Alias (Old_S)); | |
3402 | else | |
3403 | Set_Alias (New_S, Old_S); | |
3404 | end if; | |
3405 | ||
edd63e9b ES |
3406 | -- Note that we do not set Is_Intrinsic_Subprogram if we have a |
3407 | -- renaming as body, since the entity in this case is not an | |
3408 | -- intrinsic (it calls an intrinsic, but we have a real body for | |
3409 | -- this call, and it is in this body that the required intrinsic | |
3410 | -- processing will take place). | |
996ae0b0 | 3411 | |
edd63e9b ES |
3412 | -- Also, if this is a renaming of inequality, the renamed operator |
3413 | -- is intrinsic, but what matters is the corresponding equality | |
3414 | -- operator, which may be user-defined. | |
fbf5a39b | 3415 | |
996ae0b0 | 3416 | Set_Is_Intrinsic_Subprogram |
fbf5a39b | 3417 | (New_S, |
ac7d724d ES |
3418 | Is_Intrinsic_Subprogram (Old_S) |
3419 | and then | |
3420 | (Chars (Old_S) /= Name_Op_Ne | |
3421 | or else Ekind (Old_S) = E_Operator | |
3422 | or else Is_Intrinsic_Subprogram | |
3423 | (Corresponding_Equality (Old_S)))); | |
996ae0b0 RK |
3424 | |
3425 | if Ekind (Alias (New_S)) = E_Operator then | |
3426 | Set_Has_Delayed_Freeze (New_S, False); | |
3427 | end if; | |
3428 | ||
82c80734 RD |
3429 | -- If the renaming corresponds to an association for an abstract |
3430 | -- formal subprogram, then various attributes must be set to | |
3431 | -- indicate that the renaming is an abstract dispatching operation | |
3432 | -- with a controlling type. | |
3433 | ||
16ca248a ES |
3434 | if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then |
3435 | ||
82c80734 RD |
3436 | -- Mark the renaming as abstract here, so Find_Dispatching_Type |
3437 | -- see it as corresponding to a generic association for a | |
3438 | -- formal abstract subprogram | |
3439 | ||
16ca248a | 3440 | Set_Is_Abstract_Subprogram (New_S); |
82c80734 RD |
3441 | |
3442 | declare | |
3443 | New_S_Ctrl_Type : constant Entity_Id := | |
3444 | Find_Dispatching_Type (New_S); | |
3445 | Old_S_Ctrl_Type : constant Entity_Id := | |
3446 | Find_Dispatching_Type (Old_S); | |
3447 | ||
3448 | begin | |
46ee0270 AC |
3449 | |
3450 | -- The actual must match the (instance of the) formal, | |
3451 | -- and must be a controlling type. | |
3452 | ||
3453 | if Old_S_Ctrl_Type /= New_S_Ctrl_Type | |
3454 | or else No (New_S_Ctrl_Type) | |
3455 | then | |
82c80734 RD |
3456 | Error_Msg_NE |
3457 | ("actual must be dispatching subprogram for type&", | |
3458 | Nam, New_S_Ctrl_Type); | |
3459 | ||
3460 | else | |
3461 | Set_Is_Dispatching_Operation (New_S); | |
3462 | Check_Controlling_Formals (New_S_Ctrl_Type, New_S); | |
3463 | ||
16ca248a ES |
3464 | -- If the actual in the formal subprogram is itself a |
3465 | -- formal abstract subprogram association, there's no | |
3466 | -- dispatch table component or position to inherit. | |
82c80734 RD |
3467 | |
3468 | if Present (DTC_Entity (Old_S)) then | |
3469 | Set_DTC_Entity (New_S, DTC_Entity (Old_S)); | |
024d33d8 | 3470 | Set_DT_Position_Value (New_S, DT_Position (Old_S)); |
82c80734 RD |
3471 | end if; |
3472 | end if; | |
3473 | end; | |
3474 | end if; | |
996ae0b0 RK |
3475 | end if; |
3476 | ||
c91dbd18 AC |
3477 | if Is_Actual then |
3478 | null; | |
3479 | ||
3480 | -- The following is illegal, because F hides whatever other F may | |
3481 | -- be around: | |
a1390a6e | 3482 | -- function F (...) renames F; |
c91dbd18 AC |
3483 | |
3484 | elsif Old_S = New_S | |
3485 | or else (Nkind (Nam) /= N_Expanded_Name | |
3486 | and then Chars (Old_S) = Chars (New_S)) | |
3487 | then | |
3488 | Error_Msg_N ("subprogram cannot rename itself", N); | |
3489 | ||
a1390a6e AC |
3490 | -- This is illegal even if we use a selector: |
3491 | -- function F (...) renames Pkg.F; | |
3492 | -- because F is still hidden. | |
3493 | ||
c91dbd18 AC |
3494 | elsif Nkind (Nam) = N_Expanded_Name |
3495 | and then Entity (Prefix (Nam)) = Current_Scope | |
3496 | and then Chars (Selector_Name (Nam)) = Chars (New_S) | |
996ae0b0 | 3497 | then |
354c3840 AC |
3498 | -- This is an error, but we overlook the error and accept the |
3499 | -- renaming if the special Overriding_Renamings mode is in effect. | |
9b7424a7 | 3500 | |
354c3840 | 3501 | if not Overriding_Renamings then |
9b7424a7 | 3502 | Error_Msg_NE |
354c3840 AC |
3503 | ("implicit operation& is not visible (RM 8.3 (15))", |
3504 | Nam, Old_S); | |
9b7424a7 | 3505 | end if; |
996ae0b0 RK |
3506 | end if; |
3507 | ||
3508 | Set_Convention (New_S, Convention (Old_S)); | |
16ca248a ES |
3509 | |
3510 | if Is_Abstract_Subprogram (Old_S) then | |
3511 | if Present (Rename_Spec) then | |
3512 | Error_Msg_N | |
3513 | ("a renaming-as-body cannot rename an abstract subprogram", | |
3514 | N); | |
3515 | Set_Has_Completion (Rename_Spec); | |
3516 | else | |
3517 | Set_Is_Abstract_Subprogram (New_S); | |
3518 | end if; | |
3519 | end if; | |
3520 | ||
996ae0b0 RK |
3521 | Check_Library_Unit_Renaming (N, Old_S); |
3522 | ||
edd63e9b ES |
3523 | -- Pathological case: procedure renames entry in the scope of its |
3524 | -- task. Entry is given by simple name, but body must be built for | |
3525 | -- procedure. Of course if called it will deadlock. | |
996ae0b0 RK |
3526 | |
3527 | if Ekind (Old_S) = E_Entry then | |
3528 | Set_Has_Completion (New_S, False); | |
3529 | Set_Alias (New_S, Empty); | |
3530 | end if; | |
3531 | ||
66340e0e AC |
3532 | -- Do not freeze the renaming nor the renamed entity when the context |
3533 | -- is an enclosing generic. Freezing is an expansion activity, and in | |
3534 | -- addition the renamed entity may depend on the generic formals of | |
3535 | -- the enclosing generic. | |
3536 | ||
851e9f19 | 3537 | if Is_Actual and not Inside_A_Generic then |
996ae0b0 | 3538 | Freeze_Before (N, Old_S); |
02e4edea | 3539 | Freeze_Actual_Profile; |
996ae0b0 RK |
3540 | Set_Has_Delayed_Freeze (New_S, False); |
3541 | Freeze_Before (N, New_S); | |
3542 | ||
82c80734 RD |
3543 | -- An abstract subprogram is only allowed as an actual in the case |
3544 | -- where the formal subprogram is also abstract. | |
3545 | ||
996ae0b0 | 3546 | if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function) |
16ca248a ES |
3547 | and then Is_Abstract_Subprogram (Old_S) |
3548 | and then not Is_Abstract_Subprogram (Formal_Spec) | |
996ae0b0 RK |
3549 | then |
3550 | Error_Msg_N | |
3551 | ("abstract subprogram not allowed as generic actual", Nam); | |
3552 | end if; | |
3553 | end if; | |
3554 | ||
3555 | else | |
edd63e9b ES |
3556 | -- A common error is to assume that implicit operators for types are |
3557 | -- defined in Standard, or in the scope of a subtype. In those cases | |
3558 | -- where the renamed entity is given with an expanded name, it is | |
3559 | -- worth mentioning that operators for the type are not declared in | |
3560 | -- the scope given by the prefix. | |
996ae0b0 RK |
3561 | |
3562 | if Nkind (Nam) = N_Expanded_Name | |
3563 | and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol | |
3564 | and then Scope (Entity (Nam)) = Standard_Standard | |
3565 | then | |
3566 | declare | |
3567 | T : constant Entity_Id := | |
3568 | Base_Type (Etype (First_Formal (New_S))); | |
996ae0b0 RK |
3569 | begin |
3570 | Error_Msg_Node_2 := Prefix (Nam); | |
fbf5a39b AC |
3571 | Error_Msg_NE |
3572 | ("operator for type& is not declared in&", Prefix (Nam), T); | |
996ae0b0 | 3573 | end; |
fbf5a39b | 3574 | |
996ae0b0 RK |
3575 | else |
3576 | Error_Msg_NE | |
3577 | ("no visible subprogram matches the specification for&", | |
3578 | Spec, New_S); | |
3579 | end if; | |
3580 | ||
3581 | if Present (Candidate_Renaming) then | |
3582 | declare | |
3583 | F1 : Entity_Id; | |
3584 | F2 : Entity_Id; | |
70b70ce8 | 3585 | T1 : Entity_Id; |
996ae0b0 RK |
3586 | |
3587 | begin | |
3588 | F1 := First_Formal (Candidate_Renaming); | |
3589 | F2 := First_Formal (New_S); | |
70b70ce8 | 3590 | T1 := First_Subtype (Etype (F1)); |
996ae0b0 RK |
3591 | while Present (F1) and then Present (F2) loop |
3592 | Next_Formal (F1); | |
3593 | Next_Formal (F2); | |
3594 | end loop; | |
3595 | ||
3596 | if Present (F1) and then Present (Default_Value (F1)) then | |
3597 | if Present (Next_Formal (F1)) then | |
3598 | Error_Msg_NE | |
241ebe89 HK |
3599 | ("\missing specification for & and other formals with " |
3600 | & "defaults", Spec, F1); | |
996ae0b0 | 3601 | else |
241ebe89 | 3602 | Error_Msg_NE ("\missing specification for &", Spec, F1); |
996ae0b0 RK |
3603 | end if; |
3604 | end if; | |
70b70ce8 AC |
3605 | |
3606 | if Nkind (Nam) = N_Operator_Symbol | |
3607 | and then From_Default (N) | |
3608 | then | |
3609 | Error_Msg_Node_2 := T1; | |
3610 | Error_Msg_NE | |
d65a80fd | 3611 | ("default & on & is not directly visible", Nam, Nam); |
70b70ce8 | 3612 | end if; |
996ae0b0 RK |
3613 | end; |
3614 | end if; | |
3615 | end if; | |
3616 | ||
edd63e9b ES |
3617 | -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that |
3618 | -- controlling access parameters are known non-null for the renamed | |
3619 | -- subprogram. Test also applies to a subprogram instantiation that | |
cdc8c54c BD |
3620 | -- is dispatching. Test is skipped if some previous error was detected |
3621 | -- that set Old_S to Any_Id. | |
edd63e9b | 3622 | |
0791fbe9 | 3623 | if Ada_Version >= Ada_2005 |
cdc8c54c | 3624 | and then Old_S /= Any_Id |
edd63e9b ES |
3625 | and then not Is_Dispatching_Operation (Old_S) |
3626 | and then Is_Dispatching_Operation (New_S) | |
3627 | then | |
3628 | declare | |
3629 | Old_F : Entity_Id; | |
3630 | New_F : Entity_Id; | |
3631 | ||
3632 | begin | |
3633 | Old_F := First_Formal (Old_S); | |
3634 | New_F := First_Formal (New_S); | |
3635 | while Present (Old_F) loop | |
3636 | if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type | |
3637 | and then Is_Controlling_Formal (New_F) | |
3638 | and then not Can_Never_Be_Null (Old_F) | |
3639 | then | |
3640 | Error_Msg_N ("access parameter is controlling,", New_F); | |
fbe627af | 3641 | Error_Msg_NE |
d65a80fd HK |
3642 | ("\corresponding parameter of& must be explicitly null " |
3643 | & "excluding", New_F, Old_S); | |
edd63e9b ES |
3644 | end if; |
3645 | ||
3646 | Next_Formal (Old_F); | |
3647 | Next_Formal (New_F); | |
3648 | end loop; | |
3649 | end; | |
3650 | end if; | |
3651 | ||
725393ea | 3652 | -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005) |
b5c739f9 | 3653 | -- is to warn if an operator is being renamed as a different operator. |
880dabb5 AC |
3654 | -- If the operator is predefined, examine the kind of the entity, not |
3655 | -- the abbreviated declaration in Standard. | |
725393ea ES |
3656 | |
3657 | if Comes_From_Source (N) | |
3658 | and then Present (Old_S) | |
ac7d724d ES |
3659 | and then (Nkind (Old_S) = N_Defining_Operator_Symbol |
3660 | or else Ekind (Old_S) = E_Operator) | |
725393ea ES |
3661 | and then Nkind (New_S) = N_Defining_Operator_Symbol |
3662 | and then Chars (Old_S) /= Chars (New_S) | |
3663 | then | |
3664 | Error_Msg_NE | |
dbfeb4fa | 3665 | ("& is being renamed as a different operator??", N, Old_S); |
725393ea ES |
3666 | end if; |
3667 | ||
b5c739f9 RD |
3668 | -- Check for renaming of obsolescent subprogram |
3669 | ||
3670 | Check_Obsolescent_2005_Entity (Entity (Nam), Nam); | |
3671 | ||
11560bcc TQ |
3672 | -- Another warning or some utility: if the new subprogram as the same |
3673 | -- name as the old one, the old one is not hidden by an outer homograph, | |
3674 | -- the new one is not a public symbol, and the old one is otherwise | |
3675 | -- directly visible, the renaming is superfluous. | |
3676 | ||
3677 | if Chars (Old_S) = Chars (New_S) | |
3678 | and then Comes_From_Source (N) | |
3679 | and then Scope (Old_S) /= Standard_Standard | |
3680 | and then Warn_On_Redundant_Constructs | |
ac7d724d ES |
3681 | and then (Is_Immediately_Visible (Old_S) |
3682 | or else Is_Potentially_Use_Visible (Old_S)) | |
11560bcc TQ |
3683 | and then Is_Overloadable (Current_Scope) |
3684 | and then Chars (Current_Scope) /= Chars (Old_S) | |
3685 | then | |
3686 | Error_Msg_N | |
241ebe89 | 3687 | ("redundant renaming, entity is directly visible?r?", Name (N)); |
11560bcc TQ |
3688 | end if; |
3689 | ||
ef992452 | 3690 | -- Implementation-defined aspect specifications can appear in a renaming |
cc96a1b8 AC |
3691 | -- declaration, but not language-defined ones. The call to procedure |
3692 | -- Analyze_Aspect_Specifications will take care of this error check. | |
ef992452 AC |
3693 | |
3694 | if Has_Aspects (N) then | |
3695 | Analyze_Aspect_Specifications (N, New_S); | |
3696 | end if; | |
3697 | ||
0ab80019 | 3698 | Ada_Version := Save_AV; |
fb620b37 | 3699 | Ada_Version_Pragma := Save_AVP; |
edd63e9b | 3700 | Ada_Version_Explicit := Save_AV_Exp; |
948ed277 AC |
3701 | |
3702 | -- In GNATprove mode, the renamings of actual subprograms are replaced | |
3703 | -- with wrapper functions that make it easier to propagate axioms to the | |
a1954e97 AC |
3704 | -- points of call within an instance. Wrappers are generated if formal |
3705 | -- subprogram is subject to axiomatization. | |
948ed277 | 3706 | |
1a779058 AC |
3707 | -- The types in the wrapper profiles are obtained from (instances of) |
3708 | -- the types of the formal subprogram. | |
3709 | ||
948ed277 AC |
3710 | if Is_Actual |
3711 | and then GNATprove_Mode | |
a1954e97 | 3712 | and then Present (Containing_Package_With_Ext_Axioms (Formal_Spec)) |
948ed277 AC |
3713 | and then not Inside_A_Generic |
3714 | then | |
3715 | if Ekind (Old_S) = E_Function then | |
1a779058 | 3716 | Rewrite (N, Build_Function_Wrapper (Formal_Spec, Old_S)); |
948ed277 | 3717 | Analyze (N); |
fc6d9796 | 3718 | |
948ed277 | 3719 | elsif Ekind (Old_S) = E_Operator then |
fc6d9796 | 3720 | Rewrite (N, Build_Operator_Wrapper (Formal_Spec, Old_S)); |
948ed277 AC |
3721 | Analyze (N); |
3722 | end if; | |
3723 | end if; | |
851e9f19 PMR |
3724 | |
3725 | -- Check if we are looking at an Ada 2012 defaulted formal subprogram | |
3726 | -- and mark any use_package_clauses that affect the visibility of the | |
3727 | -- implicit generic actual. | |
3728 | ||
0ebf09ed JS |
3729 | -- Also, we may be looking at an internal renaming of a user-defined |
3730 | -- subprogram created for a generic formal subprogram association, | |
3731 | -- which will also have to be marked here. This can occur when the | |
3732 | -- corresponding formal subprogram contains references to other generic | |
3733 | -- formals. | |
3734 | ||
2e60feb5 | 3735 | if Is_Generic_Actual_Subprogram (New_S) |
0ebf09ed JS |
3736 | and then (Is_Intrinsic_Subprogram (New_S) |
3737 | or else From_Default (N) | |
3738 | or else Nkind (N) = N_Subprogram_Renaming_Declaration) | |
851e9f19 | 3739 | then |
2e60feb5 | 3740 | Mark_Use_Clauses (New_S); |
851e9f19 | 3741 | |
2e60feb5 | 3742 | -- Handle overloaded subprograms |
851e9f19 | 3743 | |
2e60feb5 PMR |
3744 | if Present (Alias (New_S)) then |
3745 | Mark_Use_Clauses (Alias (New_S)); | |
3746 | end if; | |
851e9f19 | 3747 | end if; |
996ae0b0 RK |
3748 | end Analyze_Subprogram_Renaming; |
3749 | ||
3750 | ------------------------- | |
3751 | -- Analyze_Use_Package -- | |
3752 | ------------------------- | |
3753 | ||
3754 | -- Resolve the package names in the use clause, and make all the visible | |
3755 | -- entities defined in the package potentially use-visible. If the package | |
3756 | -- is already in use from a previous use clause, its visible entities are | |
3757 | -- already use-visible. In that case, mark the occurrence as a redundant | |
3758 | -- use. If the package is an open scope, i.e. if the use clause occurs | |
3759 | -- within the package itself, ignore it. | |
3760 | ||
851e9f19 | 3761 | procedure Analyze_Use_Package (N : Node_Id; Chain : Boolean := True) is |
851e9f19 PMR |
3762 | procedure Analyze_Package_Name (Clause : Node_Id); |
3763 | -- Perform analysis on a package name from a use_package_clause | |
3764 | ||
3765 | procedure Analyze_Package_Name_List (Head_Clause : Node_Id); | |
3766 | -- Similar to Analyze_Package_Name but iterates over all the names | |
3767 | -- in a use clause. | |
3768 | ||
3769 | -------------------------- | |
3770 | -- Analyze_Package_Name -- | |
3771 | -------------------------- | |
3772 | ||
3773 | procedure Analyze_Package_Name (Clause : Node_Id) is | |
3774 | Pack : constant Node_Id := Name (Clause); | |
3775 | Pref : Node_Id; | |
3776 | ||
3777 | begin | |
3778 | pragma Assert (Nkind (Clause) = N_Use_Package_Clause); | |
3779 | Analyze (Pack); | |
3780 | ||
3781 | -- Verify that the package standard is not directly named in a | |
3782 | -- use_package_clause. | |
3783 | ||
3784 | if Nkind (Parent (Clause)) = N_Compilation_Unit | |
3785 | and then Nkind (Pack) = N_Expanded_Name | |
3786 | then | |
3787 | Pref := Prefix (Pack); | |
3788 | ||
3789 | while Nkind (Pref) = N_Expanded_Name loop | |
3790 | Pref := Prefix (Pref); | |
3791 | end loop; | |
3792 | ||
3793 | if Entity (Pref) = Standard_Standard then | |
3794 | Error_Msg_N | |
7f5e671b PMR |
3795 | ("predefined package Standard cannot appear in a context " |
3796 | & "clause", Pref); | |
851e9f19 PMR |
3797 | end if; |
3798 | end if; | |
3799 | end Analyze_Package_Name; | |
3800 | ||
3801 | ------------------------------- | |
3802 | -- Analyze_Package_Name_List -- | |
3803 | ------------------------------- | |
3804 | ||
3805 | procedure Analyze_Package_Name_List (Head_Clause : Node_Id) is | |
3806 | Curr : Node_Id; | |
3807 | ||
3808 | begin | |
3809 | -- Due to the way source use clauses are split during parsing we are | |
3810 | -- forced to simply iterate through all entities in scope until the | |
3811 | -- clause representing the last name in the list is found. | |
3812 | ||
3813 | Curr := Head_Clause; | |
3814 | while Present (Curr) loop | |
3815 | Analyze_Package_Name (Curr); | |
3816 | ||
3817 | -- Stop iterating over the names in the use clause when we are at | |
3818 | -- the last one. | |
3819 | ||
3820 | exit when not More_Ids (Curr) and then Prev_Ids (Curr); | |
3821 | Next (Curr); | |
3822 | end loop; | |
3823 | end Analyze_Package_Name_List; | |
3824 | ||
3825 | -- Local variables | |
3826 | ||
980f94b7 | 3827 | Pack : Entity_Id; |
851e9f19 PMR |
3828 | |
3829 | -- Start of processing for Analyze_Use_Package | |
996ae0b0 RK |
3830 | |
3831 | begin | |
ce5ba43a | 3832 | Check_SPARK_05_Restriction ("use clause is not allowed", N); |
1d801f21 | 3833 | |
996ae0b0 RK |
3834 | Set_Hidden_By_Use_Clause (N, No_Elist); |
3835 | ||
7394c8cc AC |
3836 | -- Use clause not allowed in a spec of a predefined package declaration |
3837 | -- except that packages whose file name starts a-n are OK (these are | |
3838 | -- children of Ada.Numerics, which are never loaded by Rtsfind). | |
996ae0b0 | 3839 | |
8ab31c0c | 3840 | if Is_Predefined_Unit (Current_Sem_Unit) |
b043ae01 | 3841 | and then Get_Name_String |
94d3a18d AC |
3842 | (Unit_File_Name (Current_Sem_Unit)) (1 .. 3) /= "a-n" |
3843 | and then Nkind (Unit (Cunit (Current_Sem_Unit))) = | |
3844 | N_Package_Declaration | |
996ae0b0 RK |
3845 | then |
3846 | Error_Msg_N ("use clause not allowed in predefined spec", N); | |
3847 | end if; | |
3848 | ||
851e9f19 PMR |
3849 | -- Loop through all package names from the original use clause in |
3850 | -- order to analyze referenced packages. A use_package_clause with only | |
3851 | -- one name does not have More_Ids or Prev_Ids set, while a clause with | |
3852 | -- More_Ids only starts the chain produced by the parser. | |
996ae0b0 | 3853 | |
851e9f19 PMR |
3854 | if not More_Ids (N) and then not Prev_Ids (N) then |
3855 | Analyze_Package_Name (N); | |
7f5e671b | 3856 | |
851e9f19 PMR |
3857 | elsif More_Ids (N) and then not Prev_Ids (N) then |
3858 | Analyze_Package_Name_List (N); | |
996ae0b0 RK |
3859 | end if; |
3860 | ||
851e9f19 PMR |
3861 | if not Is_Entity_Name (Name (N)) then |
3862 | Error_Msg_N ("& is not a package", Name (N)); | |
996ae0b0 | 3863 | |
851e9f19 PMR |
3864 | return; |
3865 | end if; | |
996ae0b0 | 3866 | |
851e9f19 PMR |
3867 | if Chain then |
3868 | Chain_Use_Clause (N); | |
3869 | end if; | |
996ae0b0 | 3870 | |
7f5e671b PMR |
3871 | Pack := Entity (Name (N)); |
3872 | ||
851e9f19 PMR |
3873 | -- There are many cases where scopes are manipulated during analysis, so |
3874 | -- check that Pack's current use clause has not already been chained | |
3875 | -- before setting its previous use clause. | |
996ae0b0 | 3876 | |
851e9f19 | 3877 | if Ekind (Pack) = E_Package |
ccd0ed95 PMR |
3878 | and then Present (Current_Use_Clause (Pack)) |
3879 | and then Current_Use_Clause (Pack) /= N | |
3880 | and then No (Prev_Use_Clause (N)) | |
3881 | and then Prev_Use_Clause (Current_Use_Clause (Pack)) /= N | |
851e9f19 PMR |
3882 | then |
3883 | Set_Prev_Use_Clause (N, Current_Use_Clause (Pack)); | |
3884 | end if; | |
996ae0b0 | 3885 | |
851e9f19 | 3886 | -- Mark all entities as potentially use visible. |
0566484a | 3887 | |
851e9f19 PMR |
3888 | if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then |
3889 | if Ekind (Pack) = E_Generic_Package then | |
3890 | Error_Msg_N -- CODEFIX | |
7f5e671b | 3891 | ("a generic package is not allowed in a use clause", Name (N)); |
0566484a | 3892 | |
851e9f19 PMR |
3893 | elsif Ekind_In (Pack, E_Generic_Function, E_Generic_Package) |
3894 | then | |
3895 | Error_Msg_N -- CODEFIX | |
3896 | ("a generic subprogram is not allowed in a use clause", | |
3897 | Name (N)); | |
0566484a | 3898 | |
851e9f19 PMR |
3899 | elsif Ekind_In (Pack, E_Function, E_Procedure, E_Operator) then |
3900 | Error_Msg_N -- CODEFIX | |
7f5e671b | 3901 | ("a subprogram is not allowed in a use clause", Name (N)); |
996ae0b0 | 3902 | |
851e9f19 PMR |
3903 | else |
3904 | Error_Msg_N ("& is not allowed in a use clause", Name (N)); | |
3905 | end if; | |
996ae0b0 | 3906 | |
851e9f19 PMR |
3907 | else |
3908 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
3909 | Check_In_Previous_With_Clause (N, Name (N)); | |
3910 | end if; | |
6e9e35e1 | 3911 | |
614bc51c | 3912 | Use_One_Package (N, Name (N)); |
851e9f19 | 3913 | end if; |
980f94b7 HK |
3914 | |
3915 | Mark_Ghost_Clause (N); | |
996ae0b0 RK |
3916 | end Analyze_Use_Package; |
3917 | ||
3918 | ---------------------- | |
3919 | -- Analyze_Use_Type -- | |
3920 | ---------------------- | |
3921 | ||
851e9f19 PMR |
3922 | procedure Analyze_Use_Type (N : Node_Id; Chain : Boolean := True) is |
3923 | E : Entity_Id; | |
3924 | Id : Node_Id; | |
996ae0b0 RK |
3925 | |
3926 | begin | |
3927 | Set_Hidden_By_Use_Clause (N, No_Elist); | |
3928 | ||
851e9f19 | 3929 | -- Chain clause to list of use clauses in current scope when flagged |
996ae0b0 | 3930 | |
851e9f19 | 3931 | if Chain then |
996ae0b0 RK |
3932 | Chain_Use_Clause (N); |
3933 | end if; | |
3934 | ||
851e9f19 PMR |
3935 | -- Obtain the base type of the type denoted within the use_type_clause's |
3936 | -- subtype mark. | |
3937 | ||
3938 | Id := Subtype_Mark (N); | |
3939 | Find_Type (Id); | |
3940 | E := Base_Type (Entity (Id)); | |
3941 | ||
3942 | -- There are many cases where a use_type_clause may be reanalyzed due to | |
3943 | -- manipulation of the scope stack so we much guard against those cases | |
3944 | -- here, otherwise, we must add the new use_type_clause to the previous | |
3945 | -- use_type_clause chain in order to mark redundant use_type_clauses as | |
5a5925ee ES |
3946 | -- used. When the redundant use-type clauses appear in a parent unit and |
3947 | -- a child unit we must prevent a circularity in the chain that would | |
3948 | -- otherwise result from the separate steps of analysis and installation | |
3949 | -- of the parent context. | |
851e9f19 PMR |
3950 | |
3951 | if Present (Current_Use_Clause (E)) | |
3952 | and then Current_Use_Clause (E) /= N | |
5a5925ee | 3953 | and then Prev_Use_Clause (Current_Use_Clause (E)) /= N |
851e9f19 PMR |
3954 | and then No (Prev_Use_Clause (N)) |
3955 | then | |
3956 | Set_Prev_Use_Clause (N, Current_Use_Clause (E)); | |
3957 | end if; | |
3958 | ||
780d052e | 3959 | -- If the Used_Operations list is already initialized, the clause has |
b6e6a4e3 | 3960 | -- been analyzed previously, and it is being reinstalled, for example |
780d052e RD |
3961 | -- when the clause appears in a package spec and we are compiling the |
3962 | -- corresponding package body. In that case, make the entities on the | |
806f6d37 | 3963 | -- existing list use_visible, and mark the corresponding types In_Use. |
29efbb8c | 3964 | |
780d052e | 3965 | if Present (Used_Operations (N)) then |
29efbb8c ES |
3966 | declare |
3967 | Elmt : Elmt_Id; | |
806f6d37 | 3968 | |
29efbb8c | 3969 | begin |
851e9f19 | 3970 | Use_One_Type (Subtype_Mark (N), Installed => True); |
806f6d37 | 3971 | |
29efbb8c ES |
3972 | Elmt := First_Elmt (Used_Operations (N)); |
3973 | while Present (Elmt) loop | |
3974 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
3975 | Next_Elmt (Elmt); | |
3976 | end loop; | |
3977 | end; | |
3978 | ||
3979 | return; | |
3980 | end if; | |
3981 | ||
cc0b3bac AC |
3982 | -- Otherwise, create new list and attach to it the operations that are |
3983 | -- made use-visible by the clause. | |
780d052e | 3984 | |
29efbb8c | 3985 | Set_Used_Operations (N, New_Elmt_List); |
851e9f19 | 3986 | E := Entity (Id); |
fbf5a39b | 3987 | |
851e9f19 PMR |
3988 | if E /= Any_Type then |
3989 | Use_One_Type (Id); | |
fbf5a39b | 3990 | |
851e9f19 PMR |
3991 | if Nkind (Parent (N)) = N_Compilation_Unit then |
3992 | if Nkind (Id) = N_Identifier then | |
3993 | Error_Msg_N ("type is not directly visible", Id); | |
ecc4ddde | 3994 | |
851e9f19 PMR |
3995 | elsif Is_Child_Unit (Scope (E)) |
3996 | and then Scope (E) /= System_Aux_Id | |
ecc4ddde | 3997 | then |
851e9f19 | 3998 | Check_In_Previous_With_Clause (N, Prefix (Id)); |
ecc4ddde | 3999 | end if; |
996ae0b0 RK |
4000 | end if; |
4001 | ||
851e9f19 PMR |
4002 | else |
4003 | -- If the use_type_clause appears in a compilation unit context, | |
4004 | -- check whether it comes from a unit that may appear in a | |
4005 | -- limited_with_clause, for a better error message. | |
6e9e35e1 | 4006 | |
851e9f19 PMR |
4007 | if Nkind (Parent (N)) = N_Compilation_Unit |
4008 | and then Nkind (Id) /= N_Identifier | |
4009 | then | |
4010 | declare | |
4011 | Item : Node_Id; | |
4012 | Pref : Node_Id; | |
6e9e35e1 | 4013 | |
851e9f19 PMR |
4014 | function Mentioned (Nam : Node_Id) return Boolean; |
4015 | -- Check whether the prefix of expanded name for the type | |
4016 | -- appears in the prefix of some limited_with_clause. | |
6e9e35e1 | 4017 | |
851e9f19 PMR |
4018 | --------------- |
4019 | -- Mentioned -- | |
4020 | --------------- | |
6e9e35e1 | 4021 | |
851e9f19 PMR |
4022 | function Mentioned (Nam : Node_Id) return Boolean is |
4023 | begin | |
4024 | return Nkind (Name (Item)) = N_Selected_Component | |
4025 | and then Chars (Prefix (Name (Item))) = Chars (Nam); | |
4026 | end Mentioned; | |
6e9e35e1 | 4027 | |
851e9f19 PMR |
4028 | begin |
4029 | Pref := Prefix (Id); | |
4030 | Item := First (Context_Items (Parent (N))); | |
4031 | while Present (Item) and then Item /= N loop | |
4032 | if Nkind (Item) = N_With_Clause | |
4033 | and then Limited_Present (Item) | |
4034 | and then Mentioned (Pref) | |
4035 | then | |
4036 | Change_Error_Text | |
4037 | (Get_Msg_Id, "premature usage of incomplete type"); | |
4038 | end if; | |
6e9e35e1 | 4039 | |
851e9f19 PMR |
4040 | Next (Item); |
4041 | end loop; | |
4042 | end; | |
4043 | end if; | |
6e9e35e1 AC |
4044 | end if; |
4045 | ||
4046 | Mark_Ghost_Clause (N); | |
996ae0b0 RK |
4047 | end Analyze_Use_Type; |
4048 | ||
996ae0b0 RK |
4049 | ------------------------ |
4050 | -- Attribute_Renaming -- | |
4051 | ------------------------ | |
4052 | ||
4053 | procedure Attribute_Renaming (N : Node_Id) is | |
8d80ff64 AC |
4054 | Loc : constant Source_Ptr := Sloc (N); |
4055 | Nam : constant Node_Id := Name (N); | |
4056 | Spec : constant Node_Id := Specification (N); | |
4057 | New_S : constant Entity_Id := Defining_Unit_Name (Spec); | |
4058 | Aname : constant Name_Id := Attribute_Name (Nam); | |
996ae0b0 | 4059 | |
8d80ff64 AC |
4060 | Form_Num : Nat := 0; |
4061 | Expr_List : List_Id := No_List; | |
996ae0b0 RK |
4062 | |
4063 | Attr_Node : Node_Id; | |
4064 | Body_Node : Node_Id; | |
4065 | Param_Spec : Node_Id; | |
4066 | ||
4067 | begin | |
4068 | Generate_Definition (New_S); | |
4069 | ||
4c8a5bb8 AC |
4070 | -- This procedure is called in the context of subprogram renaming, and |
4071 | -- thus the attribute must be one that is a subprogram. All of those | |
ba0c6e47 RD |
4072 | -- have at least one formal parameter, with the exceptions of the GNAT |
4073 | -- attribute 'Img, which GNAT treats as renameable. | |
996ae0b0 RK |
4074 | |
4075 | if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then | |
ba0c6e47 | 4076 | if Aname /= Name_Img then |
996ae0b0 RK |
4077 | Error_Msg_N |
4078 | ("subprogram renaming an attribute must have formals", N); | |
4079 | return; | |
4080 | end if; | |
4081 | ||
4082 | else | |
4083 | Param_Spec := First (Parameter_Specifications (Spec)); | |
996ae0b0 RK |
4084 | while Present (Param_Spec) loop |
4085 | Form_Num := Form_Num + 1; | |
4086 | ||
4087 | if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then | |
4088 | Find_Type (Parameter_Type (Param_Spec)); | |
4089 | ||
4090 | -- The profile of the new entity denotes the base type (s) of | |
4091 | -- the types given in the specification. For access parameters | |
4092 | -- there are no subtypes involved. | |
4093 | ||
4094 | Rewrite (Parameter_Type (Param_Spec), | |
e4494292 | 4095 | New_Occurrence_Of |
8d80ff64 | 4096 | (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc)); |
996ae0b0 RK |
4097 | end if; |
4098 | ||
4099 | if No (Expr_List) then | |
4100 | Expr_List := New_List; | |
4101 | end if; | |
4102 | ||
4103 | Append_To (Expr_List, | |
4104 | Make_Identifier (Loc, | |
4105 | Chars => Chars (Defining_Identifier (Param_Spec)))); | |
4106 | ||
fbf5a39b | 4107 | -- The expressions in the attribute reference are not freeze |
4c8a5bb8 | 4108 | -- points. Neither is the attribute as a whole, see below. |
fbf5a39b AC |
4109 | |
4110 | Set_Must_Not_Freeze (Last (Expr_List)); | |
996ae0b0 RK |
4111 | Next (Param_Spec); |
4112 | end loop; | |
4113 | end if; | |
4114 | ||
4c8a5bb8 AC |
4115 | -- Immediate error if too many formals. Other mismatches in number or |
4116 | -- types of parameters are detected when we analyze the body of the | |
4117 | -- subprogram that we construct. | |
996ae0b0 RK |
4118 | |
4119 | if Form_Num > 2 then | |
4120 | Error_Msg_N ("too many formals for attribute", N); | |
4121 | ||
4c8a5bb8 AC |
4122 | -- Error if the attribute reference has expressions that look like |
4123 | -- formal parameters. | |
0da2c8ac AC |
4124 | |
4125 | elsif Present (Expressions (Nam)) then | |
4126 | Error_Msg_N ("illegal expressions in attribute reference", Nam); | |
4127 | ||
996ae0b0 | 4128 | elsif |
b69cd36a AC |
4129 | Nam_In (Aname, Name_Compose, Name_Exponent, Name_Leading_Part, |
4130 | Name_Pos, Name_Round, Name_Scaling, | |
4131 | Name_Val) | |
996ae0b0 RK |
4132 | then |
4133 | if Nkind (N) = N_Subprogram_Renaming_Declaration | |
82c80734 | 4134 | and then Present (Corresponding_Formal_Spec (N)) |
996ae0b0 RK |
4135 | then |
4136 | Error_Msg_N | |
4137 | ("generic actual cannot be attribute involving universal type", | |
4138 | Nam); | |
4139 | else | |
4140 | Error_Msg_N | |
4141 | ("attribute involving a universal type cannot be renamed", | |
4142 | Nam); | |
4143 | end if; | |
4144 | end if; | |
4145 | ||
ba0c6e47 RD |
4146 | -- Rewrite attribute node to have a list of expressions corresponding to |
4147 | -- the subprogram formals. A renaming declaration is not a freeze point, | |
4148 | -- and the analysis of the attribute reference should not freeze the | |
4149 | -- type of the prefix. We use the original node in the renaming so that | |
4150 | -- its source location is preserved, and checks on stream attributes are | |
4151 | -- properly applied. | |
996ae0b0 | 4152 | |
ba0c6e47 RD |
4153 | Attr_Node := Relocate_Node (Nam); |
4154 | Set_Expressions (Attr_Node, Expr_List); | |
996ae0b0 | 4155 | |
ba0c6e47 RD |
4156 | Set_Must_Not_Freeze (Attr_Node); |
4157 | Set_Must_Not_Freeze (Prefix (Nam)); | |
996ae0b0 RK |
4158 | |
4159 | -- Case of renaming a function | |
4160 | ||
4161 | if Nkind (Spec) = N_Function_Specification then | |
996ae0b0 RK |
4162 | if Is_Procedure_Attribute_Name (Aname) then |
4163 | Error_Msg_N ("attribute can only be renamed as procedure", Nam); | |
4164 | return; | |
4165 | end if; | |
4166 | ||
725393ea ES |
4167 | Find_Type (Result_Definition (Spec)); |
4168 | Rewrite (Result_Definition (Spec), | |
e4494292 | 4169 | New_Occurrence_Of |
4bcf6815 | 4170 | (Base_Type (Entity (Result_Definition (Spec))), Loc)); |
996ae0b0 RK |
4171 | |
4172 | Body_Node := | |
4173 | Make_Subprogram_Body (Loc, | |
4174 | Specification => Spec, | |
4175 | Declarations => New_List, | |
4176 | Handled_Statement_Sequence => | |
4177 | Make_Handled_Sequence_Of_Statements (Loc, | |
4178 | Statements => New_List ( | |
11560bcc | 4179 | Make_Simple_Return_Statement (Loc, |
996ae0b0 RK |
4180 | Expression => Attr_Node)))); |
4181 | ||
4182 | -- Case of renaming a procedure | |
4183 | ||
4184 | else | |
4185 | if not Is_Procedure_Attribute_Name (Aname) then | |
4186 | Error_Msg_N ("attribute can only be renamed as function", Nam); | |
4187 | return; | |
4188 | end if; | |
4189 | ||
4190 | Body_Node := | |
4191 | Make_Subprogram_Body (Loc, | |
4192 | Specification => Spec, | |
4193 | Declarations => New_List, | |
4194 | Handled_Statement_Sequence => | |
4195 | Make_Handled_Sequence_Of_Statements (Loc, | |
4196 | Statements => New_List (Attr_Node))); | |
4197 | end if; | |
4198 | ||
90e491a7 PMR |
4199 | -- Signal the ABE mechanism that the generated subprogram body has not |
4200 | -- ABE ramifications. | |
4201 | ||
4202 | Set_Was_Attribute_Reference (Body_Node); | |
4203 | ||
fbe627af RD |
4204 | -- In case of tagged types we add the body of the generated function to |
4205 | -- the freezing actions of the type (because in the general case such | |
4206 | -- type is still not frozen). We exclude from this processing generic | |
ba0c6e47 | 4207 | -- formal subprograms found in instantiations. |
fbe627af | 4208 | |
535a8637 | 4209 | -- We must exclude restricted run-time libraries because |
6b81741c | 4210 | -- entity AST_Handler is defined in package System.Aux_Dec which is not |
9d1e0e72 JR |
4211 | -- available in those platforms. Note that we cannot use the function |
4212 | -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because | |
4213 | -- the ZFP run-time library is not defined as a profile, and we do not | |
4214 | -- want to deal with AST_Handler in ZFP mode. | |
ddc1515a | 4215 | |
535a8637 | 4216 | if not Configurable_Run_Time_Mode |
ddc1515a | 4217 | and then not Present (Corresponding_Formal_Spec (N)) |
fbe627af RD |
4218 | and then Etype (Nam) /= RTE (RE_AST_Handler) |
4219 | then | |
4220 | declare | |
a530b8bb | 4221 | P : constant Node_Id := Prefix (Nam); |
fbe627af RD |
4222 | |
4223 | begin | |
8d80ff64 AC |
4224 | -- The prefix of 'Img is an object that is evaluated for each call |
4225 | -- of the function that renames it. | |
a530b8bb AC |
4226 | |
4227 | if Aname = Name_Img then | |
4228 | Preanalyze_And_Resolve (P); | |
4229 | ||
8d80ff64 | 4230 | -- For all other attribute renamings, the prefix is a subtype |
a530b8bb AC |
4231 | |
4232 | else | |
4233 | Find_Type (P); | |
4234 | end if; | |
fbe627af | 4235 | |
4bcf6815 AC |
4236 | -- If the target type is not yet frozen, add the body to the |
4237 | -- actions to be elaborated at freeze time. | |
4238 | ||
4239 | if Is_Tagged_Type (Etype (P)) | |
4240 | and then In_Open_Scopes (Scope (Etype (P))) | |
4241 | then | |
fbe627af RD |
4242 | Ensure_Freeze_Node (Etype (P)); |
4243 | Append_Freeze_Action (Etype (P), Body_Node); | |
4244 | else | |
4245 | Rewrite (N, Body_Node); | |
4246 | Analyze (N); | |
4247 | Set_Etype (New_S, Base_Type (Etype (New_S))); | |
4248 | end if; | |
4249 | end; | |
4250 | ||
4251 | -- Generic formal subprograms or AST_Handler renaming | |
4252 | ||
4253 | else | |
4254 | Rewrite (N, Body_Node); | |
4255 | Analyze (N); | |
4256 | Set_Etype (New_S, Base_Type (Etype (New_S))); | |
4257 | end if; | |
996ae0b0 | 4258 | |
615cbd95 AC |
4259 | if Is_Compilation_Unit (New_S) then |
4260 | Error_Msg_N | |
4261 | ("a library unit can only rename another library unit", N); | |
4262 | end if; | |
967947ed PMR |
4263 | |
4264 | -- We suppress elaboration warnings for the resulting entity, since | |
4265 | -- clearly they are not needed, and more particularly, in the case | |
4266 | -- of a generic formal subprogram, the resulting entity can appear | |
4267 | -- after the instantiation itself, and thus look like a bogus case | |
4268 | -- of access before elaboration. | |
4269 | ||
4270 | if Legacy_Elaboration_Checks then | |
4271 | Set_Suppress_Elaboration_Warnings (New_S); | |
4272 | end if; | |
996ae0b0 RK |
4273 | end Attribute_Renaming; |
4274 | ||
4275 | ---------------------- | |
4276 | -- Chain_Use_Clause -- | |
4277 | ---------------------- | |
4278 | ||
4279 | procedure Chain_Use_Clause (N : Node_Id) is | |
d4810530 | 4280 | Level : Int := Scope_Stack.Last; |
7f5e671b | 4281 | Pack : Entity_Id; |
d4810530 | 4282 | |
996ae0b0 | 4283 | begin |
851e9f19 PMR |
4284 | -- Common case |
4285 | ||
d4810530 ES |
4286 | if not Is_Compilation_Unit (Current_Scope) |
4287 | or else not Is_Child_Unit (Current_Scope) | |
4288 | then | |
851e9f19 | 4289 | null; |
d4810530 | 4290 | |
851e9f19 PMR |
4291 | -- Common case for compilation unit |
4292 | ||
4293 | elsif Defining_Entity (N => Parent (N), | |
4294 | Empty_On_Errors => True) = Current_Scope | |
4295 | then | |
4296 | null; | |
d4810530 ES |
4297 | |
4298 | else | |
4299 | -- If declaration appears in some other scope, it must be in some | |
4300 | -- parent unit when compiling a child. | |
4301 | ||
851e9f19 | 4302 | Pack := Defining_Entity (Parent (N), Empty_On_Errors => True); |
7f5e671b | 4303 | |
d4810530 | 4304 | if not In_Open_Scopes (Pack) then |
851e9f19 | 4305 | null; |
d4810530 | 4306 | |
3830827c AC |
4307 | -- If the use clause appears in an ancestor and we are in the |
4308 | -- private part of the immediate parent, the use clauses are | |
4309 | -- already installed. | |
4310 | ||
4311 | elsif Pack /= Scope (Current_Scope) | |
4312 | and then In_Private_Part (Scope (Current_Scope)) | |
4313 | then | |
4314 | null; | |
4315 | ||
d4810530 ES |
4316 | else |
4317 | -- Find entry for parent unit in scope stack | |
4318 | ||
4319 | while Scope_Stack.Table (Level).Entity /= Pack loop | |
4320 | Level := Level - 1; | |
4321 | end loop; | |
4322 | end if; | |
4323 | end if; | |
4324 | ||
996ae0b0 | 4325 | Set_Next_Use_Clause (N, |
d4810530 ES |
4326 | Scope_Stack.Table (Level).First_Use_Clause); |
4327 | Scope_Stack.Table (Level).First_Use_Clause := N; | |
996ae0b0 RK |
4328 | end Chain_Use_Clause; |
4329 | ||
15ce9ca2 AC |
4330 | --------------------------- |
4331 | -- Check_Frozen_Renaming -- | |
4332 | --------------------------- | |
996ae0b0 RK |
4333 | |
4334 | procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is | |
4335 | B_Node : Node_Id; | |
4336 | Old_S : Entity_Id; | |
4337 | ||
4338 | begin | |
ac7d724d | 4339 | if Is_Frozen (Subp) and then not Has_Completion (Subp) then |
996ae0b0 RK |
4340 | B_Node := |
4341 | Build_Renamed_Body | |
4342 | (Parent (Declaration_Node (Subp)), Defining_Entity (N)); | |
4343 | ||
4344 | if Is_Entity_Name (Name (N)) then | |
4345 | Old_S := Entity (Name (N)); | |
4346 | ||
fbf5a39b AC |
4347 | if not Is_Frozen (Old_S) |
4348 | and then Operating_Mode /= Check_Semantics | |
4349 | then | |
4350 | Append_Freeze_Action (Old_S, B_Node); | |
996ae0b0 RK |
4351 | else |
4352 | Insert_After (N, B_Node); | |
4353 | Analyze (B_Node); | |
4354 | end if; | |
4355 | ||
208ebca9 AC |
4356 | if Is_Intrinsic_Subprogram (Old_S) |
4357 | and then not In_Instance | |
4358 | and then not Relaxed_RM_Semantics | |
4359 | then | |
996ae0b0 RK |
4360 | Error_Msg_N |
4361 | ("subprogram used in renaming_as_body cannot be intrinsic", | |
ac7d724d | 4362 | Name (N)); |
996ae0b0 RK |
4363 | end if; |
4364 | ||
4365 | else | |
4366 | Insert_After (N, B_Node); | |
4367 | Analyze (B_Node); | |
4368 | end if; | |
4369 | end if; | |
4370 | end Check_Frozen_Renaming; | |
4371 | ||
4c484f40 AC |
4372 | ------------------------------- |
4373 | -- Set_Entity_Or_Discriminal -- | |
4374 | ------------------------------- | |
4375 | ||
4376 | procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is | |
4377 | P : Node_Id; | |
4378 | ||
4379 | begin | |
4380 | -- If the entity is not a discriminant, or else expansion is disabled, | |
4381 | -- simply set the entity. | |
4382 | ||
4383 | if not In_Spec_Expression | |
4384 | or else Ekind (E) /= E_Discriminant | |
4385 | or else Inside_A_Generic | |
4386 | then | |
e7ba564f | 4387 | Set_Entity_With_Checks (N, E); |
4c484f40 AC |
4388 | |
4389 | -- The replacement of a discriminant by the corresponding discriminal | |
4390 | -- is not done for a task discriminant that appears in a default | |
8779dffa | 4391 | -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant |
4c484f40 AC |
4392 | -- for details on their handling. |
4393 | ||
4394 | elsif Is_Concurrent_Type (Scope (E)) then | |
4c484f40 AC |
4395 | P := Parent (N); |
4396 | while Present (P) | |
4397 | and then not Nkind_In (P, N_Parameter_Specification, | |
ac7d724d | 4398 | N_Component_Declaration) |
4c484f40 AC |
4399 | loop |
4400 | P := Parent (P); | |
4401 | end loop; | |
4402 | ||
4403 | if Present (P) | |
4404 | and then Nkind (P) = N_Parameter_Specification | |
4405 | then | |
4406 | null; | |
4407 | ||
4408 | else | |
4409 | Set_Entity (N, Discriminal (E)); | |
4410 | end if; | |
4411 | ||
4412 | -- Otherwise, this is a discriminant in a context in which | |
4413 | -- it is a reference to the corresponding parameter of the | |
4414 | -- init proc for the enclosing type. | |
4415 | ||
4416 | else | |
4417 | Set_Entity (N, Discriminal (E)); | |
4418 | end if; | |
4419 | end Set_Entity_Or_Discriminal; | |
4420 | ||
fbf5a39b AC |
4421 | ----------------------------------- |
4422 | -- Check_In_Previous_With_Clause -- | |
4423 | ----------------------------------- | |
4424 | ||
4425 | procedure Check_In_Previous_With_Clause | |
4426 | (N : Node_Id; | |
4427 | Nam : Entity_Id) | |
4428 | is | |
4429 | Pack : constant Entity_Id := Entity (Original_Node (Nam)); | |
4430 | Item : Node_Id; | |
4431 | Par : Node_Id; | |
4432 | ||
4433 | begin | |
4434 | Item := First (Context_Items (Parent (N))); | |
ac7d724d | 4435 | while Present (Item) and then Item /= N loop |
fbf5a39b | 4436 | if Nkind (Item) = N_With_Clause |
edd63e9b | 4437 | |
ac7d724d | 4438 | -- Protect the frontend against previous critical errors |
edd63e9b ES |
4439 | |
4440 | and then Nkind (Name (Item)) /= N_Selected_Component | |
fbf5a39b AC |
4441 | and then Entity (Name (Item)) = Pack |
4442 | then | |
4443 | Par := Nam; | |
4444 | ||
bc41faa2 | 4445 | -- Find root library unit in with_clause |
fbf5a39b AC |
4446 | |
4447 | while Nkind (Par) = N_Expanded_Name loop | |
4448 | Par := Prefix (Par); | |
4449 | end loop; | |
4450 | ||
4451 | if Is_Child_Unit (Entity (Original_Node (Par))) then | |
ed2233dc | 4452 | Error_Msg_NE ("& is not directly visible", Par, Entity (Par)); |
fbf5a39b AC |
4453 | else |
4454 | return; | |
4455 | end if; | |
4456 | end if; | |
4457 | ||
4458 | Next (Item); | |
4459 | end loop; | |
4460 | ||
4461 | -- On exit, package is not mentioned in a previous with_clause. | |
4462 | -- Check if its prefix is. | |
4463 | ||
4464 | if Nkind (Nam) = N_Expanded_Name then | |
4465 | Check_In_Previous_With_Clause (N, Prefix (Nam)); | |
4466 | ||
4467 | elsif Pack /= Any_Id then | |
4468 | Error_Msg_NE ("& is not visible", Nam, Pack); | |
4469 | end if; | |
4470 | end Check_In_Previous_With_Clause; | |
4471 | ||
996ae0b0 RK |
4472 | --------------------------------- |
4473 | -- Check_Library_Unit_Renaming -- | |
4474 | --------------------------------- | |
4475 | ||
4476 | procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is | |
4477 | New_E : Entity_Id; | |
4478 | ||
4479 | begin | |
4480 | if Nkind (Parent (N)) /= N_Compilation_Unit then | |
4481 | return; | |
4482 | ||
fbe627af RD |
4483 | -- Check for library unit. Note that we used to check for the scope |
4484 | -- being Standard here, but that was wrong for Standard itself. | |
4485 | ||
4486 | elsif not Is_Compilation_Unit (Old_E) | |
996ae0b0 RK |
4487 | and then not Is_Child_Unit (Old_E) |
4488 | then | |
4489 | Error_Msg_N ("renamed unit must be a library unit", Name (N)); | |
4490 | ||
615cbd95 AC |
4491 | -- Entities defined in Standard (operators and boolean literals) cannot |
4492 | -- be renamed as library units. | |
4493 | ||
4494 | elsif Scope (Old_E) = Standard_Standard | |
4495 | and then Sloc (Old_E) = Standard_Location | |
4496 | then | |
4497 | Error_Msg_N ("renamed unit must be a library unit", Name (N)); | |
4498 | ||
996ae0b0 RK |
4499 | elsif Present (Parent_Spec (N)) |
4500 | and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration | |
4501 | and then not Is_Child_Unit (Old_E) | |
4502 | then | |
4503 | Error_Msg_N | |
4504 | ("renamed unit must be a child unit of generic parent", Name (N)); | |
4505 | ||
4506 | elsif Nkind (N) in N_Generic_Renaming_Declaration | |
ac7d724d ES |
4507 | and then Nkind (Name (N)) = N_Expanded_Name |
4508 | and then Is_Generic_Instance (Entity (Prefix (Name (N)))) | |
4509 | and then Is_Generic_Unit (Old_E) | |
996ae0b0 RK |
4510 | then |
4511 | Error_Msg_N | |
4512 | ("renamed generic unit must be a library unit", Name (N)); | |
4513 | ||
b9b2405f ST |
4514 | elsif Is_Package_Or_Generic_Package (Old_E) then |
4515 | ||
996ae0b0 RK |
4516 | -- Inherit categorization flags |
4517 | ||
4518 | New_E := Defining_Entity (N); | |
4519 | Set_Is_Pure (New_E, Is_Pure (Old_E)); | |
4520 | Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E)); | |
4521 | Set_Is_Remote_Call_Interface (New_E, | |
4522 | Is_Remote_Call_Interface (Old_E)); | |
4523 | Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E)); | |
4524 | Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E)); | |
4525 | end if; | |
4526 | end Check_Library_Unit_Renaming; | |
4527 | ||
ac7d724d ES |
4528 | ------------------------ |
4529 | -- Enclosing_Instance -- | |
4530 | ------------------------ | |
4531 | ||
4532 | function Enclosing_Instance return Entity_Id is | |
4533 | S : Entity_Id; | |
4534 | ||
4535 | begin | |
4536 | if not Is_Generic_Instance (Current_Scope) then | |
4537 | return Empty; | |
4538 | end if; | |
4539 | ||
4540 | S := Scope (Current_Scope); | |
4541 | while S /= Standard_Standard loop | |
4542 | if Is_Generic_Instance (S) then | |
4543 | return S; | |
4544 | end if; | |
4545 | ||
4546 | S := Scope (S); | |
4547 | end loop; | |
4548 | ||
4549 | return Empty; | |
4550 | end Enclosing_Instance; | |
4551 | ||
996ae0b0 RK |
4552 | --------------- |
4553 | -- End_Scope -- | |
4554 | --------------- | |
4555 | ||
4556 | procedure End_Scope is | |
4557 | Id : Entity_Id; | |
4558 | Prev : Entity_Id; | |
4559 | Outer : Entity_Id; | |
4560 | ||
4561 | begin | |
4562 | Id := First_Entity (Current_Scope); | |
996ae0b0 RK |
4563 | while Present (Id) loop |
4564 | -- An entity in the current scope is not necessarily the first one | |
4565 | -- on its homonym chain. Find its predecessor if any, | |
4566 | -- If it is an internal entity, it will not be in the visibility | |
4567 | -- chain altogether, and there is nothing to unchain. | |
4568 | ||
4569 | if Id /= Current_Entity (Id) then | |
4570 | Prev := Current_Entity (Id); | |
4571 | while Present (Prev) | |
4572 | and then Present (Homonym (Prev)) | |
4573 | and then Homonym (Prev) /= Id | |
4574 | loop | |
4575 | Prev := Homonym (Prev); | |
4576 | end loop; | |
4577 | ||
4578 | -- Skip to end of loop if Id is not in the visibility chain | |
4579 | ||
4580 | if No (Prev) or else Homonym (Prev) /= Id then | |
4581 | goto Next_Ent; | |
4582 | end if; | |
4583 | ||
4584 | else | |
4585 | Prev := Empty; | |
4586 | end if; | |
4587 | ||
996ae0b0 RK |
4588 | Set_Is_Immediately_Visible (Id, False); |
4589 | ||
16ca248a | 4590 | Outer := Homonym (Id); |
996ae0b0 RK |
4591 | while Present (Outer) and then Scope (Outer) = Current_Scope loop |
4592 | Outer := Homonym (Outer); | |
4593 | end loop; | |
4594 | ||
4595 | -- Reset homonym link of other entities, but do not modify link | |
4596 | -- between entities in current scope, so that the back-end can have | |
4597 | -- a proper count of local overloadings. | |
4598 | ||
4599 | if No (Prev) then | |
4600 | Set_Name_Entity_Id (Chars (Id), Outer); | |
4601 | ||
4602 | elsif Scope (Prev) /= Scope (Id) then | |
4603 | Set_Homonym (Prev, Outer); | |
4604 | end if; | |
4605 | ||
4606 | <<Next_Ent>> | |
4607 | Next_Entity (Id); | |
4608 | end loop; | |
4609 | ||
4610 | -- If the scope generated freeze actions, place them before the | |
4611 | -- current declaration and analyze them. Type declarations and | |
4612 | -- the bodies of initialization procedures can generate such nodes. | |
4613 | -- We follow the parent chain until we reach a list node, which is | |
4614 | -- the enclosing list of declarations. If the list appears within | |
4615 | -- a protected definition, move freeze nodes outside the protected | |
4616 | -- type altogether. | |
4617 | ||
4618 | if Present | |
4619 | (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions) | |
4620 | then | |
4621 | declare | |
4622 | Decl : Node_Id; | |
4623 | L : constant List_Id := Scope_Stack.Table | |
4624 | (Scope_Stack.Last).Pending_Freeze_Actions; | |
4625 | ||
4626 | begin | |
4627 | if Is_Itype (Current_Scope) then | |
4628 | Decl := Associated_Node_For_Itype (Current_Scope); | |
4629 | else | |
4630 | Decl := Parent (Current_Scope); | |
4631 | end if; | |
4632 | ||
4633 | Pop_Scope; | |
4634 | ||
4635 | while not (Is_List_Member (Decl)) | |
294ccb21 RD |
4636 | or else Nkind_In (Parent (Decl), N_Protected_Definition, |
4637 | N_Task_Definition) | |
996ae0b0 RK |
4638 | loop |
4639 | Decl := Parent (Decl); | |
4640 | end loop; | |
4641 | ||
4642 | Insert_List_Before_And_Analyze (Decl, L); | |
4643 | end; | |
4644 | ||
4645 | else | |
4646 | Pop_Scope; | |
4647 | end if; | |
996ae0b0 RK |
4648 | end End_Scope; |
4649 | ||
4650 | --------------------- | |
4651 | -- End_Use_Clauses -- | |
4652 | --------------------- | |
4653 | ||
4654 | procedure End_Use_Clauses (Clause : Node_Id) is | |
851e9f19 | 4655 | U : Node_Id; |
996ae0b0 RK |
4656 | |
4657 | begin | |
851e9f19 PMR |
4658 | -- Remove use_type_clauses first, because they affect the visibility of |
4659 | -- operators in subsequent used packages. | |
fbf5a39b AC |
4660 | |
4661 | U := Clause; | |
4662 | while Present (U) loop | |
4663 | if Nkind (U) = N_Use_Type_Clause then | |
4664 | End_Use_Type (U); | |
4665 | end if; | |
4666 | ||
4667 | Next_Use_Clause (U); | |
4668 | end loop; | |
4669 | ||
4670 | U := Clause; | |
996ae0b0 RK |
4671 | while Present (U) loop |
4672 | if Nkind (U) = N_Use_Package_Clause then | |
4673 | End_Use_Package (U); | |
996ae0b0 RK |
4674 | end if; |
4675 | ||
4676 | Next_Use_Clause (U); | |
4677 | end loop; | |
4678 | end End_Use_Clauses; | |
4679 | ||
4680 | --------------------- | |
4681 | -- End_Use_Package -- | |
4682 | --------------------- | |
4683 | ||
4684 | procedure End_Use_Package (N : Node_Id) is | |
996ae0b0 | 4685 | Pack : Entity_Id; |
851e9f19 | 4686 | Pack_Name : Node_Id; |
996ae0b0 RK |
4687 | Id : Entity_Id; |
4688 | Elmt : Elmt_Id; | |
4689 | ||
a780db15 | 4690 | function Is_Primitive_Operator_In_Use |
2e071734 AC |
4691 | (Op : Entity_Id; |
4692 | F : Entity_Id) return Boolean; | |
fbf5a39b AC |
4693 | -- Check whether Op is a primitive operator of a use-visible type |
4694 | ||
a780db15 AC |
4695 | ---------------------------------- |
4696 | -- Is_Primitive_Operator_In_Use -- | |
4697 | ---------------------------------- | |
fbf5a39b | 4698 | |
a780db15 | 4699 | function Is_Primitive_Operator_In_Use |
2e071734 AC |
4700 | (Op : Entity_Id; |
4701 | F : Entity_Id) return Boolean | |
fbf5a39b | 4702 | is |
95eb8b69 | 4703 | T : constant Entity_Id := Base_Type (Etype (F)); |
fbf5a39b | 4704 | begin |
95eb8b69 | 4705 | return In_Use (T) and then Scope (T) = Scope (Op); |
a780db15 | 4706 | end Is_Primitive_Operator_In_Use; |
fbf5a39b AC |
4707 | |
4708 | -- Start of processing for End_Use_Package | |
4709 | ||
996ae0b0 | 4710 | begin |
851e9f19 | 4711 | Pack_Name := Name (N); |
996ae0b0 | 4712 | |
851e9f19 | 4713 | -- Test that Pack_Name actually denotes a package before processing |
462c31ef | 4714 | |
851e9f19 PMR |
4715 | if Is_Entity_Name (Pack_Name) |
4716 | and then Ekind (Entity (Pack_Name)) = E_Package | |
4717 | then | |
4718 | Pack := Entity (Pack_Name); | |
462c31ef | 4719 | |
851e9f19 PMR |
4720 | if In_Open_Scopes (Pack) then |
4721 | null; | |
996ae0b0 | 4722 | |
851e9f19 PMR |
4723 | elsif not Redundant_Use (Pack_Name) then |
4724 | Set_In_Use (Pack, False); | |
4725 | Set_Current_Use_Clause (Pack, Empty); | |
996ae0b0 | 4726 | |
851e9f19 PMR |
4727 | Id := First_Entity (Pack); |
4728 | while Present (Id) loop | |
996ae0b0 | 4729 | |
851e9f19 PMR |
4730 | -- Preserve use-visibility of operators that are primitive |
4731 | -- operators of a type that is use-visible through an active | |
4732 | -- use_type_clause. | |
996ae0b0 | 4733 | |
851e9f19 PMR |
4734 | if Nkind (Id) = N_Defining_Operator_Symbol |
4735 | and then | |
4736 | (Is_Primitive_Operator_In_Use (Id, First_Formal (Id)) | |
4737 | or else | |
4738 | (Present (Next_Formal (First_Formal (Id))) | |
4739 | and then | |
4740 | Is_Primitive_Operator_In_Use | |
4741 | (Id, Next_Formal (First_Formal (Id))))) | |
4742 | then | |
4743 | null; | |
4744 | else | |
4745 | Set_Is_Potentially_Use_Visible (Id, False); | |
4746 | end if; | |
4747 | ||
4748 | if Is_Private_Type (Id) | |
4749 | and then Present (Full_View (Id)) | |
4750 | then | |
4751 | Set_Is_Potentially_Use_Visible (Full_View (Id), False); | |
4752 | end if; | |
4753 | ||
4754 | Next_Entity (Id); | |
4755 | end loop; | |
4756 | ||
4757 | if Present (Renamed_Object (Pack)) then | |
4758 | Set_In_Use (Renamed_Object (Pack), False); | |
4759 | Set_Current_Use_Clause (Renamed_Object (Pack), Empty); | |
4760 | end if; | |
4761 | ||
4762 | if Chars (Pack) = Name_System | |
4763 | and then Scope (Pack) = Standard_Standard | |
4764 | and then Present_System_Aux | |
4765 | then | |
4766 | Id := First_Entity (System_Aux_Id); | |
4767 | while Present (Id) loop | |
4768 | Set_Is_Potentially_Use_Visible (Id, False); | |
996ae0b0 RK |
4769 | |
4770 | if Is_Private_Type (Id) | |
4771 | and then Present (Full_View (Id)) | |
4772 | then | |
4773 | Set_Is_Potentially_Use_Visible (Full_View (Id), False); | |
4774 | end if; | |
4775 | ||
4776 | Next_Entity (Id); | |
4777 | end loop; | |
4778 | ||
851e9f19 | 4779 | Set_In_Use (System_Aux_Id, False); |
996ae0b0 | 4780 | end if; |
851e9f19 PMR |
4781 | else |
4782 | Set_Redundant_Use (Pack_Name, False); | |
996ae0b0 | 4783 | end if; |
851e9f19 | 4784 | end if; |
996ae0b0 RK |
4785 | |
4786 | if Present (Hidden_By_Use_Clause (N)) then | |
4787 | Elmt := First_Elmt (Hidden_By_Use_Clause (N)); | |
996ae0b0 | 4788 | while Present (Elmt) loop |
e69614ad AC |
4789 | declare |
4790 | E : constant Entity_Id := Node (Elmt); | |
4791 | ||
4792 | begin | |
4793 | -- Reset either Use_Visibility or Direct_Visibility, depending | |
4794 | -- on how the entity was hidden by the use clause. | |
4795 | ||
4796 | if In_Use (Scope (E)) | |
4797 | and then Used_As_Generic_Actual (Scope (E)) | |
4798 | then | |
4799 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
4800 | else | |
4801 | Set_Is_Immediately_Visible (Node (Elmt)); | |
4802 | end if; | |
4803 | ||
4804 | Next_Elmt (Elmt); | |
4805 | end; | |
996ae0b0 RK |
4806 | end loop; |
4807 | ||
4808 | Set_Hidden_By_Use_Clause (N, No_Elist); | |
4809 | end if; | |
4810 | end End_Use_Package; | |
4811 | ||
4812 | ------------------ | |
4813 | -- End_Use_Type -- | |
4814 | ------------------ | |
4815 | ||
4816 | procedure End_Use_Type (N : Node_Id) is | |
851e9f19 PMR |
4817 | Elmt : Elmt_Id; |
4818 | Id : Entity_Id; | |
4819 | T : Entity_Id; | |
996ae0b0 | 4820 | |
4a214958 AC |
4821 | -- Start of processing for End_Use_Type |
4822 | ||
996ae0b0 | 4823 | begin |
851e9f19 | 4824 | Id := Subtype_Mark (N); |
fbf5a39b | 4825 | |
851e9f19 PMR |
4826 | -- A call to Rtsfind may occur while analyzing a use_type_clause, in |
4827 | -- which case the type marks are not resolved yet, so guard against that | |
4828 | -- here. | |
fbf5a39b | 4829 | |
851e9f19 | 4830 | if Is_Entity_Name (Id) and then Present (Entity (Id)) then |
996ae0b0 RK |
4831 | T := Entity (Id); |
4832 | ||
7b56a91b | 4833 | if T = Any_Type or else From_Limited_With (T) then |
996ae0b0 RK |
4834 | null; |
4835 | ||
851e9f19 | 4836 | -- Note that the use_type_clause may mention a subtype of the type |
16ca248a ES |
4837 | -- whose primitive operations have been made visible. Here as |
4838 | -- elsewhere, it is the base type that matters for visibility. | |
996ae0b0 RK |
4839 | |
4840 | elsif In_Open_Scopes (Scope (Base_Type (T))) then | |
4841 | null; | |
4842 | ||
4843 | elsif not Redundant_Use (Id) then | |
4844 | Set_In_Use (T, False); | |
4845 | Set_In_Use (Base_Type (T), False); | |
21d27997 RD |
4846 | Set_Current_Use_Clause (T, Empty); |
4847 | Set_Current_Use_Clause (Base_Type (T), Empty); | |
11699257 EB |
4848 | |
4849 | -- See Use_One_Type for the rationale. This is a bit on the naive | |
4850 | -- side, but should be good enough in practice. | |
4851 | ||
4852 | if Is_Tagged_Type (T) then | |
4853 | Set_In_Use (Class_Wide_Type (T), False); | |
4854 | end if; | |
996ae0b0 | 4855 | end if; |
851e9f19 | 4856 | end if; |
29efbb8c ES |
4857 | |
4858 | if Is_Empty_Elmt_List (Used_Operations (N)) then | |
4859 | return; | |
4860 | ||
4861 | else | |
4862 | Elmt := First_Elmt (Used_Operations (N)); | |
4863 | while Present (Elmt) loop | |
4864 | Set_Is_Potentially_Use_Visible (Node (Elmt), False); | |
4865 | Next_Elmt (Elmt); | |
4866 | end loop; | |
4867 | end if; | |
996ae0b0 RK |
4868 | end End_Use_Type; |
4869 | ||
851e9f19 PMR |
4870 | -------------------- |
4871 | -- Entity_Of_Unit -- | |
4872 | -------------------- | |
4873 | ||
4874 | function Entity_Of_Unit (U : Node_Id) return Entity_Id is | |
4875 | begin | |
7f5e671b | 4876 | if Nkind (U) = N_Package_Instantiation and then Analyzed (U) then |
851e9f19 PMR |
4877 | return Defining_Entity (Instance_Spec (U)); |
4878 | else | |
4879 | return Defining_Entity (U); | |
4880 | end if; | |
4881 | end Entity_Of_Unit; | |
4882 | ||
996ae0b0 RK |
4883 | ---------------------- |
4884 | -- Find_Direct_Name -- | |
4885 | ---------------------- | |
4886 | ||
b3b3ada9 HK |
4887 | procedure Find_Direct_Name |
4888 | (N : Node_Id; | |
4889 | Errors_OK : Boolean := True; | |
4890 | Marker_OK : Boolean := True; | |
4891 | Reference_OK : Boolean := True) | |
4892 | is | |
522aa6ee AC |
4893 | E : Entity_Id; |
4894 | E2 : Entity_Id; | |
4895 | Msg : Boolean; | |
996ae0b0 RK |
4896 | |
4897 | Homonyms : Entity_Id; | |
4898 | -- Saves start of homonym chain | |
4899 | ||
522aa6ee AC |
4900 | Inst : Entity_Id := Empty; |
4901 | -- Enclosing instance, if any | |
4902 | ||
996ae0b0 | 4903 | Nvis_Entity : Boolean; |
3764bb00 BD |
4904 | -- Set True to indicate that there is at least one entity on the homonym |
4905 | -- chain which, while not visible, is visible enough from the user point | |
4906 | -- of view to warrant an error message of "not visible" rather than | |
4907 | -- undefined. | |
996ae0b0 | 4908 | |
9bc856dd | 4909 | Nvis_Is_Private_Subprg : Boolean := False; |
0ab80019 | 4910 | -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais |
9bc856dd AC |
4911 | -- effect concerning library subprograms has been detected. Used to |
4912 | -- generate the precise error message. | |
4913 | ||
996ae0b0 | 4914 | function From_Actual_Package (E : Entity_Id) return Boolean; |
fba9ebfc | 4915 | -- Returns true if the entity is an actual for a package that is itself |
996ae0b0 | 4916 | -- an actual for a formal package of the current instance. Such an |
fba9ebfc AC |
4917 | -- entity requires special handling because it may be use-visible but |
4918 | -- hides directly visible entities defined outside the instance, because | |
4919 | -- the corresponding formal did so in the generic. | |
996ae0b0 | 4920 | |
67ce0d7e RD |
4921 | function Is_Actual_Parameter return Boolean; |
4922 | -- This function checks if the node N is an identifier that is an actual | |
4923 | -- parameter of a procedure call. If so it returns True, otherwise it | |
4924 | -- return False. The reason for this check is that at this stage we do | |
4925 | -- not know what procedure is being called if the procedure might be | |
4926 | -- overloaded, so it is premature to go setting referenced flags or | |
4927 | -- making calls to Generate_Reference. We will wait till Resolve_Actuals | |
4928 | -- for that processing | |
4929 | ||
996ae0b0 | 4930 | function Known_But_Invisible (E : Entity_Id) return Boolean; |
a43f6434 AC |
4931 | -- This function determines whether a reference to the entity E, which |
4932 | -- is not visible, can reasonably be considered to be known to the | |
4933 | -- writer of the reference. This is a heuristic test, used only for | |
4934 | -- the purposes of figuring out whether we prefer to complain that an | |
4935 | -- entity is undefined or invisible (and identify the declaration of | |
4936 | -- the invisible entity in the latter case). The point here is that we | |
4937 | -- don't want to complain that something is invisible and then point to | |
4938 | -- something entirely mysterious to the writer. | |
996ae0b0 RK |
4939 | |
4940 | procedure Nvis_Messages; | |
4941 | -- Called if there are no visible entries for N, but there is at least | |
4942 | -- one non-directly visible, or hidden declaration. This procedure | |
4943 | -- outputs an appropriate set of error messages. | |
4944 | ||
4945 | procedure Undefined (Nvis : Boolean); | |
4946 | -- This function is called if the current node has no corresponding | |
4947 | -- visible entity or entities. The value set in Msg indicates whether | |
4948 | -- an error message was generated (multiple error messages for the | |
4949 | -- same variable are generally suppressed, see body for details). | |
4950 | -- Msg is True if an error message was generated, False if not. This | |
4951 | -- value is used by the caller to determine whether or not to output | |
4952 | -- additional messages where appropriate. The parameter is set False | |
4953 | -- to get the message "X is undefined", and True to get the message | |
4954 | -- "X is not visible". | |
4955 | ||
4956 | ------------------------- | |
4957 | -- From_Actual_Package -- | |
4958 | ------------------------- | |
4959 | ||
4960 | function From_Actual_Package (E : Entity_Id) return Boolean is | |
4961 | Scop : constant Entity_Id := Scope (E); | |
fba9ebfc AC |
4962 | -- Declared scope of candidate entity |
4963 | ||
fba9ebfc AC |
4964 | function Declared_In_Actual (Pack : Entity_Id) return Boolean; |
4965 | -- Recursive function that does the work and examines actuals of | |
4966 | -- actual packages of current instance. | |
4967 | ||
4968 | ------------------------ | |
4969 | -- Declared_In_Actual -- | |
4970 | ------------------------ | |
4971 | ||
4972 | function Declared_In_Actual (Pack : Entity_Id) return Boolean is | |
4973 | Act : Entity_Id; | |
4974 | ||
4975 | begin | |
4976 | if No (Associated_Formal_Package (Pack)) then | |
4977 | return False; | |
4978 | ||
4979 | else | |
4980 | Act := First_Entity (Pack); | |
4981 | while Present (Act) loop | |
4982 | if Renamed_Object (Pack) = Scop then | |
4983 | return True; | |
4984 | ||
522aa6ee | 4985 | -- Check for end of list of actuals |
fba9ebfc AC |
4986 | |
4987 | elsif Ekind (Act) = E_Package | |
4988 | and then Renamed_Object (Act) = Pack | |
4989 | then | |
4990 | return False; | |
4991 | ||
4992 | elsif Ekind (Act) = E_Package | |
4993 | and then Declared_In_Actual (Act) | |
4994 | then | |
4995 | return True; | |
4996 | end if; | |
4997 | ||
4998 | Next_Entity (Act); | |
4999 | end loop; | |
5000 | ||
5001 | return False; | |
5002 | end if; | |
5003 | end Declared_In_Actual; | |
5004 | ||
522aa6ee AC |
5005 | -- Local variables |
5006 | ||
5007 | Act : Entity_Id; | |
5008 | ||
fba9ebfc | 5009 | -- Start of processing for From_Actual_Package |
996ae0b0 RK |
5010 | |
5011 | begin | |
5012 | if not In_Instance then | |
5013 | return False; | |
fba9ebfc | 5014 | |
996ae0b0 RK |
5015 | else |
5016 | Inst := Current_Scope; | |
996ae0b0 RK |
5017 | while Present (Inst) |
5018 | and then Ekind (Inst) /= E_Package | |
5019 | and then not Is_Generic_Instance (Inst) | |
5020 | loop | |
5021 | Inst := Scope (Inst); | |
5022 | end loop; | |
5023 | ||
5024 | if No (Inst) then | |
5025 | return False; | |
5026 | end if; | |
5027 | ||
5028 | Act := First_Entity (Inst); | |
996ae0b0 | 5029 | while Present (Act) loop |
fba9ebfc AC |
5030 | if Ekind (Act) = E_Package |
5031 | and then Declared_In_Actual (Act) | |
5032 | then | |
5033 | return True; | |
996ae0b0 | 5034 | end if; |
fba9ebfc AC |
5035 | |
5036 | Next_Entity (Act); | |
996ae0b0 RK |
5037 | end loop; |
5038 | ||
5039 | return False; | |
5040 | end if; | |
5041 | end From_Actual_Package; | |
5042 | ||
67ce0d7e RD |
5043 | ------------------------- |
5044 | -- Is_Actual_Parameter -- | |
5045 | ------------------------- | |
5046 | ||
5047 | function Is_Actual_Parameter return Boolean is | |
5048 | begin | |
5049 | return | |
5050 | Nkind (N) = N_Identifier | |
5051 | and then | |
5052 | (Nkind (Parent (N)) = N_Procedure_Call_Statement | |
ac7d724d ES |
5053 | or else |
5054 | (Nkind (Parent (N)) = N_Parameter_Association | |
5055 | and then N = Explicit_Actual_Parameter (Parent (N)) | |
5056 | and then Nkind (Parent (Parent (N))) = | |
67ce0d7e RD |
5057 | N_Procedure_Call_Statement)); |
5058 | end Is_Actual_Parameter; | |
5059 | ||
996ae0b0 RK |
5060 | ------------------------- |
5061 | -- Known_But_Invisible -- | |
5062 | ------------------------- | |
5063 | ||
5064 | function Known_But_Invisible (E : Entity_Id) return Boolean is | |
5065 | Fname : File_Name_Type; | |
5066 | ||
5067 | begin | |
5068 | -- Entities in Standard are always considered to be known | |
5069 | ||
5070 | if Sloc (E) <= Standard_Location then | |
5071 | return True; | |
5072 | ||
5073 | -- An entity that does not come from source is always considered | |
5074 | -- to be unknown, since it is an artifact of code expansion. | |
5075 | ||
5076 | elsif not Comes_From_Source (E) then | |
5077 | return False; | |
5078 | ||
a43f6434 AC |
5079 | -- In gnat internal mode, we consider all entities known. The |
5080 | -- historical reason behind this discrepancy is not known??? But the | |
5081 | -- only effect is to modify the error message given, so it is not | |
5082 | -- critical. Since it only affects the exact wording of error | |
5083 | -- messages in illegal programs, we do not mention this as an | |
5084 | -- effect of -gnatg, since it is not a language modification. | |
996ae0b0 RK |
5085 | |
5086 | elsif GNAT_Mode then | |
5087 | return True; | |
5088 | end if; | |
5089 | ||
5090 | -- Here we have an entity that is not from package Standard, and | |
5091 | -- which comes from Source. See if it comes from an internal file. | |
5092 | ||
5093 | Fname := Unit_File_Name (Get_Source_Unit (E)); | |
5094 | ||
5095 | -- Case of from internal file | |
5096 | ||
8ab31c0c | 5097 | if In_Internal_Unit (E) then |
996ae0b0 RK |
5098 | |
5099 | -- Private part entities in internal files are never considered | |
5100 | -- to be known to the writer of normal application code. | |
5101 | ||
5102 | if Is_Hidden (E) then | |
5103 | return False; | |
5104 | end if; | |
5105 | ||
5106 | -- Entities from System packages other than System and | |
5107 | -- System.Storage_Elements are not considered to be known. | |
5108 | -- System.Auxxxx files are also considered known to the user. | |
5109 | ||
5110 | -- Should refine this at some point to generally distinguish | |
5111 | -- between known and unknown internal files ??? | |
5112 | ||
5113 | Get_Name_String (Fname); | |
5114 | ||
5115 | return | |
5116 | Name_Len < 2 | |
5117 | or else | |
5118 | Name_Buffer (1 .. 2) /= "s-" | |
5119 | or else | |
5120 | Name_Buffer (3 .. 8) = "stoele" | |
5121 | or else | |
5122 | Name_Buffer (3 .. 5) = "aux"; | |
5123 | ||
497a660d AC |
5124 | -- If not an internal file, then entity is definitely known, even if |
5125 | -- it is in a private part (the message generated will note that it | |
5126 | -- is in a private part). | |
996ae0b0 RK |
5127 | |
5128 | else | |
5129 | return True; | |
5130 | end if; | |
5131 | end Known_But_Invisible; | |
5132 | ||
5133 | ------------------- | |
5134 | -- Nvis_Messages -- | |
5135 | ------------------- | |
5136 | ||
5137 | procedure Nvis_Messages is | |
9bc856dd AC |
5138 | Comp_Unit : Node_Id; |
5139 | Ent : Entity_Id; | |
1175f0b6 | 5140 | Found : Boolean := False; |
9bc856dd AC |
5141 | Hidden : Boolean := False; |
5142 | Item : Node_Id; | |
996ae0b0 RK |
5143 | |
5144 | begin | |
b3b3ada9 HK |
5145 | if not Errors_OK then |
5146 | return; | |
5147 | end if; | |
5148 | ||
0ab80019 | 5149 | -- Ada 2005 (AI-262): Generate a precise error concerning the |
9bc856dd AC |
5150 | -- Beaujolais effect that was previously detected |
5151 | ||
5152 | if Nvis_Is_Private_Subprg then | |
5153 | ||
5154 | pragma Assert (Nkind (E2) = N_Defining_Identifier | |
16ca248a ES |
5155 | and then Ekind (E2) = E_Function |
5156 | and then Scope (E2) = Standard_Standard | |
5157 | and then Has_Private_With (E2)); | |
9bc856dd AC |
5158 | |
5159 | -- Find the sloc corresponding to the private with'ed unit | |
5160 | ||
16ca248a | 5161 | Comp_Unit := Cunit (Current_Sem_Unit); |
9bc856dd AC |
5162 | Error_Msg_Sloc := No_Location; |
5163 | ||
16ca248a | 5164 | Item := First (Context_Items (Comp_Unit)); |
9bc856dd AC |
5165 | while Present (Item) loop |
5166 | if Nkind (Item) = N_With_Clause | |
5167 | and then Private_Present (Item) | |
5168 | and then Entity (Name (Item)) = E2 | |
5169 | then | |
5170 | Error_Msg_Sloc := Sloc (Item); | |
5171 | exit; | |
5172 | end if; | |
5173 | ||
5174 | Next (Item); | |
5175 | end loop; | |
5176 | ||
5177 | pragma Assert (Error_Msg_Sloc /= No_Location); | |
5178 | ||
0ab80019 | 5179 | Error_Msg_N ("(Ada 2005): hidden by private with clause #", N); |
9bc856dd AC |
5180 | return; |
5181 | end if; | |
5182 | ||
996ae0b0 RK |
5183 | Undefined (Nvis => True); |
5184 | ||
5185 | if Msg then | |
5186 | ||
5187 | -- First loop does hidden declarations | |
5188 | ||
5189 | Ent := Homonyms; | |
5190 | while Present (Ent) loop | |
5191 | if Is_Potentially_Use_Visible (Ent) then | |
996ae0b0 | 5192 | if not Hidden then |
deef4289 AC |
5193 | Error_Msg_N -- CODEFIX |
5194 | ("multiple use clauses cause hiding!", N); | |
996ae0b0 RK |
5195 | Hidden := True; |
5196 | end if; | |
5197 | ||
5198 | Error_Msg_Sloc := Sloc (Ent); | |
4e7a4f6e AC |
5199 | Error_Msg_N -- CODEFIX |
5200 | ("hidden declaration#!", N); | |
996ae0b0 RK |
5201 | end if; |
5202 | ||
5203 | Ent := Homonym (Ent); | |
5204 | end loop; | |
5205 | ||
5206 | -- If we found hidden declarations, then that's enough, don't | |
5207 | -- bother looking for non-visible declarations as well. | |
5208 | ||
5209 | if Hidden then | |
5210 | return; | |
5211 | end if; | |
5212 | ||
5213 | -- Second loop does non-directly visible declarations | |
5214 | ||
5215 | Ent := Homonyms; | |
5216 | while Present (Ent) loop | |
5217 | if not Is_Potentially_Use_Visible (Ent) then | |
5218 | ||
5219 | -- Do not bother the user with unknown entities | |
5220 | ||
5221 | if not Known_But_Invisible (Ent) then | |
5222 | goto Continue; | |
5223 | end if; | |
5224 | ||
5225 | Error_Msg_Sloc := Sloc (Ent); | |
5226 | ||
5227 | -- Output message noting that there is a non-visible | |
5228 | -- declaration, distinguishing the private part case. | |
5229 | ||
5230 | if Is_Hidden (Ent) then | |
5231 | Error_Msg_N ("non-visible (private) declaration#!", N); | |
1175f0b6 AC |
5232 | |
5233 | -- If the entity is declared in a generic package, it | |
5234 | -- cannot be visible, so there is no point in adding it | |
5235 | -- to the list of candidates if another homograph from a | |
5236 | -- non-generic package has been seen. | |
5237 | ||
5238 | elsif Ekind (Scope (Ent)) = E_Generic_Package | |
5239 | and then Found | |
5240 | then | |
5241 | null; | |
5242 | ||
996ae0b0 | 5243 | else |
483c78cb RD |
5244 | Error_Msg_N -- CODEFIX |
5245 | ("non-visible declaration#!", N); | |
fbf5a39b | 5246 | |
1175f0b6 AC |
5247 | if Ekind (Scope (Ent)) /= E_Generic_Package then |
5248 | Found := True; | |
5249 | end if; | |
5250 | ||
fbf5a39b AC |
5251 | if Is_Compilation_Unit (Ent) |
5252 | and then | |
5253 | Nkind (Parent (Parent (N))) = N_Use_Package_Clause | |
5254 | then | |
16ca248a | 5255 | Error_Msg_Qual_Level := 99; |
ed2233dc AC |
5256 | Error_Msg_NE -- CODEFIX |
5257 | ("\\missing `WITH &;`", N, Ent); | |
16ca248a | 5258 | Error_Msg_Qual_Level := 0; |
fbf5a39b | 5259 | end if; |
e1b871e9 AC |
5260 | |
5261 | if Ekind (Ent) = E_Discriminant | |
5262 | and then Present (Corresponding_Discriminant (Ent)) | |
5263 | and then Scope (Corresponding_Discriminant (Ent)) = | |
5264 | Etype (Scope (Ent)) | |
5265 | then | |
5266 | Error_Msg_N | |
5267 | ("inherited discriminant not allowed here" & | |
5268 | " (RM 3.8 (12), 3.8.1 (6))!", N); | |
5269 | end if; | |
996ae0b0 | 5270 | end if; |
07fc65c4 GB |
5271 | |
5272 | -- Set entity and its containing package as referenced. We | |
5273 | -- can't be sure of this, but this seems a better choice | |
5274 | -- to avoid unused entity messages. | |
5275 | ||
5276 | if Comes_From_Source (Ent) then | |
5277 | Set_Referenced (Ent); | |
5278 | Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent))); | |
5279 | end if; | |
996ae0b0 RK |
5280 | end if; |
5281 | ||
5282 | <<Continue>> | |
5283 | Ent := Homonym (Ent); | |
5284 | end loop; | |
996ae0b0 RK |
5285 | end if; |
5286 | end Nvis_Messages; | |
5287 | ||
5288 | --------------- | |
5289 | -- Undefined -- | |
5290 | --------------- | |
5291 | ||
5292 | procedure Undefined (Nvis : Boolean) is | |
5293 | Emsg : Error_Msg_Id; | |
5294 | ||
5295 | begin | |
fbf5a39b AC |
5296 | -- We should never find an undefined internal name. If we do, then |
5297 | -- see if we have previous errors. If so, ignore on the grounds that | |
5298 | -- it is probably a cascaded message (e.g. a block label from a badly | |
5299 | -- formed block). If no previous errors, then we have a real internal | |
5300 | -- error of some kind so raise an exception. | |
5301 | ||
5302 | if Is_Internal_Name (Chars (N)) then | |
5303 | if Total_Errors_Detected /= 0 then | |
5304 | return; | |
5305 | else | |
5306 | raise Program_Error; | |
5307 | end if; | |
5308 | end if; | |
5309 | ||
996ae0b0 RK |
5310 | -- A very specialized error check, if the undefined variable is |
5311 | -- a case tag, and the case type is an enumeration type, check | |
5312 | -- for a possible misspelling, and if so, modify the identifier | |
5313 | ||
5314 | -- Named aggregate should also be handled similarly ??? | |
5315 | ||
b3b3ada9 HK |
5316 | if Errors_OK |
5317 | and then Nkind (N) = N_Identifier | |
996ae0b0 RK |
5318 | and then Nkind (Parent (N)) = N_Case_Statement_Alternative |
5319 | then | |
996ae0b0 | 5320 | declare |
996ae0b0 RK |
5321 | Case_Stm : constant Node_Id := Parent (Parent (N)); |
5322 | Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm)); | |
5323 | ||
5324 | Lit : Node_Id; | |
5325 | ||
5326 | begin | |
5327 | if Is_Enumeration_Type (Case_Typ) | |
21d27997 | 5328 | and then not Is_Standard_Character_Type (Case_Typ) |
996ae0b0 RK |
5329 | then |
5330 | Lit := First_Literal (Case_Typ); | |
5331 | Get_Name_String (Chars (Lit)); | |
5332 | ||
5333 | if Chars (Lit) /= Chars (N) | |
0b7f0f0e AC |
5334 | and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit)) |
5335 | then | |
996ae0b0 | 5336 | Error_Msg_Node_2 := Lit; |
ed2233dc | 5337 | Error_Msg_N -- CODEFIX |
996ae0b0 RK |
5338 | ("& is undefined, assume misspelling of &", N); |
5339 | Rewrite (N, New_Occurrence_Of (Lit, Sloc (N))); | |
5340 | return; | |
5341 | end if; | |
5342 | ||
5343 | Lit := Next_Literal (Lit); | |
5344 | end if; | |
5345 | end; | |
5346 | end if; | |
5347 | ||
5348 | -- Normal processing | |
5349 | ||
5350 | Set_Entity (N, Any_Id); | |
5351 | Set_Etype (N, Any_Type); | |
5352 | ||
b3b3ada9 | 5353 | if Errors_OK then |
996ae0b0 | 5354 | |
b3b3ada9 HK |
5355 | -- We use the table Urefs to keep track of entities for which we |
5356 | -- have issued errors for undefined references. Multiple errors | |
5357 | -- for a single name are normally suppressed, however we modify | |
5358 | -- the error message to alert the programmer to this effect. | |
996ae0b0 | 5359 | |
b3b3ada9 HK |
5360 | for J in Urefs.First .. Urefs.Last loop |
5361 | if Chars (N) = Chars (Urefs.Table (J).Node) then | |
5362 | if Urefs.Table (J).Err /= No_Error_Msg | |
5363 | and then Sloc (N) /= Urefs.Table (J).Loc | |
5364 | then | |
5365 | Error_Msg_Node_1 := Urefs.Table (J).Node; | |
996ae0b0 | 5366 | |
b3b3ada9 HK |
5367 | if Urefs.Table (J).Nvis then |
5368 | Change_Error_Text (Urefs.Table (J).Err, | |
5369 | "& is not visible (more references follow)"); | |
5370 | else | |
5371 | Change_Error_Text (Urefs.Table (J).Err, | |
5372 | "& is undefined (more references follow)"); | |
5373 | end if; | |
996ae0b0 | 5374 | |
b3b3ada9 HK |
5375 | Urefs.Table (J).Err := No_Error_Msg; |
5376 | end if; | |
996ae0b0 | 5377 | |
b3b3ada9 HK |
5378 | -- Although we will set Msg False, and thus suppress the |
5379 | -- message, we also set Error_Posted True, to avoid any | |
5380 | -- cascaded messages resulting from the undefined reference. | |
996ae0b0 | 5381 | |
b3b3ada9 HK |
5382 | Msg := False; |
5383 | Set_Error_Posted (N); | |
5384 | return; | |
5385 | end if; | |
5386 | end loop; | |
996ae0b0 | 5387 | |
b3b3ada9 | 5388 | -- If entry not found, this is first undefined occurrence |
996ae0b0 | 5389 | |
b3b3ada9 HK |
5390 | if Nvis then |
5391 | Error_Msg_N ("& is not visible!", N); | |
5392 | Emsg := Get_Msg_Id; | |
996ae0b0 | 5393 | |
b3b3ada9 HK |
5394 | else |
5395 | Error_Msg_N ("& is undefined!", N); | |
5396 | Emsg := Get_Msg_Id; | |
996ae0b0 | 5397 | |
b3b3ada9 HK |
5398 | -- A very bizarre special check, if the undefined identifier |
5399 | -- is Put or Put_Line, then add a special error message (since | |
5400 | -- this is a very common error for beginners to make). | |
16ca248a | 5401 | |
b3b3ada9 HK |
5402 | if Nam_In (Chars (N), Name_Put, Name_Put_Line) then |
5403 | Error_Msg_N -- CODEFIX | |
5404 | ("\\possible missing `WITH Ada.Text_'I'O; " & | |
5405 | "USE Ada.Text_'I'O`!", N); | |
16ca248a | 5406 | |
b3b3ada9 HK |
5407 | -- Another special check if N is the prefix of a selected |
5408 | -- component which is a known unit: add message complaining | |
5409 | -- about missing with for this unit. | |
996ae0b0 | 5410 | |
b3b3ada9 HK |
5411 | elsif Nkind (Parent (N)) = N_Selected_Component |
5412 | and then N = Prefix (Parent (N)) | |
5413 | and then Is_Known_Unit (Parent (N)) | |
5414 | then | |
5415 | Error_Msg_Node_2 := Selector_Name (Parent (N)); | |
5416 | Error_Msg_N -- CODEFIX | |
5417 | ("\\missing `WITH &.&;`", Prefix (Parent (N))); | |
5418 | end if; | |
996ae0b0 | 5419 | |
b3b3ada9 | 5420 | -- Now check for possible misspellings |
996ae0b0 | 5421 | |
b3b3ada9 HK |
5422 | declare |
5423 | E : Entity_Id; | |
5424 | Ematch : Entity_Id := Empty; | |
996ae0b0 | 5425 | |
b3b3ada9 HK |
5426 | Last_Name_Id : constant Name_Id := |
5427 | Name_Id (Nat (First_Name_Id) + | |
5428 | Name_Entries_Count - 1); | |
996ae0b0 | 5429 | |
b3b3ada9 HK |
5430 | begin |
5431 | for Nam in First_Name_Id .. Last_Name_Id loop | |
5432 | E := Get_Name_Entity_Id (Nam); | |
5433 | ||
5434 | if Present (E) | |
5435 | and then (Is_Immediately_Visible (E) | |
5436 | or else | |
5437 | Is_Potentially_Use_Visible (E)) | |
5438 | then | |
5439 | if Is_Bad_Spelling_Of (Chars (N), Nam) then | |
5440 | Ematch := E; | |
5441 | exit; | |
5442 | end if; | |
996ae0b0 | 5443 | end if; |
b3b3ada9 HK |
5444 | end loop; |
5445 | ||
5446 | if Present (Ematch) then | |
5447 | Error_Msg_NE -- CODEFIX | |
5448 | ("\possible misspelling of&", N, Ematch); | |
996ae0b0 | 5449 | end if; |
b3b3ada9 HK |
5450 | end; |
5451 | end if; | |
996ae0b0 | 5452 | |
b3b3ada9 HK |
5453 | -- Make entry in undefined references table unless the full errors |
5454 | -- switch is set, in which case by refraining from generating the | |
5455 | -- table entry we guarantee that we get an error message for every | |
5456 | -- undefined reference. The entry is not added if we are ignoring | |
5457 | -- errors. | |
5458 | ||
5459 | if not All_Errors_Mode and then Ignore_Errors_Enable = 0 then | |
5460 | Urefs.Append ( | |
5461 | (Node => N, | |
5462 | Err => Emsg, | |
5463 | Nvis => Nvis, | |
5464 | Loc => Sloc (N))); | |
5465 | end if; | |
996ae0b0 | 5466 | |
b3b3ada9 | 5467 | Msg := True; |
996ae0b0 | 5468 | end if; |
996ae0b0 RK |
5469 | end Undefined; |
5470 | ||
522aa6ee AC |
5471 | -- Local variables |
5472 | ||
5473 | Nested_Inst : Entity_Id := Empty; | |
5474 | -- The entity of a nested instance which appears within Inst (if any) | |
5475 | ||
996ae0b0 RK |
5476 | -- Start of processing for Find_Direct_Name |
5477 | ||
5478 | begin | |
5479 | -- If the entity pointer is already set, this is an internal node, or | |
5480 | -- a node that is analyzed more than once, after a tree modification. | |
5481 | -- In such a case there is no resolution to perform, just set the type. | |
5482 | ||
5483 | if Present (Entity (N)) then | |
5484 | if Is_Type (Entity (N)) then | |
5485 | Set_Etype (N, Entity (N)); | |
5486 | ||
5487 | else | |
5488 | declare | |
5489 | Entyp : constant Entity_Id := Etype (Entity (N)); | |
5490 | ||
5491 | begin | |
5492 | -- One special case here. If the Etype field is already set, | |
5493 | -- and references the packed array type corresponding to the | |
5494 | -- etype of the referenced entity, then leave it alone. This | |
5495 | -- happens for trees generated from Exp_Pakd, where expressions | |
5496 | -- can be deliberately "mis-typed" to the packed array type. | |
5497 | ||
5498 | if Is_Array_Type (Entyp) | |
5499 | and then Is_Packed (Entyp) | |
5500 | and then Present (Etype (N)) | |
8ca597af | 5501 | and then Etype (N) = Packed_Array_Impl_Type (Entyp) |
996ae0b0 RK |
5502 | then |
5503 | null; | |
5504 | ||
5505 | -- If not that special case, then just reset the Etype | |
5506 | ||
5507 | else | |
5508 | Set_Etype (N, Etype (Entity (N))); | |
5509 | end if; | |
5510 | end; | |
5511 | end if; | |
5512 | ||
851e9f19 PMR |
5513 | -- Although the marking of use clauses happens at the end of |
5514 | -- Find_Direct_Name, a certain case where a generic actual satisfies | |
5515 | -- a use clause must be checked here due to how the generic machinery | |
5516 | -- handles the analysis of said actuals. | |
5517 | ||
5518 | if In_Instance | |
5519 | and then Nkind (Parent (N)) = N_Generic_Association | |
5520 | then | |
5521 | Mark_Use_Clauses (Entity (N)); | |
5522 | end if; | |
5523 | ||
996ae0b0 RK |
5524 | return; |
5525 | end if; | |
5526 | ||
90e491a7 PMR |
5527 | -- Preserve relevant elaboration-related attributes of the context which |
5528 | -- are no longer available or very expensive to recompute once analysis, | |
5529 | -- resolution, and expansion are over. | |
5530 | ||
5531 | if Nkind (N) = N_Identifier then | |
5532 | Mark_Elaboration_Attributes | |
69e6ee2f HK |
5533 | (N_Id => N, |
5534 | Checks => True, | |
5535 | Modes => True, | |
5536 | Warnings => True); | |
90e491a7 PMR |
5537 | end if; |
5538 | ||
996ae0b0 RK |
5539 | -- Here if Entity pointer was not set, we need full visibility analysis |
5540 | -- First we generate debugging output if the debug E flag is set. | |
5541 | ||
5542 | if Debug_Flag_E then | |
5543 | Write_Str ("Looking for "); | |
5544 | Write_Name (Chars (N)); | |
5545 | Write_Eol; | |
5546 | end if; | |
5547 | ||
5548 | Homonyms := Current_Entity (N); | |
5549 | Nvis_Entity := False; | |
5550 | ||
5551 | E := Homonyms; | |
5552 | while Present (E) loop | |
5553 | ||
ff81221b ES |
5554 | -- If entity is immediately visible or potentially use visible, then |
5555 | -- process the entity and we are done. | |
996ae0b0 RK |
5556 | |
5557 | if Is_Immediately_Visible (E) then | |
5558 | goto Immediately_Visible_Entity; | |
5559 | ||
5560 | elsif Is_Potentially_Use_Visible (E) then | |
5561 | goto Potentially_Use_Visible_Entity; | |
5562 | ||
5563 | -- Note if a known but invisible entity encountered | |
5564 | ||
5565 | elsif Known_But_Invisible (E) then | |
5566 | Nvis_Entity := True; | |
5567 | end if; | |
5568 | ||
5569 | -- Move to next entity in chain and continue search | |
5570 | ||
5571 | E := Homonym (E); | |
5572 | end loop; | |
5573 | ||
5574 | -- If no entries on homonym chain that were potentially visible, | |
5575 | -- and no entities reasonably considered as non-visible, then | |
5576 | -- we have a plain undefined reference, with no additional | |
a90bd866 | 5577 | -- explanation required. |
996ae0b0 RK |
5578 | |
5579 | if not Nvis_Entity then | |
5580 | Undefined (Nvis => False); | |
996ae0b0 RK |
5581 | |
5582 | -- Otherwise there is at least one entry on the homonym chain that | |
5583 | -- is reasonably considered as being known and non-visible. | |
5584 | ||
5585 | else | |
5586 | Nvis_Messages; | |
996ae0b0 RK |
5587 | end if; |
5588 | ||
18dae814 | 5589 | goto Done; |
fbf5a39b | 5590 | |
996ae0b0 RK |
5591 | -- Processing for a potentially use visible entry found. We must search |
5592 | -- the rest of the homonym chain for two reasons. First, if there is a | |
5593 | -- directly visible entry, then none of the potentially use-visible | |
5594 | -- entities are directly visible (RM 8.4(10)). Second, we need to check | |
5595 | -- for the case of multiple potentially use-visible entries hiding one | |
5596 | -- another and as a result being non-directly visible (RM 8.4(11)). | |
5597 | ||
5598 | <<Potentially_Use_Visible_Entity>> declare | |
5599 | Only_One_Visible : Boolean := True; | |
5600 | All_Overloadable : Boolean := Is_Overloadable (E); | |
5601 | ||
5602 | begin | |
5603 | E2 := Homonym (E); | |
996ae0b0 RK |
5604 | while Present (E2) loop |
5605 | if Is_Immediately_Visible (E2) then | |
5606 | ||
5607 | -- If the use-visible entity comes from the actual for a | |
5608 | -- formal package, it hides a directly visible entity from | |
5609 | -- outside the instance. | |
5610 | ||
5611 | if From_Actual_Package (E) | |
5612 | and then Scope_Depth (E2) < Scope_Depth (Inst) | |
5613 | then | |
5614 | goto Found; | |
5615 | else | |
5616 | E := E2; | |
5617 | goto Immediately_Visible_Entity; | |
5618 | end if; | |
5619 | ||
5620 | elsif Is_Potentially_Use_Visible (E2) then | |
5621 | Only_One_Visible := False; | |
5622 | All_Overloadable := All_Overloadable and Is_Overloadable (E2); | |
9bc856dd | 5623 | |
30783513 | 5624 | -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect |
f3d57416 | 5625 | -- that can occur in private_with clauses. Example: |
9bc856dd AC |
5626 | |
5627 | -- with A; | |
5628 | -- private with B; package A is | |
5629 | -- package C is function B return Integer; | |
5630 | -- use A; end A; | |
5631 | -- V1 : Integer := B; | |
5632 | -- private function B return Integer; | |
5633 | -- V2 : Integer := B; | |
5634 | -- end C; | |
5635 | ||
bc41faa2 | 5636 | -- V1 resolves to A.B, but V2 resolves to library unit B |
9bc856dd AC |
5637 | |
5638 | elsif Ekind (E2) = E_Function | |
5639 | and then Scope (E2) = Standard_Standard | |
5640 | and then Has_Private_With (E2) | |
5641 | then | |
5642 | Only_One_Visible := False; | |
5643 | All_Overloadable := False; | |
5644 | Nvis_Is_Private_Subprg := True; | |
5645 | exit; | |
996ae0b0 RK |
5646 | end if; |
5647 | ||
5648 | E2 := Homonym (E2); | |
5649 | end loop; | |
5650 | ||
5651 | -- On falling through this loop, we have checked that there are no | |
5652 | -- immediately visible entities. Only_One_Visible is set if exactly | |
5653 | -- one potentially use visible entity exists. All_Overloadable is | |
5654 | -- set if all the potentially use visible entities are overloadable. | |
5655 | -- The condition for legality is that either there is one potentially | |
5656 | -- use visible entity, or if there is more than one, then all of them | |
5657 | -- are overloadable. | |
5658 | ||
5659 | if Only_One_Visible or All_Overloadable then | |
5660 | goto Found; | |
5661 | ||
5662 | -- If there is more than one potentially use-visible entity and at | |
4ff4293f | 5663 | -- least one of them non-overloadable, we have an error (RM 8.4(11)). |
996ae0b0 | 5664 | -- Note that E points to the first such entity on the homonym list. |
996ae0b0 RK |
5665 | |
5666 | else | |
522aa6ee AC |
5667 | -- If one of the entities is declared in an actual package, it |
5668 | -- was visible in the generic, and takes precedence over other | |
5669 | -- entities that are potentially use-visible. The same applies | |
5670 | -- if the entity is declared in a local instantiation of the | |
5671 | -- current instance. | |
5672 | ||
996ae0b0 | 5673 | if In_Instance then |
fbf5a39b | 5674 | |
522aa6ee | 5675 | -- Find the current instance |
fbf5a39b | 5676 | |
16ca248a | 5677 | Inst := Current_Scope; |
ac7d724d | 5678 | while Present (Inst) and then Inst /= Standard_Standard loop |
fbf5a39b AC |
5679 | if Is_Generic_Instance (Inst) then |
5680 | exit; | |
5681 | end if; | |
5682 | ||
5683 | Inst := Scope (Inst); | |
5684 | end loop; | |
5685 | ||
522aa6ee AC |
5686 | -- Reexamine the candidate entities, giving priority to those |
5687 | -- that were visible within the generic. | |
5688 | ||
996ae0b0 | 5689 | E2 := E; |
996ae0b0 | 5690 | while Present (E2) loop |
522aa6ee AC |
5691 | Nested_Inst := Nearest_Enclosing_Instance (E2); |
5692 | ||
5693 | -- The entity is declared within an actual package, or in a | |
5694 | -- nested instance. The ">=" accounts for the case where the | |
5695 | -- current instance and the nested instance are the same. | |
5696 | ||
fbf5a39b | 5697 | if From_Actual_Package (E2) |
522aa6ee AC |
5698 | or else (Present (Nested_Inst) |
5699 | and then Scope_Depth (Nested_Inst) >= | |
5700 | Scope_Depth (Inst)) | |
fbf5a39b | 5701 | then |
996ae0b0 RK |
5702 | E := E2; |
5703 | goto Found; | |
5704 | end if; | |
5705 | ||
5706 | E2 := Homonym (E2); | |
5707 | end loop; | |
5708 | ||
5709 | Nvis_Messages; | |
18dae814 | 5710 | goto Done; |
996ae0b0 | 5711 | |
8ab31c0c | 5712 | elsif Is_Predefined_Unit (Current_Sem_Unit) then |
851e9f19 | 5713 | -- A use clause in the body of a system file creates conflict |
4de287c4 ES |
5714 | -- with some entity in a user scope, while rtsfind is active. |
5715 | -- Keep only the entity coming from another predefined unit. | |
5eb10f25 ES |
5716 | |
5717 | E2 := E; | |
5718 | while Present (E2) loop | |
8ab31c0c | 5719 | if In_Predefined_Unit (E2) then |
5eb10f25 ES |
5720 | E := E2; |
5721 | goto Found; | |
5722 | end if; | |
5723 | ||
5724 | E2 := Homonym (E2); | |
5725 | end loop; | |
5726 | ||
4de287c4 | 5727 | -- Entity must exist because predefined unit is correct |
5eb10f25 ES |
5728 | |
5729 | raise Program_Error; | |
5730 | ||
996ae0b0 RK |
5731 | else |
5732 | Nvis_Messages; | |
18dae814 | 5733 | goto Done; |
996ae0b0 RK |
5734 | end if; |
5735 | end if; | |
5736 | end; | |
5737 | ||
5738 | -- Come here with E set to the first immediately visible entity on | |
5739 | -- the homonym chain. This is the one we want unless there is another | |
ff81221b ES |
5740 | -- immediately visible entity further on in the chain for an inner |
5741 | -- scope (RM 8.3(8)). | |
996ae0b0 RK |
5742 | |
5743 | <<Immediately_Visible_Entity>> declare | |
5744 | Level : Int; | |
5745 | Scop : Entity_Id; | |
5746 | ||
5747 | begin | |
ff81221b | 5748 | -- Find scope level of initial entity. When compiling through |
996ae0b0 RK |
5749 | -- Rtsfind, the previous context is not completely invisible, and |
5750 | -- an outer entity may appear on the chain, whose scope is below | |
5751 | -- the entry for Standard that delimits the current scope stack. | |
5752 | -- Indicate that the level for this spurious entry is outside of | |
5753 | -- the current scope stack. | |
5754 | ||
5755 | Level := Scope_Stack.Last; | |
5756 | loop | |
5757 | Scop := Scope_Stack.Table (Level).Entity; | |
5758 | exit when Scop = Scope (E); | |
5759 | Level := Level - 1; | |
5760 | exit when Scop = Standard_Standard; | |
5761 | end loop; | |
5762 | ||
5763 | -- Now search remainder of homonym chain for more inner entry | |
5764 | -- If the entity is Standard itself, it has no scope, and we | |
5765 | -- compare it with the stack entry directly. | |
5766 | ||
5767 | E2 := Homonym (E); | |
5768 | while Present (E2) loop | |
5769 | if Is_Immediately_Visible (E2) then | |
4de287c4 ES |
5770 | |
5771 | -- If a generic package contains a local declaration that | |
5772 | -- has the same name as the generic, there may be a visibility | |
5773 | -- conflict in an instance, where the local declaration must | |
5774 | -- also hide the name of the corresponding package renaming. | |
5775 | -- We check explicitly for a package declared by a renaming, | |
5776 | -- whose renamed entity is an instance that is on the scope | |
5777 | -- stack, and that contains a homonym in the same scope. Once | |
5778 | -- we have found it, we know that the package renaming is not | |
5779 | -- immediately visible, and that the identifier denotes the | |
5780 | -- other entity (and its homonyms if overloaded). | |
5781 | ||
5782 | if Scope (E) = Scope (E2) | |
5783 | and then Ekind (E) = E_Package | |
5784 | and then Present (Renamed_Object (E)) | |
5785 | and then Is_Generic_Instance (Renamed_Object (E)) | |
5786 | and then In_Open_Scopes (Renamed_Object (E)) | |
5787 | and then Comes_From_Source (N) | |
5788 | then | |
5789 | Set_Is_Immediately_Visible (E, False); | |
5790 | E := E2; | |
5791 | ||
5792 | else | |
5793 | for J in Level + 1 .. Scope_Stack.Last loop | |
5794 | if Scope_Stack.Table (J).Entity = Scope (E2) | |
5795 | or else Scope_Stack.Table (J).Entity = E2 | |
5796 | then | |
5797 | Level := J; | |
5798 | E := E2; | |
5799 | exit; | |
5800 | end if; | |
5801 | end loop; | |
5802 | end if; | |
996ae0b0 RK |
5803 | end if; |
5804 | ||
5805 | E2 := Homonym (E2); | |
5806 | end loop; | |
5807 | ||
5808 | -- At the end of that loop, E is the innermost immediately | |
5809 | -- visible entity, so we are all set. | |
5810 | end; | |
5811 | ||
5812 | -- Come here with entity found, and stored in E | |
5813 | ||
5814 | <<Found>> begin | |
5815 | ||
30196a76 RD |
5816 | -- Check violation of No_Wide_Characters restriction |
5817 | ||
5818 | Check_Wide_Character_Restriction (E, N); | |
5819 | ||
294ccb21 RD |
5820 | -- When distribution features are available (Get_PCS_Name /= |
5821 | -- Name_No_DSA), a remote access-to-subprogram type is converted | |
5822 | -- into a record type holding whatever information is needed to | |
f3d57416 | 5823 | -- perform a remote call on an RCI subprogram. In that case we |
294ccb21 RD |
5824 | -- rewrite any occurrence of the RAS type into the equivalent record |
5825 | -- type here. 'Access attribute references and RAS dereferences are | |
5826 | -- then implemented using specific TSSs. However when distribution is | |
5827 | -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the | |
5828 | -- generation of these TSSs, and we must keep the RAS type in its | |
5829 | -- original access-to-subprogram form (since all calls through a | |
5830 | -- value of such type will be local anyway in the absence of a PCS). | |
5831 | ||
996ae0b0 RK |
5832 | if Comes_From_Source (N) |
5833 | and then Is_Remote_Access_To_Subprogram_Type (E) | |
d1d81616 | 5834 | and then Ekind (E) = E_Access_Subprogram_Type |
996ae0b0 | 5835 | and then Expander_Active |
a77842bd | 5836 | and then Get_PCS_Name /= Name_No_DSA |
996ae0b0 | 5837 | then |
18dae814 RD |
5838 | Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N))); |
5839 | goto Done; | |
996ae0b0 RK |
5840 | end if; |
5841 | ||
6989bc1f | 5842 | -- Set the entity. Note that the reason we call Set_Entity for the |
e7ba564f | 5843 | -- overloadable case, as opposed to Set_Entity_With_Checks is |
6989bc1f AC |
5844 | -- that in the overloaded case, the initial call can set the wrong |
5845 | -- homonym. The call that sets the right homonym is in Sem_Res and | |
e7ba564f | 5846 | -- that call does use Set_Entity_With_Checks, so we don't miss |
6989bc1f AC |
5847 | -- a style check. |
5848 | ||
5849 | if Is_Overloadable (E) then | |
5850 | Set_Entity (N, E); | |
5851 | else | |
e7ba564f | 5852 | Set_Entity_With_Checks (N, E); |
6989bc1f | 5853 | end if; |
996ae0b0 RK |
5854 | |
5855 | if Is_Type (E) then | |
5856 | Set_Etype (N, E); | |
5857 | else | |
5858 | Set_Etype (N, Get_Full_View (Etype (E))); | |
5859 | end if; | |
5860 | ||
5861 | if Debug_Flag_E then | |
5862 | Write_Str (" found "); | |
5863 | Write_Entity_Info (E, " "); | |
5864 | end if; | |
5865 | ||
5866 | -- If the Ekind of the entity is Void, it means that all homonyms | |
5867 | -- are hidden from all visibility (RM 8.3(5,14-20)). However, this | |
5868 | -- test is skipped if the current scope is a record and the name is | |
5869 | -- a pragma argument expression (case of Atomic and Volatile pragmas | |
5870 | -- and possibly other similar pragmas added later, which are allowed | |
5871 | -- to reference components in the current record). | |
5872 | ||
5873 | if Ekind (E) = E_Void | |
5874 | and then | |
5875 | (not Is_Record_Type (Current_Scope) | |
5876 | or else Nkind (Parent (N)) /= N_Pragma_Argument_Association) | |
5877 | then | |
5878 | Premature_Usage (N); | |
5879 | ||
16ca248a ES |
5880 | -- If the entity is overloadable, collect all interpretations of the |
5881 | -- name for subsequent overload resolution. We optimize a bit here to | |
5882 | -- do this only if we have an overloadable entity that is not on its | |
5883 | -- own on the homonym chain. | |
996ae0b0 RK |
5884 | |
5885 | elsif Is_Overloadable (E) | |
5886 | and then (Present (Homonym (E)) or else Current_Entity (N) /= E) | |
5887 | then | |
5888 | Collect_Interps (N); | |
5889 | ||
bc41faa2 | 5890 | -- If no homonyms were visible, the entity is unambiguous |
996ae0b0 RK |
5891 | |
5892 | if not Is_Overloaded (N) then | |
b3b3ada9 | 5893 | if Reference_OK and then not Is_Actual_Parameter then |
67ce0d7e RD |
5894 | Generate_Reference (E, N); |
5895 | end if; | |
996ae0b0 RK |
5896 | end if; |
5897 | ||
5898 | -- Case of non-overloadable entity, set the entity providing that | |
5899 | -- we do not have the case of a discriminant reference within a | |
5900 | -- default expression. Such references are replaced with the | |
5901 | -- corresponding discriminal, which is the formal corresponding to | |
5902 | -- to the discriminant in the initialization procedure. | |
5903 | ||
996ae0b0 | 5904 | else |
294ccb21 RD |
5905 | -- Entity is unambiguous, indicate that it is referenced here |
5906 | ||
5907 | -- For a renaming of an object, always generate simple reference, | |
5114f3ff AC |
5908 | -- we don't try to keep track of assignments in this case, except |
5909 | -- in SPARK mode where renamings are traversed for generating | |
5910 | -- local effects of subprograms. | |
294ccb21 | 5911 | |
b3b3ada9 HK |
5912 | if Reference_OK |
5913 | and then Is_Object (E) | |
5114f3ff | 5914 | and then Present (Renamed_Object (E)) |
f5da7a97 | 5915 | and then not GNATprove_Mode |
5114f3ff | 5916 | then |
294ccb21 | 5917 | Generate_Reference (E, N); |
996ae0b0 | 5918 | |
21d27997 RD |
5919 | -- If the renamed entity is a private protected component, |
5920 | -- reference the original component as well. This needs to be | |
5921 | -- done because the private renamings are installed before any | |
f3d0f304 | 5922 | -- analysis has occurred. Reference to a private component will |
21d27997 RD |
5923 | -- resolve to the renaming and the original component will be |
5924 | -- left unreferenced, hence the following. | |
5925 | ||
5926 | if Is_Prival (E) then | |
5927 | Generate_Reference (Prival_Link (E), N); | |
5928 | end if; | |
5929 | ||
294ccb21 RD |
5930 | -- One odd case is that we do not want to set the Referenced flag |
5931 | -- if the entity is a label, and the identifier is the label in | |
5932 | -- the source, since this is not a reference from the point of | |
5933 | -- view of the user. | |
5934 | ||
5935 | elsif Nkind (Parent (N)) = N_Label then | |
996ae0b0 RK |
5936 | declare |
5937 | R : constant Boolean := Referenced (E); | |
294ccb21 | 5938 | |
996ae0b0 | 5939 | begin |
294ccb21 RD |
5940 | -- Generate reference unless this is an actual parameter |
5941 | -- (see comment below) | |
5942 | ||
b3b3ada9 | 5943 | if Reference_OK and then Is_Actual_Parameter then |
67ce0d7e RD |
5944 | Generate_Reference (E, N); |
5945 | Set_Referenced (E, R); | |
5946 | end if; | |
996ae0b0 RK |
5947 | end; |
5948 | ||
11560bcc TQ |
5949 | -- Normal case, not a label: generate reference |
5950 | ||
a54ffd6c | 5951 | else |
b3b3ada9 | 5952 | if Reference_OK and then not Is_Actual_Parameter then |
61c161b2 | 5953 | |
a54ffd6c | 5954 | -- Package or generic package is always a simple reference |
61c161b2 | 5955 | |
a54ffd6c AC |
5956 | if Ekind_In (E, E_Package, E_Generic_Package) then |
5957 | Generate_Reference (E, N, 'r'); | |
5958 | ||
5959 | -- Else see if we have a left hand side | |
fbf5a39b | 5960 | |
84df40f7 | 5961 | else |
a54ffd6c AC |
5962 | case Is_LHS (N) is |
5963 | when Yes => | |
5964 | Generate_Reference (E, N, 'm'); | |
5965 | ||
5966 | when No => | |
5967 | Generate_Reference (E, N, 'r'); | |
5968 | ||
5969 | -- If we don't know now, generate reference later | |
5970 | ||
d8f43ee6 HK |
5971 | when Unknown => |
5972 | Deferred_References.Append ((E, N)); | |
a54ffd6c | 5973 | end case; |
84df40f7 | 5974 | end if; |
67ce0d7e | 5975 | end if; |
996ae0b0 RK |
5976 | end if; |
5977 | ||
4c484f40 | 5978 | Set_Entity_Or_Discriminal (N, E); |
d50f4827 | 5979 | |
27dd0dde | 5980 | -- The name may designate a generalized reference, in which case |
f6f40114 AC |
5981 | -- the dereference interpretation will be included. Context is |
5982 | -- one in which a name is legal. | |
27dd0dde | 5983 | |
d50f4827 | 5984 | if Ada_Version >= Ada_2012 |
444acbdd AC |
5985 | and then |
5986 | (Nkind (Parent (N)) in N_Subexpr | |
cdabbb52 HK |
5987 | or else Nkind_In (Parent (N), N_Assignment_Statement, |
5988 | N_Object_Declaration, | |
5989 | N_Parameter_Association)) | |
d50f4827 AC |
5990 | then |
5991 | Check_Implicit_Dereference (N, Etype (E)); | |
5992 | end if; | |
996ae0b0 RK |
5993 | end if; |
5994 | end; | |
18dae814 | 5995 | |
851e9f19 PMR |
5996 | -- Mark relevant use-type and use-package clauses as effective if the |
5997 | -- node in question is not overloaded and therefore does not require | |
5998 | -- resolution. | |
5999 | -- | |
6000 | -- Note: Generic actual subprograms do not follow the normal resolution | |
6001 | -- path, so ignore the fact that they are overloaded and mark them | |
6002 | -- anyway. | |
6003 | ||
7f5e671b | 6004 | if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then |
851e9f19 PMR |
6005 | Mark_Use_Clauses (N); |
6006 | end if; | |
6007 | ||
18dae814 RD |
6008 | -- Come here with entity set |
6009 | ||
6010 | <<Done>> | |
6011 | Check_Restriction_No_Use_Of_Entity (N); | |
90e491a7 | 6012 | |
daf82dd8 HK |
6013 | -- Annotate the tree by creating a variable reference marker in case the |
6014 | -- original variable reference is folded or optimized away. The variable | |
6015 | -- reference marker is automatically saved for later examination by the | |
6016 | -- ABE Processing phase. Variable references which act as actuals in a | |
6017 | -- call require special processing and are left to Resolve_Actuals. The | |
6018 | -- reference is a write when it appears on the left hand side of an | |
6019 | -- assignment. | |
6020 | ||
b3b3ada9 HK |
6021 | if Marker_OK |
6022 | and then Needs_Variable_Reference_Marker | |
6023 | (N => N, | |
6024 | Calls_OK => False) | |
692918a9 HK |
6025 | then |
6026 | declare | |
6027 | Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes; | |
6028 | ||
6029 | begin | |
6030 | Build_Variable_Reference_Marker | |
6031 | (N => N, | |
6032 | Read => not Is_Assignment_LHS, | |
6033 | Write => Is_Assignment_LHS); | |
6034 | end; | |
daf82dd8 | 6035 | end if; |
996ae0b0 RK |
6036 | end Find_Direct_Name; |
6037 | ||
6038 | ------------------------ | |
6039 | -- Find_Expanded_Name -- | |
6040 | ------------------------ | |
6041 | ||
6042 | -- This routine searches the homonym chain of the entity until it finds | |
6043 | -- an entity declared in the scope denoted by the prefix. If the entity | |
6044 | -- is private, it may nevertheless be immediately visible, if we are in | |
6045 | -- the scope of its declaration. | |
6046 | ||
6047 | procedure Find_Expanded_Name (N : Node_Id) is | |
95fef24f AC |
6048 | function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean; |
6049 | -- Determine whether expanded name Nod appears within a pragma which is | |
6050 | -- a suitable context for an abstract view of a state or variable. The | |
6051 | -- following pragmas fall in this category: | |
6052 | -- Depends | |
6053 | -- Global | |
6054 | -- Initializes | |
6055 | -- Refined_Depends | |
6056 | -- Refined_Global | |
6057 | -- | |
6058 | -- In addition, pragma Abstract_State is also considered suitable even | |
6059 | -- though it is an illegal context for an abstract view as this allows | |
6060 | -- for proper resolution of abstract views of variables. This illegal | |
6061 | -- context is later flagged in the analysis of indicator Part_Of. | |
dc726757 | 6062 | |
95fef24f AC |
6063 | ----------------------------- |
6064 | -- In_Abstract_View_Pragma -- | |
6065 | ----------------------------- | |
dc726757 | 6066 | |
95fef24f | 6067 | function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is |
dc726757 HK |
6068 | Par : Node_Id; |
6069 | ||
6070 | begin | |
6071 | -- Climb the parent chain looking for a pragma | |
6072 | ||
95fef24f | 6073 | Par := Nod; |
dc726757 | 6074 | while Present (Par) loop |
95fef24f | 6075 | if Nkind (Par) = N_Pragma then |
6e759c2a BD |
6076 | if Nam_In (Pragma_Name_Unmapped (Par), |
6077 | Name_Abstract_State, | |
6078 | Name_Depends, | |
6079 | Name_Global, | |
6080 | Name_Initializes, | |
6081 | Name_Refined_Depends, | |
6082 | Name_Refined_Global) | |
95fef24f AC |
6083 | then |
6084 | return True; | |
6085 | ||
6086 | -- Otherwise the pragma is not a legal context for an abstract | |
6087 | -- view. | |
6088 | ||
6089 | else | |
6090 | exit; | |
6091 | end if; | |
dc726757 HK |
6092 | |
6093 | -- Prevent the search from going too far | |
6094 | ||
6095 | elsif Is_Body_Or_Package_Declaration (Par) then | |
95fef24f | 6096 | exit; |
dc726757 HK |
6097 | end if; |
6098 | ||
6099 | Par := Parent (Par); | |
6100 | end loop; | |
6101 | ||
6102 | return False; | |
95fef24f | 6103 | end In_Abstract_View_Pragma; |
dc726757 HK |
6104 | |
6105 | -- Local variables | |
6106 | ||
692918a9 | 6107 | Selector : constant Node_Id := Selector_Name (N); |
daf82dd8 HK |
6108 | |
6109 | Candidate : Entity_Id := Empty; | |
996ae0b0 | 6110 | P_Name : Entity_Id; |
996ae0b0 RK |
6111 | Id : Entity_Id; |
6112 | ||
dc726757 HK |
6113 | -- Start of processing for Find_Expanded_Name |
6114 | ||
996ae0b0 RK |
6115 | begin |
6116 | P_Name := Entity (Prefix (N)); | |
996ae0b0 | 6117 | |
ff81221b ES |
6118 | -- If the prefix is a renamed package, look for the entity in the |
6119 | -- original package. | |
996ae0b0 RK |
6120 | |
6121 | if Ekind (P_Name) = E_Package | |
6122 | and then Present (Renamed_Object (P_Name)) | |
6123 | then | |
6124 | P_Name := Renamed_Object (P_Name); | |
6125 | ||
6126 | -- Rewrite node with entity field pointing to renamed object | |
6127 | ||
6128 | Rewrite (Prefix (N), New_Copy (Prefix (N))); | |
6129 | Set_Entity (Prefix (N), P_Name); | |
6130 | ||
6131 | -- If the prefix is an object of a concurrent type, look for | |
6132 | -- the entity in the associated task or protected type. | |
6133 | ||
6134 | elsif Is_Concurrent_Type (Etype (P_Name)) then | |
6135 | P_Name := Etype (P_Name); | |
6136 | end if; | |
6137 | ||
6138 | Id := Current_Entity (Selector); | |
6139 | ||
923fa078 RD |
6140 | declare |
6141 | Is_New_Candidate : Boolean; | |
996ae0b0 | 6142 | |
923fa078 RD |
6143 | begin |
6144 | while Present (Id) loop | |
6145 | if Scope (Id) = P_Name then | |
6146 | Candidate := Id; | |
6147 | Is_New_Candidate := True; | |
6148 | ||
dc726757 | 6149 | -- Handle abstract views of states and variables. These are |
95fef24f AC |
6150 | -- acceptable candidates only when the reference to the view |
6151 | -- appears in certain pragmas. | |
dc726757 HK |
6152 | |
6153 | if Ekind (Id) = E_Abstract_State | |
6154 | and then From_Limited_With (Id) | |
6155 | and then Present (Non_Limited_View (Id)) | |
6156 | then | |
95fef24f | 6157 | if In_Abstract_View_Pragma (N) then |
dc726757 HK |
6158 | Candidate := Non_Limited_View (Id); |
6159 | Is_New_Candidate := True; | |
6160 | ||
95fef24f AC |
6161 | -- Hide the candidate because it is not used in a proper |
6162 | -- context. | |
dc726757 HK |
6163 | |
6164 | else | |
6165 | Candidate := Empty; | |
6166 | Is_New_Candidate := False; | |
6167 | end if; | |
6168 | end if; | |
6169 | ||
47346923 AC |
6170 | -- Ada 2005 (AI-217): Handle shadow entities associated with |
6171 | -- types declared in limited-withed nested packages. We don't need | |
6172 | -- to handle E_Incomplete_Subtype entities because the entities | |
6173 | -- in the limited view are always E_Incomplete_Type and | |
6174 | -- E_Class_Wide_Type entities (see Build_Limited_Views). | |
6175 | ||
6176 | -- Regarding the expression used to evaluate the scope, it | |
6177 | -- is important to note that the limited view also has shadow | |
6178 | -- entities associated nested packages. For this reason the | |
6179 | -- correct scope of the entity is the scope of the real entity. | |
11560bcc TQ |
6180 | -- The non-limited view may itself be incomplete, in which case |
6181 | -- get the full view if available. | |
923fa078 | 6182 | |
47346923 | 6183 | elsif Ekind_In (Id, E_Incomplete_Type, E_Class_Wide_Type) |
dc726757 | 6184 | and then From_Limited_With (Id) |
923fa078 RD |
6185 | and then Present (Non_Limited_View (Id)) |
6186 | and then Scope (Non_Limited_View (Id)) = P_Name | |
6187 | then | |
11560bcc | 6188 | Candidate := Get_Full_View (Non_Limited_View (Id)); |
923fa078 | 6189 | Is_New_Candidate := True; |
996ae0b0 | 6190 | |
780d73d7 AC |
6191 | -- An unusual case arises with a fully qualified name for an |
6192 | -- entity local to a generic child unit package, within an | |
6193 | -- instantiation of that package. The name of the unit now | |
6194 | -- denotes the renaming created within the instance. This is | |
6195 | -- only relevant in an instance body, see below. | |
6196 | ||
6197 | elsif Is_Generic_Instance (Scope (Id)) | |
6198 | and then In_Open_Scopes (Scope (Id)) | |
6199 | and then In_Instance_Body | |
6200 | and then Ekind (Scope (Id)) = E_Package | |
6201 | and then Ekind (Id) = E_Package | |
6202 | and then Renamed_Entity (Id) = Scope (Id) | |
0cb81445 | 6203 | and then Is_Immediately_Visible (P_Name) |
780d73d7 AC |
6204 | then |
6205 | Is_New_Candidate := True; | |
6206 | ||
996ae0b0 | 6207 | else |
923fa078 | 6208 | Is_New_Candidate := False; |
996ae0b0 | 6209 | end if; |
996ae0b0 | 6210 | |
923fa078 | 6211 | if Is_New_Candidate then |
ddbc55d8 AC |
6212 | |
6213 | -- If entity is a child unit, either it is a visible child of | |
6214 | -- the prefix, or we are in the body of a generic prefix, as | |
6215 | -- will happen when a child unit is instantiated in the body | |
6216 | -- of a generic parent. This is because the instance body does | |
6217 | -- not restore the full compilation context, given that all | |
6218 | -- non-local references have been captured. | |
6219 | ||
8ca1ee5d | 6220 | if Is_Child_Unit (Id) or else P_Name = Standard_Standard then |
ddbc55d8 AC |
6221 | exit when Is_Visible_Lib_Unit (Id) |
6222 | or else (Is_Child_Unit (Id) | |
6223 | and then In_Open_Scopes (Scope (Id)) | |
6224 | and then In_Instance_Body); | |
923fa078 | 6225 | else |
8398e82e | 6226 | exit when not Is_Hidden (Id); |
923fa078 | 6227 | end if; |
8398e82e AC |
6228 | |
6229 | exit when Is_Immediately_Visible (Id); | |
923fa078 RD |
6230 | end if; |
6231 | ||
6232 | Id := Homonym (Id); | |
6233 | end loop; | |
6234 | end; | |
996ae0b0 RK |
6235 | |
6236 | if No (Id) | |
ac7d724d | 6237 | and then Ekind_In (P_Name, E_Procedure, E_Function) |
996ae0b0 RK |
6238 | and then Is_Generic_Instance (P_Name) |
6239 | then | |
6240 | -- Expanded name denotes entity in (instance of) generic subprogram. | |
6241 | -- The entity may be in the subprogram instance, or may denote one of | |
6242 | -- the formals, which is declared in the enclosing wrapper package. | |
6243 | ||
6244 | P_Name := Scope (P_Name); | |
996ae0b0 | 6245 | |
bc41faa2 | 6246 | Id := Current_Entity (Selector); |
996ae0b0 | 6247 | while Present (Id) loop |
bc41faa2 | 6248 | exit when Scope (Id) = P_Name; |
996ae0b0 RK |
6249 | Id := Homonym (Id); |
6250 | end loop; | |
6251 | end if; | |
6252 | ||
bc41faa2 | 6253 | if No (Id) or else Chars (Id) /= Chars (Selector) then |
996ae0b0 RK |
6254 | Set_Etype (N, Any_Type); |
6255 | ||
ff81221b ES |
6256 | -- If we are looking for an entity defined in System, try to find it |
6257 | -- in the child package that may have been provided as an extension | |
6258 | -- to System. The Extend_System pragma will have supplied the name of | |
6259 | -- the extension, which may have to be loaded. | |
996ae0b0 RK |
6260 | |
6261 | if Chars (P_Name) = Name_System | |
6262 | and then Scope (P_Name) = Standard_Standard | |
fbf5a39b | 6263 | and then Present (System_Extend_Unit) |
996ae0b0 RK |
6264 | and then Present_System_Aux (N) |
6265 | then | |
6266 | Set_Entity (Prefix (N), System_Aux_Id); | |
6267 | Find_Expanded_Name (N); | |
6268 | return; | |
6269 | ||
95fef24f AC |
6270 | -- There is an implicit instance of the predefined operator in |
6271 | -- the given scope. The operator entity is defined in Standard. | |
6272 | -- Has_Implicit_Operator makes the node into an Expanded_Name. | |
6273 | ||
fbf5a39b AC |
6274 | elsif Nkind (Selector) = N_Operator_Symbol |
6275 | and then Has_Implicit_Operator (N) | |
996ae0b0 | 6276 | then |
996ae0b0 RK |
6277 | return; |
6278 | ||
95fef24f AC |
6279 | -- If there is no literal defined in the scope denoted by the |
6280 | -- prefix, the literal may belong to (a type derived from) | |
6281 | -- Standard_Character, for which we have no explicit literals. | |
6282 | ||
996ae0b0 RK |
6283 | elsif Nkind (Selector) = N_Character_Literal |
6284 | and then Has_Implicit_Character_Literal (N) | |
6285 | then | |
996ae0b0 RK |
6286 | return; |
6287 | ||
6288 | else | |
ff81221b ES |
6289 | -- If the prefix is a single concurrent object, use its name in |
6290 | -- the error message, rather than that of the anonymous type. | |
996ae0b0 RK |
6291 | |
6292 | if Is_Concurrent_Type (P_Name) | |
6293 | and then Is_Internal_Name (Chars (P_Name)) | |
6294 | then | |
6295 | Error_Msg_Node_2 := Entity (Prefix (N)); | |
6296 | else | |
6297 | Error_Msg_Node_2 := P_Name; | |
6298 | end if; | |
6299 | ||
6300 | if P_Name = System_Aux_Id then | |
6301 | P_Name := Scope (P_Name); | |
6302 | Set_Entity (Prefix (N), P_Name); | |
6303 | end if; | |
6304 | ||
6305 | if Present (Candidate) then | |
6306 | ||
16ca248a ES |
6307 | -- If we know that the unit is a child unit we can give a more |
6308 | -- accurate error message. | |
6309 | ||
996ae0b0 | 6310 | if Is_Child_Unit (Candidate) then |
edd63e9b | 6311 | |
16ca248a ES |
6312 | -- If the candidate is a private child unit and we are in |
6313 | -- the visible part of a public unit, specialize the error | |
6314 | -- message. There might be a private with_clause for it, | |
6315 | -- but it is not currently active. | |
edd63e9b ES |
6316 | |
6317 | if Is_Private_Descendant (Candidate) | |
6318 | and then Ekind (Current_Scope) = E_Package | |
6319 | and then not In_Private_Part (Current_Scope) | |
6320 | and then not Is_Private_Descendant (Current_Scope) | |
6321 | then | |
95fef24f AC |
6322 | Error_Msg_N |
6323 | ("private child unit& is not visible here", Selector); | |
16ca248a ES |
6324 | |
6325 | -- Normal case where we have a missing with for a child unit | |
6326 | ||
edd63e9b | 6327 | else |
16ca248a | 6328 | Error_Msg_Qual_Level := 99; |
ed2233dc AC |
6329 | Error_Msg_NE -- CODEFIX |
6330 | ("missing `WITH &;`", Selector, Candidate); | |
16ca248a | 6331 | Error_Msg_Qual_Level := 0; |
edd63e9b | 6332 | end if; |
16ca248a ES |
6333 | |
6334 | -- Here we don't know that this is a child unit | |
6335 | ||
996ae0b0 RK |
6336 | else |
6337 | Error_Msg_NE ("& is not a visible entity of&", N, Selector); | |
6338 | end if; | |
6339 | ||
6340 | else | |
6341 | -- Within the instantiation of a child unit, the prefix may | |
16ca248a | 6342 | -- denote the parent instance, but the selector has the name |
58747e48 AC |
6343 | -- of the original child. That is to say, when A.B appears |
6344 | -- within an instantiation of generic child unit B, the scope | |
6345 | -- stack includes an instance of A (P_Name) and an instance | |
6346 | -- of B under some other name. We scan the scope to find this | |
6347 | -- child instance, which is the desired entity. | |
6348 | -- Note that the parent may itself be a child instance, if | |
6349 | -- the reference is of the form A.B.C, in which case A.B has | |
6350 | -- already been rewritten with the proper entity. | |
6351 | ||
6352 | if In_Open_Scopes (P_Name) | |
996ae0b0 RK |
6353 | and then Is_Generic_Instance (P_Name) |
6354 | then | |
6355 | declare | |
58747e48 AC |
6356 | Gen_Par : constant Entity_Id := |
6357 | Generic_Parent (Specification | |
6358 | (Unit_Declaration_Node (P_Name))); | |
996ae0b0 RK |
6359 | S : Entity_Id := Current_Scope; |
6360 | P : Entity_Id; | |
6361 | ||
6362 | begin | |
6363 | for J in reverse 0 .. Scope_Stack.Last loop | |
6364 | S := Scope_Stack.Table (J).Entity; | |
6365 | ||
6366 | exit when S = Standard_Standard; | |
6367 | ||
bce79204 AC |
6368 | if Ekind_In (S, E_Function, |
6369 | E_Package, | |
6370 | E_Procedure) | |
996ae0b0 | 6371 | then |
95fef24f AC |
6372 | P := |
6373 | Generic_Parent (Specification | |
6374 | (Unit_Declaration_Node (S))); | |
996ae0b0 | 6375 | |
58747e48 AC |
6376 | -- Check that P is a generic child of the generic |
6377 | -- parent of the prefix. | |
6378 | ||
996ae0b0 | 6379 | if Present (P) |
996ae0b0 | 6380 | and then Chars (P) = Chars (Selector) |
58747e48 | 6381 | and then Scope (P) = Gen_Par |
996ae0b0 RK |
6382 | then |
6383 | Id := S; | |
a77842bd | 6384 | goto Found; |
996ae0b0 RK |
6385 | end if; |
6386 | end if; | |
6387 | ||
6388 | end loop; | |
6389 | end; | |
6390 | end if; | |
6391 | ||
16ca248a ES |
6392 | -- If this is a selection from Ada, System or Interfaces, then |
6393 | -- we assume a missing with for the corresponding package. | |
6394 | ||
aa76fac7 JS |
6395 | if Is_Known_Unit (N) |
6396 | and then not (Present (Entity (Prefix (N))) | |
6397 | and then Scope (Entity (Prefix (N))) /= | |
6398 | Standard_Standard) | |
6399 | then | |
fbe627af RD |
6400 | if not Error_Posted (N) then |
6401 | Error_Msg_Node_2 := Selector; | |
ed2233dc AC |
6402 | Error_Msg_N -- CODEFIX |
6403 | ("missing `WITH &.&;`", Prefix (N)); | |
fbe627af | 6404 | end if; |
996ae0b0 | 6405 | |
16ca248a ES |
6406 | -- If this is a selection from a dummy package, then suppress |
6407 | -- the error message, of course the entity is missing if the | |
a90bd866 | 6408 | -- package is missing. |
996ae0b0 RK |
6409 | |
6410 | elsif Sloc (Error_Msg_Node_2) = No_Location then | |
6411 | null; | |
6412 | ||
6413 | -- Here we have the case of an undefined component | |
6414 | ||
6415 | else | |
d6a24cdb AC |
6416 | -- The prefix may hide a homonym in the context that |
6417 | -- declares the desired entity. This error can use a | |
6418 | -- specialized message. | |
6419 | ||
8398e82e | 6420 | if In_Open_Scopes (P_Name) then |
d6a24cdb AC |
6421 | declare |
6422 | H : constant Entity_Id := Homonym (P_Name); | |
0d354370 | 6423 | |
d6a24cdb | 6424 | begin |
8398e82e AC |
6425 | if Present (H) |
6426 | and then Is_Compilation_Unit (H) | |
6427 | and then | |
6428 | (Is_Immediately_Visible (H) | |
6429 | or else Is_Visible_Lib_Unit (H)) | |
6430 | then | |
6431 | Id := First_Entity (H); | |
6432 | while Present (Id) loop | |
6433 | if Chars (Id) = Chars (Selector) then | |
6434 | Error_Msg_Qual_Level := 99; | |
6435 | Error_Msg_Name_1 := Chars (Selector); | |
6436 | Error_Msg_NE | |
6437 | ("% not declared in&", N, P_Name); | |
6438 | Error_Msg_NE | |
6439 | ("\use fully qualified name starting with " | |
6440 | & "Standard to make& visible", N, H); | |
6441 | Error_Msg_Qual_Level := 0; | |
6442 | goto Done; | |
6443 | end if; | |
d6a24cdb | 6444 | |
8398e82e AC |
6445 | Next_Entity (Id); |
6446 | end loop; | |
6447 | end if; | |
689cb4ac | 6448 | |
690943fc | 6449 | -- If not found, standard error message |
689cb4ac AC |
6450 | |
6451 | Error_Msg_NE ("& not declared in&", N, Selector); | |
6452 | ||
6453 | <<Done>> null; | |
d6a24cdb AC |
6454 | end; |
6455 | ||
6456 | else | |
780d73d7 AC |
6457 | -- Might be worth specializing the case when the prefix |
6458 | -- is a limited view. | |
6459 | -- ... not declared in limited view of... | |
6460 | ||
d6a24cdb AC |
6461 | Error_Msg_NE ("& not declared in&", N, Selector); |
6462 | end if; | |
996ae0b0 | 6463 | |
bc41faa2 | 6464 | -- Check for misspelling of some entity in prefix |
996ae0b0 RK |
6465 | |
6466 | Id := First_Entity (P_Name); | |
294ccb21 RD |
6467 | while Present (Id) loop |
6468 | if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector)) | |
6469 | and then not Is_Internal_Name (Chars (Id)) | |
6470 | then | |
deef4289 | 6471 | Error_Msg_NE -- CODEFIX |
294ccb21 RD |
6472 | ("possible misspelling of&", Selector, Id); |
6473 | exit; | |
6474 | end if; | |
996ae0b0 | 6475 | |
294ccb21 RD |
6476 | Next_Entity (Id); |
6477 | end loop; | |
996ae0b0 RK |
6478 | |
6479 | -- Specialize the message if this may be an instantiation | |
6480 | -- of a child unit that was not mentioned in the context. | |
6481 | ||
6482 | if Nkind (Parent (N)) = N_Package_Instantiation | |
6483 | and then Is_Generic_Instance (Entity (Prefix (N))) | |
6484 | and then Is_Compilation_Unit | |
294ccb21 | 6485 | (Generic_Parent (Parent (Entity (Prefix (N))))) |
996ae0b0 | 6486 | then |
16ca248a | 6487 | Error_Msg_Node_2 := Selector; |
ed2233dc AC |
6488 | Error_Msg_N -- CODEFIX |
6489 | ("\missing `WITH &.&;`", Prefix (N)); | |
996ae0b0 RK |
6490 | end if; |
6491 | end if; | |
6492 | end if; | |
6493 | ||
6494 | Id := Any_Id; | |
6495 | end if; | |
6496 | end if; | |
6497 | ||
a77842bd | 6498 | <<Found>> |
996ae0b0 RK |
6499 | if Comes_From_Source (N) |
6500 | and then Is_Remote_Access_To_Subprogram_Type (Id) | |
d1d81616 | 6501 | and then Ekind (Id) = E_Access_Subprogram_Type |
a77842bd | 6502 | and then Present (Equivalent_Type (Id)) |
996ae0b0 | 6503 | then |
16ca248a ES |
6504 | -- If we are not actually generating distribution code (i.e. the |
6505 | -- current PCS is the dummy non-distributed version), then the | |
6506 | -- Equivalent_Type will be missing, and Id should be treated as | |
6507 | -- a regular access-to-subprogram type. | |
a77842bd | 6508 | |
996ae0b0 RK |
6509 | Id := Equivalent_Type (Id); |
6510 | Set_Chars (Selector, Chars (Id)); | |
6511 | end if; | |
6512 | ||
0ab80019 | 6513 | -- Ada 2005 (AI-50217): Check usage of entities in limited withed units |
19f0526a | 6514 | |
7b56a91b AC |
6515 | if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then |
6516 | if From_Limited_With (Id) | |
0fb2ea01 AC |
6517 | or else Is_Type (Id) |
6518 | or else Ekind (Id) = E_Package | |
996ae0b0 RK |
6519 | then |
6520 | null; | |
6521 | else | |
6522 | Error_Msg_N | |
497a660d AC |
6523 | ("limited withed package can only be used to access incomplete " |
6524 | & "types", N); | |
996ae0b0 RK |
6525 | end if; |
6526 | end if; | |
6527 | ||
6528 | if Is_Task_Type (P_Name) | |
6529 | and then ((Ekind (Id) = E_Entry | |
ac7d724d | 6530 | and then Nkind (Parent (N)) /= N_Attribute_Reference) |
294ccb21 | 6531 | or else |
ac7d724d ES |
6532 | (Ekind (Id) = E_Entry_Family |
6533 | and then | |
6534 | Nkind (Parent (Parent (N))) /= N_Attribute_Reference)) | |
996ae0b0 | 6535 | then |
a53c5613 AC |
6536 | -- If both the task type and the entry are in scope, this may still |
6537 | -- be the expanded name of an entry formal. | |
996ae0b0 | 6538 | |
a53c5613 AC |
6539 | if In_Open_Scopes (Id) |
6540 | and then Nkind (Parent (N)) = N_Selected_Component | |
6541 | then | |
6542 | null; | |
6543 | ||
6544 | else | |
6545 | -- It is an entry call after all, either to the current task | |
6546 | -- (which will deadlock) or to an enclosing task. | |
6547 | ||
6548 | Analyze_Selected_Component (N); | |
6549 | return; | |
6550 | end if; | |
996ae0b0 RK |
6551 | end if; |
6552 | ||
6553 | Change_Selected_Component_To_Expanded_Name (N); | |
07fc65c4 | 6554 | |
90e491a7 PMR |
6555 | -- Preserve relevant elaboration-related attributes of the context which |
6556 | -- are no longer available or very expensive to recompute once analysis, | |
6557 | -- resolution, and expansion are over. | |
6558 | ||
6559 | Mark_Elaboration_Attributes | |
69e6ee2f HK |
6560 | (N_Id => N, |
6561 | Checks => True, | |
6562 | Modes => True, | |
6563 | Warnings => True); | |
90e491a7 | 6564 | |
a54ffd6c AC |
6565 | -- Set appropriate type |
6566 | ||
6567 | if Is_Type (Id) then | |
6568 | Set_Etype (N, Id); | |
6569 | else | |
6570 | Set_Etype (N, Get_Full_View (Etype (Id))); | |
6571 | end if; | |
6572 | ||
07fc65c4 | 6573 | -- Do style check and generate reference, but skip both steps if this |
16ca248a ES |
6574 | -- entity has homonyms, since we may not have the right homonym set yet. |
6575 | -- The proper homonym will be set during the resolve phase. | |
07fc65c4 GB |
6576 | |
6577 | if Has_Homonym (Id) then | |
6578 | Set_Entity (N, Id); | |
a54ffd6c | 6579 | |
07fc65c4 | 6580 | else |
4c484f40 | 6581 | Set_Entity_Or_Discriminal (N, Id); |
84df40f7 | 6582 | |
a54ffd6c AC |
6583 | case Is_LHS (N) is |
6584 | when Yes => | |
6585 | Generate_Reference (Id, N, 'm'); | |
d8f43ee6 | 6586 | |
a54ffd6c AC |
6587 | when No => |
6588 | Generate_Reference (Id, N, 'r'); | |
d8f43ee6 | 6589 | |
a54ffd6c AC |
6590 | when Unknown => |
6591 | Deferred_References.Append ((Id, N)); | |
6592 | end case; | |
996ae0b0 RK |
6593 | end if; |
6594 | ||
30196a76 RD |
6595 | -- Check for violation of No_Wide_Characters |
6596 | ||
6597 | Check_Wide_Character_Restriction (Id, N); | |
6598 | ||
16ca248a ES |
6599 | -- If the Ekind of the entity is Void, it means that all homonyms are |
6600 | -- hidden from all visibility (RM 8.3(5,14-20)). | |
996ae0b0 RK |
6601 | |
6602 | if Ekind (Id) = E_Void then | |
6603 | Premature_Usage (N); | |
6604 | ||
ac7d724d | 6605 | elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then |
996ae0b0 RK |
6606 | declare |
6607 | H : Entity_Id := Homonym (Id); | |
6608 | ||
6609 | begin | |
6610 | while Present (H) loop | |
35ae2ed8 | 6611 | if Scope (H) = Scope (Id) |
ac7d724d ES |
6612 | and then (not Is_Hidden (H) |
6613 | or else Is_Immediately_Visible (H)) | |
35ae2ed8 | 6614 | then |
996ae0b0 RK |
6615 | Collect_Interps (N); |
6616 | exit; | |
6617 | end if; | |
6618 | ||
6619 | H := Homonym (H); | |
6620 | end loop; | |
fbf5a39b | 6621 | |
16ca248a ES |
6622 | -- If an extension of System is present, collect possible explicit |
6623 | -- overloadings declared in the extension. | |
fbf5a39b AC |
6624 | |
6625 | if Chars (P_Name) = Name_System | |
6626 | and then Scope (P_Name) = Standard_Standard | |
6627 | and then Present (System_Extend_Unit) | |
6628 | and then Present_System_Aux (N) | |
6629 | then | |
6630 | H := Current_Entity (Id); | |
6631 | ||
6632 | while Present (H) loop | |
6633 | if Scope (H) = System_Aux_Id then | |
6634 | Add_One_Interp (N, H, Etype (H)); | |
6635 | end if; | |
6636 | ||
6637 | H := Homonym (H); | |
6638 | end loop; | |
6639 | end if; | |
996ae0b0 RK |
6640 | end; |
6641 | end if; | |
6642 | ||
6643 | if Nkind (Selector_Name (N)) = N_Operator_Symbol | |
6644 | and then Scope (Id) /= Standard_Standard | |
6645 | then | |
16ca248a ES |
6646 | -- In addition to user-defined operators in the given scope, there |
6647 | -- may be an implicit instance of the predefined operator. The | |
6648 | -- operator (defined in Standard) is found in Has_Implicit_Operator, | |
6649 | -- and added to the interpretations. Procedure Add_One_Interp will | |
6650 | -- determine which hides which. | |
996ae0b0 RK |
6651 | |
6652 | if Has_Implicit_Operator (N) then | |
6653 | null; | |
6654 | end if; | |
6655 | end if; | |
be4c5193 AC |
6656 | |
6657 | -- If there is a single interpretation for N we can generate a | |
6658 | -- reference to the unique entity found. | |
6659 | ||
6660 | if Is_Overloadable (Id) and then not Is_Overloaded (N) then | |
6661 | Generate_Reference (Id, N); | |
6662 | end if; | |
fb757f7d | 6663 | |
851e9f19 PMR |
6664 | -- Mark relevant use-type and use-package clauses as effective if the |
6665 | -- node in question is not overloaded and therefore does not require | |
6666 | -- resolution. | |
6667 | ||
6668 | if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then | |
6669 | Mark_Use_Clauses (N); | |
6670 | end if; | |
6671 | ||
fb757f7d | 6672 | Check_Restriction_No_Use_Of_Entity (N); |
90e491a7 | 6673 | |
daf82dd8 HK |
6674 | -- Annotate the tree by creating a variable reference marker in case the |
6675 | -- original variable reference is folded or optimized away. The variable | |
6676 | -- reference marker is automatically saved for later examination by the | |
6677 | -- ABE Processing phase. Variable references which act as actuals in a | |
6678 | -- call require special processing and are left to Resolve_Actuals. The | |
6679 | -- reference is a write when it appears on the left hand side of an | |
6680 | -- assignment. | |
6681 | ||
692918a9 HK |
6682 | if Needs_Variable_Reference_Marker |
6683 | (N => N, | |
6684 | Calls_OK => False) | |
6685 | then | |
6686 | declare | |
6687 | Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes; | |
6688 | ||
6689 | begin | |
6690 | Build_Variable_Reference_Marker | |
6691 | (N => N, | |
6692 | Read => not Is_Assignment_LHS, | |
6693 | Write => Is_Assignment_LHS); | |
6694 | end; | |
daf82dd8 | 6695 | end if; |
996ae0b0 RK |
6696 | end Find_Expanded_Name; |
6697 | ||
851e9f19 PMR |
6698 | -------------------- |
6699 | -- Find_Most_Prev -- | |
6700 | -------------------- | |
6701 | ||
6702 | function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id is | |
6703 | Curr : Node_Id; | |
7f5e671b | 6704 | |
851e9f19 PMR |
6705 | begin |
6706 | -- Loop through the Prev_Use_Clause chain | |
6707 | ||
6708 | Curr := Use_Clause; | |
6709 | while Present (Prev_Use_Clause (Curr)) loop | |
6710 | Curr := Prev_Use_Clause (Curr); | |
6711 | end loop; | |
6712 | ||
6713 | return Curr; | |
6714 | end Find_Most_Prev; | |
6715 | ||
996ae0b0 RK |
6716 | ------------------------- |
6717 | -- Find_Renamed_Entity -- | |
6718 | ------------------------- | |
6719 | ||
6720 | function Find_Renamed_Entity | |
6721 | (N : Node_Id; | |
6722 | Nam : Node_Id; | |
6723 | New_S : Entity_Id; | |
6724 | Is_Actual : Boolean := False) return Entity_Id | |
6725 | is | |
fbf5a39b | 6726 | Ind : Interp_Index; |
996ae0b0 RK |
6727 | I1 : Interp_Index := 0; -- Suppress junk warnings |
6728 | It : Interp; | |
6729 | It1 : Interp; | |
6730 | Old_S : Entity_Id; | |
6731 | Inst : Entity_Id; | |
6732 | ||
258325dd EB |
6733 | function Find_Nearer_Entity |
6734 | (New_S : Entity_Id; | |
6735 | Old1_S : Entity_Id; | |
6736 | Old2_S : Entity_Id) return Entity_Id; | |
6737 | -- Determine whether one of Old_S1 and Old_S2 is nearer to New_S than | |
6738 | -- the other, and return it if so. Return Empty otherwise. We use this | |
6739 | -- in conjunction with Inherit_Renamed_Profile to simplify later type | |
6740 | -- disambiguation for actual subprograms in instances. | |
6741 | ||
996ae0b0 RK |
6742 | function Is_Visible_Operation (Op : Entity_Id) return Boolean; |
6743 | -- If the renamed entity is an implicit operator, check whether it is | |
16ca248a ES |
6744 | -- visible because its operand type is properly visible. This check |
6745 | -- applies to explicit renamed entities that appear in the source in a | |
6746 | -- renaming declaration or a formal subprogram instance, but not to | |
6747 | -- default generic actuals with a name. | |
996ae0b0 | 6748 | |
923fa078 RD |
6749 | function Report_Overload return Entity_Id; |
6750 | -- List possible interpretations, and specialize message in the | |
6751 | -- case of a generic actual. | |
6752 | ||
6753 | function Within (Inner, Outer : Entity_Id) return Boolean; | |
16ca248a ES |
6754 | -- Determine whether a candidate subprogram is defined within the |
6755 | -- enclosing instance. If yes, it has precedence over outer candidates. | |
923fa078 | 6756 | |
258325dd EB |
6757 | -------------------------- |
6758 | -- Find_Nearer_Entity -- | |
6759 | -------------------------- | |
6760 | ||
6761 | function Find_Nearer_Entity | |
6762 | (New_S : Entity_Id; | |
6763 | Old1_S : Entity_Id; | |
6764 | Old2_S : Entity_Id) return Entity_Id | |
6765 | is | |
6766 | New_F : Entity_Id; | |
6767 | Old1_F : Entity_Id; | |
6768 | Old2_F : Entity_Id; | |
6769 | Anc_T : Entity_Id; | |
6770 | ||
6771 | begin | |
6772 | New_F := First_Formal (New_S); | |
6773 | Old1_F := First_Formal (Old1_S); | |
6774 | Old2_F := First_Formal (Old2_S); | |
6775 | ||
6776 | -- The criterion is whether the type of the formals of one of Old1_S | |
6777 | -- and Old2_S is an ancestor subtype of the type of the corresponding | |
6778 | -- formals of New_S while the other is not (we already know that they | |
6779 | -- are all subtypes of the same base type). | |
6780 | ||
6781 | -- This makes it possible to find the more correct renamed entity in | |
6782 | -- the case of a generic instantiation nested in an enclosing one for | |
6783 | -- which different formal types get the same actual type, which will | |
6784 | -- in turn make it possible for Inherit_Renamed_Profile to preserve | |
6785 | -- types on formal parameters and ultimately simplify disambiguation. | |
6786 | ||
6787 | -- Consider the follow package G: | |
6788 | ||
6789 | -- generic | |
6790 | -- type Item_T is private; | |
6791 | -- with function Compare (L, R: Item_T) return Boolean is <>; | |
6792 | ||
6793 | -- type Bound_T is private; | |
6794 | -- with function Compare (L, R : Bound_T) return Boolean is <>; | |
6795 | -- package G is | |
6796 | -- ... | |
6797 | -- end G; | |
6798 | ||
6799 | -- package body G is | |
6800 | -- package My_Inner is Inner_G (Bound_T); | |
6801 | -- ... | |
6802 | -- end G; | |
6803 | ||
6804 | -- with the following package Inner_G: | |
6805 | ||
6806 | -- generic | |
6807 | -- type T is private; | |
6808 | -- with function Compare (L, R: T) return Boolean is <>; | |
6809 | -- package Inner_G is | |
6810 | -- function "<" (L, R: T) return Boolean is (Compare (L, R)); | |
6811 | -- end Inner_G; | |
6812 | ||
6813 | -- If G is instantiated on the same actual type with a single Compare | |
6814 | -- function: | |
6815 | ||
6816 | -- type T is ... | |
6817 | -- function Compare (L, R : T) return Boolean; | |
6818 | -- package My_G is new (T, T); | |
6819 | ||
6820 | -- then the renaming generated for Compare in the inner instantiation | |
6821 | -- is ambiguous: it can rename either of the renamings generated for | |
6822 | -- the outer instantiation. Now if the first one is picked up, then | |
6823 | -- the subtypes of the formal parameters of the renaming will not be | |
6824 | -- preserved in Inherit_Renamed_Profile because they are subtypes of | |
6825 | -- the Bound_T formal type and not of the Item_T formal type, so we | |
6826 | -- need to arrange for the second one to be picked up instead. | |
6827 | ||
6828 | while Present (New_F) loop | |
6829 | if Etype (Old1_F) /= Etype (Old2_F) then | |
6830 | Anc_T := Ancestor_Subtype (Etype (New_F)); | |
6831 | ||
6832 | if Etype (Old1_F) = Anc_T then | |
6833 | return Old1_S; | |
6834 | elsif Etype (Old2_F) = Anc_T then | |
6835 | return Old2_S; | |
6836 | end if; | |
6837 | end if; | |
6838 | ||
6839 | Next_Formal (New_F); | |
6840 | Next_Formal (Old1_F); | |
6841 | Next_Formal (Old2_F); | |
6842 | end loop; | |
6843 | ||
6844 | pragma Assert (No (Old1_F)); | |
6845 | pragma Assert (No (Old2_F)); | |
6846 | ||
6847 | return Empty; | |
6848 | end Find_Nearer_Entity; | |
6849 | ||
996ae0b0 RK |
6850 | -------------------------- |
6851 | -- Is_Visible_Operation -- | |
6852 | -------------------------- | |
6853 | ||
6854 | function Is_Visible_Operation (Op : Entity_Id) return Boolean is | |
6855 | Scop : Entity_Id; | |
6856 | Typ : Entity_Id; | |
6857 | Btyp : Entity_Id; | |
6858 | ||
6859 | begin | |
6860 | if Ekind (Op) /= E_Operator | |
6861 | or else Scope (Op) /= Standard_Standard | |
6862 | or else (In_Instance | |
ac7d724d ES |
6863 | and then (not Is_Actual |
6864 | or else Present (Enclosing_Instance))) | |
996ae0b0 RK |
6865 | then |
6866 | return True; | |
6867 | ||
6868 | else | |
6869 | -- For a fixed point type operator, check the resulting type, | |
6870 | -- because it may be a mixed mode integer * fixed operation. | |
6871 | ||
6872 | if Present (Next_Formal (First_Formal (New_S))) | |
6873 | and then Is_Fixed_Point_Type (Etype (New_S)) | |
6874 | then | |
6875 | Typ := Etype (New_S); | |
6876 | else | |
6877 | Typ := Etype (First_Formal (New_S)); | |
6878 | end if; | |
6879 | ||
6880 | Btyp := Base_Type (Typ); | |
6881 | ||
6882 | if Nkind (Nam) /= N_Expanded_Name then | |
6883 | return (In_Open_Scopes (Scope (Btyp)) | |
6884 | or else Is_Potentially_Use_Visible (Btyp) | |
6885 | or else In_Use (Btyp) | |
6886 | or else In_Use (Scope (Btyp))); | |
6887 | ||
6888 | else | |
6889 | Scop := Entity (Prefix (Nam)); | |
6890 | ||
6891 | if Ekind (Scop) = E_Package | |
6892 | and then Present (Renamed_Object (Scop)) | |
6893 | then | |
6894 | Scop := Renamed_Object (Scop); | |
6895 | end if; | |
6896 | ||
6897 | -- Operator is visible if prefix of expanded name denotes | |
16b05213 | 6898 | -- scope of type, or else type is defined in System_Aux |
996ae0b0 RK |
6899 | -- and the prefix denotes System. |
6900 | ||
6901 | return Scope (Btyp) = Scop | |
6902 | or else (Scope (Btyp) = System_Aux_Id | |
6903 | and then Scope (Scope (Btyp)) = Scop); | |
6904 | end if; | |
6905 | end if; | |
6906 | end Is_Visible_Operation; | |
6907 | ||
6908 | ------------ | |
6909 | -- Within -- | |
6910 | ------------ | |
6911 | ||
6912 | function Within (Inner, Outer : Entity_Id) return Boolean is | |
16ca248a | 6913 | Sc : Entity_Id; |
996ae0b0 RK |
6914 | |
6915 | begin | |
16ca248a | 6916 | Sc := Scope (Inner); |
996ae0b0 | 6917 | while Sc /= Standard_Standard loop |
996ae0b0 RK |
6918 | if Sc = Outer then |
6919 | return True; | |
6920 | else | |
6921 | Sc := Scope (Sc); | |
6922 | end if; | |
6923 | end loop; | |
6924 | ||
6925 | return False; | |
6926 | end Within; | |
6927 | ||
923fa078 RD |
6928 | --------------------- |
6929 | -- Report_Overload -- | |
6930 | --------------------- | |
fbf5a39b AC |
6931 | |
6932 | function Report_Overload return Entity_Id is | |
6933 | begin | |
6934 | if Is_Actual then | |
ed2233dc | 6935 | Error_Msg_NE -- CODEFIX |
fbf5a39b | 6936 | ("ambiguous actual subprogram&, " & |
923fa078 | 6937 | "possible interpretations:", N, Nam); |
fbf5a39b | 6938 | else |
ed2233dc | 6939 | Error_Msg_N -- CODEFIX |
fbf5a39b | 6940 | ("ambiguous subprogram, " & |
923fa078 | 6941 | "possible interpretations:", N); |
fbf5a39b AC |
6942 | end if; |
6943 | ||
6944 | List_Interps (Nam, N); | |
6945 | return Old_S; | |
6946 | end Report_Overload; | |
6947 | ||
0187b60e | 6948 | -- Start of processing for Find_Renamed_Entity |
996ae0b0 RK |
6949 | |
6950 | begin | |
6951 | Old_S := Any_Id; | |
6952 | Candidate_Renaming := Empty; | |
6953 | ||
fc193526 | 6954 | if Is_Overloaded (Nam) then |
fbf5a39b | 6955 | Get_First_Interp (Nam, Ind, It); |
996ae0b0 | 6956 | while Present (It.Nam) loop |
996ae0b0 | 6957 | if Entity_Matches_Spec (It.Nam, New_S) |
fc193526 | 6958 | and then Is_Visible_Operation (It.Nam) |
996ae0b0 RK |
6959 | then |
6960 | if Old_S /= Any_Id then | |
6961 | ||
6962 | -- Note: The call to Disambiguate only happens if a | |
6963 | -- previous interpretation was found, in which case I1 | |
6964 | -- has received a value. | |
6965 | ||
fbf5a39b | 6966 | It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S)); |
996ae0b0 RK |
6967 | |
6968 | if It1 = No_Interp then | |
996ae0b0 RK |
6969 | Inst := Enclosing_Instance; |
6970 | ||
6971 | if Present (Inst) then | |
996ae0b0 | 6972 | if Within (It.Nam, Inst) then |
444acbdd | 6973 | if Within (Old_S, Inst) then |
258325dd EB |
6974 | declare |
6975 | It_D : constant Uint := Scope_Depth (It.Nam); | |
6976 | Old_D : constant Uint := Scope_Depth (Old_S); | |
6977 | N_Ent : Entity_Id; | |
6978 | begin | |
6979 | -- Choose the innermost subprogram, which | |
6980 | -- would hide the outer one in the generic. | |
6981 | ||
6982 | if Old_D > It_D then | |
6983 | return Old_S; | |
6984 | elsif It_D > Old_D then | |
6985 | return It.Nam; | |
6986 | end if; | |
6987 | ||
6988 | -- Otherwise, if we can determine that one | |
6989 | -- of the entities is nearer to the renaming | |
6990 | -- than the other, choose it. If not, then | |
6991 | -- return the newer one as done historically. | |
6992 | ||
6993 | N_Ent := | |
6994 | Find_Nearer_Entity (New_S, Old_S, It.Nam); | |
6995 | if Present (N_Ent) then | |
6996 | return N_Ent; | |
6997 | else | |
6998 | return It.Nam; | |
6999 | end if; | |
7000 | end; | |
444acbdd AC |
7001 | end if; |
7002 | ||
996ae0b0 | 7003 | elsif Within (Old_S, Inst) then |
258325dd | 7004 | return Old_S; |
444acbdd | 7005 | |
996ae0b0 | 7006 | else |
fbf5a39b | 7007 | return Report_Overload; |
996ae0b0 RK |
7008 | end if; |
7009 | ||
3ddd922e | 7010 | -- If not within an instance, ambiguity is real |
444acbdd | 7011 | |
996ae0b0 | 7012 | else |
fbf5a39b | 7013 | return Report_Overload; |
996ae0b0 RK |
7014 | end if; |
7015 | ||
7016 | else | |
7017 | Old_S := It1.Nam; | |
7018 | exit; | |
7019 | end if; | |
7020 | ||
7021 | else | |
fbf5a39b | 7022 | I1 := Ind; |
996ae0b0 RK |
7023 | Old_S := It.Nam; |
7024 | end if; | |
7025 | ||
7026 | elsif | |
7027 | Present (First_Formal (It.Nam)) | |
7028 | and then Present (First_Formal (New_S)) | |
ac7d724d ES |
7029 | and then (Base_Type (Etype (First_Formal (It.Nam))) = |
7030 | Base_Type (Etype (First_Formal (New_S)))) | |
996ae0b0 RK |
7031 | then |
7032 | Candidate_Renaming := It.Nam; | |
7033 | end if; | |
7034 | ||
fbf5a39b | 7035 | Get_Next_Interp (Ind, It); |
996ae0b0 RK |
7036 | end loop; |
7037 | ||
7038 | Set_Entity (Nam, Old_S); | |
1138cf59 AC |
7039 | |
7040 | if Old_S /= Any_Id then | |
7041 | Set_Is_Overloaded (Nam, False); | |
7042 | end if; | |
fc193526 HK |
7043 | |
7044 | -- Non-overloaded case | |
7045 | ||
7046 | else | |
60aa5228 AC |
7047 | if Is_Actual |
7048 | and then Present (Enclosing_Instance) | |
7049 | and then Entity_Matches_Spec (Entity (Nam), New_S) | |
7050 | then | |
fc193526 HK |
7051 | Old_S := Entity (Nam); |
7052 | ||
7053 | elsif Entity_Matches_Spec (Entity (Nam), New_S) then | |
7054 | Candidate_Renaming := New_S; | |
7055 | ||
7056 | if Is_Visible_Operation (Entity (Nam)) then | |
7057 | Old_S := Entity (Nam); | |
7058 | end if; | |
7059 | ||
7060 | elsif Present (First_Formal (Entity (Nam))) | |
7061 | and then Present (First_Formal (New_S)) | |
7062 | and then (Base_Type (Etype (First_Formal (Entity (Nam)))) = | |
7063 | Base_Type (Etype (First_Formal (New_S)))) | |
7064 | then | |
7065 | Candidate_Renaming := Entity (Nam); | |
7066 | end if; | |
996ae0b0 RK |
7067 | end if; |
7068 | ||
7069 | return Old_S; | |
7070 | end Find_Renamed_Entity; | |
7071 | ||
7072 | ----------------------------- | |
7073 | -- Find_Selected_Component -- | |
7074 | ----------------------------- | |
7075 | ||
7076 | procedure Find_Selected_Component (N : Node_Id) is | |
fbf5a39b | 7077 | P : constant Node_Id := Prefix (N); |
996ae0b0 RK |
7078 | |
7079 | P_Name : Entity_Id; | |
7080 | -- Entity denoted by prefix | |
7081 | ||
7082 | P_Type : Entity_Id; | |
7083 | -- and its type | |
7084 | ||
7085 | Nam : Node_Id; | |
7086 | ||
95e00a3a ES |
7087 | function Available_Subtype return Boolean; |
7088 | -- A small optimization: if the prefix is constrained and the component | |
7089 | -- is an array type we may already have a usable subtype for it, so we | |
7090 | -- can use it rather than generating a new one, because the bounds | |
7091 | -- will be the values of the discriminants and not discriminant refs. | |
c9d70ab1 AC |
7092 | -- This simplifies value tracing in GNATProve. For consistency, both |
7093 | -- the entity name and the subtype come from the constrained component. | |
95e00a3a | 7094 | |
c0dd5b38 AC |
7095 | -- This is only used in GNATProve mode: when generating code it may be |
7096 | -- necessary to create an itype in the scope of use of the selected | |
7097 | -- component, e.g. in the context of a expanded record equality. | |
7098 | ||
2e70d415 AC |
7099 | function Is_Reference_In_Subunit return Boolean; |
7100 | -- In a subunit, the scope depth is not a proper measure of hiding, | |
7101 | -- because the context of the proper body may itself hide entities in | |
7102 | -- parent units. This rare case requires inspecting the tree directly | |
7103 | -- because the proper body is inserted in the main unit and its context | |
7104 | -- is simply added to that of the parent. | |
7105 | ||
95e00a3a ES |
7106 | ----------------------- |
7107 | -- Available_Subtype -- | |
7108 | ----------------------- | |
7109 | ||
7110 | function Available_Subtype return Boolean is | |
7111 | Comp : Entity_Id; | |
c9d70ab1 | 7112 | |
95e00a3a | 7113 | begin |
c0dd5b38 AC |
7114 | if GNATprove_Mode then |
7115 | Comp := First_Entity (Etype (P)); | |
7116 | while Present (Comp) loop | |
7117 | if Chars (Comp) = Chars (Selector_Name (N)) then | |
7118 | Set_Etype (N, Etype (Comp)); | |
7119 | Set_Entity (Selector_Name (N), Comp); | |
7120 | Set_Etype (Selector_Name (N), Etype (Comp)); | |
7121 | return True; | |
7122 | end if; | |
95e00a3a | 7123 | |
c0dd5b38 AC |
7124 | Next_Component (Comp); |
7125 | end loop; | |
7126 | end if; | |
95e00a3a ES |
7127 | |
7128 | return False; | |
7129 | end Available_Subtype; | |
7130 | ||
2e70d415 AC |
7131 | ----------------------------- |
7132 | -- Is_Reference_In_Subunit -- | |
7133 | ----------------------------- | |
7134 | ||
7135 | function Is_Reference_In_Subunit return Boolean is | |
7136 | Clause : Node_Id; | |
7137 | Comp_Unit : Node_Id; | |
7138 | ||
7139 | begin | |
7140 | Comp_Unit := N; | |
7141 | while Present (Comp_Unit) | |
f5f6d8d7 | 7142 | and then Nkind (Comp_Unit) /= N_Compilation_Unit |
2e70d415 AC |
7143 | loop |
7144 | Comp_Unit := Parent (Comp_Unit); | |
7145 | end loop; | |
7146 | ||
f5f6d8d7 | 7147 | if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then |
2e70d415 AC |
7148 | return False; |
7149 | end if; | |
7150 | ||
7151 | -- Now check whether the package is in the context of the subunit | |
7152 | ||
7153 | Clause := First (Context_Items (Comp_Unit)); | |
2e70d415 AC |
7154 | while Present (Clause) loop |
7155 | if Nkind (Clause) = N_With_Clause | |
7156 | and then Entity (Name (Clause)) = P_Name | |
7157 | then | |
7158 | return True; | |
7159 | end if; | |
7160 | ||
7161 | Clause := Next (Clause); | |
7162 | end loop; | |
7163 | ||
7164 | return False; | |
7165 | end Is_Reference_In_Subunit; | |
7166 | ||
f5f6d8d7 AC |
7167 | -- Start of processing for Find_Selected_Component |
7168 | ||
996ae0b0 RK |
7169 | begin |
7170 | Analyze (P); | |
7171 | ||
7172 | if Nkind (P) = N_Error then | |
7173 | return; | |
23685ae6 AC |
7174 | end if; |
7175 | ||
7176 | -- Selector name cannot be a character literal or an operator symbol in | |
db72f10a | 7177 | -- SPARK, except for the operator symbol in a renaming. |
23685ae6 | 7178 | |
6480338a | 7179 | if Restriction_Check_Required (SPARK_05) then |
23685ae6 | 7180 | if Nkind (Selector_Name (N)) = N_Character_Literal then |
ce5ba43a | 7181 | Check_SPARK_05_Restriction |
23685ae6 | 7182 | ("character literal cannot be prefixed", N); |
db72f10a AC |
7183 | elsif Nkind (Selector_Name (N)) = N_Operator_Symbol |
7184 | and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration | |
7185 | then | |
ce5ba43a AC |
7186 | Check_SPARK_05_Restriction |
7187 | ("operator symbol cannot be prefixed", N); | |
23685ae6 AC |
7188 | end if; |
7189 | end if; | |
996ae0b0 | 7190 | |
16ca248a ES |
7191 | -- If the selector already has an entity, the node has been constructed |
7192 | -- in the course of expansion, and is known to be valid. Do not verify | |
7193 | -- that it is defined for the type (it may be a private component used | |
7194 | -- in the expansion of record equality). | |
996ae0b0 | 7195 | |
23685ae6 | 7196 | if Present (Entity (Selector_Name (N))) then |
f5f6d8d7 | 7197 | if No (Etype (N)) or else Etype (N) = Any_Type then |
996ae0b0 | 7198 | declare |
fbf5a39b AC |
7199 | Sel_Name : constant Node_Id := Selector_Name (N); |
7200 | Selector : constant Entity_Id := Entity (Sel_Name); | |
996ae0b0 RK |
7201 | C_Etype : Node_Id; |
7202 | ||
7203 | begin | |
7204 | Set_Etype (Sel_Name, Etype (Selector)); | |
7205 | ||
7206 | if not Is_Entity_Name (P) then | |
fbf5a39b | 7207 | Resolve (P); |
996ae0b0 RK |
7208 | end if; |
7209 | ||
7210 | -- Build an actual subtype except for the first parameter | |
fbf5a39b | 7211 | -- of an init proc, where this actual subtype is by |
996ae0b0 RK |
7212 | -- definition incorrect, since the object is uninitialized |
7213 | -- (and does not even have defined discriminants etc.) | |
7214 | ||
7215 | if Is_Entity_Name (P) | |
7216 | and then Ekind (Entity (P)) = E_Function | |
7217 | then | |
7218 | Nam := New_Copy (P); | |
7219 | ||
7220 | if Is_Overloaded (P) then | |
7221 | Save_Interps (P, Nam); | |
7222 | end if; | |
7223 | ||
f5f6d8d7 | 7224 | Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam)); |
996ae0b0 RK |
7225 | Analyze_Call (P); |
7226 | Analyze_Selected_Component (N); | |
7227 | return; | |
7228 | ||
7229 | elsif Ekind (Selector) = E_Component | |
7230 | and then (not Is_Entity_Name (P) | |
7231 | or else Chars (Entity (P)) /= Name_uInit) | |
7232 | then | |
5a271a7f RD |
7233 | -- Check if we already have an available subtype we can use |
7234 | ||
e0c23ac7 | 7235 | if Ekind (Etype (P)) = E_Record_Subtype |
95e00a3a ES |
7236 | and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration |
7237 | and then Is_Array_Type (Etype (Selector)) | |
7238 | and then not Is_Packed (Etype (Selector)) | |
7239 | and then Available_Subtype | |
7240 | then | |
7241 | return; | |
7242 | ||
9fc2854d AC |
7243 | -- Do not build the subtype when referencing components of |
7244 | -- dispatch table wrappers. Required to avoid generating | |
535a8637 | 7245 | -- elaboration code with HI runtimes. |
9fc2854d | 7246 | |
535a8637 | 7247 | elsif RTU_Loaded (Ada_Tags) |
df3e68b1 HK |
7248 | and then |
7249 | ((RTE_Available (RE_Dispatch_Table_Wrapper) | |
2c1b72d7 | 7250 | and then Scope (Selector) = |
df3e68b1 | 7251 | RTE (RE_Dispatch_Table_Wrapper)) |
f5f6d8d7 AC |
7252 | or else |
7253 | (RTE_Available (RE_No_Dispatch_Table_Wrapper) | |
7254 | and then Scope (Selector) = | |
7255 | RTE (RE_No_Dispatch_Table_Wrapper))) | |
9fc2854d AC |
7256 | then |
7257 | C_Etype := Empty; | |
9fc2854d AC |
7258 | else |
7259 | C_Etype := | |
2c1b72d7 AC |
7260 | Build_Actual_Subtype_Of_Component |
7261 | (Etype (Selector), N); | |
9fc2854d AC |
7262 | end if; |
7263 | ||
996ae0b0 RK |
7264 | else |
7265 | C_Etype := Empty; | |
7266 | end if; | |
7267 | ||
7268 | if No (C_Etype) then | |
7269 | C_Etype := Etype (Selector); | |
7270 | else | |
7271 | Insert_Action (N, C_Etype); | |
7272 | C_Etype := Defining_Identifier (C_Etype); | |
7273 | end if; | |
7274 | ||
7275 | Set_Etype (N, C_Etype); | |
7276 | end; | |
7277 | ||
7278 | -- If this is the name of an entry or protected operation, and | |
7279 | -- the prefix is an access type, insert an explicit dereference, | |
7280 | -- so that entry calls are treated uniformly. | |
7281 | ||
7282 | if Is_Access_Type (Etype (P)) | |
7283 | and then Is_Concurrent_Type (Designated_Type (Etype (P))) | |
7284 | then | |
7285 | declare | |
fbf5a39b AC |
7286 | New_P : constant Node_Id := |
7287 | Make_Explicit_Dereference (Sloc (P), | |
7288 | Prefix => Relocate_Node (P)); | |
996ae0b0 RK |
7289 | begin |
7290 | Rewrite (P, New_P); | |
7291 | Set_Etype (P, Designated_Type (Etype (Prefix (P)))); | |
7292 | end; | |
7293 | end if; | |
7294 | ||
7295 | -- If the selected component appears within a default expression | |
812e6118 | 7296 | -- and it has an actual subtype, the preanalysis has not yet |
996ae0b0 | 7297 | -- completed its analysis, because Insert_Actions is disabled in |
fbf5a39b | 7298 | -- that context. Within the init proc of the enclosing type we |
996ae0b0 RK |
7299 | -- must complete this analysis, if an actual subtype was created. |
7300 | ||
7301 | elsif Inside_Init_Proc then | |
7302 | declare | |
7303 | Typ : constant Entity_Id := Etype (N); | |
7304 | Decl : constant Node_Id := Declaration_Node (Typ); | |
996ae0b0 RK |
7305 | begin |
7306 | if Nkind (Decl) = N_Subtype_Declaration | |
7307 | and then not Analyzed (Decl) | |
7308 | and then Is_List_Member (Decl) | |
7309 | and then No (Parent (Decl)) | |
7310 | then | |
7311 | Remove (Decl); | |
7312 | Insert_Action (N, Decl); | |
7313 | end if; | |
7314 | end; | |
7315 | end if; | |
7316 | ||
7317 | return; | |
7318 | ||
7319 | elsif Is_Entity_Name (P) then | |
7320 | P_Name := Entity (P); | |
7321 | ||
7322 | -- The prefix may denote an enclosing type which is the completion | |
7323 | -- of an incomplete type declaration. | |
7324 | ||
7325 | if Is_Type (P_Name) then | |
7326 | Set_Entity (P, Get_Full_View (P_Name)); | |
7327 | Set_Etype (P, Entity (P)); | |
7328 | P_Name := Entity (P); | |
7329 | end if; | |
7330 | ||
7331 | P_Type := Base_Type (Etype (P)); | |
7332 | ||
7333 | if Debug_Flag_E then | |
7334 | Write_Str ("Found prefix type to be "); | |
7335 | Write_Entity_Info (P_Type, " "); Write_Eol; | |
7336 | end if; | |
7337 | ||
38cea80d ES |
7338 | -- The designated type may be a limited view with no components. |
7339 | -- Check whether the non-limited view is available, because in some | |
b943a971 AC |
7340 | -- cases this will not be set when installing the context. Rewrite |
7341 | -- the node by introducing an explicit dereference at once, and | |
7342 | -- setting the type of the rewritten prefix to the non-limited view | |
7343 | -- of the original designated type. | |
38cea80d ES |
7344 | |
7345 | if Is_Access_Type (P_Type) then | |
7346 | declare | |
b943a971 AC |
7347 | Desig_Typ : constant Entity_Id := |
7348 | Directly_Designated_Type (P_Type); | |
7349 | ||
38cea80d | 7350 | begin |
b943a971 AC |
7351 | if Is_Incomplete_Type (Desig_Typ) |
7352 | and then From_Limited_With (Desig_Typ) | |
7353 | and then Present (Non_Limited_View (Desig_Typ)) | |
38cea80d | 7354 | then |
b943a971 AC |
7355 | Rewrite (P, |
7356 | Make_Explicit_Dereference (Sloc (P), | |
7357 | Prefix => Relocate_Node (P))); | |
7358 | ||
7359 | Set_Etype (P, Get_Full_View (Non_Limited_View (Desig_Typ))); | |
7360 | P_Type := Etype (P); | |
38cea80d ES |
7361 | end if; |
7362 | end; | |
7363 | end if; | |
7364 | ||
996ae0b0 RK |
7365 | -- First check for components of a record object (not the |
7366 | -- result of a call, which is handled below). | |
7367 | ||
7368 | if Is_Appropriate_For_Record (P_Type) | |
7369 | and then not Is_Overloadable (P_Name) | |
7370 | and then not Is_Type (P_Name) | |
7371 | then | |
7372 | -- Selected component of record. Type checking will validate | |
7373 | -- name of selector. | |
dbfeb4fa RD |
7374 | |
7375 | -- ??? Could we rewrite an implicit dereference into an explicit | |
11560bcc | 7376 | -- one here? |
996ae0b0 RK |
7377 | |
7378 | Analyze_Selected_Component (N); | |
7379 | ||
f6b5dc8e AC |
7380 | -- Reference to type name in predicate/invariant expression |
7381 | ||
996ae0b0 RK |
7382 | elsif Is_Appropriate_For_Entry_Prefix (P_Type) |
7383 | and then not In_Open_Scopes (P_Name) | |
7384 | and then (not Is_Concurrent_Type (Etype (P_Name)) | |
ac7d724d | 7385 | or else not In_Open_Scopes (Etype (P_Name))) |
996ae0b0 RK |
7386 | then |
7387 | -- Call to protected operation or entry. Type checking is | |
7388 | -- needed on the prefix. | |
7389 | ||
7390 | Analyze_Selected_Component (N); | |
7391 | ||
7392 | elsif (In_Open_Scopes (P_Name) | |
f6b5dc8e AC |
7393 | and then Ekind (P_Name) /= E_Void |
7394 | and then not Is_Overloadable (P_Name)) | |
996ae0b0 | 7395 | or else (Is_Concurrent_Type (Etype (P_Name)) |
f6b5dc8e | 7396 | and then In_Open_Scopes (Etype (P_Name))) |
996ae0b0 RK |
7397 | then |
7398 | -- Prefix denotes an enclosing loop, block, or task, i.e. an | |
7399 | -- enclosing construct that is not a subprogram or accept. | |
7400 | ||
549cc9c2 AC |
7401 | -- A special case: a protected body may call an operation |
7402 | -- on an external object of the same type, in which case it | |
7403 | -- is not an expanded name. If the prefix is the type itself, | |
7404 | -- or the context is a single synchronized object it can only | |
7405 | -- be interpreted as an expanded name. | |
7406 | ||
7407 | if Is_Concurrent_Type (Etype (P_Name)) then | |
7408 | if Is_Type (P_Name) | |
7409 | or else Present (Anonymous_Object (Etype (P_Name))) | |
7410 | then | |
7411 | Find_Expanded_Name (N); | |
7412 | ||
7413 | else | |
7414 | Analyze_Selected_Component (N); | |
7415 | return; | |
7416 | end if; | |
7417 | ||
7418 | else | |
7419 | Find_Expanded_Name (N); | |
7420 | end if; | |
996ae0b0 RK |
7421 | |
7422 | elsif Ekind (P_Name) = E_Package then | |
7423 | Find_Expanded_Name (N); | |
7424 | ||
7425 | elsif Is_Overloadable (P_Name) then | |
7426 | ||
7427 | -- The subprogram may be a renaming (of an enclosing scope) as | |
7428 | -- in the case of the name of the generic within an instantiation. | |
7429 | ||
f6b5dc8e | 7430 | if Ekind_In (P_Name, E_Procedure, E_Function) |
996ae0b0 RK |
7431 | and then Present (Alias (P_Name)) |
7432 | and then Is_Generic_Instance (Alias (P_Name)) | |
7433 | then | |
7434 | P_Name := Alias (P_Name); | |
7435 | end if; | |
7436 | ||
7437 | if Is_Overloaded (P) then | |
7438 | ||
bc41faa2 | 7439 | -- The prefix must resolve to a unique enclosing construct |
996ae0b0 RK |
7440 | |
7441 | declare | |
7442 | Found : Boolean := False; | |
fbf5a39b | 7443 | Ind : Interp_Index; |
996ae0b0 RK |
7444 | It : Interp; |
7445 | ||
7446 | begin | |
fbf5a39b | 7447 | Get_First_Interp (P, Ind, It); |
996ae0b0 | 7448 | while Present (It.Nam) loop |
996ae0b0 RK |
7449 | if In_Open_Scopes (It.Nam) then |
7450 | if Found then | |
7451 | Error_Msg_N ( | |
7452 | "prefix must be unique enclosing scope", N); | |
7453 | Set_Entity (N, Any_Id); | |
7454 | Set_Etype (N, Any_Type); | |
7455 | return; | |
7456 | ||
7457 | else | |
7458 | Found := True; | |
7459 | P_Name := It.Nam; | |
7460 | end if; | |
7461 | end if; | |
7462 | ||
fbf5a39b | 7463 | Get_Next_Interp (Ind, It); |
996ae0b0 RK |
7464 | end loop; |
7465 | end; | |
7466 | end if; | |
7467 | ||
7468 | if In_Open_Scopes (P_Name) then | |
7469 | Set_Entity (P, P_Name); | |
7470 | Set_Is_Overloaded (P, False); | |
7471 | Find_Expanded_Name (N); | |
7472 | ||
7473 | else | |
7474 | -- If no interpretation as an expanded name is possible, it | |
7475 | -- must be a selected component of a record returned by a | |
16ca248a | 7476 | -- function call. Reformat prefix as a function call, the rest |
2757c5bf AC |
7477 | -- is done by type resolution. |
7478 | ||
7479 | -- Error if the prefix is procedure or entry, as is P.X | |
996ae0b0 RK |
7480 | |
7481 | if Ekind (P_Name) /= E_Function | |
ac7d724d ES |
7482 | and then |
7483 | (not Is_Overloaded (P) | |
7484 | or else Nkind (Parent (N)) = N_Procedure_Call_Statement) | |
996ae0b0 | 7485 | then |
996ae0b0 | 7486 | -- Prefix may mention a package that is hidden by a local |
290986ed GB |
7487 | -- declaration: let the user know. Scan the full homonym |
7488 | -- chain, the candidate package may be anywhere on it. | |
996ae0b0 | 7489 | |
290986ed | 7490 | if Present (Homonym (Current_Entity (P_Name))) then |
290986ed | 7491 | P_Name := Current_Entity (P_Name); |
996ae0b0 RK |
7492 | |
7493 | while Present (P_Name) loop | |
7494 | exit when Ekind (P_Name) = E_Package; | |
7495 | P_Name := Homonym (P_Name); | |
7496 | end loop; | |
7497 | ||
7498 | if Present (P_Name) then | |
2e70d415 AC |
7499 | if not Is_Reference_In_Subunit then |
7500 | Error_Msg_Sloc := Sloc (Entity (Prefix (N))); | |
2e70d415 | 7501 | Error_Msg_NE |
f5f6d8d7 | 7502 | ("package& is hidden by declaration#", N, P_Name); |
2e70d415 | 7503 | end if; |
996ae0b0 RK |
7504 | |
7505 | Set_Entity (Prefix (N), P_Name); | |
7506 | Find_Expanded_Name (N); | |
7507 | return; | |
2757c5bf | 7508 | |
996ae0b0 RK |
7509 | else |
7510 | P_Name := Entity (Prefix (N)); | |
7511 | end if; | |
7512 | end if; | |
7513 | ||
7514 | Error_Msg_NE | |
7515 | ("invalid prefix in selected component&", N, P_Name); | |
7516 | Change_Selected_Component_To_Expanded_Name (N); | |
7517 | Set_Entity (N, Any_Id); | |
7518 | Set_Etype (N, Any_Type); | |
7519 | ||
2757c5bf AC |
7520 | -- Here we have a function call, so do the reformatting |
7521 | ||
996ae0b0 RK |
7522 | else |
7523 | Nam := New_Copy (P); | |
7524 | Save_Interps (P, Nam); | |
2757c5bf AC |
7525 | |
7526 | -- We use Replace here because this is one of those cases | |
c63a2ad6 AC |
7527 | -- where the parser has missclassified the node, and we fix |
7528 | -- things up and then do the semantic analysis on the fixed | |
7529 | -- up node. Normally we do this using one of the Sinfo.CN | |
7530 | -- routines, but this is too tricky for that. | |
2757c5bf | 7531 | |
c63a2ad6 AC |
7532 | -- Note that using Rewrite would be wrong, because we would |
7533 | -- have a tree where the original node is unanalyzed, and | |
7534 | -- this violates the required interface for ASIS. | |
2757c5bf AC |
7535 | |
7536 | Replace (P, | |
996ae0b0 | 7537 | Make_Function_Call (Sloc (P), Name => Nam)); |
2757c5bf AC |
7538 | |
7539 | -- Now analyze the reformatted node | |
7540 | ||
996ae0b0 | 7541 | Analyze_Call (P); |
40bf00b1 | 7542 | |
c63a2ad6 AC |
7543 | -- If the prefix is illegal after this transformation, there |
7544 | -- may be visibility errors on the prefix. The safest is to | |
7545 | -- treat the selected component as an error. | |
40bf00b1 AC |
7546 | |
7547 | if Error_Posted (P) then | |
7548 | Set_Etype (N, Any_Type); | |
7549 | return; | |
7550 | ||
7551 | else | |
7552 | Analyze_Selected_Component (N); | |
7553 | end if; | |
996ae0b0 RK |
7554 | end if; |
7555 | end if; | |
7556 | ||
7557 | -- Remaining cases generate various error messages | |
7558 | ||
7559 | else | |
7560 | -- Format node as expanded name, to avoid cascaded errors | |
7561 | ||
c63a2ad6 AC |
7562 | -- If the limited_with transformation was applied earlier, restore |
7563 | -- source for proper error reporting. | |
b943a971 AC |
7564 | |
7565 | if not Comes_From_Source (P) | |
7566 | and then Nkind (P) = N_Explicit_Dereference | |
7567 | then | |
7568 | Rewrite (P, Prefix (P)); | |
7569 | P_Type := Etype (P); | |
7570 | end if; | |
7571 | ||
fbf5a39b | 7572 | Change_Selected_Component_To_Expanded_Name (N); |
b943a971 AC |
7573 | Set_Entity (N, Any_Id); |
7574 | Set_Etype (N, Any_Type); | |
996ae0b0 | 7575 | |
996ae0b0 RK |
7576 | -- Issue error message, but avoid this if error issued already. |
7577 | -- Use identifier of prefix if one is available. | |
7578 | ||
bcb0389e | 7579 | if P_Name = Any_Id then |
996ae0b0 RK |
7580 | null; |
7581 | ||
596b25f9 AC |
7582 | -- It is not an error if the prefix is the current instance of |
7583 | -- type name, e.g. the expression of a type aspect, when it is | |
f51e316c ES |
7584 | -- analyzed for ASIS use, or within a generic unit. We still |
7585 | -- have to verify that a component of that name exists, and | |
7586 | -- decorate the node accordingly. | |
596b25f9 AC |
7587 | |
7588 | elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then | |
f51e316c ES |
7589 | declare |
7590 | Comp : Entity_Id; | |
7591 | ||
7592 | begin | |
7593 | Comp := First_Entity (Entity (P)); | |
7594 | while Present (Comp) loop | |
7595 | if Chars (Comp) = Chars (Selector_Name (N)) then | |
7596 | Set_Entity (N, Comp); | |
7597 | Set_Etype (N, Etype (Comp)); | |
7598 | Set_Entity (Selector_Name (N), Comp); | |
7599 | Set_Etype (Selector_Name (N), Etype (Comp)); | |
7600 | return; | |
7601 | end if; | |
7602 | ||
7603 | Next_Entity (Comp); | |
7604 | end loop; | |
7605 | end; | |
596b25f9 | 7606 | |
996ae0b0 RK |
7607 | elsif Ekind (P_Name) = E_Void then |
7608 | Premature_Usage (P); | |
7609 | ||
7610 | elsif Nkind (P) /= N_Attribute_Reference then | |
67c0e662 RD |
7611 | |
7612 | -- This may have been meant as a prefixed call to a primitive | |
96df3ff4 AC |
7613 | -- of an untagged type. If it is a function call check type of |
7614 | -- its first formal and add explanation. | |
67c0e662 RD |
7615 | |
7616 | declare | |
7617 | F : constant Entity_Id := | |
7618 | Current_Entity (Selector_Name (N)); | |
7619 | begin | |
7620 | if Present (F) | |
7621 | and then Is_Overloadable (F) | |
7622 | and then Present (First_Entity (F)) | |
96df3ff4 | 7623 | and then not Is_Tagged_Type (Etype (First_Entity (F))) |
67c0e662 RD |
7624 | then |
7625 | Error_Msg_N | |
c308e762 HK |
7626 | ("prefixed call is only allowed for objects of a " |
7627 | & "tagged type", N); | |
67c0e662 RD |
7628 | end if; |
7629 | end; | |
7630 | ||
7631 | Error_Msg_N ("invalid prefix in selected component&", P); | |
996ae0b0 | 7632 | |
9596236a AC |
7633 | if Is_Access_Type (P_Type) |
7634 | and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type | |
7635 | then | |
65356e64 | 7636 | Error_Msg_N |
67c0e662 RD |
7637 | ("\dereference must not be of an incomplete type " |
7638 | & "(RM 3.10.1)", P); | |
9596236a AC |
7639 | end if; |
7640 | ||
996ae0b0 | 7641 | else |
67c0e662 | 7642 | Error_Msg_N ("invalid prefix in selected component", P); |
996ae0b0 RK |
7643 | end if; |
7644 | end if; | |
7645 | ||
db72f10a AC |
7646 | -- Selector name is restricted in SPARK |
7647 | ||
7648 | if Nkind (N) = N_Expanded_Name | |
6480338a | 7649 | and then Restriction_Check_Required (SPARK_05) |
db72f10a AC |
7650 | then |
7651 | if Is_Subprogram (P_Name) then | |
ce5ba43a | 7652 | Check_SPARK_05_Restriction |
db72f10a AC |
7653 | ("prefix of expanded name cannot be a subprogram", P); |
7654 | elsif Ekind (P_Name) = E_Loop then | |
ce5ba43a | 7655 | Check_SPARK_05_Restriction |
db72f10a AC |
7656 | ("prefix of expanded name cannot be a loop statement", P); |
7657 | end if; | |
7658 | end if; | |
7659 | ||
996ae0b0 RK |
7660 | else |
7661 | -- If prefix is not the name of an entity, it must be an expression, | |
7662 | -- whose type is appropriate for a record. This is determined by | |
7663 | -- type resolution. | |
7664 | ||
7665 | Analyze_Selected_Component (N); | |
7666 | end if; | |
0929eaeb AC |
7667 | |
7668 | Analyze_Dimension (N); | |
996ae0b0 RK |
7669 | end Find_Selected_Component; |
7670 | ||
7671 | --------------- | |
7672 | -- Find_Type -- | |
7673 | --------------- | |
7674 | ||
7675 | procedure Find_Type (N : Node_Id) is | |
7676 | C : Entity_Id; | |
7677 | Typ : Entity_Id; | |
7678 | T : Entity_Id; | |
7679 | T_Name : Entity_Id; | |
7680 | ||
7681 | begin | |
7682 | if N = Error then | |
7683 | return; | |
7684 | ||
7685 | elsif Nkind (N) = N_Attribute_Reference then | |
7686 | ||
11560bcc TQ |
7687 | -- Class attribute. This is not valid in Ada 83 mode, but we do not |
7688 | -- need to enforce that at this point, since the declaration of the | |
7689 | -- tagged type in the prefix would have been flagged already. | |
996ae0b0 RK |
7690 | |
7691 | if Attribute_Name (N) = Name_Class then | |
7692 | Check_Restriction (No_Dispatch, N); | |
7693 | Find_Type (Prefix (N)); | |
7694 | ||
7695 | -- Propagate error from bad prefix | |
7696 | ||
7697 | if Etype (Prefix (N)) = Any_Type then | |
7698 | Set_Entity (N, Any_Type); | |
7699 | Set_Etype (N, Any_Type); | |
7700 | return; | |
7701 | end if; | |
7702 | ||
7703 | T := Base_Type (Entity (Prefix (N))); | |
7704 | ||
11560bcc TQ |
7705 | -- Case where type is not known to be tagged. Its appearance in |
7706 | -- the prefix of the 'Class attribute indicates that the full view | |
16ca248a | 7707 | -- will be tagged. |
996ae0b0 RK |
7708 | |
7709 | if not Is_Tagged_Type (T) then | |
7710 | if Ekind (T) = E_Incomplete_Type then | |
7711 | ||
7712 | -- It is legal to denote the class type of an incomplete | |
7713 | -- type. The full type will have to be tagged, of course. | |
90067a15 | 7714 | -- In Ada 2005 this usage is declared obsolescent, so we |
ae247488 ES |
7715 | -- warn accordingly. This usage is only legal if the type |
7716 | -- is completed in the current scope, and not for a limited | |
7717 | -- view of a type. | |
7718 | ||
2f7b7467 AC |
7719 | if Ada_Version >= Ada_2005 then |
7720 | ||
7721 | -- Test whether the Available_View of a limited type view | |
7722 | -- is tagged, since the limited view may not be marked as | |
7723 | -- tagged if the type itself has an untagged incomplete | |
7724 | -- type view in its package. | |
7725 | ||
7b56a91b | 7726 | if From_Limited_With (T) |
2f7b7467 AC |
7727 | and then not Is_Tagged_Type (Available_View (T)) |
7728 | then | |
ae247488 ES |
7729 | Error_Msg_N |
7730 | ("prefix of Class attribute must be tagged", N); | |
7731 | Set_Etype (N, Any_Type); | |
7732 | Set_Entity (N, Any_Type); | |
7733 | return; | |
11560bcc | 7734 | |
dbfeb4fa RD |
7735 | -- ??? This test is temporarily disabled (always |
7736 | -- False) because it causes an unwanted warning on | |
7737 | -- GNAT sources (built with -gnatg, which includes | |
7738 | -- Warn_On_Obsolescent_ Feature). Once this issue | |
7739 | -- is cleared in the sources, it can be enabled. | |
11560bcc | 7740 | |
ac7d724d | 7741 | elsif Warn_On_Obsolescent_Feature and then False then |
ae247488 ES |
7742 | Error_Msg_N |
7743 | ("applying 'Class to an untagged incomplete type" | |
dbfeb4fa | 7744 | & " is an obsolescent feature (RM J.11)?r?", N); |
ae247488 | 7745 | end if; |
11560bcc | 7746 | end if; |
996ae0b0 RK |
7747 | |
7748 | Set_Is_Tagged_Type (T); | |
ef2a63ba | 7749 | Set_Direct_Primitive_Operations (T, New_Elmt_List); |
996ae0b0 RK |
7750 | Make_Class_Wide_Type (T); |
7751 | Set_Entity (N, Class_Wide_Type (T)); | |
7752 | Set_Etype (N, Class_Wide_Type (T)); | |
7753 | ||
7754 | elsif Ekind (T) = E_Private_Type | |
7755 | and then not Is_Generic_Type (T) | |
7756 | and then In_Private_Part (Scope (T)) | |
7757 | then | |
16ca248a ES |
7758 | -- The Class attribute can be applied to an untagged private |
7759 | -- type fulfilled by a tagged type prior to the full type | |
7760 | -- declaration (but only within the parent package's private | |
7761 | -- part). Create the class-wide type now and check that the | |
7762 | -- full type is tagged later during its analysis. Note that | |
7763 | -- we do not mark the private type as tagged, unlike the | |
7764 | -- case of incomplete types, because the type must still | |
996ae0b0 RK |
7765 | -- appear untagged to outside units. |
7766 | ||
cdc8c54c | 7767 | if No (Class_Wide_Type (T)) then |
996ae0b0 RK |
7768 | Make_Class_Wide_Type (T); |
7769 | end if; | |
7770 | ||
7771 | Set_Entity (N, Class_Wide_Type (T)); | |
7772 | Set_Etype (N, Class_Wide_Type (T)); | |
7773 | ||
7774 | else | |
16ca248a ES |
7775 | -- Should we introduce a type Any_Tagged and use Wrong_Type |
7776 | -- here, it would be a bit more consistent??? | |
996ae0b0 RK |
7777 | |
7778 | Error_Msg_NE | |
7779 | ("tagged type required, found}", | |
7780 | Prefix (N), First_Subtype (T)); | |
7781 | Set_Entity (N, Any_Type); | |
7782 | return; | |
7783 | end if; | |
7784 | ||
7785 | -- Case of tagged type | |
7786 | ||
7787 | else | |
39edfb45 | 7788 | if Is_Concurrent_Type (T) then |
cdc8c54c BD |
7789 | if No (Corresponding_Record_Type (Entity (Prefix (N)))) then |
7790 | ||
f2a54683 AC |
7791 | -- Previous error. Create a class-wide type for the |
7792 | -- synchronized type itself, with minimal semantic | |
7793 | -- attributes, to catch other errors in some ACATS tests. | |
cdc8c54c | 7794 | |
c48e0f27 | 7795 | pragma Assert (Serious_Errors_Detected /= 0); |
f2a54683 AC |
7796 | Make_Class_Wide_Type (T); |
7797 | C := Class_Wide_Type (T); | |
7798 | Set_First_Entity (C, First_Entity (T)); | |
cdc8c54c BD |
7799 | |
7800 | else | |
7801 | C := Class_Wide_Type | |
7802 | (Corresponding_Record_Type (Entity (Prefix (N)))); | |
7803 | end if; | |
7804 | ||
39edfb45 JM |
7805 | else |
7806 | C := Class_Wide_Type (Entity (Prefix (N))); | |
7807 | end if; | |
7808 | ||
e7ba564f | 7809 | Set_Entity_With_Checks (N, C); |
996ae0b0 RK |
7810 | Generate_Reference (C, N); |
7811 | Set_Etype (N, C); | |
996ae0b0 RK |
7812 | end if; |
7813 | ||
0ab80019 | 7814 | -- Base attribute, not allowed in Ada 83 |
996ae0b0 RK |
7815 | |
7816 | elsif Attribute_Name (N) = Name_Base then | |
7a489a2b | 7817 | Error_Msg_Name_1 := Name_Base; |
ce5ba43a | 7818 | Check_SPARK_05_Restriction |
7a489a2b AC |
7819 | ("attribute% is only allowed as prefix of another attribute", N); |
7820 | ||
0ab80019 | 7821 | if Ada_Version = Ada_83 and then Comes_From_Source (N) then |
996ae0b0 RK |
7822 | Error_Msg_N |
7823 | ("(Ada 83) Base attribute not allowed in subtype mark", N); | |
7824 | ||
7825 | else | |
7826 | Find_Type (Prefix (N)); | |
7827 | Typ := Entity (Prefix (N)); | |
7828 | ||
0ab80019 | 7829 | if Ada_Version >= Ada_95 |
fbf5a39b AC |
7830 | and then not Is_Scalar_Type (Typ) |
7831 | and then not Is_Generic_Type (Typ) | |
7832 | then | |
7833 | Error_Msg_N | |
bc41faa2 AC |
7834 | ("prefix of Base attribute must be scalar type", |
7835 | Prefix (N)); | |
fbf5a39b | 7836 | |
a8930b80 | 7837 | elsif Warn_On_Redundant_Constructs |
996ae0b0 | 7838 | and then Base_Type (Typ) = Typ |
996ae0b0 | 7839 | then |
ed2233dc | 7840 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 7841 | ("redundant attribute, & is its own base type?r?", N, Typ); |
996ae0b0 RK |
7842 | end if; |
7843 | ||
7844 | T := Base_Type (Typ); | |
996ae0b0 RK |
7845 | |
7846 | -- Rewrite attribute reference with type itself (see similar | |
a8930b80 AC |
7847 | -- processing in Analyze_Attribute, case Base). Preserve prefix |
7848 | -- if present, for other legality checks. | |
fbf5a39b AC |
7849 | |
7850 | if Nkind (Prefix (N)) = N_Expanded_Name then | |
7851 | Rewrite (N, | |
7852 | Make_Expanded_Name (Sloc (N), | |
11560bcc TQ |
7853 | Chars => Chars (T), |
7854 | Prefix => New_Copy (Prefix (Prefix (N))), | |
e4494292 | 7855 | Selector_Name => New_Occurrence_Of (T, Sloc (N)))); |
996ae0b0 | 7856 | |
fbf5a39b | 7857 | else |
e4494292 | 7858 | Rewrite (N, New_Occurrence_Of (T, Sloc (N))); |
fbf5a39b AC |
7859 | end if; |
7860 | ||
7861 | Set_Entity (N, T); | |
996ae0b0 RK |
7862 | Set_Etype (N, T); |
7863 | end if; | |
7864 | ||
923fa078 RD |
7865 | elsif Attribute_Name (N) = Name_Stub_Type then |
7866 | ||
7867 | -- This is handled in Analyze_Attribute | |
7868 | ||
7869 | Analyze (N); | |
7870 | ||
996ae0b0 RK |
7871 | -- All other attributes are invalid in a subtype mark |
7872 | ||
7873 | else | |
7874 | Error_Msg_N ("invalid attribute in subtype mark", N); | |
7875 | end if; | |
7876 | ||
7877 | else | |
7878 | Analyze (N); | |
7879 | ||
7880 | if Is_Entity_Name (N) then | |
7881 | T_Name := Entity (N); | |
7882 | else | |
7883 | Error_Msg_N ("subtype mark required in this context", N); | |
7884 | Set_Etype (N, Any_Type); | |
7885 | return; | |
7886 | end if; | |
7887 | ||
7888 | if T_Name = Any_Id or else Etype (N) = Any_Type then | |
7889 | ||
7890 | -- Undefined id. Make it into a valid type | |
7891 | ||
7892 | Set_Entity (N, Any_Type); | |
7893 | ||
7894 | elsif not Is_Type (T_Name) | |
7895 | and then T_Name /= Standard_Void_Type | |
7896 | then | |
7897 | Error_Msg_Sloc := Sloc (T_Name); | |
7898 | Error_Msg_N ("subtype mark required in this context", N); | |
923fa078 | 7899 | Error_Msg_NE ("\\found & declared#", N, T_Name); |
996ae0b0 RK |
7900 | Set_Entity (N, Any_Type); |
7901 | ||
7902 | else | |
11560bcc TQ |
7903 | -- If the type is an incomplete type created to handle |
7904 | -- anonymous access components of a record type, then the | |
7905 | -- incomplete type is the visible entity and subsequent | |
7906 | -- references will point to it. Mark the original full | |
7907 | -- type as referenced, to prevent spurious warnings. | |
7908 | ||
7909 | if Is_Incomplete_Type (T_Name) | |
7910 | and then Present (Full_View (T_Name)) | |
7911 | and then not Comes_From_Source (T_Name) | |
7912 | then | |
7913 | Set_Referenced (Full_View (T_Name)); | |
7914 | end if; | |
7915 | ||
996ae0b0 RK |
7916 | T_Name := Get_Full_View (T_Name); |
7917 | ||
11560bcc TQ |
7918 | -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through |
7919 | -- limited-with clauses | |
7920 | ||
7b56a91b | 7921 | if From_Limited_With (T_Name) |
11560bcc TQ |
7922 | and then Ekind (T_Name) in Incomplete_Kind |
7923 | and then Present (Non_Limited_View (T_Name)) | |
7924 | and then Is_Interface (Non_Limited_View (T_Name)) | |
7925 | then | |
7926 | T_Name := Non_Limited_View (T_Name); | |
7927 | end if; | |
7928 | ||
996ae0b0 RK |
7929 | if In_Open_Scopes (T_Name) then |
7930 | if Ekind (Base_Type (T_Name)) = E_Task_Type then | |
923fa078 RD |
7931 | |
7932 | -- In Ada 2005, a task name can be used in an access | |
e264efcc AC |
7933 | -- definition within its own body. It cannot be used |
7934 | -- in the discriminant part of the task declaration, | |
7935 | -- nor anywhere else in the declaration because entries | |
7936 | -- cannot have access parameters. | |
923fa078 | 7937 | |
0791fbe9 | 7938 | if Ada_Version >= Ada_2005 |
923fa078 RD |
7939 | and then Nkind (Parent (N)) = N_Access_Definition |
7940 | then | |
7941 | Set_Entity (N, T_Name); | |
7942 | Set_Etype (N, T_Name); | |
e264efcc AC |
7943 | |
7944 | if Has_Completion (T_Name) then | |
7945 | return; | |
7946 | ||
7947 | else | |
7948 | Error_Msg_N | |
7949 | ("task type cannot be used as type mark " & | |
7950 | "within its own declaration", N); | |
7951 | end if; | |
923fa078 RD |
7952 | |
7953 | else | |
7954 | Error_Msg_N | |
7955 | ("task type cannot be used as type mark " & | |
fbe627af | 7956 | "within its own spec or body", N); |
923fa078 | 7957 | end if; |
fbe627af RD |
7958 | |
7959 | elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then | |
7960 | ||
7961 | -- In Ada 2005, a protected name can be used in an access | |
7962 | -- definition within its own body. | |
7963 | ||
0791fbe9 | 7964 | if Ada_Version >= Ada_2005 |
fbe627af RD |
7965 | and then Nkind (Parent (N)) = N_Access_Definition |
7966 | then | |
7967 | Set_Entity (N, T_Name); | |
7968 | Set_Etype (N, T_Name); | |
7969 | return; | |
7970 | ||
7971 | else | |
7972 | Error_Msg_N | |
7973 | ("protected type cannot be used as type mark " & | |
7974 | "within its own spec or body", N); | |
7975 | end if; | |
7976 | ||
996ae0b0 RK |
7977 | else |
7978 | Error_Msg_N ("type declaration cannot refer to itself", N); | |
7979 | end if; | |
7980 | ||
7981 | Set_Etype (N, Any_Type); | |
7982 | Set_Entity (N, Any_Type); | |
7983 | Set_Error_Posted (T_Name); | |
7984 | return; | |
7985 | end if; | |
7986 | ||
7987 | Set_Entity (N, T_Name); | |
7988 | Set_Etype (N, T_Name); | |
7989 | end if; | |
7990 | end if; | |
7991 | ||
07fc65c4 | 7992 | if Present (Etype (N)) and then Comes_From_Source (N) then |
996ae0b0 RK |
7993 | if Is_Fixed_Point_Type (Etype (N)) then |
7994 | Check_Restriction (No_Fixed_Point, N); | |
7995 | elsif Is_Floating_Point_Type (Etype (N)) then | |
7996 | Check_Restriction (No_Floating_Point, N); | |
7997 | end if; | |
8636f52f HK |
7998 | |
7999 | -- A Ghost type must appear in a specific context | |
8000 | ||
8001 | if Is_Ghost_Entity (Etype (N)) then | |
8002 | Check_Ghost_Context (Etype (N), N); | |
8003 | end if; | |
996ae0b0 RK |
8004 | end if; |
8005 | end Find_Type; | |
8006 | ||
996ae0b0 RK |
8007 | ------------------------------------ |
8008 | -- Has_Implicit_Character_Literal -- | |
8009 | ------------------------------------ | |
8010 | ||
8011 | function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is | |
8012 | Id : Entity_Id; | |
8013 | Found : Boolean := False; | |
8014 | P : constant Entity_Id := Entity (Prefix (N)); | |
8015 | Priv_Id : Entity_Id := Empty; | |
8016 | ||
8017 | begin | |
ac7d724d | 8018 | if Ekind (P) = E_Package and then not In_Open_Scopes (P) then |
996ae0b0 RK |
8019 | Priv_Id := First_Private_Entity (P); |
8020 | end if; | |
8021 | ||
8022 | if P = Standard_Standard then | |
8023 | Change_Selected_Component_To_Expanded_Name (N); | |
8024 | Rewrite (N, Selector_Name (N)); | |
8025 | Analyze (N); | |
8026 | Set_Etype (Original_Node (N), Standard_Character); | |
8027 | return True; | |
8028 | end if; | |
8029 | ||
8030 | Id := First_Entity (P); | |
ac7d724d | 8031 | while Present (Id) and then Id /= Priv_Id loop |
d347f572 AC |
8032 | if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then |
8033 | ||
996ae0b0 RK |
8034 | -- We replace the node with the literal itself, resolve as a |
8035 | -- character, and set the type correctly. | |
8036 | ||
8037 | if not Found then | |
8038 | Change_Selected_Component_To_Expanded_Name (N); | |
8039 | Rewrite (N, Selector_Name (N)); | |
8040 | Analyze (N); | |
8041 | Set_Etype (N, Id); | |
8042 | Set_Etype (Original_Node (N), Id); | |
8043 | Found := True; | |
8044 | ||
8045 | else | |
8046 | -- More than one type derived from Character in given scope. | |
8047 | -- Collect all possible interpretations. | |
8048 | ||
8049 | Add_One_Interp (N, Id, Id); | |
8050 | end if; | |
8051 | end if; | |
8052 | ||
8053 | Next_Entity (Id); | |
8054 | end loop; | |
8055 | ||
8056 | return Found; | |
8057 | end Has_Implicit_Character_Literal; | |
8058 | ||
9bc856dd AC |
8059 | ---------------------- |
8060 | -- Has_Private_With -- | |
8061 | ---------------------- | |
8062 | ||
8063 | function Has_Private_With (E : Entity_Id) return Boolean is | |
8064 | Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit); | |
8065 | Item : Node_Id; | |
8066 | ||
8067 | begin | |
8068 | Item := First (Context_Items (Comp_Unit)); | |
8069 | while Present (Item) loop | |
8070 | if Nkind (Item) = N_With_Clause | |
8071 | and then Private_Present (Item) | |
8072 | and then Entity (Name (Item)) = E | |
8073 | then | |
8074 | return True; | |
8075 | end if; | |
8076 | ||
8077 | Next (Item); | |
8078 | end loop; | |
8079 | ||
8080 | return False; | |
8081 | end Has_Private_With; | |
8082 | ||
996ae0b0 RK |
8083 | --------------------------- |
8084 | -- Has_Implicit_Operator -- | |
8085 | --------------------------- | |
8086 | ||
8087 | function Has_Implicit_Operator (N : Node_Id) return Boolean is | |
8088 | Op_Id : constant Name_Id := Chars (Selector_Name (N)); | |
8089 | P : constant Entity_Id := Entity (Prefix (N)); | |
8090 | Id : Entity_Id; | |
8091 | Priv_Id : Entity_Id := Empty; | |
8092 | ||
fbf5a39b AC |
8093 | procedure Add_Implicit_Operator |
8094 | (T : Entity_Id; | |
8095 | Op_Type : Entity_Id := Empty); | |
fbe627af RD |
8096 | -- Add implicit interpretation to node N, using the type for which a |
8097 | -- predefined operator exists. If the operator yields a boolean type, | |
8098 | -- the Operand_Type is implicitly referenced by the operator, and a | |
8099 | -- reference to it must be generated. | |
996ae0b0 RK |
8100 | |
8101 | --------------------------- | |
8102 | -- Add_Implicit_Operator -- | |
8103 | --------------------------- | |
8104 | ||
fbf5a39b AC |
8105 | procedure Add_Implicit_Operator |
8106 | (T : Entity_Id; | |
8107 | Op_Type : Entity_Id := Empty) | |
8108 | is | |
996ae0b0 RK |
8109 | Predef_Op : Entity_Id; |
8110 | ||
8111 | begin | |
8112 | Predef_Op := Current_Entity (Selector_Name (N)); | |
996ae0b0 RK |
8113 | while Present (Predef_Op) |
8114 | and then Scope (Predef_Op) /= Standard_Standard | |
8115 | loop | |
8116 | Predef_Op := Homonym (Predef_Op); | |
8117 | end loop; | |
8118 | ||
8119 | if Nkind (N) = N_Selected_Component then | |
8120 | Change_Selected_Component_To_Expanded_Name (N); | |
8121 | end if; | |
8122 | ||
719aaf4d AC |
8123 | -- If the context is an unanalyzed function call, determine whether |
8124 | -- a binary or unary interpretation is required. | |
996ae0b0 | 8125 | |
719aaf4d AC |
8126 | if Nkind (Parent (N)) = N_Indexed_Component then |
8127 | declare | |
964f13da RD |
8128 | Is_Binary_Call : constant Boolean := |
8129 | Present | |
8130 | (Next (First (Expressions (Parent (N))))); | |
8131 | Is_Binary_Op : constant Boolean := | |
8132 | First_Entity | |
8133 | (Predef_Op) /= Last_Entity (Predef_Op); | |
719aaf4d AC |
8134 | Predef_Op2 : constant Entity_Id := Homonym (Predef_Op); |
8135 | ||
8136 | begin | |
8137 | if Is_Binary_Call then | |
8138 | if Is_Binary_Op then | |
8139 | Add_One_Interp (N, Predef_Op, T); | |
8140 | else | |
8141 | Add_One_Interp (N, Predef_Op2, T); | |
8142 | end if; | |
996ae0b0 | 8143 | |
719aaf4d AC |
8144 | else |
8145 | if not Is_Binary_Op then | |
8146 | Add_One_Interp (N, Predef_Op, T); | |
8147 | else | |
8148 | Add_One_Interp (N, Predef_Op2, T); | |
8149 | end if; | |
8150 | end if; | |
8151 | end; | |
8152 | ||
8153 | else | |
8154 | Add_One_Interp (N, Predef_Op, T); | |
8155 | ||
8156 | -- For operators with unary and binary interpretations, if | |
8157 | -- context is not a call, add both | |
8158 | ||
8159 | if Present (Homonym (Predef_Op)) then | |
8160 | Add_One_Interp (N, Homonym (Predef_Op), T); | |
8161 | end if; | |
996ae0b0 | 8162 | end if; |
fbf5a39b AC |
8163 | |
8164 | -- The node is a reference to a predefined operator, and | |
8165 | -- an implicit reference to the type of its operands. | |
8166 | ||
8167 | if Present (Op_Type) then | |
8168 | Generate_Operator_Reference (N, Op_Type); | |
8169 | else | |
8170 | Generate_Operator_Reference (N, T); | |
8171 | end if; | |
996ae0b0 RK |
8172 | end Add_Implicit_Operator; |
8173 | ||
8174 | -- Start of processing for Has_Implicit_Operator | |
8175 | ||
8176 | begin | |
ac7d724d | 8177 | if Ekind (P) = E_Package and then not In_Open_Scopes (P) then |
996ae0b0 RK |
8178 | Priv_Id := First_Private_Entity (P); |
8179 | end if; | |
8180 | ||
8181 | Id := First_Entity (P); | |
8182 | ||
8183 | case Op_Id is | |
8184 | ||
8185 | -- Boolean operators: an implicit declaration exists if the scope | |
8186 | -- contains a declaration for a derived Boolean type, or for an | |
8187 | -- array of Boolean type. | |
8188 | ||
d8f43ee6 HK |
8189 | when Name_Op_And |
8190 | | Name_Op_Not | |
8191 | | Name_Op_Or | |
8192 | | Name_Op_Xor | |
8193 | => | |
fd22e260 | 8194 | while Id /= Priv_Id loop |
d347f572 | 8195 | if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then |
996ae0b0 RK |
8196 | Add_Implicit_Operator (Id); |
8197 | return True; | |
8198 | end if; | |
8199 | ||
8200 | Next_Entity (Id); | |
8201 | end loop; | |
8202 | ||
bc41faa2 | 8203 | -- Equality: look for any non-limited type (result is Boolean) |
996ae0b0 | 8204 | |
d8f43ee6 HK |
8205 | when Name_Op_Eq |
8206 | | Name_Op_Ne | |
8207 | => | |
fd22e260 | 8208 | while Id /= Priv_Id loop |
996ae0b0 RK |
8209 | if Is_Type (Id) |
8210 | and then not Is_Limited_Type (Id) | |
d347f572 | 8211 | and then Is_Base_Type (Id) |
996ae0b0 | 8212 | then |
fbf5a39b | 8213 | Add_Implicit_Operator (Standard_Boolean, Id); |
996ae0b0 RK |
8214 | return True; |
8215 | end if; | |
8216 | ||
8217 | Next_Entity (Id); | |
8218 | end loop; | |
8219 | ||
bc41faa2 | 8220 | -- Comparison operators: scalar type, or array of scalar |
996ae0b0 | 8221 | |
d8f43ee6 HK |
8222 | when Name_Op_Ge |
8223 | | Name_Op_Gt | |
8224 | | Name_Op_Le | |
8225 | | Name_Op_Lt | |
8226 | => | |
fd22e260 | 8227 | while Id /= Priv_Id loop |
996ae0b0 | 8228 | if (Is_Scalar_Type (Id) |
d347f572 AC |
8229 | or else (Is_Array_Type (Id) |
8230 | and then Is_Scalar_Type (Component_Type (Id)))) | |
8231 | and then Is_Base_Type (Id) | |
996ae0b0 | 8232 | then |
fbf5a39b | 8233 | Add_Implicit_Operator (Standard_Boolean, Id); |
996ae0b0 RK |
8234 | return True; |
8235 | end if; | |
8236 | ||
8237 | Next_Entity (Id); | |
8238 | end loop; | |
8239 | ||
8240 | -- Arithmetic operators: any numeric type | |
8241 | ||
d8f43ee6 HK |
8242 | when Name_Op_Abs |
8243 | | Name_Op_Add | |
8244 | | Name_Op_Divide | |
8245 | | Name_Op_Expon | |
8246 | | Name_Op_Mod | |
8247 | | Name_Op_Multiply | |
8248 | | Name_Op_Rem | |
8249 | | Name_Op_Subtract | |
8250 | => | |
fd22e260 | 8251 | while Id /= Priv_Id loop |
d347f572 | 8252 | if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then |
996ae0b0 RK |
8253 | Add_Implicit_Operator (Id); |
8254 | return True; | |
8255 | end if; | |
8256 | ||
8257 | Next_Entity (Id); | |
8258 | end loop; | |
8259 | ||
8260 | -- Concatenation: any one-dimensional array type | |
8261 | ||
8262 | when Name_Op_Concat => | |
fd22e260 | 8263 | while Id /= Priv_Id loop |
d347f572 AC |
8264 | if Is_Array_Type (Id) |
8265 | and then Number_Dimensions (Id) = 1 | |
8266 | and then Is_Base_Type (Id) | |
996ae0b0 RK |
8267 | then |
8268 | Add_Implicit_Operator (Id); | |
8269 | return True; | |
8270 | end if; | |
8271 | ||
8272 | Next_Entity (Id); | |
8273 | end loop; | |
8274 | ||
8275 | -- What is the others condition here? Should we be using a | |
8276 | -- subtype of Name_Id that would restrict to operators ??? | |
8277 | ||
d8f43ee6 HK |
8278 | when others => |
8279 | null; | |
996ae0b0 RK |
8280 | end case; |
8281 | ||
8282 | -- If we fall through, then we do not have an implicit operator | |
8283 | ||
8284 | return False; | |
996ae0b0 RK |
8285 | end Has_Implicit_Operator; |
8286 | ||
607d0635 AC |
8287 | ----------------------------------- |
8288 | -- Has_Loop_In_Inner_Open_Scopes -- | |
8289 | ----------------------------------- | |
8290 | ||
8291 | function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is | |
8292 | begin | |
8293 | -- Several scope stacks are maintained by Scope_Stack. The base of the | |
8294 | -- currently active scope stack is denoted by the Is_Active_Stack_Base | |
8295 | -- flag in the scope stack entry. Note that the scope stacks used to | |
8296 | -- simply be delimited implicitly by the presence of Standard_Standard | |
8297 | -- at their base, but there now are cases where this is not sufficient | |
8298 | -- because Standard_Standard actually may appear in the middle of the | |
8299 | -- active set of scopes. | |
8300 | ||
8301 | for J in reverse 0 .. Scope_Stack.Last loop | |
8d606a78 RD |
8302 | |
8303 | -- S was reached without seing a loop scope first | |
8304 | ||
607d0635 | 8305 | if Scope_Stack.Table (J).Entity = S then |
607d0635 | 8306 | return False; |
8d606a78 RD |
8307 | |
8308 | -- S was not yet reached, so it contains at least one inner loop | |
8309 | ||
607d0635 | 8310 | elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then |
607d0635 AC |
8311 | return True; |
8312 | end if; | |
8313 | ||
8314 | -- Check Is_Active_Stack_Base to tell us when to stop, as there are | |
8315 | -- cases where Standard_Standard appears in the middle of the active | |
8316 | -- set of scopes. This affects the declaration and overriding of | |
8317 | -- private inherited operations in instantiations of generic child | |
8318 | -- units. | |
8319 | ||
8320 | pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base); | |
8321 | end loop; | |
8322 | ||
2010d078 | 8323 | raise Program_Error; -- unreachable |
607d0635 AC |
8324 | end Has_Loop_In_Inner_Open_Scopes; |
8325 | ||
996ae0b0 RK |
8326 | -------------------- |
8327 | -- In_Open_Scopes -- | |
8328 | -------------------- | |
8329 | ||
8330 | function In_Open_Scopes (S : Entity_Id) return Boolean is | |
8331 | begin | |
923fa078 RD |
8332 | -- Several scope stacks are maintained by Scope_Stack. The base of the |
8333 | -- currently active scope stack is denoted by the Is_Active_Stack_Base | |
8334 | -- flag in the scope stack entry. Note that the scope stacks used to | |
8335 | -- simply be delimited implicitly by the presence of Standard_Standard | |
8336 | -- at their base, but there now are cases where this is not sufficient | |
8337 | -- because Standard_Standard actually may appear in the middle of the | |
8338 | -- active set of scopes. | |
996ae0b0 RK |
8339 | |
8340 | for J in reverse 0 .. Scope_Stack.Last loop | |
8341 | if Scope_Stack.Table (J).Entity = S then | |
8342 | return True; | |
8343 | end if; | |
8344 | ||
923fa078 RD |
8345 | -- Check Is_Active_Stack_Base to tell us when to stop, as there are |
8346 | -- cases where Standard_Standard appears in the middle of the active | |
8347 | -- set of scopes. This affects the declaration and overriding of | |
8348 | -- private inherited operations in instantiations of generic child | |
8349 | -- units. | |
996ae0b0 RK |
8350 | |
8351 | exit when Scope_Stack.Table (J).Is_Active_Stack_Base; | |
8352 | end loop; | |
8353 | ||
8354 | return False; | |
8355 | end In_Open_Scopes; | |
8356 | ||
8357 | ----------------------------- | |
8358 | -- Inherit_Renamed_Profile -- | |
8359 | ----------------------------- | |
8360 | ||
8361 | procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is | |
8362 | New_F : Entity_Id; | |
8363 | Old_F : Entity_Id; | |
8364 | Old_T : Entity_Id; | |
8365 | New_T : Entity_Id; | |
8366 | ||
8367 | begin | |
8368 | if Ekind (Old_S) = E_Operator then | |
996ae0b0 RK |
8369 | New_F := First_Formal (New_S); |
8370 | ||
8371 | while Present (New_F) loop | |
8372 | Set_Etype (New_F, Base_Type (Etype (New_F))); | |
8373 | Next_Formal (New_F); | |
8374 | end loop; | |
8375 | ||
8376 | Set_Etype (New_S, Base_Type (Etype (New_S))); | |
8377 | ||
8378 | else | |
8379 | New_F := First_Formal (New_S); | |
8380 | Old_F := First_Formal (Old_S); | |
8381 | ||
8382 | while Present (New_F) loop | |
8383 | New_T := Etype (New_F); | |
8384 | Old_T := Etype (Old_F); | |
8385 | ||
ed3fe8cc AC |
8386 | -- If the new type is a renaming of the old one, as is the case |
8387 | -- for actuals in instances, retain its name, to simplify later | |
8388 | -- disambiguation. | |
996ae0b0 RK |
8389 | |
8390 | if Nkind (Parent (New_T)) = N_Subtype_Declaration | |
8391 | and then Is_Entity_Name (Subtype_Indication (Parent (New_T))) | |
8392 | and then Entity (Subtype_Indication (Parent (New_T))) = Old_T | |
8393 | then | |
8394 | null; | |
8395 | else | |
8396 | Set_Etype (New_F, Old_T); | |
8397 | end if; | |
8398 | ||
8399 | Next_Formal (New_F); | |
8400 | Next_Formal (Old_F); | |
8401 | end loop; | |
ed3fe8cc | 8402 | |
60aa5228 | 8403 | pragma Assert (No (Old_F)); |
996ae0b0 | 8404 | |
bce79204 | 8405 | if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then |
996ae0b0 RK |
8406 | Set_Etype (New_S, Etype (Old_S)); |
8407 | end if; | |
8408 | end if; | |
8409 | end Inherit_Renamed_Profile; | |
8410 | ||
8411 | ---------------- | |
8412 | -- Initialize -- | |
8413 | ---------------- | |
8414 | ||
8415 | procedure Initialize is | |
8416 | begin | |
8417 | Urefs.Init; | |
8418 | end Initialize; | |
8419 | ||
8420 | ------------------------- | |
8421 | -- Install_Use_Clauses -- | |
8422 | ------------------------- | |
8423 | ||
0da2c8ac AC |
8424 | procedure Install_Use_Clauses |
8425 | (Clause : Node_Id; | |
8426 | Force_Installation : Boolean := False) | |
8427 | is | |
851e9f19 | 8428 | U : Node_Id; |
996ae0b0 RK |
8429 | |
8430 | begin | |
16ca248a | 8431 | U := Clause; |
996ae0b0 RK |
8432 | while Present (U) loop |
8433 | ||
8434 | -- Case of USE package | |
8435 | ||
8436 | if Nkind (U) = N_Use_Package_Clause then | |
851e9f19 | 8437 | Use_One_Package (U, Name (U), True); |
996ae0b0 | 8438 | |
16ca248a | 8439 | -- Case of USE TYPE |
996ae0b0 RK |
8440 | |
8441 | else | |
851e9f19 | 8442 | Use_One_Type (Subtype_Mark (U), Force => Force_Installation); |
996ae0b0 | 8443 | |
996ae0b0 RK |
8444 | end if; |
8445 | ||
8446 | Next_Use_Clause (U); | |
8447 | end loop; | |
8448 | end Install_Use_Clauses; | |
8449 | ||
8450 | ------------------------------------- | |
8451 | -- Is_Appropriate_For_Entry_Prefix -- | |
8452 | ------------------------------------- | |
8453 | ||
8454 | function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is | |
8455 | P_Type : Entity_Id := T; | |
8456 | ||
8457 | begin | |
8458 | if Is_Access_Type (P_Type) then | |
8459 | P_Type := Designated_Type (P_Type); | |
8460 | end if; | |
8461 | ||
8462 | return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type); | |
8463 | end Is_Appropriate_For_Entry_Prefix; | |
8464 | ||
8465 | ------------------------------- | |
8466 | -- Is_Appropriate_For_Record -- | |
8467 | ------------------------------- | |
8468 | ||
2e071734 AC |
8469 | function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is |
8470 | ||
996ae0b0 RK |
8471 | function Has_Components (T1 : Entity_Id) return Boolean; |
8472 | -- Determine if given type has components (i.e. is either a record | |
8473 | -- type or a type that has discriminants). | |
8474 | ||
16ca248a ES |
8475 | -------------------- |
8476 | -- Has_Components -- | |
8477 | -------------------- | |
8478 | ||
996ae0b0 RK |
8479 | function Has_Components (T1 : Entity_Id) return Boolean is |
8480 | begin | |
8481 | return Is_Record_Type (T1) | |
8482 | or else (Is_Private_Type (T1) and then Has_Discriminants (T1)) | |
16ca248a ES |
8483 | or else (Is_Task_Type (T1) and then Has_Discriminants (T1)) |
8484 | or else (Is_Incomplete_Type (T1) | |
7b56a91b | 8485 | and then From_Limited_With (T1) |
16ca248a | 8486 | and then Present (Non_Limited_View (T1)) |
11560bcc TQ |
8487 | and then Is_Record_Type |
8488 | (Get_Full_View (Non_Limited_View (T1)))); | |
996ae0b0 RK |
8489 | end Has_Components; |
8490 | ||
8491 | -- Start of processing for Is_Appropriate_For_Record | |
8492 | ||
8493 | begin | |
8494 | return | |
8495 | Present (T) | |
8496 | and then (Has_Components (T) | |
16ca248a ES |
8497 | or else (Is_Access_Type (T) |
8498 | and then Has_Components (Designated_Type (T)))); | |
996ae0b0 RK |
8499 | end Is_Appropriate_For_Record; |
8500 | ||
851e9f19 PMR |
8501 | ---------------------- |
8502 | -- Mark_Use_Clauses -- | |
8503 | ---------------------- | |
d4810530 | 8504 | |
851e9f19 | 8505 | procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is |
851e9f19 PMR |
8506 | procedure Mark_Parameters (Call : Entity_Id); |
8507 | -- Perform use_type_clause marking for all parameters in a subprogram | |
8508 | -- or operator call. | |
d4810530 | 8509 | |
851e9f19 PMR |
8510 | procedure Mark_Use_Package (Pak : Entity_Id); |
8511 | -- Move up the Prev_Use_Clause chain for packages denoted by Pak - | |
8512 | -- marking each clause in the chain as effective in the process. | |
d4810530 | 8513 | |
851e9f19 PMR |
8514 | procedure Mark_Use_Type (E : Entity_Id); |
8515 | -- Similar to Do_Use_Package_Marking except we move up the | |
8516 | -- Prev_Use_Clause chain for the type denoted by E. | |
d4810530 | 8517 | |
851e9f19 PMR |
8518 | --------------------- |
8519 | -- Mark_Parameters -- | |
8520 | --------------------- | |
d4810530 | 8521 | |
851e9f19 PMR |
8522 | procedure Mark_Parameters (Call : Entity_Id) is |
8523 | Curr : Node_Id; | |
cdc8c54c | 8524 | |
851e9f19 PMR |
8525 | begin |
8526 | -- Move through all of the formals | |
d4810530 | 8527 | |
851e9f19 PMR |
8528 | Curr := First_Formal (Call); |
8529 | while Present (Curr) loop | |
8530 | Mark_Use_Type (Curr); | |
d4810530 | 8531 | |
851e9f19 PMR |
8532 | Curr := Next_Formal (Curr); |
8533 | end loop; | |
d4810530 | 8534 | |
851e9f19 | 8535 | -- Handle the return type |
d4810530 | 8536 | |
851e9f19 PMR |
8537 | Mark_Use_Type (Call); |
8538 | end Mark_Parameters; | |
d4810530 | 8539 | |
851e9f19 PMR |
8540 | ---------------------- |
8541 | -- Mark_Use_Package -- | |
8542 | ---------------------- | |
d4810530 | 8543 | |
851e9f19 PMR |
8544 | procedure Mark_Use_Package (Pak : Entity_Id) is |
8545 | Curr : Node_Id; | |
d4810530 | 8546 | |
851e9f19 | 8547 | begin |
7f5e671b PMR |
8548 | -- Ignore cases where the scope of the type is not a package (e.g. |
8549 | -- Standard_Standard). | |
d4810530 | 8550 | |
851e9f19 PMR |
8551 | if Ekind (Pak) /= E_Package then |
8552 | return; | |
8553 | end if; | |
d4810530 | 8554 | |
851e9f19 PMR |
8555 | Curr := Current_Use_Clause (Pak); |
8556 | while Present (Curr) | |
7f5e671b | 8557 | and then not Is_Effective_Use_Clause (Curr) |
851e9f19 | 8558 | loop |
7f5e671b PMR |
8559 | -- We need to mark the previous use clauses as effective, but |
8560 | -- each use clause may in turn render other use_package_clauses | |
851e9f19 PMR |
8561 | -- effective. Additionally, it is possible to have a parent |
8562 | -- package renamed as a child of itself so we must check the | |
8563 | -- prefix entity is not the same as the package we are marking. | |
8564 | ||
8565 | if Nkind (Name (Curr)) /= N_Identifier | |
8566 | and then Present (Prefix (Name (Curr))) | |
8567 | and then Entity (Prefix (Name (Curr))) /= Pak | |
8568 | then | |
8569 | Mark_Use_Package (Entity (Prefix (Name (Curr)))); | |
d4810530 | 8570 | |
851e9f19 PMR |
8571 | -- It is also possible to have a child package without a prefix |
8572 | -- that relies on a previous use_package_clause. | |
d4810530 | 8573 | |
851e9f19 PMR |
8574 | elsif Nkind (Name (Curr)) = N_Identifier |
8575 | and then Is_Child_Unit (Entity (Name (Curr))) | |
8576 | then | |
8577 | Mark_Use_Package (Scope (Entity (Name (Curr)))); | |
8578 | end if; | |
d4810530 | 8579 | |
851e9f19 | 8580 | -- Mark the use_package_clause as effective and move up the chain |
d4810530 | 8581 | |
851e9f19 | 8582 | Set_Is_Effective_Use_Clause (Curr); |
d4810530 | 8583 | |
851e9f19 PMR |
8584 | Curr := Prev_Use_Clause (Curr); |
8585 | end loop; | |
8586 | end Mark_Use_Package; | |
d4810530 | 8587 | |
851e9f19 PMR |
8588 | ------------------- |
8589 | -- Mark_Use_Type -- | |
8590 | ------------------- | |
d4810530 | 8591 | |
851e9f19 PMR |
8592 | procedure Mark_Use_Type (E : Entity_Id) is |
8593 | Curr : Node_Id; | |
3e5400f4 | 8594 | Base : Entity_Id; |
d4810530 | 8595 | |
851e9f19 PMR |
8596 | begin |
8597 | -- Ignore void types and unresolved string literals and primitives | |
8598 | ||
8599 | if Nkind (E) = N_String_Literal | |
8600 | or else Nkind (Etype (E)) not in N_Entity | |
8601 | or else not Is_Type (Etype (E)) | |
8602 | then | |
8603 | return; | |
8604 | end if; | |
8605 | ||
3e5400f4 PMR |
8606 | -- Primitives with class-wide operands might additionally render |
8607 | -- their base type's use_clauses effective - so do a recursive check | |
8608 | -- here. | |
8609 | ||
8610 | Base := Base_Type (Etype (E)); | |
8611 | ||
8612 | if Ekind (Base) = E_Class_Wide_Type then | |
8613 | Mark_Use_Type (Base); | |
8614 | end if; | |
8615 | ||
851e9f19 PMR |
8616 | -- The package containing the type or operator function being used |
8617 | -- may be in use as well, so mark any use_package_clauses for it as | |
8618 | -- effective. There are also additional sanity checks performed here | |
8619 | -- for ignoring previous errors. | |
8620 | ||
3e5400f4 | 8621 | Mark_Use_Package (Scope (Base)); |
7f5e671b | 8622 | |
851e9f19 PMR |
8623 | if Nkind (E) in N_Op |
8624 | and then Present (Entity (E)) | |
8625 | and then Present (Scope (Entity (E))) | |
8626 | then | |
8627 | Mark_Use_Package (Scope (Entity (E))); | |
8628 | end if; | |
8629 | ||
3e5400f4 | 8630 | Curr := Current_Use_Clause (Base); |
851e9f19 PMR |
8631 | while Present (Curr) |
8632 | and then not Is_Effective_Use_Clause (Curr) | |
8633 | loop | |
8634 | -- Current use_type_clause may render other use_package_clauses | |
8635 | -- effective. | |
8636 | ||
8637 | if Nkind (Subtype_Mark (Curr)) /= N_Identifier | |
8638 | and then Present (Prefix (Subtype_Mark (Curr))) | |
8639 | then | |
8640 | Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr)))); | |
d4810530 | 8641 | end if; |
d4810530 | 8642 | |
851e9f19 PMR |
8643 | -- Mark the use_type_clause as effective and move up the chain |
8644 | ||
8645 | Set_Is_Effective_Use_Clause (Curr); | |
8646 | ||
8647 | Curr := Prev_Use_Clause (Curr); | |
8648 | end loop; | |
8649 | end Mark_Use_Type; | |
8650 | ||
8651 | -- Start of processing for Mark_Use_Clauses | |
8652 | ||
8653 | begin | |
8654 | -- Use clauses in and of themselves do not count as a "use" of a | |
8655 | -- package. | |
8656 | ||
7f5e671b | 8657 | if Nkind_In (Parent (Id), N_Use_Package_Clause, N_Use_Type_Clause) then |
851e9f19 PMR |
8658 | return; |
8659 | end if; | |
8660 | ||
8661 | -- Handle entities | |
8662 | ||
8663 | if Nkind (Id) in N_Entity then | |
8664 | ||
8665 | -- Mark the entity's package | |
8666 | ||
8667 | if Is_Potentially_Use_Visible (Id) then | |
8668 | Mark_Use_Package (Scope (Id)); | |
d4810530 ES |
8669 | end if; |
8670 | ||
851e9f19 | 8671 | -- Mark enumeration literals |
d4810530 | 8672 | |
851e9f19 PMR |
8673 | if Ekind (Id) = E_Enumeration_Literal then |
8674 | Mark_Use_Type (Id); | |
8675 | ||
8676 | -- Mark primitives | |
8677 | ||
8678 | elsif (Ekind (Id) in Overloadable_Kind | |
7f5e671b PMR |
8679 | or else Ekind_In (Id, E_Generic_Function, |
8680 | E_Generic_Procedure)) | |
851e9f19 | 8681 | and then (Is_Potentially_Use_Visible (Id) |
3e5400f4 PMR |
8682 | or else Is_Intrinsic_Subprogram (Id) |
8683 | or else (Ekind_In (Id, E_Function, E_Procedure) | |
8684 | and then Is_Generic_Actual_Subprogram (Id))) | |
d4810530 | 8685 | then |
851e9f19 PMR |
8686 | Mark_Parameters (Id); |
8687 | end if; | |
d4810530 | 8688 | |
851e9f19 PMR |
8689 | -- Handle nodes |
8690 | ||
8691 | else | |
8692 | -- Mark operators | |
8693 | ||
8694 | if Nkind (Id) in N_Op then | |
8695 | ||
8696 | -- At this point the left operand may not be resolved if we are | |
8697 | -- encountering multiple operators next to eachother in an | |
8698 | -- expression. | |
8699 | ||
8700 | if Nkind (Id) in N_Binary_Op | |
7f5e671b | 8701 | and then not (Nkind (Left_Opnd (Id)) in N_Op) |
851e9f19 PMR |
8702 | then |
8703 | Mark_Use_Type (Left_Opnd (Id)); | |
8704 | end if; | |
8705 | ||
8706 | Mark_Use_Type (Right_Opnd (Id)); | |
8707 | Mark_Use_Type (Id); | |
8708 | ||
8709 | -- Mark entity identifiers | |
8710 | ||
8711 | elsif Nkind (Id) in N_Has_Entity | |
8712 | and then (Is_Potentially_Use_Visible (Entity (Id)) | |
8713 | or else (Is_Generic_Instance (Entity (Id)) | |
8714 | and then Is_Immediately_Visible (Entity (Id)))) | |
8715 | then | |
8716 | -- Ignore fully qualified names as they do not count as a "use" of | |
8717 | -- a package. | |
8718 | ||
8719 | if Nkind_In (Id, N_Identifier, N_Operator_Symbol) | |
8720 | or else (Present (Prefix (Id)) | |
8721 | and then Scope (Entity (Id)) /= Entity (Prefix (Id))) | |
8722 | then | |
20ad0586 | 8723 | Mark_Use_Clauses (Entity (Id)); |
851e9f19 | 8724 | end if; |
d4810530 | 8725 | end if; |
851e9f19 PMR |
8726 | end if; |
8727 | end Mark_Use_Clauses; | |
d4810530 | 8728 | |
851e9f19 PMR |
8729 | -------------------------------- |
8730 | -- Most_Descendant_Use_Clause -- | |
8731 | -------------------------------- | |
ff81221b | 8732 | |
851e9f19 PMR |
8733 | function Most_Descendant_Use_Clause |
8734 | (Clause1 : Entity_Id; | |
8735 | Clause2 : Entity_Id) return Entity_Id | |
8736 | is | |
f9a0fd0b JS |
8737 | Scope1 : Entity_Id; |
8738 | Scope2 : Entity_Id; | |
ff81221b | 8739 | |
851e9f19 PMR |
8740 | begin |
8741 | if Clause1 = Clause2 then | |
8742 | return Clause1; | |
d4810530 ES |
8743 | end if; |
8744 | ||
851e9f19 PMR |
8745 | -- We determine which one is the most descendant by the scope distance |
8746 | -- to the ultimate parent unit. | |
8747 | ||
8748 | Scope1 := Entity_Of_Unit (Unit (Parent (Clause1))); | |
8749 | Scope2 := Entity_Of_Unit (Unit (Parent (Clause2))); | |
8750 | while Scope1 /= Standard_Standard | |
8751 | and then Scope2 /= Standard_Standard | |
8752 | loop | |
8753 | Scope1 := Scope (Scope1); | |
8754 | Scope2 := Scope (Scope2); | |
8755 | ||
8756 | if not Present (Scope1) then | |
8757 | return Clause1; | |
8758 | elsif not Present (Scope2) then | |
8759 | return Clause2; | |
8760 | end if; | |
8761 | end loop; | |
8762 | ||
8763 | if Scope1 = Standard_Standard then | |
8764 | return Clause1; | |
d4810530 | 8765 | end if; |
851e9f19 PMR |
8766 | |
8767 | return Clause2; | |
8768 | end Most_Descendant_Use_Clause; | |
d4810530 | 8769 | |
996ae0b0 RK |
8770 | --------------- |
8771 | -- Pop_Scope -- | |
8772 | --------------- | |
8773 | ||
8774 | procedure Pop_Scope is | |
fbf5a39b | 8775 | SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last); |
fab2daeb | 8776 | S : constant Entity_Id := SST.Entity; |
996ae0b0 RK |
8777 | |
8778 | begin | |
8779 | if Debug_Flag_E then | |
8780 | Write_Info; | |
8781 | end if; | |
8782 | ||
fab2daeb AC |
8783 | -- Set Default_Storage_Pool field of the library unit if necessary |
8784 | ||
8785 | if Ekind_In (S, E_Package, E_Generic_Package) | |
8786 | and then | |
8787 | Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit | |
8788 | then | |
8789 | declare | |
8790 | Aux : constant Node_Id := | |
4adf3c50 | 8791 | Aux_Decls_Node (Parent (Unit_Declaration_Node (S))); |
fab2daeb AC |
8792 | begin |
8793 | if No (Default_Storage_Pool (Aux)) then | |
8794 | Set_Default_Storage_Pool (Aux, Default_Pool); | |
8795 | end if; | |
8796 | end; | |
8797 | end if; | |
8798 | ||
21d27997 | 8799 | Scope_Suppress := SST.Save_Scope_Suppress; |
11560bcc | 8800 | Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top; |
21d27997 | 8801 | Check_Policy_List := SST.Save_Check_Policy_List; |
fab2daeb | 8802 | Default_Pool := SST.Save_Default_Storage_Pool; |
49d41397 | 8803 | No_Tagged_Streams := SST.Save_No_Tagged_Streams; |
43417b90 | 8804 | SPARK_Mode := SST.Save_SPARK_Mode; |
579847c2 | 8805 | SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma; |
220d1fd9 | 8806 | Default_SSO := SST.Save_Default_SSO; |
96e90ac1 | 8807 | Uneval_Old := SST.Save_Uneval_Old; |
996ae0b0 RK |
8808 | |
8809 | if Debug_Flag_W then | |
fab2daeb | 8810 | Write_Str ("<-- exiting scope: "); |
996ae0b0 RK |
8811 | Write_Name (Chars (Current_Scope)); |
8812 | Write_Str (", Depth="); | |
8813 | Write_Int (Int (Scope_Stack.Last)); | |
8814 | Write_Eol; | |
8815 | end if; | |
8816 | ||
fbf5a39b | 8817 | End_Use_Clauses (SST.First_Use_Clause); |
996ae0b0 RK |
8818 | |
8819 | -- If the actions to be wrapped are still there they will get lost | |
8820 | -- causing incomplete code to be generated. It is better to abort in | |
fbf5a39b | 8821 | -- this case (and we do the abort even with assertions off since the |
5eeeed5e | 8822 | -- penalty is incorrect code generation). |
996ae0b0 | 8823 | |
36295779 | 8824 | if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then |
46202729 | 8825 | raise Program_Error; |
fbf5a39b | 8826 | end if; |
996ae0b0 RK |
8827 | |
8828 | -- Free last subprogram name if allocated, and pop scope | |
8829 | ||
fbf5a39b | 8830 | Free (SST.Last_Subprogram_Name); |
996ae0b0 RK |
8831 | Scope_Stack.Decrement_Last; |
8832 | end Pop_Scope; | |
8833 | ||
851e9f19 | 8834 | ---------------- |
fbe627af | 8835 | -- Push_Scope -- |
851e9f19 | 8836 | ---------------- |
fbe627af RD |
8837 | |
8838 | procedure Push_Scope (S : Entity_Id) is | |
fab2daeb | 8839 | E : constant Entity_Id := Scope (S); |
fbe627af RD |
8840 | |
8841 | begin | |
8842 | if Ekind (S) = E_Void then | |
8843 | null; | |
8844 | ||
f7ca1d04 AC |
8845 | -- Set scope depth if not a non-concurrent type, and we have not yet set |
8846 | -- the scope depth. This means that we have the first occurrence of the | |
8847 | -- scope, and this is where the depth is set. | |
fbe627af RD |
8848 | |
8849 | elsif (not Is_Type (S) or else Is_Concurrent_Type (S)) | |
8850 | and then not Scope_Depth_Set (S) | |
8851 | then | |
8852 | if S = Standard_Standard then | |
8853 | Set_Scope_Depth_Value (S, Uint_0); | |
8854 | ||
8855 | elsif Is_Child_Unit (S) then | |
8856 | Set_Scope_Depth_Value (S, Uint_1); | |
8857 | ||
8858 | elsif not Is_Record_Type (Current_Scope) then | |
8859 | if Ekind (S) = E_Loop then | |
8860 | Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope)); | |
8861 | else | |
8862 | Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1); | |
8863 | end if; | |
8864 | end if; | |
8865 | end if; | |
8866 | ||
8867 | Scope_Stack.Increment_Last; | |
8868 | ||
8869 | declare | |
8870 | SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last); | |
8871 | ||
8872 | begin | |
11560bcc TQ |
8873 | SST.Entity := S; |
8874 | SST.Save_Scope_Suppress := Scope_Suppress; | |
8875 | SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top; | |
21d27997 | 8876 | SST.Save_Check_Policy_List := Check_Policy_List; |
fab2daeb | 8877 | SST.Save_Default_Storage_Pool := Default_Pool; |
49d41397 | 8878 | SST.Save_No_Tagged_Streams := No_Tagged_Streams; |
43417b90 | 8879 | SST.Save_SPARK_Mode := SPARK_Mode; |
579847c2 | 8880 | SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma; |
220d1fd9 | 8881 | SST.Save_Default_SSO := Default_SSO; |
96e90ac1 | 8882 | SST.Save_Uneval_Old := Uneval_Old; |
fbe627af | 8883 | |
194d6f3f JS |
8884 | -- Each new scope pushed onto the scope stack inherits the component |
8885 | -- alignment of the previous scope. This emulates the "visibility" | |
8886 | -- semantics of pragma Component_Alignment. | |
8887 | ||
fbe627af | 8888 | if Scope_Stack.Last > Scope_Stack.First then |
5dc203d2 AC |
8889 | SST.Component_Alignment_Default := |
8890 | Scope_Stack.Table | |
64ac53f4 | 8891 | (Scope_Stack.Last - 1).Component_Alignment_Default; |
194d6f3f JS |
8892 | |
8893 | -- Otherwise, this is the first scope being pushed on the scope | |
8894 | -- stack. Inherit the component alignment from the configuration | |
8895 | -- form of pragma Component_Alignment (if any). | |
8896 | ||
8897 | else | |
8898 | SST.Component_Alignment_Default := | |
8899 | Configuration_Component_Alignment; | |
fbe627af RD |
8900 | end if; |
8901 | ||
8902 | SST.Last_Subprogram_Name := null; | |
8903 | SST.Is_Transient := False; | |
8904 | SST.Node_To_Be_Wrapped := Empty; | |
8905 | SST.Pending_Freeze_Actions := No_List; | |
36295779 | 8906 | SST.Actions_To_Be_Wrapped := (others => No_List); |
fbe627af RD |
8907 | SST.First_Use_Clause := Empty; |
8908 | SST.Is_Active_Stack_Base := False; | |
8909 | SST.Previous_Visibility := False; | |
8071b771 | 8910 | SST.Locked_Shared_Objects := No_Elist; |
fbe627af RD |
8911 | end; |
8912 | ||
8913 | if Debug_Flag_W then | |
8914 | Write_Str ("--> new scope: "); | |
8915 | Write_Name (Chars (Current_Scope)); | |
8916 | Write_Str (", Id="); | |
8917 | Write_Int (Int (Current_Scope)); | |
8918 | Write_Str (", Depth="); | |
8919 | Write_Int (Int (Scope_Stack.Last)); | |
8920 | Write_Eol; | |
8921 | end if; | |
8922 | ||
f7ca1d04 AC |
8923 | -- Deal with copying flags from the previous scope to this one. This is |
8924 | -- not necessary if either scope is standard, or if the new scope is a | |
8925 | -- child unit. | |
fbe627af RD |
8926 | |
8927 | if S /= Standard_Standard | |
8928 | and then Scope (S) /= Standard_Standard | |
8929 | and then not Is_Child_Unit (S) | |
8930 | then | |
fbe627af RD |
8931 | if Nkind (E) not in N_Entity then |
8932 | return; | |
8933 | end if; | |
8934 | ||
8935 | -- Copy categorization flags from Scope (S) to S, this is not done | |
8936 | -- when Scope (S) is Standard_Standard since propagation is from | |
8937 | -- library unit entity inwards. Copy other relevant attributes as | |
8938 | -- well (Discard_Names in particular). | |
8939 | ||
8940 | -- We only propagate inwards for library level entities, | |
8941 | -- inner level subprograms do not inherit the categorization. | |
8942 | ||
8943 | if Is_Library_Level_Entity (S) then | |
8944 | Set_Is_Preelaborated (S, Is_Preelaborated (E)); | |
8945 | Set_Is_Shared_Passive (S, Is_Shared_Passive (E)); | |
8946 | Set_Discard_Names (S, Discard_Names (E)); | |
8947 | Set_Suppress_Value_Tracking_On_Call | |
8948 | (S, Suppress_Value_Tracking_On_Call (E)); | |
8949 | Set_Categorization_From_Scope (E => S, Scop => E); | |
8950 | end if; | |
8951 | end if; | |
fab2daeb AC |
8952 | |
8953 | if Is_Child_Unit (S) | |
8954 | and then Present (E) | |
8955 | and then Ekind_In (E, E_Package, E_Generic_Package) | |
8956 | and then | |
8957 | Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit | |
8958 | then | |
8959 | declare | |
8960 | Aux : constant Node_Id := | |
4adf3c50 | 8961 | Aux_Decls_Node (Parent (Unit_Declaration_Node (E))); |
fab2daeb AC |
8962 | begin |
8963 | if Present (Default_Storage_Pool (Aux)) then | |
8964 | Default_Pool := Default_Storage_Pool (Aux); | |
8965 | end if; | |
8966 | end; | |
8967 | end if; | |
fbe627af RD |
8968 | end Push_Scope; |
8969 | ||
996ae0b0 RK |
8970 | --------------------- |
8971 | -- Premature_Usage -- | |
8972 | --------------------- | |
8973 | ||
8974 | procedure Premature_Usage (N : Node_Id) is | |
fbf5a39b | 8975 | Kind : constant Node_Kind := Nkind (Parent (Entity (N))); |
996ae0b0 RK |
8976 | E : Entity_Id := Entity (N); |
8977 | ||
8978 | begin | |
8979 | -- Within an instance, the analysis of the actual for a formal object | |
16ca248a ES |
8980 | -- does not see the name of the object itself. This is significant only |
8981 | -- if the object is an aggregate, where its analysis does not do any | |
8982 | -- name resolution on component associations. (see 4717-008). In such a | |
8983 | -- case, look for the visible homonym on the chain. | |
996ae0b0 | 8984 | |
ac7d724d | 8985 | if In_Instance and then Present (Homonym (E)) then |
996ae0b0 | 8986 | E := Homonym (E); |
ac7d724d | 8987 | while Present (E) and then not In_Open_Scopes (Scope (E)) loop |
996ae0b0 RK |
8988 | E := Homonym (E); |
8989 | end loop; | |
8990 | ||
8991 | if Present (E) then | |
8992 | Set_Entity (N, E); | |
8993 | Set_Etype (N, Etype (E)); | |
8994 | return; | |
8995 | end if; | |
8996 | end if; | |
8997 | ||
8998 | if Kind = N_Component_Declaration then | |
8999 | Error_Msg_N | |
9000 | ("component&! cannot be used before end of record declaration", N); | |
9001 | ||
9002 | elsif Kind = N_Parameter_Specification then | |
9003 | Error_Msg_N | |
9004 | ("formal parameter&! cannot be used before end of specification", | |
9005 | N); | |
9006 | ||
9007 | elsif Kind = N_Discriminant_Specification then | |
9008 | Error_Msg_N | |
9009 | ("discriminant&! cannot be used before end of discriminant part", | |
9010 | N); | |
9011 | ||
9012 | elsif Kind = N_Procedure_Specification | |
9013 | or else Kind = N_Function_Specification | |
9014 | then | |
9015 | Error_Msg_N | |
9016 | ("subprogram&! cannot be used before end of its declaration", | |
9017 | N); | |
90067a15 ES |
9018 | |
9019 | elsif Kind = N_Full_Type_Declaration then | |
9020 | Error_Msg_N | |
9021 | ("type& cannot be used before end of its declaration!", N); | |
9022 | ||
996ae0b0 RK |
9023 | else |
9024 | Error_Msg_N | |
9025 | ("object& cannot be used before end of its declaration!", N); | |
42fe76e0 AC |
9026 | |
9027 | -- If the premature reference appears as the expression in its own | |
9028 | -- declaration, rewrite it to prevent compiler loops in subsequent | |
9029 | -- uses of this mangled declaration in address clauses. | |
9030 | ||
9031 | if Nkind (Parent (N)) = N_Object_Declaration then | |
9032 | Set_Entity (N, Any_Id); | |
9033 | end if; | |
996ae0b0 RK |
9034 | end if; |
9035 | end Premature_Usage; | |
9036 | ||
9037 | ------------------------ | |
9038 | -- Present_System_Aux -- | |
9039 | ------------------------ | |
9040 | ||
9041 | function Present_System_Aux (N : Node_Id := Empty) return Boolean is | |
9042 | Loc : Source_Ptr; | |
fbe627af | 9043 | Aux_Name : Unit_Name_Type; |
996ae0b0 RK |
9044 | Unum : Unit_Number_Type; |
9045 | Withn : Node_Id; | |
9046 | With_Sys : Node_Id; | |
9047 | The_Unit : Node_Id; | |
9048 | ||
9049 | function Find_System (C_Unit : Node_Id) return Entity_Id; | |
16ca248a | 9050 | -- Scan context clause of compilation unit to find with_clause |
996ae0b0 RK |
9051 | -- for System. |
9052 | ||
2e071734 AC |
9053 | ----------------- |
9054 | -- Find_System -- | |
9055 | ----------------- | |
9056 | ||
996ae0b0 RK |
9057 | function Find_System (C_Unit : Node_Id) return Entity_Id is |
9058 | With_Clause : Node_Id; | |
9059 | ||
9060 | begin | |
9061 | With_Clause := First (Context_Items (C_Unit)); | |
996ae0b0 RK |
9062 | while Present (With_Clause) loop |
9063 | if (Nkind (With_Clause) = N_With_Clause | |
9064 | and then Chars (Name (With_Clause)) = Name_System) | |
9065 | and then Comes_From_Source (With_Clause) | |
9066 | then | |
9067 | return With_Clause; | |
9068 | end if; | |
9069 | ||
9070 | Next (With_Clause); | |
9071 | end loop; | |
9072 | ||
9073 | return Empty; | |
9074 | end Find_System; | |
9075 | ||
9076 | -- Start of processing for Present_System_Aux | |
9077 | ||
9078 | begin | |
bc41faa2 | 9079 | -- The child unit may have been loaded and analyzed already |
996ae0b0 RK |
9080 | |
9081 | if Present (System_Aux_Id) then | |
9082 | return True; | |
9083 | ||
9084 | -- If no previous pragma for System.Aux, nothing to load | |
9085 | ||
fbf5a39b | 9086 | elsif No (System_Extend_Unit) then |
996ae0b0 RK |
9087 | return False; |
9088 | ||
9089 | -- Use the unit name given in the pragma to retrieve the unit. | |
9090 | -- Verify that System itself appears in the context clause of the | |
9091 | -- current compilation. If System is not present, an error will | |
9092 | -- have been reported already. | |
9093 | ||
9094 | else | |
9095 | With_Sys := Find_System (Cunit (Current_Sem_Unit)); | |
9096 | ||
9097 | The_Unit := Unit (Cunit (Current_Sem_Unit)); | |
9098 | ||
9099 | if No (With_Sys) | |
294ccb21 RD |
9100 | and then |
9101 | (Nkind (The_Unit) = N_Package_Body | |
ac7d724d ES |
9102 | or else (Nkind (The_Unit) = N_Subprogram_Body |
9103 | and then not Acts_As_Spec (Cunit (Current_Sem_Unit)))) | |
996ae0b0 RK |
9104 | then |
9105 | With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit))); | |
9106 | end if; | |
9107 | ||
ac7d724d ES |
9108 | if No (With_Sys) and then Present (N) then |
9109 | ||
996ae0b0 RK |
9110 | -- If we are compiling a subunit, we need to examine its |
9111 | -- context as well (Current_Sem_Unit is the parent unit); | |
9112 | ||
9113 | The_Unit := Parent (N); | |
996ae0b0 RK |
9114 | while Nkind (The_Unit) /= N_Compilation_Unit loop |
9115 | The_Unit := Parent (The_Unit); | |
9116 | end loop; | |
9117 | ||
9118 | if Nkind (Unit (The_Unit)) = N_Subunit then | |
9119 | With_Sys := Find_System (The_Unit); | |
9120 | end if; | |
9121 | end if; | |
9122 | ||
9123 | if No (With_Sys) then | |
9124 | return False; | |
9125 | end if; | |
9126 | ||
9127 | Loc := Sloc (With_Sys); | |
fbf5a39b | 9128 | Get_Name_String (Chars (Expression (System_Extend_Unit))); |
996ae0b0 RK |
9129 | Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len); |
9130 | Name_Buffer (1 .. 7) := "system."; | |
9131 | Name_Buffer (Name_Len + 8) := '%'; | |
9132 | Name_Buffer (Name_Len + 9) := 's'; | |
9133 | Name_Len := Name_Len + 9; | |
9134 | Aux_Name := Name_Find; | |
9135 | ||
9136 | Unum := | |
9137 | Load_Unit | |
9138 | (Load_Name => Aux_Name, | |
9139 | Required => False, | |
9140 | Subunit => False, | |
9141 | Error_Node => With_Sys); | |
9142 | ||
9143 | if Unum /= No_Unit then | |
9144 | Semantics (Cunit (Unum)); | |
9145 | System_Aux_Id := | |
9146 | Defining_Entity (Specification (Unit (Cunit (Unum)))); | |
9147 | ||
16ca248a ES |
9148 | Withn := |
9149 | Make_With_Clause (Loc, | |
9150 | Name => | |
9151 | Make_Expanded_Name (Loc, | |
94ce4941 HK |
9152 | Chars => Chars (System_Aux_Id), |
9153 | Prefix => | |
9154 | New_Occurrence_Of (Scope (System_Aux_Id), Loc), | |
e4494292 | 9155 | Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc))); |
996ae0b0 RK |
9156 | |
9157 | Set_Entity (Name (Withn), System_Aux_Id); | |
9158 | ||
16ca248a | 9159 | Set_Corresponding_Spec (Withn, System_Aux_Id); |
94ce4941 HK |
9160 | Set_First_Name (Withn); |
9161 | Set_Implicit_With (Withn); | |
9162 | Set_Library_Unit (Withn, Cunit (Unum)); | |
996ae0b0 RK |
9163 | |
9164 | Insert_After (With_Sys, Withn); | |
9165 | Mark_Rewrite_Insertion (Withn); | |
9166 | Set_Context_Installed (Withn); | |
9167 | ||
9168 | return True; | |
9169 | ||
9170 | -- Here if unit load failed | |
9171 | ||
9172 | else | |
9173 | Error_Msg_Name_1 := Name_System; | |
fbf5a39b | 9174 | Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit)); |
996ae0b0 RK |
9175 | Error_Msg_N |
9176 | ("extension package `%.%` does not exist", | |
fbf5a39b | 9177 | Opt.System_Extend_Unit); |
996ae0b0 RK |
9178 | return False; |
9179 | end if; | |
9180 | end if; | |
9181 | end Present_System_Aux; | |
9182 | ||
9183 | ------------------------- | |
9184 | -- Restore_Scope_Stack -- | |
9185 | ------------------------- | |
9186 | ||
e530a2d1 AC |
9187 | procedure Restore_Scope_Stack |
9188 | (List : Elist_Id; | |
9189 | Handle_Use : Boolean := True) | |
9190 | is | |
9191 | SS_Last : constant Int := Scope_Stack.Last; | |
9192 | Elmt : Elmt_Id; | |
996ae0b0 RK |
9193 | |
9194 | begin | |
9e40f163 AC |
9195 | -- Restore visibility of previous scope stack, if any, using the list |
9196 | -- we saved (we use Remove, since this list will not be used again). | |
996ae0b0 | 9197 | |
9e40f163 AC |
9198 | loop |
9199 | Elmt := Last_Elmt (List); | |
9200 | exit when Elmt = No_Elmt; | |
e530a2d1 | 9201 | Set_Is_Immediately_Visible (Node (Elmt)); |
9e40f163 | 9202 | Remove_Last_Elmt (List); |
996ae0b0 | 9203 | end loop; |
fbf5a39b | 9204 | |
9e40f163 AC |
9205 | -- Restore use clauses |
9206 | ||
fbf5a39b AC |
9207 | if SS_Last >= Scope_Stack.First |
9208 | and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard | |
ecad994d | 9209 | and then Handle_Use |
fbf5a39b | 9210 | then |
7f5e671b PMR |
9211 | Install_Use_Clauses |
9212 | (Scope_Stack.Table (SS_Last).First_Use_Clause, | |
9213 | Force_Installation => True); | |
fbf5a39b | 9214 | end if; |
996ae0b0 RK |
9215 | end Restore_Scope_Stack; |
9216 | ||
9217 | ---------------------- | |
9218 | -- Save_Scope_Stack -- | |
9219 | ---------------------- | |
9220 | ||
9e40f163 AC |
9221 | -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid |
9222 | -- consuming any memory. That is, Save_Scope_Stack took care of removing | |
9223 | -- from immediate visibility entities and Restore_Scope_Stack took care | |
9224 | -- of restoring their visibility analyzing the context of each entity. The | |
9225 | -- problem of such approach is that it was fragile and caused unexpected | |
9226 | -- visibility problems, and indeed one test was found where there was a | |
9227 | -- real problem. | |
9228 | ||
9229 | -- Furthermore, the following experiment was carried out: | |
9230 | ||
9231 | -- - Save_Scope_Stack was modified to store in an Elist1 all those | |
9232 | -- entities whose attribute Is_Immediately_Visible is modified | |
9233 | -- from True to False. | |
9234 | ||
9235 | -- - Restore_Scope_Stack was modified to store in another Elist2 | |
9236 | -- all the entities whose attribute Is_Immediately_Visible is | |
9237 | -- modified from False to True. | |
9238 | ||
9239 | -- - Extra code was added to verify that all the elements of Elist1 | |
9240 | -- are found in Elist2 | |
9241 | ||
a35017dc | 9242 | -- This test shows that there may be more occurrences of this problem which |
9e40f163 AC |
9243 | -- have not yet been detected. As a result, we replaced that approach by |
9244 | -- the current one in which Save_Scope_Stack returns the list of entities | |
a35017dc | 9245 | -- whose visibility is changed, and that list is passed to Restore_Scope_ |
9e40f163 AC |
9246 | -- Stack to undo that change. This approach is simpler and safer, although |
9247 | -- it consumes more memory. | |
9248 | ||
e530a2d1 AC |
9249 | function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is |
9250 | Result : constant Elist_Id := New_Elmt_List; | |
996ae0b0 RK |
9251 | E : Entity_Id; |
9252 | S : Entity_Id; | |
9253 | SS_Last : constant Int := Scope_Stack.Last; | |
9254 | ||
e530a2d1 AC |
9255 | procedure Remove_From_Visibility (E : Entity_Id); |
9256 | -- If E is immediately visible then append it to the result and remove | |
85f6a831 RD |
9257 | -- it temporarily from visibility. |
9258 | ||
9259 | ---------------------------- | |
9260 | -- Remove_From_Visibility -- | |
9261 | ---------------------------- | |
9262 | ||
e530a2d1 AC |
9263 | procedure Remove_From_Visibility (E : Entity_Id) is |
9264 | begin | |
9265 | if Is_Immediately_Visible (E) then | |
9266 | Append_Elmt (E, Result); | |
9267 | Set_Is_Immediately_Visible (E, False); | |
9268 | end if; | |
9269 | end Remove_From_Visibility; | |
9270 | ||
85f6a831 RD |
9271 | -- Start of processing for Save_Scope_Stack |
9272 | ||
996ae0b0 RK |
9273 | begin |
9274 | if SS_Last >= Scope_Stack.First | |
9275 | and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard | |
9276 | then | |
ecad994d AC |
9277 | if Handle_Use then |
9278 | End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause); | |
9279 | end if; | |
fbf5a39b | 9280 | |
16ca248a ES |
9281 | -- If the call is from within a compilation unit, as when called from |
9282 | -- Rtsfind, make current entries in scope stack invisible while we | |
9283 | -- analyze the new unit. | |
996ae0b0 RK |
9284 | |
9285 | for J in reverse 0 .. SS_Last loop | |
9286 | exit when Scope_Stack.Table (J).Entity = Standard_Standard | |
9287 | or else No (Scope_Stack.Table (J).Entity); | |
9288 | ||
9289 | S := Scope_Stack.Table (J).Entity; | |
e530a2d1 AC |
9290 | |
9291 | Remove_From_Visibility (S); | |
996ae0b0 | 9292 | |
16ca248a | 9293 | E := First_Entity (S); |
996ae0b0 | 9294 | while Present (E) loop |
e530a2d1 | 9295 | Remove_From_Visibility (E); |
996ae0b0 RK |
9296 | Next_Entity (E); |
9297 | end loop; | |
9298 | end loop; | |
9299 | ||
9300 | end if; | |
e530a2d1 AC |
9301 | |
9302 | return Result; | |
996ae0b0 RK |
9303 | end Save_Scope_Stack; |
9304 | ||
9305 | ------------- | |
9306 | -- Set_Use -- | |
9307 | ------------- | |
9308 | ||
9309 | procedure Set_Use (L : List_Id) is | |
851e9f19 | 9310 | Decl : Node_Id; |
996ae0b0 RK |
9311 | |
9312 | begin | |
9313 | if Present (L) then | |
9314 | Decl := First (L); | |
996ae0b0 RK |
9315 | while Present (Decl) loop |
9316 | if Nkind (Decl) = N_Use_Package_Clause then | |
9317 | Chain_Use_Clause (Decl); | |
851e9f19 PMR |
9318 | Use_One_Package (Decl, Name (Decl)); |
9319 | ||
9320 | elsif Nkind (Decl) = N_Use_Type_Clause then | |
9321 | Chain_Use_Clause (Decl); | |
9322 | Use_One_Type (Subtype_Mark (Decl)); | |
9323 | ||
9324 | end if; | |
9325 | ||
9326 | Next (Decl); | |
9327 | end loop; | |
9328 | end if; | |
9329 | end Set_Use; | |
9330 | ||
9331 | ----------------------------- | |
9332 | -- Update_Use_Clause_Chain -- | |
9333 | ----------------------------- | |
9334 | ||
9335 | procedure Update_Use_Clause_Chain is | |
6b199a18 | 9336 | |
851e9f19 PMR |
9337 | procedure Update_Chain_In_Scope (Level : Int); |
9338 | -- Iterate through one level in the scope stack verifying each use-type | |
9339 | -- clause within said level is used then reset the Current_Use_Clause | |
9340 | -- to a redundant use clause outside of the current ending scope if such | |
9341 | -- a clause exists. | |
9342 | ||
9343 | --------------------------- | |
9344 | -- Update_Chain_In_Scope -- | |
9345 | --------------------------- | |
9346 | ||
9347 | procedure Update_Chain_In_Scope (Level : Int) is | |
9348 | Curr : Node_Id; | |
9349 | N : Node_Id; | |
9350 | ||
9351 | begin | |
9352 | -- Loop through all use clauses within the scope dictated by Level | |
9353 | ||
9354 | Curr := Scope_Stack.Table (Level).First_Use_Clause; | |
9355 | while Present (Curr) loop | |
9356 | ||
9357 | -- Retrieve the subtype mark or name within the current current | |
9358 | -- use clause. | |
9359 | ||
9360 | if Nkind (Curr) = N_Use_Type_Clause then | |
9361 | N := Subtype_Mark (Curr); | |
9362 | else | |
9363 | N := Name (Curr); | |
9364 | end if; | |
9365 | ||
9366 | -- If warnings for unreferenced entities are enabled and the | |
9367 | -- current use clause has not been marked effective. | |
996ae0b0 | 9368 | |
851e9f19 PMR |
9369 | if Check_Unreferenced |
9370 | and then Comes_From_Source (Curr) | |
9371 | and then not Is_Effective_Use_Clause (Curr) | |
9372 | and then not In_Instance | |
e5148da0 | 9373 | and then not In_Inlined_Body |
851e9f19 | 9374 | then |
851e9f19 PMR |
9375 | -- We are dealing with a potentially unused use_package_clause |
9376 | ||
9377 | if Nkind (Curr) = N_Use_Package_Clause then | |
996ae0b0 | 9378 | |
851e9f19 | 9379 | -- Renamings and formal subprograms may cause the associated |
6b199a18 | 9380 | -- node to be marked as effective instead of the original. |
851e9f19 PMR |
9381 | |
9382 | if not (Present (Associated_Node (N)) | |
9383 | and then Present | |
7f5e671b PMR |
9384 | (Current_Use_Clause |
9385 | (Associated_Node (N))) | |
851e9f19 | 9386 | and then Is_Effective_Use_Clause |
7f5e671b PMR |
9387 | (Current_Use_Clause |
9388 | (Associated_Node (N)))) | |
996ae0b0 | 9389 | then |
851e9f19 | 9390 | Error_Msg_Node_1 := Entity (N); |
7f5e671b | 9391 | Error_Msg_NE |
8f8f531f | 9392 | ("use clause for package & has no effect?u?", |
7f5e671b | 9393 | Curr, Entity (N)); |
996ae0b0 RK |
9394 | end if; |
9395 | ||
851e9f19 | 9396 | -- We are dealing with an unused use_type_clause |
996ae0b0 | 9397 | |
851e9f19 PMR |
9398 | else |
9399 | Error_Msg_Node_1 := Etype (N); | |
7f5e671b | 9400 | Error_Msg_NE |
8f8f531f | 9401 | ("use clause for } has no effect?u?", Curr, Etype (N)); |
851e9f19 PMR |
9402 | end if; |
9403 | end if; | |
996ae0b0 | 9404 | |
851e9f19 PMR |
9405 | -- Verify that we haven't already processed a redundant |
9406 | -- use_type_clause within the same scope before we move the | |
9407 | -- current use clause up to a previous one for type T. | |
996ae0b0 | 9408 | |
851e9f19 PMR |
9409 | if Present (Prev_Use_Clause (Curr)) then |
9410 | Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr)); | |
996ae0b0 RK |
9411 | end if; |
9412 | ||
851e9f19 | 9413 | Curr := Next_Use_Clause (Curr); |
996ae0b0 | 9414 | end loop; |
851e9f19 PMR |
9415 | end Update_Chain_In_Scope; |
9416 | ||
9417 | -- Start of processing for Update_Use_Clause_Chain | |
9418 | ||
9419 | begin | |
9420 | Update_Chain_In_Scope (Scope_Stack.Last); | |
9421 | ||
9422 | -- Deal with use clauses within the context area if the current | |
9423 | -- scope is a compilation unit. | |
9424 | ||
c840bf9b PMR |
9425 | if Is_Compilation_Unit (Current_Scope) |
9426 | and then Sloc (Scope_Stack.Table | |
9427 | (Scope_Stack.Last - 1).Entity) = Standard_Location | |
9428 | then | |
851e9f19 | 9429 | Update_Chain_In_Scope (Scope_Stack.Last - 1); |
996ae0b0 | 9430 | end if; |
851e9f19 | 9431 | end Update_Use_Clause_Chain; |
996ae0b0 RK |
9432 | |
9433 | --------------------- | |
9434 | -- Use_One_Package -- | |
9435 | --------------------- | |
9436 | ||
851e9f19 PMR |
9437 | procedure Use_One_Package |
9438 | (N : Node_Id; | |
9439 | Pack_Name : Entity_Id := Empty; | |
9440 | Force : Boolean := False) | |
9441 | is | |
851e9f19 PMR |
9442 | procedure Note_Redundant_Use (Clause : Node_Id); |
9443 | -- Mark the name in a use clause as redundant if the corresponding | |
9444 | -- entity is already use-visible. Emit a warning if the use clause comes | |
9445 | -- from source and the proper warnings are enabled. | |
9446 | ||
9447 | ------------------------ | |
9448 | -- Note_Redundant_Use -- | |
9449 | ------------------------ | |
9450 | ||
9451 | procedure Note_Redundant_Use (Clause : Node_Id) is | |
851e9f19 | 9452 | Decl : constant Node_Id := Parent (Clause); |
7f5e671b | 9453 | Pack_Name : constant Entity_Id := Entity (Clause); |
851e9f19 PMR |
9454 | |
9455 | Cur_Use : Node_Id := Current_Use_Clause (Pack_Name); | |
9456 | Prev_Use : Node_Id := Empty; | |
9457 | Redundant : Node_Id := Empty; | |
9458 | -- The Use_Clause which is actually redundant. In the simplest case | |
9459 | -- it is Pack itself, but when we compile a body we install its | |
9460 | -- context before that of its spec, in which case it is the | |
9461 | -- use_clause in the spec that will appear to be redundant, and we | |
9462 | -- want the warning to be placed on the body. Similar complications | |
9463 | -- appear when the redundancy is between a child unit and one of its | |
9464 | -- ancestors. | |
9465 | ||
9466 | begin | |
9467 | -- Could be renamed... | |
9468 | ||
9469 | if No (Cur_Use) then | |
9470 | Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name)); | |
9471 | end if; | |
9472 | ||
9473 | Set_Redundant_Use (Clause, True); | |
9474 | ||
9475 | if not Comes_From_Source (Clause) | |
9476 | or else In_Instance | |
9477 | or else not Warn_On_Redundant_Constructs | |
9478 | then | |
9479 | return; | |
9480 | end if; | |
9481 | ||
9482 | if not Is_Compilation_Unit (Current_Scope) then | |
9483 | ||
9484 | -- If the use_clause is in an inner scope, it is made redundant by | |
9485 | -- some clause in the current context, with one exception: If we | |
9486 | -- are compiling a nested package body, and the use_clause comes | |
9487 | -- from then corresponding spec, the clause is not necessarily | |
9488 | -- fully redundant, so we should not warn. If a warning was | |
9489 | -- warranted, it would have been given when the spec was | |
9490 | -- processed. | |
9491 | ||
9492 | if Nkind (Parent (Decl)) = N_Package_Specification then | |
9493 | declare | |
9494 | Package_Spec_Entity : constant Entity_Id := | |
9495 | Defining_Unit_Name (Parent (Decl)); | |
9496 | begin | |
9497 | if In_Package_Body (Package_Spec_Entity) then | |
9498 | return; | |
9499 | end if; | |
9500 | end; | |
9501 | end if; | |
9502 | ||
9503 | Redundant := Clause; | |
9504 | Prev_Use := Cur_Use; | |
9505 | ||
9506 | elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then | |
9507 | declare | |
9508 | Cur_Unit : constant Unit_Number_Type := | |
7f5e671b | 9509 | Get_Source_Unit (Cur_Use); |
851e9f19 | 9510 | New_Unit : constant Unit_Number_Type := |
7f5e671b PMR |
9511 | Get_Source_Unit (Clause); |
9512 | ||
9513 | Scop : Entity_Id; | |
851e9f19 PMR |
9514 | |
9515 | begin | |
9516 | if Cur_Unit = New_Unit then | |
9517 | ||
9518 | -- Redundant clause in same body | |
9519 | ||
9520 | Redundant := Clause; | |
9521 | Prev_Use := Cur_Use; | |
9522 | ||
9523 | elsif Cur_Unit = Current_Sem_Unit then | |
9524 | ||
9525 | -- If the new clause is not in the current unit it has been | |
9526 | -- analyzed first, and it makes the other one redundant. | |
9527 | -- However, if the new clause appears in a subunit, Cur_Unit | |
9528 | -- is still the parent, and in that case the redundant one | |
9529 | -- is the one appearing in the subunit. | |
9530 | ||
9531 | if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then | |
9532 | Redundant := Clause; | |
9533 | Prev_Use := Cur_Use; | |
9534 | ||
7f5e671b PMR |
9535 | -- Most common case: redundant clause in body, original |
9536 | -- clause in spec. Current scope is spec entity. | |
851e9f19 PMR |
9537 | |
9538 | elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then | |
9539 | Redundant := Cur_Use; | |
9540 | Prev_Use := Clause; | |
9541 | ||
9542 | else | |
9543 | -- The new clause may appear in an unrelated unit, when | |
9544 | -- the parents of a generic are being installed prior to | |
9545 | -- instantiation. In this case there must be no warning. | |
9546 | -- We detect this case by checking whether the current | |
9547 | -- top of the stack is related to the current | |
9548 | -- compilation. | |
9549 | ||
9550 | Scop := Current_Scope; | |
9551 | while Present (Scop) | |
9552 | and then Scop /= Standard_Standard | |
9553 | loop | |
9554 | if Is_Compilation_Unit (Scop) | |
9555 | and then not Is_Child_Unit (Scop) | |
9556 | then | |
9557 | return; | |
9558 | ||
9559 | elsif Scop = Cunit_Entity (Current_Sem_Unit) then | |
9560 | exit; | |
9561 | end if; | |
9562 | ||
9563 | Scop := Scope (Scop); | |
9564 | end loop; | |
9565 | ||
9566 | Redundant := Cur_Use; | |
9567 | Prev_Use := Clause; | |
9568 | end if; | |
9569 | ||
9570 | elsif New_Unit = Current_Sem_Unit then | |
9571 | Redundant := Clause; | |
9572 | Prev_Use := Cur_Use; | |
9573 | ||
9574 | else | |
9575 | -- Neither is the current unit, so they appear in parent or | |
9576 | -- sibling units. Warning will be emitted elsewhere. | |
9577 | ||
9578 | return; | |
9579 | end if; | |
9580 | end; | |
9581 | ||
9582 | elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration | |
9583 | and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit)))) | |
9584 | then | |
9585 | -- Use_clause is in child unit of current unit, and the child unit | |
9586 | -- appears in the context of the body of the parent, so it has | |
9587 | -- been installed first, even though it is the redundant one. | |
9588 | -- Depending on their placement in the context, the visible or the | |
9589 | -- private parts of the two units, either might appear as | |
9590 | -- redundant, but the message has to be on the current unit. | |
9591 | ||
9592 | if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then | |
9593 | Redundant := Cur_Use; | |
9594 | Prev_Use := Clause; | |
9595 | else | |
9596 | Redundant := Clause; | |
9597 | Prev_Use := Cur_Use; | |
9598 | end if; | |
9599 | ||
9600 | -- If the new use clause appears in the private part of a parent | |
9601 | -- unit it may appear to be redundant w.r.t. a use clause in a | |
9602 | -- child unit, but the previous use clause was needed in the | |
9603 | -- visible part of the child, and no warning should be emitted. | |
9604 | ||
9605 | if Nkind (Parent (Decl)) = N_Package_Specification | |
7f5e671b PMR |
9606 | and then List_Containing (Decl) = |
9607 | Private_Declarations (Parent (Decl)) | |
851e9f19 PMR |
9608 | then |
9609 | declare | |
9610 | Par : constant Entity_Id := Defining_Entity (Parent (Decl)); | |
9611 | Spec : constant Node_Id := | |
9612 | Specification (Unit (Cunit (Current_Sem_Unit))); | |
9613 | ||
9614 | begin | |
9615 | if Is_Compilation_Unit (Par) | |
9616 | and then Par /= Cunit_Entity (Current_Sem_Unit) | |
9617 | and then Parent (Cur_Use) = Spec | |
7f5e671b PMR |
9618 | and then List_Containing (Cur_Use) = |
9619 | Visible_Declarations (Spec) | |
851e9f19 PMR |
9620 | then |
9621 | return; | |
9622 | end if; | |
9623 | end; | |
9624 | end if; | |
9625 | ||
7f5e671b PMR |
9626 | -- Finally, if the current use clause is in the context then the |
9627 | -- clause is redundant when it is nested within the unit. | |
851e9f19 PMR |
9628 | |
9629 | elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit | |
9630 | and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit | |
9631 | and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause) | |
9632 | then | |
9633 | Redundant := Clause; | |
9634 | Prev_Use := Cur_Use; | |
9635 | ||
9636 | end if; | |
9637 | ||
5b1265cc | 9638 | if Present (Redundant) and then Parent (Redundant) /= Prev_Use then |
7f5e671b | 9639 | |
851e9f19 PMR |
9640 | -- Make sure we are looking at most-descendant use_package_clause |
9641 | -- by traversing the chain with Find_Most_Prev and then verifying | |
9642 | -- there is no scope manipulation via Most_Descendant_Use_Clause. | |
9643 | ||
9644 | if Nkind (Prev_Use) = N_Use_Package_Clause | |
9645 | and then | |
9646 | (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit | |
9647 | or else Most_Descendant_Use_Clause | |
7f5e671b | 9648 | (Prev_Use, Find_Most_Prev (Prev_Use)) /= Prev_Use) |
851e9f19 PMR |
9649 | then |
9650 | Prev_Use := Find_Most_Prev (Prev_Use); | |
9651 | end if; | |
9652 | ||
9653 | Error_Msg_Sloc := Sloc (Prev_Use); | |
9654 | Error_Msg_NE -- CODEFIX | |
f192ca5e | 9655 | ("& is already use-visible through previous use_clause #??", |
851e9f19 PMR |
9656 | Redundant, Pack_Name); |
9657 | end if; | |
9658 | end Note_Redundant_Use; | |
9659 | ||
9660 | -- Local variables | |
9661 | ||
7f5e671b | 9662 | Current_Instance : Entity_Id := Empty; |
996ae0b0 | 9663 | Id : Entity_Id; |
7f5e671b | 9664 | P : Entity_Id; |
996ae0b0 | 9665 | Prev : Entity_Id; |
9bc856dd | 9666 | Private_With_OK : Boolean := False; |
7f5e671b | 9667 | Real_P : Entity_Id; |
851e9f19 PMR |
9668 | |
9669 | -- Start of processing for Use_One_Package | |
996ae0b0 RK |
9670 | |
9671 | begin | |
851e9f19 PMR |
9672 | -- Use_One_Package may have been called recursively to handle an |
9673 | -- implicit use for a auxiliary system package, so set P accordingly | |
9674 | -- and skip redundancy checks. | |
9675 | ||
9676 | if No (Pack_Name) and then Present_System_Aux (N) then | |
9677 | P := System_Aux_Id; | |
9678 | ||
9679 | -- Check for redundant use_package_clauses | |
9680 | ||
9681 | else | |
9682 | -- Ignore cases where we are dealing with a non user defined package | |
9683 | -- like Standard_Standard or something other than a valid package. | |
9684 | ||
9685 | if not Is_Entity_Name (Pack_Name) | |
9686 | or else No (Entity (Pack_Name)) | |
9687 | or else Ekind (Entity (Pack_Name)) /= E_Package | |
9688 | then | |
9689 | return; | |
9690 | end if; | |
9691 | ||
9692 | -- When a renaming exists we must check it for redundancy. The | |
9693 | -- original package would have already been seen at this point. | |
9694 | ||
9695 | if Present (Renamed_Object (Entity (Pack_Name))) then | |
9696 | P := Renamed_Object (Entity (Pack_Name)); | |
9697 | else | |
9698 | P := Entity (Pack_Name); | |
9699 | end if; | |
9700 | ||
9701 | -- Check for redundant clauses then set the current use clause for | |
9702 | -- P if were are not "forcing" an installation from a scope | |
9703 | -- reinstallation that is done throughout analysis for various | |
9704 | -- reasons. | |
9705 | ||
9706 | if In_Use (P) then | |
9707 | Note_Redundant_Use (Pack_Name); | |
7f5e671b | 9708 | |
851e9f19 PMR |
9709 | if not Force then |
9710 | Set_Current_Use_Clause (P, N); | |
9711 | end if; | |
7f5e671b | 9712 | |
851e9f19 PMR |
9713 | return; |
9714 | ||
9715 | -- Warn about detected redundant clauses | |
9716 | ||
614bc51c PMR |
9717 | elsif not Force |
9718 | and then In_Open_Scopes (P) | |
9719 | and then not Is_Hidden_Open_Scope (P) | |
9720 | then | |
851e9f19 PMR |
9721 | if Warn_On_Redundant_Constructs and then P = Current_Scope then |
9722 | Error_Msg_NE -- CODEFIX | |
9723 | ("& is already use-visible within itself?r?", | |
9724 | Pack_Name, P); | |
9725 | end if; | |
7f5e671b | 9726 | |
851e9f19 PMR |
9727 | return; |
9728 | end if; | |
9729 | ||
9730 | -- Set P back to the non-renamed package so that visiblilty of the | |
9731 | -- entities within the package can be properly set below. | |
9732 | ||
9733 | P := Entity (Pack_Name); | |
996ae0b0 RK |
9734 | end if; |
9735 | ||
9736 | Set_In_Use (P); | |
d4810530 | 9737 | Set_Current_Use_Clause (P, N); |
996ae0b0 | 9738 | |
0ab80019 | 9739 | -- Ada 2005 (AI-50217): Check restriction |
19f0526a | 9740 | |
7b56a91b | 9741 | if From_Limited_With (P) then |
657a9dd9 | 9742 | Error_Msg_N ("limited withed package cannot appear in use clause", N); |
996ae0b0 RK |
9743 | end if; |
9744 | ||
bc41faa2 | 9745 | -- Find enclosing instance, if any |
996ae0b0 RK |
9746 | |
9747 | if In_Instance then | |
9748 | Current_Instance := Current_Scope; | |
996ae0b0 RK |
9749 | while not Is_Generic_Instance (Current_Instance) loop |
9750 | Current_Instance := Scope (Current_Instance); | |
9751 | end loop; | |
9752 | ||
9753 | if No (Hidden_By_Use_Clause (N)) then | |
9754 | Set_Hidden_By_Use_Clause (N, New_Elmt_List); | |
9755 | end if; | |
9756 | end if; | |
9757 | ||
7f5e671b PMR |
9758 | -- If unit is a package renaming, indicate that the renamed package is |
9759 | -- also in use (the flags on both entities must remain consistent, and a | |
9760 | -- subsequent use of either of them should be recognized as redundant). | |
996ae0b0 RK |
9761 | |
9762 | if Present (Renamed_Object (P)) then | |
9763 | Set_In_Use (Renamed_Object (P)); | |
d4810530 | 9764 | Set_Current_Use_Clause (Renamed_Object (P), N); |
996ae0b0 RK |
9765 | Real_P := Renamed_Object (P); |
9766 | else | |
9767 | Real_P := P; | |
9768 | end if; | |
9769 | ||
0ab80019 | 9770 | -- Ada 2005 (AI-262): Check the use_clause of a private withed package |
9bc856dd AC |
9771 | -- found in the private part of a package specification |
9772 | ||
9773 | if In_Private_Part (Current_Scope) | |
9774 | and then Has_Private_With (P) | |
9775 | and then Is_Child_Unit (Current_Scope) | |
9776 | and then Is_Child_Unit (P) | |
9777 | and then Is_Ancestor_Package (Scope (Current_Scope), P) | |
9778 | then | |
9779 | Private_With_OK := True; | |
9780 | end if; | |
9781 | ||
996ae0b0 RK |
9782 | -- Loop through entities in one package making them potentially |
9783 | -- use-visible. | |
9784 | ||
9785 | Id := First_Entity (P); | |
9786 | while Present (Id) | |
9bc856dd | 9787 | and then (Id /= First_Private_Entity (P) |
ac7d724d | 9788 | or else Private_With_OK) -- Ada 2005 (AI-262) |
996ae0b0 RK |
9789 | loop |
9790 | Prev := Current_Entity (Id); | |
996ae0b0 RK |
9791 | while Present (Prev) loop |
9792 | if Is_Immediately_Visible (Prev) | |
9793 | and then (not Is_Overloadable (Prev) | |
9794 | or else not Is_Overloadable (Id) | |
9795 | or else (Type_Conformant (Id, Prev))) | |
9796 | then | |
9797 | if No (Current_Instance) then | |
9798 | ||
9799 | -- Potentially use-visible entity remains hidden | |
9800 | ||
9801 | goto Next_Usable_Entity; | |
9802 | ||
16ca248a ES |
9803 | -- A use clause within an instance hides outer global entities, |
9804 | -- which are not used to resolve local entities in the | |
9805 | -- instance. Note that the predefined entities in Standard | |
9806 | -- could not have been hidden in the generic by a use clause, | |
9807 | -- and therefore remain visible. Other compilation units whose | |
9808 | -- entities appear in Standard must be hidden in an instance. | |
996ae0b0 RK |
9809 | |
9810 | -- To determine whether an entity is external to the instance | |
9811 | -- we compare the scope depth of its scope with that of the | |
9812 | -- current instance. However, a generic actual of a subprogram | |
9813 | -- instance is declared in the wrapper package but will not be | |
e074d476 AC |
9814 | -- hidden by a use-visible entity. similarly, an entity that is |
9815 | -- declared in an enclosing instance will not be hidden by an | |
9816 | -- an entity declared in a generic actual, which can only have | |
9817 | -- been use-visible in the generic and will not have hidden the | |
9818 | -- entity in the generic parent. | |
996ae0b0 | 9819 | |
82c80734 RD |
9820 | -- If Id is called Standard, the predefined package with the |
9821 | -- same name is in the homonym chain. It has to be ignored | |
9822 | -- because it has no defined scope (being the only entity in | |
9823 | -- the system with this mandated behavior). | |
9824 | ||
996ae0b0 | 9825 | elsif not Is_Hidden (Id) |
82c80734 | 9826 | and then Present (Scope (Prev)) |
996ae0b0 RK |
9827 | and then not Is_Wrapper_Package (Scope (Prev)) |
9828 | and then Scope_Depth (Scope (Prev)) < | |
9829 | Scope_Depth (Current_Instance) | |
9830 | and then (Scope (Prev) /= Standard_Standard | |
9831 | or else Sloc (Prev) > Standard_Location) | |
9832 | then | |
e074d476 AC |
9833 | if In_Open_Scopes (Scope (Prev)) |
9834 | and then Is_Generic_Instance (Scope (Prev)) | |
c94a0b9d AC |
9835 | and then Present (Associated_Formal_Package (P)) |
9836 | then | |
9837 | null; | |
9838 | ||
9839 | else | |
9840 | Set_Is_Potentially_Use_Visible (Id); | |
9841 | Set_Is_Immediately_Visible (Prev, False); | |
9842 | Append_Elmt (Prev, Hidden_By_Use_Clause (N)); | |
9843 | end if; | |
996ae0b0 RK |
9844 | end if; |
9845 | ||
16ca248a ES |
9846 | -- A user-defined operator is not use-visible if the predefined |
9847 | -- operator for the type is immediately visible, which is the case | |
9848 | -- if the type of the operand is in an open scope. This does not | |
9849 | -- apply to user-defined operators that have operands of different | |
9850 | -- types, because the predefined mixed mode operations (multiply | |
9851 | -- and divide) apply to universal types and do not hide anything. | |
996ae0b0 RK |
9852 | |
9853 | elsif Ekind (Prev) = E_Operator | |
9854 | and then Operator_Matches_Spec (Prev, Id) | |
9855 | and then In_Open_Scopes | |
ac7d724d | 9856 | (Scope (Base_Type (Etype (First_Formal (Id))))) |
996ae0b0 | 9857 | and then (No (Next_Formal (First_Formal (Id))) |
ac7d724d ES |
9858 | or else Etype (First_Formal (Id)) = |
9859 | Etype (Next_Formal (First_Formal (Id))) | |
996ae0b0 RK |
9860 | or else Chars (Prev) = Name_Op_Expon) |
9861 | then | |
9862 | goto Next_Usable_Entity; | |
e69614ad AC |
9863 | |
9864 | -- In an instance, two homonyms may become use_visible through the | |
9865 | -- actuals of distinct formal packages. In the generic, only the | |
9866 | -- current one would have been visible, so make the other one | |
9867 | -- not use_visible. | |
9868 | ||
b7c34dff JS |
9869 | -- In certain pathological cases it is possible that unrelated |
9870 | -- homonyms from distinct formal packages may exist in an | |
9871 | -- uninstalled scope. We must test for that here. | |
9872 | ||
e69614ad AC |
9873 | elsif Present (Current_Instance) |
9874 | and then Is_Potentially_Use_Visible (Prev) | |
9875 | and then not Is_Overloadable (Prev) | |
9876 | and then Scope (Id) /= Scope (Prev) | |
9877 | and then Used_As_Generic_Actual (Scope (Prev)) | |
9878 | and then Used_As_Generic_Actual (Scope (Id)) | |
b7c34dff | 9879 | and then Is_List_Member (Scope (Prev)) |
30196a76 RD |
9880 | and then not In_Same_List (Current_Use_Clause (Scope (Prev)), |
9881 | Current_Use_Clause (Scope (Id))) | |
e69614ad AC |
9882 | then |
9883 | Set_Is_Potentially_Use_Visible (Prev, False); | |
9884 | Append_Elmt (Prev, Hidden_By_Use_Clause (N)); | |
996ae0b0 RK |
9885 | end if; |
9886 | ||
9887 | Prev := Homonym (Prev); | |
9888 | end loop; | |
9889 | ||
bc41faa2 | 9890 | -- On exit, we know entity is not hidden, unless it is private |
996ae0b0 RK |
9891 | |
9892 | if not Is_Hidden (Id) | |
ac7d724d | 9893 | and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id)) |
996ae0b0 RK |
9894 | then |
9895 | Set_Is_Potentially_Use_Visible (Id); | |
9896 | ||
ac7d724d | 9897 | if Is_Private_Type (Id) and then Present (Full_View (Id)) then |
996ae0b0 RK |
9898 | Set_Is_Potentially_Use_Visible (Full_View (Id)); |
9899 | end if; | |
9900 | end if; | |
9901 | ||
9902 | <<Next_Usable_Entity>> | |
9903 | Next_Entity (Id); | |
9904 | end loop; | |
9905 | ||
16ca248a ES |
9906 | -- Child units are also made use-visible by a use clause, but they may |
9907 | -- appear after all visible declarations in the parent entity list. | |
996ae0b0 RK |
9908 | |
9909 | while Present (Id) loop | |
8398e82e | 9910 | if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then |
996ae0b0 RK |
9911 | Set_Is_Potentially_Use_Visible (Id); |
9912 | end if; | |
9913 | ||
9914 | Next_Entity (Id); | |
9915 | end loop; | |
9916 | ||
9917 | if Chars (Real_P) = Name_System | |
9918 | and then Scope (Real_P) = Standard_Standard | |
9919 | and then Present_System_Aux (N) | |
9920 | then | |
851e9f19 | 9921 | Use_One_Package (N); |
996ae0b0 | 9922 | end if; |
996ae0b0 RK |
9923 | end Use_One_Package; |
9924 | ||
9925 | ------------------ | |
9926 | -- Use_One_Type -- | |
9927 | ------------------ | |
9928 | ||
851e9f19 | 9929 | procedure Use_One_Type |
7f5e671b PMR |
9930 | (Id : Node_Id; |
9931 | Installed : Boolean := False; | |
9932 | Force : Boolean := False) | |
851e9f19 | 9933 | is |
954c111a HK |
9934 | function Spec_Reloaded_For_Body return Boolean; |
9935 | -- Determine whether the compilation unit is a package body and the use | |
9936 | -- type clause is in the spec of the same package. Even though the spec | |
9937 | -- was analyzed first, its context is reloaded when analysing the body. | |
9938 | ||
29efbb8c ES |
9939 | procedure Use_Class_Wide_Operations (Typ : Entity_Id); |
9940 | -- AI05-150: if the use_type_clause carries the "all" qualifier, | |
9941 | -- class-wide operations of ancestor types are use-visible if the | |
9942 | -- ancestor type is visible. | |
9943 | ||
954c111a HK |
9944 | ---------------------------- |
9945 | -- Spec_Reloaded_For_Body -- | |
9946 | ---------------------------- | |
9947 | ||
9948 | function Spec_Reloaded_For_Body return Boolean is | |
9949 | begin | |
9950 | if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then | |
9951 | declare | |
9952 | Spec : constant Node_Id := | |
9953 | Parent (List_Containing (Parent (Id))); | |
f146302c | 9954 | |
d99ff0f4 | 9955 | begin |
f146302c AC |
9956 | -- Check whether type is declared in a package specification, |
9957 | -- and current unit is the corresponding package body. The | |
9958 | -- use clauses themselves may be within a nested package. | |
9959 | ||
954c111a HK |
9960 | return |
9961 | Nkind (Spec) = N_Package_Specification | |
7f5e671b PMR |
9962 | and then In_Same_Source_Unit |
9963 | (Corresponding_Body (Parent (Spec)), | |
9964 | Cunit_Entity (Current_Sem_Unit)); | |
954c111a HK |
9965 | end; |
9966 | end if; | |
9967 | ||
9968 | return False; | |
9969 | end Spec_Reloaded_For_Body; | |
9970 | ||
29efbb8c ES |
9971 | ------------------------------- |
9972 | -- Use_Class_Wide_Operations -- | |
9973 | ------------------------------- | |
9974 | ||
9975 | procedure Use_Class_Wide_Operations (Typ : Entity_Id) is | |
29efbb8c | 9976 | function Is_Class_Wide_Operation_Of |
851e9f19 PMR |
9977 | (Op : Entity_Id; |
9978 | T : Entity_Id) return Boolean; | |
29efbb8c ES |
9979 | -- Determine whether a subprogram has a class-wide parameter or |
9980 | -- result that is T'Class. | |
9981 | ||
9982 | --------------------------------- | |
9983 | -- Is_Class_Wide_Operation_Of -- | |
9984 | --------------------------------- | |
9985 | ||
9986 | function Is_Class_Wide_Operation_Of | |
7f5e671b PMR |
9987 | (Op : Entity_Id; |
9988 | T : Entity_Id) return Boolean | |
29efbb8c ES |
9989 | is |
9990 | Formal : Entity_Id; | |
9991 | ||
9992 | begin | |
9993 | Formal := First_Formal (Op); | |
9994 | while Present (Formal) loop | |
9995 | if Etype (Formal) = Class_Wide_Type (T) then | |
9996 | return True; | |
9997 | end if; | |
7f5e671b | 9998 | |
29efbb8c ES |
9999 | Next_Formal (Formal); |
10000 | end loop; | |
10001 | ||
10002 | if Etype (Op) = Class_Wide_Type (T) then | |
10003 | return True; | |
10004 | end if; | |
10005 | ||
10006 | return False; | |
10007 | end Is_Class_Wide_Operation_Of; | |
10008 | ||
7f5e671b PMR |
10009 | -- Local variables |
10010 | ||
10011 | Ent : Entity_Id; | |
10012 | Scop : Entity_Id; | |
10013 | ||
29efbb8c ES |
10014 | -- Start of processing for Use_Class_Wide_Operations |
10015 | ||
10016 | begin | |
10017 | Scop := Scope (Typ); | |
10018 | if not Is_Hidden (Scop) then | |
10019 | Ent := First_Entity (Scop); | |
10020 | while Present (Ent) loop | |
10021 | if Is_Overloadable (Ent) | |
10022 | and then Is_Class_Wide_Operation_Of (Ent, Typ) | |
10023 | and then not Is_Potentially_Use_Visible (Ent) | |
10024 | then | |
10025 | Set_Is_Potentially_Use_Visible (Ent); | |
10026 | Append_Elmt (Ent, Used_Operations (Parent (Id))); | |
10027 | end if; | |
10028 | ||
10029 | Next_Entity (Ent); | |
10030 | end loop; | |
10031 | end if; | |
10032 | ||
10033 | if Is_Derived_Type (Typ) then | |
10034 | Use_Class_Wide_Operations (Etype (Base_Type (Typ))); | |
10035 | end if; | |
10036 | end Use_Class_Wide_Operations; | |
10037 | ||
7f5e671b PMR |
10038 | -- Local variables |
10039 | ||
10040 | Elmt : Elmt_Id; | |
10041 | Is_Known_Used : Boolean; | |
10042 | Op_List : Elist_Id; | |
10043 | T : Entity_Id; | |
10044 | ||
dea1d3dc | 10045 | -- Start of processing for Use_One_Type |
996ae0b0 RK |
10046 | |
10047 | begin | |
851e9f19 PMR |
10048 | if Entity (Id) = Any_Type then |
10049 | return; | |
10050 | end if; | |
10051 | ||
996ae0b0 RK |
10052 | -- It is the type determined by the subtype mark (8.4(8)) whose |
10053 | -- operations become potentially use-visible. | |
10054 | ||
10055 | T := Base_Type (Entity (Id)); | |
10056 | ||
851e9f19 PMR |
10057 | -- Either the type itself is used, the package where it is declared is |
10058 | -- in use or the entity is declared in the current package, thus | |
954c111a HK |
10059 | -- use-visible. |
10060 | ||
7f5e671b PMR |
10061 | Is_Known_Used := |
10062 | (In_Use (T) | |
10063 | and then ((Present (Current_Use_Clause (T)) | |
10064 | and then All_Present (Current_Use_Clause (T))) | |
10065 | or else not All_Present (Parent (Id)))) | |
10066 | or else In_Use (Scope (T)) | |
10067 | or else Scope (T) = Current_Scope; | |
954c111a HK |
10068 | |
10069 | Set_Redundant_Use (Id, | |
10070 | Is_Known_Used or else Is_Potentially_Use_Visible (T)); | |
996ae0b0 | 10071 | |
ecc4ddde AC |
10072 | if Ekind (T) = E_Incomplete_Type then |
10073 | Error_Msg_N ("premature usage of incomplete type", Id); | |
10074 | ||
10075 | elsif In_Open_Scopes (Scope (T)) then | |
996ae0b0 RK |
10076 | null; |
10077 | ||
851e9f19 | 10078 | -- A limited view cannot appear in a use_type_clause. However, an access |
f7ca1d04 AC |
10079 | -- type whose designated type is limited has the flag but is not itself |
10080 | -- a limited view unless we only have a limited view of its enclosing | |
10081 | -- package. | |
294ccb21 | 10082 | |
7b56a91b | 10083 | elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then |
923fa078 | 10084 | Error_Msg_N |
cc0b3bac AC |
10085 | ("incomplete type from limited view cannot appear in use clause", |
10086 | Id); | |
923fa078 | 10087 | |
c6840e83 AC |
10088 | -- If the use clause is redundant, Used_Operations will usually be |
10089 | -- empty, but we need to set it to empty here in one case: If we are | |
10090 | -- instantiating a generic library unit, then we install the ancestors | |
10091 | -- of that unit in the scope stack, which involves reprocessing use | |
10092 | -- clauses in those ancestors. Such a use clause will typically have a | |
10093 | -- nonempty Used_Operations unless it was redundant in the generic unit, | |
10094 | -- even if it is redundant at the place of the instantiation. | |
10095 | ||
10096 | elsif Redundant_Use (Id) then | |
851e9f19 PMR |
10097 | |
10098 | -- We must avoid incorrectly setting the Current_Use_Clause when we | |
10099 | -- are working with a redundant clause that has already been linked | |
10100 | -- in the Prev_Use_Clause chain, otherwise the chain will break. | |
10101 | ||
10102 | if Present (Current_Use_Clause (T)) | |
10103 | and then Present (Prev_Use_Clause (Current_Use_Clause (T))) | |
10104 | and then Parent (Id) = Prev_Use_Clause (Current_Use_Clause (T)) | |
10105 | then | |
10106 | null; | |
10107 | else | |
10108 | Set_Current_Use_Clause (T, Parent (Id)); | |
10109 | end if; | |
10110 | ||
c6840e83 AC |
10111 | Set_Used_Operations (Parent (Id), New_Elmt_List); |
10112 | ||
fbf5a39b AC |
10113 | -- If the subtype mark designates a subtype in a different package, |
10114 | -- we have to check that the parent type is visible, otherwise the | |
851e9f19 | 10115 | -- use_type_clause is a no-op. Not clear how to do that??? |
fbf5a39b | 10116 | |
c6840e83 | 10117 | else |
851e9f19 | 10118 | Set_Current_Use_Clause (T, Parent (Id)); |
996ae0b0 | 10119 | Set_In_Use (T); |
c3b36d48 | 10120 | |
7f5e671b | 10121 | -- If T is tagged, primitive operators on class-wide operands are |
11699257 EB |
10122 | -- also deemed available. Note that this is really necessary only |
10123 | -- in semantics-only mode, because the primitive operators are not | |
10124 | -- fully constructed in this mode, but we do it in all modes for the | |
10125 | -- sake of uniformity, as this should not matter in practice. | |
c3b36d48 AC |
10126 | |
10127 | if Is_Tagged_Type (T) then | |
10128 | Set_In_Use (Class_Wide_Type (T)); | |
10129 | end if; | |
10130 | ||
29efbb8c ES |
10131 | -- Iterate over primitive operations of the type. If an operation is |
10132 | -- already use_visible, it is the result of a previous use_clause, | |
7ff2d234 AC |
10133 | -- and already appears on the corresponding entity chain. If the |
10134 | -- clause is being reinstalled, operations are already use-visible. | |
29efbb8c | 10135 | |
7ff2d234 AC |
10136 | if Installed then |
10137 | null; | |
29efbb8c | 10138 | |
7ff2d234 AC |
10139 | else |
10140 | Op_List := Collect_Primitive_Operations (T); | |
10141 | Elmt := First_Elmt (Op_List); | |
10142 | while Present (Elmt) loop | |
10143 | if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol | |
10144 | or else Chars (Node (Elmt)) in Any_Operator_Name) | |
10145 | and then not Is_Hidden (Node (Elmt)) | |
10146 | and then not Is_Potentially_Use_Visible (Node (Elmt)) | |
10147 | then | |
10148 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
10149 | Append_Elmt (Node (Elmt), Used_Operations (Parent (Id))); | |
996ae0b0 | 10150 | |
7ff2d234 AC |
10151 | elsif Ada_Version >= Ada_2012 |
10152 | and then All_Present (Parent (Id)) | |
10153 | and then not Is_Hidden (Node (Elmt)) | |
10154 | and then not Is_Potentially_Use_Visible (Node (Elmt)) | |
10155 | then | |
10156 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
10157 | Append_Elmt (Node (Elmt), Used_Operations (Parent (Id))); | |
10158 | end if; | |
954c111a | 10159 | |
7ff2d234 AC |
10160 | Next_Elmt (Elmt); |
10161 | end loop; | |
10162 | end if; | |
10163 | ||
10164 | if Ada_Version >= Ada_2012 | |
10165 | and then All_Present (Parent (Id)) | |
10166 | and then Is_Tagged_Type (T) | |
10167 | then | |
10168 | Use_Class_Wide_Operations (T); | |
10169 | end if; | |
29efbb8c ES |
10170 | end if; |
10171 | ||
954c111a HK |
10172 | -- If warning on redundant constructs, check for unnecessary WITH |
10173 | ||
851e9f19 PMR |
10174 | if not Force |
10175 | and then Warn_On_Redundant_Constructs | |
954c111a HK |
10176 | and then Is_Known_Used |
10177 | ||
ac7d724d ES |
10178 | -- with P; with P; use P; |
10179 | -- package P is package X is package body X is | |
10180 | -- type T ... use P.T; | |
954c111a | 10181 | |
ac7d724d ES |
10182 | -- The compilation unit is the body of X. GNAT first compiles the |
10183 | -- spec of X, then proceeds to the body. At that point P is marked | |
10184 | -- as use visible. The analysis then reinstalls the spec along with | |
10185 | -- its context. The use clause P.T is now recognized as redundant, | |
10186 | -- but in the wrong context. Do not emit a warning in such cases. | |
10187 | -- Do not emit a warning either if we are in an instance, there is | |
10188 | -- no redundancy between an outer use_clause and one that appears | |
10189 | -- within the generic. | |
954c111a HK |
10190 | |
10191 | and then not Spec_Reloaded_For_Body | |
c56094bd | 10192 | and then not In_Instance |
e5148da0 | 10193 | and then not In_Inlined_Body |
954c111a HK |
10194 | then |
10195 | -- The type already has a use clause | |
10196 | ||
10197 | if In_Use (T) then | |
3ea52b2e ES |
10198 | |
10199 | -- Case where we know the current use clause for the type | |
10200 | ||
21d27997 | 10201 | if Present (Current_Use_Clause (T)) then |
44b90160 | 10202 | Use_Clause_Known : declare |
7f5e671b PMR |
10203 | Clause1 : constant Node_Id := |
10204 | Find_Most_Prev (Current_Use_Clause (T)); | |
851e9f19 | 10205 | Clause2 : constant Node_Id := Parent (Id); |
3ea52b2e ES |
10206 | Ent1 : Entity_Id; |
10207 | Ent2 : Entity_Id; | |
21d27997 RD |
10208 | Err_No : Node_Id; |
10209 | Unit1 : Node_Id; | |
10210 | Unit2 : Node_Id; | |
10211 | ||
44b90160 RD |
10212 | -- Start of processing for Use_Clause_Known |
10213 | ||
21d27997 | 10214 | begin |
851e9f19 | 10215 | -- If both current use_type_clause and the use_type_clause |
f7ca1d04 AC |
10216 | -- for the type are at the compilation unit level, one of |
10217 | -- the units must be an ancestor of the other, and the | |
10218 | -- warning belongs on the descendant. | |
3ea52b2e | 10219 | |
21d27997 | 10220 | if Nkind (Parent (Clause1)) = N_Compilation_Unit |
3ea52b2e ES |
10221 | and then |
10222 | Nkind (Parent (Clause2)) = N_Compilation_Unit | |
21d27997 | 10223 | then |
b0d3b11d AC |
10224 | -- If the unit is a subprogram body that acts as spec, |
10225 | -- the context clause is shared with the constructed | |
10226 | -- subprogram spec. Clearly there is no redundancy. | |
10227 | ||
10228 | if Clause1 = Clause2 then | |
10229 | return; | |
10230 | end if; | |
10231 | ||
3ea52b2e ES |
10232 | Unit1 := Unit (Parent (Clause1)); |
10233 | Unit2 := Unit (Parent (Clause2)); | |
10234 | ||
32beb1f3 AC |
10235 | -- If both clauses are on same unit, or one is the body |
10236 | -- of the other, or one of them is in a subunit, report | |
10237 | -- redundancy on the later one. | |
f7ca1d04 | 10238 | |
851e9f19 | 10239 | if Unit1 = Unit2 or else Nkind (Unit1) = N_Subunit then |
32beb1f3 | 10240 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); |
ed2233dc | 10241 | Error_Msg_NE -- CODEFIX |
32beb1f3 | 10242 | ("& is already use-visible through previous " |
dbfeb4fa | 10243 | & "use_type_clause #??", Clause1, T); |
32beb1f3 AC |
10244 | return; |
10245 | ||
10246 | elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body) | |
10247 | and then Nkind (Unit1) /= Nkind (Unit2) | |
10248 | and then Nkind (Unit1) /= N_Subunit | |
10249 | then | |
10250 | Error_Msg_Sloc := Sloc (Clause1); | |
ed2233dc | 10251 | Error_Msg_NE -- CODEFIX |
32beb1f3 | 10252 | ("& is already use-visible through previous " |
dbfeb4fa | 10253 | & "use_type_clause #??", Current_Use_Clause (T), T); |
32beb1f3 | 10254 | return; |
f7ca1d04 AC |
10255 | end if; |
10256 | ||
851e9f19 | 10257 | -- There is a redundant use_type_clause in a child unit. |
21d27997 | 10258 | -- Determine which of the units is more deeply nested. |
3ea52b2e | 10259 | -- If a unit is a package instance, retrieve the entity |
7ca139d3 | 10260 | -- and its scope from the instance spec. |
21d27997 | 10261 | |
7ca139d3 TQ |
10262 | Ent1 := Entity_Of_Unit (Unit1); |
10263 | Ent2 := Entity_Of_Unit (Unit2); | |
3ea52b2e | 10264 | |
bcb0389e | 10265 | if Scope (Ent2) = Standard_Standard then |
21d27997 RD |
10266 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); |
10267 | Err_No := Clause1; | |
10268 | ||
3ea52b2e | 10269 | elsif Scope (Ent1) = Standard_Standard then |
21d27997 RD |
10270 | Error_Msg_Sloc := Sloc (Id); |
10271 | Err_No := Clause2; | |
10272 | ||
7ca139d3 TQ |
10273 | -- If both units are child units, we determine which one |
10274 | -- is the descendant by the scope distance to the | |
3ea52b2e | 10275 | -- ultimate parent unit. |
21d27997 | 10276 | |
3ea52b2e | 10277 | else |
21d27997 | 10278 | declare |
7f5e671b PMR |
10279 | S1 : Entity_Id; |
10280 | S2 : Entity_Id; | |
21d27997 RD |
10281 | |
10282 | begin | |
3ea52b2e ES |
10283 | S1 := Scope (Ent1); |
10284 | S2 := Scope (Ent2); | |
c97c0163 AC |
10285 | while Present (S1) |
10286 | and then Present (S2) | |
10287 | and then S1 /= Standard_Standard | |
10288 | and then S2 /= Standard_Standard | |
21d27997 RD |
10289 | loop |
10290 | S1 := Scope (S1); | |
10291 | S2 := Scope (S2); | |
10292 | end loop; | |
10293 | ||
10294 | if S1 = Standard_Standard then | |
10295 | Error_Msg_Sloc := Sloc (Id); | |
10296 | Err_No := Clause2; | |
10297 | else | |
10298 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); | |
10299 | Err_No := Clause1; | |
10300 | end if; | |
10301 | end; | |
10302 | end if; | |
10303 | ||
851e9f19 PMR |
10304 | if Parent (Id) /= Err_No then |
10305 | if Most_Descendant_Use_Clause | |
10306 | (Err_No, Parent (Id)) = Parent (Id) | |
10307 | then | |
10308 | Error_Msg_Sloc := Sloc (Err_No); | |
10309 | Err_No := Parent (Id); | |
10310 | end if; | |
10311 | ||
10312 | Error_Msg_NE -- CODEFIX | |
10313 | ("& is already use-visible through previous " | |
10314 | & "use_type_clause #??", Err_No, Id); | |
10315 | end if; | |
3ea52b2e | 10316 | |
851e9f19 PMR |
10317 | -- Case where current use_type_clause and use_type_clause |
10318 | -- for the type are not both at the compilation unit level. | |
10319 | -- In this case we don't have location information. | |
3ea52b2e | 10320 | |
21d27997 | 10321 | else |
ed2233dc | 10322 | Error_Msg_NE -- CODEFIX |
3ea52b2e | 10323 | ("& is already use-visible through previous " |
f192ca5e | 10324 | & "use_type_clause??", Id, T); |
21d27997 | 10325 | end if; |
44b90160 | 10326 | end Use_Clause_Known; |
3ea52b2e | 10327 | |
7f5e671b PMR |
10328 | -- Here if Current_Use_Clause is not set for T, another case where |
10329 | -- we do not have the location information available. | |
3ea52b2e | 10330 | |
21d27997 | 10331 | else |
ed2233dc | 10332 | Error_Msg_NE -- CODEFIX |
3ea52b2e | 10333 | ("& is already use-visible through previous " |
f192ca5e | 10334 | & "use_type_clause??", Id, T); |
21d27997 | 10335 | end if; |
954c111a HK |
10336 | |
10337 | -- The package where T is declared is already used | |
10338 | ||
10339 | elsif In_Use (Scope (T)) then | |
7f5e671b PMR |
10340 | Error_Msg_Sloc := |
10341 | Sloc (Find_Most_Prev (Current_Use_Clause (Scope (T)))); | |
ed2233dc | 10342 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 10343 | ("& is already use-visible through package use clause #??", |
37951d8e | 10344 | Id, T); |
954c111a HK |
10345 | |
10346 | -- The current scope is the package where T is declared | |
10347 | ||
10348 | else | |
10349 | Error_Msg_Node_2 := Scope (T); | |
ed2233dc | 10350 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 10351 | ("& is already use-visible inside package &??", Id, T); |
954c111a HK |
10352 | end if; |
10353 | end if; | |
996ae0b0 RK |
10354 | end Use_One_Type; |
10355 | ||
10356 | ---------------- | |
10357 | -- Write_Info -- | |
10358 | ---------------- | |
10359 | ||
10360 | procedure Write_Info is | |
10361 | Id : Entity_Id := First_Entity (Current_Scope); | |
10362 | ||
10363 | begin | |
10364 | -- No point in dumping standard entities | |
10365 | ||
10366 | if Current_Scope = Standard_Standard then | |
10367 | return; | |
10368 | end if; | |
10369 | ||
10370 | Write_Str ("========================================================"); | |
10371 | Write_Eol; | |
10372 | Write_Str (" Defined Entities in "); | |
10373 | Write_Name (Chars (Current_Scope)); | |
10374 | Write_Eol; | |
10375 | Write_Str ("========================================================"); | |
10376 | Write_Eol; | |
10377 | ||
10378 | if No (Id) then | |
10379 | Write_Str ("-- none --"); | |
10380 | Write_Eol; | |
10381 | ||
10382 | else | |
10383 | while Present (Id) loop | |
10384 | Write_Entity_Info (Id, " "); | |
10385 | Next_Entity (Id); | |
10386 | end loop; | |
10387 | end if; | |
10388 | ||
10389 | if Scope (Current_Scope) = Standard_Standard then | |
10390 | ||
10391 | -- Print information on the current unit itself | |
10392 | ||
10393 | Write_Entity_Info (Current_Scope, " "); | |
10394 | end if; | |
10395 | ||
10396 | Write_Eol; | |
10397 | end Write_Info; | |
10398 | ||
67536dcb RD |
10399 | -------- |
10400 | -- ws -- | |
10401 | -------- | |
996ae0b0 | 10402 | |
67536dcb | 10403 | procedure ws is |
996ae0b0 | 10404 | S : Entity_Id; |
996ae0b0 RK |
10405 | begin |
10406 | for J in reverse 1 .. Scope_Stack.Last loop | |
c8307596 | 10407 | S := Scope_Stack.Table (J).Entity; |
996ae0b0 RK |
10408 | Write_Int (Int (S)); |
10409 | Write_Str (" === "); | |
10410 | Write_Name (Chars (S)); | |
10411 | Write_Eol; | |
10412 | end loop; | |
67536dcb | 10413 | end ws; |
996ae0b0 | 10414 | |
c1ce0691 ES |
10415 | -------- |
10416 | -- we -- | |
10417 | -------- | |
10418 | ||
10419 | procedure we (S : Entity_Id) is | |
10420 | E : Entity_Id; | |
10421 | begin | |
10422 | E := First_Entity (S); | |
10423 | while Present (E) loop | |
10424 | Write_Int (Int (E)); | |
10425 | Write_Str (" === "); | |
10426 | Write_Name (Chars (E)); | |
10427 | Write_Eol; | |
c1ce0691 ES |
10428 | Next_Entity (E); |
10429 | end loop; | |
10430 | end we; | |
996ae0b0 | 10431 | end Sem_Ch8; |