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